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);
81 __dead static void usage_rm(void);
83 static const struct got_error* cmd_checkout(int, char *[]);
84 static const struct got_error* cmd_update(int, char *[]);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_status(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
91 static const struct got_error* cmd_add(int, char *[]);
92 static const struct got_error* cmd_rm(int, char *[]);
94 static struct cmd got_commands[] = {
95 { "checkout", cmd_checkout, usage_checkout,
96 "check out a new work tree from a repository" },
97 { "update", cmd_update, usage_update,
98 "update a work tree to a different commit" },
99 { "log", cmd_log, usage_log,
100 "show repository history" },
101 { "diff", cmd_diff, usage_diff,
102 "compare files and directories" },
103 { "blame", cmd_blame, usage_blame,
104 "show when lines in a file were changed" },
105 { "tree", cmd_tree, usage_tree,
106 "list files and directories in repository" },
107 { "status", cmd_status, usage_status,
108 "show modification status of files" },
109 { "ref", cmd_ref, usage_ref,
110 "manage references in repository" },
111 { "add", cmd_add, usage_add,
112 "add a new file to version control" },
113 { "rm", cmd_rm, usage_rm,
114 "remove a versioned file" },
115 };
117 int
118 main(int argc, char *argv[])
120 struct cmd *cmd;
121 unsigned int i;
122 int ch;
123 int hflag = 0;
125 setlocale(LC_CTYPE, "");
127 while ((ch = getopt(argc, argv, "h")) != -1) {
128 switch (ch) {
129 case 'h':
130 hflag = 1;
131 break;
132 default:
133 usage();
134 /* NOTREACHED */
138 argc -= optind;
139 argv += optind;
140 optind = 0;
142 if (argc <= 0)
143 usage();
145 signal(SIGINT, catch_sigint);
146 signal(SIGPIPE, catch_sigpipe);
148 for (i = 0; i < nitems(got_commands); i++) {
149 const struct got_error *error;
151 cmd = &got_commands[i];
153 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
154 continue;
156 if (hflag)
157 got_commands[i].cmd_usage();
159 error = got_commands[i].cmd_main(argc, argv);
160 if (error && !(sigint_received || sigpipe_received)) {
161 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
162 return 1;
165 return 0;
168 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
169 return 1;
172 __dead static void
173 usage(void)
175 int i;
177 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
178 "Available commands:\n", getprogname());
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct cmd *cmd = &got_commands[i];
181 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
183 exit(1);
186 static const struct got_error *
187 apply_unveil(const char *repo_path, int repo_read_only,
188 const char *worktree_path)
190 const struct got_error *error;
192 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
193 return got_error_from_errno();
195 if (worktree_path && unveil(worktree_path, "rwc") != 0)
196 return got_error_from_errno();
198 if (unveil("/tmp", "rwc") != 0)
199 return got_error_from_errno();
201 error = got_privsep_unveil_exec_helpers();
202 if (error != NULL)
203 return error;
205 if (unveil(NULL, NULL) != 0)
206 return got_error_from_errno();
208 return NULL;
211 __dead static void
212 usage_checkout(void)
214 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
215 "[worktree-path]\n", getprogname());
216 exit(1);
219 static void
220 checkout_progress(void *arg, unsigned char status, const char *path)
222 char *worktree_path = arg;
224 while (path[0] == '/')
225 path++;
227 printf("%c %s/%s\n", status, worktree_path, path);
230 static const struct got_error *
231 check_cancelled(void *arg)
233 if (sigint_received || sigpipe_received)
234 return got_error(GOT_ERR_CANCELLED);
235 return NULL;
238 static const struct got_error *
239 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
240 struct got_repository *repo)
242 const struct got_error *err;
243 struct got_reference *head_ref = NULL;
244 struct got_object_id *head_commit_id = NULL;
245 struct got_commit_graph *graph = NULL;
247 head_ref = got_worktree_get_head_ref(worktree);
248 if (head_ref == NULL)
249 return got_error_from_errno();
251 /* TODO: Check the reflog. The head ref may have been rebased. */
252 err = got_ref_resolve(&head_commit_id, repo, head_ref);
253 if (err)
254 goto done;
256 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
257 if (err)
258 goto done;
260 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
261 if (err)
262 goto done;
263 while (1) {
264 struct got_object_id *id;
266 if (sigint_received || sigpipe_received)
267 break;
269 err = got_commit_graph_iter_next(&id, graph);
270 if (err) {
271 if (err->code == GOT_ERR_ITER_COMPLETED) {
272 err = got_error(GOT_ERR_ANCESTRY);
273 break;
275 if (err->code != GOT_ERR_ITER_NEED_MORE)
276 break;
277 err = got_commit_graph_fetch_commits(graph, 1, repo);
278 if (err)
279 break;
280 else
281 continue;
283 if (id == NULL)
284 break;
285 if (got_object_id_cmp(id, commit_id) == 0)
286 break;
288 done:
289 if (head_ref)
290 got_ref_close(head_ref);
291 if (graph)
292 got_commit_graph_close(graph);
293 return err;
297 static const struct got_error *
298 cmd_checkout(int argc, char *argv[])
300 const struct got_error *error = NULL;
301 struct got_repository *repo = NULL;
302 struct got_reference *head_ref = NULL;
303 struct got_worktree *worktree = NULL;
304 char *repo_path = NULL;
305 char *worktree_path = NULL;
306 const char *path_prefix = "";
307 char *commit_id_str = NULL;
308 int ch, same_path_prefix;
310 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
311 switch (ch) {
312 case 'c':
313 commit_id_str = strdup(optarg);
314 if (commit_id_str == NULL)
315 return got_error_from_errno();
316 break;
317 case 'p':
318 path_prefix = optarg;
319 break;
320 default:
321 usage_checkout();
322 /* NOTREACHED */
326 argc -= optind;
327 argv += optind;
329 #ifndef PROFILE
330 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
331 "unveil", NULL) == -1)
332 err(1, "pledge");
333 #endif
334 if (argc == 1) {
335 char *cwd, *base, *dotgit;
336 repo_path = realpath(argv[0], NULL);
337 if (repo_path == NULL)
338 return got_error_from_errno();
339 cwd = getcwd(NULL, 0);
340 if (cwd == NULL) {
341 error = got_error_from_errno();
342 goto done;
344 if (path_prefix[0])
345 base = basename(path_prefix);
346 else
347 base = basename(repo_path);
348 if (base == NULL) {
349 error = got_error_from_errno();
350 goto done;
352 dotgit = strstr(base, ".git");
353 if (dotgit)
354 *dotgit = '\0';
355 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
356 error = got_error_from_errno();
357 free(cwd);
358 goto done;
360 free(cwd);
361 } else if (argc == 2) {
362 repo_path = realpath(argv[0], NULL);
363 if (repo_path == NULL) {
364 error = got_error_from_errno();
365 goto done;
367 worktree_path = realpath(argv[1], NULL);
368 if (worktree_path == NULL) {
369 error = got_error_from_errno();
370 goto done;
372 } else
373 usage_checkout();
375 error = apply_unveil(repo_path, 0, worktree_path);
376 if (error)
377 goto done;
379 error = got_repo_open(&repo, repo_path);
380 if (error != NULL)
381 goto done;
383 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
384 if (error != NULL)
385 goto done;
387 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
388 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
389 goto done;
391 error = got_worktree_open(&worktree, worktree_path);
392 if (error != NULL)
393 goto done;
395 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
396 path_prefix);
397 if (error != NULL)
398 goto done;
399 if (!same_path_prefix) {
400 error = got_error(GOT_ERR_PATH_PREFIX);
401 goto done;
404 if (commit_id_str) {
405 struct got_object_id *commit_id;
406 error = got_object_resolve_id_str(&commit_id, repo,
407 commit_id_str);
408 if (error != NULL)
409 goto done;
410 error = check_ancestry(worktree, commit_id, repo);
411 if (error != NULL) {
412 free(commit_id);
413 goto done;
415 error = got_worktree_set_base_commit_id(worktree, repo,
416 commit_id);
417 free(commit_id);
418 if (error)
419 goto done;
422 error = got_worktree_checkout_files(worktree, repo,
423 checkout_progress, worktree_path, check_cancelled, NULL);
424 if (error != NULL)
425 goto done;
427 printf("Now shut up and hack\n");
429 done:
430 free(commit_id_str);
431 free(repo_path);
432 free(worktree_path);
433 return error;
436 __dead static void
437 usage_update(void)
439 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
440 getprogname());
441 exit(1);
444 static void
445 update_progress(void *arg, unsigned char status, const char *path)
447 int *did_something = arg;
449 if (status == GOT_STATUS_EXISTS)
450 return;
452 *did_something = 1;
453 while (path[0] == '/')
454 path++;
455 printf("%c %s\n", status, path);
458 static const struct got_error *
459 cmd_update(int argc, char *argv[])
461 const struct got_error *error = NULL;
462 struct got_repository *repo = NULL;
463 struct got_worktree *worktree = NULL;
464 char *worktree_path = NULL;
465 struct got_object_id *commit_id = NULL;
466 char *commit_id_str = NULL;
467 int ch, did_something = 0;
469 while ((ch = getopt(argc, argv, "c:")) != -1) {
470 switch (ch) {
471 case 'c':
472 commit_id_str = strdup(optarg);
473 if (commit_id_str == NULL)
474 return got_error_from_errno();
475 break;
476 default:
477 usage_update();
478 /* NOTREACHED */
482 argc -= optind;
483 argv += optind;
485 #ifndef PROFILE
486 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
487 "unveil", NULL) == -1)
488 err(1, "pledge");
489 #endif
490 if (argc == 0) {
491 worktree_path = getcwd(NULL, 0);
492 if (worktree_path == NULL) {
493 error = got_error_from_errno();
494 goto done;
496 } else if (argc == 1) {
497 worktree_path = realpath(argv[0], NULL);
498 if (worktree_path == NULL) {
499 error = got_error_from_errno();
500 goto done;
502 } else
503 usage_update();
505 error = got_worktree_open(&worktree, worktree_path);
506 if (error != NULL)
507 goto done;
509 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
510 if (error != NULL)
511 goto done;
513 error = apply_unveil(got_repo_get_path(repo), 0,
514 got_worktree_get_root_path(worktree));
515 if (error)
516 goto done;
518 if (commit_id_str == NULL) {
519 struct got_reference *head_ref;
520 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
521 if (error != NULL)
522 goto done;
523 error = got_ref_resolve(&commit_id, repo, head_ref);
524 if (error != NULL)
525 goto done;
526 error = got_object_id_str(&commit_id_str, commit_id);
527 if (error != NULL)
528 goto done;
529 } else {
530 error = got_object_resolve_id_str(&commit_id, repo,
531 commit_id_str);
532 if (error != NULL)
533 goto done;
536 error = check_ancestry(worktree, commit_id, repo);
537 if (error != NULL)
538 goto done;
540 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
541 commit_id) != 0) {
542 error = got_worktree_set_base_commit_id(worktree, repo,
543 commit_id);
544 if (error)
545 goto done;
548 error = got_worktree_checkout_files(worktree, repo,
549 update_progress, &did_something, check_cancelled, NULL);
550 if (error != NULL)
551 goto done;
553 if (did_something)
554 printf("Updated to commit %s\n", commit_id_str);
555 else
556 printf("Already up-to-date\n");
557 done:
558 free(worktree_path);
559 free(commit_id);
560 free(commit_id_str);
561 return error;
564 static const struct got_error *
565 print_patch(struct got_commit_object *commit, struct got_object_id *id,
566 int diff_context, struct got_repository *repo)
568 const struct got_error *err = NULL;
569 struct got_tree_object *tree1 = NULL, *tree2;
570 struct got_object_qid *qid;
571 char *id_str1 = NULL, *id_str2;
573 err = got_object_open_as_tree(&tree2, repo,
574 got_object_commit_get_tree_id(commit));
575 if (err)
576 return err;
578 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
579 if (qid != NULL) {
580 struct got_commit_object *pcommit;
582 err = got_object_open_as_commit(&pcommit, repo, qid->id);
583 if (err)
584 return err;
586 err = got_object_open_as_tree(&tree1, repo,
587 got_object_commit_get_tree_id(pcommit));
588 got_object_commit_close(pcommit);
589 if (err)
590 return err;
592 err = got_object_id_str(&id_str1, qid->id);
593 if (err)
594 return err;
597 err = got_object_id_str(&id_str2, id);
598 if (err)
599 goto done;
601 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
602 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
603 done:
604 if (tree1)
605 got_object_tree_close(tree1);
606 got_object_tree_close(tree2);
607 free(id_str1);
608 free(id_str2);
609 return err;
612 static char *
613 get_datestr(time_t *time, char *datebuf)
615 char *p, *s = ctime_r(time, datebuf);
616 p = strchr(s, '\n');
617 if (p)
618 *p = '\0';
619 return s;
622 static const struct got_error *
623 print_commit(struct got_commit_object *commit, struct got_object_id *id,
624 struct got_repository *repo, int show_patch, int diff_context,
625 struct got_reflist_head *refs)
627 const struct got_error *err = NULL;
628 char *id_str, *datestr, *logmsg0, *logmsg, *line;
629 char datebuf[26];
630 time_t committer_time;
631 const char *author, *committer;
632 char *refs_str = NULL;
633 struct got_reflist_entry *re;
635 SIMPLEQ_FOREACH(re, refs, entry) {
636 char *s;
637 const char *name;
638 if (got_object_id_cmp(re->id, id) != 0)
639 continue;
640 name = got_ref_get_name(re->ref);
641 if (strcmp(name, GOT_REF_HEAD) == 0)
642 continue;
643 if (strncmp(name, "refs/", 5) == 0)
644 name += 5;
645 if (strncmp(name, "got/", 4) == 0)
646 continue;
647 if (strncmp(name, "heads/", 6) == 0)
648 name += 6;
649 if (strncmp(name, "remotes/", 8) == 0)
650 name += 8;
651 s = refs_str;
652 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
653 name) == -1) {
654 err = got_error_from_errno();
655 free(s);
656 break;
658 free(s);
660 err = got_object_id_str(&id_str, id);
661 if (err)
662 return err;
664 printf("-----------------------------------------------\n");
665 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
666 refs_str ? refs_str : "", refs_str ? ")" : "");
667 free(id_str);
668 id_str = NULL;
669 free(refs_str);
670 refs_str = NULL;
671 printf("from: %s\n", got_object_commit_get_author(commit));
672 committer_time = got_object_commit_get_committer_time(commit);
673 datestr = get_datestr(&committer_time, datebuf);
674 printf("date: %s UTC\n", datestr);
675 author = got_object_commit_get_author(commit);
676 committer = got_object_commit_get_committer(commit);
677 if (strcmp(author, committer) != 0)
678 printf("via: %s\n", committer);
679 if (got_object_commit_get_nparents(commit) > 1) {
680 const struct got_object_id_queue *parent_ids;
681 struct got_object_qid *qid;
682 int n = 1;
683 parent_ids = got_object_commit_get_parent_ids(commit);
684 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
685 err = got_object_id_str(&id_str, qid->id);
686 if (err)
687 return err;
688 printf("parent %d: %s\n", n++, id_str);
689 free(id_str);
693 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
694 if (logmsg0 == NULL)
695 return got_error_from_errno();
697 logmsg = logmsg0;
698 do {
699 line = strsep(&logmsg, "\n");
700 if (line)
701 printf(" %s\n", line);
702 } while (line);
703 free(logmsg0);
705 if (show_patch) {
706 err = print_patch(commit, id, diff_context, repo);
707 if (err == 0)
708 printf("\n");
711 if (fflush(stdout) != 0 && err == NULL)
712 err = got_error_from_errno();
713 return err;
716 static const struct got_error *
717 print_commits(struct got_object_id *root_id, struct got_repository *repo,
718 char *path, int show_patch, int diff_context, int limit,
719 int first_parent_traversal, struct got_reflist_head *refs)
721 const struct got_error *err;
722 struct got_commit_graph *graph;
724 err = got_commit_graph_open(&graph, root_id, path,
725 first_parent_traversal, repo);
726 if (err)
727 return err;
728 err = got_commit_graph_iter_start(graph, root_id, repo);
729 if (err)
730 goto done;
731 while (1) {
732 struct got_commit_object *commit;
733 struct got_object_id *id;
735 if (sigint_received || sigpipe_received)
736 break;
738 err = got_commit_graph_iter_next(&id, graph);
739 if (err) {
740 if (err->code == GOT_ERR_ITER_COMPLETED) {
741 err = NULL;
742 break;
744 if (err->code != GOT_ERR_ITER_NEED_MORE)
745 break;
746 err = got_commit_graph_fetch_commits(graph, 1, repo);
747 if (err)
748 break;
749 else
750 continue;
752 if (id == NULL)
753 break;
755 err = got_object_open_as_commit(&commit, repo, id);
756 if (err)
757 break;
758 err = print_commit(commit, id, repo, show_patch, diff_context,
759 refs);
760 got_object_commit_close(commit);
761 if (err || (limit && --limit == 0))
762 break;
764 done:
765 got_commit_graph_close(graph);
766 return err;
769 __dead static void
770 usage_log(void)
772 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
773 "[-r repository-path] [path]\n", getprogname());
774 exit(1);
777 static const struct got_error *
778 cmd_log(int argc, char *argv[])
780 const struct got_error *error;
781 struct got_repository *repo = NULL;
782 struct got_worktree *worktree = NULL;
783 struct got_commit_object *commit = NULL;
784 struct got_object_id *id = NULL;
785 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
786 char *start_commit = NULL;
787 int diff_context = 3, ch;
788 int show_patch = 0, limit = 0, first_parent_traversal = 0;
789 const char *errstr;
790 struct got_reflist_head refs;
792 SIMPLEQ_INIT(&refs);
794 #ifndef PROFILE
795 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
796 NULL)
797 == -1)
798 err(1, "pledge");
799 #endif
801 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
802 switch (ch) {
803 case 'p':
804 show_patch = 1;
805 break;
806 case 'c':
807 start_commit = optarg;
808 break;
809 case 'C':
810 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
811 &errstr);
812 if (errstr != NULL)
813 err(1, "-C option %s", errstr);
814 break;
815 case 'l':
816 limit = strtonum(optarg, 1, INT_MAX, &errstr);
817 if (errstr != NULL)
818 err(1, "-l option %s", errstr);
819 break;
820 case 'f':
821 first_parent_traversal = 1;
822 break;
823 case 'r':
824 repo_path = realpath(optarg, NULL);
825 if (repo_path == NULL)
826 err(1, "-r option");
827 break;
828 default:
829 usage_log();
830 /* NOTREACHED */
834 argc -= optind;
835 argv += optind;
837 cwd = getcwd(NULL, 0);
838 if (cwd == NULL) {
839 error = got_error_from_errno();
840 goto done;
843 error = got_worktree_open(&worktree, cwd);
844 if (error && error->code != GOT_ERR_NOT_WORKTREE)
845 goto done;
846 error = NULL;
848 if (argc == 0) {
849 path = strdup("");
850 if (path == NULL) {
851 error = got_error_from_errno();
852 goto done;
854 } else if (argc == 1) {
855 if (worktree) {
856 error = got_worktree_resolve_path(&path, worktree,
857 argv[0]);
858 if (error)
859 goto done;
860 } else {
861 path = strdup(argv[0]);
862 if (path == NULL) {
863 error = got_error_from_errno();
864 goto done;
867 } else
868 usage_log();
870 repo_path = worktree ?
871 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
872 if (repo_path == NULL) {
873 error = got_error_from_errno();
874 goto done;
877 error = apply_unveil(repo_path, 1,
878 worktree ? got_worktree_get_root_path(worktree) : NULL);
879 if (error)
880 goto done;
882 error = got_repo_open(&repo, repo_path);
883 if (error != NULL)
884 goto done;
886 if (start_commit == NULL) {
887 struct got_reference *head_ref;
888 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
889 if (error != NULL)
890 return error;
891 error = got_ref_resolve(&id, repo, head_ref);
892 got_ref_close(head_ref);
893 if (error != NULL)
894 return error;
895 error = got_object_open_as_commit(&commit, repo, id);
896 } else {
897 struct got_reference *ref;
898 error = got_ref_open(&ref, repo, start_commit);
899 if (error == NULL) {
900 int obj_type;
901 error = got_ref_resolve(&id, repo, ref);
902 got_ref_close(ref);
903 if (error != NULL)
904 goto done;
905 error = got_object_get_type(&obj_type, repo, id);
906 if (error != NULL)
907 goto done;
908 if (obj_type == GOT_OBJ_TYPE_TAG) {
909 struct got_tag_object *tag;
910 error = got_object_open_as_tag(&tag, repo, id);
911 if (error != NULL)
912 goto done;
913 if (got_object_tag_get_object_type(tag) !=
914 GOT_OBJ_TYPE_COMMIT) {
915 got_object_tag_close(tag);
916 error = got_error(GOT_ERR_OBJ_TYPE);
917 goto done;
919 free(id);
920 id = got_object_id_dup(
921 got_object_tag_get_object_id(tag));
922 if (id == NULL)
923 error = got_error_from_errno();
924 got_object_tag_close(tag);
925 if (error)
926 goto done;
927 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
928 error = got_error(GOT_ERR_OBJ_TYPE);
929 goto done;
931 error = got_object_open_as_commit(&commit, repo, id);
932 if (error != NULL)
933 goto done;
935 if (commit == NULL) {
936 error = got_object_resolve_id_str(&id, repo,
937 start_commit);
938 if (error != NULL)
939 return error;
942 if (error != NULL)
943 goto done;
945 error = got_repo_map_path(&in_repo_path, repo, path, 1);
946 if (error != NULL)
947 goto done;
948 if (in_repo_path) {
949 free(path);
950 path = in_repo_path;
953 error = got_ref_list(&refs, repo);
954 if (error)
955 goto done;
957 error = print_commits(id, repo, path, show_patch,
958 diff_context, limit, first_parent_traversal, &refs);
959 done:
960 free(path);
961 free(repo_path);
962 free(cwd);
963 free(id);
964 if (worktree)
965 got_worktree_close(worktree);
966 if (repo) {
967 const struct got_error *repo_error;
968 repo_error = got_repo_close(repo);
969 if (error == NULL)
970 error = repo_error;
972 got_ref_list_free(&refs);
973 return error;
976 __dead static void
977 usage_diff(void)
979 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
980 "[object1 object2 | path]\n", getprogname());
981 exit(1);
984 struct print_diff_arg {
985 struct got_repository *repo;
986 struct got_worktree *worktree;
987 int diff_context;
988 const char *id_str;
989 int header_shown;
990 };
992 static const struct got_error *
993 print_diff(void *arg, unsigned char status, const char *path,
994 struct got_object_id *id)
996 struct print_diff_arg *a = arg;
997 const struct got_error *err = NULL;
998 struct got_blob_object *blob1 = NULL;
999 FILE *f2 = NULL;
1000 char *abspath = NULL;
1001 struct stat sb;
1003 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1004 status != GOT_STATUS_DELETE)
1005 return NULL;
1007 if (!a->header_shown) {
1008 printf("diff %s %s\n", a->id_str,
1009 got_worktree_get_root_path(a->worktree));
1010 a->header_shown = 1;
1013 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_DELETE) {
1014 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1015 if (err)
1016 goto done;
1020 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1021 if (asprintf(&abspath, "%s/%s",
1022 got_worktree_get_root_path(a->worktree), path) == -1) {
1023 err = got_error_from_errno();
1024 goto done;
1027 f2 = fopen(abspath, "r");
1028 if (f2 == NULL) {
1029 err = got_error_from_errno();
1030 goto done;
1032 if (lstat(abspath, &sb) == -1) {
1033 err = got_error_from_errno();
1034 goto done;
1036 } else
1037 sb.st_size = 0;
1039 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1040 stdout);
1041 done:
1042 if (blob1)
1043 got_object_blob_close(blob1);
1044 if (f2 && fclose(f2) != 0 && err == NULL)
1045 err = got_error_from_errno();
1046 free(abspath);
1047 return err;
1050 static const struct got_error *
1051 cmd_diff(int argc, char *argv[])
1053 const struct got_error *error;
1054 struct got_repository *repo = NULL;
1055 struct got_worktree *worktree = NULL;
1056 char *cwd = NULL, *repo_path = NULL;
1057 struct got_object_id *id1 = NULL, *id2 = NULL;
1058 char *id_str1 = NULL, *id_str2 = NULL;
1059 int type1, type2;
1060 int diff_context = 3, ch;
1061 const char *errstr;
1062 char *path = NULL;
1064 #ifndef PROFILE
1065 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1066 NULL) == -1)
1067 err(1, "pledge");
1068 #endif
1070 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1071 switch (ch) {
1072 case 'C':
1073 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1074 if (errstr != NULL)
1075 err(1, "-C option %s", errstr);
1076 break;
1077 case 'r':
1078 repo_path = realpath(optarg, NULL);
1079 if (repo_path == NULL)
1080 err(1, "-r option");
1081 break;
1082 default:
1083 usage_diff();
1084 /* NOTREACHED */
1088 argc -= optind;
1089 argv += optind;
1091 cwd = getcwd(NULL, 0);
1092 if (cwd == NULL) {
1093 error = got_error_from_errno();
1094 goto done;
1096 error = got_worktree_open(&worktree, cwd);
1097 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1098 goto done;
1099 if (argc <= 1) {
1100 if (worktree == NULL) {
1101 error = got_error(GOT_ERR_NOT_WORKTREE);
1102 goto done;
1104 if (repo_path)
1105 errx(1,
1106 "-r option can't be used when diffing a work tree");
1107 repo_path = strdup(got_worktree_get_repo_path(worktree));
1108 if (repo_path == NULL) {
1109 error = got_error_from_errno();
1110 goto done;
1112 if (argc == 1) {
1113 error = got_worktree_resolve_path(&path, worktree,
1114 argv[0]);
1115 if (error)
1116 goto done;
1117 } else {
1118 path = strdup("");
1119 if (path == NULL) {
1120 error = got_error_from_errno();
1121 goto done;
1124 } else if (argc == 2) {
1125 id_str1 = argv[0];
1126 id_str2 = argv[1];
1127 } else
1128 usage_diff();
1130 if (repo_path == NULL) {
1131 repo_path = getcwd(NULL, 0);
1132 if (repo_path == NULL)
1133 return got_error_from_errno();
1136 error = apply_unveil(repo_path, 1,
1137 worktree ? got_worktree_get_root_path(worktree) : NULL);
1138 if (error)
1139 goto done;
1141 error = got_repo_open(&repo, repo_path);
1142 free(repo_path);
1143 if (error != NULL)
1144 goto done;
1146 if (worktree) {
1147 struct print_diff_arg arg;
1148 char *id_str;
1149 error = got_object_id_str(&id_str,
1150 got_worktree_get_base_commit_id(worktree));
1151 if (error)
1152 goto done;
1153 arg.repo = repo;
1154 arg.worktree = worktree;
1155 arg.diff_context = diff_context;
1156 arg.id_str = id_str;
1157 arg.header_shown = 0;
1159 error = got_worktree_status(worktree, path, repo, print_diff,
1160 &arg, check_cancelled, NULL);
1161 free(id_str);
1162 goto done;
1165 error = got_object_resolve_id_str(&id1, repo, id_str1);
1166 if (error)
1167 goto done;
1169 error = got_object_resolve_id_str(&id2, repo, id_str2);
1170 if (error)
1171 goto done;
1173 error = got_object_get_type(&type1, repo, id1);
1174 if (error)
1175 goto done;
1177 error = got_object_get_type(&type2, repo, id2);
1178 if (error)
1179 goto done;
1181 if (type1 != type2) {
1182 error = got_error(GOT_ERR_OBJ_TYPE);
1183 goto done;
1186 switch (type1) {
1187 case GOT_OBJ_TYPE_BLOB:
1188 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1189 diff_context, repo, stdout);
1190 break;
1191 case GOT_OBJ_TYPE_TREE:
1192 error = got_diff_objects_as_trees(id1, id2, "", "",
1193 diff_context, repo, stdout);
1194 break;
1195 case GOT_OBJ_TYPE_COMMIT:
1196 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1197 id_str2);
1198 error = got_diff_objects_as_commits(id1, id2, diff_context,
1199 repo, stdout);
1200 break;
1201 default:
1202 error = got_error(GOT_ERR_OBJ_TYPE);
1205 done:
1206 free(id1);
1207 free(id2);
1208 free(path);
1209 if (worktree)
1210 got_worktree_close(worktree);
1211 if (repo) {
1212 const struct got_error *repo_error;
1213 repo_error = got_repo_close(repo);
1214 if (error == NULL)
1215 error = repo_error;
1217 return error;
1220 __dead static void
1221 usage_blame(void)
1223 fprintf(stderr,
1224 "usage: %s blame [-c commit] [-r repository-path] path\n",
1225 getprogname());
1226 exit(1);
1229 static const struct got_error *
1230 cmd_blame(int argc, char *argv[])
1232 const struct got_error *error;
1233 struct got_repository *repo = NULL;
1234 struct got_worktree *worktree = NULL;
1235 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1236 struct got_object_id *commit_id = NULL;
1237 char *commit_id_str = NULL;
1238 int ch;
1240 #ifndef PROFILE
1241 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1242 NULL) == -1)
1243 err(1, "pledge");
1244 #endif
1246 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1247 switch (ch) {
1248 case 'c':
1249 commit_id_str = optarg;
1250 break;
1251 case 'r':
1252 repo_path = realpath(optarg, NULL);
1253 if (repo_path == NULL)
1254 err(1, "-r option");
1255 break;
1256 default:
1257 usage_blame();
1258 /* NOTREACHED */
1262 argc -= optind;
1263 argv += optind;
1265 if (argc == 1)
1266 path = argv[0];
1267 else
1268 usage_blame();
1270 cwd = getcwd(NULL, 0);
1271 if (cwd == NULL) {
1272 error = got_error_from_errno();
1273 goto done;
1275 if (repo_path == NULL) {
1276 error = got_worktree_open(&worktree, cwd);
1277 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1278 goto done;
1279 else
1280 error = NULL;
1281 if (worktree) {
1282 repo_path =
1283 strdup(got_worktree_get_repo_path(worktree));
1284 if (repo_path == NULL)
1285 error = got_error_from_errno();
1286 if (error)
1287 goto done;
1288 } else {
1289 repo_path = strdup(cwd);
1290 if (repo_path == NULL) {
1291 error = got_error_from_errno();
1292 goto done;
1297 error = apply_unveil(repo_path, 1, NULL);
1298 if (error)
1299 goto done;
1301 error = got_repo_open(&repo, repo_path);
1302 if (error != NULL)
1303 goto done;
1305 if (worktree) {
1306 const char *prefix = got_worktree_get_path_prefix(worktree);
1307 char *p, *worktree_subdir = cwd +
1308 strlen(got_worktree_get_root_path(worktree));
1309 if (asprintf(&p, "%s%s%s%s%s",
1310 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1311 worktree_subdir, worktree_subdir[0] ? "/" : "",
1312 path) == -1) {
1313 error = got_error_from_errno();
1314 goto done;
1316 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1317 free(p);
1318 } else {
1319 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1321 if (error)
1322 goto done;
1324 if (commit_id_str == NULL) {
1325 struct got_reference *head_ref;
1326 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1327 if (error != NULL)
1328 goto done;
1329 error = got_ref_resolve(&commit_id, repo, head_ref);
1330 got_ref_close(head_ref);
1331 if (error != NULL)
1332 goto done;
1333 } else {
1334 error = got_object_resolve_id_str(&commit_id, repo,
1335 commit_id_str);
1336 if (error != NULL)
1337 goto done;
1340 error = got_blame(in_repo_path, commit_id, repo, stdout);
1341 done:
1342 free(in_repo_path);
1343 free(repo_path);
1344 free(cwd);
1345 free(commit_id);
1346 if (worktree)
1347 got_worktree_close(worktree);
1348 if (repo) {
1349 const struct got_error *repo_error;
1350 repo_error = got_repo_close(repo);
1351 if (error == NULL)
1352 error = repo_error;
1354 return error;
1357 __dead static void
1358 usage_tree(void)
1360 fprintf(stderr,
1361 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1362 getprogname());
1363 exit(1);
1366 static void
1367 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1368 const char *root_path)
1370 int is_root_path = (strcmp(path, root_path) == 0);
1372 path += strlen(root_path);
1373 while (path[0] == '/')
1374 path++;
1376 printf("%s%s%s%s%s\n", id ? id : "", path,
1377 is_root_path ? "" : "/", te->name,
1378 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1381 static const struct got_error *
1382 print_tree(const char *path, struct got_object_id *commit_id,
1383 int show_ids, int recurse, const char *root_path,
1384 struct got_repository *repo)
1386 const struct got_error *err = NULL;
1387 struct got_object_id *tree_id = NULL;
1388 struct got_tree_object *tree = NULL;
1389 const struct got_tree_entries *entries;
1390 struct got_tree_entry *te;
1392 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1393 if (err)
1394 goto done;
1396 err = got_object_open_as_tree(&tree, repo, tree_id);
1397 if (err)
1398 goto done;
1399 entries = got_object_tree_get_entries(tree);
1400 te = SIMPLEQ_FIRST(&entries->head);
1401 while (te) {
1402 char *id = NULL;
1404 if (sigint_received || sigpipe_received)
1405 break;
1407 if (show_ids) {
1408 char *id_str;
1409 err = got_object_id_str(&id_str, te->id);
1410 if (err)
1411 goto done;
1412 if (asprintf(&id, "%s ", id_str) == -1) {
1413 err = got_error_from_errno();
1414 free(id_str);
1415 goto done;
1417 free(id_str);
1419 print_entry(te, id, path, root_path);
1420 free(id);
1422 if (recurse && S_ISDIR(te->mode)) {
1423 char *child_path;
1424 if (asprintf(&child_path, "%s%s%s", path,
1425 path[0] == '/' && path[1] == '\0' ? "" : "/",
1426 te->name) == -1) {
1427 err = got_error_from_errno();
1428 goto done;
1430 err = print_tree(child_path, commit_id, show_ids, 1,
1431 root_path, repo);
1432 free(child_path);
1433 if (err)
1434 goto done;
1437 te = SIMPLEQ_NEXT(te, entry);
1439 done:
1440 if (tree)
1441 got_object_tree_close(tree);
1442 free(tree_id);
1443 return err;
1446 static const struct got_error *
1447 cmd_tree(int argc, char *argv[])
1449 const struct got_error *error;
1450 struct got_repository *repo = NULL;
1451 struct got_worktree *worktree = NULL;
1452 const char *path;
1453 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1454 struct got_object_id *commit_id = NULL;
1455 char *commit_id_str = NULL;
1456 int show_ids = 0, recurse = 0;
1457 int ch;
1459 #ifndef PROFILE
1460 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1461 NULL) == -1)
1462 err(1, "pledge");
1463 #endif
1465 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1466 switch (ch) {
1467 case 'c':
1468 commit_id_str = optarg;
1469 break;
1470 case 'r':
1471 repo_path = realpath(optarg, NULL);
1472 if (repo_path == NULL)
1473 err(1, "-r option");
1474 break;
1475 case 'i':
1476 show_ids = 1;
1477 break;
1478 case 'R':
1479 recurse = 1;
1480 break;
1481 default:
1482 usage_tree();
1483 /* NOTREACHED */
1487 argc -= optind;
1488 argv += optind;
1490 if (argc == 1)
1491 path = argv[0];
1492 else if (argc > 1)
1493 usage_tree();
1494 else
1495 path = NULL;
1497 cwd = getcwd(NULL, 0);
1498 if (cwd == NULL) {
1499 error = got_error_from_errno();
1500 goto done;
1502 if (repo_path == NULL) {
1503 error = got_worktree_open(&worktree, cwd);
1504 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1505 goto done;
1506 else
1507 error = NULL;
1508 if (worktree) {
1509 repo_path =
1510 strdup(got_worktree_get_repo_path(worktree));
1511 if (repo_path == NULL)
1512 error = got_error_from_errno();
1513 if (error)
1514 goto done;
1515 } else {
1516 repo_path = strdup(cwd);
1517 if (repo_path == NULL) {
1518 error = got_error_from_errno();
1519 goto done;
1524 error = apply_unveil(repo_path, 1, NULL);
1525 if (error)
1526 goto done;
1528 error = got_repo_open(&repo, repo_path);
1529 if (error != NULL)
1530 goto done;
1532 if (path == NULL) {
1533 if (worktree) {
1534 char *p, *worktree_subdir = cwd +
1535 strlen(got_worktree_get_root_path(worktree));
1536 if (asprintf(&p, "%s/%s",
1537 got_worktree_get_path_prefix(worktree),
1538 worktree_subdir) == -1) {
1539 error = got_error_from_errno();
1540 goto done;
1542 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1543 free(p);
1544 if (error)
1545 goto done;
1546 } else
1547 path = "/";
1549 if (in_repo_path == NULL) {
1550 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1551 if (error != NULL)
1552 goto done;
1555 if (commit_id_str == NULL) {
1556 struct got_reference *head_ref;
1557 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1558 if (error != NULL)
1559 goto done;
1560 error = got_ref_resolve(&commit_id, repo, head_ref);
1561 got_ref_close(head_ref);
1562 if (error != NULL)
1563 goto done;
1564 } else {
1565 error = got_object_resolve_id_str(&commit_id, repo,
1566 commit_id_str);
1567 if (error != NULL)
1568 goto done;
1571 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1572 in_repo_path, repo);
1573 done:
1574 free(in_repo_path);
1575 free(repo_path);
1576 free(cwd);
1577 free(commit_id);
1578 if (worktree)
1579 got_worktree_close(worktree);
1580 if (repo) {
1581 const struct got_error *repo_error;
1582 repo_error = got_repo_close(repo);
1583 if (error == NULL)
1584 error = repo_error;
1586 return error;
1589 __dead static void
1590 usage_status(void)
1592 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1593 exit(1);
1596 static const struct got_error *
1597 print_status(void *arg, unsigned char status, const char *path,
1598 struct got_object_id *id)
1600 printf("%c %s\n", status, path);
1601 return NULL;
1604 static const struct got_error *
1605 cmd_status(int argc, char *argv[])
1607 const struct got_error *error = NULL;
1608 struct got_repository *repo = NULL;
1609 struct got_worktree *worktree = NULL;
1610 char *cwd = NULL, *path = NULL;
1611 int ch;
1613 while ((ch = getopt(argc, argv, "")) != -1) {
1614 switch (ch) {
1615 default:
1616 usage_status();
1617 /* NOTREACHED */
1621 argc -= optind;
1622 argv += optind;
1624 #ifndef PROFILE
1625 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1626 NULL) == -1)
1627 err(1, "pledge");
1628 #endif
1629 cwd = getcwd(NULL, 0);
1630 if (cwd == NULL) {
1631 error = got_error_from_errno();
1632 goto done;
1635 error = got_worktree_open(&worktree, cwd);
1636 if (error != NULL)
1637 goto done;
1639 if (argc == 0) {
1640 path = strdup("");
1641 if (path == NULL) {
1642 error = got_error_from_errno();
1643 goto done;
1645 } else if (argc == 1) {
1646 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1647 if (error)
1648 goto done;
1649 } else
1650 usage_status();
1652 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1653 if (error != NULL)
1654 goto done;
1656 error = apply_unveil(got_repo_get_path(repo), 1,
1657 got_worktree_get_root_path(worktree));
1658 if (error)
1659 goto done;
1661 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1662 check_cancelled, NULL);
1663 done:
1664 free(cwd);
1665 free(path);
1666 return error;
1669 __dead static void
1670 usage_ref(void)
1672 fprintf(stderr,
1673 "usage: %s ref [-r repository] -l | -d name | name object\n",
1674 getprogname());
1675 exit(1);
1678 static const struct got_error *
1679 list_refs(struct got_repository *repo)
1681 static const struct got_error *err = NULL;
1682 struct got_reflist_head refs;
1683 struct got_reflist_entry *re;
1685 SIMPLEQ_INIT(&refs);
1686 err = got_ref_list(&refs, repo);
1687 if (err)
1688 return err;
1690 SIMPLEQ_FOREACH(re, &refs, entry) {
1691 char *refstr;
1692 refstr = got_ref_to_str(re->ref);
1693 if (refstr == NULL)
1694 return got_error_from_errno();
1695 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1696 free(refstr);
1699 got_ref_list_free(&refs);
1700 return NULL;
1703 static const struct got_error *
1704 delete_ref(struct got_repository *repo, const char *refname)
1706 const struct got_error *err = NULL;
1707 struct got_reference *ref;
1709 err = got_ref_open(&ref, repo, refname);
1710 if (err)
1711 return err;
1713 err = got_ref_delete(ref, repo);
1714 got_ref_close(ref);
1715 return err;
1718 static const struct got_error *
1719 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1721 const struct got_error *err = NULL;
1722 struct got_object_id *id;
1723 struct got_reference *ref = NULL;
1725 err = got_object_resolve_id_str(&id, repo, id_str);
1726 if (err)
1727 return err;
1729 err = got_ref_alloc(&ref, refname, id);
1730 if (err)
1731 goto done;
1733 err = got_ref_write(ref, repo);
1734 done:
1735 if (ref)
1736 got_ref_close(ref);
1737 free(id);
1738 return err;
1741 static const struct got_error *
1742 cmd_ref(int argc, char *argv[])
1744 const struct got_error *error = NULL;
1745 struct got_repository *repo = NULL;
1746 struct got_worktree *worktree = NULL;
1747 char *cwd = NULL, *repo_path = NULL;
1748 int ch, do_list = 0;
1749 const char *delref = NULL;
1751 /* TODO: Add -s option for adding symbolic references. */
1752 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1753 switch (ch) {
1754 case 'd':
1755 delref = optarg;
1756 break;
1757 case 'r':
1758 repo_path = realpath(optarg, NULL);
1759 if (repo_path == NULL)
1760 err(1, "-r option");
1761 break;
1762 case 'l':
1763 do_list = 1;
1764 break;
1765 default:
1766 usage_ref();
1767 /* NOTREACHED */
1771 if (do_list && delref)
1772 errx(1, "-l and -d options are mutually exclusive\n");
1774 argc -= optind;
1775 argv += optind;
1777 if (do_list || delref) {
1778 if (argc > 0)
1779 usage_ref();
1780 } else if (argc != 2)
1781 usage_ref();
1783 #ifndef PROFILE
1784 if (do_list) {
1785 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1786 NULL) == -1)
1787 err(1, "pledge");
1788 } else {
1789 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1790 "sendfd unveil", NULL) == -1)
1791 err(1, "pledge");
1793 #endif
1794 cwd = getcwd(NULL, 0);
1795 if (cwd == NULL) {
1796 error = got_error_from_errno();
1797 goto done;
1800 if (repo_path == NULL) {
1801 error = got_worktree_open(&worktree, cwd);
1802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1803 goto done;
1804 else
1805 error = NULL;
1806 if (worktree) {
1807 repo_path =
1808 strdup(got_worktree_get_repo_path(worktree));
1809 if (repo_path == NULL)
1810 error = got_error_from_errno();
1811 if (error)
1812 goto done;
1813 } else {
1814 repo_path = strdup(cwd);
1815 if (repo_path == NULL) {
1816 error = got_error_from_errno();
1817 goto done;
1822 error = apply_unveil(repo_path, do_list,
1823 worktree ? got_worktree_get_root_path(worktree) : NULL);
1824 if (error)
1825 goto done;
1827 error = got_repo_open(&repo, repo_path);
1828 if (error != NULL)
1829 goto done;
1831 if (do_list)
1832 error = list_refs(repo);
1833 else if (delref)
1834 error = delete_ref(repo, delref);
1835 else
1836 error = add_ref(repo, argv[0], argv[1]);
1837 done:
1838 if (repo)
1839 got_repo_close(repo);
1840 if (worktree)
1841 got_worktree_close(worktree);
1842 free(cwd);
1843 free(repo_path);
1844 return error;
1847 __dead static void
1848 usage_add(void)
1850 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1851 exit(1);
1854 static const struct got_error *
1855 cmd_add(int argc, char *argv[])
1857 const struct got_error *error = NULL;
1858 struct got_worktree *worktree = NULL;
1859 char *cwd = NULL, *path = NULL, *relpath = NULL;
1860 int ch;
1862 while ((ch = getopt(argc, argv, "")) != -1) {
1863 switch (ch) {
1864 default:
1865 usage_add();
1866 /* NOTREACHED */
1870 argc -= optind;
1871 argv += optind;
1873 if (argc != 1)
1874 usage_add();
1876 path = realpath(argv[0], NULL);
1877 if (path == NULL) {
1878 error = got_error_from_errno();
1879 goto done;
1882 cwd = getcwd(NULL, 0);
1883 if (cwd == NULL) {
1884 error = got_error_from_errno();
1885 goto done;
1887 error = got_worktree_open(&worktree, cwd);
1888 if (error)
1889 goto done;
1891 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
1892 if (error)
1893 goto done;
1895 error = got_worktree_schedule_add(&relpath, worktree, path);
1896 if (error)
1897 goto done;
1898 printf("%c %s\n", GOT_STATUS_ADD, relpath);
1899 done:
1900 if (worktree)
1901 got_worktree_close(worktree);
1902 free(path);
1903 free(relpath);
1904 free(cwd);
1905 return error;
1908 __dead static void
1909 usage_rm(void)
1911 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1912 exit(1);
1915 static const struct got_error *
1916 cmd_rm(int argc, char *argv[])
1918 const struct got_error *error = NULL;
1919 struct got_worktree *worktree = NULL;
1920 struct got_repository *repo = NULL;
1921 char *cwd = NULL, *path = NULL;
1922 int ch, delete_local_mods = 0;
1924 while ((ch = getopt(argc, argv, "f")) != -1) {
1925 switch (ch) {
1926 case 'f':
1927 delete_local_mods = 1;
1928 break;
1929 default:
1930 usage_add();
1931 /* NOTREACHED */
1935 argc -= optind;
1936 argv += optind;
1938 if (argc != 1)
1939 usage_rm();
1941 path = realpath(argv[0], NULL);
1942 if (path == NULL) {
1943 error = got_error_from_errno();
1944 goto done;
1947 cwd = getcwd(NULL, 0);
1948 if (cwd == NULL) {
1949 error = got_error_from_errno();
1950 goto done;
1952 error = got_worktree_open(&worktree, cwd);
1953 if (error)
1954 goto done;
1956 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1957 if (error != NULL)
1958 goto done;
1960 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
1961 if (error)
1962 goto done;
1964 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1965 print_status, NULL, repo);
1966 if (error)
1967 goto done;
1968 done:
1969 if (repo)
1970 got_repo_close(repo);
1971 if (worktree)
1972 got_worktree_close(worktree);
1973 free(path);
1974 free(cwd);
1975 return error;