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 = got_repo_open(&repo, repo_path);
380 if (error != NULL)
381 goto done;
383 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
384 if (error)
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] [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, *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 worktree_path = getcwd(NULL, 0);
495 if (worktree_path == NULL) {
496 error = got_error_from_errno();
497 goto done;
499 error = got_worktree_open(&worktree, worktree_path);
500 if (error)
501 goto done;
503 if (argc == 0) {
504 path = strdup("");
505 if (path == NULL) {
506 error = got_error_from_errno();
507 goto done;
509 } else if (argc == 1) {
510 error = got_worktree_resolve_path(&path, worktree, argv[0]);
511 if (error)
512 goto done;
513 } else
514 usage_update();
516 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
517 if (error != NULL)
518 goto done;
520 error = apply_unveil(got_repo_get_path(repo), 0,
521 got_worktree_get_root_path(worktree));
522 if (error)
523 goto done;
525 if (commit_id_str == NULL) {
526 struct got_reference *head_ref;
527 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
528 if (error != NULL)
529 goto done;
530 error = got_ref_resolve(&commit_id, repo, head_ref);
531 if (error != NULL)
532 goto done;
533 error = got_object_id_str(&commit_id_str, commit_id);
534 if (error != NULL)
535 goto done;
536 } else {
537 error = got_object_resolve_id_str(&commit_id, repo,
538 commit_id_str);
539 if (error != NULL)
540 goto done;
543 error = check_ancestry(worktree, commit_id, repo);
544 if (error != NULL)
545 goto done;
547 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
548 commit_id) != 0) {
549 error = got_worktree_set_base_commit_id(worktree, repo,
550 commit_id);
551 if (error)
552 goto done;
555 error = got_worktree_checkout_files(worktree, path, repo,
556 update_progress, &did_something, check_cancelled, NULL);
557 if (error != NULL)
558 goto done;
560 if (did_something)
561 printf("Updated to commit %s\n", commit_id_str);
562 else
563 printf("Already up-to-date\n");
564 done:
565 free(worktree_path);
566 free(path);
567 free(commit_id);
568 free(commit_id_str);
569 return error;
572 static const struct got_error *
573 print_patch(struct got_commit_object *commit, struct got_object_id *id,
574 int diff_context, struct got_repository *repo)
576 const struct got_error *err = NULL;
577 struct got_tree_object *tree1 = NULL, *tree2;
578 struct got_object_qid *qid;
579 char *id_str1 = NULL, *id_str2;
581 err = got_object_open_as_tree(&tree2, repo,
582 got_object_commit_get_tree_id(commit));
583 if (err)
584 return err;
586 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
587 if (qid != NULL) {
588 struct got_commit_object *pcommit;
590 err = got_object_open_as_commit(&pcommit, repo, qid->id);
591 if (err)
592 return err;
594 err = got_object_open_as_tree(&tree1, repo,
595 got_object_commit_get_tree_id(pcommit));
596 got_object_commit_close(pcommit);
597 if (err)
598 return err;
600 err = got_object_id_str(&id_str1, qid->id);
601 if (err)
602 return err;
605 err = got_object_id_str(&id_str2, id);
606 if (err)
607 goto done;
609 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
610 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
611 done:
612 if (tree1)
613 got_object_tree_close(tree1);
614 got_object_tree_close(tree2);
615 free(id_str1);
616 free(id_str2);
617 return err;
620 static char *
621 get_datestr(time_t *time, char *datebuf)
623 char *p, *s = ctime_r(time, datebuf);
624 p = strchr(s, '\n');
625 if (p)
626 *p = '\0';
627 return s;
630 static const struct got_error *
631 print_commit(struct got_commit_object *commit, struct got_object_id *id,
632 struct got_repository *repo, int show_patch, int diff_context,
633 struct got_reflist_head *refs)
635 const struct got_error *err = NULL;
636 char *id_str, *datestr, *logmsg0, *logmsg, *line;
637 char datebuf[26];
638 time_t committer_time;
639 const char *author, *committer;
640 char *refs_str = NULL;
641 struct got_reflist_entry *re;
643 SIMPLEQ_FOREACH(re, refs, entry) {
644 char *s;
645 const char *name;
646 if (got_object_id_cmp(re->id, id) != 0)
647 continue;
648 name = got_ref_get_name(re->ref);
649 if (strcmp(name, GOT_REF_HEAD) == 0)
650 continue;
651 if (strncmp(name, "refs/", 5) == 0)
652 name += 5;
653 if (strncmp(name, "got/", 4) == 0)
654 continue;
655 if (strncmp(name, "heads/", 6) == 0)
656 name += 6;
657 if (strncmp(name, "remotes/", 8) == 0)
658 name += 8;
659 s = refs_str;
660 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
661 name) == -1) {
662 err = got_error_from_errno();
663 free(s);
664 break;
666 free(s);
668 err = got_object_id_str(&id_str, id);
669 if (err)
670 return err;
672 printf("-----------------------------------------------\n");
673 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
674 refs_str ? refs_str : "", refs_str ? ")" : "");
675 free(id_str);
676 id_str = NULL;
677 free(refs_str);
678 refs_str = NULL;
679 printf("from: %s\n", got_object_commit_get_author(commit));
680 committer_time = got_object_commit_get_committer_time(commit);
681 datestr = get_datestr(&committer_time, datebuf);
682 printf("date: %s UTC\n", datestr);
683 author = got_object_commit_get_author(commit);
684 committer = got_object_commit_get_committer(commit);
685 if (strcmp(author, committer) != 0)
686 printf("via: %s\n", committer);
687 if (got_object_commit_get_nparents(commit) > 1) {
688 const struct got_object_id_queue *parent_ids;
689 struct got_object_qid *qid;
690 int n = 1;
691 parent_ids = got_object_commit_get_parent_ids(commit);
692 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
693 err = got_object_id_str(&id_str, qid->id);
694 if (err)
695 return err;
696 printf("parent %d: %s\n", n++, id_str);
697 free(id_str);
701 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
702 if (logmsg0 == NULL)
703 return got_error_from_errno();
705 logmsg = logmsg0;
706 do {
707 line = strsep(&logmsg, "\n");
708 if (line)
709 printf(" %s\n", line);
710 } while (line);
711 free(logmsg0);
713 if (show_patch) {
714 err = print_patch(commit, id, diff_context, repo);
715 if (err == 0)
716 printf("\n");
719 if (fflush(stdout) != 0 && err == NULL)
720 err = got_error_from_errno();
721 return err;
724 static const struct got_error *
725 print_commits(struct got_object_id *root_id, struct got_repository *repo,
726 char *path, int show_patch, int diff_context, int limit,
727 int first_parent_traversal, struct got_reflist_head *refs)
729 const struct got_error *err;
730 struct got_commit_graph *graph;
732 err = got_commit_graph_open(&graph, root_id, path,
733 first_parent_traversal, repo);
734 if (err)
735 return err;
736 err = got_commit_graph_iter_start(graph, root_id, repo);
737 if (err)
738 goto done;
739 while (1) {
740 struct got_commit_object *commit;
741 struct got_object_id *id;
743 if (sigint_received || sigpipe_received)
744 break;
746 err = got_commit_graph_iter_next(&id, graph);
747 if (err) {
748 if (err->code == GOT_ERR_ITER_COMPLETED) {
749 err = NULL;
750 break;
752 if (err->code != GOT_ERR_ITER_NEED_MORE)
753 break;
754 err = got_commit_graph_fetch_commits(graph, 1, repo);
755 if (err)
756 break;
757 else
758 continue;
760 if (id == NULL)
761 break;
763 err = got_object_open_as_commit(&commit, repo, id);
764 if (err)
765 break;
766 err = print_commit(commit, id, repo, show_patch, diff_context,
767 refs);
768 got_object_commit_close(commit);
769 if (err || (limit && --limit == 0))
770 break;
772 done:
773 got_commit_graph_close(graph);
774 return err;
777 __dead static void
778 usage_log(void)
780 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
781 "[-r repository-path] [path]\n", getprogname());
782 exit(1);
785 static const struct got_error *
786 cmd_log(int argc, char *argv[])
788 const struct got_error *error;
789 struct got_repository *repo = NULL;
790 struct got_worktree *worktree = NULL;
791 struct got_commit_object *commit = NULL;
792 struct got_object_id *id = NULL;
793 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
794 char *start_commit = NULL;
795 int diff_context = 3, ch;
796 int show_patch = 0, limit = 0, first_parent_traversal = 0;
797 const char *errstr;
798 struct got_reflist_head refs;
800 SIMPLEQ_INIT(&refs);
802 #ifndef PROFILE
803 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
804 NULL)
805 == -1)
806 err(1, "pledge");
807 #endif
809 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
810 switch (ch) {
811 case 'p':
812 show_patch = 1;
813 break;
814 case 'c':
815 start_commit = optarg;
816 break;
817 case 'C':
818 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
819 &errstr);
820 if (errstr != NULL)
821 err(1, "-C option %s", errstr);
822 break;
823 case 'l':
824 limit = strtonum(optarg, 1, INT_MAX, &errstr);
825 if (errstr != NULL)
826 err(1, "-l option %s", errstr);
827 break;
828 case 'f':
829 first_parent_traversal = 1;
830 break;
831 case 'r':
832 repo_path = realpath(optarg, NULL);
833 if (repo_path == NULL)
834 err(1, "-r option");
835 break;
836 default:
837 usage_log();
838 /* NOTREACHED */
842 argc -= optind;
843 argv += optind;
845 cwd = getcwd(NULL, 0);
846 if (cwd == NULL) {
847 error = got_error_from_errno();
848 goto done;
851 error = got_worktree_open(&worktree, cwd);
852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
853 goto done;
854 error = NULL;
856 if (argc == 0) {
857 path = strdup("");
858 if (path == NULL) {
859 error = got_error_from_errno();
860 goto done;
862 } else if (argc == 1) {
863 if (worktree) {
864 error = got_worktree_resolve_path(&path, worktree,
865 argv[0]);
866 if (error)
867 goto done;
868 } else {
869 path = strdup(argv[0]);
870 if (path == NULL) {
871 error = got_error_from_errno();
872 goto done;
875 } else
876 usage_log();
878 repo_path = worktree ?
879 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
880 if (repo_path == NULL) {
881 error = got_error_from_errno();
882 goto done;
885 error = got_repo_open(&repo, repo_path);
886 if (error != NULL)
887 goto done;
889 error = apply_unveil(got_repo_get_path(repo), 1,
890 worktree ? got_worktree_get_root_path(worktree) : NULL);
891 if (error)
892 goto done;
894 if (start_commit == NULL) {
895 struct got_reference *head_ref;
896 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
897 if (error != NULL)
898 return error;
899 error = got_ref_resolve(&id, repo, head_ref);
900 got_ref_close(head_ref);
901 if (error != NULL)
902 return error;
903 error = got_object_open_as_commit(&commit, repo, id);
904 } else {
905 struct got_reference *ref;
906 error = got_ref_open(&ref, repo, start_commit);
907 if (error == NULL) {
908 int obj_type;
909 error = got_ref_resolve(&id, repo, ref);
910 got_ref_close(ref);
911 if (error != NULL)
912 goto done;
913 error = got_object_get_type(&obj_type, repo, id);
914 if (error != NULL)
915 goto done;
916 if (obj_type == GOT_OBJ_TYPE_TAG) {
917 struct got_tag_object *tag;
918 error = got_object_open_as_tag(&tag, repo, id);
919 if (error != NULL)
920 goto done;
921 if (got_object_tag_get_object_type(tag) !=
922 GOT_OBJ_TYPE_COMMIT) {
923 got_object_tag_close(tag);
924 error = got_error(GOT_ERR_OBJ_TYPE);
925 goto done;
927 free(id);
928 id = got_object_id_dup(
929 got_object_tag_get_object_id(tag));
930 if (id == NULL)
931 error = got_error_from_errno();
932 got_object_tag_close(tag);
933 if (error)
934 goto done;
935 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
936 error = got_error(GOT_ERR_OBJ_TYPE);
937 goto done;
939 error = got_object_open_as_commit(&commit, repo, id);
940 if (error != NULL)
941 goto done;
943 if (commit == NULL) {
944 error = got_object_resolve_id_str(&id, repo,
945 start_commit);
946 if (error != NULL)
947 return error;
950 if (error != NULL)
951 goto done;
953 error = got_repo_map_path(&in_repo_path, repo, path, 1);
954 if (error != NULL)
955 goto done;
956 if (in_repo_path) {
957 free(path);
958 path = in_repo_path;
961 error = got_ref_list(&refs, repo);
962 if (error)
963 goto done;
965 error = print_commits(id, repo, path, show_patch,
966 diff_context, limit, first_parent_traversal, &refs);
967 done:
968 free(path);
969 free(repo_path);
970 free(cwd);
971 free(id);
972 if (worktree)
973 got_worktree_close(worktree);
974 if (repo) {
975 const struct got_error *repo_error;
976 repo_error = got_repo_close(repo);
977 if (error == NULL)
978 error = repo_error;
980 got_ref_list_free(&refs);
981 return error;
984 __dead static void
985 usage_diff(void)
987 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
988 "[object1 object2 | path]\n", getprogname());
989 exit(1);
992 struct print_diff_arg {
993 struct got_repository *repo;
994 struct got_worktree *worktree;
995 int diff_context;
996 const char *id_str;
997 int header_shown;
998 };
1000 static const struct got_error *
1001 print_diff(void *arg, unsigned char status, const char *path,
1002 struct got_object_id *id)
1004 struct print_diff_arg *a = arg;
1005 const struct got_error *err = NULL;
1006 struct got_blob_object *blob1 = NULL;
1007 FILE *f2 = NULL;
1008 char *abspath = NULL;
1009 struct stat sb;
1011 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1012 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1013 return NULL;
1015 if (!a->header_shown) {
1016 printf("diff %s %s\n", a->id_str,
1017 got_worktree_get_root_path(a->worktree));
1018 a->header_shown = 1;
1021 if (status != GOT_STATUS_ADD) {
1022 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1023 if (err)
1024 goto done;
1028 if (status != GOT_STATUS_DELETE) {
1029 if (asprintf(&abspath, "%s/%s",
1030 got_worktree_get_root_path(a->worktree), path) == -1) {
1031 err = got_error_from_errno();
1032 goto done;
1035 f2 = fopen(abspath, "r");
1036 if (f2 == NULL) {
1037 err = got_error_from_errno();
1038 goto done;
1040 if (lstat(abspath, &sb) == -1) {
1041 err = got_error_from_errno();
1042 goto done;
1044 } else
1045 sb.st_size = 0;
1047 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1048 stdout);
1049 done:
1050 if (blob1)
1051 got_object_blob_close(blob1);
1052 if (f2 && fclose(f2) != 0 && err == NULL)
1053 err = got_error_from_errno();
1054 free(abspath);
1055 return err;
1058 static const struct got_error *
1059 cmd_diff(int argc, char *argv[])
1061 const struct got_error *error;
1062 struct got_repository *repo = NULL;
1063 struct got_worktree *worktree = NULL;
1064 char *cwd = NULL, *repo_path = NULL;
1065 struct got_object_id *id1 = NULL, *id2 = NULL;
1066 char *id_str1 = NULL, *id_str2 = NULL;
1067 int type1, type2;
1068 int diff_context = 3, ch;
1069 const char *errstr;
1070 char *path = NULL;
1072 #ifndef PROFILE
1073 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1074 NULL) == -1)
1075 err(1, "pledge");
1076 #endif
1078 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1079 switch (ch) {
1080 case 'C':
1081 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1082 if (errstr != NULL)
1083 err(1, "-C option %s", errstr);
1084 break;
1085 case 'r':
1086 repo_path = realpath(optarg, NULL);
1087 if (repo_path == NULL)
1088 err(1, "-r option");
1089 break;
1090 default:
1091 usage_diff();
1092 /* NOTREACHED */
1096 argc -= optind;
1097 argv += optind;
1099 cwd = getcwd(NULL, 0);
1100 if (cwd == NULL) {
1101 error = got_error_from_errno();
1102 goto done;
1104 error = got_worktree_open(&worktree, cwd);
1105 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1106 goto done;
1107 if (argc <= 1) {
1108 if (worktree == NULL) {
1109 error = got_error(GOT_ERR_NOT_WORKTREE);
1110 goto done;
1112 if (repo_path)
1113 errx(1,
1114 "-r option can't be used when diffing a work tree");
1115 repo_path = strdup(got_worktree_get_repo_path(worktree));
1116 if (repo_path == NULL) {
1117 error = got_error_from_errno();
1118 goto done;
1120 if (argc == 1) {
1121 error = got_worktree_resolve_path(&path, worktree,
1122 argv[0]);
1123 if (error)
1124 goto done;
1125 } else {
1126 path = strdup("");
1127 if (path == NULL) {
1128 error = got_error_from_errno();
1129 goto done;
1132 } else if (argc == 2) {
1133 id_str1 = argv[0];
1134 id_str2 = argv[1];
1135 } else
1136 usage_diff();
1138 if (repo_path == NULL) {
1139 repo_path = getcwd(NULL, 0);
1140 if (repo_path == NULL)
1141 return got_error_from_errno();
1144 error = got_repo_open(&repo, repo_path);
1145 free(repo_path);
1146 if (error != NULL)
1147 goto done;
1149 error = apply_unveil(got_repo_get_path(repo), 1,
1150 worktree ? got_worktree_get_root_path(worktree) : NULL);
1151 if (error)
1152 goto done;
1154 if (worktree) {
1155 struct print_diff_arg arg;
1156 char *id_str;
1157 error = got_object_id_str(&id_str,
1158 got_worktree_get_base_commit_id(worktree));
1159 if (error)
1160 goto done;
1161 arg.repo = repo;
1162 arg.worktree = worktree;
1163 arg.diff_context = diff_context;
1164 arg.id_str = id_str;
1165 arg.header_shown = 0;
1167 error = got_worktree_status(worktree, path, repo, print_diff,
1168 &arg, check_cancelled, NULL);
1169 free(id_str);
1170 goto done;
1173 error = got_object_resolve_id_str(&id1, repo, id_str1);
1174 if (error)
1175 goto done;
1177 error = got_object_resolve_id_str(&id2, repo, id_str2);
1178 if (error)
1179 goto done;
1181 error = got_object_get_type(&type1, repo, id1);
1182 if (error)
1183 goto done;
1185 error = got_object_get_type(&type2, repo, id2);
1186 if (error)
1187 goto done;
1189 if (type1 != type2) {
1190 error = got_error(GOT_ERR_OBJ_TYPE);
1191 goto done;
1194 switch (type1) {
1195 case GOT_OBJ_TYPE_BLOB:
1196 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1197 diff_context, repo, stdout);
1198 break;
1199 case GOT_OBJ_TYPE_TREE:
1200 error = got_diff_objects_as_trees(id1, id2, "", "",
1201 diff_context, repo, stdout);
1202 break;
1203 case GOT_OBJ_TYPE_COMMIT:
1204 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1205 id_str2);
1206 error = got_diff_objects_as_commits(id1, id2, diff_context,
1207 repo, stdout);
1208 break;
1209 default:
1210 error = got_error(GOT_ERR_OBJ_TYPE);
1213 done:
1214 free(id1);
1215 free(id2);
1216 free(path);
1217 if (worktree)
1218 got_worktree_close(worktree);
1219 if (repo) {
1220 const struct got_error *repo_error;
1221 repo_error = got_repo_close(repo);
1222 if (error == NULL)
1223 error = repo_error;
1225 return error;
1228 __dead static void
1229 usage_blame(void)
1231 fprintf(stderr,
1232 "usage: %s blame [-c commit] [-r repository-path] path\n",
1233 getprogname());
1234 exit(1);
1237 static const struct got_error *
1238 cmd_blame(int argc, char *argv[])
1240 const struct got_error *error;
1241 struct got_repository *repo = NULL;
1242 struct got_worktree *worktree = NULL;
1243 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1244 struct got_object_id *commit_id = NULL;
1245 char *commit_id_str = NULL;
1246 int ch;
1248 #ifndef PROFILE
1249 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1250 NULL) == -1)
1251 err(1, "pledge");
1252 #endif
1254 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1255 switch (ch) {
1256 case 'c':
1257 commit_id_str = optarg;
1258 break;
1259 case 'r':
1260 repo_path = realpath(optarg, NULL);
1261 if (repo_path == NULL)
1262 err(1, "-r option");
1263 break;
1264 default:
1265 usage_blame();
1266 /* NOTREACHED */
1270 argc -= optind;
1271 argv += optind;
1273 if (argc == 1)
1274 path = argv[0];
1275 else
1276 usage_blame();
1278 cwd = getcwd(NULL, 0);
1279 if (cwd == NULL) {
1280 error = got_error_from_errno();
1281 goto done;
1283 if (repo_path == NULL) {
1284 error = got_worktree_open(&worktree, cwd);
1285 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1286 goto done;
1287 else
1288 error = NULL;
1289 if (worktree) {
1290 repo_path =
1291 strdup(got_worktree_get_repo_path(worktree));
1292 if (repo_path == NULL)
1293 error = got_error_from_errno();
1294 if (error)
1295 goto done;
1296 } else {
1297 repo_path = strdup(cwd);
1298 if (repo_path == NULL) {
1299 error = got_error_from_errno();
1300 goto done;
1305 error = got_repo_open(&repo, repo_path);
1306 if (error != NULL)
1307 goto done;
1309 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1310 if (error)
1311 goto done;
1313 if (worktree) {
1314 const char *prefix = got_worktree_get_path_prefix(worktree);
1315 char *p, *worktree_subdir = cwd +
1316 strlen(got_worktree_get_root_path(worktree));
1317 if (asprintf(&p, "%s%s%s%s%s",
1318 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1319 worktree_subdir, worktree_subdir[0] ? "/" : "",
1320 path) == -1) {
1321 error = got_error_from_errno();
1322 goto done;
1324 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1325 free(p);
1326 } else {
1327 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1329 if (error)
1330 goto done;
1332 if (commit_id_str == NULL) {
1333 struct got_reference *head_ref;
1334 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1335 if (error != NULL)
1336 goto done;
1337 error = got_ref_resolve(&commit_id, repo, head_ref);
1338 got_ref_close(head_ref);
1339 if (error != NULL)
1340 goto done;
1341 } else {
1342 error = got_object_resolve_id_str(&commit_id, repo,
1343 commit_id_str);
1344 if (error != NULL)
1345 goto done;
1348 error = got_blame(in_repo_path, commit_id, repo, stdout);
1349 done:
1350 free(in_repo_path);
1351 free(repo_path);
1352 free(cwd);
1353 free(commit_id);
1354 if (worktree)
1355 got_worktree_close(worktree);
1356 if (repo) {
1357 const struct got_error *repo_error;
1358 repo_error = got_repo_close(repo);
1359 if (error == NULL)
1360 error = repo_error;
1362 return error;
1365 __dead static void
1366 usage_tree(void)
1368 fprintf(stderr,
1369 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1370 getprogname());
1371 exit(1);
1374 static void
1375 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1376 const char *root_path)
1378 int is_root_path = (strcmp(path, root_path) == 0);
1380 path += strlen(root_path);
1381 while (path[0] == '/')
1382 path++;
1384 printf("%s%s%s%s%s\n", id ? id : "", path,
1385 is_root_path ? "" : "/", te->name,
1386 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1389 static const struct got_error *
1390 print_tree(const char *path, struct got_object_id *commit_id,
1391 int show_ids, int recurse, const char *root_path,
1392 struct got_repository *repo)
1394 const struct got_error *err = NULL;
1395 struct got_object_id *tree_id = NULL;
1396 struct got_tree_object *tree = NULL;
1397 const struct got_tree_entries *entries;
1398 struct got_tree_entry *te;
1400 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1401 if (err)
1402 goto done;
1404 err = got_object_open_as_tree(&tree, repo, tree_id);
1405 if (err)
1406 goto done;
1407 entries = got_object_tree_get_entries(tree);
1408 te = SIMPLEQ_FIRST(&entries->head);
1409 while (te) {
1410 char *id = NULL;
1412 if (sigint_received || sigpipe_received)
1413 break;
1415 if (show_ids) {
1416 char *id_str;
1417 err = got_object_id_str(&id_str, te->id);
1418 if (err)
1419 goto done;
1420 if (asprintf(&id, "%s ", id_str) == -1) {
1421 err = got_error_from_errno();
1422 free(id_str);
1423 goto done;
1425 free(id_str);
1427 print_entry(te, id, path, root_path);
1428 free(id);
1430 if (recurse && S_ISDIR(te->mode)) {
1431 char *child_path;
1432 if (asprintf(&child_path, "%s%s%s", path,
1433 path[0] == '/' && path[1] == '\0' ? "" : "/",
1434 te->name) == -1) {
1435 err = got_error_from_errno();
1436 goto done;
1438 err = print_tree(child_path, commit_id, show_ids, 1,
1439 root_path, repo);
1440 free(child_path);
1441 if (err)
1442 goto done;
1445 te = SIMPLEQ_NEXT(te, entry);
1447 done:
1448 if (tree)
1449 got_object_tree_close(tree);
1450 free(tree_id);
1451 return err;
1454 static const struct got_error *
1455 cmd_tree(int argc, char *argv[])
1457 const struct got_error *error;
1458 struct got_repository *repo = NULL;
1459 struct got_worktree *worktree = NULL;
1460 const char *path;
1461 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1462 struct got_object_id *commit_id = NULL;
1463 char *commit_id_str = NULL;
1464 int show_ids = 0, recurse = 0;
1465 int ch;
1467 #ifndef PROFILE
1468 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1469 NULL) == -1)
1470 err(1, "pledge");
1471 #endif
1473 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1474 switch (ch) {
1475 case 'c':
1476 commit_id_str = optarg;
1477 break;
1478 case 'r':
1479 repo_path = realpath(optarg, NULL);
1480 if (repo_path == NULL)
1481 err(1, "-r option");
1482 break;
1483 case 'i':
1484 show_ids = 1;
1485 break;
1486 case 'R':
1487 recurse = 1;
1488 break;
1489 default:
1490 usage_tree();
1491 /* NOTREACHED */
1495 argc -= optind;
1496 argv += optind;
1498 if (argc == 1)
1499 path = argv[0];
1500 else if (argc > 1)
1501 usage_tree();
1502 else
1503 path = NULL;
1505 cwd = getcwd(NULL, 0);
1506 if (cwd == NULL) {
1507 error = got_error_from_errno();
1508 goto done;
1510 if (repo_path == NULL) {
1511 error = got_worktree_open(&worktree, cwd);
1512 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1513 goto done;
1514 else
1515 error = NULL;
1516 if (worktree) {
1517 repo_path =
1518 strdup(got_worktree_get_repo_path(worktree));
1519 if (repo_path == NULL)
1520 error = got_error_from_errno();
1521 if (error)
1522 goto done;
1523 } else {
1524 repo_path = strdup(cwd);
1525 if (repo_path == NULL) {
1526 error = got_error_from_errno();
1527 goto done;
1532 error = got_repo_open(&repo, repo_path);
1533 if (error != NULL)
1534 goto done;
1536 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1537 if (error)
1538 goto done;
1540 if (path == NULL) {
1541 if (worktree) {
1542 char *p, *worktree_subdir = cwd +
1543 strlen(got_worktree_get_root_path(worktree));
1544 if (asprintf(&p, "%s/%s",
1545 got_worktree_get_path_prefix(worktree),
1546 worktree_subdir) == -1) {
1547 error = got_error_from_errno();
1548 goto done;
1550 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1551 free(p);
1552 if (error)
1553 goto done;
1554 } else
1555 path = "/";
1557 if (in_repo_path == NULL) {
1558 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1559 if (error != NULL)
1560 goto done;
1563 if (commit_id_str == NULL) {
1564 struct got_reference *head_ref;
1565 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1566 if (error != NULL)
1567 goto done;
1568 error = got_ref_resolve(&commit_id, repo, head_ref);
1569 got_ref_close(head_ref);
1570 if (error != NULL)
1571 goto done;
1572 } else {
1573 error = got_object_resolve_id_str(&commit_id, repo,
1574 commit_id_str);
1575 if (error != NULL)
1576 goto done;
1579 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1580 in_repo_path, repo);
1581 done:
1582 free(in_repo_path);
1583 free(repo_path);
1584 free(cwd);
1585 free(commit_id);
1586 if (worktree)
1587 got_worktree_close(worktree);
1588 if (repo) {
1589 const struct got_error *repo_error;
1590 repo_error = got_repo_close(repo);
1591 if (error == NULL)
1592 error = repo_error;
1594 return error;
1597 __dead static void
1598 usage_status(void)
1600 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1601 exit(1);
1604 static const struct got_error *
1605 print_status(void *arg, unsigned char status, const char *path,
1606 struct got_object_id *id)
1608 printf("%c %s\n", status, path);
1609 return NULL;
1612 static const struct got_error *
1613 cmd_status(int argc, char *argv[])
1615 const struct got_error *error = NULL;
1616 struct got_repository *repo = NULL;
1617 struct got_worktree *worktree = NULL;
1618 char *cwd = NULL, *path = NULL;
1619 int ch;
1621 while ((ch = getopt(argc, argv, "")) != -1) {
1622 switch (ch) {
1623 default:
1624 usage_status();
1625 /* NOTREACHED */
1629 argc -= optind;
1630 argv += optind;
1632 #ifndef PROFILE
1633 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1634 NULL) == -1)
1635 err(1, "pledge");
1636 #endif
1637 cwd = getcwd(NULL, 0);
1638 if (cwd == NULL) {
1639 error = got_error_from_errno();
1640 goto done;
1643 error = got_worktree_open(&worktree, cwd);
1644 if (error != NULL)
1645 goto done;
1647 if (argc == 0) {
1648 path = strdup("");
1649 if (path == NULL) {
1650 error = got_error_from_errno();
1651 goto done;
1653 } else if (argc == 1) {
1654 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1655 if (error)
1656 goto done;
1657 } else
1658 usage_status();
1660 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1661 if (error != NULL)
1662 goto done;
1664 error = apply_unveil(got_repo_get_path(repo), 1,
1665 got_worktree_get_root_path(worktree));
1666 if (error)
1667 goto done;
1669 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1670 check_cancelled, NULL);
1671 done:
1672 free(cwd);
1673 free(path);
1674 return error;
1677 __dead static void
1678 usage_ref(void)
1680 fprintf(stderr,
1681 "usage: %s ref [-r repository] -l | -d name | name object\n",
1682 getprogname());
1683 exit(1);
1686 static const struct got_error *
1687 list_refs(struct got_repository *repo)
1689 static const struct got_error *err = NULL;
1690 struct got_reflist_head refs;
1691 struct got_reflist_entry *re;
1693 SIMPLEQ_INIT(&refs);
1694 err = got_ref_list(&refs, repo);
1695 if (err)
1696 return err;
1698 SIMPLEQ_FOREACH(re, &refs, entry) {
1699 char *refstr;
1700 refstr = got_ref_to_str(re->ref);
1701 if (refstr == NULL)
1702 return got_error_from_errno();
1703 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1704 free(refstr);
1707 got_ref_list_free(&refs);
1708 return NULL;
1711 static const struct got_error *
1712 delete_ref(struct got_repository *repo, const char *refname)
1714 const struct got_error *err = NULL;
1715 struct got_reference *ref;
1717 err = got_ref_open(&ref, repo, refname);
1718 if (err)
1719 return err;
1721 err = got_ref_delete(ref, repo);
1722 got_ref_close(ref);
1723 return err;
1726 static const struct got_error *
1727 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1729 const struct got_error *err = NULL;
1730 struct got_object_id *id;
1731 struct got_reference *ref = NULL;
1733 err = got_object_resolve_id_str(&id, repo, id_str);
1734 if (err)
1735 return err;
1737 err = got_ref_alloc(&ref, refname, id);
1738 if (err)
1739 goto done;
1741 err = got_ref_write(ref, repo);
1742 done:
1743 if (ref)
1744 got_ref_close(ref);
1745 free(id);
1746 return err;
1749 static const struct got_error *
1750 cmd_ref(int argc, char *argv[])
1752 const struct got_error *error = NULL;
1753 struct got_repository *repo = NULL;
1754 struct got_worktree *worktree = NULL;
1755 char *cwd = NULL, *repo_path = NULL;
1756 int ch, do_list = 0;
1757 const char *delref = NULL;
1759 /* TODO: Add -s option for adding symbolic references. */
1760 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1761 switch (ch) {
1762 case 'd':
1763 delref = optarg;
1764 break;
1765 case 'r':
1766 repo_path = realpath(optarg, NULL);
1767 if (repo_path == NULL)
1768 err(1, "-r option");
1769 break;
1770 case 'l':
1771 do_list = 1;
1772 break;
1773 default:
1774 usage_ref();
1775 /* NOTREACHED */
1779 if (do_list && delref)
1780 errx(1, "-l and -d options are mutually exclusive\n");
1782 argc -= optind;
1783 argv += optind;
1785 if (do_list || delref) {
1786 if (argc > 0)
1787 usage_ref();
1788 } else if (argc != 2)
1789 usage_ref();
1791 #ifndef PROFILE
1792 if (do_list) {
1793 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1794 NULL) == -1)
1795 err(1, "pledge");
1796 } else {
1797 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1798 "sendfd unveil", NULL) == -1)
1799 err(1, "pledge");
1801 #endif
1802 cwd = getcwd(NULL, 0);
1803 if (cwd == NULL) {
1804 error = got_error_from_errno();
1805 goto done;
1808 if (repo_path == NULL) {
1809 error = got_worktree_open(&worktree, cwd);
1810 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1811 goto done;
1812 else
1813 error = NULL;
1814 if (worktree) {
1815 repo_path =
1816 strdup(got_worktree_get_repo_path(worktree));
1817 if (repo_path == NULL)
1818 error = got_error_from_errno();
1819 if (error)
1820 goto done;
1821 } else {
1822 repo_path = strdup(cwd);
1823 if (repo_path == NULL) {
1824 error = got_error_from_errno();
1825 goto done;
1830 error = got_repo_open(&repo, repo_path);
1831 if (error != NULL)
1832 goto done;
1834 error = apply_unveil(got_repo_get_path(repo), do_list,
1835 worktree ? got_worktree_get_root_path(worktree) : NULL);
1836 if (error)
1837 goto done;
1839 if (do_list)
1840 error = list_refs(repo);
1841 else if (delref)
1842 error = delete_ref(repo, delref);
1843 else
1844 error = add_ref(repo, argv[0], argv[1]);
1845 done:
1846 if (repo)
1847 got_repo_close(repo);
1848 if (worktree)
1849 got_worktree_close(worktree);
1850 free(cwd);
1851 free(repo_path);
1852 return error;
1855 __dead static void
1856 usage_add(void)
1858 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1859 exit(1);
1862 static const struct got_error *
1863 cmd_add(int argc, char *argv[])
1865 const struct got_error *error = NULL;
1866 struct got_repository *repo = NULL;
1867 struct got_worktree *worktree = NULL;
1868 char *cwd = NULL, *path = NULL, *relpath = NULL;
1869 int ch;
1871 while ((ch = getopt(argc, argv, "")) != -1) {
1872 switch (ch) {
1873 default:
1874 usage_add();
1875 /* NOTREACHED */
1879 argc -= optind;
1880 argv += optind;
1882 if (argc != 1)
1883 usage_add();
1885 path = realpath(argv[0], NULL);
1886 if (path == NULL) {
1887 error = got_error_from_errno();
1888 goto done;
1891 cwd = getcwd(NULL, 0);
1892 if (cwd == NULL) {
1893 error = got_error_from_errno();
1894 goto done;
1896 error = got_worktree_open(&worktree, cwd);
1897 if (error)
1898 goto done;
1900 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1901 if (error != NULL)
1902 goto done;
1904 error = apply_unveil(got_repo_get_path(repo), 1,
1905 got_worktree_get_root_path(worktree));
1906 if (error)
1907 goto done;
1909 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1910 repo);
1911 if (error)
1912 goto done;
1913 done:
1914 if (repo)
1915 got_repo_close(repo);
1916 if (worktree)
1917 got_worktree_close(worktree);
1918 free(path);
1919 free(relpath);
1920 free(cwd);
1921 return error;
1924 __dead static void
1925 usage_rm(void)
1927 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1928 exit(1);
1931 static const struct got_error *
1932 cmd_rm(int argc, char *argv[])
1934 const struct got_error *error = NULL;
1935 struct got_worktree *worktree = NULL;
1936 struct got_repository *repo = NULL;
1937 char *cwd = NULL, *path = NULL;
1938 int ch, delete_local_mods = 0;
1940 while ((ch = getopt(argc, argv, "f")) != -1) {
1941 switch (ch) {
1942 case 'f':
1943 delete_local_mods = 1;
1944 break;
1945 default:
1946 usage_add();
1947 /* NOTREACHED */
1951 argc -= optind;
1952 argv += optind;
1954 if (argc != 1)
1955 usage_rm();
1957 path = realpath(argv[0], NULL);
1958 if (path == NULL) {
1959 error = got_error_from_errno();
1960 goto done;
1963 cwd = getcwd(NULL, 0);
1964 if (cwd == NULL) {
1965 error = got_error_from_errno();
1966 goto done;
1968 error = got_worktree_open(&worktree, cwd);
1969 if (error)
1970 goto done;
1972 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1973 if (error != NULL)
1974 goto done;
1976 error = apply_unveil(got_repo_get_path(repo), 1,
1977 got_worktree_get_root_path(worktree));
1978 if (error)
1979 goto done;
1981 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1982 print_status, NULL, repo);
1983 if (error)
1984 goto done;
1985 done:
1986 if (repo)
1987 got_repo_close(repo);
1988 if (worktree)
1989 got_worktree_close(worktree);
1990 free(path);
1991 free(cwd);
1992 return error;
1995 __dead static void
1996 usage_revert(void)
1998 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
1999 exit(1);
2002 static void
2003 revert_progress(void *arg, unsigned char status, const char *path)
2005 while (path[0] == '/')
2006 path++;
2007 printf("%c %s\n", status, path);
2010 static const struct got_error *
2011 cmd_revert(int argc, char *argv[])
2013 const struct got_error *error = NULL;
2014 struct got_worktree *worktree = NULL;
2015 struct got_repository *repo = NULL;
2016 char *cwd = NULL, *path = NULL;
2017 int ch;
2019 while ((ch = getopt(argc, argv, "")) != -1) {
2020 switch (ch) {
2021 default:
2022 usage_revert();
2023 /* NOTREACHED */
2027 argc -= optind;
2028 argv += optind;
2030 if (argc != 1)
2031 usage_revert();
2033 path = realpath(argv[0], NULL);
2034 if (path == NULL) {
2035 error = got_error_from_errno();
2036 goto done;
2039 cwd = getcwd(NULL, 0);
2040 if (cwd == NULL) {
2041 error = got_error_from_errno();
2042 goto done;
2044 error = got_worktree_open(&worktree, cwd);
2045 if (error)
2046 goto done;
2048 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2049 if (error != NULL)
2050 goto done;
2052 error = apply_unveil(got_repo_get_path(repo), 1,
2053 got_worktree_get_root_path(worktree));
2054 if (error)
2055 goto done;
2057 error = got_worktree_revert(worktree, path,
2058 revert_progress, NULL, repo);
2059 if (error)
2060 goto done;
2061 done:
2062 if (repo)
2063 got_repo_close(repo);
2064 if (worktree)
2065 got_worktree_close(worktree);
2066 free(path);
2067 free(cwd);
2068 return error;