4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
9 #include "cache-tree.h"
15 #include "wt-status.h"
16 #include "run-command.h"
21 #include "parse-options.h"
23 static const char * const builtin_commit_usage
[] = {
24 "git-commit [options] [--] <filepattern>...",
28 static unsigned char head_sha1
[20], merge_head_sha1
[20];
29 static char *use_message_buffer
;
30 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
31 static struct lock_file lock_file
;
33 static char *logfile
, *force_author
, *message
, *template_file
;
34 static char *edit_message
, *use_message
;
35 static int all
, edit_flag
, also
, interactive
, only
, amend
, signoff
;
36 static int quiet
, verbose
, untracked_files
, no_verify
;
38 static int no_edit
, initial_commit
, in_merge
;
39 const char *only_include_assumed
;
41 static struct option builtin_commit_options
[] = {
43 OPT__VERBOSE(&verbose
),
44 OPT_GROUP("Commit message options"),
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"),
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"),
67 static char *prepare_index(const char **files
, const char *prefix
)
71 struct lock_file
*next_index_lock
;
75 return get_index_file();
78 fd
= hold_locked_index(&lock_file
, 1);
80 die("index file corrupt");
83 add_files_to_cache(verbose
, also
? prefix
: NULL
, files
);
84 refresh_cache(REFRESH_QUIET
);
85 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
86 die("unable to write new_index file");
87 return lock_file
.filename
;
91 /* Commit index as-is. */
92 rollback_lock_file(&lock_file
);
93 return get_index_file();
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");
101 if (!initial_commit
) {
102 tree
= parse_tree_indirect(head_sha1
);
104 die("failed to unpack HEAD tree object");
105 if (read_tree(tree
, 0, NULL
))
106 die("failed to read HEAD tree object");
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
);
114 refresh_cache(REFRESH_QUIET
);
115 if (write_cache(fd
, active_cache
, active_nr
) || close(fd
))
116 die("unable to write new_index file");
118 return next_index_lock
->filename
;
121 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
)
125 wt_status_prepare(&s
);
130 s
.reference
= "HEAD^1";
133 s
.untracked
= untracked_files
;
134 s
.index_file
= index_file
;
142 static const char sign_off_header
[] = "Signed-off-by: ";
144 static int prepare_log_message(const char *index_file
, const char *prefix
)
154 strbuf_add(&sb
, message
, strlen(message
));
155 } else if (logfile
&& !strcmp(logfile
, "-")) {
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
));
181 fp
= fopen(git_path(commit_editmsg
), "w");
183 die("could not open %s\n", git_path(commit_editmsg
));
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"),
196 strbuf_addch(&sob
, '\n');
198 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
200 if (prefixcmp(sb
.buf
+ i
, sob
.buf
))
201 strbuf_addbuf(&sb
, &sob
);
202 strbuf_release(&sob
);
205 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
206 die("could not write commit template: %s\n",
211 if (in_merge
&& !no_edit
)
214 "# It looks like you may be committing a MERGE.\n"
215 "# If this is not correct, please remove the file\n"
219 git_path("MERGE_HEAD"));
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
);
228 commitable
= run_status(fp
, index_file
, prefix
);
236 * Find out if the message starting at position 'start' in the strbuf
237 * contains only whitespace and Signed-off-by lines.
239 static int message_is_empty(struct strbuf
*sb
, int start
)
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)
253 strbuf_release(&tmpl
);
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
);
263 if (strlen(sign_off_header
) <= eol
- i
&&
264 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
269 if (!isspace(sb
->buf
[i
++]))
276 static void determine_author_info(struct strbuf
*sb
)
278 char *name
, *email
, *date
;
280 name
= getenv("GIT_AUTHOR_NAME");
281 email
= getenv("GIT_AUTHOR_EMAIL");
282 date
= getenv("GIT_AUTHOR_DATE");
285 const char *a
, *lb
, *rb
, *eol
;
287 a
= strstr(use_message_buffer
, "\nauthor ");
289 die("invalid commit: %s\n", use_message
);
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
);
297 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
298 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
299 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
303 const char *lb
= strstr(force_author
, " <");
304 const char *rb
= strchr(force_author
, '>');
307 die("malformed --author parameter\n");
308 name
= xstrndup(force_author
, lb
- force_author
);
309 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
312 strbuf_addf(sb
, "author %s\n", fmt_ident(name
, email
, date
, 1));
315 static int parse_and_validate_options(int argc
, const char *argv
[])
319 argc
= parse_options(argc
, argv
, builtin_commit_options
,
320 builtin_commit_usage
, 0);
322 if (logfile
|| message
|| use_message
)
327 if (get_sha1("HEAD", head_sha1
))
330 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
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.");
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.");
350 use_message
= edit_message
;
352 use_message
= "HEAD";
354 unsigned char sha1
[20];
355 static char utf8
[] = "UTF-8";
358 struct commit
*commit
;
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
);
366 enc
= strstr(commit
->buffer
, "\nencoding");
368 end
= strchr(enc
+ 10, '\n');
369 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
373 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
375 if (strcmp(out_enc
, enc
))
377 reencode_string(commit
->buffer
, out_enc
, enc
);
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.
385 if (use_message_buffer
== NULL
)
386 use_message_buffer
= xstrdup(commit
->buffer
);
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...";
403 die("Paths with -a does not make sense.");
404 else if (interactive
&& argc
> 0)
405 die("Paths with --interactive does not make sense.");
410 int cmd_status(int argc
, const char **argv
, const char *prefix
)
412 const char *index_file
;
415 git_config(git_status_config
);
417 argc
= parse_and_validate_options(argc
, argv
);
419 index_file
= prepare_index(argv
, prefix
);
421 commitable
= run_status(stdout
, index_file
, prefix
);
423 rollback_lock_file(&lock_file
);
425 return commitable
? 0 : 1;
428 static int run_hook(const char *index_file
, const char *name
, const char *arg
)
430 struct child_process hook
;
431 const char *argv
[3], *env
[2];
432 char index
[PATH_MAX
];
434 argv
[0] = git_path("hooks/%s", name
);
437 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
441 if (access(argv
[0], X_OK
) < 0)
444 memset(&hook
, 0, sizeof(hook
));
447 hook
.stdout_to_stderr
= 1;
450 return run_command(&hook
);
453 static void print_summary(const char *prefix
, const unsigned char *sha1
)
456 struct commit
*commit
;
458 commit
= lookup_commit(sha1
);
460 die("couldn't look up newly created commit\n");
461 if (!commit
|| parse_commit(commit
))
462 die("could not parse newly created commit");
464 init_revisions(&rev
, prefix
);
465 setup_revisions(0, NULL
, &rev
, NULL
);
469 rev
.diffopt
.output_format
=
470 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
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;
477 printf("Created %scommit ", initial_commit
? "initial " : "");
479 log_tree_commit(&rev
, commit
);
482 int git_commit_config(const char *k
, const char *v
)
484 if (!strcmp(k
, "commit.template")) {
485 template_file
= xstrdup(v
);
489 return git_status_config(k
, v
);
492 static 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";
497 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
499 int header_len
, parent_count
= 0;
501 const char *index_file
, *reflog_msg
;
503 unsigned char commit_sha1
[20];
504 struct ref_lock
*ref_lock
;
506 git_config(git_commit_config
);
508 argc
= parse_and_validate_options(argc
, argv
);
510 index_file
= prepare_index(argv
, prefix
);
512 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
515 if (!prepare_log_message(index_file
, prefix
) && !in_merge
) {
516 run_status(stdout
, index_file
, prefix
);
517 unlink(commit_editmsg
);
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
));
532 /* Determine parents */
533 if (initial_commit
) {
534 reflog_msg
= "commit (initial)";
537 struct commit_list
*c
;
538 struct commit
*commit
;
540 reflog_msg
= "commit (amend)";
541 commit
= lookup_commit(head_sha1
);
542 if (!commit
|| parse_commit(commit
))
543 die("could not parse HEAD commit");
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
) {
552 reflog_msg
= "commit (merge)";
553 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
555 fp
= fopen(git_path("MERGE_HEAD"), "r");
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
);
564 reflog_msg
= "commit";
565 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
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');
574 /* Get the commit message and validate it */
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
))
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
);
591 if (write_sha1_file(sb
.buf
, sb
.len
- 1, commit_type
, commit_sha1
))
592 die("failed to write commit object");
594 ref_lock
= lock_any_ref_for_update("HEAD",
595 initial_commit
? NULL
: head_sha1
,
598 nl
= strchr(sb
.buf
+ header_len
, '\n');
600 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
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);
608 die("cannot lock HEAD ref");
609 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0)
610 die("cannot update HEAD ref");
612 unlink(git_path("MERGE_HEAD"));
613 unlink(git_path("MERGE_MSG"));
615 if (lock_file
.filename
[0] && commit_locked_index(&lock_file
))
616 die("failed to write new index");
620 run_hook(index_file
, "post-commit", NULL
);
623 print_summary(prefix
, commit_sha1
);