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 /* Base commit bump happens silently. */
289 if (status == GOT_STATUS_BUMP_BASE)
292 while (path[0] == '/')
295 printf("%c %s/%s\n", status, worktree_path, path);
298 static const struct got_error *
299 check_cancelled(void *arg)
301 if (sigint_received || sigpipe_received)
302 return got_error(GOT_ERR_CANCELLED);
306 static const struct got_error *
307 check_linear_ancestry(struct got_object_id *commit_id,
308 struct got_object_id *base_commit_id, struct got_repository *repo)
310 const struct got_error *err = NULL;
311 struct got_object_id *yca_id;
313 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
314 commit_id, base_commit_id, repo);
319 return got_error(GOT_ERR_ANCESTRY);
322 * Require a straight line of history between the target commit
323 * and the work tree's base commit.
325 * Non-linear situations such as this require a rebase:
327 * (commit) D F (base_commit)
335 * 'got update' only handles linear cases:
336 * Update forwards in time: A (base/yca) - B - C - D (commit)
337 * Update backwards in time: D (base) - C - B - A (commit/yca)
339 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
340 got_object_id_cmp(base_commit_id, yca_id) != 0)
341 return got_error(GOT_ERR_ANCESTRY);
347 static const struct got_error *
348 check_same_branch(struct got_object_id *commit_id,
349 struct got_reference *head_ref, struct got_repository *repo)
351 const struct got_error *err = NULL;
352 struct got_commit_graph *graph = NULL;
353 struct got_object_id *head_commit_id = NULL;
354 int is_same_branch = 0;
356 err = got_ref_resolve(&head_commit_id, repo, head_ref);
360 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
364 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
369 struct got_object_id *id;
370 err = got_commit_graph_iter_next(&id, graph);
372 if (err->code == GOT_ERR_ITER_COMPLETED) {
376 else if (err->code != GOT_ERR_ITER_NEED_MORE)
378 err = got_commit_graph_fetch_commits(graph, 1,
385 if (got_object_id_cmp(id, commit_id) == 0) {
393 got_commit_graph_close(graph);
394 free(head_commit_id);
395 if (!err && !is_same_branch)
396 err = got_error(GOT_ERR_ANCESTRY);
400 static const struct got_error *
401 cmd_checkout(int argc, char *argv[])
403 const struct got_error *error = NULL;
404 struct got_repository *repo = NULL;
405 struct got_reference *head_ref = NULL;
406 struct got_worktree *worktree = NULL;
407 char *repo_path = NULL;
408 char *worktree_path = NULL;
409 const char *path_prefix = "";
410 const char *branch_name = GOT_REF_HEAD;
411 char *commit_id_str = NULL;
412 int ch, same_path_prefix;
414 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
417 branch_name = optarg;
420 commit_id_str = strdup(optarg);
421 if (commit_id_str == NULL)
422 return got_error_from_errno("strdup");
425 path_prefix = optarg;
437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
438 "unveil", NULL) == -1)
442 char *cwd, *base, *dotgit;
443 repo_path = realpath(argv[0], NULL);
444 if (repo_path == NULL)
445 return got_error_from_errno2("realpath", argv[0]);
446 cwd = getcwd(NULL, 0);
448 error = got_error_from_errno("getcwd");
451 if (path_prefix[0]) {
452 base = basename(path_prefix);
454 error = got_error_from_errno2("basename",
459 base = basename(repo_path);
461 error = got_error_from_errno2("basename",
466 dotgit = strstr(base, ".git");
469 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
470 error = got_error_from_errno("asprintf");
475 } else if (argc == 2) {
476 repo_path = realpath(argv[0], NULL);
477 if (repo_path == NULL) {
478 error = got_error_from_errno2("realpath", argv[0]);
481 worktree_path = realpath(argv[1], NULL);
482 if (worktree_path == NULL) {
483 error = got_error_from_errno2("realpath", argv[1]);
489 got_path_strip_trailing_slashes(repo_path);
490 got_path_strip_trailing_slashes(worktree_path);
492 error = got_repo_open(&repo, repo_path);
496 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
500 error = got_ref_open(&head_ref, repo, branch_name, 0);
504 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
505 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
508 error = got_worktree_open(&worktree, worktree_path);
512 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
516 if (!same_path_prefix) {
517 error = got_error(GOT_ERR_PATH_PREFIX);
522 struct got_object_id *commit_id;
523 error = got_object_resolve_id_str(&commit_id, repo,
527 error = check_linear_ancestry(commit_id,
528 got_worktree_get_base_commit_id(worktree), repo);
533 error = check_same_branch(commit_id, head_ref, repo);
536 error = got_worktree_set_base_commit_id(worktree, repo,
543 error = got_worktree_checkout_files(worktree, "", repo,
544 checkout_progress, worktree_path, check_cancelled, NULL);
548 printf("Now shut up and hack\n");
560 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
566 update_progress(void *arg, unsigned char status, const char *path)
568 int *did_something = arg;
570 if (status == GOT_STATUS_EXISTS)
575 /* Base commit bump happens silently. */
576 if (status == GOT_STATUS_BUMP_BASE)
579 while (path[0] == '/')
581 printf("%c %s\n", status, path);
584 static const struct got_error *
585 switch_head_ref(struct got_reference *head_ref,
586 struct got_object_id *commit_id, struct got_worktree *worktree,
587 struct got_repository *repo)
589 const struct got_error *err = NULL;
591 int ref_has_moved = 0;
593 /* Trivial case: switching between two different references. */
594 if (strcmp(got_ref_get_name(head_ref),
595 got_worktree_get_head_ref_name(worktree)) != 0) {
596 printf("Switching work tree from %s to %s\n",
597 got_worktree_get_head_ref_name(worktree),
598 got_ref_get_name(head_ref));
599 return got_worktree_set_head_ref(worktree, head_ref);
602 err = check_linear_ancestry(commit_id,
603 got_worktree_get_base_commit_id(worktree), repo);
605 if (err->code != GOT_ERR_ANCESTRY)
612 /* Switching to a rebased branch with the same reference name. */
613 err = got_object_id_str(&base_id_str,
614 got_worktree_get_base_commit_id(worktree));
617 printf("Reference %s now points at a different branch\n",
618 got_worktree_get_head_ref_name(worktree));
619 printf("Switching work tree from %s to %s\n", base_id_str,
620 got_worktree_get_head_ref_name(worktree));
624 static const struct got_error *
625 cmd_update(int argc, char *argv[])
627 const struct got_error *error = NULL;
628 struct got_repository *repo = NULL;
629 struct got_worktree *worktree = NULL;
630 char *worktree_path = NULL, *path = NULL;
631 struct got_object_id *commit_id = NULL;
632 char *commit_id_str = NULL;
633 const char *branch_name = NULL;
634 struct got_reference *head_ref = NULL;
635 int ch, did_something = 0;
637 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
640 branch_name = optarg;
643 commit_id_str = strdup(optarg);
644 if (commit_id_str == NULL)
645 return got_error_from_errno("strdup");
657 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
658 "unveil", NULL) == -1)
661 worktree_path = getcwd(NULL, 0);
662 if (worktree_path == NULL) {
663 error = got_error_from_errno("getcwd");
666 error = got_worktree_open(&worktree, worktree_path);
673 error = got_error_from_errno("strdup");
676 } else if (argc == 1) {
677 error = got_worktree_resolve_path(&path, worktree, argv[0]);
683 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
687 error = apply_unveil(got_repo_get_path(repo), 0,
688 got_worktree_get_root_path(worktree), 0);
692 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
693 got_worktree_get_head_ref_name(worktree), 0);
696 if (commit_id_str == NULL) {
697 error = got_ref_resolve(&commit_id, repo, head_ref);
700 error = got_object_id_str(&commit_id_str, commit_id);
704 error = got_object_resolve_id_str(&commit_id, repo,
711 struct got_object_id *head_commit_id;
712 if (strlen(path) != 0) {
713 fprintf(stderr, "%s: switching between branches "
714 "requires that the entire work tree "
715 "gets updated, not just '%s'\n",
716 getprogname(), path);
717 error = got_error(GOT_ERR_BAD_PATH);
720 error = got_ref_resolve(&head_commit_id, repo, head_ref);
723 error = check_linear_ancestry(commit_id, head_commit_id, repo);
724 free(head_commit_id);
727 error = check_same_branch(commit_id, head_ref, repo);
730 error = switch_head_ref(head_ref, commit_id, worktree, repo);
734 error = check_linear_ancestry(commit_id,
735 got_worktree_get_base_commit_id(worktree), repo);
737 if (error->code == GOT_ERR_ANCESTRY)
738 error = got_error(GOT_ERR_BRANCH_MOVED);
741 error = check_same_branch(commit_id, head_ref, repo);
746 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
748 error = got_worktree_set_base_commit_id(worktree, repo,
754 error = got_worktree_checkout_files(worktree, path, repo,
755 update_progress, &did_something, check_cancelled, NULL);
760 printf("Updated to commit %s\n", commit_id_str);
762 printf("Already up-to-date\n");
771 static const struct got_error *
772 print_patch(struct got_commit_object *commit, struct got_object_id *id,
773 int diff_context, struct got_repository *repo)
775 const struct got_error *err = NULL;
776 struct got_tree_object *tree1 = NULL, *tree2;
777 struct got_object_qid *qid;
778 char *id_str1 = NULL, *id_str2;
779 struct got_diff_blob_output_unidiff_arg arg;
781 err = got_object_open_as_tree(&tree2, repo,
782 got_object_commit_get_tree_id(commit));
786 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
788 struct got_commit_object *pcommit;
790 err = got_object_open_as_commit(&pcommit, repo, qid->id);
794 err = got_object_open_as_tree(&tree1, repo,
795 got_object_commit_get_tree_id(pcommit));
796 got_object_commit_close(pcommit);
800 err = got_object_id_str(&id_str1, qid->id);
805 err = got_object_id_str(&id_str2, id);
809 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
810 arg.diff_context = diff_context;
811 arg.outfile = stdout;
812 err = got_diff_tree(tree1, tree2, "", "", repo,
813 got_diff_blob_output_unidiff, &arg);
816 got_object_tree_close(tree1);
817 got_object_tree_close(tree2);
824 get_datestr(time_t *time, char *datebuf)
826 char *p, *s = ctime_r(time, datebuf);
833 static const struct got_error *
834 print_commit(struct got_commit_object *commit, struct got_object_id *id,
835 struct got_repository *repo, int show_patch, int diff_context,
836 struct got_reflist_head *refs)
838 const struct got_error *err = NULL;
839 char *id_str, *datestr, *logmsg0, *logmsg, *line;
841 time_t committer_time;
842 const char *author, *committer;
843 char *refs_str = NULL;
844 struct got_reflist_entry *re;
846 SIMPLEQ_FOREACH(re, refs, entry) {
849 if (got_object_id_cmp(re->id, id) != 0)
851 name = got_ref_get_name(re->ref);
852 if (strcmp(name, GOT_REF_HEAD) == 0)
854 if (strncmp(name, "refs/", 5) == 0)
856 if (strncmp(name, "got/", 4) == 0)
858 if (strncmp(name, "heads/", 6) == 0)
860 if (strncmp(name, "remotes/", 8) == 0)
863 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
865 err = got_error_from_errno("asprintf");
871 err = got_object_id_str(&id_str, id);
875 printf("-----------------------------------------------\n");
876 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
877 refs_str ? refs_str : "", refs_str ? ")" : "");
882 printf("from: %s\n", got_object_commit_get_author(commit));
883 committer_time = got_object_commit_get_committer_time(commit);
884 datestr = get_datestr(&committer_time, datebuf);
885 printf("date: %s UTC\n", datestr);
886 author = got_object_commit_get_author(commit);
887 committer = got_object_commit_get_committer(commit);
888 if (strcmp(author, committer) != 0)
889 printf("via: %s\n", committer);
890 if (got_object_commit_get_nparents(commit) > 1) {
891 const struct got_object_id_queue *parent_ids;
892 struct got_object_qid *qid;
894 parent_ids = got_object_commit_get_parent_ids(commit);
895 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
896 err = got_object_id_str(&id_str, qid->id);
899 printf("parent %d: %s\n", n++, id_str);
904 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
906 return got_error_from_errno("strdup");
910 line = strsep(&logmsg, "\n");
912 printf(" %s\n", line);
917 err = print_patch(commit, id, diff_context, repo);
922 if (fflush(stdout) != 0 && err == NULL)
923 err = got_error_from_errno("fflush");
927 static const struct got_error *
928 print_commits(struct got_object_id *root_id, struct got_repository *repo,
929 char *path, int show_patch, int diff_context, int limit,
930 int first_parent_traversal, struct got_reflist_head *refs)
932 const struct got_error *err;
933 struct got_commit_graph *graph;
935 err = got_commit_graph_open(&graph, root_id, path,
936 first_parent_traversal, repo);
939 err = got_commit_graph_iter_start(graph, root_id, repo);
943 struct got_commit_object *commit;
944 struct got_object_id *id;
946 if (sigint_received || sigpipe_received)
949 err = got_commit_graph_iter_next(&id, graph);
951 if (err->code == GOT_ERR_ITER_COMPLETED) {
955 if (err->code != GOT_ERR_ITER_NEED_MORE)
957 err = got_commit_graph_fetch_commits(graph, 1, repo);
966 err = got_object_open_as_commit(&commit, repo, id);
969 err = print_commit(commit, id, repo, show_patch, diff_context,
971 got_object_commit_close(commit);
972 if (err || (limit && --limit == 0))
976 got_commit_graph_close(graph);
983 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
984 "[-r repository-path] [path]\n", getprogname());
988 static const struct got_error *
989 cmd_log(int argc, char *argv[])
991 const struct got_error *error;
992 struct got_repository *repo = NULL;
993 struct got_worktree *worktree = NULL;
994 struct got_commit_object *commit = NULL;
995 struct got_object_id *id = NULL;
996 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
997 char *start_commit = NULL;
998 int diff_context = 3, ch;
999 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1001 struct got_reflist_head refs;
1003 SIMPLEQ_INIT(&refs);
1006 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1012 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1015 first_parent_traversal = 1;
1021 start_commit = optarg;
1024 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1027 err(1, "-C option %s", errstr);
1030 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1032 err(1, "-l option %s", errstr);
1035 repo_path = realpath(optarg, NULL);
1036 if (repo_path == NULL)
1037 err(1, "-r option");
1038 got_path_strip_trailing_slashes(repo_path);
1049 cwd = getcwd(NULL, 0);
1051 error = got_error_from_errno("getcwd");
1055 error = got_worktree_open(&worktree, cwd);
1056 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1063 error = got_error_from_errno("strdup");
1066 } else if (argc == 1) {
1068 error = got_worktree_resolve_path(&path, worktree,
1073 path = strdup(argv[0]);
1075 error = got_error_from_errno("strdup");
1082 if (repo_path == NULL) {
1083 repo_path = worktree ?
1084 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1086 if (repo_path == NULL) {
1087 error = got_error_from_errno("strdup");
1091 error = got_repo_open(&repo, repo_path);
1095 error = apply_unveil(got_repo_get_path(repo), 1,
1096 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1100 if (start_commit == NULL) {
1101 struct got_reference *head_ref;
1102 error = got_ref_open(&head_ref, repo,
1103 worktree ? got_worktree_get_head_ref_name(worktree)
1107 error = got_ref_resolve(&id, repo, head_ref);
1108 got_ref_close(head_ref);
1111 error = got_object_open_as_commit(&commit, repo, id);
1113 struct got_reference *ref;
1114 error = got_ref_open(&ref, repo, start_commit, 0);
1115 if (error == NULL) {
1117 error = got_ref_resolve(&id, repo, ref);
1121 error = got_object_get_type(&obj_type, repo, id);
1124 if (obj_type == GOT_OBJ_TYPE_TAG) {
1125 struct got_tag_object *tag;
1126 error = got_object_open_as_tag(&tag, repo, id);
1129 if (got_object_tag_get_object_type(tag) !=
1130 GOT_OBJ_TYPE_COMMIT) {
1131 got_object_tag_close(tag);
1132 error = got_error(GOT_ERR_OBJ_TYPE);
1136 id = got_object_id_dup(
1137 got_object_tag_get_object_id(tag));
1139 error = got_error_from_errno(
1140 "got_object_id_dup");
1141 got_object_tag_close(tag);
1144 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1145 error = got_error(GOT_ERR_OBJ_TYPE);
1148 error = got_object_open_as_commit(&commit, repo, id);
1152 if (commit == NULL) {
1153 error = got_object_resolve_id_str(&id, repo,
1162 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1167 path = in_repo_path;
1170 error = got_ref_list(&refs, repo);
1174 error = print_commits(id, repo, path, show_patch,
1175 diff_context, limit, first_parent_traversal, &refs);
1182 got_worktree_close(worktree);
1184 const struct got_error *repo_error;
1185 repo_error = got_repo_close(repo);
1189 got_ref_list_free(&refs);
1196 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1197 "[object1 object2 | path]\n", getprogname());
1201 struct print_diff_arg {
1202 struct got_repository *repo;
1203 struct got_worktree *worktree;
1209 static const struct got_error *
1210 print_diff(void *arg, unsigned char status, const char *path,
1211 struct got_object_id *blob_id, struct got_object_id *commit_id)
1213 struct print_diff_arg *a = arg;
1214 const struct got_error *err = NULL;
1215 struct got_blob_object *blob1 = NULL;
1217 char *abspath = NULL;
1220 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1221 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1224 if (!a->header_shown) {
1225 printf("diff %s %s\n", a->id_str,
1226 got_worktree_get_root_path(a->worktree));
1227 a->header_shown = 1;
1230 if (status != GOT_STATUS_ADD) {
1231 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1237 if (status != GOT_STATUS_DELETE) {
1238 if (asprintf(&abspath, "%s/%s",
1239 got_worktree_get_root_path(a->worktree), path) == -1) {
1240 err = got_error_from_errno("asprintf");
1244 f2 = fopen(abspath, "r");
1246 err = got_error_from_errno2("fopen", abspath);
1249 if (lstat(abspath, &sb) == -1) {
1250 err = got_error_from_errno2("lstat", abspath);
1256 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1260 got_object_blob_close(blob1);
1261 if (f2 && fclose(f2) != 0 && err == NULL)
1262 err = got_error_from_errno("fclose");
1267 static const struct got_error *
1268 cmd_diff(int argc, char *argv[])
1270 const struct got_error *error;
1271 struct got_repository *repo = NULL;
1272 struct got_worktree *worktree = NULL;
1273 char *cwd = NULL, *repo_path = NULL;
1274 struct got_object_id *id1 = NULL, *id2 = NULL;
1275 const char *id_str1 = NULL, *id_str2 = NULL;
1276 char *label1 = NULL, *label2 = NULL;
1278 int diff_context = 3, ch;
1283 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1288 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1291 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1293 err(1, "-C option %s", errstr);
1296 repo_path = realpath(optarg, NULL);
1297 if (repo_path == NULL)
1298 err(1, "-r option");
1299 got_path_strip_trailing_slashes(repo_path);
1310 cwd = getcwd(NULL, 0);
1312 error = got_error_from_errno("getcwd");
1315 error = got_worktree_open(&worktree, cwd);
1316 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1319 if (worktree == NULL) {
1320 error = got_error(GOT_ERR_NOT_WORKTREE);
1325 "-r option can't be used when diffing a work tree");
1326 repo_path = strdup(got_worktree_get_repo_path(worktree));
1327 if (repo_path == NULL) {
1328 error = got_error_from_errno("strdup");
1332 error = got_worktree_resolve_path(&path, worktree,
1339 error = got_error_from_errno("strdup");
1343 } else if (argc == 2) {
1346 if (worktree && repo_path == NULL) {
1348 strdup(got_worktree_get_repo_path(worktree));
1349 if (repo_path == NULL) {
1350 error = got_error_from_errno("strdup");
1357 if (repo_path == NULL) {
1358 repo_path = getcwd(NULL, 0);
1359 if (repo_path == NULL)
1360 return got_error_from_errno("getcwd");
1363 error = got_repo_open(&repo, repo_path);
1368 error = apply_unveil(got_repo_get_path(repo), 1,
1369 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1374 struct print_diff_arg arg;
1376 error = got_object_id_str(&id_str,
1377 got_worktree_get_base_commit_id(worktree));
1381 arg.worktree = worktree;
1382 arg.diff_context = diff_context;
1383 arg.id_str = id_str;
1384 arg.header_shown = 0;
1386 error = got_worktree_status(worktree, path, repo, print_diff,
1387 &arg, check_cancelled, NULL);
1392 error = got_object_resolve_id_str(&id1, repo, id_str1);
1394 struct got_reference *ref;
1395 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1397 error = got_ref_open(&ref, repo, id_str1, 0);
1400 label1 = strdup(got_ref_get_name(ref));
1401 if (label1 == NULL) {
1402 error = got_error_from_errno("strdup");
1405 error = got_ref_resolve(&id1, repo, ref);
1410 label1 = strdup(id_str1);
1411 if (label1 == NULL) {
1412 error = got_error_from_errno("strdup");
1417 error = got_object_resolve_id_str(&id2, repo, id_str2);
1419 struct got_reference *ref;
1420 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1422 error = got_ref_open(&ref, repo, id_str2, 0);
1425 label2 = strdup(got_ref_get_name(ref));
1426 if (label2 == NULL) {
1427 error = got_error_from_errno("strdup");
1430 error = got_ref_resolve(&id2, repo, ref);
1435 label2 = strdup(id_str2);
1436 if (label2 == NULL) {
1437 error = got_error_from_errno("strdup");
1442 error = got_object_get_type(&type1, repo, id1);
1446 error = got_object_get_type(&type2, repo, id2);
1450 if (type1 != type2) {
1451 error = got_error(GOT_ERR_OBJ_TYPE);
1456 case GOT_OBJ_TYPE_BLOB:
1457 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1458 diff_context, repo, stdout);
1460 case GOT_OBJ_TYPE_TREE:
1461 error = got_diff_objects_as_trees(id1, id2, "", "",
1462 diff_context, repo, stdout);
1464 case GOT_OBJ_TYPE_COMMIT:
1465 printf("diff %s %s\n", label1, label2);
1466 error = got_diff_objects_as_commits(id1, id2, diff_context,
1470 error = got_error(GOT_ERR_OBJ_TYPE);
1480 got_worktree_close(worktree);
1482 const struct got_error *repo_error;
1483 repo_error = got_repo_close(repo);
1494 "usage: %s blame [-c commit] [-r repository-path] path\n",
1499 static const struct got_error *
1500 cmd_blame(int argc, char *argv[])
1502 const struct got_error *error;
1503 struct got_repository *repo = NULL;
1504 struct got_worktree *worktree = NULL;
1505 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1506 struct got_object_id *commit_id = NULL;
1507 char *commit_id_str = NULL;
1511 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1516 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1519 commit_id_str = optarg;
1522 repo_path = realpath(optarg, NULL);
1523 if (repo_path == NULL)
1524 err(1, "-r option");
1525 got_path_strip_trailing_slashes(repo_path);
1541 cwd = getcwd(NULL, 0);
1543 error = got_error_from_errno("getcwd");
1546 if (repo_path == NULL) {
1547 error = got_worktree_open(&worktree, cwd);
1548 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1554 strdup(got_worktree_get_repo_path(worktree));
1555 if (repo_path == NULL)
1556 error = got_error_from_errno("strdup");
1560 repo_path = strdup(cwd);
1561 if (repo_path == NULL) {
1562 error = got_error_from_errno("strdup");
1568 error = got_repo_open(&repo, repo_path);
1572 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1577 const char *prefix = got_worktree_get_path_prefix(worktree);
1578 char *p, *worktree_subdir = cwd +
1579 strlen(got_worktree_get_root_path(worktree));
1580 if (asprintf(&p, "%s%s%s%s%s",
1581 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1582 worktree_subdir, worktree_subdir[0] ? "/" : "",
1584 error = got_error_from_errno("asprintf");
1587 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1590 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1595 if (commit_id_str == NULL) {
1596 struct got_reference *head_ref;
1597 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1600 error = got_ref_resolve(&commit_id, repo, head_ref);
1601 got_ref_close(head_ref);
1605 error = got_object_resolve_id_str(&commit_id, repo,
1611 error = got_blame(in_repo_path, commit_id, repo, stdout);
1618 got_worktree_close(worktree);
1620 const struct got_error *repo_error;
1621 repo_error = got_repo_close(repo);
1632 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1638 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1639 const char *root_path)
1641 int is_root_path = (strcmp(path, root_path) == 0);
1643 path += strlen(root_path);
1644 while (path[0] == '/')
1647 printf("%s%s%s%s%s\n", id ? id : "", path,
1648 is_root_path ? "" : "/", te->name,
1649 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1652 static const struct got_error *
1653 print_tree(const char *path, struct got_object_id *commit_id,
1654 int show_ids, int recurse, const char *root_path,
1655 struct got_repository *repo)
1657 const struct got_error *err = NULL;
1658 struct got_object_id *tree_id = NULL;
1659 struct got_tree_object *tree = NULL;
1660 const struct got_tree_entries *entries;
1661 struct got_tree_entry *te;
1663 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1667 err = got_object_open_as_tree(&tree, repo, tree_id);
1670 entries = got_object_tree_get_entries(tree);
1671 te = SIMPLEQ_FIRST(&entries->head);
1675 if (sigint_received || sigpipe_received)
1680 err = got_object_id_str(&id_str, te->id);
1683 if (asprintf(&id, "%s ", id_str) == -1) {
1684 err = got_error_from_errno("asprintf");
1690 print_entry(te, id, path, root_path);
1693 if (recurse && S_ISDIR(te->mode)) {
1695 if (asprintf(&child_path, "%s%s%s", path,
1696 path[0] == '/' && path[1] == '\0' ? "" : "/",
1698 err = got_error_from_errno("asprintf");
1701 err = print_tree(child_path, commit_id, show_ids, 1,
1708 te = SIMPLEQ_NEXT(te, entry);
1712 got_object_tree_close(tree);
1717 static const struct got_error *
1718 cmd_tree(int argc, char *argv[])
1720 const struct got_error *error;
1721 struct got_repository *repo = NULL;
1722 struct got_worktree *worktree = NULL;
1724 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1725 struct got_object_id *commit_id = NULL;
1726 char *commit_id_str = NULL;
1727 int show_ids = 0, recurse = 0;
1731 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1736 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1739 commit_id_str = optarg;
1742 repo_path = realpath(optarg, NULL);
1743 if (repo_path == NULL)
1744 err(1, "-r option");
1745 got_path_strip_trailing_slashes(repo_path);
1769 cwd = getcwd(NULL, 0);
1771 error = got_error_from_errno("getcwd");
1774 if (repo_path == NULL) {
1775 error = got_worktree_open(&worktree, cwd);
1776 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1782 strdup(got_worktree_get_repo_path(worktree));
1783 if (repo_path == NULL)
1784 error = got_error_from_errno("strdup");
1788 repo_path = strdup(cwd);
1789 if (repo_path == NULL) {
1790 error = got_error_from_errno("strdup");
1796 error = got_repo_open(&repo, repo_path);
1800 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1806 char *p, *worktree_subdir = cwd +
1807 strlen(got_worktree_get_root_path(worktree));
1808 if (asprintf(&p, "%s/%s",
1809 got_worktree_get_path_prefix(worktree),
1810 worktree_subdir) == -1) {
1811 error = got_error_from_errno("asprintf");
1814 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1821 if (in_repo_path == NULL) {
1822 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1827 if (commit_id_str == NULL) {
1828 struct got_reference *head_ref;
1829 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1832 error = got_ref_resolve(&commit_id, repo, head_ref);
1833 got_ref_close(head_ref);
1837 error = got_object_resolve_id_str(&commit_id, repo,
1843 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1844 in_repo_path, repo);
1851 got_worktree_close(worktree);
1853 const struct got_error *repo_error;
1854 repo_error = got_repo_close(repo);
1864 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1868 static const struct got_error *
1869 print_status(void *arg, unsigned char status, const char *path,
1870 struct got_object_id *blob_id, struct got_object_id *commit_id)
1872 printf("%c %s\n", status, path);
1876 static const struct got_error *
1877 cmd_status(int argc, char *argv[])
1879 const struct got_error *error = NULL;
1880 struct got_repository *repo = NULL;
1881 struct got_worktree *worktree = NULL;
1882 char *cwd = NULL, *path = NULL;
1885 while ((ch = getopt(argc, argv, "")) != -1) {
1897 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1901 cwd = getcwd(NULL, 0);
1903 error = got_error_from_errno("getcwd");
1907 error = got_worktree_open(&worktree, cwd);
1914 error = got_error_from_errno("strdup");
1917 } else if (argc == 1) {
1918 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1924 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1928 error = apply_unveil(got_repo_get_path(repo), 1,
1929 got_worktree_get_root_path(worktree), 0);
1933 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1934 check_cancelled, NULL);
1945 "usage: %s ref [-r repository] -l | -d name | name target\n",
1950 static const struct got_error *
1951 list_refs(struct got_repository *repo)
1953 static const struct got_error *err = NULL;
1954 struct got_reflist_head refs;
1955 struct got_reflist_entry *re;
1957 SIMPLEQ_INIT(&refs);
1958 err = got_ref_list(&refs, repo);
1962 SIMPLEQ_FOREACH(re, &refs, entry) {
1964 refstr = got_ref_to_str(re->ref);
1966 return got_error_from_errno("got_ref_to_str");
1967 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1971 got_ref_list_free(&refs);
1975 static const struct got_error *
1976 delete_ref(struct got_repository *repo, const char *refname)
1978 const struct got_error *err = NULL;
1979 struct got_reference *ref;
1981 err = got_ref_open(&ref, repo, refname, 0);
1985 err = got_ref_delete(ref, repo);
1990 static const struct got_error *
1991 add_ref(struct got_repository *repo, const char *refname, const char *target)
1993 const struct got_error *err = NULL;
1994 struct got_object_id *id;
1995 struct got_reference *ref = NULL;
1997 err = got_object_resolve_id_str(&id, repo, target);
1999 struct got_reference *target_ref;
2001 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2003 err = got_ref_open(&target_ref, repo, target, 0);
2006 err = got_ref_resolve(&id, repo, target_ref);
2007 got_ref_close(target_ref);
2012 err = got_ref_alloc(&ref, refname, id);
2016 err = got_ref_write(ref, repo);
2024 static const struct got_error *
2025 cmd_ref(int argc, char *argv[])
2027 const struct got_error *error = NULL;
2028 struct got_repository *repo = NULL;
2029 struct got_worktree *worktree = NULL;
2030 char *cwd = NULL, *repo_path = NULL;
2031 int ch, do_list = 0;
2032 const char *delref = NULL;
2034 /* TODO: Add -s option for adding symbolic references. */
2035 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2041 repo_path = realpath(optarg, NULL);
2042 if (repo_path == NULL)
2043 err(1, "-r option");
2044 got_path_strip_trailing_slashes(repo_path);
2055 if (do_list && delref)
2056 errx(1, "-l and -d options are mutually exclusive\n");
2061 if (do_list || delref) {
2064 } else if (argc != 2)
2069 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2073 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2074 "sendfd unveil", NULL) == -1)
2078 cwd = getcwd(NULL, 0);
2080 error = got_error_from_errno("getcwd");
2084 if (repo_path == NULL) {
2085 error = got_worktree_open(&worktree, cwd);
2086 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2092 strdup(got_worktree_get_repo_path(worktree));
2093 if (repo_path == NULL)
2094 error = got_error_from_errno("strdup");
2098 repo_path = strdup(cwd);
2099 if (repo_path == NULL) {
2100 error = got_error_from_errno("strdup");
2106 error = got_repo_open(&repo, repo_path);
2110 error = apply_unveil(got_repo_get_path(repo), do_list,
2111 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2116 error = list_refs(repo);
2118 error = delete_ref(repo, delref);
2120 error = add_ref(repo, argv[0], argv[1]);
2123 got_repo_close(repo);
2125 got_worktree_close(worktree);
2134 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2138 static const struct got_error *
2139 cmd_add(int argc, char *argv[])
2141 const struct got_error *error = NULL;
2142 struct got_repository *repo = NULL;
2143 struct got_worktree *worktree = NULL;
2145 struct got_pathlist_head paths;
2146 struct got_pathlist_entry *pe;
2151 while ((ch = getopt(argc, argv, "")) != -1) {
2165 /* make sure each file exists before doing anything halfway */
2166 for (x = 0; x < argc; x++) {
2167 char *path = realpath(argv[x], NULL);
2169 error = got_error_from_errno2("realpath", argv[x]);
2175 cwd = getcwd(NULL, 0);
2177 error = got_error_from_errno("getcwd");
2181 error = got_worktree_open(&worktree, cwd);
2185 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2189 error = apply_unveil(got_repo_get_path(repo), 1,
2190 got_worktree_get_root_path(worktree), 0);
2194 for (x = 0; x < argc; x++) {
2195 char *path = realpath(argv[x], NULL);
2197 error = got_error_from_errno2("realpath", argv[x]);
2201 got_path_strip_trailing_slashes(path);
2202 error = got_pathlist_insert(&pe, &paths, path, NULL);
2208 error = got_worktree_schedule_add(worktree, &paths, print_status,
2212 got_repo_close(repo);
2214 got_worktree_close(worktree);
2215 TAILQ_FOREACH(pe, &paths, entry)
2216 free((char *)pe->path);
2217 got_pathlist_free(&paths);
2225 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2229 static const struct got_error *
2230 cmd_rm(int argc, char *argv[])
2232 const struct got_error *error = NULL;
2233 struct got_worktree *worktree = NULL;
2234 struct got_repository *repo = NULL;
2236 struct got_pathlist_head paths;
2237 struct got_pathlist_entry *pe;
2238 int ch, i, delete_local_mods = 0;
2242 while ((ch = getopt(argc, argv, "f")) != -1) {
2245 delete_local_mods = 1;
2259 /* make sure each file exists before doing anything halfway */
2260 for (i = 0; i < argc; i++) {
2261 char *path = realpath(argv[i], NULL);
2263 error = got_error_from_errno2("realpath", argv[i]);
2269 cwd = getcwd(NULL, 0);
2271 error = got_error_from_errno("getcwd");
2274 error = got_worktree_open(&worktree, cwd);
2278 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2282 error = apply_unveil(got_repo_get_path(repo), 1,
2283 got_worktree_get_root_path(worktree), 0);
2287 for (i = 0; i < argc; i++) {
2288 char *path = realpath(argv[i], NULL);
2290 error = got_error_from_errno2("realpath", argv[i]);
2294 got_path_strip_trailing_slashes(path);
2295 error = got_pathlist_insert(&pe, &paths, path, NULL);
2301 error = got_worktree_schedule_delete(worktree, &paths,
2302 delete_local_mods, print_status, NULL, repo);
2307 got_repo_close(repo);
2309 got_worktree_close(worktree);
2310 TAILQ_FOREACH(pe, &paths, entry)
2311 free((char *)pe->path);
2312 got_pathlist_free(&paths);
2320 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2325 revert_progress(void *arg, unsigned char status, const char *path)
2327 while (path[0] == '/')
2329 printf("%c %s\n", status, path);
2332 static const struct got_error *
2333 cmd_revert(int argc, char *argv[])
2335 const struct got_error *error = NULL;
2336 struct got_worktree *worktree = NULL;
2337 struct got_repository *repo = NULL;
2338 char *cwd = NULL, *path = NULL;
2339 struct got_pathlist_head paths;
2340 struct got_pathlist_entry *pe;
2345 while ((ch = getopt(argc, argv, "")) != -1) {
2359 for (i = 0; i < argc; i++) {
2360 char *path = realpath(argv[i], NULL);
2362 error = got_error_from_errno2("realpath", argv[i]);
2366 got_path_strip_trailing_slashes(path);
2367 error = got_pathlist_insert(&pe, &paths, path, NULL);
2374 cwd = getcwd(NULL, 0);
2376 error = got_error_from_errno("getcwd");
2379 error = got_worktree_open(&worktree, cwd);
2383 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2387 error = apply_unveil(got_repo_get_path(repo), 1,
2388 got_worktree_get_root_path(worktree), 0);
2392 error = got_worktree_revert(worktree, &paths,
2393 revert_progress, NULL, repo);
2398 got_repo_close(repo);
2400 got_worktree_close(worktree);
2409 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2414 spawn_editor(const char *editor, const char *file)
2417 sig_t sighup, sigint, sigquit;
2420 sighup = signal(SIGHUP, SIG_IGN);
2421 sigint = signal(SIGINT, SIG_IGN);
2422 sigquit = signal(SIGQUIT, SIG_IGN);
2424 switch (pid = fork()) {
2428 execl(editor, editor, file, (char *)NULL);
2432 while (waitpid(pid, &st, 0) == -1)
2437 (void)signal(SIGHUP, sighup);
2438 (void)signal(SIGINT, sigint);
2439 (void)signal(SIGQUIT, sigquit);
2441 if (!WIFEXITED(st)) {
2446 return WEXITSTATUS(st);
2449 struct collect_commit_logmsg_arg {
2450 const char *cmdline_log;
2452 const char *worktree_path;
2453 const char *branch_name;
2454 const char *repo_path;
2459 static const struct got_error *
2460 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2463 char *initial_content = NULL;
2464 struct got_pathlist_entry *pe;
2465 const struct got_error *err = NULL;
2466 char *template = NULL;
2467 struct collect_commit_logmsg_arg *a = arg;
2469 struct stat st, st2;
2472 int fd, content_changed = 0;
2474 /* if a message was specified on the command line, just use it */
2475 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2476 len = strlen(a->cmdline_log) + 1;
2477 *logmsg = malloc(len + 1);
2478 if (*logmsg == NULL)
2479 return got_error_from_errno("malloc");
2480 strlcpy(*logmsg, a->cmdline_log, len);
2484 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2485 return got_error_from_errno("asprintf");
2487 if (asprintf(&initial_content,
2488 "\n# changes to be committed on branch %s:\n",
2489 a->branch_name) == -1)
2490 return got_error_from_errno("asprintf");
2492 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2496 dprintf(fd, initial_content);
2498 TAILQ_FOREACH(pe, commitable_paths, entry) {
2499 struct got_commitable *ct = pe->data;
2500 dprintf(fd, "# %c %s\n",
2501 got_commitable_get_status(ct),
2502 got_commitable_get_path(ct));
2506 if (stat(a->logmsg_path, &st) == -1) {
2507 err = got_error_from_errno2("stat", a->logmsg_path);
2511 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2512 err = got_error_from_errno("failed spawning editor");
2516 if (stat(a->logmsg_path, &st2) == -1) {
2517 err = got_error_from_errno("stat");
2521 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2522 unlink(a->logmsg_path);
2523 free(a->logmsg_path);
2524 a->logmsg_path = NULL;
2525 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2526 "no changes made to commit message, aborting");
2530 *logmsg = malloc(st2.st_size + 1);
2531 if (*logmsg == NULL) {
2532 err = got_error_from_errno("malloc");
2535 (*logmsg)[0] = '\0';
2538 fp = fopen(a->logmsg_path, "r");
2540 err = got_error_from_errno("fopen");
2543 while (fgets(buf, sizeof(buf), fp) != NULL) {
2544 if (!content_changed && strcmp(buf, initial_content) != 0)
2545 content_changed = 1;
2546 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2547 continue; /* remove comments and leading empty lines */
2548 len = strlcat(*logmsg, buf, st2.st_size);
2552 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2553 (*logmsg)[len - 1] = '\0';
2557 if (len == 0 || !content_changed) {
2558 unlink(a->logmsg_path);
2559 free(a->logmsg_path);
2560 a->logmsg_path = NULL;
2561 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2562 "commit message cannot be empty, aborting");
2566 free(initial_content);
2569 /* Editor is done; we can now apply unveil(2) */
2571 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2575 static const struct got_error *
2576 cmd_commit(int argc, char *argv[])
2578 const struct got_error *error = NULL;
2579 struct got_worktree *worktree = NULL;
2580 struct got_repository *repo = NULL;
2581 char *cwd = NULL, *path = NULL, *id_str = NULL;
2582 struct got_object_id *id = NULL;
2583 const char *logmsg = NULL;
2584 const char *got_author = getenv("GOT_AUTHOR");
2585 struct collect_commit_logmsg_arg cl_arg;
2586 char *editor = NULL;
2589 while ((ch = getopt(argc, argv, "m:")) != -1) {
2604 path = realpath(argv[0], NULL);
2606 error = got_error_from_errno2("realpath", argv[0]);
2609 got_path_strip_trailing_slashes(path);
2610 } else if (argc != 0)
2613 if (got_author == NULL) {
2614 /* TODO: Look current user up in password database */
2615 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2619 cwd = getcwd(NULL, 0);
2621 error = got_error_from_errno("getcwd");
2624 error = got_worktree_open(&worktree, cwd);
2628 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2633 * unveil(2) traverses exec(2); if an editor is used we have
2634 * to apply unveil after the log message has been written.
2636 if (logmsg == NULL || strlen(logmsg) == 0)
2637 error = get_editor(&editor);
2639 error = apply_unveil(got_repo_get_path(repo), 0,
2640 got_worktree_get_root_path(worktree), 0);
2644 cl_arg.editor = editor;
2645 cl_arg.cmdline_log = logmsg;
2646 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2647 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2648 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2649 cl_arg.branch_name += 5;
2650 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2651 cl_arg.branch_name += 6;
2652 cl_arg.repo_path = got_repo_get_path(repo);
2653 cl_arg.logmsg_path = NULL;
2654 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2655 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2657 if (cl_arg.logmsg_path)
2658 fprintf(stderr, "%s: log message preserved in %s\n",
2659 getprogname(), cl_arg.logmsg_path);
2663 if (cl_arg.logmsg_path)
2664 unlink(cl_arg.logmsg_path);
2666 error = got_object_id_str(&id_str, id);
2669 printf("Created commit %s\n", id_str);
2672 got_repo_close(repo);
2674 got_worktree_close(worktree);
2683 usage_cherrypick(void)
2685 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2689 static const struct got_error *
2690 cmd_cherrypick(int argc, char *argv[])
2692 const struct got_error *error = NULL;
2693 struct got_worktree *worktree = NULL;
2694 struct got_repository *repo = NULL;
2695 char *cwd = NULL, *commit_id_str = NULL;
2696 struct got_object_id *commit_id = NULL;
2697 struct got_commit_object *commit = NULL;
2698 struct got_object_qid *pid;
2699 struct got_reference *head_ref = NULL;
2700 int ch, did_something = 0;
2702 while ((ch = getopt(argc, argv, "")) != -1) {
2716 cwd = getcwd(NULL, 0);
2718 error = got_error_from_errno("getcwd");
2721 error = got_worktree_open(&worktree, cwd);
2725 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2729 error = apply_unveil(got_repo_get_path(repo), 0,
2730 got_worktree_get_root_path(worktree), 0);
2734 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2735 if (error != NULL) {
2736 struct got_reference *ref;
2737 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2739 error = got_ref_open(&ref, repo, argv[0], 0);
2742 error = got_ref_resolve(&commit_id, repo, ref);
2747 error = got_object_id_str(&commit_id_str, commit_id);
2751 error = got_ref_open(&head_ref, repo,
2752 got_worktree_get_head_ref_name(worktree), 0);
2756 error = check_same_branch(commit_id, head_ref, repo);
2758 if (error->code != GOT_ERR_ANCESTRY)
2762 error = got_error(GOT_ERR_SAME_BRANCH);
2766 error = got_object_open_as_commit(&commit, repo, commit_id);
2769 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2770 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2771 commit_id, repo, update_progress, &did_something, check_cancelled,
2777 printf("Merged commit %s\n", commit_id_str);
2780 got_object_commit_close(commit);
2781 free(commit_id_str);
2783 got_ref_close(head_ref);
2785 got_worktree_close(worktree);
2787 got_repo_close(repo);
2794 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2798 static const struct got_error *
2799 cmd_backout(int argc, char *argv[])
2801 const struct got_error *error = NULL;
2802 struct got_worktree *worktree = NULL;
2803 struct got_repository *repo = NULL;
2804 char *cwd = NULL, *commit_id_str = NULL;
2805 struct got_object_id *commit_id = NULL;
2806 struct got_commit_object *commit = NULL;
2807 struct got_object_qid *pid;
2808 struct got_reference *head_ref = NULL;
2809 int ch, did_something = 0;
2811 while ((ch = getopt(argc, argv, "")) != -1) {
2825 cwd = getcwd(NULL, 0);
2827 error = got_error_from_errno("getcwd");
2830 error = got_worktree_open(&worktree, cwd);
2834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2838 error = apply_unveil(got_repo_get_path(repo), 0,
2839 got_worktree_get_root_path(worktree), 0);
2843 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2844 if (error != NULL) {
2845 struct got_reference *ref;
2846 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2848 error = got_ref_open(&ref, repo, argv[0], 0);
2851 error = got_ref_resolve(&commit_id, repo, ref);
2856 error = got_object_id_str(&commit_id_str, commit_id);
2860 error = got_ref_open(&head_ref, repo,
2861 got_worktree_get_head_ref_name(worktree), 0);
2865 error = check_same_branch(commit_id, head_ref, repo);
2869 error = got_object_open_as_commit(&commit, repo, commit_id);
2872 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2874 error = got_error(GOT_ERR_ROOT_COMMIT);
2878 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2879 update_progress, &did_something, check_cancelled, NULL);
2884 printf("Backed out commit %s\n", commit_id_str);
2887 got_object_commit_close(commit);
2888 free(commit_id_str);
2890 got_ref_close(head_ref);
2892 got_worktree_close(worktree);
2894 got_repo_close(repo);