diff options
author | Junio C Hamano <junkio@cox.net> | 2007-02-27 22:18:22 -0800 |
---|---|---|
committer | Junio C Hamano <junkio@cox.net> | 2007-02-27 22:18:22 -0800 |
commit | 25a0b20c742985f31a4694f2f98e8e0059765726 (patch) | |
tree | bac574dd85770c7017d446a97d5a6257cf209713 | |
parent | c4f8f827555dcbbee148fd1daa4821533ead2ea2 (diff) | |
parent | 34a5e1a2d900b5dd9b9c446a3db9fdb3b29e0575 (diff) | |
download | git-25a0b20c742985f31a4694f2f98e8e0059765726.tar.gz git-25a0b20c742985f31a4694f2f98e8e0059765726.tar.xz |
Merge branch 'js/diff-ni' (early part)
* 'js/diff-ni' (early part):
diff --no-index: also imitate the exit status of diff(1)
Fix typo: do not show name1 when name2 fails
Teach git-diff-files the new option `--no-index`
run_diff_{files,index}(): update calling convention.
update-index: do not die too early in a read-only repository.
git-status: do not be totally useless in a read-only repository.
-rw-r--r-- | Documentation/git-diff-files.txt | 5 | ||||
-rw-r--r-- | Documentation/git-diff.txt | 4 | ||||
-rw-r--r-- | builtin-diff-files.c | 29 | ||||
-rw-r--r-- | builtin-diff-index.c | 4 | ||||
-rw-r--r-- | builtin-diff.c | 42 | ||||
-rw-r--r-- | builtin-update-index.c | 11 | ||||
-rw-r--r-- | diff-lib.c | 226 | ||||
-rw-r--r-- | diff.c | 9 | ||||
-rw-r--r-- | diff.h | 4 | ||||
-rwxr-xr-x | git-commit.sh | 21 | ||||
-rw-r--r-- | git.c | 4 | ||||
-rw-r--r-- | wt-status.c | 12 |
12 files changed, 285 insertions, 86 deletions
diff --git a/Documentation/git-diff-files.txt b/Documentation/git-diff-files.txt index 7248b35d9..b78c4c64f 100644 --- a/Documentation/git-diff-files.txt +++ b/Documentation/git-diff-files.txt @@ -8,7 +8,7 @@ git-diff-files - Compares files in the working tree and the index SYNOPSIS -------- -'git-diff-files' [-q] [-0|-1|-2|-3|-c|--cc] [<common diff options>] [<path>...] +'git-diff-files' [-q] [-0|-1|-2|-3|-c|--cc|-n|--no-index] [<common diff options>] [<path>...] DESCRIPTION ----------- @@ -36,6 +36,9 @@ omit diff output for unmerged entries and just show "Unmerged". diff, similar to the way 'diff-tree' shows a merge commit with these flags. +\-n,\--no-index:: + Compare the two given files / directories. + -q:: Remain silent even on nonexistent files diff --git a/Documentation/git-diff.txt b/Documentation/git-diff.txt index 6a098df26..12a531d1e 100644 --- a/Documentation/git-diff.txt +++ b/Documentation/git-diff.txt @@ -23,6 +23,10 @@ tree and the index file, or the index file and the working tree. further add to the index but you still haven't. You can stage these changes by using gitlink:git-add[1]. + If exactly two paths are given, and at least one is untracked, + compare the two files / directories. This behavior can be + forced by --no-index. + 'git-diff' [--options] --cached [<commit>] [--] [<path>...]:: This form is to view the changes you staged for the next diff --git a/builtin-diff-files.c b/builtin-diff-files.c index 5d4a5c582..e1199f80a 100644 --- a/builtin-diff-files.c +++ b/builtin-diff-files.c @@ -10,42 +10,21 @@ #include "builtin.h" static const char diff_files_usage[] = -"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]" +"git-diff-files [-q] [-0/-1/2/3 |-c|--cc|-n|--no-index] [<common diff options>] [<path>...]" COMMON_DIFF_OPTIONS_HELP; int cmd_diff_files(int argc, const char **argv, const char *prefix) { struct rev_info rev; - int silent = 0; + int nongit = 0; + prefix = setup_git_directory_gently(&nongit); init_revisions(&rev, prefix); git_config(git_default_config); /* no "diff" UI options */ rev.abbrev = 0; argc = setup_revisions(argc, argv, &rev, NULL); - while (1 < argc && argv[1][0] == '-') { - if (!strcmp(argv[1], "--base")) - rev.max_count = 1; - else if (!strcmp(argv[1], "--ours")) - rev.max_count = 2; - else if (!strcmp(argv[1], "--theirs")) - rev.max_count = 3; - else if (!strcmp(argv[1], "-q")) - silent = 1; - else - usage(diff_files_usage); - argv++; argc--; - } if (!rev.diffopt.output_format) rev.diffopt.output_format = DIFF_FORMAT_RAW; - - /* - * Make sure there are NO revision (i.e. pending object) parameter, - * rev.max_count is reasonable (0 <= n <= 3), - * there is no other revision filtering parameters. - */ - if (rev.pending.nr || - rev.min_age != -1 || rev.max_age != -1) - usage(diff_files_usage); - return run_diff_files(&rev, silent); + return run_diff_files_cmd(&rev, argc, argv); } diff --git a/builtin-diff-index.c b/builtin-diff-index.c index 95a3db156..083599d5c 100644 --- a/builtin-diff-index.c +++ b/builtin-diff-index.c @@ -38,5 +38,9 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix) if (rev.pending.nr != 1 || rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1) usage(diff_cache_usage); + if (read_cache() < 0) { + perror("read_cache"); + return -1; + } return run_diff_index(&rev, cached); } diff --git a/builtin-diff.c b/builtin-diff.c index 67f49329b..28b660a78 100644 --- a/builtin-diff.c +++ b/builtin-diff.c @@ -25,40 +25,6 @@ struct blobinfo { static const char builtin_diff_usage[] = "git-diff <options> <rev>{0,2} -- <path>*"; -static int builtin_diff_files(struct rev_info *revs, - int argc, const char **argv) -{ - int silent = 0; - while (1 < argc) { - const char *arg = argv[1]; - if (!strcmp(arg, "--base")) - revs->max_count = 1; - else if (!strcmp(arg, "--ours")) - revs->max_count = 2; - else if (!strcmp(arg, "--theirs")) - revs->max_count = 3; - else if (!strcmp(arg, "-q")) - silent = 1; - else - usage(builtin_diff_usage); - argv++; argc--; - } - /* - * Make sure there are NO revision (i.e. pending object) parameter, - * specified rev.max_count is reasonable (0 <= n <= 3), and - * there is no other revision filtering parameter. - */ - if (revs->pending.nr || - revs->min_age != -1 || - revs->max_age != -1 || - 3 < revs->max_count) - usage(builtin_diff_usage); - if (revs->max_count < 0 && - (revs->diffopt.output_format & DIFF_FORMAT_PATCH)) - revs->combine_merges = revs->dense_combined_merges = 1; - return run_diff_files(revs, silent); -} - static void stuff_change(struct diff_options *opt, unsigned old_mode, unsigned new_mode, const unsigned char *old_sha1, @@ -151,6 +117,10 @@ static int builtin_diff_index(struct rev_info *revs, revs->max_count != -1 || revs->min_age != -1 || revs->max_age != -1) usage(builtin_diff_usage); + if (read_cache() < 0) { + perror("read_cache"); + return -1; + } return run_diff_index(revs, cached); } @@ -219,6 +189,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix) int ents = 0, blobs = 0, paths = 0; const char *path = NULL; struct blobinfo blob[2]; + int nongit = 0; /* * We could get N tree-ish in the rev.pending_objects list. @@ -240,6 +211,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix) * Other cases are errors. */ + prefix = setup_git_directory_gently(&nongit); git_config(git_diff_ui_config); init_revisions(&rev, prefix); @@ -317,7 +289,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix) if (!ents) { switch (blobs) { case 0: - return builtin_diff_files(&rev, argc, argv); + return run_diff_files_cmd(&rev, argc, argv); break; case 1: if (paths != 1) diff --git a/builtin-update-index.c b/builtin-update-index.c index 772aaba7b..65246dad8 100644 --- a/builtin-update-index.c +++ b/builtin-update-index.c @@ -487,6 +487,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) int prefix_length = prefix ? strlen(prefix) : 0; char set_executable_bit = 0; unsigned int refresh_flags = 0; + int lock_error = 0; struct lock_file *lock_file; git_config(git_default_config); @@ -494,7 +495,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) /* We can't free this memory, it becomes part of a linked list parsed atexit() */ lock_file = xcalloc(1, sizeof(struct lock_file)); - newfd = hold_lock_file_for_update(lock_file, get_index_file(), 1); + newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0); + if (newfd < 0) + lock_error = errno; entries = read_cache(); if (entries < 0) @@ -651,6 +654,12 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) finish: if (active_cache_changed) { + if (newfd < 0) { + if (refresh_flags & REFRESH_QUIET) + exit(128); + die("unable to create '%s.lock': %s", + get_index_file(), strerror(lock_error)); + } if (write_cache(newfd, active_cache, active_nr) || close(newfd) || commit_lock_file(lock_file)) die("Unable to write new index file"); diff --git a/diff-lib.c b/diff-lib.c index 60c0fa648..2e9161990 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -8,11 +8,227 @@ #include "diffcore.h" #include "revision.h" #include "cache-tree.h" +#include "path-list.h" /* * diff-files */ +static int read_directory(const char *path, struct path_list *list) +{ + DIR *dir; + struct dirent *e; + + if (!(dir = opendir(path))) + return error("Could not open directory %s", path); + + while ((e = readdir(dir))) + if (strcmp(".", e->d_name) && strcmp("..", e->d_name)) + path_list_insert(xstrdup(e->d_name), list); + + closedir(dir); + return 0; +} + +static int queue_diff(struct diff_options *o, + const char *name1, const char *name2) +{ + struct stat st; + int mode1 = 0, mode2 = 0; + + if (name1) { + if (stat(name1, &st)) + return error("Could not access '%s'", name1); + mode1 = st.st_mode; + } + if (name2) { + if (stat(name2, &st)) + return error("Could not access '%s'", name2); + mode2 = st.st_mode; + } + + if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) + return error("file/directory conflict: %s, %s", name1, name2); + + if (S_ISDIR(mode1) || S_ISDIR(mode2)) { + char buffer1[PATH_MAX], buffer2[PATH_MAX]; + struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1}; + int len1 = 0, len2 = 0, i1, i2, ret = 0; + + if (name1 && read_directory(name1, &p1)) + return -1; + if (name2 && read_directory(name2, &p2)) { + path_list_clear(&p1, 0); + return -1; + } + + if (name1) { + len1 = strlen(name1); + if (len1 > 0 && name1[len1 - 1] == '/') + len1--; + memcpy(buffer1, name1, len1); + buffer1[len1++] = '/'; + } + + if (name2) { + len2 = strlen(name2); + if (len2 > 0 && name2[len2 - 1] == '/') + len2--; + memcpy(buffer2, name2, len2); + buffer2[len2++] = '/'; + } + + for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) { + const char *n1, *n2; + int comp; + + if (i1 == p1.nr) + comp = 1; + else if (i2 == p2.nr) + comp = -1; + else + comp = strcmp(p1.items[i1].path, + p2.items[i2].path); + + if (comp > 0) + n1 = NULL; + else { + n1 = buffer1; + strncpy(buffer1 + len1, p1.items[i1++].path, + PATH_MAX - len1); + } + + if (comp < 0) + n2 = NULL; + else { + n2 = buffer2; + strncpy(buffer2 + len2, p2.items[i2++].path, + PATH_MAX - len2); + } + + ret = queue_diff(o, n1, n2); + } + path_list_clear(&p1, 0); + path_list_clear(&p2, 0); + + return ret; + } else { + struct diff_filespec *d1, *d2; + + if (o->reverse_diff) { + unsigned tmp; + const char *tmp_c; + tmp = mode1; mode1 = mode2; mode2 = tmp; + tmp_c = name1; name1 = name2; name2 = tmp_c; + } + + if (!name1) + name1 = "/dev/null"; + if (!name2) + name2 = "/dev/null"; + d1 = alloc_filespec(name1); + d2 = alloc_filespec(name2); + fill_filespec(d1, null_sha1, mode1); + fill_filespec(d2, null_sha1, mode2); + + diff_queue(&diff_queued_diff, d1, d2); + return 0; + } +} + +static int is_in_index(const char *path) +{ + int len = strlen(path); + int pos = cache_name_pos(path, len); + char c; + + if (pos < 0) + return 0; + if (strncmp(active_cache[pos]->name, path, len)) + return 0; + c = active_cache[pos]->name[len]; + return c == '\0' || c == '/'; +} + +static int handle_diff_files_args(struct rev_info *revs, + int argc, const char **argv, int *silent) +{ + *silent = 0; + + /* revs->max_count == -2 means --no-index */ + while (1 < argc && argv[1][0] == '-') { + if (!strcmp(argv[1], "--base")) + revs->max_count = 1; + else if (!strcmp(argv[1], "--ours")) + revs->max_count = 2; + else if (!strcmp(argv[1], "--theirs")) + revs->max_count = 3; + else if (!strcmp(argv[1], "-n") || + !strcmp(argv[1], "--no-index")) + revs->max_count = -2; + else if (!strcmp(argv[1], "-q")) + *silent = 1; + else + return error("invalid option: %s", argv[1]); + argv++; argc--; + } + + if (revs->max_count == -1 && revs->diffopt.nr_paths == 2) { + /* + * If two files are specified, and at least one is untracked, + * default to no-index. + */ + read_cache(); + if (!is_in_index(revs->diffopt.paths[0]) || + !is_in_index(revs->diffopt.paths[1])) + revs->max_count = -2; + } + + /* + * Make sure there are NO revision (i.e. pending object) parameter, + * rev.max_count is reasonable (0 <= n <= 3), + * there is no other revision filtering parameters. + */ + if (revs->pending.nr || revs->max_count > 3 || + revs->min_age != -1 || revs->max_age != -1) + return error("no revision allowed with diff-files"); + + if (revs->max_count == -1 && + (revs->diffopt.output_format & DIFF_FORMAT_PATCH)) + revs->combine_merges = revs->dense_combined_merges = 1; + + return 0; +} + +int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv) +{ + int silent_on_removed; + + if (handle_diff_files_args(revs, argc, argv, &silent_on_removed)) + return -1; + + if (revs->max_count == -2) { + if (revs->diffopt.nr_paths != 2) + return error("need two files/directories with --no-index"); + if (queue_diff(&revs->diffopt, revs->diffopt.paths[0], + revs->diffopt.paths[1])) + return -1; + diffcore_std(&revs->diffopt); + diff_flush(&revs->diffopt); + /* + * The return code for --no-index imitates diff(1): + * 0 = no changes, 1 = changes, else error + */ + return revs->diffopt.found_changes; + } + + if (read_cache() < 0) { + perror("read_cache"); + return -1; + } + return run_diff_files(revs, silent_on_removed); +} + int run_diff_files(struct rev_info *revs, int silent_on_removed) { int entries, i; @@ -20,11 +236,7 @@ int run_diff_files(struct rev_info *revs, int silent_on_removed) if (diff_unmerged_stage < 0) diff_unmerged_stage = 2; - entries = read_cache(); - if (entries < 0) { - perror("read_cache"); - return -1; - } + entries = active_nr; for (i = 0; i < entries; i++) { struct stat st; unsigned int oldmode, newmode; @@ -362,10 +574,6 @@ int run_diff_index(struct rev_info *revs, int cached) if (!revs->ignore_merges) match_missing = 1; - if (read_cache() < 0) { - perror("read_cache"); - return -1; - } mark_merge_entries(); ent = revs->pending.objects[0].item; @@ -382,6 +382,7 @@ struct emit_callback { int nparents, color_diff; const char **label_path; struct diff_words_data *diff_words; + int *found_changesp; }; static void free_diff_words_data(struct emit_callback *ecbdata) @@ -501,6 +502,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO); const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); + *(ecbdata->found_changesp) = 1; + if (ecbdata->label_path[0]) { const char *name_a_tab, *name_b_tab; @@ -1098,6 +1101,7 @@ static void builtin_diff(const char *name_a, if (complete_rewrite) { emit_rewrite_diff(name_a, name_b, one, two, o->color_diff); + o->found_changes = 1; goto free_ab_and_return; } } @@ -1115,6 +1119,7 @@ static void builtin_diff(const char *name_a, else printf("Binary files %s and %s differ\n", lbl[0], lbl[1]); + o->found_changes = 1; } else { /* Crazy xdl interfaces.. */ @@ -1127,6 +1132,7 @@ static void builtin_diff(const char *name_a, memset(&ecbdata, 0, sizeof(ecbdata)); ecbdata.label_path = lbl; ecbdata.color_diff = o->color_diff; + ecbdata.found_changesp = &o->found_changes; xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts; xecfg.ctxlen = o->context; xecfg.flags = XDL_EMIT_FUNCNAMES; @@ -2455,7 +2461,8 @@ static void diff_resolve_rename_copy(void) p->status = DIFF_STATUS_RENAMED; } else if (hashcmp(p->one->sha1, p->two->sha1) || - p->one->mode != p->two->mode) + p->one->mode != p->two->mode || + is_null_sha1(p->one->sha1)) p->status = DIFF_STATUS_MODIFIED; else { /* This is a "no-change" entry and should not @@ -75,6 +75,9 @@ struct diff_options { int stat_width; int stat_name_width; + /* this is set by diffcore for DIFF_FORMAT_PATCH */ + int found_changes; + int nr_paths; const char **paths; int *pathlens; @@ -219,6 +222,7 @@ extern void diff_flush(struct diff_options*); extern const char *diff_unique_abbrev(const unsigned char *, int); extern int run_diff_files(struct rev_info *revs, int silent_on_removed); +extern int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv); extern int run_diff_index(struct rev_info *revs, int cached); diff --git a/git-commit.sh b/git-commit.sh index 476f4f18d..be3677c20 100755 --- a/git-commit.sh +++ b/git-commit.sh @@ -13,10 +13,10 @@ git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t case "$0" in *status) status_only=t - unmerged_ok_if_status=--unmerged ;; + ;; *commit) status_only= - unmerged_ok_if_status= ;; + ;; esac refuse_partial () { @@ -393,16 +393,17 @@ else USE_INDEX="$THIS_INDEX" fi -GIT_INDEX_FILE="$USE_INDEX" \ - git-update-index -q $unmerged_ok_if_status --refresh || exit - -################################################################ -# If the request is status, just show it and exit. - -case "$0" in -*status) +case "$status_only" in +t) + # This will silently fail in a read-only repository, which is + # what we want. + GIT_INDEX_FILE="$USE_INDEX" git-update-index -q --unmerged --refresh run_status exit $? + ;; +'') + GIT_INDEX_FILE="$USE_INDEX" git-update-index -q --refresh || exit + ;; esac ################################################################ @@ -237,8 +237,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "config", cmd_config }, { "count-objects", cmd_count_objects, RUN_SETUP }, { "describe", cmd_describe, RUN_SETUP }, - { "diff", cmd_diff, RUN_SETUP | USE_PAGER }, - { "diff-files", cmd_diff_files, RUN_SETUP }, + { "diff", cmd_diff, USE_PAGER }, + { "diff-files", cmd_diff_files }, { "diff-index", cmd_diff_index, RUN_SETUP }, { "diff-tree", cmd_diff_tree, RUN_SETUP }, { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, diff --git a/wt-status.c b/wt-status.c index 035e546ed..a25632bc8 100644 --- a/wt-status.c +++ b/wt-status.c @@ -191,12 +191,18 @@ static void wt_status_print_changed_cb(struct diff_queue_struct *q, wt_status_print_trailer(); } +static void wt_read_cache(struct wt_status *s) +{ + discard_cache(); + read_cache(); +} + void wt_status_print_initial(struct wt_status *s) { int i; char buf[PATH_MAX]; - read_cache(); + wt_read_cache(s); if (active_nr) { s->commitable = 1; wt_status_print_cached_header(NULL); @@ -220,6 +226,7 @@ static void wt_status_print_updated(struct wt_status *s) rev.diffopt.format_callback = wt_status_print_updated_cb; rev.diffopt.format_callback_data = s; rev.diffopt.detect_rename = 1; + wt_read_cache(s); run_diff_index(&rev, 1); } @@ -231,6 +238,7 @@ static void wt_status_print_changed(struct wt_status *s) rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; rev.diffopt.format_callback = wt_status_print_changed_cb; rev.diffopt.format_callback_data = s; + wt_read_cache(s); run_diff_files(&rev, 0); } @@ -287,6 +295,7 @@ static void wt_status_print_verbose(struct wt_status *s) setup_revisions(0, NULL, &rev, s->reference); rev.diffopt.output_format |= DIFF_FORMAT_PATCH; rev.diffopt.detect_rename = 1; + wt_read_cache(s); run_diff_index(&rev, 1); } @@ -316,7 +325,6 @@ void wt_status_print(struct wt_status *s) } else { wt_status_print_updated(s); - discard_cache(); } wt_status_print_changed(s); |