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