@matheusbsbs / matheus-bagliano / commits / 64586e75af
Fork 0

git-commit: Allow partial commit of file removal.

When making a partial commit, git-commit uses git-ls-files with the --error-unmatch option to expand and sanity check the user supplied path patterns. When any path pattern does not match with the paths known to the index, it errors out, in order to catch a common mistake to say "git commit Makefiel cache.h" and end up with a commit that touches only cache.h (notice the misspelled "Makefile"). This detection however does not work well when the path has already been removed from the index. If you drop a path from the index and try to commit that partially, i.e. $ git rm COPYING $ git commit -m 'Remove COPYING' COPYING the command complains because git does not know anything about COPYING anymore. This introduces a new option --with-tree to git-ls-files and uses it in git-commit when we build a temporary index to write a tree object for the partial commit. When --with-tree=<tree-ish> option is specified, names from the given tree are added to the set of names the index knows about, so we can treat COPYING file in the example as known. Of course, there is no reason to use "git rm" and git-aware people have long time done: $ rm COPYING $ git commit -m 'Remove COPYING' COPYING which works just fine. But this caused a constant confusion. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 12, 2007 at 16:04 UTC 64586e75af3c84844b80652575a8b63a9612b24a
3 files changed +103 -1
builtin-ls-files.c
+78
@@ -9,6 +9,7 @@
9 #include "quote.h"
10 #include "dir.h"
11 #include "builtin.h"
12 +#include "tree.h"
13
14 static int abbrev;
15 static int show_deleted;
@@ -26,6 +27,7 @@ static int prefix_offset;
27 static const char **pathspec;
28 static int error_unmatch;
29 static char *ps_matched;
30 +static const char *with_tree;
31
32 static const char *tag_cached = "";
33 static const char *tag_unmerged = "";
@@ -247,6 +249,8 @@ static void show_files(struct dir_struct *dir, const char *prefix)
249 continue;
250 if (show_unmerged && !ce_stage(ce))
251 continue;
252 + if (ce->ce_flags & htons(CE_UPDATE))
253 + continue;
254 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
255 }
256 }
@@ -332,6 +336,67 @@ static const char *verify_pathspec(const char *prefix)
336 return real_prefix;
337 }
338
339 +/*
340 + * Read the tree specified with --with-tree option
341 + * (typically, HEAD) into stage #1 and then
342 + * squash them down to stage #0. This is used for
343 + * --error-unmatch to list and check the path patterns
344 + * that were given from the command line. We are not
345 + * going to write this index out.
346 + */
347 +static void overlay_tree(const char *tree_name, const char *prefix)
348 +{
349 + struct tree *tree;
350 + unsigned char sha1[20];
351 + const char **match;
352 + struct cache_entry *last_stage0 = NULL;
353 + int i;
354 +
355 + if (get_sha1(tree_name, sha1))
356 + die("tree-ish %s not found.", tree_name);
357 + tree = parse_tree_indirect(sha1);
358 + if (!tree)
359 + die("bad tree-ish %s", tree_name);
360 +
361 + /* Hoist the unmerged entries up to stage #3 to make room */
362 + for (i = 0; i < active_nr; i++) {
363 + struct cache_entry *ce = active_cache[i];
364 + if (!ce_stage(ce))
365 + continue;
366 + ce->ce_flags |= htons(CE_STAGEMASK);
367 + }
368 +
369 + if (prefix) {
370 + static const char *(matchbuf[2]);
371 + matchbuf[0] = prefix;
372 + matchbuf [1] = NULL;
373 + match = matchbuf;
374 + } else
375 + match = NULL;
376 + if (read_tree(tree, 1, match))
377 + die("unable to read tree entries %s", tree_name);
378 +
379 + for (i = 0; i < active_nr; i++) {
380 + struct cache_entry *ce = active_cache[i];
381 + switch (ce_stage(ce)) {
382 + case 0:
383 + last_stage0 = ce;
384 + /* fallthru */
385 + default:
386 + continue;
387 + case 1:
388 + /*
389 + * If there is stage #0 entry for this, we do not
390 + * need to show it. We use CE_UPDATE bit to mark
391 + * such an entry.
392 + */
393 + if (last_stage0 &&
394 + !strcmp(last_stage0->name, ce->name))
395 + ce->ce_flags |= htons(CE_UPDATE);
396 + }
397 + }
398 +}
399 +
400 static const char ls_files_usage[] =
401 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
402 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
@@ -452,6 +517,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
517 error_unmatch = 1;
518 continue;
519 }
520 + if (!prefixcmp(arg, "--with-tree=")) {
521 + with_tree = arg + 12;
522 + continue;
523 + }
524 if (!prefixcmp(arg, "--abbrev=")) {
525 abbrev = strtoul(arg+9, NULL, 10);
526 if (abbrev && abbrev < MINIMUM_ABBREV)
@@ -503,6 +572,15 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
572 read_cache();
573 if (prefix)
574 prune_cache(prefix);
575 + if (with_tree) {
576 + /*
577 + * Basic sanity check; show-stages and show-unmerged
578 + * would not make any sense with this option.
579 + */
580 + if (show_stage || show_unmerged)
581 + die("ls-files --with-tree is incompatible with -s or -u");
582 + overlay_tree(with_tree, prefix);
583 + }
584 show_files(&dir, prefix);
585
586 if (ps_matched) {
git-commit.sh
+4 -1
@@ -379,8 +379,11 @@ t,)
379 then
380 refuse_partial "Cannot do a partial commit during a merge."
381 fi
382 +
383 TMP_INDEX="$GIT_DIR/tmp-index$$"
383 - commit_only=`git ls-files --error-unmatch -- "$@"` || exit
384 + W=
385 + test -z "$initial_commit" && W=--with-tree=HEAD
386 + commit_only=`git ls-files --error-unmatch $W -- "$@"` || exit
387
388 # Build a temporary index and update the real index
389 # the same way.
t/t7501-commit.sh
+21
@@ -131,4 +131,25 @@ test_expect_success \
131 'validate git-rev-list output.' \
132 'diff current expected'
133
134 +test_expect_success 'partial commit that involve removal (1)' '
135 +
136 + git rm --cached file &&
137 + mv file elif &&
138 + git add elif &&
139 + git commit -m "Partial: add elif" elif &&
140 + git diff-tree --name-status HEAD^ HEAD >current &&
141 + echo "A elif" >expected &&
142 + diff expected current
143 +
144 +'
145 +
146 +test_expect_success 'partial commit that involve removal (2)' '
147 +
148 + git commit -m "Partial: remove file" file &&
149 + git diff-tree --name-status HEAD^ HEAD >current &&
150 + echo "D file" >expected &&
151 + diff expected current
152 +
153 +'
154 +
155 test_done