Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <libgen.h>
34 #include <time.h>
35 #include <paths.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_diff.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
56 static void
57 catch_sigint(int signo)
58 {
59 sigint_received = 1;
60 }
62 static void
63 catch_sigpipe(int signo)
64 {
65 sigpipe_received = 1;
66 }
69 struct cmd {
70 const char *cmd_name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 const char *cmd_descr;
74 };
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" },
128 };
130 int
131 main(int argc, char *argv[])
133 struct cmd *cmd;
134 unsigned int i;
135 int ch;
136 int hflag = 0;
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt(argc, argv, "h")) != -1) {
141 switch (ch) {
142 case 'h':
143 hflag = 1;
144 break;
145 default:
146 usage();
147 /* NOTREACHED */
151 argc -= optind;
152 argv += optind;
153 optind = 0;
155 if (argc <= 0)
156 usage();
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])))
167 continue;
169 if (hflag)
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);
175 return 1;
178 return 0;
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 return 1;
185 __dead static void
186 usage(void)
188 int i;
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);
196 exit(1);
199 static const struct got_error *
200 get_editor(char **abspath)
202 const struct got_error *err = NULL;
203 const char *editor;
205 editor = getenv("VISUAL");
206 if (editor == NULL)
207 editor = getenv("EDITOR");
209 if (editor) {
210 err = got_path_find_prog(abspath, editor);
211 if (err)
212 return err;
215 if (*abspath == NULL) {
216 *abspath = strdup("/bin/ed");
217 if (*abspath == NULL)
218 return got_error_from_errno("strdup");
221 return NULL;
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)) {
236 errno = 0;
237 err = NULL;
238 } else {
239 err = got_error_path(worktree_path,
240 GOT_ERR_DIR_NOT_EMPTY);
244 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
245 return err;
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();
258 if (err != NULL)
259 return err;
261 if (unveil(NULL, NULL) != 0)
262 return got_error_from_errno("unveil");
264 return NULL;
267 __dead static void
268 usage_checkout(void)
270 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
271 "[-p prefix] repository-path [worktree-path]\n", getprogname());
272 exit(1);
275 static void
276 checkout_progress(void *arg, unsigned char status, const char *path)
278 char *worktree_path = arg;
280 while (path[0] == '/')
281 path++;
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);
291 return NULL;
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);
303 if (err)
304 return err;
306 if (yca_id == NULL)
307 return got_error(GOT_ERR_ANCESTRY);
309 /*
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)
316 * \ /
317 * C E
318 * \ /
319 * B (yca)
320 * |
321 * A
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)
326 */
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);
331 free(yca_id);
332 return NULL;
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);
345 if (err)
346 goto done;
348 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
349 if (err)
350 goto done;
352 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
353 if (err)
354 goto done;
356 for (;;) {
357 struct got_object_id *id;
358 err = got_commit_graph_iter_next(&id, graph);
359 if (err) {
360 if (err->code == GOT_ERR_ITER_COMPLETED) {
361 err = NULL;
362 break;
364 else if (err->code != GOT_ERR_ITER_NEED_MORE)
365 break;
366 err = got_commit_graph_fetch_commits(graph, 1,
367 repo);
368 if (err)
369 break;
372 if (id) {
373 if (got_object_id_cmp(id, commit_id) == 0) {
374 is_same_branch = 1;
375 break;
379 done:
380 if (graph)
381 got_commit_graph_close(graph);
382 free(head_commit_id);
383 if (!err && !is_same_branch)
384 err = got_error(GOT_ERR_ANCESTRY);
385 return err;
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) {
403 switch (ch) {
404 case 'b':
405 branch_name = optarg;
406 break;
407 case 'c':
408 commit_id_str = strdup(optarg);
409 if (commit_id_str == NULL)
410 return got_error_from_errno("strdup");
411 break;
412 case 'p':
413 path_prefix = optarg;
414 break;
415 default:
416 usage_checkout();
417 /* NOTREACHED */
421 argc -= optind;
422 argv += optind;
424 #ifndef PROFILE
425 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
426 "unveil", NULL) == -1)
427 err(1, "pledge");
428 #endif
429 if (argc == 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);
435 if (cwd == NULL) {
436 error = got_error_from_errno("getcwd");
437 goto done;
439 if (path_prefix[0]) {
440 base = basename(path_prefix);
441 if (base == NULL) {
442 error = got_error_from_errno2("basename",
443 path_prefix);
444 goto done;
446 } else {
447 base = basename(repo_path);
448 if (base == NULL) {
449 error = got_error_from_errno2("basename",
450 repo_path);
451 goto done;
454 dotgit = strstr(base, ".git");
455 if (dotgit)
456 *dotgit = '\0';
457 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
458 error = got_error_from_errno("asprintf");
459 free(cwd);
460 goto done;
462 free(cwd);
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]);
467 goto done;
469 worktree_path = realpath(argv[1], NULL);
470 if (worktree_path == NULL) {
471 error = got_error_from_errno2("realpath", argv[1]);
472 goto done;
474 } else
475 usage_checkout();
477 got_path_strip_trailing_slashes(repo_path);
478 got_path_strip_trailing_slashes(worktree_path);
480 error = got_repo_open(&repo, repo_path);
481 if (error != NULL)
482 goto done;
484 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
485 if (error)
486 goto done;
488 error = got_ref_open(&head_ref, repo, branch_name, 0);
489 if (error != NULL)
490 goto done;
492 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
493 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
494 goto done;
496 error = got_worktree_open(&worktree, worktree_path);
497 if (error != NULL)
498 goto done;
500 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
501 path_prefix);
502 if (error != NULL)
503 goto done;
504 if (!same_path_prefix) {
505 error = got_error(GOT_ERR_PATH_PREFIX);
506 goto done;
509 if (commit_id_str) {
510 struct got_object_id *commit_id;
511 error = got_object_resolve_id_str(&commit_id, repo,
512 commit_id_str);
513 if (error != NULL)
514 goto done;
515 error = check_linear_ancestry(commit_id,
516 got_worktree_get_base_commit_id(worktree), repo);
517 if (error != NULL) {
518 free(commit_id);
519 goto done;
521 error = check_same_branch(commit_id, head_ref, repo);
522 if (error)
523 goto done;
524 error = got_worktree_set_base_commit_id(worktree, repo,
525 commit_id);
526 free(commit_id);
527 if (error)
528 goto done;
531 error = got_worktree_checkout_files(worktree, "", repo,
532 checkout_progress, worktree_path, check_cancelled, NULL);
533 if (error != NULL)
534 goto done;
536 printf("Now shut up and hack\n");
538 done:
539 free(commit_id_str);
540 free(repo_path);
541 free(worktree_path);
542 return error;
545 __dead static void
546 usage_update(void)
548 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
549 getprogname());
550 exit(1);
553 static void
554 update_progress(void *arg, unsigned char status, const char *path)
556 int *did_something = arg;
558 if (status == GOT_STATUS_EXISTS)
559 return;
561 *did_something = 1;
562 while (path[0] == '/')
563 path++;
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;
573 char *base_id_str;
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);
587 if (err) {
588 if (err->code != GOT_ERR_ANCESTRY)
589 return err;
590 ref_has_moved = 1;
592 if (!ref_has_moved)
593 return NULL;
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));
598 if (err)
599 return err;
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));
604 return NULL;
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) {
621 switch (ch) {
622 case 'b':
623 branch_name = optarg;
624 break;
625 case 'c':
626 commit_id_str = strdup(optarg);
627 if (commit_id_str == NULL)
628 return got_error_from_errno("strdup");
629 break;
630 default:
631 usage_update();
632 /* NOTREACHED */
636 argc -= optind;
637 argv += optind;
639 #ifndef PROFILE
640 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
641 "unveil", NULL) == -1)
642 err(1, "pledge");
643 #endif
644 worktree_path = getcwd(NULL, 0);
645 if (worktree_path == NULL) {
646 error = got_error_from_errno("getcwd");
647 goto done;
649 error = got_worktree_open(&worktree, worktree_path);
650 if (error)
651 goto done;
653 if (argc == 0) {
654 path = strdup("");
655 if (path == NULL) {
656 error = got_error_from_errno("strdup");
657 goto done;
659 } else if (argc == 1) {
660 error = got_worktree_resolve_path(&path, worktree, argv[0]);
661 if (error)
662 goto done;
663 } else
664 usage_update();
666 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
667 if (error != NULL)
668 goto done;
670 error = apply_unveil(got_repo_get_path(repo), 0,
671 got_worktree_get_root_path(worktree), 0);
672 if (error)
673 goto done;
675 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
676 got_worktree_get_head_ref_name(worktree), 0);
677 if (error != NULL)
678 goto done;
679 if (commit_id_str == NULL) {
680 error = got_ref_resolve(&commit_id, repo, head_ref);
681 if (error != NULL)
682 goto done;
683 error = got_object_id_str(&commit_id_str, commit_id);
684 if (error != NULL)
685 goto done;
686 } else {
687 error = got_object_resolve_id_str(&commit_id, repo,
688 commit_id_str);
689 if (error != NULL)
690 goto done;
693 if (branch_name) {
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);
701 goto done;
703 error = got_ref_resolve(&head_commit_id, repo, head_ref);
704 if (error)
705 goto done;
706 error = check_linear_ancestry(commit_id, head_commit_id, repo);
707 free(head_commit_id);
708 if (error != NULL)
709 goto done;
710 error = check_same_branch(commit_id, head_ref, repo);
711 if (error)
712 goto done;
713 error = switch_head_ref(head_ref, commit_id, worktree, repo);
714 if (error)
715 goto done;
716 } else {
717 error = check_linear_ancestry(commit_id,
718 got_worktree_get_base_commit_id(worktree), repo);
719 if (error != NULL) {
720 if (error->code == GOT_ERR_ANCESTRY)
721 error = got_error(GOT_ERR_BRANCH_MOVED);
722 goto done;
724 error = check_same_branch(commit_id, head_ref, repo);
725 if (error)
726 goto done;
729 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
730 commit_id) != 0) {
731 error = got_worktree_set_base_commit_id(worktree, repo,
732 commit_id);
733 if (error)
734 goto done;
737 error = got_worktree_checkout_files(worktree, path, repo,
738 update_progress, &did_something, check_cancelled, NULL);
739 if (error != NULL)
740 goto done;
742 if (did_something)
743 printf("Updated to commit %s\n", commit_id_str);
744 else
745 printf("Already up-to-date\n");
746 done:
747 free(worktree_path);
748 free(path);
749 free(commit_id);
750 free(commit_id_str);
751 return error;
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));
765 if (err)
766 return err;
768 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
769 if (qid != NULL) {
770 struct got_commit_object *pcommit;
772 err = got_object_open_as_commit(&pcommit, repo, qid->id);
773 if (err)
774 return err;
776 err = got_object_open_as_tree(&tree1, repo,
777 got_object_commit_get_tree_id(pcommit));
778 got_object_commit_close(pcommit);
779 if (err)
780 return err;
782 err = got_object_id_str(&id_str1, qid->id);
783 if (err)
784 return err;
787 err = got_object_id_str(&id_str2, id);
788 if (err)
789 goto done;
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);
793 done:
794 if (tree1)
795 got_object_tree_close(tree1);
796 got_object_tree_close(tree2);
797 free(id_str1);
798 free(id_str2);
799 return err;
802 static char *
803 get_datestr(time_t *time, char *datebuf)
805 char *p, *s = ctime_r(time, datebuf);
806 p = strchr(s, '\n');
807 if (p)
808 *p = '\0';
809 return s;
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;
819 char datebuf[26];
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) {
826 char *s;
827 const char *name;
828 if (got_object_id_cmp(re->id, id) != 0)
829 continue;
830 name = got_ref_get_name(re->ref);
831 if (strcmp(name, GOT_REF_HEAD) == 0)
832 continue;
833 if (strncmp(name, "refs/", 5) == 0)
834 name += 5;
835 if (strncmp(name, "got/", 4) == 0)
836 continue;
837 if (strncmp(name, "heads/", 6) == 0)
838 name += 6;
839 if (strncmp(name, "remotes/", 8) == 0)
840 name += 8;
841 s = refs_str;
842 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
843 name) == -1) {
844 err = got_error_from_errno("asprintf");
845 free(s);
846 break;
848 free(s);
850 err = got_object_id_str(&id_str, id);
851 if (err)
852 return err;
854 printf("-----------------------------------------------\n");
855 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
856 refs_str ? refs_str : "", refs_str ? ")" : "");
857 free(id_str);
858 id_str = NULL;
859 free(refs_str);
860 refs_str = NULL;
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;
872 int n = 1;
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);
876 if (err)
877 return err;
878 printf("parent %d: %s\n", n++, id_str);
879 free(id_str);
883 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
884 if (logmsg0 == NULL)
885 return got_error_from_errno("strdup");
887 logmsg = logmsg0;
888 do {
889 line = strsep(&logmsg, "\n");
890 if (line)
891 printf(" %s\n", line);
892 } while (line);
893 free(logmsg0);
895 if (show_patch) {
896 err = print_patch(commit, id, diff_context, repo);
897 if (err == 0)
898 printf("\n");
901 if (fflush(stdout) != 0 && err == NULL)
902 err = got_error_from_errno("fflush");
903 return err;
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);
916 if (err)
917 return err;
918 err = got_commit_graph_iter_start(graph, root_id, repo);
919 if (err)
920 goto done;
921 for (;;) {
922 struct got_commit_object *commit;
923 struct got_object_id *id;
925 if (sigint_received || sigpipe_received)
926 break;
928 err = got_commit_graph_iter_next(&id, graph);
929 if (err) {
930 if (err->code == GOT_ERR_ITER_COMPLETED) {
931 err = NULL;
932 break;
934 if (err->code != GOT_ERR_ITER_NEED_MORE)
935 break;
936 err = got_commit_graph_fetch_commits(graph, 1, repo);
937 if (err)
938 break;
939 else
940 continue;
942 if (id == NULL)
943 break;
945 err = got_object_open_as_commit(&commit, repo, id);
946 if (err)
947 break;
948 err = print_commit(commit, id, repo, show_patch, diff_context,
949 refs);
950 got_object_commit_close(commit);
951 if (err || (limit && --limit == 0))
952 break;
954 done:
955 got_commit_graph_close(graph);
956 return err;
959 __dead static void
960 usage_log(void)
962 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
963 "[-r repository-path] [path]\n", getprogname());
964 exit(1);
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;
979 const char *errstr;
980 struct got_reflist_head refs;
982 SIMPLEQ_INIT(&refs);
984 #ifndef PROFILE
985 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
986 NULL)
987 == -1)
988 err(1, "pledge");
989 #endif
991 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
992 switch (ch) {
993 case 'b':
994 first_parent_traversal = 1;
995 break;
996 case 'p':
997 show_patch = 1;
998 break;
999 case 'c':
1000 start_commit = optarg;
1001 break;
1002 case 'C':
1003 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1004 &errstr);
1005 if (errstr != NULL)
1006 err(1, "-C option %s", errstr);
1007 break;
1008 case 'l':
1009 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1010 if (errstr != NULL)
1011 err(1, "-l option %s", errstr);
1012 break;
1013 case 'r':
1014 repo_path = realpath(optarg, NULL);
1015 if (repo_path == NULL)
1016 err(1, "-r option");
1017 got_path_strip_trailing_slashes(repo_path);
1018 break;
1019 default:
1020 usage_log();
1021 /* NOTREACHED */
1025 argc -= optind;
1026 argv += optind;
1028 cwd = getcwd(NULL, 0);
1029 if (cwd == NULL) {
1030 error = got_error_from_errno("getcwd");
1031 goto done;
1034 error = got_worktree_open(&worktree, cwd);
1035 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1036 goto done;
1037 error = NULL;
1039 if (argc == 0) {
1040 path = strdup("");
1041 if (path == NULL) {
1042 error = got_error_from_errno("strdup");
1043 goto done;
1045 } else if (argc == 1) {
1046 if (worktree) {
1047 error = got_worktree_resolve_path(&path, worktree,
1048 argv[0]);
1049 if (error)
1050 goto done;
1051 } else {
1052 path = strdup(argv[0]);
1053 if (path == NULL) {
1054 error = got_error_from_errno("strdup");
1055 goto done;
1058 } else
1059 usage_log();
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");
1067 goto done;
1070 error = got_repo_open(&repo, repo_path);
1071 if (error != NULL)
1072 goto done;
1074 error = apply_unveil(got_repo_get_path(repo), 1,
1075 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1076 if (error)
1077 goto done;
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)
1083 : GOT_REF_HEAD, 0);
1084 if (error != NULL)
1085 return error;
1086 error = got_ref_resolve(&id, repo, head_ref);
1087 got_ref_close(head_ref);
1088 if (error != NULL)
1089 return error;
1090 error = got_object_open_as_commit(&commit, repo, id);
1091 } else {
1092 struct got_reference *ref;
1093 error = got_ref_open(&ref, repo, start_commit, 0);
1094 if (error == NULL) {
1095 int obj_type;
1096 error = got_ref_resolve(&id, repo, ref);
1097 got_ref_close(ref);
1098 if (error != NULL)
1099 goto done;
1100 error = got_object_get_type(&obj_type, repo, id);
1101 if (error != NULL)
1102 goto done;
1103 if (obj_type == GOT_OBJ_TYPE_TAG) {
1104 struct got_tag_object *tag;
1105 error = got_object_open_as_tag(&tag, repo, id);
1106 if (error != NULL)
1107 goto done;
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);
1112 goto done;
1114 free(id);
1115 id = got_object_id_dup(
1116 got_object_tag_get_object_id(tag));
1117 if (id == NULL)
1118 error = got_error_from_errno(
1119 "got_object_id_dup");
1120 got_object_tag_close(tag);
1121 if (error)
1122 goto done;
1123 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1124 error = got_error(GOT_ERR_OBJ_TYPE);
1125 goto done;
1127 error = got_object_open_as_commit(&commit, repo, id);
1128 if (error != NULL)
1129 goto done;
1131 if (commit == NULL) {
1132 error = got_object_resolve_id_str(&id, repo,
1133 start_commit);
1134 if (error != NULL)
1135 return error;
1138 if (error != NULL)
1139 goto done;
1141 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1142 if (error != NULL)
1143 goto done;
1144 if (in_repo_path) {
1145 free(path);
1146 path = in_repo_path;
1149 error = got_ref_list(&refs, repo);
1150 if (error)
1151 goto done;
1153 error = print_commits(id, repo, path, show_patch,
1154 diff_context, limit, first_parent_traversal, &refs);
1155 done:
1156 free(path);
1157 free(repo_path);
1158 free(cwd);
1159 free(id);
1160 if (worktree)
1161 got_worktree_close(worktree);
1162 if (repo) {
1163 const struct got_error *repo_error;
1164 repo_error = got_repo_close(repo);
1165 if (error == NULL)
1166 error = repo_error;
1168 got_ref_list_free(&refs);
1169 return error;
1172 __dead static void
1173 usage_diff(void)
1175 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1176 "[object1 object2 | path]\n", getprogname());
1177 exit(1);
1180 struct print_diff_arg {
1181 struct got_repository *repo;
1182 struct got_worktree *worktree;
1183 int diff_context;
1184 const char *id_str;
1185 int header_shown;
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;
1195 FILE *f2 = NULL;
1196 char *abspath = NULL;
1197 struct stat sb;
1199 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1200 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1201 return NULL;
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);
1211 if (err)
1212 goto done;
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");
1220 goto done;
1223 f2 = fopen(abspath, "r");
1224 if (f2 == NULL) {
1225 err = got_error_from_errno2("fopen", abspath);
1226 goto done;
1228 if (lstat(abspath, &sb) == -1) {
1229 err = got_error_from_errno2("lstat", abspath);
1230 goto done;
1232 } else
1233 sb.st_size = 0;
1235 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1236 stdout);
1237 done:
1238 if (blob1)
1239 got_object_blob_close(blob1);
1240 if (f2 && fclose(f2) != 0 && err == NULL)
1241 err = got_error_from_errno("fclose");
1242 free(abspath);
1243 return err;
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;
1255 int type1, type2;
1256 int diff_context = 3, ch;
1257 const char *errstr;
1258 char *path = NULL;
1260 #ifndef PROFILE
1261 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1262 NULL) == -1)
1263 err(1, "pledge");
1264 #endif
1266 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1267 switch (ch) {
1268 case 'C':
1269 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1270 if (errstr != NULL)
1271 err(1, "-C option %s", errstr);
1272 break;
1273 case 'r':
1274 repo_path = realpath(optarg, NULL);
1275 if (repo_path == NULL)
1276 err(1, "-r option");
1277 got_path_strip_trailing_slashes(repo_path);
1278 break;
1279 default:
1280 usage_diff();
1281 /* NOTREACHED */
1285 argc -= optind;
1286 argv += optind;
1288 cwd = getcwd(NULL, 0);
1289 if (cwd == NULL) {
1290 error = got_error_from_errno("getcwd");
1291 goto done;
1293 error = got_worktree_open(&worktree, cwd);
1294 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1295 goto done;
1296 if (argc <= 1) {
1297 if (worktree == NULL) {
1298 error = got_error(GOT_ERR_NOT_WORKTREE);
1299 goto done;
1301 if (repo_path)
1302 errx(1,
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");
1307 goto done;
1309 if (argc == 1) {
1310 error = got_worktree_resolve_path(&path, worktree,
1311 argv[0]);
1312 if (error)
1313 goto done;
1314 } else {
1315 path = strdup("");
1316 if (path == NULL) {
1317 error = got_error_from_errno("strdup");
1318 goto done;
1321 } else if (argc == 2) {
1322 id_str1 = argv[0];
1323 id_str2 = argv[1];
1324 } else
1325 usage_diff();
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);
1334 free(repo_path);
1335 if (error != NULL)
1336 goto done;
1338 error = apply_unveil(got_repo_get_path(repo), 1,
1339 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1340 if (error)
1341 goto done;
1343 if (worktree) {
1344 struct print_diff_arg arg;
1345 char *id_str;
1346 error = got_object_id_str(&id_str,
1347 got_worktree_get_base_commit_id(worktree));
1348 if (error)
1349 goto done;
1350 arg.repo = repo;
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);
1358 free(id_str);
1359 goto done;
1362 error = got_object_resolve_id_str(&id1, repo, id_str1);
1363 if (error) {
1364 struct got_reference *ref;
1365 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1366 goto done;
1367 error = got_ref_open(&ref, repo, id_str1, 0);
1368 if (error != NULL)
1369 goto done;
1370 error = got_ref_resolve(&id1, repo, ref);
1371 got_ref_close(ref);
1372 if (error != NULL)
1373 goto done;
1376 error = got_object_resolve_id_str(&id2, repo, id_str2);
1377 if (error) {
1378 struct got_reference *ref;
1379 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1380 goto done;
1381 error = got_ref_open(&ref, repo, id_str2, 0);
1382 if (error != NULL)
1383 goto done;
1384 error = got_ref_resolve(&id2, repo, ref);
1385 got_ref_close(ref);
1386 if (error != NULL)
1387 goto done;
1390 error = got_object_get_type(&type1, repo, id1);
1391 if (error)
1392 goto done;
1394 error = got_object_get_type(&type2, repo, id2);
1395 if (error)
1396 goto done;
1398 if (type1 != type2) {
1399 error = got_error(GOT_ERR_OBJ_TYPE);
1400 goto done;
1403 switch (type1) {
1404 case GOT_OBJ_TYPE_BLOB:
1405 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1406 diff_context, repo, stdout);
1407 break;
1408 case GOT_OBJ_TYPE_TREE:
1409 error = got_diff_objects_as_trees(id1, id2, "", "",
1410 diff_context, repo, stdout);
1411 break;
1412 case GOT_OBJ_TYPE_COMMIT:
1413 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1414 id_str2);
1415 error = got_diff_objects_as_commits(id1, id2, diff_context,
1416 repo, stdout);
1417 break;
1418 default:
1419 error = got_error(GOT_ERR_OBJ_TYPE);
1422 done:
1423 free(id1);
1424 free(id2);
1425 free(path);
1426 if (worktree)
1427 got_worktree_close(worktree);
1428 if (repo) {
1429 const struct got_error *repo_error;
1430 repo_error = got_repo_close(repo);
1431 if (error == NULL)
1432 error = repo_error;
1434 return error;
1437 __dead static void
1438 usage_blame(void)
1440 fprintf(stderr,
1441 "usage: %s blame [-c commit] [-r repository-path] path\n",
1442 getprogname());
1443 exit(1);
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;
1455 int ch;
1457 #ifndef PROFILE
1458 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1459 NULL) == -1)
1460 err(1, "pledge");
1461 #endif
1463 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1464 switch (ch) {
1465 case 'c':
1466 commit_id_str = optarg;
1467 break;
1468 case 'r':
1469 repo_path = realpath(optarg, NULL);
1470 if (repo_path == NULL)
1471 err(1, "-r option");
1472 got_path_strip_trailing_slashes(repo_path);
1473 break;
1474 default:
1475 usage_blame();
1476 /* NOTREACHED */
1480 argc -= optind;
1481 argv += optind;
1483 if (argc == 1)
1484 path = argv[0];
1485 else
1486 usage_blame();
1488 cwd = getcwd(NULL, 0);
1489 if (cwd == NULL) {
1490 error = got_error_from_errno("getcwd");
1491 goto done;
1493 if (repo_path == NULL) {
1494 error = got_worktree_open(&worktree, cwd);
1495 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1496 goto done;
1497 else
1498 error = NULL;
1499 if (worktree) {
1500 repo_path =
1501 strdup(got_worktree_get_repo_path(worktree));
1502 if (repo_path == NULL)
1503 error = got_error_from_errno("strdup");
1504 if (error)
1505 goto done;
1506 } else {
1507 repo_path = strdup(cwd);
1508 if (repo_path == NULL) {
1509 error = got_error_from_errno("strdup");
1510 goto done;
1515 error = got_repo_open(&repo, repo_path);
1516 if (error != NULL)
1517 goto done;
1519 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1520 if (error)
1521 goto done;
1523 if (worktree) {
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] ? "/" : "",
1530 path) == -1) {
1531 error = got_error_from_errno("asprintf");
1532 goto done;
1534 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1535 free(p);
1536 } else {
1537 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1539 if (error)
1540 goto done;
1542 if (commit_id_str == NULL) {
1543 struct got_reference *head_ref;
1544 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1545 if (error != NULL)
1546 goto done;
1547 error = got_ref_resolve(&commit_id, repo, head_ref);
1548 got_ref_close(head_ref);
1549 if (error != NULL)
1550 goto done;
1551 } else {
1552 error = got_object_resolve_id_str(&commit_id, repo,
1553 commit_id_str);
1554 if (error != NULL)
1555 goto done;
1558 error = got_blame(in_repo_path, commit_id, repo, stdout);
1559 done:
1560 free(in_repo_path);
1561 free(repo_path);
1562 free(cwd);
1563 free(commit_id);
1564 if (worktree)
1565 got_worktree_close(worktree);
1566 if (repo) {
1567 const struct got_error *repo_error;
1568 repo_error = got_repo_close(repo);
1569 if (error == NULL)
1570 error = repo_error;
1572 return error;
1575 __dead static void
1576 usage_tree(void)
1578 fprintf(stderr,
1579 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1580 getprogname());
1581 exit(1);
1584 static void
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] == '/')
1592 path++;
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);
1611 if (err)
1612 goto done;
1614 err = got_object_open_as_tree(&tree, repo, tree_id);
1615 if (err)
1616 goto done;
1617 entries = got_object_tree_get_entries(tree);
1618 te = SIMPLEQ_FIRST(&entries->head);
1619 while (te) {
1620 char *id = NULL;
1622 if (sigint_received || sigpipe_received)
1623 break;
1625 if (show_ids) {
1626 char *id_str;
1627 err = got_object_id_str(&id_str, te->id);
1628 if (err)
1629 goto done;
1630 if (asprintf(&id, "%s ", id_str) == -1) {
1631 err = got_error_from_errno("asprintf");
1632 free(id_str);
1633 goto done;
1635 free(id_str);
1637 print_entry(te, id, path, root_path);
1638 free(id);
1640 if (recurse && S_ISDIR(te->mode)) {
1641 char *child_path;
1642 if (asprintf(&child_path, "%s%s%s", path,
1643 path[0] == '/' && path[1] == '\0' ? "" : "/",
1644 te->name) == -1) {
1645 err = got_error_from_errno("asprintf");
1646 goto done;
1648 err = print_tree(child_path, commit_id, show_ids, 1,
1649 root_path, repo);
1650 free(child_path);
1651 if (err)
1652 goto done;
1655 te = SIMPLEQ_NEXT(te, entry);
1657 done:
1658 if (tree)
1659 got_object_tree_close(tree);
1660 free(tree_id);
1661 return err;
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;
1670 const char *path;
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;
1675 int ch;
1677 #ifndef PROFILE
1678 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1679 NULL) == -1)
1680 err(1, "pledge");
1681 #endif
1683 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1684 switch (ch) {
1685 case 'c':
1686 commit_id_str = optarg;
1687 break;
1688 case 'r':
1689 repo_path = realpath(optarg, NULL);
1690 if (repo_path == NULL)
1691 err(1, "-r option");
1692 got_path_strip_trailing_slashes(repo_path);
1693 break;
1694 case 'i':
1695 show_ids = 1;
1696 break;
1697 case 'R':
1698 recurse = 1;
1699 break;
1700 default:
1701 usage_tree();
1702 /* NOTREACHED */
1706 argc -= optind;
1707 argv += optind;
1709 if (argc == 1)
1710 path = argv[0];
1711 else if (argc > 1)
1712 usage_tree();
1713 else
1714 path = NULL;
1716 cwd = getcwd(NULL, 0);
1717 if (cwd == NULL) {
1718 error = got_error_from_errno("getcwd");
1719 goto done;
1721 if (repo_path == NULL) {
1722 error = got_worktree_open(&worktree, cwd);
1723 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1724 goto done;
1725 else
1726 error = NULL;
1727 if (worktree) {
1728 repo_path =
1729 strdup(got_worktree_get_repo_path(worktree));
1730 if (repo_path == NULL)
1731 error = got_error_from_errno("strdup");
1732 if (error)
1733 goto done;
1734 } else {
1735 repo_path = strdup(cwd);
1736 if (repo_path == NULL) {
1737 error = got_error_from_errno("strdup");
1738 goto done;
1743 error = got_repo_open(&repo, repo_path);
1744 if (error != NULL)
1745 goto done;
1747 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1748 if (error)
1749 goto done;
1751 if (path == NULL) {
1752 if (worktree) {
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");
1759 goto done;
1761 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1762 free(p);
1763 if (error)
1764 goto done;
1765 } else
1766 path = "/";
1768 if (in_repo_path == NULL) {
1769 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1770 if (error != NULL)
1771 goto done;
1774 if (commit_id_str == NULL) {
1775 struct got_reference *head_ref;
1776 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1777 if (error != NULL)
1778 goto done;
1779 error = got_ref_resolve(&commit_id, repo, head_ref);
1780 got_ref_close(head_ref);
1781 if (error != NULL)
1782 goto done;
1783 } else {
1784 error = got_object_resolve_id_str(&commit_id, repo,
1785 commit_id_str);
1786 if (error != NULL)
1787 goto done;
1790 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1791 in_repo_path, repo);
1792 done:
1793 free(in_repo_path);
1794 free(repo_path);
1795 free(cwd);
1796 free(commit_id);
1797 if (worktree)
1798 got_worktree_close(worktree);
1799 if (repo) {
1800 const struct got_error *repo_error;
1801 repo_error = got_repo_close(repo);
1802 if (error == NULL)
1803 error = repo_error;
1805 return error;
1808 __dead static void
1809 usage_status(void)
1811 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1812 exit(1);
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);
1820 return NULL;
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;
1830 int ch;
1832 while ((ch = getopt(argc, argv, "")) != -1) {
1833 switch (ch) {
1834 default:
1835 usage_status();
1836 /* NOTREACHED */
1840 argc -= optind;
1841 argv += optind;
1843 #ifndef PROFILE
1844 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1845 NULL) == -1)
1846 err(1, "pledge");
1847 #endif
1848 cwd = getcwd(NULL, 0);
1849 if (cwd == NULL) {
1850 error = got_error_from_errno("getcwd");
1851 goto done;
1854 error = got_worktree_open(&worktree, cwd);
1855 if (error != NULL)
1856 goto done;
1858 if (argc == 0) {
1859 path = strdup("");
1860 if (path == NULL) {
1861 error = got_error_from_errno("strdup");
1862 goto done;
1864 } else if (argc == 1) {
1865 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1866 if (error)
1867 goto done;
1868 } else
1869 usage_status();
1871 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1872 if (error != NULL)
1873 goto done;
1875 error = apply_unveil(got_repo_get_path(repo), 1,
1876 got_worktree_get_root_path(worktree), 0);
1877 if (error)
1878 goto done;
1880 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1881 check_cancelled, NULL);
1882 done:
1883 free(cwd);
1884 free(path);
1885 return error;
1888 __dead static void
1889 usage_ref(void)
1891 fprintf(stderr,
1892 "usage: %s ref [-r repository] -l | -d name | name target\n",
1893 getprogname());
1894 exit(1);
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);
1906 if (err)
1907 return err;
1909 SIMPLEQ_FOREACH(re, &refs, entry) {
1910 char *refstr;
1911 refstr = got_ref_to_str(re->ref);
1912 if (refstr == NULL)
1913 return got_error_from_errno("got_ref_to_str");
1914 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1915 free(refstr);
1918 got_ref_list_free(&refs);
1919 return NULL;
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);
1929 if (err)
1930 return err;
1932 err = got_ref_delete(ref, repo);
1933 got_ref_close(ref);
1934 return err;
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);
1945 if (err) {
1946 struct got_reference *target_ref;
1948 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1949 return err;
1950 err = got_ref_open(&target_ref, repo, target, 0);
1951 if (err)
1952 return err;
1953 err = got_ref_resolve(&id, repo, target_ref);
1954 got_ref_close(target_ref);
1955 if (err)
1956 return err;
1959 err = got_ref_alloc(&ref, refname, id);
1960 if (err)
1961 goto done;
1963 err = got_ref_write(ref, repo);
1964 done:
1965 if (ref)
1966 got_ref_close(ref);
1967 free(id);
1968 return err;
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) {
1983 switch (ch) {
1984 case 'd':
1985 delref = optarg;
1986 break;
1987 case 'r':
1988 repo_path = realpath(optarg, NULL);
1989 if (repo_path == NULL)
1990 err(1, "-r option");
1991 got_path_strip_trailing_slashes(repo_path);
1992 break;
1993 case 'l':
1994 do_list = 1;
1995 break;
1996 default:
1997 usage_ref();
1998 /* NOTREACHED */
2002 if (do_list && delref)
2003 errx(1, "-l and -d options are mutually exclusive\n");
2005 argc -= optind;
2006 argv += optind;
2008 if (do_list || delref) {
2009 if (argc > 0)
2010 usage_ref();
2011 } else if (argc != 2)
2012 usage_ref();
2014 #ifndef PROFILE
2015 if (do_list) {
2016 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2017 NULL) == -1)
2018 err(1, "pledge");
2019 } else {
2020 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2021 "sendfd unveil", NULL) == -1)
2022 err(1, "pledge");
2024 #endif
2025 cwd = getcwd(NULL, 0);
2026 if (cwd == NULL) {
2027 error = got_error_from_errno("getcwd");
2028 goto done;
2031 if (repo_path == NULL) {
2032 error = got_worktree_open(&worktree, cwd);
2033 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2034 goto done;
2035 else
2036 error = NULL;
2037 if (worktree) {
2038 repo_path =
2039 strdup(got_worktree_get_repo_path(worktree));
2040 if (repo_path == NULL)
2041 error = got_error_from_errno("strdup");
2042 if (error)
2043 goto done;
2044 } else {
2045 repo_path = strdup(cwd);
2046 if (repo_path == NULL) {
2047 error = got_error_from_errno("strdup");
2048 goto done;
2053 error = got_repo_open(&repo, repo_path);
2054 if (error != NULL)
2055 goto done;
2057 error = apply_unveil(got_repo_get_path(repo), do_list,
2058 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2059 if (error)
2060 goto done;
2062 if (do_list)
2063 error = list_refs(repo);
2064 else if (delref)
2065 error = delete_ref(repo, delref);
2066 else
2067 error = add_ref(repo, argv[0], argv[1]);
2068 done:
2069 if (repo)
2070 got_repo_close(repo);
2071 if (worktree)
2072 got_worktree_close(worktree);
2073 free(cwd);
2074 free(repo_path);
2075 return error;
2078 __dead static void
2079 usage_add(void)
2081 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2082 exit(1);
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;
2091 char *cwd = NULL;
2092 struct got_pathlist_head paths;
2093 struct got_pathlist_entry *pe;
2094 int ch, x;
2096 TAILQ_INIT(&paths);
2098 while ((ch = getopt(argc, argv, "")) != -1) {
2099 switch (ch) {
2100 default:
2101 usage_add();
2102 /* NOTREACHED */
2106 argc -= optind;
2107 argv += optind;
2109 if (argc < 1)
2110 usage_add();
2112 /* make sure each file exists before doing anything halfway */
2113 for (x = 0; x < argc; x++) {
2114 char *path = realpath(argv[x], NULL);
2115 if (path == NULL) {
2116 error = got_error_from_errno2("realpath", argv[x]);
2117 goto done;
2119 free(path);
2122 cwd = getcwd(NULL, 0);
2123 if (cwd == NULL) {
2124 error = got_error_from_errno("getcwd");
2125 goto done;
2128 error = got_worktree_open(&worktree, cwd);
2129 if (error)
2130 goto done;
2132 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2133 if (error != NULL)
2134 goto done;
2136 error = apply_unveil(got_repo_get_path(repo), 1,
2137 got_worktree_get_root_path(worktree), 0);
2138 if (error)
2139 goto done;
2141 for (x = 0; x < argc; x++) {
2142 char *path = realpath(argv[x], NULL);
2143 if (path == NULL) {
2144 error = got_error_from_errno2("realpath", argv[x]);
2145 goto done;
2148 got_path_strip_trailing_slashes(path);
2149 error = got_pathlist_insert(&pe, &paths, path, NULL);
2150 if (error) {
2151 free(path);
2152 goto done;
2155 error = got_worktree_schedule_add(worktree, &paths, print_status,
2156 NULL, repo);
2157 done:
2158 if (repo)
2159 got_repo_close(repo);
2160 if (worktree)
2161 got_worktree_close(worktree);
2162 TAILQ_FOREACH(pe, &paths, entry)
2163 free((char *)pe->path);
2164 got_pathlist_free(&paths);
2165 free(cwd);
2166 return error;
2169 __dead static void
2170 usage_rm(void)
2172 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2173 exit(1);
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) {
2186 switch (ch) {
2187 case 'f':
2188 delete_local_mods = 1;
2189 break;
2190 default:
2191 usage_add();
2192 /* NOTREACHED */
2196 argc -= optind;
2197 argv += optind;
2199 if (argc != 1)
2200 usage_rm();
2202 path = realpath(argv[0], NULL);
2203 if (path == NULL) {
2204 error = got_error_from_errno2("realpath", argv[0]);
2205 goto done;
2207 got_path_strip_trailing_slashes(path);
2209 cwd = getcwd(NULL, 0);
2210 if (cwd == NULL) {
2211 error = got_error_from_errno("getcwd");
2212 goto done;
2214 error = got_worktree_open(&worktree, cwd);
2215 if (error)
2216 goto done;
2218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2219 if (error)
2220 goto done;
2222 error = apply_unveil(got_repo_get_path(repo), 1,
2223 got_worktree_get_root_path(worktree), 0);
2224 if (error)
2225 goto done;
2227 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2228 print_status, NULL, repo);
2229 if (error)
2230 goto done;
2231 done:
2232 if (repo)
2233 got_repo_close(repo);
2234 if (worktree)
2235 got_worktree_close(worktree);
2236 free(path);
2237 free(cwd);
2238 return error;
2241 __dead static void
2242 usage_revert(void)
2244 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2245 exit(1);
2248 static void
2249 revert_progress(void *arg, unsigned char status, const char *path)
2251 while (path[0] == '/')
2252 path++;
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;
2263 int ch;
2265 while ((ch = getopt(argc, argv, "")) != -1) {
2266 switch (ch) {
2267 default:
2268 usage_revert();
2269 /* NOTREACHED */
2273 argc -= optind;
2274 argv += optind;
2276 if (argc != 1)
2277 usage_revert();
2279 path = realpath(argv[0], NULL);
2280 if (path == NULL) {
2281 error = got_error_from_errno2("realpath", argv[0]);
2282 goto done;
2284 got_path_strip_trailing_slashes(path);
2286 cwd = getcwd(NULL, 0);
2287 if (cwd == NULL) {
2288 error = got_error_from_errno("getcwd");
2289 goto done;
2291 error = got_worktree_open(&worktree, cwd);
2292 if (error)
2293 goto done;
2295 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2296 if (error != NULL)
2297 goto done;
2299 error = apply_unveil(got_repo_get_path(repo), 1,
2300 got_worktree_get_root_path(worktree), 0);
2301 if (error)
2302 goto done;
2304 error = got_worktree_revert(worktree, path,
2305 revert_progress, NULL, repo);
2306 if (error)
2307 goto done;
2308 done:
2309 if (repo)
2310 got_repo_close(repo);
2311 if (worktree)
2312 got_worktree_close(worktree);
2313 free(path);
2314 free(cwd);
2315 return error;
2318 __dead static void
2319 usage_commit(void)
2321 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2322 exit(1);
2325 int
2326 spawn_editor(const char *editor, const char *file)
2328 pid_t pid;
2329 sig_t sighup, sigint, sigquit;
2330 int st = -1;
2332 sighup = signal(SIGHUP, SIG_IGN);
2333 sigint = signal(SIGINT, SIG_IGN);
2334 sigquit = signal(SIGQUIT, SIG_IGN);
2336 switch (pid = fork()) {
2337 case -1:
2338 goto doneediting;
2339 case 0:
2340 execl(editor, editor, file, (char *)NULL);
2341 _exit(127);
2344 while (waitpid(pid, &st, 0) == -1)
2345 if (errno != EINTR)
2346 break;
2348 doneediting:
2349 (void)signal(SIGHUP, sighup);
2350 (void)signal(SIGINT, sigint);
2351 (void)signal(SIGQUIT, sigquit);
2353 if (!WIFEXITED(st)) {
2354 errno = EINTR;
2355 return -1;
2358 return WEXITSTATUS(st);
2361 struct collect_commit_logmsg_arg {
2362 const char *cmdline_log;
2363 const char *editor;
2364 const char *worktree_path;
2365 const char *repo_path;
2366 char *logmsg_path;
2370 static const struct got_error *
2371 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2372 void *arg)
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;
2379 char buf[1024];
2380 struct stat st, st2;
2381 FILE *fp;
2382 size_t len;
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);
2392 return NULL;
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);
2399 if (err)
2400 goto done;
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));
2410 close(fd);
2412 if (stat(a->logmsg_path, &st) == -1) {
2413 err = got_error_from_errno2("stat", a->logmsg_path);
2414 goto done;
2417 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2418 err = got_error_from_errno("failed spawning editor");
2419 goto done;
2422 if (stat(a->logmsg_path, &st2) == -1) {
2423 err = got_error_from_errno("stat");
2424 goto done;
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");
2433 goto done;
2436 *logmsg = malloc(st2.st_size + 1);
2437 if (*logmsg == NULL) {
2438 err = got_error_from_errno("malloc");
2439 goto done;
2441 (*logmsg)[0] = '\0';
2442 len = 0;
2444 fp = fopen(a->logmsg_path, "r");
2445 if (fp == NULL) {
2446 err = got_error_from_errno("fopen");
2447 goto done;
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);
2456 fclose(fp);
2458 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2459 (*logmsg)[len - 1] = '\0';
2460 len--;
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");
2469 goto done;
2471 done:
2472 free(template);
2474 /* Editor is done; we can now apply unveil(2) */
2475 if (err == NULL)
2476 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2477 return err;
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;
2492 int ch;
2494 while ((ch = getopt(argc, argv, "m:")) != -1) {
2495 switch (ch) {
2496 case 'm':
2497 logmsg = optarg;
2498 break;
2499 default:
2500 usage_commit();
2501 /* NOTREACHED */
2505 argc -= optind;
2506 argv += optind;
2508 if (argc == 1) {
2509 path = realpath(argv[0], NULL);
2510 if (path == NULL) {
2511 error = got_error_from_errno2("realpath", argv[0]);
2512 goto done;
2514 got_path_strip_trailing_slashes(path);
2515 } else if (argc != 0)
2516 usage_commit();
2518 if (got_author == NULL) {
2519 /* TODO: Look current user up in password database */
2520 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2521 goto done;
2524 cwd = getcwd(NULL, 0);
2525 if (cwd == NULL) {
2526 error = got_error_from_errno("getcwd");
2527 goto done;
2529 error = got_worktree_open(&worktree, cwd);
2530 if (error)
2531 goto done;
2533 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2534 if (error != NULL)
2535 goto done;
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);
2543 else
2544 error = apply_unveil(got_repo_get_path(repo), 0,
2545 got_worktree_get_root_path(worktree), 0);
2546 if (error)
2547 goto done;
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);
2556 if (error) {
2557 if (cl_arg.logmsg_path)
2558 fprintf(stderr, "%s: log message preserved in %s\n",
2559 getprogname(), cl_arg.logmsg_path);
2560 goto done;
2563 if (cl_arg.logmsg_path)
2564 unlink(cl_arg.logmsg_path);
2566 error = got_object_id_str(&id_str, id);
2567 if (error)
2568 goto done;
2569 printf("created commit %s\n", id_str);
2570 done:
2571 if (repo)
2572 got_repo_close(repo);
2573 if (worktree)
2574 got_worktree_close(worktree);
2575 free(path);
2576 free(cwd);
2577 free(id_str);
2578 free(editor);
2579 return error;