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_checkout();
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_update();
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 id_str = NULL;
654 free(refs_str);
655 refs_str = NULL;
656 printf("from: %s\n", got_object_commit_get_author(commit));
657 committer_time = got_object_commit_get_committer_time(commit);
658 datestr = get_datestr(&committer_time, datebuf);
659 printf("date: %s UTC\n", datestr);
660 author = got_object_commit_get_author(commit);
661 committer = got_object_commit_get_committer(commit);
662 if (strcmp(author, committer) != 0)
663 printf("via: %s\n", committer);
664 if (got_object_commit_get_nparents(commit) > 1) {
665 const struct got_object_id_queue *parent_ids;
666 struct got_object_qid *qid;
667 int n = 1;
668 parent_ids = got_object_commit_get_parent_ids(commit);
669 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
670 err = got_object_id_str(&id_str, qid->id);
671 if (err)
672 return err;
673 printf("parent %d: %s\n", n++, id_str);
674 free(id_str);
678 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
679 if (logmsg0 == NULL)
680 return got_error_from_errno();
682 logmsg = logmsg0;
683 do {
684 line = strsep(&logmsg, "\n");
685 if (line)
686 printf(" %s\n", line);
687 } while (line);
688 free(logmsg0);
690 if (show_patch) {
691 err = print_patch(commit, id, diff_context, repo);
692 if (err == 0)
693 printf("\n");
696 if (fflush(stdout) != 0 && err == NULL)
697 err = got_error_from_errno();
698 return err;
701 static const struct got_error *
702 print_commits(struct got_object_id *root_id, struct got_repository *repo,
703 char *path, int show_patch, int diff_context, int limit,
704 int first_parent_traversal, struct got_reflist_head *refs)
706 const struct got_error *err;
707 struct got_commit_graph *graph;
709 err = got_commit_graph_open(&graph, root_id, path,
710 first_parent_traversal, repo);
711 if (err)
712 return err;
713 err = got_commit_graph_iter_start(graph, root_id, repo);
714 if (err)
715 goto done;
716 while (1) {
717 struct got_commit_object *commit;
718 struct got_object_id *id;
720 if (sigint_received || sigpipe_received)
721 break;
723 err = got_commit_graph_iter_next(&id, graph);
724 if (err) {
725 if (err->code == GOT_ERR_ITER_COMPLETED) {
726 err = NULL;
727 break;
729 if (err->code != GOT_ERR_ITER_NEED_MORE)
730 break;
731 err = got_commit_graph_fetch_commits(graph, 1, repo);
732 if (err)
733 break;
734 else
735 continue;
737 if (id == NULL)
738 break;
740 err = got_object_open_as_commit(&commit, repo, id);
741 if (err)
742 break;
743 err = print_commit(commit, id, repo, show_patch, diff_context,
744 refs);
745 got_object_commit_close(commit);
746 if (err || (limit && --limit == 0))
747 break;
749 done:
750 got_commit_graph_close(graph);
751 return err;
754 __dead static void
755 usage_log(void)
757 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
758 "[-r repository-path] [path]\n", getprogname());
759 exit(1);
762 static const struct got_error *
763 cmd_log(int argc, char *argv[])
765 const struct got_error *error;
766 struct got_repository *repo = NULL;
767 struct got_worktree *worktree = NULL;
768 struct got_commit_object *commit = NULL;
769 struct got_object_id *id = NULL;
770 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
771 char *start_commit = NULL;
772 int diff_context = 3, ch;
773 int show_patch = 0, limit = 0, first_parent_traversal = 0;
774 const char *errstr;
775 struct got_reflist_head refs;
777 #ifndef PROFILE
778 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
779 NULL)
780 == -1)
781 err(1, "pledge");
782 #endif
784 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
785 switch (ch) {
786 case 'p':
787 show_patch = 1;
788 break;
789 case 'c':
790 start_commit = optarg;
791 break;
792 case 'C':
793 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
794 &errstr);
795 if (errstr != NULL)
796 err(1, "-C option %s", errstr);
797 break;
798 case 'l':
799 limit = strtonum(optarg, 1, INT_MAX, &errstr);
800 if (errstr != NULL)
801 err(1, "-l option %s", errstr);
802 break;
803 case 'f':
804 first_parent_traversal = 1;
805 break;
806 case 'r':
807 repo_path = realpath(optarg, NULL);
808 if (repo_path == NULL)
809 err(1, "-r option");
810 break;
811 default:
812 usage_log();
813 /* NOTREACHED */
817 argc -= optind;
818 argv += optind;
820 if (argc == 0)
821 path = strdup("");
822 else if (argc == 1)
823 path = strdup(argv[0]);
824 else
825 usage_log();
826 if (path == NULL)
827 return got_error_from_errno();
829 cwd = getcwd(NULL, 0);
830 if (cwd == NULL) {
831 error = got_error_from_errno();
832 goto done;
835 error = got_worktree_open(&worktree, cwd);
836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
837 goto done;
838 error = NULL;
840 repo_path = worktree ?
841 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
842 if (repo_path == NULL) {
843 error = got_error_from_errno();
844 goto done;
847 error = apply_unveil(repo_path,
848 worktree ? got_worktree_get_root_path(worktree) : NULL);
849 if (error)
850 goto done;
852 error = got_repo_open(&repo, repo_path);
853 if (error != NULL)
854 goto done;
856 if (start_commit == NULL) {
857 struct got_reference *head_ref;
858 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
859 if (error != NULL)
860 return error;
861 error = got_ref_resolve(&id, repo, head_ref);
862 got_ref_close(head_ref);
863 if (error != NULL)
864 return error;
865 error = got_object_open_as_commit(&commit, repo, id);
866 } else {
867 struct got_reference *ref;
868 error = got_ref_open(&ref, repo, start_commit);
869 if (error == NULL) {
870 int obj_type;
871 error = got_ref_resolve(&id, repo, ref);
872 got_ref_close(ref);
873 if (error != NULL)
874 goto done;
875 error = got_object_get_type(&obj_type, repo, id);
876 if (error != NULL)
877 goto done;
878 if (obj_type == GOT_OBJ_TYPE_TAG) {
879 struct got_tag_object *tag;
880 error = got_object_open_as_tag(&tag, repo, id);
881 if (error != NULL)
882 goto done;
883 if (got_object_tag_get_object_type(tag) !=
884 GOT_OBJ_TYPE_COMMIT) {
885 got_object_tag_close(tag);
886 error = got_error(GOT_ERR_OBJ_TYPE);
887 goto done;
889 free(id);
890 id = got_object_id_dup(
891 got_object_tag_get_object_id(tag));
892 if (id == NULL)
893 error = got_error_from_errno();
894 got_object_tag_close(tag);
895 if (error)
896 goto done;
897 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
898 error = got_error(GOT_ERR_OBJ_TYPE);
899 goto done;
901 error = got_object_open_as_commit(&commit, repo, id);
902 if (error != NULL)
903 goto done;
905 if (commit == NULL) {
906 error = got_object_resolve_id_str(&id, repo,
907 start_commit);
908 if (error != NULL)
909 return error;
912 if (error != NULL)
913 goto done;
915 error = got_repo_map_path(&in_repo_path, repo, path, 1);
916 if (error != NULL)
917 goto done;
918 if (in_repo_path) {
919 free(path);
920 path = in_repo_path;
923 SIMPLEQ_INIT(&refs);
924 error = got_ref_list(&refs, repo);
925 if (error)
926 goto done;
928 error = print_commits(id, repo, path, show_patch,
929 diff_context, limit, first_parent_traversal, &refs);
930 done:
931 free(path);
932 free(repo_path);
933 free(cwd);
934 free(id);
935 if (worktree)
936 got_worktree_close(worktree);
937 if (repo) {
938 const struct got_error *repo_error;
939 repo_error = got_repo_close(repo);
940 if (error == NULL)
941 error = repo_error;
943 return error;
946 __dead static void
947 usage_diff(void)
949 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
950 "[object1 object2 | path]\n", getprogname());
951 exit(1);
954 struct print_diff_arg {
955 struct got_repository *repo;
956 struct got_worktree *worktree;
957 int diff_context;
958 const char *id_str;
959 int header_shown;
960 };
962 static const struct got_error *
963 print_diff(void *arg, unsigned char status, const char *path,
964 struct got_object_id *id)
966 struct print_diff_arg *a = arg;
967 const struct got_error *err = NULL;
968 struct got_blob_object *blob1 = NULL;
969 FILE *f2 = NULL;
970 char *abspath = NULL;
971 struct stat sb;
973 if (status != GOT_STATUS_MODIFY)
974 return NULL;
976 if (!a->header_shown) {
977 printf("diff %s %s\n", a->id_str,
978 got_worktree_get_root_path(a->worktree));
979 a->header_shown = 1;
982 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
983 if (err)
984 goto done;
986 if (asprintf(&abspath, "%s/%s",
987 got_worktree_get_root_path(a->worktree), path) == -1) {
988 err = got_error_from_errno();
989 goto done;
992 f2 = fopen(abspath, "r");
993 if (f2 == NULL) {
994 err = got_error_from_errno();
995 goto done;
997 if (lstat(abspath, &sb) == -1) {
998 err = got_error_from_errno();
999 goto done;
1002 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1003 stdout);
1004 done:
1005 if (blob1)
1006 got_object_blob_close(blob1);
1007 if (f2 && fclose(f2) != 0 && err == NULL)
1008 err = got_error_from_errno();
1009 free(abspath);
1010 return err;
1013 static const struct got_error *
1014 get_status_path(char **status_path, struct got_worktree *worktree,
1015 const char *arg)
1017 const struct got_error *err = NULL;
1018 char *resolved, *path = NULL;
1019 size_t len;
1021 *status_path = NULL;
1023 resolved = realpath(arg, NULL);
1024 if (resolved == NULL)
1025 return got_error_from_errno();
1027 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1028 strlen(got_worktree_get_root_path(worktree)))) {
1029 err = got_error(GOT_ERR_BAD_PATH);
1030 goto done;
1033 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1034 if (path == NULL) {
1035 err = got_error_from_errno();
1036 goto done;
1039 /* XXX status walk can't deal with trailing slash! */
1040 len = strlen(path);
1041 while (path[len - 1] == '/') {
1042 path[len - 1] = '\0';
1043 len--;
1045 done:
1046 free(resolved);
1047 if (err == NULL)
1048 *status_path = path;
1049 else
1050 free(path);
1051 return err;
1054 static const struct got_error *
1055 cmd_diff(int argc, char *argv[])
1057 const struct got_error *error;
1058 struct got_repository *repo = NULL;
1059 struct got_worktree *worktree = NULL;
1060 char *cwd = NULL, *repo_path = NULL;
1061 struct got_object_id *id1 = NULL, *id2 = NULL;
1062 char *id_str1 = NULL, *id_str2 = NULL;
1063 int type1, type2;
1064 int diff_context = 3, ch;
1065 const char *errstr;
1066 char *path = NULL;
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 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1078 if (errstr != NULL)
1079 err(1, "-C option %s", errstr);
1080 break;
1081 case 'r':
1082 repo_path = realpath(optarg, NULL);
1083 if (repo_path == NULL)
1084 err(1, "-r option");
1085 break;
1086 default:
1087 usage_diff();
1088 /* NOTREACHED */
1092 argc -= optind;
1093 argv += optind;
1095 cwd = getcwd(NULL, 0);
1096 if (cwd == NULL) {
1097 error = got_error_from_errno();
1098 goto done;
1100 error = got_worktree_open(&worktree, cwd);
1101 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1102 goto done;
1103 if (argc <= 1) {
1104 if (worktree == NULL) {
1105 error = got_error(GOT_ERR_NOT_WORKTREE);
1106 goto done;
1108 if (repo_path)
1109 errx(1,
1110 "-r option can't be used when diffing a work tree");
1111 repo_path = strdup(got_worktree_get_repo_path(worktree));
1112 if (repo_path == NULL) {
1113 error = got_error_from_errno();
1114 goto done;
1116 if (argc == 1) {
1117 error = get_status_path(&path, worktree, argv[0]);
1118 if (error)
1119 goto done;
1120 } else {
1121 path = strdup("");
1122 if (path == NULL) {
1123 error = got_error_from_errno();
1124 goto done;
1127 } else if (argc == 2) {
1128 id_str1 = argv[0];
1129 id_str2 = argv[1];
1130 } else
1131 usage_diff();
1133 if (repo_path == NULL) {
1134 repo_path = getcwd(NULL, 0);
1135 if (repo_path == NULL)
1136 return got_error_from_errno();
1139 error = apply_unveil(repo_path,
1140 worktree ? got_worktree_get_root_path(worktree) : NULL);
1141 if (error)
1142 goto done;
1144 error = got_repo_open(&repo, repo_path);
1145 free(repo_path);
1146 if (error != NULL)
1147 goto done;
1149 if (worktree) {
1150 struct print_diff_arg arg;
1151 char *id_str;
1152 error = got_object_id_str(&id_str,
1153 got_worktree_get_base_commit_id(worktree));
1154 if (error)
1155 goto done;
1156 arg.repo = repo;
1157 arg.worktree = worktree;
1158 arg.diff_context = diff_context;
1159 arg.id_str = id_str;
1160 arg.header_shown = 0;
1162 error = got_worktree_status(worktree, path, repo, print_diff,
1163 &arg, check_cancelled, NULL);
1164 free(id_str);
1165 goto done;
1168 error = got_object_resolve_id_str(&id1, repo, id_str1);
1169 if (error)
1170 goto done;
1172 error = got_object_resolve_id_str(&id2, repo, id_str2);
1173 if (error)
1174 goto done;
1176 error = got_object_get_type(&type1, repo, id1);
1177 if (error)
1178 goto done;
1180 error = got_object_get_type(&type2, repo, id2);
1181 if (error)
1182 goto done;
1184 if (type1 != type2) {
1185 error = got_error(GOT_ERR_OBJ_TYPE);
1186 goto done;
1189 switch (type1) {
1190 case GOT_OBJ_TYPE_BLOB:
1191 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1192 diff_context, repo, stdout);
1193 break;
1194 case GOT_OBJ_TYPE_TREE:
1195 error = got_diff_objects_as_trees(id1, id2, "", "",
1196 diff_context, repo, stdout);
1197 break;
1198 case GOT_OBJ_TYPE_COMMIT:
1199 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1200 id_str2);
1201 error = got_diff_objects_as_commits(id1, id2, diff_context,
1202 repo, stdout);
1203 break;
1204 default:
1205 error = got_error(GOT_ERR_OBJ_TYPE);
1208 done:
1209 free(id1);
1210 free(id2);
1211 free(path);
1212 if (worktree)
1213 got_worktree_close(worktree);
1214 if (repo) {
1215 const struct got_error *repo_error;
1216 repo_error = got_repo_close(repo);
1217 if (error == NULL)
1218 error = repo_error;
1220 return error;
1223 __dead static void
1224 usage_blame(void)
1226 fprintf(stderr,
1227 "usage: %s blame [-c commit] [-r repository-path] path\n",
1228 getprogname());
1229 exit(1);
1232 static const struct got_error *
1233 cmd_blame(int argc, char *argv[])
1235 const struct got_error *error;
1236 struct got_repository *repo = NULL;
1237 struct got_worktree *worktree = NULL;
1238 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1239 struct got_object_id *commit_id = NULL;
1240 char *commit_id_str = NULL;
1241 int ch;
1243 #ifndef PROFILE
1244 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1245 NULL) == -1)
1246 err(1, "pledge");
1247 #endif
1249 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1250 switch (ch) {
1251 case 'c':
1252 commit_id_str = optarg;
1253 break;
1254 case 'r':
1255 repo_path = realpath(optarg, NULL);
1256 if (repo_path == NULL)
1257 err(1, "-r option");
1258 break;
1259 default:
1260 usage_blame();
1261 /* NOTREACHED */
1265 argc -= optind;
1266 argv += optind;
1268 if (argc == 1)
1269 path = argv[0];
1270 else
1271 usage_blame();
1273 cwd = getcwd(NULL, 0);
1274 if (cwd == NULL) {
1275 error = got_error_from_errno();
1276 goto done;
1278 if (repo_path == NULL) {
1279 error = got_worktree_open(&worktree, cwd);
1280 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1281 goto done;
1282 else
1283 error = NULL;
1284 if (worktree) {
1285 repo_path =
1286 strdup(got_worktree_get_repo_path(worktree));
1287 if (repo_path == NULL)
1288 error = got_error_from_errno();
1289 if (error)
1290 goto done;
1291 } else {
1292 repo_path = strdup(cwd);
1293 if (repo_path == NULL) {
1294 error = got_error_from_errno();
1295 goto done;
1300 error = apply_unveil(repo_path, NULL);
1301 if (error)
1302 goto done;
1304 error = got_repo_open(&repo, repo_path);
1305 if (error != NULL)
1306 goto done;
1308 if (worktree) {
1309 const char *prefix = got_worktree_get_path_prefix(worktree);
1310 char *p, *worktree_subdir = cwd +
1311 strlen(got_worktree_get_root_path(worktree));
1312 if (asprintf(&p, "%s%s%s%s%s",
1313 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1314 worktree_subdir, worktree_subdir[0] ? "/" : "",
1315 path) == -1) {
1316 error = got_error_from_errno();
1317 goto done;
1319 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1320 free(p);
1321 } else {
1322 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1324 if (error)
1325 goto done;
1327 if (commit_id_str == NULL) {
1328 struct got_reference *head_ref;
1329 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1330 if (error != NULL)
1331 goto done;
1332 error = got_ref_resolve(&commit_id, repo, head_ref);
1333 got_ref_close(head_ref);
1334 if (error != NULL)
1335 goto done;
1336 } else {
1337 error = got_object_resolve_id_str(&commit_id, repo,
1338 commit_id_str);
1339 if (error != NULL)
1340 goto done;
1343 error = got_blame(in_repo_path, commit_id, repo, stdout);
1344 done:
1345 free(in_repo_path);
1346 free(repo_path);
1347 free(cwd);
1348 free(commit_id);
1349 if (worktree)
1350 got_worktree_close(worktree);
1351 if (repo) {
1352 const struct got_error *repo_error;
1353 repo_error = got_repo_close(repo);
1354 if (error == NULL)
1355 error = repo_error;
1357 return error;
1360 __dead static void
1361 usage_tree(void)
1363 fprintf(stderr,
1364 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1365 getprogname());
1366 exit(1);
1369 static void
1370 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1371 const char *root_path)
1373 int is_root_path = (strcmp(path, root_path) == 0);
1375 path += strlen(root_path);
1376 while (path[0] == '/')
1377 path++;
1379 printf("%s%s%s%s%s\n", id ? id : "", path,
1380 is_root_path ? "" : "/", te->name,
1381 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1384 static const struct got_error *
1385 print_tree(const char *path, struct got_object_id *commit_id,
1386 int show_ids, int recurse, const char *root_path,
1387 struct got_repository *repo)
1389 const struct got_error *err = NULL;
1390 struct got_object_id *tree_id = NULL;
1391 struct got_tree_object *tree = NULL;
1392 const struct got_tree_entries *entries;
1393 struct got_tree_entry *te;
1395 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1396 if (err)
1397 goto done;
1399 err = got_object_open_as_tree(&tree, repo, tree_id);
1400 if (err)
1401 goto done;
1402 entries = got_object_tree_get_entries(tree);
1403 te = SIMPLEQ_FIRST(&entries->head);
1404 while (te) {
1405 char *id = NULL;
1407 if (sigint_received || sigpipe_received)
1408 break;
1410 if (show_ids) {
1411 char *id_str;
1412 err = got_object_id_str(&id_str, te->id);
1413 if (err)
1414 goto done;
1415 if (asprintf(&id, "%s ", id_str) == -1) {
1416 err = got_error_from_errno();
1417 free(id_str);
1418 goto done;
1420 free(id_str);
1422 print_entry(te, id, path, root_path);
1423 free(id);
1425 if (recurse && S_ISDIR(te->mode)) {
1426 char *child_path;
1427 if (asprintf(&child_path, "%s%s%s", path,
1428 path[0] == '/' && path[1] == '\0' ? "" : "/",
1429 te->name) == -1) {
1430 err = got_error_from_errno();
1431 goto done;
1433 err = print_tree(child_path, commit_id, show_ids, 1,
1434 root_path, repo);
1435 free(child_path);
1436 if (err)
1437 goto done;
1440 te = SIMPLEQ_NEXT(te, entry);
1442 done:
1443 if (tree)
1444 got_object_tree_close(tree);
1445 free(tree_id);
1446 return err;
1449 static const struct got_error *
1450 cmd_tree(int argc, char *argv[])
1452 const struct got_error *error;
1453 struct got_repository *repo = NULL;
1454 struct got_worktree *worktree = NULL;
1455 const char *path;
1456 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1457 struct got_object_id *commit_id = NULL;
1458 char *commit_id_str = NULL;
1459 int show_ids = 0, recurse = 0;
1460 int ch;
1462 #ifndef PROFILE
1463 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1464 NULL) == -1)
1465 err(1, "pledge");
1466 #endif
1468 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1469 switch (ch) {
1470 case 'c':
1471 commit_id_str = optarg;
1472 break;
1473 case 'r':
1474 repo_path = realpath(optarg, NULL);
1475 if (repo_path == NULL)
1476 err(1, "-r option");
1477 break;
1478 case 'i':
1479 show_ids = 1;
1480 break;
1481 case 'R':
1482 recurse = 1;
1483 break;
1484 default:
1485 usage_tree();
1486 /* NOTREACHED */
1490 argc -= optind;
1491 argv += optind;
1493 if (argc == 1)
1494 path = argv[0];
1495 else if (argc > 1)
1496 usage_tree();
1497 else
1498 path = NULL;
1500 cwd = getcwd(NULL, 0);
1501 if (cwd == NULL) {
1502 error = got_error_from_errno();
1503 goto done;
1505 if (repo_path == NULL) {
1506 error = got_worktree_open(&worktree, cwd);
1507 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1508 goto done;
1509 else
1510 error = NULL;
1511 if (worktree) {
1512 repo_path =
1513 strdup(got_worktree_get_repo_path(worktree));
1514 if (repo_path == NULL)
1515 error = got_error_from_errno();
1516 if (error)
1517 goto done;
1518 } else {
1519 repo_path = strdup(cwd);
1520 if (repo_path == NULL) {
1521 error = got_error_from_errno();
1522 goto done;
1527 error = apply_unveil(repo_path, NULL);
1528 if (error)
1529 goto done;
1531 error = got_repo_open(&repo, repo_path);
1532 if (error != NULL)
1533 goto done;
1535 if (path == NULL) {
1536 if (worktree) {
1537 char *p, *worktree_subdir = cwd +
1538 strlen(got_worktree_get_root_path(worktree));
1539 if (asprintf(&p, "%s/%s",
1540 got_worktree_get_path_prefix(worktree),
1541 worktree_subdir) == -1) {
1542 error = got_error_from_errno();
1543 goto done;
1545 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1546 free(p);
1547 if (error)
1548 goto done;
1549 } else
1550 path = "/";
1552 if (in_repo_path == NULL) {
1553 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1554 if (error != NULL)
1555 goto done;
1558 if (commit_id_str == NULL) {
1559 struct got_reference *head_ref;
1560 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1561 if (error != NULL)
1562 goto done;
1563 error = got_ref_resolve(&commit_id, repo, head_ref);
1564 got_ref_close(head_ref);
1565 if (error != NULL)
1566 goto done;
1567 } else {
1568 error = got_object_resolve_id_str(&commit_id, repo,
1569 commit_id_str);
1570 if (error != NULL)
1571 goto done;
1574 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1575 in_repo_path, repo);
1576 done:
1577 free(in_repo_path);
1578 free(repo_path);
1579 free(cwd);
1580 free(commit_id);
1581 if (worktree)
1582 got_worktree_close(worktree);
1583 if (repo) {
1584 const struct got_error *repo_error;
1585 repo_error = got_repo_close(repo);
1586 if (error == NULL)
1587 error = repo_error;
1589 return error;
1592 __dead static void
1593 usage_status(void)
1595 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1596 exit(1);
1599 static const struct got_error *
1600 print_status(void *arg, unsigned char status, const char *path,
1601 struct got_object_id *id)
1603 printf("%c %s\n", status, path);
1604 return NULL;
1607 static const struct got_error *
1608 cmd_status(int argc, char *argv[])
1610 const struct got_error *error = NULL;
1611 struct got_repository *repo = NULL;
1612 struct got_worktree *worktree = NULL;
1613 char *cwd = NULL, *path = NULL;
1614 int ch;
1616 while ((ch = getopt(argc, argv, "")) != -1) {
1617 switch (ch) {
1618 default:
1619 usage_status();
1620 /* NOTREACHED */
1624 argc -= optind;
1625 argv += optind;
1627 #ifndef PROFILE
1628 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1629 NULL) == -1)
1630 err(1, "pledge");
1631 #endif
1632 cwd = getcwd(NULL, 0);
1633 if (cwd == NULL) {
1634 error = got_error_from_errno();
1635 goto done;
1638 error = got_worktree_open(&worktree, cwd);
1639 if (error != NULL)
1640 goto done;
1642 if (argc == 0) {
1643 path = strdup("");
1644 if (path == NULL) {
1645 error = got_error_from_errno();
1646 goto done;
1648 } else if (argc == 1) {
1649 error = get_status_path(&path, worktree, argv[0]);
1650 if (error)
1651 goto done;
1652 } else
1653 usage_status();
1655 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1656 if (error != NULL)
1657 goto done;
1659 error = apply_unveil(got_repo_get_path(repo),
1660 got_worktree_get_root_path(worktree));
1661 if (error)
1662 goto done;
1664 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1665 check_cancelled, NULL);
1666 done:
1667 free(cwd);
1668 free(path);
1669 return error;