]> git.proxmox.com Git - libgit2.git/blob - examples/diff.c
Simplify diff example using revparse
[libgit2.git] / examples / diff.c
1 #include <stdio.h>
2 #include <git2.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 static void check(int error, const char *message)
7 {
8 if (error) {
9 fprintf(stderr, "%s (%d)\n", message, error);
10 exit(1);
11 }
12 }
13
14 static int resolve_to_tree(
15 git_repository *repo, const char *identifier, git_tree **tree)
16 {
17 int err = 0;
18 git_object *obj = NULL;
19
20 if (git_revparse_single(&obj, repo, identifier) < 0)
21 return GIT_ENOTFOUND;
22
23 switch (git_object_type(obj)) {
24 case GIT_OBJ_TREE:
25 *tree = (git_tree *)obj;
26 break;
27 case GIT_OBJ_COMMIT:
28 err = git_commit_tree(tree, (git_commit *)obj);
29 git_object_free(obj);
30 break;
31 default:
32 err = GIT_ENOTFOUND;
33 }
34
35 return err;
36 }
37
38 char *colors[] = {
39 "\033[m", /* reset */
40 "\033[1m", /* bold */
41 "\033[31m", /* red */
42 "\033[32m", /* green */
43 "\033[36m" /* cyan */
44 };
45
46 static int printer(
47 const git_diff_delta *delta,
48 const git_diff_range *range,
49 char usage,
50 const char *line,
51 size_t line_len,
52 void *data)
53 {
54 int *last_color = data, color = 0;
55
56 (void)delta; (void)range; (void)line_len;
57
58 if (*last_color >= 0) {
59 switch (usage) {
60 case GIT_DIFF_LINE_ADDITION: color = 3; break;
61 case GIT_DIFF_LINE_DELETION: color = 2; break;
62 case GIT_DIFF_LINE_ADD_EOFNL: color = 3; break;
63 case GIT_DIFF_LINE_DEL_EOFNL: color = 2; break;
64 case GIT_DIFF_LINE_FILE_HDR: color = 1; break;
65 case GIT_DIFF_LINE_HUNK_HDR: color = 4; break;
66 default: color = 0;
67 }
68 if (color != *last_color) {
69 if (*last_color == 1 || color == 1)
70 fputs(colors[0], stdout);
71 fputs(colors[color], stdout);
72 *last_color = color;
73 }
74 }
75
76 fputs(line, stdout);
77 return 0;
78 }
79
80 static int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
81 {
82 size_t len = strlen(pattern);
83 uint16_t strval;
84 char *endptr = NULL;
85 if (strncmp(arg, pattern, len))
86 return 0;
87 strval = strtoul(arg + len, &endptr, 0);
88 if (endptr == arg)
89 return 0;
90 *val = strval;
91 return 1;
92 }
93
94 static int check_str_param(const char *arg, const char *pattern, const char **val)
95 {
96 size_t len = strlen(pattern);
97 if (strncmp(arg, pattern, len))
98 return 0;
99 *val = (const char *)(arg + len);
100 return 1;
101 }
102
103 static void usage(const char *message, const char *arg)
104 {
105 if (message && arg)
106 fprintf(stderr, "%s: %s\n", message, arg);
107 else if (message)
108 fprintf(stderr, "%s\n", message);
109 fprintf(stderr, "usage: diff [<tree-oid> [<tree-oid>]]\n");
110 exit(1);
111 }
112
113 int main(int argc, char *argv[])
114 {
115 git_repository *repo = NULL;
116 git_tree *t1 = NULL, *t2 = NULL;
117 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
118 git_diff_list *diff;
119 int i, color = -1, compact = 0, cached = 0;
120 char *a, *dir = ".", *treeish1 = NULL, *treeish2 = NULL;
121
122 /* parse arguments as copied from git-diff */
123
124 for (i = 1; i < argc; ++i) {
125 a = argv[i];
126
127 if (a[0] != '-') {
128 if (treeish1 == NULL)
129 treeish1 = a;
130 else if (treeish2 == NULL)
131 treeish2 = a;
132 else
133 usage("Only one or two tree identifiers can be provided", NULL);
134 }
135 else if (!strcmp(a, "-p") || !strcmp(a, "-u") ||
136 !strcmp(a, "--patch"))
137 compact = 0;
138 else if (!strcmp(a, "--cached"))
139 cached = 1;
140 else if (!strcmp(a, "--name-status"))
141 compact = 1;
142 else if (!strcmp(a, "--color"))
143 color = 0;
144 else if (!strcmp(a, "--no-color"))
145 color = -1;
146 else if (!strcmp(a, "-R"))
147 opts.flags |= GIT_DIFF_REVERSE;
148 else if (!strcmp(a, "-a") || !strcmp(a, "--text"))
149 opts.flags |= GIT_DIFF_FORCE_TEXT;
150 else if (!strcmp(a, "--ignore-space-at-eol"))
151 opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL;
152 else if (!strcmp(a, "-b") || !strcmp(a, "--ignore-space-change"))
153 opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
154 else if (!strcmp(a, "-w") || !strcmp(a, "--ignore-all-space"))
155 opts.flags |= GIT_DIFF_IGNORE_WHITESPACE;
156 else if (!strcmp(a, "--ignored"))
157 opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
158 else if (!strcmp(a, "--untracked"))
159 opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
160 else if (!check_uint16_param(a, "-U", &opts.context_lines) &&
161 !check_uint16_param(a, "--unified=", &opts.context_lines) &&
162 !check_uint16_param(a, "--inter-hunk-context=",
163 &opts.interhunk_lines) &&
164 !check_str_param(a, "--src-prefix=", &opts.old_prefix) &&
165 !check_str_param(a, "--dst-prefix=", &opts.new_prefix))
166 usage("Unknown arg", a);
167 }
168
169 /* open repo */
170
171 check(git_repository_open_ext(&repo, dir, 0, NULL),
172 "Could not open repository");
173
174 if (treeish1)
175 check(resolve_to_tree(repo, treeish1, &t1), "Looking up first tree");
176 if (treeish2)
177 check(resolve_to_tree(repo, treeish2, &t2), "Looking up second tree");
178
179 /* <sha1> <sha2> */
180 /* <sha1> --cached */
181 /* <sha1> */
182 /* --cached */
183 /* nothing */
184
185 if (t1 && t2)
186 check(git_diff_tree_to_tree(&diff, repo, t1, t2, &opts), "Diff");
187 else if (t1 && cached)
188 check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
189 else if (t1) {
190 git_diff_list *diff2;
191 check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
192 check(git_diff_index_to_workdir(&diff2, repo, NULL, &opts), "Diff");
193 check(git_diff_merge(diff, diff2), "Merge diffs");
194 git_diff_list_free(diff2);
195 }
196 else if (cached) {
197 check(resolve_to_tree(repo, "HEAD", &t1), "looking up HEAD");
198 check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
199 }
200 else
201 check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff");
202
203 if (color >= 0)
204 fputs(colors[0], stdout);
205
206 if (compact)
207 check(git_diff_print_compact(diff, printer, &color), "Displaying diff");
208 else
209 check(git_diff_print_patch(diff, printer, &color), "Displaying diff");
210
211 if (color >= 0)
212 fputs(colors[0], stdout);
213
214 git_diff_list_free(diff);
215 git_tree_free(t1);
216 git_tree_free(t2);
217 git_repository_free(repo);
218
219 return 0;
220 }
221