Skip to content

Commit 0403660

Browse files
committed
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>
1 parent 0569e9b commit 0403660

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

diff-no-index.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ static int queue_diff(struct diff_options *o,
144144
}
145145
}
146146

147+
static int path_outside_repo(const char *path)
148+
{
149+
/*
150+
* We have already done setup_git_directory_gently() so we
151+
* know we are inside a git work tree already.
152+
*/
153+
const char *work_tree;
154+
size_t len;
155+
156+
if (!is_absolute_path(path))
157+
return 0;
158+
work_tree = get_git_work_tree();
159+
len = strlen(work_tree);
160+
if (strncmp(path, work_tree, len) ||
161+
(path[len] != '\0' && path[len] != '/'))
162+
return 1;
163+
return 0;
164+
}
165+
147166
void diff_no_index(struct rev_info *revs,
148167
int argc, const char **argv,
149168
int nongit, const char *prefix)
@@ -162,13 +181,19 @@ void diff_no_index(struct rev_info *revs,
162181
break;
163182
}
164183

165-
/*
166-
* No explicit --no-index, but "git diff --opts A B" outside
167-
* a git repository is a cute hack to support.
168-
*/
169-
if (!no_index && !nongit)
170-
return;
171-
184+
if (!no_index && !nongit) {
185+
/*
186+
* Inside a git repository, without --no-index. Only
187+
* when a path outside the repository is given,
188+
* e.g. "git diff /var/tmp/[12]", or "git diff
189+
* Makefile /var/tmp/Makefile", allow it to be used as
190+
* a colourful "diff" replacement.
191+
*/
192+
if ((argc != i + 2) ||
193+
(!path_outside_repo(argv[i]) &&
194+
!path_outside_repo(argv[i+1])))
195+
return;
196+
}
172197
if (argc != i + 2)
173198
die("git diff %s takes two paths",
174199
no_index ? "--no-index" : "[--no-index]");

0 commit comments

Comments
 (0)