diff-tree.con commit Common option parsing for "git log --diff" and friends (cd2bdc5)
   1#include "cache.h"
   2#include "diff.h"
   3#include "commit.h"
   4#include "log-tree.h"
   5
   6static struct rev_info log_tree_opt;
   7
   8static int diff_tree_commit_sha1(const unsigned char *sha1)
   9{
  10        struct commit *commit = lookup_commit_reference(sha1);
  11        if (!commit)
  12                return -1;
  13        return log_tree_commit(&log_tree_opt, commit);
  14}
  15
  16static int diff_tree_stdin(char *line)
  17{
  18        int len = strlen(line);
  19        unsigned char sha1[20];
  20        struct commit *commit;
  21
  22        if (!len || line[len-1] != '\n')
  23                return -1;
  24        line[len-1] = 0;
  25        if (get_sha1_hex(line, sha1))
  26                return -1;
  27        commit = lookup_commit(sha1);
  28        if (!commit || parse_commit(commit))
  29                return -1;
  30        if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
  31                /* Graft the fake parents locally to the commit */
  32                int pos = 41;
  33                struct commit_list **pptr, *parents;
  34
  35                /* Free the real parent list */
  36                for (parents = commit->parents; parents; ) {
  37                        struct commit_list *tmp = parents->next;
  38                        free(parents);
  39                        parents = tmp;
  40                }
  41                commit->parents = NULL;
  42                pptr = &(commit->parents);
  43                while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
  44                        struct commit *parent = lookup_commit(sha1);
  45                        if (parent) {
  46                                pptr = &commit_list_insert(parent, pptr)->next;
  47                        }
  48                        pos += 41;
  49                }
  50        }
  51        return log_tree_commit(&log_tree_opt, commit);
  52}
  53
  54static const char diff_tree_usage[] =
  55"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
  56"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
  57"  -r            diff recursively\n"
  58"  --root        include the initial commit as diff against /dev/null\n"
  59COMMON_DIFF_OPTIONS_HELP;
  60
  61int main(int argc, const char **argv)
  62{
  63        int nr_sha1;
  64        char line[1000];
  65        struct object *tree1, *tree2;
  66        static struct rev_info *opt = &log_tree_opt;
  67        struct object_list *list;
  68        int read_stdin = 0;
  69
  70        git_config(git_diff_config);
  71        nr_sha1 = 0;
  72        argc = setup_revisions(argc, argv, opt, NULL);
  73
  74        while (--argc > 0) {
  75                const char *arg = *++argv;
  76
  77                if (!strcmp(arg, "--stdin")) {
  78                        read_stdin = 1;
  79                        continue;
  80                }
  81                usage(diff_tree_usage);
  82        }
  83
  84        /*
  85         * NOTE! "setup_revisions()" will have inserted the revisions
  86         * it parsed in reverse order. So if you do
  87         *
  88         *      git-diff-tree a b
  89         *
  90         * the commit list will be "b" -> "a" -> NULL, so we reverse
  91         * the order of the objects if the first one is not marked
  92         * UNINTERESTING.
  93         */
  94        nr_sha1 = 0;
  95        list = opt->pending_objects;
  96        if (list) {
  97                nr_sha1++;
  98                tree1 = list->item;
  99                list = list->next;
 100                if (list) {
 101                        nr_sha1++;
 102                        tree2 = tree1;
 103                        tree1 = list->item;
 104                        if (list->next)
 105                                usage(diff_tree_usage);
 106                        /* Switch them around if the second one was uninteresting.. */
 107                        if (tree2->flags & UNINTERESTING) {
 108                                struct object *tmp = tree2;
 109                                tree2 = tree1;
 110                                tree1 = tmp;
 111                        }
 112                }
 113        }
 114
 115        switch (nr_sha1) {
 116        case 0:
 117                if (!read_stdin)
 118                        usage(diff_tree_usage);
 119                break;
 120        case 1:
 121                diff_tree_commit_sha1(tree1->sha1);
 122                break;
 123        case 2:
 124                diff_tree_sha1(tree1->sha1,
 125                               tree2->sha1,
 126                               "", &opt->diffopt);
 127                log_tree_diff_flush(opt);
 128                break;
 129        }
 130
 131        if (!read_stdin)
 132                return 0;
 133
 134        if (opt->diffopt.detect_rename)
 135                opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
 136                                       DIFF_SETUP_USE_CACHE);
 137        while (fgets(line, sizeof(line), stdin))
 138                diff_tree_stdin(line);
 139
 140        return 0;
 141}