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 return cmd_log_wc(argc
, argv
, envp
, &rev
);
57 int cmd_show(int argc
, const char **argv
, char **envp
)
63 rev
.diffopt
.recursive
= 1;
64 rev
.combine_merges
= 1;
65 rev
.dense_combined_merges
= 1;
66 rev
.always_show_header
= 1;
67 rev
.ignore_merges
= 0;
69 return cmd_log_wc(argc
, argv
, envp
, &rev
);
72 int cmd_log(int argc
, const char **argv
, char **envp
)
77 rev
.always_show_header
= 1;
78 rev
.diffopt
.recursive
= 1;
79 return cmd_log_wc(argc
, argv
, envp
, &rev
);
82 static int istitlechar(char c
)
84 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
85 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
88 static char *extra_headers
= NULL
;
89 static int extra_headers_size
= 0;
91 static int git_format_config(const char *var
, const char *value
)
93 if (!strcmp(var
, "format.headers")) {
94 int len
= strlen(value
);
95 extra_headers_size
+= len
+ 1;
96 extra_headers
= realloc(extra_headers
, extra_headers_size
);
97 extra_headers
[extra_headers_size
- len
- 1] = 0;
98 strcat(extra_headers
, value
);
101 return git_default_config(var
, value
);
105 static FILE *realstdout
= NULL
;
106 static const char *output_directory
= NULL
;
108 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
114 if (output_directory
) {
115 strncpy(filename
, output_directory
, 1010);
116 len
= strlen(filename
);
117 if (filename
[len
- 1] != '/')
118 filename
[len
++] = '/';
121 sprintf(filename
+ len
, "%04d", nr
);
122 len
= strlen(filename
);
124 sol
= strstr(commit
->buffer
, "\n\n");
129 /* strip [PATCH] or [PATCH blabla] */
130 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
131 char *eos
= strchr(sol
+ 6, ']');
133 while (isspace(*eos
))
139 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
140 if (istitlechar(sol
[j
])) {
142 filename
[len
++] = '-';
145 filename
[len
++] = sol
[j
];
147 while (sol
[j
+ 1] == '.')
152 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
155 strcpy(filename
+ len
, ".txt");
156 fprintf(realstdout
, "%s\n", filename
);
157 freopen(filename
, "w", stdout
);
160 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
162 struct commit
*commit
;
163 struct commit
**list
= NULL
;
165 int nr
= 0, total
, i
, j
;
168 int start_number
= -1;
169 int keep_subject
= 0;
170 char *add_signoff
= NULL
;
172 init_revisions(&rev
);
173 rev
.commit_format
= CMIT_FMT_EMAIL
;
174 rev
.verbose_header
= 1;
176 rev
.diffopt
.with_raw
= 0;
177 rev
.diffopt
.with_stat
= 1;
178 rev
.combine_merges
= 0;
179 rev
.ignore_merges
= 1;
180 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
182 git_config(git_format_config
);
183 rev
.extra_headers
= extra_headers
;
186 * Parse the arguments before setup_revisions(), or something
187 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
188 * possibly a valid SHA1.
190 for (i
= 1, j
= 1; i
< argc
; i
++) {
191 if (!strcmp(argv
[i
], "--stdout"))
193 else if (!strcmp(argv
[i
], "-n") ||
194 !strcmp(argv
[i
], "--numbered"))
196 else if (!strncmp(argv
[i
], "--start-number=", 15))
197 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
198 else if (!strcmp(argv
[i
], "--start-number")) {
201 die("Need a number for --start-number");
202 start_number
= strtol(argv
[i
], NULL
, 10);
204 else if (!strcmp(argv
[i
], "-k") ||
205 !strcmp(argv
[i
], "--keep-subject")) {
209 else if (!strcmp(argv
[i
], "--output-directory") ||
210 !strcmp(argv
[i
], "-o")) {
213 die("Which directory?");
214 if (output_directory
)
215 die("Two output directories?");
216 output_directory
= argv
[i
];
218 else if (!strcmp(argv
[i
], "--signoff") ||
219 !strcmp(argv
[i
], "-s")) {
220 const char *committer
= git_committer_info(1);
221 const char *endpos
= strchr(committer
, '>');
223 die("bogos committer info %s\n", committer
);
224 add_signoff
= xmalloc(endpos
- committer
+ 2);
225 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
226 add_signoff
[endpos
- committer
+ 1] = 0;
228 else if (!strcmp(argv
[i
], "--attach"))
229 rev
.mime_boundary
= git_version_string
;
230 else if (!strncmp(argv
[i
], "--attach=", 9))
231 rev
.mime_boundary
= argv
[i
] + 9;
237 if (start_number
< 0)
239 if (numbered
&& keep_subject
)
240 die ("-n and -k are mutually exclusive.");
242 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
244 die ("unrecognized argument: %s", argv
[1]);
246 if (output_directory
) {
248 die("standard output, or directory, which one?");
249 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
250 die("Could not create directory %s",
254 if (rev
.pending_objects
&& rev
.pending_objects
->next
== NULL
) {
255 rev
.pending_objects
->item
->flags
|= UNINTERESTING
;
260 realstdout
= fdopen(dup(1), "w");
262 prepare_revision_walk(&rev
);
263 while ((commit
= get_revision(&rev
)) != NULL
) {
265 if (commit
->parents
&& commit
->parents
->next
)
268 list
= realloc(list
, nr
* sizeof(list
[0]));
269 list
[nr
- 1] = commit
;
273 rev
.total
= total
+ start_number
- 1;
274 rev
.add_signoff
= add_signoff
;
278 rev
.nr
= total
- nr
+ (start_number
- 1);
280 reopen_stdout(commit
, rev
.nr
, keep_subject
);
281 shown
= log_tree_commit(&rev
, commit
);
282 free(commit
->buffer
);
283 commit
->buffer
= NULL
;
285 /* We put one extra blank line between formatted
286 * patches and this flag is used by log-tree code
287 * to see if it needs to emit a LF before showing
288 * the log; when using one file per patch, we do
289 * not want the extra blank line.
294 if (rev
.mime_boundary
)
295 printf("\n--%s%s--\n\n\n",
296 mime_boundary_leader
,
299 printf("-- \n%s\n\n", git_version_string
);