Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <locale.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <libgen.h>
32 #include <time.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_worktree.h"
39 #include "got_diff.h"
40 #include "got_commit_graph.h"
41 #include "got_blame.h"
42 #include "got_privsep.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 static volatile sig_atomic_t sigint_received;
49 static volatile sig_atomic_t sigpipe_received;
51 static void
52 catch_sigint(int signo)
53 {
54 sigint_received = 1;
55 }
57 static void
58 catch_sigpipe(int signo)
59 {
60 sigpipe_received = 1;
61 }
64 struct cmd {
65 const char *cmd_name;
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
68 const char *cmd_descr;
69 };
71 __dead static void usage(void);
72 __dead static void usage_checkout(void);
73 __dead static void usage_update(void);
74 __dead static void usage_log(void);
75 __dead static void usage_diff(void);
76 __dead static void usage_blame(void);
77 __dead static void usage_tree(void);
78 __dead static void usage_status(void);
80 static const struct got_error* cmd_checkout(int, char *[]);
81 static const struct got_error* cmd_update(int, char *[]);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
86 static const struct got_error* cmd_status(int, char *[]);
88 static struct cmd got_commands[] = {
89 { "checkout", cmd_checkout, usage_checkout,
90 "check out a new work tree from a repository" },
91 { "update", cmd_update, usage_update,
92 "update a work tree to a different commit" },
93 { "log", cmd_log, usage_log,
94 "show repository history" },
95 { "diff", cmd_diff, usage_diff,
96 "compare files and directories" },
97 { "blame", cmd_blame, usage_blame,
98 " show when lines in a file were changed" },
99 { "tree", cmd_tree, usage_tree,
100 " list files and directories in repository" },
101 { "status", cmd_status, usage_status,
102 "show modification status of files" },
103 };
105 int
106 main(int argc, char *argv[])
108 struct cmd *cmd;
109 unsigned int i;
110 int ch;
111 int hflag = 0;
113 setlocale(LC_CTYPE, "");
115 while ((ch = getopt(argc, argv, "h")) != -1) {
116 switch (ch) {
117 case 'h':
118 hflag = 1;
119 break;
120 default:
121 usage();
122 /* NOTREACHED */
126 argc -= optind;
127 argv += optind;
128 optind = 0;
130 if (argc <= 0)
131 usage();
133 signal(SIGINT, catch_sigint);
134 signal(SIGPIPE, catch_sigpipe);
136 for (i = 0; i < nitems(got_commands); i++) {
137 const struct got_error *error;
139 cmd = &got_commands[i];
141 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
142 continue;
144 if (hflag)
145 got_commands[i].cmd_usage();
147 error = got_commands[i].cmd_main(argc, argv);
148 if (error && !(sigint_received || sigpipe_received)) {
149 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
150 return 1;
153 return 0;
156 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
157 return 1;
160 __dead static void
161 usage(void)
163 int i;
165 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
166 "Available commands:\n", getprogname());
167 for (i = 0; i < nitems(got_commands); i++) {
168 struct cmd *cmd = &got_commands[i];
169 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
171 exit(1);
174 static const struct got_error *
175 apply_unveil(const char *repo_path, const char *worktree_path)
177 const struct got_error *error;
179 if (repo_path && unveil(repo_path, "r") != 0)
180 return got_error_from_errno();
182 if (worktree_path && unveil(worktree_path, "rwc") != 0)
183 return got_error_from_errno();
185 if (unveil("/tmp", "rwc") != 0)
186 return got_error_from_errno();
188 error = got_privsep_unveil_exec_helpers();
189 if (error != NULL)
190 return error;
192 if (unveil(NULL, NULL) != 0)
193 return got_error_from_errno();
195 return NULL;
198 __dead static void
199 usage_checkout(void)
201 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
202 "[worktree-path]\n", getprogname());
203 exit(1);
206 static void
207 checkout_progress(void *arg, unsigned char status, const char *path)
209 char *worktree_path = arg;
211 while (path[0] == '/')
212 path++;
214 printf("%c %s/%s\n", status, worktree_path, path);
217 static const struct got_error *
218 check_cancelled(void *arg)
220 if (sigint_received || sigpipe_received)
221 return got_error(GOT_ERR_CANCELLED);
222 return NULL;
225 static const struct got_error *
226 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
227 struct got_repository *repo)
229 const struct got_error *err;
230 struct got_reference *head_ref = NULL;
231 struct got_object_id *head_commit_id = NULL;
232 struct got_commit_graph *graph = NULL;
234 head_ref = got_worktree_get_head_ref(worktree);
235 if (head_ref == NULL)
236 return got_error_from_errno();
238 /* TODO: Check the reflog. The head ref may have been rebased. */
239 err = got_ref_resolve(&head_commit_id, repo, head_ref);
240 if (err)
241 goto done;
243 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
244 if (err)
245 goto done;
247 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
248 if (err)
249 goto done;
250 while (1) {
251 struct got_object_id *id;
253 if (sigint_received || sigpipe_received)
254 break;
256 err = got_commit_graph_iter_next(&id, graph);
257 if (err) {
258 if (err->code == GOT_ERR_ITER_COMPLETED) {
259 err = got_error(GOT_ERR_ANCESTRY);
260 break;
262 if (err->code != GOT_ERR_ITER_NEED_MORE)
263 break;
264 err = got_commit_graph_fetch_commits(graph, 1, repo);
265 if (err)
266 break;
267 else
268 continue;
270 if (id == NULL)
271 break;
272 if (got_object_id_cmp(id, commit_id) == 0)
273 break;
275 done:
276 if (head_ref)
277 got_ref_close(head_ref);
278 if (graph)
279 got_commit_graph_close(graph);
280 return err;
284 static const struct got_error *
285 cmd_checkout(int argc, char *argv[])
287 const struct got_error *error = NULL;
288 struct got_repository *repo = NULL;
289 struct got_reference *head_ref = NULL;
290 struct got_worktree *worktree = NULL;
291 char *repo_path = NULL;
292 char *worktree_path = NULL;
293 const char *path_prefix = "";
294 char *commit_id_str = NULL;
295 int ch, same_path_prefix;
297 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
298 switch (ch) {
299 case 'c':
300 commit_id_str = strdup(optarg);
301 if (commit_id_str == NULL)
302 return got_error_from_errno();
303 break;
304 case 'p':
305 path_prefix = optarg;
306 break;
307 default:
308 usage();
309 /* NOTREACHED */
313 argc -= optind;
314 argv += optind;
316 #ifndef PROFILE
317 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
318 NULL) == -1)
319 err(1, "pledge");
320 #endif
321 if (argc == 1) {
322 char *cwd, *base, *dotgit;
323 repo_path = realpath(argv[0], NULL);
324 if (repo_path == NULL)
325 return got_error_from_errno();
326 cwd = getcwd(NULL, 0);
327 if (cwd == NULL) {
328 error = got_error_from_errno();
329 goto done;
331 if (path_prefix[0])
332 base = basename(path_prefix);
333 else
334 base = basename(repo_path);
335 if (base == NULL) {
336 error = got_error_from_errno();
337 goto done;
339 dotgit = strstr(base, ".git");
340 if (dotgit)
341 *dotgit = '\0';
342 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
343 error = got_error_from_errno();
344 free(cwd);
345 goto done;
347 free(cwd);
348 } else if (argc == 2) {
349 repo_path = realpath(argv[0], NULL);
350 if (repo_path == NULL) {
351 error = got_error_from_errno();
352 goto done;
354 worktree_path = realpath(argv[1], NULL);
355 if (worktree_path == NULL) {
356 error = got_error_from_errno();
357 goto done;
359 } else
360 usage_checkout();
362 error = apply_unveil(repo_path, worktree_path);
363 if (error)
364 goto done;
366 error = got_repo_open(&repo, repo_path);
367 if (error != NULL)
368 goto done;
370 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
371 if (error != NULL)
372 goto done;
374 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
375 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
376 goto done;
378 error = got_worktree_open(&worktree, worktree_path);
379 if (error != NULL)
380 goto done;
382 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
383 path_prefix);
384 if (error != NULL)
385 goto done;
386 if (!same_path_prefix) {
387 error = got_error(GOT_ERR_PATH_PREFIX);
388 goto done;
391 if (commit_id_str) {
392 struct got_object_id *commit_id;
393 error = got_object_resolve_id_str(&commit_id, repo,
394 commit_id_str);
395 if (error != NULL)
396 goto done;
397 error = check_ancestry(worktree, commit_id, repo);
398 if (error != NULL) {
399 free(commit_id);
400 goto done;
402 error = got_worktree_set_base_commit_id(worktree, repo,
403 commit_id);
404 free(commit_id);
405 if (error)
406 goto done;
409 error = got_worktree_checkout_files(worktree, repo,
410 checkout_progress, worktree_path, check_cancelled, NULL);
411 if (error != NULL)
412 goto done;
414 printf("Now shut up and hack\n");
416 done:
417 free(commit_id_str);
418 free(repo_path);
419 free(worktree_path);
420 return error;
423 __dead static void
424 usage_update(void)
426 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
427 getprogname());
428 exit(1);
431 static void
432 update_progress(void *arg, unsigned char status, const char *path)
434 int *did_something = arg;
436 if (status == GOT_STATUS_EXISTS)
437 return;
439 *did_something = 1;
440 while (path[0] == '/')
441 path++;
442 printf("%c %s\n", status, path);
445 static const struct got_error *
446 cmd_update(int argc, char *argv[])
448 const struct got_error *error = NULL;
449 struct got_repository *repo = NULL;
450 struct got_worktree *worktree = NULL;
451 char *worktree_path = NULL;
452 struct got_object_id *commit_id = NULL;
453 char *commit_id_str = NULL;
454 int ch, did_something = 0;
456 while ((ch = getopt(argc, argv, "c:")) != -1) {
457 switch (ch) {
458 case 'c':
459 commit_id_str = strdup(optarg);
460 if (commit_id_str == NULL)
461 return got_error_from_errno();
462 break;
463 default:
464 usage();
465 /* NOTREACHED */
469 argc -= optind;
470 argv += optind;
472 #ifndef PROFILE
473 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
474 NULL) == -1)
475 err(1, "pledge");
476 #endif
477 if (argc == 0) {
478 worktree_path = getcwd(NULL, 0);
479 if (worktree_path == NULL) {
480 error = got_error_from_errno();
481 goto done;
483 } else if (argc == 1) {
484 worktree_path = realpath(argv[0], NULL);
485 if (worktree_path == NULL) {
486 error = got_error_from_errno();
487 goto done;
489 } else
490 usage_update();
492 error = got_worktree_open(&worktree, worktree_path);
493 if (error != NULL)
494 goto done;
496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
497 if (error != NULL)
498 goto done;
500 error = apply_unveil(got_repo_get_path(repo), worktree_path);
501 if (error)
502 goto done;
504 if (commit_id_str == NULL) {
505 struct got_reference *head_ref;
506 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
507 if (error != NULL)
508 goto done;
509 error = got_ref_resolve(&commit_id, repo, head_ref);
510 if (error != NULL)
511 goto done;
512 error = got_object_id_str(&commit_id_str, commit_id);
513 if (error != NULL)
514 goto done;
515 } else {
516 error = got_object_resolve_id_str(&commit_id, repo,
517 commit_id_str);
518 if (error != NULL)
519 goto done;
522 error = check_ancestry(worktree, commit_id, repo);
523 if (error != NULL)
524 goto done;
526 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
527 commit_id) != 0) {
528 error = got_worktree_set_base_commit_id(worktree, repo,
529 commit_id);
530 if (error)
531 goto done;
534 error = got_worktree_checkout_files(worktree, repo,
535 update_progress, &did_something, check_cancelled, NULL);
536 if (error != NULL)
537 goto done;
539 if (did_something)
540 printf("Updated to commit %s\n", commit_id_str);
541 else
542 printf("Already up-to-date\n");
543 done:
544 free(worktree_path);
545 free(commit_id);
546 free(commit_id_str);
547 return error;
550 static const struct got_error *
551 print_patch(struct got_commit_object *commit, struct got_object_id *id,
552 int diff_context, struct got_repository *repo)
554 const struct got_error *err = NULL;
555 struct got_tree_object *tree1 = NULL, *tree2;
556 struct got_object_qid *qid;
557 char *id_str1 = NULL, *id_str2;
559 err = got_object_open_as_tree(&tree2, repo,
560 got_object_commit_get_tree_id(commit));
561 if (err)
562 return err;
564 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
565 if (qid != NULL) {
566 struct got_commit_object *pcommit;
568 err = got_object_open_as_commit(&pcommit, repo, qid->id);
569 if (err)
570 return err;
572 err = got_object_open_as_tree(&tree1, repo,
573 got_object_commit_get_tree_id(pcommit));
574 got_object_commit_close(pcommit);
575 if (err)
576 return err;
578 err = got_object_id_str(&id_str1, qid->id);
579 if (err)
580 return err;
583 err = got_object_id_str(&id_str2, id);
584 if (err)
585 goto done;
587 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
588 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
589 done:
590 if (tree1)
591 got_object_tree_close(tree1);
592 got_object_tree_close(tree2);
593 free(id_str1);
594 free(id_str2);
595 return err;
598 static char *
599 get_datestr(time_t *time, char *datebuf)
601 char *p, *s = ctime_r(time, datebuf);
602 p = strchr(s, '\n');
603 if (p)
604 *p = '\0';
605 return s;
608 static const struct got_error *
609 print_commit(struct got_commit_object *commit, struct got_object_id *id,
610 struct got_repository *repo, int show_patch, int diff_context,
611 struct got_reflist_head *refs)
613 const struct got_error *err = NULL;
614 char *id_str, *datestr, *logmsg0, *logmsg, *line;
615 char datebuf[26];
616 time_t committer_time;
617 const char *author, *committer;
618 char *refs_str = NULL;
619 struct got_reflist_entry *re;
621 SIMPLEQ_FOREACH(re, refs, entry) {
622 char *s;
623 const char *name;
624 if (got_object_id_cmp(re->id, id) != 0)
625 continue;
626 name = got_ref_get_name(re->ref);
627 if (strncmp(name, "refs/", 5) == 0)
628 name += 5;
629 if (strncmp(name, "heads/", 6) == 0)
630 name += 6;
631 if (strncmp(name, "remotes/", 8) == 0)
632 name += 8;
633 s = refs_str;
634 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
635 name) == -1) {
636 err = got_error_from_errno();
637 free(s);
638 break;
640 free(s);
642 err = got_object_id_str(&id_str, id);
643 if (err)
644 return err;
646 printf("-----------------------------------------------\n");
647 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
648 refs_str ? refs_str : "", refs_str ? ")" : "");
649 free(id_str);
650 printf("from: %s\n", got_object_commit_get_author(commit));
651 committer_time = got_object_commit_get_committer_time(commit);
652 datestr = get_datestr(&committer_time, datebuf);
653 printf("date: %s UTC\n", datestr);
654 author = got_object_commit_get_author(commit);
655 committer = got_object_commit_get_committer(commit);
656 if (strcmp(author, committer) != 0)
657 printf("via: %s\n", committer);
658 if (got_object_commit_get_nparents(commit) > 1) {
659 const struct got_object_id_queue *parent_ids;
660 struct got_object_qid *qid;
661 int n = 1;
662 parent_ids = got_object_commit_get_parent_ids(commit);
663 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
664 err = got_object_id_str(&id_str, qid->id);
665 if (err)
666 return err;
667 printf("parent %d: %s\n", n++, id_str);
668 free(id_str);
672 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
673 if (logmsg0 == NULL)
674 return got_error_from_errno();
676 logmsg = logmsg0;
677 do {
678 line = strsep(&logmsg, "\n");
679 if (line)
680 printf(" %s\n", line);
681 } while (line);
682 free(logmsg0);
684 if (show_patch) {
685 err = print_patch(commit, id, diff_context, repo);
686 if (err == 0)
687 printf("\n");
690 fflush(stdout);
691 return err;
694 static const struct got_error *
695 print_commits(struct got_object_id *root_id, struct got_repository *repo,
696 char *path, int show_patch, int diff_context, int limit,
697 int first_parent_traversal, struct got_reflist_head *refs)
699 const struct got_error *err;
700 struct got_commit_graph *graph;
702 err = got_commit_graph_open(&graph, root_id, path,
703 first_parent_traversal, repo);
704 if (err)
705 return err;
706 err = got_commit_graph_iter_start(graph, root_id, repo);
707 if (err)
708 goto done;
709 while (1) {
710 struct got_commit_object *commit;
711 struct got_object_id *id;
713 if (sigint_received || sigpipe_received)
714 break;
716 err = got_commit_graph_iter_next(&id, graph);
717 if (err) {
718 if (err->code == GOT_ERR_ITER_COMPLETED) {
719 err = NULL;
720 break;
722 if (err->code != GOT_ERR_ITER_NEED_MORE)
723 break;
724 err = got_commit_graph_fetch_commits(graph, 1, repo);
725 if (err)
726 break;
727 else
728 continue;
730 if (id == NULL)
731 break;
733 err = got_object_open_as_commit(&commit, repo, id);
734 if (err)
735 break;
736 err = print_commit(commit, id, repo, show_patch, diff_context,
737 refs);
738 got_object_commit_close(commit);
739 if (err || (limit && --limit == 0))
740 break;
742 done:
743 got_commit_graph_close(graph);
744 return err;
747 __dead static void
748 usage_log(void)
750 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
751 "[-r repository-path] [path]\n", getprogname());
752 exit(1);
755 static const struct got_error *
756 cmd_log(int argc, char *argv[])
758 const struct got_error *error;
759 struct got_repository *repo = NULL;
760 struct got_commit_object *commit = NULL;
761 struct got_object_id *id = NULL;
762 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
763 char *start_commit = NULL;
764 int diff_context = 3, ch;
765 int show_patch = 0, limit = 0, first_parent_traversal = 0;
766 const char *errstr;
767 struct got_reflist_head refs;
769 #ifndef PROFILE
770 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
771 NULL)
772 == -1)
773 err(1, "pledge");
774 #endif
776 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
777 switch (ch) {
778 case 'p':
779 show_patch = 1;
780 break;
781 case 'c':
782 start_commit = optarg;
783 break;
784 case 'C':
785 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
786 &errstr);
787 if (errstr != NULL)
788 err(1, "-C option %s", errstr);
789 break;
790 case 'l':
791 limit = strtonum(optarg, 1, INT_MAX, &errstr);
792 if (errstr != NULL)
793 err(1, "-l option %s", errstr);
794 break;
795 case 'f':
796 first_parent_traversal = 1;
797 break;
798 case 'r':
799 repo_path = realpath(optarg, NULL);
800 if (repo_path == NULL)
801 err(1, "-r option");
802 break;
803 default:
804 usage();
805 /* NOTREACHED */
809 argc -= optind;
810 argv += optind;
812 if (argc == 0)
813 path = strdup("");
814 else if (argc == 1)
815 path = strdup(argv[0]);
816 else
817 usage_log();
818 if (path == NULL)
819 return got_error_from_errno();
821 cwd = getcwd(NULL, 0);
822 if (cwd == NULL) {
823 error = got_error_from_errno();
824 goto done;
826 if (repo_path == NULL) {
827 repo_path = strdup(cwd);
828 if (repo_path == NULL) {
829 error = got_error_from_errno();
830 goto done;
834 error = apply_unveil(repo_path, NULL);
835 if (error)
836 goto done;
838 error = got_repo_open(&repo, repo_path);
839 if (error != NULL)
840 goto done;
842 if (start_commit == NULL) {
843 struct got_reference *head_ref;
844 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
845 if (error != NULL)
846 return error;
847 error = got_ref_resolve(&id, repo, head_ref);
848 got_ref_close(head_ref);
849 if (error != NULL)
850 return error;
851 error = got_object_open_as_commit(&commit, repo, id);
852 } else {
853 struct got_reference *ref;
854 error = got_ref_open(&ref, repo, start_commit);
855 if (error == NULL) {
856 int obj_type;
857 error = got_ref_resolve(&id, repo, ref);
858 got_ref_close(ref);
859 if (error != NULL)
860 goto done;
861 error = got_object_get_type(&obj_type, repo, id);
862 if (error != NULL)
863 goto done;
864 if (obj_type == GOT_OBJ_TYPE_TAG) {
865 struct got_tag_object *tag;
866 error = got_object_open_as_tag(&tag, repo, id);
867 if (error != NULL)
868 goto done;
869 if (got_object_tag_get_object_type(tag) !=
870 GOT_OBJ_TYPE_COMMIT) {
871 got_object_tag_close(tag);
872 error = got_error(GOT_ERR_OBJ_TYPE);
873 goto done;
875 free(id);
876 id = got_object_id_dup(
877 got_object_tag_get_object_id(tag));
878 if (id == NULL)
879 error = got_error_from_errno();
880 got_object_tag_close(tag);
881 if (error)
882 goto done;
883 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
884 error = got_error(GOT_ERR_OBJ_TYPE);
885 goto done;
887 error = got_object_open_as_commit(&commit, repo, id);
888 if (error != NULL)
889 goto done;
891 if (commit == NULL) {
892 error = got_object_resolve_id_str(&id, repo,
893 start_commit);
894 if (error != NULL)
895 return error;
898 if (error != NULL)
899 goto done;
901 error = got_repo_map_path(&in_repo_path, repo, path, 1);
902 if (error != NULL)
903 goto done;
904 if (in_repo_path) {
905 free(path);
906 path = in_repo_path;
909 SIMPLEQ_INIT(&refs);
910 error = got_ref_list(&refs, repo);
911 if (error)
912 goto done;
914 error = print_commits(id, repo, path, show_patch,
915 diff_context, limit, first_parent_traversal, &refs);
916 done:
917 free(path);
918 free(repo_path);
919 free(cwd);
920 free(id);
921 if (repo) {
922 const struct got_error *repo_error;
923 repo_error = got_repo_close(repo);
924 if (error == NULL)
925 error = repo_error;
927 return error;
930 __dead static void
931 usage_diff(void)
933 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
934 "object1 object2\n", getprogname());
935 exit(1);
938 static const struct got_error *
939 cmd_diff(int argc, char *argv[])
941 const struct got_error *error;
942 struct got_repository *repo = NULL;
943 char *repo_path = NULL;
944 struct got_object_id *id1 = NULL, *id2 = NULL;
945 char *id_str1 = NULL, *id_str2 = NULL;
946 int type1, type2;
947 int diff_context = 3, ch;
948 const char *errstr;
950 #ifndef PROFILE
951 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
952 NULL) == -1)
953 err(1, "pledge");
954 #endif
956 while ((ch = getopt(argc, argv, "C:")) != -1) {
957 switch (ch) {
958 case 'C':
959 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
960 if (errstr != NULL)
961 err(1, "-C option %s", errstr);
962 break;
963 default:
964 usage();
965 /* NOTREACHED */
969 argc -= optind;
970 argv += optind;
972 if (argc == 0) {
973 usage_diff(); /* TODO show local worktree changes */
974 } else if (argc == 2) {
975 repo_path = getcwd(NULL, 0);
976 if (repo_path == NULL)
977 return got_error_from_errno();
978 id_str1 = argv[0];
979 id_str2 = argv[1];
980 } else if (argc == 3) {
981 repo_path = realpath(argv[0], NULL);
982 if (repo_path == NULL)
983 return got_error_from_errno();
984 id_str1 = argv[1];
985 id_str2 = argv[2];
986 } else
987 usage_diff();
989 error = apply_unveil(repo_path, NULL);
990 if (error)
991 goto done;
993 error = got_repo_open(&repo, repo_path);
994 free(repo_path);
995 if (error != NULL)
996 goto done;
998 error = got_object_resolve_id_str(&id1, repo, id_str1);
999 if (error)
1000 goto done;
1002 error = got_object_resolve_id_str(&id2, repo, id_str2);
1003 if (error)
1004 goto done;
1006 error = got_object_get_type(&type1, repo, id1);
1007 if (error)
1008 goto done;
1010 error = got_object_get_type(&type2, repo, id2);
1011 if (error)
1012 goto done;
1014 if (type1 != type2) {
1015 error = got_error(GOT_ERR_OBJ_TYPE);
1016 goto done;
1019 switch (type1) {
1020 case GOT_OBJ_TYPE_BLOB:
1021 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1022 diff_context, repo, stdout);
1023 break;
1024 case GOT_OBJ_TYPE_TREE:
1025 error = got_diff_objects_as_trees(id1, id2, "", "",
1026 diff_context, repo, stdout);
1027 break;
1028 case GOT_OBJ_TYPE_COMMIT:
1029 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1030 id_str2);
1031 error = got_diff_objects_as_commits(id1, id2, diff_context,
1032 repo, stdout);
1033 break;
1034 default:
1035 error = got_error(GOT_ERR_OBJ_TYPE);
1038 done:
1039 free(id1);
1040 free(id2);
1041 if (repo) {
1042 const struct got_error *repo_error;
1043 repo_error = got_repo_close(repo);
1044 if (error == NULL)
1045 error = repo_error;
1047 return error;
1050 __dead static void
1051 usage_blame(void)
1053 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1054 getprogname());
1055 exit(1);
1058 static const struct got_error *
1059 cmd_blame(int argc, char *argv[])
1061 const struct got_error *error;
1062 struct got_repository *repo = NULL;
1063 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1064 struct got_object_id *commit_id = NULL;
1065 char *commit_id_str = NULL;
1066 int ch;
1068 #ifndef PROFILE
1069 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1070 NULL) == -1)
1071 err(1, "pledge");
1072 #endif
1074 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1075 switch (ch) {
1076 case 'c':
1077 commit_id_str = optarg;
1078 break;
1079 case 'r':
1080 repo_path = realpath(optarg, NULL);
1081 if (repo_path == NULL)
1082 err(1, "-r option");
1083 break;
1084 default:
1085 usage();
1086 /* NOTREACHED */
1090 argc -= optind;
1091 argv += optind;
1093 if (argc == 1)
1094 path = argv[0];
1095 else
1096 usage_blame();
1098 cwd = getcwd(NULL, 0);
1099 if (cwd == NULL) {
1100 error = got_error_from_errno();
1101 goto done;
1103 if (repo_path == NULL) {
1104 repo_path = strdup(cwd);
1105 if (repo_path == NULL) {
1106 error = got_error_from_errno();
1107 goto done;
1111 error = apply_unveil(repo_path, NULL);
1112 if (error)
1113 goto done;
1115 error = got_repo_open(&repo, repo_path);
1116 if (error != NULL)
1117 goto done;
1119 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1120 if (error != NULL)
1121 goto done;
1123 if (commit_id_str == NULL) {
1124 struct got_reference *head_ref;
1125 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1126 if (error != NULL)
1127 goto done;
1128 error = got_ref_resolve(&commit_id, repo, head_ref);
1129 got_ref_close(head_ref);
1130 if (error != NULL)
1131 goto done;
1132 } else {
1133 error = got_object_resolve_id_str(&commit_id, repo,
1134 commit_id_str);
1135 if (error != NULL)
1136 goto done;
1139 error = got_blame(in_repo_path, commit_id, repo, stdout);
1140 done:
1141 free(in_repo_path);
1142 free(repo_path);
1143 free(cwd);
1144 free(commit_id);
1145 if (repo) {
1146 const struct got_error *repo_error;
1147 repo_error = got_repo_close(repo);
1148 if (error == NULL)
1149 error = repo_error;
1151 return error;
1154 __dead static void
1155 usage_tree(void)
1157 fprintf(stderr,
1158 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1159 getprogname());
1160 exit(1);
1163 static void
1164 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1165 const char *root_path)
1167 int is_root_path = (strcmp(path, root_path) == 0);
1169 path += strlen(root_path);
1170 while (path[0] == '/')
1171 path++;
1173 printf("%s%s%s%s%s\n", id ? id : "", path,
1174 is_root_path ? "" : "/",
1175 te->name, S_ISDIR(te->mode) ? "/" : "");
1178 static const struct got_error *
1179 print_tree(const char *path, struct got_object_id *commit_id,
1180 int show_ids, int recurse, const char *root_path,
1181 struct got_repository *repo)
1183 const struct got_error *err = NULL;
1184 struct got_object_id *tree_id = NULL;
1185 struct got_tree_object *tree = NULL;
1186 const struct got_tree_entries *entries;
1187 struct got_tree_entry *te;
1189 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1190 if (err)
1191 goto done;
1193 err = got_object_open_as_tree(&tree, repo, tree_id);
1194 if (err)
1195 goto done;
1196 entries = got_object_tree_get_entries(tree);
1197 te = SIMPLEQ_FIRST(&entries->head);
1198 while (te) {
1199 char *id = NULL;
1201 if (sigint_received || sigpipe_received)
1202 break;
1204 if (show_ids) {
1205 char *id_str;
1206 err = got_object_id_str(&id_str, te->id);
1207 if (err)
1208 goto done;
1209 if (asprintf(&id, "%s ", id_str) == -1) {
1210 err = got_error_from_errno();
1211 free(id_str);
1212 goto done;
1214 free(id_str);
1216 print_entry(te, id, path, root_path);
1217 free(id);
1219 if (recurse && S_ISDIR(te->mode)) {
1220 char *child_path;
1221 if (asprintf(&child_path, "%s%s%s", path,
1222 path[0] == '/' && path[1] == '\0' ? "" : "/",
1223 te->name) == -1) {
1224 err = got_error_from_errno();
1225 goto done;
1227 err = print_tree(child_path, commit_id, show_ids, 1,
1228 root_path, repo);
1229 free(child_path);
1230 if (err)
1231 goto done;
1234 te = SIMPLEQ_NEXT(te, entry);
1236 done:
1237 if (tree)
1238 got_object_tree_close(tree);
1239 free(tree_id);
1240 return err;
1243 static const struct got_error *
1244 cmd_tree(int argc, char *argv[])
1246 const struct got_error *error;
1247 struct got_repository *repo = NULL;
1248 struct got_worktree *worktree = NULL;
1249 const char *path;
1250 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1251 struct got_object_id *commit_id = NULL;
1252 char *commit_id_str = NULL;
1253 int show_ids = 0, recurse = 0;
1254 int ch;
1256 #ifndef PROFILE
1257 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1258 NULL) == -1)
1259 err(1, "pledge");
1260 #endif
1262 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1263 switch (ch) {
1264 case 'c':
1265 commit_id_str = optarg;
1266 break;
1267 case 'r':
1268 repo_path = realpath(optarg, NULL);
1269 if (repo_path == NULL)
1270 err(1, "-r option");
1271 break;
1272 case 'i':
1273 show_ids = 1;
1274 break;
1275 case 'R':
1276 recurse = 1;
1277 break;
1278 default:
1279 usage();
1280 /* NOTREACHED */
1284 argc -= optind;
1285 argv += optind;
1287 if (argc == 1)
1288 path = argv[0];
1289 else if (argc > 1)
1290 usage_tree();
1291 else
1292 path = NULL;
1294 if (repo_path == NULL) {
1295 cwd = getcwd(NULL, 0);
1296 if (cwd == NULL) {
1297 error = got_error_from_errno();
1298 goto done;
1300 error = got_worktree_open(&worktree, cwd);
1301 if (error && (error->code != GOT_ERR_ERRNO && errno != ENOENT))
1302 goto done;
1303 else
1304 error = NULL;
1305 if (worktree) {
1306 repo_path =
1307 strdup(got_worktree_get_repo_path(worktree));
1308 if (repo_path == NULL)
1309 error = got_error_from_errno();
1310 if (error)
1311 goto done;
1312 if (path == NULL)
1313 path = got_worktree_get_path_prefix(worktree);
1314 } else {
1315 repo_path = strdup(cwd);
1316 if (repo_path == NULL) {
1317 error = got_error_from_errno();
1318 goto done;
1323 error = apply_unveil(repo_path, NULL);
1324 if (error)
1325 goto done;
1327 error = got_repo_open(&repo, repo_path);
1328 if (error != NULL)
1329 goto done;
1331 if (path == NULL)
1332 path = "/";
1333 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1334 if (error != NULL)
1335 goto done;
1337 if (commit_id_str == NULL) {
1338 struct got_reference *head_ref;
1339 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1340 if (error != NULL)
1341 goto done;
1342 error = got_ref_resolve(&commit_id, repo, head_ref);
1343 got_ref_close(head_ref);
1344 if (error != NULL)
1345 goto done;
1346 } else {
1347 error = got_object_resolve_id_str(&commit_id, repo,
1348 commit_id_str);
1349 if (error != NULL)
1350 goto done;
1353 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1354 in_repo_path, repo);
1355 done:
1356 free(in_repo_path);
1357 free(repo_path);
1358 free(cwd);
1359 free(commit_id);
1360 if (worktree)
1361 got_worktree_close(worktree);
1362 if (repo) {
1363 const struct got_error *repo_error;
1364 repo_error = got_repo_close(repo);
1365 if (error == NULL)
1366 error = repo_error;
1368 return error;
1371 __dead static void
1372 usage_status(void)
1374 fprintf(stderr, "usage: %s status [worktree-path]\n", getprogname());
1375 exit(1);
1378 static void
1379 print_status(void *arg, unsigned char status, const char *path)
1381 printf("%c %s\n", status, path);
1384 static const struct got_error *
1385 cmd_status(int argc, char *argv[])
1387 const struct got_error *error = NULL;
1388 struct got_repository *repo = NULL;
1389 struct got_worktree *worktree = NULL;
1390 char *worktree_path = NULL;
1391 int ch;
1393 while ((ch = getopt(argc, argv, "")) != -1) {
1394 switch (ch) {
1395 default:
1396 usage();
1397 /* NOTREACHED */
1401 argc -= optind;
1402 argv += optind;
1404 #ifndef PROFILE
1405 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1406 NULL) == -1)
1407 err(1, "pledge");
1408 #endif
1409 if (argc == 0) {
1410 worktree_path = getcwd(NULL, 0);
1411 if (worktree_path == NULL) {
1412 error = got_error_from_errno();
1413 goto done;
1415 } else if (argc == 1) {
1416 worktree_path = realpath(argv[0], NULL);
1417 if (worktree_path == NULL) {
1418 error = got_error_from_errno();
1419 goto done;
1421 } else
1422 usage_status();
1424 error = got_worktree_open(&worktree, worktree_path);
1425 if (error != NULL)
1426 goto done;
1428 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1429 if (error != NULL)
1430 goto done;
1432 error = apply_unveil(got_repo_get_path(repo), worktree_path);
1433 if (error)
1434 goto done;
1436 error = got_worktree_status(worktree, repo, print_status, NULL,
1437 check_cancelled, NULL);
1438 done:
1439 free(worktree_path);
1440 return error;