builtin / diff-tree.con commit submodule foreach: fix "<command> --quiet" not being respected (a282f5a)
   1#define USE_THE_INDEX_COMPATIBILITY_MACROS
   2#include "cache.h"
   3#include "config.h"
   4#include "diff.h"
   5#include "commit.h"
   6#include "log-tree.h"
   7#include "builtin.h"
   8#include "submodule.h"
   9#include "repository.h"
  10
  11static struct rev_info log_tree_opt;
  12
  13static int diff_tree_commit_oid(const struct object_id *oid)
  14{
  15        struct commit *commit = lookup_commit_reference(the_repository, oid);
  16        if (!commit)
  17                return -1;
  18        return log_tree_commit(&log_tree_opt, commit);
  19}
  20
  21/* Diff one or more commits. */
  22static int stdin_diff_commit(struct commit *commit, const char *p)
  23{
  24        struct object_id oid;
  25        struct commit_list **pptr = NULL;
  26
  27        /* Graft the fake parents locally to the commit */
  28        while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
  29                struct commit *parent = lookup_commit(the_repository, &oid);
  30                if (!pptr) {
  31                        /* Free the real parent list */
  32                        free_commit_list(commit->parents);
  33                        commit->parents = NULL;
  34                        pptr = &(commit->parents);
  35                }
  36                if (parent) {
  37                        pptr = &commit_list_insert(parent, pptr)->next;
  38                }
  39        }
  40        return log_tree_commit(&log_tree_opt, commit);
  41}
  42
  43/* Diff two trees. */
  44static int stdin_diff_trees(struct tree *tree1, const char *p)
  45{
  46        struct object_id oid;
  47        struct tree *tree2;
  48        if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
  49                return error("Need exactly two trees, separated by a space");
  50        tree2 = lookup_tree(the_repository, &oid);
  51        if (!tree2 || parse_tree(tree2))
  52                return -1;
  53        printf("%s %s\n", oid_to_hex(&tree1->object.oid),
  54                          oid_to_hex(&tree2->object.oid));
  55        diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
  56                      "", &log_tree_opt.diffopt);
  57        log_tree_diff_flush(&log_tree_opt);
  58        return 0;
  59}
  60
  61static int diff_tree_stdin(char *line)
  62{
  63        int len = strlen(line);
  64        struct object_id oid;
  65        struct object *obj;
  66        const char *p;
  67
  68        if (!len || line[len-1] != '\n')
  69                return -1;
  70        line[len-1] = 0;
  71        if (parse_oid_hex(line, &oid, &p))
  72                return -1;
  73        obj = parse_object(the_repository, &oid);
  74        if (!obj)
  75                return -1;
  76        if (obj->type == OBJ_COMMIT)
  77                return stdin_diff_commit((struct commit *)obj, p);
  78        if (obj->type == OBJ_TREE)
  79                return stdin_diff_trees((struct tree *)obj, p);
  80        error("Object %s is a %s, not a commit or tree",
  81              oid_to_hex(&oid), type_name(obj->type));
  82        return -1;
  83}
  84
  85static const char diff_tree_usage[] =
  86"git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
  87"[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
  88"  -r            diff recursively\n"
  89"  --root        include the initial commit as diff against /dev/null\n"
  90COMMON_DIFF_OPTIONS_HELP;
  91
  92static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
  93{
  94        if (!rev->diffopt.output_format) {
  95                if (rev->dense_combined_merges)
  96                        rev->diffopt.output_format = DIFF_FORMAT_PATCH;
  97                else
  98                        rev->diffopt.output_format = DIFF_FORMAT_RAW;
  99        }
 100}
 101
 102int cmd_diff_tree(int argc, const char **argv, const char *prefix)
 103{
 104        char line[1000];
 105        struct object *tree1, *tree2;
 106        static struct rev_info *opt = &log_tree_opt;
 107        struct setup_revision_opt s_r_opt;
 108        int read_stdin = 0;
 109
 110        if (argc == 2 && !strcmp(argv[1], "-h"))
 111                usage(diff_tree_usage);
 112
 113        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 114        repo_init_revisions(the_repository, opt, prefix);
 115        if (read_cache() < 0)
 116                die(_("index file corrupt"));
 117        opt->abbrev = 0;
 118        opt->diff = 1;
 119        opt->disable_stdin = 1;
 120        memset(&s_r_opt, 0, sizeof(s_r_opt));
 121        s_r_opt.tweak = diff_tree_tweak_rev;
 122
 123        precompose_argv(argc, argv);
 124        argc = setup_revisions(argc, argv, opt, &s_r_opt);
 125
 126        while (--argc > 0) {
 127                const char *arg = *++argv;
 128
 129                if (!strcmp(arg, "--stdin")) {
 130                        read_stdin = 1;
 131                        continue;
 132                }
 133                usage(diff_tree_usage);
 134        }
 135
 136        /*
 137         * NOTE!  We expect "a..b" to expand to "^a b" but it is
 138         * perfectly valid for revision range parser to yield "b ^a",
 139         * which means the same thing. If we get the latter, i.e. the
 140         * second one is marked UNINTERESTING, we recover the original
 141         * order the user gave, i.e. "a..b", by swapping the trees.
 142         */
 143        switch (opt->pending.nr) {
 144        case 0:
 145                if (!read_stdin)
 146                        usage(diff_tree_usage);
 147                break;
 148        case 1:
 149                tree1 = opt->pending.objects[0].item;
 150                diff_tree_commit_oid(&tree1->oid);
 151                break;
 152        case 2:
 153                tree1 = opt->pending.objects[0].item;
 154                tree2 = opt->pending.objects[1].item;
 155                if (tree2->flags & UNINTERESTING) {
 156                        SWAP(tree2, tree1);
 157                }
 158                diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
 159                log_tree_diff_flush(opt);
 160                break;
 161        }
 162
 163        if (read_stdin) {
 164                int saved_nrl = 0;
 165                int saved_dcctc = 0;
 166
 167                if (opt->diffopt.detect_rename) {
 168                        if (!the_index.cache)
 169                                repo_read_index(the_repository);
 170                        opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
 171                }
 172                while (fgets(line, sizeof(line), stdin)) {
 173                        struct object_id oid;
 174
 175                        if (get_oid_hex(line, &oid)) {
 176                                fputs(line, stdout);
 177                                fflush(stdout);
 178                        }
 179                        else {
 180                                diff_tree_stdin(line);
 181                                if (saved_nrl < opt->diffopt.needed_rename_limit)
 182                                        saved_nrl = opt->diffopt.needed_rename_limit;
 183                                if (opt->diffopt.degraded_cc_to_c)
 184                                        saved_dcctc = 1;
 185                        }
 186                }
 187                opt->diffopt.degraded_cc_to_c = saved_dcctc;
 188                opt->diffopt.needed_rename_limit = saved_nrl;
 189        }
 190
 191        return diff_result_code(&opt->diffopt, 0);
 192}