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;
778 free(commit_id_str);
779 error = got_object_id_str(&commit_id_str, commit_id);
780 if (error)
781 goto done;
784 if (branch_name) {
785 struct got_object_id *head_commit_id;
786 if (strlen(path) != 0) {
787 fprintf(stderr, "%s: switching between branches "
788 "requires that the entire work tree "
789 "gets updated, not just '%s'\n",
790 getprogname(), path);
791 error = got_error(GOT_ERR_BAD_PATH);
792 goto done;
794 error = got_ref_resolve(&head_commit_id, repo, head_ref);
795 if (error)
796 goto done;
797 error = check_linear_ancestry(commit_id, head_commit_id, repo);
798 free(head_commit_id);
799 if (error != NULL)
800 goto done;
801 error = check_same_branch(commit_id, head_ref, repo);
802 if (error)
803 goto done;
804 error = switch_head_ref(head_ref, commit_id, worktree, repo);
805 if (error)
806 goto done;
807 } else {
808 error = check_linear_ancestry(commit_id,
809 got_worktree_get_base_commit_id(worktree), repo);
810 if (error != NULL) {
811 if (error->code == GOT_ERR_ANCESTRY)
812 error = got_error(GOT_ERR_BRANCH_MOVED);
813 goto done;
815 error = check_same_branch(commit_id, head_ref, repo);
816 if (error)
817 goto done;
820 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
821 commit_id) != 0) {
822 error = got_worktree_set_base_commit_id(worktree, repo,
823 commit_id);
824 if (error)
825 goto done;
828 error = got_worktree_checkout_files(worktree, path, repo,
829 update_progress, &did_something, check_cancelled, NULL);
830 if (error != NULL)
831 goto done;
833 if (did_something)
834 printf("Updated to commit %s\n", commit_id_str);
835 else
836 printf("Already up-to-date\n");
837 done:
838 free(worktree_path);
839 free(path);
840 free(commit_id);
841 free(commit_id_str);
842 return error;
845 static const struct got_error *
846 print_patch(struct got_commit_object *commit, struct got_object_id *id,
847 int diff_context, struct got_repository *repo)
849 const struct got_error *err = NULL;
850 struct got_tree_object *tree1 = NULL, *tree2;
851 struct got_object_qid *qid;
852 char *id_str1 = NULL, *id_str2;
853 struct got_diff_blob_output_unidiff_arg arg;
855 err = got_object_open_as_tree(&tree2, repo,
856 got_object_commit_get_tree_id(commit));
857 if (err)
858 return err;
860 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
861 if (qid != NULL) {
862 struct got_commit_object *pcommit;
864 err = got_object_open_as_commit(&pcommit, repo, qid->id);
865 if (err)
866 return err;
868 err = got_object_open_as_tree(&tree1, repo,
869 got_object_commit_get_tree_id(pcommit));
870 got_object_commit_close(pcommit);
871 if (err)
872 return err;
874 err = got_object_id_str(&id_str1, qid->id);
875 if (err)
876 return err;
879 err = got_object_id_str(&id_str2, id);
880 if (err)
881 goto done;
883 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
884 arg.diff_context = diff_context;
885 arg.outfile = stdout;
886 err = got_diff_tree(tree1, tree2, "", "", repo,
887 got_diff_blob_output_unidiff, &arg);
888 done:
889 if (tree1)
890 got_object_tree_close(tree1);
891 got_object_tree_close(tree2);
892 free(id_str1);
893 free(id_str2);
894 return err;
897 static char *
898 get_datestr(time_t *time, char *datebuf)
900 char *p, *s = ctime_r(time, datebuf);
901 p = strchr(s, '\n');
902 if (p)
903 *p = '\0';
904 return s;
907 static const struct got_error *
908 print_commit(struct got_commit_object *commit, struct got_object_id *id,
909 struct got_repository *repo, int show_patch, int diff_context,
910 struct got_reflist_head *refs)
912 const struct got_error *err = NULL;
913 char *id_str, *datestr, *logmsg0, *logmsg, *line;
914 char datebuf[26];
915 time_t committer_time;
916 const char *author, *committer;
917 char *refs_str = NULL;
918 struct got_reflist_entry *re;
920 SIMPLEQ_FOREACH(re, refs, entry) {
921 char *s;
922 const char *name;
923 if (got_object_id_cmp(re->id, id) != 0)
924 continue;
925 name = got_ref_get_name(re->ref);
926 if (strcmp(name, GOT_REF_HEAD) == 0)
927 continue;
928 if (strncmp(name, "refs/", 5) == 0)
929 name += 5;
930 if (strncmp(name, "got/", 4) == 0)
931 continue;
932 if (strncmp(name, "heads/", 6) == 0)
933 name += 6;
934 if (strncmp(name, "remotes/", 8) == 0)
935 name += 8;
936 s = refs_str;
937 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
938 name) == -1) {
939 err = got_error_from_errno("asprintf");
940 free(s);
941 break;
943 free(s);
945 err = got_object_id_str(&id_str, id);
946 if (err)
947 return err;
949 printf("-----------------------------------------------\n");
950 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
951 refs_str ? refs_str : "", refs_str ? ")" : "");
952 free(id_str);
953 id_str = NULL;
954 free(refs_str);
955 refs_str = NULL;
956 printf("from: %s\n", got_object_commit_get_author(commit));
957 committer_time = got_object_commit_get_committer_time(commit);
958 datestr = get_datestr(&committer_time, datebuf);
959 printf("date: %s UTC\n", datestr);
960 author = got_object_commit_get_author(commit);
961 committer = got_object_commit_get_committer(commit);
962 if (strcmp(author, committer) != 0)
963 printf("via: %s\n", committer);
964 if (got_object_commit_get_nparents(commit) > 1) {
965 const struct got_object_id_queue *parent_ids;
966 struct got_object_qid *qid;
967 int n = 1;
968 parent_ids = got_object_commit_get_parent_ids(commit);
969 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
970 err = got_object_id_str(&id_str, qid->id);
971 if (err)
972 return err;
973 printf("parent %d: %s\n", n++, id_str);
974 free(id_str);
978 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
979 if (logmsg0 == NULL)
980 return got_error_from_errno("strdup");
982 logmsg = logmsg0;
983 do {
984 line = strsep(&logmsg, "\n");
985 if (line)
986 printf(" %s\n", line);
987 } while (line);
988 free(logmsg0);
990 if (show_patch) {
991 err = print_patch(commit, id, diff_context, repo);
992 if (err == 0)
993 printf("\n");
996 if (fflush(stdout) != 0 && err == NULL)
997 err = got_error_from_errno("fflush");
998 return err;
1001 static const struct got_error *
1002 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1003 char *path, int show_patch, int diff_context, int limit,
1004 int first_parent_traversal, struct got_reflist_head *refs)
1006 const struct got_error *err;
1007 struct got_commit_graph *graph;
1009 err = got_commit_graph_open(&graph, root_id, path,
1010 first_parent_traversal, repo);
1011 if (err)
1012 return err;
1013 err = got_commit_graph_iter_start(graph, root_id, repo);
1014 if (err)
1015 goto done;
1016 for (;;) {
1017 struct got_commit_object *commit;
1018 struct got_object_id *id;
1020 if (sigint_received || sigpipe_received)
1021 break;
1023 err = got_commit_graph_iter_next(&id, graph);
1024 if (err) {
1025 if (err->code == GOT_ERR_ITER_COMPLETED) {
1026 err = NULL;
1027 break;
1029 if (err->code != GOT_ERR_ITER_NEED_MORE)
1030 break;
1031 err = got_commit_graph_fetch_commits(graph, 1, repo);
1032 if (err)
1033 break;
1034 else
1035 continue;
1037 if (id == NULL)
1038 break;
1040 err = got_object_open_as_commit(&commit, repo, id);
1041 if (err)
1042 break;
1043 err = print_commit(commit, id, repo, show_patch, diff_context,
1044 refs);
1045 got_object_commit_close(commit);
1046 if (err || (limit && --limit == 0))
1047 break;
1049 done:
1050 got_commit_graph_close(graph);
1051 return err;
1054 __dead static void
1055 usage_log(void)
1057 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1058 "[-r repository-path] [path]\n", getprogname());
1059 exit(1);
1062 static const struct got_error *
1063 cmd_log(int argc, char *argv[])
1065 const struct got_error *error;
1066 struct got_repository *repo = NULL;
1067 struct got_worktree *worktree = NULL;
1068 struct got_commit_object *commit = NULL;
1069 struct got_object_id *id = NULL;
1070 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1071 char *start_commit = NULL;
1072 int diff_context = 3, ch;
1073 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1074 const char *errstr;
1075 struct got_reflist_head refs;
1077 SIMPLEQ_INIT(&refs);
1079 #ifndef PROFILE
1080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1081 NULL)
1082 == -1)
1083 err(1, "pledge");
1084 #endif
1086 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1087 switch (ch) {
1088 case 'b':
1089 first_parent_traversal = 1;
1090 break;
1091 case 'p':
1092 show_patch = 1;
1093 break;
1094 case 'c':
1095 start_commit = optarg;
1096 break;
1097 case 'C':
1098 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1099 &errstr);
1100 if (errstr != NULL)
1101 err(1, "-C option %s", errstr);
1102 break;
1103 case 'l':
1104 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1105 if (errstr != NULL)
1106 err(1, "-l option %s", errstr);
1107 break;
1108 case 'r':
1109 repo_path = realpath(optarg, NULL);
1110 if (repo_path == NULL)
1111 err(1, "-r option");
1112 got_path_strip_trailing_slashes(repo_path);
1113 break;
1114 default:
1115 usage_log();
1116 /* NOTREACHED */
1120 argc -= optind;
1121 argv += optind;
1123 cwd = getcwd(NULL, 0);
1124 if (cwd == NULL) {
1125 error = got_error_from_errno("getcwd");
1126 goto done;
1129 error = got_worktree_open(&worktree, cwd);
1130 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1131 goto done;
1132 error = NULL;
1134 if (argc == 0) {
1135 path = strdup("");
1136 if (path == NULL) {
1137 error = got_error_from_errno("strdup");
1138 goto done;
1140 } else if (argc == 1) {
1141 if (worktree) {
1142 error = got_worktree_resolve_path(&path, worktree,
1143 argv[0]);
1144 if (error)
1145 goto done;
1146 } else {
1147 path = strdup(argv[0]);
1148 if (path == NULL) {
1149 error = got_error_from_errno("strdup");
1150 goto done;
1153 } else
1154 usage_log();
1156 if (repo_path == NULL) {
1157 repo_path = worktree ?
1158 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1160 if (repo_path == NULL) {
1161 error = got_error_from_errno("strdup");
1162 goto done;
1165 error = got_repo_open(&repo, repo_path);
1166 if (error != NULL)
1167 goto done;
1169 error = apply_unveil(got_repo_get_path(repo), 1,
1170 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1171 if (error)
1172 goto done;
1174 if (start_commit == NULL) {
1175 struct got_reference *head_ref;
1176 error = got_ref_open(&head_ref, repo,
1177 worktree ? got_worktree_get_head_ref_name(worktree)
1178 : GOT_REF_HEAD, 0);
1179 if (error != NULL)
1180 return error;
1181 error = got_ref_resolve(&id, repo, head_ref);
1182 got_ref_close(head_ref);
1183 if (error != NULL)
1184 return error;
1185 error = got_object_open_as_commit(&commit, repo, id);
1186 } else {
1187 struct got_reference *ref;
1188 error = got_ref_open(&ref, repo, start_commit, 0);
1189 if (error == NULL) {
1190 int obj_type;
1191 error = got_ref_resolve(&id, repo, ref);
1192 got_ref_close(ref);
1193 if (error != NULL)
1194 goto done;
1195 error = got_object_get_type(&obj_type, repo, id);
1196 if (error != NULL)
1197 goto done;
1198 if (obj_type == GOT_OBJ_TYPE_TAG) {
1199 struct got_tag_object *tag;
1200 error = got_object_open_as_tag(&tag, repo, id);
1201 if (error != NULL)
1202 goto done;
1203 if (got_object_tag_get_object_type(tag) !=
1204 GOT_OBJ_TYPE_COMMIT) {
1205 got_object_tag_close(tag);
1206 error = got_error(GOT_ERR_OBJ_TYPE);
1207 goto done;
1209 free(id);
1210 id = got_object_id_dup(
1211 got_object_tag_get_object_id(tag));
1212 if (id == NULL)
1213 error = got_error_from_errno(
1214 "got_object_id_dup");
1215 got_object_tag_close(tag);
1216 if (error)
1217 goto done;
1218 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1219 error = got_error(GOT_ERR_OBJ_TYPE);
1220 goto done;
1222 error = got_object_open_as_commit(&commit, repo, id);
1223 if (error != NULL)
1224 goto done;
1226 if (commit == NULL) {
1227 error = got_repo_match_object_id_prefix(&id,
1228 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1229 if (error != NULL)
1230 return error;
1233 if (error != NULL)
1234 goto done;
1236 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1237 if (error != NULL)
1238 goto done;
1239 if (in_repo_path) {
1240 free(path);
1241 path = in_repo_path;
1244 error = got_ref_list(&refs, repo);
1245 if (error)
1246 goto done;
1248 error = print_commits(id, repo, path, show_patch,
1249 diff_context, limit, first_parent_traversal, &refs);
1250 done:
1251 free(path);
1252 free(repo_path);
1253 free(cwd);
1254 free(id);
1255 if (worktree)
1256 got_worktree_close(worktree);
1257 if (repo) {
1258 const struct got_error *repo_error;
1259 repo_error = got_repo_close(repo);
1260 if (error == NULL)
1261 error = repo_error;
1263 got_ref_list_free(&refs);
1264 return error;
1267 __dead static void
1268 usage_diff(void)
1270 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1271 "[object1 object2 | path]\n", getprogname());
1272 exit(1);
1275 struct print_diff_arg {
1276 struct got_repository *repo;
1277 struct got_worktree *worktree;
1278 int diff_context;
1279 const char *id_str;
1280 int header_shown;
1283 static const struct got_error *
1284 print_diff(void *arg, unsigned char status, const char *path,
1285 struct got_object_id *blob_id, struct got_object_id *commit_id)
1287 struct print_diff_arg *a = arg;
1288 const struct got_error *err = NULL;
1289 struct got_blob_object *blob1 = NULL;
1290 FILE *f2 = NULL;
1291 char *abspath = NULL;
1292 struct stat sb;
1294 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1295 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1296 return NULL;
1298 if (!a->header_shown) {
1299 printf("diff %s %s\n", a->id_str,
1300 got_worktree_get_root_path(a->worktree));
1301 a->header_shown = 1;
1304 if (status != GOT_STATUS_ADD) {
1305 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1306 if (err)
1307 goto done;
1311 if (status != GOT_STATUS_DELETE) {
1312 if (asprintf(&abspath, "%s/%s",
1313 got_worktree_get_root_path(a->worktree), path) == -1) {
1314 err = got_error_from_errno("asprintf");
1315 goto done;
1318 f2 = fopen(abspath, "r");
1319 if (f2 == NULL) {
1320 err = got_error_from_errno2("fopen", abspath);
1321 goto done;
1323 if (lstat(abspath, &sb) == -1) {
1324 err = got_error_from_errno2("lstat", abspath);
1325 goto done;
1327 } else
1328 sb.st_size = 0;
1330 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1331 stdout);
1332 done:
1333 if (blob1)
1334 got_object_blob_close(blob1);
1335 if (f2 && fclose(f2) != 0 && err == NULL)
1336 err = got_error_from_errno("fclose");
1337 free(abspath);
1338 return err;
1341 static const struct got_error *
1342 cmd_diff(int argc, char *argv[])
1344 const struct got_error *error;
1345 struct got_repository *repo = NULL;
1346 struct got_worktree *worktree = NULL;
1347 char *cwd = NULL, *repo_path = NULL;
1348 struct got_object_id *id1 = NULL, *id2 = NULL;
1349 const char *id_str1 = NULL, *id_str2 = NULL;
1350 char *label1 = NULL, *label2 = NULL;
1351 int type1, type2;
1352 int diff_context = 3, ch;
1353 const char *errstr;
1354 char *path = NULL;
1356 #ifndef PROFILE
1357 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1358 NULL) == -1)
1359 err(1, "pledge");
1360 #endif
1362 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1363 switch (ch) {
1364 case 'C':
1365 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1366 if (errstr != NULL)
1367 err(1, "-C option %s", errstr);
1368 break;
1369 case 'r':
1370 repo_path = realpath(optarg, NULL);
1371 if (repo_path == NULL)
1372 err(1, "-r option");
1373 got_path_strip_trailing_slashes(repo_path);
1374 break;
1375 default:
1376 usage_diff();
1377 /* NOTREACHED */
1381 argc -= optind;
1382 argv += optind;
1384 cwd = getcwd(NULL, 0);
1385 if (cwd == NULL) {
1386 error = got_error_from_errno("getcwd");
1387 goto done;
1389 error = got_worktree_open(&worktree, cwd);
1390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1391 goto done;
1392 if (argc <= 1) {
1393 if (worktree == NULL) {
1394 error = got_error(GOT_ERR_NOT_WORKTREE);
1395 goto done;
1397 if (repo_path)
1398 errx(1,
1399 "-r option can't be used when diffing a work tree");
1400 repo_path = strdup(got_worktree_get_repo_path(worktree));
1401 if (repo_path == NULL) {
1402 error = got_error_from_errno("strdup");
1403 goto done;
1405 if (argc == 1) {
1406 error = got_worktree_resolve_path(&path, worktree,
1407 argv[0]);
1408 if (error)
1409 goto done;
1410 } else {
1411 path = strdup("");
1412 if (path == NULL) {
1413 error = got_error_from_errno("strdup");
1414 goto done;
1417 } else if (argc == 2) {
1418 id_str1 = argv[0];
1419 id_str2 = argv[1];
1420 if (worktree && repo_path == NULL) {
1421 repo_path =
1422 strdup(got_worktree_get_repo_path(worktree));
1423 if (repo_path == NULL) {
1424 error = got_error_from_errno("strdup");
1425 goto done;
1428 } else
1429 usage_diff();
1431 if (repo_path == NULL) {
1432 repo_path = getcwd(NULL, 0);
1433 if (repo_path == NULL)
1434 return got_error_from_errno("getcwd");
1437 error = got_repo_open(&repo, repo_path);
1438 free(repo_path);
1439 if (error != NULL)
1440 goto done;
1442 error = apply_unveil(got_repo_get_path(repo), 1,
1443 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1444 if (error)
1445 goto done;
1447 if (argc <= 1) {
1448 struct print_diff_arg arg;
1449 char *id_str;
1450 error = got_object_id_str(&id_str,
1451 got_worktree_get_base_commit_id(worktree));
1452 if (error)
1453 goto done;
1454 arg.repo = repo;
1455 arg.worktree = worktree;
1456 arg.diff_context = diff_context;
1457 arg.id_str = id_str;
1458 arg.header_shown = 0;
1460 error = got_worktree_status(worktree, path, repo, print_diff,
1461 &arg, check_cancelled, NULL);
1462 free(id_str);
1463 goto done;
1466 error = got_repo_match_object_id_prefix(&id1, id_str1,
1467 GOT_OBJ_TYPE_ANY, repo);
1468 if (error) {
1469 struct got_reference *ref;
1470 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1471 goto done;
1472 error = got_ref_open(&ref, repo, id_str1, 0);
1473 if (error != NULL)
1474 goto done;
1475 label1 = strdup(got_ref_get_name(ref));
1476 if (label1 == NULL) {
1477 error = got_error_from_errno("strdup");
1478 goto done;
1480 error = got_ref_resolve(&id1, repo, ref);
1481 got_ref_close(ref);
1482 if (error != NULL)
1483 goto done;
1484 } else {
1485 error = got_object_id_str(&label1, id1);
1486 if (label1 == NULL) {
1487 error = got_error_from_errno("strdup");
1488 goto done;
1492 error = got_repo_match_object_id_prefix(&id2, id_str2,
1493 GOT_OBJ_TYPE_ANY, repo);
1494 if (error) {
1495 struct got_reference *ref;
1496 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1497 goto done;
1498 error = got_ref_open(&ref, repo, id_str2, 0);
1499 if (error != NULL)
1500 goto done;
1501 label2 = strdup(got_ref_get_name(ref));
1502 if (label2 == NULL) {
1503 error = got_error_from_errno("strdup");
1504 goto done;
1506 error = got_ref_resolve(&id2, repo, ref);
1507 got_ref_close(ref);
1508 if (error != NULL)
1509 goto done;
1510 } else {
1511 error = got_object_id_str(&label2, id2);
1512 if (label2 == NULL) {
1513 error = got_error_from_errno("strdup");
1514 goto done;
1518 error = got_object_get_type(&type1, repo, id1);
1519 if (error)
1520 goto done;
1522 error = got_object_get_type(&type2, repo, id2);
1523 if (error)
1524 goto done;
1526 if (type1 != type2) {
1527 error = got_error(GOT_ERR_OBJ_TYPE);
1528 goto done;
1531 switch (type1) {
1532 case GOT_OBJ_TYPE_BLOB:
1533 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1534 diff_context, repo, stdout);
1535 break;
1536 case GOT_OBJ_TYPE_TREE:
1537 error = got_diff_objects_as_trees(id1, id2, "", "",
1538 diff_context, repo, stdout);
1539 break;
1540 case GOT_OBJ_TYPE_COMMIT:
1541 printf("diff %s %s\n", label1, label2);
1542 error = got_diff_objects_as_commits(id1, id2, diff_context,
1543 repo, stdout);
1544 break;
1545 default:
1546 error = got_error(GOT_ERR_OBJ_TYPE);
1549 done:
1550 free(label1);
1551 free(label2);
1552 free(id1);
1553 free(id2);
1554 free(path);
1555 if (worktree)
1556 got_worktree_close(worktree);
1557 if (repo) {
1558 const struct got_error *repo_error;
1559 repo_error = got_repo_close(repo);
1560 if (error == NULL)
1561 error = repo_error;
1563 return error;
1566 __dead static void
1567 usage_blame(void)
1569 fprintf(stderr,
1570 "usage: %s blame [-c commit] [-r repository-path] path\n",
1571 getprogname());
1572 exit(1);
1575 static const struct got_error *
1576 cmd_blame(int argc, char *argv[])
1578 const struct got_error *error;
1579 struct got_repository *repo = NULL;
1580 struct got_worktree *worktree = NULL;
1581 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1582 struct got_object_id *commit_id = NULL;
1583 char *commit_id_str = NULL;
1584 int ch;
1586 #ifndef PROFILE
1587 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1588 NULL) == -1)
1589 err(1, "pledge");
1590 #endif
1592 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1593 switch (ch) {
1594 case 'c':
1595 commit_id_str = optarg;
1596 break;
1597 case 'r':
1598 repo_path = realpath(optarg, NULL);
1599 if (repo_path == NULL)
1600 err(1, "-r option");
1601 got_path_strip_trailing_slashes(repo_path);
1602 break;
1603 default:
1604 usage_blame();
1605 /* NOTREACHED */
1609 argc -= optind;
1610 argv += optind;
1612 if (argc == 1)
1613 path = argv[0];
1614 else
1615 usage_blame();
1617 cwd = getcwd(NULL, 0);
1618 if (cwd == NULL) {
1619 error = got_error_from_errno("getcwd");
1620 goto done;
1622 if (repo_path == NULL) {
1623 error = got_worktree_open(&worktree, cwd);
1624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1625 goto done;
1626 else
1627 error = NULL;
1628 if (worktree) {
1629 repo_path =
1630 strdup(got_worktree_get_repo_path(worktree));
1631 if (repo_path == NULL)
1632 error = got_error_from_errno("strdup");
1633 if (error)
1634 goto done;
1635 } else {
1636 repo_path = strdup(cwd);
1637 if (repo_path == NULL) {
1638 error = got_error_from_errno("strdup");
1639 goto done;
1644 error = got_repo_open(&repo, repo_path);
1645 if (error != NULL)
1646 goto done;
1648 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1649 if (error)
1650 goto done;
1652 if (worktree) {
1653 const char *prefix = got_worktree_get_path_prefix(worktree);
1654 char *p, *worktree_subdir = cwd +
1655 strlen(got_worktree_get_root_path(worktree));
1656 if (asprintf(&p, "%s%s%s%s%s",
1657 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1658 worktree_subdir, worktree_subdir[0] ? "/" : "",
1659 path) == -1) {
1660 error = got_error_from_errno("asprintf");
1661 goto done;
1663 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1664 free(p);
1665 } else {
1666 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1668 if (error)
1669 goto done;
1671 if (commit_id_str == NULL) {
1672 struct got_reference *head_ref;
1673 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1674 if (error != NULL)
1675 goto done;
1676 error = got_ref_resolve(&commit_id, repo, head_ref);
1677 got_ref_close(head_ref);
1678 if (error != NULL)
1679 goto done;
1680 } else {
1681 error = got_repo_match_object_id_prefix(&commit_id,
1682 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1683 if (error != NULL)
1684 goto done;
1687 error = got_blame(in_repo_path, commit_id, repo, stdout);
1688 done:
1689 free(in_repo_path);
1690 free(repo_path);
1691 free(cwd);
1692 free(commit_id);
1693 if (worktree)
1694 got_worktree_close(worktree);
1695 if (repo) {
1696 const struct got_error *repo_error;
1697 repo_error = got_repo_close(repo);
1698 if (error == NULL)
1699 error = repo_error;
1701 return error;
1704 __dead static void
1705 usage_tree(void)
1707 fprintf(stderr,
1708 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1709 getprogname());
1710 exit(1);
1713 static void
1714 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1715 const char *root_path)
1717 int is_root_path = (strcmp(path, root_path) == 0);
1719 path += strlen(root_path);
1720 while (path[0] == '/')
1721 path++;
1723 printf("%s%s%s%s%s\n", id ? id : "", path,
1724 is_root_path ? "" : "/", te->name,
1725 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1728 static const struct got_error *
1729 print_tree(const char *path, struct got_object_id *commit_id,
1730 int show_ids, int recurse, const char *root_path,
1731 struct got_repository *repo)
1733 const struct got_error *err = NULL;
1734 struct got_object_id *tree_id = NULL;
1735 struct got_tree_object *tree = NULL;
1736 const struct got_tree_entries *entries;
1737 struct got_tree_entry *te;
1739 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1740 if (err)
1741 goto done;
1743 err = got_object_open_as_tree(&tree, repo, tree_id);
1744 if (err)
1745 goto done;
1746 entries = got_object_tree_get_entries(tree);
1747 te = SIMPLEQ_FIRST(&entries->head);
1748 while (te) {
1749 char *id = NULL;
1751 if (sigint_received || sigpipe_received)
1752 break;
1754 if (show_ids) {
1755 char *id_str;
1756 err = got_object_id_str(&id_str, te->id);
1757 if (err)
1758 goto done;
1759 if (asprintf(&id, "%s ", id_str) == -1) {
1760 err = got_error_from_errno("asprintf");
1761 free(id_str);
1762 goto done;
1764 free(id_str);
1766 print_entry(te, id, path, root_path);
1767 free(id);
1769 if (recurse && S_ISDIR(te->mode)) {
1770 char *child_path;
1771 if (asprintf(&child_path, "%s%s%s", path,
1772 path[0] == '/' && path[1] == '\0' ? "" : "/",
1773 te->name) == -1) {
1774 err = got_error_from_errno("asprintf");
1775 goto done;
1777 err = print_tree(child_path, commit_id, show_ids, 1,
1778 root_path, repo);
1779 free(child_path);
1780 if (err)
1781 goto done;
1784 te = SIMPLEQ_NEXT(te, entry);
1786 done:
1787 if (tree)
1788 got_object_tree_close(tree);
1789 free(tree_id);
1790 return err;
1793 static const struct got_error *
1794 cmd_tree(int argc, char *argv[])
1796 const struct got_error *error;
1797 struct got_repository *repo = NULL;
1798 struct got_worktree *worktree = NULL;
1799 const char *path;
1800 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1801 struct got_object_id *commit_id = NULL;
1802 char *commit_id_str = NULL;
1803 int show_ids = 0, recurse = 0;
1804 int ch;
1806 #ifndef PROFILE
1807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1808 NULL) == -1)
1809 err(1, "pledge");
1810 #endif
1812 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1813 switch (ch) {
1814 case 'c':
1815 commit_id_str = optarg;
1816 break;
1817 case 'r':
1818 repo_path = realpath(optarg, NULL);
1819 if (repo_path == NULL)
1820 err(1, "-r option");
1821 got_path_strip_trailing_slashes(repo_path);
1822 break;
1823 case 'i':
1824 show_ids = 1;
1825 break;
1826 case 'R':
1827 recurse = 1;
1828 break;
1829 default:
1830 usage_tree();
1831 /* NOTREACHED */
1835 argc -= optind;
1836 argv += optind;
1838 if (argc == 1)
1839 path = argv[0];
1840 else if (argc > 1)
1841 usage_tree();
1842 else
1843 path = NULL;
1845 cwd = getcwd(NULL, 0);
1846 if (cwd == NULL) {
1847 error = got_error_from_errno("getcwd");
1848 goto done;
1850 if (repo_path == NULL) {
1851 error = got_worktree_open(&worktree, cwd);
1852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1853 goto done;
1854 else
1855 error = NULL;
1856 if (worktree) {
1857 repo_path =
1858 strdup(got_worktree_get_repo_path(worktree));
1859 if (repo_path == NULL)
1860 error = got_error_from_errno("strdup");
1861 if (error)
1862 goto done;
1863 } else {
1864 repo_path = strdup(cwd);
1865 if (repo_path == NULL) {
1866 error = got_error_from_errno("strdup");
1867 goto done;
1872 error = got_repo_open(&repo, repo_path);
1873 if (error != NULL)
1874 goto done;
1876 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1877 if (error)
1878 goto done;
1880 if (path == NULL) {
1881 if (worktree) {
1882 char *p, *worktree_subdir = cwd +
1883 strlen(got_worktree_get_root_path(worktree));
1884 if (asprintf(&p, "%s/%s",
1885 got_worktree_get_path_prefix(worktree),
1886 worktree_subdir) == -1) {
1887 error = got_error_from_errno("asprintf");
1888 goto done;
1890 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1891 free(p);
1892 if (error)
1893 goto done;
1894 } else
1895 path = "/";
1897 if (in_repo_path == NULL) {
1898 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1899 if (error != NULL)
1900 goto done;
1903 if (commit_id_str == NULL) {
1904 struct got_reference *head_ref;
1905 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1906 if (error != NULL)
1907 goto done;
1908 error = got_ref_resolve(&commit_id, repo, head_ref);
1909 got_ref_close(head_ref);
1910 if (error != NULL)
1911 goto done;
1912 } else {
1913 error = got_repo_match_object_id_prefix(&commit_id,
1914 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1915 if (error != NULL)
1916 goto done;
1919 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1920 in_repo_path, repo);
1921 done:
1922 free(in_repo_path);
1923 free(repo_path);
1924 free(cwd);
1925 free(commit_id);
1926 if (worktree)
1927 got_worktree_close(worktree);
1928 if (repo) {
1929 const struct got_error *repo_error;
1930 repo_error = got_repo_close(repo);
1931 if (error == NULL)
1932 error = repo_error;
1934 return error;
1937 __dead static void
1938 usage_status(void)
1940 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1941 exit(1);
1944 static const struct got_error *
1945 print_status(void *arg, unsigned char status, const char *path,
1946 struct got_object_id *blob_id, struct got_object_id *commit_id)
1948 printf("%c %s\n", status, path);
1949 return NULL;
1952 static const struct got_error *
1953 cmd_status(int argc, char *argv[])
1955 const struct got_error *error = NULL;
1956 struct got_repository *repo = NULL;
1957 struct got_worktree *worktree = NULL;
1958 char *cwd = NULL, *path = NULL;
1959 int ch;
1961 while ((ch = getopt(argc, argv, "")) != -1) {
1962 switch (ch) {
1963 default:
1964 usage_status();
1965 /* NOTREACHED */
1969 argc -= optind;
1970 argv += optind;
1972 #ifndef PROFILE
1973 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1974 NULL) == -1)
1975 err(1, "pledge");
1976 #endif
1977 cwd = getcwd(NULL, 0);
1978 if (cwd == NULL) {
1979 error = got_error_from_errno("getcwd");
1980 goto done;
1983 error = got_worktree_open(&worktree, cwd);
1984 if (error != NULL)
1985 goto done;
1987 if (argc == 0) {
1988 path = strdup("");
1989 if (path == NULL) {
1990 error = got_error_from_errno("strdup");
1991 goto done;
1993 } else if (argc == 1) {
1994 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1995 if (error)
1996 goto done;
1997 } else
1998 usage_status();
2000 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2001 if (error != NULL)
2002 goto done;
2004 error = apply_unveil(got_repo_get_path(repo), 1,
2005 got_worktree_get_root_path(worktree), 0);
2006 if (error)
2007 goto done;
2009 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2010 check_cancelled, NULL);
2011 done:
2012 free(cwd);
2013 free(path);
2014 return error;
2017 __dead static void
2018 usage_ref(void)
2020 fprintf(stderr,
2021 "usage: %s ref [-r repository] -l | -d name | name target\n",
2022 getprogname());
2023 exit(1);
2026 static const struct got_error *
2027 list_refs(struct got_repository *repo)
2029 static const struct got_error *err = NULL;
2030 struct got_reflist_head refs;
2031 struct got_reflist_entry *re;
2033 SIMPLEQ_INIT(&refs);
2034 err = got_ref_list(&refs, repo);
2035 if (err)
2036 return err;
2038 SIMPLEQ_FOREACH(re, &refs, entry) {
2039 char *refstr;
2040 refstr = got_ref_to_str(re->ref);
2041 if (refstr == NULL)
2042 return got_error_from_errno("got_ref_to_str");
2043 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2044 free(refstr);
2047 got_ref_list_free(&refs);
2048 return NULL;
2051 static const struct got_error *
2052 delete_ref(struct got_repository *repo, const char *refname)
2054 const struct got_error *err = NULL;
2055 struct got_reference *ref;
2057 err = got_ref_open(&ref, repo, refname, 0);
2058 if (err)
2059 return err;
2061 err = got_ref_delete(ref, repo);
2062 got_ref_close(ref);
2063 return err;
2066 static const struct got_error *
2067 add_ref(struct got_repository *repo, const char *refname, const char *target)
2069 const struct got_error *err = NULL;
2070 struct got_object_id *id;
2071 struct got_reference *ref = NULL;
2073 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2074 repo);
2075 if (err) {
2076 struct got_reference *target_ref;
2078 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2079 return err;
2080 err = got_ref_open(&target_ref, repo, target, 0);
2081 if (err)
2082 return err;
2083 err = got_ref_resolve(&id, repo, target_ref);
2084 got_ref_close(target_ref);
2085 if (err)
2086 return err;
2089 err = got_ref_alloc(&ref, refname, id);
2090 if (err)
2091 goto done;
2093 err = got_ref_write(ref, repo);
2094 done:
2095 if (ref)
2096 got_ref_close(ref);
2097 free(id);
2098 return err;
2101 static const struct got_error *
2102 cmd_ref(int argc, char *argv[])
2104 const struct got_error *error = NULL;
2105 struct got_repository *repo = NULL;
2106 struct got_worktree *worktree = NULL;
2107 char *cwd = NULL, *repo_path = NULL;
2108 int ch, do_list = 0;
2109 const char *delref = NULL;
2111 /* TODO: Add -s option for adding symbolic references. */
2112 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2113 switch (ch) {
2114 case 'd':
2115 delref = optarg;
2116 break;
2117 case 'r':
2118 repo_path = realpath(optarg, NULL);
2119 if (repo_path == NULL)
2120 err(1, "-r option");
2121 got_path_strip_trailing_slashes(repo_path);
2122 break;
2123 case 'l':
2124 do_list = 1;
2125 break;
2126 default:
2127 usage_ref();
2128 /* NOTREACHED */
2132 if (do_list && delref)
2133 errx(1, "-l and -d options are mutually exclusive\n");
2135 argc -= optind;
2136 argv += optind;
2138 if (do_list || delref) {
2139 if (argc > 0)
2140 usage_ref();
2141 } else if (argc != 2)
2142 usage_ref();
2144 #ifndef PROFILE
2145 if (do_list) {
2146 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2147 NULL) == -1)
2148 err(1, "pledge");
2149 } else {
2150 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2151 "sendfd unveil", NULL) == -1)
2152 err(1, "pledge");
2154 #endif
2155 cwd = getcwd(NULL, 0);
2156 if (cwd == NULL) {
2157 error = got_error_from_errno("getcwd");
2158 goto done;
2161 if (repo_path == NULL) {
2162 error = got_worktree_open(&worktree, cwd);
2163 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2164 goto done;
2165 else
2166 error = NULL;
2167 if (worktree) {
2168 repo_path =
2169 strdup(got_worktree_get_repo_path(worktree));
2170 if (repo_path == NULL)
2171 error = got_error_from_errno("strdup");
2172 if (error)
2173 goto done;
2174 } else {
2175 repo_path = strdup(cwd);
2176 if (repo_path == NULL) {
2177 error = got_error_from_errno("strdup");
2178 goto done;
2183 error = got_repo_open(&repo, repo_path);
2184 if (error != NULL)
2185 goto done;
2187 error = apply_unveil(got_repo_get_path(repo), do_list,
2188 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2189 if (error)
2190 goto done;
2192 if (do_list)
2193 error = list_refs(repo);
2194 else if (delref)
2195 error = delete_ref(repo, delref);
2196 else
2197 error = add_ref(repo, argv[0], argv[1]);
2198 done:
2199 if (repo)
2200 got_repo_close(repo);
2201 if (worktree)
2202 got_worktree_close(worktree);
2203 free(cwd);
2204 free(repo_path);
2205 return error;
2208 __dead static void
2209 usage_branch(void)
2211 fprintf(stderr,
2212 "usage: %s branch [-r repository] -l | -d name | "
2213 "name [base-branch]\n", getprogname());
2214 exit(1);
2217 static const struct got_error *
2218 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2220 static const struct got_error *err = NULL;
2221 struct got_reflist_head refs;
2222 struct got_reflist_entry *re;
2224 SIMPLEQ_INIT(&refs);
2226 err = got_ref_list(&refs, repo);
2227 if (err)
2228 return err;
2230 SIMPLEQ_FOREACH(re, &refs, entry) {
2231 const char *refname, *marker = " ";
2232 char *refstr;
2233 refname = got_ref_get_name(re->ref);
2234 if (strncmp(refname, "refs/heads/", 11) != 0)
2235 continue;
2236 if (worktree && strcmp(refname,
2237 got_worktree_get_head_ref_name(worktree)) == 0) {
2238 struct got_object_id *id = NULL;
2239 err = got_ref_resolve(&id, repo, re->ref);
2240 if (err)
2241 return err;
2242 if (got_object_id_cmp(id,
2243 got_worktree_get_base_commit_id(worktree)) == 0)
2244 marker = "* ";
2245 else
2246 marker = "~ ";
2247 free(id);
2249 refname += 11;
2250 refstr = got_ref_to_str(re->ref);
2251 if (refstr == NULL)
2252 return got_error_from_errno("got_ref_to_str");
2253 printf("%s%s: %s\n", marker, refname, refstr);
2254 free(refstr);
2257 got_ref_list_free(&refs);
2258 return NULL;
2261 static const struct got_error *
2262 delete_branch(struct got_repository *repo, const char *branch_name)
2264 const struct got_error *err = NULL;
2265 struct got_reference *ref;
2266 char *refname;
2268 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2269 return got_error_from_errno("asprintf");
2271 err = got_ref_open(&ref, repo, refname, 0);
2272 if (err)
2273 goto done;
2275 err = got_ref_delete(ref, repo);
2276 got_ref_close(ref);
2277 done:
2278 free(refname);
2279 return err;
2282 static const struct got_error *
2283 add_branch(struct got_repository *repo, const char *branch_name,
2284 const char *base_branch)
2286 const struct got_error *err = NULL;
2287 struct got_object_id *id = NULL;
2288 struct got_reference *ref = NULL;
2289 char *base_refname = NULL, *refname = NULL;
2290 struct got_reference *base_ref;
2293 * Don't let the user create a branch named '-'.
2294 * While technically a valid reference name, this case is usually
2295 * an unintended typo.
2297 if (branch_name[0] == '-' && branch_name[1] == '\0')
2298 return got_error(GOT_ERR_BAD_REF_NAME);
2300 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2301 base_refname = strdup(GOT_REF_HEAD);
2302 if (base_refname == NULL)
2303 return got_error_from_errno("strdup");
2304 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2305 return got_error_from_errno("asprintf");
2307 err = got_ref_open(&base_ref, repo, base_refname, 0);
2308 if (err)
2309 goto done;
2310 err = got_ref_resolve(&id, repo, base_ref);
2311 got_ref_close(base_ref);
2312 if (err)
2313 goto done;
2315 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2316 err = got_error_from_errno("asprintf");
2317 goto done;
2320 err = got_ref_open(&ref, repo, refname, 0);
2321 if (err == NULL) {
2322 err = got_error(GOT_ERR_BRANCH_EXISTS);
2323 goto done;
2324 } else if (err->code != GOT_ERR_NOT_REF)
2325 goto done;
2327 err = got_ref_alloc(&ref, refname, id);
2328 if (err)
2329 goto done;
2331 err = got_ref_write(ref, repo);
2332 done:
2333 if (ref)
2334 got_ref_close(ref);
2335 free(id);
2336 free(base_refname);
2337 free(refname);
2338 return err;
2341 static const struct got_error *
2342 cmd_branch(int argc, char *argv[])
2344 const struct got_error *error = NULL;
2345 struct got_repository *repo = NULL;
2346 struct got_worktree *worktree = NULL;
2347 char *cwd = NULL, *repo_path = NULL;
2348 int ch, do_list = 0;
2349 const char *delref = NULL;
2351 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2352 switch (ch) {
2353 case 'd':
2354 delref = optarg;
2355 break;
2356 case 'r':
2357 repo_path = realpath(optarg, NULL);
2358 if (repo_path == NULL)
2359 err(1, "-r option");
2360 got_path_strip_trailing_slashes(repo_path);
2361 break;
2362 case 'l':
2363 do_list = 1;
2364 break;
2365 default:
2366 usage_branch();
2367 /* NOTREACHED */
2371 if (do_list && delref)
2372 errx(1, "-l and -d options are mutually exclusive\n");
2374 argc -= optind;
2375 argv += optind;
2377 if (do_list || delref) {
2378 if (argc > 0)
2379 usage_branch();
2380 } else if (argc < 1 || argc > 2)
2381 usage_branch();
2383 #ifndef PROFILE
2384 if (do_list) {
2385 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2386 NULL) == -1)
2387 err(1, "pledge");
2388 } else {
2389 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2390 "sendfd unveil", NULL) == -1)
2391 err(1, "pledge");
2393 #endif
2394 cwd = getcwd(NULL, 0);
2395 if (cwd == NULL) {
2396 error = got_error_from_errno("getcwd");
2397 goto done;
2400 if (repo_path == NULL) {
2401 error = got_worktree_open(&worktree, cwd);
2402 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2403 goto done;
2404 else
2405 error = NULL;
2406 if (worktree) {
2407 repo_path =
2408 strdup(got_worktree_get_repo_path(worktree));
2409 if (repo_path == NULL)
2410 error = got_error_from_errno("strdup");
2411 if (error)
2412 goto done;
2413 } else {
2414 repo_path = strdup(cwd);
2415 if (repo_path == NULL) {
2416 error = got_error_from_errno("strdup");
2417 goto done;
2422 error = got_repo_open(&repo, repo_path);
2423 if (error != NULL)
2424 goto done;
2426 error = apply_unveil(got_repo_get_path(repo), do_list,
2427 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2428 if (error)
2429 goto done;
2431 if (do_list)
2432 error = list_branches(repo, worktree);
2433 else if (delref)
2434 error = delete_branch(repo, delref);
2435 else {
2436 const char *base_branch;
2437 if (argc == 1) {
2438 base_branch = worktree ?
2439 got_worktree_get_head_ref_name(worktree) :
2440 GOT_REF_HEAD;
2441 } else
2442 base_branch = argv[1];
2443 error = add_branch(repo, argv[0], base_branch);
2445 done:
2446 if (repo)
2447 got_repo_close(repo);
2448 if (worktree)
2449 got_worktree_close(worktree);
2450 free(cwd);
2451 free(repo_path);
2452 return error;
2455 __dead static void
2456 usage_add(void)
2458 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2459 exit(1);
2462 static const struct got_error *
2463 cmd_add(int argc, char *argv[])
2465 const struct got_error *error = NULL;
2466 struct got_repository *repo = NULL;
2467 struct got_worktree *worktree = NULL;
2468 char *cwd = NULL;
2469 struct got_pathlist_head paths;
2470 struct got_pathlist_entry *pe;
2471 int ch, x;
2473 TAILQ_INIT(&paths);
2475 while ((ch = getopt(argc, argv, "")) != -1) {
2476 switch (ch) {
2477 default:
2478 usage_add();
2479 /* NOTREACHED */
2483 argc -= optind;
2484 argv += optind;
2486 if (argc < 1)
2487 usage_add();
2489 /* make sure each file exists before doing anything halfway */
2490 for (x = 0; x < argc; x++) {
2491 char *path = realpath(argv[x], NULL);
2492 if (path == NULL) {
2493 error = got_error_from_errno2("realpath", argv[x]);
2494 goto done;
2496 free(path);
2499 cwd = getcwd(NULL, 0);
2500 if (cwd == NULL) {
2501 error = got_error_from_errno("getcwd");
2502 goto done;
2505 error = got_worktree_open(&worktree, cwd);
2506 if (error)
2507 goto done;
2509 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2510 if (error != NULL)
2511 goto done;
2513 error = apply_unveil(got_repo_get_path(repo), 1,
2514 got_worktree_get_root_path(worktree), 0);
2515 if (error)
2516 goto done;
2518 for (x = 0; x < argc; x++) {
2519 char *path = realpath(argv[x], NULL);
2520 if (path == NULL) {
2521 error = got_error_from_errno2("realpath", argv[x]);
2522 goto done;
2525 got_path_strip_trailing_slashes(path);
2526 error = got_pathlist_insert(&pe, &paths, path, NULL);
2527 if (error) {
2528 free(path);
2529 goto done;
2532 error = got_worktree_schedule_add(worktree, &paths, print_status,
2533 NULL, repo);
2534 done:
2535 if (repo)
2536 got_repo_close(repo);
2537 if (worktree)
2538 got_worktree_close(worktree);
2539 TAILQ_FOREACH(pe, &paths, entry)
2540 free((char *)pe->path);
2541 got_pathlist_free(&paths);
2542 free(cwd);
2543 return error;
2546 __dead static void
2547 usage_remove(void)
2549 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2550 exit(1);
2553 static const struct got_error *
2554 cmd_remove(int argc, char *argv[])
2556 const struct got_error *error = NULL;
2557 struct got_worktree *worktree = NULL;
2558 struct got_repository *repo = NULL;
2559 char *cwd = NULL;
2560 struct got_pathlist_head paths;
2561 struct got_pathlist_entry *pe;
2562 int ch, i, delete_local_mods = 0;
2564 TAILQ_INIT(&paths);
2566 while ((ch = getopt(argc, argv, "f")) != -1) {
2567 switch (ch) {
2568 case 'f':
2569 delete_local_mods = 1;
2570 break;
2571 default:
2572 usage_add();
2573 /* NOTREACHED */
2577 argc -= optind;
2578 argv += optind;
2580 if (argc < 1)
2581 usage_remove();
2583 /* make sure each file exists before doing anything halfway */
2584 for (i = 0; i < argc; i++) {
2585 char *path = realpath(argv[i], NULL);
2586 if (path == NULL) {
2587 error = got_error_from_errno2("realpath", argv[i]);
2588 goto done;
2590 free(path);
2593 cwd = getcwd(NULL, 0);
2594 if (cwd == NULL) {
2595 error = got_error_from_errno("getcwd");
2596 goto done;
2598 error = got_worktree_open(&worktree, cwd);
2599 if (error)
2600 goto done;
2602 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2603 if (error)
2604 goto done;
2606 error = apply_unveil(got_repo_get_path(repo), 1,
2607 got_worktree_get_root_path(worktree), 0);
2608 if (error)
2609 goto done;
2611 for (i = 0; i < argc; i++) {
2612 char *path = realpath(argv[i], NULL);
2613 if (path == NULL) {
2614 error = got_error_from_errno2("realpath", argv[i]);
2615 goto done;
2618 got_path_strip_trailing_slashes(path);
2619 error = got_pathlist_insert(&pe, &paths, path, NULL);
2620 if (error) {
2621 free(path);
2622 goto done;
2625 error = got_worktree_schedule_delete(worktree, &paths,
2626 delete_local_mods, print_status, NULL, repo);
2627 if (error)
2628 goto done;
2629 done:
2630 if (repo)
2631 got_repo_close(repo);
2632 if (worktree)
2633 got_worktree_close(worktree);
2634 TAILQ_FOREACH(pe, &paths, entry)
2635 free((char *)pe->path);
2636 got_pathlist_free(&paths);
2637 free(cwd);
2638 return error;
2641 __dead static void
2642 usage_revert(void)
2644 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2645 exit(1);
2648 static void
2649 revert_progress(void *arg, unsigned char status, const char *path)
2651 while (path[0] == '/')
2652 path++;
2653 printf("%c %s\n", status, path);
2656 static const struct got_error *
2657 cmd_revert(int argc, char *argv[])
2659 const struct got_error *error = NULL;
2660 struct got_worktree *worktree = NULL;
2661 struct got_repository *repo = NULL;
2662 char *cwd = NULL, *path = NULL;
2663 struct got_pathlist_head paths;
2664 struct got_pathlist_entry *pe;
2665 int ch, i;
2667 TAILQ_INIT(&paths);
2669 while ((ch = getopt(argc, argv, "")) != -1) {
2670 switch (ch) {
2671 default:
2672 usage_revert();
2673 /* NOTREACHED */
2677 argc -= optind;
2678 argv += optind;
2680 if (argc < 1)
2681 usage_revert();
2683 for (i = 0; i < argc; i++) {
2684 char *path = realpath(argv[i], NULL);
2685 if (path == NULL) {
2686 error = got_error_from_errno2("realpath", argv[i]);
2687 goto done;
2690 got_path_strip_trailing_slashes(path);
2691 error = got_pathlist_insert(&pe, &paths, path, NULL);
2692 if (error) {
2693 free(path);
2694 goto done;
2698 cwd = getcwd(NULL, 0);
2699 if (cwd == NULL) {
2700 error = got_error_from_errno("getcwd");
2701 goto done;
2703 error = got_worktree_open(&worktree, cwd);
2704 if (error)
2705 goto done;
2707 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2708 if (error != NULL)
2709 goto done;
2711 error = apply_unveil(got_repo_get_path(repo), 1,
2712 got_worktree_get_root_path(worktree), 0);
2713 if (error)
2714 goto done;
2716 error = got_worktree_revert(worktree, &paths,
2717 revert_progress, NULL, repo);
2718 if (error)
2719 goto done;
2720 done:
2721 if (repo)
2722 got_repo_close(repo);
2723 if (worktree)
2724 got_worktree_close(worktree);
2725 free(path);
2726 free(cwd);
2727 return error;
2730 __dead static void
2731 usage_commit(void)
2733 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2734 exit(1);
2737 int
2738 spawn_editor(const char *editor, const char *file)
2740 pid_t pid;
2741 sig_t sighup, sigint, sigquit;
2742 int st = -1;
2744 sighup = signal(SIGHUP, SIG_IGN);
2745 sigint = signal(SIGINT, SIG_IGN);
2746 sigquit = signal(SIGQUIT, SIG_IGN);
2748 switch (pid = fork()) {
2749 case -1:
2750 goto doneediting;
2751 case 0:
2752 execl(editor, editor, file, (char *)NULL);
2753 _exit(127);
2756 while (waitpid(pid, &st, 0) == -1)
2757 if (errno != EINTR)
2758 break;
2760 doneediting:
2761 (void)signal(SIGHUP, sighup);
2762 (void)signal(SIGINT, sigint);
2763 (void)signal(SIGQUIT, sigquit);
2765 if (!WIFEXITED(st)) {
2766 errno = EINTR;
2767 return -1;
2770 return WEXITSTATUS(st);
2773 struct collect_commit_logmsg_arg {
2774 const char *cmdline_log;
2775 const char *editor;
2776 const char *worktree_path;
2777 const char *branch_name;
2778 const char *repo_path;
2779 char *logmsg_path;
2783 static const struct got_error *
2784 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2785 void *arg)
2787 char *initial_content = NULL;
2788 struct got_pathlist_entry *pe;
2789 const struct got_error *err = NULL;
2790 char *template = NULL;
2791 struct collect_commit_logmsg_arg *a = arg;
2792 char buf[1024];
2793 struct stat st, st2;
2794 FILE *fp;
2795 size_t len;
2796 int fd, content_changed = 0;
2798 /* if a message was specified on the command line, just use it */
2799 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2800 len = strlen(a->cmdline_log) + 1;
2801 *logmsg = malloc(len + 1);
2802 if (*logmsg == NULL)
2803 return got_error_from_errno("malloc");
2804 strlcpy(*logmsg, a->cmdline_log, len);
2805 return NULL;
2808 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2809 return got_error_from_errno("asprintf");
2811 if (asprintf(&initial_content,
2812 "\n# changes to be committed on branch %s:\n",
2813 a->branch_name) == -1)
2814 return got_error_from_errno("asprintf");
2816 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2817 if (err)
2818 goto done;
2820 dprintf(fd, initial_content);
2822 TAILQ_FOREACH(pe, commitable_paths, entry) {
2823 struct got_commitable *ct = pe->data;
2824 dprintf(fd, "# %c %s\n",
2825 got_commitable_get_status(ct),
2826 got_commitable_get_path(ct));
2828 close(fd);
2830 if (stat(a->logmsg_path, &st) == -1) {
2831 err = got_error_from_errno2("stat", a->logmsg_path);
2832 goto done;
2835 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2836 err = got_error_from_errno("failed spawning editor");
2837 goto done;
2840 if (stat(a->logmsg_path, &st2) == -1) {
2841 err = got_error_from_errno("stat");
2842 goto done;
2845 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2846 unlink(a->logmsg_path);
2847 free(a->logmsg_path);
2848 a->logmsg_path = NULL;
2849 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2850 "no changes made to commit message, aborting");
2851 goto done;
2854 *logmsg = malloc(st2.st_size + 1);
2855 if (*logmsg == NULL) {
2856 err = got_error_from_errno("malloc");
2857 goto done;
2859 (*logmsg)[0] = '\0';
2860 len = 0;
2862 fp = fopen(a->logmsg_path, "r");
2863 if (fp == NULL) {
2864 err = got_error_from_errno("fopen");
2865 goto done;
2867 while (fgets(buf, sizeof(buf), fp) != NULL) {
2868 if (!content_changed && strcmp(buf, initial_content) != 0)
2869 content_changed = 1;
2870 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2871 continue; /* remove comments and leading empty lines */
2872 len = strlcat(*logmsg, buf, st2.st_size);
2874 fclose(fp);
2876 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2877 (*logmsg)[len - 1] = '\0';
2878 len--;
2881 if (len == 0 || !content_changed) {
2882 unlink(a->logmsg_path);
2883 free(a->logmsg_path);
2884 a->logmsg_path = NULL;
2885 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2886 "commit message cannot be empty, aborting");
2887 goto done;
2889 done:
2890 free(initial_content);
2891 free(template);
2893 /* Editor is done; we can now apply unveil(2) */
2894 if (err == NULL)
2895 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2896 return err;
2899 static const struct got_error *
2900 cmd_commit(int argc, char *argv[])
2902 const struct got_error *error = NULL;
2903 struct got_worktree *worktree = NULL;
2904 struct got_repository *repo = NULL;
2905 char *cwd = NULL, *path = NULL, *id_str = NULL;
2906 struct got_object_id *id = NULL;
2907 const char *logmsg = NULL;
2908 const char *got_author = getenv("GOT_AUTHOR");
2909 struct collect_commit_logmsg_arg cl_arg;
2910 char *editor = NULL;
2911 int ch, rebase_in_progress;
2913 while ((ch = getopt(argc, argv, "m:")) != -1) {
2914 switch (ch) {
2915 case 'm':
2916 logmsg = optarg;
2917 break;
2918 default:
2919 usage_commit();
2920 /* NOTREACHED */
2924 argc -= optind;
2925 argv += optind;
2927 if (argc == 1) {
2928 path = realpath(argv[0], NULL);
2929 if (path == NULL) {
2930 error = got_error_from_errno2("realpath", argv[0]);
2931 goto done;
2933 got_path_strip_trailing_slashes(path);
2934 } else if (argc != 0)
2935 usage_commit();
2937 if (got_author == NULL) {
2938 /* TODO: Look current user up in password database */
2939 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2940 goto done;
2943 cwd = getcwd(NULL, 0);
2944 if (cwd == NULL) {
2945 error = got_error_from_errno("getcwd");
2946 goto done;
2948 error = got_worktree_open(&worktree, cwd);
2949 if (error)
2950 goto done;
2952 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2953 if (error)
2954 goto done;
2955 if (rebase_in_progress) {
2956 error = got_error(GOT_ERR_REBASING);
2957 goto done;
2960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2961 if (error != NULL)
2962 goto done;
2965 * unveil(2) traverses exec(2); if an editor is used we have
2966 * to apply unveil after the log message has been written.
2968 if (logmsg == NULL || strlen(logmsg) == 0)
2969 error = get_editor(&editor);
2970 else
2971 error = apply_unveil(got_repo_get_path(repo), 0,
2972 got_worktree_get_root_path(worktree), 0);
2973 if (error)
2974 goto done;
2976 cl_arg.editor = editor;
2977 cl_arg.cmdline_log = logmsg;
2978 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2979 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2980 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2981 cl_arg.branch_name += 5;
2982 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2983 cl_arg.branch_name += 6;
2984 cl_arg.repo_path = got_repo_get_path(repo);
2985 cl_arg.logmsg_path = NULL;
2986 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2987 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2988 if (error) {
2989 if (cl_arg.logmsg_path)
2990 fprintf(stderr, "%s: log message preserved in %s\n",
2991 getprogname(), cl_arg.logmsg_path);
2992 goto done;
2995 if (cl_arg.logmsg_path)
2996 unlink(cl_arg.logmsg_path);
2998 error = got_object_id_str(&id_str, id);
2999 if (error)
3000 goto done;
3001 printf("Created commit %s\n", id_str);
3002 done:
3003 if (repo)
3004 got_repo_close(repo);
3005 if (worktree)
3006 got_worktree_close(worktree);
3007 free(path);
3008 free(cwd);
3009 free(id_str);
3010 free(editor);
3011 return error;
3014 __dead static void
3015 usage_cherrypick(void)
3017 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3018 exit(1);
3021 static const struct got_error *
3022 cmd_cherrypick(int argc, char *argv[])
3024 const struct got_error *error = NULL;
3025 struct got_worktree *worktree = NULL;
3026 struct got_repository *repo = NULL;
3027 char *cwd = NULL, *commit_id_str = NULL;
3028 struct got_object_id *commit_id = NULL;
3029 struct got_commit_object *commit = NULL;
3030 struct got_object_qid *pid;
3031 struct got_reference *head_ref = NULL;
3032 int ch, did_something = 0;
3034 while ((ch = getopt(argc, argv, "")) != -1) {
3035 switch (ch) {
3036 default:
3037 usage_cherrypick();
3038 /* NOTREACHED */
3042 argc -= optind;
3043 argv += optind;
3045 if (argc != 1)
3046 usage_cherrypick();
3048 cwd = getcwd(NULL, 0);
3049 if (cwd == NULL) {
3050 error = got_error_from_errno("getcwd");
3051 goto done;
3053 error = got_worktree_open(&worktree, cwd);
3054 if (error)
3055 goto done;
3057 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3058 if (error != NULL)
3059 goto done;
3061 error = apply_unveil(got_repo_get_path(repo), 0,
3062 got_worktree_get_root_path(worktree), 0);
3063 if (error)
3064 goto done;
3066 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3067 GOT_OBJ_TYPE_COMMIT, repo);
3068 if (error != NULL) {
3069 struct got_reference *ref;
3070 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3071 goto done;
3072 error = got_ref_open(&ref, repo, argv[0], 0);
3073 if (error != NULL)
3074 goto done;
3075 error = got_ref_resolve(&commit_id, repo, ref);
3076 got_ref_close(ref);
3077 if (error != NULL)
3078 goto done;
3080 error = got_object_id_str(&commit_id_str, commit_id);
3081 if (error)
3082 goto done;
3084 error = got_ref_open(&head_ref, repo,
3085 got_worktree_get_head_ref_name(worktree), 0);
3086 if (error != NULL)
3087 goto done;
3089 error = check_same_branch(commit_id, head_ref, repo);
3090 if (error) {
3091 if (error->code != GOT_ERR_ANCESTRY)
3092 goto done;
3093 error = NULL;
3094 } else {
3095 error = got_error(GOT_ERR_SAME_BRANCH);
3096 goto done;
3099 error = got_object_open_as_commit(&commit, repo, commit_id);
3100 if (error)
3101 goto done;
3102 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3103 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3104 commit_id, repo, update_progress, &did_something, check_cancelled,
3105 NULL);
3106 if (error != NULL)
3107 goto done;
3109 if (did_something)
3110 printf("Merged commit %s\n", commit_id_str);
3111 done:
3112 if (commit)
3113 got_object_commit_close(commit);
3114 free(commit_id_str);
3115 if (head_ref)
3116 got_ref_close(head_ref);
3117 if (worktree)
3118 got_worktree_close(worktree);
3119 if (repo)
3120 got_repo_close(repo);
3121 return error;
3124 __dead static void
3125 usage_backout(void)
3127 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3128 exit(1);
3131 static const struct got_error *
3132 cmd_backout(int argc, char *argv[])
3134 const struct got_error *error = NULL;
3135 struct got_worktree *worktree = NULL;
3136 struct got_repository *repo = NULL;
3137 char *cwd = NULL, *commit_id_str = NULL;
3138 struct got_object_id *commit_id = NULL;
3139 struct got_commit_object *commit = NULL;
3140 struct got_object_qid *pid;
3141 struct got_reference *head_ref = NULL;
3142 int ch, did_something = 0;
3144 while ((ch = getopt(argc, argv, "")) != -1) {
3145 switch (ch) {
3146 default:
3147 usage_backout();
3148 /* NOTREACHED */
3152 argc -= optind;
3153 argv += optind;
3155 if (argc != 1)
3156 usage_backout();
3158 cwd = getcwd(NULL, 0);
3159 if (cwd == NULL) {
3160 error = got_error_from_errno("getcwd");
3161 goto done;
3163 error = got_worktree_open(&worktree, cwd);
3164 if (error)
3165 goto done;
3167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3168 if (error != NULL)
3169 goto done;
3171 error = apply_unveil(got_repo_get_path(repo), 0,
3172 got_worktree_get_root_path(worktree), 0);
3173 if (error)
3174 goto done;
3176 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3177 GOT_OBJ_TYPE_COMMIT, repo);
3178 if (error != NULL) {
3179 struct got_reference *ref;
3180 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3181 goto done;
3182 error = got_ref_open(&ref, repo, argv[0], 0);
3183 if (error != NULL)
3184 goto done;
3185 error = got_ref_resolve(&commit_id, repo, ref);
3186 got_ref_close(ref);
3187 if (error != NULL)
3188 goto done;
3190 error = got_object_id_str(&commit_id_str, commit_id);
3191 if (error)
3192 goto done;
3194 error = got_ref_open(&head_ref, repo,
3195 got_worktree_get_head_ref_name(worktree), 0);
3196 if (error != NULL)
3197 goto done;
3199 error = check_same_branch(commit_id, head_ref, repo);
3200 if (error)
3201 goto done;
3203 error = got_object_open_as_commit(&commit, repo, commit_id);
3204 if (error)
3205 goto done;
3206 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3207 if (pid == NULL) {
3208 error = got_error(GOT_ERR_ROOT_COMMIT);
3209 goto done;
3212 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3213 update_progress, &did_something, check_cancelled, NULL);
3214 if (error != NULL)
3215 goto done;
3217 if (did_something)
3218 printf("Backed out commit %s\n", commit_id_str);
3219 done:
3220 if (commit)
3221 got_object_commit_close(commit);
3222 free(commit_id_str);
3223 if (head_ref)
3224 got_ref_close(head_ref);
3225 if (worktree)
3226 got_worktree_close(worktree);
3227 if (repo)
3228 got_repo_close(repo);
3229 return error;
3232 __dead static void
3233 usage_rebase(void)
3235 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3236 getprogname());
3237 exit(1);
3240 static const struct got_error *
3241 show_rebase_progress(struct got_commit_object *commit,
3242 struct got_object_id *old_id, struct got_object_id *new_id)
3244 const struct got_error *err;
3245 char *old_id_str = NULL, *new_id_str = NULL;
3246 char *logmsg0 = NULL, *logmsg, *nl;
3247 size_t len;
3249 err = got_object_id_str(&old_id_str, old_id);
3250 if (err)
3251 goto done;
3253 if (new_id) {
3254 err = got_object_id_str(&new_id_str, new_id);
3255 if (err)
3256 goto done;
3259 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3260 if (logmsg0 == NULL) {
3261 err = got_error_from_errno("strdup");
3262 goto done;
3264 logmsg = logmsg0;
3266 while (isspace((unsigned char)logmsg[0]))
3267 logmsg++;
3269 old_id_str[12] = '\0';
3270 if (new_id_str)
3271 new_id_str[12] = '\0';
3272 len = strlen(logmsg);
3273 if (len > 42)
3274 len = 42;
3275 logmsg[len] = '\0';
3276 nl = strchr(logmsg, '\n');
3277 if (nl)
3278 *nl = '\0';
3279 printf("%s -> %s: %s\n", old_id_str,
3280 new_id_str ? new_id_str : "no-op change", logmsg);
3281 done:
3282 free(old_id_str);
3283 free(new_id_str);
3284 free(logmsg0);
3285 return err;
3288 static void
3289 rebase_progress(void *arg, unsigned char status, const char *path)
3291 unsigned char *rebase_status = arg;
3293 while (path[0] == '/')
3294 path++;
3295 printf("%c %s\n", status, path);
3297 if (*rebase_status == GOT_STATUS_CONFLICT)
3298 return;
3299 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3300 *rebase_status = status;
3303 static const struct got_error *
3304 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3305 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3306 struct got_repository *repo)
3308 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3309 return got_worktree_rebase_complete(worktree,
3310 new_base_branch, tmp_branch, branch, repo);
3313 static const struct got_error *
3314 rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
3315 struct got_object_id *commit_id, struct got_repository *repo)
3317 const struct got_error *error;
3318 struct got_commit_object *commit;
3319 struct got_object_id *new_commit_id;
3321 error = got_object_open_as_commit(&commit, repo, commit_id);
3322 if (error)
3323 return error;
3325 error = got_worktree_rebase_commit(&new_commit_id, worktree,
3326 tmp_branch, commit, commit_id, repo);
3327 if (error) {
3328 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3329 goto done;
3330 error = show_rebase_progress(commit, commit_id, NULL);
3331 } else {
3332 error = show_rebase_progress(commit, commit_id, new_commit_id);
3333 free(new_commit_id);
3335 done:
3336 got_object_commit_close(commit);
3337 return error;
3340 struct check_path_prefix_arg {
3341 const char *path_prefix;
3342 size_t len;
3345 static const struct got_error *
3346 check_path_prefix(void *arg, struct got_blob_object *blob1,
3347 struct got_blob_object *blob2, struct got_object_id *id1,
3348 struct got_object_id *id2, const char *path1, const char *path2,
3349 struct got_repository *repo)
3351 struct check_path_prefix_arg *a = arg;
3353 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3354 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3355 return got_error(GOT_ERR_REBASE_PATH);
3357 return NULL;
3360 static const struct got_error *
3361 rebase_check_path_prefix(struct got_object_id *parent_id,
3362 struct got_object_id *commit_id, const char *path_prefix,
3363 struct got_repository *repo)
3365 const struct got_error *err;
3366 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3367 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3368 struct check_path_prefix_arg cpp_arg;
3370 if (got_path_is_root_dir(path_prefix))
3371 return NULL;
3373 err = got_object_open_as_commit(&commit, repo, commit_id);
3374 if (err)
3375 goto done;
3377 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3378 if (err)
3379 goto done;
3381 err = got_object_open_as_tree(&tree1, repo,
3382 got_object_commit_get_tree_id(parent_commit));
3383 if (err)
3384 goto done;
3386 err = got_object_open_as_tree(&tree2, repo,
3387 got_object_commit_get_tree_id(commit));
3388 if (err)
3389 goto done;
3391 cpp_arg.path_prefix = path_prefix;
3392 cpp_arg.len = strlen(path_prefix);
3393 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3394 &cpp_arg);
3395 done:
3396 if (tree1)
3397 got_object_tree_close(tree1);
3398 if (tree2)
3399 got_object_tree_close(tree2);
3400 if (commit)
3401 got_object_commit_close(commit);
3402 if (parent_commit)
3403 got_object_commit_close(parent_commit);
3404 return err;
3407 static const struct got_error *
3408 cmd_rebase(int argc, char *argv[])
3410 const struct got_error *error = NULL;
3411 struct got_worktree *worktree = NULL;
3412 struct got_repository *repo = NULL;
3413 char *cwd = NULL;
3414 struct got_reference *branch = NULL;
3415 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3416 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3417 struct got_object_id *resume_commit_id = NULL;
3418 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3419 struct got_commit_graph *graph = NULL;
3420 struct got_commit_object *commit = NULL;
3421 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3422 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3423 struct got_object_id_queue commits;
3424 const struct got_object_id_queue *parent_ids;
3425 struct got_object_qid *qid, *pid;
3427 SIMPLEQ_INIT(&commits);
3429 while ((ch = getopt(argc, argv, "ac")) != -1) {
3430 switch (ch) {
3431 case 'a':
3432 abort_rebase = 1;
3433 break;
3434 case 'c':
3435 continue_rebase = 1;
3436 break;
3437 default:
3438 usage_rebase();
3439 /* NOTREACHED */
3443 argc -= optind;
3444 argv += optind;
3446 if (abort_rebase && continue_rebase)
3447 usage_rebase();
3448 else if (abort_rebase || continue_rebase) {
3449 if (argc != 0)
3450 usage_rebase();
3451 } else if (argc != 1)
3452 usage_rebase();
3454 cwd = getcwd(NULL, 0);
3455 if (cwd == NULL) {
3456 error = got_error_from_errno("getcwd");
3457 goto done;
3459 error = got_worktree_open(&worktree, cwd);
3460 if (error)
3461 goto done;
3463 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3464 if (error != NULL)
3465 goto done;
3467 error = apply_unveil(got_repo_get_path(repo), 0,
3468 got_worktree_get_root_path(worktree), 0);
3469 if (error)
3470 goto done;
3472 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3473 if (error)
3474 goto done;
3476 if (rebase_in_progress && abort_rebase) {
3477 int did_something;
3478 error = got_worktree_rebase_continue(&resume_commit_id,
3479 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3480 if (error)
3481 goto done;
3482 printf("Switching work tree to %s\n",
3483 got_ref_get_symref_target(new_base_branch));
3484 error = got_worktree_rebase_abort(worktree, repo,
3485 new_base_branch, update_progress, &did_something);
3486 if (error)
3487 goto done;
3488 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3489 goto done; /* nothing else to do */
3490 } else if (abort_rebase) {
3491 error = got_error(GOT_ERR_NOT_REBASING);
3492 goto done;
3495 if (continue_rebase) {
3496 error = got_worktree_rebase_continue(&resume_commit_id,
3497 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3498 if (error)
3499 goto done;
3501 error = rebase_commit(worktree, tmp_branch, resume_commit_id,
3502 repo);
3503 if (error)
3504 goto done;
3506 yca_id = got_object_id_dup(resume_commit_id);
3507 if (yca_id == NULL) {
3508 error = got_error_from_errno("got_object_id_dup");
3509 goto done;
3511 } else {
3512 error = got_ref_open(&branch, repo, argv[0], 0);
3513 if (error != NULL)
3514 goto done;
3516 error = check_same_branch(
3517 got_worktree_get_base_commit_id(worktree), branch, repo);
3518 if (error) {
3519 if (error->code != GOT_ERR_ANCESTRY)
3520 goto done;
3521 error = NULL;
3522 } else {
3523 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3524 "specified branch resolves to a commit which "
3525 "is already contained in work tree's branch");
3526 goto done;
3530 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3531 if (error)
3532 goto done;
3534 if (!continue_rebase) {
3535 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3536 got_worktree_get_base_commit_id(worktree),
3537 branch_head_commit_id, repo);
3538 if (error)
3539 goto done;
3540 if (yca_id == NULL) {
3541 error = got_error_msg(GOT_ERR_ANCESTRY,
3542 "specified branch shares no common ancestry "
3543 "with work tree's branch");
3544 goto done;
3547 error = got_worktree_rebase_prepare(&new_base_branch,
3548 &tmp_branch, worktree, branch, repo);
3549 if (error)
3550 goto done;
3553 commit_id = branch_head_commit_id;
3554 error = got_object_open_as_commit(&commit, repo, commit_id);
3555 if (error)
3556 goto done;
3558 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3559 if (error)
3560 goto done;
3561 parent_ids = got_object_commit_get_parent_ids(commit);
3562 pid = SIMPLEQ_FIRST(parent_ids);
3563 error = got_commit_graph_iter_start(graph, pid->id, repo);
3564 got_object_commit_close(commit);
3565 commit = NULL;
3566 if (error)
3567 goto done;
3568 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3569 error = got_commit_graph_iter_next(&parent_id, graph);
3570 if (error) {
3571 if (error->code == GOT_ERR_ITER_COMPLETED) {
3572 error = got_error_msg(GOT_ERR_ANCESTRY,
3573 "ran out of commits to rebase before "
3574 "youngest common ancestor commit has "
3575 "been reached?!?");
3576 goto done;
3577 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3578 goto done;
3579 error = got_commit_graph_fetch_commits(graph, 1, repo);
3580 if (error)
3581 goto done;
3582 } else {
3583 error = rebase_check_path_prefix(parent_id, commit_id,
3584 got_worktree_get_path_prefix(worktree), repo);
3585 if (error)
3586 goto done;
3588 error = got_object_qid_alloc(&qid, commit_id);
3589 if (error)
3590 goto done;
3591 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3592 commit_id = parent_id;
3596 if (SIMPLEQ_EMPTY(&commits)) {
3597 if (continue_rebase)
3598 error = rebase_complete(worktree, branch,
3599 new_base_branch, tmp_branch, repo);
3600 else
3601 error = got_error(GOT_ERR_EMPTY_REBASE);
3602 goto done;
3605 pid = NULL;
3606 SIMPLEQ_FOREACH(qid, &commits, entry) {
3607 commit_id = qid->id;
3608 parent_id = pid ? pid->id : yca_id;
3609 pid = qid;
3611 error = got_worktree_rebase_merge_files(worktree, parent_id,
3612 commit_id, repo, rebase_progress, &rebase_status,
3613 check_cancelled, NULL);
3614 if (error)
3615 goto done;
3617 if (rebase_status == GOT_STATUS_CONFLICT)
3618 break;
3620 error = rebase_commit(worktree, tmp_branch, commit_id, repo);
3621 if (error)
3622 goto done;
3625 if (rebase_status == GOT_STATUS_CONFLICT) {
3626 error = got_worktree_rebase_postpone(worktree);
3627 if (error)
3628 goto done;
3629 error = got_error_msg(GOT_ERR_CONFLICTS,
3630 "conflicts must be resolved before rebase can be resumed");
3631 } else
3632 error = rebase_complete(worktree, branch, new_base_branch,
3633 tmp_branch, repo);
3634 done:
3635 got_object_id_queue_free(&commits);
3636 free(branch_head_commit_id);
3637 free(resume_commit_id);
3638 free(yca_id);
3639 if (graph)
3640 got_commit_graph_close(graph);
3641 if (commit)
3642 got_object_commit_close(commit);
3643 if (branch)
3644 got_ref_close(branch);
3645 if (new_base_branch)
3646 got_ref_close(new_base_branch);
3647 if (tmp_branch)
3648 got_ref_close(tmp_branch);
3649 if (worktree)
3650 got_worktree_close(worktree);
3651 if (repo)
3652 got_repo_close(repo);
3653 return error;