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);
1364 struct got_reference *ref;
1365 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1367 error = got_ref_open(&ref, repo, id_str1, 0);
1370 error = got_ref_resolve(&id1, repo, ref);
1376 error = got_object_resolve_id_str(&id2, repo, id_str2);
1378 struct got_reference *ref;
1379 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1381 error = got_ref_open(&ref, repo, id_str2, 0);
1384 error = got_ref_resolve(&id2, repo, ref);
1390 error = got_object_get_type(&type1, repo, id1);
1394 error = got_object_get_type(&type2, repo, id2);
1398 if (type1 != type2) {
1399 error = got_error(GOT_ERR_OBJ_TYPE);
1404 case GOT_OBJ_TYPE_BLOB:
1405 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1406 diff_context, repo, stdout);
1408 case GOT_OBJ_TYPE_TREE:
1409 error = got_diff_objects_as_trees(id1, id2, "", "",
1410 diff_context, repo, stdout);
1412 case GOT_OBJ_TYPE_COMMIT:
1413 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1415 error = got_diff_objects_as_commits(id1, id2, diff_context,
1419 error = got_error(GOT_ERR_OBJ_TYPE);
1427 got_worktree_close(worktree);
1429 const struct got_error *repo_error;
1430 repo_error = got_repo_close(repo);
1441 "usage: %s blame [-c commit] [-r repository-path] path\n",
1446 static const struct got_error *
1447 cmd_blame(int argc, char *argv[])
1449 const struct got_error *error;
1450 struct got_repository *repo = NULL;
1451 struct got_worktree *worktree = NULL;
1452 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1453 struct got_object_id *commit_id = NULL;
1454 char *commit_id_str = NULL;
1458 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1463 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1466 commit_id_str = optarg;
1469 repo_path = realpath(optarg, NULL);
1470 if (repo_path == NULL)
1471 err(1, "-r option");
1472 got_path_strip_trailing_slashes(repo_path);
1488 cwd = getcwd(NULL, 0);
1490 error = got_error_from_errno("getcwd");
1493 if (repo_path == NULL) {
1494 error = got_worktree_open(&worktree, cwd);
1495 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1501 strdup(got_worktree_get_repo_path(worktree));
1502 if (repo_path == NULL)
1503 error = got_error_from_errno("strdup");
1507 repo_path = strdup(cwd);
1508 if (repo_path == NULL) {
1509 error = got_error_from_errno("strdup");
1515 error = got_repo_open(&repo, repo_path);
1519 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1524 const char *prefix = got_worktree_get_path_prefix(worktree);
1525 char *p, *worktree_subdir = cwd +
1526 strlen(got_worktree_get_root_path(worktree));
1527 if (asprintf(&p, "%s%s%s%s%s",
1528 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1529 worktree_subdir, worktree_subdir[0] ? "/" : "",
1531 error = got_error_from_errno("asprintf");
1534 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1537 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1542 if (commit_id_str == NULL) {
1543 struct got_reference *head_ref;
1544 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1547 error = got_ref_resolve(&commit_id, repo, head_ref);
1548 got_ref_close(head_ref);
1552 error = got_object_resolve_id_str(&commit_id, repo,
1558 error = got_blame(in_repo_path, commit_id, repo, stdout);
1565 got_worktree_close(worktree);
1567 const struct got_error *repo_error;
1568 repo_error = got_repo_close(repo);
1579 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1585 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1586 const char *root_path)
1588 int is_root_path = (strcmp(path, root_path) == 0);
1590 path += strlen(root_path);
1591 while (path[0] == '/')
1594 printf("%s%s%s%s%s\n", id ? id : "", path,
1595 is_root_path ? "" : "/", te->name,
1596 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1599 static const struct got_error *
1600 print_tree(const char *path, struct got_object_id *commit_id,
1601 int show_ids, int recurse, const char *root_path,
1602 struct got_repository *repo)
1604 const struct got_error *err = NULL;
1605 struct got_object_id *tree_id = NULL;
1606 struct got_tree_object *tree = NULL;
1607 const struct got_tree_entries *entries;
1608 struct got_tree_entry *te;
1610 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1614 err = got_object_open_as_tree(&tree, repo, tree_id);
1617 entries = got_object_tree_get_entries(tree);
1618 te = SIMPLEQ_FIRST(&entries->head);
1622 if (sigint_received || sigpipe_received)
1627 err = got_object_id_str(&id_str, te->id);
1630 if (asprintf(&id, "%s ", id_str) == -1) {
1631 err = got_error_from_errno("asprintf");
1637 print_entry(te, id, path, root_path);
1640 if (recurse && S_ISDIR(te->mode)) {
1642 if (asprintf(&child_path, "%s%s%s", path,
1643 path[0] == '/' && path[1] == '\0' ? "" : "/",
1645 err = got_error_from_errno("asprintf");
1648 err = print_tree(child_path, commit_id, show_ids, 1,
1655 te = SIMPLEQ_NEXT(te, entry);
1659 got_object_tree_close(tree);
1664 static const struct got_error *
1665 cmd_tree(int argc, char *argv[])
1667 const struct got_error *error;
1668 struct got_repository *repo = NULL;
1669 struct got_worktree *worktree = NULL;
1671 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1672 struct got_object_id *commit_id = NULL;
1673 char *commit_id_str = NULL;
1674 int show_ids = 0, recurse = 0;
1678 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1683 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1686 commit_id_str = optarg;
1689 repo_path = realpath(optarg, NULL);
1690 if (repo_path == NULL)
1691 err(1, "-r option");
1692 got_path_strip_trailing_slashes(repo_path);
1716 cwd = getcwd(NULL, 0);
1718 error = got_error_from_errno("getcwd");
1721 if (repo_path == NULL) {
1722 error = got_worktree_open(&worktree, cwd);
1723 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1729 strdup(got_worktree_get_repo_path(worktree));
1730 if (repo_path == NULL)
1731 error = got_error_from_errno("strdup");
1735 repo_path = strdup(cwd);
1736 if (repo_path == NULL) {
1737 error = got_error_from_errno("strdup");
1743 error = got_repo_open(&repo, repo_path);
1747 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1753 char *p, *worktree_subdir = cwd +
1754 strlen(got_worktree_get_root_path(worktree));
1755 if (asprintf(&p, "%s/%s",
1756 got_worktree_get_path_prefix(worktree),
1757 worktree_subdir) == -1) {
1758 error = got_error_from_errno("asprintf");
1761 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1768 if (in_repo_path == NULL) {
1769 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1774 if (commit_id_str == NULL) {
1775 struct got_reference *head_ref;
1776 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1779 error = got_ref_resolve(&commit_id, repo, head_ref);
1780 got_ref_close(head_ref);
1784 error = got_object_resolve_id_str(&commit_id, repo,
1790 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1791 in_repo_path, repo);
1798 got_worktree_close(worktree);
1800 const struct got_error *repo_error;
1801 repo_error = got_repo_close(repo);
1811 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1815 static const struct got_error *
1816 print_status(void *arg, unsigned char status, const char *path,
1817 struct got_object_id *blob_id, struct got_object_id *commit_id)
1819 printf("%c %s\n", status, path);
1823 static const struct got_error *
1824 cmd_status(int argc, char *argv[])
1826 const struct got_error *error = NULL;
1827 struct got_repository *repo = NULL;
1828 struct got_worktree *worktree = NULL;
1829 char *cwd = NULL, *path = NULL;
1832 while ((ch = getopt(argc, argv, "")) != -1) {
1844 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1848 cwd = getcwd(NULL, 0);
1850 error = got_error_from_errno("getcwd");
1854 error = got_worktree_open(&worktree, cwd);
1861 error = got_error_from_errno("strdup");
1864 } else if (argc == 1) {
1865 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1871 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1875 error = apply_unveil(got_repo_get_path(repo), 1,
1876 got_worktree_get_root_path(worktree), 0);
1880 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1881 check_cancelled, NULL);
1892 "usage: %s ref [-r repository] -l | -d name | name target\n",
1897 static const struct got_error *
1898 list_refs(struct got_repository *repo)
1900 static const struct got_error *err = NULL;
1901 struct got_reflist_head refs;
1902 struct got_reflist_entry *re;
1904 SIMPLEQ_INIT(&refs);
1905 err = got_ref_list(&refs, repo);
1909 SIMPLEQ_FOREACH(re, &refs, entry) {
1911 refstr = got_ref_to_str(re->ref);
1913 return got_error_from_errno("got_ref_to_str");
1914 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1918 got_ref_list_free(&refs);
1922 static const struct got_error *
1923 delete_ref(struct got_repository *repo, const char *refname)
1925 const struct got_error *err = NULL;
1926 struct got_reference *ref;
1928 err = got_ref_open(&ref, repo, refname, 0);
1932 err = got_ref_delete(ref, repo);
1937 static const struct got_error *
1938 add_ref(struct got_repository *repo, const char *refname, const char *target)
1940 const struct got_error *err = NULL;
1941 struct got_object_id *id;
1942 struct got_reference *ref = NULL;
1944 err = got_object_resolve_id_str(&id, repo, target);
1946 struct got_reference *target_ref;
1948 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1950 err = got_ref_open(&target_ref, repo, target, 0);
1953 err = got_ref_resolve(&id, repo, target_ref);
1954 got_ref_close(target_ref);
1959 err = got_ref_alloc(&ref, refname, id);
1963 err = got_ref_write(ref, repo);
1971 static const struct got_error *
1972 cmd_ref(int argc, char *argv[])
1974 const struct got_error *error = NULL;
1975 struct got_repository *repo = NULL;
1976 struct got_worktree *worktree = NULL;
1977 char *cwd = NULL, *repo_path = NULL;
1978 int ch, do_list = 0;
1979 const char *delref = NULL;
1981 /* TODO: Add -s option for adding symbolic references. */
1982 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1988 repo_path = realpath(optarg, NULL);
1989 if (repo_path == NULL)
1990 err(1, "-r option");
1991 got_path_strip_trailing_slashes(repo_path);
2002 if (do_list && delref)
2003 errx(1, "-l and -d options are mutually exclusive\n");
2008 if (do_list || delref) {
2011 } else if (argc != 2)
2016 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2020 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2021 "sendfd unveil", NULL) == -1)
2025 cwd = getcwd(NULL, 0);
2027 error = got_error_from_errno("getcwd");
2031 if (repo_path == NULL) {
2032 error = got_worktree_open(&worktree, cwd);
2033 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2039 strdup(got_worktree_get_repo_path(worktree));
2040 if (repo_path == NULL)
2041 error = got_error_from_errno("strdup");
2045 repo_path = strdup(cwd);
2046 if (repo_path == NULL) {
2047 error = got_error_from_errno("strdup");
2053 error = got_repo_open(&repo, repo_path);
2057 error = apply_unveil(got_repo_get_path(repo), do_list,
2058 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2063 error = list_refs(repo);
2065 error = delete_ref(repo, delref);
2067 error = add_ref(repo, argv[0], argv[1]);
2070 got_repo_close(repo);
2072 got_worktree_close(worktree);
2081 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2085 static const struct got_error *
2086 cmd_add(int argc, char *argv[])
2088 const struct got_error *error = NULL;
2089 struct got_repository *repo = NULL;
2090 struct got_worktree *worktree = NULL;
2092 struct got_pathlist_head paths;
2093 struct got_pathlist_entry *pe;
2098 while ((ch = getopt(argc, argv, "")) != -1) {
2112 /* make sure each file exists before doing anything halfway */
2113 for (x = 0; x < argc; x++) {
2114 char *path = realpath(argv[x], NULL);
2116 error = got_error_from_errno2("realpath", argv[x]);
2122 cwd = getcwd(NULL, 0);
2124 error = got_error_from_errno("getcwd");
2128 error = got_worktree_open(&worktree, cwd);
2132 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2136 error = apply_unveil(got_repo_get_path(repo), 1,
2137 got_worktree_get_root_path(worktree), 0);
2141 for (x = 0; x < argc; x++) {
2142 char *path = realpath(argv[x], NULL);
2144 error = got_error_from_errno2("realpath", argv[x]);
2148 got_path_strip_trailing_slashes(path);
2149 error = got_pathlist_insert(&pe, &paths, path, NULL);
2155 error = got_worktree_schedule_add(worktree, &paths, print_status,
2159 got_repo_close(repo);
2161 got_worktree_close(worktree);
2162 TAILQ_FOREACH(pe, &paths, entry)
2163 free((char *)pe->path);
2164 got_pathlist_free(&paths);
2172 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2176 static const struct got_error *
2177 cmd_rm(int argc, char *argv[])
2179 const struct got_error *error = NULL;
2180 struct got_worktree *worktree = NULL;
2181 struct got_repository *repo = NULL;
2182 char *cwd = NULL, *path = NULL;
2183 int ch, delete_local_mods = 0;
2185 while ((ch = getopt(argc, argv, "f")) != -1) {
2188 delete_local_mods = 1;
2202 path = realpath(argv[0], NULL);
2204 error = got_error_from_errno2("realpath", argv[0]);
2207 got_path_strip_trailing_slashes(path);
2209 cwd = getcwd(NULL, 0);
2211 error = got_error_from_errno("getcwd");
2214 error = got_worktree_open(&worktree, cwd);
2218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2222 error = apply_unveil(got_repo_get_path(repo), 1,
2223 got_worktree_get_root_path(worktree), 0);
2227 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2228 print_status, NULL, repo);
2233 got_repo_close(repo);
2235 got_worktree_close(worktree);
2244 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2249 revert_progress(void *arg, unsigned char status, const char *path)
2251 while (path[0] == '/')
2253 printf("%c %s\n", status, path);
2256 static const struct got_error *
2257 cmd_revert(int argc, char *argv[])
2259 const struct got_error *error = NULL;
2260 struct got_worktree *worktree = NULL;
2261 struct got_repository *repo = NULL;
2262 char *cwd = NULL, *path = NULL;
2265 while ((ch = getopt(argc, argv, "")) != -1) {
2279 path = realpath(argv[0], NULL);
2281 error = got_error_from_errno2("realpath", argv[0]);
2284 got_path_strip_trailing_slashes(path);
2286 cwd = getcwd(NULL, 0);
2288 error = got_error_from_errno("getcwd");
2291 error = got_worktree_open(&worktree, cwd);
2295 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2299 error = apply_unveil(got_repo_get_path(repo), 1,
2300 got_worktree_get_root_path(worktree), 0);
2304 error = got_worktree_revert(worktree, path,
2305 revert_progress, NULL, repo);
2310 got_repo_close(repo);
2312 got_worktree_close(worktree);
2321 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2326 spawn_editor(const char *editor, const char *file)
2329 sig_t sighup, sigint, sigquit;
2332 sighup = signal(SIGHUP, SIG_IGN);
2333 sigint = signal(SIGINT, SIG_IGN);
2334 sigquit = signal(SIGQUIT, SIG_IGN);
2336 switch (pid = fork()) {
2340 execl(editor, editor, file, (char *)NULL);
2344 while (waitpid(pid, &st, 0) == -1)
2349 (void)signal(SIGHUP, sighup);
2350 (void)signal(SIGINT, sigint);
2351 (void)signal(SIGQUIT, sigquit);
2353 if (!WIFEXITED(st)) {
2358 return WEXITSTATUS(st);
2361 struct collect_commit_logmsg_arg {
2362 const char *cmdline_log;
2364 const char *worktree_path;
2365 const char *repo_path;
2370 static const struct got_error *
2371 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2374 const char *initial_content = "\n# changes to be committed:\n";
2375 struct got_pathlist_entry *pe;
2376 const struct got_error *err = NULL;
2377 char *template = NULL;
2378 struct collect_commit_logmsg_arg *a = arg;
2380 struct stat st, st2;
2383 int fd, content_changed = 0;
2385 /* if a message was specified on the command line, just use it */
2386 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2387 len = strlen(a->cmdline_log) + 1;
2388 *logmsg = malloc(len + 1);
2389 if (*logmsg == NULL)
2390 return got_error_from_errno("malloc");
2391 strlcpy(*logmsg, a->cmdline_log, len);
2395 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2396 return got_error_from_errno("asprintf");
2398 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2402 dprintf(fd, initial_content);
2404 TAILQ_FOREACH(pe, commitable_paths, entry) {
2405 struct got_commitable *ct = pe->data;
2406 dprintf(fd, "# %c %s\n",
2407 got_commitable_get_status(ct),
2408 got_commitable_get_path(ct));
2412 if (stat(a->logmsg_path, &st) == -1) {
2413 err = got_error_from_errno2("stat", a->logmsg_path);
2417 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2418 err = got_error_from_errno("failed spawning editor");
2422 if (stat(a->logmsg_path, &st2) == -1) {
2423 err = got_error_from_errno("stat");
2427 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2428 unlink(a->logmsg_path);
2429 free(a->logmsg_path);
2430 a->logmsg_path = NULL;
2431 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2432 "no changes made to commit message, aborting");
2436 *logmsg = malloc(st2.st_size + 1);
2437 if (*logmsg == NULL) {
2438 err = got_error_from_errno("malloc");
2441 (*logmsg)[0] = '\0';
2444 fp = fopen(a->logmsg_path, "r");
2446 err = got_error_from_errno("fopen");
2449 while (fgets(buf, sizeof(buf), fp) != NULL) {
2450 if (!content_changed && strcmp(buf, initial_content) != 0)
2451 content_changed = 1;
2452 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2453 continue; /* remove comments and leading empty lines */
2454 len = strlcat(*logmsg, buf, st2.st_size);
2458 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2459 (*logmsg)[len - 1] = '\0';
2463 if (len == 0 || !content_changed) {
2464 unlink(a->logmsg_path);
2465 free(a->logmsg_path);
2466 a->logmsg_path = NULL;
2467 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2468 "commit message cannot be empty, aborting");
2474 /* Editor is done; we can now apply unveil(2) */
2476 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2480 static const struct got_error *
2481 cmd_commit(int argc, char *argv[])
2483 const struct got_error *error = NULL;
2484 struct got_worktree *worktree = NULL;
2485 struct got_repository *repo = NULL;
2486 char *cwd = NULL, *path = NULL, *id_str = NULL;
2487 struct got_object_id *id = NULL;
2488 const char *logmsg = NULL;
2489 const char *got_author = getenv("GOT_AUTHOR");
2490 struct collect_commit_logmsg_arg cl_arg;
2491 char *editor = NULL;
2494 while ((ch = getopt(argc, argv, "m:")) != -1) {
2509 path = realpath(argv[0], NULL);
2511 error = got_error_from_errno2("realpath", argv[0]);
2514 got_path_strip_trailing_slashes(path);
2515 } else if (argc != 0)
2518 if (got_author == NULL) {
2519 /* TODO: Look current user up in password database */
2520 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2524 cwd = getcwd(NULL, 0);
2526 error = got_error_from_errno("getcwd");
2529 error = got_worktree_open(&worktree, cwd);
2533 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2538 * unveil(2) traverses exec(2); if an editor is used we have
2539 * to apply unveil after the log message has been written.
2541 if (logmsg == NULL || strlen(logmsg) == 0)
2542 error = get_editor(&editor);
2544 error = apply_unveil(got_repo_get_path(repo), 0,
2545 got_worktree_get_root_path(worktree), 0);
2549 cl_arg.editor = editor;
2550 cl_arg.cmdline_log = logmsg;
2551 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2552 cl_arg.repo_path = got_repo_get_path(repo);
2553 cl_arg.logmsg_path = NULL;
2554 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2555 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2557 if (cl_arg.logmsg_path)
2558 fprintf(stderr, "%s: log message preserved in %s\n",
2559 getprogname(), cl_arg.logmsg_path);
2563 if (cl_arg.logmsg_path)
2564 unlink(cl_arg.logmsg_path);
2566 error = got_object_id_str(&id_str, id);
2569 printf("created commit %s\n", id_str);
2572 got_repo_close(repo);
2574 got_worktree_close(worktree);