Use svn pools to solve the memory leak problem.
[git/git-svn.git] / diff-tree.c
blobed323d877cad1e78e08f84c1e9ee1ebb09f05d91
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
5 static int show_root_diff = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int read_stdin = 0;
10 static const char *header = NULL;
11 static const char *header_prefix = "";
12 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
14 static struct diff_options diff_options;
16 static void call_diff_setup_done(void)
18 diff_setup_done(&diff_options);
21 static int call_diff_flush(void)
23 diffcore_std(&diff_options);
24 if (diff_queue_is_empty()) {
25 int saved_fmt = diff_options.output_format;
26 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
27 diff_flush(&diff_options);
28 diff_options.output_format = saved_fmt;
29 return 0;
31 if (header) {
32 printf("%s%c", header, diff_options.line_termination);
33 header = NULL;
35 diff_flush(&diff_options);
36 return 1;
39 static int diff_tree_sha1_top(const unsigned char *old,
40 const unsigned char *new, const char *base)
42 int ret;
44 call_diff_setup_done();
45 ret = diff_tree_sha1(old, new, base, &diff_options);
46 call_diff_flush();
47 return ret;
50 static int diff_root_tree(const unsigned char *new, const char *base)
52 int retval;
53 void *tree;
54 struct tree_desc empty, real;
56 call_diff_setup_done();
57 tree = read_object_with_reference(new, "tree", &real.size, NULL);
58 if (!tree)
59 die("unable to read root tree (%s)", sha1_to_hex(new));
60 real.buf = tree;
62 empty.buf = "";
63 empty.size = 0;
64 retval = diff_tree(&empty, &real, base, &diff_options);
65 free(tree);
66 call_diff_flush();
67 return retval;
70 static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
72 static char this_header[16384];
73 int offset;
75 if (!verbose_header)
76 return commit;
78 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
79 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
80 return this_header;
83 static int diff_tree_commit(const unsigned char *commit, const char *name)
85 unsigned long size, offset;
86 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
88 if (!buf)
89 return -1;
91 if (!name) {
92 static char commit_name[60];
93 strcpy(commit_name, sha1_to_hex(commit));
94 name = commit_name;
97 /* Root commit? */
98 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
99 header = generate_header(name, "root", buf, size);
100 diff_root_tree(commit, "");
103 /* More than one parent? */
104 if (ignore_merges) {
105 if (!memcmp(buf + 46 + 48, "parent ", 7))
106 return 0;
109 offset = 46;
110 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
111 unsigned char parent[20];
112 if (get_sha1_hex(buf + offset + 7, parent))
113 return -1;
114 header = generate_header(name, sha1_to_hex(parent), buf, size);
115 diff_tree_sha1_top(parent, commit, "");
116 if (!header && verbose_header) {
117 header_prefix = "\ndiff-tree ";
119 * Don't print multiple merge entries if we
120 * don't print the diffs.
123 offset += 48;
125 free(buf);
126 return 0;
129 static int diff_tree_stdin(char *line)
131 int len = strlen(line);
132 unsigned char commit[20], parent[20];
133 static char this_header[1000];
135 if (!len || line[len-1] != '\n')
136 return -1;
137 line[len-1] = 0;
138 if (get_sha1_hex(line, commit))
139 return -1;
140 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
141 line[40] = 0;
142 line[81] = 0;
143 sprintf(this_header, "%s (from %s)\n", line, line+41);
144 header = this_header;
145 return diff_tree_sha1_top(parent, commit, "");
147 line[40] = 0;
148 return diff_tree_commit(commit, line);
151 static const char diff_tree_usage[] =
152 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
153 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
154 " -r diff recursively\n"
155 " --root include the initial commit as diff against /dev/null\n"
156 COMMON_DIFF_OPTIONS_HELP;
158 int main(int argc, const char **argv)
160 int nr_sha1;
161 char line[1000];
162 unsigned char sha1[2][20];
163 const char *prefix = setup_git_directory();
165 git_config(git_default_config);
166 nr_sha1 = 0;
167 diff_setup(&diff_options);
169 for (;;) {
170 int diff_opt_cnt;
171 const char *arg;
173 argv++;
174 argc--;
175 arg = *argv;
176 if (!arg)
177 break;
179 if (*arg != '-') {
180 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
181 nr_sha1++;
182 continue;
184 break;
187 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
188 if (diff_opt_cnt < 0)
189 usage(diff_tree_usage);
190 else if (diff_opt_cnt) {
191 argv += diff_opt_cnt - 1;
192 argc -= diff_opt_cnt - 1;
193 continue;
197 if (!strcmp(arg, "--")) {
198 argv++;
199 argc--;
200 break;
202 if (!strcmp(arg, "-r")) {
203 diff_options.recursive = 1;
204 continue;
206 if (!strcmp(arg, "-t")) {
207 diff_options.recursive = 1;
208 diff_options.tree_in_recursive = 1;
209 continue;
211 if (!strcmp(arg, "-m")) {
212 ignore_merges = 0;
213 continue;
215 if (!strcmp(arg, "-v")) {
216 verbose_header = 1;
217 header_prefix = "diff-tree ";
218 continue;
220 if (!strncmp(arg, "--pretty", 8)) {
221 verbose_header = 1;
222 header_prefix = "diff-tree ";
223 commit_format = get_commit_format(arg+8);
224 continue;
226 if (!strcmp(arg, "--stdin")) {
227 read_stdin = 1;
228 continue;
230 if (!strcmp(arg, "--root")) {
231 show_root_diff = 1;
232 continue;
234 usage(diff_tree_usage);
236 if (diff_options.output_format == DIFF_FORMAT_PATCH)
237 diff_options.recursive = 1;
239 diff_tree_setup_paths(get_pathspec(prefix, argv));
241 switch (nr_sha1) {
242 case 0:
243 if (!read_stdin)
244 usage(diff_tree_usage);
245 break;
246 case 1:
247 diff_tree_commit(sha1[0], NULL);
248 break;
249 case 2:
250 diff_tree_sha1_top(sha1[0], sha1[1], "");
251 break;
254 if (!read_stdin)
255 return 0;
257 if (diff_options.detect_rename)
258 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
259 DIFF_SETUP_USE_CACHE);
260 while (fgets(line, sizeof(line), stdin))
261 diff_tree_stdin(line);
263 return 0;