@matheusbsbs / matheus-bagliano / commits / 2888605c64
Fork 0

builtin-commit: fix partial-commit support

When making a partial-commit, we need to prepare two index files, one to be used to write out the tree to be committed (temporary index) and the other to be used as the index file after the commit is made. The temporary index needs to be initialized to HEAD and then all the named paths on the command line need to be staged on top of the index. For this, running add_files_to_cache() that compares what is in the index and the paths given from the command line is not enough -- the comparison will miss the paths that the user previously ran "git add" to the index since the HEAD because the index reset to the HEAD would not know about them. The index file needs to get the same modification done when preparing the temporary index as described above. This implementation mimics the behaviour of the scripted version of git-commit. It first runs overlay_tree_on_cache(), which was stolen from ls-files with the earlier change, to get the list of paths that the user can potentially mean, and then uses pathspec_match() to find which ones the user meant. This list of paths is used to update both the temporary and the real index file. Additional fixes are: - read the index file after pre-commit hook returns, as the hook can modify it to affect the contents of the commit. - remove the temporary index file .git/next-index-* after commit is done or aborted. - run post-commit hook with the real index file to be used after the commit (previously it gave the temporary commit if a partial commit was made). - resurrect the safety mechanism to refuse partial commits during a merge to match the scripted version. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 18, 2007 at 01:52 UTC 2888605c649ccd423232161186d72c0e6c458a48
1 file changed +202 -40
builtin-commit.c
+202 -40
@@ -7,6 +7,7 @@
7
8 #include "cache.h"
9 #include "cache-tree.h"
10 +#include "dir.h"
11 #include "builtin.h"
12 #include "diff.h"
13 #include "diffcore.h"
@@ -19,6 +20,7 @@
20 #include "strbuf.h"
21 #include "utf8.h"
22 #include "parse-options.h"
23 +#include "path-list.h"
24
25 static const char * const builtin_commit_usage[] = {
26 "git-commit [options] [--] <filepattern>...",
@@ -28,7 +30,13 @@ static const char * const builtin_commit_usage[] = {
30 static unsigned char head_sha1[20], merge_head_sha1[20];
31 static char *use_message_buffer;
32 static const char commit_editmsg[] = "COMMIT_EDITMSG";
31 -static struct lock_file lock_file;
33 +static struct lock_file index_lock; /* real index */
34 +static struct lock_file false_lock; /* used only for partial commits */
35 +static enum {
36 + COMMIT_AS_IS = 1,
37 + COMMIT_NORMAL,
38 + COMMIT_PARTIAL,
39 +} commit_style;
40
41 static char *logfile, *force_author, *template_file;
42 static char *edit_message, *use_message;
@@ -78,41 +86,179 @@ static struct option builtin_commit_options[] = {
86 OPT_END()
87 };
88
89 +static void rollback_index_files(void)
90 +{
91 + switch (commit_style) {
92 + case COMMIT_AS_IS:
93 + break; /* nothing to do */
94 + case COMMIT_NORMAL:
95 + rollback_lock_file(&index_lock);
96 + break;
97 + case COMMIT_PARTIAL:
98 + rollback_lock_file(&index_lock);
99 + rollback_lock_file(&false_lock);
100 + break;
101 + }
102 +}
103 +
104 +static void commit_index_files(void)
105 +{
106 + switch (commit_style) {
107 + case COMMIT_AS_IS:
108 + break; /* nothing to do */
109 + case COMMIT_NORMAL:
110 + commit_lock_file(&index_lock);
111 + break;
112 + case COMMIT_PARTIAL:
113 + commit_lock_file(&index_lock);
114 + rollback_lock_file(&false_lock);
115 + break;
116 + }
117 +}
118 +
119 +/*
120 + * Take a union of paths in the index and the named tree (typically, "HEAD"),
121 + * and return the paths that match the given pattern in list.
122 + */
123 +static int list_paths(struct path_list *list, const char *with_tree,
124 + const char *prefix, const char **pattern)
125 +{
126 + int i;
127 + char *m;
128 +
129 + for (i = 0; pattern[i]; i++)
130 + ;
131 + m = xcalloc(1, i);
132 +
133 + if (with_tree)
134 + overlay_tree_on_cache(with_tree, prefix);
135 +
136 + for (i = 0; i < active_nr; i++) {
137 + struct cache_entry *ce = active_cache[i];
138 + if (ce->ce_flags & htons(CE_UPDATE))
139 + continue;
140 + if (!pathspec_match(pattern, m, ce->name, 0))
141 + continue;
142 + path_list_insert(ce->name, list);
143 + }
144 +
145 + return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
146 +}
147 +
148 +static void add_remove_files(struct path_list *list)
149 +{
150 + int i;
151 + for (i = 0; i < list->nr; i++) {
152 + struct path_list_item *p = &(list->items[i]);
153 + if (file_exists(p->path))
154 + add_file_to_cache(p->path, 0);
155 + else
156 + remove_file_from_cache(p->path);
157 + }
158 +}
159 +
160 static char *prepare_index(const char **files, const char *prefix)
161 {
162 int fd;
163 struct tree *tree;
85 - struct lock_file *next_index_lock;
164 + struct path_list partial;
165 + const char **pathspec = NULL;
166
167 if (interactive) {
168 interactive_add();
169 + commit_style = COMMIT_AS_IS;
170 return get_index_file();
171 }
172
92 - fd = hold_locked_index(&lock_file, 1);
173 if (read_cache() < 0)
174 die("index file corrupt");
175
96 - if (all || also) {
97 - add_files_to_cache(verbose, also ? prefix : NULL, files);
176 + if (*files)
177 + pathspec = get_pathspec(prefix, files);
178 +
179 + /*
180 + * Non partial, non as-is commit.
181 + *
182 + * (1) get the real index;
183 + * (2) update the_index as necessary;
184 + * (3) write the_index out to the real index (still locked);
185 + * (4) return the name of the locked index file.
186 + *
187 + * The caller should run hooks on the locked real index, and
188 + * (A) if all goes well, commit the real index;
189 + * (B) on failure, rollback the real index.
190 + */
191 + if (all || (also && pathspec && *pathspec)) {
192 + int fd = hold_locked_index(&index_lock, 1);
193 + add_files_to_cache(0, also ? prefix : NULL, pathspec);
194 refresh_cache(REFRESH_QUIET);
195 if (write_cache(fd, active_cache, active_nr) || close(fd))
196 die("unable to write new_index file");
101 - return lock_file.filename;
197 + commit_style = COMMIT_NORMAL;
198 + return index_lock.filename;
199 }
200
104 - if (*files == NULL) {
105 - /* Commit index as-is. */
106 - rollback_lock_file(&lock_file);
201 + /*
202 + * As-is commit.
203 + *
204 + * (1) return the name of the real index file.
205 + *
206 + * The caller should run hooks on the real index, and run
207 + * hooks on the real index, and create commit from the_index.
208 + * We still need to refresh the index here.
209 + */
210 + if (!pathspec || !*pathspec) {
211 + fd = hold_locked_index(&index_lock, 1);
212 + refresh_cache(REFRESH_QUIET);
213 + if (write_cache(fd, active_cache, active_nr) ||
214 + close(fd) || commit_locked_index(&index_lock))
215 + die("unable to write new_index file");
216 + commit_style = COMMIT_AS_IS;
217 return get_index_file();
218 }
219
110 - /* update the user index file */
111 - add_files_to_cache(verbose, prefix, files);
220 + /*
221 + * A partial commit.
222 + *
223 + * (0) find the set of affected paths;
224 + * (1) get lock on the real index file;
225 + * (2) update the_index with the given paths;
226 + * (3) write the_index out to the real index (still locked);
227 + * (4) get lock on the false index file;
228 + * (5) reset the_index from HEAD;
229 + * (6) update the_index the same way as (2);
230 + * (7) write the_index out to the false index file;
231 + * (8) return the name of the false index file (still locked);
232 + *
233 + * The caller should run hooks on the locked false index, and
234 + * create commit from it. Then
235 + * (A) if all goes well, commit the real index;
236 + * (B) on failure, rollback the real index;
237 + * In either case, rollback the false index.
238 + */
239 + commit_style = COMMIT_PARTIAL;
240 +
241 + if (file_exists(git_path("MERGE_HEAD")))
242 + die("cannot do a partial commit during a merge.");
243 +
244 + memset(&partial, 0, sizeof(partial));
245 + partial.strdup_paths = 1;
246 + if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
247 + exit(1);
248 +
249 + discard_cache();
250 + if (read_cache() < 0)
251 + die("cannot read the index");
252 +
253 + fd = hold_locked_index(&index_lock, 1);
254 + add_remove_files(&partial);
255 refresh_cache(REFRESH_QUIET);
256 if (write_cache(fd, active_cache, active_nr) || close(fd))
257 die("unable to write new_index file");
258
259 + fd = hold_lock_file_for_update(&false_lock,
260 + git_path("next-index-%d", getpid()), 1);
261 + discard_cache();
262 if (!initial_commit) {
263 tree = parse_tree_indirect(head_sha1);
264 if (!tree)
@@ -120,17 +266,12 @@ static char *prepare_index(const char **files, const char *prefix)
266 if (read_tree(tree, 0, NULL))
267 die("failed to read HEAD tree object");
268 }
123 -
124 - /* Use a lock file to garbage collect the temporary index file. */
125 - next_index_lock = xmalloc(sizeof(*next_index_lock));
126 - fd = hold_lock_file_for_update(next_index_lock,
127 - git_path("next-index-%d", getpid()), 1);
128 - add_files_to_cache(verbose, prefix, files);
269 + add_remove_files(&partial);
270 refresh_cache(REFRESH_QUIET);
130 - if (write_cache(fd, active_cache, active_nr) || close(fd))
131 - die("unable to write new_index file");
271
133 - return next_index_lock->filename;
272 + if (write_cache(fd, active_cache, active_nr) || close(fd))
273 + die("unable to write temporary index file");
274 + return false_lock.filename;
275 }
276
277 static int run_status(FILE *fp, const char *index_file, const char *prefix)
@@ -441,7 +582,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
582
583 commitable = run_status(stdout, index_file, prefix);
584
444 - rollback_lock_file(&lock_file);
585 + rollback_index_files();
586
587 return commitable ? 0 : 1;
588 }
@@ -531,23 +672,36 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
672
673 index_file = prepare_index(argv, prefix);
674
534 - if (!no_verify && run_hook(index_file, "pre-commit", NULL))
535 - exit(1);
675 + if (!no_verify && run_hook(index_file, "pre-commit", NULL)) {
676 + rollback_index_files();
677 + return 1;
678 + }
679
680 if (!prepare_log_message(index_file, prefix) && !in_merge) {
681 run_status(stdout, index_file, prefix);
682 + rollback_index_files();
683 unlink(commit_editmsg);
684 return 1;
685 }
686
543 - strbuf_init(&sb, 0);
544 -
545 - /* Start building up the commit header */
687 + /*
688 + * Re-read the index as pre-commit hook could have updated it,
689 + * and write it out as a tree.
690 + */
691 + discard_cache();
692 read_cache_from(index_file);
547 - active_cache_tree = cache_tree();
693 + if (!active_cache_tree)
694 + active_cache_tree = cache_tree();
695 if (cache_tree_update(active_cache_tree,
549 - active_cache, active_nr, 0, 0) < 0)
696 + active_cache, active_nr, 0, 0) < 0) {
697 + rollback_index_files();
698 die("Error building trees");
699 + }
700 +
701 + /*
702 + * The commit object
703 + */
704 + strbuf_init(&sb, 0);
705 strbuf_addf(&sb, "tree %s\n",
706 sha1_to_hex(active_cache_tree->sha1));
707
@@ -596,20 +750,27 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
750 header_len = sb.len;
751 if (!no_edit)
752 launch_editor(git_path(commit_editmsg), &sb);
599 - else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0)
753 + else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
754 + rollback_index_files();
755 die("could not read commit message\n");
601 - if (run_hook(index_file, "commit-msg", git_path(commit_editmsg)))
756 + }
757 + if (run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
758 + rollback_index_files();
759 exit(1);
760 + }
761 stripspace(&sb, 1);
604 - if (sb.len < header_len ||
605 - message_is_empty(&sb, header_len))
762 + if (sb.len < header_len || message_is_empty(&sb, header_len)) {
763 + rollback_index_files();
764 die("* no commit message? aborting commit.");
765 + }
766 strbuf_addch(&sb, '\0');
767 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
768 fprintf(stderr, commit_utf8_warn);
769
611 - if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1))
770 + if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
771 + rollback_index_files();
772 die("failed to write commit object");
773 + }
774
775 ref_lock = lock_any_ref_for_update("HEAD",
776 initial_commit ? NULL : head_sha1,
@@ -624,21 +785,22 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
785 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
786 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
787
627 - if (!ref_lock)
788 + if (!ref_lock) {
789 + rollback_index_files();
790 die("cannot lock HEAD ref");
629 - if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0)
791 + }
792 + if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
793 + rollback_index_files();
794 die("cannot update HEAD ref");
795 + }
796
797 unlink(git_path("MERGE_HEAD"));
798 unlink(git_path("MERGE_MSG"));
799
635 - if (lock_file.filename[0] && commit_locked_index(&lock_file))
636 - die("failed to write new index");
800 + commit_index_files();
801
802 rerere();
639 -
640 - run_hook(index_file, "post-commit", NULL);
641 -
803 + run_hook(get_index_file(), "post-commit", NULL);
804 if (!quiet)
805 print_summary(prefix, commit_sha1);
806