diff-files.con commit git-daemon: timeout, eliminate double DWIM (b7080d8)
   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 struct diff_options diff_options;
  15static int silent = 0;
  16
  17static void show_unmerge(const char *path)
  18{
  19        diff_unmerge(&diff_options, path);
  20}
  21
  22static void show_file(int pfx, struct cache_entry *ce)
  23{
  24        diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
  25                       ce->sha1, ce->name, NULL);
  26}
  27
  28static void show_modified(int oldmode, int mode,
  29                          const unsigned char *old_sha1, const unsigned char *sha1,
  30                          char *path)
  31{
  32        diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
  33}
  34
  35int main(int argc, const char **argv)
  36{
  37        const char **pathspec;
  38        const char *prefix = setup_git_directory();
  39        int entries, i;
  40
  41        diff_setup(&diff_options);
  42        while (1 < argc && argv[1][0] == '-') {
  43                if (!strcmp(argv[1], "-q"))
  44                        silent = 1;
  45                else if (!strcmp(argv[1], "-r"))
  46                        ; /* no-op */
  47                else if (!strcmp(argv[1], "-s"))
  48                        ; /* no-op */
  49                else {
  50                        int diff_opt_cnt;
  51                        diff_opt_cnt = diff_opt_parse(&diff_options,
  52                                                      argv+1, argc-1);
  53                        if (diff_opt_cnt < 0)
  54                                usage(diff_files_usage);
  55                        else if (diff_opt_cnt) {
  56                                argv += diff_opt_cnt;
  57                                argc -= diff_opt_cnt;
  58                                continue;
  59                        }
  60                        else
  61                                usage(diff_files_usage);
  62                }
  63                argv++; argc--;
  64        }
  65
  66        /* Find the directory, and set up the pathspec */
  67        pathspec = get_pathspec(prefix, argv + 1);
  68        entries = read_cache();
  69
  70        if (diff_setup_done(&diff_options) < 0)
  71                usage(diff_files_usage);
  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        for (i = 0; i < entries; i++) {
  82                struct stat st;
  83                unsigned int oldmode;
  84                struct cache_entry *ce = active_cache[i];
  85                int changed;
  86
  87                if (!ce_path_match(ce, pathspec))
  88                        continue;
  89
  90                if (ce_stage(ce)) {
  91                        show_unmerge(ce->name);
  92                        while (i < entries &&
  93                               !strcmp(ce->name, active_cache[i]->name))
  94                                i++;
  95                        i--; /* compensate for loop control increments */
  96                        continue;
  97                }
  98
  99                if (lstat(ce->name, &st) < 0) {
 100                        if (errno != ENOENT && errno != ENOTDIR) {
 101                                perror(ce->name);
 102                                continue;
 103                        }
 104                        if (silent)
 105                                continue;
 106                        show_file('-', ce);
 107                        continue;
 108                }
 109                changed = ce_match_stat(ce, &st);
 110                if (!changed && !diff_options.find_copies_harder)
 111                        continue;
 112                oldmode = ntohl(ce->ce_mode);
 113                show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
 114                              ce->sha1, (changed ? null_sha1 : ce->sha1),
 115                              ce->name);
 116        }
 117        diffcore_std(&diff_options);
 118        diff_flush(&diff_options);
 119        return 0;
 120}