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 const struct got_error *
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 NULL;
354 while (path[0] == '/')
355 path++;
357 printf("%c %s/%s\n", status, worktree_path, path);
358 return NULL;
361 static const struct got_error *
362 check_cancelled(void *arg)
364 if (sigint_received || sigpipe_received)
365 return got_error(GOT_ERR_CANCELLED);
366 return NULL;
369 static const struct got_error *
370 check_linear_ancestry(struct got_object_id *commit_id,
371 struct got_object_id *base_commit_id, struct got_repository *repo)
373 const struct got_error *err = NULL;
374 struct got_object_id *yca_id;
376 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
377 commit_id, base_commit_id, repo);
378 if (err)
379 return err;
381 if (yca_id == NULL)
382 return got_error(GOT_ERR_ANCESTRY);
384 /*
385 * Require a straight line of history between the target commit
386 * and the work tree's base commit.
388 * Non-linear situations such as this require a rebase:
390 * (commit) D F (base_commit)
391 * \ /
392 * C E
393 * \ /
394 * B (yca)
395 * |
396 * A
398 * 'got update' only handles linear cases:
399 * Update forwards in time: A (base/yca) - B - C - D (commit)
400 * Update backwards in time: D (base) - C - B - A (commit/yca)
401 */
402 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
403 got_object_id_cmp(base_commit_id, yca_id) != 0)
404 return got_error(GOT_ERR_ANCESTRY);
406 free(yca_id);
407 return NULL;
410 static const struct got_error *
411 check_same_branch(struct got_object_id *commit_id,
412 struct got_reference *head_ref, struct got_repository *repo)
414 const struct got_error *err = NULL;
415 struct got_commit_graph *graph = NULL;
416 struct got_object_id *head_commit_id = NULL;
417 int is_same_branch = 0;
419 err = got_ref_resolve(&head_commit_id, repo, head_ref);
420 if (err)
421 goto done;
423 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
424 if (err)
425 goto done;
427 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
428 if (err)
429 goto done;
431 for (;;) {
432 struct got_object_id *id;
433 err = got_commit_graph_iter_next(&id, graph);
434 if (err) {
435 if (err->code == GOT_ERR_ITER_COMPLETED) {
436 err = NULL;
437 break;
439 else if (err->code != GOT_ERR_ITER_NEED_MORE)
440 break;
441 err = got_commit_graph_fetch_commits(graph, 1,
442 repo);
443 if (err)
444 break;
447 if (id) {
448 if (got_object_id_cmp(id, commit_id) == 0) {
449 is_same_branch = 1;
450 break;
454 done:
455 if (graph)
456 got_commit_graph_close(graph);
457 free(head_commit_id);
458 if (!err && !is_same_branch)
459 err = got_error(GOT_ERR_ANCESTRY);
460 return err;
463 static const struct got_error *
464 cmd_checkout(int argc, char *argv[])
466 const struct got_error *error = NULL;
467 struct got_repository *repo = NULL;
468 struct got_reference *head_ref = NULL;
469 struct got_worktree *worktree = NULL;
470 char *repo_path = NULL;
471 char *worktree_path = NULL;
472 const char *path_prefix = "";
473 const char *branch_name = GOT_REF_HEAD;
474 char *commit_id_str = NULL;
475 int ch, same_path_prefix;
477 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
478 switch (ch) {
479 case 'b':
480 branch_name = optarg;
481 break;
482 case 'c':
483 commit_id_str = strdup(optarg);
484 if (commit_id_str == NULL)
485 return got_error_from_errno("strdup");
486 break;
487 case 'p':
488 path_prefix = optarg;
489 break;
490 default:
491 usage_checkout();
492 /* NOTREACHED */
496 argc -= optind;
497 argv += optind;
499 #ifndef PROFILE
500 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
501 "unveil", NULL) == -1)
502 err(1, "pledge");
503 #endif
504 if (argc == 1) {
505 char *cwd, *base, *dotgit;
506 repo_path = realpath(argv[0], NULL);
507 if (repo_path == NULL)
508 return got_error_from_errno2("realpath", argv[0]);
509 cwd = getcwd(NULL, 0);
510 if (cwd == NULL) {
511 error = got_error_from_errno("getcwd");
512 goto done;
514 if (path_prefix[0]) {
515 base = basename(path_prefix);
516 if (base == NULL) {
517 error = got_error_from_errno2("basename",
518 path_prefix);
519 goto done;
521 } else {
522 base = basename(repo_path);
523 if (base == NULL) {
524 error = got_error_from_errno2("basename",
525 repo_path);
526 goto done;
529 dotgit = strstr(base, ".git");
530 if (dotgit)
531 *dotgit = '\0';
532 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
533 error = got_error_from_errno("asprintf");
534 free(cwd);
535 goto done;
537 free(cwd);
538 } else if (argc == 2) {
539 repo_path = realpath(argv[0], NULL);
540 if (repo_path == NULL) {
541 error = got_error_from_errno2("realpath", argv[0]);
542 goto done;
544 worktree_path = realpath(argv[1], NULL);
545 if (worktree_path == NULL) {
546 error = got_error_from_errno2("realpath", argv[1]);
547 goto done;
549 } else
550 usage_checkout();
552 got_path_strip_trailing_slashes(repo_path);
553 got_path_strip_trailing_slashes(worktree_path);
555 error = got_repo_open(&repo, repo_path);
556 if (error != NULL)
557 goto done;
559 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
560 if (error)
561 goto done;
563 error = got_ref_open(&head_ref, repo, branch_name, 0);
564 if (error != NULL)
565 goto done;
567 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
568 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
569 goto done;
571 error = got_worktree_open(&worktree, worktree_path);
572 if (error != NULL)
573 goto done;
575 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
576 path_prefix);
577 if (error != NULL)
578 goto done;
579 if (!same_path_prefix) {
580 error = got_error(GOT_ERR_PATH_PREFIX);
581 goto done;
584 if (commit_id_str) {
585 struct got_object_id *commit_id;
586 error = got_repo_match_object_id_prefix(&commit_id,
587 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
588 if (error != NULL)
589 goto done;
590 error = check_linear_ancestry(commit_id,
591 got_worktree_get_base_commit_id(worktree), repo);
592 if (error != NULL) {
593 free(commit_id);
594 goto done;
596 error = check_same_branch(commit_id, head_ref, repo);
597 if (error)
598 goto done;
599 error = got_worktree_set_base_commit_id(worktree, repo,
600 commit_id);
601 free(commit_id);
602 if (error)
603 goto done;
606 error = got_worktree_checkout_files(worktree, "", repo,
607 checkout_progress, worktree_path, check_cancelled, NULL);
608 if (error != NULL)
609 goto done;
611 printf("Now shut up and hack\n");
613 done:
614 free(commit_id_str);
615 free(repo_path);
616 free(worktree_path);
617 return error;
620 __dead static void
621 usage_update(void)
623 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
624 getprogname());
625 exit(1);
628 static const struct got_error *
629 update_progress(void *arg, unsigned char status, const char *path)
631 int *did_something = arg;
633 if (status == GOT_STATUS_EXISTS)
634 return NULL;
636 *did_something = 1;
638 /* Base commit bump happens silently. */
639 if (status == GOT_STATUS_BUMP_BASE)
640 return NULL;
642 while (path[0] == '/')
643 path++;
644 printf("%c %s\n", status, path);
645 return NULL;
648 static const struct got_error *
649 switch_head_ref(struct got_reference *head_ref,
650 struct got_object_id *commit_id, struct got_worktree *worktree,
651 struct got_repository *repo)
653 const struct got_error *err = NULL;
654 char *base_id_str;
655 int ref_has_moved = 0;
657 /* Trivial case: switching between two different references. */
658 if (strcmp(got_ref_get_name(head_ref),
659 got_worktree_get_head_ref_name(worktree)) != 0) {
660 printf("Switching work tree from %s to %s\n",
661 got_worktree_get_head_ref_name(worktree),
662 got_ref_get_name(head_ref));
663 return got_worktree_set_head_ref(worktree, head_ref);
666 err = check_linear_ancestry(commit_id,
667 got_worktree_get_base_commit_id(worktree), repo);
668 if (err) {
669 if (err->code != GOT_ERR_ANCESTRY)
670 return err;
671 ref_has_moved = 1;
673 if (!ref_has_moved)
674 return NULL;
676 /* Switching to a rebased branch with the same reference name. */
677 err = got_object_id_str(&base_id_str,
678 got_worktree_get_base_commit_id(worktree));
679 if (err)
680 return err;
681 printf("Reference %s now points at a different branch\n",
682 got_worktree_get_head_ref_name(worktree));
683 printf("Switching work tree from %s to %s\n", base_id_str,
684 got_worktree_get_head_ref_name(worktree));
685 return NULL;
688 static const struct got_error *
689 cmd_update(int argc, char *argv[])
691 const struct got_error *error = NULL;
692 struct got_repository *repo = NULL;
693 struct got_worktree *worktree = NULL;
694 char *worktree_path = NULL, *path = NULL;
695 struct got_object_id *commit_id = NULL;
696 char *commit_id_str = NULL;
697 const char *branch_name = NULL;
698 struct got_reference *head_ref = NULL;
699 int ch, did_something = 0, rebase_in_progress;
701 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
702 switch (ch) {
703 case 'b':
704 branch_name = optarg;
705 break;
706 case 'c':
707 commit_id_str = strdup(optarg);
708 if (commit_id_str == NULL)
709 return got_error_from_errno("strdup");
710 break;
711 default:
712 usage_update();
713 /* NOTREACHED */
717 argc -= optind;
718 argv += optind;
720 #ifndef PROFILE
721 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
722 "unveil", NULL) == -1)
723 err(1, "pledge");
724 #endif
725 worktree_path = getcwd(NULL, 0);
726 if (worktree_path == NULL) {
727 error = got_error_from_errno("getcwd");
728 goto done;
730 error = got_worktree_open(&worktree, worktree_path);
731 if (error)
732 goto done;
734 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
735 if (error)
736 goto done;
737 if (rebase_in_progress) {
738 error = got_error(GOT_ERR_REBASING);
739 goto done;
742 if (argc == 0) {
743 path = strdup("");
744 if (path == NULL) {
745 error = got_error_from_errno("strdup");
746 goto done;
748 } else if (argc == 1) {
749 error = got_worktree_resolve_path(&path, worktree, argv[0]);
750 if (error)
751 goto done;
752 } else
753 usage_update();
755 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
756 if (error != NULL)
757 goto done;
759 error = apply_unveil(got_repo_get_path(repo), 0,
760 got_worktree_get_root_path(worktree), 0);
761 if (error)
762 goto done;
764 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
765 got_worktree_get_head_ref_name(worktree), 0);
766 if (error != NULL)
767 goto done;
768 if (commit_id_str == NULL) {
769 error = got_ref_resolve(&commit_id, repo, head_ref);
770 if (error != NULL)
771 goto done;
772 error = got_object_id_str(&commit_id_str, commit_id);
773 if (error != NULL)
774 goto done;
775 } else {
776 error = got_repo_match_object_id_prefix(&commit_id,
777 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
778 if (error != NULL)
779 goto done;
780 free(commit_id_str);
781 error = got_object_id_str(&commit_id_str, commit_id);
782 if (error)
783 goto done;
786 if (branch_name) {
787 struct got_object_id *head_commit_id;
788 if (strlen(path) != 0) {
789 fprintf(stderr, "%s: switching between branches "
790 "requires that the entire work tree "
791 "gets updated, not just '%s'\n",
792 getprogname(), path);
793 error = got_error(GOT_ERR_BAD_PATH);
794 goto done;
796 error = got_ref_resolve(&head_commit_id, repo, head_ref);
797 if (error)
798 goto done;
799 error = check_linear_ancestry(commit_id, head_commit_id, repo);
800 free(head_commit_id);
801 if (error != NULL)
802 goto done;
803 error = check_same_branch(commit_id, head_ref, repo);
804 if (error)
805 goto done;
806 error = switch_head_ref(head_ref, commit_id, worktree, repo);
807 if (error)
808 goto done;
809 } else {
810 error = check_linear_ancestry(commit_id,
811 got_worktree_get_base_commit_id(worktree), repo);
812 if (error != NULL) {
813 if (error->code == GOT_ERR_ANCESTRY)
814 error = got_error(GOT_ERR_BRANCH_MOVED);
815 goto done;
817 error = check_same_branch(commit_id, head_ref, repo);
818 if (error)
819 goto done;
822 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
823 commit_id) != 0) {
824 error = got_worktree_set_base_commit_id(worktree, repo,
825 commit_id);
826 if (error)
827 goto done;
830 error = got_worktree_checkout_files(worktree, path, repo,
831 update_progress, &did_something, check_cancelled, NULL);
832 if (error != NULL)
833 goto done;
835 if (did_something)
836 printf("Updated to commit %s\n", commit_id_str);
837 else
838 printf("Already up-to-date\n");
839 done:
840 free(worktree_path);
841 free(path);
842 free(commit_id);
843 free(commit_id_str);
844 return error;
847 static const struct got_error *
848 print_patch(struct got_commit_object *commit, struct got_object_id *id,
849 int diff_context, struct got_repository *repo)
851 const struct got_error *err = NULL;
852 struct got_tree_object *tree1 = NULL, *tree2;
853 struct got_object_qid *qid;
854 char *id_str1 = NULL, *id_str2;
855 struct got_diff_blob_output_unidiff_arg arg;
857 err = got_object_open_as_tree(&tree2, repo,
858 got_object_commit_get_tree_id(commit));
859 if (err)
860 return err;
862 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
863 if (qid != NULL) {
864 struct got_commit_object *pcommit;
866 err = got_object_open_as_commit(&pcommit, repo, qid->id);
867 if (err)
868 return err;
870 err = got_object_open_as_tree(&tree1, repo,
871 got_object_commit_get_tree_id(pcommit));
872 got_object_commit_close(pcommit);
873 if (err)
874 return err;
876 err = got_object_id_str(&id_str1, qid->id);
877 if (err)
878 return err;
881 err = got_object_id_str(&id_str2, id);
882 if (err)
883 goto done;
885 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
886 arg.diff_context = diff_context;
887 arg.outfile = stdout;
888 err = got_diff_tree(tree1, tree2, "", "", repo,
889 got_diff_blob_output_unidiff, &arg);
890 done:
891 if (tree1)
892 got_object_tree_close(tree1);
893 got_object_tree_close(tree2);
894 free(id_str1);
895 free(id_str2);
896 return err;
899 static char *
900 get_datestr(time_t *time, char *datebuf)
902 char *p, *s = ctime_r(time, datebuf);
903 p = strchr(s, '\n');
904 if (p)
905 *p = '\0';
906 return s;
909 static const struct got_error *
910 print_commit(struct got_commit_object *commit, struct got_object_id *id,
911 struct got_repository *repo, int show_patch, int diff_context,
912 struct got_reflist_head *refs)
914 const struct got_error *err = NULL;
915 char *id_str, *datestr, *logmsg0, *logmsg, *line;
916 char datebuf[26];
917 time_t committer_time;
918 const char *author, *committer;
919 char *refs_str = NULL;
920 struct got_reflist_entry *re;
922 SIMPLEQ_FOREACH(re, refs, entry) {
923 char *s;
924 const char *name;
925 if (got_object_id_cmp(re->id, id) != 0)
926 continue;
927 name = got_ref_get_name(re->ref);
928 if (strcmp(name, GOT_REF_HEAD) == 0)
929 continue;
930 if (strncmp(name, "refs/", 5) == 0)
931 name += 5;
932 if (strncmp(name, "got/", 4) == 0)
933 continue;
934 if (strncmp(name, "heads/", 6) == 0)
935 name += 6;
936 if (strncmp(name, "remotes/", 8) == 0)
937 name += 8;
938 s = refs_str;
939 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
940 name) == -1) {
941 err = got_error_from_errno("asprintf");
942 free(s);
943 break;
945 free(s);
947 err = got_object_id_str(&id_str, id);
948 if (err)
949 return err;
951 printf("-----------------------------------------------\n");
952 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
953 refs_str ? refs_str : "", refs_str ? ")" : "");
954 free(id_str);
955 id_str = NULL;
956 free(refs_str);
957 refs_str = NULL;
958 printf("from: %s\n", got_object_commit_get_author(commit));
959 committer_time = got_object_commit_get_committer_time(commit);
960 datestr = get_datestr(&committer_time, datebuf);
961 printf("date: %s UTC\n", datestr);
962 author = got_object_commit_get_author(commit);
963 committer = got_object_commit_get_committer(commit);
964 if (strcmp(author, committer) != 0)
965 printf("via: %s\n", committer);
966 if (got_object_commit_get_nparents(commit) > 1) {
967 const struct got_object_id_queue *parent_ids;
968 struct got_object_qid *qid;
969 int n = 1;
970 parent_ids = got_object_commit_get_parent_ids(commit);
971 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
972 err = got_object_id_str(&id_str, qid->id);
973 if (err)
974 return err;
975 printf("parent %d: %s\n", n++, id_str);
976 free(id_str);
980 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
981 if (logmsg0 == NULL)
982 return got_error_from_errno("strdup");
984 logmsg = logmsg0;
985 do {
986 line = strsep(&logmsg, "\n");
987 if (line)
988 printf(" %s\n", line);
989 } while (line);
990 free(logmsg0);
992 if (show_patch) {
993 err = print_patch(commit, id, diff_context, repo);
994 if (err == 0)
995 printf("\n");
998 if (fflush(stdout) != 0 && err == NULL)
999 err = got_error_from_errno("fflush");
1000 return err;
1003 static const struct got_error *
1004 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1005 char *path, int show_patch, int diff_context, int limit,
1006 int first_parent_traversal, struct got_reflist_head *refs)
1008 const struct got_error *err;
1009 struct got_commit_graph *graph;
1011 err = got_commit_graph_open(&graph, root_id, path,
1012 first_parent_traversal, repo);
1013 if (err)
1014 return err;
1015 err = got_commit_graph_iter_start(graph, root_id, repo);
1016 if (err)
1017 goto done;
1018 for (;;) {
1019 struct got_commit_object *commit;
1020 struct got_object_id *id;
1022 if (sigint_received || sigpipe_received)
1023 break;
1025 err = got_commit_graph_iter_next(&id, graph);
1026 if (err) {
1027 if (err->code == GOT_ERR_ITER_COMPLETED) {
1028 err = NULL;
1029 break;
1031 if (err->code != GOT_ERR_ITER_NEED_MORE)
1032 break;
1033 err = got_commit_graph_fetch_commits(graph, 1, repo);
1034 if (err)
1035 break;
1036 else
1037 continue;
1039 if (id == NULL)
1040 break;
1042 err = got_object_open_as_commit(&commit, repo, id);
1043 if (err)
1044 break;
1045 err = print_commit(commit, id, repo, show_patch, diff_context,
1046 refs);
1047 got_object_commit_close(commit);
1048 if (err || (limit && --limit == 0))
1049 break;
1051 done:
1052 got_commit_graph_close(graph);
1053 return err;
1056 __dead static void
1057 usage_log(void)
1059 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1060 "[-r repository-path] [path]\n", getprogname());
1061 exit(1);
1064 static const struct got_error *
1065 cmd_log(int argc, char *argv[])
1067 const struct got_error *error;
1068 struct got_repository *repo = NULL;
1069 struct got_worktree *worktree = NULL;
1070 struct got_commit_object *commit = NULL;
1071 struct got_object_id *id = NULL;
1072 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1073 char *start_commit = NULL;
1074 int diff_context = 3, ch;
1075 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1076 const char *errstr;
1077 struct got_reflist_head refs;
1079 SIMPLEQ_INIT(&refs);
1081 #ifndef PROFILE
1082 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1083 NULL)
1084 == -1)
1085 err(1, "pledge");
1086 #endif
1088 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1089 switch (ch) {
1090 case 'b':
1091 first_parent_traversal = 1;
1092 break;
1093 case 'p':
1094 show_patch = 1;
1095 break;
1096 case 'c':
1097 start_commit = optarg;
1098 break;
1099 case 'C':
1100 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1101 &errstr);
1102 if (errstr != NULL)
1103 err(1, "-C option %s", errstr);
1104 break;
1105 case 'l':
1106 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1107 if (errstr != NULL)
1108 err(1, "-l option %s", errstr);
1109 break;
1110 case 'r':
1111 repo_path = realpath(optarg, NULL);
1112 if (repo_path == NULL)
1113 err(1, "-r option");
1114 got_path_strip_trailing_slashes(repo_path);
1115 break;
1116 default:
1117 usage_log();
1118 /* NOTREACHED */
1122 argc -= optind;
1123 argv += optind;
1125 cwd = getcwd(NULL, 0);
1126 if (cwd == NULL) {
1127 error = got_error_from_errno("getcwd");
1128 goto done;
1131 error = got_worktree_open(&worktree, cwd);
1132 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1133 goto done;
1134 error = NULL;
1136 if (argc == 0) {
1137 path = strdup("");
1138 if (path == NULL) {
1139 error = got_error_from_errno("strdup");
1140 goto done;
1142 } else if (argc == 1) {
1143 if (worktree) {
1144 error = got_worktree_resolve_path(&path, worktree,
1145 argv[0]);
1146 if (error)
1147 goto done;
1148 } else {
1149 path = strdup(argv[0]);
1150 if (path == NULL) {
1151 error = got_error_from_errno("strdup");
1152 goto done;
1155 } else
1156 usage_log();
1158 if (repo_path == NULL) {
1159 repo_path = worktree ?
1160 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1162 if (repo_path == NULL) {
1163 error = got_error_from_errno("strdup");
1164 goto done;
1167 error = got_repo_open(&repo, repo_path);
1168 if (error != NULL)
1169 goto done;
1171 error = apply_unveil(got_repo_get_path(repo), 1,
1172 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1173 if (error)
1174 goto done;
1176 if (start_commit == NULL) {
1177 struct got_reference *head_ref;
1178 error = got_ref_open(&head_ref, repo,
1179 worktree ? got_worktree_get_head_ref_name(worktree)
1180 : GOT_REF_HEAD, 0);
1181 if (error != NULL)
1182 return error;
1183 error = got_ref_resolve(&id, repo, head_ref);
1184 got_ref_close(head_ref);
1185 if (error != NULL)
1186 return error;
1187 error = got_object_open_as_commit(&commit, repo, id);
1188 } else {
1189 struct got_reference *ref;
1190 error = got_ref_open(&ref, repo, start_commit, 0);
1191 if (error == NULL) {
1192 int obj_type;
1193 error = got_ref_resolve(&id, repo, ref);
1194 got_ref_close(ref);
1195 if (error != NULL)
1196 goto done;
1197 error = got_object_get_type(&obj_type, repo, id);
1198 if (error != NULL)
1199 goto done;
1200 if (obj_type == GOT_OBJ_TYPE_TAG) {
1201 struct got_tag_object *tag;
1202 error = got_object_open_as_tag(&tag, repo, id);
1203 if (error != NULL)
1204 goto done;
1205 if (got_object_tag_get_object_type(tag) !=
1206 GOT_OBJ_TYPE_COMMIT) {
1207 got_object_tag_close(tag);
1208 error = got_error(GOT_ERR_OBJ_TYPE);
1209 goto done;
1211 free(id);
1212 id = got_object_id_dup(
1213 got_object_tag_get_object_id(tag));
1214 if (id == NULL)
1215 error = got_error_from_errno(
1216 "got_object_id_dup");
1217 got_object_tag_close(tag);
1218 if (error)
1219 goto done;
1220 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1221 error = got_error(GOT_ERR_OBJ_TYPE);
1222 goto done;
1224 error = got_object_open_as_commit(&commit, repo, id);
1225 if (error != NULL)
1226 goto done;
1228 if (commit == NULL) {
1229 error = got_repo_match_object_id_prefix(&id,
1230 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1231 if (error != NULL)
1232 return error;
1235 if (error != NULL)
1236 goto done;
1238 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1239 if (error != NULL)
1240 goto done;
1241 if (in_repo_path) {
1242 free(path);
1243 path = in_repo_path;
1246 error = got_ref_list(&refs, repo);
1247 if (error)
1248 goto done;
1250 error = print_commits(id, repo, path, show_patch,
1251 diff_context, limit, first_parent_traversal, &refs);
1252 done:
1253 free(path);
1254 free(repo_path);
1255 free(cwd);
1256 free(id);
1257 if (worktree)
1258 got_worktree_close(worktree);
1259 if (repo) {
1260 const struct got_error *repo_error;
1261 repo_error = got_repo_close(repo);
1262 if (error == NULL)
1263 error = repo_error;
1265 got_ref_list_free(&refs);
1266 return error;
1269 __dead static void
1270 usage_diff(void)
1272 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1273 "[object1 object2 | path]\n", getprogname());
1274 exit(1);
1277 struct print_diff_arg {
1278 struct got_repository *repo;
1279 struct got_worktree *worktree;
1280 int diff_context;
1281 const char *id_str;
1282 int header_shown;
1285 static const struct got_error *
1286 print_diff(void *arg, unsigned char status, const char *path,
1287 struct got_object_id *blob_id, struct got_object_id *commit_id)
1289 struct print_diff_arg *a = arg;
1290 const struct got_error *err = NULL;
1291 struct got_blob_object *blob1 = NULL;
1292 FILE *f2 = NULL;
1293 char *abspath = NULL;
1294 struct stat sb;
1296 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1297 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1298 return NULL;
1300 if (!a->header_shown) {
1301 printf("diff %s %s\n", a->id_str,
1302 got_worktree_get_root_path(a->worktree));
1303 a->header_shown = 1;
1306 if (status != GOT_STATUS_ADD) {
1307 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1308 if (err)
1309 goto done;
1313 if (status != GOT_STATUS_DELETE) {
1314 if (asprintf(&abspath, "%s/%s",
1315 got_worktree_get_root_path(a->worktree), path) == -1) {
1316 err = got_error_from_errno("asprintf");
1317 goto done;
1320 f2 = fopen(abspath, "r");
1321 if (f2 == NULL) {
1322 err = got_error_from_errno2("fopen", abspath);
1323 goto done;
1325 if (lstat(abspath, &sb) == -1) {
1326 err = got_error_from_errno2("lstat", abspath);
1327 goto done;
1329 } else
1330 sb.st_size = 0;
1332 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1333 stdout);
1334 done:
1335 if (blob1)
1336 got_object_blob_close(blob1);
1337 if (f2 && fclose(f2) != 0 && err == NULL)
1338 err = got_error_from_errno("fclose");
1339 free(abspath);
1340 return err;
1343 static const struct got_error *
1344 cmd_diff(int argc, char *argv[])
1346 const struct got_error *error;
1347 struct got_repository *repo = NULL;
1348 struct got_worktree *worktree = NULL;
1349 char *cwd = NULL, *repo_path = NULL;
1350 struct got_object_id *id1 = NULL, *id2 = NULL;
1351 const char *id_str1 = NULL, *id_str2 = NULL;
1352 char *label1 = NULL, *label2 = NULL;
1353 int type1, type2;
1354 int diff_context = 3, ch;
1355 const char *errstr;
1356 char *path = NULL;
1358 #ifndef PROFILE
1359 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1360 NULL) == -1)
1361 err(1, "pledge");
1362 #endif
1364 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1365 switch (ch) {
1366 case 'C':
1367 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1368 if (errstr != NULL)
1369 err(1, "-C option %s", errstr);
1370 break;
1371 case 'r':
1372 repo_path = realpath(optarg, NULL);
1373 if (repo_path == NULL)
1374 err(1, "-r option");
1375 got_path_strip_trailing_slashes(repo_path);
1376 break;
1377 default:
1378 usage_diff();
1379 /* NOTREACHED */
1383 argc -= optind;
1384 argv += optind;
1386 cwd = getcwd(NULL, 0);
1387 if (cwd == NULL) {
1388 error = got_error_from_errno("getcwd");
1389 goto done;
1391 error = got_worktree_open(&worktree, cwd);
1392 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1393 goto done;
1394 if (argc <= 1) {
1395 if (worktree == NULL) {
1396 error = got_error(GOT_ERR_NOT_WORKTREE);
1397 goto done;
1399 if (repo_path)
1400 errx(1,
1401 "-r option can't be used when diffing a work tree");
1402 repo_path = strdup(got_worktree_get_repo_path(worktree));
1403 if (repo_path == NULL) {
1404 error = got_error_from_errno("strdup");
1405 goto done;
1407 if (argc == 1) {
1408 error = got_worktree_resolve_path(&path, worktree,
1409 argv[0]);
1410 if (error)
1411 goto done;
1412 } else {
1413 path = strdup("");
1414 if (path == NULL) {
1415 error = got_error_from_errno("strdup");
1416 goto done;
1419 } else if (argc == 2) {
1420 id_str1 = argv[0];
1421 id_str2 = argv[1];
1422 if (worktree && repo_path == NULL) {
1423 repo_path =
1424 strdup(got_worktree_get_repo_path(worktree));
1425 if (repo_path == NULL) {
1426 error = got_error_from_errno("strdup");
1427 goto done;
1430 } else
1431 usage_diff();
1433 if (repo_path == NULL) {
1434 repo_path = getcwd(NULL, 0);
1435 if (repo_path == NULL)
1436 return got_error_from_errno("getcwd");
1439 error = got_repo_open(&repo, repo_path);
1440 free(repo_path);
1441 if (error != NULL)
1442 goto done;
1444 error = apply_unveil(got_repo_get_path(repo), 1,
1445 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1446 if (error)
1447 goto done;
1449 if (argc <= 1) {
1450 struct print_diff_arg arg;
1451 char *id_str;
1452 error = got_object_id_str(&id_str,
1453 got_worktree_get_base_commit_id(worktree));
1454 if (error)
1455 goto done;
1456 arg.repo = repo;
1457 arg.worktree = worktree;
1458 arg.diff_context = diff_context;
1459 arg.id_str = id_str;
1460 arg.header_shown = 0;
1462 error = got_worktree_status(worktree, path, repo, print_diff,
1463 &arg, check_cancelled, NULL);
1464 free(id_str);
1465 goto done;
1468 error = got_repo_match_object_id_prefix(&id1, id_str1,
1469 GOT_OBJ_TYPE_ANY, repo);
1470 if (error) {
1471 struct got_reference *ref;
1472 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1473 goto done;
1474 error = got_ref_open(&ref, repo, id_str1, 0);
1475 if (error != NULL)
1476 goto done;
1477 label1 = strdup(got_ref_get_name(ref));
1478 if (label1 == NULL) {
1479 error = got_error_from_errno("strdup");
1480 goto done;
1482 error = got_ref_resolve(&id1, repo, ref);
1483 got_ref_close(ref);
1484 if (error != NULL)
1485 goto done;
1486 } else {
1487 error = got_object_id_str(&label1, id1);
1488 if (label1 == NULL) {
1489 error = got_error_from_errno("strdup");
1490 goto done;
1494 error = got_repo_match_object_id_prefix(&id2, id_str2,
1495 GOT_OBJ_TYPE_ANY, repo);
1496 if (error) {
1497 struct got_reference *ref;
1498 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1499 goto done;
1500 error = got_ref_open(&ref, repo, id_str2, 0);
1501 if (error != NULL)
1502 goto done;
1503 label2 = strdup(got_ref_get_name(ref));
1504 if (label2 == NULL) {
1505 error = got_error_from_errno("strdup");
1506 goto done;
1508 error = got_ref_resolve(&id2, repo, ref);
1509 got_ref_close(ref);
1510 if (error != NULL)
1511 goto done;
1512 } else {
1513 error = got_object_id_str(&label2, id2);
1514 if (label2 == NULL) {
1515 error = got_error_from_errno("strdup");
1516 goto done;
1520 error = got_object_get_type(&type1, repo, id1);
1521 if (error)
1522 goto done;
1524 error = got_object_get_type(&type2, repo, id2);
1525 if (error)
1526 goto done;
1528 if (type1 != type2) {
1529 error = got_error(GOT_ERR_OBJ_TYPE);
1530 goto done;
1533 switch (type1) {
1534 case GOT_OBJ_TYPE_BLOB:
1535 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1536 diff_context, repo, stdout);
1537 break;
1538 case GOT_OBJ_TYPE_TREE:
1539 error = got_diff_objects_as_trees(id1, id2, "", "",
1540 diff_context, repo, stdout);
1541 break;
1542 case GOT_OBJ_TYPE_COMMIT:
1543 printf("diff %s %s\n", label1, label2);
1544 error = got_diff_objects_as_commits(id1, id2, diff_context,
1545 repo, stdout);
1546 break;
1547 default:
1548 error = got_error(GOT_ERR_OBJ_TYPE);
1551 done:
1552 free(label1);
1553 free(label2);
1554 free(id1);
1555 free(id2);
1556 free(path);
1557 if (worktree)
1558 got_worktree_close(worktree);
1559 if (repo) {
1560 const struct got_error *repo_error;
1561 repo_error = got_repo_close(repo);
1562 if (error == NULL)
1563 error = repo_error;
1565 return error;
1568 __dead static void
1569 usage_blame(void)
1571 fprintf(stderr,
1572 "usage: %s blame [-c commit] [-r repository-path] path\n",
1573 getprogname());
1574 exit(1);
1577 static const struct got_error *
1578 cmd_blame(int argc, char *argv[])
1580 const struct got_error *error;
1581 struct got_repository *repo = NULL;
1582 struct got_worktree *worktree = NULL;
1583 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1584 struct got_object_id *commit_id = NULL;
1585 char *commit_id_str = NULL;
1586 int ch;
1588 #ifndef PROFILE
1589 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1590 NULL) == -1)
1591 err(1, "pledge");
1592 #endif
1594 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1595 switch (ch) {
1596 case 'c':
1597 commit_id_str = optarg;
1598 break;
1599 case 'r':
1600 repo_path = realpath(optarg, NULL);
1601 if (repo_path == NULL)
1602 err(1, "-r option");
1603 got_path_strip_trailing_slashes(repo_path);
1604 break;
1605 default:
1606 usage_blame();
1607 /* NOTREACHED */
1611 argc -= optind;
1612 argv += optind;
1614 if (argc == 1)
1615 path = argv[0];
1616 else
1617 usage_blame();
1619 cwd = getcwd(NULL, 0);
1620 if (cwd == NULL) {
1621 error = got_error_from_errno("getcwd");
1622 goto done;
1624 if (repo_path == NULL) {
1625 error = got_worktree_open(&worktree, cwd);
1626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1627 goto done;
1628 else
1629 error = NULL;
1630 if (worktree) {
1631 repo_path =
1632 strdup(got_worktree_get_repo_path(worktree));
1633 if (repo_path == NULL)
1634 error = got_error_from_errno("strdup");
1635 if (error)
1636 goto done;
1637 } else {
1638 repo_path = strdup(cwd);
1639 if (repo_path == NULL) {
1640 error = got_error_from_errno("strdup");
1641 goto done;
1646 error = got_repo_open(&repo, repo_path);
1647 if (error != NULL)
1648 goto done;
1650 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1651 if (error)
1652 goto done;
1654 if (worktree) {
1655 const char *prefix = got_worktree_get_path_prefix(worktree);
1656 char *p, *worktree_subdir = cwd +
1657 strlen(got_worktree_get_root_path(worktree));
1658 if (asprintf(&p, "%s%s%s%s%s",
1659 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1660 worktree_subdir, worktree_subdir[0] ? "/" : "",
1661 path) == -1) {
1662 error = got_error_from_errno("asprintf");
1663 goto done;
1665 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1666 free(p);
1667 } else {
1668 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1670 if (error)
1671 goto done;
1673 if (commit_id_str == NULL) {
1674 struct got_reference *head_ref;
1675 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1676 if (error != NULL)
1677 goto done;
1678 error = got_ref_resolve(&commit_id, repo, head_ref);
1679 got_ref_close(head_ref);
1680 if (error != NULL)
1681 goto done;
1682 } else {
1683 error = got_repo_match_object_id_prefix(&commit_id,
1684 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1685 if (error != NULL)
1686 goto done;
1689 error = got_blame(in_repo_path, commit_id, repo, stdout);
1690 done:
1691 free(in_repo_path);
1692 free(repo_path);
1693 free(cwd);
1694 free(commit_id);
1695 if (worktree)
1696 got_worktree_close(worktree);
1697 if (repo) {
1698 const struct got_error *repo_error;
1699 repo_error = got_repo_close(repo);
1700 if (error == NULL)
1701 error = repo_error;
1703 return error;
1706 __dead static void
1707 usage_tree(void)
1709 fprintf(stderr,
1710 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1711 getprogname());
1712 exit(1);
1715 static void
1716 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1717 const char *root_path)
1719 int is_root_path = (strcmp(path, root_path) == 0);
1721 path += strlen(root_path);
1722 while (path[0] == '/')
1723 path++;
1725 printf("%s%s%s%s%s\n", id ? id : "", path,
1726 is_root_path ? "" : "/", te->name,
1727 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1730 static const struct got_error *
1731 print_tree(const char *path, struct got_object_id *commit_id,
1732 int show_ids, int recurse, const char *root_path,
1733 struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_object_id *tree_id = NULL;
1737 struct got_tree_object *tree = NULL;
1738 const struct got_tree_entries *entries;
1739 struct got_tree_entry *te;
1741 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1742 if (err)
1743 goto done;
1745 err = got_object_open_as_tree(&tree, repo, tree_id);
1746 if (err)
1747 goto done;
1748 entries = got_object_tree_get_entries(tree);
1749 te = SIMPLEQ_FIRST(&entries->head);
1750 while (te) {
1751 char *id = NULL;
1753 if (sigint_received || sigpipe_received)
1754 break;
1756 if (show_ids) {
1757 char *id_str;
1758 err = got_object_id_str(&id_str, te->id);
1759 if (err)
1760 goto done;
1761 if (asprintf(&id, "%s ", id_str) == -1) {
1762 err = got_error_from_errno("asprintf");
1763 free(id_str);
1764 goto done;
1766 free(id_str);
1768 print_entry(te, id, path, root_path);
1769 free(id);
1771 if (recurse && S_ISDIR(te->mode)) {
1772 char *child_path;
1773 if (asprintf(&child_path, "%s%s%s", path,
1774 path[0] == '/' && path[1] == '\0' ? "" : "/",
1775 te->name) == -1) {
1776 err = got_error_from_errno("asprintf");
1777 goto done;
1779 err = print_tree(child_path, commit_id, show_ids, 1,
1780 root_path, repo);
1781 free(child_path);
1782 if (err)
1783 goto done;
1786 te = SIMPLEQ_NEXT(te, entry);
1788 done:
1789 if (tree)
1790 got_object_tree_close(tree);
1791 free(tree_id);
1792 return err;
1795 static const struct got_error *
1796 cmd_tree(int argc, char *argv[])
1798 const struct got_error *error;
1799 struct got_repository *repo = NULL;
1800 struct got_worktree *worktree = NULL;
1801 const char *path;
1802 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1803 struct got_object_id *commit_id = NULL;
1804 char *commit_id_str = NULL;
1805 int show_ids = 0, recurse = 0;
1806 int ch;
1808 #ifndef PROFILE
1809 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1810 NULL) == -1)
1811 err(1, "pledge");
1812 #endif
1814 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1815 switch (ch) {
1816 case 'c':
1817 commit_id_str = optarg;
1818 break;
1819 case 'r':
1820 repo_path = realpath(optarg, NULL);
1821 if (repo_path == NULL)
1822 err(1, "-r option");
1823 got_path_strip_trailing_slashes(repo_path);
1824 break;
1825 case 'i':
1826 show_ids = 1;
1827 break;
1828 case 'R':
1829 recurse = 1;
1830 break;
1831 default:
1832 usage_tree();
1833 /* NOTREACHED */
1837 argc -= optind;
1838 argv += optind;
1840 if (argc == 1)
1841 path = argv[0];
1842 else if (argc > 1)
1843 usage_tree();
1844 else
1845 path = NULL;
1847 cwd = getcwd(NULL, 0);
1848 if (cwd == NULL) {
1849 error = got_error_from_errno("getcwd");
1850 goto done;
1852 if (repo_path == NULL) {
1853 error = got_worktree_open(&worktree, cwd);
1854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1855 goto done;
1856 else
1857 error = NULL;
1858 if (worktree) {
1859 repo_path =
1860 strdup(got_worktree_get_repo_path(worktree));
1861 if (repo_path == NULL)
1862 error = got_error_from_errno("strdup");
1863 if (error)
1864 goto done;
1865 } else {
1866 repo_path = strdup(cwd);
1867 if (repo_path == NULL) {
1868 error = got_error_from_errno("strdup");
1869 goto done;
1874 error = got_repo_open(&repo, repo_path);
1875 if (error != NULL)
1876 goto done;
1878 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1879 if (error)
1880 goto done;
1882 if (path == NULL) {
1883 if (worktree) {
1884 char *p, *worktree_subdir = cwd +
1885 strlen(got_worktree_get_root_path(worktree));
1886 if (asprintf(&p, "%s/%s",
1887 got_worktree_get_path_prefix(worktree),
1888 worktree_subdir) == -1) {
1889 error = got_error_from_errno("asprintf");
1890 goto done;
1892 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1893 free(p);
1894 if (error)
1895 goto done;
1896 } else
1897 path = "/";
1899 if (in_repo_path == NULL) {
1900 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1901 if (error != NULL)
1902 goto done;
1905 if (commit_id_str == NULL) {
1906 struct got_reference *head_ref;
1907 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1908 if (error != NULL)
1909 goto done;
1910 error = got_ref_resolve(&commit_id, repo, head_ref);
1911 got_ref_close(head_ref);
1912 if (error != NULL)
1913 goto done;
1914 } else {
1915 error = got_repo_match_object_id_prefix(&commit_id,
1916 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1917 if (error != NULL)
1918 goto done;
1921 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1922 in_repo_path, repo);
1923 done:
1924 free(in_repo_path);
1925 free(repo_path);
1926 free(cwd);
1927 free(commit_id);
1928 if (worktree)
1929 got_worktree_close(worktree);
1930 if (repo) {
1931 const struct got_error *repo_error;
1932 repo_error = got_repo_close(repo);
1933 if (error == NULL)
1934 error = repo_error;
1936 return error;
1939 __dead static void
1940 usage_status(void)
1942 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1943 exit(1);
1946 static const struct got_error *
1947 print_status(void *arg, unsigned char status, const char *path,
1948 struct got_object_id *blob_id, struct got_object_id *commit_id)
1950 printf("%c %s\n", status, path);
1951 return NULL;
1954 static const struct got_error *
1955 cmd_status(int argc, char *argv[])
1957 const struct got_error *error = NULL;
1958 struct got_repository *repo = NULL;
1959 struct got_worktree *worktree = NULL;
1960 char *cwd = NULL, *path = NULL;
1961 int ch;
1963 while ((ch = getopt(argc, argv, "")) != -1) {
1964 switch (ch) {
1965 default:
1966 usage_status();
1967 /* NOTREACHED */
1971 argc -= optind;
1972 argv += optind;
1974 #ifndef PROFILE
1975 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1976 NULL) == -1)
1977 err(1, "pledge");
1978 #endif
1979 cwd = getcwd(NULL, 0);
1980 if (cwd == NULL) {
1981 error = got_error_from_errno("getcwd");
1982 goto done;
1985 error = got_worktree_open(&worktree, cwd);
1986 if (error != NULL)
1987 goto done;
1989 if (argc == 0) {
1990 path = strdup("");
1991 if (path == NULL) {
1992 error = got_error_from_errno("strdup");
1993 goto done;
1995 } else if (argc == 1) {
1996 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1997 if (error)
1998 goto done;
1999 } else
2000 usage_status();
2002 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2003 if (error != NULL)
2004 goto done;
2006 error = apply_unveil(got_repo_get_path(repo), 1,
2007 got_worktree_get_root_path(worktree), 0);
2008 if (error)
2009 goto done;
2011 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2012 check_cancelled, NULL);
2013 done:
2014 free(cwd);
2015 free(path);
2016 return error;
2019 __dead static void
2020 usage_ref(void)
2022 fprintf(stderr,
2023 "usage: %s ref [-r repository] -l | -d name | name target\n",
2024 getprogname());
2025 exit(1);
2028 static const struct got_error *
2029 list_refs(struct got_repository *repo)
2031 static const struct got_error *err = NULL;
2032 struct got_reflist_head refs;
2033 struct got_reflist_entry *re;
2035 SIMPLEQ_INIT(&refs);
2036 err = got_ref_list(&refs, repo);
2037 if (err)
2038 return err;
2040 SIMPLEQ_FOREACH(re, &refs, entry) {
2041 char *refstr;
2042 refstr = got_ref_to_str(re->ref);
2043 if (refstr == NULL)
2044 return got_error_from_errno("got_ref_to_str");
2045 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2046 free(refstr);
2049 got_ref_list_free(&refs);
2050 return NULL;
2053 static const struct got_error *
2054 delete_ref(struct got_repository *repo, const char *refname)
2056 const struct got_error *err = NULL;
2057 struct got_reference *ref;
2059 err = got_ref_open(&ref, repo, refname, 0);
2060 if (err)
2061 return err;
2063 err = got_ref_delete(ref, repo);
2064 got_ref_close(ref);
2065 return err;
2068 static const struct got_error *
2069 add_ref(struct got_repository *repo, const char *refname, const char *target)
2071 const struct got_error *err = NULL;
2072 struct got_object_id *id;
2073 struct got_reference *ref = NULL;
2075 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2076 repo);
2077 if (err) {
2078 struct got_reference *target_ref;
2080 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2081 return err;
2082 err = got_ref_open(&target_ref, repo, target, 0);
2083 if (err)
2084 return err;
2085 err = got_ref_resolve(&id, repo, target_ref);
2086 got_ref_close(target_ref);
2087 if (err)
2088 return err;
2091 err = got_ref_alloc(&ref, refname, id);
2092 if (err)
2093 goto done;
2095 err = got_ref_write(ref, repo);
2096 done:
2097 if (ref)
2098 got_ref_close(ref);
2099 free(id);
2100 return err;
2103 static const struct got_error *
2104 cmd_ref(int argc, char *argv[])
2106 const struct got_error *error = NULL;
2107 struct got_repository *repo = NULL;
2108 struct got_worktree *worktree = NULL;
2109 char *cwd = NULL, *repo_path = NULL;
2110 int ch, do_list = 0;
2111 const char *delref = NULL;
2113 /* TODO: Add -s option for adding symbolic references. */
2114 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2115 switch (ch) {
2116 case 'd':
2117 delref = optarg;
2118 break;
2119 case 'r':
2120 repo_path = realpath(optarg, NULL);
2121 if (repo_path == NULL)
2122 err(1, "-r option");
2123 got_path_strip_trailing_slashes(repo_path);
2124 break;
2125 case 'l':
2126 do_list = 1;
2127 break;
2128 default:
2129 usage_ref();
2130 /* NOTREACHED */
2134 if (do_list && delref)
2135 errx(1, "-l and -d options are mutually exclusive\n");
2137 argc -= optind;
2138 argv += optind;
2140 if (do_list || delref) {
2141 if (argc > 0)
2142 usage_ref();
2143 } else if (argc != 2)
2144 usage_ref();
2146 #ifndef PROFILE
2147 if (do_list) {
2148 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2149 NULL) == -1)
2150 err(1, "pledge");
2151 } else {
2152 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2153 "sendfd unveil", NULL) == -1)
2154 err(1, "pledge");
2156 #endif
2157 cwd = getcwd(NULL, 0);
2158 if (cwd == NULL) {
2159 error = got_error_from_errno("getcwd");
2160 goto done;
2163 if (repo_path == NULL) {
2164 error = got_worktree_open(&worktree, cwd);
2165 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2166 goto done;
2167 else
2168 error = NULL;
2169 if (worktree) {
2170 repo_path =
2171 strdup(got_worktree_get_repo_path(worktree));
2172 if (repo_path == NULL)
2173 error = got_error_from_errno("strdup");
2174 if (error)
2175 goto done;
2176 } else {
2177 repo_path = strdup(cwd);
2178 if (repo_path == NULL) {
2179 error = got_error_from_errno("strdup");
2180 goto done;
2185 error = got_repo_open(&repo, repo_path);
2186 if (error != NULL)
2187 goto done;
2189 error = apply_unveil(got_repo_get_path(repo), do_list,
2190 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2191 if (error)
2192 goto done;
2194 if (do_list)
2195 error = list_refs(repo);
2196 else if (delref)
2197 error = delete_ref(repo, delref);
2198 else
2199 error = add_ref(repo, argv[0], argv[1]);
2200 done:
2201 if (repo)
2202 got_repo_close(repo);
2203 if (worktree)
2204 got_worktree_close(worktree);
2205 free(cwd);
2206 free(repo_path);
2207 return error;
2210 __dead static void
2211 usage_branch(void)
2213 fprintf(stderr,
2214 "usage: %s branch [-r repository] -l | -d name | "
2215 "name [base-branch]\n", getprogname());
2216 exit(1);
2219 static const struct got_error *
2220 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2222 static const struct got_error *err = NULL;
2223 struct got_reflist_head refs;
2224 struct got_reflist_entry *re;
2226 SIMPLEQ_INIT(&refs);
2228 err = got_ref_list(&refs, repo);
2229 if (err)
2230 return err;
2232 SIMPLEQ_FOREACH(re, &refs, entry) {
2233 const char *refname, *marker = " ";
2234 char *refstr;
2235 refname = got_ref_get_name(re->ref);
2236 if (strncmp(refname, "refs/heads/", 11) != 0)
2237 continue;
2238 if (worktree && strcmp(refname,
2239 got_worktree_get_head_ref_name(worktree)) == 0) {
2240 struct got_object_id *id = NULL;
2241 err = got_ref_resolve(&id, repo, re->ref);
2242 if (err)
2243 return err;
2244 if (got_object_id_cmp(id,
2245 got_worktree_get_base_commit_id(worktree)) == 0)
2246 marker = "* ";
2247 else
2248 marker = "~ ";
2249 free(id);
2251 refname += 11;
2252 refstr = got_ref_to_str(re->ref);
2253 if (refstr == NULL)
2254 return got_error_from_errno("got_ref_to_str");
2255 printf("%s%s: %s\n", marker, refname, refstr);
2256 free(refstr);
2259 got_ref_list_free(&refs);
2260 return NULL;
2263 static const struct got_error *
2264 delete_branch(struct got_repository *repo, const char *branch_name)
2266 const struct got_error *err = NULL;
2267 struct got_reference *ref;
2268 char *refname;
2270 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2271 return got_error_from_errno("asprintf");
2273 err = got_ref_open(&ref, repo, refname, 0);
2274 if (err)
2275 goto done;
2277 err = got_ref_delete(ref, repo);
2278 got_ref_close(ref);
2279 done:
2280 free(refname);
2281 return err;
2284 static const struct got_error *
2285 add_branch(struct got_repository *repo, const char *branch_name,
2286 const char *base_branch)
2288 const struct got_error *err = NULL;
2289 struct got_object_id *id = NULL;
2290 struct got_reference *ref = NULL;
2291 char *base_refname = NULL, *refname = NULL;
2292 struct got_reference *base_ref;
2295 * Don't let the user create a branch named '-'.
2296 * While technically a valid reference name, this case is usually
2297 * an unintended typo.
2299 if (branch_name[0] == '-' && branch_name[1] == '\0')
2300 return got_error(GOT_ERR_BAD_REF_NAME);
2302 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2303 base_refname = strdup(GOT_REF_HEAD);
2304 if (base_refname == NULL)
2305 return got_error_from_errno("strdup");
2306 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2307 return got_error_from_errno("asprintf");
2309 err = got_ref_open(&base_ref, repo, base_refname, 0);
2310 if (err)
2311 goto done;
2312 err = got_ref_resolve(&id, repo, base_ref);
2313 got_ref_close(base_ref);
2314 if (err)
2315 goto done;
2317 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2318 err = got_error_from_errno("asprintf");
2319 goto done;
2322 err = got_ref_open(&ref, repo, refname, 0);
2323 if (err == NULL) {
2324 err = got_error(GOT_ERR_BRANCH_EXISTS);
2325 goto done;
2326 } else if (err->code != GOT_ERR_NOT_REF)
2327 goto done;
2329 err = got_ref_alloc(&ref, refname, id);
2330 if (err)
2331 goto done;
2333 err = got_ref_write(ref, repo);
2334 done:
2335 if (ref)
2336 got_ref_close(ref);
2337 free(id);
2338 free(base_refname);
2339 free(refname);
2340 return err;
2343 static const struct got_error *
2344 cmd_branch(int argc, char *argv[])
2346 const struct got_error *error = NULL;
2347 struct got_repository *repo = NULL;
2348 struct got_worktree *worktree = NULL;
2349 char *cwd = NULL, *repo_path = NULL;
2350 int ch, do_list = 0;
2351 const char *delref = NULL;
2353 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2354 switch (ch) {
2355 case 'd':
2356 delref = optarg;
2357 break;
2358 case 'r':
2359 repo_path = realpath(optarg, NULL);
2360 if (repo_path == NULL)
2361 err(1, "-r option");
2362 got_path_strip_trailing_slashes(repo_path);
2363 break;
2364 case 'l':
2365 do_list = 1;
2366 break;
2367 default:
2368 usage_branch();
2369 /* NOTREACHED */
2373 if (do_list && delref)
2374 errx(1, "-l and -d options are mutually exclusive\n");
2376 argc -= optind;
2377 argv += optind;
2379 if (do_list || delref) {
2380 if (argc > 0)
2381 usage_branch();
2382 } else if (argc < 1 || argc > 2)
2383 usage_branch();
2385 #ifndef PROFILE
2386 if (do_list) {
2387 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2388 NULL) == -1)
2389 err(1, "pledge");
2390 } else {
2391 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2392 "sendfd unveil", NULL) == -1)
2393 err(1, "pledge");
2395 #endif
2396 cwd = getcwd(NULL, 0);
2397 if (cwd == NULL) {
2398 error = got_error_from_errno("getcwd");
2399 goto done;
2402 if (repo_path == NULL) {
2403 error = got_worktree_open(&worktree, cwd);
2404 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2405 goto done;
2406 else
2407 error = NULL;
2408 if (worktree) {
2409 repo_path =
2410 strdup(got_worktree_get_repo_path(worktree));
2411 if (repo_path == NULL)
2412 error = got_error_from_errno("strdup");
2413 if (error)
2414 goto done;
2415 } else {
2416 repo_path = strdup(cwd);
2417 if (repo_path == NULL) {
2418 error = got_error_from_errno("strdup");
2419 goto done;
2424 error = got_repo_open(&repo, repo_path);
2425 if (error != NULL)
2426 goto done;
2428 error = apply_unveil(got_repo_get_path(repo), do_list,
2429 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2430 if (error)
2431 goto done;
2433 if (do_list)
2434 error = list_branches(repo, worktree);
2435 else if (delref)
2436 error = delete_branch(repo, delref);
2437 else {
2438 const char *base_branch;
2439 if (argc == 1) {
2440 base_branch = worktree ?
2441 got_worktree_get_head_ref_name(worktree) :
2442 GOT_REF_HEAD;
2443 } else
2444 base_branch = argv[1];
2445 error = add_branch(repo, argv[0], base_branch);
2447 done:
2448 if (repo)
2449 got_repo_close(repo);
2450 if (worktree)
2451 got_worktree_close(worktree);
2452 free(cwd);
2453 free(repo_path);
2454 return error;
2457 __dead static void
2458 usage_add(void)
2460 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2461 exit(1);
2464 static const struct got_error *
2465 cmd_add(int argc, char *argv[])
2467 const struct got_error *error = NULL;
2468 struct got_repository *repo = NULL;
2469 struct got_worktree *worktree = NULL;
2470 char *cwd = NULL;
2471 struct got_pathlist_head paths;
2472 struct got_pathlist_entry *pe;
2473 int ch, x;
2475 TAILQ_INIT(&paths);
2477 while ((ch = getopt(argc, argv, "")) != -1) {
2478 switch (ch) {
2479 default:
2480 usage_add();
2481 /* NOTREACHED */
2485 argc -= optind;
2486 argv += optind;
2488 if (argc < 1)
2489 usage_add();
2491 /* make sure each file exists before doing anything halfway */
2492 for (x = 0; x < argc; x++) {
2493 char *path = realpath(argv[x], NULL);
2494 if (path == NULL) {
2495 error = got_error_from_errno2("realpath", argv[x]);
2496 goto done;
2498 free(path);
2501 cwd = getcwd(NULL, 0);
2502 if (cwd == NULL) {
2503 error = got_error_from_errno("getcwd");
2504 goto done;
2507 error = got_worktree_open(&worktree, cwd);
2508 if (error)
2509 goto done;
2511 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2512 if (error != NULL)
2513 goto done;
2515 error = apply_unveil(got_repo_get_path(repo), 1,
2516 got_worktree_get_root_path(worktree), 0);
2517 if (error)
2518 goto done;
2520 for (x = 0; x < argc; x++) {
2521 char *path = realpath(argv[x], NULL);
2522 if (path == NULL) {
2523 error = got_error_from_errno2("realpath", argv[x]);
2524 goto done;
2527 got_path_strip_trailing_slashes(path);
2528 error = got_pathlist_insert(&pe, &paths, path, NULL);
2529 if (error) {
2530 free(path);
2531 goto done;
2534 error = got_worktree_schedule_add(worktree, &paths, print_status,
2535 NULL, repo);
2536 done:
2537 if (repo)
2538 got_repo_close(repo);
2539 if (worktree)
2540 got_worktree_close(worktree);
2541 TAILQ_FOREACH(pe, &paths, entry)
2542 free((char *)pe->path);
2543 got_pathlist_free(&paths);
2544 free(cwd);
2545 return error;
2548 __dead static void
2549 usage_remove(void)
2551 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2552 exit(1);
2555 static const struct got_error *
2556 cmd_remove(int argc, char *argv[])
2558 const struct got_error *error = NULL;
2559 struct got_worktree *worktree = NULL;
2560 struct got_repository *repo = NULL;
2561 char *cwd = NULL;
2562 struct got_pathlist_head paths;
2563 struct got_pathlist_entry *pe;
2564 int ch, i, delete_local_mods = 0;
2566 TAILQ_INIT(&paths);
2568 while ((ch = getopt(argc, argv, "f")) != -1) {
2569 switch (ch) {
2570 case 'f':
2571 delete_local_mods = 1;
2572 break;
2573 default:
2574 usage_add();
2575 /* NOTREACHED */
2579 argc -= optind;
2580 argv += optind;
2582 if (argc < 1)
2583 usage_remove();
2585 /* make sure each file exists before doing anything halfway */
2586 for (i = 0; i < argc; i++) {
2587 char *path = realpath(argv[i], NULL);
2588 if (path == NULL) {
2589 error = got_error_from_errno2("realpath", argv[i]);
2590 goto done;
2592 free(path);
2595 cwd = getcwd(NULL, 0);
2596 if (cwd == NULL) {
2597 error = got_error_from_errno("getcwd");
2598 goto done;
2600 error = got_worktree_open(&worktree, cwd);
2601 if (error)
2602 goto done;
2604 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2605 if (error)
2606 goto done;
2608 error = apply_unveil(got_repo_get_path(repo), 1,
2609 got_worktree_get_root_path(worktree), 0);
2610 if (error)
2611 goto done;
2613 for (i = 0; i < argc; i++) {
2614 char *path = realpath(argv[i], NULL);
2615 if (path == NULL) {
2616 error = got_error_from_errno2("realpath", argv[i]);
2617 goto done;
2620 got_path_strip_trailing_slashes(path);
2621 error = got_pathlist_insert(&pe, &paths, path, NULL);
2622 if (error) {
2623 free(path);
2624 goto done;
2627 error = got_worktree_schedule_delete(worktree, &paths,
2628 delete_local_mods, print_status, NULL, repo);
2629 if (error)
2630 goto done;
2631 done:
2632 if (repo)
2633 got_repo_close(repo);
2634 if (worktree)
2635 got_worktree_close(worktree);
2636 TAILQ_FOREACH(pe, &paths, entry)
2637 free((char *)pe->path);
2638 got_pathlist_free(&paths);
2639 free(cwd);
2640 return error;
2643 __dead static void
2644 usage_revert(void)
2646 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2647 exit(1);
2650 static const struct got_error *
2651 revert_progress(void *arg, unsigned char status, const char *path)
2653 while (path[0] == '/')
2654 path++;
2655 printf("%c %s\n", status, path);
2656 return NULL;
2659 static const struct got_error *
2660 cmd_revert(int argc, char *argv[])
2662 const struct got_error *error = NULL;
2663 struct got_worktree *worktree = NULL;
2664 struct got_repository *repo = NULL;
2665 char *cwd = NULL, *path = NULL;
2666 struct got_pathlist_head paths;
2667 struct got_pathlist_entry *pe;
2668 int ch, i;
2670 TAILQ_INIT(&paths);
2672 while ((ch = getopt(argc, argv, "")) != -1) {
2673 switch (ch) {
2674 default:
2675 usage_revert();
2676 /* NOTREACHED */
2680 argc -= optind;
2681 argv += optind;
2683 if (argc < 1)
2684 usage_revert();
2686 for (i = 0; i < argc; i++) {
2687 char *path = realpath(argv[i], NULL);
2688 if (path == NULL) {
2689 error = got_error_from_errno2("realpath", argv[i]);
2690 goto done;
2693 got_path_strip_trailing_slashes(path);
2694 error = got_pathlist_insert(&pe, &paths, path, NULL);
2695 if (error) {
2696 free(path);
2697 goto done;
2701 cwd = getcwd(NULL, 0);
2702 if (cwd == NULL) {
2703 error = got_error_from_errno("getcwd");
2704 goto done;
2706 error = got_worktree_open(&worktree, cwd);
2707 if (error)
2708 goto done;
2710 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2711 if (error != NULL)
2712 goto done;
2714 error = apply_unveil(got_repo_get_path(repo), 1,
2715 got_worktree_get_root_path(worktree), 0);
2716 if (error)
2717 goto done;
2719 error = got_worktree_revert(worktree, &paths,
2720 revert_progress, NULL, repo);
2721 if (error)
2722 goto done;
2723 done:
2724 if (repo)
2725 got_repo_close(repo);
2726 if (worktree)
2727 got_worktree_close(worktree);
2728 free(path);
2729 free(cwd);
2730 return error;
2733 __dead static void
2734 usage_commit(void)
2736 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2737 exit(1);
2740 int
2741 spawn_editor(const char *editor, const char *file)
2743 pid_t pid;
2744 sig_t sighup, sigint, sigquit;
2745 int st = -1;
2747 sighup = signal(SIGHUP, SIG_IGN);
2748 sigint = signal(SIGINT, SIG_IGN);
2749 sigquit = signal(SIGQUIT, SIG_IGN);
2751 switch (pid = fork()) {
2752 case -1:
2753 goto doneediting;
2754 case 0:
2755 execl(editor, editor, file, (char *)NULL);
2756 _exit(127);
2759 while (waitpid(pid, &st, 0) == -1)
2760 if (errno != EINTR)
2761 break;
2763 doneediting:
2764 (void)signal(SIGHUP, sighup);
2765 (void)signal(SIGINT, sigint);
2766 (void)signal(SIGQUIT, sigquit);
2768 if (!WIFEXITED(st)) {
2769 errno = EINTR;
2770 return -1;
2773 return WEXITSTATUS(st);
2776 struct collect_commit_logmsg_arg {
2777 const char *cmdline_log;
2778 const char *editor;
2779 const char *worktree_path;
2780 const char *branch_name;
2781 const char *repo_path;
2782 char *logmsg_path;
2786 static const struct got_error *
2787 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2788 void *arg)
2790 char *initial_content = NULL;
2791 struct got_pathlist_entry *pe;
2792 const struct got_error *err = NULL;
2793 char *template = NULL;
2794 struct collect_commit_logmsg_arg *a = arg;
2795 char buf[1024];
2796 struct stat st, st2;
2797 FILE *fp;
2798 size_t len;
2799 int fd, content_changed = 0;
2801 /* if a message was specified on the command line, just use it */
2802 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2803 len = strlen(a->cmdline_log) + 1;
2804 *logmsg = malloc(len + 1);
2805 if (*logmsg == NULL)
2806 return got_error_from_errno("malloc");
2807 strlcpy(*logmsg, a->cmdline_log, len);
2808 return NULL;
2811 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2812 return got_error_from_errno("asprintf");
2814 if (asprintf(&initial_content,
2815 "\n# changes to be committed on branch %s:\n",
2816 a->branch_name) == -1)
2817 return got_error_from_errno("asprintf");
2819 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2820 if (err)
2821 goto done;
2823 dprintf(fd, initial_content);
2825 TAILQ_FOREACH(pe, commitable_paths, entry) {
2826 struct got_commitable *ct = pe->data;
2827 dprintf(fd, "# %c %s\n",
2828 got_commitable_get_status(ct),
2829 got_commitable_get_path(ct));
2831 close(fd);
2833 if (stat(a->logmsg_path, &st) == -1) {
2834 err = got_error_from_errno2("stat", a->logmsg_path);
2835 goto done;
2838 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2839 err = got_error_from_errno("failed spawning editor");
2840 goto done;
2843 if (stat(a->logmsg_path, &st2) == -1) {
2844 err = got_error_from_errno("stat");
2845 goto done;
2848 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2849 unlink(a->logmsg_path);
2850 free(a->logmsg_path);
2851 a->logmsg_path = NULL;
2852 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2853 "no changes made to commit message, aborting");
2854 goto done;
2857 *logmsg = malloc(st2.st_size + 1);
2858 if (*logmsg == NULL) {
2859 err = got_error_from_errno("malloc");
2860 goto done;
2862 (*logmsg)[0] = '\0';
2863 len = 0;
2865 fp = fopen(a->logmsg_path, "r");
2866 if (fp == NULL) {
2867 err = got_error_from_errno("fopen");
2868 goto done;
2870 while (fgets(buf, sizeof(buf), fp) != NULL) {
2871 if (!content_changed && strcmp(buf, initial_content) != 0)
2872 content_changed = 1;
2873 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2874 continue; /* remove comments and leading empty lines */
2875 len = strlcat(*logmsg, buf, st2.st_size);
2877 fclose(fp);
2879 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2880 (*logmsg)[len - 1] = '\0';
2881 len--;
2884 if (len == 0 || !content_changed) {
2885 unlink(a->logmsg_path);
2886 free(a->logmsg_path);
2887 a->logmsg_path = NULL;
2888 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2889 "commit message cannot be empty, aborting");
2890 goto done;
2892 done:
2893 free(initial_content);
2894 free(template);
2896 /* Editor is done; we can now apply unveil(2) */
2897 if (err == NULL)
2898 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2899 return err;
2902 static const struct got_error *
2903 cmd_commit(int argc, char *argv[])
2905 const struct got_error *error = NULL;
2906 struct got_worktree *worktree = NULL;
2907 struct got_repository *repo = NULL;
2908 char *cwd = NULL, *path = NULL, *id_str = NULL;
2909 struct got_object_id *id = NULL;
2910 const char *logmsg = NULL;
2911 const char *got_author = getenv("GOT_AUTHOR");
2912 struct collect_commit_logmsg_arg cl_arg;
2913 char *editor = NULL;
2914 int ch, rebase_in_progress;
2916 while ((ch = getopt(argc, argv, "m:")) != -1) {
2917 switch (ch) {
2918 case 'm':
2919 logmsg = optarg;
2920 break;
2921 default:
2922 usage_commit();
2923 /* NOTREACHED */
2927 argc -= optind;
2928 argv += optind;
2930 if (argc == 1) {
2931 path = realpath(argv[0], NULL);
2932 if (path == NULL) {
2933 error = got_error_from_errno2("realpath", argv[0]);
2934 goto done;
2936 got_path_strip_trailing_slashes(path);
2937 } else if (argc != 0)
2938 usage_commit();
2940 if (got_author == NULL) {
2941 /* TODO: Look current user up in password database */
2942 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2943 goto done;
2946 cwd = getcwd(NULL, 0);
2947 if (cwd == NULL) {
2948 error = got_error_from_errno("getcwd");
2949 goto done;
2951 error = got_worktree_open(&worktree, cwd);
2952 if (error)
2953 goto done;
2955 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2956 if (error)
2957 goto done;
2958 if (rebase_in_progress) {
2959 error = got_error(GOT_ERR_REBASING);
2960 goto done;
2963 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2964 if (error != NULL)
2965 goto done;
2968 * unveil(2) traverses exec(2); if an editor is used we have
2969 * to apply unveil after the log message has been written.
2971 if (logmsg == NULL || strlen(logmsg) == 0)
2972 error = get_editor(&editor);
2973 else
2974 error = apply_unveil(got_repo_get_path(repo), 0,
2975 got_worktree_get_root_path(worktree), 0);
2976 if (error)
2977 goto done;
2979 cl_arg.editor = editor;
2980 cl_arg.cmdline_log = logmsg;
2981 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2982 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2983 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2984 cl_arg.branch_name += 5;
2985 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2986 cl_arg.branch_name += 6;
2987 cl_arg.repo_path = got_repo_get_path(repo);
2988 cl_arg.logmsg_path = NULL;
2989 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2990 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2991 if (error) {
2992 if (cl_arg.logmsg_path)
2993 fprintf(stderr, "%s: log message preserved in %s\n",
2994 getprogname(), cl_arg.logmsg_path);
2995 goto done;
2998 if (cl_arg.logmsg_path)
2999 unlink(cl_arg.logmsg_path);
3001 error = got_object_id_str(&id_str, id);
3002 if (error)
3003 goto done;
3004 printf("Created commit %s\n", id_str);
3005 done:
3006 if (repo)
3007 got_repo_close(repo);
3008 if (worktree)
3009 got_worktree_close(worktree);
3010 free(path);
3011 free(cwd);
3012 free(id_str);
3013 free(editor);
3014 return error;
3017 __dead static void
3018 usage_cherrypick(void)
3020 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3021 exit(1);
3024 static const struct got_error *
3025 cmd_cherrypick(int argc, char *argv[])
3027 const struct got_error *error = NULL;
3028 struct got_worktree *worktree = NULL;
3029 struct got_repository *repo = NULL;
3030 char *cwd = NULL, *commit_id_str = NULL;
3031 struct got_object_id *commit_id = NULL;
3032 struct got_commit_object *commit = NULL;
3033 struct got_object_qid *pid;
3034 struct got_reference *head_ref = NULL;
3035 int ch, did_something = 0;
3037 while ((ch = getopt(argc, argv, "")) != -1) {
3038 switch (ch) {
3039 default:
3040 usage_cherrypick();
3041 /* NOTREACHED */
3045 argc -= optind;
3046 argv += optind;
3048 if (argc != 1)
3049 usage_cherrypick();
3051 cwd = getcwd(NULL, 0);
3052 if (cwd == NULL) {
3053 error = got_error_from_errno("getcwd");
3054 goto done;
3056 error = got_worktree_open(&worktree, cwd);
3057 if (error)
3058 goto done;
3060 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3061 if (error != NULL)
3062 goto done;
3064 error = apply_unveil(got_repo_get_path(repo), 0,
3065 got_worktree_get_root_path(worktree), 0);
3066 if (error)
3067 goto done;
3069 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3070 GOT_OBJ_TYPE_COMMIT, repo);
3071 if (error != NULL) {
3072 struct got_reference *ref;
3073 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3074 goto done;
3075 error = got_ref_open(&ref, repo, argv[0], 0);
3076 if (error != NULL)
3077 goto done;
3078 error = got_ref_resolve(&commit_id, repo, ref);
3079 got_ref_close(ref);
3080 if (error != NULL)
3081 goto done;
3083 error = got_object_id_str(&commit_id_str, commit_id);
3084 if (error)
3085 goto done;
3087 error = got_ref_open(&head_ref, repo,
3088 got_worktree_get_head_ref_name(worktree), 0);
3089 if (error != NULL)
3090 goto done;
3092 error = check_same_branch(commit_id, head_ref, repo);
3093 if (error) {
3094 if (error->code != GOT_ERR_ANCESTRY)
3095 goto done;
3096 error = NULL;
3097 } else {
3098 error = got_error(GOT_ERR_SAME_BRANCH);
3099 goto done;
3102 error = got_object_open_as_commit(&commit, repo, commit_id);
3103 if (error)
3104 goto done;
3105 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3106 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3107 commit_id, repo, update_progress, &did_something, check_cancelled,
3108 NULL);
3109 if (error != NULL)
3110 goto done;
3112 if (did_something)
3113 printf("Merged commit %s\n", commit_id_str);
3114 done:
3115 if (commit)
3116 got_object_commit_close(commit);
3117 free(commit_id_str);
3118 if (head_ref)
3119 got_ref_close(head_ref);
3120 if (worktree)
3121 got_worktree_close(worktree);
3122 if (repo)
3123 got_repo_close(repo);
3124 return error;
3127 __dead static void
3128 usage_backout(void)
3130 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3131 exit(1);
3134 static const struct got_error *
3135 cmd_backout(int argc, char *argv[])
3137 const struct got_error *error = NULL;
3138 struct got_worktree *worktree = NULL;
3139 struct got_repository *repo = NULL;
3140 char *cwd = NULL, *commit_id_str = NULL;
3141 struct got_object_id *commit_id = NULL;
3142 struct got_commit_object *commit = NULL;
3143 struct got_object_qid *pid;
3144 struct got_reference *head_ref = NULL;
3145 int ch, did_something = 0;
3147 while ((ch = getopt(argc, argv, "")) != -1) {
3148 switch (ch) {
3149 default:
3150 usage_backout();
3151 /* NOTREACHED */
3155 argc -= optind;
3156 argv += optind;
3158 if (argc != 1)
3159 usage_backout();
3161 cwd = getcwd(NULL, 0);
3162 if (cwd == NULL) {
3163 error = got_error_from_errno("getcwd");
3164 goto done;
3166 error = got_worktree_open(&worktree, cwd);
3167 if (error)
3168 goto done;
3170 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3171 if (error != NULL)
3172 goto done;
3174 error = apply_unveil(got_repo_get_path(repo), 0,
3175 got_worktree_get_root_path(worktree), 0);
3176 if (error)
3177 goto done;
3179 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3180 GOT_OBJ_TYPE_COMMIT, repo);
3181 if (error != NULL) {
3182 struct got_reference *ref;
3183 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3184 goto done;
3185 error = got_ref_open(&ref, repo, argv[0], 0);
3186 if (error != NULL)
3187 goto done;
3188 error = got_ref_resolve(&commit_id, repo, ref);
3189 got_ref_close(ref);
3190 if (error != NULL)
3191 goto done;
3193 error = got_object_id_str(&commit_id_str, commit_id);
3194 if (error)
3195 goto done;
3197 error = got_ref_open(&head_ref, repo,
3198 got_worktree_get_head_ref_name(worktree), 0);
3199 if (error != NULL)
3200 goto done;
3202 error = check_same_branch(commit_id, head_ref, repo);
3203 if (error)
3204 goto done;
3206 error = got_object_open_as_commit(&commit, repo, commit_id);
3207 if (error)
3208 goto done;
3209 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3210 if (pid == NULL) {
3211 error = got_error(GOT_ERR_ROOT_COMMIT);
3212 goto done;
3215 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3216 update_progress, &did_something, check_cancelled, NULL);
3217 if (error != NULL)
3218 goto done;
3220 if (did_something)
3221 printf("Backed out commit %s\n", commit_id_str);
3222 done:
3223 if (commit)
3224 got_object_commit_close(commit);
3225 free(commit_id_str);
3226 if (head_ref)
3227 got_ref_close(head_ref);
3228 if (worktree)
3229 got_worktree_close(worktree);
3230 if (repo)
3231 got_repo_close(repo);
3232 return error;
3235 __dead static void
3236 usage_rebase(void)
3238 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3239 getprogname());
3240 exit(1);
3243 static const struct got_error *
3244 show_rebase_progress(struct got_commit_object *commit,
3245 struct got_object_id *old_id, struct got_object_id *new_id)
3247 const struct got_error *err;
3248 char *old_id_str = NULL, *new_id_str = NULL;
3249 char *logmsg0 = NULL, *logmsg, *nl;
3250 size_t len;
3252 err = got_object_id_str(&old_id_str, old_id);
3253 if (err)
3254 goto done;
3256 if (new_id) {
3257 err = got_object_id_str(&new_id_str, new_id);
3258 if (err)
3259 goto done;
3262 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3263 if (logmsg0 == NULL) {
3264 err = got_error_from_errno("strdup");
3265 goto done;
3267 logmsg = logmsg0;
3269 while (isspace((unsigned char)logmsg[0]))
3270 logmsg++;
3272 old_id_str[12] = '\0';
3273 if (new_id_str)
3274 new_id_str[12] = '\0';
3275 len = strlen(logmsg);
3276 if (len > 42)
3277 len = 42;
3278 logmsg[len] = '\0';
3279 nl = strchr(logmsg, '\n');
3280 if (nl)
3281 *nl = '\0';
3282 printf("%s -> %s: %s\n", old_id_str,
3283 new_id_str ? new_id_str : "no-op change", logmsg);
3284 done:
3285 free(old_id_str);
3286 free(new_id_str);
3287 free(logmsg0);
3288 return err;
3291 static const struct got_error *
3292 rebase_progress(void *arg, unsigned char status, const char *path)
3294 unsigned char *rebase_status = arg;
3296 while (path[0] == '/')
3297 path++;
3298 printf("%c %s\n", status, path);
3300 if (*rebase_status == GOT_STATUS_CONFLICT)
3301 return NULL;
3302 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3303 *rebase_status = status;
3304 return NULL;
3307 static const struct got_error *
3308 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3309 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3310 struct got_repository *repo)
3312 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3313 return got_worktree_rebase_complete(worktree,
3314 new_base_branch, tmp_branch, branch, repo);
3317 static const struct got_error *
3318 rebase_commit(struct got_pathlist_head *merged_paths,
3319 struct got_worktree *worktree, struct got_reference *tmp_branch,
3320 struct got_object_id *commit_id, struct got_repository *repo)
3322 const struct got_error *error;
3323 struct got_commit_object *commit;
3324 struct got_object_id *new_commit_id;
3326 error = got_object_open_as_commit(&commit, repo, commit_id);
3327 if (error)
3328 return error;
3330 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3331 worktree, tmp_branch, commit, commit_id, repo);
3332 if (error) {
3333 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3334 goto done;
3335 error = show_rebase_progress(commit, commit_id, NULL);
3336 } else {
3337 error = show_rebase_progress(commit, commit_id, new_commit_id);
3338 free(new_commit_id);
3340 done:
3341 got_object_commit_close(commit);
3342 return error;
3345 struct check_path_prefix_arg {
3346 const char *path_prefix;
3347 size_t len;
3350 static const struct got_error *
3351 check_path_prefix(void *arg, struct got_blob_object *blob1,
3352 struct got_blob_object *blob2, struct got_object_id *id1,
3353 struct got_object_id *id2, const char *path1, const char *path2,
3354 struct got_repository *repo)
3356 struct check_path_prefix_arg *a = arg;
3358 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3359 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3360 return got_error(GOT_ERR_REBASE_PATH);
3362 return NULL;
3365 static const struct got_error *
3366 rebase_check_path_prefix(struct got_object_id *parent_id,
3367 struct got_object_id *commit_id, const char *path_prefix,
3368 struct got_repository *repo)
3370 const struct got_error *err;
3371 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3372 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3373 struct check_path_prefix_arg cpp_arg;
3375 if (got_path_is_root_dir(path_prefix))
3376 return NULL;
3378 err = got_object_open_as_commit(&commit, repo, commit_id);
3379 if (err)
3380 goto done;
3382 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3383 if (err)
3384 goto done;
3386 err = got_object_open_as_tree(&tree1, repo,
3387 got_object_commit_get_tree_id(parent_commit));
3388 if (err)
3389 goto done;
3391 err = got_object_open_as_tree(&tree2, repo,
3392 got_object_commit_get_tree_id(commit));
3393 if (err)
3394 goto done;
3396 cpp_arg.path_prefix = path_prefix;
3397 cpp_arg.len = strlen(path_prefix);
3398 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3399 &cpp_arg);
3400 done:
3401 if (tree1)
3402 got_object_tree_close(tree1);
3403 if (tree2)
3404 got_object_tree_close(tree2);
3405 if (commit)
3406 got_object_commit_close(commit);
3407 if (parent_commit)
3408 got_object_commit_close(parent_commit);
3409 return err;
3412 static const struct got_error *
3413 cmd_rebase(int argc, char *argv[])
3415 const struct got_error *error = NULL;
3416 struct got_worktree *worktree = NULL;
3417 struct got_repository *repo = NULL;
3418 char *cwd = NULL;
3419 struct got_reference *branch = NULL;
3420 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3421 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3422 struct got_object_id *resume_commit_id = NULL;
3423 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3424 struct got_commit_graph *graph = NULL;
3425 struct got_commit_object *commit = NULL;
3426 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3427 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3428 struct got_object_id_queue commits;
3429 struct got_pathlist_head merged_paths;
3430 const struct got_object_id_queue *parent_ids;
3431 struct got_object_qid *qid, *pid;
3433 SIMPLEQ_INIT(&commits);
3434 TAILQ_INIT(&merged_paths);
3436 while ((ch = getopt(argc, argv, "ac")) != -1) {
3437 switch (ch) {
3438 case 'a':
3439 abort_rebase = 1;
3440 break;
3441 case 'c':
3442 continue_rebase = 1;
3443 break;
3444 default:
3445 usage_rebase();
3446 /* NOTREACHED */
3450 argc -= optind;
3451 argv += optind;
3453 if (abort_rebase && continue_rebase)
3454 usage_rebase();
3455 else if (abort_rebase || continue_rebase) {
3456 if (argc != 0)
3457 usage_rebase();
3458 } else if (argc != 1)
3459 usage_rebase();
3461 cwd = getcwd(NULL, 0);
3462 if (cwd == NULL) {
3463 error = got_error_from_errno("getcwd");
3464 goto done;
3466 error = got_worktree_open(&worktree, cwd);
3467 if (error)
3468 goto done;
3470 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3471 if (error != NULL)
3472 goto done;
3474 error = apply_unveil(got_repo_get_path(repo), 0,
3475 got_worktree_get_root_path(worktree), 0);
3476 if (error)
3477 goto done;
3479 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3480 if (error)
3481 goto done;
3483 if (rebase_in_progress && abort_rebase) {
3484 int did_something;
3485 error = got_worktree_rebase_continue(&resume_commit_id,
3486 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3487 if (error)
3488 goto done;
3489 printf("Switching work tree to %s\n",
3490 got_ref_get_symref_target(new_base_branch));
3491 error = got_worktree_rebase_abort(worktree, repo,
3492 new_base_branch, update_progress, &did_something);
3493 if (error)
3494 goto done;
3495 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3496 goto done; /* nothing else to do */
3497 } else if (abort_rebase) {
3498 error = got_error(GOT_ERR_NOT_REBASING);
3499 goto done;
3502 if (continue_rebase) {
3503 error = got_worktree_rebase_continue(&resume_commit_id,
3504 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3505 if (error)
3506 goto done;
3508 error = rebase_commit(NULL, worktree, tmp_branch,
3509 resume_commit_id, repo);
3510 if (error)
3511 goto done;
3513 yca_id = got_object_id_dup(resume_commit_id);
3514 if (yca_id == NULL) {
3515 error = got_error_from_errno("got_object_id_dup");
3516 goto done;
3518 } else {
3519 error = got_ref_open(&branch, repo, argv[0], 0);
3520 if (error != NULL)
3521 goto done;
3523 error = check_same_branch(
3524 got_worktree_get_base_commit_id(worktree), branch, repo);
3525 if (error) {
3526 if (error->code != GOT_ERR_ANCESTRY)
3527 goto done;
3528 error = NULL;
3529 } else {
3530 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3531 "specified branch resolves to a commit which "
3532 "is already contained in work tree's branch");
3533 goto done;
3537 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3538 if (error)
3539 goto done;
3541 if (!continue_rebase) {
3542 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3543 got_worktree_get_base_commit_id(worktree),
3544 branch_head_commit_id, repo);
3545 if (error)
3546 goto done;
3547 if (yca_id == NULL) {
3548 error = got_error_msg(GOT_ERR_ANCESTRY,
3549 "specified branch shares no common ancestry "
3550 "with work tree's branch");
3551 goto done;
3554 error = got_worktree_rebase_prepare(&new_base_branch,
3555 &tmp_branch, worktree, branch, repo);
3556 if (error)
3557 goto done;
3560 commit_id = branch_head_commit_id;
3561 error = got_object_open_as_commit(&commit, repo, commit_id);
3562 if (error)
3563 goto done;
3565 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3566 if (error)
3567 goto done;
3568 parent_ids = got_object_commit_get_parent_ids(commit);
3569 pid = SIMPLEQ_FIRST(parent_ids);
3570 error = got_commit_graph_iter_start(graph, pid->id, repo);
3571 got_object_commit_close(commit);
3572 commit = NULL;
3573 if (error)
3574 goto done;
3575 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3576 error = got_commit_graph_iter_next(&parent_id, graph);
3577 if (error) {
3578 if (error->code == GOT_ERR_ITER_COMPLETED) {
3579 error = got_error_msg(GOT_ERR_ANCESTRY,
3580 "ran out of commits to rebase before "
3581 "youngest common ancestor commit has "
3582 "been reached?!?");
3583 goto done;
3584 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3585 goto done;
3586 error = got_commit_graph_fetch_commits(graph, 1, repo);
3587 if (error)
3588 goto done;
3589 } else {
3590 error = rebase_check_path_prefix(parent_id, commit_id,
3591 got_worktree_get_path_prefix(worktree), repo);
3592 if (error)
3593 goto done;
3595 error = got_object_qid_alloc(&qid, commit_id);
3596 if (error)
3597 goto done;
3598 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3599 commit_id = parent_id;
3603 if (SIMPLEQ_EMPTY(&commits)) {
3604 if (continue_rebase)
3605 error = rebase_complete(worktree, branch,
3606 new_base_branch, tmp_branch, repo);
3607 else
3608 error = got_error(GOT_ERR_EMPTY_REBASE);
3609 goto done;
3612 pid = NULL;
3613 SIMPLEQ_FOREACH(qid, &commits, entry) {
3614 commit_id = qid->id;
3615 parent_id = pid ? pid->id : yca_id;
3616 pid = qid;
3618 error = got_worktree_rebase_merge_files(&merged_paths,
3619 worktree, parent_id, commit_id, repo, rebase_progress,
3620 &rebase_status, check_cancelled, NULL);
3621 if (error)
3622 goto done;
3624 if (rebase_status == GOT_STATUS_CONFLICT) {
3625 got_worktree_rebase_pathlist_free(&merged_paths);
3626 break;
3629 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3630 commit_id, repo);
3631 got_worktree_rebase_pathlist_free(&merged_paths);
3632 if (error)
3633 goto done;
3636 if (rebase_status == GOT_STATUS_CONFLICT) {
3637 error = got_worktree_rebase_postpone(worktree);
3638 if (error)
3639 goto done;
3640 error = got_error_msg(GOT_ERR_CONFLICTS,
3641 "conflicts must be resolved before rebasing can continue");
3642 } else
3643 error = rebase_complete(worktree, branch, new_base_branch,
3644 tmp_branch, repo);
3645 done:
3646 got_object_id_queue_free(&commits);
3647 free(branch_head_commit_id);
3648 free(resume_commit_id);
3649 free(yca_id);
3650 if (graph)
3651 got_commit_graph_close(graph);
3652 if (commit)
3653 got_object_commit_close(commit);
3654 if (branch)
3655 got_ref_close(branch);
3656 if (new_base_branch)
3657 got_ref_close(new_base_branch);
3658 if (tmp_branch)
3659 got_ref_close(tmp_branch);
3660 if (worktree)
3661 got_worktree_close(worktree);
3662 if (repo)
3663 got_repo_close(repo);
3664 return error;