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;
2076 * Don't let the user create a reference named '-'.
2077 * While technically a valid reference name, this case is usually
2078 * an unintended typo.
2080 if (refname[0] == '-' && refname[1] == '\0')
2081 return got_error(GOT_ERR_BAD_REF_NAME);
2083 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2084 repo);
2085 if (err) {
2086 struct got_reference *target_ref;
2088 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2089 return err;
2090 err = got_ref_open(&target_ref, repo, target, 0);
2091 if (err)
2092 return err;
2093 err = got_ref_resolve(&id, repo, target_ref);
2094 got_ref_close(target_ref);
2095 if (err)
2096 return err;
2099 err = got_ref_alloc(&ref, refname, id);
2100 if (err)
2101 goto done;
2103 err = got_ref_write(ref, repo);
2104 done:
2105 if (ref)
2106 got_ref_close(ref);
2107 free(id);
2108 return err;
2111 static const struct got_error *
2112 cmd_ref(int argc, char *argv[])
2114 const struct got_error *error = NULL;
2115 struct got_repository *repo = NULL;
2116 struct got_worktree *worktree = NULL;
2117 char *cwd = NULL, *repo_path = NULL;
2118 int ch, do_list = 0;
2119 const char *delref = NULL;
2121 /* TODO: Add -s option for adding symbolic references. */
2122 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2123 switch (ch) {
2124 case 'd':
2125 delref = optarg;
2126 break;
2127 case 'r':
2128 repo_path = realpath(optarg, NULL);
2129 if (repo_path == NULL)
2130 err(1, "-r option");
2131 got_path_strip_trailing_slashes(repo_path);
2132 break;
2133 case 'l':
2134 do_list = 1;
2135 break;
2136 default:
2137 usage_ref();
2138 /* NOTREACHED */
2142 if (do_list && delref)
2143 errx(1, "-l and -d options are mutually exclusive\n");
2145 argc -= optind;
2146 argv += optind;
2148 if (do_list || delref) {
2149 if (argc > 0)
2150 usage_ref();
2151 } else if (argc != 2)
2152 usage_ref();
2154 #ifndef PROFILE
2155 if (do_list) {
2156 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2157 NULL) == -1)
2158 err(1, "pledge");
2159 } else {
2160 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2161 "sendfd unveil", NULL) == -1)
2162 err(1, "pledge");
2164 #endif
2165 cwd = getcwd(NULL, 0);
2166 if (cwd == NULL) {
2167 error = got_error_from_errno("getcwd");
2168 goto done;
2171 if (repo_path == NULL) {
2172 error = got_worktree_open(&worktree, cwd);
2173 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2174 goto done;
2175 else
2176 error = NULL;
2177 if (worktree) {
2178 repo_path =
2179 strdup(got_worktree_get_repo_path(worktree));
2180 if (repo_path == NULL)
2181 error = got_error_from_errno("strdup");
2182 if (error)
2183 goto done;
2184 } else {
2185 repo_path = strdup(cwd);
2186 if (repo_path == NULL) {
2187 error = got_error_from_errno("strdup");
2188 goto done;
2193 error = got_repo_open(&repo, repo_path);
2194 if (error != NULL)
2195 goto done;
2197 error = apply_unveil(got_repo_get_path(repo), do_list,
2198 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2199 if (error)
2200 goto done;
2202 if (do_list)
2203 error = list_refs(repo);
2204 else if (delref)
2205 error = delete_ref(repo, delref);
2206 else
2207 error = add_ref(repo, argv[0], argv[1]);
2208 done:
2209 if (repo)
2210 got_repo_close(repo);
2211 if (worktree)
2212 got_worktree_close(worktree);
2213 free(cwd);
2214 free(repo_path);
2215 return error;
2218 __dead static void
2219 usage_branch(void)
2221 fprintf(stderr,
2222 "usage: %s branch [-r repository] -l | -d name | "
2223 "name [base-branch]\n", getprogname());
2224 exit(1);
2227 static const struct got_error *
2228 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2230 static const struct got_error *err = NULL;
2231 struct got_reflist_head refs;
2232 struct got_reflist_entry *re;
2234 SIMPLEQ_INIT(&refs);
2236 err = got_ref_list(&refs, repo);
2237 if (err)
2238 return err;
2240 SIMPLEQ_FOREACH(re, &refs, entry) {
2241 const char *refname, *marker = " ";
2242 char *refstr;
2243 refname = got_ref_get_name(re->ref);
2244 if (strncmp(refname, "refs/heads/", 11) != 0)
2245 continue;
2246 if (worktree && strcmp(refname,
2247 got_worktree_get_head_ref_name(worktree)) == 0) {
2248 struct got_object_id *id = NULL;
2249 err = got_ref_resolve(&id, repo, re->ref);
2250 if (err)
2251 return err;
2252 if (got_object_id_cmp(id,
2253 got_worktree_get_base_commit_id(worktree)) == 0)
2254 marker = "* ";
2255 else
2256 marker = "~ ";
2257 free(id);
2259 refname += 11;
2260 refstr = got_ref_to_str(re->ref);
2261 if (refstr == NULL)
2262 return got_error_from_errno("got_ref_to_str");
2263 printf("%s%s: %s\n", marker, refname, refstr);
2264 free(refstr);
2267 got_ref_list_free(&refs);
2268 return NULL;
2271 static const struct got_error *
2272 delete_branch(struct got_repository *repo, const char *branch_name)
2274 const struct got_error *err = NULL;
2275 struct got_reference *ref;
2276 char *refname;
2278 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2279 return got_error_from_errno("asprintf");
2281 err = got_ref_open(&ref, repo, refname, 0);
2282 if (err)
2283 goto done;
2285 err = got_ref_delete(ref, repo);
2286 got_ref_close(ref);
2287 done:
2288 free(refname);
2289 return err;
2292 static const struct got_error *
2293 add_branch(struct got_repository *repo, const char *branch_name,
2294 const char *base_branch)
2296 const struct got_error *err = NULL;
2297 struct got_object_id *id = NULL;
2298 struct got_reference *ref = NULL;
2299 char *base_refname = NULL, *refname = NULL;
2300 struct got_reference *base_ref;
2303 * Don't let the user create a branch named '-'.
2304 * While technically a valid reference name, this case is usually
2305 * an unintended typo.
2307 if (branch_name[0] == '-' && branch_name[1] == '\0')
2308 return got_error(GOT_ERR_BAD_REF_NAME);
2310 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2311 base_refname = strdup(GOT_REF_HEAD);
2312 if (base_refname == NULL)
2313 return got_error_from_errno("strdup");
2314 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2315 return got_error_from_errno("asprintf");
2317 err = got_ref_open(&base_ref, repo, base_refname, 0);
2318 if (err)
2319 goto done;
2320 err = got_ref_resolve(&id, repo, base_ref);
2321 got_ref_close(base_ref);
2322 if (err)
2323 goto done;
2325 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2326 err = got_error_from_errno("asprintf");
2327 goto done;
2330 err = got_ref_open(&ref, repo, refname, 0);
2331 if (err == NULL) {
2332 err = got_error(GOT_ERR_BRANCH_EXISTS);
2333 goto done;
2334 } else if (err->code != GOT_ERR_NOT_REF)
2335 goto done;
2337 err = got_ref_alloc(&ref, refname, id);
2338 if (err)
2339 goto done;
2341 err = got_ref_write(ref, repo);
2342 done:
2343 if (ref)
2344 got_ref_close(ref);
2345 free(id);
2346 free(base_refname);
2347 free(refname);
2348 return err;
2351 static const struct got_error *
2352 cmd_branch(int argc, char *argv[])
2354 const struct got_error *error = NULL;
2355 struct got_repository *repo = NULL;
2356 struct got_worktree *worktree = NULL;
2357 char *cwd = NULL, *repo_path = NULL;
2358 int ch, do_list = 0;
2359 const char *delref = NULL;
2361 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2362 switch (ch) {
2363 case 'd':
2364 delref = optarg;
2365 break;
2366 case 'r':
2367 repo_path = realpath(optarg, NULL);
2368 if (repo_path == NULL)
2369 err(1, "-r option");
2370 got_path_strip_trailing_slashes(repo_path);
2371 break;
2372 case 'l':
2373 do_list = 1;
2374 break;
2375 default:
2376 usage_branch();
2377 /* NOTREACHED */
2381 if (do_list && delref)
2382 errx(1, "-l and -d options are mutually exclusive\n");
2384 argc -= optind;
2385 argv += optind;
2387 if (do_list || delref) {
2388 if (argc > 0)
2389 usage_branch();
2390 } else if (argc < 1 || argc > 2)
2391 usage_branch();
2393 #ifndef PROFILE
2394 if (do_list) {
2395 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2396 NULL) == -1)
2397 err(1, "pledge");
2398 } else {
2399 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2400 "sendfd unveil", NULL) == -1)
2401 err(1, "pledge");
2403 #endif
2404 cwd = getcwd(NULL, 0);
2405 if (cwd == NULL) {
2406 error = got_error_from_errno("getcwd");
2407 goto done;
2410 if (repo_path == NULL) {
2411 error = got_worktree_open(&worktree, cwd);
2412 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2413 goto done;
2414 else
2415 error = NULL;
2416 if (worktree) {
2417 repo_path =
2418 strdup(got_worktree_get_repo_path(worktree));
2419 if (repo_path == NULL)
2420 error = got_error_from_errno("strdup");
2421 if (error)
2422 goto done;
2423 } else {
2424 repo_path = strdup(cwd);
2425 if (repo_path == NULL) {
2426 error = got_error_from_errno("strdup");
2427 goto done;
2432 error = got_repo_open(&repo, repo_path);
2433 if (error != NULL)
2434 goto done;
2436 error = apply_unveil(got_repo_get_path(repo), do_list,
2437 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2438 if (error)
2439 goto done;
2441 if (do_list)
2442 error = list_branches(repo, worktree);
2443 else if (delref)
2444 error = delete_branch(repo, delref);
2445 else {
2446 const char *base_branch;
2447 if (argc == 1) {
2448 base_branch = worktree ?
2449 got_worktree_get_head_ref_name(worktree) :
2450 GOT_REF_HEAD;
2451 } else
2452 base_branch = argv[1];
2453 error = add_branch(repo, argv[0], base_branch);
2455 done:
2456 if (repo)
2457 got_repo_close(repo);
2458 if (worktree)
2459 got_worktree_close(worktree);
2460 free(cwd);
2461 free(repo_path);
2462 return error;
2465 __dead static void
2466 usage_add(void)
2468 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2469 exit(1);
2472 static const struct got_error *
2473 cmd_add(int argc, char *argv[])
2475 const struct got_error *error = NULL;
2476 struct got_repository *repo = NULL;
2477 struct got_worktree *worktree = NULL;
2478 char *cwd = NULL;
2479 struct got_pathlist_head paths;
2480 struct got_pathlist_entry *pe;
2481 int ch, x;
2483 TAILQ_INIT(&paths);
2485 while ((ch = getopt(argc, argv, "")) != -1) {
2486 switch (ch) {
2487 default:
2488 usage_add();
2489 /* NOTREACHED */
2493 argc -= optind;
2494 argv += optind;
2496 if (argc < 1)
2497 usage_add();
2499 /* make sure each file exists before doing anything halfway */
2500 for (x = 0; x < argc; x++) {
2501 char *path = realpath(argv[x], NULL);
2502 if (path == NULL) {
2503 error = got_error_from_errno2("realpath", argv[x]);
2504 goto done;
2506 free(path);
2509 cwd = getcwd(NULL, 0);
2510 if (cwd == NULL) {
2511 error = got_error_from_errno("getcwd");
2512 goto done;
2515 error = got_worktree_open(&worktree, cwd);
2516 if (error)
2517 goto done;
2519 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2520 if (error != NULL)
2521 goto done;
2523 error = apply_unveil(got_repo_get_path(repo), 1,
2524 got_worktree_get_root_path(worktree), 0);
2525 if (error)
2526 goto done;
2528 for (x = 0; x < argc; x++) {
2529 char *path = realpath(argv[x], NULL);
2530 if (path == NULL) {
2531 error = got_error_from_errno2("realpath", argv[x]);
2532 goto done;
2535 got_path_strip_trailing_slashes(path);
2536 error = got_pathlist_insert(&pe, &paths, path, NULL);
2537 if (error) {
2538 free(path);
2539 goto done;
2542 error = got_worktree_schedule_add(worktree, &paths, print_status,
2543 NULL, repo);
2544 done:
2545 if (repo)
2546 got_repo_close(repo);
2547 if (worktree)
2548 got_worktree_close(worktree);
2549 TAILQ_FOREACH(pe, &paths, entry)
2550 free((char *)pe->path);
2551 got_pathlist_free(&paths);
2552 free(cwd);
2553 return error;
2556 __dead static void
2557 usage_remove(void)
2559 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2560 exit(1);
2563 static const struct got_error *
2564 cmd_remove(int argc, char *argv[])
2566 const struct got_error *error = NULL;
2567 struct got_worktree *worktree = NULL;
2568 struct got_repository *repo = NULL;
2569 char *cwd = NULL;
2570 struct got_pathlist_head paths;
2571 struct got_pathlist_entry *pe;
2572 int ch, i, delete_local_mods = 0;
2574 TAILQ_INIT(&paths);
2576 while ((ch = getopt(argc, argv, "f")) != -1) {
2577 switch (ch) {
2578 case 'f':
2579 delete_local_mods = 1;
2580 break;
2581 default:
2582 usage_add();
2583 /* NOTREACHED */
2587 argc -= optind;
2588 argv += optind;
2590 if (argc < 1)
2591 usage_remove();
2593 /* make sure each file exists before doing anything halfway */
2594 for (i = 0; i < argc; i++) {
2595 char *path = realpath(argv[i], NULL);
2596 if (path == NULL) {
2597 error = got_error_from_errno2("realpath", argv[i]);
2598 goto done;
2600 free(path);
2603 cwd = getcwd(NULL, 0);
2604 if (cwd == NULL) {
2605 error = got_error_from_errno("getcwd");
2606 goto done;
2608 error = got_worktree_open(&worktree, cwd);
2609 if (error)
2610 goto done;
2612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2613 if (error)
2614 goto done;
2616 error = apply_unveil(got_repo_get_path(repo), 1,
2617 got_worktree_get_root_path(worktree), 0);
2618 if (error)
2619 goto done;
2621 for (i = 0; i < argc; i++) {
2622 char *path = realpath(argv[i], NULL);
2623 if (path == NULL) {
2624 error = got_error_from_errno2("realpath", argv[i]);
2625 goto done;
2628 got_path_strip_trailing_slashes(path);
2629 error = got_pathlist_insert(&pe, &paths, path, NULL);
2630 if (error) {
2631 free(path);
2632 goto done;
2635 error = got_worktree_schedule_delete(worktree, &paths,
2636 delete_local_mods, print_status, NULL, repo);
2637 if (error)
2638 goto done;
2639 done:
2640 if (repo)
2641 got_repo_close(repo);
2642 if (worktree)
2643 got_worktree_close(worktree);
2644 TAILQ_FOREACH(pe, &paths, entry)
2645 free((char *)pe->path);
2646 got_pathlist_free(&paths);
2647 free(cwd);
2648 return error;
2651 __dead static void
2652 usage_revert(void)
2654 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2655 exit(1);
2658 static const struct got_error *
2659 revert_progress(void *arg, unsigned char status, const char *path)
2661 while (path[0] == '/')
2662 path++;
2663 printf("%c %s\n", status, path);
2664 return NULL;
2667 static const struct got_error *
2668 cmd_revert(int argc, char *argv[])
2670 const struct got_error *error = NULL;
2671 struct got_worktree *worktree = NULL;
2672 struct got_repository *repo = NULL;
2673 char *cwd = NULL, *path = NULL;
2674 struct got_pathlist_head paths;
2675 struct got_pathlist_entry *pe;
2676 int ch, i;
2678 TAILQ_INIT(&paths);
2680 while ((ch = getopt(argc, argv, "")) != -1) {
2681 switch (ch) {
2682 default:
2683 usage_revert();
2684 /* NOTREACHED */
2688 argc -= optind;
2689 argv += optind;
2691 if (argc < 1)
2692 usage_revert();
2694 for (i = 0; i < argc; i++) {
2695 char *path = realpath(argv[i], NULL);
2696 if (path == NULL) {
2697 error = got_error_from_errno2("realpath", argv[i]);
2698 goto done;
2701 got_path_strip_trailing_slashes(path);
2702 error = got_pathlist_insert(&pe, &paths, path, NULL);
2703 if (error) {
2704 free(path);
2705 goto done;
2709 cwd = getcwd(NULL, 0);
2710 if (cwd == NULL) {
2711 error = got_error_from_errno("getcwd");
2712 goto done;
2714 error = got_worktree_open(&worktree, cwd);
2715 if (error)
2716 goto done;
2718 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2719 if (error != NULL)
2720 goto done;
2722 error = apply_unveil(got_repo_get_path(repo), 1,
2723 got_worktree_get_root_path(worktree), 0);
2724 if (error)
2725 goto done;
2727 error = got_worktree_revert(worktree, &paths,
2728 revert_progress, NULL, repo);
2729 if (error)
2730 goto done;
2731 done:
2732 if (repo)
2733 got_repo_close(repo);
2734 if (worktree)
2735 got_worktree_close(worktree);
2736 free(path);
2737 free(cwd);
2738 return error;
2741 __dead static void
2742 usage_commit(void)
2744 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2745 exit(1);
2748 int
2749 spawn_editor(const char *editor, const char *file)
2751 pid_t pid;
2752 sig_t sighup, sigint, sigquit;
2753 int st = -1;
2755 sighup = signal(SIGHUP, SIG_IGN);
2756 sigint = signal(SIGINT, SIG_IGN);
2757 sigquit = signal(SIGQUIT, SIG_IGN);
2759 switch (pid = fork()) {
2760 case -1:
2761 goto doneediting;
2762 case 0:
2763 execl(editor, editor, file, (char *)NULL);
2764 _exit(127);
2767 while (waitpid(pid, &st, 0) == -1)
2768 if (errno != EINTR)
2769 break;
2771 doneediting:
2772 (void)signal(SIGHUP, sighup);
2773 (void)signal(SIGINT, sigint);
2774 (void)signal(SIGQUIT, sigquit);
2776 if (!WIFEXITED(st)) {
2777 errno = EINTR;
2778 return -1;
2781 return WEXITSTATUS(st);
2784 struct collect_commit_logmsg_arg {
2785 const char *cmdline_log;
2786 const char *editor;
2787 const char *worktree_path;
2788 const char *branch_name;
2789 const char *repo_path;
2790 char *logmsg_path;
2794 static const struct got_error *
2795 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2796 void *arg)
2798 char *initial_content = NULL;
2799 struct got_pathlist_entry *pe;
2800 const struct got_error *err = NULL;
2801 char *template = NULL;
2802 struct collect_commit_logmsg_arg *a = arg;
2803 char buf[1024];
2804 struct stat st, st2;
2805 FILE *fp;
2806 size_t len;
2807 int fd, content_changed = 0;
2809 /* if a message was specified on the command line, just use it */
2810 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2811 len = strlen(a->cmdline_log) + 1;
2812 *logmsg = malloc(len + 1);
2813 if (*logmsg == NULL)
2814 return got_error_from_errno("malloc");
2815 strlcpy(*logmsg, a->cmdline_log, len);
2816 return NULL;
2819 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2820 return got_error_from_errno("asprintf");
2822 if (asprintf(&initial_content,
2823 "\n# changes to be committed on branch %s:\n",
2824 a->branch_name) == -1)
2825 return got_error_from_errno("asprintf");
2827 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2828 if (err)
2829 goto done;
2831 dprintf(fd, initial_content);
2833 TAILQ_FOREACH(pe, commitable_paths, entry) {
2834 struct got_commitable *ct = pe->data;
2835 dprintf(fd, "# %c %s\n",
2836 got_commitable_get_status(ct),
2837 got_commitable_get_path(ct));
2839 close(fd);
2841 if (stat(a->logmsg_path, &st) == -1) {
2842 err = got_error_from_errno2("stat", a->logmsg_path);
2843 goto done;
2846 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2847 err = got_error_from_errno("failed spawning editor");
2848 goto done;
2851 if (stat(a->logmsg_path, &st2) == -1) {
2852 err = got_error_from_errno("stat");
2853 goto done;
2856 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2857 unlink(a->logmsg_path);
2858 free(a->logmsg_path);
2859 a->logmsg_path = NULL;
2860 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2861 "no changes made to commit message, aborting");
2862 goto done;
2865 *logmsg = malloc(st2.st_size + 1);
2866 if (*logmsg == NULL) {
2867 err = got_error_from_errno("malloc");
2868 goto done;
2870 (*logmsg)[0] = '\0';
2871 len = 0;
2873 fp = fopen(a->logmsg_path, "r");
2874 if (fp == NULL) {
2875 err = got_error_from_errno("fopen");
2876 goto done;
2878 while (fgets(buf, sizeof(buf), fp) != NULL) {
2879 if (!content_changed && strcmp(buf, initial_content) != 0)
2880 content_changed = 1;
2881 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2882 continue; /* remove comments and leading empty lines */
2883 len = strlcat(*logmsg, buf, st2.st_size);
2885 fclose(fp);
2887 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2888 (*logmsg)[len - 1] = '\0';
2889 len--;
2892 if (len == 0 || !content_changed) {
2893 unlink(a->logmsg_path);
2894 free(a->logmsg_path);
2895 a->logmsg_path = NULL;
2896 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2897 "commit message cannot be empty, aborting");
2898 goto done;
2900 done:
2901 free(initial_content);
2902 free(template);
2904 /* Editor is done; we can now apply unveil(2) */
2905 if (err == NULL)
2906 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2907 return err;
2910 static const struct got_error *
2911 cmd_commit(int argc, char *argv[])
2913 const struct got_error *error = NULL;
2914 struct got_worktree *worktree = NULL;
2915 struct got_repository *repo = NULL;
2916 char *cwd = NULL, *path = NULL, *id_str = NULL;
2917 struct got_object_id *id = NULL;
2918 const char *logmsg = NULL;
2919 const char *got_author = getenv("GOT_AUTHOR");
2920 struct collect_commit_logmsg_arg cl_arg;
2921 char *editor = NULL;
2922 int ch, rebase_in_progress;
2924 while ((ch = getopt(argc, argv, "m:")) != -1) {
2925 switch (ch) {
2926 case 'm':
2927 logmsg = optarg;
2928 break;
2929 default:
2930 usage_commit();
2931 /* NOTREACHED */
2935 argc -= optind;
2936 argv += optind;
2938 if (argc == 1) {
2939 path = realpath(argv[0], NULL);
2940 if (path == NULL) {
2941 error = got_error_from_errno2("realpath", argv[0]);
2942 goto done;
2944 got_path_strip_trailing_slashes(path);
2945 } else if (argc != 0)
2946 usage_commit();
2948 if (got_author == NULL) {
2949 /* TODO: Look current user up in password database */
2950 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2951 goto done;
2954 cwd = getcwd(NULL, 0);
2955 if (cwd == NULL) {
2956 error = got_error_from_errno("getcwd");
2957 goto done;
2959 error = got_worktree_open(&worktree, cwd);
2960 if (error)
2961 goto done;
2963 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2964 if (error)
2965 goto done;
2966 if (rebase_in_progress) {
2967 error = got_error(GOT_ERR_REBASING);
2968 goto done;
2971 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2972 if (error != NULL)
2973 goto done;
2976 * unveil(2) traverses exec(2); if an editor is used we have
2977 * to apply unveil after the log message has been written.
2979 if (logmsg == NULL || strlen(logmsg) == 0)
2980 error = get_editor(&editor);
2981 else
2982 error = apply_unveil(got_repo_get_path(repo), 0,
2983 got_worktree_get_root_path(worktree), 0);
2984 if (error)
2985 goto done;
2987 cl_arg.editor = editor;
2988 cl_arg.cmdline_log = logmsg;
2989 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2990 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2991 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2992 cl_arg.branch_name += 5;
2993 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2994 cl_arg.branch_name += 6;
2995 cl_arg.repo_path = got_repo_get_path(repo);
2996 cl_arg.logmsg_path = NULL;
2997 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2998 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2999 if (error) {
3000 if (cl_arg.logmsg_path)
3001 fprintf(stderr, "%s: log message preserved in %s\n",
3002 getprogname(), cl_arg.logmsg_path);
3003 goto done;
3006 if (cl_arg.logmsg_path)
3007 unlink(cl_arg.logmsg_path);
3009 error = got_object_id_str(&id_str, id);
3010 if (error)
3011 goto done;
3012 printf("Created commit %s\n", id_str);
3013 done:
3014 if (repo)
3015 got_repo_close(repo);
3016 if (worktree)
3017 got_worktree_close(worktree);
3018 free(path);
3019 free(cwd);
3020 free(id_str);
3021 free(editor);
3022 return error;
3025 __dead static void
3026 usage_cherrypick(void)
3028 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3029 exit(1);
3032 static const struct got_error *
3033 cmd_cherrypick(int argc, char *argv[])
3035 const struct got_error *error = NULL;
3036 struct got_worktree *worktree = NULL;
3037 struct got_repository *repo = NULL;
3038 char *cwd = NULL, *commit_id_str = NULL;
3039 struct got_object_id *commit_id = NULL;
3040 struct got_commit_object *commit = NULL;
3041 struct got_object_qid *pid;
3042 struct got_reference *head_ref = NULL;
3043 int ch, did_something = 0;
3045 while ((ch = getopt(argc, argv, "")) != -1) {
3046 switch (ch) {
3047 default:
3048 usage_cherrypick();
3049 /* NOTREACHED */
3053 argc -= optind;
3054 argv += optind;
3056 if (argc != 1)
3057 usage_cherrypick();
3059 cwd = getcwd(NULL, 0);
3060 if (cwd == NULL) {
3061 error = got_error_from_errno("getcwd");
3062 goto done;
3064 error = got_worktree_open(&worktree, cwd);
3065 if (error)
3066 goto done;
3068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3069 if (error != NULL)
3070 goto done;
3072 error = apply_unveil(got_repo_get_path(repo), 0,
3073 got_worktree_get_root_path(worktree), 0);
3074 if (error)
3075 goto done;
3077 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3078 GOT_OBJ_TYPE_COMMIT, repo);
3079 if (error != NULL) {
3080 struct got_reference *ref;
3081 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3082 goto done;
3083 error = got_ref_open(&ref, repo, argv[0], 0);
3084 if (error != NULL)
3085 goto done;
3086 error = got_ref_resolve(&commit_id, repo, ref);
3087 got_ref_close(ref);
3088 if (error != NULL)
3089 goto done;
3091 error = got_object_id_str(&commit_id_str, commit_id);
3092 if (error)
3093 goto done;
3095 error = got_ref_open(&head_ref, repo,
3096 got_worktree_get_head_ref_name(worktree), 0);
3097 if (error != NULL)
3098 goto done;
3100 error = check_same_branch(commit_id, head_ref, repo);
3101 if (error) {
3102 if (error->code != GOT_ERR_ANCESTRY)
3103 goto done;
3104 error = NULL;
3105 } else {
3106 error = got_error(GOT_ERR_SAME_BRANCH);
3107 goto done;
3110 error = got_object_open_as_commit(&commit, repo, commit_id);
3111 if (error)
3112 goto done;
3113 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3114 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3115 commit_id, repo, update_progress, &did_something, check_cancelled,
3116 NULL);
3117 if (error != NULL)
3118 goto done;
3120 if (did_something)
3121 printf("Merged commit %s\n", commit_id_str);
3122 done:
3123 if (commit)
3124 got_object_commit_close(commit);
3125 free(commit_id_str);
3126 if (head_ref)
3127 got_ref_close(head_ref);
3128 if (worktree)
3129 got_worktree_close(worktree);
3130 if (repo)
3131 got_repo_close(repo);
3132 return error;
3135 __dead static void
3136 usage_backout(void)
3138 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3139 exit(1);
3142 static const struct got_error *
3143 cmd_backout(int argc, char *argv[])
3145 const struct got_error *error = NULL;
3146 struct got_worktree *worktree = NULL;
3147 struct got_repository *repo = NULL;
3148 char *cwd = NULL, *commit_id_str = NULL;
3149 struct got_object_id *commit_id = NULL;
3150 struct got_commit_object *commit = NULL;
3151 struct got_object_qid *pid;
3152 struct got_reference *head_ref = NULL;
3153 int ch, did_something = 0;
3155 while ((ch = getopt(argc, argv, "")) != -1) {
3156 switch (ch) {
3157 default:
3158 usage_backout();
3159 /* NOTREACHED */
3163 argc -= optind;
3164 argv += optind;
3166 if (argc != 1)
3167 usage_backout();
3169 cwd = getcwd(NULL, 0);
3170 if (cwd == NULL) {
3171 error = got_error_from_errno("getcwd");
3172 goto done;
3174 error = got_worktree_open(&worktree, cwd);
3175 if (error)
3176 goto done;
3178 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3179 if (error != NULL)
3180 goto done;
3182 error = apply_unveil(got_repo_get_path(repo), 0,
3183 got_worktree_get_root_path(worktree), 0);
3184 if (error)
3185 goto done;
3187 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3188 GOT_OBJ_TYPE_COMMIT, repo);
3189 if (error != NULL) {
3190 struct got_reference *ref;
3191 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3192 goto done;
3193 error = got_ref_open(&ref, repo, argv[0], 0);
3194 if (error != NULL)
3195 goto done;
3196 error = got_ref_resolve(&commit_id, repo, ref);
3197 got_ref_close(ref);
3198 if (error != NULL)
3199 goto done;
3201 error = got_object_id_str(&commit_id_str, commit_id);
3202 if (error)
3203 goto done;
3205 error = got_ref_open(&head_ref, repo,
3206 got_worktree_get_head_ref_name(worktree), 0);
3207 if (error != NULL)
3208 goto done;
3210 error = check_same_branch(commit_id, head_ref, repo);
3211 if (error)
3212 goto done;
3214 error = got_object_open_as_commit(&commit, repo, commit_id);
3215 if (error)
3216 goto done;
3217 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3218 if (pid == NULL) {
3219 error = got_error(GOT_ERR_ROOT_COMMIT);
3220 goto done;
3223 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3224 update_progress, &did_something, check_cancelled, NULL);
3225 if (error != NULL)
3226 goto done;
3228 if (did_something)
3229 printf("Backed out commit %s\n", commit_id_str);
3230 done:
3231 if (commit)
3232 got_object_commit_close(commit);
3233 free(commit_id_str);
3234 if (head_ref)
3235 got_ref_close(head_ref);
3236 if (worktree)
3237 got_worktree_close(worktree);
3238 if (repo)
3239 got_repo_close(repo);
3240 return error;
3243 __dead static void
3244 usage_rebase(void)
3246 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3247 getprogname());
3248 exit(1);
3251 static const struct got_error *
3252 show_rebase_progress(struct got_commit_object *commit,
3253 struct got_object_id *old_id, struct got_object_id *new_id)
3255 const struct got_error *err;
3256 char *old_id_str = NULL, *new_id_str = NULL;
3257 char *logmsg0 = NULL, *logmsg, *nl;
3258 size_t len;
3260 err = got_object_id_str(&old_id_str, old_id);
3261 if (err)
3262 goto done;
3264 if (new_id) {
3265 err = got_object_id_str(&new_id_str, new_id);
3266 if (err)
3267 goto done;
3270 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3271 if (logmsg0 == NULL) {
3272 err = got_error_from_errno("strdup");
3273 goto done;
3275 logmsg = logmsg0;
3277 while (isspace((unsigned char)logmsg[0]))
3278 logmsg++;
3280 old_id_str[12] = '\0';
3281 if (new_id_str)
3282 new_id_str[12] = '\0';
3283 len = strlen(logmsg);
3284 if (len > 42)
3285 len = 42;
3286 logmsg[len] = '\0';
3287 nl = strchr(logmsg, '\n');
3288 if (nl)
3289 *nl = '\0';
3290 printf("%s -> %s: %s\n", old_id_str,
3291 new_id_str ? new_id_str : "no-op change", logmsg);
3292 done:
3293 free(old_id_str);
3294 free(new_id_str);
3295 free(logmsg0);
3296 return err;
3299 static const struct got_error *
3300 rebase_progress(void *arg, unsigned char status, const char *path)
3302 unsigned char *rebase_status = arg;
3304 while (path[0] == '/')
3305 path++;
3306 printf("%c %s\n", status, path);
3308 if (*rebase_status == GOT_STATUS_CONFLICT)
3309 return NULL;
3310 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3311 *rebase_status = status;
3312 return NULL;
3315 static const struct got_error *
3316 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3317 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3318 struct got_repository *repo)
3320 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3321 return got_worktree_rebase_complete(worktree,
3322 new_base_branch, tmp_branch, branch, repo);
3325 static const struct got_error *
3326 rebase_commit(struct got_pathlist_head *merged_paths,
3327 struct got_worktree *worktree, struct got_reference *tmp_branch,
3328 struct got_object_id *commit_id, struct got_repository *repo)
3330 const struct got_error *error;
3331 struct got_commit_object *commit;
3332 struct got_object_id *new_commit_id;
3334 error = got_object_open_as_commit(&commit, repo, commit_id);
3335 if (error)
3336 return error;
3338 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3339 worktree, tmp_branch, commit, commit_id, repo);
3340 if (error) {
3341 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3342 goto done;
3343 error = show_rebase_progress(commit, commit_id, NULL);
3344 } else {
3345 error = show_rebase_progress(commit, commit_id, new_commit_id);
3346 free(new_commit_id);
3348 done:
3349 got_object_commit_close(commit);
3350 return error;
3353 struct check_path_prefix_arg {
3354 const char *path_prefix;
3355 size_t len;
3358 static const struct got_error *
3359 check_path_prefix(void *arg, struct got_blob_object *blob1,
3360 struct got_blob_object *blob2, struct got_object_id *id1,
3361 struct got_object_id *id2, const char *path1, const char *path2,
3362 struct got_repository *repo)
3364 struct check_path_prefix_arg *a = arg;
3366 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3367 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3368 return got_error(GOT_ERR_REBASE_PATH);
3370 return NULL;
3373 static const struct got_error *
3374 rebase_check_path_prefix(struct got_object_id *parent_id,
3375 struct got_object_id *commit_id, const char *path_prefix,
3376 struct got_repository *repo)
3378 const struct got_error *err;
3379 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3380 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3381 struct check_path_prefix_arg cpp_arg;
3383 if (got_path_is_root_dir(path_prefix))
3384 return NULL;
3386 err = got_object_open_as_commit(&commit, repo, commit_id);
3387 if (err)
3388 goto done;
3390 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3391 if (err)
3392 goto done;
3394 err = got_object_open_as_tree(&tree1, repo,
3395 got_object_commit_get_tree_id(parent_commit));
3396 if (err)
3397 goto done;
3399 err = got_object_open_as_tree(&tree2, repo,
3400 got_object_commit_get_tree_id(commit));
3401 if (err)
3402 goto done;
3404 cpp_arg.path_prefix = path_prefix;
3405 cpp_arg.len = strlen(path_prefix);
3406 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3407 &cpp_arg);
3408 done:
3409 if (tree1)
3410 got_object_tree_close(tree1);
3411 if (tree2)
3412 got_object_tree_close(tree2);
3413 if (commit)
3414 got_object_commit_close(commit);
3415 if (parent_commit)
3416 got_object_commit_close(parent_commit);
3417 return err;
3420 static const struct got_error *
3421 cmd_rebase(int argc, char *argv[])
3423 const struct got_error *error = NULL;
3424 struct got_worktree *worktree = NULL;
3425 struct got_repository *repo = NULL;
3426 char *cwd = NULL;
3427 struct got_reference *branch = NULL;
3428 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3429 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3430 struct got_object_id *resume_commit_id = NULL;
3431 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3432 struct got_commit_graph *graph = NULL;
3433 struct got_commit_object *commit = NULL;
3434 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3435 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3436 struct got_object_id_queue commits;
3437 struct got_pathlist_head merged_paths;
3438 const struct got_object_id_queue *parent_ids;
3439 struct got_object_qid *qid, *pid;
3441 SIMPLEQ_INIT(&commits);
3442 TAILQ_INIT(&merged_paths);
3444 while ((ch = getopt(argc, argv, "ac")) != -1) {
3445 switch (ch) {
3446 case 'a':
3447 abort_rebase = 1;
3448 break;
3449 case 'c':
3450 continue_rebase = 1;
3451 break;
3452 default:
3453 usage_rebase();
3454 /* NOTREACHED */
3458 argc -= optind;
3459 argv += optind;
3461 if (abort_rebase && continue_rebase)
3462 usage_rebase();
3463 else if (abort_rebase || continue_rebase) {
3464 if (argc != 0)
3465 usage_rebase();
3466 } else if (argc != 1)
3467 usage_rebase();
3469 cwd = getcwd(NULL, 0);
3470 if (cwd == NULL) {
3471 error = got_error_from_errno("getcwd");
3472 goto done;
3474 error = got_worktree_open(&worktree, cwd);
3475 if (error)
3476 goto done;
3478 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3479 if (error != NULL)
3480 goto done;
3482 error = apply_unveil(got_repo_get_path(repo), 0,
3483 got_worktree_get_root_path(worktree), 0);
3484 if (error)
3485 goto done;
3487 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3488 if (error)
3489 goto done;
3491 if (rebase_in_progress && abort_rebase) {
3492 int did_something;
3493 error = got_worktree_rebase_continue(&resume_commit_id,
3494 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3495 if (error)
3496 goto done;
3497 printf("Switching work tree to %s\n",
3498 got_ref_get_symref_target(new_base_branch));
3499 error = got_worktree_rebase_abort(worktree, repo,
3500 new_base_branch, update_progress, &did_something);
3501 if (error)
3502 goto done;
3503 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3504 goto done; /* nothing else to do */
3505 } else if (abort_rebase) {
3506 error = got_error(GOT_ERR_NOT_REBASING);
3507 goto done;
3510 if (continue_rebase) {
3511 error = got_worktree_rebase_continue(&resume_commit_id,
3512 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3513 if (error)
3514 goto done;
3516 error = rebase_commit(NULL, worktree, tmp_branch,
3517 resume_commit_id, repo);
3518 if (error)
3519 goto done;
3521 yca_id = got_object_id_dup(resume_commit_id);
3522 if (yca_id == NULL) {
3523 error = got_error_from_errno("got_object_id_dup");
3524 goto done;
3526 } else {
3527 error = got_ref_open(&branch, repo, argv[0], 0);
3528 if (error != NULL)
3529 goto done;
3531 error = check_same_branch(
3532 got_worktree_get_base_commit_id(worktree), branch, repo);
3533 if (error) {
3534 if (error->code != GOT_ERR_ANCESTRY)
3535 goto done;
3536 error = NULL;
3537 } else {
3538 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3539 "specified branch resolves to a commit which "
3540 "is already contained in work tree's branch");
3541 goto done;
3545 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3546 if (error)
3547 goto done;
3549 if (!continue_rebase) {
3550 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3551 got_worktree_get_base_commit_id(worktree),
3552 branch_head_commit_id, repo);
3553 if (error)
3554 goto done;
3555 if (yca_id == NULL) {
3556 error = got_error_msg(GOT_ERR_ANCESTRY,
3557 "specified branch shares no common ancestry "
3558 "with work tree's branch");
3559 goto done;
3562 error = got_worktree_rebase_prepare(&new_base_branch,
3563 &tmp_branch, worktree, branch, repo);
3564 if (error)
3565 goto done;
3568 commit_id = branch_head_commit_id;
3569 error = got_object_open_as_commit(&commit, repo, commit_id);
3570 if (error)
3571 goto done;
3573 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3574 if (error)
3575 goto done;
3576 parent_ids = got_object_commit_get_parent_ids(commit);
3577 pid = SIMPLEQ_FIRST(parent_ids);
3578 error = got_commit_graph_iter_start(graph, pid->id, repo);
3579 got_object_commit_close(commit);
3580 commit = NULL;
3581 if (error)
3582 goto done;
3583 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3584 error = got_commit_graph_iter_next(&parent_id, graph);
3585 if (error) {
3586 if (error->code == GOT_ERR_ITER_COMPLETED) {
3587 error = got_error_msg(GOT_ERR_ANCESTRY,
3588 "ran out of commits to rebase before "
3589 "youngest common ancestor commit has "
3590 "been reached?!?");
3591 goto done;
3592 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3593 goto done;
3594 error = got_commit_graph_fetch_commits(graph, 1, repo);
3595 if (error)
3596 goto done;
3597 } else {
3598 error = rebase_check_path_prefix(parent_id, commit_id,
3599 got_worktree_get_path_prefix(worktree), repo);
3600 if (error)
3601 goto done;
3603 error = got_object_qid_alloc(&qid, commit_id);
3604 if (error)
3605 goto done;
3606 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3607 commit_id = parent_id;
3611 if (SIMPLEQ_EMPTY(&commits)) {
3612 if (continue_rebase)
3613 error = rebase_complete(worktree, branch,
3614 new_base_branch, tmp_branch, repo);
3615 else
3616 error = got_error(GOT_ERR_EMPTY_REBASE);
3617 goto done;
3620 pid = NULL;
3621 SIMPLEQ_FOREACH(qid, &commits, entry) {
3622 commit_id = qid->id;
3623 parent_id = pid ? pid->id : yca_id;
3624 pid = qid;
3626 error = got_worktree_rebase_merge_files(&merged_paths,
3627 worktree, parent_id, commit_id, repo, rebase_progress,
3628 &rebase_status, check_cancelled, NULL);
3629 if (error)
3630 goto done;
3632 if (rebase_status == GOT_STATUS_CONFLICT) {
3633 got_worktree_rebase_pathlist_free(&merged_paths);
3634 break;
3637 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3638 commit_id, repo);
3639 got_worktree_rebase_pathlist_free(&merged_paths);
3640 if (error)
3641 goto done;
3644 if (rebase_status == GOT_STATUS_CONFLICT) {
3645 error = got_worktree_rebase_postpone(worktree);
3646 if (error)
3647 goto done;
3648 error = got_error_msg(GOT_ERR_CONFLICTS,
3649 "conflicts must be resolved before rebasing can continue");
3650 } else
3651 error = rebase_complete(worktree, branch, new_base_branch,
3652 tmp_branch, repo);
3653 done:
3654 got_object_id_queue_free(&commits);
3655 free(branch_head_commit_id);
3656 free(resume_commit_id);
3657 free(yca_id);
3658 if (graph)
3659 got_commit_graph_close(graph);
3660 if (commit)
3661 got_object_commit_close(commit);
3662 if (branch)
3663 got_ref_close(branch);
3664 if (new_base_branch)
3665 got_ref_close(new_base_branch);
3666 if (tmp_branch)
3667 got_ref_close(tmp_branch);
3668 if (worktree)
3669 got_worktree_close(worktree);
3670 if (repo)
3671 got_repo_close(repo);
3672 return error;