Skip to content

Commit cd2bdc5

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
Common option parsing for "git log --diff" and friends
This basically does a few things that are sadly somewhat interdependent, and nontrivial to split out - get rid of "struct log_tree_opt" The fields in "log_tree_opt" are moved into "struct rev_info", and all users of log_tree_opt are changed to use the rev_info struct instead. - add the parsing for the log_tree_opt arguments to "setup_revision()" - make setup_revision set a flag (revs->diff) if the diff-related arguments were used. This allows "git log" to decide whether it wants to show diffs or not. - make setup_revision() also initialize the diffopt part of rev_info (which we had from before, but we just didn't initialize it) - make setup_revision() do all the "finishing touches" on it all (it will do the proper flag combination logic, and call "diff_setup_done()") Now, that was the easy and straightforward part. The slightly more involved part is that some of the programs that want to use the new-and-improved rev_info parsing don't actually want _commits_, they may want tree'ish arguments instead. That meant that I had to change setup_revision() to parse the arguments not into the "revs->commits" list, but into the "revs->pending_objects" list. Then, when we do "prepare_revision_walk()", we walk that list, and create the sorted commit list from there. This actually cleaned some stuff up, but it's the less obvious part of the patch, and re-organized the "revision.c" logic somewhat. It actually paves the way for splitting argument parsing _entirely_ out of "revision.c", since now the argument parsing really is totally independent of the commit walking: that didn't use to be true, since there was lots of overlap with get_commit_reference() handling etc, now the _only_ overlap is the shared (and trivial) "add_pending_object()" thing. However, I didn't do that file split, just because I wanted the diff itself to be smaller, and show the actual changes more clearly. If this gets accepted, I'll do further cleanups then - that includes the file split, but also using the new infrastructure to do a nicer "git diff" etc. Even in this form, it actually ends up removing more lines than it adds. It's nice to note how simple and straightforward this makes the built-in "git log" command, even though it continues to support all the diff flags too. It doesn't get much simpler that this. I think this is worth merging soonish, because it does allow for future cleanup and even more sharing of code. However, it obviously touches "revision.c", which is subtle. I've tested that it passes all the tests we have, and it passes my "looks sane" detector, but somebody else should also give it a good look-over. [jc: squashed the original and three "oops this too" updates, with another fix-up.] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent afcb536 commit cd2bdc5

File tree

6 files changed

+218
-227
lines changed

6 files changed

+218
-227
lines changed

diff-tree.c

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "commit.h"
44
#include "log-tree.h"
55

6-
static struct log_tree_opt log_tree_opt;
6+
static struct rev_info log_tree_opt;
77

88
static int diff_tree_commit_sha1(const unsigned char *sha1)
99
{
@@ -62,77 +62,68 @@ int main(int argc, const char **argv)
6262
{
6363
int nr_sha1;
6464
char line[1000];
65-
unsigned char sha1[2][20];
66-
const char *prefix = setup_git_directory();
67-
static struct log_tree_opt *opt = &log_tree_opt;
65+
struct object *tree1, *tree2;
66+
static struct rev_info *opt = &log_tree_opt;
67+
struct object_list *list;
6868
int read_stdin = 0;
6969

7070
git_config(git_diff_config);
7171
nr_sha1 = 0;
72-
init_log_tree_opt(opt);
72+
argc = setup_revisions(argc, argv, opt, NULL);
7373

74-
for (;;) {
75-
int opt_cnt;
76-
const char *arg;
74+
while (--argc > 0) {
75+
const char *arg = *++argv;
7776

78-
argv++;
79-
argc--;
80-
arg = *argv;
81-
if (!arg)
82-
break;
83-
84-
if (*arg != '-') {
85-
if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
86-
nr_sha1++;
87-
continue;
88-
}
89-
break;
90-
}
91-
92-
opt_cnt = log_tree_opt_parse(opt, argv, argc);
93-
if (opt_cnt < 0)
94-
usage(diff_tree_usage);
95-
else if (opt_cnt) {
96-
argv += opt_cnt - 1;
97-
argc -= opt_cnt - 1;
98-
continue;
99-
}
100-
101-
if (!strcmp(arg, "--")) {
102-
argv++;
103-
argc--;
104-
break;
105-
}
10677
if (!strcmp(arg, "--stdin")) {
10778
read_stdin = 1;
10879
continue;
10980
}
11081
usage(diff_tree_usage);
11182
}
11283

113-
if (opt->combine_merges)
114-
opt->ignore_merges = 0;
115-
116-
/* We can only do dense combined merges with diff output */
117-
if (opt->dense_combined_merges)
118-
opt->diffopt.output_format = DIFF_FORMAT_PATCH;
119-
120-
if (opt->diffopt.output_format == DIFF_FORMAT_PATCH)
121-
opt->diffopt.recursive = 1;
122-
123-
diff_tree_setup_paths(get_pathspec(prefix, argv), opt);
124-
diff_setup_done(&opt->diffopt);
84+
/*
85+
* NOTE! "setup_revisions()" will have inserted the revisions
86+
* it parsed in reverse order. So if you do
87+
*
88+
* git-diff-tree a b
89+
*
90+
* the commit list will be "b" -> "a" -> NULL, so we reverse
91+
* the order of the objects if the first one is not marked
92+
* UNINTERESTING.
93+
*/
94+
nr_sha1 = 0;
95+
list = opt->pending_objects;
96+
if (list) {
97+
nr_sha1++;
98+
tree1 = list->item;
99+
list = list->next;
100+
if (list) {
101+
nr_sha1++;
102+
tree2 = tree1;
103+
tree1 = list->item;
104+
if (list->next)
105+
usage(diff_tree_usage);
106+
/* Switch them around if the second one was uninteresting.. */
107+
if (tree2->flags & UNINTERESTING) {
108+
struct object *tmp = tree2;
109+
tree2 = tree1;
110+
tree1 = tmp;
111+
}
112+
}
113+
}
125114

126115
switch (nr_sha1) {
127116
case 0:
128117
if (!read_stdin)
129118
usage(diff_tree_usage);
130119
break;
131120
case 1:
132-
diff_tree_commit_sha1(sha1[0]);
121+
diff_tree_commit_sha1(tree1->sha1);
133122
break;
134123
case 2:
135-
diff_tree_sha1(sha1[0], sha1[1], "", &opt->diffopt);
124+
diff_tree_sha1(tree1->sha1,
125+
tree2->sha1,
126+
"", &opt->diffopt);
136127
log_tree_diff_flush(opt);
137128
break;
138129
}

git.c

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -283,82 +283,25 @@ static int cmd_log(int argc, const char **argv, char **envp)
283283
struct rev_info rev;
284284
struct commit *commit;
285285
char *buf = xmalloc(LOGSIZE);
286-
static enum cmit_fmt commit_format = CMIT_FMT_DEFAULT;
287-
int abbrev = DEFAULT_ABBREV;
288-
int abbrev_commit = 0;
289286
const char *commit_prefix = "commit ";
290-
struct log_tree_opt opt;
291287
int shown = 0;
292-
int do_diff = 0;
293-
int full_diff = 0;
294288

295-
init_log_tree_opt(&opt);
296289
argc = setup_revisions(argc, argv, &rev, "HEAD");
297-
while (1 < argc) {
298-
const char *arg = argv[1];
299-
if (!strncmp(arg, "--pretty", 8)) {
300-
commit_format = get_commit_format(arg + 8);
301-
if (commit_format == CMIT_FMT_ONELINE)
302-
commit_prefix = "";
303-
}
304-
else if (!strcmp(arg, "--no-abbrev")) {
305-
abbrev = 0;
306-
}
307-
else if (!strcmp(arg, "--abbrev")) {
308-
abbrev = DEFAULT_ABBREV;
309-
}
310-
else if (!strcmp(arg, "--abbrev-commit")) {
311-
abbrev_commit = 1;
312-
}
313-
else if (!strncmp(arg, "--abbrev=", 9)) {
314-
abbrev = strtoul(arg + 9, NULL, 10);
315-
if (abbrev && abbrev < MINIMUM_ABBREV)
316-
abbrev = MINIMUM_ABBREV;
317-
else if (40 < abbrev)
318-
abbrev = 40;
319-
}
320-
else if (!strcmp(arg, "--full-diff")) {
321-
do_diff = 1;
322-
full_diff = 1;
323-
}
324-
else {
325-
int cnt = log_tree_opt_parse(&opt, argv+1, argc-1);
326-
if (0 < cnt) {
327-
do_diff = 1;
328-
argv += cnt;
329-
argc -= cnt;
330-
continue;
331-
}
332-
die("unrecognized argument: %s", arg);
333-
}
290+
if (argc > 1)
291+
die("unrecognized argument: %s", argv[1]);
334292

335-
argc--; argv++;
336-
}
337-
338-
if (do_diff) {
339-
opt.diffopt.abbrev = abbrev;
340-
opt.verbose_header = 0;
341-
opt.always_show_header = 0;
342-
opt.no_commit_id = 1;
343-
if (opt.combine_merges)
344-
opt.ignore_merges = 0;
345-
if (opt.dense_combined_merges)
346-
opt.diffopt.output_format = DIFF_FORMAT_PATCH;
347-
if (opt.diffopt.output_format == DIFF_FORMAT_PATCH)
348-
opt.diffopt.recursive = 1;
349-
if (!full_diff && rev.prune_data)
350-
diff_tree_setup_paths(rev.prune_data, &opt.diffopt);
351-
diff_setup_done(&opt.diffopt);
352-
}
293+
rev.no_commit_id = 1;
294+
if (rev.commit_format == CMIT_FMT_ONELINE)
295+
commit_prefix = "";
353296

354297
prepare_revision_walk(&rev);
355298
setup_pager();
356299
while ((commit = get_revision(&rev)) != NULL) {
357-
if (shown && do_diff && commit_format != CMIT_FMT_ONELINE)
300+
if (shown && rev.diff && rev.commit_format != CMIT_FMT_ONELINE)
358301
putchar('\n');
359302
fputs(commit_prefix, stdout);
360-
if (abbrev_commit && abbrev)
361-
fputs(find_unique_abbrev(commit->object.sha1, abbrev),
303+
if (rev.abbrev_commit && rev.abbrev)
304+
fputs(find_unique_abbrev(commit->object.sha1, rev.abbrev),
362305
stdout);
363306
else
364307
fputs(sha1_to_hex(commit->object.sha1), stdout);
@@ -381,15 +324,15 @@ static int cmd_log(int argc, const char **argv, char **envp)
381324
parents = parents->next)
382325
parents->item->object.flags &= ~TMP_MARK;
383326
}
384-
if (commit_format == CMIT_FMT_ONELINE)
327+
if (rev.commit_format == CMIT_FMT_ONELINE)
385328
putchar(' ');
386329
else
387330
putchar('\n');
388-
pretty_print_commit(commit_format, commit, ~0, buf,
389-
LOGSIZE, abbrev);
331+
pretty_print_commit(rev.commit_format, commit, ~0, buf,
332+
LOGSIZE, rev.abbrev);
390333
printf("%s\n", buf);
391-
if (do_diff)
392-
log_tree_commit(&opt, commit);
334+
if (rev.diff)
335+
log_tree_commit(&rev, commit);
393336
shown = 1;
394337
free(commit->buffer);
395338
commit->buffer = NULL;

log-tree.c

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,7 @@
33
#include "commit.h"
44
#include "log-tree.h"
55

6-
void init_log_tree_opt(struct log_tree_opt *opt)
7-
{
8-
memset(opt, 0, sizeof *opt);
9-
opt->ignore_merges = 1;
10-
opt->header_prefix = "";
11-
opt->commit_format = CMIT_FMT_RAW;
12-
diff_setup(&opt->diffopt);
13-
}
14-
15-
int log_tree_opt_parse(struct log_tree_opt *opt, const char **av, int ac)
16-
{
17-
const char *arg;
18-
int cnt = diff_opt_parse(&opt->diffopt, av, ac);
19-
if (0 < cnt)
20-
return cnt;
21-
arg = *av;
22-
if (!strcmp(arg, "-r"))
23-
opt->diffopt.recursive = 1;
24-
else if (!strcmp(arg, "-t")) {
25-
opt->diffopt.recursive = 1;
26-
opt->diffopt.tree_in_recursive = 1;
27-
}
28-
else if (!strcmp(arg, "-m"))
29-
opt->ignore_merges = 0;
30-
else if (!strcmp(arg, "-c"))
31-
opt->combine_merges = 1;
32-
else if (!strcmp(arg, "--cc")) {
33-
opt->dense_combined_merges = 1;
34-
opt->combine_merges = 1;
35-
}
36-
else if (!strcmp(arg, "-v")) {
37-
opt->verbose_header = 1;
38-
opt->header_prefix = "diff-tree ";
39-
}
40-
else if (!strncmp(arg, "--pretty", 8)) {
41-
opt->verbose_header = 1;
42-
opt->header_prefix = "diff-tree ";
43-
opt->commit_format = get_commit_format(arg+8);
44-
}
45-
else if (!strcmp(arg, "--root"))
46-
opt->show_root_diff = 1;
47-
else if (!strcmp(arg, "--no-commit-id"))
48-
opt->no_commit_id = 1;
49-
else if (!strcmp(arg, "--always"))
50-
opt->always_show_header = 1;
51-
else
52-
return 0;
53-
return 1;
54-
}
55-
56-
int log_tree_diff_flush(struct log_tree_opt *opt)
6+
int log_tree_diff_flush(struct rev_info *opt)
577
{
588
diffcore_std(&opt->diffopt);
599
if (diff_queue_is_empty()) {
@@ -73,7 +23,7 @@ int log_tree_diff_flush(struct log_tree_opt *opt)
7323
return 1;
7424
}
7525

76-
static int diff_root_tree(struct log_tree_opt *opt,
26+
static int diff_root_tree(struct rev_info *opt,
7727
const unsigned char *new, const char *base)
7828
{
7929
int retval;
@@ -93,7 +43,7 @@ static int diff_root_tree(struct log_tree_opt *opt,
9343
return retval;
9444
}
9545

96-
static const char *generate_header(struct log_tree_opt *opt,
46+
static const char *generate_header(struct rev_info *opt,
9747
const unsigned char *commit_sha1,
9848
const unsigned char *parent_sha1,
9949
const struct commit *commit)
@@ -129,7 +79,7 @@ static const char *generate_header(struct log_tree_opt *opt,
12979
return this_header;
13080
}
13181

132-
static int do_diff_combined(struct log_tree_opt *opt, struct commit *commit)
82+
static int do_diff_combined(struct rev_info *opt, struct commit *commit)
13383
{
13484
unsigned const char *sha1 = commit->object.sha1;
13585

@@ -142,7 +92,7 @@ static int do_diff_combined(struct log_tree_opt *opt, struct commit *commit)
14292
return 0;
14393
}
14494

145-
int log_tree_commit(struct log_tree_opt *opt, struct commit *commit)
95+
int log_tree_commit(struct rev_info *opt, struct commit *commit)
14696
{
14797
struct commit_list *parents;
14898
unsigned const char *sha1 = commit->object.sha1;

log-tree.h

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
#ifndef LOG_TREE_H
22
#define LOG_TREE_H
33

4-
struct log_tree_opt {
5-
struct diff_options diffopt;
6-
int show_root_diff;
7-
int no_commit_id;
8-
int verbose_header;
9-
int ignore_merges;
10-
int combine_merges;
11-
int dense_combined_merges;
12-
int always_show_header;
13-
const char *header_prefix;
14-
const char *header;
15-
enum cmit_fmt commit_format;
16-
};
4+
#include "revision.h"
175

18-
void init_log_tree_opt(struct log_tree_opt *);
19-
int log_tree_diff_flush(struct log_tree_opt *);
20-
int log_tree_commit(struct log_tree_opt *, struct commit *);
21-
int log_tree_opt_parse(struct log_tree_opt *, const char **, int);
6+
void init_log_tree_opt(struct rev_info *);
7+
int log_tree_diff_flush(struct rev_info *);
8+
int log_tree_commit(struct rev_info *, struct commit *);
9+
int log_tree_opt_parse(struct rev_info *, const char **, int);
2210

2311
#endif

0 commit comments

Comments
 (0)