]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-commit.c
git-svn: handle our top-level path being deleted and later re-added
[thirdparty/git.git] / builtin-commit.c
CommitLineData
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"
2888605c 10#include "dir.h"
f5bbc322
KH
11#include "builtin.h"
12#include "diff.h"
13#include "diffcore.h"
14#include "commit.h"
15#include "revision.h"
16#include "wt-status.h"
17#include "run-command.h"
18#include "refs.h"
19#include "log-tree.h"
20#include "strbuf.h"
21#include "utf8.h"
22#include "parse-options.h"
2888605c 23#include "path-list.h"
f5bbc322
KH
24
25static const char * const builtin_commit_usage[] = {
26 "git-commit [options] [--] <filepattern>...",
27 NULL
28};
29
2f02b25f
SB
30static const char * const builtin_status_usage[] = {
31 "git-status [options] [--] <filepattern>...",
32 NULL
33};
34
f5bbc322
KH
35static unsigned char head_sha1[20], merge_head_sha1[20];
36static char *use_message_buffer;
37static const char commit_editmsg[] = "COMMIT_EDITMSG";
2888605c
JH
38static struct lock_file index_lock; /* real index */
39static struct lock_file false_lock; /* used only for partial commits */
40static enum {
41 COMMIT_AS_IS = 1,
42 COMMIT_NORMAL,
43 COMMIT_PARTIAL,
44} commit_style;
f5bbc322 45
f9568530 46static char *logfile, *force_author, *template_file;
f5bbc322
KH
47static char *edit_message, *use_message;
48static int all, edit_flag, also, interactive, only, amend, signoff;
5241b6bf 49static int quiet, verbose, untracked_files, no_verify, allow_empty;
f5bbc322
KH
50
51static int no_edit, initial_commit, in_merge;
52const char *only_include_assumed;
f9568530
JS
53struct strbuf message;
54
55static int opt_parse_m(const struct option *opt, const char *arg, int unset)
56{
57 struct strbuf *buf = opt->value;
58 if (unset)
59 strbuf_setlen(buf, 0);
60 else {
61 strbuf_addstr(buf, arg);
62 strbuf_addch(buf, '\n');
63 strbuf_addch(buf, '\n');
64 }
65 return 0;
66}
f5bbc322
KH
67
68static struct option builtin_commit_options[] = {
69 OPT__QUIET(&quiet),
70 OPT__VERBOSE(&verbose),
71 OPT_GROUP("Commit message options"),
72
73 OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
74 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
f9568530 75 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
f5bbc322
KH
76 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
77 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
78 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by: header"),
79 OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
80 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
81
82 OPT_GROUP("Commit contents options"),
83 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
84 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
85 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
86 OPT_BOOLEAN('o', "only", &only, ""),
87 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
88 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
89 OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
5241b6bf 90 OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
f5bbc322
KH
91
92 OPT_END()
93};
94
2888605c
JH
95static void rollback_index_files(void)
96{
97 switch (commit_style) {
98 case COMMIT_AS_IS:
99 break; /* nothing to do */
100 case COMMIT_NORMAL:
101 rollback_lock_file(&index_lock);
102 break;
103 case COMMIT_PARTIAL:
104 rollback_lock_file(&index_lock);
105 rollback_lock_file(&false_lock);
106 break;
107 }
108}
109
110static void commit_index_files(void)
111{
112 switch (commit_style) {
113 case COMMIT_AS_IS:
114 break; /* nothing to do */
115 case COMMIT_NORMAL:
116 commit_lock_file(&index_lock);
117 break;
118 case COMMIT_PARTIAL:
119 commit_lock_file(&index_lock);
120 rollback_lock_file(&false_lock);
121 break;
122 }
123}
124
125/*
126 * Take a union of paths in the index and the named tree (typically, "HEAD"),
127 * and return the paths that match the given pattern in list.
128 */
129static int list_paths(struct path_list *list, const char *with_tree,
130 const char *prefix, const char **pattern)
131{
132 int i;
133 char *m;
134
135 for (i = 0; pattern[i]; i++)
136 ;
137 m = xcalloc(1, i);
138
139 if (with_tree)
140 overlay_tree_on_cache(with_tree, prefix);
141
142 for (i = 0; i < active_nr; i++) {
143 struct cache_entry *ce = active_cache[i];
144 if (ce->ce_flags & htons(CE_UPDATE))
145 continue;
146 if (!pathspec_match(pattern, m, ce->name, 0))
147 continue;
148 path_list_insert(ce->name, list);
149 }
150
151 return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
152}
153
154static void add_remove_files(struct path_list *list)
155{
156 int i;
157 for (i = 0; i < list->nr; i++) {
158 struct path_list_item *p = &(list->items[i]);
159 if (file_exists(p->path))
160 add_file_to_cache(p->path, 0);
161 else
162 remove_file_from_cache(p->path);
163 }
164}
165
f64fe7b4 166static char *prepare_index(int argc, const char **argv, const char *prefix)
f5bbc322
KH
167{
168 int fd;
169 struct tree *tree;
2888605c
JH
170 struct path_list partial;
171 const char **pathspec = NULL;
f5bbc322
KH
172
173 if (interactive) {
3f061887 174 interactive_add(argc, argv, prefix);
2888605c 175 commit_style = COMMIT_AS_IS;
f5bbc322
KH
176 return get_index_file();
177 }
178
f5bbc322
KH
179 if (read_cache() < 0)
180 die("index file corrupt");
181
f64fe7b4
JH
182 if (*argv)
183 pathspec = get_pathspec(prefix, argv);
2888605c
JH
184
185 /*
186 * Non partial, non as-is commit.
187 *
188 * (1) get the real index;
189 * (2) update the_index as necessary;
190 * (3) write the_index out to the real index (still locked);
191 * (4) return the name of the locked index file.
192 *
193 * The caller should run hooks on the locked real index, and
194 * (A) if all goes well, commit the real index;
195 * (B) on failure, rollback the real index.
196 */
197 if (all || (also && pathspec && *pathspec)) {
198 int fd = hold_locked_index(&index_lock, 1);
199 add_files_to_cache(0, also ? prefix : NULL, pathspec);
d37d3203 200 refresh_cache(REFRESH_QUIET);
f5bbc322
KH
201 if (write_cache(fd, active_cache, active_nr) || close(fd))
202 die("unable to write new_index file");
2888605c
JH
203 commit_style = COMMIT_NORMAL;
204 return index_lock.filename;
f5bbc322
KH
205 }
206
2888605c
JH
207 /*
208 * As-is commit.
209 *
210 * (1) return the name of the real index file.
211 *
212 * The caller should run hooks on the real index, and run
213 * hooks on the real index, and create commit from the_index.
214 * We still need to refresh the index here.
215 */
216 if (!pathspec || !*pathspec) {
217 fd = hold_locked_index(&index_lock, 1);
218 refresh_cache(REFRESH_QUIET);
219 if (write_cache(fd, active_cache, active_nr) ||
220 close(fd) || commit_locked_index(&index_lock))
221 die("unable to write new_index file");
222 commit_style = COMMIT_AS_IS;
f5bbc322
KH
223 return get_index_file();
224 }
225
2888605c
JH
226 /*
227 * A partial commit.
228 *
229 * (0) find the set of affected paths;
230 * (1) get lock on the real index file;
231 * (2) update the_index with the given paths;
232 * (3) write the_index out to the real index (still locked);
233 * (4) get lock on the false index file;
234 * (5) reset the_index from HEAD;
235 * (6) update the_index the same way as (2);
236 * (7) write the_index out to the false index file;
237 * (8) return the name of the false index file (still locked);
238 *
239 * The caller should run hooks on the locked false index, and
240 * create commit from it. Then
241 * (A) if all goes well, commit the real index;
242 * (B) on failure, rollback the real index;
243 * In either case, rollback the false index.
244 */
245 commit_style = COMMIT_PARTIAL;
246
247 if (file_exists(git_path("MERGE_HEAD")))
248 die("cannot do a partial commit during a merge.");
249
250 memset(&partial, 0, sizeof(partial));
251 partial.strdup_paths = 1;
252 if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
253 exit(1);
254
255 discard_cache();
256 if (read_cache() < 0)
257 die("cannot read the index");
258
259 fd = hold_locked_index(&index_lock, 1);
260 add_remove_files(&partial);
ef12b50d 261 refresh_cache(REFRESH_QUIET);
f5bbc322
KH
262 if (write_cache(fd, active_cache, active_nr) || close(fd))
263 die("unable to write new_index file");
264
2888605c
JH
265 fd = hold_lock_file_for_update(&false_lock,
266 git_path("next-index-%d", getpid()), 1);
267 discard_cache();
f5bbc322
KH
268 if (!initial_commit) {
269 tree = parse_tree_indirect(head_sha1);
270 if (!tree)
271 die("failed to unpack HEAD tree object");
272 if (read_tree(tree, 0, NULL))
273 die("failed to read HEAD tree object");
274 }
2888605c 275 add_remove_files(&partial);
d37d3203 276 refresh_cache(REFRESH_QUIET);
f5bbc322 277
2888605c
JH
278 if (write_cache(fd, active_cache, active_nr) || close(fd))
279 die("unable to write temporary index file");
280 return false_lock.filename;
f5bbc322
KH
281}
282
37d07f8f 283static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
f5bbc322
KH
284{
285 struct wt_status s;
286
287 wt_status_prepare(&s);
46f721c8
JK
288 if (wt_status_relative_paths)
289 s.prefix = prefix;
f5bbc322
KH
290
291 if (amend) {
292 s.amend = 1;
293 s.reference = "HEAD^1";
294 }
295 s.verbose = verbose;
296 s.untracked = untracked_files;
297 s.index_file = index_file;
298 s.fp = fp;
37d07f8f 299 s.nowarn = nowarn;
f5bbc322
KH
300
301 wt_status_print(&s);
302
303 return s.commitable;
304}
305
306static const char sign_off_header[] = "Signed-off-by: ";
307
367c9886 308static int prepare_log_message(const char *index_file, const char *prefix)
f5bbc322
KH
309{
310 struct stat statbuf;
bc5d248a 311 int commitable, saved_color_setting;
f5bbc322
KH
312 struct strbuf sb;
313 char *buffer;
314 FILE *fp;
315
316 strbuf_init(&sb, 0);
f9568530
JS
317 if (message.len) {
318 strbuf_addbuf(&sb, &message);
f5bbc322
KH
319 } else if (logfile && !strcmp(logfile, "-")) {
320 if (isatty(0))
321 fprintf(stderr, "(reading log message from standard input)\n");
322 if (strbuf_read(&sb, 0, 0) < 0)
323 die("could not read log from standard input");
324 } else if (logfile) {
325 if (strbuf_read_file(&sb, logfile, 0) < 0)
326 die("could not read log file '%s': %s",
327 logfile, strerror(errno));
328 } else if (use_message) {
329 buffer = strstr(use_message_buffer, "\n\n");
330 if (!buffer || buffer[2] == '\0')
331 die("commit has empty message");
332 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
333 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
334 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
335 die("could not read MERGE_MSG: %s", strerror(errno));
336 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
337 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
338 die("could not read SQUASH_MSG: %s", strerror(errno));
339 } else if (template_file && !stat(template_file, &statbuf)) {
340 if (strbuf_read_file(&sb, template_file, 0) < 0)
341 die("could not read %s: %s",
342 template_file, strerror(errno));
343 }
344
345 fp = fopen(git_path(commit_editmsg), "w");
346 if (fp == NULL)
b5b644a9 347 die("could not open %s", git_path(commit_editmsg));
f5bbc322
KH
348
349 stripspace(&sb, 0);
f5bbc322
KH
350
351 if (signoff) {
13208572
JS
352 struct strbuf sob;
353 int i;
354
355 strbuf_init(&sob, 0);
356 strbuf_addstr(&sob, sign_off_header);
d9ccfe77
JH
357 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
358 getenv("GIT_COMMITTER_EMAIL")));
13208572 359 strbuf_addch(&sob, '\n');
13208572
JS
360 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
361 ; /* do nothing */
2150554b
JS
362 if (prefixcmp(sb.buf + i, sob.buf)) {
363 if (prefixcmp(sb.buf + i, sign_off_header))
364 strbuf_addch(&sb, '\n');
13208572 365 strbuf_addbuf(&sb, &sob);
2150554b 366 }
13208572 367 strbuf_release(&sob);
f5bbc322
KH
368 }
369
13208572 370 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
b5b644a9 371 die("could not write commit template: %s", strerror(errno));
13208572 372
f5bbc322
KH
373 strbuf_release(&sb);
374
7168624c
AR
375 if (no_edit) {
376 struct rev_info rev;
377 unsigned char sha1[40];
378
379 fclose(fp);
380
381 if (!active_nr && read_cache() < 0)
382 die("Cannot read index");
383
384 if (get_sha1("HEAD", sha1) != 0)
385 return !!active_nr;
386
387 init_revisions(&rev, "");
388 rev.abbrev = 0;
389 setup_revisions(0, NULL, &rev, "HEAD");
390 DIFF_OPT_SET(&rev.diffopt, QUIET);
391 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
392 run_diff_index(&rev, 1 /* cached */);
393
394 return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
395 }
396
f5bbc322
KH
397 if (in_merge && !no_edit)
398 fprintf(fp,
399 "#\n"
400 "# It looks like you may be committing a MERGE.\n"
401 "# If this is not correct, please remove the file\n"
402 "# %s\n"
403 "# and try again.\n"
404 "#\n",
405 git_path("MERGE_HEAD"));
406
407 fprintf(fp,
408 "\n"
409 "# Please enter the commit message for your changes.\n"
410 "# (Comment lines starting with '#' will not be included)\n");
411 if (only_include_assumed)
412 fprintf(fp, "# %s\n", only_include_assumed);
413
bc5d248a
JH
414 saved_color_setting = wt_status_use_color;
415 wt_status_use_color = 0;
37d07f8f 416 commitable = run_status(fp, index_file, prefix, 1);
bc5d248a 417 wt_status_use_color = saved_color_setting;
f5bbc322
KH
418
419 fclose(fp);
420
421 return commitable;
422}
423
424/*
425 * Find out if the message starting at position 'start' in the strbuf
426 * contains only whitespace and Signed-off-by lines.
427 */
428static int message_is_empty(struct strbuf *sb, int start)
429{
430 struct strbuf tmpl;
431 const char *nl;
432 int eol, i;
433
434 /* See if the template is just a prefix of the message. */
435 strbuf_init(&tmpl, 0);
436 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
437 stripspace(&tmpl, 1);
438 if (start + tmpl.len <= sb->len &&
439 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
440 start += tmpl.len;
441 }
442 strbuf_release(&tmpl);
443
444 /* Check if the rest is just whitespace and Signed-of-by's. */
445 for (i = start; i < sb->len; i++) {
446 nl = memchr(sb->buf + i, '\n', sb->len - i);
447 if (nl)
448 eol = nl - sb->buf;
449 else
450 eol = sb->len;
451
452 if (strlen(sign_off_header) <= eol - i &&
453 !prefixcmp(sb->buf + i, sign_off_header)) {
454 i = eol;
455 continue;
456 }
457 while (i < eol)
458 if (!isspace(sb->buf[i++]))
459 return 0;
460 }
461
462 return 1;
463}
464
465static void determine_author_info(struct strbuf *sb)
466{
467 char *name, *email, *date;
468
469 name = getenv("GIT_AUTHOR_NAME");
470 email = getenv("GIT_AUTHOR_EMAIL");
471 date = getenv("GIT_AUTHOR_DATE");
472
473 if (use_message) {
474 const char *a, *lb, *rb, *eol;
475
476 a = strstr(use_message_buffer, "\nauthor ");
477 if (!a)
b5b644a9 478 die("invalid commit: %s", use_message);
f5bbc322
KH
479
480 lb = strstr(a + 8, " <");
481 rb = strstr(a + 8, "> ");
482 eol = strchr(a + 8, '\n');
483 if (!lb || !rb || !eol)
b5b644a9 484 die("invalid commit: %s", use_message);
f5bbc322
KH
485
486 name = xstrndup(a + 8, lb - (a + 8));
487 email = xstrndup(lb + 2, rb - (lb + 2));
488 date = xstrndup(rb + 2, eol - (rb + 2));
489 }
490
491 if (force_author) {
492 const char *lb = strstr(force_author, " <");
493 const char *rb = strchr(force_author, '>');
494
495 if (!lb || !rb)
b5b644a9 496 die("malformed --author parameter");
f5bbc322
KH
497 name = xstrndup(force_author, lb - force_author);
498 email = xstrndup(lb + 2, rb - (lb + 2));
499 }
500
774751a8 501 strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME));
f5bbc322
KH
502}
503
2f02b25f
SB
504static int parse_and_validate_options(int argc, const char *argv[],
505 const char * const usage[])
f5bbc322
KH
506{
507 int f = 0;
508
2f02b25f 509 argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
f5bbc322 510
f9568530 511 if (logfile || message.len || use_message)
f5bbc322
KH
512 no_edit = 1;
513 if (edit_flag)
514 no_edit = 0;
515
516 if (get_sha1("HEAD", head_sha1))
517 initial_commit = 1;
518
519 if (!get_sha1("MERGE_HEAD", merge_head_sha1))
520 in_merge = 1;
521
522 /* Sanity check options */
523 if (amend && initial_commit)
524 die("You have nothing to amend.");
525 if (amend && in_merge)
b5b644a9 526 die("You are in the middle of a merge -- cannot amend.");
f5bbc322
KH
527
528 if (use_message)
529 f++;
530 if (edit_message)
531 f++;
532 if (logfile)
533 f++;
534 if (f > 1)
535 die("Only one of -c/-C/-F can be used.");
f9568530 536 if (message.len && f > 0)
f5bbc322
KH
537 die("Option -m cannot be combined with -c/-C/-F.");
538 if (edit_message)
539 use_message = edit_message;
540 if (amend)
541 use_message = "HEAD";
542 if (use_message) {
543 unsigned char sha1[20];
544 static char utf8[] = "UTF-8";
545 const char *out_enc;
546 char *enc, *end;
547 struct commit *commit;
548
549 if (get_sha1(use_message, sha1))
550 die("could not lookup commit %s", use_message);
551 commit = lookup_commit(sha1);
552 if (!commit || parse_commit(commit))
553 die("could not parse commit %s", use_message);
554
555 enc = strstr(commit->buffer, "\nencoding");
556 if (enc) {
557 end = strchr(enc + 10, '\n');
558 enc = xstrndup(enc + 10, end - (enc + 10));
559 } else {
560 enc = utf8;
561 }
562 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
563
564 if (strcmp(out_enc, enc))
565 use_message_buffer =
566 reencode_string(commit->buffer, out_enc, enc);
567
568 /*
569 * If we failed to reencode the buffer, just copy it
570 * byte for byte so the user can try to fix it up.
571 * This also handles the case where input and output
572 * encodings are identical.
573 */
574 if (use_message_buffer == NULL)
575 use_message_buffer = xstrdup(commit->buffer);
576 if (enc != utf8)
577 free(enc);
578 }
579
580 if (!!also + !!only + !!all + !!interactive > 1)
581 die("Only one of --include/--only/--all/--interactive can be used.");
582 if (argc == 0 && (also || (only && !amend)))
583 die("No paths with --include/--only does not make sense.");
584 if (argc == 0 && only && amend)
585 only_include_assumed = "Clever... amending the last one with dirty index.";
586 if (argc > 0 && !also && !only) {
587 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
588 also = 0;
589 }
590
591 if (all && argc > 0)
592 die("Paths with -a does not make sense.");
593 else if (interactive && argc > 0)
594 die("Paths with --interactive does not make sense.");
595
596 return argc;
597}
598
599int cmd_status(int argc, const char **argv, const char *prefix)
600{
601 const char *index_file;
602 int commitable;
603
604 git_config(git_status_config);
605
2f02b25f 606 argc = parse_and_validate_options(argc, argv, builtin_status_usage);
f5bbc322 607
f64fe7b4 608 index_file = prepare_index(argc, argv, prefix);
f5bbc322 609
37d07f8f 610 commitable = run_status(stdout, index_file, prefix, 0);
f5bbc322 611
2888605c 612 rollback_index_files();
f5bbc322
KH
613
614 return commitable ? 0 : 1;
615}
616
617static int run_hook(const char *index_file, const char *name, const char *arg)
618{
619 struct child_process hook;
620 const char *argv[3], *env[2];
621 char index[PATH_MAX];
622
623 argv[0] = git_path("hooks/%s", name);
624 argv[1] = arg;
625 argv[2] = NULL;
626 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
627 env[0] = index;
628 env[1] = NULL;
629
630 if (access(argv[0], X_OK) < 0)
631 return 0;
632
633 memset(&hook, 0, sizeof(hook));
634 hook.argv = argv;
635 hook.no_stdin = 1;
636 hook.stdout_to_stderr = 1;
637 hook.env = env;
638
639 return run_command(&hook);
640}
641
642static void print_summary(const char *prefix, const unsigned char *sha1)
643{
644 struct rev_info rev;
645 struct commit *commit;
646
647 commit = lookup_commit(sha1);
648 if (!commit)
b5b644a9 649 die("couldn't look up newly created commit");
f5bbc322
KH
650 if (!commit || parse_commit(commit))
651 die("could not parse newly created commit");
652
653 init_revisions(&rev, prefix);
654 setup_revisions(0, NULL, &rev, NULL);
655
656 rev.abbrev = 0;
657 rev.diff = 1;
658 rev.diffopt.output_format =
659 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
660
661 rev.verbose_header = 1;
662 rev.show_root_diff = 1;
663 rev.commit_format = get_commit_format("format:%h: %s");
bf82a150 664 rev.always_show_header = 0;
f5bbc322
KH
665
666 printf("Created %scommit ", initial_commit ? "initial " : "");
667
bf82a150
JH
668 if (!log_tree_commit(&rev, commit)) {
669 struct strbuf buf = STRBUF_INIT;
670 format_commit_message(commit, "%h: %s", &buf);
671 printf("%s\n", buf.buf);
672 strbuf_release(&buf);
673 }
f5bbc322
KH
674}
675
676int git_commit_config(const char *k, const char *v)
677{
678 if (!strcmp(k, "commit.template")) {
679 template_file = xstrdup(v);
680 return 0;
681 }
682
683 return git_status_config(k, v);
684}
685
9663c3bc
JH
686static int is_a_merge(const unsigned char *sha1)
687{
688 struct commit *commit = lookup_commit(sha1);
689 if (!commit || parse_commit(commit))
690 die("could not parse HEAD commit");
691 return !!(commit->parents && commit->parents->next);
692}
693
f5bbc322
KH
694static const char commit_utf8_warn[] =
695"Warning: commit message does not conform to UTF-8.\n"
696"You may want to amend it after fixing the message, or set the config\n"
697"variable i18n.commitencoding to the encoding your project uses.\n";
698
699int cmd_commit(int argc, const char **argv, const char *prefix)
700{
18abc2db 701 int header_len;
f5bbc322
KH
702 struct strbuf sb;
703 const char *index_file, *reflog_msg;
99a12694 704 char *nl, *p;
f5bbc322
KH
705 unsigned char commit_sha1[20];
706 struct ref_lock *ref_lock;
707
708 git_config(git_commit_config);
709
2f02b25f 710 argc = parse_and_validate_options(argc, argv, builtin_commit_usage);
f5bbc322 711
f64fe7b4 712 index_file = prepare_index(argc, argv, prefix);
f5bbc322 713
2888605c
JH
714 if (!no_verify && run_hook(index_file, "pre-commit", NULL)) {
715 rollback_index_files();
716 return 1;
717 }
f5bbc322 718
9663c3bc 719 if (!prepare_log_message(index_file, prefix) && !in_merge &&
5241b6bf 720 !allow_empty && !(amend && is_a_merge(head_sha1))) {
37d07f8f 721 run_status(stdout, index_file, prefix, 0);
2888605c 722 rollback_index_files();
f5bbc322
KH
723 unlink(commit_editmsg);
724 return 1;
725 }
726
2888605c
JH
727 /*
728 * Re-read the index as pre-commit hook could have updated it,
729 * and write it out as a tree.
730 */
731 discard_cache();
f5bbc322 732 read_cache_from(index_file);
2888605c
JH
733 if (!active_cache_tree)
734 active_cache_tree = cache_tree();
f5bbc322 735 if (cache_tree_update(active_cache_tree,
2888605c
JH
736 active_cache, active_nr, 0, 0) < 0) {
737 rollback_index_files();
f5bbc322 738 die("Error building trees");
2888605c
JH
739 }
740
741 /*
742 * The commit object
743 */
744 strbuf_init(&sb, 0);
f5bbc322
KH
745 strbuf_addf(&sb, "tree %s\n",
746 sha1_to_hex(active_cache_tree->sha1));
747
748 /* Determine parents */
749 if (initial_commit) {
750 reflog_msg = "commit (initial)";
f5bbc322
KH
751 } else if (amend) {
752 struct commit_list *c;
753 struct commit *commit;
754
755 reflog_msg = "commit (amend)";
756 commit = lookup_commit(head_sha1);
757 if (!commit || parse_commit(commit))
758 die("could not parse HEAD commit");
759
760 for (c = commit->parents; c; c = c->next)
761 strbuf_addf(&sb, "parent %s\n",
762 sha1_to_hex(c->item->object.sha1));
763 } else if (in_merge) {
764 struct strbuf m;
765 FILE *fp;
766
767 reflog_msg = "commit (merge)";
768 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
769 strbuf_init(&m, 0);
770 fp = fopen(git_path("MERGE_HEAD"), "r");
771 if (fp == NULL)
772 die("could not open %s for reading: %s",
773 git_path("MERGE_HEAD"), strerror(errno));
774 while (strbuf_getline(&m, fp, '\n') != EOF)
775 strbuf_addf(&sb, "parent %s\n", m.buf);
776 fclose(fp);
777 strbuf_release(&m);
778 } else {
779 reflog_msg = "commit";
780 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
781 }
782
783 determine_author_info(&sb);
774751a8 784 strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
f5bbc322
KH
785 if (!is_encoding_utf8(git_commit_encoding))
786 strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
787 strbuf_addch(&sb, '\n');
788
789 /* Get the commit message and validate it */
790 header_len = sb.len;
8babab95
PH
791 if (!no_edit) {
792 char index[PATH_MAX];
793 const char *env[2] = { index, NULL };
794 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
740001a5 795 launch_editor(git_path(commit_editmsg), NULL, env);
2888605c 796 }
6b95655d
WC
797 if (!no_verify &&
798 run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
2888605c 799 rollback_index_files();
f5bbc322 800 exit(1);
2888605c 801 }
740001a5
JH
802 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
803 rollback_index_files();
804 die("could not read commit message");
805 }
99a12694
KH
806
807 /* Truncate the message just before the diff, if any. */
808 p = strstr(sb.buf, "\ndiff --git a/");
809 if (p != NULL)
a98b8191 810 strbuf_setlen(&sb, p - sb.buf + 1);
99a12694 811
f5bbc322 812 stripspace(&sb, 1);
2888605c
JH
813 if (sb.len < header_len || message_is_empty(&sb, header_len)) {
814 rollback_index_files();
b5b644a9 815 die("no commit message? aborting commit.");
2888605c 816 }
f5bbc322
KH
817 strbuf_addch(&sb, '\0');
818 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
819 fprintf(stderr, commit_utf8_warn);
820
2888605c
JH
821 if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
822 rollback_index_files();
f5bbc322 823 die("failed to write commit object");
2888605c 824 }
f5bbc322
KH
825
826 ref_lock = lock_any_ref_for_update("HEAD",
827 initial_commit ? NULL : head_sha1,
828 0);
829
830 nl = strchr(sb.buf + header_len, '\n');
741707b1
JS
831 if (nl)
832 strbuf_setlen(&sb, nl + 1 - sb.buf);
833 else
834 strbuf_addch(&sb, '\n');
835 strbuf_remove(&sb, 0, header_len);
836 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
837 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
f5bbc322 838
2888605c
JH
839 if (!ref_lock) {
840 rollback_index_files();
f5bbc322 841 die("cannot lock HEAD ref");
2888605c
JH
842 }
843 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
844 rollback_index_files();
f5bbc322 845 die("cannot update HEAD ref");
2888605c 846 }
f5bbc322
KH
847
848 unlink(git_path("MERGE_HEAD"));
849 unlink(git_path("MERGE_MSG"));
850
2888605c 851 commit_index_files();
f5bbc322
KH
852
853 rerere();
2888605c 854 run_hook(get_index_file(), "post-commit", NULL);
f5bbc322
KH
855 if (!quiet)
856 print_summary(prefix, commit_sha1);
857
858 return 0;
859}