Skip to content

Commit ca92e59

Browse files
martinvonzgitster
authored andcommitted
teach log --no-walk=unsorted, which avoids sorting
When 'git log' is passed the --no-walk option, no revision walk takes place, naturally. Perhaps somewhat surprisingly, however, the provided revisions still get sorted by commit date. So e.g 'git log --no-walk HEAD HEAD~1' and 'git log --no-walk HEAD~1 HEAD' give the same result (unless the two revisions share the commit date, in which case they will retain the order given on the command line). As the commit that introduced --no-walk (8e64006 (Teach revision machinery about --no-walk, 2007-07-24)) points out, the sorting is intentional, to allow things like git log --abbrev-commit --pretty=oneline --decorate --all --no-walk to show all refs in order by commit date. But there are also other cases where the sorting is not wanted, such as <command producing revisions in order> | git log --oneline --no-walk --stdin To accomodate both cases, leave the decision of whether or not to sort up to the caller, by allowing --no-walk={sorted,unsorted}, defaulting to 'sorted' for backward-compatibility reasons. Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ce2e39 commit ca92e59

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

Documentation/rev-list-options.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,14 @@ These options are mostly targeted for packing of git repositories.
619619
Only useful with '--objects'; print the object IDs that are not
620620
in packs.
621621

622-
--no-walk::
623-
624-
Only show the given revs, but do not traverse their ancestors.
622+
--no-walk[=(sorted|unsorted)]::
623+
624+
Only show the given commits, but do not traverse their ancestors.
625+
This has no effect if a range is specified. If the argument
626+
"unsorted" is given, the commits are show in the order they were
627+
given on the command line. Otherwise (if "sorted" or no argument
628+
was given), the commits are show in reverse chronological order
629+
by commit time.
625630

626631
--do-walk::
627632

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
454454
init_revisions(&rev, prefix);
455455
rev.diff = 1;
456456
rev.always_show_header = 1;
457-
rev.no_walk = 1;
457+
rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
458458
rev.diffopt.stat_width = -1; /* Scale to real terminal size */
459459

460460
memset(&opt, 0, sizeof(opt));

builtin/revert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
193193
struct setup_revision_opt s_r_opt;
194194
opts->revs = xmalloc(sizeof(*opts->revs));
195195
init_revisions(opts->revs, NULL);
196-
opts->revs->no_walk = 1;
196+
opts->revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
197197
if (argc < 2)
198198
usage_with_options(usage_str, options);
199199
memset(&s_r_opt, 0, sizeof(s_r_opt));

revision.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
12941294
!strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
12951295
!strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
12961296
!prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1297-
!prefixcmp(arg, "--remotes="))
1297+
!prefixcmp(arg, "--remotes=") || !prefixcmp(arg, "--no-walk="))
12981298
{
12991299
unkv[(*unkc)++] = arg;
13001300
return 1;
@@ -1687,7 +1687,18 @@ static int handle_revision_pseudo_opt(const char *submodule,
16871687
} else if (!strcmp(arg, "--not")) {
16881688
*flags ^= UNINTERESTING;
16891689
} else if (!strcmp(arg, "--no-walk")) {
1690-
revs->no_walk = 1;
1690+
revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
1691+
} else if (!prefixcmp(arg, "--no-walk=")) {
1692+
/*
1693+
* Detached form ("--no-walk X" as opposed to "--no-walk=X")
1694+
* not allowed, since the argument is optional.
1695+
*/
1696+
if (!strcmp(arg + 10, "sorted"))
1697+
revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
1698+
else if (!strcmp(arg + 10, "unsorted"))
1699+
revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
1700+
else
1701+
return error("invalid argument to --no-walk");
16911702
} else if (!strcmp(arg, "--do-walk")) {
16921703
revs->no_walk = 0;
16931704
} else {
@@ -2091,10 +2102,11 @@ int prepare_revision_walk(struct rev_info *revs)
20912102
}
20922103
e++;
20932104
}
2094-
commit_list_sort_by_date(&revs->commits);
20952105
if (!revs->leak_pending)
20962106
free(list);
20972107

2108+
if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2109+
commit_list_sort_by_date(&revs->commits);
20982110
if (revs->no_walk)
20992111
return 0;
21002112
if (revs->limited)

revision.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ struct rev_cmdline_info {
4141
} *rev;
4242
};
4343

44+
#define REVISION_WALK_WALK 0
45+
#define REVISION_WALK_NO_WALK_SORTED 1
46+
#define REVISION_WALK_NO_WALK_UNSORTED 2
47+
4448
struct rev_info {
4549
/* Starting list */
4650
struct commit_list *commits;
@@ -62,7 +66,7 @@ struct rev_info {
6266
/* Traversal flags */
6367
unsigned int dense:1,
6468
prune:1,
65-
no_walk:1,
69+
no_walk:2,
6670
show_all:1,
6771
remove_empty_trees:1,
6872
simplify_history:1,

t/t4202-log.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,21 @@ test_expect_success 'git log --no-walk <commits> sorts by commit time' '
178178
test_cmp expect actual
179179
'
180180

181+
test_expect_success 'git log --no-walk=sorted <commits> sorts by commit time' '
182+
git log --no-walk=sorted --oneline 5d31159 804a787 394ef78 > actual &&
183+
test_cmp expect actual
184+
'
185+
181186
cat > expect << EOF
182187
5d31159 fourth
183188
804a787 sixth
184189
394ef78 fifth
185190
EOF
191+
test_expect_success 'git log --no-walk=unsorted <commits> leaves list of commits as given' '
192+
git log --no-walk=unsorted --oneline 5d31159 804a787 394ef78 > actual &&
193+
test_cmp expect actual
194+
'
195+
186196
test_expect_success 'git show <commits> leaves list of commits as given' '
187197
git show --oneline -s 5d31159 804a787 394ef78 > actual &&
188198
test_cmp expect actual

0 commit comments

Comments
 (0)