Skip to content

Commit 40a56f4

Browse files
committed
Merge branch 'ef/cherry-abbrev'
* ef/cherry-abbrev: ls: remove redundant logic cherry: support --abbrev option
2 parents 0cb050a + 531e758 commit 40a56f4

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

builtin/log.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,8 +1303,11 @@ static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
13031303
return -1;
13041304
}
13051305

1306-
static const char cherry_usage[] =
1307-
"git cherry [-v] [<upstream> [<head> [<limit>]]]";
1306+
static const char * const cherry_usage[] = {
1307+
"git cherry [-v] [<upstream> [<head> [<limit>]]]",
1308+
NULL
1309+
};
1310+
13081311
int cmd_cherry(int argc, const char **argv, const char *prefix)
13091312
{
13101313
struct rev_info revs;
@@ -1315,26 +1318,25 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
13151318
const char *upstream;
13161319
const char *head = "HEAD";
13171320
const char *limit = NULL;
1318-
int verbose = 0;
1321+
int verbose = 0, abbrev = 0;
13191322

1320-
if (argc > 1 && !strcmp(argv[1], "-v")) {
1321-
verbose = 1;
1322-
argc--;
1323-
argv++;
1324-
}
1323+
struct option options[] = {
1324+
OPT__ABBREV(&abbrev),
1325+
OPT__VERBOSE(&verbose),
1326+
OPT_END()
1327+
};
13251328

1326-
if (argc > 1 && !strcmp(argv[1], "-h"))
1327-
usage(cherry_usage);
1329+
argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
13281330

13291331
switch (argc) {
1330-
case 4:
1331-
limit = argv[3];
1332-
/* FALLTHROUGH */
13331332
case 3:
1334-
head = argv[2];
1333+
limit = argv[2];
13351334
/* FALLTHROUGH */
13361335
case 2:
1337-
upstream = argv[1];
1336+
head = argv[1];
1337+
/* FALLTHROUGH */
1338+
case 1:
1339+
upstream = argv[0];
13381340
break;
13391341
default:
13401342
current_branch = branch_get(NULL);
@@ -1344,7 +1346,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
13441346
fprintf(stderr, "Could not find a tracked"
13451347
" remote branch, please"
13461348
" specify <upstream> manually.\n");
1347-
usage(cherry_usage);
1349+
usage_with_options(cherry_usage, options);
13481350
}
13491351

13501352
upstream = current_branch->merge[0]->dst;
@@ -1397,12 +1399,13 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
13971399
pretty_print_commit(CMIT_FMT_ONELINE, commit,
13981400
&buf, &ctx);
13991401
printf("%c %s %s\n", sign,
1400-
sha1_to_hex(commit->object.sha1), buf.buf);
1402+
find_unique_abbrev(commit->object.sha1, abbrev),
1403+
buf.buf);
14011404
strbuf_release(&buf);
14021405
}
14031406
else {
14041407
printf("%c %s\n", sign,
1405-
sha1_to_hex(commit->object.sha1));
1408+
find_unique_abbrev(commit->object.sha1, abbrev));
14061409
}
14071410

14081411
list = list->next;

builtin/ls-files.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
153153
printf("%s%06o %s %d\t",
154154
tag,
155155
ce->ce_mode,
156-
abbrev ? find_unique_abbrev(ce->sha1,abbrev)
157-
: sha1_to_hex(ce->sha1),
156+
find_unique_abbrev(ce->sha1,abbrev),
158157
ce_stage(ce));
159158
}
160159
write_name_quoted(ce->name + offset, stdout, line_terminator);
@@ -176,9 +175,7 @@ static int show_one_ru(struct string_list_item *item, void *cbdata)
176175
if (!ui->mode[i])
177176
continue;
178177
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
179-
abbrev
180-
? find_unique_abbrev(ui->sha1[i], abbrev)
181-
: sha1_to_hex(ui->sha1[i]),
178+
find_unique_abbrev(ui->sha1[i], abbrev),
182179
i + 1);
183180
write_name_quoted(path + offset, stdout, line_terminator);
184181
}

builtin/ls-tree.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,11 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
103103
} else
104104
strcpy(size_text, "-");
105105
printf("%06o %s %s %7s\t", mode, type,
106-
abbrev ? find_unique_abbrev(sha1, abbrev)
107-
: sha1_to_hex(sha1),
106+
find_unique_abbrev(sha1, abbrev),
108107
size_text);
109108
} else
110109
printf("%06o %s %s\t", mode, type,
111-
abbrev ? find_unique_abbrev(sha1, abbrev)
112-
: sha1_to_hex(sha1));
110+
find_unique_abbrev(sha1, abbrev));
113111
}
114112
write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
115113
pathname, stdout, line_termination);

0 commit comments

Comments
 (0)