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 if (prefixcmp(sb
.buf
+ i
, sign_off_header
))
202 strbuf_addch(&sb
, '\n');
203 strbuf_addbuf(&sb
, &sob
);
205 strbuf_release(&sob
);
208 if (fwrite(sb
.buf
, 1, sb
.len
, fp
) < sb
.len
)
209 die("could not write commit template: %s\n",
214 if (in_merge
&& !no_edit
)
217 "# It looks like you may be committing a MERGE.\n"
218 "# If this is not correct, please remove the file\n"
222 git_path("MERGE_HEAD"));
226 "# Please enter the commit message for your changes.\n"
227 "# (Comment lines starting with '#' will not be included)\n");
228 if (only_include_assumed
)
229 fprintf(fp
, "# %s\n", only_include_assumed
);
231 commitable
= run_status(fp
, index_file
, prefix
);
239 * Find out if the message starting at position 'start' in the strbuf
240 * contains only whitespace and Signed-off-by lines.
242 static int message_is_empty(struct strbuf
*sb
, int start
)
248 /* See if the template is just a prefix of the message. */
249 strbuf_init(&tmpl
, 0);
250 if (template_file
&& strbuf_read_file(&tmpl
, template_file
, 0) > 0) {
251 stripspace(&tmpl
, 1);
252 if (start
+ tmpl
.len
<= sb
->len
&&
253 memcmp(tmpl
.buf
, sb
->buf
+ start
, tmpl
.len
) == 0)
256 strbuf_release(&tmpl
);
258 /* Check if the rest is just whitespace and Signed-of-by's. */
259 for (i
= start
; i
< sb
->len
; i
++) {
260 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
266 if (strlen(sign_off_header
) <= eol
- i
&&
267 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
272 if (!isspace(sb
->buf
[i
++]))
279 static void determine_author_info(struct strbuf
*sb
)
281 char *name
, *email
, *date
;
283 name
= getenv("GIT_AUTHOR_NAME");
284 email
= getenv("GIT_AUTHOR_EMAIL");
285 date
= getenv("GIT_AUTHOR_DATE");
288 const char *a
, *lb
, *rb
, *eol
;
290 a
= strstr(use_message_buffer
, "\nauthor ");
292 die("invalid commit: %s\n", use_message
);
294 lb
= strstr(a
+ 8, " <");
295 rb
= strstr(a
+ 8, "> ");
296 eol
= strchr(a
+ 8, '\n');
297 if (!lb
|| !rb
|| !eol
)
298 die("invalid commit: %s\n", use_message
);
300 name
= xstrndup(a
+ 8, lb
- (a
+ 8));
301 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
302 date
= xstrndup(rb
+ 2, eol
- (rb
+ 2));
306 const char *lb
= strstr(force_author
, " <");
307 const char *rb
= strchr(force_author
, '>');
310 die("malformed --author parameter\n");
311 name
= xstrndup(force_author
, lb
- force_author
);
312 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
315 strbuf_addf(sb
, "author %s\n", fmt_ident(name
, email
, date
, 1));
318 static int parse_and_validate_options(int argc
, const char *argv
[])
322 argc
= parse_options(argc
, argv
, builtin_commit_options
,
323 builtin_commit_usage
, 0);
325 if (logfile
|| message
|| use_message
)
330 if (get_sha1("HEAD", head_sha1
))
333 if (!get_sha1("MERGE_HEAD", merge_head_sha1
))
336 /* Sanity check options */
337 if (amend
&& initial_commit
)
338 die("You have nothing to amend.");
339 if (amend
&& in_merge
)
340 die("You are in the middle of a merger -- cannot amend.");
349 die("Only one of -c/-C/-F can be used.");
350 if (message
&& f
> 0)
351 die("Option -m cannot be combined with -c/-C/-F.");
353 use_message
= edit_message
;
355 use_message
= "HEAD";
357 unsigned char sha1
[20];
358 static char utf8
[] = "UTF-8";
361 struct commit
*commit
;
363 if (get_sha1(use_message
, sha1
))
364 die("could not lookup commit %s", use_message
);
365 commit
= lookup_commit(sha1
);
366 if (!commit
|| parse_commit(commit
))
367 die("could not parse commit %s", use_message
);
369 enc
= strstr(commit
->buffer
, "\nencoding");
371 end
= strchr(enc
+ 10, '\n');
372 enc
= xstrndup(enc
+ 10, end
- (enc
+ 10));
376 out_enc
= git_commit_encoding
? git_commit_encoding
: utf8
;
378 if (strcmp(out_enc
, enc
))
380 reencode_string(commit
->buffer
, out_enc
, enc
);
383 * If we failed to reencode the buffer, just copy it
384 * byte for byte so the user can try to fix it up.
385 * This also handles the case where input and output
386 * encodings are identical.
388 if (use_message_buffer
== NULL
)
389 use_message_buffer
= xstrdup(commit
->buffer
);
394 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
395 die("Only one of --include/--only/--all/--interactive can be used.");
396 if (argc
== 0 && (also
|| (only
&& !amend
)))
397 die("No paths with --include/--only does not make sense.");
398 if (argc
== 0 && only
&& amend
)
399 only_include_assumed
= "Clever... amending the last one with dirty index.";
400 if (argc
> 0 && !also
&& !only
) {
401 only_include_assumed
= "Explicit paths specified without -i nor -o; assuming --only paths...";
406 die("Paths with -a does not make sense.");
407 else if (interactive
&& argc
> 0)
408 die("Paths with --interactive does not make sense.");
413 int cmd_status(int argc
, const char **argv
, const char *prefix
)
415 const char *index_file
;
418 git_config(git_status_config
);
420 argc
= parse_and_validate_options(argc
, argv
);
422 index_file
= prepare_index(argv
, prefix
);
424 commitable
= run_status(stdout
, index_file
, prefix
);
426 rollback_lock_file(&lock_file
);
428 return commitable
? 0 : 1;
431 static int run_hook(const char *index_file
, const char *name
, const char *arg
)
433 struct child_process hook
;
434 const char *argv
[3], *env
[2];
435 char index
[PATH_MAX
];
437 argv
[0] = git_path("hooks/%s", name
);
440 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
444 if (access(argv
[0], X_OK
) < 0)
447 memset(&hook
, 0, sizeof(hook
));
450 hook
.stdout_to_stderr
= 1;
453 return run_command(&hook
);
456 static void print_summary(const char *prefix
, const unsigned char *sha1
)
459 struct commit
*commit
;
461 commit
= lookup_commit(sha1
);
463 die("couldn't look up newly created commit\n");
464 if (!commit
|| parse_commit(commit
))
465 die("could not parse newly created commit");
467 init_revisions(&rev
, prefix
);
468 setup_revisions(0, NULL
, &rev
, NULL
);
472 rev
.diffopt
.output_format
=
473 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
475 rev
.verbose_header
= 1;
476 rev
.show_root_diff
= 1;
477 rev
.commit_format
= get_commit_format("format:%h: %s");
478 rev
.always_show_header
= 1;
480 printf("Created %scommit ", initial_commit
? "initial " : "");
482 log_tree_commit(&rev
, commit
);
485 int git_commit_config(const char *k
, const char *v
)
487 if (!strcmp(k
, "commit.template")) {
488 template_file
= xstrdup(v
);
492 return git_status_config(k
, v
);
495 static const char commit_utf8_warn
[] =
496 "Warning: commit message does not conform to UTF-8.\n"
497 "You may want to amend it after fixing the message, or set the config\n"
498 "variable i18n.commitencoding to the encoding your project uses.\n";
500 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
502 int header_len
, parent_count
= 0;
504 const char *index_file
, *reflog_msg
;
506 unsigned char commit_sha1
[20];
507 struct ref_lock
*ref_lock
;
509 git_config(git_commit_config
);
511 argc
= parse_and_validate_options(argc
, argv
);
513 index_file
= prepare_index(argv
, prefix
);
515 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
518 if (!prepare_log_message(index_file
, prefix
) && !in_merge
) {
519 run_status(stdout
, index_file
, prefix
);
520 unlink(commit_editmsg
);
526 /* Start building up the commit header */
527 read_cache_from(index_file
);
528 active_cache_tree
= cache_tree();
529 if (cache_tree_update(active_cache_tree
,
530 active_cache
, active_nr
, 0, 0) < 0)
531 die("Error building trees");
532 strbuf_addf(&sb
, "tree %s\n",
533 sha1_to_hex(active_cache_tree
->sha1
));
535 /* Determine parents */
536 if (initial_commit
) {
537 reflog_msg
= "commit (initial)";
540 struct commit_list
*c
;
541 struct commit
*commit
;
543 reflog_msg
= "commit (amend)";
544 commit
= lookup_commit(head_sha1
);
545 if (!commit
|| parse_commit(commit
))
546 die("could not parse HEAD commit");
548 for (c
= commit
->parents
; c
; c
= c
->next
)
549 strbuf_addf(&sb
, "parent %s\n",
550 sha1_to_hex(c
->item
->object
.sha1
));
551 } else if (in_merge
) {
555 reflog_msg
= "commit (merge)";
556 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
558 fp
= fopen(git_path("MERGE_HEAD"), "r");
560 die("could not open %s for reading: %s",
561 git_path("MERGE_HEAD"), strerror(errno
));
562 while (strbuf_getline(&m
, fp
, '\n') != EOF
)
563 strbuf_addf(&sb
, "parent %s\n", m
.buf
);
567 reflog_msg
= "commit";
568 strbuf_addf(&sb
, "parent %s\n", sha1_to_hex(head_sha1
));
571 determine_author_info(&sb
);
572 strbuf_addf(&sb
, "committer %s\n", git_committer_info(1));
573 if (!is_encoding_utf8(git_commit_encoding
))
574 strbuf_addf(&sb
, "encoding %s\n", git_commit_encoding
);
575 strbuf_addch(&sb
, '\n');
577 /* Get the commit message and validate it */
580 fprintf(stderr
, "launching editor, log %s\n", logfile
);
581 launch_editor(git_path(commit_editmsg
), &sb
);
582 } else if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0)
583 die("could not read commit message\n");
584 if (run_hook(index_file
, "commit-msg", commit_editmsg
))
587 if (sb
.len
< header_len
||
588 message_is_empty(&sb
, header_len
))
589 die("* no commit message? aborting commit.");
590 strbuf_addch(&sb
, '\0');
591 if (is_encoding_utf8(git_commit_encoding
) && !is_utf8(sb
.buf
))
592 fprintf(stderr
, commit_utf8_warn
);
594 if (write_sha1_file(sb
.buf
, sb
.len
- 1, commit_type
, commit_sha1
))
595 die("failed to write commit object");
597 ref_lock
= lock_any_ref_for_update("HEAD",
598 initial_commit
? NULL
: head_sha1
,
601 nl
= strchr(sb
.buf
+ header_len
, '\n');
603 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
605 strbuf_addch(&sb
, '\n');
606 strbuf_remove(&sb
, 0, header_len
);
607 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
608 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
611 die("cannot lock HEAD ref");
612 if (write_ref_sha1(ref_lock
, commit_sha1
, sb
.buf
) < 0)
613 die("cannot update HEAD ref");
615 unlink(git_path("MERGE_HEAD"));
616 unlink(git_path("MERGE_MSG"));
618 if (lock_file
.filename
[0] && commit_locked_index(&lock_file
))
619 die("failed to write new index");
623 run_hook(index_file
, "post-commit", NULL
);
626 print_summary(prefix
, commit_sha1
);