Skip to content

Commit c2058ea

Browse files
committed
Merge branch 'rj/branch-edit-description-with-nth-checkout'
"git branch --edit-description @{-1}" is now a way to edit branch description of the branch you were on before switching to the current branch. * rj/branch-edit-description-with-nth-checkout: branch: support for shortcuts like @{-1}, completed
2 parents 1f20aa2 + 0dc4e5c commit c2058ea

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

builtin/branch.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -800,31 +800,33 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
800800
} else if (edit_description) {
801801
const char *branch_name;
802802
struct strbuf branch_ref = STRBUF_INIT;
803+
struct strbuf buf = STRBUF_INIT;
804+
int ret = 1; /* assume failure */
803805

804806
if (!argc) {
805807
if (filter.detached)
806808
die(_("Cannot give description to detached HEAD"));
807809
branch_name = head;
808-
} else if (argc == 1)
809-
branch_name = argv[0];
810-
else
810+
} else if (argc == 1) {
811+
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
812+
branch_name = buf.buf;
813+
} else {
811814
die(_("cannot edit description of more than one branch"));
815+
}
812816

813817
strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
814-
if (!ref_exists(branch_ref.buf)) {
815-
strbuf_release(&branch_ref);
816-
817-
if (!argc || !strcmp(head, branch_name))
818-
return error(_("No commit on branch '%s' yet."),
819-
branch_name);
820-
else
821-
return error(_("No branch named '%s'."),
822-
branch_name);
823-
}
818+
if (!ref_exists(branch_ref.buf))
819+
ret = error((!argc || !strcmp(head, branch_name))
820+
? _("No commit on branch '%s' yet.")
821+
: _("No branch named '%s'."),
822+
branch_name);
823+
else if (!edit_branch_description(branch_name))
824+
ret = 0; /* happy */
825+
824826
strbuf_release(&branch_ref);
827+
strbuf_release(&buf);
825828

826-
if (edit_branch_description(branch_name))
827-
return 1;
829+
return ret;
828830
} else if (copy) {
829831
if (!argc)
830832
die(_("branch name required"));
@@ -844,9 +846,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
844846
else
845847
die(_("too many arguments for a rename operation"));
846848
} else if (new_upstream) {
847-
struct branch *branch = branch_get(argv[0]);
849+
struct branch *branch;
850+
struct strbuf buf = STRBUF_INIT;
848851

849-
if (argc > 1)
852+
if (!argc)
853+
branch = branch_get(NULL);
854+
else if (argc == 1) {
855+
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
856+
branch = branch_get(buf.buf);
857+
} else
850858
die(_("too many arguments to set new upstream"));
851859

852860
if (!branch) {
@@ -866,11 +874,17 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
866874
dwim_and_setup_tracking(the_repository, branch->name,
867875
new_upstream, BRANCH_TRACK_OVERRIDE,
868876
quiet);
877+
strbuf_release(&buf);
869878
} else if (unset_upstream) {
870-
struct branch *branch = branch_get(argv[0]);
879+
struct branch *branch;
871880
struct strbuf buf = STRBUF_INIT;
872881

873-
if (argc > 1)
882+
if (!argc)
883+
branch = branch_get(NULL);
884+
else if (argc == 1) {
885+
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
886+
branch = branch_get(buf.buf);
887+
} else
874888
die(_("too many arguments to unset upstream"));
875889

876890
if (!branch) {
@@ -883,6 +897,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
883897
if (!branch_has_merge_config(branch))
884898
die(_("Branch '%s' has no upstream information"), branch->name);
885899

900+
strbuf_reset(&buf);
886901
strbuf_addf(&buf, "branch.%s.remote", branch->name);
887902
git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
888903
strbuf_reset(&buf);

t/t3204-branch-name-interpretation.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,28 @@ test_expect_success 'checkout does not treat remote @{upstream} as a branch' '
133133
expect_branch HEAD one
134134
'
135135

136+
test_expect_success 'edit-description via @{-1}' '
137+
git checkout -b desc-branch &&
138+
git checkout -b non-desc-branch &&
139+
write_script editor <<-\EOF &&
140+
echo "Branch description" >"$1"
141+
EOF
142+
EDITOR=./editor git branch --edit-description @{-1} &&
143+
test_must_fail git config branch.non-desc-branch.description &&
144+
git config branch.desc-branch.description >actual &&
145+
printf "Branch description\n\n" >expect &&
146+
test_cmp expect actual
147+
'
148+
149+
test_expect_success 'modify branch upstream via "@{-1}" and "@{-1}@{upstream}"' '
150+
git checkout -b upstream-branch &&
151+
git checkout -b upstream-other -t upstream-branch &&
152+
git branch --set-upstream-to upstream-other @{-1} &&
153+
git config branch.upstream-branch.merge >actual &&
154+
echo "refs/heads/upstream-other" >expect &&
155+
test_cmp expect actual &&
156+
git branch --unset-upstream @{-1}@{upstream} &&
157+
test_must_fail git config branch.upstream-other.merge
158+
'
159+
136160
test_done

0 commit comments

Comments
 (0)