builtin / diff-tree.con commit ll_merge(): add ancestor label parameter for diff3-style output (f01de62)
   1#include "cache.h"
   2#include "diff.h"
   3#include "commit.h"
   4#include "log-tree.h"
   5#include "builtin.h"
   6
   7static struct rev_info log_tree_opt;
   8
   9static int diff_tree_commit_sha1(const unsigned char *sha1)
  10{
  11        struct commit *commit = lookup_commit_reference(sha1);
  12        if (!commit)
  13                return -1;
  14        return log_tree_commit(&log_tree_opt, commit);
  15}
  16
  17/* Diff one or more commits. */
  18static int stdin_diff_commit(struct commit *commit, char *line, int len)
  19{
  20        unsigned char sha1[20];
  21        if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
  22                /* Graft the fake parents locally to the commit */
  23                int pos = 41;
  24                struct commit_list **pptr, *parents;
  25
  26                /* Free the real parent list */
  27                for (parents = commit->parents; parents; ) {
  28                        struct commit_list *tmp = parents->next;
  29                        free(parents);
  30                        parents = tmp;
  31                }
  32                commit->parents = NULL;
  33                pptr = &(commit->parents);
  34                while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
  35                        struct commit *parent = lookup_commit(sha1);
  36                        if (parent) {
  37                                pptr = &commit_list_insert(parent, pptr)->next;
  38                        }
  39                        pos += 41;
  40                }
  41        }
  42        return log_tree_commit(&log_tree_opt, commit);
  43}
  44
  45/* Diff two trees. */
  46static int stdin_diff_trees(struct tree *tree1, char *line, int len)
  47{
  48        unsigned char sha1[20];
  49        struct tree *tree2;
  50        if (len != 82 || !isspace(line[40]) || get_sha1_hex(line + 41, sha1))
  51                return error("Need exactly two trees, separated by a space");
  52        tree2 = lookup_tree(sha1);
  53        if (!tree2 || parse_tree(tree2))
  54                return -1;
  55        printf("%s %s\n", sha1_to_hex(tree1->object.sha1),
  56                          sha1_to_hex(tree2->object.sha1));
  57        diff_tree_sha1(tree1->object.sha1, tree2->object.sha1,
  58                       "", &log_tree_opt.diffopt);
  59        log_tree_diff_flush(&log_tree_opt);
  60        return 0;
  61}
  62
  63static int diff_tree_stdin(char *line)
  64{
  65        int len = strlen(line);
  66        unsigned char sha1[20];
  67        struct object *obj;
  68
  69        if (!len || line[len-1] != '\n')
  70                return -1;
  71        line[len-1] = 0;
  72        if (get_sha1_hex(line, sha1))
  73                return -1;
  74        obj = lookup_unknown_object(sha1);
  75        if (!obj || !obj->parsed)
  76                obj = parse_object(sha1);
  77        if (!obj)
  78                return -1;
  79        if (obj->type == OBJ_COMMIT)
  80                return stdin_diff_commit((struct commit *)obj, line, len);
  81        if (obj->type == OBJ_TREE)
  82                return stdin_diff_trees((struct tree *)obj, line, len);
  83        error("Object %s is a %s, not a commit or tree",
  84              sha1_to_hex(sha1), typename(obj->type));
  85        return -1;
  86}
  87
  88static const char diff_tree_usage[] =
  89"git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
  90"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
  91"  -r            diff recursively\n"
  92"  --root        include the initial commit as diff against /dev/null\n"
  93COMMON_DIFF_OPTIONS_HELP;
  94
  95int cmd_diff_tree(int argc, const char **argv, const char *prefix)
  96{
  97        int nr_sha1;
  98        char line[1000];
  99        struct object *tree1, *tree2;
 100        static struct rev_info *opt = &log_tree_opt;
 101        int read_stdin = 0;
 102
 103        init_revisions(opt, prefix);
 104        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 105        opt->abbrev = 0;
 106        opt->diff = 1;
 107        opt->disable_stdin = 1;
 108        argc = setup_revisions(argc, argv, opt, NULL);
 109
 110        while (--argc > 0) {
 111                const char *arg = *++argv;
 112
 113                if (!strcmp(arg, "--stdin")) {
 114                        read_stdin = 1;
 115                        continue;
 116                }
 117                usage(diff_tree_usage);
 118        }
 119
 120        if (!opt->diffopt.output_format)
 121                opt->diffopt.output_format = DIFF_FORMAT_RAW;
 122
 123        /*
 124         * NOTE! We expect "a ^b" to be equal to "a..b", so we
 125         * reverse the order of the objects if the second one
 126         * is marked UNINTERESTING.
 127         */
 128        nr_sha1 = opt->pending.nr;
 129        switch (nr_sha1) {
 130        case 0:
 131                if (!read_stdin)
 132                        usage(diff_tree_usage);
 133                break;
 134        case 1:
 135                tree1 = opt->pending.objects[0].item;
 136                diff_tree_commit_sha1(tree1->sha1);
 137                break;
 138        case 2:
 139                tree1 = opt->pending.objects[0].item;
 140                tree2 = opt->pending.objects[1].item;
 141                if (tree2->flags & UNINTERESTING) {
 142                        struct object *tmp = tree2;
 143                        tree2 = tree1;
 144                        tree1 = tmp;
 145                }
 146                diff_tree_sha1(tree1->sha1,
 147                               tree2->sha1,
 148                               "", &opt->diffopt);
 149                log_tree_diff_flush(opt);
 150                break;
 151        }
 152
 153        if (read_stdin) {
 154                if (opt->diffopt.detect_rename)
 155                        opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
 156                                               DIFF_SETUP_USE_CACHE);
 157                while (fgets(line, sizeof(line), stdin)) {
 158                        unsigned char sha1[20];
 159
 160                        if (get_sha1_hex(line, sha1)) {
 161                                fputs(line, stdout);
 162                                fflush(stdout);
 163                        }
 164                        else
 165                                diff_tree_stdin(line);
 166                }
 167        }
 168
 169        return diff_result_code(&opt->diffopt, 0);
 170}