Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 f42b1b34 2018-03-12 stsp
21 5c860e29 2018-03-12 stsp #include <err.h>
22 5c860e29 2018-03-12 stsp #include <errno.h>
23 5c860e29 2018-03-12 stsp #include <locale.h>
24 5c860e29 2018-03-12 stsp #include <stdio.h>
25 5c860e29 2018-03-12 stsp #include <stdlib.h>
26 5c860e29 2018-03-12 stsp #include <string.h>
27 5c860e29 2018-03-12 stsp #include <unistd.h>
28 c09a553d 2018-03-12 stsp #include <libgen.h>
29 5c860e29 2018-03-12 stsp
30 f42b1b34 2018-03-12 stsp #include "got_error.h"
31 f42b1b34 2018-03-12 stsp #include "got_object.h"
32 f42b1b34 2018-03-12 stsp #include "got_refs.h"
33 f42b1b34 2018-03-12 stsp #include "got_repository.h"
34 c09a553d 2018-03-12 stsp #include "got_worktree.h"
35 79109fed 2018-03-27 stsp #include "got_diff.h"
36 5c860e29 2018-03-12 stsp
37 5c860e29 2018-03-12 stsp #ifndef nitems
38 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
39 5c860e29 2018-03-12 stsp #endif
40 5c860e29 2018-03-12 stsp
41 5c860e29 2018-03-12 stsp struct cmd {
42 5c860e29 2018-03-12 stsp const char *cmd_name;
43 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
44 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
45 46a0db7d 2018-03-12 stsp const char *cmd_descr;
46 5c860e29 2018-03-12 stsp };
47 5c860e29 2018-03-12 stsp
48 5c860e29 2018-03-12 stsp __dead void usage(void);
49 c09a553d 2018-03-12 stsp __dead void usage_checkout(void);
50 6f3d1eb0 2018-03-12 stsp __dead void usage_log(void);
51 b00d56cd 2018-04-01 stsp __dead void usage_diff(void);
52 5c860e29 2018-03-12 stsp
53 c09a553d 2018-03-12 stsp const struct got_error* cmd_checkout(int, char *[]);
54 d7d4f210 2018-03-12 stsp const struct got_error* cmd_log(int, char *[]);
55 b00d56cd 2018-04-01 stsp const struct got_error* cmd_diff(int, char *[]);
56 d7d4f210 2018-03-12 stsp const struct got_error* cmd_status(int, char *[]);
57 5c860e29 2018-03-12 stsp
58 5c860e29 2018-03-12 stsp struct cmd got_commands[] = {
59 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
60 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
61 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
62 1b6b95a8 2018-03-12 stsp "show repository history" },
63 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
64 b00d56cd 2018-04-01 stsp "compare files and directories" },
65 f42b1b34 2018-03-12 stsp #ifdef notyet
66 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
67 1b6b95a8 2018-03-12 stsp "show modification status of files" },
68 f42b1b34 2018-03-12 stsp #endif
69 5c860e29 2018-03-12 stsp };
70 5c860e29 2018-03-12 stsp
71 5c860e29 2018-03-12 stsp int
72 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
73 5c860e29 2018-03-12 stsp {
74 5c860e29 2018-03-12 stsp struct cmd *cmd;
75 5c860e29 2018-03-12 stsp unsigned int i;
76 5c860e29 2018-03-12 stsp int ch;
77 1b6b95a8 2018-03-12 stsp int hflag = 0;
78 5c860e29 2018-03-12 stsp
79 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
80 5c860e29 2018-03-12 stsp
81 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
82 5c860e29 2018-03-12 stsp switch (ch) {
83 1b6b95a8 2018-03-12 stsp case 'h':
84 1b6b95a8 2018-03-12 stsp hflag = 1;
85 1b6b95a8 2018-03-12 stsp break;
86 5c860e29 2018-03-12 stsp default:
87 5c860e29 2018-03-12 stsp usage();
88 5c860e29 2018-03-12 stsp /* NOTREACHED */
89 5c860e29 2018-03-12 stsp }
90 5c860e29 2018-03-12 stsp }
91 5c860e29 2018-03-12 stsp
92 5c860e29 2018-03-12 stsp argc -= optind;
93 5c860e29 2018-03-12 stsp argv += optind;
94 1e70621d 2018-03-27 stsp optind = 0;
95 5c860e29 2018-03-12 stsp
96 5c860e29 2018-03-12 stsp if (argc <= 0)
97 5c860e29 2018-03-12 stsp usage();
98 5c860e29 2018-03-12 stsp
99 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
100 d7d4f210 2018-03-12 stsp const struct got_error *error;
101 d7d4f210 2018-03-12 stsp
102 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
103 5c860e29 2018-03-12 stsp
104 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
105 5c860e29 2018-03-12 stsp continue;
106 5c860e29 2018-03-12 stsp
107 1b6b95a8 2018-03-12 stsp if (hflag)
108 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
109 1b6b95a8 2018-03-12 stsp
110 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
111 d7d4f210 2018-03-12 stsp if (error) {
112 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
113 d7d4f210 2018-03-12 stsp return 1;
114 d7d4f210 2018-03-12 stsp }
115 d7d4f210 2018-03-12 stsp
116 d7d4f210 2018-03-12 stsp return 0;
117 5c860e29 2018-03-12 stsp }
118 5c860e29 2018-03-12 stsp
119 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
120 5c860e29 2018-03-12 stsp return 1;
121 5c860e29 2018-03-12 stsp }
122 5c860e29 2018-03-12 stsp
123 5c860e29 2018-03-12 stsp __dead void
124 5c860e29 2018-03-12 stsp usage(void)
125 5c860e29 2018-03-12 stsp {
126 46a0db7d 2018-03-12 stsp int i;
127 46a0db7d 2018-03-12 stsp
128 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
129 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
130 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
131 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
132 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
133 46a0db7d 2018-03-12 stsp }
134 5c860e29 2018-03-12 stsp exit(1);
135 5c860e29 2018-03-12 stsp }
136 5c860e29 2018-03-12 stsp
137 c09a553d 2018-03-12 stsp __dead void
138 c09a553d 2018-03-12 stsp usage_checkout(void)
139 c09a553d 2018-03-12 stsp {
140 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
141 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
142 c09a553d 2018-03-12 stsp exit(1);
143 92a684f4 2018-03-12 stsp }
144 92a684f4 2018-03-12 stsp
145 92a684f4 2018-03-12 stsp static void
146 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
147 92a684f4 2018-03-12 stsp {
148 92a684f4 2018-03-12 stsp char *worktree_path = arg;
149 92a684f4 2018-03-12 stsp
150 92a684f4 2018-03-12 stsp while (path[0] == '/')
151 92a684f4 2018-03-12 stsp path++;
152 92a684f4 2018-03-12 stsp
153 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
154 c09a553d 2018-03-12 stsp }
155 c09a553d 2018-03-12 stsp
156 c09a553d 2018-03-12 stsp const struct got_error *
157 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
158 c09a553d 2018-03-12 stsp {
159 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
160 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
161 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
162 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
163 c09a553d 2018-03-12 stsp char *repo_path = NULL;
164 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
165 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
166 0bb8a95e 2018-03-12 stsp int ch;
167 c09a553d 2018-03-12 stsp
168 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
169 0bb8a95e 2018-03-12 stsp switch (ch) {
170 0bb8a95e 2018-03-12 stsp case 'p':
171 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
172 0bb8a95e 2018-03-12 stsp break;
173 0bb8a95e 2018-03-12 stsp default:
174 0bb8a95e 2018-03-12 stsp usage();
175 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
176 0bb8a95e 2018-03-12 stsp }
177 0bb8a95e 2018-03-12 stsp }
178 0bb8a95e 2018-03-12 stsp
179 0bb8a95e 2018-03-12 stsp argc -= optind;
180 0bb8a95e 2018-03-12 stsp argv += optind;
181 0bb8a95e 2018-03-12 stsp
182 6715a751 2018-03-16 stsp #ifndef PROFILE
183 c09a553d 2018-03-12 stsp if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
184 c09a553d 2018-03-12 stsp err(1, "pledge");
185 6715a751 2018-03-16 stsp #endif
186 0bb8a95e 2018-03-12 stsp if (argc == 1) {
187 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
188 0bb8a95e 2018-03-12 stsp repo_path = argv[0];
189 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
190 c09a553d 2018-03-12 stsp if (cwd == NULL)
191 c09a553d 2018-03-12 stsp err(1, "getcwd");
192 c09a553d 2018-03-12 stsp base = basename(repo_path);
193 c09a553d 2018-03-12 stsp if (base == NULL)
194 c09a553d 2018-03-12 stsp err(1, "basename");
195 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
196 c09a553d 2018-03-12 stsp if (dotgit)
197 c09a553d 2018-03-12 stsp *dotgit = '\0';
198 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
199 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
200 c09a553d 2018-03-12 stsp free(cwd);
201 0a585a0d 2018-03-17 stsp return error;
202 c09a553d 2018-03-12 stsp }
203 c09a553d 2018-03-12 stsp free(cwd);
204 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
205 0bb8a95e 2018-03-12 stsp repo_path = argv[0];
206 0bb8a95e 2018-03-12 stsp worktree_path = strdup(argv[1]);
207 c09a553d 2018-03-12 stsp if (worktree_path == NULL)
208 0a585a0d 2018-03-17 stsp return got_error_from_errno();
209 c09a553d 2018-03-12 stsp } else
210 c09a553d 2018-03-12 stsp usage_checkout();
211 c09a553d 2018-03-12 stsp
212 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
213 c09a553d 2018-03-12 stsp if (error != NULL)
214 c09a553d 2018-03-12 stsp goto done;
215 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
216 c09a553d 2018-03-12 stsp if (error != NULL)
217 c09a553d 2018-03-12 stsp goto done;
218 c09a553d 2018-03-12 stsp
219 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
220 c09a553d 2018-03-12 stsp if (error != NULL)
221 c09a553d 2018-03-12 stsp goto done;
222 c09a553d 2018-03-12 stsp
223 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
224 c09a553d 2018-03-12 stsp if (error != NULL)
225 c09a553d 2018-03-12 stsp goto done;
226 c09a553d 2018-03-12 stsp
227 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
228 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
229 c09a553d 2018-03-12 stsp if (error != NULL)
230 c09a553d 2018-03-12 stsp goto done;
231 c09a553d 2018-03-12 stsp
232 c09a553d 2018-03-12 stsp printf("checked out %s\n", worktree_path);
233 c09a553d 2018-03-12 stsp
234 c09a553d 2018-03-12 stsp done:
235 c09a553d 2018-03-12 stsp free(worktree_path);
236 c09a553d 2018-03-12 stsp return error;
237 c09a553d 2018-03-12 stsp }
238 c09a553d 2018-03-12 stsp
239 f42b1b34 2018-03-12 stsp static const struct got_error *
240 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
241 f42b1b34 2018-03-12 stsp struct got_repository *repo)
242 5c860e29 2018-03-12 stsp {
243 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
244 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
245 79109fed 2018-03-27 stsp struct got_object *obj;
246 79109fed 2018-03-27 stsp struct got_parent_id *pid;
247 79109fed 2018-03-27 stsp
248 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
249 79109fed 2018-03-27 stsp if (err)
250 79109fed 2018-03-27 stsp return err;
251 79109fed 2018-03-27 stsp
252 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
253 79109fed 2018-03-27 stsp got_object_close(obj);
254 79109fed 2018-03-27 stsp if (err)
255 79109fed 2018-03-27 stsp return err;
256 79109fed 2018-03-27 stsp
257 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
258 79109fed 2018-03-27 stsp if (pid != NULL) {
259 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
260 79109fed 2018-03-27 stsp
261 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
262 79109fed 2018-03-27 stsp if (err)
263 79109fed 2018-03-27 stsp return err;
264 79109fed 2018-03-27 stsp
265 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
266 79109fed 2018-03-27 stsp got_object_close(obj);
267 79109fed 2018-03-27 stsp if (err)
268 79109fed 2018-03-27 stsp return err;
269 79109fed 2018-03-27 stsp
270 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
271 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
272 79109fed 2018-03-27 stsp if (err)
273 79109fed 2018-03-27 stsp return err;
274 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
275 79109fed 2018-03-27 stsp got_object_close(obj);
276 79109fed 2018-03-27 stsp if (err)
277 79109fed 2018-03-27 stsp return err;
278 79109fed 2018-03-27 stsp }
279 79109fed 2018-03-27 stsp
280 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
281 79109fed 2018-03-27 stsp if (tree1)
282 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
283 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
284 79109fed 2018-03-27 stsp return err;
285 79109fed 2018-03-27 stsp }
286 79109fed 2018-03-27 stsp
287 79109fed 2018-03-27 stsp static const struct got_error *
288 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
289 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
290 79109fed 2018-03-27 stsp {
291 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
292 8bf5b3c9 2018-03-17 stsp char *buf;
293 5c860e29 2018-03-12 stsp
294 8bf5b3c9 2018-03-17 stsp err = got_object_id_str(&buf, id);
295 8bf5b3c9 2018-03-17 stsp if (err)
296 8bf5b3c9 2018-03-17 stsp return err;
297 5c860e29 2018-03-12 stsp
298 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
299 8bf5b3c9 2018-03-17 stsp printf("commit: %s\n", buf);
300 8bf5b3c9 2018-03-17 stsp printf("Author: %s\n", commit->author);
301 8bf5b3c9 2018-03-17 stsp printf("\n%s\n", commit->logmsg);
302 8bf5b3c9 2018-03-17 stsp
303 971751ac 2018-03-27 stsp if (show_patch) {
304 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
305 971751ac 2018-03-27 stsp if (err == 0)
306 971751ac 2018-03-27 stsp printf("\n");
307 971751ac 2018-03-27 stsp }
308 79109fed 2018-03-27 stsp
309 8bf5b3c9 2018-03-17 stsp free(buf);
310 79109fed 2018-03-27 stsp return err;
311 f42b1b34 2018-03-12 stsp }
312 8bf5b3c9 2018-03-17 stsp
313 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry {
314 8bf5b3c9 2018-03-17 stsp TAILQ_ENTRY(commit_queue_entry) entry;
315 8bf5b3c9 2018-03-17 stsp struct got_object_id *id;
316 8bf5b3c9 2018-03-17 stsp struct got_commit_object *commit;
317 8bf5b3c9 2018-03-17 stsp };
318 5c860e29 2018-03-12 stsp
319 f42b1b34 2018-03-12 stsp static const struct got_error *
320 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
321 64a96a6d 2018-04-01 stsp struct got_repository *repo, int show_patch, int limit)
322 f42b1b34 2018-03-12 stsp {
323 f42b1b34 2018-03-12 stsp const struct got_error *err;
324 8bf5b3c9 2018-03-17 stsp struct got_commit_object *root_commit;
325 8bf5b3c9 2018-03-17 stsp TAILQ_HEAD(, commit_queue_entry) commits;
326 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *entry;
327 5c860e29 2018-03-12 stsp
328 8bf5b3c9 2018-03-17 stsp TAILQ_INIT(&commits);
329 5c860e29 2018-03-12 stsp
330 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&root_commit, repo, root_obj);
331 f42b1b34 2018-03-12 stsp if (err)
332 f42b1b34 2018-03-12 stsp return err;
333 5c860e29 2018-03-12 stsp
334 8bf5b3c9 2018-03-17 stsp entry = calloc(1, sizeof(*entry));
335 8bf5b3c9 2018-03-17 stsp if (entry == NULL)
336 0a585a0d 2018-03-17 stsp return got_error_from_errno();
337 8bf5b3c9 2018-03-17 stsp entry->id = got_object_id_dup(root_id);
338 8bf5b3c9 2018-03-17 stsp if (entry->id == NULL) {
339 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
340 8bf5b3c9 2018-03-17 stsp free(entry);
341 0a585a0d 2018-03-17 stsp return err;
342 8bf5b3c9 2018-03-17 stsp }
343 8bf5b3c9 2018-03-17 stsp entry->commit = root_commit;
344 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_HEAD(&commits, entry, entry);
345 8bf5b3c9 2018-03-17 stsp
346 8bf5b3c9 2018-03-17 stsp while (!TAILQ_EMPTY(&commits)) {
347 8bf5b3c9 2018-03-17 stsp struct got_parent_id *pid;
348 5c860e29 2018-03-12 stsp
349 8bf5b3c9 2018-03-17 stsp entry = TAILQ_FIRST(&commits);
350 79109fed 2018-03-27 stsp err = print_commit(entry->commit, entry->id, repo, show_patch);
351 25470781 2018-04-01 stsp if (err) {
352 25470781 2018-04-01 stsp while (!TAILQ_EMPTY(&commits)) {
353 25470781 2018-04-01 stsp entry = TAILQ_FIRST(&commits);
354 25470781 2018-04-01 stsp TAILQ_REMOVE(&commits, entry, entry);
355 25470781 2018-04-01 stsp got_object_commit_close(entry->commit);
356 25470781 2018-04-01 stsp free(entry->id);
357 25470781 2018-04-01 stsp free(entry);
358 25470781 2018-04-01 stsp }
359 8bf5b3c9 2018-03-17 stsp break;
360 25470781 2018-04-01 stsp }
361 5c860e29 2018-03-12 stsp
362 64a96a6d 2018-04-01 stsp if (limit && --limit == 0)
363 64a96a6d 2018-04-01 stsp break;
364 64a96a6d 2018-04-01 stsp
365 8bf5b3c9 2018-03-17 stsp SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
366 8bf5b3c9 2018-03-17 stsp struct got_object *obj;
367 8bf5b3c9 2018-03-17 stsp struct got_commit_object *pcommit;
368 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *pentry;
369 8bf5b3c9 2018-03-17 stsp
370 8bf5b3c9 2018-03-17 stsp err = got_object_open(&obj, repo, pid->id);
371 8bf5b3c9 2018-03-17 stsp if (err)
372 8bf5b3c9 2018-03-17 stsp break;
373 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
374 8bf5b3c9 2018-03-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
375 8bf5b3c9 2018-03-17 stsp break;
376 8bf5b3c9 2018-03-17 stsp }
377 8bf5b3c9 2018-03-17 stsp
378 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&pcommit, repo, obj);
379 8bf5b3c9 2018-03-17 stsp got_object_close(obj);
380 8bf5b3c9 2018-03-17 stsp if (err)
381 8bf5b3c9 2018-03-17 stsp break;
382 8bf5b3c9 2018-03-17 stsp
383 8bf5b3c9 2018-03-17 stsp pentry = calloc(1, sizeof(*pentry));
384 8bf5b3c9 2018-03-17 stsp if (pentry == NULL) {
385 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
386 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
387 8bf5b3c9 2018-03-17 stsp break;
388 8bf5b3c9 2018-03-17 stsp }
389 8bf5b3c9 2018-03-17 stsp pentry->id = got_object_id_dup(pid->id);
390 8bf5b3c9 2018-03-17 stsp if (pentry->id == NULL) {
391 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
392 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
393 8bf5b3c9 2018-03-17 stsp break;
394 8bf5b3c9 2018-03-17 stsp }
395 8bf5b3c9 2018-03-17 stsp pentry->commit = pcommit;
396 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_TAIL(&commits, pentry, entry);
397 8bf5b3c9 2018-03-17 stsp }
398 8bf5b3c9 2018-03-17 stsp
399 8bf5b3c9 2018-03-17 stsp TAILQ_REMOVE(&commits, entry, entry);
400 8bf5b3c9 2018-03-17 stsp got_object_commit_close(entry->commit);
401 8bf5b3c9 2018-03-17 stsp free(entry->id);
402 8bf5b3c9 2018-03-17 stsp free(entry);
403 8bf5b3c9 2018-03-17 stsp }
404 8bf5b3c9 2018-03-17 stsp
405 f42b1b34 2018-03-12 stsp return err;
406 f42b1b34 2018-03-12 stsp }
407 5c860e29 2018-03-12 stsp
408 6f3d1eb0 2018-03-12 stsp __dead void
409 6f3d1eb0 2018-03-12 stsp usage_log(void)
410 6f3d1eb0 2018-03-12 stsp {
411 64a96a6d 2018-04-01 stsp fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
412 64a96a6d 2018-04-01 stsp "[repository-path]\n", getprogname());
413 6f3d1eb0 2018-03-12 stsp exit(1);
414 6f3d1eb0 2018-03-12 stsp }
415 6f3d1eb0 2018-03-12 stsp
416 d7d4f210 2018-03-12 stsp const struct got_error *
417 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
418 f42b1b34 2018-03-12 stsp {
419 f42b1b34 2018-03-12 stsp const struct got_error *error;
420 f42b1b34 2018-03-12 stsp struct got_repository *repo;
421 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
422 f42b1b34 2018-03-12 stsp struct got_object *obj;
423 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
424 d142fc45 2018-04-01 stsp char *start_commit = NULL;
425 79109fed 2018-03-27 stsp int ch;
426 64a96a6d 2018-04-01 stsp int show_patch = 0, limit = 0;
427 64a96a6d 2018-04-01 stsp const char *errstr;
428 5c860e29 2018-03-12 stsp
429 6715a751 2018-03-16 stsp #ifndef PROFILE
430 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
431 f42b1b34 2018-03-12 stsp err(1, "pledge");
432 6715a751 2018-03-16 stsp #endif
433 79109fed 2018-03-27 stsp
434 64a96a6d 2018-04-01 stsp while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
435 79109fed 2018-03-27 stsp switch (ch) {
436 79109fed 2018-03-27 stsp case 'p':
437 79109fed 2018-03-27 stsp show_patch = 1;
438 d142fc45 2018-04-01 stsp break;
439 d142fc45 2018-04-01 stsp case 'c':
440 d142fc45 2018-04-01 stsp start_commit = optarg;
441 64a96a6d 2018-04-01 stsp break;
442 64a96a6d 2018-04-01 stsp case 'l':
443 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
444 64a96a6d 2018-04-01 stsp if (errstr != NULL)
445 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
446 79109fed 2018-03-27 stsp break;
447 79109fed 2018-03-27 stsp default:
448 79109fed 2018-03-27 stsp usage();
449 79109fed 2018-03-27 stsp /* NOTREACHED */
450 79109fed 2018-03-27 stsp }
451 79109fed 2018-03-27 stsp }
452 79109fed 2018-03-27 stsp
453 79109fed 2018-03-27 stsp argc -= optind;
454 79109fed 2018-03-27 stsp argv += optind;
455 79109fed 2018-03-27 stsp
456 79109fed 2018-03-27 stsp if (argc == 0) {
457 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
458 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
459 6f3d1eb0 2018-03-12 stsp err(1, "getcwd");
460 3235492e 2018-04-01 stsp } else if (argc == 1) {
461 1ca4538e 2018-03-27 stsp repo_path = argv[0];
462 3235492e 2018-04-01 stsp } else
463 6f3d1eb0 2018-03-12 stsp usage_log();
464 f42b1b34 2018-03-12 stsp
465 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
466 d7d4f210 2018-03-12 stsp if (error != NULL)
467 d7d4f210 2018-03-12 stsp return error;
468 f42b1b34 2018-03-12 stsp
469 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
470 3235492e 2018-04-01 stsp struct got_reference *head_ref;
471 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
472 3235492e 2018-04-01 stsp if (error != NULL)
473 3235492e 2018-04-01 stsp return error;
474 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
475 3235492e 2018-04-01 stsp got_ref_close(head_ref);
476 3235492e 2018-04-01 stsp if (error != NULL)
477 3235492e 2018-04-01 stsp return error;
478 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
479 3235492e 2018-04-01 stsp } else {
480 d142fc45 2018-04-01 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
481 3235492e 2018-04-01 stsp if (error == NULL) {
482 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
483 3235492e 2018-04-01 stsp if (id == NULL)
484 3235492e 2018-04-01 stsp error = got_error_from_errno();
485 3235492e 2018-04-01 stsp }
486 3235492e 2018-04-01 stsp }
487 d7d4f210 2018-03-12 stsp if (error != NULL)
488 d7d4f210 2018-03-12 stsp return error;
489 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
490 64a96a6d 2018-04-01 stsp error = print_commits(obj, id, repo, show_patch, limit);
491 8bf5b3c9 2018-03-17 stsp else
492 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
493 f42b1b34 2018-03-12 stsp got_object_close(obj);
494 f42b1b34 2018-03-12 stsp free(id);
495 f42b1b34 2018-03-12 stsp got_repo_close(repo);
496 8bf5b3c9 2018-03-17 stsp return error;
497 b00d56cd 2018-04-01 stsp }
498 b00d56cd 2018-04-01 stsp
499 b00d56cd 2018-04-01 stsp __dead void
500 b00d56cd 2018-04-01 stsp usage_diff(void)
501 b00d56cd 2018-04-01 stsp {
502 b00d56cd 2018-04-01 stsp fprintf(stderr, "usage: %s diff repository-path object1 object2\n",
503 b00d56cd 2018-04-01 stsp getprogname());
504 b00d56cd 2018-04-01 stsp exit(1);
505 b00d56cd 2018-04-01 stsp }
506 b00d56cd 2018-04-01 stsp
507 b00d56cd 2018-04-01 stsp static const struct got_error *
508 b00d56cd 2018-04-01 stsp diff_blobs(struct got_object *obj1, struct got_object *obj2,
509 b00d56cd 2018-04-01 stsp struct got_repository *repo)
510 b00d56cd 2018-04-01 stsp {
511 b00d56cd 2018-04-01 stsp const struct got_error *err;
512 b00d56cd 2018-04-01 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
513 b00d56cd 2018-04-01 stsp
514 b00d56cd 2018-04-01 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
515 b00d56cd 2018-04-01 stsp if (err)
516 b00d56cd 2018-04-01 stsp goto done;
517 b00d56cd 2018-04-01 stsp err = got_object_blob_open(&blob2, repo, obj2, 81992);
518 b00d56cd 2018-04-01 stsp if (err)
519 b00d56cd 2018-04-01 stsp goto done;
520 b00d56cd 2018-04-01 stsp
521 b00d56cd 2018-04-01 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
522 b00d56cd 2018-04-01 stsp done:
523 b00d56cd 2018-04-01 stsp if (blob1)
524 b00d56cd 2018-04-01 stsp got_object_blob_close(blob1);
525 b00d56cd 2018-04-01 stsp if (blob2)
526 b00d56cd 2018-04-01 stsp got_object_blob_close(blob2);
527 b00d56cd 2018-04-01 stsp return err;
528 b00d56cd 2018-04-01 stsp }
529 b00d56cd 2018-04-01 stsp
530 b00d56cd 2018-04-01 stsp static const struct got_error *
531 b00d56cd 2018-04-01 stsp diff_trees(struct got_object *obj1, struct got_object *obj2,
532 b00d56cd 2018-04-01 stsp struct got_repository *repo)
533 b00d56cd 2018-04-01 stsp {
534 b00d56cd 2018-04-01 stsp const struct got_error *err;
535 b00d56cd 2018-04-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
536 b00d56cd 2018-04-01 stsp
537 b00d56cd 2018-04-01 stsp err = got_object_tree_open(&tree1, repo, obj1);
538 b00d56cd 2018-04-01 stsp if (err)
539 b00d56cd 2018-04-01 stsp goto done;
540 b00d56cd 2018-04-01 stsp err = got_object_tree_open(&tree2, repo, obj2);
541 b00d56cd 2018-04-01 stsp if (err)
542 b00d56cd 2018-04-01 stsp goto done;
543 b00d56cd 2018-04-01 stsp
544 b00d56cd 2018-04-01 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
545 b00d56cd 2018-04-01 stsp done:
546 b00d56cd 2018-04-01 stsp if (tree1)
547 b00d56cd 2018-04-01 stsp got_object_tree_close(tree1);
548 b00d56cd 2018-04-01 stsp if (tree2)
549 b00d56cd 2018-04-01 stsp got_object_tree_close(tree2);
550 b00d56cd 2018-04-01 stsp return err;
551 5c860e29 2018-03-12 stsp }
552 5c860e29 2018-03-12 stsp
553 b00d56cd 2018-04-01 stsp static const struct got_error *
554 b00d56cd 2018-04-01 stsp diff_commits(struct got_object *obj1, struct got_object *obj2,
555 b00d56cd 2018-04-01 stsp struct got_repository *repo)
556 b00d56cd 2018-04-01 stsp {
557 b00d56cd 2018-04-01 stsp const struct got_error *err;
558 b00d56cd 2018-04-01 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
559 b00d56cd 2018-04-01 stsp struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
560 b00d56cd 2018-04-01 stsp
561 b00d56cd 2018-04-01 stsp err = got_object_commit_open(&commit1, repo, obj1);
562 b00d56cd 2018-04-01 stsp if (err)
563 b00d56cd 2018-04-01 stsp goto done;
564 b00d56cd 2018-04-01 stsp err = got_object_commit_open(&commit2, repo, obj2);
565 b00d56cd 2018-04-01 stsp if (err)
566 b00d56cd 2018-04-01 stsp goto done;
567 b00d56cd 2018-04-01 stsp
568 b00d56cd 2018-04-01 stsp err = got_object_open(&tree_obj1, repo, commit1->tree_id);
569 b00d56cd 2018-04-01 stsp if (err)
570 b00d56cd 2018-04-01 stsp goto done;
571 b00d56cd 2018-04-01 stsp err = got_object_open(&tree_obj2, repo, commit2->tree_id);
572 b00d56cd 2018-04-01 stsp if (err)
573 b00d56cd 2018-04-01 stsp goto done;
574 b00d56cd 2018-04-01 stsp
575 b00d56cd 2018-04-01 stsp err = diff_trees(tree_obj1, tree_obj2, repo);
576 b00d56cd 2018-04-01 stsp done:
577 b00d56cd 2018-04-01 stsp if (tree_obj1)
578 b00d56cd 2018-04-01 stsp got_object_close(tree_obj1);
579 b00d56cd 2018-04-01 stsp if (tree_obj2)
580 b00d56cd 2018-04-01 stsp got_object_close(tree_obj2);
581 b00d56cd 2018-04-01 stsp if (commit1)
582 b00d56cd 2018-04-01 stsp got_object_commit_close(commit1);
583 b00d56cd 2018-04-01 stsp if (commit2)
584 b00d56cd 2018-04-01 stsp got_object_commit_close(commit2);
585 b00d56cd 2018-04-01 stsp return err;
586 b00d56cd 2018-04-01 stsp
587 b00d56cd 2018-04-01 stsp }
588 b00d56cd 2018-04-01 stsp
589 b00d56cd 2018-04-01 stsp const struct got_error *
590 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
591 b00d56cd 2018-04-01 stsp {
592 b00d56cd 2018-04-01 stsp const struct got_error *error;
593 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
594 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
595 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
596 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
597 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
598 b00d56cd 2018-04-01 stsp int ch;
599 b00d56cd 2018-04-01 stsp
600 b00d56cd 2018-04-01 stsp #ifndef PROFILE
601 b00d56cd 2018-04-01 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
602 b00d56cd 2018-04-01 stsp err(1, "pledge");
603 b00d56cd 2018-04-01 stsp #endif
604 b00d56cd 2018-04-01 stsp
605 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
606 b00d56cd 2018-04-01 stsp switch (ch) {
607 b00d56cd 2018-04-01 stsp default:
608 b00d56cd 2018-04-01 stsp usage();
609 b00d56cd 2018-04-01 stsp /* NOTREACHED */
610 b00d56cd 2018-04-01 stsp }
611 b00d56cd 2018-04-01 stsp }
612 b00d56cd 2018-04-01 stsp
613 b00d56cd 2018-04-01 stsp argc -= optind;
614 b00d56cd 2018-04-01 stsp argv += optind;
615 b00d56cd 2018-04-01 stsp
616 b00d56cd 2018-04-01 stsp if (argc == 0) {
617 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
618 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
619 b00d56cd 2018-04-01 stsp repo_path = argv[0];
620 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
621 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
622 b00d56cd 2018-04-01 stsp } else
623 b00d56cd 2018-04-01 stsp usage_diff();
624 b00d56cd 2018-04-01 stsp
625 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
626 b00d56cd 2018-04-01 stsp if (error != NULL)
627 b00d56cd 2018-04-01 stsp goto done;
628 b00d56cd 2018-04-01 stsp
629 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
630 b00d56cd 2018-04-01 stsp if (error == NULL) {
631 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
632 b00d56cd 2018-04-01 stsp if (id1 == NULL)
633 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
634 b00d56cd 2018-04-01 stsp }
635 b00d56cd 2018-04-01 stsp if (error != NULL)
636 b00d56cd 2018-04-01 stsp goto done;
637 b00d56cd 2018-04-01 stsp
638 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
639 b00d56cd 2018-04-01 stsp if (error == NULL) {
640 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
641 b00d56cd 2018-04-01 stsp if (id2 == NULL)
642 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
643 b00d56cd 2018-04-01 stsp }
644 b00d56cd 2018-04-01 stsp if (error != NULL)
645 b00d56cd 2018-04-01 stsp goto done;
646 b00d56cd 2018-04-01 stsp
647 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
648 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
649 b00d56cd 2018-04-01 stsp goto done;
650 b00d56cd 2018-04-01 stsp }
651 b00d56cd 2018-04-01 stsp
652 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
653 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
654 b00d56cd 2018-04-01 stsp error = diff_blobs(obj1, obj2, repo);
655 b00d56cd 2018-04-01 stsp break;
656 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
657 b00d56cd 2018-04-01 stsp error = diff_trees(obj1, obj2, repo);
658 b00d56cd 2018-04-01 stsp break;
659 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
660 b00d56cd 2018-04-01 stsp error = diff_commits(obj1, obj2, repo);
661 b00d56cd 2018-04-01 stsp break;
662 b00d56cd 2018-04-01 stsp default:
663 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
664 b00d56cd 2018-04-01 stsp }
665 b00d56cd 2018-04-01 stsp
666 b00d56cd 2018-04-01 stsp done:
667 b00d56cd 2018-04-01 stsp if (obj1)
668 b00d56cd 2018-04-01 stsp got_object_close(obj1);
669 b00d56cd 2018-04-01 stsp if (obj2)
670 b00d56cd 2018-04-01 stsp got_object_close(obj2);
671 b00d56cd 2018-04-01 stsp if (id1)
672 b00d56cd 2018-04-01 stsp free(id1);
673 b00d56cd 2018-04-01 stsp if (id2)
674 b00d56cd 2018-04-01 stsp free(id2);
675 b00d56cd 2018-04-01 stsp if (repo)
676 b00d56cd 2018-04-01 stsp got_repo_close(repo);
677 b00d56cd 2018-04-01 stsp return error;
678 b00d56cd 2018-04-01 stsp }
679 b00d56cd 2018-04-01 stsp
680 f42b1b34 2018-03-12 stsp #ifdef notyet
681 d7d4f210 2018-03-12 stsp const struct got_error *
682 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
683 5c860e29 2018-03-12 stsp {
684 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
685 5c860e29 2018-03-12 stsp git_status_list *status;
686 5c860e29 2018-03-12 stsp git_status_options statusopts;
687 5c860e29 2018-03-12 stsp size_t i;
688 5c860e29 2018-03-12 stsp
689 5c860e29 2018-03-12 stsp git_libgit2_init();
690 5c860e29 2018-03-12 stsp
691 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
692 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
693 5c860e29 2018-03-12 stsp
694 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
695 5c860e29 2018-03-12 stsp errx(1, "bar repository");
696 5c860e29 2018-03-12 stsp
697 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
698 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
699 5c860e29 2018-03-12 stsp
700 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
701 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
702 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
703 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
704 5c860e29 2018-03-12 stsp
705 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
706 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
707 5c860e29 2018-03-12 stsp
708 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
709 5c860e29 2018-03-12 stsp const git_status_entry *se;
710 5c860e29 2018-03-12 stsp
711 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
712 5c860e29 2018-03-12 stsp switch (se->status) {
713 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
714 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
715 5c860e29 2018-03-12 stsp break;
716 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
717 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
718 5c860e29 2018-03-12 stsp break;
719 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
720 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
721 5c860e29 2018-03-12 stsp break;
722 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
723 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
724 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
725 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
726 5c860e29 2018-03-12 stsp break;
727 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
728 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
729 5c860e29 2018-03-12 stsp break;
730 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
731 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
732 5c860e29 2018-03-12 stsp break;
733 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
734 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
735 5c860e29 2018-03-12 stsp break;
736 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
737 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
738 5c860e29 2018-03-12 stsp break;
739 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
740 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
741 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
742 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
743 5c860e29 2018-03-12 stsp break;
744 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
745 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
746 5c860e29 2018-03-12 stsp break;
747 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
748 5c860e29 2018-03-12 stsp default:
749 5c860e29 2018-03-12 stsp break;
750 5c860e29 2018-03-12 stsp }
751 5c860e29 2018-03-12 stsp }
752 5c860e29 2018-03-12 stsp
753 5c860e29 2018-03-12 stsp git_status_list_free(status);
754 5c860e29 2018-03-12 stsp git_repository_free(repo);
755 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
756 5c860e29 2018-03-12 stsp
757 5c860e29 2018-03-12 stsp return 0;
758 5c860e29 2018-03-12 stsp }
759 f42b1b34 2018-03-12 stsp #endif