2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
42 #include "got_worktree.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
57 catch_sigint(int signo)
63 catch_sigpipe(int signo)
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 const char *cmd_descr;
76 __dead static void usage(void);
77 __dead static void usage_checkout(void);
78 __dead static void usage_update(void);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_status(void);
84 __dead static void usage_ref(void);
85 __dead static void usage_add(void);
86 __dead static void usage_rm(void);
87 __dead static void usage_revert(void);
88 __dead static void usage_commit(void);
90 static const struct got_error* cmd_checkout(int, char *[]);
91 static const struct got_error* cmd_update(int, char *[]);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_status(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct got_error* cmd_add(int, char *[]);
99 static const struct got_error* cmd_rm(int, char *[]);
100 static const struct got_error* cmd_revert(int, char *[]);
101 static const struct got_error* cmd_commit(int, char *[]);
103 static struct cmd got_commands[] = {
104 { "checkout", cmd_checkout, usage_checkout,
105 "check out a new work tree from a repository" },
106 { "update", cmd_update, usage_update,
107 "update a work tree to a different commit" },
108 { "log", cmd_log, usage_log,
109 "show repository history" },
110 { "diff", cmd_diff, usage_diff,
111 "compare files and directories" },
112 { "blame", cmd_blame, usage_blame,
113 "show when lines in a file were changed" },
114 { "tree", cmd_tree, usage_tree,
115 "list files and directories in repository" },
116 { "status", cmd_status, usage_status,
117 "show modification status of files" },
118 { "ref", cmd_ref, usage_ref,
119 "manage references in repository" },
120 { "add", cmd_add, usage_add,
121 "add new files to version control" },
122 { "rm", cmd_rm, usage_rm,
123 "remove a versioned file" },
124 { "revert", cmd_revert, usage_revert,
125 "revert uncommitted changes" },
126 { "commit", cmd_commit, usage_commit,
127 "write changes from work tree to repository" },
131 main(int argc, char *argv[])
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt(argc, argv, "h")) != -1) {
158 signal(SIGINT, catch_sigint);
159 signal(SIGPIPE, catch_sigpipe);
161 for (i = 0; i < nitems(got_commands); i++) {
162 const struct got_error *error;
164 cmd = &got_commands[i];
166 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
170 got_commands[i].cmd_usage();
172 error = got_commands[i].cmd_main(argc, argv);
173 if (error && !(sigint_received || sigpipe_received)) {
174 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
190 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
191 "Available commands:\n", getprogname());
192 for (i = 0; i < nitems(got_commands); i++) {
193 struct cmd *cmd = &got_commands[i];
194 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
199 static const struct got_error *
200 apply_unveil(const char *repo_path, int repo_read_only,
201 const char *worktree_path, int create_worktree)
203 const struct got_error *error;
204 static char err_msg[MAXPATHLEN + 36];
206 if (create_worktree) {
207 /* Pre-create work tree path to avoid unveiling its parents. */
208 error = got_path_mkdir(worktree_path);
210 if (errno == EEXIST) {
211 if (got_path_dir_is_empty(worktree_path)) {
215 snprintf(err_msg, sizeof(err_msg),
216 "%s: directory exists but is not empty",
218 error = got_error_msg(GOT_ERR_BAD_PATH,
223 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
227 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
228 return got_error_prefix_errno2("unveil", repo_path);
230 if (worktree_path && unveil(worktree_path, "rwc") != 0)
231 return got_error_prefix_errno2("unveil", worktree_path);
233 if (unveil("/tmp", "rwc") != 0)
234 return got_error_prefix_errno2("unveil", "/tmp");
236 error = got_privsep_unveil_exec_helpers();
240 if (unveil(NULL, NULL) != 0)
241 return got_error_prefix_errno("unveil");
249 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
250 "[worktree-path]\n", getprogname());
255 checkout_progress(void *arg, unsigned char status, const char *path)
257 char *worktree_path = arg;
259 while (path[0] == '/')
262 printf("%c %s/%s\n", status, worktree_path, path);
265 static const struct got_error *
266 check_cancelled(void *arg)
268 if (sigint_received || sigpipe_received)
269 return got_error(GOT_ERR_CANCELLED);
273 static const struct got_error *
274 check_linear_ancestry(struct got_worktree *worktree,
275 struct got_object_id *commit_id, struct got_repository *repo)
277 const struct got_error *err = NULL;
278 struct got_object_id *yca_id, *base_commit_id;
280 base_commit_id = got_worktree_get_base_commit_id(worktree);
281 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
282 commit_id, base_commit_id, repo);
287 return got_error(GOT_ERR_ANCESTRY);
290 * Require a straight line of history between the target commit
291 * and the work tree's base commit.
293 * Non-linear situation such as the this require a rebase:
295 * (commit) D F (base_commit)
303 * 'got update' only handles linear cases:
304 * Update forwards in time: A (base/yca) - B - C - D (commit)
305 * Update backwards in time: D (base) - C - D - A (commit/yca)
307 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
308 got_object_id_cmp(base_commit_id, yca_id) != 0)
309 return got_error(GOT_ERR_ANCESTRY);
316 static const struct got_error *
317 cmd_checkout(int argc, char *argv[])
319 const struct got_error *error = NULL;
320 struct got_repository *repo = NULL;
321 struct got_reference *head_ref = NULL;
322 struct got_worktree *worktree = NULL;
323 char *repo_path = NULL;
324 char *worktree_path = NULL;
325 const char *path_prefix = "";
326 char *commit_id_str = NULL;
327 int ch, same_path_prefix;
329 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
332 commit_id_str = strdup(optarg);
333 if (commit_id_str == NULL)
334 return got_error_prefix_errno("strdup");
337 path_prefix = optarg;
349 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
350 "unveil", NULL) == -1)
354 char *cwd, *base, *dotgit;
355 repo_path = realpath(argv[0], NULL);
356 if (repo_path == NULL)
357 return got_error_prefix_errno2("realpath", argv[0]);
358 cwd = getcwd(NULL, 0);
360 error = got_error_prefix_errno("getcwd");
363 if (path_prefix[0]) {
364 base = basename(path_prefix);
366 error = got_error_prefix_errno2("basename",
371 base = basename(repo_path);
373 error = got_error_prefix_errno2("basename",
378 dotgit = strstr(base, ".git");
381 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
382 error = got_error_prefix_errno("asprintf");
387 } else if (argc == 2) {
388 repo_path = realpath(argv[0], NULL);
389 if (repo_path == NULL) {
390 error = got_error_prefix_errno2("realpath", argv[0]);
393 worktree_path = realpath(argv[1], NULL);
394 if (worktree_path == NULL) {
395 error = got_error_prefix_errno2("realpath", argv[1]);
401 got_path_strip_trailing_slashes(repo_path);
402 got_path_strip_trailing_slashes(worktree_path);
404 error = got_repo_open(&repo, repo_path);
408 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
412 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
416 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
417 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
420 error = got_worktree_open(&worktree, worktree_path);
424 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
428 if (!same_path_prefix) {
429 error = got_error(GOT_ERR_PATH_PREFIX);
434 struct got_object_id *commit_id;
435 error = got_object_resolve_id_str(&commit_id, repo,
439 error = check_linear_ancestry(worktree, commit_id, repo);
444 error = got_worktree_set_base_commit_id(worktree, repo,
451 error = got_worktree_checkout_files(worktree, "", repo,
452 checkout_progress, worktree_path, check_cancelled, NULL);
456 printf("Now shut up and hack\n");
468 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
474 update_progress(void *arg, unsigned char status, const char *path)
476 int *did_something = arg;
478 if (status == GOT_STATUS_EXISTS)
482 while (path[0] == '/')
484 printf("%c %s\n", status, path);
487 static const struct got_error *
488 cmd_update(int argc, char *argv[])
490 const struct got_error *error = NULL;
491 struct got_repository *repo = NULL;
492 struct got_worktree *worktree = NULL;
493 char *worktree_path = NULL, *path = NULL;
494 struct got_object_id *commit_id = NULL;
495 char *commit_id_str = NULL;
496 int ch, did_something = 0;
498 while ((ch = getopt(argc, argv, "c:")) != -1) {
501 commit_id_str = strdup(optarg);
502 if (commit_id_str == NULL)
503 return got_error_prefix_errno("strdup");
515 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
516 "unveil", NULL) == -1)
519 worktree_path = getcwd(NULL, 0);
520 if (worktree_path == NULL) {
521 error = got_error_prefix_errno("getcwd");
524 error = got_worktree_open(&worktree, worktree_path);
531 error = got_error_prefix_errno("strdup");
534 } else if (argc == 1) {
535 error = got_worktree_resolve_path(&path, worktree, argv[0]);
541 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
545 error = apply_unveil(got_repo_get_path(repo), 0,
546 got_worktree_get_root_path(worktree), 0);
550 if (commit_id_str == NULL) {
551 struct got_reference *head_ref;
552 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
555 error = got_ref_resolve(&commit_id, repo, head_ref);
558 error = got_object_id_str(&commit_id_str, commit_id);
562 error = got_object_resolve_id_str(&commit_id, repo,
568 error = check_linear_ancestry(worktree, commit_id, repo);
572 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
574 error = got_worktree_set_base_commit_id(worktree, repo,
580 error = got_worktree_checkout_files(worktree, path, repo,
581 update_progress, &did_something, check_cancelled, NULL);
586 printf("Updated to commit %s\n", commit_id_str);
588 printf("Already up-to-date\n");
597 static const struct got_error *
598 print_patch(struct got_commit_object *commit, struct got_object_id *id,
599 int diff_context, struct got_repository *repo)
601 const struct got_error *err = NULL;
602 struct got_tree_object *tree1 = NULL, *tree2;
603 struct got_object_qid *qid;
604 char *id_str1 = NULL, *id_str2;
606 err = got_object_open_as_tree(&tree2, repo,
607 got_object_commit_get_tree_id(commit));
611 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
613 struct got_commit_object *pcommit;
615 err = got_object_open_as_commit(&pcommit, repo, qid->id);
619 err = got_object_open_as_tree(&tree1, repo,
620 got_object_commit_get_tree_id(pcommit));
621 got_object_commit_close(pcommit);
625 err = got_object_id_str(&id_str1, qid->id);
630 err = got_object_id_str(&id_str2, id);
634 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
635 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
638 got_object_tree_close(tree1);
639 got_object_tree_close(tree2);
646 get_datestr(time_t *time, char *datebuf)
648 char *p, *s = ctime_r(time, datebuf);
655 static const struct got_error *
656 print_commit(struct got_commit_object *commit, struct got_object_id *id,
657 struct got_repository *repo, int show_patch, int diff_context,
658 struct got_reflist_head *refs)
660 const struct got_error *err = NULL;
661 char *id_str, *datestr, *logmsg0, *logmsg, *line;
663 time_t committer_time;
664 const char *author, *committer;
665 char *refs_str = NULL;
666 struct got_reflist_entry *re;
668 SIMPLEQ_FOREACH(re, refs, entry) {
671 if (got_object_id_cmp(re->id, id) != 0)
673 name = got_ref_get_name(re->ref);
674 if (strcmp(name, GOT_REF_HEAD) == 0)
676 if (strncmp(name, "refs/", 5) == 0)
678 if (strncmp(name, "got/", 4) == 0)
680 if (strncmp(name, "heads/", 6) == 0)
682 if (strncmp(name, "remotes/", 8) == 0)
685 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
687 err = got_error_prefix_errno("asprintf");
693 err = got_object_id_str(&id_str, id);
697 printf("-----------------------------------------------\n");
698 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
699 refs_str ? refs_str : "", refs_str ? ")" : "");
704 printf("from: %s\n", got_object_commit_get_author(commit));
705 committer_time = got_object_commit_get_committer_time(commit);
706 datestr = get_datestr(&committer_time, datebuf);
707 printf("date: %s UTC\n", datestr);
708 author = got_object_commit_get_author(commit);
709 committer = got_object_commit_get_committer(commit);
710 if (strcmp(author, committer) != 0)
711 printf("via: %s\n", committer);
712 if (got_object_commit_get_nparents(commit) > 1) {
713 const struct got_object_id_queue *parent_ids;
714 struct got_object_qid *qid;
716 parent_ids = got_object_commit_get_parent_ids(commit);
717 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
718 err = got_object_id_str(&id_str, qid->id);
721 printf("parent %d: %s\n", n++, id_str);
726 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
728 return got_error_prefix_errno("strdup");
732 line = strsep(&logmsg, "\n");
734 printf(" %s\n", line);
739 err = print_patch(commit, id, diff_context, repo);
744 if (fflush(stdout) != 0 && err == NULL)
745 err = got_error_prefix_errno("fflush");
749 static const struct got_error *
750 print_commits(struct got_object_id *root_id, struct got_repository *repo,
751 char *path, int show_patch, int diff_context, int limit,
752 int first_parent_traversal, struct got_reflist_head *refs)
754 const struct got_error *err;
755 struct got_commit_graph *graph;
757 err = got_commit_graph_open(&graph, root_id, path,
758 first_parent_traversal, repo);
761 err = got_commit_graph_iter_start(graph, root_id, repo);
765 struct got_commit_object *commit;
766 struct got_object_id *id;
768 if (sigint_received || sigpipe_received)
771 err = got_commit_graph_iter_next(&id, graph);
773 if (err->code == GOT_ERR_ITER_COMPLETED) {
777 if (err->code != GOT_ERR_ITER_NEED_MORE)
779 err = got_commit_graph_fetch_commits(graph, 1, repo);
788 err = got_object_open_as_commit(&commit, repo, id);
791 err = print_commit(commit, id, repo, show_patch, diff_context,
793 got_object_commit_close(commit);
794 if (err || (limit && --limit == 0))
798 got_commit_graph_close(graph);
805 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
806 "[-r repository-path] [path]\n", getprogname());
810 static const struct got_error *
811 cmd_log(int argc, char *argv[])
813 const struct got_error *error;
814 struct got_repository *repo = NULL;
815 struct got_worktree *worktree = NULL;
816 struct got_commit_object *commit = NULL;
817 struct got_object_id *id = NULL;
818 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
819 char *start_commit = NULL;
820 int diff_context = 3, ch;
821 int show_patch = 0, limit = 0, first_parent_traversal = 0;
823 struct got_reflist_head refs;
828 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
834 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
840 start_commit = optarg;
843 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
846 err(1, "-C option %s", errstr);
849 limit = strtonum(optarg, 1, INT_MAX, &errstr);
851 err(1, "-l option %s", errstr);
854 first_parent_traversal = 1;
857 repo_path = realpath(optarg, NULL);
858 if (repo_path == NULL)
860 got_path_strip_trailing_slashes(repo_path);
871 cwd = getcwd(NULL, 0);
873 error = got_error_prefix_errno("getcwd");
877 error = got_worktree_open(&worktree, cwd);
878 if (error && error->code != GOT_ERR_NOT_WORKTREE)
885 error = got_error_prefix_errno("strdup");
888 } else if (argc == 1) {
890 error = got_worktree_resolve_path(&path, worktree,
895 path = strdup(argv[0]);
897 error = got_error_prefix_errno("strdup");
904 if (repo_path == NULL) {
905 repo_path = worktree ?
906 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
908 if (repo_path == NULL) {
909 error = got_error_prefix_errno("strdup");
913 error = got_repo_open(&repo, repo_path);
917 error = apply_unveil(got_repo_get_path(repo), 1,
918 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
922 if (start_commit == NULL) {
923 struct got_reference *head_ref;
924 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
927 error = got_ref_resolve(&id, repo, head_ref);
928 got_ref_close(head_ref);
931 error = got_object_open_as_commit(&commit, repo, id);
933 struct got_reference *ref;
934 error = got_ref_open(&ref, repo, start_commit, 0);
937 error = got_ref_resolve(&id, repo, ref);
941 error = got_object_get_type(&obj_type, repo, id);
944 if (obj_type == GOT_OBJ_TYPE_TAG) {
945 struct got_tag_object *tag;
946 error = got_object_open_as_tag(&tag, repo, id);
949 if (got_object_tag_get_object_type(tag) !=
950 GOT_OBJ_TYPE_COMMIT) {
951 got_object_tag_close(tag);
952 error = got_error(GOT_ERR_OBJ_TYPE);
956 id = got_object_id_dup(
957 got_object_tag_get_object_id(tag));
959 error = got_error_prefix_errno(
960 "got_object_id_dup");
961 got_object_tag_close(tag);
964 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
965 error = got_error(GOT_ERR_OBJ_TYPE);
968 error = got_object_open_as_commit(&commit, repo, id);
972 if (commit == NULL) {
973 error = got_object_resolve_id_str(&id, repo,
982 error = got_repo_map_path(&in_repo_path, repo, path, 1);
990 error = got_ref_list(&refs, repo);
994 error = print_commits(id, repo, path, show_patch,
995 diff_context, limit, first_parent_traversal, &refs);
1002 got_worktree_close(worktree);
1004 const struct got_error *repo_error;
1005 repo_error = got_repo_close(repo);
1009 got_ref_list_free(&refs);
1016 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1017 "[object1 object2 | path]\n", getprogname());
1021 struct print_diff_arg {
1022 struct got_repository *repo;
1023 struct got_worktree *worktree;
1029 static const struct got_error *
1030 print_diff(void *arg, unsigned char status, const char *path,
1031 struct got_object_id *id)
1033 struct print_diff_arg *a = arg;
1034 const struct got_error *err = NULL;
1035 struct got_blob_object *blob1 = NULL;
1037 char *abspath = NULL;
1040 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1041 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1044 if (!a->header_shown) {
1045 printf("diff %s %s\n", a->id_str,
1046 got_worktree_get_root_path(a->worktree));
1047 a->header_shown = 1;
1050 if (status != GOT_STATUS_ADD) {
1051 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1057 if (status != GOT_STATUS_DELETE) {
1058 if (asprintf(&abspath, "%s/%s",
1059 got_worktree_get_root_path(a->worktree), path) == -1) {
1060 err = got_error_prefix_errno("asprintf");
1064 f2 = fopen(abspath, "r");
1066 err = got_error_prefix_errno2("fopen", abspath);
1069 if (lstat(abspath, &sb) == -1) {
1070 err = got_error_prefix_errno2("lstat", abspath);
1076 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1080 got_object_blob_close(blob1);
1081 if (f2 && fclose(f2) != 0 && err == NULL)
1082 err = got_error_prefix_errno("fclose");
1087 static const struct got_error *
1088 cmd_diff(int argc, char *argv[])
1090 const struct got_error *error;
1091 struct got_repository *repo = NULL;
1092 struct got_worktree *worktree = NULL;
1093 char *cwd = NULL, *repo_path = NULL;
1094 struct got_object_id *id1 = NULL, *id2 = NULL;
1095 char *id_str1 = NULL, *id_str2 = NULL;
1097 int diff_context = 3, ch;
1102 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1107 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1110 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1112 err(1, "-C option %s", errstr);
1115 repo_path = realpath(optarg, NULL);
1116 if (repo_path == NULL)
1117 err(1, "-r option");
1118 got_path_strip_trailing_slashes(repo_path);
1129 cwd = getcwd(NULL, 0);
1131 error = got_error_prefix_errno("getcwd");
1134 error = got_worktree_open(&worktree, cwd);
1135 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1138 if (worktree == NULL) {
1139 error = got_error(GOT_ERR_NOT_WORKTREE);
1144 "-r option can't be used when diffing a work tree");
1145 repo_path = strdup(got_worktree_get_repo_path(worktree));
1146 if (repo_path == NULL) {
1147 error = got_error_prefix_errno("strdup");
1151 error = got_worktree_resolve_path(&path, worktree,
1158 error = got_error_prefix_errno("strdup");
1162 } else if (argc == 2) {
1168 if (repo_path == NULL) {
1169 repo_path = getcwd(NULL, 0);
1170 if (repo_path == NULL)
1171 return got_error_prefix_errno("getcwd");
1174 error = got_repo_open(&repo, repo_path);
1179 error = apply_unveil(got_repo_get_path(repo), 1,
1180 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1185 struct print_diff_arg arg;
1187 error = got_object_id_str(&id_str,
1188 got_worktree_get_base_commit_id(worktree));
1192 arg.worktree = worktree;
1193 arg.diff_context = diff_context;
1194 arg.id_str = id_str;
1195 arg.header_shown = 0;
1197 error = got_worktree_status(worktree, path, repo, print_diff,
1198 &arg, check_cancelled, NULL);
1203 error = got_object_resolve_id_str(&id1, repo, id_str1);
1207 error = got_object_resolve_id_str(&id2, repo, id_str2);
1211 error = got_object_get_type(&type1, repo, id1);
1215 error = got_object_get_type(&type2, repo, id2);
1219 if (type1 != type2) {
1220 error = got_error(GOT_ERR_OBJ_TYPE);
1225 case GOT_OBJ_TYPE_BLOB:
1226 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1227 diff_context, repo, stdout);
1229 case GOT_OBJ_TYPE_TREE:
1230 error = got_diff_objects_as_trees(id1, id2, "", "",
1231 diff_context, repo, stdout);
1233 case GOT_OBJ_TYPE_COMMIT:
1234 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1236 error = got_diff_objects_as_commits(id1, id2, diff_context,
1240 error = got_error(GOT_ERR_OBJ_TYPE);
1248 got_worktree_close(worktree);
1250 const struct got_error *repo_error;
1251 repo_error = got_repo_close(repo);
1262 "usage: %s blame [-c commit] [-r repository-path] path\n",
1267 static const struct got_error *
1268 cmd_blame(int argc, char *argv[])
1270 const struct got_error *error;
1271 struct got_repository *repo = NULL;
1272 struct got_worktree *worktree = NULL;
1273 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1274 struct got_object_id *commit_id = NULL;
1275 char *commit_id_str = NULL;
1279 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1284 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1287 commit_id_str = optarg;
1290 repo_path = realpath(optarg, NULL);
1291 if (repo_path == NULL)
1292 err(1, "-r option");
1293 got_path_strip_trailing_slashes(repo_path);
1309 cwd = getcwd(NULL, 0);
1311 error = got_error_prefix_errno("getcwd");
1314 if (repo_path == NULL) {
1315 error = got_worktree_open(&worktree, cwd);
1316 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1322 strdup(got_worktree_get_repo_path(worktree));
1323 if (repo_path == NULL)
1324 error = got_error_prefix_errno("strdup");
1328 repo_path = strdup(cwd);
1329 if (repo_path == NULL) {
1330 error = got_error_prefix_errno("strdup");
1336 error = got_repo_open(&repo, repo_path);
1340 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1345 const char *prefix = got_worktree_get_path_prefix(worktree);
1346 char *p, *worktree_subdir = cwd +
1347 strlen(got_worktree_get_root_path(worktree));
1348 if (asprintf(&p, "%s%s%s%s%s",
1349 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1350 worktree_subdir, worktree_subdir[0] ? "/" : "",
1352 error = got_error_prefix_errno("asprintf");
1355 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1358 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1363 if (commit_id_str == NULL) {
1364 struct got_reference *head_ref;
1365 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1368 error = got_ref_resolve(&commit_id, repo, head_ref);
1369 got_ref_close(head_ref);
1373 error = got_object_resolve_id_str(&commit_id, repo,
1379 error = got_blame(in_repo_path, commit_id, repo, stdout);
1386 got_worktree_close(worktree);
1388 const struct got_error *repo_error;
1389 repo_error = got_repo_close(repo);
1400 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1406 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1407 const char *root_path)
1409 int is_root_path = (strcmp(path, root_path) == 0);
1411 path += strlen(root_path);
1412 while (path[0] == '/')
1415 printf("%s%s%s%s%s\n", id ? id : "", path,
1416 is_root_path ? "" : "/", te->name,
1417 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1420 static const struct got_error *
1421 print_tree(const char *path, struct got_object_id *commit_id,
1422 int show_ids, int recurse, const char *root_path,
1423 struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 struct got_object_id *tree_id = NULL;
1427 struct got_tree_object *tree = NULL;
1428 const struct got_tree_entries *entries;
1429 struct got_tree_entry *te;
1431 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1435 err = got_object_open_as_tree(&tree, repo, tree_id);
1438 entries = got_object_tree_get_entries(tree);
1439 te = SIMPLEQ_FIRST(&entries->head);
1443 if (sigint_received || sigpipe_received)
1448 err = got_object_id_str(&id_str, te->id);
1451 if (asprintf(&id, "%s ", id_str) == -1) {
1452 err = got_error_prefix_errno("asprintf");
1458 print_entry(te, id, path, root_path);
1461 if (recurse && S_ISDIR(te->mode)) {
1463 if (asprintf(&child_path, "%s%s%s", path,
1464 path[0] == '/' && path[1] == '\0' ? "" : "/",
1466 err = got_error_prefix_errno("asprintf");
1469 err = print_tree(child_path, commit_id, show_ids, 1,
1476 te = SIMPLEQ_NEXT(te, entry);
1480 got_object_tree_close(tree);
1485 static const struct got_error *
1486 cmd_tree(int argc, char *argv[])
1488 const struct got_error *error;
1489 struct got_repository *repo = NULL;
1490 struct got_worktree *worktree = NULL;
1492 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1493 struct got_object_id *commit_id = NULL;
1494 char *commit_id_str = NULL;
1495 int show_ids = 0, recurse = 0;
1499 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1504 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1507 commit_id_str = optarg;
1510 repo_path = realpath(optarg, NULL);
1511 if (repo_path == NULL)
1512 err(1, "-r option");
1513 got_path_strip_trailing_slashes(repo_path);
1537 cwd = getcwd(NULL, 0);
1539 error = got_error_prefix_errno("getcwd");
1542 if (repo_path == NULL) {
1543 error = got_worktree_open(&worktree, cwd);
1544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1550 strdup(got_worktree_get_repo_path(worktree));
1551 if (repo_path == NULL)
1552 error = got_error_prefix_errno("strdup");
1556 repo_path = strdup(cwd);
1557 if (repo_path == NULL) {
1558 error = got_error_prefix_errno("strdup");
1564 error = got_repo_open(&repo, repo_path);
1568 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1574 char *p, *worktree_subdir = cwd +
1575 strlen(got_worktree_get_root_path(worktree));
1576 if (asprintf(&p, "%s/%s",
1577 got_worktree_get_path_prefix(worktree),
1578 worktree_subdir) == -1) {
1579 error = got_error_prefix_errno("asprintf");
1582 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1589 if (in_repo_path == NULL) {
1590 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1595 if (commit_id_str == NULL) {
1596 struct got_reference *head_ref;
1597 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1600 error = got_ref_resolve(&commit_id, repo, head_ref);
1601 got_ref_close(head_ref);
1605 error = got_object_resolve_id_str(&commit_id, repo,
1611 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1612 in_repo_path, repo);
1619 got_worktree_close(worktree);
1621 const struct got_error *repo_error;
1622 repo_error = got_repo_close(repo);
1632 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1636 static const struct got_error *
1637 print_status(void *arg, unsigned char status, const char *path,
1638 struct got_object_id *id)
1640 printf("%c %s\n", status, path);
1644 static const struct got_error *
1645 cmd_status(int argc, char *argv[])
1647 const struct got_error *error = NULL;
1648 struct got_repository *repo = NULL;
1649 struct got_worktree *worktree = NULL;
1650 char *cwd = NULL, *path = NULL;
1653 while ((ch = getopt(argc, argv, "")) != -1) {
1665 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1669 cwd = getcwd(NULL, 0);
1671 error = got_error_prefix_errno("getcwd");
1675 error = got_worktree_open(&worktree, cwd);
1682 error = got_error_prefix_errno("strdup");
1685 } else if (argc == 1) {
1686 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1692 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1696 error = apply_unveil(got_repo_get_path(repo), 1,
1697 got_worktree_get_root_path(worktree), 0);
1701 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1702 check_cancelled, NULL);
1713 "usage: %s ref [-r repository] -l | -d name | name object\n",
1718 static const struct got_error *
1719 list_refs(struct got_repository *repo)
1721 static const struct got_error *err = NULL;
1722 struct got_reflist_head refs;
1723 struct got_reflist_entry *re;
1725 SIMPLEQ_INIT(&refs);
1726 err = got_ref_list(&refs, repo);
1730 SIMPLEQ_FOREACH(re, &refs, entry) {
1732 refstr = got_ref_to_str(re->ref);
1734 return got_error_prefix_errno("got_ref_to_str");
1735 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1739 got_ref_list_free(&refs);
1743 static const struct got_error *
1744 delete_ref(struct got_repository *repo, const char *refname)
1746 const struct got_error *err = NULL;
1747 struct got_reference *ref;
1749 err = got_ref_open(&ref, repo, refname, 0);
1753 err = got_ref_delete(ref, repo);
1758 static const struct got_error *
1759 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1761 const struct got_error *err = NULL;
1762 struct got_object_id *id;
1763 struct got_reference *ref = NULL;
1765 err = got_object_resolve_id_str(&id, repo, id_str);
1769 err = got_ref_alloc(&ref, refname, id);
1773 err = got_ref_write(ref, repo);
1781 static const struct got_error *
1782 cmd_ref(int argc, char *argv[])
1784 const struct got_error *error = NULL;
1785 struct got_repository *repo = NULL;
1786 struct got_worktree *worktree = NULL;
1787 char *cwd = NULL, *repo_path = NULL;
1788 int ch, do_list = 0;
1789 const char *delref = NULL;
1791 /* TODO: Add -s option for adding symbolic references. */
1792 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1798 repo_path = realpath(optarg, NULL);
1799 if (repo_path == NULL)
1800 err(1, "-r option");
1801 got_path_strip_trailing_slashes(repo_path);
1812 if (do_list && delref)
1813 errx(1, "-l and -d options are mutually exclusive\n");
1818 if (do_list || delref) {
1821 } else if (argc != 2)
1826 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1830 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1831 "sendfd unveil", NULL) == -1)
1835 cwd = getcwd(NULL, 0);
1837 error = got_error_prefix_errno("getcwd");
1841 if (repo_path == NULL) {
1842 error = got_worktree_open(&worktree, cwd);
1843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1849 strdup(got_worktree_get_repo_path(worktree));
1850 if (repo_path == NULL)
1851 error = got_error_prefix_errno("strdup");
1855 repo_path = strdup(cwd);
1856 if (repo_path == NULL) {
1857 error = got_error_prefix_errno("strdup");
1863 error = got_repo_open(&repo, repo_path);
1867 error = apply_unveil(got_repo_get_path(repo), do_list,
1868 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1873 error = list_refs(repo);
1875 error = delete_ref(repo, delref);
1877 error = add_ref(repo, argv[0], argv[1]);
1880 got_repo_close(repo);
1882 got_worktree_close(worktree);
1891 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1895 static const struct got_error *
1896 cmd_add(int argc, char *argv[])
1898 const struct got_error *error = NULL;
1899 struct got_repository *repo = NULL;
1900 struct got_worktree *worktree = NULL;
1902 struct got_pathlist_head paths;
1903 struct got_pathlist_entry *pe;
1908 while ((ch = getopt(argc, argv, "")) != -1) {
1922 /* make sure each file exists before doing anything halfway */
1923 for (x = 0; x < argc; x++) {
1924 char *path = realpath(argv[x], NULL);
1926 error = got_error_prefix_errno2("realpath", argv[x]);
1932 cwd = getcwd(NULL, 0);
1934 error = got_error_prefix_errno("getcwd");
1938 error = got_worktree_open(&worktree, cwd);
1942 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1946 error = apply_unveil(got_repo_get_path(repo), 1,
1947 got_worktree_get_root_path(worktree), 0);
1951 for (x = 0; x < argc; x++) {
1952 char *path = realpath(argv[x], NULL);
1954 error = got_error_prefix_errno2("realpath", argv[x]);
1958 got_path_strip_trailing_slashes(path);
1959 error = got_pathlist_insert(&pe, &paths, path, NULL);
1965 error = got_worktree_schedule_add(worktree, &paths, print_status,
1969 got_repo_close(repo);
1971 got_worktree_close(worktree);
1972 TAILQ_FOREACH(pe, &paths, entry)
1973 free((char *)pe->path);
1974 got_pathlist_free(&paths);
1982 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1986 static const struct got_error *
1987 cmd_rm(int argc, char *argv[])
1989 const struct got_error *error = NULL;
1990 struct got_worktree *worktree = NULL;
1991 struct got_repository *repo = NULL;
1992 char *cwd = NULL, *path = NULL;
1993 int ch, delete_local_mods = 0;
1995 while ((ch = getopt(argc, argv, "f")) != -1) {
1998 delete_local_mods = 1;
2012 path = realpath(argv[0], NULL);
2014 error = got_error_prefix_errno2("realpath", argv[0]);
2017 got_path_strip_trailing_slashes(path);
2019 cwd = getcwd(NULL, 0);
2021 error = got_error_prefix_errno("getcwd");
2024 error = got_worktree_open(&worktree, cwd);
2028 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2032 error = apply_unveil(got_repo_get_path(repo), 1,
2033 got_worktree_get_root_path(worktree), 0);
2037 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2038 print_status, NULL, repo);
2043 got_repo_close(repo);
2045 got_worktree_close(worktree);
2054 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2059 revert_progress(void *arg, unsigned char status, const char *path)
2061 while (path[0] == '/')
2063 printf("%c %s\n", status, path);
2066 static const struct got_error *
2067 cmd_revert(int argc, char *argv[])
2069 const struct got_error *error = NULL;
2070 struct got_worktree *worktree = NULL;
2071 struct got_repository *repo = NULL;
2072 char *cwd = NULL, *path = NULL;
2075 while ((ch = getopt(argc, argv, "")) != -1) {
2089 path = realpath(argv[0], NULL);
2091 error = got_error_prefix_errno2("realpath", argv[0]);
2094 got_path_strip_trailing_slashes(path);
2096 cwd = getcwd(NULL, 0);
2098 error = got_error_prefix_errno("getcwd");
2101 error = got_worktree_open(&worktree, cwd);
2105 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2109 error = apply_unveil(got_repo_get_path(repo), 1,
2110 got_worktree_get_root_path(worktree), 0);
2114 error = got_worktree_revert(worktree, path,
2115 revert_progress, NULL, repo);
2120 got_repo_close(repo);
2122 got_worktree_close(worktree);
2131 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2136 spawn_editor(const char *file)
2138 char *argp[] = { "sh", "-c", NULL, NULL };
2139 char *editor, *editp;
2141 sig_t sighup, sigint, sigquit;
2144 editor = getenv("VISUAL");
2146 editor = getenv("EDITOR");
2150 if (asprintf(&editp, "%s %s", editor, file) == -1)
2155 sighup = signal(SIGHUP, SIG_IGN);
2156 sigint = signal(SIGINT, SIG_IGN);
2157 sigquit = signal(SIGQUIT, SIG_IGN);
2159 switch (pid = fork()) {
2163 execv(_PATH_BSHELL, argp);
2169 while (waitpid(pid, &st, 0) == -1)
2174 (void)signal(SIGHUP, sighup);
2175 (void)signal(SIGINT, sigint);
2176 (void)signal(SIGQUIT, sigquit);
2178 if (!WIFEXITED(st)) {
2183 return WEXITSTATUS(st);
2186 static const struct got_error *
2187 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2190 struct got_pathlist_entry *pe;
2191 const struct got_error *err = NULL;
2192 char *tmpfile = NULL;
2193 char *cmdline_log = (char *)arg;
2195 struct stat st, st2;
2200 /* if a message was specified on the command line, just use it */
2201 if (cmdline_log != NULL && strlen(cmdline_log) != 0) {
2202 len = strlen(cmdline_log) + 1;
2203 *logmsg = malloc(len + 1);
2204 if (*logmsg == NULL)
2205 return got_error_prefix_errno("malloc");
2206 strlcpy(*logmsg, cmdline_log, len);
2210 err = got_opentemp_named_fd(&tmpfile, &fd, "got-XXXXXXXXXX");
2215 "# changes to be committed:\n");
2217 TAILQ_FOREACH(pe, commitable_paths, entry) {
2218 struct got_commitable *ct = pe->data;
2219 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2223 if (stat(tmpfile, &st) == -1) {
2224 err = got_error_prefix_errno2("stat", tmpfile);
2228 if (spawn_editor(tmpfile) == -1) {
2229 err = got_error_prefix_errno("failed spawning editor");
2233 if (stat(tmpfile, &st2) == -1) {
2234 err = got_error_prefix_errno("stat");
2238 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2239 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2240 "no changes made to commit message, aborting");
2244 /* remove comments */
2245 *logmsg = malloc(st2.st_size + 1);
2246 if (*logmsg == NULL) {
2247 err = got_error_prefix_errno("malloc");
2252 fp = fopen(tmpfile, "r");
2253 while (fgets(buf, sizeof(buf), fp) != NULL) {
2254 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2256 len = strlcat(*logmsg, buf, st2.st_size);
2260 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2261 (*logmsg)[len - 1] = '\0';
2266 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2267 "commit message cannot be empty, aborting");
2279 static const struct got_error *
2280 cmd_commit(int argc, char *argv[])
2282 const struct got_error *error = NULL;
2283 struct got_worktree *worktree = NULL;
2284 struct got_repository *repo = NULL;
2285 char *cwd = NULL, *path = NULL, *id_str = NULL;
2286 struct got_object_id *id = NULL;
2287 const char *logmsg = NULL;
2288 const char *got_author = getenv("GOT_AUTHOR");
2291 while ((ch = getopt(argc, argv, "m:")) != -1) {
2306 path = realpath(argv[0], NULL);
2308 error = got_error_prefix_errno2("realpath", argv[0]);
2311 got_path_strip_trailing_slashes(path);
2312 } else if (argc != 0)
2315 if (got_author == NULL) {
2316 /* TODO: Look current user up in password database */
2317 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2321 cwd = getcwd(NULL, 0);
2323 error = got_error_prefix_errno("getcwd");
2326 error = got_worktree_open(&worktree, cwd);
2330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2335 error = apply_unveil(got_repo_get_path(repo), 0,
2336 got_worktree_get_root_path(worktree), 0);
2341 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2342 collect_commit_logmsg, (void *)logmsg, print_status, NULL, repo);
2346 error = got_object_id_str(&id_str, id);
2349 printf("created commit %s\n", id_str);
2352 got_repo_close(repo);
2354 got_worktree_close(worktree);