blob: 89eb29b3e26678a294353ec4b6f52ca3285c7adc [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
Linus Torvaldse68b6f12005-07-14 17:59:17 -070014static int diff_output_format = DIFF_FORMAT_RAW;
15static int diff_line_termination = '\n';
Junio C Hamano5c975582005-05-19 03:32:35 -070016static int detect_rename = 0;
Junio C Hamano4727f642005-06-19 13:14:05 -070017static int find_copies_harder = 0;
Junio C Hamano19feebc2005-05-27 15:54:37 -070018static int diff_setup_opt = 0;
Junio C Hamano57fe64a2005-05-19 19:00:36 -070019static int diff_score_opt = 0;
Junio C Hamano057c7d32005-05-21 15:02:51 -070020static const char *pickaxe = NULL;
Junio C Hamano367cec12005-05-27 15:55:28 -070021static int pickaxe_opts = 0;
Junio C Hamanof345b0a2005-05-30 00:08:37 -070022static int diff_break_opt = -1;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070023static const char *orderfile = NULL;
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -070024static const char *diff_filter = NULL;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070025static int silent = 0;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070026
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070027static void show_unmerge(const char *path)
Linus Torvalds0a7668e2005-04-26 17:17:36 -070028{
Junio C Hamano57fe64a2005-05-19 19:00:36 -070029 diff_unmerge(path);
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070030}
31
32static void show_file(int pfx, struct cache_entry *ce)
33{
Junio C Hamano57fe64a2005-05-19 19:00:36 -070034 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070035}
36
37static void show_modified(int oldmode, int mode,
Brian Gerstbf0f9102005-05-18 08:14:09 -040038 const unsigned char *old_sha1, const unsigned char *sha1,
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070039 char *path)
40{
Junio C Hamano57fe64a2005-05-19 19:00:36 -070041 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
Linus Torvalds0a7668e2005-04-26 17:17:36 -070042}
43
Linus Torvaldsd288a702005-08-16 18:06:34 -070044int main(int argc, char **argv)
Linus Torvaldse83c5162005-04-07 15:13:13 -070045{
Brian Gerstbf0f9102005-05-18 08:14:09 -040046 static const unsigned char null_sha1[20] = { 0, };
Linus Torvaldsc0fd1f52005-07-14 16:55:06 -070047 const char **pathspec;
Linus Torvaldsd288a702005-08-16 18:06:34 -070048 const char *prefix = setup_git_directory();
49 int entries, i;
Linus Torvaldse83c5162005-04-07 15:13:13 -070050
Junio C Hamanob8f80922005-04-16 21:29:45 -070051 while (1 < argc && argv[1][0] == '-') {
Linus Torvaldsacb46f82005-07-08 10:45:07 -070052 if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
Junio C Hamano81e50ea2005-05-21 19:42:18 -070053 diff_output_format = DIFF_FORMAT_PATCH;
Junio C Hamanob8f80922005-04-16 21:29:45 -070054 else if (!strcmp(argv[1], "-q"))
Junio C Hamanod15aa432005-04-27 15:22:02 -070055 silent = 1;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070056 else if (!strcmp(argv[1], "-r"))
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070057 ; /* no-op */
Junio C Hamanod15aa432005-04-27 15:22:02 -070058 else if (!strcmp(argv[1], "-s"))
59 ; /* no-op */
60 else if (!strcmp(argv[1], "-z"))
Linus Torvaldse68b6f12005-07-14 17:59:17 -070061 diff_line_termination = 0;
Junio C Hamano52f28522005-07-13 12:45:51 -070062 else if (!strcmp(argv[1], "--name-only"))
63 diff_output_format = DIFF_FORMAT_NAME;
Junio C Hamano57fe64a2005-05-19 19:00:36 -070064 else if (!strcmp(argv[1], "-R"))
Junio C Hamano19feebc2005-05-27 15:54:37 -070065 diff_setup_opt |= DIFF_SETUP_REVERSE;
Junio C Hamanoe25de752005-05-28 02:53:43 -070066 else if (!strncmp(argv[1], "-S", 2))
Junio C Hamano52e95782005-05-21 02:40:01 -070067 pickaxe = argv[1] + 2;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070068 else if (!strncmp(argv[1], "-O", 2))
69 orderfile = argv[1] + 2;
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -070070 else if (!strncmp(argv[1], "--diff-filter=", 14))
71 diff_filter = argv[1] + 14;
Junio C Hamano367cec12005-05-27 15:55:28 -070072 else if (!strcmp(argv[1], "--pickaxe-all"))
73 pickaxe_opts = DIFF_PICKAXE_ALL;
Junio C Hamano0e3994f2005-06-03 01:37:54 -070074 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 }
Junio C Hamano57fe64a2005-05-19 19:00:36 -070079 else if (!strncmp(argv[1], "-M", 2)) {
Junio C Hamano0e3994f2005-06-03 01:37:54 -070080 if ((diff_score_opt =
81 diff_scoreopt_parse(argv[1])) == -1)
82 usage(diff_files_usage);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070083 detect_rename = DIFF_DETECT_RENAME;
Junio C Hamano5c975582005-05-19 03:32:35 -070084 }
Junio C Hamano427dcb42005-05-21 02:39:09 -070085 else if (!strncmp(argv[1], "-C", 2)) {
Junio C Hamano0e3994f2005-06-03 01:37:54 -070086 if ((diff_score_opt =
87 diff_scoreopt_parse(argv[1])) == -1)
88 usage(diff_files_usage);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070089 detect_rename = DIFF_DETECT_COPY;
Junio C Hamano427dcb42005-05-21 02:39:09 -070090 }
Junio C Hamano4727f642005-06-19 13:14:05 -070091 else if (!strcmp(argv[1], "--find-copies-harder"))
92 find_copies_harder = 1;
Junio C Hamanob8f80922005-04-16 21:29:45 -070093 else
Nicolas Pitre17710392005-04-30 13:59:38 -070094 usage(diff_files_usage);
Junio C Hamanob8f80922005-04-16 21:29:45 -070095 argv++; argc--;
Petr Baudise2e5e982005-04-13 01:40:09 -070096 }
97
Linus Torvaldsd288a702005-08-16 18:06:34 -070098 /* Find the directory, and set up the pathspec */
99 pathspec = get_pathspec(prefix, argv + 1);
100 entries = read_cache();
Linus Torvaldsc0fd1f52005-07-14 16:55:06 -0700101
Junio C Hamano4727f642005-06-19 13:14:05 -0700102 if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
103 usage(diff_files_usage);
104
Junio C Hamanob8f80922005-04-16 21:29:45 -0700105 /* At this point, if argc == 1, then we are doing everything.
106 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
107 */
Linus Torvaldse83c5162005-04-07 15:13:13 -0700108 if (entries < 0) {
109 perror("read_cache");
110 exit(1);
111 }
Junio C Hamanobe3cfa82005-04-26 09:25:05 -0700112
Junio C Hamano19feebc2005-05-27 15:54:37 -0700113 diff_setup(diff_setup_opt);
Junio C Hamano5c975582005-05-19 03:32:35 -0700114
Linus Torvaldse83c5162005-04-07 15:13:13 -0700115 for (i = 0; i < entries; i++) {
116 struct stat st;
Junio C Hamano67574c42005-06-01 11:38:07 -0700117 unsigned int oldmode;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700118 struct cache_entry *ce = active_cache[i];
Junio C Hamanod94c6122005-04-16 21:29:45 -0700119 int changed;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700120
Linus Torvaldsc0fd1f52005-07-14 16:55:06 -0700121 if (!ce_path_match(ce, pathspec))
122 continue;
123
Junio C Hamano9fec8b22005-04-16 21:29:45 -0700124 if (ce_stage(ce)) {
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700125 show_unmerge(ce->name);
Junio C Hamano9fec8b22005-04-16 21:29:45 -0700126 while (i < entries &&
127 !strcmp(ce->name, active_cache[i]->name))
128 i++;
129 i--; /* compensate for loop control increments */
130 continue;
131 }
Junio C Hamano57fe64a2005-05-19 19:00:36 -0700132
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200133 if (lstat(ce->name, &st) < 0) {
Junio C Hamano41174692005-05-20 09:48:38 -0700134 if (errno != ENOENT && errno != ENOTDIR) {
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700135 perror(ce->name);
Junio C Hamanoca2a0792005-04-15 15:08:09 -0700136 continue;
Junio C Hamano57fe64a2005-05-19 19:00:36 -0700137 }
Junio C Hamanod15aa432005-04-27 15:22:02 -0700138 if (silent)
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700139 continue;
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700140 show_file('-', ce);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700141 continue;
142 }
Brad Roberts5d728c82005-05-14 19:04:25 -0700143 changed = ce_match_stat(ce, &st);
Junio C Hamano4727f642005-06-19 13:14:05 -0700144 if (!changed && !find_copies_harder)
Linus Torvaldse83c5162005-04-07 15:13:13 -0700145 continue;
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700146 oldmode = ntohl(ce->ce_mode);
Junio C Hamano67574c42005-06-01 11:38:07 -0700147 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
Junio C Hamano4727f642005-06-19 13:14:05 -0700148 ce->sha1, (changed ? null_sha1 : ce->sha1),
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700149 ce->name);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700150 }
Linus Torvaldsc0fd1f52005-07-14 16:55:06 -0700151 diffcore_std(pathspec,
Junio C Hamanobefe8632005-05-29 16:56:13 -0700152 detect_rename, diff_score_opt,
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700153 pickaxe, pickaxe_opts,
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700154 diff_break_opt,
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -0700155 orderfile, diff_filter);
Linus Torvaldse68b6f12005-07-14 17:59:17 -0700156 diff_flush(diff_output_format, diff_line_termination);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700157 return 0;
158}