builtin-commit: fix --signoff
[git/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"
10#include "builtin.h"
11#include "diff.h"
12#include "diffcore.h"
13#include "commit.h"
14#include "revision.h"
15#include "wt-status.h"
16#include "run-command.h"
17#include "refs.h"
18#include "log-tree.h"
19#include "strbuf.h"
20#include "utf8.h"
21#include "parse-options.h"
22
23static const char * const builtin_commit_usage[] = {
24 "git-commit [options] [--] <filepattern>...",
25 NULL
26};
27
28static unsigned char head_sha1[20], merge_head_sha1[20];
29static char *use_message_buffer;
30static const char commit_editmsg[] = "COMMIT_EDITMSG";
31static struct lock_file lock_file;
32
33static char *logfile, *force_author, *message, *template_file;
34static char *edit_message, *use_message;
35static int all, edit_flag, also, interactive, only, amend, signoff;
36static int quiet, verbose, untracked_files, no_verify;
37
38static int no_edit, initial_commit, in_merge;
39const char *only_include_assumed;
40
41static struct option builtin_commit_options[] = {
42 OPT__QUIET(&quiet),
43 OPT__VERBOSE(&verbose),
44 OPT_GROUP("Commit message options"),
45
46 OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
47 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
48 OPT_STRING('m', "message", &message, "MESSAGE", "specify commit message"),
49 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
50 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
51 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by: header"),
52 OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
53 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
54
55 OPT_GROUP("Commit contents options"),
56 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
57 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
58 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
59 OPT_BOOLEAN('o', "only", &only, ""),
60 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
61 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
62 OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
63
64 OPT_END()
65};
66
67static char *prepare_index(const char **files, const char *prefix)
68{
69 int fd;
70 struct tree *tree;
71 struct lock_file *next_index_lock;
72
73 if (interactive) {
74 interactive_add();
75 return get_index_file();
76 }
77
78 fd = hold_locked_index(&lock_file, 1);
79 if (read_cache() < 0)
80 die("index file corrupt");
81
82 if (all || also) {
83 add_files_to_cache(verbose, also ? prefix : NULL, files);
d37d3203 84 refresh_cache(REFRESH_QUIET);
f5bbc322
KH
85 if (write_cache(fd, active_cache, active_nr) || close(fd))
86 die("unable to write new_index file");
87 return lock_file.filename;
88 }
89
90 if (*files == NULL) {
91 /* Commit index as-is. */
92 rollback_lock_file(&lock_file);
93 return get_index_file();
94 }
95
96 /* update the user index file */
97 add_files_to_cache(verbose, prefix, files);
98 if (write_cache(fd, active_cache, active_nr) || close(fd))
99 die("unable to write new_index file");
100
101 if (!initial_commit) {
102 tree = parse_tree_indirect(head_sha1);
103 if (!tree)
104 die("failed to unpack HEAD tree object");
105 if (read_tree(tree, 0, NULL))
106 die("failed to read HEAD tree object");
107 }
108
109 /* Use a lock file to garbage collect the temporary index file. */
110 next_index_lock = xmalloc(sizeof(*next_index_lock));
111 fd = hold_lock_file_for_update(next_index_lock,
112 git_path("next-index-%d", getpid()), 1);
113 add_files_to_cache(verbose, prefix, files);
d37d3203 114 refresh_cache(REFRESH_QUIET);
f5bbc322
KH
115 if (write_cache(fd, active_cache, active_nr) || close(fd))
116 die("unable to write new_index file");
117
118 return next_index_lock->filename;
119}
120
367c9886 121static int run_status(FILE *fp, const char *index_file, const char *prefix)
f5bbc322
KH
122{
123 struct wt_status s;
124
125 wt_status_prepare(&s);
367c9886 126 s.prefix = prefix;
f5bbc322
KH
127
128 if (amend) {
129 s.amend = 1;
130 s.reference = "HEAD^1";
131 }
132 s.verbose = verbose;
133 s.untracked = untracked_files;
134 s.index_file = index_file;
135 s.fp = fp;
136
137 wt_status_print(&s);
138
139 return s.commitable;
140}
141
142static const char sign_off_header[] = "Signed-off-by: ";
143
367c9886 144static int prepare_log_message(const char *index_file, const char *prefix)
f5bbc322
KH
145{
146 struct stat statbuf;
147 int commitable;
148 struct strbuf sb;
149 char *buffer;
150 FILE *fp;
151
152 strbuf_init(&sb, 0);
153 if (message) {
154 strbuf_add(&sb, message, strlen(message));
155 } else if (logfile && !strcmp(logfile, "-")) {
156 if (isatty(0))
157 fprintf(stderr, "(reading log message from standard input)\n");
158 if (strbuf_read(&sb, 0, 0) < 0)
159 die("could not read log from standard input");
160 } else if (logfile) {
161 if (strbuf_read_file(&sb, logfile, 0) < 0)
162 die("could not read log file '%s': %s",
163 logfile, strerror(errno));
164 } else if (use_message) {
165 buffer = strstr(use_message_buffer, "\n\n");
166 if (!buffer || buffer[2] == '\0')
167 die("commit has empty message");
168 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
169 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
170 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
171 die("could not read MERGE_MSG: %s", strerror(errno));
172 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
173 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
174 die("could not read SQUASH_MSG: %s", strerror(errno));
175 } else if (template_file && !stat(template_file, &statbuf)) {
176 if (strbuf_read_file(&sb, template_file, 0) < 0)
177 die("could not read %s: %s",
178 template_file, strerror(errno));
179 }
180
181 fp = fopen(git_path(commit_editmsg), "w");
182 if (fp == NULL)
183 die("could not open %s\n", git_path(commit_editmsg));
184
185 stripspace(&sb, 0);
f5bbc322
KH
186
187 if (signoff) {
13208572
JS
188 struct strbuf sob;
189 int i;
190
191 strbuf_init(&sob, 0);
192 strbuf_addstr(&sob, sign_off_header);
193 strbuf_addstr(&sob, fmt_ident(getenv("GIT_COMMITTER_NAME"),
194 getenv("GIT_COMMITTER_EMAIL"),
195 "", 1));
196 strbuf_addch(&sob, '\n');
197
198 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
199 ; /* do nothing */
200 if (prefixcmp(sb.buf + i, sob.buf))
201 strbuf_addbuf(&sb, &sob);
202 strbuf_release(&sob);
f5bbc322
KH
203 }
204
13208572
JS
205 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
206 die("could not write commit template: %s\n",
207 strerror(errno));
208
f5bbc322
KH
209 strbuf_release(&sb);
210
211 if (in_merge && !no_edit)
212 fprintf(fp,
213 "#\n"
214 "# It looks like you may be committing a MERGE.\n"
215 "# If this is not correct, please remove the file\n"
216 "# %s\n"
217 "# and try again.\n"
218 "#\n",
219 git_path("MERGE_HEAD"));
220
221 fprintf(fp,
222 "\n"
223 "# Please enter the commit message for your changes.\n"
224 "# (Comment lines starting with '#' will not be included)\n");
225 if (only_include_assumed)
226 fprintf(fp, "# %s\n", only_include_assumed);
227
367c9886 228 commitable = run_status(fp, index_file, prefix);
f5bbc322
KH
229
230 fclose(fp);
231
232 return commitable;
233}
234
235/*
236 * Find out if the message starting at position 'start' in the strbuf
237 * contains only whitespace and Signed-off-by lines.
238 */
239static int message_is_empty(struct strbuf *sb, int start)
240{
241 struct strbuf tmpl;
242 const char *nl;
243 int eol, i;
244
245 /* See if the template is just a prefix of the message. */
246 strbuf_init(&tmpl, 0);
247 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
248 stripspace(&tmpl, 1);
249 if (start + tmpl.len <= sb->len &&
250 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
251 start += tmpl.len;
252 }
253 strbuf_release(&tmpl);
254
255 /* Check if the rest is just whitespace and Signed-of-by's. */
256 for (i = start; i < sb->len; i++) {
257 nl = memchr(sb->buf + i, '\n', sb->len - i);
258 if (nl)
259 eol = nl - sb->buf;
260 else
261 eol = sb->len;
262
263 if (strlen(sign_off_header) <= eol - i &&
264 !prefixcmp(sb->buf + i, sign_off_header)) {
265 i = eol;
266 continue;
267 }
268 while (i < eol)
269 if (!isspace(sb->buf[i++]))
270 return 0;
271 }
272
273 return 1;
274}
275
276static void determine_author_info(struct strbuf *sb)
277{
278 char *name, *email, *date;
279
280 name = getenv("GIT_AUTHOR_NAME");
281 email = getenv("GIT_AUTHOR_EMAIL");
282 date = getenv("GIT_AUTHOR_DATE");
283
284 if (use_message) {
285 const char *a, *lb, *rb, *eol;
286
287 a = strstr(use_message_buffer, "\nauthor ");
288 if (!a)
289 die("invalid commit: %s\n", use_message);
290
291 lb = strstr(a + 8, " <");
292 rb = strstr(a + 8, "> ");
293 eol = strchr(a + 8, '\n');
294 if (!lb || !rb || !eol)
295 die("invalid commit: %s\n", use_message);
296
297 name = xstrndup(a + 8, lb - (a + 8));
298 email = xstrndup(lb + 2, rb - (lb + 2));
299 date = xstrndup(rb + 2, eol - (rb + 2));
300 }
301
302 if (force_author) {
303 const char *lb = strstr(force_author, " <");
304 const char *rb = strchr(force_author, '>');
305
306 if (!lb || !rb)
307 die("malformed --author parameter\n");
308 name = xstrndup(force_author, lb - force_author);
309 email = xstrndup(lb + 2, rb - (lb + 2));
310 }
311
312 strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, 1));
313}
314
315static int parse_and_validate_options(int argc, const char *argv[])
316{
317 int f = 0;
318
319 argc = parse_options(argc, argv, builtin_commit_options,
320 builtin_commit_usage, 0);
321
322 if (logfile || message || use_message)
323 no_edit = 1;
324 if (edit_flag)
325 no_edit = 0;
326
327 if (get_sha1("HEAD", head_sha1))
328 initial_commit = 1;
329
330 if (!get_sha1("MERGE_HEAD", merge_head_sha1))
331 in_merge = 1;
332
333 /* Sanity check options */
334 if (amend && initial_commit)
335 die("You have nothing to amend.");
336 if (amend && in_merge)
337 die("You are in the middle of a merger -- cannot amend.");
338
339 if (use_message)
340 f++;
341 if (edit_message)
342 f++;
343 if (logfile)
344 f++;
345 if (f > 1)
346 die("Only one of -c/-C/-F can be used.");
347 if (message && f > 0)
348 die("Option -m cannot be combined with -c/-C/-F.");
349 if (edit_message)
350 use_message = edit_message;
351 if (amend)
352 use_message = "HEAD";
353 if (use_message) {
354 unsigned char sha1[20];
355 static char utf8[] = "UTF-8";
356 const char *out_enc;
357 char *enc, *end;
358 struct commit *commit;
359
360 if (get_sha1(use_message, sha1))
361 die("could not lookup commit %s", use_message);
362 commit = lookup_commit(sha1);
363 if (!commit || parse_commit(commit))
364 die("could not parse commit %s", use_message);
365
366 enc = strstr(commit->buffer, "\nencoding");
367 if (enc) {
368 end = strchr(enc + 10, '\n');
369 enc = xstrndup(enc + 10, end - (enc + 10));
370 } else {
371 enc = utf8;
372 }
373 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
374
375 if (strcmp(out_enc, enc))
376 use_message_buffer =
377 reencode_string(commit->buffer, out_enc, enc);
378
379 /*
380 * If we failed to reencode the buffer, just copy it
381 * byte for byte so the user can try to fix it up.
382 * This also handles the case where input and output
383 * encodings are identical.
384 */
385 if (use_message_buffer == NULL)
386 use_message_buffer = xstrdup(commit->buffer);
387 if (enc != utf8)
388 free(enc);
389 }
390
391 if (!!also + !!only + !!all + !!interactive > 1)
392 die("Only one of --include/--only/--all/--interactive can be used.");
393 if (argc == 0 && (also || (only && !amend)))
394 die("No paths with --include/--only does not make sense.");
395 if (argc == 0 && only && amend)
396 only_include_assumed = "Clever... amending the last one with dirty index.";
397 if (argc > 0 && !also && !only) {
398 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
399 also = 0;
400 }
401
402 if (all && argc > 0)
403 die("Paths with -a does not make sense.");
404 else if (interactive && argc > 0)
405 die("Paths with --interactive does not make sense.");
406
407 return argc;
408}
409
410int cmd_status(int argc, const char **argv, const char *prefix)
411{
412 const char *index_file;
413 int commitable;
414
415 git_config(git_status_config);
416
417 argc = parse_and_validate_options(argc, argv);
418
419 index_file = prepare_index(argv, prefix);
420
367c9886 421 commitable = run_status(stdout, index_file, prefix);
f5bbc322
KH
422
423 rollback_lock_file(&lock_file);
424
425 return commitable ? 0 : 1;
426}
427
428static int run_hook(const char *index_file, const char *name, const char *arg)
429{
430 struct child_process hook;
431 const char *argv[3], *env[2];
432 char index[PATH_MAX];
433
434 argv[0] = git_path("hooks/%s", name);
435 argv[1] = arg;
436 argv[2] = NULL;
437 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
438 env[0] = index;
439 env[1] = NULL;
440
441 if (access(argv[0], X_OK) < 0)
442 return 0;
443
444 memset(&hook, 0, sizeof(hook));
445 hook.argv = argv;
446 hook.no_stdin = 1;
447 hook.stdout_to_stderr = 1;
448 hook.env = env;
449
450 return run_command(&hook);
451}
452
453static void print_summary(const char *prefix, const unsigned char *sha1)
454{
455 struct rev_info rev;
456 struct commit *commit;
457
458 commit = lookup_commit(sha1);
459 if (!commit)
460 die("couldn't look up newly created commit\n");
461 if (!commit || parse_commit(commit))
462 die("could not parse newly created commit");
463
464 init_revisions(&rev, prefix);
465 setup_revisions(0, NULL, &rev, NULL);
466
467 rev.abbrev = 0;
468 rev.diff = 1;
469 rev.diffopt.output_format =
470 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
471
472 rev.verbose_header = 1;
473 rev.show_root_diff = 1;
474 rev.commit_format = get_commit_format("format:%h: %s");
475 rev.always_show_header = 1;
476
477 printf("Created %scommit ", initial_commit ? "initial " : "");
478
479 log_tree_commit(&rev, commit);
480}
481
482int git_commit_config(const char *k, const char *v)
483{
484 if (!strcmp(k, "commit.template")) {
485 template_file = xstrdup(v);
486 return 0;
487 }
488
489 return git_status_config(k, v);
490}
491
492static const char commit_utf8_warn[] =
493"Warning: commit message does not conform to UTF-8.\n"
494"You may want to amend it after fixing the message, or set the config\n"
495"variable i18n.commitencoding to the encoding your project uses.\n";
496
497int cmd_commit(int argc, const char **argv, const char *prefix)
498{
499 int header_len, parent_count = 0;
500 struct strbuf sb;
501 const char *index_file, *reflog_msg;
741707b1 502 char *nl;
f5bbc322
KH
503 unsigned char commit_sha1[20];
504 struct ref_lock *ref_lock;
505
506 git_config(git_commit_config);
507
508 argc = parse_and_validate_options(argc, argv);
509
510 index_file = prepare_index(argv, prefix);
511
512 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
513 exit(1);
514
367c9886
JS
515 if (!prepare_log_message(index_file, prefix) && !in_merge) {
516 run_status(stdout, index_file, prefix);
f5bbc322
KH
517 unlink(commit_editmsg);
518 return 1;
519 }
520
521 strbuf_init(&sb, 0);
522
523 /* Start building up the commit header */
524 read_cache_from(index_file);
525 active_cache_tree = cache_tree();
526 if (cache_tree_update(active_cache_tree,
527 active_cache, active_nr, 0, 0) < 0)
528 die("Error building trees");
529 strbuf_addf(&sb, "tree %s\n",
530 sha1_to_hex(active_cache_tree->sha1));
531
532 /* Determine parents */
533 if (initial_commit) {
534 reflog_msg = "commit (initial)";
535 parent_count = 0;
536 } else if (amend) {
537 struct commit_list *c;
538 struct commit *commit;
539
540 reflog_msg = "commit (amend)";
541 commit = lookup_commit(head_sha1);
542 if (!commit || parse_commit(commit))
543 die("could not parse HEAD commit");
544
545 for (c = commit->parents; c; c = c->next)
546 strbuf_addf(&sb, "parent %s\n",
547 sha1_to_hex(c->item->object.sha1));
548 } else if (in_merge) {
549 struct strbuf m;
550 FILE *fp;
551
552 reflog_msg = "commit (merge)";
553 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
554 strbuf_init(&m, 0);
555 fp = fopen(git_path("MERGE_HEAD"), "r");
556 if (fp == NULL)
557 die("could not open %s for reading: %s",
558 git_path("MERGE_HEAD"), strerror(errno));
559 while (strbuf_getline(&m, fp, '\n') != EOF)
560 strbuf_addf(&sb, "parent %s\n", m.buf);
561 fclose(fp);
562 strbuf_release(&m);
563 } else {
564 reflog_msg = "commit";
565 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
566 }
567
568 determine_author_info(&sb);
569 strbuf_addf(&sb, "committer %s\n", git_committer_info(1));
570 if (!is_encoding_utf8(git_commit_encoding))
571 strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
572 strbuf_addch(&sb, '\n');
573
574 /* Get the commit message and validate it */
575 header_len = sb.len;
576 if (!no_edit) {
577 fprintf(stderr, "launching editor, log %s\n", logfile);
578 launch_editor(git_path(commit_editmsg), &sb);
579 } else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0)
580 die("could not read commit message\n");
581 if (run_hook(index_file, "commit-msg", commit_editmsg))
582 exit(1);
583 stripspace(&sb, 1);
584 if (sb.len < header_len ||
585 message_is_empty(&sb, header_len))
586 die("* no commit message? aborting commit.");
587 strbuf_addch(&sb, '\0');
588 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
589 fprintf(stderr, commit_utf8_warn);
590
591 if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1))
592 die("failed to write commit object");
593
594 ref_lock = lock_any_ref_for_update("HEAD",
595 initial_commit ? NULL : head_sha1,
596 0);
597
598 nl = strchr(sb.buf + header_len, '\n');
741707b1
JS
599 if (nl)
600 strbuf_setlen(&sb, nl + 1 - sb.buf);
601 else
602 strbuf_addch(&sb, '\n');
603 strbuf_remove(&sb, 0, header_len);
604 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
605 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
f5bbc322
KH
606
607 if (!ref_lock)
608 die("cannot lock HEAD ref");
609 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0)
610 die("cannot update HEAD ref");
611
612 unlink(git_path("MERGE_HEAD"));
613 unlink(git_path("MERGE_MSG"));
614
615 if (lock_file.filename[0] && commit_locked_index(&lock_file))
616 die("failed to write new index");
617
618 rerere();
619
620 run_hook(index_file, "post-commit", NULL);
621
622 if (!quiet)
623 print_summary(prefix, commit_sha1);
624
625 return 0;
626}