blob: 4d60017e46b1aa54ea07a85b478592408dc34233 [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
Nicolas Pitre17710392005-04-30 13:59:38 -07009static const char *diff_files_usage =
Junio C Hamano4727f642005-06-19 13:14:05 -070010"git-diff-files [-p] [-q] [-r] [-z] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] [<path>...]";
Junio C Hamanob8f80922005-04-16 21:29:45 -070011
Junio C Hamano81e50ea2005-05-21 19:42:18 -070012static int diff_output_format = DIFF_FORMAT_HUMAN;
Junio C Hamano5c975582005-05-19 03:32:35 -070013static int detect_rename = 0;
Junio C Hamano4727f642005-06-19 13:14:05 -070014static int find_copies_harder = 0;
Junio C Hamano19feebc2005-05-27 15:54:37 -070015static int diff_setup_opt = 0;
Junio C Hamano57fe64a2005-05-19 19:00:36 -070016static int diff_score_opt = 0;
Junio C Hamano057c7d32005-05-21 15:02:51 -070017static const char *pickaxe = NULL;
Junio C Hamano367cec12005-05-27 15:55:28 -070018static int pickaxe_opts = 0;
Junio C Hamanof345b0a2005-05-30 00:08:37 -070019static int diff_break_opt = -1;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070020static const char *orderfile = NULL;
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -070021static const char *diff_filter = NULL;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070022static int silent = 0;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070023
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070024static void show_unmerge(const char *path)
Linus Torvalds0a7668e2005-04-26 17:17:36 -070025{
Junio C Hamano57fe64a2005-05-19 19:00:36 -070026 diff_unmerge(path);
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070027}
28
29static void show_file(int pfx, struct cache_entry *ce)
30{
Junio C Hamano57fe64a2005-05-19 19:00:36 -070031 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070032}
33
34static void show_modified(int oldmode, int mode,
Brian Gerstbf0f9102005-05-18 08:14:09 -040035 const unsigned char *old_sha1, const unsigned char *sha1,
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070036 char *path)
37{
Junio C Hamano57fe64a2005-05-19 19:00:36 -070038 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
Linus Torvalds0a7668e2005-04-26 17:17:36 -070039}
40
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070041int main(int argc, const char **argv)
Linus Torvaldse83c5162005-04-07 15:13:13 -070042{
Brian Gerstbf0f9102005-05-18 08:14:09 -040043 static const unsigned char null_sha1[20] = { 0, };
Linus Torvaldse83c5162005-04-07 15:13:13 -070044 int entries = read_cache();
45 int i;
46
Junio C Hamanob8f80922005-04-16 21:29:45 -070047 while (1 < argc && argv[1][0] == '-') {
Junio C Hamanod15aa432005-04-27 15:22:02 -070048 if (!strcmp(argv[1], "-p"))
Junio C Hamano81e50ea2005-05-21 19:42:18 -070049 diff_output_format = DIFF_FORMAT_PATCH;
Junio C Hamanob8f80922005-04-16 21:29:45 -070050 else if (!strcmp(argv[1], "-q"))
Junio C Hamanod15aa432005-04-27 15:22:02 -070051 silent = 1;
Linus Torvalds0a7668e2005-04-26 17:17:36 -070052 else if (!strcmp(argv[1], "-r"))
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -070053 ; /* no-op */
Junio C Hamanod15aa432005-04-27 15:22:02 -070054 else if (!strcmp(argv[1], "-s"))
55 ; /* no-op */
56 else if (!strcmp(argv[1], "-z"))
Junio C Hamano81e50ea2005-05-21 19:42:18 -070057 diff_output_format = DIFF_FORMAT_MACHINE;
Junio C Hamano57fe64a2005-05-19 19:00:36 -070058 else if (!strcmp(argv[1], "-R"))
Junio C Hamano19feebc2005-05-27 15:54:37 -070059 diff_setup_opt |= DIFF_SETUP_REVERSE;
Junio C Hamanoe25de752005-05-28 02:53:43 -070060 else if (!strncmp(argv[1], "-S", 2))
Junio C Hamano52e95782005-05-21 02:40:01 -070061 pickaxe = argv[1] + 2;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070062 else if (!strncmp(argv[1], "-O", 2))
63 orderfile = argv[1] + 2;
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -070064 else if (!strncmp(argv[1], "--diff-filter=", 14))
65 diff_filter = argv[1] + 14;
Junio C Hamano367cec12005-05-27 15:55:28 -070066 else if (!strcmp(argv[1], "--pickaxe-all"))
67 pickaxe_opts = DIFF_PICKAXE_ALL;
Junio C Hamano0e3994f2005-06-03 01:37:54 -070068 else if (!strncmp(argv[1], "-B", 2)) {
69 if ((diff_break_opt =
70 diff_scoreopt_parse(argv[1])) == -1)
71 usage(diff_files_usage);
72 }
Junio C Hamano57fe64a2005-05-19 19:00:36 -070073 else if (!strncmp(argv[1], "-M", 2)) {
Junio C Hamano0e3994f2005-06-03 01:37:54 -070074 if ((diff_score_opt =
75 diff_scoreopt_parse(argv[1])) == -1)
76 usage(diff_files_usage);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070077 detect_rename = DIFF_DETECT_RENAME;
Junio C Hamano5c975582005-05-19 03:32:35 -070078 }
Junio C Hamano427dcb42005-05-21 02:39:09 -070079 else if (!strncmp(argv[1], "-C", 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_COPY;
Junio C Hamano427dcb42005-05-21 02:39:09 -070084 }
Junio C Hamano4727f642005-06-19 13:14:05 -070085 else if (!strcmp(argv[1], "--find-copies-harder"))
86 find_copies_harder = 1;
Junio C Hamanob8f80922005-04-16 21:29:45 -070087 else
Nicolas Pitre17710392005-04-30 13:59:38 -070088 usage(diff_files_usage);
Junio C Hamanob8f80922005-04-16 21:29:45 -070089 argv++; argc--;
Petr Baudise2e5e982005-04-13 01:40:09 -070090 }
91
Junio C Hamano4727f642005-06-19 13:14:05 -070092 if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
93 usage(diff_files_usage);
94
Junio C Hamanob8f80922005-04-16 21:29:45 -070095 /* At this point, if argc == 1, then we are doing everything.
96 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
97 */
Linus Torvaldse83c5162005-04-07 15:13:13 -070098 if (entries < 0) {
99 perror("read_cache");
100 exit(1);
101 }
Junio C Hamanobe3cfa82005-04-26 09:25:05 -0700102
Junio C Hamano19feebc2005-05-27 15:54:37 -0700103 diff_setup(diff_setup_opt);
Junio C Hamano5c975582005-05-19 03:32:35 -0700104
Linus Torvaldse83c5162005-04-07 15:13:13 -0700105 for (i = 0; i < entries; i++) {
106 struct stat st;
Junio C Hamano67574c42005-06-01 11:38:07 -0700107 unsigned int oldmode;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700108 struct cache_entry *ce = active_cache[i];
Junio C Hamanod94c6122005-04-16 21:29:45 -0700109 int changed;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700110
Junio C Hamano9fec8b22005-04-16 21:29:45 -0700111 if (ce_stage(ce)) {
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700112 show_unmerge(ce->name);
Junio C Hamano9fec8b22005-04-16 21:29:45 -0700113 while (i < entries &&
114 !strcmp(ce->name, active_cache[i]->name))
115 i++;
116 i--; /* compensate for loop control increments */
117 continue;
118 }
Junio C Hamano57fe64a2005-05-19 19:00:36 -0700119
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200120 if (lstat(ce->name, &st) < 0) {
Junio C Hamano41174692005-05-20 09:48:38 -0700121 if (errno != ENOENT && errno != ENOTDIR) {
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700122 perror(ce->name);
Junio C Hamanoca2a0792005-04-15 15:08:09 -0700123 continue;
Junio C Hamano57fe64a2005-05-19 19:00:36 -0700124 }
Junio C Hamanod15aa432005-04-27 15:22:02 -0700125 if (silent)
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700126 continue;
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700127 show_file('-', ce);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700128 continue;
129 }
Brad Roberts5d728c82005-05-14 19:04:25 -0700130 changed = ce_match_stat(ce, &st);
Junio C Hamano4727f642005-06-19 13:14:05 -0700131 if (!changed && !find_copies_harder)
Linus Torvaldse83c5162005-04-07 15:13:13 -0700132 continue;
Linus Torvalds0a7668e2005-04-26 17:17:36 -0700133 oldmode = ntohl(ce->ce_mode);
Junio C Hamano67574c42005-06-01 11:38:07 -0700134 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
Junio C Hamano4727f642005-06-19 13:14:05 -0700135 ce->sha1, (changed ? null_sha1 : ce->sha1),
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -0700136 ce->name);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700137 }
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700138 diffcore_std((1 < argc) ? argv + 1 : NULL,
Junio C Hamanobefe8632005-05-29 16:56:13 -0700139 detect_rename, diff_score_opt,
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700140 pickaxe, pickaxe_opts,
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700141 diff_break_opt,
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -0700142 orderfile, diff_filter);
143 diff_flush(diff_output_format);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700144 return 0;
145}