{"thread":{"id":"22272","subject":"[PATCH 0/2] Branch --set-upstream","startedAt":"2010-01-18T20:44:10Z","lastAt":"2010-01-19T07:22:26Z","messageCount":4,"participants":["Ilari Liusvaara","Matthieu Moy"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"132047","messageId":"1263847452-29634-1-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"22272","inReplyTo":null,"subject":"[PATCH 0/2] Branch --set-upstream","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-01-18T20:44:10Z","receivedAt":"2010-01-18T20:44:10Z","isPatch":true,"sender":{"key":"ilari.liusvaara@elisanet.fi","avatar":null},"body":"This adds new switch --set-upstream that can set upstream information\non existing branch branch without changing where the branch points\nto.\n\npush --set-upstream doesn't help if user doesn't have push priviledges\nor if user is not up to date with upstream. Forcing branch recreate\nis not possible if user has own patches appiled to the branch.\n\nThis series is edited version of Matthieu Moy's branch --track patch\nseries. The first patch is edited, the second is resent as-is.\n\nIlari Liusvaara (1):\n  Add branch --set-upstream\n\nMatthieu Moy (1):\n  branch: warn and refuse to set a branch as a tracking branch of\n    itself.\n\n Documentation/git-branch.txt |    8 ++++++-\n branch.c                     |   45 ++++++++++++++++++++++++++++++-----------\n builtin-branch.c             |    2 +\n cache.h                      |    1 +\n t/t6040-tracking-info.sh     |   21 +++++++++++++++++++\n 5 files changed, 64 insertions(+), 13 deletions(-)\n"},{"id":"132048","messageId":"1263847452-29634-2-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"22272","inReplyTo":"1263847452-29634-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[PATCH 1/2] Add branch --set-upstream","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-01-18T20:44:11Z","receivedAt":"2010-01-18T20:44:11Z","isPatch":true,"sender":{"key":"ilari.liusvaara@elisanet.fi","avatar":null},"body":"Add --set-upstream option to branch that works like --track, except that\nwhen branch exists already, its upstream info is changed without changing\nthe ref value.\n\nBased-on-patch-from: Matthieu Moy <Matthieu.Moy@imag.fr>\nSigned-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>\nSigned-off-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>\n---\n Documentation/git-branch.txt |    8 +++++++-\n branch.c                     |   31 +++++++++++++++++++++----------\n builtin-branch.c             |    2 ++\n cache.h                      |    1 +\n t/t6040-tracking-info.sh     |   21 +++++++++++++++++++++\n 5 files changed, 52 insertions(+), 11 deletions(-)\n\ndiff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt\nindex 0e83680..a0d6a7a 100644\n--- a/Documentation/git-branch.txt\n+++ b/Documentation/git-branch.txt\n@@ -11,7 +11,7 @@ SYNOPSIS\n 'git branch' [--color | --no-color] [-r | -a]\n \t[-v [--abbrev=<length> | --no-abbrev]]\n \t[(--merged | --no-merged | --contains) [<commit>]]\n-'git branch' [--track | --no-track] [-l] [-f] <branchname> [<start-point>]\n+'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]\n 'git branch' (-m | -M) [<oldbranch>] <newbranch>\n 'git branch' (-d | -D) [-r] <branchname>...\n \n@@ -129,6 +129,12 @@ start-point is either a local or remote branch.\n \tDo not set up \"upstream\" configuration, even if the\n \tbranch.autosetupmerge configuration variable is true.\n \n+--set-upstream::\n+\tIf specified branch does not exist yet or if '--force' has been\n+\tgiven, acts exactly like '--track'. Otherwise sets up configuration\n+\tlike '--track' would when creating the branch, except that where\n+\tbranch points to is not changed.\n+\n --contains <commit>::\n \tOnly list branches which contain the specified commit.\n \ndiff --git a/branch.c b/branch.c\nindex 05ef3f5..40d3c45 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -108,6 +108,7 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,\n \t\tswitch (track) {\n \t\tcase BRANCH_TRACK_ALWAYS:\n \t\tcase BRANCH_TRACK_EXPLICIT:\n+\t\tcase BRANCH_TRACK_OVERRIDE:\n \t\t\tbreak;\n \t\tdefault:\n \t\t\treturn 1;\n@@ -128,18 +129,25 @@ void create_branch(const char *head,\n \t\t   const char *name, const char *start_name,\n \t\t   int force, int reflog, enum branch_track track)\n {\n-\tstruct ref_lock *lock;\n+\tstruct ref_lock *lock = NULL;\n \tstruct commit *commit;\n \tunsigned char sha1[20];\n \tchar *real_ref, msg[PATH_MAX + 20];\n \tstruct strbuf ref = STRBUF_INIT;\n \tint forcing = 0;\n+\tint dont_change_ref = 0;\n+\tint explicit_tracking = 0;\n+\n+\tif (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)\n+\t\texplicit_tracking = 1;\n \n \tif (strbuf_check_branch_ref(&ref, name))\n \t\tdie(\"'%s' is not a valid branch name.\", name);\n \n \tif (resolve_ref(ref.buf, sha1, 1, NULL)) {\n-\t\tif (!force)\n+\t\tif (!force && track == BRANCH_TRACK_OVERRIDE)\n+\t\t\tdont_change_ref = 1;\n+\t\telse if (!force)\n \t\t\tdie(\"A branch named '%s' already exists.\", name);\n \t\telse if (!is_bare_repository() && !strcmp(head, name))\n \t\t\tdie(\"Cannot force update the current branch.\");\n@@ -153,12 +161,12 @@ void create_branch(const char *head,\n \tswitch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {\n \tcase 0:\n \t\t/* Not branching from any existing branch */\n-\t\tif (track == BRANCH_TRACK_EXPLICIT)\n+\t\tif (explicit_tracking)\n \t\t\tdie(\"Cannot setup tracking information; starting point is not a branch.\");\n \t\tbreak;\n \tcase 1:\n \t\t/* Unique completion -- good, only if it is a real ref */\n-\t\tif (track == BRANCH_TRACK_EXPLICIT && !strcmp(real_ref, \"HEAD\"))\n+\t\tif (explicit_tracking && !strcmp(real_ref, \"HEAD\"))\n \t\t\tdie(\"Cannot setup tracking information; starting point is not a branch.\");\n \t\tbreak;\n \tdefault:\n@@ -170,9 +178,11 @@ void create_branch(const char *head,\n \t\tdie(\"Not a valid branch point: '%s'.\", start_name);\n \thashcpy(sha1, commit->object.sha1);\n \n-\tlock = lock_any_ref_for_update(ref.buf, NULL, 0);\n-\tif (!lock)\n-\t\tdie_errno(\"Failed to lock ref for update\");\n+\tif (!dont_change_ref) {\n+\t\tlock = lock_any_ref_for_update(ref.buf, NULL, 0);\n+\t\tif (!lock)\n+\t\t\tdie_errno(\"Failed to lock ref for update\");\n+\t}\n \n \tif (reflog)\n \t\tlog_all_ref_updates = 1;\n@@ -180,15 +190,16 @@ void create_branch(const char *head,\n \tif (forcing)\n \t\tsnprintf(msg, sizeof msg, \"branch: Reset from %s\",\n \t\t\t start_name);\n-\telse\n+\telse if (!dont_change_ref)\n \t\tsnprintf(msg, sizeof msg, \"branch: Created from %s\",\n \t\t\t start_name);\n \n \tif (real_ref && track)\n \t\tsetup_tracking(name, real_ref, track);\n \n-\tif (write_ref_sha1(lock, sha1, msg) < 0)\n-\t\tdie_errno(\"Failed to write ref\");\n+\tif (!dont_change_ref)\n+\t\tif (write_ref_sha1(lock, sha1, msg) < 0)\n+\t\t\tdie_errno(\"Failed to write ref\");\n \n \tstrbuf_release(&ref);\n \tfree(real_ref);\ndiff --git a/builtin-branch.c b/builtin-branch.c\nindex ddc9f2d..970cf31 100644\n--- a/builtin-branch.c\n+++ b/builtin-branch.c\n@@ -564,6 +564,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)\n \t\tOPT__VERBOSE(&verbose),\n \t\tOPT_SET_INT('t', \"track\",  &track, \"set up tracking mode (see git-pull(1))\",\n \t\t\tBRANCH_TRACK_EXPLICIT),\n+\t\tOPT_SET_INT( 0, \"set-upstream\",  &track, \"change upstream info\",\n+\t\t\tBRANCH_TRACK_OVERRIDE),\n \t\tOPT_BOOLEAN( 0 , \"color\",  &branch_use_color, \"use colored output\"),\n \t\tOPT_SET_INT('r', NULL,     &kinds, \"act on remote-tracking branches\",\n \t\t\tREF_REMOTE_BRANCH),\ndiff --git a/cache.h b/cache.h\nindex cf36c81..e9ec537 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -553,6 +553,7 @@ enum branch_track {\n \tBRANCH_TRACK_REMOTE,\n \tBRANCH_TRACK_ALWAYS,\n \tBRANCH_TRACK_EXPLICIT,\n+\tBRANCH_TRACK_OVERRIDE,\n };\n \n enum rebase_setup_type {\ndiff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh\nindex 664b0f8..1785e17 100755\n--- a/t/t6040-tracking-info.sh\n+++ b/t/t6040-tracking-info.sh\n@@ -89,4 +89,25 @@ test_expect_success 'status when tracking annotated tags' '\n \tgrep \"set up to track\" actual &&\n \tgit checkout heavytrack\n '\n+\n+test_expect_success 'setup tracking with branch --set-upstream on existing branch' '\n+\tgit branch from-master master &&\n+\ttest_must_fail git config branch.from-master.merge > actual &&\n+\tgit branch --set-upstream from-master master &&\n+\tgit config branch.from-master.merge > actual &&\n+\tgrep -q \"^refs/heads/master$\" actual\n+'\n+\n+test_expect_success '--set-upstream does not change branch' '\n+\tgit branch from-master2 master &&\n+\ttest_must_fail git config branch.from-master2.merge > actual &&\n+\tgit rev-list from-master2 &&\n+\tgit update-ref refs/heads/from-master2 from-master2^ &&\n+\tgit rev-parse from-master2 >expect2 &&\n+\tgit branch --set-upstream from-master2 master &&\n+\tgit config branch.from-master.merge > actual &&\n+\tgit rev-parse from-master2 >actual2 &&\n+\tgrep -q \"^refs/heads/master$\" actual &&\n+\tcmp expect2 actual2\n+'\n test_done\n-- \n1.6.6.199.gff4b0\n"},{"id":"132049","messageId":"1263847452-29634-3-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"22272","inReplyTo":"1263847452-29634-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[PATCH 2/2] branch: warn and refuse to set a branch as a tracking branch of itself.","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-01-18T20:44:12Z","receivedAt":"2010-01-18T20:44:12Z","isPatch":true,"sender":{"key":"ilari.liusvaara@elisanet.fi","avatar":null},"body":"From: Matthieu Moy <Matthieu.Moy@imag.fr>\n\nPrevious patch allows commands like \"git branch foo foo --track\", which\ndoesn't make much sense. Warn the user and don't change the configuration\nin this case. Don't die to let the caller finish its job in such case.\n\nSigned-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>\nSigned-off-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>\n---\n branch.c |   14 ++++++++++++--\n 1 files changed, 12 insertions(+), 2 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 40d3c45..9e1f63e 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -49,9 +49,19 @@ static int should_setup_rebase(const char *origin)\n \n void install_branch_config(int flag, const char *local, const char *origin, const char *remote)\n {\n+\tconst char *shortname = remote + 11;\n+\tint remote_is_branch = !prefixcmp(remote, \"refs/heads/\");\n \tstruct strbuf key = STRBUF_INIT;\n \tint rebasing = should_setup_rebase(origin);\n \n+\tif (remote_is_branch\n+\t    && !strcmp(local, shortname)\n+\t    && !origin) {\n+\t\twarning(\"Not setting branch %s as its own upstream.\",\n+\t\t\tlocal);\n+\t\treturn;\n+\t}\n+\n \tstrbuf_addf(&key, \"branch.%s.remote\", local);\n \tgit_config_set(key.buf, origin ? origin : \".\");\n \n@@ -71,8 +81,8 @@ void install_branch_config(int flag, const char *local, const char *origin, cons\n \t\tstrbuf_addstr(&key, origin ? \"remote\" : \"local\");\n \n \t\t/* Are we tracking a proper \"branch\"? */\n-\t\tif (!prefixcmp(remote, \"refs/heads/\")) {\n-\t\t\tstrbuf_addf(&key, \" branch %s\", remote + 11);\n+\t\tif (remote_is_branch) {\n+\t\t\tstrbuf_addf(&key, \" branch %s\", shortname);\n \t\t\tif (origin)\n \t\t\t\tstrbuf_addf(&key, \" from %s\", origin);\n \t\t}\n-- \n1.6.6.199.gff4b0\n"},{"id":"132084","messageId":"vpqy6juy2x9.fsf@bauges.imag.fr","threadId":"22272","inReplyTo":"1263847452-29634-2-git-send-email-ilari.liusvaara@elisanet.fi","subject":"Re: [PATCH 1/2] Add branch --set-upstream","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2010-01-19T07:22:26Z","receivedAt":"2010-01-19T07:22:26Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Ilari Liusvaara <ilari.liusvaara@elisanet.fi> writes:\n\n> Add --set-upstream option to branch that works like --track, except that\n> when branch exists already, its upstream info is changed without changing\n> the ref value.\n>\n> Based-on-patch-from: Matthieu Moy <Matthieu.Moy@imag.fr>\n> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>\n\nI'm short on Git time budget, so I happily let you take over on this\npatch.\n\nI'm a bit worried about the vocabulary: my understanding is that\n\"--track\" is considered confusing, and should have been called\n\"--set-upstream\" since the beginning.\n\nWith your patch, we have both --set-upstream and --track, with\ndifferent meanings. Should we consider --track as semi-deprecated, and\n--set-upstream as \"--track done right\"? If so, you can consider\nsquashing this to promote your option instead of --track, and perhaps\nreword git-branch.txt so that --track says it does like\n--set-upstream, and not the other way around.\n\ndiff --git a/Documentation/user-manual.txt b/Documentation/user-manual.txt\nindex b169836..4c66f19 100644\n--- a/Documentation/user-manual.txt\n+++ b/Documentation/user-manual.txt\n@@ -2114,12 +2114,12 @@ linkgit:git-fetch[1] to keep them up-to-date; see\n \n Now create the branches in which you are going to work; these start out\n at the current tip of origin/master branch, and should be set up (using\n-the --track option to linkgit:git-branch[1]) to merge changes in from\n+the --set-upstream option to linkgit:git-branch[1]) to merge changes in from\n Linus by default.\n \n -------------------------------------------------\n-$ git branch --track test origin/master\n-$ git branch --track release origin/master\n+$ git branch --set-upstream test origin/master\n+$ git branch --set-upstream release origin/master\n -------------------------------------------------\n \n These can be easily kept up to date using linkgit:git-pull[1].\n\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"}]}