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) {
1340 if (repo_path == NULL) {
1341 repo_path = getcwd(NULL, 0);
1342 if (repo_path == NULL)
1343 return got_error_from_errno("getcwd");
1346 error = got_repo_open(&repo, repo_path);
1351 error = apply_unveil(got_repo_get_path(repo), 1,
1352 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1357 struct print_diff_arg arg;
1359 error = got_object_id_str(&id_str,
1360 got_worktree_get_base_commit_id(worktree));
1364 arg.worktree = worktree;
1365 arg.diff_context = diff_context;
1366 arg.id_str = id_str;
1367 arg.header_shown = 0;
1369 error = got_worktree_status(worktree, path, repo, print_diff,
1370 &arg, check_cancelled, NULL);
1375 error = got_object_resolve_id_str(&id1, repo, id_str1);
1377 struct got_reference *ref;
1378 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1380 error = got_ref_open(&ref, repo, id_str1, 0);
1383 label1 = strdup(got_ref_get_name(ref));
1384 if (label1 == NULL) {
1385 error = got_error_from_errno("strdup");
1388 error = got_ref_resolve(&id1, repo, ref);
1393 label1 = strdup(id_str1);
1394 if (label1 == NULL) {
1395 error = got_error_from_errno("strdup");
1400 error = got_object_resolve_id_str(&id2, repo, id_str2);
1402 struct got_reference *ref;
1403 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1405 error = got_ref_open(&ref, repo, id_str2, 0);
1408 label2 = strdup(got_ref_get_name(ref));
1409 if (label2 == NULL) {
1410 error = got_error_from_errno("strdup");
1413 error = got_ref_resolve(&id2, repo, ref);
1418 label2 = strdup(id_str2);
1419 if (label2 == NULL) {
1420 error = got_error_from_errno("strdup");
1425 error = got_object_get_type(&type1, repo, id1);
1429 error = got_object_get_type(&type2, repo, id2);
1433 if (type1 != type2) {
1434 error = got_error(GOT_ERR_OBJ_TYPE);
1439 case GOT_OBJ_TYPE_BLOB:
1440 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1441 diff_context, repo, stdout);
1443 case GOT_OBJ_TYPE_TREE:
1444 error = got_diff_objects_as_trees(id1, id2, "", "",
1445 diff_context, repo, stdout);
1447 case GOT_OBJ_TYPE_COMMIT:
1448 printf("diff %s %s\n", label1, label2);
1449 error = got_diff_objects_as_commits(id1, id2, diff_context,
1453 error = got_error(GOT_ERR_OBJ_TYPE);
1463 got_worktree_close(worktree);
1465 const struct got_error *repo_error;
1466 repo_error = got_repo_close(repo);
1477 "usage: %s blame [-c commit] [-r repository-path] path\n",
1482 static const struct got_error *
1483 cmd_blame(int argc, char *argv[])
1485 const struct got_error *error;
1486 struct got_repository *repo = NULL;
1487 struct got_worktree *worktree = NULL;
1488 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1489 struct got_object_id *commit_id = NULL;
1490 char *commit_id_str = NULL;
1494 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1499 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1502 commit_id_str = optarg;
1505 repo_path = realpath(optarg, NULL);
1506 if (repo_path == NULL)
1507 err(1, "-r option");
1508 got_path_strip_trailing_slashes(repo_path);
1524 cwd = getcwd(NULL, 0);
1526 error = got_error_from_errno("getcwd");
1529 if (repo_path == NULL) {
1530 error = got_worktree_open(&worktree, cwd);
1531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1537 strdup(got_worktree_get_repo_path(worktree));
1538 if (repo_path == NULL)
1539 error = got_error_from_errno("strdup");
1543 repo_path = strdup(cwd);
1544 if (repo_path == NULL) {
1545 error = got_error_from_errno("strdup");
1551 error = got_repo_open(&repo, repo_path);
1555 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1560 const char *prefix = got_worktree_get_path_prefix(worktree);
1561 char *p, *worktree_subdir = cwd +
1562 strlen(got_worktree_get_root_path(worktree));
1563 if (asprintf(&p, "%s%s%s%s%s",
1564 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1565 worktree_subdir, worktree_subdir[0] ? "/" : "",
1567 error = got_error_from_errno("asprintf");
1570 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1573 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1578 if (commit_id_str == NULL) {
1579 struct got_reference *head_ref;
1580 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1583 error = got_ref_resolve(&commit_id, repo, head_ref);
1584 got_ref_close(head_ref);
1588 error = got_object_resolve_id_str(&commit_id, repo,
1594 error = got_blame(in_repo_path, commit_id, repo, stdout);
1601 got_worktree_close(worktree);
1603 const struct got_error *repo_error;
1604 repo_error = got_repo_close(repo);
1615 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1621 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1622 const char *root_path)
1624 int is_root_path = (strcmp(path, root_path) == 0);
1626 path += strlen(root_path);
1627 while (path[0] == '/')
1630 printf("%s%s%s%s%s\n", id ? id : "", path,
1631 is_root_path ? "" : "/", te->name,
1632 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1635 static const struct got_error *
1636 print_tree(const char *path, struct got_object_id *commit_id,
1637 int show_ids, int recurse, const char *root_path,
1638 struct got_repository *repo)
1640 const struct got_error *err = NULL;
1641 struct got_object_id *tree_id = NULL;
1642 struct got_tree_object *tree = NULL;
1643 const struct got_tree_entries *entries;
1644 struct got_tree_entry *te;
1646 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1650 err = got_object_open_as_tree(&tree, repo, tree_id);
1653 entries = got_object_tree_get_entries(tree);
1654 te = SIMPLEQ_FIRST(&entries->head);
1658 if (sigint_received || sigpipe_received)
1663 err = got_object_id_str(&id_str, te->id);
1666 if (asprintf(&id, "%s ", id_str) == -1) {
1667 err = got_error_from_errno("asprintf");
1673 print_entry(te, id, path, root_path);
1676 if (recurse && S_ISDIR(te->mode)) {
1678 if (asprintf(&child_path, "%s%s%s", path,
1679 path[0] == '/' && path[1] == '\0' ? "" : "/",
1681 err = got_error_from_errno("asprintf");
1684 err = print_tree(child_path, commit_id, show_ids, 1,
1691 te = SIMPLEQ_NEXT(te, entry);
1695 got_object_tree_close(tree);
1700 static const struct got_error *
1701 cmd_tree(int argc, char *argv[])
1703 const struct got_error *error;
1704 struct got_repository *repo = NULL;
1705 struct got_worktree *worktree = NULL;
1707 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1708 struct got_object_id *commit_id = NULL;
1709 char *commit_id_str = NULL;
1710 int show_ids = 0, recurse = 0;
1714 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1719 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1722 commit_id_str = optarg;
1725 repo_path = realpath(optarg, NULL);
1726 if (repo_path == NULL)
1727 err(1, "-r option");
1728 got_path_strip_trailing_slashes(repo_path);
1752 cwd = getcwd(NULL, 0);
1754 error = got_error_from_errno("getcwd");
1757 if (repo_path == NULL) {
1758 error = got_worktree_open(&worktree, cwd);
1759 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1765 strdup(got_worktree_get_repo_path(worktree));
1766 if (repo_path == NULL)
1767 error = got_error_from_errno("strdup");
1771 repo_path = strdup(cwd);
1772 if (repo_path == NULL) {
1773 error = got_error_from_errno("strdup");
1779 error = got_repo_open(&repo, repo_path);
1783 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1789 char *p, *worktree_subdir = cwd +
1790 strlen(got_worktree_get_root_path(worktree));
1791 if (asprintf(&p, "%s/%s",
1792 got_worktree_get_path_prefix(worktree),
1793 worktree_subdir) == -1) {
1794 error = got_error_from_errno("asprintf");
1797 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1804 if (in_repo_path == NULL) {
1805 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1810 if (commit_id_str == NULL) {
1811 struct got_reference *head_ref;
1812 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1815 error = got_ref_resolve(&commit_id, repo, head_ref);
1816 got_ref_close(head_ref);
1820 error = got_object_resolve_id_str(&commit_id, repo,
1826 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1827 in_repo_path, repo);
1834 got_worktree_close(worktree);
1836 const struct got_error *repo_error;
1837 repo_error = got_repo_close(repo);
1847 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1851 static const struct got_error *
1852 print_status(void *arg, unsigned char status, const char *path,
1853 struct got_object_id *blob_id, struct got_object_id *commit_id)
1855 printf("%c %s\n", status, path);
1859 static const struct got_error *
1860 cmd_status(int argc, char *argv[])
1862 const struct got_error *error = NULL;
1863 struct got_repository *repo = NULL;
1864 struct got_worktree *worktree = NULL;
1865 char *cwd = NULL, *path = NULL;
1868 while ((ch = getopt(argc, argv, "")) != -1) {
1880 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1884 cwd = getcwd(NULL, 0);
1886 error = got_error_from_errno("getcwd");
1890 error = got_worktree_open(&worktree, cwd);
1897 error = got_error_from_errno("strdup");
1900 } else if (argc == 1) {
1901 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1907 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1911 error = apply_unveil(got_repo_get_path(repo), 1,
1912 got_worktree_get_root_path(worktree), 0);
1916 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1917 check_cancelled, NULL);
1928 "usage: %s ref [-r repository] -l | -d name | name target\n",
1933 static const struct got_error *
1934 list_refs(struct got_repository *repo)
1936 static const struct got_error *err = NULL;
1937 struct got_reflist_head refs;
1938 struct got_reflist_entry *re;
1940 SIMPLEQ_INIT(&refs);
1941 err = got_ref_list(&refs, repo);
1945 SIMPLEQ_FOREACH(re, &refs, entry) {
1947 refstr = got_ref_to_str(re->ref);
1949 return got_error_from_errno("got_ref_to_str");
1950 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1954 got_ref_list_free(&refs);
1958 static const struct got_error *
1959 delete_ref(struct got_repository *repo, const char *refname)
1961 const struct got_error *err = NULL;
1962 struct got_reference *ref;
1964 err = got_ref_open(&ref, repo, refname, 0);
1968 err = got_ref_delete(ref, repo);
1973 static const struct got_error *
1974 add_ref(struct got_repository *repo, const char *refname, const char *target)
1976 const struct got_error *err = NULL;
1977 struct got_object_id *id;
1978 struct got_reference *ref = NULL;
1980 err = got_object_resolve_id_str(&id, repo, target);
1982 struct got_reference *target_ref;
1984 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1986 err = got_ref_open(&target_ref, repo, target, 0);
1989 err = got_ref_resolve(&id, repo, target_ref);
1990 got_ref_close(target_ref);
1995 err = got_ref_alloc(&ref, refname, id);
1999 err = got_ref_write(ref, repo);
2007 static const struct got_error *
2008 cmd_ref(int argc, char *argv[])
2010 const struct got_error *error = NULL;
2011 struct got_repository *repo = NULL;
2012 struct got_worktree *worktree = NULL;
2013 char *cwd = NULL, *repo_path = NULL;
2014 int ch, do_list = 0;
2015 const char *delref = NULL;
2017 /* TODO: Add -s option for adding symbolic references. */
2018 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2024 repo_path = realpath(optarg, NULL);
2025 if (repo_path == NULL)
2026 err(1, "-r option");
2027 got_path_strip_trailing_slashes(repo_path);
2038 if (do_list && delref)
2039 errx(1, "-l and -d options are mutually exclusive\n");
2044 if (do_list || delref) {
2047 } else if (argc != 2)
2052 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2056 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2057 "sendfd unveil", NULL) == -1)
2061 cwd = getcwd(NULL, 0);
2063 error = got_error_from_errno("getcwd");
2067 if (repo_path == NULL) {
2068 error = got_worktree_open(&worktree, cwd);
2069 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2075 strdup(got_worktree_get_repo_path(worktree));
2076 if (repo_path == NULL)
2077 error = got_error_from_errno("strdup");
2081 repo_path = strdup(cwd);
2082 if (repo_path == NULL) {
2083 error = got_error_from_errno("strdup");
2089 error = got_repo_open(&repo, repo_path);
2093 error = apply_unveil(got_repo_get_path(repo), do_list,
2094 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2099 error = list_refs(repo);
2101 error = delete_ref(repo, delref);
2103 error = add_ref(repo, argv[0], argv[1]);
2106 got_repo_close(repo);
2108 got_worktree_close(worktree);
2117 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2121 static const struct got_error *
2122 cmd_add(int argc, char *argv[])
2124 const struct got_error *error = NULL;
2125 struct got_repository *repo = NULL;
2126 struct got_worktree *worktree = NULL;
2128 struct got_pathlist_head paths;
2129 struct got_pathlist_entry *pe;
2134 while ((ch = getopt(argc, argv, "")) != -1) {
2148 /* make sure each file exists before doing anything halfway */
2149 for (x = 0; x < argc; x++) {
2150 char *path = realpath(argv[x], NULL);
2152 error = got_error_from_errno2("realpath", argv[x]);
2158 cwd = getcwd(NULL, 0);
2160 error = got_error_from_errno("getcwd");
2164 error = got_worktree_open(&worktree, cwd);
2168 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2172 error = apply_unveil(got_repo_get_path(repo), 1,
2173 got_worktree_get_root_path(worktree), 0);
2177 for (x = 0; x < argc; x++) {
2178 char *path = realpath(argv[x], NULL);
2180 error = got_error_from_errno2("realpath", argv[x]);
2184 got_path_strip_trailing_slashes(path);
2185 error = got_pathlist_insert(&pe, &paths, path, NULL);
2191 error = got_worktree_schedule_add(worktree, &paths, print_status,
2195 got_repo_close(repo);
2197 got_worktree_close(worktree);
2198 TAILQ_FOREACH(pe, &paths, entry)
2199 free((char *)pe->path);
2200 got_pathlist_free(&paths);
2208 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2212 static const struct got_error *
2213 cmd_rm(int argc, char *argv[])
2215 const struct got_error *error = NULL;
2216 struct got_worktree *worktree = NULL;
2217 struct got_repository *repo = NULL;
2219 struct got_pathlist_head paths;
2220 struct got_pathlist_entry *pe;
2221 int ch, i, delete_local_mods = 0;
2225 while ((ch = getopt(argc, argv, "f")) != -1) {
2228 delete_local_mods = 1;
2242 /* make sure each file exists before doing anything halfway */
2243 for (i = 0; i < argc; i++) {
2244 char *path = realpath(argv[i], NULL);
2246 error = got_error_from_errno2("realpath", argv[i]);
2252 cwd = getcwd(NULL, 0);
2254 error = got_error_from_errno("getcwd");
2257 error = got_worktree_open(&worktree, cwd);
2261 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2265 error = apply_unveil(got_repo_get_path(repo), 1,
2266 got_worktree_get_root_path(worktree), 0);
2270 for (i = 0; i < argc; i++) {
2271 char *path = realpath(argv[i], NULL);
2273 error = got_error_from_errno2("realpath", argv[i]);
2277 got_path_strip_trailing_slashes(path);
2278 error = got_pathlist_insert(&pe, &paths, path, NULL);
2284 error = got_worktree_schedule_delete(worktree, &paths,
2285 delete_local_mods, print_status, NULL, repo);
2290 got_repo_close(repo);
2292 got_worktree_close(worktree);
2293 TAILQ_FOREACH(pe, &paths, entry)
2294 free((char *)pe->path);
2295 got_pathlist_free(&paths);
2303 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2308 revert_progress(void *arg, unsigned char status, const char *path)
2310 while (path[0] == '/')
2312 printf("%c %s\n", status, path);
2315 static const struct got_error *
2316 cmd_revert(int argc, char *argv[])
2318 const struct got_error *error = NULL;
2319 struct got_worktree *worktree = NULL;
2320 struct got_repository *repo = NULL;
2321 char *cwd = NULL, *path = NULL;
2324 while ((ch = getopt(argc, argv, "")) != -1) {
2338 path = realpath(argv[0], NULL);
2340 error = got_error_from_errno2("realpath", argv[0]);
2343 got_path_strip_trailing_slashes(path);
2345 cwd = getcwd(NULL, 0);
2347 error = got_error_from_errno("getcwd");
2350 error = got_worktree_open(&worktree, cwd);
2354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2358 error = apply_unveil(got_repo_get_path(repo), 1,
2359 got_worktree_get_root_path(worktree), 0);
2363 error = got_worktree_revert(worktree, path,
2364 revert_progress, NULL, repo);
2369 got_repo_close(repo);
2371 got_worktree_close(worktree);
2380 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2385 spawn_editor(const char *editor, const char *file)
2388 sig_t sighup, sigint, sigquit;
2391 sighup = signal(SIGHUP, SIG_IGN);
2392 sigint = signal(SIGINT, SIG_IGN);
2393 sigquit = signal(SIGQUIT, SIG_IGN);
2395 switch (pid = fork()) {
2399 execl(editor, editor, file, (char *)NULL);
2403 while (waitpid(pid, &st, 0) == -1)
2408 (void)signal(SIGHUP, sighup);
2409 (void)signal(SIGINT, sigint);
2410 (void)signal(SIGQUIT, sigquit);
2412 if (!WIFEXITED(st)) {
2417 return WEXITSTATUS(st);
2420 struct collect_commit_logmsg_arg {
2421 const char *cmdline_log;
2423 const char *worktree_path;
2424 const char *repo_path;
2429 static const struct got_error *
2430 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2433 const char *initial_content = "\n# changes to be committed:\n";
2434 struct got_pathlist_entry *pe;
2435 const struct got_error *err = NULL;
2436 char *template = NULL;
2437 struct collect_commit_logmsg_arg *a = arg;
2439 struct stat st, st2;
2442 int fd, content_changed = 0;
2444 /* if a message was specified on the command line, just use it */
2445 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2446 len = strlen(a->cmdline_log) + 1;
2447 *logmsg = malloc(len + 1);
2448 if (*logmsg == NULL)
2449 return got_error_from_errno("malloc");
2450 strlcpy(*logmsg, a->cmdline_log, len);
2454 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2455 return got_error_from_errno("asprintf");
2457 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2461 dprintf(fd, initial_content);
2463 TAILQ_FOREACH(pe, commitable_paths, entry) {
2464 struct got_commitable *ct = pe->data;
2465 dprintf(fd, "# %c %s\n",
2466 got_commitable_get_status(ct),
2467 got_commitable_get_path(ct));
2471 if (stat(a->logmsg_path, &st) == -1) {
2472 err = got_error_from_errno2("stat", a->logmsg_path);
2476 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2477 err = got_error_from_errno("failed spawning editor");
2481 if (stat(a->logmsg_path, &st2) == -1) {
2482 err = got_error_from_errno("stat");
2486 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2487 unlink(a->logmsg_path);
2488 free(a->logmsg_path);
2489 a->logmsg_path = NULL;
2490 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2491 "no changes made to commit message, aborting");
2495 *logmsg = malloc(st2.st_size + 1);
2496 if (*logmsg == NULL) {
2497 err = got_error_from_errno("malloc");
2500 (*logmsg)[0] = '\0';
2503 fp = fopen(a->logmsg_path, "r");
2505 err = got_error_from_errno("fopen");
2508 while (fgets(buf, sizeof(buf), fp) != NULL) {
2509 if (!content_changed && strcmp(buf, initial_content) != 0)
2510 content_changed = 1;
2511 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2512 continue; /* remove comments and leading empty lines */
2513 len = strlcat(*logmsg, buf, st2.st_size);
2517 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2518 (*logmsg)[len - 1] = '\0';
2522 if (len == 0 || !content_changed) {
2523 unlink(a->logmsg_path);
2524 free(a->logmsg_path);
2525 a->logmsg_path = NULL;
2526 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2527 "commit message cannot be empty, aborting");
2533 /* Editor is done; we can now apply unveil(2) */
2535 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2539 static const struct got_error *
2540 cmd_commit(int argc, char *argv[])
2542 const struct got_error *error = NULL;
2543 struct got_worktree *worktree = NULL;
2544 struct got_repository *repo = NULL;
2545 char *cwd = NULL, *path = NULL, *id_str = NULL;
2546 struct got_object_id *id = NULL;
2547 const char *logmsg = NULL;
2548 const char *got_author = getenv("GOT_AUTHOR");
2549 struct collect_commit_logmsg_arg cl_arg;
2550 char *editor = NULL;
2553 while ((ch = getopt(argc, argv, "m:")) != -1) {
2568 path = realpath(argv[0], NULL);
2570 error = got_error_from_errno2("realpath", argv[0]);
2573 got_path_strip_trailing_slashes(path);
2574 } else if (argc != 0)
2577 if (got_author == NULL) {
2578 /* TODO: Look current user up in password database */
2579 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2583 cwd = getcwd(NULL, 0);
2585 error = got_error_from_errno("getcwd");
2588 error = got_worktree_open(&worktree, cwd);
2592 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2597 * unveil(2) traverses exec(2); if an editor is used we have
2598 * to apply unveil after the log message has been written.
2600 if (logmsg == NULL || strlen(logmsg) == 0)
2601 error = get_editor(&editor);
2603 error = apply_unveil(got_repo_get_path(repo), 0,
2604 got_worktree_get_root_path(worktree), 0);
2608 cl_arg.editor = editor;
2609 cl_arg.cmdline_log = logmsg;
2610 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2611 cl_arg.repo_path = got_repo_get_path(repo);
2612 cl_arg.logmsg_path = NULL;
2613 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2614 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2616 if (cl_arg.logmsg_path)
2617 fprintf(stderr, "%s: log message preserved in %s\n",
2618 getprogname(), cl_arg.logmsg_path);
2622 if (cl_arg.logmsg_path)
2623 unlink(cl_arg.logmsg_path);
2625 error = got_object_id_str(&id_str, id);
2628 printf("Created commit %s\n", id_str);
2631 got_repo_close(repo);
2633 got_worktree_close(worktree);
2642 usage_cherrypick(void)
2644 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2648 static const struct got_error *
2649 cmd_cherrypick(int argc, char *argv[])
2651 const struct got_error *error = NULL;
2652 struct got_worktree *worktree = NULL;
2653 struct got_repository *repo = NULL;
2654 char *cwd = NULL, *commit_id_str = NULL;
2655 struct got_object_id *commit_id = NULL;
2656 struct got_commit_object *commit = NULL;
2657 struct got_object_qid *pid;
2658 struct got_reference *head_ref = NULL;
2659 int ch, did_something = 0;
2661 while ((ch = getopt(argc, argv, "")) != -1) {
2675 cwd = getcwd(NULL, 0);
2677 error = got_error_from_errno("getcwd");
2680 error = got_worktree_open(&worktree, cwd);
2684 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2688 error = apply_unveil(got_repo_get_path(repo), 0,
2689 got_worktree_get_root_path(worktree), 0);
2693 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2694 if (error != NULL) {
2695 struct got_reference *ref;
2696 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2698 error = got_ref_open(&ref, repo, argv[0], 0);
2701 error = got_ref_resolve(&commit_id, repo, ref);
2706 error = got_object_id_str(&commit_id_str, commit_id);
2710 error = got_ref_open(&head_ref, repo,
2711 got_worktree_get_head_ref_name(worktree), 0);
2715 error = check_same_branch(commit_id, head_ref, repo);
2717 if (error->code != GOT_ERR_ANCESTRY)
2721 error = got_error(GOT_ERR_SAME_BRANCH);
2725 error = got_object_open_as_commit(&commit, repo, commit_id);
2728 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2729 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2730 commit_id, repo, update_progress, &did_something, check_cancelled,
2736 printf("Merged commit %s\n", commit_id_str);
2739 got_object_commit_close(commit);
2740 free(commit_id_str);
2742 got_ref_close(head_ref);
2744 got_worktree_close(worktree);
2746 got_repo_close(repo);
2753 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2757 static const struct got_error *
2758 cmd_backout(int argc, char *argv[])
2760 const struct got_error *error = NULL;
2761 struct got_worktree *worktree = NULL;
2762 struct got_repository *repo = NULL;
2763 char *cwd = NULL, *commit_id_str = NULL;
2764 struct got_object_id *commit_id = NULL;
2765 struct got_commit_object *commit = NULL;
2766 struct got_object_qid *pid;
2767 struct got_reference *head_ref = NULL;
2768 int ch, did_something = 0;
2770 while ((ch = getopt(argc, argv, "")) != -1) {
2784 cwd = getcwd(NULL, 0);
2786 error = got_error_from_errno("getcwd");
2789 error = got_worktree_open(&worktree, cwd);
2793 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2797 error = apply_unveil(got_repo_get_path(repo), 0,
2798 got_worktree_get_root_path(worktree), 0);
2802 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2803 if (error != NULL) {
2804 struct got_reference *ref;
2805 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2807 error = got_ref_open(&ref, repo, argv[0], 0);
2810 error = got_ref_resolve(&commit_id, repo, ref);
2815 error = got_object_id_str(&commit_id_str, commit_id);
2819 error = got_ref_open(&head_ref, repo,
2820 got_worktree_get_head_ref_name(worktree), 0);
2824 error = check_same_branch(commit_id, head_ref, repo);
2828 error = got_object_open_as_commit(&commit, repo, commit_id);
2831 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2833 error = got_error(GOT_ERR_ROOT_COMMIT);
2837 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2838 update_progress, &did_something, check_cancelled, NULL);
2843 printf("Backed out commit %s\n", commit_id_str);
2846 got_object_commit_close(commit);
2847 free(commit_id_str);
2849 got_ref_close(head_ref);
2851 got_worktree_close(worktree);
2853 got_repo_close(repo);