@matheusbsbs / matheus-bagliano / commits / f4235f8b2e
Fork 0

Extract "log [diff options]" parser out.

Merging of the log-tree-opt structure with rev-info structure did not work out very well and it broke things that did not want diff options and/or rev options. This is an alternative approach to define a combined interface that can be used by commands that wants both. The use of it is opt-in to reduce the risk of breaking existing programs. We might want to slurp "setup_revisions() places things in pending objects list" part from Linus's earlier attempt. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 15, 2006 at 03:16 UTC f4235f8b2ef875b85ead74ffa199d827f9ee9d8d
3 files changed +100 -75
git.c
+21 -75
@@ -280,89 +280,35 @@ static int cmd_help(int argc, const char **argv, char **envp)
280
281 static int cmd_log(int argc, const char **argv, char **envp)
282 {
283 - struct rev_info rev;
283 + struct whatchanged_opt wcopt;
284 struct commit *commit;
285 char *buf = xmalloc(LOGSIZE);
286 - static enum cmit_fmt commit_format = CMIT_FMT_DEFAULT;
287 - int abbrev = DEFAULT_ABBREV;
288 - int abbrev_commit = 0;
286 const char *commit_prefix = "commit ";
290 - struct log_tree_opt opt;
287 int shown = 0;
292 - int do_diff = 0;
293 - int full_diff = 0;
294 -
295 - init_log_tree_opt(&opt);
296 - 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 - }
288 + struct rev_info *rev = &wcopt.revopt;
289 + struct log_tree_opt *opt = &wcopt.logopt;
290
335 - argc--; argv++;
336 - }
291 + init_log_tree_opt(&wcopt.logopt);
292 + wcopt.commit_format = CMIT_FMT_DEFAULT;
293 + wcopt.abbrev = DEFAULT_ABBREV;
294 + argc = parse_whatchanged_opt(argc, argv, &wcopt);
295
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 - }
296 + if (wcopt.logopt.commit_format == CMIT_FMT_ONELINE)
297 + commit_prefix = "";
298
354 - prepare_revision_walk(&rev);
299 + prepare_revision_walk(rev);
300 setup_pager();
356 - while ((commit = get_revision(&rev)) != NULL) {
357 - if (shown && do_diff && commit_format != CMIT_FMT_ONELINE)
301 + while ((commit = get_revision(rev)) != NULL) {
302 + if (shown && wcopt.do_diff &&
303 + wcopt.commit_format != CMIT_FMT_ONELINE)
304 putchar('\n');
305 fputs(commit_prefix, stdout);
360 - if (abbrev_commit && abbrev)
361 - fputs(find_unique_abbrev(commit->object.sha1, abbrev),
306 + if (wcopt.abbrev_commit && wcopt.abbrev)
307 + fputs(find_unique_abbrev(commit->object.sha1, wcopt.abbrev),
308 stdout);
309 else
310 fputs(sha1_to_hex(commit->object.sha1), stdout);
365 - if (rev.parents) {
311 + if (rev->parents) {
312 struct commit_list *parents = commit->parents;
313 while (parents) {
314 struct object *o = &(parents->item->object);
@@ -381,15 +327,15 @@ static int cmd_log(int argc, const char **argv, char **envp)
327 parents = parents->next)
328 parents->item->object.flags &= ~TMP_MARK;
329 }
384 - if (commit_format == CMIT_FMT_ONELINE)
330 + if (wcopt.commit_format == CMIT_FMT_ONELINE)
331 putchar(' ');
332 else
333 putchar('\n');
388 - pretty_print_commit(commit_format, commit, ~0, buf,
389 - LOGSIZE, abbrev);
334 + pretty_print_commit(wcopt.commit_format, commit, ~0, buf,
335 + LOGSIZE, wcopt.abbrev);
336 printf("%s\n", buf);
391 - if (do_diff)
392 - log_tree_commit(&opt, commit);
337 + if (wcopt.do_diff)
338 + log_tree_commit(opt, commit);
339 shown = 1;
340 free(commit->buffer);
341 commit->buffer = NULL;
log-tree.c
+65
@@ -173,3 +173,68 @@ int log_tree_commit(struct log_tree_opt *opt, struct commit *commit)
173 }
174 return 0;
175 }
176 +
177 +int parse_whatchanged_opt(int ac, const char **av, struct whatchanged_opt *wcopt)
178 +{
179 + struct rev_info *rev = &wcopt->revopt;
180 + struct log_tree_opt *opt = &wcopt->logopt;
181 + const char **unrecognized = av+1;
182 + int left = 1;
183 +
184 + ac = setup_revisions(ac, av, rev, "HEAD");
185 + while (1 < ac) {
186 + const char *arg = av[1];
187 + if (!strncmp(arg, "--pretty", 8)) {
188 + opt->commit_format = get_commit_format(arg + 8);
189 + }
190 + else if (!strcmp(arg, "--no-abbrev")) {
191 + wcopt->abbrev = 0;
192 + }
193 + else if (!strcmp(arg, "--abbrev")) {
194 + wcopt->abbrev = DEFAULT_ABBREV;
195 + }
196 + else if (!strcmp(arg, "--abbrev-commit")) {
197 + wcopt->abbrev_commit = 1;
198 + }
199 + else if (!strncmp(arg, "--abbrev=", 9)) {
200 + wcopt->abbrev = strtoul(arg + 9, NULL, 10);
201 + if (wcopt->abbrev && wcopt->abbrev < MINIMUM_ABBREV)
202 + wcopt->abbrev = MINIMUM_ABBREV;
203 + else if (40 < wcopt->abbrev)
204 + wcopt->abbrev = 40;
205 + }
206 + else if (!strcmp(arg, "--full-diff")) {
207 + wcopt->do_diff = 1;
208 + wcopt->full_diff = 1;
209 + }
210 + else {
211 + int cnt = log_tree_opt_parse(opt, av+1, ac-1);
212 + if (0 < cnt) {
213 + wcopt->do_diff = 1;
214 + av += cnt;
215 + ac -= cnt;
216 + continue;
217 + }
218 + *unrecognized++ = arg;
219 + left++;
220 + }
221 + ac--; av++;
222 + }
223 +
224 + if (wcopt->do_diff) {
225 + opt->diffopt.abbrev = wcopt->abbrev;
226 + opt->verbose_header = 0;
227 + opt->always_show_header = 0;
228 + opt->no_commit_id = 1;
229 + if (opt->combine_merges)
230 + opt->ignore_merges = 0;
231 + if (opt->dense_combined_merges)
232 + opt->diffopt.output_format = DIFF_FORMAT_PATCH;
233 + if (opt->diffopt.output_format == DIFF_FORMAT_PATCH)
234 + opt->diffopt.recursive = 1;
235 + if (!wcopt->full_diff && rev->prune_data)
236 + diff_tree_setup_paths(rev->prune_data, &opt->diffopt);
237 + diff_setup_done(&opt->diffopt);
238 + }
239 + return left;
240 +}
log-tree.h
+14
@@ -1,6 +1,8 @@
1 #ifndef LOG_TREE_H
2 #define LOG_TREE_H
3
4 +#include "revision.h"
5 +
6 struct log_tree_opt {
7 struct diff_options diffopt;
8 int show_root_diff;
@@ -20,4 +22,16 @@ int log_tree_diff_flush(struct log_tree_opt *);
22 int log_tree_commit(struct log_tree_opt *, struct commit *);
23 int log_tree_opt_parse(struct log_tree_opt *, const char **, int);
24
25 +struct whatchanged_opt {
26 + struct rev_info revopt;
27 + struct log_tree_opt logopt;
28 + enum cmit_fmt commit_format;
29 + int abbrev;
30 + int abbrev_commit;
31 + int do_diff;
32 + int full_diff;
33 +};
34 +
35 +int parse_whatchanged_opt(int, const char **, struct whatchanged_opt *);
36 +
37 #endif