]>
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" | |
2888605c | 24 | #include "path-list.h" |
fa9dcf80 | 25 | #include "unpack-trees.h" |
f5bbc322 KH |
26 | |
27 | static const char * const builtin_commit_usage[] = { | |
28 | "git-commit [options] [--] <filepattern>...", | |
29 | NULL | |
30 | }; | |
31 | ||
2f02b25f SB |
32 | static const char * const builtin_status_usage[] = { |
33 | "git-status [options] [--] <filepattern>...", | |
34 | NULL | |
35 | }; | |
36 | ||
f5bbc322 KH |
37 | static unsigned char head_sha1[20], merge_head_sha1[20]; |
38 | static char *use_message_buffer; | |
39 | static const char commit_editmsg[] = "COMMIT_EDITMSG"; | |
2888605c JH |
40 | static struct lock_file index_lock; /* real index */ |
41 | static struct lock_file false_lock; /* used only for partial commits */ | |
42 | static enum { | |
43 | COMMIT_AS_IS = 1, | |
44 | COMMIT_NORMAL, | |
45 | COMMIT_PARTIAL, | |
46 | } commit_style; | |
f5bbc322 | 47 | |
f9568530 | 48 | static char *logfile, *force_author, *template_file; |
f5bbc322 | 49 | static char *edit_message, *use_message; |
e83dbe80 | 50 | static char *author_name, *author_email, *author_date; |
f5bbc322 | 51 | static int all, edit_flag, also, interactive, only, amend, signoff; |
4bfee30a MSO |
52 | static int quiet, verbose, no_verify, allow_empty; |
53 | static char *untracked_files_arg; | |
5f065737 AR |
54 | /* |
55 | * The default commit message cleanup mode will remove the lines | |
56 | * beginning with # (shell comments) and leading and trailing | |
57 | * whitespaces (empty lines or containing only whitespaces) | |
58 | * if editor is used, and only the whitespaces if the message | |
59 | * is specified explicitly. | |
60 | */ | |
61 | static enum { | |
62 | CLEANUP_SPACE, | |
63 | CLEANUP_NONE, | |
64 | CLEANUP_ALL, | |
65 | } cleanup_mode; | |
66 | static char *cleanup_arg; | |
f5bbc322 | 67 | |
4803466f | 68 | static int use_editor = 1, initial_commit, in_merge; |
f5bbc322 | 69 | const char *only_include_assumed; |
f9568530 JS |
70 | struct strbuf message; |
71 | ||
72 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) | |
73 | { | |
74 | struct strbuf *buf = opt->value; | |
75 | if (unset) | |
76 | strbuf_setlen(buf, 0); | |
77 | else { | |
78 | strbuf_addstr(buf, arg); | |
79 | strbuf_addch(buf, '\n'); | |
80 | strbuf_addch(buf, '\n'); | |
81 | } | |
82 | return 0; | |
83 | } | |
f5bbc322 KH |
84 | |
85 | static struct option builtin_commit_options[] = { | |
86 | OPT__QUIET(&quiet), | |
87 | OPT__VERBOSE(&verbose), | |
88 | OPT_GROUP("Commit message options"), | |
89 | ||
90 | OPT_STRING('F', "file", &logfile, "FILE", "read log from file"), | |
91 | OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"), | |
f9568530 | 92 | OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m), |
f5bbc322 KH |
93 | OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "), |
94 | OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"), | |
362b0dd5 | 95 | OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), |
f5bbc322 KH |
96 | OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"), |
97 | OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"), | |
98 | ||
99 | OPT_GROUP("Commit contents options"), | |
100 | OPT_BOOLEAN('a', "all", &all, "commit all changed files"), | |
101 | OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"), | |
102 | OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"), | |
d4ba07ca | 103 | OPT_BOOLEAN('o', "only", &only, "commit only specified files"), |
f5bbc322 KH |
104 | OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"), |
105 | OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"), | |
6c2ce048 | 106 | { 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 | 107 | OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"), |
5f065737 | 108 | OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"), |
f5bbc322 KH |
109 | |
110 | OPT_END() | |
111 | }; | |
112 | ||
2888605c JH |
113 | static void rollback_index_files(void) |
114 | { | |
115 | switch (commit_style) { | |
116 | case COMMIT_AS_IS: | |
117 | break; /* nothing to do */ | |
118 | case COMMIT_NORMAL: | |
119 | rollback_lock_file(&index_lock); | |
120 | break; | |
121 | case COMMIT_PARTIAL: | |
122 | rollback_lock_file(&index_lock); | |
123 | rollback_lock_file(&false_lock); | |
124 | break; | |
125 | } | |
126 | } | |
127 | ||
5a9dd399 | 128 | static int commit_index_files(void) |
2888605c | 129 | { |
5a9dd399 BC |
130 | int err = 0; |
131 | ||
2888605c JH |
132 | switch (commit_style) { |
133 | case COMMIT_AS_IS: | |
134 | break; /* nothing to do */ | |
135 | case COMMIT_NORMAL: | |
5a9dd399 | 136 | err = commit_lock_file(&index_lock); |
2888605c JH |
137 | break; |
138 | case COMMIT_PARTIAL: | |
5a9dd399 | 139 | err = commit_lock_file(&index_lock); |
2888605c JH |
140 | rollback_lock_file(&false_lock); |
141 | break; | |
142 | } | |
5a9dd399 BC |
143 | |
144 | return err; | |
2888605c JH |
145 | } |
146 | ||
147 | /* | |
148 | * Take a union of paths in the index and the named tree (typically, "HEAD"), | |
149 | * and return the paths that match the given pattern in list. | |
150 | */ | |
151 | static int list_paths(struct path_list *list, const char *with_tree, | |
152 | const char *prefix, const char **pattern) | |
153 | { | |
154 | int i; | |
155 | char *m; | |
156 | ||
157 | for (i = 0; pattern[i]; i++) | |
158 | ; | |
159 | m = xcalloc(1, i); | |
160 | ||
161 | if (with_tree) | |
162 | overlay_tree_on_cache(with_tree, prefix); | |
163 | ||
164 | for (i = 0; i < active_nr; i++) { | |
165 | struct cache_entry *ce = active_cache[i]; | |
7a51ed66 | 166 | if (ce->ce_flags & CE_UPDATE) |
e87e22d0 | 167 | continue; |
2888605c JH |
168 | if (!pathspec_match(pattern, m, ce->name, 0)) |
169 | continue; | |
170 | path_list_insert(ce->name, list); | |
171 | } | |
172 | ||
173 | return report_path_error(m, pattern, prefix ? strlen(prefix) : 0); | |
174 | } | |
175 | ||
176 | static void add_remove_files(struct path_list *list) | |
177 | { | |
178 | int i; | |
179 | for (i = 0; i < list->nr; i++) { | |
d177cab0 | 180 | struct stat st; |
2888605c | 181 | struct path_list_item *p = &(list->items[i]); |
d177cab0 | 182 | |
960b8ad1 AR |
183 | if (!lstat(p->path, &st)) { |
184 | if (add_to_cache(p->path, &st, 0)) | |
185 | die("updating files failed"); | |
186 | } else | |
2888605c JH |
187 | remove_file_from_cache(p->path); |
188 | } | |
189 | } | |
190 | ||
fa9dcf80 LT |
191 | static void create_base_index(void) |
192 | { | |
193 | struct tree *tree; | |
194 | struct unpack_trees_options opts; | |
195 | struct tree_desc t; | |
196 | ||
197 | if (initial_commit) { | |
198 | discard_cache(); | |
199 | return; | |
200 | } | |
201 | ||
202 | memset(&opts, 0, sizeof(opts)); | |
203 | opts.head_idx = 1; | |
204 | opts.index_only = 1; | |
205 | opts.merge = 1; | |
34110cd4 LT |
206 | opts.src_index = &the_index; |
207 | opts.dst_index = &the_index; | |
fa9dcf80 LT |
208 | |
209 | opts.fn = oneway_merge; | |
210 | tree = parse_tree_indirect(head_sha1); | |
211 | if (!tree) | |
212 | die("failed to unpack HEAD tree object"); | |
213 | parse_tree(tree); | |
214 | init_tree_desc(&t, tree->buffer, tree->size); | |
203a2fe1 DB |
215 | if (unpack_trees(1, &t, &opts)) |
216 | exit(128); /* We've already reported the error, finish dying */ | |
fa9dcf80 LT |
217 | } |
218 | ||
f64fe7b4 | 219 | static char *prepare_index(int argc, const char **argv, const char *prefix) |
f5bbc322 KH |
220 | { |
221 | int fd; | |
2888605c JH |
222 | struct path_list partial; |
223 | const char **pathspec = NULL; | |
f5bbc322 KH |
224 | |
225 | if (interactive) { | |
3f061887 | 226 | interactive_add(argc, argv, prefix); |
d5350fd2 GP |
227 | if (read_cache() < 0) |
228 | die("index file corrupt"); | |
2888605c | 229 | commit_style = COMMIT_AS_IS; |
f5bbc322 KH |
230 | return get_index_file(); |
231 | } | |
232 | ||
f5bbc322 KH |
233 | if (read_cache() < 0) |
234 | die("index file corrupt"); | |
235 | ||
f64fe7b4 JH |
236 | if (*argv) |
237 | pathspec = get_pathspec(prefix, argv); | |
2888605c JH |
238 | |
239 | /* | |
240 | * Non partial, non as-is commit. | |
241 | * | |
242 | * (1) get the real index; | |
243 | * (2) update the_index as necessary; | |
244 | * (3) write the_index out to the real index (still locked); | |
245 | * (4) return the name of the locked index file. | |
246 | * | |
247 | * The caller should run hooks on the locked real index, and | |
248 | * (A) if all goes well, commit the real index; | |
249 | * (B) on failure, rollback the real index. | |
250 | */ | |
251 | if (all || (also && pathspec && *pathspec)) { | |
252 | int fd = hold_locked_index(&index_lock, 1); | |
7ae02a30 | 253 | add_files_to_cache(also ? prefix : NULL, pathspec, 0); |
d37d3203 | 254 | refresh_cache(REFRESH_QUIET); |
4ed7cd3a BC |
255 | if (write_cache(fd, active_cache, active_nr) || |
256 | close_lock_file(&index_lock)) | |
f5bbc322 | 257 | die("unable to write new_index file"); |
2888605c JH |
258 | commit_style = COMMIT_NORMAL; |
259 | return index_lock.filename; | |
f5bbc322 KH |
260 | } |
261 | ||
2888605c JH |
262 | /* |
263 | * As-is commit. | |
264 | * | |
265 | * (1) return the name of the real index file. | |
266 | * | |
267 | * The caller should run hooks on the real index, and run | |
268 | * hooks on the real index, and create commit from the_index. | |
269 | * We still need to refresh the index here. | |
270 | */ | |
271 | if (!pathspec || !*pathspec) { | |
272 | fd = hold_locked_index(&index_lock, 1); | |
273 | refresh_cache(REFRESH_QUIET); | |
274 | if (write_cache(fd, active_cache, active_nr) || | |
4439751d | 275 | commit_locked_index(&index_lock)) |
2888605c JH |
276 | die("unable to write new_index file"); |
277 | commit_style = COMMIT_AS_IS; | |
f5bbc322 KH |
278 | return get_index_file(); |
279 | } | |
280 | ||
2888605c JH |
281 | /* |
282 | * A partial commit. | |
283 | * | |
284 | * (0) find the set of affected paths; | |
285 | * (1) get lock on the real index file; | |
286 | * (2) update the_index with the given paths; | |
287 | * (3) write the_index out to the real index (still locked); | |
288 | * (4) get lock on the false index file; | |
289 | * (5) reset the_index from HEAD; | |
290 | * (6) update the_index the same way as (2); | |
291 | * (7) write the_index out to the false index file; | |
292 | * (8) return the name of the false index file (still locked); | |
293 | * | |
294 | * The caller should run hooks on the locked false index, and | |
295 | * create commit from it. Then | |
296 | * (A) if all goes well, commit the real index; | |
297 | * (B) on failure, rollback the real index; | |
298 | * In either case, rollback the false index. | |
299 | */ | |
300 | commit_style = COMMIT_PARTIAL; | |
301 | ||
302 | if (file_exists(git_path("MERGE_HEAD"))) | |
303 | die("cannot do a partial commit during a merge."); | |
304 | ||
305 | memset(&partial, 0, sizeof(partial)); | |
306 | partial.strdup_paths = 1; | |
307 | if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec)) | |
308 | exit(1); | |
309 | ||
310 | discard_cache(); | |
311 | if (read_cache() < 0) | |
312 | die("cannot read the index"); | |
313 | ||
314 | fd = hold_locked_index(&index_lock, 1); | |
315 | add_remove_files(&partial); | |
ef12b50d | 316 | refresh_cache(REFRESH_QUIET); |
4ed7cd3a BC |
317 | if (write_cache(fd, active_cache, active_nr) || |
318 | close_lock_file(&index_lock)) | |
f5bbc322 KH |
319 | die("unable to write new_index file"); |
320 | ||
2888605c JH |
321 | fd = hold_lock_file_for_update(&false_lock, |
322 | git_path("next-index-%d", getpid()), 1); | |
fa9dcf80 LT |
323 | |
324 | create_base_index(); | |
2888605c | 325 | add_remove_files(&partial); |
d37d3203 | 326 | refresh_cache(REFRESH_QUIET); |
f5bbc322 | 327 | |
4ed7cd3a BC |
328 | if (write_cache(fd, active_cache, active_nr) || |
329 | close_lock_file(&false_lock)) | |
2888605c | 330 | die("unable to write temporary index file"); |
959ba670 JK |
331 | |
332 | discard_cache(); | |
333 | read_cache_from(false_lock.filename); | |
334 | ||
2888605c | 335 | return false_lock.filename; |
f5bbc322 KH |
336 | } |
337 | ||
37d07f8f | 338 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn) |
f5bbc322 KH |
339 | { |
340 | struct wt_status s; | |
341 | ||
342 | wt_status_prepare(&s); | |
46f721c8 JK |
343 | if (wt_status_relative_paths) |
344 | s.prefix = prefix; | |
f5bbc322 KH |
345 | |
346 | if (amend) { | |
347 | s.amend = 1; | |
348 | s.reference = "HEAD^1"; | |
349 | } | |
350 | s.verbose = verbose; | |
4bfee30a | 351 | s.untracked = (show_untracked_files == SHOW_ALL_UNTRACKED_FILES); |
f5bbc322 KH |
352 | s.index_file = index_file; |
353 | s.fp = fp; | |
37d07f8f | 354 | s.nowarn = nowarn; |
f5bbc322 KH |
355 | |
356 | wt_status_print(&s); | |
357 | ||
358 | return s.commitable; | |
359 | } | |
360 | ||
3473f303 PB |
361 | static int run_hook(const char *index_file, const char *name, ...) |
362 | { | |
363 | struct child_process hook; | |
364 | const char *argv[10], *env[2]; | |
365 | char index[PATH_MAX]; | |
366 | va_list args; | |
367 | int i; | |
368 | ||
369 | va_start(args, name); | |
370 | argv[0] = git_path("hooks/%s", name); | |
371 | i = 0; | |
372 | do { | |
373 | if (++i >= ARRAY_SIZE(argv)) | |
374 | die ("run_hook(): too many arguments"); | |
375 | argv[i] = va_arg(args, const char *); | |
376 | } while (argv[i]); | |
377 | va_end(args); | |
378 | ||
379 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); | |
380 | env[0] = index; | |
381 | env[1] = NULL; | |
382 | ||
383 | if (access(argv[0], X_OK) < 0) | |
384 | return 0; | |
385 | ||
386 | memset(&hook, 0, sizeof(hook)); | |
387 | hook.argv = argv; | |
388 | hook.no_stdin = 1; | |
389 | hook.stdout_to_stderr = 1; | |
390 | hook.env = env; | |
391 | ||
392 | return run_command(&hook); | |
393 | } | |
394 | ||
ec84bd00 PB |
395 | static int is_a_merge(const unsigned char *sha1) |
396 | { | |
397 | struct commit *commit = lookup_commit(sha1); | |
398 | if (!commit || parse_commit(commit)) | |
399 | die("could not parse HEAD commit"); | |
400 | return !!(commit->parents && commit->parents->next); | |
401 | } | |
402 | ||
f5bbc322 KH |
403 | static const char sign_off_header[] = "Signed-off-by: "; |
404 | ||
e83dbe80 | 405 | static void determine_author_info(void) |
a45d46ba SB |
406 | { |
407 | char *name, *email, *date; | |
408 | ||
409 | name = getenv("GIT_AUTHOR_NAME"); | |
410 | email = getenv("GIT_AUTHOR_EMAIL"); | |
411 | date = getenv("GIT_AUTHOR_DATE"); | |
412 | ||
413 | if (use_message) { | |
414 | const char *a, *lb, *rb, *eol; | |
415 | ||
416 | a = strstr(use_message_buffer, "\nauthor "); | |
417 | if (!a) | |
418 | die("invalid commit: %s", use_message); | |
419 | ||
420 | lb = strstr(a + 8, " <"); | |
421 | rb = strstr(a + 8, "> "); | |
422 | eol = strchr(a + 8, '\n'); | |
423 | if (!lb || !rb || !eol) | |
424 | die("invalid commit: %s", use_message); | |
425 | ||
426 | name = xstrndup(a + 8, lb - (a + 8)); | |
427 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
428 | date = xstrndup(rb + 2, eol - (rb + 2)); | |
429 | } | |
430 | ||
431 | if (force_author) { | |
432 | const char *lb = strstr(force_author, " <"); | |
433 | const char *rb = strchr(force_author, '>'); | |
434 | ||
435 | if (!lb || !rb) | |
436 | die("malformed --author parameter"); | |
437 | name = xstrndup(force_author, lb - force_author); | |
438 | email = xstrndup(lb + 2, rb - (lb + 2)); | |
439 | } | |
440 | ||
e83dbe80 SB |
441 | author_name = name; |
442 | author_email = email; | |
443 | author_date = date; | |
a45d46ba SB |
444 | } |
445 | ||
ec84bd00 | 446 | static int prepare_to_commit(const char *index_file, const char *prefix) |
f5bbc322 KH |
447 | { |
448 | struct stat statbuf; | |
bc5d248a | 449 | int commitable, saved_color_setting; |
f5bbc322 KH |
450 | struct strbuf sb; |
451 | char *buffer; | |
452 | FILE *fp; | |
8089c85b PB |
453 | const char *hook_arg1 = NULL; |
454 | const char *hook_arg2 = NULL; | |
bb1ae3f6 | 455 | int ident_shown = 0; |
f5bbc322 | 456 | |
ec84bd00 PB |
457 | if (!no_verify && run_hook(index_file, "pre-commit", NULL)) |
458 | return 0; | |
f5bbc322 KH |
459 | |
460 | strbuf_init(&sb, 0); | |
f9568530 JS |
461 | if (message.len) { |
462 | strbuf_addbuf(&sb, &message); | |
8089c85b | 463 | hook_arg1 = "message"; |
f5bbc322 KH |
464 | } else if (logfile && !strcmp(logfile, "-")) { |
465 | if (isatty(0)) | |
466 | fprintf(stderr, "(reading log message from standard input)\n"); | |
467 | if (strbuf_read(&sb, 0, 0) < 0) | |
468 | die("could not read log from standard input"); | |
8089c85b | 469 | hook_arg1 = "message"; |
f5bbc322 KH |
470 | } else if (logfile) { |
471 | if (strbuf_read_file(&sb, logfile, 0) < 0) | |
472 | die("could not read log file '%s': %s", | |
473 | logfile, strerror(errno)); | |
8089c85b | 474 | hook_arg1 = "message"; |
f5bbc322 KH |
475 | } else if (use_message) { |
476 | buffer = strstr(use_message_buffer, "\n\n"); | |
477 | if (!buffer || buffer[2] == '\0') | |
478 | die("commit has empty message"); | |
479 | strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); | |
8089c85b PB |
480 | hook_arg1 = "commit"; |
481 | hook_arg2 = use_message; | |
f5bbc322 KH |
482 | } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { |
483 | if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) | |
484 | die("could not read MERGE_MSG: %s", strerror(errno)); | |
8089c85b | 485 | hook_arg1 = "merge"; |
f5bbc322 KH |
486 | } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { |
487 | if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) | |
488 | die("could not read SQUASH_MSG: %s", strerror(errno)); | |
8089c85b | 489 | hook_arg1 = "squash"; |
f5bbc322 KH |
490 | } else if (template_file && !stat(template_file, &statbuf)) { |
491 | if (strbuf_read_file(&sb, template_file, 0) < 0) | |
492 | die("could not read %s: %s", | |
493 | template_file, strerror(errno)); | |
8089c85b | 494 | hook_arg1 = "template"; |
f5bbc322 KH |
495 | } |
496 | ||
8089c85b PB |
497 | /* |
498 | * This final case does not modify the template message, | |
499 | * it just sets the argument to the prepare-commit-msg hook. | |
500 | */ | |
501 | else if (in_merge) | |
502 | hook_arg1 = "merge"; | |
503 | ||
f5bbc322 KH |
504 | fp = fopen(git_path(commit_editmsg), "w"); |
505 | if (fp == NULL) | |
cdeaf10f CP |
506 | die("could not open %s: %s", |
507 | git_path(commit_editmsg), strerror(errno)); | |
f5bbc322 | 508 | |
5f065737 AR |
509 | if (cleanup_mode != CLEANUP_NONE) |
510 | stripspace(&sb, 0); | |
f5bbc322 KH |
511 | |
512 | if (signoff) { | |
13208572 JS |
513 | struct strbuf sob; |
514 | int i; | |
515 | ||
516 | strbuf_init(&sob, 0); | |
517 | strbuf_addstr(&sob, sign_off_header); | |
d9ccfe77 JH |
518 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
519 | getenv("GIT_COMMITTER_EMAIL"))); | |
13208572 | 520 | strbuf_addch(&sob, '\n'); |
13208572 JS |
521 | for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--) |
522 | ; /* do nothing */ | |
2150554b JS |
523 | if (prefixcmp(sb.buf + i, sob.buf)) { |
524 | if (prefixcmp(sb.buf + i, sign_off_header)) | |
525 | strbuf_addch(&sb, '\n'); | |
13208572 | 526 | strbuf_addbuf(&sb, &sob); |
2150554b | 527 | } |
13208572 | 528 | strbuf_release(&sob); |
f5bbc322 KH |
529 | } |
530 | ||
13208572 | 531 | if (fwrite(sb.buf, 1, sb.len, fp) < sb.len) |
b5b644a9 | 532 | die("could not write commit template: %s", strerror(errno)); |
13208572 | 533 | |
f5bbc322 KH |
534 | strbuf_release(&sb); |
535 | ||
e83dbe80 SB |
536 | determine_author_info(); |
537 | ||
bb1ae3f6 SB |
538 | /* This checks if committer ident is explicitly given */ |
539 | git_committer_info(0); | |
ec84bd00 | 540 | if (use_editor) { |
e83dbe80 SB |
541 | char *author_ident; |
542 | const char *committer_ident; | |
543 | ||
ec84bd00 PB |
544 | if (in_merge) |
545 | fprintf(fp, | |
546 | "#\n" | |
547 | "# It looks like you may be committing a MERGE.\n" | |
548 | "# If this is not correct, please remove the file\n" | |
549 | "# %s\n" | |
550 | "# and try again.\n" | |
551 | "#\n", | |
552 | git_path("MERGE_HEAD")); | |
553 | ||
554 | fprintf(fp, | |
555 | "\n" | |
556 | "# Please enter the commit message for your changes.\n" | |
557 | "# (Comment lines starting with '#' will "); | |
558 | if (cleanup_mode == CLEANUP_ALL) | |
559 | fprintf(fp, "not be included)\n"); | |
560 | else /* CLEANUP_SPACE, that is. */ | |
561 | fprintf(fp, "be kept.\n" | |
562 | "# You can remove them yourself if you want to)\n"); | |
563 | if (only_include_assumed) | |
564 | fprintf(fp, "# %s\n", only_include_assumed); | |
565 | ||
e83dbe80 SB |
566 | author_ident = xstrdup(fmt_name(author_name, author_email)); |
567 | committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"), | |
568 | getenv("GIT_COMMITTER_EMAIL")); | |
569 | if (strcmp(author_ident, committer_ident)) | |
570 | fprintf(fp, | |
bb1ae3f6 SB |
571 | "%s" |
572 | "# Author: %s\n", | |
573 | ident_shown++ ? "" : "#\n", | |
e83dbe80 SB |
574 | author_ident); |
575 | free(author_ident); | |
576 | ||
bb1ae3f6 SB |
577 | if (!user_ident_explicitly_given) |
578 | fprintf(fp, | |
579 | "%s" | |
580 | "# Committer: %s\n", | |
581 | ident_shown++ ? "" : "#\n", | |
582 | committer_ident); | |
583 | ||
584 | if (ident_shown) | |
585 | fprintf(fp, "#\n"); | |
586 | ||
ec84bd00 PB |
587 | saved_color_setting = wt_status_use_color; |
588 | wt_status_use_color = 0; | |
589 | commitable = run_status(fp, index_file, prefix, 1); | |
590 | wt_status_use_color = saved_color_setting; | |
591 | } else { | |
7168624c | 592 | struct rev_info rev; |
d616a239 | 593 | unsigned char sha1[20]; |
fbcf1184 | 594 | const char *parent = "HEAD"; |
7168624c | 595 | |
7168624c AR |
596 | if (!active_nr && read_cache() < 0) |
597 | die("Cannot read index"); | |
598 | ||
fbcf1184 JH |
599 | if (amend) |
600 | parent = "HEAD^1"; | |
601 | ||
d616a239 | 602 | if (get_sha1(parent, sha1)) |
ec84bd00 PB |
603 | commitable = !!active_nr; |
604 | else { | |
605 | init_revisions(&rev, ""); | |
606 | rev.abbrev = 0; | |
607 | setup_revisions(0, NULL, &rev, parent); | |
608 | DIFF_OPT_SET(&rev.diffopt, QUIET); | |
609 | DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS); | |
610 | run_diff_index(&rev, 1 /* cached */); | |
611 | ||
612 | commitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES); | |
613 | } | |
614 | } | |
d616a239 | 615 | |
ec84bd00 | 616 | fclose(fp); |
7168624c | 617 | |
ec84bd00 PB |
618 | if (!commitable && !in_merge && !allow_empty && |
619 | !(amend && is_a_merge(head_sha1))) { | |
620 | run_status(stdout, index_file, prefix, 0); | |
621 | unlink(commit_editmsg); | |
622 | return 0; | |
7168624c AR |
623 | } |
624 | ||
ec84bd00 PB |
625 | /* |
626 | * Re-read the index as pre-commit hook could have updated it, | |
627 | * and write it out as a tree. We must do this before we invoke | |
628 | * the editor and after we invoke run_status above. | |
629 | */ | |
630 | discard_cache(); | |
631 | read_cache_from(index_file); | |
632 | if (!active_cache_tree) | |
633 | active_cache_tree = cache_tree(); | |
634 | if (cache_tree_update(active_cache_tree, | |
635 | active_cache, active_nr, 0, 0) < 0) { | |
636 | error("Error building trees"); | |
637 | return 0; | |
7168624c | 638 | } |
f5bbc322 | 639 | |
8089c85b PB |
640 | if (run_hook(index_file, "prepare-commit-msg", |
641 | git_path(commit_editmsg), hook_arg1, hook_arg2, NULL)) | |
642 | return 0; | |
f5bbc322 | 643 | |
ec84bd00 PB |
644 | if (use_editor) { |
645 | char index[PATH_MAX]; | |
646 | const char *env[2] = { index, NULL }; | |
647 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); | |
648 | launch_editor(git_path(commit_editmsg), NULL, env); | |
649 | } | |
f5bbc322 | 650 | |
ec84bd00 PB |
651 | if (!no_verify && |
652 | run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) { | |
653 | return 0; | |
654 | } | |
f5bbc322 | 655 | |
ec84bd00 | 656 | return 1; |
f5bbc322 KH |
657 | } |
658 | ||
659 | /* | |
660 | * Find out if the message starting at position 'start' in the strbuf | |
661 | * contains only whitespace and Signed-off-by lines. | |
662 | */ | |
663 | static int message_is_empty(struct strbuf *sb, int start) | |
664 | { | |
665 | struct strbuf tmpl; | |
666 | const char *nl; | |
667 | int eol, i; | |
668 | ||
5f065737 AR |
669 | if (cleanup_mode == CLEANUP_NONE && sb->len) |
670 | return 0; | |
671 | ||
f5bbc322 KH |
672 | /* See if the template is just a prefix of the message. */ |
673 | strbuf_init(&tmpl, 0); | |
674 | if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) { | |
5f065737 | 675 | stripspace(&tmpl, cleanup_mode == CLEANUP_ALL); |
f5bbc322 KH |
676 | if (start + tmpl.len <= sb->len && |
677 | memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0) | |
678 | start += tmpl.len; | |
679 | } | |
680 | strbuf_release(&tmpl); | |
681 | ||
682 | /* Check if the rest is just whitespace and Signed-of-by's. */ | |
683 | for (i = start; i < sb->len; i++) { | |
684 | nl = memchr(sb->buf + i, '\n', sb->len - i); | |
685 | if (nl) | |
686 | eol = nl - sb->buf; | |
687 | else | |
688 | eol = sb->len; | |
689 | ||
690 | if (strlen(sign_off_header) <= eol - i && | |
691 | !prefixcmp(sb->buf + i, sign_off_header)) { | |
692 | i = eol; | |
693 | continue; | |
694 | } | |
695 | while (i < eol) | |
696 | if (!isspace(sb->buf[i++])) | |
697 | return 0; | |
698 | } | |
699 | ||
700 | return 1; | |
701 | } | |
702 | ||
2f02b25f SB |
703 | static int parse_and_validate_options(int argc, const char *argv[], |
704 | const char * const usage[]) | |
f5bbc322 KH |
705 | { |
706 | int f = 0; | |
707 | ||
2f02b25f | 708 | argc = parse_options(argc, argv, builtin_commit_options, usage, 0); |
f5bbc322 | 709 | |
f9568530 | 710 | if (logfile || message.len || use_message) |
4803466f | 711 | use_editor = 0; |
f5bbc322 | 712 | if (edit_flag) |
4803466f | 713 | use_editor = 1; |
406400ce PB |
714 | if (!use_editor) |
715 | setenv("GIT_EDITOR", ":", 1); | |
f5bbc322 KH |
716 | |
717 | if (get_sha1("HEAD", head_sha1)) | |
718 | initial_commit = 1; | |
719 | ||
720 | if (!get_sha1("MERGE_HEAD", merge_head_sha1)) | |
721 | in_merge = 1; | |
722 | ||
723 | /* Sanity check options */ | |
724 | if (amend && initial_commit) | |
725 | die("You have nothing to amend."); | |
726 | if (amend && in_merge) | |
b5b644a9 | 727 | die("You are in the middle of a merge -- cannot amend."); |
f5bbc322 KH |
728 | |
729 | if (use_message) | |
730 | f++; | |
731 | if (edit_message) | |
732 | f++; | |
733 | if (logfile) | |
734 | f++; | |
735 | if (f > 1) | |
736 | die("Only one of -c/-C/-F can be used."); | |
f9568530 | 737 | if (message.len && f > 0) |
f5bbc322 KH |
738 | die("Option -m cannot be combined with -c/-C/-F."); |
739 | if (edit_message) | |
740 | use_message = edit_message; | |
1eb1e9ee | 741 | if (amend && !use_message) |
f5bbc322 KH |
742 | use_message = "HEAD"; |
743 | if (use_message) { | |
744 | unsigned char sha1[20]; | |
745 | static char utf8[] = "UTF-8"; | |
746 | const char *out_enc; | |
747 | char *enc, *end; | |
748 | struct commit *commit; | |
749 | ||
750 | if (get_sha1(use_message, sha1)) | |
751 | die("could not lookup commit %s", use_message); | |
8a2f8733 | 752 | commit = lookup_commit_reference(sha1); |
f5bbc322 KH |
753 | if (!commit || parse_commit(commit)) |
754 | die("could not parse commit %s", use_message); | |
755 | ||
756 | enc = strstr(commit->buffer, "\nencoding"); | |
757 | if (enc) { | |
758 | end = strchr(enc + 10, '\n'); | |
759 | enc = xstrndup(enc + 10, end - (enc + 10)); | |
760 | } else { | |
761 | enc = utf8; | |
762 | } | |
763 | out_enc = git_commit_encoding ? git_commit_encoding : utf8; | |
764 | ||
765 | if (strcmp(out_enc, enc)) | |
766 | use_message_buffer = | |
767 | reencode_string(commit->buffer, out_enc, enc); | |
768 | ||
769 | /* | |
770 | * If we failed to reencode the buffer, just copy it | |
771 | * byte for byte so the user can try to fix it up. | |
772 | * This also handles the case where input and output | |
773 | * encodings are identical. | |
774 | */ | |
775 | if (use_message_buffer == NULL) | |
776 | use_message_buffer = xstrdup(commit->buffer); | |
777 | if (enc != utf8) | |
778 | free(enc); | |
779 | } | |
780 | ||
781 | if (!!also + !!only + !!all + !!interactive > 1) | |
782 | die("Only one of --include/--only/--all/--interactive can be used."); | |
783 | if (argc == 0 && (also || (only && !amend))) | |
784 | die("No paths with --include/--only does not make sense."); | |
785 | if (argc == 0 && only && amend) | |
786 | only_include_assumed = "Clever... amending the last one with dirty index."; | |
3c5283f8 | 787 | if (argc > 0 && !also && !only) |
f5bbc322 | 788 | only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths..."; |
5f065737 AR |
789 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
790 | cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE; | |
791 | else if (!strcmp(cleanup_arg, "verbatim")) | |
792 | cleanup_mode = CLEANUP_NONE; | |
793 | else if (!strcmp(cleanup_arg, "whitespace")) | |
794 | cleanup_mode = CLEANUP_SPACE; | |
795 | else if (!strcmp(cleanup_arg, "strip")) | |
796 | cleanup_mode = CLEANUP_ALL; | |
797 | else | |
798 | die("Invalid cleanup mode %s", cleanup_arg); | |
f5bbc322 | 799 | |
4bfee30a MSO |
800 | if (!untracked_files_arg) |
801 | ; /* default already initialized */ | |
6c2ce048 MSO |
802 | else if (!strcmp(untracked_files_arg, "no")) |
803 | show_untracked_files = SHOW_NO_UNTRACKED_FILES; | |
4bfee30a MSO |
804 | else if (!strcmp(untracked_files_arg, "normal")) |
805 | show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
806 | else if (!strcmp(untracked_files_arg, "all")) | |
807 | show_untracked_files = SHOW_ALL_UNTRACKED_FILES; | |
808 | else | |
809 | die("Invalid untracked files mode '%s'", untracked_files_arg); | |
810 | ||
f5bbc322 KH |
811 | if (all && argc > 0) |
812 | die("Paths with -a does not make sense."); | |
813 | else if (interactive && argc > 0) | |
814 | die("Paths with --interactive does not make sense."); | |
815 | ||
816 | return argc; | |
817 | } | |
818 | ||
819 | int cmd_status(int argc, const char **argv, const char *prefix) | |
820 | { | |
821 | const char *index_file; | |
822 | int commitable; | |
823 | ||
ef90d6d4 | 824 | git_config(git_status_config, NULL); |
f5bbc322 | 825 | |
6b2f2d98 MK |
826 | if (wt_status_use_color == -1) |
827 | wt_status_use_color = git_use_color_default; | |
828 | ||
2f02b25f | 829 | argc = parse_and_validate_options(argc, argv, builtin_status_usage); |
f5bbc322 | 830 | |
f64fe7b4 | 831 | index_file = prepare_index(argc, argv, prefix); |
f5bbc322 | 832 | |
37d07f8f | 833 | commitable = run_status(stdout, index_file, prefix, 0); |
f5bbc322 | 834 | |
2888605c | 835 | rollback_index_files(); |
f5bbc322 KH |
836 | |
837 | return commitable ? 0 : 1; | |
838 | } | |
839 | ||
f5bbc322 KH |
840 | static void print_summary(const char *prefix, const unsigned char *sha1) |
841 | { | |
842 | struct rev_info rev; | |
843 | struct commit *commit; | |
844 | ||
845 | commit = lookup_commit(sha1); | |
846 | if (!commit) | |
b5b644a9 | 847 | die("couldn't look up newly created commit"); |
f5bbc322 KH |
848 | if (!commit || parse_commit(commit)) |
849 | die("could not parse newly created commit"); | |
850 | ||
851 | init_revisions(&rev, prefix); | |
852 | setup_revisions(0, NULL, &rev, NULL); | |
853 | ||
854 | rev.abbrev = 0; | |
855 | rev.diff = 1; | |
856 | rev.diffopt.output_format = | |
857 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; | |
858 | ||
859 | rev.verbose_header = 1; | |
860 | rev.show_root_diff = 1; | |
4da45bef | 861 | get_commit_format("format:%h: %s", &rev); |
bf82a150 | 862 | rev.always_show_header = 0; |
3eb2a15e JH |
863 | rev.diffopt.detect_rename = 1; |
864 | rev.diffopt.rename_limit = 100; | |
865 | rev.diffopt.break_opt = 0; | |
15964563 | 866 | diff_setup_done(&rev.diffopt); |
f5bbc322 KH |
867 | |
868 | printf("Created %scommit ", initial_commit ? "initial " : ""); | |
869 | ||
bf82a150 JH |
870 | if (!log_tree_commit(&rev, commit)) { |
871 | struct strbuf buf = STRBUF_INIT; | |
872 | format_commit_message(commit, "%h: %s", &buf); | |
873 | printf("%s\n", buf.buf); | |
874 | strbuf_release(&buf); | |
875 | } | |
f5bbc322 KH |
876 | } |
877 | ||
ef90d6d4 | 878 | int git_commit_config(const char *k, const char *v, void *cb) |
f5bbc322 KH |
879 | { |
880 | if (!strcmp(k, "commit.template")) { | |
d865eb2a JH |
881 | if (!v) |
882 | return config_error_nonbool(v); | |
f5bbc322 KH |
883 | template_file = xstrdup(v); |
884 | return 0; | |
885 | } | |
886 | ||
ef90d6d4 | 887 | return git_status_config(k, v, cb); |
f5bbc322 KH |
888 | } |
889 | ||
890 | static const char commit_utf8_warn[] = | |
891 | "Warning: commit message does not conform to UTF-8.\n" | |
892 | "You may want to amend it after fixing the message, or set the config\n" | |
893 | "variable i18n.commitencoding to the encoding your project uses.\n"; | |
894 | ||
7c3fd25d LT |
895 | static void add_parent(struct strbuf *sb, const unsigned char *sha1) |
896 | { | |
897 | struct object *obj = parse_object(sha1); | |
898 | const char *parent = sha1_to_hex(sha1); | |
67bfc030 JH |
899 | const char *cp; |
900 | ||
7c3fd25d LT |
901 | if (!obj) |
902 | die("Unable to find commit parent %s", parent); | |
903 | if (obj->type != OBJ_COMMIT) | |
904 | die("Parent %s isn't a proper commit", parent); | |
67bfc030 JH |
905 | |
906 | for (cp = sb->buf; cp && (cp = strstr(cp, "\nparent ")); cp += 8) { | |
907 | if (!memcmp(cp + 8, parent, 40) && cp[48] == '\n') { | |
908 | error("duplicate parent %s ignored", parent); | |
909 | return; | |
910 | } | |
911 | } | |
7c3fd25d LT |
912 | strbuf_addf(sb, "parent %s\n", parent); |
913 | } | |
914 | ||
f5bbc322 KH |
915 | int cmd_commit(int argc, const char **argv, const char *prefix) |
916 | { | |
18abc2db | 917 | int header_len; |
f5bbc322 KH |
918 | struct strbuf sb; |
919 | const char *index_file, *reflog_msg; | |
99a12694 | 920 | char *nl, *p; |
f5bbc322 KH |
921 | unsigned char commit_sha1[20]; |
922 | struct ref_lock *ref_lock; | |
923 | ||
ef90d6d4 | 924 | git_config(git_commit_config, NULL); |
f5bbc322 | 925 | |
2f02b25f | 926 | argc = parse_and_validate_options(argc, argv, builtin_commit_usage); |
f5bbc322 | 927 | |
f64fe7b4 | 928 | index_file = prepare_index(argc, argv, prefix); |
f5bbc322 | 929 | |
ec84bd00 PB |
930 | /* Set up everything for writing the commit object. This includes |
931 | running hooks, writing the trees, and interacting with the user. */ | |
932 | if (!prepare_to_commit(index_file, prefix)) { | |
2888605c | 933 | rollback_index_files(); |
f5bbc322 KH |
934 | return 1; |
935 | } | |
936 | ||
2888605c JH |
937 | /* |
938 | * The commit object | |
939 | */ | |
940 | strbuf_init(&sb, 0); | |
f5bbc322 KH |
941 | strbuf_addf(&sb, "tree %s\n", |
942 | sha1_to_hex(active_cache_tree->sha1)); | |
943 | ||
944 | /* Determine parents */ | |
945 | if (initial_commit) { | |
946 | reflog_msg = "commit (initial)"; | |
f5bbc322 KH |
947 | } else if (amend) { |
948 | struct commit_list *c; | |
949 | struct commit *commit; | |
950 | ||
951 | reflog_msg = "commit (amend)"; | |
952 | commit = lookup_commit(head_sha1); | |
953 | if (!commit || parse_commit(commit)) | |
954 | die("could not parse HEAD commit"); | |
955 | ||
956 | for (c = commit->parents; c; c = c->next) | |
7c3fd25d | 957 | add_parent(&sb, c->item->object.sha1); |
f5bbc322 KH |
958 | } else if (in_merge) { |
959 | struct strbuf m; | |
960 | FILE *fp; | |
961 | ||
962 | reflog_msg = "commit (merge)"; | |
7c3fd25d | 963 | add_parent(&sb, head_sha1); |
f5bbc322 KH |
964 | strbuf_init(&m, 0); |
965 | fp = fopen(git_path("MERGE_HEAD"), "r"); | |
966 | if (fp == NULL) | |
967 | die("could not open %s for reading: %s", | |
968 | git_path("MERGE_HEAD"), strerror(errno)); | |
7c3fd25d LT |
969 | while (strbuf_getline(&m, fp, '\n') != EOF) { |
970 | unsigned char sha1[20]; | |
971 | if (get_sha1_hex(m.buf, sha1) < 0) | |
972 | die("Corrupt MERGE_HEAD file (%s)", m.buf); | |
973 | add_parent(&sb, sha1); | |
974 | } | |
f5bbc322 KH |
975 | fclose(fp); |
976 | strbuf_release(&m); | |
977 | } else { | |
978 | reflog_msg = "commit"; | |
979 | strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1)); | |
980 | } | |
981 | ||
e83dbe80 SB |
982 | strbuf_addf(&sb, "author %s\n", |
983 | fmt_ident(author_name, author_email, author_date, IDENT_ERROR_ON_NO_NAME)); | |
774751a8 | 984 | strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME)); |
f5bbc322 KH |
985 | if (!is_encoding_utf8(git_commit_encoding)) |
986 | strbuf_addf(&sb, "encoding %s\n", git_commit_encoding); | |
987 | strbuf_addch(&sb, '\n'); | |
988 | ||
ec84bd00 | 989 | /* Finally, get the commit message */ |
f5bbc322 | 990 | header_len = sb.len; |
740001a5 JH |
991 | if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) { |
992 | rollback_index_files(); | |
993 | die("could not read commit message"); | |
994 | } | |
99a12694 KH |
995 | |
996 | /* Truncate the message just before the diff, if any. */ | |
997 | p = strstr(sb.buf, "\ndiff --git a/"); | |
998 | if (p != NULL) | |
a98b8191 | 999 | strbuf_setlen(&sb, p - sb.buf + 1); |
99a12694 | 1000 | |
5f065737 AR |
1001 | if (cleanup_mode != CLEANUP_NONE) |
1002 | stripspace(&sb, cleanup_mode == CLEANUP_ALL); | |
2888605c JH |
1003 | if (sb.len < header_len || message_is_empty(&sb, header_len)) { |
1004 | rollback_index_files(); | |
b5b644a9 | 1005 | die("no commit message? aborting commit."); |
2888605c | 1006 | } |
f5bbc322 KH |
1007 | strbuf_addch(&sb, '\0'); |
1008 | if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf)) | |
1009 | fprintf(stderr, commit_utf8_warn); | |
1010 | ||
2888605c JH |
1011 | if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) { |
1012 | rollback_index_files(); | |
f5bbc322 | 1013 | die("failed to write commit object"); |
2888605c | 1014 | } |
f5bbc322 KH |
1015 | |
1016 | ref_lock = lock_any_ref_for_update("HEAD", | |
1017 | initial_commit ? NULL : head_sha1, | |
1018 | 0); | |
1019 | ||
1020 | nl = strchr(sb.buf + header_len, '\n'); | |
741707b1 JS |
1021 | if (nl) |
1022 | strbuf_setlen(&sb, nl + 1 - sb.buf); | |
1023 | else | |
1024 | strbuf_addch(&sb, '\n'); | |
1025 | strbuf_remove(&sb, 0, header_len); | |
1026 | strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg)); | |
1027 | strbuf_insert(&sb, strlen(reflog_msg), ": ", 2); | |
f5bbc322 | 1028 | |
2888605c JH |
1029 | if (!ref_lock) { |
1030 | rollback_index_files(); | |
f5bbc322 | 1031 | die("cannot lock HEAD ref"); |
2888605c JH |
1032 | } |
1033 | if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) { | |
1034 | rollback_index_files(); | |
f5bbc322 | 1035 | die("cannot update HEAD ref"); |
2888605c | 1036 | } |
f5bbc322 KH |
1037 | |
1038 | unlink(git_path("MERGE_HEAD")); | |
1039 | unlink(git_path("MERGE_MSG")); | |
5a95b855 | 1040 | unlink(git_path("SQUASH_MSG")); |
f5bbc322 | 1041 | |
5a9dd399 BC |
1042 | if (commit_index_files()) |
1043 | die ("Repository has been updated, but unable to write\n" | |
1044 | "new_index file. Check that disk is not full or quota is\n" | |
1045 | "not exceeded, and then \"git reset HEAD\" to recover."); | |
f5bbc322 KH |
1046 | |
1047 | rerere(); | |
2888605c | 1048 | run_hook(get_index_file(), "post-commit", NULL); |
f5bbc322 KH |
1049 | if (!quiet) |
1050 | print_summary(prefix, commit_sha1); | |
1051 | ||
1052 | return 0; | |
1053 | } |