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_prefix_errno2("unveil", repo_path);
211 if (worktree_path && unveil(worktree_path, "rwc") != 0)
212 return got_error_prefix_errno2("unveil", worktree_path);
214 if (unveil("/tmp", "rwc") != 0)
215 return got_error_prefix_errno2("unveil", "/tmp");
217 error = got_privsep_unveil_exec_helpers();
218 if (error != NULL)
219 return error;
221 if (unveil(NULL, NULL) != 0)
222 return got_error_prefix_errno("unveil");
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_prefix_errno("strdup");
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_prefix_errno2("realpath", argv[0]);
356 cwd = getcwd(NULL, 0);
357 if (cwd == NULL) {
358 error = got_error_prefix_errno("getcwd");
359 goto done;
361 if (path_prefix[0]) {
362 base = basename(path_prefix);
363 if (base == NULL) {
364 error = got_error_prefix_errno2("basename",
365 path_prefix);
366 goto done;
368 } else {
369 base = basename(repo_path);
370 if (base == NULL) {
371 error = got_error_prefix_errno2("basename",
372 repo_path);
373 goto done;
376 dotgit = strstr(base, ".git");
377 if (dotgit)
378 *dotgit = '\0';
379 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
380 error = got_error_prefix_errno("asprintf");
381 free(cwd);
382 goto done;
384 free(cwd);
385 } else if (argc == 2) {
386 repo_path = realpath(argv[0], NULL);
387 if (repo_path == NULL) {
388 error = got_error_prefix_errno2("realpath", argv[0]);
389 goto done;
391 worktree_path = realpath(argv[1], NULL);
392 if (worktree_path == NULL) {
393 error = got_error_prefix_errno2("realpath", argv[1]);
394 goto done;
396 } else
397 usage_checkout();
399 got_path_strip_trailing_slashes(repo_path);
400 got_path_strip_trailing_slashes(worktree_path);
402 error = got_repo_open(&repo, repo_path);
403 if (error != NULL)
404 goto done;
406 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
407 if (error)
408 goto done;
410 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
411 if (error != NULL)
412 goto done;
414 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
415 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
416 goto done;
418 error = got_worktree_open(&worktree, worktree_path);
419 if (error != NULL)
420 goto done;
422 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
423 path_prefix);
424 if (error != NULL)
425 goto done;
426 if (!same_path_prefix) {
427 error = got_error(GOT_ERR_PATH_PREFIX);
428 goto done;
431 if (commit_id_str) {
432 struct got_object_id *commit_id;
433 error = got_object_resolve_id_str(&commit_id, repo,
434 commit_id_str);
435 if (error != NULL)
436 goto done;
437 error = check_ancestry(worktree, commit_id, repo);
438 if (error != NULL) {
439 free(commit_id);
440 goto done;
442 error = got_worktree_set_base_commit_id(worktree, repo,
443 commit_id);
444 free(commit_id);
445 if (error)
446 goto done;
449 error = got_worktree_checkout_files(worktree, "", repo,
450 checkout_progress, worktree_path, check_cancelled, NULL);
451 if (error != NULL)
452 goto done;
454 printf("Now shut up and hack\n");
456 done:
457 free(commit_id_str);
458 free(repo_path);
459 free(worktree_path);
460 return error;
463 __dead static void
464 usage_update(void)
466 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
467 getprogname());
468 exit(1);
471 static void
472 update_progress(void *arg, unsigned char status, const char *path)
474 int *did_something = arg;
476 if (status == GOT_STATUS_EXISTS)
477 return;
479 *did_something = 1;
480 while (path[0] == '/')
481 path++;
482 printf("%c %s\n", status, path);
485 static const struct got_error *
486 cmd_update(int argc, char *argv[])
488 const struct got_error *error = NULL;
489 struct got_repository *repo = NULL;
490 struct got_worktree *worktree = NULL;
491 char *worktree_path = NULL, *path = NULL;
492 struct got_object_id *commit_id = NULL;
493 char *commit_id_str = NULL;
494 int ch, did_something = 0;
496 while ((ch = getopt(argc, argv, "c:")) != -1) {
497 switch (ch) {
498 case 'c':
499 commit_id_str = strdup(optarg);
500 if (commit_id_str == NULL)
501 return got_error_prefix_errno("strdup");
502 break;
503 default:
504 usage_update();
505 /* NOTREACHED */
509 argc -= optind;
510 argv += optind;
512 #ifndef PROFILE
513 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
514 "unveil", NULL) == -1)
515 err(1, "pledge");
516 #endif
517 worktree_path = getcwd(NULL, 0);
518 if (worktree_path == NULL) {
519 error = got_error_prefix_errno("getcwd");
520 goto done;
522 error = got_worktree_open(&worktree, worktree_path);
523 if (error)
524 goto done;
526 if (argc == 0) {
527 path = strdup("");
528 if (path == NULL) {
529 error = got_error_prefix_errno("strdup");
530 goto done;
532 } else if (argc == 1) {
533 error = got_worktree_resolve_path(&path, worktree, argv[0]);
534 if (error)
535 goto done;
536 } else
537 usage_update();
539 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
540 if (error != NULL)
541 goto done;
543 error = apply_unveil(got_repo_get_path(repo), 0,
544 got_worktree_get_root_path(worktree), 0);
545 if (error)
546 goto done;
548 if (commit_id_str == NULL) {
549 struct got_reference *head_ref;
550 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
551 if (error != NULL)
552 goto done;
553 error = got_ref_resolve(&commit_id, repo, head_ref);
554 if (error != NULL)
555 goto done;
556 error = got_object_id_str(&commit_id_str, commit_id);
557 if (error != NULL)
558 goto done;
559 } else {
560 error = got_object_resolve_id_str(&commit_id, repo,
561 commit_id_str);
562 if (error != NULL)
563 goto done;
566 error = check_ancestry(worktree, commit_id, repo);
567 if (error != NULL)
568 goto done;
570 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
571 commit_id) != 0) {
572 error = got_worktree_set_base_commit_id(worktree, repo,
573 commit_id);
574 if (error)
575 goto done;
578 error = got_worktree_checkout_files(worktree, path, repo,
579 update_progress, &did_something, check_cancelled, NULL);
580 if (error != NULL)
581 goto done;
583 if (did_something)
584 printf("Updated to commit %s\n", commit_id_str);
585 else
586 printf("Already up-to-date\n");
587 done:
588 free(worktree_path);
589 free(path);
590 free(commit_id);
591 free(commit_id_str);
592 return error;
595 static const struct got_error *
596 print_patch(struct got_commit_object *commit, struct got_object_id *id,
597 int diff_context, struct got_repository *repo)
599 const struct got_error *err = NULL;
600 struct got_tree_object *tree1 = NULL, *tree2;
601 struct got_object_qid *qid;
602 char *id_str1 = NULL, *id_str2;
604 err = got_object_open_as_tree(&tree2, repo,
605 got_object_commit_get_tree_id(commit));
606 if (err)
607 return err;
609 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
610 if (qid != NULL) {
611 struct got_commit_object *pcommit;
613 err = got_object_open_as_commit(&pcommit, repo, qid->id);
614 if (err)
615 return err;
617 err = got_object_open_as_tree(&tree1, repo,
618 got_object_commit_get_tree_id(pcommit));
619 got_object_commit_close(pcommit);
620 if (err)
621 return err;
623 err = got_object_id_str(&id_str1, qid->id);
624 if (err)
625 return err;
628 err = got_object_id_str(&id_str2, id);
629 if (err)
630 goto done;
632 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
633 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
634 done:
635 if (tree1)
636 got_object_tree_close(tree1);
637 got_object_tree_close(tree2);
638 free(id_str1);
639 free(id_str2);
640 return err;
643 static char *
644 get_datestr(time_t *time, char *datebuf)
646 char *p, *s = ctime_r(time, datebuf);
647 p = strchr(s, '\n');
648 if (p)
649 *p = '\0';
650 return s;
653 static const struct got_error *
654 print_commit(struct got_commit_object *commit, struct got_object_id *id,
655 struct got_repository *repo, int show_patch, int diff_context,
656 struct got_reflist_head *refs)
658 const struct got_error *err = NULL;
659 char *id_str, *datestr, *logmsg0, *logmsg, *line;
660 char datebuf[26];
661 time_t committer_time;
662 const char *author, *committer;
663 char *refs_str = NULL;
664 struct got_reflist_entry *re;
666 SIMPLEQ_FOREACH(re, refs, entry) {
667 char *s;
668 const char *name;
669 if (got_object_id_cmp(re->id, id) != 0)
670 continue;
671 name = got_ref_get_name(re->ref);
672 if (strcmp(name, GOT_REF_HEAD) == 0)
673 continue;
674 if (strncmp(name, "refs/", 5) == 0)
675 name += 5;
676 if (strncmp(name, "got/", 4) == 0)
677 continue;
678 if (strncmp(name, "heads/", 6) == 0)
679 name += 6;
680 if (strncmp(name, "remotes/", 8) == 0)
681 name += 8;
682 s = refs_str;
683 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
684 name) == -1) {
685 err = got_error_prefix_errno("asprintf");
686 free(s);
687 break;
689 free(s);
691 err = got_object_id_str(&id_str, id);
692 if (err)
693 return err;
695 printf("-----------------------------------------------\n");
696 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
697 refs_str ? refs_str : "", refs_str ? ")" : "");
698 free(id_str);
699 id_str = NULL;
700 free(refs_str);
701 refs_str = NULL;
702 printf("from: %s\n", got_object_commit_get_author(commit));
703 committer_time = got_object_commit_get_committer_time(commit);
704 datestr = get_datestr(&committer_time, datebuf);
705 printf("date: %s UTC\n", datestr);
706 author = got_object_commit_get_author(commit);
707 committer = got_object_commit_get_committer(commit);
708 if (strcmp(author, committer) != 0)
709 printf("via: %s\n", committer);
710 if (got_object_commit_get_nparents(commit) > 1) {
711 const struct got_object_id_queue *parent_ids;
712 struct got_object_qid *qid;
713 int n = 1;
714 parent_ids = got_object_commit_get_parent_ids(commit);
715 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
716 err = got_object_id_str(&id_str, qid->id);
717 if (err)
718 return err;
719 printf("parent %d: %s\n", n++, id_str);
720 free(id_str);
724 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
725 if (logmsg0 == NULL)
726 return got_error_prefix_errno("strdup");
728 logmsg = logmsg0;
729 do {
730 line = strsep(&logmsg, "\n");
731 if (line)
732 printf(" %s\n", line);
733 } while (line);
734 free(logmsg0);
736 if (show_patch) {
737 err = print_patch(commit, id, diff_context, repo);
738 if (err == 0)
739 printf("\n");
742 if (fflush(stdout) != 0 && err == NULL)
743 err = got_error_prefix_errno("fflush");
744 return err;
747 static const struct got_error *
748 print_commits(struct got_object_id *root_id, struct got_repository *repo,
749 char *path, int show_patch, int diff_context, int limit,
750 int first_parent_traversal, struct got_reflist_head *refs)
752 const struct got_error *err;
753 struct got_commit_graph *graph;
755 err = got_commit_graph_open(&graph, root_id, path,
756 first_parent_traversal, repo);
757 if (err)
758 return err;
759 err = got_commit_graph_iter_start(graph, root_id, repo);
760 if (err)
761 goto done;
762 while (1) {
763 struct got_commit_object *commit;
764 struct got_object_id *id;
766 if (sigint_received || sigpipe_received)
767 break;
769 err = got_commit_graph_iter_next(&id, graph);
770 if (err) {
771 if (err->code == GOT_ERR_ITER_COMPLETED) {
772 err = NULL;
773 break;
775 if (err->code != GOT_ERR_ITER_NEED_MORE)
776 break;
777 err = got_commit_graph_fetch_commits(graph, 1, repo);
778 if (err)
779 break;
780 else
781 continue;
783 if (id == NULL)
784 break;
786 err = got_object_open_as_commit(&commit, repo, id);
787 if (err)
788 break;
789 err = print_commit(commit, id, repo, show_patch, diff_context,
790 refs);
791 got_object_commit_close(commit);
792 if (err || (limit && --limit == 0))
793 break;
795 done:
796 got_commit_graph_close(graph);
797 return err;
800 __dead static void
801 usage_log(void)
803 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
804 "[-r repository-path] [path]\n", getprogname());
805 exit(1);
808 static const struct got_error *
809 cmd_log(int argc, char *argv[])
811 const struct got_error *error;
812 struct got_repository *repo = NULL;
813 struct got_worktree *worktree = NULL;
814 struct got_commit_object *commit = NULL;
815 struct got_object_id *id = NULL;
816 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
817 char *start_commit = NULL;
818 int diff_context = 3, ch;
819 int show_patch = 0, limit = 0, first_parent_traversal = 0;
820 const char *errstr;
821 struct got_reflist_head refs;
823 SIMPLEQ_INIT(&refs);
825 #ifndef PROFILE
826 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
827 NULL)
828 == -1)
829 err(1, "pledge");
830 #endif
832 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
833 switch (ch) {
834 case 'p':
835 show_patch = 1;
836 break;
837 case 'c':
838 start_commit = optarg;
839 break;
840 case 'C':
841 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
842 &errstr);
843 if (errstr != NULL)
844 err(1, "-C option %s", errstr);
845 break;
846 case 'l':
847 limit = strtonum(optarg, 1, INT_MAX, &errstr);
848 if (errstr != NULL)
849 err(1, "-l option %s", errstr);
850 break;
851 case 'f':
852 first_parent_traversal = 1;
853 break;
854 case 'r':
855 repo_path = realpath(optarg, NULL);
856 if (repo_path == NULL)
857 err(1, "-r option");
858 got_path_strip_trailing_slashes(repo_path);
859 break;
860 default:
861 usage_log();
862 /* NOTREACHED */
866 argc -= optind;
867 argv += optind;
869 cwd = getcwd(NULL, 0);
870 if (cwd == NULL) {
871 error = got_error_prefix_errno("getcwd");
872 goto done;
875 error = got_worktree_open(&worktree, cwd);
876 if (error && error->code != GOT_ERR_NOT_WORKTREE)
877 goto done;
878 error = NULL;
880 if (argc == 0) {
881 path = strdup("");
882 if (path == NULL) {
883 error = got_error_prefix_errno("strdup");
884 goto done;
886 } else if (argc == 1) {
887 if (worktree) {
888 error = got_worktree_resolve_path(&path, worktree,
889 argv[0]);
890 if (error)
891 goto done;
892 } else {
893 path = strdup(argv[0]);
894 if (path == NULL) {
895 error = got_error_prefix_errno("strdup");
896 goto done;
899 } else
900 usage_log();
902 if (repo_path == NULL) {
903 repo_path = worktree ?
904 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
906 if (repo_path == NULL) {
907 error = got_error_prefix_errno("strdup");
908 goto done;
911 error = got_repo_open(&repo, repo_path);
912 if (error != NULL)
913 goto done;
915 error = apply_unveil(got_repo_get_path(repo), 1,
916 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
917 if (error)
918 goto done;
920 if (start_commit == NULL) {
921 struct got_reference *head_ref;
922 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
923 if (error != NULL)
924 return error;
925 error = got_ref_resolve(&id, repo, head_ref);
926 got_ref_close(head_ref);
927 if (error != NULL)
928 return error;
929 error = got_object_open_as_commit(&commit, repo, id);
930 } else {
931 struct got_reference *ref;
932 error = got_ref_open(&ref, repo, start_commit);
933 if (error == NULL) {
934 int obj_type;
935 error = got_ref_resolve(&id, repo, ref);
936 got_ref_close(ref);
937 if (error != NULL)
938 goto done;
939 error = got_object_get_type(&obj_type, repo, id);
940 if (error != NULL)
941 goto done;
942 if (obj_type == GOT_OBJ_TYPE_TAG) {
943 struct got_tag_object *tag;
944 error = got_object_open_as_tag(&tag, repo, id);
945 if (error != NULL)
946 goto done;
947 if (got_object_tag_get_object_type(tag) !=
948 GOT_OBJ_TYPE_COMMIT) {
949 got_object_tag_close(tag);
950 error = got_error(GOT_ERR_OBJ_TYPE);
951 goto done;
953 free(id);
954 id = got_object_id_dup(
955 got_object_tag_get_object_id(tag));
956 if (id == NULL)
957 error = got_error_prefix_errno(
958 "got_object_id_dup");
959 got_object_tag_close(tag);
960 if (error)
961 goto done;
962 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
963 error = got_error(GOT_ERR_OBJ_TYPE);
964 goto done;
966 error = got_object_open_as_commit(&commit, repo, id);
967 if (error != NULL)
968 goto done;
970 if (commit == NULL) {
971 error = got_object_resolve_id_str(&id, repo,
972 start_commit);
973 if (error != NULL)
974 return error;
977 if (error != NULL)
978 goto done;
980 error = got_repo_map_path(&in_repo_path, repo, path, 1);
981 if (error != NULL)
982 goto done;
983 if (in_repo_path) {
984 free(path);
985 path = in_repo_path;
988 error = got_ref_list(&refs, repo);
989 if (error)
990 goto done;
992 error = print_commits(id, repo, path, show_patch,
993 diff_context, limit, first_parent_traversal, &refs);
994 done:
995 free(path);
996 free(repo_path);
997 free(cwd);
998 free(id);
999 if (worktree)
1000 got_worktree_close(worktree);
1001 if (repo) {
1002 const struct got_error *repo_error;
1003 repo_error = got_repo_close(repo);
1004 if (error == NULL)
1005 error = repo_error;
1007 got_ref_list_free(&refs);
1008 return error;
1011 __dead static void
1012 usage_diff(void)
1014 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1015 "[object1 object2 | path]\n", getprogname());
1016 exit(1);
1019 struct print_diff_arg {
1020 struct got_repository *repo;
1021 struct got_worktree *worktree;
1022 int diff_context;
1023 const char *id_str;
1024 int header_shown;
1027 static const struct got_error *
1028 print_diff(void *arg, unsigned char status, const char *path,
1029 struct got_object_id *id)
1031 struct print_diff_arg *a = arg;
1032 const struct got_error *err = NULL;
1033 struct got_blob_object *blob1 = NULL;
1034 FILE *f2 = NULL;
1035 char *abspath = NULL;
1036 struct stat sb;
1038 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1039 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1040 return NULL;
1042 if (!a->header_shown) {
1043 printf("diff %s %s\n", a->id_str,
1044 got_worktree_get_root_path(a->worktree));
1045 a->header_shown = 1;
1048 if (status != GOT_STATUS_ADD) {
1049 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1050 if (err)
1051 goto done;
1055 if (status != GOT_STATUS_DELETE) {
1056 if (asprintf(&abspath, "%s/%s",
1057 got_worktree_get_root_path(a->worktree), path) == -1) {
1058 err = got_error_prefix_errno("asprintf");
1059 goto done;
1062 f2 = fopen(abspath, "r");
1063 if (f2 == NULL) {
1064 err = got_error_prefix_errno2("fopen", abspath);
1065 goto done;
1067 if (lstat(abspath, &sb) == -1) {
1068 err = got_error_prefix_errno2("lstat", abspath);
1069 goto done;
1071 } else
1072 sb.st_size = 0;
1074 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1075 stdout);
1076 done:
1077 if (blob1)
1078 got_object_blob_close(blob1);
1079 if (f2 && fclose(f2) != 0 && err == NULL)
1080 err = got_error_prefix_errno("fclose");
1081 free(abspath);
1082 return err;
1085 static const struct got_error *
1086 cmd_diff(int argc, char *argv[])
1088 const struct got_error *error;
1089 struct got_repository *repo = NULL;
1090 struct got_worktree *worktree = NULL;
1091 char *cwd = NULL, *repo_path = NULL;
1092 struct got_object_id *id1 = NULL, *id2 = NULL;
1093 char *id_str1 = NULL, *id_str2 = NULL;
1094 int type1, type2;
1095 int diff_context = 3, ch;
1096 const char *errstr;
1097 char *path = NULL;
1099 #ifndef PROFILE
1100 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1101 NULL) == -1)
1102 err(1, "pledge");
1103 #endif
1105 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1106 switch (ch) {
1107 case 'C':
1108 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1109 if (errstr != NULL)
1110 err(1, "-C option %s", errstr);
1111 break;
1112 case 'r':
1113 repo_path = realpath(optarg, NULL);
1114 if (repo_path == NULL)
1115 err(1, "-r option");
1116 got_path_strip_trailing_slashes(repo_path);
1117 break;
1118 default:
1119 usage_diff();
1120 /* NOTREACHED */
1124 argc -= optind;
1125 argv += optind;
1127 cwd = getcwd(NULL, 0);
1128 if (cwd == NULL) {
1129 error = got_error_prefix_errno("getcwd");
1130 goto done;
1132 error = got_worktree_open(&worktree, cwd);
1133 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1134 goto done;
1135 if (argc <= 1) {
1136 if (worktree == NULL) {
1137 error = got_error(GOT_ERR_NOT_WORKTREE);
1138 goto done;
1140 if (repo_path)
1141 errx(1,
1142 "-r option can't be used when diffing a work tree");
1143 repo_path = strdup(got_worktree_get_repo_path(worktree));
1144 if (repo_path == NULL) {
1145 error = got_error_prefix_errno("strdup");
1146 goto done;
1148 if (argc == 1) {
1149 error = got_worktree_resolve_path(&path, worktree,
1150 argv[0]);
1151 if (error)
1152 goto done;
1153 } else {
1154 path = strdup("");
1155 if (path == NULL) {
1156 error = got_error_prefix_errno("strdup");
1157 goto done;
1160 } else if (argc == 2) {
1161 id_str1 = argv[0];
1162 id_str2 = argv[1];
1163 } else
1164 usage_diff();
1166 if (repo_path == NULL) {
1167 repo_path = getcwd(NULL, 0);
1168 if (repo_path == NULL)
1169 return got_error_prefix_errno("getcwd");
1172 error = got_repo_open(&repo, repo_path);
1173 free(repo_path);
1174 if (error != NULL)
1175 goto done;
1177 error = apply_unveil(got_repo_get_path(repo), 1,
1178 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1179 if (error)
1180 goto done;
1182 if (worktree) {
1183 struct print_diff_arg arg;
1184 char *id_str;
1185 error = got_object_id_str(&id_str,
1186 got_worktree_get_base_commit_id(worktree));
1187 if (error)
1188 goto done;
1189 arg.repo = repo;
1190 arg.worktree = worktree;
1191 arg.diff_context = diff_context;
1192 arg.id_str = id_str;
1193 arg.header_shown = 0;
1195 error = got_worktree_status(worktree, path, repo, print_diff,
1196 &arg, check_cancelled, NULL);
1197 free(id_str);
1198 goto done;
1201 error = got_object_resolve_id_str(&id1, repo, id_str1);
1202 if (error)
1203 goto done;
1205 error = got_object_resolve_id_str(&id2, repo, id_str2);
1206 if (error)
1207 goto done;
1209 error = got_object_get_type(&type1, repo, id1);
1210 if (error)
1211 goto done;
1213 error = got_object_get_type(&type2, repo, id2);
1214 if (error)
1215 goto done;
1217 if (type1 != type2) {
1218 error = got_error(GOT_ERR_OBJ_TYPE);
1219 goto done;
1222 switch (type1) {
1223 case GOT_OBJ_TYPE_BLOB:
1224 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1225 diff_context, repo, stdout);
1226 break;
1227 case GOT_OBJ_TYPE_TREE:
1228 error = got_diff_objects_as_trees(id1, id2, "", "",
1229 diff_context, repo, stdout);
1230 break;
1231 case GOT_OBJ_TYPE_COMMIT:
1232 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1233 id_str2);
1234 error = got_diff_objects_as_commits(id1, id2, diff_context,
1235 repo, stdout);
1236 break;
1237 default:
1238 error = got_error(GOT_ERR_OBJ_TYPE);
1241 done:
1242 free(id1);
1243 free(id2);
1244 free(path);
1245 if (worktree)
1246 got_worktree_close(worktree);
1247 if (repo) {
1248 const struct got_error *repo_error;
1249 repo_error = got_repo_close(repo);
1250 if (error == NULL)
1251 error = repo_error;
1253 return error;
1256 __dead static void
1257 usage_blame(void)
1259 fprintf(stderr,
1260 "usage: %s blame [-c commit] [-r repository-path] path\n",
1261 getprogname());
1262 exit(1);
1265 static const struct got_error *
1266 cmd_blame(int argc, char *argv[])
1268 const struct got_error *error;
1269 struct got_repository *repo = NULL;
1270 struct got_worktree *worktree = NULL;
1271 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1272 struct got_object_id *commit_id = NULL;
1273 char *commit_id_str = NULL;
1274 int ch;
1276 #ifndef PROFILE
1277 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1278 NULL) == -1)
1279 err(1, "pledge");
1280 #endif
1282 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1283 switch (ch) {
1284 case 'c':
1285 commit_id_str = optarg;
1286 break;
1287 case 'r':
1288 repo_path = realpath(optarg, NULL);
1289 if (repo_path == NULL)
1290 err(1, "-r option");
1291 got_path_strip_trailing_slashes(repo_path);
1292 break;
1293 default:
1294 usage_blame();
1295 /* NOTREACHED */
1299 argc -= optind;
1300 argv += optind;
1302 if (argc == 1)
1303 path = argv[0];
1304 else
1305 usage_blame();
1307 cwd = getcwd(NULL, 0);
1308 if (cwd == NULL) {
1309 error = got_error_prefix_errno("getcwd");
1310 goto done;
1312 if (repo_path == NULL) {
1313 error = got_worktree_open(&worktree, cwd);
1314 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1315 goto done;
1316 else
1317 error = NULL;
1318 if (worktree) {
1319 repo_path =
1320 strdup(got_worktree_get_repo_path(worktree));
1321 if (repo_path == NULL)
1322 error = got_error_prefix_errno("strdup");
1323 if (error)
1324 goto done;
1325 } else {
1326 repo_path = strdup(cwd);
1327 if (repo_path == NULL) {
1328 error = got_error_prefix_errno("strdup");
1329 goto done;
1334 error = got_repo_open(&repo, repo_path);
1335 if (error != NULL)
1336 goto done;
1338 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1339 if (error)
1340 goto done;
1342 if (worktree) {
1343 const char *prefix = got_worktree_get_path_prefix(worktree);
1344 char *p, *worktree_subdir = cwd +
1345 strlen(got_worktree_get_root_path(worktree));
1346 if (asprintf(&p, "%s%s%s%s%s",
1347 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1348 worktree_subdir, worktree_subdir[0] ? "/" : "",
1349 path) == -1) {
1350 error = got_error_prefix_errno("asprintf");
1351 goto done;
1353 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1354 free(p);
1355 } else {
1356 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1358 if (error)
1359 goto done;
1361 if (commit_id_str == NULL) {
1362 struct got_reference *head_ref;
1363 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1364 if (error != NULL)
1365 goto done;
1366 error = got_ref_resolve(&commit_id, repo, head_ref);
1367 got_ref_close(head_ref);
1368 if (error != NULL)
1369 goto done;
1370 } else {
1371 error = got_object_resolve_id_str(&commit_id, repo,
1372 commit_id_str);
1373 if (error != NULL)
1374 goto done;
1377 error = got_blame(in_repo_path, commit_id, repo, stdout);
1378 done:
1379 free(in_repo_path);
1380 free(repo_path);
1381 free(cwd);
1382 free(commit_id);
1383 if (worktree)
1384 got_worktree_close(worktree);
1385 if (repo) {
1386 const struct got_error *repo_error;
1387 repo_error = got_repo_close(repo);
1388 if (error == NULL)
1389 error = repo_error;
1391 return error;
1394 __dead static void
1395 usage_tree(void)
1397 fprintf(stderr,
1398 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1399 getprogname());
1400 exit(1);
1403 static void
1404 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1405 const char *root_path)
1407 int is_root_path = (strcmp(path, root_path) == 0);
1409 path += strlen(root_path);
1410 while (path[0] == '/')
1411 path++;
1413 printf("%s%s%s%s%s\n", id ? id : "", path,
1414 is_root_path ? "" : "/", te->name,
1415 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1418 static const struct got_error *
1419 print_tree(const char *path, struct got_object_id *commit_id,
1420 int show_ids, int recurse, const char *root_path,
1421 struct got_repository *repo)
1423 const struct got_error *err = NULL;
1424 struct got_object_id *tree_id = NULL;
1425 struct got_tree_object *tree = NULL;
1426 const struct got_tree_entries *entries;
1427 struct got_tree_entry *te;
1429 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1430 if (err)
1431 goto done;
1433 err = got_object_open_as_tree(&tree, repo, tree_id);
1434 if (err)
1435 goto done;
1436 entries = got_object_tree_get_entries(tree);
1437 te = SIMPLEQ_FIRST(&entries->head);
1438 while (te) {
1439 char *id = NULL;
1441 if (sigint_received || sigpipe_received)
1442 break;
1444 if (show_ids) {
1445 char *id_str;
1446 err = got_object_id_str(&id_str, te->id);
1447 if (err)
1448 goto done;
1449 if (asprintf(&id, "%s ", id_str) == -1) {
1450 err = got_error_prefix_errno("asprintf");
1451 free(id_str);
1452 goto done;
1454 free(id_str);
1456 print_entry(te, id, path, root_path);
1457 free(id);
1459 if (recurse && S_ISDIR(te->mode)) {
1460 char *child_path;
1461 if (asprintf(&child_path, "%s%s%s", path,
1462 path[0] == '/' && path[1] == '\0' ? "" : "/",
1463 te->name) == -1) {
1464 err = got_error_prefix_errno("asprintf");
1465 goto done;
1467 err = print_tree(child_path, commit_id, show_ids, 1,
1468 root_path, repo);
1469 free(child_path);
1470 if (err)
1471 goto done;
1474 te = SIMPLEQ_NEXT(te, entry);
1476 done:
1477 if (tree)
1478 got_object_tree_close(tree);
1479 free(tree_id);
1480 return err;
1483 static const struct got_error *
1484 cmd_tree(int argc, char *argv[])
1486 const struct got_error *error;
1487 struct got_repository *repo = NULL;
1488 struct got_worktree *worktree = NULL;
1489 const char *path;
1490 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1491 struct got_object_id *commit_id = NULL;
1492 char *commit_id_str = NULL;
1493 int show_ids = 0, recurse = 0;
1494 int ch;
1496 #ifndef PROFILE
1497 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1498 NULL) == -1)
1499 err(1, "pledge");
1500 #endif
1502 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1503 switch (ch) {
1504 case 'c':
1505 commit_id_str = optarg;
1506 break;
1507 case 'r':
1508 repo_path = realpath(optarg, NULL);
1509 if (repo_path == NULL)
1510 err(1, "-r option");
1511 got_path_strip_trailing_slashes(repo_path);
1512 break;
1513 case 'i':
1514 show_ids = 1;
1515 break;
1516 case 'R':
1517 recurse = 1;
1518 break;
1519 default:
1520 usage_tree();
1521 /* NOTREACHED */
1525 argc -= optind;
1526 argv += optind;
1528 if (argc == 1)
1529 path = argv[0];
1530 else if (argc > 1)
1531 usage_tree();
1532 else
1533 path = NULL;
1535 cwd = getcwd(NULL, 0);
1536 if (cwd == NULL) {
1537 error = got_error_prefix_errno("getcwd");
1538 goto done;
1540 if (repo_path == NULL) {
1541 error = got_worktree_open(&worktree, cwd);
1542 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1543 goto done;
1544 else
1545 error = NULL;
1546 if (worktree) {
1547 repo_path =
1548 strdup(got_worktree_get_repo_path(worktree));
1549 if (repo_path == NULL)
1550 error = got_error_prefix_errno("strdup");
1551 if (error)
1552 goto done;
1553 } else {
1554 repo_path = strdup(cwd);
1555 if (repo_path == NULL) {
1556 error = got_error_prefix_errno("strdup");
1557 goto done;
1562 error = got_repo_open(&repo, repo_path);
1563 if (error != NULL)
1564 goto done;
1566 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1567 if (error)
1568 goto done;
1570 if (path == NULL) {
1571 if (worktree) {
1572 char *p, *worktree_subdir = cwd +
1573 strlen(got_worktree_get_root_path(worktree));
1574 if (asprintf(&p, "%s/%s",
1575 got_worktree_get_path_prefix(worktree),
1576 worktree_subdir) == -1) {
1577 error = got_error_prefix_errno("asprintf");
1578 goto done;
1580 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1581 free(p);
1582 if (error)
1583 goto done;
1584 } else
1585 path = "/";
1587 if (in_repo_path == NULL) {
1588 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1589 if (error != NULL)
1590 goto done;
1593 if (commit_id_str == NULL) {
1594 struct got_reference *head_ref;
1595 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1596 if (error != NULL)
1597 goto done;
1598 error = got_ref_resolve(&commit_id, repo, head_ref);
1599 got_ref_close(head_ref);
1600 if (error != NULL)
1601 goto done;
1602 } else {
1603 error = got_object_resolve_id_str(&commit_id, repo,
1604 commit_id_str);
1605 if (error != NULL)
1606 goto done;
1609 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1610 in_repo_path, repo);
1611 done:
1612 free(in_repo_path);
1613 free(repo_path);
1614 free(cwd);
1615 free(commit_id);
1616 if (worktree)
1617 got_worktree_close(worktree);
1618 if (repo) {
1619 const struct got_error *repo_error;
1620 repo_error = got_repo_close(repo);
1621 if (error == NULL)
1622 error = repo_error;
1624 return error;
1627 __dead static void
1628 usage_status(void)
1630 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1631 exit(1);
1634 static const struct got_error *
1635 print_status(void *arg, unsigned char status, const char *path,
1636 struct got_object_id *id)
1638 printf("%c %s\n", status, path);
1639 return NULL;
1642 static const struct got_error *
1643 cmd_status(int argc, char *argv[])
1645 const struct got_error *error = NULL;
1646 struct got_repository *repo = NULL;
1647 struct got_worktree *worktree = NULL;
1648 char *cwd = NULL, *path = NULL;
1649 int ch;
1651 while ((ch = getopt(argc, argv, "")) != -1) {
1652 switch (ch) {
1653 default:
1654 usage_status();
1655 /* NOTREACHED */
1659 argc -= optind;
1660 argv += optind;
1662 #ifndef PROFILE
1663 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1664 NULL) == -1)
1665 err(1, "pledge");
1666 #endif
1667 cwd = getcwd(NULL, 0);
1668 if (cwd == NULL) {
1669 error = got_error_prefix_errno("getcwd");
1670 goto done;
1673 error = got_worktree_open(&worktree, cwd);
1674 if (error != NULL)
1675 goto done;
1677 if (argc == 0) {
1678 path = strdup("");
1679 if (path == NULL) {
1680 error = got_error_prefix_errno("strdup");
1681 goto done;
1683 } else if (argc == 1) {
1684 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1685 if (error)
1686 goto done;
1687 } else
1688 usage_status();
1690 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1691 if (error != NULL)
1692 goto done;
1694 error = apply_unveil(got_repo_get_path(repo), 1,
1695 got_worktree_get_root_path(worktree), 0);
1696 if (error)
1697 goto done;
1699 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1700 check_cancelled, NULL);
1701 done:
1702 free(cwd);
1703 free(path);
1704 return error;
1707 __dead static void
1708 usage_ref(void)
1710 fprintf(stderr,
1711 "usage: %s ref [-r repository] -l | -d name | name object\n",
1712 getprogname());
1713 exit(1);
1716 static const struct got_error *
1717 list_refs(struct got_repository *repo)
1719 static const struct got_error *err = NULL;
1720 struct got_reflist_head refs;
1721 struct got_reflist_entry *re;
1723 SIMPLEQ_INIT(&refs);
1724 err = got_ref_list(&refs, repo);
1725 if (err)
1726 return err;
1728 SIMPLEQ_FOREACH(re, &refs, entry) {
1729 char *refstr;
1730 refstr = got_ref_to_str(re->ref);
1731 if (refstr == NULL)
1732 return got_error_prefix_errno("got_ref_to_str");
1733 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1734 free(refstr);
1737 got_ref_list_free(&refs);
1738 return NULL;
1741 static const struct got_error *
1742 delete_ref(struct got_repository *repo, const char *refname)
1744 const struct got_error *err = NULL;
1745 struct got_reference *ref;
1747 err = got_ref_open(&ref, repo, refname);
1748 if (err)
1749 return err;
1751 err = got_ref_delete(ref, repo);
1752 got_ref_close(ref);
1753 return err;
1756 static const struct got_error *
1757 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1759 const struct got_error *err = NULL;
1760 struct got_object_id *id;
1761 struct got_reference *ref = NULL;
1763 err = got_object_resolve_id_str(&id, repo, id_str);
1764 if (err)
1765 return err;
1767 err = got_ref_alloc(&ref, refname, id);
1768 if (err)
1769 goto done;
1771 err = got_ref_write(ref, repo);
1772 done:
1773 if (ref)
1774 got_ref_close(ref);
1775 free(id);
1776 return err;
1779 static const struct got_error *
1780 cmd_ref(int argc, char *argv[])
1782 const struct got_error *error = NULL;
1783 struct got_repository *repo = NULL;
1784 struct got_worktree *worktree = NULL;
1785 char *cwd = NULL, *repo_path = NULL;
1786 int ch, do_list = 0;
1787 const char *delref = NULL;
1789 /* TODO: Add -s option for adding symbolic references. */
1790 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1791 switch (ch) {
1792 case 'd':
1793 delref = optarg;
1794 break;
1795 case 'r':
1796 repo_path = realpath(optarg, NULL);
1797 if (repo_path == NULL)
1798 err(1, "-r option");
1799 got_path_strip_trailing_slashes(repo_path);
1800 break;
1801 case 'l':
1802 do_list = 1;
1803 break;
1804 default:
1805 usage_ref();
1806 /* NOTREACHED */
1810 if (do_list && delref)
1811 errx(1, "-l and -d options are mutually exclusive\n");
1813 argc -= optind;
1814 argv += optind;
1816 if (do_list || delref) {
1817 if (argc > 0)
1818 usage_ref();
1819 } else if (argc != 2)
1820 usage_ref();
1822 #ifndef PROFILE
1823 if (do_list) {
1824 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1825 NULL) == -1)
1826 err(1, "pledge");
1827 } else {
1828 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1829 "sendfd unveil", NULL) == -1)
1830 err(1, "pledge");
1832 #endif
1833 cwd = getcwd(NULL, 0);
1834 if (cwd == NULL) {
1835 error = got_error_prefix_errno("getcwd");
1836 goto done;
1839 if (repo_path == NULL) {
1840 error = got_worktree_open(&worktree, cwd);
1841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1842 goto done;
1843 else
1844 error = NULL;
1845 if (worktree) {
1846 repo_path =
1847 strdup(got_worktree_get_repo_path(worktree));
1848 if (repo_path == NULL)
1849 error = got_error_prefix_errno("strdup");
1850 if (error)
1851 goto done;
1852 } else {
1853 repo_path = strdup(cwd);
1854 if (repo_path == NULL) {
1855 error = got_error_prefix_errno("strdup");
1856 goto done;
1861 error = got_repo_open(&repo, repo_path);
1862 if (error != NULL)
1863 goto done;
1865 error = apply_unveil(got_repo_get_path(repo), do_list,
1866 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1867 if (error)
1868 goto done;
1870 if (do_list)
1871 error = list_refs(repo);
1872 else if (delref)
1873 error = delete_ref(repo, delref);
1874 else
1875 error = add_ref(repo, argv[0], argv[1]);
1876 done:
1877 if (repo)
1878 got_repo_close(repo);
1879 if (worktree)
1880 got_worktree_close(worktree);
1881 free(cwd);
1882 free(repo_path);
1883 return error;
1886 __dead static void
1887 usage_add(void)
1889 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1890 exit(1);
1893 static const struct got_error *
1894 cmd_add(int argc, char *argv[])
1896 const struct got_error *error = NULL;
1897 struct got_repository *repo = NULL;
1898 struct got_worktree *worktree = NULL;
1899 char *cwd = NULL, *path = NULL, *relpath = NULL;
1900 int ch;
1902 while ((ch = getopt(argc, argv, "")) != -1) {
1903 switch (ch) {
1904 default:
1905 usage_add();
1906 /* NOTREACHED */
1910 argc -= optind;
1911 argv += optind;
1913 if (argc != 1)
1914 usage_add();
1916 path = realpath(argv[0], NULL);
1917 if (path == NULL) {
1918 error = got_error_prefix_errno2("realpath", argv[0]);
1919 goto done;
1921 got_path_strip_trailing_slashes(path);
1923 cwd = getcwd(NULL, 0);
1924 if (cwd == NULL) {
1925 error = got_error_prefix_errno("getcwd");
1926 goto done;
1928 error = got_worktree_open(&worktree, cwd);
1929 if (error)
1930 goto done;
1932 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1933 if (error != NULL)
1934 goto done;
1936 error = apply_unveil(got_repo_get_path(repo), 1,
1937 got_worktree_get_root_path(worktree), 0);
1938 if (error)
1939 goto done;
1941 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1942 repo);
1943 if (error)
1944 goto done;
1945 done:
1946 if (repo)
1947 got_repo_close(repo);
1948 if (worktree)
1949 got_worktree_close(worktree);
1950 free(path);
1951 free(relpath);
1952 free(cwd);
1953 return error;
1956 __dead static void
1957 usage_rm(void)
1959 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1960 exit(1);
1963 static const struct got_error *
1964 cmd_rm(int argc, char *argv[])
1966 const struct got_error *error = NULL;
1967 struct got_worktree *worktree = NULL;
1968 struct got_repository *repo = NULL;
1969 char *cwd = NULL, *path = NULL;
1970 int ch, delete_local_mods = 0;
1972 while ((ch = getopt(argc, argv, "f")) != -1) {
1973 switch (ch) {
1974 case 'f':
1975 delete_local_mods = 1;
1976 break;
1977 default:
1978 usage_add();
1979 /* NOTREACHED */
1983 argc -= optind;
1984 argv += optind;
1986 if (argc != 1)
1987 usage_rm();
1989 path = realpath(argv[0], NULL);
1990 if (path == NULL) {
1991 error = got_error_prefix_errno2("realpath", argv[0]);
1992 goto done;
1994 got_path_strip_trailing_slashes(path);
1996 cwd = getcwd(NULL, 0);
1997 if (cwd == NULL) {
1998 error = got_error_prefix_errno("getcwd");
1999 goto done;
2001 error = got_worktree_open(&worktree, cwd);
2002 if (error)
2003 goto done;
2005 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2006 if (error != NULL)
2007 goto done;
2009 error = apply_unveil(got_repo_get_path(repo), 1,
2010 got_worktree_get_root_path(worktree), 0);
2011 if (error)
2012 goto done;
2014 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2015 print_status, NULL, repo);
2016 if (error)
2017 goto done;
2018 done:
2019 if (repo)
2020 got_repo_close(repo);
2021 if (worktree)
2022 got_worktree_close(worktree);
2023 free(path);
2024 free(cwd);
2025 return error;
2028 __dead static void
2029 usage_revert(void)
2031 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2032 exit(1);
2035 static void
2036 revert_progress(void *arg, unsigned char status, const char *path)
2038 while (path[0] == '/')
2039 path++;
2040 printf("%c %s\n", status, path);
2043 static const struct got_error *
2044 cmd_revert(int argc, char *argv[])
2046 const struct got_error *error = NULL;
2047 struct got_worktree *worktree = NULL;
2048 struct got_repository *repo = NULL;
2049 char *cwd = NULL, *path = NULL;
2050 int ch;
2052 while ((ch = getopt(argc, argv, "")) != -1) {
2053 switch (ch) {
2054 default:
2055 usage_revert();
2056 /* NOTREACHED */
2060 argc -= optind;
2061 argv += optind;
2063 if (argc != 1)
2064 usage_revert();
2066 path = realpath(argv[0], NULL);
2067 if (path == NULL) {
2068 error = got_error_prefix_errno2("realpath", argv[0]);
2069 goto done;
2071 got_path_strip_trailing_slashes(path);
2073 cwd = getcwd(NULL, 0);
2074 if (cwd == NULL) {
2075 error = got_error_prefix_errno("getcwd");
2076 goto done;
2078 error = got_worktree_open(&worktree, cwd);
2079 if (error)
2080 goto done;
2082 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2083 if (error != NULL)
2084 goto done;
2086 error = apply_unveil(got_repo_get_path(repo), 1,
2087 got_worktree_get_root_path(worktree), 0);
2088 if (error)
2089 goto done;
2091 error = got_worktree_revert(worktree, path,
2092 revert_progress, NULL, repo);
2093 if (error)
2094 goto done;
2095 done:
2096 if (repo)
2097 got_repo_close(repo);
2098 if (worktree)
2099 got_worktree_close(worktree);
2100 free(path);
2101 free(cwd);
2102 return error;
2105 __dead static void
2106 usage_commit(void)
2108 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2109 exit(1);
2112 static const struct got_error *
2113 cmd_commit(int argc, char *argv[])
2115 const struct got_error *error = NULL;
2116 struct got_worktree *worktree = NULL;
2117 struct got_repository *repo = NULL;
2118 char *cwd = NULL, *path = NULL, *id_str = NULL;
2119 struct got_object_id *id = NULL;
2120 const char *logmsg = "<no log message was specified>";
2121 const char *got_author = getenv("GOT_AUTHOR");
2122 int ch;
2124 while ((ch = getopt(argc, argv, "m:")) != -1) {
2125 switch (ch) {
2126 case 'm':
2127 logmsg = optarg;
2128 break;
2129 default:
2130 usage_commit();
2131 /* NOTREACHED */
2135 argc -= optind;
2136 argv += optind;
2138 if (argc == 1) {
2139 path = realpath(argv[0], NULL);
2140 if (path == NULL) {
2141 error = got_error_prefix_errno2("realpath", argv[0]);
2142 goto done;
2144 got_path_strip_trailing_slashes(path);
2145 } else if (argc != 0)
2146 usage_commit();
2148 if (got_author == NULL) {
2149 /* TODO: Look current user up in password database */
2150 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2151 goto done;
2154 cwd = getcwd(NULL, 0);
2155 if (cwd == NULL) {
2156 error = got_error_prefix_errno("getcwd");
2157 goto done;
2159 error = got_worktree_open(&worktree, cwd);
2160 if (error)
2161 goto done;
2163 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2164 if (error != NULL)
2165 goto done;
2167 error = apply_unveil(got_repo_get_path(repo), 0,
2168 got_worktree_get_root_path(worktree), 0);
2169 if (error)
2170 goto done;
2172 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2173 logmsg, print_status, NULL, repo);
2174 if (error)
2175 goto done;
2177 error = got_object_id_str(&id_str, id);
2178 if (error)
2179 goto done;
2180 printf("created commit %s\n", id_str);
2181 done:
2182 if (repo)
2183 got_repo_close(repo);
2184 if (worktree)
2185 got_worktree_close(worktree);
2186 free(path);
2187 free(cwd);
2188 free(id_str);
2189 return error;