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;
229 static char err_msg[MAXPATHLEN + 36];
231 if (create_worktree) {
232 /* Pre-create work tree path to avoid unveiling its parents. */
233 err = got_path_mkdir(worktree_path);
235 if (errno == EEXIST) {
236 if (got_path_dir_is_empty(worktree_path)) {
240 snprintf(err_msg, sizeof(err_msg),
241 "%s: directory exists but is not empty",
243 err = got_error_msg(GOT_ERR_BAD_PATH,
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 cmd_update(int argc, char *argv[])
574 const struct got_error *error = NULL;
575 struct got_repository *repo = NULL;
576 struct got_worktree *worktree = NULL;
577 char *worktree_path = NULL, *path = NULL;
578 struct got_object_id *commit_id = NULL;
579 char *commit_id_str = NULL;
580 const char *branch_name = NULL;
581 struct got_reference *head_ref = NULL;
582 int ch, did_something = 0;
584 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
587 branch_name = optarg;
590 commit_id_str = strdup(optarg);
591 if (commit_id_str == NULL)
592 return got_error_from_errno("strdup");
604 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
605 "unveil", NULL) == -1)
608 worktree_path = getcwd(NULL, 0);
609 if (worktree_path == NULL) {
610 error = got_error_from_errno("getcwd");
613 error = got_worktree_open(&worktree, worktree_path);
620 error = got_error_from_errno("strdup");
623 } else if (argc == 1) {
624 error = got_worktree_resolve_path(&path, worktree, argv[0]);
630 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
634 error = apply_unveil(got_repo_get_path(repo), 0,
635 got_worktree_get_root_path(worktree), 0);
639 if (branch_name == NULL)
640 branch_name = got_worktree_get_head_ref_name(worktree);
641 error = got_ref_open(&head_ref, repo, branch_name, 0);
644 if (commit_id_str == NULL) {
645 error = got_ref_resolve(&commit_id, repo, head_ref);
648 error = got_object_id_str(&commit_id_str, commit_id);
652 error = got_object_resolve_id_str(&commit_id, repo,
658 if (strcmp(got_ref_get_name(head_ref),
659 got_worktree_get_head_ref_name(worktree)) != 0) {
660 struct got_object_id *head_commit_id;
661 if (strlen(path) != 0) {
662 fprintf(stderr, "%s: switching to a different "
663 "branch requires that the entire work tree "
664 "gets updated, not just '%s'\n",
665 getprogname(), path);
666 error = got_error(GOT_ERR_BAD_PATH);
669 error = got_ref_resolve(&head_commit_id, repo, head_ref);
672 error = check_linear_ancestry(commit_id, head_commit_id, repo);
673 free(head_commit_id);
676 error = check_same_branch(commit_id, head_ref, repo);
679 error = got_worktree_set_head_ref(worktree, head_ref);
683 error = check_linear_ancestry(commit_id,
684 got_worktree_get_base_commit_id(worktree), repo);
686 if (error->code == GOT_ERR_ANCESTRY)
687 error = got_error(GOT_ERR_BRANCH_MOVED);
690 error = check_same_branch(commit_id, head_ref, repo);
695 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
697 error = got_worktree_set_base_commit_id(worktree, repo,
703 error = got_worktree_checkout_files(worktree, path, repo,
704 update_progress, &did_something, check_cancelled, NULL);
709 printf("Updated to commit %s\n", commit_id_str);
711 printf("Already up-to-date\n");
720 static const struct got_error *
721 print_patch(struct got_commit_object *commit, struct got_object_id *id,
722 int diff_context, struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_tree_object *tree1 = NULL, *tree2;
726 struct got_object_qid *qid;
727 char *id_str1 = NULL, *id_str2;
729 err = got_object_open_as_tree(&tree2, repo,
730 got_object_commit_get_tree_id(commit));
734 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
736 struct got_commit_object *pcommit;
738 err = got_object_open_as_commit(&pcommit, repo, qid->id);
742 err = got_object_open_as_tree(&tree1, repo,
743 got_object_commit_get_tree_id(pcommit));
744 got_object_commit_close(pcommit);
748 err = got_object_id_str(&id_str1, qid->id);
753 err = got_object_id_str(&id_str2, id);
757 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
758 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
761 got_object_tree_close(tree1);
762 got_object_tree_close(tree2);
769 get_datestr(time_t *time, char *datebuf)
771 char *p, *s = ctime_r(time, datebuf);
778 static const struct got_error *
779 print_commit(struct got_commit_object *commit, struct got_object_id *id,
780 struct got_repository *repo, int show_patch, int diff_context,
781 struct got_reflist_head *refs)
783 const struct got_error *err = NULL;
784 char *id_str, *datestr, *logmsg0, *logmsg, *line;
786 time_t committer_time;
787 const char *author, *committer;
788 char *refs_str = NULL;
789 struct got_reflist_entry *re;
791 SIMPLEQ_FOREACH(re, refs, entry) {
794 if (got_object_id_cmp(re->id, id) != 0)
796 name = got_ref_get_name(re->ref);
797 if (strcmp(name, GOT_REF_HEAD) == 0)
799 if (strncmp(name, "refs/", 5) == 0)
801 if (strncmp(name, "got/", 4) == 0)
803 if (strncmp(name, "heads/", 6) == 0)
805 if (strncmp(name, "remotes/", 8) == 0)
808 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
810 err = got_error_from_errno("asprintf");
816 err = got_object_id_str(&id_str, id);
820 printf("-----------------------------------------------\n");
821 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
822 refs_str ? refs_str : "", refs_str ? ")" : "");
827 printf("from: %s\n", got_object_commit_get_author(commit));
828 committer_time = got_object_commit_get_committer_time(commit);
829 datestr = get_datestr(&committer_time, datebuf);
830 printf("date: %s UTC\n", datestr);
831 author = got_object_commit_get_author(commit);
832 committer = got_object_commit_get_committer(commit);
833 if (strcmp(author, committer) != 0)
834 printf("via: %s\n", committer);
835 if (got_object_commit_get_nparents(commit) > 1) {
836 const struct got_object_id_queue *parent_ids;
837 struct got_object_qid *qid;
839 parent_ids = got_object_commit_get_parent_ids(commit);
840 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
841 err = got_object_id_str(&id_str, qid->id);
844 printf("parent %d: %s\n", n++, id_str);
849 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
851 return got_error_from_errno("strdup");
855 line = strsep(&logmsg, "\n");
857 printf(" %s\n", line);
862 err = print_patch(commit, id, diff_context, repo);
867 if (fflush(stdout) != 0 && err == NULL)
868 err = got_error_from_errno("fflush");
872 static const struct got_error *
873 print_commits(struct got_object_id *root_id, struct got_repository *repo,
874 char *path, int show_patch, int diff_context, int limit,
875 int first_parent_traversal, struct got_reflist_head *refs)
877 const struct got_error *err;
878 struct got_commit_graph *graph;
880 err = got_commit_graph_open(&graph, root_id, path,
881 first_parent_traversal, repo);
884 err = got_commit_graph_iter_start(graph, root_id, repo);
888 struct got_commit_object *commit;
889 struct got_object_id *id;
891 if (sigint_received || sigpipe_received)
894 err = got_commit_graph_iter_next(&id, graph);
896 if (err->code == GOT_ERR_ITER_COMPLETED) {
900 if (err->code != GOT_ERR_ITER_NEED_MORE)
902 err = got_commit_graph_fetch_commits(graph, 1, repo);
911 err = got_object_open_as_commit(&commit, repo, id);
914 err = print_commit(commit, id, repo, show_patch, diff_context,
916 got_object_commit_close(commit);
917 if (err || (limit && --limit == 0))
921 got_commit_graph_close(graph);
928 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
929 "[-r repository-path] [path]\n", getprogname());
933 static const struct got_error *
934 cmd_log(int argc, char *argv[])
936 const struct got_error *error;
937 struct got_repository *repo = NULL;
938 struct got_worktree *worktree = NULL;
939 struct got_commit_object *commit = NULL;
940 struct got_object_id *id = NULL;
941 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
942 char *start_commit = NULL;
943 int diff_context = 3, ch;
944 int show_patch = 0, limit = 0, first_parent_traversal = 0;
946 struct got_reflist_head refs;
951 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
957 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
963 start_commit = optarg;
966 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
969 err(1, "-C option %s", errstr);
972 limit = strtonum(optarg, 1, INT_MAX, &errstr);
974 err(1, "-l option %s", errstr);
977 first_parent_traversal = 1;
980 repo_path = realpath(optarg, NULL);
981 if (repo_path == NULL)
983 got_path_strip_trailing_slashes(repo_path);
994 cwd = getcwd(NULL, 0);
996 error = got_error_from_errno("getcwd");
1000 error = got_worktree_open(&worktree, cwd);
1001 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1008 error = got_error_from_errno("strdup");
1011 } else if (argc == 1) {
1013 error = got_worktree_resolve_path(&path, worktree,
1018 path = strdup(argv[0]);
1020 error = got_error_from_errno("strdup");
1027 if (repo_path == NULL) {
1028 repo_path = worktree ?
1029 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1031 if (repo_path == NULL) {
1032 error = got_error_from_errno("strdup");
1036 error = got_repo_open(&repo, repo_path);
1040 error = apply_unveil(got_repo_get_path(repo), 1,
1041 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1045 if (start_commit == NULL) {
1046 struct got_reference *head_ref;
1047 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1050 error = got_ref_resolve(&id, repo, head_ref);
1051 got_ref_close(head_ref);
1054 error = got_object_open_as_commit(&commit, repo, id);
1056 struct got_reference *ref;
1057 error = got_ref_open(&ref, repo, start_commit, 0);
1058 if (error == NULL) {
1060 error = got_ref_resolve(&id, repo, ref);
1064 error = got_object_get_type(&obj_type, repo, id);
1067 if (obj_type == GOT_OBJ_TYPE_TAG) {
1068 struct got_tag_object *tag;
1069 error = got_object_open_as_tag(&tag, repo, id);
1072 if (got_object_tag_get_object_type(tag) !=
1073 GOT_OBJ_TYPE_COMMIT) {
1074 got_object_tag_close(tag);
1075 error = got_error(GOT_ERR_OBJ_TYPE);
1079 id = got_object_id_dup(
1080 got_object_tag_get_object_id(tag));
1082 error = got_error_from_errno(
1083 "got_object_id_dup");
1084 got_object_tag_close(tag);
1087 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1088 error = got_error(GOT_ERR_OBJ_TYPE);
1091 error = got_object_open_as_commit(&commit, repo, id);
1095 if (commit == NULL) {
1096 error = got_object_resolve_id_str(&id, repo,
1105 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1110 path = in_repo_path;
1113 error = got_ref_list(&refs, repo);
1117 error = print_commits(id, repo, path, show_patch,
1118 diff_context, limit, first_parent_traversal, &refs);
1125 got_worktree_close(worktree);
1127 const struct got_error *repo_error;
1128 repo_error = got_repo_close(repo);
1132 got_ref_list_free(&refs);
1139 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1140 "[object1 object2 | path]\n", getprogname());
1144 struct print_diff_arg {
1145 struct got_repository *repo;
1146 struct got_worktree *worktree;
1152 static const struct got_error *
1153 print_diff(void *arg, unsigned char status, const char *path,
1154 struct got_object_id *blob_id, struct got_object_id *commit_id)
1156 struct print_diff_arg *a = arg;
1157 const struct got_error *err = NULL;
1158 struct got_blob_object *blob1 = NULL;
1160 char *abspath = NULL;
1163 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1164 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1167 if (!a->header_shown) {
1168 printf("diff %s %s\n", a->id_str,
1169 got_worktree_get_root_path(a->worktree));
1170 a->header_shown = 1;
1173 if (status != GOT_STATUS_ADD) {
1174 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1180 if (status != GOT_STATUS_DELETE) {
1181 if (asprintf(&abspath, "%s/%s",
1182 got_worktree_get_root_path(a->worktree), path) == -1) {
1183 err = got_error_from_errno("asprintf");
1187 f2 = fopen(abspath, "r");
1189 err = got_error_from_errno2("fopen", abspath);
1192 if (lstat(abspath, &sb) == -1) {
1193 err = got_error_from_errno2("lstat", abspath);
1199 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1203 got_object_blob_close(blob1);
1204 if (f2 && fclose(f2) != 0 && err == NULL)
1205 err = got_error_from_errno("fclose");
1210 static const struct got_error *
1211 cmd_diff(int argc, char *argv[])
1213 const struct got_error *error;
1214 struct got_repository *repo = NULL;
1215 struct got_worktree *worktree = NULL;
1216 char *cwd = NULL, *repo_path = NULL;
1217 struct got_object_id *id1 = NULL, *id2 = NULL;
1218 char *id_str1 = NULL, *id_str2 = NULL;
1220 int diff_context = 3, ch;
1225 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1230 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1233 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1235 err(1, "-C option %s", errstr);
1238 repo_path = realpath(optarg, NULL);
1239 if (repo_path == NULL)
1240 err(1, "-r option");
1241 got_path_strip_trailing_slashes(repo_path);
1252 cwd = getcwd(NULL, 0);
1254 error = got_error_from_errno("getcwd");
1257 error = got_worktree_open(&worktree, cwd);
1258 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1261 if (worktree == NULL) {
1262 error = got_error(GOT_ERR_NOT_WORKTREE);
1267 "-r option can't be used when diffing a work tree");
1268 repo_path = strdup(got_worktree_get_repo_path(worktree));
1269 if (repo_path == NULL) {
1270 error = got_error_from_errno("strdup");
1274 error = got_worktree_resolve_path(&path, worktree,
1281 error = got_error_from_errno("strdup");
1285 } else if (argc == 2) {
1291 if (repo_path == NULL) {
1292 repo_path = getcwd(NULL, 0);
1293 if (repo_path == NULL)
1294 return got_error_from_errno("getcwd");
1297 error = got_repo_open(&repo, repo_path);
1302 error = apply_unveil(got_repo_get_path(repo), 1,
1303 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1308 struct print_diff_arg arg;
1310 error = got_object_id_str(&id_str,
1311 got_worktree_get_base_commit_id(worktree));
1315 arg.worktree = worktree;
1316 arg.diff_context = diff_context;
1317 arg.id_str = id_str;
1318 arg.header_shown = 0;
1320 error = got_worktree_status(worktree, path, repo, print_diff,
1321 &arg, check_cancelled, NULL);
1326 error = got_object_resolve_id_str(&id1, repo, id_str1);
1330 error = got_object_resolve_id_str(&id2, repo, id_str2);
1334 error = got_object_get_type(&type1, repo, id1);
1338 error = got_object_get_type(&type2, repo, id2);
1342 if (type1 != type2) {
1343 error = got_error(GOT_ERR_OBJ_TYPE);
1348 case GOT_OBJ_TYPE_BLOB:
1349 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1350 diff_context, repo, stdout);
1352 case GOT_OBJ_TYPE_TREE:
1353 error = got_diff_objects_as_trees(id1, id2, "", "",
1354 diff_context, repo, stdout);
1356 case GOT_OBJ_TYPE_COMMIT:
1357 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1359 error = got_diff_objects_as_commits(id1, id2, diff_context,
1363 error = got_error(GOT_ERR_OBJ_TYPE);
1371 got_worktree_close(worktree);
1373 const struct got_error *repo_error;
1374 repo_error = got_repo_close(repo);
1385 "usage: %s blame [-c commit] [-r repository-path] path\n",
1390 static const struct got_error *
1391 cmd_blame(int argc, char *argv[])
1393 const struct got_error *error;
1394 struct got_repository *repo = NULL;
1395 struct got_worktree *worktree = NULL;
1396 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1397 struct got_object_id *commit_id = NULL;
1398 char *commit_id_str = NULL;
1402 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1407 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1410 commit_id_str = optarg;
1413 repo_path = realpath(optarg, NULL);
1414 if (repo_path == NULL)
1415 err(1, "-r option");
1416 got_path_strip_trailing_slashes(repo_path);
1432 cwd = getcwd(NULL, 0);
1434 error = got_error_from_errno("getcwd");
1437 if (repo_path == NULL) {
1438 error = got_worktree_open(&worktree, cwd);
1439 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1445 strdup(got_worktree_get_repo_path(worktree));
1446 if (repo_path == NULL)
1447 error = got_error_from_errno("strdup");
1451 repo_path = strdup(cwd);
1452 if (repo_path == NULL) {
1453 error = got_error_from_errno("strdup");
1459 error = got_repo_open(&repo, repo_path);
1463 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1468 const char *prefix = got_worktree_get_path_prefix(worktree);
1469 char *p, *worktree_subdir = cwd +
1470 strlen(got_worktree_get_root_path(worktree));
1471 if (asprintf(&p, "%s%s%s%s%s",
1472 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1473 worktree_subdir, worktree_subdir[0] ? "/" : "",
1475 error = got_error_from_errno("asprintf");
1478 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1481 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1486 if (commit_id_str == NULL) {
1487 struct got_reference *head_ref;
1488 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1491 error = got_ref_resolve(&commit_id, repo, head_ref);
1492 got_ref_close(head_ref);
1496 error = got_object_resolve_id_str(&commit_id, repo,
1502 error = got_blame(in_repo_path, commit_id, repo, stdout);
1509 got_worktree_close(worktree);
1511 const struct got_error *repo_error;
1512 repo_error = got_repo_close(repo);
1523 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1529 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1530 const char *root_path)
1532 int is_root_path = (strcmp(path, root_path) == 0);
1534 path += strlen(root_path);
1535 while (path[0] == '/')
1538 printf("%s%s%s%s%s\n", id ? id : "", path,
1539 is_root_path ? "" : "/", te->name,
1540 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1543 static const struct got_error *
1544 print_tree(const char *path, struct got_object_id *commit_id,
1545 int show_ids, int recurse, const char *root_path,
1546 struct got_repository *repo)
1548 const struct got_error *err = NULL;
1549 struct got_object_id *tree_id = NULL;
1550 struct got_tree_object *tree = NULL;
1551 const struct got_tree_entries *entries;
1552 struct got_tree_entry *te;
1554 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1558 err = got_object_open_as_tree(&tree, repo, tree_id);
1561 entries = got_object_tree_get_entries(tree);
1562 te = SIMPLEQ_FIRST(&entries->head);
1566 if (sigint_received || sigpipe_received)
1571 err = got_object_id_str(&id_str, te->id);
1574 if (asprintf(&id, "%s ", id_str) == -1) {
1575 err = got_error_from_errno("asprintf");
1581 print_entry(te, id, path, root_path);
1584 if (recurse && S_ISDIR(te->mode)) {
1586 if (asprintf(&child_path, "%s%s%s", path,
1587 path[0] == '/' && path[1] == '\0' ? "" : "/",
1589 err = got_error_from_errno("asprintf");
1592 err = print_tree(child_path, commit_id, show_ids, 1,
1599 te = SIMPLEQ_NEXT(te, entry);
1603 got_object_tree_close(tree);
1608 static const struct got_error *
1609 cmd_tree(int argc, char *argv[])
1611 const struct got_error *error;
1612 struct got_repository *repo = NULL;
1613 struct got_worktree *worktree = NULL;
1615 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1616 struct got_object_id *commit_id = NULL;
1617 char *commit_id_str = NULL;
1618 int show_ids = 0, recurse = 0;
1622 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1627 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1630 commit_id_str = optarg;
1633 repo_path = realpath(optarg, NULL);
1634 if (repo_path == NULL)
1635 err(1, "-r option");
1636 got_path_strip_trailing_slashes(repo_path);
1660 cwd = getcwd(NULL, 0);
1662 error = got_error_from_errno("getcwd");
1665 if (repo_path == NULL) {
1666 error = got_worktree_open(&worktree, cwd);
1667 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1673 strdup(got_worktree_get_repo_path(worktree));
1674 if (repo_path == NULL)
1675 error = got_error_from_errno("strdup");
1679 repo_path = strdup(cwd);
1680 if (repo_path == NULL) {
1681 error = got_error_from_errno("strdup");
1687 error = got_repo_open(&repo, repo_path);
1691 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1697 char *p, *worktree_subdir = cwd +
1698 strlen(got_worktree_get_root_path(worktree));
1699 if (asprintf(&p, "%s/%s",
1700 got_worktree_get_path_prefix(worktree),
1701 worktree_subdir) == -1) {
1702 error = got_error_from_errno("asprintf");
1705 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1712 if (in_repo_path == NULL) {
1713 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1718 if (commit_id_str == NULL) {
1719 struct got_reference *head_ref;
1720 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1723 error = got_ref_resolve(&commit_id, repo, head_ref);
1724 got_ref_close(head_ref);
1728 error = got_object_resolve_id_str(&commit_id, repo,
1734 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1735 in_repo_path, repo);
1742 got_worktree_close(worktree);
1744 const struct got_error *repo_error;
1745 repo_error = got_repo_close(repo);
1755 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1759 static const struct got_error *
1760 print_status(void *arg, unsigned char status, const char *path,
1761 struct got_object_id *blob_id, struct got_object_id *commit_id)
1763 printf("%c %s\n", status, path);
1767 static const struct got_error *
1768 cmd_status(int argc, char *argv[])
1770 const struct got_error *error = NULL;
1771 struct got_repository *repo = NULL;
1772 struct got_worktree *worktree = NULL;
1773 char *cwd = NULL, *path = NULL;
1776 while ((ch = getopt(argc, argv, "")) != -1) {
1788 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1792 cwd = getcwd(NULL, 0);
1794 error = got_error_from_errno("getcwd");
1798 error = got_worktree_open(&worktree, cwd);
1805 error = got_error_from_errno("strdup");
1808 } else if (argc == 1) {
1809 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1815 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1819 error = apply_unveil(got_repo_get_path(repo), 1,
1820 got_worktree_get_root_path(worktree), 0);
1824 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1825 check_cancelled, NULL);
1836 "usage: %s ref [-r repository] -l | -d name | name target\n",
1841 static const struct got_error *
1842 list_refs(struct got_repository *repo)
1844 static const struct got_error *err = NULL;
1845 struct got_reflist_head refs;
1846 struct got_reflist_entry *re;
1848 SIMPLEQ_INIT(&refs);
1849 err = got_ref_list(&refs, repo);
1853 SIMPLEQ_FOREACH(re, &refs, entry) {
1855 refstr = got_ref_to_str(re->ref);
1857 return got_error_from_errno("got_ref_to_str");
1858 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1862 got_ref_list_free(&refs);
1866 static const struct got_error *
1867 delete_ref(struct got_repository *repo, const char *refname)
1869 const struct got_error *err = NULL;
1870 struct got_reference *ref;
1872 err = got_ref_open(&ref, repo, refname, 0);
1876 err = got_ref_delete(ref, repo);
1881 static const struct got_error *
1882 add_ref(struct got_repository *repo, const char *refname, const char *target)
1884 const struct got_error *err = NULL;
1885 struct got_object_id *id;
1886 struct got_reference *ref = NULL;
1888 err = got_object_resolve_id_str(&id, repo, target);
1890 struct got_reference *target_ref;
1892 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1894 err = got_ref_open(&target_ref, repo, target, 0);
1897 err = got_ref_resolve(&id, repo, target_ref);
1898 got_ref_close(target_ref);
1903 err = got_ref_alloc(&ref, refname, id);
1907 err = got_ref_write(ref, repo);
1915 static const struct got_error *
1916 cmd_ref(int argc, char *argv[])
1918 const struct got_error *error = NULL;
1919 struct got_repository *repo = NULL;
1920 struct got_worktree *worktree = NULL;
1921 char *cwd = NULL, *repo_path = NULL;
1922 int ch, do_list = 0;
1923 const char *delref = NULL;
1925 /* TODO: Add -s option for adding symbolic references. */
1926 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1932 repo_path = realpath(optarg, NULL);
1933 if (repo_path == NULL)
1934 err(1, "-r option");
1935 got_path_strip_trailing_slashes(repo_path);
1946 if (do_list && delref)
1947 errx(1, "-l and -d options are mutually exclusive\n");
1952 if (do_list || delref) {
1955 } else if (argc != 2)
1960 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1964 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1965 "sendfd unveil", NULL) == -1)
1969 cwd = getcwd(NULL, 0);
1971 error = got_error_from_errno("getcwd");
1975 if (repo_path == NULL) {
1976 error = got_worktree_open(&worktree, cwd);
1977 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1983 strdup(got_worktree_get_repo_path(worktree));
1984 if (repo_path == NULL)
1985 error = got_error_from_errno("strdup");
1989 repo_path = strdup(cwd);
1990 if (repo_path == NULL) {
1991 error = got_error_from_errno("strdup");
1997 error = got_repo_open(&repo, repo_path);
2001 error = apply_unveil(got_repo_get_path(repo), do_list,
2002 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2007 error = list_refs(repo);
2009 error = delete_ref(repo, delref);
2011 error = add_ref(repo, argv[0], argv[1]);
2014 got_repo_close(repo);
2016 got_worktree_close(worktree);
2025 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2029 static const struct got_error *
2030 cmd_add(int argc, char *argv[])
2032 const struct got_error *error = NULL;
2033 struct got_repository *repo = NULL;
2034 struct got_worktree *worktree = NULL;
2036 struct got_pathlist_head paths;
2037 struct got_pathlist_entry *pe;
2042 while ((ch = getopt(argc, argv, "")) != -1) {
2056 /* make sure each file exists before doing anything halfway */
2057 for (x = 0; x < argc; x++) {
2058 char *path = realpath(argv[x], NULL);
2060 error = got_error_from_errno2("realpath", argv[x]);
2066 cwd = getcwd(NULL, 0);
2068 error = got_error_from_errno("getcwd");
2072 error = got_worktree_open(&worktree, cwd);
2076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2080 error = apply_unveil(got_repo_get_path(repo), 1,
2081 got_worktree_get_root_path(worktree), 0);
2085 for (x = 0; x < argc; x++) {
2086 char *path = realpath(argv[x], NULL);
2088 error = got_error_from_errno2("realpath", argv[x]);
2092 got_path_strip_trailing_slashes(path);
2093 error = got_pathlist_insert(&pe, &paths, path, NULL);
2099 error = got_worktree_schedule_add(worktree, &paths, print_status,
2103 got_repo_close(repo);
2105 got_worktree_close(worktree);
2106 TAILQ_FOREACH(pe, &paths, entry)
2107 free((char *)pe->path);
2108 got_pathlist_free(&paths);
2116 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2120 static const struct got_error *
2121 cmd_rm(int argc, char *argv[])
2123 const struct got_error *error = NULL;
2124 struct got_worktree *worktree = NULL;
2125 struct got_repository *repo = NULL;
2126 char *cwd = NULL, *path = NULL;
2127 int ch, delete_local_mods = 0;
2129 while ((ch = getopt(argc, argv, "f")) != -1) {
2132 delete_local_mods = 1;
2146 path = realpath(argv[0], NULL);
2148 error = got_error_from_errno2("realpath", argv[0]);
2151 got_path_strip_trailing_slashes(path);
2153 cwd = getcwd(NULL, 0);
2155 error = got_error_from_errno("getcwd");
2158 error = got_worktree_open(&worktree, cwd);
2162 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2166 error = apply_unveil(got_repo_get_path(repo), 1,
2167 got_worktree_get_root_path(worktree), 0);
2171 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2172 print_status, NULL, repo);
2177 got_repo_close(repo);
2179 got_worktree_close(worktree);
2188 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2193 revert_progress(void *arg, unsigned char status, const char *path)
2195 while (path[0] == '/')
2197 printf("%c %s\n", status, path);
2200 static const struct got_error *
2201 cmd_revert(int argc, char *argv[])
2203 const struct got_error *error = NULL;
2204 struct got_worktree *worktree = NULL;
2205 struct got_repository *repo = NULL;
2206 char *cwd = NULL, *path = NULL;
2209 while ((ch = getopt(argc, argv, "")) != -1) {
2223 path = realpath(argv[0], NULL);
2225 error = got_error_from_errno2("realpath", argv[0]);
2228 got_path_strip_trailing_slashes(path);
2230 cwd = getcwd(NULL, 0);
2232 error = got_error_from_errno("getcwd");
2235 error = got_worktree_open(&worktree, cwd);
2239 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2243 error = apply_unveil(got_repo_get_path(repo), 1,
2244 got_worktree_get_root_path(worktree), 0);
2248 error = got_worktree_revert(worktree, path,
2249 revert_progress, NULL, repo);
2254 got_repo_close(repo);
2256 got_worktree_close(worktree);
2265 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2270 spawn_editor(const char *editor, const char *file)
2273 sig_t sighup, sigint, sigquit;
2276 sighup = signal(SIGHUP, SIG_IGN);
2277 sigint = signal(SIGINT, SIG_IGN);
2278 sigquit = signal(SIGQUIT, SIG_IGN);
2280 switch (pid = fork()) {
2284 execl(editor, editor, file, (char *)NULL);
2288 while (waitpid(pid, &st, 0) == -1)
2293 (void)signal(SIGHUP, sighup);
2294 (void)signal(SIGINT, sigint);
2295 (void)signal(SIGQUIT, sigquit);
2297 if (!WIFEXITED(st)) {
2302 return WEXITSTATUS(st);
2305 struct collect_commit_logmsg_arg {
2306 const char *cmdline_log;
2308 const char *worktree_path;
2309 const char *repo_path;
2314 static const struct got_error *
2315 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2318 const char *initial_content = "\n# changes to be committed:\n";
2319 struct got_pathlist_entry *pe;
2320 const struct got_error *err = NULL;
2321 char *template = NULL;
2322 struct collect_commit_logmsg_arg *a = arg;
2324 struct stat st, st2;
2327 int fd, content_changed = 0;
2329 /* if a message was specified on the command line, just use it */
2330 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2331 len = strlen(a->cmdline_log) + 1;
2332 *logmsg = malloc(len + 1);
2333 if (*logmsg == NULL)
2334 return got_error_from_errno("malloc");
2335 strlcpy(*logmsg, a->cmdline_log, len);
2339 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2340 return got_error_from_errno("asprintf");
2342 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2346 dprintf(fd, initial_content);
2348 TAILQ_FOREACH(pe, commitable_paths, entry) {
2349 struct got_commitable *ct = pe->data;
2350 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2354 if (stat(a->logmsg_path, &st) == -1) {
2355 err = got_error_from_errno2("stat", a->logmsg_path);
2359 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2360 err = got_error_from_errno("failed spawning editor");
2364 if (stat(a->logmsg_path, &st2) == -1) {
2365 err = got_error_from_errno("stat");
2369 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2370 unlink(a->logmsg_path);
2371 free(a->logmsg_path);
2372 a->logmsg_path = NULL;
2373 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2374 "no changes made to commit message, aborting");
2378 /* remove comments */
2379 *logmsg = malloc(st2.st_size + 1);
2380 if (*logmsg == NULL) {
2381 err = got_error_from_errno("malloc");
2386 fp = fopen(a->logmsg_path, "r");
2387 while (fgets(buf, sizeof(buf), fp) != NULL) {
2388 if (!content_changed && strcmp(buf, initial_content) != 0)
2389 content_changed = 1;
2390 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2392 len = strlcat(*logmsg, buf, st2.st_size);
2396 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2397 (*logmsg)[len - 1] = '\0';
2401 if (len == 0 || !content_changed) {
2402 unlink(a->logmsg_path);
2403 free(a->logmsg_path);
2404 a->logmsg_path = NULL;
2405 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2406 "commit message cannot be empty, aborting");
2412 /* Editor is done; we can now apply unveil(2) */
2414 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2418 static const struct got_error *
2419 cmd_commit(int argc, char *argv[])
2421 const struct got_error *error = NULL;
2422 struct got_worktree *worktree = NULL;
2423 struct got_repository *repo = NULL;
2424 char *cwd = NULL, *path = NULL, *id_str = NULL;
2425 struct got_object_id *id = NULL;
2426 const char *logmsg = NULL;
2427 const char *got_author = getenv("GOT_AUTHOR");
2428 struct collect_commit_logmsg_arg cl_arg;
2429 char *editor = NULL;
2432 while ((ch = getopt(argc, argv, "m:")) != -1) {
2447 path = realpath(argv[0], NULL);
2449 error = got_error_from_errno2("realpath", argv[0]);
2452 got_path_strip_trailing_slashes(path);
2453 } else if (argc != 0)
2456 if (got_author == NULL) {
2457 /* TODO: Look current user up in password database */
2458 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2462 cwd = getcwd(NULL, 0);
2464 error = got_error_from_errno("getcwd");
2467 error = got_worktree_open(&worktree, cwd);
2471 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2476 * unveil(2) traverses exec(2); if an editor is used we have
2477 * to apply unveil after the log message has been written.
2479 if (logmsg == NULL || strlen(logmsg) == 0)
2480 error = get_editor(&editor);
2482 error = apply_unveil(got_repo_get_path(repo), 0,
2483 got_worktree_get_root_path(worktree), 0);
2487 cl_arg.editor = editor;
2488 cl_arg.cmdline_log = logmsg;
2489 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2490 cl_arg.repo_path = got_repo_get_path(repo);
2491 cl_arg.logmsg_path = NULL;
2492 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2493 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2495 if (cl_arg.logmsg_path)
2496 fprintf(stderr, "%s: log message preserved in %s\n",
2497 getprogname(), cl_arg.logmsg_path);
2501 if (cl_arg.logmsg_path)
2502 unlink(cl_arg.logmsg_path);
2504 error = got_object_id_str(&id_str, id);
2507 printf("created commit %s\n", id_str);
2510 got_repo_close(repo);
2512 got_worktree_close(worktree);