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);
80 __dead static void usage_add(void);
82 static const struct got_error* cmd_checkout(int, char *[]);
83 static const struct got_error* cmd_update(int, char *[]);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
88 static const struct got_error* cmd_status(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
90 static const struct got_error* cmd_add(int, char *[]);
92 static struct cmd got_commands[] = {
93 { "checkout", cmd_checkout, usage_checkout,
94 "check out a new work tree from a repository" },
95 { "update", cmd_update, usage_update,
96 "update a work tree to a different commit" },
97 { "log", cmd_log, usage_log,
98 "show repository history" },
99 { "diff", cmd_diff, usage_diff,
100 "compare files and directories" },
101 { "blame", cmd_blame, usage_blame,
102 " show when lines in a file were changed" },
103 { "tree", cmd_tree, usage_tree,
104 " list files and directories in repository" },
105 { "status", cmd_status, usage_status,
106 "show modification status of files" },
107 { "ref", cmd_ref, usage_ref,
108 "manage references in repository" },
109 { "add", cmd_add, usage_add,
110 "add a new file to version control" },
111 };
113 int
114 main(int argc, char *argv[])
116 struct cmd *cmd;
117 unsigned int i;
118 int ch;
119 int hflag = 0;
121 setlocale(LC_CTYPE, "");
123 while ((ch = getopt(argc, argv, "h")) != -1) {
124 switch (ch) {
125 case 'h':
126 hflag = 1;
127 break;
128 default:
129 usage();
130 /* NOTREACHED */
134 argc -= optind;
135 argv += optind;
136 optind = 0;
138 if (argc <= 0)
139 usage();
141 signal(SIGINT, catch_sigint);
142 signal(SIGPIPE, catch_sigpipe);
144 for (i = 0; i < nitems(got_commands); i++) {
145 const struct got_error *error;
147 cmd = &got_commands[i];
149 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
150 continue;
152 if (hflag)
153 got_commands[i].cmd_usage();
155 error = got_commands[i].cmd_main(argc, argv);
156 if (error && !(sigint_received || sigpipe_received)) {
157 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
158 return 1;
161 return 0;
164 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
165 return 1;
168 __dead static void
169 usage(void)
171 int i;
173 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
174 "Available commands:\n", getprogname());
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
179 exit(1);
182 static const struct got_error *
183 apply_unveil(const char *repo_path, int repo_read_only,
184 const char *worktree_path)
186 const struct got_error *error;
188 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
189 return got_error_from_errno();
191 if (worktree_path && unveil(worktree_path, "rwc") != 0)
192 return got_error_from_errno();
194 if (unveil("/tmp", "rwc") != 0)
195 return got_error_from_errno();
197 error = got_privsep_unveil_exec_helpers();
198 if (error != NULL)
199 return error;
201 if (unveil(NULL, NULL) != 0)
202 return got_error_from_errno();
204 return NULL;
207 __dead static void
208 usage_checkout(void)
210 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
211 "[worktree-path]\n", getprogname());
212 exit(1);
215 static void
216 checkout_progress(void *arg, unsigned char status, const char *path)
218 char *worktree_path = arg;
220 while (path[0] == '/')
221 path++;
223 printf("%c %s/%s\n", status, worktree_path, path);
226 static const struct got_error *
227 check_cancelled(void *arg)
229 if (sigint_received || sigpipe_received)
230 return got_error(GOT_ERR_CANCELLED);
231 return NULL;
234 static const struct got_error *
235 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
236 struct got_repository *repo)
238 const struct got_error *err;
239 struct got_reference *head_ref = NULL;
240 struct got_object_id *head_commit_id = NULL;
241 struct got_commit_graph *graph = NULL;
243 head_ref = got_worktree_get_head_ref(worktree);
244 if (head_ref == NULL)
245 return got_error_from_errno();
247 /* TODO: Check the reflog. The head ref may have been rebased. */
248 err = got_ref_resolve(&head_commit_id, repo, head_ref);
249 if (err)
250 goto done;
252 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
253 if (err)
254 goto done;
256 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
257 if (err)
258 goto done;
259 while (1) {
260 struct got_object_id *id;
262 if (sigint_received || sigpipe_received)
263 break;
265 err = got_commit_graph_iter_next(&id, graph);
266 if (err) {
267 if (err->code == GOT_ERR_ITER_COMPLETED) {
268 err = got_error(GOT_ERR_ANCESTRY);
269 break;
271 if (err->code != GOT_ERR_ITER_NEED_MORE)
272 break;
273 err = got_commit_graph_fetch_commits(graph, 1, repo);
274 if (err)
275 break;
276 else
277 continue;
279 if (id == NULL)
280 break;
281 if (got_object_id_cmp(id, commit_id) == 0)
282 break;
284 done:
285 if (head_ref)
286 got_ref_close(head_ref);
287 if (graph)
288 got_commit_graph_close(graph);
289 return err;
293 static const struct got_error *
294 cmd_checkout(int argc, char *argv[])
296 const struct got_error *error = NULL;
297 struct got_repository *repo = NULL;
298 struct got_reference *head_ref = NULL;
299 struct got_worktree *worktree = NULL;
300 char *repo_path = NULL;
301 char *worktree_path = NULL;
302 const char *path_prefix = "";
303 char *commit_id_str = NULL;
304 int ch, same_path_prefix;
306 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
307 switch (ch) {
308 case 'c':
309 commit_id_str = strdup(optarg);
310 if (commit_id_str == NULL)
311 return got_error_from_errno();
312 break;
313 case 'p':
314 path_prefix = optarg;
315 break;
316 default:
317 usage_checkout();
318 /* NOTREACHED */
322 argc -= optind;
323 argv += optind;
325 #ifndef PROFILE
326 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
327 "unveil", NULL) == -1)
328 err(1, "pledge");
329 #endif
330 if (argc == 1) {
331 char *cwd, *base, *dotgit;
332 repo_path = realpath(argv[0], NULL);
333 if (repo_path == NULL)
334 return got_error_from_errno();
335 cwd = getcwd(NULL, 0);
336 if (cwd == NULL) {
337 error = got_error_from_errno();
338 goto done;
340 if (path_prefix[0])
341 base = basename(path_prefix);
342 else
343 base = basename(repo_path);
344 if (base == NULL) {
345 error = got_error_from_errno();
346 goto done;
348 dotgit = strstr(base, ".git");
349 if (dotgit)
350 *dotgit = '\0';
351 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
352 error = got_error_from_errno();
353 free(cwd);
354 goto done;
356 free(cwd);
357 } else if (argc == 2) {
358 repo_path = realpath(argv[0], NULL);
359 if (repo_path == NULL) {
360 error = got_error_from_errno();
361 goto done;
363 worktree_path = realpath(argv[1], NULL);
364 if (worktree_path == NULL) {
365 error = got_error_from_errno();
366 goto done;
368 } else
369 usage_checkout();
371 error = apply_unveil(repo_path, 0, worktree_path);
372 if (error)
373 goto done;
375 error = got_repo_open(&repo, repo_path);
376 if (error != NULL)
377 goto done;
379 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
380 if (error != NULL)
381 goto done;
383 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
384 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
385 goto done;
387 error = got_worktree_open(&worktree, worktree_path);
388 if (error != NULL)
389 goto done;
391 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
392 path_prefix);
393 if (error != NULL)
394 goto done;
395 if (!same_path_prefix) {
396 error = got_error(GOT_ERR_PATH_PREFIX);
397 goto done;
400 if (commit_id_str) {
401 struct got_object_id *commit_id;
402 error = got_object_resolve_id_str(&commit_id, repo,
403 commit_id_str);
404 if (error != NULL)
405 goto done;
406 error = check_ancestry(worktree, commit_id, repo);
407 if (error != NULL) {
408 free(commit_id);
409 goto done;
411 error = got_worktree_set_base_commit_id(worktree, repo,
412 commit_id);
413 free(commit_id);
414 if (error)
415 goto done;
418 error = got_worktree_checkout_files(worktree, repo,
419 checkout_progress, worktree_path, check_cancelled, NULL);
420 if (error != NULL)
421 goto done;
423 printf("Now shut up and hack\n");
425 done:
426 free(commit_id_str);
427 free(repo_path);
428 free(worktree_path);
429 return error;
432 __dead static void
433 usage_update(void)
435 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
436 getprogname());
437 exit(1);
440 static void
441 update_progress(void *arg, unsigned char status, const char *path)
443 int *did_something = arg;
445 if (status == GOT_STATUS_EXISTS)
446 return;
448 *did_something = 1;
449 while (path[0] == '/')
450 path++;
451 printf("%c %s\n", status, path);
454 static const struct got_error *
455 cmd_update(int argc, char *argv[])
457 const struct got_error *error = NULL;
458 struct got_repository *repo = NULL;
459 struct got_worktree *worktree = NULL;
460 char *worktree_path = NULL;
461 struct got_object_id *commit_id = NULL;
462 char *commit_id_str = NULL;
463 int ch, did_something = 0;
465 while ((ch = getopt(argc, argv, "c:")) != -1) {
466 switch (ch) {
467 case 'c':
468 commit_id_str = strdup(optarg);
469 if (commit_id_str == NULL)
470 return got_error_from_errno();
471 break;
472 default:
473 usage_update();
474 /* NOTREACHED */
478 argc -= optind;
479 argv += optind;
481 #ifndef PROFILE
482 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
483 "unveil", NULL) == -1)
484 err(1, "pledge");
485 #endif
486 if (argc == 0) {
487 worktree_path = getcwd(NULL, 0);
488 if (worktree_path == NULL) {
489 error = got_error_from_errno();
490 goto done;
492 } else if (argc == 1) {
493 worktree_path = realpath(argv[0], NULL);
494 if (worktree_path == NULL) {
495 error = got_error_from_errno();
496 goto done;
498 } else
499 usage_update();
501 error = got_worktree_open(&worktree, worktree_path);
502 if (error != NULL)
503 goto done;
505 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
506 if (error != NULL)
507 goto done;
509 error = apply_unveil(got_repo_get_path(repo), 0,
510 got_worktree_get_root_path(worktree));
511 if (error)
512 goto done;
514 if (commit_id_str == NULL) {
515 struct got_reference *head_ref;
516 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
517 if (error != NULL)
518 goto done;
519 error = got_ref_resolve(&commit_id, repo, head_ref);
520 if (error != NULL)
521 goto done;
522 error = got_object_id_str(&commit_id_str, commit_id);
523 if (error != NULL)
524 goto done;
525 } else {
526 error = got_object_resolve_id_str(&commit_id, repo,
527 commit_id_str);
528 if (error != NULL)
529 goto done;
532 error = check_ancestry(worktree, commit_id, repo);
533 if (error != NULL)
534 goto done;
536 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
537 commit_id) != 0) {
538 error = got_worktree_set_base_commit_id(worktree, repo,
539 commit_id);
540 if (error)
541 goto done;
544 error = got_worktree_checkout_files(worktree, repo,
545 update_progress, &did_something, check_cancelled, NULL);
546 if (error != NULL)
547 goto done;
549 if (did_something)
550 printf("Updated to commit %s\n", commit_id_str);
551 else
552 printf("Already up-to-date\n");
553 done:
554 free(worktree_path);
555 free(commit_id);
556 free(commit_id_str);
557 return error;
560 static const struct got_error *
561 print_patch(struct got_commit_object *commit, struct got_object_id *id,
562 int diff_context, struct got_repository *repo)
564 const struct got_error *err = NULL;
565 struct got_tree_object *tree1 = NULL, *tree2;
566 struct got_object_qid *qid;
567 char *id_str1 = NULL, *id_str2;
569 err = got_object_open_as_tree(&tree2, repo,
570 got_object_commit_get_tree_id(commit));
571 if (err)
572 return err;
574 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
575 if (qid != NULL) {
576 struct got_commit_object *pcommit;
578 err = got_object_open_as_commit(&pcommit, repo, qid->id);
579 if (err)
580 return err;
582 err = got_object_open_as_tree(&tree1, repo,
583 got_object_commit_get_tree_id(pcommit));
584 got_object_commit_close(pcommit);
585 if (err)
586 return err;
588 err = got_object_id_str(&id_str1, qid->id);
589 if (err)
590 return err;
593 err = got_object_id_str(&id_str2, id);
594 if (err)
595 goto done;
597 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
598 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
599 done:
600 if (tree1)
601 got_object_tree_close(tree1);
602 got_object_tree_close(tree2);
603 free(id_str1);
604 free(id_str2);
605 return err;
608 static char *
609 get_datestr(time_t *time, char *datebuf)
611 char *p, *s = ctime_r(time, datebuf);
612 p = strchr(s, '\n');
613 if (p)
614 *p = '\0';
615 return s;
618 static const struct got_error *
619 print_commit(struct got_commit_object *commit, struct got_object_id *id,
620 struct got_repository *repo, int show_patch, int diff_context,
621 struct got_reflist_head *refs)
623 const struct got_error *err = NULL;
624 char *id_str, *datestr, *logmsg0, *logmsg, *line;
625 char datebuf[26];
626 time_t committer_time;
627 const char *author, *committer;
628 char *refs_str = NULL;
629 struct got_reflist_entry *re;
631 SIMPLEQ_FOREACH(re, refs, entry) {
632 char *s;
633 const char *name;
634 if (got_object_id_cmp(re->id, id) != 0)
635 continue;
636 name = got_ref_get_name(re->ref);
637 if (strcmp(name, GOT_REF_HEAD) == 0)
638 continue;
639 if (strncmp(name, "refs/", 5) == 0)
640 name += 5;
641 if (strncmp(name, "got/", 4) == 0)
642 continue;
643 if (strncmp(name, "heads/", 6) == 0)
644 name += 6;
645 if (strncmp(name, "remotes/", 8) == 0)
646 name += 8;
647 s = refs_str;
648 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
649 name) == -1) {
650 err = got_error_from_errno();
651 free(s);
652 break;
654 free(s);
656 err = got_object_id_str(&id_str, id);
657 if (err)
658 return err;
660 printf("-----------------------------------------------\n");
661 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
662 refs_str ? refs_str : "", refs_str ? ")" : "");
663 free(id_str);
664 id_str = NULL;
665 free(refs_str);
666 refs_str = NULL;
667 printf("from: %s\n", got_object_commit_get_author(commit));
668 committer_time = got_object_commit_get_committer_time(commit);
669 datestr = get_datestr(&committer_time, datebuf);
670 printf("date: %s UTC\n", datestr);
671 author = got_object_commit_get_author(commit);
672 committer = got_object_commit_get_committer(commit);
673 if (strcmp(author, committer) != 0)
674 printf("via: %s\n", committer);
675 if (got_object_commit_get_nparents(commit) > 1) {
676 const struct got_object_id_queue *parent_ids;
677 struct got_object_qid *qid;
678 int n = 1;
679 parent_ids = got_object_commit_get_parent_ids(commit);
680 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
681 err = got_object_id_str(&id_str, qid->id);
682 if (err)
683 return err;
684 printf("parent %d: %s\n", n++, id_str);
685 free(id_str);
689 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
690 if (logmsg0 == NULL)
691 return got_error_from_errno();
693 logmsg = logmsg0;
694 do {
695 line = strsep(&logmsg, "\n");
696 if (line)
697 printf(" %s\n", line);
698 } while (line);
699 free(logmsg0);
701 if (show_patch) {
702 err = print_patch(commit, id, diff_context, repo);
703 if (err == 0)
704 printf("\n");
707 if (fflush(stdout) != 0 && err == NULL)
708 err = got_error_from_errno();
709 return err;
712 static const struct got_error *
713 print_commits(struct got_object_id *root_id, struct got_repository *repo,
714 char *path, int show_patch, int diff_context, int limit,
715 int first_parent_traversal, struct got_reflist_head *refs)
717 const struct got_error *err;
718 struct got_commit_graph *graph;
720 err = got_commit_graph_open(&graph, root_id, path,
721 first_parent_traversal, repo);
722 if (err)
723 return err;
724 err = got_commit_graph_iter_start(graph, root_id, repo);
725 if (err)
726 goto done;
727 while (1) {
728 struct got_commit_object *commit;
729 struct got_object_id *id;
731 if (sigint_received || sigpipe_received)
732 break;
734 err = got_commit_graph_iter_next(&id, graph);
735 if (err) {
736 if (err->code == GOT_ERR_ITER_COMPLETED) {
737 err = NULL;
738 break;
740 if (err->code != GOT_ERR_ITER_NEED_MORE)
741 break;
742 err = got_commit_graph_fetch_commits(graph, 1, repo);
743 if (err)
744 break;
745 else
746 continue;
748 if (id == NULL)
749 break;
751 err = got_object_open_as_commit(&commit, repo, id);
752 if (err)
753 break;
754 err = print_commit(commit, id, repo, show_patch, diff_context,
755 refs);
756 got_object_commit_close(commit);
757 if (err || (limit && --limit == 0))
758 break;
760 done:
761 got_commit_graph_close(graph);
762 return err;
765 __dead static void
766 usage_log(void)
768 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
769 "[-r repository-path] [path]\n", getprogname());
770 exit(1);
773 static const struct got_error *
774 cmd_log(int argc, char *argv[])
776 const struct got_error *error;
777 struct got_repository *repo = NULL;
778 struct got_worktree *worktree = NULL;
779 struct got_commit_object *commit = NULL;
780 struct got_object_id *id = NULL;
781 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
782 char *start_commit = NULL;
783 int diff_context = 3, ch;
784 int show_patch = 0, limit = 0, first_parent_traversal = 0;
785 const char *errstr;
786 struct got_reflist_head refs;
788 SIMPLEQ_INIT(&refs);
790 #ifndef PROFILE
791 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
792 NULL)
793 == -1)
794 err(1, "pledge");
795 #endif
797 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
798 switch (ch) {
799 case 'p':
800 show_patch = 1;
801 break;
802 case 'c':
803 start_commit = optarg;
804 break;
805 case 'C':
806 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
807 &errstr);
808 if (errstr != NULL)
809 err(1, "-C option %s", errstr);
810 break;
811 case 'l':
812 limit = strtonum(optarg, 1, INT_MAX, &errstr);
813 if (errstr != NULL)
814 err(1, "-l option %s", errstr);
815 break;
816 case 'f':
817 first_parent_traversal = 1;
818 break;
819 case 'r':
820 repo_path = realpath(optarg, NULL);
821 if (repo_path == NULL)
822 err(1, "-r option");
823 break;
824 default:
825 usage_log();
826 /* NOTREACHED */
830 argc -= optind;
831 argv += optind;
833 cwd = getcwd(NULL, 0);
834 if (cwd == NULL) {
835 error = got_error_from_errno();
836 goto done;
839 error = got_worktree_open(&worktree, cwd);
840 if (error && error->code != GOT_ERR_NOT_WORKTREE)
841 goto done;
842 error = NULL;
844 if (argc == 0) {
845 path = strdup("");
846 if (path == NULL) {
847 error = got_error_from_errno();
848 goto done;
850 } else if (argc == 1) {
851 if (worktree) {
852 error = got_worktree_resolve_path(&path, worktree,
853 argv[0]);
854 if (error)
855 goto done;
856 } else {
857 path = strdup(argv[0]);
858 if (path == NULL) {
859 error = got_error_from_errno();
860 goto done;
863 } else
864 usage_log();
866 repo_path = worktree ?
867 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
868 if (repo_path == NULL) {
869 error = got_error_from_errno();
870 goto done;
873 error = apply_unveil(repo_path, 1,
874 worktree ? got_worktree_get_root_path(worktree) : NULL);
875 if (error)
876 goto done;
878 error = got_repo_open(&repo, repo_path);
879 if (error != NULL)
880 goto done;
882 if (start_commit == NULL) {
883 struct got_reference *head_ref;
884 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
885 if (error != NULL)
886 return error;
887 error = got_ref_resolve(&id, repo, head_ref);
888 got_ref_close(head_ref);
889 if (error != NULL)
890 return error;
891 error = got_object_open_as_commit(&commit, repo, id);
892 } else {
893 struct got_reference *ref;
894 error = got_ref_open(&ref, repo, start_commit);
895 if (error == NULL) {
896 int obj_type;
897 error = got_ref_resolve(&id, repo, ref);
898 got_ref_close(ref);
899 if (error != NULL)
900 goto done;
901 error = got_object_get_type(&obj_type, repo, id);
902 if (error != NULL)
903 goto done;
904 if (obj_type == GOT_OBJ_TYPE_TAG) {
905 struct got_tag_object *tag;
906 error = got_object_open_as_tag(&tag, repo, id);
907 if (error != NULL)
908 goto done;
909 if (got_object_tag_get_object_type(tag) !=
910 GOT_OBJ_TYPE_COMMIT) {
911 got_object_tag_close(tag);
912 error = got_error(GOT_ERR_OBJ_TYPE);
913 goto done;
915 free(id);
916 id = got_object_id_dup(
917 got_object_tag_get_object_id(tag));
918 if (id == NULL)
919 error = got_error_from_errno();
920 got_object_tag_close(tag);
921 if (error)
922 goto done;
923 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
924 error = got_error(GOT_ERR_OBJ_TYPE);
925 goto done;
927 error = got_object_open_as_commit(&commit, repo, id);
928 if (error != NULL)
929 goto done;
931 if (commit == NULL) {
932 error = got_object_resolve_id_str(&id, repo,
933 start_commit);
934 if (error != NULL)
935 return error;
938 if (error != NULL)
939 goto done;
941 error = got_repo_map_path(&in_repo_path, repo, path, 1);
942 if (error != NULL)
943 goto done;
944 if (in_repo_path) {
945 free(path);
946 path = in_repo_path;
949 error = got_ref_list(&refs, repo);
950 if (error)
951 goto done;
953 error = print_commits(id, repo, path, show_patch,
954 diff_context, limit, first_parent_traversal, &refs);
955 done:
956 free(path);
957 free(repo_path);
958 free(cwd);
959 free(id);
960 if (worktree)
961 got_worktree_close(worktree);
962 if (repo) {
963 const struct got_error *repo_error;
964 repo_error = got_repo_close(repo);
965 if (error == NULL)
966 error = repo_error;
968 got_ref_list_free(&refs);
969 return error;
972 __dead static void
973 usage_diff(void)
975 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
976 "[object1 object2 | path]\n", getprogname());
977 exit(1);
980 struct print_diff_arg {
981 struct got_repository *repo;
982 struct got_worktree *worktree;
983 int diff_context;
984 const char *id_str;
985 int header_shown;
986 };
988 static const struct got_error *
989 print_diff(void *arg, unsigned char status, const char *path,
990 struct got_object_id *id)
992 struct print_diff_arg *a = arg;
993 const struct got_error *err = NULL;
994 struct got_blob_object *blob1 = NULL;
995 FILE *f2 = NULL;
996 char *abspath = NULL;
997 struct stat sb;
999 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD)
1000 return NULL;
1002 if (!a->header_shown) {
1003 printf("diff %s %s\n", a->id_str,
1004 got_worktree_get_root_path(a->worktree));
1005 a->header_shown = 1;
1008 if (status == GOT_STATUS_MODIFY) {
1009 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1010 if (err)
1011 goto done;
1015 if (asprintf(&abspath, "%s/%s",
1016 got_worktree_get_root_path(a->worktree), path) == -1) {
1017 err = got_error_from_errno();
1018 goto done;
1021 f2 = fopen(abspath, "r");
1022 if (f2 == NULL) {
1023 err = got_error_from_errno();
1024 goto done;
1026 if (lstat(abspath, &sb) == -1) {
1027 err = got_error_from_errno();
1028 goto done;
1031 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1032 stdout);
1033 done:
1034 if (blob1)
1035 got_object_blob_close(blob1);
1036 if (f2 && fclose(f2) != 0 && err == NULL)
1037 err = got_error_from_errno();
1038 free(abspath);
1039 return err;
1042 static const struct got_error *
1043 cmd_diff(int argc, char *argv[])
1045 const struct got_error *error;
1046 struct got_repository *repo = NULL;
1047 struct got_worktree *worktree = NULL;
1048 char *cwd = NULL, *repo_path = NULL;
1049 struct got_object_id *id1 = NULL, *id2 = NULL;
1050 char *id_str1 = NULL, *id_str2 = NULL;
1051 int type1, type2;
1052 int diff_context = 3, ch;
1053 const char *errstr;
1054 char *path = NULL;
1056 #ifndef PROFILE
1057 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1058 NULL) == -1)
1059 err(1, "pledge");
1060 #endif
1062 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1063 switch (ch) {
1064 case 'C':
1065 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1066 if (errstr != NULL)
1067 err(1, "-C option %s", errstr);
1068 break;
1069 case 'r':
1070 repo_path = realpath(optarg, NULL);
1071 if (repo_path == NULL)
1072 err(1, "-r option");
1073 break;
1074 default:
1075 usage_diff();
1076 /* NOTREACHED */
1080 argc -= optind;
1081 argv += optind;
1083 cwd = getcwd(NULL, 0);
1084 if (cwd == NULL) {
1085 error = got_error_from_errno();
1086 goto done;
1088 error = got_worktree_open(&worktree, cwd);
1089 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1090 goto done;
1091 if (argc <= 1) {
1092 if (worktree == NULL) {
1093 error = got_error(GOT_ERR_NOT_WORKTREE);
1094 goto done;
1096 if (repo_path)
1097 errx(1,
1098 "-r option can't be used when diffing a work tree");
1099 repo_path = strdup(got_worktree_get_repo_path(worktree));
1100 if (repo_path == NULL) {
1101 error = got_error_from_errno();
1102 goto done;
1104 if (argc == 1) {
1105 error = got_worktree_resolve_path(&path, worktree,
1106 argv[0]);
1107 if (error)
1108 goto done;
1109 } else {
1110 path = strdup("");
1111 if (path == NULL) {
1112 error = got_error_from_errno();
1113 goto done;
1116 } else if (argc == 2) {
1117 id_str1 = argv[0];
1118 id_str2 = argv[1];
1119 } else
1120 usage_diff();
1122 if (repo_path == NULL) {
1123 repo_path = getcwd(NULL, 0);
1124 if (repo_path == NULL)
1125 return got_error_from_errno();
1128 error = apply_unveil(repo_path, 1,
1129 worktree ? got_worktree_get_root_path(worktree) : NULL);
1130 if (error)
1131 goto done;
1133 error = got_repo_open(&repo, repo_path);
1134 free(repo_path);
1135 if (error != NULL)
1136 goto done;
1138 if (worktree) {
1139 struct print_diff_arg arg;
1140 char *id_str;
1141 error = got_object_id_str(&id_str,
1142 got_worktree_get_base_commit_id(worktree));
1143 if (error)
1144 goto done;
1145 arg.repo = repo;
1146 arg.worktree = worktree;
1147 arg.diff_context = diff_context;
1148 arg.id_str = id_str;
1149 arg.header_shown = 0;
1151 error = got_worktree_status(worktree, path, repo, print_diff,
1152 &arg, check_cancelled, NULL);
1153 free(id_str);
1154 goto done;
1157 error = got_object_resolve_id_str(&id1, repo, id_str1);
1158 if (error)
1159 goto done;
1161 error = got_object_resolve_id_str(&id2, repo, id_str2);
1162 if (error)
1163 goto done;
1165 error = got_object_get_type(&type1, repo, id1);
1166 if (error)
1167 goto done;
1169 error = got_object_get_type(&type2, repo, id2);
1170 if (error)
1171 goto done;
1173 if (type1 != type2) {
1174 error = got_error(GOT_ERR_OBJ_TYPE);
1175 goto done;
1178 switch (type1) {
1179 case GOT_OBJ_TYPE_BLOB:
1180 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1181 diff_context, repo, stdout);
1182 break;
1183 case GOT_OBJ_TYPE_TREE:
1184 error = got_diff_objects_as_trees(id1, id2, "", "",
1185 diff_context, repo, stdout);
1186 break;
1187 case GOT_OBJ_TYPE_COMMIT:
1188 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1189 id_str2);
1190 error = got_diff_objects_as_commits(id1, id2, diff_context,
1191 repo, stdout);
1192 break;
1193 default:
1194 error = got_error(GOT_ERR_OBJ_TYPE);
1197 done:
1198 free(id1);
1199 free(id2);
1200 free(path);
1201 if (worktree)
1202 got_worktree_close(worktree);
1203 if (repo) {
1204 const struct got_error *repo_error;
1205 repo_error = got_repo_close(repo);
1206 if (error == NULL)
1207 error = repo_error;
1209 return error;
1212 __dead static void
1213 usage_blame(void)
1215 fprintf(stderr,
1216 "usage: %s blame [-c commit] [-r repository-path] path\n",
1217 getprogname());
1218 exit(1);
1221 static const struct got_error *
1222 cmd_blame(int argc, char *argv[])
1224 const struct got_error *error;
1225 struct got_repository *repo = NULL;
1226 struct got_worktree *worktree = NULL;
1227 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1228 struct got_object_id *commit_id = NULL;
1229 char *commit_id_str = NULL;
1230 int ch;
1232 #ifndef PROFILE
1233 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1234 NULL) == -1)
1235 err(1, "pledge");
1236 #endif
1238 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1239 switch (ch) {
1240 case 'c':
1241 commit_id_str = optarg;
1242 break;
1243 case 'r':
1244 repo_path = realpath(optarg, NULL);
1245 if (repo_path == NULL)
1246 err(1, "-r option");
1247 break;
1248 default:
1249 usage_blame();
1250 /* NOTREACHED */
1254 argc -= optind;
1255 argv += optind;
1257 if (argc == 1)
1258 path = argv[0];
1259 else
1260 usage_blame();
1262 cwd = getcwd(NULL, 0);
1263 if (cwd == NULL) {
1264 error = got_error_from_errno();
1265 goto done;
1267 if (repo_path == NULL) {
1268 error = got_worktree_open(&worktree, cwd);
1269 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1270 goto done;
1271 else
1272 error = NULL;
1273 if (worktree) {
1274 repo_path =
1275 strdup(got_worktree_get_repo_path(worktree));
1276 if (repo_path == NULL)
1277 error = got_error_from_errno();
1278 if (error)
1279 goto done;
1280 } else {
1281 repo_path = strdup(cwd);
1282 if (repo_path == NULL) {
1283 error = got_error_from_errno();
1284 goto done;
1289 error = apply_unveil(repo_path, 1, NULL);
1290 if (error)
1291 goto done;
1293 error = got_repo_open(&repo, repo_path);
1294 if (error != NULL)
1295 goto done;
1297 if (worktree) {
1298 const char *prefix = got_worktree_get_path_prefix(worktree);
1299 char *p, *worktree_subdir = cwd +
1300 strlen(got_worktree_get_root_path(worktree));
1301 if (asprintf(&p, "%s%s%s%s%s",
1302 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1303 worktree_subdir, worktree_subdir[0] ? "/" : "",
1304 path) == -1) {
1305 error = got_error_from_errno();
1306 goto done;
1308 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1309 free(p);
1310 } else {
1311 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1313 if (error)
1314 goto done;
1316 if (commit_id_str == NULL) {
1317 struct got_reference *head_ref;
1318 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1319 if (error != NULL)
1320 goto done;
1321 error = got_ref_resolve(&commit_id, repo, head_ref);
1322 got_ref_close(head_ref);
1323 if (error != NULL)
1324 goto done;
1325 } else {
1326 error = got_object_resolve_id_str(&commit_id, repo,
1327 commit_id_str);
1328 if (error != NULL)
1329 goto done;
1332 error = got_blame(in_repo_path, commit_id, repo, stdout);
1333 done:
1334 free(in_repo_path);
1335 free(repo_path);
1336 free(cwd);
1337 free(commit_id);
1338 if (worktree)
1339 got_worktree_close(worktree);
1340 if (repo) {
1341 const struct got_error *repo_error;
1342 repo_error = got_repo_close(repo);
1343 if (error == NULL)
1344 error = repo_error;
1346 return error;
1349 __dead static void
1350 usage_tree(void)
1352 fprintf(stderr,
1353 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1354 getprogname());
1355 exit(1);
1358 static void
1359 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1360 const char *root_path)
1362 int is_root_path = (strcmp(path, root_path) == 0);
1364 path += strlen(root_path);
1365 while (path[0] == '/')
1366 path++;
1368 printf("%s%s%s%s%s\n", id ? id : "", path,
1369 is_root_path ? "" : "/", te->name,
1370 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1373 static const struct got_error *
1374 print_tree(const char *path, struct got_object_id *commit_id,
1375 int show_ids, int recurse, const char *root_path,
1376 struct got_repository *repo)
1378 const struct got_error *err = NULL;
1379 struct got_object_id *tree_id = NULL;
1380 struct got_tree_object *tree = NULL;
1381 const struct got_tree_entries *entries;
1382 struct got_tree_entry *te;
1384 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1385 if (err)
1386 goto done;
1388 err = got_object_open_as_tree(&tree, repo, tree_id);
1389 if (err)
1390 goto done;
1391 entries = got_object_tree_get_entries(tree);
1392 te = SIMPLEQ_FIRST(&entries->head);
1393 while (te) {
1394 char *id = NULL;
1396 if (sigint_received || sigpipe_received)
1397 break;
1399 if (show_ids) {
1400 char *id_str;
1401 err = got_object_id_str(&id_str, te->id);
1402 if (err)
1403 goto done;
1404 if (asprintf(&id, "%s ", id_str) == -1) {
1405 err = got_error_from_errno();
1406 free(id_str);
1407 goto done;
1409 free(id_str);
1411 print_entry(te, id, path, root_path);
1412 free(id);
1414 if (recurse && S_ISDIR(te->mode)) {
1415 char *child_path;
1416 if (asprintf(&child_path, "%s%s%s", path,
1417 path[0] == '/' && path[1] == '\0' ? "" : "/",
1418 te->name) == -1) {
1419 err = got_error_from_errno();
1420 goto done;
1422 err = print_tree(child_path, commit_id, show_ids, 1,
1423 root_path, repo);
1424 free(child_path);
1425 if (err)
1426 goto done;
1429 te = SIMPLEQ_NEXT(te, entry);
1431 done:
1432 if (tree)
1433 got_object_tree_close(tree);
1434 free(tree_id);
1435 return err;
1438 static const struct got_error *
1439 cmd_tree(int argc, char *argv[])
1441 const struct got_error *error;
1442 struct got_repository *repo = NULL;
1443 struct got_worktree *worktree = NULL;
1444 const char *path;
1445 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1446 struct got_object_id *commit_id = NULL;
1447 char *commit_id_str = NULL;
1448 int show_ids = 0, recurse = 0;
1449 int ch;
1451 #ifndef PROFILE
1452 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1453 NULL) == -1)
1454 err(1, "pledge");
1455 #endif
1457 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1458 switch (ch) {
1459 case 'c':
1460 commit_id_str = optarg;
1461 break;
1462 case 'r':
1463 repo_path = realpath(optarg, NULL);
1464 if (repo_path == NULL)
1465 err(1, "-r option");
1466 break;
1467 case 'i':
1468 show_ids = 1;
1469 break;
1470 case 'R':
1471 recurse = 1;
1472 break;
1473 default:
1474 usage_tree();
1475 /* NOTREACHED */
1479 argc -= optind;
1480 argv += optind;
1482 if (argc == 1)
1483 path = argv[0];
1484 else if (argc > 1)
1485 usage_tree();
1486 else
1487 path = NULL;
1489 cwd = getcwd(NULL, 0);
1490 if (cwd == NULL) {
1491 error = got_error_from_errno();
1492 goto done;
1494 if (repo_path == NULL) {
1495 error = got_worktree_open(&worktree, cwd);
1496 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1497 goto done;
1498 else
1499 error = NULL;
1500 if (worktree) {
1501 repo_path =
1502 strdup(got_worktree_get_repo_path(worktree));
1503 if (repo_path == NULL)
1504 error = got_error_from_errno();
1505 if (error)
1506 goto done;
1507 } else {
1508 repo_path = strdup(cwd);
1509 if (repo_path == NULL) {
1510 error = got_error_from_errno();
1511 goto done;
1516 error = apply_unveil(repo_path, 1, NULL);
1517 if (error)
1518 goto done;
1520 error = got_repo_open(&repo, repo_path);
1521 if (error != NULL)
1522 goto done;
1524 if (path == NULL) {
1525 if (worktree) {
1526 char *p, *worktree_subdir = cwd +
1527 strlen(got_worktree_get_root_path(worktree));
1528 if (asprintf(&p, "%s/%s",
1529 got_worktree_get_path_prefix(worktree),
1530 worktree_subdir) == -1) {
1531 error = got_error_from_errno();
1532 goto done;
1534 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1535 free(p);
1536 if (error)
1537 goto done;
1538 } else
1539 path = "/";
1541 if (in_repo_path == NULL) {
1542 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1543 if (error != NULL)
1544 goto done;
1547 if (commit_id_str == NULL) {
1548 struct got_reference *head_ref;
1549 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1550 if (error != NULL)
1551 goto done;
1552 error = got_ref_resolve(&commit_id, repo, head_ref);
1553 got_ref_close(head_ref);
1554 if (error != NULL)
1555 goto done;
1556 } else {
1557 error = got_object_resolve_id_str(&commit_id, repo,
1558 commit_id_str);
1559 if (error != NULL)
1560 goto done;
1563 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1564 in_repo_path, repo);
1565 done:
1566 free(in_repo_path);
1567 free(repo_path);
1568 free(cwd);
1569 free(commit_id);
1570 if (worktree)
1571 got_worktree_close(worktree);
1572 if (repo) {
1573 const struct got_error *repo_error;
1574 repo_error = got_repo_close(repo);
1575 if (error == NULL)
1576 error = repo_error;
1578 return error;
1581 __dead static void
1582 usage_status(void)
1584 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1585 exit(1);
1588 static const struct got_error *
1589 print_status(void *arg, unsigned char status, const char *path,
1590 struct got_object_id *id)
1592 printf("%c %s\n", status, path);
1593 return NULL;
1596 static const struct got_error *
1597 cmd_status(int argc, char *argv[])
1599 const struct got_error *error = NULL;
1600 struct got_repository *repo = NULL;
1601 struct got_worktree *worktree = NULL;
1602 char *cwd = NULL, *path = NULL;
1603 int ch;
1605 while ((ch = getopt(argc, argv, "")) != -1) {
1606 switch (ch) {
1607 default:
1608 usage_status();
1609 /* NOTREACHED */
1613 argc -= optind;
1614 argv += optind;
1616 #ifndef PROFILE
1617 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1618 NULL) == -1)
1619 err(1, "pledge");
1620 #endif
1621 cwd = getcwd(NULL, 0);
1622 if (cwd == NULL) {
1623 error = got_error_from_errno();
1624 goto done;
1627 error = got_worktree_open(&worktree, cwd);
1628 if (error != NULL)
1629 goto done;
1631 if (argc == 0) {
1632 path = strdup("");
1633 if (path == NULL) {
1634 error = got_error_from_errno();
1635 goto done;
1637 } else if (argc == 1) {
1638 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1639 if (error)
1640 goto done;
1641 } else
1642 usage_status();
1644 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1645 if (error != NULL)
1646 goto done;
1648 error = apply_unveil(got_repo_get_path(repo), 1,
1649 got_worktree_get_root_path(worktree));
1650 if (error)
1651 goto done;
1653 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1654 check_cancelled, NULL);
1655 done:
1656 free(cwd);
1657 free(path);
1658 return error;
1661 __dead static void
1662 usage_ref(void)
1664 fprintf(stderr,
1665 "usage: %s ref [-r repository] -l | -d name | name object\n",
1666 getprogname());
1667 exit(1);
1670 static const struct got_error *
1671 list_refs(struct got_repository *repo)
1673 static const struct got_error *err = NULL;
1674 struct got_reflist_head refs;
1675 struct got_reflist_entry *re;
1677 SIMPLEQ_INIT(&refs);
1678 err = got_ref_list(&refs, repo);
1679 if (err)
1680 return err;
1682 SIMPLEQ_FOREACH(re, &refs, entry) {
1683 char *refstr;
1684 refstr = got_ref_to_str(re->ref);
1685 if (refstr == NULL)
1686 return got_error_from_errno();
1687 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1688 free(refstr);
1691 got_ref_list_free(&refs);
1692 return NULL;
1695 static const struct got_error *
1696 delete_ref(struct got_repository *repo, const char *refname)
1698 const struct got_error *err = NULL;
1699 struct got_reference *ref;
1701 err = got_ref_open(&ref, repo, refname);
1702 if (err)
1703 return err;
1705 err = got_ref_delete(ref, repo);
1706 got_ref_close(ref);
1707 return err;
1710 static const struct got_error *
1711 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1713 const struct got_error *err = NULL;
1714 struct got_object_id *id;
1715 struct got_reference *ref = NULL;
1717 err = got_object_resolve_id_str(&id, repo, id_str);
1718 if (err)
1719 return err;
1721 err = got_ref_alloc(&ref, refname, id);
1722 if (err)
1723 goto done;
1725 err = got_ref_write(ref, repo);
1726 done:
1727 if (ref)
1728 got_ref_close(ref);
1729 free(id);
1730 return err;
1733 static const struct got_error *
1734 cmd_ref(int argc, char *argv[])
1736 const struct got_error *error = NULL;
1737 struct got_repository *repo = NULL;
1738 struct got_worktree *worktree = NULL;
1739 char *cwd = NULL, *repo_path = NULL;
1740 int ch, do_list = 0;
1741 const char *delref = NULL;
1743 /* TODO: Add -s option for adding symbolic references. */
1744 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1745 switch (ch) {
1746 case 'd':
1747 delref = optarg;
1748 break;
1749 case 'r':
1750 repo_path = realpath(optarg, NULL);
1751 if (repo_path == NULL)
1752 err(1, "-r option");
1753 break;
1754 case 'l':
1755 do_list = 1;
1756 break;
1757 default:
1758 usage_ref();
1759 /* NOTREACHED */
1763 if (do_list && delref)
1764 errx(1, "-l and -d options are mutually exclusive\n");
1766 argc -= optind;
1767 argv += optind;
1769 if (do_list || delref) {
1770 if (argc > 0)
1771 usage_ref();
1772 } else if (argc != 2)
1773 usage_ref();
1775 #ifndef PROFILE
1776 if (do_list) {
1777 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1778 NULL) == -1)
1779 err(1, "pledge");
1780 } else {
1781 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1782 "sendfd unveil", NULL) == -1)
1783 err(1, "pledge");
1785 #endif
1786 cwd = getcwd(NULL, 0);
1787 if (cwd == NULL) {
1788 error = got_error_from_errno();
1789 goto done;
1792 if (repo_path == NULL) {
1793 error = got_worktree_open(&worktree, cwd);
1794 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1795 goto done;
1796 else
1797 error = NULL;
1798 if (worktree) {
1799 repo_path =
1800 strdup(got_worktree_get_repo_path(worktree));
1801 if (repo_path == NULL)
1802 error = got_error_from_errno();
1803 if (error)
1804 goto done;
1805 } else {
1806 repo_path = strdup(cwd);
1807 if (repo_path == NULL) {
1808 error = got_error_from_errno();
1809 goto done;
1814 error = apply_unveil(repo_path, do_list,
1815 worktree ? got_worktree_get_root_path(worktree) : NULL);
1816 if (error)
1817 goto done;
1819 error = got_repo_open(&repo, repo_path);
1820 if (error != NULL)
1821 goto done;
1823 if (do_list)
1824 error = list_refs(repo);
1825 else if (delref)
1826 error = delete_ref(repo, delref);
1827 else
1828 error = add_ref(repo, argv[0], argv[1]);
1829 done:
1830 if (repo)
1831 got_repo_close(repo);
1832 if (worktree)
1833 got_worktree_close(worktree);
1834 free(cwd);
1835 free(repo_path);
1836 return error;
1839 __dead static void
1840 usage_add(void)
1842 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1843 exit(1);
1846 static const struct got_error *
1847 cmd_add(int argc, char *argv[])
1849 const struct got_error *error = NULL;
1850 struct got_worktree *worktree = NULL;
1851 char *cwd = NULL, *path = NULL, *relpath = NULL;
1852 int ch;
1854 while ((ch = getopt(argc, argv, "")) != -1) {
1855 switch (ch) {
1856 default:
1857 usage_add();
1858 /* NOTREACHED */
1862 argc -= optind;
1863 argv += optind;
1865 if (argc != 1)
1866 usage_add();
1868 path = realpath(argv[0], NULL);
1869 if (path == NULL) {
1870 error = got_error_from_errno();
1871 goto done;
1874 cwd = getcwd(NULL, 0);
1875 if (cwd == NULL) {
1876 error = got_error_from_errno();
1877 goto done;
1879 error = got_worktree_open(&worktree, cwd);
1880 if (error)
1881 goto done;
1883 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
1884 if (error)
1885 goto done;
1887 error = got_worktree_schedule_add(&relpath, worktree, path);
1888 if (error)
1889 goto done;
1890 printf("%c %s\n", GOT_STATUS_ADD, relpath);
1891 done:
1892 if (worktree)
1893 got_worktree_close(worktree);
1894 free(path);
1895 free(relpath);
1896 free(cwd);
1897 return error;