builtin-diff-tree.con commit log --raw: Don't descend into subdirectories by default (1798562)
   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
  17static int diff_tree_stdin(char *line)
  18{
  19        int len = strlen(line);
  20        unsigned char sha1[20];
  21        struct commit *commit;
  22
  23        if (!len || line[len-1] != '\n')
  24                return -1;
  25        line[len-1] = 0;
  26        if (get_sha1_hex(line, sha1))
  27                return -1;
  28        commit = lookup_commit(sha1);
  29        if (!commit || parse_commit(commit))
  30                return -1;
  31        if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
  32                /* Graft the fake parents locally to the commit */
  33                int pos = 41;
  34                struct commit_list **pptr, *parents;
  35
  36                /* Free the real parent list */
  37                for (parents = commit->parents; parents; ) {
  38                        struct commit_list *tmp = parents->next;
  39                        free(parents);
  40                        parents = tmp;
  41                }
  42                commit->parents = NULL;
  43                pptr = &(commit->parents);
  44                while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
  45                        struct commit *parent = lookup_commit(sha1);
  46                        if (parent) {
  47                                pptr = &commit_list_insert(parent, pptr)->next;
  48                        }
  49                        pos += 41;
  50                }
  51        }
  52        return log_tree_commit(&log_tree_opt, commit);
  53}
  54
  55static const char diff_tree_usage[] =
  56"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
  57"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
  58"  -r            diff recursively\n"
  59"  --root        include the initial commit as diff against /dev/null\n"
  60COMMON_DIFF_OPTIONS_HELP;
  61
  62int cmd_diff_tree(int argc, const char **argv, char **envp)
  63{
  64        int nr_sha1;
  65        char line[1000];
  66        struct object *tree1, *tree2;
  67        static struct rev_info *opt = &log_tree_opt;
  68        int read_stdin = 0;
  69
  70        git_config(git_diff_config);
  71        nr_sha1 = 0;
  72        init_revisions(opt);
  73        opt->abbrev = 0;
  74        opt->diff = 1;
  75        opt->diffopt.msg_sep = "---\n";
  76        argc = setup_revisions(argc, argv, opt, NULL);
  77
  78        while (--argc > 0) {
  79                const char *arg = *++argv;
  80
  81                if (!strcmp(arg, "--stdin")) {
  82                        read_stdin = 1;
  83                        continue;
  84                }
  85                usage(diff_tree_usage);
  86        }
  87
  88        if (!opt->diffopt.output_format)
  89                opt->diffopt.output_format = DIFF_FORMAT_RAW;
  90
  91        /*
  92         * NOTE! We expect "a ^b" to be equal to "a..b", so we
  93         * reverse the order of the objects if the second one
  94         * is marked UNINTERESTING.
  95         */
  96        nr_sha1 = opt->pending.nr;
  97        switch (nr_sha1) {
  98        case 0:
  99                if (!read_stdin)
 100                        usage(diff_tree_usage);
 101                break;
 102        case 1:
 103                tree1 = opt->pending.objects[0].item;
 104                diff_tree_commit_sha1(tree1->sha1);
 105                break;
 106        case 2:
 107                tree1 = opt->pending.objects[0].item;
 108                tree2 = opt->pending.objects[1].item;
 109                if (tree2->flags & UNINTERESTING) {
 110                        struct object *tmp = tree2;
 111                        tree2 = tree1;
 112                        tree1 = tmp;
 113                }
 114                diff_tree_sha1(tree1->sha1,
 115                               tree2->sha1,
 116                               "", &opt->diffopt);
 117                log_tree_diff_flush(opt);
 118                break;
 119        }
 120
 121        if (!read_stdin)
 122                return 0;
 123
 124        if (opt->diffopt.detect_rename)
 125                opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
 126                                       DIFF_SETUP_USE_CACHE);
 127        while (fgets(line, sizeof(line), stdin)) {
 128                unsigned char sha1[20];
 129
 130                if (get_sha1_hex(line, sha1)) {
 131                        fputs(line, stdout);
 132                        fflush(stdout);
 133                }
 134                else
 135                        diff_tree_stdin(line);
 136        }
 137        return 0;
 138}