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 fattr flock proc exec sendfd "
318 "unveil", 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 fattr flock proc exec sendfd "
474 "unveil", 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 if (fflush(stdout) != 0 && err == NULL)
694 err = got_error_from_errno();
695 return err;
698 static const struct got_error *
699 print_commits(struct got_object_id *root_id, struct got_repository *repo,
700 char *path, int show_patch, int diff_context, int limit,
701 int first_parent_traversal, struct got_reflist_head *refs)
703 const struct got_error *err;
704 struct got_commit_graph *graph;
706 err = got_commit_graph_open(&graph, root_id, path,
707 first_parent_traversal, repo);
708 if (err)
709 return err;
710 err = got_commit_graph_iter_start(graph, root_id, repo);
711 if (err)
712 goto done;
713 while (1) {
714 struct got_commit_object *commit;
715 struct got_object_id *id;
717 if (sigint_received || sigpipe_received)
718 break;
720 err = got_commit_graph_iter_next(&id, graph);
721 if (err) {
722 if (err->code == GOT_ERR_ITER_COMPLETED) {
723 err = NULL;
724 break;
726 if (err->code != GOT_ERR_ITER_NEED_MORE)
727 break;
728 err = got_commit_graph_fetch_commits(graph, 1, repo);
729 if (err)
730 break;
731 else
732 continue;
734 if (id == NULL)
735 break;
737 err = got_object_open_as_commit(&commit, repo, id);
738 if (err)
739 break;
740 err = print_commit(commit, id, repo, show_patch, diff_context,
741 refs);
742 got_object_commit_close(commit);
743 if (err || (limit && --limit == 0))
744 break;
746 done:
747 got_commit_graph_close(graph);
748 return err;
751 __dead static void
752 usage_log(void)
754 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
755 "[-r repository-path] [path]\n", getprogname());
756 exit(1);
759 static const struct got_error *
760 cmd_log(int argc, char *argv[])
762 const struct got_error *error;
763 struct got_repository *repo = NULL;
764 struct got_worktree *worktree = NULL;
765 struct got_commit_object *commit = NULL;
766 struct got_object_id *id = NULL;
767 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
768 char *start_commit = NULL;
769 int diff_context = 3, ch;
770 int show_patch = 0, limit = 0, first_parent_traversal = 0;
771 const char *errstr;
772 struct got_reflist_head refs;
774 #ifndef PROFILE
775 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
776 NULL)
777 == -1)
778 err(1, "pledge");
779 #endif
781 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
782 switch (ch) {
783 case 'p':
784 show_patch = 1;
785 break;
786 case 'c':
787 start_commit = optarg;
788 break;
789 case 'C':
790 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
791 &errstr);
792 if (errstr != NULL)
793 err(1, "-C option %s", errstr);
794 break;
795 case 'l':
796 limit = strtonum(optarg, 1, INT_MAX, &errstr);
797 if (errstr != NULL)
798 err(1, "-l option %s", errstr);
799 break;
800 case 'f':
801 first_parent_traversal = 1;
802 break;
803 case 'r':
804 repo_path = realpath(optarg, NULL);
805 if (repo_path == NULL)
806 err(1, "-r option");
807 break;
808 default:
809 usage();
810 /* NOTREACHED */
814 argc -= optind;
815 argv += optind;
817 if (argc == 0)
818 path = strdup("");
819 else if (argc == 1)
820 path = strdup(argv[0]);
821 else
822 usage_log();
823 if (path == NULL)
824 return got_error_from_errno();
826 cwd = getcwd(NULL, 0);
827 if (cwd == NULL) {
828 error = got_error_from_errno();
829 goto done;
832 error = got_worktree_open(&worktree, cwd);
833 if (error && error->code != GOT_ERR_NOT_WORKTREE)
834 goto done;
835 error = NULL;
837 repo_path = worktree ?
838 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
839 if (repo_path == NULL) {
840 error = got_error_from_errno();
841 goto done;
844 error = apply_unveil(repo_path, NULL);
845 if (error)
846 goto done;
848 error = got_repo_open(&repo, repo_path);
849 if (error != NULL)
850 goto done;
852 if (start_commit == NULL) {
853 struct got_reference *head_ref;
854 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
855 if (error != NULL)
856 return error;
857 error = got_ref_resolve(&id, repo, head_ref);
858 got_ref_close(head_ref);
859 if (error != NULL)
860 return error;
861 error = got_object_open_as_commit(&commit, repo, id);
862 } else {
863 struct got_reference *ref;
864 error = got_ref_open(&ref, repo, start_commit);
865 if (error == NULL) {
866 int obj_type;
867 error = got_ref_resolve(&id, repo, ref);
868 got_ref_close(ref);
869 if (error != NULL)
870 goto done;
871 error = got_object_get_type(&obj_type, repo, id);
872 if (error != NULL)
873 goto done;
874 if (obj_type == GOT_OBJ_TYPE_TAG) {
875 struct got_tag_object *tag;
876 error = got_object_open_as_tag(&tag, repo, id);
877 if (error != NULL)
878 goto done;
879 if (got_object_tag_get_object_type(tag) !=
880 GOT_OBJ_TYPE_COMMIT) {
881 got_object_tag_close(tag);
882 error = got_error(GOT_ERR_OBJ_TYPE);
883 goto done;
885 free(id);
886 id = got_object_id_dup(
887 got_object_tag_get_object_id(tag));
888 if (id == NULL)
889 error = got_error_from_errno();
890 got_object_tag_close(tag);
891 if (error)
892 goto done;
893 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
894 error = got_error(GOT_ERR_OBJ_TYPE);
895 goto done;
897 error = got_object_open_as_commit(&commit, repo, id);
898 if (error != NULL)
899 goto done;
901 if (commit == NULL) {
902 error = got_object_resolve_id_str(&id, repo,
903 start_commit);
904 if (error != NULL)
905 return error;
908 if (error != NULL)
909 goto done;
911 error = got_repo_map_path(&in_repo_path, repo, path, 1);
912 if (error != NULL)
913 goto done;
914 if (in_repo_path) {
915 free(path);
916 path = in_repo_path;
919 SIMPLEQ_INIT(&refs);
920 error = got_ref_list(&refs, repo);
921 if (error)
922 goto done;
924 error = print_commits(id, repo, path, show_patch,
925 diff_context, limit, first_parent_traversal, &refs);
926 done:
927 free(path);
928 free(repo_path);
929 free(cwd);
930 free(id);
931 if (worktree)
932 got_worktree_close(worktree);
933 if (repo) {
934 const struct got_error *repo_error;
935 repo_error = got_repo_close(repo);
936 if (error == NULL)
937 error = repo_error;
939 return error;
942 __dead static void
943 usage_diff(void)
945 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
946 "[object1 object2 | path]\n", getprogname());
947 exit(1);
950 struct print_diff_arg {
951 struct got_repository *repo;
952 struct got_worktree *worktree;
953 int diff_context;
954 const char *id_str;
955 int header_shown;
956 };
958 static const struct got_error *
959 print_diff(void *arg, unsigned char status, const char *path,
960 struct got_object_id *id)
962 struct print_diff_arg *a = arg;
963 const struct got_error *err = NULL;
964 struct got_blob_object *blob1 = NULL;
965 FILE *f2 = NULL;
966 char *abspath = NULL;
967 struct stat sb;
969 if (status != GOT_STATUS_MODIFY)
970 return NULL;
972 if (!a->header_shown) {
973 printf("diff %s %s\n", a->id_str,
974 got_worktree_get_root_path(a->worktree));
975 a->header_shown = 1;
978 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
979 if (err)
980 goto done;
982 if (asprintf(&abspath, "%s/%s",
983 got_worktree_get_root_path(a->worktree), path) == -1) {
984 err = got_error_from_errno();
985 goto done;
988 f2 = fopen(abspath, "r");
989 if (f2 == NULL) {
990 err = got_error_from_errno();
991 goto done;
993 if (lstat(abspath, &sb) == -1) {
994 err = got_error_from_errno();
995 goto done;
998 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
999 stdout);
1000 done:
1001 if (blob1)
1002 got_object_blob_close(blob1);
1003 if (f2)
1004 fclose(f2);
1005 free(abspath);
1006 return err;
1009 static const struct got_error *
1010 get_status_path(char **status_path, struct got_worktree *worktree,
1011 const char *arg)
1013 const struct got_error *err = NULL;
1014 char *resolved, *path = NULL;
1015 size_t len;
1017 *status_path = NULL;
1019 resolved = realpath(arg, NULL);
1020 if (resolved == NULL)
1021 return got_error_from_errno();
1023 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1024 strlen(got_worktree_get_root_path(worktree)))) {
1025 err = got_error(GOT_ERR_BAD_PATH);
1026 goto done;
1029 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1030 if (path == NULL) {
1031 err = got_error_from_errno();
1032 goto done;
1035 /* XXX status walk can't deal with trailing slash! */
1036 len = strlen(path);
1037 while (path[len - 1] == '/') {
1038 path[len - 1] = '\0';
1039 len--;
1041 done:
1042 free(resolved);
1043 if (err == NULL)
1044 *status_path = path;
1045 else
1046 free(path);
1047 return err;
1050 static const struct got_error *
1051 cmd_diff(int argc, char *argv[])
1053 const struct got_error *error;
1054 struct got_repository *repo = NULL;
1055 struct got_worktree *worktree = NULL;
1056 char *cwd = NULL, *repo_path = NULL;
1057 struct got_object_id *id1 = NULL, *id2 = NULL;
1058 char *id_str1 = NULL, *id_str2 = NULL;
1059 int type1, type2;
1060 int diff_context = 3, ch;
1061 const char *errstr;
1062 char *path = NULL;
1064 #ifndef PROFILE
1065 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1066 NULL) == -1)
1067 err(1, "pledge");
1068 #endif
1070 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1071 switch (ch) {
1072 case 'C':
1073 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1074 if (errstr != NULL)
1075 err(1, "-C option %s", errstr);
1076 break;
1077 case 'r':
1078 repo_path = realpath(optarg, NULL);
1079 if (repo_path == NULL)
1080 err(1, "-r option");
1081 break;
1082 default:
1083 usage();
1084 /* NOTREACHED */
1088 argc -= optind;
1089 argv += optind;
1091 cwd = getcwd(NULL, 0);
1092 if (cwd == NULL) {
1093 error = got_error_from_errno();
1094 goto done;
1096 error = got_worktree_open(&worktree, cwd);
1097 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1098 goto done;
1099 if (argc <= 1) {
1100 if (worktree == NULL) {
1101 error = got_error(GOT_ERR_NOT_WORKTREE);
1102 goto done;
1104 if (repo_path)
1105 errx(1,
1106 "-r option can't be used when diffing a work tree");
1107 repo_path = strdup(got_worktree_get_repo_path(worktree));
1108 if (repo_path == NULL) {
1109 error = got_error_from_errno();
1110 goto done;
1112 if (argc == 1) {
1113 error = get_status_path(&path, worktree, argv[0]);
1114 if (error)
1115 goto done;
1116 } else {
1117 path = strdup("");
1118 if (path == NULL) {
1119 error = got_error_from_errno();
1120 goto done;
1123 } else if (argc == 2) {
1124 id_str1 = argv[0];
1125 id_str2 = argv[1];
1126 } else
1127 usage_diff();
1129 if (repo_path == NULL) {
1130 repo_path = getcwd(NULL, 0);
1131 if (repo_path == NULL)
1132 return got_error_from_errno();
1135 error = apply_unveil(repo_path,
1136 worktree ? got_worktree_get_root_path(worktree) : NULL);
1137 if (error)
1138 goto done;
1140 error = got_repo_open(&repo, repo_path);
1141 free(repo_path);
1142 if (error != NULL)
1143 goto done;
1145 if (worktree) {
1146 struct print_diff_arg arg;
1147 char *id_str;
1148 error = got_object_id_str(&id_str,
1149 got_worktree_get_base_commit_id(worktree));
1150 if (error)
1151 goto done;
1152 arg.repo = repo;
1153 arg.worktree = worktree;
1154 arg.diff_context = diff_context;
1155 arg.id_str = id_str;
1156 arg.header_shown = 0;
1158 error = got_worktree_status(worktree, path, repo, print_diff,
1159 &arg, check_cancelled, NULL);
1160 free(id_str);
1161 goto done;
1164 error = got_object_resolve_id_str(&id1, repo, id_str1);
1165 if (error)
1166 goto done;
1168 error = got_object_resolve_id_str(&id2, repo, id_str2);
1169 if (error)
1170 goto done;
1172 error = got_object_get_type(&type1, repo, id1);
1173 if (error)
1174 goto done;
1176 error = got_object_get_type(&type2, repo, id2);
1177 if (error)
1178 goto done;
1180 if (type1 != type2) {
1181 error = got_error(GOT_ERR_OBJ_TYPE);
1182 goto done;
1185 switch (type1) {
1186 case GOT_OBJ_TYPE_BLOB:
1187 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1188 diff_context, repo, stdout);
1189 break;
1190 case GOT_OBJ_TYPE_TREE:
1191 error = got_diff_objects_as_trees(id1, id2, "", "",
1192 diff_context, repo, stdout);
1193 break;
1194 case GOT_OBJ_TYPE_COMMIT:
1195 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1196 id_str2);
1197 error = got_diff_objects_as_commits(id1, id2, diff_context,
1198 repo, stdout);
1199 break;
1200 default:
1201 error = got_error(GOT_ERR_OBJ_TYPE);
1204 done:
1205 free(id1);
1206 free(id2);
1207 free(path);
1208 if (worktree)
1209 got_worktree_close(worktree);
1210 if (repo) {
1211 const struct got_error *repo_error;
1212 repo_error = got_repo_close(repo);
1213 if (error == NULL)
1214 error = repo_error;
1216 return error;
1219 __dead static void
1220 usage_blame(void)
1222 fprintf(stderr,
1223 "usage: %s blame [-c commit] [-r repository-path] path\n",
1224 getprogname());
1225 exit(1);
1228 static const struct got_error *
1229 cmd_blame(int argc, char *argv[])
1231 const struct got_error *error;
1232 struct got_repository *repo = NULL;
1233 struct got_worktree *worktree = NULL;
1234 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1235 struct got_object_id *commit_id = NULL;
1236 char *commit_id_str = NULL;
1237 int ch;
1239 #ifndef PROFILE
1240 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1241 NULL) == -1)
1242 err(1, "pledge");
1243 #endif
1245 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1246 switch (ch) {
1247 case 'c':
1248 commit_id_str = optarg;
1249 break;
1250 case 'r':
1251 repo_path = realpath(optarg, NULL);
1252 if (repo_path == NULL)
1253 err(1, "-r option");
1254 break;
1255 default:
1256 usage();
1257 /* NOTREACHED */
1261 argc -= optind;
1262 argv += optind;
1264 if (argc == 1)
1265 path = argv[0];
1266 else
1267 usage_blame();
1269 cwd = getcwd(NULL, 0);
1270 if (cwd == NULL) {
1271 error = got_error_from_errno();
1272 goto done;
1274 if (repo_path == NULL) {
1275 error = got_worktree_open(&worktree, cwd);
1276 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1277 goto done;
1278 else
1279 error = NULL;
1280 if (worktree) {
1281 repo_path =
1282 strdup(got_worktree_get_repo_path(worktree));
1283 if (repo_path == NULL)
1284 error = got_error_from_errno();
1285 if (error)
1286 goto done;
1287 } else {
1288 repo_path = strdup(cwd);
1289 if (repo_path == NULL) {
1290 error = got_error_from_errno();
1291 goto done;
1296 error = apply_unveil(repo_path, NULL);
1297 if (error)
1298 goto done;
1300 error = got_repo_open(&repo, repo_path);
1301 if (error != NULL)
1302 goto done;
1304 if (worktree) {
1305 const char *prefix = got_worktree_get_path_prefix(worktree);
1306 char *p, *worktree_subdir = cwd +
1307 strlen(got_worktree_get_root_path(worktree));
1308 if (asprintf(&p, "%s%s%s%s%s",
1309 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1310 worktree_subdir, worktree_subdir[0] ? "/" : "",
1311 path) == -1) {
1312 error = got_error_from_errno();
1313 goto done;
1315 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1316 free(p);
1317 } else {
1318 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1320 if (error)
1321 goto done;
1323 if (commit_id_str == NULL) {
1324 struct got_reference *head_ref;
1325 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1326 if (error != NULL)
1327 goto done;
1328 error = got_ref_resolve(&commit_id, repo, head_ref);
1329 got_ref_close(head_ref);
1330 if (error != NULL)
1331 goto done;
1332 } else {
1333 error = got_object_resolve_id_str(&commit_id, repo,
1334 commit_id_str);
1335 if (error != NULL)
1336 goto done;
1339 error = got_blame(in_repo_path, commit_id, repo, stdout);
1340 done:
1341 free(in_repo_path);
1342 free(repo_path);
1343 free(cwd);
1344 free(commit_id);
1345 if (worktree)
1346 got_worktree_close(worktree);
1347 if (repo) {
1348 const struct got_error *repo_error;
1349 repo_error = got_repo_close(repo);
1350 if (error == NULL)
1351 error = repo_error;
1353 return error;
1356 __dead static void
1357 usage_tree(void)
1359 fprintf(stderr,
1360 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1361 getprogname());
1362 exit(1);
1365 static void
1366 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1367 const char *root_path)
1369 int is_root_path = (strcmp(path, root_path) == 0);
1371 path += strlen(root_path);
1372 while (path[0] == '/')
1373 path++;
1375 printf("%s%s%s%s%s\n", id ? id : "", path,
1376 is_root_path ? "" : "/", te->name,
1377 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1380 static const struct got_error *
1381 print_tree(const char *path, struct got_object_id *commit_id,
1382 int show_ids, int recurse, const char *root_path,
1383 struct got_repository *repo)
1385 const struct got_error *err = NULL;
1386 struct got_object_id *tree_id = NULL;
1387 struct got_tree_object *tree = NULL;
1388 const struct got_tree_entries *entries;
1389 struct got_tree_entry *te;
1391 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1392 if (err)
1393 goto done;
1395 err = got_object_open_as_tree(&tree, repo, tree_id);
1396 if (err)
1397 goto done;
1398 entries = got_object_tree_get_entries(tree);
1399 te = SIMPLEQ_FIRST(&entries->head);
1400 while (te) {
1401 char *id = NULL;
1403 if (sigint_received || sigpipe_received)
1404 break;
1406 if (show_ids) {
1407 char *id_str;
1408 err = got_object_id_str(&id_str, te->id);
1409 if (err)
1410 goto done;
1411 if (asprintf(&id, "%s ", id_str) == -1) {
1412 err = got_error_from_errno();
1413 free(id_str);
1414 goto done;
1416 free(id_str);
1418 print_entry(te, id, path, root_path);
1419 free(id);
1421 if (recurse && S_ISDIR(te->mode)) {
1422 char *child_path;
1423 if (asprintf(&child_path, "%s%s%s", path,
1424 path[0] == '/' && path[1] == '\0' ? "" : "/",
1425 te->name) == -1) {
1426 err = got_error_from_errno();
1427 goto done;
1429 err = print_tree(child_path, commit_id, show_ids, 1,
1430 root_path, repo);
1431 free(child_path);
1432 if (err)
1433 goto done;
1436 te = SIMPLEQ_NEXT(te, entry);
1438 done:
1439 if (tree)
1440 got_object_tree_close(tree);
1441 free(tree_id);
1442 return err;
1445 static const struct got_error *
1446 cmd_tree(int argc, char *argv[])
1448 const struct got_error *error;
1449 struct got_repository *repo = NULL;
1450 struct got_worktree *worktree = NULL;
1451 const char *path;
1452 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1453 struct got_object_id *commit_id = NULL;
1454 char *commit_id_str = NULL;
1455 int show_ids = 0, recurse = 0;
1456 int ch;
1458 #ifndef PROFILE
1459 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1460 NULL) == -1)
1461 err(1, "pledge");
1462 #endif
1464 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1465 switch (ch) {
1466 case 'c':
1467 commit_id_str = optarg;
1468 break;
1469 case 'r':
1470 repo_path = realpath(optarg, NULL);
1471 if (repo_path == NULL)
1472 err(1, "-r option");
1473 break;
1474 case 'i':
1475 show_ids = 1;
1476 break;
1477 case 'R':
1478 recurse = 1;
1479 break;
1480 default:
1481 usage();
1482 /* NOTREACHED */
1486 argc -= optind;
1487 argv += optind;
1489 if (argc == 1)
1490 path = argv[0];
1491 else if (argc > 1)
1492 usage_tree();
1493 else
1494 path = NULL;
1496 cwd = getcwd(NULL, 0);
1497 if (cwd == NULL) {
1498 error = got_error_from_errno();
1499 goto done;
1501 if (repo_path == NULL) {
1502 error = got_worktree_open(&worktree, cwd);
1503 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1504 goto done;
1505 else
1506 error = NULL;
1507 if (worktree) {
1508 repo_path =
1509 strdup(got_worktree_get_repo_path(worktree));
1510 if (repo_path == NULL)
1511 error = got_error_from_errno();
1512 if (error)
1513 goto done;
1514 } else {
1515 repo_path = strdup(cwd);
1516 if (repo_path == NULL) {
1517 error = got_error_from_errno();
1518 goto done;
1523 error = apply_unveil(repo_path, NULL);
1524 if (error)
1525 goto done;
1527 error = got_repo_open(&repo, repo_path);
1528 if (error != NULL)
1529 goto done;
1531 if (path == NULL) {
1532 if (worktree) {
1533 char *p, *worktree_subdir = cwd +
1534 strlen(got_worktree_get_root_path(worktree));
1535 if (asprintf(&p, "%s/%s",
1536 got_worktree_get_path_prefix(worktree),
1537 worktree_subdir) == -1) {
1538 error = got_error_from_errno();
1539 goto done;
1541 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1542 free(p);
1543 if (error)
1544 goto done;
1545 } else
1546 path = "/";
1548 if (in_repo_path == NULL) {
1549 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1550 if (error != NULL)
1551 goto done;
1554 if (commit_id_str == NULL) {
1555 struct got_reference *head_ref;
1556 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1557 if (error != NULL)
1558 goto done;
1559 error = got_ref_resolve(&commit_id, repo, head_ref);
1560 got_ref_close(head_ref);
1561 if (error != NULL)
1562 goto done;
1563 } else {
1564 error = got_object_resolve_id_str(&commit_id, repo,
1565 commit_id_str);
1566 if (error != NULL)
1567 goto done;
1570 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1571 in_repo_path, repo);
1572 done:
1573 free(in_repo_path);
1574 free(repo_path);
1575 free(cwd);
1576 free(commit_id);
1577 if (worktree)
1578 got_worktree_close(worktree);
1579 if (repo) {
1580 const struct got_error *repo_error;
1581 repo_error = got_repo_close(repo);
1582 if (error == NULL)
1583 error = repo_error;
1585 return error;
1588 __dead static void
1589 usage_status(void)
1591 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1592 exit(1);
1595 static const struct got_error *
1596 print_status(void *arg, unsigned char status, const char *path,
1597 struct got_object_id *id)
1599 printf("%c %s\n", status, path);
1600 return NULL;
1603 static const struct got_error *
1604 cmd_status(int argc, char *argv[])
1606 const struct got_error *error = NULL;
1607 struct got_repository *repo = NULL;
1608 struct got_worktree *worktree = NULL;
1609 char *cwd = NULL, *path = NULL;
1610 int ch;
1612 while ((ch = getopt(argc, argv, "")) != -1) {
1613 switch (ch) {
1614 default:
1615 usage();
1616 /* NOTREACHED */
1620 argc -= optind;
1621 argv += optind;
1623 #ifndef PROFILE
1624 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1625 NULL) == -1)
1626 err(1, "pledge");
1627 #endif
1628 cwd = getcwd(NULL, 0);
1629 if (cwd == NULL) {
1630 error = got_error_from_errno();
1631 goto done;
1634 error = got_worktree_open(&worktree, cwd);
1635 if (error != NULL)
1636 goto done;
1638 if (argc == 0) {
1639 path = strdup("");
1640 if (path == NULL) {
1641 error = got_error_from_errno();
1642 goto done;
1644 } else if (argc == 1) {
1645 error = get_status_path(&path, worktree, argv[0]);
1646 if (error)
1647 goto done;
1648 } else
1649 usage_status();
1651 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1652 if (error != NULL)
1653 goto done;
1655 error = apply_unveil(got_repo_get_path(repo),
1656 got_worktree_get_root_path(worktree));
1657 if (error)
1658 goto done;
1660 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1661 check_cancelled, NULL);
1662 done:
1663 free(cwd);
1664 free(path);
1665 return error;