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),
501 got_worktree_get_root_path(worktree));
502 if (error)
503 goto done;
505 if (commit_id_str == NULL) {
506 struct got_reference *head_ref;
507 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
508 if (error != NULL)
509 goto done;
510 error = got_ref_resolve(&commit_id, repo, head_ref);
511 if (error != NULL)
512 goto done;
513 error = got_object_id_str(&commit_id_str, commit_id);
514 if (error != NULL)
515 goto done;
516 } else {
517 error = got_object_resolve_id_str(&commit_id, repo,
518 commit_id_str);
519 if (error != NULL)
520 goto done;
523 error = check_ancestry(worktree, commit_id, repo);
524 if (error != NULL)
525 goto done;
527 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
528 commit_id) != 0) {
529 error = got_worktree_set_base_commit_id(worktree, repo,
530 commit_id);
531 if (error)
532 goto done;
535 error = got_worktree_checkout_files(worktree, repo,
536 update_progress, &did_something, check_cancelled, NULL);
537 if (error != NULL)
538 goto done;
540 if (did_something)
541 printf("Updated to commit %s\n", commit_id_str);
542 else
543 printf("Already up-to-date\n");
544 done:
545 free(worktree_path);
546 free(commit_id);
547 free(commit_id_str);
548 return error;
551 static const struct got_error *
552 print_patch(struct got_commit_object *commit, struct got_object_id *id,
553 int diff_context, struct got_repository *repo)
555 const struct got_error *err = NULL;
556 struct got_tree_object *tree1 = NULL, *tree2;
557 struct got_object_qid *qid;
558 char *id_str1 = NULL, *id_str2;
560 err = got_object_open_as_tree(&tree2, repo,
561 got_object_commit_get_tree_id(commit));
562 if (err)
563 return err;
565 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
566 if (qid != NULL) {
567 struct got_commit_object *pcommit;
569 err = got_object_open_as_commit(&pcommit, repo, qid->id);
570 if (err)
571 return err;
573 err = got_object_open_as_tree(&tree1, repo,
574 got_object_commit_get_tree_id(pcommit));
575 got_object_commit_close(pcommit);
576 if (err)
577 return err;
579 err = got_object_id_str(&id_str1, qid->id);
580 if (err)
581 return err;
584 err = got_object_id_str(&id_str2, id);
585 if (err)
586 goto done;
588 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
589 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
590 done:
591 if (tree1)
592 got_object_tree_close(tree1);
593 got_object_tree_close(tree2);
594 free(id_str1);
595 free(id_str2);
596 return err;
599 static char *
600 get_datestr(time_t *time, char *datebuf)
602 char *p, *s = ctime_r(time, datebuf);
603 p = strchr(s, '\n');
604 if (p)
605 *p = '\0';
606 return s;
609 static const struct got_error *
610 print_commit(struct got_commit_object *commit, struct got_object_id *id,
611 struct got_repository *repo, int show_patch, int diff_context,
612 struct got_reflist_head *refs)
614 const struct got_error *err = NULL;
615 char *id_str, *datestr, *logmsg0, *logmsg, *line;
616 char datebuf[26];
617 time_t committer_time;
618 const char *author, *committer;
619 char *refs_str = NULL;
620 struct got_reflist_entry *re;
622 SIMPLEQ_FOREACH(re, refs, entry) {
623 char *s;
624 const char *name;
625 if (got_object_id_cmp(re->id, id) != 0)
626 continue;
627 name = got_ref_get_name(re->ref);
628 if (strcmp(name, GOT_REF_HEAD) == 0)
629 continue;
630 if (strncmp(name, "refs/", 5) == 0)
631 name += 5;
632 if (strncmp(name, "heads/", 6) == 0)
633 name += 6;
634 if (strncmp(name, "remotes/", 8) == 0)
635 name += 8;
636 s = refs_str;
637 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
638 name) == -1) {
639 err = got_error_from_errno();
640 free(s);
641 break;
643 free(s);
645 err = got_object_id_str(&id_str, id);
646 if (err)
647 return err;
649 printf("-----------------------------------------------\n");
650 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
651 refs_str ? refs_str : "", refs_str ? ")" : "");
652 free(id_str);
653 printf("from: %s\n", got_object_commit_get_author(commit));
654 committer_time = got_object_commit_get_committer_time(commit);
655 datestr = get_datestr(&committer_time, datebuf);
656 printf("date: %s UTC\n", datestr);
657 author = got_object_commit_get_author(commit);
658 committer = got_object_commit_get_committer(commit);
659 if (strcmp(author, committer) != 0)
660 printf("via: %s\n", committer);
661 if (got_object_commit_get_nparents(commit) > 1) {
662 const struct got_object_id_queue *parent_ids;
663 struct got_object_qid *qid;
664 int n = 1;
665 parent_ids = got_object_commit_get_parent_ids(commit);
666 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
667 err = got_object_id_str(&id_str, qid->id);
668 if (err)
669 return err;
670 printf("parent %d: %s\n", n++, id_str);
671 free(id_str);
675 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
676 if (logmsg0 == NULL)
677 return got_error_from_errno();
679 logmsg = logmsg0;
680 do {
681 line = strsep(&logmsg, "\n");
682 if (line)
683 printf(" %s\n", line);
684 } while (line);
685 free(logmsg0);
687 if (show_patch) {
688 err = print_patch(commit, id, diff_context, repo);
689 if (err == 0)
690 printf("\n");
693 fflush(stdout);
694 return err;
697 static const struct got_error *
698 print_commits(struct got_object_id *root_id, struct got_repository *repo,
699 char *path, int show_patch, int diff_context, int limit,
700 int first_parent_traversal, struct got_reflist_head *refs)
702 const struct got_error *err;
703 struct got_commit_graph *graph;
705 err = got_commit_graph_open(&graph, root_id, path,
706 first_parent_traversal, repo);
707 if (err)
708 return err;
709 err = got_commit_graph_iter_start(graph, root_id, repo);
710 if (err)
711 goto done;
712 while (1) {
713 struct got_commit_object *commit;
714 struct got_object_id *id;
716 if (sigint_received || sigpipe_received)
717 break;
719 err = got_commit_graph_iter_next(&id, graph);
720 if (err) {
721 if (err->code == GOT_ERR_ITER_COMPLETED) {
722 err = NULL;
723 break;
725 if (err->code != GOT_ERR_ITER_NEED_MORE)
726 break;
727 err = got_commit_graph_fetch_commits(graph, 1, repo);
728 if (err)
729 break;
730 else
731 continue;
733 if (id == NULL)
734 break;
736 err = got_object_open_as_commit(&commit, repo, id);
737 if (err)
738 break;
739 err = print_commit(commit, id, repo, show_patch, diff_context,
740 refs);
741 got_object_commit_close(commit);
742 if (err || (limit && --limit == 0))
743 break;
745 done:
746 got_commit_graph_close(graph);
747 return err;
750 __dead static void
751 usage_log(void)
753 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
754 "[-r repository-path] [path]\n", getprogname());
755 exit(1);
758 static const struct got_error *
759 cmd_log(int argc, char *argv[])
761 const struct got_error *error;
762 struct got_repository *repo = NULL;
763 struct got_worktree *worktree = NULL;
764 struct got_commit_object *commit = NULL;
765 struct got_object_id *id = NULL;
766 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
767 char *start_commit = NULL;
768 int diff_context = 3, ch;
769 int show_patch = 0, limit = 0, first_parent_traversal = 0;
770 const char *errstr;
771 struct got_reflist_head refs;
773 #ifndef PROFILE
774 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
775 NULL)
776 == -1)
777 err(1, "pledge");
778 #endif
780 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
781 switch (ch) {
782 case 'p':
783 show_patch = 1;
784 break;
785 case 'c':
786 start_commit = optarg;
787 break;
788 case 'C':
789 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
790 &errstr);
791 if (errstr != NULL)
792 err(1, "-C option %s", errstr);
793 break;
794 case 'l':
795 limit = strtonum(optarg, 1, INT_MAX, &errstr);
796 if (errstr != NULL)
797 err(1, "-l option %s", errstr);
798 break;
799 case 'f':
800 first_parent_traversal = 1;
801 break;
802 case 'r':
803 repo_path = realpath(optarg, NULL);
804 if (repo_path == NULL)
805 err(1, "-r option");
806 break;
807 default:
808 usage();
809 /* NOTREACHED */
813 argc -= optind;
814 argv += optind;
816 if (argc == 0)
817 path = strdup("");
818 else if (argc == 1)
819 path = strdup(argv[0]);
820 else
821 usage_log();
822 if (path == NULL)
823 return got_error_from_errno();
825 cwd = getcwd(NULL, 0);
826 if (cwd == NULL) {
827 error = got_error_from_errno();
828 goto done;
831 error = got_worktree_open(&worktree, cwd);
832 if (error && error->code != GOT_ERR_NOT_WORKTREE)
833 goto done;
834 error = NULL;
836 repo_path = worktree ?
837 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
838 if (repo_path == NULL) {
839 error = got_error_from_errno();
840 goto done;
843 error = apply_unveil(repo_path, NULL);
844 if (error)
845 goto done;
847 error = got_repo_open(&repo, repo_path);
848 if (error != NULL)
849 goto done;
851 if (start_commit == NULL) {
852 struct got_reference *head_ref;
853 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
854 if (error != NULL)
855 return error;
856 error = got_ref_resolve(&id, repo, head_ref);
857 got_ref_close(head_ref);
858 if (error != NULL)
859 return error;
860 error = got_object_open_as_commit(&commit, repo, id);
861 } else {
862 struct got_reference *ref;
863 error = got_ref_open(&ref, repo, start_commit);
864 if (error == NULL) {
865 int obj_type;
866 error = got_ref_resolve(&id, repo, ref);
867 got_ref_close(ref);
868 if (error != NULL)
869 goto done;
870 error = got_object_get_type(&obj_type, repo, id);
871 if (error != NULL)
872 goto done;
873 if (obj_type == GOT_OBJ_TYPE_TAG) {
874 struct got_tag_object *tag;
875 error = got_object_open_as_tag(&tag, repo, id);
876 if (error != NULL)
877 goto done;
878 if (got_object_tag_get_object_type(tag) !=
879 GOT_OBJ_TYPE_COMMIT) {
880 got_object_tag_close(tag);
881 error = got_error(GOT_ERR_OBJ_TYPE);
882 goto done;
884 free(id);
885 id = got_object_id_dup(
886 got_object_tag_get_object_id(tag));
887 if (id == NULL)
888 error = got_error_from_errno();
889 got_object_tag_close(tag);
890 if (error)
891 goto done;
892 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
893 error = got_error(GOT_ERR_OBJ_TYPE);
894 goto done;
896 error = got_object_open_as_commit(&commit, repo, id);
897 if (error != NULL)
898 goto done;
900 if (commit == NULL) {
901 error = got_object_resolve_id_str(&id, repo,
902 start_commit);
903 if (error != NULL)
904 return error;
907 if (error != NULL)
908 goto done;
910 error = got_repo_map_path(&in_repo_path, repo, path, 1);
911 if (error != NULL)
912 goto done;
913 if (in_repo_path) {
914 free(path);
915 path = in_repo_path;
918 SIMPLEQ_INIT(&refs);
919 error = got_ref_list(&refs, repo);
920 if (error)
921 goto done;
923 error = print_commits(id, repo, path, show_patch,
924 diff_context, limit, first_parent_traversal, &refs);
925 done:
926 free(path);
927 free(repo_path);
928 free(cwd);
929 free(id);
930 if (worktree)
931 got_worktree_close(worktree);
932 if (repo) {
933 const struct got_error *repo_error;
934 repo_error = got_repo_close(repo);
935 if (error == NULL)
936 error = repo_error;
938 return error;
941 __dead static void
942 usage_diff(void)
944 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
945 "[object1 object2]\n", getprogname());
946 exit(1);
949 struct print_diff_arg {
950 struct got_repository *repo;
951 struct got_worktree *worktree;
952 int diff_context;
953 };
955 static const struct got_error *
956 print_diff(void *arg, unsigned char status, const char *path,
957 struct got_object_id *id)
959 struct print_diff_arg *a = arg;
960 const struct got_error *err = NULL;
961 struct got_blob_object *blob1 = NULL;
962 FILE *f2 = NULL;
963 char *abspath = NULL;
964 struct stat sb;
966 if (status != GOT_STATUS_MODIFIY)
967 return NULL;
969 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
970 if (err)
971 goto done;
973 if (asprintf(&abspath, "%s/%s",
974 got_worktree_get_root_path(a->worktree), path) == -1) {
975 err = got_error_from_errno();
976 goto done;
979 f2 = fopen(abspath, "r");
980 if (f2 == NULL) {
981 err = got_error_from_errno();
982 goto done;
984 if (lstat(abspath, &sb) == -1) {
985 err = got_error_from_errno();
986 goto done;
989 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
990 stdout);
991 done:
992 if (blob1)
993 got_object_blob_close(blob1);
994 if (f2)
995 fclose(f2);
996 free(abspath);
997 return err;
1000 static const struct got_error *
1001 cmd_diff(int argc, char *argv[])
1003 const struct got_error *error;
1004 struct got_repository *repo = NULL;
1005 struct got_worktree *worktree = NULL;
1006 char *cwd = NULL, *repo_path = NULL;
1007 struct got_object_id *id1 = NULL, *id2 = NULL;
1008 char *id_str1 = NULL, *id_str2 = NULL;
1009 int type1, type2;
1010 int diff_context = 3, ch;
1011 const char *errstr;
1013 #ifndef PROFILE
1014 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1015 NULL) == -1)
1016 err(1, "pledge");
1017 #endif
1019 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1020 switch (ch) {
1021 case 'C':
1022 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1023 if (errstr != NULL)
1024 err(1, "-C option %s", errstr);
1025 break;
1026 case 'r':
1027 repo_path = realpath(optarg, NULL);
1028 if (repo_path == NULL)
1029 err(1, "-r option");
1030 break;
1031 default:
1032 usage();
1033 /* NOTREACHED */
1037 argc -= optind;
1038 argv += optind;
1040 cwd = getcwd(NULL, 0);
1041 if (cwd == NULL) {
1042 error = got_error_from_errno();
1043 goto done;
1045 if (argc == 0) {
1046 if (repo_path)
1047 errx(1,
1048 "-r option can't be used when diffing a work tree");
1049 error = got_worktree_open(&worktree, cwd);
1050 if (error)
1051 goto done;
1052 repo_path = strdup(got_worktree_get_repo_path(worktree));
1053 if (repo_path == NULL)
1054 return got_error_from_errno();
1055 } else if (argc == 2) {
1056 id_str1 = argv[0];
1057 id_str2 = argv[1];
1058 } else
1059 usage_diff();
1061 if (repo_path == NULL) {
1062 repo_path = getcwd(NULL, 0);
1063 if (repo_path == NULL)
1064 return got_error_from_errno();
1067 error = apply_unveil(repo_path,
1068 worktree ? got_worktree_get_root_path(worktree) : NULL);
1069 if (error)
1070 goto done;
1072 error = got_repo_open(&repo, repo_path);
1073 free(repo_path);
1074 if (error != NULL)
1075 goto done;
1077 if (worktree) {
1078 struct print_diff_arg arg;
1079 char *id_str;
1080 error = got_object_id_str(&id_str,
1081 got_worktree_get_base_commit_id(worktree));
1082 if (error)
1083 goto done;
1084 arg.repo = repo;
1085 arg.worktree = worktree;
1086 arg.diff_context = diff_context;
1088 printf("diff %s %s\n", id_str,
1089 got_worktree_get_root_path(worktree));
1090 error = got_worktree_status(worktree, repo, print_diff,
1091 &arg, check_cancelled, NULL);
1092 free(id_str);
1093 goto done;
1096 error = got_object_resolve_id_str(&id1, repo, id_str1);
1097 if (error)
1098 goto done;
1100 error = got_object_resolve_id_str(&id2, repo, id_str2);
1101 if (error)
1102 goto done;
1104 error = got_object_get_type(&type1, repo, id1);
1105 if (error)
1106 goto done;
1108 error = got_object_get_type(&type2, repo, id2);
1109 if (error)
1110 goto done;
1112 if (type1 != type2) {
1113 error = got_error(GOT_ERR_OBJ_TYPE);
1114 goto done;
1117 switch (type1) {
1118 case GOT_OBJ_TYPE_BLOB:
1119 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1120 diff_context, repo, stdout);
1121 break;
1122 case GOT_OBJ_TYPE_TREE:
1123 error = got_diff_objects_as_trees(id1, id2, "", "",
1124 diff_context, repo, stdout);
1125 break;
1126 case GOT_OBJ_TYPE_COMMIT:
1127 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1128 id_str2);
1129 error = got_diff_objects_as_commits(id1, id2, diff_context,
1130 repo, stdout);
1131 break;
1132 default:
1133 error = got_error(GOT_ERR_OBJ_TYPE);
1136 done:
1137 free(id1);
1138 free(id2);
1139 if (worktree)
1140 got_worktree_close(worktree);
1141 if (repo) {
1142 const struct got_error *repo_error;
1143 repo_error = got_repo_close(repo);
1144 if (error == NULL)
1145 error = repo_error;
1147 return error;
1150 __dead static void
1151 usage_blame(void)
1153 fprintf(stderr,
1154 "usage: %s blame [-c commit] [-r repository-path] path\n",
1155 getprogname());
1156 exit(1);
1159 static const struct got_error *
1160 cmd_blame(int argc, char *argv[])
1162 const struct got_error *error;
1163 struct got_repository *repo = NULL;
1164 struct got_worktree *worktree = NULL;
1165 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1166 struct got_object_id *commit_id = NULL;
1167 char *commit_id_str = NULL;
1168 int ch;
1170 #ifndef PROFILE
1171 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1172 NULL) == -1)
1173 err(1, "pledge");
1174 #endif
1176 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1177 switch (ch) {
1178 case 'c':
1179 commit_id_str = optarg;
1180 break;
1181 case 'r':
1182 repo_path = realpath(optarg, NULL);
1183 if (repo_path == NULL)
1184 err(1, "-r option");
1185 break;
1186 default:
1187 usage();
1188 /* NOTREACHED */
1192 argc -= optind;
1193 argv += optind;
1195 if (argc == 1)
1196 path = argv[0];
1197 else
1198 usage_blame();
1200 cwd = getcwd(NULL, 0);
1201 if (cwd == NULL) {
1202 error = got_error_from_errno();
1203 goto done;
1205 if (repo_path == NULL) {
1206 error = got_worktree_open(&worktree, cwd);
1207 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1208 goto done;
1209 else
1210 error = NULL;
1211 if (worktree) {
1212 repo_path =
1213 strdup(got_worktree_get_repo_path(worktree));
1214 if (repo_path == NULL)
1215 error = got_error_from_errno();
1216 if (error)
1217 goto done;
1218 } else {
1219 repo_path = strdup(cwd);
1220 if (repo_path == NULL) {
1221 error = got_error_from_errno();
1222 goto done;
1227 error = apply_unveil(repo_path, NULL);
1228 if (error)
1229 goto done;
1231 error = got_repo_open(&repo, repo_path);
1232 if (error != NULL)
1233 goto done;
1235 if (worktree) {
1236 const char *prefix = got_worktree_get_path_prefix(worktree);
1237 char *p, *worktree_subdir = cwd +
1238 strlen(got_worktree_get_root_path(worktree));
1239 if (asprintf(&p, "%s%s%s%s%s",
1240 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1241 worktree_subdir, worktree_subdir[0] ? "/" : "",
1242 path) == -1) {
1243 error = got_error_from_errno();
1244 goto done;
1246 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1247 free(p);
1248 } else {
1249 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1251 if (error)
1252 goto done;
1254 if (commit_id_str == NULL) {
1255 struct got_reference *head_ref;
1256 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1257 if (error != NULL)
1258 goto done;
1259 error = got_ref_resolve(&commit_id, repo, head_ref);
1260 got_ref_close(head_ref);
1261 if (error != NULL)
1262 goto done;
1263 } else {
1264 error = got_object_resolve_id_str(&commit_id, repo,
1265 commit_id_str);
1266 if (error != NULL)
1267 goto done;
1270 error = got_blame(in_repo_path, commit_id, repo, stdout);
1271 done:
1272 free(in_repo_path);
1273 free(repo_path);
1274 free(cwd);
1275 free(commit_id);
1276 if (worktree)
1277 got_worktree_close(worktree);
1278 if (repo) {
1279 const struct got_error *repo_error;
1280 repo_error = got_repo_close(repo);
1281 if (error == NULL)
1282 error = repo_error;
1284 return error;
1287 __dead static void
1288 usage_tree(void)
1290 fprintf(stderr,
1291 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1292 getprogname());
1293 exit(1);
1296 static void
1297 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1298 const char *root_path)
1300 int is_root_path = (strcmp(path, root_path) == 0);
1302 path += strlen(root_path);
1303 while (path[0] == '/')
1304 path++;
1306 printf("%s%s%s%s%s\n", id ? id : "", path,
1307 is_root_path ? "" : "/",
1308 te->name, S_ISDIR(te->mode) ? "/" : "");
1311 static const struct got_error *
1312 print_tree(const char *path, struct got_object_id *commit_id,
1313 int show_ids, int recurse, const char *root_path,
1314 struct got_repository *repo)
1316 const struct got_error *err = NULL;
1317 struct got_object_id *tree_id = NULL;
1318 struct got_tree_object *tree = NULL;
1319 const struct got_tree_entries *entries;
1320 struct got_tree_entry *te;
1322 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1323 if (err)
1324 goto done;
1326 err = got_object_open_as_tree(&tree, repo, tree_id);
1327 if (err)
1328 goto done;
1329 entries = got_object_tree_get_entries(tree);
1330 te = SIMPLEQ_FIRST(&entries->head);
1331 while (te) {
1332 char *id = NULL;
1334 if (sigint_received || sigpipe_received)
1335 break;
1337 if (show_ids) {
1338 char *id_str;
1339 err = got_object_id_str(&id_str, te->id);
1340 if (err)
1341 goto done;
1342 if (asprintf(&id, "%s ", id_str) == -1) {
1343 err = got_error_from_errno();
1344 free(id_str);
1345 goto done;
1347 free(id_str);
1349 print_entry(te, id, path, root_path);
1350 free(id);
1352 if (recurse && S_ISDIR(te->mode)) {
1353 char *child_path;
1354 if (asprintf(&child_path, "%s%s%s", path,
1355 path[0] == '/' && path[1] == '\0' ? "" : "/",
1356 te->name) == -1) {
1357 err = got_error_from_errno();
1358 goto done;
1360 err = print_tree(child_path, commit_id, show_ids, 1,
1361 root_path, repo);
1362 free(child_path);
1363 if (err)
1364 goto done;
1367 te = SIMPLEQ_NEXT(te, entry);
1369 done:
1370 if (tree)
1371 got_object_tree_close(tree);
1372 free(tree_id);
1373 return err;
1376 static const struct got_error *
1377 cmd_tree(int argc, char *argv[])
1379 const struct got_error *error;
1380 struct got_repository *repo = NULL;
1381 struct got_worktree *worktree = NULL;
1382 const char *path;
1383 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1384 struct got_object_id *commit_id = NULL;
1385 char *commit_id_str = NULL;
1386 int show_ids = 0, recurse = 0;
1387 int ch;
1389 #ifndef PROFILE
1390 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1391 NULL) == -1)
1392 err(1, "pledge");
1393 #endif
1395 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1396 switch (ch) {
1397 case 'c':
1398 commit_id_str = optarg;
1399 break;
1400 case 'r':
1401 repo_path = realpath(optarg, NULL);
1402 if (repo_path == NULL)
1403 err(1, "-r option");
1404 break;
1405 case 'i':
1406 show_ids = 1;
1407 break;
1408 case 'R':
1409 recurse = 1;
1410 break;
1411 default:
1412 usage();
1413 /* NOTREACHED */
1417 argc -= optind;
1418 argv += optind;
1420 if (argc == 1)
1421 path = argv[0];
1422 else if (argc > 1)
1423 usage_tree();
1424 else
1425 path = NULL;
1427 cwd = getcwd(NULL, 0);
1428 if (cwd == NULL) {
1429 error = got_error_from_errno();
1430 goto done;
1432 if (repo_path == NULL) {
1433 error = got_worktree_open(&worktree, cwd);
1434 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1435 goto done;
1436 else
1437 error = NULL;
1438 if (worktree) {
1439 repo_path =
1440 strdup(got_worktree_get_repo_path(worktree));
1441 if (repo_path == NULL)
1442 error = got_error_from_errno();
1443 if (error)
1444 goto done;
1445 } else {
1446 repo_path = strdup(cwd);
1447 if (repo_path == NULL) {
1448 error = got_error_from_errno();
1449 goto done;
1454 error = apply_unveil(repo_path, NULL);
1455 if (error)
1456 goto done;
1458 error = got_repo_open(&repo, repo_path);
1459 if (error != NULL)
1460 goto done;
1462 if (path == NULL) {
1463 if (worktree) {
1464 char *p, *worktree_subdir = cwd +
1465 strlen(got_worktree_get_root_path(worktree));
1466 if (asprintf(&p, "%s/%s",
1467 got_worktree_get_path_prefix(worktree),
1468 worktree_subdir) == -1) {
1469 error = got_error_from_errno();
1470 goto done;
1472 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1473 free(p);
1474 if (error)
1475 goto done;
1476 } else
1477 path = "/";
1479 if (in_repo_path == NULL) {
1480 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1481 if (error != NULL)
1482 goto done;
1485 if (commit_id_str == NULL) {
1486 struct got_reference *head_ref;
1487 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1488 if (error != NULL)
1489 goto done;
1490 error = got_ref_resolve(&commit_id, repo, head_ref);
1491 got_ref_close(head_ref);
1492 if (error != NULL)
1493 goto done;
1494 } else {
1495 error = got_object_resolve_id_str(&commit_id, repo,
1496 commit_id_str);
1497 if (error != NULL)
1498 goto done;
1501 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1502 in_repo_path, repo);
1503 done:
1504 free(in_repo_path);
1505 free(repo_path);
1506 free(cwd);
1507 free(commit_id);
1508 if (worktree)
1509 got_worktree_close(worktree);
1510 if (repo) {
1511 const struct got_error *repo_error;
1512 repo_error = got_repo_close(repo);
1513 if (error == NULL)
1514 error = repo_error;
1516 return error;
1519 __dead static void
1520 usage_status(void)
1522 fprintf(stderr, "usage: %s status [worktree-path]\n", getprogname());
1523 exit(1);
1526 static const struct got_error *
1527 print_status(void *arg, unsigned char status, const char *path,
1528 struct got_object_id *id)
1530 printf("%c %s\n", status, path);
1531 return NULL;
1534 static const struct got_error *
1535 cmd_status(int argc, char *argv[])
1537 const struct got_error *error = NULL;
1538 struct got_repository *repo = NULL;
1539 struct got_worktree *worktree = NULL;
1540 char *worktree_path = NULL;
1541 int ch;
1543 while ((ch = getopt(argc, argv, "")) != -1) {
1544 switch (ch) {
1545 default:
1546 usage();
1547 /* NOTREACHED */
1551 argc -= optind;
1552 argv += optind;
1554 #ifndef PROFILE
1555 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1556 NULL) == -1)
1557 err(1, "pledge");
1558 #endif
1559 if (argc == 0) {
1560 worktree_path = getcwd(NULL, 0);
1561 if (worktree_path == NULL) {
1562 error = got_error_from_errno();
1563 goto done;
1565 } else if (argc == 1) {
1566 worktree_path = realpath(argv[0], NULL);
1567 if (worktree_path == NULL) {
1568 error = got_error_from_errno();
1569 goto done;
1571 } else
1572 usage_status();
1574 error = got_worktree_open(&worktree, worktree_path);
1575 if (error != NULL)
1576 goto done;
1578 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1579 if (error != NULL)
1580 goto done;
1582 error = apply_unveil(got_repo_get_path(repo),
1583 got_worktree_get_root_path(worktree));
1584 if (error)
1585 goto done;
1587 error = got_worktree_status(worktree, repo, print_status, NULL,
1588 check_cancelled, NULL);
1589 done:
1590 free(worktree_path);
1591 return error;