cf7721928535e2350c1e32b34bc0ca5bc6e77aab
   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 diff_output_format = DIFF_FORMAT_HUMAN;
  13static int detect_rename = 0;
  14static int diff_setup_opt = 0;
  15static int diff_score_opt = 0;
  16static const char *pickaxe = NULL;
  17static int pickaxe_opts = 0;
  18static int silent = 0;
  19
  20static void show_unmerge(const char *path)
  21{
  22        diff_unmerge(path);
  23}
  24
  25static void show_file(int pfx, struct cache_entry *ce)
  26{
  27        diff_addremove(pfx, ntohl(ce->ce_mode), 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(oldmode, mode, old_sha1, sha1, path, NULL);
  35}
  36
  37int main(int argc, const char **argv)
  38{
  39        static const unsigned char null_sha1[20] = { 0, };
  40        int entries = read_cache();
  41        int i;
  42
  43        while (1 < argc && argv[1][0] == '-') {
  44                if (!strcmp(argv[1], "-p"))
  45                        diff_output_format = DIFF_FORMAT_PATCH;
  46                else if (!strcmp(argv[1], "-q"))
  47                        silent = 1;
  48                else if (!strcmp(argv[1], "-r"))
  49                        ; /* no-op */
  50                else if (!strcmp(argv[1], "-s"))
  51                        ; /* no-op */
  52                else if (!strcmp(argv[1], "-z"))
  53                        diff_output_format = DIFF_FORMAT_MACHINE;
  54                else if (!strcmp(argv[1], "-R"))
  55                        diff_setup_opt |= DIFF_SETUP_REVERSE;
  56                else if (!strcmp(argv[1], "-S"))
  57                        pickaxe = argv[1] + 2;
  58                else if (!strcmp(argv[1], "--pickaxe-all"))
  59                        pickaxe_opts = DIFF_PICKAXE_ALL;
  60                else if (!strncmp(argv[1], "-M", 2)) {
  61                        diff_score_opt = diff_scoreopt_parse(argv[1]);
  62                        detect_rename = DIFF_DETECT_RENAME;
  63                }
  64                else if (!strncmp(argv[1], "-C", 2)) {
  65                        diff_score_opt = diff_scoreopt_parse(argv[1]);
  66                        detect_rename = DIFF_DETECT_COPY;
  67                }
  68                else
  69                        usage(diff_files_usage);
  70                argv++; argc--;
  71        }
  72
  73        /* At this point, if argc == 1, then we are doing everything.
  74         * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
  75         */
  76        if (entries < 0) {
  77                perror("read_cache");
  78                exit(1);
  79        }
  80
  81        diff_setup(diff_setup_opt);
  82
  83        for (i = 0; i < entries; i++) {
  84                struct stat st;
  85                unsigned int oldmode, mode;
  86                struct cache_entry *ce = active_cache[i];
  87                int changed;
  88
  89                if (ce_stage(ce)) {
  90                        show_unmerge(ce->name);
  91                        while (i < entries &&
  92                               !strcmp(ce->name, active_cache[i]->name))
  93                                i++;
  94                        i--; /* compensate for loop control increments */
  95                        continue;
  96                }
  97
  98                if (lstat(ce->name, &st) < 0) {
  99                        if (errno != ENOENT && errno != ENOTDIR) {
 100                                perror(ce->name);
 101                                continue;
 102                        }
 103                        if (silent)
 104                                continue;
 105                        show_file('-', ce);
 106                        continue;
 107                }
 108                changed = ce_match_stat(ce, &st);
 109                if (!changed && detect_rename < DIFF_DETECT_COPY)
 110                        continue;
 111
 112                oldmode = ntohl(ce->ce_mode);
 113                mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
 114                        S_IFREG | ce_permissions(st.st_mode));
 115
 116                show_modified(oldmode, mode, ce->sha1, null_sha1,
 117                              ce->name);
 118        }
 119        if (detect_rename)
 120                diffcore_rename(detect_rename, diff_score_opt);
 121        if (pickaxe)
 122                diffcore_pickaxe(pickaxe, pickaxe_opts);
 123        if (1 < argc)
 124                diffcore_pathspec(argv + 1);
 125        diff_flush(diff_output_format, 1);
 126        return 0;
 127}