git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jim Hill <gjthill@gmail.com>
To: Junio Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>
Cc: git@vger.kernel.org
Subject: [PATCH 1/2] add_to_alternates_file: don't add duplicate paths
Date: Sun, 31 May 2015 11:15:22 -0700	[thread overview]
Message-ID: <1433096123-14420-2-git-send-email-gjthill@gmail.com> (raw)
In-Reply-To: <1433096123-14420-1-git-send-email-gjthill@gmail.com>

Check for an existing match before appending a path to the alternates
file.  Beyond making git look smart to anyone checking the alternates
file, this removes the last use of hold_lock_file_for_append.

Signed-off-by: Jim Hill <gjthill@gmail.com>
---
 sha1_file.c                | 29 +++++++++++++++++++++++++----
 t/t5700-clone-reference.sh |  4 ++--
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/sha1_file.c b/sha1_file.c
index 47f56f2..43d9530 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -403,14 +403,35 @@ void read_info_alternates(const char * relative_base, int depth)
 
 void add_to_alternates_file(const char *reference)
 {
-	struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
-	int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), LOCK_DIE_ON_ERROR);
+	static struct lock_file lock = {0};
 	char *alt = mkpath("%s\n", reference);
+	char *alts = git_path("objects/info/alternates");
+	int fd = hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
+	struct strbuf altdata = STRBUF_INIT;
+	struct string_list lines = STRING_LIST_INIT_NODUP;
+
+	if (strbuf_read_file(&altdata, alts, 0) < 0)
+		if (errno != ENOENT)
+			die("alternates file unreadable");
+	strbuf_complete_line(&altdata);
+	write_or_die(fd, altdata.buf, altdata.len);
+
+	string_list_split_in_place(&lines, altdata.buf, '\n', -1);
+	lines.cmp = strcmp_icase;
+	if (unsorted_string_list_has_string(&lines, reference)) {
+		rollback_lock_file(&lock);
+		goto cleanup;
+	}
+
 	write_or_die(fd, alt, strlen(alt));
-	if (commit_lock_file(lock))
-		die("could not close alternates file");
+	if (commit_lock_file(&lock))
+		die("could not update alternates file");
 	if (alt_odb_tail)
 		link_alt_odb_entries(alt, strlen(alt), '\n', NULL, 0);
+
+cleanup:
+	strbuf_reset(&altdata);
+	string_list_clear(&lines,0);
 }
 
 int foreach_alt_odb(alt_odb_fn fn, void *cb)
diff --git a/t/t5700-clone-reference.sh b/t/t5700-clone-reference.sh
index 3e783fc..cd9fa34 100755
--- a/t/t5700-clone-reference.sh
+++ b/t/t5700-clone-reference.sh
@@ -29,11 +29,11 @@ git prune'
 cd "$base_dir"
 
 test_expect_success 'cloning with reference (-l -s)' \
-'git clone -l -s --reference B A C'
+'git clone -l -s --reference B --reference A --reference B A C'
 
 cd "$base_dir"
 
-test_expect_success 'existence of info/alternates' \
+test_expect_success 'existence of info/alternates, no duplicates' \
 'test_line_count = 2 C/.git/objects/info/alternates'
 
 cd "$base_dir"
-- 
2.4.1.4.gfc728c2

  reply	other threads:[~2015-05-31 18:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-31 18:15 [PATCH 0/2] don't add duplicate paths to info/alternates Jim Hill
2015-05-31 18:15 ` Jim Hill [this message]
2015-06-01 10:51   ` [PATCH 1/2] add_to_alternates_file: don't add duplicate paths Jeff King
2015-05-31 18:15 ` [PATCH 2/2] remove hold_lock_file_for_append Jim Hill
2015-06-01 10:53 ` [PATCH 0/2] don't add duplicate paths to info/alternates Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1433096123-14420-2-git-send-email-gjthill@gmail.com \
    --to=gjthill@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).