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 FILE *realstdout
= NULL
;
89 static char *output_directory
= NULL
;
91 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
97 if (output_directory
) {
98 strncpy(filename
, output_directory
, 1010);
99 len
= strlen(filename
);
100 if (filename
[len
- 1] != '/')
101 filename
[len
++] = '/';
104 sprintf(filename
+ len
, "%04d", nr
);
105 len
= strlen(filename
);
107 sol
= strstr(commit
->buffer
, "\n\n");
112 /* strip [PATCH] or [PATCH blabla] */
113 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
114 char *eos
= strchr(sol
+ 6, ']');
116 while (isspace(*eos
))
122 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
123 if (istitlechar(sol
[j
])) {
125 filename
[len
++] = '-';
128 filename
[len
++] = sol
[j
];
130 while (sol
[j
+ 1] == '.')
135 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
138 strcpy(filename
+ len
, ".txt");
139 fprintf(realstdout
, "%s\n", filename
);
140 freopen(filename
, "w", stdout
);
143 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
145 struct commit
*commit
;
146 struct commit
**list
= NULL
;
148 int nr
= 0, total
, i
, j
;
151 int start_number
= -1;
152 int keep_subject
= 0;
153 char *add_signoff
= NULL
;
155 init_revisions(&rev
);
156 rev
.commit_format
= CMIT_FMT_EMAIL
;
157 rev
.verbose_header
= 1;
159 rev
.diffopt
.with_raw
= 0;
160 rev
.diffopt
.with_stat
= 1;
161 rev
.combine_merges
= 0;
162 rev
.ignore_merges
= 1;
163 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
166 * Parse the arguments before setup_revisions(), or something
167 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
168 * possibly a valid SHA1.
170 for (i
= 1, j
= 1; i
< argc
; i
++) {
171 if (!strcmp(argv
[i
], "--stdout"))
173 else if (!strcmp(argv
[i
], "-n") ||
174 !strcmp(argv
[i
], "--numbered"))
176 else if (!strncmp(argv
[i
], "--start-number=", 15))
177 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
178 else if (!strcmp(argv
[i
], "--start-number")) {
181 die("Need a number for --start-number");
182 start_number
= strtol(argv
[i
], NULL
, 10);
184 else if (!strcmp(argv
[i
], "-k") ||
185 !strcmp(argv
[i
], "--keep-subject")) {
189 else if (!strcmp(argv
[i
], "-o")) {
191 die ("Which directory?");
192 if (mkdir(argv
[i
+ 1], 0777) < 0 && errno
!= EEXIST
)
193 die("Could not create directory %s",
195 output_directory
= strdup(argv
[i
+ 1]);
198 else if (!strcmp(argv
[i
], "--signoff") ||
199 !strcmp(argv
[i
], "-s")) {
200 const char *committer
= git_committer_info(1);
201 const char *endpos
= strchr(committer
, '>');
203 die("bogos committer info %s\n", committer
);
204 add_signoff
= xmalloc(endpos
- committer
+ 2);
205 memcpy(add_signoff
, committer
, endpos
- committer
+ 1);
206 add_signoff
[endpos
- committer
+ 1] = 0;
208 else if (!strcmp(argv
[i
], "--attach"))
209 rev
.mime_boundary
= git_version_string
;
210 else if (!strncmp(argv
[i
], "--attach=", 9))
211 rev
.mime_boundary
= argv
[i
] + 9;
217 if (start_number
< 0)
219 if (numbered
&& keep_subject
)
220 die ("-n and -k are mutually exclusive.");
222 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
224 die ("unrecognized argument: %s", argv
[1]);
226 if (rev
.pending_objects
&& rev
.pending_objects
->next
== NULL
) {
227 rev
.pending_objects
->item
->flags
|= UNINTERESTING
;
232 realstdout
= fdopen(dup(1), "w");
234 prepare_revision_walk(&rev
);
235 while ((commit
= get_revision(&rev
)) != NULL
) {
237 if (commit
->parents
&& commit
->parents
->next
)
240 list
= realloc(list
, nr
* sizeof(list
[0]));
241 list
[nr
- 1] = commit
;
245 rev
.total
= total
+ start_number
- 1;
246 rev
.add_signoff
= add_signoff
;
250 rev
.nr
= total
- nr
+ (start_number
- 1);
252 reopen_stdout(commit
, rev
.nr
, keep_subject
);
253 shown
= log_tree_commit(&rev
, commit
);
254 free(commit
->buffer
);
255 commit
->buffer
= NULL
;
257 /* We put one extra blank line between formatted
258 * patches and this flag is used by log-tree code
259 * to see if it needs to emit a LF before showing
260 * the log; when using one file per patch, we do
261 * not want the extra blank line.
266 if (rev
.mime_boundary
)
267 printf("\n--%s%s--\n\n\n",
268 mime_boundary_leader
,
271 printf("-- \n%s\n\n", git_version_string
);
276 if (output_directory
)
277 free(output_directory
);