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 error = got_worktree_set_head_ref(worktree, head_ref);
679 error = check_linear_ancestry(commit_id,
680 got_worktree_get_base_commit_id(worktree), repo);
682 if (error->code == GOT_ERR_ANCESTRY)
683 error = got_error(GOT_ERR_BRANCH_MOVED);
686 error = check_same_branch(commit_id, head_ref, repo);
691 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
693 error = got_worktree_set_base_commit_id(worktree, repo,
699 error = got_worktree_checkout_files(worktree, path, repo,
700 update_progress, &did_something, check_cancelled, NULL);
705 printf("Updated to commit %s\n", commit_id_str);
707 printf("Already up-to-date\n");
716 static const struct got_error *
717 print_patch(struct got_commit_object *commit, struct got_object_id *id,
718 int diff_context, struct got_repository *repo)
720 const struct got_error *err = NULL;
721 struct got_tree_object *tree1 = NULL, *tree2;
722 struct got_object_qid *qid;
723 char *id_str1 = NULL, *id_str2;
725 err = got_object_open_as_tree(&tree2, repo,
726 got_object_commit_get_tree_id(commit));
730 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
732 struct got_commit_object *pcommit;
734 err = got_object_open_as_commit(&pcommit, repo, qid->id);
738 err = got_object_open_as_tree(&tree1, repo,
739 got_object_commit_get_tree_id(pcommit));
740 got_object_commit_close(pcommit);
744 err = got_object_id_str(&id_str1, qid->id);
749 err = got_object_id_str(&id_str2, id);
753 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
754 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
757 got_object_tree_close(tree1);
758 got_object_tree_close(tree2);
765 get_datestr(time_t *time, char *datebuf)
767 char *p, *s = ctime_r(time, datebuf);
774 static const struct got_error *
775 print_commit(struct got_commit_object *commit, struct got_object_id *id,
776 struct got_repository *repo, int show_patch, int diff_context,
777 struct got_reflist_head *refs)
779 const struct got_error *err = NULL;
780 char *id_str, *datestr, *logmsg0, *logmsg, *line;
782 time_t committer_time;
783 const char *author, *committer;
784 char *refs_str = NULL;
785 struct got_reflist_entry *re;
787 SIMPLEQ_FOREACH(re, refs, entry) {
790 if (got_object_id_cmp(re->id, id) != 0)
792 name = got_ref_get_name(re->ref);
793 if (strcmp(name, GOT_REF_HEAD) == 0)
795 if (strncmp(name, "refs/", 5) == 0)
797 if (strncmp(name, "got/", 4) == 0)
799 if (strncmp(name, "heads/", 6) == 0)
801 if (strncmp(name, "remotes/", 8) == 0)
804 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
806 err = got_error_from_errno("asprintf");
812 err = got_object_id_str(&id_str, id);
816 printf("-----------------------------------------------\n");
817 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
818 refs_str ? refs_str : "", refs_str ? ")" : "");
823 printf("from: %s\n", got_object_commit_get_author(commit));
824 committer_time = got_object_commit_get_committer_time(commit);
825 datestr = get_datestr(&committer_time, datebuf);
826 printf("date: %s UTC\n", datestr);
827 author = got_object_commit_get_author(commit);
828 committer = got_object_commit_get_committer(commit);
829 if (strcmp(author, committer) != 0)
830 printf("via: %s\n", committer);
831 if (got_object_commit_get_nparents(commit) > 1) {
832 const struct got_object_id_queue *parent_ids;
833 struct got_object_qid *qid;
835 parent_ids = got_object_commit_get_parent_ids(commit);
836 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
837 err = got_object_id_str(&id_str, qid->id);
840 printf("parent %d: %s\n", n++, id_str);
845 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
847 return got_error_from_errno("strdup");
851 line = strsep(&logmsg, "\n");
853 printf(" %s\n", line);
858 err = print_patch(commit, id, diff_context, repo);
863 if (fflush(stdout) != 0 && err == NULL)
864 err = got_error_from_errno("fflush");
868 static const struct got_error *
869 print_commits(struct got_object_id *root_id, struct got_repository *repo,
870 char *path, int show_patch, int diff_context, int limit,
871 int first_parent_traversal, struct got_reflist_head *refs)
873 const struct got_error *err;
874 struct got_commit_graph *graph;
876 err = got_commit_graph_open(&graph, root_id, path,
877 first_parent_traversal, repo);
880 err = got_commit_graph_iter_start(graph, root_id, repo);
884 struct got_commit_object *commit;
885 struct got_object_id *id;
887 if (sigint_received || sigpipe_received)
890 err = got_commit_graph_iter_next(&id, graph);
892 if (err->code == GOT_ERR_ITER_COMPLETED) {
896 if (err->code != GOT_ERR_ITER_NEED_MORE)
898 err = got_commit_graph_fetch_commits(graph, 1, repo);
907 err = got_object_open_as_commit(&commit, repo, id);
910 err = print_commit(commit, id, repo, show_patch, diff_context,
912 got_object_commit_close(commit);
913 if (err || (limit && --limit == 0))
917 got_commit_graph_close(graph);
924 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
925 "[-r repository-path] [path]\n", getprogname());
929 static const struct got_error *
930 cmd_log(int argc, char *argv[])
932 const struct got_error *error;
933 struct got_repository *repo = NULL;
934 struct got_worktree *worktree = NULL;
935 struct got_commit_object *commit = NULL;
936 struct got_object_id *id = NULL;
937 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
938 char *start_commit = NULL;
939 int diff_context = 3, ch;
940 int show_patch = 0, limit = 0, first_parent_traversal = 0;
942 struct got_reflist_head refs;
947 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
953 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
959 start_commit = optarg;
962 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
965 err(1, "-C option %s", errstr);
968 limit = strtonum(optarg, 1, INT_MAX, &errstr);
970 err(1, "-l option %s", errstr);
973 first_parent_traversal = 1;
976 repo_path = realpath(optarg, NULL);
977 if (repo_path == NULL)
979 got_path_strip_trailing_slashes(repo_path);
990 cwd = getcwd(NULL, 0);
992 error = got_error_from_errno("getcwd");
996 error = got_worktree_open(&worktree, cwd);
997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1004 error = got_error_from_errno("strdup");
1007 } else if (argc == 1) {
1009 error = got_worktree_resolve_path(&path, worktree,
1014 path = strdup(argv[0]);
1016 error = got_error_from_errno("strdup");
1023 if (repo_path == NULL) {
1024 repo_path = worktree ?
1025 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1027 if (repo_path == NULL) {
1028 error = got_error_from_errno("strdup");
1032 error = got_repo_open(&repo, repo_path);
1036 error = apply_unveil(got_repo_get_path(repo), 1,
1037 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1041 if (start_commit == NULL) {
1042 struct got_reference *head_ref;
1043 error = got_ref_open(&head_ref, repo,
1044 worktree ? got_worktree_get_head_ref_name(worktree)
1048 error = got_ref_resolve(&id, repo, head_ref);
1049 got_ref_close(head_ref);
1052 error = got_object_open_as_commit(&commit, repo, id);
1054 struct got_reference *ref;
1055 error = got_ref_open(&ref, repo, start_commit, 0);
1056 if (error == NULL) {
1058 error = got_ref_resolve(&id, repo, ref);
1062 error = got_object_get_type(&obj_type, repo, id);
1065 if (obj_type == GOT_OBJ_TYPE_TAG) {
1066 struct got_tag_object *tag;
1067 error = got_object_open_as_tag(&tag, repo, id);
1070 if (got_object_tag_get_object_type(tag) !=
1071 GOT_OBJ_TYPE_COMMIT) {
1072 got_object_tag_close(tag);
1073 error = got_error(GOT_ERR_OBJ_TYPE);
1077 id = got_object_id_dup(
1078 got_object_tag_get_object_id(tag));
1080 error = got_error_from_errno(
1081 "got_object_id_dup");
1082 got_object_tag_close(tag);
1085 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1086 error = got_error(GOT_ERR_OBJ_TYPE);
1089 error = got_object_open_as_commit(&commit, repo, id);
1093 if (commit == NULL) {
1094 error = got_object_resolve_id_str(&id, repo,
1103 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1108 path = in_repo_path;
1111 error = got_ref_list(&refs, repo);
1115 error = print_commits(id, repo, path, show_patch,
1116 diff_context, limit, first_parent_traversal, &refs);
1123 got_worktree_close(worktree);
1125 const struct got_error *repo_error;
1126 repo_error = got_repo_close(repo);
1130 got_ref_list_free(&refs);
1137 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1138 "[object1 object2 | path]\n", getprogname());
1142 struct print_diff_arg {
1143 struct got_repository *repo;
1144 struct got_worktree *worktree;
1150 static const struct got_error *
1151 print_diff(void *arg, unsigned char status, const char *path,
1152 struct got_object_id *blob_id, struct got_object_id *commit_id)
1154 struct print_diff_arg *a = arg;
1155 const struct got_error *err = NULL;
1156 struct got_blob_object *blob1 = NULL;
1158 char *abspath = NULL;
1161 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1162 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1165 if (!a->header_shown) {
1166 printf("diff %s %s\n", a->id_str,
1167 got_worktree_get_root_path(a->worktree));
1168 a->header_shown = 1;
1171 if (status != GOT_STATUS_ADD) {
1172 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1178 if (status != GOT_STATUS_DELETE) {
1179 if (asprintf(&abspath, "%s/%s",
1180 got_worktree_get_root_path(a->worktree), path) == -1) {
1181 err = got_error_from_errno("asprintf");
1185 f2 = fopen(abspath, "r");
1187 err = got_error_from_errno2("fopen", abspath);
1190 if (lstat(abspath, &sb) == -1) {
1191 err = got_error_from_errno2("lstat", abspath);
1197 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1201 got_object_blob_close(blob1);
1202 if (f2 && fclose(f2) != 0 && err == NULL)
1203 err = got_error_from_errno("fclose");
1208 static const struct got_error *
1209 cmd_diff(int argc, char *argv[])
1211 const struct got_error *error;
1212 struct got_repository *repo = NULL;
1213 struct got_worktree *worktree = NULL;
1214 char *cwd = NULL, *repo_path = NULL;
1215 struct got_object_id *id1 = NULL, *id2 = NULL;
1216 char *id_str1 = NULL, *id_str2 = NULL;
1218 int diff_context = 3, ch;
1223 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1228 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1231 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1233 err(1, "-C option %s", errstr);
1236 repo_path = realpath(optarg, NULL);
1237 if (repo_path == NULL)
1238 err(1, "-r option");
1239 got_path_strip_trailing_slashes(repo_path);
1250 cwd = getcwd(NULL, 0);
1252 error = got_error_from_errno("getcwd");
1255 error = got_worktree_open(&worktree, cwd);
1256 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1259 if (worktree == NULL) {
1260 error = got_error(GOT_ERR_NOT_WORKTREE);
1265 "-r option can't be used when diffing a work tree");
1266 repo_path = strdup(got_worktree_get_repo_path(worktree));
1267 if (repo_path == NULL) {
1268 error = got_error_from_errno("strdup");
1272 error = got_worktree_resolve_path(&path, worktree,
1279 error = got_error_from_errno("strdup");
1283 } else if (argc == 2) {
1289 if (repo_path == NULL) {
1290 repo_path = getcwd(NULL, 0);
1291 if (repo_path == NULL)
1292 return got_error_from_errno("getcwd");
1295 error = got_repo_open(&repo, repo_path);
1300 error = apply_unveil(got_repo_get_path(repo), 1,
1301 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1306 struct print_diff_arg arg;
1308 error = got_object_id_str(&id_str,
1309 got_worktree_get_base_commit_id(worktree));
1313 arg.worktree = worktree;
1314 arg.diff_context = diff_context;
1315 arg.id_str = id_str;
1316 arg.header_shown = 0;
1318 error = got_worktree_status(worktree, path, repo, print_diff,
1319 &arg, check_cancelled, NULL);
1324 error = got_object_resolve_id_str(&id1, repo, id_str1);
1328 error = got_object_resolve_id_str(&id2, repo, id_str2);
1332 error = got_object_get_type(&type1, repo, id1);
1336 error = got_object_get_type(&type2, repo, id2);
1340 if (type1 != type2) {
1341 error = got_error(GOT_ERR_OBJ_TYPE);
1346 case GOT_OBJ_TYPE_BLOB:
1347 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1348 diff_context, repo, stdout);
1350 case GOT_OBJ_TYPE_TREE:
1351 error = got_diff_objects_as_trees(id1, id2, "", "",
1352 diff_context, repo, stdout);
1354 case GOT_OBJ_TYPE_COMMIT:
1355 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1357 error = got_diff_objects_as_commits(id1, id2, diff_context,
1361 error = got_error(GOT_ERR_OBJ_TYPE);
1369 got_worktree_close(worktree);
1371 const struct got_error *repo_error;
1372 repo_error = got_repo_close(repo);
1383 "usage: %s blame [-c commit] [-r repository-path] path\n",
1388 static const struct got_error *
1389 cmd_blame(int argc, char *argv[])
1391 const struct got_error *error;
1392 struct got_repository *repo = NULL;
1393 struct got_worktree *worktree = NULL;
1394 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1395 struct got_object_id *commit_id = NULL;
1396 char *commit_id_str = NULL;
1400 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1405 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1408 commit_id_str = optarg;
1411 repo_path = realpath(optarg, NULL);
1412 if (repo_path == NULL)
1413 err(1, "-r option");
1414 got_path_strip_trailing_slashes(repo_path);
1430 cwd = getcwd(NULL, 0);
1432 error = got_error_from_errno("getcwd");
1435 if (repo_path == NULL) {
1436 error = got_worktree_open(&worktree, cwd);
1437 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1443 strdup(got_worktree_get_repo_path(worktree));
1444 if (repo_path == NULL)
1445 error = got_error_from_errno("strdup");
1449 repo_path = strdup(cwd);
1450 if (repo_path == NULL) {
1451 error = got_error_from_errno("strdup");
1457 error = got_repo_open(&repo, repo_path);
1461 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1466 const char *prefix = got_worktree_get_path_prefix(worktree);
1467 char *p, *worktree_subdir = cwd +
1468 strlen(got_worktree_get_root_path(worktree));
1469 if (asprintf(&p, "%s%s%s%s%s",
1470 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1471 worktree_subdir, worktree_subdir[0] ? "/" : "",
1473 error = got_error_from_errno("asprintf");
1476 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1479 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1484 if (commit_id_str == NULL) {
1485 struct got_reference *head_ref;
1486 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1489 error = got_ref_resolve(&commit_id, repo, head_ref);
1490 got_ref_close(head_ref);
1494 error = got_object_resolve_id_str(&commit_id, repo,
1500 error = got_blame(in_repo_path, commit_id, repo, stdout);
1507 got_worktree_close(worktree);
1509 const struct got_error *repo_error;
1510 repo_error = got_repo_close(repo);
1521 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1527 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1528 const char *root_path)
1530 int is_root_path = (strcmp(path, root_path) == 0);
1532 path += strlen(root_path);
1533 while (path[0] == '/')
1536 printf("%s%s%s%s%s\n", id ? id : "", path,
1537 is_root_path ? "" : "/", te->name,
1538 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1541 static const struct got_error *
1542 print_tree(const char *path, struct got_object_id *commit_id,
1543 int show_ids, int recurse, const char *root_path,
1544 struct got_repository *repo)
1546 const struct got_error *err = NULL;
1547 struct got_object_id *tree_id = NULL;
1548 struct got_tree_object *tree = NULL;
1549 const struct got_tree_entries *entries;
1550 struct got_tree_entry *te;
1552 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1556 err = got_object_open_as_tree(&tree, repo, tree_id);
1559 entries = got_object_tree_get_entries(tree);
1560 te = SIMPLEQ_FIRST(&entries->head);
1564 if (sigint_received || sigpipe_received)
1569 err = got_object_id_str(&id_str, te->id);
1572 if (asprintf(&id, "%s ", id_str) == -1) {
1573 err = got_error_from_errno("asprintf");
1579 print_entry(te, id, path, root_path);
1582 if (recurse && S_ISDIR(te->mode)) {
1584 if (asprintf(&child_path, "%s%s%s", path,
1585 path[0] == '/' && path[1] == '\0' ? "" : "/",
1587 err = got_error_from_errno("asprintf");
1590 err = print_tree(child_path, commit_id, show_ids, 1,
1597 te = SIMPLEQ_NEXT(te, entry);
1601 got_object_tree_close(tree);
1606 static const struct got_error *
1607 cmd_tree(int argc, char *argv[])
1609 const struct got_error *error;
1610 struct got_repository *repo = NULL;
1611 struct got_worktree *worktree = NULL;
1613 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1614 struct got_object_id *commit_id = NULL;
1615 char *commit_id_str = NULL;
1616 int show_ids = 0, recurse = 0;
1620 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1625 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1628 commit_id_str = optarg;
1631 repo_path = realpath(optarg, NULL);
1632 if (repo_path == NULL)
1633 err(1, "-r option");
1634 got_path_strip_trailing_slashes(repo_path);
1658 cwd = getcwd(NULL, 0);
1660 error = got_error_from_errno("getcwd");
1663 if (repo_path == NULL) {
1664 error = got_worktree_open(&worktree, cwd);
1665 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1671 strdup(got_worktree_get_repo_path(worktree));
1672 if (repo_path == NULL)
1673 error = got_error_from_errno("strdup");
1677 repo_path = strdup(cwd);
1678 if (repo_path == NULL) {
1679 error = got_error_from_errno("strdup");
1685 error = got_repo_open(&repo, repo_path);
1689 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1695 char *p, *worktree_subdir = cwd +
1696 strlen(got_worktree_get_root_path(worktree));
1697 if (asprintf(&p, "%s/%s",
1698 got_worktree_get_path_prefix(worktree),
1699 worktree_subdir) == -1) {
1700 error = got_error_from_errno("asprintf");
1703 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1710 if (in_repo_path == NULL) {
1711 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1716 if (commit_id_str == NULL) {
1717 struct got_reference *head_ref;
1718 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1721 error = got_ref_resolve(&commit_id, repo, head_ref);
1722 got_ref_close(head_ref);
1726 error = got_object_resolve_id_str(&commit_id, repo,
1732 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1733 in_repo_path, repo);
1740 got_worktree_close(worktree);
1742 const struct got_error *repo_error;
1743 repo_error = got_repo_close(repo);
1753 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1757 static const struct got_error *
1758 print_status(void *arg, unsigned char status, const char *path,
1759 struct got_object_id *blob_id, struct got_object_id *commit_id)
1761 printf("%c %s\n", status, path);
1765 static const struct got_error *
1766 cmd_status(int argc, char *argv[])
1768 const struct got_error *error = NULL;
1769 struct got_repository *repo = NULL;
1770 struct got_worktree *worktree = NULL;
1771 char *cwd = NULL, *path = NULL;
1774 while ((ch = getopt(argc, argv, "")) != -1) {
1786 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1790 cwd = getcwd(NULL, 0);
1792 error = got_error_from_errno("getcwd");
1796 error = got_worktree_open(&worktree, cwd);
1803 error = got_error_from_errno("strdup");
1806 } else if (argc == 1) {
1807 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1813 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1817 error = apply_unveil(got_repo_get_path(repo), 1,
1818 got_worktree_get_root_path(worktree), 0);
1822 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1823 check_cancelled, NULL);
1834 "usage: %s ref [-r repository] -l | -d name | name target\n",
1839 static const struct got_error *
1840 list_refs(struct got_repository *repo)
1842 static const struct got_error *err = NULL;
1843 struct got_reflist_head refs;
1844 struct got_reflist_entry *re;
1846 SIMPLEQ_INIT(&refs);
1847 err = got_ref_list(&refs, repo);
1851 SIMPLEQ_FOREACH(re, &refs, entry) {
1853 refstr = got_ref_to_str(re->ref);
1855 return got_error_from_errno("got_ref_to_str");
1856 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1860 got_ref_list_free(&refs);
1864 static const struct got_error *
1865 delete_ref(struct got_repository *repo, const char *refname)
1867 const struct got_error *err = NULL;
1868 struct got_reference *ref;
1870 err = got_ref_open(&ref, repo, refname, 0);
1874 err = got_ref_delete(ref, repo);
1879 static const struct got_error *
1880 add_ref(struct got_repository *repo, const char *refname, const char *target)
1882 const struct got_error *err = NULL;
1883 struct got_object_id *id;
1884 struct got_reference *ref = NULL;
1886 err = got_object_resolve_id_str(&id, repo, target);
1888 struct got_reference *target_ref;
1890 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1892 err = got_ref_open(&target_ref, repo, target, 0);
1895 err = got_ref_resolve(&id, repo, target_ref);
1896 got_ref_close(target_ref);
1901 err = got_ref_alloc(&ref, refname, id);
1905 err = got_ref_write(ref, repo);
1913 static const struct got_error *
1914 cmd_ref(int argc, char *argv[])
1916 const struct got_error *error = NULL;
1917 struct got_repository *repo = NULL;
1918 struct got_worktree *worktree = NULL;
1919 char *cwd = NULL, *repo_path = NULL;
1920 int ch, do_list = 0;
1921 const char *delref = NULL;
1923 /* TODO: Add -s option for adding symbolic references. */
1924 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1930 repo_path = realpath(optarg, NULL);
1931 if (repo_path == NULL)
1932 err(1, "-r option");
1933 got_path_strip_trailing_slashes(repo_path);
1944 if (do_list && delref)
1945 errx(1, "-l and -d options are mutually exclusive\n");
1950 if (do_list || delref) {
1953 } else if (argc != 2)
1958 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1962 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1963 "sendfd unveil", NULL) == -1)
1967 cwd = getcwd(NULL, 0);
1969 error = got_error_from_errno("getcwd");
1973 if (repo_path == NULL) {
1974 error = got_worktree_open(&worktree, cwd);
1975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1981 strdup(got_worktree_get_repo_path(worktree));
1982 if (repo_path == NULL)
1983 error = got_error_from_errno("strdup");
1987 repo_path = strdup(cwd);
1988 if (repo_path == NULL) {
1989 error = got_error_from_errno("strdup");
1995 error = got_repo_open(&repo, repo_path);
1999 error = apply_unveil(got_repo_get_path(repo), do_list,
2000 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2005 error = list_refs(repo);
2007 error = delete_ref(repo, delref);
2009 error = add_ref(repo, argv[0], argv[1]);
2012 got_repo_close(repo);
2014 got_worktree_close(worktree);
2023 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2027 static const struct got_error *
2028 cmd_add(int argc, char *argv[])
2030 const struct got_error *error = NULL;
2031 struct got_repository *repo = NULL;
2032 struct got_worktree *worktree = NULL;
2034 struct got_pathlist_head paths;
2035 struct got_pathlist_entry *pe;
2040 while ((ch = getopt(argc, argv, "")) != -1) {
2054 /* make sure each file exists before doing anything halfway */
2055 for (x = 0; x < argc; x++) {
2056 char *path = realpath(argv[x], NULL);
2058 error = got_error_from_errno2("realpath", argv[x]);
2064 cwd = getcwd(NULL, 0);
2066 error = got_error_from_errno("getcwd");
2070 error = got_worktree_open(&worktree, cwd);
2074 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2078 error = apply_unveil(got_repo_get_path(repo), 1,
2079 got_worktree_get_root_path(worktree), 0);
2083 for (x = 0; x < argc; x++) {
2084 char *path = realpath(argv[x], NULL);
2086 error = got_error_from_errno2("realpath", argv[x]);
2090 got_path_strip_trailing_slashes(path);
2091 error = got_pathlist_insert(&pe, &paths, path, NULL);
2097 error = got_worktree_schedule_add(worktree, &paths, print_status,
2101 got_repo_close(repo);
2103 got_worktree_close(worktree);
2104 TAILQ_FOREACH(pe, &paths, entry)
2105 free((char *)pe->path);
2106 got_pathlist_free(&paths);
2114 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2118 static const struct got_error *
2119 cmd_rm(int argc, char *argv[])
2121 const struct got_error *error = NULL;
2122 struct got_worktree *worktree = NULL;
2123 struct got_repository *repo = NULL;
2124 char *cwd = NULL, *path = NULL;
2125 int ch, delete_local_mods = 0;
2127 while ((ch = getopt(argc, argv, "f")) != -1) {
2130 delete_local_mods = 1;
2144 path = realpath(argv[0], NULL);
2146 error = got_error_from_errno2("realpath", argv[0]);
2149 got_path_strip_trailing_slashes(path);
2151 cwd = getcwd(NULL, 0);
2153 error = got_error_from_errno("getcwd");
2156 error = got_worktree_open(&worktree, cwd);
2160 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2164 error = apply_unveil(got_repo_get_path(repo), 1,
2165 got_worktree_get_root_path(worktree), 0);
2169 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2170 print_status, NULL, repo);
2175 got_repo_close(repo);
2177 got_worktree_close(worktree);
2186 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2191 revert_progress(void *arg, unsigned char status, const char *path)
2193 while (path[0] == '/')
2195 printf("%c %s\n", status, path);
2198 static const struct got_error *
2199 cmd_revert(int argc, char *argv[])
2201 const struct got_error *error = NULL;
2202 struct got_worktree *worktree = NULL;
2203 struct got_repository *repo = NULL;
2204 char *cwd = NULL, *path = NULL;
2207 while ((ch = getopt(argc, argv, "")) != -1) {
2221 path = realpath(argv[0], NULL);
2223 error = got_error_from_errno2("realpath", argv[0]);
2226 got_path_strip_trailing_slashes(path);
2228 cwd = getcwd(NULL, 0);
2230 error = got_error_from_errno("getcwd");
2233 error = got_worktree_open(&worktree, cwd);
2237 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2241 error = apply_unveil(got_repo_get_path(repo), 1,
2242 got_worktree_get_root_path(worktree), 0);
2246 error = got_worktree_revert(worktree, path,
2247 revert_progress, NULL, repo);
2252 got_repo_close(repo);
2254 got_worktree_close(worktree);
2263 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2268 spawn_editor(const char *editor, const char *file)
2271 sig_t sighup, sigint, sigquit;
2274 sighup = signal(SIGHUP, SIG_IGN);
2275 sigint = signal(SIGINT, SIG_IGN);
2276 sigquit = signal(SIGQUIT, SIG_IGN);
2278 switch (pid = fork()) {
2282 execl(editor, editor, file, (char *)NULL);
2286 while (waitpid(pid, &st, 0) == -1)
2291 (void)signal(SIGHUP, sighup);
2292 (void)signal(SIGINT, sigint);
2293 (void)signal(SIGQUIT, sigquit);
2295 if (!WIFEXITED(st)) {
2300 return WEXITSTATUS(st);
2303 struct collect_commit_logmsg_arg {
2304 const char *cmdline_log;
2306 const char *worktree_path;
2307 const char *repo_path;
2312 static const struct got_error *
2313 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2316 const char *initial_content = "\n# changes to be committed:\n";
2317 struct got_pathlist_entry *pe;
2318 const struct got_error *err = NULL;
2319 char *template = NULL;
2320 struct collect_commit_logmsg_arg *a = arg;
2322 struct stat st, st2;
2325 int fd, content_changed = 0;
2327 /* if a message was specified on the command line, just use it */
2328 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2329 len = strlen(a->cmdline_log) + 1;
2330 *logmsg = malloc(len + 1);
2331 if (*logmsg == NULL)
2332 return got_error_from_errno("malloc");
2333 strlcpy(*logmsg, a->cmdline_log, len);
2337 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2338 return got_error_from_errno("asprintf");
2340 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2344 dprintf(fd, initial_content);
2346 TAILQ_FOREACH(pe, commitable_paths, entry) {
2347 struct got_commitable *ct = pe->data;
2348 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2352 if (stat(a->logmsg_path, &st) == -1) {
2353 err = got_error_from_errno2("stat", a->logmsg_path);
2357 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2358 err = got_error_from_errno("failed spawning editor");
2362 if (stat(a->logmsg_path, &st2) == -1) {
2363 err = got_error_from_errno("stat");
2367 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2368 unlink(a->logmsg_path);
2369 free(a->logmsg_path);
2370 a->logmsg_path = NULL;
2371 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2372 "no changes made to commit message, aborting");
2376 /* remove comments */
2377 *logmsg = malloc(st2.st_size + 1);
2378 if (*logmsg == NULL) {
2379 err = got_error_from_errno("malloc");
2384 fp = fopen(a->logmsg_path, "r");
2385 while (fgets(buf, sizeof(buf), fp) != NULL) {
2386 if (!content_changed && strcmp(buf, initial_content) != 0)
2387 content_changed = 1;
2388 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2390 len = strlcat(*logmsg, buf, st2.st_size);
2394 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2395 (*logmsg)[len - 1] = '\0';
2399 if (len == 0 || !content_changed) {
2400 unlink(a->logmsg_path);
2401 free(a->logmsg_path);
2402 a->logmsg_path = NULL;
2403 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2404 "commit message cannot be empty, aborting");
2410 /* Editor is done; we can now apply unveil(2) */
2412 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2416 static const struct got_error *
2417 cmd_commit(int argc, char *argv[])
2419 const struct got_error *error = NULL;
2420 struct got_worktree *worktree = NULL;
2421 struct got_repository *repo = NULL;
2422 char *cwd = NULL, *path = NULL, *id_str = NULL;
2423 struct got_object_id *id = NULL;
2424 const char *logmsg = NULL;
2425 const char *got_author = getenv("GOT_AUTHOR");
2426 struct collect_commit_logmsg_arg cl_arg;
2427 char *editor = NULL;
2430 while ((ch = getopt(argc, argv, "m:")) != -1) {
2445 path = realpath(argv[0], NULL);
2447 error = got_error_from_errno2("realpath", argv[0]);
2450 got_path_strip_trailing_slashes(path);
2451 } else if (argc != 0)
2454 if (got_author == NULL) {
2455 /* TODO: Look current user up in password database */
2456 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2460 cwd = getcwd(NULL, 0);
2462 error = got_error_from_errno("getcwd");
2465 error = got_worktree_open(&worktree, cwd);
2469 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2474 * unveil(2) traverses exec(2); if an editor is used we have
2475 * to apply unveil after the log message has been written.
2477 if (logmsg == NULL || strlen(logmsg) == 0)
2478 error = get_editor(&editor);
2480 error = apply_unveil(got_repo_get_path(repo), 0,
2481 got_worktree_get_root_path(worktree), 0);
2485 cl_arg.editor = editor;
2486 cl_arg.cmdline_log = logmsg;
2487 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2488 cl_arg.repo_path = got_repo_get_path(repo);
2489 cl_arg.logmsg_path = NULL;
2490 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2491 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2493 if (cl_arg.logmsg_path)
2494 fprintf(stderr, "%s: log message preserved in %s\n",
2495 getprogname(), cl_arg.logmsg_path);
2499 if (cl_arg.logmsg_path)
2500 unlink(cl_arg.logmsg_path);
2502 error = got_object_id_str(&id_str, id);
2505 printf("created commit %s\n", id_str);
2508 got_repo_close(repo);
2510 got_worktree_close(worktree);