diff-files.con commit [PATCH] Prepare diffcore interface for diff-tree header supression. (38c6f78)
   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 [-p] [-q] [-r] [-z] [-M] [-C] [-R] [-S<string>] [paths...]";
  11
  12static int generate_patch = 0;
  13static int line_termination = '\n';
  14static int detect_rename = 0;
  15static int reverse_diff = 0;
  16static int diff_score_opt = 0;
  17static const char *pickaxe = NULL;
  18static int silent = 0;
  19
  20static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
  21{
  22        int i;
  23        int namelen = ce_namelen(ce);
  24        for (i = 0; i < cnt; i++) {
  25                int speclen = strlen(spec[i]);
  26                if (! strncmp(spec[i], ce->name, speclen) &&
  27                    speclen <= namelen &&
  28                    (ce->name[speclen] == 0 ||
  29                     ce->name[speclen] == '/'))
  30                        return 1;
  31        }
  32        return 0;
  33}
  34
  35static void show_unmerge(const char *path)
  36{
  37        diff_unmerge(path);
  38}
  39
  40static void show_file(int pfx, struct cache_entry *ce)
  41{
  42        diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
  43}
  44
  45static void show_modified(int oldmode, int mode,
  46                          const unsigned char *old_sha1, const unsigned char *sha1,
  47                          char *path)
  48{
  49        diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
  50}
  51
  52int main(int argc, char **argv)
  53{
  54        static const unsigned char null_sha1[20] = { 0, };
  55        int entries = read_cache();
  56        int i;
  57
  58        while (1 < argc && argv[1][0] == '-') {
  59                if (!strcmp(argv[1], "-p"))
  60                        generate_patch = 1;
  61                else if (!strcmp(argv[1], "-q"))
  62                        silent = 1;
  63                else if (!strcmp(argv[1], "-r"))
  64                        ; /* no-op */
  65                else if (!strcmp(argv[1], "-s"))
  66                        ; /* no-op */
  67                else if (!strcmp(argv[1], "-z"))
  68                        line_termination = 0;
  69                else if (!strcmp(argv[1], "-R"))
  70                        reverse_diff = 1;
  71                else if (!strcmp(argv[1], "-S"))
  72                        pickaxe = argv[1] + 2;
  73                else if (!strncmp(argv[1], "-M", 2)) {
  74                        diff_score_opt = diff_scoreopt_parse(argv[1]);
  75                        detect_rename = generate_patch = 1;
  76                }
  77                else if (!strncmp(argv[1], "-C", 2)) {
  78                        diff_score_opt = diff_scoreopt_parse(argv[1]);
  79                        detect_rename = 2;
  80                        generate_patch = 1;
  81                }
  82                else
  83                        usage(diff_files_usage);
  84                argv++; argc--;
  85        }
  86
  87        /* At this point, if argc == 1, then we are doing everything.
  88         * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
  89         */
  90        if (entries < 0) {
  91                perror("read_cache");
  92                exit(1);
  93        }
  94
  95        diff_setup(reverse_diff, (generate_patch ? -1 : line_termination));
  96
  97        for (i = 0; i < entries; i++) {
  98                struct stat st;
  99                unsigned int oldmode, mode;
 100                struct cache_entry *ce = active_cache[i];
 101                int changed;
 102
 103                if (1 < argc &&
 104                    ! matches_pathspec(ce, argv+1, argc-1))
 105                        continue;
 106
 107                if (ce_stage(ce)) {
 108                        show_unmerge(ce->name);
 109                        while (i < entries &&
 110                               !strcmp(ce->name, active_cache[i]->name))
 111                                i++;
 112                        i--; /* compensate for loop control increments */
 113                        continue;
 114                }
 115
 116                if (lstat(ce->name, &st) < 0) {
 117                        if (errno != ENOENT && errno != ENOTDIR) {
 118                                perror(ce->name);
 119                                continue;
 120                        }
 121                        if (silent)
 122                                continue;
 123                        show_file('-', ce);
 124                        continue;
 125                }
 126                changed = ce_match_stat(ce, &st);
 127                if (!changed && detect_rename < 2)
 128                        continue;
 129
 130                oldmode = ntohl(ce->ce_mode);
 131                mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
 132                        S_IFREG | ce_permissions(st.st_mode));
 133
 134                show_modified(oldmode, mode, ce->sha1, null_sha1,
 135                              ce->name);
 136        }
 137        if (detect_rename)
 138                diff_detect_rename(detect_rename, diff_score_opt);
 139        if (pickaxe)
 140                diff_pickaxe(pickaxe);
 141        diff_flush(NULL, 0);
 142        return 0;
 143}