diff: support --root --cached combination
PATCH 1/1 diff: support --root --cached combination
---
I have a ritual of doing "git dic" (short for diff --cached) before committing and does not want to break it, even on new repos.
Looks like a good thing and no harm to the rest of the world.
builtin/diff.c | 9 +++++++-- t/t4046-diff-cached-root.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100755 t/t4046-diff-cached-root.sh
builtin/diff.c +7 -2
@@ -330,8 +330,13 @@ int cmd_diff(int argc, const char **argv, const char *prefix)else if (!strcmp (arg,"--cached" ) || !strcmp (arg,"--staged" )) {add_head_to_pending (&rev); -if (!rev.pending.nr) -die ("No HEAD commit to compare with (yet)" ); +if (!rev.pending.nr) { +struct object *obj; +if (!rev.show_root_diff) +die ("No HEAD commit to compare with (yet)" ); + obj = (struct object *)lookup_tree ((unsigned char *)EMPTY_TREE_SHA1_BIN ); +add_pending_object (&rev, obj,"HEAD" ); + }break ; } }
t/t4046-diff-cached-root.sh new file +31 -0
@@ -0,0 +1,31 @@ +#!/bin/sh + +test_description='git diff --cached --root test' + +. ./test-lib.sh + +test_expect_success 'setup' ' + echo cached >file && + git add file && + echo not-cached >>file +' + +test_expect_success 'diff --cached' ' + test_must_fail git diff --cached +' + +test_expect_success 'diff --cached --root' ' + git diff --cached --root >result && + cat >expected <<\EOF && +diff --git a/file b/file +new file mode 100644 +index 0000000..ec9a961 +--- /dev/null ++++ b/file +@@ -0,0 +1 @@ ++cached +EOF + test_cmp expected result +' + +test_done
Signature · 1 line
1.7.0.2.445.gcbdb3
Quoted 4 lines · Nguyễn Thái Ngọc Duy
I have a ritual of doing "git dic" (short for diff --cached) before committing and does not want to break it, even on new repos.
Looks like a good thing and no harm to the rest of the world.
This explanation belongs in the commit message, methinks.
builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 336
Quoted 12 lines
builtin/diff.c +4
-2@@ -330,8 +330,13 @@ int cmd_diff(int argc, const char **argv, const char *prefix)else if (!strcmp (arg,"--cached" ) || !strcmp (arg,"--staged" )) {add_head_to_pending (&rev); -if (!rev.pending.nr) -die ("No HEAD commit to compare with (yet)" ); +if (!rev.pending.nr) { +struct object *obj; +if (!rev.show_root_diff) +die ("No HEAD commit to compare with (yet)" );
How does this condition get tripped? The code allowing "[log] showroot" to be set to false is only invoked by the log family of commands.
Using --root as the backward-compatibility option seems like an abuse of language, anyway. "git diff --cached" has two meanings:
1. show changes to be committed
1b. show what git show --format=" " would say after a commit
2. show differences between the index and the commit named by the (implicit) HEAD argument
With interpretation (1b), --root should be respected, and the output should be empty (!), not an error, when "[log] showroot" is false.
With interpretation (2), --root should not be respected, and an attempt to diff --cached in an unborn branch should be an error.
(1a) and (1b) are the only useful interpretations. So for simplicity, would it make sense to drop the "if ()" for --root and make
builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 339
Quoted 3 lines
builtin/diff.c +3
-0+test_expect_success 'diff --cached' ' + test_must_fail git diff --cached +'
fail?
builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 337
Quoted 1 line
builtin/diff.c +1
-0+ obj = (struct object *)lookup_tree ((unsigned char *)EMPTY_TREE_SHA1_BIN );
struct tree *tree = lookup_tree((const unsigned char *) ... obj = &tree->object;
might be more clear (and robust against future layout changes).
Quoted 17 lines · Jonathan Nieder
Using --root as the backward-compatibility option seems like an abuse of language, anyway. "git diff --cached" has two meanings:
1. show changes to be committed
1b. show what git show --format=" " would say after a commit
2. show differences between the index and the commit named by the (implicit) HEAD argument
With interpretation (1b), --root should be respected, and the output should be empty (!), not an error, when "[log] showroot" is false.
With interpretation (2), --root should not be respected, and an attempt to diff --cached in an unborn branch should be an error.
Quoted 8 lines
(1a) and (1b) are the only useful interpretations. So for simplicity, would it make sense to drop the "if ()" for --root and make
t/t4046-diff-cached-root.sh line 15
Quoted 3 lines
t/t4046-diff-cached-root.sh new file +3
-0+test_expect_success 'diff --cached' ' + test_must_fail git diff --cached +'fail?
I don't see 1a up above, but I agree.
Let's explain the patch this way (yes, I am writing a proposed commit log message Duy should have written):
"git diff --cached" (without revision) used to mean "git diff --cached HEAD" (i.e. the user was too lazy to type HEAD). This "correctly" failed when there was no commit yet. But was that correctness useful?
This patch changes the definition of what particular command means. It is a request to show what _would_ be committed without further "git add". The internal implementation is still the same "git diff --cached HEAD" when HEAD exists, but when there is no commit yet, it compares the index with an empty tree object to achieve the desired result.
Unlike "diff-index --cached HEAD" that must fail when HEAD does not name a valid rev, we do not have to be so strict in "git diff" Porcelain, especially when the end user does not even explicitly say HEAD.
To put it in another way, we should strive to define the behaviour of the plumbing precisely in terms of the mechanism and machinery (e.g. if you ask for an operation between a tree and the index, and if you incorrectly specified the tree, you _should_ get an error, instead of a result that somebody randomly chose, saying "we thought it would be more useful for you this way"). But we should try to define the behaviour of the Porcelain commands in terms of the use case and the workflow we try to encourage and support.
PATCH v2 1/1 diff: support --cached on unborn branches
"git diff --cached" (without revision) used to mean "git diff --cached HEAD" (i.e. the user was too lazy to type HEAD). This "correctly" failed when there was no commit yet. But was that correctness useful?
This patch changes the definition of what particular command means. It is a request to show what _would_ be committed without further "git add". The internal implementation is still the same "git diff --cached HEAD" when HEAD exists, but when there is no commit yet, it compares the index with an empty tree object to achieve the desired result.
(Written by Junio)
---
"git diff --cached HEAD" does fail, but I don't really care.
builtin/diff.c | 7 ++++- t/t4013-diff-various.sh | 11 ++++++++++ t/t4013/diff.diff_--cached | 38 +++++++++++++++++++++++++++++++++++ t/t4013/diff.diff_--cached_--_file0 | 15 +++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 t/t4013/diff.diff_--cached create mode 100644 t/t4013/diff.diff_--cached_--_file0
builtin/diff.c +5 -2
@@ -330,8 +330,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)else if (!strcmp (arg,"--cached" ) || !strcmp (arg,"--staged" )) {add_head_to_pending (&rev); -if (!rev.pending.nr) -die ("No HEAD commit to compare with (yet)" ); +if (!rev.pending.nr) { +struct tree *tree; + tree =lookup_tree ((const unsigned char *)EMPTY_TREE_SHA1_BIN ); +add_pending_object (&rev, &tree->object,"HEAD" ); + }break ; } }
t/t4013-diff-various.sh +11 -0
@@ -290,4 +290,15 @@ test_expect_success 'log -S requires an argument' ' test_must_fail git log -S ' +test_expect_success 'diff --cached on unborn branch' ' + echo ref: refs/heads/unborn >.git/HEAD && + git diff --cached >result && + test_cmp "$TEST_DIRECTORY/t4013/diff.diff_--cached" result +' + +test_expect_success 'diff --cached -- file on unborn branch' ' + git diff --cached -- file0 >result && + test_cmp "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" result +' + test_done
t/t4013/diff.diff_--cached new file +38 -0
@@ -0,0 +1,38 @@ +diff --git a/dir/sub b/dir/sub +new file mode 100644 +index 0000000..992913c +--- /dev/null ++++ b/dir/sub +@@ -0,0 +1,8 @@ ++A ++B ++C ++D ++E ++F ++1 ++2 +diff --git a/file0 b/file0 +new file mode 100644 +index 0000000..10a8a9f +--- /dev/null ++++ b/file0 +@@ -0,0 +1,9 @@ ++1 ++2 ++3 ++4 ++5 ++6 ++A ++B ++C +diff --git a/file1 b/file1 +new file mode 100644 +index 0000000..b1e6722 +--- /dev/null ++++ b/file1 +@@ -0,0 +1,3 @@ ++A ++B ++C
t/t4013/diff.diff_--cached_--_file0 new file +15 -0
@@ -0,0 +1,15 @@ +diff --git a/file0 b/file0 +new file mode 100644 +index 0000000..10a8a9f +--- /dev/null ++++ b/file0 +@@ -0,0 +1,9 @@ ++1 ++2 ++3 ++4 ++5 ++6 ++A ++B ++C
Signature · 1 line
1.7.0.2.445.gcbdb3
Re: [PATCH v2] diff: support --cached on unborn branches
Quoted 4 lines · Nguyễn Thái Ngọc Duy
"git diff --cached" (without revision) used to mean "git diff --cached HEAD" (i.e. the user was too lazy to type HEAD). This "correctly" failed when there was no commit yet. But was that correctness useful?
Quoted 8 lines
This patch changes the definition of what particular command means. It is a request to show what _would_ be committed without further "git add". The internal implementation is still the same "git diff --cached HEAD" when HEAD exists, but when there is no commit yet, it compares the index with an empty tree object to achieve the desired result.
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>
Will take a look at it, and queue. Thanks.
Quoted 1 line
"git diff --cached HEAD" does fail, but I don't really care.
I _do_ care, and so should you. And I think it _should_ fail, if the user explicitly asked to compare the index with HEAD that does not exist yet.
As we are updating the semantics of a Porcelain command, there should be an update that explains the new usage in the documentation, no?
Re: [PATCH v2] diff: support --cached on unborn branches
Quoted 17 lines · Junio C Hamano
Quoted 4 lines · Nguyễn Thái Ngọc Duy
"git diff --cached" (without revision) used to mean "git diff --cached HEAD" (i.e. the user was too lazy to type HEAD). This "correctly" failed when there was no commit yet. But was that correctness useful?
Quoted 8 lines
This patch changes the definition of what particular command means. It is a request to show what _would_ be committed without further "git add". The internal implementation is still the same "git diff --cached HEAD" when HEAD exists, but when there is no commit yet, it compares the index with an empty tree object to achieve the desired result.
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>Will take a look at it, and queue. Thanks.
Quoted 7 lines
Quoted 1 line
"git diff --cached HEAD" does fail, but I don't really care.
I _do_ care, and so should you. And I think it _should_ fail, if the user explicitly asked to compare the index with HEAD that does not exist yet.
As we are updating the semantics of a Porcelain command, there should be an update that explains the new usage in the documentation, no?
Yes.
--8<--
Documentation/git-diff.txt +3 -1
@@ -32,7 +32,9 @@ directories. This behavior can be forced by --no-index. This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you - do not give <commit>, it defaults to HEAD. + do not give <commit>, it defaults to HEAD. If HEAD does + not exist (e.g. unborn branches) and <commit> is not + given, it shows all staged changes. --staged is a synonym of --cached. 'git diff' [--options] <commit> [--] [<path>...]::
--8<--
Signature · 1 line
Duy
PATCH 1/1 diff: support --cached on unborn branches
"git diff --cached" (without revision) used to mean "git diff --cached HEAD" (i.e. the user was too lazy to type HEAD). This "correctly" failed when there was no commit yet. But was that correctness useful?
This patch changes the definition of what particular command means. It is a request to show what _would_ be committed without further "git add". The internal implementation is still the same "git diff --cached HEAD" when HEAD exists, but when there is no commit yet, it compares the index with an empty tree object to achieve the desired result.
(Written by Junio)
---
(Good) old stuff. I think it wasn't accepted because I was lazy in updating docs. Resend (with doc updates).
Documentation/git-diff.txt | 2 + builtin/diff.c | 7 ++++- t/t4013-diff-various.sh | 11 ++++++++++ t/t4013/diff.diff_--cached | 38 +++++++++++++++++++++++++++++++++++ t/t4013/diff.diff_--cached_--_file0 | 15 +++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 t/t4013/diff.diff_--cached create mode 100644 t/t4013/diff.diff_--cached_--_file0
Documentation/git-diff.txt +2 -0
@@ -38,6 +38,8 @@ directories. This behavior can be forced by --no-index. commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. + If HEAD does not exist (e.g. unborned branches) and + <commit> is not given, it shows all staged changes. --staged is a synonym of --cached. 'git diff' [--options] <commit> [--] [<path>...]::
builtin/diff.c +5 -2
@@ -330,8 +330,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)else if (!strcmp (arg,"--cached" ) || !strcmp (arg,"--staged" )) {add_head_to_pending (&rev); -if (!rev.pending.nr) -die ("No HEAD commit to compare with (yet)" ); +if (!rev.pending.nr) { +struct tree *tree; + tree =lookup_tree ((const unsigned char *)EMPTY_TREE_SHA1_BIN ); +add_pending_object (&rev, &tree->object,"HEAD" ); + }break ; } }
t/t4013-diff-various.sh +11 -0
@@ -290,4 +290,15 @@ test_expect_success 'log -S requires an argument' ' test_must_fail git log -S ' +test_expect_success 'diff --cached on unborn branch' ' + echo ref: refs/heads/unborn >.git/HEAD && + git diff --cached >result && + test_cmp "$TEST_DIRECTORY/t4013/diff.diff_--cached" result +' + +test_expect_success 'diff --cached -- file on unborn branch' ' + git diff --cached -- file0 >result && + test_cmp "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" result +' + test_done
t/t4013/diff.diff_--cached new file +38 -0
@@ -0,0 +1,38 @@ +diff --git a/dir/sub b/dir/sub +new file mode 100644 +index 0000000..992913c +--- /dev/null ++++ b/dir/sub +@@ -0,0 +1,8 @@ ++A ++B ++C ++D ++E ++F ++1 ++2 +diff --git a/file0 b/file0 +new file mode 100644 +index 0000000..10a8a9f +--- /dev/null ++++ b/file0 +@@ -0,0 +1,9 @@ ++1 ++2 ++3 ++4 ++5 ++6 ++A ++B ++C +diff --git a/file1 b/file1 +new file mode 100644 +index 0000000..b1e6722 +--- /dev/null ++++ b/file1 +@@ -0,0 +1,3 @@ ++A ++B ++C
t/t4013/diff.diff_--cached_--_file0 new file +15 -0
@@ -0,0 +1,15 @@ +diff --git a/file0 b/file0 +new file mode 100644 +index 0000000..10a8a9f +--- /dev/null ++++ b/file0 +@@ -0,0 +1,9 @@ ++1 ++2 ++3 ++4 ++5 ++6 ++A ++B ++C
Signature · 1 line
1.7.3.4.878.g439c7
2010/10/29 Jonathan Nieder <[email protected]>:
Quoted 17 lines
Quoted 4 lines
builtin/diff.c +0
-0@@ -330,8 +330,13 @@ int cmd_diff(int argc, const char **argv, const char *prefix)else if (!strcmp(arg, "--cached") ||
Quoted 8 lines
!strcmp(arg, "--staged")) {
add_head_to_pending(&rev);
- if (!rev.pending.nr)
-die("No HEAD commit to compare with (yet)");
+ if (!rev.pending.nr) {
+ struct object *obj;
+ if (!rev.show_root_diff)
+die("No HEAD commit to compare with (yet)");How does this condition get tripped? The code allowing "[log] showroot" to be set to false is only invoked by the log family of commands.
Quoted 2 lines
Using --root as the backward-compatibility option seems like an abuse of language, anyway.
Hmm.. I thought --root was supported by all diff command family. Now I think of it, only "diff-tree --root" makes sense.
Quoted 6 lines
"git diff --cached" has two meanings:
1. show changes to be committed
1b. show what git show --format=" " would say after a commit
Quoted 8 lines
2. show differences between the index and the commit named by the (implicit) HEAD argument
With interpretation (1b), --root should be respected, and the output should be empty (!), not an error, when "[log] showroot" is false.
With interpretation (2), --root should not be respected, and an attempt to diff --cached in an unborn branch should be an error.
If you commit to an unborn branch, it would become the first commit of that branch. So by (1a), it should show what is to be commited, isn't it?
Quoted 9 lines
(1a) and (1b) are the only useful interpretations. So for simplicity, would it make sense to drop the "if ()" for --root and make
Quoted 3 lines
+test_expect_success 'diff --cached' '
+test_must_failgit diff --cached
+'fail?
All for simpler, yes.
Quoted 8 lines
Quoted 2 lines
+ obj = (struct object*)lookup_tree((unsigned char*)EMPTY_TREE_SHA1_BIN);
struct tree *tree = lookup_tree((const unsigned char *) ... obj =
&tree->object;might be more clear (and robust against future layout changes).
OK
Signature · 1 line
Duy
Quoted 7 lines · Nguyễn Thái Ngọc Duy
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>---
I have a ritual of doing "git dic" (short for diff --cached) before committing and does not want to break it, even on new repos.Looks like a good thing and no harm to the rest of the world.
Hmm. What's new is old, I suppose. You might want to read the comments on my very similar patch here:
http://thread.gmane.org/gmane.comp.version-control.git/95935/focus=96187
-Peff