path: root/diff-no-index.c
diff options
authorJeff King <>2016-09-13 03:23:32 (GMT)
committerJunio C Hamano <>2016-09-13 22:45:45 (GMT)
commit7d8930d903fd8b775a1db63bcf373df5ac5af0be (patch)
tree1b1270925f748b945d1865946835488ad4b06109 /diff-no-index.c
parent475b362c2a326ddc9987ee0ff6448e795f009d5d (diff)
diff: handle --no-index prefixes consistently
If we see an explicit "git diff --no-index ../foo ../bar", then we do not set up the git repository at all (we already know we are in --no-index mode, so do not have to check "are we in a repository?"), and hence have no "prefix" within the repository. A patch generated by this command will have the filenames "a/../foo" and "b/../bar", no matter which directory we are in with respect to any repository. However, in the implicit case, where we notice that the files are outside the repository, we will have chdir()'d to the top-level of the repository. We then feed the prefix back to the diff machinery. As a result, running the same diff from a subdirectory will result in paths that look like "a/subdir/../../foo". Besides being unnecessarily long, this may also be confusing to the user: they don't care about the subdir or the repository at all; it's just where they happened to be when running the command. We should treat this the same as the explicit --no-index case. One way to address this would be to chdir() back to the original path before running our diff. However, that's a bit hacky, as we would also need to adjust $GIT_DIR, which could be a relative path from our top-level. Instead, we can reuse the diff machinery's RELATIVE_NAME option, which automatically strips off the prefix. Note that this _also_ restricts the diff to this relative prefix, but that's OK for our purposes: we queue our own diff pairs manually, and do not rely on that part of the diff code. Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'diff-no-index.c')
1 files changed, 3 insertions, 0 deletions
diff --git a/diff-no-index.c b/diff-no-index.c
index 03daadb..59237a6 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -282,6 +282,9 @@ void diff_no_index(struct rev_info *revs,
DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
+ DIFF_OPT_SET(&revs->diffopt, RELATIVE_NAME);
+ revs->diffopt.prefix = prefix;
revs->max_count = -2;