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 if (argc == 0)
828 path = strdup("");
829 else if (argc == 1)
830 path = strdup(argv[0]);
831 else
832 usage_log();
833 if (path == NULL)
834 return got_error_from_errno();
836 cwd = getcwd(NULL, 0);
837 if (cwd == NULL) {
838 error = got_error_from_errno();
839 goto done;
842 error = got_worktree_open(&worktree, cwd);
843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
844 goto done;
845 error = NULL;
847 repo_path = worktree ?
848 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
849 if (repo_path == NULL) {
850 error = got_error_from_errno();
851 goto done;
854 error = apply_unveil(repo_path, 1,
855 worktree ? got_worktree_get_root_path(worktree) : NULL);
856 if (error)
857 goto done;
859 error = got_repo_open(&repo, repo_path);
860 if (error != NULL)
861 goto done;
863 if (start_commit == NULL) {
864 struct got_reference *head_ref;
865 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
866 if (error != NULL)
867 return error;
868 error = got_ref_resolve(&id, repo, head_ref);
869 got_ref_close(head_ref);
870 if (error != NULL)
871 return error;
872 error = got_object_open_as_commit(&commit, repo, id);
873 } else {
874 struct got_reference *ref;
875 error = got_ref_open(&ref, repo, start_commit);
876 if (error == NULL) {
877 int obj_type;
878 error = got_ref_resolve(&id, repo, ref);
879 got_ref_close(ref);
880 if (error != NULL)
881 goto done;
882 error = got_object_get_type(&obj_type, repo, id);
883 if (error != NULL)
884 goto done;
885 if (obj_type == GOT_OBJ_TYPE_TAG) {
886 struct got_tag_object *tag;
887 error = got_object_open_as_tag(&tag, repo, id);
888 if (error != NULL)
889 goto done;
890 if (got_object_tag_get_object_type(tag) !=
891 GOT_OBJ_TYPE_COMMIT) {
892 got_object_tag_close(tag);
893 error = got_error(GOT_ERR_OBJ_TYPE);
894 goto done;
896 free(id);
897 id = got_object_id_dup(
898 got_object_tag_get_object_id(tag));
899 if (id == NULL)
900 error = got_error_from_errno();
901 got_object_tag_close(tag);
902 if (error)
903 goto done;
904 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
905 error = got_error(GOT_ERR_OBJ_TYPE);
906 goto done;
908 error = got_object_open_as_commit(&commit, repo, id);
909 if (error != NULL)
910 goto done;
912 if (commit == NULL) {
913 error = got_object_resolve_id_str(&id, repo,
914 start_commit);
915 if (error != NULL)
916 return error;
919 if (error != NULL)
920 goto done;
922 error = got_repo_map_path(&in_repo_path, repo, path, 1);
923 if (error != NULL)
924 goto done;
925 if (in_repo_path) {
926 free(path);
927 path = in_repo_path;
930 SIMPLEQ_INIT(&refs);
931 error = got_ref_list(&refs, repo);
932 if (error)
933 goto done;
935 error = print_commits(id, repo, path, show_patch,
936 diff_context, limit, first_parent_traversal, &refs);
937 done:
938 free(path);
939 free(repo_path);
940 free(cwd);
941 free(id);
942 if (worktree)
943 got_worktree_close(worktree);
944 if (repo) {
945 const struct got_error *repo_error;
946 repo_error = got_repo_close(repo);
947 if (error == NULL)
948 error = repo_error;
950 got_ref_list_free(&refs);
951 return error;
954 __dead static void
955 usage_diff(void)
957 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
958 "[object1 object2 | path]\n", getprogname());
959 exit(1);
962 struct print_diff_arg {
963 struct got_repository *repo;
964 struct got_worktree *worktree;
965 int diff_context;
966 const char *id_str;
967 int header_shown;
968 };
970 static const struct got_error *
971 print_diff(void *arg, unsigned char status, const char *path,
972 struct got_object_id *id)
974 struct print_diff_arg *a = arg;
975 const struct got_error *err = NULL;
976 struct got_blob_object *blob1 = NULL;
977 FILE *f2 = NULL;
978 char *abspath = NULL;
979 struct stat sb;
981 if (status != GOT_STATUS_MODIFY)
982 return NULL;
984 if (!a->header_shown) {
985 printf("diff %s %s\n", a->id_str,
986 got_worktree_get_root_path(a->worktree));
987 a->header_shown = 1;
990 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
991 if (err)
992 goto done;
994 if (asprintf(&abspath, "%s/%s",
995 got_worktree_get_root_path(a->worktree), path) == -1) {
996 err = got_error_from_errno();
997 goto done;
1000 f2 = fopen(abspath, "r");
1001 if (f2 == NULL) {
1002 err = got_error_from_errno();
1003 goto done;
1005 if (lstat(abspath, &sb) == -1) {
1006 err = got_error_from_errno();
1007 goto done;
1010 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1011 stdout);
1012 done:
1013 if (blob1)
1014 got_object_blob_close(blob1);
1015 if (f2 && fclose(f2) != 0 && err == NULL)
1016 err = got_error_from_errno();
1017 free(abspath);
1018 return err;
1021 static const struct got_error *
1022 get_status_path(char **status_path, struct got_worktree *worktree,
1023 const char *arg)
1025 const struct got_error *err = NULL;
1026 char *resolved, *path = NULL;
1027 size_t len;
1029 *status_path = NULL;
1031 resolved = realpath(arg, NULL);
1032 if (resolved == NULL)
1033 return got_error_from_errno();
1035 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1036 strlen(got_worktree_get_root_path(worktree)))) {
1037 err = got_error(GOT_ERR_BAD_PATH);
1038 goto done;
1041 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1042 if (path == NULL) {
1043 err = got_error_from_errno();
1044 goto done;
1047 /* XXX status walk can't deal with trailing slash! */
1048 len = strlen(path);
1049 while (path[len - 1] == '/') {
1050 path[len - 1] = '\0';
1051 len--;
1053 done:
1054 free(resolved);
1055 if (err == NULL)
1056 *status_path = path;
1057 else
1058 free(path);
1059 return err;
1062 static const struct got_error *
1063 cmd_diff(int argc, char *argv[])
1065 const struct got_error *error;
1066 struct got_repository *repo = NULL;
1067 struct got_worktree *worktree = NULL;
1068 char *cwd = NULL, *repo_path = NULL;
1069 struct got_object_id *id1 = NULL, *id2 = NULL;
1070 char *id_str1 = NULL, *id_str2 = NULL;
1071 int type1, type2;
1072 int diff_context = 3, ch;
1073 const char *errstr;
1074 char *path = NULL;
1076 #ifndef PROFILE
1077 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1078 NULL) == -1)
1079 err(1, "pledge");
1080 #endif
1082 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1083 switch (ch) {
1084 case 'C':
1085 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1086 if (errstr != NULL)
1087 err(1, "-C option %s", errstr);
1088 break;
1089 case 'r':
1090 repo_path = realpath(optarg, NULL);
1091 if (repo_path == NULL)
1092 err(1, "-r option");
1093 break;
1094 default:
1095 usage_diff();
1096 /* NOTREACHED */
1100 argc -= optind;
1101 argv += optind;
1103 cwd = getcwd(NULL, 0);
1104 if (cwd == NULL) {
1105 error = got_error_from_errno();
1106 goto done;
1108 error = got_worktree_open(&worktree, cwd);
1109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1110 goto done;
1111 if (argc <= 1) {
1112 if (worktree == NULL) {
1113 error = got_error(GOT_ERR_NOT_WORKTREE);
1114 goto done;
1116 if (repo_path)
1117 errx(1,
1118 "-r option can't be used when diffing a work tree");
1119 repo_path = strdup(got_worktree_get_repo_path(worktree));
1120 if (repo_path == NULL) {
1121 error = got_error_from_errno();
1122 goto done;
1124 if (argc == 1) {
1125 error = get_status_path(&path, worktree, argv[0]);
1126 if (error)
1127 goto done;
1128 } else {
1129 path = strdup("");
1130 if (path == NULL) {
1131 error = got_error_from_errno();
1132 goto done;
1135 } else if (argc == 2) {
1136 id_str1 = argv[0];
1137 id_str2 = argv[1];
1138 } else
1139 usage_diff();
1141 if (repo_path == NULL) {
1142 repo_path = getcwd(NULL, 0);
1143 if (repo_path == NULL)
1144 return got_error_from_errno();
1147 error = apply_unveil(repo_path, 1,
1148 worktree ? got_worktree_get_root_path(worktree) : NULL);
1149 if (error)
1150 goto done;
1152 error = got_repo_open(&repo, repo_path);
1153 free(repo_path);
1154 if (error != NULL)
1155 goto done;
1157 if (worktree) {
1158 struct print_diff_arg arg;
1159 char *id_str;
1160 error = got_object_id_str(&id_str,
1161 got_worktree_get_base_commit_id(worktree));
1162 if (error)
1163 goto done;
1164 arg.repo = repo;
1165 arg.worktree = worktree;
1166 arg.diff_context = diff_context;
1167 arg.id_str = id_str;
1168 arg.header_shown = 0;
1170 error = got_worktree_status(worktree, path, repo, print_diff,
1171 &arg, check_cancelled, NULL);
1172 free(id_str);
1173 goto done;
1176 error = got_object_resolve_id_str(&id1, repo, id_str1);
1177 if (error)
1178 goto done;
1180 error = got_object_resolve_id_str(&id2, repo, id_str2);
1181 if (error)
1182 goto done;
1184 error = got_object_get_type(&type1, repo, id1);
1185 if (error)
1186 goto done;
1188 error = got_object_get_type(&type2, repo, id2);
1189 if (error)
1190 goto done;
1192 if (type1 != type2) {
1193 error = got_error(GOT_ERR_OBJ_TYPE);
1194 goto done;
1197 switch (type1) {
1198 case GOT_OBJ_TYPE_BLOB:
1199 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1200 diff_context, repo, stdout);
1201 break;
1202 case GOT_OBJ_TYPE_TREE:
1203 error = got_diff_objects_as_trees(id1, id2, "", "",
1204 diff_context, repo, stdout);
1205 break;
1206 case GOT_OBJ_TYPE_COMMIT:
1207 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1208 id_str2);
1209 error = got_diff_objects_as_commits(id1, id2, diff_context,
1210 repo, stdout);
1211 break;
1212 default:
1213 error = got_error(GOT_ERR_OBJ_TYPE);
1216 done:
1217 free(id1);
1218 free(id2);
1219 free(path);
1220 if (worktree)
1221 got_worktree_close(worktree);
1222 if (repo) {
1223 const struct got_error *repo_error;
1224 repo_error = got_repo_close(repo);
1225 if (error == NULL)
1226 error = repo_error;
1228 return error;
1231 __dead static void
1232 usage_blame(void)
1234 fprintf(stderr,
1235 "usage: %s blame [-c commit] [-r repository-path] path\n",
1236 getprogname());
1237 exit(1);
1240 static const struct got_error *
1241 cmd_blame(int argc, char *argv[])
1243 const struct got_error *error;
1244 struct got_repository *repo = NULL;
1245 struct got_worktree *worktree = NULL;
1246 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1247 struct got_object_id *commit_id = NULL;
1248 char *commit_id_str = NULL;
1249 int ch;
1251 #ifndef PROFILE
1252 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1253 NULL) == -1)
1254 err(1, "pledge");
1255 #endif
1257 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1258 switch (ch) {
1259 case 'c':
1260 commit_id_str = optarg;
1261 break;
1262 case 'r':
1263 repo_path = realpath(optarg, NULL);
1264 if (repo_path == NULL)
1265 err(1, "-r option");
1266 break;
1267 default:
1268 usage_blame();
1269 /* NOTREACHED */
1273 argc -= optind;
1274 argv += optind;
1276 if (argc == 1)
1277 path = argv[0];
1278 else
1279 usage_blame();
1281 cwd = getcwd(NULL, 0);
1282 if (cwd == NULL) {
1283 error = got_error_from_errno();
1284 goto done;
1286 if (repo_path == NULL) {
1287 error = got_worktree_open(&worktree, cwd);
1288 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1289 goto done;
1290 else
1291 error = NULL;
1292 if (worktree) {
1293 repo_path =
1294 strdup(got_worktree_get_repo_path(worktree));
1295 if (repo_path == NULL)
1296 error = got_error_from_errno();
1297 if (error)
1298 goto done;
1299 } else {
1300 repo_path = strdup(cwd);
1301 if (repo_path == NULL) {
1302 error = got_error_from_errno();
1303 goto done;
1308 error = apply_unveil(repo_path, 1, NULL);
1309 if (error)
1310 goto done;
1312 error = got_repo_open(&repo, repo_path);
1313 if (error != NULL)
1314 goto done;
1316 if (worktree) {
1317 const char *prefix = got_worktree_get_path_prefix(worktree);
1318 char *p, *worktree_subdir = cwd +
1319 strlen(got_worktree_get_root_path(worktree));
1320 if (asprintf(&p, "%s%s%s%s%s",
1321 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1322 worktree_subdir, worktree_subdir[0] ? "/" : "",
1323 path) == -1) {
1324 error = got_error_from_errno();
1325 goto done;
1327 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1328 free(p);
1329 } else {
1330 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1332 if (error)
1333 goto done;
1335 if (commit_id_str == NULL) {
1336 struct got_reference *head_ref;
1337 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1338 if (error != NULL)
1339 goto done;
1340 error = got_ref_resolve(&commit_id, repo, head_ref);
1341 got_ref_close(head_ref);
1342 if (error != NULL)
1343 goto done;
1344 } else {
1345 error = got_object_resolve_id_str(&commit_id, repo,
1346 commit_id_str);
1347 if (error != NULL)
1348 goto done;
1351 error = got_blame(in_repo_path, commit_id, repo, stdout);
1352 done:
1353 free(in_repo_path);
1354 free(repo_path);
1355 free(cwd);
1356 free(commit_id);
1357 if (worktree)
1358 got_worktree_close(worktree);
1359 if (repo) {
1360 const struct got_error *repo_error;
1361 repo_error = got_repo_close(repo);
1362 if (error == NULL)
1363 error = repo_error;
1365 return error;
1368 __dead static void
1369 usage_tree(void)
1371 fprintf(stderr,
1372 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1373 getprogname());
1374 exit(1);
1377 static void
1378 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1379 const char *root_path)
1381 int is_root_path = (strcmp(path, root_path) == 0);
1383 path += strlen(root_path);
1384 while (path[0] == '/')
1385 path++;
1387 printf("%s%s%s%s%s\n", id ? id : "", path,
1388 is_root_path ? "" : "/", te->name,
1389 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1392 static const struct got_error *
1393 print_tree(const char *path, struct got_object_id *commit_id,
1394 int show_ids, int recurse, const char *root_path,
1395 struct got_repository *repo)
1397 const struct got_error *err = NULL;
1398 struct got_object_id *tree_id = NULL;
1399 struct got_tree_object *tree = NULL;
1400 const struct got_tree_entries *entries;
1401 struct got_tree_entry *te;
1403 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1404 if (err)
1405 goto done;
1407 err = got_object_open_as_tree(&tree, repo, tree_id);
1408 if (err)
1409 goto done;
1410 entries = got_object_tree_get_entries(tree);
1411 te = SIMPLEQ_FIRST(&entries->head);
1412 while (te) {
1413 char *id = NULL;
1415 if (sigint_received || sigpipe_received)
1416 break;
1418 if (show_ids) {
1419 char *id_str;
1420 err = got_object_id_str(&id_str, te->id);
1421 if (err)
1422 goto done;
1423 if (asprintf(&id, "%s ", id_str) == -1) {
1424 err = got_error_from_errno();
1425 free(id_str);
1426 goto done;
1428 free(id_str);
1430 print_entry(te, id, path, root_path);
1431 free(id);
1433 if (recurse && S_ISDIR(te->mode)) {
1434 char *child_path;
1435 if (asprintf(&child_path, "%s%s%s", path,
1436 path[0] == '/' && path[1] == '\0' ? "" : "/",
1437 te->name) == -1) {
1438 err = got_error_from_errno();
1439 goto done;
1441 err = print_tree(child_path, commit_id, show_ids, 1,
1442 root_path, repo);
1443 free(child_path);
1444 if (err)
1445 goto done;
1448 te = SIMPLEQ_NEXT(te, entry);
1450 done:
1451 if (tree)
1452 got_object_tree_close(tree);
1453 free(tree_id);
1454 return err;
1457 static const struct got_error *
1458 cmd_tree(int argc, char *argv[])
1460 const struct got_error *error;
1461 struct got_repository *repo = NULL;
1462 struct got_worktree *worktree = NULL;
1463 const char *path;
1464 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1465 struct got_object_id *commit_id = NULL;
1466 char *commit_id_str = NULL;
1467 int show_ids = 0, recurse = 0;
1468 int ch;
1470 #ifndef PROFILE
1471 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1472 NULL) == -1)
1473 err(1, "pledge");
1474 #endif
1476 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1477 switch (ch) {
1478 case 'c':
1479 commit_id_str = optarg;
1480 break;
1481 case 'r':
1482 repo_path = realpath(optarg, NULL);
1483 if (repo_path == NULL)
1484 err(1, "-r option");
1485 break;
1486 case 'i':
1487 show_ids = 1;
1488 break;
1489 case 'R':
1490 recurse = 1;
1491 break;
1492 default:
1493 usage_tree();
1494 /* NOTREACHED */
1498 argc -= optind;
1499 argv += optind;
1501 if (argc == 1)
1502 path = argv[0];
1503 else if (argc > 1)
1504 usage_tree();
1505 else
1506 path = NULL;
1508 cwd = getcwd(NULL, 0);
1509 if (cwd == NULL) {
1510 error = got_error_from_errno();
1511 goto done;
1513 if (repo_path == NULL) {
1514 error = got_worktree_open(&worktree, cwd);
1515 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1516 goto done;
1517 else
1518 error = NULL;
1519 if (worktree) {
1520 repo_path =
1521 strdup(got_worktree_get_repo_path(worktree));
1522 if (repo_path == NULL)
1523 error = got_error_from_errno();
1524 if (error)
1525 goto done;
1526 } else {
1527 repo_path = strdup(cwd);
1528 if (repo_path == NULL) {
1529 error = got_error_from_errno();
1530 goto done;
1535 error = apply_unveil(repo_path, 1, NULL);
1536 if (error)
1537 goto done;
1539 error = got_repo_open(&repo, repo_path);
1540 if (error != NULL)
1541 goto done;
1543 if (path == NULL) {
1544 if (worktree) {
1545 char *p, *worktree_subdir = cwd +
1546 strlen(got_worktree_get_root_path(worktree));
1547 if (asprintf(&p, "%s/%s",
1548 got_worktree_get_path_prefix(worktree),
1549 worktree_subdir) == -1) {
1550 error = got_error_from_errno();
1551 goto done;
1553 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1554 free(p);
1555 if (error)
1556 goto done;
1557 } else
1558 path = "/";
1560 if (in_repo_path == NULL) {
1561 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1562 if (error != NULL)
1563 goto done;
1566 if (commit_id_str == NULL) {
1567 struct got_reference *head_ref;
1568 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1569 if (error != NULL)
1570 goto done;
1571 error = got_ref_resolve(&commit_id, repo, head_ref);
1572 got_ref_close(head_ref);
1573 if (error != NULL)
1574 goto done;
1575 } else {
1576 error = got_object_resolve_id_str(&commit_id, repo,
1577 commit_id_str);
1578 if (error != NULL)
1579 goto done;
1582 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1583 in_repo_path, repo);
1584 done:
1585 free(in_repo_path);
1586 free(repo_path);
1587 free(cwd);
1588 free(commit_id);
1589 if (worktree)
1590 got_worktree_close(worktree);
1591 if (repo) {
1592 const struct got_error *repo_error;
1593 repo_error = got_repo_close(repo);
1594 if (error == NULL)
1595 error = repo_error;
1597 return error;
1600 __dead static void
1601 usage_status(void)
1603 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1604 exit(1);
1607 static const struct got_error *
1608 print_status(void *arg, unsigned char status, const char *path,
1609 struct got_object_id *id)
1611 printf("%c %s\n", status, path);
1612 return NULL;
1615 static const struct got_error *
1616 cmd_status(int argc, char *argv[])
1618 const struct got_error *error = NULL;
1619 struct got_repository *repo = NULL;
1620 struct got_worktree *worktree = NULL;
1621 char *cwd = NULL, *path = NULL;
1622 int ch;
1624 while ((ch = getopt(argc, argv, "")) != -1) {
1625 switch (ch) {
1626 default:
1627 usage_status();
1628 /* NOTREACHED */
1632 argc -= optind;
1633 argv += optind;
1635 #ifndef PROFILE
1636 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1637 NULL) == -1)
1638 err(1, "pledge");
1639 #endif
1640 cwd = getcwd(NULL, 0);
1641 if (cwd == NULL) {
1642 error = got_error_from_errno();
1643 goto done;
1646 error = got_worktree_open(&worktree, cwd);
1647 if (error != NULL)
1648 goto done;
1650 if (argc == 0) {
1651 path = strdup("");
1652 if (path == NULL) {
1653 error = got_error_from_errno();
1654 goto done;
1656 } else if (argc == 1) {
1657 error = get_status_path(&path, worktree, argv[0]);
1658 if (error)
1659 goto done;
1660 } else
1661 usage_status();
1663 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1664 if (error != NULL)
1665 goto done;
1667 error = apply_unveil(got_repo_get_path(repo), 1,
1668 got_worktree_get_root_path(worktree));
1669 if (error)
1670 goto done;
1672 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1673 check_cancelled, NULL);
1674 done:
1675 free(cwd);
1676 free(path);
1677 return error;
1680 __dead static void
1681 usage_ref(void)
1683 fprintf(stderr,
1684 "usage: %s ref [-r repository] -l | -d name | name object\n",
1685 getprogname());
1686 exit(1);
1689 static const struct got_error *
1690 list_refs(struct got_repository *repo)
1692 static const struct got_error *err = NULL;
1693 struct got_reflist_head refs;
1694 struct got_reflist_entry *re;
1696 SIMPLEQ_INIT(&refs);
1697 err = got_ref_list(&refs, repo);
1698 if (err)
1699 return err;
1701 SIMPLEQ_FOREACH(re, &refs, entry) {
1702 char *refstr;
1703 refstr = got_ref_to_str(re->ref);
1704 if (refstr == NULL)
1705 return got_error_from_errno();
1706 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1707 free(refstr);
1710 got_ref_list_free(&refs);
1711 return NULL;
1714 static const struct got_error *
1715 delete_ref(struct got_repository *repo, const char *refname)
1717 const struct got_error *err = NULL;
1718 struct got_reference *ref;
1720 err = got_ref_open(&ref, repo, refname);
1721 if (err)
1722 return err;
1724 err = got_ref_delete(ref, repo);
1725 got_ref_close(ref);
1726 return err;
1729 static const struct got_error *
1730 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1732 const struct got_error *err = NULL;
1733 struct got_object_id *id;
1734 struct got_reference *ref = NULL;
1736 err = got_object_resolve_id_str(&id, repo, id_str);
1737 if (err)
1738 return err;
1740 err = got_ref_alloc(&ref, refname, id);
1741 if (err)
1742 goto done;
1744 err = got_ref_write(ref, repo);
1745 done:
1746 if (ref)
1747 got_ref_close(ref);
1748 free(id);
1749 return err;
1752 static const struct got_error *
1753 cmd_ref(int argc, char *argv[])
1755 const struct got_error *error = NULL;
1756 struct got_repository *repo = NULL;
1757 struct got_worktree *worktree = NULL;
1758 char *cwd = NULL, *repo_path = NULL;
1759 int ch, do_list = 0;
1760 const char *delref = NULL;
1762 /* TODO: Add -s option for adding symbolic references. */
1763 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1764 switch (ch) {
1765 case 'd':
1766 delref = optarg;
1767 break;
1768 case 'r':
1769 repo_path = realpath(optarg, NULL);
1770 if (repo_path == NULL)
1771 err(1, "-r option");
1772 break;
1773 case 'l':
1774 do_list = 1;
1775 break;
1776 default:
1777 usage_ref();
1778 /* NOTREACHED */
1782 if (do_list && delref)
1783 errx(1, "-l and -d options are mutually exclusive\n");
1785 argc -= optind;
1786 argv += optind;
1788 if (do_list || delref) {
1789 if (argc > 0)
1790 usage_ref();
1791 } else if (argc != 2)
1792 usage_ref();
1794 #ifndef PROFILE
1795 if (do_list) {
1796 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1797 NULL) == -1)
1798 err(1, "pledge");
1799 } else {
1800 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1801 "sendfd unveil", NULL) == -1)
1802 err(1, "pledge");
1804 #endif
1805 cwd = getcwd(NULL, 0);
1806 if (cwd == NULL) {
1807 error = got_error_from_errno();
1808 goto done;
1811 if (repo_path == NULL) {
1812 error = got_worktree_open(&worktree, cwd);
1813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1814 goto done;
1815 else
1816 error = NULL;
1817 if (worktree) {
1818 repo_path =
1819 strdup(got_worktree_get_repo_path(worktree));
1820 if (repo_path == NULL)
1821 error = got_error_from_errno();
1822 if (error)
1823 goto done;
1824 } else {
1825 repo_path = strdup(cwd);
1826 if (repo_path == NULL) {
1827 error = got_error_from_errno();
1828 goto done;
1833 error = apply_unveil(repo_path, do_list,
1834 worktree ? got_worktree_get_root_path(worktree) : NULL);
1835 if (error)
1836 goto done;
1838 error = got_repo_open(&repo, repo_path);
1839 if (error != NULL)
1840 goto done;
1842 if (do_list)
1843 error = list_refs(repo);
1844 else if (delref)
1845 error = delete_ref(repo, delref);
1846 else
1847 error = add_ref(repo, argv[0], argv[1]);
1848 done:
1849 if (repo)
1850 got_repo_close(repo);
1851 if (worktree)
1852 got_worktree_close(worktree);
1853 free(cwd);
1854 free(repo_path);
1855 return error;