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);
79 __dead static void usage_ref(void);
81 static const struct got_error* cmd_checkout(int, char *[]);
82 static const struct got_error* cmd_update(int, char *[]);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
87 static const struct got_error* cmd_status(int, char *[]);
88 static const struct got_error* cmd_ref(int, char *[]);
90 static struct cmd got_commands[] = {
91 { "checkout", cmd_checkout, usage_checkout,
92 "check out a new work tree from a repository" },
93 { "update", cmd_update, usage_update,
94 "update a work tree to a different commit" },
95 { "log", cmd_log, usage_log,
96 "show repository history" },
97 { "diff", cmd_diff, usage_diff,
98 "compare files and directories" },
99 { "blame", cmd_blame, usage_blame,
100 " show when lines in a file were changed" },
101 { "tree", cmd_tree, usage_tree,
102 " list files and directories in repository" },
103 { "status", cmd_status, usage_status,
104 "show modification status of files" },
105 { "ref", cmd_ref, usage_ref,
106 "manage references in repository" },
107 };
109 int
110 main(int argc, char *argv[])
112 struct cmd *cmd;
113 unsigned int i;
114 int ch;
115 int hflag = 0;
117 setlocale(LC_CTYPE, "");
119 while ((ch = getopt(argc, argv, "h")) != -1) {
120 switch (ch) {
121 case 'h':
122 hflag = 1;
123 break;
124 default:
125 usage();
126 /* NOTREACHED */
130 argc -= optind;
131 argv += optind;
132 optind = 0;
134 if (argc <= 0)
135 usage();
137 signal(SIGINT, catch_sigint);
138 signal(SIGPIPE, catch_sigpipe);
140 for (i = 0; i < nitems(got_commands); i++) {
141 const struct got_error *error;
143 cmd = &got_commands[i];
145 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
146 continue;
148 if (hflag)
149 got_commands[i].cmd_usage();
151 error = got_commands[i].cmd_main(argc, argv);
152 if (error && !(sigint_received || sigpipe_received)) {
153 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
154 return 1;
157 return 0;
160 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
161 return 1;
164 __dead static void
165 usage(void)
167 int i;
169 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
170 "Available commands:\n", getprogname());
171 for (i = 0; i < nitems(got_commands); i++) {
172 struct cmd *cmd = &got_commands[i];
173 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
175 exit(1);
178 static const struct got_error *
179 apply_unveil(const char *repo_path, int repo_read_only,
180 const char *worktree_path)
182 const struct got_error *error;
184 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
185 return got_error_from_errno();
187 if (worktree_path && unveil(worktree_path, "rwc") != 0)
188 return got_error_from_errno();
190 if (unveil("/tmp", "rwc") != 0)
191 return got_error_from_errno();
193 error = got_privsep_unveil_exec_helpers();
194 if (error != NULL)
195 return error;
197 if (unveil(NULL, NULL) != 0)
198 return got_error_from_errno();
200 return NULL;
203 __dead static void
204 usage_checkout(void)
206 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
207 "[worktree-path]\n", getprogname());
208 exit(1);
211 static void
212 checkout_progress(void *arg, unsigned char status, const char *path)
214 char *worktree_path = arg;
216 while (path[0] == '/')
217 path++;
219 printf("%c %s/%s\n", status, worktree_path, path);
222 static const struct got_error *
223 check_cancelled(void *arg)
225 if (sigint_received || sigpipe_received)
226 return got_error(GOT_ERR_CANCELLED);
227 return NULL;
230 static const struct got_error *
231 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
232 struct got_repository *repo)
234 const struct got_error *err;
235 struct got_reference *head_ref = NULL;
236 struct got_object_id *head_commit_id = NULL;
237 struct got_commit_graph *graph = NULL;
239 head_ref = got_worktree_get_head_ref(worktree);
240 if (head_ref == NULL)
241 return got_error_from_errno();
243 /* TODO: Check the reflog. The head ref may have been rebased. */
244 err = got_ref_resolve(&head_commit_id, repo, head_ref);
245 if (err)
246 goto done;
248 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
249 if (err)
250 goto done;
252 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
253 if (err)
254 goto done;
255 while (1) {
256 struct got_object_id *id;
258 if (sigint_received || sigpipe_received)
259 break;
261 err = got_commit_graph_iter_next(&id, graph);
262 if (err) {
263 if (err->code == GOT_ERR_ITER_COMPLETED) {
264 err = got_error(GOT_ERR_ANCESTRY);
265 break;
267 if (err->code != GOT_ERR_ITER_NEED_MORE)
268 break;
269 err = got_commit_graph_fetch_commits(graph, 1, repo);
270 if (err)
271 break;
272 else
273 continue;
275 if (id == NULL)
276 break;
277 if (got_object_id_cmp(id, commit_id) == 0)
278 break;
280 done:
281 if (head_ref)
282 got_ref_close(head_ref);
283 if (graph)
284 got_commit_graph_close(graph);
285 return err;
289 static const struct got_error *
290 cmd_checkout(int argc, char *argv[])
292 const struct got_error *error = NULL;
293 struct got_repository *repo = NULL;
294 struct got_reference *head_ref = NULL;
295 struct got_worktree *worktree = NULL;
296 char *repo_path = NULL;
297 char *worktree_path = NULL;
298 const char *path_prefix = "";
299 char *commit_id_str = NULL;
300 int ch, same_path_prefix;
302 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
303 switch (ch) {
304 case 'c':
305 commit_id_str = strdup(optarg);
306 if (commit_id_str == NULL)
307 return got_error_from_errno();
308 break;
309 case 'p':
310 path_prefix = optarg;
311 break;
312 default:
313 usage_checkout();
314 /* NOTREACHED */
318 argc -= optind;
319 argv += optind;
321 #ifndef PROFILE
322 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
323 "unveil", NULL) == -1)
324 err(1, "pledge");
325 #endif
326 if (argc == 1) {
327 char *cwd, *base, *dotgit;
328 repo_path = realpath(argv[0], NULL);
329 if (repo_path == NULL)
330 return got_error_from_errno();
331 cwd = getcwd(NULL, 0);
332 if (cwd == NULL) {
333 error = got_error_from_errno();
334 goto done;
336 if (path_prefix[0])
337 base = basename(path_prefix);
338 else
339 base = basename(repo_path);
340 if (base == NULL) {
341 error = got_error_from_errno();
342 goto done;
344 dotgit = strstr(base, ".git");
345 if (dotgit)
346 *dotgit = '\0';
347 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
348 error = got_error_from_errno();
349 free(cwd);
350 goto done;
352 free(cwd);
353 } else if (argc == 2) {
354 repo_path = realpath(argv[0], NULL);
355 if (repo_path == NULL) {
356 error = got_error_from_errno();
357 goto done;
359 worktree_path = realpath(argv[1], NULL);
360 if (worktree_path == NULL) {
361 error = got_error_from_errno();
362 goto done;
364 } else
365 usage_checkout();
367 error = apply_unveil(repo_path, 0, worktree_path);
368 if (error)
369 goto done;
371 error = got_repo_open(&repo, repo_path);
372 if (error != NULL)
373 goto done;
375 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
376 if (error != NULL)
377 goto done;
379 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
380 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
381 goto done;
383 error = got_worktree_open(&worktree, worktree_path);
384 if (error != NULL)
385 goto done;
387 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
388 path_prefix);
389 if (error != NULL)
390 goto done;
391 if (!same_path_prefix) {
392 error = got_error(GOT_ERR_PATH_PREFIX);
393 goto done;
396 if (commit_id_str) {
397 struct got_object_id *commit_id;
398 error = got_object_resolve_id_str(&commit_id, repo,
399 commit_id_str);
400 if (error != NULL)
401 goto done;
402 error = check_ancestry(worktree, commit_id, repo);
403 if (error != NULL) {
404 free(commit_id);
405 goto done;
407 error = got_worktree_set_base_commit_id(worktree, repo,
408 commit_id);
409 free(commit_id);
410 if (error)
411 goto done;
414 error = got_worktree_checkout_files(worktree, repo,
415 checkout_progress, worktree_path, check_cancelled, NULL);
416 if (error != NULL)
417 goto done;
419 printf("Now shut up and hack\n");
421 done:
422 free(commit_id_str);
423 free(repo_path);
424 free(worktree_path);
425 return error;
428 __dead static void
429 usage_update(void)
431 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
432 getprogname());
433 exit(1);
436 static void
437 update_progress(void *arg, unsigned char status, const char *path)
439 int *did_something = arg;
441 if (status == GOT_STATUS_EXISTS)
442 return;
444 *did_something = 1;
445 while (path[0] == '/')
446 path++;
447 printf("%c %s\n", status, path);
450 static const struct got_error *
451 cmd_update(int argc, char *argv[])
453 const struct got_error *error = NULL;
454 struct got_repository *repo = NULL;
455 struct got_worktree *worktree = NULL;
456 char *worktree_path = NULL;
457 struct got_object_id *commit_id = NULL;
458 char *commit_id_str = NULL;
459 int ch, did_something = 0;
461 while ((ch = getopt(argc, argv, "c:")) != -1) {
462 switch (ch) {
463 case 'c':
464 commit_id_str = strdup(optarg);
465 if (commit_id_str == NULL)
466 return got_error_from_errno();
467 break;
468 default:
469 usage_update();
470 /* NOTREACHED */
474 argc -= optind;
475 argv += optind;
477 #ifndef PROFILE
478 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
479 "unveil", NULL) == -1)
480 err(1, "pledge");
481 #endif
482 if (argc == 0) {
483 worktree_path = getcwd(NULL, 0);
484 if (worktree_path == NULL) {
485 error = got_error_from_errno();
486 goto done;
488 } else if (argc == 1) {
489 worktree_path = realpath(argv[0], NULL);
490 if (worktree_path == NULL) {
491 error = got_error_from_errno();
492 goto done;
494 } else
495 usage_update();
497 error = got_worktree_open(&worktree, worktree_path);
498 if (error != NULL)
499 goto done;
501 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
502 if (error != NULL)
503 goto done;
505 error = apply_unveil(got_repo_get_path(repo), 0,
506 got_worktree_get_root_path(worktree));
507 if (error)
508 goto done;
510 if (commit_id_str == NULL) {
511 struct got_reference *head_ref;
512 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
513 if (error != NULL)
514 goto done;
515 error = got_ref_resolve(&commit_id, repo, head_ref);
516 if (error != NULL)
517 goto done;
518 error = got_object_id_str(&commit_id_str, commit_id);
519 if (error != NULL)
520 goto done;
521 } else {
522 error = got_object_resolve_id_str(&commit_id, repo,
523 commit_id_str);
524 if (error != NULL)
525 goto done;
528 error = check_ancestry(worktree, commit_id, repo);
529 if (error != NULL)
530 goto done;
532 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
533 commit_id) != 0) {
534 error = got_worktree_set_base_commit_id(worktree, repo,
535 commit_id);
536 if (error)
537 goto done;
540 error = got_worktree_checkout_files(worktree, repo,
541 update_progress, &did_something, check_cancelled, NULL);
542 if (error != NULL)
543 goto done;
545 if (did_something)
546 printf("Updated to commit %s\n", commit_id_str);
547 else
548 printf("Already up-to-date\n");
549 done:
550 free(worktree_path);
551 free(commit_id);
552 free(commit_id_str);
553 return error;
556 static const struct got_error *
557 print_patch(struct got_commit_object *commit, struct got_object_id *id,
558 int diff_context, struct got_repository *repo)
560 const struct got_error *err = NULL;
561 struct got_tree_object *tree1 = NULL, *tree2;
562 struct got_object_qid *qid;
563 char *id_str1 = NULL, *id_str2;
565 err = got_object_open_as_tree(&tree2, repo,
566 got_object_commit_get_tree_id(commit));
567 if (err)
568 return err;
570 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
571 if (qid != NULL) {
572 struct got_commit_object *pcommit;
574 err = got_object_open_as_commit(&pcommit, repo, qid->id);
575 if (err)
576 return err;
578 err = got_object_open_as_tree(&tree1, repo,
579 got_object_commit_get_tree_id(pcommit));
580 got_object_commit_close(pcommit);
581 if (err)
582 return err;
584 err = got_object_id_str(&id_str1, qid->id);
585 if (err)
586 return err;
589 err = got_object_id_str(&id_str2, id);
590 if (err)
591 goto done;
593 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
594 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
595 done:
596 if (tree1)
597 got_object_tree_close(tree1);
598 got_object_tree_close(tree2);
599 free(id_str1);
600 free(id_str2);
601 return err;
604 static char *
605 get_datestr(time_t *time, char *datebuf)
607 char *p, *s = ctime_r(time, datebuf);
608 p = strchr(s, '\n');
609 if (p)
610 *p = '\0';
611 return s;
614 static const struct got_error *
615 print_commit(struct got_commit_object *commit, struct got_object_id *id,
616 struct got_repository *repo, int show_patch, int diff_context,
617 struct got_reflist_head *refs)
619 const struct got_error *err = NULL;
620 char *id_str, *datestr, *logmsg0, *logmsg, *line;
621 char datebuf[26];
622 time_t committer_time;
623 const char *author, *committer;
624 char *refs_str = NULL;
625 struct got_reflist_entry *re;
627 SIMPLEQ_FOREACH(re, refs, entry) {
628 char *s;
629 const char *name;
630 if (got_object_id_cmp(re->id, id) != 0)
631 continue;
632 name = got_ref_get_name(re->ref);
633 if (strcmp(name, GOT_REF_HEAD) == 0)
634 continue;
635 if (strncmp(name, "refs/", 5) == 0)
636 name += 5;
637 if (strncmp(name, "got/", 4) == 0)
638 continue;
639 if (strncmp(name, "heads/", 6) == 0)
640 name += 6;
641 if (strncmp(name, "remotes/", 8) == 0)
642 name += 8;
643 s = refs_str;
644 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
645 name) == -1) {
646 err = got_error_from_errno();
647 free(s);
648 break;
650 free(s);
652 err = got_object_id_str(&id_str, id);
653 if (err)
654 return err;
656 printf("-----------------------------------------------\n");
657 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
658 refs_str ? refs_str : "", refs_str ? ")" : "");
659 free(id_str);
660 id_str = NULL;
661 free(refs_str);
662 refs_str = NULL;
663 printf("from: %s\n", got_object_commit_get_author(commit));
664 committer_time = got_object_commit_get_committer_time(commit);
665 datestr = get_datestr(&committer_time, datebuf);
666 printf("date: %s UTC\n", datestr);
667 author = got_object_commit_get_author(commit);
668 committer = got_object_commit_get_committer(commit);
669 if (strcmp(author, committer) != 0)
670 printf("via: %s\n", committer);
671 if (got_object_commit_get_nparents(commit) > 1) {
672 const struct got_object_id_queue *parent_ids;
673 struct got_object_qid *qid;
674 int n = 1;
675 parent_ids = got_object_commit_get_parent_ids(commit);
676 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
677 err = got_object_id_str(&id_str, qid->id);
678 if (err)
679 return err;
680 printf("parent %d: %s\n", n++, id_str);
681 free(id_str);
685 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
686 if (logmsg0 == NULL)
687 return got_error_from_errno();
689 logmsg = logmsg0;
690 do {
691 line = strsep(&logmsg, "\n");
692 if (line)
693 printf(" %s\n", line);
694 } while (line);
695 free(logmsg0);
697 if (show_patch) {
698 err = print_patch(commit, id, diff_context, repo);
699 if (err == 0)
700 printf("\n");
703 if (fflush(stdout) != 0 && err == NULL)
704 err = got_error_from_errno();
705 return err;
708 static const struct got_error *
709 print_commits(struct got_object_id *root_id, struct got_repository *repo,
710 char *path, int show_patch, int diff_context, int limit,
711 int first_parent_traversal, struct got_reflist_head *refs)
713 const struct got_error *err;
714 struct got_commit_graph *graph;
716 err = got_commit_graph_open(&graph, root_id, path,
717 first_parent_traversal, repo);
718 if (err)
719 return err;
720 err = got_commit_graph_iter_start(graph, root_id, repo);
721 if (err)
722 goto done;
723 while (1) {
724 struct got_commit_object *commit;
725 struct got_object_id *id;
727 if (sigint_received || sigpipe_received)
728 break;
730 err = got_commit_graph_iter_next(&id, graph);
731 if (err) {
732 if (err->code == GOT_ERR_ITER_COMPLETED) {
733 err = NULL;
734 break;
736 if (err->code != GOT_ERR_ITER_NEED_MORE)
737 break;
738 err = got_commit_graph_fetch_commits(graph, 1, repo);
739 if (err)
740 break;
741 else
742 continue;
744 if (id == NULL)
745 break;
747 err = got_object_open_as_commit(&commit, repo, id);
748 if (err)
749 break;
750 err = print_commit(commit, id, repo, show_patch, diff_context,
751 refs);
752 got_object_commit_close(commit);
753 if (err || (limit && --limit == 0))
754 break;
756 done:
757 got_commit_graph_close(graph);
758 return err;
761 __dead static void
762 usage_log(void)
764 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
765 "[-r repository-path] [path]\n", getprogname());
766 exit(1);
769 static const struct got_error *
770 cmd_log(int argc, char *argv[])
772 const struct got_error *error;
773 struct got_repository *repo = NULL;
774 struct got_worktree *worktree = NULL;
775 struct got_commit_object *commit = NULL;
776 struct got_object_id *id = NULL;
777 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
778 char *start_commit = NULL;
779 int diff_context = 3, ch;
780 int show_patch = 0, limit = 0, first_parent_traversal = 0;
781 const char *errstr;
782 struct got_reflist_head refs;
784 #ifndef PROFILE
785 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
786 NULL)
787 == -1)
788 err(1, "pledge");
789 #endif
791 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
792 switch (ch) {
793 case 'p':
794 show_patch = 1;
795 break;
796 case 'c':
797 start_commit = optarg;
798 break;
799 case 'C':
800 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
801 &errstr);
802 if (errstr != NULL)
803 err(1, "-C option %s", errstr);
804 break;
805 case 'l':
806 limit = strtonum(optarg, 1, INT_MAX, &errstr);
807 if (errstr != NULL)
808 err(1, "-l option %s", errstr);
809 break;
810 case 'f':
811 first_parent_traversal = 1;
812 break;
813 case 'r':
814 repo_path = realpath(optarg, NULL);
815 if (repo_path == NULL)
816 err(1, "-r option");
817 break;
818 default:
819 usage_log();
820 /* NOTREACHED */
824 argc -= optind;
825 argv += optind;
827 cwd = getcwd(NULL, 0);
828 if (cwd == NULL) {
829 error = got_error_from_errno();
830 goto done;
833 error = got_worktree_open(&worktree, cwd);
834 if (error && error->code != GOT_ERR_NOT_WORKTREE)
835 goto done;
836 error = NULL;
838 if (argc == 0)
839 path = strdup("");
840 else if (argc == 1) {
841 error = got_worktree_resolve_path(&path, worktree, argv[0]);
842 if (error)
843 goto done;
844 } else
845 usage_log();
846 if (path == NULL)
847 goto done;
849 repo_path = worktree ?
850 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
851 if (repo_path == NULL) {
852 error = got_error_from_errno();
853 goto done;
856 error = apply_unveil(repo_path, 1,
857 worktree ? got_worktree_get_root_path(worktree) : NULL);
858 if (error)
859 goto done;
861 error = got_repo_open(&repo, repo_path);
862 if (error != NULL)
863 goto done;
865 if (start_commit == NULL) {
866 struct got_reference *head_ref;
867 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
868 if (error != NULL)
869 return error;
870 error = got_ref_resolve(&id, repo, head_ref);
871 got_ref_close(head_ref);
872 if (error != NULL)
873 return error;
874 error = got_object_open_as_commit(&commit, repo, id);
875 } else {
876 struct got_reference *ref;
877 error = got_ref_open(&ref, repo, start_commit);
878 if (error == NULL) {
879 int obj_type;
880 error = got_ref_resolve(&id, repo, ref);
881 got_ref_close(ref);
882 if (error != NULL)
883 goto done;
884 error = got_object_get_type(&obj_type, repo, id);
885 if (error != NULL)
886 goto done;
887 if (obj_type == GOT_OBJ_TYPE_TAG) {
888 struct got_tag_object *tag;
889 error = got_object_open_as_tag(&tag, repo, id);
890 if (error != NULL)
891 goto done;
892 if (got_object_tag_get_object_type(tag) !=
893 GOT_OBJ_TYPE_COMMIT) {
894 got_object_tag_close(tag);
895 error = got_error(GOT_ERR_OBJ_TYPE);
896 goto done;
898 free(id);
899 id = got_object_id_dup(
900 got_object_tag_get_object_id(tag));
901 if (id == NULL)
902 error = got_error_from_errno();
903 got_object_tag_close(tag);
904 if (error)
905 goto done;
906 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
907 error = got_error(GOT_ERR_OBJ_TYPE);
908 goto done;
910 error = got_object_open_as_commit(&commit, repo, id);
911 if (error != NULL)
912 goto done;
914 if (commit == NULL) {
915 error = got_object_resolve_id_str(&id, repo,
916 start_commit);
917 if (error != NULL)
918 return error;
921 if (error != NULL)
922 goto done;
924 error = got_repo_map_path(&in_repo_path, repo, path, 1);
925 if (error != NULL)
926 goto done;
927 if (in_repo_path) {
928 free(path);
929 path = in_repo_path;
932 SIMPLEQ_INIT(&refs);
933 error = got_ref_list(&refs, repo);
934 if (error)
935 goto done;
937 error = print_commits(id, repo, path, show_patch,
938 diff_context, limit, first_parent_traversal, &refs);
939 done:
940 free(path);
941 free(repo_path);
942 free(cwd);
943 free(id);
944 if (worktree)
945 got_worktree_close(worktree);
946 if (repo) {
947 const struct got_error *repo_error;
948 repo_error = got_repo_close(repo);
949 if (error == NULL)
950 error = repo_error;
952 got_ref_list_free(&refs);
953 return error;
956 __dead static void
957 usage_diff(void)
959 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
960 "[object1 object2 | path]\n", getprogname());
961 exit(1);
964 struct print_diff_arg {
965 struct got_repository *repo;
966 struct got_worktree *worktree;
967 int diff_context;
968 const char *id_str;
969 int header_shown;
970 };
972 static const struct got_error *
973 print_diff(void *arg, unsigned char status, const char *path,
974 struct got_object_id *id)
976 struct print_diff_arg *a = arg;
977 const struct got_error *err = NULL;
978 struct got_blob_object *blob1 = NULL;
979 FILE *f2 = NULL;
980 char *abspath = NULL;
981 struct stat sb;
983 if (status != GOT_STATUS_MODIFY)
984 return NULL;
986 if (!a->header_shown) {
987 printf("diff %s %s\n", a->id_str,
988 got_worktree_get_root_path(a->worktree));
989 a->header_shown = 1;
992 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
993 if (err)
994 goto done;
996 if (asprintf(&abspath, "%s/%s",
997 got_worktree_get_root_path(a->worktree), path) == -1) {
998 err = got_error_from_errno();
999 goto done;
1002 f2 = fopen(abspath, "r");
1003 if (f2 == NULL) {
1004 err = got_error_from_errno();
1005 goto done;
1007 if (lstat(abspath, &sb) == -1) {
1008 err = got_error_from_errno();
1009 goto done;
1012 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1013 stdout);
1014 done:
1015 if (blob1)
1016 got_object_blob_close(blob1);
1017 if (f2 && fclose(f2) != 0 && err == NULL)
1018 err = got_error_from_errno();
1019 free(abspath);
1020 return err;
1023 static const struct got_error *
1024 cmd_diff(int argc, char *argv[])
1026 const struct got_error *error;
1027 struct got_repository *repo = NULL;
1028 struct got_worktree *worktree = NULL;
1029 char *cwd = NULL, *repo_path = NULL;
1030 struct got_object_id *id1 = NULL, *id2 = NULL;
1031 char *id_str1 = NULL, *id_str2 = NULL;
1032 int type1, type2;
1033 int diff_context = 3, ch;
1034 const char *errstr;
1035 char *path = NULL;
1037 #ifndef PROFILE
1038 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1039 NULL) == -1)
1040 err(1, "pledge");
1041 #endif
1043 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1044 switch (ch) {
1045 case 'C':
1046 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1047 if (errstr != NULL)
1048 err(1, "-C option %s", errstr);
1049 break;
1050 case 'r':
1051 repo_path = realpath(optarg, NULL);
1052 if (repo_path == NULL)
1053 err(1, "-r option");
1054 break;
1055 default:
1056 usage_diff();
1057 /* NOTREACHED */
1061 argc -= optind;
1062 argv += optind;
1064 cwd = getcwd(NULL, 0);
1065 if (cwd == NULL) {
1066 error = got_error_from_errno();
1067 goto done;
1069 error = got_worktree_open(&worktree, cwd);
1070 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1071 goto done;
1072 if (argc <= 1) {
1073 if (worktree == NULL) {
1074 error = got_error(GOT_ERR_NOT_WORKTREE);
1075 goto done;
1077 if (repo_path)
1078 errx(1,
1079 "-r option can't be used when diffing a work tree");
1080 repo_path = strdup(got_worktree_get_repo_path(worktree));
1081 if (repo_path == NULL) {
1082 error = got_error_from_errno();
1083 goto done;
1085 if (argc == 1) {
1086 error = got_worktree_resolve_path(&path, worktree,
1087 argv[0]);
1088 if (error)
1089 goto done;
1090 } else {
1091 path = strdup("");
1092 if (path == NULL) {
1093 error = got_error_from_errno();
1094 goto done;
1097 } else if (argc == 2) {
1098 id_str1 = argv[0];
1099 id_str2 = argv[1];
1100 } else
1101 usage_diff();
1103 if (repo_path == NULL) {
1104 repo_path = getcwd(NULL, 0);
1105 if (repo_path == NULL)
1106 return got_error_from_errno();
1109 error = apply_unveil(repo_path, 1,
1110 worktree ? got_worktree_get_root_path(worktree) : NULL);
1111 if (error)
1112 goto done;
1114 error = got_repo_open(&repo, repo_path);
1115 free(repo_path);
1116 if (error != NULL)
1117 goto done;
1119 if (worktree) {
1120 struct print_diff_arg arg;
1121 char *id_str;
1122 error = got_object_id_str(&id_str,
1123 got_worktree_get_base_commit_id(worktree));
1124 if (error)
1125 goto done;
1126 arg.repo = repo;
1127 arg.worktree = worktree;
1128 arg.diff_context = diff_context;
1129 arg.id_str = id_str;
1130 arg.header_shown = 0;
1132 error = got_worktree_status(worktree, path, repo, print_diff,
1133 &arg, check_cancelled, NULL);
1134 free(id_str);
1135 goto done;
1138 error = got_object_resolve_id_str(&id1, repo, id_str1);
1139 if (error)
1140 goto done;
1142 error = got_object_resolve_id_str(&id2, repo, id_str2);
1143 if (error)
1144 goto done;
1146 error = got_object_get_type(&type1, repo, id1);
1147 if (error)
1148 goto done;
1150 error = got_object_get_type(&type2, repo, id2);
1151 if (error)
1152 goto done;
1154 if (type1 != type2) {
1155 error = got_error(GOT_ERR_OBJ_TYPE);
1156 goto done;
1159 switch (type1) {
1160 case GOT_OBJ_TYPE_BLOB:
1161 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1162 diff_context, repo, stdout);
1163 break;
1164 case GOT_OBJ_TYPE_TREE:
1165 error = got_diff_objects_as_trees(id1, id2, "", "",
1166 diff_context, repo, stdout);
1167 break;
1168 case GOT_OBJ_TYPE_COMMIT:
1169 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1170 id_str2);
1171 error = got_diff_objects_as_commits(id1, id2, diff_context,
1172 repo, stdout);
1173 break;
1174 default:
1175 error = got_error(GOT_ERR_OBJ_TYPE);
1178 done:
1179 free(id1);
1180 free(id2);
1181 free(path);
1182 if (worktree)
1183 got_worktree_close(worktree);
1184 if (repo) {
1185 const struct got_error *repo_error;
1186 repo_error = got_repo_close(repo);
1187 if (error == NULL)
1188 error = repo_error;
1190 return error;
1193 __dead static void
1194 usage_blame(void)
1196 fprintf(stderr,
1197 "usage: %s blame [-c commit] [-r repository-path] path\n",
1198 getprogname());
1199 exit(1);
1202 static const struct got_error *
1203 cmd_blame(int argc, char *argv[])
1205 const struct got_error *error;
1206 struct got_repository *repo = NULL;
1207 struct got_worktree *worktree = NULL;
1208 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1209 struct got_object_id *commit_id = NULL;
1210 char *commit_id_str = NULL;
1211 int ch;
1213 #ifndef PROFILE
1214 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1215 NULL) == -1)
1216 err(1, "pledge");
1217 #endif
1219 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1220 switch (ch) {
1221 case 'c':
1222 commit_id_str = optarg;
1223 break;
1224 case 'r':
1225 repo_path = realpath(optarg, NULL);
1226 if (repo_path == NULL)
1227 err(1, "-r option");
1228 break;
1229 default:
1230 usage_blame();
1231 /* NOTREACHED */
1235 argc -= optind;
1236 argv += optind;
1238 if (argc == 1)
1239 path = argv[0];
1240 else
1241 usage_blame();
1243 cwd = getcwd(NULL, 0);
1244 if (cwd == NULL) {
1245 error = got_error_from_errno();
1246 goto done;
1248 if (repo_path == NULL) {
1249 error = got_worktree_open(&worktree, cwd);
1250 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1251 goto done;
1252 else
1253 error = NULL;
1254 if (worktree) {
1255 repo_path =
1256 strdup(got_worktree_get_repo_path(worktree));
1257 if (repo_path == NULL)
1258 error = got_error_from_errno();
1259 if (error)
1260 goto done;
1261 } else {
1262 repo_path = strdup(cwd);
1263 if (repo_path == NULL) {
1264 error = got_error_from_errno();
1265 goto done;
1270 error = apply_unveil(repo_path, 1, NULL);
1271 if (error)
1272 goto done;
1274 error = got_repo_open(&repo, repo_path);
1275 if (error != NULL)
1276 goto done;
1278 if (worktree) {
1279 const char *prefix = got_worktree_get_path_prefix(worktree);
1280 char *p, *worktree_subdir = cwd +
1281 strlen(got_worktree_get_root_path(worktree));
1282 if (asprintf(&p, "%s%s%s%s%s",
1283 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1284 worktree_subdir, worktree_subdir[0] ? "/" : "",
1285 path) == -1) {
1286 error = got_error_from_errno();
1287 goto done;
1289 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1290 free(p);
1291 } else {
1292 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1294 if (error)
1295 goto done;
1297 if (commit_id_str == NULL) {
1298 struct got_reference *head_ref;
1299 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1300 if (error != NULL)
1301 goto done;
1302 error = got_ref_resolve(&commit_id, repo, head_ref);
1303 got_ref_close(head_ref);
1304 if (error != NULL)
1305 goto done;
1306 } else {
1307 error = got_object_resolve_id_str(&commit_id, repo,
1308 commit_id_str);
1309 if (error != NULL)
1310 goto done;
1313 error = got_blame(in_repo_path, commit_id, repo, stdout);
1314 done:
1315 free(in_repo_path);
1316 free(repo_path);
1317 free(cwd);
1318 free(commit_id);
1319 if (worktree)
1320 got_worktree_close(worktree);
1321 if (repo) {
1322 const struct got_error *repo_error;
1323 repo_error = got_repo_close(repo);
1324 if (error == NULL)
1325 error = repo_error;
1327 return error;
1330 __dead static void
1331 usage_tree(void)
1333 fprintf(stderr,
1334 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1335 getprogname());
1336 exit(1);
1339 static void
1340 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1341 const char *root_path)
1343 int is_root_path = (strcmp(path, root_path) == 0);
1345 path += strlen(root_path);
1346 while (path[0] == '/')
1347 path++;
1349 printf("%s%s%s%s%s\n", id ? id : "", path,
1350 is_root_path ? "" : "/", te->name,
1351 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1354 static const struct got_error *
1355 print_tree(const char *path, struct got_object_id *commit_id,
1356 int show_ids, int recurse, const char *root_path,
1357 struct got_repository *repo)
1359 const struct got_error *err = NULL;
1360 struct got_object_id *tree_id = NULL;
1361 struct got_tree_object *tree = NULL;
1362 const struct got_tree_entries *entries;
1363 struct got_tree_entry *te;
1365 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1366 if (err)
1367 goto done;
1369 err = got_object_open_as_tree(&tree, repo, tree_id);
1370 if (err)
1371 goto done;
1372 entries = got_object_tree_get_entries(tree);
1373 te = SIMPLEQ_FIRST(&entries->head);
1374 while (te) {
1375 char *id = NULL;
1377 if (sigint_received || sigpipe_received)
1378 break;
1380 if (show_ids) {
1381 char *id_str;
1382 err = got_object_id_str(&id_str, te->id);
1383 if (err)
1384 goto done;
1385 if (asprintf(&id, "%s ", id_str) == -1) {
1386 err = got_error_from_errno();
1387 free(id_str);
1388 goto done;
1390 free(id_str);
1392 print_entry(te, id, path, root_path);
1393 free(id);
1395 if (recurse && S_ISDIR(te->mode)) {
1396 char *child_path;
1397 if (asprintf(&child_path, "%s%s%s", path,
1398 path[0] == '/' && path[1] == '\0' ? "" : "/",
1399 te->name) == -1) {
1400 err = got_error_from_errno();
1401 goto done;
1403 err = print_tree(child_path, commit_id, show_ids, 1,
1404 root_path, repo);
1405 free(child_path);
1406 if (err)
1407 goto done;
1410 te = SIMPLEQ_NEXT(te, entry);
1412 done:
1413 if (tree)
1414 got_object_tree_close(tree);
1415 free(tree_id);
1416 return err;
1419 static const struct got_error *
1420 cmd_tree(int argc, char *argv[])
1422 const struct got_error *error;
1423 struct got_repository *repo = NULL;
1424 struct got_worktree *worktree = NULL;
1425 const char *path;
1426 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1427 struct got_object_id *commit_id = NULL;
1428 char *commit_id_str = NULL;
1429 int show_ids = 0, recurse = 0;
1430 int ch;
1432 #ifndef PROFILE
1433 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1434 NULL) == -1)
1435 err(1, "pledge");
1436 #endif
1438 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1439 switch (ch) {
1440 case 'c':
1441 commit_id_str = optarg;
1442 break;
1443 case 'r':
1444 repo_path = realpath(optarg, NULL);
1445 if (repo_path == NULL)
1446 err(1, "-r option");
1447 break;
1448 case 'i':
1449 show_ids = 1;
1450 break;
1451 case 'R':
1452 recurse = 1;
1453 break;
1454 default:
1455 usage_tree();
1456 /* NOTREACHED */
1460 argc -= optind;
1461 argv += optind;
1463 if (argc == 1)
1464 path = argv[0];
1465 else if (argc > 1)
1466 usage_tree();
1467 else
1468 path = NULL;
1470 cwd = getcwd(NULL, 0);
1471 if (cwd == NULL) {
1472 error = got_error_from_errno();
1473 goto done;
1475 if (repo_path == NULL) {
1476 error = got_worktree_open(&worktree, cwd);
1477 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1478 goto done;
1479 else
1480 error = NULL;
1481 if (worktree) {
1482 repo_path =
1483 strdup(got_worktree_get_repo_path(worktree));
1484 if (repo_path == NULL)
1485 error = got_error_from_errno();
1486 if (error)
1487 goto done;
1488 } else {
1489 repo_path = strdup(cwd);
1490 if (repo_path == NULL) {
1491 error = got_error_from_errno();
1492 goto done;
1497 error = apply_unveil(repo_path, 1, NULL);
1498 if (error)
1499 goto done;
1501 error = got_repo_open(&repo, repo_path);
1502 if (error != NULL)
1503 goto done;
1505 if (path == NULL) {
1506 if (worktree) {
1507 char *p, *worktree_subdir = cwd +
1508 strlen(got_worktree_get_root_path(worktree));
1509 if (asprintf(&p, "%s/%s",
1510 got_worktree_get_path_prefix(worktree),
1511 worktree_subdir) == -1) {
1512 error = got_error_from_errno();
1513 goto done;
1515 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1516 free(p);
1517 if (error)
1518 goto done;
1519 } else
1520 path = "/";
1522 if (in_repo_path == NULL) {
1523 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1524 if (error != NULL)
1525 goto done;
1528 if (commit_id_str == NULL) {
1529 struct got_reference *head_ref;
1530 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1531 if (error != NULL)
1532 goto done;
1533 error = got_ref_resolve(&commit_id, repo, head_ref);
1534 got_ref_close(head_ref);
1535 if (error != NULL)
1536 goto done;
1537 } else {
1538 error = got_object_resolve_id_str(&commit_id, repo,
1539 commit_id_str);
1540 if (error != NULL)
1541 goto done;
1544 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1545 in_repo_path, repo);
1546 done:
1547 free(in_repo_path);
1548 free(repo_path);
1549 free(cwd);
1550 free(commit_id);
1551 if (worktree)
1552 got_worktree_close(worktree);
1553 if (repo) {
1554 const struct got_error *repo_error;
1555 repo_error = got_repo_close(repo);
1556 if (error == NULL)
1557 error = repo_error;
1559 return error;
1562 __dead static void
1563 usage_status(void)
1565 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1566 exit(1);
1569 static const struct got_error *
1570 print_status(void *arg, unsigned char status, const char *path,
1571 struct got_object_id *id)
1573 printf("%c %s\n", status, path);
1574 return NULL;
1577 static const struct got_error *
1578 cmd_status(int argc, char *argv[])
1580 const struct got_error *error = NULL;
1581 struct got_repository *repo = NULL;
1582 struct got_worktree *worktree = NULL;
1583 char *cwd = NULL, *path = NULL;
1584 int ch;
1586 while ((ch = getopt(argc, argv, "")) != -1) {
1587 switch (ch) {
1588 default:
1589 usage_status();
1590 /* NOTREACHED */
1594 argc -= optind;
1595 argv += optind;
1597 #ifndef PROFILE
1598 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1599 NULL) == -1)
1600 err(1, "pledge");
1601 #endif
1602 cwd = getcwd(NULL, 0);
1603 if (cwd == NULL) {
1604 error = got_error_from_errno();
1605 goto done;
1608 error = got_worktree_open(&worktree, cwd);
1609 if (error != NULL)
1610 goto done;
1612 if (argc == 0) {
1613 path = strdup("");
1614 if (path == NULL) {
1615 error = got_error_from_errno();
1616 goto done;
1618 } else if (argc == 1) {
1619 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1620 if (error)
1621 goto done;
1622 } else
1623 usage_status();
1625 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1626 if (error != NULL)
1627 goto done;
1629 error = apply_unveil(got_repo_get_path(repo), 1,
1630 got_worktree_get_root_path(worktree));
1631 if (error)
1632 goto done;
1634 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1635 check_cancelled, NULL);
1636 done:
1637 free(cwd);
1638 free(path);
1639 return error;
1642 __dead static void
1643 usage_ref(void)
1645 fprintf(stderr,
1646 "usage: %s ref [-r repository] -l | -d name | name object\n",
1647 getprogname());
1648 exit(1);
1651 static const struct got_error *
1652 list_refs(struct got_repository *repo)
1654 static const struct got_error *err = NULL;
1655 struct got_reflist_head refs;
1656 struct got_reflist_entry *re;
1658 SIMPLEQ_INIT(&refs);
1659 err = got_ref_list(&refs, repo);
1660 if (err)
1661 return err;
1663 SIMPLEQ_FOREACH(re, &refs, entry) {
1664 char *refstr;
1665 refstr = got_ref_to_str(re->ref);
1666 if (refstr == NULL)
1667 return got_error_from_errno();
1668 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1669 free(refstr);
1672 got_ref_list_free(&refs);
1673 return NULL;
1676 static const struct got_error *
1677 delete_ref(struct got_repository *repo, const char *refname)
1679 const struct got_error *err = NULL;
1680 struct got_reference *ref;
1682 err = got_ref_open(&ref, repo, refname);
1683 if (err)
1684 return err;
1686 err = got_ref_delete(ref, repo);
1687 got_ref_close(ref);
1688 return err;
1691 static const struct got_error *
1692 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1694 const struct got_error *err = NULL;
1695 struct got_object_id *id;
1696 struct got_reference *ref = NULL;
1698 err = got_object_resolve_id_str(&id, repo, id_str);
1699 if (err)
1700 return err;
1702 err = got_ref_alloc(&ref, refname, id);
1703 if (err)
1704 goto done;
1706 err = got_ref_write(ref, repo);
1707 done:
1708 if (ref)
1709 got_ref_close(ref);
1710 free(id);
1711 return err;
1714 static const struct got_error *
1715 cmd_ref(int argc, char *argv[])
1717 const struct got_error *error = NULL;
1718 struct got_repository *repo = NULL;
1719 struct got_worktree *worktree = NULL;
1720 char *cwd = NULL, *repo_path = NULL;
1721 int ch, do_list = 0;
1722 const char *delref = NULL;
1724 /* TODO: Add -s option for adding symbolic references. */
1725 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1726 switch (ch) {
1727 case 'd':
1728 delref = optarg;
1729 break;
1730 case 'r':
1731 repo_path = realpath(optarg, NULL);
1732 if (repo_path == NULL)
1733 err(1, "-r option");
1734 break;
1735 case 'l':
1736 do_list = 1;
1737 break;
1738 default:
1739 usage_ref();
1740 /* NOTREACHED */
1744 if (do_list && delref)
1745 errx(1, "-l and -d options are mutually exclusive\n");
1747 argc -= optind;
1748 argv += optind;
1750 if (do_list || delref) {
1751 if (argc > 0)
1752 usage_ref();
1753 } else if (argc != 2)
1754 usage_ref();
1756 #ifndef PROFILE
1757 if (do_list) {
1758 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1759 NULL) == -1)
1760 err(1, "pledge");
1761 } else {
1762 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1763 "sendfd unveil", NULL) == -1)
1764 err(1, "pledge");
1766 #endif
1767 cwd = getcwd(NULL, 0);
1768 if (cwd == NULL) {
1769 error = got_error_from_errno();
1770 goto done;
1773 if (repo_path == NULL) {
1774 error = got_worktree_open(&worktree, cwd);
1775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1776 goto done;
1777 else
1778 error = NULL;
1779 if (worktree) {
1780 repo_path =
1781 strdup(got_worktree_get_repo_path(worktree));
1782 if (repo_path == NULL)
1783 error = got_error_from_errno();
1784 if (error)
1785 goto done;
1786 } else {
1787 repo_path = strdup(cwd);
1788 if (repo_path == NULL) {
1789 error = got_error_from_errno();
1790 goto done;
1795 error = apply_unveil(repo_path, do_list,
1796 worktree ? got_worktree_get_root_path(worktree) : NULL);
1797 if (error)
1798 goto done;
1800 error = got_repo_open(&repo, repo_path);
1801 if (error != NULL)
1802 goto done;
1804 if (do_list)
1805 error = list_refs(repo);
1806 else if (delref)
1807 error = delete_ref(repo, delref);
1808 else
1809 error = add_ref(repo, argv[0], argv[1]);
1810 done:
1811 if (repo)
1812 got_repo_close(repo);
1813 if (worktree)
1814 got_worktree_close(worktree);
1815 free(cwd);
1816 free(repo_path);
1817 return error;