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, rebase_in_progress;
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 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
733 if (error)
734 goto done;
735 if (rebase_in_progress) {
736 error = got_error(GOT_ERR_REBASING);
737 goto done;
740 if (argc == 0) {
741 path = strdup("");
742 if (path == NULL) {
743 error = got_error_from_errno("strdup");
744 goto done;
746 } else if (argc == 1) {
747 error = got_worktree_resolve_path(&path, worktree, argv[0]);
748 if (error)
749 goto done;
750 } else
751 usage_update();
753 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
754 if (error != NULL)
755 goto done;
757 error = apply_unveil(got_repo_get_path(repo), 0,
758 got_worktree_get_root_path(worktree), 0);
759 if (error)
760 goto done;
762 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
763 got_worktree_get_head_ref_name(worktree), 0);
764 if (error != NULL)
765 goto done;
766 if (commit_id_str == NULL) {
767 error = got_ref_resolve(&commit_id, repo, head_ref);
768 if (error != NULL)
769 goto done;
770 error = got_object_id_str(&commit_id_str, commit_id);
771 if (error != NULL)
772 goto done;
773 } else {
774 error = got_repo_match_object_id_prefix(&commit_id,
775 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
776 if (error != NULL)
777 goto done;
780 if (branch_name) {
781 struct got_object_id *head_commit_id;
782 if (strlen(path) != 0) {
783 fprintf(stderr, "%s: switching between branches "
784 "requires that the entire work tree "
785 "gets updated, not just '%s'\n",
786 getprogname(), path);
787 error = got_error(GOT_ERR_BAD_PATH);
788 goto done;
790 error = got_ref_resolve(&head_commit_id, repo, head_ref);
791 if (error)
792 goto done;
793 error = check_linear_ancestry(commit_id, head_commit_id, repo);
794 free(head_commit_id);
795 if (error != NULL)
796 goto done;
797 error = check_same_branch(commit_id, head_ref, repo);
798 if (error)
799 goto done;
800 error = switch_head_ref(head_ref, commit_id, worktree, repo);
801 if (error)
802 goto done;
803 } else {
804 error = check_linear_ancestry(commit_id,
805 got_worktree_get_base_commit_id(worktree), repo);
806 if (error != NULL) {
807 if (error->code == GOT_ERR_ANCESTRY)
808 error = got_error(GOT_ERR_BRANCH_MOVED);
809 goto done;
811 error = check_same_branch(commit_id, head_ref, repo);
812 if (error)
813 goto done;
816 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
817 commit_id) != 0) {
818 error = got_worktree_set_base_commit_id(worktree, repo,
819 commit_id);
820 if (error)
821 goto done;
824 error = got_worktree_checkout_files(worktree, path, repo,
825 update_progress, &did_something, check_cancelled, NULL);
826 if (error != NULL)
827 goto done;
829 if (did_something)
830 printf("Updated to commit %s\n", commit_id_str);
831 else
832 printf("Already up-to-date\n");
833 done:
834 free(worktree_path);
835 free(path);
836 free(commit_id);
837 free(commit_id_str);
838 return error;
841 static const struct got_error *
842 print_patch(struct got_commit_object *commit, struct got_object_id *id,
843 int diff_context, struct got_repository *repo)
845 const struct got_error *err = NULL;
846 struct got_tree_object *tree1 = NULL, *tree2;
847 struct got_object_qid *qid;
848 char *id_str1 = NULL, *id_str2;
849 struct got_diff_blob_output_unidiff_arg arg;
851 err = got_object_open_as_tree(&tree2, repo,
852 got_object_commit_get_tree_id(commit));
853 if (err)
854 return err;
856 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
857 if (qid != NULL) {
858 struct got_commit_object *pcommit;
860 err = got_object_open_as_commit(&pcommit, repo, qid->id);
861 if (err)
862 return err;
864 err = got_object_open_as_tree(&tree1, repo,
865 got_object_commit_get_tree_id(pcommit));
866 got_object_commit_close(pcommit);
867 if (err)
868 return err;
870 err = got_object_id_str(&id_str1, qid->id);
871 if (err)
872 return err;
875 err = got_object_id_str(&id_str2, id);
876 if (err)
877 goto done;
879 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
880 arg.diff_context = diff_context;
881 arg.outfile = stdout;
882 err = got_diff_tree(tree1, tree2, "", "", repo,
883 got_diff_blob_output_unidiff, &arg);
884 done:
885 if (tree1)
886 got_object_tree_close(tree1);
887 got_object_tree_close(tree2);
888 free(id_str1);
889 free(id_str2);
890 return err;
893 static char *
894 get_datestr(time_t *time, char *datebuf)
896 char *p, *s = ctime_r(time, datebuf);
897 p = strchr(s, '\n');
898 if (p)
899 *p = '\0';
900 return s;
903 static const struct got_error *
904 print_commit(struct got_commit_object *commit, struct got_object_id *id,
905 struct got_repository *repo, int show_patch, int diff_context,
906 struct got_reflist_head *refs)
908 const struct got_error *err = NULL;
909 char *id_str, *datestr, *logmsg0, *logmsg, *line;
910 char datebuf[26];
911 time_t committer_time;
912 const char *author, *committer;
913 char *refs_str = NULL;
914 struct got_reflist_entry *re;
916 SIMPLEQ_FOREACH(re, refs, entry) {
917 char *s;
918 const char *name;
919 if (got_object_id_cmp(re->id, id) != 0)
920 continue;
921 name = got_ref_get_name(re->ref);
922 if (strcmp(name, GOT_REF_HEAD) == 0)
923 continue;
924 if (strncmp(name, "refs/", 5) == 0)
925 name += 5;
926 if (strncmp(name, "got/", 4) == 0)
927 continue;
928 if (strncmp(name, "heads/", 6) == 0)
929 name += 6;
930 if (strncmp(name, "remotes/", 8) == 0)
931 name += 8;
932 s = refs_str;
933 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
934 name) == -1) {
935 err = got_error_from_errno("asprintf");
936 free(s);
937 break;
939 free(s);
941 err = got_object_id_str(&id_str, id);
942 if (err)
943 return err;
945 printf("-----------------------------------------------\n");
946 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
947 refs_str ? refs_str : "", refs_str ? ")" : "");
948 free(id_str);
949 id_str = NULL;
950 free(refs_str);
951 refs_str = NULL;
952 printf("from: %s\n", got_object_commit_get_author(commit));
953 committer_time = got_object_commit_get_committer_time(commit);
954 datestr = get_datestr(&committer_time, datebuf);
955 printf("date: %s UTC\n", datestr);
956 author = got_object_commit_get_author(commit);
957 committer = got_object_commit_get_committer(commit);
958 if (strcmp(author, committer) != 0)
959 printf("via: %s\n", committer);
960 if (got_object_commit_get_nparents(commit) > 1) {
961 const struct got_object_id_queue *parent_ids;
962 struct got_object_qid *qid;
963 int n = 1;
964 parent_ids = got_object_commit_get_parent_ids(commit);
965 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
966 err = got_object_id_str(&id_str, qid->id);
967 if (err)
968 return err;
969 printf("parent %d: %s\n", n++, id_str);
970 free(id_str);
974 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
975 if (logmsg0 == NULL)
976 return got_error_from_errno("strdup");
978 logmsg = logmsg0;
979 do {
980 line = strsep(&logmsg, "\n");
981 if (line)
982 printf(" %s\n", line);
983 } while (line);
984 free(logmsg0);
986 if (show_patch) {
987 err = print_patch(commit, id, diff_context, repo);
988 if (err == 0)
989 printf("\n");
992 if (fflush(stdout) != 0 && err == NULL)
993 err = got_error_from_errno("fflush");
994 return err;
997 static const struct got_error *
998 print_commits(struct got_object_id *root_id, struct got_repository *repo,
999 char *path, int show_patch, int diff_context, int limit,
1000 int first_parent_traversal, struct got_reflist_head *refs)
1002 const struct got_error *err;
1003 struct got_commit_graph *graph;
1005 err = got_commit_graph_open(&graph, root_id, path,
1006 first_parent_traversal, repo);
1007 if (err)
1008 return err;
1009 err = got_commit_graph_iter_start(graph, root_id, repo);
1010 if (err)
1011 goto done;
1012 for (;;) {
1013 struct got_commit_object *commit;
1014 struct got_object_id *id;
1016 if (sigint_received || sigpipe_received)
1017 break;
1019 err = got_commit_graph_iter_next(&id, graph);
1020 if (err) {
1021 if (err->code == GOT_ERR_ITER_COMPLETED) {
1022 err = NULL;
1023 break;
1025 if (err->code != GOT_ERR_ITER_NEED_MORE)
1026 break;
1027 err = got_commit_graph_fetch_commits(graph, 1, repo);
1028 if (err)
1029 break;
1030 else
1031 continue;
1033 if (id == NULL)
1034 break;
1036 err = got_object_open_as_commit(&commit, repo, id);
1037 if (err)
1038 break;
1039 err = print_commit(commit, id, repo, show_patch, diff_context,
1040 refs);
1041 got_object_commit_close(commit);
1042 if (err || (limit && --limit == 0))
1043 break;
1045 done:
1046 got_commit_graph_close(graph);
1047 return err;
1050 __dead static void
1051 usage_log(void)
1053 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1054 "[-r repository-path] [path]\n", getprogname());
1055 exit(1);
1058 static const struct got_error *
1059 cmd_log(int argc, char *argv[])
1061 const struct got_error *error;
1062 struct got_repository *repo = NULL;
1063 struct got_worktree *worktree = NULL;
1064 struct got_commit_object *commit = NULL;
1065 struct got_object_id *id = NULL;
1066 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1067 char *start_commit = NULL;
1068 int diff_context = 3, ch;
1069 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1070 const char *errstr;
1071 struct got_reflist_head refs;
1073 SIMPLEQ_INIT(&refs);
1075 #ifndef PROFILE
1076 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1077 NULL)
1078 == -1)
1079 err(1, "pledge");
1080 #endif
1082 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1083 switch (ch) {
1084 case 'b':
1085 first_parent_traversal = 1;
1086 break;
1087 case 'p':
1088 show_patch = 1;
1089 break;
1090 case 'c':
1091 start_commit = optarg;
1092 break;
1093 case 'C':
1094 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1095 &errstr);
1096 if (errstr != NULL)
1097 err(1, "-C option %s", errstr);
1098 break;
1099 case 'l':
1100 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1101 if (errstr != NULL)
1102 err(1, "-l option %s", errstr);
1103 break;
1104 case 'r':
1105 repo_path = realpath(optarg, NULL);
1106 if (repo_path == NULL)
1107 err(1, "-r option");
1108 got_path_strip_trailing_slashes(repo_path);
1109 break;
1110 default:
1111 usage_log();
1112 /* NOTREACHED */
1116 argc -= optind;
1117 argv += optind;
1119 cwd = getcwd(NULL, 0);
1120 if (cwd == NULL) {
1121 error = got_error_from_errno("getcwd");
1122 goto done;
1125 error = got_worktree_open(&worktree, cwd);
1126 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1127 goto done;
1128 error = NULL;
1130 if (argc == 0) {
1131 path = strdup("");
1132 if (path == NULL) {
1133 error = got_error_from_errno("strdup");
1134 goto done;
1136 } else if (argc == 1) {
1137 if (worktree) {
1138 error = got_worktree_resolve_path(&path, worktree,
1139 argv[0]);
1140 if (error)
1141 goto done;
1142 } else {
1143 path = strdup(argv[0]);
1144 if (path == NULL) {
1145 error = got_error_from_errno("strdup");
1146 goto done;
1149 } else
1150 usage_log();
1152 if (repo_path == NULL) {
1153 repo_path = worktree ?
1154 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1156 if (repo_path == NULL) {
1157 error = got_error_from_errno("strdup");
1158 goto done;
1161 error = got_repo_open(&repo, repo_path);
1162 if (error != NULL)
1163 goto done;
1165 error = apply_unveil(got_repo_get_path(repo), 1,
1166 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1167 if (error)
1168 goto done;
1170 if (start_commit == NULL) {
1171 struct got_reference *head_ref;
1172 error = got_ref_open(&head_ref, repo,
1173 worktree ? got_worktree_get_head_ref_name(worktree)
1174 : GOT_REF_HEAD, 0);
1175 if (error != NULL)
1176 return error;
1177 error = got_ref_resolve(&id, repo, head_ref);
1178 got_ref_close(head_ref);
1179 if (error != NULL)
1180 return error;
1181 error = got_object_open_as_commit(&commit, repo, id);
1182 } else {
1183 struct got_reference *ref;
1184 error = got_ref_open(&ref, repo, start_commit, 0);
1185 if (error == NULL) {
1186 int obj_type;
1187 error = got_ref_resolve(&id, repo, ref);
1188 got_ref_close(ref);
1189 if (error != NULL)
1190 goto done;
1191 error = got_object_get_type(&obj_type, repo, id);
1192 if (error != NULL)
1193 goto done;
1194 if (obj_type == GOT_OBJ_TYPE_TAG) {
1195 struct got_tag_object *tag;
1196 error = got_object_open_as_tag(&tag, repo, id);
1197 if (error != NULL)
1198 goto done;
1199 if (got_object_tag_get_object_type(tag) !=
1200 GOT_OBJ_TYPE_COMMIT) {
1201 got_object_tag_close(tag);
1202 error = got_error(GOT_ERR_OBJ_TYPE);
1203 goto done;
1205 free(id);
1206 id = got_object_id_dup(
1207 got_object_tag_get_object_id(tag));
1208 if (id == NULL)
1209 error = got_error_from_errno(
1210 "got_object_id_dup");
1211 got_object_tag_close(tag);
1212 if (error)
1213 goto done;
1214 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1215 error = got_error(GOT_ERR_OBJ_TYPE);
1216 goto done;
1218 error = got_object_open_as_commit(&commit, repo, id);
1219 if (error != NULL)
1220 goto done;
1222 if (commit == NULL) {
1223 error = got_repo_match_object_id_prefix(&id,
1224 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1225 if (error != NULL)
1226 return error;
1229 if (error != NULL)
1230 goto done;
1232 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1233 if (error != NULL)
1234 goto done;
1235 if (in_repo_path) {
1236 free(path);
1237 path = in_repo_path;
1240 error = got_ref_list(&refs, repo);
1241 if (error)
1242 goto done;
1244 error = print_commits(id, repo, path, show_patch,
1245 diff_context, limit, first_parent_traversal, &refs);
1246 done:
1247 free(path);
1248 free(repo_path);
1249 free(cwd);
1250 free(id);
1251 if (worktree)
1252 got_worktree_close(worktree);
1253 if (repo) {
1254 const struct got_error *repo_error;
1255 repo_error = got_repo_close(repo);
1256 if (error == NULL)
1257 error = repo_error;
1259 got_ref_list_free(&refs);
1260 return error;
1263 __dead static void
1264 usage_diff(void)
1266 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1267 "[object1 object2 | path]\n", getprogname());
1268 exit(1);
1271 struct print_diff_arg {
1272 struct got_repository *repo;
1273 struct got_worktree *worktree;
1274 int diff_context;
1275 const char *id_str;
1276 int header_shown;
1279 static const struct got_error *
1280 print_diff(void *arg, unsigned char status, const char *path,
1281 struct got_object_id *blob_id, struct got_object_id *commit_id)
1283 struct print_diff_arg *a = arg;
1284 const struct got_error *err = NULL;
1285 struct got_blob_object *blob1 = NULL;
1286 FILE *f2 = NULL;
1287 char *abspath = NULL;
1288 struct stat sb;
1290 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1291 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1292 return NULL;
1294 if (!a->header_shown) {
1295 printf("diff %s %s\n", a->id_str,
1296 got_worktree_get_root_path(a->worktree));
1297 a->header_shown = 1;
1300 if (status != GOT_STATUS_ADD) {
1301 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1302 if (err)
1303 goto done;
1307 if (status != GOT_STATUS_DELETE) {
1308 if (asprintf(&abspath, "%s/%s",
1309 got_worktree_get_root_path(a->worktree), path) == -1) {
1310 err = got_error_from_errno("asprintf");
1311 goto done;
1314 f2 = fopen(abspath, "r");
1315 if (f2 == NULL) {
1316 err = got_error_from_errno2("fopen", abspath);
1317 goto done;
1319 if (lstat(abspath, &sb) == -1) {
1320 err = got_error_from_errno2("lstat", abspath);
1321 goto done;
1323 } else
1324 sb.st_size = 0;
1326 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1327 stdout);
1328 done:
1329 if (blob1)
1330 got_object_blob_close(blob1);
1331 if (f2 && fclose(f2) != 0 && err == NULL)
1332 err = got_error_from_errno("fclose");
1333 free(abspath);
1334 return err;
1337 static const struct got_error *
1338 cmd_diff(int argc, char *argv[])
1340 const struct got_error *error;
1341 struct got_repository *repo = NULL;
1342 struct got_worktree *worktree = NULL;
1343 char *cwd = NULL, *repo_path = NULL;
1344 struct got_object_id *id1 = NULL, *id2 = NULL;
1345 const char *id_str1 = NULL, *id_str2 = NULL;
1346 char *label1 = NULL, *label2 = NULL;
1347 int type1, type2;
1348 int diff_context = 3, ch;
1349 const char *errstr;
1350 char *path = NULL;
1352 #ifndef PROFILE
1353 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1354 NULL) == -1)
1355 err(1, "pledge");
1356 #endif
1358 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1359 switch (ch) {
1360 case 'C':
1361 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1362 if (errstr != NULL)
1363 err(1, "-C option %s", errstr);
1364 break;
1365 case 'r':
1366 repo_path = realpath(optarg, NULL);
1367 if (repo_path == NULL)
1368 err(1, "-r option");
1369 got_path_strip_trailing_slashes(repo_path);
1370 break;
1371 default:
1372 usage_diff();
1373 /* NOTREACHED */
1377 argc -= optind;
1378 argv += optind;
1380 cwd = getcwd(NULL, 0);
1381 if (cwd == NULL) {
1382 error = got_error_from_errno("getcwd");
1383 goto done;
1385 error = got_worktree_open(&worktree, cwd);
1386 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1387 goto done;
1388 if (argc <= 1) {
1389 if (worktree == NULL) {
1390 error = got_error(GOT_ERR_NOT_WORKTREE);
1391 goto done;
1393 if (repo_path)
1394 errx(1,
1395 "-r option can't be used when diffing a work tree");
1396 repo_path = strdup(got_worktree_get_repo_path(worktree));
1397 if (repo_path == NULL) {
1398 error = got_error_from_errno("strdup");
1399 goto done;
1401 if (argc == 1) {
1402 error = got_worktree_resolve_path(&path, worktree,
1403 argv[0]);
1404 if (error)
1405 goto done;
1406 } else {
1407 path = strdup("");
1408 if (path == NULL) {
1409 error = got_error_from_errno("strdup");
1410 goto done;
1413 } else if (argc == 2) {
1414 id_str1 = argv[0];
1415 id_str2 = argv[1];
1416 if (worktree && repo_path == NULL) {
1417 repo_path =
1418 strdup(got_worktree_get_repo_path(worktree));
1419 if (repo_path == NULL) {
1420 error = got_error_from_errno("strdup");
1421 goto done;
1424 } else
1425 usage_diff();
1427 if (repo_path == NULL) {
1428 repo_path = getcwd(NULL, 0);
1429 if (repo_path == NULL)
1430 return got_error_from_errno("getcwd");
1433 error = got_repo_open(&repo, repo_path);
1434 free(repo_path);
1435 if (error != NULL)
1436 goto done;
1438 error = apply_unveil(got_repo_get_path(repo), 1,
1439 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1440 if (error)
1441 goto done;
1443 if (argc <= 1) {
1444 struct print_diff_arg arg;
1445 char *id_str;
1446 error = got_object_id_str(&id_str,
1447 got_worktree_get_base_commit_id(worktree));
1448 if (error)
1449 goto done;
1450 arg.repo = repo;
1451 arg.worktree = worktree;
1452 arg.diff_context = diff_context;
1453 arg.id_str = id_str;
1454 arg.header_shown = 0;
1456 error = got_worktree_status(worktree, path, repo, print_diff,
1457 &arg, check_cancelled, NULL);
1458 free(id_str);
1459 goto done;
1462 error = got_repo_match_object_id_prefix(&id1, id_str1,
1463 GOT_OBJ_TYPE_ANY, repo);
1464 if (error) {
1465 struct got_reference *ref;
1466 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1467 goto done;
1468 error = got_ref_open(&ref, repo, id_str1, 0);
1469 if (error != NULL)
1470 goto done;
1471 label1 = strdup(got_ref_get_name(ref));
1472 if (label1 == NULL) {
1473 error = got_error_from_errno("strdup");
1474 goto done;
1476 error = got_ref_resolve(&id1, repo, ref);
1477 got_ref_close(ref);
1478 if (error != NULL)
1479 goto done;
1480 } else {
1481 label1 = strdup(id_str1);
1482 if (label1 == NULL) {
1483 error = got_error_from_errno("strdup");
1484 goto done;
1488 error = got_repo_match_object_id_prefix(&id2, id_str2,
1489 GOT_OBJ_TYPE_ANY, repo);
1490 if (error) {
1491 struct got_reference *ref;
1492 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1493 goto done;
1494 error = got_ref_open(&ref, repo, id_str2, 0);
1495 if (error != NULL)
1496 goto done;
1497 label2 = strdup(got_ref_get_name(ref));
1498 if (label2 == NULL) {
1499 error = got_error_from_errno("strdup");
1500 goto done;
1502 error = got_ref_resolve(&id2, repo, ref);
1503 got_ref_close(ref);
1504 if (error != NULL)
1505 goto done;
1506 } else {
1507 label2 = strdup(id_str2);
1508 if (label2 == NULL) {
1509 error = got_error_from_errno("strdup");
1510 goto done;
1514 error = got_object_get_type(&type1, repo, id1);
1515 if (error)
1516 goto done;
1518 error = got_object_get_type(&type2, repo, id2);
1519 if (error)
1520 goto done;
1522 if (type1 != type2) {
1523 error = got_error(GOT_ERR_OBJ_TYPE);
1524 goto done;
1527 switch (type1) {
1528 case GOT_OBJ_TYPE_BLOB:
1529 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1530 diff_context, repo, stdout);
1531 break;
1532 case GOT_OBJ_TYPE_TREE:
1533 error = got_diff_objects_as_trees(id1, id2, "", "",
1534 diff_context, repo, stdout);
1535 break;
1536 case GOT_OBJ_TYPE_COMMIT:
1537 printf("diff %s %s\n", label1, label2);
1538 error = got_diff_objects_as_commits(id1, id2, diff_context,
1539 repo, stdout);
1540 break;
1541 default:
1542 error = got_error(GOT_ERR_OBJ_TYPE);
1545 done:
1546 free(label1);
1547 free(label2);
1548 free(id1);
1549 free(id2);
1550 free(path);
1551 if (worktree)
1552 got_worktree_close(worktree);
1553 if (repo) {
1554 const struct got_error *repo_error;
1555 repo_error = got_repo_close(repo);
1556 if (error == NULL)
1557 error = repo_error;
1559 return error;
1562 __dead static void
1563 usage_blame(void)
1565 fprintf(stderr,
1566 "usage: %s blame [-c commit] [-r repository-path] path\n",
1567 getprogname());
1568 exit(1);
1571 static const struct got_error *
1572 cmd_blame(int argc, char *argv[])
1574 const struct got_error *error;
1575 struct got_repository *repo = NULL;
1576 struct got_worktree *worktree = NULL;
1577 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1578 struct got_object_id *commit_id = NULL;
1579 char *commit_id_str = NULL;
1580 int ch;
1582 #ifndef PROFILE
1583 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1584 NULL) == -1)
1585 err(1, "pledge");
1586 #endif
1588 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1589 switch (ch) {
1590 case 'c':
1591 commit_id_str = optarg;
1592 break;
1593 case 'r':
1594 repo_path = realpath(optarg, NULL);
1595 if (repo_path == NULL)
1596 err(1, "-r option");
1597 got_path_strip_trailing_slashes(repo_path);
1598 break;
1599 default:
1600 usage_blame();
1601 /* NOTREACHED */
1605 argc -= optind;
1606 argv += optind;
1608 if (argc == 1)
1609 path = argv[0];
1610 else
1611 usage_blame();
1613 cwd = getcwd(NULL, 0);
1614 if (cwd == NULL) {
1615 error = got_error_from_errno("getcwd");
1616 goto done;
1618 if (repo_path == NULL) {
1619 error = got_worktree_open(&worktree, cwd);
1620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1621 goto done;
1622 else
1623 error = NULL;
1624 if (worktree) {
1625 repo_path =
1626 strdup(got_worktree_get_repo_path(worktree));
1627 if (repo_path == NULL)
1628 error = got_error_from_errno("strdup");
1629 if (error)
1630 goto done;
1631 } else {
1632 repo_path = strdup(cwd);
1633 if (repo_path == NULL) {
1634 error = got_error_from_errno("strdup");
1635 goto done;
1640 error = got_repo_open(&repo, repo_path);
1641 if (error != NULL)
1642 goto done;
1644 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1645 if (error)
1646 goto done;
1648 if (worktree) {
1649 const char *prefix = got_worktree_get_path_prefix(worktree);
1650 char *p, *worktree_subdir = cwd +
1651 strlen(got_worktree_get_root_path(worktree));
1652 if (asprintf(&p, "%s%s%s%s%s",
1653 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1654 worktree_subdir, worktree_subdir[0] ? "/" : "",
1655 path) == -1) {
1656 error = got_error_from_errno("asprintf");
1657 goto done;
1659 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1660 free(p);
1661 } else {
1662 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1664 if (error)
1665 goto done;
1667 if (commit_id_str == NULL) {
1668 struct got_reference *head_ref;
1669 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1670 if (error != NULL)
1671 goto done;
1672 error = got_ref_resolve(&commit_id, repo, head_ref);
1673 got_ref_close(head_ref);
1674 if (error != NULL)
1675 goto done;
1676 } else {
1677 error = got_repo_match_object_id_prefix(&commit_id,
1678 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1679 if (error != NULL)
1680 goto done;
1683 error = got_blame(in_repo_path, commit_id, repo, stdout);
1684 done:
1685 free(in_repo_path);
1686 free(repo_path);
1687 free(cwd);
1688 free(commit_id);
1689 if (worktree)
1690 got_worktree_close(worktree);
1691 if (repo) {
1692 const struct got_error *repo_error;
1693 repo_error = got_repo_close(repo);
1694 if (error == NULL)
1695 error = repo_error;
1697 return error;
1700 __dead static void
1701 usage_tree(void)
1703 fprintf(stderr,
1704 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1705 getprogname());
1706 exit(1);
1709 static void
1710 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1711 const char *root_path)
1713 int is_root_path = (strcmp(path, root_path) == 0);
1715 path += strlen(root_path);
1716 while (path[0] == '/')
1717 path++;
1719 printf("%s%s%s%s%s\n", id ? id : "", path,
1720 is_root_path ? "" : "/", te->name,
1721 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1724 static const struct got_error *
1725 print_tree(const char *path, struct got_object_id *commit_id,
1726 int show_ids, int recurse, const char *root_path,
1727 struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 struct got_object_id *tree_id = NULL;
1731 struct got_tree_object *tree = NULL;
1732 const struct got_tree_entries *entries;
1733 struct got_tree_entry *te;
1735 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1736 if (err)
1737 goto done;
1739 err = got_object_open_as_tree(&tree, repo, tree_id);
1740 if (err)
1741 goto done;
1742 entries = got_object_tree_get_entries(tree);
1743 te = SIMPLEQ_FIRST(&entries->head);
1744 while (te) {
1745 char *id = NULL;
1747 if (sigint_received || sigpipe_received)
1748 break;
1750 if (show_ids) {
1751 char *id_str;
1752 err = got_object_id_str(&id_str, te->id);
1753 if (err)
1754 goto done;
1755 if (asprintf(&id, "%s ", id_str) == -1) {
1756 err = got_error_from_errno("asprintf");
1757 free(id_str);
1758 goto done;
1760 free(id_str);
1762 print_entry(te, id, path, root_path);
1763 free(id);
1765 if (recurse && S_ISDIR(te->mode)) {
1766 char *child_path;
1767 if (asprintf(&child_path, "%s%s%s", path,
1768 path[0] == '/' && path[1] == '\0' ? "" : "/",
1769 te->name) == -1) {
1770 err = got_error_from_errno("asprintf");
1771 goto done;
1773 err = print_tree(child_path, commit_id, show_ids, 1,
1774 root_path, repo);
1775 free(child_path);
1776 if (err)
1777 goto done;
1780 te = SIMPLEQ_NEXT(te, entry);
1782 done:
1783 if (tree)
1784 got_object_tree_close(tree);
1785 free(tree_id);
1786 return err;
1789 static const struct got_error *
1790 cmd_tree(int argc, char *argv[])
1792 const struct got_error *error;
1793 struct got_repository *repo = NULL;
1794 struct got_worktree *worktree = NULL;
1795 const char *path;
1796 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1797 struct got_object_id *commit_id = NULL;
1798 char *commit_id_str = NULL;
1799 int show_ids = 0, recurse = 0;
1800 int ch;
1802 #ifndef PROFILE
1803 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1804 NULL) == -1)
1805 err(1, "pledge");
1806 #endif
1808 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1809 switch (ch) {
1810 case 'c':
1811 commit_id_str = optarg;
1812 break;
1813 case 'r':
1814 repo_path = realpath(optarg, NULL);
1815 if (repo_path == NULL)
1816 err(1, "-r option");
1817 got_path_strip_trailing_slashes(repo_path);
1818 break;
1819 case 'i':
1820 show_ids = 1;
1821 break;
1822 case 'R':
1823 recurse = 1;
1824 break;
1825 default:
1826 usage_tree();
1827 /* NOTREACHED */
1831 argc -= optind;
1832 argv += optind;
1834 if (argc == 1)
1835 path = argv[0];
1836 else if (argc > 1)
1837 usage_tree();
1838 else
1839 path = NULL;
1841 cwd = getcwd(NULL, 0);
1842 if (cwd == NULL) {
1843 error = got_error_from_errno("getcwd");
1844 goto done;
1846 if (repo_path == NULL) {
1847 error = got_worktree_open(&worktree, cwd);
1848 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1849 goto done;
1850 else
1851 error = NULL;
1852 if (worktree) {
1853 repo_path =
1854 strdup(got_worktree_get_repo_path(worktree));
1855 if (repo_path == NULL)
1856 error = got_error_from_errno("strdup");
1857 if (error)
1858 goto done;
1859 } else {
1860 repo_path = strdup(cwd);
1861 if (repo_path == NULL) {
1862 error = got_error_from_errno("strdup");
1863 goto done;
1868 error = got_repo_open(&repo, repo_path);
1869 if (error != NULL)
1870 goto done;
1872 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1873 if (error)
1874 goto done;
1876 if (path == NULL) {
1877 if (worktree) {
1878 char *p, *worktree_subdir = cwd +
1879 strlen(got_worktree_get_root_path(worktree));
1880 if (asprintf(&p, "%s/%s",
1881 got_worktree_get_path_prefix(worktree),
1882 worktree_subdir) == -1) {
1883 error = got_error_from_errno("asprintf");
1884 goto done;
1886 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1887 free(p);
1888 if (error)
1889 goto done;
1890 } else
1891 path = "/";
1893 if (in_repo_path == NULL) {
1894 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1895 if (error != NULL)
1896 goto done;
1899 if (commit_id_str == NULL) {
1900 struct got_reference *head_ref;
1901 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1902 if (error != NULL)
1903 goto done;
1904 error = got_ref_resolve(&commit_id, repo, head_ref);
1905 got_ref_close(head_ref);
1906 if (error != NULL)
1907 goto done;
1908 } else {
1909 error = got_repo_match_object_id_prefix(&commit_id,
1910 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1911 if (error != NULL)
1912 goto done;
1915 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1916 in_repo_path, repo);
1917 done:
1918 free(in_repo_path);
1919 free(repo_path);
1920 free(cwd);
1921 free(commit_id);
1922 if (worktree)
1923 got_worktree_close(worktree);
1924 if (repo) {
1925 const struct got_error *repo_error;
1926 repo_error = got_repo_close(repo);
1927 if (error == NULL)
1928 error = repo_error;
1930 return error;
1933 __dead static void
1934 usage_status(void)
1936 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1937 exit(1);
1940 static const struct got_error *
1941 print_status(void *arg, unsigned char status, const char *path,
1942 struct got_object_id *blob_id, struct got_object_id *commit_id)
1944 printf("%c %s\n", status, path);
1945 return NULL;
1948 static const struct got_error *
1949 cmd_status(int argc, char *argv[])
1951 const struct got_error *error = NULL;
1952 struct got_repository *repo = NULL;
1953 struct got_worktree *worktree = NULL;
1954 char *cwd = NULL, *path = NULL;
1955 int ch;
1957 while ((ch = getopt(argc, argv, "")) != -1) {
1958 switch (ch) {
1959 default:
1960 usage_status();
1961 /* NOTREACHED */
1965 argc -= optind;
1966 argv += optind;
1968 #ifndef PROFILE
1969 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1970 NULL) == -1)
1971 err(1, "pledge");
1972 #endif
1973 cwd = getcwd(NULL, 0);
1974 if (cwd == NULL) {
1975 error = got_error_from_errno("getcwd");
1976 goto done;
1979 error = got_worktree_open(&worktree, cwd);
1980 if (error != NULL)
1981 goto done;
1983 if (argc == 0) {
1984 path = strdup("");
1985 if (path == NULL) {
1986 error = got_error_from_errno("strdup");
1987 goto done;
1989 } else if (argc == 1) {
1990 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1991 if (error)
1992 goto done;
1993 } else
1994 usage_status();
1996 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1997 if (error != NULL)
1998 goto done;
2000 error = apply_unveil(got_repo_get_path(repo), 1,
2001 got_worktree_get_root_path(worktree), 0);
2002 if (error)
2003 goto done;
2005 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2006 check_cancelled, NULL);
2007 done:
2008 free(cwd);
2009 free(path);
2010 return error;
2013 __dead static void
2014 usage_ref(void)
2016 fprintf(stderr,
2017 "usage: %s ref [-r repository] -l | -d name | name target\n",
2018 getprogname());
2019 exit(1);
2022 static const struct got_error *
2023 list_refs(struct got_repository *repo)
2025 static const struct got_error *err = NULL;
2026 struct got_reflist_head refs;
2027 struct got_reflist_entry *re;
2029 SIMPLEQ_INIT(&refs);
2030 err = got_ref_list(&refs, repo);
2031 if (err)
2032 return err;
2034 SIMPLEQ_FOREACH(re, &refs, entry) {
2035 char *refstr;
2036 refstr = got_ref_to_str(re->ref);
2037 if (refstr == NULL)
2038 return got_error_from_errno("got_ref_to_str");
2039 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2040 free(refstr);
2043 got_ref_list_free(&refs);
2044 return NULL;
2047 static const struct got_error *
2048 delete_ref(struct got_repository *repo, const char *refname)
2050 const struct got_error *err = NULL;
2051 struct got_reference *ref;
2053 err = got_ref_open(&ref, repo, refname, 0);
2054 if (err)
2055 return err;
2057 err = got_ref_delete(ref, repo);
2058 got_ref_close(ref);
2059 return err;
2062 static const struct got_error *
2063 add_ref(struct got_repository *repo, const char *refname, const char *target)
2065 const struct got_error *err = NULL;
2066 struct got_object_id *id;
2067 struct got_reference *ref = NULL;
2069 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2070 repo);
2071 if (err) {
2072 struct got_reference *target_ref;
2074 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2075 return err;
2076 err = got_ref_open(&target_ref, repo, target, 0);
2077 if (err)
2078 return err;
2079 err = got_ref_resolve(&id, repo, target_ref);
2080 got_ref_close(target_ref);
2081 if (err)
2082 return err;
2085 err = got_ref_alloc(&ref, refname, id);
2086 if (err)
2087 goto done;
2089 err = got_ref_write(ref, repo);
2090 done:
2091 if (ref)
2092 got_ref_close(ref);
2093 free(id);
2094 return err;
2097 static const struct got_error *
2098 cmd_ref(int argc, char *argv[])
2100 const struct got_error *error = NULL;
2101 struct got_repository *repo = NULL;
2102 struct got_worktree *worktree = NULL;
2103 char *cwd = NULL, *repo_path = NULL;
2104 int ch, do_list = 0;
2105 const char *delref = NULL;
2107 /* TODO: Add -s option for adding symbolic references. */
2108 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2109 switch (ch) {
2110 case 'd':
2111 delref = optarg;
2112 break;
2113 case 'r':
2114 repo_path = realpath(optarg, NULL);
2115 if (repo_path == NULL)
2116 err(1, "-r option");
2117 got_path_strip_trailing_slashes(repo_path);
2118 break;
2119 case 'l':
2120 do_list = 1;
2121 break;
2122 default:
2123 usage_ref();
2124 /* NOTREACHED */
2128 if (do_list && delref)
2129 errx(1, "-l and -d options are mutually exclusive\n");
2131 argc -= optind;
2132 argv += optind;
2134 if (do_list || delref) {
2135 if (argc > 0)
2136 usage_ref();
2137 } else if (argc != 2)
2138 usage_ref();
2140 #ifndef PROFILE
2141 if (do_list) {
2142 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2143 NULL) == -1)
2144 err(1, "pledge");
2145 } else {
2146 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2147 "sendfd unveil", NULL) == -1)
2148 err(1, "pledge");
2150 #endif
2151 cwd = getcwd(NULL, 0);
2152 if (cwd == NULL) {
2153 error = got_error_from_errno("getcwd");
2154 goto done;
2157 if (repo_path == NULL) {
2158 error = got_worktree_open(&worktree, cwd);
2159 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2160 goto done;
2161 else
2162 error = NULL;
2163 if (worktree) {
2164 repo_path =
2165 strdup(got_worktree_get_repo_path(worktree));
2166 if (repo_path == NULL)
2167 error = got_error_from_errno("strdup");
2168 if (error)
2169 goto done;
2170 } else {
2171 repo_path = strdup(cwd);
2172 if (repo_path == NULL) {
2173 error = got_error_from_errno("strdup");
2174 goto done;
2179 error = got_repo_open(&repo, repo_path);
2180 if (error != NULL)
2181 goto done;
2183 error = apply_unveil(got_repo_get_path(repo), do_list,
2184 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2185 if (error)
2186 goto done;
2188 if (do_list)
2189 error = list_refs(repo);
2190 else if (delref)
2191 error = delete_ref(repo, delref);
2192 else
2193 error = add_ref(repo, argv[0], argv[1]);
2194 done:
2195 if (repo)
2196 got_repo_close(repo);
2197 if (worktree)
2198 got_worktree_close(worktree);
2199 free(cwd);
2200 free(repo_path);
2201 return error;
2204 __dead static void
2205 usage_branch(void)
2207 fprintf(stderr,
2208 "usage: %s branch [-r repository] -l | -d name | "
2209 "name [base-branch]\n", getprogname());
2210 exit(1);
2213 static const struct got_error *
2214 list_branches(struct got_repository *repo)
2216 static const struct got_error *err = NULL;
2217 struct got_reflist_head refs;
2218 struct got_reflist_entry *re;
2220 SIMPLEQ_INIT(&refs);
2221 err = got_ref_list(&refs, repo);
2222 if (err)
2223 return err;
2225 SIMPLEQ_FOREACH(re, &refs, entry) {
2226 const char *refname;
2227 char *refstr;
2228 refname = got_ref_get_name(re->ref);
2229 if (strncmp(refname, "refs/heads/", 11) != 0)
2230 continue;
2231 refname += 11;
2232 refstr = got_ref_to_str(re->ref);
2233 if (refstr == NULL)
2234 return got_error_from_errno("got_ref_to_str");
2235 printf("%s: %s\n", refname, refstr);
2236 free(refstr);
2239 got_ref_list_free(&refs);
2240 return NULL;
2243 static const struct got_error *
2244 delete_branch(struct got_repository *repo, const char *branch_name)
2246 const struct got_error *err = NULL;
2247 struct got_reference *ref;
2248 char *refname;
2250 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2251 return got_error_from_errno("asprintf");
2253 err = got_ref_open(&ref, repo, refname, 0);
2254 if (err)
2255 goto done;
2257 err = got_ref_delete(ref, repo);
2258 got_ref_close(ref);
2259 done:
2260 free(refname);
2261 return err;
2264 static const struct got_error *
2265 add_branch(struct got_repository *repo, const char *branch_name,
2266 const char *base_branch)
2268 const struct got_error *err = NULL;
2269 struct got_object_id *id = NULL;
2270 struct got_reference *ref = NULL;
2271 char *base_refname = NULL, *refname = NULL;
2272 struct got_reference *base_ref;
2274 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2275 base_refname = strdup(GOT_REF_HEAD);
2276 if (base_refname == NULL)
2277 return got_error_from_errno("strdup");
2278 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2279 return got_error_from_errno("asprintf");
2281 err = got_ref_open(&base_ref, repo, base_refname, 0);
2282 if (err)
2283 goto done;
2284 err = got_ref_resolve(&id, repo, base_ref);
2285 got_ref_close(base_ref);
2286 if (err)
2287 goto done;
2289 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2290 err = got_error_from_errno("asprintf");
2291 goto done;
2294 err = got_ref_open(&ref, repo, refname, 0);
2295 if (err == NULL) {
2296 err = got_error(GOT_ERR_BRANCH_EXISTS);
2297 goto done;
2298 } else if (err->code != GOT_ERR_NOT_REF)
2299 goto done;
2301 err = got_ref_alloc(&ref, refname, id);
2302 if (err)
2303 goto done;
2305 err = got_ref_write(ref, repo);
2306 done:
2307 if (ref)
2308 got_ref_close(ref);
2309 free(id);
2310 free(base_refname);
2311 free(refname);
2312 return err;
2315 static const struct got_error *
2316 cmd_branch(int argc, char *argv[])
2318 const struct got_error *error = NULL;
2319 struct got_repository *repo = NULL;
2320 struct got_worktree *worktree = NULL;
2321 char *cwd = NULL, *repo_path = NULL;
2322 int ch, do_list = 0;
2323 const char *delref = NULL;
2325 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2326 switch (ch) {
2327 case 'd':
2328 delref = optarg;
2329 break;
2330 case 'r':
2331 repo_path = realpath(optarg, NULL);
2332 if (repo_path == NULL)
2333 err(1, "-r option");
2334 got_path_strip_trailing_slashes(repo_path);
2335 break;
2336 case 'l':
2337 do_list = 1;
2338 break;
2339 default:
2340 usage_branch();
2341 /* NOTREACHED */
2345 if (do_list && delref)
2346 errx(1, "-l and -d options are mutually exclusive\n");
2348 argc -= optind;
2349 argv += optind;
2351 if (do_list || delref) {
2352 if (argc > 0)
2353 usage_branch();
2354 } else if (argc < 1 || argc > 2)
2355 usage_branch();
2357 #ifndef PROFILE
2358 if (do_list) {
2359 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2360 NULL) == -1)
2361 err(1, "pledge");
2362 } else {
2363 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2364 "sendfd unveil", NULL) == -1)
2365 err(1, "pledge");
2367 #endif
2368 cwd = getcwd(NULL, 0);
2369 if (cwd == NULL) {
2370 error = got_error_from_errno("getcwd");
2371 goto done;
2374 if (repo_path == NULL) {
2375 error = got_worktree_open(&worktree, cwd);
2376 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2377 goto done;
2378 else
2379 error = NULL;
2380 if (worktree) {
2381 repo_path =
2382 strdup(got_worktree_get_repo_path(worktree));
2383 if (repo_path == NULL)
2384 error = got_error_from_errno("strdup");
2385 if (error)
2386 goto done;
2387 } else {
2388 repo_path = strdup(cwd);
2389 if (repo_path == NULL) {
2390 error = got_error_from_errno("strdup");
2391 goto done;
2396 error = got_repo_open(&repo, repo_path);
2397 if (error != NULL)
2398 goto done;
2400 error = apply_unveil(got_repo_get_path(repo), do_list,
2401 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2402 if (error)
2403 goto done;
2405 if (do_list)
2406 error = list_branches(repo);
2407 else if (delref)
2408 error = delete_branch(repo, delref);
2409 else {
2410 const char *base_branch;
2411 if (argc == 1) {
2412 base_branch = worktree ?
2413 got_worktree_get_head_ref_name(worktree) :
2414 GOT_REF_HEAD;
2415 } else
2416 base_branch = argv[1];
2417 error = add_branch(repo, argv[0], base_branch);
2419 done:
2420 if (repo)
2421 got_repo_close(repo);
2422 if (worktree)
2423 got_worktree_close(worktree);
2424 free(cwd);
2425 free(repo_path);
2426 return error;
2429 __dead static void
2430 usage_add(void)
2432 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2433 exit(1);
2436 static const struct got_error *
2437 cmd_add(int argc, char *argv[])
2439 const struct got_error *error = NULL;
2440 struct got_repository *repo = NULL;
2441 struct got_worktree *worktree = NULL;
2442 char *cwd = NULL;
2443 struct got_pathlist_head paths;
2444 struct got_pathlist_entry *pe;
2445 int ch, x;
2447 TAILQ_INIT(&paths);
2449 while ((ch = getopt(argc, argv, "")) != -1) {
2450 switch (ch) {
2451 default:
2452 usage_add();
2453 /* NOTREACHED */
2457 argc -= optind;
2458 argv += optind;
2460 if (argc < 1)
2461 usage_add();
2463 /* make sure each file exists before doing anything halfway */
2464 for (x = 0; x < argc; x++) {
2465 char *path = realpath(argv[x], NULL);
2466 if (path == NULL) {
2467 error = got_error_from_errno2("realpath", argv[x]);
2468 goto done;
2470 free(path);
2473 cwd = getcwd(NULL, 0);
2474 if (cwd == NULL) {
2475 error = got_error_from_errno("getcwd");
2476 goto done;
2479 error = got_worktree_open(&worktree, cwd);
2480 if (error)
2481 goto done;
2483 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2484 if (error != NULL)
2485 goto done;
2487 error = apply_unveil(got_repo_get_path(repo), 1,
2488 got_worktree_get_root_path(worktree), 0);
2489 if (error)
2490 goto done;
2492 for (x = 0; x < argc; x++) {
2493 char *path = realpath(argv[x], NULL);
2494 if (path == NULL) {
2495 error = got_error_from_errno2("realpath", argv[x]);
2496 goto done;
2499 got_path_strip_trailing_slashes(path);
2500 error = got_pathlist_insert(&pe, &paths, path, NULL);
2501 if (error) {
2502 free(path);
2503 goto done;
2506 error = got_worktree_schedule_add(worktree, &paths, print_status,
2507 NULL, repo);
2508 done:
2509 if (repo)
2510 got_repo_close(repo);
2511 if (worktree)
2512 got_worktree_close(worktree);
2513 TAILQ_FOREACH(pe, &paths, entry)
2514 free((char *)pe->path);
2515 got_pathlist_free(&paths);
2516 free(cwd);
2517 return error;
2520 __dead static void
2521 usage_remove(void)
2523 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2524 exit(1);
2527 static const struct got_error *
2528 cmd_remove(int argc, char *argv[])
2530 const struct got_error *error = NULL;
2531 struct got_worktree *worktree = NULL;
2532 struct got_repository *repo = NULL;
2533 char *cwd = NULL;
2534 struct got_pathlist_head paths;
2535 struct got_pathlist_entry *pe;
2536 int ch, i, delete_local_mods = 0;
2538 TAILQ_INIT(&paths);
2540 while ((ch = getopt(argc, argv, "f")) != -1) {
2541 switch (ch) {
2542 case 'f':
2543 delete_local_mods = 1;
2544 break;
2545 default:
2546 usage_add();
2547 /* NOTREACHED */
2551 argc -= optind;
2552 argv += optind;
2554 if (argc < 1)
2555 usage_remove();
2557 /* make sure each file exists before doing anything halfway */
2558 for (i = 0; i < argc; i++) {
2559 char *path = realpath(argv[i], NULL);
2560 if (path == NULL) {
2561 error = got_error_from_errno2("realpath", argv[i]);
2562 goto done;
2564 free(path);
2567 cwd = getcwd(NULL, 0);
2568 if (cwd == NULL) {
2569 error = got_error_from_errno("getcwd");
2570 goto done;
2572 error = got_worktree_open(&worktree, cwd);
2573 if (error)
2574 goto done;
2576 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2577 if (error)
2578 goto done;
2580 error = apply_unveil(got_repo_get_path(repo), 1,
2581 got_worktree_get_root_path(worktree), 0);
2582 if (error)
2583 goto done;
2585 for (i = 0; i < argc; i++) {
2586 char *path = realpath(argv[i], NULL);
2587 if (path == NULL) {
2588 error = got_error_from_errno2("realpath", argv[i]);
2589 goto done;
2592 got_path_strip_trailing_slashes(path);
2593 error = got_pathlist_insert(&pe, &paths, path, NULL);
2594 if (error) {
2595 free(path);
2596 goto done;
2599 error = got_worktree_schedule_delete(worktree, &paths,
2600 delete_local_mods, print_status, NULL, repo);
2601 if (error)
2602 goto done;
2603 done:
2604 if (repo)
2605 got_repo_close(repo);
2606 if (worktree)
2607 got_worktree_close(worktree);
2608 TAILQ_FOREACH(pe, &paths, entry)
2609 free((char *)pe->path);
2610 got_pathlist_free(&paths);
2611 free(cwd);
2612 return error;
2615 __dead static void
2616 usage_revert(void)
2618 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2619 exit(1);
2622 static void
2623 revert_progress(void *arg, unsigned char status, const char *path)
2625 while (path[0] == '/')
2626 path++;
2627 printf("%c %s\n", status, path);
2630 static const struct got_error *
2631 cmd_revert(int argc, char *argv[])
2633 const struct got_error *error = NULL;
2634 struct got_worktree *worktree = NULL;
2635 struct got_repository *repo = NULL;
2636 char *cwd = NULL, *path = NULL;
2637 struct got_pathlist_head paths;
2638 struct got_pathlist_entry *pe;
2639 int ch, i;
2641 TAILQ_INIT(&paths);
2643 while ((ch = getopt(argc, argv, "")) != -1) {
2644 switch (ch) {
2645 default:
2646 usage_revert();
2647 /* NOTREACHED */
2651 argc -= optind;
2652 argv += optind;
2654 if (argc < 1)
2655 usage_revert();
2657 for (i = 0; i < argc; i++) {
2658 char *path = realpath(argv[i], NULL);
2659 if (path == NULL) {
2660 error = got_error_from_errno2("realpath", argv[i]);
2661 goto done;
2664 got_path_strip_trailing_slashes(path);
2665 error = got_pathlist_insert(&pe, &paths, path, NULL);
2666 if (error) {
2667 free(path);
2668 goto done;
2672 cwd = getcwd(NULL, 0);
2673 if (cwd == NULL) {
2674 error = got_error_from_errno("getcwd");
2675 goto done;
2677 error = got_worktree_open(&worktree, cwd);
2678 if (error)
2679 goto done;
2681 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2682 if (error != NULL)
2683 goto done;
2685 error = apply_unveil(got_repo_get_path(repo), 1,
2686 got_worktree_get_root_path(worktree), 0);
2687 if (error)
2688 goto done;
2690 error = got_worktree_revert(worktree, &paths,
2691 revert_progress, NULL, repo);
2692 if (error)
2693 goto done;
2694 done:
2695 if (repo)
2696 got_repo_close(repo);
2697 if (worktree)
2698 got_worktree_close(worktree);
2699 free(path);
2700 free(cwd);
2701 return error;
2704 __dead static void
2705 usage_commit(void)
2707 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2708 exit(1);
2711 int
2712 spawn_editor(const char *editor, const char *file)
2714 pid_t pid;
2715 sig_t sighup, sigint, sigquit;
2716 int st = -1;
2718 sighup = signal(SIGHUP, SIG_IGN);
2719 sigint = signal(SIGINT, SIG_IGN);
2720 sigquit = signal(SIGQUIT, SIG_IGN);
2722 switch (pid = fork()) {
2723 case -1:
2724 goto doneediting;
2725 case 0:
2726 execl(editor, editor, file, (char *)NULL);
2727 _exit(127);
2730 while (waitpid(pid, &st, 0) == -1)
2731 if (errno != EINTR)
2732 break;
2734 doneediting:
2735 (void)signal(SIGHUP, sighup);
2736 (void)signal(SIGINT, sigint);
2737 (void)signal(SIGQUIT, sigquit);
2739 if (!WIFEXITED(st)) {
2740 errno = EINTR;
2741 return -1;
2744 return WEXITSTATUS(st);
2747 struct collect_commit_logmsg_arg {
2748 const char *cmdline_log;
2749 const char *editor;
2750 const char *worktree_path;
2751 const char *branch_name;
2752 const char *repo_path;
2753 char *logmsg_path;
2757 static const struct got_error *
2758 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2759 void *arg)
2761 char *initial_content = NULL;
2762 struct got_pathlist_entry *pe;
2763 const struct got_error *err = NULL;
2764 char *template = NULL;
2765 struct collect_commit_logmsg_arg *a = arg;
2766 char buf[1024];
2767 struct stat st, st2;
2768 FILE *fp;
2769 size_t len;
2770 int fd, content_changed = 0;
2772 /* if a message was specified on the command line, just use it */
2773 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2774 len = strlen(a->cmdline_log) + 1;
2775 *logmsg = malloc(len + 1);
2776 if (*logmsg == NULL)
2777 return got_error_from_errno("malloc");
2778 strlcpy(*logmsg, a->cmdline_log, len);
2779 return NULL;
2782 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2783 return got_error_from_errno("asprintf");
2785 if (asprintf(&initial_content,
2786 "\n# changes to be committed on branch %s:\n",
2787 a->branch_name) == -1)
2788 return got_error_from_errno("asprintf");
2790 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2791 if (err)
2792 goto done;
2794 dprintf(fd, initial_content);
2796 TAILQ_FOREACH(pe, commitable_paths, entry) {
2797 struct got_commitable *ct = pe->data;
2798 dprintf(fd, "# %c %s\n",
2799 got_commitable_get_status(ct),
2800 got_commitable_get_path(ct));
2802 close(fd);
2804 if (stat(a->logmsg_path, &st) == -1) {
2805 err = got_error_from_errno2("stat", a->logmsg_path);
2806 goto done;
2809 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2810 err = got_error_from_errno("failed spawning editor");
2811 goto done;
2814 if (stat(a->logmsg_path, &st2) == -1) {
2815 err = got_error_from_errno("stat");
2816 goto done;
2819 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2820 unlink(a->logmsg_path);
2821 free(a->logmsg_path);
2822 a->logmsg_path = NULL;
2823 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2824 "no changes made to commit message, aborting");
2825 goto done;
2828 *logmsg = malloc(st2.st_size + 1);
2829 if (*logmsg == NULL) {
2830 err = got_error_from_errno("malloc");
2831 goto done;
2833 (*logmsg)[0] = '\0';
2834 len = 0;
2836 fp = fopen(a->logmsg_path, "r");
2837 if (fp == NULL) {
2838 err = got_error_from_errno("fopen");
2839 goto done;
2841 while (fgets(buf, sizeof(buf), fp) != NULL) {
2842 if (!content_changed && strcmp(buf, initial_content) != 0)
2843 content_changed = 1;
2844 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2845 continue; /* remove comments and leading empty lines */
2846 len = strlcat(*logmsg, buf, st2.st_size);
2848 fclose(fp);
2850 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2851 (*logmsg)[len - 1] = '\0';
2852 len--;
2855 if (len == 0 || !content_changed) {
2856 unlink(a->logmsg_path);
2857 free(a->logmsg_path);
2858 a->logmsg_path = NULL;
2859 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2860 "commit message cannot be empty, aborting");
2861 goto done;
2863 done:
2864 free(initial_content);
2865 free(template);
2867 /* Editor is done; we can now apply unveil(2) */
2868 if (err == NULL)
2869 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2870 return err;
2873 static const struct got_error *
2874 cmd_commit(int argc, char *argv[])
2876 const struct got_error *error = NULL;
2877 struct got_worktree *worktree = NULL;
2878 struct got_repository *repo = NULL;
2879 char *cwd = NULL, *path = NULL, *id_str = NULL;
2880 struct got_object_id *id = NULL;
2881 const char *logmsg = NULL;
2882 const char *got_author = getenv("GOT_AUTHOR");
2883 struct collect_commit_logmsg_arg cl_arg;
2884 char *editor = NULL;
2885 int ch, rebase_in_progress;
2887 while ((ch = getopt(argc, argv, "m:")) != -1) {
2888 switch (ch) {
2889 case 'm':
2890 logmsg = optarg;
2891 break;
2892 default:
2893 usage_commit();
2894 /* NOTREACHED */
2898 argc -= optind;
2899 argv += optind;
2901 if (argc == 1) {
2902 path = realpath(argv[0], NULL);
2903 if (path == NULL) {
2904 error = got_error_from_errno2("realpath", argv[0]);
2905 goto done;
2907 got_path_strip_trailing_slashes(path);
2908 } else if (argc != 0)
2909 usage_commit();
2911 if (got_author == NULL) {
2912 /* TODO: Look current user up in password database */
2913 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2914 goto done;
2917 cwd = getcwd(NULL, 0);
2918 if (cwd == NULL) {
2919 error = got_error_from_errno("getcwd");
2920 goto done;
2922 error = got_worktree_open(&worktree, cwd);
2923 if (error)
2924 goto done;
2926 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2927 if (error)
2928 goto done;
2929 if (rebase_in_progress) {
2930 error = got_error(GOT_ERR_REBASING);
2931 goto done;
2934 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2935 if (error != NULL)
2936 goto done;
2939 * unveil(2) traverses exec(2); if an editor is used we have
2940 * to apply unveil after the log message has been written.
2942 if (logmsg == NULL || strlen(logmsg) == 0)
2943 error = get_editor(&editor);
2944 else
2945 error = apply_unveil(got_repo_get_path(repo), 0,
2946 got_worktree_get_root_path(worktree), 0);
2947 if (error)
2948 goto done;
2950 cl_arg.editor = editor;
2951 cl_arg.cmdline_log = logmsg;
2952 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2953 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2954 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2955 cl_arg.branch_name += 5;
2956 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2957 cl_arg.branch_name += 6;
2958 cl_arg.repo_path = got_repo_get_path(repo);
2959 cl_arg.logmsg_path = NULL;
2960 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2961 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2962 if (error) {
2963 if (cl_arg.logmsg_path)
2964 fprintf(stderr, "%s: log message preserved in %s\n",
2965 getprogname(), cl_arg.logmsg_path);
2966 goto done;
2969 if (cl_arg.logmsg_path)
2970 unlink(cl_arg.logmsg_path);
2972 error = got_object_id_str(&id_str, id);
2973 if (error)
2974 goto done;
2975 printf("Created commit %s\n", id_str);
2976 done:
2977 if (repo)
2978 got_repo_close(repo);
2979 if (worktree)
2980 got_worktree_close(worktree);
2981 free(path);
2982 free(cwd);
2983 free(id_str);
2984 free(editor);
2985 return error;
2988 __dead static void
2989 usage_cherrypick(void)
2991 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2992 exit(1);
2995 static const struct got_error *
2996 cmd_cherrypick(int argc, char *argv[])
2998 const struct got_error *error = NULL;
2999 struct got_worktree *worktree = NULL;
3000 struct got_repository *repo = NULL;
3001 char *cwd = NULL, *commit_id_str = NULL;
3002 struct got_object_id *commit_id = NULL;
3003 struct got_commit_object *commit = NULL;
3004 struct got_object_qid *pid;
3005 struct got_reference *head_ref = NULL;
3006 int ch, did_something = 0;
3008 while ((ch = getopt(argc, argv, "")) != -1) {
3009 switch (ch) {
3010 default:
3011 usage_cherrypick();
3012 /* NOTREACHED */
3016 argc -= optind;
3017 argv += optind;
3019 if (argc != 1)
3020 usage_cherrypick();
3022 cwd = getcwd(NULL, 0);
3023 if (cwd == NULL) {
3024 error = got_error_from_errno("getcwd");
3025 goto done;
3027 error = got_worktree_open(&worktree, cwd);
3028 if (error)
3029 goto done;
3031 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3032 if (error != NULL)
3033 goto done;
3035 error = apply_unveil(got_repo_get_path(repo), 0,
3036 got_worktree_get_root_path(worktree), 0);
3037 if (error)
3038 goto done;
3040 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3041 GOT_OBJ_TYPE_COMMIT, repo);
3042 if (error != NULL) {
3043 struct got_reference *ref;
3044 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3045 goto done;
3046 error = got_ref_open(&ref, repo, argv[0], 0);
3047 if (error != NULL)
3048 goto done;
3049 error = got_ref_resolve(&commit_id, repo, ref);
3050 got_ref_close(ref);
3051 if (error != NULL)
3052 goto done;
3054 error = got_object_id_str(&commit_id_str, commit_id);
3055 if (error)
3056 goto done;
3058 error = got_ref_open(&head_ref, repo,
3059 got_worktree_get_head_ref_name(worktree), 0);
3060 if (error != NULL)
3061 goto done;
3063 error = check_same_branch(commit_id, head_ref, repo);
3064 if (error) {
3065 if (error->code != GOT_ERR_ANCESTRY)
3066 goto done;
3067 error = NULL;
3068 } else {
3069 error = got_error(GOT_ERR_SAME_BRANCH);
3070 goto done;
3073 error = got_object_open_as_commit(&commit, repo, commit_id);
3074 if (error)
3075 goto done;
3076 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3077 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3078 commit_id, repo, update_progress, &did_something, check_cancelled,
3079 NULL);
3080 if (error != NULL)
3081 goto done;
3083 if (did_something)
3084 printf("Merged commit %s\n", commit_id_str);
3085 done:
3086 if (commit)
3087 got_object_commit_close(commit);
3088 free(commit_id_str);
3089 if (head_ref)
3090 got_ref_close(head_ref);
3091 if (worktree)
3092 got_worktree_close(worktree);
3093 if (repo)
3094 got_repo_close(repo);
3095 return error;
3098 __dead static void
3099 usage_backout(void)
3101 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3102 exit(1);
3105 static const struct got_error *
3106 cmd_backout(int argc, char *argv[])
3108 const struct got_error *error = NULL;
3109 struct got_worktree *worktree = NULL;
3110 struct got_repository *repo = NULL;
3111 char *cwd = NULL, *commit_id_str = NULL;
3112 struct got_object_id *commit_id = NULL;
3113 struct got_commit_object *commit = NULL;
3114 struct got_object_qid *pid;
3115 struct got_reference *head_ref = NULL;
3116 int ch, did_something = 0;
3118 while ((ch = getopt(argc, argv, "")) != -1) {
3119 switch (ch) {
3120 default:
3121 usage_backout();
3122 /* NOTREACHED */
3126 argc -= optind;
3127 argv += optind;
3129 if (argc != 1)
3130 usage_backout();
3132 cwd = getcwd(NULL, 0);
3133 if (cwd == NULL) {
3134 error = got_error_from_errno("getcwd");
3135 goto done;
3137 error = got_worktree_open(&worktree, cwd);
3138 if (error)
3139 goto done;
3141 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3142 if (error != NULL)
3143 goto done;
3145 error = apply_unveil(got_repo_get_path(repo), 0,
3146 got_worktree_get_root_path(worktree), 0);
3147 if (error)
3148 goto done;
3150 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3151 GOT_OBJ_TYPE_COMMIT, repo);
3152 if (error != NULL) {
3153 struct got_reference *ref;
3154 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3155 goto done;
3156 error = got_ref_open(&ref, repo, argv[0], 0);
3157 if (error != NULL)
3158 goto done;
3159 error = got_ref_resolve(&commit_id, repo, ref);
3160 got_ref_close(ref);
3161 if (error != NULL)
3162 goto done;
3164 error = got_object_id_str(&commit_id_str, commit_id);
3165 if (error)
3166 goto done;
3168 error = got_ref_open(&head_ref, repo,
3169 got_worktree_get_head_ref_name(worktree), 0);
3170 if (error != NULL)
3171 goto done;
3173 error = check_same_branch(commit_id, head_ref, repo);
3174 if (error)
3175 goto done;
3177 error = got_object_open_as_commit(&commit, repo, commit_id);
3178 if (error)
3179 goto done;
3180 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3181 if (pid == NULL) {
3182 error = got_error(GOT_ERR_ROOT_COMMIT);
3183 goto done;
3186 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3187 update_progress, &did_something, check_cancelled, NULL);
3188 if (error != NULL)
3189 goto done;
3191 if (did_something)
3192 printf("Backed out commit %s\n", commit_id_str);
3193 done:
3194 if (commit)
3195 got_object_commit_close(commit);
3196 free(commit_id_str);
3197 if (head_ref)
3198 got_ref_close(head_ref);
3199 if (worktree)
3200 got_worktree_close(worktree);
3201 if (repo)
3202 got_repo_close(repo);
3203 return error;
3206 __dead static void
3207 usage_rebase(void)
3209 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3210 getprogname());
3211 exit(1);
3214 static const struct got_error *
3215 show_rebase_progress(struct got_commit_object *commit,
3216 struct got_object_id *old_id, struct got_object_id *new_id)
3218 const struct got_error *err;
3219 char *old_id_str = NULL, *new_id_str = NULL;
3220 char *logmsg0 = NULL, *logmsg, *nl;
3221 size_t len;
3223 err = got_object_id_str(&old_id_str, old_id);
3224 if (err)
3225 goto done;
3227 if (new_id) {
3228 err = got_object_id_str(&new_id_str, new_id);
3229 if (err)
3230 goto done;
3233 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3234 if (logmsg0 == NULL) {
3235 err = got_error_from_errno("strdup");
3236 goto done;
3238 logmsg = logmsg0;
3240 while (isspace(logmsg[0]))
3241 logmsg++;
3243 old_id_str[12] = '\0';
3244 if (new_id_str)
3245 new_id_str[12] = '\0';
3246 len = strlen(logmsg);
3247 if (len > 42)
3248 len = 42;
3249 logmsg[len] = '\0';
3250 nl = strchr(logmsg, '\n');
3251 if (nl)
3252 *nl = '\0';
3253 printf("%s -> %s: %s\n", old_id_str,
3254 new_id_str ? new_id_str : "no-op change", logmsg);
3255 done:
3256 free(old_id_str);
3257 free(new_id_str);
3258 free(logmsg0);
3259 return err;
3262 static void
3263 rebase_progress(void *arg, unsigned char status, const char *path)
3265 unsigned char *rebase_status = arg;
3267 while (path[0] == '/')
3268 path++;
3269 printf("%c %s\n", status, path);
3271 if (*rebase_status == GOT_STATUS_CONFLICT)
3272 return;
3273 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3274 *rebase_status = status;
3277 static const struct got_error *
3278 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3279 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3280 struct got_repository *repo)
3282 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3283 return got_worktree_rebase_complete(worktree,
3284 new_base_branch, tmp_branch, branch, repo);
3287 static const struct got_error *
3288 rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
3289 struct got_object_id *commit_id, struct got_repository *repo)
3291 const struct got_error *error;
3292 struct got_commit_object *commit;
3293 struct got_object_id *new_commit_id;
3295 error = got_object_open_as_commit(&commit, repo, commit_id);
3296 if (error)
3297 return error;
3299 error = got_worktree_rebase_commit(&new_commit_id, worktree,
3300 tmp_branch, commit, commit_id, repo);
3301 if (error) {
3302 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3303 goto done;
3304 error = show_rebase_progress(commit, commit_id, NULL);
3305 } else {
3306 error = show_rebase_progress(commit, commit_id, new_commit_id);
3307 free(new_commit_id);
3309 done:
3310 got_object_commit_close(commit);
3311 return error;
3314 static const struct got_error *
3315 cmd_rebase(int argc, char *argv[])
3317 const struct got_error *error = NULL;
3318 struct got_worktree *worktree = NULL;
3319 struct got_repository *repo = NULL;
3320 char *cwd = NULL;
3321 struct got_reference *branch = NULL;
3322 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3323 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3324 struct got_object_id *resume_commit_id = NULL;
3325 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3326 struct got_commit_graph *graph = NULL;
3327 struct got_commit_object *commit = NULL;
3328 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3329 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3330 struct got_object_id_queue commits;
3331 const struct got_object_id_queue *parent_ids;
3332 struct got_object_qid *qid, *pid;
3334 SIMPLEQ_INIT(&commits);
3336 while ((ch = getopt(argc, argv, "ac")) != -1) {
3337 switch (ch) {
3338 case 'a':
3339 abort_rebase = 1;
3340 break;
3341 case 'c':
3342 continue_rebase = 1;
3343 break;
3344 default:
3345 usage_rebase();
3346 /* NOTREACHED */
3350 argc -= optind;
3351 argv += optind;
3353 if (abort_rebase && continue_rebase)
3354 usage_rebase();
3355 else if (abort_rebase || continue_rebase) {
3356 if (argc != 0)
3357 usage_rebase();
3358 } else if (argc != 1)
3359 usage_rebase();
3361 cwd = getcwd(NULL, 0);
3362 if (cwd == NULL) {
3363 error = got_error_from_errno("getcwd");
3364 goto done;
3366 error = got_worktree_open(&worktree, cwd);
3367 if (error)
3368 goto done;
3370 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3371 if (error != NULL)
3372 goto done;
3374 error = apply_unveil(got_repo_get_path(repo), 0,
3375 got_worktree_get_root_path(worktree), 0);
3376 if (error)
3377 goto done;
3379 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3380 if (error)
3381 goto done;
3383 if (rebase_in_progress && abort_rebase) {
3384 int did_something;
3385 error = got_worktree_rebase_continue(&resume_commit_id,
3386 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3387 if (error)
3388 goto done;
3389 printf("Switching work tree to %s\n",
3390 got_ref_get_symref_target(new_base_branch));
3391 error = got_worktree_rebase_abort(worktree, repo,
3392 new_base_branch, update_progress, &did_something);
3393 if (error)
3394 goto done;
3395 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3396 goto done; /* nothing else to do */
3397 } else if (abort_rebase) {
3398 error = got_error(GOT_ERR_NOT_REBASING);
3399 goto done;
3402 if (continue_rebase) {
3403 error = got_worktree_rebase_continue(&resume_commit_id,
3404 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3405 if (error)
3406 goto done;
3408 error = rebase_commit(worktree, tmp_branch, resume_commit_id,
3409 repo);
3410 if (error)
3411 goto done;
3413 yca_id = got_object_id_dup(resume_commit_id);
3414 if (yca_id == NULL) {
3415 error = got_error_from_errno("got_object_id_dup");
3416 goto done;
3418 } else {
3419 error = got_ref_open(&branch, repo, argv[0], 0);
3420 if (error != NULL)
3421 goto done;
3423 error = check_same_branch(
3424 got_worktree_get_base_commit_id(worktree), branch, repo);
3425 if (error) {
3426 if (error->code != GOT_ERR_ANCESTRY)
3427 goto done;
3428 error = NULL;
3429 } else {
3430 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3431 "specified branch resolves to a commit which "
3432 "is already contained in work tree's branch");
3433 goto done;
3437 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3438 if (error)
3439 goto done;
3441 if (!continue_rebase) {
3442 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3443 got_worktree_get_base_commit_id(worktree),
3444 branch_head_commit_id, repo);
3445 if (error)
3446 goto done;
3447 if (yca_id == NULL) {
3448 error = got_error_msg(GOT_ERR_ANCESTRY,
3449 "specified branch shares no common ancestry "
3450 "with work tree's branch");
3451 goto done;
3454 error = got_worktree_rebase_prepare(&new_base_branch,
3455 &tmp_branch, worktree, branch, repo);
3456 if (error)
3457 goto done;
3460 commit_id = branch_head_commit_id;
3461 error = got_object_open_as_commit(&commit, repo, commit_id);
3462 if (error)
3463 goto done;
3465 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3466 if (error)
3467 goto done;
3468 parent_ids = got_object_commit_get_parent_ids(commit);
3469 pid = SIMPLEQ_FIRST(parent_ids);
3470 error = got_commit_graph_iter_start(graph, pid->id, repo);
3471 got_object_commit_close(commit);
3472 commit = NULL;
3473 if (error)
3474 goto done;
3475 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3476 error = got_commit_graph_iter_next(&parent_id, graph);
3477 if (error) {
3478 if (error->code == GOT_ERR_ITER_COMPLETED) {
3479 error = got_error_msg(GOT_ERR_ANCESTRY,
3480 "ran out of commits to rebase before "
3481 "youngest common ancestor commit has "
3482 "been reached?!?");
3483 goto done;
3484 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3485 goto done;
3486 error = got_commit_graph_fetch_commits(graph, 1, repo);
3487 if (error)
3488 goto done;
3489 } else {
3490 error = got_object_qid_alloc(&qid, commit_id);
3491 if (error)
3492 goto done;
3493 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3494 commit_id = parent_id;
3498 if (SIMPLEQ_EMPTY(&commits)) {
3499 if (continue_rebase)
3500 error = rebase_complete(worktree, branch,
3501 new_base_branch, tmp_branch, repo);
3502 else
3503 error = got_error(GOT_ERR_EMPTY_REBASE);
3504 goto done;
3507 pid = NULL;
3508 SIMPLEQ_FOREACH(qid, &commits, entry) {
3509 commit_id = qid->id;
3510 parent_id = pid ? pid->id : yca_id;
3511 pid = qid;
3513 error = got_worktree_rebase_merge_files(worktree, parent_id,
3514 commit_id, repo, rebase_progress, &rebase_status,
3515 check_cancelled, NULL);
3516 if (error)
3517 goto done;
3519 if (rebase_status == GOT_STATUS_CONFLICT)
3520 break;
3522 error = rebase_commit(worktree, tmp_branch, commit_id, repo);
3523 if (error)
3524 goto done;
3527 if (rebase_status == GOT_STATUS_CONFLICT) {
3528 error = got_worktree_rebase_postpone(worktree);
3529 if (error)
3530 goto done;
3531 error = got_error_msg(GOT_ERR_CONFLICTS,
3532 "conflicts must be resolved before rebase can be resumed");
3533 } else
3534 error = rebase_complete(worktree, branch, new_base_branch,
3535 tmp_branch, repo);
3536 done:
3537 got_object_id_queue_free(&commits);
3538 free(branch_head_commit_id);
3539 free(resume_commit_id);
3540 free(yca_id);
3541 if (graph)
3542 got_commit_graph_close(graph);
3543 if (commit)
3544 got_object_commit_close(commit);
3545 if (branch)
3546 got_ref_close(branch);
3547 if (new_base_branch)
3548 got_ref_close(new_base_branch);
3549 if (tmp_branch)
3550 got_ref_close(tmp_branch);
3551 if (worktree)
3552 got_worktree_close(worktree);
3553 if (repo)
3554 got_repo_close(repo);
3555 return error;