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>
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.
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.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
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>
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"
37 5c860e29 2018-03-12 stsp #ifndef nitems
38 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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;
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);
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 *[]);
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" },
72 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
74 5c860e29 2018-03-12 stsp struct cmd *cmd;
75 5c860e29 2018-03-12 stsp unsigned int i;
77 1b6b95a8 2018-03-12 stsp int hflag = 0;
79 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
81 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
82 5c860e29 2018-03-12 stsp switch (ch) {
88 5c860e29 2018-03-12 stsp /* NOTREACHED */
92 5c860e29 2018-03-12 stsp argc -= optind;
93 5c860e29 2018-03-12 stsp argv += optind;
96 5c860e29 2018-03-12 stsp if (argc <= 0)
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;
102 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
104 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
108 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
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);
119 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
123 5c860e29 2018-03-12 stsp __dead void
124 5c860e29 2018-03-12 stsp usage(void)
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);
137 c09a553d 2018-03-12 stsp __dead void
138 c09a553d 2018-03-12 stsp usage_checkout(void)
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());
145 92a684f4 2018-03-12 stsp static void
146 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
148 92a684f4 2018-03-12 stsp char *worktree_path = arg;
150 92a684f4 2018-03-12 stsp while (path[0] == '/')
153 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
156 c09a553d 2018-03-12 stsp const struct got_error *
157 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
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 = "";
168 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
169 0bb8a95e 2018-03-12 stsp switch (ch) {
171 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
175 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
179 0bb8a95e 2018-03-12 stsp argc -= optind;
180 0bb8a95e 2018-03-12 stsp argv += optind;
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");
186 0bb8a95e 2018-03-12 stsp if (argc == 1) {
187 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
188 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
189 76089277 2018-04-01 stsp if (repo_path == NULL)
190 76089277 2018-04-01 stsp return got_error_from_errno();
191 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
192 76089277 2018-04-01 stsp if (cwd == NULL) {
193 76089277 2018-04-01 stsp error = got_error_from_errno();
196 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
197 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
199 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
200 76089277 2018-04-01 stsp if (base == NULL) {
201 76089277 2018-04-01 stsp error = got_error_from_errno();
204 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
205 c09a553d 2018-03-12 stsp if (dotgit)
206 c09a553d 2018-03-12 stsp *dotgit = '\0';
207 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
208 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
213 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
214 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
215 76089277 2018-04-01 stsp if (repo_path == NULL) {
216 76089277 2018-04-01 stsp error = got_error_from_errno();
219 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
220 76089277 2018-04-01 stsp if (worktree_path == NULL) {
221 76089277 2018-04-01 stsp error = got_error_from_errno();
225 c09a553d 2018-03-12 stsp usage_checkout();
227 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
228 76089277 2018-04-01 stsp free(repo_path);
229 c09a553d 2018-03-12 stsp if (error != NULL)
231 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
232 c09a553d 2018-03-12 stsp if (error != NULL)
235 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
236 c09a553d 2018-03-12 stsp if (error != NULL)
239 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
240 c09a553d 2018-03-12 stsp if (error != NULL)
243 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
244 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
245 c09a553d 2018-03-12 stsp if (error != NULL)
248 c09a553d 2018-03-12 stsp printf("checked out %s\n", worktree_path);
251 76089277 2018-04-01 stsp free(repo_path);
252 c09a553d 2018-03-12 stsp free(worktree_path);
253 c09a553d 2018-03-12 stsp return error;
256 f42b1b34 2018-03-12 stsp static const struct got_error *
257 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
258 f42b1b34 2018-03-12 stsp struct got_repository *repo)
260 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
261 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
262 79109fed 2018-03-27 stsp struct got_object *obj;
263 79109fed 2018-03-27 stsp struct got_parent_id *pid;
265 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
267 79109fed 2018-03-27 stsp return err;
269 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
270 79109fed 2018-03-27 stsp got_object_close(obj);
272 79109fed 2018-03-27 stsp return err;
274 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
275 79109fed 2018-03-27 stsp if (pid != NULL) {
276 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
278 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
280 79109fed 2018-03-27 stsp return err;
282 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
283 79109fed 2018-03-27 stsp got_object_close(obj);
285 79109fed 2018-03-27 stsp return err;
287 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
288 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
290 79109fed 2018-03-27 stsp return err;
291 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
292 79109fed 2018-03-27 stsp got_object_close(obj);
294 79109fed 2018-03-27 stsp return err;
297 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
299 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
300 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
301 79109fed 2018-03-27 stsp return err;
304 79109fed 2018-03-27 stsp static const struct got_error *
305 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
306 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
308 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
311 8bf5b3c9 2018-03-17 stsp err = got_object_id_str(&buf, id);
313 8bf5b3c9 2018-03-17 stsp return err;
315 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
316 8bf5b3c9 2018-03-17 stsp printf("commit: %s\n", buf);
317 8bf5b3c9 2018-03-17 stsp printf("Author: %s\n", commit->author);
318 f3d135e1 2018-04-01 stsp if (strcmp(commit->author, commit->committer) != 0)
319 f3d135e1 2018-04-01 stsp printf("Committer: %s\n", commit->committer);
320 8bf5b3c9 2018-03-17 stsp printf("\n%s\n", commit->logmsg);
322 971751ac 2018-03-27 stsp if (show_patch) {
323 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
324 971751ac 2018-03-27 stsp if (err == 0)
325 971751ac 2018-03-27 stsp printf("\n");
329 79109fed 2018-03-27 stsp return err;
332 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry {
333 8bf5b3c9 2018-03-17 stsp TAILQ_ENTRY(commit_queue_entry) entry;
334 8bf5b3c9 2018-03-17 stsp struct got_object_id *id;
335 8bf5b3c9 2018-03-17 stsp struct got_commit_object *commit;
338 f42b1b34 2018-03-12 stsp static const struct got_error *
339 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
340 64a96a6d 2018-04-01 stsp struct got_repository *repo, int show_patch, int limit)
342 f42b1b34 2018-03-12 stsp const struct got_error *err;
343 8bf5b3c9 2018-03-17 stsp struct got_commit_object *root_commit;
344 8bf5b3c9 2018-03-17 stsp TAILQ_HEAD(, commit_queue_entry) commits;
345 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *entry;
347 8bf5b3c9 2018-03-17 stsp TAILQ_INIT(&commits);
349 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&root_commit, repo, root_obj);
351 f42b1b34 2018-03-12 stsp return err;
353 8bf5b3c9 2018-03-17 stsp entry = calloc(1, sizeof(*entry));
354 8bf5b3c9 2018-03-17 stsp if (entry == NULL)
355 0a585a0d 2018-03-17 stsp return got_error_from_errno();
356 8bf5b3c9 2018-03-17 stsp entry->id = got_object_id_dup(root_id);
357 8bf5b3c9 2018-03-17 stsp if (entry->id == NULL) {
358 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
359 8bf5b3c9 2018-03-17 stsp free(entry);
360 0a585a0d 2018-03-17 stsp return err;
362 8bf5b3c9 2018-03-17 stsp entry->commit = root_commit;
363 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_HEAD(&commits, entry, entry);
365 8bf5b3c9 2018-03-17 stsp while (!TAILQ_EMPTY(&commits)) {
366 8bf5b3c9 2018-03-17 stsp struct got_parent_id *pid;
368 8bf5b3c9 2018-03-17 stsp entry = TAILQ_FIRST(&commits);
369 79109fed 2018-03-27 stsp err = print_commit(entry->commit, entry->id, repo, show_patch);
371 25470781 2018-04-01 stsp while (!TAILQ_EMPTY(&commits)) {
372 25470781 2018-04-01 stsp entry = TAILQ_FIRST(&commits);
373 25470781 2018-04-01 stsp TAILQ_REMOVE(&commits, entry, entry);
374 25470781 2018-04-01 stsp got_object_commit_close(entry->commit);
375 25470781 2018-04-01 stsp free(entry->id);
376 25470781 2018-04-01 stsp free(entry);
381 64a96a6d 2018-04-01 stsp if (limit && --limit == 0)
384 8bf5b3c9 2018-03-17 stsp SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
385 8bf5b3c9 2018-03-17 stsp struct got_object *obj;
386 8bf5b3c9 2018-03-17 stsp struct got_commit_object *pcommit;
387 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *pentry;
389 8bf5b3c9 2018-03-17 stsp err = got_object_open(&obj, repo, pid->id);
392 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
393 8bf5b3c9 2018-03-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
397 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&pcommit, repo, obj);
398 8bf5b3c9 2018-03-17 stsp got_object_close(obj);
402 8bf5b3c9 2018-03-17 stsp pentry = calloc(1, sizeof(*pentry));
403 8bf5b3c9 2018-03-17 stsp if (pentry == NULL) {
404 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
405 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
408 8bf5b3c9 2018-03-17 stsp pentry->id = got_object_id_dup(pid->id);
409 8bf5b3c9 2018-03-17 stsp if (pentry->id == NULL) {
410 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
411 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
414 8bf5b3c9 2018-03-17 stsp pentry->commit = pcommit;
415 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_TAIL(&commits, pentry, entry);
418 8bf5b3c9 2018-03-17 stsp TAILQ_REMOVE(&commits, entry, entry);
419 8bf5b3c9 2018-03-17 stsp got_object_commit_close(entry->commit);
420 8bf5b3c9 2018-03-17 stsp free(entry->id);
421 8bf5b3c9 2018-03-17 stsp free(entry);
424 f42b1b34 2018-03-12 stsp return err;
427 6f3d1eb0 2018-03-12 stsp __dead void
428 6f3d1eb0 2018-03-12 stsp usage_log(void)
430 64a96a6d 2018-04-01 stsp fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
431 64a96a6d 2018-04-01 stsp "[repository-path]\n", getprogname());
435 d7d4f210 2018-03-12 stsp const struct got_error *
436 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
438 f42b1b34 2018-03-12 stsp const struct got_error *error;
439 f42b1b34 2018-03-12 stsp struct got_repository *repo;
440 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
441 f42b1b34 2018-03-12 stsp struct got_object *obj;
442 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
443 d142fc45 2018-04-01 stsp char *start_commit = NULL;
445 64a96a6d 2018-04-01 stsp int show_patch = 0, limit = 0;
446 64a96a6d 2018-04-01 stsp const char *errstr;
448 6715a751 2018-03-16 stsp #ifndef PROFILE
449 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
450 f42b1b34 2018-03-12 stsp err(1, "pledge");
453 64a96a6d 2018-04-01 stsp while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
454 79109fed 2018-03-27 stsp switch (ch) {
456 79109fed 2018-03-27 stsp show_patch = 1;
459 d142fc45 2018-04-01 stsp start_commit = optarg;
462 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
463 64a96a6d 2018-04-01 stsp if (errstr != NULL)
464 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
468 79109fed 2018-03-27 stsp /* NOTREACHED */
472 79109fed 2018-03-27 stsp argc -= optind;
473 79109fed 2018-03-27 stsp argv += optind;
475 79109fed 2018-03-27 stsp if (argc == 0) {
476 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
477 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
478 e1e3f570 2018-04-01 stsp return got_error_from_errno();
479 3235492e 2018-04-01 stsp } else if (argc == 1) {
480 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
481 76089277 2018-04-01 stsp if (repo_path == NULL)
482 76089277 2018-04-01 stsp return got_error_from_errno();
484 6f3d1eb0 2018-03-12 stsp usage_log();
486 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
487 76089277 2018-04-01 stsp free(repo_path);
488 d7d4f210 2018-03-12 stsp if (error != NULL)
489 d7d4f210 2018-03-12 stsp return error;
491 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
492 3235492e 2018-04-01 stsp struct got_reference *head_ref;
493 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
494 3235492e 2018-04-01 stsp if (error != NULL)
495 3235492e 2018-04-01 stsp return error;
496 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
497 3235492e 2018-04-01 stsp got_ref_close(head_ref);
498 3235492e 2018-04-01 stsp if (error != NULL)
499 3235492e 2018-04-01 stsp return error;
500 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
502 d142fc45 2018-04-01 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
503 3235492e 2018-04-01 stsp if (error == NULL) {
504 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
505 3235492e 2018-04-01 stsp if (id == NULL)
506 3235492e 2018-04-01 stsp error = got_error_from_errno();
509 d7d4f210 2018-03-12 stsp if (error != NULL)
510 d7d4f210 2018-03-12 stsp return error;
511 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
512 64a96a6d 2018-04-01 stsp error = print_commits(obj, id, repo, show_patch, limit);
514 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
515 f42b1b34 2018-03-12 stsp got_object_close(obj);
517 f42b1b34 2018-03-12 stsp got_repo_close(repo);
518 8bf5b3c9 2018-03-17 stsp return error;
521 b00d56cd 2018-04-01 stsp static const struct got_error *
522 b00d56cd 2018-04-01 stsp diff_blobs(struct got_object *obj1, struct got_object *obj2,
523 b00d56cd 2018-04-01 stsp struct got_repository *repo)
525 b00d56cd 2018-04-01 stsp const struct got_error *err;
526 b00d56cd 2018-04-01 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
528 b00d56cd 2018-04-01 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
531 b00d56cd 2018-04-01 stsp err = got_object_blob_open(&blob2, repo, obj2, 81992);
535 b00d56cd 2018-04-01 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
538 b00d56cd 2018-04-01 stsp got_object_blob_close(blob1);
540 b00d56cd 2018-04-01 stsp got_object_blob_close(blob2);
541 b00d56cd 2018-04-01 stsp return err;
544 b00d56cd 2018-04-01 stsp static const struct got_error *
545 b00d56cd 2018-04-01 stsp diff_trees(struct got_object *obj1, struct got_object *obj2,
546 b00d56cd 2018-04-01 stsp struct got_repository *repo)
548 b00d56cd 2018-04-01 stsp const struct got_error *err;
549 b00d56cd 2018-04-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
551 b00d56cd 2018-04-01 stsp err = got_object_tree_open(&tree1, repo, obj1);
554 b00d56cd 2018-04-01 stsp err = got_object_tree_open(&tree2, repo, obj2);
558 b00d56cd 2018-04-01 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
561 b00d56cd 2018-04-01 stsp got_object_tree_close(tree1);
563 b00d56cd 2018-04-01 stsp got_object_tree_close(tree2);
564 b00d56cd 2018-04-01 stsp return err;
567 b00d56cd 2018-04-01 stsp static const struct got_error *
568 b00d56cd 2018-04-01 stsp diff_commits(struct got_object *obj1, struct got_object *obj2,
569 b00d56cd 2018-04-01 stsp struct got_repository *repo)
571 b00d56cd 2018-04-01 stsp const struct got_error *err;
572 b00d56cd 2018-04-01 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
573 b00d56cd 2018-04-01 stsp struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
575 b00d56cd 2018-04-01 stsp err = got_object_commit_open(&commit1, repo, obj1);
578 b00d56cd 2018-04-01 stsp err = got_object_commit_open(&commit2, repo, obj2);
582 b00d56cd 2018-04-01 stsp err = got_object_open(&tree_obj1, repo, commit1->tree_id);
585 b00d56cd 2018-04-01 stsp err = got_object_open(&tree_obj2, repo, commit2->tree_id);
589 b00d56cd 2018-04-01 stsp err = diff_trees(tree_obj1, tree_obj2, repo);
591 b00d56cd 2018-04-01 stsp if (tree_obj1)
592 b00d56cd 2018-04-01 stsp got_object_close(tree_obj1);
593 b00d56cd 2018-04-01 stsp if (tree_obj2)
594 b00d56cd 2018-04-01 stsp got_object_close(tree_obj2);
595 b00d56cd 2018-04-01 stsp if (commit1)
596 b00d56cd 2018-04-01 stsp got_object_commit_close(commit1);
597 b00d56cd 2018-04-01 stsp if (commit2)
598 b00d56cd 2018-04-01 stsp got_object_commit_close(commit2);
599 b00d56cd 2018-04-01 stsp return err;
603 3f8b7d6a 2018-04-01 stsp __dead void
604 3f8b7d6a 2018-04-01 stsp usage_diff(void)
606 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
607 3f8b7d6a 2018-04-01 stsp getprogname());
611 b00d56cd 2018-04-01 stsp const struct got_error *
612 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
614 b00d56cd 2018-04-01 stsp const struct got_error *error;
615 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
616 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
617 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
618 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
619 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
622 b00d56cd 2018-04-01 stsp #ifndef PROFILE
623 b00d56cd 2018-04-01 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
624 b00d56cd 2018-04-01 stsp err(1, "pledge");
627 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
628 b00d56cd 2018-04-01 stsp switch (ch) {
631 b00d56cd 2018-04-01 stsp /* NOTREACHED */
635 b00d56cd 2018-04-01 stsp argc -= optind;
636 b00d56cd 2018-04-01 stsp argv += optind;
638 b00d56cd 2018-04-01 stsp if (argc == 0) {
639 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
640 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
641 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
642 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
643 e1e3f570 2018-04-01 stsp return got_error_from_errno();
644 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
645 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
646 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
647 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
648 76089277 2018-04-01 stsp if (repo_path == NULL)
649 76089277 2018-04-01 stsp return got_error_from_errno();
650 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
651 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
653 b00d56cd 2018-04-01 stsp usage_diff();
655 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
656 76089277 2018-04-01 stsp free(repo_path);
657 b00d56cd 2018-04-01 stsp if (error != NULL)
660 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
661 b00d56cd 2018-04-01 stsp if (error == NULL) {
662 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
663 b00d56cd 2018-04-01 stsp if (id1 == NULL)
664 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
666 b00d56cd 2018-04-01 stsp if (error != NULL)
669 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
670 b00d56cd 2018-04-01 stsp if (error == NULL) {
671 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
672 b00d56cd 2018-04-01 stsp if (id2 == NULL)
673 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
675 b00d56cd 2018-04-01 stsp if (error != NULL)
678 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
679 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
683 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
684 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
685 b00d56cd 2018-04-01 stsp error = diff_blobs(obj1, obj2, repo);
687 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
688 b00d56cd 2018-04-01 stsp error = diff_trees(obj1, obj2, repo);
690 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
691 b00d56cd 2018-04-01 stsp error = diff_commits(obj1, obj2, repo);
694 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
699 b00d56cd 2018-04-01 stsp got_object_close(obj1);
701 b00d56cd 2018-04-01 stsp got_object_close(obj2);
707 b00d56cd 2018-04-01 stsp got_repo_close(repo);
708 b00d56cd 2018-04-01 stsp return error;
711 f42b1b34 2018-03-12 stsp #ifdef notyet
712 d7d4f210 2018-03-12 stsp const struct got_error *
713 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
715 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
716 5c860e29 2018-03-12 stsp git_status_list *status;
717 5c860e29 2018-03-12 stsp git_status_options statusopts;
720 5c860e29 2018-03-12 stsp git_libgit2_init();
722 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
723 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
725 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
726 5c860e29 2018-03-12 stsp errx(1, "bar repository");
728 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
729 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
731 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
732 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
733 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
734 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
736 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
737 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
739 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
740 5c860e29 2018-03-12 stsp const git_status_entry *se;
742 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
743 5c860e29 2018-03-12 stsp switch (se->status) {
744 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
745 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
747 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
748 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
750 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
751 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
753 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
754 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
755 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
756 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
758 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
759 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
761 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
762 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
764 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
765 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
767 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
768 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
770 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
771 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
772 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
773 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
775 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
776 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
778 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
784 5c860e29 2018-03-12 stsp git_status_list_free(status);
785 5c860e29 2018-03-12 stsp git_repository_free(repo);
786 5c860e29 2018-03-12 stsp git_libgit2_shutdown();