blob: 17899390b80f4bfc03718e92029b5c4de65055b6 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -07007#include "diff.h"
Christopher Lic0fb9762005-04-12 02:04:44 -07008
Petr Baudis4d1f1192005-07-29 11:01:26 +02009static const char diff_files_usage[] =
Junio C Hamanodda2d792005-07-13 12:52:35 -070010"git-diff-files [-q] "
11"[<common diff options>] [<path>...]"
12COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanob8f80922005-04-16 21:29:45 -070013
Junio C Hamano6b5ee132005-09-21 00:00:47 -070014static struct diff_options diff_options;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070015static int silent = 0;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070016
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070017static void show_unmerge(const char *path)
Linus Torvalds0a7668e2005-04-26 17:17:36 -070018{
Junio C Hamano6b5ee132005-09-21 00:00:47 -070019 diff_unmerge(&diff_options, path);
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070020}
21
22static void show_file(int pfx, struct cache_entry *ce)
23{
Junio C Hamano6b5ee132005-09-21 00:00:47 -070024 diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25 ce->sha1, ce->name, NULL);
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070026}
27
28static void show_modified(int oldmode, int mode,
Brian Gerstbf0f9102005-05-18 08:14:09 -040029 const unsigned char *old_sha1, const unsigned char *sha1,
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070030 char *path)
31{
Junio C Hamano6b5ee132005-09-21 00:00:47 -070032 diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
Linus Torvalds0a7668e2005-04-26 17:17:36 -070033}
34
Junio C Hamano6b5ee132005-09-21 00:00:47 -070035int main(int argc, const char **argv)
Linus Torvaldse83c5162005-04-07 15:13:13 -070036{
Linus Torvaldsc0fd1f52005-07-14 16:55:06 -070037 const char **pathspec;
Linus Torvaldsd288a702005-08-16 18:06:34 -070038 const char *prefix = setup_git_directory();
39 int entries, i;
Linus Torvaldse83c5162005-04-07 15:13:13 -070040
Linus Torvalds17712992005-10-10 16:31:08 -070041 git_config(git_default_config);
Junio C Hamano6b5ee132005-09-21 00:00:47 -070042 diff_setup(&diff_options);
Junio C Hamanob8f80922005-04-16 21:29:45 -070043 while (1 < argc && argv[1][0] == '-') {
Linus Torvaldsea51d412005-10-18 00:16:45 -070044 if (!strcmp(argv[1], "--")) {
45 argv++;
46 argc--;
47 break;
48 }
Junio C Hamano6b5ee132005-09-21 00:00:47 -070049 if (!strcmp(argv[1], "-q"))
Junio C Hamanod15aa432005-04-27 15:22:02 -070050 silent = 1;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070051 else if (!strcmp(argv[1], "-r"))
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070052 ; /* no-op */
Junio C Hamanod15aa432005-04-27 15:22:02 -070053 else if (!strcmp(argv[1], "-s"))
54 ; /* no-op */
Junio C Hamano6b5ee132005-09-21 00:00:47 -070055 else {
56 int diff_opt_cnt;
57 diff_opt_cnt = diff_opt_parse(&diff_options,
58 argv+1, argc-1);
59 if (diff_opt_cnt < 0)
60 usage(diff_files_usage);
61 else if (diff_opt_cnt) {
62 argv += diff_opt_cnt;
63 argc -= diff_opt_cnt;
64 continue;
65 }
66 else
Junio C Hamano0e3994f2005-06-03 01:37:54 -070067 usage(diff_files_usage);
68 }
Junio C Hamanob8f80922005-04-16 21:29:45 -070069 argv++; argc--;
Petr Baudise2e5e982005-04-13 01:40:09 -070070 }
71
Linus Torvaldsd288a702005-08-16 18:06:34 -070072 /* Find the directory, and set up the pathspec */
73 pathspec = get_pathspec(prefix, argv + 1);
74 entries = read_cache();
Linus Torvaldsc0fd1f52005-07-14 16:55:06 -070075
Junio C Hamano6b5ee132005-09-21 00:00:47 -070076 if (diff_setup_done(&diff_options) < 0)
Junio C Hamano4727f642005-06-19 13:14:05 -070077 usage(diff_files_usage);
78
Junio C Hamanob8f80922005-04-16 21:29:45 -070079 /* At this point, if argc == 1, then we are doing everything.
80 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
81 */
Linus Torvaldse83c5162005-04-07 15:13:13 -070082 if (entries < 0) {
83 perror("read_cache");
84 exit(1);
85 }
Junio C Hamanobe3cfa82005-04-26 09:25:05 -070086
Linus Torvaldse83c5162005-04-07 15:13:13 -070087 for (i = 0; i < entries; i++) {
88 struct stat st;
Junio C Hamano3e09cdf2005-10-11 18:45:33 -070089 unsigned int oldmode, newmode;
Linus Torvaldse83c5162005-04-07 15:13:13 -070090 struct cache_entry *ce = active_cache[i];
Junio C Hamanod94c6122005-04-16 21:29:45 -070091 int changed;
Linus Torvaldse83c5162005-04-07 15:13:13 -070092
Linus Torvaldsc0fd1f52005-07-14 16:55:06 -070093 if (!ce_path_match(ce, pathspec))
94 continue;
95
Junio C Hamano9fec8b22005-04-16 21:29:45 -070096 if (ce_stage(ce)) {
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070097 show_unmerge(ce->name);
Junio C Hamano9fec8b22005-04-16 21:29:45 -070098 while (i < entries &&
99 !strcmp(ce->name, active_cache[i]->name))
100 i++;
101 i--; /* compensate for loop control increments */
102 continue;
103 }
Junio C Hamano57fe64a2005-05-19 19:00:36 -0700104
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200105 if (lstat(ce->name, &st) < 0) {
Junio C Hamano41174692005-05-20 09:48:38 -0700106 if (errno != ENOENT && errno != ENOTDIR) {
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700107 perror(ce->name);
Junio C Hamanoca2a0792005-04-15 15:08:09 -0700108 continue;
Junio C Hamano57fe64a2005-05-19 19:00:36 -0700109 }
Junio C Hamanod15aa432005-04-27 15:22:02 -0700110 if (silent)
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700111 continue;
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700112 show_file('-', ce);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700113 continue;
114 }
Brad Roberts5d728c82005-05-14 19:04:25 -0700115 changed = ce_match_stat(ce, &st);
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700116 if (!changed && !diff_options.find_copies_harder)
Linus Torvaldse83c5162005-04-07 15:13:13 -0700117 continue;
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700118 oldmode = ntohl(ce->ce_mode);
Junio C Hamano3e09cdf2005-10-11 18:45:33 -0700119
120 newmode = DIFF_FILE_CANON_MODE(st.st_mode);
121 if (!trust_executable_bit &&
122 S_ISREG(newmode) && S_ISREG(oldmode) &&
123 ((newmode ^ oldmode) == 0111))
124 newmode = oldmode;
125 show_modified(oldmode, newmode,
Junio C Hamano4727f642005-06-19 13:14:05 -0700126 ce->sha1, (changed ? null_sha1 : ce->sha1),
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700127 ce->name);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700128 }
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700129 diffcore_std(&diff_options);
130 diff_flush(&diff_options);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700131 return 0;
132}