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 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: Junio C Hamano <gitster@pobox.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
2e9c8789b7
commit
a2b7a3b3a9
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user