]>
Commit | Line | Data |
---|---|---|
f5bbc322 KH |
1 | /* |
2 | * Builtin "git commit" | |
3 | * | |
4 | * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com> | |
5 | * Based on git-commit.sh by Junio C Hamano and Linus Torvalds | |
6 | */ | |
7 | ||
8 | #include "cache.h" | |
9 | #include "cache-tree.h" | |
6b2f2d98 | 10 | #include "color.h" |
2888605c | 11 | #include "dir.h" |
f5bbc322 KH |
12 | #include "builtin.h" |
13 | #include "diff.h" | |
14 | #include "diffcore.h" | |
15 | #include "commit.h" | |
16 | #include "revision.h" | |
17 | #include "wt-status.h" | |
18 | #include "run-command.h" | |
19 | #include "refs.h" | |
20 | #include "log-tree.h" | |
21 | #include "strbuf.h" | |
22 | #include "utf8.h" | |
23 | #include "parse-options.h" | |
c455c87c | 24 | #include "string-list.h" |
5b2fd956 | 25 | #include "rerere.h" |
fa9dcf80 | 26 | #include "unpack-trees.h" |
76e2f7ce | 27 | #include "quote.h" |
f5bbc322 KH |
28 | |
29 | static const char * const builtin_commit_usage[] = { | |
1b1dd23f | 30 | "git commit [options] [--] <filepattern>...", |
f5bbc322 KH |
31 | NULL |
32 | }; | |
33 | ||
2f02b25f | 34 | static const char * const builtin_status_usage[] = { |
1b1dd23f | 35 | "git status [options] [--] <filepattern>...", |
2f02b25f SB |
36 | NULL |
37 | }; | |
38 | ||
30988301 | 39 | static unsigned char head_sha1[20]; |
f5bbc322 KH |
40 | static char *use_message_buffer; |
41 | static const char commit_editmsg[] = "COMMIT_EDITMSG"; | |
2888605c JH |
42 | static struct lock_file index_lock; /* real index */ |
43 | static struct lock_file false_lock; /* used only for partial commits */ | |
44 | static enum { | |
45 | COMMIT_AS_IS = 1, | |
46 | COMMIT_NORMAL, | |
47 | COMMIT_PARTIAL, | |
48 | } commit_style; | |
f5bbc322 | 49 | |
dbd0f5c7 | 50 | static const char *logfile, *force_author; |
984c6e7e | 51 | static const char *template_file; |
f5bbc322 | 52 | static char *edit_message, *use_message; |
e83dbe80 | 53 | static char *author_name, *author_email, *author_date; |
f5bbc322 | 54 | static int all, edit_flag, also, interactive, only, amend, signoff; |
c51f6cee | 55 | static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; |
02b47cd7 | 56 | static char *untracked_files_arg, *force_date; |
5f065737 AR |
57 | /* |
58 | * The default commit message cleanup mode will remove the lines | |
59 | * beginning with # (shell comments) and leading and trailing | |
60 | * whitespaces (empty lines or containing only whitespaces) | |
61 | * if editor is used, and only the whitespaces if the message | |
62 | * is specified explicitly. | |
63 | */ | |
64 | static enum { | |
65 | CLEANUP_SPACE, | |
66 | CLEANUP_NONE, | |
67 | CLEANUP_ALL, | |
68 | } cleanup_mode; | |
69 | static char *cleanup_arg; | |
f5bbc322 | 70 | |
4803466f | 71 | static int use_editor = 1, initial_commit, in_merge; |
3b6aeb3c JS |
72 | static const char *only_include_assumed; |
73 | static struct strbuf message; | |
f9568530 | 74 | |
7c9f7038 JK |
75 | static int null_termination; |
76 | static enum { | |
77 | STATUS_FORMAT_LONG, | |
78 | STATUS_FORMAT_SHORT, | |
79 | STATUS_FORMAT_PORCELAIN, | |
80 | } status_format = STATUS_FORMAT_LONG; | |
81 | ||
f9568530 JS |
82 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) |
83 | { | |
84 | struct strbuf *buf = opt->value; | |
85 | if (unset) | |
86 | strbuf_setlen(buf, 0); | |
87 | else { | |
88 | strbuf_addstr(buf, arg); | |
3b6aeb3c | 89 | strbuf_addstr(buf, "\n\n"); |
f9568530 JS |
90 | } |
91 | return 0; | |
92 | } | |
f5bbc322 KH |
93 | |
94 | static struct option builtin_commit_options[] = { | |
95 | OPT__QUIET(&quiet), | |
96 | OPT__VERBOSE(&verbose), | |
f5bbc322 | 97 | |
e97ca7f4 | 98 | OPT_GROUP("Commit message options"), |
df217ed6 | 99 | OPT_FILENAME('F', "file", &logfile, "read log from file"), |
f5bbc322 | 100 | OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"), |
02b47cd7 | 101 | OPT_STRING(0, "date", &force_date, "DATE", "override date for commit"), |
f9568530 | 102 | OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m), |
c51f6cee | 103 | OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"), |
f5bbc322 | 104 | OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"), |
c51f6cee | 105 | OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"), |
362b0dd5 | 106 | OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), |
df217ed6 | 107 | OPT_FILENAME('t', "template", &template_file, "use specified template file"), |
f5bbc322 | 108 | OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"), |
e97ca7f4 GP |
109 | OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"), |
110 | /* end commit message options */ | |
f5bbc322 KH |
111 | |
112 | OPT_GROUP("Commit contents options"), | |
113 | OPT_BOOLEAN('a', "all", &all, "commit all changed files"), | |
114 | OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"), | |
115 | OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"), | |
d4ba07ca | 116 | OPT_BOOLEAN('o', "only", &only, "commit only specified files"), |
f5bbc322 | 117 | OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"), |
3a5d13a3 | 118 | OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"), |
7c9f7038 JK |
119 | OPT_SET_INT(0, "short", &status_format, "show status concisely", |
120 | STATUS_FORMAT_SHORT), | |
121 | OPT_SET_INT(0, "porcelain", &status_format, | |
122 | "show porcelain output format", STATUS_FORMAT_PORCELAIN), | |
123 | OPT_BOOLEAN('z', "null", &null_termination, | |
124 | "terminate entries with NUL"), | |
f5bbc322 | 125 | OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"), |
6c2ce048 | 126 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
5241b6bf | 127 | OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"), |
e97ca7f4 | 128 | /* end commit contents options */ |
f5bbc322 KH |
129 | |
130 | OPT_END() | |
131 | }; | |
132 | ||
2888605c JH |
133 | static void rollback_index_files(void) |
134 | { | |
135 | switch (commit_style) { | |
136 | case COMMIT_AS_IS: | |
137 | break; /* nothing to do */ | |
138 | case COMMIT_NORMAL: | |
139 | rollback_lock_file(&index_lock); | |
140 | break; | |
141 | case COMMIT_PARTIAL: | |
142 | rollback_lock_file(&index_lock); | |
143 | rollback_lock_file(&false_lock); | |
144 | break; | |
145 | } | |
146 | } | |
147 | ||
5a9dd399 | 148 | static int commit_index_files(void) |
2888605c | 149 | { |
5a9dd399 BC |
150 | int err = 0; |
151 | ||
2888605c JH |
152 | switch (commit_style) { |
153 | case COMMIT_AS_IS: | |
154 | break; /* nothing to do */ | |
155 | case COMMIT_NORMAL: | |
5a9dd399 | 156 | err = commit_lock_file(&index_lock); |
2888605c JH |
157 | break; |
158 | case COMMIT_PARTIAL: | |
5a9dd399 | 159 | err = commit_lock_file(&index_lock); |
2888605c JH |
160 | rollback_lock_file(&false_lock); |
161 | break; | |
162 | } | |
5a9dd399 BC |
163 | |
164 | return err; | |
2888605c JH |
165 | } |
166 | ||
167 | /* | |
168 | * Take a union of paths in the index and the named tree (typically, "HEAD"), | |
169 | * and return the paths that match the given pattern in list. | |
170 | */ | |
c455c87c | 171 | static int list_paths(struct string_list *list, const char *with_tree, |
2888605c JH |
172 | const char *prefix, const char **pattern) |
173 | { | |
174 | int i; | |
175 | char *m; | |
176 | ||
177 | for (i = 0; pattern[i]; i++) | |
178 | ; | |
179 | m = xcalloc(1, i); | |
180 | ||
181 | if (with_tree) | |
182 | overlay_tree_on_cache(with_tree, prefix); | |
183 | ||
184 | for (i = 0; i < active_nr; i++) { | |
185 | struct cache_entry *ce = active_cache[i]; | |
7a51ed66 | 186 | if (ce->ce_flags & CE_UPDATE) |
e87e22d0 | 187 | continue; |
0b50922a | 188 | if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m)) |
2888605c | 189 | continue; |
c455c87c | 190 | string_list_insert(ce->name, list); |
2888605c JH |
191 | } |
192 | ||
193 | return report_path_error(m, pattern, prefix ? strlen(prefix) : 0); | |
194 | } | |
195 | ||
c455c87c | 196 | static void add_remove_files(struct string_list *list) |
2888605c JH |
197 | { |
198 | int i; | |
199 | for (i = 0; i < list->nr; i++) { | |
d177cab0 | 200 | struct stat st; |
c455c87c | 201 | struct string_list_item *p = &(list->items[i]); |
d177cab0 | 202 | |
c455c87c JS |
203 | if (!lstat(p->string, &st)) { |
204 | if (add_to_cache(p->string, &st, 0)) | |
960b8ad1 AR |
205 | die("updating files failed"); |
206 | } else | |
c455c87c | 207 | remove_file_from_cache(p->string); |
2888605c JH |
208 | } |
209 | } | |
210 | ||
fa9dcf80 LT |
211 | static void create_base_index(void) |
212 | { | |
213 | struct tree *tree; | |
214 | struct unpack_trees_options opts; | |
215 | struct tree_desc t; | |
216 | ||
217 | if (initial_commit) { | |
218 | discard_cache(); | |
219 | return; | |
220 | } | |
221 | ||
222 | memset(&opts, 0, sizeof(opts)); | |
223 | opts.head_idx = 1; | |
224 | opts.index_only = 1; | |
225 | opts.merge = 1; | |
34110cd4 LT |
226 | opts.src_index = &the_index; |
227 | opts.dst_index = &the_index; | |
fa9dcf80 LT |
228 | |
229 | opts.fn = oneway_merge; | |
230 | tree = parse_tree_indirect(head_sha1); | |
231 | if (!tree) | |
232 | die("failed to unpack HEAD tree object"); | |
233 | parse_tree(tree); | |
234 | init_tree_desc(&t, tree->buffer, tree->size); | |
203a2fe1 DB |
235 | if (unpack_trees(1, &t, &opts)) |
236 | exit(128); /* We've already reported the error, finish dying */ | |
fa9dcf80 LT |
237 | } |
238 | ||
50b7e70f | 239 | static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status) |
f5bbc322 KH |
240 | { |
241 | int fd; | |
c455c87c | 242 | struct string_list partial; |
2888605c | 243 | const char **pathspec = NULL; |
50b7e70f | 244 | int refresh_flags = REFRESH_QUIET; |
f5bbc322 | 245 | |
50b7e70f JH |
246 | if (is_status) |
247 | refresh_flags |= REFRESH_UNMERGED; | |
f5bbc322 | 248 | if (interactive) { |
4f6a32f8 JK |
249 | if (interactive_add(argc, argv, prefix) != 0) |
250 | die("interactive add failed"); | |
671c9b7e | 251 | if (read_cache_preload(NULL) < 0) |
d5350fd2 | 252 | die("index file corrupt"); |
2888605c | 253 | commit_style = COMMIT_AS_IS; |
f5bbc322 KH |
254 | return get_index_file(); |
255 | } | |
256 | ||
f64fe7b4 JH |
257 | if (*argv) |
258 | pathspec = get_pathspec(prefix, argv); | |
2888605c | 259 | |
671c9b7e LT |
260 | if (read_cache_preload(pathspec) < 0) |
261 | die("index file corrupt"); | |
262 | ||
2888605c JH |
263 | /* |
264 | * Non partial, non as-is commit. | |
265 | * | |
266 | * (1) get the real index; | |
267 | * (2) update the_index as necessary; | |
268 | * (3) write the_index out to the real index (still locked); | |
269 | * (4) return the name of the locked index file. | |
270 | * | |
271 | * The caller should run hooks on the locked real index, and | |
272 | * (A) if all goes well, commit the real index; | |
273 | * (B) on failure, rollback the real index. | |
274 | */ | |
275 | if (all || (also && pathspec && *pathspec)) { | |
276 | int fd = hold_locked_index(&index_lock, 1); | |
7ae02a30 | 277 | add_files_to_cache(also ? prefix : NULL, pathspec, 0); |
50b7e70f | 278 | refresh_cache(refresh_flags); |
4ed7cd3a BC |
279 | if (write_cache(fd, active_cache, active_nr) || |
280 | close_lock_file(&index_lock)) | |
f5bbc322 | 281 | die("unable to write new_index file"); |
2888605c JH |
282 | commit_style = COMMIT_NORMAL; |
283 | return index_lock.filename; | |
f5bbc322 KH |
284 | } |
285 | ||
2888605c JH |
286 | /* |
287 | * As-is commit. | |
288 | * | |
289 | * (1) return the name of the real index file. | |
290 | * | |
291 | * The caller should run hooks on the real index, and run | |
292 | * hooks on the real index, and create commit from the_index. | |
293 | * We still need to refresh the index here. | |
294 | */ | |
295 | if (!pathspec || !*pathspec) { | |
296 | fd = hold_locked_index(&index_lock, 1); | |
50b7e70f | 297 | refresh_cache(refresh_flags); |
2888605c | 298 | if (write_cache(fd, active_cache, active_nr) || |
4439751d | 299 | commit_locked_index(&index_lock)) |
2888605c JH |
300 | die("unable to write new_index file"); |
301 | commit_style = COMMIT_AS_IS; | |
f5bbc322 KH |
302 | return get_index_file(); |
303 | } | |
304 | ||
2888605c JH |
305 | /* |
306 | * A partial commit. | |
307 | * | |
308 | * (0) find the set of affected paths; | |
309 | * (1) get lock on the real index file; | |
310 | * (2) update the_index with the given paths; | |
311 | * (3) write the_index out to the real index (still locked); | |
312 | * (4) get lock on the false index file; | |
313 | * (5) reset the_index from HEAD; | |
314 | * (6) update the_index the same way as (2); | |
315 | * (7) write the_index out to the false index file; | |
316 | * (8) return the name of the false index file (still locked); | |
317 | * | |
318 | * The caller should run hooks on the locked false index, and | |
319 | * create commit from it. Then | |
320 | * (A) if all goes well, commit the real index; | |
321 | * (B) on failure, rollback the real index; | |
322 | * In either case, rollback the false index. | |
323 | */ | |
324 | commit_style = COMMIT_PARTIAL; | |
325 | ||
30988301 | 326 | if (in_merge) |
2888605c JH |
327 | die("cannot do a partial commit during a merge."); |
328 | ||
329 | memset(&partial, 0, sizeof(partial)); | |
c455c87c | 330 | partial.strdup_strings = 1; |
2888605c JH |
331 | if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec)) |
332 | exit(1); | |
333 | ||
334 | discard_cache(); | |
335 | if (read_cache() < 0) | |
336 | die("cannot read the index"); | |
337 | ||
338 | fd = hold_locked_index(&index_lock, 1); | |
339 | add_remove_files(&partial); | |
ef12b50d | 340 | refresh_cache(REFRESH_QUIET); |
4ed7cd3a BC |
341 | if (write_cache(fd, active_cache, active_nr) || |
342 | close_lock_file(&index_lock)) | |
f5bbc322 KH |
343 | die("unable to write new_index file"); |
344 | ||
2888605c | 345 | fd = hold_lock_file_for_update(&false_lock, |
a157400c JH |
346 | git_path("next-index-%"PRIuMAX, |
347 | (uintmax_t) getpid()), | |
acd3b9ec | 348 | LOCK_DIE_ON_ERROR); |
fa9dcf80 LT |
349 | |
350 | create_base_index(); | |
2888605c | 351 | add_remove_files(&partial); |
d37d3203 | 352 | refresh_cache(REFRESH_QUIET); |
f5bbc322 | 353 | |
4ed7cd3a BC |
354 | if (write_cache(fd, active_cache, active_nr) || |
355 | close_lock_file(&false_lock)) | |
2888605c | 356 | die("unable to write temporary index file"); |
959ba670 JK |
357 | |
358 | discard_cache(); | |
359 | read_cache_from(false_lock.filename); | |
360 | ||
2888605c | 361 | return false_lock.filename; |
f5bbc322 KH |
362 | } |
363 | ||
d249b098 JH |
364 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, |
365 | struct wt_status *s) | |
f5bbc322 | 366 | { |
76e2f7ce JH |
367 | unsigned char sha1[20]; |
368 | ||
d249b098 JH |
369 | if (s->relative_paths) |
370 | s->prefix = prefix; | |
f5bbc322 KH |
371 | |
372 | if (amend) { | |
d249b098 JH |
373 | s->amend = 1; |
374 | s->reference = "HEAD^1"; | |
f5bbc322 | 375 | } |
d249b098 JH |
376 | s->verbose = verbose; |
377 | s->index_file = index_file; | |
378 | s->fp = fp; | |
379 | s->nowarn = nowarn; | |
76e2f7ce | 380 | s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0; |
f5bbc322 | 381 | |
76e2f7ce | 382 | wt_status_collect(s); |
7c9f7038 JK |
383 | |
384 | switch (status_format) { | |
385 | case STATUS_FORMAT_SHORT: | |
84dbe7b8 | 386 | wt_shortstatus_print(s, null_termination); |
7c9f7038 JK |
387 | break; |
388 | case STATUS_FORMAT_PORCELAIN: | |
4a7cc2fd | 389 | wt_porcelain_print(s, null_termination); |
7c9f7038 JK |
390 | break; |
391 | case STATUS_FORMAT_LONG: | |
392 | wt_status_print(s); | |
393 | break; | |
394 | } | |
f5bbc322 | 395 | |
d249b098 | 396 | return s->commitable; |
f5bbc322 KH |
397 | } |
398 | ||
ec84bd00 PB |
399 | static int is_a_merge(const unsigned char *sha1) |
400 | { | |
401 | struct commit *commit = lookup_commit(sha1); | |
402 | if (!commit || parse_commit(commit)) | |
403 | die("could not parse HEAD commit"); | |
404 | return !!(commit->parents && commit->parents->next); | |
405 | } | |
406 | ||
f5bbc322 KH |
407 | static const char sign_off_header[] = "Signed-off-by: "; |
408 | ||
e83dbe80 | 409 | static void determine_author_info(void) |
a45d46ba SB |
410 | { |
411 | char *name, *email, *date; | |
412 | ||
413 | name = getenv("GIT_AUTHOR_NAME"); | |
414 | email = getenv("GIT_AUTHOR_EMAIL"); | |
415 | date = getenv("GIT_AUTHOR_DATE"); | |
416 | ||
c51f6cee | 417 | if (use_message && !renew_authorship) { |
a45d46ba SB |
418 | const char *a, *lb, *rb, *eol; |
419 | ||
420 | a = strstr(use_message_buffer, "\nauthor "); | |
421 | if (!a) | |
422 | die("invalid commit: %s", use_message); | |
423 | ||
424 | lb = strstr(a + 8, " <"); | |
425 | rb = strstr(a + 8, "> "); | |
426 | eol = strchr(a + 8, '\n'); | |
427 | if (!lb || !rb || !eol) | |
428 | die("invalid commit: %s", use_message); | |
429 | ||
430 | name = xstrndup(a + 8, lb - (a + 8)); | |
431 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
432 | date = xstrndup(rb + 2, eol - (rb + 2)); | |
433 | } | |
434 | ||
435 | if (force_author) { | |
436 | const char *lb = strstr(force_author, " <"); | |
437 | const char *rb = strchr(force_author, '>'); | |
438 | ||
439 | if (!lb || !rb) | |
440 | die("malformed --author parameter"); | |
441 | name = xstrndup(force_author, lb - force_author); | |
442 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
443 | } | |
444 | ||
02b47cd7 MV |
445 | if (force_date) |
446 | date = force_date; | |
447 | ||
e83dbe80 SB |
448 | author_name = name; |
449 | author_email = email; | |
450 | author_date = date; | |
a45d46ba SB |
451 | } |
452 | ||
c1e01b0c DB |
453 | static int ends_rfc2822_footer(struct strbuf *sb) |
454 | { | |
455 | int ch; | |
456 | int hit = 0; | |
457 | int i, j, k; | |
458 | int len = sb->len; | |
459 | int first = 1; | |
460 | const char *buf = sb->buf; | |
461 | ||
462 | for (i = len - 1; i > 0; i--) { | |
463 | if (hit && buf[i] == '\n') | |
464 | break; | |
465 | hit = (buf[i] == '\n'); | |
466 | } | |
467 | ||
468 | while (i < len - 1 && buf[i] == '\n') | |
469 | i++; | |
470 | ||
471 | for (; i < len; i = k) { | |
472 | for (k = i; k < len && buf[k] != '\n'; k++) | |
473 | ; /* do nothing */ | |
474 | k++; | |
475 | ||
476 | if ((buf[k] == ' ' || buf[k] == '\t') && !first) | |
477 | continue; | |
478 | ||
479 | first = 0; | |
480 | ||
481 | for (j = 0; i + j < len; j++) { | |
482 | ch = buf[i + j]; | |
483 | if (ch == ':') | |
484 | break; | |
485 | if (isalnum(ch) || | |
486 | (ch == '-')) | |
487 | continue; | |
488 | return 0; | |
489 | } | |
490 | } | |
491 | return 1; | |
492 | } | |
493 | ||
d249b098 JH |
494 | static int prepare_to_commit(const char *index_file, const char *prefix, |
495 | struct wt_status *s) | |
f5bbc322 KH |
496 | { |
497 | struct stat statbuf; | |
bc5d248a | 498 | int commitable, saved_color_setting; |
f285a2d7 | 499 | struct strbuf sb = STRBUF_INIT; |
f5bbc322 KH |
500 | char *buffer; |
501 | FILE *fp; | |
8089c85b PB |
502 | const char *hook_arg1 = NULL; |
503 | const char *hook_arg2 = NULL; | |
bb1ae3f6 | 504 | int ident_shown = 0; |
f5bbc322 | 505 | |
ec84bd00 PB |
506 | if (!no_verify && run_hook(index_file, "pre-commit", NULL)) |
507 | return 0; | |
f5bbc322 | 508 | |
f9568530 JS |
509 | if (message.len) { |
510 | strbuf_addbuf(&sb, &message); | |
8089c85b | 511 | hook_arg1 = "message"; |
f5bbc322 KH |
512 | } else if (logfile && !strcmp(logfile, "-")) { |
513 | if (isatty(0)) | |
514 | fprintf(stderr, "(reading log message from standard input)\n"); | |
515 | if (strbuf_read(&sb, 0, 0) < 0) | |
0721c314 | 516 | die_errno("could not read log from standard input"); |
8089c85b | 517 | hook_arg1 = "message"; |
f5bbc322 KH |
518 | } else if (logfile) { |
519 | if (strbuf_read_file(&sb, logfile, 0) < 0) | |
d824cbba TR |
520 | die_errno("could not read log file '%s'", |
521 | logfile); | |
8089c85b | 522 | hook_arg1 = "message"; |
f5bbc322 KH |
523 | } else if (use_message) { |
524 | buffer = strstr(use_message_buffer, "\n\n"); | |
525 | if (!buffer || buffer[2] == '\0') | |
526 | die("commit has empty message"); | |
527 | strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); | |
8089c85b PB |
528 | hook_arg1 = "commit"; |
529 | hook_arg2 = use_message; | |
f5bbc322 KH |
530 | } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { |
531 | if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) | |
d824cbba | 532 | die_errno("could not read MERGE_MSG"); |
8089c85b | 533 | hook_arg1 = "merge"; |
f5bbc322 KH |
534 | } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { |
535 | if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) | |
d824cbba | 536 | die_errno("could not read SQUASH_MSG"); |
8089c85b | 537 | hook_arg1 = "squash"; |
f5bbc322 KH |
538 | } else if (template_file && !stat(template_file, &statbuf)) { |
539 | if (strbuf_read_file(&sb, template_file, 0) < 0) | |
d824cbba | 540 | die_errno("could not read '%s'", template_file); |
8089c85b | 541 | hook_arg1 = "template"; |
f5bbc322 KH |
542 | } |
543 | ||
8089c85b PB |
544 | /* |
545 | * This final case does not modify the template message, | |
546 | * it just sets the argument to the prepare-commit-msg hook. | |
547 | */ | |
548 | else if (in_merge) | |
549 | hook_arg1 = "merge"; | |
550 | ||
f5bbc322 KH |
551 | fp = fopen(git_path(commit_editmsg), "w"); |
552 | if (fp == NULL) | |
d824cbba | 553 | die_errno("could not open '%s'", git_path(commit_editmsg)); |
f5bbc322 | 554 | |
5f065737 AR |
555 | if (cleanup_mode != CLEANUP_NONE) |
556 | stripspace(&sb, 0); | |
f5bbc322 KH |
557 | |
558 | if (signoff) { | |
f285a2d7 | 559 | struct strbuf sob = STRBUF_INIT; |
13208572 JS |
560 | int i; |
561 | ||
13208572 | 562 | strbuf_addstr(&sob, sign_off_header); |
d9ccfe77 JH |
563 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
564 | getenv("GIT_COMMITTER_EMAIL"))); | |
13208572 | 565 | strbuf_addch(&sob, '\n'); |
13208572 JS |
566 | for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--) |
567 | ; /* do nothing */ | |
2150554b | 568 | if (prefixcmp(sb.buf + i, sob.buf)) { |
e5138436 | 569 | if (!i || !ends_rfc2822_footer(&sb)) |
2150554b | 570 | strbuf_addch(&sb, '\n'); |
13208572 | 571 | strbuf_addbuf(&sb, &sob); |
2150554b | 572 | } |
13208572 | 573 | strbuf_release(&sob); |
f5bbc322 KH |
574 | } |
575 | ||
13208572 | 576 | if (fwrite(sb.buf, 1, sb.len, fp) < sb.len) |
d824cbba | 577 | die_errno("could not write commit template"); |
13208572 | 578 | |
f5bbc322 KH |
579 | strbuf_release(&sb); |
580 | ||
e83dbe80 SB |
581 | determine_author_info(); |
582 | ||
bb1ae3f6 SB |
583 | /* This checks if committer ident is explicitly given */ |
584 | git_committer_info(0); | |
ec84bd00 | 585 | if (use_editor) { |
e83dbe80 SB |
586 | char *author_ident; |
587 | const char *committer_ident; | |
588 | ||
ec84bd00 PB |
589 | if (in_merge) |
590 | fprintf(fp, | |
591 | "#\n" | |
592 | "# It looks like you may be committing a MERGE.\n" | |
593 | "# If this is not correct, please remove the file\n" | |
594 | "# %s\n" | |
595 | "# and try again.\n" | |
596 | "#\n", | |
597 | git_path("MERGE_HEAD")); | |
598 | ||
599 | fprintf(fp, | |
600 | "\n" | |
fdc7c811 | 601 | "# Please enter the commit message for your changes."); |
ec84bd00 | 602 | if (cleanup_mode == CLEANUP_ALL) |
fdc7c811 JK |
603 | fprintf(fp, |
604 | " Lines starting\n" | |
605 | "# with '#' will be ignored, and an empty" | |
606 | " message aborts the commit.\n"); | |
ec84bd00 | 607 | else /* CLEANUP_SPACE, that is. */ |
fdc7c811 JK |
608 | fprintf(fp, |
609 | " Lines starting\n" | |
610 | "# with '#' will be kept; you may remove them" | |
611 | " yourself if you want to.\n" | |
612 | "# An empty message aborts the commit.\n"); | |
ec84bd00 PB |
613 | if (only_include_assumed) |
614 | fprintf(fp, "# %s\n", only_include_assumed); | |
615 | ||
e83dbe80 SB |
616 | author_ident = xstrdup(fmt_name(author_name, author_email)); |
617 | committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"), | |
618 | getenv("GIT_COMMITTER_EMAIL")); | |
619 | if (strcmp(author_ident, committer_ident)) | |
620 | fprintf(fp, | |
bb1ae3f6 SB |
621 | "%s" |
622 | "# Author: %s\n", | |
623 | ident_shown++ ? "" : "#\n", | |
e83dbe80 SB |
624 | author_ident); |
625 | free(author_ident); | |
626 | ||
91c38a21 | 627 | if (user_ident_explicitly_given != IDENT_ALL_GIVEN) |
bb1ae3f6 SB |
628 | fprintf(fp, |
629 | "%s" | |
630 | "# Committer: %s\n", | |
631 | ident_shown++ ? "" : "#\n", | |
632 | committer_ident); | |
633 | ||
634 | if (ident_shown) | |
635 | fprintf(fp, "#\n"); | |
636 | ||
d249b098 JH |
637 | saved_color_setting = s->use_color; |
638 | s->use_color = 0; | |
639 | commitable = run_status(fp, index_file, prefix, 1, s); | |
640 | s->use_color = saved_color_setting; | |
ec84bd00 | 641 | } else { |
d616a239 | 642 | unsigned char sha1[20]; |
fbcf1184 | 643 | const char *parent = "HEAD"; |
7168624c | 644 | |
7168624c AR |
645 | if (!active_nr && read_cache() < 0) |
646 | die("Cannot read index"); | |
647 | ||
fbcf1184 JH |
648 | if (amend) |
649 | parent = "HEAD^1"; | |
650 | ||
d616a239 | 651 | if (get_sha1(parent, sha1)) |
ec84bd00 | 652 | commitable = !!active_nr; |
75f3ff2e SB |
653 | else |
654 | commitable = index_differs_from(parent, 0); | |
ec84bd00 | 655 | } |
d616a239 | 656 | |
ec84bd00 | 657 | fclose(fp); |
7168624c | 658 | |
ec84bd00 PB |
659 | if (!commitable && !in_merge && !allow_empty && |
660 | !(amend && is_a_merge(head_sha1))) { | |
d249b098 | 661 | run_status(stdout, index_file, prefix, 0, s); |
ec84bd00 | 662 | return 0; |
7168624c AR |
663 | } |
664 | ||
ec84bd00 PB |
665 | /* |
666 | * Re-read the index as pre-commit hook could have updated it, | |
667 | * and write it out as a tree. We must do this before we invoke | |
668 | * the editor and after we invoke run_status above. | |
669 | */ | |
670 | discard_cache(); | |
671 | read_cache_from(index_file); | |
672 | if (!active_cache_tree) | |
673 | active_cache_tree = cache_tree(); | |
674 | if (cache_tree_update(active_cache_tree, | |
675 | active_cache, active_nr, 0, 0) < 0) { | |
331fcb59 | 676 | error("Error building trees"); |
ec84bd00 | 677 | return 0; |
7168624c | 678 | } |
f5bbc322 | 679 | |
8089c85b PB |
680 | if (run_hook(index_file, "prepare-commit-msg", |
681 | git_path(commit_editmsg), hook_arg1, hook_arg2, NULL)) | |
682 | return 0; | |
f5bbc322 | 683 | |
ec84bd00 PB |
684 | if (use_editor) { |
685 | char index[PATH_MAX]; | |
686 | const char *env[2] = { index, NULL }; | |
687 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); | |
7198203a SB |
688 | if (launch_editor(git_path(commit_editmsg), NULL, env)) { |
689 | fprintf(stderr, | |
690 | "Please supply the message using either -m or -F option.\n"); | |
691 | exit(1); | |
692 | } | |
ec84bd00 | 693 | } |
f5bbc322 | 694 | |
ec84bd00 PB |
695 | if (!no_verify && |
696 | run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) { | |
697 | return 0; | |
698 | } | |
f5bbc322 | 699 | |
ec84bd00 | 700 | return 1; |
f5bbc322 KH |
701 | } |
702 | ||
703 | /* | |
6bb6b034 MV |
704 | * Find out if the message in the strbuf contains only whitespace and |
705 | * Signed-off-by lines. | |
f5bbc322 | 706 | */ |
6bb6b034 | 707 | static int message_is_empty(struct strbuf *sb) |
f5bbc322 | 708 | { |
f285a2d7 | 709 | struct strbuf tmpl = STRBUF_INIT; |
f5bbc322 | 710 | const char *nl; |
6bb6b034 | 711 | int eol, i, start = 0; |
f5bbc322 | 712 | |
5f065737 AR |
713 | if (cleanup_mode == CLEANUP_NONE && sb->len) |
714 | return 0; | |
715 | ||
f5bbc322 | 716 | /* See if the template is just a prefix of the message. */ |
f5bbc322 | 717 | if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) { |
5f065737 | 718 | stripspace(&tmpl, cleanup_mode == CLEANUP_ALL); |
f5bbc322 KH |
719 | if (start + tmpl.len <= sb->len && |
720 | memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0) | |
721 | start += tmpl.len; | |
722 | } | |
723 | strbuf_release(&tmpl); | |
724 | ||
725 | /* Check if the rest is just whitespace and Signed-of-by's. */ | |
726 | for (i = start; i < sb->len; i++) { | |
727 | nl = memchr(sb->buf + i, '\n', sb->len - i); | |
728 | if (nl) | |
729 | eol = nl - sb->buf; | |
730 | else | |
731 | eol = sb->len; | |
732 | ||
733 | if (strlen(sign_off_header) <= eol - i && | |
734 | !prefixcmp(sb->buf + i, sign_off_header)) { | |
735 | i = eol; | |
736 | continue; | |
737 | } | |
738 | while (i < eol) | |
739 | if (!isspace(sb->buf[i++])) | |
740 | return 0; | |
741 | } | |
742 | ||
743 | return 1; | |
744 | } | |
745 | ||
146ea068 JH |
746 | static const char *find_author_by_nickname(const char *name) |
747 | { | |
748 | struct rev_info revs; | |
749 | struct commit *commit; | |
750 | struct strbuf buf = STRBUF_INIT; | |
751 | const char *av[20]; | |
752 | int ac = 0; | |
753 | ||
754 | init_revisions(&revs, NULL); | |
755 | strbuf_addf(&buf, "--author=%s", name); | |
756 | av[++ac] = "--all"; | |
757 | av[++ac] = "-i"; | |
758 | av[++ac] = buf.buf; | |
759 | av[++ac] = NULL; | |
760 | setup_revisions(ac, av, &revs, NULL); | |
761 | prepare_revision_walk(&revs); | |
762 | commit = get_revision(&revs); | |
763 | if (commit) { | |
dd2e794a TR |
764 | struct pretty_print_context ctx = {0}; |
765 | ctx.date_mode = DATE_NORMAL; | |
146ea068 | 766 | strbuf_release(&buf); |
dd2e794a | 767 | format_commit_message(commit, "%an <%ae>", &buf, &ctx); |
146ea068 JH |
768 | return strbuf_detach(&buf, NULL); |
769 | } | |
770 | die("No existing author found with '%s'", name); | |
771 | } | |
772 | ||
76e2f7ce JH |
773 | |
774 | static void handle_untracked_files_arg(struct wt_status *s) | |
775 | { | |
776 | if (!untracked_files_arg) | |
777 | ; /* default already initialized */ | |
778 | else if (!strcmp(untracked_files_arg, "no")) | |
779 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
780 | else if (!strcmp(untracked_files_arg, "normal")) | |
781 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
782 | else if (!strcmp(untracked_files_arg, "all")) | |
783 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
784 | else | |
785 | die("Invalid untracked files mode '%s'", untracked_files_arg); | |
786 | } | |
787 | ||
2f02b25f | 788 | static int parse_and_validate_options(int argc, const char *argv[], |
dbd0f5c7 | 789 | const char * const usage[], |
d249b098 JH |
790 | const char *prefix, |
791 | struct wt_status *s) | |
f5bbc322 KH |
792 | { |
793 | int f = 0; | |
794 | ||
37782920 SB |
795 | argc = parse_options(argc, argv, prefix, builtin_commit_options, usage, |
796 | 0); | |
f5bbc322 | 797 | |
146ea068 JH |
798 | if (force_author && !strchr(force_author, '>')) |
799 | force_author = find_author_by_nickname(force_author); | |
800 | ||
c51f6cee EM |
801 | if (force_author && renew_authorship) |
802 | die("Using both --reset-author and --author does not make sense"); | |
803 | ||
f9568530 | 804 | if (logfile || message.len || use_message) |
4803466f | 805 | use_editor = 0; |
f5bbc322 | 806 | if (edit_flag) |
4803466f | 807 | use_editor = 1; |
406400ce PB |
808 | if (!use_editor) |
809 | setenv("GIT_EDITOR", ":", 1); | |
f5bbc322 KH |
810 | |
811 | if (get_sha1("HEAD", head_sha1)) | |
812 | initial_commit = 1; | |
813 | ||
f5bbc322 KH |
814 | /* Sanity check options */ |
815 | if (amend && initial_commit) | |
816 | die("You have nothing to amend."); | |
817 | if (amend && in_merge) | |
b5b644a9 | 818 | die("You are in the middle of a merge -- cannot amend."); |
f5bbc322 KH |
819 | |
820 | if (use_message) | |
821 | f++; | |
822 | if (edit_message) | |
823 | f++; | |
824 | if (logfile) | |
825 | f++; | |
826 | if (f > 1) | |
827 | die("Only one of -c/-C/-F can be used."); | |
f9568530 | 828 | if (message.len && f > 0) |
f5bbc322 KH |
829 | die("Option -m cannot be combined with -c/-C/-F."); |
830 | if (edit_message) | |
831 | use_message = edit_message; | |
1eb1e9ee | 832 | if (amend && !use_message) |
f5bbc322 | 833 | use_message = "HEAD"; |
c51f6cee EM |
834 | if (!use_message && renew_authorship) |
835 | die("--reset-author can be used only with -C, -c or --amend."); | |
f5bbc322 KH |
836 | if (use_message) { |
837 | unsigned char sha1[20]; | |
838 | static char utf8[] = "UTF-8"; | |
839 | const char *out_enc; | |
840 | char *enc, *end; | |
841 | struct commit *commit; | |
842 | ||
843 | if (get_sha1(use_message, sha1)) | |
844 | die("could not lookup commit %s", use_message); | |
8a2f8733 | 845 | commit = lookup_commit_reference(sha1); |
f5bbc322 KH |
846 | if (!commit || parse_commit(commit)) |
847 | die("could not parse commit %s", use_message); | |
848 | ||
849 | enc = strstr(commit->buffer, "\nencoding"); | |
850 | if (enc) { | |
851 | end = strchr(enc + 10, '\n'); | |
852 | enc = xstrndup(enc + 10, end - (enc + 10)); | |
853 | } else { | |
854 | enc = utf8; | |
855 | } | |
856 | out_enc = git_commit_encoding ? git_commit_encoding : utf8; | |
857 | ||
858 | if (strcmp(out_enc, enc)) | |
859 | use_message_buffer = | |
860 | reencode_string(commit->buffer, out_enc, enc); | |
861 | ||
862 | /* | |
863 | * If we failed to reencode the buffer, just copy it | |
864 | * byte for byte so the user can try to fix it up. | |
865 | * This also handles the case where input and output | |
866 | * encodings are identical. | |
867 | */ | |
868 | if (use_message_buffer == NULL) | |
869 | use_message_buffer = xstrdup(commit->buffer); | |
870 | if (enc != utf8) | |
871 | free(enc); | |
872 | } | |
873 | ||
874 | if (!!also + !!only + !!all + !!interactive > 1) | |
875 | die("Only one of --include/--only/--all/--interactive can be used."); | |
876 | if (argc == 0 && (also || (only && !amend))) | |
877 | die("No paths with --include/--only does not make sense."); | |
878 | if (argc == 0 && only && amend) | |
879 | only_include_assumed = "Clever... amending the last one with dirty index."; | |
3c5283f8 | 880 | if (argc > 0 && !also && !only) |
f5bbc322 | 881 | only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths..."; |
5f065737 AR |
882 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
883 | cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE; | |
884 | else if (!strcmp(cleanup_arg, "verbatim")) | |
885 | cleanup_mode = CLEANUP_NONE; | |
886 | else if (!strcmp(cleanup_arg, "whitespace")) | |
887 | cleanup_mode = CLEANUP_SPACE; | |
888 | else if (!strcmp(cleanup_arg, "strip")) | |
889 | cleanup_mode = CLEANUP_ALL; | |
890 | else | |
891 | die("Invalid cleanup mode %s", cleanup_arg); | |
f5bbc322 | 892 | |
76e2f7ce | 893 | handle_untracked_files_arg(s); |
4bfee30a | 894 | |
f5bbc322 KH |
895 | if (all && argc > 0) |
896 | die("Paths with -a does not make sense."); | |
897 | else if (interactive && argc > 0) | |
898 | die("Paths with --interactive does not make sense."); | |
899 | ||
7c9f7038 JK |
900 | if (null_termination && status_format == STATUS_FORMAT_LONG) |
901 | status_format = STATUS_FORMAT_PORCELAIN; | |
902 | if (status_format != STATUS_FORMAT_LONG) | |
903 | dry_run = 1; | |
904 | ||
f5bbc322 KH |
905 | return argc; |
906 | } | |
907 | ||
d249b098 JH |
908 | static int dry_run_commit(int argc, const char **argv, const char *prefix, |
909 | struct wt_status *s) | |
f5bbc322 | 910 | { |
f5bbc322 | 911 | int commitable; |
3a5d13a3 | 912 | const char *index_file; |
f5bbc322 | 913 | |
3a5d13a3 | 914 | index_file = prepare_index(argc, argv, prefix, 1); |
d249b098 | 915 | commitable = run_status(stdout, index_file, prefix, 0, s); |
3a5d13a3 | 916 | rollback_index_files(); |
f5bbc322 | 917 | |
3a5d13a3 JH |
918 | return commitable ? 0 : 1; |
919 | } | |
920 | ||
f766b367 JH |
921 | static int parse_status_slot(const char *var, int offset) |
922 | { | |
923 | if (!strcasecmp(var+offset, "header")) | |
924 | return WT_STATUS_HEADER; | |
925 | if (!strcasecmp(var+offset, "updated") | |
926 | || !strcasecmp(var+offset, "added")) | |
927 | return WT_STATUS_UPDATED; | |
928 | if (!strcasecmp(var+offset, "changed")) | |
929 | return WT_STATUS_CHANGED; | |
930 | if (!strcasecmp(var+offset, "untracked")) | |
931 | return WT_STATUS_UNTRACKED; | |
932 | if (!strcasecmp(var+offset, "nobranch")) | |
933 | return WT_STATUS_NOBRANCH; | |
934 | if (!strcasecmp(var+offset, "unmerged")) | |
935 | return WT_STATUS_UNMERGED; | |
8b8e8624 | 936 | return -1; |
f766b367 JH |
937 | } |
938 | ||
939 | static int git_status_config(const char *k, const char *v, void *cb) | |
940 | { | |
941 | struct wt_status *s = cb; | |
942 | ||
943 | if (!strcmp(k, "status.submodulesummary")) { | |
944 | int is_bool; | |
945 | s->submodule_summary = git_config_bool_or_int(k, v, &is_bool); | |
946 | if (is_bool && s->submodule_summary) | |
947 | s->submodule_summary = -1; | |
948 | return 0; | |
949 | } | |
950 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { | |
951 | s->use_color = git_config_colorbool(k, v, -1); | |
952 | return 0; | |
953 | } | |
954 | if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { | |
955 | int slot = parse_status_slot(k, 13); | |
8b8e8624 JK |
956 | if (slot < 0) |
957 | return 0; | |
f766b367 JH |
958 | if (!v) |
959 | return config_error_nonbool(k); | |
960 | color_parse(v, k, s->color_palette[slot]); | |
961 | return 0; | |
962 | } | |
963 | if (!strcmp(k, "status.relativepaths")) { | |
964 | s->relative_paths = git_config_bool(k, v); | |
965 | return 0; | |
966 | } | |
967 | if (!strcmp(k, "status.showuntrackedfiles")) { | |
968 | if (!v) | |
969 | return config_error_nonbool(k); | |
970 | else if (!strcmp(v, "no")) | |
971 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
972 | else if (!strcmp(v, "normal")) | |
973 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
974 | else if (!strcmp(v, "all")) | |
975 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
976 | else | |
977 | return error("Invalid untracked files mode '%s'", v); | |
978 | return 0; | |
979 | } | |
980 | return git_diff_ui_config(k, v, NULL); | |
981 | } | |
982 | ||
3a5d13a3 JH |
983 | int cmd_status(int argc, const char **argv, const char *prefix) |
984 | { | |
d249b098 | 985 | struct wt_status s; |
76e2f7ce | 986 | unsigned char sha1[20]; |
9e4b7ab6 | 987 | static struct option builtin_status_options[] = { |
76e2f7ce | 988 | OPT__VERBOSE(&verbose), |
dd2be243 JK |
989 | OPT_SET_INT('s', "short", &status_format, |
990 | "show status concisely", STATUS_FORMAT_SHORT), | |
6f157871 JK |
991 | OPT_SET_INT(0, "porcelain", &status_format, |
992 | "show porcelain output format", | |
993 | STATUS_FORMAT_PORCELAIN), | |
173e6c88 JH |
994 | OPT_BOOLEAN('z', "null", &null_termination, |
995 | "terminate entries with NUL"), | |
76e2f7ce JH |
996 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, |
997 | "mode", | |
998 | "show untracked files, optional modes: all, normal, no. (Default: all)", | |
999 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, | |
1000 | OPT_END(), | |
1001 | }; | |
1002 | ||
dd2be243 | 1003 | if (null_termination && status_format == STATUS_FORMAT_LONG) |
6f157871 | 1004 | status_format = STATUS_FORMAT_PORCELAIN; |
d249b098 JH |
1005 | |
1006 | wt_status_prepare(&s); | |
1007 | git_config(git_status_config, &s); | |
30988301 | 1008 | in_merge = file_exists(git_path("MERGE_HEAD")); |
76e2f7ce | 1009 | argc = parse_options(argc, argv, prefix, |
9e4b7ab6 JH |
1010 | builtin_status_options, |
1011 | builtin_status_usage, 0); | |
76e2f7ce JH |
1012 | handle_untracked_files_arg(&s); |
1013 | ||
1014 | if (*argv) | |
1015 | s.pathspec = get_pathspec(prefix, argv); | |
1016 | ||
1017 | read_cache(); | |
1018 | refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED); | |
1019 | s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0; | |
3c588453 | 1020 | s.in_merge = in_merge; |
76e2f7ce JH |
1021 | wt_status_collect(&s); |
1022 | ||
8661768f JK |
1023 | if (s.relative_paths) |
1024 | s.prefix = prefix; | |
d249b098 JH |
1025 | if (s.use_color == -1) |
1026 | s.use_color = git_use_color_default; | |
38920dd6 MH |
1027 | if (diff_use_color_default == -1) |
1028 | diff_use_color_default = git_use_color_default; | |
1029 | ||
dd2be243 JK |
1030 | switch (status_format) { |
1031 | case STATUS_FORMAT_SHORT: | |
84dbe7b8 | 1032 | wt_shortstatus_print(&s, null_termination); |
dd2be243 | 1033 | break; |
6f157871 | 1034 | case STATUS_FORMAT_PORCELAIN: |
4a7cc2fd | 1035 | wt_porcelain_print(&s, null_termination); |
6f157871 | 1036 | break; |
dd2be243 | 1037 | case STATUS_FORMAT_LONG: |
173e6c88 | 1038 | s.verbose = verbose; |
173e6c88 | 1039 | wt_status_print(&s); |
dd2be243 | 1040 | break; |
173e6c88 | 1041 | } |
76e2f7ce | 1042 | return 0; |
f5bbc322 KH |
1043 | } |
1044 | ||
f5bbc322 KH |
1045 | static void print_summary(const char *prefix, const unsigned char *sha1) |
1046 | { | |
1047 | struct rev_info rev; | |
1048 | struct commit *commit; | |
c5ee71fd | 1049 | static const char *format = "format:%h] %s"; |
c85db254 JK |
1050 | unsigned char junk_sha1[20]; |
1051 | const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL); | |
f5bbc322 KH |
1052 | |
1053 | commit = lookup_commit(sha1); | |
1054 | if (!commit) | |
b5b644a9 | 1055 | die("couldn't look up newly created commit"); |
f5bbc322 KH |
1056 | if (!commit || parse_commit(commit)) |
1057 | die("could not parse newly created commit"); | |
1058 | ||
1059 | init_revisions(&rev, prefix); | |
1060 | setup_revisions(0, NULL, &rev, NULL); | |
1061 | ||
1062 | rev.abbrev = 0; | |
1063 | rev.diff = 1; | |
1064 | rev.diffopt.output_format = | |
1065 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; | |
1066 | ||
1067 | rev.verbose_header = 1; | |
1068 | rev.show_root_diff = 1; | |
e502f9e7 | 1069 | get_commit_format(format, &rev); |
bf82a150 | 1070 | rev.always_show_header = 0; |
3eb2a15e JH |
1071 | rev.diffopt.detect_rename = 1; |
1072 | rev.diffopt.rename_limit = 100; | |
1073 | rev.diffopt.break_opt = 0; | |
15964563 | 1074 | diff_setup_done(&rev.diffopt); |
f5bbc322 | 1075 | |
c5ee71fd | 1076 | printf("[%s%s ", |
c85db254 JK |
1077 | !prefixcmp(head, "refs/heads/") ? |
1078 | head + 11 : | |
1079 | !strcmp(head, "HEAD") ? | |
1080 | "detached HEAD" : | |
1081 | head, | |
1082 | initial_commit ? " (root-commit)" : ""); | |
f5bbc322 | 1083 | |
bf82a150 | 1084 | if (!log_tree_commit(&rev, commit)) { |
dd2e794a | 1085 | struct pretty_print_context ctx = {0}; |
bf82a150 | 1086 | struct strbuf buf = STRBUF_INIT; |
dd2e794a TR |
1087 | ctx.date_mode = DATE_NORMAL; |
1088 | format_commit_message(commit, format + 7, &buf, &ctx); | |
bf82a150 JH |
1089 | printf("%s\n", buf.buf); |
1090 | strbuf_release(&buf); | |
1091 | } | |
f5bbc322 KH |
1092 | } |
1093 | ||
186458b1 | 1094 | static int git_commit_config(const char *k, const char *v, void *cb) |
f5bbc322 | 1095 | { |
d249b098 JH |
1096 | struct wt_status *s = cb; |
1097 | ||
984c6e7e | 1098 | if (!strcmp(k, "commit.template")) |
395de250 | 1099 | return git_config_pathname(&template_file, k, v); |
f5bbc322 | 1100 | |
d249b098 | 1101 | return git_status_config(k, v, s); |
f5bbc322 KH |
1102 | } |
1103 | ||
f5bbc322 KH |
1104 | int cmd_commit(int argc, const char **argv, const char *prefix) |
1105 | { | |
f285a2d7 | 1106 | struct strbuf sb = STRBUF_INIT; |
f5bbc322 | 1107 | const char *index_file, *reflog_msg; |
99a12694 | 1108 | char *nl, *p; |
f5bbc322 KH |
1109 | unsigned char commit_sha1[20]; |
1110 | struct ref_lock *ref_lock; | |
6bb6b034 | 1111 | struct commit_list *parents = NULL, **pptr = &parents; |
cf10f9fd MV |
1112 | struct stat statbuf; |
1113 | int allow_fast_forward = 1; | |
d249b098 | 1114 | struct wt_status s; |
f5bbc322 | 1115 | |
d249b098 JH |
1116 | wt_status_prepare(&s); |
1117 | git_config(git_commit_config, &s); | |
30988301 | 1118 | in_merge = file_exists(git_path("MERGE_HEAD")); |
3c588453 | 1119 | s.in_merge = in_merge; |
f5bbc322 | 1120 | |
d249b098 JH |
1121 | if (s.use_color == -1) |
1122 | s.use_color = git_use_color_default; | |
d249b098 JH |
1123 | argc = parse_and_validate_options(argc, argv, builtin_commit_usage, |
1124 | prefix, &s); | |
3fa509df JH |
1125 | if (dry_run) { |
1126 | if (diff_use_color_default == -1) | |
1127 | diff_use_color_default = git_use_color_default; | |
d249b098 | 1128 | return dry_run_commit(argc, argv, prefix, &s); |
3fa509df | 1129 | } |
50b7e70f | 1130 | index_file = prepare_index(argc, argv, prefix, 0); |
f5bbc322 | 1131 | |
ec84bd00 PB |
1132 | /* Set up everything for writing the commit object. This includes |
1133 | running hooks, writing the trees, and interacting with the user. */ | |
d249b098 | 1134 | if (!prepare_to_commit(index_file, prefix, &s)) { |
2888605c | 1135 | rollback_index_files(); |
f5bbc322 KH |
1136 | return 1; |
1137 | } | |
1138 | ||
f5bbc322 KH |
1139 | /* Determine parents */ |
1140 | if (initial_commit) { | |
1141 | reflog_msg = "commit (initial)"; | |
f5bbc322 KH |
1142 | } else if (amend) { |
1143 | struct commit_list *c; | |
1144 | struct commit *commit; | |
1145 | ||
1146 | reflog_msg = "commit (amend)"; | |
1147 | commit = lookup_commit(head_sha1); | |
1148 | if (!commit || parse_commit(commit)) | |
1149 | die("could not parse HEAD commit"); | |
1150 | ||
1151 | for (c = commit->parents; c; c = c->next) | |
6bb6b034 | 1152 | pptr = &commit_list_insert(c->item, pptr)->next; |
f5bbc322 | 1153 | } else if (in_merge) { |
f285a2d7 | 1154 | struct strbuf m = STRBUF_INIT; |
f5bbc322 KH |
1155 | FILE *fp; |
1156 | ||
1157 | reflog_msg = "commit (merge)"; | |
6bb6b034 | 1158 | pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next; |
f5bbc322 KH |
1159 | fp = fopen(git_path("MERGE_HEAD"), "r"); |
1160 | if (fp == NULL) | |
d824cbba TR |
1161 | die_errno("could not open '%s' for reading", |
1162 | git_path("MERGE_HEAD")); | |
7c3fd25d LT |
1163 | while (strbuf_getline(&m, fp, '\n') != EOF) { |
1164 | unsigned char sha1[20]; | |
1165 | if (get_sha1_hex(m.buf, sha1) < 0) | |
1166 | die("Corrupt MERGE_HEAD file (%s)", m.buf); | |
6bb6b034 | 1167 | pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next; |
7c3fd25d | 1168 | } |
f5bbc322 KH |
1169 | fclose(fp); |
1170 | strbuf_release(&m); | |
cf10f9fd MV |
1171 | if (!stat(git_path("MERGE_MODE"), &statbuf)) { |
1172 | if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0) | |
d824cbba | 1173 | die_errno("could not read MERGE_MODE"); |
cf10f9fd MV |
1174 | if (!strcmp(sb.buf, "no-ff")) |
1175 | allow_fast_forward = 0; | |
1176 | } | |
1177 | if (allow_fast_forward) | |
1178 | parents = reduce_heads(parents); | |
f5bbc322 KH |
1179 | } else { |
1180 | reflog_msg = "commit"; | |
6bb6b034 | 1181 | pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next; |
f5bbc322 | 1182 | } |
f5bbc322 | 1183 | |
ec84bd00 | 1184 | /* Finally, get the commit message */ |
cf10f9fd | 1185 | strbuf_reset(&sb); |
740001a5 | 1186 | if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) { |
0721c314 | 1187 | int saved_errno = errno; |
740001a5 | 1188 | rollback_index_files(); |
0721c314 | 1189 | die("could not read commit message: %s", strerror(saved_errno)); |
740001a5 | 1190 | } |
99a12694 KH |
1191 | |
1192 | /* Truncate the message just before the diff, if any. */ | |
0b38227f JK |
1193 | if (verbose) { |
1194 | p = strstr(sb.buf, "\ndiff --git "); | |
1195 | if (p != NULL) | |
1196 | strbuf_setlen(&sb, p - sb.buf + 1); | |
1197 | } | |
99a12694 | 1198 | |
5f065737 AR |
1199 | if (cleanup_mode != CLEANUP_NONE) |
1200 | stripspace(&sb, cleanup_mode == CLEANUP_ALL); | |
6bb6b034 | 1201 | if (message_is_empty(&sb)) { |
2888605c | 1202 | rollback_index_files(); |
fdc7c811 JK |
1203 | fprintf(stderr, "Aborting commit due to empty commit message.\n"); |
1204 | exit(1); | |
2888605c | 1205 | } |
f5bbc322 | 1206 | |
6bb6b034 MV |
1207 | if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1, |
1208 | fmt_ident(author_name, author_email, author_date, | |
1209 | IDENT_ERROR_ON_NO_NAME))) { | |
2888605c | 1210 | rollback_index_files(); |
f5bbc322 | 1211 | die("failed to write commit object"); |
2888605c | 1212 | } |
f5bbc322 KH |
1213 | |
1214 | ref_lock = lock_any_ref_for_update("HEAD", | |
1215 | initial_commit ? NULL : head_sha1, | |
1216 | 0); | |
1217 | ||
6bb6b034 | 1218 | nl = strchr(sb.buf, '\n'); |
741707b1 JS |
1219 | if (nl) |
1220 | strbuf_setlen(&sb, nl + 1 - sb.buf); | |
1221 | else | |
1222 | strbuf_addch(&sb, '\n'); | |
741707b1 JS |
1223 | strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg)); |
1224 | strbuf_insert(&sb, strlen(reflog_msg), ": ", 2); | |
f5bbc322 | 1225 | |
2888605c JH |
1226 | if (!ref_lock) { |
1227 | rollback_index_files(); | |
f5bbc322 | 1228 | die("cannot lock HEAD ref"); |
2888605c JH |
1229 | } |
1230 | if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) { | |
1231 | rollback_index_files(); | |
f5bbc322 | 1232 | die("cannot update HEAD ref"); |
2888605c | 1233 | } |
f5bbc322 KH |
1234 | |
1235 | unlink(git_path("MERGE_HEAD")); | |
1236 | unlink(git_path("MERGE_MSG")); | |
cf10f9fd | 1237 | unlink(git_path("MERGE_MODE")); |
5a95b855 | 1238 | unlink(git_path("SQUASH_MSG")); |
f5bbc322 | 1239 | |
5a9dd399 BC |
1240 | if (commit_index_files()) |
1241 | die ("Repository has been updated, but unable to write\n" | |
1242 | "new_index file. Check that disk is not full or quota is\n" | |
1243 | "not exceeded, and then \"git reset HEAD\" to recover."); | |
f5bbc322 KH |
1244 | |
1245 | rerere(); | |
2888605c | 1246 | run_hook(get_index_file(), "post-commit", NULL); |
f5bbc322 KH |
1247 | if (!quiet) |
1248 | print_summary(prefix, commit_sha1); | |
1249 | ||
1250 | return 0; | |
1251 | } |