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_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
96 __dead static void usage_histedit(void);
98 static const struct got_error* cmd_init(int, char *[]);
99 static const struct got_error* cmd_import(int, char *[]);
100 static const struct got_error* cmd_checkout(int, char *[]);
101 static const struct got_error* cmd_update(int, char *[]);
102 static const struct got_error* cmd_log(int, char *[]);
103 static const struct got_error* cmd_diff(int, char *[]);
104 static const struct got_error* cmd_blame(int, char *[]);
105 static const struct got_error* cmd_tree(int, char *[]);
106 static const struct got_error* cmd_status(int, char *[]);
107 static const struct got_error* cmd_ref(int, char *[]);
108 static const struct got_error* cmd_branch(int, char *[]);
109 static const struct got_error* cmd_add(int, char *[]);
110 static const struct got_error* cmd_remove(int, char *[]);
111 static const struct got_error* cmd_revert(int, char *[]);
112 static const struct got_error* cmd_commit(int, char *[]);
113 static const struct got_error* cmd_cherrypick(int, char *[]);
114 static const struct got_error* cmd_backout(int, char *[]);
115 static const struct got_error* cmd_rebase(int, char *[]);
116 static const struct got_error* cmd_histedit(int, char *[]);
118 static struct got_cmd got_commands[] = {
119 { "init", cmd_init, usage_init, "" },
120 { "import", cmd_import, usage_import, "" },
121 { "checkout", cmd_checkout, usage_checkout, "co" },
122 { "update", cmd_update, usage_update, "up" },
123 { "log", cmd_log, usage_log, "" },
124 { "diff", cmd_diff, usage_diff, "" },
125 { "blame", cmd_blame, usage_blame, "" },
126 { "tree", cmd_tree, usage_tree, "" },
127 { "status", cmd_status, usage_status, "st" },
128 { "ref", cmd_ref, usage_ref, "" },
129 { "branch", cmd_branch, usage_branch, "br" },
130 { "add", cmd_add, usage_add, "" },
131 { "remove", cmd_remove, usage_remove, "rm" },
132 { "revert", cmd_revert, usage_revert, "rv" },
133 { "commit", cmd_commit, usage_commit, "ci" },
134 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
135 { "backout", cmd_backout, usage_backout, "bo" },
136 { "rebase", cmd_rebase, usage_rebase, "rb" },
137 { "histedit", cmd_histedit, usage_histedit, "he" },
138 };
140 static void
141 list_commands(void)
143 int i;
145 fprintf(stderr, "commands:");
146 for (i = 0; i < nitems(got_commands); i++) {
147 struct got_cmd *cmd = &got_commands[i];
148 fprintf(stderr, " %s", cmd->cmd_name);
150 fputc('\n', stderr);
153 int
154 main(int argc, char *argv[])
156 struct got_cmd *cmd;
157 unsigned int i;
158 int ch;
159 int hflag = 0;
161 setlocale(LC_CTYPE, "");
163 while ((ch = getopt(argc, argv, "h")) != -1) {
164 switch (ch) {
165 case 'h':
166 hflag = 1;
167 break;
168 default:
169 usage(hflag);
170 /* NOTREACHED */
174 argc -= optind;
175 argv += optind;
176 optind = 0;
178 if (argc <= 0)
179 usage(hflag);
181 signal(SIGINT, catch_sigint);
182 signal(SIGPIPE, catch_sigpipe);
184 for (i = 0; i < nitems(got_commands); i++) {
185 const struct got_error *error;
187 cmd = &got_commands[i];
189 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
190 strcmp(cmd->cmd_alias, argv[0]) != 0)
191 continue;
193 if (hflag)
194 got_commands[i].cmd_usage();
196 error = got_commands[i].cmd_main(argc, argv);
197 if (error && !(sigint_received || sigpipe_received)) {
198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
199 return 1;
202 return 0;
205 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
206 list_commands();
207 return 1;
210 __dead static void
211 usage(int hflag)
213 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
214 if (hflag)
215 list_commands();
216 exit(1);
219 static const struct got_error *
220 get_editor(char **abspath)
222 const struct got_error *err = NULL;
223 const char *editor;
225 editor = getenv("VISUAL");
226 if (editor == NULL)
227 editor = getenv("EDITOR");
229 if (editor) {
230 err = got_path_find_prog(abspath, editor);
231 if (err)
232 return err;
235 if (*abspath == NULL) {
236 *abspath = strdup("/bin/ed");
237 if (*abspath == NULL)
238 return got_error_from_errno("strdup");
241 return NULL;
244 static const struct got_error *
245 apply_unveil(const char *repo_path, int repo_read_only,
246 const char *worktree_path)
248 const struct got_error *err;
250 #ifdef PROFILE
251 if (unveil("gmon.out", "rwc") != 0)
252 return got_error_from_errno2("unveil", "gmon.out");
253 #endif
254 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
255 return got_error_from_errno2("unveil", repo_path);
257 if (worktree_path && unveil(worktree_path, "rwc") != 0)
258 return got_error_from_errno2("unveil", worktree_path);
260 if (unveil("/tmp", "rwc") != 0)
261 return got_error_from_errno2("unveil", "/tmp");
263 err = got_privsep_unveil_exec_helpers();
264 if (err != NULL)
265 return err;
267 if (unveil(NULL, NULL) != 0)
268 return got_error_from_errno("unveil");
270 return NULL;
273 __dead static void
274 usage_init(void)
276 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
277 exit(1);
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 char *repo_path = NULL;
285 int ch;
287 while ((ch = getopt(argc, argv, "")) != -1) {
288 switch (ch) {
289 default:
290 usage_init();
291 /* NOTREACHED */
295 argc -= optind;
296 argv += optind;
298 #ifndef PROFILE
299 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
300 err(1, "pledge");
301 #endif
302 if (argc != 1)
303 usage_init();
305 repo_path = strdup(argv[0]);
306 if (repo_path == NULL)
307 return got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(repo_path);
311 error = got_path_mkdir(repo_path);
312 if (error &&
313 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
314 goto done;
316 error = apply_unveil(repo_path, 0, NULL);
317 if (error)
318 goto done;
320 error = got_repo_init(repo_path);
321 if (error != NULL)
322 goto done;
324 done:
325 free(repo_path);
326 return error;
329 __dead static void
330 usage_import(void)
332 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 "[-r repository-path] [-I pattern] path\n", getprogname());
334 exit(1);
337 int
338 spawn_editor(const char *editor, const char *file)
340 pid_t pid;
341 sig_t sighup, sigint, sigquit;
342 int st = -1;
344 sighup = signal(SIGHUP, SIG_IGN);
345 sigint = signal(SIGINT, SIG_IGN);
346 sigquit = signal(SIGQUIT, SIG_IGN);
348 switch (pid = fork()) {
349 case -1:
350 goto doneediting;
351 case 0:
352 execl(editor, editor, file, (char *)NULL);
353 _exit(127);
356 while (waitpid(pid, &st, 0) == -1)
357 if (errno != EINTR)
358 break;
360 doneediting:
361 (void)signal(SIGHUP, sighup);
362 (void)signal(SIGINT, sigint);
363 (void)signal(SIGQUIT, sigquit);
365 if (!WIFEXITED(st)) {
366 errno = EINTR;
367 return -1;
370 return WEXITSTATUS(st);
373 static const struct got_error *
374 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 const char *initial_content)
377 const struct got_error *err = NULL;
378 char buf[1024];
379 struct stat st, st2;
380 FILE *fp;
381 int content_changed = 0;
382 size_t len;
384 *logmsg = NULL;
386 if (stat(logmsg_path, &st) == -1)
387 return got_error_from_errno2("stat", logmsg_path);
389 if (spawn_editor(editor, logmsg_path) == -1)
390 return got_error_from_errno("failed spawning editor");
392 if (stat(logmsg_path, &st2) == -1)
393 return got_error_from_errno("stat");
395 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 "no changes made to commit message, aborting");
399 *logmsg = malloc(st2.st_size + 1);
400 if (*logmsg == NULL)
401 return got_error_from_errno("malloc");
402 (*logmsg)[0] = '\0';
403 len = 0;
405 fp = fopen(logmsg_path, "r");
406 if (fp == NULL) {
407 err = got_error_from_errno("fopen");
408 goto done;
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 if (!content_changed && strcmp(buf, initial_content) != 0)
412 content_changed = 1;
413 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 len = strlcat(*logmsg, buf, st2.st_size);
417 fclose(fp);
419 while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 (*logmsg)[len - 1] = '\0';
421 len--;
424 if (len == 0 || !content_changed)
425 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 "commit message cannot be empty, aborting");
427 done:
428 if (err) {
429 free(*logmsg);
430 *logmsg = NULL;
432 return err;
435 static const struct got_error *
436 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 const char *branch_name)
439 char *initial_content = NULL, *logmsg_path = NULL;
440 const struct got_error *err = NULL;
441 int fd;
443 if (asprintf(&initial_content,
444 "\n# %s to be imported to branch %s\n", path_dir,
445 branch_name) == -1)
446 return got_error_from_errno("asprintf");
448 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
449 if (err)
450 goto done;
452 dprintf(fd, initial_content);
453 close(fd);
455 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
456 done:
457 free(initial_content);
458 free(logmsg_path);
459 return err;
462 static const struct got_error *
463 import_progress(void *arg, const char *path)
465 printf("A %s\n", path);
466 return NULL;
469 static const struct got_error *
470 cmd_import(int argc, char *argv[])
472 const struct got_error *error = NULL;
473 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
474 char *editor = NULL;
475 const char *got_author = getenv("GOT_AUTHOR");
476 const char *branch_name = "master";
477 char *refname = NULL, *id_str = NULL;
478 struct got_repository *repo = NULL;
479 struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 struct got_object_id *new_commit_id = NULL;
481 int ch;
482 struct got_pathlist_head ignores;
483 struct got_pathlist_entry *pe;
485 TAILQ_INIT(&ignores);
487 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
488 switch (ch) {
489 case 'b':
490 branch_name = optarg;
491 break;
492 case 'm':
493 logmsg = strdup(optarg);
494 if (logmsg == NULL) {
495 error = got_error_from_errno("strdup");
496 goto done;
498 break;
499 case 'r':
500 repo_path = realpath(optarg, NULL);
501 if (repo_path == NULL) {
502 error = got_error_from_errno("realpath");
503 goto done;
505 break;
506 case 'I':
507 if (optarg[0] == '\0')
508 break;
509 error = got_pathlist_insert(&pe, &ignores, optarg,
510 NULL);
511 if (error)
512 goto done;
513 break;
514 default:
515 usage_init();
516 /* NOTREACHED */
520 argc -= optind;
521 argv += optind;
523 #ifndef PROFILE
524 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
525 NULL) == -1)
526 err(1, "pledge");
527 #endif
528 if (argc != 1)
529 usage_import();
531 if (got_author == NULL) {
532 /* TODO: Look current user up in password database */
533 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 goto done;
537 if (repo_path == NULL) {
538 repo_path = getcwd(NULL, 0);
539 if (repo_path == NULL)
540 return got_error_from_errno("getcwd");
542 got_path_strip_trailing_slashes(repo_path);
543 error = got_repo_open(&repo, repo_path);
544 if (error)
545 goto done;
547 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 error = got_error_from_errno("asprintf");
549 goto done;
552 error = got_ref_open(&branch_ref, repo, refname, 0);
553 if (error) {
554 if (error->code != GOT_ERR_NOT_REF)
555 goto done;
556 } else {
557 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 "import target branch already exists");
559 goto done;
562 path_dir = realpath(argv[0], NULL);
563 if (path_dir == NULL) {
564 error = got_error_from_errno("realpath");
565 goto done;
567 got_path_strip_trailing_slashes(path_dir);
569 /*
570 * unveil(2) traverses exec(2); if an editor is used we have
571 * to apply unveil after the log message has been written.
572 */
573 if (logmsg == NULL || strlen(logmsg) == 0) {
574 error = get_editor(&editor);
575 if (error)
576 goto done;
577 error = collect_import_msg(&logmsg, editor, path_dir, refname);
578 if (error)
579 goto done;
582 if (unveil(path_dir, "r") != 0)
583 return got_error_from_errno2("unveil", path_dir);
585 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
586 if (error)
587 goto done;
589 error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 got_author, &ignores, repo, import_progress, NULL);
591 if (error)
592 goto done;
594 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
595 if (error)
596 goto done;
598 error = got_ref_write(branch_ref, repo);
599 if (error)
600 goto done;
602 error = got_object_id_str(&id_str, new_commit_id);
603 if (error)
604 goto done;
606 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
607 if (error) {
608 if (error->code != GOT_ERR_NOT_REF)
609 goto done;
611 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
612 branch_ref);
613 if (error)
614 goto done;
616 error = got_ref_write(head_ref, repo);
617 if (error)
618 goto done;
621 printf("Created branch %s with commit %s\n",
622 got_ref_get_name(branch_ref), id_str);
623 done:
624 free(repo_path);
625 free(editor);
626 free(refname);
627 free(new_commit_id);
628 free(id_str);
629 if (branch_ref)
630 got_ref_close(branch_ref);
631 if (head_ref)
632 got_ref_close(head_ref);
633 return error;
636 __dead static void
637 usage_checkout(void)
639 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
641 exit(1);
644 static const struct got_error *
645 checkout_progress(void *arg, unsigned char status, const char *path)
647 char *worktree_path = arg;
649 /* Base commit bump happens silently. */
650 if (status == GOT_STATUS_BUMP_BASE)
651 return NULL;
653 while (path[0] == '/')
654 path++;
656 printf("%c %s/%s\n", status, worktree_path, path);
657 return NULL;
660 static const struct got_error *
661 check_cancelled(void *arg)
663 if (sigint_received || sigpipe_received)
664 return got_error(GOT_ERR_CANCELLED);
665 return NULL;
668 static const struct got_error *
669 check_linear_ancestry(struct got_object_id *commit_id,
670 struct got_object_id *base_commit_id, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 struct got_object_id *yca_id;
675 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 commit_id, base_commit_id, repo);
677 if (err)
678 return err;
680 if (yca_id == NULL)
681 return got_error(GOT_ERR_ANCESTRY);
683 /*
684 * Require a straight line of history between the target commit
685 * and the work tree's base commit.
687 * Non-linear situations such as this require a rebase:
689 * (commit) D F (base_commit)
690 * \ /
691 * C E
692 * \ /
693 * B (yca)
694 * |
695 * A
697 * 'got update' only handles linear cases:
698 * Update forwards in time: A (base/yca) - B - C - D (commit)
699 * Update backwards in time: D (base) - C - B - A (commit/yca)
700 */
701 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 got_object_id_cmp(base_commit_id, yca_id) != 0)
703 return got_error(GOT_ERR_ANCESTRY);
705 free(yca_id);
706 return NULL;
709 static const struct got_error *
710 check_same_branch(struct got_object_id *commit_id,
711 struct got_reference *head_ref, struct got_repository *repo)
713 const struct got_error *err = NULL;
714 struct got_commit_graph *graph = NULL;
715 struct got_object_id *head_commit_id = NULL;
716 int is_same_branch = 0;
718 err = got_ref_resolve(&head_commit_id, repo, head_ref);
719 if (err)
720 goto done;
722 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
723 if (err)
724 goto done;
726 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
727 if (err)
728 goto done;
730 for (;;) {
731 struct got_object_id *id;
732 err = got_commit_graph_iter_next(&id, graph);
733 if (err) {
734 if (err->code == GOT_ERR_ITER_COMPLETED) {
735 err = NULL;
736 break;
737 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
738 break;
739 err = got_commit_graph_fetch_commits(graph, 1,
740 repo);
741 if (err)
742 break;
745 if (id) {
746 if (got_object_id_cmp(id, commit_id) == 0) {
747 is_same_branch = 1;
748 break;
752 done:
753 if (graph)
754 got_commit_graph_close(graph);
755 free(head_commit_id);
756 if (!err && !is_same_branch)
757 err = got_error(GOT_ERR_ANCESTRY);
758 return err;
761 static const struct got_error *
762 resolve_commit_arg(struct got_object_id **commit_id,
763 const char *commit_id_arg, struct got_repository *repo)
765 const struct got_error *err;
766 struct got_reference *ref;
768 err = got_ref_open(&ref, repo, commit_id_arg, 0);
769 if (err == NULL) {
770 err = got_ref_resolve(commit_id, repo, ref);
771 got_ref_close(ref);
772 } else {
773 if (err->code != GOT_ERR_NOT_REF)
774 return err;
775 err = got_repo_match_object_id_prefix(commit_id,
776 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
778 return err;
781 static const struct got_error *
782 cmd_checkout(int argc, char *argv[])
784 const struct got_error *error = NULL;
785 struct got_repository *repo = NULL;
786 struct got_reference *head_ref = NULL;
787 struct got_worktree *worktree = NULL;
788 char *repo_path = NULL;
789 char *worktree_path = NULL;
790 const char *path_prefix = "";
791 const char *branch_name = GOT_REF_HEAD;
792 char *commit_id_str = NULL;
793 int ch, same_path_prefix;
795 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
796 switch (ch) {
797 case 'b':
798 branch_name = optarg;
799 break;
800 case 'c':
801 commit_id_str = strdup(optarg);
802 if (commit_id_str == NULL)
803 return got_error_from_errno("strdup");
804 break;
805 case 'p':
806 path_prefix = optarg;
807 break;
808 default:
809 usage_checkout();
810 /* NOTREACHED */
814 argc -= optind;
815 argv += optind;
817 #ifndef PROFILE
818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
819 "unveil", NULL) == -1)
820 err(1, "pledge");
821 #endif
822 if (argc == 1) {
823 char *cwd, *base, *dotgit;
824 repo_path = realpath(argv[0], NULL);
825 if (repo_path == NULL)
826 return got_error_from_errno2("realpath", argv[0]);
827 cwd = getcwd(NULL, 0);
828 if (cwd == NULL) {
829 error = got_error_from_errno("getcwd");
830 goto done;
832 if (path_prefix[0]) {
833 base = basename(path_prefix);
834 if (base == NULL) {
835 error = got_error_from_errno2("basename",
836 path_prefix);
837 goto done;
839 } else {
840 base = basename(repo_path);
841 if (base == NULL) {
842 error = got_error_from_errno2("basename",
843 repo_path);
844 goto done;
847 dotgit = strstr(base, ".git");
848 if (dotgit)
849 *dotgit = '\0';
850 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
851 error = got_error_from_errno("asprintf");
852 free(cwd);
853 goto done;
855 free(cwd);
856 } else if (argc == 2) {
857 repo_path = realpath(argv[0], NULL);
858 if (repo_path == NULL) {
859 error = got_error_from_errno2("realpath", argv[0]);
860 goto done;
862 worktree_path = realpath(argv[1], NULL);
863 if (worktree_path == NULL) {
864 if (errno != ENOENT) {
865 error = got_error_from_errno2("realpath",
866 argv[1]);
867 goto done;
869 worktree_path = strdup(argv[1]);
870 if (worktree_path == NULL) {
871 error = got_error_from_errno("strdup");
872 goto done;
875 } else
876 usage_checkout();
878 got_path_strip_trailing_slashes(repo_path);
879 got_path_strip_trailing_slashes(worktree_path);
881 error = got_repo_open(&repo, repo_path);
882 if (error != NULL)
883 goto done;
885 /* Pre-create work tree path for unveil(2) */
886 error = got_path_mkdir(worktree_path);
887 if (error) {
888 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
889 goto done;
890 if (!got_path_dir_is_empty(worktree_path)) {
891 error = got_error_path(worktree_path,
892 GOT_ERR_DIR_NOT_EMPTY);
893 goto done;
897 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
898 if (error)
899 goto done;
901 error = got_ref_open(&head_ref, repo, branch_name, 0);
902 if (error != NULL)
903 goto done;
905 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
906 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
907 goto done;
909 error = got_worktree_open(&worktree, worktree_path);
910 if (error != NULL)
911 goto done;
913 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
914 path_prefix);
915 if (error != NULL)
916 goto done;
917 if (!same_path_prefix) {
918 error = got_error(GOT_ERR_PATH_PREFIX);
919 goto done;
922 if (commit_id_str) {
923 struct got_object_id *commit_id;
924 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
925 if (error)
926 goto done;
927 error = check_linear_ancestry(commit_id,
928 got_worktree_get_base_commit_id(worktree), repo);
929 if (error != NULL) {
930 free(commit_id);
931 goto done;
933 error = check_same_branch(commit_id, head_ref, repo);
934 if (error)
935 goto done;
936 error = got_worktree_set_base_commit_id(worktree, repo,
937 commit_id);
938 free(commit_id);
939 if (error)
940 goto done;
943 error = got_worktree_checkout_files(worktree, "", repo,
944 checkout_progress, worktree_path, check_cancelled, NULL);
945 if (error != NULL)
946 goto done;
948 printf("Now shut up and hack\n");
950 done:
951 free(commit_id_str);
952 free(repo_path);
953 free(worktree_path);
954 return error;
957 __dead static void
958 usage_update(void)
960 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
961 getprogname());
962 exit(1);
965 static const struct got_error *
966 update_progress(void *arg, unsigned char status, const char *path)
968 int *did_something = arg;
970 if (status == GOT_STATUS_EXISTS)
971 return NULL;
973 *did_something = 1;
975 /* Base commit bump happens silently. */
976 if (status == GOT_STATUS_BUMP_BASE)
977 return NULL;
979 while (path[0] == '/')
980 path++;
981 printf("%c %s\n", status, path);
982 return NULL;
985 static const struct got_error *
986 switch_head_ref(struct got_reference *head_ref,
987 struct got_object_id *commit_id, struct got_worktree *worktree,
988 struct got_repository *repo)
990 const struct got_error *err = NULL;
991 char *base_id_str;
992 int ref_has_moved = 0;
994 /* Trivial case: switching between two different references. */
995 if (strcmp(got_ref_get_name(head_ref),
996 got_worktree_get_head_ref_name(worktree)) != 0) {
997 printf("Switching work tree from %s to %s\n",
998 got_worktree_get_head_ref_name(worktree),
999 got_ref_get_name(head_ref));
1000 return got_worktree_set_head_ref(worktree, head_ref);
1003 err = check_linear_ancestry(commit_id,
1004 got_worktree_get_base_commit_id(worktree), repo);
1005 if (err) {
1006 if (err->code != GOT_ERR_ANCESTRY)
1007 return err;
1008 ref_has_moved = 1;
1010 if (!ref_has_moved)
1011 return NULL;
1013 /* Switching to a rebased branch with the same reference name. */
1014 err = got_object_id_str(&base_id_str,
1015 got_worktree_get_base_commit_id(worktree));
1016 if (err)
1017 return err;
1018 printf("Reference %s now points at a different branch\n",
1019 got_worktree_get_head_ref_name(worktree));
1020 printf("Switching work tree from %s to %s\n", base_id_str,
1021 got_worktree_get_head_ref_name(worktree));
1022 return NULL;
1025 static const struct got_error *
1026 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1028 const struct got_error *err;
1029 int in_progress;
1031 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1032 if (err)
1033 return err;
1034 if (in_progress)
1035 return got_error(GOT_ERR_REBASING);
1037 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1038 if (err)
1039 return err;
1040 if (in_progress)
1041 return got_error(GOT_ERR_HISTEDIT_BUSY);
1043 return NULL;
1046 static const struct got_error *
1047 cmd_update(int argc, char *argv[])
1049 const struct got_error *error = NULL;
1050 struct got_repository *repo = NULL;
1051 struct got_worktree *worktree = NULL;
1052 char *worktree_path = NULL, *path = NULL;
1053 struct got_object_id *commit_id = NULL;
1054 char *commit_id_str = NULL;
1055 const char *branch_name = NULL;
1056 struct got_reference *head_ref = NULL;
1057 int ch, did_something = 0;
1059 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1060 switch (ch) {
1061 case 'b':
1062 branch_name = optarg;
1063 break;
1064 case 'c':
1065 commit_id_str = strdup(optarg);
1066 if (commit_id_str == NULL)
1067 return got_error_from_errno("strdup");
1068 break;
1069 default:
1070 usage_update();
1071 /* NOTREACHED */
1075 argc -= optind;
1076 argv += optind;
1078 #ifndef PROFILE
1079 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1080 "unveil", NULL) == -1)
1081 err(1, "pledge");
1082 #endif
1083 worktree_path = getcwd(NULL, 0);
1084 if (worktree_path == NULL) {
1085 error = got_error_from_errno("getcwd");
1086 goto done;
1088 error = got_worktree_open(&worktree, worktree_path);
1089 if (error)
1090 goto done;
1092 error = check_rebase_or_histedit_in_progress(worktree);
1093 if (error)
1094 goto done;
1096 if (argc == 0) {
1097 path = strdup("");
1098 if (path == NULL) {
1099 error = got_error_from_errno("strdup");
1100 goto done;
1102 } else if (argc == 1) {
1103 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1104 if (error)
1105 goto done;
1106 } else
1107 usage_update();
1109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1110 if (error != NULL)
1111 goto done;
1113 error = apply_unveil(got_repo_get_path(repo), 0,
1114 got_worktree_get_root_path(worktree));
1115 if (error)
1116 goto done;
1118 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1119 got_worktree_get_head_ref_name(worktree), 0);
1120 if (error != NULL)
1121 goto done;
1122 if (commit_id_str == NULL) {
1123 error = got_ref_resolve(&commit_id, repo, head_ref);
1124 if (error != NULL)
1125 goto done;
1126 error = got_object_id_str(&commit_id_str, commit_id);
1127 if (error != NULL)
1128 goto done;
1129 } else {
1130 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1131 free(commit_id_str);
1132 commit_id_str = NULL;
1133 if (error)
1134 goto done;
1135 error = got_object_id_str(&commit_id_str, commit_id);
1136 if (error)
1137 goto done;
1140 if (branch_name) {
1141 struct got_object_id *head_commit_id;
1142 if (strlen(path) != 0) {
1143 fprintf(stderr, "%s: switching between branches "
1144 "requires that the entire work tree "
1145 "gets updated, not just '%s'\n",
1146 getprogname(), path);
1147 error = got_error(GOT_ERR_BAD_PATH);
1148 goto done;
1150 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1151 if (error)
1152 goto done;
1153 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1154 free(head_commit_id);
1155 if (error != NULL)
1156 goto done;
1157 error = check_same_branch(commit_id, head_ref, repo);
1158 if (error)
1159 goto done;
1160 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1161 if (error)
1162 goto done;
1163 } else {
1164 error = check_linear_ancestry(commit_id,
1165 got_worktree_get_base_commit_id(worktree), repo);
1166 if (error != NULL) {
1167 if (error->code == GOT_ERR_ANCESTRY)
1168 error = got_error(GOT_ERR_BRANCH_MOVED);
1169 goto done;
1171 error = check_same_branch(commit_id, head_ref, repo);
1172 if (error)
1173 goto done;
1176 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1177 commit_id) != 0) {
1178 error = got_worktree_set_base_commit_id(worktree, repo,
1179 commit_id);
1180 if (error)
1181 goto done;
1184 error = got_worktree_checkout_files(worktree, path, repo,
1185 update_progress, &did_something, check_cancelled, NULL);
1186 if (error != NULL)
1187 goto done;
1189 if (did_something)
1190 printf("Updated to commit %s\n", commit_id_str);
1191 else
1192 printf("Already up-to-date\n");
1193 done:
1194 free(worktree_path);
1195 free(path);
1196 free(commit_id);
1197 free(commit_id_str);
1198 return error;
1201 static const struct got_error *
1202 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1203 int diff_context, struct got_repository *repo)
1205 const struct got_error *err = NULL;
1206 struct got_tree_object *tree1 = NULL, *tree2;
1207 struct got_object_qid *qid;
1208 char *id_str1 = NULL, *id_str2;
1209 struct got_diff_blob_output_unidiff_arg arg;
1211 err = got_object_open_as_tree(&tree2, repo,
1212 got_object_commit_get_tree_id(commit));
1213 if (err)
1214 return err;
1216 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1217 if (qid != NULL) {
1218 struct got_commit_object *pcommit;
1220 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1221 if (err)
1222 return err;
1224 err = got_object_open_as_tree(&tree1, repo,
1225 got_object_commit_get_tree_id(pcommit));
1226 got_object_commit_close(pcommit);
1227 if (err)
1228 return err;
1230 err = got_object_id_str(&id_str1, qid->id);
1231 if (err)
1232 return err;
1235 err = got_object_id_str(&id_str2, id);
1236 if (err)
1237 goto done;
1239 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1240 arg.diff_context = diff_context;
1241 arg.outfile = stdout;
1242 err = got_diff_tree(tree1, tree2, "", "", repo,
1243 got_diff_blob_output_unidiff, &arg);
1244 done:
1245 if (tree1)
1246 got_object_tree_close(tree1);
1247 got_object_tree_close(tree2);
1248 free(id_str1);
1249 free(id_str2);
1250 return err;
1253 static char *
1254 get_datestr(time_t *time, char *datebuf)
1256 char *p, *s = ctime_r(time, datebuf);
1257 p = strchr(s, '\n');
1258 if (p)
1259 *p = '\0';
1260 return s;
1263 static const struct got_error *
1264 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1265 struct got_repository *repo, int show_patch, int diff_context,
1266 struct got_reflist_head *refs)
1268 const struct got_error *err = NULL;
1269 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1270 char datebuf[26];
1271 time_t committer_time;
1272 const char *author, *committer;
1273 char *refs_str = NULL;
1274 struct got_reflist_entry *re;
1276 SIMPLEQ_FOREACH(re, refs, entry) {
1277 char *s;
1278 const char *name;
1279 if (got_object_id_cmp(re->id, id) != 0)
1280 continue;
1281 name = got_ref_get_name(re->ref);
1282 if (strcmp(name, GOT_REF_HEAD) == 0)
1283 continue;
1284 if (strncmp(name, "refs/", 5) == 0)
1285 name += 5;
1286 if (strncmp(name, "got/", 4) == 0)
1287 continue;
1288 if (strncmp(name, "heads/", 6) == 0)
1289 name += 6;
1290 if (strncmp(name, "remotes/", 8) == 0)
1291 name += 8;
1292 s = refs_str;
1293 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1294 name) == -1) {
1295 err = got_error_from_errno("asprintf");
1296 free(s);
1297 break;
1299 free(s);
1301 err = got_object_id_str(&id_str, id);
1302 if (err)
1303 return err;
1305 printf("-----------------------------------------------\n");
1306 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1307 refs_str ? refs_str : "", refs_str ? ")" : "");
1308 free(id_str);
1309 id_str = NULL;
1310 free(refs_str);
1311 refs_str = NULL;
1312 printf("from: %s\n", got_object_commit_get_author(commit));
1313 committer_time = got_object_commit_get_committer_time(commit);
1314 datestr = get_datestr(&committer_time, datebuf);
1315 printf("date: %s UTC\n", datestr);
1316 author = got_object_commit_get_author(commit);
1317 committer = got_object_commit_get_committer(commit);
1318 if (strcmp(author, committer) != 0)
1319 printf("via: %s\n", committer);
1320 if (got_object_commit_get_nparents(commit) > 1) {
1321 const struct got_object_id_queue *parent_ids;
1322 struct got_object_qid *qid;
1323 int n = 1;
1324 parent_ids = got_object_commit_get_parent_ids(commit);
1325 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1326 err = got_object_id_str(&id_str, qid->id);
1327 if (err)
1328 return err;
1329 printf("parent %d: %s\n", n++, id_str);
1330 free(id_str);
1334 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1335 if (logmsg0 == NULL)
1336 return got_error_from_errno("strdup");
1338 logmsg = logmsg0;
1339 do {
1340 line = strsep(&logmsg, "\n");
1341 if (line)
1342 printf(" %s\n", line);
1343 } while (line);
1344 free(logmsg0);
1346 if (show_patch) {
1347 err = print_patch(commit, id, diff_context, repo);
1348 if (err == 0)
1349 printf("\n");
1352 if (fflush(stdout) != 0 && err == NULL)
1353 err = got_error_from_errno("fflush");
1354 return err;
1357 static const struct got_error *
1358 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1359 char *path, int show_patch, int diff_context, int limit,
1360 int first_parent_traversal, struct got_reflist_head *refs)
1362 const struct got_error *err;
1363 struct got_commit_graph *graph;
1365 err = got_commit_graph_open(&graph, root_id, path,
1366 first_parent_traversal, repo);
1367 if (err)
1368 return err;
1369 err = got_commit_graph_iter_start(graph, root_id, repo);
1370 if (err)
1371 goto done;
1372 for (;;) {
1373 struct got_commit_object *commit;
1374 struct got_object_id *id;
1376 if (sigint_received || sigpipe_received)
1377 break;
1379 err = got_commit_graph_iter_next(&id, graph);
1380 if (err) {
1381 if (err->code == GOT_ERR_ITER_COMPLETED) {
1382 err = NULL;
1383 break;
1385 if (err->code != GOT_ERR_ITER_NEED_MORE)
1386 break;
1387 err = got_commit_graph_fetch_commits(graph, 1, repo);
1388 if (err)
1389 break;
1390 else
1391 continue;
1393 if (id == NULL)
1394 break;
1396 err = got_object_open_as_commit(&commit, repo, id);
1397 if (err)
1398 break;
1399 err = print_commit(commit, id, repo, show_patch, diff_context,
1400 refs);
1401 got_object_commit_close(commit);
1402 if (err || (limit && --limit == 0))
1403 break;
1405 done:
1406 got_commit_graph_close(graph);
1407 return err;
1410 __dead static void
1411 usage_log(void)
1413 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1414 "[-r repository-path] [path]\n", getprogname());
1415 exit(1);
1418 static const struct got_error *
1419 cmd_log(int argc, char *argv[])
1421 const struct got_error *error;
1422 struct got_repository *repo = NULL;
1423 struct got_worktree *worktree = NULL;
1424 struct got_commit_object *commit = NULL;
1425 struct got_object_id *id = NULL;
1426 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1427 char *start_commit = NULL;
1428 int diff_context = 3, ch;
1429 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1430 const char *errstr;
1431 struct got_reflist_head refs;
1433 SIMPLEQ_INIT(&refs);
1435 #ifndef PROFILE
1436 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1437 NULL)
1438 == -1)
1439 err(1, "pledge");
1440 #endif
1442 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1443 switch (ch) {
1444 case 'p':
1445 show_patch = 1;
1446 break;
1447 case 'c':
1448 start_commit = optarg;
1449 break;
1450 case 'C':
1451 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1452 &errstr);
1453 if (errstr != NULL)
1454 err(1, "-C option %s", errstr);
1455 break;
1456 case 'l':
1457 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1458 if (errstr != NULL)
1459 err(1, "-l option %s", errstr);
1460 break;
1461 case 'f':
1462 first_parent_traversal = 1;
1463 break;
1464 case 'r':
1465 repo_path = realpath(optarg, NULL);
1466 if (repo_path == NULL)
1467 err(1, "-r option");
1468 got_path_strip_trailing_slashes(repo_path);
1469 break;
1470 default:
1471 usage_log();
1472 /* NOTREACHED */
1476 argc -= optind;
1477 argv += optind;
1479 cwd = getcwd(NULL, 0);
1480 if (cwd == NULL) {
1481 error = got_error_from_errno("getcwd");
1482 goto done;
1485 error = got_worktree_open(&worktree, cwd);
1486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1487 goto done;
1488 error = NULL;
1490 if (argc == 0) {
1491 path = strdup("");
1492 if (path == NULL) {
1493 error = got_error_from_errno("strdup");
1494 goto done;
1496 } else if (argc == 1) {
1497 if (worktree) {
1498 error = got_worktree_resolve_path(&path, worktree,
1499 argv[0]);
1500 if (error)
1501 goto done;
1502 } else {
1503 path = strdup(argv[0]);
1504 if (path == NULL) {
1505 error = got_error_from_errno("strdup");
1506 goto done;
1509 } else
1510 usage_log();
1512 if (repo_path == NULL) {
1513 repo_path = worktree ?
1514 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1516 if (repo_path == NULL) {
1517 error = got_error_from_errno("strdup");
1518 goto done;
1521 error = got_repo_open(&repo, repo_path);
1522 if (error != NULL)
1523 goto done;
1525 error = apply_unveil(got_repo_get_path(repo), 1,
1526 worktree ? got_worktree_get_root_path(worktree) : NULL);
1527 if (error)
1528 goto done;
1530 if (start_commit == NULL) {
1531 struct got_reference *head_ref;
1532 error = got_ref_open(&head_ref, repo,
1533 worktree ? got_worktree_get_head_ref_name(worktree)
1534 : GOT_REF_HEAD, 0);
1535 if (error != NULL)
1536 return error;
1537 error = got_ref_resolve(&id, repo, head_ref);
1538 got_ref_close(head_ref);
1539 if (error != NULL)
1540 return error;
1541 error = got_object_open_as_commit(&commit, repo, id);
1542 } else {
1543 struct got_reference *ref;
1544 error = got_ref_open(&ref, repo, start_commit, 0);
1545 if (error == NULL) {
1546 int obj_type;
1547 error = got_ref_resolve(&id, repo, ref);
1548 got_ref_close(ref);
1549 if (error != NULL)
1550 goto done;
1551 error = got_object_get_type(&obj_type, repo, id);
1552 if (error != NULL)
1553 goto done;
1554 if (obj_type == GOT_OBJ_TYPE_TAG) {
1555 struct got_tag_object *tag;
1556 error = got_object_open_as_tag(&tag, repo, id);
1557 if (error != NULL)
1558 goto done;
1559 if (got_object_tag_get_object_type(tag) !=
1560 GOT_OBJ_TYPE_COMMIT) {
1561 got_object_tag_close(tag);
1562 error = got_error(GOT_ERR_OBJ_TYPE);
1563 goto done;
1565 free(id);
1566 id = got_object_id_dup(
1567 got_object_tag_get_object_id(tag));
1568 if (id == NULL)
1569 error = got_error_from_errno(
1570 "got_object_id_dup");
1571 got_object_tag_close(tag);
1572 if (error)
1573 goto done;
1574 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1575 error = got_error(GOT_ERR_OBJ_TYPE);
1576 goto done;
1578 error = got_object_open_as_commit(&commit, repo, id);
1579 if (error != NULL)
1580 goto done;
1582 if (commit == NULL) {
1583 error = got_repo_match_object_id_prefix(&id,
1584 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1585 if (error != NULL)
1586 return error;
1589 if (error != NULL)
1590 goto done;
1592 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1593 if (error != NULL)
1594 goto done;
1595 if (in_repo_path) {
1596 free(path);
1597 path = in_repo_path;
1600 error = got_ref_list(&refs, repo);
1601 if (error)
1602 goto done;
1604 error = print_commits(id, repo, path, show_patch,
1605 diff_context, limit, first_parent_traversal, &refs);
1606 done:
1607 free(path);
1608 free(repo_path);
1609 free(cwd);
1610 free(id);
1611 if (worktree)
1612 got_worktree_close(worktree);
1613 if (repo) {
1614 const struct got_error *repo_error;
1615 repo_error = got_repo_close(repo);
1616 if (error == NULL)
1617 error = repo_error;
1619 got_ref_list_free(&refs);
1620 return error;
1623 __dead static void
1624 usage_diff(void)
1626 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1627 "[object1 object2 | path]\n", getprogname());
1628 exit(1);
1631 struct print_diff_arg {
1632 struct got_repository *repo;
1633 struct got_worktree *worktree;
1634 int diff_context;
1635 const char *id_str;
1636 int header_shown;
1639 static const struct got_error *
1640 print_diff(void *arg, unsigned char status, const char *path,
1641 struct got_object_id *blob_id, struct got_object_id *commit_id)
1643 struct print_diff_arg *a = arg;
1644 const struct got_error *err = NULL;
1645 struct got_blob_object *blob1 = NULL;
1646 FILE *f2 = NULL;
1647 char *abspath = NULL;
1648 struct stat sb;
1650 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1651 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1652 return NULL;
1654 if (!a->header_shown) {
1655 printf("diff %s %s\n", a->id_str,
1656 got_worktree_get_root_path(a->worktree));
1657 a->header_shown = 1;
1660 if (status != GOT_STATUS_ADD) {
1661 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1662 if (err)
1663 goto done;
1667 if (status != GOT_STATUS_DELETE) {
1668 if (asprintf(&abspath, "%s/%s",
1669 got_worktree_get_root_path(a->worktree), path) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 goto done;
1674 f2 = fopen(abspath, "r");
1675 if (f2 == NULL) {
1676 err = got_error_from_errno2("fopen", abspath);
1677 goto done;
1679 if (lstat(abspath, &sb) == -1) {
1680 err = got_error_from_errno2("lstat", abspath);
1681 goto done;
1683 } else
1684 sb.st_size = 0;
1686 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1687 stdout);
1688 done:
1689 if (blob1)
1690 got_object_blob_close(blob1);
1691 if (f2 && fclose(f2) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1693 free(abspath);
1694 return err;
1697 static const struct got_error *
1698 cmd_diff(int argc, char *argv[])
1700 const struct got_error *error;
1701 struct got_repository *repo = NULL;
1702 struct got_worktree *worktree = NULL;
1703 char *cwd = NULL, *repo_path = NULL;
1704 struct got_object_id *id1 = NULL, *id2 = NULL;
1705 const char *id_str1 = NULL, *id_str2 = NULL;
1706 char *label1 = NULL, *label2 = NULL;
1707 int type1, type2;
1708 int diff_context = 3, ch;
1709 const char *errstr;
1710 char *path = NULL;
1712 #ifndef PROFILE
1713 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1714 NULL) == -1)
1715 err(1, "pledge");
1716 #endif
1718 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1719 switch (ch) {
1720 case 'C':
1721 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1722 if (errstr != NULL)
1723 err(1, "-C option %s", errstr);
1724 break;
1725 case 'r':
1726 repo_path = realpath(optarg, NULL);
1727 if (repo_path == NULL)
1728 err(1, "-r option");
1729 got_path_strip_trailing_slashes(repo_path);
1730 break;
1731 default:
1732 usage_diff();
1733 /* NOTREACHED */
1737 argc -= optind;
1738 argv += optind;
1740 cwd = getcwd(NULL, 0);
1741 if (cwd == NULL) {
1742 error = got_error_from_errno("getcwd");
1743 goto done;
1745 error = got_worktree_open(&worktree, cwd);
1746 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1747 goto done;
1748 if (argc <= 1) {
1749 if (worktree == NULL) {
1750 error = got_error(GOT_ERR_NOT_WORKTREE);
1751 goto done;
1753 if (repo_path)
1754 errx(1,
1755 "-r option can't be used when diffing a work tree");
1756 repo_path = strdup(got_worktree_get_repo_path(worktree));
1757 if (repo_path == NULL) {
1758 error = got_error_from_errno("strdup");
1759 goto done;
1761 if (argc == 1) {
1762 error = got_worktree_resolve_path(&path, worktree,
1763 argv[0]);
1764 if (error)
1765 goto done;
1766 } else {
1767 path = strdup("");
1768 if (path == NULL) {
1769 error = got_error_from_errno("strdup");
1770 goto done;
1773 } else if (argc == 2) {
1774 id_str1 = argv[0];
1775 id_str2 = argv[1];
1776 if (worktree && repo_path == NULL) {
1777 repo_path =
1778 strdup(got_worktree_get_repo_path(worktree));
1779 if (repo_path == NULL) {
1780 error = got_error_from_errno("strdup");
1781 goto done;
1784 } else
1785 usage_diff();
1787 if (repo_path == NULL) {
1788 repo_path = getcwd(NULL, 0);
1789 if (repo_path == NULL)
1790 return got_error_from_errno("getcwd");
1793 error = got_repo_open(&repo, repo_path);
1794 free(repo_path);
1795 if (error != NULL)
1796 goto done;
1798 error = apply_unveil(got_repo_get_path(repo), 1,
1799 worktree ? got_worktree_get_root_path(worktree) : NULL);
1800 if (error)
1801 goto done;
1803 if (argc <= 1) {
1804 struct print_diff_arg arg;
1805 struct got_pathlist_head paths;
1806 char *id_str;
1808 TAILQ_INIT(&paths);
1810 error = got_object_id_str(&id_str,
1811 got_worktree_get_base_commit_id(worktree));
1812 if (error)
1813 goto done;
1814 arg.repo = repo;
1815 arg.worktree = worktree;
1816 arg.diff_context = diff_context;
1817 arg.id_str = id_str;
1818 arg.header_shown = 0;
1820 error = got_pathlist_append(NULL, &paths, path, NULL);
1821 if (error)
1822 goto done;
1824 error = got_worktree_status(worktree, &paths, repo, print_diff,
1825 &arg, check_cancelled, NULL);
1826 free(id_str);
1827 got_pathlist_free(&paths);
1828 goto done;
1831 error = got_repo_match_object_id_prefix(&id1, id_str1,
1832 GOT_OBJ_TYPE_ANY, repo);
1833 if (error) {
1834 struct got_reference *ref;
1835 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1836 goto done;
1837 error = got_ref_open(&ref, repo, id_str1, 0);
1838 if (error != NULL)
1839 goto done;
1840 label1 = strdup(got_ref_get_name(ref));
1841 if (label1 == NULL) {
1842 error = got_error_from_errno("strdup");
1843 goto done;
1845 error = got_ref_resolve(&id1, repo, ref);
1846 got_ref_close(ref);
1847 if (error != NULL)
1848 goto done;
1849 } else {
1850 error = got_object_id_str(&label1, id1);
1851 if (label1 == NULL) {
1852 error = got_error_from_errno("strdup");
1853 goto done;
1857 error = got_repo_match_object_id_prefix(&id2, id_str2,
1858 GOT_OBJ_TYPE_ANY, repo);
1859 if (error) {
1860 struct got_reference *ref;
1861 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1862 goto done;
1863 error = got_ref_open(&ref, repo, id_str2, 0);
1864 if (error != NULL)
1865 goto done;
1866 label2 = strdup(got_ref_get_name(ref));
1867 if (label2 == NULL) {
1868 error = got_error_from_errno("strdup");
1869 goto done;
1871 error = got_ref_resolve(&id2, repo, ref);
1872 got_ref_close(ref);
1873 if (error != NULL)
1874 goto done;
1875 } else {
1876 error = got_object_id_str(&label2, id2);
1877 if (label2 == NULL) {
1878 error = got_error_from_errno("strdup");
1879 goto done;
1883 error = got_object_get_type(&type1, repo, id1);
1884 if (error)
1885 goto done;
1887 error = got_object_get_type(&type2, repo, id2);
1888 if (error)
1889 goto done;
1891 if (type1 != type2) {
1892 error = got_error(GOT_ERR_OBJ_TYPE);
1893 goto done;
1896 switch (type1) {
1897 case GOT_OBJ_TYPE_BLOB:
1898 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1899 diff_context, repo, stdout);
1900 break;
1901 case GOT_OBJ_TYPE_TREE:
1902 error = got_diff_objects_as_trees(id1, id2, "", "",
1903 diff_context, repo, stdout);
1904 break;
1905 case GOT_OBJ_TYPE_COMMIT:
1906 printf("diff %s %s\n", label1, label2);
1907 error = got_diff_objects_as_commits(id1, id2, diff_context,
1908 repo, stdout);
1909 break;
1910 default:
1911 error = got_error(GOT_ERR_OBJ_TYPE);
1914 done:
1915 free(label1);
1916 free(label2);
1917 free(id1);
1918 free(id2);
1919 free(path);
1920 if (worktree)
1921 got_worktree_close(worktree);
1922 if (repo) {
1923 const struct got_error *repo_error;
1924 repo_error = got_repo_close(repo);
1925 if (error == NULL)
1926 error = repo_error;
1928 return error;
1931 __dead static void
1932 usage_blame(void)
1934 fprintf(stderr,
1935 "usage: %s blame [-c commit] [-r repository-path] path\n",
1936 getprogname());
1937 exit(1);
1940 static const struct got_error *
1941 cmd_blame(int argc, char *argv[])
1943 const struct got_error *error;
1944 struct got_repository *repo = NULL;
1945 struct got_worktree *worktree = NULL;
1946 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1947 struct got_object_id *commit_id = NULL;
1948 char *commit_id_str = NULL;
1949 int ch;
1951 #ifndef PROFILE
1952 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1953 NULL) == -1)
1954 err(1, "pledge");
1955 #endif
1957 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1958 switch (ch) {
1959 case 'c':
1960 commit_id_str = optarg;
1961 break;
1962 case 'r':
1963 repo_path = realpath(optarg, NULL);
1964 if (repo_path == NULL)
1965 err(1, "-r option");
1966 got_path_strip_trailing_slashes(repo_path);
1967 break;
1968 default:
1969 usage_blame();
1970 /* NOTREACHED */
1974 argc -= optind;
1975 argv += optind;
1977 if (argc == 1)
1978 path = argv[0];
1979 else
1980 usage_blame();
1982 cwd = getcwd(NULL, 0);
1983 if (cwd == NULL) {
1984 error = got_error_from_errno("getcwd");
1985 goto done;
1987 if (repo_path == NULL) {
1988 error = got_worktree_open(&worktree, cwd);
1989 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1990 goto done;
1991 else
1992 error = NULL;
1993 if (worktree) {
1994 repo_path =
1995 strdup(got_worktree_get_repo_path(worktree));
1996 if (repo_path == NULL)
1997 error = got_error_from_errno("strdup");
1998 if (error)
1999 goto done;
2000 } else {
2001 repo_path = strdup(cwd);
2002 if (repo_path == NULL) {
2003 error = got_error_from_errno("strdup");
2004 goto done;
2009 error = got_repo_open(&repo, repo_path);
2010 if (error != NULL)
2011 goto done;
2013 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2014 if (error)
2015 goto done;
2017 if (worktree) {
2018 const char *prefix = got_worktree_get_path_prefix(worktree);
2019 char *p, *worktree_subdir = cwd +
2020 strlen(got_worktree_get_root_path(worktree));
2021 if (asprintf(&p, "%s%s%s%s%s",
2022 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2023 worktree_subdir, worktree_subdir[0] ? "/" : "",
2024 path) == -1) {
2025 error = got_error_from_errno("asprintf");
2026 goto done;
2028 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2029 free(p);
2030 } else {
2031 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2033 if (error)
2034 goto done;
2036 if (commit_id_str == NULL) {
2037 struct got_reference *head_ref;
2038 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2039 if (error != NULL)
2040 goto done;
2041 error = got_ref_resolve(&commit_id, repo, head_ref);
2042 got_ref_close(head_ref);
2043 if (error != NULL)
2044 goto done;
2045 } else {
2046 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2047 if (error)
2048 goto done;
2051 error = got_blame(in_repo_path, commit_id, repo, stdout);
2052 done:
2053 free(in_repo_path);
2054 free(repo_path);
2055 free(cwd);
2056 free(commit_id);
2057 if (worktree)
2058 got_worktree_close(worktree);
2059 if (repo) {
2060 const struct got_error *repo_error;
2061 repo_error = got_repo_close(repo);
2062 if (error == NULL)
2063 error = repo_error;
2065 return error;
2068 __dead static void
2069 usage_tree(void)
2071 fprintf(stderr,
2072 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2073 getprogname());
2074 exit(1);
2077 static void
2078 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2079 const char *root_path)
2081 int is_root_path = (strcmp(path, root_path) == 0);
2083 path += strlen(root_path);
2084 while (path[0] == '/')
2085 path++;
2087 printf("%s%s%s%s%s\n", id ? id : "", path,
2088 is_root_path ? "" : "/", te->name,
2089 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2092 static const struct got_error *
2093 print_tree(const char *path, struct got_object_id *commit_id,
2094 int show_ids, int recurse, const char *root_path,
2095 struct got_repository *repo)
2097 const struct got_error *err = NULL;
2098 struct got_object_id *tree_id = NULL;
2099 struct got_tree_object *tree = NULL;
2100 const struct got_tree_entries *entries;
2101 struct got_tree_entry *te;
2103 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2104 if (err)
2105 goto done;
2107 err = got_object_open_as_tree(&tree, repo, tree_id);
2108 if (err)
2109 goto done;
2110 entries = got_object_tree_get_entries(tree);
2111 te = SIMPLEQ_FIRST(&entries->head);
2112 while (te) {
2113 char *id = NULL;
2115 if (sigint_received || sigpipe_received)
2116 break;
2118 if (show_ids) {
2119 char *id_str;
2120 err = got_object_id_str(&id_str, te->id);
2121 if (err)
2122 goto done;
2123 if (asprintf(&id, "%s ", id_str) == -1) {
2124 err = got_error_from_errno("asprintf");
2125 free(id_str);
2126 goto done;
2128 free(id_str);
2130 print_entry(te, id, path, root_path);
2131 free(id);
2133 if (recurse && S_ISDIR(te->mode)) {
2134 char *child_path;
2135 if (asprintf(&child_path, "%s%s%s", path,
2136 path[0] == '/' && path[1] == '\0' ? "" : "/",
2137 te->name) == -1) {
2138 err = got_error_from_errno("asprintf");
2139 goto done;
2141 err = print_tree(child_path, commit_id, show_ids, 1,
2142 root_path, repo);
2143 free(child_path);
2144 if (err)
2145 goto done;
2148 te = SIMPLEQ_NEXT(te, entry);
2150 done:
2151 if (tree)
2152 got_object_tree_close(tree);
2153 free(tree_id);
2154 return err;
2157 static const struct got_error *
2158 cmd_tree(int argc, char *argv[])
2160 const struct got_error *error;
2161 struct got_repository *repo = NULL;
2162 struct got_worktree *worktree = NULL;
2163 const char *path;
2164 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2165 struct got_object_id *commit_id = NULL;
2166 char *commit_id_str = NULL;
2167 int show_ids = 0, recurse = 0;
2168 int ch;
2170 #ifndef PROFILE
2171 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2172 NULL) == -1)
2173 err(1, "pledge");
2174 #endif
2176 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2177 switch (ch) {
2178 case 'c':
2179 commit_id_str = optarg;
2180 break;
2181 case 'r':
2182 repo_path = realpath(optarg, NULL);
2183 if (repo_path == NULL)
2184 err(1, "-r option");
2185 got_path_strip_trailing_slashes(repo_path);
2186 break;
2187 case 'i':
2188 show_ids = 1;
2189 break;
2190 case 'R':
2191 recurse = 1;
2192 break;
2193 default:
2194 usage_tree();
2195 /* NOTREACHED */
2199 argc -= optind;
2200 argv += optind;
2202 if (argc == 1)
2203 path = argv[0];
2204 else if (argc > 1)
2205 usage_tree();
2206 else
2207 path = NULL;
2209 cwd = getcwd(NULL, 0);
2210 if (cwd == NULL) {
2211 error = got_error_from_errno("getcwd");
2212 goto done;
2214 if (repo_path == NULL) {
2215 error = got_worktree_open(&worktree, cwd);
2216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2217 goto done;
2218 else
2219 error = NULL;
2220 if (worktree) {
2221 repo_path =
2222 strdup(got_worktree_get_repo_path(worktree));
2223 if (repo_path == NULL)
2224 error = got_error_from_errno("strdup");
2225 if (error)
2226 goto done;
2227 } else {
2228 repo_path = strdup(cwd);
2229 if (repo_path == NULL) {
2230 error = got_error_from_errno("strdup");
2231 goto done;
2236 error = got_repo_open(&repo, repo_path);
2237 if (error != NULL)
2238 goto done;
2240 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2241 if (error)
2242 goto done;
2244 if (path == NULL) {
2245 if (worktree) {
2246 char *p, *worktree_subdir = cwd +
2247 strlen(got_worktree_get_root_path(worktree));
2248 if (asprintf(&p, "%s/%s",
2249 got_worktree_get_path_prefix(worktree),
2250 worktree_subdir) == -1) {
2251 error = got_error_from_errno("asprintf");
2252 goto done;
2254 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2255 free(p);
2256 if (error)
2257 goto done;
2258 } else
2259 path = "/";
2261 if (in_repo_path == NULL) {
2262 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2263 if (error != NULL)
2264 goto done;
2267 if (commit_id_str == NULL) {
2268 struct got_reference *head_ref;
2269 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2270 if (error != NULL)
2271 goto done;
2272 error = got_ref_resolve(&commit_id, repo, head_ref);
2273 got_ref_close(head_ref);
2274 if (error != NULL)
2275 goto done;
2276 } else {
2277 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2278 if (error)
2279 goto done;
2282 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2283 in_repo_path, repo);
2284 done:
2285 free(in_repo_path);
2286 free(repo_path);
2287 free(cwd);
2288 free(commit_id);
2289 if (worktree)
2290 got_worktree_close(worktree);
2291 if (repo) {
2292 const struct got_error *repo_error;
2293 repo_error = got_repo_close(repo);
2294 if (error == NULL)
2295 error = repo_error;
2297 return error;
2300 __dead static void
2301 usage_status(void)
2303 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2304 exit(1);
2307 static const struct got_error *
2308 print_status(void *arg, unsigned char status, const char *path,
2309 struct got_object_id *blob_id, struct got_object_id *commit_id)
2311 printf("%c %s\n", status, path);
2312 return NULL;
2315 static const struct got_error *
2316 cmd_status(int argc, char *argv[])
2318 const struct got_error *error = NULL;
2319 struct got_repository *repo = NULL;
2320 struct got_worktree *worktree = NULL;
2321 char *cwd = NULL;
2322 struct got_pathlist_head paths;
2323 int ch, i;
2325 TAILQ_INIT(&paths);
2327 while ((ch = getopt(argc, argv, "")) != -1) {
2328 switch (ch) {
2329 default:
2330 usage_status();
2331 /* NOTREACHED */
2335 argc -= optind;
2336 argv += optind;
2338 #ifndef PROFILE
2339 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2340 NULL) == -1)
2341 err(1, "pledge");
2342 #endif
2343 cwd = getcwd(NULL, 0);
2344 if (cwd == NULL) {
2345 error = got_error_from_errno("getcwd");
2346 goto done;
2349 error = got_worktree_open(&worktree, cwd);
2350 if (error != NULL)
2351 goto done;
2353 if (argc == 0) {
2354 error = got_pathlist_append(NULL, &paths, "", NULL);
2355 if (error)
2356 goto done;
2357 } else if (argc >= 1) {
2358 for (i = 0; i < argc; i++) {
2359 char *path;
2360 error = got_worktree_resolve_path(&path, worktree,
2361 argv[i]);
2362 if (error)
2363 goto done;
2364 error = got_pathlist_append(NULL, &paths, path, NULL);
2365 if (error) {
2366 free(path);
2367 goto done;
2370 } else
2371 usage_status();
2373 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2374 if (error != NULL)
2375 goto done;
2377 error = apply_unveil(got_repo_get_path(repo), 1,
2378 got_worktree_get_root_path(worktree));
2379 if (error)
2380 goto done;
2382 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2383 check_cancelled, NULL);
2384 done:
2385 if (argc >= 1) {
2386 struct got_pathlist_entry *pe;
2387 TAILQ_FOREACH(pe, &paths, entry)
2388 free((char *)pe->path);
2390 got_pathlist_free(&paths);
2391 free(cwd);
2392 return error;
2395 __dead static void
2396 usage_ref(void)
2398 fprintf(stderr,
2399 "usage: %s ref [-r repository] -l | -d name | name target\n",
2400 getprogname());
2401 exit(1);
2404 static const struct got_error *
2405 list_refs(struct got_repository *repo)
2407 static const struct got_error *err = NULL;
2408 struct got_reflist_head refs;
2409 struct got_reflist_entry *re;
2411 SIMPLEQ_INIT(&refs);
2412 err = got_ref_list(&refs, repo);
2413 if (err)
2414 return err;
2416 SIMPLEQ_FOREACH(re, &refs, entry) {
2417 char *refstr;
2418 refstr = got_ref_to_str(re->ref);
2419 if (refstr == NULL)
2420 return got_error_from_errno("got_ref_to_str");
2421 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2422 free(refstr);
2425 got_ref_list_free(&refs);
2426 return NULL;
2429 static const struct got_error *
2430 delete_ref(struct got_repository *repo, const char *refname)
2432 const struct got_error *err = NULL;
2433 struct got_reference *ref;
2435 err = got_ref_open(&ref, repo, refname, 0);
2436 if (err)
2437 return err;
2439 err = got_ref_delete(ref, repo);
2440 got_ref_close(ref);
2441 return err;
2444 static const struct got_error *
2445 add_ref(struct got_repository *repo, const char *refname, const char *target)
2447 const struct got_error *err = NULL;
2448 struct got_object_id *id;
2449 struct got_reference *ref = NULL;
2452 * Don't let the user create a reference named '-'.
2453 * While technically a valid reference name, this case is usually
2454 * an unintended typo.
2456 if (refname[0] == '-' && refname[1] == '\0')
2457 return got_error(GOT_ERR_BAD_REF_NAME);
2459 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2460 repo);
2461 if (err) {
2462 struct got_reference *target_ref;
2464 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2465 return err;
2466 err = got_ref_open(&target_ref, repo, target, 0);
2467 if (err)
2468 return err;
2469 err = got_ref_resolve(&id, repo, target_ref);
2470 got_ref_close(target_ref);
2471 if (err)
2472 return err;
2475 err = got_ref_alloc(&ref, refname, id);
2476 if (err)
2477 goto done;
2479 err = got_ref_write(ref, repo);
2480 done:
2481 if (ref)
2482 got_ref_close(ref);
2483 free(id);
2484 return err;
2487 static const struct got_error *
2488 cmd_ref(int argc, char *argv[])
2490 const struct got_error *error = NULL;
2491 struct got_repository *repo = NULL;
2492 struct got_worktree *worktree = NULL;
2493 char *cwd = NULL, *repo_path = NULL;
2494 int ch, do_list = 0;
2495 const char *delref = NULL;
2497 /* TODO: Add -s option for adding symbolic references. */
2498 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2499 switch (ch) {
2500 case 'd':
2501 delref = optarg;
2502 break;
2503 case 'r':
2504 repo_path = realpath(optarg, NULL);
2505 if (repo_path == NULL)
2506 err(1, "-r option");
2507 got_path_strip_trailing_slashes(repo_path);
2508 break;
2509 case 'l':
2510 do_list = 1;
2511 break;
2512 default:
2513 usage_ref();
2514 /* NOTREACHED */
2518 if (do_list && delref)
2519 errx(1, "-l and -d options are mutually exclusive\n");
2521 argc -= optind;
2522 argv += optind;
2524 if (do_list || delref) {
2525 if (argc > 0)
2526 usage_ref();
2527 } else if (argc != 2)
2528 usage_ref();
2530 #ifndef PROFILE
2531 if (do_list) {
2532 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2533 NULL) == -1)
2534 err(1, "pledge");
2535 } else {
2536 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2537 "sendfd unveil", NULL) == -1)
2538 err(1, "pledge");
2540 #endif
2541 cwd = getcwd(NULL, 0);
2542 if (cwd == NULL) {
2543 error = got_error_from_errno("getcwd");
2544 goto done;
2547 if (repo_path == NULL) {
2548 error = got_worktree_open(&worktree, cwd);
2549 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2550 goto done;
2551 else
2552 error = NULL;
2553 if (worktree) {
2554 repo_path =
2555 strdup(got_worktree_get_repo_path(worktree));
2556 if (repo_path == NULL)
2557 error = got_error_from_errno("strdup");
2558 if (error)
2559 goto done;
2560 } else {
2561 repo_path = strdup(cwd);
2562 if (repo_path == NULL) {
2563 error = got_error_from_errno("strdup");
2564 goto done;
2569 error = got_repo_open(&repo, repo_path);
2570 if (error != NULL)
2571 goto done;
2573 error = apply_unveil(got_repo_get_path(repo), do_list,
2574 worktree ? got_worktree_get_root_path(worktree) : NULL);
2575 if (error)
2576 goto done;
2578 if (do_list)
2579 error = list_refs(repo);
2580 else if (delref)
2581 error = delete_ref(repo, delref);
2582 else
2583 error = add_ref(repo, argv[0], argv[1]);
2584 done:
2585 if (repo)
2586 got_repo_close(repo);
2587 if (worktree)
2588 got_worktree_close(worktree);
2589 free(cwd);
2590 free(repo_path);
2591 return error;
2594 __dead static void
2595 usage_branch(void)
2597 fprintf(stderr,
2598 "usage: %s branch [-r repository] -l | -d name | "
2599 "name [base-branch]\n", getprogname());
2600 exit(1);
2603 static const struct got_error *
2604 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2606 static const struct got_error *err = NULL;
2607 struct got_reflist_head refs;
2608 struct got_reflist_entry *re;
2610 SIMPLEQ_INIT(&refs);
2612 err = got_ref_list(&refs, repo);
2613 if (err)
2614 return err;
2616 SIMPLEQ_FOREACH(re, &refs, entry) {
2617 const char *refname, *marker = " ";
2618 char *refstr;
2619 refname = got_ref_get_name(re->ref);
2620 if (strncmp(refname, "refs/heads/", 11) != 0)
2621 continue;
2622 if (worktree && strcmp(refname,
2623 got_worktree_get_head_ref_name(worktree)) == 0) {
2624 struct got_object_id *id = NULL;
2625 err = got_ref_resolve(&id, repo, re->ref);
2626 if (err)
2627 return err;
2628 if (got_object_id_cmp(id,
2629 got_worktree_get_base_commit_id(worktree)) == 0)
2630 marker = "* ";
2631 else
2632 marker = "~ ";
2633 free(id);
2635 refname += 11;
2636 refstr = got_ref_to_str(re->ref);
2637 if (refstr == NULL)
2638 return got_error_from_errno("got_ref_to_str");
2639 printf("%s%s: %s\n", marker, refname, refstr);
2640 free(refstr);
2643 got_ref_list_free(&refs);
2644 return NULL;
2647 static const struct got_error *
2648 delete_branch(struct got_repository *repo, const char *branch_name)
2650 const struct got_error *err = NULL;
2651 struct got_reference *ref;
2652 char *refname;
2654 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2655 return got_error_from_errno("asprintf");
2657 err = got_ref_open(&ref, repo, refname, 0);
2658 if (err)
2659 goto done;
2661 err = got_ref_delete(ref, repo);
2662 got_ref_close(ref);
2663 done:
2664 free(refname);
2665 return err;
2668 static const struct got_error *
2669 add_branch(struct got_repository *repo, const char *branch_name,
2670 const char *base_branch)
2672 const struct got_error *err = NULL;
2673 struct got_object_id *id = NULL;
2674 struct got_reference *ref = NULL;
2675 char *base_refname = NULL, *refname = NULL;
2676 struct got_reference *base_ref;
2679 * Don't let the user create a branch named '-'.
2680 * While technically a valid reference name, this case is usually
2681 * an unintended typo.
2683 if (branch_name[0] == '-' && branch_name[1] == '\0')
2684 return got_error(GOT_ERR_BAD_REF_NAME);
2686 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2687 base_refname = strdup(GOT_REF_HEAD);
2688 if (base_refname == NULL)
2689 return got_error_from_errno("strdup");
2690 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2691 return got_error_from_errno("asprintf");
2693 err = got_ref_open(&base_ref, repo, base_refname, 0);
2694 if (err)
2695 goto done;
2696 err = got_ref_resolve(&id, repo, base_ref);
2697 got_ref_close(base_ref);
2698 if (err)
2699 goto done;
2701 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2702 err = got_error_from_errno("asprintf");
2703 goto done;
2706 err = got_ref_open(&ref, repo, refname, 0);
2707 if (err == NULL) {
2708 err = got_error(GOT_ERR_BRANCH_EXISTS);
2709 goto done;
2710 } else if (err->code != GOT_ERR_NOT_REF)
2711 goto done;
2713 err = got_ref_alloc(&ref, refname, id);
2714 if (err)
2715 goto done;
2717 err = got_ref_write(ref, repo);
2718 done:
2719 if (ref)
2720 got_ref_close(ref);
2721 free(id);
2722 free(base_refname);
2723 free(refname);
2724 return err;
2727 static const struct got_error *
2728 cmd_branch(int argc, char *argv[])
2730 const struct got_error *error = NULL;
2731 struct got_repository *repo = NULL;
2732 struct got_worktree *worktree = NULL;
2733 char *cwd = NULL, *repo_path = NULL;
2734 int ch, do_list = 0;
2735 const char *delref = NULL;
2737 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2738 switch (ch) {
2739 case 'd':
2740 delref = optarg;
2741 break;
2742 case 'r':
2743 repo_path = realpath(optarg, NULL);
2744 if (repo_path == NULL)
2745 err(1, "-r option");
2746 got_path_strip_trailing_slashes(repo_path);
2747 break;
2748 case 'l':
2749 do_list = 1;
2750 break;
2751 default:
2752 usage_branch();
2753 /* NOTREACHED */
2757 if (do_list && delref)
2758 errx(1, "-l and -d options are mutually exclusive\n");
2760 argc -= optind;
2761 argv += optind;
2763 if (do_list || delref) {
2764 if (argc > 0)
2765 usage_branch();
2766 } else if (argc < 1 || argc > 2)
2767 usage_branch();
2769 #ifndef PROFILE
2770 if (do_list) {
2771 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2772 NULL) == -1)
2773 err(1, "pledge");
2774 } else {
2775 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2776 "sendfd unveil", NULL) == -1)
2777 err(1, "pledge");
2779 #endif
2780 cwd = getcwd(NULL, 0);
2781 if (cwd == NULL) {
2782 error = got_error_from_errno("getcwd");
2783 goto done;
2786 if (repo_path == NULL) {
2787 error = got_worktree_open(&worktree, cwd);
2788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2789 goto done;
2790 else
2791 error = NULL;
2792 if (worktree) {
2793 repo_path =
2794 strdup(got_worktree_get_repo_path(worktree));
2795 if (repo_path == NULL)
2796 error = got_error_from_errno("strdup");
2797 if (error)
2798 goto done;
2799 } else {
2800 repo_path = strdup(cwd);
2801 if (repo_path == NULL) {
2802 error = got_error_from_errno("strdup");
2803 goto done;
2808 error = got_repo_open(&repo, repo_path);
2809 if (error != NULL)
2810 goto done;
2812 error = apply_unveil(got_repo_get_path(repo), do_list,
2813 worktree ? got_worktree_get_root_path(worktree) : NULL);
2814 if (error)
2815 goto done;
2817 if (do_list)
2818 error = list_branches(repo, worktree);
2819 else if (delref)
2820 error = delete_branch(repo, delref);
2821 else {
2822 const char *base_branch;
2823 if (argc == 1) {
2824 base_branch = worktree ?
2825 got_worktree_get_head_ref_name(worktree) :
2826 GOT_REF_HEAD;
2827 } else
2828 base_branch = argv[1];
2829 error = add_branch(repo, argv[0], base_branch);
2831 done:
2832 if (repo)
2833 got_repo_close(repo);
2834 if (worktree)
2835 got_worktree_close(worktree);
2836 free(cwd);
2837 free(repo_path);
2838 return error;
2841 __dead static void
2842 usage_add(void)
2844 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2845 exit(1);
2848 static const struct got_error *
2849 cmd_add(int argc, char *argv[])
2851 const struct got_error *error = NULL;
2852 struct got_repository *repo = NULL;
2853 struct got_worktree *worktree = NULL;
2854 char *cwd = NULL;
2855 struct got_pathlist_head paths;
2856 struct got_pathlist_entry *pe;
2857 int ch, x;
2859 TAILQ_INIT(&paths);
2861 while ((ch = getopt(argc, argv, "")) != -1) {
2862 switch (ch) {
2863 default:
2864 usage_add();
2865 /* NOTREACHED */
2869 argc -= optind;
2870 argv += optind;
2872 #ifndef PROFILE
2873 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2874 NULL) == -1)
2875 err(1, "pledge");
2876 #endif
2877 if (argc < 1)
2878 usage_add();
2880 cwd = getcwd(NULL, 0);
2881 if (cwd == NULL) {
2882 error = got_error_from_errno("getcwd");
2883 goto done;
2886 error = got_worktree_open(&worktree, cwd);
2887 if (error)
2888 goto done;
2890 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2891 if (error != NULL)
2892 goto done;
2894 error = apply_unveil(got_repo_get_path(repo), 1,
2895 got_worktree_get_root_path(worktree));
2896 if (error)
2897 goto done;
2899 for (x = 0; x < argc; x++) {
2900 char *path = realpath(argv[x], NULL);
2901 if (path == NULL) {
2902 error = got_error_from_errno2("realpath", argv[x]);
2903 goto done;
2906 got_path_strip_trailing_slashes(path);
2907 error = got_pathlist_insert(&pe, &paths, path, NULL);
2908 if (error) {
2909 free(path);
2910 goto done;
2913 error = got_worktree_schedule_add(worktree, &paths, print_status,
2914 NULL, repo);
2915 done:
2916 if (repo)
2917 got_repo_close(repo);
2918 if (worktree)
2919 got_worktree_close(worktree);
2920 TAILQ_FOREACH(pe, &paths, entry)
2921 free((char *)pe->path);
2922 got_pathlist_free(&paths);
2923 free(cwd);
2924 return error;
2927 __dead static void
2928 usage_remove(void)
2930 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2931 exit(1);
2934 static const struct got_error *
2935 cmd_remove(int argc, char *argv[])
2937 const struct got_error *error = NULL;
2938 struct got_worktree *worktree = NULL;
2939 struct got_repository *repo = NULL;
2940 char *cwd = NULL;
2941 struct got_pathlist_head paths;
2942 struct got_pathlist_entry *pe;
2943 int ch, i, delete_local_mods = 0;
2945 TAILQ_INIT(&paths);
2947 while ((ch = getopt(argc, argv, "f")) != -1) {
2948 switch (ch) {
2949 case 'f':
2950 delete_local_mods = 1;
2951 break;
2952 default:
2953 usage_add();
2954 /* NOTREACHED */
2958 argc -= optind;
2959 argv += optind;
2961 #ifndef PROFILE
2962 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2963 NULL) == -1)
2964 err(1, "pledge");
2965 #endif
2966 if (argc < 1)
2967 usage_remove();
2969 cwd = getcwd(NULL, 0);
2970 if (cwd == NULL) {
2971 error = got_error_from_errno("getcwd");
2972 goto done;
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error)
2976 goto done;
2978 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2979 if (error)
2980 goto done;
2982 error = apply_unveil(got_repo_get_path(repo), 1,
2983 got_worktree_get_root_path(worktree));
2984 if (error)
2985 goto done;
2987 for (i = 0; i < argc; i++) {
2988 char *path = realpath(argv[i], NULL);
2989 if (path == NULL) {
2990 error = got_error_from_errno2("realpath", argv[i]);
2991 goto done;
2994 got_path_strip_trailing_slashes(path);
2995 error = got_pathlist_insert(&pe, &paths, path, NULL);
2996 if (error) {
2997 free(path);
2998 goto done;
3001 error = got_worktree_schedule_delete(worktree, &paths,
3002 delete_local_mods, print_status, NULL, repo);
3003 if (error)
3004 goto done;
3005 done:
3006 if (repo)
3007 got_repo_close(repo);
3008 if (worktree)
3009 got_worktree_close(worktree);
3010 TAILQ_FOREACH(pe, &paths, entry)
3011 free((char *)pe->path);
3012 got_pathlist_free(&paths);
3013 free(cwd);
3014 return error;
3017 __dead static void
3018 usage_revert(void)
3020 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3021 exit(1);
3024 static const struct got_error *
3025 revert_progress(void *arg, unsigned char status, const char *path)
3027 while (path[0] == '/')
3028 path++;
3029 printf("%c %s\n", status, path);
3030 return NULL;
3033 static const struct got_error *
3034 cmd_revert(int argc, char *argv[])
3036 const struct got_error *error = NULL;
3037 struct got_worktree *worktree = NULL;
3038 struct got_repository *repo = NULL;
3039 char *cwd = NULL, *path = NULL;
3040 struct got_pathlist_head paths;
3041 struct got_pathlist_entry *pe;
3042 int ch, i;
3044 TAILQ_INIT(&paths);
3046 while ((ch = getopt(argc, argv, "")) != -1) {
3047 switch (ch) {
3048 default:
3049 usage_revert();
3050 /* NOTREACHED */
3054 argc -= optind;
3055 argv += optind;
3057 #ifndef PROFILE
3058 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3059 "unveil", NULL) == -1)
3060 err(1, "pledge");
3061 #endif
3062 if (argc < 1)
3063 usage_revert();
3065 for (i = 0; i < argc; i++) {
3066 char *path = realpath(argv[i], NULL);
3067 if (path == NULL) {
3068 error = got_error_from_errno2("realpath", argv[i]);
3069 goto done;
3072 got_path_strip_trailing_slashes(path);
3073 error = got_pathlist_insert(&pe, &paths, path, NULL);
3074 if (error) {
3075 free(path);
3076 goto done;
3080 cwd = getcwd(NULL, 0);
3081 if (cwd == NULL) {
3082 error = got_error_from_errno("getcwd");
3083 goto done;
3085 error = got_worktree_open(&worktree, cwd);
3086 if (error)
3087 goto done;
3089 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3090 if (error != NULL)
3091 goto done;
3093 error = apply_unveil(got_repo_get_path(repo), 1,
3094 got_worktree_get_root_path(worktree));
3095 if (error)
3096 goto done;
3098 error = got_worktree_revert(worktree, &paths,
3099 revert_progress, NULL, repo);
3100 if (error)
3101 goto done;
3102 done:
3103 if (repo)
3104 got_repo_close(repo);
3105 if (worktree)
3106 got_worktree_close(worktree);
3107 free(path);
3108 free(cwd);
3109 return error;
3112 __dead static void
3113 usage_commit(void)
3115 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3116 exit(1);
3119 struct collect_commit_logmsg_arg {
3120 const char *cmdline_log;
3121 const char *editor;
3122 const char *worktree_path;
3123 const char *branch_name;
3124 const char *repo_path;
3125 char *logmsg_path;
3129 static const struct got_error *
3130 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3131 void *arg)
3133 char *initial_content = NULL;
3134 struct got_pathlist_entry *pe;
3135 const struct got_error *err = NULL;
3136 char *template = NULL;
3137 struct collect_commit_logmsg_arg *a = arg;
3138 int fd;
3139 size_t len;
3141 /* if a message was specified on the command line, just use it */
3142 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3143 len = strlen(a->cmdline_log) + 1;
3144 *logmsg = malloc(len + 1);
3145 if (*logmsg == NULL)
3146 return got_error_from_errno("malloc");
3147 strlcpy(*logmsg, a->cmdline_log, len);
3148 return NULL;
3151 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3152 return got_error_from_errno("asprintf");
3154 if (asprintf(&initial_content,
3155 "\n# changes to be committed on branch %s:\n",
3156 a->branch_name) == -1)
3157 return got_error_from_errno("asprintf");
3159 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3160 if (err)
3161 goto done;
3163 dprintf(fd, initial_content);
3165 TAILQ_FOREACH(pe, commitable_paths, entry) {
3166 struct got_commitable *ct = pe->data;
3167 dprintf(fd, "# %c %s\n",
3168 got_commitable_get_status(ct),
3169 got_commitable_get_path(ct));
3171 close(fd);
3173 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3174 done:
3175 unlink(a->logmsg_path);
3176 free(a->logmsg_path);
3177 free(initial_content);
3178 free(template);
3180 /* Editor is done; we can now apply unveil(2) */
3181 if (err == NULL) {
3182 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3183 if (err) {
3184 free(*logmsg);
3185 *logmsg = NULL;
3188 return err;
3191 static const struct got_error *
3192 cmd_commit(int argc, char *argv[])
3194 const struct got_error *error = NULL;
3195 struct got_worktree *worktree = NULL;
3196 struct got_repository *repo = NULL;
3197 char *cwd = NULL, *path = NULL, *id_str = NULL;
3198 struct got_object_id *id = NULL;
3199 const char *logmsg = NULL;
3200 const char *got_author = getenv("GOT_AUTHOR");
3201 struct collect_commit_logmsg_arg cl_arg;
3202 char *editor = NULL;
3203 int ch, rebase_in_progress;
3205 while ((ch = getopt(argc, argv, "m:")) != -1) {
3206 switch (ch) {
3207 case 'm':
3208 logmsg = optarg;
3209 break;
3210 default:
3211 usage_commit();
3212 /* NOTREACHED */
3216 argc -= optind;
3217 argv += optind;
3219 #ifndef PROFILE
3220 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3221 "unveil", NULL) == -1)
3222 err(1, "pledge");
3223 #endif
3224 if (argc == 1) {
3225 path = realpath(argv[0], NULL);
3226 if (path == NULL) {
3227 error = got_error_from_errno2("realpath", argv[0]);
3228 goto done;
3230 got_path_strip_trailing_slashes(path);
3231 } else if (argc != 0)
3232 usage_commit();
3234 if (got_author == NULL) {
3235 /* TODO: Look current user up in password database */
3236 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3237 goto done;
3240 cwd = getcwd(NULL, 0);
3241 if (cwd == NULL) {
3242 error = got_error_from_errno("getcwd");
3243 goto done;
3245 error = got_worktree_open(&worktree, cwd);
3246 if (error)
3247 goto done;
3249 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3250 if (error)
3251 goto done;
3252 if (rebase_in_progress) {
3253 error = got_error(GOT_ERR_REBASING);
3254 goto done;
3257 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3258 if (error != NULL)
3259 goto done;
3262 * unveil(2) traverses exec(2); if an editor is used we have
3263 * to apply unveil after the log message has been written.
3265 if (logmsg == NULL || strlen(logmsg) == 0)
3266 error = get_editor(&editor);
3267 else
3268 error = apply_unveil(got_repo_get_path(repo), 0,
3269 got_worktree_get_root_path(worktree));
3270 if (error)
3271 goto done;
3273 cl_arg.editor = editor;
3274 cl_arg.cmdline_log = logmsg;
3275 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3276 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3277 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3278 cl_arg.branch_name += 5;
3279 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3280 cl_arg.branch_name += 6;
3281 cl_arg.repo_path = got_repo_get_path(repo);
3282 cl_arg.logmsg_path = NULL;
3283 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3284 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3285 if (error) {
3286 if (cl_arg.logmsg_path)
3287 fprintf(stderr, "%s: log message preserved in %s\n",
3288 getprogname(), cl_arg.logmsg_path);
3289 goto done;
3292 if (cl_arg.logmsg_path)
3293 unlink(cl_arg.logmsg_path);
3295 error = got_object_id_str(&id_str, id);
3296 if (error)
3297 goto done;
3298 printf("Created commit %s\n", id_str);
3299 done:
3300 if (repo)
3301 got_repo_close(repo);
3302 if (worktree)
3303 got_worktree_close(worktree);
3304 free(path);
3305 free(cwd);
3306 free(id_str);
3307 free(editor);
3308 return error;
3311 __dead static void
3312 usage_cherrypick(void)
3314 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3315 exit(1);
3318 static const struct got_error *
3319 cmd_cherrypick(int argc, char *argv[])
3321 const struct got_error *error = NULL;
3322 struct got_worktree *worktree = NULL;
3323 struct got_repository *repo = NULL;
3324 char *cwd = NULL, *commit_id_str = NULL;
3325 struct got_object_id *commit_id = NULL;
3326 struct got_commit_object *commit = NULL;
3327 struct got_object_qid *pid;
3328 struct got_reference *head_ref = NULL;
3329 int ch, did_something = 0;
3331 while ((ch = getopt(argc, argv, "")) != -1) {
3332 switch (ch) {
3333 default:
3334 usage_cherrypick();
3335 /* NOTREACHED */
3339 argc -= optind;
3340 argv += optind;
3342 #ifndef PROFILE
3343 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3344 "unveil", NULL) == -1)
3345 err(1, "pledge");
3346 #endif
3347 if (argc != 1)
3348 usage_cherrypick();
3350 cwd = getcwd(NULL, 0);
3351 if (cwd == NULL) {
3352 error = got_error_from_errno("getcwd");
3353 goto done;
3355 error = got_worktree_open(&worktree, cwd);
3356 if (error)
3357 goto done;
3359 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3360 if (error != NULL)
3361 goto done;
3363 error = apply_unveil(got_repo_get_path(repo), 0,
3364 got_worktree_get_root_path(worktree));
3365 if (error)
3366 goto done;
3368 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3369 GOT_OBJ_TYPE_COMMIT, repo);
3370 if (error != NULL) {
3371 struct got_reference *ref;
3372 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3373 goto done;
3374 error = got_ref_open(&ref, repo, argv[0], 0);
3375 if (error != NULL)
3376 goto done;
3377 error = got_ref_resolve(&commit_id, repo, ref);
3378 got_ref_close(ref);
3379 if (error != NULL)
3380 goto done;
3382 error = got_object_id_str(&commit_id_str, commit_id);
3383 if (error)
3384 goto done;
3386 error = got_ref_open(&head_ref, repo,
3387 got_worktree_get_head_ref_name(worktree), 0);
3388 if (error != NULL)
3389 goto done;
3391 error = check_same_branch(commit_id, head_ref, repo);
3392 if (error) {
3393 if (error->code != GOT_ERR_ANCESTRY)
3394 goto done;
3395 error = NULL;
3396 } else {
3397 error = got_error(GOT_ERR_SAME_BRANCH);
3398 goto done;
3401 error = got_object_open_as_commit(&commit, repo, commit_id);
3402 if (error)
3403 goto done;
3404 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3405 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3406 commit_id, repo, update_progress, &did_something, check_cancelled,
3407 NULL);
3408 if (error != NULL)
3409 goto done;
3411 if (did_something)
3412 printf("Merged commit %s\n", commit_id_str);
3413 done:
3414 if (commit)
3415 got_object_commit_close(commit);
3416 free(commit_id_str);
3417 if (head_ref)
3418 got_ref_close(head_ref);
3419 if (worktree)
3420 got_worktree_close(worktree);
3421 if (repo)
3422 got_repo_close(repo);
3423 return error;
3426 __dead static void
3427 usage_backout(void)
3429 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3430 exit(1);
3433 static const struct got_error *
3434 cmd_backout(int argc, char *argv[])
3436 const struct got_error *error = NULL;
3437 struct got_worktree *worktree = NULL;
3438 struct got_repository *repo = NULL;
3439 char *cwd = NULL, *commit_id_str = NULL;
3440 struct got_object_id *commit_id = NULL;
3441 struct got_commit_object *commit = NULL;
3442 struct got_object_qid *pid;
3443 struct got_reference *head_ref = NULL;
3444 int ch, did_something = 0;
3446 while ((ch = getopt(argc, argv, "")) != -1) {
3447 switch (ch) {
3448 default:
3449 usage_backout();
3450 /* NOTREACHED */
3454 argc -= optind;
3455 argv += optind;
3457 #ifndef PROFILE
3458 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3459 "unveil", NULL) == -1)
3460 err(1, "pledge");
3461 #endif
3462 if (argc != 1)
3463 usage_backout();
3465 cwd = getcwd(NULL, 0);
3466 if (cwd == NULL) {
3467 error = got_error_from_errno("getcwd");
3468 goto done;
3470 error = got_worktree_open(&worktree, cwd);
3471 if (error)
3472 goto done;
3474 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3475 if (error != NULL)
3476 goto done;
3478 error = apply_unveil(got_repo_get_path(repo), 0,
3479 got_worktree_get_root_path(worktree));
3480 if (error)
3481 goto done;
3483 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3484 GOT_OBJ_TYPE_COMMIT, repo);
3485 if (error != NULL) {
3486 struct got_reference *ref;
3487 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3488 goto done;
3489 error = got_ref_open(&ref, repo, argv[0], 0);
3490 if (error != NULL)
3491 goto done;
3492 error = got_ref_resolve(&commit_id, repo, ref);
3493 got_ref_close(ref);
3494 if (error != NULL)
3495 goto done;
3497 error = got_object_id_str(&commit_id_str, commit_id);
3498 if (error)
3499 goto done;
3501 error = got_ref_open(&head_ref, repo,
3502 got_worktree_get_head_ref_name(worktree), 0);
3503 if (error != NULL)
3504 goto done;
3506 error = check_same_branch(commit_id, head_ref, repo);
3507 if (error)
3508 goto done;
3510 error = got_object_open_as_commit(&commit, repo, commit_id);
3511 if (error)
3512 goto done;
3513 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3514 if (pid == NULL) {
3515 error = got_error(GOT_ERR_ROOT_COMMIT);
3516 goto done;
3519 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3520 update_progress, &did_something, check_cancelled, NULL);
3521 if (error != NULL)
3522 goto done;
3524 if (did_something)
3525 printf("Backed out commit %s\n", commit_id_str);
3526 done:
3527 if (commit)
3528 got_object_commit_close(commit);
3529 free(commit_id_str);
3530 if (head_ref)
3531 got_ref_close(head_ref);
3532 if (worktree)
3533 got_worktree_close(worktree);
3534 if (repo)
3535 got_repo_close(repo);
3536 return error;
3539 __dead static void
3540 usage_rebase(void)
3542 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3543 getprogname());
3544 exit(1);
3547 void
3548 trim_logmsg(char *logmsg, int limit)
3550 char *nl;
3551 size_t len;
3553 len = strlen(logmsg);
3554 if (len > limit)
3555 len = limit;
3556 logmsg[len] = '\0';
3557 nl = strchr(logmsg, '\n');
3558 if (nl)
3559 *nl = '\0';
3562 static const struct got_error *
3563 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3565 const char *logmsg0 = NULL;
3567 logmsg0 = got_object_commit_get_logmsg(commit);
3569 while (isspace((unsigned char)logmsg0[0]))
3570 logmsg0++;
3572 *logmsg = strdup(logmsg0);
3573 if (*logmsg == NULL)
3574 return got_error_from_errno("strdup");
3576 trim_logmsg(*logmsg, limit);
3577 return NULL;
3580 static const struct got_error *
3581 show_rebase_progress(struct got_commit_object *commit,
3582 struct got_object_id *old_id, struct got_object_id *new_id)
3584 const struct got_error *err;
3585 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3587 err = got_object_id_str(&old_id_str, old_id);
3588 if (err)
3589 goto done;
3591 if (new_id) {
3592 err = got_object_id_str(&new_id_str, new_id);
3593 if (err)
3594 goto done;
3597 old_id_str[12] = '\0';
3598 if (new_id_str)
3599 new_id_str[12] = '\0';
3601 err = get_short_logmsg(&logmsg, 42, commit);
3602 if (err)
3603 goto done;
3605 printf("%s -> %s: %s\n", old_id_str,
3606 new_id_str ? new_id_str : "no-op change", logmsg);
3607 done:
3608 free(old_id_str);
3609 free(new_id_str);
3610 return err;
3613 static const struct got_error *
3614 rebase_progress(void *arg, unsigned char status, const char *path)
3616 unsigned char *rebase_status = arg;
3618 while (path[0] == '/')
3619 path++;
3620 printf("%c %s\n", status, path);
3622 if (*rebase_status == GOT_STATUS_CONFLICT)
3623 return NULL;
3624 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3625 *rebase_status = status;
3626 return NULL;
3629 static const struct got_error *
3630 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3631 struct got_reference *branch, struct got_reference *new_base_branch,
3632 struct got_reference *tmp_branch, struct got_repository *repo)
3634 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3635 return got_worktree_rebase_complete(worktree, fileindex,
3636 new_base_branch, tmp_branch, branch, repo);
3639 static const struct got_error *
3640 rebase_commit(struct got_pathlist_head *merged_paths,
3641 struct got_worktree *worktree, struct got_fileindex *fileindex,
3642 struct got_reference *tmp_branch,
3643 struct got_object_id *commit_id, struct got_repository *repo)
3645 const struct got_error *error;
3646 struct got_commit_object *commit;
3647 struct got_object_id *new_commit_id;
3649 error = got_object_open_as_commit(&commit, repo, commit_id);
3650 if (error)
3651 return error;
3653 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3654 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3655 if (error) {
3656 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3657 goto done;
3658 error = show_rebase_progress(commit, commit_id, NULL);
3659 } else {
3660 error = show_rebase_progress(commit, commit_id, new_commit_id);
3661 free(new_commit_id);
3663 done:
3664 got_object_commit_close(commit);
3665 return error;
3668 struct check_path_prefix_arg {
3669 const char *path_prefix;
3670 size_t len;
3671 int errcode;
3674 static const struct got_error *
3675 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3676 struct got_blob_object *blob2, struct got_object_id *id1,
3677 struct got_object_id *id2, const char *path1, const char *path2,
3678 struct got_repository *repo)
3680 struct check_path_prefix_arg *a = arg;
3682 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3683 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3684 return got_error(a->errcode);
3686 return NULL;
3689 static const struct got_error *
3690 check_path_prefix(struct got_object_id *parent_id,
3691 struct got_object_id *commit_id, const char *path_prefix,
3692 int errcode, struct got_repository *repo)
3694 const struct got_error *err;
3695 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3696 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3697 struct check_path_prefix_arg cpp_arg;
3699 if (got_path_is_root_dir(path_prefix))
3700 return NULL;
3702 err = got_object_open_as_commit(&commit, repo, commit_id);
3703 if (err)
3704 goto done;
3706 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3707 if (err)
3708 goto done;
3710 err = got_object_open_as_tree(&tree1, repo,
3711 got_object_commit_get_tree_id(parent_commit));
3712 if (err)
3713 goto done;
3715 err = got_object_open_as_tree(&tree2, repo,
3716 got_object_commit_get_tree_id(commit));
3717 if (err)
3718 goto done;
3720 cpp_arg.path_prefix = path_prefix;
3721 while (cpp_arg.path_prefix[0] == '/')
3722 cpp_arg.path_prefix++;
3723 cpp_arg.len = strlen(cpp_arg.path_prefix);
3724 cpp_arg.errcode = errcode;
3725 err = got_diff_tree(tree1, tree2, "", "", repo,
3726 check_path_prefix_in_diff, &cpp_arg);
3727 done:
3728 if (tree1)
3729 got_object_tree_close(tree1);
3730 if (tree2)
3731 got_object_tree_close(tree2);
3732 if (commit)
3733 got_object_commit_close(commit);
3734 if (parent_commit)
3735 got_object_commit_close(parent_commit);
3736 return err;
3739 static const struct got_error *
3740 collect_commits(struct got_object_id_queue *commits,
3741 struct got_object_id *initial_commit_id,
3742 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3743 const char *path_prefix, int path_prefix_errcode,
3744 struct got_repository *repo)
3746 const struct got_error *err = NULL;
3747 struct got_commit_graph *graph = NULL;
3748 struct got_object_id *parent_id = NULL;
3749 struct got_object_qid *qid;
3750 struct got_object_id *commit_id = initial_commit_id;
3752 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3753 if (err)
3754 return err;
3756 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3757 if (err)
3758 goto done;
3759 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3760 err = got_commit_graph_iter_next(&parent_id, graph);
3761 if (err) {
3762 if (err->code == GOT_ERR_ITER_COMPLETED) {
3763 err = got_error_msg(GOT_ERR_ANCESTRY,
3764 "ran out of commits to rebase before "
3765 "youngest common ancestor commit has "
3766 "been reached?!?");
3767 goto done;
3768 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3769 goto done;
3770 err = got_commit_graph_fetch_commits(graph, 1, repo);
3771 if (err)
3772 goto done;
3773 } else {
3774 err = check_path_prefix(parent_id, commit_id,
3775 path_prefix, path_prefix_errcode, repo);
3776 if (err)
3777 goto done;
3779 err = got_object_qid_alloc(&qid, commit_id);
3780 if (err)
3781 goto done;
3782 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3783 commit_id = parent_id;
3786 done:
3787 got_commit_graph_close(graph);
3788 return err;
3791 static const struct got_error *
3792 cmd_rebase(int argc, char *argv[])
3794 const struct got_error *error = NULL;
3795 struct got_worktree *worktree = NULL;
3796 struct got_repository *repo = NULL;
3797 struct got_fileindex *fileindex = NULL;
3798 char *cwd = NULL;
3799 struct got_reference *branch = NULL;
3800 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3801 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3802 struct got_object_id *resume_commit_id = NULL;
3803 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3804 struct got_commit_object *commit = NULL;
3805 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3806 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3807 struct got_object_id_queue commits;
3808 struct got_pathlist_head merged_paths;
3809 const struct got_object_id_queue *parent_ids;
3810 struct got_object_qid *qid, *pid;
3812 SIMPLEQ_INIT(&commits);
3813 TAILQ_INIT(&merged_paths);
3815 while ((ch = getopt(argc, argv, "ac")) != -1) {
3816 switch (ch) {
3817 case 'a':
3818 abort_rebase = 1;
3819 break;
3820 case 'c':
3821 continue_rebase = 1;
3822 break;
3823 default:
3824 usage_rebase();
3825 /* NOTREACHED */
3829 argc -= optind;
3830 argv += optind;
3832 #ifndef PROFILE
3833 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3834 "unveil", NULL) == -1)
3835 err(1, "pledge");
3836 #endif
3837 if (abort_rebase && continue_rebase)
3838 usage_rebase();
3839 else if (abort_rebase || continue_rebase) {
3840 if (argc != 0)
3841 usage_rebase();
3842 } else if (argc != 1)
3843 usage_rebase();
3845 cwd = getcwd(NULL, 0);
3846 if (cwd == NULL) {
3847 error = got_error_from_errno("getcwd");
3848 goto done;
3850 error = got_worktree_open(&worktree, cwd);
3851 if (error)
3852 goto done;
3854 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3855 if (error != NULL)
3856 goto done;
3858 error = apply_unveil(got_repo_get_path(repo), 0,
3859 got_worktree_get_root_path(worktree));
3860 if (error)
3861 goto done;
3863 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3864 if (error)
3865 goto done;
3867 if (abort_rebase) {
3868 int did_something;
3869 if (!rebase_in_progress) {
3870 error = got_error(GOT_ERR_NOT_REBASING);
3871 goto done;
3873 error = got_worktree_rebase_continue(&resume_commit_id,
3874 &new_base_branch, &tmp_branch, &branch, &fileindex,
3875 worktree, repo);
3876 if (error)
3877 goto done;
3878 printf("Switching work tree to %s\n",
3879 got_ref_get_symref_target(new_base_branch));
3880 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3881 new_base_branch, update_progress, &did_something);
3882 if (error)
3883 goto done;
3884 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3885 goto done; /* nothing else to do */
3888 if (continue_rebase) {
3889 if (!rebase_in_progress) {
3890 error = got_error(GOT_ERR_NOT_REBASING);
3891 goto done;
3893 error = got_worktree_rebase_continue(&resume_commit_id,
3894 &new_base_branch, &tmp_branch, &branch, &fileindex,
3895 worktree, repo);
3896 if (error)
3897 goto done;
3899 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3900 resume_commit_id, repo);
3901 if (error)
3902 goto done;
3904 yca_id = got_object_id_dup(resume_commit_id);
3905 if (yca_id == NULL) {
3906 error = got_error_from_errno("got_object_id_dup");
3907 goto done;
3909 } else {
3910 error = got_ref_open(&branch, repo, argv[0], 0);
3911 if (error != NULL)
3912 goto done;
3914 error = check_same_branch(
3915 got_worktree_get_base_commit_id(worktree), branch, repo);
3916 if (error) {
3917 if (error->code != GOT_ERR_ANCESTRY)
3918 goto done;
3919 error = NULL;
3920 } else {
3921 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3922 "specified branch resolves to a commit which "
3923 "is already contained in work tree's branch");
3924 goto done;
3928 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3929 if (error)
3930 goto done;
3932 if (!continue_rebase) {
3933 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3934 got_worktree_get_base_commit_id(worktree),
3935 branch_head_commit_id, repo);
3936 if (error)
3937 goto done;
3938 if (yca_id == NULL) {
3939 error = got_error_msg(GOT_ERR_ANCESTRY,
3940 "specified branch shares no common ancestry "
3941 "with work tree's branch");
3942 goto done;
3945 error = got_worktree_rebase_prepare(&new_base_branch,
3946 &tmp_branch, &fileindex, worktree, branch, repo);
3947 if (error)
3948 goto done;
3951 commit_id = branch_head_commit_id;
3952 error = got_object_open_as_commit(&commit, repo, commit_id);
3953 if (error)
3954 goto done;
3956 parent_ids = got_object_commit_get_parent_ids(commit);
3957 pid = SIMPLEQ_FIRST(parent_ids);
3958 error = collect_commits(&commits, commit_id, pid->id,
3959 yca_id, got_worktree_get_path_prefix(worktree),
3960 GOT_ERR_REBASE_PATH, repo);
3961 got_object_commit_close(commit);
3962 commit = NULL;
3963 if (error)
3964 goto done;
3966 if (SIMPLEQ_EMPTY(&commits)) {
3967 if (continue_rebase)
3968 error = rebase_complete(worktree, fileindex,
3969 branch, new_base_branch, tmp_branch, repo);
3970 else
3971 error = got_error(GOT_ERR_EMPTY_REBASE);
3972 goto done;
3975 pid = NULL;
3976 SIMPLEQ_FOREACH(qid, &commits, entry) {
3977 commit_id = qid->id;
3978 parent_id = pid ? pid->id : yca_id;
3979 pid = qid;
3981 error = got_worktree_rebase_merge_files(&merged_paths,
3982 worktree, fileindex, parent_id, commit_id, repo,
3983 rebase_progress, &rebase_status, check_cancelled, NULL);
3984 if (error)
3985 goto done;
3987 if (rebase_status == GOT_STATUS_CONFLICT) {
3988 got_worktree_rebase_pathlist_free(&merged_paths);
3989 break;
3992 error = rebase_commit(&merged_paths, worktree, fileindex,
3993 tmp_branch, commit_id, repo);
3994 got_worktree_rebase_pathlist_free(&merged_paths);
3995 if (error)
3996 goto done;
3999 if (rebase_status == GOT_STATUS_CONFLICT) {
4000 error = got_worktree_rebase_postpone(worktree, fileindex);
4001 if (error)
4002 goto done;
4003 error = got_error_msg(GOT_ERR_CONFLICTS,
4004 "conflicts must be resolved before rebasing can continue");
4005 } else
4006 error = rebase_complete(worktree, fileindex, branch,
4007 new_base_branch, tmp_branch, repo);
4008 done:
4009 got_object_id_queue_free(&commits);
4010 free(branch_head_commit_id);
4011 free(resume_commit_id);
4012 free(yca_id);
4013 if (commit)
4014 got_object_commit_close(commit);
4015 if (branch)
4016 got_ref_close(branch);
4017 if (new_base_branch)
4018 got_ref_close(new_base_branch);
4019 if (tmp_branch)
4020 got_ref_close(tmp_branch);
4021 if (worktree)
4022 got_worktree_close(worktree);
4023 if (repo)
4024 got_repo_close(repo);
4025 return error;
4028 __dead static void
4029 usage_histedit(void)
4031 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4032 getprogname());
4033 exit(1);
4036 #define GOT_HISTEDIT_PICK 'p'
4037 #define GOT_HISTEDIT_EDIT 'e'
4038 #define GOT_HISTEDIT_FOLD 'f'
4039 #define GOT_HISTEDIT_DROP 'd'
4040 #define GOT_HISTEDIT_MESG 'm'
4042 static struct got_histedit_cmd {
4043 unsigned char code;
4044 const char *name;
4045 const char *desc;
4046 } got_histedit_cmds[] = {
4047 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4048 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4049 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4050 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4051 { GOT_HISTEDIT_MESG, "mesg",
4052 "single-line log message for commit above (open editor if empty)" },
4055 struct got_histedit_list_entry {
4056 TAILQ_ENTRY(got_histedit_list_entry) entry;
4057 struct got_object_id *commit_id;
4058 const struct got_histedit_cmd *cmd;
4059 char *logmsg;
4061 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4063 static const struct got_error *
4064 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4065 FILE *f, struct got_repository *repo)
4067 const struct got_error *err = NULL;
4068 char *logmsg = NULL, *id_str = NULL;
4069 struct got_commit_object *commit = NULL;
4070 size_t n;
4072 err = got_object_open_as_commit(&commit, repo, commit_id);
4073 if (err)
4074 goto done;
4076 err = get_short_logmsg(&logmsg, 34, commit);
4077 if (err)
4078 goto done;
4080 err = got_object_id_str(&id_str, commit_id);
4081 if (err)
4082 goto done;
4084 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4085 if (n < 0)
4086 err = got_ferror(f, GOT_ERR_IO);
4087 done:
4088 if (commit)
4089 got_object_commit_close(commit);
4090 free(id_str);
4091 free(logmsg);
4092 return err;
4095 static const struct got_error *
4096 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4097 struct got_repository *repo)
4099 const struct got_error *err = NULL;
4100 struct got_object_qid *qid;
4102 if (SIMPLEQ_EMPTY(commits))
4103 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4105 SIMPLEQ_FOREACH(qid, commits, entry) {
4106 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4107 f, repo);
4108 if (err)
4109 break;
4112 return err;
4115 static const struct got_error *
4116 write_cmd_list(FILE *f)
4118 const struct got_error *err = NULL;
4119 int n, i;
4121 n = fprintf(f, "# Available histedit commands:\n");
4122 if (n < 0)
4123 return got_ferror(f, GOT_ERR_IO);
4125 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4126 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4127 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4128 cmd->desc);
4129 if (n < 0) {
4130 err = got_ferror(f, GOT_ERR_IO);
4131 break;
4134 n = fprintf(f, "# Commits will be processed in order from top to "
4135 "bottom of this file.\n");
4136 if (n < 0)
4137 return got_ferror(f, GOT_ERR_IO);
4138 return err;
4141 static const struct got_error *
4142 histedit_syntax_error(int lineno)
4144 static char msg[42];
4145 int ret;
4147 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4148 lineno);
4149 if (ret == -1 || ret >= sizeof(msg))
4150 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4152 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4155 static const struct got_error *
4156 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4157 char *logmsg, struct got_repository *repo)
4159 const struct got_error *err;
4160 struct got_commit_object *folded_commit = NULL;
4161 char *id_str;
4163 err = got_object_id_str(&id_str, hle->commit_id);
4164 if (err)
4165 return err;
4167 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4168 if (err)
4169 goto done;
4171 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4172 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4173 got_object_commit_get_logmsg(folded_commit)) == -1) {
4174 err = got_error_from_errno("asprintf");
4175 goto done;
4177 done:
4178 if (folded_commit)
4179 got_object_commit_close(folded_commit);
4180 free(id_str);
4181 return err;
4184 static struct got_histedit_list_entry *
4185 get_folded_commits(struct got_histedit_list_entry *hle)
4187 struct got_histedit_list_entry *prev, *folded = NULL;
4189 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4190 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4191 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4192 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4193 folded = prev;
4194 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4197 return folded;
4200 static const struct got_error *
4201 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4202 struct got_repository *repo)
4204 char *logmsg_path = NULL, *id_str = NULL;
4205 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4206 const struct got_error *err = NULL;
4207 struct got_commit_object *commit = NULL;
4208 int fd;
4209 struct got_histedit_list_entry *folded = NULL;
4211 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4212 if (err)
4213 return err;
4215 folded = get_folded_commits(hle);
4216 if (folded) {
4217 while (folded != hle) {
4218 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4219 folded = TAILQ_NEXT(folded, entry);
4220 continue;
4222 err = append_folded_commit_msg(&new_msg, folded,
4223 logmsg, repo);
4224 if (err)
4225 goto done;
4226 free(logmsg);
4227 logmsg = new_msg;
4228 folded = TAILQ_NEXT(folded, entry);
4232 err = got_object_id_str(&id_str, hle->commit_id);
4233 if (err)
4234 goto done;
4235 if (asprintf(&new_msg,
4236 "%s\n# original log message of commit %s: %s",
4237 logmsg ? logmsg : "", id_str,
4238 got_object_commit_get_logmsg(commit)) == -1) {
4239 err = got_error_from_errno("asprintf");
4240 goto done;
4242 free(logmsg);
4243 logmsg = new_msg;
4245 err = got_object_id_str(&id_str, hle->commit_id);
4246 if (err)
4247 goto done;
4249 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4250 if (err)
4251 goto done;
4253 dprintf(fd, logmsg);
4254 close(fd);
4256 err = get_editor(&editor);
4257 if (err)
4258 goto done;
4260 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4261 if (err) {
4262 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4263 goto done;
4264 err = NULL;
4265 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4266 if (hle->logmsg == NULL)
4267 err = got_error_from_errno("strdup");
4269 done:
4270 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4271 err = got_error_from_errno2("unlink", logmsg_path);
4272 free(logmsg_path);
4273 free(logmsg);
4274 free(editor);
4275 if (commit)
4276 got_object_commit_close(commit);
4277 return err;
4280 static const struct got_error *
4281 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4282 FILE *f, struct got_repository *repo)
4284 const struct got_error *err = NULL;
4285 char *line = NULL, *p, *end;
4286 size_t size;
4287 ssize_t len;
4288 int lineno = 0, i;
4289 const struct got_histedit_cmd *cmd;
4290 struct got_object_id *commit_id = NULL;
4291 struct got_histedit_list_entry *hle = NULL;
4293 for (;;) {
4294 len = getline(&line, &size, f);
4295 if (len == -1) {
4296 const struct got_error *getline_err;
4297 if (feof(f))
4298 break;
4299 getline_err = got_error_from_errno("getline");
4300 err = got_ferror(f, getline_err->code);
4301 break;
4303 lineno++;
4304 p = line;
4305 while (isspace((unsigned char)p[0]))
4306 p++;
4307 if (p[0] == '#' || p[0] == '\0') {
4308 free(line);
4309 line = NULL;
4310 continue;
4312 cmd = NULL;
4313 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4314 cmd = &got_histedit_cmds[i];
4315 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4316 isspace((unsigned char)p[strlen(cmd->name)])) {
4317 p += strlen(cmd->name);
4318 break;
4320 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4321 p++;
4322 break;
4325 if (i == nitems(got_histedit_cmds)) {
4326 err = histedit_syntax_error(lineno);
4327 break;
4329 while (isspace((unsigned char)p[0]))
4330 p++;
4331 if (cmd->code == GOT_HISTEDIT_MESG) {
4332 if (hle == NULL || hle->logmsg != NULL) {
4333 err = got_error(GOT_ERR_HISTEDIT_CMD);
4334 break;
4336 if (p[0] == '\0') {
4337 err = histedit_edit_logmsg(hle, repo);
4338 if (err)
4339 break;
4340 } else {
4341 hle->logmsg = strdup(p);
4342 if (hle->logmsg == NULL) {
4343 err = got_error_from_errno("strdup");
4344 break;
4347 free(line);
4348 line = NULL;
4349 continue;
4350 } else {
4351 end = p;
4352 while (end[0] && !isspace((unsigned char)end[0]))
4353 end++;
4354 *end = '\0';
4356 err = got_object_resolve_id_str(&commit_id, repo, p);
4357 if (err) {
4358 /* override error code */
4359 err = histedit_syntax_error(lineno);
4360 break;
4363 hle = malloc(sizeof(*hle));
4364 if (hle == NULL) {
4365 err = got_error_from_errno("malloc");
4366 break;
4368 hle->cmd = cmd;
4369 hle->commit_id = commit_id;
4370 hle->logmsg = NULL;
4371 commit_id = NULL;
4372 free(line);
4373 line = NULL;
4374 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4377 free(line);
4378 free(commit_id);
4379 return err;
4382 static const struct got_error *
4383 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4384 const char *path, struct got_repository *repo)
4386 const struct got_error *err = NULL;
4387 char *editor;
4388 FILE *f = NULL;
4390 err = get_editor(&editor);
4391 if (err)
4392 return err;
4394 if (spawn_editor(editor, path) == -1) {
4395 err = got_error_from_errno("failed spawning editor");
4396 goto done;
4399 f = fopen(path, "r");
4400 if (f == NULL) {
4401 err = got_error_from_errno("fopen");
4402 goto done;
4404 err = histedit_parse_list(histedit_cmds, f, repo);
4405 done:
4406 if (f && fclose(f) != 0 && err == NULL)
4407 err = got_error_from_errno("fclose");
4408 free(editor);
4409 return err;
4412 static const struct got_error *
4413 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4414 struct got_object_id_queue *, const char *, struct got_repository *);
4416 static const struct got_error *
4417 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4418 struct got_object_id_queue *commits, struct got_repository *repo)
4420 const struct got_error *err;
4421 FILE *f = NULL;
4422 char *path = NULL;
4424 err = got_opentemp_named(&path, &f, "got-histedit");
4425 if (err)
4426 return err;
4428 err = write_cmd_list(f);
4429 if (err)
4430 goto done;
4432 err = histedit_write_commit_list(commits, f, repo);
4433 if (err)
4434 goto done;
4436 if (fclose(f) != 0) {
4437 err = got_error_from_errno("fclose");
4438 goto done;
4440 f = NULL;
4442 err = histedit_run_editor(histedit_cmds, path, repo);
4443 if (err) {
4444 const char *errmsg = err->msg;
4445 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4446 goto done;
4447 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4448 commits, path, repo);
4450 done:
4451 if (f && fclose(f) != 0 && err == NULL)
4452 err = got_error_from_errno("fclose");
4453 if (path && unlink(path) != 0 && err == NULL)
4454 err = got_error_from_errno2("unlink", path);
4455 free(path);
4456 return err;
4459 static const struct got_error *
4460 histedit_save_list(struct got_histedit_list *histedit_cmds,
4461 struct got_worktree *worktree, struct got_repository *repo)
4463 const struct got_error *err = NULL;
4464 char *path = NULL;
4465 FILE *f = NULL;
4466 struct got_histedit_list_entry *hle;
4467 struct got_commit_object *commit = NULL;
4469 err = got_worktree_get_histedit_list_path(&path, worktree);
4470 if (err)
4471 return err;
4473 f = fopen(path, "w");
4474 if (f == NULL) {
4475 err = got_error_from_errno2("fopen", path);
4476 goto done;
4478 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4479 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4480 repo);
4481 if (err)
4482 break;
4484 if (hle->logmsg) {
4485 int n = fprintf(f, "%c %s\n",
4486 GOT_HISTEDIT_MESG, hle->logmsg);
4487 if (n < 0) {
4488 err = got_ferror(f, GOT_ERR_IO);
4489 break;
4493 done:
4494 if (f && fclose(f) != 0 && err == NULL)
4495 err = got_error_from_errno("fclose");
4496 free(path);
4497 if (commit)
4498 got_object_commit_close(commit);
4499 return err;
4502 static const struct got_error *
4503 histedit_load_list(struct got_histedit_list *histedit_cmds,
4504 const char *path, struct got_repository *repo)
4506 const struct got_error *err = NULL;
4507 FILE *f = NULL;
4509 f = fopen(path, "r");
4510 if (f == NULL) {
4511 err = got_error_from_errno2("fopen", path);
4512 goto done;
4515 err = histedit_parse_list(histedit_cmds, f, repo);
4516 done:
4517 if (f && fclose(f) != 0 && err == NULL)
4518 err = got_error_from_errno("fclose");
4519 return err;
4522 static const struct got_error *
4523 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4524 const char *errmsg, struct got_object_id_queue *commits,
4525 const char *path, struct got_repository *repo)
4527 const struct got_error *err = NULL;
4528 int resp = ' ';
4530 while (resp != 'c' && resp != 'r' && resp != 'a') {
4531 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4532 "or (a)bort: ", getprogname(), errmsg);
4533 resp = getchar();
4534 if (resp == 'c') {
4535 err = histedit_run_editor(histedit_cmds, path, repo);
4536 if (err) {
4537 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4538 break;
4539 resp = ' ';
4540 continue;
4542 } else if (resp == 'r') {
4543 err = histedit_edit_script(histedit_cmds,
4544 commits, repo);
4545 if (err) {
4546 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4547 break;
4548 resp = ' ';
4549 continue;
4551 } else if (resp == 'a') {
4552 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4553 break;
4554 } else
4555 printf("invalid response '%c'\n", resp);
4558 return err;
4561 static const struct got_error *
4562 histedit_complete(struct got_worktree *worktree,
4563 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4564 struct got_reference *branch, struct got_repository *repo)
4566 printf("Switching work tree to %s\n",
4567 got_ref_get_symref_target(branch));
4568 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4569 branch, repo);
4572 static const struct got_error *
4573 show_histedit_progress(struct got_commit_object *commit,
4574 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4576 const struct got_error *err;
4577 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4579 err = got_object_id_str(&old_id_str, hle->commit_id);
4580 if (err)
4581 goto done;
4583 if (new_id) {
4584 err = got_object_id_str(&new_id_str, new_id);
4585 if (err)
4586 goto done;
4589 old_id_str[12] = '\0';
4590 if (new_id_str)
4591 new_id_str[12] = '\0';
4593 if (hle->logmsg) {
4594 logmsg = strdup(hle->logmsg);
4595 if (logmsg == NULL) {
4596 err = got_error_from_errno("strdup");
4597 goto done;
4599 trim_logmsg(logmsg, 42);
4600 } else {
4601 err = get_short_logmsg(&logmsg, 42, commit);
4602 if (err)
4603 goto done;
4606 switch (hle->cmd->code) {
4607 case GOT_HISTEDIT_PICK:
4608 case GOT_HISTEDIT_EDIT:
4609 printf("%s -> %s: %s\n", old_id_str,
4610 new_id_str ? new_id_str : "no-op change", logmsg);
4611 break;
4612 case GOT_HISTEDIT_DROP:
4613 case GOT_HISTEDIT_FOLD:
4614 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4615 logmsg);
4616 break;
4617 default:
4618 break;
4621 done:
4622 free(old_id_str);
4623 free(new_id_str);
4624 return err;
4627 static const struct got_error *
4628 histedit_commit(struct got_pathlist_head *merged_paths,
4629 struct got_worktree *worktree, struct got_fileindex *fileindex,
4630 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4631 struct got_repository *repo)
4633 const struct got_error *err;
4634 struct got_commit_object *commit;
4635 struct got_object_id *new_commit_id;
4637 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4638 && hle->logmsg == NULL) {
4639 err = histedit_edit_logmsg(hle, repo);
4640 if (err)
4641 return err;
4644 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4645 if (err)
4646 return err;
4648 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4649 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4650 hle->logmsg, repo);
4651 if (err) {
4652 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4653 goto done;
4654 err = show_histedit_progress(commit, hle, NULL);
4655 } else {
4656 err = show_histedit_progress(commit, hle, new_commit_id);
4657 free(new_commit_id);
4659 done:
4660 got_object_commit_close(commit);
4661 return err;
4664 static const struct got_error *
4665 histedit_skip_commit(struct got_histedit_list_entry *hle,
4666 struct got_worktree *worktree, struct got_repository *repo)
4668 const struct got_error *error;
4669 struct got_commit_object *commit;
4671 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4672 repo);
4673 if (error)
4674 return error;
4676 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4677 if (error)
4678 return error;
4680 error = show_histedit_progress(commit, hle, NULL);
4681 got_object_commit_close(commit);
4682 return error;
4685 static const struct got_error *
4686 histedit_check_script(struct got_histedit_list *histedit_cmds,
4687 struct got_object_id_queue *commits, struct got_repository *repo)
4689 const struct got_error *err = NULL;
4690 struct got_object_qid *qid;
4691 struct got_histedit_list_entry *hle;
4692 static char msg[80];
4693 char *id_str;
4695 if (TAILQ_EMPTY(histedit_cmds))
4696 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4697 "histedit script contains no commands");
4699 SIMPLEQ_FOREACH(qid, commits, entry) {
4700 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4701 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4702 break;
4704 if (hle == NULL) {
4705 err = got_object_id_str(&id_str, qid->id);
4706 if (err)
4707 return err;
4708 snprintf(msg, sizeof(msg),
4709 "commit %s missing from histedit script", id_str);
4710 free(id_str);
4711 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4715 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4716 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4717 "last commit in histedit script cannot be folded");
4719 return NULL;
4722 static const struct got_error *
4723 cmd_histedit(int argc, char *argv[])
4725 const struct got_error *error = NULL;
4726 struct got_worktree *worktree = NULL;
4727 struct got_fileindex *fileindex = NULL;
4728 struct got_repository *repo = NULL;
4729 char *cwd = NULL;
4730 struct got_reference *branch = NULL;
4731 struct got_reference *tmp_branch = NULL;
4732 struct got_object_id *resume_commit_id = NULL;
4733 struct got_object_id *base_commit_id = NULL;
4734 struct got_object_id *head_commit_id = NULL;
4735 struct got_commit_object *commit = NULL;
4736 int ch, rebase_in_progress = 0;
4737 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4738 const char *edit_script_path = NULL;
4739 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4740 struct got_object_id_queue commits;
4741 struct got_pathlist_head merged_paths;
4742 const struct got_object_id_queue *parent_ids;
4743 struct got_object_qid *pid;
4744 struct got_histedit_list histedit_cmds;
4745 struct got_histedit_list_entry *hle;
4747 SIMPLEQ_INIT(&commits);
4748 TAILQ_INIT(&histedit_cmds);
4749 TAILQ_INIT(&merged_paths);
4751 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4752 switch (ch) {
4753 case 'a':
4754 abort_edit = 1;
4755 break;
4756 case 'c':
4757 continue_edit = 1;
4758 break;
4759 case 'F':
4760 edit_script_path = optarg;
4761 break;
4762 default:
4763 usage_histedit();
4764 /* NOTREACHED */
4768 argc -= optind;
4769 argv += optind;
4771 #ifndef PROFILE
4772 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4773 "unveil", NULL) == -1)
4774 err(1, "pledge");
4775 #endif
4776 if (abort_edit && continue_edit)
4777 usage_histedit();
4778 if (argc != 0)
4779 usage_histedit();
4782 * This command cannot apply unveil(2) in all cases because the
4783 * user may choose to run an editor to edit the histedit script
4784 * and to edit individual commit log messages.
4785 * unveil(2) traverses exec(2); if an editor is used we have to
4786 * apply unveil after edit script and log messages have been written.
4787 * XXX TODO: Make use of unveil(2) where possible.
4790 cwd = getcwd(NULL, 0);
4791 if (cwd == NULL) {
4792 error = got_error_from_errno("getcwd");
4793 goto done;
4795 error = got_worktree_open(&worktree, cwd);
4796 if (error)
4797 goto done;
4799 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4800 if (error != NULL)
4801 goto done;
4803 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4804 if (error)
4805 goto done;
4806 if (rebase_in_progress) {
4807 error = got_error(GOT_ERR_REBASING);
4808 goto done;
4811 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4812 if (error)
4813 goto done;
4815 if (edit_in_progress && abort_edit) {
4816 int did_something;
4817 error = got_worktree_histedit_continue(&resume_commit_id,
4818 &tmp_branch, &branch, &base_commit_id, &fileindex,
4819 worktree, repo);
4820 if (error)
4821 goto done;
4822 printf("Switching work tree to %s\n",
4823 got_ref_get_symref_target(branch));
4824 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4825 branch, base_commit_id, update_progress, &did_something);
4826 if (error)
4827 goto done;
4828 printf("Histedit of %s aborted\n",
4829 got_ref_get_symref_target(branch));
4830 goto done; /* nothing else to do */
4831 } else if (abort_edit) {
4832 error = got_error(GOT_ERR_NOT_HISTEDIT);
4833 goto done;
4836 if (continue_edit) {
4837 char *path;
4839 if (!edit_in_progress) {
4840 error = got_error(GOT_ERR_NOT_HISTEDIT);
4841 goto done;
4844 error = got_worktree_get_histedit_list_path(&path, worktree);
4845 if (error)
4846 goto done;
4848 error = histedit_load_list(&histedit_cmds, path, repo);
4849 free(path);
4850 if (error)
4851 goto done;
4853 error = got_worktree_histedit_continue(&resume_commit_id,
4854 &tmp_branch, &branch, &base_commit_id, &fileindex,
4855 worktree, repo);
4856 if (error)
4857 goto done;
4859 error = got_ref_resolve(&head_commit_id, repo, branch);
4860 if (error)
4861 goto done;
4863 error = got_object_open_as_commit(&commit, repo,
4864 head_commit_id);
4865 if (error)
4866 goto done;
4867 parent_ids = got_object_commit_get_parent_ids(commit);
4868 pid = SIMPLEQ_FIRST(parent_ids);
4869 if (pid == NULL) {
4870 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4871 goto done;
4873 error = collect_commits(&commits, head_commit_id, pid->id,
4874 base_commit_id, got_worktree_get_path_prefix(worktree),
4875 GOT_ERR_HISTEDIT_PATH, repo);
4876 got_object_commit_close(commit);
4877 commit = NULL;
4878 if (error)
4879 goto done;
4880 } else {
4881 if (edit_in_progress) {
4882 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4883 goto done;
4886 error = got_ref_open(&branch, repo,
4887 got_worktree_get_head_ref_name(worktree), 0);
4888 if (error != NULL)
4889 goto done;
4891 error = got_ref_resolve(&head_commit_id, repo, branch);
4892 if (error)
4893 goto done;
4895 error = got_object_open_as_commit(&commit, repo,
4896 head_commit_id);
4897 if (error)
4898 goto done;
4899 parent_ids = got_object_commit_get_parent_ids(commit);
4900 pid = SIMPLEQ_FIRST(parent_ids);
4901 if (pid == NULL) {
4902 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4903 goto done;
4905 error = collect_commits(&commits, head_commit_id, pid->id,
4906 got_worktree_get_base_commit_id(worktree),
4907 got_worktree_get_path_prefix(worktree),
4908 GOT_ERR_HISTEDIT_PATH, repo);
4909 got_object_commit_close(commit);
4910 commit = NULL;
4911 if (error)
4912 goto done;
4914 if (edit_script_path) {
4915 error = histedit_load_list(&histedit_cmds,
4916 edit_script_path, repo);
4917 if (error)
4918 goto done;
4919 } else {
4920 error = histedit_edit_script(&histedit_cmds, &commits,
4921 repo);
4922 if (error)
4923 goto done;
4927 error = histedit_save_list(&histedit_cmds, worktree,
4928 repo);
4929 if (error)
4930 goto done;
4932 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4933 &base_commit_id, &fileindex, worktree, repo);
4934 if (error)
4935 goto done;
4939 error = histedit_check_script(&histedit_cmds, &commits, repo);
4940 if (error)
4941 goto done;
4943 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4944 if (resume_commit_id) {
4945 if (got_object_id_cmp(hle->commit_id,
4946 resume_commit_id) != 0)
4947 continue;
4949 resume_commit_id = NULL;
4950 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4951 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4952 error = histedit_skip_commit(hle, worktree,
4953 repo);
4954 } else {
4955 error = histedit_commit(NULL, worktree,
4956 fileindex, tmp_branch, hle, repo);
4958 if (error)
4959 goto done;
4960 continue;
4963 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4964 error = histedit_skip_commit(hle, worktree, repo);
4965 if (error)
4966 goto done;
4967 continue;
4970 error = got_object_open_as_commit(&commit, repo,
4971 hle->commit_id);
4972 if (error)
4973 goto done;
4974 parent_ids = got_object_commit_get_parent_ids(commit);
4975 pid = SIMPLEQ_FIRST(parent_ids);
4977 error = got_worktree_histedit_merge_files(&merged_paths,
4978 worktree, fileindex, pid->id, hle->commit_id, repo,
4979 rebase_progress, &rebase_status, check_cancelled, NULL);
4980 if (error)
4981 goto done;
4982 got_object_commit_close(commit);
4983 commit = NULL;
4985 if (rebase_status == GOT_STATUS_CONFLICT) {
4986 got_worktree_rebase_pathlist_free(&merged_paths);
4987 break;
4990 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4991 char *id_str;
4992 error = got_object_id_str(&id_str, hle->commit_id);
4993 if (error)
4994 goto done;
4995 printf("Stopping histedit for amending commit %s\n",
4996 id_str);
4997 free(id_str);
4998 got_worktree_rebase_pathlist_free(&merged_paths);
4999 error = got_worktree_histedit_postpone(worktree,
5000 fileindex);
5001 goto done;
5004 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5005 error = histedit_skip_commit(hle, worktree, repo);
5006 if (error)
5007 goto done;
5008 continue;
5011 error = histedit_commit(&merged_paths, worktree, fileindex,
5012 tmp_branch, hle, repo);
5013 got_worktree_rebase_pathlist_free(&merged_paths);
5014 if (error)
5015 goto done;
5018 if (rebase_status == GOT_STATUS_CONFLICT) {
5019 error = got_worktree_histedit_postpone(worktree, fileindex);
5020 if (error)
5021 goto done;
5022 error = got_error_msg(GOT_ERR_CONFLICTS,
5023 "conflicts must be resolved before rebasing can continue");
5024 } else
5025 error = histedit_complete(worktree, fileindex, tmp_branch,
5026 branch, repo);
5027 done:
5028 got_object_id_queue_free(&commits);
5029 free(head_commit_id);
5030 free(base_commit_id);
5031 free(resume_commit_id);
5032 if (commit)
5033 got_object_commit_close(commit);
5034 if (branch)
5035 got_ref_close(branch);
5036 if (tmp_branch)
5037 got_ref_close(tmp_branch);
5038 if (worktree)
5039 got_worktree_close(worktree);
5040 if (repo)
5041 got_repo_close(repo);
5042 return error;