diff-files.con commit Update tutorial. (914328a)
   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] "
  11"[<common diff options>] [<path>...]"
  12COMMON_DIFF_OPTIONS_HELP;
  13
  14static int diff_output_format = DIFF_FORMAT_RAW;
  15static int diff_line_termination = '\n';
  16static int detect_rename = 0;
  17static int find_copies_harder = 0;
  18static int diff_setup_opt = 0;
  19static int diff_score_opt = 0;
  20static const char *pickaxe = NULL;
  21static int pickaxe_opts = 0;
  22static int diff_break_opt = -1;
  23static const char *orderfile = NULL;
  24static const char *diff_filter = NULL;
  25static int silent = 0;
  26
  27static void show_unmerge(const char *path)
  28{
  29        diff_unmerge(path);
  30}
  31
  32static void show_file(int pfx, struct cache_entry *ce)
  33{
  34        diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
  35}
  36
  37static void show_modified(int oldmode, int mode,
  38                          const unsigned char *old_sha1, const unsigned char *sha1,
  39                          char *path)
  40{
  41        diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
  42}
  43
  44int main(int argc, char **argv)
  45{
  46        static const unsigned char null_sha1[20] = { 0, };
  47        const char **pathspec;
  48        const char *prefix = setup_git_directory();
  49        int entries, i;
  50
  51        while (1 < argc && argv[1][0] == '-') {
  52                if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
  53                        diff_output_format = DIFF_FORMAT_PATCH;
  54                else if (!strcmp(argv[1], "-q"))
  55                        silent = 1;
  56                else if (!strcmp(argv[1], "-r"))
  57                        ; /* no-op */
  58                else if (!strcmp(argv[1], "-s"))
  59                        ; /* no-op */
  60                else if (!strcmp(argv[1], "-z"))
  61                        diff_line_termination = 0;
  62                else if (!strcmp(argv[1], "--name-only"))
  63                        diff_output_format = DIFF_FORMAT_NAME;
  64                else if (!strcmp(argv[1], "-R"))
  65                        diff_setup_opt |= DIFF_SETUP_REVERSE;
  66                else if (!strncmp(argv[1], "-S", 2))
  67                        pickaxe = argv[1] + 2;
  68                else if (!strncmp(argv[1], "-O", 2))
  69                        orderfile = argv[1] + 2;
  70                else if (!strncmp(argv[1], "--diff-filter=", 14))
  71                        diff_filter = argv[1] + 14;
  72                else if (!strcmp(argv[1], "--pickaxe-all"))
  73                        pickaxe_opts = DIFF_PICKAXE_ALL;
  74                else if (!strncmp(argv[1], "-B", 2)) {
  75                        if ((diff_break_opt =
  76                             diff_scoreopt_parse(argv[1])) == -1)
  77                                usage(diff_files_usage);
  78                }
  79                else if (!strncmp(argv[1], "-M", 2)) {
  80                        if ((diff_score_opt =
  81                             diff_scoreopt_parse(argv[1])) == -1)
  82                                usage(diff_files_usage);
  83                        detect_rename = DIFF_DETECT_RENAME;
  84                }
  85                else if (!strncmp(argv[1], "-C", 2)) {
  86                        if ((diff_score_opt =
  87                             diff_scoreopt_parse(argv[1])) == -1)
  88                                usage(diff_files_usage);
  89                        detect_rename = DIFF_DETECT_COPY;
  90                }
  91                else if (!strcmp(argv[1], "--find-copies-harder"))
  92                        find_copies_harder = 1;
  93                else
  94                        usage(diff_files_usage);
  95                argv++; argc--;
  96        }
  97
  98        /* Find the directory, and set up the pathspec */
  99        pathspec = get_pathspec(prefix, argv + 1);
 100        entries = read_cache();
 101
 102        if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
 103                usage(diff_files_usage);
 104
 105        /* At this point, if argc == 1, then we are doing everything.
 106         * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
 107         */
 108        if (entries < 0) {
 109                perror("read_cache");
 110                exit(1);
 111        }
 112
 113        diff_setup(diff_setup_opt);
 114
 115        for (i = 0; i < entries; i++) {
 116                struct stat st;
 117                unsigned int oldmode;
 118                struct cache_entry *ce = active_cache[i];
 119                int changed;
 120
 121                if (!ce_path_match(ce, pathspec))
 122                        continue;
 123
 124                if (ce_stage(ce)) {
 125                        show_unmerge(ce->name);
 126                        while (i < entries &&
 127                               !strcmp(ce->name, active_cache[i]->name))
 128                                i++;
 129                        i--; /* compensate for loop control increments */
 130                        continue;
 131                }
 132
 133                if (lstat(ce->name, &st) < 0) {
 134                        if (errno != ENOENT && errno != ENOTDIR) {
 135                                perror(ce->name);
 136                                continue;
 137                        }
 138                        if (silent)
 139                                continue;
 140                        show_file('-', ce);
 141                        continue;
 142                }
 143                changed = ce_match_stat(ce, &st);
 144                if (!changed && !find_copies_harder)
 145                        continue;
 146                oldmode = ntohl(ce->ce_mode);
 147                show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
 148                              ce->sha1, (changed ? null_sha1 : ce->sha1),
 149                              ce->name);
 150        }
 151        diffcore_std(pathspec, 
 152                     detect_rename, diff_score_opt,
 153                     pickaxe, pickaxe_opts,
 154                     diff_break_opt,
 155                     orderfile, diff_filter);
 156        diff_flush(diff_output_format, diff_line_termination);
 157        return 0;
 158}