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
)
52 rev
.diffopt
.recursive
= 1;
53 rev
.simplify_history
= 0;
54 cmd_log_init(argc
, argv
, envp
, &rev
);
55 if (!rev
.diffopt
.output_format
)
56 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
57 return cmd_log_walk(&rev
);
60 int cmd_show(int argc
, const char **argv
, char **envp
)
66 rev
.diffopt
.recursive
= 1;
67 rev
.combine_merges
= 1;
68 rev
.dense_combined_merges
= 1;
69 rev
.always_show_header
= 1;
70 rev
.ignore_merges
= 0;
72 cmd_log_init(argc
, argv
, envp
, &rev
);
73 return cmd_log_walk(&rev
);
76 int cmd_log(int argc
, const char **argv
, char **envp
)
81 rev
.always_show_header
= 1;
82 cmd_log_init(argc
, argv
, envp
, &rev
);
83 return cmd_log_walk(&rev
);
86 static int istitlechar(char c
)
88 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
89 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
92 static char *extra_headers
= NULL
;
93 static int extra_headers_size
= 0;
95 static int git_format_config(const char *var
, const char *value
)
97 if (!strcmp(var
, "format.headers")) {
98 int len
= strlen(value
);
99 extra_headers_size
+= len
+ 1;
100 extra_headers
= realloc(extra_headers
, extra_headers_size
);
101 extra_headers
[extra_headers_size
- len
- 1] = 0;
102 strcat(extra_headers
, value
);
105 return git_default_config(var
, value
);
109 static FILE *realstdout
= NULL
;
110 static const char *output_directory
= NULL
;
112 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
118 if (output_directory
) {
119 strlcpy(filename
, output_directory
, 1010);
120 len
= strlen(filename
);
121 if (filename
[len
- 1] != '/')
122 filename
[len
++] = '/';
125 sprintf(filename
+ len
, "%04d", nr
);
126 len
= strlen(filename
);
128 sol
= strstr(commit
->buffer
, "\n\n");
133 /* strip [PATCH] or [PATCH blabla] */
134 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
135 char *eos
= strchr(sol
+ 6, ']');
137 while (isspace(*eos
))
143 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
144 if (istitlechar(sol
[j
])) {
146 filename
[len
++] = '-';
149 filename
[len
++] = sol
[j
];
151 while (sol
[j
+ 1] == '.')
156 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
159 strcpy(filename
+ len
, ".txt");
160 fprintf(realstdout
, "%s\n", filename
);
161 freopen(filename
, "w", stdout
);
164 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
166 struct commit
*commit
;
167 struct commit
**list
= NULL
;
169 int nr
= 0, total
, i
, j
;
172 int start_number
= -1;
173 int keep_subject
= 0;
174 char *add_signoff
= NULL
;
176 init_revisions(&rev
);
177 rev
.commit_format
= CMIT_FMT_EMAIL
;
178 rev
.verbose_header
= 1;
180 rev
.combine_merges
= 0;
181 rev
.ignore_merges
= 1;
182 rev
.diffopt
.msg_sep
= "";
183 rev
.diffopt
.recursive
= 1;
185 git_config(git_format_config
);
186 rev
.extra_headers
= extra_headers
;
189 * Parse the arguments before setup_revisions(), or something
190 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
191 * possibly a valid SHA1.
193 for (i
= 1, j
= 1; i
< argc
; i
++) {
194 if (!strcmp(argv
[i
], "--stdout"))
196 else if (!strcmp(argv
[i
], "-n") ||
197 !strcmp(argv
[i
], "--numbered"))
199 else if (!strncmp(argv
[i
], "--start-number=", 15))
200 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
201 else if (!strcmp(argv
[i
], "--start-number")) {
204 die("Need a number for --start-number");
205 start_number
= strtol(argv
[i
], NULL
, 10);
207 else if (!strcmp(argv
[i
], "-k") ||
208 !strcmp(argv
[i
], "--keep-subject")) {
212 else if (!strcmp(argv
[i
], "--output-directory") ||
213 !strcmp(argv
[i
], "-o")) {
216 die("Which directory?");
217 if (output_directory
)
218 die("Two output directories?");
219 output_directory
= argv
[i
];
221 else if (!strcmp(argv
[i
], "--signoff") ||
222 !strcmp(argv
[i
], "-s")) {
223 const char *committer
;
226 committer
= git_committer_info(1);
227 endpos
= strchr(committer
, '>');
229 die("bogos committer info %s\n", committer
);
230 add_signoff
= xmalloc(endpos
- committer
+ 2);
231 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
232 add_signoff
[endpos
- committer
+ 1] = 0;
234 else if (!strcmp(argv
[i
], "--attach"))
235 rev
.mime_boundary
= git_version_string
;
236 else if (!strncmp(argv
[i
], "--attach=", 9))
237 rev
.mime_boundary
= argv
[i
] + 9;
243 if (start_number
< 0)
245 if (numbered
&& keep_subject
)
246 die ("-n and -k are mutually exclusive.");
248 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
250 die ("unrecognized argument: %s", argv
[1]);
252 if (!rev
.diffopt
.output_format
)
253 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
255 if (output_directory
) {
257 die("standard output, or directory, which one?");
258 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
259 die("Could not create directory %s",
263 if (rev
.pending
.nr
== 1) {
264 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
269 realstdout
= fdopen(dup(1), "w");
271 prepare_revision_walk(&rev
);
272 while ((commit
= get_revision(&rev
)) != NULL
) {
274 if (commit
->parents
&& commit
->parents
->next
)
277 list
= realloc(list
, nr
* sizeof(list
[0]));
278 list
[nr
- 1] = commit
;
282 rev
.total
= total
+ start_number
- 1;
283 rev
.add_signoff
= add_signoff
;
287 rev
.nr
= total
- nr
+ (start_number
- 1);
289 reopen_stdout(commit
, rev
.nr
, keep_subject
);
290 shown
= log_tree_commit(&rev
, commit
);
291 free(commit
->buffer
);
292 commit
->buffer
= NULL
;
294 /* We put one extra blank line between formatted
295 * patches and this flag is used by log-tree code
296 * to see if it needs to emit a LF before showing
297 * the log; when using one file per patch, we do
298 * not want the extra blank line.
303 if (rev
.mime_boundary
)
304 printf("\n--%s%s--\n\n\n",
305 mime_boundary_leader
,
308 printf("-- \n%s\n\n", git_version_string
);