Skip to content

Commit c69f921

Browse files
committed
Merge branch 'jk/cherry-pick-reword' into maint
* jk/cherry-pick-reword: cherry-pick: prettify the advice message cherry-pick: show commit name instead of sha1 cherry-pick: format help message as strbuf cherry-pick: refactor commit parsing code cherry-pick: rewrap advice message
2 parents 031f82f + 4d12888 commit c69f921

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

builtin-revert.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static const char * const cherry_pick_usage[] = {
3838
static int edit, no_replay, no_commit, mainline, signoff;
3939
static enum { REVERT, CHERRY_PICK } action;
4040
static struct commit *commit;
41+
static const char *commit_name;
4142
static int allow_rerere_auto;
4243

4344
static const char *me;
@@ -49,7 +50,6 @@ static void parse_args(int argc, const char **argv)
4950
const char * const * usage_str =
5051
action == REVERT ? revert_usage : cherry_pick_usage;
5152
unsigned char sha1[20];
52-
const char *arg;
5353
int noop;
5454
struct option options[] = {
5555
OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
@@ -64,19 +64,13 @@ static void parse_args(int argc, const char **argv)
6464

6565
if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
6666
usage_with_options(usage_str, options);
67-
arg = argv[0];
6867

69-
if (get_sha1(arg, sha1))
70-
die ("Cannot find '%s'", arg);
71-
commit = (struct commit *)parse_object(sha1);
68+
commit_name = argv[0];
69+
if (get_sha1(commit_name, sha1))
70+
die ("Cannot find '%s'", commit_name);
71+
commit = lookup_commit_reference(sha1);
7272
if (!commit)
73-
die ("Could not find %s", sha1_to_hex(sha1));
74-
if (commit->object.type == OBJ_TAG) {
75-
commit = (struct commit *)
76-
deref_tag((struct object *)commit, arg, strlen(arg));
77-
}
78-
if (commit->object.type != OBJ_COMMIT)
79-
die ("'%s' does not point to a commit", arg);
73+
exit(1);
8074
}
8175

8276
static char *get_oneline(const char *message)
@@ -204,25 +198,27 @@ static void set_author_ident_env(const char *message)
204198
sha1_to_hex(commit->object.sha1));
205199
}
206200

207-
static char *help_msg(const unsigned char *sha1)
201+
static char *help_msg(const char *name)
208202
{
209-
static char helpbuf[1024];
203+
struct strbuf helpbuf = STRBUF_INIT;
210204
char *msg = getenv("GIT_CHERRY_PICK_HELP");
211205

212206
if (msg)
213207
return msg;
214208

215-
strcpy(helpbuf, " After resolving the conflicts,\n"
216-
"mark the corrected paths with 'git add <paths>' "
217-
"or 'git rm <paths>' and commit the result.");
209+
strbuf_addstr(&helpbuf, " After resolving the conflicts,\n"
210+
"mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
211+
"and commit the result");
218212

219213
if (action == CHERRY_PICK) {
220-
sprintf(helpbuf + strlen(helpbuf),
221-
"\nWhen commiting, use the option "
222-
"'-c %s' to retain authorship and message.",
223-
find_unique_abbrev(sha1, DEFAULT_ABBREV));
214+
strbuf_addf(&helpbuf, " with: \n"
215+
"\n"
216+
" git commit -c %s\n",
217+
name);
224218
}
225-
return helpbuf;
219+
else
220+
strbuf_addch(&helpbuf, '.');
221+
return strbuf_detach(&helpbuf, NULL);
226222
}
227223

228224
static struct tree *empty_tree(void)
@@ -409,7 +405,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
409405
if (commit_lock_file(&msg_file) < 0)
410406
die ("Error wrapping up %s", defmsg);
411407
fprintf(stderr, "Automatic %s failed.%s\n",
412-
me, help_msg(commit->object.sha1));
408+
me, help_msg(commit_name));
413409
rerere(allow_rerere_auto);
414410
exit(1);
415411
}

0 commit comments

Comments
 (0)