1/*2* GIT - The information manager from hell3*4* Copyright (C) Linus Torvalds, 20055*/6#include "cache.h"7#include "diff.h"89static const char diff_files_usage[] =10"git-diff-files [-q] [-0/-1/2/3] [<common diff options>] [<path>...]"11COMMON_DIFF_OPTIONS_HELP;1213static struct diff_options diff_options;14static int silent = 0;15static int diff_unmerged_stage = 2;1617static void show_unmerge(const char *path)18{19diff_unmerge(&diff_options, path);20}2122static void show_file(int pfx, struct cache_entry *ce)23{24diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),25ce->sha1, ce->name, NULL);26}2728static void show_modified(int oldmode, int mode,29const unsigned char *old_sha1, const unsigned char *sha1,30char *path)31{32diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);33}3435int main(int argc, const char **argv)36{37const char **pathspec;38const char *prefix = setup_git_directory();39int entries, i;4041git_config(git_diff_config);42diff_setup(&diff_options);43while (1 < argc && argv[1][0] == '-') {44if (!strcmp(argv[1], "--")) {45argv++;46argc--;47break;48}49if (!strcmp(argv[1], "-0"))50diff_unmerged_stage = 0;51else if (!strcmp(argv[1], "-1"))52diff_unmerged_stage = 1;53else if (!strcmp(argv[1], "-2"))54diff_unmerged_stage = 2;55else if (!strcmp(argv[1], "-3"))56diff_unmerged_stage = 3;57else if (!strcmp(argv[1], "--base"))58diff_unmerged_stage = 1;59else if (!strcmp(argv[1], "--ours"))60diff_unmerged_stage = 2;61else if (!strcmp(argv[1], "--theirs"))62diff_unmerged_stage = 3;63else if (!strcmp(argv[1], "-q"))64silent = 1;65else if (!strcmp(argv[1], "-r"))66; /* no-op */67else if (!strcmp(argv[1], "-s"))68; /* no-op */69else {70int diff_opt_cnt;71diff_opt_cnt = diff_opt_parse(&diff_options,72argv+1, argc-1);73if (diff_opt_cnt < 0)74usage(diff_files_usage);75else if (diff_opt_cnt) {76argv += diff_opt_cnt;77argc -= diff_opt_cnt;78continue;79}80else81usage(diff_files_usage);82}83argv++; argc--;84}8586/* Find the directory, and set up the pathspec */87pathspec = get_pathspec(prefix, argv + 1);88entries = read_cache();8990if (diff_setup_done(&diff_options) < 0)91usage(diff_files_usage);9293/* At this point, if argc == 1, then we are doing everything.94* Otherwise argv[1] .. argv[argc-1] have the explicit paths.95*/96if (entries < 0) {97perror("read_cache");98exit(1);99}100101for (i = 0; i < entries; i++) {102struct stat st;103unsigned int oldmode, newmode;104struct cache_entry *ce = active_cache[i];105int changed;106107if (!ce_path_match(ce, pathspec))108continue;109110if (ce_stage(ce)) {111show_unmerge(ce->name);112while (i < entries) {113struct cache_entry *nce = active_cache[i];114115if (strcmp(ce->name, nce->name))116break;117/* diff against the proper unmerged stage */118if (ce_stage(nce) == diff_unmerged_stage)119ce = nce;120i++;121}122/*123* Compensate for loop update124*/125i--;126/*127* Show the diff for the 'ce' if we found the one128* from the desired stage.129*/130if (ce_stage(ce) != diff_unmerged_stage)131continue;132}133134if (lstat(ce->name, &st) < 0) {135if (errno != ENOENT && errno != ENOTDIR) {136perror(ce->name);137continue;138}139if (silent)140continue;141show_file('-', ce);142continue;143}144changed = ce_match_stat(ce, &st);145if (!changed && !diff_options.find_copies_harder)146continue;147oldmode = ntohl(ce->ce_mode);148149newmode = DIFF_FILE_CANON_MODE(st.st_mode);150if (!trust_executable_bit &&151S_ISREG(newmode) && S_ISREG(oldmode) &&152((newmode ^ oldmode) == 0111))153newmode = oldmode;154show_modified(oldmode, newmode,155ce->sha1, (changed ? null_sha1 : ce->sha1),156ce->name);157}158diffcore_std(&diff_options);159diff_flush(&diff_options);160return 0;161}