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);
75 __dead static void usage(void);
76 __dead static void usage_init(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_init(int, char *[]);
93 static const struct got_error* cmd_checkout(int, char *[]);
94 static const struct got_error* cmd_update(int, char *[]);
95 static const struct got_error* cmd_log(int, char *[]);
96 static const struct got_error* cmd_diff(int, char *[]);
97 static const struct got_error* cmd_blame(int, char *[]);
98 static const struct got_error* cmd_tree(int, char *[]);
99 static const struct got_error* cmd_status(int, char *[]);
100 static const struct got_error* cmd_ref(int, char *[]);
101 static const struct got_error* cmd_add(int, char *[]);
102 static const struct got_error* cmd_rm(int, char *[]);
103 static const struct got_error* cmd_revert(int, char *[]);
104 static const struct got_error* cmd_commit(int, char *[]);
105 static const struct got_error* cmd_cherrypick(int, char *[]);
106 static const struct got_error* cmd_backout(int, char *[]);
108 static struct cmd got_commands[] = {
109 { "init", cmd_init, usage_init },
110 { "checkout", cmd_checkout, usage_checkout },
111 { "update", cmd_update, usage_update },
112 { "log", cmd_log, usage_log },
113 { "diff", cmd_diff, usage_diff },
114 { "blame", cmd_blame, usage_blame },
115 { "tree", cmd_tree, usage_tree },
116 { "status", cmd_status, usage_status },
117 { "ref", cmd_ref, usage_ref },
118 { "add", cmd_add, usage_add },
119 { "rm", cmd_rm, usage_rm },
120 { "revert", cmd_revert, usage_revert },
121 { "commit", cmd_commit, usage_commit },
122 { "cherrypick", cmd_cherrypick, usage_cherrypick },
123 { "backout", cmd_backout, usage_backout },
127 main(int argc, char *argv[])
134 setlocale(LC_CTYPE, "");
136 while ((ch = getopt(argc, argv, "h")) != -1) {
154 signal(SIGINT, catch_sigint);
155 signal(SIGPIPE, catch_sigpipe);
157 for (i = 0; i < nitems(got_commands); i++) {
158 const struct got_error *error;
160 cmd = &got_commands[i];
162 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
166 got_commands[i].cmd_usage();
168 error = got_commands[i].cmd_main(argc, argv);
169 if (error && !(sigint_received || sigpipe_received)) {
170 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
177 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
184 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
188 static const struct got_error *
189 get_editor(char **abspath)
191 const struct got_error *err = NULL;
194 editor = getenv("VISUAL");
196 editor = getenv("EDITOR");
199 err = got_path_find_prog(abspath, editor);
204 if (*abspath == NULL) {
205 *abspath = strdup("/bin/ed");
206 if (*abspath == NULL)
207 return got_error_from_errno("strdup");
213 static const struct got_error *
214 apply_unveil(const char *repo_path, int repo_read_only,
215 const char *worktree_path, int create_worktree)
217 const struct got_error *err;
219 if (create_worktree) {
220 /* Pre-create work tree path to avoid unveiling its parents. */
221 err = got_path_mkdir(worktree_path);
223 if (errno == EEXIST) {
224 if (got_path_dir_is_empty(worktree_path)) {
228 err = got_error_path(worktree_path,
229 GOT_ERR_DIR_NOT_EMPTY);
233 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
237 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
238 return got_error_from_errno2("unveil", repo_path);
240 if (worktree_path && unveil(worktree_path, "rwc") != 0)
241 return got_error_from_errno2("unveil", worktree_path);
243 if (unveil("/tmp", "rwc") != 0)
244 return got_error_from_errno2("unveil", "/tmp");
246 err = got_privsep_unveil_exec_helpers();
250 if (unveil(NULL, NULL) != 0)
251 return got_error_from_errno("unveil");
259 fprintf(stderr, "usage: %s init path\n", getprogname());
263 static const struct got_error *
264 cmd_init(int argc, char *argv[])
266 const struct got_error *error = NULL;
267 char *repo_path = NULL;
270 while ((ch = getopt(argc, argv, "")) != -1) {
282 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
288 repo_path = strdup(argv[0]);
289 if (repo_path == NULL)
290 return got_error_from_errno("strdup");
292 got_path_strip_trailing_slashes(repo_path);
294 error = got_path_mkdir(repo_path);
296 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
299 error = apply_unveil(repo_path, 0, NULL, 0);
303 error = got_repo_init(repo_path);
315 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
316 "[-p prefix] repository-path [worktree-path]\n", getprogname());
321 checkout_progress(void *arg, unsigned char status, const char *path)
323 char *worktree_path = arg;
325 /* Base commit bump happens silently. */
326 if (status == GOT_STATUS_BUMP_BASE)
329 while (path[0] == '/')
332 printf("%c %s/%s\n", status, worktree_path, path);
335 static const struct got_error *
336 check_cancelled(void *arg)
338 if (sigint_received || sigpipe_received)
339 return got_error(GOT_ERR_CANCELLED);
343 static const struct got_error *
344 check_linear_ancestry(struct got_object_id *commit_id,
345 struct got_object_id *base_commit_id, struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_object_id *yca_id;
350 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
351 commit_id, base_commit_id, repo);
356 return got_error(GOT_ERR_ANCESTRY);
359 * Require a straight line of history between the target commit
360 * and the work tree's base commit.
362 * Non-linear situations such as this require a rebase:
364 * (commit) D F (base_commit)
372 * 'got update' only handles linear cases:
373 * Update forwards in time: A (base/yca) - B - C - D (commit)
374 * Update backwards in time: D (base) - C - B - A (commit/yca)
376 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
377 got_object_id_cmp(base_commit_id, yca_id) != 0)
378 return got_error(GOT_ERR_ANCESTRY);
384 static const struct got_error *
385 check_same_branch(struct got_object_id *commit_id,
386 struct got_reference *head_ref, struct got_repository *repo)
388 const struct got_error *err = NULL;
389 struct got_commit_graph *graph = NULL;
390 struct got_object_id *head_commit_id = NULL;
391 int is_same_branch = 0;
393 err = got_ref_resolve(&head_commit_id, repo, head_ref);
397 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
401 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
406 struct got_object_id *id;
407 err = got_commit_graph_iter_next(&id, graph);
409 if (err->code == GOT_ERR_ITER_COMPLETED) {
413 else if (err->code != GOT_ERR_ITER_NEED_MORE)
415 err = got_commit_graph_fetch_commits(graph, 1,
422 if (got_object_id_cmp(id, commit_id) == 0) {
430 got_commit_graph_close(graph);
431 free(head_commit_id);
432 if (!err && !is_same_branch)
433 err = got_error(GOT_ERR_ANCESTRY);
437 static const struct got_error *
438 cmd_checkout(int argc, char *argv[])
440 const struct got_error *error = NULL;
441 struct got_repository *repo = NULL;
442 struct got_reference *head_ref = NULL;
443 struct got_worktree *worktree = NULL;
444 char *repo_path = NULL;
445 char *worktree_path = NULL;
446 const char *path_prefix = "";
447 const char *branch_name = GOT_REF_HEAD;
448 char *commit_id_str = NULL;
449 int ch, same_path_prefix;
451 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
454 branch_name = optarg;
457 commit_id_str = strdup(optarg);
458 if (commit_id_str == NULL)
459 return got_error_from_errno("strdup");
462 path_prefix = optarg;
474 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
475 "unveil", NULL) == -1)
479 char *cwd, *base, *dotgit;
480 repo_path = realpath(argv[0], NULL);
481 if (repo_path == NULL)
482 return got_error_from_errno2("realpath", argv[0]);
483 cwd = getcwd(NULL, 0);
485 error = got_error_from_errno("getcwd");
488 if (path_prefix[0]) {
489 base = basename(path_prefix);
491 error = got_error_from_errno2("basename",
496 base = basename(repo_path);
498 error = got_error_from_errno2("basename",
503 dotgit = strstr(base, ".git");
506 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
507 error = got_error_from_errno("asprintf");
512 } else if (argc == 2) {
513 repo_path = realpath(argv[0], NULL);
514 if (repo_path == NULL) {
515 error = got_error_from_errno2("realpath", argv[0]);
518 worktree_path = realpath(argv[1], NULL);
519 if (worktree_path == NULL) {
520 error = got_error_from_errno2("realpath", argv[1]);
526 got_path_strip_trailing_slashes(repo_path);
527 got_path_strip_trailing_slashes(worktree_path);
529 error = got_repo_open(&repo, repo_path);
533 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
537 error = got_ref_open(&head_ref, repo, branch_name, 0);
541 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
542 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
545 error = got_worktree_open(&worktree, worktree_path);
549 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
553 if (!same_path_prefix) {
554 error = got_error(GOT_ERR_PATH_PREFIX);
559 struct got_object_id *commit_id;
560 error = got_object_resolve_id_str(&commit_id, repo,
564 error = check_linear_ancestry(commit_id,
565 got_worktree_get_base_commit_id(worktree), repo);
570 error = check_same_branch(commit_id, head_ref, repo);
573 error = got_worktree_set_base_commit_id(worktree, repo,
580 error = got_worktree_checkout_files(worktree, "", repo,
581 checkout_progress, worktree_path, check_cancelled, NULL);
585 printf("Now shut up and hack\n");
597 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
603 update_progress(void *arg, unsigned char status, const char *path)
605 int *did_something = arg;
607 if (status == GOT_STATUS_EXISTS)
612 /* Base commit bump happens silently. */
613 if (status == GOT_STATUS_BUMP_BASE)
616 while (path[0] == '/')
618 printf("%c %s\n", status, path);
621 static const struct got_error *
622 switch_head_ref(struct got_reference *head_ref,
623 struct got_object_id *commit_id, struct got_worktree *worktree,
624 struct got_repository *repo)
626 const struct got_error *err = NULL;
628 int ref_has_moved = 0;
630 /* Trivial case: switching between two different references. */
631 if (strcmp(got_ref_get_name(head_ref),
632 got_worktree_get_head_ref_name(worktree)) != 0) {
633 printf("Switching work tree from %s to %s\n",
634 got_worktree_get_head_ref_name(worktree),
635 got_ref_get_name(head_ref));
636 return got_worktree_set_head_ref(worktree, head_ref);
639 err = check_linear_ancestry(commit_id,
640 got_worktree_get_base_commit_id(worktree), repo);
642 if (err->code != GOT_ERR_ANCESTRY)
649 /* Switching to a rebased branch with the same reference name. */
650 err = got_object_id_str(&base_id_str,
651 got_worktree_get_base_commit_id(worktree));
654 printf("Reference %s now points at a different branch\n",
655 got_worktree_get_head_ref_name(worktree));
656 printf("Switching work tree from %s to %s\n", base_id_str,
657 got_worktree_get_head_ref_name(worktree));
661 static const struct got_error *
662 cmd_update(int argc, char *argv[])
664 const struct got_error *error = NULL;
665 struct got_repository *repo = NULL;
666 struct got_worktree *worktree = NULL;
667 char *worktree_path = NULL, *path = NULL;
668 struct got_object_id *commit_id = NULL;
669 char *commit_id_str = NULL;
670 const char *branch_name = NULL;
671 struct got_reference *head_ref = NULL;
672 int ch, did_something = 0;
674 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
677 branch_name = optarg;
680 commit_id_str = strdup(optarg);
681 if (commit_id_str == NULL)
682 return got_error_from_errno("strdup");
694 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
695 "unveil", NULL) == -1)
698 worktree_path = getcwd(NULL, 0);
699 if (worktree_path == NULL) {
700 error = got_error_from_errno("getcwd");
703 error = got_worktree_open(&worktree, worktree_path);
710 error = got_error_from_errno("strdup");
713 } else if (argc == 1) {
714 error = got_worktree_resolve_path(&path, worktree, argv[0]);
720 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
724 error = apply_unveil(got_repo_get_path(repo), 0,
725 got_worktree_get_root_path(worktree), 0);
729 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
730 got_worktree_get_head_ref_name(worktree), 0);
733 if (commit_id_str == NULL) {
734 error = got_ref_resolve(&commit_id, repo, head_ref);
737 error = got_object_id_str(&commit_id_str, commit_id);
741 error = got_object_resolve_id_str(&commit_id, repo,
748 struct got_object_id *head_commit_id;
749 if (strlen(path) != 0) {
750 fprintf(stderr, "%s: switching between branches "
751 "requires that the entire work tree "
752 "gets updated, not just '%s'\n",
753 getprogname(), path);
754 error = got_error(GOT_ERR_BAD_PATH);
757 error = got_ref_resolve(&head_commit_id, repo, head_ref);
760 error = check_linear_ancestry(commit_id, head_commit_id, repo);
761 free(head_commit_id);
764 error = check_same_branch(commit_id, head_ref, repo);
767 error = switch_head_ref(head_ref, commit_id, worktree, repo);
771 error = check_linear_ancestry(commit_id,
772 got_worktree_get_base_commit_id(worktree), repo);
774 if (error->code == GOT_ERR_ANCESTRY)
775 error = got_error(GOT_ERR_BRANCH_MOVED);
778 error = check_same_branch(commit_id, head_ref, repo);
783 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
785 error = got_worktree_set_base_commit_id(worktree, repo,
791 error = got_worktree_checkout_files(worktree, path, repo,
792 update_progress, &did_something, check_cancelled, NULL);
797 printf("Updated to commit %s\n", commit_id_str);
799 printf("Already up-to-date\n");
808 static const struct got_error *
809 print_patch(struct got_commit_object *commit, struct got_object_id *id,
810 int diff_context, struct got_repository *repo)
812 const struct got_error *err = NULL;
813 struct got_tree_object *tree1 = NULL, *tree2;
814 struct got_object_qid *qid;
815 char *id_str1 = NULL, *id_str2;
816 struct got_diff_blob_output_unidiff_arg arg;
818 err = got_object_open_as_tree(&tree2, repo,
819 got_object_commit_get_tree_id(commit));
823 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
825 struct got_commit_object *pcommit;
827 err = got_object_open_as_commit(&pcommit, repo, qid->id);
831 err = got_object_open_as_tree(&tree1, repo,
832 got_object_commit_get_tree_id(pcommit));
833 got_object_commit_close(pcommit);
837 err = got_object_id_str(&id_str1, qid->id);
842 err = got_object_id_str(&id_str2, id);
846 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
847 arg.diff_context = diff_context;
848 arg.outfile = stdout;
849 err = got_diff_tree(tree1, tree2, "", "", repo,
850 got_diff_blob_output_unidiff, &arg);
853 got_object_tree_close(tree1);
854 got_object_tree_close(tree2);
861 get_datestr(time_t *time, char *datebuf)
863 char *p, *s = ctime_r(time, datebuf);
870 static const struct got_error *
871 print_commit(struct got_commit_object *commit, struct got_object_id *id,
872 struct got_repository *repo, int show_patch, int diff_context,
873 struct got_reflist_head *refs)
875 const struct got_error *err = NULL;
876 char *id_str, *datestr, *logmsg0, *logmsg, *line;
878 time_t committer_time;
879 const char *author, *committer;
880 char *refs_str = NULL;
881 struct got_reflist_entry *re;
883 SIMPLEQ_FOREACH(re, refs, entry) {
886 if (got_object_id_cmp(re->id, id) != 0)
888 name = got_ref_get_name(re->ref);
889 if (strcmp(name, GOT_REF_HEAD) == 0)
891 if (strncmp(name, "refs/", 5) == 0)
893 if (strncmp(name, "got/", 4) == 0)
895 if (strncmp(name, "heads/", 6) == 0)
897 if (strncmp(name, "remotes/", 8) == 0)
900 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
902 err = got_error_from_errno("asprintf");
908 err = got_object_id_str(&id_str, id);
912 printf("-----------------------------------------------\n");
913 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
914 refs_str ? refs_str : "", refs_str ? ")" : "");
919 printf("from: %s\n", got_object_commit_get_author(commit));
920 committer_time = got_object_commit_get_committer_time(commit);
921 datestr = get_datestr(&committer_time, datebuf);
922 printf("date: %s UTC\n", datestr);
923 author = got_object_commit_get_author(commit);
924 committer = got_object_commit_get_committer(commit);
925 if (strcmp(author, committer) != 0)
926 printf("via: %s\n", committer);
927 if (got_object_commit_get_nparents(commit) > 1) {
928 const struct got_object_id_queue *parent_ids;
929 struct got_object_qid *qid;
931 parent_ids = got_object_commit_get_parent_ids(commit);
932 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
933 err = got_object_id_str(&id_str, qid->id);
936 printf("parent %d: %s\n", n++, id_str);
941 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
943 return got_error_from_errno("strdup");
947 line = strsep(&logmsg, "\n");
949 printf(" %s\n", line);
954 err = print_patch(commit, id, diff_context, repo);
959 if (fflush(stdout) != 0 && err == NULL)
960 err = got_error_from_errno("fflush");
964 static const struct got_error *
965 print_commits(struct got_object_id *root_id, struct got_repository *repo,
966 char *path, int show_patch, int diff_context, int limit,
967 int first_parent_traversal, struct got_reflist_head *refs)
969 const struct got_error *err;
970 struct got_commit_graph *graph;
972 err = got_commit_graph_open(&graph, root_id, path,
973 first_parent_traversal, repo);
976 err = got_commit_graph_iter_start(graph, root_id, repo);
980 struct got_commit_object *commit;
981 struct got_object_id *id;
983 if (sigint_received || sigpipe_received)
986 err = got_commit_graph_iter_next(&id, graph);
988 if (err->code == GOT_ERR_ITER_COMPLETED) {
992 if (err->code != GOT_ERR_ITER_NEED_MORE)
994 err = got_commit_graph_fetch_commits(graph, 1, repo);
1003 err = got_object_open_as_commit(&commit, repo, id);
1006 err = print_commit(commit, id, repo, show_patch, diff_context,
1008 got_object_commit_close(commit);
1009 if (err || (limit && --limit == 0))
1013 got_commit_graph_close(graph);
1020 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1021 "[-r repository-path] [path]\n", getprogname());
1025 static const struct got_error *
1026 cmd_log(int argc, char *argv[])
1028 const struct got_error *error;
1029 struct got_repository *repo = NULL;
1030 struct got_worktree *worktree = NULL;
1031 struct got_commit_object *commit = NULL;
1032 struct got_object_id *id = NULL;
1033 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1034 char *start_commit = NULL;
1035 int diff_context = 3, ch;
1036 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1038 struct got_reflist_head refs;
1040 SIMPLEQ_INIT(&refs);
1043 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1049 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1052 first_parent_traversal = 1;
1058 start_commit = optarg;
1061 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1064 err(1, "-C option %s", errstr);
1067 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1069 err(1, "-l option %s", errstr);
1072 repo_path = realpath(optarg, NULL);
1073 if (repo_path == NULL)
1074 err(1, "-r option");
1075 got_path_strip_trailing_slashes(repo_path);
1086 cwd = getcwd(NULL, 0);
1088 error = got_error_from_errno("getcwd");
1092 error = got_worktree_open(&worktree, cwd);
1093 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1100 error = got_error_from_errno("strdup");
1103 } else if (argc == 1) {
1105 error = got_worktree_resolve_path(&path, worktree,
1110 path = strdup(argv[0]);
1112 error = got_error_from_errno("strdup");
1119 if (repo_path == NULL) {
1120 repo_path = worktree ?
1121 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1123 if (repo_path == NULL) {
1124 error = got_error_from_errno("strdup");
1128 error = got_repo_open(&repo, repo_path);
1132 error = apply_unveil(got_repo_get_path(repo), 1,
1133 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1137 if (start_commit == NULL) {
1138 struct got_reference *head_ref;
1139 error = got_ref_open(&head_ref, repo,
1140 worktree ? got_worktree_get_head_ref_name(worktree)
1144 error = got_ref_resolve(&id, repo, head_ref);
1145 got_ref_close(head_ref);
1148 error = got_object_open_as_commit(&commit, repo, id);
1150 struct got_reference *ref;
1151 error = got_ref_open(&ref, repo, start_commit, 0);
1152 if (error == NULL) {
1154 error = got_ref_resolve(&id, repo, ref);
1158 error = got_object_get_type(&obj_type, repo, id);
1161 if (obj_type == GOT_OBJ_TYPE_TAG) {
1162 struct got_tag_object *tag;
1163 error = got_object_open_as_tag(&tag, repo, id);
1166 if (got_object_tag_get_object_type(tag) !=
1167 GOT_OBJ_TYPE_COMMIT) {
1168 got_object_tag_close(tag);
1169 error = got_error(GOT_ERR_OBJ_TYPE);
1173 id = got_object_id_dup(
1174 got_object_tag_get_object_id(tag));
1176 error = got_error_from_errno(
1177 "got_object_id_dup");
1178 got_object_tag_close(tag);
1181 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1182 error = got_error(GOT_ERR_OBJ_TYPE);
1185 error = got_object_open_as_commit(&commit, repo, id);
1189 if (commit == NULL) {
1190 error = got_object_resolve_id_str(&id, repo,
1199 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1204 path = in_repo_path;
1207 error = got_ref_list(&refs, repo);
1211 error = print_commits(id, repo, path, show_patch,
1212 diff_context, limit, first_parent_traversal, &refs);
1219 got_worktree_close(worktree);
1221 const struct got_error *repo_error;
1222 repo_error = got_repo_close(repo);
1226 got_ref_list_free(&refs);
1233 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1234 "[object1 object2 | path]\n", getprogname());
1238 struct print_diff_arg {
1239 struct got_repository *repo;
1240 struct got_worktree *worktree;
1246 static const struct got_error *
1247 print_diff(void *arg, unsigned char status, const char *path,
1248 struct got_object_id *blob_id, struct got_object_id *commit_id)
1250 struct print_diff_arg *a = arg;
1251 const struct got_error *err = NULL;
1252 struct got_blob_object *blob1 = NULL;
1254 char *abspath = NULL;
1257 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1258 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1261 if (!a->header_shown) {
1262 printf("diff %s %s\n", a->id_str,
1263 got_worktree_get_root_path(a->worktree));
1264 a->header_shown = 1;
1267 if (status != GOT_STATUS_ADD) {
1268 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1274 if (status != GOT_STATUS_DELETE) {
1275 if (asprintf(&abspath, "%s/%s",
1276 got_worktree_get_root_path(a->worktree), path) == -1) {
1277 err = got_error_from_errno("asprintf");
1281 f2 = fopen(abspath, "r");
1283 err = got_error_from_errno2("fopen", abspath);
1286 if (lstat(abspath, &sb) == -1) {
1287 err = got_error_from_errno2("lstat", abspath);
1293 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1297 got_object_blob_close(blob1);
1298 if (f2 && fclose(f2) != 0 && err == NULL)
1299 err = got_error_from_errno("fclose");
1304 static const struct got_error *
1305 cmd_diff(int argc, char *argv[])
1307 const struct got_error *error;
1308 struct got_repository *repo = NULL;
1309 struct got_worktree *worktree = NULL;
1310 char *cwd = NULL, *repo_path = NULL;
1311 struct got_object_id *id1 = NULL, *id2 = NULL;
1312 const char *id_str1 = NULL, *id_str2 = NULL;
1313 char *label1 = NULL, *label2 = NULL;
1315 int diff_context = 3, ch;
1320 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1325 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1328 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1330 err(1, "-C option %s", errstr);
1333 repo_path = realpath(optarg, NULL);
1334 if (repo_path == NULL)
1335 err(1, "-r option");
1336 got_path_strip_trailing_slashes(repo_path);
1347 cwd = getcwd(NULL, 0);
1349 error = got_error_from_errno("getcwd");
1352 error = got_worktree_open(&worktree, cwd);
1353 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1356 if (worktree == NULL) {
1357 error = got_error(GOT_ERR_NOT_WORKTREE);
1362 "-r option can't be used when diffing a work tree");
1363 repo_path = strdup(got_worktree_get_repo_path(worktree));
1364 if (repo_path == NULL) {
1365 error = got_error_from_errno("strdup");
1369 error = got_worktree_resolve_path(&path, worktree,
1376 error = got_error_from_errno("strdup");
1380 } else if (argc == 2) {
1383 if (worktree && repo_path == NULL) {
1385 strdup(got_worktree_get_repo_path(worktree));
1386 if (repo_path == NULL) {
1387 error = got_error_from_errno("strdup");
1394 if (repo_path == NULL) {
1395 repo_path = getcwd(NULL, 0);
1396 if (repo_path == NULL)
1397 return got_error_from_errno("getcwd");
1400 error = got_repo_open(&repo, repo_path);
1405 error = apply_unveil(got_repo_get_path(repo), 1,
1406 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1411 struct print_diff_arg arg;
1413 error = got_object_id_str(&id_str,
1414 got_worktree_get_base_commit_id(worktree));
1418 arg.worktree = worktree;
1419 arg.diff_context = diff_context;
1420 arg.id_str = id_str;
1421 arg.header_shown = 0;
1423 error = got_worktree_status(worktree, path, repo, print_diff,
1424 &arg, check_cancelled, NULL);
1429 error = got_object_resolve_id_str(&id1, repo, id_str1);
1431 struct got_reference *ref;
1432 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1434 error = got_ref_open(&ref, repo, id_str1, 0);
1437 label1 = strdup(got_ref_get_name(ref));
1438 if (label1 == NULL) {
1439 error = got_error_from_errno("strdup");
1442 error = got_ref_resolve(&id1, repo, ref);
1447 label1 = strdup(id_str1);
1448 if (label1 == NULL) {
1449 error = got_error_from_errno("strdup");
1454 error = got_object_resolve_id_str(&id2, repo, id_str2);
1456 struct got_reference *ref;
1457 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1459 error = got_ref_open(&ref, repo, id_str2, 0);
1462 label2 = strdup(got_ref_get_name(ref));
1463 if (label2 == NULL) {
1464 error = got_error_from_errno("strdup");
1467 error = got_ref_resolve(&id2, repo, ref);
1472 label2 = strdup(id_str2);
1473 if (label2 == NULL) {
1474 error = got_error_from_errno("strdup");
1479 error = got_object_get_type(&type1, repo, id1);
1483 error = got_object_get_type(&type2, repo, id2);
1487 if (type1 != type2) {
1488 error = got_error(GOT_ERR_OBJ_TYPE);
1493 case GOT_OBJ_TYPE_BLOB:
1494 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1495 diff_context, repo, stdout);
1497 case GOT_OBJ_TYPE_TREE:
1498 error = got_diff_objects_as_trees(id1, id2, "", "",
1499 diff_context, repo, stdout);
1501 case GOT_OBJ_TYPE_COMMIT:
1502 printf("diff %s %s\n", label1, label2);
1503 error = got_diff_objects_as_commits(id1, id2, diff_context,
1507 error = got_error(GOT_ERR_OBJ_TYPE);
1517 got_worktree_close(worktree);
1519 const struct got_error *repo_error;
1520 repo_error = got_repo_close(repo);
1531 "usage: %s blame [-c commit] [-r repository-path] path\n",
1536 static const struct got_error *
1537 cmd_blame(int argc, char *argv[])
1539 const struct got_error *error;
1540 struct got_repository *repo = NULL;
1541 struct got_worktree *worktree = NULL;
1542 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1543 struct got_object_id *commit_id = NULL;
1544 char *commit_id_str = NULL;
1548 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1553 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1556 commit_id_str = optarg;
1559 repo_path = realpath(optarg, NULL);
1560 if (repo_path == NULL)
1561 err(1, "-r option");
1562 got_path_strip_trailing_slashes(repo_path);
1578 cwd = getcwd(NULL, 0);
1580 error = got_error_from_errno("getcwd");
1583 if (repo_path == NULL) {
1584 error = got_worktree_open(&worktree, cwd);
1585 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1591 strdup(got_worktree_get_repo_path(worktree));
1592 if (repo_path == NULL)
1593 error = got_error_from_errno("strdup");
1597 repo_path = strdup(cwd);
1598 if (repo_path == NULL) {
1599 error = got_error_from_errno("strdup");
1605 error = got_repo_open(&repo, repo_path);
1609 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1614 const char *prefix = got_worktree_get_path_prefix(worktree);
1615 char *p, *worktree_subdir = cwd +
1616 strlen(got_worktree_get_root_path(worktree));
1617 if (asprintf(&p, "%s%s%s%s%s",
1618 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1619 worktree_subdir, worktree_subdir[0] ? "/" : "",
1621 error = got_error_from_errno("asprintf");
1624 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1632 if (commit_id_str == NULL) {
1633 struct got_reference *head_ref;
1634 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1637 error = got_ref_resolve(&commit_id, repo, head_ref);
1638 got_ref_close(head_ref);
1642 error = got_object_resolve_id_str(&commit_id, repo,
1648 error = got_blame(in_repo_path, commit_id, repo, stdout);
1655 got_worktree_close(worktree);
1657 const struct got_error *repo_error;
1658 repo_error = got_repo_close(repo);
1669 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1675 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1676 const char *root_path)
1678 int is_root_path = (strcmp(path, root_path) == 0);
1680 path += strlen(root_path);
1681 while (path[0] == '/')
1684 printf("%s%s%s%s%s\n", id ? id : "", path,
1685 is_root_path ? "" : "/", te->name,
1686 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1689 static const struct got_error *
1690 print_tree(const char *path, struct got_object_id *commit_id,
1691 int show_ids, int recurse, const char *root_path,
1692 struct got_repository *repo)
1694 const struct got_error *err = NULL;
1695 struct got_object_id *tree_id = NULL;
1696 struct got_tree_object *tree = NULL;
1697 const struct got_tree_entries *entries;
1698 struct got_tree_entry *te;
1700 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1704 err = got_object_open_as_tree(&tree, repo, tree_id);
1707 entries = got_object_tree_get_entries(tree);
1708 te = SIMPLEQ_FIRST(&entries->head);
1712 if (sigint_received || sigpipe_received)
1717 err = got_object_id_str(&id_str, te->id);
1720 if (asprintf(&id, "%s ", id_str) == -1) {
1721 err = got_error_from_errno("asprintf");
1727 print_entry(te, id, path, root_path);
1730 if (recurse && S_ISDIR(te->mode)) {
1732 if (asprintf(&child_path, "%s%s%s", path,
1733 path[0] == '/' && path[1] == '\0' ? "" : "/",
1735 err = got_error_from_errno("asprintf");
1738 err = print_tree(child_path, commit_id, show_ids, 1,
1745 te = SIMPLEQ_NEXT(te, entry);
1749 got_object_tree_close(tree);
1754 static const struct got_error *
1755 cmd_tree(int argc, char *argv[])
1757 const struct got_error *error;
1758 struct got_repository *repo = NULL;
1759 struct got_worktree *worktree = NULL;
1761 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1762 struct got_object_id *commit_id = NULL;
1763 char *commit_id_str = NULL;
1764 int show_ids = 0, recurse = 0;
1768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1773 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1776 commit_id_str = optarg;
1779 repo_path = realpath(optarg, NULL);
1780 if (repo_path == NULL)
1781 err(1, "-r option");
1782 got_path_strip_trailing_slashes(repo_path);
1806 cwd = getcwd(NULL, 0);
1808 error = got_error_from_errno("getcwd");
1811 if (repo_path == NULL) {
1812 error = got_worktree_open(&worktree, cwd);
1813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1819 strdup(got_worktree_get_repo_path(worktree));
1820 if (repo_path == NULL)
1821 error = got_error_from_errno("strdup");
1825 repo_path = strdup(cwd);
1826 if (repo_path == NULL) {
1827 error = got_error_from_errno("strdup");
1833 error = got_repo_open(&repo, repo_path);
1837 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1843 char *p, *worktree_subdir = cwd +
1844 strlen(got_worktree_get_root_path(worktree));
1845 if (asprintf(&p, "%s/%s",
1846 got_worktree_get_path_prefix(worktree),
1847 worktree_subdir) == -1) {
1848 error = got_error_from_errno("asprintf");
1851 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1858 if (in_repo_path == NULL) {
1859 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1864 if (commit_id_str == NULL) {
1865 struct got_reference *head_ref;
1866 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1869 error = got_ref_resolve(&commit_id, repo, head_ref);
1870 got_ref_close(head_ref);
1874 error = got_object_resolve_id_str(&commit_id, repo,
1880 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1881 in_repo_path, repo);
1888 got_worktree_close(worktree);
1890 const struct got_error *repo_error;
1891 repo_error = got_repo_close(repo);
1901 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1905 static const struct got_error *
1906 print_status(void *arg, unsigned char status, const char *path,
1907 struct got_object_id *blob_id, struct got_object_id *commit_id)
1909 printf("%c %s\n", status, path);
1913 static const struct got_error *
1914 cmd_status(int argc, char *argv[])
1916 const struct got_error *error = NULL;
1917 struct got_repository *repo = NULL;
1918 struct got_worktree *worktree = NULL;
1919 char *cwd = NULL, *path = NULL;
1922 while ((ch = getopt(argc, argv, "")) != -1) {
1934 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1938 cwd = getcwd(NULL, 0);
1940 error = got_error_from_errno("getcwd");
1944 error = got_worktree_open(&worktree, cwd);
1951 error = got_error_from_errno("strdup");
1954 } else if (argc == 1) {
1955 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1961 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1965 error = apply_unveil(got_repo_get_path(repo), 1,
1966 got_worktree_get_root_path(worktree), 0);
1970 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1971 check_cancelled, NULL);
1982 "usage: %s ref [-r repository] -l | -d name | name target\n",
1987 static const struct got_error *
1988 list_refs(struct got_repository *repo)
1990 static const struct got_error *err = NULL;
1991 struct got_reflist_head refs;
1992 struct got_reflist_entry *re;
1994 SIMPLEQ_INIT(&refs);
1995 err = got_ref_list(&refs, repo);
1999 SIMPLEQ_FOREACH(re, &refs, entry) {
2001 refstr = got_ref_to_str(re->ref);
2003 return got_error_from_errno("got_ref_to_str");
2004 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2008 got_ref_list_free(&refs);
2012 static const struct got_error *
2013 delete_ref(struct got_repository *repo, const char *refname)
2015 const struct got_error *err = NULL;
2016 struct got_reference *ref;
2018 err = got_ref_open(&ref, repo, refname, 0);
2022 err = got_ref_delete(ref, repo);
2027 static const struct got_error *
2028 add_ref(struct got_repository *repo, const char *refname, const char *target)
2030 const struct got_error *err = NULL;
2031 struct got_object_id *id;
2032 struct got_reference *ref = NULL;
2034 err = got_object_resolve_id_str(&id, repo, target);
2036 struct got_reference *target_ref;
2038 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2040 err = got_ref_open(&target_ref, repo, target, 0);
2043 err = got_ref_resolve(&id, repo, target_ref);
2044 got_ref_close(target_ref);
2049 err = got_ref_alloc(&ref, refname, id);
2053 err = got_ref_write(ref, repo);
2061 static const struct got_error *
2062 cmd_ref(int argc, char *argv[])
2064 const struct got_error *error = NULL;
2065 struct got_repository *repo = NULL;
2066 struct got_worktree *worktree = NULL;
2067 char *cwd = NULL, *repo_path = NULL;
2068 int ch, do_list = 0;
2069 const char *delref = NULL;
2071 /* TODO: Add -s option for adding symbolic references. */
2072 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2078 repo_path = realpath(optarg, NULL);
2079 if (repo_path == NULL)
2080 err(1, "-r option");
2081 got_path_strip_trailing_slashes(repo_path);
2092 if (do_list && delref)
2093 errx(1, "-l and -d options are mutually exclusive\n");
2098 if (do_list || delref) {
2101 } else if (argc != 2)
2106 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2110 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2111 "sendfd unveil", NULL) == -1)
2115 cwd = getcwd(NULL, 0);
2117 error = got_error_from_errno("getcwd");
2121 if (repo_path == NULL) {
2122 error = got_worktree_open(&worktree, cwd);
2123 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2129 strdup(got_worktree_get_repo_path(worktree));
2130 if (repo_path == NULL)
2131 error = got_error_from_errno("strdup");
2135 repo_path = strdup(cwd);
2136 if (repo_path == NULL) {
2137 error = got_error_from_errno("strdup");
2143 error = got_repo_open(&repo, repo_path);
2147 error = apply_unveil(got_repo_get_path(repo), do_list,
2148 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2153 error = list_refs(repo);
2155 error = delete_ref(repo, delref);
2157 error = add_ref(repo, argv[0], argv[1]);
2160 got_repo_close(repo);
2162 got_worktree_close(worktree);
2171 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2175 static const struct got_error *
2176 cmd_add(int argc, char *argv[])
2178 const struct got_error *error = NULL;
2179 struct got_repository *repo = NULL;
2180 struct got_worktree *worktree = NULL;
2182 struct got_pathlist_head paths;
2183 struct got_pathlist_entry *pe;
2188 while ((ch = getopt(argc, argv, "")) != -1) {
2202 /* make sure each file exists before doing anything halfway */
2203 for (x = 0; x < argc; x++) {
2204 char *path = realpath(argv[x], NULL);
2206 error = got_error_from_errno2("realpath", argv[x]);
2212 cwd = getcwd(NULL, 0);
2214 error = got_error_from_errno("getcwd");
2218 error = got_worktree_open(&worktree, cwd);
2222 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2226 error = apply_unveil(got_repo_get_path(repo), 1,
2227 got_worktree_get_root_path(worktree), 0);
2231 for (x = 0; x < argc; x++) {
2232 char *path = realpath(argv[x], NULL);
2234 error = got_error_from_errno2("realpath", argv[x]);
2238 got_path_strip_trailing_slashes(path);
2239 error = got_pathlist_insert(&pe, &paths, path, NULL);
2245 error = got_worktree_schedule_add(worktree, &paths, print_status,
2249 got_repo_close(repo);
2251 got_worktree_close(worktree);
2252 TAILQ_FOREACH(pe, &paths, entry)
2253 free((char *)pe->path);
2254 got_pathlist_free(&paths);
2262 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2266 static const struct got_error *
2267 cmd_rm(int argc, char *argv[])
2269 const struct got_error *error = NULL;
2270 struct got_worktree *worktree = NULL;
2271 struct got_repository *repo = NULL;
2273 struct got_pathlist_head paths;
2274 struct got_pathlist_entry *pe;
2275 int ch, i, delete_local_mods = 0;
2279 while ((ch = getopt(argc, argv, "f")) != -1) {
2282 delete_local_mods = 1;
2296 /* make sure each file exists before doing anything halfway */
2297 for (i = 0; i < argc; i++) {
2298 char *path = realpath(argv[i], NULL);
2300 error = got_error_from_errno2("realpath", argv[i]);
2306 cwd = getcwd(NULL, 0);
2308 error = got_error_from_errno("getcwd");
2311 error = got_worktree_open(&worktree, cwd);
2315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2319 error = apply_unveil(got_repo_get_path(repo), 1,
2320 got_worktree_get_root_path(worktree), 0);
2324 for (i = 0; i < argc; i++) {
2325 char *path = realpath(argv[i], NULL);
2327 error = got_error_from_errno2("realpath", argv[i]);
2331 got_path_strip_trailing_slashes(path);
2332 error = got_pathlist_insert(&pe, &paths, path, NULL);
2338 error = got_worktree_schedule_delete(worktree, &paths,
2339 delete_local_mods, print_status, NULL, repo);
2344 got_repo_close(repo);
2346 got_worktree_close(worktree);
2347 TAILQ_FOREACH(pe, &paths, entry)
2348 free((char *)pe->path);
2349 got_pathlist_free(&paths);
2357 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2362 revert_progress(void *arg, unsigned char status, const char *path)
2364 while (path[0] == '/')
2366 printf("%c %s\n", status, path);
2369 static const struct got_error *
2370 cmd_revert(int argc, char *argv[])
2372 const struct got_error *error = NULL;
2373 struct got_worktree *worktree = NULL;
2374 struct got_repository *repo = NULL;
2375 char *cwd = NULL, *path = NULL;
2376 struct got_pathlist_head paths;
2377 struct got_pathlist_entry *pe;
2382 while ((ch = getopt(argc, argv, "")) != -1) {
2396 for (i = 0; i < argc; i++) {
2397 char *path = realpath(argv[i], NULL);
2399 error = got_error_from_errno2("realpath", argv[i]);
2403 got_path_strip_trailing_slashes(path);
2404 error = got_pathlist_insert(&pe, &paths, path, NULL);
2411 cwd = getcwd(NULL, 0);
2413 error = got_error_from_errno("getcwd");
2416 error = got_worktree_open(&worktree, cwd);
2420 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2424 error = apply_unveil(got_repo_get_path(repo), 1,
2425 got_worktree_get_root_path(worktree), 0);
2429 error = got_worktree_revert(worktree, &paths,
2430 revert_progress, NULL, repo);
2435 got_repo_close(repo);
2437 got_worktree_close(worktree);
2446 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2451 spawn_editor(const char *editor, const char *file)
2454 sig_t sighup, sigint, sigquit;
2457 sighup = signal(SIGHUP, SIG_IGN);
2458 sigint = signal(SIGINT, SIG_IGN);
2459 sigquit = signal(SIGQUIT, SIG_IGN);
2461 switch (pid = fork()) {
2465 execl(editor, editor, file, (char *)NULL);
2469 while (waitpid(pid, &st, 0) == -1)
2474 (void)signal(SIGHUP, sighup);
2475 (void)signal(SIGINT, sigint);
2476 (void)signal(SIGQUIT, sigquit);
2478 if (!WIFEXITED(st)) {
2483 return WEXITSTATUS(st);
2486 struct collect_commit_logmsg_arg {
2487 const char *cmdline_log;
2489 const char *worktree_path;
2490 const char *branch_name;
2491 const char *repo_path;
2496 static const struct got_error *
2497 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2500 char *initial_content = NULL;
2501 struct got_pathlist_entry *pe;
2502 const struct got_error *err = NULL;
2503 char *template = NULL;
2504 struct collect_commit_logmsg_arg *a = arg;
2506 struct stat st, st2;
2509 int fd, content_changed = 0;
2511 /* if a message was specified on the command line, just use it */
2512 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2513 len = strlen(a->cmdline_log) + 1;
2514 *logmsg = malloc(len + 1);
2515 if (*logmsg == NULL)
2516 return got_error_from_errno("malloc");
2517 strlcpy(*logmsg, a->cmdline_log, len);
2521 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2522 return got_error_from_errno("asprintf");
2524 if (asprintf(&initial_content,
2525 "\n# changes to be committed on branch %s:\n",
2526 a->branch_name) == -1)
2527 return got_error_from_errno("asprintf");
2529 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2533 dprintf(fd, initial_content);
2535 TAILQ_FOREACH(pe, commitable_paths, entry) {
2536 struct got_commitable *ct = pe->data;
2537 dprintf(fd, "# %c %s\n",
2538 got_commitable_get_status(ct),
2539 got_commitable_get_path(ct));
2543 if (stat(a->logmsg_path, &st) == -1) {
2544 err = got_error_from_errno2("stat", a->logmsg_path);
2548 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2549 err = got_error_from_errno("failed spawning editor");
2553 if (stat(a->logmsg_path, &st2) == -1) {
2554 err = got_error_from_errno("stat");
2558 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2559 unlink(a->logmsg_path);
2560 free(a->logmsg_path);
2561 a->logmsg_path = NULL;
2562 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2563 "no changes made to commit message, aborting");
2567 *logmsg = malloc(st2.st_size + 1);
2568 if (*logmsg == NULL) {
2569 err = got_error_from_errno("malloc");
2572 (*logmsg)[0] = '\0';
2575 fp = fopen(a->logmsg_path, "r");
2577 err = got_error_from_errno("fopen");
2580 while (fgets(buf, sizeof(buf), fp) != NULL) {
2581 if (!content_changed && strcmp(buf, initial_content) != 0)
2582 content_changed = 1;
2583 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2584 continue; /* remove comments and leading empty lines */
2585 len = strlcat(*logmsg, buf, st2.st_size);
2589 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2590 (*logmsg)[len - 1] = '\0';
2594 if (len == 0 || !content_changed) {
2595 unlink(a->logmsg_path);
2596 free(a->logmsg_path);
2597 a->logmsg_path = NULL;
2598 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2599 "commit message cannot be empty, aborting");
2603 free(initial_content);
2606 /* Editor is done; we can now apply unveil(2) */
2608 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2612 static const struct got_error *
2613 cmd_commit(int argc, char *argv[])
2615 const struct got_error *error = NULL;
2616 struct got_worktree *worktree = NULL;
2617 struct got_repository *repo = NULL;
2618 char *cwd = NULL, *path = NULL, *id_str = NULL;
2619 struct got_object_id *id = NULL;
2620 const char *logmsg = NULL;
2621 const char *got_author = getenv("GOT_AUTHOR");
2622 struct collect_commit_logmsg_arg cl_arg;
2623 char *editor = NULL;
2626 while ((ch = getopt(argc, argv, "m:")) != -1) {
2641 path = realpath(argv[0], NULL);
2643 error = got_error_from_errno2("realpath", argv[0]);
2646 got_path_strip_trailing_slashes(path);
2647 } else if (argc != 0)
2650 if (got_author == NULL) {
2651 /* TODO: Look current user up in password database */
2652 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2656 cwd = getcwd(NULL, 0);
2658 error = got_error_from_errno("getcwd");
2661 error = got_worktree_open(&worktree, cwd);
2665 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2670 * unveil(2) traverses exec(2); if an editor is used we have
2671 * to apply unveil after the log message has been written.
2673 if (logmsg == NULL || strlen(logmsg) == 0)
2674 error = get_editor(&editor);
2676 error = apply_unveil(got_repo_get_path(repo), 0,
2677 got_worktree_get_root_path(worktree), 0);
2681 cl_arg.editor = editor;
2682 cl_arg.cmdline_log = logmsg;
2683 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2684 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2685 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2686 cl_arg.branch_name += 5;
2687 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2688 cl_arg.branch_name += 6;
2689 cl_arg.repo_path = got_repo_get_path(repo);
2690 cl_arg.logmsg_path = NULL;
2691 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2692 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2694 if (cl_arg.logmsg_path)
2695 fprintf(stderr, "%s: log message preserved in %s\n",
2696 getprogname(), cl_arg.logmsg_path);
2700 if (cl_arg.logmsg_path)
2701 unlink(cl_arg.logmsg_path);
2703 error = got_object_id_str(&id_str, id);
2706 printf("Created commit %s\n", id_str);
2709 got_repo_close(repo);
2711 got_worktree_close(worktree);
2720 usage_cherrypick(void)
2722 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2726 static const struct got_error *
2727 cmd_cherrypick(int argc, char *argv[])
2729 const struct got_error *error = NULL;
2730 struct got_worktree *worktree = NULL;
2731 struct got_repository *repo = NULL;
2732 char *cwd = NULL, *commit_id_str = NULL;
2733 struct got_object_id *commit_id = NULL;
2734 struct got_commit_object *commit = NULL;
2735 struct got_object_qid *pid;
2736 struct got_reference *head_ref = NULL;
2737 int ch, did_something = 0;
2739 while ((ch = getopt(argc, argv, "")) != -1) {
2753 cwd = getcwd(NULL, 0);
2755 error = got_error_from_errno("getcwd");
2758 error = got_worktree_open(&worktree, cwd);
2762 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2766 error = apply_unveil(got_repo_get_path(repo), 0,
2767 got_worktree_get_root_path(worktree), 0);
2771 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2772 if (error != NULL) {
2773 struct got_reference *ref;
2774 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2776 error = got_ref_open(&ref, repo, argv[0], 0);
2779 error = got_ref_resolve(&commit_id, repo, ref);
2784 error = got_object_id_str(&commit_id_str, commit_id);
2788 error = got_ref_open(&head_ref, repo,
2789 got_worktree_get_head_ref_name(worktree), 0);
2793 error = check_same_branch(commit_id, head_ref, repo);
2795 if (error->code != GOT_ERR_ANCESTRY)
2799 error = got_error(GOT_ERR_SAME_BRANCH);
2803 error = got_object_open_as_commit(&commit, repo, commit_id);
2806 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2807 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2808 commit_id, repo, update_progress, &did_something, check_cancelled,
2814 printf("Merged commit %s\n", commit_id_str);
2817 got_object_commit_close(commit);
2818 free(commit_id_str);
2820 got_ref_close(head_ref);
2822 got_worktree_close(worktree);
2824 got_repo_close(repo);
2831 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2835 static const struct got_error *
2836 cmd_backout(int argc, char *argv[])
2838 const struct got_error *error = NULL;
2839 struct got_worktree *worktree = NULL;
2840 struct got_repository *repo = NULL;
2841 char *cwd = NULL, *commit_id_str = NULL;
2842 struct got_object_id *commit_id = NULL;
2843 struct got_commit_object *commit = NULL;
2844 struct got_object_qid *pid;
2845 struct got_reference *head_ref = NULL;
2846 int ch, did_something = 0;
2848 while ((ch = getopt(argc, argv, "")) != -1) {
2862 cwd = getcwd(NULL, 0);
2864 error = got_error_from_errno("getcwd");
2867 error = got_worktree_open(&worktree, cwd);
2871 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2875 error = apply_unveil(got_repo_get_path(repo), 0,
2876 got_worktree_get_root_path(worktree), 0);
2880 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2881 if (error != NULL) {
2882 struct got_reference *ref;
2883 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2885 error = got_ref_open(&ref, repo, argv[0], 0);
2888 error = got_ref_resolve(&commit_id, repo, ref);
2893 error = got_object_id_str(&commit_id_str, commit_id);
2897 error = got_ref_open(&head_ref, repo,
2898 got_worktree_get_head_ref_name(worktree), 0);
2902 error = check_same_branch(commit_id, head_ref, repo);
2906 error = got_object_open_as_commit(&commit, repo, commit_id);
2909 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2911 error = got_error(GOT_ERR_ROOT_COMMIT);
2915 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2916 update_progress, &did_something, check_cancelled, NULL);
2921 printf("Backed out commit %s\n", commit_id_str);
2924 got_object_commit_close(commit);
2925 free(commit_id_str);
2927 got_ref_close(head_ref);
2929 got_worktree_close(worktree);
2931 got_repo_close(repo);