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_init(void);
78 __dead static void usage_checkout(void);
79 __dead static void usage_update(void);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_status(void);
85 __dead static void usage_ref(void);
86 __dead static void usage_add(void);
87 __dead static void usage_rm(void);
88 __dead static void usage_revert(void);
89 __dead static void usage_commit(void);
90 __dead static void usage_cherrypick(void);
91 __dead static void usage_backout(void);
93 static const struct got_error* cmd_init(int, char *[]);
94 static const struct got_error* cmd_checkout(int, char *[]);
95 static const struct got_error* cmd_update(int, char *[]);
96 static const struct got_error* cmd_log(int, char *[]);
97 static const struct got_error* cmd_diff(int, char *[]);
98 static const struct got_error* cmd_blame(int, char *[]);
99 static const struct got_error* cmd_tree(int, char *[]);
100 static const struct got_error* cmd_status(int, char *[]);
101 static const struct got_error* cmd_ref(int, char *[]);
102 static const struct got_error* cmd_add(int, char *[]);
103 static const struct got_error* cmd_rm(int, char *[]);
104 static const struct got_error* cmd_revert(int, char *[]);
105 static const struct got_error* cmd_commit(int, char *[]);
106 static const struct got_error* cmd_cherrypick(int, char *[]);
107 static const struct got_error* cmd_backout(int, char *[]);
109 static struct cmd got_commands[] = {
110 { "init", cmd_init, usage_init,
111 "create a new empty repository" },
112 { "checkout", cmd_checkout, usage_checkout,
113 "check out a new work tree from a repository" },
114 { "update", cmd_update, usage_update,
115 "update a work tree to a different commit" },
116 { "log", cmd_log, usage_log,
117 "show repository history" },
118 { "diff", cmd_diff, usage_diff,
119 "compare files and directories" },
120 { "blame", cmd_blame, usage_blame,
121 "show when lines in a file were changed" },
122 { "tree", cmd_tree, usage_tree,
123 "list files and directories in repository" },
124 { "status", cmd_status, usage_status,
125 "show modification status of files" },
126 { "ref", cmd_ref, usage_ref,
127 "manage references in repository" },
128 { "add", cmd_add, usage_add,
129 "add new files to version control" },
130 { "rm", cmd_rm, usage_rm,
131 "remove a versioned file" },
132 { "revert", cmd_revert, usage_revert,
133 "revert uncommitted changes" },
134 { "commit", cmd_commit, usage_commit,
135 "write changes from work tree to repository" },
136 { "cherrypick", cmd_cherrypick, usage_cherrypick,
137 "merge a single commit from another branch into a work tree" },
138 { "backout", cmd_backout, usage_backout,
139 "reverse-merge changes from a commit into a work tree" },
140 };
142 int
143 main(int argc, char *argv[])
145 struct cmd *cmd;
146 unsigned int i;
147 int ch;
148 int hflag = 0;
150 setlocale(LC_CTYPE, "");
152 while ((ch = getopt(argc, argv, "h")) != -1) {
153 switch (ch) {
154 case 'h':
155 hflag = 1;
156 break;
157 default:
158 usage();
159 /* NOTREACHED */
163 argc -= optind;
164 argv += optind;
165 optind = 0;
167 if (argc <= 0)
168 usage();
170 signal(SIGINT, catch_sigint);
171 signal(SIGPIPE, catch_sigpipe);
173 for (i = 0; i < nitems(got_commands); i++) {
174 const struct got_error *error;
176 cmd = &got_commands[i];
178 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
179 continue;
181 if (hflag)
182 got_commands[i].cmd_usage();
184 error = got_commands[i].cmd_main(argc, argv);
185 if (error && !(sigint_received || sigpipe_received)) {
186 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
187 return 1;
190 return 0;
193 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
194 return 1;
197 __dead static void
198 usage(void)
200 int i;
202 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
203 "Available commands:\n", getprogname());
204 for (i = 0; i < nitems(got_commands); i++) {
205 struct cmd *cmd = &got_commands[i];
206 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
208 exit(1);
211 static const struct got_error *
212 get_editor(char **abspath)
214 const struct got_error *err = NULL;
215 const char *editor;
217 editor = getenv("VISUAL");
218 if (editor == NULL)
219 editor = getenv("EDITOR");
221 if (editor) {
222 err = got_path_find_prog(abspath, editor);
223 if (err)
224 return err;
227 if (*abspath == NULL) {
228 *abspath = strdup("/bin/ed");
229 if (*abspath == NULL)
230 return got_error_from_errno("strdup");
233 return NULL;
236 static const struct got_error *
237 apply_unveil(const char *repo_path, int repo_read_only,
238 const char *worktree_path, int create_worktree)
240 const struct got_error *err;
242 if (create_worktree) {
243 /* Pre-create work tree path to avoid unveiling its parents. */
244 err = got_path_mkdir(worktree_path);
246 if (errno == EEXIST) {
247 if (got_path_dir_is_empty(worktree_path)) {
248 errno = 0;
249 err = NULL;
250 } else {
251 err = got_error_path(worktree_path,
252 GOT_ERR_DIR_NOT_EMPTY);
256 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
257 return err;
260 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
261 return got_error_from_errno2("unveil", repo_path);
263 if (worktree_path && unveil(worktree_path, "rwc") != 0)
264 return got_error_from_errno2("unveil", worktree_path);
266 if (unveil("/tmp", "rwc") != 0)
267 return got_error_from_errno2("unveil", "/tmp");
269 err = got_privsep_unveil_exec_helpers();
270 if (err != NULL)
271 return err;
273 if (unveil(NULL, NULL) != 0)
274 return got_error_from_errno("unveil");
276 return NULL;
279 __dead static void
280 usage_init(void)
282 fprintf(stderr, "usage: %s init path\n", getprogname());
283 exit(1);
286 static const struct got_error *
287 cmd_init(int argc, char *argv[])
289 const struct got_error *error = NULL;
290 char *repo_path = NULL;
291 int ch;
293 while ((ch = getopt(argc, argv, "")) != -1) {
294 switch (ch) {
295 default:
296 usage_init();
297 /* NOTREACHED */
301 argc -= optind;
302 argv += optind;
304 #ifndef PROFILE
305 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
306 err(1, "pledge");
307 #endif
308 if (argc != 1)
309 usage_init();
311 repo_path = strdup(argv[0]);
312 if (repo_path == NULL)
313 return got_error_from_errno("strdup");
315 got_path_strip_trailing_slashes(repo_path);
317 error = got_path_mkdir(repo_path);
318 if (error &&
319 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
320 goto done;
322 error = apply_unveil(repo_path, 0, NULL, 0);
323 if (error)
324 goto done;
326 error = got_repo_init(repo_path);
327 if (error != NULL)
328 goto done;
330 done:
331 free(repo_path);
332 return error;
335 __dead static void
336 usage_checkout(void)
338 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
339 "[-p prefix] repository-path [worktree-path]\n", getprogname());
340 exit(1);
343 static void
344 checkout_progress(void *arg, unsigned char status, const char *path)
346 char *worktree_path = arg;
348 /* Base commit bump happens silently. */
349 if (status == GOT_STATUS_BUMP_BASE)
350 return;
352 while (path[0] == '/')
353 path++;
355 printf("%c %s/%s\n", status, worktree_path, path);
358 static const struct got_error *
359 check_cancelled(void *arg)
361 if (sigint_received || sigpipe_received)
362 return got_error(GOT_ERR_CANCELLED);
363 return NULL;
366 static const struct got_error *
367 check_linear_ancestry(struct got_object_id *commit_id,
368 struct got_object_id *base_commit_id, struct got_repository *repo)
370 const struct got_error *err = NULL;
371 struct got_object_id *yca_id;
373 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
374 commit_id, base_commit_id, repo);
375 if (err)
376 return err;
378 if (yca_id == NULL)
379 return got_error(GOT_ERR_ANCESTRY);
381 /*
382 * Require a straight line of history between the target commit
383 * and the work tree's base commit.
385 * Non-linear situations such as this require a rebase:
387 * (commit) D F (base_commit)
388 * \ /
389 * C E
390 * \ /
391 * B (yca)
392 * |
393 * A
395 * 'got update' only handles linear cases:
396 * Update forwards in time: A (base/yca) - B - C - D (commit)
397 * Update backwards in time: D (base) - C - B - A (commit/yca)
398 */
399 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
400 got_object_id_cmp(base_commit_id, yca_id) != 0)
401 return got_error(GOT_ERR_ANCESTRY);
403 free(yca_id);
404 return NULL;
407 static const struct got_error *
408 check_same_branch(struct got_object_id *commit_id,
409 struct got_reference *head_ref, struct got_repository *repo)
411 const struct got_error *err = NULL;
412 struct got_commit_graph *graph = NULL;
413 struct got_object_id *head_commit_id = NULL;
414 int is_same_branch = 0;
416 err = got_ref_resolve(&head_commit_id, repo, head_ref);
417 if (err)
418 goto done;
420 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
421 if (err)
422 goto done;
424 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
425 if (err)
426 goto done;
428 for (;;) {
429 struct got_object_id *id;
430 err = got_commit_graph_iter_next(&id, graph);
431 if (err) {
432 if (err->code == GOT_ERR_ITER_COMPLETED) {
433 err = NULL;
434 break;
436 else if (err->code != GOT_ERR_ITER_NEED_MORE)
437 break;
438 err = got_commit_graph_fetch_commits(graph, 1,
439 repo);
440 if (err)
441 break;
444 if (id) {
445 if (got_object_id_cmp(id, commit_id) == 0) {
446 is_same_branch = 1;
447 break;
451 done:
452 if (graph)
453 got_commit_graph_close(graph);
454 free(head_commit_id);
455 if (!err && !is_same_branch)
456 err = got_error(GOT_ERR_ANCESTRY);
457 return err;
460 static const struct got_error *
461 cmd_checkout(int argc, char *argv[])
463 const struct got_error *error = NULL;
464 struct got_repository *repo = NULL;
465 struct got_reference *head_ref = NULL;
466 struct got_worktree *worktree = NULL;
467 char *repo_path = NULL;
468 char *worktree_path = NULL;
469 const char *path_prefix = "";
470 const char *branch_name = GOT_REF_HEAD;
471 char *commit_id_str = NULL;
472 int ch, same_path_prefix;
474 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
475 switch (ch) {
476 case 'b':
477 branch_name = optarg;
478 break;
479 case 'c':
480 commit_id_str = strdup(optarg);
481 if (commit_id_str == NULL)
482 return got_error_from_errno("strdup");
483 break;
484 case 'p':
485 path_prefix = optarg;
486 break;
487 default:
488 usage_checkout();
489 /* NOTREACHED */
493 argc -= optind;
494 argv += optind;
496 #ifndef PROFILE
497 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
498 "unveil", NULL) == -1)
499 err(1, "pledge");
500 #endif
501 if (argc == 1) {
502 char *cwd, *base, *dotgit;
503 repo_path = realpath(argv[0], NULL);
504 if (repo_path == NULL)
505 return got_error_from_errno2("realpath", argv[0]);
506 cwd = getcwd(NULL, 0);
507 if (cwd == NULL) {
508 error = got_error_from_errno("getcwd");
509 goto done;
511 if (path_prefix[0]) {
512 base = basename(path_prefix);
513 if (base == NULL) {
514 error = got_error_from_errno2("basename",
515 path_prefix);
516 goto done;
518 } else {
519 base = basename(repo_path);
520 if (base == NULL) {
521 error = got_error_from_errno2("basename",
522 repo_path);
523 goto done;
526 dotgit = strstr(base, ".git");
527 if (dotgit)
528 *dotgit = '\0';
529 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
530 error = got_error_from_errno("asprintf");
531 free(cwd);
532 goto done;
534 free(cwd);
535 } else if (argc == 2) {
536 repo_path = realpath(argv[0], NULL);
537 if (repo_path == NULL) {
538 error = got_error_from_errno2("realpath", argv[0]);
539 goto done;
541 worktree_path = realpath(argv[1], NULL);
542 if (worktree_path == NULL) {
543 error = got_error_from_errno2("realpath", argv[1]);
544 goto done;
546 } else
547 usage_checkout();
549 got_path_strip_trailing_slashes(repo_path);
550 got_path_strip_trailing_slashes(worktree_path);
552 error = got_repo_open(&repo, repo_path);
553 if (error != NULL)
554 goto done;
556 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
557 if (error)
558 goto done;
560 error = got_ref_open(&head_ref, repo, branch_name, 0);
561 if (error != NULL)
562 goto done;
564 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
565 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
566 goto done;
568 error = got_worktree_open(&worktree, worktree_path);
569 if (error != NULL)
570 goto done;
572 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
573 path_prefix);
574 if (error != NULL)
575 goto done;
576 if (!same_path_prefix) {
577 error = got_error(GOT_ERR_PATH_PREFIX);
578 goto done;
581 if (commit_id_str) {
582 struct got_object_id *commit_id;
583 error = got_object_resolve_id_str(&commit_id, repo,
584 commit_id_str);
585 if (error != NULL)
586 goto done;
587 error = check_linear_ancestry(commit_id,
588 got_worktree_get_base_commit_id(worktree), repo);
589 if (error != NULL) {
590 free(commit_id);
591 goto done;
593 error = check_same_branch(commit_id, head_ref, repo);
594 if (error)
595 goto done;
596 error = got_worktree_set_base_commit_id(worktree, repo,
597 commit_id);
598 free(commit_id);
599 if (error)
600 goto done;
603 error = got_worktree_checkout_files(worktree, "", repo,
604 checkout_progress, worktree_path, check_cancelled, NULL);
605 if (error != NULL)
606 goto done;
608 printf("Now shut up and hack\n");
610 done:
611 free(commit_id_str);
612 free(repo_path);
613 free(worktree_path);
614 return error;
617 __dead static void
618 usage_update(void)
620 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
621 getprogname());
622 exit(1);
625 static void
626 update_progress(void *arg, unsigned char status, const char *path)
628 int *did_something = arg;
630 if (status == GOT_STATUS_EXISTS)
631 return;
633 *did_something = 1;
635 /* Base commit bump happens silently. */
636 if (status == GOT_STATUS_BUMP_BASE)
637 return;
639 while (path[0] == '/')
640 path++;
641 printf("%c %s\n", status, path);
644 static const struct got_error *
645 switch_head_ref(struct got_reference *head_ref,
646 struct got_object_id *commit_id, struct got_worktree *worktree,
647 struct got_repository *repo)
649 const struct got_error *err = NULL;
650 char *base_id_str;
651 int ref_has_moved = 0;
653 /* Trivial case: switching between two different references. */
654 if (strcmp(got_ref_get_name(head_ref),
655 got_worktree_get_head_ref_name(worktree)) != 0) {
656 printf("Switching work tree from %s to %s\n",
657 got_worktree_get_head_ref_name(worktree),
658 got_ref_get_name(head_ref));
659 return got_worktree_set_head_ref(worktree, head_ref);
662 err = check_linear_ancestry(commit_id,
663 got_worktree_get_base_commit_id(worktree), repo);
664 if (err) {
665 if (err->code != GOT_ERR_ANCESTRY)
666 return err;
667 ref_has_moved = 1;
669 if (!ref_has_moved)
670 return NULL;
672 /* Switching to a rebased branch with the same reference name. */
673 err = got_object_id_str(&base_id_str,
674 got_worktree_get_base_commit_id(worktree));
675 if (err)
676 return err;
677 printf("Reference %s now points at a different branch\n",
678 got_worktree_get_head_ref_name(worktree));
679 printf("Switching work tree from %s to %s\n", base_id_str,
680 got_worktree_get_head_ref_name(worktree));
681 return NULL;
684 static const struct got_error *
685 cmd_update(int argc, char *argv[])
687 const struct got_error *error = NULL;
688 struct got_repository *repo = NULL;
689 struct got_worktree *worktree = NULL;
690 char *worktree_path = NULL, *path = NULL;
691 struct got_object_id *commit_id = NULL;
692 char *commit_id_str = NULL;
693 const char *branch_name = NULL;
694 struct got_reference *head_ref = NULL;
695 int ch, did_something = 0;
697 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
698 switch (ch) {
699 case 'b':
700 branch_name = optarg;
701 break;
702 case 'c':
703 commit_id_str = strdup(optarg);
704 if (commit_id_str == NULL)
705 return got_error_from_errno("strdup");
706 break;
707 default:
708 usage_update();
709 /* NOTREACHED */
713 argc -= optind;
714 argv += optind;
716 #ifndef PROFILE
717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
718 "unveil", NULL) == -1)
719 err(1, "pledge");
720 #endif
721 worktree_path = getcwd(NULL, 0);
722 if (worktree_path == NULL) {
723 error = got_error_from_errno("getcwd");
724 goto done;
726 error = got_worktree_open(&worktree, worktree_path);
727 if (error)
728 goto done;
730 if (argc == 0) {
731 path = strdup("");
732 if (path == NULL) {
733 error = got_error_from_errno("strdup");
734 goto done;
736 } else if (argc == 1) {
737 error = got_worktree_resolve_path(&path, worktree, argv[0]);
738 if (error)
739 goto done;
740 } else
741 usage_update();
743 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
744 if (error != NULL)
745 goto done;
747 error = apply_unveil(got_repo_get_path(repo), 0,
748 got_worktree_get_root_path(worktree), 0);
749 if (error)
750 goto done;
752 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
753 got_worktree_get_head_ref_name(worktree), 0);
754 if (error != NULL)
755 goto done;
756 if (commit_id_str == NULL) {
757 error = got_ref_resolve(&commit_id, repo, head_ref);
758 if (error != NULL)
759 goto done;
760 error = got_object_id_str(&commit_id_str, commit_id);
761 if (error != NULL)
762 goto done;
763 } else {
764 error = got_object_resolve_id_str(&commit_id, repo,
765 commit_id_str);
766 if (error != NULL)
767 goto done;
770 if (branch_name) {
771 struct got_object_id *head_commit_id;
772 if (strlen(path) != 0) {
773 fprintf(stderr, "%s: switching between branches "
774 "requires that the entire work tree "
775 "gets updated, not just '%s'\n",
776 getprogname(), path);
777 error = got_error(GOT_ERR_BAD_PATH);
778 goto done;
780 error = got_ref_resolve(&head_commit_id, repo, head_ref);
781 if (error)
782 goto done;
783 error = check_linear_ancestry(commit_id, head_commit_id, repo);
784 free(head_commit_id);
785 if (error != NULL)
786 goto done;
787 error = check_same_branch(commit_id, head_ref, repo);
788 if (error)
789 goto done;
790 error = switch_head_ref(head_ref, commit_id, worktree, repo);
791 if (error)
792 goto done;
793 } else {
794 error = check_linear_ancestry(commit_id,
795 got_worktree_get_base_commit_id(worktree), repo);
796 if (error != NULL) {
797 if (error->code == GOT_ERR_ANCESTRY)
798 error = got_error(GOT_ERR_BRANCH_MOVED);
799 goto done;
801 error = check_same_branch(commit_id, head_ref, repo);
802 if (error)
803 goto done;
806 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
807 commit_id) != 0) {
808 error = got_worktree_set_base_commit_id(worktree, repo,
809 commit_id);
810 if (error)
811 goto done;
814 error = got_worktree_checkout_files(worktree, path, repo,
815 update_progress, &did_something, check_cancelled, NULL);
816 if (error != NULL)
817 goto done;
819 if (did_something)
820 printf("Updated to commit %s\n", commit_id_str);
821 else
822 printf("Already up-to-date\n");
823 done:
824 free(worktree_path);
825 free(path);
826 free(commit_id);
827 free(commit_id_str);
828 return error;
831 static const struct got_error *
832 print_patch(struct got_commit_object *commit, struct got_object_id *id,
833 int diff_context, struct got_repository *repo)
835 const struct got_error *err = NULL;
836 struct got_tree_object *tree1 = NULL, *tree2;
837 struct got_object_qid *qid;
838 char *id_str1 = NULL, *id_str2;
839 struct got_diff_blob_output_unidiff_arg arg;
841 err = got_object_open_as_tree(&tree2, repo,
842 got_object_commit_get_tree_id(commit));
843 if (err)
844 return err;
846 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
847 if (qid != NULL) {
848 struct got_commit_object *pcommit;
850 err = got_object_open_as_commit(&pcommit, repo, qid->id);
851 if (err)
852 return err;
854 err = got_object_open_as_tree(&tree1, repo,
855 got_object_commit_get_tree_id(pcommit));
856 got_object_commit_close(pcommit);
857 if (err)
858 return err;
860 err = got_object_id_str(&id_str1, qid->id);
861 if (err)
862 return err;
865 err = got_object_id_str(&id_str2, id);
866 if (err)
867 goto done;
869 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
870 arg.diff_context = diff_context;
871 arg.outfile = stdout;
872 err = got_diff_tree(tree1, tree2, "", "", repo,
873 got_diff_blob_output_unidiff, &arg);
874 done:
875 if (tree1)
876 got_object_tree_close(tree1);
877 got_object_tree_close(tree2);
878 free(id_str1);
879 free(id_str2);
880 return err;
883 static char *
884 get_datestr(time_t *time, char *datebuf)
886 char *p, *s = ctime_r(time, datebuf);
887 p = strchr(s, '\n');
888 if (p)
889 *p = '\0';
890 return s;
893 static const struct got_error *
894 print_commit(struct got_commit_object *commit, struct got_object_id *id,
895 struct got_repository *repo, int show_patch, int diff_context,
896 struct got_reflist_head *refs)
898 const struct got_error *err = NULL;
899 char *id_str, *datestr, *logmsg0, *logmsg, *line;
900 char datebuf[26];
901 time_t committer_time;
902 const char *author, *committer;
903 char *refs_str = NULL;
904 struct got_reflist_entry *re;
906 SIMPLEQ_FOREACH(re, refs, entry) {
907 char *s;
908 const char *name;
909 if (got_object_id_cmp(re->id, id) != 0)
910 continue;
911 name = got_ref_get_name(re->ref);
912 if (strcmp(name, GOT_REF_HEAD) == 0)
913 continue;
914 if (strncmp(name, "refs/", 5) == 0)
915 name += 5;
916 if (strncmp(name, "got/", 4) == 0)
917 continue;
918 if (strncmp(name, "heads/", 6) == 0)
919 name += 6;
920 if (strncmp(name, "remotes/", 8) == 0)
921 name += 8;
922 s = refs_str;
923 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
924 name) == -1) {
925 err = got_error_from_errno("asprintf");
926 free(s);
927 break;
929 free(s);
931 err = got_object_id_str(&id_str, id);
932 if (err)
933 return err;
935 printf("-----------------------------------------------\n");
936 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
937 refs_str ? refs_str : "", refs_str ? ")" : "");
938 free(id_str);
939 id_str = NULL;
940 free(refs_str);
941 refs_str = NULL;
942 printf("from: %s\n", got_object_commit_get_author(commit));
943 committer_time = got_object_commit_get_committer_time(commit);
944 datestr = get_datestr(&committer_time, datebuf);
945 printf("date: %s UTC\n", datestr);
946 author = got_object_commit_get_author(commit);
947 committer = got_object_commit_get_committer(commit);
948 if (strcmp(author, committer) != 0)
949 printf("via: %s\n", committer);
950 if (got_object_commit_get_nparents(commit) > 1) {
951 const struct got_object_id_queue *parent_ids;
952 struct got_object_qid *qid;
953 int n = 1;
954 parent_ids = got_object_commit_get_parent_ids(commit);
955 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
956 err = got_object_id_str(&id_str, qid->id);
957 if (err)
958 return err;
959 printf("parent %d: %s\n", n++, id_str);
960 free(id_str);
964 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
965 if (logmsg0 == NULL)
966 return got_error_from_errno("strdup");
968 logmsg = logmsg0;
969 do {
970 line = strsep(&logmsg, "\n");
971 if (line)
972 printf(" %s\n", line);
973 } while (line);
974 free(logmsg0);
976 if (show_patch) {
977 err = print_patch(commit, id, diff_context, repo);
978 if (err == 0)
979 printf("\n");
982 if (fflush(stdout) != 0 && err == NULL)
983 err = got_error_from_errno("fflush");
984 return err;
987 static const struct got_error *
988 print_commits(struct got_object_id *root_id, struct got_repository *repo,
989 char *path, int show_patch, int diff_context, int limit,
990 int first_parent_traversal, struct got_reflist_head *refs)
992 const struct got_error *err;
993 struct got_commit_graph *graph;
995 err = got_commit_graph_open(&graph, root_id, path,
996 first_parent_traversal, repo);
997 if (err)
998 return err;
999 err = got_commit_graph_iter_start(graph, root_id, repo);
1000 if (err)
1001 goto done;
1002 for (;;) {
1003 struct got_commit_object *commit;
1004 struct got_object_id *id;
1006 if (sigint_received || sigpipe_received)
1007 break;
1009 err = got_commit_graph_iter_next(&id, graph);
1010 if (err) {
1011 if (err->code == GOT_ERR_ITER_COMPLETED) {
1012 err = NULL;
1013 break;
1015 if (err->code != GOT_ERR_ITER_NEED_MORE)
1016 break;
1017 err = got_commit_graph_fetch_commits(graph, 1, repo);
1018 if (err)
1019 break;
1020 else
1021 continue;
1023 if (id == NULL)
1024 break;
1026 err = got_object_open_as_commit(&commit, repo, id);
1027 if (err)
1028 break;
1029 err = print_commit(commit, id, repo, show_patch, diff_context,
1030 refs);
1031 got_object_commit_close(commit);
1032 if (err || (limit && --limit == 0))
1033 break;
1035 done:
1036 got_commit_graph_close(graph);
1037 return err;
1040 __dead static void
1041 usage_log(void)
1043 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1044 "[-r repository-path] [path]\n", getprogname());
1045 exit(1);
1048 static const struct got_error *
1049 cmd_log(int argc, char *argv[])
1051 const struct got_error *error;
1052 struct got_repository *repo = NULL;
1053 struct got_worktree *worktree = NULL;
1054 struct got_commit_object *commit = NULL;
1055 struct got_object_id *id = NULL;
1056 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1057 char *start_commit = NULL;
1058 int diff_context = 3, ch;
1059 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1060 const char *errstr;
1061 struct got_reflist_head refs;
1063 SIMPLEQ_INIT(&refs);
1065 #ifndef PROFILE
1066 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1067 NULL)
1068 == -1)
1069 err(1, "pledge");
1070 #endif
1072 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1073 switch (ch) {
1074 case 'b':
1075 first_parent_traversal = 1;
1076 break;
1077 case 'p':
1078 show_patch = 1;
1079 break;
1080 case 'c':
1081 start_commit = optarg;
1082 break;
1083 case 'C':
1084 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1085 &errstr);
1086 if (errstr != NULL)
1087 err(1, "-C option %s", errstr);
1088 break;
1089 case 'l':
1090 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1091 if (errstr != NULL)
1092 err(1, "-l option %s", errstr);
1093 break;
1094 case 'r':
1095 repo_path = realpath(optarg, NULL);
1096 if (repo_path == NULL)
1097 err(1, "-r option");
1098 got_path_strip_trailing_slashes(repo_path);
1099 break;
1100 default:
1101 usage_log();
1102 /* NOTREACHED */
1106 argc -= optind;
1107 argv += optind;
1109 cwd = getcwd(NULL, 0);
1110 if (cwd == NULL) {
1111 error = got_error_from_errno("getcwd");
1112 goto done;
1115 error = got_worktree_open(&worktree, cwd);
1116 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1117 goto done;
1118 error = NULL;
1120 if (argc == 0) {
1121 path = strdup("");
1122 if (path == NULL) {
1123 error = got_error_from_errno("strdup");
1124 goto done;
1126 } else if (argc == 1) {
1127 if (worktree) {
1128 error = got_worktree_resolve_path(&path, worktree,
1129 argv[0]);
1130 if (error)
1131 goto done;
1132 } else {
1133 path = strdup(argv[0]);
1134 if (path == NULL) {
1135 error = got_error_from_errno("strdup");
1136 goto done;
1139 } else
1140 usage_log();
1142 if (repo_path == NULL) {
1143 repo_path = worktree ?
1144 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1146 if (repo_path == NULL) {
1147 error = got_error_from_errno("strdup");
1148 goto done;
1151 error = got_repo_open(&repo, repo_path);
1152 if (error != NULL)
1153 goto done;
1155 error = apply_unveil(got_repo_get_path(repo), 1,
1156 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1157 if (error)
1158 goto done;
1160 if (start_commit == NULL) {
1161 struct got_reference *head_ref;
1162 error = got_ref_open(&head_ref, repo,
1163 worktree ? got_worktree_get_head_ref_name(worktree)
1164 : GOT_REF_HEAD, 0);
1165 if (error != NULL)
1166 return error;
1167 error = got_ref_resolve(&id, repo, head_ref);
1168 got_ref_close(head_ref);
1169 if (error != NULL)
1170 return error;
1171 error = got_object_open_as_commit(&commit, repo, id);
1172 } else {
1173 struct got_reference *ref;
1174 error = got_ref_open(&ref, repo, start_commit, 0);
1175 if (error == NULL) {
1176 int obj_type;
1177 error = got_ref_resolve(&id, repo, ref);
1178 got_ref_close(ref);
1179 if (error != NULL)
1180 goto done;
1181 error = got_object_get_type(&obj_type, repo, id);
1182 if (error != NULL)
1183 goto done;
1184 if (obj_type == GOT_OBJ_TYPE_TAG) {
1185 struct got_tag_object *tag;
1186 error = got_object_open_as_tag(&tag, repo, id);
1187 if (error != NULL)
1188 goto done;
1189 if (got_object_tag_get_object_type(tag) !=
1190 GOT_OBJ_TYPE_COMMIT) {
1191 got_object_tag_close(tag);
1192 error = got_error(GOT_ERR_OBJ_TYPE);
1193 goto done;
1195 free(id);
1196 id = got_object_id_dup(
1197 got_object_tag_get_object_id(tag));
1198 if (id == NULL)
1199 error = got_error_from_errno(
1200 "got_object_id_dup");
1201 got_object_tag_close(tag);
1202 if (error)
1203 goto done;
1204 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1205 error = got_error(GOT_ERR_OBJ_TYPE);
1206 goto done;
1208 error = got_object_open_as_commit(&commit, repo, id);
1209 if (error != NULL)
1210 goto done;
1212 if (commit == NULL) {
1213 error = got_object_resolve_id_str(&id, repo,
1214 start_commit);
1215 if (error != NULL)
1216 return error;
1219 if (error != NULL)
1220 goto done;
1222 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1223 if (error != NULL)
1224 goto done;
1225 if (in_repo_path) {
1226 free(path);
1227 path = in_repo_path;
1230 error = got_ref_list(&refs, repo);
1231 if (error)
1232 goto done;
1234 error = print_commits(id, repo, path, show_patch,
1235 diff_context, limit, first_parent_traversal, &refs);
1236 done:
1237 free(path);
1238 free(repo_path);
1239 free(cwd);
1240 free(id);
1241 if (worktree)
1242 got_worktree_close(worktree);
1243 if (repo) {
1244 const struct got_error *repo_error;
1245 repo_error = got_repo_close(repo);
1246 if (error == NULL)
1247 error = repo_error;
1249 got_ref_list_free(&refs);
1250 return error;
1253 __dead static void
1254 usage_diff(void)
1256 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1257 "[object1 object2 | path]\n", getprogname());
1258 exit(1);
1261 struct print_diff_arg {
1262 struct got_repository *repo;
1263 struct got_worktree *worktree;
1264 int diff_context;
1265 const char *id_str;
1266 int header_shown;
1269 static const struct got_error *
1270 print_diff(void *arg, unsigned char status, const char *path,
1271 struct got_object_id *blob_id, struct got_object_id *commit_id)
1273 struct print_diff_arg *a = arg;
1274 const struct got_error *err = NULL;
1275 struct got_blob_object *blob1 = NULL;
1276 FILE *f2 = NULL;
1277 char *abspath = NULL;
1278 struct stat sb;
1280 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1281 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1282 return NULL;
1284 if (!a->header_shown) {
1285 printf("diff %s %s\n", a->id_str,
1286 got_worktree_get_root_path(a->worktree));
1287 a->header_shown = 1;
1290 if (status != GOT_STATUS_ADD) {
1291 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1292 if (err)
1293 goto done;
1297 if (status != GOT_STATUS_DELETE) {
1298 if (asprintf(&abspath, "%s/%s",
1299 got_worktree_get_root_path(a->worktree), path) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 goto done;
1304 f2 = fopen(abspath, "r");
1305 if (f2 == NULL) {
1306 err = got_error_from_errno2("fopen", abspath);
1307 goto done;
1309 if (lstat(abspath, &sb) == -1) {
1310 err = got_error_from_errno2("lstat", abspath);
1311 goto done;
1313 } else
1314 sb.st_size = 0;
1316 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1317 stdout);
1318 done:
1319 if (blob1)
1320 got_object_blob_close(blob1);
1321 if (f2 && fclose(f2) != 0 && err == NULL)
1322 err = got_error_from_errno("fclose");
1323 free(abspath);
1324 return err;
1327 static const struct got_error *
1328 cmd_diff(int argc, char *argv[])
1330 const struct got_error *error;
1331 struct got_repository *repo = NULL;
1332 struct got_worktree *worktree = NULL;
1333 char *cwd = NULL, *repo_path = NULL;
1334 struct got_object_id *id1 = NULL, *id2 = NULL;
1335 const char *id_str1 = NULL, *id_str2 = NULL;
1336 char *label1 = NULL, *label2 = NULL;
1337 int type1, type2;
1338 int diff_context = 3, ch;
1339 const char *errstr;
1340 char *path = NULL;
1342 #ifndef PROFILE
1343 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1344 NULL) == -1)
1345 err(1, "pledge");
1346 #endif
1348 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1349 switch (ch) {
1350 case 'C':
1351 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1352 if (errstr != NULL)
1353 err(1, "-C option %s", errstr);
1354 break;
1355 case 'r':
1356 repo_path = realpath(optarg, NULL);
1357 if (repo_path == NULL)
1358 err(1, "-r option");
1359 got_path_strip_trailing_slashes(repo_path);
1360 break;
1361 default:
1362 usage_diff();
1363 /* NOTREACHED */
1367 argc -= optind;
1368 argv += optind;
1370 cwd = getcwd(NULL, 0);
1371 if (cwd == NULL) {
1372 error = got_error_from_errno("getcwd");
1373 goto done;
1375 error = got_worktree_open(&worktree, cwd);
1376 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1377 goto done;
1378 if (argc <= 1) {
1379 if (worktree == NULL) {
1380 error = got_error(GOT_ERR_NOT_WORKTREE);
1381 goto done;
1383 if (repo_path)
1384 errx(1,
1385 "-r option can't be used when diffing a work tree");
1386 repo_path = strdup(got_worktree_get_repo_path(worktree));
1387 if (repo_path == NULL) {
1388 error = got_error_from_errno("strdup");
1389 goto done;
1391 if (argc == 1) {
1392 error = got_worktree_resolve_path(&path, worktree,
1393 argv[0]);
1394 if (error)
1395 goto done;
1396 } else {
1397 path = strdup("");
1398 if (path == NULL) {
1399 error = got_error_from_errno("strdup");
1400 goto done;
1403 } else if (argc == 2) {
1404 id_str1 = argv[0];
1405 id_str2 = argv[1];
1406 if (worktree && repo_path == NULL) {
1407 repo_path =
1408 strdup(got_worktree_get_repo_path(worktree));
1409 if (repo_path == NULL) {
1410 error = got_error_from_errno("strdup");
1411 goto done;
1414 } else
1415 usage_diff();
1417 if (repo_path == NULL) {
1418 repo_path = getcwd(NULL, 0);
1419 if (repo_path == NULL)
1420 return got_error_from_errno("getcwd");
1423 error = got_repo_open(&repo, repo_path);
1424 free(repo_path);
1425 if (error != NULL)
1426 goto done;
1428 error = apply_unveil(got_repo_get_path(repo), 1,
1429 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1430 if (error)
1431 goto done;
1433 if (argc <= 1) {
1434 struct print_diff_arg arg;
1435 char *id_str;
1436 error = got_object_id_str(&id_str,
1437 got_worktree_get_base_commit_id(worktree));
1438 if (error)
1439 goto done;
1440 arg.repo = repo;
1441 arg.worktree = worktree;
1442 arg.diff_context = diff_context;
1443 arg.id_str = id_str;
1444 arg.header_shown = 0;
1446 error = got_worktree_status(worktree, path, repo, print_diff,
1447 &arg, check_cancelled, NULL);
1448 free(id_str);
1449 goto done;
1452 error = got_object_resolve_id_str(&id1, repo, id_str1);
1453 if (error) {
1454 struct got_reference *ref;
1455 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1456 goto done;
1457 error = got_ref_open(&ref, repo, id_str1, 0);
1458 if (error != NULL)
1459 goto done;
1460 label1 = strdup(got_ref_get_name(ref));
1461 if (label1 == NULL) {
1462 error = got_error_from_errno("strdup");
1463 goto done;
1465 error = got_ref_resolve(&id1, repo, ref);
1466 got_ref_close(ref);
1467 if (error != NULL)
1468 goto done;
1469 } else {
1470 label1 = strdup(id_str1);
1471 if (label1 == NULL) {
1472 error = got_error_from_errno("strdup");
1473 goto done;
1477 error = got_object_resolve_id_str(&id2, repo, id_str2);
1478 if (error) {
1479 struct got_reference *ref;
1480 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1481 goto done;
1482 error = got_ref_open(&ref, repo, id_str2, 0);
1483 if (error != NULL)
1484 goto done;
1485 label2 = strdup(got_ref_get_name(ref));
1486 if (label2 == NULL) {
1487 error = got_error_from_errno("strdup");
1488 goto done;
1490 error = got_ref_resolve(&id2, repo, ref);
1491 got_ref_close(ref);
1492 if (error != NULL)
1493 goto done;
1494 } else {
1495 label2 = strdup(id_str2);
1496 if (label2 == NULL) {
1497 error = got_error_from_errno("strdup");
1498 goto done;
1502 error = got_object_get_type(&type1, repo, id1);
1503 if (error)
1504 goto done;
1506 error = got_object_get_type(&type2, repo, id2);
1507 if (error)
1508 goto done;
1510 if (type1 != type2) {
1511 error = got_error(GOT_ERR_OBJ_TYPE);
1512 goto done;
1515 switch (type1) {
1516 case GOT_OBJ_TYPE_BLOB:
1517 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1518 diff_context, repo, stdout);
1519 break;
1520 case GOT_OBJ_TYPE_TREE:
1521 error = got_diff_objects_as_trees(id1, id2, "", "",
1522 diff_context, repo, stdout);
1523 break;
1524 case GOT_OBJ_TYPE_COMMIT:
1525 printf("diff %s %s\n", label1, label2);
1526 error = got_diff_objects_as_commits(id1, id2, diff_context,
1527 repo, stdout);
1528 break;
1529 default:
1530 error = got_error(GOT_ERR_OBJ_TYPE);
1533 done:
1534 free(label1);
1535 free(label2);
1536 free(id1);
1537 free(id2);
1538 free(path);
1539 if (worktree)
1540 got_worktree_close(worktree);
1541 if (repo) {
1542 const struct got_error *repo_error;
1543 repo_error = got_repo_close(repo);
1544 if (error == NULL)
1545 error = repo_error;
1547 return error;
1550 __dead static void
1551 usage_blame(void)
1553 fprintf(stderr,
1554 "usage: %s blame [-c commit] [-r repository-path] path\n",
1555 getprogname());
1556 exit(1);
1559 static const struct got_error *
1560 cmd_blame(int argc, char *argv[])
1562 const struct got_error *error;
1563 struct got_repository *repo = NULL;
1564 struct got_worktree *worktree = NULL;
1565 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1566 struct got_object_id *commit_id = NULL;
1567 char *commit_id_str = NULL;
1568 int ch;
1570 #ifndef PROFILE
1571 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1572 NULL) == -1)
1573 err(1, "pledge");
1574 #endif
1576 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1577 switch (ch) {
1578 case 'c':
1579 commit_id_str = optarg;
1580 break;
1581 case 'r':
1582 repo_path = realpath(optarg, NULL);
1583 if (repo_path == NULL)
1584 err(1, "-r option");
1585 got_path_strip_trailing_slashes(repo_path);
1586 break;
1587 default:
1588 usage_blame();
1589 /* NOTREACHED */
1593 argc -= optind;
1594 argv += optind;
1596 if (argc == 1)
1597 path = argv[0];
1598 else
1599 usage_blame();
1601 cwd = getcwd(NULL, 0);
1602 if (cwd == NULL) {
1603 error = got_error_from_errno("getcwd");
1604 goto done;
1606 if (repo_path == NULL) {
1607 error = got_worktree_open(&worktree, cwd);
1608 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1609 goto done;
1610 else
1611 error = NULL;
1612 if (worktree) {
1613 repo_path =
1614 strdup(got_worktree_get_repo_path(worktree));
1615 if (repo_path == NULL)
1616 error = got_error_from_errno("strdup");
1617 if (error)
1618 goto done;
1619 } else {
1620 repo_path = strdup(cwd);
1621 if (repo_path == NULL) {
1622 error = got_error_from_errno("strdup");
1623 goto done;
1628 error = got_repo_open(&repo, repo_path);
1629 if (error != NULL)
1630 goto done;
1632 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1633 if (error)
1634 goto done;
1636 if (worktree) {
1637 const char *prefix = got_worktree_get_path_prefix(worktree);
1638 char *p, *worktree_subdir = cwd +
1639 strlen(got_worktree_get_root_path(worktree));
1640 if (asprintf(&p, "%s%s%s%s%s",
1641 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1642 worktree_subdir, worktree_subdir[0] ? "/" : "",
1643 path) == -1) {
1644 error = got_error_from_errno("asprintf");
1645 goto done;
1647 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1648 free(p);
1649 } else {
1650 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1652 if (error)
1653 goto done;
1655 if (commit_id_str == NULL) {
1656 struct got_reference *head_ref;
1657 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1658 if (error != NULL)
1659 goto done;
1660 error = got_ref_resolve(&commit_id, repo, head_ref);
1661 got_ref_close(head_ref);
1662 if (error != NULL)
1663 goto done;
1664 } else {
1665 error = got_object_resolve_id_str(&commit_id, repo,
1666 commit_id_str);
1667 if (error != NULL)
1668 goto done;
1671 error = got_blame(in_repo_path, commit_id, repo, stdout);
1672 done:
1673 free(in_repo_path);
1674 free(repo_path);
1675 free(cwd);
1676 free(commit_id);
1677 if (worktree)
1678 got_worktree_close(worktree);
1679 if (repo) {
1680 const struct got_error *repo_error;
1681 repo_error = got_repo_close(repo);
1682 if (error == NULL)
1683 error = repo_error;
1685 return error;
1688 __dead static void
1689 usage_tree(void)
1691 fprintf(stderr,
1692 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1693 getprogname());
1694 exit(1);
1697 static void
1698 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1699 const char *root_path)
1701 int is_root_path = (strcmp(path, root_path) == 0);
1703 path += strlen(root_path);
1704 while (path[0] == '/')
1705 path++;
1707 printf("%s%s%s%s%s\n", id ? id : "", path,
1708 is_root_path ? "" : "/", te->name,
1709 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1712 static const struct got_error *
1713 print_tree(const char *path, struct got_object_id *commit_id,
1714 int show_ids, int recurse, const char *root_path,
1715 struct got_repository *repo)
1717 const struct got_error *err = NULL;
1718 struct got_object_id *tree_id = NULL;
1719 struct got_tree_object *tree = NULL;
1720 const struct got_tree_entries *entries;
1721 struct got_tree_entry *te;
1723 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1724 if (err)
1725 goto done;
1727 err = got_object_open_as_tree(&tree, repo, tree_id);
1728 if (err)
1729 goto done;
1730 entries = got_object_tree_get_entries(tree);
1731 te = SIMPLEQ_FIRST(&entries->head);
1732 while (te) {
1733 char *id = NULL;
1735 if (sigint_received || sigpipe_received)
1736 break;
1738 if (show_ids) {
1739 char *id_str;
1740 err = got_object_id_str(&id_str, te->id);
1741 if (err)
1742 goto done;
1743 if (asprintf(&id, "%s ", id_str) == -1) {
1744 err = got_error_from_errno("asprintf");
1745 free(id_str);
1746 goto done;
1748 free(id_str);
1750 print_entry(te, id, path, root_path);
1751 free(id);
1753 if (recurse && S_ISDIR(te->mode)) {
1754 char *child_path;
1755 if (asprintf(&child_path, "%s%s%s", path,
1756 path[0] == '/' && path[1] == '\0' ? "" : "/",
1757 te->name) == -1) {
1758 err = got_error_from_errno("asprintf");
1759 goto done;
1761 err = print_tree(child_path, commit_id, show_ids, 1,
1762 root_path, repo);
1763 free(child_path);
1764 if (err)
1765 goto done;
1768 te = SIMPLEQ_NEXT(te, entry);
1770 done:
1771 if (tree)
1772 got_object_tree_close(tree);
1773 free(tree_id);
1774 return err;
1777 static const struct got_error *
1778 cmd_tree(int argc, char *argv[])
1780 const struct got_error *error;
1781 struct got_repository *repo = NULL;
1782 struct got_worktree *worktree = NULL;
1783 const char *path;
1784 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1785 struct got_object_id *commit_id = NULL;
1786 char *commit_id_str = NULL;
1787 int show_ids = 0, recurse = 0;
1788 int ch;
1790 #ifndef PROFILE
1791 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1792 NULL) == -1)
1793 err(1, "pledge");
1794 #endif
1796 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1797 switch (ch) {
1798 case 'c':
1799 commit_id_str = optarg;
1800 break;
1801 case 'r':
1802 repo_path = realpath(optarg, NULL);
1803 if (repo_path == NULL)
1804 err(1, "-r option");
1805 got_path_strip_trailing_slashes(repo_path);
1806 break;
1807 case 'i':
1808 show_ids = 1;
1809 break;
1810 case 'R':
1811 recurse = 1;
1812 break;
1813 default:
1814 usage_tree();
1815 /* NOTREACHED */
1819 argc -= optind;
1820 argv += optind;
1822 if (argc == 1)
1823 path = argv[0];
1824 else if (argc > 1)
1825 usage_tree();
1826 else
1827 path = NULL;
1829 cwd = getcwd(NULL, 0);
1830 if (cwd == NULL) {
1831 error = got_error_from_errno("getcwd");
1832 goto done;
1834 if (repo_path == NULL) {
1835 error = got_worktree_open(&worktree, cwd);
1836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1837 goto done;
1838 else
1839 error = NULL;
1840 if (worktree) {
1841 repo_path =
1842 strdup(got_worktree_get_repo_path(worktree));
1843 if (repo_path == NULL)
1844 error = got_error_from_errno("strdup");
1845 if (error)
1846 goto done;
1847 } else {
1848 repo_path = strdup(cwd);
1849 if (repo_path == NULL) {
1850 error = got_error_from_errno("strdup");
1851 goto done;
1856 error = got_repo_open(&repo, repo_path);
1857 if (error != NULL)
1858 goto done;
1860 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1861 if (error)
1862 goto done;
1864 if (path == NULL) {
1865 if (worktree) {
1866 char *p, *worktree_subdir = cwd +
1867 strlen(got_worktree_get_root_path(worktree));
1868 if (asprintf(&p, "%s/%s",
1869 got_worktree_get_path_prefix(worktree),
1870 worktree_subdir) == -1) {
1871 error = got_error_from_errno("asprintf");
1872 goto done;
1874 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1875 free(p);
1876 if (error)
1877 goto done;
1878 } else
1879 path = "/";
1881 if (in_repo_path == NULL) {
1882 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1883 if (error != NULL)
1884 goto done;
1887 if (commit_id_str == NULL) {
1888 struct got_reference *head_ref;
1889 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1890 if (error != NULL)
1891 goto done;
1892 error = got_ref_resolve(&commit_id, repo, head_ref);
1893 got_ref_close(head_ref);
1894 if (error != NULL)
1895 goto done;
1896 } else {
1897 error = got_object_resolve_id_str(&commit_id, repo,
1898 commit_id_str);
1899 if (error != NULL)
1900 goto done;
1903 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1904 in_repo_path, repo);
1905 done:
1906 free(in_repo_path);
1907 free(repo_path);
1908 free(cwd);
1909 free(commit_id);
1910 if (worktree)
1911 got_worktree_close(worktree);
1912 if (repo) {
1913 const struct got_error *repo_error;
1914 repo_error = got_repo_close(repo);
1915 if (error == NULL)
1916 error = repo_error;
1918 return error;
1921 __dead static void
1922 usage_status(void)
1924 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1925 exit(1);
1928 static const struct got_error *
1929 print_status(void *arg, unsigned char status, const char *path,
1930 struct got_object_id *blob_id, struct got_object_id *commit_id)
1932 printf("%c %s\n", status, path);
1933 return NULL;
1936 static const struct got_error *
1937 cmd_status(int argc, char *argv[])
1939 const struct got_error *error = NULL;
1940 struct got_repository *repo = NULL;
1941 struct got_worktree *worktree = NULL;
1942 char *cwd = NULL, *path = NULL;
1943 int ch;
1945 while ((ch = getopt(argc, argv, "")) != -1) {
1946 switch (ch) {
1947 default:
1948 usage_status();
1949 /* NOTREACHED */
1953 argc -= optind;
1954 argv += optind;
1956 #ifndef PROFILE
1957 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1958 NULL) == -1)
1959 err(1, "pledge");
1960 #endif
1961 cwd = getcwd(NULL, 0);
1962 if (cwd == NULL) {
1963 error = got_error_from_errno("getcwd");
1964 goto done;
1967 error = got_worktree_open(&worktree, cwd);
1968 if (error != NULL)
1969 goto done;
1971 if (argc == 0) {
1972 path = strdup("");
1973 if (path == NULL) {
1974 error = got_error_from_errno("strdup");
1975 goto done;
1977 } else if (argc == 1) {
1978 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1979 if (error)
1980 goto done;
1981 } else
1982 usage_status();
1984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1985 if (error != NULL)
1986 goto done;
1988 error = apply_unveil(got_repo_get_path(repo), 1,
1989 got_worktree_get_root_path(worktree), 0);
1990 if (error)
1991 goto done;
1993 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1994 check_cancelled, NULL);
1995 done:
1996 free(cwd);
1997 free(path);
1998 return error;
2001 __dead static void
2002 usage_ref(void)
2004 fprintf(stderr,
2005 "usage: %s ref [-r repository] -l | -d name | name target\n",
2006 getprogname());
2007 exit(1);
2010 static const struct got_error *
2011 list_refs(struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct got_reflist_head refs;
2015 struct got_reflist_entry *re;
2017 SIMPLEQ_INIT(&refs);
2018 err = got_ref_list(&refs, repo);
2019 if (err)
2020 return err;
2022 SIMPLEQ_FOREACH(re, &refs, entry) {
2023 char *refstr;
2024 refstr = got_ref_to_str(re->ref);
2025 if (refstr == NULL)
2026 return got_error_from_errno("got_ref_to_str");
2027 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2028 free(refstr);
2031 got_ref_list_free(&refs);
2032 return NULL;
2035 static const struct got_error *
2036 delete_ref(struct got_repository *repo, const char *refname)
2038 const struct got_error *err = NULL;
2039 struct got_reference *ref;
2041 err = got_ref_open(&ref, repo, refname, 0);
2042 if (err)
2043 return err;
2045 err = got_ref_delete(ref, repo);
2046 got_ref_close(ref);
2047 return err;
2050 static const struct got_error *
2051 add_ref(struct got_repository *repo, const char *refname, const char *target)
2053 const struct got_error *err = NULL;
2054 struct got_object_id *id;
2055 struct got_reference *ref = NULL;
2057 err = got_object_resolve_id_str(&id, repo, target);
2058 if (err) {
2059 struct got_reference *target_ref;
2061 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2062 return err;
2063 err = got_ref_open(&target_ref, repo, target, 0);
2064 if (err)
2065 return err;
2066 err = got_ref_resolve(&id, repo, target_ref);
2067 got_ref_close(target_ref);
2068 if (err)
2069 return err;
2072 err = got_ref_alloc(&ref, refname, id);
2073 if (err)
2074 goto done;
2076 err = got_ref_write(ref, repo);
2077 done:
2078 if (ref)
2079 got_ref_close(ref);
2080 free(id);
2081 return err;
2084 static const struct got_error *
2085 cmd_ref(int argc, char *argv[])
2087 const struct got_error *error = NULL;
2088 struct got_repository *repo = NULL;
2089 struct got_worktree *worktree = NULL;
2090 char *cwd = NULL, *repo_path = NULL;
2091 int ch, do_list = 0;
2092 const char *delref = NULL;
2094 /* TODO: Add -s option for adding symbolic references. */
2095 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2096 switch (ch) {
2097 case 'd':
2098 delref = optarg;
2099 break;
2100 case 'r':
2101 repo_path = realpath(optarg, NULL);
2102 if (repo_path == NULL)
2103 err(1, "-r option");
2104 got_path_strip_trailing_slashes(repo_path);
2105 break;
2106 case 'l':
2107 do_list = 1;
2108 break;
2109 default:
2110 usage_ref();
2111 /* NOTREACHED */
2115 if (do_list && delref)
2116 errx(1, "-l and -d options are mutually exclusive\n");
2118 argc -= optind;
2119 argv += optind;
2121 if (do_list || delref) {
2122 if (argc > 0)
2123 usage_ref();
2124 } else if (argc != 2)
2125 usage_ref();
2127 #ifndef PROFILE
2128 if (do_list) {
2129 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2130 NULL) == -1)
2131 err(1, "pledge");
2132 } else {
2133 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2134 "sendfd unveil", NULL) == -1)
2135 err(1, "pledge");
2137 #endif
2138 cwd = getcwd(NULL, 0);
2139 if (cwd == NULL) {
2140 error = got_error_from_errno("getcwd");
2141 goto done;
2144 if (repo_path == NULL) {
2145 error = got_worktree_open(&worktree, cwd);
2146 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2147 goto done;
2148 else
2149 error = NULL;
2150 if (worktree) {
2151 repo_path =
2152 strdup(got_worktree_get_repo_path(worktree));
2153 if (repo_path == NULL)
2154 error = got_error_from_errno("strdup");
2155 if (error)
2156 goto done;
2157 } else {
2158 repo_path = strdup(cwd);
2159 if (repo_path == NULL) {
2160 error = got_error_from_errno("strdup");
2161 goto done;
2166 error = got_repo_open(&repo, repo_path);
2167 if (error != NULL)
2168 goto done;
2170 error = apply_unveil(got_repo_get_path(repo), do_list,
2171 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2172 if (error)
2173 goto done;
2175 if (do_list)
2176 error = list_refs(repo);
2177 else if (delref)
2178 error = delete_ref(repo, delref);
2179 else
2180 error = add_ref(repo, argv[0], argv[1]);
2181 done:
2182 if (repo)
2183 got_repo_close(repo);
2184 if (worktree)
2185 got_worktree_close(worktree);
2186 free(cwd);
2187 free(repo_path);
2188 return error;
2191 __dead static void
2192 usage_add(void)
2194 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2195 exit(1);
2198 static const struct got_error *
2199 cmd_add(int argc, char *argv[])
2201 const struct got_error *error = NULL;
2202 struct got_repository *repo = NULL;
2203 struct got_worktree *worktree = NULL;
2204 char *cwd = NULL;
2205 struct got_pathlist_head paths;
2206 struct got_pathlist_entry *pe;
2207 int ch, x;
2209 TAILQ_INIT(&paths);
2211 while ((ch = getopt(argc, argv, "")) != -1) {
2212 switch (ch) {
2213 default:
2214 usage_add();
2215 /* NOTREACHED */
2219 argc -= optind;
2220 argv += optind;
2222 if (argc < 1)
2223 usage_add();
2225 /* make sure each file exists before doing anything halfway */
2226 for (x = 0; x < argc; x++) {
2227 char *path = realpath(argv[x], NULL);
2228 if (path == NULL) {
2229 error = got_error_from_errno2("realpath", argv[x]);
2230 goto done;
2232 free(path);
2235 cwd = getcwd(NULL, 0);
2236 if (cwd == NULL) {
2237 error = got_error_from_errno("getcwd");
2238 goto done;
2241 error = got_worktree_open(&worktree, cwd);
2242 if (error)
2243 goto done;
2245 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2246 if (error != NULL)
2247 goto done;
2249 error = apply_unveil(got_repo_get_path(repo), 1,
2250 got_worktree_get_root_path(worktree), 0);
2251 if (error)
2252 goto done;
2254 for (x = 0; x < argc; x++) {
2255 char *path = realpath(argv[x], NULL);
2256 if (path == NULL) {
2257 error = got_error_from_errno2("realpath", argv[x]);
2258 goto done;
2261 got_path_strip_trailing_slashes(path);
2262 error = got_pathlist_insert(&pe, &paths, path, NULL);
2263 if (error) {
2264 free(path);
2265 goto done;
2268 error = got_worktree_schedule_add(worktree, &paths, print_status,
2269 NULL, repo);
2270 done:
2271 if (repo)
2272 got_repo_close(repo);
2273 if (worktree)
2274 got_worktree_close(worktree);
2275 TAILQ_FOREACH(pe, &paths, entry)
2276 free((char *)pe->path);
2277 got_pathlist_free(&paths);
2278 free(cwd);
2279 return error;
2282 __dead static void
2283 usage_rm(void)
2285 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2286 exit(1);
2289 static const struct got_error *
2290 cmd_rm(int argc, char *argv[])
2292 const struct got_error *error = NULL;
2293 struct got_worktree *worktree = NULL;
2294 struct got_repository *repo = NULL;
2295 char *cwd = NULL;
2296 struct got_pathlist_head paths;
2297 struct got_pathlist_entry *pe;
2298 int ch, i, delete_local_mods = 0;
2300 TAILQ_INIT(&paths);
2302 while ((ch = getopt(argc, argv, "f")) != -1) {
2303 switch (ch) {
2304 case 'f':
2305 delete_local_mods = 1;
2306 break;
2307 default:
2308 usage_add();
2309 /* NOTREACHED */
2313 argc -= optind;
2314 argv += optind;
2316 if (argc < 1)
2317 usage_rm();
2319 /* make sure each file exists before doing anything halfway */
2320 for (i = 0; i < argc; i++) {
2321 char *path = realpath(argv[i], NULL);
2322 if (path == NULL) {
2323 error = got_error_from_errno2("realpath", argv[i]);
2324 goto done;
2326 free(path);
2329 cwd = getcwd(NULL, 0);
2330 if (cwd == NULL) {
2331 error = got_error_from_errno("getcwd");
2332 goto done;
2334 error = got_worktree_open(&worktree, cwd);
2335 if (error)
2336 goto done;
2338 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2339 if (error)
2340 goto done;
2342 error = apply_unveil(got_repo_get_path(repo), 1,
2343 got_worktree_get_root_path(worktree), 0);
2344 if (error)
2345 goto done;
2347 for (i = 0; i < argc; i++) {
2348 char *path = realpath(argv[i], NULL);
2349 if (path == NULL) {
2350 error = got_error_from_errno2("realpath", argv[i]);
2351 goto done;
2354 got_path_strip_trailing_slashes(path);
2355 error = got_pathlist_insert(&pe, &paths, path, NULL);
2356 if (error) {
2357 free(path);
2358 goto done;
2361 error = got_worktree_schedule_delete(worktree, &paths,
2362 delete_local_mods, print_status, NULL, repo);
2363 if (error)
2364 goto done;
2365 done:
2366 if (repo)
2367 got_repo_close(repo);
2368 if (worktree)
2369 got_worktree_close(worktree);
2370 TAILQ_FOREACH(pe, &paths, entry)
2371 free((char *)pe->path);
2372 got_pathlist_free(&paths);
2373 free(cwd);
2374 return error;
2377 __dead static void
2378 usage_revert(void)
2380 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2381 exit(1);
2384 static void
2385 revert_progress(void *arg, unsigned char status, const char *path)
2387 while (path[0] == '/')
2388 path++;
2389 printf("%c %s\n", status, path);
2392 static const struct got_error *
2393 cmd_revert(int argc, char *argv[])
2395 const struct got_error *error = NULL;
2396 struct got_worktree *worktree = NULL;
2397 struct got_repository *repo = NULL;
2398 char *cwd = NULL, *path = NULL;
2399 struct got_pathlist_head paths;
2400 struct got_pathlist_entry *pe;
2401 int ch, i;
2403 TAILQ_INIT(&paths);
2405 while ((ch = getopt(argc, argv, "")) != -1) {
2406 switch (ch) {
2407 default:
2408 usage_revert();
2409 /* NOTREACHED */
2413 argc -= optind;
2414 argv += optind;
2416 if (argc < 1)
2417 usage_revert();
2419 for (i = 0; i < argc; i++) {
2420 char *path = realpath(argv[i], NULL);
2421 if (path == NULL) {
2422 error = got_error_from_errno2("realpath", argv[i]);
2423 goto done;
2426 got_path_strip_trailing_slashes(path);
2427 error = got_pathlist_insert(&pe, &paths, path, NULL);
2428 if (error) {
2429 free(path);
2430 goto done;
2434 cwd = getcwd(NULL, 0);
2435 if (cwd == NULL) {
2436 error = got_error_from_errno("getcwd");
2437 goto done;
2439 error = got_worktree_open(&worktree, cwd);
2440 if (error)
2441 goto done;
2443 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2444 if (error != NULL)
2445 goto done;
2447 error = apply_unveil(got_repo_get_path(repo), 1,
2448 got_worktree_get_root_path(worktree), 0);
2449 if (error)
2450 goto done;
2452 error = got_worktree_revert(worktree, &paths,
2453 revert_progress, NULL, repo);
2454 if (error)
2455 goto done;
2456 done:
2457 if (repo)
2458 got_repo_close(repo);
2459 if (worktree)
2460 got_worktree_close(worktree);
2461 free(path);
2462 free(cwd);
2463 return error;
2466 __dead static void
2467 usage_commit(void)
2469 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2470 exit(1);
2473 int
2474 spawn_editor(const char *editor, const char *file)
2476 pid_t pid;
2477 sig_t sighup, sigint, sigquit;
2478 int st = -1;
2480 sighup = signal(SIGHUP, SIG_IGN);
2481 sigint = signal(SIGINT, SIG_IGN);
2482 sigquit = signal(SIGQUIT, SIG_IGN);
2484 switch (pid = fork()) {
2485 case -1:
2486 goto doneediting;
2487 case 0:
2488 execl(editor, editor, file, (char *)NULL);
2489 _exit(127);
2492 while (waitpid(pid, &st, 0) == -1)
2493 if (errno != EINTR)
2494 break;
2496 doneediting:
2497 (void)signal(SIGHUP, sighup);
2498 (void)signal(SIGINT, sigint);
2499 (void)signal(SIGQUIT, sigquit);
2501 if (!WIFEXITED(st)) {
2502 errno = EINTR;
2503 return -1;
2506 return WEXITSTATUS(st);
2509 struct collect_commit_logmsg_arg {
2510 const char *cmdline_log;
2511 const char *editor;
2512 const char *worktree_path;
2513 const char *branch_name;
2514 const char *repo_path;
2515 char *logmsg_path;
2519 static const struct got_error *
2520 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2521 void *arg)
2523 char *initial_content = NULL;
2524 struct got_pathlist_entry *pe;
2525 const struct got_error *err = NULL;
2526 char *template = NULL;
2527 struct collect_commit_logmsg_arg *a = arg;
2528 char buf[1024];
2529 struct stat st, st2;
2530 FILE *fp;
2531 size_t len;
2532 int fd, content_changed = 0;
2534 /* if a message was specified on the command line, just use it */
2535 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2536 len = strlen(a->cmdline_log) + 1;
2537 *logmsg = malloc(len + 1);
2538 if (*logmsg == NULL)
2539 return got_error_from_errno("malloc");
2540 strlcpy(*logmsg, a->cmdline_log, len);
2541 return NULL;
2544 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2545 return got_error_from_errno("asprintf");
2547 if (asprintf(&initial_content,
2548 "\n# changes to be committed on branch %s:\n",
2549 a->branch_name) == -1)
2550 return got_error_from_errno("asprintf");
2552 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2553 if (err)
2554 goto done;
2556 dprintf(fd, initial_content);
2558 TAILQ_FOREACH(pe, commitable_paths, entry) {
2559 struct got_commitable *ct = pe->data;
2560 dprintf(fd, "# %c %s\n",
2561 got_commitable_get_status(ct),
2562 got_commitable_get_path(ct));
2564 close(fd);
2566 if (stat(a->logmsg_path, &st) == -1) {
2567 err = got_error_from_errno2("stat", a->logmsg_path);
2568 goto done;
2571 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2572 err = got_error_from_errno("failed spawning editor");
2573 goto done;
2576 if (stat(a->logmsg_path, &st2) == -1) {
2577 err = got_error_from_errno("stat");
2578 goto done;
2581 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2582 unlink(a->logmsg_path);
2583 free(a->logmsg_path);
2584 a->logmsg_path = NULL;
2585 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2586 "no changes made to commit message, aborting");
2587 goto done;
2590 *logmsg = malloc(st2.st_size + 1);
2591 if (*logmsg == NULL) {
2592 err = got_error_from_errno("malloc");
2593 goto done;
2595 (*logmsg)[0] = '\0';
2596 len = 0;
2598 fp = fopen(a->logmsg_path, "r");
2599 if (fp == NULL) {
2600 err = got_error_from_errno("fopen");
2601 goto done;
2603 while (fgets(buf, sizeof(buf), fp) != NULL) {
2604 if (!content_changed && strcmp(buf, initial_content) != 0)
2605 content_changed = 1;
2606 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2607 continue; /* remove comments and leading empty lines */
2608 len = strlcat(*logmsg, buf, st2.st_size);
2610 fclose(fp);
2612 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2613 (*logmsg)[len - 1] = '\0';
2614 len--;
2617 if (len == 0 || !content_changed) {
2618 unlink(a->logmsg_path);
2619 free(a->logmsg_path);
2620 a->logmsg_path = NULL;
2621 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2622 "commit message cannot be empty, aborting");
2623 goto done;
2625 done:
2626 free(initial_content);
2627 free(template);
2629 /* Editor is done; we can now apply unveil(2) */
2630 if (err == NULL)
2631 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2632 return err;
2635 static const struct got_error *
2636 cmd_commit(int argc, char *argv[])
2638 const struct got_error *error = NULL;
2639 struct got_worktree *worktree = NULL;
2640 struct got_repository *repo = NULL;
2641 char *cwd = NULL, *path = NULL, *id_str = NULL;
2642 struct got_object_id *id = NULL;
2643 const char *logmsg = NULL;
2644 const char *got_author = getenv("GOT_AUTHOR");
2645 struct collect_commit_logmsg_arg cl_arg;
2646 char *editor = NULL;
2647 int ch;
2649 while ((ch = getopt(argc, argv, "m:")) != -1) {
2650 switch (ch) {
2651 case 'm':
2652 logmsg = optarg;
2653 break;
2654 default:
2655 usage_commit();
2656 /* NOTREACHED */
2660 argc -= optind;
2661 argv += optind;
2663 if (argc == 1) {
2664 path = realpath(argv[0], NULL);
2665 if (path == NULL) {
2666 error = got_error_from_errno2("realpath", argv[0]);
2667 goto done;
2669 got_path_strip_trailing_slashes(path);
2670 } else if (argc != 0)
2671 usage_commit();
2673 if (got_author == NULL) {
2674 /* TODO: Look current user up in password database */
2675 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2676 goto done;
2679 cwd = getcwd(NULL, 0);
2680 if (cwd == NULL) {
2681 error = got_error_from_errno("getcwd");
2682 goto done;
2684 error = got_worktree_open(&worktree, cwd);
2685 if (error)
2686 goto done;
2688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2689 if (error != NULL)
2690 goto done;
2693 * unveil(2) traverses exec(2); if an editor is used we have
2694 * to apply unveil after the log message has been written.
2696 if (logmsg == NULL || strlen(logmsg) == 0)
2697 error = get_editor(&editor);
2698 else
2699 error = apply_unveil(got_repo_get_path(repo), 0,
2700 got_worktree_get_root_path(worktree), 0);
2701 if (error)
2702 goto done;
2704 cl_arg.editor = editor;
2705 cl_arg.cmdline_log = logmsg;
2706 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2707 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2708 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2709 cl_arg.branch_name += 5;
2710 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2711 cl_arg.branch_name += 6;
2712 cl_arg.repo_path = got_repo_get_path(repo);
2713 cl_arg.logmsg_path = NULL;
2714 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2715 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2716 if (error) {
2717 if (cl_arg.logmsg_path)
2718 fprintf(stderr, "%s: log message preserved in %s\n",
2719 getprogname(), cl_arg.logmsg_path);
2720 goto done;
2723 if (cl_arg.logmsg_path)
2724 unlink(cl_arg.logmsg_path);
2726 error = got_object_id_str(&id_str, id);
2727 if (error)
2728 goto done;
2729 printf("Created commit %s\n", id_str);
2730 done:
2731 if (repo)
2732 got_repo_close(repo);
2733 if (worktree)
2734 got_worktree_close(worktree);
2735 free(path);
2736 free(cwd);
2737 free(id_str);
2738 free(editor);
2739 return error;
2742 __dead static void
2743 usage_cherrypick(void)
2745 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2746 exit(1);
2749 static const struct got_error *
2750 cmd_cherrypick(int argc, char *argv[])
2752 const struct got_error *error = NULL;
2753 struct got_worktree *worktree = NULL;
2754 struct got_repository *repo = NULL;
2755 char *cwd = NULL, *commit_id_str = NULL;
2756 struct got_object_id *commit_id = NULL;
2757 struct got_commit_object *commit = NULL;
2758 struct got_object_qid *pid;
2759 struct got_reference *head_ref = NULL;
2760 int ch, did_something = 0;
2762 while ((ch = getopt(argc, argv, "")) != -1) {
2763 switch (ch) {
2764 default:
2765 usage_cherrypick();
2766 /* NOTREACHED */
2770 argc -= optind;
2771 argv += optind;
2773 if (argc != 1)
2774 usage_cherrypick();
2776 cwd = getcwd(NULL, 0);
2777 if (cwd == NULL) {
2778 error = got_error_from_errno("getcwd");
2779 goto done;
2781 error = got_worktree_open(&worktree, cwd);
2782 if (error)
2783 goto done;
2785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2786 if (error != NULL)
2787 goto done;
2789 error = apply_unveil(got_repo_get_path(repo), 0,
2790 got_worktree_get_root_path(worktree), 0);
2791 if (error)
2792 goto done;
2794 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2795 if (error != NULL) {
2796 struct got_reference *ref;
2797 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2798 goto done;
2799 error = got_ref_open(&ref, repo, argv[0], 0);
2800 if (error != NULL)
2801 goto done;
2802 error = got_ref_resolve(&commit_id, repo, ref);
2803 got_ref_close(ref);
2804 if (error != NULL)
2805 goto done;
2807 error = got_object_id_str(&commit_id_str, commit_id);
2808 if (error)
2809 goto done;
2811 error = got_ref_open(&head_ref, repo,
2812 got_worktree_get_head_ref_name(worktree), 0);
2813 if (error != NULL)
2814 goto done;
2816 error = check_same_branch(commit_id, head_ref, repo);
2817 if (error) {
2818 if (error->code != GOT_ERR_ANCESTRY)
2819 goto done;
2820 error = NULL;
2821 } else {
2822 error = got_error(GOT_ERR_SAME_BRANCH);
2823 goto done;
2826 error = got_object_open_as_commit(&commit, repo, commit_id);
2827 if (error)
2828 goto done;
2829 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2830 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2831 commit_id, repo, update_progress, &did_something, check_cancelled,
2832 NULL);
2833 if (error != NULL)
2834 goto done;
2836 if (did_something)
2837 printf("Merged commit %s\n", commit_id_str);
2838 done:
2839 if (commit)
2840 got_object_commit_close(commit);
2841 free(commit_id_str);
2842 if (head_ref)
2843 got_ref_close(head_ref);
2844 if (worktree)
2845 got_worktree_close(worktree);
2846 if (repo)
2847 got_repo_close(repo);
2848 return error;
2851 __dead static void
2852 usage_backout(void)
2854 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2855 exit(1);
2858 static const struct got_error *
2859 cmd_backout(int argc, char *argv[])
2861 const struct got_error *error = NULL;
2862 struct got_worktree *worktree = NULL;
2863 struct got_repository *repo = NULL;
2864 char *cwd = NULL, *commit_id_str = NULL;
2865 struct got_object_id *commit_id = NULL;
2866 struct got_commit_object *commit = NULL;
2867 struct got_object_qid *pid;
2868 struct got_reference *head_ref = NULL;
2869 int ch, did_something = 0;
2871 while ((ch = getopt(argc, argv, "")) != -1) {
2872 switch (ch) {
2873 default:
2874 usage_backout();
2875 /* NOTREACHED */
2879 argc -= optind;
2880 argv += optind;
2882 if (argc != 1)
2883 usage_backout();
2885 cwd = getcwd(NULL, 0);
2886 if (cwd == NULL) {
2887 error = got_error_from_errno("getcwd");
2888 goto done;
2890 error = got_worktree_open(&worktree, cwd);
2891 if (error)
2892 goto done;
2894 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2895 if (error != NULL)
2896 goto done;
2898 error = apply_unveil(got_repo_get_path(repo), 0,
2899 got_worktree_get_root_path(worktree), 0);
2900 if (error)
2901 goto done;
2903 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2904 if (error != NULL) {
2905 struct got_reference *ref;
2906 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2907 goto done;
2908 error = got_ref_open(&ref, repo, argv[0], 0);
2909 if (error != NULL)
2910 goto done;
2911 error = got_ref_resolve(&commit_id, repo, ref);
2912 got_ref_close(ref);
2913 if (error != NULL)
2914 goto done;
2916 error = got_object_id_str(&commit_id_str, commit_id);
2917 if (error)
2918 goto done;
2920 error = got_ref_open(&head_ref, repo,
2921 got_worktree_get_head_ref_name(worktree), 0);
2922 if (error != NULL)
2923 goto done;
2925 error = check_same_branch(commit_id, head_ref, repo);
2926 if (error)
2927 goto done;
2929 error = got_object_open_as_commit(&commit, repo, commit_id);
2930 if (error)
2931 goto done;
2932 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2933 if (pid == NULL) {
2934 error = got_error(GOT_ERR_ROOT_COMMIT);
2935 goto done;
2938 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2939 update_progress, &did_something, check_cancelled, NULL);
2940 if (error != NULL)
2941 goto done;
2943 if (did_something)
2944 printf("Backed out commit %s\n", commit_id_str);
2945 done:
2946 if (commit)
2947 got_object_commit_close(commit);
2948 free(commit_id_str);
2949 if (head_ref)
2950 got_ref_close(head_ref);
2951 if (worktree)
2952 got_worktree_close(worktree);
2953 if (repo)
2954 got_repo_close(repo);
2955 return error;