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);
82 __dead static void usage_revert(void);
84 static const struct got_error* cmd_checkout(int, char *[]);
85 static const struct got_error* cmd_update(int, char *[]);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_status(int, char *[]);
91 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct got_error* cmd_add(int, char *[]);
93 static const struct got_error* cmd_rm(int, char *[]);
94 static const struct got_error* cmd_revert(int, char *[]);
96 static struct cmd got_commands[] = {
97 { "checkout", cmd_checkout, usage_checkout,
98 "check out a new work tree from a repository" },
99 { "update", cmd_update, usage_update,
100 "update a work tree to a different commit" },
101 { "log", cmd_log, usage_log,
102 "show repository history" },
103 { "diff", cmd_diff, usage_diff,
104 "compare files and directories" },
105 { "blame", cmd_blame, usage_blame,
106 "show when lines in a file were changed" },
107 { "tree", cmd_tree, usage_tree,
108 "list files and directories in repository" },
109 { "status", cmd_status, usage_status,
110 "show modification status of files" },
111 { "ref", cmd_ref, usage_ref,
112 "manage references in repository" },
113 { "add", cmd_add, usage_add,
114 "add a new file to version control" },
115 { "rm", cmd_rm, usage_rm,
116 "remove a versioned file" },
117 { "revert", cmd_revert, usage_revert,
118 "revert uncommitted changes" },
119 };
121 int
122 main(int argc, char *argv[])
124 struct cmd *cmd;
125 unsigned int i;
126 int ch;
127 int hflag = 0;
129 setlocale(LC_CTYPE, "");
131 while ((ch = getopt(argc, argv, "h")) != -1) {
132 switch (ch) {
133 case 'h':
134 hflag = 1;
135 break;
136 default:
137 usage();
138 /* NOTREACHED */
142 argc -= optind;
143 argv += optind;
144 optind = 0;
146 if (argc <= 0)
147 usage();
149 signal(SIGINT, catch_sigint);
150 signal(SIGPIPE, catch_sigpipe);
152 for (i = 0; i < nitems(got_commands); i++) {
153 const struct got_error *error;
155 cmd = &got_commands[i];
157 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
158 continue;
160 if (hflag)
161 got_commands[i].cmd_usage();
163 error = got_commands[i].cmd_main(argc, argv);
164 if (error && !(sigint_received || sigpipe_received)) {
165 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
166 return 1;
169 return 0;
172 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
173 return 1;
176 __dead static void
177 usage(void)
179 int i;
181 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
182 "Available commands:\n", getprogname());
183 for (i = 0; i < nitems(got_commands); i++) {
184 struct cmd *cmd = &got_commands[i];
185 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
187 exit(1);
190 static const struct got_error *
191 apply_unveil(const char *repo_path, int repo_read_only,
192 const char *worktree_path)
194 const struct got_error *error;
196 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
197 return got_error_from_errno();
199 if (worktree_path && unveil(worktree_path, "rwc") != 0)
200 return got_error_from_errno();
202 if (unveil("/tmp", "rwc") != 0)
203 return got_error_from_errno();
205 error = got_privsep_unveil_exec_helpers();
206 if (error != NULL)
207 return error;
209 if (unveil(NULL, NULL) != 0)
210 return got_error_from_errno();
212 return NULL;
215 __dead static void
216 usage_checkout(void)
218 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
219 "[worktree-path]\n", getprogname());
220 exit(1);
223 static void
224 checkout_progress(void *arg, unsigned char status, const char *path)
226 char *worktree_path = arg;
228 while (path[0] == '/')
229 path++;
231 printf("%c %s/%s\n", status, worktree_path, path);
234 static const struct got_error *
235 check_cancelled(void *arg)
237 if (sigint_received || sigpipe_received)
238 return got_error(GOT_ERR_CANCELLED);
239 return NULL;
242 static const struct got_error *
243 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
244 struct got_repository *repo)
246 const struct got_error *err;
247 struct got_reference *head_ref = NULL;
248 struct got_object_id *head_commit_id = NULL;
249 struct got_commit_graph *graph = NULL;
251 head_ref = got_worktree_get_head_ref(worktree);
252 if (head_ref == NULL)
253 return got_error_from_errno();
255 /* TODO: Check the reflog. The head ref may have been rebased. */
256 err = got_ref_resolve(&head_commit_id, repo, head_ref);
257 if (err)
258 goto done;
260 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
261 if (err)
262 goto done;
264 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
265 if (err)
266 goto done;
267 while (1) {
268 struct got_object_id *id;
270 if (sigint_received || sigpipe_received)
271 break;
273 err = got_commit_graph_iter_next(&id, graph);
274 if (err) {
275 if (err->code == GOT_ERR_ITER_COMPLETED) {
276 err = got_error(GOT_ERR_ANCESTRY);
277 break;
279 if (err->code != GOT_ERR_ITER_NEED_MORE)
280 break;
281 err = got_commit_graph_fetch_commits(graph, 1, repo);
282 if (err)
283 break;
284 else
285 continue;
287 if (id == NULL)
288 break;
289 if (got_object_id_cmp(id, commit_id) == 0)
290 break;
292 done:
293 if (head_ref)
294 got_ref_close(head_ref);
295 if (graph)
296 got_commit_graph_close(graph);
297 return err;
301 static const struct got_error *
302 cmd_checkout(int argc, char *argv[])
304 const struct got_error *error = NULL;
305 struct got_repository *repo = NULL;
306 struct got_reference *head_ref = NULL;
307 struct got_worktree *worktree = NULL;
308 char *repo_path = NULL;
309 char *worktree_path = NULL;
310 const char *path_prefix = "";
311 char *commit_id_str = NULL;
312 int ch, same_path_prefix;
314 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
315 switch (ch) {
316 case 'c':
317 commit_id_str = strdup(optarg);
318 if (commit_id_str == NULL)
319 return got_error_from_errno();
320 break;
321 case 'p':
322 path_prefix = optarg;
323 break;
324 default:
325 usage_checkout();
326 /* NOTREACHED */
330 argc -= optind;
331 argv += optind;
333 #ifndef PROFILE
334 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
335 "unveil", NULL) == -1)
336 err(1, "pledge");
337 #endif
338 if (argc == 1) {
339 char *cwd, *base, *dotgit;
340 repo_path = realpath(argv[0], NULL);
341 if (repo_path == NULL)
342 return got_error_from_errno();
343 cwd = getcwd(NULL, 0);
344 if (cwd == NULL) {
345 error = got_error_from_errno();
346 goto done;
348 if (path_prefix[0])
349 base = basename(path_prefix);
350 else
351 base = basename(repo_path);
352 if (base == NULL) {
353 error = got_error_from_errno();
354 goto done;
356 dotgit = strstr(base, ".git");
357 if (dotgit)
358 *dotgit = '\0';
359 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
360 error = got_error_from_errno();
361 free(cwd);
362 goto done;
364 free(cwd);
365 } else if (argc == 2) {
366 repo_path = realpath(argv[0], NULL);
367 if (repo_path == NULL) {
368 error = got_error_from_errno();
369 goto done;
371 worktree_path = realpath(argv[1], NULL);
372 if (worktree_path == NULL) {
373 error = got_error_from_errno();
374 goto done;
376 } else
377 usage_checkout();
379 error = apply_unveil(repo_path, 0, worktree_path);
380 if (error)
381 goto done;
383 error = got_repo_open(&repo, repo_path);
384 if (error != NULL)
385 goto done;
387 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
388 if (error != NULL)
389 goto done;
391 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
392 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
393 goto done;
395 error = got_worktree_open(&worktree, worktree_path);
396 if (error != NULL)
397 goto done;
399 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
400 path_prefix);
401 if (error != NULL)
402 goto done;
403 if (!same_path_prefix) {
404 error = got_error(GOT_ERR_PATH_PREFIX);
405 goto done;
408 if (commit_id_str) {
409 struct got_object_id *commit_id;
410 error = got_object_resolve_id_str(&commit_id, repo,
411 commit_id_str);
412 if (error != NULL)
413 goto done;
414 error = check_ancestry(worktree, commit_id, repo);
415 if (error != NULL) {
416 free(commit_id);
417 goto done;
419 error = got_worktree_set_base_commit_id(worktree, repo,
420 commit_id);
421 free(commit_id);
422 if (error)
423 goto done;
426 error = got_worktree_checkout_files(worktree, repo,
427 checkout_progress, worktree_path, check_cancelled, NULL);
428 if (error != NULL)
429 goto done;
431 printf("Now shut up and hack\n");
433 done:
434 free(commit_id_str);
435 free(repo_path);
436 free(worktree_path);
437 return error;
440 __dead static void
441 usage_update(void)
443 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
444 getprogname());
445 exit(1);
448 static void
449 update_progress(void *arg, unsigned char status, const char *path)
451 int *did_something = arg;
453 if (status == GOT_STATUS_EXISTS)
454 return;
456 *did_something = 1;
457 while (path[0] == '/')
458 path++;
459 printf("%c %s\n", status, path);
462 static const struct got_error *
463 cmd_update(int argc, char *argv[])
465 const struct got_error *error = NULL;
466 struct got_repository *repo = NULL;
467 struct got_worktree *worktree = NULL;
468 char *worktree_path = NULL;
469 struct got_object_id *commit_id = NULL;
470 char *commit_id_str = NULL;
471 int ch, did_something = 0;
473 while ((ch = getopt(argc, argv, "c:")) != -1) {
474 switch (ch) {
475 case 'c':
476 commit_id_str = strdup(optarg);
477 if (commit_id_str == NULL)
478 return got_error_from_errno();
479 break;
480 default:
481 usage_update();
482 /* NOTREACHED */
486 argc -= optind;
487 argv += optind;
489 #ifndef PROFILE
490 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
491 "unveil", NULL) == -1)
492 err(1, "pledge");
493 #endif
494 if (argc == 0) {
495 worktree_path = getcwd(NULL, 0);
496 if (worktree_path == NULL) {
497 error = got_error_from_errno();
498 goto done;
500 } else if (argc == 1) {
501 worktree_path = realpath(argv[0], NULL);
502 if (worktree_path == NULL) {
503 error = got_error_from_errno();
504 goto done;
506 } else
507 usage_update();
509 error = got_worktree_open(&worktree, worktree_path);
510 if (error != NULL)
511 goto done;
513 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
514 if (error != NULL)
515 goto done;
517 error = apply_unveil(got_repo_get_path(repo), 0,
518 got_worktree_get_root_path(worktree));
519 if (error)
520 goto done;
522 if (commit_id_str == NULL) {
523 struct got_reference *head_ref;
524 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
525 if (error != NULL)
526 goto done;
527 error = got_ref_resolve(&commit_id, repo, head_ref);
528 if (error != NULL)
529 goto done;
530 error = got_object_id_str(&commit_id_str, commit_id);
531 if (error != NULL)
532 goto done;
533 } else {
534 error = got_object_resolve_id_str(&commit_id, repo,
535 commit_id_str);
536 if (error != NULL)
537 goto done;
540 error = check_ancestry(worktree, commit_id, repo);
541 if (error != NULL)
542 goto done;
544 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
545 commit_id) != 0) {
546 error = got_worktree_set_base_commit_id(worktree, repo,
547 commit_id);
548 if (error)
549 goto done;
552 error = got_worktree_checkout_files(worktree, repo,
553 update_progress, &did_something, check_cancelled, NULL);
554 if (error != NULL)
555 goto done;
557 if (did_something)
558 printf("Updated to commit %s\n", commit_id_str);
559 else
560 printf("Already up-to-date\n");
561 done:
562 free(worktree_path);
563 free(commit_id);
564 free(commit_id_str);
565 return error;
568 static const struct got_error *
569 print_patch(struct got_commit_object *commit, struct got_object_id *id,
570 int diff_context, struct got_repository *repo)
572 const struct got_error *err = NULL;
573 struct got_tree_object *tree1 = NULL, *tree2;
574 struct got_object_qid *qid;
575 char *id_str1 = NULL, *id_str2;
577 err = got_object_open_as_tree(&tree2, repo,
578 got_object_commit_get_tree_id(commit));
579 if (err)
580 return err;
582 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
583 if (qid != NULL) {
584 struct got_commit_object *pcommit;
586 err = got_object_open_as_commit(&pcommit, repo, qid->id);
587 if (err)
588 return err;
590 err = got_object_open_as_tree(&tree1, repo,
591 got_object_commit_get_tree_id(pcommit));
592 got_object_commit_close(pcommit);
593 if (err)
594 return err;
596 err = got_object_id_str(&id_str1, qid->id);
597 if (err)
598 return err;
601 err = got_object_id_str(&id_str2, id);
602 if (err)
603 goto done;
605 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
606 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
607 done:
608 if (tree1)
609 got_object_tree_close(tree1);
610 got_object_tree_close(tree2);
611 free(id_str1);
612 free(id_str2);
613 return err;
616 static char *
617 get_datestr(time_t *time, char *datebuf)
619 char *p, *s = ctime_r(time, datebuf);
620 p = strchr(s, '\n');
621 if (p)
622 *p = '\0';
623 return s;
626 static const struct got_error *
627 print_commit(struct got_commit_object *commit, struct got_object_id *id,
628 struct got_repository *repo, int show_patch, int diff_context,
629 struct got_reflist_head *refs)
631 const struct got_error *err = NULL;
632 char *id_str, *datestr, *logmsg0, *logmsg, *line;
633 char datebuf[26];
634 time_t committer_time;
635 const char *author, *committer;
636 char *refs_str = NULL;
637 struct got_reflist_entry *re;
639 SIMPLEQ_FOREACH(re, refs, entry) {
640 char *s;
641 const char *name;
642 if (got_object_id_cmp(re->id, id) != 0)
643 continue;
644 name = got_ref_get_name(re->ref);
645 if (strcmp(name, GOT_REF_HEAD) == 0)
646 continue;
647 if (strncmp(name, "refs/", 5) == 0)
648 name += 5;
649 if (strncmp(name, "got/", 4) == 0)
650 continue;
651 if (strncmp(name, "heads/", 6) == 0)
652 name += 6;
653 if (strncmp(name, "remotes/", 8) == 0)
654 name += 8;
655 s = refs_str;
656 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
657 name) == -1) {
658 err = got_error_from_errno();
659 free(s);
660 break;
662 free(s);
664 err = got_object_id_str(&id_str, id);
665 if (err)
666 return err;
668 printf("-----------------------------------------------\n");
669 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
670 refs_str ? refs_str : "", refs_str ? ")" : "");
671 free(id_str);
672 id_str = NULL;
673 free(refs_str);
674 refs_str = NULL;
675 printf("from: %s\n", got_object_commit_get_author(commit));
676 committer_time = got_object_commit_get_committer_time(commit);
677 datestr = get_datestr(&committer_time, datebuf);
678 printf("date: %s UTC\n", datestr);
679 author = got_object_commit_get_author(commit);
680 committer = got_object_commit_get_committer(commit);
681 if (strcmp(author, committer) != 0)
682 printf("via: %s\n", committer);
683 if (got_object_commit_get_nparents(commit) > 1) {
684 const struct got_object_id_queue *parent_ids;
685 struct got_object_qid *qid;
686 int n = 1;
687 parent_ids = got_object_commit_get_parent_ids(commit);
688 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
689 err = got_object_id_str(&id_str, qid->id);
690 if (err)
691 return err;
692 printf("parent %d: %s\n", n++, id_str);
693 free(id_str);
697 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
698 if (logmsg0 == NULL)
699 return got_error_from_errno();
701 logmsg = logmsg0;
702 do {
703 line = strsep(&logmsg, "\n");
704 if (line)
705 printf(" %s\n", line);
706 } while (line);
707 free(logmsg0);
709 if (show_patch) {
710 err = print_patch(commit, id, diff_context, repo);
711 if (err == 0)
712 printf("\n");
715 if (fflush(stdout) != 0 && err == NULL)
716 err = got_error_from_errno();
717 return err;
720 static const struct got_error *
721 print_commits(struct got_object_id *root_id, struct got_repository *repo,
722 char *path, int show_patch, int diff_context, int limit,
723 int first_parent_traversal, struct got_reflist_head *refs)
725 const struct got_error *err;
726 struct got_commit_graph *graph;
728 err = got_commit_graph_open(&graph, root_id, path,
729 first_parent_traversal, repo);
730 if (err)
731 return err;
732 err = got_commit_graph_iter_start(graph, root_id, repo);
733 if (err)
734 goto done;
735 while (1) {
736 struct got_commit_object *commit;
737 struct got_object_id *id;
739 if (sigint_received || sigpipe_received)
740 break;
742 err = got_commit_graph_iter_next(&id, graph);
743 if (err) {
744 if (err->code == GOT_ERR_ITER_COMPLETED) {
745 err = NULL;
746 break;
748 if (err->code != GOT_ERR_ITER_NEED_MORE)
749 break;
750 err = got_commit_graph_fetch_commits(graph, 1, repo);
751 if (err)
752 break;
753 else
754 continue;
756 if (id == NULL)
757 break;
759 err = got_object_open_as_commit(&commit, repo, id);
760 if (err)
761 break;
762 err = print_commit(commit, id, repo, show_patch, diff_context,
763 refs);
764 got_object_commit_close(commit);
765 if (err || (limit && --limit == 0))
766 break;
768 done:
769 got_commit_graph_close(graph);
770 return err;
773 __dead static void
774 usage_log(void)
776 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
777 "[-r repository-path] [path]\n", getprogname());
778 exit(1);
781 static const struct got_error *
782 cmd_log(int argc, char *argv[])
784 const struct got_error *error;
785 struct got_repository *repo = NULL;
786 struct got_worktree *worktree = NULL;
787 struct got_commit_object *commit = NULL;
788 struct got_object_id *id = NULL;
789 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
790 char *start_commit = NULL;
791 int diff_context = 3, ch;
792 int show_patch = 0, limit = 0, first_parent_traversal = 0;
793 const char *errstr;
794 struct got_reflist_head refs;
796 SIMPLEQ_INIT(&refs);
798 #ifndef PROFILE
799 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
800 NULL)
801 == -1)
802 err(1, "pledge");
803 #endif
805 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
806 switch (ch) {
807 case 'p':
808 show_patch = 1;
809 break;
810 case 'c':
811 start_commit = optarg;
812 break;
813 case 'C':
814 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
815 &errstr);
816 if (errstr != NULL)
817 err(1, "-C option %s", errstr);
818 break;
819 case 'l':
820 limit = strtonum(optarg, 1, INT_MAX, &errstr);
821 if (errstr != NULL)
822 err(1, "-l option %s", errstr);
823 break;
824 case 'f':
825 first_parent_traversal = 1;
826 break;
827 case 'r':
828 repo_path = realpath(optarg, NULL);
829 if (repo_path == NULL)
830 err(1, "-r option");
831 break;
832 default:
833 usage_log();
834 /* NOTREACHED */
838 argc -= optind;
839 argv += optind;
841 cwd = getcwd(NULL, 0);
842 if (cwd == NULL) {
843 error = got_error_from_errno();
844 goto done;
847 error = got_worktree_open(&worktree, cwd);
848 if (error && error->code != GOT_ERR_NOT_WORKTREE)
849 goto done;
850 error = NULL;
852 if (argc == 0) {
853 path = strdup("");
854 if (path == NULL) {
855 error = got_error_from_errno();
856 goto done;
858 } else if (argc == 1) {
859 if (worktree) {
860 error = got_worktree_resolve_path(&path, worktree,
861 argv[0]);
862 if (error)
863 goto done;
864 } else {
865 path = strdup(argv[0]);
866 if (path == NULL) {
867 error = got_error_from_errno();
868 goto done;
871 } else
872 usage_log();
874 repo_path = worktree ?
875 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
876 if (repo_path == NULL) {
877 error = got_error_from_errno();
878 goto done;
881 error = apply_unveil(repo_path, 1,
882 worktree ? got_worktree_get_root_path(worktree) : NULL);
883 if (error)
884 goto done;
886 error = got_repo_open(&repo, repo_path);
887 if (error != NULL)
888 goto done;
890 if (start_commit == NULL) {
891 struct got_reference *head_ref;
892 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
893 if (error != NULL)
894 return error;
895 error = got_ref_resolve(&id, repo, head_ref);
896 got_ref_close(head_ref);
897 if (error != NULL)
898 return error;
899 error = got_object_open_as_commit(&commit, repo, id);
900 } else {
901 struct got_reference *ref;
902 error = got_ref_open(&ref, repo, start_commit);
903 if (error == NULL) {
904 int obj_type;
905 error = got_ref_resolve(&id, repo, ref);
906 got_ref_close(ref);
907 if (error != NULL)
908 goto done;
909 error = got_object_get_type(&obj_type, repo, id);
910 if (error != NULL)
911 goto done;
912 if (obj_type == GOT_OBJ_TYPE_TAG) {
913 struct got_tag_object *tag;
914 error = got_object_open_as_tag(&tag, repo, id);
915 if (error != NULL)
916 goto done;
917 if (got_object_tag_get_object_type(tag) !=
918 GOT_OBJ_TYPE_COMMIT) {
919 got_object_tag_close(tag);
920 error = got_error(GOT_ERR_OBJ_TYPE);
921 goto done;
923 free(id);
924 id = got_object_id_dup(
925 got_object_tag_get_object_id(tag));
926 if (id == NULL)
927 error = got_error_from_errno();
928 got_object_tag_close(tag);
929 if (error)
930 goto done;
931 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
932 error = got_error(GOT_ERR_OBJ_TYPE);
933 goto done;
935 error = got_object_open_as_commit(&commit, repo, id);
936 if (error != NULL)
937 goto done;
939 if (commit == NULL) {
940 error = got_object_resolve_id_str(&id, repo,
941 start_commit);
942 if (error != NULL)
943 return error;
946 if (error != NULL)
947 goto done;
949 error = got_repo_map_path(&in_repo_path, repo, path, 1);
950 if (error != NULL)
951 goto done;
952 if (in_repo_path) {
953 free(path);
954 path = in_repo_path;
957 error = got_ref_list(&refs, repo);
958 if (error)
959 goto done;
961 error = print_commits(id, repo, path, show_patch,
962 diff_context, limit, first_parent_traversal, &refs);
963 done:
964 free(path);
965 free(repo_path);
966 free(cwd);
967 free(id);
968 if (worktree)
969 got_worktree_close(worktree);
970 if (repo) {
971 const struct got_error *repo_error;
972 repo_error = got_repo_close(repo);
973 if (error == NULL)
974 error = repo_error;
976 got_ref_list_free(&refs);
977 return error;
980 __dead static void
981 usage_diff(void)
983 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
984 "[object1 object2 | path]\n", getprogname());
985 exit(1);
988 struct print_diff_arg {
989 struct got_repository *repo;
990 struct got_worktree *worktree;
991 int diff_context;
992 const char *id_str;
993 int header_shown;
994 };
996 static const struct got_error *
997 print_diff(void *arg, unsigned char status, const char *path,
998 struct got_object_id *id)
1000 struct print_diff_arg *a = arg;
1001 const struct got_error *err = NULL;
1002 struct got_blob_object *blob1 = NULL;
1003 FILE *f2 = NULL;
1004 char *abspath = NULL;
1005 struct stat sb;
1007 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1008 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1009 return NULL;
1011 if (!a->header_shown) {
1012 printf("diff %s %s\n", a->id_str,
1013 got_worktree_get_root_path(a->worktree));
1014 a->header_shown = 1;
1017 if (status != GOT_STATUS_ADD) {
1018 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1019 if (err)
1020 goto done;
1024 if (status != GOT_STATUS_DELETE) {
1025 if (asprintf(&abspath, "%s/%s",
1026 got_worktree_get_root_path(a->worktree), path) == -1) {
1027 err = got_error_from_errno();
1028 goto done;
1031 f2 = fopen(abspath, "r");
1032 if (f2 == NULL) {
1033 err = got_error_from_errno();
1034 goto done;
1036 if (lstat(abspath, &sb) == -1) {
1037 err = got_error_from_errno();
1038 goto done;
1040 } else
1041 sb.st_size = 0;
1043 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1044 stdout);
1045 done:
1046 if (blob1)
1047 got_object_blob_close(blob1);
1048 if (f2 && fclose(f2) != 0 && err == NULL)
1049 err = got_error_from_errno();
1050 free(abspath);
1051 return err;
1054 static const struct got_error *
1055 cmd_diff(int argc, char *argv[])
1057 const struct got_error *error;
1058 struct got_repository *repo = NULL;
1059 struct got_worktree *worktree = NULL;
1060 char *cwd = NULL, *repo_path = NULL;
1061 struct got_object_id *id1 = NULL, *id2 = NULL;
1062 char *id_str1 = NULL, *id_str2 = NULL;
1063 int type1, type2;
1064 int diff_context = 3, ch;
1065 const char *errstr;
1066 char *path = NULL;
1068 #ifndef PROFILE
1069 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1070 NULL) == -1)
1071 err(1, "pledge");
1072 #endif
1074 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1075 switch (ch) {
1076 case 'C':
1077 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1078 if (errstr != NULL)
1079 err(1, "-C option %s", errstr);
1080 break;
1081 case 'r':
1082 repo_path = realpath(optarg, NULL);
1083 if (repo_path == NULL)
1084 err(1, "-r option");
1085 break;
1086 default:
1087 usage_diff();
1088 /* NOTREACHED */
1092 argc -= optind;
1093 argv += optind;
1095 cwd = getcwd(NULL, 0);
1096 if (cwd == NULL) {
1097 error = got_error_from_errno();
1098 goto done;
1100 error = got_worktree_open(&worktree, cwd);
1101 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1102 goto done;
1103 if (argc <= 1) {
1104 if (worktree == NULL) {
1105 error = got_error(GOT_ERR_NOT_WORKTREE);
1106 goto done;
1108 if (repo_path)
1109 errx(1,
1110 "-r option can't be used when diffing a work tree");
1111 repo_path = strdup(got_worktree_get_repo_path(worktree));
1112 if (repo_path == NULL) {
1113 error = got_error_from_errno();
1114 goto done;
1116 if (argc == 1) {
1117 error = got_worktree_resolve_path(&path, worktree,
1118 argv[0]);
1119 if (error)
1120 goto done;
1121 } else {
1122 path = strdup("");
1123 if (path == NULL) {
1124 error = got_error_from_errno();
1125 goto done;
1128 } else if (argc == 2) {
1129 id_str1 = argv[0];
1130 id_str2 = argv[1];
1131 } else
1132 usage_diff();
1134 if (repo_path == NULL) {
1135 repo_path = getcwd(NULL, 0);
1136 if (repo_path == NULL)
1137 return got_error_from_errno();
1140 error = apply_unveil(repo_path, 1,
1141 worktree ? got_worktree_get_root_path(worktree) : NULL);
1142 if (error)
1143 goto done;
1145 error = got_repo_open(&repo, repo_path);
1146 free(repo_path);
1147 if (error != NULL)
1148 goto done;
1150 if (worktree) {
1151 struct print_diff_arg arg;
1152 char *id_str;
1153 error = got_object_id_str(&id_str,
1154 got_worktree_get_base_commit_id(worktree));
1155 if (error)
1156 goto done;
1157 arg.repo = repo;
1158 arg.worktree = worktree;
1159 arg.diff_context = diff_context;
1160 arg.id_str = id_str;
1161 arg.header_shown = 0;
1163 error = got_worktree_status(worktree, path, repo, print_diff,
1164 &arg, check_cancelled, NULL);
1165 free(id_str);
1166 goto done;
1169 error = got_object_resolve_id_str(&id1, repo, id_str1);
1170 if (error)
1171 goto done;
1173 error = got_object_resolve_id_str(&id2, repo, id_str2);
1174 if (error)
1175 goto done;
1177 error = got_object_get_type(&type1, repo, id1);
1178 if (error)
1179 goto done;
1181 error = got_object_get_type(&type2, repo, id2);
1182 if (error)
1183 goto done;
1185 if (type1 != type2) {
1186 error = got_error(GOT_ERR_OBJ_TYPE);
1187 goto done;
1190 switch (type1) {
1191 case GOT_OBJ_TYPE_BLOB:
1192 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1193 diff_context, repo, stdout);
1194 break;
1195 case GOT_OBJ_TYPE_TREE:
1196 error = got_diff_objects_as_trees(id1, id2, "", "",
1197 diff_context, repo, stdout);
1198 break;
1199 case GOT_OBJ_TYPE_COMMIT:
1200 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1201 id_str2);
1202 error = got_diff_objects_as_commits(id1, id2, diff_context,
1203 repo, stdout);
1204 break;
1205 default:
1206 error = got_error(GOT_ERR_OBJ_TYPE);
1209 done:
1210 free(id1);
1211 free(id2);
1212 free(path);
1213 if (worktree)
1214 got_worktree_close(worktree);
1215 if (repo) {
1216 const struct got_error *repo_error;
1217 repo_error = got_repo_close(repo);
1218 if (error == NULL)
1219 error = repo_error;
1221 return error;
1224 __dead static void
1225 usage_blame(void)
1227 fprintf(stderr,
1228 "usage: %s blame [-c commit] [-r repository-path] path\n",
1229 getprogname());
1230 exit(1);
1233 static const struct got_error *
1234 cmd_blame(int argc, char *argv[])
1236 const struct got_error *error;
1237 struct got_repository *repo = NULL;
1238 struct got_worktree *worktree = NULL;
1239 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1240 struct got_object_id *commit_id = NULL;
1241 char *commit_id_str = NULL;
1242 int ch;
1244 #ifndef PROFILE
1245 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1246 NULL) == -1)
1247 err(1, "pledge");
1248 #endif
1250 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1251 switch (ch) {
1252 case 'c':
1253 commit_id_str = optarg;
1254 break;
1255 case 'r':
1256 repo_path = realpath(optarg, NULL);
1257 if (repo_path == NULL)
1258 err(1, "-r option");
1259 break;
1260 default:
1261 usage_blame();
1262 /* NOTREACHED */
1266 argc -= optind;
1267 argv += optind;
1269 if (argc == 1)
1270 path = argv[0];
1271 else
1272 usage_blame();
1274 cwd = getcwd(NULL, 0);
1275 if (cwd == NULL) {
1276 error = got_error_from_errno();
1277 goto done;
1279 if (repo_path == NULL) {
1280 error = got_worktree_open(&worktree, cwd);
1281 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1282 goto done;
1283 else
1284 error = NULL;
1285 if (worktree) {
1286 repo_path =
1287 strdup(got_worktree_get_repo_path(worktree));
1288 if (repo_path == NULL)
1289 error = got_error_from_errno();
1290 if (error)
1291 goto done;
1292 } else {
1293 repo_path = strdup(cwd);
1294 if (repo_path == NULL) {
1295 error = got_error_from_errno();
1296 goto done;
1301 error = apply_unveil(repo_path, 1, NULL);
1302 if (error)
1303 goto done;
1305 error = got_repo_open(&repo, repo_path);
1306 if (error != NULL)
1307 goto done;
1309 if (worktree) {
1310 const char *prefix = got_worktree_get_path_prefix(worktree);
1311 char *p, *worktree_subdir = cwd +
1312 strlen(got_worktree_get_root_path(worktree));
1313 if (asprintf(&p, "%s%s%s%s%s",
1314 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1315 worktree_subdir, worktree_subdir[0] ? "/" : "",
1316 path) == -1) {
1317 error = got_error_from_errno();
1318 goto done;
1320 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1321 free(p);
1322 } else {
1323 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1325 if (error)
1326 goto done;
1328 if (commit_id_str == NULL) {
1329 struct got_reference *head_ref;
1330 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1331 if (error != NULL)
1332 goto done;
1333 error = got_ref_resolve(&commit_id, repo, head_ref);
1334 got_ref_close(head_ref);
1335 if (error != NULL)
1336 goto done;
1337 } else {
1338 error = got_object_resolve_id_str(&commit_id, repo,
1339 commit_id_str);
1340 if (error != NULL)
1341 goto done;
1344 error = got_blame(in_repo_path, commit_id, repo, stdout);
1345 done:
1346 free(in_repo_path);
1347 free(repo_path);
1348 free(cwd);
1349 free(commit_id);
1350 if (worktree)
1351 got_worktree_close(worktree);
1352 if (repo) {
1353 const struct got_error *repo_error;
1354 repo_error = got_repo_close(repo);
1355 if (error == NULL)
1356 error = repo_error;
1358 return error;
1361 __dead static void
1362 usage_tree(void)
1364 fprintf(stderr,
1365 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1366 getprogname());
1367 exit(1);
1370 static void
1371 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1372 const char *root_path)
1374 int is_root_path = (strcmp(path, root_path) == 0);
1376 path += strlen(root_path);
1377 while (path[0] == '/')
1378 path++;
1380 printf("%s%s%s%s%s\n", id ? id : "", path,
1381 is_root_path ? "" : "/", te->name,
1382 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1385 static const struct got_error *
1386 print_tree(const char *path, struct got_object_id *commit_id,
1387 int show_ids, int recurse, const char *root_path,
1388 struct got_repository *repo)
1390 const struct got_error *err = NULL;
1391 struct got_object_id *tree_id = NULL;
1392 struct got_tree_object *tree = NULL;
1393 const struct got_tree_entries *entries;
1394 struct got_tree_entry *te;
1396 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1397 if (err)
1398 goto done;
1400 err = got_object_open_as_tree(&tree, repo, tree_id);
1401 if (err)
1402 goto done;
1403 entries = got_object_tree_get_entries(tree);
1404 te = SIMPLEQ_FIRST(&entries->head);
1405 while (te) {
1406 char *id = NULL;
1408 if (sigint_received || sigpipe_received)
1409 break;
1411 if (show_ids) {
1412 char *id_str;
1413 err = got_object_id_str(&id_str, te->id);
1414 if (err)
1415 goto done;
1416 if (asprintf(&id, "%s ", id_str) == -1) {
1417 err = got_error_from_errno();
1418 free(id_str);
1419 goto done;
1421 free(id_str);
1423 print_entry(te, id, path, root_path);
1424 free(id);
1426 if (recurse && S_ISDIR(te->mode)) {
1427 char *child_path;
1428 if (asprintf(&child_path, "%s%s%s", path,
1429 path[0] == '/' && path[1] == '\0' ? "" : "/",
1430 te->name) == -1) {
1431 err = got_error_from_errno();
1432 goto done;
1434 err = print_tree(child_path, commit_id, show_ids, 1,
1435 root_path, repo);
1436 free(child_path);
1437 if (err)
1438 goto done;
1441 te = SIMPLEQ_NEXT(te, entry);
1443 done:
1444 if (tree)
1445 got_object_tree_close(tree);
1446 free(tree_id);
1447 return err;
1450 static const struct got_error *
1451 cmd_tree(int argc, char *argv[])
1453 const struct got_error *error;
1454 struct got_repository *repo = NULL;
1455 struct got_worktree *worktree = NULL;
1456 const char *path;
1457 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1458 struct got_object_id *commit_id = NULL;
1459 char *commit_id_str = NULL;
1460 int show_ids = 0, recurse = 0;
1461 int ch;
1463 #ifndef PROFILE
1464 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1465 NULL) == -1)
1466 err(1, "pledge");
1467 #endif
1469 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1470 switch (ch) {
1471 case 'c':
1472 commit_id_str = optarg;
1473 break;
1474 case 'r':
1475 repo_path = realpath(optarg, NULL);
1476 if (repo_path == NULL)
1477 err(1, "-r option");
1478 break;
1479 case 'i':
1480 show_ids = 1;
1481 break;
1482 case 'R':
1483 recurse = 1;
1484 break;
1485 default:
1486 usage_tree();
1487 /* NOTREACHED */
1491 argc -= optind;
1492 argv += optind;
1494 if (argc == 1)
1495 path = argv[0];
1496 else if (argc > 1)
1497 usage_tree();
1498 else
1499 path = NULL;
1501 cwd = getcwd(NULL, 0);
1502 if (cwd == NULL) {
1503 error = got_error_from_errno();
1504 goto done;
1506 if (repo_path == NULL) {
1507 error = got_worktree_open(&worktree, cwd);
1508 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1509 goto done;
1510 else
1511 error = NULL;
1512 if (worktree) {
1513 repo_path =
1514 strdup(got_worktree_get_repo_path(worktree));
1515 if (repo_path == NULL)
1516 error = got_error_from_errno();
1517 if (error)
1518 goto done;
1519 } else {
1520 repo_path = strdup(cwd);
1521 if (repo_path == NULL) {
1522 error = got_error_from_errno();
1523 goto done;
1528 error = apply_unveil(repo_path, 1, NULL);
1529 if (error)
1530 goto done;
1532 error = got_repo_open(&repo, repo_path);
1533 if (error != NULL)
1534 goto done;
1536 if (path == NULL) {
1537 if (worktree) {
1538 char *p, *worktree_subdir = cwd +
1539 strlen(got_worktree_get_root_path(worktree));
1540 if (asprintf(&p, "%s/%s",
1541 got_worktree_get_path_prefix(worktree),
1542 worktree_subdir) == -1) {
1543 error = got_error_from_errno();
1544 goto done;
1546 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1547 free(p);
1548 if (error)
1549 goto done;
1550 } else
1551 path = "/";
1553 if (in_repo_path == NULL) {
1554 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1555 if (error != NULL)
1556 goto done;
1559 if (commit_id_str == NULL) {
1560 struct got_reference *head_ref;
1561 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1562 if (error != NULL)
1563 goto done;
1564 error = got_ref_resolve(&commit_id, repo, head_ref);
1565 got_ref_close(head_ref);
1566 if (error != NULL)
1567 goto done;
1568 } else {
1569 error = got_object_resolve_id_str(&commit_id, repo,
1570 commit_id_str);
1571 if (error != NULL)
1572 goto done;
1575 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1576 in_repo_path, repo);
1577 done:
1578 free(in_repo_path);
1579 free(repo_path);
1580 free(cwd);
1581 free(commit_id);
1582 if (worktree)
1583 got_worktree_close(worktree);
1584 if (repo) {
1585 const struct got_error *repo_error;
1586 repo_error = got_repo_close(repo);
1587 if (error == NULL)
1588 error = repo_error;
1590 return error;
1593 __dead static void
1594 usage_status(void)
1596 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1597 exit(1);
1600 static const struct got_error *
1601 print_status(void *arg, unsigned char status, const char *path,
1602 struct got_object_id *id)
1604 printf("%c %s\n", status, path);
1605 return NULL;
1608 static const struct got_error *
1609 cmd_status(int argc, char *argv[])
1611 const struct got_error *error = NULL;
1612 struct got_repository *repo = NULL;
1613 struct got_worktree *worktree = NULL;
1614 char *cwd = NULL, *path = NULL;
1615 int ch;
1617 while ((ch = getopt(argc, argv, "")) != -1) {
1618 switch (ch) {
1619 default:
1620 usage_status();
1621 /* NOTREACHED */
1625 argc -= optind;
1626 argv += optind;
1628 #ifndef PROFILE
1629 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1630 NULL) == -1)
1631 err(1, "pledge");
1632 #endif
1633 cwd = getcwd(NULL, 0);
1634 if (cwd == NULL) {
1635 error = got_error_from_errno();
1636 goto done;
1639 error = got_worktree_open(&worktree, cwd);
1640 if (error != NULL)
1641 goto done;
1643 if (argc == 0) {
1644 path = strdup("");
1645 if (path == NULL) {
1646 error = got_error_from_errno();
1647 goto done;
1649 } else if (argc == 1) {
1650 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1651 if (error)
1652 goto done;
1653 } else
1654 usage_status();
1656 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1657 if (error != NULL)
1658 goto done;
1660 error = apply_unveil(got_repo_get_path(repo), 1,
1661 got_worktree_get_root_path(worktree));
1662 if (error)
1663 goto done;
1665 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1666 check_cancelled, NULL);
1667 done:
1668 free(cwd);
1669 free(path);
1670 return error;
1673 __dead static void
1674 usage_ref(void)
1676 fprintf(stderr,
1677 "usage: %s ref [-r repository] -l | -d name | name object\n",
1678 getprogname());
1679 exit(1);
1682 static const struct got_error *
1683 list_refs(struct got_repository *repo)
1685 static const struct got_error *err = NULL;
1686 struct got_reflist_head refs;
1687 struct got_reflist_entry *re;
1689 SIMPLEQ_INIT(&refs);
1690 err = got_ref_list(&refs, repo);
1691 if (err)
1692 return err;
1694 SIMPLEQ_FOREACH(re, &refs, entry) {
1695 char *refstr;
1696 refstr = got_ref_to_str(re->ref);
1697 if (refstr == NULL)
1698 return got_error_from_errno();
1699 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1700 free(refstr);
1703 got_ref_list_free(&refs);
1704 return NULL;
1707 static const struct got_error *
1708 delete_ref(struct got_repository *repo, const char *refname)
1710 const struct got_error *err = NULL;
1711 struct got_reference *ref;
1713 err = got_ref_open(&ref, repo, refname);
1714 if (err)
1715 return err;
1717 err = got_ref_delete(ref, repo);
1718 got_ref_close(ref);
1719 return err;
1722 static const struct got_error *
1723 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1725 const struct got_error *err = NULL;
1726 struct got_object_id *id;
1727 struct got_reference *ref = NULL;
1729 err = got_object_resolve_id_str(&id, repo, id_str);
1730 if (err)
1731 return err;
1733 err = got_ref_alloc(&ref, refname, id);
1734 if (err)
1735 goto done;
1737 err = got_ref_write(ref, repo);
1738 done:
1739 if (ref)
1740 got_ref_close(ref);
1741 free(id);
1742 return err;
1745 static const struct got_error *
1746 cmd_ref(int argc, char *argv[])
1748 const struct got_error *error = NULL;
1749 struct got_repository *repo = NULL;
1750 struct got_worktree *worktree = NULL;
1751 char *cwd = NULL, *repo_path = NULL;
1752 int ch, do_list = 0;
1753 const char *delref = NULL;
1755 /* TODO: Add -s option for adding symbolic references. */
1756 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1757 switch (ch) {
1758 case 'd':
1759 delref = optarg;
1760 break;
1761 case 'r':
1762 repo_path = realpath(optarg, NULL);
1763 if (repo_path == NULL)
1764 err(1, "-r option");
1765 break;
1766 case 'l':
1767 do_list = 1;
1768 break;
1769 default:
1770 usage_ref();
1771 /* NOTREACHED */
1775 if (do_list && delref)
1776 errx(1, "-l and -d options are mutually exclusive\n");
1778 argc -= optind;
1779 argv += optind;
1781 if (do_list || delref) {
1782 if (argc > 0)
1783 usage_ref();
1784 } else if (argc != 2)
1785 usage_ref();
1787 #ifndef PROFILE
1788 if (do_list) {
1789 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1790 NULL) == -1)
1791 err(1, "pledge");
1792 } else {
1793 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1794 "sendfd unveil", NULL) == -1)
1795 err(1, "pledge");
1797 #endif
1798 cwd = getcwd(NULL, 0);
1799 if (cwd == NULL) {
1800 error = got_error_from_errno();
1801 goto done;
1804 if (repo_path == NULL) {
1805 error = got_worktree_open(&worktree, cwd);
1806 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1807 goto done;
1808 else
1809 error = NULL;
1810 if (worktree) {
1811 repo_path =
1812 strdup(got_worktree_get_repo_path(worktree));
1813 if (repo_path == NULL)
1814 error = got_error_from_errno();
1815 if (error)
1816 goto done;
1817 } else {
1818 repo_path = strdup(cwd);
1819 if (repo_path == NULL) {
1820 error = got_error_from_errno();
1821 goto done;
1826 error = apply_unveil(repo_path, do_list,
1827 worktree ? got_worktree_get_root_path(worktree) : NULL);
1828 if (error)
1829 goto done;
1831 error = got_repo_open(&repo, repo_path);
1832 if (error != NULL)
1833 goto done;
1835 if (do_list)
1836 error = list_refs(repo);
1837 else if (delref)
1838 error = delete_ref(repo, delref);
1839 else
1840 error = add_ref(repo, argv[0], argv[1]);
1841 done:
1842 if (repo)
1843 got_repo_close(repo);
1844 if (worktree)
1845 got_worktree_close(worktree);
1846 free(cwd);
1847 free(repo_path);
1848 return error;
1851 __dead static void
1852 usage_add(void)
1854 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1855 exit(1);
1858 static const struct got_error *
1859 cmd_add(int argc, char *argv[])
1861 const struct got_error *error = NULL;
1862 struct got_repository *repo = NULL;
1863 struct got_worktree *worktree = NULL;
1864 char *cwd = NULL, *path = NULL, *relpath = NULL;
1865 int ch;
1867 while ((ch = getopt(argc, argv, "")) != -1) {
1868 switch (ch) {
1869 default:
1870 usage_add();
1871 /* NOTREACHED */
1875 argc -= optind;
1876 argv += optind;
1878 if (argc != 1)
1879 usage_add();
1881 path = realpath(argv[0], NULL);
1882 if (path == NULL) {
1883 error = got_error_from_errno();
1884 goto done;
1887 cwd = getcwd(NULL, 0);
1888 if (cwd == NULL) {
1889 error = got_error_from_errno();
1890 goto done;
1892 error = got_worktree_open(&worktree, cwd);
1893 if (error)
1894 goto done;
1896 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1897 if (error != NULL)
1898 goto done;
1900 error = apply_unveil(got_repo_get_path(repo), 1,
1901 got_worktree_get_root_path(worktree));
1902 if (error)
1903 goto done;
1905 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1906 repo);
1907 if (error)
1908 goto done;
1909 done:
1910 if (repo)
1911 got_repo_close(repo);
1912 if (worktree)
1913 got_worktree_close(worktree);
1914 free(path);
1915 free(relpath);
1916 free(cwd);
1917 return error;
1920 __dead static void
1921 usage_rm(void)
1923 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1924 exit(1);
1927 static const struct got_error *
1928 cmd_rm(int argc, char *argv[])
1930 const struct got_error *error = NULL;
1931 struct got_worktree *worktree = NULL;
1932 struct got_repository *repo = NULL;
1933 char *cwd = NULL, *path = NULL;
1934 int ch, delete_local_mods = 0;
1936 while ((ch = getopt(argc, argv, "f")) != -1) {
1937 switch (ch) {
1938 case 'f':
1939 delete_local_mods = 1;
1940 break;
1941 default:
1942 usage_add();
1943 /* NOTREACHED */
1947 argc -= optind;
1948 argv += optind;
1950 if (argc != 1)
1951 usage_rm();
1953 path = realpath(argv[0], NULL);
1954 if (path == NULL) {
1955 error = got_error_from_errno();
1956 goto done;
1959 cwd = getcwd(NULL, 0);
1960 if (cwd == NULL) {
1961 error = got_error_from_errno();
1962 goto done;
1964 error = got_worktree_open(&worktree, cwd);
1965 if (error)
1966 goto done;
1968 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1969 if (error != NULL)
1970 goto done;
1972 error = apply_unveil(got_repo_get_path(repo), 1,
1973 got_worktree_get_root_path(worktree));
1974 if (error)
1975 goto done;
1977 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1978 print_status, NULL, repo);
1979 if (error)
1980 goto done;
1981 done:
1982 if (repo)
1983 got_repo_close(repo);
1984 if (worktree)
1985 got_worktree_close(worktree);
1986 free(path);
1987 free(cwd);
1988 return error;
1991 __dead static void
1992 usage_revert(void)
1994 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
1995 exit(1);
1998 static void
1999 revert_progress(void *arg, unsigned char status, const char *path)
2001 while (path[0] == '/')
2002 path++;
2003 printf("%c %s\n", status, path);
2006 static const struct got_error *
2007 cmd_revert(int argc, char *argv[])
2009 const struct got_error *error = NULL;
2010 struct got_worktree *worktree = NULL;
2011 struct got_repository *repo = NULL;
2012 char *cwd = NULL, *path = NULL;
2013 int ch;
2015 while ((ch = getopt(argc, argv, "")) != -1) {
2016 switch (ch) {
2017 default:
2018 usage_revert();
2019 /* NOTREACHED */
2023 argc -= optind;
2024 argv += optind;
2026 if (argc != 1)
2027 usage_revert();
2029 path = realpath(argv[0], NULL);
2030 if (path == NULL) {
2031 error = got_error_from_errno();
2032 goto done;
2035 cwd = getcwd(NULL, 0);
2036 if (cwd == NULL) {
2037 error = got_error_from_errno();
2038 goto done;
2040 error = got_worktree_open(&worktree, cwd);
2041 if (error)
2042 goto done;
2044 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2045 if (error != NULL)
2046 goto done;
2048 error = apply_unveil(got_repo_get_path(repo), 1,
2049 got_worktree_get_root_path(worktree));
2050 if (error)
2051 goto done;
2053 error = got_worktree_revert(worktree, path,
2054 revert_progress, NULL, repo);
2055 if (error)
2056 goto done;
2057 done:
2058 if (repo)
2059 got_repo_close(repo);
2060 if (worktree)
2061 got_worktree_close(worktree);
2062 free(path);
2063 free(cwd);
2064 return error;