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 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 cmd_checkout(int argc, char *argv[])
764 const struct got_error *error = NULL;
765 struct got_repository *repo = NULL;
766 struct got_reference *head_ref = NULL;
767 struct got_worktree *worktree = NULL;
768 char *repo_path = NULL;
769 char *worktree_path = NULL;
770 const char *path_prefix = "";
771 const char *branch_name = GOT_REF_HEAD;
772 char *commit_id_str = NULL;
773 int ch, same_path_prefix;
775 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
776 switch (ch) {
777 case 'b':
778 branch_name = optarg;
779 break;
780 case 'c':
781 commit_id_str = strdup(optarg);
782 if (commit_id_str == NULL)
783 return got_error_from_errno("strdup");
784 break;
785 case 'p':
786 path_prefix = optarg;
787 break;
788 default:
789 usage_checkout();
790 /* NOTREACHED */
794 argc -= optind;
795 argv += optind;
797 #ifndef PROFILE
798 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
799 "unveil", NULL) == -1)
800 err(1, "pledge");
801 #endif
802 if (argc == 1) {
803 char *cwd, *base, *dotgit;
804 repo_path = realpath(argv[0], NULL);
805 if (repo_path == NULL)
806 return got_error_from_errno2("realpath", argv[0]);
807 cwd = getcwd(NULL, 0);
808 if (cwd == NULL) {
809 error = got_error_from_errno("getcwd");
810 goto done;
812 if (path_prefix[0]) {
813 base = basename(path_prefix);
814 if (base == NULL) {
815 error = got_error_from_errno2("basename",
816 path_prefix);
817 goto done;
819 } else {
820 base = basename(repo_path);
821 if (base == NULL) {
822 error = got_error_from_errno2("basename",
823 repo_path);
824 goto done;
827 dotgit = strstr(base, ".git");
828 if (dotgit)
829 *dotgit = '\0';
830 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
831 error = got_error_from_errno("asprintf");
832 free(cwd);
833 goto done;
835 free(cwd);
836 } else if (argc == 2) {
837 repo_path = realpath(argv[0], NULL);
838 if (repo_path == NULL) {
839 error = got_error_from_errno2("realpath", argv[0]);
840 goto done;
842 worktree_path = realpath(argv[1], NULL);
843 if (worktree_path == NULL) {
844 if (errno != ENOENT) {
845 error = got_error_from_errno2("realpath",
846 argv[1]);
847 goto done;
849 worktree_path = strdup(argv[1]);
850 if (worktree_path == NULL) {
851 error = got_error_from_errno("strdup");
852 goto done;
855 } else
856 usage_checkout();
858 got_path_strip_trailing_slashes(repo_path);
859 got_path_strip_trailing_slashes(worktree_path);
861 error = got_repo_open(&repo, repo_path);
862 if (error != NULL)
863 goto done;
865 /* Pre-create work tree path for unveil(2) */
866 error = got_path_mkdir(worktree_path);
867 if (error) {
868 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
869 goto done;
870 if (!got_path_dir_is_empty(worktree_path)) {
871 error = got_error_path(worktree_path,
872 GOT_ERR_DIR_NOT_EMPTY);
873 goto done;
877 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
878 if (error)
879 goto done;
881 error = got_ref_open(&head_ref, repo, branch_name, 0);
882 if (error != NULL)
883 goto done;
885 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
886 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
887 goto done;
889 error = got_worktree_open(&worktree, worktree_path);
890 if (error != NULL)
891 goto done;
893 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
894 path_prefix);
895 if (error != NULL)
896 goto done;
897 if (!same_path_prefix) {
898 error = got_error(GOT_ERR_PATH_PREFIX);
899 goto done;
902 if (commit_id_str) {
903 struct got_object_id *commit_id;
904 error = got_repo_match_object_id_prefix(&commit_id,
905 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
906 if (error != NULL)
907 goto done;
908 error = check_linear_ancestry(commit_id,
909 got_worktree_get_base_commit_id(worktree), repo);
910 if (error != NULL) {
911 free(commit_id);
912 goto done;
914 error = check_same_branch(commit_id, head_ref, repo);
915 if (error)
916 goto done;
917 error = got_worktree_set_base_commit_id(worktree, repo,
918 commit_id);
919 free(commit_id);
920 if (error)
921 goto done;
924 error = got_worktree_checkout_files(worktree, "", repo,
925 checkout_progress, worktree_path, check_cancelled, NULL);
926 if (error != NULL)
927 goto done;
929 printf("Now shut up and hack\n");
931 done:
932 free(commit_id_str);
933 free(repo_path);
934 free(worktree_path);
935 return error;
938 __dead static void
939 usage_update(void)
941 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
942 getprogname());
943 exit(1);
946 static const struct got_error *
947 update_progress(void *arg, unsigned char status, const char *path)
949 int *did_something = arg;
951 if (status == GOT_STATUS_EXISTS)
952 return NULL;
954 *did_something = 1;
956 /* Base commit bump happens silently. */
957 if (status == GOT_STATUS_BUMP_BASE)
958 return NULL;
960 while (path[0] == '/')
961 path++;
962 printf("%c %s\n", status, path);
963 return NULL;
966 static const struct got_error *
967 switch_head_ref(struct got_reference *head_ref,
968 struct got_object_id *commit_id, struct got_worktree *worktree,
969 struct got_repository *repo)
971 const struct got_error *err = NULL;
972 char *base_id_str;
973 int ref_has_moved = 0;
975 /* Trivial case: switching between two different references. */
976 if (strcmp(got_ref_get_name(head_ref),
977 got_worktree_get_head_ref_name(worktree)) != 0) {
978 printf("Switching work tree from %s to %s\n",
979 got_worktree_get_head_ref_name(worktree),
980 got_ref_get_name(head_ref));
981 return got_worktree_set_head_ref(worktree, head_ref);
984 err = check_linear_ancestry(commit_id,
985 got_worktree_get_base_commit_id(worktree), repo);
986 if (err) {
987 if (err->code != GOT_ERR_ANCESTRY)
988 return err;
989 ref_has_moved = 1;
991 if (!ref_has_moved)
992 return NULL;
994 /* Switching to a rebased branch with the same reference name. */
995 err = got_object_id_str(&base_id_str,
996 got_worktree_get_base_commit_id(worktree));
997 if (err)
998 return err;
999 printf("Reference %s now points at a different branch\n",
1000 got_worktree_get_head_ref_name(worktree));
1001 printf("Switching work tree from %s to %s\n", base_id_str,
1002 got_worktree_get_head_ref_name(worktree));
1003 return NULL;
1006 static const struct got_error *
1007 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1009 const struct got_error *err;
1010 int in_progress;
1012 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1013 if (err)
1014 return err;
1015 if (in_progress)
1016 return got_error(GOT_ERR_REBASING);
1018 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1019 if (err)
1020 return err;
1021 if (in_progress)
1022 return got_error(GOT_ERR_HISTEDIT_BUSY);
1024 return NULL;
1027 static const struct got_error *
1028 cmd_update(int argc, char *argv[])
1030 const struct got_error *error = NULL;
1031 struct got_repository *repo = NULL;
1032 struct got_worktree *worktree = NULL;
1033 char *worktree_path = NULL, *path = NULL;
1034 struct got_object_id *commit_id = NULL;
1035 char *commit_id_str = NULL;
1036 const char *branch_name = NULL;
1037 struct got_reference *head_ref = NULL;
1038 int ch, did_something = 0;
1040 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1041 switch (ch) {
1042 case 'b':
1043 branch_name = optarg;
1044 break;
1045 case 'c':
1046 commit_id_str = strdup(optarg);
1047 if (commit_id_str == NULL)
1048 return got_error_from_errno("strdup");
1049 break;
1050 default:
1051 usage_update();
1052 /* NOTREACHED */
1056 argc -= optind;
1057 argv += optind;
1059 #ifndef PROFILE
1060 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1061 "unveil", NULL) == -1)
1062 err(1, "pledge");
1063 #endif
1064 worktree_path = getcwd(NULL, 0);
1065 if (worktree_path == NULL) {
1066 error = got_error_from_errno("getcwd");
1067 goto done;
1069 error = got_worktree_open(&worktree, worktree_path);
1070 if (error)
1071 goto done;
1073 error = check_rebase_or_histedit_in_progress(worktree);
1074 if (error)
1075 goto done;
1077 if (argc == 0) {
1078 path = strdup("");
1079 if (path == NULL) {
1080 error = got_error_from_errno("strdup");
1081 goto done;
1083 } else if (argc == 1) {
1084 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1085 if (error)
1086 goto done;
1087 } else
1088 usage_update();
1090 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1091 if (error != NULL)
1092 goto done;
1094 error = apply_unveil(got_repo_get_path(repo), 0,
1095 got_worktree_get_root_path(worktree));
1096 if (error)
1097 goto done;
1099 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1100 got_worktree_get_head_ref_name(worktree), 0);
1101 if (error != NULL)
1102 goto done;
1103 if (commit_id_str == NULL) {
1104 error = got_ref_resolve(&commit_id, repo, head_ref);
1105 if (error != NULL)
1106 goto done;
1107 error = got_object_id_str(&commit_id_str, commit_id);
1108 if (error != NULL)
1109 goto done;
1110 } else {
1111 error = got_repo_match_object_id_prefix(&commit_id,
1112 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1113 if (error != NULL)
1114 goto done;
1115 free(commit_id_str);
1116 error = got_object_id_str(&commit_id_str, commit_id);
1117 if (error)
1118 goto done;
1121 if (branch_name) {
1122 struct got_object_id *head_commit_id;
1123 if (strlen(path) != 0) {
1124 fprintf(stderr, "%s: switching between branches "
1125 "requires that the entire work tree "
1126 "gets updated, not just '%s'\n",
1127 getprogname(), path);
1128 error = got_error(GOT_ERR_BAD_PATH);
1129 goto done;
1131 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1132 if (error)
1133 goto done;
1134 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1135 free(head_commit_id);
1136 if (error != NULL)
1137 goto done;
1138 error = check_same_branch(commit_id, head_ref, repo);
1139 if (error)
1140 goto done;
1141 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1142 if (error)
1143 goto done;
1144 } else {
1145 error = check_linear_ancestry(commit_id,
1146 got_worktree_get_base_commit_id(worktree), repo);
1147 if (error != NULL) {
1148 if (error->code == GOT_ERR_ANCESTRY)
1149 error = got_error(GOT_ERR_BRANCH_MOVED);
1150 goto done;
1152 error = check_same_branch(commit_id, head_ref, repo);
1153 if (error)
1154 goto done;
1157 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1158 commit_id) != 0) {
1159 error = got_worktree_set_base_commit_id(worktree, repo,
1160 commit_id);
1161 if (error)
1162 goto done;
1165 error = got_worktree_checkout_files(worktree, path, repo,
1166 update_progress, &did_something, check_cancelled, NULL);
1167 if (error != NULL)
1168 goto done;
1170 if (did_something)
1171 printf("Updated to commit %s\n", commit_id_str);
1172 else
1173 printf("Already up-to-date\n");
1174 done:
1175 free(worktree_path);
1176 free(path);
1177 free(commit_id);
1178 free(commit_id_str);
1179 return error;
1182 static const struct got_error *
1183 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1184 int diff_context, struct got_repository *repo)
1186 const struct got_error *err = NULL;
1187 struct got_tree_object *tree1 = NULL, *tree2;
1188 struct got_object_qid *qid;
1189 char *id_str1 = NULL, *id_str2;
1190 struct got_diff_blob_output_unidiff_arg arg;
1192 err = got_object_open_as_tree(&tree2, repo,
1193 got_object_commit_get_tree_id(commit));
1194 if (err)
1195 return err;
1197 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1198 if (qid != NULL) {
1199 struct got_commit_object *pcommit;
1201 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1202 if (err)
1203 return err;
1205 err = got_object_open_as_tree(&tree1, repo,
1206 got_object_commit_get_tree_id(pcommit));
1207 got_object_commit_close(pcommit);
1208 if (err)
1209 return err;
1211 err = got_object_id_str(&id_str1, qid->id);
1212 if (err)
1213 return err;
1216 err = got_object_id_str(&id_str2, id);
1217 if (err)
1218 goto done;
1220 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1221 arg.diff_context = diff_context;
1222 arg.outfile = stdout;
1223 err = got_diff_tree(tree1, tree2, "", "", repo,
1224 got_diff_blob_output_unidiff, &arg);
1225 done:
1226 if (tree1)
1227 got_object_tree_close(tree1);
1228 got_object_tree_close(tree2);
1229 free(id_str1);
1230 free(id_str2);
1231 return err;
1234 static char *
1235 get_datestr(time_t *time, char *datebuf)
1237 char *p, *s = ctime_r(time, datebuf);
1238 p = strchr(s, '\n');
1239 if (p)
1240 *p = '\0';
1241 return s;
1244 static const struct got_error *
1245 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1246 struct got_repository *repo, int show_patch, int diff_context,
1247 struct got_reflist_head *refs)
1249 const struct got_error *err = NULL;
1250 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1251 char datebuf[26];
1252 time_t committer_time;
1253 const char *author, *committer;
1254 char *refs_str = NULL;
1255 struct got_reflist_entry *re;
1257 SIMPLEQ_FOREACH(re, refs, entry) {
1258 char *s;
1259 const char *name;
1260 if (got_object_id_cmp(re->id, id) != 0)
1261 continue;
1262 name = got_ref_get_name(re->ref);
1263 if (strcmp(name, GOT_REF_HEAD) == 0)
1264 continue;
1265 if (strncmp(name, "refs/", 5) == 0)
1266 name += 5;
1267 if (strncmp(name, "got/", 4) == 0)
1268 continue;
1269 if (strncmp(name, "heads/", 6) == 0)
1270 name += 6;
1271 if (strncmp(name, "remotes/", 8) == 0)
1272 name += 8;
1273 s = refs_str;
1274 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1275 name) == -1) {
1276 err = got_error_from_errno("asprintf");
1277 free(s);
1278 break;
1280 free(s);
1282 err = got_object_id_str(&id_str, id);
1283 if (err)
1284 return err;
1286 printf("-----------------------------------------------\n");
1287 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1288 refs_str ? refs_str : "", refs_str ? ")" : "");
1289 free(id_str);
1290 id_str = NULL;
1291 free(refs_str);
1292 refs_str = NULL;
1293 printf("from: %s\n", got_object_commit_get_author(commit));
1294 committer_time = got_object_commit_get_committer_time(commit);
1295 datestr = get_datestr(&committer_time, datebuf);
1296 printf("date: %s UTC\n", datestr);
1297 author = got_object_commit_get_author(commit);
1298 committer = got_object_commit_get_committer(commit);
1299 if (strcmp(author, committer) != 0)
1300 printf("via: %s\n", committer);
1301 if (got_object_commit_get_nparents(commit) > 1) {
1302 const struct got_object_id_queue *parent_ids;
1303 struct got_object_qid *qid;
1304 int n = 1;
1305 parent_ids = got_object_commit_get_parent_ids(commit);
1306 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1307 err = got_object_id_str(&id_str, qid->id);
1308 if (err)
1309 return err;
1310 printf("parent %d: %s\n", n++, id_str);
1311 free(id_str);
1315 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1316 if (logmsg0 == NULL)
1317 return got_error_from_errno("strdup");
1319 logmsg = logmsg0;
1320 do {
1321 line = strsep(&logmsg, "\n");
1322 if (line)
1323 printf(" %s\n", line);
1324 } while (line);
1325 free(logmsg0);
1327 if (show_patch) {
1328 err = print_patch(commit, id, diff_context, repo);
1329 if (err == 0)
1330 printf("\n");
1333 if (fflush(stdout) != 0 && err == NULL)
1334 err = got_error_from_errno("fflush");
1335 return err;
1338 static const struct got_error *
1339 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1340 char *path, int show_patch, int diff_context, int limit,
1341 int first_parent_traversal, struct got_reflist_head *refs)
1343 const struct got_error *err;
1344 struct got_commit_graph *graph;
1346 err = got_commit_graph_open(&graph, root_id, path,
1347 first_parent_traversal, repo);
1348 if (err)
1349 return err;
1350 err = got_commit_graph_iter_start(graph, root_id, repo);
1351 if (err)
1352 goto done;
1353 for (;;) {
1354 struct got_commit_object *commit;
1355 struct got_object_id *id;
1357 if (sigint_received || sigpipe_received)
1358 break;
1360 err = got_commit_graph_iter_next(&id, graph);
1361 if (err) {
1362 if (err->code == GOT_ERR_ITER_COMPLETED) {
1363 err = NULL;
1364 break;
1366 if (err->code != GOT_ERR_ITER_NEED_MORE)
1367 break;
1368 err = got_commit_graph_fetch_commits(graph, 1, repo);
1369 if (err)
1370 break;
1371 else
1372 continue;
1374 if (id == NULL)
1375 break;
1377 err = got_object_open_as_commit(&commit, repo, id);
1378 if (err)
1379 break;
1380 err = print_commit(commit, id, repo, show_patch, diff_context,
1381 refs);
1382 got_object_commit_close(commit);
1383 if (err || (limit && --limit == 0))
1384 break;
1386 done:
1387 got_commit_graph_close(graph);
1388 return err;
1391 __dead static void
1392 usage_log(void)
1394 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1395 "[-r repository-path] [path]\n", getprogname());
1396 exit(1);
1399 static const struct got_error *
1400 cmd_log(int argc, char *argv[])
1402 const struct got_error *error;
1403 struct got_repository *repo = NULL;
1404 struct got_worktree *worktree = NULL;
1405 struct got_commit_object *commit = NULL;
1406 struct got_object_id *id = NULL;
1407 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1408 char *start_commit = NULL;
1409 int diff_context = 3, ch;
1410 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1411 const char *errstr;
1412 struct got_reflist_head refs;
1414 SIMPLEQ_INIT(&refs);
1416 #ifndef PROFILE
1417 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1418 NULL)
1419 == -1)
1420 err(1, "pledge");
1421 #endif
1423 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1424 switch (ch) {
1425 case 'p':
1426 show_patch = 1;
1427 break;
1428 case 'c':
1429 start_commit = optarg;
1430 break;
1431 case 'C':
1432 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1433 &errstr);
1434 if (errstr != NULL)
1435 err(1, "-C option %s", errstr);
1436 break;
1437 case 'l':
1438 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1439 if (errstr != NULL)
1440 err(1, "-l option %s", errstr);
1441 break;
1442 case 'f':
1443 first_parent_traversal = 1;
1444 break;
1445 case 'r':
1446 repo_path = realpath(optarg, NULL);
1447 if (repo_path == NULL)
1448 err(1, "-r option");
1449 got_path_strip_trailing_slashes(repo_path);
1450 break;
1451 default:
1452 usage_log();
1453 /* NOTREACHED */
1457 argc -= optind;
1458 argv += optind;
1460 cwd = getcwd(NULL, 0);
1461 if (cwd == NULL) {
1462 error = got_error_from_errno("getcwd");
1463 goto done;
1466 error = got_worktree_open(&worktree, cwd);
1467 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1468 goto done;
1469 error = NULL;
1471 if (argc == 0) {
1472 path = strdup("");
1473 if (path == NULL) {
1474 error = got_error_from_errno("strdup");
1475 goto done;
1477 } else if (argc == 1) {
1478 if (worktree) {
1479 error = got_worktree_resolve_path(&path, worktree,
1480 argv[0]);
1481 if (error)
1482 goto done;
1483 } else {
1484 path = strdup(argv[0]);
1485 if (path == NULL) {
1486 error = got_error_from_errno("strdup");
1487 goto done;
1490 } else
1491 usage_log();
1493 if (repo_path == NULL) {
1494 repo_path = worktree ?
1495 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1497 if (repo_path == NULL) {
1498 error = got_error_from_errno("strdup");
1499 goto done;
1502 error = got_repo_open(&repo, repo_path);
1503 if (error != NULL)
1504 goto done;
1506 error = apply_unveil(got_repo_get_path(repo), 1,
1507 worktree ? got_worktree_get_root_path(worktree) : NULL);
1508 if (error)
1509 goto done;
1511 if (start_commit == NULL) {
1512 struct got_reference *head_ref;
1513 error = got_ref_open(&head_ref, repo,
1514 worktree ? got_worktree_get_head_ref_name(worktree)
1515 : GOT_REF_HEAD, 0);
1516 if (error != NULL)
1517 return error;
1518 error = got_ref_resolve(&id, repo, head_ref);
1519 got_ref_close(head_ref);
1520 if (error != NULL)
1521 return error;
1522 error = got_object_open_as_commit(&commit, repo, id);
1523 } else {
1524 struct got_reference *ref;
1525 error = got_ref_open(&ref, repo, start_commit, 0);
1526 if (error == NULL) {
1527 int obj_type;
1528 error = got_ref_resolve(&id, repo, ref);
1529 got_ref_close(ref);
1530 if (error != NULL)
1531 goto done;
1532 error = got_object_get_type(&obj_type, repo, id);
1533 if (error != NULL)
1534 goto done;
1535 if (obj_type == GOT_OBJ_TYPE_TAG) {
1536 struct got_tag_object *tag;
1537 error = got_object_open_as_tag(&tag, repo, id);
1538 if (error != NULL)
1539 goto done;
1540 if (got_object_tag_get_object_type(tag) !=
1541 GOT_OBJ_TYPE_COMMIT) {
1542 got_object_tag_close(tag);
1543 error = got_error(GOT_ERR_OBJ_TYPE);
1544 goto done;
1546 free(id);
1547 id = got_object_id_dup(
1548 got_object_tag_get_object_id(tag));
1549 if (id == NULL)
1550 error = got_error_from_errno(
1551 "got_object_id_dup");
1552 got_object_tag_close(tag);
1553 if (error)
1554 goto done;
1555 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1556 error = got_error(GOT_ERR_OBJ_TYPE);
1557 goto done;
1559 error = got_object_open_as_commit(&commit, repo, id);
1560 if (error != NULL)
1561 goto done;
1563 if (commit == NULL) {
1564 error = got_repo_match_object_id_prefix(&id,
1565 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1566 if (error != NULL)
1567 return error;
1570 if (error != NULL)
1571 goto done;
1573 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1574 if (error != NULL)
1575 goto done;
1576 if (in_repo_path) {
1577 free(path);
1578 path = in_repo_path;
1581 error = got_ref_list(&refs, repo);
1582 if (error)
1583 goto done;
1585 error = print_commits(id, repo, path, show_patch,
1586 diff_context, limit, first_parent_traversal, &refs);
1587 done:
1588 free(path);
1589 free(repo_path);
1590 free(cwd);
1591 free(id);
1592 if (worktree)
1593 got_worktree_close(worktree);
1594 if (repo) {
1595 const struct got_error *repo_error;
1596 repo_error = got_repo_close(repo);
1597 if (error == NULL)
1598 error = repo_error;
1600 got_ref_list_free(&refs);
1601 return error;
1604 __dead static void
1605 usage_diff(void)
1607 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1608 "[object1 object2 | path]\n", getprogname());
1609 exit(1);
1612 struct print_diff_arg {
1613 struct got_repository *repo;
1614 struct got_worktree *worktree;
1615 int diff_context;
1616 const char *id_str;
1617 int header_shown;
1620 static const struct got_error *
1621 print_diff(void *arg, unsigned char status, const char *path,
1622 struct got_object_id *blob_id, struct got_object_id *commit_id)
1624 struct print_diff_arg *a = arg;
1625 const struct got_error *err = NULL;
1626 struct got_blob_object *blob1 = NULL;
1627 FILE *f2 = NULL;
1628 char *abspath = NULL;
1629 struct stat sb;
1631 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1632 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1633 return NULL;
1635 if (!a->header_shown) {
1636 printf("diff %s %s\n", a->id_str,
1637 got_worktree_get_root_path(a->worktree));
1638 a->header_shown = 1;
1641 if (status != GOT_STATUS_ADD) {
1642 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1643 if (err)
1644 goto done;
1648 if (status != GOT_STATUS_DELETE) {
1649 if (asprintf(&abspath, "%s/%s",
1650 got_worktree_get_root_path(a->worktree), path) == -1) {
1651 err = got_error_from_errno("asprintf");
1652 goto done;
1655 f2 = fopen(abspath, "r");
1656 if (f2 == NULL) {
1657 err = got_error_from_errno2("fopen", abspath);
1658 goto done;
1660 if (lstat(abspath, &sb) == -1) {
1661 err = got_error_from_errno2("lstat", abspath);
1662 goto done;
1664 } else
1665 sb.st_size = 0;
1667 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1668 stdout);
1669 done:
1670 if (blob1)
1671 got_object_blob_close(blob1);
1672 if (f2 && fclose(f2) != 0 && err == NULL)
1673 err = got_error_from_errno("fclose");
1674 free(abspath);
1675 return err;
1678 static const struct got_error *
1679 cmd_diff(int argc, char *argv[])
1681 const struct got_error *error;
1682 struct got_repository *repo = NULL;
1683 struct got_worktree *worktree = NULL;
1684 char *cwd = NULL, *repo_path = NULL;
1685 struct got_object_id *id1 = NULL, *id2 = NULL;
1686 const char *id_str1 = NULL, *id_str2 = NULL;
1687 char *label1 = NULL, *label2 = NULL;
1688 int type1, type2;
1689 int diff_context = 3, ch;
1690 const char *errstr;
1691 char *path = NULL;
1693 #ifndef PROFILE
1694 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1695 NULL) == -1)
1696 err(1, "pledge");
1697 #endif
1699 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1700 switch (ch) {
1701 case 'C':
1702 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1703 if (errstr != NULL)
1704 err(1, "-C option %s", errstr);
1705 break;
1706 case 'r':
1707 repo_path = realpath(optarg, NULL);
1708 if (repo_path == NULL)
1709 err(1, "-r option");
1710 got_path_strip_trailing_slashes(repo_path);
1711 break;
1712 default:
1713 usage_diff();
1714 /* NOTREACHED */
1718 argc -= optind;
1719 argv += optind;
1721 cwd = getcwd(NULL, 0);
1722 if (cwd == NULL) {
1723 error = got_error_from_errno("getcwd");
1724 goto done;
1726 error = got_worktree_open(&worktree, cwd);
1727 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1728 goto done;
1729 if (argc <= 1) {
1730 if (worktree == NULL) {
1731 error = got_error(GOT_ERR_NOT_WORKTREE);
1732 goto done;
1734 if (repo_path)
1735 errx(1,
1736 "-r option can't be used when diffing a work tree");
1737 repo_path = strdup(got_worktree_get_repo_path(worktree));
1738 if (repo_path == NULL) {
1739 error = got_error_from_errno("strdup");
1740 goto done;
1742 if (argc == 1) {
1743 error = got_worktree_resolve_path(&path, worktree,
1744 argv[0]);
1745 if (error)
1746 goto done;
1747 } else {
1748 path = strdup("");
1749 if (path == NULL) {
1750 error = got_error_from_errno("strdup");
1751 goto done;
1754 } else if (argc == 2) {
1755 id_str1 = argv[0];
1756 id_str2 = argv[1];
1757 if (worktree && repo_path == NULL) {
1758 repo_path =
1759 strdup(got_worktree_get_repo_path(worktree));
1760 if (repo_path == NULL) {
1761 error = got_error_from_errno("strdup");
1762 goto done;
1765 } else
1766 usage_diff();
1768 if (repo_path == NULL) {
1769 repo_path = getcwd(NULL, 0);
1770 if (repo_path == NULL)
1771 return got_error_from_errno("getcwd");
1774 error = got_repo_open(&repo, repo_path);
1775 free(repo_path);
1776 if (error != NULL)
1777 goto done;
1779 error = apply_unveil(got_repo_get_path(repo), 1,
1780 worktree ? got_worktree_get_root_path(worktree) : NULL);
1781 if (error)
1782 goto done;
1784 if (argc <= 1) {
1785 struct print_diff_arg arg;
1786 char *id_str;
1787 error = got_object_id_str(&id_str,
1788 got_worktree_get_base_commit_id(worktree));
1789 if (error)
1790 goto done;
1791 arg.repo = repo;
1792 arg.worktree = worktree;
1793 arg.diff_context = diff_context;
1794 arg.id_str = id_str;
1795 arg.header_shown = 0;
1797 error = got_worktree_status(worktree, path, repo, print_diff,
1798 &arg, check_cancelled, NULL);
1799 free(id_str);
1800 goto done;
1803 error = got_repo_match_object_id_prefix(&id1, id_str1,
1804 GOT_OBJ_TYPE_ANY, repo);
1805 if (error) {
1806 struct got_reference *ref;
1807 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1808 goto done;
1809 error = got_ref_open(&ref, repo, id_str1, 0);
1810 if (error != NULL)
1811 goto done;
1812 label1 = strdup(got_ref_get_name(ref));
1813 if (label1 == NULL) {
1814 error = got_error_from_errno("strdup");
1815 goto done;
1817 error = got_ref_resolve(&id1, repo, ref);
1818 got_ref_close(ref);
1819 if (error != NULL)
1820 goto done;
1821 } else {
1822 error = got_object_id_str(&label1, id1);
1823 if (label1 == NULL) {
1824 error = got_error_from_errno("strdup");
1825 goto done;
1829 error = got_repo_match_object_id_prefix(&id2, id_str2,
1830 GOT_OBJ_TYPE_ANY, repo);
1831 if (error) {
1832 struct got_reference *ref;
1833 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1834 goto done;
1835 error = got_ref_open(&ref, repo, id_str2, 0);
1836 if (error != NULL)
1837 goto done;
1838 label2 = strdup(got_ref_get_name(ref));
1839 if (label2 == NULL) {
1840 error = got_error_from_errno("strdup");
1841 goto done;
1843 error = got_ref_resolve(&id2, repo, ref);
1844 got_ref_close(ref);
1845 if (error != NULL)
1846 goto done;
1847 } else {
1848 error = got_object_id_str(&label2, id2);
1849 if (label2 == NULL) {
1850 error = got_error_from_errno("strdup");
1851 goto done;
1855 error = got_object_get_type(&type1, repo, id1);
1856 if (error)
1857 goto done;
1859 error = got_object_get_type(&type2, repo, id2);
1860 if (error)
1861 goto done;
1863 if (type1 != type2) {
1864 error = got_error(GOT_ERR_OBJ_TYPE);
1865 goto done;
1868 switch (type1) {
1869 case GOT_OBJ_TYPE_BLOB:
1870 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1871 diff_context, repo, stdout);
1872 break;
1873 case GOT_OBJ_TYPE_TREE:
1874 error = got_diff_objects_as_trees(id1, id2, "", "",
1875 diff_context, repo, stdout);
1876 break;
1877 case GOT_OBJ_TYPE_COMMIT:
1878 printf("diff %s %s\n", label1, label2);
1879 error = got_diff_objects_as_commits(id1, id2, diff_context,
1880 repo, stdout);
1881 break;
1882 default:
1883 error = got_error(GOT_ERR_OBJ_TYPE);
1886 done:
1887 free(label1);
1888 free(label2);
1889 free(id1);
1890 free(id2);
1891 free(path);
1892 if (worktree)
1893 got_worktree_close(worktree);
1894 if (repo) {
1895 const struct got_error *repo_error;
1896 repo_error = got_repo_close(repo);
1897 if (error == NULL)
1898 error = repo_error;
1900 return error;
1903 __dead static void
1904 usage_blame(void)
1906 fprintf(stderr,
1907 "usage: %s blame [-c commit] [-r repository-path] path\n",
1908 getprogname());
1909 exit(1);
1912 static const struct got_error *
1913 cmd_blame(int argc, char *argv[])
1915 const struct got_error *error;
1916 struct got_repository *repo = NULL;
1917 struct got_worktree *worktree = NULL;
1918 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1919 struct got_object_id *commit_id = NULL;
1920 char *commit_id_str = NULL;
1921 int ch;
1923 #ifndef PROFILE
1924 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1925 NULL) == -1)
1926 err(1, "pledge");
1927 #endif
1929 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1930 switch (ch) {
1931 case 'c':
1932 commit_id_str = optarg;
1933 break;
1934 case 'r':
1935 repo_path = realpath(optarg, NULL);
1936 if (repo_path == NULL)
1937 err(1, "-r option");
1938 got_path_strip_trailing_slashes(repo_path);
1939 break;
1940 default:
1941 usage_blame();
1942 /* NOTREACHED */
1946 argc -= optind;
1947 argv += optind;
1949 if (argc == 1)
1950 path = argv[0];
1951 else
1952 usage_blame();
1954 cwd = getcwd(NULL, 0);
1955 if (cwd == NULL) {
1956 error = got_error_from_errno("getcwd");
1957 goto done;
1959 if (repo_path == NULL) {
1960 error = got_worktree_open(&worktree, cwd);
1961 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1962 goto done;
1963 else
1964 error = NULL;
1965 if (worktree) {
1966 repo_path =
1967 strdup(got_worktree_get_repo_path(worktree));
1968 if (repo_path == NULL)
1969 error = got_error_from_errno("strdup");
1970 if (error)
1971 goto done;
1972 } else {
1973 repo_path = strdup(cwd);
1974 if (repo_path == NULL) {
1975 error = got_error_from_errno("strdup");
1976 goto done;
1981 error = got_repo_open(&repo, repo_path);
1982 if (error != NULL)
1983 goto done;
1985 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1986 if (error)
1987 goto done;
1989 if (worktree) {
1990 const char *prefix = got_worktree_get_path_prefix(worktree);
1991 char *p, *worktree_subdir = cwd +
1992 strlen(got_worktree_get_root_path(worktree));
1993 if (asprintf(&p, "%s%s%s%s%s",
1994 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1995 worktree_subdir, worktree_subdir[0] ? "/" : "",
1996 path) == -1) {
1997 error = got_error_from_errno("asprintf");
1998 goto done;
2000 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2001 free(p);
2002 } else {
2003 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2005 if (error)
2006 goto done;
2008 if (commit_id_str == NULL) {
2009 struct got_reference *head_ref;
2010 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2011 if (error != NULL)
2012 goto done;
2013 error = got_ref_resolve(&commit_id, repo, head_ref);
2014 got_ref_close(head_ref);
2015 if (error != NULL)
2016 goto done;
2017 } else {
2018 error = got_repo_match_object_id_prefix(&commit_id,
2019 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2020 if (error != NULL)
2021 goto done;
2024 error = got_blame(in_repo_path, commit_id, repo, stdout);
2025 done:
2026 free(in_repo_path);
2027 free(repo_path);
2028 free(cwd);
2029 free(commit_id);
2030 if (worktree)
2031 got_worktree_close(worktree);
2032 if (repo) {
2033 const struct got_error *repo_error;
2034 repo_error = got_repo_close(repo);
2035 if (error == NULL)
2036 error = repo_error;
2038 return error;
2041 __dead static void
2042 usage_tree(void)
2044 fprintf(stderr,
2045 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2046 getprogname());
2047 exit(1);
2050 static void
2051 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2052 const char *root_path)
2054 int is_root_path = (strcmp(path, root_path) == 0);
2056 path += strlen(root_path);
2057 while (path[0] == '/')
2058 path++;
2060 printf("%s%s%s%s%s\n", id ? id : "", path,
2061 is_root_path ? "" : "/", te->name,
2062 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2065 static const struct got_error *
2066 print_tree(const char *path, struct got_object_id *commit_id,
2067 int show_ids, int recurse, const char *root_path,
2068 struct got_repository *repo)
2070 const struct got_error *err = NULL;
2071 struct got_object_id *tree_id = NULL;
2072 struct got_tree_object *tree = NULL;
2073 const struct got_tree_entries *entries;
2074 struct got_tree_entry *te;
2076 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2077 if (err)
2078 goto done;
2080 err = got_object_open_as_tree(&tree, repo, tree_id);
2081 if (err)
2082 goto done;
2083 entries = got_object_tree_get_entries(tree);
2084 te = SIMPLEQ_FIRST(&entries->head);
2085 while (te) {
2086 char *id = NULL;
2088 if (sigint_received || sigpipe_received)
2089 break;
2091 if (show_ids) {
2092 char *id_str;
2093 err = got_object_id_str(&id_str, te->id);
2094 if (err)
2095 goto done;
2096 if (asprintf(&id, "%s ", id_str) == -1) {
2097 err = got_error_from_errno("asprintf");
2098 free(id_str);
2099 goto done;
2101 free(id_str);
2103 print_entry(te, id, path, root_path);
2104 free(id);
2106 if (recurse && S_ISDIR(te->mode)) {
2107 char *child_path;
2108 if (asprintf(&child_path, "%s%s%s", path,
2109 path[0] == '/' && path[1] == '\0' ? "" : "/",
2110 te->name) == -1) {
2111 err = got_error_from_errno("asprintf");
2112 goto done;
2114 err = print_tree(child_path, commit_id, show_ids, 1,
2115 root_path, repo);
2116 free(child_path);
2117 if (err)
2118 goto done;
2121 te = SIMPLEQ_NEXT(te, entry);
2123 done:
2124 if (tree)
2125 got_object_tree_close(tree);
2126 free(tree_id);
2127 return err;
2130 static const struct got_error *
2131 cmd_tree(int argc, char *argv[])
2133 const struct got_error *error;
2134 struct got_repository *repo = NULL;
2135 struct got_worktree *worktree = NULL;
2136 const char *path;
2137 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2138 struct got_object_id *commit_id = NULL;
2139 char *commit_id_str = NULL;
2140 int show_ids = 0, recurse = 0;
2141 int ch;
2143 #ifndef PROFILE
2144 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2145 NULL) == -1)
2146 err(1, "pledge");
2147 #endif
2149 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2150 switch (ch) {
2151 case 'c':
2152 commit_id_str = optarg;
2153 break;
2154 case 'r':
2155 repo_path = realpath(optarg, NULL);
2156 if (repo_path == NULL)
2157 err(1, "-r option");
2158 got_path_strip_trailing_slashes(repo_path);
2159 break;
2160 case 'i':
2161 show_ids = 1;
2162 break;
2163 case 'R':
2164 recurse = 1;
2165 break;
2166 default:
2167 usage_tree();
2168 /* NOTREACHED */
2172 argc -= optind;
2173 argv += optind;
2175 if (argc == 1)
2176 path = argv[0];
2177 else if (argc > 1)
2178 usage_tree();
2179 else
2180 path = NULL;
2182 cwd = getcwd(NULL, 0);
2183 if (cwd == NULL) {
2184 error = got_error_from_errno("getcwd");
2185 goto done;
2187 if (repo_path == NULL) {
2188 error = got_worktree_open(&worktree, cwd);
2189 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2190 goto done;
2191 else
2192 error = NULL;
2193 if (worktree) {
2194 repo_path =
2195 strdup(got_worktree_get_repo_path(worktree));
2196 if (repo_path == NULL)
2197 error = got_error_from_errno("strdup");
2198 if (error)
2199 goto done;
2200 } else {
2201 repo_path = strdup(cwd);
2202 if (repo_path == NULL) {
2203 error = got_error_from_errno("strdup");
2204 goto done;
2209 error = got_repo_open(&repo, repo_path);
2210 if (error != NULL)
2211 goto done;
2213 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2214 if (error)
2215 goto done;
2217 if (path == NULL) {
2218 if (worktree) {
2219 char *p, *worktree_subdir = cwd +
2220 strlen(got_worktree_get_root_path(worktree));
2221 if (asprintf(&p, "%s/%s",
2222 got_worktree_get_path_prefix(worktree),
2223 worktree_subdir) == -1) {
2224 error = got_error_from_errno("asprintf");
2225 goto done;
2227 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2228 free(p);
2229 if (error)
2230 goto done;
2231 } else
2232 path = "/";
2234 if (in_repo_path == NULL) {
2235 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2236 if (error != NULL)
2237 goto done;
2240 if (commit_id_str == NULL) {
2241 struct got_reference *head_ref;
2242 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2243 if (error != NULL)
2244 goto done;
2245 error = got_ref_resolve(&commit_id, repo, head_ref);
2246 got_ref_close(head_ref);
2247 if (error != NULL)
2248 goto done;
2249 } else {
2250 error = got_repo_match_object_id_prefix(&commit_id,
2251 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2252 if (error != NULL)
2253 goto done;
2256 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2257 in_repo_path, repo);
2258 done:
2259 free(in_repo_path);
2260 free(repo_path);
2261 free(cwd);
2262 free(commit_id);
2263 if (worktree)
2264 got_worktree_close(worktree);
2265 if (repo) {
2266 const struct got_error *repo_error;
2267 repo_error = got_repo_close(repo);
2268 if (error == NULL)
2269 error = repo_error;
2271 return error;
2274 __dead static void
2275 usage_status(void)
2277 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2278 exit(1);
2281 static const struct got_error *
2282 print_status(void *arg, unsigned char status, const char *path,
2283 struct got_object_id *blob_id, struct got_object_id *commit_id)
2285 printf("%c %s\n", status, path);
2286 return NULL;
2289 static const struct got_error *
2290 cmd_status(int argc, char *argv[])
2292 const struct got_error *error = NULL;
2293 struct got_repository *repo = NULL;
2294 struct got_worktree *worktree = NULL;
2295 char *cwd = NULL, *path = NULL;
2296 int ch;
2298 while ((ch = getopt(argc, argv, "")) != -1) {
2299 switch (ch) {
2300 default:
2301 usage_status();
2302 /* NOTREACHED */
2306 argc -= optind;
2307 argv += optind;
2309 #ifndef PROFILE
2310 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2311 NULL) == -1)
2312 err(1, "pledge");
2313 #endif
2314 cwd = getcwd(NULL, 0);
2315 if (cwd == NULL) {
2316 error = got_error_from_errno("getcwd");
2317 goto done;
2320 error = got_worktree_open(&worktree, cwd);
2321 if (error != NULL)
2322 goto done;
2324 if (argc == 0) {
2325 path = strdup("");
2326 if (path == NULL) {
2327 error = got_error_from_errno("strdup");
2328 goto done;
2330 } else if (argc == 1) {
2331 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2332 if (error)
2333 goto done;
2334 } else
2335 usage_status();
2337 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2338 if (error != NULL)
2339 goto done;
2341 error = apply_unveil(got_repo_get_path(repo), 1,
2342 got_worktree_get_root_path(worktree));
2343 if (error)
2344 goto done;
2346 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2347 check_cancelled, NULL);
2348 done:
2349 free(cwd);
2350 free(path);
2351 return error;
2354 __dead static void
2355 usage_ref(void)
2357 fprintf(stderr,
2358 "usage: %s ref [-r repository] -l | -d name | name target\n",
2359 getprogname());
2360 exit(1);
2363 static const struct got_error *
2364 list_refs(struct got_repository *repo)
2366 static const struct got_error *err = NULL;
2367 struct got_reflist_head refs;
2368 struct got_reflist_entry *re;
2370 SIMPLEQ_INIT(&refs);
2371 err = got_ref_list(&refs, repo);
2372 if (err)
2373 return err;
2375 SIMPLEQ_FOREACH(re, &refs, entry) {
2376 char *refstr;
2377 refstr = got_ref_to_str(re->ref);
2378 if (refstr == NULL)
2379 return got_error_from_errno("got_ref_to_str");
2380 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2381 free(refstr);
2384 got_ref_list_free(&refs);
2385 return NULL;
2388 static const struct got_error *
2389 delete_ref(struct got_repository *repo, const char *refname)
2391 const struct got_error *err = NULL;
2392 struct got_reference *ref;
2394 err = got_ref_open(&ref, repo, refname, 0);
2395 if (err)
2396 return err;
2398 err = got_ref_delete(ref, repo);
2399 got_ref_close(ref);
2400 return err;
2403 static const struct got_error *
2404 add_ref(struct got_repository *repo, const char *refname, const char *target)
2406 const struct got_error *err = NULL;
2407 struct got_object_id *id;
2408 struct got_reference *ref = NULL;
2411 * Don't let the user create a reference named '-'.
2412 * While technically a valid reference name, this case is usually
2413 * an unintended typo.
2415 if (refname[0] == '-' && refname[1] == '\0')
2416 return got_error(GOT_ERR_BAD_REF_NAME);
2418 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2419 repo);
2420 if (err) {
2421 struct got_reference *target_ref;
2423 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2424 return err;
2425 err = got_ref_open(&target_ref, repo, target, 0);
2426 if (err)
2427 return err;
2428 err = got_ref_resolve(&id, repo, target_ref);
2429 got_ref_close(target_ref);
2430 if (err)
2431 return err;
2434 err = got_ref_alloc(&ref, refname, id);
2435 if (err)
2436 goto done;
2438 err = got_ref_write(ref, repo);
2439 done:
2440 if (ref)
2441 got_ref_close(ref);
2442 free(id);
2443 return err;
2446 static const struct got_error *
2447 cmd_ref(int argc, char *argv[])
2449 const struct got_error *error = NULL;
2450 struct got_repository *repo = NULL;
2451 struct got_worktree *worktree = NULL;
2452 char *cwd = NULL, *repo_path = NULL;
2453 int ch, do_list = 0;
2454 const char *delref = NULL;
2456 /* TODO: Add -s option for adding symbolic references. */
2457 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2458 switch (ch) {
2459 case 'd':
2460 delref = optarg;
2461 break;
2462 case 'r':
2463 repo_path = realpath(optarg, NULL);
2464 if (repo_path == NULL)
2465 err(1, "-r option");
2466 got_path_strip_trailing_slashes(repo_path);
2467 break;
2468 case 'l':
2469 do_list = 1;
2470 break;
2471 default:
2472 usage_ref();
2473 /* NOTREACHED */
2477 if (do_list && delref)
2478 errx(1, "-l and -d options are mutually exclusive\n");
2480 argc -= optind;
2481 argv += optind;
2483 if (do_list || delref) {
2484 if (argc > 0)
2485 usage_ref();
2486 } else if (argc != 2)
2487 usage_ref();
2489 #ifndef PROFILE
2490 if (do_list) {
2491 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2492 NULL) == -1)
2493 err(1, "pledge");
2494 } else {
2495 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2496 "sendfd unveil", NULL) == -1)
2497 err(1, "pledge");
2499 #endif
2500 cwd = getcwd(NULL, 0);
2501 if (cwd == NULL) {
2502 error = got_error_from_errno("getcwd");
2503 goto done;
2506 if (repo_path == NULL) {
2507 error = got_worktree_open(&worktree, cwd);
2508 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2509 goto done;
2510 else
2511 error = NULL;
2512 if (worktree) {
2513 repo_path =
2514 strdup(got_worktree_get_repo_path(worktree));
2515 if (repo_path == NULL)
2516 error = got_error_from_errno("strdup");
2517 if (error)
2518 goto done;
2519 } else {
2520 repo_path = strdup(cwd);
2521 if (repo_path == NULL) {
2522 error = got_error_from_errno("strdup");
2523 goto done;
2528 error = got_repo_open(&repo, repo_path);
2529 if (error != NULL)
2530 goto done;
2532 error = apply_unveil(got_repo_get_path(repo), do_list,
2533 worktree ? got_worktree_get_root_path(worktree) : NULL);
2534 if (error)
2535 goto done;
2537 if (do_list)
2538 error = list_refs(repo);
2539 else if (delref)
2540 error = delete_ref(repo, delref);
2541 else
2542 error = add_ref(repo, argv[0], argv[1]);
2543 done:
2544 if (repo)
2545 got_repo_close(repo);
2546 if (worktree)
2547 got_worktree_close(worktree);
2548 free(cwd);
2549 free(repo_path);
2550 return error;
2553 __dead static void
2554 usage_branch(void)
2556 fprintf(stderr,
2557 "usage: %s branch [-r repository] -l | -d name | "
2558 "name [base-branch]\n", getprogname());
2559 exit(1);
2562 static const struct got_error *
2563 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2565 static const struct got_error *err = NULL;
2566 struct got_reflist_head refs;
2567 struct got_reflist_entry *re;
2569 SIMPLEQ_INIT(&refs);
2571 err = got_ref_list(&refs, repo);
2572 if (err)
2573 return err;
2575 SIMPLEQ_FOREACH(re, &refs, entry) {
2576 const char *refname, *marker = " ";
2577 char *refstr;
2578 refname = got_ref_get_name(re->ref);
2579 if (strncmp(refname, "refs/heads/", 11) != 0)
2580 continue;
2581 if (worktree && strcmp(refname,
2582 got_worktree_get_head_ref_name(worktree)) == 0) {
2583 struct got_object_id *id = NULL;
2584 err = got_ref_resolve(&id, repo, re->ref);
2585 if (err)
2586 return err;
2587 if (got_object_id_cmp(id,
2588 got_worktree_get_base_commit_id(worktree)) == 0)
2589 marker = "* ";
2590 else
2591 marker = "~ ";
2592 free(id);
2594 refname += 11;
2595 refstr = got_ref_to_str(re->ref);
2596 if (refstr == NULL)
2597 return got_error_from_errno("got_ref_to_str");
2598 printf("%s%s: %s\n", marker, refname, refstr);
2599 free(refstr);
2602 got_ref_list_free(&refs);
2603 return NULL;
2606 static const struct got_error *
2607 delete_branch(struct got_repository *repo, const char *branch_name)
2609 const struct got_error *err = NULL;
2610 struct got_reference *ref;
2611 char *refname;
2613 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2614 return got_error_from_errno("asprintf");
2616 err = got_ref_open(&ref, repo, refname, 0);
2617 if (err)
2618 goto done;
2620 err = got_ref_delete(ref, repo);
2621 got_ref_close(ref);
2622 done:
2623 free(refname);
2624 return err;
2627 static const struct got_error *
2628 add_branch(struct got_repository *repo, const char *branch_name,
2629 const char *base_branch)
2631 const struct got_error *err = NULL;
2632 struct got_object_id *id = NULL;
2633 struct got_reference *ref = NULL;
2634 char *base_refname = NULL, *refname = NULL;
2635 struct got_reference *base_ref;
2638 * Don't let the user create a branch named '-'.
2639 * While technically a valid reference name, this case is usually
2640 * an unintended typo.
2642 if (branch_name[0] == '-' && branch_name[1] == '\0')
2643 return got_error(GOT_ERR_BAD_REF_NAME);
2645 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2646 base_refname = strdup(GOT_REF_HEAD);
2647 if (base_refname == NULL)
2648 return got_error_from_errno("strdup");
2649 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2650 return got_error_from_errno("asprintf");
2652 err = got_ref_open(&base_ref, repo, base_refname, 0);
2653 if (err)
2654 goto done;
2655 err = got_ref_resolve(&id, repo, base_ref);
2656 got_ref_close(base_ref);
2657 if (err)
2658 goto done;
2660 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2661 err = got_error_from_errno("asprintf");
2662 goto done;
2665 err = got_ref_open(&ref, repo, refname, 0);
2666 if (err == NULL) {
2667 err = got_error(GOT_ERR_BRANCH_EXISTS);
2668 goto done;
2669 } else if (err->code != GOT_ERR_NOT_REF)
2670 goto done;
2672 err = got_ref_alloc(&ref, refname, id);
2673 if (err)
2674 goto done;
2676 err = got_ref_write(ref, repo);
2677 done:
2678 if (ref)
2679 got_ref_close(ref);
2680 free(id);
2681 free(base_refname);
2682 free(refname);
2683 return err;
2686 static const struct got_error *
2687 cmd_branch(int argc, char *argv[])
2689 const struct got_error *error = NULL;
2690 struct got_repository *repo = NULL;
2691 struct got_worktree *worktree = NULL;
2692 char *cwd = NULL, *repo_path = NULL;
2693 int ch, do_list = 0;
2694 const char *delref = NULL;
2696 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2697 switch (ch) {
2698 case 'd':
2699 delref = optarg;
2700 break;
2701 case 'r':
2702 repo_path = realpath(optarg, NULL);
2703 if (repo_path == NULL)
2704 err(1, "-r option");
2705 got_path_strip_trailing_slashes(repo_path);
2706 break;
2707 case 'l':
2708 do_list = 1;
2709 break;
2710 default:
2711 usage_branch();
2712 /* NOTREACHED */
2716 if (do_list && delref)
2717 errx(1, "-l and -d options are mutually exclusive\n");
2719 argc -= optind;
2720 argv += optind;
2722 if (do_list || delref) {
2723 if (argc > 0)
2724 usage_branch();
2725 } else if (argc < 1 || argc > 2)
2726 usage_branch();
2728 #ifndef PROFILE
2729 if (do_list) {
2730 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2731 NULL) == -1)
2732 err(1, "pledge");
2733 } else {
2734 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2735 "sendfd unveil", NULL) == -1)
2736 err(1, "pledge");
2738 #endif
2739 cwd = getcwd(NULL, 0);
2740 if (cwd == NULL) {
2741 error = got_error_from_errno("getcwd");
2742 goto done;
2745 if (repo_path == NULL) {
2746 error = got_worktree_open(&worktree, cwd);
2747 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2748 goto done;
2749 else
2750 error = NULL;
2751 if (worktree) {
2752 repo_path =
2753 strdup(got_worktree_get_repo_path(worktree));
2754 if (repo_path == NULL)
2755 error = got_error_from_errno("strdup");
2756 if (error)
2757 goto done;
2758 } else {
2759 repo_path = strdup(cwd);
2760 if (repo_path == NULL) {
2761 error = got_error_from_errno("strdup");
2762 goto done;
2767 error = got_repo_open(&repo, repo_path);
2768 if (error != NULL)
2769 goto done;
2771 error = apply_unveil(got_repo_get_path(repo), do_list,
2772 worktree ? got_worktree_get_root_path(worktree) : NULL);
2773 if (error)
2774 goto done;
2776 if (do_list)
2777 error = list_branches(repo, worktree);
2778 else if (delref)
2779 error = delete_branch(repo, delref);
2780 else {
2781 const char *base_branch;
2782 if (argc == 1) {
2783 base_branch = worktree ?
2784 got_worktree_get_head_ref_name(worktree) :
2785 GOT_REF_HEAD;
2786 } else
2787 base_branch = argv[1];
2788 error = add_branch(repo, argv[0], base_branch);
2790 done:
2791 if (repo)
2792 got_repo_close(repo);
2793 if (worktree)
2794 got_worktree_close(worktree);
2795 free(cwd);
2796 free(repo_path);
2797 return error;
2800 __dead static void
2801 usage_add(void)
2803 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2804 exit(1);
2807 static const struct got_error *
2808 cmd_add(int argc, char *argv[])
2810 const struct got_error *error = NULL;
2811 struct got_repository *repo = NULL;
2812 struct got_worktree *worktree = NULL;
2813 char *cwd = NULL;
2814 struct got_pathlist_head paths;
2815 struct got_pathlist_entry *pe;
2816 int ch, x;
2818 TAILQ_INIT(&paths);
2820 while ((ch = getopt(argc, argv, "")) != -1) {
2821 switch (ch) {
2822 default:
2823 usage_add();
2824 /* NOTREACHED */
2828 argc -= optind;
2829 argv += optind;
2831 #ifndef PROFILE
2832 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2833 NULL) == -1)
2834 err(1, "pledge");
2835 #endif
2836 if (argc < 1)
2837 usage_add();
2839 cwd = getcwd(NULL, 0);
2840 if (cwd == NULL) {
2841 error = got_error_from_errno("getcwd");
2842 goto done;
2845 error = got_worktree_open(&worktree, cwd);
2846 if (error)
2847 goto done;
2849 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2850 if (error != NULL)
2851 goto done;
2853 error = apply_unveil(got_repo_get_path(repo), 1,
2854 got_worktree_get_root_path(worktree));
2855 if (error)
2856 goto done;
2858 for (x = 0; x < argc; x++) {
2859 char *path = realpath(argv[x], NULL);
2860 if (path == NULL) {
2861 error = got_error_from_errno2("realpath", argv[x]);
2862 goto done;
2865 got_path_strip_trailing_slashes(path);
2866 error = got_pathlist_insert(&pe, &paths, path, NULL);
2867 if (error) {
2868 free(path);
2869 goto done;
2872 error = got_worktree_schedule_add(worktree, &paths, print_status,
2873 NULL, repo);
2874 done:
2875 if (repo)
2876 got_repo_close(repo);
2877 if (worktree)
2878 got_worktree_close(worktree);
2879 TAILQ_FOREACH(pe, &paths, entry)
2880 free((char *)pe->path);
2881 got_pathlist_free(&paths);
2882 free(cwd);
2883 return error;
2886 __dead static void
2887 usage_remove(void)
2889 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2890 exit(1);
2893 static const struct got_error *
2894 cmd_remove(int argc, char *argv[])
2896 const struct got_error *error = NULL;
2897 struct got_worktree *worktree = NULL;
2898 struct got_repository *repo = NULL;
2899 char *cwd = NULL;
2900 struct got_pathlist_head paths;
2901 struct got_pathlist_entry *pe;
2902 int ch, i, delete_local_mods = 0;
2904 TAILQ_INIT(&paths);
2906 while ((ch = getopt(argc, argv, "f")) != -1) {
2907 switch (ch) {
2908 case 'f':
2909 delete_local_mods = 1;
2910 break;
2911 default:
2912 usage_add();
2913 /* NOTREACHED */
2917 argc -= optind;
2918 argv += optind;
2920 #ifndef PROFILE
2921 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2922 NULL) == -1)
2923 err(1, "pledge");
2924 #endif
2925 if (argc < 1)
2926 usage_remove();
2928 cwd = getcwd(NULL, 0);
2929 if (cwd == NULL) {
2930 error = got_error_from_errno("getcwd");
2931 goto done;
2933 error = got_worktree_open(&worktree, cwd);
2934 if (error)
2935 goto done;
2937 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2938 if (error)
2939 goto done;
2941 error = apply_unveil(got_repo_get_path(repo), 1,
2942 got_worktree_get_root_path(worktree));
2943 if (error)
2944 goto done;
2946 for (i = 0; i < argc; i++) {
2947 char *path = realpath(argv[i], NULL);
2948 if (path == NULL) {
2949 error = got_error_from_errno2("realpath", argv[i]);
2950 goto done;
2953 got_path_strip_trailing_slashes(path);
2954 error = got_pathlist_insert(&pe, &paths, path, NULL);
2955 if (error) {
2956 free(path);
2957 goto done;
2960 error = got_worktree_schedule_delete(worktree, &paths,
2961 delete_local_mods, print_status, NULL, repo);
2962 if (error)
2963 goto done;
2964 done:
2965 if (repo)
2966 got_repo_close(repo);
2967 if (worktree)
2968 got_worktree_close(worktree);
2969 TAILQ_FOREACH(pe, &paths, entry)
2970 free((char *)pe->path);
2971 got_pathlist_free(&paths);
2972 free(cwd);
2973 return error;
2976 __dead static void
2977 usage_revert(void)
2979 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2980 exit(1);
2983 static const struct got_error *
2984 revert_progress(void *arg, unsigned char status, const char *path)
2986 while (path[0] == '/')
2987 path++;
2988 printf("%c %s\n", status, path);
2989 return NULL;
2992 static const struct got_error *
2993 cmd_revert(int argc, char *argv[])
2995 const struct got_error *error = NULL;
2996 struct got_worktree *worktree = NULL;
2997 struct got_repository *repo = NULL;
2998 char *cwd = NULL, *path = NULL;
2999 struct got_pathlist_head paths;
3000 struct got_pathlist_entry *pe;
3001 int ch, i;
3003 TAILQ_INIT(&paths);
3005 while ((ch = getopt(argc, argv, "")) != -1) {
3006 switch (ch) {
3007 default:
3008 usage_revert();
3009 /* NOTREACHED */
3013 argc -= optind;
3014 argv += optind;
3016 #ifndef PROFILE
3017 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3018 "unveil", NULL) == -1)
3019 err(1, "pledge");
3020 #endif
3021 if (argc < 1)
3022 usage_revert();
3024 for (i = 0; i < argc; i++) {
3025 char *path = realpath(argv[i], NULL);
3026 if (path == NULL) {
3027 error = got_error_from_errno2("realpath", argv[i]);
3028 goto done;
3031 got_path_strip_trailing_slashes(path);
3032 error = got_pathlist_insert(&pe, &paths, path, NULL);
3033 if (error) {
3034 free(path);
3035 goto done;
3039 cwd = getcwd(NULL, 0);
3040 if (cwd == NULL) {
3041 error = got_error_from_errno("getcwd");
3042 goto done;
3044 error = got_worktree_open(&worktree, cwd);
3045 if (error)
3046 goto done;
3048 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3049 if (error != NULL)
3050 goto done;
3052 error = apply_unveil(got_repo_get_path(repo), 1,
3053 got_worktree_get_root_path(worktree));
3054 if (error)
3055 goto done;
3057 error = got_worktree_revert(worktree, &paths,
3058 revert_progress, NULL, repo);
3059 if (error)
3060 goto done;
3061 done:
3062 if (repo)
3063 got_repo_close(repo);
3064 if (worktree)
3065 got_worktree_close(worktree);
3066 free(path);
3067 free(cwd);
3068 return error;
3071 __dead static void
3072 usage_commit(void)
3074 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3075 exit(1);
3078 struct collect_commit_logmsg_arg {
3079 const char *cmdline_log;
3080 const char *editor;
3081 const char *worktree_path;
3082 const char *branch_name;
3083 const char *repo_path;
3084 char *logmsg_path;
3088 static const struct got_error *
3089 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3090 void *arg)
3092 char *initial_content = NULL;
3093 struct got_pathlist_entry *pe;
3094 const struct got_error *err = NULL;
3095 char *template = NULL;
3096 struct collect_commit_logmsg_arg *a = arg;
3097 int fd;
3098 size_t len;
3100 /* if a message was specified on the command line, just use it */
3101 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3102 len = strlen(a->cmdline_log) + 1;
3103 *logmsg = malloc(len + 1);
3104 if (*logmsg == NULL)
3105 return got_error_from_errno("malloc");
3106 strlcpy(*logmsg, a->cmdline_log, len);
3107 return NULL;
3110 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3111 return got_error_from_errno("asprintf");
3113 if (asprintf(&initial_content,
3114 "\n# changes to be committed on branch %s:\n",
3115 a->branch_name) == -1)
3116 return got_error_from_errno("asprintf");
3118 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3119 if (err)
3120 goto done;
3122 dprintf(fd, initial_content);
3124 TAILQ_FOREACH(pe, commitable_paths, entry) {
3125 struct got_commitable *ct = pe->data;
3126 dprintf(fd, "# %c %s\n",
3127 got_commitable_get_status(ct),
3128 got_commitable_get_path(ct));
3130 close(fd);
3132 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3133 done:
3134 unlink(a->logmsg_path);
3135 free(a->logmsg_path);
3136 free(initial_content);
3137 free(template);
3139 /* Editor is done; we can now apply unveil(2) */
3140 if (err == NULL) {
3141 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3142 if (err) {
3143 free(*logmsg);
3144 *logmsg = NULL;
3147 return err;
3150 static const struct got_error *
3151 cmd_commit(int argc, char *argv[])
3153 const struct got_error *error = NULL;
3154 struct got_worktree *worktree = NULL;
3155 struct got_repository *repo = NULL;
3156 char *cwd = NULL, *path = NULL, *id_str = NULL;
3157 struct got_object_id *id = NULL;
3158 const char *logmsg = NULL;
3159 const char *got_author = getenv("GOT_AUTHOR");
3160 struct collect_commit_logmsg_arg cl_arg;
3161 char *editor = NULL;
3162 int ch;
3164 while ((ch = getopt(argc, argv, "m:")) != -1) {
3165 switch (ch) {
3166 case 'm':
3167 logmsg = optarg;
3168 break;
3169 default:
3170 usage_commit();
3171 /* NOTREACHED */
3175 argc -= optind;
3176 argv += optind;
3178 #ifndef PROFILE
3179 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3180 "unveil", NULL) == -1)
3181 err(1, "pledge");
3182 #endif
3183 if (argc == 1) {
3184 path = realpath(argv[0], NULL);
3185 if (path == NULL) {
3186 error = got_error_from_errno2("realpath", argv[0]);
3187 goto done;
3189 got_path_strip_trailing_slashes(path);
3190 } else if (argc != 0)
3191 usage_commit();
3193 if (got_author == NULL) {
3194 /* TODO: Look current user up in password database */
3195 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3196 goto done;
3199 cwd = getcwd(NULL, 0);
3200 if (cwd == NULL) {
3201 error = got_error_from_errno("getcwd");
3202 goto done;
3204 error = got_worktree_open(&worktree, cwd);
3205 if (error)
3206 goto done;
3208 error = check_rebase_or_histedit_in_progress(worktree);
3209 if (error)
3210 goto done;
3212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3213 if (error != NULL)
3214 goto done;
3217 * unveil(2) traverses exec(2); if an editor is used we have
3218 * to apply unveil after the log message has been written.
3220 if (logmsg == NULL || strlen(logmsg) == 0)
3221 error = get_editor(&editor);
3222 else
3223 error = apply_unveil(got_repo_get_path(repo), 0,
3224 got_worktree_get_root_path(worktree));
3225 if (error)
3226 goto done;
3228 cl_arg.editor = editor;
3229 cl_arg.cmdline_log = logmsg;
3230 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3231 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3232 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3233 cl_arg.branch_name += 5;
3234 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3235 cl_arg.branch_name += 6;
3236 cl_arg.repo_path = got_repo_get_path(repo);
3237 cl_arg.logmsg_path = NULL;
3238 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3239 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3240 if (error) {
3241 if (cl_arg.logmsg_path)
3242 fprintf(stderr, "%s: log message preserved in %s\n",
3243 getprogname(), cl_arg.logmsg_path);
3244 goto done;
3247 if (cl_arg.logmsg_path)
3248 unlink(cl_arg.logmsg_path);
3250 error = got_object_id_str(&id_str, id);
3251 if (error)
3252 goto done;
3253 printf("Created commit %s\n", id_str);
3254 done:
3255 if (repo)
3256 got_repo_close(repo);
3257 if (worktree)
3258 got_worktree_close(worktree);
3259 free(path);
3260 free(cwd);
3261 free(id_str);
3262 free(editor);
3263 return error;
3266 __dead static void
3267 usage_cherrypick(void)
3269 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3270 exit(1);
3273 static const struct got_error *
3274 cmd_cherrypick(int argc, char *argv[])
3276 const struct got_error *error = NULL;
3277 struct got_worktree *worktree = NULL;
3278 struct got_repository *repo = NULL;
3279 char *cwd = NULL, *commit_id_str = NULL;
3280 struct got_object_id *commit_id = NULL;
3281 struct got_commit_object *commit = NULL;
3282 struct got_object_qid *pid;
3283 struct got_reference *head_ref = NULL;
3284 int ch, did_something = 0;
3286 while ((ch = getopt(argc, argv, "")) != -1) {
3287 switch (ch) {
3288 default:
3289 usage_cherrypick();
3290 /* NOTREACHED */
3294 argc -= optind;
3295 argv += optind;
3297 #ifndef PROFILE
3298 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3299 "unveil", NULL) == -1)
3300 err(1, "pledge");
3301 #endif
3302 if (argc != 1)
3303 usage_cherrypick();
3305 cwd = getcwd(NULL, 0);
3306 if (cwd == NULL) {
3307 error = got_error_from_errno("getcwd");
3308 goto done;
3310 error = got_worktree_open(&worktree, cwd);
3311 if (error)
3312 goto done;
3314 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3315 if (error != NULL)
3316 goto done;
3318 error = apply_unveil(got_repo_get_path(repo), 0,
3319 got_worktree_get_root_path(worktree));
3320 if (error)
3321 goto done;
3323 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3324 GOT_OBJ_TYPE_COMMIT, repo);
3325 if (error != NULL) {
3326 struct got_reference *ref;
3327 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3328 goto done;
3329 error = got_ref_open(&ref, repo, argv[0], 0);
3330 if (error != NULL)
3331 goto done;
3332 error = got_ref_resolve(&commit_id, repo, ref);
3333 got_ref_close(ref);
3334 if (error != NULL)
3335 goto done;
3337 error = got_object_id_str(&commit_id_str, commit_id);
3338 if (error)
3339 goto done;
3341 error = got_ref_open(&head_ref, repo,
3342 got_worktree_get_head_ref_name(worktree), 0);
3343 if (error != NULL)
3344 goto done;
3346 error = check_same_branch(commit_id, head_ref, repo);
3347 if (error) {
3348 if (error->code != GOT_ERR_ANCESTRY)
3349 goto done;
3350 error = NULL;
3351 } else {
3352 error = got_error(GOT_ERR_SAME_BRANCH);
3353 goto done;
3356 error = got_object_open_as_commit(&commit, repo, commit_id);
3357 if (error)
3358 goto done;
3359 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3360 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3361 commit_id, repo, update_progress, &did_something, check_cancelled,
3362 NULL);
3363 if (error != NULL)
3364 goto done;
3366 if (did_something)
3367 printf("Merged commit %s\n", commit_id_str);
3368 done:
3369 if (commit)
3370 got_object_commit_close(commit);
3371 free(commit_id_str);
3372 if (head_ref)
3373 got_ref_close(head_ref);
3374 if (worktree)
3375 got_worktree_close(worktree);
3376 if (repo)
3377 got_repo_close(repo);
3378 return error;
3381 __dead static void
3382 usage_backout(void)
3384 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3385 exit(1);
3388 static const struct got_error *
3389 cmd_backout(int argc, char *argv[])
3391 const struct got_error *error = NULL;
3392 struct got_worktree *worktree = NULL;
3393 struct got_repository *repo = NULL;
3394 char *cwd = NULL, *commit_id_str = NULL;
3395 struct got_object_id *commit_id = NULL;
3396 struct got_commit_object *commit = NULL;
3397 struct got_object_qid *pid;
3398 struct got_reference *head_ref = NULL;
3399 int ch, did_something = 0;
3401 while ((ch = getopt(argc, argv, "")) != -1) {
3402 switch (ch) {
3403 default:
3404 usage_backout();
3405 /* NOTREACHED */
3409 argc -= optind;
3410 argv += optind;
3412 #ifndef PROFILE
3413 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3414 "unveil", NULL) == -1)
3415 err(1, "pledge");
3416 #endif
3417 if (argc != 1)
3418 usage_backout();
3420 cwd = getcwd(NULL, 0);
3421 if (cwd == NULL) {
3422 error = got_error_from_errno("getcwd");
3423 goto done;
3425 error = got_worktree_open(&worktree, cwd);
3426 if (error)
3427 goto done;
3429 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3430 if (error != NULL)
3431 goto done;
3433 error = apply_unveil(got_repo_get_path(repo), 0,
3434 got_worktree_get_root_path(worktree));
3435 if (error)
3436 goto done;
3438 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3439 GOT_OBJ_TYPE_COMMIT, repo);
3440 if (error != NULL) {
3441 struct got_reference *ref;
3442 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3443 goto done;
3444 error = got_ref_open(&ref, repo, argv[0], 0);
3445 if (error != NULL)
3446 goto done;
3447 error = got_ref_resolve(&commit_id, repo, ref);
3448 got_ref_close(ref);
3449 if (error != NULL)
3450 goto done;
3452 error = got_object_id_str(&commit_id_str, commit_id);
3453 if (error)
3454 goto done;
3456 error = got_ref_open(&head_ref, repo,
3457 got_worktree_get_head_ref_name(worktree), 0);
3458 if (error != NULL)
3459 goto done;
3461 error = check_same_branch(commit_id, head_ref, repo);
3462 if (error)
3463 goto done;
3465 error = got_object_open_as_commit(&commit, repo, commit_id);
3466 if (error)
3467 goto done;
3468 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3469 if (pid == NULL) {
3470 error = got_error(GOT_ERR_ROOT_COMMIT);
3471 goto done;
3474 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3475 update_progress, &did_something, check_cancelled, NULL);
3476 if (error != NULL)
3477 goto done;
3479 if (did_something)
3480 printf("Backed out commit %s\n", commit_id_str);
3481 done:
3482 if (commit)
3483 got_object_commit_close(commit);
3484 free(commit_id_str);
3485 if (head_ref)
3486 got_ref_close(head_ref);
3487 if (worktree)
3488 got_worktree_close(worktree);
3489 if (repo)
3490 got_repo_close(repo);
3491 return error;
3494 __dead static void
3495 usage_rebase(void)
3497 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3498 getprogname());
3499 exit(1);
3502 void
3503 trim_logmsg(char *logmsg, int limit)
3505 char *nl;
3506 size_t len;
3508 len = strlen(logmsg);
3509 if (len > limit)
3510 len = limit;
3511 logmsg[len] = '\0';
3512 nl = strchr(logmsg, '\n');
3513 if (nl)
3514 *nl = '\0';
3517 static const struct got_error *
3518 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3520 const char *logmsg0 = NULL;
3522 logmsg0 = got_object_commit_get_logmsg(commit);
3524 while (isspace((unsigned char)logmsg0[0]))
3525 logmsg0++;
3527 *logmsg = strdup(logmsg0);
3528 if (*logmsg == NULL)
3529 return got_error_from_errno("strdup");
3531 trim_logmsg(*logmsg, limit);
3532 return NULL;
3535 static const struct got_error *
3536 show_rebase_progress(struct got_commit_object *commit,
3537 struct got_object_id *old_id, struct got_object_id *new_id)
3539 const struct got_error *err;
3540 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3542 err = got_object_id_str(&old_id_str, old_id);
3543 if (err)
3544 goto done;
3546 if (new_id) {
3547 err = got_object_id_str(&new_id_str, new_id);
3548 if (err)
3549 goto done;
3552 old_id_str[12] = '\0';
3553 if (new_id_str)
3554 new_id_str[12] = '\0';
3556 err = get_short_logmsg(&logmsg, 42, commit);
3557 if (err)
3558 goto done;
3560 printf("%s -> %s: %s\n", old_id_str,
3561 new_id_str ? new_id_str : "no-op change", logmsg);
3562 done:
3563 free(old_id_str);
3564 free(new_id_str);
3565 return err;
3568 static const struct got_error *
3569 rebase_progress(void *arg, unsigned char status, const char *path)
3571 unsigned char *rebase_status = arg;
3573 while (path[0] == '/')
3574 path++;
3575 printf("%c %s\n", status, path);
3577 if (*rebase_status == GOT_STATUS_CONFLICT)
3578 return NULL;
3579 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3580 *rebase_status = status;
3581 return NULL;
3584 static const struct got_error *
3585 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3586 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3587 struct got_repository *repo)
3589 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3590 return got_worktree_rebase_complete(worktree,
3591 new_base_branch, tmp_branch, branch, repo);
3594 static const struct got_error *
3595 rebase_commit(struct got_pathlist_head *merged_paths,
3596 struct got_worktree *worktree, struct got_reference *tmp_branch,
3597 struct got_object_id *commit_id, struct got_repository *repo)
3599 const struct got_error *error;
3600 struct got_commit_object *commit;
3601 struct got_object_id *new_commit_id;
3603 error = got_object_open_as_commit(&commit, repo, commit_id);
3604 if (error)
3605 return error;
3607 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3608 worktree, tmp_branch, commit, commit_id, repo);
3609 if (error) {
3610 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3611 goto done;
3612 error = show_rebase_progress(commit, commit_id, NULL);
3613 } else {
3614 error = show_rebase_progress(commit, commit_id, new_commit_id);
3615 free(new_commit_id);
3617 done:
3618 got_object_commit_close(commit);
3619 return error;
3622 struct check_path_prefix_arg {
3623 const char *path_prefix;
3624 size_t len;
3627 static const struct got_error *
3628 check_path_prefix(void *arg, struct got_blob_object *blob1,
3629 struct got_blob_object *blob2, struct got_object_id *id1,
3630 struct got_object_id *id2, const char *path1, const char *path2,
3631 struct got_repository *repo)
3633 struct check_path_prefix_arg *a = arg;
3635 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3636 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3637 return got_error(GOT_ERR_REBASE_PATH);
3639 return NULL;
3642 static const struct got_error *
3643 rebase_check_path_prefix(struct got_object_id *parent_id,
3644 struct got_object_id *commit_id, const char *path_prefix,
3645 struct got_repository *repo)
3647 const struct got_error *err;
3648 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3649 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3650 struct check_path_prefix_arg cpp_arg;
3652 if (got_path_is_root_dir(path_prefix))
3653 return NULL;
3655 err = got_object_open_as_commit(&commit, repo, commit_id);
3656 if (err)
3657 goto done;
3659 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3660 if (err)
3661 goto done;
3663 err = got_object_open_as_tree(&tree1, repo,
3664 got_object_commit_get_tree_id(parent_commit));
3665 if (err)
3666 goto done;
3668 err = got_object_open_as_tree(&tree2, repo,
3669 got_object_commit_get_tree_id(commit));
3670 if (err)
3671 goto done;
3673 cpp_arg.path_prefix = path_prefix;
3674 cpp_arg.len = strlen(path_prefix);
3675 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3676 &cpp_arg);
3677 done:
3678 if (tree1)
3679 got_object_tree_close(tree1);
3680 if (tree2)
3681 got_object_tree_close(tree2);
3682 if (commit)
3683 got_object_commit_close(commit);
3684 if (parent_commit)
3685 got_object_commit_close(parent_commit);
3686 return err;
3689 static const struct got_error *
3690 collect_commits_to_rebase(struct got_object_id_queue *commits,
3691 struct got_object_id *initial_commit_id,
3692 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3693 const char *path_prefix, struct got_repository *repo)
3695 const struct got_error *err = NULL;
3696 struct got_commit_graph *graph = NULL;
3697 struct got_object_id *parent_id = NULL;
3698 struct got_object_qid *qid;
3699 struct got_object_id *commit_id = initial_commit_id;
3701 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3702 if (err)
3703 return err;
3705 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3706 if (err)
3707 goto done;
3708 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3709 err = got_commit_graph_iter_next(&parent_id, graph);
3710 if (err) {
3711 if (err->code == GOT_ERR_ITER_COMPLETED) {
3712 err = got_error_msg(GOT_ERR_ANCESTRY,
3713 "ran out of commits to rebase before "
3714 "youngest common ancestor commit has "
3715 "been reached?!?");
3716 goto done;
3717 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3718 goto done;
3719 err = got_commit_graph_fetch_commits(graph, 1, repo);
3720 if (err)
3721 goto done;
3722 } else {
3723 err = rebase_check_path_prefix(parent_id, commit_id,
3724 path_prefix, repo);
3725 if (err)
3726 goto done;
3728 err = got_object_qid_alloc(&qid, commit_id);
3729 if (err)
3730 goto done;
3731 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3732 commit_id = parent_id;
3735 done:
3736 got_commit_graph_close(graph);
3737 return err;
3740 static const struct got_error *
3741 cmd_rebase(int argc, char *argv[])
3743 const struct got_error *error = NULL;
3744 struct got_worktree *worktree = NULL;
3745 struct got_repository *repo = NULL;
3746 char *cwd = NULL;
3747 struct got_reference *branch = NULL;
3748 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3749 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3750 struct got_object_id *resume_commit_id = NULL;
3751 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3752 struct got_commit_object *commit = NULL;
3753 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3754 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3755 struct got_object_id_queue commits;
3756 struct got_pathlist_head merged_paths;
3757 const struct got_object_id_queue *parent_ids;
3758 struct got_object_qid *qid, *pid;
3760 SIMPLEQ_INIT(&commits);
3761 TAILQ_INIT(&merged_paths);
3763 while ((ch = getopt(argc, argv, "ac")) != -1) {
3764 switch (ch) {
3765 case 'a':
3766 abort_rebase = 1;
3767 break;
3768 case 'c':
3769 continue_rebase = 1;
3770 break;
3771 default:
3772 usage_rebase();
3773 /* NOTREACHED */
3777 argc -= optind;
3778 argv += optind;
3780 #ifndef PROFILE
3781 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3782 "unveil", NULL) == -1)
3783 err(1, "pledge");
3784 #endif
3785 if (abort_rebase && continue_rebase)
3786 usage_rebase();
3787 else if (abort_rebase || continue_rebase) {
3788 if (argc != 0)
3789 usage_rebase();
3790 } else if (argc != 1)
3791 usage_rebase();
3793 cwd = getcwd(NULL, 0);
3794 if (cwd == NULL) {
3795 error = got_error_from_errno("getcwd");
3796 goto done;
3798 error = got_worktree_open(&worktree, cwd);
3799 if (error)
3800 goto done;
3802 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3803 if (error != NULL)
3804 goto done;
3806 error = apply_unveil(got_repo_get_path(repo), 0,
3807 got_worktree_get_root_path(worktree));
3808 if (error)
3809 goto done;
3811 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3812 if (error)
3813 goto done;
3815 if (abort_rebase) {
3816 int did_something;
3817 if (!rebase_in_progress) {
3818 error = got_error(GOT_ERR_NOT_REBASING);
3819 goto done;
3821 error = got_worktree_rebase_continue(&resume_commit_id,
3822 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3823 if (error)
3824 goto done;
3825 printf("Switching work tree to %s\n",
3826 got_ref_get_symref_target(new_base_branch));
3827 error = got_worktree_rebase_abort(worktree, repo,
3828 new_base_branch, update_progress, &did_something);
3829 if (error)
3830 goto done;
3831 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3832 goto done; /* nothing else to do */
3835 if (continue_rebase) {
3836 if (!rebase_in_progress) {
3837 error = got_error(GOT_ERR_NOT_REBASING);
3838 goto done;
3840 error = got_worktree_rebase_continue(&resume_commit_id,
3841 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3842 if (error)
3843 goto done;
3845 error = rebase_commit(NULL, worktree, tmp_branch,
3846 resume_commit_id, repo);
3847 if (error)
3848 goto done;
3850 yca_id = got_object_id_dup(resume_commit_id);
3851 if (yca_id == NULL) {
3852 error = got_error_from_errno("got_object_id_dup");
3853 goto done;
3855 } else {
3856 error = got_ref_open(&branch, repo, argv[0], 0);
3857 if (error != NULL)
3858 goto done;
3860 error = check_same_branch(
3861 got_worktree_get_base_commit_id(worktree), branch, repo);
3862 if (error) {
3863 if (error->code != GOT_ERR_ANCESTRY)
3864 goto done;
3865 error = NULL;
3866 } else {
3867 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3868 "specified branch resolves to a commit which "
3869 "is already contained in work tree's branch");
3870 goto done;
3874 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3875 if (error)
3876 goto done;
3878 if (!continue_rebase) {
3879 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3880 got_worktree_get_base_commit_id(worktree),
3881 branch_head_commit_id, repo);
3882 if (error)
3883 goto done;
3884 if (yca_id == NULL) {
3885 error = got_error_msg(GOT_ERR_ANCESTRY,
3886 "specified branch shares no common ancestry "
3887 "with work tree's branch");
3888 goto done;
3891 error = got_worktree_rebase_prepare(&new_base_branch,
3892 &tmp_branch, worktree, branch, repo);
3893 if (error)
3894 goto done;
3897 commit_id = branch_head_commit_id;
3898 error = got_object_open_as_commit(&commit, repo, commit_id);
3899 if (error)
3900 goto done;
3902 parent_ids = got_object_commit_get_parent_ids(commit);
3903 pid = SIMPLEQ_FIRST(parent_ids);
3904 error = collect_commits_to_rebase(&commits, commit_id, pid->id,
3905 yca_id, got_worktree_get_path_prefix(worktree), repo);
3906 got_object_commit_close(commit);
3907 commit = NULL;
3908 if (error)
3909 goto done;
3911 if (SIMPLEQ_EMPTY(&commits)) {
3912 if (continue_rebase)
3913 error = rebase_complete(worktree, branch,
3914 new_base_branch, tmp_branch, repo);
3915 else
3916 error = got_error(GOT_ERR_EMPTY_REBASE);
3917 goto done;
3920 pid = NULL;
3921 SIMPLEQ_FOREACH(qid, &commits, entry) {
3922 commit_id = qid->id;
3923 parent_id = pid ? pid->id : yca_id;
3924 pid = qid;
3926 error = got_worktree_rebase_merge_files(&merged_paths,
3927 worktree, parent_id, commit_id, repo, rebase_progress,
3928 &rebase_status, check_cancelled, NULL);
3929 if (error)
3930 goto done;
3932 if (rebase_status == GOT_STATUS_CONFLICT) {
3933 got_worktree_rebase_pathlist_free(&merged_paths);
3934 break;
3937 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3938 commit_id, repo);
3939 got_worktree_rebase_pathlist_free(&merged_paths);
3940 if (error)
3941 goto done;
3944 if (rebase_status == GOT_STATUS_CONFLICT) {
3945 error = got_worktree_rebase_postpone(worktree);
3946 if (error)
3947 goto done;
3948 error = got_error_msg(GOT_ERR_CONFLICTS,
3949 "conflicts must be resolved before rebasing can continue");
3950 } else
3951 error = rebase_complete(worktree, branch, new_base_branch,
3952 tmp_branch, repo);
3953 done:
3954 got_object_id_queue_free(&commits);
3955 free(branch_head_commit_id);
3956 free(resume_commit_id);
3957 free(yca_id);
3958 if (commit)
3959 got_object_commit_close(commit);
3960 if (branch)
3961 got_ref_close(branch);
3962 if (new_base_branch)
3963 got_ref_close(new_base_branch);
3964 if (tmp_branch)
3965 got_ref_close(tmp_branch);
3966 if (worktree)
3967 got_worktree_close(worktree);
3968 if (repo)
3969 got_repo_close(repo);
3970 return error;
3973 __dead static void
3974 usage_histedit(void)
3976 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
3977 getprogname());
3978 exit(1);
3981 #define GOT_HISTEDIT_PICK 'p'
3982 #define GOT_HISTEDIT_EDIT 'e'
3983 #define GOT_HISTEDIT_FOLD 'f'
3984 #define GOT_HISTEDIT_DROP 'd'
3985 #define GOT_HISTEDIT_MESG 'm'
3987 static struct got_histedit_cmd {
3988 unsigned char code;
3989 const char *name;
3990 const char *desc;
3991 } got_histedit_cmds[] = {
3992 { GOT_HISTEDIT_PICK, "pick", "use commit" },
3993 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
3994 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
3995 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
3996 { GOT_HISTEDIT_MESG, "mesg",
3997 "single-line log message for commit above (open editor if empty)" },
4000 struct got_histedit_list_entry {
4001 TAILQ_ENTRY(got_histedit_list_entry) entry;
4002 struct got_object_id *commit_id;
4003 const struct got_histedit_cmd *cmd;
4004 char *logmsg;
4006 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4008 static const struct got_error *
4009 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4010 FILE *f, struct got_repository *repo)
4012 const struct got_error *err = NULL;
4013 char *logmsg = NULL, *id_str = NULL;
4014 struct got_commit_object *commit = NULL;
4015 size_t n;
4017 err = got_object_open_as_commit(&commit, repo, commit_id);
4018 if (err)
4019 goto done;
4021 err = get_short_logmsg(&logmsg, 34, commit);
4022 if (err)
4023 goto done;
4025 err = got_object_id_str(&id_str, commit_id);
4026 if (err)
4027 goto done;
4029 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4030 if (n < 0)
4031 err = got_ferror(f, GOT_ERR_IO);
4032 done:
4033 if (commit)
4034 got_object_commit_close(commit);
4035 free(id_str);
4036 free(logmsg);
4037 return err;
4040 static const struct got_error *
4041 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4042 struct got_repository *repo)
4044 const struct got_error *err = NULL;
4045 struct got_object_qid *qid;
4047 if (SIMPLEQ_EMPTY(commits))
4048 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4050 SIMPLEQ_FOREACH(qid, commits, entry) {
4051 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4052 f, repo);
4053 if (err)
4054 break;
4057 return err;
4060 static const struct got_error *
4061 write_cmd_list(FILE *f)
4063 const struct got_error *err = NULL;
4064 int n, i;
4066 n = fprintf(f, "# Available histedit commands:\n");
4067 if (n < 0)
4068 return got_ferror(f, GOT_ERR_IO);
4070 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4071 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4072 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4073 cmd->desc);
4074 if (n < 0) {
4075 err = got_ferror(f, GOT_ERR_IO);
4076 break;
4079 n = fprintf(f, "# Commits will be processed in order from top to "
4080 "bottom of this file.\n");
4081 if (n < 0)
4082 return got_ferror(f, GOT_ERR_IO);
4083 return err;
4086 static const struct got_error *
4087 histedit_syntax_error(int lineno)
4089 static char msg[42];
4090 int ret;
4092 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4093 lineno);
4094 if (ret == -1 || ret >= sizeof(msg))
4095 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4097 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4100 static const struct got_error *
4101 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4102 char *logmsg, struct got_repository *repo)
4104 const struct got_error *err;
4105 struct got_commit_object *folded_commit = NULL;
4106 char *id_str;
4108 err = got_object_id_str(&id_str, hle->commit_id);
4109 if (err)
4110 return err;
4112 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4113 if (err)
4114 goto done;
4116 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4117 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4118 got_object_commit_get_logmsg(folded_commit)) == -1) {
4119 err = got_error_from_errno("asprintf");
4120 goto done;
4122 done:
4123 if (folded_commit)
4124 got_object_commit_close(folded_commit);
4125 free(id_str);
4126 return err;
4129 static struct got_histedit_list_entry *
4130 get_folded_commits(struct got_histedit_list_entry *hle)
4132 struct got_histedit_list_entry *prev, *folded = NULL;
4134 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4135 while (prev && prev->cmd->code == GOT_HISTEDIT_FOLD) {
4136 folded = prev;
4137 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4140 return folded;
4143 static const struct got_error *
4144 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4145 struct got_repository *repo)
4147 char *logmsg_path = NULL, *id_str = NULL;
4148 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4149 const struct got_error *err = NULL;
4150 struct got_commit_object *commit = NULL;
4151 int fd;
4152 struct got_histedit_list_entry *folded = NULL;
4154 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4155 if (err)
4156 return err;
4158 folded = get_folded_commits(hle);
4159 if (folded) {
4160 while (folded != hle) {
4161 err = append_folded_commit_msg(&new_msg, folded,
4162 logmsg, repo);
4163 if (err)
4164 goto done;
4165 free(logmsg);
4166 logmsg = new_msg;
4167 folded = TAILQ_NEXT(folded, entry);
4171 err = got_object_id_str(&id_str, hle->commit_id);
4172 if (err)
4173 goto done;
4174 if (asprintf(&new_msg,
4175 "%s\n# original log message of commit %s: %s",
4176 logmsg ? logmsg : "", id_str,
4177 got_object_commit_get_logmsg(commit)) == -1) {
4178 err = got_error_from_errno("asprintf");
4179 goto done;
4181 free(logmsg);
4182 logmsg = new_msg;
4184 err = got_object_id_str(&id_str, hle->commit_id);
4185 if (err)
4186 goto done;
4188 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4189 if (err)
4190 goto done;
4192 dprintf(fd, logmsg);
4193 close(fd);
4195 err = get_editor(&editor);
4196 if (err)
4197 goto done;
4199 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4200 if (err) {
4201 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4202 goto done;
4203 err = NULL;
4204 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4205 if (hle->logmsg == NULL)
4206 err = got_error_from_errno("strdup");
4208 done:
4209 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4210 err = got_error_from_errno2("unlink", logmsg_path);
4211 free(logmsg_path);
4212 free(logmsg);
4213 free(editor);
4214 if (commit)
4215 got_object_commit_close(commit);
4216 return err;
4219 static const struct got_error *
4220 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4221 FILE *f, struct got_repository *repo)
4223 const struct got_error *err = NULL;
4224 char *line = NULL, *p, *end;
4225 size_t size;
4226 ssize_t len;
4227 int lineno = 0, i;
4228 const struct got_histedit_cmd *cmd;
4229 struct got_object_id *commit_id = NULL;
4230 struct got_histedit_list_entry *hle = NULL;
4232 for (;;) {
4233 len = getline(&line, &size, f);
4234 if (len == -1) {
4235 const struct got_error *getline_err;
4236 if (feof(f))
4237 break;
4238 getline_err = got_error_from_errno("getline");
4239 err = got_ferror(f, getline_err->code);
4240 break;
4242 lineno++;
4243 p = line;
4244 while (isspace((unsigned char)p[0]))
4245 p++;
4246 if (p[0] == '#' || p[0] == '\0') {
4247 free(line);
4248 line = NULL;
4249 continue;
4251 cmd = NULL;
4252 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4253 cmd = &got_histedit_cmds[i];
4254 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4255 isspace((unsigned char)p[strlen(cmd->name)])) {
4256 p += strlen(cmd->name);
4257 break;
4259 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4260 p++;
4261 break;
4264 if (cmd == NULL) {
4265 err = histedit_syntax_error(lineno);
4266 break;
4268 while (isspace((unsigned char)p[0]))
4269 p++;
4270 if (cmd->code == GOT_HISTEDIT_MESG) {
4271 if (hle == NULL || hle->logmsg != NULL) {
4272 err = got_error(GOT_ERR_HISTEDIT_CMD);
4273 break;
4275 if (p[0] == '\0') {
4276 err = histedit_edit_logmsg(hle, repo);
4277 if (err)
4278 break;
4279 } else {
4280 hle->logmsg = strdup(p);
4281 if (hle->logmsg == NULL) {
4282 err = got_error_from_errno("strdup");
4283 break;
4286 free(line);
4287 line = NULL;
4288 continue;
4289 } else {
4290 end = p;
4291 while (end[0] && !isspace((unsigned char)end[0]))
4292 end++;
4293 *end = '\0';
4295 err = got_object_resolve_id_str(&commit_id, repo, p);
4296 if (err) {
4297 /* override error code */
4298 err = histedit_syntax_error(lineno);
4299 break;
4302 hle = malloc(sizeof(*hle));
4303 if (hle == NULL) {
4304 err = got_error_from_errno("malloc");
4305 break;
4307 hle->cmd = cmd;
4308 hle->commit_id = commit_id;
4309 hle->logmsg = NULL;
4310 commit_id = NULL;
4311 free(line);
4312 line = NULL;
4313 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4316 free(line);
4317 free(commit_id);
4318 return err;
4321 static const struct got_error *
4322 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4323 const char *path, struct got_repository *repo)
4325 const struct got_error *err = NULL;
4326 char *editor;
4327 FILE *f = NULL;
4329 err = get_editor(&editor);
4330 if (err)
4331 return err;
4333 if (spawn_editor(editor, path) == -1) {
4334 err = got_error_from_errno("failed spawning editor");
4335 goto done;
4338 f = fopen(path, "r");
4339 if (f == NULL) {
4340 err = got_error_from_errno("fopen");
4341 goto done;
4343 err = histedit_parse_list(histedit_cmds, f, repo);
4344 done:
4345 if (f && fclose(f) != 0 && err == NULL)
4346 err = got_error_from_errno("fclose");
4347 free(editor);
4348 return err;
4351 static const struct got_error *
4352 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4353 struct got_object_id_queue *, const char *, struct got_repository *);
4355 static const struct got_error *
4356 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4357 struct got_object_id_queue *commits, struct got_repository *repo)
4359 const struct got_error *err;
4360 FILE *f = NULL;
4361 char *path = NULL;
4363 err = got_opentemp_named(&path, &f, "got-histedit");
4364 if (err)
4365 return err;
4367 err = write_cmd_list(f);
4368 if (err)
4369 goto done;
4371 err = histedit_write_commit_list(commits, f, repo);
4372 if (err)
4373 goto done;
4375 if (fclose(f) != 0) {
4376 err = got_error_from_errno("fclose");
4377 goto done;
4379 f = NULL;
4381 err = histedit_run_editor(histedit_cmds, path, repo);
4382 if (err) {
4383 const char *errmsg = err->msg;
4384 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4385 goto done;
4386 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4387 commits, path, repo);
4389 done:
4390 if (f && fclose(f) != 0 && err == NULL)
4391 err = got_error_from_errno("fclose");
4392 if (path && unlink(path) != 0 && err == NULL)
4393 err = got_error_from_errno2("unlink", path);
4394 free(path);
4395 return err;
4398 static const struct got_error *
4399 histedit_save_list(struct got_histedit_list *histedit_cmds,
4400 struct got_worktree *worktree, struct got_repository *repo)
4402 const struct got_error *err = NULL;
4403 char *path = NULL;
4404 FILE *f = NULL;
4405 struct got_histedit_list_entry *hle;
4406 struct got_commit_object *commit = NULL;
4408 err = got_worktree_get_histedit_list_path(&path, worktree);
4409 if (err)
4410 return err;
4412 f = fopen(path, "w");
4413 if (f == NULL) {
4414 err = got_error_from_errno2("fopen", path);
4415 goto done;
4417 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4418 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4419 repo);
4420 if (err)
4421 break;
4423 if (hle->logmsg) {
4424 int n = fprintf(f, "%c %s\n",
4425 GOT_HISTEDIT_MESG, hle->logmsg);
4426 if (n < 0) {
4427 err = got_ferror(f, GOT_ERR_IO);
4428 break;
4432 done:
4433 if (f && fclose(f) != 0 && err == NULL)
4434 err = got_error_from_errno("fclose");
4435 free(path);
4436 if (commit)
4437 got_object_commit_close(commit);
4438 return err;
4441 static const struct got_error *
4442 histedit_load_list(struct got_histedit_list *histedit_cmds,
4443 const char *path, struct got_repository *repo)
4445 const struct got_error *err = NULL;
4446 FILE *f = NULL;
4448 f = fopen(path, "r");
4449 if (f == NULL) {
4450 err = got_error_from_errno2("fopen", path);
4451 goto done;
4454 err = histedit_parse_list(histedit_cmds, f, repo);
4455 done:
4456 if (f && fclose(f) != 0 && err == NULL)
4457 err = got_error_from_errno("fclose");
4458 return err;
4461 static const struct got_error *
4462 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4463 const char *errmsg, struct got_object_id_queue *commits,
4464 const char *path, struct got_repository *repo)
4466 const struct got_error *err = NULL;
4467 int resp = ' ';
4469 while (resp != 'c' && resp != 'r' && resp != 'q') {
4470 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4471 "or (a)bort: ", getprogname(), errmsg);
4472 resp = getchar();
4473 switch (resp) {
4474 case 'c':
4475 err = histedit_run_editor(histedit_cmds, path, repo);
4476 break;
4477 case 'r':
4478 err = histedit_edit_script(histedit_cmds,
4479 commits, repo);
4480 break;
4481 case 'a':
4482 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4483 break;
4484 default:
4485 printf("invalid response '%c'\n", resp);
4486 break;
4490 return err;
4493 static const struct got_error *
4494 histedit_complete(struct got_worktree *worktree,
4495 struct got_reference *tmp_branch, struct got_reference *branch,
4496 struct got_repository *repo)
4498 printf("Switching work tree to %s\n",
4499 got_ref_get_symref_target(branch));
4500 return got_worktree_histedit_complete(worktree, tmp_branch, branch,
4501 repo);
4504 static const struct got_error *
4505 show_histedit_progress(struct got_commit_object *commit,
4506 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4508 const struct got_error *err;
4509 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4511 err = got_object_id_str(&old_id_str, hle->commit_id);
4512 if (err)
4513 goto done;
4515 if (new_id) {
4516 err = got_object_id_str(&new_id_str, new_id);
4517 if (err)
4518 goto done;
4521 old_id_str[12] = '\0';
4522 if (new_id_str)
4523 new_id_str[12] = '\0';
4525 if (hle->logmsg) {
4526 logmsg = strdup(hle->logmsg);
4527 if (logmsg == NULL) {
4528 err = got_error_from_errno("strdup");
4529 goto done;
4531 trim_logmsg(logmsg, 42);
4532 } else {
4533 err = get_short_logmsg(&logmsg, 42, commit);
4534 if (err)
4535 goto done;
4538 switch (hle->cmd->code) {
4539 case GOT_HISTEDIT_PICK:
4540 case GOT_HISTEDIT_EDIT:
4541 printf("%s -> %s: %s\n", old_id_str,
4542 new_id_str ? new_id_str : "no-op change", logmsg);
4543 break;
4544 case GOT_HISTEDIT_DROP:
4545 case GOT_HISTEDIT_FOLD:
4546 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4547 logmsg);
4548 break;
4549 default:
4550 break;
4553 done:
4554 free(old_id_str);
4555 free(new_id_str);
4556 return err;
4559 static const struct got_error *
4560 histedit_commit(struct got_pathlist_head *merged_paths,
4561 struct got_worktree *worktree, struct got_reference *tmp_branch,
4562 struct got_histedit_list_entry *hle, struct got_repository *repo)
4564 const struct got_error *err;
4565 struct got_commit_object *commit;
4566 struct got_object_id *new_commit_id;
4568 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4569 && hle->logmsg == NULL) {
4570 err = histedit_edit_logmsg(hle, repo);
4571 if (err)
4572 return err;
4575 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4576 if (err)
4577 return err;
4579 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4580 worktree, tmp_branch, commit, hle->commit_id, hle->logmsg, repo);
4581 if (err) {
4582 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4583 goto done;
4584 err = show_histedit_progress(commit, hle, NULL);
4585 } else {
4586 err = show_histedit_progress(commit, hle, new_commit_id);
4587 free(new_commit_id);
4589 done:
4590 got_object_commit_close(commit);
4591 return err;
4594 static const struct got_error *
4595 histedit_skip_commit(struct got_histedit_list_entry *hle,
4596 struct got_worktree *worktree, struct got_repository *repo)
4598 const struct got_error *error;
4599 struct got_commit_object *commit;
4601 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4602 repo);
4603 if (error)
4604 return error;
4606 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4607 if (error)
4608 return error;
4610 error = show_histedit_progress(commit, hle, NULL);
4611 got_object_commit_close(commit);
4612 return error;
4615 static const struct got_error *
4616 histedit_check_script(struct got_histedit_list *histedit_cmds,
4617 struct got_object_id_queue *commits, struct got_repository *repo)
4619 const struct got_error *err = NULL;
4620 struct got_object_qid *qid;
4621 struct got_histedit_list_entry *hle;
4622 static char msg[80];
4623 char *id_str;
4625 if (TAILQ_EMPTY(histedit_cmds))
4626 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4628 SIMPLEQ_FOREACH(qid, commits, entry) {
4629 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4630 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4631 break;
4633 if (hle == NULL) {
4634 err = got_object_id_str(&id_str, qid->id);
4635 if (err)
4636 return err;
4637 snprintf(msg, sizeof(msg),
4638 "commit %s missing from histedit script", id_str);
4639 free(id_str);
4640 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4644 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4645 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4646 "last commit in histedit script cannot be folded");
4648 return NULL;
4651 static const struct got_error *
4652 cmd_histedit(int argc, char *argv[])
4654 const struct got_error *error = NULL;
4655 struct got_worktree *worktree = NULL;
4656 struct got_repository *repo = NULL;
4657 char *cwd = NULL;
4658 struct got_reference *branch = NULL;
4659 struct got_reference *tmp_branch = NULL;
4660 struct got_object_id *resume_commit_id = NULL;
4661 struct got_object_id *base_commit_id = NULL;
4662 struct got_object_id *head_commit_id = NULL;
4663 struct got_commit_object *commit = NULL;
4664 int ch, rebase_in_progress = 0;
4665 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4666 const char *edit_script_path = NULL;
4667 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4668 struct got_object_id_queue commits;
4669 struct got_pathlist_head merged_paths;
4670 const struct got_object_id_queue *parent_ids;
4671 struct got_object_qid *pid;
4672 struct got_histedit_list histedit_cmds;
4673 struct got_histedit_list_entry *hle;
4675 SIMPLEQ_INIT(&commits);
4676 TAILQ_INIT(&histedit_cmds);
4677 TAILQ_INIT(&merged_paths);
4679 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4680 switch (ch) {
4681 case 'a':
4682 abort_edit = 1;
4683 break;
4684 case 'c':
4685 continue_edit = 1;
4686 break;
4687 case 'F':
4688 edit_script_path = optarg;
4689 break;
4690 default:
4691 usage_histedit();
4692 /* NOTREACHED */
4696 argc -= optind;
4697 argv += optind;
4699 #ifndef PROFILE
4700 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4701 "unveil", NULL) == -1)
4702 err(1, "pledge");
4703 #endif
4704 if (abort_edit && continue_edit)
4705 usage_histedit();
4706 if (argc != 0)
4707 usage_histedit();
4710 * This command cannot apply unveil(2) in all cases because the
4711 * user may choose to run an editor to edit the histedit script
4712 * and to edit individual commit log messages.
4713 * unveil(2) traverses exec(2); if an editor is used we have to
4714 * apply unveil after edit script and log messages have been written.
4715 * XXX TODO: Make use of unveil(2) where possible.
4718 cwd = getcwd(NULL, 0);
4719 if (cwd == NULL) {
4720 error = got_error_from_errno("getcwd");
4721 goto done;
4723 error = got_worktree_open(&worktree, cwd);
4724 if (error)
4725 goto done;
4727 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4728 if (error != NULL)
4729 goto done;
4731 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4732 if (error)
4733 goto done;
4734 if (rebase_in_progress) {
4735 error = got_error(GOT_ERR_REBASING);
4736 goto done;
4739 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4740 if (error)
4741 goto done;
4743 if (edit_in_progress && abort_edit) {
4744 int did_something;
4745 error = got_worktree_histedit_continue(&resume_commit_id,
4746 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4747 if (error)
4748 goto done;
4749 printf("Switching work tree to %s\n",
4750 got_ref_get_symref_target(branch));
4751 error = got_worktree_histedit_abort(worktree, repo,
4752 branch, base_commit_id, update_progress, &did_something);
4753 if (error)
4754 goto done;
4755 printf("Histedit of %s aborted\n",
4756 got_ref_get_symref_target(branch));
4757 goto done; /* nothing else to do */
4758 } else if (abort_edit) {
4759 error = got_error(GOT_ERR_NOT_HISTEDIT);
4760 goto done;
4763 if (continue_edit) {
4764 char *path;
4766 if (!edit_in_progress) {
4767 error = got_error(GOT_ERR_NOT_HISTEDIT);
4768 goto done;
4771 error = got_worktree_get_histedit_list_path(&path, worktree);
4772 if (error)
4773 goto done;
4775 error = histedit_load_list(&histedit_cmds, path, repo);
4776 free(path);
4777 if (error)
4778 goto done;
4780 error = got_worktree_histedit_continue(&resume_commit_id,
4781 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4782 if (error)
4783 goto done;
4785 error = got_ref_resolve(&head_commit_id, repo, branch);
4786 if (error)
4787 goto done;
4789 error = got_object_open_as_commit(&commit, repo,
4790 head_commit_id);
4791 if (error)
4792 goto done;
4793 parent_ids = got_object_commit_get_parent_ids(commit);
4794 pid = SIMPLEQ_FIRST(parent_ids);
4795 error = collect_commits_to_rebase(&commits, head_commit_id,
4796 pid->id, base_commit_id,
4797 got_worktree_get_path_prefix(worktree), repo);
4798 got_object_commit_close(commit);
4799 commit = NULL;
4800 if (error)
4801 goto done;
4802 } else {
4803 if (edit_in_progress) {
4804 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4805 goto done;
4808 error = got_ref_open(&branch, repo,
4809 got_worktree_get_head_ref_name(worktree), 0);
4810 if (error != NULL)
4811 goto done;
4813 error = got_ref_resolve(&head_commit_id, repo, branch);
4814 if (error)
4815 goto done;
4817 error = got_object_open_as_commit(&commit, repo,
4818 head_commit_id);
4819 if (error)
4820 goto done;
4821 parent_ids = got_object_commit_get_parent_ids(commit);
4822 pid = SIMPLEQ_FIRST(parent_ids);
4823 error = collect_commits_to_rebase(&commits, head_commit_id,
4824 pid->id, got_worktree_get_base_commit_id(worktree),
4825 got_worktree_get_path_prefix(worktree), repo);
4826 got_object_commit_close(commit);
4827 commit = NULL;
4828 if (error)
4829 goto done;
4831 if (edit_script_path) {
4832 error = histedit_load_list(&histedit_cmds,
4833 edit_script_path, repo);
4834 if (error)
4835 goto done;
4836 } else {
4837 error = histedit_edit_script(&histedit_cmds, &commits,
4838 repo);
4839 if (error)
4840 goto done;
4844 error = histedit_save_list(&histedit_cmds, worktree,
4845 repo);
4846 if (error)
4847 goto done;
4849 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4850 &base_commit_id, worktree, repo);
4851 if (error)
4852 goto done;
4856 error = histedit_check_script(&histedit_cmds, &commits, repo);
4857 if (error)
4858 goto done;
4860 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4861 if (resume_commit_id) {
4862 if (got_object_id_cmp(hle->commit_id,
4863 resume_commit_id) != 0)
4864 continue;
4866 resume_commit_id = NULL;
4867 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4868 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4869 error = histedit_skip_commit(hle, worktree,
4870 repo);
4871 } else {
4872 error = histedit_commit(NULL, worktree,
4873 tmp_branch, hle, repo);
4875 if (error)
4876 goto done;
4877 continue;
4880 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4881 error = histedit_skip_commit(hle, worktree, repo);
4882 if (error)
4883 goto done;
4884 continue;
4887 error = got_object_open_as_commit(&commit, repo,
4888 hle->commit_id);
4889 if (error)
4890 goto done;
4891 parent_ids = got_object_commit_get_parent_ids(commit);
4892 pid = SIMPLEQ_FIRST(parent_ids);
4894 error = got_worktree_histedit_merge_files(&merged_paths,
4895 worktree, pid->id, hle->commit_id, repo, rebase_progress,
4896 &rebase_status, check_cancelled, NULL);
4897 if (error)
4898 goto done;
4899 got_object_commit_close(commit);
4900 commit = NULL;
4902 if (rebase_status == GOT_STATUS_CONFLICT) {
4903 got_worktree_rebase_pathlist_free(&merged_paths);
4904 break;
4907 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4908 char *id_str;
4909 error = got_object_id_str(&id_str, hle->commit_id);
4910 if (error)
4911 goto done;
4912 printf("Stopping histedit for amending commit %s\n",
4913 id_str);
4914 free(id_str);
4915 got_worktree_rebase_pathlist_free(&merged_paths);
4916 error = got_worktree_histedit_postpone(worktree);
4917 goto done;
4920 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4921 error = histedit_skip_commit(hle, worktree, repo);
4922 if (error)
4923 goto done;
4924 continue;
4927 error = histedit_commit(&merged_paths, worktree, tmp_branch,
4928 hle, repo);
4929 got_worktree_rebase_pathlist_free(&merged_paths);
4930 if (error)
4931 goto done;
4934 if (rebase_status == GOT_STATUS_CONFLICT) {
4935 error = got_worktree_histedit_postpone(worktree);
4936 if (error)
4937 goto done;
4938 error = got_error_msg(GOT_ERR_CONFLICTS,
4939 "conflicts must be resolved before rebasing can continue");
4940 } else
4941 error = histedit_complete(worktree, tmp_branch, branch, repo);
4942 done:
4943 got_object_id_queue_free(&commits);
4944 free(head_commit_id);
4945 free(base_commit_id);
4946 free(resume_commit_id);
4947 if (commit)
4948 got_object_commit_close(commit);
4949 if (branch)
4950 got_ref_close(branch);
4951 if (tmp_branch)
4952 got_ref_close(tmp_branch);
4953 if (worktree)
4954 got_worktree_close(worktree);
4955 if (repo)
4956 got_repo_close(repo);
4957 return error;