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, "b: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,
1048 worktree ? got_worktree_get_head_ref_name(worktree)
1052 error = got_ref_resolve(&id, repo, head_ref);
1053 got_ref_close(head_ref);
1056 error = got_object_open_as_commit(&commit, repo, id);
1058 struct got_reference *ref;
1059 error = got_ref_open(&ref, repo, start_commit, 0);
1060 if (error == NULL) {
1062 error = got_ref_resolve(&id, repo, ref);
1066 error = got_object_get_type(&obj_type, repo, id);
1069 if (obj_type == GOT_OBJ_TYPE_TAG) {
1070 struct got_tag_object *tag;
1071 error = got_object_open_as_tag(&tag, repo, id);
1074 if (got_object_tag_get_object_type(tag) !=
1075 GOT_OBJ_TYPE_COMMIT) {
1076 got_object_tag_close(tag);
1077 error = got_error(GOT_ERR_OBJ_TYPE);
1081 id = got_object_id_dup(
1082 got_object_tag_get_object_id(tag));
1084 error = got_error_from_errno(
1085 "got_object_id_dup");
1086 got_object_tag_close(tag);
1089 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1090 error = got_error(GOT_ERR_OBJ_TYPE);
1093 error = got_object_open_as_commit(&commit, repo, id);
1097 if (commit == NULL) {
1098 error = got_object_resolve_id_str(&id, repo,
1107 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1112 path = in_repo_path;
1115 error = got_ref_list(&refs, repo);
1119 error = print_commits(id, repo, path, show_patch,
1120 diff_context, limit, first_parent_traversal, &refs);
1127 got_worktree_close(worktree);
1129 const struct got_error *repo_error;
1130 repo_error = got_repo_close(repo);
1134 got_ref_list_free(&refs);
1141 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1142 "[object1 object2 | path]\n", getprogname());
1146 struct print_diff_arg {
1147 struct got_repository *repo;
1148 struct got_worktree *worktree;
1154 static const struct got_error *
1155 print_diff(void *arg, unsigned char status, const char *path,
1156 struct got_object_id *blob_id, struct got_object_id *commit_id)
1158 struct print_diff_arg *a = arg;
1159 const struct got_error *err = NULL;
1160 struct got_blob_object *blob1 = NULL;
1162 char *abspath = NULL;
1165 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1166 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1169 if (!a->header_shown) {
1170 printf("diff %s %s\n", a->id_str,
1171 got_worktree_get_root_path(a->worktree));
1172 a->header_shown = 1;
1175 if (status != GOT_STATUS_ADD) {
1176 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1182 if (status != GOT_STATUS_DELETE) {
1183 if (asprintf(&abspath, "%s/%s",
1184 got_worktree_get_root_path(a->worktree), path) == -1) {
1185 err = got_error_from_errno("asprintf");
1189 f2 = fopen(abspath, "r");
1191 err = got_error_from_errno2("fopen", abspath);
1194 if (lstat(abspath, &sb) == -1) {
1195 err = got_error_from_errno2("lstat", abspath);
1201 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1205 got_object_blob_close(blob1);
1206 if (f2 && fclose(f2) != 0 && err == NULL)
1207 err = got_error_from_errno("fclose");
1212 static const struct got_error *
1213 cmd_diff(int argc, char *argv[])
1215 const struct got_error *error;
1216 struct got_repository *repo = NULL;
1217 struct got_worktree *worktree = NULL;
1218 char *cwd = NULL, *repo_path = NULL;
1219 struct got_object_id *id1 = NULL, *id2 = NULL;
1220 char *id_str1 = NULL, *id_str2 = NULL;
1222 int diff_context = 3, ch;
1227 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1232 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1235 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1237 err(1, "-C option %s", errstr);
1240 repo_path = realpath(optarg, NULL);
1241 if (repo_path == NULL)
1242 err(1, "-r option");
1243 got_path_strip_trailing_slashes(repo_path);
1254 cwd = getcwd(NULL, 0);
1256 error = got_error_from_errno("getcwd");
1259 error = got_worktree_open(&worktree, cwd);
1260 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1263 if (worktree == NULL) {
1264 error = got_error(GOT_ERR_NOT_WORKTREE);
1269 "-r option can't be used when diffing a work tree");
1270 repo_path = strdup(got_worktree_get_repo_path(worktree));
1271 if (repo_path == NULL) {
1272 error = got_error_from_errno("strdup");
1276 error = got_worktree_resolve_path(&path, worktree,
1283 error = got_error_from_errno("strdup");
1287 } else if (argc == 2) {
1293 if (repo_path == NULL) {
1294 repo_path = getcwd(NULL, 0);
1295 if (repo_path == NULL)
1296 return got_error_from_errno("getcwd");
1299 error = got_repo_open(&repo, repo_path);
1304 error = apply_unveil(got_repo_get_path(repo), 1,
1305 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1310 struct print_diff_arg arg;
1312 error = got_object_id_str(&id_str,
1313 got_worktree_get_base_commit_id(worktree));
1317 arg.worktree = worktree;
1318 arg.diff_context = diff_context;
1319 arg.id_str = id_str;
1320 arg.header_shown = 0;
1322 error = got_worktree_status(worktree, path, repo, print_diff,
1323 &arg, check_cancelled, NULL);
1328 error = got_object_resolve_id_str(&id1, repo, id_str1);
1332 error = got_object_resolve_id_str(&id2, repo, id_str2);
1336 error = got_object_get_type(&type1, repo, id1);
1340 error = got_object_get_type(&type2, repo, id2);
1344 if (type1 != type2) {
1345 error = got_error(GOT_ERR_OBJ_TYPE);
1350 case GOT_OBJ_TYPE_BLOB:
1351 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1352 diff_context, repo, stdout);
1354 case GOT_OBJ_TYPE_TREE:
1355 error = got_diff_objects_as_trees(id1, id2, "", "",
1356 diff_context, repo, stdout);
1358 case GOT_OBJ_TYPE_COMMIT:
1359 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1361 error = got_diff_objects_as_commits(id1, id2, diff_context,
1365 error = got_error(GOT_ERR_OBJ_TYPE);
1373 got_worktree_close(worktree);
1375 const struct got_error *repo_error;
1376 repo_error = got_repo_close(repo);
1387 "usage: %s blame [-c commit] [-r repository-path] path\n",
1392 static const struct got_error *
1393 cmd_blame(int argc, char *argv[])
1395 const struct got_error *error;
1396 struct got_repository *repo = NULL;
1397 struct got_worktree *worktree = NULL;
1398 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1399 struct got_object_id *commit_id = NULL;
1400 char *commit_id_str = NULL;
1404 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1409 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1412 commit_id_str = optarg;
1415 repo_path = realpath(optarg, NULL);
1416 if (repo_path == NULL)
1417 err(1, "-r option");
1418 got_path_strip_trailing_slashes(repo_path);
1434 cwd = getcwd(NULL, 0);
1436 error = got_error_from_errno("getcwd");
1439 if (repo_path == NULL) {
1440 error = got_worktree_open(&worktree, cwd);
1441 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1447 strdup(got_worktree_get_repo_path(worktree));
1448 if (repo_path == NULL)
1449 error = got_error_from_errno("strdup");
1453 repo_path = strdup(cwd);
1454 if (repo_path == NULL) {
1455 error = got_error_from_errno("strdup");
1461 error = got_repo_open(&repo, repo_path);
1465 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1470 const char *prefix = got_worktree_get_path_prefix(worktree);
1471 char *p, *worktree_subdir = cwd +
1472 strlen(got_worktree_get_root_path(worktree));
1473 if (asprintf(&p, "%s%s%s%s%s",
1474 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1475 worktree_subdir, worktree_subdir[0] ? "/" : "",
1477 error = got_error_from_errno("asprintf");
1480 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1483 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1488 if (commit_id_str == NULL) {
1489 struct got_reference *head_ref;
1490 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1493 error = got_ref_resolve(&commit_id, repo, head_ref);
1494 got_ref_close(head_ref);
1498 error = got_object_resolve_id_str(&commit_id, repo,
1504 error = got_blame(in_repo_path, commit_id, repo, stdout);
1511 got_worktree_close(worktree);
1513 const struct got_error *repo_error;
1514 repo_error = got_repo_close(repo);
1525 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1531 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1532 const char *root_path)
1534 int is_root_path = (strcmp(path, root_path) == 0);
1536 path += strlen(root_path);
1537 while (path[0] == '/')
1540 printf("%s%s%s%s%s\n", id ? id : "", path,
1541 is_root_path ? "" : "/", te->name,
1542 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1545 static const struct got_error *
1546 print_tree(const char *path, struct got_object_id *commit_id,
1547 int show_ids, int recurse, const char *root_path,
1548 struct got_repository *repo)
1550 const struct got_error *err = NULL;
1551 struct got_object_id *tree_id = NULL;
1552 struct got_tree_object *tree = NULL;
1553 const struct got_tree_entries *entries;
1554 struct got_tree_entry *te;
1556 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1560 err = got_object_open_as_tree(&tree, repo, tree_id);
1563 entries = got_object_tree_get_entries(tree);
1564 te = SIMPLEQ_FIRST(&entries->head);
1568 if (sigint_received || sigpipe_received)
1573 err = got_object_id_str(&id_str, te->id);
1576 if (asprintf(&id, "%s ", id_str) == -1) {
1577 err = got_error_from_errno("asprintf");
1583 print_entry(te, id, path, root_path);
1586 if (recurse && S_ISDIR(te->mode)) {
1588 if (asprintf(&child_path, "%s%s%s", path,
1589 path[0] == '/' && path[1] == '\0' ? "" : "/",
1591 err = got_error_from_errno("asprintf");
1594 err = print_tree(child_path, commit_id, show_ids, 1,
1601 te = SIMPLEQ_NEXT(te, entry);
1605 got_object_tree_close(tree);
1610 static const struct got_error *
1611 cmd_tree(int argc, char *argv[])
1613 const struct got_error *error;
1614 struct got_repository *repo = NULL;
1615 struct got_worktree *worktree = NULL;
1617 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1618 struct got_object_id *commit_id = NULL;
1619 char *commit_id_str = NULL;
1620 int show_ids = 0, recurse = 0;
1624 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1629 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1632 commit_id_str = optarg;
1635 repo_path = realpath(optarg, NULL);
1636 if (repo_path == NULL)
1637 err(1, "-r option");
1638 got_path_strip_trailing_slashes(repo_path);
1662 cwd = getcwd(NULL, 0);
1664 error = got_error_from_errno("getcwd");
1667 if (repo_path == NULL) {
1668 error = got_worktree_open(&worktree, cwd);
1669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1675 strdup(got_worktree_get_repo_path(worktree));
1676 if (repo_path == NULL)
1677 error = got_error_from_errno("strdup");
1681 repo_path = strdup(cwd);
1682 if (repo_path == NULL) {
1683 error = got_error_from_errno("strdup");
1689 error = got_repo_open(&repo, repo_path);
1693 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1699 char *p, *worktree_subdir = cwd +
1700 strlen(got_worktree_get_root_path(worktree));
1701 if (asprintf(&p, "%s/%s",
1702 got_worktree_get_path_prefix(worktree),
1703 worktree_subdir) == -1) {
1704 error = got_error_from_errno("asprintf");
1707 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1714 if (in_repo_path == NULL) {
1715 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1720 if (commit_id_str == NULL) {
1721 struct got_reference *head_ref;
1722 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1725 error = got_ref_resolve(&commit_id, repo, head_ref);
1726 got_ref_close(head_ref);
1730 error = got_object_resolve_id_str(&commit_id, repo,
1736 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1737 in_repo_path, repo);
1744 got_worktree_close(worktree);
1746 const struct got_error *repo_error;
1747 repo_error = got_repo_close(repo);
1757 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1761 static const struct got_error *
1762 print_status(void *arg, unsigned char status, const char *path,
1763 struct got_object_id *blob_id, struct got_object_id *commit_id)
1765 printf("%c %s\n", status, path);
1769 static const struct got_error *
1770 cmd_status(int argc, char *argv[])
1772 const struct got_error *error = NULL;
1773 struct got_repository *repo = NULL;
1774 struct got_worktree *worktree = NULL;
1775 char *cwd = NULL, *path = NULL;
1778 while ((ch = getopt(argc, argv, "")) != -1) {
1790 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1794 cwd = getcwd(NULL, 0);
1796 error = got_error_from_errno("getcwd");
1800 error = got_worktree_open(&worktree, cwd);
1807 error = got_error_from_errno("strdup");
1810 } else if (argc == 1) {
1811 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1817 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1821 error = apply_unveil(got_repo_get_path(repo), 1,
1822 got_worktree_get_root_path(worktree), 0);
1826 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1827 check_cancelled, NULL);
1838 "usage: %s ref [-r repository] -l | -d name | name target\n",
1843 static const struct got_error *
1844 list_refs(struct got_repository *repo)
1846 static const struct got_error *err = NULL;
1847 struct got_reflist_head refs;
1848 struct got_reflist_entry *re;
1850 SIMPLEQ_INIT(&refs);
1851 err = got_ref_list(&refs, repo);
1855 SIMPLEQ_FOREACH(re, &refs, entry) {
1857 refstr = got_ref_to_str(re->ref);
1859 return got_error_from_errno("got_ref_to_str");
1860 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1864 got_ref_list_free(&refs);
1868 static const struct got_error *
1869 delete_ref(struct got_repository *repo, const char *refname)
1871 const struct got_error *err = NULL;
1872 struct got_reference *ref;
1874 err = got_ref_open(&ref, repo, refname, 0);
1878 err = got_ref_delete(ref, repo);
1883 static const struct got_error *
1884 add_ref(struct got_repository *repo, const char *refname, const char *target)
1886 const struct got_error *err = NULL;
1887 struct got_object_id *id;
1888 struct got_reference *ref = NULL;
1890 err = got_object_resolve_id_str(&id, repo, target);
1892 struct got_reference *target_ref;
1894 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1896 err = got_ref_open(&target_ref, repo, target, 0);
1899 err = got_ref_resolve(&id, repo, target_ref);
1900 got_ref_close(target_ref);
1905 err = got_ref_alloc(&ref, refname, id);
1909 err = got_ref_write(ref, repo);
1917 static const struct got_error *
1918 cmd_ref(int argc, char *argv[])
1920 const struct got_error *error = NULL;
1921 struct got_repository *repo = NULL;
1922 struct got_worktree *worktree = NULL;
1923 char *cwd = NULL, *repo_path = NULL;
1924 int ch, do_list = 0;
1925 const char *delref = NULL;
1927 /* TODO: Add -s option for adding symbolic references. */
1928 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1934 repo_path = realpath(optarg, NULL);
1935 if (repo_path == NULL)
1936 err(1, "-r option");
1937 got_path_strip_trailing_slashes(repo_path);
1948 if (do_list && delref)
1949 errx(1, "-l and -d options are mutually exclusive\n");
1954 if (do_list || delref) {
1957 } else if (argc != 2)
1962 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1966 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1967 "sendfd unveil", NULL) == -1)
1971 cwd = getcwd(NULL, 0);
1973 error = got_error_from_errno("getcwd");
1977 if (repo_path == NULL) {
1978 error = got_worktree_open(&worktree, cwd);
1979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1985 strdup(got_worktree_get_repo_path(worktree));
1986 if (repo_path == NULL)
1987 error = got_error_from_errno("strdup");
1991 repo_path = strdup(cwd);
1992 if (repo_path == NULL) {
1993 error = got_error_from_errno("strdup");
1999 error = got_repo_open(&repo, repo_path);
2003 error = apply_unveil(got_repo_get_path(repo), do_list,
2004 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2009 error = list_refs(repo);
2011 error = delete_ref(repo, delref);
2013 error = add_ref(repo, argv[0], argv[1]);
2016 got_repo_close(repo);
2018 got_worktree_close(worktree);
2027 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2031 static const struct got_error *
2032 cmd_add(int argc, char *argv[])
2034 const struct got_error *error = NULL;
2035 struct got_repository *repo = NULL;
2036 struct got_worktree *worktree = NULL;
2038 struct got_pathlist_head paths;
2039 struct got_pathlist_entry *pe;
2044 while ((ch = getopt(argc, argv, "")) != -1) {
2058 /* make sure each file exists before doing anything halfway */
2059 for (x = 0; x < argc; x++) {
2060 char *path = realpath(argv[x], NULL);
2062 error = got_error_from_errno2("realpath", argv[x]);
2068 cwd = getcwd(NULL, 0);
2070 error = got_error_from_errno("getcwd");
2074 error = got_worktree_open(&worktree, cwd);
2078 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2082 error = apply_unveil(got_repo_get_path(repo), 1,
2083 got_worktree_get_root_path(worktree), 0);
2087 for (x = 0; x < argc; x++) {
2088 char *path = realpath(argv[x], NULL);
2090 error = got_error_from_errno2("realpath", argv[x]);
2094 got_path_strip_trailing_slashes(path);
2095 error = got_pathlist_insert(&pe, &paths, path, NULL);
2101 error = got_worktree_schedule_add(worktree, &paths, print_status,
2105 got_repo_close(repo);
2107 got_worktree_close(worktree);
2108 TAILQ_FOREACH(pe, &paths, entry)
2109 free((char *)pe->path);
2110 got_pathlist_free(&paths);
2118 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2122 static const struct got_error *
2123 cmd_rm(int argc, char *argv[])
2125 const struct got_error *error = NULL;
2126 struct got_worktree *worktree = NULL;
2127 struct got_repository *repo = NULL;
2128 char *cwd = NULL, *path = NULL;
2129 int ch, delete_local_mods = 0;
2131 while ((ch = getopt(argc, argv, "f")) != -1) {
2134 delete_local_mods = 1;
2148 path = realpath(argv[0], NULL);
2150 error = got_error_from_errno2("realpath", argv[0]);
2153 got_path_strip_trailing_slashes(path);
2155 cwd = getcwd(NULL, 0);
2157 error = got_error_from_errno("getcwd");
2160 error = got_worktree_open(&worktree, cwd);
2164 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2168 error = apply_unveil(got_repo_get_path(repo), 1,
2169 got_worktree_get_root_path(worktree), 0);
2173 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2174 print_status, NULL, repo);
2179 got_repo_close(repo);
2181 got_worktree_close(worktree);
2190 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2195 revert_progress(void *arg, unsigned char status, const char *path)
2197 while (path[0] == '/')
2199 printf("%c %s\n", status, path);
2202 static const struct got_error *
2203 cmd_revert(int argc, char *argv[])
2205 const struct got_error *error = NULL;
2206 struct got_worktree *worktree = NULL;
2207 struct got_repository *repo = NULL;
2208 char *cwd = NULL, *path = NULL;
2211 while ((ch = getopt(argc, argv, "")) != -1) {
2225 path = realpath(argv[0], NULL);
2227 error = got_error_from_errno2("realpath", argv[0]);
2230 got_path_strip_trailing_slashes(path);
2232 cwd = getcwd(NULL, 0);
2234 error = got_error_from_errno("getcwd");
2237 error = got_worktree_open(&worktree, cwd);
2241 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2245 error = apply_unveil(got_repo_get_path(repo), 1,
2246 got_worktree_get_root_path(worktree), 0);
2250 error = got_worktree_revert(worktree, path,
2251 revert_progress, NULL, repo);
2256 got_repo_close(repo);
2258 got_worktree_close(worktree);
2267 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2272 spawn_editor(const char *editor, const char *file)
2275 sig_t sighup, sigint, sigquit;
2278 sighup = signal(SIGHUP, SIG_IGN);
2279 sigint = signal(SIGINT, SIG_IGN);
2280 sigquit = signal(SIGQUIT, SIG_IGN);
2282 switch (pid = fork()) {
2286 execl(editor, editor, file, (char *)NULL);
2290 while (waitpid(pid, &st, 0) == -1)
2295 (void)signal(SIGHUP, sighup);
2296 (void)signal(SIGINT, sigint);
2297 (void)signal(SIGQUIT, sigquit);
2299 if (!WIFEXITED(st)) {
2304 return WEXITSTATUS(st);
2307 struct collect_commit_logmsg_arg {
2308 const char *cmdline_log;
2310 const char *worktree_path;
2311 const char *repo_path;
2316 static const struct got_error *
2317 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2320 const char *initial_content = "\n# changes to be committed:\n";
2321 struct got_pathlist_entry *pe;
2322 const struct got_error *err = NULL;
2323 char *template = NULL;
2324 struct collect_commit_logmsg_arg *a = arg;
2326 struct stat st, st2;
2329 int fd, content_changed = 0;
2331 /* if a message was specified on the command line, just use it */
2332 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2333 len = strlen(a->cmdline_log) + 1;
2334 *logmsg = malloc(len + 1);
2335 if (*logmsg == NULL)
2336 return got_error_from_errno("malloc");
2337 strlcpy(*logmsg, a->cmdline_log, len);
2341 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2342 return got_error_from_errno("asprintf");
2344 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2348 dprintf(fd, initial_content);
2350 TAILQ_FOREACH(pe, commitable_paths, entry) {
2351 struct got_commitable *ct = pe->data;
2352 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2356 if (stat(a->logmsg_path, &st) == -1) {
2357 err = got_error_from_errno2("stat", a->logmsg_path);
2361 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2362 err = got_error_from_errno("failed spawning editor");
2366 if (stat(a->logmsg_path, &st2) == -1) {
2367 err = got_error_from_errno("stat");
2371 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2372 unlink(a->logmsg_path);
2373 free(a->logmsg_path);
2374 a->logmsg_path = NULL;
2375 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2376 "no changes made to commit message, aborting");
2380 /* remove comments */
2381 *logmsg = malloc(st2.st_size + 1);
2382 if (*logmsg == NULL) {
2383 err = got_error_from_errno("malloc");
2388 fp = fopen(a->logmsg_path, "r");
2389 while (fgets(buf, sizeof(buf), fp) != NULL) {
2390 if (!content_changed && strcmp(buf, initial_content) != 0)
2391 content_changed = 1;
2392 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2394 len = strlcat(*logmsg, buf, st2.st_size);
2398 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2399 (*logmsg)[len - 1] = '\0';
2403 if (len == 0 || !content_changed) {
2404 unlink(a->logmsg_path);
2405 free(a->logmsg_path);
2406 a->logmsg_path = NULL;
2407 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2408 "commit message cannot be empty, aborting");
2414 /* Editor is done; we can now apply unveil(2) */
2416 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2420 static const struct got_error *
2421 cmd_commit(int argc, char *argv[])
2423 const struct got_error *error = NULL;
2424 struct got_worktree *worktree = NULL;
2425 struct got_repository *repo = NULL;
2426 char *cwd = NULL, *path = NULL, *id_str = NULL;
2427 struct got_object_id *id = NULL;
2428 const char *logmsg = NULL;
2429 const char *got_author = getenv("GOT_AUTHOR");
2430 struct collect_commit_logmsg_arg cl_arg;
2431 char *editor = NULL;
2434 while ((ch = getopt(argc, argv, "m:")) != -1) {
2449 path = realpath(argv[0], NULL);
2451 error = got_error_from_errno2("realpath", argv[0]);
2454 got_path_strip_trailing_slashes(path);
2455 } else if (argc != 0)
2458 if (got_author == NULL) {
2459 /* TODO: Look current user up in password database */
2460 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2464 cwd = getcwd(NULL, 0);
2466 error = got_error_from_errno("getcwd");
2469 error = got_worktree_open(&worktree, cwd);
2473 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2478 * unveil(2) traverses exec(2); if an editor is used we have
2479 * to apply unveil after the log message has been written.
2481 if (logmsg == NULL || strlen(logmsg) == 0)
2482 error = get_editor(&editor);
2484 error = apply_unveil(got_repo_get_path(repo), 0,
2485 got_worktree_get_root_path(worktree), 0);
2489 cl_arg.editor = editor;
2490 cl_arg.cmdline_log = logmsg;
2491 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2492 cl_arg.repo_path = got_repo_get_path(repo);
2493 cl_arg.logmsg_path = NULL;
2494 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2495 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2497 if (cl_arg.logmsg_path)
2498 fprintf(stderr, "%s: log message preserved in %s\n",
2499 getprogname(), cl_arg.logmsg_path);
2503 if (cl_arg.logmsg_path)
2504 unlink(cl_arg.logmsg_path);
2506 error = got_object_id_str(&id_str, id);
2509 printf("created commit %s\n", id_str);
2512 got_repo_close(repo);
2514 got_worktree_close(worktree);