Skip to content

Commit 076c983

Browse files
dturner-twgitster
authored andcommitted
log: add "log.follow" configuration variable
People who work on projects with mostly linear history with frequent whole file renames may want to always use "git log --follow" when inspecting the life of the content that live in a single path. Teach the command to behave as if "--follow" was given from the command line when log.follow configuration variable is set *and* there is one (and only one) path on the command line. Signed-off-by: David Turner <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bdb7a7 commit 076c983

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

Documentation/git-log.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ log.date::
184184
`--date` option.) Defaults to "default", which means to write
185185
dates like `Sat May 8 19:35:34 2010 -0500`.
186186

187+
log.follow::
188+
If a single <path> is given to git log, it will act as
189+
if the `--follow` option was also used. This has the same
190+
limitations as `--follow`, i.e. it cannot be used to follow
191+
multiple files and does not work well on non-linear history.
192+
187193
log.showRoot::
188194
If `false`, `git log` and related commands will not treat the
189195
initial commit as a big creation event. Any root commits in

builtin/log.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static const char *default_date_mode = NULL;
3131

3232
static int default_abbrev_commit;
3333
static int default_show_root = 1;
34+
static int default_follow;
3435
static int decoration_style;
3536
static int decoration_given;
3637
static int use_mailmap_config;
@@ -102,6 +103,8 @@ static void cmd_log_init_defaults(struct rev_info *rev)
102103
{
103104
if (fmt_pretty)
104105
get_commit_format(fmt_pretty, rev);
106+
if (default_follow)
107+
DIFF_OPT_SET(&rev->diffopt, DEFAULT_FOLLOW_RENAMES);
105108
rev->verbose_header = 1;
106109
DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
107110
rev->diffopt.stat_width = -1; /* use full terminal width */
@@ -390,6 +393,10 @@ static int git_log_config(const char *var, const char *value, void *cb)
390393
default_show_root = git_config_bool(var, value);
391394
return 0;
392395
}
396+
if (!strcmp(var, "log.follow")) {
397+
default_follow = git_config_bool(var, value);
398+
return 0;
399+
}
393400
if (skip_prefix(var, "color.decorate.", &slot_name))
394401
return parse_decorate_color_config(var, slot_name, value);
395402
if (!strcmp(var, "log.mailmap")) {
@@ -618,6 +625,14 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
618625
return cmd_log_walk(&rev);
619626
}
620627

628+
static void default_follow_tweak(struct rev_info *rev,
629+
struct setup_revision_opt *opt)
630+
{
631+
if (DIFF_OPT_TST(&rev->diffopt, DEFAULT_FOLLOW_RENAMES) &&
632+
rev->prune_data.nr == 1)
633+
DIFF_OPT_SET(&rev->diffopt, FOLLOW_RENAMES);
634+
}
635+
621636
int cmd_log(int argc, const char **argv, const char *prefix)
622637
{
623638
struct rev_info rev;
@@ -631,6 +646,7 @@ int cmd_log(int argc, const char **argv, const char *prefix)
631646
memset(&opt, 0, sizeof(opt));
632647
opt.def = "HEAD";
633648
opt.revarg_opt = REVARG_COMMITTISH;
649+
opt.tweak = default_follow_tweak;
634650
cmd_log_init(argc, argv, prefix, &rev, &opt);
635651
return cmd_log_walk(&rev);
636652
}

diff.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3815,9 +3815,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
38153815
DIFF_OPT_SET(options, FIND_COPIES_HARDER);
38163816
else if (!strcmp(arg, "--follow"))
38173817
DIFF_OPT_SET(options, FOLLOW_RENAMES);
3818-
else if (!strcmp(arg, "--no-follow"))
3818+
else if (!strcmp(arg, "--no-follow")) {
38193819
DIFF_OPT_CLR(options, FOLLOW_RENAMES);
3820-
else if (!strcmp(arg, "--color"))
3820+
DIFF_OPT_CLR(options, DEFAULT_FOLLOW_RENAMES);
3821+
} else if (!strcmp(arg, "--color"))
38213822
options->use_color = 1;
38223823
else if (skip_prefix(arg, "--color=", &arg)) {
38233824
int value = git_config_colorbool(NULL, arg);

diff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
9191
#define DIFF_OPT_DIRSTAT_BY_LINE (1 << 28)
9292
#define DIFF_OPT_FUNCCONTEXT (1 << 29)
9393
#define DIFF_OPT_PICKAXE_IGNORE_CASE (1 << 30)
94+
#define DIFF_OPT_DEFAULT_FOLLOW_RENAMES (1 << 31)
9495

9596
#define DIFF_OPT_TST(opts, flag) ((opts)->flags & DIFF_OPT_##flag)
9697
#define DIFF_OPT_TOUCHED(opts, flag) ((opts)->touched_flags & DIFF_OPT_##flag)

t/t4202-log.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,30 @@ test_expect_success 'git log --follow' '
146146
actual=$(git log --follow --pretty="format:%s" ichi) &&
147147
expect=$(echo third ; echo second ; echo initial) &&
148148
verbose test "$actual" = "$expect"
149+
'
150+
151+
test_expect_success 'git config log.follow works like --follow' '
152+
test_config log.follow true &&
153+
actual=$(git log --pretty="format:%s" ichi) &&
154+
expect=$(echo third ; echo second ; echo initial) &&
155+
verbose test "$actual" = "$expect"
156+
'
149157

158+
test_expect_success 'git config log.follow does not die with multiple paths' '
159+
test_config log.follow true &&
160+
git log --pretty="format:%s" ichi ein
161+
'
162+
163+
test_expect_success 'git config log.follow does not die with no paths' '
164+
test_config log.follow true &&
165+
git log --
166+
'
167+
168+
test_expect_success 'git config log.follow is overridden by --no-follow' '
169+
test_config log.follow true &&
170+
actual=$(git log --no-follow --pretty="format:%s" ichi) &&
171+
expect="third" &&
172+
verbose test "$actual" = "$expect"
150173
'
151174

152175
cat > expect << EOF

0 commit comments

Comments
 (0)