2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 /* this is in builtin-diff.c */
15 void add_head(struct rev_info
*revs
);
17 static void cmd_log_init(int argc
, const char **argv
, char **envp
,
20 rev
->abbrev
= DEFAULT_ABBREV
;
21 rev
->commit_format
= CMIT_FMT_DEFAULT
;
22 rev
->verbose_header
= 1;
23 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
24 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
25 rev
->always_show_header
= 0;
27 die("unrecognized argument: %s", argv
[1]);
30 static int cmd_log_walk(struct rev_info
*rev
)
32 struct commit
*commit
;
34 prepare_revision_walk(rev
);
36 while ((commit
= get_revision(rev
)) != NULL
) {
37 log_tree_commit(rev
, commit
);
39 commit
->buffer
= NULL
;
40 free_commit_list(commit
->parents
);
41 commit
->parents
= NULL
;
46 int cmd_whatchanged(int argc
, const char **argv
, char **envp
)
50 git_config(git_diff_ui_config
);
53 rev
.diffopt
.recursive
= 1;
54 rev
.simplify_history
= 0;
55 cmd_log_init(argc
, argv
, envp
, &rev
);
56 if (!rev
.diffopt
.output_format
)
57 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
58 return cmd_log_walk(&rev
);
61 int cmd_show(int argc
, const char **argv
, char **envp
)
65 git_config(git_diff_ui_config
);
68 rev
.diffopt
.recursive
= 1;
69 rev
.combine_merges
= 1;
70 rev
.dense_combined_merges
= 1;
71 rev
.always_show_header
= 1;
72 rev
.ignore_merges
= 0;
74 cmd_log_init(argc
, argv
, envp
, &rev
);
75 return cmd_log_walk(&rev
);
78 int cmd_log(int argc
, const char **argv
, char **envp
)
82 git_config(git_diff_ui_config
);
84 rev
.always_show_header
= 1;
85 cmd_log_init(argc
, argv
, envp
, &rev
);
86 return cmd_log_walk(&rev
);
89 static int istitlechar(char c
)
91 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
92 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
95 static char *extra_headers
= NULL
;
96 static int extra_headers_size
= 0;
98 static int git_format_config(const char *var
, const char *value
)
100 if (!strcmp(var
, "format.headers")) {
101 int len
= strlen(value
);
102 extra_headers_size
+= len
+ 1;
103 extra_headers
= realloc(extra_headers
, extra_headers_size
);
104 extra_headers
[extra_headers_size
- len
- 1] = 0;
105 strcat(extra_headers
, value
);
108 if (!strcmp(var
, "diff.color")) {
111 return git_diff_ui_config(var
, value
);
115 static FILE *realstdout
= NULL
;
116 static const char *output_directory
= NULL
;
118 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
124 if (output_directory
) {
125 strlcpy(filename
, output_directory
, 1010);
126 len
= strlen(filename
);
127 if (filename
[len
- 1] != '/')
128 filename
[len
++] = '/';
131 sprintf(filename
+ len
, "%04d", nr
);
132 len
= strlen(filename
);
134 sol
= strstr(commit
->buffer
, "\n\n");
139 /* strip [PATCH] or [PATCH blabla] */
140 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
141 char *eos
= strchr(sol
+ 6, ']');
143 while (isspace(*eos
))
149 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
150 if (istitlechar(sol
[j
])) {
152 filename
[len
++] = '-';
155 filename
[len
++] = sol
[j
];
157 while (sol
[j
+ 1] == '.')
162 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
165 strcpy(filename
+ len
, ".txt");
166 fprintf(realstdout
, "%s\n", filename
);
167 freopen(filename
, "w", stdout
);
170 static int get_patch_id(struct commit
*commit
, struct diff_options
*options
,
173 diff_tree_sha1(commit
->parents
->item
->object
.sha1
, commit
->object
.sha1
,
175 diffcore_std(options
);
176 return diff_flush_patch_id(options
, sha1
);
179 static void get_patch_ids(struct rev_info
*rev
, struct diff_options
*options
)
181 struct rev_info check_rev
;
182 struct commit
*commit
;
183 struct object
*o1
, *o2
;
184 unsigned flags1
, flags2
;
185 unsigned char sha1
[20];
187 if (rev
->pending
.nr
!= 2)
188 die("Need exactly one range.");
190 o1
= rev
->pending
.objects
[0].item
;
192 o2
= rev
->pending
.objects
[1].item
;
195 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
199 options
->recursive
= 1;
200 if (diff_setup_done(options
) < 0)
201 die("diff_setup_done failed");
203 /* given a range a..b get all patch ids for b..a */
204 init_revisions(&check_rev
);
205 o1
->flags
^= UNINTERESTING
;
206 o2
->flags
^= UNINTERESTING
;
207 add_pending_object(&check_rev
, o1
, "o1");
208 add_pending_object(&check_rev
, o2
, "o2");
209 prepare_revision_walk(&check_rev
);
211 while ((commit
= get_revision(&check_rev
)) != NULL
) {
213 if (commit
->parents
&& commit
->parents
->next
)
216 if (!get_patch_id(commit
, options
, sha1
))
217 created_object(sha1
, xcalloc(1, sizeof(struct object
)));
220 /* reset for next revision walk */
221 clear_commit_marks((struct commit
*)o1
,
222 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
223 clear_commit_marks((struct commit
*)o2
,
224 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
229 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
231 const char *committer
= git_committer_info(1);
232 const char *email_start
= strrchr(committer
, '<');
233 const char *email_end
= strrchr(committer
, '>');
234 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
235 die("Could not extract email from committer identity.");
236 snprintf(dest
, length
, "%s.%u.git.%.*s", base
, time(NULL
),
237 email_end
- email_start
- 1, email_start
+ 1);
240 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
242 struct commit
*commit
;
243 struct commit
**list
= NULL
;
245 int nr
= 0, total
, i
, j
;
248 int start_number
= -1;
249 int keep_subject
= 0;
250 int ignore_if_in_upstream
= 0;
252 struct diff_options patch_id_opts
;
253 char *add_signoff
= NULL
;
254 char message_id
[1024];
255 char ref_message_id
[1024];
257 git_config(git_format_config
);
258 init_revisions(&rev
);
259 rev
.commit_format
= CMIT_FMT_EMAIL
;
260 rev
.verbose_header
= 1;
262 rev
.combine_merges
= 0;
263 rev
.ignore_merges
= 1;
264 rev
.diffopt
.msg_sep
= "";
265 rev
.diffopt
.recursive
= 1;
267 rev
.extra_headers
= extra_headers
;
270 * Parse the arguments before setup_revisions(), or something
271 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
272 * possibly a valid SHA1.
274 for (i
= 1, j
= 1; i
< argc
; i
++) {
275 if (!strcmp(argv
[i
], "--stdout"))
277 else if (!strcmp(argv
[i
], "-n") ||
278 !strcmp(argv
[i
], "--numbered"))
280 else if (!strncmp(argv
[i
], "--start-number=", 15))
281 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
282 else if (!strcmp(argv
[i
], "--start-number")) {
285 die("Need a number for --start-number");
286 start_number
= strtol(argv
[i
], NULL
, 10);
288 else if (!strcmp(argv
[i
], "-k") ||
289 !strcmp(argv
[i
], "--keep-subject")) {
293 else if (!strcmp(argv
[i
], "--output-directory") ||
294 !strcmp(argv
[i
], "-o")) {
297 die("Which directory?");
298 if (output_directory
)
299 die("Two output directories?");
300 output_directory
= argv
[i
];
302 else if (!strcmp(argv
[i
], "--signoff") ||
303 !strcmp(argv
[i
], "-s")) {
304 const char *committer
;
307 committer
= git_committer_info(1);
308 endpos
= strchr(committer
, '>');
310 die("bogos committer info %s\n", committer
);
311 add_signoff
= xmalloc(endpos
- committer
+ 2);
312 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
313 add_signoff
[endpos
- committer
+ 1] = 0;
315 else if (!strcmp(argv
[i
], "--attach"))
316 rev
.mime_boundary
= git_version_string
;
317 else if (!strncmp(argv
[i
], "--attach=", 9))
318 rev
.mime_boundary
= argv
[i
] + 9;
319 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
320 ignore_if_in_upstream
= 1;
321 else if (!strcmp(argv
[i
], "--thread"))
328 if (start_number
< 0)
330 if (numbered
&& keep_subject
)
331 die ("-n and -k are mutually exclusive.");
333 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
335 die ("unrecognized argument: %s", argv
[1]);
337 if (!rev
.diffopt
.output_format
)
338 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
340 if (output_directory
) {
342 die("standard output, or directory, which one?");
343 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
344 die("Could not create directory %s",
348 if (rev
.pending
.nr
== 1) {
349 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
353 if (ignore_if_in_upstream
)
354 get_patch_ids(&rev
, &patch_id_opts
);
357 realstdout
= fdopen(dup(1), "w");
359 prepare_revision_walk(&rev
);
360 while ((commit
= get_revision(&rev
)) != NULL
) {
361 unsigned char sha1
[20];
364 if (commit
->parents
&& commit
->parents
->next
)
367 if (ignore_if_in_upstream
&&
368 !get_patch_id(commit
, &patch_id_opts
, sha1
) &&
373 list
= realloc(list
, nr
* sizeof(list
[0]));
374 list
[nr
- 1] = commit
;
378 rev
.total
= total
+ start_number
- 1;
379 rev
.add_signoff
= add_signoff
;
383 rev
.nr
= total
- nr
+ (start_number
- 1);
384 /* Make the second and subsequent mails replies to the first */
386 if (nr
== (total
- 2)) {
387 strncpy(ref_message_id
, message_id
,
388 sizeof(ref_message_id
));
389 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
390 rev
.ref_message_id
= ref_message_id
;
392 gen_message_id(message_id
, sizeof(message_id
),
393 sha1_to_hex(commit
->object
.sha1
));
394 rev
.message_id
= message_id
;
397 reopen_stdout(commit
, rev
.nr
, keep_subject
);
398 shown
= log_tree_commit(&rev
, commit
);
399 free(commit
->buffer
);
400 commit
->buffer
= NULL
;
402 /* We put one extra blank line between formatted
403 * patches and this flag is used by log-tree code
404 * to see if it needs to emit a LF before showing
405 * the log; when using one file per patch, we do
406 * not want the extra blank line.
411 if (rev
.mime_boundary
)
412 printf("\n--%s%s--\n\n\n",
413 mime_boundary_leader
,
416 printf("-- \n%s\n\n", git_version_string
);