Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 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" |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 10 | #include "dir.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 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" |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 23 | #include "path-list.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 24 | |
| 25 | static const char * const builtin_commit_usage[] = { |
| 26 | "git-commit [options] [--] <filepattern>...", |
| 27 | NULL |
| 28 | }; |
| 29 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 30 | static const char * const builtin_status_usage[] = { |
| 31 | "git-status [options] [--] <filepattern>...", |
| 32 | NULL |
| 33 | }; |
| 34 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 35 | static unsigned char head_sha1[20], merge_head_sha1[20]; |
| 36 | static char *use_message_buffer; |
| 37 | static const char commit_editmsg[] = "COMMIT_EDITMSG"; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 38 | static struct lock_file index_lock; /* real index */ |
| 39 | static struct lock_file false_lock; /* used only for partial commits */ |
| 40 | static enum { |
| 41 | COMMIT_AS_IS = 1, |
| 42 | COMMIT_NORMAL, |
| 43 | COMMIT_PARTIAL, |
| 44 | } commit_style; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 45 | |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 46 | static char *logfile, *force_author, *template_file; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 47 | static char *edit_message, *use_message; |
| 48 | static int all, edit_flag, also, interactive, only, amend, signoff; |
Junio C Hamano | 5241b6b | 2007-12-03 00:03:10 -0800 | [diff] [blame] | 49 | static int quiet, verbose, untracked_files, no_verify, allow_empty; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 50 | |
| 51 | static int no_edit, initial_commit, in_merge; |
| 52 | const char *only_include_assumed; |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 53 | struct strbuf message; |
| 54 | |
| 55 | static 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 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 67 | |
| 68 | static 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"), |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 75 | OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m), |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 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"), |
Junio C Hamano | 5241b6b | 2007-12-03 00:03:10 -0800 | [diff] [blame] | 90 | OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"), |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 91 | |
| 92 | OPT_END() |
| 93 | }; |
| 94 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 95 | static 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 | |
| 110 | static 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 | */ |
| 129 | static 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 | |
| 154 | static 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 | |
Junio C Hamano | f64fe7b | 2007-11-25 08:46:29 -0800 | [diff] [blame] | 166 | static char *prepare_index(int argc, const char **argv, const char *prefix) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 167 | { |
| 168 | int fd; |
| 169 | struct tree *tree; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 170 | struct path_list partial; |
| 171 | const char **pathspec = NULL; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 172 | |
| 173 | if (interactive) { |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 174 | interactive_add(argc, argv, prefix); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 175 | commit_style = COMMIT_AS_IS; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 176 | return get_index_file(); |
| 177 | } |
| 178 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 179 | if (read_cache() < 0) |
| 180 | die("index file corrupt"); |
| 181 | |
Junio C Hamano | f64fe7b | 2007-11-25 08:46:29 -0800 | [diff] [blame] | 182 | if (*argv) |
| 183 | pathspec = get_pathspec(prefix, argv); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 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); |
Kristian Høgsberg | d37d320 | 2007-11-09 11:40:27 -0500 | [diff] [blame] | 200 | refresh_cache(REFRESH_QUIET); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 201 | if (write_cache(fd, active_cache, active_nr) || close(fd)) |
| 202 | die("unable to write new_index file"); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 203 | commit_style = COMMIT_NORMAL; |
| 204 | return index_lock.filename; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 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; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 223 | return get_index_file(); |
| 224 | } |
| 225 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 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); |
Kristian Høgsberg | ef12b50 | 2007-11-12 15:48:22 -0500 | [diff] [blame] | 261 | refresh_cache(REFRESH_QUIET); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 262 | if (write_cache(fd, active_cache, active_nr) || close(fd)) |
| 263 | die("unable to write new_index file"); |
| 264 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 265 | fd = hold_lock_file_for_update(&false_lock, |
| 266 | git_path("next-index-%d", getpid()), 1); |
| 267 | discard_cache(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 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 | } |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 275 | add_remove_files(&partial); |
Kristian Høgsberg | d37d320 | 2007-11-09 11:40:27 -0500 | [diff] [blame] | 276 | refresh_cache(REFRESH_QUIET); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 277 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 278 | if (write_cache(fd, active_cache, active_nr) || close(fd)) |
| 279 | die("unable to write temporary index file"); |
| 280 | return false_lock.filename; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 281 | } |
| 282 | |
Johannes Schindelin | 367c988 | 2007-11-11 17:35:41 +0000 | [diff] [blame] | 283 | static int run_status(FILE *fp, const char *index_file, const char *prefix) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 284 | { |
| 285 | struct wt_status s; |
| 286 | |
| 287 | wt_status_prepare(&s); |
Jeff King | 46f721c | 2007-12-07 16:26:07 -0500 | [diff] [blame] | 288 | if (wt_status_relative_paths) |
| 289 | s.prefix = prefix; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 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; |
| 299 | |
| 300 | wt_status_print(&s); |
| 301 | |
| 302 | return s.commitable; |
| 303 | } |
| 304 | |
| 305 | static const char sign_off_header[] = "Signed-off-by: "; |
| 306 | |
Johannes Schindelin | 367c988 | 2007-11-11 17:35:41 +0000 | [diff] [blame] | 307 | static int prepare_log_message(const char *index_file, const char *prefix) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 308 | { |
| 309 | struct stat statbuf; |
Junio C Hamano | bc5d248 | 2007-11-18 12:01:38 -0800 | [diff] [blame] | 310 | int commitable, saved_color_setting; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 311 | struct strbuf sb; |
| 312 | char *buffer; |
| 313 | FILE *fp; |
| 314 | |
| 315 | strbuf_init(&sb, 0); |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 316 | if (message.len) { |
| 317 | strbuf_addbuf(&sb, &message); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 318 | } else if (logfile && !strcmp(logfile, "-")) { |
| 319 | if (isatty(0)) |
| 320 | fprintf(stderr, "(reading log message from standard input)\n"); |
| 321 | if (strbuf_read(&sb, 0, 0) < 0) |
| 322 | die("could not read log from standard input"); |
| 323 | } else if (logfile) { |
| 324 | if (strbuf_read_file(&sb, logfile, 0) < 0) |
| 325 | die("could not read log file '%s': %s", |
| 326 | logfile, strerror(errno)); |
| 327 | } else if (use_message) { |
| 328 | buffer = strstr(use_message_buffer, "\n\n"); |
| 329 | if (!buffer || buffer[2] == '\0') |
| 330 | die("commit has empty message"); |
| 331 | strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); |
| 332 | } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { |
| 333 | if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) |
| 334 | die("could not read MERGE_MSG: %s", strerror(errno)); |
| 335 | } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { |
| 336 | if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) |
| 337 | die("could not read SQUASH_MSG: %s", strerror(errno)); |
| 338 | } else if (template_file && !stat(template_file, &statbuf)) { |
| 339 | if (strbuf_read_file(&sb, template_file, 0) < 0) |
| 340 | die("could not read %s: %s", |
| 341 | template_file, strerror(errno)); |
| 342 | } |
| 343 | |
| 344 | fp = fopen(git_path(commit_editmsg), "w"); |
| 345 | if (fp == NULL) |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 346 | die("could not open %s", git_path(commit_editmsg)); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 347 | |
| 348 | stripspace(&sb, 0); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 349 | |
| 350 | if (signoff) { |
| 351 | struct strbuf sob; |
| 352 | int i; |
| 353 | |
| 354 | strbuf_init(&sob, 0); |
| 355 | strbuf_addstr(&sob, sign_off_header); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 356 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
| 357 | getenv("GIT_COMMITTER_EMAIL"))); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 358 | strbuf_addch(&sob, '\n'); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 359 | for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--) |
| 360 | ; /* do nothing */ |
Johannes Schindelin | 2150554 | 2007-11-11 17:36:27 +0000 | [diff] [blame] | 361 | if (prefixcmp(sb.buf + i, sob.buf)) { |
| 362 | if (prefixcmp(sb.buf + i, sign_off_header)) |
| 363 | strbuf_addch(&sb, '\n'); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 364 | strbuf_addbuf(&sb, &sob); |
Johannes Schindelin | 2150554 | 2007-11-11 17:36:27 +0000 | [diff] [blame] | 365 | } |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 366 | strbuf_release(&sob); |
| 367 | } |
| 368 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 369 | if (fwrite(sb.buf, 1, sb.len, fp) < sb.len) |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 370 | die("could not write commit template: %s", strerror(errno)); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 371 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 372 | strbuf_release(&sb); |
| 373 | |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 374 | if (no_edit) { |
| 375 | struct rev_info rev; |
| 376 | unsigned char sha1[40]; |
| 377 | |
| 378 | fclose(fp); |
| 379 | |
| 380 | if (!active_nr && read_cache() < 0) |
| 381 | die("Cannot read index"); |
| 382 | |
| 383 | if (get_sha1("HEAD", sha1) != 0) |
| 384 | return !!active_nr; |
| 385 | |
| 386 | init_revisions(&rev, ""); |
| 387 | rev.abbrev = 0; |
| 388 | setup_revisions(0, NULL, &rev, "HEAD"); |
| 389 | DIFF_OPT_SET(&rev.diffopt, QUIET); |
| 390 | DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS); |
| 391 | run_diff_index(&rev, 1 /* cached */); |
| 392 | |
| 393 | return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES); |
| 394 | } |
| 395 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 396 | if (in_merge && !no_edit) |
| 397 | fprintf(fp, |
| 398 | "#\n" |
| 399 | "# It looks like you may be committing a MERGE.\n" |
| 400 | "# If this is not correct, please remove the file\n" |
| 401 | "# %s\n" |
| 402 | "# and try again.\n" |
| 403 | "#\n", |
| 404 | git_path("MERGE_HEAD")); |
| 405 | |
| 406 | fprintf(fp, |
| 407 | "\n" |
| 408 | "# Please enter the commit message for your changes.\n" |
| 409 | "# (Comment lines starting with '#' will not be included)\n"); |
| 410 | if (only_include_assumed) |
| 411 | fprintf(fp, "# %s\n", only_include_assumed); |
| 412 | |
Junio C Hamano | bc5d248 | 2007-11-18 12:01:38 -0800 | [diff] [blame] | 413 | saved_color_setting = wt_status_use_color; |
| 414 | wt_status_use_color = 0; |
Johannes Schindelin | 367c988 | 2007-11-11 17:35:41 +0000 | [diff] [blame] | 415 | commitable = run_status(fp, index_file, prefix); |
Junio C Hamano | bc5d248 | 2007-11-18 12:01:38 -0800 | [diff] [blame] | 416 | wt_status_use_color = saved_color_setting; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 417 | |
| 418 | fclose(fp); |
| 419 | |
| 420 | return commitable; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Find out if the message starting at position 'start' in the strbuf |
| 425 | * contains only whitespace and Signed-off-by lines. |
| 426 | */ |
| 427 | static int message_is_empty(struct strbuf *sb, int start) |
| 428 | { |
| 429 | struct strbuf tmpl; |
| 430 | const char *nl; |
| 431 | int eol, i; |
| 432 | |
| 433 | /* See if the template is just a prefix of the message. */ |
| 434 | strbuf_init(&tmpl, 0); |
| 435 | if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) { |
| 436 | stripspace(&tmpl, 1); |
| 437 | if (start + tmpl.len <= sb->len && |
| 438 | memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0) |
| 439 | start += tmpl.len; |
| 440 | } |
| 441 | strbuf_release(&tmpl); |
| 442 | |
| 443 | /* Check if the rest is just whitespace and Signed-of-by's. */ |
| 444 | for (i = start; i < sb->len; i++) { |
| 445 | nl = memchr(sb->buf + i, '\n', sb->len - i); |
| 446 | if (nl) |
| 447 | eol = nl - sb->buf; |
| 448 | else |
| 449 | eol = sb->len; |
| 450 | |
| 451 | if (strlen(sign_off_header) <= eol - i && |
| 452 | !prefixcmp(sb->buf + i, sign_off_header)) { |
| 453 | i = eol; |
| 454 | continue; |
| 455 | } |
| 456 | while (i < eol) |
| 457 | if (!isspace(sb->buf[i++])) |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | static void determine_author_info(struct strbuf *sb) |
| 465 | { |
| 466 | char *name, *email, *date; |
| 467 | |
| 468 | name = getenv("GIT_AUTHOR_NAME"); |
| 469 | email = getenv("GIT_AUTHOR_EMAIL"); |
| 470 | date = getenv("GIT_AUTHOR_DATE"); |
| 471 | |
| 472 | if (use_message) { |
| 473 | const char *a, *lb, *rb, *eol; |
| 474 | |
| 475 | a = strstr(use_message_buffer, "\nauthor "); |
| 476 | if (!a) |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 477 | die("invalid commit: %s", use_message); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 478 | |
| 479 | lb = strstr(a + 8, " <"); |
| 480 | rb = strstr(a + 8, "> "); |
| 481 | eol = strchr(a + 8, '\n'); |
| 482 | if (!lb || !rb || !eol) |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 483 | die("invalid commit: %s", use_message); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 484 | |
| 485 | name = xstrndup(a + 8, lb - (a + 8)); |
| 486 | email = xstrndup(lb + 2, rb - (lb + 2)); |
| 487 | date = xstrndup(rb + 2, eol - (rb + 2)); |
| 488 | } |
| 489 | |
| 490 | if (force_author) { |
| 491 | const char *lb = strstr(force_author, " <"); |
| 492 | const char *rb = strchr(force_author, '>'); |
| 493 | |
| 494 | if (!lb || !rb) |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 495 | die("malformed --author parameter"); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 496 | name = xstrndup(force_author, lb - force_author); |
| 497 | email = xstrndup(lb + 2, rb - (lb + 2)); |
| 498 | } |
| 499 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame^] | 500 | strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME)); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 503 | static int parse_and_validate_options(int argc, const char *argv[], |
| 504 | const char * const usage[]) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 505 | { |
| 506 | int f = 0; |
| 507 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 508 | argc = parse_options(argc, argv, builtin_commit_options, usage, 0); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 509 | |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 510 | if (logfile || message.len || use_message) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 511 | no_edit = 1; |
| 512 | if (edit_flag) |
| 513 | no_edit = 0; |
| 514 | |
| 515 | if (get_sha1("HEAD", head_sha1)) |
| 516 | initial_commit = 1; |
| 517 | |
| 518 | if (!get_sha1("MERGE_HEAD", merge_head_sha1)) |
| 519 | in_merge = 1; |
| 520 | |
| 521 | /* Sanity check options */ |
| 522 | if (amend && initial_commit) |
| 523 | die("You have nothing to amend."); |
| 524 | if (amend && in_merge) |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 525 | die("You are in the middle of a merge -- cannot amend."); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 526 | |
| 527 | if (use_message) |
| 528 | f++; |
| 529 | if (edit_message) |
| 530 | f++; |
| 531 | if (logfile) |
| 532 | f++; |
| 533 | if (f > 1) |
| 534 | die("Only one of -c/-C/-F can be used."); |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 535 | if (message.len && f > 0) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 536 | die("Option -m cannot be combined with -c/-C/-F."); |
| 537 | if (edit_message) |
| 538 | use_message = edit_message; |
| 539 | if (amend) |
| 540 | use_message = "HEAD"; |
| 541 | if (use_message) { |
| 542 | unsigned char sha1[20]; |
| 543 | static char utf8[] = "UTF-8"; |
| 544 | const char *out_enc; |
| 545 | char *enc, *end; |
| 546 | struct commit *commit; |
| 547 | |
| 548 | if (get_sha1(use_message, sha1)) |
| 549 | die("could not lookup commit %s", use_message); |
| 550 | commit = lookup_commit(sha1); |
| 551 | if (!commit || parse_commit(commit)) |
| 552 | die("could not parse commit %s", use_message); |
| 553 | |
| 554 | enc = strstr(commit->buffer, "\nencoding"); |
| 555 | if (enc) { |
| 556 | end = strchr(enc + 10, '\n'); |
| 557 | enc = xstrndup(enc + 10, end - (enc + 10)); |
| 558 | } else { |
| 559 | enc = utf8; |
| 560 | } |
| 561 | out_enc = git_commit_encoding ? git_commit_encoding : utf8; |
| 562 | |
| 563 | if (strcmp(out_enc, enc)) |
| 564 | use_message_buffer = |
| 565 | reencode_string(commit->buffer, out_enc, enc); |
| 566 | |
| 567 | /* |
| 568 | * If we failed to reencode the buffer, just copy it |
| 569 | * byte for byte so the user can try to fix it up. |
| 570 | * This also handles the case where input and output |
| 571 | * encodings are identical. |
| 572 | */ |
| 573 | if (use_message_buffer == NULL) |
| 574 | use_message_buffer = xstrdup(commit->buffer); |
| 575 | if (enc != utf8) |
| 576 | free(enc); |
| 577 | } |
| 578 | |
| 579 | if (!!also + !!only + !!all + !!interactive > 1) |
| 580 | die("Only one of --include/--only/--all/--interactive can be used."); |
| 581 | if (argc == 0 && (also || (only && !amend))) |
| 582 | die("No paths with --include/--only does not make sense."); |
| 583 | if (argc == 0 && only && amend) |
| 584 | only_include_assumed = "Clever... amending the last one with dirty index."; |
| 585 | if (argc > 0 && !also && !only) { |
| 586 | only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths..."; |
| 587 | also = 0; |
| 588 | } |
| 589 | |
| 590 | if (all && argc > 0) |
| 591 | die("Paths with -a does not make sense."); |
| 592 | else if (interactive && argc > 0) |
| 593 | die("Paths with --interactive does not make sense."); |
| 594 | |
| 595 | return argc; |
| 596 | } |
| 597 | |
| 598 | int cmd_status(int argc, const char **argv, const char *prefix) |
| 599 | { |
| 600 | const char *index_file; |
| 601 | int commitable; |
| 602 | |
| 603 | git_config(git_status_config); |
| 604 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 605 | argc = parse_and_validate_options(argc, argv, builtin_status_usage); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 606 | |
Junio C Hamano | f64fe7b | 2007-11-25 08:46:29 -0800 | [diff] [blame] | 607 | index_file = prepare_index(argc, argv, prefix); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 608 | |
Johannes Schindelin | 367c988 | 2007-11-11 17:35:41 +0000 | [diff] [blame] | 609 | commitable = run_status(stdout, index_file, prefix); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 610 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 611 | rollback_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 612 | |
| 613 | return commitable ? 0 : 1; |
| 614 | } |
| 615 | |
| 616 | static int run_hook(const char *index_file, const char *name, const char *arg) |
| 617 | { |
| 618 | struct child_process hook; |
| 619 | const char *argv[3], *env[2]; |
| 620 | char index[PATH_MAX]; |
| 621 | |
| 622 | argv[0] = git_path("hooks/%s", name); |
| 623 | argv[1] = arg; |
| 624 | argv[2] = NULL; |
| 625 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); |
| 626 | env[0] = index; |
| 627 | env[1] = NULL; |
| 628 | |
| 629 | if (access(argv[0], X_OK) < 0) |
| 630 | return 0; |
| 631 | |
| 632 | memset(&hook, 0, sizeof(hook)); |
| 633 | hook.argv = argv; |
| 634 | hook.no_stdin = 1; |
| 635 | hook.stdout_to_stderr = 1; |
| 636 | hook.env = env; |
| 637 | |
| 638 | return run_command(&hook); |
| 639 | } |
| 640 | |
| 641 | static void print_summary(const char *prefix, const unsigned char *sha1) |
| 642 | { |
| 643 | struct rev_info rev; |
| 644 | struct commit *commit; |
| 645 | |
| 646 | commit = lookup_commit(sha1); |
| 647 | if (!commit) |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 648 | die("couldn't look up newly created commit"); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 649 | if (!commit || parse_commit(commit)) |
| 650 | die("could not parse newly created commit"); |
| 651 | |
| 652 | init_revisions(&rev, prefix); |
| 653 | setup_revisions(0, NULL, &rev, NULL); |
| 654 | |
| 655 | rev.abbrev = 0; |
| 656 | rev.diff = 1; |
| 657 | rev.diffopt.output_format = |
| 658 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; |
| 659 | |
| 660 | rev.verbose_header = 1; |
| 661 | rev.show_root_diff = 1; |
| 662 | rev.commit_format = get_commit_format("format:%h: %s"); |
| 663 | rev.always_show_header = 1; |
| 664 | |
| 665 | printf("Created %scommit ", initial_commit ? "initial " : ""); |
| 666 | |
| 667 | log_tree_commit(&rev, commit); |
Johannes Schindelin | 129fa60 | 2007-11-11 17:36:52 +0000 | [diff] [blame] | 668 | printf("\n"); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | int git_commit_config(const char *k, const char *v) |
| 672 | { |
| 673 | if (!strcmp(k, "commit.template")) { |
| 674 | template_file = xstrdup(v); |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | return git_status_config(k, v); |
| 679 | } |
| 680 | |
Junio C Hamano | 9663c3b | 2007-12-02 23:55:23 -0800 | [diff] [blame] | 681 | static int is_a_merge(const unsigned char *sha1) |
| 682 | { |
| 683 | struct commit *commit = lookup_commit(sha1); |
| 684 | if (!commit || parse_commit(commit)) |
| 685 | die("could not parse HEAD commit"); |
| 686 | return !!(commit->parents && commit->parents->next); |
| 687 | } |
| 688 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 689 | static const char commit_utf8_warn[] = |
| 690 | "Warning: commit message does not conform to UTF-8.\n" |
| 691 | "You may want to amend it after fixing the message, or set the config\n" |
| 692 | "variable i18n.commitencoding to the encoding your project uses.\n"; |
| 693 | |
| 694 | int cmd_commit(int argc, const char **argv, const char *prefix) |
| 695 | { |
Kristian Høgsberg | 18abc2d | 2007-11-14 10:31:53 -0500 | [diff] [blame] | 696 | int header_len; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 697 | struct strbuf sb; |
| 698 | const char *index_file, *reflog_msg; |
Kristian Høgsberg | 99a1269 | 2007-11-21 21:54:49 -0500 | [diff] [blame] | 699 | char *nl, *p; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 700 | unsigned char commit_sha1[20]; |
| 701 | struct ref_lock *ref_lock; |
| 702 | |
| 703 | git_config(git_commit_config); |
| 704 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 705 | argc = parse_and_validate_options(argc, argv, builtin_commit_usage); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 706 | |
Junio C Hamano | f64fe7b | 2007-11-25 08:46:29 -0800 | [diff] [blame] | 707 | index_file = prepare_index(argc, argv, prefix); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 708 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 709 | if (!no_verify && run_hook(index_file, "pre-commit", NULL)) { |
| 710 | rollback_index_files(); |
| 711 | return 1; |
| 712 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 713 | |
Junio C Hamano | 9663c3b | 2007-12-02 23:55:23 -0800 | [diff] [blame] | 714 | if (!prepare_log_message(index_file, prefix) && !in_merge && |
Junio C Hamano | 5241b6b | 2007-12-03 00:03:10 -0800 | [diff] [blame] | 715 | !allow_empty && !(amend && is_a_merge(head_sha1))) { |
Johannes Schindelin | 367c988 | 2007-11-11 17:35:41 +0000 | [diff] [blame] | 716 | run_status(stdout, index_file, prefix); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 717 | rollback_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 718 | unlink(commit_editmsg); |
| 719 | return 1; |
| 720 | } |
| 721 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 722 | /* |
| 723 | * Re-read the index as pre-commit hook could have updated it, |
| 724 | * and write it out as a tree. |
| 725 | */ |
| 726 | discard_cache(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 727 | read_cache_from(index_file); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 728 | if (!active_cache_tree) |
| 729 | active_cache_tree = cache_tree(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 730 | if (cache_tree_update(active_cache_tree, |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 731 | active_cache, active_nr, 0, 0) < 0) { |
| 732 | rollback_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 733 | die("Error building trees"); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* |
| 737 | * The commit object |
| 738 | */ |
| 739 | strbuf_init(&sb, 0); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 740 | strbuf_addf(&sb, "tree %s\n", |
| 741 | sha1_to_hex(active_cache_tree->sha1)); |
| 742 | |
| 743 | /* Determine parents */ |
| 744 | if (initial_commit) { |
| 745 | reflog_msg = "commit (initial)"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 746 | } else if (amend) { |
| 747 | struct commit_list *c; |
| 748 | struct commit *commit; |
| 749 | |
| 750 | reflog_msg = "commit (amend)"; |
| 751 | commit = lookup_commit(head_sha1); |
| 752 | if (!commit || parse_commit(commit)) |
| 753 | die("could not parse HEAD commit"); |
| 754 | |
| 755 | for (c = commit->parents; c; c = c->next) |
| 756 | strbuf_addf(&sb, "parent %s\n", |
| 757 | sha1_to_hex(c->item->object.sha1)); |
| 758 | } else if (in_merge) { |
| 759 | struct strbuf m; |
| 760 | FILE *fp; |
| 761 | |
| 762 | reflog_msg = "commit (merge)"; |
| 763 | strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1)); |
| 764 | strbuf_init(&m, 0); |
| 765 | fp = fopen(git_path("MERGE_HEAD"), "r"); |
| 766 | if (fp == NULL) |
| 767 | die("could not open %s for reading: %s", |
| 768 | git_path("MERGE_HEAD"), strerror(errno)); |
| 769 | while (strbuf_getline(&m, fp, '\n') != EOF) |
| 770 | strbuf_addf(&sb, "parent %s\n", m.buf); |
| 771 | fclose(fp); |
| 772 | strbuf_release(&m); |
| 773 | } else { |
| 774 | reflog_msg = "commit"; |
| 775 | strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1)); |
| 776 | } |
| 777 | |
| 778 | determine_author_info(&sb); |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame^] | 779 | strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME)); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 780 | if (!is_encoding_utf8(git_commit_encoding)) |
| 781 | strbuf_addf(&sb, "encoding %s\n", git_commit_encoding); |
| 782 | strbuf_addch(&sb, '\n'); |
| 783 | |
| 784 | /* Get the commit message and validate it */ |
| 785 | header_len = sb.len; |
Pierre Habouzit | 8babab9 | 2007-11-26 09:59:27 +0100 | [diff] [blame] | 786 | if (!no_edit) { |
| 787 | char index[PATH_MAX]; |
| 788 | const char *env[2] = { index, NULL }; |
| 789 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); |
Junio C Hamano | 740001a | 2007-12-08 23:23:20 -0800 | [diff] [blame] | 790 | launch_editor(git_path(commit_editmsg), NULL, env); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 791 | } |
Wincent Colaiuta | 6b95655 | 2007-12-08 12:38:07 +0100 | [diff] [blame] | 792 | if (!no_verify && |
| 793 | run_hook(index_file, "commit-msg", git_path(commit_editmsg))) { |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 794 | rollback_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 795 | exit(1); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 796 | } |
Junio C Hamano | 740001a | 2007-12-08 23:23:20 -0800 | [diff] [blame] | 797 | if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) { |
| 798 | rollback_index_files(); |
| 799 | die("could not read commit message"); |
| 800 | } |
Kristian Høgsberg | 99a1269 | 2007-11-21 21:54:49 -0500 | [diff] [blame] | 801 | |
| 802 | /* Truncate the message just before the diff, if any. */ |
| 803 | p = strstr(sb.buf, "\ndiff --git a/"); |
| 804 | if (p != NULL) |
Kristian Høgsberg | a98b819 | 2007-11-26 10:16:08 -0500 | [diff] [blame] | 805 | strbuf_setlen(&sb, p - sb.buf + 1); |
Kristian Høgsberg | 99a1269 | 2007-11-21 21:54:49 -0500 | [diff] [blame] | 806 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 807 | stripspace(&sb, 1); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 808 | if (sb.len < header_len || message_is_empty(&sb, header_len)) { |
| 809 | rollback_index_files(); |
Jeff King | b5b644a | 2007-12-02 01:07:03 -0500 | [diff] [blame] | 810 | die("no commit message? aborting commit."); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 811 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 812 | strbuf_addch(&sb, '\0'); |
| 813 | if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf)) |
| 814 | fprintf(stderr, commit_utf8_warn); |
| 815 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 816 | if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) { |
| 817 | rollback_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 818 | die("failed to write commit object"); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 819 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 820 | |
| 821 | ref_lock = lock_any_ref_for_update("HEAD", |
| 822 | initial_commit ? NULL : head_sha1, |
| 823 | 0); |
| 824 | |
| 825 | nl = strchr(sb.buf + header_len, '\n'); |
Johannes Schindelin | 741707b | 2007-11-08 12:15:26 +0000 | [diff] [blame] | 826 | if (nl) |
| 827 | strbuf_setlen(&sb, nl + 1 - sb.buf); |
| 828 | else |
| 829 | strbuf_addch(&sb, '\n'); |
| 830 | strbuf_remove(&sb, 0, header_len); |
| 831 | strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg)); |
| 832 | strbuf_insert(&sb, strlen(reflog_msg), ": ", 2); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 833 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 834 | if (!ref_lock) { |
| 835 | rollback_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 836 | die("cannot lock HEAD ref"); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 837 | } |
| 838 | if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) { |
| 839 | rollback_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 840 | die("cannot update HEAD ref"); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 841 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 842 | |
| 843 | unlink(git_path("MERGE_HEAD")); |
| 844 | unlink(git_path("MERGE_MSG")); |
| 845 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 846 | commit_index_files(); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 847 | |
| 848 | rerere(); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 849 | run_hook(get_index_file(), "post-commit", NULL); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 850 | if (!quiet) |
| 851 | print_summary(prefix, commit_sha1); |
| 852 | |
| 853 | return 0; |
| 854 | } |