From: Matthieu Moy <Matthieu.Moy@imag.fr>
To: git@vger.kernel.org, gitster@pobox.com
Cc: Matthieu Moy <Matthieu.Moy@imag.fr>
Subject: [PATCH 1/2] branch: allow creating a branch with same name and same starting point.
Date: Sun, 17 Jan 2010 15:06:51 +0100 [thread overview]
Message-ID: <1263737212-8101-2-git-send-email-Matthieu.Moy@imag.fr> (raw)
In-Reply-To: <1263737212-8101-1-git-send-email-Matthieu.Moy@imag.fr>
Previously, "git branch --track newname old" was rejected if newname
existed, even if it already had the same value. This patch allows it,
even without --force. This has two advantages:
* Not requiring --force for a safe operation, hence allowing the user to
benefit from the other safety checks.
* Allow changing the configuration of the checked-out branch.
As a result, "git branch --track" becomes a conveinient and safe way to
set up upstream information for an existing branch.
The error message "A branch named '%s' already exists" is no longer
precise enough to refuse to act, since part of the reason of the failure
is the old branch value, so we include this value in the message.
Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
branch.c | 45 ++++++++++++++++++++++++++++++---------------
t/t6040-tracking-info.sh | 8 ++++++++
2 files changed, 38 insertions(+), 15 deletions(-)
diff --git a/branch.c b/branch.c
index 05ef3f5..d187891 100644
--- a/branch.c
+++ b/branch.c
@@ -128,27 +128,39 @@ void create_branch(const char *head,
const char *name, const char *start_name,
int force, int reflog, enum branch_track track)
{
- struct ref_lock *lock;
+ struct ref_lock *lock = NULL;
struct commit *commit;
- unsigned char sha1[20];
+ unsigned char old_sha1[20], sha1[20];
char *real_ref, msg[PATH_MAX + 20];
struct strbuf ref = STRBUF_INIT;
int forcing = 0;
+ int dont_change_ref = 0;
if (strbuf_check_branch_ref(&ref, name))
die("'%s' is not a valid branch name.", name);
- if (resolve_ref(ref.buf, sha1, 1, NULL)) {
- if (!force)
- die("A branch named '%s' already exists.", name);
- else if (!is_bare_repository() && !strcmp(head, name))
- die("Cannot force update the current branch.");
+ if (get_sha1(start_name, sha1))
+ die("Not a valid object name: '%s'.", start_name);
+
+ if (resolve_ref(ref.buf, old_sha1, 1, NULL)) {
+ if (hashcmp(old_sha1, sha1)) {
+ if (!force)
+ die("A branch named '%s' already exists pointing to %.*s.",
+ name, 8, sha1_to_hex(old_sha1));
+ else if (!is_bare_repository() && !strcmp(head, name))
+ die("Cannot force update the current branch.");
+ } else {
+ /*
+ * Re-creating a branch with the same value.
+ * Safe, and usefull to set up the upstream
+ * branch without touching the ref.
+ */
+ dont_change_ref = 1;
+ }
forcing = 1;
}
real_ref = NULL;
- if (get_sha1(start_name, sha1))
- die("Not a valid object name: '%s'.", start_name);
switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
case 0:
@@ -170,9 +182,11 @@ void create_branch(const char *head,
die("Not a valid branch point: '%s'.", start_name);
hashcpy(sha1, commit->object.sha1);
- lock = lock_any_ref_for_update(ref.buf, NULL, 0);
- if (!lock)
- die_errno("Failed to lock ref for update");
+ if (!dont_change_ref) {
+ lock = lock_any_ref_for_update(ref.buf, NULL, 0);
+ if (!lock)
+ die_errno("Failed to lock ref for update");
+ }
if (reflog)
log_all_ref_updates = 1;
@@ -180,15 +194,16 @@ void create_branch(const char *head,
if (forcing)
snprintf(msg, sizeof msg, "branch: Reset from %s",
start_name);
- else
+ else if (!dont_change_ref)
snprintf(msg, sizeof msg, "branch: Created from %s",
start_name);
if (real_ref && track)
setup_tracking(name, real_ref, track);
- if (write_ref_sha1(lock, sha1, msg) < 0)
- die_errno("Failed to write ref");
+ if (!dont_change_ref)
+ if (write_ref_sha1(lock, sha1, msg) < 0)
+ die_errno("Failed to write ref");
strbuf_release(&ref);
free(real_ref);
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 664b0f8..3c200b6 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -89,4 +89,12 @@ test_expect_success 'status when tracking annotated tags' '
grep "set up to track" actual &&
git checkout heavytrack
'
+
+test_expect_success 'setup tracking with branch --track on existing branch' '
+ git branch from-master master &&
+ test_must_fail git config branch.from-master.merge > actual &&
+ git branch from-master master --track &&
+ git config branch.from-master.merge > actual &&
+ grep -q "^refs/heads/master$" actual
+'
test_done
--
1.6.6.198.gbaea2
next prev parent reply other threads:[~2010-01-17 14:07 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-17 14:06 [PATCH 0/2] Make it easy to use branch --track on existing branch Matthieu Moy
2010-01-17 14:06 ` Matthieu Moy [this message]
2010-01-17 19:38 ` [PATCH 1/2] branch: allow creating a branch with same name and same starting point Junio C Hamano
2010-01-17 14:06 ` [PATCH 2/2] branch: warn and refuse to set a branch as a tracking branch of itself Matthieu Moy
2010-01-17 14:40 ` [PATCH 0/2] Make it easy to use branch --track on existing branch Ilari Liusvaara
2010-01-17 14:53 ` Matthieu Moy
2010-01-17 15:29 ` Ilari Liusvaara
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=1263737212-8101-2-git-send-email-Matthieu.Moy@imag.fr \
--to=matthieu.moy@imag.fr \
--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).