git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH] diff: support --root --cached combination
Date: Fri, 29 Oct 2010 16:54:47 +0700	[thread overview]
Message-ID: <1288346087-20263-1-git-send-email-pclouds@gmail.com> (raw)


Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 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

diff --git a/builtin/diff.c b/builtin/diff.c
index a43d326..45be6f3 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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;
 			}
 		}
diff --git a/t/t4046-diff-cached-root.sh b/t/t4046-diff-cached-root.sh
new file mode 100755
index 0000000..82e3ad8
--- /dev/null
+++ b/t/t4046-diff-cached-root.sh
@@ -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
-- 
1.7.0.2.445.gcbdb3

             reply	other threads:[~2010-10-29  9:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-29  9:54 Nguyễn Thái Ngọc Duy [this message]
2010-10-29 10:19 ` [PATCH] diff: support --root --cached combination Jonathan Nieder
2010-10-29 11:00   ` Nguyen Thai Ngoc Duy
2010-10-29 17:06   ` Junio C Hamano
2010-10-30 11:16     ` [PATCH v2] diff: support --cached on unborn branches Nguyễn Thái Ngọc Duy
2010-10-31  3:12       ` Junio C Hamano
2010-10-31  3:24         ` Nguyen Thai Ngoc Duy
2011-02-03  6:23           ` [PATCH] " Nguyễn Thái Ngọc Duy
2010-10-29 15:40 ` [PATCH] diff: support --root --cached combination Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1288346087-20263-1-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).