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 <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_worktree.h"
44 #include "got_diff.h"
45 #include "got_commit_graph.h"
46 #include "got_blame.h"
47 #include "got_privsep.h"
48 #include "got_opentemp.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
57 static void
58 catch_sigint(int signo)
59 {
60 sigint_received = 1;
61 }
63 static void
64 catch_sigpipe(int signo)
65 {
66 sigpipe_received = 1;
67 }
70 struct got_cmd {
71 const char *cmd_name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 const char *cmd_alias;
75 };
77 __dead static void usage(int);
78 __dead static void usage_init(void);
79 __dead static void usage_checkout(void);
80 __dead static void usage_update(void);
81 __dead static void usage_log(void);
82 __dead static void usage_diff(void);
83 __dead static void usage_blame(void);
84 __dead static void usage_tree(void);
85 __dead static void usage_status(void);
86 __dead static void usage_ref(void);
87 __dead static void usage_branch(void);
88 __dead static void usage_add(void);
89 __dead static void usage_remove(void);
90 __dead static void usage_revert(void);
91 __dead static void usage_commit(void);
92 __dead static void usage_cherrypick(void);
93 __dead static void usage_backout(void);
94 __dead static void usage_rebase(void);
96 static const struct got_error* cmd_init(int, char *[]);
97 static const struct got_error* cmd_checkout(int, char *[]);
98 static const struct got_error* cmd_update(int, char *[]);
99 static const struct got_error* cmd_log(int, char *[]);
100 static const struct got_error* cmd_diff(int, char *[]);
101 static const struct got_error* cmd_blame(int, char *[]);
102 static const struct got_error* cmd_tree(int, char *[]);
103 static const struct got_error* cmd_status(int, char *[]);
104 static const struct got_error* cmd_ref(int, char *[]);
105 static const struct got_error* cmd_branch(int, char *[]);
106 static const struct got_error* cmd_add(int, char *[]);
107 static const struct got_error* cmd_remove(int, char *[]);
108 static const struct got_error* cmd_revert(int, char *[]);
109 static const struct got_error* cmd_commit(int, char *[]);
110 static const struct got_error* cmd_cherrypick(int, char *[]);
111 static const struct got_error* cmd_backout(int, char *[]);
112 static const struct got_error* cmd_rebase(int, char *[]);
114 static struct got_cmd got_commands[] = {
115 { "init", cmd_init, usage_init, "" },
116 { "checkout", cmd_checkout, usage_checkout, "co" },
117 { "update", cmd_update, usage_update, "up" },
118 { "log", cmd_log, usage_log, "" },
119 { "diff", cmd_diff, usage_diff, "" },
120 { "blame", cmd_blame, usage_blame, "" },
121 { "tree", cmd_tree, usage_tree, "" },
122 { "status", cmd_status, usage_status, "st" },
123 { "ref", cmd_ref, usage_ref, "" },
124 { "branch", cmd_branch, usage_branch, "br" },
125 { "add", cmd_add, usage_add, "" },
126 { "remove", cmd_remove, usage_remove, "rm" },
127 { "revert", cmd_revert, usage_revert, "rv" },
128 { "commit", cmd_commit, usage_commit, "ci" },
129 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
130 { "backout", cmd_backout, usage_backout, "bo" },
131 { "rebase", cmd_rebase, usage_rebase, "rb" },
132 };
134 static void
135 list_commands(void)
137 int i;
139 fprintf(stderr, "commands:");
140 for (i = 0; i < nitems(got_commands); i++) {
141 struct got_cmd *cmd = &got_commands[i];
142 fprintf(stderr, " %s", cmd->cmd_name);
144 fputc('\n', stderr);
147 int
148 main(int argc, char *argv[])
150 struct got_cmd *cmd;
151 unsigned int i;
152 int ch;
153 int hflag = 0;
155 setlocale(LC_CTYPE, "");
157 while ((ch = getopt(argc, argv, "h")) != -1) {
158 switch (ch) {
159 case 'h':
160 hflag = 1;
161 break;
162 default:
163 usage(hflag);
164 /* NOTREACHED */
168 argc -= optind;
169 argv += optind;
170 optind = 0;
172 if (argc <= 0)
173 usage(hflag);
175 signal(SIGINT, catch_sigint);
176 signal(SIGPIPE, catch_sigpipe);
178 for (i = 0; i < nitems(got_commands); i++) {
179 const struct got_error *error;
181 cmd = &got_commands[i];
183 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
184 strcmp(cmd->cmd_alias, argv[0]) != 0)
185 continue;
187 if (hflag)
188 got_commands[i].cmd_usage();
190 error = got_commands[i].cmd_main(argc, argv);
191 if (error && !(sigint_received || sigpipe_received)) {
192 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
193 return 1;
196 return 0;
199 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
200 list_commands();
201 return 1;
204 __dead static void
205 usage(int hflag)
207 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
208 if (hflag)
209 list_commands();
210 exit(1);
213 static const struct got_error *
214 get_editor(char **abspath)
216 const struct got_error *err = NULL;
217 const char *editor;
219 editor = getenv("VISUAL");
220 if (editor == NULL)
221 editor = getenv("EDITOR");
223 if (editor) {
224 err = got_path_find_prog(abspath, editor);
225 if (err)
226 return err;
229 if (*abspath == NULL) {
230 *abspath = strdup("/bin/ed");
231 if (*abspath == NULL)
232 return got_error_from_errno("strdup");
235 return NULL;
238 static const struct got_error *
239 apply_unveil(const char *repo_path, int repo_read_only,
240 const char *worktree_path, int create_worktree)
242 const struct got_error *err;
244 if (create_worktree) {
245 /* Pre-create work tree path to avoid unveiling its parents. */
246 err = got_path_mkdir(worktree_path);
248 if (errno == EEXIST) {
249 if (got_path_dir_is_empty(worktree_path)) {
250 errno = 0;
251 err = NULL;
252 } else {
253 err = got_error_path(worktree_path,
254 GOT_ERR_DIR_NOT_EMPTY);
258 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
259 return err;
262 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
263 return got_error_from_errno2("unveil", repo_path);
265 if (worktree_path && unveil(worktree_path, "rwc") != 0)
266 return got_error_from_errno2("unveil", worktree_path);
268 if (unveil("/tmp", "rwc") != 0)
269 return got_error_from_errno2("unveil", "/tmp");
271 err = got_privsep_unveil_exec_helpers();
272 if (err != NULL)
273 return err;
275 if (unveil(NULL, NULL) != 0)
276 return got_error_from_errno("unveil");
278 return NULL;
281 __dead static void
282 usage_init(void)
284 fprintf(stderr, "usage: %s init path\n", getprogname());
285 exit(1);
288 static const struct got_error *
289 cmd_init(int argc, char *argv[])
291 const struct got_error *error = NULL;
292 char *repo_path = NULL;
293 int ch;
295 while ((ch = getopt(argc, argv, "")) != -1) {
296 switch (ch) {
297 default:
298 usage_init();
299 /* NOTREACHED */
303 argc -= optind;
304 argv += optind;
306 #ifndef PROFILE
307 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
308 err(1, "pledge");
309 #endif
310 if (argc != 1)
311 usage_init();
313 repo_path = strdup(argv[0]);
314 if (repo_path == NULL)
315 return got_error_from_errno("strdup");
317 got_path_strip_trailing_slashes(repo_path);
319 error = got_path_mkdir(repo_path);
320 if (error &&
321 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
322 goto done;
324 error = apply_unveil(repo_path, 0, NULL, 0);
325 if (error)
326 goto done;
328 error = got_repo_init(repo_path);
329 if (error != NULL)
330 goto done;
332 done:
333 free(repo_path);
334 return error;
337 __dead static void
338 usage_checkout(void)
340 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
341 "[-p prefix] repository-path [worktree-path]\n", getprogname());
342 exit(1);
345 static void
346 checkout_progress(void *arg, unsigned char status, const char *path)
348 char *worktree_path = arg;
350 /* Base commit bump happens silently. */
351 if (status == GOT_STATUS_BUMP_BASE)
352 return;
354 while (path[0] == '/')
355 path++;
357 printf("%c %s/%s\n", status, worktree_path, path);
360 static const struct got_error *
361 check_cancelled(void *arg)
363 if (sigint_received || sigpipe_received)
364 return got_error(GOT_ERR_CANCELLED);
365 return NULL;
368 static const struct got_error *
369 check_linear_ancestry(struct got_object_id *commit_id,
370 struct got_object_id *base_commit_id, struct got_repository *repo)
372 const struct got_error *err = NULL;
373 struct got_object_id *yca_id;
375 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
376 commit_id, base_commit_id, repo);
377 if (err)
378 return err;
380 if (yca_id == NULL)
381 return got_error(GOT_ERR_ANCESTRY);
383 /*
384 * Require a straight line of history between the target commit
385 * and the work tree's base commit.
387 * Non-linear situations such as this require a rebase:
389 * (commit) D F (base_commit)
390 * \ /
391 * C E
392 * \ /
393 * B (yca)
394 * |
395 * A
397 * 'got update' only handles linear cases:
398 * Update forwards in time: A (base/yca) - B - C - D (commit)
399 * Update backwards in time: D (base) - C - B - A (commit/yca)
400 */
401 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
402 got_object_id_cmp(base_commit_id, yca_id) != 0)
403 return got_error(GOT_ERR_ANCESTRY);
405 free(yca_id);
406 return NULL;
409 static const struct got_error *
410 check_same_branch(struct got_object_id *commit_id,
411 struct got_reference *head_ref, struct got_repository *repo)
413 const struct got_error *err = NULL;
414 struct got_commit_graph *graph = NULL;
415 struct got_object_id *head_commit_id = NULL;
416 int is_same_branch = 0;
418 err = got_ref_resolve(&head_commit_id, repo, head_ref);
419 if (err)
420 goto done;
422 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
423 if (err)
424 goto done;
426 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
427 if (err)
428 goto done;
430 for (;;) {
431 struct got_object_id *id;
432 err = got_commit_graph_iter_next(&id, graph);
433 if (err) {
434 if (err->code == GOT_ERR_ITER_COMPLETED) {
435 err = NULL;
436 break;
438 else if (err->code != GOT_ERR_ITER_NEED_MORE)
439 break;
440 err = got_commit_graph_fetch_commits(graph, 1,
441 repo);
442 if (err)
443 break;
446 if (id) {
447 if (got_object_id_cmp(id, commit_id) == 0) {
448 is_same_branch = 1;
449 break;
453 done:
454 if (graph)
455 got_commit_graph_close(graph);
456 free(head_commit_id);
457 if (!err && !is_same_branch)
458 err = got_error(GOT_ERR_ANCESTRY);
459 return err;
462 static const struct got_error *
463 cmd_checkout(int argc, char *argv[])
465 const struct got_error *error = NULL;
466 struct got_repository *repo = NULL;
467 struct got_reference *head_ref = NULL;
468 struct got_worktree *worktree = NULL;
469 char *repo_path = NULL;
470 char *worktree_path = NULL;
471 const char *path_prefix = "";
472 const char *branch_name = GOT_REF_HEAD;
473 char *commit_id_str = NULL;
474 int ch, same_path_prefix;
476 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
477 switch (ch) {
478 case 'b':
479 branch_name = optarg;
480 break;
481 case 'c':
482 commit_id_str = strdup(optarg);
483 if (commit_id_str == NULL)
484 return got_error_from_errno("strdup");
485 break;
486 case 'p':
487 path_prefix = optarg;
488 break;
489 default:
490 usage_checkout();
491 /* NOTREACHED */
495 argc -= optind;
496 argv += optind;
498 #ifndef PROFILE
499 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
500 "unveil", NULL) == -1)
501 err(1, "pledge");
502 #endif
503 if (argc == 1) {
504 char *cwd, *base, *dotgit;
505 repo_path = realpath(argv[0], NULL);
506 if (repo_path == NULL)
507 return got_error_from_errno2("realpath", argv[0]);
508 cwd = getcwd(NULL, 0);
509 if (cwd == NULL) {
510 error = got_error_from_errno("getcwd");
511 goto done;
513 if (path_prefix[0]) {
514 base = basename(path_prefix);
515 if (base == NULL) {
516 error = got_error_from_errno2("basename",
517 path_prefix);
518 goto done;
520 } else {
521 base = basename(repo_path);
522 if (base == NULL) {
523 error = got_error_from_errno2("basename",
524 repo_path);
525 goto done;
528 dotgit = strstr(base, ".git");
529 if (dotgit)
530 *dotgit = '\0';
531 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
532 error = got_error_from_errno("asprintf");
533 free(cwd);
534 goto done;
536 free(cwd);
537 } else if (argc == 2) {
538 repo_path = realpath(argv[0], NULL);
539 if (repo_path == NULL) {
540 error = got_error_from_errno2("realpath", argv[0]);
541 goto done;
543 worktree_path = realpath(argv[1], NULL);
544 if (worktree_path == NULL) {
545 error = got_error_from_errno2("realpath", argv[1]);
546 goto done;
548 } else
549 usage_checkout();
551 got_path_strip_trailing_slashes(repo_path);
552 got_path_strip_trailing_slashes(worktree_path);
554 error = got_repo_open(&repo, repo_path);
555 if (error != NULL)
556 goto done;
558 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
559 if (error)
560 goto done;
562 error = got_ref_open(&head_ref, repo, branch_name, 0);
563 if (error != NULL)
564 goto done;
566 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
567 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
568 goto done;
570 error = got_worktree_open(&worktree, worktree_path);
571 if (error != NULL)
572 goto done;
574 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
575 path_prefix);
576 if (error != NULL)
577 goto done;
578 if (!same_path_prefix) {
579 error = got_error(GOT_ERR_PATH_PREFIX);
580 goto done;
583 if (commit_id_str) {
584 struct got_object_id *commit_id;
585 error = got_repo_match_object_id_prefix(&commit_id,
586 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
587 if (error != NULL)
588 goto done;
589 error = check_linear_ancestry(commit_id,
590 got_worktree_get_base_commit_id(worktree), repo);
591 if (error != NULL) {
592 free(commit_id);
593 goto done;
595 error = check_same_branch(commit_id, head_ref, repo);
596 if (error)
597 goto done;
598 error = got_worktree_set_base_commit_id(worktree, repo,
599 commit_id);
600 free(commit_id);
601 if (error)
602 goto done;
605 error = got_worktree_checkout_files(worktree, "", repo,
606 checkout_progress, worktree_path, check_cancelled, NULL);
607 if (error != NULL)
608 goto done;
610 printf("Now shut up and hack\n");
612 done:
613 free(commit_id_str);
614 free(repo_path);
615 free(worktree_path);
616 return error;
619 __dead static void
620 usage_update(void)
622 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
623 getprogname());
624 exit(1);
627 static void
628 update_progress(void *arg, unsigned char status, const char *path)
630 int *did_something = arg;
632 if (status == GOT_STATUS_EXISTS)
633 return;
635 *did_something = 1;
637 /* Base commit bump happens silently. */
638 if (status == GOT_STATUS_BUMP_BASE)
639 return;
641 while (path[0] == '/')
642 path++;
643 printf("%c %s\n", status, path);
646 static const struct got_error *
647 switch_head_ref(struct got_reference *head_ref,
648 struct got_object_id *commit_id, struct got_worktree *worktree,
649 struct got_repository *repo)
651 const struct got_error *err = NULL;
652 char *base_id_str;
653 int ref_has_moved = 0;
655 /* Trivial case: switching between two different references. */
656 if (strcmp(got_ref_get_name(head_ref),
657 got_worktree_get_head_ref_name(worktree)) != 0) {
658 printf("Switching work tree from %s to %s\n",
659 got_worktree_get_head_ref_name(worktree),
660 got_ref_get_name(head_ref));
661 return got_worktree_set_head_ref(worktree, head_ref);
664 err = check_linear_ancestry(commit_id,
665 got_worktree_get_base_commit_id(worktree), repo);
666 if (err) {
667 if (err->code != GOT_ERR_ANCESTRY)
668 return err;
669 ref_has_moved = 1;
671 if (!ref_has_moved)
672 return NULL;
674 /* Switching to a rebased branch with the same reference name. */
675 err = got_object_id_str(&base_id_str,
676 got_worktree_get_base_commit_id(worktree));
677 if (err)
678 return err;
679 printf("Reference %s now points at a different branch\n",
680 got_worktree_get_head_ref_name(worktree));
681 printf("Switching work tree from %s to %s\n", base_id_str,
682 got_worktree_get_head_ref_name(worktree));
683 return NULL;
686 static const struct got_error *
687 cmd_update(int argc, char *argv[])
689 const struct got_error *error = NULL;
690 struct got_repository *repo = NULL;
691 struct got_worktree *worktree = NULL;
692 char *worktree_path = NULL, *path = NULL;
693 struct got_object_id *commit_id = NULL;
694 char *commit_id_str = NULL;
695 const char *branch_name = NULL;
696 struct got_reference *head_ref = NULL;
697 int ch, did_something = 0;
699 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
700 switch (ch) {
701 case 'b':
702 branch_name = optarg;
703 break;
704 case 'c':
705 commit_id_str = strdup(optarg);
706 if (commit_id_str == NULL)
707 return got_error_from_errno("strdup");
708 break;
709 default:
710 usage_update();
711 /* NOTREACHED */
715 argc -= optind;
716 argv += optind;
718 #ifndef PROFILE
719 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
720 "unveil", NULL) == -1)
721 err(1, "pledge");
722 #endif
723 worktree_path = getcwd(NULL, 0);
724 if (worktree_path == NULL) {
725 error = got_error_from_errno("getcwd");
726 goto done;
728 error = got_worktree_open(&worktree, worktree_path);
729 if (error)
730 goto done;
732 if (argc == 0) {
733 path = strdup("");
734 if (path == NULL) {
735 error = got_error_from_errno("strdup");
736 goto done;
738 } else if (argc == 1) {
739 error = got_worktree_resolve_path(&path, worktree, argv[0]);
740 if (error)
741 goto done;
742 } else
743 usage_update();
745 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
746 if (error != NULL)
747 goto done;
749 error = apply_unveil(got_repo_get_path(repo), 0,
750 got_worktree_get_root_path(worktree), 0);
751 if (error)
752 goto done;
754 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
755 got_worktree_get_head_ref_name(worktree), 0);
756 if (error != NULL)
757 goto done;
758 if (commit_id_str == NULL) {
759 error = got_ref_resolve(&commit_id, repo, head_ref);
760 if (error != NULL)
761 goto done;
762 error = got_object_id_str(&commit_id_str, commit_id);
763 if (error != NULL)
764 goto done;
765 } else {
766 error = got_repo_match_object_id_prefix(&commit_id,
767 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
768 if (error != NULL)
769 goto done;
772 if (branch_name) {
773 struct got_object_id *head_commit_id;
774 if (strlen(path) != 0) {
775 fprintf(stderr, "%s: switching between branches "
776 "requires that the entire work tree "
777 "gets updated, not just '%s'\n",
778 getprogname(), path);
779 error = got_error(GOT_ERR_BAD_PATH);
780 goto done;
782 error = got_ref_resolve(&head_commit_id, repo, head_ref);
783 if (error)
784 goto done;
785 error = check_linear_ancestry(commit_id, head_commit_id, repo);
786 free(head_commit_id);
787 if (error != NULL)
788 goto done;
789 error = check_same_branch(commit_id, head_ref, repo);
790 if (error)
791 goto done;
792 error = switch_head_ref(head_ref, commit_id, worktree, repo);
793 if (error)
794 goto done;
795 } else {
796 error = check_linear_ancestry(commit_id,
797 got_worktree_get_base_commit_id(worktree), repo);
798 if (error != NULL) {
799 if (error->code == GOT_ERR_ANCESTRY)
800 error = got_error(GOT_ERR_BRANCH_MOVED);
801 goto done;
803 error = check_same_branch(commit_id, head_ref, repo);
804 if (error)
805 goto done;
808 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
809 commit_id) != 0) {
810 error = got_worktree_set_base_commit_id(worktree, repo,
811 commit_id);
812 if (error)
813 goto done;
816 error = got_worktree_checkout_files(worktree, path, repo,
817 update_progress, &did_something, check_cancelled, NULL);
818 if (error != NULL)
819 goto done;
821 if (did_something)
822 printf("Updated to commit %s\n", commit_id_str);
823 else
824 printf("Already up-to-date\n");
825 done:
826 free(worktree_path);
827 free(path);
828 free(commit_id);
829 free(commit_id_str);
830 return error;
833 static const struct got_error *
834 print_patch(struct got_commit_object *commit, struct got_object_id *id,
835 int diff_context, struct got_repository *repo)
837 const struct got_error *err = NULL;
838 struct got_tree_object *tree1 = NULL, *tree2;
839 struct got_object_qid *qid;
840 char *id_str1 = NULL, *id_str2;
841 struct got_diff_blob_output_unidiff_arg arg;
843 err = got_object_open_as_tree(&tree2, repo,
844 got_object_commit_get_tree_id(commit));
845 if (err)
846 return err;
848 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
849 if (qid != NULL) {
850 struct got_commit_object *pcommit;
852 err = got_object_open_as_commit(&pcommit, repo, qid->id);
853 if (err)
854 return err;
856 err = got_object_open_as_tree(&tree1, repo,
857 got_object_commit_get_tree_id(pcommit));
858 got_object_commit_close(pcommit);
859 if (err)
860 return err;
862 err = got_object_id_str(&id_str1, qid->id);
863 if (err)
864 return err;
867 err = got_object_id_str(&id_str2, id);
868 if (err)
869 goto done;
871 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
872 arg.diff_context = diff_context;
873 arg.outfile = stdout;
874 err = got_diff_tree(tree1, tree2, "", "", repo,
875 got_diff_blob_output_unidiff, &arg);
876 done:
877 if (tree1)
878 got_object_tree_close(tree1);
879 got_object_tree_close(tree2);
880 free(id_str1);
881 free(id_str2);
882 return err;
885 static char *
886 get_datestr(time_t *time, char *datebuf)
888 char *p, *s = ctime_r(time, datebuf);
889 p = strchr(s, '\n');
890 if (p)
891 *p = '\0';
892 return s;
895 static const struct got_error *
896 print_commit(struct got_commit_object *commit, struct got_object_id *id,
897 struct got_repository *repo, int show_patch, int diff_context,
898 struct got_reflist_head *refs)
900 const struct got_error *err = NULL;
901 char *id_str, *datestr, *logmsg0, *logmsg, *line;
902 char datebuf[26];
903 time_t committer_time;
904 const char *author, *committer;
905 char *refs_str = NULL;
906 struct got_reflist_entry *re;
908 SIMPLEQ_FOREACH(re, refs, entry) {
909 char *s;
910 const char *name;
911 if (got_object_id_cmp(re->id, id) != 0)
912 continue;
913 name = got_ref_get_name(re->ref);
914 if (strcmp(name, GOT_REF_HEAD) == 0)
915 continue;
916 if (strncmp(name, "refs/", 5) == 0)
917 name += 5;
918 if (strncmp(name, "got/", 4) == 0)
919 continue;
920 if (strncmp(name, "heads/", 6) == 0)
921 name += 6;
922 if (strncmp(name, "remotes/", 8) == 0)
923 name += 8;
924 s = refs_str;
925 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
926 name) == -1) {
927 err = got_error_from_errno("asprintf");
928 free(s);
929 break;
931 free(s);
933 err = got_object_id_str(&id_str, id);
934 if (err)
935 return err;
937 printf("-----------------------------------------------\n");
938 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
939 refs_str ? refs_str : "", refs_str ? ")" : "");
940 free(id_str);
941 id_str = NULL;
942 free(refs_str);
943 refs_str = NULL;
944 printf("from: %s\n", got_object_commit_get_author(commit));
945 committer_time = got_object_commit_get_committer_time(commit);
946 datestr = get_datestr(&committer_time, datebuf);
947 printf("date: %s UTC\n", datestr);
948 author = got_object_commit_get_author(commit);
949 committer = got_object_commit_get_committer(commit);
950 if (strcmp(author, committer) != 0)
951 printf("via: %s\n", committer);
952 if (got_object_commit_get_nparents(commit) > 1) {
953 const struct got_object_id_queue *parent_ids;
954 struct got_object_qid *qid;
955 int n = 1;
956 parent_ids = got_object_commit_get_parent_ids(commit);
957 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
958 err = got_object_id_str(&id_str, qid->id);
959 if (err)
960 return err;
961 printf("parent %d: %s\n", n++, id_str);
962 free(id_str);
966 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
967 if (logmsg0 == NULL)
968 return got_error_from_errno("strdup");
970 logmsg = logmsg0;
971 do {
972 line = strsep(&logmsg, "\n");
973 if (line)
974 printf(" %s\n", line);
975 } while (line);
976 free(logmsg0);
978 if (show_patch) {
979 err = print_patch(commit, id, diff_context, repo);
980 if (err == 0)
981 printf("\n");
984 if (fflush(stdout) != 0 && err == NULL)
985 err = got_error_from_errno("fflush");
986 return err;
989 static const struct got_error *
990 print_commits(struct got_object_id *root_id, struct got_repository *repo,
991 char *path, int show_patch, int diff_context, int limit,
992 int first_parent_traversal, struct got_reflist_head *refs)
994 const struct got_error *err;
995 struct got_commit_graph *graph;
997 err = got_commit_graph_open(&graph, root_id, path,
998 first_parent_traversal, repo);
999 if (err)
1000 return err;
1001 err = got_commit_graph_iter_start(graph, root_id, repo);
1002 if (err)
1003 goto done;
1004 for (;;) {
1005 struct got_commit_object *commit;
1006 struct got_object_id *id;
1008 if (sigint_received || sigpipe_received)
1009 break;
1011 err = got_commit_graph_iter_next(&id, graph);
1012 if (err) {
1013 if (err->code == GOT_ERR_ITER_COMPLETED) {
1014 err = NULL;
1015 break;
1017 if (err->code != GOT_ERR_ITER_NEED_MORE)
1018 break;
1019 err = got_commit_graph_fetch_commits(graph, 1, repo);
1020 if (err)
1021 break;
1022 else
1023 continue;
1025 if (id == NULL)
1026 break;
1028 err = got_object_open_as_commit(&commit, repo, id);
1029 if (err)
1030 break;
1031 err = print_commit(commit, id, repo, show_patch, diff_context,
1032 refs);
1033 got_object_commit_close(commit);
1034 if (err || (limit && --limit == 0))
1035 break;
1037 done:
1038 got_commit_graph_close(graph);
1039 return err;
1042 __dead static void
1043 usage_log(void)
1045 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1046 "[-r repository-path] [path]\n", getprogname());
1047 exit(1);
1050 static const struct got_error *
1051 cmd_log(int argc, char *argv[])
1053 const struct got_error *error;
1054 struct got_repository *repo = NULL;
1055 struct got_worktree *worktree = NULL;
1056 struct got_commit_object *commit = NULL;
1057 struct got_object_id *id = NULL;
1058 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1059 char *start_commit = NULL;
1060 int diff_context = 3, ch;
1061 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1062 const char *errstr;
1063 struct got_reflist_head refs;
1065 SIMPLEQ_INIT(&refs);
1067 #ifndef PROFILE
1068 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1069 NULL)
1070 == -1)
1071 err(1, "pledge");
1072 #endif
1074 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1075 switch (ch) {
1076 case 'b':
1077 first_parent_traversal = 1;
1078 break;
1079 case 'p':
1080 show_patch = 1;
1081 break;
1082 case 'c':
1083 start_commit = optarg;
1084 break;
1085 case 'C':
1086 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1087 &errstr);
1088 if (errstr != NULL)
1089 err(1, "-C option %s", errstr);
1090 break;
1091 case 'l':
1092 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1093 if (errstr != NULL)
1094 err(1, "-l option %s", errstr);
1095 break;
1096 case 'r':
1097 repo_path = realpath(optarg, NULL);
1098 if (repo_path == NULL)
1099 err(1, "-r option");
1100 got_path_strip_trailing_slashes(repo_path);
1101 break;
1102 default:
1103 usage_log();
1104 /* NOTREACHED */
1108 argc -= optind;
1109 argv += optind;
1111 cwd = getcwd(NULL, 0);
1112 if (cwd == NULL) {
1113 error = got_error_from_errno("getcwd");
1114 goto done;
1117 error = got_worktree_open(&worktree, cwd);
1118 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1119 goto done;
1120 error = NULL;
1122 if (argc == 0) {
1123 path = strdup("");
1124 if (path == NULL) {
1125 error = got_error_from_errno("strdup");
1126 goto done;
1128 } else if (argc == 1) {
1129 if (worktree) {
1130 error = got_worktree_resolve_path(&path, worktree,
1131 argv[0]);
1132 if (error)
1133 goto done;
1134 } else {
1135 path = strdup(argv[0]);
1136 if (path == NULL) {
1137 error = got_error_from_errno("strdup");
1138 goto done;
1141 } else
1142 usage_log();
1144 if (repo_path == NULL) {
1145 repo_path = worktree ?
1146 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1148 if (repo_path == NULL) {
1149 error = got_error_from_errno("strdup");
1150 goto done;
1153 error = got_repo_open(&repo, repo_path);
1154 if (error != NULL)
1155 goto done;
1157 error = apply_unveil(got_repo_get_path(repo), 1,
1158 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1159 if (error)
1160 goto done;
1162 if (start_commit == NULL) {
1163 struct got_reference *head_ref;
1164 error = got_ref_open(&head_ref, repo,
1165 worktree ? got_worktree_get_head_ref_name(worktree)
1166 : GOT_REF_HEAD, 0);
1167 if (error != NULL)
1168 return error;
1169 error = got_ref_resolve(&id, repo, head_ref);
1170 got_ref_close(head_ref);
1171 if (error != NULL)
1172 return error;
1173 error = got_object_open_as_commit(&commit, repo, id);
1174 } else {
1175 struct got_reference *ref;
1176 error = got_ref_open(&ref, repo, start_commit, 0);
1177 if (error == NULL) {
1178 int obj_type;
1179 error = got_ref_resolve(&id, repo, ref);
1180 got_ref_close(ref);
1181 if (error != NULL)
1182 goto done;
1183 error = got_object_get_type(&obj_type, repo, id);
1184 if (error != NULL)
1185 goto done;
1186 if (obj_type == GOT_OBJ_TYPE_TAG) {
1187 struct got_tag_object *tag;
1188 error = got_object_open_as_tag(&tag, repo, id);
1189 if (error != NULL)
1190 goto done;
1191 if (got_object_tag_get_object_type(tag) !=
1192 GOT_OBJ_TYPE_COMMIT) {
1193 got_object_tag_close(tag);
1194 error = got_error(GOT_ERR_OBJ_TYPE);
1195 goto done;
1197 free(id);
1198 id = got_object_id_dup(
1199 got_object_tag_get_object_id(tag));
1200 if (id == NULL)
1201 error = got_error_from_errno(
1202 "got_object_id_dup");
1203 got_object_tag_close(tag);
1204 if (error)
1205 goto done;
1206 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1207 error = got_error(GOT_ERR_OBJ_TYPE);
1208 goto done;
1210 error = got_object_open_as_commit(&commit, repo, id);
1211 if (error != NULL)
1212 goto done;
1214 if (commit == NULL) {
1215 error = got_repo_match_object_id_prefix(&id,
1216 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1217 if (error != NULL)
1218 return error;
1221 if (error != NULL)
1222 goto done;
1224 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1225 if (error != NULL)
1226 goto done;
1227 if (in_repo_path) {
1228 free(path);
1229 path = in_repo_path;
1232 error = got_ref_list(&refs, repo);
1233 if (error)
1234 goto done;
1236 error = print_commits(id, repo, path, show_patch,
1237 diff_context, limit, first_parent_traversal, &refs);
1238 done:
1239 free(path);
1240 free(repo_path);
1241 free(cwd);
1242 free(id);
1243 if (worktree)
1244 got_worktree_close(worktree);
1245 if (repo) {
1246 const struct got_error *repo_error;
1247 repo_error = got_repo_close(repo);
1248 if (error == NULL)
1249 error = repo_error;
1251 got_ref_list_free(&refs);
1252 return error;
1255 __dead static void
1256 usage_diff(void)
1258 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1259 "[object1 object2 | path]\n", getprogname());
1260 exit(1);
1263 struct print_diff_arg {
1264 struct got_repository *repo;
1265 struct got_worktree *worktree;
1266 int diff_context;
1267 const char *id_str;
1268 int header_shown;
1271 static const struct got_error *
1272 print_diff(void *arg, unsigned char status, const char *path,
1273 struct got_object_id *blob_id, struct got_object_id *commit_id)
1275 struct print_diff_arg *a = arg;
1276 const struct got_error *err = NULL;
1277 struct got_blob_object *blob1 = NULL;
1278 FILE *f2 = NULL;
1279 char *abspath = NULL;
1280 struct stat sb;
1282 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1283 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1284 return NULL;
1286 if (!a->header_shown) {
1287 printf("diff %s %s\n", a->id_str,
1288 got_worktree_get_root_path(a->worktree));
1289 a->header_shown = 1;
1292 if (status != GOT_STATUS_ADD) {
1293 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1294 if (err)
1295 goto done;
1299 if (status != GOT_STATUS_DELETE) {
1300 if (asprintf(&abspath, "%s/%s",
1301 got_worktree_get_root_path(a->worktree), path) == -1) {
1302 err = got_error_from_errno("asprintf");
1303 goto done;
1306 f2 = fopen(abspath, "r");
1307 if (f2 == NULL) {
1308 err = got_error_from_errno2("fopen", abspath);
1309 goto done;
1311 if (lstat(abspath, &sb) == -1) {
1312 err = got_error_from_errno2("lstat", abspath);
1313 goto done;
1315 } else
1316 sb.st_size = 0;
1318 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1319 stdout);
1320 done:
1321 if (blob1)
1322 got_object_blob_close(blob1);
1323 if (f2 && fclose(f2) != 0 && err == NULL)
1324 err = got_error_from_errno("fclose");
1325 free(abspath);
1326 return err;
1329 static const struct got_error *
1330 cmd_diff(int argc, char *argv[])
1332 const struct got_error *error;
1333 struct got_repository *repo = NULL;
1334 struct got_worktree *worktree = NULL;
1335 char *cwd = NULL, *repo_path = NULL;
1336 struct got_object_id *id1 = NULL, *id2 = NULL;
1337 const char *id_str1 = NULL, *id_str2 = NULL;
1338 char *label1 = NULL, *label2 = NULL;
1339 int type1, type2;
1340 int diff_context = 3, ch;
1341 const char *errstr;
1342 char *path = NULL;
1344 #ifndef PROFILE
1345 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1346 NULL) == -1)
1347 err(1, "pledge");
1348 #endif
1350 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1351 switch (ch) {
1352 case 'C':
1353 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1354 if (errstr != NULL)
1355 err(1, "-C option %s", errstr);
1356 break;
1357 case 'r':
1358 repo_path = realpath(optarg, NULL);
1359 if (repo_path == NULL)
1360 err(1, "-r option");
1361 got_path_strip_trailing_slashes(repo_path);
1362 break;
1363 default:
1364 usage_diff();
1365 /* NOTREACHED */
1369 argc -= optind;
1370 argv += optind;
1372 cwd = getcwd(NULL, 0);
1373 if (cwd == NULL) {
1374 error = got_error_from_errno("getcwd");
1375 goto done;
1377 error = got_worktree_open(&worktree, cwd);
1378 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1379 goto done;
1380 if (argc <= 1) {
1381 if (worktree == NULL) {
1382 error = got_error(GOT_ERR_NOT_WORKTREE);
1383 goto done;
1385 if (repo_path)
1386 errx(1,
1387 "-r option can't be used when diffing a work tree");
1388 repo_path = strdup(got_worktree_get_repo_path(worktree));
1389 if (repo_path == NULL) {
1390 error = got_error_from_errno("strdup");
1391 goto done;
1393 if (argc == 1) {
1394 error = got_worktree_resolve_path(&path, worktree,
1395 argv[0]);
1396 if (error)
1397 goto done;
1398 } else {
1399 path = strdup("");
1400 if (path == NULL) {
1401 error = got_error_from_errno("strdup");
1402 goto done;
1405 } else if (argc == 2) {
1406 id_str1 = argv[0];
1407 id_str2 = argv[1];
1408 if (worktree && repo_path == NULL) {
1409 repo_path =
1410 strdup(got_worktree_get_repo_path(worktree));
1411 if (repo_path == NULL) {
1412 error = got_error_from_errno("strdup");
1413 goto done;
1416 } else
1417 usage_diff();
1419 if (repo_path == NULL) {
1420 repo_path = getcwd(NULL, 0);
1421 if (repo_path == NULL)
1422 return got_error_from_errno("getcwd");
1425 error = got_repo_open(&repo, repo_path);
1426 free(repo_path);
1427 if (error != NULL)
1428 goto done;
1430 error = apply_unveil(got_repo_get_path(repo), 1,
1431 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1432 if (error)
1433 goto done;
1435 if (argc <= 1) {
1436 struct print_diff_arg arg;
1437 char *id_str;
1438 error = got_object_id_str(&id_str,
1439 got_worktree_get_base_commit_id(worktree));
1440 if (error)
1441 goto done;
1442 arg.repo = repo;
1443 arg.worktree = worktree;
1444 arg.diff_context = diff_context;
1445 arg.id_str = id_str;
1446 arg.header_shown = 0;
1448 error = got_worktree_status(worktree, path, repo, print_diff,
1449 &arg, check_cancelled, NULL);
1450 free(id_str);
1451 goto done;
1454 error = got_repo_match_object_id_prefix(&id1, id_str1,
1455 GOT_OBJ_TYPE_ANY, repo);
1456 if (error) {
1457 struct got_reference *ref;
1458 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1459 goto done;
1460 error = got_ref_open(&ref, repo, id_str1, 0);
1461 if (error != NULL)
1462 goto done;
1463 label1 = strdup(got_ref_get_name(ref));
1464 if (label1 == NULL) {
1465 error = got_error_from_errno("strdup");
1466 goto done;
1468 error = got_ref_resolve(&id1, repo, ref);
1469 got_ref_close(ref);
1470 if (error != NULL)
1471 goto done;
1472 } else {
1473 label1 = strdup(id_str1);
1474 if (label1 == NULL) {
1475 error = got_error_from_errno("strdup");
1476 goto done;
1480 error = got_repo_match_object_id_prefix(&id2, id_str2,
1481 GOT_OBJ_TYPE_ANY, repo);
1482 if (error) {
1483 struct got_reference *ref;
1484 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1485 goto done;
1486 error = got_ref_open(&ref, repo, id_str2, 0);
1487 if (error != NULL)
1488 goto done;
1489 label2 = strdup(got_ref_get_name(ref));
1490 if (label2 == NULL) {
1491 error = got_error_from_errno("strdup");
1492 goto done;
1494 error = got_ref_resolve(&id2, repo, ref);
1495 got_ref_close(ref);
1496 if (error != NULL)
1497 goto done;
1498 } else {
1499 label2 = strdup(id_str2);
1500 if (label2 == NULL) {
1501 error = got_error_from_errno("strdup");
1502 goto done;
1506 error = got_object_get_type(&type1, repo, id1);
1507 if (error)
1508 goto done;
1510 error = got_object_get_type(&type2, repo, id2);
1511 if (error)
1512 goto done;
1514 if (type1 != type2) {
1515 error = got_error(GOT_ERR_OBJ_TYPE);
1516 goto done;
1519 switch (type1) {
1520 case GOT_OBJ_TYPE_BLOB:
1521 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1522 diff_context, repo, stdout);
1523 break;
1524 case GOT_OBJ_TYPE_TREE:
1525 error = got_diff_objects_as_trees(id1, id2, "", "",
1526 diff_context, repo, stdout);
1527 break;
1528 case GOT_OBJ_TYPE_COMMIT:
1529 printf("diff %s %s\n", label1, label2);
1530 error = got_diff_objects_as_commits(id1, id2, diff_context,
1531 repo, stdout);
1532 break;
1533 default:
1534 error = got_error(GOT_ERR_OBJ_TYPE);
1537 done:
1538 free(label1);
1539 free(label2);
1540 free(id1);
1541 free(id2);
1542 free(path);
1543 if (worktree)
1544 got_worktree_close(worktree);
1545 if (repo) {
1546 const struct got_error *repo_error;
1547 repo_error = got_repo_close(repo);
1548 if (error == NULL)
1549 error = repo_error;
1551 return error;
1554 __dead static void
1555 usage_blame(void)
1557 fprintf(stderr,
1558 "usage: %s blame [-c commit] [-r repository-path] path\n",
1559 getprogname());
1560 exit(1);
1563 static const struct got_error *
1564 cmd_blame(int argc, char *argv[])
1566 const struct got_error *error;
1567 struct got_repository *repo = NULL;
1568 struct got_worktree *worktree = NULL;
1569 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1570 struct got_object_id *commit_id = NULL;
1571 char *commit_id_str = NULL;
1572 int ch;
1574 #ifndef PROFILE
1575 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1576 NULL) == -1)
1577 err(1, "pledge");
1578 #endif
1580 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1581 switch (ch) {
1582 case 'c':
1583 commit_id_str = optarg;
1584 break;
1585 case 'r':
1586 repo_path = realpath(optarg, NULL);
1587 if (repo_path == NULL)
1588 err(1, "-r option");
1589 got_path_strip_trailing_slashes(repo_path);
1590 break;
1591 default:
1592 usage_blame();
1593 /* NOTREACHED */
1597 argc -= optind;
1598 argv += optind;
1600 if (argc == 1)
1601 path = argv[0];
1602 else
1603 usage_blame();
1605 cwd = getcwd(NULL, 0);
1606 if (cwd == NULL) {
1607 error = got_error_from_errno("getcwd");
1608 goto done;
1610 if (repo_path == NULL) {
1611 error = got_worktree_open(&worktree, cwd);
1612 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1613 goto done;
1614 else
1615 error = NULL;
1616 if (worktree) {
1617 repo_path =
1618 strdup(got_worktree_get_repo_path(worktree));
1619 if (repo_path == NULL)
1620 error = got_error_from_errno("strdup");
1621 if (error)
1622 goto done;
1623 } else {
1624 repo_path = strdup(cwd);
1625 if (repo_path == NULL) {
1626 error = got_error_from_errno("strdup");
1627 goto done;
1632 error = got_repo_open(&repo, repo_path);
1633 if (error != NULL)
1634 goto done;
1636 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1637 if (error)
1638 goto done;
1640 if (worktree) {
1641 const char *prefix = got_worktree_get_path_prefix(worktree);
1642 char *p, *worktree_subdir = cwd +
1643 strlen(got_worktree_get_root_path(worktree));
1644 if (asprintf(&p, "%s%s%s%s%s",
1645 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1646 worktree_subdir, worktree_subdir[0] ? "/" : "",
1647 path) == -1) {
1648 error = got_error_from_errno("asprintf");
1649 goto done;
1651 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1652 free(p);
1653 } else {
1654 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1656 if (error)
1657 goto done;
1659 if (commit_id_str == NULL) {
1660 struct got_reference *head_ref;
1661 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1662 if (error != NULL)
1663 goto done;
1664 error = got_ref_resolve(&commit_id, repo, head_ref);
1665 got_ref_close(head_ref);
1666 if (error != NULL)
1667 goto done;
1668 } else {
1669 error = got_repo_match_object_id_prefix(&commit_id,
1670 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1671 if (error != NULL)
1672 goto done;
1675 error = got_blame(in_repo_path, commit_id, repo, stdout);
1676 done:
1677 free(in_repo_path);
1678 free(repo_path);
1679 free(cwd);
1680 free(commit_id);
1681 if (worktree)
1682 got_worktree_close(worktree);
1683 if (repo) {
1684 const struct got_error *repo_error;
1685 repo_error = got_repo_close(repo);
1686 if (error == NULL)
1687 error = repo_error;
1689 return error;
1692 __dead static void
1693 usage_tree(void)
1695 fprintf(stderr,
1696 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1697 getprogname());
1698 exit(1);
1701 static void
1702 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1703 const char *root_path)
1705 int is_root_path = (strcmp(path, root_path) == 0);
1707 path += strlen(root_path);
1708 while (path[0] == '/')
1709 path++;
1711 printf("%s%s%s%s%s\n", id ? id : "", path,
1712 is_root_path ? "" : "/", te->name,
1713 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1716 static const struct got_error *
1717 print_tree(const char *path, struct got_object_id *commit_id,
1718 int show_ids, int recurse, const char *root_path,
1719 struct got_repository *repo)
1721 const struct got_error *err = NULL;
1722 struct got_object_id *tree_id = NULL;
1723 struct got_tree_object *tree = NULL;
1724 const struct got_tree_entries *entries;
1725 struct got_tree_entry *te;
1727 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1728 if (err)
1729 goto done;
1731 err = got_object_open_as_tree(&tree, repo, tree_id);
1732 if (err)
1733 goto done;
1734 entries = got_object_tree_get_entries(tree);
1735 te = SIMPLEQ_FIRST(&entries->head);
1736 while (te) {
1737 char *id = NULL;
1739 if (sigint_received || sigpipe_received)
1740 break;
1742 if (show_ids) {
1743 char *id_str;
1744 err = got_object_id_str(&id_str, te->id);
1745 if (err)
1746 goto done;
1747 if (asprintf(&id, "%s ", id_str) == -1) {
1748 err = got_error_from_errno("asprintf");
1749 free(id_str);
1750 goto done;
1752 free(id_str);
1754 print_entry(te, id, path, root_path);
1755 free(id);
1757 if (recurse && S_ISDIR(te->mode)) {
1758 char *child_path;
1759 if (asprintf(&child_path, "%s%s%s", path,
1760 path[0] == '/' && path[1] == '\0' ? "" : "/",
1761 te->name) == -1) {
1762 err = got_error_from_errno("asprintf");
1763 goto done;
1765 err = print_tree(child_path, commit_id, show_ids, 1,
1766 root_path, repo);
1767 free(child_path);
1768 if (err)
1769 goto done;
1772 te = SIMPLEQ_NEXT(te, entry);
1774 done:
1775 if (tree)
1776 got_object_tree_close(tree);
1777 free(tree_id);
1778 return err;
1781 static const struct got_error *
1782 cmd_tree(int argc, char *argv[])
1784 const struct got_error *error;
1785 struct got_repository *repo = NULL;
1786 struct got_worktree *worktree = NULL;
1787 const char *path;
1788 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1789 struct got_object_id *commit_id = NULL;
1790 char *commit_id_str = NULL;
1791 int show_ids = 0, recurse = 0;
1792 int ch;
1794 #ifndef PROFILE
1795 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1796 NULL) == -1)
1797 err(1, "pledge");
1798 #endif
1800 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1801 switch (ch) {
1802 case 'c':
1803 commit_id_str = optarg;
1804 break;
1805 case 'r':
1806 repo_path = realpath(optarg, NULL);
1807 if (repo_path == NULL)
1808 err(1, "-r option");
1809 got_path_strip_trailing_slashes(repo_path);
1810 break;
1811 case 'i':
1812 show_ids = 1;
1813 break;
1814 case 'R':
1815 recurse = 1;
1816 break;
1817 default:
1818 usage_tree();
1819 /* NOTREACHED */
1823 argc -= optind;
1824 argv += optind;
1826 if (argc == 1)
1827 path = argv[0];
1828 else if (argc > 1)
1829 usage_tree();
1830 else
1831 path = NULL;
1833 cwd = getcwd(NULL, 0);
1834 if (cwd == NULL) {
1835 error = got_error_from_errno("getcwd");
1836 goto done;
1838 if (repo_path == NULL) {
1839 error = got_worktree_open(&worktree, cwd);
1840 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1841 goto done;
1842 else
1843 error = NULL;
1844 if (worktree) {
1845 repo_path =
1846 strdup(got_worktree_get_repo_path(worktree));
1847 if (repo_path == NULL)
1848 error = got_error_from_errno("strdup");
1849 if (error)
1850 goto done;
1851 } else {
1852 repo_path = strdup(cwd);
1853 if (repo_path == NULL) {
1854 error = got_error_from_errno("strdup");
1855 goto done;
1860 error = got_repo_open(&repo, repo_path);
1861 if (error != NULL)
1862 goto done;
1864 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1865 if (error)
1866 goto done;
1868 if (path == NULL) {
1869 if (worktree) {
1870 char *p, *worktree_subdir = cwd +
1871 strlen(got_worktree_get_root_path(worktree));
1872 if (asprintf(&p, "%s/%s",
1873 got_worktree_get_path_prefix(worktree),
1874 worktree_subdir) == -1) {
1875 error = got_error_from_errno("asprintf");
1876 goto done;
1878 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1879 free(p);
1880 if (error)
1881 goto done;
1882 } else
1883 path = "/";
1885 if (in_repo_path == NULL) {
1886 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1887 if (error != NULL)
1888 goto done;
1891 if (commit_id_str == NULL) {
1892 struct got_reference *head_ref;
1893 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1894 if (error != NULL)
1895 goto done;
1896 error = got_ref_resolve(&commit_id, repo, head_ref);
1897 got_ref_close(head_ref);
1898 if (error != NULL)
1899 goto done;
1900 } else {
1901 error = got_repo_match_object_id_prefix(&commit_id,
1902 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1903 if (error != NULL)
1904 goto done;
1907 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1908 in_repo_path, repo);
1909 done:
1910 free(in_repo_path);
1911 free(repo_path);
1912 free(cwd);
1913 free(commit_id);
1914 if (worktree)
1915 got_worktree_close(worktree);
1916 if (repo) {
1917 const struct got_error *repo_error;
1918 repo_error = got_repo_close(repo);
1919 if (error == NULL)
1920 error = repo_error;
1922 return error;
1925 __dead static void
1926 usage_status(void)
1928 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1929 exit(1);
1932 static const struct got_error *
1933 print_status(void *arg, unsigned char status, const char *path,
1934 struct got_object_id *blob_id, struct got_object_id *commit_id)
1936 printf("%c %s\n", status, path);
1937 return NULL;
1940 static const struct got_error *
1941 cmd_status(int argc, char *argv[])
1943 const struct got_error *error = NULL;
1944 struct got_repository *repo = NULL;
1945 struct got_worktree *worktree = NULL;
1946 char *cwd = NULL, *path = NULL;
1947 int ch;
1949 while ((ch = getopt(argc, argv, "")) != -1) {
1950 switch (ch) {
1951 default:
1952 usage_status();
1953 /* NOTREACHED */
1957 argc -= optind;
1958 argv += optind;
1960 #ifndef PROFILE
1961 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1962 NULL) == -1)
1963 err(1, "pledge");
1964 #endif
1965 cwd = getcwd(NULL, 0);
1966 if (cwd == NULL) {
1967 error = got_error_from_errno("getcwd");
1968 goto done;
1971 error = got_worktree_open(&worktree, cwd);
1972 if (error != NULL)
1973 goto done;
1975 if (argc == 0) {
1976 path = strdup("");
1977 if (path == NULL) {
1978 error = got_error_from_errno("strdup");
1979 goto done;
1981 } else if (argc == 1) {
1982 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1983 if (error)
1984 goto done;
1985 } else
1986 usage_status();
1988 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1989 if (error != NULL)
1990 goto done;
1992 error = apply_unveil(got_repo_get_path(repo), 1,
1993 got_worktree_get_root_path(worktree), 0);
1994 if (error)
1995 goto done;
1997 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1998 check_cancelled, NULL);
1999 done:
2000 free(cwd);
2001 free(path);
2002 return error;
2005 __dead static void
2006 usage_ref(void)
2008 fprintf(stderr,
2009 "usage: %s ref [-r repository] -l | -d name | name target\n",
2010 getprogname());
2011 exit(1);
2014 static const struct got_error *
2015 list_refs(struct got_repository *repo)
2017 static const struct got_error *err = NULL;
2018 struct got_reflist_head refs;
2019 struct got_reflist_entry *re;
2021 SIMPLEQ_INIT(&refs);
2022 err = got_ref_list(&refs, repo);
2023 if (err)
2024 return err;
2026 SIMPLEQ_FOREACH(re, &refs, entry) {
2027 char *refstr;
2028 refstr = got_ref_to_str(re->ref);
2029 if (refstr == NULL)
2030 return got_error_from_errno("got_ref_to_str");
2031 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2032 free(refstr);
2035 got_ref_list_free(&refs);
2036 return NULL;
2039 static const struct got_error *
2040 delete_ref(struct got_repository *repo, const char *refname)
2042 const struct got_error *err = NULL;
2043 struct got_reference *ref;
2045 err = got_ref_open(&ref, repo, refname, 0);
2046 if (err)
2047 return err;
2049 err = got_ref_delete(ref, repo);
2050 got_ref_close(ref);
2051 return err;
2054 static const struct got_error *
2055 add_ref(struct got_repository *repo, const char *refname, const char *target)
2057 const struct got_error *err = NULL;
2058 struct got_object_id *id;
2059 struct got_reference *ref = NULL;
2061 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2062 repo);
2063 if (err) {
2064 struct got_reference *target_ref;
2066 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2067 return err;
2068 err = got_ref_open(&target_ref, repo, target, 0);
2069 if (err)
2070 return err;
2071 err = got_ref_resolve(&id, repo, target_ref);
2072 got_ref_close(target_ref);
2073 if (err)
2074 return err;
2077 err = got_ref_alloc(&ref, refname, id);
2078 if (err)
2079 goto done;
2081 err = got_ref_write(ref, repo);
2082 done:
2083 if (ref)
2084 got_ref_close(ref);
2085 free(id);
2086 return err;
2089 static const struct got_error *
2090 cmd_ref(int argc, char *argv[])
2092 const struct got_error *error = NULL;
2093 struct got_repository *repo = NULL;
2094 struct got_worktree *worktree = NULL;
2095 char *cwd = NULL, *repo_path = NULL;
2096 int ch, do_list = 0;
2097 const char *delref = NULL;
2099 /* TODO: Add -s option for adding symbolic references. */
2100 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2101 switch (ch) {
2102 case 'd':
2103 delref = optarg;
2104 break;
2105 case 'r':
2106 repo_path = realpath(optarg, NULL);
2107 if (repo_path == NULL)
2108 err(1, "-r option");
2109 got_path_strip_trailing_slashes(repo_path);
2110 break;
2111 case 'l':
2112 do_list = 1;
2113 break;
2114 default:
2115 usage_ref();
2116 /* NOTREACHED */
2120 if (do_list && delref)
2121 errx(1, "-l and -d options are mutually exclusive\n");
2123 argc -= optind;
2124 argv += optind;
2126 if (do_list || delref) {
2127 if (argc > 0)
2128 usage_ref();
2129 } else if (argc != 2)
2130 usage_ref();
2132 #ifndef PROFILE
2133 if (do_list) {
2134 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2135 NULL) == -1)
2136 err(1, "pledge");
2137 } else {
2138 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2139 "sendfd unveil", NULL) == -1)
2140 err(1, "pledge");
2142 #endif
2143 cwd = getcwd(NULL, 0);
2144 if (cwd == NULL) {
2145 error = got_error_from_errno("getcwd");
2146 goto done;
2149 if (repo_path == NULL) {
2150 error = got_worktree_open(&worktree, cwd);
2151 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2152 goto done;
2153 else
2154 error = NULL;
2155 if (worktree) {
2156 repo_path =
2157 strdup(got_worktree_get_repo_path(worktree));
2158 if (repo_path == NULL)
2159 error = got_error_from_errno("strdup");
2160 if (error)
2161 goto done;
2162 } else {
2163 repo_path = strdup(cwd);
2164 if (repo_path == NULL) {
2165 error = got_error_from_errno("strdup");
2166 goto done;
2171 error = got_repo_open(&repo, repo_path);
2172 if (error != NULL)
2173 goto done;
2175 error = apply_unveil(got_repo_get_path(repo), do_list,
2176 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2177 if (error)
2178 goto done;
2180 if (do_list)
2181 error = list_refs(repo);
2182 else if (delref)
2183 error = delete_ref(repo, delref);
2184 else
2185 error = add_ref(repo, argv[0], argv[1]);
2186 done:
2187 if (repo)
2188 got_repo_close(repo);
2189 if (worktree)
2190 got_worktree_close(worktree);
2191 free(cwd);
2192 free(repo_path);
2193 return error;
2196 __dead static void
2197 usage_branch(void)
2199 fprintf(stderr,
2200 "usage: %s branch [-r repository] -l | -d name | "
2201 "name [base-branch]\n", getprogname());
2202 exit(1);
2205 static const struct got_error *
2206 list_branches(struct got_repository *repo)
2208 static const struct got_error *err = NULL;
2209 struct got_reflist_head refs;
2210 struct got_reflist_entry *re;
2212 SIMPLEQ_INIT(&refs);
2213 err = got_ref_list(&refs, repo);
2214 if (err)
2215 return err;
2217 SIMPLEQ_FOREACH(re, &refs, entry) {
2218 const char *refname;
2219 char *refstr;
2220 refname = got_ref_get_name(re->ref);
2221 if (strncmp(refname, "refs/heads/", 11) != 0)
2222 continue;
2223 refname += 11;
2224 refstr = got_ref_to_str(re->ref);
2225 if (refstr == NULL)
2226 return got_error_from_errno("got_ref_to_str");
2227 printf("%s: %s\n", refname, refstr);
2228 free(refstr);
2231 got_ref_list_free(&refs);
2232 return NULL;
2235 static const struct got_error *
2236 delete_branch(struct got_repository *repo, const char *branch_name)
2238 const struct got_error *err = NULL;
2239 struct got_reference *ref;
2240 char *refname;
2242 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2243 return got_error_from_errno("asprintf");
2245 err = got_ref_open(&ref, repo, refname, 0);
2246 if (err)
2247 goto done;
2249 err = got_ref_delete(ref, repo);
2250 got_ref_close(ref);
2251 done:
2252 free(refname);
2253 return err;
2256 static const struct got_error *
2257 add_branch(struct got_repository *repo, const char *branch_name,
2258 const char *base_branch)
2260 const struct got_error *err = NULL;
2261 struct got_object_id *id = NULL;
2262 struct got_reference *ref = NULL;
2263 char *base_refname = NULL, *refname = NULL;
2264 struct got_reference *base_ref;
2266 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2267 base_refname = strdup(GOT_REF_HEAD);
2268 if (base_refname == NULL)
2269 return got_error_from_errno("strdup");
2270 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2271 return got_error_from_errno("asprintf");
2273 err = got_ref_open(&base_ref, repo, base_refname, 0);
2274 if (err)
2275 goto done;
2276 err = got_ref_resolve(&id, repo, base_ref);
2277 got_ref_close(base_ref);
2278 if (err)
2279 goto done;
2281 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2282 err = got_error_from_errno("asprintf");
2283 goto done;
2286 err = got_ref_open(&ref, repo, refname, 0);
2287 if (err == NULL) {
2288 err = got_error(GOT_ERR_BRANCH_EXISTS);
2289 goto done;
2290 } else if (err->code != GOT_ERR_NOT_REF)
2291 goto done;
2293 err = got_ref_alloc(&ref, refname, id);
2294 if (err)
2295 goto done;
2297 err = got_ref_write(ref, repo);
2298 done:
2299 if (ref)
2300 got_ref_close(ref);
2301 free(id);
2302 free(base_refname);
2303 free(refname);
2304 return err;
2307 static const struct got_error *
2308 cmd_branch(int argc, char *argv[])
2310 const struct got_error *error = NULL;
2311 struct got_repository *repo = NULL;
2312 struct got_worktree *worktree = NULL;
2313 char *cwd = NULL, *repo_path = NULL;
2314 int ch, do_list = 0;
2315 const char *delref = NULL;
2317 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2318 switch (ch) {
2319 case 'd':
2320 delref = optarg;
2321 break;
2322 case 'r':
2323 repo_path = realpath(optarg, NULL);
2324 if (repo_path == NULL)
2325 err(1, "-r option");
2326 got_path_strip_trailing_slashes(repo_path);
2327 break;
2328 case 'l':
2329 do_list = 1;
2330 break;
2331 default:
2332 usage_branch();
2333 /* NOTREACHED */
2337 if (do_list && delref)
2338 errx(1, "-l and -d options are mutually exclusive\n");
2340 argc -= optind;
2341 argv += optind;
2343 if (do_list || delref) {
2344 if (argc > 0)
2345 usage_branch();
2346 } else if (argc < 1 || argc > 2)
2347 usage_branch();
2349 #ifndef PROFILE
2350 if (do_list) {
2351 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2352 NULL) == -1)
2353 err(1, "pledge");
2354 } else {
2355 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2356 "sendfd unveil", NULL) == -1)
2357 err(1, "pledge");
2359 #endif
2360 cwd = getcwd(NULL, 0);
2361 if (cwd == NULL) {
2362 error = got_error_from_errno("getcwd");
2363 goto done;
2366 if (repo_path == NULL) {
2367 error = got_worktree_open(&worktree, cwd);
2368 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2369 goto done;
2370 else
2371 error = NULL;
2372 if (worktree) {
2373 repo_path =
2374 strdup(got_worktree_get_repo_path(worktree));
2375 if (repo_path == NULL)
2376 error = got_error_from_errno("strdup");
2377 if (error)
2378 goto done;
2379 } else {
2380 repo_path = strdup(cwd);
2381 if (repo_path == NULL) {
2382 error = got_error_from_errno("strdup");
2383 goto done;
2388 error = got_repo_open(&repo, repo_path);
2389 if (error != NULL)
2390 goto done;
2392 error = apply_unveil(got_repo_get_path(repo), do_list,
2393 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2394 if (error)
2395 goto done;
2397 if (do_list)
2398 error = list_branches(repo);
2399 else if (delref)
2400 error = delete_branch(repo, delref);
2401 else {
2402 const char *base_branch;
2403 if (argc == 1) {
2404 base_branch = worktree ?
2405 got_worktree_get_head_ref_name(worktree) :
2406 GOT_REF_HEAD;
2407 } else
2408 base_branch = argv[1];
2409 error = add_branch(repo, argv[0], base_branch);
2411 done:
2412 if (repo)
2413 got_repo_close(repo);
2414 if (worktree)
2415 got_worktree_close(worktree);
2416 free(cwd);
2417 free(repo_path);
2418 return error;
2421 __dead static void
2422 usage_add(void)
2424 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2425 exit(1);
2428 static const struct got_error *
2429 cmd_add(int argc, char *argv[])
2431 const struct got_error *error = NULL;
2432 struct got_repository *repo = NULL;
2433 struct got_worktree *worktree = NULL;
2434 char *cwd = NULL;
2435 struct got_pathlist_head paths;
2436 struct got_pathlist_entry *pe;
2437 int ch, x;
2439 TAILQ_INIT(&paths);
2441 while ((ch = getopt(argc, argv, "")) != -1) {
2442 switch (ch) {
2443 default:
2444 usage_add();
2445 /* NOTREACHED */
2449 argc -= optind;
2450 argv += optind;
2452 if (argc < 1)
2453 usage_add();
2455 /* make sure each file exists before doing anything halfway */
2456 for (x = 0; x < argc; x++) {
2457 char *path = realpath(argv[x], NULL);
2458 if (path == NULL) {
2459 error = got_error_from_errno2("realpath", argv[x]);
2460 goto done;
2462 free(path);
2465 cwd = getcwd(NULL, 0);
2466 if (cwd == NULL) {
2467 error = got_error_from_errno("getcwd");
2468 goto done;
2471 error = got_worktree_open(&worktree, cwd);
2472 if (error)
2473 goto done;
2475 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2476 if (error != NULL)
2477 goto done;
2479 error = apply_unveil(got_repo_get_path(repo), 1,
2480 got_worktree_get_root_path(worktree), 0);
2481 if (error)
2482 goto done;
2484 for (x = 0; x < argc; x++) {
2485 char *path = realpath(argv[x], NULL);
2486 if (path == NULL) {
2487 error = got_error_from_errno2("realpath", argv[x]);
2488 goto done;
2491 got_path_strip_trailing_slashes(path);
2492 error = got_pathlist_insert(&pe, &paths, path, NULL);
2493 if (error) {
2494 free(path);
2495 goto done;
2498 error = got_worktree_schedule_add(worktree, &paths, print_status,
2499 NULL, repo);
2500 done:
2501 if (repo)
2502 got_repo_close(repo);
2503 if (worktree)
2504 got_worktree_close(worktree);
2505 TAILQ_FOREACH(pe, &paths, entry)
2506 free((char *)pe->path);
2507 got_pathlist_free(&paths);
2508 free(cwd);
2509 return error;
2512 __dead static void
2513 usage_remove(void)
2515 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2516 exit(1);
2519 static const struct got_error *
2520 cmd_remove(int argc, char *argv[])
2522 const struct got_error *error = NULL;
2523 struct got_worktree *worktree = NULL;
2524 struct got_repository *repo = NULL;
2525 char *cwd = NULL;
2526 struct got_pathlist_head paths;
2527 struct got_pathlist_entry *pe;
2528 int ch, i, delete_local_mods = 0;
2530 TAILQ_INIT(&paths);
2532 while ((ch = getopt(argc, argv, "f")) != -1) {
2533 switch (ch) {
2534 case 'f':
2535 delete_local_mods = 1;
2536 break;
2537 default:
2538 usage_add();
2539 /* NOTREACHED */
2543 argc -= optind;
2544 argv += optind;
2546 if (argc < 1)
2547 usage_remove();
2549 /* make sure each file exists before doing anything halfway */
2550 for (i = 0; i < argc; i++) {
2551 char *path = realpath(argv[i], NULL);
2552 if (path == NULL) {
2553 error = got_error_from_errno2("realpath", argv[i]);
2554 goto done;
2556 free(path);
2559 cwd = getcwd(NULL, 0);
2560 if (cwd == NULL) {
2561 error = got_error_from_errno("getcwd");
2562 goto done;
2564 error = got_worktree_open(&worktree, cwd);
2565 if (error)
2566 goto done;
2568 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2569 if (error)
2570 goto done;
2572 error = apply_unveil(got_repo_get_path(repo), 1,
2573 got_worktree_get_root_path(worktree), 0);
2574 if (error)
2575 goto done;
2577 for (i = 0; i < argc; i++) {
2578 char *path = realpath(argv[i], NULL);
2579 if (path == NULL) {
2580 error = got_error_from_errno2("realpath", argv[i]);
2581 goto done;
2584 got_path_strip_trailing_slashes(path);
2585 error = got_pathlist_insert(&pe, &paths, path, NULL);
2586 if (error) {
2587 free(path);
2588 goto done;
2591 error = got_worktree_schedule_delete(worktree, &paths,
2592 delete_local_mods, print_status, NULL, repo);
2593 if (error)
2594 goto done;
2595 done:
2596 if (repo)
2597 got_repo_close(repo);
2598 if (worktree)
2599 got_worktree_close(worktree);
2600 TAILQ_FOREACH(pe, &paths, entry)
2601 free((char *)pe->path);
2602 got_pathlist_free(&paths);
2603 free(cwd);
2604 return error;
2607 __dead static void
2608 usage_revert(void)
2610 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2611 exit(1);
2614 static void
2615 revert_progress(void *arg, unsigned char status, const char *path)
2617 while (path[0] == '/')
2618 path++;
2619 printf("%c %s\n", status, path);
2622 static const struct got_error *
2623 cmd_revert(int argc, char *argv[])
2625 const struct got_error *error = NULL;
2626 struct got_worktree *worktree = NULL;
2627 struct got_repository *repo = NULL;
2628 char *cwd = NULL, *path = NULL;
2629 struct got_pathlist_head paths;
2630 struct got_pathlist_entry *pe;
2631 int ch, i;
2633 TAILQ_INIT(&paths);
2635 while ((ch = getopt(argc, argv, "")) != -1) {
2636 switch (ch) {
2637 default:
2638 usage_revert();
2639 /* NOTREACHED */
2643 argc -= optind;
2644 argv += optind;
2646 if (argc < 1)
2647 usage_revert();
2649 for (i = 0; i < argc; i++) {
2650 char *path = realpath(argv[i], NULL);
2651 if (path == NULL) {
2652 error = got_error_from_errno2("realpath", argv[i]);
2653 goto done;
2656 got_path_strip_trailing_slashes(path);
2657 error = got_pathlist_insert(&pe, &paths, path, NULL);
2658 if (error) {
2659 free(path);
2660 goto done;
2664 cwd = getcwd(NULL, 0);
2665 if (cwd == NULL) {
2666 error = got_error_from_errno("getcwd");
2667 goto done;
2669 error = got_worktree_open(&worktree, cwd);
2670 if (error)
2671 goto done;
2673 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2674 if (error != NULL)
2675 goto done;
2677 error = apply_unveil(got_repo_get_path(repo), 1,
2678 got_worktree_get_root_path(worktree), 0);
2679 if (error)
2680 goto done;
2682 error = got_worktree_revert(worktree, &paths,
2683 revert_progress, NULL, repo);
2684 if (error)
2685 goto done;
2686 done:
2687 if (repo)
2688 got_repo_close(repo);
2689 if (worktree)
2690 got_worktree_close(worktree);
2691 free(path);
2692 free(cwd);
2693 return error;
2696 __dead static void
2697 usage_commit(void)
2699 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2700 exit(1);
2703 int
2704 spawn_editor(const char *editor, const char *file)
2706 pid_t pid;
2707 sig_t sighup, sigint, sigquit;
2708 int st = -1;
2710 sighup = signal(SIGHUP, SIG_IGN);
2711 sigint = signal(SIGINT, SIG_IGN);
2712 sigquit = signal(SIGQUIT, SIG_IGN);
2714 switch (pid = fork()) {
2715 case -1:
2716 goto doneediting;
2717 case 0:
2718 execl(editor, editor, file, (char *)NULL);
2719 _exit(127);
2722 while (waitpid(pid, &st, 0) == -1)
2723 if (errno != EINTR)
2724 break;
2726 doneediting:
2727 (void)signal(SIGHUP, sighup);
2728 (void)signal(SIGINT, sigint);
2729 (void)signal(SIGQUIT, sigquit);
2731 if (!WIFEXITED(st)) {
2732 errno = EINTR;
2733 return -1;
2736 return WEXITSTATUS(st);
2739 struct collect_commit_logmsg_arg {
2740 const char *cmdline_log;
2741 const char *editor;
2742 const char *worktree_path;
2743 const char *branch_name;
2744 const char *repo_path;
2745 char *logmsg_path;
2749 static const struct got_error *
2750 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2751 void *arg)
2753 char *initial_content = NULL;
2754 struct got_pathlist_entry *pe;
2755 const struct got_error *err = NULL;
2756 char *template = NULL;
2757 struct collect_commit_logmsg_arg *a = arg;
2758 char buf[1024];
2759 struct stat st, st2;
2760 FILE *fp;
2761 size_t len;
2762 int fd, content_changed = 0;
2764 /* if a message was specified on the command line, just use it */
2765 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2766 len = strlen(a->cmdline_log) + 1;
2767 *logmsg = malloc(len + 1);
2768 if (*logmsg == NULL)
2769 return got_error_from_errno("malloc");
2770 strlcpy(*logmsg, a->cmdline_log, len);
2771 return NULL;
2774 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2775 return got_error_from_errno("asprintf");
2777 if (asprintf(&initial_content,
2778 "\n# changes to be committed on branch %s:\n",
2779 a->branch_name) == -1)
2780 return got_error_from_errno("asprintf");
2782 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2783 if (err)
2784 goto done;
2786 dprintf(fd, initial_content);
2788 TAILQ_FOREACH(pe, commitable_paths, entry) {
2789 struct got_commitable *ct = pe->data;
2790 dprintf(fd, "# %c %s\n",
2791 got_commitable_get_status(ct),
2792 got_commitable_get_path(ct));
2794 close(fd);
2796 if (stat(a->logmsg_path, &st) == -1) {
2797 err = got_error_from_errno2("stat", a->logmsg_path);
2798 goto done;
2801 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2802 err = got_error_from_errno("failed spawning editor");
2803 goto done;
2806 if (stat(a->logmsg_path, &st2) == -1) {
2807 err = got_error_from_errno("stat");
2808 goto done;
2811 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2812 unlink(a->logmsg_path);
2813 free(a->logmsg_path);
2814 a->logmsg_path = NULL;
2815 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2816 "no changes made to commit message, aborting");
2817 goto done;
2820 *logmsg = malloc(st2.st_size + 1);
2821 if (*logmsg == NULL) {
2822 err = got_error_from_errno("malloc");
2823 goto done;
2825 (*logmsg)[0] = '\0';
2826 len = 0;
2828 fp = fopen(a->logmsg_path, "r");
2829 if (fp == NULL) {
2830 err = got_error_from_errno("fopen");
2831 goto done;
2833 while (fgets(buf, sizeof(buf), fp) != NULL) {
2834 if (!content_changed && strcmp(buf, initial_content) != 0)
2835 content_changed = 1;
2836 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2837 continue; /* remove comments and leading empty lines */
2838 len = strlcat(*logmsg, buf, st2.st_size);
2840 fclose(fp);
2842 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2843 (*logmsg)[len - 1] = '\0';
2844 len--;
2847 if (len == 0 || !content_changed) {
2848 unlink(a->logmsg_path);
2849 free(a->logmsg_path);
2850 a->logmsg_path = NULL;
2851 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2852 "commit message cannot be empty, aborting");
2853 goto done;
2855 done:
2856 free(initial_content);
2857 free(template);
2859 /* Editor is done; we can now apply unveil(2) */
2860 if (err == NULL)
2861 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2862 return err;
2865 static const struct got_error *
2866 cmd_commit(int argc, char *argv[])
2868 const struct got_error *error = NULL;
2869 struct got_worktree *worktree = NULL;
2870 struct got_repository *repo = NULL;
2871 char *cwd = NULL, *path = NULL, *id_str = NULL;
2872 struct got_object_id *id = NULL;
2873 const char *logmsg = NULL;
2874 const char *got_author = getenv("GOT_AUTHOR");
2875 struct collect_commit_logmsg_arg cl_arg;
2876 char *editor = NULL;
2877 int ch;
2879 while ((ch = getopt(argc, argv, "m:")) != -1) {
2880 switch (ch) {
2881 case 'm':
2882 logmsg = optarg;
2883 break;
2884 default:
2885 usage_commit();
2886 /* NOTREACHED */
2890 argc -= optind;
2891 argv += optind;
2893 if (argc == 1) {
2894 path = realpath(argv[0], NULL);
2895 if (path == NULL) {
2896 error = got_error_from_errno2("realpath", argv[0]);
2897 goto done;
2899 got_path_strip_trailing_slashes(path);
2900 } else if (argc != 0)
2901 usage_commit();
2903 if (got_author == NULL) {
2904 /* TODO: Look current user up in password database */
2905 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2906 goto done;
2909 cwd = getcwd(NULL, 0);
2910 if (cwd == NULL) {
2911 error = got_error_from_errno("getcwd");
2912 goto done;
2914 error = got_worktree_open(&worktree, cwd);
2915 if (error)
2916 goto done;
2918 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2919 if (error != NULL)
2920 goto done;
2923 * unveil(2) traverses exec(2); if an editor is used we have
2924 * to apply unveil after the log message has been written.
2926 if (logmsg == NULL || strlen(logmsg) == 0)
2927 error = get_editor(&editor);
2928 else
2929 error = apply_unveil(got_repo_get_path(repo), 0,
2930 got_worktree_get_root_path(worktree), 0);
2931 if (error)
2932 goto done;
2934 cl_arg.editor = editor;
2935 cl_arg.cmdline_log = logmsg;
2936 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2937 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2938 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2939 cl_arg.branch_name += 5;
2940 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2941 cl_arg.branch_name += 6;
2942 cl_arg.repo_path = got_repo_get_path(repo);
2943 cl_arg.logmsg_path = NULL;
2944 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2945 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2946 if (error) {
2947 if (cl_arg.logmsg_path)
2948 fprintf(stderr, "%s: log message preserved in %s\n",
2949 getprogname(), cl_arg.logmsg_path);
2950 goto done;
2953 if (cl_arg.logmsg_path)
2954 unlink(cl_arg.logmsg_path);
2956 error = got_object_id_str(&id_str, id);
2957 if (error)
2958 goto done;
2959 printf("Created commit %s\n", id_str);
2960 done:
2961 if (repo)
2962 got_repo_close(repo);
2963 if (worktree)
2964 got_worktree_close(worktree);
2965 free(path);
2966 free(cwd);
2967 free(id_str);
2968 free(editor);
2969 return error;
2972 __dead static void
2973 usage_cherrypick(void)
2975 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2976 exit(1);
2979 static const struct got_error *
2980 cmd_cherrypick(int argc, char *argv[])
2982 const struct got_error *error = NULL;
2983 struct got_worktree *worktree = NULL;
2984 struct got_repository *repo = NULL;
2985 char *cwd = NULL, *commit_id_str = NULL;
2986 struct got_object_id *commit_id = NULL;
2987 struct got_commit_object *commit = NULL;
2988 struct got_object_qid *pid;
2989 struct got_reference *head_ref = NULL;
2990 int ch, did_something = 0;
2992 while ((ch = getopt(argc, argv, "")) != -1) {
2993 switch (ch) {
2994 default:
2995 usage_cherrypick();
2996 /* NOTREACHED */
3000 argc -= optind;
3001 argv += optind;
3003 if (argc != 1)
3004 usage_cherrypick();
3006 cwd = getcwd(NULL, 0);
3007 if (cwd == NULL) {
3008 error = got_error_from_errno("getcwd");
3009 goto done;
3011 error = got_worktree_open(&worktree, cwd);
3012 if (error)
3013 goto done;
3015 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3016 if (error != NULL)
3017 goto done;
3019 error = apply_unveil(got_repo_get_path(repo), 0,
3020 got_worktree_get_root_path(worktree), 0);
3021 if (error)
3022 goto done;
3024 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3025 GOT_OBJ_TYPE_COMMIT, repo);
3026 if (error != NULL) {
3027 struct got_reference *ref;
3028 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3029 goto done;
3030 error = got_ref_open(&ref, repo, argv[0], 0);
3031 if (error != NULL)
3032 goto done;
3033 error = got_ref_resolve(&commit_id, repo, ref);
3034 got_ref_close(ref);
3035 if (error != NULL)
3036 goto done;
3038 error = got_object_id_str(&commit_id_str, commit_id);
3039 if (error)
3040 goto done;
3042 error = got_ref_open(&head_ref, repo,
3043 got_worktree_get_head_ref_name(worktree), 0);
3044 if (error != NULL)
3045 goto done;
3047 error = check_same_branch(commit_id, head_ref, repo);
3048 if (error) {
3049 if (error->code != GOT_ERR_ANCESTRY)
3050 goto done;
3051 error = NULL;
3052 } else {
3053 error = got_error(GOT_ERR_SAME_BRANCH);
3054 goto done;
3057 error = got_object_open_as_commit(&commit, repo, commit_id);
3058 if (error)
3059 goto done;
3060 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3061 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3062 commit_id, repo, update_progress, &did_something, check_cancelled,
3063 NULL);
3064 if (error != NULL)
3065 goto done;
3067 if (did_something)
3068 printf("Merged commit %s\n", commit_id_str);
3069 done:
3070 if (commit)
3071 got_object_commit_close(commit);
3072 free(commit_id_str);
3073 if (head_ref)
3074 got_ref_close(head_ref);
3075 if (worktree)
3076 got_worktree_close(worktree);
3077 if (repo)
3078 got_repo_close(repo);
3079 return error;
3082 __dead static void
3083 usage_backout(void)
3085 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3086 exit(1);
3089 static const struct got_error *
3090 cmd_backout(int argc, char *argv[])
3092 const struct got_error *error = NULL;
3093 struct got_worktree *worktree = NULL;
3094 struct got_repository *repo = NULL;
3095 char *cwd = NULL, *commit_id_str = NULL;
3096 struct got_object_id *commit_id = NULL;
3097 struct got_commit_object *commit = NULL;
3098 struct got_object_qid *pid;
3099 struct got_reference *head_ref = NULL;
3100 int ch, did_something = 0;
3102 while ((ch = getopt(argc, argv, "")) != -1) {
3103 switch (ch) {
3104 default:
3105 usage_backout();
3106 /* NOTREACHED */
3110 argc -= optind;
3111 argv += optind;
3113 if (argc != 1)
3114 usage_backout();
3116 cwd = getcwd(NULL, 0);
3117 if (cwd == NULL) {
3118 error = got_error_from_errno("getcwd");
3119 goto done;
3121 error = got_worktree_open(&worktree, cwd);
3122 if (error)
3123 goto done;
3125 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3126 if (error != NULL)
3127 goto done;
3129 error = apply_unveil(got_repo_get_path(repo), 0,
3130 got_worktree_get_root_path(worktree), 0);
3131 if (error)
3132 goto done;
3134 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3135 GOT_OBJ_TYPE_COMMIT, repo);
3136 if (error != NULL) {
3137 struct got_reference *ref;
3138 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3139 goto done;
3140 error = got_ref_open(&ref, repo, argv[0], 0);
3141 if (error != NULL)
3142 goto done;
3143 error = got_ref_resolve(&commit_id, repo, ref);
3144 got_ref_close(ref);
3145 if (error != NULL)
3146 goto done;
3148 error = got_object_id_str(&commit_id_str, commit_id);
3149 if (error)
3150 goto done;
3152 error = got_ref_open(&head_ref, repo,
3153 got_worktree_get_head_ref_name(worktree), 0);
3154 if (error != NULL)
3155 goto done;
3157 error = check_same_branch(commit_id, head_ref, repo);
3158 if (error)
3159 goto done;
3161 error = got_object_open_as_commit(&commit, repo, commit_id);
3162 if (error)
3163 goto done;
3164 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3165 if (pid == NULL) {
3166 error = got_error(GOT_ERR_ROOT_COMMIT);
3167 goto done;
3170 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3171 update_progress, &did_something, check_cancelled, NULL);
3172 if (error != NULL)
3173 goto done;
3175 if (did_something)
3176 printf("Backed out commit %s\n", commit_id_str);
3177 done:
3178 if (commit)
3179 got_object_commit_close(commit);
3180 free(commit_id_str);
3181 if (head_ref)
3182 got_ref_close(head_ref);
3183 if (worktree)
3184 got_worktree_close(worktree);
3185 if (repo)
3186 got_repo_close(repo);
3187 return error;
3190 __dead static void
3191 usage_rebase(void)
3193 fprintf(stderr, "usage: %s rebase [-a] [-c] | branch\n", getprogname());
3194 exit(1);
3197 static const struct got_error *
3198 show_rebase_progress(struct got_commit_object *commit,
3199 struct got_object_id *old_id, struct got_object_id *new_id)
3201 const struct got_error *err;
3202 char *old_id_str = NULL, *new_id_str = NULL;
3203 char *logmsg0 = NULL, *logmsg, *nl;
3204 size_t len;
3206 err = got_object_id_str(&old_id_str, old_id);
3207 if (err)
3208 goto done;
3210 if (new_id) {
3211 err = got_object_id_str(&new_id_str, new_id);
3212 if (err)
3213 goto done;
3216 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3217 if (logmsg0 == NULL) {
3218 err = got_error_from_errno("strdup");
3219 goto done;
3221 logmsg = logmsg0;
3223 while (isspace(logmsg[0]))
3224 logmsg++;
3226 old_id_str[12] = '\0';
3227 if (new_id_str)
3228 new_id_str[12] = '\0';
3229 len = strlen(logmsg);
3230 if (len > 42)
3231 len = 42;
3232 logmsg[len] = '\0';
3233 nl = strchr(logmsg, '\n');
3234 if (nl)
3235 *nl = '\0';
3236 printf("%s -> %s: %s\n", old_id_str,
3237 new_id_str ? new_id_str : "no-op change", logmsg);
3238 done:
3239 free(old_id_str);
3240 free(new_id_str);
3241 free(logmsg0);
3242 return err;
3245 static void
3246 rebase_progress(void *arg, unsigned char status, const char *path)
3248 unsigned char *rebase_status = arg;
3250 while (path[0] == '/')
3251 path++;
3252 printf("%c %s\n", status, path);
3254 if (*rebase_status == GOT_STATUS_CONFLICT)
3255 return;
3256 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3257 *rebase_status = status;
3260 static const struct got_error *
3261 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3262 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3263 struct got_repository *repo)
3265 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3266 return got_worktree_rebase_complete(worktree,
3267 new_base_branch, tmp_branch, branch, repo);
3270 static const struct got_error *
3271 rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
3272 struct got_object_id *commit_id, struct got_repository *repo)
3274 const struct got_error *error;
3275 struct got_commit_object *commit;
3276 struct got_object_id *new_commit_id;
3278 error = got_object_open_as_commit(&commit, repo, commit_id);
3279 if (error)
3280 return error;
3282 error = got_worktree_rebase_commit(&new_commit_id, worktree,
3283 tmp_branch, commit, commit_id, repo);
3284 if (error) {
3285 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3286 goto done;
3287 error = show_rebase_progress(commit, commit_id, NULL);
3288 } else {
3289 error = show_rebase_progress(commit, commit_id, new_commit_id);
3290 free(new_commit_id);
3292 done:
3293 got_object_commit_close(commit);
3294 return error;
3297 static const struct got_error *
3298 cmd_rebase(int argc, char *argv[])
3300 const struct got_error *error = NULL;
3301 struct got_worktree *worktree = NULL;
3302 struct got_repository *repo = NULL;
3303 char *cwd = NULL;
3304 struct got_reference *branch = NULL;
3305 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3306 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3307 struct got_object_id *resume_commit_id = NULL;
3308 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3309 struct got_commit_graph *graph = NULL;
3310 struct got_commit_object *commit = NULL;
3311 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3312 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3313 struct got_object_id_queue commits;
3314 const struct got_object_id_queue *parent_ids;
3315 struct got_object_qid *qid, *pid;
3317 SIMPLEQ_INIT(&commits);
3319 while ((ch = getopt(argc, argv, "ac")) != -1) {
3320 switch (ch) {
3321 case 'a':
3322 abort_rebase = 1;
3323 break;
3324 case 'c':
3325 continue_rebase = 1;
3326 break;
3327 default:
3328 usage_rebase();
3329 /* NOTREACHED */
3333 argc -= optind;
3334 argv += optind;
3336 if (abort_rebase && continue_rebase)
3337 usage_rebase();
3338 else if (abort_rebase || continue_rebase) {
3339 if (argc != 0)
3340 usage_rebase();
3341 } else if (argc != 1)
3342 usage_rebase();
3344 cwd = getcwd(NULL, 0);
3345 if (cwd == NULL) {
3346 error = got_error_from_errno("getcwd");
3347 goto done;
3349 error = got_worktree_open(&worktree, cwd);
3350 if (error)
3351 goto done;
3354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3355 if (error != NULL)
3356 goto done;
3358 error = apply_unveil(got_repo_get_path(repo), 0,
3359 got_worktree_get_root_path(worktree), 0);
3360 if (error)
3361 goto done;
3363 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3364 if (error)
3365 goto done;
3367 if (rebase_in_progress && abort_rebase) {
3368 int did_something;
3369 error = got_worktree_rebase_continue(&resume_commit_id,
3370 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3371 if (error)
3372 goto done;
3373 printf("Switching work tree to %s\n",
3374 got_ref_get_symref_target(new_base_branch));
3375 error = got_worktree_rebase_abort(worktree, repo,
3376 new_base_branch, update_progress, &did_something);
3377 if (error)
3378 goto done;
3379 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3380 goto done; /* nothing else to do */
3381 } else if (abort_rebase) {
3382 error = got_error(GOT_ERR_NOT_REBASING);
3383 goto done;
3386 if (continue_rebase) {
3387 error = got_worktree_rebase_continue(&resume_commit_id,
3388 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3389 if (error)
3390 goto done;
3392 error = rebase_commit(worktree, tmp_branch, resume_commit_id,
3393 repo);
3394 if (error)
3395 goto done;
3397 yca_id = got_object_id_dup(resume_commit_id);
3398 if (yca_id == NULL) {
3399 error = got_error_from_errno("got_object_id_dup");
3400 goto done;
3402 } else {
3403 error = got_ref_open(&branch, repo, argv[0], 0);
3404 if (error != NULL)
3405 goto done;
3407 error = check_same_branch(
3408 got_worktree_get_base_commit_id(worktree), branch, repo);
3409 if (error) {
3410 if (error->code != GOT_ERR_ANCESTRY)
3411 goto done;
3412 error = NULL;
3413 } else {
3414 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3415 "specified branch resolves to a commit which "
3416 "is already contained in work tree's branch");
3417 goto done;
3421 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3422 if (error)
3423 goto done;
3425 if (!continue_rebase) {
3426 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3427 got_worktree_get_base_commit_id(worktree),
3428 branch_head_commit_id, repo);
3429 if (error)
3430 goto done;
3431 if (yca_id == NULL) {
3432 error = got_error_msg(GOT_ERR_ANCESTRY,
3433 "specified branch shares no common ancestry "
3434 "with work tree's branch");
3435 goto done;
3438 error = got_worktree_rebase_prepare(&new_base_branch,
3439 &tmp_branch, worktree, branch, repo);
3440 if (error)
3441 goto done;
3444 commit_id = branch_head_commit_id;
3445 error = got_object_open_as_commit(&commit, repo, commit_id);
3446 if (error)
3447 goto done;
3449 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3450 if (error)
3451 goto done;
3452 parent_ids = got_object_commit_get_parent_ids(commit);
3453 pid = SIMPLEQ_FIRST(parent_ids);
3454 error = got_commit_graph_iter_start(graph, pid->id, repo);
3455 got_object_commit_close(commit);
3456 commit = NULL;
3457 if (error)
3458 goto done;
3459 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3460 error = got_commit_graph_iter_next(&parent_id, graph);
3461 if (error) {
3462 if (error->code == GOT_ERR_ITER_COMPLETED) {
3463 error = got_error_msg(GOT_ERR_ANCESTRY,
3464 "ran out of commits to rebase before "
3465 "youngest common ancestor commit has "
3466 "been reached?!?");
3467 goto done;
3468 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3469 goto done;
3470 error = got_commit_graph_fetch_commits(graph, 1, repo);
3471 if (error)
3472 goto done;
3473 } else {
3474 error = got_object_qid_alloc(&qid, commit_id);
3475 if (error)
3476 goto done;
3477 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3478 commit_id = parent_id;
3482 if (SIMPLEQ_EMPTY(&commits)) {
3483 if (continue_rebase)
3484 error = rebase_complete(worktree, branch,
3485 new_base_branch, tmp_branch, repo);
3486 else
3487 error = got_error(GOT_ERR_EMPTY_REBASE);
3488 goto done;
3491 pid = NULL;
3492 SIMPLEQ_FOREACH(qid, &commits, entry) {
3493 commit_id = qid->id;
3494 parent_id = pid ? pid->id : yca_id;
3495 pid = qid;
3497 error = got_worktree_rebase_merge_files(worktree, parent_id,
3498 commit_id, repo, rebase_progress, &rebase_status,
3499 check_cancelled, NULL);
3500 if (error)
3501 goto done;
3503 if (rebase_status == GOT_STATUS_CONFLICT)
3504 break;
3506 error = rebase_commit(worktree, tmp_branch, commit_id, repo);
3507 if (error)
3508 goto done;
3511 if (rebase_status == GOT_STATUS_CONFLICT) {
3512 error = got_worktree_rebase_postpone(worktree);
3513 if (error)
3514 goto done;
3515 error = got_error_msg(GOT_ERR_CONFLICTS,
3516 "conflicts must be resolved before rebase can be resumed");
3517 } else
3518 error = rebase_complete(worktree, branch, new_base_branch,
3519 tmp_branch, repo);
3520 done:
3521 got_object_id_queue_free(&commits);
3522 free(branch_head_commit_id);
3523 free(resume_commit_id);
3524 free(yca_id);
3525 if (graph)
3526 got_commit_graph_close(graph);
3527 if (commit)
3528 got_object_commit_close(commit);
3529 if (branch)
3530 got_ref_close(branch);
3531 if (new_base_branch)
3532 got_ref_close(new_base_branch);
3533 if (tmp_branch)
3534 got_ref_close(tmp_branch);
3535 if (worktree)
3536 got_worktree_close(worktree);
3537 if (repo)
3538 got_repo_close(repo);
3539 return error;