git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCH] Fix git-diff --cached to not error out if HEAD points to a nonexistant branch

From
PBPeter Baumann <waste.manager@gmx.de>
Date
Feb 24, 2007, 17:20 UTC
Message-ID
<20070224172037.GA31963@xp.machine.xx>

The documentation mentions "git-diff --cached" to see what is staged for the next commit. But this failes if you haven't done any commits yet. So lets fix it.

Signed-off-by: Peter Baumann <siprbaum@stud.informatik.uni-erlangen.de>
---

I was bitten by this during explaining a total git newbie the index and the several ways to diff index <-> wd, index <-> commit, commit <-> commit. I posted this example

 mkdir testrepo && cd testrepo
 git init
 echo foo > test.txt
 git add test.txt
 git diff --cached
 usage: git-diff <options> <rev>{0,2} -- <path>*

and was totaly shocked to see the above error message. I am not sure if this is the right fix and/or if git-diff-index should also be fixed. I decided against it and let the core cmd git-diff-index stay as it is now.

 builtin-diff.c         |   22 +++++++++++++++++++---
 t/t4017-diff-cached.sh |   21 +++++++++++++++++++++
 2 files changed, 40 insertions(+), 3 deletions(-)
 create mode 100755 t/t4017-diff-cached.sh
diff --git a/builtin-diff.c b/builtin-diff.c
index c387ebb..aace507 100644
--- a/builtin-diff.c
+++ b/builtin-diff.c
@@ -203,11 +203,27 @@ void add_head(struct rev_info *revs)
 {
 	unsigned char sha1[20];
 	struct object *obj;
-	if (get_sha1("HEAD", sha1))
+	int ret = get_sha1("HEAD", sha1);
+	if (ret > 0)
 		return;
-	obj = parse_object(sha1);
-	if (!obj)
+
+	if (ret == 0)
+		obj = parse_object(sha1);
+	else {
+		/* HEAD exists but the branch it points to does not;
+		 * we haven't done any commit yet => create an empty tree
+		 * to make git diff --cached work
+		 */
+		obj = xcalloc(1, sizeof(struct object));
+	        obj->parsed = 1;
+		obj->type = OBJ_TREE;
+
+		pretend_sha1_file(NULL, 0, tree_type, obj->sha1);
+	}
+
+	if (!obj) {
 		return;
+	}
 	add_pending_object(revs, obj, "HEAD");
 }
 
diff --git a/t/t4017-diff-cached.sh b/t/t4017-diff-cached.sh
new file mode 100755
index 0000000..39fc32f
--- /dev/null
+++ b/t/t4017-diff-cached.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Peter Baumann
+#
+
+test_description='Test diff --cached without inital commit.
+
+'
+. ./test-lib.sh
+
+test_expect_success \
+    'setup' \
+    'echo frotz >rezrov &&
+     git-update-index --add rezrov'
+
+test_expect_success \
+    'git-diff --cached' \
+    'git-diff --cached'
+
+test_done
+
-- 
1.5.0.1.213.g509b-dirty
Next: Junio C Hamano
Message 1 of 8 in “Fix git-diff --cached to not error out if HEAD points to a nonexistant branch”
  1. Fix git-diff --cached to not error out if HEAD points to a nonexistant branchPeter Baumann, Feb 24, 2007
  2. Junio C HamanoFeb 24, 2007
  3. Peter BaumannFeb 24, 2007
  4. Junio C HamanoFeb 25, 2007
  5. Peter BaumannFeb 25, 2007
  6. Junio C HamanoFeb 25, 2007
  7. Peter BaumannFeb 25, 2007
  8. Peter BaumannFeb 25, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.