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 goto done;
1366 error = got_object_resolve_id_str(&id2, repo, id_str2);
1367 if (error)
1368 goto done;
1370 error = got_object_get_type(&type1, repo, id1);
1371 if (error)
1372 goto done;
1374 error = got_object_get_type(&type2, repo, id2);
1375 if (error)
1376 goto done;
1378 if (type1 != type2) {
1379 error = got_error(GOT_ERR_OBJ_TYPE);
1380 goto done;
1383 switch (type1) {
1384 case GOT_OBJ_TYPE_BLOB:
1385 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1386 diff_context, repo, stdout);
1387 break;
1388 case GOT_OBJ_TYPE_TREE:
1389 error = got_diff_objects_as_trees(id1, id2, "", "",
1390 diff_context, repo, stdout);
1391 break;
1392 case GOT_OBJ_TYPE_COMMIT:
1393 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1394 id_str2);
1395 error = got_diff_objects_as_commits(id1, id2, diff_context,
1396 repo, stdout);
1397 break;
1398 default:
1399 error = got_error(GOT_ERR_OBJ_TYPE);
1402 done:
1403 free(id1);
1404 free(id2);
1405 free(path);
1406 if (worktree)
1407 got_worktree_close(worktree);
1408 if (repo) {
1409 const struct got_error *repo_error;
1410 repo_error = got_repo_close(repo);
1411 if (error == NULL)
1412 error = repo_error;
1414 return error;
1417 __dead static void
1418 usage_blame(void)
1420 fprintf(stderr,
1421 "usage: %s blame [-c commit] [-r repository-path] path\n",
1422 getprogname());
1423 exit(1);
1426 static const struct got_error *
1427 cmd_blame(int argc, char *argv[])
1429 const struct got_error *error;
1430 struct got_repository *repo = NULL;
1431 struct got_worktree *worktree = NULL;
1432 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1433 struct got_object_id *commit_id = NULL;
1434 char *commit_id_str = NULL;
1435 int ch;
1437 #ifndef PROFILE
1438 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1439 NULL) == -1)
1440 err(1, "pledge");
1441 #endif
1443 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1444 switch (ch) {
1445 case 'c':
1446 commit_id_str = optarg;
1447 break;
1448 case 'r':
1449 repo_path = realpath(optarg, NULL);
1450 if (repo_path == NULL)
1451 err(1, "-r option");
1452 got_path_strip_trailing_slashes(repo_path);
1453 break;
1454 default:
1455 usage_blame();
1456 /* NOTREACHED */
1460 argc -= optind;
1461 argv += optind;
1463 if (argc == 1)
1464 path = argv[0];
1465 else
1466 usage_blame();
1468 cwd = getcwd(NULL, 0);
1469 if (cwd == NULL) {
1470 error = got_error_from_errno("getcwd");
1471 goto done;
1473 if (repo_path == NULL) {
1474 error = got_worktree_open(&worktree, cwd);
1475 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1476 goto done;
1477 else
1478 error = NULL;
1479 if (worktree) {
1480 repo_path =
1481 strdup(got_worktree_get_repo_path(worktree));
1482 if (repo_path == NULL)
1483 error = got_error_from_errno("strdup");
1484 if (error)
1485 goto done;
1486 } else {
1487 repo_path = strdup(cwd);
1488 if (repo_path == NULL) {
1489 error = got_error_from_errno("strdup");
1490 goto done;
1495 error = got_repo_open(&repo, repo_path);
1496 if (error != NULL)
1497 goto done;
1499 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1500 if (error)
1501 goto done;
1503 if (worktree) {
1504 const char *prefix = got_worktree_get_path_prefix(worktree);
1505 char *p, *worktree_subdir = cwd +
1506 strlen(got_worktree_get_root_path(worktree));
1507 if (asprintf(&p, "%s%s%s%s%s",
1508 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1509 worktree_subdir, worktree_subdir[0] ? "/" : "",
1510 path) == -1) {
1511 error = got_error_from_errno("asprintf");
1512 goto done;
1514 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1515 free(p);
1516 } else {
1517 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1519 if (error)
1520 goto done;
1522 if (commit_id_str == NULL) {
1523 struct got_reference *head_ref;
1524 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1525 if (error != NULL)
1526 goto done;
1527 error = got_ref_resolve(&commit_id, repo, head_ref);
1528 got_ref_close(head_ref);
1529 if (error != NULL)
1530 goto done;
1531 } else {
1532 error = got_object_resolve_id_str(&commit_id, repo,
1533 commit_id_str);
1534 if (error != NULL)
1535 goto done;
1538 error = got_blame(in_repo_path, commit_id, repo, stdout);
1539 done:
1540 free(in_repo_path);
1541 free(repo_path);
1542 free(cwd);
1543 free(commit_id);
1544 if (worktree)
1545 got_worktree_close(worktree);
1546 if (repo) {
1547 const struct got_error *repo_error;
1548 repo_error = got_repo_close(repo);
1549 if (error == NULL)
1550 error = repo_error;
1552 return error;
1555 __dead static void
1556 usage_tree(void)
1558 fprintf(stderr,
1559 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1560 getprogname());
1561 exit(1);
1564 static void
1565 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1566 const char *root_path)
1568 int is_root_path = (strcmp(path, root_path) == 0);
1570 path += strlen(root_path);
1571 while (path[0] == '/')
1572 path++;
1574 printf("%s%s%s%s%s\n", id ? id : "", path,
1575 is_root_path ? "" : "/", te->name,
1576 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1579 static const struct got_error *
1580 print_tree(const char *path, struct got_object_id *commit_id,
1581 int show_ids, int recurse, const char *root_path,
1582 struct got_repository *repo)
1584 const struct got_error *err = NULL;
1585 struct got_object_id *tree_id = NULL;
1586 struct got_tree_object *tree = NULL;
1587 const struct got_tree_entries *entries;
1588 struct got_tree_entry *te;
1590 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1591 if (err)
1592 goto done;
1594 err = got_object_open_as_tree(&tree, repo, tree_id);
1595 if (err)
1596 goto done;
1597 entries = got_object_tree_get_entries(tree);
1598 te = SIMPLEQ_FIRST(&entries->head);
1599 while (te) {
1600 char *id = NULL;
1602 if (sigint_received || sigpipe_received)
1603 break;
1605 if (show_ids) {
1606 char *id_str;
1607 err = got_object_id_str(&id_str, te->id);
1608 if (err)
1609 goto done;
1610 if (asprintf(&id, "%s ", id_str) == -1) {
1611 err = got_error_from_errno("asprintf");
1612 free(id_str);
1613 goto done;
1615 free(id_str);
1617 print_entry(te, id, path, root_path);
1618 free(id);
1620 if (recurse && S_ISDIR(te->mode)) {
1621 char *child_path;
1622 if (asprintf(&child_path, "%s%s%s", path,
1623 path[0] == '/' && path[1] == '\0' ? "" : "/",
1624 te->name) == -1) {
1625 err = got_error_from_errno("asprintf");
1626 goto done;
1628 err = print_tree(child_path, commit_id, show_ids, 1,
1629 root_path, repo);
1630 free(child_path);
1631 if (err)
1632 goto done;
1635 te = SIMPLEQ_NEXT(te, entry);
1637 done:
1638 if (tree)
1639 got_object_tree_close(tree);
1640 free(tree_id);
1641 return err;
1644 static const struct got_error *
1645 cmd_tree(int argc, char *argv[])
1647 const struct got_error *error;
1648 struct got_repository *repo = NULL;
1649 struct got_worktree *worktree = NULL;
1650 const char *path;
1651 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1652 struct got_object_id *commit_id = NULL;
1653 char *commit_id_str = NULL;
1654 int show_ids = 0, recurse = 0;
1655 int ch;
1657 #ifndef PROFILE
1658 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1659 NULL) == -1)
1660 err(1, "pledge");
1661 #endif
1663 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1664 switch (ch) {
1665 case 'c':
1666 commit_id_str = optarg;
1667 break;
1668 case 'r':
1669 repo_path = realpath(optarg, NULL);
1670 if (repo_path == NULL)
1671 err(1, "-r option");
1672 got_path_strip_trailing_slashes(repo_path);
1673 break;
1674 case 'i':
1675 show_ids = 1;
1676 break;
1677 case 'R':
1678 recurse = 1;
1679 break;
1680 default:
1681 usage_tree();
1682 /* NOTREACHED */
1686 argc -= optind;
1687 argv += optind;
1689 if (argc == 1)
1690 path = argv[0];
1691 else if (argc > 1)
1692 usage_tree();
1693 else
1694 path = NULL;
1696 cwd = getcwd(NULL, 0);
1697 if (cwd == NULL) {
1698 error = got_error_from_errno("getcwd");
1699 goto done;
1701 if (repo_path == NULL) {
1702 error = got_worktree_open(&worktree, cwd);
1703 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1704 goto done;
1705 else
1706 error = NULL;
1707 if (worktree) {
1708 repo_path =
1709 strdup(got_worktree_get_repo_path(worktree));
1710 if (repo_path == NULL)
1711 error = got_error_from_errno("strdup");
1712 if (error)
1713 goto done;
1714 } else {
1715 repo_path = strdup(cwd);
1716 if (repo_path == NULL) {
1717 error = got_error_from_errno("strdup");
1718 goto done;
1723 error = got_repo_open(&repo, repo_path);
1724 if (error != NULL)
1725 goto done;
1727 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1728 if (error)
1729 goto done;
1731 if (path == NULL) {
1732 if (worktree) {
1733 char *p, *worktree_subdir = cwd +
1734 strlen(got_worktree_get_root_path(worktree));
1735 if (asprintf(&p, "%s/%s",
1736 got_worktree_get_path_prefix(worktree),
1737 worktree_subdir) == -1) {
1738 error = got_error_from_errno("asprintf");
1739 goto done;
1741 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1742 free(p);
1743 if (error)
1744 goto done;
1745 } else
1746 path = "/";
1748 if (in_repo_path == NULL) {
1749 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1750 if (error != NULL)
1751 goto done;
1754 if (commit_id_str == NULL) {
1755 struct got_reference *head_ref;
1756 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1757 if (error != NULL)
1758 goto done;
1759 error = got_ref_resolve(&commit_id, repo, head_ref);
1760 got_ref_close(head_ref);
1761 if (error != NULL)
1762 goto done;
1763 } else {
1764 error = got_object_resolve_id_str(&commit_id, repo,
1765 commit_id_str);
1766 if (error != NULL)
1767 goto done;
1770 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1771 in_repo_path, repo);
1772 done:
1773 free(in_repo_path);
1774 free(repo_path);
1775 free(cwd);
1776 free(commit_id);
1777 if (worktree)
1778 got_worktree_close(worktree);
1779 if (repo) {
1780 const struct got_error *repo_error;
1781 repo_error = got_repo_close(repo);
1782 if (error == NULL)
1783 error = repo_error;
1785 return error;
1788 __dead static void
1789 usage_status(void)
1791 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1792 exit(1);
1795 static const struct got_error *
1796 print_status(void *arg, unsigned char status, const char *path,
1797 struct got_object_id *blob_id, struct got_object_id *commit_id)
1799 printf("%c %s\n", status, path);
1800 return NULL;
1803 static const struct got_error *
1804 cmd_status(int argc, char *argv[])
1806 const struct got_error *error = NULL;
1807 struct got_repository *repo = NULL;
1808 struct got_worktree *worktree = NULL;
1809 char *cwd = NULL, *path = NULL;
1810 int ch;
1812 while ((ch = getopt(argc, argv, "")) != -1) {
1813 switch (ch) {
1814 default:
1815 usage_status();
1816 /* NOTREACHED */
1820 argc -= optind;
1821 argv += optind;
1823 #ifndef PROFILE
1824 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1825 NULL) == -1)
1826 err(1, "pledge");
1827 #endif
1828 cwd = getcwd(NULL, 0);
1829 if (cwd == NULL) {
1830 error = got_error_from_errno("getcwd");
1831 goto done;
1834 error = got_worktree_open(&worktree, cwd);
1835 if (error != NULL)
1836 goto done;
1838 if (argc == 0) {
1839 path = strdup("");
1840 if (path == NULL) {
1841 error = got_error_from_errno("strdup");
1842 goto done;
1844 } else if (argc == 1) {
1845 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1846 if (error)
1847 goto done;
1848 } else
1849 usage_status();
1851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1852 if (error != NULL)
1853 goto done;
1855 error = apply_unveil(got_repo_get_path(repo), 1,
1856 got_worktree_get_root_path(worktree), 0);
1857 if (error)
1858 goto done;
1860 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1861 check_cancelled, NULL);
1862 done:
1863 free(cwd);
1864 free(path);
1865 return error;
1868 __dead static void
1869 usage_ref(void)
1871 fprintf(stderr,
1872 "usage: %s ref [-r repository] -l | -d name | name target\n",
1873 getprogname());
1874 exit(1);
1877 static const struct got_error *
1878 list_refs(struct got_repository *repo)
1880 static const struct got_error *err = NULL;
1881 struct got_reflist_head refs;
1882 struct got_reflist_entry *re;
1884 SIMPLEQ_INIT(&refs);
1885 err = got_ref_list(&refs, repo);
1886 if (err)
1887 return err;
1889 SIMPLEQ_FOREACH(re, &refs, entry) {
1890 char *refstr;
1891 refstr = got_ref_to_str(re->ref);
1892 if (refstr == NULL)
1893 return got_error_from_errno("got_ref_to_str");
1894 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1895 free(refstr);
1898 got_ref_list_free(&refs);
1899 return NULL;
1902 static const struct got_error *
1903 delete_ref(struct got_repository *repo, const char *refname)
1905 const struct got_error *err = NULL;
1906 struct got_reference *ref;
1908 err = got_ref_open(&ref, repo, refname, 0);
1909 if (err)
1910 return err;
1912 err = got_ref_delete(ref, repo);
1913 got_ref_close(ref);
1914 return err;
1917 static const struct got_error *
1918 add_ref(struct got_repository *repo, const char *refname, const char *target)
1920 const struct got_error *err = NULL;
1921 struct got_object_id *id;
1922 struct got_reference *ref = NULL;
1924 err = got_object_resolve_id_str(&id, repo, target);
1925 if (err) {
1926 struct got_reference *target_ref;
1928 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1929 return err;
1930 err = got_ref_open(&target_ref, repo, target, 0);
1931 if (err)
1932 return err;
1933 err = got_ref_resolve(&id, repo, target_ref);
1934 got_ref_close(target_ref);
1935 if (err)
1936 return err;
1939 err = got_ref_alloc(&ref, refname, id);
1940 if (err)
1941 goto done;
1943 err = got_ref_write(ref, repo);
1944 done:
1945 if (ref)
1946 got_ref_close(ref);
1947 free(id);
1948 return err;
1951 static const struct got_error *
1952 cmd_ref(int argc, char *argv[])
1954 const struct got_error *error = NULL;
1955 struct got_repository *repo = NULL;
1956 struct got_worktree *worktree = NULL;
1957 char *cwd = NULL, *repo_path = NULL;
1958 int ch, do_list = 0;
1959 const char *delref = NULL;
1961 /* TODO: Add -s option for adding symbolic references. */
1962 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1963 switch (ch) {
1964 case 'd':
1965 delref = optarg;
1966 break;
1967 case 'r':
1968 repo_path = realpath(optarg, NULL);
1969 if (repo_path == NULL)
1970 err(1, "-r option");
1971 got_path_strip_trailing_slashes(repo_path);
1972 break;
1973 case 'l':
1974 do_list = 1;
1975 break;
1976 default:
1977 usage_ref();
1978 /* NOTREACHED */
1982 if (do_list && delref)
1983 errx(1, "-l and -d options are mutually exclusive\n");
1985 argc -= optind;
1986 argv += optind;
1988 if (do_list || delref) {
1989 if (argc > 0)
1990 usage_ref();
1991 } else if (argc != 2)
1992 usage_ref();
1994 #ifndef PROFILE
1995 if (do_list) {
1996 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1997 NULL) == -1)
1998 err(1, "pledge");
1999 } else {
2000 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2001 "sendfd unveil", NULL) == -1)
2002 err(1, "pledge");
2004 #endif
2005 cwd = getcwd(NULL, 0);
2006 if (cwd == NULL) {
2007 error = got_error_from_errno("getcwd");
2008 goto done;
2011 if (repo_path == NULL) {
2012 error = got_worktree_open(&worktree, cwd);
2013 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2014 goto done;
2015 else
2016 error = NULL;
2017 if (worktree) {
2018 repo_path =
2019 strdup(got_worktree_get_repo_path(worktree));
2020 if (repo_path == NULL)
2021 error = got_error_from_errno("strdup");
2022 if (error)
2023 goto done;
2024 } else {
2025 repo_path = strdup(cwd);
2026 if (repo_path == NULL) {
2027 error = got_error_from_errno("strdup");
2028 goto done;
2033 error = got_repo_open(&repo, repo_path);
2034 if (error != NULL)
2035 goto done;
2037 error = apply_unveil(got_repo_get_path(repo), do_list,
2038 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2039 if (error)
2040 goto done;
2042 if (do_list)
2043 error = list_refs(repo);
2044 else if (delref)
2045 error = delete_ref(repo, delref);
2046 else
2047 error = add_ref(repo, argv[0], argv[1]);
2048 done:
2049 if (repo)
2050 got_repo_close(repo);
2051 if (worktree)
2052 got_worktree_close(worktree);
2053 free(cwd);
2054 free(repo_path);
2055 return error;
2058 __dead static void
2059 usage_add(void)
2061 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2062 exit(1);
2065 static const struct got_error *
2066 cmd_add(int argc, char *argv[])
2068 const struct got_error *error = NULL;
2069 struct got_repository *repo = NULL;
2070 struct got_worktree *worktree = NULL;
2071 char *cwd = NULL;
2072 struct got_pathlist_head paths;
2073 struct got_pathlist_entry *pe;
2074 int ch, x;
2076 TAILQ_INIT(&paths);
2078 while ((ch = getopt(argc, argv, "")) != -1) {
2079 switch (ch) {
2080 default:
2081 usage_add();
2082 /* NOTREACHED */
2086 argc -= optind;
2087 argv += optind;
2089 if (argc < 1)
2090 usage_add();
2092 /* make sure each file exists before doing anything halfway */
2093 for (x = 0; x < argc; x++) {
2094 char *path = realpath(argv[x], NULL);
2095 if (path == NULL) {
2096 error = got_error_from_errno2("realpath", argv[x]);
2097 goto done;
2099 free(path);
2102 cwd = getcwd(NULL, 0);
2103 if (cwd == NULL) {
2104 error = got_error_from_errno("getcwd");
2105 goto done;
2108 error = got_worktree_open(&worktree, cwd);
2109 if (error)
2110 goto done;
2112 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2113 if (error != NULL)
2114 goto done;
2116 error = apply_unveil(got_repo_get_path(repo), 1,
2117 got_worktree_get_root_path(worktree), 0);
2118 if (error)
2119 goto done;
2121 for (x = 0; x < argc; x++) {
2122 char *path = realpath(argv[x], NULL);
2123 if (path == NULL) {
2124 error = got_error_from_errno2("realpath", argv[x]);
2125 goto done;
2128 got_path_strip_trailing_slashes(path);
2129 error = got_pathlist_insert(&pe, &paths, path, NULL);
2130 if (error) {
2131 free(path);
2132 goto done;
2135 error = got_worktree_schedule_add(worktree, &paths, print_status,
2136 NULL, repo);
2137 done:
2138 if (repo)
2139 got_repo_close(repo);
2140 if (worktree)
2141 got_worktree_close(worktree);
2142 TAILQ_FOREACH(pe, &paths, entry)
2143 free((char *)pe->path);
2144 got_pathlist_free(&paths);
2145 free(cwd);
2146 return error;
2149 __dead static void
2150 usage_rm(void)
2152 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2153 exit(1);
2156 static const struct got_error *
2157 cmd_rm(int argc, char *argv[])
2159 const struct got_error *error = NULL;
2160 struct got_worktree *worktree = NULL;
2161 struct got_repository *repo = NULL;
2162 char *cwd = NULL, *path = NULL;
2163 int ch, delete_local_mods = 0;
2165 while ((ch = getopt(argc, argv, "f")) != -1) {
2166 switch (ch) {
2167 case 'f':
2168 delete_local_mods = 1;
2169 break;
2170 default:
2171 usage_add();
2172 /* NOTREACHED */
2176 argc -= optind;
2177 argv += optind;
2179 if (argc != 1)
2180 usage_rm();
2182 path = realpath(argv[0], NULL);
2183 if (path == NULL) {
2184 error = got_error_from_errno2("realpath", argv[0]);
2185 goto done;
2187 got_path_strip_trailing_slashes(path);
2189 cwd = getcwd(NULL, 0);
2190 if (cwd == NULL) {
2191 error = got_error_from_errno("getcwd");
2192 goto done;
2194 error = got_worktree_open(&worktree, cwd);
2195 if (error)
2196 goto done;
2198 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2199 if (error)
2200 goto done;
2202 error = apply_unveil(got_repo_get_path(repo), 1,
2203 got_worktree_get_root_path(worktree), 0);
2204 if (error)
2205 goto done;
2207 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2208 print_status, NULL, repo);
2209 if (error)
2210 goto done;
2211 done:
2212 if (repo)
2213 got_repo_close(repo);
2214 if (worktree)
2215 got_worktree_close(worktree);
2216 free(path);
2217 free(cwd);
2218 return error;
2221 __dead static void
2222 usage_revert(void)
2224 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2225 exit(1);
2228 static void
2229 revert_progress(void *arg, unsigned char status, const char *path)
2231 while (path[0] == '/')
2232 path++;
2233 printf("%c %s\n", status, path);
2236 static const struct got_error *
2237 cmd_revert(int argc, char *argv[])
2239 const struct got_error *error = NULL;
2240 struct got_worktree *worktree = NULL;
2241 struct got_repository *repo = NULL;
2242 char *cwd = NULL, *path = NULL;
2243 int ch;
2245 while ((ch = getopt(argc, argv, "")) != -1) {
2246 switch (ch) {
2247 default:
2248 usage_revert();
2249 /* NOTREACHED */
2253 argc -= optind;
2254 argv += optind;
2256 if (argc != 1)
2257 usage_revert();
2259 path = realpath(argv[0], NULL);
2260 if (path == NULL) {
2261 error = got_error_from_errno2("realpath", argv[0]);
2262 goto done;
2264 got_path_strip_trailing_slashes(path);
2266 cwd = getcwd(NULL, 0);
2267 if (cwd == NULL) {
2268 error = got_error_from_errno("getcwd");
2269 goto done;
2271 error = got_worktree_open(&worktree, cwd);
2272 if (error)
2273 goto done;
2275 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2276 if (error != NULL)
2277 goto done;
2279 error = apply_unveil(got_repo_get_path(repo), 1,
2280 got_worktree_get_root_path(worktree), 0);
2281 if (error)
2282 goto done;
2284 error = got_worktree_revert(worktree, path,
2285 revert_progress, NULL, repo);
2286 if (error)
2287 goto done;
2288 done:
2289 if (repo)
2290 got_repo_close(repo);
2291 if (worktree)
2292 got_worktree_close(worktree);
2293 free(path);
2294 free(cwd);
2295 return error;
2298 __dead static void
2299 usage_commit(void)
2301 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2302 exit(1);
2305 int
2306 spawn_editor(const char *editor, const char *file)
2308 pid_t pid;
2309 sig_t sighup, sigint, sigquit;
2310 int st = -1;
2312 sighup = signal(SIGHUP, SIG_IGN);
2313 sigint = signal(SIGINT, SIG_IGN);
2314 sigquit = signal(SIGQUIT, SIG_IGN);
2316 switch (pid = fork()) {
2317 case -1:
2318 goto doneediting;
2319 case 0:
2320 execl(editor, editor, file, (char *)NULL);
2321 _exit(127);
2324 while (waitpid(pid, &st, 0) == -1)
2325 if (errno != EINTR)
2326 break;
2328 doneediting:
2329 (void)signal(SIGHUP, sighup);
2330 (void)signal(SIGINT, sigint);
2331 (void)signal(SIGQUIT, sigquit);
2333 if (!WIFEXITED(st)) {
2334 errno = EINTR;
2335 return -1;
2338 return WEXITSTATUS(st);
2341 struct collect_commit_logmsg_arg {
2342 const char *cmdline_log;
2343 const char *editor;
2344 const char *worktree_path;
2345 const char *repo_path;
2346 char *logmsg_path;
2350 static const struct got_error *
2351 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2352 void *arg)
2354 const char *initial_content = "\n# changes to be committed:\n";
2355 struct got_pathlist_entry *pe;
2356 const struct got_error *err = NULL;
2357 char *template = NULL;
2358 struct collect_commit_logmsg_arg *a = arg;
2359 char buf[1024];
2360 struct stat st, st2;
2361 FILE *fp;
2362 size_t len;
2363 int fd, content_changed = 0;
2365 /* if a message was specified on the command line, just use it */
2366 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2367 len = strlen(a->cmdline_log) + 1;
2368 *logmsg = malloc(len + 1);
2369 if (*logmsg == NULL)
2370 return got_error_from_errno("malloc");
2371 strlcpy(*logmsg, a->cmdline_log, len);
2372 return NULL;
2375 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2376 return got_error_from_errno("asprintf");
2378 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2379 if (err)
2380 goto done;
2382 dprintf(fd, initial_content);
2384 TAILQ_FOREACH(pe, commitable_paths, entry) {
2385 struct got_commitable *ct = pe->data;
2386 dprintf(fd, "# %c %s\n",
2387 got_commitable_get_status(ct),
2388 got_commitable_get_path(ct));
2390 close(fd);
2392 if (stat(a->logmsg_path, &st) == -1) {
2393 err = got_error_from_errno2("stat", a->logmsg_path);
2394 goto done;
2397 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2398 err = got_error_from_errno("failed spawning editor");
2399 goto done;
2402 if (stat(a->logmsg_path, &st2) == -1) {
2403 err = got_error_from_errno("stat");
2404 goto done;
2407 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2408 unlink(a->logmsg_path);
2409 free(a->logmsg_path);
2410 a->logmsg_path = NULL;
2411 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2412 "no changes made to commit message, aborting");
2413 goto done;
2416 *logmsg = malloc(st2.st_size + 1);
2417 if (*logmsg == NULL) {
2418 err = got_error_from_errno("malloc");
2419 goto done;
2421 (*logmsg)[0] = '\0';
2422 len = 0;
2424 fp = fopen(a->logmsg_path, "r");
2425 if (fp == NULL) {
2426 err = got_error_from_errno("fopen");
2427 goto done;
2429 while (fgets(buf, sizeof(buf), fp) != NULL) {
2430 if (!content_changed && strcmp(buf, initial_content) != 0)
2431 content_changed = 1;
2432 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2433 continue; /* remove comments and leading empty lines */
2434 len = strlcat(*logmsg, buf, st2.st_size);
2436 fclose(fp);
2438 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2439 (*logmsg)[len - 1] = '\0';
2440 len--;
2443 if (len == 0 || !content_changed) {
2444 unlink(a->logmsg_path);
2445 free(a->logmsg_path);
2446 a->logmsg_path = NULL;
2447 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2448 "commit message cannot be empty, aborting");
2449 goto done;
2451 done:
2452 free(template);
2454 /* Editor is done; we can now apply unveil(2) */
2455 if (err == NULL)
2456 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2457 return err;
2460 static const struct got_error *
2461 cmd_commit(int argc, char *argv[])
2463 const struct got_error *error = NULL;
2464 struct got_worktree *worktree = NULL;
2465 struct got_repository *repo = NULL;
2466 char *cwd = NULL, *path = NULL, *id_str = NULL;
2467 struct got_object_id *id = NULL;
2468 const char *logmsg = NULL;
2469 const char *got_author = getenv("GOT_AUTHOR");
2470 struct collect_commit_logmsg_arg cl_arg;
2471 char *editor = NULL;
2472 int ch;
2474 while ((ch = getopt(argc, argv, "m:")) != -1) {
2475 switch (ch) {
2476 case 'm':
2477 logmsg = optarg;
2478 break;
2479 default:
2480 usage_commit();
2481 /* NOTREACHED */
2485 argc -= optind;
2486 argv += optind;
2488 if (argc == 1) {
2489 path = realpath(argv[0], NULL);
2490 if (path == NULL) {
2491 error = got_error_from_errno2("realpath", argv[0]);
2492 goto done;
2494 got_path_strip_trailing_slashes(path);
2495 } else if (argc != 0)
2496 usage_commit();
2498 if (got_author == NULL) {
2499 /* TODO: Look current user up in password database */
2500 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2501 goto done;
2504 cwd = getcwd(NULL, 0);
2505 if (cwd == NULL) {
2506 error = got_error_from_errno("getcwd");
2507 goto done;
2509 error = got_worktree_open(&worktree, cwd);
2510 if (error)
2511 goto done;
2513 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2514 if (error != NULL)
2515 goto done;
2518 * unveil(2) traverses exec(2); if an editor is used we have
2519 * to apply unveil after the log message has been written.
2521 if (logmsg == NULL || strlen(logmsg) == 0)
2522 error = get_editor(&editor);
2523 else
2524 error = apply_unveil(got_repo_get_path(repo), 0,
2525 got_worktree_get_root_path(worktree), 0);
2526 if (error)
2527 goto done;
2529 cl_arg.editor = editor;
2530 cl_arg.cmdline_log = logmsg;
2531 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2532 cl_arg.repo_path = got_repo_get_path(repo);
2533 cl_arg.logmsg_path = NULL;
2534 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2535 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2536 if (error) {
2537 if (cl_arg.logmsg_path)
2538 fprintf(stderr, "%s: log message preserved in %s\n",
2539 getprogname(), cl_arg.logmsg_path);
2540 goto done;
2543 if (cl_arg.logmsg_path)
2544 unlink(cl_arg.logmsg_path);
2546 error = got_object_id_str(&id_str, id);
2547 if (error)
2548 goto done;
2549 printf("created commit %s\n", id_str);
2550 done:
2551 if (repo)
2552 got_repo_close(repo);
2553 if (worktree)
2554 got_worktree_close(worktree);
2555 free(path);
2556 free(cwd);
2557 free(id_str);
2558 free(editor);
2559 return error;