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);
91 static const struct got_error* cmd_checkout(int, char *[]);
92 static const struct got_error* cmd_update(int, char *[]);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_status(int, char *[]);
98 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct got_error* cmd_add(int, char *[]);
100 static const struct got_error* cmd_rm(int, char *[]);
101 static const struct got_error* cmd_revert(int, char *[]);
102 static const struct got_error* cmd_commit(int, char *[]);
103 static const struct got_error* cmd_cherrypick(int, char *[]);
105 static struct cmd got_commands[] = {
106 { "checkout", cmd_checkout, usage_checkout,
107 "check out a new work tree from a repository" },
108 { "update", cmd_update, usage_update,
109 "update a work tree to a different commit" },
110 { "log", cmd_log, usage_log,
111 "show repository history" },
112 { "diff", cmd_diff, usage_diff,
113 "compare files and directories" },
114 { "blame", cmd_blame, usage_blame,
115 "show when lines in a file were changed" },
116 { "tree", cmd_tree, usage_tree,
117 "list files and directories in repository" },
118 { "status", cmd_status, usage_status,
119 "show modification status of files" },
120 { "ref", cmd_ref, usage_ref,
121 "manage references in repository" },
122 { "add", cmd_add, usage_add,
123 "add new files to version control" },
124 { "rm", cmd_rm, usage_rm,
125 "remove a versioned file" },
126 { "revert", cmd_revert, usage_revert,
127 "revert uncommitted changes" },
128 { "commit", cmd_commit, usage_commit,
129 "write changes from work tree to repository" },
130 { "cherrypick", cmd_cherrypick, usage_cherrypick,
131 "merge a single commit from another branch into a work tree" },
135 main(int argc, char *argv[])
142 setlocale(LC_CTYPE, "");
144 while ((ch = getopt(argc, argv, "h")) != -1) {
162 signal(SIGINT, catch_sigint);
163 signal(SIGPIPE, catch_sigpipe);
165 for (i = 0; i < nitems(got_commands); i++) {
166 const struct got_error *error;
168 cmd = &got_commands[i];
170 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
174 got_commands[i].cmd_usage();
176 error = got_commands[i].cmd_main(argc, argv);
177 if (error && !(sigint_received || sigpipe_received)) {
178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
185 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
194 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
195 "Available commands:\n", getprogname());
196 for (i = 0; i < nitems(got_commands); i++) {
197 struct cmd *cmd = &got_commands[i];
198 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
203 static const struct got_error *
204 get_editor(char **abspath)
206 const struct got_error *err = NULL;
209 editor = getenv("VISUAL");
211 editor = getenv("EDITOR");
214 err = got_path_find_prog(abspath, editor);
219 if (*abspath == NULL) {
220 *abspath = strdup("/bin/ed");
221 if (*abspath == NULL)
222 return got_error_from_errno("strdup");
228 static const struct got_error *
229 apply_unveil(const char *repo_path, int repo_read_only,
230 const char *worktree_path, int create_worktree)
232 const struct got_error *err;
234 if (create_worktree) {
235 /* Pre-create work tree path to avoid unveiling its parents. */
236 err = got_path_mkdir(worktree_path);
238 if (errno == EEXIST) {
239 if (got_path_dir_is_empty(worktree_path)) {
243 err = got_error_path(worktree_path,
244 GOT_ERR_DIR_NOT_EMPTY);
248 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
252 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
253 return got_error_from_errno2("unveil", repo_path);
255 if (worktree_path && unveil(worktree_path, "rwc") != 0)
256 return got_error_from_errno2("unveil", worktree_path);
258 if (unveil("/tmp", "rwc") != 0)
259 return got_error_from_errno2("unveil", "/tmp");
261 err = got_privsep_unveil_exec_helpers();
265 if (unveil(NULL, NULL) != 0)
266 return got_error_from_errno("unveil");
274 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
275 "[-p prefix] repository-path [worktree-path]\n", getprogname());
280 checkout_progress(void *arg, unsigned char status, const char *path)
282 char *worktree_path = arg;
284 while (path[0] == '/')
287 printf("%c %s/%s\n", status, worktree_path, path);
290 static const struct got_error *
291 check_cancelled(void *arg)
293 if (sigint_received || sigpipe_received)
294 return got_error(GOT_ERR_CANCELLED);
298 static const struct got_error *
299 check_linear_ancestry(struct got_object_id *commit_id,
300 struct got_object_id *base_commit_id, struct got_repository *repo)
302 const struct got_error *err = NULL;
303 struct got_object_id *yca_id;
305 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
306 commit_id, base_commit_id, repo);
311 return got_error(GOT_ERR_ANCESTRY);
314 * Require a straight line of history between the target commit
315 * and the work tree's base commit.
317 * Non-linear situations such as this require a rebase:
319 * (commit) D F (base_commit)
327 * 'got update' only handles linear cases:
328 * Update forwards in time: A (base/yca) - B - C - D (commit)
329 * Update backwards in time: D (base) - C - B - A (commit/yca)
331 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
332 got_object_id_cmp(base_commit_id, yca_id) != 0)
333 return got_error(GOT_ERR_ANCESTRY);
339 static const struct got_error *
340 check_same_branch(struct got_object_id *commit_id,
341 struct got_reference *head_ref, struct got_repository *repo)
343 const struct got_error *err = NULL;
344 struct got_commit_graph *graph = NULL;
345 struct got_object_id *head_commit_id = NULL;
346 int is_same_branch = 0;
348 err = got_ref_resolve(&head_commit_id, repo, head_ref);
352 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
356 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
361 struct got_object_id *id;
362 err = got_commit_graph_iter_next(&id, graph);
364 if (err->code == GOT_ERR_ITER_COMPLETED) {
368 else if (err->code != GOT_ERR_ITER_NEED_MORE)
370 err = got_commit_graph_fetch_commits(graph, 1,
377 if (got_object_id_cmp(id, commit_id) == 0) {
385 got_commit_graph_close(graph);
386 free(head_commit_id);
387 if (!err && !is_same_branch)
388 err = got_error(GOT_ERR_ANCESTRY);
392 static const struct got_error *
393 cmd_checkout(int argc, char *argv[])
395 const struct got_error *error = NULL;
396 struct got_repository *repo = NULL;
397 struct got_reference *head_ref = NULL;
398 struct got_worktree *worktree = NULL;
399 char *repo_path = NULL;
400 char *worktree_path = NULL;
401 const char *path_prefix = "";
402 const char *branch_name = GOT_REF_HEAD;
403 char *commit_id_str = NULL;
404 int ch, same_path_prefix;
406 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
409 branch_name = optarg;
412 commit_id_str = strdup(optarg);
413 if (commit_id_str == NULL)
414 return got_error_from_errno("strdup");
417 path_prefix = optarg;
429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
430 "unveil", NULL) == -1)
434 char *cwd, *base, *dotgit;
435 repo_path = realpath(argv[0], NULL);
436 if (repo_path == NULL)
437 return got_error_from_errno2("realpath", argv[0]);
438 cwd = getcwd(NULL, 0);
440 error = got_error_from_errno("getcwd");
443 if (path_prefix[0]) {
444 base = basename(path_prefix);
446 error = got_error_from_errno2("basename",
451 base = basename(repo_path);
453 error = got_error_from_errno2("basename",
458 dotgit = strstr(base, ".git");
461 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
462 error = got_error_from_errno("asprintf");
467 } else if (argc == 2) {
468 repo_path = realpath(argv[0], NULL);
469 if (repo_path == NULL) {
470 error = got_error_from_errno2("realpath", argv[0]);
473 worktree_path = realpath(argv[1], NULL);
474 if (worktree_path == NULL) {
475 error = got_error_from_errno2("realpath", argv[1]);
481 got_path_strip_trailing_slashes(repo_path);
482 got_path_strip_trailing_slashes(worktree_path);
484 error = got_repo_open(&repo, repo_path);
488 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
492 error = got_ref_open(&head_ref, repo, branch_name, 0);
496 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
497 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
500 error = got_worktree_open(&worktree, worktree_path);
504 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
508 if (!same_path_prefix) {
509 error = got_error(GOT_ERR_PATH_PREFIX);
514 struct got_object_id *commit_id;
515 error = got_object_resolve_id_str(&commit_id, repo,
519 error = check_linear_ancestry(commit_id,
520 got_worktree_get_base_commit_id(worktree), repo);
525 error = check_same_branch(commit_id, head_ref, repo);
528 error = got_worktree_set_base_commit_id(worktree, repo,
535 error = got_worktree_checkout_files(worktree, "", repo,
536 checkout_progress, worktree_path, check_cancelled, NULL);
540 printf("Now shut up and hack\n");
552 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
558 update_progress(void *arg, unsigned char status, const char *path)
560 int *did_something = arg;
562 if (status == GOT_STATUS_EXISTS)
566 while (path[0] == '/')
568 printf("%c %s\n", status, path);
571 static const struct got_error *
572 switch_head_ref(struct got_reference *head_ref,
573 struct got_object_id *commit_id, struct got_worktree *worktree,
574 struct got_repository *repo)
576 const struct got_error *err = NULL;
578 int ref_has_moved = 0;
580 /* Trivial case: switching between two different references. */
581 if (strcmp(got_ref_get_name(head_ref),
582 got_worktree_get_head_ref_name(worktree)) != 0) {
583 printf("Switching work tree from %s to %s\n",
584 got_worktree_get_head_ref_name(worktree),
585 got_ref_get_name(head_ref));
586 return got_worktree_set_head_ref(worktree, head_ref);
589 err = check_linear_ancestry(commit_id,
590 got_worktree_get_base_commit_id(worktree), repo);
592 if (err->code != GOT_ERR_ANCESTRY)
599 /* Switching to a rebased branch with the same reference name. */
600 err = got_object_id_str(&base_id_str,
601 got_worktree_get_base_commit_id(worktree));
604 printf("Reference %s now points at a different branch\n",
605 got_worktree_get_head_ref_name(worktree));
606 printf("Switching work tree from %s to %s\n", base_id_str,
607 got_worktree_get_head_ref_name(worktree));
611 static const struct got_error *
612 cmd_update(int argc, char *argv[])
614 const struct got_error *error = NULL;
615 struct got_repository *repo = NULL;
616 struct got_worktree *worktree = NULL;
617 char *worktree_path = NULL, *path = NULL;
618 struct got_object_id *commit_id = NULL;
619 char *commit_id_str = NULL;
620 const char *branch_name = NULL;
621 struct got_reference *head_ref = NULL;
622 int ch, did_something = 0;
624 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
627 branch_name = optarg;
630 commit_id_str = strdup(optarg);
631 if (commit_id_str == NULL)
632 return got_error_from_errno("strdup");
644 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
645 "unveil", NULL) == -1)
648 worktree_path = getcwd(NULL, 0);
649 if (worktree_path == NULL) {
650 error = got_error_from_errno("getcwd");
653 error = got_worktree_open(&worktree, worktree_path);
660 error = got_error_from_errno("strdup");
663 } else if (argc == 1) {
664 error = got_worktree_resolve_path(&path, worktree, argv[0]);
670 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
674 error = apply_unveil(got_repo_get_path(repo), 0,
675 got_worktree_get_root_path(worktree), 0);
679 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
680 got_worktree_get_head_ref_name(worktree), 0);
683 if (commit_id_str == NULL) {
684 error = got_ref_resolve(&commit_id, repo, head_ref);
687 error = got_object_id_str(&commit_id_str, commit_id);
691 error = got_object_resolve_id_str(&commit_id, repo,
698 struct got_object_id *head_commit_id;
699 if (strlen(path) != 0) {
700 fprintf(stderr, "%s: switching between branches "
701 "requires that the entire work tree "
702 "gets updated, not just '%s'\n",
703 getprogname(), path);
704 error = got_error(GOT_ERR_BAD_PATH);
707 error = got_ref_resolve(&head_commit_id, repo, head_ref);
710 error = check_linear_ancestry(commit_id, head_commit_id, repo);
711 free(head_commit_id);
714 error = check_same_branch(commit_id, head_ref, repo);
717 error = switch_head_ref(head_ref, commit_id, worktree, repo);
721 error = check_linear_ancestry(commit_id,
722 got_worktree_get_base_commit_id(worktree), repo);
724 if (error->code == GOT_ERR_ANCESTRY)
725 error = got_error(GOT_ERR_BRANCH_MOVED);
728 error = check_same_branch(commit_id, head_ref, repo);
733 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
735 error = got_worktree_set_base_commit_id(worktree, repo,
741 error = got_worktree_checkout_files(worktree, path, repo,
742 update_progress, &did_something, check_cancelled, NULL);
747 printf("Updated to commit %s\n", commit_id_str);
749 printf("Already up-to-date\n");
758 static const struct got_error *
759 print_patch(struct got_commit_object *commit, struct got_object_id *id,
760 int diff_context, struct got_repository *repo)
762 const struct got_error *err = NULL;
763 struct got_tree_object *tree1 = NULL, *tree2;
764 struct got_object_qid *qid;
765 char *id_str1 = NULL, *id_str2;
766 struct got_diff_blob_output_unidiff_arg arg;
768 err = got_object_open_as_tree(&tree2, repo,
769 got_object_commit_get_tree_id(commit));
773 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
775 struct got_commit_object *pcommit;
777 err = got_object_open_as_commit(&pcommit, repo, qid->id);
781 err = got_object_open_as_tree(&tree1, repo,
782 got_object_commit_get_tree_id(pcommit));
783 got_object_commit_close(pcommit);
787 err = got_object_id_str(&id_str1, qid->id);
792 err = got_object_id_str(&id_str2, id);
796 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
797 arg.diff_context = diff_context;
798 arg.outfile = stdout;
799 err = got_diff_tree(tree1, tree2, "", "", repo,
800 got_diff_blob_output_unidiff, &arg);
803 got_object_tree_close(tree1);
804 got_object_tree_close(tree2);
811 get_datestr(time_t *time, char *datebuf)
813 char *p, *s = ctime_r(time, datebuf);
820 static const struct got_error *
821 print_commit(struct got_commit_object *commit, struct got_object_id *id,
822 struct got_repository *repo, int show_patch, int diff_context,
823 struct got_reflist_head *refs)
825 const struct got_error *err = NULL;
826 char *id_str, *datestr, *logmsg0, *logmsg, *line;
828 time_t committer_time;
829 const char *author, *committer;
830 char *refs_str = NULL;
831 struct got_reflist_entry *re;
833 SIMPLEQ_FOREACH(re, refs, entry) {
836 if (got_object_id_cmp(re->id, id) != 0)
838 name = got_ref_get_name(re->ref);
839 if (strcmp(name, GOT_REF_HEAD) == 0)
841 if (strncmp(name, "refs/", 5) == 0)
843 if (strncmp(name, "got/", 4) == 0)
845 if (strncmp(name, "heads/", 6) == 0)
847 if (strncmp(name, "remotes/", 8) == 0)
850 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
852 err = got_error_from_errno("asprintf");
858 err = got_object_id_str(&id_str, id);
862 printf("-----------------------------------------------\n");
863 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
864 refs_str ? refs_str : "", refs_str ? ")" : "");
869 printf("from: %s\n", got_object_commit_get_author(commit));
870 committer_time = got_object_commit_get_committer_time(commit);
871 datestr = get_datestr(&committer_time, datebuf);
872 printf("date: %s UTC\n", datestr);
873 author = got_object_commit_get_author(commit);
874 committer = got_object_commit_get_committer(commit);
875 if (strcmp(author, committer) != 0)
876 printf("via: %s\n", committer);
877 if (got_object_commit_get_nparents(commit) > 1) {
878 const struct got_object_id_queue *parent_ids;
879 struct got_object_qid *qid;
881 parent_ids = got_object_commit_get_parent_ids(commit);
882 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
883 err = got_object_id_str(&id_str, qid->id);
886 printf("parent %d: %s\n", n++, id_str);
891 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
893 return got_error_from_errno("strdup");
897 line = strsep(&logmsg, "\n");
899 printf(" %s\n", line);
904 err = print_patch(commit, id, diff_context, repo);
909 if (fflush(stdout) != 0 && err == NULL)
910 err = got_error_from_errno("fflush");
914 static const struct got_error *
915 print_commits(struct got_object_id *root_id, struct got_repository *repo,
916 char *path, int show_patch, int diff_context, int limit,
917 int first_parent_traversal, struct got_reflist_head *refs)
919 const struct got_error *err;
920 struct got_commit_graph *graph;
922 err = got_commit_graph_open(&graph, root_id, path,
923 first_parent_traversal, repo);
926 err = got_commit_graph_iter_start(graph, root_id, repo);
930 struct got_commit_object *commit;
931 struct got_object_id *id;
933 if (sigint_received || sigpipe_received)
936 err = got_commit_graph_iter_next(&id, graph);
938 if (err->code == GOT_ERR_ITER_COMPLETED) {
942 if (err->code != GOT_ERR_ITER_NEED_MORE)
944 err = got_commit_graph_fetch_commits(graph, 1, repo);
953 err = got_object_open_as_commit(&commit, repo, id);
956 err = print_commit(commit, id, repo, show_patch, diff_context,
958 got_object_commit_close(commit);
959 if (err || (limit && --limit == 0))
963 got_commit_graph_close(graph);
970 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
971 "[-r repository-path] [path]\n", getprogname());
975 static const struct got_error *
976 cmd_log(int argc, char *argv[])
978 const struct got_error *error;
979 struct got_repository *repo = NULL;
980 struct got_worktree *worktree = NULL;
981 struct got_commit_object *commit = NULL;
982 struct got_object_id *id = NULL;
983 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
984 char *start_commit = NULL;
985 int diff_context = 3, ch;
986 int show_patch = 0, limit = 0, first_parent_traversal = 0;
988 struct got_reflist_head refs;
993 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
999 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1002 first_parent_traversal = 1;
1008 start_commit = optarg;
1011 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1014 err(1, "-C option %s", errstr);
1017 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1019 err(1, "-l option %s", errstr);
1022 repo_path = realpath(optarg, NULL);
1023 if (repo_path == NULL)
1024 err(1, "-r option");
1025 got_path_strip_trailing_slashes(repo_path);
1036 cwd = getcwd(NULL, 0);
1038 error = got_error_from_errno("getcwd");
1042 error = got_worktree_open(&worktree, cwd);
1043 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1050 error = got_error_from_errno("strdup");
1053 } else if (argc == 1) {
1055 error = got_worktree_resolve_path(&path, worktree,
1060 path = strdup(argv[0]);
1062 error = got_error_from_errno("strdup");
1069 if (repo_path == NULL) {
1070 repo_path = worktree ?
1071 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1073 if (repo_path == NULL) {
1074 error = got_error_from_errno("strdup");
1078 error = got_repo_open(&repo, repo_path);
1082 error = apply_unveil(got_repo_get_path(repo), 1,
1083 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1087 if (start_commit == NULL) {
1088 struct got_reference *head_ref;
1089 error = got_ref_open(&head_ref, repo,
1090 worktree ? got_worktree_get_head_ref_name(worktree)
1094 error = got_ref_resolve(&id, repo, head_ref);
1095 got_ref_close(head_ref);
1098 error = got_object_open_as_commit(&commit, repo, id);
1100 struct got_reference *ref;
1101 error = got_ref_open(&ref, repo, start_commit, 0);
1102 if (error == NULL) {
1104 error = got_ref_resolve(&id, repo, ref);
1108 error = got_object_get_type(&obj_type, repo, id);
1111 if (obj_type == GOT_OBJ_TYPE_TAG) {
1112 struct got_tag_object *tag;
1113 error = got_object_open_as_tag(&tag, repo, id);
1116 if (got_object_tag_get_object_type(tag) !=
1117 GOT_OBJ_TYPE_COMMIT) {
1118 got_object_tag_close(tag);
1119 error = got_error(GOT_ERR_OBJ_TYPE);
1123 id = got_object_id_dup(
1124 got_object_tag_get_object_id(tag));
1126 error = got_error_from_errno(
1127 "got_object_id_dup");
1128 got_object_tag_close(tag);
1131 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1132 error = got_error(GOT_ERR_OBJ_TYPE);
1135 error = got_object_open_as_commit(&commit, repo, id);
1139 if (commit == NULL) {
1140 error = got_object_resolve_id_str(&id, repo,
1149 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1154 path = in_repo_path;
1157 error = got_ref_list(&refs, repo);
1161 error = print_commits(id, repo, path, show_patch,
1162 diff_context, limit, first_parent_traversal, &refs);
1169 got_worktree_close(worktree);
1171 const struct got_error *repo_error;
1172 repo_error = got_repo_close(repo);
1176 got_ref_list_free(&refs);
1183 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1184 "[object1 object2 | path]\n", getprogname());
1188 struct print_diff_arg {
1189 struct got_repository *repo;
1190 struct got_worktree *worktree;
1196 static const struct got_error *
1197 print_diff(void *arg, unsigned char status, const char *path,
1198 struct got_object_id *blob_id, struct got_object_id *commit_id)
1200 struct print_diff_arg *a = arg;
1201 const struct got_error *err = NULL;
1202 struct got_blob_object *blob1 = NULL;
1204 char *abspath = NULL;
1207 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1208 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1211 if (!a->header_shown) {
1212 printf("diff %s %s\n", a->id_str,
1213 got_worktree_get_root_path(a->worktree));
1214 a->header_shown = 1;
1217 if (status != GOT_STATUS_ADD) {
1218 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1224 if (status != GOT_STATUS_DELETE) {
1225 if (asprintf(&abspath, "%s/%s",
1226 got_worktree_get_root_path(a->worktree), path) == -1) {
1227 err = got_error_from_errno("asprintf");
1231 f2 = fopen(abspath, "r");
1233 err = got_error_from_errno2("fopen", abspath);
1236 if (lstat(abspath, &sb) == -1) {
1237 err = got_error_from_errno2("lstat", abspath);
1243 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1247 got_object_blob_close(blob1);
1248 if (f2 && fclose(f2) != 0 && err == NULL)
1249 err = got_error_from_errno("fclose");
1254 static const struct got_error *
1255 cmd_diff(int argc, char *argv[])
1257 const struct got_error *error;
1258 struct got_repository *repo = NULL;
1259 struct got_worktree *worktree = NULL;
1260 char *cwd = NULL, *repo_path = NULL;
1261 struct got_object_id *id1 = NULL, *id2 = NULL;
1262 const char *id_str1 = NULL, *id_str2 = NULL;
1263 char *label1 = NULL, *label2 = NULL;
1265 int diff_context = 3, ch;
1270 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1275 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1278 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1280 err(1, "-C option %s", errstr);
1283 repo_path = realpath(optarg, NULL);
1284 if (repo_path == NULL)
1285 err(1, "-r option");
1286 got_path_strip_trailing_slashes(repo_path);
1297 cwd = getcwd(NULL, 0);
1299 error = got_error_from_errno("getcwd");
1302 error = got_worktree_open(&worktree, cwd);
1303 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1306 if (worktree == NULL) {
1307 error = got_error(GOT_ERR_NOT_WORKTREE);
1312 "-r option can't be used when diffing a work tree");
1313 repo_path = strdup(got_worktree_get_repo_path(worktree));
1314 if (repo_path == NULL) {
1315 error = got_error_from_errno("strdup");
1319 error = got_worktree_resolve_path(&path, worktree,
1326 error = got_error_from_errno("strdup");
1330 } else if (argc == 2) {
1336 if (repo_path == NULL) {
1337 repo_path = getcwd(NULL, 0);
1338 if (repo_path == NULL)
1339 return got_error_from_errno("getcwd");
1342 error = got_repo_open(&repo, repo_path);
1347 error = apply_unveil(got_repo_get_path(repo), 1,
1348 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1353 struct print_diff_arg arg;
1355 error = got_object_id_str(&id_str,
1356 got_worktree_get_base_commit_id(worktree));
1360 arg.worktree = worktree;
1361 arg.diff_context = diff_context;
1362 arg.id_str = id_str;
1363 arg.header_shown = 0;
1365 error = got_worktree_status(worktree, path, repo, print_diff,
1366 &arg, check_cancelled, NULL);
1371 error = got_object_resolve_id_str(&id1, repo, id_str1);
1373 struct got_reference *ref;
1374 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1376 error = got_ref_open(&ref, repo, id_str1, 0);
1379 label1 = strdup(got_ref_get_name(ref));
1380 if (label1 == NULL) {
1381 error = got_error_from_errno("strdup");
1384 error = got_ref_resolve(&id1, repo, ref);
1389 label1 = strdup(id_str1);
1390 if (label1 == NULL) {
1391 error = got_error_from_errno("strdup");
1396 error = got_object_resolve_id_str(&id2, repo, id_str2);
1398 struct got_reference *ref;
1399 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1401 error = got_ref_open(&ref, repo, id_str2, 0);
1404 label2 = strdup(got_ref_get_name(ref));
1405 if (label2 == NULL) {
1406 error = got_error_from_errno("strdup");
1409 error = got_ref_resolve(&id2, repo, ref);
1414 label2 = strdup(id_str2);
1415 if (label2 == NULL) {
1416 error = got_error_from_errno("strdup");
1421 error = got_object_get_type(&type1, repo, id1);
1425 error = got_object_get_type(&type2, repo, id2);
1429 if (type1 != type2) {
1430 error = got_error(GOT_ERR_OBJ_TYPE);
1435 case GOT_OBJ_TYPE_BLOB:
1436 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1437 diff_context, repo, stdout);
1439 case GOT_OBJ_TYPE_TREE:
1440 error = got_diff_objects_as_trees(id1, id2, "", "",
1441 diff_context, repo, stdout);
1443 case GOT_OBJ_TYPE_COMMIT:
1444 printf("diff %s %s\n", label1, label2);
1445 error = got_diff_objects_as_commits(id1, id2, diff_context,
1449 error = got_error(GOT_ERR_OBJ_TYPE);
1459 got_worktree_close(worktree);
1461 const struct got_error *repo_error;
1462 repo_error = got_repo_close(repo);
1473 "usage: %s blame [-c commit] [-r repository-path] path\n",
1478 static const struct got_error *
1479 cmd_blame(int argc, char *argv[])
1481 const struct got_error *error;
1482 struct got_repository *repo = NULL;
1483 struct got_worktree *worktree = NULL;
1484 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1485 struct got_object_id *commit_id = NULL;
1486 char *commit_id_str = NULL;
1490 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1495 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1498 commit_id_str = optarg;
1501 repo_path = realpath(optarg, NULL);
1502 if (repo_path == NULL)
1503 err(1, "-r option");
1504 got_path_strip_trailing_slashes(repo_path);
1520 cwd = getcwd(NULL, 0);
1522 error = got_error_from_errno("getcwd");
1525 if (repo_path == NULL) {
1526 error = got_worktree_open(&worktree, cwd);
1527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1533 strdup(got_worktree_get_repo_path(worktree));
1534 if (repo_path == NULL)
1535 error = got_error_from_errno("strdup");
1539 repo_path = strdup(cwd);
1540 if (repo_path == NULL) {
1541 error = got_error_from_errno("strdup");
1547 error = got_repo_open(&repo, repo_path);
1551 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1556 const char *prefix = got_worktree_get_path_prefix(worktree);
1557 char *p, *worktree_subdir = cwd +
1558 strlen(got_worktree_get_root_path(worktree));
1559 if (asprintf(&p, "%s%s%s%s%s",
1560 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1561 worktree_subdir, worktree_subdir[0] ? "/" : "",
1563 error = got_error_from_errno("asprintf");
1566 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1569 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1574 if (commit_id_str == NULL) {
1575 struct got_reference *head_ref;
1576 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1579 error = got_ref_resolve(&commit_id, repo, head_ref);
1580 got_ref_close(head_ref);
1584 error = got_object_resolve_id_str(&commit_id, repo,
1590 error = got_blame(in_repo_path, commit_id, repo, stdout);
1597 got_worktree_close(worktree);
1599 const struct got_error *repo_error;
1600 repo_error = got_repo_close(repo);
1611 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1617 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1618 const char *root_path)
1620 int is_root_path = (strcmp(path, root_path) == 0);
1622 path += strlen(root_path);
1623 while (path[0] == '/')
1626 printf("%s%s%s%s%s\n", id ? id : "", path,
1627 is_root_path ? "" : "/", te->name,
1628 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1631 static const struct got_error *
1632 print_tree(const char *path, struct got_object_id *commit_id,
1633 int show_ids, int recurse, const char *root_path,
1634 struct got_repository *repo)
1636 const struct got_error *err = NULL;
1637 struct got_object_id *tree_id = NULL;
1638 struct got_tree_object *tree = NULL;
1639 const struct got_tree_entries *entries;
1640 struct got_tree_entry *te;
1642 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1646 err = got_object_open_as_tree(&tree, repo, tree_id);
1649 entries = got_object_tree_get_entries(tree);
1650 te = SIMPLEQ_FIRST(&entries->head);
1654 if (sigint_received || sigpipe_received)
1659 err = got_object_id_str(&id_str, te->id);
1662 if (asprintf(&id, "%s ", id_str) == -1) {
1663 err = got_error_from_errno("asprintf");
1669 print_entry(te, id, path, root_path);
1672 if (recurse && S_ISDIR(te->mode)) {
1674 if (asprintf(&child_path, "%s%s%s", path,
1675 path[0] == '/' && path[1] == '\0' ? "" : "/",
1677 err = got_error_from_errno("asprintf");
1680 err = print_tree(child_path, commit_id, show_ids, 1,
1687 te = SIMPLEQ_NEXT(te, entry);
1691 got_object_tree_close(tree);
1696 static const struct got_error *
1697 cmd_tree(int argc, char *argv[])
1699 const struct got_error *error;
1700 struct got_repository *repo = NULL;
1701 struct got_worktree *worktree = NULL;
1703 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1704 struct got_object_id *commit_id = NULL;
1705 char *commit_id_str = NULL;
1706 int show_ids = 0, recurse = 0;
1710 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1715 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1718 commit_id_str = optarg;
1721 repo_path = realpath(optarg, NULL);
1722 if (repo_path == NULL)
1723 err(1, "-r option");
1724 got_path_strip_trailing_slashes(repo_path);
1748 cwd = getcwd(NULL, 0);
1750 error = got_error_from_errno("getcwd");
1753 if (repo_path == NULL) {
1754 error = got_worktree_open(&worktree, cwd);
1755 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1761 strdup(got_worktree_get_repo_path(worktree));
1762 if (repo_path == NULL)
1763 error = got_error_from_errno("strdup");
1767 repo_path = strdup(cwd);
1768 if (repo_path == NULL) {
1769 error = got_error_from_errno("strdup");
1775 error = got_repo_open(&repo, repo_path);
1779 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1785 char *p, *worktree_subdir = cwd +
1786 strlen(got_worktree_get_root_path(worktree));
1787 if (asprintf(&p, "%s/%s",
1788 got_worktree_get_path_prefix(worktree),
1789 worktree_subdir) == -1) {
1790 error = got_error_from_errno("asprintf");
1793 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1800 if (in_repo_path == NULL) {
1801 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1806 if (commit_id_str == NULL) {
1807 struct got_reference *head_ref;
1808 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1811 error = got_ref_resolve(&commit_id, repo, head_ref);
1812 got_ref_close(head_ref);
1816 error = got_object_resolve_id_str(&commit_id, repo,
1822 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1823 in_repo_path, repo);
1830 got_worktree_close(worktree);
1832 const struct got_error *repo_error;
1833 repo_error = got_repo_close(repo);
1843 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1847 static const struct got_error *
1848 print_status(void *arg, unsigned char status, const char *path,
1849 struct got_object_id *blob_id, struct got_object_id *commit_id)
1851 printf("%c %s\n", status, path);
1855 static const struct got_error *
1856 cmd_status(int argc, char *argv[])
1858 const struct got_error *error = NULL;
1859 struct got_repository *repo = NULL;
1860 struct got_worktree *worktree = NULL;
1861 char *cwd = NULL, *path = NULL;
1864 while ((ch = getopt(argc, argv, "")) != -1) {
1876 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1880 cwd = getcwd(NULL, 0);
1882 error = got_error_from_errno("getcwd");
1886 error = got_worktree_open(&worktree, cwd);
1893 error = got_error_from_errno("strdup");
1896 } else if (argc == 1) {
1897 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1903 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1907 error = apply_unveil(got_repo_get_path(repo), 1,
1908 got_worktree_get_root_path(worktree), 0);
1912 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1913 check_cancelled, NULL);
1924 "usage: %s ref [-r repository] -l | -d name | name target\n",
1929 static const struct got_error *
1930 list_refs(struct got_repository *repo)
1932 static const struct got_error *err = NULL;
1933 struct got_reflist_head refs;
1934 struct got_reflist_entry *re;
1936 SIMPLEQ_INIT(&refs);
1937 err = got_ref_list(&refs, repo);
1941 SIMPLEQ_FOREACH(re, &refs, entry) {
1943 refstr = got_ref_to_str(re->ref);
1945 return got_error_from_errno("got_ref_to_str");
1946 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1950 got_ref_list_free(&refs);
1954 static const struct got_error *
1955 delete_ref(struct got_repository *repo, const char *refname)
1957 const struct got_error *err = NULL;
1958 struct got_reference *ref;
1960 err = got_ref_open(&ref, repo, refname, 0);
1964 err = got_ref_delete(ref, repo);
1969 static const struct got_error *
1970 add_ref(struct got_repository *repo, const char *refname, const char *target)
1972 const struct got_error *err = NULL;
1973 struct got_object_id *id;
1974 struct got_reference *ref = NULL;
1976 err = got_object_resolve_id_str(&id, repo, target);
1978 struct got_reference *target_ref;
1980 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1982 err = got_ref_open(&target_ref, repo, target, 0);
1985 err = got_ref_resolve(&id, repo, target_ref);
1986 got_ref_close(target_ref);
1991 err = got_ref_alloc(&ref, refname, id);
1995 err = got_ref_write(ref, repo);
2003 static const struct got_error *
2004 cmd_ref(int argc, char *argv[])
2006 const struct got_error *error = NULL;
2007 struct got_repository *repo = NULL;
2008 struct got_worktree *worktree = NULL;
2009 char *cwd = NULL, *repo_path = NULL;
2010 int ch, do_list = 0;
2011 const char *delref = NULL;
2013 /* TODO: Add -s option for adding symbolic references. */
2014 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2020 repo_path = realpath(optarg, NULL);
2021 if (repo_path == NULL)
2022 err(1, "-r option");
2023 got_path_strip_trailing_slashes(repo_path);
2034 if (do_list && delref)
2035 errx(1, "-l and -d options are mutually exclusive\n");
2040 if (do_list || delref) {
2043 } else if (argc != 2)
2048 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2052 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2053 "sendfd unveil", NULL) == -1)
2057 cwd = getcwd(NULL, 0);
2059 error = got_error_from_errno("getcwd");
2063 if (repo_path == NULL) {
2064 error = got_worktree_open(&worktree, cwd);
2065 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2071 strdup(got_worktree_get_repo_path(worktree));
2072 if (repo_path == NULL)
2073 error = got_error_from_errno("strdup");
2077 repo_path = strdup(cwd);
2078 if (repo_path == NULL) {
2079 error = got_error_from_errno("strdup");
2085 error = got_repo_open(&repo, repo_path);
2089 error = apply_unveil(got_repo_get_path(repo), do_list,
2090 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2095 error = list_refs(repo);
2097 error = delete_ref(repo, delref);
2099 error = add_ref(repo, argv[0], argv[1]);
2102 got_repo_close(repo);
2104 got_worktree_close(worktree);
2113 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2117 static const struct got_error *
2118 cmd_add(int argc, char *argv[])
2120 const struct got_error *error = NULL;
2121 struct got_repository *repo = NULL;
2122 struct got_worktree *worktree = NULL;
2124 struct got_pathlist_head paths;
2125 struct got_pathlist_entry *pe;
2130 while ((ch = getopt(argc, argv, "")) != -1) {
2144 /* make sure each file exists before doing anything halfway */
2145 for (x = 0; x < argc; x++) {
2146 char *path = realpath(argv[x], NULL);
2148 error = got_error_from_errno2("realpath", argv[x]);
2154 cwd = getcwd(NULL, 0);
2156 error = got_error_from_errno("getcwd");
2160 error = got_worktree_open(&worktree, cwd);
2164 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2168 error = apply_unveil(got_repo_get_path(repo), 1,
2169 got_worktree_get_root_path(worktree), 0);
2173 for (x = 0; x < argc; x++) {
2174 char *path = realpath(argv[x], NULL);
2176 error = got_error_from_errno2("realpath", argv[x]);
2180 got_path_strip_trailing_slashes(path);
2181 error = got_pathlist_insert(&pe, &paths, path, NULL);
2187 error = got_worktree_schedule_add(worktree, &paths, print_status,
2191 got_repo_close(repo);
2193 got_worktree_close(worktree);
2194 TAILQ_FOREACH(pe, &paths, entry)
2195 free((char *)pe->path);
2196 got_pathlist_free(&paths);
2204 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2208 static const struct got_error *
2209 cmd_rm(int argc, char *argv[])
2211 const struct got_error *error = NULL;
2212 struct got_worktree *worktree = NULL;
2213 struct got_repository *repo = NULL;
2214 char *cwd = NULL, *path = NULL;
2215 int ch, delete_local_mods = 0;
2217 while ((ch = getopt(argc, argv, "f")) != -1) {
2220 delete_local_mods = 1;
2234 path = realpath(argv[0], NULL);
2236 error = got_error_from_errno2("realpath", argv[0]);
2239 got_path_strip_trailing_slashes(path);
2241 cwd = getcwd(NULL, 0);
2243 error = got_error_from_errno("getcwd");
2246 error = got_worktree_open(&worktree, cwd);
2250 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2254 error = apply_unveil(got_repo_get_path(repo), 1,
2255 got_worktree_get_root_path(worktree), 0);
2259 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2260 print_status, NULL, repo);
2265 got_repo_close(repo);
2267 got_worktree_close(worktree);
2276 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2281 revert_progress(void *arg, unsigned char status, const char *path)
2283 while (path[0] == '/')
2285 printf("%c %s\n", status, path);
2288 static const struct got_error *
2289 cmd_revert(int argc, char *argv[])
2291 const struct got_error *error = NULL;
2292 struct got_worktree *worktree = NULL;
2293 struct got_repository *repo = NULL;
2294 char *cwd = NULL, *path = NULL;
2297 while ((ch = getopt(argc, argv, "")) != -1) {
2311 path = realpath(argv[0], NULL);
2313 error = got_error_from_errno2("realpath", argv[0]);
2316 got_path_strip_trailing_slashes(path);
2318 cwd = getcwd(NULL, 0);
2320 error = got_error_from_errno("getcwd");
2323 error = got_worktree_open(&worktree, cwd);
2327 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2331 error = apply_unveil(got_repo_get_path(repo), 1,
2332 got_worktree_get_root_path(worktree), 0);
2336 error = got_worktree_revert(worktree, path,
2337 revert_progress, NULL, repo);
2342 got_repo_close(repo);
2344 got_worktree_close(worktree);
2353 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2358 spawn_editor(const char *editor, const char *file)
2361 sig_t sighup, sigint, sigquit;
2364 sighup = signal(SIGHUP, SIG_IGN);
2365 sigint = signal(SIGINT, SIG_IGN);
2366 sigquit = signal(SIGQUIT, SIG_IGN);
2368 switch (pid = fork()) {
2372 execl(editor, editor, file, (char *)NULL);
2376 while (waitpid(pid, &st, 0) == -1)
2381 (void)signal(SIGHUP, sighup);
2382 (void)signal(SIGINT, sigint);
2383 (void)signal(SIGQUIT, sigquit);
2385 if (!WIFEXITED(st)) {
2390 return WEXITSTATUS(st);
2393 struct collect_commit_logmsg_arg {
2394 const char *cmdline_log;
2396 const char *worktree_path;
2397 const char *repo_path;
2402 static const struct got_error *
2403 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2406 const char *initial_content = "\n# changes to be committed:\n";
2407 struct got_pathlist_entry *pe;
2408 const struct got_error *err = NULL;
2409 char *template = NULL;
2410 struct collect_commit_logmsg_arg *a = arg;
2412 struct stat st, st2;
2415 int fd, content_changed = 0;
2417 /* if a message was specified on the command line, just use it */
2418 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2419 len = strlen(a->cmdline_log) + 1;
2420 *logmsg = malloc(len + 1);
2421 if (*logmsg == NULL)
2422 return got_error_from_errno("malloc");
2423 strlcpy(*logmsg, a->cmdline_log, len);
2427 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2428 return got_error_from_errno("asprintf");
2430 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2434 dprintf(fd, initial_content);
2436 TAILQ_FOREACH(pe, commitable_paths, entry) {
2437 struct got_commitable *ct = pe->data;
2438 dprintf(fd, "# %c %s\n",
2439 got_commitable_get_status(ct),
2440 got_commitable_get_path(ct));
2444 if (stat(a->logmsg_path, &st) == -1) {
2445 err = got_error_from_errno2("stat", a->logmsg_path);
2449 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2450 err = got_error_from_errno("failed spawning editor");
2454 if (stat(a->logmsg_path, &st2) == -1) {
2455 err = got_error_from_errno("stat");
2459 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2460 unlink(a->logmsg_path);
2461 free(a->logmsg_path);
2462 a->logmsg_path = NULL;
2463 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2464 "no changes made to commit message, aborting");
2468 *logmsg = malloc(st2.st_size + 1);
2469 if (*logmsg == NULL) {
2470 err = got_error_from_errno("malloc");
2473 (*logmsg)[0] = '\0';
2476 fp = fopen(a->logmsg_path, "r");
2478 err = got_error_from_errno("fopen");
2481 while (fgets(buf, sizeof(buf), fp) != NULL) {
2482 if (!content_changed && strcmp(buf, initial_content) != 0)
2483 content_changed = 1;
2484 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2485 continue; /* remove comments and leading empty lines */
2486 len = strlcat(*logmsg, buf, st2.st_size);
2490 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2491 (*logmsg)[len - 1] = '\0';
2495 if (len == 0 || !content_changed) {
2496 unlink(a->logmsg_path);
2497 free(a->logmsg_path);
2498 a->logmsg_path = NULL;
2499 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2500 "commit message cannot be empty, aborting");
2506 /* Editor is done; we can now apply unveil(2) */
2508 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2512 static const struct got_error *
2513 cmd_commit(int argc, char *argv[])
2515 const struct got_error *error = NULL;
2516 struct got_worktree *worktree = NULL;
2517 struct got_repository *repo = NULL;
2518 char *cwd = NULL, *path = NULL, *id_str = NULL;
2519 struct got_object_id *id = NULL;
2520 const char *logmsg = NULL;
2521 const char *got_author = getenv("GOT_AUTHOR");
2522 struct collect_commit_logmsg_arg cl_arg;
2523 char *editor = NULL;
2526 while ((ch = getopt(argc, argv, "m:")) != -1) {
2541 path = realpath(argv[0], NULL);
2543 error = got_error_from_errno2("realpath", argv[0]);
2546 got_path_strip_trailing_slashes(path);
2547 } else if (argc != 0)
2550 if (got_author == NULL) {
2551 /* TODO: Look current user up in password database */
2552 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2556 cwd = getcwd(NULL, 0);
2558 error = got_error_from_errno("getcwd");
2561 error = got_worktree_open(&worktree, cwd);
2565 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2570 * unveil(2) traverses exec(2); if an editor is used we have
2571 * to apply unveil after the log message has been written.
2573 if (logmsg == NULL || strlen(logmsg) == 0)
2574 error = get_editor(&editor);
2576 error = apply_unveil(got_repo_get_path(repo), 0,
2577 got_worktree_get_root_path(worktree), 0);
2581 cl_arg.editor = editor;
2582 cl_arg.cmdline_log = logmsg;
2583 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2584 cl_arg.repo_path = got_repo_get_path(repo);
2585 cl_arg.logmsg_path = NULL;
2586 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2587 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2589 if (cl_arg.logmsg_path)
2590 fprintf(stderr, "%s: log message preserved in %s\n",
2591 getprogname(), cl_arg.logmsg_path);
2595 if (cl_arg.logmsg_path)
2596 unlink(cl_arg.logmsg_path);
2598 error = got_object_id_str(&id_str, id);
2601 printf("created commit %s\n", id_str);
2604 got_repo_close(repo);
2606 got_worktree_close(worktree);
2615 usage_cherrypick(void)
2617 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2621 static const struct got_error *
2622 cmd_cherrypick(int argc, char *argv[])
2624 const struct got_error *error = NULL;
2625 struct got_worktree *worktree = NULL;
2626 struct got_repository *repo = NULL;
2627 char *cwd = NULL, *commit_id_str = NULL;
2628 struct got_object_id *commit_id = NULL;
2629 struct got_commit_object *commit = NULL;
2630 struct got_object_qid *pid;
2631 struct got_reference *head_ref = NULL;
2632 int ch, did_something = 0;
2634 while ((ch = getopt(argc, argv, "")) != -1) {
2648 cwd = getcwd(NULL, 0);
2650 error = got_error_from_errno("getcwd");
2653 error = got_worktree_open(&worktree, cwd);
2657 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2661 error = apply_unveil(got_repo_get_path(repo), 0,
2662 got_worktree_get_root_path(worktree), 0);
2666 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2667 if (error != NULL) {
2668 struct got_reference *ref;
2669 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2671 error = got_ref_open(&ref, repo, argv[0], 0);
2674 error = got_ref_resolve(&commit_id, repo, ref);
2679 error = got_object_id_str(&commit_id_str, commit_id);
2683 error = got_ref_open(&head_ref, repo,
2684 got_worktree_get_head_ref_name(worktree), 0);
2688 error = check_same_branch(commit_id, head_ref, repo);
2690 if (error->code != GOT_ERR_ANCESTRY)
2694 error = got_error(GOT_ERR_SAME_BRANCH);
2698 error = got_object_open_as_commit(&commit, repo, commit_id);
2701 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2702 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2703 commit_id, repo, update_progress, &did_something, check_cancelled,
2709 printf("merged commit %s\n", commit_id_str);
2712 got_object_commit_close(commit);
2713 free(commit_id_str);
2715 got_ref_close(head_ref);
2717 got_worktree_close(worktree);
2719 got_repo_close(repo);