diff-files.con commit [PATCH] Diff overhaul, adding the other half of copy detection. (c3e7fbc)
   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 char *pickaxe = 0;
  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(detect_rename, diff_score_opt, pickaxe,
  96                   reverse_diff, (generate_patch ? -1 : line_termination),
  97                   NULL, 0);
  98
  99        for (i = 0; i < entries; i++) {
 100                struct stat st;
 101                unsigned int oldmode, mode;
 102                struct cache_entry *ce = active_cache[i];
 103                int changed;
 104
 105                if (1 < argc &&
 106                    ! matches_pathspec(ce, argv+1, argc-1))
 107                        continue;
 108
 109                if (ce_stage(ce)) {
 110                        show_unmerge(ce->name);
 111                        while (i < entries &&
 112                               !strcmp(ce->name, active_cache[i]->name))
 113                                i++;
 114                        i--; /* compensate for loop control increments */
 115                        continue;
 116                }
 117
 118                if (lstat(ce->name, &st) < 0) {
 119                        if (errno != ENOENT && errno != ENOTDIR) {
 120                                perror(ce->name);
 121                                continue;
 122                        }
 123                        if (silent)
 124                                continue;
 125                        show_file('-', ce);
 126                        continue;
 127                }
 128                changed = ce_match_stat(ce, &st);
 129                if (!changed && detect_rename < 2)
 130                        continue;
 131
 132                oldmode = ntohl(ce->ce_mode);
 133                mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
 134                        S_IFREG | ce_permissions(st.st_mode));
 135
 136                show_modified(oldmode, mode, ce->sha1, null_sha1,
 137                              ce->name);
 138        }
 139        diff_flush();
 140        return 0;
 141}