builtin-diff-stages.con commit git-svn: support -C<num> passing to git-diff-tree (162f412)
   1/*
   2 * Copyright (c) 2005 Junio C Hamano
   3 */
   4
   5#include "cache.h"
   6#include "diff.h"
   7#include "builtin.h"
   8
   9static struct diff_options diff_options;
  10
  11static const char diff_stages_usage[] =
  12"git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
  13COMMON_DIFF_OPTIONS_HELP;
  14
  15static void diff_stages(int stage1, int stage2, const char **pathspec)
  16{
  17        int i = 0;
  18        while (i < active_nr) {
  19                struct cache_entry *ce, *stages[4] = { NULL, };
  20                struct cache_entry *one, *two;
  21                const char *name;
  22                int len, skip;
  23
  24                ce = active_cache[i];
  25                skip = !ce_path_match(ce, pathspec);
  26                len = ce_namelen(ce);
  27                name = ce->name;
  28                for (;;) {
  29                        int stage = ce_stage(ce);
  30                        stages[stage] = ce;
  31                        if (active_nr <= ++i)
  32                                break;
  33                        ce = active_cache[i];
  34                        if (ce_namelen(ce) != len ||
  35                            memcmp(name, ce->name, len))
  36                                break;
  37                }
  38                one = stages[stage1];
  39                two = stages[stage2];
  40
  41                if (skip || (!one && !two))
  42                        continue;
  43                if (!one)
  44                        diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
  45                                       two->sha1, name, NULL);
  46                else if (!two)
  47                        diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
  48                                       one->sha1, name, NULL);
  49                else if (memcmp(one->sha1, two->sha1, 20) ||
  50                         (one->ce_mode != two->ce_mode) ||
  51                         diff_options.find_copies_harder)
  52                        diff_change(&diff_options,
  53                                    ntohl(one->ce_mode), ntohl(two->ce_mode),
  54                                    one->sha1, two->sha1, name, NULL);
  55        }
  56}
  57
  58int cmd_diff_stages(int ac, const char **av, char **envp)
  59{
  60        int stage1, stage2;
  61        const char *prefix = setup_git_directory();
  62        const char **pathspec = NULL;
  63
  64        git_config(git_diff_config);
  65        read_cache();
  66        diff_setup(&diff_options);
  67        while (1 < ac && av[1][0] == '-') {
  68                const char *arg = av[1];
  69                if (!strcmp(arg, "-r"))
  70                        ; /* as usual */
  71                else {
  72                        int diff_opt_cnt;
  73                        diff_opt_cnt = diff_opt_parse(&diff_options,
  74                                                      av+1, ac-1);
  75                        if (diff_opt_cnt < 0)
  76                                usage(diff_stages_usage);
  77                        else if (diff_opt_cnt) {
  78                                av += diff_opt_cnt;
  79                                ac -= diff_opt_cnt;
  80                                continue;
  81                        }
  82                        else
  83                                usage(diff_stages_usage);
  84                }
  85                ac--; av++;
  86        }
  87
  88        if (ac < 3 ||
  89            sscanf(av[1], "%d", &stage1) != 1 ||
  90            ! (0 <= stage1 && stage1 <= 3) ||
  91            sscanf(av[2], "%d", &stage2) != 1 ||
  92            ! (0 <= stage2 && stage2 <= 3))
  93                usage(diff_stages_usage);
  94
  95        av += 3; /* The rest from av[0] are for paths restriction. */
  96        pathspec = get_pathspec(prefix, av);
  97
  98        if (diff_setup_done(&diff_options) < 0)
  99                usage(diff_stages_usage);
 100
 101        diff_stages(stage1, stage2, pathspec);
 102        diffcore_std(&diff_options);
 103        diff_flush(&diff_options);
 104        return 0;
 105}