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>
22 #include <sys/param.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <locale.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <libgen.h>
33 #include <time.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
39 #include "got_path.h"
40 #include "got_worktree.h"
41 #include "got_diff.h"
42 #include "got_commit_graph.h"
43 #include "got_blame.h"
44 #include "got_privsep.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 #endif
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
53 static void
54 catch_sigint(int signo)
55 {
56 sigint_received = 1;
57 }
59 static void
60 catch_sigpipe(int signo)
61 {
62 sigpipe_received = 1;
63 }
66 struct cmd {
67 const char *cmd_name;
68 const struct got_error *(*cmd_main)(int, char *[]);
69 void (*cmd_usage)(void);
70 const char *cmd_descr;
71 };
73 __dead static void usage(void);
74 __dead static void usage_checkout(void);
75 __dead static void usage_update(void);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
80 __dead static void usage_status(void);
81 __dead static void usage_ref(void);
82 __dead static void usage_add(void);
83 __dead static void usage_rm(void);
84 __dead static void usage_revert(void);
85 __dead static void usage_commit(void);
87 static const struct got_error* cmd_checkout(int, char *[]);
88 static const struct got_error* cmd_update(int, char *[]);
89 static const struct got_error* cmd_log(int, char *[]);
90 static const struct got_error* cmd_diff(int, char *[]);
91 static const struct got_error* cmd_blame(int, char *[]);
92 static const struct got_error* cmd_tree(int, char *[]);
93 static const struct got_error* cmd_status(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
95 static const struct got_error* cmd_add(int, char *[]);
96 static const struct got_error* cmd_rm(int, char *[]);
97 static const struct got_error* cmd_revert(int, char *[]);
98 static const struct got_error* cmd_commit(int, char *[]);
100 static struct cmd got_commands[] = {
101 { "checkout", cmd_checkout, usage_checkout,
102 "check out a new work tree from a repository" },
103 { "update", cmd_update, usage_update,
104 "update a work tree to a different commit" },
105 { "log", cmd_log, usage_log,
106 "show repository history" },
107 { "diff", cmd_diff, usage_diff,
108 "compare files and directories" },
109 { "blame", cmd_blame, usage_blame,
110 "show when lines in a file were changed" },
111 { "tree", cmd_tree, usage_tree,
112 "list files and directories in repository" },
113 { "status", cmd_status, usage_status,
114 "show modification status of files" },
115 { "ref", cmd_ref, usage_ref,
116 "manage references in repository" },
117 { "add", cmd_add, usage_add,
118 "add a new file to version control" },
119 { "rm", cmd_rm, usage_rm,
120 "remove a versioned file" },
121 { "revert", cmd_revert, usage_revert,
122 "revert uncommitted changes" },
123 { "commit", cmd_commit, usage_commit,
124 "write changes from work tree to repository" },
125 };
127 int
128 main(int argc, char *argv[])
130 struct cmd *cmd;
131 unsigned int i;
132 int ch;
133 int hflag = 0;
135 setlocale(LC_CTYPE, "");
137 while ((ch = getopt(argc, argv, "h")) != -1) {
138 switch (ch) {
139 case 'h':
140 hflag = 1;
141 break;
142 default:
143 usage();
144 /* NOTREACHED */
148 argc -= optind;
149 argv += optind;
150 optind = 0;
152 if (argc <= 0)
153 usage();
155 signal(SIGINT, catch_sigint);
156 signal(SIGPIPE, catch_sigpipe);
158 for (i = 0; i < nitems(got_commands); i++) {
159 const struct got_error *error;
161 cmd = &got_commands[i];
163 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
164 continue;
166 if (hflag)
167 got_commands[i].cmd_usage();
169 error = got_commands[i].cmd_main(argc, argv);
170 if (error && !(sigint_received || sigpipe_received)) {
171 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
172 return 1;
175 return 0;
178 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
179 return 1;
182 __dead static void
183 usage(void)
185 int i;
187 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
188 "Available commands:\n", getprogname());
189 for (i = 0; i < nitems(got_commands); i++) {
190 struct cmd *cmd = &got_commands[i];
191 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
193 exit(1);
196 static const struct got_error *
197 apply_unveil(const char *repo_path, int repo_read_only,
198 const char *worktree_path, int create_worktree)
200 const struct got_error *error;
201 static char err_msg[MAXPATHLEN + 36];
203 if (create_worktree) {
204 /* Pre-create work tree path to avoid unveiling its parents. */
205 error = got_path_mkdir(worktree_path);
207 if (errno == EEXIST) {
208 if (got_path_dir_is_empty(worktree_path)) {
209 errno = 0;
210 error = NULL;
211 } else {
212 snprintf(err_msg, sizeof(err_msg),
213 "%s: directory exists but is not empty",
214 worktree_path);
215 error = got_error_msg(GOT_ERR_BAD_PATH,
216 err_msg);
220 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
221 return error;
224 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
225 return got_error_prefix_errno2("unveil", repo_path);
227 if (worktree_path && unveil(worktree_path, "rwc") != 0)
228 return got_error_prefix_errno2("unveil", worktree_path);
230 if (unveil("/tmp", "rwc") != 0)
231 return got_error_prefix_errno2("unveil", "/tmp");
233 error = got_privsep_unveil_exec_helpers();
234 if (error != NULL)
235 return error;
237 if (unveil(NULL, NULL) != 0)
238 return got_error_prefix_errno("unveil");
240 return NULL;
243 __dead static void
244 usage_checkout(void)
246 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
247 "[worktree-path]\n", getprogname());
248 exit(1);
251 static void
252 checkout_progress(void *arg, unsigned char status, const char *path)
254 char *worktree_path = arg;
256 while (path[0] == '/')
257 path++;
259 printf("%c %s/%s\n", status, worktree_path, path);
262 static const struct got_error *
263 check_cancelled(void *arg)
265 if (sigint_received || sigpipe_received)
266 return got_error(GOT_ERR_CANCELLED);
267 return NULL;
270 static const struct got_error *
271 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
272 struct got_repository *repo)
274 const struct got_error *err;
275 struct got_reference *head_ref = NULL;
276 struct got_object_id *head_commit_id = NULL;
277 struct got_commit_graph *graph = NULL;
279 err = got_ref_open(&head_ref, repo,
280 got_worktree_get_head_ref_name(worktree), 0);
281 if (err)
282 return err;
284 /* TODO: Check the reflog. The head ref may have been rebased. */
285 err = got_ref_resolve(&head_commit_id, repo, head_ref);
286 if (err)
287 goto done;
289 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
290 if (err)
291 goto done;
293 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
294 if (err)
295 goto done;
296 for (;;) {
297 struct got_object_id *id;
299 if (sigint_received || sigpipe_received)
300 break;
302 err = got_commit_graph_iter_next(&id, graph);
303 if (err) {
304 if (err->code == GOT_ERR_ITER_COMPLETED) {
305 err = got_error(GOT_ERR_ANCESTRY);
306 break;
308 if (err->code != GOT_ERR_ITER_NEED_MORE)
309 break;
310 err = got_commit_graph_fetch_commits(graph, 1, repo);
311 if (err)
312 break;
313 else
314 continue;
316 if (id == NULL)
317 break;
318 if (got_object_id_cmp(id, commit_id) == 0)
319 break;
321 done:
322 if (head_ref)
323 got_ref_close(head_ref);
324 if (graph)
325 got_commit_graph_close(graph);
326 return err;
330 static const struct got_error *
331 cmd_checkout(int argc, char *argv[])
333 const struct got_error *error = NULL;
334 struct got_repository *repo = NULL;
335 struct got_reference *head_ref = NULL;
336 struct got_worktree *worktree = NULL;
337 char *repo_path = NULL;
338 char *worktree_path = NULL;
339 const char *path_prefix = "";
340 char *commit_id_str = NULL;
341 int ch, same_path_prefix;
343 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
344 switch (ch) {
345 case 'c':
346 commit_id_str = strdup(optarg);
347 if (commit_id_str == NULL)
348 return got_error_prefix_errno("strdup");
349 break;
350 case 'p':
351 path_prefix = optarg;
352 break;
353 default:
354 usage_checkout();
355 /* NOTREACHED */
359 argc -= optind;
360 argv += optind;
362 #ifndef PROFILE
363 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
364 "unveil", NULL) == -1)
365 err(1, "pledge");
366 #endif
367 if (argc == 1) {
368 char *cwd, *base, *dotgit;
369 repo_path = realpath(argv[0], NULL);
370 if (repo_path == NULL)
371 return got_error_prefix_errno2("realpath", argv[0]);
372 cwd = getcwd(NULL, 0);
373 if (cwd == NULL) {
374 error = got_error_prefix_errno("getcwd");
375 goto done;
377 if (path_prefix[0]) {
378 base = basename(path_prefix);
379 if (base == NULL) {
380 error = got_error_prefix_errno2("basename",
381 path_prefix);
382 goto done;
384 } else {
385 base = basename(repo_path);
386 if (base == NULL) {
387 error = got_error_prefix_errno2("basename",
388 repo_path);
389 goto done;
392 dotgit = strstr(base, ".git");
393 if (dotgit)
394 *dotgit = '\0';
395 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
396 error = got_error_prefix_errno("asprintf");
397 free(cwd);
398 goto done;
400 free(cwd);
401 } else if (argc == 2) {
402 repo_path = realpath(argv[0], NULL);
403 if (repo_path == NULL) {
404 error = got_error_prefix_errno2("realpath", argv[0]);
405 goto done;
407 worktree_path = realpath(argv[1], NULL);
408 if (worktree_path == NULL) {
409 error = got_error_prefix_errno2("realpath", argv[1]);
410 goto done;
412 } else
413 usage_checkout();
415 got_path_strip_trailing_slashes(repo_path);
416 got_path_strip_trailing_slashes(worktree_path);
418 error = got_repo_open(&repo, repo_path);
419 if (error != NULL)
420 goto done;
422 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
423 if (error)
424 goto done;
426 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
427 if (error != NULL)
428 goto done;
430 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
431 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
432 goto done;
434 error = got_worktree_open(&worktree, worktree_path);
435 if (error != NULL)
436 goto done;
438 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
439 path_prefix);
440 if (error != NULL)
441 goto done;
442 if (!same_path_prefix) {
443 error = got_error(GOT_ERR_PATH_PREFIX);
444 goto done;
447 if (commit_id_str) {
448 struct got_object_id *commit_id;
449 error = got_object_resolve_id_str(&commit_id, repo,
450 commit_id_str);
451 if (error != NULL)
452 goto done;
453 error = check_ancestry(worktree, commit_id, repo);
454 if (error != NULL) {
455 free(commit_id);
456 goto done;
458 error = got_worktree_set_base_commit_id(worktree, repo,
459 commit_id);
460 free(commit_id);
461 if (error)
462 goto done;
465 error = got_worktree_checkout_files(worktree, "", repo,
466 checkout_progress, worktree_path, check_cancelled, NULL);
467 if (error != NULL)
468 goto done;
470 printf("Now shut up and hack\n");
472 done:
473 free(commit_id_str);
474 free(repo_path);
475 free(worktree_path);
476 return error;
479 __dead static void
480 usage_update(void)
482 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
483 getprogname());
484 exit(1);
487 static void
488 update_progress(void *arg, unsigned char status, const char *path)
490 int *did_something = arg;
492 if (status == GOT_STATUS_EXISTS)
493 return;
495 *did_something = 1;
496 while (path[0] == '/')
497 path++;
498 printf("%c %s\n", status, path);
501 static const struct got_error *
502 cmd_update(int argc, char *argv[])
504 const struct got_error *error = NULL;
505 struct got_repository *repo = NULL;
506 struct got_worktree *worktree = NULL;
507 char *worktree_path = NULL, *path = NULL;
508 struct got_object_id *commit_id = NULL;
509 char *commit_id_str = NULL;
510 int ch, did_something = 0;
512 while ((ch = getopt(argc, argv, "c:")) != -1) {
513 switch (ch) {
514 case 'c':
515 commit_id_str = strdup(optarg);
516 if (commit_id_str == NULL)
517 return got_error_prefix_errno("strdup");
518 break;
519 default:
520 usage_update();
521 /* NOTREACHED */
525 argc -= optind;
526 argv += optind;
528 #ifndef PROFILE
529 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
530 "unveil", NULL) == -1)
531 err(1, "pledge");
532 #endif
533 worktree_path = getcwd(NULL, 0);
534 if (worktree_path == NULL) {
535 error = got_error_prefix_errno("getcwd");
536 goto done;
538 error = got_worktree_open(&worktree, worktree_path);
539 if (error)
540 goto done;
542 if (argc == 0) {
543 path = strdup("");
544 if (path == NULL) {
545 error = got_error_prefix_errno("strdup");
546 goto done;
548 } else if (argc == 1) {
549 error = got_worktree_resolve_path(&path, worktree, argv[0]);
550 if (error)
551 goto done;
552 } else
553 usage_update();
555 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
556 if (error != NULL)
557 goto done;
559 error = apply_unveil(got_repo_get_path(repo), 0,
560 got_worktree_get_root_path(worktree), 0);
561 if (error)
562 goto done;
564 if (commit_id_str == NULL) {
565 struct got_reference *head_ref;
566 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
567 if (error != NULL)
568 goto done;
569 error = got_ref_resolve(&commit_id, repo, head_ref);
570 if (error != NULL)
571 goto done;
572 error = got_object_id_str(&commit_id_str, commit_id);
573 if (error != NULL)
574 goto done;
575 } else {
576 error = got_object_resolve_id_str(&commit_id, repo,
577 commit_id_str);
578 if (error != NULL)
579 goto done;
582 error = check_ancestry(worktree, commit_id, repo);
583 if (error != NULL)
584 goto done;
586 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
587 commit_id) != 0) {
588 error = got_worktree_set_base_commit_id(worktree, repo,
589 commit_id);
590 if (error)
591 goto done;
594 error = got_worktree_checkout_files(worktree, path, repo,
595 update_progress, &did_something, check_cancelled, NULL);
596 if (error != NULL)
597 goto done;
599 if (did_something)
600 printf("Updated to commit %s\n", commit_id_str);
601 else
602 printf("Already up-to-date\n");
603 done:
604 free(worktree_path);
605 free(path);
606 free(commit_id);
607 free(commit_id_str);
608 return error;
611 static const struct got_error *
612 print_patch(struct got_commit_object *commit, struct got_object_id *id,
613 int diff_context, struct got_repository *repo)
615 const struct got_error *err = NULL;
616 struct got_tree_object *tree1 = NULL, *tree2;
617 struct got_object_qid *qid;
618 char *id_str1 = NULL, *id_str2;
620 err = got_object_open_as_tree(&tree2, repo,
621 got_object_commit_get_tree_id(commit));
622 if (err)
623 return err;
625 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
626 if (qid != NULL) {
627 struct got_commit_object *pcommit;
629 err = got_object_open_as_commit(&pcommit, repo, qid->id);
630 if (err)
631 return err;
633 err = got_object_open_as_tree(&tree1, repo,
634 got_object_commit_get_tree_id(pcommit));
635 got_object_commit_close(pcommit);
636 if (err)
637 return err;
639 err = got_object_id_str(&id_str1, qid->id);
640 if (err)
641 return err;
644 err = got_object_id_str(&id_str2, id);
645 if (err)
646 goto done;
648 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
649 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
650 done:
651 if (tree1)
652 got_object_tree_close(tree1);
653 got_object_tree_close(tree2);
654 free(id_str1);
655 free(id_str2);
656 return err;
659 static char *
660 get_datestr(time_t *time, char *datebuf)
662 char *p, *s = ctime_r(time, datebuf);
663 p = strchr(s, '\n');
664 if (p)
665 *p = '\0';
666 return s;
669 static const struct got_error *
670 print_commit(struct got_commit_object *commit, struct got_object_id *id,
671 struct got_repository *repo, int show_patch, int diff_context,
672 struct got_reflist_head *refs)
674 const struct got_error *err = NULL;
675 char *id_str, *datestr, *logmsg0, *logmsg, *line;
676 char datebuf[26];
677 time_t committer_time;
678 const char *author, *committer;
679 char *refs_str = NULL;
680 struct got_reflist_entry *re;
682 SIMPLEQ_FOREACH(re, refs, entry) {
683 char *s;
684 const char *name;
685 if (got_object_id_cmp(re->id, id) != 0)
686 continue;
687 name = got_ref_get_name(re->ref);
688 if (strcmp(name, GOT_REF_HEAD) == 0)
689 continue;
690 if (strncmp(name, "refs/", 5) == 0)
691 name += 5;
692 if (strncmp(name, "got/", 4) == 0)
693 continue;
694 if (strncmp(name, "heads/", 6) == 0)
695 name += 6;
696 if (strncmp(name, "remotes/", 8) == 0)
697 name += 8;
698 s = refs_str;
699 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
700 name) == -1) {
701 err = got_error_prefix_errno("asprintf");
702 free(s);
703 break;
705 free(s);
707 err = got_object_id_str(&id_str, id);
708 if (err)
709 return err;
711 printf("-----------------------------------------------\n");
712 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
713 refs_str ? refs_str : "", refs_str ? ")" : "");
714 free(id_str);
715 id_str = NULL;
716 free(refs_str);
717 refs_str = NULL;
718 printf("from: %s\n", got_object_commit_get_author(commit));
719 committer_time = got_object_commit_get_committer_time(commit);
720 datestr = get_datestr(&committer_time, datebuf);
721 printf("date: %s UTC\n", datestr);
722 author = got_object_commit_get_author(commit);
723 committer = got_object_commit_get_committer(commit);
724 if (strcmp(author, committer) != 0)
725 printf("via: %s\n", committer);
726 if (got_object_commit_get_nparents(commit) > 1) {
727 const struct got_object_id_queue *parent_ids;
728 struct got_object_qid *qid;
729 int n = 1;
730 parent_ids = got_object_commit_get_parent_ids(commit);
731 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
732 err = got_object_id_str(&id_str, qid->id);
733 if (err)
734 return err;
735 printf("parent %d: %s\n", n++, id_str);
736 free(id_str);
740 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
741 if (logmsg0 == NULL)
742 return got_error_prefix_errno("strdup");
744 logmsg = logmsg0;
745 do {
746 line = strsep(&logmsg, "\n");
747 if (line)
748 printf(" %s\n", line);
749 } while (line);
750 free(logmsg0);
752 if (show_patch) {
753 err = print_patch(commit, id, diff_context, repo);
754 if (err == 0)
755 printf("\n");
758 if (fflush(stdout) != 0 && err == NULL)
759 err = got_error_prefix_errno("fflush");
760 return err;
763 static const struct got_error *
764 print_commits(struct got_object_id *root_id, struct got_repository *repo,
765 char *path, int show_patch, int diff_context, int limit,
766 int first_parent_traversal, struct got_reflist_head *refs)
768 const struct got_error *err;
769 struct got_commit_graph *graph;
771 err = got_commit_graph_open(&graph, root_id, path,
772 first_parent_traversal, repo);
773 if (err)
774 return err;
775 err = got_commit_graph_iter_start(graph, root_id, repo);
776 if (err)
777 goto done;
778 for (;;) {
779 struct got_commit_object *commit;
780 struct got_object_id *id;
782 if (sigint_received || sigpipe_received)
783 break;
785 err = got_commit_graph_iter_next(&id, graph);
786 if (err) {
787 if (err->code == GOT_ERR_ITER_COMPLETED) {
788 err = NULL;
789 break;
791 if (err->code != GOT_ERR_ITER_NEED_MORE)
792 break;
793 err = got_commit_graph_fetch_commits(graph, 1, repo);
794 if (err)
795 break;
796 else
797 continue;
799 if (id == NULL)
800 break;
802 err = got_object_open_as_commit(&commit, repo, id);
803 if (err)
804 break;
805 err = print_commit(commit, id, repo, show_patch, diff_context,
806 refs);
807 got_object_commit_close(commit);
808 if (err || (limit && --limit == 0))
809 break;
811 done:
812 got_commit_graph_close(graph);
813 return err;
816 __dead static void
817 usage_log(void)
819 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
820 "[-r repository-path] [path]\n", getprogname());
821 exit(1);
824 static const struct got_error *
825 cmd_log(int argc, char *argv[])
827 const struct got_error *error;
828 struct got_repository *repo = NULL;
829 struct got_worktree *worktree = NULL;
830 struct got_commit_object *commit = NULL;
831 struct got_object_id *id = NULL;
832 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
833 char *start_commit = NULL;
834 int diff_context = 3, ch;
835 int show_patch = 0, limit = 0, first_parent_traversal = 0;
836 const char *errstr;
837 struct got_reflist_head refs;
839 SIMPLEQ_INIT(&refs);
841 #ifndef PROFILE
842 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
843 NULL)
844 == -1)
845 err(1, "pledge");
846 #endif
848 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
849 switch (ch) {
850 case 'p':
851 show_patch = 1;
852 break;
853 case 'c':
854 start_commit = optarg;
855 break;
856 case 'C':
857 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
858 &errstr);
859 if (errstr != NULL)
860 err(1, "-C option %s", errstr);
861 break;
862 case 'l':
863 limit = strtonum(optarg, 1, INT_MAX, &errstr);
864 if (errstr != NULL)
865 err(1, "-l option %s", errstr);
866 break;
867 case 'f':
868 first_parent_traversal = 1;
869 break;
870 case 'r':
871 repo_path = realpath(optarg, NULL);
872 if (repo_path == NULL)
873 err(1, "-r option");
874 got_path_strip_trailing_slashes(repo_path);
875 break;
876 default:
877 usage_log();
878 /* NOTREACHED */
882 argc -= optind;
883 argv += optind;
885 cwd = getcwd(NULL, 0);
886 if (cwd == NULL) {
887 error = got_error_prefix_errno("getcwd");
888 goto done;
891 error = got_worktree_open(&worktree, cwd);
892 if (error && error->code != GOT_ERR_NOT_WORKTREE)
893 goto done;
894 error = NULL;
896 if (argc == 0) {
897 path = strdup("");
898 if (path == NULL) {
899 error = got_error_prefix_errno("strdup");
900 goto done;
902 } else if (argc == 1) {
903 if (worktree) {
904 error = got_worktree_resolve_path(&path, worktree,
905 argv[0]);
906 if (error)
907 goto done;
908 } else {
909 path = strdup(argv[0]);
910 if (path == NULL) {
911 error = got_error_prefix_errno("strdup");
912 goto done;
915 } else
916 usage_log();
918 if (repo_path == NULL) {
919 repo_path = worktree ?
920 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
922 if (repo_path == NULL) {
923 error = got_error_prefix_errno("strdup");
924 goto done;
927 error = got_repo_open(&repo, repo_path);
928 if (error != NULL)
929 goto done;
931 error = apply_unveil(got_repo_get_path(repo), 1,
932 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
933 if (error)
934 goto done;
936 if (start_commit == NULL) {
937 struct got_reference *head_ref;
938 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
939 if (error != NULL)
940 return error;
941 error = got_ref_resolve(&id, repo, head_ref);
942 got_ref_close(head_ref);
943 if (error != NULL)
944 return error;
945 error = got_object_open_as_commit(&commit, repo, id);
946 } else {
947 struct got_reference *ref;
948 error = got_ref_open(&ref, repo, start_commit, 0);
949 if (error == NULL) {
950 int obj_type;
951 error = got_ref_resolve(&id, repo, ref);
952 got_ref_close(ref);
953 if (error != NULL)
954 goto done;
955 error = got_object_get_type(&obj_type, repo, id);
956 if (error != NULL)
957 goto done;
958 if (obj_type == GOT_OBJ_TYPE_TAG) {
959 struct got_tag_object *tag;
960 error = got_object_open_as_tag(&tag, repo, id);
961 if (error != NULL)
962 goto done;
963 if (got_object_tag_get_object_type(tag) !=
964 GOT_OBJ_TYPE_COMMIT) {
965 got_object_tag_close(tag);
966 error = got_error(GOT_ERR_OBJ_TYPE);
967 goto done;
969 free(id);
970 id = got_object_id_dup(
971 got_object_tag_get_object_id(tag));
972 if (id == NULL)
973 error = got_error_prefix_errno(
974 "got_object_id_dup");
975 got_object_tag_close(tag);
976 if (error)
977 goto done;
978 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
979 error = got_error(GOT_ERR_OBJ_TYPE);
980 goto done;
982 error = got_object_open_as_commit(&commit, repo, id);
983 if (error != NULL)
984 goto done;
986 if (commit == NULL) {
987 error = got_object_resolve_id_str(&id, repo,
988 start_commit);
989 if (error != NULL)
990 return error;
993 if (error != NULL)
994 goto done;
996 error = got_repo_map_path(&in_repo_path, repo, path, 1);
997 if (error != NULL)
998 goto done;
999 if (in_repo_path) {
1000 free(path);
1001 path = in_repo_path;
1004 error = got_ref_list(&refs, repo);
1005 if (error)
1006 goto done;
1008 error = print_commits(id, repo, path, show_patch,
1009 diff_context, limit, first_parent_traversal, &refs);
1010 done:
1011 free(path);
1012 free(repo_path);
1013 free(cwd);
1014 free(id);
1015 if (worktree)
1016 got_worktree_close(worktree);
1017 if (repo) {
1018 const struct got_error *repo_error;
1019 repo_error = got_repo_close(repo);
1020 if (error == NULL)
1021 error = repo_error;
1023 got_ref_list_free(&refs);
1024 return error;
1027 __dead static void
1028 usage_diff(void)
1030 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1031 "[object1 object2 | path]\n", getprogname());
1032 exit(1);
1035 struct print_diff_arg {
1036 struct got_repository *repo;
1037 struct got_worktree *worktree;
1038 int diff_context;
1039 const char *id_str;
1040 int header_shown;
1043 static const struct got_error *
1044 print_diff(void *arg, unsigned char status, const char *path,
1045 struct got_object_id *id)
1047 struct print_diff_arg *a = arg;
1048 const struct got_error *err = NULL;
1049 struct got_blob_object *blob1 = NULL;
1050 FILE *f2 = NULL;
1051 char *abspath = NULL;
1052 struct stat sb;
1054 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1055 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1056 return NULL;
1058 if (!a->header_shown) {
1059 printf("diff %s %s\n", a->id_str,
1060 got_worktree_get_root_path(a->worktree));
1061 a->header_shown = 1;
1064 if (status != GOT_STATUS_ADD) {
1065 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1066 if (err)
1067 goto done;
1071 if (status != GOT_STATUS_DELETE) {
1072 if (asprintf(&abspath, "%s/%s",
1073 got_worktree_get_root_path(a->worktree), path) == -1) {
1074 err = got_error_prefix_errno("asprintf");
1075 goto done;
1078 f2 = fopen(abspath, "r");
1079 if (f2 == NULL) {
1080 err = got_error_prefix_errno2("fopen", abspath);
1081 goto done;
1083 if (lstat(abspath, &sb) == -1) {
1084 err = got_error_prefix_errno2("lstat", abspath);
1085 goto done;
1087 } else
1088 sb.st_size = 0;
1090 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1091 stdout);
1092 done:
1093 if (blob1)
1094 got_object_blob_close(blob1);
1095 if (f2 && fclose(f2) != 0 && err == NULL)
1096 err = got_error_prefix_errno("fclose");
1097 free(abspath);
1098 return err;
1101 static const struct got_error *
1102 cmd_diff(int argc, char *argv[])
1104 const struct got_error *error;
1105 struct got_repository *repo = NULL;
1106 struct got_worktree *worktree = NULL;
1107 char *cwd = NULL, *repo_path = NULL;
1108 struct got_object_id *id1 = NULL, *id2 = NULL;
1109 char *id_str1 = NULL, *id_str2 = NULL;
1110 int type1, type2;
1111 int diff_context = 3, ch;
1112 const char *errstr;
1113 char *path = NULL;
1115 #ifndef PROFILE
1116 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1117 NULL) == -1)
1118 err(1, "pledge");
1119 #endif
1121 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1122 switch (ch) {
1123 case 'C':
1124 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1125 if (errstr != NULL)
1126 err(1, "-C option %s", errstr);
1127 break;
1128 case 'r':
1129 repo_path = realpath(optarg, NULL);
1130 if (repo_path == NULL)
1131 err(1, "-r option");
1132 got_path_strip_trailing_slashes(repo_path);
1133 break;
1134 default:
1135 usage_diff();
1136 /* NOTREACHED */
1140 argc -= optind;
1141 argv += optind;
1143 cwd = getcwd(NULL, 0);
1144 if (cwd == NULL) {
1145 error = got_error_prefix_errno("getcwd");
1146 goto done;
1148 error = got_worktree_open(&worktree, cwd);
1149 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1150 goto done;
1151 if (argc <= 1) {
1152 if (worktree == NULL) {
1153 error = got_error(GOT_ERR_NOT_WORKTREE);
1154 goto done;
1156 if (repo_path)
1157 errx(1,
1158 "-r option can't be used when diffing a work tree");
1159 repo_path = strdup(got_worktree_get_repo_path(worktree));
1160 if (repo_path == NULL) {
1161 error = got_error_prefix_errno("strdup");
1162 goto done;
1164 if (argc == 1) {
1165 error = got_worktree_resolve_path(&path, worktree,
1166 argv[0]);
1167 if (error)
1168 goto done;
1169 } else {
1170 path = strdup("");
1171 if (path == NULL) {
1172 error = got_error_prefix_errno("strdup");
1173 goto done;
1176 } else if (argc == 2) {
1177 id_str1 = argv[0];
1178 id_str2 = argv[1];
1179 } else
1180 usage_diff();
1182 if (repo_path == NULL) {
1183 repo_path = getcwd(NULL, 0);
1184 if (repo_path == NULL)
1185 return got_error_prefix_errno("getcwd");
1188 error = got_repo_open(&repo, repo_path);
1189 free(repo_path);
1190 if (error != NULL)
1191 goto done;
1193 error = apply_unveil(got_repo_get_path(repo), 1,
1194 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1195 if (error)
1196 goto done;
1198 if (worktree) {
1199 struct print_diff_arg arg;
1200 char *id_str;
1201 error = got_object_id_str(&id_str,
1202 got_worktree_get_base_commit_id(worktree));
1203 if (error)
1204 goto done;
1205 arg.repo = repo;
1206 arg.worktree = worktree;
1207 arg.diff_context = diff_context;
1208 arg.id_str = id_str;
1209 arg.header_shown = 0;
1211 error = got_worktree_status(worktree, path, repo, print_diff,
1212 &arg, check_cancelled, NULL);
1213 free(id_str);
1214 goto done;
1217 error = got_object_resolve_id_str(&id1, repo, id_str1);
1218 if (error)
1219 goto done;
1221 error = got_object_resolve_id_str(&id2, repo, id_str2);
1222 if (error)
1223 goto done;
1225 error = got_object_get_type(&type1, repo, id1);
1226 if (error)
1227 goto done;
1229 error = got_object_get_type(&type2, repo, id2);
1230 if (error)
1231 goto done;
1233 if (type1 != type2) {
1234 error = got_error(GOT_ERR_OBJ_TYPE);
1235 goto done;
1238 switch (type1) {
1239 case GOT_OBJ_TYPE_BLOB:
1240 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1241 diff_context, repo, stdout);
1242 break;
1243 case GOT_OBJ_TYPE_TREE:
1244 error = got_diff_objects_as_trees(id1, id2, "", "",
1245 diff_context, repo, stdout);
1246 break;
1247 case GOT_OBJ_TYPE_COMMIT:
1248 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1249 id_str2);
1250 error = got_diff_objects_as_commits(id1, id2, diff_context,
1251 repo, stdout);
1252 break;
1253 default:
1254 error = got_error(GOT_ERR_OBJ_TYPE);
1257 done:
1258 free(id1);
1259 free(id2);
1260 free(path);
1261 if (worktree)
1262 got_worktree_close(worktree);
1263 if (repo) {
1264 const struct got_error *repo_error;
1265 repo_error = got_repo_close(repo);
1266 if (error == NULL)
1267 error = repo_error;
1269 return error;
1272 __dead static void
1273 usage_blame(void)
1275 fprintf(stderr,
1276 "usage: %s blame [-c commit] [-r repository-path] path\n",
1277 getprogname());
1278 exit(1);
1281 static const struct got_error *
1282 cmd_blame(int argc, char *argv[])
1284 const struct got_error *error;
1285 struct got_repository *repo = NULL;
1286 struct got_worktree *worktree = NULL;
1287 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1288 struct got_object_id *commit_id = NULL;
1289 char *commit_id_str = NULL;
1290 int ch;
1292 #ifndef PROFILE
1293 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1294 NULL) == -1)
1295 err(1, "pledge");
1296 #endif
1298 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1299 switch (ch) {
1300 case 'c':
1301 commit_id_str = optarg;
1302 break;
1303 case 'r':
1304 repo_path = realpath(optarg, NULL);
1305 if (repo_path == NULL)
1306 err(1, "-r option");
1307 got_path_strip_trailing_slashes(repo_path);
1308 break;
1309 default:
1310 usage_blame();
1311 /* NOTREACHED */
1315 argc -= optind;
1316 argv += optind;
1318 if (argc == 1)
1319 path = argv[0];
1320 else
1321 usage_blame();
1323 cwd = getcwd(NULL, 0);
1324 if (cwd == NULL) {
1325 error = got_error_prefix_errno("getcwd");
1326 goto done;
1328 if (repo_path == NULL) {
1329 error = got_worktree_open(&worktree, cwd);
1330 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1331 goto done;
1332 else
1333 error = NULL;
1334 if (worktree) {
1335 repo_path =
1336 strdup(got_worktree_get_repo_path(worktree));
1337 if (repo_path == NULL)
1338 error = got_error_prefix_errno("strdup");
1339 if (error)
1340 goto done;
1341 } else {
1342 repo_path = strdup(cwd);
1343 if (repo_path == NULL) {
1344 error = got_error_prefix_errno("strdup");
1345 goto done;
1350 error = got_repo_open(&repo, repo_path);
1351 if (error != NULL)
1352 goto done;
1354 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1355 if (error)
1356 goto done;
1358 if (worktree) {
1359 const char *prefix = got_worktree_get_path_prefix(worktree);
1360 char *p, *worktree_subdir = cwd +
1361 strlen(got_worktree_get_root_path(worktree));
1362 if (asprintf(&p, "%s%s%s%s%s",
1363 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1364 worktree_subdir, worktree_subdir[0] ? "/" : "",
1365 path) == -1) {
1366 error = got_error_prefix_errno("asprintf");
1367 goto done;
1369 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1370 free(p);
1371 } else {
1372 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1374 if (error)
1375 goto done;
1377 if (commit_id_str == NULL) {
1378 struct got_reference *head_ref;
1379 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1380 if (error != NULL)
1381 goto done;
1382 error = got_ref_resolve(&commit_id, repo, head_ref);
1383 got_ref_close(head_ref);
1384 if (error != NULL)
1385 goto done;
1386 } else {
1387 error = got_object_resolve_id_str(&commit_id, repo,
1388 commit_id_str);
1389 if (error != NULL)
1390 goto done;
1393 error = got_blame(in_repo_path, commit_id, repo, stdout);
1394 done:
1395 free(in_repo_path);
1396 free(repo_path);
1397 free(cwd);
1398 free(commit_id);
1399 if (worktree)
1400 got_worktree_close(worktree);
1401 if (repo) {
1402 const struct got_error *repo_error;
1403 repo_error = got_repo_close(repo);
1404 if (error == NULL)
1405 error = repo_error;
1407 return error;
1410 __dead static void
1411 usage_tree(void)
1413 fprintf(stderr,
1414 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1415 getprogname());
1416 exit(1);
1419 static void
1420 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1421 const char *root_path)
1423 int is_root_path = (strcmp(path, root_path) == 0);
1425 path += strlen(root_path);
1426 while (path[0] == '/')
1427 path++;
1429 printf("%s%s%s%s%s\n", id ? id : "", path,
1430 is_root_path ? "" : "/", te->name,
1431 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1434 static const struct got_error *
1435 print_tree(const char *path, struct got_object_id *commit_id,
1436 int show_ids, int recurse, const char *root_path,
1437 struct got_repository *repo)
1439 const struct got_error *err = NULL;
1440 struct got_object_id *tree_id = NULL;
1441 struct got_tree_object *tree = NULL;
1442 const struct got_tree_entries *entries;
1443 struct got_tree_entry *te;
1445 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1446 if (err)
1447 goto done;
1449 err = got_object_open_as_tree(&tree, repo, tree_id);
1450 if (err)
1451 goto done;
1452 entries = got_object_tree_get_entries(tree);
1453 te = SIMPLEQ_FIRST(&entries->head);
1454 while (te) {
1455 char *id = NULL;
1457 if (sigint_received || sigpipe_received)
1458 break;
1460 if (show_ids) {
1461 char *id_str;
1462 err = got_object_id_str(&id_str, te->id);
1463 if (err)
1464 goto done;
1465 if (asprintf(&id, "%s ", id_str) == -1) {
1466 err = got_error_prefix_errno("asprintf");
1467 free(id_str);
1468 goto done;
1470 free(id_str);
1472 print_entry(te, id, path, root_path);
1473 free(id);
1475 if (recurse && S_ISDIR(te->mode)) {
1476 char *child_path;
1477 if (asprintf(&child_path, "%s%s%s", path,
1478 path[0] == '/' && path[1] == '\0' ? "" : "/",
1479 te->name) == -1) {
1480 err = got_error_prefix_errno("asprintf");
1481 goto done;
1483 err = print_tree(child_path, commit_id, show_ids, 1,
1484 root_path, repo);
1485 free(child_path);
1486 if (err)
1487 goto done;
1490 te = SIMPLEQ_NEXT(te, entry);
1492 done:
1493 if (tree)
1494 got_object_tree_close(tree);
1495 free(tree_id);
1496 return err;
1499 static const struct got_error *
1500 cmd_tree(int argc, char *argv[])
1502 const struct got_error *error;
1503 struct got_repository *repo = NULL;
1504 struct got_worktree *worktree = NULL;
1505 const char *path;
1506 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1507 struct got_object_id *commit_id = NULL;
1508 char *commit_id_str = NULL;
1509 int show_ids = 0, recurse = 0;
1510 int ch;
1512 #ifndef PROFILE
1513 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1514 NULL) == -1)
1515 err(1, "pledge");
1516 #endif
1518 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1519 switch (ch) {
1520 case 'c':
1521 commit_id_str = optarg;
1522 break;
1523 case 'r':
1524 repo_path = realpath(optarg, NULL);
1525 if (repo_path == NULL)
1526 err(1, "-r option");
1527 got_path_strip_trailing_slashes(repo_path);
1528 break;
1529 case 'i':
1530 show_ids = 1;
1531 break;
1532 case 'R':
1533 recurse = 1;
1534 break;
1535 default:
1536 usage_tree();
1537 /* NOTREACHED */
1541 argc -= optind;
1542 argv += optind;
1544 if (argc == 1)
1545 path = argv[0];
1546 else if (argc > 1)
1547 usage_tree();
1548 else
1549 path = NULL;
1551 cwd = getcwd(NULL, 0);
1552 if (cwd == NULL) {
1553 error = got_error_prefix_errno("getcwd");
1554 goto done;
1556 if (repo_path == NULL) {
1557 error = got_worktree_open(&worktree, cwd);
1558 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1559 goto done;
1560 else
1561 error = NULL;
1562 if (worktree) {
1563 repo_path =
1564 strdup(got_worktree_get_repo_path(worktree));
1565 if (repo_path == NULL)
1566 error = got_error_prefix_errno("strdup");
1567 if (error)
1568 goto done;
1569 } else {
1570 repo_path = strdup(cwd);
1571 if (repo_path == NULL) {
1572 error = got_error_prefix_errno("strdup");
1573 goto done;
1578 error = got_repo_open(&repo, repo_path);
1579 if (error != NULL)
1580 goto done;
1582 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1583 if (error)
1584 goto done;
1586 if (path == NULL) {
1587 if (worktree) {
1588 char *p, *worktree_subdir = cwd +
1589 strlen(got_worktree_get_root_path(worktree));
1590 if (asprintf(&p, "%s/%s",
1591 got_worktree_get_path_prefix(worktree),
1592 worktree_subdir) == -1) {
1593 error = got_error_prefix_errno("asprintf");
1594 goto done;
1596 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1597 free(p);
1598 if (error)
1599 goto done;
1600 } else
1601 path = "/";
1603 if (in_repo_path == NULL) {
1604 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1605 if (error != NULL)
1606 goto done;
1609 if (commit_id_str == NULL) {
1610 struct got_reference *head_ref;
1611 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1612 if (error != NULL)
1613 goto done;
1614 error = got_ref_resolve(&commit_id, repo, head_ref);
1615 got_ref_close(head_ref);
1616 if (error != NULL)
1617 goto done;
1618 } else {
1619 error = got_object_resolve_id_str(&commit_id, repo,
1620 commit_id_str);
1621 if (error != NULL)
1622 goto done;
1625 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1626 in_repo_path, repo);
1627 done:
1628 free(in_repo_path);
1629 free(repo_path);
1630 free(cwd);
1631 free(commit_id);
1632 if (worktree)
1633 got_worktree_close(worktree);
1634 if (repo) {
1635 const struct got_error *repo_error;
1636 repo_error = got_repo_close(repo);
1637 if (error == NULL)
1638 error = repo_error;
1640 return error;
1643 __dead static void
1644 usage_status(void)
1646 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1647 exit(1);
1650 static const struct got_error *
1651 print_status(void *arg, unsigned char status, const char *path,
1652 struct got_object_id *id)
1654 printf("%c %s\n", status, path);
1655 return NULL;
1658 static const struct got_error *
1659 cmd_status(int argc, char *argv[])
1661 const struct got_error *error = NULL;
1662 struct got_repository *repo = NULL;
1663 struct got_worktree *worktree = NULL;
1664 char *cwd = NULL, *path = NULL;
1665 int ch;
1667 while ((ch = getopt(argc, argv, "")) != -1) {
1668 switch (ch) {
1669 default:
1670 usage_status();
1671 /* NOTREACHED */
1675 argc -= optind;
1676 argv += optind;
1678 #ifndef PROFILE
1679 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1680 NULL) == -1)
1681 err(1, "pledge");
1682 #endif
1683 cwd = getcwd(NULL, 0);
1684 if (cwd == NULL) {
1685 error = got_error_prefix_errno("getcwd");
1686 goto done;
1689 error = got_worktree_open(&worktree, cwd);
1690 if (error != NULL)
1691 goto done;
1693 if (argc == 0) {
1694 path = strdup("");
1695 if (path == NULL) {
1696 error = got_error_prefix_errno("strdup");
1697 goto done;
1699 } else if (argc == 1) {
1700 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1701 if (error)
1702 goto done;
1703 } else
1704 usage_status();
1706 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1707 if (error != NULL)
1708 goto done;
1710 error = apply_unveil(got_repo_get_path(repo), 1,
1711 got_worktree_get_root_path(worktree), 0);
1712 if (error)
1713 goto done;
1715 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1716 check_cancelled, NULL);
1717 done:
1718 free(cwd);
1719 free(path);
1720 return error;
1723 __dead static void
1724 usage_ref(void)
1726 fprintf(stderr,
1727 "usage: %s ref [-r repository] -l | -d name | name object\n",
1728 getprogname());
1729 exit(1);
1732 static const struct got_error *
1733 list_refs(struct got_repository *repo)
1735 static const struct got_error *err = NULL;
1736 struct got_reflist_head refs;
1737 struct got_reflist_entry *re;
1739 SIMPLEQ_INIT(&refs);
1740 err = got_ref_list(&refs, repo);
1741 if (err)
1742 return err;
1744 SIMPLEQ_FOREACH(re, &refs, entry) {
1745 char *refstr;
1746 refstr = got_ref_to_str(re->ref);
1747 if (refstr == NULL)
1748 return got_error_prefix_errno("got_ref_to_str");
1749 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1750 free(refstr);
1753 got_ref_list_free(&refs);
1754 return NULL;
1757 static const struct got_error *
1758 delete_ref(struct got_repository *repo, const char *refname)
1760 const struct got_error *err = NULL;
1761 struct got_reference *ref;
1763 err = got_ref_open(&ref, repo, refname, 0);
1764 if (err)
1765 return err;
1767 err = got_ref_delete(ref, repo);
1768 got_ref_close(ref);
1769 return err;
1772 static const struct got_error *
1773 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1775 const struct got_error *err = NULL;
1776 struct got_object_id *id;
1777 struct got_reference *ref = NULL;
1779 err = got_object_resolve_id_str(&id, repo, id_str);
1780 if (err)
1781 return err;
1783 err = got_ref_alloc(&ref, refname, id);
1784 if (err)
1785 goto done;
1787 err = got_ref_write(ref, repo);
1788 done:
1789 if (ref)
1790 got_ref_close(ref);
1791 free(id);
1792 return err;
1795 static const struct got_error *
1796 cmd_ref(int argc, char *argv[])
1798 const struct got_error *error = NULL;
1799 struct got_repository *repo = NULL;
1800 struct got_worktree *worktree = NULL;
1801 char *cwd = NULL, *repo_path = NULL;
1802 int ch, do_list = 0;
1803 const char *delref = NULL;
1805 /* TODO: Add -s option for adding symbolic references. */
1806 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1807 switch (ch) {
1808 case 'd':
1809 delref = optarg;
1810 break;
1811 case 'r':
1812 repo_path = realpath(optarg, NULL);
1813 if (repo_path == NULL)
1814 err(1, "-r option");
1815 got_path_strip_trailing_slashes(repo_path);
1816 break;
1817 case 'l':
1818 do_list = 1;
1819 break;
1820 default:
1821 usage_ref();
1822 /* NOTREACHED */
1826 if (do_list && delref)
1827 errx(1, "-l and -d options are mutually exclusive\n");
1829 argc -= optind;
1830 argv += optind;
1832 if (do_list || delref) {
1833 if (argc > 0)
1834 usage_ref();
1835 } else if (argc != 2)
1836 usage_ref();
1838 #ifndef PROFILE
1839 if (do_list) {
1840 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1841 NULL) == -1)
1842 err(1, "pledge");
1843 } else {
1844 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1845 "sendfd unveil", NULL) == -1)
1846 err(1, "pledge");
1848 #endif
1849 cwd = getcwd(NULL, 0);
1850 if (cwd == NULL) {
1851 error = got_error_prefix_errno("getcwd");
1852 goto done;
1855 if (repo_path == NULL) {
1856 error = got_worktree_open(&worktree, cwd);
1857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1858 goto done;
1859 else
1860 error = NULL;
1861 if (worktree) {
1862 repo_path =
1863 strdup(got_worktree_get_repo_path(worktree));
1864 if (repo_path == NULL)
1865 error = got_error_prefix_errno("strdup");
1866 if (error)
1867 goto done;
1868 } else {
1869 repo_path = strdup(cwd);
1870 if (repo_path == NULL) {
1871 error = got_error_prefix_errno("strdup");
1872 goto done;
1877 error = got_repo_open(&repo, repo_path);
1878 if (error != NULL)
1879 goto done;
1881 error = apply_unveil(got_repo_get_path(repo), do_list,
1882 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1883 if (error)
1884 goto done;
1886 if (do_list)
1887 error = list_refs(repo);
1888 else if (delref)
1889 error = delete_ref(repo, delref);
1890 else
1891 error = add_ref(repo, argv[0], argv[1]);
1892 done:
1893 if (repo)
1894 got_repo_close(repo);
1895 if (worktree)
1896 got_worktree_close(worktree);
1897 free(cwd);
1898 free(repo_path);
1899 return error;
1902 __dead static void
1903 usage_add(void)
1905 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1906 exit(1);
1909 static const struct got_error *
1910 cmd_add(int argc, char *argv[])
1912 const struct got_error *error = NULL;
1913 struct got_repository *repo = NULL;
1914 struct got_worktree *worktree = NULL;
1915 char *cwd = NULL;
1916 struct got_pathlist_head paths;
1917 struct got_pathlist_entry *pe;
1918 int ch, x;
1920 TAILQ_INIT(&paths);
1922 while ((ch = getopt(argc, argv, "")) != -1) {
1923 switch (ch) {
1924 default:
1925 usage_add();
1926 /* NOTREACHED */
1930 argc -= optind;
1931 argv += optind;
1933 if (argc < 1)
1934 usage_add();
1936 /* make sure each file exists before doing anything halfway */
1937 for (x = 0; x < argc; x++) {
1938 char *path = realpath(argv[x], NULL);
1939 if (path == NULL) {
1940 error = got_error_prefix_errno2("realpath", argv[x]);
1941 goto done;
1943 free(path);
1946 cwd = getcwd(NULL, 0);
1947 if (cwd == NULL) {
1948 error = got_error_prefix_errno("getcwd");
1949 goto done;
1952 error = got_worktree_open(&worktree, cwd);
1953 if (error)
1954 goto done;
1956 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1957 if (error != NULL)
1958 goto done;
1960 error = apply_unveil(got_repo_get_path(repo), 1,
1961 got_worktree_get_root_path(worktree), 0);
1962 if (error)
1963 goto done;
1965 for (x = 0; x < argc; x++) {
1966 char *path = realpath(argv[x], NULL);
1967 if (path == NULL) {
1968 error = got_error_prefix_errno2("realpath", argv[x]);
1969 goto done;
1972 got_path_strip_trailing_slashes(path);
1973 error = got_pathlist_insert(&pe, &paths, path, NULL);
1974 if (error) {
1975 free(path);
1976 goto done;
1979 error = got_worktree_schedule_add(worktree, &paths, print_status,
1980 NULL, repo);
1981 done:
1982 if (repo)
1983 got_repo_close(repo);
1984 if (worktree)
1985 got_worktree_close(worktree);
1986 TAILQ_FOREACH(pe, &paths, entry)
1987 free((char *)pe->path);
1988 got_pathlist_free(&paths);
1989 free(cwd);
1990 return error;
1993 __dead static void
1994 usage_rm(void)
1996 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1997 exit(1);
2000 static const struct got_error *
2001 cmd_rm(int argc, char *argv[])
2003 const struct got_error *error = NULL;
2004 struct got_worktree *worktree = NULL;
2005 struct got_repository *repo = NULL;
2006 char *cwd = NULL, *path = NULL;
2007 int ch, delete_local_mods = 0;
2009 while ((ch = getopt(argc, argv, "f")) != -1) {
2010 switch (ch) {
2011 case 'f':
2012 delete_local_mods = 1;
2013 break;
2014 default:
2015 usage_add();
2016 /* NOTREACHED */
2020 argc -= optind;
2021 argv += optind;
2023 if (argc != 1)
2024 usage_rm();
2026 path = realpath(argv[0], NULL);
2027 if (path == NULL) {
2028 error = got_error_prefix_errno2("realpath", argv[0]);
2029 goto done;
2031 got_path_strip_trailing_slashes(path);
2033 cwd = getcwd(NULL, 0);
2034 if (cwd == NULL) {
2035 error = got_error_prefix_errno("getcwd");
2036 goto done;
2038 error = got_worktree_open(&worktree, cwd);
2039 if (error)
2040 goto done;
2042 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2043 if (error)
2044 goto done;
2046 error = apply_unveil(got_repo_get_path(repo), 1,
2047 got_worktree_get_root_path(worktree), 0);
2048 if (error)
2049 goto done;
2051 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2052 print_status, NULL, repo);
2053 if (error)
2054 goto done;
2055 done:
2056 if (repo)
2057 got_repo_close(repo);
2058 if (worktree)
2059 got_worktree_close(worktree);
2060 free(path);
2061 free(cwd);
2062 return error;
2065 __dead static void
2066 usage_revert(void)
2068 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2069 exit(1);
2072 static void
2073 revert_progress(void *arg, unsigned char status, const char *path)
2075 while (path[0] == '/')
2076 path++;
2077 printf("%c %s\n", status, path);
2080 static const struct got_error *
2081 cmd_revert(int argc, char *argv[])
2083 const struct got_error *error = NULL;
2084 struct got_worktree *worktree = NULL;
2085 struct got_repository *repo = NULL;
2086 char *cwd = NULL, *path = NULL;
2087 int ch;
2089 while ((ch = getopt(argc, argv, "")) != -1) {
2090 switch (ch) {
2091 default:
2092 usage_revert();
2093 /* NOTREACHED */
2097 argc -= optind;
2098 argv += optind;
2100 if (argc != 1)
2101 usage_revert();
2103 path = realpath(argv[0], NULL);
2104 if (path == NULL) {
2105 error = got_error_prefix_errno2("realpath", argv[0]);
2106 goto done;
2108 got_path_strip_trailing_slashes(path);
2110 cwd = getcwd(NULL, 0);
2111 if (cwd == NULL) {
2112 error = got_error_prefix_errno("getcwd");
2113 goto done;
2115 error = got_worktree_open(&worktree, cwd);
2116 if (error)
2117 goto done;
2119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2120 if (error != NULL)
2121 goto done;
2123 error = apply_unveil(got_repo_get_path(repo), 1,
2124 got_worktree_get_root_path(worktree), 0);
2125 if (error)
2126 goto done;
2128 error = got_worktree_revert(worktree, path,
2129 revert_progress, NULL, repo);
2130 if (error)
2131 goto done;
2132 done:
2133 if (repo)
2134 got_repo_close(repo);
2135 if (worktree)
2136 got_worktree_close(worktree);
2137 free(path);
2138 free(cwd);
2139 return error;
2142 __dead static void
2143 usage_commit(void)
2145 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2146 exit(1);
2149 static const struct got_error *
2150 cmd_commit(int argc, char *argv[])
2152 const struct got_error *error = NULL;
2153 struct got_worktree *worktree = NULL;
2154 struct got_repository *repo = NULL;
2155 char *cwd = NULL, *path = NULL, *id_str = NULL;
2156 struct got_object_id *id = NULL;
2157 const char *logmsg = "<no log message was specified>";
2158 const char *got_author = getenv("GOT_AUTHOR");
2159 int ch;
2161 while ((ch = getopt(argc, argv, "m:")) != -1) {
2162 switch (ch) {
2163 case 'm':
2164 logmsg = optarg;
2165 break;
2166 default:
2167 usage_commit();
2168 /* NOTREACHED */
2172 argc -= optind;
2173 argv += optind;
2175 if (argc == 1) {
2176 path = realpath(argv[0], NULL);
2177 if (path == NULL) {
2178 error = got_error_prefix_errno2("realpath", argv[0]);
2179 goto done;
2181 got_path_strip_trailing_slashes(path);
2182 } else if (argc != 0)
2183 usage_commit();
2185 if (got_author == NULL) {
2186 /* TODO: Look current user up in password database */
2187 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2188 goto done;
2191 cwd = getcwd(NULL, 0);
2192 if (cwd == NULL) {
2193 error = got_error_prefix_errno("getcwd");
2194 goto done;
2196 error = got_worktree_open(&worktree, cwd);
2197 if (error)
2198 goto done;
2200 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2201 if (error != NULL)
2202 goto done;
2204 error = apply_unveil(got_repo_get_path(repo), 0,
2205 got_worktree_get_root_path(worktree), 0);
2206 if (error)
2207 goto done;
2209 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2210 logmsg, print_status, NULL, repo);
2211 if (error)
2212 goto done;
2214 error = got_object_id_str(&id_str, id);
2215 if (error)
2216 goto done;
2217 printf("created commit %s\n", id_str);
2218 done:
2219 if (repo)
2220 got_repo_close(repo);
2221 if (worktree)
2222 got_worktree_close(worktree);
2223 free(path);
2224 free(cwd);
2225 free(id_str);
2226 return error;