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);
89 __dead static void usage_cherrypick(void);
90 __dead static void usage_backout(void);
92 static const struct got_error* cmd_checkout(int, char *[]);
93 static const struct got_error* cmd_update(int, char *[]);
94 static const struct got_error* cmd_log(int, char *[]);
95 static const struct got_error* cmd_diff(int, char *[]);
96 static const struct got_error* cmd_blame(int, char *[]);
97 static const struct got_error* cmd_tree(int, char *[]);
98 static const struct got_error* cmd_status(int, char *[]);
99 static const struct got_error* cmd_ref(int, char *[]);
100 static const struct got_error* cmd_add(int, char *[]);
101 static const struct got_error* cmd_rm(int, char *[]);
102 static const struct got_error* cmd_revert(int, char *[]);
103 static const struct got_error* cmd_commit(int, char *[]);
104 static const struct got_error* cmd_cherrypick(int, char *[]);
105 static const struct got_error* cmd_backout(int, char *[]);
107 static struct cmd got_commands[] = {
108 { "checkout", cmd_checkout, usage_checkout,
109 "check out a new work tree from a repository" },
110 { "update", cmd_update, usage_update,
111 "update a work tree to a different commit" },
112 { "log", cmd_log, usage_log,
113 "show repository history" },
114 { "diff", cmd_diff, usage_diff,
115 "compare files and directories" },
116 { "blame", cmd_blame, usage_blame,
117 "show when lines in a file were changed" },
118 { "tree", cmd_tree, usage_tree,
119 "list files and directories in repository" },
120 { "status", cmd_status, usage_status,
121 "show modification status of files" },
122 { "ref", cmd_ref, usage_ref,
123 "manage references in repository" },
124 { "add", cmd_add, usage_add,
125 "add new files to version control" },
126 { "rm", cmd_rm, usage_rm,
127 "remove a versioned file" },
128 { "revert", cmd_revert, usage_revert,
129 "revert uncommitted changes" },
130 { "commit", cmd_commit, usage_commit,
131 "write changes from work tree to repository" },
132 { "cherrypick", cmd_cherrypick, usage_cherrypick,
133 "merge a single commit from another branch into a work tree" },
134 { "backout", cmd_backout, usage_backout,
135 "reverse-merge changes from a commit into a work tree" },
139 main(int argc, char *argv[])
146 setlocale(LC_CTYPE, "");
148 while ((ch = getopt(argc, argv, "h")) != -1) {
166 signal(SIGINT, catch_sigint);
167 signal(SIGPIPE, catch_sigpipe);
169 for (i = 0; i < nitems(got_commands); i++) {
170 const struct got_error *error;
172 cmd = &got_commands[i];
174 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
178 got_commands[i].cmd_usage();
180 error = got_commands[i].cmd_main(argc, argv);
181 if (error && !(sigint_received || sigpipe_received)) {
182 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
189 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
198 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
199 "Available commands:\n", getprogname());
200 for (i = 0; i < nitems(got_commands); i++) {
201 struct cmd *cmd = &got_commands[i];
202 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
207 static const struct got_error *
208 get_editor(char **abspath)
210 const struct got_error *err = NULL;
213 editor = getenv("VISUAL");
215 editor = getenv("EDITOR");
218 err = got_path_find_prog(abspath, editor);
223 if (*abspath == NULL) {
224 *abspath = strdup("/bin/ed");
225 if (*abspath == NULL)
226 return got_error_from_errno("strdup");
232 static const struct got_error *
233 apply_unveil(const char *repo_path, int repo_read_only,
234 const char *worktree_path, int create_worktree)
236 const struct got_error *err;
238 if (create_worktree) {
239 /* Pre-create work tree path to avoid unveiling its parents. */
240 err = got_path_mkdir(worktree_path);
242 if (errno == EEXIST) {
243 if (got_path_dir_is_empty(worktree_path)) {
247 err = got_error_path(worktree_path,
248 GOT_ERR_DIR_NOT_EMPTY);
252 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
256 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
257 return got_error_from_errno2("unveil", repo_path);
259 if (worktree_path && unveil(worktree_path, "rwc") != 0)
260 return got_error_from_errno2("unveil", worktree_path);
262 if (unveil("/tmp", "rwc") != 0)
263 return got_error_from_errno2("unveil", "/tmp");
265 err = got_privsep_unveil_exec_helpers();
269 if (unveil(NULL, NULL) != 0)
270 return got_error_from_errno("unveil");
278 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
279 "[-p prefix] repository-path [worktree-path]\n", getprogname());
284 checkout_progress(void *arg, unsigned char status, const char *path)
286 char *worktree_path = arg;
288 while (path[0] == '/')
291 printf("%c %s/%s\n", status, worktree_path, path);
294 static const struct got_error *
295 check_cancelled(void *arg)
297 if (sigint_received || sigpipe_received)
298 return got_error(GOT_ERR_CANCELLED);
302 static const struct got_error *
303 check_linear_ancestry(struct got_object_id *commit_id,
304 struct got_object_id *base_commit_id, struct got_repository *repo)
306 const struct got_error *err = NULL;
307 struct got_object_id *yca_id;
309 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
310 commit_id, base_commit_id, repo);
315 return got_error(GOT_ERR_ANCESTRY);
318 * Require a straight line of history between the target commit
319 * and the work tree's base commit.
321 * Non-linear situations such as this require a rebase:
323 * (commit) D F (base_commit)
331 * 'got update' only handles linear cases:
332 * Update forwards in time: A (base/yca) - B - C - D (commit)
333 * Update backwards in time: D (base) - C - B - A (commit/yca)
335 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
336 got_object_id_cmp(base_commit_id, yca_id) != 0)
337 return got_error(GOT_ERR_ANCESTRY);
343 static const struct got_error *
344 check_same_branch(struct got_object_id *commit_id,
345 struct got_reference *head_ref, struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_commit_graph *graph = NULL;
349 struct got_object_id *head_commit_id = NULL;
350 int is_same_branch = 0;
352 err = got_ref_resolve(&head_commit_id, repo, head_ref);
356 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
360 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
365 struct got_object_id *id;
366 err = got_commit_graph_iter_next(&id, graph);
368 if (err->code == GOT_ERR_ITER_COMPLETED) {
372 else if (err->code != GOT_ERR_ITER_NEED_MORE)
374 err = got_commit_graph_fetch_commits(graph, 1,
381 if (got_object_id_cmp(id, commit_id) == 0) {
389 got_commit_graph_close(graph);
390 free(head_commit_id);
391 if (!err && !is_same_branch)
392 err = got_error(GOT_ERR_ANCESTRY);
396 static const struct got_error *
397 cmd_checkout(int argc, char *argv[])
399 const struct got_error *error = NULL;
400 struct got_repository *repo = NULL;
401 struct got_reference *head_ref = NULL;
402 struct got_worktree *worktree = NULL;
403 char *repo_path = NULL;
404 char *worktree_path = NULL;
405 const char *path_prefix = "";
406 const char *branch_name = GOT_REF_HEAD;
407 char *commit_id_str = NULL;
408 int ch, same_path_prefix;
410 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
413 branch_name = optarg;
416 commit_id_str = strdup(optarg);
417 if (commit_id_str == NULL)
418 return got_error_from_errno("strdup");
421 path_prefix = optarg;
433 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
434 "unveil", NULL) == -1)
438 char *cwd, *base, *dotgit;
439 repo_path = realpath(argv[0], NULL);
440 if (repo_path == NULL)
441 return got_error_from_errno2("realpath", argv[0]);
442 cwd = getcwd(NULL, 0);
444 error = got_error_from_errno("getcwd");
447 if (path_prefix[0]) {
448 base = basename(path_prefix);
450 error = got_error_from_errno2("basename",
455 base = basename(repo_path);
457 error = got_error_from_errno2("basename",
462 dotgit = strstr(base, ".git");
465 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
466 error = got_error_from_errno("asprintf");
471 } else if (argc == 2) {
472 repo_path = realpath(argv[0], NULL);
473 if (repo_path == NULL) {
474 error = got_error_from_errno2("realpath", argv[0]);
477 worktree_path = realpath(argv[1], NULL);
478 if (worktree_path == NULL) {
479 error = got_error_from_errno2("realpath", argv[1]);
485 got_path_strip_trailing_slashes(repo_path);
486 got_path_strip_trailing_slashes(worktree_path);
488 error = got_repo_open(&repo, repo_path);
492 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
496 error = got_ref_open(&head_ref, repo, branch_name, 0);
500 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
501 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
504 error = got_worktree_open(&worktree, worktree_path);
508 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
512 if (!same_path_prefix) {
513 error = got_error(GOT_ERR_PATH_PREFIX);
518 struct got_object_id *commit_id;
519 error = got_object_resolve_id_str(&commit_id, repo,
523 error = check_linear_ancestry(commit_id,
524 got_worktree_get_base_commit_id(worktree), repo);
529 error = check_same_branch(commit_id, head_ref, repo);
532 error = got_worktree_set_base_commit_id(worktree, repo,
539 error = got_worktree_checkout_files(worktree, "", repo,
540 checkout_progress, worktree_path, check_cancelled, NULL);
544 printf("Now shut up and hack\n");
556 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
562 update_progress(void *arg, unsigned char status, const char *path)
564 int *did_something = arg;
566 if (status == GOT_STATUS_EXISTS)
570 while (path[0] == '/')
572 printf("%c %s\n", status, path);
575 static const struct got_error *
576 switch_head_ref(struct got_reference *head_ref,
577 struct got_object_id *commit_id, struct got_worktree *worktree,
578 struct got_repository *repo)
580 const struct got_error *err = NULL;
582 int ref_has_moved = 0;
584 /* Trivial case: switching between two different references. */
585 if (strcmp(got_ref_get_name(head_ref),
586 got_worktree_get_head_ref_name(worktree)) != 0) {
587 printf("Switching work tree from %s to %s\n",
588 got_worktree_get_head_ref_name(worktree),
589 got_ref_get_name(head_ref));
590 return got_worktree_set_head_ref(worktree, head_ref);
593 err = check_linear_ancestry(commit_id,
594 got_worktree_get_base_commit_id(worktree), repo);
596 if (err->code != GOT_ERR_ANCESTRY)
603 /* Switching to a rebased branch with the same reference name. */
604 err = got_object_id_str(&base_id_str,
605 got_worktree_get_base_commit_id(worktree));
608 printf("Reference %s now points at a different branch\n",
609 got_worktree_get_head_ref_name(worktree));
610 printf("Switching work tree from %s to %s\n", base_id_str,
611 got_worktree_get_head_ref_name(worktree));
615 static const struct got_error *
616 cmd_update(int argc, char *argv[])
618 const struct got_error *error = NULL;
619 struct got_repository *repo = NULL;
620 struct got_worktree *worktree = NULL;
621 char *worktree_path = NULL, *path = NULL;
622 struct got_object_id *commit_id = NULL;
623 char *commit_id_str = NULL;
624 const char *branch_name = NULL;
625 struct got_reference *head_ref = NULL;
626 int ch, did_something = 0;
628 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
631 branch_name = optarg;
634 commit_id_str = strdup(optarg);
635 if (commit_id_str == NULL)
636 return got_error_from_errno("strdup");
648 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
649 "unveil", NULL) == -1)
652 worktree_path = getcwd(NULL, 0);
653 if (worktree_path == NULL) {
654 error = got_error_from_errno("getcwd");
657 error = got_worktree_open(&worktree, worktree_path);
664 error = got_error_from_errno("strdup");
667 } else if (argc == 1) {
668 error = got_worktree_resolve_path(&path, worktree, argv[0]);
674 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
678 error = apply_unveil(got_repo_get_path(repo), 0,
679 got_worktree_get_root_path(worktree), 0);
683 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
684 got_worktree_get_head_ref_name(worktree), 0);
687 if (commit_id_str == NULL) {
688 error = got_ref_resolve(&commit_id, repo, head_ref);
691 error = got_object_id_str(&commit_id_str, commit_id);
695 error = got_object_resolve_id_str(&commit_id, repo,
702 struct got_object_id *head_commit_id;
703 if (strlen(path) != 0) {
704 fprintf(stderr, "%s: switching between branches "
705 "requires that the entire work tree "
706 "gets updated, not just '%s'\n",
707 getprogname(), path);
708 error = got_error(GOT_ERR_BAD_PATH);
711 error = got_ref_resolve(&head_commit_id, repo, head_ref);
714 error = check_linear_ancestry(commit_id, head_commit_id, repo);
715 free(head_commit_id);
718 error = check_same_branch(commit_id, head_ref, repo);
721 error = switch_head_ref(head_ref, commit_id, worktree, repo);
725 error = check_linear_ancestry(commit_id,
726 got_worktree_get_base_commit_id(worktree), repo);
728 if (error->code == GOT_ERR_ANCESTRY)
729 error = got_error(GOT_ERR_BRANCH_MOVED);
732 error = check_same_branch(commit_id, head_ref, repo);
737 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
739 error = got_worktree_set_base_commit_id(worktree, repo,
745 error = got_worktree_checkout_files(worktree, path, repo,
746 update_progress, &did_something, check_cancelled, NULL);
751 printf("Updated to commit %s\n", commit_id_str);
753 printf("Already up-to-date\n");
762 static const struct got_error *
763 print_patch(struct got_commit_object *commit, struct got_object_id *id,
764 int diff_context, struct got_repository *repo)
766 const struct got_error *err = NULL;
767 struct got_tree_object *tree1 = NULL, *tree2;
768 struct got_object_qid *qid;
769 char *id_str1 = NULL, *id_str2;
770 struct got_diff_blob_output_unidiff_arg arg;
772 err = got_object_open_as_tree(&tree2, repo,
773 got_object_commit_get_tree_id(commit));
777 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
779 struct got_commit_object *pcommit;
781 err = got_object_open_as_commit(&pcommit, repo, qid->id);
785 err = got_object_open_as_tree(&tree1, repo,
786 got_object_commit_get_tree_id(pcommit));
787 got_object_commit_close(pcommit);
791 err = got_object_id_str(&id_str1, qid->id);
796 err = got_object_id_str(&id_str2, id);
800 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
801 arg.diff_context = diff_context;
802 arg.outfile = stdout;
803 err = got_diff_tree(tree1, tree2, "", "", repo,
804 got_diff_blob_output_unidiff, &arg);
807 got_object_tree_close(tree1);
808 got_object_tree_close(tree2);
815 get_datestr(time_t *time, char *datebuf)
817 char *p, *s = ctime_r(time, datebuf);
824 static const struct got_error *
825 print_commit(struct got_commit_object *commit, struct got_object_id *id,
826 struct got_repository *repo, int show_patch, int diff_context,
827 struct got_reflist_head *refs)
829 const struct got_error *err = NULL;
830 char *id_str, *datestr, *logmsg0, *logmsg, *line;
832 time_t committer_time;
833 const char *author, *committer;
834 char *refs_str = NULL;
835 struct got_reflist_entry *re;
837 SIMPLEQ_FOREACH(re, refs, entry) {
840 if (got_object_id_cmp(re->id, id) != 0)
842 name = got_ref_get_name(re->ref);
843 if (strcmp(name, GOT_REF_HEAD) == 0)
845 if (strncmp(name, "refs/", 5) == 0)
847 if (strncmp(name, "got/", 4) == 0)
849 if (strncmp(name, "heads/", 6) == 0)
851 if (strncmp(name, "remotes/", 8) == 0)
854 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
856 err = got_error_from_errno("asprintf");
862 err = got_object_id_str(&id_str, id);
866 printf("-----------------------------------------------\n");
867 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
868 refs_str ? refs_str : "", refs_str ? ")" : "");
873 printf("from: %s\n", got_object_commit_get_author(commit));
874 committer_time = got_object_commit_get_committer_time(commit);
875 datestr = get_datestr(&committer_time, datebuf);
876 printf("date: %s UTC\n", datestr);
877 author = got_object_commit_get_author(commit);
878 committer = got_object_commit_get_committer(commit);
879 if (strcmp(author, committer) != 0)
880 printf("via: %s\n", committer);
881 if (got_object_commit_get_nparents(commit) > 1) {
882 const struct got_object_id_queue *parent_ids;
883 struct got_object_qid *qid;
885 parent_ids = got_object_commit_get_parent_ids(commit);
886 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
887 err = got_object_id_str(&id_str, qid->id);
890 printf("parent %d: %s\n", n++, id_str);
895 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
897 return got_error_from_errno("strdup");
901 line = strsep(&logmsg, "\n");
903 printf(" %s\n", line);
908 err = print_patch(commit, id, diff_context, repo);
913 if (fflush(stdout) != 0 && err == NULL)
914 err = got_error_from_errno("fflush");
918 static const struct got_error *
919 print_commits(struct got_object_id *root_id, struct got_repository *repo,
920 char *path, int show_patch, int diff_context, int limit,
921 int first_parent_traversal, struct got_reflist_head *refs)
923 const struct got_error *err;
924 struct got_commit_graph *graph;
926 err = got_commit_graph_open(&graph, root_id, path,
927 first_parent_traversal, repo);
930 err = got_commit_graph_iter_start(graph, root_id, repo);
934 struct got_commit_object *commit;
935 struct got_object_id *id;
937 if (sigint_received || sigpipe_received)
940 err = got_commit_graph_iter_next(&id, graph);
942 if (err->code == GOT_ERR_ITER_COMPLETED) {
946 if (err->code != GOT_ERR_ITER_NEED_MORE)
948 err = got_commit_graph_fetch_commits(graph, 1, repo);
957 err = got_object_open_as_commit(&commit, repo, id);
960 err = print_commit(commit, id, repo, show_patch, diff_context,
962 got_object_commit_close(commit);
963 if (err || (limit && --limit == 0))
967 got_commit_graph_close(graph);
974 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
975 "[-r repository-path] [path]\n", getprogname());
979 static const struct got_error *
980 cmd_log(int argc, char *argv[])
982 const struct got_error *error;
983 struct got_repository *repo = NULL;
984 struct got_worktree *worktree = NULL;
985 struct got_commit_object *commit = NULL;
986 struct got_object_id *id = NULL;
987 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
988 char *start_commit = NULL;
989 int diff_context = 3, ch;
990 int show_patch = 0, limit = 0, first_parent_traversal = 0;
992 struct got_reflist_head refs;
997 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1003 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1006 first_parent_traversal = 1;
1012 start_commit = optarg;
1015 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1018 err(1, "-C option %s", errstr);
1021 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1023 err(1, "-l option %s", errstr);
1026 repo_path = realpath(optarg, NULL);
1027 if (repo_path == NULL)
1028 err(1, "-r option");
1029 got_path_strip_trailing_slashes(repo_path);
1040 cwd = getcwd(NULL, 0);
1042 error = got_error_from_errno("getcwd");
1046 error = got_worktree_open(&worktree, cwd);
1047 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1054 error = got_error_from_errno("strdup");
1057 } else if (argc == 1) {
1059 error = got_worktree_resolve_path(&path, worktree,
1064 path = strdup(argv[0]);
1066 error = got_error_from_errno("strdup");
1073 if (repo_path == NULL) {
1074 repo_path = worktree ?
1075 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1077 if (repo_path == NULL) {
1078 error = got_error_from_errno("strdup");
1082 error = got_repo_open(&repo, repo_path);
1086 error = apply_unveil(got_repo_get_path(repo), 1,
1087 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1091 if (start_commit == NULL) {
1092 struct got_reference *head_ref;
1093 error = got_ref_open(&head_ref, repo,
1094 worktree ? got_worktree_get_head_ref_name(worktree)
1098 error = got_ref_resolve(&id, repo, head_ref);
1099 got_ref_close(head_ref);
1102 error = got_object_open_as_commit(&commit, repo, id);
1104 struct got_reference *ref;
1105 error = got_ref_open(&ref, repo, start_commit, 0);
1106 if (error == NULL) {
1108 error = got_ref_resolve(&id, repo, ref);
1112 error = got_object_get_type(&obj_type, repo, id);
1115 if (obj_type == GOT_OBJ_TYPE_TAG) {
1116 struct got_tag_object *tag;
1117 error = got_object_open_as_tag(&tag, repo, id);
1120 if (got_object_tag_get_object_type(tag) !=
1121 GOT_OBJ_TYPE_COMMIT) {
1122 got_object_tag_close(tag);
1123 error = got_error(GOT_ERR_OBJ_TYPE);
1127 id = got_object_id_dup(
1128 got_object_tag_get_object_id(tag));
1130 error = got_error_from_errno(
1131 "got_object_id_dup");
1132 got_object_tag_close(tag);
1135 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1136 error = got_error(GOT_ERR_OBJ_TYPE);
1139 error = got_object_open_as_commit(&commit, repo, id);
1143 if (commit == NULL) {
1144 error = got_object_resolve_id_str(&id, repo,
1153 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1158 path = in_repo_path;
1161 error = got_ref_list(&refs, repo);
1165 error = print_commits(id, repo, path, show_patch,
1166 diff_context, limit, first_parent_traversal, &refs);
1173 got_worktree_close(worktree);
1175 const struct got_error *repo_error;
1176 repo_error = got_repo_close(repo);
1180 got_ref_list_free(&refs);
1187 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1188 "[object1 object2 | path]\n", getprogname());
1192 struct print_diff_arg {
1193 struct got_repository *repo;
1194 struct got_worktree *worktree;
1200 static const struct got_error *
1201 print_diff(void *arg, unsigned char status, const char *path,
1202 struct got_object_id *blob_id, struct got_object_id *commit_id)
1204 struct print_diff_arg *a = arg;
1205 const struct got_error *err = NULL;
1206 struct got_blob_object *blob1 = NULL;
1208 char *abspath = NULL;
1211 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1212 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1215 if (!a->header_shown) {
1216 printf("diff %s %s\n", a->id_str,
1217 got_worktree_get_root_path(a->worktree));
1218 a->header_shown = 1;
1221 if (status != GOT_STATUS_ADD) {
1222 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1228 if (status != GOT_STATUS_DELETE) {
1229 if (asprintf(&abspath, "%s/%s",
1230 got_worktree_get_root_path(a->worktree), path) == -1) {
1231 err = got_error_from_errno("asprintf");
1235 f2 = fopen(abspath, "r");
1237 err = got_error_from_errno2("fopen", abspath);
1240 if (lstat(abspath, &sb) == -1) {
1241 err = got_error_from_errno2("lstat", abspath);
1247 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1251 got_object_blob_close(blob1);
1252 if (f2 && fclose(f2) != 0 && err == NULL)
1253 err = got_error_from_errno("fclose");
1258 static const struct got_error *
1259 cmd_diff(int argc, char *argv[])
1261 const struct got_error *error;
1262 struct got_repository *repo = NULL;
1263 struct got_worktree *worktree = NULL;
1264 char *cwd = NULL, *repo_path = NULL;
1265 struct got_object_id *id1 = NULL, *id2 = NULL;
1266 const char *id_str1 = NULL, *id_str2 = NULL;
1267 char *label1 = NULL, *label2 = NULL;
1269 int diff_context = 3, ch;
1274 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1279 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1282 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1284 err(1, "-C option %s", errstr);
1287 repo_path = realpath(optarg, NULL);
1288 if (repo_path == NULL)
1289 err(1, "-r option");
1290 got_path_strip_trailing_slashes(repo_path);
1301 cwd = getcwd(NULL, 0);
1303 error = got_error_from_errno("getcwd");
1306 error = got_worktree_open(&worktree, cwd);
1307 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1310 if (worktree == NULL) {
1311 error = got_error(GOT_ERR_NOT_WORKTREE);
1316 "-r option can't be used when diffing a work tree");
1317 repo_path = strdup(got_worktree_get_repo_path(worktree));
1318 if (repo_path == NULL) {
1319 error = got_error_from_errno("strdup");
1323 error = got_worktree_resolve_path(&path, worktree,
1330 error = got_error_from_errno("strdup");
1334 } else if (argc == 2) {
1337 if (worktree && repo_path == NULL) {
1339 strdup(got_worktree_get_repo_path(worktree));
1340 if (repo_path == NULL) {
1341 error = got_error_from_errno("strdup");
1348 if (repo_path == NULL) {
1349 repo_path = getcwd(NULL, 0);
1350 if (repo_path == NULL)
1351 return got_error_from_errno("getcwd");
1354 error = got_repo_open(&repo, repo_path);
1359 error = apply_unveil(got_repo_get_path(repo), 1,
1360 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1365 struct print_diff_arg arg;
1367 error = got_object_id_str(&id_str,
1368 got_worktree_get_base_commit_id(worktree));
1372 arg.worktree = worktree;
1373 arg.diff_context = diff_context;
1374 arg.id_str = id_str;
1375 arg.header_shown = 0;
1377 error = got_worktree_status(worktree, path, repo, print_diff,
1378 &arg, check_cancelled, NULL);
1383 error = got_object_resolve_id_str(&id1, repo, id_str1);
1385 struct got_reference *ref;
1386 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1388 error = got_ref_open(&ref, repo, id_str1, 0);
1391 label1 = strdup(got_ref_get_name(ref));
1392 if (label1 == NULL) {
1393 error = got_error_from_errno("strdup");
1396 error = got_ref_resolve(&id1, repo, ref);
1401 label1 = strdup(id_str1);
1402 if (label1 == NULL) {
1403 error = got_error_from_errno("strdup");
1408 error = got_object_resolve_id_str(&id2, repo, id_str2);
1410 struct got_reference *ref;
1411 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1413 error = got_ref_open(&ref, repo, id_str2, 0);
1416 label2 = strdup(got_ref_get_name(ref));
1417 if (label2 == NULL) {
1418 error = got_error_from_errno("strdup");
1421 error = got_ref_resolve(&id2, repo, ref);
1426 label2 = strdup(id_str2);
1427 if (label2 == NULL) {
1428 error = got_error_from_errno("strdup");
1433 error = got_object_get_type(&type1, repo, id1);
1437 error = got_object_get_type(&type2, repo, id2);
1441 if (type1 != type2) {
1442 error = got_error(GOT_ERR_OBJ_TYPE);
1447 case GOT_OBJ_TYPE_BLOB:
1448 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1449 diff_context, repo, stdout);
1451 case GOT_OBJ_TYPE_TREE:
1452 error = got_diff_objects_as_trees(id1, id2, "", "",
1453 diff_context, repo, stdout);
1455 case GOT_OBJ_TYPE_COMMIT:
1456 printf("diff %s %s\n", label1, label2);
1457 error = got_diff_objects_as_commits(id1, id2, diff_context,
1461 error = got_error(GOT_ERR_OBJ_TYPE);
1471 got_worktree_close(worktree);
1473 const struct got_error *repo_error;
1474 repo_error = got_repo_close(repo);
1485 "usage: %s blame [-c commit] [-r repository-path] path\n",
1490 static const struct got_error *
1491 cmd_blame(int argc, char *argv[])
1493 const struct got_error *error;
1494 struct got_repository *repo = NULL;
1495 struct got_worktree *worktree = NULL;
1496 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1497 struct got_object_id *commit_id = NULL;
1498 char *commit_id_str = NULL;
1502 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1507 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1510 commit_id_str = optarg;
1513 repo_path = realpath(optarg, NULL);
1514 if (repo_path == NULL)
1515 err(1, "-r option");
1516 got_path_strip_trailing_slashes(repo_path);
1532 cwd = getcwd(NULL, 0);
1534 error = got_error_from_errno("getcwd");
1537 if (repo_path == NULL) {
1538 error = got_worktree_open(&worktree, cwd);
1539 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1545 strdup(got_worktree_get_repo_path(worktree));
1546 if (repo_path == NULL)
1547 error = got_error_from_errno("strdup");
1551 repo_path = strdup(cwd);
1552 if (repo_path == NULL) {
1553 error = got_error_from_errno("strdup");
1559 error = got_repo_open(&repo, repo_path);
1563 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1568 const char *prefix = got_worktree_get_path_prefix(worktree);
1569 char *p, *worktree_subdir = cwd +
1570 strlen(got_worktree_get_root_path(worktree));
1571 if (asprintf(&p, "%s%s%s%s%s",
1572 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1573 worktree_subdir, worktree_subdir[0] ? "/" : "",
1575 error = got_error_from_errno("asprintf");
1578 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1581 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1586 if (commit_id_str == NULL) {
1587 struct got_reference *head_ref;
1588 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1591 error = got_ref_resolve(&commit_id, repo, head_ref);
1592 got_ref_close(head_ref);
1596 error = got_object_resolve_id_str(&commit_id, repo,
1602 error = got_blame(in_repo_path, commit_id, repo, stdout);
1609 got_worktree_close(worktree);
1611 const struct got_error *repo_error;
1612 repo_error = got_repo_close(repo);
1623 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1629 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1630 const char *root_path)
1632 int is_root_path = (strcmp(path, root_path) == 0);
1634 path += strlen(root_path);
1635 while (path[0] == '/')
1638 printf("%s%s%s%s%s\n", id ? id : "", path,
1639 is_root_path ? "" : "/", te->name,
1640 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1643 static const struct got_error *
1644 print_tree(const char *path, struct got_object_id *commit_id,
1645 int show_ids, int recurse, const char *root_path,
1646 struct got_repository *repo)
1648 const struct got_error *err = NULL;
1649 struct got_object_id *tree_id = NULL;
1650 struct got_tree_object *tree = NULL;
1651 const struct got_tree_entries *entries;
1652 struct got_tree_entry *te;
1654 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1658 err = got_object_open_as_tree(&tree, repo, tree_id);
1661 entries = got_object_tree_get_entries(tree);
1662 te = SIMPLEQ_FIRST(&entries->head);
1666 if (sigint_received || sigpipe_received)
1671 err = got_object_id_str(&id_str, te->id);
1674 if (asprintf(&id, "%s ", id_str) == -1) {
1675 err = got_error_from_errno("asprintf");
1681 print_entry(te, id, path, root_path);
1684 if (recurse && S_ISDIR(te->mode)) {
1686 if (asprintf(&child_path, "%s%s%s", path,
1687 path[0] == '/' && path[1] == '\0' ? "" : "/",
1689 err = got_error_from_errno("asprintf");
1692 err = print_tree(child_path, commit_id, show_ids, 1,
1699 te = SIMPLEQ_NEXT(te, entry);
1703 got_object_tree_close(tree);
1708 static const struct got_error *
1709 cmd_tree(int argc, char *argv[])
1711 const struct got_error *error;
1712 struct got_repository *repo = NULL;
1713 struct got_worktree *worktree = NULL;
1715 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1716 struct got_object_id *commit_id = NULL;
1717 char *commit_id_str = NULL;
1718 int show_ids = 0, recurse = 0;
1722 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1727 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1730 commit_id_str = optarg;
1733 repo_path = realpath(optarg, NULL);
1734 if (repo_path == NULL)
1735 err(1, "-r option");
1736 got_path_strip_trailing_slashes(repo_path);
1760 cwd = getcwd(NULL, 0);
1762 error = got_error_from_errno("getcwd");
1765 if (repo_path == NULL) {
1766 error = got_worktree_open(&worktree, cwd);
1767 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1773 strdup(got_worktree_get_repo_path(worktree));
1774 if (repo_path == NULL)
1775 error = got_error_from_errno("strdup");
1779 repo_path = strdup(cwd);
1780 if (repo_path == NULL) {
1781 error = got_error_from_errno("strdup");
1787 error = got_repo_open(&repo, repo_path);
1791 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1797 char *p, *worktree_subdir = cwd +
1798 strlen(got_worktree_get_root_path(worktree));
1799 if (asprintf(&p, "%s/%s",
1800 got_worktree_get_path_prefix(worktree),
1801 worktree_subdir) == -1) {
1802 error = got_error_from_errno("asprintf");
1805 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1812 if (in_repo_path == NULL) {
1813 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1818 if (commit_id_str == NULL) {
1819 struct got_reference *head_ref;
1820 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1823 error = got_ref_resolve(&commit_id, repo, head_ref);
1824 got_ref_close(head_ref);
1828 error = got_object_resolve_id_str(&commit_id, repo,
1834 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1835 in_repo_path, repo);
1842 got_worktree_close(worktree);
1844 const struct got_error *repo_error;
1845 repo_error = got_repo_close(repo);
1855 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1859 static const struct got_error *
1860 print_status(void *arg, unsigned char status, const char *path,
1861 struct got_object_id *blob_id, struct got_object_id *commit_id)
1863 printf("%c %s\n", status, path);
1867 static const struct got_error *
1868 cmd_status(int argc, char *argv[])
1870 const struct got_error *error = NULL;
1871 struct got_repository *repo = NULL;
1872 struct got_worktree *worktree = NULL;
1873 char *cwd = NULL, *path = NULL;
1876 while ((ch = getopt(argc, argv, "")) != -1) {
1888 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1892 cwd = getcwd(NULL, 0);
1894 error = got_error_from_errno("getcwd");
1898 error = got_worktree_open(&worktree, cwd);
1905 error = got_error_from_errno("strdup");
1908 } else if (argc == 1) {
1909 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1915 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1919 error = apply_unveil(got_repo_get_path(repo), 1,
1920 got_worktree_get_root_path(worktree), 0);
1924 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1925 check_cancelled, NULL);
1936 "usage: %s ref [-r repository] -l | -d name | name target\n",
1941 static const struct got_error *
1942 list_refs(struct got_repository *repo)
1944 static const struct got_error *err = NULL;
1945 struct got_reflist_head refs;
1946 struct got_reflist_entry *re;
1948 SIMPLEQ_INIT(&refs);
1949 err = got_ref_list(&refs, repo);
1953 SIMPLEQ_FOREACH(re, &refs, entry) {
1955 refstr = got_ref_to_str(re->ref);
1957 return got_error_from_errno("got_ref_to_str");
1958 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1962 got_ref_list_free(&refs);
1966 static const struct got_error *
1967 delete_ref(struct got_repository *repo, const char *refname)
1969 const struct got_error *err = NULL;
1970 struct got_reference *ref;
1972 err = got_ref_open(&ref, repo, refname, 0);
1976 err = got_ref_delete(ref, repo);
1981 static const struct got_error *
1982 add_ref(struct got_repository *repo, const char *refname, const char *target)
1984 const struct got_error *err = NULL;
1985 struct got_object_id *id;
1986 struct got_reference *ref = NULL;
1988 err = got_object_resolve_id_str(&id, repo, target);
1990 struct got_reference *target_ref;
1992 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1994 err = got_ref_open(&target_ref, repo, target, 0);
1997 err = got_ref_resolve(&id, repo, target_ref);
1998 got_ref_close(target_ref);
2003 err = got_ref_alloc(&ref, refname, id);
2007 err = got_ref_write(ref, repo);
2015 static const struct got_error *
2016 cmd_ref(int argc, char *argv[])
2018 const struct got_error *error = NULL;
2019 struct got_repository *repo = NULL;
2020 struct got_worktree *worktree = NULL;
2021 char *cwd = NULL, *repo_path = NULL;
2022 int ch, do_list = 0;
2023 const char *delref = NULL;
2025 /* TODO: Add -s option for adding symbolic references. */
2026 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2032 repo_path = realpath(optarg, NULL);
2033 if (repo_path == NULL)
2034 err(1, "-r option");
2035 got_path_strip_trailing_slashes(repo_path);
2046 if (do_list && delref)
2047 errx(1, "-l and -d options are mutually exclusive\n");
2052 if (do_list || delref) {
2055 } else if (argc != 2)
2060 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2064 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2065 "sendfd unveil", NULL) == -1)
2069 cwd = getcwd(NULL, 0);
2071 error = got_error_from_errno("getcwd");
2075 if (repo_path == NULL) {
2076 error = got_worktree_open(&worktree, cwd);
2077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2083 strdup(got_worktree_get_repo_path(worktree));
2084 if (repo_path == NULL)
2085 error = got_error_from_errno("strdup");
2089 repo_path = strdup(cwd);
2090 if (repo_path == NULL) {
2091 error = got_error_from_errno("strdup");
2097 error = got_repo_open(&repo, repo_path);
2101 error = apply_unveil(got_repo_get_path(repo), do_list,
2102 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2107 error = list_refs(repo);
2109 error = delete_ref(repo, delref);
2111 error = add_ref(repo, argv[0], argv[1]);
2114 got_repo_close(repo);
2116 got_worktree_close(worktree);
2125 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2129 static const struct got_error *
2130 cmd_add(int argc, char *argv[])
2132 const struct got_error *error = NULL;
2133 struct got_repository *repo = NULL;
2134 struct got_worktree *worktree = NULL;
2136 struct got_pathlist_head paths;
2137 struct got_pathlist_entry *pe;
2142 while ((ch = getopt(argc, argv, "")) != -1) {
2156 /* make sure each file exists before doing anything halfway */
2157 for (x = 0; x < argc; x++) {
2158 char *path = realpath(argv[x], NULL);
2160 error = got_error_from_errno2("realpath", argv[x]);
2166 cwd = getcwd(NULL, 0);
2168 error = got_error_from_errno("getcwd");
2172 error = got_worktree_open(&worktree, cwd);
2176 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2180 error = apply_unveil(got_repo_get_path(repo), 1,
2181 got_worktree_get_root_path(worktree), 0);
2185 for (x = 0; x < argc; x++) {
2186 char *path = realpath(argv[x], NULL);
2188 error = got_error_from_errno2("realpath", argv[x]);
2192 got_path_strip_trailing_slashes(path);
2193 error = got_pathlist_insert(&pe, &paths, path, NULL);
2199 error = got_worktree_schedule_add(worktree, &paths, print_status,
2203 got_repo_close(repo);
2205 got_worktree_close(worktree);
2206 TAILQ_FOREACH(pe, &paths, entry)
2207 free((char *)pe->path);
2208 got_pathlist_free(&paths);
2216 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2220 static const struct got_error *
2221 cmd_rm(int argc, char *argv[])
2223 const struct got_error *error = NULL;
2224 struct got_worktree *worktree = NULL;
2225 struct got_repository *repo = NULL;
2227 struct got_pathlist_head paths;
2228 struct got_pathlist_entry *pe;
2229 int ch, i, delete_local_mods = 0;
2233 while ((ch = getopt(argc, argv, "f")) != -1) {
2236 delete_local_mods = 1;
2250 /* make sure each file exists before doing anything halfway */
2251 for (i = 0; i < argc; i++) {
2252 char *path = realpath(argv[i], NULL);
2254 error = got_error_from_errno2("realpath", argv[i]);
2260 cwd = getcwd(NULL, 0);
2262 error = got_error_from_errno("getcwd");
2265 error = got_worktree_open(&worktree, cwd);
2269 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2273 error = apply_unveil(got_repo_get_path(repo), 1,
2274 got_worktree_get_root_path(worktree), 0);
2278 for (i = 0; i < argc; i++) {
2279 char *path = realpath(argv[i], NULL);
2281 error = got_error_from_errno2("realpath", argv[i]);
2285 got_path_strip_trailing_slashes(path);
2286 error = got_pathlist_insert(&pe, &paths, path, NULL);
2292 error = got_worktree_schedule_delete(worktree, &paths,
2293 delete_local_mods, print_status, NULL, repo);
2298 got_repo_close(repo);
2300 got_worktree_close(worktree);
2301 TAILQ_FOREACH(pe, &paths, entry)
2302 free((char *)pe->path);
2303 got_pathlist_free(&paths);
2311 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2316 revert_progress(void *arg, unsigned char status, const char *path)
2318 while (path[0] == '/')
2320 printf("%c %s\n", status, path);
2323 static const struct got_error *
2324 cmd_revert(int argc, char *argv[])
2326 const struct got_error *error = NULL;
2327 struct got_worktree *worktree = NULL;
2328 struct got_repository *repo = NULL;
2329 char *cwd = NULL, *path = NULL;
2330 struct got_pathlist_head paths;
2331 struct got_pathlist_entry *pe;
2336 while ((ch = getopt(argc, argv, "")) != -1) {
2350 for (i = 0; i < argc; i++) {
2351 char *path = realpath(argv[i], NULL);
2353 error = got_error_from_errno2("realpath", argv[i]);
2357 got_path_strip_trailing_slashes(path);
2358 error = got_pathlist_insert(&pe, &paths, path, NULL);
2365 cwd = getcwd(NULL, 0);
2367 error = got_error_from_errno("getcwd");
2370 error = got_worktree_open(&worktree, cwd);
2374 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2378 error = apply_unveil(got_repo_get_path(repo), 1,
2379 got_worktree_get_root_path(worktree), 0);
2383 error = got_worktree_revert(worktree, &paths,
2384 revert_progress, NULL, repo);
2389 got_repo_close(repo);
2391 got_worktree_close(worktree);
2400 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2405 spawn_editor(const char *editor, const char *file)
2408 sig_t sighup, sigint, sigquit;
2411 sighup = signal(SIGHUP, SIG_IGN);
2412 sigint = signal(SIGINT, SIG_IGN);
2413 sigquit = signal(SIGQUIT, SIG_IGN);
2415 switch (pid = fork()) {
2419 execl(editor, editor, file, (char *)NULL);
2423 while (waitpid(pid, &st, 0) == -1)
2428 (void)signal(SIGHUP, sighup);
2429 (void)signal(SIGINT, sigint);
2430 (void)signal(SIGQUIT, sigquit);
2432 if (!WIFEXITED(st)) {
2437 return WEXITSTATUS(st);
2440 struct collect_commit_logmsg_arg {
2441 const char *cmdline_log;
2443 const char *worktree_path;
2444 const char *branch_name;
2445 const char *repo_path;
2450 static const struct got_error *
2451 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2454 char *initial_content = NULL;
2455 struct got_pathlist_entry *pe;
2456 const struct got_error *err = NULL;
2457 char *template = NULL;
2458 struct collect_commit_logmsg_arg *a = arg;
2460 struct stat st, st2;
2463 int fd, content_changed = 0;
2465 /* if a message was specified on the command line, just use it */
2466 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2467 len = strlen(a->cmdline_log) + 1;
2468 *logmsg = malloc(len + 1);
2469 if (*logmsg == NULL)
2470 return got_error_from_errno("malloc");
2471 strlcpy(*logmsg, a->cmdline_log, len);
2475 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2476 return got_error_from_errno("asprintf");
2478 if (asprintf(&initial_content,
2479 "\n# changes to be committed on branch %s:\n",
2480 a->branch_name) == -1)
2481 return got_error_from_errno("asprintf");
2483 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2487 dprintf(fd, initial_content);
2489 TAILQ_FOREACH(pe, commitable_paths, entry) {
2490 struct got_commitable *ct = pe->data;
2491 dprintf(fd, "# %c %s\n",
2492 got_commitable_get_status(ct),
2493 got_commitable_get_path(ct));
2497 if (stat(a->logmsg_path, &st) == -1) {
2498 err = got_error_from_errno2("stat", a->logmsg_path);
2502 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2503 err = got_error_from_errno("failed spawning editor");
2507 if (stat(a->logmsg_path, &st2) == -1) {
2508 err = got_error_from_errno("stat");
2512 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2513 unlink(a->logmsg_path);
2514 free(a->logmsg_path);
2515 a->logmsg_path = NULL;
2516 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2517 "no changes made to commit message, aborting");
2521 *logmsg = malloc(st2.st_size + 1);
2522 if (*logmsg == NULL) {
2523 err = got_error_from_errno("malloc");
2526 (*logmsg)[0] = '\0';
2529 fp = fopen(a->logmsg_path, "r");
2531 err = got_error_from_errno("fopen");
2534 while (fgets(buf, sizeof(buf), fp) != NULL) {
2535 if (!content_changed && strcmp(buf, initial_content) != 0)
2536 content_changed = 1;
2537 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2538 continue; /* remove comments and leading empty lines */
2539 len = strlcat(*logmsg, buf, st2.st_size);
2543 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2544 (*logmsg)[len - 1] = '\0';
2548 if (len == 0 || !content_changed) {
2549 unlink(a->logmsg_path);
2550 free(a->logmsg_path);
2551 a->logmsg_path = NULL;
2552 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2553 "commit message cannot be empty, aborting");
2557 free(initial_content);
2560 /* Editor is done; we can now apply unveil(2) */
2562 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2566 static const struct got_error *
2567 cmd_commit(int argc, char *argv[])
2569 const struct got_error *error = NULL;
2570 struct got_worktree *worktree = NULL;
2571 struct got_repository *repo = NULL;
2572 char *cwd = NULL, *path = NULL, *id_str = NULL;
2573 struct got_object_id *id = NULL;
2574 const char *logmsg = NULL;
2575 const char *got_author = getenv("GOT_AUTHOR");
2576 struct collect_commit_logmsg_arg cl_arg;
2577 char *editor = NULL;
2580 while ((ch = getopt(argc, argv, "m:")) != -1) {
2595 path = realpath(argv[0], NULL);
2597 error = got_error_from_errno2("realpath", argv[0]);
2600 got_path_strip_trailing_slashes(path);
2601 } else if (argc != 0)
2604 if (got_author == NULL) {
2605 /* TODO: Look current user up in password database */
2606 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2610 cwd = getcwd(NULL, 0);
2612 error = got_error_from_errno("getcwd");
2615 error = got_worktree_open(&worktree, cwd);
2619 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2624 * unveil(2) traverses exec(2); if an editor is used we have
2625 * to apply unveil after the log message has been written.
2627 if (logmsg == NULL || strlen(logmsg) == 0)
2628 error = get_editor(&editor);
2630 error = apply_unveil(got_repo_get_path(repo), 0,
2631 got_worktree_get_root_path(worktree), 0);
2635 cl_arg.editor = editor;
2636 cl_arg.cmdline_log = logmsg;
2637 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2638 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2639 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2640 cl_arg.branch_name += 5;
2641 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2642 cl_arg.branch_name += 6;
2643 cl_arg.repo_path = got_repo_get_path(repo);
2644 cl_arg.logmsg_path = NULL;
2645 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2646 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2648 if (cl_arg.logmsg_path)
2649 fprintf(stderr, "%s: log message preserved in %s\n",
2650 getprogname(), cl_arg.logmsg_path);
2654 if (cl_arg.logmsg_path)
2655 unlink(cl_arg.logmsg_path);
2657 error = got_object_id_str(&id_str, id);
2660 printf("Created commit %s\n", id_str);
2663 got_repo_close(repo);
2665 got_worktree_close(worktree);
2674 usage_cherrypick(void)
2676 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2680 static const struct got_error *
2681 cmd_cherrypick(int argc, char *argv[])
2683 const struct got_error *error = NULL;
2684 struct got_worktree *worktree = NULL;
2685 struct got_repository *repo = NULL;
2686 char *cwd = NULL, *commit_id_str = NULL;
2687 struct got_object_id *commit_id = NULL;
2688 struct got_commit_object *commit = NULL;
2689 struct got_object_qid *pid;
2690 struct got_reference *head_ref = NULL;
2691 int ch, did_something = 0;
2693 while ((ch = getopt(argc, argv, "")) != -1) {
2707 cwd = getcwd(NULL, 0);
2709 error = got_error_from_errno("getcwd");
2712 error = got_worktree_open(&worktree, cwd);
2716 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2720 error = apply_unveil(got_repo_get_path(repo), 0,
2721 got_worktree_get_root_path(worktree), 0);
2725 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2726 if (error != NULL) {
2727 struct got_reference *ref;
2728 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2730 error = got_ref_open(&ref, repo, argv[0], 0);
2733 error = got_ref_resolve(&commit_id, repo, ref);
2738 error = got_object_id_str(&commit_id_str, commit_id);
2742 error = got_ref_open(&head_ref, repo,
2743 got_worktree_get_head_ref_name(worktree), 0);
2747 error = check_same_branch(commit_id, head_ref, repo);
2749 if (error->code != GOT_ERR_ANCESTRY)
2753 error = got_error(GOT_ERR_SAME_BRANCH);
2757 error = got_object_open_as_commit(&commit, repo, commit_id);
2760 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2761 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2762 commit_id, repo, update_progress, &did_something, check_cancelled,
2768 printf("Merged commit %s\n", commit_id_str);
2771 got_object_commit_close(commit);
2772 free(commit_id_str);
2774 got_ref_close(head_ref);
2776 got_worktree_close(worktree);
2778 got_repo_close(repo);
2785 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2789 static const struct got_error *
2790 cmd_backout(int argc, char *argv[])
2792 const struct got_error *error = NULL;
2793 struct got_worktree *worktree = NULL;
2794 struct got_repository *repo = NULL;
2795 char *cwd = NULL, *commit_id_str = NULL;
2796 struct got_object_id *commit_id = NULL;
2797 struct got_commit_object *commit = NULL;
2798 struct got_object_qid *pid;
2799 struct got_reference *head_ref = NULL;
2800 int ch, did_something = 0;
2802 while ((ch = getopt(argc, argv, "")) != -1) {
2816 cwd = getcwd(NULL, 0);
2818 error = got_error_from_errno("getcwd");
2821 error = got_worktree_open(&worktree, cwd);
2825 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2829 error = apply_unveil(got_repo_get_path(repo), 0,
2830 got_worktree_get_root_path(worktree), 0);
2834 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2835 if (error != NULL) {
2836 struct got_reference *ref;
2837 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2839 error = got_ref_open(&ref, repo, argv[0], 0);
2842 error = got_ref_resolve(&commit_id, repo, ref);
2847 error = got_object_id_str(&commit_id_str, commit_id);
2851 error = got_ref_open(&head_ref, repo,
2852 got_worktree_get_head_ref_name(worktree), 0);
2856 error = check_same_branch(commit_id, head_ref, repo);
2860 error = got_object_open_as_commit(&commit, repo, commit_id);
2863 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2865 error = got_error(GOT_ERR_ROOT_COMMIT);
2869 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2870 update_progress, &did_something, check_cancelled, NULL);
2875 printf("Backed out commit %s\n", commit_id_str);
2878 got_object_commit_close(commit);
2879 free(commit_id_str);
2881 got_ref_close(head_ref);
2883 got_worktree_close(worktree);
2885 got_repo_close(repo);