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"
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 static volatile sig_atomic_t sigint_received;
53 static volatile sig_atomic_t sigpipe_received;
56 catch_sigint(int signo)
62 catch_sigpipe(int signo)
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 const char *cmd_descr;
75 __dead static void usage(void);
76 __dead static void usage_checkout(void);
77 __dead static void usage_update(void);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
82 __dead static void usage_status(void);
83 __dead static void usage_ref(void);
84 __dead static void usage_add(void);
85 __dead static void usage_rm(void);
86 __dead static void usage_revert(void);
87 __dead static void usage_commit(void);
89 static const struct got_error* cmd_checkout(int, char *[]);
90 static const struct got_error* cmd_update(int, char *[]);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_status(int, char *[]);
96 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct got_error* cmd_add(int, char *[]);
98 static const struct got_error* cmd_rm(int, char *[]);
99 static const struct got_error* cmd_revert(int, char *[]);
100 static const struct got_error* cmd_commit(int, char *[]);
102 static struct cmd got_commands[] = {
103 { "checkout", cmd_checkout, usage_checkout,
104 "check out a new work tree from a repository" },
105 { "update", cmd_update, usage_update,
106 "update a work tree to a different commit" },
107 { "log", cmd_log, usage_log,
108 "show repository history" },
109 { "diff", cmd_diff, usage_diff,
110 "compare files and directories" },
111 { "blame", cmd_blame, usage_blame,
112 "show when lines in a file were changed" },
113 { "tree", cmd_tree, usage_tree,
114 "list files and directories in repository" },
115 { "status", cmd_status, usage_status,
116 "show modification status of files" },
117 { "ref", cmd_ref, usage_ref,
118 "manage references in repository" },
119 { "add", cmd_add, usage_add,
120 "add new files to version control" },
121 { "rm", cmd_rm, usage_rm,
122 "remove a versioned file" },
123 { "revert", cmd_revert, usage_revert,
124 "revert uncommitted changes" },
125 { "commit", cmd_commit, usage_commit,
126 "write changes from work tree to repository" },
130 main(int argc, char *argv[])
137 setlocale(LC_CTYPE, "");
139 while ((ch = getopt(argc, argv, "h")) != -1) {
157 signal(SIGINT, catch_sigint);
158 signal(SIGPIPE, catch_sigpipe);
160 for (i = 0; i < nitems(got_commands); i++) {
161 const struct got_error *error;
163 cmd = &got_commands[i];
165 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
169 got_commands[i].cmd_usage();
171 error = got_commands[i].cmd_main(argc, argv);
172 if (error && !(sigint_received || sigpipe_received)) {
173 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
180 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
189 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
190 "Available commands:\n", getprogname());
191 for (i = 0; i < nitems(got_commands); i++) {
192 struct cmd *cmd = &got_commands[i];
193 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
198 static const struct got_error *
199 apply_unveil(const char *repo_path, int repo_read_only,
200 const char *worktree_path, int create_worktree)
202 const struct got_error *error;
203 static char err_msg[MAXPATHLEN + 36];
205 if (create_worktree) {
206 /* Pre-create work tree path to avoid unveiling its parents. */
207 error = got_path_mkdir(worktree_path);
209 if (errno == EEXIST) {
210 if (got_path_dir_is_empty(worktree_path)) {
214 snprintf(err_msg, sizeof(err_msg),
215 "%s: directory exists but is not empty",
217 error = got_error_msg(GOT_ERR_BAD_PATH,
222 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
226 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
227 return got_error_prefix_errno2("unveil", repo_path);
229 if (worktree_path && unveil(worktree_path, "rwc") != 0)
230 return got_error_prefix_errno2("unveil", worktree_path);
232 if (unveil("/tmp", "rwc") != 0)
233 return got_error_prefix_errno2("unveil", "/tmp");
235 error = got_privsep_unveil_exec_helpers();
239 if (unveil(NULL, NULL) != 0)
240 return got_error_prefix_errno("unveil");
248 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
249 "[worktree-path]\n", getprogname());
254 checkout_progress(void *arg, unsigned char status, const char *path)
256 char *worktree_path = arg;
258 while (path[0] == '/')
261 printf("%c %s/%s\n", status, worktree_path, path);
264 static const struct got_error *
265 check_cancelled(void *arg)
267 if (sigint_received || sigpipe_received)
268 return got_error(GOT_ERR_CANCELLED);
272 static const struct got_error *
273 check_linear_ancestry(struct got_worktree *worktree,
274 struct got_object_id *commit_id, struct got_repository *repo)
276 const struct got_error *err = NULL;
277 struct got_object_id *yca_id, *base_commit_id;
279 base_commit_id = got_worktree_get_base_commit_id(worktree);
280 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
281 commit_id, base_commit_id, repo);
286 return got_error(GOT_ERR_ANCESTRY);
289 * Require a straight line of history between the target commit
290 * and the work tree's base commit.
292 * Non-linear situation such as the this require a rebase:
294 * (commit) D F (base_commit)
302 * 'got update' only handles linear cases:
303 * Update forwards in time: A (base/yca) - B - C - D (commit)
304 * Update backwards in time: D (base) - C - D - A (commit/yca)
306 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
307 got_object_id_cmp(base_commit_id, yca_id) != 0)
308 return got_error(GOT_ERR_ANCESTRY);
315 static const struct got_error *
316 cmd_checkout(int argc, char *argv[])
318 const struct got_error *error = NULL;
319 struct got_repository *repo = NULL;
320 struct got_reference *head_ref = NULL;
321 struct got_worktree *worktree = NULL;
322 char *repo_path = NULL;
323 char *worktree_path = NULL;
324 const char *path_prefix = "";
325 char *commit_id_str = NULL;
326 int ch, same_path_prefix;
328 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
331 commit_id_str = strdup(optarg);
332 if (commit_id_str == NULL)
333 return got_error_prefix_errno("strdup");
336 path_prefix = optarg;
348 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
349 "unveil", NULL) == -1)
353 char *cwd, *base, *dotgit;
354 repo_path = realpath(argv[0], NULL);
355 if (repo_path == NULL)
356 return got_error_prefix_errno2("realpath", argv[0]);
357 cwd = getcwd(NULL, 0);
359 error = got_error_prefix_errno("getcwd");
362 if (path_prefix[0]) {
363 base = basename(path_prefix);
365 error = got_error_prefix_errno2("basename",
370 base = basename(repo_path);
372 error = got_error_prefix_errno2("basename",
377 dotgit = strstr(base, ".git");
380 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
381 error = got_error_prefix_errno("asprintf");
386 } else if (argc == 2) {
387 repo_path = realpath(argv[0], NULL);
388 if (repo_path == NULL) {
389 error = got_error_prefix_errno2("realpath", argv[0]);
392 worktree_path = realpath(argv[1], NULL);
393 if (worktree_path == NULL) {
394 error = got_error_prefix_errno2("realpath", argv[1]);
400 got_path_strip_trailing_slashes(repo_path);
401 got_path_strip_trailing_slashes(worktree_path);
403 error = got_repo_open(&repo, repo_path);
407 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
411 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
415 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
416 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
419 error = got_worktree_open(&worktree, worktree_path);
423 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
427 if (!same_path_prefix) {
428 error = got_error(GOT_ERR_PATH_PREFIX);
433 struct got_object_id *commit_id;
434 error = got_object_resolve_id_str(&commit_id, repo,
438 error = check_linear_ancestry(worktree, commit_id, repo);
443 error = got_worktree_set_base_commit_id(worktree, repo,
450 error = got_worktree_checkout_files(worktree, "", repo,
451 checkout_progress, worktree_path, check_cancelled, NULL);
455 printf("Now shut up and hack\n");
467 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
473 update_progress(void *arg, unsigned char status, const char *path)
475 int *did_something = arg;
477 if (status == GOT_STATUS_EXISTS)
481 while (path[0] == '/')
483 printf("%c %s\n", status, path);
486 static const struct got_error *
487 cmd_update(int argc, char *argv[])
489 const struct got_error *error = NULL;
490 struct got_repository *repo = NULL;
491 struct got_worktree *worktree = NULL;
492 char *worktree_path = NULL, *path = NULL;
493 struct got_object_id *commit_id = NULL;
494 char *commit_id_str = NULL;
495 int ch, did_something = 0;
497 while ((ch = getopt(argc, argv, "c:")) != -1) {
500 commit_id_str = strdup(optarg);
501 if (commit_id_str == NULL)
502 return got_error_prefix_errno("strdup");
514 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
515 "unveil", NULL) == -1)
518 worktree_path = getcwd(NULL, 0);
519 if (worktree_path == NULL) {
520 error = got_error_prefix_errno("getcwd");
523 error = got_worktree_open(&worktree, worktree_path);
530 error = got_error_prefix_errno("strdup");
533 } else if (argc == 1) {
534 error = got_worktree_resolve_path(&path, worktree, argv[0]);
540 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
544 error = apply_unveil(got_repo_get_path(repo), 0,
545 got_worktree_get_root_path(worktree), 0);
549 if (commit_id_str == NULL) {
550 struct got_reference *head_ref;
551 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
554 error = got_ref_resolve(&commit_id, repo, head_ref);
557 error = got_object_id_str(&commit_id_str, commit_id);
561 error = got_object_resolve_id_str(&commit_id, repo,
567 error = check_linear_ancestry(worktree, commit_id, repo);
571 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
573 error = got_worktree_set_base_commit_id(worktree, repo,
579 error = got_worktree_checkout_files(worktree, path, repo,
580 update_progress, &did_something, check_cancelled, NULL);
585 printf("Updated to commit %s\n", commit_id_str);
587 printf("Already up-to-date\n");
596 static const struct got_error *
597 print_patch(struct got_commit_object *commit, struct got_object_id *id,
598 int diff_context, struct got_repository *repo)
600 const struct got_error *err = NULL;
601 struct got_tree_object *tree1 = NULL, *tree2;
602 struct got_object_qid *qid;
603 char *id_str1 = NULL, *id_str2;
605 err = got_object_open_as_tree(&tree2, repo,
606 got_object_commit_get_tree_id(commit));
610 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
612 struct got_commit_object *pcommit;
614 err = got_object_open_as_commit(&pcommit, repo, qid->id);
618 err = got_object_open_as_tree(&tree1, repo,
619 got_object_commit_get_tree_id(pcommit));
620 got_object_commit_close(pcommit);
624 err = got_object_id_str(&id_str1, qid->id);
629 err = got_object_id_str(&id_str2, id);
633 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
634 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
637 got_object_tree_close(tree1);
638 got_object_tree_close(tree2);
645 get_datestr(time_t *time, char *datebuf)
647 char *p, *s = ctime_r(time, datebuf);
654 static const struct got_error *
655 print_commit(struct got_commit_object *commit, struct got_object_id *id,
656 struct got_repository *repo, int show_patch, int diff_context,
657 struct got_reflist_head *refs)
659 const struct got_error *err = NULL;
660 char *id_str, *datestr, *logmsg0, *logmsg, *line;
662 time_t committer_time;
663 const char *author, *committer;
664 char *refs_str = NULL;
665 struct got_reflist_entry *re;
667 SIMPLEQ_FOREACH(re, refs, entry) {
670 if (got_object_id_cmp(re->id, id) != 0)
672 name = got_ref_get_name(re->ref);
673 if (strcmp(name, GOT_REF_HEAD) == 0)
675 if (strncmp(name, "refs/", 5) == 0)
677 if (strncmp(name, "got/", 4) == 0)
679 if (strncmp(name, "heads/", 6) == 0)
681 if (strncmp(name, "remotes/", 8) == 0)
684 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
686 err = got_error_prefix_errno("asprintf");
692 err = got_object_id_str(&id_str, id);
696 printf("-----------------------------------------------\n");
697 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
698 refs_str ? refs_str : "", refs_str ? ")" : "");
703 printf("from: %s\n", got_object_commit_get_author(commit));
704 committer_time = got_object_commit_get_committer_time(commit);
705 datestr = get_datestr(&committer_time, datebuf);
706 printf("date: %s UTC\n", datestr);
707 author = got_object_commit_get_author(commit);
708 committer = got_object_commit_get_committer(commit);
709 if (strcmp(author, committer) != 0)
710 printf("via: %s\n", committer);
711 if (got_object_commit_get_nparents(commit) > 1) {
712 const struct got_object_id_queue *parent_ids;
713 struct got_object_qid *qid;
715 parent_ids = got_object_commit_get_parent_ids(commit);
716 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
717 err = got_object_id_str(&id_str, qid->id);
720 printf("parent %d: %s\n", n++, id_str);
725 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
727 return got_error_prefix_errno("strdup");
731 line = strsep(&logmsg, "\n");
733 printf(" %s\n", line);
738 err = print_patch(commit, id, diff_context, repo);
743 if (fflush(stdout) != 0 && err == NULL)
744 err = got_error_prefix_errno("fflush");
748 static const struct got_error *
749 print_commits(struct got_object_id *root_id, struct got_repository *repo,
750 char *path, int show_patch, int diff_context, int limit,
751 int first_parent_traversal, struct got_reflist_head *refs)
753 const struct got_error *err;
754 struct got_commit_graph *graph;
756 err = got_commit_graph_open(&graph, root_id, path,
757 first_parent_traversal, repo);
760 err = got_commit_graph_iter_start(graph, root_id, repo);
764 struct got_commit_object *commit;
765 struct got_object_id *id;
767 if (sigint_received || sigpipe_received)
770 err = got_commit_graph_iter_next(&id, graph);
772 if (err->code == GOT_ERR_ITER_COMPLETED) {
776 if (err->code != GOT_ERR_ITER_NEED_MORE)
778 err = got_commit_graph_fetch_commits(graph, 1, repo);
787 err = got_object_open_as_commit(&commit, repo, id);
790 err = print_commit(commit, id, repo, show_patch, diff_context,
792 got_object_commit_close(commit);
793 if (err || (limit && --limit == 0))
797 got_commit_graph_close(graph);
804 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
805 "[-r repository-path] [path]\n", getprogname());
809 static const struct got_error *
810 cmd_log(int argc, char *argv[])
812 const struct got_error *error;
813 struct got_repository *repo = NULL;
814 struct got_worktree *worktree = NULL;
815 struct got_commit_object *commit = NULL;
816 struct got_object_id *id = NULL;
817 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
818 char *start_commit = NULL;
819 int diff_context = 3, ch;
820 int show_patch = 0, limit = 0, first_parent_traversal = 0;
822 struct got_reflist_head refs;
827 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
833 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
839 start_commit = optarg;
842 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
845 err(1, "-C option %s", errstr);
848 limit = strtonum(optarg, 1, INT_MAX, &errstr);
850 err(1, "-l option %s", errstr);
853 first_parent_traversal = 1;
856 repo_path = realpath(optarg, NULL);
857 if (repo_path == NULL)
859 got_path_strip_trailing_slashes(repo_path);
870 cwd = getcwd(NULL, 0);
872 error = got_error_prefix_errno("getcwd");
876 error = got_worktree_open(&worktree, cwd);
877 if (error && error->code != GOT_ERR_NOT_WORKTREE)
884 error = got_error_prefix_errno("strdup");
887 } else if (argc == 1) {
889 error = got_worktree_resolve_path(&path, worktree,
894 path = strdup(argv[0]);
896 error = got_error_prefix_errno("strdup");
903 if (repo_path == NULL) {
904 repo_path = worktree ?
905 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
907 if (repo_path == NULL) {
908 error = got_error_prefix_errno("strdup");
912 error = got_repo_open(&repo, repo_path);
916 error = apply_unveil(got_repo_get_path(repo), 1,
917 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
921 if (start_commit == NULL) {
922 struct got_reference *head_ref;
923 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
926 error = got_ref_resolve(&id, repo, head_ref);
927 got_ref_close(head_ref);
930 error = got_object_open_as_commit(&commit, repo, id);
932 struct got_reference *ref;
933 error = got_ref_open(&ref, repo, start_commit, 0);
936 error = got_ref_resolve(&id, repo, ref);
940 error = got_object_get_type(&obj_type, repo, id);
943 if (obj_type == GOT_OBJ_TYPE_TAG) {
944 struct got_tag_object *tag;
945 error = got_object_open_as_tag(&tag, repo, id);
948 if (got_object_tag_get_object_type(tag) !=
949 GOT_OBJ_TYPE_COMMIT) {
950 got_object_tag_close(tag);
951 error = got_error(GOT_ERR_OBJ_TYPE);
955 id = got_object_id_dup(
956 got_object_tag_get_object_id(tag));
958 error = got_error_prefix_errno(
959 "got_object_id_dup");
960 got_object_tag_close(tag);
963 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
964 error = got_error(GOT_ERR_OBJ_TYPE);
967 error = got_object_open_as_commit(&commit, repo, id);
971 if (commit == NULL) {
972 error = got_object_resolve_id_str(&id, repo,
981 error = got_repo_map_path(&in_repo_path, repo, path, 1);
989 error = got_ref_list(&refs, repo);
993 error = print_commits(id, repo, path, show_patch,
994 diff_context, limit, first_parent_traversal, &refs);
1001 got_worktree_close(worktree);
1003 const struct got_error *repo_error;
1004 repo_error = got_repo_close(repo);
1008 got_ref_list_free(&refs);
1015 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1016 "[object1 object2 | path]\n", getprogname());
1020 struct print_diff_arg {
1021 struct got_repository *repo;
1022 struct got_worktree *worktree;
1028 static const struct got_error *
1029 print_diff(void *arg, unsigned char status, const char *path,
1030 struct got_object_id *id)
1032 struct print_diff_arg *a = arg;
1033 const struct got_error *err = NULL;
1034 struct got_blob_object *blob1 = NULL;
1036 char *abspath = NULL;
1039 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1040 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1043 if (!a->header_shown) {
1044 printf("diff %s %s\n", a->id_str,
1045 got_worktree_get_root_path(a->worktree));
1046 a->header_shown = 1;
1049 if (status != GOT_STATUS_ADD) {
1050 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1056 if (status != GOT_STATUS_DELETE) {
1057 if (asprintf(&abspath, "%s/%s",
1058 got_worktree_get_root_path(a->worktree), path) == -1) {
1059 err = got_error_prefix_errno("asprintf");
1063 f2 = fopen(abspath, "r");
1065 err = got_error_prefix_errno2("fopen", abspath);
1068 if (lstat(abspath, &sb) == -1) {
1069 err = got_error_prefix_errno2("lstat", abspath);
1075 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1079 got_object_blob_close(blob1);
1080 if (f2 && fclose(f2) != 0 && err == NULL)
1081 err = got_error_prefix_errno("fclose");
1086 static const struct got_error *
1087 cmd_diff(int argc, char *argv[])
1089 const struct got_error *error;
1090 struct got_repository *repo = NULL;
1091 struct got_worktree *worktree = NULL;
1092 char *cwd = NULL, *repo_path = NULL;
1093 struct got_object_id *id1 = NULL, *id2 = NULL;
1094 char *id_str1 = NULL, *id_str2 = NULL;
1096 int diff_context = 3, ch;
1101 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1106 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1109 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1111 err(1, "-C option %s", errstr);
1114 repo_path = realpath(optarg, NULL);
1115 if (repo_path == NULL)
1116 err(1, "-r option");
1117 got_path_strip_trailing_slashes(repo_path);
1128 cwd = getcwd(NULL, 0);
1130 error = got_error_prefix_errno("getcwd");
1133 error = got_worktree_open(&worktree, cwd);
1134 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1137 if (worktree == NULL) {
1138 error = got_error(GOT_ERR_NOT_WORKTREE);
1143 "-r option can't be used when diffing a work tree");
1144 repo_path = strdup(got_worktree_get_repo_path(worktree));
1145 if (repo_path == NULL) {
1146 error = got_error_prefix_errno("strdup");
1150 error = got_worktree_resolve_path(&path, worktree,
1157 error = got_error_prefix_errno("strdup");
1161 } else if (argc == 2) {
1167 if (repo_path == NULL) {
1168 repo_path = getcwd(NULL, 0);
1169 if (repo_path == NULL)
1170 return got_error_prefix_errno("getcwd");
1173 error = got_repo_open(&repo, repo_path);
1178 error = apply_unveil(got_repo_get_path(repo), 1,
1179 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1184 struct print_diff_arg arg;
1186 error = got_object_id_str(&id_str,
1187 got_worktree_get_base_commit_id(worktree));
1191 arg.worktree = worktree;
1192 arg.diff_context = diff_context;
1193 arg.id_str = id_str;
1194 arg.header_shown = 0;
1196 error = got_worktree_status(worktree, path, repo, print_diff,
1197 &arg, check_cancelled, NULL);
1202 error = got_object_resolve_id_str(&id1, repo, id_str1);
1206 error = got_object_resolve_id_str(&id2, repo, id_str2);
1210 error = got_object_get_type(&type1, repo, id1);
1214 error = got_object_get_type(&type2, repo, id2);
1218 if (type1 != type2) {
1219 error = got_error(GOT_ERR_OBJ_TYPE);
1224 case GOT_OBJ_TYPE_BLOB:
1225 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1226 diff_context, repo, stdout);
1228 case GOT_OBJ_TYPE_TREE:
1229 error = got_diff_objects_as_trees(id1, id2, "", "",
1230 diff_context, repo, stdout);
1232 case GOT_OBJ_TYPE_COMMIT:
1233 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1235 error = got_diff_objects_as_commits(id1, id2, diff_context,
1239 error = got_error(GOT_ERR_OBJ_TYPE);
1247 got_worktree_close(worktree);
1249 const struct got_error *repo_error;
1250 repo_error = got_repo_close(repo);
1261 "usage: %s blame [-c commit] [-r repository-path] path\n",
1266 static const struct got_error *
1267 cmd_blame(int argc, char *argv[])
1269 const struct got_error *error;
1270 struct got_repository *repo = NULL;
1271 struct got_worktree *worktree = NULL;
1272 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1273 struct got_object_id *commit_id = NULL;
1274 char *commit_id_str = NULL;
1278 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1283 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1286 commit_id_str = optarg;
1289 repo_path = realpath(optarg, NULL);
1290 if (repo_path == NULL)
1291 err(1, "-r option");
1292 got_path_strip_trailing_slashes(repo_path);
1308 cwd = getcwd(NULL, 0);
1310 error = got_error_prefix_errno("getcwd");
1313 if (repo_path == NULL) {
1314 error = got_worktree_open(&worktree, cwd);
1315 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1321 strdup(got_worktree_get_repo_path(worktree));
1322 if (repo_path == NULL)
1323 error = got_error_prefix_errno("strdup");
1327 repo_path = strdup(cwd);
1328 if (repo_path == NULL) {
1329 error = got_error_prefix_errno("strdup");
1335 error = got_repo_open(&repo, repo_path);
1339 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1344 const char *prefix = got_worktree_get_path_prefix(worktree);
1345 char *p, *worktree_subdir = cwd +
1346 strlen(got_worktree_get_root_path(worktree));
1347 if (asprintf(&p, "%s%s%s%s%s",
1348 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1349 worktree_subdir, worktree_subdir[0] ? "/" : "",
1351 error = got_error_prefix_errno("asprintf");
1354 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1357 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1362 if (commit_id_str == NULL) {
1363 struct got_reference *head_ref;
1364 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1367 error = got_ref_resolve(&commit_id, repo, head_ref);
1368 got_ref_close(head_ref);
1372 error = got_object_resolve_id_str(&commit_id, repo,
1378 error = got_blame(in_repo_path, commit_id, repo, stdout);
1385 got_worktree_close(worktree);
1387 const struct got_error *repo_error;
1388 repo_error = got_repo_close(repo);
1399 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1405 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1406 const char *root_path)
1408 int is_root_path = (strcmp(path, root_path) == 0);
1410 path += strlen(root_path);
1411 while (path[0] == '/')
1414 printf("%s%s%s%s%s\n", id ? id : "", path,
1415 is_root_path ? "" : "/", te->name,
1416 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1419 static const struct got_error *
1420 print_tree(const char *path, struct got_object_id *commit_id,
1421 int show_ids, int recurse, const char *root_path,
1422 struct got_repository *repo)
1424 const struct got_error *err = NULL;
1425 struct got_object_id *tree_id = NULL;
1426 struct got_tree_object *tree = NULL;
1427 const struct got_tree_entries *entries;
1428 struct got_tree_entry *te;
1430 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1434 err = got_object_open_as_tree(&tree, repo, tree_id);
1437 entries = got_object_tree_get_entries(tree);
1438 te = SIMPLEQ_FIRST(&entries->head);
1442 if (sigint_received || sigpipe_received)
1447 err = got_object_id_str(&id_str, te->id);
1450 if (asprintf(&id, "%s ", id_str) == -1) {
1451 err = got_error_prefix_errno("asprintf");
1457 print_entry(te, id, path, root_path);
1460 if (recurse && S_ISDIR(te->mode)) {
1462 if (asprintf(&child_path, "%s%s%s", path,
1463 path[0] == '/' && path[1] == '\0' ? "" : "/",
1465 err = got_error_prefix_errno("asprintf");
1468 err = print_tree(child_path, commit_id, show_ids, 1,
1475 te = SIMPLEQ_NEXT(te, entry);
1479 got_object_tree_close(tree);
1484 static const struct got_error *
1485 cmd_tree(int argc, char *argv[])
1487 const struct got_error *error;
1488 struct got_repository *repo = NULL;
1489 struct got_worktree *worktree = NULL;
1491 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1492 struct got_object_id *commit_id = NULL;
1493 char *commit_id_str = NULL;
1494 int show_ids = 0, recurse = 0;
1498 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1503 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1506 commit_id_str = optarg;
1509 repo_path = realpath(optarg, NULL);
1510 if (repo_path == NULL)
1511 err(1, "-r option");
1512 got_path_strip_trailing_slashes(repo_path);
1536 cwd = getcwd(NULL, 0);
1538 error = got_error_prefix_errno("getcwd");
1541 if (repo_path == NULL) {
1542 error = got_worktree_open(&worktree, cwd);
1543 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1549 strdup(got_worktree_get_repo_path(worktree));
1550 if (repo_path == NULL)
1551 error = got_error_prefix_errno("strdup");
1555 repo_path = strdup(cwd);
1556 if (repo_path == NULL) {
1557 error = got_error_prefix_errno("strdup");
1563 error = got_repo_open(&repo, repo_path);
1567 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1573 char *p, *worktree_subdir = cwd +
1574 strlen(got_worktree_get_root_path(worktree));
1575 if (asprintf(&p, "%s/%s",
1576 got_worktree_get_path_prefix(worktree),
1577 worktree_subdir) == -1) {
1578 error = got_error_prefix_errno("asprintf");
1581 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1588 if (in_repo_path == NULL) {
1589 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1594 if (commit_id_str == NULL) {
1595 struct got_reference *head_ref;
1596 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1599 error = got_ref_resolve(&commit_id, repo, head_ref);
1600 got_ref_close(head_ref);
1604 error = got_object_resolve_id_str(&commit_id, repo,
1610 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1611 in_repo_path, repo);
1618 got_worktree_close(worktree);
1620 const struct got_error *repo_error;
1621 repo_error = got_repo_close(repo);
1631 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1635 static const struct got_error *
1636 print_status(void *arg, unsigned char status, const char *path,
1637 struct got_object_id *id)
1639 printf("%c %s\n", status, path);
1643 static const struct got_error *
1644 cmd_status(int argc, char *argv[])
1646 const struct got_error *error = NULL;
1647 struct got_repository *repo = NULL;
1648 struct got_worktree *worktree = NULL;
1649 char *cwd = NULL, *path = NULL;
1652 while ((ch = getopt(argc, argv, "")) != -1) {
1664 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1668 cwd = getcwd(NULL, 0);
1670 error = got_error_prefix_errno("getcwd");
1674 error = got_worktree_open(&worktree, cwd);
1681 error = got_error_prefix_errno("strdup");
1684 } else if (argc == 1) {
1685 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1691 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1695 error = apply_unveil(got_repo_get_path(repo), 1,
1696 got_worktree_get_root_path(worktree), 0);
1700 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1701 check_cancelled, NULL);
1712 "usage: %s ref [-r repository] -l | -d name | name object\n",
1717 static const struct got_error *
1718 list_refs(struct got_repository *repo)
1720 static const struct got_error *err = NULL;
1721 struct got_reflist_head refs;
1722 struct got_reflist_entry *re;
1724 SIMPLEQ_INIT(&refs);
1725 err = got_ref_list(&refs, repo);
1729 SIMPLEQ_FOREACH(re, &refs, entry) {
1731 refstr = got_ref_to_str(re->ref);
1733 return got_error_prefix_errno("got_ref_to_str");
1734 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1738 got_ref_list_free(&refs);
1742 static const struct got_error *
1743 delete_ref(struct got_repository *repo, const char *refname)
1745 const struct got_error *err = NULL;
1746 struct got_reference *ref;
1748 err = got_ref_open(&ref, repo, refname, 0);
1752 err = got_ref_delete(ref, repo);
1757 static const struct got_error *
1758 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1760 const struct got_error *err = NULL;
1761 struct got_object_id *id;
1762 struct got_reference *ref = NULL;
1764 err = got_object_resolve_id_str(&id, repo, id_str);
1768 err = got_ref_alloc(&ref, refname, id);
1772 err = got_ref_write(ref, repo);
1780 static const struct got_error *
1781 cmd_ref(int argc, char *argv[])
1783 const struct got_error *error = NULL;
1784 struct got_repository *repo = NULL;
1785 struct got_worktree *worktree = NULL;
1786 char *cwd = NULL, *repo_path = NULL;
1787 int ch, do_list = 0;
1788 const char *delref = NULL;
1790 /* TODO: Add -s option for adding symbolic references. */
1791 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1797 repo_path = realpath(optarg, NULL);
1798 if (repo_path == NULL)
1799 err(1, "-r option");
1800 got_path_strip_trailing_slashes(repo_path);
1811 if (do_list && delref)
1812 errx(1, "-l and -d options are mutually exclusive\n");
1817 if (do_list || delref) {
1820 } else if (argc != 2)
1825 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1829 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1830 "sendfd unveil", NULL) == -1)
1834 cwd = getcwd(NULL, 0);
1836 error = got_error_prefix_errno("getcwd");
1840 if (repo_path == NULL) {
1841 error = got_worktree_open(&worktree, cwd);
1842 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1848 strdup(got_worktree_get_repo_path(worktree));
1849 if (repo_path == NULL)
1850 error = got_error_prefix_errno("strdup");
1854 repo_path = strdup(cwd);
1855 if (repo_path == NULL) {
1856 error = got_error_prefix_errno("strdup");
1862 error = got_repo_open(&repo, repo_path);
1866 error = apply_unveil(got_repo_get_path(repo), do_list,
1867 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1872 error = list_refs(repo);
1874 error = delete_ref(repo, delref);
1876 error = add_ref(repo, argv[0], argv[1]);
1879 got_repo_close(repo);
1881 got_worktree_close(worktree);
1890 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1894 static const struct got_error *
1895 cmd_add(int argc, char *argv[])
1897 const struct got_error *error = NULL;
1898 struct got_repository *repo = NULL;
1899 struct got_worktree *worktree = NULL;
1901 struct got_pathlist_head paths;
1902 struct got_pathlist_entry *pe;
1907 while ((ch = getopt(argc, argv, "")) != -1) {
1921 /* make sure each file exists before doing anything halfway */
1922 for (x = 0; x < argc; x++) {
1923 char *path = realpath(argv[x], NULL);
1925 error = got_error_prefix_errno2("realpath", argv[x]);
1931 cwd = getcwd(NULL, 0);
1933 error = got_error_prefix_errno("getcwd");
1937 error = got_worktree_open(&worktree, cwd);
1941 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1945 error = apply_unveil(got_repo_get_path(repo), 1,
1946 got_worktree_get_root_path(worktree), 0);
1950 for (x = 0; x < argc; x++) {
1951 char *path = realpath(argv[x], NULL);
1953 error = got_error_prefix_errno2("realpath", argv[x]);
1957 got_path_strip_trailing_slashes(path);
1958 error = got_pathlist_insert(&pe, &paths, path, NULL);
1964 error = got_worktree_schedule_add(worktree, &paths, print_status,
1968 got_repo_close(repo);
1970 got_worktree_close(worktree);
1971 TAILQ_FOREACH(pe, &paths, entry)
1972 free((char *)pe->path);
1973 got_pathlist_free(&paths);
1981 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1985 static const struct got_error *
1986 cmd_rm(int argc, char *argv[])
1988 const struct got_error *error = NULL;
1989 struct got_worktree *worktree = NULL;
1990 struct got_repository *repo = NULL;
1991 char *cwd = NULL, *path = NULL;
1992 int ch, delete_local_mods = 0;
1994 while ((ch = getopt(argc, argv, "f")) != -1) {
1997 delete_local_mods = 1;
2011 path = realpath(argv[0], NULL);
2013 error = got_error_prefix_errno2("realpath", argv[0]);
2016 got_path_strip_trailing_slashes(path);
2018 cwd = getcwd(NULL, 0);
2020 error = got_error_prefix_errno("getcwd");
2023 error = got_worktree_open(&worktree, cwd);
2027 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2031 error = apply_unveil(got_repo_get_path(repo), 1,
2032 got_worktree_get_root_path(worktree), 0);
2036 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2037 print_status, NULL, repo);
2042 got_repo_close(repo);
2044 got_worktree_close(worktree);
2053 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2058 revert_progress(void *arg, unsigned char status, const char *path)
2060 while (path[0] == '/')
2062 printf("%c %s\n", status, path);
2065 static const struct got_error *
2066 cmd_revert(int argc, char *argv[])
2068 const struct got_error *error = NULL;
2069 struct got_worktree *worktree = NULL;
2070 struct got_repository *repo = NULL;
2071 char *cwd = NULL, *path = NULL;
2074 while ((ch = getopt(argc, argv, "")) != -1) {
2088 path = realpath(argv[0], NULL);
2090 error = got_error_prefix_errno2("realpath", argv[0]);
2093 got_path_strip_trailing_slashes(path);
2095 cwd = getcwd(NULL, 0);
2097 error = got_error_prefix_errno("getcwd");
2100 error = got_worktree_open(&worktree, cwd);
2104 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2108 error = apply_unveil(got_repo_get_path(repo), 1,
2109 got_worktree_get_root_path(worktree), 0);
2113 error = got_worktree_revert(worktree, path,
2114 revert_progress, NULL, repo);
2119 got_repo_close(repo);
2121 got_worktree_close(worktree);
2130 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2135 spawn_editor(const char *file)
2137 char *argp[] = { "sh", "-c", NULL, NULL };
2138 char *editor, *editp;
2140 sig_t sighup, sigint, sigquit;
2143 editor = getenv("VISUAL");
2145 editor = getenv("EDITOR");
2149 if (asprintf(&editp, "%s %s", editor, file) == -1)
2154 sighup = signal(SIGHUP, SIG_IGN);
2155 sigint = signal(SIGINT, SIG_IGN);
2156 sigquit = signal(SIGQUIT, SIG_IGN);
2158 switch (pid = fork()) {
2162 execv(_PATH_BSHELL, argp);
2168 while (waitpid(pid, &st, 0) == -1)
2173 (void)signal(SIGHUP, sighup);
2174 (void)signal(SIGINT, sigint);
2175 (void)signal(SIGQUIT, sigquit);
2177 if (!WIFEXITED(st)) {
2182 return WEXITSTATUS(st);
2185 static const struct got_error *
2186 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2189 struct got_pathlist_entry *pe;
2190 const struct got_error *err = NULL;
2191 char *tmppath = 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 strlcpy(*logmsg, cmdline_log, len);
2208 tmppath = getenv("TMPDIR");
2209 if (tmppath == NULL || strlen(tmppath) == 0)
2212 if (asprintf(&tmpfile, "%s/got-XXXXXXXXXX", tmppath) == -1) {
2213 err = got_error_prefix_errno("asprintf");
2217 fd = mkstemp(tmpfile);
2219 err = got_error_prefix_errno("mktemp");
2225 "# changes to be committed:\n");
2227 TAILQ_FOREACH(pe, commitable_paths, entry) {
2228 struct got_commitable *ct = pe->data;
2229 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2233 if (stat(tmpfile, &st) == -1) {
2234 err = got_error_prefix_errno2("stat", tmpfile);
2238 if (spawn_editor(tmpfile) == -1) {
2239 err = got_error_prefix_errno("failed spawning editor");
2243 if (stat(tmpfile, &st2) == -1) {
2244 err = got_error_prefix_errno("stat");
2248 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2249 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2250 "no changes made to commit message, aborting");
2254 /* remove comments */
2255 *logmsg = malloc(st2.st_size + 1);
2258 fp = fopen(tmpfile, "r");
2259 while (fgets(buf, sizeof(buf), fp) != NULL) {
2260 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2262 len = strlcat(*logmsg, buf, st2.st_size);
2266 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2267 (*logmsg)[len - 1] = '\0';
2272 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2273 "commit message cannot be empty, aborting");
2288 static const struct got_error *
2289 cmd_commit(int argc, char *argv[])
2291 const struct got_error *error = NULL;
2292 struct got_worktree *worktree = NULL;
2293 struct got_repository *repo = NULL;
2294 char *cwd = NULL, *path = NULL, *id_str = NULL;
2295 struct got_object_id *id = NULL;
2296 const char *logmsg = NULL;
2297 const char *got_author = getenv("GOT_AUTHOR");
2300 while ((ch = getopt(argc, argv, "m:")) != -1) {
2315 path = realpath(argv[0], NULL);
2317 error = got_error_prefix_errno2("realpath", argv[0]);
2320 got_path_strip_trailing_slashes(path);
2321 } else if (argc != 0)
2324 if (got_author == NULL) {
2325 /* TODO: Look current user up in password database */
2326 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2330 cwd = getcwd(NULL, 0);
2332 error = got_error_prefix_errno("getcwd");
2335 error = got_worktree_open(&worktree, cwd);
2339 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2344 error = apply_unveil(got_repo_get_path(repo), 0,
2345 got_worktree_get_root_path(worktree), 0);
2350 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2351 collect_commit_logmsg, (void *)logmsg, print_status, NULL, repo);
2355 error = got_object_id_str(&id_str, id);
2358 printf("created commit %s\n", id_str);
2361 got_repo_close(repo);
2363 got_worktree_close(worktree);