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_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_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_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 return git_diff_config(var
, value
);
112 static FILE *realstdout
= NULL
;
113 static const char *output_directory
= NULL
;
115 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
121 if (output_directory
) {
122 strlcpy(filename
, output_directory
, 1010);
123 len
= strlen(filename
);
124 if (filename
[len
- 1] != '/')
125 filename
[len
++] = '/';
128 sprintf(filename
+ len
, "%04d", nr
);
129 len
= strlen(filename
);
131 sol
= strstr(commit
->buffer
, "\n\n");
136 /* strip [PATCH] or [PATCH blabla] */
137 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
138 char *eos
= strchr(sol
+ 6, ']');
140 while (isspace(*eos
))
146 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
147 if (istitlechar(sol
[j
])) {
149 filename
[len
++] = '-';
152 filename
[len
++] = sol
[j
];
154 while (sol
[j
+ 1] == '.')
159 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
162 strcpy(filename
+ len
, ".txt");
163 fprintf(realstdout
, "%s\n", filename
);
164 freopen(filename
, "w", stdout
);
167 static int get_patch_id(struct commit
*commit
, struct diff_options
*options
,
170 diff_tree_sha1(commit
->parents
->item
->object
.sha1
, commit
->object
.sha1
,
172 diffcore_std(options
);
173 return diff_flush_patch_id(options
, sha1
);
176 static void get_patch_ids(struct rev_info
*rev
, struct diff_options
*options
)
178 struct rev_info check_rev
;
179 struct commit
*commit
;
180 struct object
*o1
, *o2
;
181 unsigned flags1
, flags2
;
182 unsigned char sha1
[20];
184 if (rev
->pending
.nr
!= 2)
185 die("Need exactly one range.");
187 o1
= rev
->pending
.objects
[0].item
;
189 o2
= rev
->pending
.objects
[1].item
;
192 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
196 options
->recursive
= 1;
197 if (diff_setup_done(options
) < 0)
198 die("diff_setup_done failed");
200 /* given a range a..b get all patch ids for b..a */
201 init_revisions(&check_rev
);
202 o1
->flags
^= UNINTERESTING
;
203 o2
->flags
^= UNINTERESTING
;
204 add_pending_object(&check_rev
, o1
, "o1");
205 add_pending_object(&check_rev
, o2
, "o2");
206 prepare_revision_walk(&check_rev
);
208 while ((commit
= get_revision(&check_rev
)) != NULL
) {
210 if (commit
->parents
&& commit
->parents
->next
)
213 if (!get_patch_id(commit
, options
, sha1
))
214 created_object(sha1
, xcalloc(1, sizeof(struct object
)));
217 /* reset for next revision walk */
218 clear_commit_marks((struct commit
*)o1
,
219 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
220 clear_commit_marks((struct commit
*)o2
,
221 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
226 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
228 struct commit
*commit
;
229 struct commit
**list
= NULL
;
231 int nr
= 0, total
, i
, j
;
234 int start_number
= -1;
235 int keep_subject
= 0;
236 int ignore_if_in_upstream
= 0;
237 struct diff_options patch_id_opts
;
238 char *add_signoff
= NULL
;
240 git_config(git_format_config
);
241 init_revisions(&rev
);
242 rev
.commit_format
= CMIT_FMT_EMAIL
;
243 rev
.verbose_header
= 1;
245 rev
.combine_merges
= 0;
246 rev
.ignore_merges
= 1;
247 rev
.diffopt
.msg_sep
= "";
248 rev
.diffopt
.recursive
= 1;
250 rev
.extra_headers
= extra_headers
;
253 * Parse the arguments before setup_revisions(), or something
254 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
255 * possibly a valid SHA1.
257 for (i
= 1, j
= 1; i
< argc
; i
++) {
258 if (!strcmp(argv
[i
], "--stdout"))
260 else if (!strcmp(argv
[i
], "-n") ||
261 !strcmp(argv
[i
], "--numbered"))
263 else if (!strncmp(argv
[i
], "--start-number=", 15))
264 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
265 else if (!strcmp(argv
[i
], "--start-number")) {
268 die("Need a number for --start-number");
269 start_number
= strtol(argv
[i
], NULL
, 10);
271 else if (!strcmp(argv
[i
], "-k") ||
272 !strcmp(argv
[i
], "--keep-subject")) {
276 else if (!strcmp(argv
[i
], "--output-directory") ||
277 !strcmp(argv
[i
], "-o")) {
280 die("Which directory?");
281 if (output_directory
)
282 die("Two output directories?");
283 output_directory
= argv
[i
];
285 else if (!strcmp(argv
[i
], "--signoff") ||
286 !strcmp(argv
[i
], "-s")) {
287 const char *committer
;
290 committer
= git_committer_info(1);
291 endpos
= strchr(committer
, '>');
293 die("bogos committer info %s\n", committer
);
294 add_signoff
= xmalloc(endpos
- committer
+ 2);
295 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
296 add_signoff
[endpos
- committer
+ 1] = 0;
298 else if (!strcmp(argv
[i
], "--attach"))
299 rev
.mime_boundary
= git_version_string
;
300 else if (!strncmp(argv
[i
], "--attach=", 9))
301 rev
.mime_boundary
= argv
[i
] + 9;
302 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
303 ignore_if_in_upstream
= 1;
309 if (start_number
< 0)
311 if (numbered
&& keep_subject
)
312 die ("-n and -k are mutually exclusive.");
314 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
316 die ("unrecognized argument: %s", argv
[1]);
318 if (!rev
.diffopt
.output_format
)
319 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
321 if (output_directory
) {
323 die("standard output, or directory, which one?");
324 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
325 die("Could not create directory %s",
329 if (rev
.pending
.nr
== 1) {
330 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
334 if (ignore_if_in_upstream
)
335 get_patch_ids(&rev
, &patch_id_opts
);
338 realstdout
= fdopen(dup(1), "w");
340 prepare_revision_walk(&rev
);
341 while ((commit
= get_revision(&rev
)) != NULL
) {
342 unsigned char sha1
[20];
345 if (commit
->parents
&& commit
->parents
->next
)
348 if (ignore_if_in_upstream
&&
349 !get_patch_id(commit
, &patch_id_opts
, sha1
) &&
354 list
= realloc(list
, nr
* sizeof(list
[0]));
355 list
[nr
- 1] = commit
;
359 rev
.total
= total
+ start_number
- 1;
360 rev
.add_signoff
= add_signoff
;
364 rev
.nr
= total
- nr
+ (start_number
- 1);
366 reopen_stdout(commit
, rev
.nr
, keep_subject
);
367 shown
= log_tree_commit(&rev
, commit
);
368 free(commit
->buffer
);
369 commit
->buffer
= NULL
;
371 /* We put one extra blank line between formatted
372 * patches and this flag is used by log-tree code
373 * to see if it needs to emit a LF before showing
374 * the log; when using one file per patch, we do
375 * not want the extra blank line.
380 if (rev
.mime_boundary
)
381 printf("\n--%s%s--\n\n\n",
382 mime_boundary_leader
,
385 printf("-- \n%s\n\n", git_version_string
);