diff-files.con commit rev-parse lstat() workaround cleanup. (9ad0a93)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "diff.h"
   8
   9static const char diff_files_usage[] =
  10"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
  11COMMON_DIFF_OPTIONS_HELP;
  12
  13static struct diff_options diff_options;
  14static int silent = 0;
  15static int diff_unmerged_stage = 2;
  16static int combine_merges = 0;
  17static int dense_combined_merges = 0;
  18
  19static void show_unmerge(const char *path)
  20{
  21        diff_unmerge(&diff_options, path);
  22}
  23
  24static void show_file(int pfx, struct cache_entry *ce)
  25{
  26        diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
  27                       ce->sha1, ce->name, NULL);
  28}
  29
  30static void show_modified(int oldmode, int mode,
  31                          const unsigned char *old_sha1, const unsigned char *sha1,
  32                          char *path)
  33{
  34        diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
  35}
  36
  37int main(int argc, const char **argv)
  38{
  39        const char **pathspec;
  40        const char *prefix = setup_git_directory();
  41        int entries, i;
  42
  43        git_config(git_diff_config);
  44        diff_setup(&diff_options);
  45        while (1 < argc && argv[1][0] == '-') {
  46                if (!strcmp(argv[1], "--")) {
  47                        argv++;
  48                        argc--;
  49                        break;
  50                }
  51                if (!strcmp(argv[1], "-0"))
  52                        diff_unmerged_stage = 0;
  53                else if (!strcmp(argv[1], "-1"))
  54                        diff_unmerged_stage = 1;
  55                else if (!strcmp(argv[1], "-2"))
  56                        diff_unmerged_stage = 2;
  57                else if (!strcmp(argv[1], "-3"))
  58                        diff_unmerged_stage = 3;
  59                else if (!strcmp(argv[1], "--base"))
  60                        diff_unmerged_stage = 1;
  61                else if (!strcmp(argv[1], "--ours"))
  62                        diff_unmerged_stage = 2;
  63                else if (!strcmp(argv[1], "--theirs"))
  64                        diff_unmerged_stage = 3;
  65                else if (!strcmp(argv[1], "-q"))
  66                        silent = 1;
  67                else if (!strcmp(argv[1], "-r"))
  68                        ; /* no-op */
  69                else if (!strcmp(argv[1], "-s"))
  70                        ; /* no-op */
  71                else if (!strcmp(argv[1], "-c"))
  72                        combine_merges = 1;
  73                else if (!strcmp(argv[1], "--cc"))
  74                        dense_combined_merges = combine_merges = 1;
  75                else {
  76                        int diff_opt_cnt;
  77                        diff_opt_cnt = diff_opt_parse(&diff_options,
  78                                                      argv+1, argc-1);
  79                        if (diff_opt_cnt < 0)
  80                                usage(diff_files_usage);
  81                        else if (diff_opt_cnt) {
  82                                argv += diff_opt_cnt;
  83                                argc -= diff_opt_cnt;
  84                                continue;
  85                        }
  86                        else
  87                                usage(diff_files_usage);
  88                }
  89                argv++; argc--;
  90        }
  91        if (combine_merges) {
  92                diff_options.output_format = DIFF_FORMAT_PATCH;
  93        }
  94
  95        /* Find the directory, and set up the pathspec */
  96        pathspec = get_pathspec(prefix, argv + 1);
  97        entries = read_cache();
  98
  99        if (diff_setup_done(&diff_options) < 0)
 100                usage(diff_files_usage);
 101
 102        /* At this point, if argc == 1, then we are doing everything.
 103         * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
 104         */
 105        if (entries < 0) {
 106                perror("read_cache");
 107                exit(1);
 108        }
 109
 110        for (i = 0; i < entries; i++) {
 111                struct stat st;
 112                unsigned int oldmode, newmode;
 113                struct cache_entry *ce = active_cache[i];
 114                int changed;
 115
 116                if (!ce_path_match(ce, pathspec))
 117                        continue;
 118
 119                if (ce_stage(ce)) {
 120                        struct {
 121                                struct combine_diff_path p;
 122                                unsigned char fill[4][20];
 123                        } combine;
 124                        int num_compare_stages = 0;
 125
 126                        combine.p.next = NULL;
 127                        combine.p.len = ce_namelen(ce);
 128                        combine.p.path = xmalloc(combine.p.len + 1);
 129                        memcpy(combine.p.path, ce->name, combine.p.len);
 130                        combine.p.path[combine.p.len] = 0;
 131                        memset(combine.p.sha1, 0, 100);
 132
 133                        while (i < entries) {
 134                                struct cache_entry *nce = active_cache[i];
 135                                int stage;
 136
 137                                if (strcmp(ce->name, nce->name))
 138                                        break;
 139
 140                                /* Stage #2 (ours) is the first parent,
 141                                 * stage #3 (theirs) is the second.
 142                                 */
 143                                stage = ce_stage(nce);
 144                                if (2 <= stage) {
 145                                        num_compare_stages++;
 146                                        memcpy(combine.p.parent_sha1[stage-2],
 147                                               nce->sha1, 20);
 148                                }
 149
 150                                /* diff against the proper unmerged stage */
 151                                if (stage == diff_unmerged_stage)
 152                                        ce = nce;
 153                                i++;
 154                        }
 155                        /*
 156                         * Compensate for loop update
 157                         */
 158                        i--;
 159
 160                        if (combine_merges && num_compare_stages == 2) {
 161                                show_combined_diff(&combine.p, 2,
 162                                                   dense_combined_merges,
 163                                                   NULL, 0);
 164                                free(combine.p.path);
 165                                continue;
 166                        }
 167                        free(combine.p.path);
 168
 169                        /*
 170                         * Show the diff for the 'ce' if we found the one
 171                         * from the desired stage.
 172                         */
 173                        show_unmerge(ce->name);
 174                        if (ce_stage(ce) != diff_unmerged_stage)
 175                                continue;
 176                }
 177
 178                if (lstat(ce->name, &st) < 0) {
 179                        if (errno != ENOENT && errno != ENOTDIR) {
 180                                perror(ce->name);
 181                                continue;
 182                        }
 183                        if (silent)
 184                                continue;
 185                        show_file('-', ce);
 186                        continue;
 187                }
 188                changed = ce_match_stat(ce, &st);
 189                if (!changed && !diff_options.find_copies_harder)
 190                        continue;
 191                oldmode = ntohl(ce->ce_mode);
 192
 193                newmode = DIFF_FILE_CANON_MODE(st.st_mode);
 194                if (!trust_executable_bit &&
 195                    S_ISREG(newmode) && S_ISREG(oldmode) &&
 196                    ((newmode ^ oldmode) == 0111))
 197                        newmode = oldmode;
 198                show_modified(oldmode, newmode,
 199                              ce->sha1, (changed ? null_sha1 : ce->sha1),
 200                              ce->name);
 201        }
 202        diffcore_std(&diff_options);
 203        diff_flush(&diff_options);
 204        return 0;
 205}