Skip to content

Commit 8a3d203

Browse files
committed
log.decorate: usability fixes
The configuration is meant to suppliment --decorate command line option that can be used as a boolean to turn the feature on, so it is natural to expect [log] decorate decorate = yes to work. The original commit would segfault with the first one, and would not understand the second one. Once a user has this configuration in ~/.gitconfig, there needs to be a way to override it from the command line. Add --no-decorate option to log family and also allow --decorate=no to mean the same thing. Since we allow setting log.decorate to 'true', the command line also should accept --decorate=yes and behave accordingly. New tests in t4202 are designed to exercise the interaction between the configuration variable and the command line option that overrides it. Signed-off-by: Junio C Hamano <[email protected]>
1 parent eb73445 commit 8a3d203

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

Documentation/git-log.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ include::diff-options.txt[]
3737
and <until>, see "SPECIFYING REVISIONS" section in
3838
linkgit:git-rev-parse[1].
3939

40-
--decorate[=short|full]::
40+
--no-decorate::
41+
--decorate[=short|full|no]::
4142
Print out the ref names of any commits that are shown. If 'short' is
4243
specified, the ref name prefixes 'refs/heads/', 'refs/tags/' and
4344
'refs/remotes/' will not be printed. If 'full' is specified, the

builtin-log.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,31 @@
2424
static const char *default_date_mode = NULL;
2525

2626
static int default_show_root = 1;
27-
static int decoration_style = 0;
27+
static int decoration_style;
2828
static const char *fmt_patch_subject_prefix = "PATCH";
2929
static const char *fmt_pretty;
3030

3131
static const char * const builtin_log_usage =
3232
"git log [<options>] [<since>..<until>] [[--] <path>...]\n"
3333
" or: git show [options] <object>...";
3434

35+
static int parse_decoration_style(const char *var, const char *value)
36+
{
37+
switch (git_config_maybe_bool(var, value)) {
38+
case 1:
39+
return DECORATE_SHORT_REFS;
40+
case 0:
41+
return 0;
42+
default:
43+
break;
44+
}
45+
if (!strcmp(value, "full"))
46+
return DECORATE_FULL_REFS;
47+
else if (!strcmp(value, "short"))
48+
return DECORATE_SHORT_REFS;
49+
return -1;
50+
}
51+
3552
static void cmd_log_init(int argc, const char **argv, const char *prefix,
3653
struct rev_info *rev)
3754
{
@@ -74,12 +91,11 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
7491
decoration_style = DECORATE_SHORT_REFS;
7592
} else if (!prefixcmp(arg, "--decorate=")) {
7693
const char *v = skip_prefix(arg, "--decorate=");
77-
if (!strcmp(v, "full"))
78-
decoration_style = DECORATE_FULL_REFS;
79-
else if (!strcmp(v, "short"))
80-
decoration_style = DECORATE_SHORT_REFS;
81-
else
94+
decoration_style = parse_decoration_style(arg, v);
95+
if (decoration_style < 0)
8296
die("invalid --decorate option: %s", arg);
97+
} else if (!strcmp(arg, "--no-decorate")) {
98+
decoration_style = 0;
8399
} else if (!strcmp(arg, "--source")) {
84100
rev->show_source = 1;
85101
} else if (!strcmp(arg, "-h")) {
@@ -253,10 +269,9 @@ static int git_log_config(const char *var, const char *value, void *cb)
253269
if (!strcmp(var, "log.date"))
254270
return git_config_string(&default_date_mode, var, value);
255271
if (!strcmp(var, "log.decorate")) {
256-
if (!strcmp(value, "full"))
257-
decoration_style = DECORATE_FULL_REFS;
258-
else if (!strcmp(value, "short"))
259-
decoration_style = DECORATE_SHORT_REFS;
272+
decoration_style = parse_decoration_style(var, value);
273+
if (decoration_style < 0)
274+
decoration_style = 0; /* maybe warn? */
260275
return 0;
261276
}
262277
if (!strcmp(var, "log.showroot")) {

t/t4202-log.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,5 +387,54 @@ test_expect_success 'log --graph with merge' '
387387
test_cmp expect actual
388388
'
389389

390+
test_expect_success 'log.decorate configuration' '
391+
git config --unset-all log.decorate || :
392+
393+
git log --oneline >expect.none &&
394+
git log --oneline --decorate >expect.short &&
395+
git log --oneline --decorate=full >expect.full &&
396+
397+
echo "[log] decorate" >>.git/config &&
398+
git log --oneline >actual &&
399+
test_cmp expect.short actual &&
400+
401+
git config --unset-all log.decorate &&
402+
git config log.decorate true &&
403+
git log --oneline >actual &&
404+
test_cmp expect.short actual &&
405+
git log --oneline --decorate=full >actual &&
406+
test_cmp expect.full actual &&
407+
git log --oneline --decorate=no >actual &&
408+
test_cmp expect.none actual &&
409+
410+
git config --unset-all log.decorate &&
411+
git config log.decorate no &&
412+
git log --oneline >actual &&
413+
test_cmp expect.none actual &&
414+
git log --oneline --decorate >actual &&
415+
test_cmp expect.short actual &&
416+
git log --oneline --decorate=full >actual &&
417+
test_cmp expect.full actual &&
418+
419+
git config --unset-all log.decorate &&
420+
git config log.decorate short &&
421+
git log --oneline >actual &&
422+
test_cmp expect.short actual &&
423+
git log --oneline --no-decorate >actual &&
424+
test_cmp expect.none actual &&
425+
git log --oneline --decorate=full >actual &&
426+
test_cmp expect.full actual &&
427+
428+
git config --unset-all log.decorate &&
429+
git config log.decorate full &&
430+
git log --oneline >actual &&
431+
test_cmp expect.full actual &&
432+
git log --oneline --no-decorate >actual &&
433+
test_cmp expect.none actual &&
434+
git log --oneline --decorate >actual &&
435+
test_cmp expect.short actual
436+
437+
'
438+
390439
test_done
391440

0 commit comments

Comments
 (0)