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 SIMPLEQ_INIT(&refs);
786 #ifndef PROFILE
787 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
788 NULL)
789 == -1)
790 err(1, "pledge");
791 #endif
793 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
794 switch (ch) {
795 case 'p':
796 show_patch = 1;
797 break;
798 case 'c':
799 start_commit = optarg;
800 break;
801 case 'C':
802 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
803 &errstr);
804 if (errstr != NULL)
805 err(1, "-C option %s", errstr);
806 break;
807 case 'l':
808 limit = strtonum(optarg, 1, INT_MAX, &errstr);
809 if (errstr != NULL)
810 err(1, "-l option %s", errstr);
811 break;
812 case 'f':
813 first_parent_traversal = 1;
814 break;
815 case 'r':
816 repo_path = realpath(optarg, NULL);
817 if (repo_path == NULL)
818 err(1, "-r option");
819 break;
820 default:
821 usage_log();
822 /* NOTREACHED */
826 argc -= optind;
827 argv += optind;
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 if (argc == 0) {
841 path = strdup("");
842 if (path == NULL) {
843 error = got_error_from_errno();
844 goto done;
846 } else if (argc == 1) {
847 if (worktree) {
848 error = got_worktree_resolve_path(&path, worktree,
849 argv[0]);
850 if (error)
851 goto done;
852 } else {
853 path = strdup(argv[0]);
854 if (path == NULL) {
855 error = got_error_from_errno();
856 goto done;
859 } else
860 usage_log();
862 repo_path = worktree ?
863 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
864 if (repo_path == NULL) {
865 error = got_error_from_errno();
866 goto done;
869 error = apply_unveil(repo_path, 1,
870 worktree ? got_worktree_get_root_path(worktree) : NULL);
871 if (error)
872 goto done;
874 error = got_repo_open(&repo, repo_path);
875 if (error != NULL)
876 goto done;
878 if (start_commit == NULL) {
879 struct got_reference *head_ref;
880 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
881 if (error != NULL)
882 return error;
883 error = got_ref_resolve(&id, repo, head_ref);
884 got_ref_close(head_ref);
885 if (error != NULL)
886 return error;
887 error = got_object_open_as_commit(&commit, repo, id);
888 } else {
889 struct got_reference *ref;
890 error = got_ref_open(&ref, repo, start_commit);
891 if (error == NULL) {
892 int obj_type;
893 error = got_ref_resolve(&id, repo, ref);
894 got_ref_close(ref);
895 if (error != NULL)
896 goto done;
897 error = got_object_get_type(&obj_type, repo, id);
898 if (error != NULL)
899 goto done;
900 if (obj_type == GOT_OBJ_TYPE_TAG) {
901 struct got_tag_object *tag;
902 error = got_object_open_as_tag(&tag, repo, id);
903 if (error != NULL)
904 goto done;
905 if (got_object_tag_get_object_type(tag) !=
906 GOT_OBJ_TYPE_COMMIT) {
907 got_object_tag_close(tag);
908 error = got_error(GOT_ERR_OBJ_TYPE);
909 goto done;
911 free(id);
912 id = got_object_id_dup(
913 got_object_tag_get_object_id(tag));
914 if (id == NULL)
915 error = got_error_from_errno();
916 got_object_tag_close(tag);
917 if (error)
918 goto done;
919 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
920 error = got_error(GOT_ERR_OBJ_TYPE);
921 goto done;
923 error = got_object_open_as_commit(&commit, repo, id);
924 if (error != NULL)
925 goto done;
927 if (commit == NULL) {
928 error = got_object_resolve_id_str(&id, repo,
929 start_commit);
930 if (error != NULL)
931 return error;
934 if (error != NULL)
935 goto done;
937 error = got_repo_map_path(&in_repo_path, repo, path, 1);
938 if (error != NULL)
939 goto done;
940 if (in_repo_path) {
941 free(path);
942 path = in_repo_path;
945 error = got_ref_list(&refs, repo);
946 if (error)
947 goto done;
949 error = print_commits(id, repo, path, show_patch,
950 diff_context, limit, first_parent_traversal, &refs);
951 done:
952 free(path);
953 free(repo_path);
954 free(cwd);
955 free(id);
956 if (worktree)
957 got_worktree_close(worktree);
958 if (repo) {
959 const struct got_error *repo_error;
960 repo_error = got_repo_close(repo);
961 if (error == NULL)
962 error = repo_error;
964 got_ref_list_free(&refs);
965 return error;
968 __dead static void
969 usage_diff(void)
971 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
972 "[object1 object2 | path]\n", getprogname());
973 exit(1);
976 struct print_diff_arg {
977 struct got_repository *repo;
978 struct got_worktree *worktree;
979 int diff_context;
980 const char *id_str;
981 int header_shown;
982 };
984 static const struct got_error *
985 print_diff(void *arg, unsigned char status, const char *path,
986 struct got_object_id *id)
988 struct print_diff_arg *a = arg;
989 const struct got_error *err = NULL;
990 struct got_blob_object *blob1 = NULL;
991 FILE *f2 = NULL;
992 char *abspath = NULL;
993 struct stat sb;
995 if (status != GOT_STATUS_MODIFY)
996 return NULL;
998 if (!a->header_shown) {
999 printf("diff %s %s\n", a->id_str,
1000 got_worktree_get_root_path(a->worktree));
1001 a->header_shown = 1;
1004 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1005 if (err)
1006 goto done;
1008 if (asprintf(&abspath, "%s/%s",
1009 got_worktree_get_root_path(a->worktree), path) == -1) {
1010 err = got_error_from_errno();
1011 goto done;
1014 f2 = fopen(abspath, "r");
1015 if (f2 == NULL) {
1016 err = got_error_from_errno();
1017 goto done;
1019 if (lstat(abspath, &sb) == -1) {
1020 err = got_error_from_errno();
1021 goto done;
1024 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1025 stdout);
1026 done:
1027 if (blob1)
1028 got_object_blob_close(blob1);
1029 if (f2 && fclose(f2) != 0 && err == NULL)
1030 err = got_error_from_errno();
1031 free(abspath);
1032 return err;
1035 static const struct got_error *
1036 cmd_diff(int argc, char *argv[])
1038 const struct got_error *error;
1039 struct got_repository *repo = NULL;
1040 struct got_worktree *worktree = NULL;
1041 char *cwd = NULL, *repo_path = NULL;
1042 struct got_object_id *id1 = NULL, *id2 = NULL;
1043 char *id_str1 = NULL, *id_str2 = NULL;
1044 int type1, type2;
1045 int diff_context = 3, ch;
1046 const char *errstr;
1047 char *path = NULL;
1049 #ifndef PROFILE
1050 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1051 NULL) == -1)
1052 err(1, "pledge");
1053 #endif
1055 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1056 switch (ch) {
1057 case 'C':
1058 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1059 if (errstr != NULL)
1060 err(1, "-C option %s", errstr);
1061 break;
1062 case 'r':
1063 repo_path = realpath(optarg, NULL);
1064 if (repo_path == NULL)
1065 err(1, "-r option");
1066 break;
1067 default:
1068 usage_diff();
1069 /* NOTREACHED */
1073 argc -= optind;
1074 argv += optind;
1076 cwd = getcwd(NULL, 0);
1077 if (cwd == NULL) {
1078 error = got_error_from_errno();
1079 goto done;
1081 error = got_worktree_open(&worktree, cwd);
1082 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1083 goto done;
1084 if (argc <= 1) {
1085 if (worktree == NULL) {
1086 error = got_error(GOT_ERR_NOT_WORKTREE);
1087 goto done;
1089 if (repo_path)
1090 errx(1,
1091 "-r option can't be used when diffing a work tree");
1092 repo_path = strdup(got_worktree_get_repo_path(worktree));
1093 if (repo_path == NULL) {
1094 error = got_error_from_errno();
1095 goto done;
1097 if (argc == 1) {
1098 error = got_worktree_resolve_path(&path, worktree,
1099 argv[0]);
1100 if (error)
1101 goto done;
1102 } else {
1103 path = strdup("");
1104 if (path == NULL) {
1105 error = got_error_from_errno();
1106 goto done;
1109 } else if (argc == 2) {
1110 id_str1 = argv[0];
1111 id_str2 = argv[1];
1112 } else
1113 usage_diff();
1115 if (repo_path == NULL) {
1116 repo_path = getcwd(NULL, 0);
1117 if (repo_path == NULL)
1118 return got_error_from_errno();
1121 error = apply_unveil(repo_path, 1,
1122 worktree ? got_worktree_get_root_path(worktree) : NULL);
1123 if (error)
1124 goto done;
1126 error = got_repo_open(&repo, repo_path);
1127 free(repo_path);
1128 if (error != NULL)
1129 goto done;
1131 if (worktree) {
1132 struct print_diff_arg arg;
1133 char *id_str;
1134 error = got_object_id_str(&id_str,
1135 got_worktree_get_base_commit_id(worktree));
1136 if (error)
1137 goto done;
1138 arg.repo = repo;
1139 arg.worktree = worktree;
1140 arg.diff_context = diff_context;
1141 arg.id_str = id_str;
1142 arg.header_shown = 0;
1144 error = got_worktree_status(worktree, path, repo, print_diff,
1145 &arg, check_cancelled, NULL);
1146 free(id_str);
1147 goto done;
1150 error = got_object_resolve_id_str(&id1, repo, id_str1);
1151 if (error)
1152 goto done;
1154 error = got_object_resolve_id_str(&id2, repo, id_str2);
1155 if (error)
1156 goto done;
1158 error = got_object_get_type(&type1, repo, id1);
1159 if (error)
1160 goto done;
1162 error = got_object_get_type(&type2, repo, id2);
1163 if (error)
1164 goto done;
1166 if (type1 != type2) {
1167 error = got_error(GOT_ERR_OBJ_TYPE);
1168 goto done;
1171 switch (type1) {
1172 case GOT_OBJ_TYPE_BLOB:
1173 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1174 diff_context, repo, stdout);
1175 break;
1176 case GOT_OBJ_TYPE_TREE:
1177 error = got_diff_objects_as_trees(id1, id2, "", "",
1178 diff_context, repo, stdout);
1179 break;
1180 case GOT_OBJ_TYPE_COMMIT:
1181 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1182 id_str2);
1183 error = got_diff_objects_as_commits(id1, id2, diff_context,
1184 repo, stdout);
1185 break;
1186 default:
1187 error = got_error(GOT_ERR_OBJ_TYPE);
1190 done:
1191 free(id1);
1192 free(id2);
1193 free(path);
1194 if (worktree)
1195 got_worktree_close(worktree);
1196 if (repo) {
1197 const struct got_error *repo_error;
1198 repo_error = got_repo_close(repo);
1199 if (error == NULL)
1200 error = repo_error;
1202 return error;
1205 __dead static void
1206 usage_blame(void)
1208 fprintf(stderr,
1209 "usage: %s blame [-c commit] [-r repository-path] path\n",
1210 getprogname());
1211 exit(1);
1214 static const struct got_error *
1215 cmd_blame(int argc, char *argv[])
1217 const struct got_error *error;
1218 struct got_repository *repo = NULL;
1219 struct got_worktree *worktree = NULL;
1220 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1221 struct got_object_id *commit_id = NULL;
1222 char *commit_id_str = NULL;
1223 int ch;
1225 #ifndef PROFILE
1226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1227 NULL) == -1)
1228 err(1, "pledge");
1229 #endif
1231 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1232 switch (ch) {
1233 case 'c':
1234 commit_id_str = optarg;
1235 break;
1236 case 'r':
1237 repo_path = realpath(optarg, NULL);
1238 if (repo_path == NULL)
1239 err(1, "-r option");
1240 break;
1241 default:
1242 usage_blame();
1243 /* NOTREACHED */
1247 argc -= optind;
1248 argv += optind;
1250 if (argc == 1)
1251 path = argv[0];
1252 else
1253 usage_blame();
1255 cwd = getcwd(NULL, 0);
1256 if (cwd == NULL) {
1257 error = got_error_from_errno();
1258 goto done;
1260 if (repo_path == NULL) {
1261 error = got_worktree_open(&worktree, cwd);
1262 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1263 goto done;
1264 else
1265 error = NULL;
1266 if (worktree) {
1267 repo_path =
1268 strdup(got_worktree_get_repo_path(worktree));
1269 if (repo_path == NULL)
1270 error = got_error_from_errno();
1271 if (error)
1272 goto done;
1273 } else {
1274 repo_path = strdup(cwd);
1275 if (repo_path == NULL) {
1276 error = got_error_from_errno();
1277 goto done;
1282 error = apply_unveil(repo_path, 1, NULL);
1283 if (error)
1284 goto done;
1286 error = got_repo_open(&repo, repo_path);
1287 if (error != NULL)
1288 goto done;
1290 if (worktree) {
1291 const char *prefix = got_worktree_get_path_prefix(worktree);
1292 char *p, *worktree_subdir = cwd +
1293 strlen(got_worktree_get_root_path(worktree));
1294 if (asprintf(&p, "%s%s%s%s%s",
1295 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1296 worktree_subdir, worktree_subdir[0] ? "/" : "",
1297 path) == -1) {
1298 error = got_error_from_errno();
1299 goto done;
1301 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1302 free(p);
1303 } else {
1304 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1306 if (error)
1307 goto done;
1309 if (commit_id_str == NULL) {
1310 struct got_reference *head_ref;
1311 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1312 if (error != NULL)
1313 goto done;
1314 error = got_ref_resolve(&commit_id, repo, head_ref);
1315 got_ref_close(head_ref);
1316 if (error != NULL)
1317 goto done;
1318 } else {
1319 error = got_object_resolve_id_str(&commit_id, repo,
1320 commit_id_str);
1321 if (error != NULL)
1322 goto done;
1325 error = got_blame(in_repo_path, commit_id, repo, stdout);
1326 done:
1327 free(in_repo_path);
1328 free(repo_path);
1329 free(cwd);
1330 free(commit_id);
1331 if (worktree)
1332 got_worktree_close(worktree);
1333 if (repo) {
1334 const struct got_error *repo_error;
1335 repo_error = got_repo_close(repo);
1336 if (error == NULL)
1337 error = repo_error;
1339 return error;
1342 __dead static void
1343 usage_tree(void)
1345 fprintf(stderr,
1346 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1347 getprogname());
1348 exit(1);
1351 static void
1352 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1353 const char *root_path)
1355 int is_root_path = (strcmp(path, root_path) == 0);
1357 path += strlen(root_path);
1358 while (path[0] == '/')
1359 path++;
1361 printf("%s%s%s%s%s\n", id ? id : "", path,
1362 is_root_path ? "" : "/", te->name,
1363 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1366 static const struct got_error *
1367 print_tree(const char *path, struct got_object_id *commit_id,
1368 int show_ids, int recurse, const char *root_path,
1369 struct got_repository *repo)
1371 const struct got_error *err = NULL;
1372 struct got_object_id *tree_id = NULL;
1373 struct got_tree_object *tree = NULL;
1374 const struct got_tree_entries *entries;
1375 struct got_tree_entry *te;
1377 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1378 if (err)
1379 goto done;
1381 err = got_object_open_as_tree(&tree, repo, tree_id);
1382 if (err)
1383 goto done;
1384 entries = got_object_tree_get_entries(tree);
1385 te = SIMPLEQ_FIRST(&entries->head);
1386 while (te) {
1387 char *id = NULL;
1389 if (sigint_received || sigpipe_received)
1390 break;
1392 if (show_ids) {
1393 char *id_str;
1394 err = got_object_id_str(&id_str, te->id);
1395 if (err)
1396 goto done;
1397 if (asprintf(&id, "%s ", id_str) == -1) {
1398 err = got_error_from_errno();
1399 free(id_str);
1400 goto done;
1402 free(id_str);
1404 print_entry(te, id, path, root_path);
1405 free(id);
1407 if (recurse && S_ISDIR(te->mode)) {
1408 char *child_path;
1409 if (asprintf(&child_path, "%s%s%s", path,
1410 path[0] == '/' && path[1] == '\0' ? "" : "/",
1411 te->name) == -1) {
1412 err = got_error_from_errno();
1413 goto done;
1415 err = print_tree(child_path, commit_id, show_ids, 1,
1416 root_path, repo);
1417 free(child_path);
1418 if (err)
1419 goto done;
1422 te = SIMPLEQ_NEXT(te, entry);
1424 done:
1425 if (tree)
1426 got_object_tree_close(tree);
1427 free(tree_id);
1428 return err;
1431 static const struct got_error *
1432 cmd_tree(int argc, char *argv[])
1434 const struct got_error *error;
1435 struct got_repository *repo = NULL;
1436 struct got_worktree *worktree = NULL;
1437 const char *path;
1438 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1439 struct got_object_id *commit_id = NULL;
1440 char *commit_id_str = NULL;
1441 int show_ids = 0, recurse = 0;
1442 int ch;
1444 #ifndef PROFILE
1445 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1446 NULL) == -1)
1447 err(1, "pledge");
1448 #endif
1450 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1451 switch (ch) {
1452 case 'c':
1453 commit_id_str = optarg;
1454 break;
1455 case 'r':
1456 repo_path = realpath(optarg, NULL);
1457 if (repo_path == NULL)
1458 err(1, "-r option");
1459 break;
1460 case 'i':
1461 show_ids = 1;
1462 break;
1463 case 'R':
1464 recurse = 1;
1465 break;
1466 default:
1467 usage_tree();
1468 /* NOTREACHED */
1472 argc -= optind;
1473 argv += optind;
1475 if (argc == 1)
1476 path = argv[0];
1477 else if (argc > 1)
1478 usage_tree();
1479 else
1480 path = NULL;
1482 cwd = getcwd(NULL, 0);
1483 if (cwd == NULL) {
1484 error = got_error_from_errno();
1485 goto done;
1487 if (repo_path == NULL) {
1488 error = got_worktree_open(&worktree, cwd);
1489 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1490 goto done;
1491 else
1492 error = NULL;
1493 if (worktree) {
1494 repo_path =
1495 strdup(got_worktree_get_repo_path(worktree));
1496 if (repo_path == NULL)
1497 error = got_error_from_errno();
1498 if (error)
1499 goto done;
1500 } else {
1501 repo_path = strdup(cwd);
1502 if (repo_path == NULL) {
1503 error = got_error_from_errno();
1504 goto done;
1509 error = apply_unveil(repo_path, 1, NULL);
1510 if (error)
1511 goto done;
1513 error = got_repo_open(&repo, repo_path);
1514 if (error != NULL)
1515 goto done;
1517 if (path == NULL) {
1518 if (worktree) {
1519 char *p, *worktree_subdir = cwd +
1520 strlen(got_worktree_get_root_path(worktree));
1521 if (asprintf(&p, "%s/%s",
1522 got_worktree_get_path_prefix(worktree),
1523 worktree_subdir) == -1) {
1524 error = got_error_from_errno();
1525 goto done;
1527 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1528 free(p);
1529 if (error)
1530 goto done;
1531 } else
1532 path = "/";
1534 if (in_repo_path == NULL) {
1535 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1536 if (error != NULL)
1537 goto done;
1540 if (commit_id_str == NULL) {
1541 struct got_reference *head_ref;
1542 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1543 if (error != NULL)
1544 goto done;
1545 error = got_ref_resolve(&commit_id, repo, head_ref);
1546 got_ref_close(head_ref);
1547 if (error != NULL)
1548 goto done;
1549 } else {
1550 error = got_object_resolve_id_str(&commit_id, repo,
1551 commit_id_str);
1552 if (error != NULL)
1553 goto done;
1556 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1557 in_repo_path, repo);
1558 done:
1559 free(in_repo_path);
1560 free(repo_path);
1561 free(cwd);
1562 free(commit_id);
1563 if (worktree)
1564 got_worktree_close(worktree);
1565 if (repo) {
1566 const struct got_error *repo_error;
1567 repo_error = got_repo_close(repo);
1568 if (error == NULL)
1569 error = repo_error;
1571 return error;
1574 __dead static void
1575 usage_status(void)
1577 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1578 exit(1);
1581 static const struct got_error *
1582 print_status(void *arg, unsigned char status, const char *path,
1583 struct got_object_id *id)
1585 printf("%c %s\n", status, path);
1586 return NULL;
1589 static const struct got_error *
1590 cmd_status(int argc, char *argv[])
1592 const struct got_error *error = NULL;
1593 struct got_repository *repo = NULL;
1594 struct got_worktree *worktree = NULL;
1595 char *cwd = NULL, *path = NULL;
1596 int ch;
1598 while ((ch = getopt(argc, argv, "")) != -1) {
1599 switch (ch) {
1600 default:
1601 usage_status();
1602 /* NOTREACHED */
1606 argc -= optind;
1607 argv += optind;
1609 #ifndef PROFILE
1610 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1611 NULL) == -1)
1612 err(1, "pledge");
1613 #endif
1614 cwd = getcwd(NULL, 0);
1615 if (cwd == NULL) {
1616 error = got_error_from_errno();
1617 goto done;
1620 error = got_worktree_open(&worktree, cwd);
1621 if (error != NULL)
1622 goto done;
1624 if (argc == 0) {
1625 path = strdup("");
1626 if (path == NULL) {
1627 error = got_error_from_errno();
1628 goto done;
1630 } else if (argc == 1) {
1631 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1632 if (error)
1633 goto done;
1634 } else
1635 usage_status();
1637 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1638 if (error != NULL)
1639 goto done;
1641 error = apply_unveil(got_repo_get_path(repo), 1,
1642 got_worktree_get_root_path(worktree));
1643 if (error)
1644 goto done;
1646 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1647 check_cancelled, NULL);
1648 done:
1649 free(cwd);
1650 free(path);
1651 return error;
1654 __dead static void
1655 usage_ref(void)
1657 fprintf(stderr,
1658 "usage: %s ref [-r repository] -l | -d name | name object\n",
1659 getprogname());
1660 exit(1);
1663 static const struct got_error *
1664 list_refs(struct got_repository *repo)
1666 static const struct got_error *err = NULL;
1667 struct got_reflist_head refs;
1668 struct got_reflist_entry *re;
1670 SIMPLEQ_INIT(&refs);
1671 err = got_ref_list(&refs, repo);
1672 if (err)
1673 return err;
1675 SIMPLEQ_FOREACH(re, &refs, entry) {
1676 char *refstr;
1677 refstr = got_ref_to_str(re->ref);
1678 if (refstr == NULL)
1679 return got_error_from_errno();
1680 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1681 free(refstr);
1684 got_ref_list_free(&refs);
1685 return NULL;
1688 static const struct got_error *
1689 delete_ref(struct got_repository *repo, const char *refname)
1691 const struct got_error *err = NULL;
1692 struct got_reference *ref;
1694 err = got_ref_open(&ref, repo, refname);
1695 if (err)
1696 return err;
1698 err = got_ref_delete(ref, repo);
1699 got_ref_close(ref);
1700 return err;
1703 static const struct got_error *
1704 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1706 const struct got_error *err = NULL;
1707 struct got_object_id *id;
1708 struct got_reference *ref = NULL;
1710 err = got_object_resolve_id_str(&id, repo, id_str);
1711 if (err)
1712 return err;
1714 err = got_ref_alloc(&ref, refname, id);
1715 if (err)
1716 goto done;
1718 err = got_ref_write(ref, repo);
1719 done:
1720 if (ref)
1721 got_ref_close(ref);
1722 free(id);
1723 return err;
1726 static const struct got_error *
1727 cmd_ref(int argc, char *argv[])
1729 const struct got_error *error = NULL;
1730 struct got_repository *repo = NULL;
1731 struct got_worktree *worktree = NULL;
1732 char *cwd = NULL, *repo_path = NULL;
1733 int ch, do_list = 0;
1734 const char *delref = NULL;
1736 /* TODO: Add -s option for adding symbolic references. */
1737 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1738 switch (ch) {
1739 case 'd':
1740 delref = optarg;
1741 break;
1742 case 'r':
1743 repo_path = realpath(optarg, NULL);
1744 if (repo_path == NULL)
1745 err(1, "-r option");
1746 break;
1747 case 'l':
1748 do_list = 1;
1749 break;
1750 default:
1751 usage_ref();
1752 /* NOTREACHED */
1756 if (do_list && delref)
1757 errx(1, "-l and -d options are mutually exclusive\n");
1759 argc -= optind;
1760 argv += optind;
1762 if (do_list || delref) {
1763 if (argc > 0)
1764 usage_ref();
1765 } else if (argc != 2)
1766 usage_ref();
1768 #ifndef PROFILE
1769 if (do_list) {
1770 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1771 NULL) == -1)
1772 err(1, "pledge");
1773 } else {
1774 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1775 "sendfd unveil", NULL) == -1)
1776 err(1, "pledge");
1778 #endif
1779 cwd = getcwd(NULL, 0);
1780 if (cwd == NULL) {
1781 error = got_error_from_errno();
1782 goto done;
1785 if (repo_path == NULL) {
1786 error = got_worktree_open(&worktree, cwd);
1787 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1788 goto done;
1789 else
1790 error = NULL;
1791 if (worktree) {
1792 repo_path =
1793 strdup(got_worktree_get_repo_path(worktree));
1794 if (repo_path == NULL)
1795 error = got_error_from_errno();
1796 if (error)
1797 goto done;
1798 } else {
1799 repo_path = strdup(cwd);
1800 if (repo_path == NULL) {
1801 error = got_error_from_errno();
1802 goto done;
1807 error = apply_unveil(repo_path, do_list,
1808 worktree ? got_worktree_get_root_path(worktree) : NULL);
1809 if (error)
1810 goto done;
1812 error = got_repo_open(&repo, repo_path);
1813 if (error != NULL)
1814 goto done;
1816 if (do_list)
1817 error = list_refs(repo);
1818 else if (delref)
1819 error = delete_ref(repo, delref);
1820 else
1821 error = add_ref(repo, argv[0], argv[1]);
1822 done:
1823 if (repo)
1824 got_repo_close(repo);
1825 if (worktree)
1826 got_worktree_close(worktree);
1827 free(cwd);
1828 free(repo_path);
1829 return error;