path: root/builtin/diff.c
diff options
authorNguyễn Thái Ngọc Duy <>2011-02-03 06:23:34 (GMT)
committerJunio C Hamano <>2011-02-07 23:04:30 (GMT)
commita2b7a3b3a966c5801efe968d4b5656fa5a885409 (patch)
tree10fa9f1407f71ccec95459533dddf9c956f3559d /builtin/diff.c
parent2e9c8789b7b0d4d3cd2f951e1694387686e2d740 (diff)
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 <> Signed-off-by: Nguyễn Thái Ngọc Duy <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/diff.c')
1 files changed, 5 insertions, 2 deletions
diff --git a/builtin/diff.c b/builtin/diff.c
index 945e758..42822cd 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -330,8 +330,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
else if (!strcmp(arg, "--cached") ||
!strcmp(arg, "--staged")) {
- if (!
- die("No HEAD commit to compare with (yet)");
+ if (! {
+ struct tree *tree;
+ tree = lookup_tree((const unsigned char*)EMPTY_TREE_SHA1_BIN);
+ add_pending_object(&rev, &tree->object, "HEAD");
+ }