{"thread":{"id":"52616","subject":"[PATCH v3] branch: let '--edit-description' default to rebased branch during rebase","startedAt":"2020-01-11T17:58:13Z","lastAt":"2020-01-11T17:58:13Z","messageCount":1,"participants":["marcandre.lureau@redhat.com"],"isPatch":true,"patchVersion":3,"patchTotal":null},"messages":[{"id":"389616","messageId":"20200111175757.1970457-1-marcandre.lureau@redhat.com","threadId":"52616","inReplyTo":null,"subject":"[PATCH v3] branch: let '--edit-description' default to rebased branch during rebase","fromName":"","fromEmail":"marcandre.lureau@redhat.com","sentAt":"2020-01-11T17:57:57Z","receivedAt":"2020-01-11T17:58:13Z","isPatch":true,"sender":{"key":"marcandre.lureau@redhat.com","avatar":null},"body":"From: Marc-André Lureau <marcandre.lureau@redhat.com>\n\nDefaulting to editing the description of the rebased branch without an\nexplicit branchname argument would be useful.  Even the git bash prompt\nshows the name of the rebased branch, and then\n\n  ~/src/git (mybranch|REBASE-i 1/2)$ git branch --edit-description\n  fatal: Cannot give description to detached HEAD\n\nlooks quite unhelpful.\n\nSigned-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>\n---\nChanged in v3:\n - fix memory leaks on error paths\n - remove test_done left-over\n - style fixes\n\nbuiltin/branch.c  | 40 +++++++++++++++++++++++++++++-----------\n t/t3200-branch.sh | 18 ++++++++++++++++++\n 2 files changed, 47 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex d8297f80ff..033a045d40 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -745,33 +745,51 @@ int cmd_branch(int argc, const char **argv, const char *prefix)\n \t\tstring_list_clear(&output, 0);\n \t\treturn 0;\n \t} else if (edit_description) {\n-\t\tconst char *branch_name;\n+\t\tchar *branch_name = NULL;\n \t\tstruct strbuf branch_ref = STRBUF_INIT;\n \n \t\tif (!argc) {\n-\t\t\tif (filter.detached)\n-\t\t\t\tdie(_(\"Cannot give description to detached HEAD\"));\n-\t\t\tbranch_name = head;\n+\t\t\tif (!filter.detached)\n+\t\t\t\tbranch_name = xstrdup(head);\n+\t\t\telse {\n+\t\t\t\tstruct wt_status_state state;\n+\n+\t\t\t\tmemset(&state, 0, sizeof(state));\n+\t\t\t\tif (wt_status_check_rebase(NULL, &state))\n+\t\t\t\t\tbranch_name = state.branch;\n+\t\t\t\tif (!branch_name)\n+\t\t\t\t\tdie(_(\"Cannot give description to detached HEAD\"));\n+\t\t\t\tfree(state.onto);\n+\t\t\t}\n \t\t} else if (argc == 1)\n-\t\t\tbranch_name = argv[0];\n+\t\t\tbranch_name = xstrdup(argv[0]);\n \t\telse\n \t\t\tdie(_(\"cannot edit description of more than one branch\"));\n \n \t\tstrbuf_addf(&branch_ref, \"refs/heads/%s\", branch_name);\n \t\tif (!ref_exists(branch_ref.buf)) {\n-\t\t\tstrbuf_release(&branch_ref);\n+\t\t\tint ret;\n \n \t\t\tif (!argc)\n-\t\t\t\treturn error(_(\"No commit on branch '%s' yet.\"),\n-\t\t\t\t\t     branch_name);\n+\t\t\t\tret = error(_(\"No commit on branch '%s' yet.\"),\n+\t\t\t\t\t    branch_name);\n \t\t\telse\n-\t\t\t\treturn error(_(\"No branch named '%s'.\"),\n-\t\t\t\t\t     branch_name);\n+\t\t\t\tret = error(_(\"No branch named '%s'.\"),\n+\t\t\t\t\t    branch_name);\n+\n+\t\t\tstrbuf_release(&branch_ref);\n+\t\t\tfree(branch_name);\n+\t\t\treturn ret;\n+\n \t\t}\n \t\tstrbuf_release(&branch_ref);\n \n-\t\tif (edit_branch_description(branch_name))\n+\t\tif (edit_branch_description(branch_name)) {\n+\t\t\tfree(branch_name);\n \t\t\treturn 1;\n+\t\t}\n+\n+\t\tfree(branch_name);\n \t} else if (copy) {\n \t\tif (!argc)\n \t\t\tdie(_(\"branch name required\"));\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 411a70b0ce..d2bdaf324d 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1260,6 +1260,24 @@ test_expect_success 'use --edit-description' '\n \ttest_cmp expect EDITOR_OUTPUT\n '\n \n+test_expect_success 'use --edit-description during rebase' '\n+\twrite_script editor <<-\\EOF &&\n+\t\techo \"Rebase contents\" >\"$1\"\n+\tEOF\n+\t(\n+\t\tset_fake_editor &&\n+\t\tFAKE_LINES=\"break 1\" git rebase -i HEAD^ &&\n+\t\tEDITOR=./editor git branch --edit-description &&\n+\t\tgit rebase --continue\n+\t) &&\n+\twrite_script editor <<-\\EOF &&\n+\t\tgit stripspace -s <\"$1\" >\"EDITOR_OUTPUT\"\n+\tEOF\n+\tEDITOR=./editor git branch --edit-description &&\n+\techo \"Rebase contents\" >expect &&\n+\ttest_cmp expect EDITOR_OUTPUT\n+'\n+\n test_expect_success 'detect typo in branch name when using --edit-description' '\n \twrite_script editor <<-\\EOF &&\n \t\techo \"New contents\" >\"$1\"\n\nbase-commit: 7a6a90c6ec48fc78c83d7090d6c1b95d8f3739c0\n-- \n2.25.0.rc2.2.g5aece98438\n\n"}]}