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 int cmd_log_wc(int argc
, const char **argv
, char **envp
,
20 struct commit
*commit
;
22 rev
->abbrev
= DEFAULT_ABBREV
;
23 rev
->commit_format
= CMIT_FMT_DEFAULT
;
24 rev
->verbose_header
= 1;
25 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
26 if (rev
->always_show_header
) {
27 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
) {
28 rev
->always_show_header
= 0;
29 if (rev
->diffopt
.output_format
== DIFF_FORMAT_RAW
)
30 rev
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
35 die("unrecognized argument: %s", argv
[1]);
37 prepare_revision_walk(rev
);
39 while ((commit
= get_revision(rev
)) != NULL
) {
40 log_tree_commit(rev
, commit
);
42 commit
->buffer
= NULL
;
47 int cmd_whatchanged(int argc
, const char **argv
, char **envp
)
53 rev
.diffopt
.recursive
= 1;
54 rev
.simplify_history
= 0;
55 return cmd_log_wc(argc
, argv
, envp
, &rev
);
58 int cmd_show(int argc
, const char **argv
, char **envp
)
64 rev
.diffopt
.recursive
= 1;
65 rev
.combine_merges
= 1;
66 rev
.dense_combined_merges
= 1;
67 rev
.always_show_header
= 1;
68 rev
.ignore_merges
= 0;
70 return cmd_log_wc(argc
, argv
, envp
, &rev
);
73 int cmd_log(int argc
, const char **argv
, char **envp
)
78 rev
.always_show_header
= 1;
79 rev
.diffopt
.recursive
= 1;
80 return cmd_log_wc(argc
, argv
, envp
, &rev
);
83 static int istitlechar(char c
)
85 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
86 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
89 static char *extra_headers
= NULL
;
90 static int extra_headers_size
= 0;
92 static int git_format_config(const char *var
, const char *value
)
94 if (!strcmp(var
, "format.headers")) {
95 int len
= strlen(value
);
96 extra_headers_size
+= len
+ 1;
97 extra_headers
= realloc(extra_headers
, extra_headers_size
);
98 extra_headers
[extra_headers_size
- len
- 1] = 0;
99 strcat(extra_headers
, value
);
102 return git_default_config(var
, value
);
106 static FILE *realstdout
= NULL
;
107 static const char *output_directory
= NULL
;
109 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
115 if (output_directory
) {
116 safe_strncpy(filename
, output_directory
, 1010);
117 len
= strlen(filename
);
118 if (filename
[len
- 1] != '/')
119 filename
[len
++] = '/';
122 sprintf(filename
+ len
, "%04d", nr
);
123 len
= strlen(filename
);
125 sol
= strstr(commit
->buffer
, "\n\n");
130 /* strip [PATCH] or [PATCH blabla] */
131 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
132 char *eos
= strchr(sol
+ 6, ']');
134 while (isspace(*eos
))
140 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
141 if (istitlechar(sol
[j
])) {
143 filename
[len
++] = '-';
146 filename
[len
++] = sol
[j
];
148 while (sol
[j
+ 1] == '.')
153 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
156 strcpy(filename
+ len
, ".txt");
157 fprintf(realstdout
, "%s\n", filename
);
158 freopen(filename
, "w", stdout
);
161 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
163 struct commit
*commit
;
164 struct commit
**list
= NULL
;
166 int nr
= 0, total
, i
, j
;
169 int start_number
= -1;
170 int keep_subject
= 0;
171 char *add_signoff
= NULL
;
173 init_revisions(&rev
);
174 rev
.commit_format
= CMIT_FMT_EMAIL
;
175 rev
.verbose_header
= 1;
177 rev
.diffopt
.with_raw
= 0;
178 rev
.diffopt
.with_stat
= 1;
179 rev
.combine_merges
= 0;
180 rev
.ignore_merges
= 1;
181 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
183 git_config(git_format_config
);
184 rev
.extra_headers
= extra_headers
;
187 * Parse the arguments before setup_revisions(), or something
188 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
189 * possibly a valid SHA1.
191 for (i
= 1, j
= 1; i
< argc
; i
++) {
192 if (!strcmp(argv
[i
], "--stdout"))
194 else if (!strcmp(argv
[i
], "-n") ||
195 !strcmp(argv
[i
], "--numbered"))
197 else if (!strncmp(argv
[i
], "--start-number=", 15))
198 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
199 else if (!strcmp(argv
[i
], "--start-number")) {
202 die("Need a number for --start-number");
203 start_number
= strtol(argv
[i
], NULL
, 10);
205 else if (!strcmp(argv
[i
], "-k") ||
206 !strcmp(argv
[i
], "--keep-subject")) {
210 else if (!strcmp(argv
[i
], "--output-directory") ||
211 !strcmp(argv
[i
], "-o")) {
214 die("Which directory?");
215 if (output_directory
)
216 die("Two output directories?");
217 output_directory
= argv
[i
];
219 else if (!strcmp(argv
[i
], "--signoff") ||
220 !strcmp(argv
[i
], "-s")) {
221 const char *committer
= git_committer_info(1);
222 const char *endpos
= strchr(committer
, '>');
224 die("bogos committer info %s\n", committer
);
225 add_signoff
= xmalloc(endpos
- committer
+ 2);
226 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
227 add_signoff
[endpos
- committer
+ 1] = 0;
229 else if (!strcmp(argv
[i
], "--attach"))
230 rev
.mime_boundary
= git_version_string
;
231 else if (!strncmp(argv
[i
], "--attach=", 9))
232 rev
.mime_boundary
= argv
[i
] + 9;
238 if (start_number
< 0)
240 if (numbered
&& keep_subject
)
241 die ("-n and -k are mutually exclusive.");
243 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
245 die ("unrecognized argument: %s", argv
[1]);
247 if (output_directory
) {
249 die("standard output, or directory, which one?");
250 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
251 die("Could not create directory %s",
255 if (rev
.pending_objects
&& rev
.pending_objects
->next
== NULL
) {
256 rev
.pending_objects
->item
->flags
|= UNINTERESTING
;
261 realstdout
= fdopen(dup(1), "w");
263 prepare_revision_walk(&rev
);
264 while ((commit
= get_revision(&rev
)) != NULL
) {
266 if (commit
->parents
&& commit
->parents
->next
)
269 list
= realloc(list
, nr
* sizeof(list
[0]));
270 list
[nr
- 1] = commit
;
274 rev
.total
= total
+ start_number
- 1;
275 rev
.add_signoff
= add_signoff
;
279 rev
.nr
= total
- nr
+ (start_number
- 1);
281 reopen_stdout(commit
, rev
.nr
, keep_subject
);
282 shown
= log_tree_commit(&rev
, commit
);
283 free(commit
->buffer
);
284 commit
->buffer
= NULL
;
286 /* We put one extra blank line between formatted
287 * patches and this flag is used by log-tree code
288 * to see if it needs to emit a LF before showing
289 * the log; when using one file per patch, we do
290 * not want the extra blank line.
295 if (rev
.mime_boundary
)
296 printf("\n--%s%s--\n\n\n",
297 mime_boundary_leader
,
300 printf("-- \n%s\n\n", git_version_string
);