diff options
Diffstat (limited to 'diff-no-index.c')
| -rw-r--r-- | diff-no-index.c | 71 |
1 files changed, 41 insertions, 30 deletions
diff --git a/diff-no-index.c b/diff-no-index.c index 0ed5f0f496..a3cf358baf 100644 --- a/diff-no-index.c +++ b/diff-no-index.c @@ -13,7 +13,7 @@ #include "diffcore.h" #include "revision.h" #include "log-tree.h" -#include "builtin.h" +#include "parse-options.h" #include "string-list.h" #include "dir.h" @@ -25,9 +25,8 @@ static int read_directory_contents(const char *path, struct string_list *list) if (!(dir = opendir(path))) return error("Could not open directory %s", path); - while ((e = readdir(dir))) - if (!is_dot_or_dotdot(e->d_name)) - string_list_insert(list, e->d_name); + while ((e = readdir_skip_dot_and_dotdot(dir))) + string_list_insert(list, e->d_name); closedir(dir); return 0; @@ -82,7 +81,7 @@ static struct diff_filespec *noindex_filespec(const char *name, int mode) if (!name) name = "/dev/null"; s = alloc_filespec(name); - fill_filespec(s, &null_oid, 0, mode); + fill_filespec(s, null_oid(), 0, mode); if (name == file_from_standard_input) populate_from_stdin(s); return s; @@ -233,32 +232,40 @@ static void fixup_paths(const char **path, struct strbuf *replacement) } } -void diff_no_index(struct rev_info *revs, - int argc, const char **argv) +static const char * const diff_no_index_usage[] = { + N_("git diff --no-index [<options>] <path> <path>"), + NULL +}; + +int diff_no_index(struct rev_info *revs, + int implicit_no_index, + int argc, const char **argv) { - int i; + int i, no_index; + int ret = 1; const char *paths[2]; + char *to_free[ARRAY_SIZE(paths)] = { 0 }; struct strbuf replacement = STRBUF_INIT; const char *prefix = revs->prefix; - - diff_setup(&revs->diffopt); - for (i = 1; i < argc - 2; ) { - int j; - if (!strcmp(argv[i], "--no-index")) - i++; - else if (!strcmp(argv[i], "--")) - i++; - else { - j = diff_opt_parse(&revs->diffopt, argv + i, argc - i, - revs->prefix); - if (j <= 0) - die("invalid diff option/value: %s", argv[i]); - i += j; - } + struct option no_index_options[] = { + OPT_BOOL_F(0, "no-index", &no_index, "", + PARSE_OPT_NONEG | PARSE_OPT_HIDDEN), + OPT_END(), + }; + struct option *options; + + options = add_diff_options(no_index_options, &revs->diffopt); + argc = parse_options(argc, argv, revs->prefix, options, + diff_no_index_usage, 0); + if (argc != 2) { + if (implicit_no_index) + warning(_("Not a git repository. Use --no-index to " + "compare two paths outside a working tree")); + usage_with_options(diff_no_index_usage, options); } - + FREE_AND_NULL(options); for (i = 0; i < 2; i++) { - const char *p = argv[argc - 2 + i]; + const char *p = argv[i]; if (!strcmp(p, "-")) /* * stdin should be spelled as "-"; if you have @@ -266,7 +273,7 @@ void diff_no_index(struct rev_info *revs, */ p = file_from_standard_input; else if (prefix) - p = prefix_filename(prefix, p); + p = to_free[i] = prefix_filename(prefix, p); paths[i] = p; } @@ -288,16 +295,20 @@ void diff_no_index(struct rev_info *revs, revs->diffopt.flags.exit_with_status = 1; if (queue_diff(&revs->diffopt, paths[0], paths[1])) - exit(1); + goto out; diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/"); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); - strbuf_release(&replacement); - /* * The return code for --no-index imitates diff(1): * 0 = no changes, 1 = changes, else error */ - exit(diff_result_code(&revs->diffopt, 0)); + ret = diff_result_code(&revs->diffopt, 0); + +out: + for (i = 0; i < ARRAY_SIZE(to_free); i++) + free(to_free[i]); + strbuf_release(&replacement); + return ret; } |
