summaryrefslogtreecommitdiff
path: root/diff-no-index.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2008-05-27 04:54:23 (GMT)
committerJunio C Hamano <gitster@pobox.com>2008-05-27 04:54:23 (GMT)
commit040366076fa0dc540309531997a987cc6d83a577 (patch)
tree42da27189fb3a58ce2da5d202240aa8e6aa5f20c /diff-no-index.c
parent0569e9b8cea20d5eedfec66730a9711a0907ab0d (diff)
downloadgit-040366076fa0dc540309531997a987cc6d83a577.zip
git-040366076fa0dc540309531997a987cc6d83a577.tar.gz
git-040366076fa0dc540309531997a987cc6d83a577.tar.bz2
git-diff: allow --no-index semantics a bit more
Even when inside a git work tree, if two paths are given and at least one is clearly outside the work tree, it cannot be a request to diff a tracked path anyway; allow such an invocation to use --no-index semantics. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'diff-no-index.c')
-rw-r--r--diff-no-index.c39
1 files changed, 32 insertions, 7 deletions
diff --git a/diff-no-index.c b/diff-no-index.c
index 1b57fee..b1ae791 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -144,6 +144,25 @@ static int queue_diff(struct diff_options *o,
}
}
+static int path_outside_repo(const char *path)
+{
+ /*
+ * We have already done setup_git_directory_gently() so we
+ * know we are inside a git work tree already.
+ */
+ const char *work_tree;
+ size_t len;
+
+ if (!is_absolute_path(path))
+ return 0;
+ work_tree = get_git_work_tree();
+ len = strlen(work_tree);
+ if (strncmp(path, work_tree, len) ||
+ (path[len] != '\0' && path[len] != '/'))
+ return 1;
+ return 0;
+}
+
void diff_no_index(struct rev_info *revs,
int argc, const char **argv,
int nongit, const char *prefix)
@@ -162,13 +181,19 @@ void diff_no_index(struct rev_info *revs,
break;
}
- /*
- * No explicit --no-index, but "git diff --opts A B" outside
- * a git repository is a cute hack to support.
- */
- if (!no_index && !nongit)
- return;
-
+ if (!no_index && !nongit) {
+ /*
+ * Inside a git repository, without --no-index. Only
+ * when a path outside the repository is given,
+ * e.g. "git diff /var/tmp/[12]", or "git diff
+ * Makefile /var/tmp/Makefile", allow it to be used as
+ * a colourful "diff" replacement.
+ */
+ if ((argc != i + 2) ||
+ (!path_outside_repo(argv[i]) &&
+ !path_outside_repo(argv[i+1])))
+ return;
+ }
if (argc != i + 2)
die("git diff %s takes two paths",
no_index ? "--no-index" : "[--no-index]");