@matheusbsbs / matheus-bagliano / commits / 3eefc18917
Fork 0

Tentative built-in format-patch.

This only does --stdout right now. To write into separate files with pretty-printed filenames like the real thing does, it needs a bit mroe work. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 18, 2006 at 16:45 UTC 3eefc189172b88dece6fb6d479b3ed13cc483dbc
4 files changed +89 -13
commit.c
+31 -5
@@ -36,6 +36,8 @@ enum cmit_fmt get_commit_format(const char *arg)
36 return CMIT_FMT_FULL;
37 if (!strcmp(arg, "=fuller"))
38 return CMIT_FMT_FULLER;
39 + if (!strcmp(arg, "=email"))
40 + return CMIT_FMT_EMAIL;
41 if (!strcmp(arg, "=oneline"))
42 return CMIT_FMT_ONELINE;
43 die("invalid --pretty format");
@@ -428,6 +430,10 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
430 time = strtoul(date, &date, 10);
431 tz = strtol(date, NULL, 10);
432
433 + if (fmt == CMIT_FMT_EMAIL) {
434 + what = "From";
435 + filler = "";
436 + }
437 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
438 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
439 filler, namelen, line);
@@ -435,6 +441,9 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
441 case CMIT_FMT_MEDIUM:
442 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
443 break;
444 + case CMIT_FMT_EMAIL:
445 + ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
446 + break;
447 case CMIT_FMT_FULLER:
448 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
449 break;
@@ -445,10 +454,12 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
454 return ret;
455 }
456
448 -static int is_empty_line(const char *line, int len)
457 +static int is_empty_line(const char *line, int *len_p)
458 {
459 + int len = *len_p;
460 while (len && isspace(line[len-1]))
461 len--;
462 + *len_p = len;
463 return !len;
464 }
465
@@ -457,7 +468,8 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
468 struct commit_list *parent = commit->parents;
469 int offset;
470
460 - if ((fmt == CMIT_FMT_ONELINE) || !parent || !parent->next)
471 + if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
472 + !parent || !parent->next)
473 return 0;
474
475 offset = sprintf(buf, "Merge:");
@@ -480,9 +492,15 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
492 {
493 int hdr = 1, body = 0;
494 unsigned long offset = 0;
483 - int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
495 + int indent = 4;
496 int parents_shown = 0;
497 const char *msg = commit->buffer;
498 + const char *subject = NULL;
499 +
500 + if (fmt == CMIT_FMT_EMAIL)
501 + subject = "Subject: ";
502 + if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
503 + indent = 0;
504
505 for (;;) {
506 const char *line = msg;
@@ -506,7 +524,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
524 if (hdr) {
525 if (linelen == 1) {
526 hdr = 0;
509 - if (fmt != CMIT_FMT_ONELINE)
527 + if ((fmt != CMIT_FMT_ONELINE) && !subject)
528 buf[offset++] = '\n';
529 continue;
530 }
@@ -544,20 +562,28 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
562 continue;
563 }
564
547 - if (is_empty_line(line, linelen)) {
565 + if (is_empty_line(line, &linelen)) {
566 if (!body)
567 continue;
568 + if (subject)
569 + continue;
570 if (fmt == CMIT_FMT_SHORT)
571 break;
572 } else {
573 body = 1;
574 }
575
576 + if (subject) {
577 + memcpy(buf + offset, subject, 9);
578 + offset += 9;
579 + }
580 memset(buf + offset, ' ', indent);
581 memcpy(buf + offset + indent, line, linelen);
582 offset += linelen + indent;
583 + buf[offset++] = '\n';
584 if (fmt == CMIT_FMT_ONELINE)
585 break;
586 + subject = NULL;
587 }
588 while (offset && isspace(buf[offset-1]))
589 offset--;
commit.h
+1
@@ -45,6 +45,7 @@ enum cmit_fmt {
45 CMIT_FMT_FULL,
46 CMIT_FMT_FULLER,
47 CMIT_FMT_ONELINE,
48 + CMIT_FMT_EMAIL,
49
50 CMIT_FMT_UNSPECIFIED,
51 };
git.c
+38
@@ -276,6 +276,43 @@ static int cmd_help(int argc, const char **argv, char **envp)
276 return 0;
277 }
278
279 +static int cmd_format_patch(int argc, const char **argv, char **envp)
280 +{
281 + struct commit *commit;
282 + struct commit **list = NULL;
283 + struct rev_info rev;
284 + int nr = 0;
285 +
286 + init_revisions(&rev);
287 + rev.commit_format = CMIT_FMT_EMAIL;
288 + rev.verbose_header = 1;
289 + rev.diff = 1;
290 + rev.diffopt.with_raw = 0;
291 + rev.diffopt.with_stat = 1;
292 + rev.combine_merges = 0;
293 + rev.ignore_merges = 1;
294 + rev.diffopt.output_format = DIFF_FORMAT_PATCH;
295 + argc = setup_revisions(argc, argv, &rev, "HEAD");
296 +
297 + prepare_revision_walk(&rev);
298 + while ((commit = get_revision(&rev)) != NULL) {
299 + nr++;
300 + list = realloc(list, nr * sizeof(list[0]));
301 + list[nr - 1] = commit;
302 + }
303 + while (0 <= --nr) {
304 + int shown;
305 + commit = list[nr];
306 + shown = log_tree_commit(&rev, commit);
307 + free(commit->buffer);
308 + commit->buffer = NULL;
309 + if (shown)
310 + printf("-- \n%s\n\n", GIT_VERSION);
311 + }
312 + free(list);
313 + return 0;
314 +}
315 +
316 static int cmd_log_wc(int argc, const char **argv, char **envp,
317 struct rev_info *rev)
318 {
@@ -348,6 +385,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
385 { "log", cmd_log },
386 { "whatchanged", cmd_wc },
387 { "show", cmd_show },
388 + { "format-patch", cmd_format_patch },
389 };
390 int i;
391
log-tree.c
+19 -8
@@ -37,12 +37,20 @@ void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
37 /*
38 * Print header line of header..
39 */
40 - printf("%s%s",
41 - opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
42 - diff_unique_abbrev(commit->object.sha1, abbrev_commit));
43 - if (parent)
44 - printf(" (from %s)", diff_unique_abbrev(parent->object.sha1, abbrev_commit));
45 - putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
40 +
41 + if (opt->commit_format == CMIT_FMT_EMAIL)
42 + printf("From %s Thu Apr 7 15:13:13 2005\n",
43 + sha1_to_hex(commit->object.sha1));
44 + else {
45 + printf("%s%s",
46 + opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
47 + diff_unique_abbrev(commit->object.sha1, abbrev_commit));
48 + if (parent)
49 + printf(" (from %s)",
50 + diff_unique_abbrev(parent->object.sha1,
51 + abbrev_commit));
52 + putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
53 + }
54
55 /*
56 * And then the pretty-printed message itself
@@ -152,15 +160,18 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
160 int log_tree_commit(struct rev_info *opt, struct commit *commit)
161 {
162 struct log_info log;
163 + int shown;
164
165 log.commit = commit;
166 log.parent = NULL;
167 opt->loginfo = &log;
168
160 - if (!log_tree_diff(opt, commit, &log) && opt->loginfo && opt->always_show_header) {
169 + shown = log_tree_diff(opt, commit, &log);
170 + if (!shown && opt->loginfo && opt->always_show_header) {
171 log.parent = NULL;
172 show_log(opt, opt->loginfo, "");
173 + shown = 1;
174 }
175 opt->loginfo = NULL;
165 - return 0;
176 + return shown;
177 }