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"
43 #include "got_path.h"
45 #ifndef nitems
46 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 #endif
49 static volatile sig_atomic_t sigint_received;
50 static volatile sig_atomic_t sigpipe_received;
52 static void
53 catch_sigint(int signo)
54 {
55 sigint_received = 1;
56 }
58 static void
59 catch_sigpipe(int signo)
60 {
61 sigpipe_received = 1;
62 }
65 struct cmd {
66 const char *cmd_name;
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *cmd_descr;
70 };
72 __dead static void usage(void);
73 __dead static void usage_checkout(void);
74 __dead static void usage_update(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
79 __dead static void usage_status(void);
80 __dead static void usage_ref(void);
81 __dead static void usage_add(void);
82 __dead static void usage_rm(void);
83 __dead static void usage_revert(void);
84 __dead static void usage_commit(void);
86 static const struct got_error* cmd_checkout(int, char *[]);
87 static const struct got_error* cmd_update(int, char *[]);
88 static const struct got_error* cmd_log(int, char *[]);
89 static const struct got_error* cmd_diff(int, char *[]);
90 static const struct got_error* cmd_blame(int, char *[]);
91 static const struct got_error* cmd_tree(int, char *[]);
92 static const struct got_error* cmd_status(int, char *[]);
93 static const struct got_error* cmd_ref(int, char *[]);
94 static const struct got_error* cmd_add(int, char *[]);
95 static const struct got_error* cmd_rm(int, char *[]);
96 static const struct got_error* cmd_revert(int, char *[]);
97 static const struct got_error* cmd_commit(int, char *[]);
99 static struct cmd got_commands[] = {
100 { "checkout", cmd_checkout, usage_checkout,
101 "check out a new work tree from a repository" },
102 { "update", cmd_update, usage_update,
103 "update a work tree to a different commit" },
104 { "log", cmd_log, usage_log,
105 "show repository history" },
106 { "diff", cmd_diff, usage_diff,
107 "compare files and directories" },
108 { "blame", cmd_blame, usage_blame,
109 "show when lines in a file were changed" },
110 { "tree", cmd_tree, usage_tree,
111 "list files and directories in repository" },
112 { "status", cmd_status, usage_status,
113 "show modification status of files" },
114 { "ref", cmd_ref, usage_ref,
115 "manage references in repository" },
116 { "add", cmd_add, usage_add,
117 "add a new file to version control" },
118 { "rm", cmd_rm, usage_rm,
119 "remove a versioned file" },
120 { "revert", cmd_revert, usage_revert,
121 "revert uncommitted changes" },
122 { "commit", cmd_commit, usage_commit,
123 "write changes from work tree to repository" },
124 };
126 int
127 main(int argc, char *argv[])
129 struct cmd *cmd;
130 unsigned int i;
131 int ch;
132 int hflag = 0;
134 setlocale(LC_CTYPE, "");
136 while ((ch = getopt(argc, argv, "h")) != -1) {
137 switch (ch) {
138 case 'h':
139 hflag = 1;
140 break;
141 default:
142 usage();
143 /* NOTREACHED */
147 argc -= optind;
148 argv += optind;
149 optind = 0;
151 if (argc <= 0)
152 usage();
154 signal(SIGINT, catch_sigint);
155 signal(SIGPIPE, catch_sigpipe);
157 for (i = 0; i < nitems(got_commands); i++) {
158 const struct got_error *error;
160 cmd = &got_commands[i];
162 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
163 continue;
165 if (hflag)
166 got_commands[i].cmd_usage();
168 error = got_commands[i].cmd_main(argc, argv);
169 if (error && !(sigint_received || sigpipe_received)) {
170 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
171 return 1;
174 return 0;
177 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
178 return 1;
181 __dead static void
182 usage(void)
184 int i;
186 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
187 "Available commands:\n", getprogname());
188 for (i = 0; i < nitems(got_commands); i++) {
189 struct cmd *cmd = &got_commands[i];
190 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
192 exit(1);
195 static const struct got_error *
196 apply_unveil(const char *repo_path, int repo_read_only,
197 const char *worktree_path, int create_worktree)
199 const struct got_error *error;
201 if (create_worktree) {
202 /* Pre-create work tree path to avoid unveiling its parents. */
203 error = got_path_mkdir(worktree_path);
204 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
205 return error;
208 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
209 return got_error_from_errno();
211 if (worktree_path && unveil(worktree_path, "rwc") != 0)
212 return got_error_from_errno();
214 if (unveil("/tmp", "rwc") != 0)
215 return got_error_from_errno();
217 error = got_privsep_unveil_exec_helpers();
218 if (error != NULL)
219 return error;
221 if (unveil(NULL, NULL) != 0)
222 return got_error_from_errno();
224 return NULL;
227 __dead static void
228 usage_checkout(void)
230 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
231 "[worktree-path]\n", getprogname());
232 exit(1);
235 static void
236 checkout_progress(void *arg, unsigned char status, const char *path)
238 char *worktree_path = arg;
240 while (path[0] == '/')
241 path++;
243 printf("%c %s/%s\n", status, worktree_path, path);
246 static const struct got_error *
247 check_cancelled(void *arg)
249 if (sigint_received || sigpipe_received)
250 return got_error(GOT_ERR_CANCELLED);
251 return NULL;
254 static const struct got_error *
255 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
256 struct got_repository *repo)
258 const struct got_error *err;
259 struct got_reference *head_ref = NULL;
260 struct got_object_id *head_commit_id = NULL;
261 struct got_commit_graph *graph = NULL;
263 err = got_ref_open(&head_ref, repo,
264 got_worktree_get_head_ref_name(worktree));
265 if (err)
266 return err;
268 /* TODO: Check the reflog. The head ref may have been rebased. */
269 err = got_ref_resolve(&head_commit_id, repo, head_ref);
270 if (err)
271 goto done;
273 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
274 if (err)
275 goto done;
277 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
278 if (err)
279 goto done;
280 while (1) {
281 struct got_object_id *id;
283 if (sigint_received || sigpipe_received)
284 break;
286 err = got_commit_graph_iter_next(&id, graph);
287 if (err) {
288 if (err->code == GOT_ERR_ITER_COMPLETED) {
289 err = got_error(GOT_ERR_ANCESTRY);
290 break;
292 if (err->code != GOT_ERR_ITER_NEED_MORE)
293 break;
294 err = got_commit_graph_fetch_commits(graph, 1, repo);
295 if (err)
296 break;
297 else
298 continue;
300 if (id == NULL)
301 break;
302 if (got_object_id_cmp(id, commit_id) == 0)
303 break;
305 done:
306 if (head_ref)
307 got_ref_close(head_ref);
308 if (graph)
309 got_commit_graph_close(graph);
310 return err;
314 static const struct got_error *
315 cmd_checkout(int argc, char *argv[])
317 const struct got_error *error = NULL;
318 struct got_repository *repo = NULL;
319 struct got_reference *head_ref = NULL;
320 struct got_worktree *worktree = NULL;
321 char *repo_path = NULL;
322 char *worktree_path = NULL;
323 const char *path_prefix = "";
324 char *commit_id_str = NULL;
325 int ch, same_path_prefix;
327 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
328 switch (ch) {
329 case 'c':
330 commit_id_str = strdup(optarg);
331 if (commit_id_str == NULL)
332 return got_error_from_errno();
333 break;
334 case 'p':
335 path_prefix = optarg;
336 break;
337 default:
338 usage_checkout();
339 /* NOTREACHED */
343 argc -= optind;
344 argv += optind;
346 #ifndef PROFILE
347 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
348 "unveil", NULL) == -1)
349 err(1, "pledge");
350 #endif
351 if (argc == 1) {
352 char *cwd, *base, *dotgit;
353 repo_path = realpath(argv[0], NULL);
354 if (repo_path == NULL)
355 return got_error_from_errno();
356 cwd = getcwd(NULL, 0);
357 if (cwd == NULL) {
358 error = got_error_from_errno();
359 goto done;
361 if (path_prefix[0])
362 base = basename(path_prefix);
363 else
364 base = basename(repo_path);
365 if (base == NULL) {
366 error = got_error_from_errno();
367 goto done;
369 dotgit = strstr(base, ".git");
370 if (dotgit)
371 *dotgit = '\0';
372 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
373 error = got_error_from_errno();
374 free(cwd);
375 goto done;
377 free(cwd);
378 } else if (argc == 2) {
379 repo_path = realpath(argv[0], NULL);
380 if (repo_path == NULL) {
381 error = got_error_from_errno();
382 goto done;
384 worktree_path = realpath(argv[1], NULL);
385 if (worktree_path == NULL) {
386 error = got_error_from_errno();
387 goto done;
389 } else
390 usage_checkout();
392 got_path_strip_trailing_slashes(repo_path);
393 got_path_strip_trailing_slashes(worktree_path);
395 error = got_repo_open(&repo, repo_path);
396 if (error != NULL)
397 goto done;
399 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
400 if (error)
401 goto done;
403 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
404 if (error != NULL)
405 goto done;
407 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
408 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
409 goto done;
411 error = got_worktree_open(&worktree, worktree_path);
412 if (error != NULL)
413 goto done;
415 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
416 path_prefix);
417 if (error != NULL)
418 goto done;
419 if (!same_path_prefix) {
420 error = got_error(GOT_ERR_PATH_PREFIX);
421 goto done;
424 if (commit_id_str) {
425 struct got_object_id *commit_id;
426 error = got_object_resolve_id_str(&commit_id, repo,
427 commit_id_str);
428 if (error != NULL)
429 goto done;
430 error = check_ancestry(worktree, commit_id, repo);
431 if (error != NULL) {
432 free(commit_id);
433 goto done;
435 error = got_worktree_set_base_commit_id(worktree, repo,
436 commit_id);
437 free(commit_id);
438 if (error)
439 goto done;
442 error = got_worktree_checkout_files(worktree, "", repo,
443 checkout_progress, worktree_path, check_cancelled, NULL);
444 if (error != NULL)
445 goto done;
447 printf("Now shut up and hack\n");
449 done:
450 free(commit_id_str);
451 free(repo_path);
452 free(worktree_path);
453 return error;
456 __dead static void
457 usage_update(void)
459 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
460 getprogname());
461 exit(1);
464 static void
465 update_progress(void *arg, unsigned char status, const char *path)
467 int *did_something = arg;
469 if (status == GOT_STATUS_EXISTS)
470 return;
472 *did_something = 1;
473 while (path[0] == '/')
474 path++;
475 printf("%c %s\n", status, path);
478 static const struct got_error *
479 cmd_update(int argc, char *argv[])
481 const struct got_error *error = NULL;
482 struct got_repository *repo = NULL;
483 struct got_worktree *worktree = NULL;
484 char *worktree_path = NULL, *path = NULL;
485 struct got_object_id *commit_id = NULL;
486 char *commit_id_str = NULL;
487 int ch, did_something = 0;
489 while ((ch = getopt(argc, argv, "c:")) != -1) {
490 switch (ch) {
491 case 'c':
492 commit_id_str = strdup(optarg);
493 if (commit_id_str == NULL)
494 return got_error_from_errno();
495 break;
496 default:
497 usage_update();
498 /* NOTREACHED */
502 argc -= optind;
503 argv += optind;
505 #ifndef PROFILE
506 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
507 "unveil", NULL) == -1)
508 err(1, "pledge");
509 #endif
510 worktree_path = getcwd(NULL, 0);
511 if (worktree_path == NULL) {
512 error = got_error_from_errno();
513 goto done;
515 error = got_worktree_open(&worktree, worktree_path);
516 if (error)
517 goto done;
519 if (argc == 0) {
520 path = strdup("");
521 if (path == NULL) {
522 error = got_error_from_errno();
523 goto done;
525 } else if (argc == 1) {
526 error = got_worktree_resolve_path(&path, worktree, argv[0]);
527 if (error)
528 goto done;
529 } else
530 usage_update();
532 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
533 if (error != NULL)
534 goto done;
536 error = apply_unveil(got_repo_get_path(repo), 0,
537 got_worktree_get_root_path(worktree), 0);
538 if (error)
539 goto done;
541 if (commit_id_str == NULL) {
542 struct got_reference *head_ref;
543 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
544 if (error != NULL)
545 goto done;
546 error = got_ref_resolve(&commit_id, repo, head_ref);
547 if (error != NULL)
548 goto done;
549 error = got_object_id_str(&commit_id_str, commit_id);
550 if (error != NULL)
551 goto done;
552 } else {
553 error = got_object_resolve_id_str(&commit_id, repo,
554 commit_id_str);
555 if (error != NULL)
556 goto done;
559 error = check_ancestry(worktree, commit_id, repo);
560 if (error != NULL)
561 goto done;
563 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
564 commit_id) != 0) {
565 error = got_worktree_set_base_commit_id(worktree, repo,
566 commit_id);
567 if (error)
568 goto done;
571 error = got_worktree_checkout_files(worktree, path, repo,
572 update_progress, &did_something, check_cancelled, NULL);
573 if (error != NULL)
574 goto done;
576 if (did_something)
577 printf("Updated to commit %s\n", commit_id_str);
578 else
579 printf("Already up-to-date\n");
580 done:
581 free(worktree_path);
582 free(path);
583 free(commit_id);
584 free(commit_id_str);
585 return error;
588 static const struct got_error *
589 print_patch(struct got_commit_object *commit, struct got_object_id *id,
590 int diff_context, struct got_repository *repo)
592 const struct got_error *err = NULL;
593 struct got_tree_object *tree1 = NULL, *tree2;
594 struct got_object_qid *qid;
595 char *id_str1 = NULL, *id_str2;
597 err = got_object_open_as_tree(&tree2, repo,
598 got_object_commit_get_tree_id(commit));
599 if (err)
600 return err;
602 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
603 if (qid != NULL) {
604 struct got_commit_object *pcommit;
606 err = got_object_open_as_commit(&pcommit, repo, qid->id);
607 if (err)
608 return err;
610 err = got_object_open_as_tree(&tree1, repo,
611 got_object_commit_get_tree_id(pcommit));
612 got_object_commit_close(pcommit);
613 if (err)
614 return err;
616 err = got_object_id_str(&id_str1, qid->id);
617 if (err)
618 return err;
621 err = got_object_id_str(&id_str2, id);
622 if (err)
623 goto done;
625 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
626 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
627 done:
628 if (tree1)
629 got_object_tree_close(tree1);
630 got_object_tree_close(tree2);
631 free(id_str1);
632 free(id_str2);
633 return err;
636 static char *
637 get_datestr(time_t *time, char *datebuf)
639 char *p, *s = ctime_r(time, datebuf);
640 p = strchr(s, '\n');
641 if (p)
642 *p = '\0';
643 return s;
646 static const struct got_error *
647 print_commit(struct got_commit_object *commit, struct got_object_id *id,
648 struct got_repository *repo, int show_patch, int diff_context,
649 struct got_reflist_head *refs)
651 const struct got_error *err = NULL;
652 char *id_str, *datestr, *logmsg0, *logmsg, *line;
653 char datebuf[26];
654 time_t committer_time;
655 const char *author, *committer;
656 char *refs_str = NULL;
657 struct got_reflist_entry *re;
659 SIMPLEQ_FOREACH(re, refs, entry) {
660 char *s;
661 const char *name;
662 if (got_object_id_cmp(re->id, id) != 0)
663 continue;
664 name = got_ref_get_name(re->ref);
665 if (strcmp(name, GOT_REF_HEAD) == 0)
666 continue;
667 if (strncmp(name, "refs/", 5) == 0)
668 name += 5;
669 if (strncmp(name, "got/", 4) == 0)
670 continue;
671 if (strncmp(name, "heads/", 6) == 0)
672 name += 6;
673 if (strncmp(name, "remotes/", 8) == 0)
674 name += 8;
675 s = refs_str;
676 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
677 name) == -1) {
678 err = got_error_from_errno();
679 free(s);
680 break;
682 free(s);
684 err = got_object_id_str(&id_str, id);
685 if (err)
686 return err;
688 printf("-----------------------------------------------\n");
689 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
690 refs_str ? refs_str : "", refs_str ? ")" : "");
691 free(id_str);
692 id_str = NULL;
693 free(refs_str);
694 refs_str = NULL;
695 printf("from: %s\n", got_object_commit_get_author(commit));
696 committer_time = got_object_commit_get_committer_time(commit);
697 datestr = get_datestr(&committer_time, datebuf);
698 printf("date: %s UTC\n", datestr);
699 author = got_object_commit_get_author(commit);
700 committer = got_object_commit_get_committer(commit);
701 if (strcmp(author, committer) != 0)
702 printf("via: %s\n", committer);
703 if (got_object_commit_get_nparents(commit) > 1) {
704 const struct got_object_id_queue *parent_ids;
705 struct got_object_qid *qid;
706 int n = 1;
707 parent_ids = got_object_commit_get_parent_ids(commit);
708 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
709 err = got_object_id_str(&id_str, qid->id);
710 if (err)
711 return err;
712 printf("parent %d: %s\n", n++, id_str);
713 free(id_str);
717 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
718 if (logmsg0 == NULL)
719 return got_error_from_errno();
721 logmsg = logmsg0;
722 do {
723 line = strsep(&logmsg, "\n");
724 if (line)
725 printf(" %s\n", line);
726 } while (line);
727 free(logmsg0);
729 if (show_patch) {
730 err = print_patch(commit, id, diff_context, repo);
731 if (err == 0)
732 printf("\n");
735 if (fflush(stdout) != 0 && err == NULL)
736 err = got_error_from_errno();
737 return err;
740 static const struct got_error *
741 print_commits(struct got_object_id *root_id, struct got_repository *repo,
742 char *path, int show_patch, int diff_context, int limit,
743 int first_parent_traversal, struct got_reflist_head *refs)
745 const struct got_error *err;
746 struct got_commit_graph *graph;
748 err = got_commit_graph_open(&graph, root_id, path,
749 first_parent_traversal, repo);
750 if (err)
751 return err;
752 err = got_commit_graph_iter_start(graph, root_id, repo);
753 if (err)
754 goto done;
755 while (1) {
756 struct got_commit_object *commit;
757 struct got_object_id *id;
759 if (sigint_received || sigpipe_received)
760 break;
762 err = got_commit_graph_iter_next(&id, graph);
763 if (err) {
764 if (err->code == GOT_ERR_ITER_COMPLETED) {
765 err = NULL;
766 break;
768 if (err->code != GOT_ERR_ITER_NEED_MORE)
769 break;
770 err = got_commit_graph_fetch_commits(graph, 1, repo);
771 if (err)
772 break;
773 else
774 continue;
776 if (id == NULL)
777 break;
779 err = got_object_open_as_commit(&commit, repo, id);
780 if (err)
781 break;
782 err = print_commit(commit, id, repo, show_patch, diff_context,
783 refs);
784 got_object_commit_close(commit);
785 if (err || (limit && --limit == 0))
786 break;
788 done:
789 got_commit_graph_close(graph);
790 return err;
793 __dead static void
794 usage_log(void)
796 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
797 "[-r repository-path] [path]\n", getprogname());
798 exit(1);
801 static const struct got_error *
802 cmd_log(int argc, char *argv[])
804 const struct got_error *error;
805 struct got_repository *repo = NULL;
806 struct got_worktree *worktree = NULL;
807 struct got_commit_object *commit = NULL;
808 struct got_object_id *id = NULL;
809 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
810 char *start_commit = NULL;
811 int diff_context = 3, ch;
812 int show_patch = 0, limit = 0, first_parent_traversal = 0;
813 const char *errstr;
814 struct got_reflist_head refs;
816 SIMPLEQ_INIT(&refs);
818 #ifndef PROFILE
819 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
820 NULL)
821 == -1)
822 err(1, "pledge");
823 #endif
825 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
826 switch (ch) {
827 case 'p':
828 show_patch = 1;
829 break;
830 case 'c':
831 start_commit = optarg;
832 break;
833 case 'C':
834 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
835 &errstr);
836 if (errstr != NULL)
837 err(1, "-C option %s", errstr);
838 break;
839 case 'l':
840 limit = strtonum(optarg, 1, INT_MAX, &errstr);
841 if (errstr != NULL)
842 err(1, "-l option %s", errstr);
843 break;
844 case 'f':
845 first_parent_traversal = 1;
846 break;
847 case 'r':
848 repo_path = realpath(optarg, NULL);
849 if (repo_path == NULL)
850 err(1, "-r option");
851 got_path_strip_trailing_slashes(repo_path);
852 break;
853 default:
854 usage_log();
855 /* NOTREACHED */
859 argc -= optind;
860 argv += optind;
862 cwd = getcwd(NULL, 0);
863 if (cwd == NULL) {
864 error = got_error_from_errno();
865 goto done;
868 error = got_worktree_open(&worktree, cwd);
869 if (error && error->code != GOT_ERR_NOT_WORKTREE)
870 goto done;
871 error = NULL;
873 if (argc == 0) {
874 path = strdup("");
875 if (path == NULL) {
876 error = got_error_from_errno();
877 goto done;
879 } else if (argc == 1) {
880 if (worktree) {
881 error = got_worktree_resolve_path(&path, worktree,
882 argv[0]);
883 if (error)
884 goto done;
885 } else {
886 path = strdup(argv[0]);
887 if (path == NULL) {
888 error = got_error_from_errno();
889 goto done;
892 } else
893 usage_log();
895 if (repo_path == NULL) {
896 repo_path = worktree ?
897 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
899 if (repo_path == NULL) {
900 error = got_error_from_errno();
901 goto done;
904 error = got_repo_open(&repo, repo_path);
905 if (error != NULL)
906 goto done;
908 error = apply_unveil(got_repo_get_path(repo), 1,
909 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
910 if (error)
911 goto done;
913 if (start_commit == NULL) {
914 struct got_reference *head_ref;
915 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
916 if (error != NULL)
917 return error;
918 error = got_ref_resolve(&id, repo, head_ref);
919 got_ref_close(head_ref);
920 if (error != NULL)
921 return error;
922 error = got_object_open_as_commit(&commit, repo, id);
923 } else {
924 struct got_reference *ref;
925 error = got_ref_open(&ref, repo, start_commit);
926 if (error == NULL) {
927 int obj_type;
928 error = got_ref_resolve(&id, repo, ref);
929 got_ref_close(ref);
930 if (error != NULL)
931 goto done;
932 error = got_object_get_type(&obj_type, repo, id);
933 if (error != NULL)
934 goto done;
935 if (obj_type == GOT_OBJ_TYPE_TAG) {
936 struct got_tag_object *tag;
937 error = got_object_open_as_tag(&tag, repo, id);
938 if (error != NULL)
939 goto done;
940 if (got_object_tag_get_object_type(tag) !=
941 GOT_OBJ_TYPE_COMMIT) {
942 got_object_tag_close(tag);
943 error = got_error(GOT_ERR_OBJ_TYPE);
944 goto done;
946 free(id);
947 id = got_object_id_dup(
948 got_object_tag_get_object_id(tag));
949 if (id == NULL)
950 error = got_error_from_errno();
951 got_object_tag_close(tag);
952 if (error)
953 goto done;
954 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
955 error = got_error(GOT_ERR_OBJ_TYPE);
956 goto done;
958 error = got_object_open_as_commit(&commit, repo, id);
959 if (error != NULL)
960 goto done;
962 if (commit == NULL) {
963 error = got_object_resolve_id_str(&id, repo,
964 start_commit);
965 if (error != NULL)
966 return error;
969 if (error != NULL)
970 goto done;
972 error = got_repo_map_path(&in_repo_path, repo, path, 1);
973 if (error != NULL)
974 goto done;
975 if (in_repo_path) {
976 free(path);
977 path = in_repo_path;
980 error = got_ref_list(&refs, repo);
981 if (error)
982 goto done;
984 error = print_commits(id, repo, path, show_patch,
985 diff_context, limit, first_parent_traversal, &refs);
986 done:
987 free(path);
988 free(repo_path);
989 free(cwd);
990 free(id);
991 if (worktree)
992 got_worktree_close(worktree);
993 if (repo) {
994 const struct got_error *repo_error;
995 repo_error = got_repo_close(repo);
996 if (error == NULL)
997 error = repo_error;
999 got_ref_list_free(&refs);
1000 return error;
1003 __dead static void
1004 usage_diff(void)
1006 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1007 "[object1 object2 | path]\n", getprogname());
1008 exit(1);
1011 struct print_diff_arg {
1012 struct got_repository *repo;
1013 struct got_worktree *worktree;
1014 int diff_context;
1015 const char *id_str;
1016 int header_shown;
1019 static const struct got_error *
1020 print_diff(void *arg, unsigned char status, const char *path,
1021 struct got_object_id *id)
1023 struct print_diff_arg *a = arg;
1024 const struct got_error *err = NULL;
1025 struct got_blob_object *blob1 = NULL;
1026 FILE *f2 = NULL;
1027 char *abspath = NULL;
1028 struct stat sb;
1030 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1031 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1032 return NULL;
1034 if (!a->header_shown) {
1035 printf("diff %s %s\n", a->id_str,
1036 got_worktree_get_root_path(a->worktree));
1037 a->header_shown = 1;
1040 if (status != GOT_STATUS_ADD) {
1041 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1042 if (err)
1043 goto done;
1047 if (status != GOT_STATUS_DELETE) {
1048 if (asprintf(&abspath, "%s/%s",
1049 got_worktree_get_root_path(a->worktree), path) == -1) {
1050 err = got_error_from_errno();
1051 goto done;
1054 f2 = fopen(abspath, "r");
1055 if (f2 == NULL) {
1056 err = got_error_from_errno();
1057 goto done;
1059 if (lstat(abspath, &sb) == -1) {
1060 err = got_error_from_errno();
1061 goto done;
1063 } else
1064 sb.st_size = 0;
1066 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1067 stdout);
1068 done:
1069 if (blob1)
1070 got_object_blob_close(blob1);
1071 if (f2 && fclose(f2) != 0 && err == NULL)
1072 err = got_error_from_errno();
1073 free(abspath);
1074 return err;
1077 static const struct got_error *
1078 cmd_diff(int argc, char *argv[])
1080 const struct got_error *error;
1081 struct got_repository *repo = NULL;
1082 struct got_worktree *worktree = NULL;
1083 char *cwd = NULL, *repo_path = NULL;
1084 struct got_object_id *id1 = NULL, *id2 = NULL;
1085 char *id_str1 = NULL, *id_str2 = NULL;
1086 int type1, type2;
1087 int diff_context = 3, ch;
1088 const char *errstr;
1089 char *path = NULL;
1091 #ifndef PROFILE
1092 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1093 NULL) == -1)
1094 err(1, "pledge");
1095 #endif
1097 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1098 switch (ch) {
1099 case 'C':
1100 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1101 if (errstr != NULL)
1102 err(1, "-C option %s", errstr);
1103 break;
1104 case 'r':
1105 repo_path = realpath(optarg, NULL);
1106 if (repo_path == NULL)
1107 err(1, "-r option");
1108 got_path_strip_trailing_slashes(repo_path);
1109 break;
1110 default:
1111 usage_diff();
1112 /* NOTREACHED */
1116 argc -= optind;
1117 argv += optind;
1119 cwd = getcwd(NULL, 0);
1120 if (cwd == NULL) {
1121 error = got_error_from_errno();
1122 goto done;
1124 error = got_worktree_open(&worktree, cwd);
1125 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1126 goto done;
1127 if (argc <= 1) {
1128 if (worktree == NULL) {
1129 error = got_error(GOT_ERR_NOT_WORKTREE);
1130 goto done;
1132 if (repo_path)
1133 errx(1,
1134 "-r option can't be used when diffing a work tree");
1135 repo_path = strdup(got_worktree_get_repo_path(worktree));
1136 if (repo_path == NULL) {
1137 error = got_error_from_errno();
1138 goto done;
1140 if (argc == 1) {
1141 error = got_worktree_resolve_path(&path, worktree,
1142 argv[0]);
1143 if (error)
1144 goto done;
1145 } else {
1146 path = strdup("");
1147 if (path == NULL) {
1148 error = got_error_from_errno();
1149 goto done;
1152 } else if (argc == 2) {
1153 id_str1 = argv[0];
1154 id_str2 = argv[1];
1155 } else
1156 usage_diff();
1158 if (repo_path == NULL) {
1159 repo_path = getcwd(NULL, 0);
1160 if (repo_path == NULL)
1161 return got_error_from_errno();
1164 error = got_repo_open(&repo, repo_path);
1165 free(repo_path);
1166 if (error != NULL)
1167 goto done;
1169 error = apply_unveil(got_repo_get_path(repo), 1,
1170 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1171 if (error)
1172 goto done;
1174 if (worktree) {
1175 struct print_diff_arg arg;
1176 char *id_str;
1177 error = got_object_id_str(&id_str,
1178 got_worktree_get_base_commit_id(worktree));
1179 if (error)
1180 goto done;
1181 arg.repo = repo;
1182 arg.worktree = worktree;
1183 arg.diff_context = diff_context;
1184 arg.id_str = id_str;
1185 arg.header_shown = 0;
1187 error = got_worktree_status(worktree, path, repo, print_diff,
1188 &arg, check_cancelled, NULL);
1189 free(id_str);
1190 goto done;
1193 error = got_object_resolve_id_str(&id1, repo, id_str1);
1194 if (error)
1195 goto done;
1197 error = got_object_resolve_id_str(&id2, repo, id_str2);
1198 if (error)
1199 goto done;
1201 error = got_object_get_type(&type1, repo, id1);
1202 if (error)
1203 goto done;
1205 error = got_object_get_type(&type2, repo, id2);
1206 if (error)
1207 goto done;
1209 if (type1 != type2) {
1210 error = got_error(GOT_ERR_OBJ_TYPE);
1211 goto done;
1214 switch (type1) {
1215 case GOT_OBJ_TYPE_BLOB:
1216 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1217 diff_context, repo, stdout);
1218 break;
1219 case GOT_OBJ_TYPE_TREE:
1220 error = got_diff_objects_as_trees(id1, id2, "", "",
1221 diff_context, repo, stdout);
1222 break;
1223 case GOT_OBJ_TYPE_COMMIT:
1224 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1225 id_str2);
1226 error = got_diff_objects_as_commits(id1, id2, diff_context,
1227 repo, stdout);
1228 break;
1229 default:
1230 error = got_error(GOT_ERR_OBJ_TYPE);
1233 done:
1234 free(id1);
1235 free(id2);
1236 free(path);
1237 if (worktree)
1238 got_worktree_close(worktree);
1239 if (repo) {
1240 const struct got_error *repo_error;
1241 repo_error = got_repo_close(repo);
1242 if (error == NULL)
1243 error = repo_error;
1245 return error;
1248 __dead static void
1249 usage_blame(void)
1251 fprintf(stderr,
1252 "usage: %s blame [-c commit] [-r repository-path] path\n",
1253 getprogname());
1254 exit(1);
1257 static const struct got_error *
1258 cmd_blame(int argc, char *argv[])
1260 const struct got_error *error;
1261 struct got_repository *repo = NULL;
1262 struct got_worktree *worktree = NULL;
1263 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1264 struct got_object_id *commit_id = NULL;
1265 char *commit_id_str = NULL;
1266 int ch;
1268 #ifndef PROFILE
1269 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1270 NULL) == -1)
1271 err(1, "pledge");
1272 #endif
1274 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1275 switch (ch) {
1276 case 'c':
1277 commit_id_str = optarg;
1278 break;
1279 case 'r':
1280 repo_path = realpath(optarg, NULL);
1281 if (repo_path == NULL)
1282 err(1, "-r option");
1283 got_path_strip_trailing_slashes(repo_path);
1284 break;
1285 default:
1286 usage_blame();
1287 /* NOTREACHED */
1291 argc -= optind;
1292 argv += optind;
1294 if (argc == 1)
1295 path = argv[0];
1296 else
1297 usage_blame();
1299 cwd = getcwd(NULL, 0);
1300 if (cwd == NULL) {
1301 error = got_error_from_errno();
1302 goto done;
1304 if (repo_path == NULL) {
1305 error = got_worktree_open(&worktree, cwd);
1306 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1307 goto done;
1308 else
1309 error = NULL;
1310 if (worktree) {
1311 repo_path =
1312 strdup(got_worktree_get_repo_path(worktree));
1313 if (repo_path == NULL)
1314 error = got_error_from_errno();
1315 if (error)
1316 goto done;
1317 } else {
1318 repo_path = strdup(cwd);
1319 if (repo_path == NULL) {
1320 error = got_error_from_errno();
1321 goto done;
1326 error = got_repo_open(&repo, repo_path);
1327 if (error != NULL)
1328 goto done;
1330 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1331 if (error)
1332 goto done;
1334 if (worktree) {
1335 const char *prefix = got_worktree_get_path_prefix(worktree);
1336 char *p, *worktree_subdir = cwd +
1337 strlen(got_worktree_get_root_path(worktree));
1338 if (asprintf(&p, "%s%s%s%s%s",
1339 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1340 worktree_subdir, worktree_subdir[0] ? "/" : "",
1341 path) == -1) {
1342 error = got_error_from_errno();
1343 goto done;
1345 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1346 free(p);
1347 } else {
1348 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1350 if (error)
1351 goto done;
1353 if (commit_id_str == NULL) {
1354 struct got_reference *head_ref;
1355 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1356 if (error != NULL)
1357 goto done;
1358 error = got_ref_resolve(&commit_id, repo, head_ref);
1359 got_ref_close(head_ref);
1360 if (error != NULL)
1361 goto done;
1362 } else {
1363 error = got_object_resolve_id_str(&commit_id, repo,
1364 commit_id_str);
1365 if (error != NULL)
1366 goto done;
1369 error = got_blame(in_repo_path, commit_id, repo, stdout);
1370 done:
1371 free(in_repo_path);
1372 free(repo_path);
1373 free(cwd);
1374 free(commit_id);
1375 if (worktree)
1376 got_worktree_close(worktree);
1377 if (repo) {
1378 const struct got_error *repo_error;
1379 repo_error = got_repo_close(repo);
1380 if (error == NULL)
1381 error = repo_error;
1383 return error;
1386 __dead static void
1387 usage_tree(void)
1389 fprintf(stderr,
1390 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1391 getprogname());
1392 exit(1);
1395 static void
1396 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1397 const char *root_path)
1399 int is_root_path = (strcmp(path, root_path) == 0);
1401 path += strlen(root_path);
1402 while (path[0] == '/')
1403 path++;
1405 printf("%s%s%s%s%s\n", id ? id : "", path,
1406 is_root_path ? "" : "/", te->name,
1407 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1410 static const struct got_error *
1411 print_tree(const char *path, struct got_object_id *commit_id,
1412 int show_ids, int recurse, const char *root_path,
1413 struct got_repository *repo)
1415 const struct got_error *err = NULL;
1416 struct got_object_id *tree_id = NULL;
1417 struct got_tree_object *tree = NULL;
1418 const struct got_tree_entries *entries;
1419 struct got_tree_entry *te;
1421 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1422 if (err)
1423 goto done;
1425 err = got_object_open_as_tree(&tree, repo, tree_id);
1426 if (err)
1427 goto done;
1428 entries = got_object_tree_get_entries(tree);
1429 te = SIMPLEQ_FIRST(&entries->head);
1430 while (te) {
1431 char *id = NULL;
1433 if (sigint_received || sigpipe_received)
1434 break;
1436 if (show_ids) {
1437 char *id_str;
1438 err = got_object_id_str(&id_str, te->id);
1439 if (err)
1440 goto done;
1441 if (asprintf(&id, "%s ", id_str) == -1) {
1442 err = got_error_from_errno();
1443 free(id_str);
1444 goto done;
1446 free(id_str);
1448 print_entry(te, id, path, root_path);
1449 free(id);
1451 if (recurse && S_ISDIR(te->mode)) {
1452 char *child_path;
1453 if (asprintf(&child_path, "%s%s%s", path,
1454 path[0] == '/' && path[1] == '\0' ? "" : "/",
1455 te->name) == -1) {
1456 err = got_error_from_errno();
1457 goto done;
1459 err = print_tree(child_path, commit_id, show_ids, 1,
1460 root_path, repo);
1461 free(child_path);
1462 if (err)
1463 goto done;
1466 te = SIMPLEQ_NEXT(te, entry);
1468 done:
1469 if (tree)
1470 got_object_tree_close(tree);
1471 free(tree_id);
1472 return err;
1475 static const struct got_error *
1476 cmd_tree(int argc, char *argv[])
1478 const struct got_error *error;
1479 struct got_repository *repo = NULL;
1480 struct got_worktree *worktree = NULL;
1481 const char *path;
1482 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1483 struct got_object_id *commit_id = NULL;
1484 char *commit_id_str = NULL;
1485 int show_ids = 0, recurse = 0;
1486 int ch;
1488 #ifndef PROFILE
1489 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1490 NULL) == -1)
1491 err(1, "pledge");
1492 #endif
1494 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1495 switch (ch) {
1496 case 'c':
1497 commit_id_str = optarg;
1498 break;
1499 case 'r':
1500 repo_path = realpath(optarg, NULL);
1501 if (repo_path == NULL)
1502 err(1, "-r option");
1503 got_path_strip_trailing_slashes(repo_path);
1504 break;
1505 case 'i':
1506 show_ids = 1;
1507 break;
1508 case 'R':
1509 recurse = 1;
1510 break;
1511 default:
1512 usage_tree();
1513 /* NOTREACHED */
1517 argc -= optind;
1518 argv += optind;
1520 if (argc == 1)
1521 path = argv[0];
1522 else if (argc > 1)
1523 usage_tree();
1524 else
1525 path = NULL;
1527 cwd = getcwd(NULL, 0);
1528 if (cwd == NULL) {
1529 error = got_error_from_errno();
1530 goto done;
1532 if (repo_path == NULL) {
1533 error = got_worktree_open(&worktree, cwd);
1534 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1535 goto done;
1536 else
1537 error = NULL;
1538 if (worktree) {
1539 repo_path =
1540 strdup(got_worktree_get_repo_path(worktree));
1541 if (repo_path == NULL)
1542 error = got_error_from_errno();
1543 if (error)
1544 goto done;
1545 } else {
1546 repo_path = strdup(cwd);
1547 if (repo_path == NULL) {
1548 error = got_error_from_errno();
1549 goto done;
1554 error = got_repo_open(&repo, repo_path);
1555 if (error != NULL)
1556 goto done;
1558 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1559 if (error)
1560 goto done;
1562 if (path == NULL) {
1563 if (worktree) {
1564 char *p, *worktree_subdir = cwd +
1565 strlen(got_worktree_get_root_path(worktree));
1566 if (asprintf(&p, "%s/%s",
1567 got_worktree_get_path_prefix(worktree),
1568 worktree_subdir) == -1) {
1569 error = got_error_from_errno();
1570 goto done;
1572 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1573 free(p);
1574 if (error)
1575 goto done;
1576 } else
1577 path = "/";
1579 if (in_repo_path == NULL) {
1580 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1581 if (error != NULL)
1582 goto done;
1585 if (commit_id_str == NULL) {
1586 struct got_reference *head_ref;
1587 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1588 if (error != NULL)
1589 goto done;
1590 error = got_ref_resolve(&commit_id, repo, head_ref);
1591 got_ref_close(head_ref);
1592 if (error != NULL)
1593 goto done;
1594 } else {
1595 error = got_object_resolve_id_str(&commit_id, repo,
1596 commit_id_str);
1597 if (error != NULL)
1598 goto done;
1601 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1602 in_repo_path, repo);
1603 done:
1604 free(in_repo_path);
1605 free(repo_path);
1606 free(cwd);
1607 free(commit_id);
1608 if (worktree)
1609 got_worktree_close(worktree);
1610 if (repo) {
1611 const struct got_error *repo_error;
1612 repo_error = got_repo_close(repo);
1613 if (error == NULL)
1614 error = repo_error;
1616 return error;
1619 __dead static void
1620 usage_status(void)
1622 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1623 exit(1);
1626 static const struct got_error *
1627 print_status(void *arg, unsigned char status, const char *path,
1628 struct got_object_id *id)
1630 printf("%c %s\n", status, path);
1631 return NULL;
1634 static const struct got_error *
1635 cmd_status(int argc, char *argv[])
1637 const struct got_error *error = NULL;
1638 struct got_repository *repo = NULL;
1639 struct got_worktree *worktree = NULL;
1640 char *cwd = NULL, *path = NULL;
1641 int ch;
1643 while ((ch = getopt(argc, argv, "")) != -1) {
1644 switch (ch) {
1645 default:
1646 usage_status();
1647 /* NOTREACHED */
1651 argc -= optind;
1652 argv += optind;
1654 #ifndef PROFILE
1655 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1656 NULL) == -1)
1657 err(1, "pledge");
1658 #endif
1659 cwd = getcwd(NULL, 0);
1660 if (cwd == NULL) {
1661 error = got_error_from_errno();
1662 goto done;
1665 error = got_worktree_open(&worktree, cwd);
1666 if (error != NULL)
1667 goto done;
1669 if (argc == 0) {
1670 path = strdup("");
1671 if (path == NULL) {
1672 error = got_error_from_errno();
1673 goto done;
1675 } else if (argc == 1) {
1676 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1677 if (error)
1678 goto done;
1679 } else
1680 usage_status();
1682 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1683 if (error != NULL)
1684 goto done;
1686 error = apply_unveil(got_repo_get_path(repo), 1,
1687 got_worktree_get_root_path(worktree), 0);
1688 if (error)
1689 goto done;
1691 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1692 check_cancelled, NULL);
1693 done:
1694 free(cwd);
1695 free(path);
1696 return error;
1699 __dead static void
1700 usage_ref(void)
1702 fprintf(stderr,
1703 "usage: %s ref [-r repository] -l | -d name | name object\n",
1704 getprogname());
1705 exit(1);
1708 static const struct got_error *
1709 list_refs(struct got_repository *repo)
1711 static const struct got_error *err = NULL;
1712 struct got_reflist_head refs;
1713 struct got_reflist_entry *re;
1715 SIMPLEQ_INIT(&refs);
1716 err = got_ref_list(&refs, repo);
1717 if (err)
1718 return err;
1720 SIMPLEQ_FOREACH(re, &refs, entry) {
1721 char *refstr;
1722 refstr = got_ref_to_str(re->ref);
1723 if (refstr == NULL)
1724 return got_error_from_errno();
1725 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1726 free(refstr);
1729 got_ref_list_free(&refs);
1730 return NULL;
1733 static const struct got_error *
1734 delete_ref(struct got_repository *repo, const char *refname)
1736 const struct got_error *err = NULL;
1737 struct got_reference *ref;
1739 err = got_ref_open(&ref, repo, refname);
1740 if (err)
1741 return err;
1743 err = got_ref_delete(ref, repo);
1744 got_ref_close(ref);
1745 return err;
1748 static const struct got_error *
1749 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1751 const struct got_error *err = NULL;
1752 struct got_object_id *id;
1753 struct got_reference *ref = NULL;
1755 err = got_object_resolve_id_str(&id, repo, id_str);
1756 if (err)
1757 return err;
1759 err = got_ref_alloc(&ref, refname, id);
1760 if (err)
1761 goto done;
1763 err = got_ref_write(ref, repo);
1764 done:
1765 if (ref)
1766 got_ref_close(ref);
1767 free(id);
1768 return err;
1771 static const struct got_error *
1772 cmd_ref(int argc, char *argv[])
1774 const struct got_error *error = NULL;
1775 struct got_repository *repo = NULL;
1776 struct got_worktree *worktree = NULL;
1777 char *cwd = NULL, *repo_path = NULL;
1778 int ch, do_list = 0;
1779 const char *delref = NULL;
1781 /* TODO: Add -s option for adding symbolic references. */
1782 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1783 switch (ch) {
1784 case 'd':
1785 delref = optarg;
1786 break;
1787 case 'r':
1788 repo_path = realpath(optarg, NULL);
1789 if (repo_path == NULL)
1790 err(1, "-r option");
1791 got_path_strip_trailing_slashes(repo_path);
1792 break;
1793 case 'l':
1794 do_list = 1;
1795 break;
1796 default:
1797 usage_ref();
1798 /* NOTREACHED */
1802 if (do_list && delref)
1803 errx(1, "-l and -d options are mutually exclusive\n");
1805 argc -= optind;
1806 argv += optind;
1808 if (do_list || delref) {
1809 if (argc > 0)
1810 usage_ref();
1811 } else if (argc != 2)
1812 usage_ref();
1814 #ifndef PROFILE
1815 if (do_list) {
1816 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1817 NULL) == -1)
1818 err(1, "pledge");
1819 } else {
1820 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1821 "sendfd unveil", NULL) == -1)
1822 err(1, "pledge");
1824 #endif
1825 cwd = getcwd(NULL, 0);
1826 if (cwd == NULL) {
1827 error = got_error_from_errno();
1828 goto done;
1831 if (repo_path == NULL) {
1832 error = got_worktree_open(&worktree, cwd);
1833 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1834 goto done;
1835 else
1836 error = NULL;
1837 if (worktree) {
1838 repo_path =
1839 strdup(got_worktree_get_repo_path(worktree));
1840 if (repo_path == NULL)
1841 error = got_error_from_errno();
1842 if (error)
1843 goto done;
1844 } else {
1845 repo_path = strdup(cwd);
1846 if (repo_path == NULL) {
1847 error = got_error_from_errno();
1848 goto done;
1853 error = got_repo_open(&repo, repo_path);
1854 if (error != NULL)
1855 goto done;
1857 error = apply_unveil(got_repo_get_path(repo), do_list,
1858 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1859 if (error)
1860 goto done;
1862 if (do_list)
1863 error = list_refs(repo);
1864 else if (delref)
1865 error = delete_ref(repo, delref);
1866 else
1867 error = add_ref(repo, argv[0], argv[1]);
1868 done:
1869 if (repo)
1870 got_repo_close(repo);
1871 if (worktree)
1872 got_worktree_close(worktree);
1873 free(cwd);
1874 free(repo_path);
1875 return error;
1878 __dead static void
1879 usage_add(void)
1881 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1882 exit(1);
1885 static const struct got_error *
1886 cmd_add(int argc, char *argv[])
1888 const struct got_error *error = NULL;
1889 struct got_repository *repo = NULL;
1890 struct got_worktree *worktree = NULL;
1891 char *cwd = NULL, *path = NULL, *relpath = NULL;
1892 int ch;
1894 while ((ch = getopt(argc, argv, "")) != -1) {
1895 switch (ch) {
1896 default:
1897 usage_add();
1898 /* NOTREACHED */
1902 argc -= optind;
1903 argv += optind;
1905 if (argc != 1)
1906 usage_add();
1908 path = realpath(argv[0], NULL);
1909 if (path == NULL) {
1910 error = got_error_from_errno();
1911 goto done;
1913 got_path_strip_trailing_slashes(path);
1915 cwd = getcwd(NULL, 0);
1916 if (cwd == NULL) {
1917 error = got_error_from_errno();
1918 goto done;
1920 error = got_worktree_open(&worktree, cwd);
1921 if (error)
1922 goto done;
1924 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1925 if (error != NULL)
1926 goto done;
1928 error = apply_unveil(got_repo_get_path(repo), 1,
1929 got_worktree_get_root_path(worktree), 0);
1930 if (error)
1931 goto done;
1933 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1934 repo);
1935 if (error)
1936 goto done;
1937 done:
1938 if (repo)
1939 got_repo_close(repo);
1940 if (worktree)
1941 got_worktree_close(worktree);
1942 free(path);
1943 free(relpath);
1944 free(cwd);
1945 return error;
1948 __dead static void
1949 usage_rm(void)
1951 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1952 exit(1);
1955 static const struct got_error *
1956 cmd_rm(int argc, char *argv[])
1958 const struct got_error *error = NULL;
1959 struct got_worktree *worktree = NULL;
1960 struct got_repository *repo = NULL;
1961 char *cwd = NULL, *path = NULL;
1962 int ch, delete_local_mods = 0;
1964 while ((ch = getopt(argc, argv, "f")) != -1) {
1965 switch (ch) {
1966 case 'f':
1967 delete_local_mods = 1;
1968 break;
1969 default:
1970 usage_add();
1971 /* NOTREACHED */
1975 argc -= optind;
1976 argv += optind;
1978 if (argc != 1)
1979 usage_rm();
1981 path = realpath(argv[0], NULL);
1982 if (path == NULL) {
1983 error = got_error_from_errno();
1984 goto done;
1986 got_path_strip_trailing_slashes(path);
1988 cwd = getcwd(NULL, 0);
1989 if (cwd == NULL) {
1990 error = got_error_from_errno();
1991 goto done;
1993 error = got_worktree_open(&worktree, cwd);
1994 if (error)
1995 goto done;
1997 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1998 if (error != NULL)
1999 goto done;
2001 error = apply_unveil(got_repo_get_path(repo), 1,
2002 got_worktree_get_root_path(worktree), 0);
2003 if (error)
2004 goto done;
2006 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2007 print_status, NULL, repo);
2008 if (error)
2009 goto done;
2010 done:
2011 if (repo)
2012 got_repo_close(repo);
2013 if (worktree)
2014 got_worktree_close(worktree);
2015 free(path);
2016 free(cwd);
2017 return error;
2020 __dead static void
2021 usage_revert(void)
2023 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2024 exit(1);
2027 static void
2028 revert_progress(void *arg, unsigned char status, const char *path)
2030 while (path[0] == '/')
2031 path++;
2032 printf("%c %s\n", status, path);
2035 static const struct got_error *
2036 cmd_revert(int argc, char *argv[])
2038 const struct got_error *error = NULL;
2039 struct got_worktree *worktree = NULL;
2040 struct got_repository *repo = NULL;
2041 char *cwd = NULL, *path = NULL;
2042 int ch;
2044 while ((ch = getopt(argc, argv, "")) != -1) {
2045 switch (ch) {
2046 default:
2047 usage_revert();
2048 /* NOTREACHED */
2052 argc -= optind;
2053 argv += optind;
2055 if (argc != 1)
2056 usage_revert();
2058 path = realpath(argv[0], NULL);
2059 if (path == NULL) {
2060 error = got_error_from_errno();
2061 goto done;
2063 got_path_strip_trailing_slashes(path);
2065 cwd = getcwd(NULL, 0);
2066 if (cwd == NULL) {
2067 error = got_error_from_errno();
2068 goto done;
2070 error = got_worktree_open(&worktree, cwd);
2071 if (error)
2072 goto done;
2074 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2075 if (error != NULL)
2076 goto done;
2078 error = apply_unveil(got_repo_get_path(repo), 1,
2079 got_worktree_get_root_path(worktree), 0);
2080 if (error)
2081 goto done;
2083 error = got_worktree_revert(worktree, path,
2084 revert_progress, NULL, repo);
2085 if (error)
2086 goto done;
2087 done:
2088 if (repo)
2089 got_repo_close(repo);
2090 if (worktree)
2091 got_worktree_close(worktree);
2092 free(path);
2093 free(cwd);
2094 return error;
2097 __dead static void
2098 usage_commit(void)
2100 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2101 exit(1);
2104 static const struct got_error *
2105 cmd_commit(int argc, char *argv[])
2107 const struct got_error *error = NULL;
2108 struct got_worktree *worktree = NULL;
2109 struct got_repository *repo = NULL;
2110 char *cwd = NULL, *path = NULL, *id_str = NULL;
2111 struct got_object_id *id = NULL;
2112 const char *logmsg = "<no log message was specified>";
2113 const char *got_author = getenv("GOT_AUTHOR");
2114 int ch;
2116 while ((ch = getopt(argc, argv, "m:")) != -1) {
2117 switch (ch) {
2118 case 'm':
2119 logmsg = optarg;
2120 break;
2121 default:
2122 usage_commit();
2123 /* NOTREACHED */
2127 argc -= optind;
2128 argv += optind;
2130 if (argc == 1) {
2131 path = realpath(argv[0], NULL);
2132 if (path == NULL) {
2133 error = got_error_from_errno();
2134 goto done;
2136 got_path_strip_trailing_slashes(path);
2137 } else if (argc != 0)
2138 usage_commit();
2140 if (got_author == NULL) {
2141 /* TODO: Look current user up in password database */
2142 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2143 goto done;
2146 cwd = getcwd(NULL, 0);
2147 if (cwd == NULL) {
2148 error = got_error_from_errno();
2149 goto done;
2151 error = got_worktree_open(&worktree, cwd);
2152 if (error)
2153 goto done;
2155 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2156 if (error != NULL)
2157 goto done;
2159 error = apply_unveil(got_repo_get_path(repo), 0,
2160 got_worktree_get_root_path(worktree), 0);
2161 if (error)
2162 goto done;
2164 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2165 logmsg, print_status, NULL, repo);
2166 if (error)
2167 goto done;
2169 error = got_object_id_str(&id_str, id);
2170 if (error)
2171 goto done;
2172 printf("created commit %s\n", id_str);
2173 done:
2174 if (repo)
2175 got_repo_close(repo);
2176 if (worktree)
2177 got_worktree_close(worktree);
2178 free(path);
2179 free(cwd);
2180 free(id_str);
2181 return error;