| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "builtin.h" |
| 4 | #include "config.h" |
| 5 | #include "diff.h" |
| 6 | #include "commit.h" |
| 7 | #include "gettext.h" |
| 8 | #include "hex.h" |
| 9 | #include "log-tree.h" |
| 10 | #include "read-cache-ll.h" |
| 11 | #include "revision.h" |
| 12 | #include "tmp-objdir.h" |
| 13 | #include "tree.h" |
| 14 | |
| 15 | static struct rev_info log_tree_opt; |
| 16 | |
| 17 | static int diff_tree_commit_oid(const struct object_id *oid) |
| 18 | { |
| 19 | struct commit *commit = lookup_commit_reference(the_repository, oid); |
| 20 | if (!commit) |
| 21 | return -1; |
| 22 | return log_tree_commit(&log_tree_opt, commit); |
| 23 | } |
| 24 | |
| 25 | /* Diff one or more commits. */ |
| 26 | static int stdin_diff_commit(struct commit *commit, const char *p) |
| 27 | { |
| 28 | struct object_id oid; |
| 29 | struct commit_list **pptr = NULL; |
| 30 | |
| 31 | /* Graft the fake parents locally to the commit */ |
| 32 | while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) { |
| 33 | struct commit *parent = lookup_commit(the_repository, &oid); |
| 34 | if (!pptr) { |
| 35 | /* Free the real parent list */ |
| 36 | commit_list_free(commit->parents); |
| 37 | commit->parents = NULL; |
| 38 | pptr = &(commit->parents); |
| 39 | } |
| 40 | if (parent) { |
| 41 | pptr = &commit_list_insert(parent, pptr)->next; |
| 42 | } |
| 43 | } |
| 44 | return log_tree_commit(&log_tree_opt, commit); |
| 45 | } |
| 46 | |
| 47 | /* Diff two trees. */ |
| 48 | static int stdin_diff_trees(struct tree *tree1, const char *p) |
| 49 | { |
| 50 | struct object_id oid; |
| 51 | struct tree *tree2; |
| 52 | if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p) |
| 53 | return error("Need exactly two trees, separated by a space"); |
| 54 | tree2 = lookup_tree(the_repository, &oid); |
| 55 | if (!tree2 || repo_parse_tree(the_repository, tree2)) |
| 56 | return -1; |
| 57 | printf("%s %s\n", oid_to_hex(&tree1->object.oid), |
| 58 | oid_to_hex(&tree2->object.oid)); |
| 59 | diff_tree_oid(&tree1->object.oid, &tree2->object.oid, |
| 60 | "", &log_tree_opt.diffopt); |
| 61 | log_tree_diff_flush(&log_tree_opt); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int diff_tree_stdin(char *line) |
| 66 | { |
| 67 | int len = strlen(line); |
| 68 | struct object_id oid; |
| 69 | struct object *obj; |
| 70 | const char *p; |
| 71 | |
| 72 | if (!len || line[len-1] != '\n') |
| 73 | return -1; |
| 74 | line[len-1] = 0; |
| 75 | if (parse_oid_hex(line, &oid, &p)) |
| 76 | return -1; |
| 77 | obj = parse_object(the_repository, &oid); |
| 78 | if (!obj) |
| 79 | return -1; |
| 80 | if (obj->type == OBJ_COMMIT) |
| 81 | return stdin_diff_commit((struct commit *)obj, p); |
| 82 | if (obj->type == OBJ_TREE) |
| 83 | return stdin_diff_trees((struct tree *)obj, p); |
| 84 | error("Object %s is a %s, not a commit or tree", |
| 85 | oid_to_hex(&oid), type_name(obj->type)); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | static const char diff_tree_usage[] = |
| 90 | "git diff-tree [--stdin] [-m] [-s] [-v] [--no-commit-id] [--pretty]\n" |
| 91 | " [-t] [-r] [-c | --cc] [--combined-all-paths] [--root] [--merge-base]\n" |
| 92 | " [<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n" |
| 93 | "\n" |
| 94 | " -r diff recursively\n" |
| 95 | " -c show combined diff for merge commits\n" |
| 96 | " --cc show combined diff for merge commits removing uninteresting hunks\n" |
| 97 | " --combined-all-paths\n" |
| 98 | " show name of file in all parents for combined diffs\n" |
| 99 | " --root include the initial commit as diff against /dev/null\n" |
| 100 | COMMON_DIFF_OPTIONS_HELP; |
| 101 | |
| 102 | static void diff_tree_tweak_rev(struct rev_info *rev) |
| 103 | { |
| 104 | if (!rev->diffopt.output_format) { |
| 105 | if (rev->dense_combined_merges) |
| 106 | rev->diffopt.output_format = DIFF_FORMAT_PATCH; |
| 107 | else |
| 108 | rev->diffopt.output_format = DIFF_FORMAT_RAW; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int cmd_diff_tree(int argc, |
| 113 | const char **argv, |
| 114 | const char *prefix, |
| 115 | struct repository *repo UNUSED) |
| 116 | { |
| 117 | char line[1000]; |
| 118 | struct object *tree1, *tree2; |
| 119 | static struct rev_info *opt = &log_tree_opt; |
| 120 | struct setup_revision_opt s_r_opt; |
| 121 | struct userformat_want w; |
| 122 | int read_stdin = 0; |
| 123 | int merge_base = 0; |
| 124 | |
| 125 | show_usage_if_asked(argc, argv, diff_tree_usage); |
| 126 | |
| 127 | repo_config(the_repository, git_diff_basic_config, NULL); /* no "diff" UI options */ |
| 128 | |
| 129 | prepare_repo_settings(the_repository); |
| 130 | the_repository->settings.command_requires_full_index = 0; |
| 131 | |
| 132 | repo_init_revisions(the_repository, opt, prefix); |
| 133 | if (repo_read_index(the_repository) < 0) |
| 134 | die(_("index file corrupt")); |
| 135 | opt->abbrev = 0; |
| 136 | opt->diff = 1; |
| 137 | opt->disable_stdin = 1; |
| 138 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
| 139 | s_r_opt.tweak = diff_tree_tweak_rev; |
| 140 | |
| 141 | prefix = precompose_argv_prefix(argc, argv, prefix); |
| 142 | argc = setup_revisions(argc, argv, opt, &s_r_opt); |
| 143 | |
| 144 | memset(&w, 0, sizeof(w)); |
| 145 | userformat_find_requirements(NULL, &w); |
| 146 | |
| 147 | if (!opt->show_notes_given && w.notes) |
| 148 | opt->show_notes = 1; |
| 149 | if (opt->show_notes) |
| 150 | load_display_notes(&opt->notes_opt); |
| 151 | |
| 152 | while (--argc > 0) { |
| 153 | const char *arg = *++argv; |
| 154 | |
| 155 | if (!strcmp(arg, "--stdin")) { |
| 156 | read_stdin = 1; |
| 157 | continue; |
| 158 | } |
| 159 | if (!strcmp(arg, "--merge-base")) { |
| 160 | merge_base = 1; |
| 161 | continue; |
| 162 | } |
| 163 | usage(diff_tree_usage); |
| 164 | } |
| 165 | |
| 166 | if (read_stdin && merge_base) |
| 167 | die(_("options '%s' and '%s' cannot be used together"), "--stdin", "--merge-base"); |
| 168 | if (merge_base && opt->pending.nr != 2) |
| 169 | die(_("--merge-base only works with two commits")); |
| 170 | |
| 171 | opt->diffopt.rotate_to_strict = 1; |
| 172 | |
| 173 | diff_hunks_attach(&opt->diffopt); |
| 174 | |
| 175 | /* |
| 176 | * NOTE! We expect "a..b" to expand to "^a b" but it is |
| 177 | * perfectly valid for revision range parser to yield "b ^a", |
| 178 | * which means the same thing. If we get the latter, i.e. the |
| 179 | * second one is marked UNINTERESTING, we recover the original |
| 180 | * order the user gave, i.e. "a..b", by swapping the trees. |
| 181 | */ |
| 182 | switch (opt->pending.nr) { |
| 183 | case 0: |
| 184 | if (!read_stdin) |
| 185 | usage(diff_tree_usage); |
| 186 | break; |
| 187 | case 1: |
| 188 | tree1 = opt->pending.objects[0].item; |
| 189 | diff_tree_commit_oid(&tree1->oid); |
| 190 | break; |
| 191 | case 2: |
| 192 | tree1 = opt->pending.objects[0].item; |
| 193 | tree2 = opt->pending.objects[1].item; |
| 194 | if (merge_base) { |
| 195 | struct object_id oid; |
| 196 | |
| 197 | diff_get_merge_base(opt, &oid); |
| 198 | tree1 = lookup_object(the_repository, &oid); |
| 199 | } else if (tree2->flags & UNINTERESTING) { |
| 200 | SWAP(tree2, tree1); |
| 201 | } |
| 202 | diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt); |
| 203 | log_tree_diff_flush(opt); |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | if (read_stdin) { |
| 208 | int saved_nrl = 0; |
| 209 | int saved_dcctc = 0; |
| 210 | |
| 211 | opt->diffopt.rotate_to_strict = 0; |
| 212 | opt->diffopt.no_free = 1; |
| 213 | if (opt->diffopt.detect_rename) { |
| 214 | if (the_repository->index->cache) |
| 215 | repo_read_index(the_repository); |
| 216 | opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE; |
| 217 | } |
| 218 | while (fgets(line, sizeof(line), stdin)) { |
| 219 | struct object_id oid; |
| 220 | |
| 221 | if (get_oid_hex(line, &oid)) { |
| 222 | fputs(line, stdout); |
| 223 | fflush(stdout); |
| 224 | } |
| 225 | else { |
| 226 | diff_tree_stdin(line); |
| 227 | if (saved_nrl < opt->diffopt.needed_rename_limit) |
| 228 | saved_nrl = opt->diffopt.needed_rename_limit; |
| 229 | if (opt->diffopt.degraded_cc_to_c) |
| 230 | saved_dcctc = 1; |
| 231 | } |
| 232 | } |
| 233 | opt->diffopt.degraded_cc_to_c = saved_dcctc; |
| 234 | opt->diffopt.needed_rename_limit = saved_nrl; |
| 235 | opt->diffopt.no_free = 0; |
| 236 | diff_free(&opt->diffopt); |
| 237 | } |
| 238 | |
| 239 | diff_hunks_detach(&opt->diffopt); |
| 240 | return diff_result_code(opt); |
| 241 | } |