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, "heads/", 6) == 0)
638 name += 6;
639 if (strncmp(name, "remotes/", 8) == 0)
640 name += 8;
641 s = refs_str;
642 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
643 name) == -1) {
644 err = got_error_from_errno();
645 free(s);
646 break;
648 free(s);
650 err = got_object_id_str(&id_str, id);
651 if (err)
652 return err;
654 printf("-----------------------------------------------\n");
655 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
656 refs_str ? refs_str : "", refs_str ? ")" : "");
657 free(id_str);
658 id_str = NULL;
659 free(refs_str);
660 refs_str = NULL;
661 printf("from: %s\n", got_object_commit_get_author(commit));
662 committer_time = got_object_commit_get_committer_time(commit);
663 datestr = get_datestr(&committer_time, datebuf);
664 printf("date: %s UTC\n", datestr);
665 author = got_object_commit_get_author(commit);
666 committer = got_object_commit_get_committer(commit);
667 if (strcmp(author, committer) != 0)
668 printf("via: %s\n", committer);
669 if (got_object_commit_get_nparents(commit) > 1) {
670 const struct got_object_id_queue *parent_ids;
671 struct got_object_qid *qid;
672 int n = 1;
673 parent_ids = got_object_commit_get_parent_ids(commit);
674 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
675 err = got_object_id_str(&id_str, qid->id);
676 if (err)
677 return err;
678 printf("parent %d: %s\n", n++, id_str);
679 free(id_str);
683 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
684 if (logmsg0 == NULL)
685 return got_error_from_errno();
687 logmsg = logmsg0;
688 do {
689 line = strsep(&logmsg, "\n");
690 if (line)
691 printf(" %s\n", line);
692 } while (line);
693 free(logmsg0);
695 if (show_patch) {
696 err = print_patch(commit, id, diff_context, repo);
697 if (err == 0)
698 printf("\n");
701 if (fflush(stdout) != 0 && err == NULL)
702 err = got_error_from_errno();
703 return err;
706 static const struct got_error *
707 print_commits(struct got_object_id *root_id, struct got_repository *repo,
708 char *path, int show_patch, int diff_context, int limit,
709 int first_parent_traversal, struct got_reflist_head *refs)
711 const struct got_error *err;
712 struct got_commit_graph *graph;
714 err = got_commit_graph_open(&graph, root_id, path,
715 first_parent_traversal, repo);
716 if (err)
717 return err;
718 err = got_commit_graph_iter_start(graph, root_id, repo);
719 if (err)
720 goto done;
721 while (1) {
722 struct got_commit_object *commit;
723 struct got_object_id *id;
725 if (sigint_received || sigpipe_received)
726 break;
728 err = got_commit_graph_iter_next(&id, graph);
729 if (err) {
730 if (err->code == GOT_ERR_ITER_COMPLETED) {
731 err = NULL;
732 break;
734 if (err->code != GOT_ERR_ITER_NEED_MORE)
735 break;
736 err = got_commit_graph_fetch_commits(graph, 1, repo);
737 if (err)
738 break;
739 else
740 continue;
742 if (id == NULL)
743 break;
745 err = got_object_open_as_commit(&commit, repo, id);
746 if (err)
747 break;
748 err = print_commit(commit, id, repo, show_patch, diff_context,
749 refs);
750 got_object_commit_close(commit);
751 if (err || (limit && --limit == 0))
752 break;
754 done:
755 got_commit_graph_close(graph);
756 return err;
759 __dead static void
760 usage_log(void)
762 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
763 "[-r repository-path] [path]\n", getprogname());
764 exit(1);
767 static const struct got_error *
768 cmd_log(int argc, char *argv[])
770 const struct got_error *error;
771 struct got_repository *repo = NULL;
772 struct got_worktree *worktree = NULL;
773 struct got_commit_object *commit = NULL;
774 struct got_object_id *id = NULL;
775 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
776 char *start_commit = NULL;
777 int diff_context = 3, ch;
778 int show_patch = 0, limit = 0, first_parent_traversal = 0;
779 const char *errstr;
780 struct got_reflist_head refs;
782 #ifndef PROFILE
783 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
784 NULL)
785 == -1)
786 err(1, "pledge");
787 #endif
789 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
790 switch (ch) {
791 case 'p':
792 show_patch = 1;
793 break;
794 case 'c':
795 start_commit = optarg;
796 break;
797 case 'C':
798 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
799 &errstr);
800 if (errstr != NULL)
801 err(1, "-C option %s", errstr);
802 break;
803 case 'l':
804 limit = strtonum(optarg, 1, INT_MAX, &errstr);
805 if (errstr != NULL)
806 err(1, "-l option %s", errstr);
807 break;
808 case 'f':
809 first_parent_traversal = 1;
810 break;
811 case 'r':
812 repo_path = realpath(optarg, NULL);
813 if (repo_path == NULL)
814 err(1, "-r option");
815 break;
816 default:
817 usage_log();
818 /* NOTREACHED */
822 argc -= optind;
823 argv += optind;
825 if (argc == 0)
826 path = strdup("");
827 else if (argc == 1)
828 path = strdup(argv[0]);
829 else
830 usage_log();
831 if (path == NULL)
832 return got_error_from_errno();
834 cwd = getcwd(NULL, 0);
835 if (cwd == NULL) {
836 error = got_error_from_errno();
837 goto done;
840 error = got_worktree_open(&worktree, cwd);
841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
842 goto done;
843 error = NULL;
845 repo_path = worktree ?
846 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
847 if (repo_path == NULL) {
848 error = got_error_from_errno();
849 goto done;
852 error = apply_unveil(repo_path, 1,
853 worktree ? got_worktree_get_root_path(worktree) : NULL);
854 if (error)
855 goto done;
857 error = got_repo_open(&repo, repo_path);
858 if (error != NULL)
859 goto done;
861 if (start_commit == NULL) {
862 struct got_reference *head_ref;
863 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
864 if (error != NULL)
865 return error;
866 error = got_ref_resolve(&id, repo, head_ref);
867 got_ref_close(head_ref);
868 if (error != NULL)
869 return error;
870 error = got_object_open_as_commit(&commit, repo, id);
871 } else {
872 struct got_reference *ref;
873 error = got_ref_open(&ref, repo, start_commit);
874 if (error == NULL) {
875 int obj_type;
876 error = got_ref_resolve(&id, repo, ref);
877 got_ref_close(ref);
878 if (error != NULL)
879 goto done;
880 error = got_object_get_type(&obj_type, repo, id);
881 if (error != NULL)
882 goto done;
883 if (obj_type == GOT_OBJ_TYPE_TAG) {
884 struct got_tag_object *tag;
885 error = got_object_open_as_tag(&tag, repo, id);
886 if (error != NULL)
887 goto done;
888 if (got_object_tag_get_object_type(tag) !=
889 GOT_OBJ_TYPE_COMMIT) {
890 got_object_tag_close(tag);
891 error = got_error(GOT_ERR_OBJ_TYPE);
892 goto done;
894 free(id);
895 id = got_object_id_dup(
896 got_object_tag_get_object_id(tag));
897 if (id == NULL)
898 error = got_error_from_errno();
899 got_object_tag_close(tag);
900 if (error)
901 goto done;
902 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
903 error = got_error(GOT_ERR_OBJ_TYPE);
904 goto done;
906 error = got_object_open_as_commit(&commit, repo, id);
907 if (error != NULL)
908 goto done;
910 if (commit == NULL) {
911 error = got_object_resolve_id_str(&id, repo,
912 start_commit);
913 if (error != NULL)
914 return error;
917 if (error != NULL)
918 goto done;
920 error = got_repo_map_path(&in_repo_path, repo, path, 1);
921 if (error != NULL)
922 goto done;
923 if (in_repo_path) {
924 free(path);
925 path = in_repo_path;
928 SIMPLEQ_INIT(&refs);
929 error = got_ref_list(&refs, repo);
930 if (error)
931 goto done;
933 error = print_commits(id, repo, path, show_patch,
934 diff_context, limit, first_parent_traversal, &refs);
935 done:
936 free(path);
937 free(repo_path);
938 free(cwd);
939 free(id);
940 if (worktree)
941 got_worktree_close(worktree);
942 if (repo) {
943 const struct got_error *repo_error;
944 repo_error = got_repo_close(repo);
945 if (error == NULL)
946 error = repo_error;
948 got_ref_list_free(&refs);
949 return error;
952 __dead static void
953 usage_diff(void)
955 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
956 "[object1 object2 | path]\n", getprogname());
957 exit(1);
960 struct print_diff_arg {
961 struct got_repository *repo;
962 struct got_worktree *worktree;
963 int diff_context;
964 const char *id_str;
965 int header_shown;
966 };
968 static const struct got_error *
969 print_diff(void *arg, unsigned char status, const char *path,
970 struct got_object_id *id)
972 struct print_diff_arg *a = arg;
973 const struct got_error *err = NULL;
974 struct got_blob_object *blob1 = NULL;
975 FILE *f2 = NULL;
976 char *abspath = NULL;
977 struct stat sb;
979 if (status != GOT_STATUS_MODIFY)
980 return NULL;
982 if (!a->header_shown) {
983 printf("diff %s %s\n", a->id_str,
984 got_worktree_get_root_path(a->worktree));
985 a->header_shown = 1;
988 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
989 if (err)
990 goto done;
992 if (asprintf(&abspath, "%s/%s",
993 got_worktree_get_root_path(a->worktree), path) == -1) {
994 err = got_error_from_errno();
995 goto done;
998 f2 = fopen(abspath, "r");
999 if (f2 == NULL) {
1000 err = got_error_from_errno();
1001 goto done;
1003 if (lstat(abspath, &sb) == -1) {
1004 err = got_error_from_errno();
1005 goto done;
1008 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1009 stdout);
1010 done:
1011 if (blob1)
1012 got_object_blob_close(blob1);
1013 if (f2 && fclose(f2) != 0 && err == NULL)
1014 err = got_error_from_errno();
1015 free(abspath);
1016 return err;
1019 static const struct got_error *
1020 get_status_path(char **status_path, struct got_worktree *worktree,
1021 const char *arg)
1023 const struct got_error *err = NULL;
1024 char *resolved, *path = NULL;
1025 size_t len;
1027 *status_path = NULL;
1029 resolved = realpath(arg, NULL);
1030 if (resolved == NULL)
1031 return got_error_from_errno();
1033 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1034 strlen(got_worktree_get_root_path(worktree)))) {
1035 err = got_error(GOT_ERR_BAD_PATH);
1036 goto done;
1039 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1040 if (path == NULL) {
1041 err = got_error_from_errno();
1042 goto done;
1045 /* XXX status walk can't deal with trailing slash! */
1046 len = strlen(path);
1047 while (path[len - 1] == '/') {
1048 path[len - 1] = '\0';
1049 len--;
1051 done:
1052 free(resolved);
1053 if (err == NULL)
1054 *status_path = path;
1055 else
1056 free(path);
1057 return err;
1060 static const struct got_error *
1061 cmd_diff(int argc, char *argv[])
1063 const struct got_error *error;
1064 struct got_repository *repo = NULL;
1065 struct got_worktree *worktree = NULL;
1066 char *cwd = NULL, *repo_path = NULL;
1067 struct got_object_id *id1 = NULL, *id2 = NULL;
1068 char *id_str1 = NULL, *id_str2 = NULL;
1069 int type1, type2;
1070 int diff_context = 3, ch;
1071 const char *errstr;
1072 char *path = NULL;
1074 #ifndef PROFILE
1075 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1076 NULL) == -1)
1077 err(1, "pledge");
1078 #endif
1080 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1081 switch (ch) {
1082 case 'C':
1083 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1084 if (errstr != NULL)
1085 err(1, "-C option %s", errstr);
1086 break;
1087 case 'r':
1088 repo_path = realpath(optarg, NULL);
1089 if (repo_path == NULL)
1090 err(1, "-r option");
1091 break;
1092 default:
1093 usage_diff();
1094 /* NOTREACHED */
1098 argc -= optind;
1099 argv += optind;
1101 cwd = getcwd(NULL, 0);
1102 if (cwd == NULL) {
1103 error = got_error_from_errno();
1104 goto done;
1106 error = got_worktree_open(&worktree, cwd);
1107 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1108 goto done;
1109 if (argc <= 1) {
1110 if (worktree == NULL) {
1111 error = got_error(GOT_ERR_NOT_WORKTREE);
1112 goto done;
1114 if (repo_path)
1115 errx(1,
1116 "-r option can't be used when diffing a work tree");
1117 repo_path = strdup(got_worktree_get_repo_path(worktree));
1118 if (repo_path == NULL) {
1119 error = got_error_from_errno();
1120 goto done;
1122 if (argc == 1) {
1123 error = get_status_path(&path, worktree, argv[0]);
1124 if (error)
1125 goto done;
1126 } else {
1127 path = strdup("");
1128 if (path == NULL) {
1129 error = got_error_from_errno();
1130 goto done;
1133 } else if (argc == 2) {
1134 id_str1 = argv[0];
1135 id_str2 = argv[1];
1136 } else
1137 usage_diff();
1139 if (repo_path == NULL) {
1140 repo_path = getcwd(NULL, 0);
1141 if (repo_path == NULL)
1142 return got_error_from_errno();
1145 error = apply_unveil(repo_path, 1,
1146 worktree ? got_worktree_get_root_path(worktree) : NULL);
1147 if (error)
1148 goto done;
1150 error = got_repo_open(&repo, repo_path);
1151 free(repo_path);
1152 if (error != NULL)
1153 goto done;
1155 if (worktree) {
1156 struct print_diff_arg arg;
1157 char *id_str;
1158 error = got_object_id_str(&id_str,
1159 got_worktree_get_base_commit_id(worktree));
1160 if (error)
1161 goto done;
1162 arg.repo = repo;
1163 arg.worktree = worktree;
1164 arg.diff_context = diff_context;
1165 arg.id_str = id_str;
1166 arg.header_shown = 0;
1168 error = got_worktree_status(worktree, path, repo, print_diff,
1169 &arg, check_cancelled, NULL);
1170 free(id_str);
1171 goto done;
1174 error = got_object_resolve_id_str(&id1, repo, id_str1);
1175 if (error)
1176 goto done;
1178 error = got_object_resolve_id_str(&id2, repo, id_str2);
1179 if (error)
1180 goto done;
1182 error = got_object_get_type(&type1, repo, id1);
1183 if (error)
1184 goto done;
1186 error = got_object_get_type(&type2, repo, id2);
1187 if (error)
1188 goto done;
1190 if (type1 != type2) {
1191 error = got_error(GOT_ERR_OBJ_TYPE);
1192 goto done;
1195 switch (type1) {
1196 case GOT_OBJ_TYPE_BLOB:
1197 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1198 diff_context, repo, stdout);
1199 break;
1200 case GOT_OBJ_TYPE_TREE:
1201 error = got_diff_objects_as_trees(id1, id2, "", "",
1202 diff_context, repo, stdout);
1203 break;
1204 case GOT_OBJ_TYPE_COMMIT:
1205 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1206 id_str2);
1207 error = got_diff_objects_as_commits(id1, id2, diff_context,
1208 repo, stdout);
1209 break;
1210 default:
1211 error = got_error(GOT_ERR_OBJ_TYPE);
1214 done:
1215 free(id1);
1216 free(id2);
1217 free(path);
1218 if (worktree)
1219 got_worktree_close(worktree);
1220 if (repo) {
1221 const struct got_error *repo_error;
1222 repo_error = got_repo_close(repo);
1223 if (error == NULL)
1224 error = repo_error;
1226 return error;
1229 __dead static void
1230 usage_blame(void)
1232 fprintf(stderr,
1233 "usage: %s blame [-c commit] [-r repository-path] path\n",
1234 getprogname());
1235 exit(1);
1238 static const struct got_error *
1239 cmd_blame(int argc, char *argv[])
1241 const struct got_error *error;
1242 struct got_repository *repo = NULL;
1243 struct got_worktree *worktree = NULL;
1244 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1245 struct got_object_id *commit_id = NULL;
1246 char *commit_id_str = NULL;
1247 int ch;
1249 #ifndef PROFILE
1250 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1251 NULL) == -1)
1252 err(1, "pledge");
1253 #endif
1255 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1256 switch (ch) {
1257 case 'c':
1258 commit_id_str = optarg;
1259 break;
1260 case 'r':
1261 repo_path = realpath(optarg, NULL);
1262 if (repo_path == NULL)
1263 err(1, "-r option");
1264 break;
1265 default:
1266 usage_blame();
1267 /* NOTREACHED */
1271 argc -= optind;
1272 argv += optind;
1274 if (argc == 1)
1275 path = argv[0];
1276 else
1277 usage_blame();
1279 cwd = getcwd(NULL, 0);
1280 if (cwd == NULL) {
1281 error = got_error_from_errno();
1282 goto done;
1284 if (repo_path == NULL) {
1285 error = got_worktree_open(&worktree, cwd);
1286 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1287 goto done;
1288 else
1289 error = NULL;
1290 if (worktree) {
1291 repo_path =
1292 strdup(got_worktree_get_repo_path(worktree));
1293 if (repo_path == NULL)
1294 error = got_error_from_errno();
1295 if (error)
1296 goto done;
1297 } else {
1298 repo_path = strdup(cwd);
1299 if (repo_path == NULL) {
1300 error = got_error_from_errno();
1301 goto done;
1306 error = apply_unveil(repo_path, 1, NULL);
1307 if (error)
1308 goto done;
1310 error = got_repo_open(&repo, repo_path);
1311 if (error != NULL)
1312 goto done;
1314 if (worktree) {
1315 const char *prefix = got_worktree_get_path_prefix(worktree);
1316 char *p, *worktree_subdir = cwd +
1317 strlen(got_worktree_get_root_path(worktree));
1318 if (asprintf(&p, "%s%s%s%s%s",
1319 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1320 worktree_subdir, worktree_subdir[0] ? "/" : "",
1321 path) == -1) {
1322 error = got_error_from_errno();
1323 goto done;
1325 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1326 free(p);
1327 } else {
1328 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1330 if (error)
1331 goto done;
1333 if (commit_id_str == NULL) {
1334 struct got_reference *head_ref;
1335 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1336 if (error != NULL)
1337 goto done;
1338 error = got_ref_resolve(&commit_id, repo, head_ref);
1339 got_ref_close(head_ref);
1340 if (error != NULL)
1341 goto done;
1342 } else {
1343 error = got_object_resolve_id_str(&commit_id, repo,
1344 commit_id_str);
1345 if (error != NULL)
1346 goto done;
1349 error = got_blame(in_repo_path, commit_id, repo, stdout);
1350 done:
1351 free(in_repo_path);
1352 free(repo_path);
1353 free(cwd);
1354 free(commit_id);
1355 if (worktree)
1356 got_worktree_close(worktree);
1357 if (repo) {
1358 const struct got_error *repo_error;
1359 repo_error = got_repo_close(repo);
1360 if (error == NULL)
1361 error = repo_error;
1363 return error;
1366 __dead static void
1367 usage_tree(void)
1369 fprintf(stderr,
1370 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1371 getprogname());
1372 exit(1);
1375 static void
1376 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1377 const char *root_path)
1379 int is_root_path = (strcmp(path, root_path) == 0);
1381 path += strlen(root_path);
1382 while (path[0] == '/')
1383 path++;
1385 printf("%s%s%s%s%s\n", id ? id : "", path,
1386 is_root_path ? "" : "/", te->name,
1387 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1390 static const struct got_error *
1391 print_tree(const char *path, struct got_object_id *commit_id,
1392 int show_ids, int recurse, const char *root_path,
1393 struct got_repository *repo)
1395 const struct got_error *err = NULL;
1396 struct got_object_id *tree_id = NULL;
1397 struct got_tree_object *tree = NULL;
1398 const struct got_tree_entries *entries;
1399 struct got_tree_entry *te;
1401 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1402 if (err)
1403 goto done;
1405 err = got_object_open_as_tree(&tree, repo, tree_id);
1406 if (err)
1407 goto done;
1408 entries = got_object_tree_get_entries(tree);
1409 te = SIMPLEQ_FIRST(&entries->head);
1410 while (te) {
1411 char *id = NULL;
1413 if (sigint_received || sigpipe_received)
1414 break;
1416 if (show_ids) {
1417 char *id_str;
1418 err = got_object_id_str(&id_str, te->id);
1419 if (err)
1420 goto done;
1421 if (asprintf(&id, "%s ", id_str) == -1) {
1422 err = got_error_from_errno();
1423 free(id_str);
1424 goto done;
1426 free(id_str);
1428 print_entry(te, id, path, root_path);
1429 free(id);
1431 if (recurse && S_ISDIR(te->mode)) {
1432 char *child_path;
1433 if (asprintf(&child_path, "%s%s%s", path,
1434 path[0] == '/' && path[1] == '\0' ? "" : "/",
1435 te->name) == -1) {
1436 err = got_error_from_errno();
1437 goto done;
1439 err = print_tree(child_path, commit_id, show_ids, 1,
1440 root_path, repo);
1441 free(child_path);
1442 if (err)
1443 goto done;
1446 te = SIMPLEQ_NEXT(te, entry);
1448 done:
1449 if (tree)
1450 got_object_tree_close(tree);
1451 free(tree_id);
1452 return err;
1455 static const struct got_error *
1456 cmd_tree(int argc, char *argv[])
1458 const struct got_error *error;
1459 struct got_repository *repo = NULL;
1460 struct got_worktree *worktree = NULL;
1461 const char *path;
1462 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1463 struct got_object_id *commit_id = NULL;
1464 char *commit_id_str = NULL;
1465 int show_ids = 0, recurse = 0;
1466 int ch;
1468 #ifndef PROFILE
1469 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1470 NULL) == -1)
1471 err(1, "pledge");
1472 #endif
1474 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1475 switch (ch) {
1476 case 'c':
1477 commit_id_str = optarg;
1478 break;
1479 case 'r':
1480 repo_path = realpath(optarg, NULL);
1481 if (repo_path == NULL)
1482 err(1, "-r option");
1483 break;
1484 case 'i':
1485 show_ids = 1;
1486 break;
1487 case 'R':
1488 recurse = 1;
1489 break;
1490 default:
1491 usage_tree();
1492 /* NOTREACHED */
1496 argc -= optind;
1497 argv += optind;
1499 if (argc == 1)
1500 path = argv[0];
1501 else if (argc > 1)
1502 usage_tree();
1503 else
1504 path = NULL;
1506 cwd = getcwd(NULL, 0);
1507 if (cwd == NULL) {
1508 error = got_error_from_errno();
1509 goto done;
1511 if (repo_path == NULL) {
1512 error = got_worktree_open(&worktree, cwd);
1513 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1514 goto done;
1515 else
1516 error = NULL;
1517 if (worktree) {
1518 repo_path =
1519 strdup(got_worktree_get_repo_path(worktree));
1520 if (repo_path == NULL)
1521 error = got_error_from_errno();
1522 if (error)
1523 goto done;
1524 } else {
1525 repo_path = strdup(cwd);
1526 if (repo_path == NULL) {
1527 error = got_error_from_errno();
1528 goto done;
1533 error = apply_unveil(repo_path, 1, NULL);
1534 if (error)
1535 goto done;
1537 error = got_repo_open(&repo, repo_path);
1538 if (error != NULL)
1539 goto done;
1541 if (path == NULL) {
1542 if (worktree) {
1543 char *p, *worktree_subdir = cwd +
1544 strlen(got_worktree_get_root_path(worktree));
1545 if (asprintf(&p, "%s/%s",
1546 got_worktree_get_path_prefix(worktree),
1547 worktree_subdir) == -1) {
1548 error = got_error_from_errno();
1549 goto done;
1551 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1552 free(p);
1553 if (error)
1554 goto done;
1555 } else
1556 path = "/";
1558 if (in_repo_path == NULL) {
1559 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1560 if (error != NULL)
1561 goto done;
1564 if (commit_id_str == NULL) {
1565 struct got_reference *head_ref;
1566 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1567 if (error != NULL)
1568 goto done;
1569 error = got_ref_resolve(&commit_id, repo, head_ref);
1570 got_ref_close(head_ref);
1571 if (error != NULL)
1572 goto done;
1573 } else {
1574 error = got_object_resolve_id_str(&commit_id, repo,
1575 commit_id_str);
1576 if (error != NULL)
1577 goto done;
1580 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1581 in_repo_path, repo);
1582 done:
1583 free(in_repo_path);
1584 free(repo_path);
1585 free(cwd);
1586 free(commit_id);
1587 if (worktree)
1588 got_worktree_close(worktree);
1589 if (repo) {
1590 const struct got_error *repo_error;
1591 repo_error = got_repo_close(repo);
1592 if (error == NULL)
1593 error = repo_error;
1595 return error;
1598 __dead static void
1599 usage_status(void)
1601 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1602 exit(1);
1605 static const struct got_error *
1606 print_status(void *arg, unsigned char status, const char *path,
1607 struct got_object_id *id)
1609 printf("%c %s\n", status, path);
1610 return NULL;
1613 static const struct got_error *
1614 cmd_status(int argc, char *argv[])
1616 const struct got_error *error = NULL;
1617 struct got_repository *repo = NULL;
1618 struct got_worktree *worktree = NULL;
1619 char *cwd = NULL, *path = NULL;
1620 int ch;
1622 while ((ch = getopt(argc, argv, "")) != -1) {
1623 switch (ch) {
1624 default:
1625 usage_status();
1626 /* NOTREACHED */
1630 argc -= optind;
1631 argv += optind;
1633 #ifndef PROFILE
1634 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1635 NULL) == -1)
1636 err(1, "pledge");
1637 #endif
1638 cwd = getcwd(NULL, 0);
1639 if (cwd == NULL) {
1640 error = got_error_from_errno();
1641 goto done;
1644 error = got_worktree_open(&worktree, cwd);
1645 if (error != NULL)
1646 goto done;
1648 if (argc == 0) {
1649 path = strdup("");
1650 if (path == NULL) {
1651 error = got_error_from_errno();
1652 goto done;
1654 } else if (argc == 1) {
1655 error = get_status_path(&path, worktree, argv[0]);
1656 if (error)
1657 goto done;
1658 } else
1659 usage_status();
1661 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1662 if (error != NULL)
1663 goto done;
1665 error = apply_unveil(got_repo_get_path(repo), 1,
1666 got_worktree_get_root_path(worktree));
1667 if (error)
1668 goto done;
1670 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1671 check_cancelled, NULL);
1672 done:
1673 free(cwd);
1674 free(path);
1675 return error;
1678 __dead static void
1679 usage_ref(void)
1681 fprintf(stderr,
1682 "usage: %s ref [-r repository] -l | -d name | name object\n",
1683 getprogname());
1684 exit(1);
1687 static const struct got_error *
1688 list_refs(struct got_repository *repo)
1690 static const struct got_error *err = NULL;
1691 struct got_reflist_head refs;
1692 struct got_reflist_entry *re;
1694 SIMPLEQ_INIT(&refs);
1695 err = got_ref_list(&refs, repo);
1696 if (err)
1697 return err;
1699 SIMPLEQ_FOREACH(re, &refs, entry) {
1700 char *refstr;
1701 refstr = got_ref_to_str(re->ref);
1702 if (refstr == NULL)
1703 return got_error_from_errno();
1704 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1705 free(refstr);
1708 got_ref_list_free(&refs);
1709 return NULL;
1712 static const struct got_error *
1713 delete_ref(struct got_repository *repo, const char *refname)
1715 const struct got_error *err = NULL;
1716 struct got_reference *ref;
1718 err = got_ref_open(&ref, repo, refname);
1719 if (err)
1720 return err;
1722 err = got_ref_delete(ref, repo);
1723 got_ref_close(ref);
1724 return err;
1727 static const struct got_error *
1728 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1730 const struct got_error *err = NULL;
1731 struct got_object_id *id;
1732 struct got_reference *ref = NULL;
1734 err = got_object_resolve_id_str(&id, repo, id_str);
1735 if (err)
1736 return err;
1738 err = got_ref_alloc(&ref, refname, id);
1739 if (err)
1740 goto done;
1742 err = got_ref_write(ref, repo);
1743 done:
1744 if (ref)
1745 got_ref_close(ref);
1746 free(id);
1747 return err;
1750 static const struct got_error *
1751 cmd_ref(int argc, char *argv[])
1753 const struct got_error *error = NULL;
1754 struct got_repository *repo = NULL;
1755 struct got_worktree *worktree = NULL;
1756 char *cwd = NULL, *repo_path = NULL;
1757 int ch, do_list = 0;
1758 const char *delref = NULL;
1760 /* TODO: Add -s option for adding symbolic references. */
1761 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1762 switch (ch) {
1763 case 'd':
1764 delref = optarg;
1765 break;
1766 case 'r':
1767 repo_path = realpath(optarg, NULL);
1768 if (repo_path == NULL)
1769 err(1, "-r option");
1770 break;
1771 case 'l':
1772 do_list = 1;
1773 break;
1774 default:
1775 usage_ref();
1776 /* NOTREACHED */
1780 if (do_list && delref)
1781 errx(1, "-l and -d options are mutually exclusive\n");
1783 argc -= optind;
1784 argv += optind;
1786 if (do_list || delref) {
1787 if (argc > 0)
1788 usage_ref();
1789 } else if (argc != 2)
1790 usage_ref();
1792 #ifndef PROFILE
1793 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1794 "unveil", NULL) == -1)
1795 err(1, "pledge");
1796 #endif
1797 cwd = getcwd(NULL, 0);
1798 if (cwd == NULL) {
1799 error = got_error_from_errno();
1800 goto done;
1803 if (repo_path == NULL) {
1804 error = got_worktree_open(&worktree, cwd);
1805 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1806 goto done;
1807 else
1808 error = NULL;
1809 if (worktree) {
1810 repo_path =
1811 strdup(got_worktree_get_repo_path(worktree));
1812 if (repo_path == NULL)
1813 error = got_error_from_errno();
1814 if (error)
1815 goto done;
1816 } else {
1817 repo_path = strdup(cwd);
1818 if (repo_path == NULL) {
1819 error = got_error_from_errno();
1820 goto done;
1825 error = apply_unveil(repo_path, do_list,
1826 worktree ? got_worktree_get_root_path(worktree) : NULL);
1827 if (error)
1828 goto done;
1830 error = got_repo_open(&repo, repo_path);
1831 if (error != NULL)
1832 goto done;
1834 if (do_list)
1835 error = list_refs(repo);
1836 else if (delref)
1837 error = delete_ref(repo, delref);
1838 else
1839 error = add_ref(repo, argv[0], argv[1]);
1840 done:
1841 if (repo)
1842 got_repo_close(repo);
1843 if (worktree)
1844 got_worktree_close(worktree);
1845 free(cwd);
1846 free(repo_path);
1847 return error;