2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 static const char *diff_files_usage
=
10 "diff-files [-p] [-q] [-r] [-z] [-M] [paths...]";
12 static int generate_patch
= 0;
13 static int line_termination
= '\n';
14 static int detect_rename
= 0;
15 static int silent
= 0;
17 static int matches_pathspec(struct cache_entry
*ce
, char **spec
, int cnt
)
20 int namelen
= ce_namelen(ce
);
21 for (i
= 0; i
< cnt
; i
++) {
22 int speclen
= strlen(spec
[i
]);
23 if (! strncmp(spec
[i
], ce
->name
, speclen
) &&
25 (ce
->name
[speclen
] == 0 ||
26 ce
->name
[speclen
] == '/'))
32 static void show_unmerge(const char *path
)
37 printf("U %s%c", path
, line_termination
);
40 static void show_file(int pfx
, struct cache_entry
*ce
)
43 diff_addremove(pfx
, ntohl(ce
->ce_mode
), ce
->sha1
,
46 printf("%c%06o\t%s\t%s\t%s%c",
47 pfx
, ntohl(ce
->ce_mode
), "blob",
48 sha1_to_hex(ce
->sha1
), ce
->name
, line_termination
);
51 static void show_modified(int oldmode
, int mode
,
52 const unsigned char *old_sha1
, const unsigned char *sha1
,
55 char old_sha1_hex
[41];
56 strcpy(old_sha1_hex
, sha1_to_hex(old_sha1
));
59 diff_change(oldmode
, mode
, old_sha1
, sha1
, path
, NULL
);
61 printf("*%06o->%06o\tblob\t%s->%s\t%s%c",
62 oldmode
, mode
, old_sha1_hex
, sha1_to_hex(sha1
), path
,
66 int main(int argc
, char **argv
)
68 static const unsigned char null_sha1
[20] = { 0, };
69 int entries
= read_cache();
72 while (1 < argc
&& argv
[1][0] == '-') {
73 if (!strcmp(argv
[1], "-p"))
75 else if (!strcmp(argv
[1], "-q"))
77 else if (!strcmp(argv
[1], "-r"))
79 else if (!strcmp(argv
[1], "-s"))
81 else if (!strcmp(argv
[1], "-z"))
83 else if (!strcmp(argv
[1], "-M")) {
84 detect_rename
= generate_patch
= 1;
87 usage(diff_files_usage
);
91 /* At this point, if argc == 1, then we are doing everything.
92 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
100 diff_setup(detect_rename
, 0, 0, 0, 0);
102 for (i
= 0; i
< entries
; i
++) {
104 unsigned int oldmode
, mode
;
105 struct cache_entry
*ce
= active_cache
[i
];
109 ! matches_pathspec(ce
, argv
+1, argc
-1))
113 show_unmerge(ce
->name
);
114 while (i
< entries
&&
115 !strcmp(ce
->name
, active_cache
[i
]->name
))
117 i
--; /* compensate for loop control increments */
121 if (lstat(ce
->name
, &st
) < 0) {
122 if (errno
!= ENOENT
) {
131 changed
= ce_match_stat(ce
, &st
);
135 oldmode
= ntohl(ce
->ce_mode
);
136 mode
= (S_ISLNK(st
.st_mode
) ? S_IFLNK
:
137 S_IFREG
| ce_permissions(st
.st_mode
));
139 show_modified(oldmode
, mode
, ce
->sha1
, null_sha1
,