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 switch_head_ref(struct got_reference *head_ref,
569 struct got_object_id *commit_id, struct got_worktree *worktree,
570 struct got_repository *repo)
572 const struct got_error *err = NULL;
574 int ref_has_moved = 0;
576 /* Trivial case: switching between two different references. */
577 if (strcmp(got_ref_get_name(head_ref),
578 got_worktree_get_head_ref_name(worktree)) != 0) {
579 printf("Switching work tree from %s to %s\n",
580 got_worktree_get_head_ref_name(worktree),
581 got_ref_get_name(head_ref));
582 return got_worktree_set_head_ref(worktree, head_ref);
585 err = check_linear_ancestry(commit_id,
586 got_worktree_get_base_commit_id(worktree), repo);
588 if (err->code != GOT_ERR_ANCESTRY)
595 /* Switching to a rebased branch with the same reference name. */
596 err = got_object_id_str(&base_id_str,
597 got_worktree_get_base_commit_id(worktree));
600 printf("Reference %s now points at a different branch\n",
601 got_worktree_get_head_ref_name(worktree));
602 printf("Switching work tree from %s to %s\n", base_id_str,
603 got_worktree_get_head_ref_name(worktree));
607 static const struct got_error *
608 cmd_update(int argc, char *argv[])
610 const struct got_error *error = NULL;
611 struct got_repository *repo = NULL;
612 struct got_worktree *worktree = NULL;
613 char *worktree_path = NULL, *path = NULL;
614 struct got_object_id *commit_id = NULL;
615 char *commit_id_str = NULL;
616 const char *branch_name = NULL;
617 struct got_reference *head_ref = NULL;
618 int ch, did_something = 0;
620 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
623 branch_name = optarg;
626 commit_id_str = strdup(optarg);
627 if (commit_id_str == NULL)
628 return got_error_from_errno("strdup");
640 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
641 "unveil", NULL) == -1)
644 worktree_path = getcwd(NULL, 0);
645 if (worktree_path == NULL) {
646 error = got_error_from_errno("getcwd");
649 error = got_worktree_open(&worktree, worktree_path);
656 error = got_error_from_errno("strdup");
659 } else if (argc == 1) {
660 error = got_worktree_resolve_path(&path, worktree, argv[0]);
666 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
670 error = apply_unveil(got_repo_get_path(repo), 0,
671 got_worktree_get_root_path(worktree), 0);
675 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
676 got_worktree_get_head_ref_name(worktree), 0);
679 if (commit_id_str == NULL) {
680 error = got_ref_resolve(&commit_id, repo, head_ref);
683 error = got_object_id_str(&commit_id_str, commit_id);
687 error = got_object_resolve_id_str(&commit_id, repo,
694 struct got_object_id *head_commit_id;
695 if (strlen(path) != 0) {
696 fprintf(stderr, "%s: switching between branches "
697 "requires that the entire work tree "
698 "gets updated, not just '%s'\n",
699 getprogname(), path);
700 error = got_error(GOT_ERR_BAD_PATH);
703 error = got_ref_resolve(&head_commit_id, repo, head_ref);
706 error = check_linear_ancestry(commit_id, head_commit_id, repo);
707 free(head_commit_id);
710 error = check_same_branch(commit_id, head_ref, repo);
713 error = switch_head_ref(head_ref, commit_id, worktree, repo);
717 error = check_linear_ancestry(commit_id,
718 got_worktree_get_base_commit_id(worktree), repo);
720 if (error->code == GOT_ERR_ANCESTRY)
721 error = got_error(GOT_ERR_BRANCH_MOVED);
724 error = check_same_branch(commit_id, head_ref, repo);
729 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
731 error = got_worktree_set_base_commit_id(worktree, repo,
737 error = got_worktree_checkout_files(worktree, path, repo,
738 update_progress, &did_something, check_cancelled, NULL);
743 printf("Updated to commit %s\n", commit_id_str);
745 printf("Already up-to-date\n");
754 static const struct got_error *
755 print_patch(struct got_commit_object *commit, struct got_object_id *id,
756 int diff_context, struct got_repository *repo)
758 const struct got_error *err = NULL;
759 struct got_tree_object *tree1 = NULL, *tree2;
760 struct got_object_qid *qid;
761 char *id_str1 = NULL, *id_str2;
763 err = got_object_open_as_tree(&tree2, repo,
764 got_object_commit_get_tree_id(commit));
768 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
770 struct got_commit_object *pcommit;
772 err = got_object_open_as_commit(&pcommit, repo, qid->id);
776 err = got_object_open_as_tree(&tree1, repo,
777 got_object_commit_get_tree_id(pcommit));
778 got_object_commit_close(pcommit);
782 err = got_object_id_str(&id_str1, qid->id);
787 err = got_object_id_str(&id_str2, id);
791 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
792 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
795 got_object_tree_close(tree1);
796 got_object_tree_close(tree2);
803 get_datestr(time_t *time, char *datebuf)
805 char *p, *s = ctime_r(time, datebuf);
812 static const struct got_error *
813 print_commit(struct got_commit_object *commit, struct got_object_id *id,
814 struct got_repository *repo, int show_patch, int diff_context,
815 struct got_reflist_head *refs)
817 const struct got_error *err = NULL;
818 char *id_str, *datestr, *logmsg0, *logmsg, *line;
820 time_t committer_time;
821 const char *author, *committer;
822 char *refs_str = NULL;
823 struct got_reflist_entry *re;
825 SIMPLEQ_FOREACH(re, refs, entry) {
828 if (got_object_id_cmp(re->id, id) != 0)
830 name = got_ref_get_name(re->ref);
831 if (strcmp(name, GOT_REF_HEAD) == 0)
833 if (strncmp(name, "refs/", 5) == 0)
835 if (strncmp(name, "got/", 4) == 0)
837 if (strncmp(name, "heads/", 6) == 0)
839 if (strncmp(name, "remotes/", 8) == 0)
842 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
844 err = got_error_from_errno("asprintf");
850 err = got_object_id_str(&id_str, id);
854 printf("-----------------------------------------------\n");
855 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
856 refs_str ? refs_str : "", refs_str ? ")" : "");
861 printf("from: %s\n", got_object_commit_get_author(commit));
862 committer_time = got_object_commit_get_committer_time(commit);
863 datestr = get_datestr(&committer_time, datebuf);
864 printf("date: %s UTC\n", datestr);
865 author = got_object_commit_get_author(commit);
866 committer = got_object_commit_get_committer(commit);
867 if (strcmp(author, committer) != 0)
868 printf("via: %s\n", committer);
869 if (got_object_commit_get_nparents(commit) > 1) {
870 const struct got_object_id_queue *parent_ids;
871 struct got_object_qid *qid;
873 parent_ids = got_object_commit_get_parent_ids(commit);
874 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
875 err = got_object_id_str(&id_str, qid->id);
878 printf("parent %d: %s\n", n++, id_str);
883 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
885 return got_error_from_errno("strdup");
889 line = strsep(&logmsg, "\n");
891 printf(" %s\n", line);
896 err = print_patch(commit, id, diff_context, repo);
901 if (fflush(stdout) != 0 && err == NULL)
902 err = got_error_from_errno("fflush");
906 static const struct got_error *
907 print_commits(struct got_object_id *root_id, struct got_repository *repo,
908 char *path, int show_patch, int diff_context, int limit,
909 int first_parent_traversal, struct got_reflist_head *refs)
911 const struct got_error *err;
912 struct got_commit_graph *graph;
914 err = got_commit_graph_open(&graph, root_id, path,
915 first_parent_traversal, repo);
918 err = got_commit_graph_iter_start(graph, root_id, repo);
922 struct got_commit_object *commit;
923 struct got_object_id *id;
925 if (sigint_received || sigpipe_received)
928 err = got_commit_graph_iter_next(&id, graph);
930 if (err->code == GOT_ERR_ITER_COMPLETED) {
934 if (err->code != GOT_ERR_ITER_NEED_MORE)
936 err = got_commit_graph_fetch_commits(graph, 1, repo);
945 err = got_object_open_as_commit(&commit, repo, id);
948 err = print_commit(commit, id, repo, show_patch, diff_context,
950 got_object_commit_close(commit);
951 if (err || (limit && --limit == 0))
955 got_commit_graph_close(graph);
962 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
963 "[-r repository-path] [path]\n", getprogname());
967 static const struct got_error *
968 cmd_log(int argc, char *argv[])
970 const struct got_error *error;
971 struct got_repository *repo = NULL;
972 struct got_worktree *worktree = NULL;
973 struct got_commit_object *commit = NULL;
974 struct got_object_id *id = NULL;
975 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
976 char *start_commit = NULL;
977 int diff_context = 3, ch;
978 int show_patch = 0, limit = 0, first_parent_traversal = 0;
980 struct got_reflist_head refs;
985 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
991 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
994 first_parent_traversal = 1;
1000 start_commit = optarg;
1003 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1006 err(1, "-C option %s", errstr);
1009 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1011 err(1, "-l option %s", errstr);
1014 repo_path = realpath(optarg, NULL);
1015 if (repo_path == NULL)
1016 err(1, "-r option");
1017 got_path_strip_trailing_slashes(repo_path);
1028 cwd = getcwd(NULL, 0);
1030 error = got_error_from_errno("getcwd");
1034 error = got_worktree_open(&worktree, cwd);
1035 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1042 error = got_error_from_errno("strdup");
1045 } else if (argc == 1) {
1047 error = got_worktree_resolve_path(&path, worktree,
1052 path = strdup(argv[0]);
1054 error = got_error_from_errno("strdup");
1061 if (repo_path == NULL) {
1062 repo_path = worktree ?
1063 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1065 if (repo_path == NULL) {
1066 error = got_error_from_errno("strdup");
1070 error = got_repo_open(&repo, repo_path);
1074 error = apply_unveil(got_repo_get_path(repo), 1,
1075 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1079 if (start_commit == NULL) {
1080 struct got_reference *head_ref;
1081 error = got_ref_open(&head_ref, repo,
1082 worktree ? got_worktree_get_head_ref_name(worktree)
1086 error = got_ref_resolve(&id, repo, head_ref);
1087 got_ref_close(head_ref);
1090 error = got_object_open_as_commit(&commit, repo, id);
1092 struct got_reference *ref;
1093 error = got_ref_open(&ref, repo, start_commit, 0);
1094 if (error == NULL) {
1096 error = got_ref_resolve(&id, repo, ref);
1100 error = got_object_get_type(&obj_type, repo, id);
1103 if (obj_type == GOT_OBJ_TYPE_TAG) {
1104 struct got_tag_object *tag;
1105 error = got_object_open_as_tag(&tag, repo, id);
1108 if (got_object_tag_get_object_type(tag) !=
1109 GOT_OBJ_TYPE_COMMIT) {
1110 got_object_tag_close(tag);
1111 error = got_error(GOT_ERR_OBJ_TYPE);
1115 id = got_object_id_dup(
1116 got_object_tag_get_object_id(tag));
1118 error = got_error_from_errno(
1119 "got_object_id_dup");
1120 got_object_tag_close(tag);
1123 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1124 error = got_error(GOT_ERR_OBJ_TYPE);
1127 error = got_object_open_as_commit(&commit, repo, id);
1131 if (commit == NULL) {
1132 error = got_object_resolve_id_str(&id, repo,
1141 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1146 path = in_repo_path;
1149 error = got_ref_list(&refs, repo);
1153 error = print_commits(id, repo, path, show_patch,
1154 diff_context, limit, first_parent_traversal, &refs);
1161 got_worktree_close(worktree);
1163 const struct got_error *repo_error;
1164 repo_error = got_repo_close(repo);
1168 got_ref_list_free(&refs);
1175 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1176 "[object1 object2 | path]\n", getprogname());
1180 struct print_diff_arg {
1181 struct got_repository *repo;
1182 struct got_worktree *worktree;
1188 static const struct got_error *
1189 print_diff(void *arg, unsigned char status, const char *path,
1190 struct got_object_id *blob_id, struct got_object_id *commit_id)
1192 struct print_diff_arg *a = arg;
1193 const struct got_error *err = NULL;
1194 struct got_blob_object *blob1 = NULL;
1196 char *abspath = NULL;
1199 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1200 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1203 if (!a->header_shown) {
1204 printf("diff %s %s\n", a->id_str,
1205 got_worktree_get_root_path(a->worktree));
1206 a->header_shown = 1;
1209 if (status != GOT_STATUS_ADD) {
1210 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1216 if (status != GOT_STATUS_DELETE) {
1217 if (asprintf(&abspath, "%s/%s",
1218 got_worktree_get_root_path(a->worktree), path) == -1) {
1219 err = got_error_from_errno("asprintf");
1223 f2 = fopen(abspath, "r");
1225 err = got_error_from_errno2("fopen", abspath);
1228 if (lstat(abspath, &sb) == -1) {
1229 err = got_error_from_errno2("lstat", abspath);
1235 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1239 got_object_blob_close(blob1);
1240 if (f2 && fclose(f2) != 0 && err == NULL)
1241 err = got_error_from_errno("fclose");
1246 static const struct got_error *
1247 cmd_diff(int argc, char *argv[])
1249 const struct got_error *error;
1250 struct got_repository *repo = NULL;
1251 struct got_worktree *worktree = NULL;
1252 char *cwd = NULL, *repo_path = NULL;
1253 struct got_object_id *id1 = NULL, *id2 = NULL;
1254 char *id_str1 = NULL, *id_str2 = NULL;
1256 int diff_context = 3, ch;
1261 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1266 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1269 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1271 err(1, "-C option %s", errstr);
1274 repo_path = realpath(optarg, NULL);
1275 if (repo_path == NULL)
1276 err(1, "-r option");
1277 got_path_strip_trailing_slashes(repo_path);
1288 cwd = getcwd(NULL, 0);
1290 error = got_error_from_errno("getcwd");
1293 error = got_worktree_open(&worktree, cwd);
1294 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1297 if (worktree == NULL) {
1298 error = got_error(GOT_ERR_NOT_WORKTREE);
1303 "-r option can't be used when diffing a work tree");
1304 repo_path = strdup(got_worktree_get_repo_path(worktree));
1305 if (repo_path == NULL) {
1306 error = got_error_from_errno("strdup");
1310 error = got_worktree_resolve_path(&path, worktree,
1317 error = got_error_from_errno("strdup");
1321 } else if (argc == 2) {
1327 if (repo_path == NULL) {
1328 repo_path = getcwd(NULL, 0);
1329 if (repo_path == NULL)
1330 return got_error_from_errno("getcwd");
1333 error = got_repo_open(&repo, repo_path);
1338 error = apply_unveil(got_repo_get_path(repo), 1,
1339 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1344 struct print_diff_arg arg;
1346 error = got_object_id_str(&id_str,
1347 got_worktree_get_base_commit_id(worktree));
1351 arg.worktree = worktree;
1352 arg.diff_context = diff_context;
1353 arg.id_str = id_str;
1354 arg.header_shown = 0;
1356 error = got_worktree_status(worktree, path, repo, print_diff,
1357 &arg, check_cancelled, NULL);
1362 error = got_object_resolve_id_str(&id1, repo, id_str1);
1366 error = got_object_resolve_id_str(&id2, repo, id_str2);
1370 error = got_object_get_type(&type1, repo, id1);
1374 error = got_object_get_type(&type2, repo, id2);
1378 if (type1 != type2) {
1379 error = got_error(GOT_ERR_OBJ_TYPE);
1384 case GOT_OBJ_TYPE_BLOB:
1385 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1386 diff_context, repo, stdout);
1388 case GOT_OBJ_TYPE_TREE:
1389 error = got_diff_objects_as_trees(id1, id2, "", "",
1390 diff_context, repo, stdout);
1392 case GOT_OBJ_TYPE_COMMIT:
1393 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1395 error = got_diff_objects_as_commits(id1, id2, diff_context,
1399 error = got_error(GOT_ERR_OBJ_TYPE);
1407 got_worktree_close(worktree);
1409 const struct got_error *repo_error;
1410 repo_error = got_repo_close(repo);
1421 "usage: %s blame [-c commit] [-r repository-path] path\n",
1426 static const struct got_error *
1427 cmd_blame(int argc, char *argv[])
1429 const struct got_error *error;
1430 struct got_repository *repo = NULL;
1431 struct got_worktree *worktree = NULL;
1432 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1433 struct got_object_id *commit_id = NULL;
1434 char *commit_id_str = NULL;
1438 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1443 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1446 commit_id_str = optarg;
1449 repo_path = realpath(optarg, NULL);
1450 if (repo_path == NULL)
1451 err(1, "-r option");
1452 got_path_strip_trailing_slashes(repo_path);
1468 cwd = getcwd(NULL, 0);
1470 error = got_error_from_errno("getcwd");
1473 if (repo_path == NULL) {
1474 error = got_worktree_open(&worktree, cwd);
1475 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1481 strdup(got_worktree_get_repo_path(worktree));
1482 if (repo_path == NULL)
1483 error = got_error_from_errno("strdup");
1487 repo_path = strdup(cwd);
1488 if (repo_path == NULL) {
1489 error = got_error_from_errno("strdup");
1495 error = got_repo_open(&repo, repo_path);
1499 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1504 const char *prefix = got_worktree_get_path_prefix(worktree);
1505 char *p, *worktree_subdir = cwd +
1506 strlen(got_worktree_get_root_path(worktree));
1507 if (asprintf(&p, "%s%s%s%s%s",
1508 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1509 worktree_subdir, worktree_subdir[0] ? "/" : "",
1511 error = got_error_from_errno("asprintf");
1514 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1517 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1522 if (commit_id_str == NULL) {
1523 struct got_reference *head_ref;
1524 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1527 error = got_ref_resolve(&commit_id, repo, head_ref);
1528 got_ref_close(head_ref);
1532 error = got_object_resolve_id_str(&commit_id, repo,
1538 error = got_blame(in_repo_path, commit_id, repo, stdout);
1545 got_worktree_close(worktree);
1547 const struct got_error *repo_error;
1548 repo_error = got_repo_close(repo);
1559 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1565 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1566 const char *root_path)
1568 int is_root_path = (strcmp(path, root_path) == 0);
1570 path += strlen(root_path);
1571 while (path[0] == '/')
1574 printf("%s%s%s%s%s\n", id ? id : "", path,
1575 is_root_path ? "" : "/", te->name,
1576 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1579 static const struct got_error *
1580 print_tree(const char *path, struct got_object_id *commit_id,
1581 int show_ids, int recurse, const char *root_path,
1582 struct got_repository *repo)
1584 const struct got_error *err = NULL;
1585 struct got_object_id *tree_id = NULL;
1586 struct got_tree_object *tree = NULL;
1587 const struct got_tree_entries *entries;
1588 struct got_tree_entry *te;
1590 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1594 err = got_object_open_as_tree(&tree, repo, tree_id);
1597 entries = got_object_tree_get_entries(tree);
1598 te = SIMPLEQ_FIRST(&entries->head);
1602 if (sigint_received || sigpipe_received)
1607 err = got_object_id_str(&id_str, te->id);
1610 if (asprintf(&id, "%s ", id_str) == -1) {
1611 err = got_error_from_errno("asprintf");
1617 print_entry(te, id, path, root_path);
1620 if (recurse && S_ISDIR(te->mode)) {
1622 if (asprintf(&child_path, "%s%s%s", path,
1623 path[0] == '/' && path[1] == '\0' ? "" : "/",
1625 err = got_error_from_errno("asprintf");
1628 err = print_tree(child_path, commit_id, show_ids, 1,
1635 te = SIMPLEQ_NEXT(te, entry);
1639 got_object_tree_close(tree);
1644 static const struct got_error *
1645 cmd_tree(int argc, char *argv[])
1647 const struct got_error *error;
1648 struct got_repository *repo = NULL;
1649 struct got_worktree *worktree = NULL;
1651 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1652 struct got_object_id *commit_id = NULL;
1653 char *commit_id_str = NULL;
1654 int show_ids = 0, recurse = 0;
1658 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1663 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1666 commit_id_str = optarg;
1669 repo_path = realpath(optarg, NULL);
1670 if (repo_path == NULL)
1671 err(1, "-r option");
1672 got_path_strip_trailing_slashes(repo_path);
1696 cwd = getcwd(NULL, 0);
1698 error = got_error_from_errno("getcwd");
1701 if (repo_path == NULL) {
1702 error = got_worktree_open(&worktree, cwd);
1703 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1709 strdup(got_worktree_get_repo_path(worktree));
1710 if (repo_path == NULL)
1711 error = got_error_from_errno("strdup");
1715 repo_path = strdup(cwd);
1716 if (repo_path == NULL) {
1717 error = got_error_from_errno("strdup");
1723 error = got_repo_open(&repo, repo_path);
1727 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1733 char *p, *worktree_subdir = cwd +
1734 strlen(got_worktree_get_root_path(worktree));
1735 if (asprintf(&p, "%s/%s",
1736 got_worktree_get_path_prefix(worktree),
1737 worktree_subdir) == -1) {
1738 error = got_error_from_errno("asprintf");
1741 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1748 if (in_repo_path == NULL) {
1749 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1754 if (commit_id_str == NULL) {
1755 struct got_reference *head_ref;
1756 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1759 error = got_ref_resolve(&commit_id, repo, head_ref);
1760 got_ref_close(head_ref);
1764 error = got_object_resolve_id_str(&commit_id, repo,
1770 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1771 in_repo_path, repo);
1778 got_worktree_close(worktree);
1780 const struct got_error *repo_error;
1781 repo_error = got_repo_close(repo);
1791 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1795 static const struct got_error *
1796 print_status(void *arg, unsigned char status, const char *path,
1797 struct got_object_id *blob_id, struct got_object_id *commit_id)
1799 printf("%c %s\n", status, path);
1803 static const struct got_error *
1804 cmd_status(int argc, char *argv[])
1806 const struct got_error *error = NULL;
1807 struct got_repository *repo = NULL;
1808 struct got_worktree *worktree = NULL;
1809 char *cwd = NULL, *path = NULL;
1812 while ((ch = getopt(argc, argv, "")) != -1) {
1824 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1828 cwd = getcwd(NULL, 0);
1830 error = got_error_from_errno("getcwd");
1834 error = got_worktree_open(&worktree, cwd);
1841 error = got_error_from_errno("strdup");
1844 } else if (argc == 1) {
1845 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1855 error = apply_unveil(got_repo_get_path(repo), 1,
1856 got_worktree_get_root_path(worktree), 0);
1860 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1861 check_cancelled, NULL);
1872 "usage: %s ref [-r repository] -l | -d name | name target\n",
1877 static const struct got_error *
1878 list_refs(struct got_repository *repo)
1880 static const struct got_error *err = NULL;
1881 struct got_reflist_head refs;
1882 struct got_reflist_entry *re;
1884 SIMPLEQ_INIT(&refs);
1885 err = got_ref_list(&refs, repo);
1889 SIMPLEQ_FOREACH(re, &refs, entry) {
1891 refstr = got_ref_to_str(re->ref);
1893 return got_error_from_errno("got_ref_to_str");
1894 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1898 got_ref_list_free(&refs);
1902 static const struct got_error *
1903 delete_ref(struct got_repository *repo, const char *refname)
1905 const struct got_error *err = NULL;
1906 struct got_reference *ref;
1908 err = got_ref_open(&ref, repo, refname, 0);
1912 err = got_ref_delete(ref, repo);
1917 static const struct got_error *
1918 add_ref(struct got_repository *repo, const char *refname, const char *target)
1920 const struct got_error *err = NULL;
1921 struct got_object_id *id;
1922 struct got_reference *ref = NULL;
1924 err = got_object_resolve_id_str(&id, repo, target);
1926 struct got_reference *target_ref;
1928 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1930 err = got_ref_open(&target_ref, repo, target, 0);
1933 err = got_ref_resolve(&id, repo, target_ref);
1934 got_ref_close(target_ref);
1939 err = got_ref_alloc(&ref, refname, id);
1943 err = got_ref_write(ref, repo);
1951 static const struct got_error *
1952 cmd_ref(int argc, char *argv[])
1954 const struct got_error *error = NULL;
1955 struct got_repository *repo = NULL;
1956 struct got_worktree *worktree = NULL;
1957 char *cwd = NULL, *repo_path = NULL;
1958 int ch, do_list = 0;
1959 const char *delref = NULL;
1961 /* TODO: Add -s option for adding symbolic references. */
1962 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1968 repo_path = realpath(optarg, NULL);
1969 if (repo_path == NULL)
1970 err(1, "-r option");
1971 got_path_strip_trailing_slashes(repo_path);
1982 if (do_list && delref)
1983 errx(1, "-l and -d options are mutually exclusive\n");
1988 if (do_list || delref) {
1991 } else if (argc != 2)
1996 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2000 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2001 "sendfd unveil", NULL) == -1)
2005 cwd = getcwd(NULL, 0);
2007 error = got_error_from_errno("getcwd");
2011 if (repo_path == NULL) {
2012 error = got_worktree_open(&worktree, cwd);
2013 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2019 strdup(got_worktree_get_repo_path(worktree));
2020 if (repo_path == NULL)
2021 error = got_error_from_errno("strdup");
2025 repo_path = strdup(cwd);
2026 if (repo_path == NULL) {
2027 error = got_error_from_errno("strdup");
2033 error = got_repo_open(&repo, repo_path);
2037 error = apply_unveil(got_repo_get_path(repo), do_list,
2038 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2043 error = list_refs(repo);
2045 error = delete_ref(repo, delref);
2047 error = add_ref(repo, argv[0], argv[1]);
2050 got_repo_close(repo);
2052 got_worktree_close(worktree);
2061 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2065 static const struct got_error *
2066 cmd_add(int argc, char *argv[])
2068 const struct got_error *error = NULL;
2069 struct got_repository *repo = NULL;
2070 struct got_worktree *worktree = NULL;
2072 struct got_pathlist_head paths;
2073 struct got_pathlist_entry *pe;
2078 while ((ch = getopt(argc, argv, "")) != -1) {
2092 /* make sure each file exists before doing anything halfway */
2093 for (x = 0; x < argc; x++) {
2094 char *path = realpath(argv[x], NULL);
2096 error = got_error_from_errno2("realpath", argv[x]);
2102 cwd = getcwd(NULL, 0);
2104 error = got_error_from_errno("getcwd");
2108 error = got_worktree_open(&worktree, cwd);
2112 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2116 error = apply_unveil(got_repo_get_path(repo), 1,
2117 got_worktree_get_root_path(worktree), 0);
2121 for (x = 0; x < argc; x++) {
2122 char *path = realpath(argv[x], NULL);
2124 error = got_error_from_errno2("realpath", argv[x]);
2128 got_path_strip_trailing_slashes(path);
2129 error = got_pathlist_insert(&pe, &paths, path, NULL);
2135 error = got_worktree_schedule_add(worktree, &paths, print_status,
2139 got_repo_close(repo);
2141 got_worktree_close(worktree);
2142 TAILQ_FOREACH(pe, &paths, entry)
2143 free((char *)pe->path);
2144 got_pathlist_free(&paths);
2152 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2156 static const struct got_error *
2157 cmd_rm(int argc, char *argv[])
2159 const struct got_error *error = NULL;
2160 struct got_worktree *worktree = NULL;
2161 struct got_repository *repo = NULL;
2162 char *cwd = NULL, *path = NULL;
2163 int ch, delete_local_mods = 0;
2165 while ((ch = getopt(argc, argv, "f")) != -1) {
2168 delete_local_mods = 1;
2182 path = realpath(argv[0], NULL);
2184 error = got_error_from_errno2("realpath", argv[0]);
2187 got_path_strip_trailing_slashes(path);
2189 cwd = getcwd(NULL, 0);
2191 error = got_error_from_errno("getcwd");
2194 error = got_worktree_open(&worktree, cwd);
2198 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2202 error = apply_unveil(got_repo_get_path(repo), 1,
2203 got_worktree_get_root_path(worktree), 0);
2207 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2208 print_status, NULL, repo);
2213 got_repo_close(repo);
2215 got_worktree_close(worktree);
2224 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2229 revert_progress(void *arg, unsigned char status, const char *path)
2231 while (path[0] == '/')
2233 printf("%c %s\n", status, path);
2236 static const struct got_error *
2237 cmd_revert(int argc, char *argv[])
2239 const struct got_error *error = NULL;
2240 struct got_worktree *worktree = NULL;
2241 struct got_repository *repo = NULL;
2242 char *cwd = NULL, *path = NULL;
2245 while ((ch = getopt(argc, argv, "")) != -1) {
2259 path = realpath(argv[0], NULL);
2261 error = got_error_from_errno2("realpath", argv[0]);
2264 got_path_strip_trailing_slashes(path);
2266 cwd = getcwd(NULL, 0);
2268 error = got_error_from_errno("getcwd");
2271 error = got_worktree_open(&worktree, cwd);
2275 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2279 error = apply_unveil(got_repo_get_path(repo), 1,
2280 got_worktree_get_root_path(worktree), 0);
2284 error = got_worktree_revert(worktree, path,
2285 revert_progress, NULL, repo);
2290 got_repo_close(repo);
2292 got_worktree_close(worktree);
2301 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2306 spawn_editor(const char *editor, const char *file)
2309 sig_t sighup, sigint, sigquit;
2312 sighup = signal(SIGHUP, SIG_IGN);
2313 sigint = signal(SIGINT, SIG_IGN);
2314 sigquit = signal(SIGQUIT, SIG_IGN);
2316 switch (pid = fork()) {
2320 execl(editor, editor, file, (char *)NULL);
2324 while (waitpid(pid, &st, 0) == -1)
2329 (void)signal(SIGHUP, sighup);
2330 (void)signal(SIGINT, sigint);
2331 (void)signal(SIGQUIT, sigquit);
2333 if (!WIFEXITED(st)) {
2338 return WEXITSTATUS(st);
2341 struct collect_commit_logmsg_arg {
2342 const char *cmdline_log;
2344 const char *worktree_path;
2345 const char *repo_path;
2350 static const struct got_error *
2351 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2354 const char *initial_content = "\n# changes to be committed:\n";
2355 struct got_pathlist_entry *pe;
2356 const struct got_error *err = NULL;
2357 char *template = NULL;
2358 struct collect_commit_logmsg_arg *a = arg;
2360 struct stat st, st2;
2363 int fd, content_changed = 0;
2365 /* if a message was specified on the command line, just use it */
2366 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2367 len = strlen(a->cmdline_log) + 1;
2368 *logmsg = malloc(len + 1);
2369 if (*logmsg == NULL)
2370 return got_error_from_errno("malloc");
2371 strlcpy(*logmsg, a->cmdline_log, len);
2375 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2376 return got_error_from_errno("asprintf");
2378 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2382 dprintf(fd, initial_content);
2384 TAILQ_FOREACH(pe, commitable_paths, entry) {
2385 struct got_commitable *ct = pe->data;
2386 dprintf(fd, "# %c %s\n",
2387 got_commitable_get_status(ct),
2388 got_commitable_get_path(ct));
2392 if (stat(a->logmsg_path, &st) == -1) {
2393 err = got_error_from_errno2("stat", a->logmsg_path);
2397 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2398 err = got_error_from_errno("failed spawning editor");
2402 if (stat(a->logmsg_path, &st2) == -1) {
2403 err = got_error_from_errno("stat");
2407 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2408 unlink(a->logmsg_path);
2409 free(a->logmsg_path);
2410 a->logmsg_path = NULL;
2411 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2412 "no changes made to commit message, aborting");
2416 *logmsg = malloc(st2.st_size + 1);
2417 if (*logmsg == NULL) {
2418 err = got_error_from_errno("malloc");
2421 (*logmsg)[0] = '\0';
2424 fp = fopen(a->logmsg_path, "r");
2426 err = got_error_from_errno("fopen");
2429 while (fgets(buf, sizeof(buf), fp) != NULL) {
2430 if (!content_changed && strcmp(buf, initial_content) != 0)
2431 content_changed = 1;
2432 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2433 continue; /* remove comments and leading empty lines */
2434 len = strlcat(*logmsg, buf, st2.st_size);
2438 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2439 (*logmsg)[len - 1] = '\0';
2443 if (len == 0 || !content_changed) {
2444 unlink(a->logmsg_path);
2445 free(a->logmsg_path);
2446 a->logmsg_path = NULL;
2447 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2448 "commit message cannot be empty, aborting");
2454 /* Editor is done; we can now apply unveil(2) */
2456 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2460 static const struct got_error *
2461 cmd_commit(int argc, char *argv[])
2463 const struct got_error *error = NULL;
2464 struct got_worktree *worktree = NULL;
2465 struct got_repository *repo = NULL;
2466 char *cwd = NULL, *path = NULL, *id_str = NULL;
2467 struct got_object_id *id = NULL;
2468 const char *logmsg = NULL;
2469 const char *got_author = getenv("GOT_AUTHOR");
2470 struct collect_commit_logmsg_arg cl_arg;
2471 char *editor = NULL;
2474 while ((ch = getopt(argc, argv, "m:")) != -1) {
2489 path = realpath(argv[0], NULL);
2491 error = got_error_from_errno2("realpath", argv[0]);
2494 got_path_strip_trailing_slashes(path);
2495 } else if (argc != 0)
2498 if (got_author == NULL) {
2499 /* TODO: Look current user up in password database */
2500 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2504 cwd = getcwd(NULL, 0);
2506 error = got_error_from_errno("getcwd");
2509 error = got_worktree_open(&worktree, cwd);
2513 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2518 * unveil(2) traverses exec(2); if an editor is used we have
2519 * to apply unveil after the log message has been written.
2521 if (logmsg == NULL || strlen(logmsg) == 0)
2522 error = get_editor(&editor);
2524 error = apply_unveil(got_repo_get_path(repo), 0,
2525 got_worktree_get_root_path(worktree), 0);
2529 cl_arg.editor = editor;
2530 cl_arg.cmdline_log = logmsg;
2531 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2532 cl_arg.repo_path = got_repo_get_path(repo);
2533 cl_arg.logmsg_path = NULL;
2534 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2535 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2537 if (cl_arg.logmsg_path)
2538 fprintf(stderr, "%s: log message preserved in %s\n",
2539 getprogname(), cl_arg.logmsg_path);
2543 if (cl_arg.logmsg_path)
2544 unlink(cl_arg.logmsg_path);
2546 error = got_object_id_str(&id_str, id);
2549 printf("created commit %s\n", id_str);
2552 got_repo_close(repo);
2554 got_worktree_close(worktree);