Skip to contentSkip to conversation

committing selected 'changed' or 'added' files works, but not 'removed'

DISCUSSION Jim Meyering 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hello,

If I've just run "git-rm -f c" in a directory with a few modified or git-added files, is there some way to commit (non-interactively) only the removal of that selected file? I.e., not any other changes in the working directory? git-commit -m. -- c fails with this:

error: pathspec 'c' did not match any file(s) known to git

If I use "cg-commit" instead of "git-commit", it works fine.

When I asked on IRC, "cehteh" suggested to create a temporary branch, do the removal there, and then to rebase that change back onto the original branch -- and to remove the disposable branch. Yes, that works, but I want do the job with a single commit command, the same way I can for "added" and "modified" files. And I've been trying (until now, successfully) to wean myself away from cogito.

Why should "removed" files be handled so differently? If I cannot commit a selected "file removal" (regardless of the state of the index), then isn't that an opportunity to add a feature?

In case you're wondering, I want this functionality in order to make a version control agnostic commit/diff/ChangeLog tool work the same way with raw git as it did using cogito.

-------------------------------------------
In case an actual scenario helps, I'd like to be able to commit the removal of "c", below without also committing the change to "b":

mkdir .j && cd .j && git-init > /dev/null && touch b c && git-add b c \
&& git-commit -q -m. && echo > b && git-rm --quiet -f c \
&& git-commit -m. -- c

The final commit above fails like this:

error: pathspec 'c' did not match any file(s) known to git. Did you forget to 'git add'?

#1 lore raw
Quoted 3 lines · Jim Meyering

Why should "removed" files be handled so differently? If I cannot commit a selected "file removal" (regardless of the state of the index), then isn't that an opportunity to add a feature?

The answer is because it is a bit cumbersome to arrange, and people who felt the need were too lazy to add that. And everybody knows that I am not from the "partial commit" camp.

You could do something like this...

NOTE NOTE NOTE.

I am not quite happy with this one, as it exposes one of my favorite pet peeves -- wildcard pathspecs behave differently between diff-tree family and ls-files family. After modifying a random C source file, you can say:

git commit -m 'C files changed' -- '*.c'

but you cannot say:

git commit -m 'C files modified and/or removed' -- '*.c'

after removing a C source file, because diff-tree's pathspec only works as top-down, subdirectory limiter.

-- >8 --
git-commit: Allow removal to be partially committed as well

We allow partial commit of modified and added files but never handled removed files. This hacks it around.

---

 git-commit.sh     |   36 ++++++++++++++++++++++++++++++-
 t/t7400-commit.sh |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 1 deletions(-)
git-commit.sh +35 -1
index f28fc24..f4ba0ef 100755
@@ -59,6 +59,40 @@ run_status () {
 		${untracked_files:+--untracked}
 }
 
+compute_commit_only () {
+
+	# The first one cannot commit removal
+	if test -n "$initial_commit"
+	then
+		exec git-ls-files --error-unmatch -- "$@"
+	fi
+
+	# Usual case -- no unmatch
+	if files=$(git-ls-files --error-unmatch -- "$@" 2>/dev/null)
+	then
+		echo "$files"
+		exit 0
+	fi
+
+	has_unmatch_errs=
+	# Otherwise we need to do it the hard way
+	for p in "$@"
+	do
+		removed=$(git-diff-index --cached --name-only \
+			--diff-filter=D HEAD -- "$p")
+		if test -n "$removed"
+		then
+			echo "$removed"
+		elif git-ls-files --error-unmatch -- "$p"
+		then
+			: ok so far
+		else
+			has_unmatch_errs=t
+		fi
+	done
+	test -z "$has_unmatch_errs"
+}
+
 trap '
 	test -z "$TMP_INDEX" || {
 		test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
@@ -364,7 +398,7 @@ t,)
 			refuse_partial "Cannot do a partial commit during a merge."
 		fi
 		TMP_INDEX="$GIT_DIR/tmp-index$$"
-		commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
+		commit_only=$( (compute_commit_only "$@") ) || exit
 
 		# Build a temporary index and update the real index
 		# the same way.
t/t7400-commit.sh new file +60 -0

60 lines not on this page · raw message

#2 lore raw
Quoted 1 line · Junio C Hamano

You could do something like this...

...

Quoted 5 lines

-- >8 --
git-commit: Allow removal to be partially committed as well

We allow partial commit of modified and added files but never handled removed files. This hacks it around.

Nice! Thanks for the quick patch.
I liked the test cases :-)