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);
90 static const struct got_error* cmd_checkout(int, char *[]);
91 static const struct got_error* cmd_update(int, char *[]);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_status(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct got_error* cmd_add(int, char *[]);
99 static const struct got_error* cmd_rm(int, char *[]);
100 static const struct got_error* cmd_revert(int, char *[]);
101 static const struct got_error* cmd_commit(int, char *[]);
103 static struct cmd got_commands[] = {
104 { "checkout", cmd_checkout, usage_checkout,
105 "check out a new work tree from a repository" },
106 { "update", cmd_update, usage_update,
107 "update a work tree to a different commit" },
108 { "log", cmd_log, usage_log,
109 "show repository history" },
110 { "diff", cmd_diff, usage_diff,
111 "compare files and directories" },
112 { "blame", cmd_blame, usage_blame,
113 "show when lines in a file were changed" },
114 { "tree", cmd_tree, usage_tree,
115 "list files and directories in repository" },
116 { "status", cmd_status, usage_status,
117 "show modification status of files" },
118 { "ref", cmd_ref, usage_ref,
119 "manage references in repository" },
120 { "add", cmd_add, usage_add,
121 "add new files to version control" },
122 { "rm", cmd_rm, usage_rm,
123 "remove a versioned file" },
124 { "revert", cmd_revert, usage_revert,
125 "revert uncommitted changes" },
126 { "commit", cmd_commit, usage_commit,
127 "write changes from work tree to repository" },
131 main(int argc, char *argv[])
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt(argc, argv, "h")) != -1) {
158 signal(SIGINT, catch_sigint);
159 signal(SIGPIPE, catch_sigpipe);
161 for (i = 0; i < nitems(got_commands); i++) {
162 const struct got_error *error;
164 cmd = &got_commands[i];
166 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
170 got_commands[i].cmd_usage();
172 error = got_commands[i].cmd_main(argc, argv);
173 if (error && !(sigint_received || sigpipe_received)) {
174 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
190 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
191 "Available commands:\n", getprogname());
192 for (i = 0; i < nitems(got_commands); i++) {
193 struct cmd *cmd = &got_commands[i];
194 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
199 static const struct got_error *
200 get_editor(char **abspath)
202 const struct got_error *err = NULL;
205 editor = getenv("VISUAL");
207 editor = getenv("EDITOR");
210 err = got_path_find_prog(abspath, editor);
215 if (*abspath == NULL) {
216 *abspath = strdup("/bin/ed");
217 if (*abspath == NULL)
218 return got_error_from_errno("strdup");
224 static const struct got_error *
225 apply_unveil(const char *repo_path, int repo_read_only,
226 const char *worktree_path, int create_worktree)
228 const struct got_error *err;
230 if (create_worktree) {
231 /* Pre-create work tree path to avoid unveiling its parents. */
232 err = got_path_mkdir(worktree_path);
234 if (errno == EEXIST) {
235 if (got_path_dir_is_empty(worktree_path)) {
239 err = got_error_path(worktree_path,
240 GOT_ERR_DIR_NOT_EMPTY);
244 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
248 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
249 return got_error_from_errno2("unveil", repo_path);
251 if (worktree_path && unveil(worktree_path, "rwc") != 0)
252 return got_error_from_errno2("unveil", worktree_path);
254 if (unveil("/tmp", "rwc") != 0)
255 return got_error_from_errno2("unveil", "/tmp");
257 err = got_privsep_unveil_exec_helpers();
261 if (unveil(NULL, NULL) != 0)
262 return got_error_from_errno("unveil");
270 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
271 "[-p prefix] repository-path [worktree-path]\n", getprogname());
276 checkout_progress(void *arg, unsigned char status, const char *path)
278 char *worktree_path = arg;
280 while (path[0] == '/')
283 printf("%c %s/%s\n", status, worktree_path, path);
286 static const struct got_error *
287 check_cancelled(void *arg)
289 if (sigint_received || sigpipe_received)
290 return got_error(GOT_ERR_CANCELLED);
294 static const struct got_error *
295 check_linear_ancestry(struct got_object_id *commit_id,
296 struct got_object_id *base_commit_id, struct got_repository *repo)
298 const struct got_error *err = NULL;
299 struct got_object_id *yca_id;
301 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
302 commit_id, base_commit_id, repo);
307 return got_error(GOT_ERR_ANCESTRY);
310 * Require a straight line of history between the target commit
311 * and the work tree's base commit.
313 * Non-linear situations such as this require a rebase:
315 * (commit) D F (base_commit)
323 * 'got update' only handles linear cases:
324 * Update forwards in time: A (base/yca) - B - C - D (commit)
325 * Update backwards in time: D (base) - C - B - A (commit/yca)
327 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
328 got_object_id_cmp(base_commit_id, yca_id) != 0)
329 return got_error(GOT_ERR_ANCESTRY);
335 static const struct got_error *
336 check_same_branch(struct got_object_id *commit_id,
337 struct got_reference *head_ref, struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_commit_graph *graph = NULL;
341 struct got_object_id *head_commit_id = NULL;
342 int is_same_branch = 0;
344 err = got_ref_resolve(&head_commit_id, repo, head_ref);
348 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
352 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
357 struct got_object_id *id;
358 err = got_commit_graph_iter_next(&id, graph);
360 if (err->code == GOT_ERR_ITER_COMPLETED) {
364 else if (err->code != GOT_ERR_ITER_NEED_MORE)
366 err = got_commit_graph_fetch_commits(graph, 1,
373 if (got_object_id_cmp(id, commit_id) == 0) {
381 got_commit_graph_close(graph);
382 free(head_commit_id);
383 if (!err && !is_same_branch)
384 err = got_error(GOT_ERR_ANCESTRY);
388 static const struct got_error *
389 cmd_checkout(int argc, char *argv[])
391 const struct got_error *error = NULL;
392 struct got_repository *repo = NULL;
393 struct got_reference *head_ref = NULL;
394 struct got_worktree *worktree = NULL;
395 char *repo_path = NULL;
396 char *worktree_path = NULL;
397 const char *path_prefix = "";
398 const char *branch_name = GOT_REF_HEAD;
399 char *commit_id_str = NULL;
400 int ch, same_path_prefix;
402 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
405 branch_name = optarg;
408 commit_id_str = strdup(optarg);
409 if (commit_id_str == NULL)
410 return got_error_from_errno("strdup");
413 path_prefix = optarg;
425 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
426 "unveil", NULL) == -1)
430 char *cwd, *base, *dotgit;
431 repo_path = realpath(argv[0], NULL);
432 if (repo_path == NULL)
433 return got_error_from_errno2("realpath", argv[0]);
434 cwd = getcwd(NULL, 0);
436 error = got_error_from_errno("getcwd");
439 if (path_prefix[0]) {
440 base = basename(path_prefix);
442 error = got_error_from_errno2("basename",
447 base = basename(repo_path);
449 error = got_error_from_errno2("basename",
454 dotgit = strstr(base, ".git");
457 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
458 error = got_error_from_errno("asprintf");
463 } else if (argc == 2) {
464 repo_path = realpath(argv[0], NULL);
465 if (repo_path == NULL) {
466 error = got_error_from_errno2("realpath", argv[0]);
469 worktree_path = realpath(argv[1], NULL);
470 if (worktree_path == NULL) {
471 error = got_error_from_errno2("realpath", argv[1]);
477 got_path_strip_trailing_slashes(repo_path);
478 got_path_strip_trailing_slashes(worktree_path);
480 error = got_repo_open(&repo, repo_path);
484 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
488 error = got_ref_open(&head_ref, repo, branch_name, 0);
492 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
493 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
496 error = got_worktree_open(&worktree, worktree_path);
500 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
504 if (!same_path_prefix) {
505 error = got_error(GOT_ERR_PATH_PREFIX);
510 struct got_object_id *commit_id;
511 error = got_object_resolve_id_str(&commit_id, repo,
515 error = check_linear_ancestry(commit_id,
516 got_worktree_get_base_commit_id(worktree), repo);
521 error = check_same_branch(commit_id, head_ref, repo);
524 error = got_worktree_set_base_commit_id(worktree, repo,
531 error = got_worktree_checkout_files(worktree, "", repo,
532 checkout_progress, worktree_path, check_cancelled, NULL);
536 printf("Now shut up and hack\n");
548 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
554 update_progress(void *arg, unsigned char status, const char *path)
556 int *did_something = arg;
558 if (status == GOT_STATUS_EXISTS)
562 while (path[0] == '/')
564 printf("%c %s\n", status, path);
567 static const struct got_error *
568 cmd_update(int argc, char *argv[])
570 const struct got_error *error = NULL;
571 struct got_repository *repo = NULL;
572 struct got_worktree *worktree = NULL;
573 char *worktree_path = NULL, *path = NULL;
574 struct got_object_id *commit_id = NULL;
575 char *commit_id_str = NULL;
576 const char *branch_name = NULL;
577 struct got_reference *head_ref = NULL;
578 int ch, did_something = 0;
580 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
583 branch_name = optarg;
586 commit_id_str = strdup(optarg);
587 if (commit_id_str == NULL)
588 return got_error_from_errno("strdup");
600 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
601 "unveil", NULL) == -1)
604 worktree_path = getcwd(NULL, 0);
605 if (worktree_path == NULL) {
606 error = got_error_from_errno("getcwd");
609 error = got_worktree_open(&worktree, worktree_path);
616 error = got_error_from_errno("strdup");
619 } else if (argc == 1) {
620 error = got_worktree_resolve_path(&path, worktree, argv[0]);
626 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
630 error = apply_unveil(got_repo_get_path(repo), 0,
631 got_worktree_get_root_path(worktree), 0);
635 if (branch_name == NULL)
636 branch_name = got_worktree_get_head_ref_name(worktree);
637 error = got_ref_open(&head_ref, repo, branch_name, 0);
640 if (commit_id_str == NULL) {
641 error = got_ref_resolve(&commit_id, repo, head_ref);
644 error = got_object_id_str(&commit_id_str, commit_id);
648 error = got_object_resolve_id_str(&commit_id, repo,
654 if (strcmp(got_ref_get_name(head_ref),
655 got_worktree_get_head_ref_name(worktree)) != 0) {
656 struct got_object_id *head_commit_id;
657 if (strlen(path) != 0) {
658 fprintf(stderr, "%s: switching to a different "
659 "branch requires that the entire work tree "
660 "gets updated, not just '%s'\n",
661 getprogname(), path);
662 error = got_error(GOT_ERR_BAD_PATH);
665 error = got_ref_resolve(&head_commit_id, repo, head_ref);
668 error = check_linear_ancestry(commit_id, head_commit_id, repo);
669 free(head_commit_id);
672 error = check_same_branch(commit_id, head_ref, repo);
675 printf("Switching work tree from %s to %s\n",
676 got_worktree_get_head_ref_name(worktree),
677 got_ref_get_name(head_ref));
678 error = got_worktree_set_head_ref(worktree, head_ref);
682 error = check_linear_ancestry(commit_id,
683 got_worktree_get_base_commit_id(worktree), repo);
685 if (error->code == GOT_ERR_ANCESTRY)
686 error = got_error(GOT_ERR_BRANCH_MOVED);
689 error = check_same_branch(commit_id, head_ref, repo);
694 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
696 error = got_worktree_set_base_commit_id(worktree, repo,
702 error = got_worktree_checkout_files(worktree, path, repo,
703 update_progress, &did_something, check_cancelled, NULL);
708 printf("Updated to commit %s\n", commit_id_str);
710 printf("Already up-to-date\n");
719 static const struct got_error *
720 print_patch(struct got_commit_object *commit, struct got_object_id *id,
721 int diff_context, struct got_repository *repo)
723 const struct got_error *err = NULL;
724 struct got_tree_object *tree1 = NULL, *tree2;
725 struct got_object_qid *qid;
726 char *id_str1 = NULL, *id_str2;
728 err = got_object_open_as_tree(&tree2, repo,
729 got_object_commit_get_tree_id(commit));
733 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
735 struct got_commit_object *pcommit;
737 err = got_object_open_as_commit(&pcommit, repo, qid->id);
741 err = got_object_open_as_tree(&tree1, repo,
742 got_object_commit_get_tree_id(pcommit));
743 got_object_commit_close(pcommit);
747 err = got_object_id_str(&id_str1, qid->id);
752 err = got_object_id_str(&id_str2, id);
756 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
757 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
760 got_object_tree_close(tree1);
761 got_object_tree_close(tree2);
768 get_datestr(time_t *time, char *datebuf)
770 char *p, *s = ctime_r(time, datebuf);
777 static const struct got_error *
778 print_commit(struct got_commit_object *commit, struct got_object_id *id,
779 struct got_repository *repo, int show_patch, int diff_context,
780 struct got_reflist_head *refs)
782 const struct got_error *err = NULL;
783 char *id_str, *datestr, *logmsg0, *logmsg, *line;
785 time_t committer_time;
786 const char *author, *committer;
787 char *refs_str = NULL;
788 struct got_reflist_entry *re;
790 SIMPLEQ_FOREACH(re, refs, entry) {
793 if (got_object_id_cmp(re->id, id) != 0)
795 name = got_ref_get_name(re->ref);
796 if (strcmp(name, GOT_REF_HEAD) == 0)
798 if (strncmp(name, "refs/", 5) == 0)
800 if (strncmp(name, "got/", 4) == 0)
802 if (strncmp(name, "heads/", 6) == 0)
804 if (strncmp(name, "remotes/", 8) == 0)
807 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
809 err = got_error_from_errno("asprintf");
815 err = got_object_id_str(&id_str, id);
819 printf("-----------------------------------------------\n");
820 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
821 refs_str ? refs_str : "", refs_str ? ")" : "");
826 printf("from: %s\n", got_object_commit_get_author(commit));
827 committer_time = got_object_commit_get_committer_time(commit);
828 datestr = get_datestr(&committer_time, datebuf);
829 printf("date: %s UTC\n", datestr);
830 author = got_object_commit_get_author(commit);
831 committer = got_object_commit_get_committer(commit);
832 if (strcmp(author, committer) != 0)
833 printf("via: %s\n", committer);
834 if (got_object_commit_get_nparents(commit) > 1) {
835 const struct got_object_id_queue *parent_ids;
836 struct got_object_qid *qid;
838 parent_ids = got_object_commit_get_parent_ids(commit);
839 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
840 err = got_object_id_str(&id_str, qid->id);
843 printf("parent %d: %s\n", n++, id_str);
848 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
850 return got_error_from_errno("strdup");
854 line = strsep(&logmsg, "\n");
856 printf(" %s\n", line);
861 err = print_patch(commit, id, diff_context, repo);
866 if (fflush(stdout) != 0 && err == NULL)
867 err = got_error_from_errno("fflush");
871 static const struct got_error *
872 print_commits(struct got_object_id *root_id, struct got_repository *repo,
873 char *path, int show_patch, int diff_context, int limit,
874 int first_parent_traversal, struct got_reflist_head *refs)
876 const struct got_error *err;
877 struct got_commit_graph *graph;
879 err = got_commit_graph_open(&graph, root_id, path,
880 first_parent_traversal, repo);
883 err = got_commit_graph_iter_start(graph, root_id, repo);
887 struct got_commit_object *commit;
888 struct got_object_id *id;
890 if (sigint_received || sigpipe_received)
893 err = got_commit_graph_iter_next(&id, graph);
895 if (err->code == GOT_ERR_ITER_COMPLETED) {
899 if (err->code != GOT_ERR_ITER_NEED_MORE)
901 err = got_commit_graph_fetch_commits(graph, 1, repo);
910 err = got_object_open_as_commit(&commit, repo, id);
913 err = print_commit(commit, id, repo, show_patch, diff_context,
915 got_object_commit_close(commit);
916 if (err || (limit && --limit == 0))
920 got_commit_graph_close(graph);
927 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
928 "[-r repository-path] [path]\n", getprogname());
932 static const struct got_error *
933 cmd_log(int argc, char *argv[])
935 const struct got_error *error;
936 struct got_repository *repo = NULL;
937 struct got_worktree *worktree = NULL;
938 struct got_commit_object *commit = NULL;
939 struct got_object_id *id = NULL;
940 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
941 char *start_commit = NULL;
942 int diff_context = 3, ch;
943 int show_patch = 0, limit = 0, first_parent_traversal = 0;
945 struct got_reflist_head refs;
950 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
956 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
959 first_parent_traversal = 1;
965 start_commit = optarg;
968 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
971 err(1, "-C option %s", errstr);
974 limit = strtonum(optarg, 1, INT_MAX, &errstr);
976 err(1, "-l option %s", errstr);
979 repo_path = realpath(optarg, NULL);
980 if (repo_path == NULL)
982 got_path_strip_trailing_slashes(repo_path);
993 cwd = getcwd(NULL, 0);
995 error = got_error_from_errno("getcwd");
999 error = got_worktree_open(&worktree, cwd);
1000 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1007 error = got_error_from_errno("strdup");
1010 } else if (argc == 1) {
1012 error = got_worktree_resolve_path(&path, worktree,
1017 path = strdup(argv[0]);
1019 error = got_error_from_errno("strdup");
1026 if (repo_path == NULL) {
1027 repo_path = worktree ?
1028 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1030 if (repo_path == NULL) {
1031 error = got_error_from_errno("strdup");
1035 error = got_repo_open(&repo, repo_path);
1039 error = apply_unveil(got_repo_get_path(repo), 1,
1040 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1044 if (start_commit == NULL) {
1045 struct got_reference *head_ref;
1046 error = got_ref_open(&head_ref, repo,
1047 worktree ? got_worktree_get_head_ref_name(worktree)
1051 error = got_ref_resolve(&id, repo, head_ref);
1052 got_ref_close(head_ref);
1055 error = got_object_open_as_commit(&commit, repo, id);
1057 struct got_reference *ref;
1058 error = got_ref_open(&ref, repo, start_commit, 0);
1059 if (error == NULL) {
1061 error = got_ref_resolve(&id, repo, ref);
1065 error = got_object_get_type(&obj_type, repo, id);
1068 if (obj_type == GOT_OBJ_TYPE_TAG) {
1069 struct got_tag_object *tag;
1070 error = got_object_open_as_tag(&tag, repo, id);
1073 if (got_object_tag_get_object_type(tag) !=
1074 GOT_OBJ_TYPE_COMMIT) {
1075 got_object_tag_close(tag);
1076 error = got_error(GOT_ERR_OBJ_TYPE);
1080 id = got_object_id_dup(
1081 got_object_tag_get_object_id(tag));
1083 error = got_error_from_errno(
1084 "got_object_id_dup");
1085 got_object_tag_close(tag);
1088 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1089 error = got_error(GOT_ERR_OBJ_TYPE);
1092 error = got_object_open_as_commit(&commit, repo, id);
1096 if (commit == NULL) {
1097 error = got_object_resolve_id_str(&id, repo,
1106 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1111 path = in_repo_path;
1114 error = got_ref_list(&refs, repo);
1118 error = print_commits(id, repo, path, show_patch,
1119 diff_context, limit, first_parent_traversal, &refs);
1126 got_worktree_close(worktree);
1128 const struct got_error *repo_error;
1129 repo_error = got_repo_close(repo);
1133 got_ref_list_free(&refs);
1140 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1141 "[object1 object2 | path]\n", getprogname());
1145 struct print_diff_arg {
1146 struct got_repository *repo;
1147 struct got_worktree *worktree;
1153 static const struct got_error *
1154 print_diff(void *arg, unsigned char status, const char *path,
1155 struct got_object_id *blob_id, struct got_object_id *commit_id)
1157 struct print_diff_arg *a = arg;
1158 const struct got_error *err = NULL;
1159 struct got_blob_object *blob1 = NULL;
1161 char *abspath = NULL;
1164 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1165 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1168 if (!a->header_shown) {
1169 printf("diff %s %s\n", a->id_str,
1170 got_worktree_get_root_path(a->worktree));
1171 a->header_shown = 1;
1174 if (status != GOT_STATUS_ADD) {
1175 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1181 if (status != GOT_STATUS_DELETE) {
1182 if (asprintf(&abspath, "%s/%s",
1183 got_worktree_get_root_path(a->worktree), path) == -1) {
1184 err = got_error_from_errno("asprintf");
1188 f2 = fopen(abspath, "r");
1190 err = got_error_from_errno2("fopen", abspath);
1193 if (lstat(abspath, &sb) == -1) {
1194 err = got_error_from_errno2("lstat", abspath);
1200 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1204 got_object_blob_close(blob1);
1205 if (f2 && fclose(f2) != 0 && err == NULL)
1206 err = got_error_from_errno("fclose");
1211 static const struct got_error *
1212 cmd_diff(int argc, char *argv[])
1214 const struct got_error *error;
1215 struct got_repository *repo = NULL;
1216 struct got_worktree *worktree = NULL;
1217 char *cwd = NULL, *repo_path = NULL;
1218 struct got_object_id *id1 = NULL, *id2 = NULL;
1219 char *id_str1 = NULL, *id_str2 = NULL;
1221 int diff_context = 3, ch;
1226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1231 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1234 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1236 err(1, "-C option %s", errstr);
1239 repo_path = realpath(optarg, NULL);
1240 if (repo_path == NULL)
1241 err(1, "-r option");
1242 got_path_strip_trailing_slashes(repo_path);
1253 cwd = getcwd(NULL, 0);
1255 error = got_error_from_errno("getcwd");
1258 error = got_worktree_open(&worktree, cwd);
1259 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1262 if (worktree == NULL) {
1263 error = got_error(GOT_ERR_NOT_WORKTREE);
1268 "-r option can't be used when diffing a work tree");
1269 repo_path = strdup(got_worktree_get_repo_path(worktree));
1270 if (repo_path == NULL) {
1271 error = got_error_from_errno("strdup");
1275 error = got_worktree_resolve_path(&path, worktree,
1282 error = got_error_from_errno("strdup");
1286 } else if (argc == 2) {
1292 if (repo_path == NULL) {
1293 repo_path = getcwd(NULL, 0);
1294 if (repo_path == NULL)
1295 return got_error_from_errno("getcwd");
1298 error = got_repo_open(&repo, repo_path);
1303 error = apply_unveil(got_repo_get_path(repo), 1,
1304 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1309 struct print_diff_arg arg;
1311 error = got_object_id_str(&id_str,
1312 got_worktree_get_base_commit_id(worktree));
1316 arg.worktree = worktree;
1317 arg.diff_context = diff_context;
1318 arg.id_str = id_str;
1319 arg.header_shown = 0;
1321 error = got_worktree_status(worktree, path, repo, print_diff,
1322 &arg, check_cancelled, NULL);
1327 error = got_object_resolve_id_str(&id1, repo, id_str1);
1331 error = got_object_resolve_id_str(&id2, repo, id_str2);
1335 error = got_object_get_type(&type1, repo, id1);
1339 error = got_object_get_type(&type2, repo, id2);
1343 if (type1 != type2) {
1344 error = got_error(GOT_ERR_OBJ_TYPE);
1349 case GOT_OBJ_TYPE_BLOB:
1350 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1351 diff_context, repo, stdout);
1353 case GOT_OBJ_TYPE_TREE:
1354 error = got_diff_objects_as_trees(id1, id2, "", "",
1355 diff_context, repo, stdout);
1357 case GOT_OBJ_TYPE_COMMIT:
1358 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1360 error = got_diff_objects_as_commits(id1, id2, diff_context,
1364 error = got_error(GOT_ERR_OBJ_TYPE);
1372 got_worktree_close(worktree);
1374 const struct got_error *repo_error;
1375 repo_error = got_repo_close(repo);
1386 "usage: %s blame [-c commit] [-r repository-path] path\n",
1391 static const struct got_error *
1392 cmd_blame(int argc, char *argv[])
1394 const struct got_error *error;
1395 struct got_repository *repo = NULL;
1396 struct got_worktree *worktree = NULL;
1397 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1398 struct got_object_id *commit_id = NULL;
1399 char *commit_id_str = NULL;
1403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1408 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1411 commit_id_str = optarg;
1414 repo_path = realpath(optarg, NULL);
1415 if (repo_path == NULL)
1416 err(1, "-r option");
1417 got_path_strip_trailing_slashes(repo_path);
1433 cwd = getcwd(NULL, 0);
1435 error = got_error_from_errno("getcwd");
1438 if (repo_path == NULL) {
1439 error = got_worktree_open(&worktree, cwd);
1440 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1446 strdup(got_worktree_get_repo_path(worktree));
1447 if (repo_path == NULL)
1448 error = got_error_from_errno("strdup");
1452 repo_path = strdup(cwd);
1453 if (repo_path == NULL) {
1454 error = got_error_from_errno("strdup");
1460 error = got_repo_open(&repo, repo_path);
1464 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1469 const char *prefix = got_worktree_get_path_prefix(worktree);
1470 char *p, *worktree_subdir = cwd +
1471 strlen(got_worktree_get_root_path(worktree));
1472 if (asprintf(&p, "%s%s%s%s%s",
1473 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1474 worktree_subdir, worktree_subdir[0] ? "/" : "",
1476 error = got_error_from_errno("asprintf");
1479 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1482 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1487 if (commit_id_str == NULL) {
1488 struct got_reference *head_ref;
1489 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1492 error = got_ref_resolve(&commit_id, repo, head_ref);
1493 got_ref_close(head_ref);
1497 error = got_object_resolve_id_str(&commit_id, repo,
1503 error = got_blame(in_repo_path, commit_id, repo, stdout);
1510 got_worktree_close(worktree);
1512 const struct got_error *repo_error;
1513 repo_error = got_repo_close(repo);
1524 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1530 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1531 const char *root_path)
1533 int is_root_path = (strcmp(path, root_path) == 0);
1535 path += strlen(root_path);
1536 while (path[0] == '/')
1539 printf("%s%s%s%s%s\n", id ? id : "", path,
1540 is_root_path ? "" : "/", te->name,
1541 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1544 static const struct got_error *
1545 print_tree(const char *path, struct got_object_id *commit_id,
1546 int show_ids, int recurse, const char *root_path,
1547 struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_object_id *tree_id = NULL;
1551 struct got_tree_object *tree = NULL;
1552 const struct got_tree_entries *entries;
1553 struct got_tree_entry *te;
1555 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1559 err = got_object_open_as_tree(&tree, repo, tree_id);
1562 entries = got_object_tree_get_entries(tree);
1563 te = SIMPLEQ_FIRST(&entries->head);
1567 if (sigint_received || sigpipe_received)
1572 err = got_object_id_str(&id_str, te->id);
1575 if (asprintf(&id, "%s ", id_str) == -1) {
1576 err = got_error_from_errno("asprintf");
1582 print_entry(te, id, path, root_path);
1585 if (recurse && S_ISDIR(te->mode)) {
1587 if (asprintf(&child_path, "%s%s%s", path,
1588 path[0] == '/' && path[1] == '\0' ? "" : "/",
1590 err = got_error_from_errno("asprintf");
1593 err = print_tree(child_path, commit_id, show_ids, 1,
1600 te = SIMPLEQ_NEXT(te, entry);
1604 got_object_tree_close(tree);
1609 static const struct got_error *
1610 cmd_tree(int argc, char *argv[])
1612 const struct got_error *error;
1613 struct got_repository *repo = NULL;
1614 struct got_worktree *worktree = NULL;
1616 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1617 struct got_object_id *commit_id = NULL;
1618 char *commit_id_str = NULL;
1619 int show_ids = 0, recurse = 0;
1623 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1628 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1631 commit_id_str = optarg;
1634 repo_path = realpath(optarg, NULL);
1635 if (repo_path == NULL)
1636 err(1, "-r option");
1637 got_path_strip_trailing_slashes(repo_path);
1661 cwd = getcwd(NULL, 0);
1663 error = got_error_from_errno("getcwd");
1666 if (repo_path == NULL) {
1667 error = got_worktree_open(&worktree, cwd);
1668 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1674 strdup(got_worktree_get_repo_path(worktree));
1675 if (repo_path == NULL)
1676 error = got_error_from_errno("strdup");
1680 repo_path = strdup(cwd);
1681 if (repo_path == NULL) {
1682 error = got_error_from_errno("strdup");
1688 error = got_repo_open(&repo, repo_path);
1692 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1698 char *p, *worktree_subdir = cwd +
1699 strlen(got_worktree_get_root_path(worktree));
1700 if (asprintf(&p, "%s/%s",
1701 got_worktree_get_path_prefix(worktree),
1702 worktree_subdir) == -1) {
1703 error = got_error_from_errno("asprintf");
1706 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1713 if (in_repo_path == NULL) {
1714 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1719 if (commit_id_str == NULL) {
1720 struct got_reference *head_ref;
1721 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1724 error = got_ref_resolve(&commit_id, repo, head_ref);
1725 got_ref_close(head_ref);
1729 error = got_object_resolve_id_str(&commit_id, repo,
1735 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1736 in_repo_path, repo);
1743 got_worktree_close(worktree);
1745 const struct got_error *repo_error;
1746 repo_error = got_repo_close(repo);
1756 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1760 static const struct got_error *
1761 print_status(void *arg, unsigned char status, const char *path,
1762 struct got_object_id *blob_id, struct got_object_id *commit_id)
1764 printf("%c %s\n", status, path);
1768 static const struct got_error *
1769 cmd_status(int argc, char *argv[])
1771 const struct got_error *error = NULL;
1772 struct got_repository *repo = NULL;
1773 struct got_worktree *worktree = NULL;
1774 char *cwd = NULL, *path = NULL;
1777 while ((ch = getopt(argc, argv, "")) != -1) {
1789 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1793 cwd = getcwd(NULL, 0);
1795 error = got_error_from_errno("getcwd");
1799 error = got_worktree_open(&worktree, cwd);
1806 error = got_error_from_errno("strdup");
1809 } else if (argc == 1) {
1810 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1816 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1820 error = apply_unveil(got_repo_get_path(repo), 1,
1821 got_worktree_get_root_path(worktree), 0);
1825 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1826 check_cancelled, NULL);
1837 "usage: %s ref [-r repository] -l | -d name | name target\n",
1842 static const struct got_error *
1843 list_refs(struct got_repository *repo)
1845 static const struct got_error *err = NULL;
1846 struct got_reflist_head refs;
1847 struct got_reflist_entry *re;
1849 SIMPLEQ_INIT(&refs);
1850 err = got_ref_list(&refs, repo);
1854 SIMPLEQ_FOREACH(re, &refs, entry) {
1856 refstr = got_ref_to_str(re->ref);
1858 return got_error_from_errno("got_ref_to_str");
1859 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1863 got_ref_list_free(&refs);
1867 static const struct got_error *
1868 delete_ref(struct got_repository *repo, const char *refname)
1870 const struct got_error *err = NULL;
1871 struct got_reference *ref;
1873 err = got_ref_open(&ref, repo, refname, 0);
1877 err = got_ref_delete(ref, repo);
1882 static const struct got_error *
1883 add_ref(struct got_repository *repo, const char *refname, const char *target)
1885 const struct got_error *err = NULL;
1886 struct got_object_id *id;
1887 struct got_reference *ref = NULL;
1889 err = got_object_resolve_id_str(&id, repo, target);
1891 struct got_reference *target_ref;
1893 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1895 err = got_ref_open(&target_ref, repo, target, 0);
1898 err = got_ref_resolve(&id, repo, target_ref);
1899 got_ref_close(target_ref);
1904 err = got_ref_alloc(&ref, refname, id);
1908 err = got_ref_write(ref, repo);
1916 static const struct got_error *
1917 cmd_ref(int argc, char *argv[])
1919 const struct got_error *error = NULL;
1920 struct got_repository *repo = NULL;
1921 struct got_worktree *worktree = NULL;
1922 char *cwd = NULL, *repo_path = NULL;
1923 int ch, do_list = 0;
1924 const char *delref = NULL;
1926 /* TODO: Add -s option for adding symbolic references. */
1927 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1933 repo_path = realpath(optarg, NULL);
1934 if (repo_path == NULL)
1935 err(1, "-r option");
1936 got_path_strip_trailing_slashes(repo_path);
1947 if (do_list && delref)
1948 errx(1, "-l and -d options are mutually exclusive\n");
1953 if (do_list || delref) {
1956 } else if (argc != 2)
1961 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1965 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1966 "sendfd unveil", NULL) == -1)
1970 cwd = getcwd(NULL, 0);
1972 error = got_error_from_errno("getcwd");
1976 if (repo_path == NULL) {
1977 error = got_worktree_open(&worktree, cwd);
1978 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1984 strdup(got_worktree_get_repo_path(worktree));
1985 if (repo_path == NULL)
1986 error = got_error_from_errno("strdup");
1990 repo_path = strdup(cwd);
1991 if (repo_path == NULL) {
1992 error = got_error_from_errno("strdup");
1998 error = got_repo_open(&repo, repo_path);
2002 error = apply_unveil(got_repo_get_path(repo), do_list,
2003 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2008 error = list_refs(repo);
2010 error = delete_ref(repo, delref);
2012 error = add_ref(repo, argv[0], argv[1]);
2015 got_repo_close(repo);
2017 got_worktree_close(worktree);
2026 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2030 static const struct got_error *
2031 cmd_add(int argc, char *argv[])
2033 const struct got_error *error = NULL;
2034 struct got_repository *repo = NULL;
2035 struct got_worktree *worktree = NULL;
2037 struct got_pathlist_head paths;
2038 struct got_pathlist_entry *pe;
2043 while ((ch = getopt(argc, argv, "")) != -1) {
2057 /* make sure each file exists before doing anything halfway */
2058 for (x = 0; x < argc; x++) {
2059 char *path = realpath(argv[x], NULL);
2061 error = got_error_from_errno2("realpath", argv[x]);
2067 cwd = getcwd(NULL, 0);
2069 error = got_error_from_errno("getcwd");
2073 error = got_worktree_open(&worktree, cwd);
2077 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2081 error = apply_unveil(got_repo_get_path(repo), 1,
2082 got_worktree_get_root_path(worktree), 0);
2086 for (x = 0; x < argc; x++) {
2087 char *path = realpath(argv[x], NULL);
2089 error = got_error_from_errno2("realpath", argv[x]);
2093 got_path_strip_trailing_slashes(path);
2094 error = got_pathlist_insert(&pe, &paths, path, NULL);
2100 error = got_worktree_schedule_add(worktree, &paths, print_status,
2104 got_repo_close(repo);
2106 got_worktree_close(worktree);
2107 TAILQ_FOREACH(pe, &paths, entry)
2108 free((char *)pe->path);
2109 got_pathlist_free(&paths);
2117 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2121 static const struct got_error *
2122 cmd_rm(int argc, char *argv[])
2124 const struct got_error *error = NULL;
2125 struct got_worktree *worktree = NULL;
2126 struct got_repository *repo = NULL;
2127 char *cwd = NULL, *path = NULL;
2128 int ch, delete_local_mods = 0;
2130 while ((ch = getopt(argc, argv, "f")) != -1) {
2133 delete_local_mods = 1;
2147 path = realpath(argv[0], NULL);
2149 error = got_error_from_errno2("realpath", argv[0]);
2152 got_path_strip_trailing_slashes(path);
2154 cwd = getcwd(NULL, 0);
2156 error = got_error_from_errno("getcwd");
2159 error = got_worktree_open(&worktree, cwd);
2163 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2167 error = apply_unveil(got_repo_get_path(repo), 1,
2168 got_worktree_get_root_path(worktree), 0);
2172 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2173 print_status, NULL, repo);
2178 got_repo_close(repo);
2180 got_worktree_close(worktree);
2189 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2194 revert_progress(void *arg, unsigned char status, const char *path)
2196 while (path[0] == '/')
2198 printf("%c %s\n", status, path);
2201 static const struct got_error *
2202 cmd_revert(int argc, char *argv[])
2204 const struct got_error *error = NULL;
2205 struct got_worktree *worktree = NULL;
2206 struct got_repository *repo = NULL;
2207 char *cwd = NULL, *path = NULL;
2210 while ((ch = getopt(argc, argv, "")) != -1) {
2224 path = realpath(argv[0], NULL);
2226 error = got_error_from_errno2("realpath", argv[0]);
2229 got_path_strip_trailing_slashes(path);
2231 cwd = getcwd(NULL, 0);
2233 error = got_error_from_errno("getcwd");
2236 error = got_worktree_open(&worktree, cwd);
2240 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2244 error = apply_unveil(got_repo_get_path(repo), 1,
2245 got_worktree_get_root_path(worktree), 0);
2249 error = got_worktree_revert(worktree, path,
2250 revert_progress, NULL, repo);
2255 got_repo_close(repo);
2257 got_worktree_close(worktree);
2266 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2271 spawn_editor(const char *editor, const char *file)
2274 sig_t sighup, sigint, sigquit;
2277 sighup = signal(SIGHUP, SIG_IGN);
2278 sigint = signal(SIGINT, SIG_IGN);
2279 sigquit = signal(SIGQUIT, SIG_IGN);
2281 switch (pid = fork()) {
2285 execl(editor, editor, file, (char *)NULL);
2289 while (waitpid(pid, &st, 0) == -1)
2294 (void)signal(SIGHUP, sighup);
2295 (void)signal(SIGINT, sigint);
2296 (void)signal(SIGQUIT, sigquit);
2298 if (!WIFEXITED(st)) {
2303 return WEXITSTATUS(st);
2306 struct collect_commit_logmsg_arg {
2307 const char *cmdline_log;
2309 const char *worktree_path;
2310 const char *repo_path;
2315 static const struct got_error *
2316 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2319 const char *initial_content = "\n# changes to be committed:\n";
2320 struct got_pathlist_entry *pe;
2321 const struct got_error *err = NULL;
2322 char *template = NULL;
2323 struct collect_commit_logmsg_arg *a = arg;
2325 struct stat st, st2;
2328 int fd, content_changed = 0;
2330 /* if a message was specified on the command line, just use it */
2331 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2332 len = strlen(a->cmdline_log) + 1;
2333 *logmsg = malloc(len + 1);
2334 if (*logmsg == NULL)
2335 return got_error_from_errno("malloc");
2336 strlcpy(*logmsg, a->cmdline_log, len);
2340 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2341 return got_error_from_errno("asprintf");
2343 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2347 dprintf(fd, initial_content);
2349 TAILQ_FOREACH(pe, commitable_paths, entry) {
2350 struct got_commitable *ct = pe->data;
2351 dprintf(fd, "# %c %s\n",
2352 got_commitable_get_status(ct),
2353 got_commitable_get_path(ct));
2357 if (stat(a->logmsg_path, &st) == -1) {
2358 err = got_error_from_errno2("stat", a->logmsg_path);
2362 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2363 err = got_error_from_errno("failed spawning editor");
2367 if (stat(a->logmsg_path, &st2) == -1) {
2368 err = got_error_from_errno("stat");
2372 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2373 unlink(a->logmsg_path);
2374 free(a->logmsg_path);
2375 a->logmsg_path = NULL;
2376 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2377 "no changes made to commit message, aborting");
2381 *logmsg = malloc(st2.st_size + 1);
2382 if (*logmsg == NULL) {
2383 err = got_error_from_errno("malloc");
2386 (*logmsg)[0] = '\0';
2389 fp = fopen(a->logmsg_path, "r");
2391 err = got_error_from_errno("fopen");
2394 while (fgets(buf, sizeof(buf), fp) != NULL) {
2395 if (!content_changed && strcmp(buf, initial_content) != 0)
2396 content_changed = 1;
2397 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2398 continue; /* remove comments and leading empty lines */
2399 len = strlcat(*logmsg, buf, st2.st_size);
2403 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2404 (*logmsg)[len - 1] = '\0';
2408 if (len == 0 || !content_changed) {
2409 unlink(a->logmsg_path);
2410 free(a->logmsg_path);
2411 a->logmsg_path = NULL;
2412 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2413 "commit message cannot be empty, aborting");
2419 /* Editor is done; we can now apply unveil(2) */
2421 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2425 static const struct got_error *
2426 cmd_commit(int argc, char *argv[])
2428 const struct got_error *error = NULL;
2429 struct got_worktree *worktree = NULL;
2430 struct got_repository *repo = NULL;
2431 char *cwd = NULL, *path = NULL, *id_str = NULL;
2432 struct got_object_id *id = NULL;
2433 const char *logmsg = NULL;
2434 const char *got_author = getenv("GOT_AUTHOR");
2435 struct collect_commit_logmsg_arg cl_arg;
2436 char *editor = NULL;
2439 while ((ch = getopt(argc, argv, "m:")) != -1) {
2454 path = realpath(argv[0], NULL);
2456 error = got_error_from_errno2("realpath", argv[0]);
2459 got_path_strip_trailing_slashes(path);
2460 } else if (argc != 0)
2463 if (got_author == NULL) {
2464 /* TODO: Look current user up in password database */
2465 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2469 cwd = getcwd(NULL, 0);
2471 error = got_error_from_errno("getcwd");
2474 error = got_worktree_open(&worktree, cwd);
2478 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2483 * unveil(2) traverses exec(2); if an editor is used we have
2484 * to apply unveil after the log message has been written.
2486 if (logmsg == NULL || strlen(logmsg) == 0)
2487 error = get_editor(&editor);
2489 error = apply_unveil(got_repo_get_path(repo), 0,
2490 got_worktree_get_root_path(worktree), 0);
2494 cl_arg.editor = editor;
2495 cl_arg.cmdline_log = logmsg;
2496 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2497 cl_arg.repo_path = got_repo_get_path(repo);
2498 cl_arg.logmsg_path = NULL;
2499 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2500 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2502 if (cl_arg.logmsg_path)
2503 fprintf(stderr, "%s: log message preserved in %s\n",
2504 getprogname(), cl_arg.logmsg_path);
2508 if (cl_arg.logmsg_path)
2509 unlink(cl_arg.logmsg_path);
2511 error = got_object_id_str(&id_str, id);
2514 printf("created commit %s\n", id_str);
2517 got_repo_close(repo);
2519 got_worktree_close(worktree);