Skip to content

Commit 64586e7

Browse files
committed
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>
1 parent a017f27 commit 64586e7

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

builtin-ls-files.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "quote.h"
1010
#include "dir.h"
1111
#include "builtin.h"
12+
#include "tree.h"
1213

1314
static int abbrev;
1415
static int show_deleted;
@@ -26,6 +27,7 @@ static int prefix_offset;
2627
static const char **pathspec;
2728
static int error_unmatch;
2829
static char *ps_matched;
30+
static const char *with_tree;
2931

3032
static const char *tag_cached = "";
3133
static const char *tag_unmerged = "";
@@ -247,6 +249,8 @@ static void show_files(struct dir_struct *dir, const char *prefix)
247249
continue;
248250
if (show_unmerged && !ce_stage(ce))
249251
continue;
252+
if (ce->ce_flags & htons(CE_UPDATE))
253+
continue;
250254
show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
251255
}
252256
}
@@ -332,6 +336,67 @@ static const char *verify_pathspec(const char *prefix)
332336
return real_prefix;
333337
}
334338

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+
335400
static const char ls_files_usage[] =
336401
"git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
337402
"[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
@@ -452,6 +517,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
452517
error_unmatch = 1;
453518
continue;
454519
}
520+
if (!prefixcmp(arg, "--with-tree=")) {
521+
with_tree = arg + 12;
522+
continue;
523+
}
455524
if (!prefixcmp(arg, "--abbrev=")) {
456525
abbrev = strtoul(arg+9, NULL, 10);
457526
if (abbrev && abbrev < MINIMUM_ABBREV)
@@ -503,6 +572,15 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
503572
read_cache();
504573
if (prefix)
505574
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+
}
506584
show_files(&dir, prefix);
507585

508586
if (ps_matched) {

git-commit.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,11 @@ t,)
379379
then
380380
refuse_partial "Cannot do a partial commit during a merge."
381381
fi
382+
382383
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
384387

385388
# Build a temporary index and update the real index
386389
# the same way.

t/t7501-commit.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,25 @@ test_expect_success \
131131
'validate git-rev-list output.' \
132132
'diff current expected'
133133

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+
134155
test_done

0 commit comments

Comments
 (0)