Skip to content

Commit 43bc820

Browse files
qurJunio C Hamano
authored andcommitted
git-branch: improve abbreviation of sha1s in verbose mode
git-branch has an --abbrev= command line option, but it does no checking of the input. Take the argument parsing code from setup_revisions in revisions.c, and also the code for parsing the --no-abbrev option. Signed-off-by: Julian Phillips <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6227382 commit 43bc820

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

builtin-branch.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "builtin.h"
1313

1414
static const char builtin_branch_usage[] =
15-
"git-branch [-r] (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length>]]";
15+
"git-branch [-r] (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length> | --no-abbrev]]";
1616

1717
#define REF_UNKNOWN_TYPE 0x00
1818
#define REF_LOCAL_BRANCH 0x01
@@ -446,8 +446,16 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
446446
reflog = 1;
447447
continue;
448448
}
449+
if (!prefixcmp(arg, "--no-abbrev")) {
450+
abbrev = 0;
451+
continue;
452+
}
449453
if (!prefixcmp(arg, "--abbrev=")) {
450-
abbrev = atoi(arg+9);
454+
abbrev = strtoul(arg + 9, NULL, 10);
455+
if (abbrev < MINIMUM_ABBREV)
456+
abbrev = MINIMUM_ABBREV;
457+
else if (abbrev > 40)
458+
abbrev = 40;
451459
continue;
452460
}
453461
if (!strcmp(arg, "-v")) {

0 commit comments

Comments
 (0)