git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Haggerty <mhagger@alum.mit.edu>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Aman Gupta <aman@tmm1.net>,
	Brad King <brad.king@kitware.com>,
	Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH] update-ref: fail create operation over stdin if ref already exists
Date: Wed,  2 Apr 2014 10:09:54 +0200	[thread overview]
Message-ID: <1396426194-3292-1-git-send-email-mhagger@alum.mit.edu> (raw)

From: Aman Gupta <aman@tmm1.net>

Signed-off-by: Aman Gupta <aman@tmm1.net>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
My colleague Aman ran across this bug and wrote the fix.  I didn't
notice this bug, but I just verified that it is also fixed by my
mh/ref-transaction patch series (albeit without a test case).

Because the bug could cause somebody to overwrite a reference
unintentionally, I propose that we apply this unintrusive fix to
maint.  When mh/ref-transaction makes it to a release, the bug will
continue to be fixed, but in a different way.

 builtin/update-ref.c  |  1 +
 t/t1400-update-ref.sh | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 1292cfe..5c208bb 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -147,6 +147,7 @@ static void parse_cmd_create(const char *next)
 	struct ref_update *update;
 
 	update = update_alloc();
+	update->have_old = 1;
 
 	if ((next = parse_first_arg(next, &ref)) != NULL && ref.buf[0])
 		update_store_ref_name(update, ref.buf);
diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
index 6ffd82f..e130c52 100755
--- a/t/t1400-update-ref.sh
+++ b/t/t1400-update-ref.sh
@@ -820,7 +820,18 @@ test_expect_success 'stdin -z update ref fails with bad old value' '
 	test_must_fail git rev-parse --verify -q $c
 '
 
+test_expect_success 'stdin -z create ref fails when ref exists' '
+	git update-ref $c $m &&
+	git rev-parse "$c" >expect &&
+	printf $F "create $c" "$m~1" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
+	git rev-parse "$c" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'stdin -z create ref fails with bad new value' '
+	git update-ref -d "$c" &&
 	printf $F "create $c" "does-not-exist" >stdin &&
 	test_must_fail git update-ref -z --stdin <stdin 2>err &&
 	grep "fatal: invalid new value for ref $c: does-not-exist" err &&
-- 
1.9.0

             reply	other threads:[~2014-04-03  9:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-02  8:09 Michael Haggerty [this message]
2014-04-02 12:57 ` [PATCH] update-ref: fail create operation over stdin if ref already exists Brad King
2014-04-03 13:20   ` Michael Haggerty

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=1396426194-3292-1-git-send-email-mhagger@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=aman@tmm1.net \
    --cc=brad.king@kitware.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).