diff: make more cases implicit --no-index
When specifying an absolute path, or a relative path pointing outside the working tree, do not fail, but roll your own diffopt parsing, and execute a --no-index diff. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Feb 25, 2007 at 23:35 UTC
fcfa33ec905fcde1c16e7cbbe00d7147b89f1f01
4 files changed
+64
-2
builtin-diff-files.c
+4
-1
@@ -23,7 +23,10 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
23
git_config(git_default_config); /* no "diff" UI options */
24
rev.abbrev = 0;
25
26
- argc = setup_revisions(argc, argv, &rev, NULL);
26
+ if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
27
+ argc = 0;
28
+ else
29
+ argc = setup_revisions(argc, argv, &rev, NULL);
30
if (!rev.diffopt.output_format)
31
rev.diffopt.output_format = DIFF_FORMAT_RAW;
32
return run_diff_files_cmd(&rev, argc, argv);
builtin-diff.c
+4
-1
@@ -215,7 +215,10 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
215
git_config(git_diff_ui_config);
216
init_revisions(&rev, prefix);
217
218
- argc = setup_revisions(argc, argv, &rev, NULL);
218
+ if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
219
+ argc = 0;
220
+ else
221
+ argc = setup_revisions(argc, argv, &rev, NULL);
222
if (!rev.diffopt.output_format) {
223
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
224
if (diff_setup_done(&rev.diffopt) < 0)
diff-lib.c
+54
@@ -200,6 +200,60 @@ static int handle_diff_files_args(struct rev_info *revs,
200
return 0;
201
}
202
203
+static int is_outside_repo(const char *path, int nongit, const char *prefix)
204
+{
205
+ int i;
206
+ if (nongit || !strcmp(path, "-") || path[0] == '/')
207
+ return 1;
208
+ if (prefixcmp(path, "../"))
209
+ return 0;
210
+ if (!prefix)
211
+ return 1;
212
+ for (i = strlen(prefix); !prefixcmp(path, "../"); ) {
213
+ while (i > 0 && prefix[i - 1] != '/')
214
+ i--;
215
+ if (--i < 0)
216
+ return 1;
217
+ path += 3;
218
+ }
219
+ return 0;
220
+}
221
+
222
+int setup_diff_no_index(struct rev_info *revs,
223
+ int argc, const char ** argv, int nongit, const char *prefix)
224
+{
225
+ int i;
226
+ for (i = 1; i < argc; i++)
227
+ if (argv[i][0] != '-')
228
+ break;
229
+ else if (!strcmp(argv[i], "--")) {
230
+ i++;
231
+ break;
232
+ } else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) {
233
+ i = argc - 3;
234
+ break;
235
+ }
236
+ if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) &&
237
+ !is_outside_repo(argv[i], nongit, prefix)))
238
+ return -1;
239
+
240
+ diff_setup(&revs->diffopt);
241
+ for (i = 1; i < argc - 2; )
242
+ if (!strcmp(argv[i], "--no-index"))
243
+ i++;
244
+ else {
245
+ int j = diff_opt_parse(&revs->diffopt,
246
+ argv + i, argc - i);
247
+ if (!j)
248
+ die("invalid diff option/value: %s", argv[i]);
249
+ i += j;
250
+ }
251
+ revs->diffopt.paths = argv + argc - 2;
252
+ revs->diffopt.nr_paths = 2;
253
+ revs->max_count = -2;
254
+ return 0;
255
+}
256
+
257
int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
258
{
259
int silent_on_removed;
diff.h
+2
@@ -222,6 +222,8 @@ extern void diff_flush(struct diff_options*);
222
extern const char *diff_unique_abbrev(const unsigned char *, int);
223
224
extern int run_diff_files(struct rev_info *revs, int silent_on_removed);
225
+extern int setup_diff_no_index(struct rev_info *revs,
226
+ int argc, const char ** argv, int nongit, const char *prefix);
227
extern int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv);
228
229
extern int run_diff_index(struct rev_info *revs, int cached);