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 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
231 struct commit
*commit
;
232 struct commit
**list
= NULL
;
234 int nr
= 0, total
, i
, j
;
237 int start_number
= -1;
238 int keep_subject
= 0;
239 int ignore_if_in_upstream
= 0;
240 struct diff_options patch_id_opts
;
241 char *add_signoff
= NULL
;
243 git_config(git_format_config
);
244 init_revisions(&rev
);
245 rev
.commit_format
= CMIT_FMT_EMAIL
;
246 rev
.verbose_header
= 1;
248 rev
.combine_merges
= 0;
249 rev
.ignore_merges
= 1;
250 rev
.diffopt
.msg_sep
= "";
251 rev
.diffopt
.recursive
= 1;
253 rev
.extra_headers
= extra_headers
;
256 * Parse the arguments before setup_revisions(), or something
257 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
258 * possibly a valid SHA1.
260 for (i
= 1, j
= 1; i
< argc
; i
++) {
261 if (!strcmp(argv
[i
], "--stdout"))
263 else if (!strcmp(argv
[i
], "-n") ||
264 !strcmp(argv
[i
], "--numbered"))
266 else if (!strncmp(argv
[i
], "--start-number=", 15))
267 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
268 else if (!strcmp(argv
[i
], "--start-number")) {
271 die("Need a number for --start-number");
272 start_number
= strtol(argv
[i
], NULL
, 10);
274 else if (!strcmp(argv
[i
], "-k") ||
275 !strcmp(argv
[i
], "--keep-subject")) {
279 else if (!strcmp(argv
[i
], "--output-directory") ||
280 !strcmp(argv
[i
], "-o")) {
283 die("Which directory?");
284 if (output_directory
)
285 die("Two output directories?");
286 output_directory
= argv
[i
];
288 else if (!strcmp(argv
[i
], "--signoff") ||
289 !strcmp(argv
[i
], "-s")) {
290 const char *committer
;
293 committer
= git_committer_info(1);
294 endpos
= strchr(committer
, '>');
296 die("bogos committer info %s\n", committer
);
297 add_signoff
= xmalloc(endpos
- committer
+ 2);
298 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
299 add_signoff
[endpos
- committer
+ 1] = 0;
301 else if (!strcmp(argv
[i
], "--attach"))
302 rev
.mime_boundary
= git_version_string
;
303 else if (!strncmp(argv
[i
], "--attach=", 9))
304 rev
.mime_boundary
= argv
[i
] + 9;
305 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
306 ignore_if_in_upstream
= 1;
312 if (start_number
< 0)
314 if (numbered
&& keep_subject
)
315 die ("-n and -k are mutually exclusive.");
317 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
319 die ("unrecognized argument: %s", argv
[1]);
321 if (!rev
.diffopt
.output_format
)
322 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
324 if (output_directory
) {
326 die("standard output, or directory, which one?");
327 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
328 die("Could not create directory %s",
332 if (rev
.pending
.nr
== 1) {
333 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
337 if (ignore_if_in_upstream
)
338 get_patch_ids(&rev
, &patch_id_opts
);
341 realstdout
= fdopen(dup(1), "w");
343 prepare_revision_walk(&rev
);
344 while ((commit
= get_revision(&rev
)) != NULL
) {
345 unsigned char sha1
[20];
348 if (commit
->parents
&& commit
->parents
->next
)
351 if (ignore_if_in_upstream
&&
352 !get_patch_id(commit
, &patch_id_opts
, sha1
) &&
357 list
= realloc(list
, nr
* sizeof(list
[0]));
358 list
[nr
- 1] = commit
;
362 rev
.total
= total
+ start_number
- 1;
363 rev
.add_signoff
= add_signoff
;
367 rev
.nr
= total
- nr
+ (start_number
- 1);
369 reopen_stdout(commit
, rev
.nr
, keep_subject
);
370 shown
= log_tree_commit(&rev
, commit
);
371 free(commit
->buffer
);
372 commit
->buffer
= NULL
;
374 /* We put one extra blank line between formatted
375 * patches and this flag is used by log-tree code
376 * to see if it needs to emit a LF before showing
377 * the log; when using one file per patch, we do
378 * not want the extra blank line.
383 if (rev
.mime_boundary
)
384 printf("\n--%s%s--\n\n\n",
385 mime_boundary_leader
,
388 printf("-- \n%s\n\n", git_version_string
);