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 resolve_commit_arg(struct got_object_id **commit_id,
763 const char *commit_id_arg, struct got_repository *repo)
765 const struct got_error *err;
766 struct got_reference *ref;
768 err = got_ref_open(&ref, repo, commit_id_arg, 0);
769 if (err == NULL) {
770 err = got_ref_resolve(commit_id, repo, ref);
771 got_ref_close(ref);
772 } else {
773 if (err->code != GOT_ERR_NOT_REF)
774 return err;
775 err = got_repo_match_object_id_prefix(commit_id,
776 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
778 return err;
781 static const struct got_error *
782 cmd_checkout(int argc, char *argv[])
784 const struct got_error *error = NULL;
785 struct got_repository *repo = NULL;
786 struct got_reference *head_ref = NULL;
787 struct got_worktree *worktree = NULL;
788 char *repo_path = NULL;
789 char *worktree_path = NULL;
790 const char *path_prefix = "";
791 const char *branch_name = GOT_REF_HEAD;
792 char *commit_id_str = NULL;
793 int ch, same_path_prefix;
795 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
796 switch (ch) {
797 case 'b':
798 branch_name = optarg;
799 break;
800 case 'c':
801 commit_id_str = strdup(optarg);
802 if (commit_id_str == NULL)
803 return got_error_from_errno("strdup");
804 break;
805 case 'p':
806 path_prefix = optarg;
807 break;
808 default:
809 usage_checkout();
810 /* NOTREACHED */
814 argc -= optind;
815 argv += optind;
817 #ifndef PROFILE
818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
819 "unveil", NULL) == -1)
820 err(1, "pledge");
821 #endif
822 if (argc == 1) {
823 char *cwd, *base, *dotgit;
824 repo_path = realpath(argv[0], NULL);
825 if (repo_path == NULL)
826 return got_error_from_errno2("realpath", argv[0]);
827 cwd = getcwd(NULL, 0);
828 if (cwd == NULL) {
829 error = got_error_from_errno("getcwd");
830 goto done;
832 if (path_prefix[0]) {
833 base = basename(path_prefix);
834 if (base == NULL) {
835 error = got_error_from_errno2("basename",
836 path_prefix);
837 goto done;
839 } else {
840 base = basename(repo_path);
841 if (base == NULL) {
842 error = got_error_from_errno2("basename",
843 repo_path);
844 goto done;
847 dotgit = strstr(base, ".git");
848 if (dotgit)
849 *dotgit = '\0';
850 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
851 error = got_error_from_errno("asprintf");
852 free(cwd);
853 goto done;
855 free(cwd);
856 } else if (argc == 2) {
857 repo_path = realpath(argv[0], NULL);
858 if (repo_path == NULL) {
859 error = got_error_from_errno2("realpath", argv[0]);
860 goto done;
862 worktree_path = realpath(argv[1], NULL);
863 if (worktree_path == NULL) {
864 if (errno != ENOENT) {
865 error = got_error_from_errno2("realpath",
866 argv[1]);
867 goto done;
869 worktree_path = strdup(argv[1]);
870 if (worktree_path == NULL) {
871 error = got_error_from_errno("strdup");
872 goto done;
875 } else
876 usage_checkout();
878 got_path_strip_trailing_slashes(repo_path);
879 got_path_strip_trailing_slashes(worktree_path);
881 error = got_repo_open(&repo, repo_path);
882 if (error != NULL)
883 goto done;
885 /* Pre-create work tree path for unveil(2) */
886 error = got_path_mkdir(worktree_path);
887 if (error) {
888 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
889 goto done;
890 if (!got_path_dir_is_empty(worktree_path)) {
891 error = got_error_path(worktree_path,
892 GOT_ERR_DIR_NOT_EMPTY);
893 goto done;
897 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
898 if (error)
899 goto done;
901 error = got_ref_open(&head_ref, repo, branch_name, 0);
902 if (error != NULL)
903 goto done;
905 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
906 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
907 goto done;
909 error = got_worktree_open(&worktree, worktree_path);
910 if (error != NULL)
911 goto done;
913 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
914 path_prefix);
915 if (error != NULL)
916 goto done;
917 if (!same_path_prefix) {
918 error = got_error(GOT_ERR_PATH_PREFIX);
919 goto done;
922 if (commit_id_str) {
923 struct got_object_id *commit_id;
924 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
925 if (error)
926 goto done;
927 error = check_linear_ancestry(commit_id,
928 got_worktree_get_base_commit_id(worktree), repo);
929 if (error != NULL) {
930 free(commit_id);
931 goto done;
933 error = check_same_branch(commit_id, head_ref, repo);
934 if (error)
935 goto done;
936 error = got_worktree_set_base_commit_id(worktree, repo,
937 commit_id);
938 free(commit_id);
939 if (error)
940 goto done;
943 error = got_worktree_checkout_files(worktree, "", repo,
944 checkout_progress, worktree_path, check_cancelled, NULL);
945 if (error != NULL)
946 goto done;
948 printf("Now shut up and hack\n");
950 done:
951 free(commit_id_str);
952 free(repo_path);
953 free(worktree_path);
954 return error;
957 __dead static void
958 usage_update(void)
960 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
961 getprogname());
962 exit(1);
965 static const struct got_error *
966 update_progress(void *arg, unsigned char status, const char *path)
968 int *did_something = arg;
970 if (status == GOT_STATUS_EXISTS)
971 return NULL;
973 *did_something = 1;
975 /* Base commit bump happens silently. */
976 if (status == GOT_STATUS_BUMP_BASE)
977 return NULL;
979 while (path[0] == '/')
980 path++;
981 printf("%c %s\n", status, path);
982 return NULL;
985 static const struct got_error *
986 switch_head_ref(struct got_reference *head_ref,
987 struct got_object_id *commit_id, struct got_worktree *worktree,
988 struct got_repository *repo)
990 const struct got_error *err = NULL;
991 char *base_id_str;
992 int ref_has_moved = 0;
994 /* Trivial case: switching between two different references. */
995 if (strcmp(got_ref_get_name(head_ref),
996 got_worktree_get_head_ref_name(worktree)) != 0) {
997 printf("Switching work tree from %s to %s\n",
998 got_worktree_get_head_ref_name(worktree),
999 got_ref_get_name(head_ref));
1000 return got_worktree_set_head_ref(worktree, head_ref);
1003 err = check_linear_ancestry(commit_id,
1004 got_worktree_get_base_commit_id(worktree), repo);
1005 if (err) {
1006 if (err->code != GOT_ERR_ANCESTRY)
1007 return err;
1008 ref_has_moved = 1;
1010 if (!ref_has_moved)
1011 return NULL;
1013 /* Switching to a rebased branch with the same reference name. */
1014 err = got_object_id_str(&base_id_str,
1015 got_worktree_get_base_commit_id(worktree));
1016 if (err)
1017 return err;
1018 printf("Reference %s now points at a different branch\n",
1019 got_worktree_get_head_ref_name(worktree));
1020 printf("Switching work tree from %s to %s\n", base_id_str,
1021 got_worktree_get_head_ref_name(worktree));
1022 return NULL;
1025 static const struct got_error *
1026 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1028 const struct got_error *err;
1029 int in_progress;
1031 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1032 if (err)
1033 return err;
1034 if (in_progress)
1035 return got_error(GOT_ERR_REBASING);
1037 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1038 if (err)
1039 return err;
1040 if (in_progress)
1041 return got_error(GOT_ERR_HISTEDIT_BUSY);
1043 return NULL;
1046 static const struct got_error *
1047 cmd_update(int argc, char *argv[])
1049 const struct got_error *error = NULL;
1050 struct got_repository *repo = NULL;
1051 struct got_worktree *worktree = NULL;
1052 char *worktree_path = NULL, *path = NULL;
1053 struct got_object_id *commit_id = NULL;
1054 char *commit_id_str = NULL;
1055 const char *branch_name = NULL;
1056 struct got_reference *head_ref = NULL;
1057 int ch, did_something = 0;
1059 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1060 switch (ch) {
1061 case 'b':
1062 branch_name = optarg;
1063 break;
1064 case 'c':
1065 commit_id_str = strdup(optarg);
1066 if (commit_id_str == NULL)
1067 return got_error_from_errno("strdup");
1068 break;
1069 default:
1070 usage_update();
1071 /* NOTREACHED */
1075 argc -= optind;
1076 argv += optind;
1078 #ifndef PROFILE
1079 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1080 "unveil", NULL) == -1)
1081 err(1, "pledge");
1082 #endif
1083 worktree_path = getcwd(NULL, 0);
1084 if (worktree_path == NULL) {
1085 error = got_error_from_errno("getcwd");
1086 goto done;
1088 error = got_worktree_open(&worktree, worktree_path);
1089 if (error)
1090 goto done;
1092 error = check_rebase_or_histedit_in_progress(worktree);
1093 if (error)
1094 goto done;
1096 if (argc == 0) {
1097 path = strdup("");
1098 if (path == NULL) {
1099 error = got_error_from_errno("strdup");
1100 goto done;
1102 } else if (argc == 1) {
1103 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1104 if (error)
1105 goto done;
1106 } else
1107 usage_update();
1109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1110 if (error != NULL)
1111 goto done;
1113 error = apply_unveil(got_repo_get_path(repo), 0,
1114 got_worktree_get_root_path(worktree));
1115 if (error)
1116 goto done;
1118 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1119 got_worktree_get_head_ref_name(worktree), 0);
1120 if (error != NULL)
1121 goto done;
1122 if (commit_id_str == NULL) {
1123 error = got_ref_resolve(&commit_id, repo, head_ref);
1124 if (error != NULL)
1125 goto done;
1126 error = got_object_id_str(&commit_id_str, commit_id);
1127 if (error != NULL)
1128 goto done;
1129 } else {
1130 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1131 free(commit_id_str);
1132 commit_id_str = NULL;
1133 if (error)
1134 goto done;
1135 error = got_object_id_str(&commit_id_str, commit_id);
1136 if (error)
1137 goto done;
1140 if (branch_name) {
1141 struct got_object_id *head_commit_id;
1142 if (strlen(path) != 0) {
1143 fprintf(stderr, "%s: switching between branches "
1144 "requires that the entire work tree "
1145 "gets updated, not just '%s'\n",
1146 getprogname(), path);
1147 error = got_error(GOT_ERR_BAD_PATH);
1148 goto done;
1150 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1151 if (error)
1152 goto done;
1153 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1154 free(head_commit_id);
1155 if (error != NULL)
1156 goto done;
1157 error = check_same_branch(commit_id, head_ref, repo);
1158 if (error)
1159 goto done;
1160 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1161 if (error)
1162 goto done;
1163 } else {
1164 error = check_linear_ancestry(commit_id,
1165 got_worktree_get_base_commit_id(worktree), repo);
1166 if (error != NULL) {
1167 if (error->code == GOT_ERR_ANCESTRY)
1168 error = got_error(GOT_ERR_BRANCH_MOVED);
1169 goto done;
1171 error = check_same_branch(commit_id, head_ref, repo);
1172 if (error)
1173 goto done;
1176 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1177 commit_id) != 0) {
1178 error = got_worktree_set_base_commit_id(worktree, repo,
1179 commit_id);
1180 if (error)
1181 goto done;
1184 error = got_worktree_checkout_files(worktree, path, repo,
1185 update_progress, &did_something, check_cancelled, NULL);
1186 if (error != NULL)
1187 goto done;
1189 if (did_something)
1190 printf("Updated to commit %s\n", commit_id_str);
1191 else
1192 printf("Already up-to-date\n");
1193 done:
1194 free(worktree_path);
1195 free(path);
1196 free(commit_id);
1197 free(commit_id_str);
1198 return error;
1201 static const struct got_error *
1202 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1203 int diff_context, struct got_repository *repo)
1205 const struct got_error *err = NULL;
1206 struct got_tree_object *tree1 = NULL, *tree2;
1207 struct got_object_qid *qid;
1208 char *id_str1 = NULL, *id_str2;
1209 struct got_diff_blob_output_unidiff_arg arg;
1211 err = got_object_open_as_tree(&tree2, repo,
1212 got_object_commit_get_tree_id(commit));
1213 if (err)
1214 return err;
1216 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1217 if (qid != NULL) {
1218 struct got_commit_object *pcommit;
1220 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1221 if (err)
1222 return err;
1224 err = got_object_open_as_tree(&tree1, repo,
1225 got_object_commit_get_tree_id(pcommit));
1226 got_object_commit_close(pcommit);
1227 if (err)
1228 return err;
1230 err = got_object_id_str(&id_str1, qid->id);
1231 if (err)
1232 return err;
1235 err = got_object_id_str(&id_str2, id);
1236 if (err)
1237 goto done;
1239 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1240 arg.diff_context = diff_context;
1241 arg.outfile = stdout;
1242 err = got_diff_tree(tree1, tree2, "", "", repo,
1243 got_diff_blob_output_unidiff, &arg);
1244 done:
1245 if (tree1)
1246 got_object_tree_close(tree1);
1247 got_object_tree_close(tree2);
1248 free(id_str1);
1249 free(id_str2);
1250 return err;
1253 static char *
1254 get_datestr(time_t *time, char *datebuf)
1256 char *p, *s = ctime_r(time, datebuf);
1257 p = strchr(s, '\n');
1258 if (p)
1259 *p = '\0';
1260 return s;
1263 static const struct got_error *
1264 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1265 struct got_repository *repo, int show_patch, int diff_context,
1266 struct got_reflist_head *refs)
1268 const struct got_error *err = NULL;
1269 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1270 char datebuf[26];
1271 time_t committer_time;
1272 const char *author, *committer;
1273 char *refs_str = NULL;
1274 struct got_reflist_entry *re;
1276 SIMPLEQ_FOREACH(re, refs, entry) {
1277 char *s;
1278 const char *name;
1279 if (got_object_id_cmp(re->id, id) != 0)
1280 continue;
1281 name = got_ref_get_name(re->ref);
1282 if (strcmp(name, GOT_REF_HEAD) == 0)
1283 continue;
1284 if (strncmp(name, "refs/", 5) == 0)
1285 name += 5;
1286 if (strncmp(name, "got/", 4) == 0)
1287 continue;
1288 if (strncmp(name, "heads/", 6) == 0)
1289 name += 6;
1290 if (strncmp(name, "remotes/", 8) == 0)
1291 name += 8;
1292 s = refs_str;
1293 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1294 name) == -1) {
1295 err = got_error_from_errno("asprintf");
1296 free(s);
1297 break;
1299 free(s);
1301 err = got_object_id_str(&id_str, id);
1302 if (err)
1303 return err;
1305 printf("-----------------------------------------------\n");
1306 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1307 refs_str ? refs_str : "", refs_str ? ")" : "");
1308 free(id_str);
1309 id_str = NULL;
1310 free(refs_str);
1311 refs_str = NULL;
1312 printf("from: %s\n", got_object_commit_get_author(commit));
1313 committer_time = got_object_commit_get_committer_time(commit);
1314 datestr = get_datestr(&committer_time, datebuf);
1315 printf("date: %s UTC\n", datestr);
1316 author = got_object_commit_get_author(commit);
1317 committer = got_object_commit_get_committer(commit);
1318 if (strcmp(author, committer) != 0)
1319 printf("via: %s\n", committer);
1320 if (got_object_commit_get_nparents(commit) > 1) {
1321 const struct got_object_id_queue *parent_ids;
1322 struct got_object_qid *qid;
1323 int n = 1;
1324 parent_ids = got_object_commit_get_parent_ids(commit);
1325 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1326 err = got_object_id_str(&id_str, qid->id);
1327 if (err)
1328 return err;
1329 printf("parent %d: %s\n", n++, id_str);
1330 free(id_str);
1334 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1335 if (logmsg0 == NULL)
1336 return got_error_from_errno("strdup");
1338 logmsg = logmsg0;
1339 do {
1340 line = strsep(&logmsg, "\n");
1341 if (line)
1342 printf(" %s\n", line);
1343 } while (line);
1344 free(logmsg0);
1346 if (show_patch) {
1347 err = print_patch(commit, id, diff_context, repo);
1348 if (err == 0)
1349 printf("\n");
1352 if (fflush(stdout) != 0 && err == NULL)
1353 err = got_error_from_errno("fflush");
1354 return err;
1357 static const struct got_error *
1358 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1359 char *path, int show_patch, int diff_context, int limit,
1360 int first_parent_traversal, struct got_reflist_head *refs)
1362 const struct got_error *err;
1363 struct got_commit_graph *graph;
1365 err = got_commit_graph_open(&graph, root_id, path,
1366 first_parent_traversal, repo);
1367 if (err)
1368 return err;
1369 err = got_commit_graph_iter_start(graph, root_id, repo);
1370 if (err)
1371 goto done;
1372 for (;;) {
1373 struct got_commit_object *commit;
1374 struct got_object_id *id;
1376 if (sigint_received || sigpipe_received)
1377 break;
1379 err = got_commit_graph_iter_next(&id, graph);
1380 if (err) {
1381 if (err->code == GOT_ERR_ITER_COMPLETED) {
1382 err = NULL;
1383 break;
1385 if (err->code != GOT_ERR_ITER_NEED_MORE)
1386 break;
1387 err = got_commit_graph_fetch_commits(graph, 1, repo);
1388 if (err)
1389 break;
1390 else
1391 continue;
1393 if (id == NULL)
1394 break;
1396 err = got_object_open_as_commit(&commit, repo, id);
1397 if (err)
1398 break;
1399 err = print_commit(commit, id, repo, show_patch, diff_context,
1400 refs);
1401 got_object_commit_close(commit);
1402 if (err || (limit && --limit == 0))
1403 break;
1405 done:
1406 got_commit_graph_close(graph);
1407 return err;
1410 __dead static void
1411 usage_log(void)
1413 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1414 "[-r repository-path] [path]\n", getprogname());
1415 exit(1);
1418 static const struct got_error *
1419 cmd_log(int argc, char *argv[])
1421 const struct got_error *error;
1422 struct got_repository *repo = NULL;
1423 struct got_worktree *worktree = NULL;
1424 struct got_commit_object *commit = NULL;
1425 struct got_object_id *id = NULL;
1426 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1427 char *start_commit = NULL;
1428 int diff_context = 3, ch;
1429 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1430 const char *errstr;
1431 struct got_reflist_head refs;
1433 SIMPLEQ_INIT(&refs);
1435 #ifndef PROFILE
1436 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1437 NULL)
1438 == -1)
1439 err(1, "pledge");
1440 #endif
1442 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1443 switch (ch) {
1444 case 'p':
1445 show_patch = 1;
1446 break;
1447 case 'c':
1448 start_commit = optarg;
1449 break;
1450 case 'C':
1451 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1452 &errstr);
1453 if (errstr != NULL)
1454 err(1, "-C option %s", errstr);
1455 break;
1456 case 'l':
1457 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1458 if (errstr != NULL)
1459 err(1, "-l option %s", errstr);
1460 break;
1461 case 'f':
1462 first_parent_traversal = 1;
1463 break;
1464 case 'r':
1465 repo_path = realpath(optarg, NULL);
1466 if (repo_path == NULL)
1467 err(1, "-r option");
1468 got_path_strip_trailing_slashes(repo_path);
1469 break;
1470 default:
1471 usage_log();
1472 /* NOTREACHED */
1476 argc -= optind;
1477 argv += optind;
1479 cwd = getcwd(NULL, 0);
1480 if (cwd == NULL) {
1481 error = got_error_from_errno("getcwd");
1482 goto done;
1485 error = got_worktree_open(&worktree, cwd);
1486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1487 goto done;
1488 error = NULL;
1490 if (argc == 0) {
1491 path = strdup("");
1492 if (path == NULL) {
1493 error = got_error_from_errno("strdup");
1494 goto done;
1496 } else if (argc == 1) {
1497 if (worktree) {
1498 error = got_worktree_resolve_path(&path, worktree,
1499 argv[0]);
1500 if (error)
1501 goto done;
1502 } else {
1503 path = strdup(argv[0]);
1504 if (path == NULL) {
1505 error = got_error_from_errno("strdup");
1506 goto done;
1509 } else
1510 usage_log();
1512 if (repo_path == NULL) {
1513 repo_path = worktree ?
1514 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1516 if (repo_path == NULL) {
1517 error = got_error_from_errno("strdup");
1518 goto done;
1521 error = got_repo_open(&repo, repo_path);
1522 if (error != NULL)
1523 goto done;
1525 error = apply_unveil(got_repo_get_path(repo), 1,
1526 worktree ? got_worktree_get_root_path(worktree) : NULL);
1527 if (error)
1528 goto done;
1530 if (start_commit == NULL) {
1531 struct got_reference *head_ref;
1532 error = got_ref_open(&head_ref, repo,
1533 worktree ? got_worktree_get_head_ref_name(worktree)
1534 : GOT_REF_HEAD, 0);
1535 if (error != NULL)
1536 return error;
1537 error = got_ref_resolve(&id, repo, head_ref);
1538 got_ref_close(head_ref);
1539 if (error != NULL)
1540 return error;
1541 error = got_object_open_as_commit(&commit, repo, id);
1542 } else {
1543 struct got_reference *ref;
1544 error = got_ref_open(&ref, repo, start_commit, 0);
1545 if (error == NULL) {
1546 int obj_type;
1547 error = got_ref_resolve(&id, repo, ref);
1548 got_ref_close(ref);
1549 if (error != NULL)
1550 goto done;
1551 error = got_object_get_type(&obj_type, repo, id);
1552 if (error != NULL)
1553 goto done;
1554 if (obj_type == GOT_OBJ_TYPE_TAG) {
1555 struct got_tag_object *tag;
1556 error = got_object_open_as_tag(&tag, repo, id);
1557 if (error != NULL)
1558 goto done;
1559 if (got_object_tag_get_object_type(tag) !=
1560 GOT_OBJ_TYPE_COMMIT) {
1561 got_object_tag_close(tag);
1562 error = got_error(GOT_ERR_OBJ_TYPE);
1563 goto done;
1565 free(id);
1566 id = got_object_id_dup(
1567 got_object_tag_get_object_id(tag));
1568 if (id == NULL)
1569 error = got_error_from_errno(
1570 "got_object_id_dup");
1571 got_object_tag_close(tag);
1572 if (error)
1573 goto done;
1574 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1575 error = got_error(GOT_ERR_OBJ_TYPE);
1576 goto done;
1578 error = got_object_open_as_commit(&commit, repo, id);
1579 if (error != NULL)
1580 goto done;
1582 if (commit == NULL) {
1583 error = got_repo_match_object_id_prefix(&id,
1584 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1585 if (error != NULL)
1586 return error;
1589 if (error != NULL)
1590 goto done;
1592 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1593 if (error != NULL)
1594 goto done;
1595 if (in_repo_path) {
1596 free(path);
1597 path = in_repo_path;
1600 error = got_ref_list(&refs, repo);
1601 if (error)
1602 goto done;
1604 error = print_commits(id, repo, path, show_patch,
1605 diff_context, limit, first_parent_traversal, &refs);
1606 done:
1607 free(path);
1608 free(repo_path);
1609 free(cwd);
1610 free(id);
1611 if (worktree)
1612 got_worktree_close(worktree);
1613 if (repo) {
1614 const struct got_error *repo_error;
1615 repo_error = got_repo_close(repo);
1616 if (error == NULL)
1617 error = repo_error;
1619 got_ref_list_free(&refs);
1620 return error;
1623 __dead static void
1624 usage_diff(void)
1626 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1627 "[object1 object2 | path]\n", getprogname());
1628 exit(1);
1631 struct print_diff_arg {
1632 struct got_repository *repo;
1633 struct got_worktree *worktree;
1634 int diff_context;
1635 const char *id_str;
1636 int header_shown;
1639 static const struct got_error *
1640 print_diff(void *arg, unsigned char status, const char *path,
1641 struct got_object_id *blob_id, struct got_object_id *commit_id)
1643 struct print_diff_arg *a = arg;
1644 const struct got_error *err = NULL;
1645 struct got_blob_object *blob1 = NULL;
1646 FILE *f2 = NULL;
1647 char *abspath = NULL;
1648 struct stat sb;
1650 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1651 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1652 return NULL;
1654 if (!a->header_shown) {
1655 printf("diff %s %s\n", a->id_str,
1656 got_worktree_get_root_path(a->worktree));
1657 a->header_shown = 1;
1660 if (status != GOT_STATUS_ADD) {
1661 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1662 if (err)
1663 goto done;
1667 if (status != GOT_STATUS_DELETE) {
1668 if (asprintf(&abspath, "%s/%s",
1669 got_worktree_get_root_path(a->worktree), path) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 goto done;
1674 f2 = fopen(abspath, "r");
1675 if (f2 == NULL) {
1676 err = got_error_from_errno2("fopen", abspath);
1677 goto done;
1679 if (lstat(abspath, &sb) == -1) {
1680 err = got_error_from_errno2("lstat", abspath);
1681 goto done;
1683 } else
1684 sb.st_size = 0;
1686 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1687 stdout);
1688 done:
1689 if (blob1)
1690 got_object_blob_close(blob1);
1691 if (f2 && fclose(f2) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1693 free(abspath);
1694 return err;
1697 static const struct got_error *
1698 cmd_diff(int argc, char *argv[])
1700 const struct got_error *error;
1701 struct got_repository *repo = NULL;
1702 struct got_worktree *worktree = NULL;
1703 char *cwd = NULL, *repo_path = NULL;
1704 struct got_object_id *id1 = NULL, *id2 = NULL;
1705 const char *id_str1 = NULL, *id_str2 = NULL;
1706 char *label1 = NULL, *label2 = NULL;
1707 int type1, type2;
1708 int diff_context = 3, ch;
1709 const char *errstr;
1710 char *path = NULL;
1712 #ifndef PROFILE
1713 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1714 NULL) == -1)
1715 err(1, "pledge");
1716 #endif
1718 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1719 switch (ch) {
1720 case 'C':
1721 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1722 if (errstr != NULL)
1723 err(1, "-C option %s", errstr);
1724 break;
1725 case 'r':
1726 repo_path = realpath(optarg, NULL);
1727 if (repo_path == NULL)
1728 err(1, "-r option");
1729 got_path_strip_trailing_slashes(repo_path);
1730 break;
1731 default:
1732 usage_diff();
1733 /* NOTREACHED */
1737 argc -= optind;
1738 argv += optind;
1740 cwd = getcwd(NULL, 0);
1741 if (cwd == NULL) {
1742 error = got_error_from_errno("getcwd");
1743 goto done;
1745 error = got_worktree_open(&worktree, cwd);
1746 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1747 goto done;
1748 if (argc <= 1) {
1749 if (worktree == NULL) {
1750 error = got_error(GOT_ERR_NOT_WORKTREE);
1751 goto done;
1753 if (repo_path)
1754 errx(1,
1755 "-r option can't be used when diffing a work tree");
1756 repo_path = strdup(got_worktree_get_repo_path(worktree));
1757 if (repo_path == NULL) {
1758 error = got_error_from_errno("strdup");
1759 goto done;
1761 if (argc == 1) {
1762 error = got_worktree_resolve_path(&path, worktree,
1763 argv[0]);
1764 if (error)
1765 goto done;
1766 } else {
1767 path = strdup("");
1768 if (path == NULL) {
1769 error = got_error_from_errno("strdup");
1770 goto done;
1773 } else if (argc == 2) {
1774 id_str1 = argv[0];
1775 id_str2 = argv[1];
1776 if (worktree && repo_path == NULL) {
1777 repo_path =
1778 strdup(got_worktree_get_repo_path(worktree));
1779 if (repo_path == NULL) {
1780 error = got_error_from_errno("strdup");
1781 goto done;
1784 } else
1785 usage_diff();
1787 if (repo_path == NULL) {
1788 repo_path = getcwd(NULL, 0);
1789 if (repo_path == NULL)
1790 return got_error_from_errno("getcwd");
1793 error = got_repo_open(&repo, repo_path);
1794 free(repo_path);
1795 if (error != NULL)
1796 goto done;
1798 error = apply_unveil(got_repo_get_path(repo), 1,
1799 worktree ? got_worktree_get_root_path(worktree) : NULL);
1800 if (error)
1801 goto done;
1803 if (argc <= 1) {
1804 struct print_diff_arg arg;
1805 char *id_str;
1806 error = got_object_id_str(&id_str,
1807 got_worktree_get_base_commit_id(worktree));
1808 if (error)
1809 goto done;
1810 arg.repo = repo;
1811 arg.worktree = worktree;
1812 arg.diff_context = diff_context;
1813 arg.id_str = id_str;
1814 arg.header_shown = 0;
1816 error = got_worktree_status(worktree, path, repo, print_diff,
1817 &arg, check_cancelled, NULL);
1818 free(id_str);
1819 goto done;
1822 error = got_repo_match_object_id_prefix(&id1, id_str1,
1823 GOT_OBJ_TYPE_ANY, repo);
1824 if (error) {
1825 struct got_reference *ref;
1826 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1827 goto done;
1828 error = got_ref_open(&ref, repo, id_str1, 0);
1829 if (error != NULL)
1830 goto done;
1831 label1 = strdup(got_ref_get_name(ref));
1832 if (label1 == NULL) {
1833 error = got_error_from_errno("strdup");
1834 goto done;
1836 error = got_ref_resolve(&id1, repo, ref);
1837 got_ref_close(ref);
1838 if (error != NULL)
1839 goto done;
1840 } else {
1841 error = got_object_id_str(&label1, id1);
1842 if (label1 == NULL) {
1843 error = got_error_from_errno("strdup");
1844 goto done;
1848 error = got_repo_match_object_id_prefix(&id2, id_str2,
1849 GOT_OBJ_TYPE_ANY, repo);
1850 if (error) {
1851 struct got_reference *ref;
1852 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1853 goto done;
1854 error = got_ref_open(&ref, repo, id_str2, 0);
1855 if (error != NULL)
1856 goto done;
1857 label2 = strdup(got_ref_get_name(ref));
1858 if (label2 == NULL) {
1859 error = got_error_from_errno("strdup");
1860 goto done;
1862 error = got_ref_resolve(&id2, repo, ref);
1863 got_ref_close(ref);
1864 if (error != NULL)
1865 goto done;
1866 } else {
1867 error = got_object_id_str(&label2, id2);
1868 if (label2 == NULL) {
1869 error = got_error_from_errno("strdup");
1870 goto done;
1874 error = got_object_get_type(&type1, repo, id1);
1875 if (error)
1876 goto done;
1878 error = got_object_get_type(&type2, repo, id2);
1879 if (error)
1880 goto done;
1882 if (type1 != type2) {
1883 error = got_error(GOT_ERR_OBJ_TYPE);
1884 goto done;
1887 switch (type1) {
1888 case GOT_OBJ_TYPE_BLOB:
1889 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1890 diff_context, repo, stdout);
1891 break;
1892 case GOT_OBJ_TYPE_TREE:
1893 error = got_diff_objects_as_trees(id1, id2, "", "",
1894 diff_context, repo, stdout);
1895 break;
1896 case GOT_OBJ_TYPE_COMMIT:
1897 printf("diff %s %s\n", label1, label2);
1898 error = got_diff_objects_as_commits(id1, id2, diff_context,
1899 repo, stdout);
1900 break;
1901 default:
1902 error = got_error(GOT_ERR_OBJ_TYPE);
1905 done:
1906 free(label1);
1907 free(label2);
1908 free(id1);
1909 free(id2);
1910 free(path);
1911 if (worktree)
1912 got_worktree_close(worktree);
1913 if (repo) {
1914 const struct got_error *repo_error;
1915 repo_error = got_repo_close(repo);
1916 if (error == NULL)
1917 error = repo_error;
1919 return error;
1922 __dead static void
1923 usage_blame(void)
1925 fprintf(stderr,
1926 "usage: %s blame [-c commit] [-r repository-path] path\n",
1927 getprogname());
1928 exit(1);
1931 static const struct got_error *
1932 cmd_blame(int argc, char *argv[])
1934 const struct got_error *error;
1935 struct got_repository *repo = NULL;
1936 struct got_worktree *worktree = NULL;
1937 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1938 struct got_object_id *commit_id = NULL;
1939 char *commit_id_str = NULL;
1940 int ch;
1942 #ifndef PROFILE
1943 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1944 NULL) == -1)
1945 err(1, "pledge");
1946 #endif
1948 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1949 switch (ch) {
1950 case 'c':
1951 commit_id_str = optarg;
1952 break;
1953 case 'r':
1954 repo_path = realpath(optarg, NULL);
1955 if (repo_path == NULL)
1956 err(1, "-r option");
1957 got_path_strip_trailing_slashes(repo_path);
1958 break;
1959 default:
1960 usage_blame();
1961 /* NOTREACHED */
1965 argc -= optind;
1966 argv += optind;
1968 if (argc == 1)
1969 path = argv[0];
1970 else
1971 usage_blame();
1973 cwd = getcwd(NULL, 0);
1974 if (cwd == NULL) {
1975 error = got_error_from_errno("getcwd");
1976 goto done;
1978 if (repo_path == NULL) {
1979 error = got_worktree_open(&worktree, cwd);
1980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1981 goto done;
1982 else
1983 error = NULL;
1984 if (worktree) {
1985 repo_path =
1986 strdup(got_worktree_get_repo_path(worktree));
1987 if (repo_path == NULL)
1988 error = got_error_from_errno("strdup");
1989 if (error)
1990 goto done;
1991 } else {
1992 repo_path = strdup(cwd);
1993 if (repo_path == NULL) {
1994 error = got_error_from_errno("strdup");
1995 goto done;
2000 error = got_repo_open(&repo, repo_path);
2001 if (error != NULL)
2002 goto done;
2004 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2005 if (error)
2006 goto done;
2008 if (worktree) {
2009 const char *prefix = got_worktree_get_path_prefix(worktree);
2010 char *p, *worktree_subdir = cwd +
2011 strlen(got_worktree_get_root_path(worktree));
2012 if (asprintf(&p, "%s%s%s%s%s",
2013 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2014 worktree_subdir, worktree_subdir[0] ? "/" : "",
2015 path) == -1) {
2016 error = got_error_from_errno("asprintf");
2017 goto done;
2019 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2020 free(p);
2021 } else {
2022 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2024 if (error)
2025 goto done;
2027 if (commit_id_str == NULL) {
2028 struct got_reference *head_ref;
2029 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2030 if (error != NULL)
2031 goto done;
2032 error = got_ref_resolve(&commit_id, repo, head_ref);
2033 got_ref_close(head_ref);
2034 if (error != NULL)
2035 goto done;
2036 } else {
2037 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2038 if (error)
2039 goto done;
2042 error = got_blame(in_repo_path, commit_id, repo, stdout);
2043 done:
2044 free(in_repo_path);
2045 free(repo_path);
2046 free(cwd);
2047 free(commit_id);
2048 if (worktree)
2049 got_worktree_close(worktree);
2050 if (repo) {
2051 const struct got_error *repo_error;
2052 repo_error = got_repo_close(repo);
2053 if (error == NULL)
2054 error = repo_error;
2056 return error;
2059 __dead static void
2060 usage_tree(void)
2062 fprintf(stderr,
2063 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2064 getprogname());
2065 exit(1);
2068 static void
2069 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2070 const char *root_path)
2072 int is_root_path = (strcmp(path, root_path) == 0);
2074 path += strlen(root_path);
2075 while (path[0] == '/')
2076 path++;
2078 printf("%s%s%s%s%s\n", id ? id : "", path,
2079 is_root_path ? "" : "/", te->name,
2080 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2083 static const struct got_error *
2084 print_tree(const char *path, struct got_object_id *commit_id,
2085 int show_ids, int recurse, const char *root_path,
2086 struct got_repository *repo)
2088 const struct got_error *err = NULL;
2089 struct got_object_id *tree_id = NULL;
2090 struct got_tree_object *tree = NULL;
2091 const struct got_tree_entries *entries;
2092 struct got_tree_entry *te;
2094 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2095 if (err)
2096 goto done;
2098 err = got_object_open_as_tree(&tree, repo, tree_id);
2099 if (err)
2100 goto done;
2101 entries = got_object_tree_get_entries(tree);
2102 te = SIMPLEQ_FIRST(&entries->head);
2103 while (te) {
2104 char *id = NULL;
2106 if (sigint_received || sigpipe_received)
2107 break;
2109 if (show_ids) {
2110 char *id_str;
2111 err = got_object_id_str(&id_str, te->id);
2112 if (err)
2113 goto done;
2114 if (asprintf(&id, "%s ", id_str) == -1) {
2115 err = got_error_from_errno("asprintf");
2116 free(id_str);
2117 goto done;
2119 free(id_str);
2121 print_entry(te, id, path, root_path);
2122 free(id);
2124 if (recurse && S_ISDIR(te->mode)) {
2125 char *child_path;
2126 if (asprintf(&child_path, "%s%s%s", path,
2127 path[0] == '/' && path[1] == '\0' ? "" : "/",
2128 te->name) == -1) {
2129 err = got_error_from_errno("asprintf");
2130 goto done;
2132 err = print_tree(child_path, commit_id, show_ids, 1,
2133 root_path, repo);
2134 free(child_path);
2135 if (err)
2136 goto done;
2139 te = SIMPLEQ_NEXT(te, entry);
2141 done:
2142 if (tree)
2143 got_object_tree_close(tree);
2144 free(tree_id);
2145 return err;
2148 static const struct got_error *
2149 cmd_tree(int argc, char *argv[])
2151 const struct got_error *error;
2152 struct got_repository *repo = NULL;
2153 struct got_worktree *worktree = NULL;
2154 const char *path;
2155 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2156 struct got_object_id *commit_id = NULL;
2157 char *commit_id_str = NULL;
2158 int show_ids = 0, recurse = 0;
2159 int ch;
2161 #ifndef PROFILE
2162 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2163 NULL) == -1)
2164 err(1, "pledge");
2165 #endif
2167 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2168 switch (ch) {
2169 case 'c':
2170 commit_id_str = optarg;
2171 break;
2172 case 'r':
2173 repo_path = realpath(optarg, NULL);
2174 if (repo_path == NULL)
2175 err(1, "-r option");
2176 got_path_strip_trailing_slashes(repo_path);
2177 break;
2178 case 'i':
2179 show_ids = 1;
2180 break;
2181 case 'R':
2182 recurse = 1;
2183 break;
2184 default:
2185 usage_tree();
2186 /* NOTREACHED */
2190 argc -= optind;
2191 argv += optind;
2193 if (argc == 1)
2194 path = argv[0];
2195 else if (argc > 1)
2196 usage_tree();
2197 else
2198 path = NULL;
2200 cwd = getcwd(NULL, 0);
2201 if (cwd == NULL) {
2202 error = got_error_from_errno("getcwd");
2203 goto done;
2205 if (repo_path == NULL) {
2206 error = got_worktree_open(&worktree, cwd);
2207 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2208 goto done;
2209 else
2210 error = NULL;
2211 if (worktree) {
2212 repo_path =
2213 strdup(got_worktree_get_repo_path(worktree));
2214 if (repo_path == NULL)
2215 error = got_error_from_errno("strdup");
2216 if (error)
2217 goto done;
2218 } else {
2219 repo_path = strdup(cwd);
2220 if (repo_path == NULL) {
2221 error = got_error_from_errno("strdup");
2222 goto done;
2227 error = got_repo_open(&repo, repo_path);
2228 if (error != NULL)
2229 goto done;
2231 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2232 if (error)
2233 goto done;
2235 if (path == NULL) {
2236 if (worktree) {
2237 char *p, *worktree_subdir = cwd +
2238 strlen(got_worktree_get_root_path(worktree));
2239 if (asprintf(&p, "%s/%s",
2240 got_worktree_get_path_prefix(worktree),
2241 worktree_subdir) == -1) {
2242 error = got_error_from_errno("asprintf");
2243 goto done;
2245 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2246 free(p);
2247 if (error)
2248 goto done;
2249 } else
2250 path = "/";
2252 if (in_repo_path == NULL) {
2253 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2254 if (error != NULL)
2255 goto done;
2258 if (commit_id_str == NULL) {
2259 struct got_reference *head_ref;
2260 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2261 if (error != NULL)
2262 goto done;
2263 error = got_ref_resolve(&commit_id, repo, head_ref);
2264 got_ref_close(head_ref);
2265 if (error != NULL)
2266 goto done;
2267 } else {
2268 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2269 if (error)
2270 goto done;
2273 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2274 in_repo_path, repo);
2275 done:
2276 free(in_repo_path);
2277 free(repo_path);
2278 free(cwd);
2279 free(commit_id);
2280 if (worktree)
2281 got_worktree_close(worktree);
2282 if (repo) {
2283 const struct got_error *repo_error;
2284 repo_error = got_repo_close(repo);
2285 if (error == NULL)
2286 error = repo_error;
2288 return error;
2291 __dead static void
2292 usage_status(void)
2294 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2295 exit(1);
2298 static const struct got_error *
2299 print_status(void *arg, unsigned char status, const char *path,
2300 struct got_object_id *blob_id, struct got_object_id *commit_id)
2302 printf("%c %s\n", status, path);
2303 return NULL;
2306 static const struct got_error *
2307 cmd_status(int argc, char *argv[])
2309 const struct got_error *error = NULL;
2310 struct got_repository *repo = NULL;
2311 struct got_worktree *worktree = NULL;
2312 char *cwd = NULL, *path = NULL;
2313 int ch;
2315 while ((ch = getopt(argc, argv, "")) != -1) {
2316 switch (ch) {
2317 default:
2318 usage_status();
2319 /* NOTREACHED */
2323 argc -= optind;
2324 argv += optind;
2326 #ifndef PROFILE
2327 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2328 NULL) == -1)
2329 err(1, "pledge");
2330 #endif
2331 cwd = getcwd(NULL, 0);
2332 if (cwd == NULL) {
2333 error = got_error_from_errno("getcwd");
2334 goto done;
2337 error = got_worktree_open(&worktree, cwd);
2338 if (error != NULL)
2339 goto done;
2341 if (argc == 0) {
2342 path = strdup("");
2343 if (path == NULL) {
2344 error = got_error_from_errno("strdup");
2345 goto done;
2347 } else if (argc == 1) {
2348 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2349 if (error)
2350 goto done;
2351 } else
2352 usage_status();
2354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2355 if (error != NULL)
2356 goto done;
2358 error = apply_unveil(got_repo_get_path(repo), 1,
2359 got_worktree_get_root_path(worktree));
2360 if (error)
2361 goto done;
2363 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2364 check_cancelled, NULL);
2365 done:
2366 free(cwd);
2367 free(path);
2368 return error;
2371 __dead static void
2372 usage_ref(void)
2374 fprintf(stderr,
2375 "usage: %s ref [-r repository] -l | -d name | name target\n",
2376 getprogname());
2377 exit(1);
2380 static const struct got_error *
2381 list_refs(struct got_repository *repo)
2383 static const struct got_error *err = NULL;
2384 struct got_reflist_head refs;
2385 struct got_reflist_entry *re;
2387 SIMPLEQ_INIT(&refs);
2388 err = got_ref_list(&refs, repo);
2389 if (err)
2390 return err;
2392 SIMPLEQ_FOREACH(re, &refs, entry) {
2393 char *refstr;
2394 refstr = got_ref_to_str(re->ref);
2395 if (refstr == NULL)
2396 return got_error_from_errno("got_ref_to_str");
2397 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2398 free(refstr);
2401 got_ref_list_free(&refs);
2402 return NULL;
2405 static const struct got_error *
2406 delete_ref(struct got_repository *repo, const char *refname)
2408 const struct got_error *err = NULL;
2409 struct got_reference *ref;
2411 err = got_ref_open(&ref, repo, refname, 0);
2412 if (err)
2413 return err;
2415 err = got_ref_delete(ref, repo);
2416 got_ref_close(ref);
2417 return err;
2420 static const struct got_error *
2421 add_ref(struct got_repository *repo, const char *refname, const char *target)
2423 const struct got_error *err = NULL;
2424 struct got_object_id *id;
2425 struct got_reference *ref = NULL;
2428 * Don't let the user create a reference named '-'.
2429 * While technically a valid reference name, this case is usually
2430 * an unintended typo.
2432 if (refname[0] == '-' && refname[1] == '\0')
2433 return got_error(GOT_ERR_BAD_REF_NAME);
2435 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2436 repo);
2437 if (err) {
2438 struct got_reference *target_ref;
2440 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2441 return err;
2442 err = got_ref_open(&target_ref, repo, target, 0);
2443 if (err)
2444 return err;
2445 err = got_ref_resolve(&id, repo, target_ref);
2446 got_ref_close(target_ref);
2447 if (err)
2448 return err;
2451 err = got_ref_alloc(&ref, refname, id);
2452 if (err)
2453 goto done;
2455 err = got_ref_write(ref, repo);
2456 done:
2457 if (ref)
2458 got_ref_close(ref);
2459 free(id);
2460 return err;
2463 static const struct got_error *
2464 cmd_ref(int argc, char *argv[])
2466 const struct got_error *error = NULL;
2467 struct got_repository *repo = NULL;
2468 struct got_worktree *worktree = NULL;
2469 char *cwd = NULL, *repo_path = NULL;
2470 int ch, do_list = 0;
2471 const char *delref = NULL;
2473 /* TODO: Add -s option for adding symbolic references. */
2474 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2475 switch (ch) {
2476 case 'd':
2477 delref = optarg;
2478 break;
2479 case 'r':
2480 repo_path = realpath(optarg, NULL);
2481 if (repo_path == NULL)
2482 err(1, "-r option");
2483 got_path_strip_trailing_slashes(repo_path);
2484 break;
2485 case 'l':
2486 do_list = 1;
2487 break;
2488 default:
2489 usage_ref();
2490 /* NOTREACHED */
2494 if (do_list && delref)
2495 errx(1, "-l and -d options are mutually exclusive\n");
2497 argc -= optind;
2498 argv += optind;
2500 if (do_list || delref) {
2501 if (argc > 0)
2502 usage_ref();
2503 } else if (argc != 2)
2504 usage_ref();
2506 #ifndef PROFILE
2507 if (do_list) {
2508 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2509 NULL) == -1)
2510 err(1, "pledge");
2511 } else {
2512 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2513 "sendfd unveil", NULL) == -1)
2514 err(1, "pledge");
2516 #endif
2517 cwd = getcwd(NULL, 0);
2518 if (cwd == NULL) {
2519 error = got_error_from_errno("getcwd");
2520 goto done;
2523 if (repo_path == NULL) {
2524 error = got_worktree_open(&worktree, cwd);
2525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2526 goto done;
2527 else
2528 error = NULL;
2529 if (worktree) {
2530 repo_path =
2531 strdup(got_worktree_get_repo_path(worktree));
2532 if (repo_path == NULL)
2533 error = got_error_from_errno("strdup");
2534 if (error)
2535 goto done;
2536 } else {
2537 repo_path = strdup(cwd);
2538 if (repo_path == NULL) {
2539 error = got_error_from_errno("strdup");
2540 goto done;
2545 error = got_repo_open(&repo, repo_path);
2546 if (error != NULL)
2547 goto done;
2549 error = apply_unveil(got_repo_get_path(repo), do_list,
2550 worktree ? got_worktree_get_root_path(worktree) : NULL);
2551 if (error)
2552 goto done;
2554 if (do_list)
2555 error = list_refs(repo);
2556 else if (delref)
2557 error = delete_ref(repo, delref);
2558 else
2559 error = add_ref(repo, argv[0], argv[1]);
2560 done:
2561 if (repo)
2562 got_repo_close(repo);
2563 if (worktree)
2564 got_worktree_close(worktree);
2565 free(cwd);
2566 free(repo_path);
2567 return error;
2570 __dead static void
2571 usage_branch(void)
2573 fprintf(stderr,
2574 "usage: %s branch [-r repository] -l | -d name | "
2575 "name [base-branch]\n", getprogname());
2576 exit(1);
2579 static const struct got_error *
2580 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2582 static const struct got_error *err = NULL;
2583 struct got_reflist_head refs;
2584 struct got_reflist_entry *re;
2586 SIMPLEQ_INIT(&refs);
2588 err = got_ref_list(&refs, repo);
2589 if (err)
2590 return err;
2592 SIMPLEQ_FOREACH(re, &refs, entry) {
2593 const char *refname, *marker = " ";
2594 char *refstr;
2595 refname = got_ref_get_name(re->ref);
2596 if (strncmp(refname, "refs/heads/", 11) != 0)
2597 continue;
2598 if (worktree && strcmp(refname,
2599 got_worktree_get_head_ref_name(worktree)) == 0) {
2600 struct got_object_id *id = NULL;
2601 err = got_ref_resolve(&id, repo, re->ref);
2602 if (err)
2603 return err;
2604 if (got_object_id_cmp(id,
2605 got_worktree_get_base_commit_id(worktree)) == 0)
2606 marker = "* ";
2607 else
2608 marker = "~ ";
2609 free(id);
2611 refname += 11;
2612 refstr = got_ref_to_str(re->ref);
2613 if (refstr == NULL)
2614 return got_error_from_errno("got_ref_to_str");
2615 printf("%s%s: %s\n", marker, refname, refstr);
2616 free(refstr);
2619 got_ref_list_free(&refs);
2620 return NULL;
2623 static const struct got_error *
2624 delete_branch(struct got_repository *repo, const char *branch_name)
2626 const struct got_error *err = NULL;
2627 struct got_reference *ref;
2628 char *refname;
2630 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2631 return got_error_from_errno("asprintf");
2633 err = got_ref_open(&ref, repo, refname, 0);
2634 if (err)
2635 goto done;
2637 err = got_ref_delete(ref, repo);
2638 got_ref_close(ref);
2639 done:
2640 free(refname);
2641 return err;
2644 static const struct got_error *
2645 add_branch(struct got_repository *repo, const char *branch_name,
2646 const char *base_branch)
2648 const struct got_error *err = NULL;
2649 struct got_object_id *id = NULL;
2650 struct got_reference *ref = NULL;
2651 char *base_refname = NULL, *refname = NULL;
2652 struct got_reference *base_ref;
2655 * Don't let the user create a branch named '-'.
2656 * While technically a valid reference name, this case is usually
2657 * an unintended typo.
2659 if (branch_name[0] == '-' && branch_name[1] == '\0')
2660 return got_error(GOT_ERR_BAD_REF_NAME);
2662 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2663 base_refname = strdup(GOT_REF_HEAD);
2664 if (base_refname == NULL)
2665 return got_error_from_errno("strdup");
2666 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2667 return got_error_from_errno("asprintf");
2669 err = got_ref_open(&base_ref, repo, base_refname, 0);
2670 if (err)
2671 goto done;
2672 err = got_ref_resolve(&id, repo, base_ref);
2673 got_ref_close(base_ref);
2674 if (err)
2675 goto done;
2677 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2678 err = got_error_from_errno("asprintf");
2679 goto done;
2682 err = got_ref_open(&ref, repo, refname, 0);
2683 if (err == NULL) {
2684 err = got_error(GOT_ERR_BRANCH_EXISTS);
2685 goto done;
2686 } else if (err->code != GOT_ERR_NOT_REF)
2687 goto done;
2689 err = got_ref_alloc(&ref, refname, id);
2690 if (err)
2691 goto done;
2693 err = got_ref_write(ref, repo);
2694 done:
2695 if (ref)
2696 got_ref_close(ref);
2697 free(id);
2698 free(base_refname);
2699 free(refname);
2700 return err;
2703 static const struct got_error *
2704 cmd_branch(int argc, char *argv[])
2706 const struct got_error *error = NULL;
2707 struct got_repository *repo = NULL;
2708 struct got_worktree *worktree = NULL;
2709 char *cwd = NULL, *repo_path = NULL;
2710 int ch, do_list = 0;
2711 const char *delref = NULL;
2713 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2714 switch (ch) {
2715 case 'd':
2716 delref = optarg;
2717 break;
2718 case 'r':
2719 repo_path = realpath(optarg, NULL);
2720 if (repo_path == NULL)
2721 err(1, "-r option");
2722 got_path_strip_trailing_slashes(repo_path);
2723 break;
2724 case 'l':
2725 do_list = 1;
2726 break;
2727 default:
2728 usage_branch();
2729 /* NOTREACHED */
2733 if (do_list && delref)
2734 errx(1, "-l and -d options are mutually exclusive\n");
2736 argc -= optind;
2737 argv += optind;
2739 if (do_list || delref) {
2740 if (argc > 0)
2741 usage_branch();
2742 } else if (argc < 1 || argc > 2)
2743 usage_branch();
2745 #ifndef PROFILE
2746 if (do_list) {
2747 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2748 NULL) == -1)
2749 err(1, "pledge");
2750 } else {
2751 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2752 "sendfd unveil", NULL) == -1)
2753 err(1, "pledge");
2755 #endif
2756 cwd = getcwd(NULL, 0);
2757 if (cwd == NULL) {
2758 error = got_error_from_errno("getcwd");
2759 goto done;
2762 if (repo_path == NULL) {
2763 error = got_worktree_open(&worktree, cwd);
2764 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2765 goto done;
2766 else
2767 error = NULL;
2768 if (worktree) {
2769 repo_path =
2770 strdup(got_worktree_get_repo_path(worktree));
2771 if (repo_path == NULL)
2772 error = got_error_from_errno("strdup");
2773 if (error)
2774 goto done;
2775 } else {
2776 repo_path = strdup(cwd);
2777 if (repo_path == NULL) {
2778 error = got_error_from_errno("strdup");
2779 goto done;
2784 error = got_repo_open(&repo, repo_path);
2785 if (error != NULL)
2786 goto done;
2788 error = apply_unveil(got_repo_get_path(repo), do_list,
2789 worktree ? got_worktree_get_root_path(worktree) : NULL);
2790 if (error)
2791 goto done;
2793 if (do_list)
2794 error = list_branches(repo, worktree);
2795 else if (delref)
2796 error = delete_branch(repo, delref);
2797 else {
2798 const char *base_branch;
2799 if (argc == 1) {
2800 base_branch = worktree ?
2801 got_worktree_get_head_ref_name(worktree) :
2802 GOT_REF_HEAD;
2803 } else
2804 base_branch = argv[1];
2805 error = add_branch(repo, argv[0], base_branch);
2807 done:
2808 if (repo)
2809 got_repo_close(repo);
2810 if (worktree)
2811 got_worktree_close(worktree);
2812 free(cwd);
2813 free(repo_path);
2814 return error;
2817 __dead static void
2818 usage_add(void)
2820 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2821 exit(1);
2824 static const struct got_error *
2825 cmd_add(int argc, char *argv[])
2827 const struct got_error *error = NULL;
2828 struct got_repository *repo = NULL;
2829 struct got_worktree *worktree = NULL;
2830 char *cwd = NULL;
2831 struct got_pathlist_head paths;
2832 struct got_pathlist_entry *pe;
2833 int ch, x;
2835 TAILQ_INIT(&paths);
2837 while ((ch = getopt(argc, argv, "")) != -1) {
2838 switch (ch) {
2839 default:
2840 usage_add();
2841 /* NOTREACHED */
2845 argc -= optind;
2846 argv += optind;
2848 #ifndef PROFILE
2849 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2850 NULL) == -1)
2851 err(1, "pledge");
2852 #endif
2853 if (argc < 1)
2854 usage_add();
2856 cwd = getcwd(NULL, 0);
2857 if (cwd == NULL) {
2858 error = got_error_from_errno("getcwd");
2859 goto done;
2862 error = got_worktree_open(&worktree, cwd);
2863 if (error)
2864 goto done;
2866 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2867 if (error != NULL)
2868 goto done;
2870 error = apply_unveil(got_repo_get_path(repo), 1,
2871 got_worktree_get_root_path(worktree));
2872 if (error)
2873 goto done;
2875 for (x = 0; x < argc; x++) {
2876 char *path = realpath(argv[x], NULL);
2877 if (path == NULL) {
2878 error = got_error_from_errno2("realpath", argv[x]);
2879 goto done;
2882 got_path_strip_trailing_slashes(path);
2883 error = got_pathlist_insert(&pe, &paths, path, NULL);
2884 if (error) {
2885 free(path);
2886 goto done;
2889 error = got_worktree_schedule_add(worktree, &paths, print_status,
2890 NULL, repo);
2891 done:
2892 if (repo)
2893 got_repo_close(repo);
2894 if (worktree)
2895 got_worktree_close(worktree);
2896 TAILQ_FOREACH(pe, &paths, entry)
2897 free((char *)pe->path);
2898 got_pathlist_free(&paths);
2899 free(cwd);
2900 return error;
2903 __dead static void
2904 usage_remove(void)
2906 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2907 exit(1);
2910 static const struct got_error *
2911 cmd_remove(int argc, char *argv[])
2913 const struct got_error *error = NULL;
2914 struct got_worktree *worktree = NULL;
2915 struct got_repository *repo = NULL;
2916 char *cwd = NULL;
2917 struct got_pathlist_head paths;
2918 struct got_pathlist_entry *pe;
2919 int ch, i, delete_local_mods = 0;
2921 TAILQ_INIT(&paths);
2923 while ((ch = getopt(argc, argv, "f")) != -1) {
2924 switch (ch) {
2925 case 'f':
2926 delete_local_mods = 1;
2927 break;
2928 default:
2929 usage_add();
2930 /* NOTREACHED */
2934 argc -= optind;
2935 argv += optind;
2937 #ifndef PROFILE
2938 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2939 NULL) == -1)
2940 err(1, "pledge");
2941 #endif
2942 if (argc < 1)
2943 usage_remove();
2945 cwd = getcwd(NULL, 0);
2946 if (cwd == NULL) {
2947 error = got_error_from_errno("getcwd");
2948 goto done;
2950 error = got_worktree_open(&worktree, cwd);
2951 if (error)
2952 goto done;
2954 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2955 if (error)
2956 goto done;
2958 error = apply_unveil(got_repo_get_path(repo), 1,
2959 got_worktree_get_root_path(worktree));
2960 if (error)
2961 goto done;
2963 for (i = 0; i < argc; i++) {
2964 char *path = realpath(argv[i], NULL);
2965 if (path == NULL) {
2966 error = got_error_from_errno2("realpath", argv[i]);
2967 goto done;
2970 got_path_strip_trailing_slashes(path);
2971 error = got_pathlist_insert(&pe, &paths, path, NULL);
2972 if (error) {
2973 free(path);
2974 goto done;
2977 error = got_worktree_schedule_delete(worktree, &paths,
2978 delete_local_mods, print_status, NULL, repo);
2979 if (error)
2980 goto done;
2981 done:
2982 if (repo)
2983 got_repo_close(repo);
2984 if (worktree)
2985 got_worktree_close(worktree);
2986 TAILQ_FOREACH(pe, &paths, entry)
2987 free((char *)pe->path);
2988 got_pathlist_free(&paths);
2989 free(cwd);
2990 return error;
2993 __dead static void
2994 usage_revert(void)
2996 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2997 exit(1);
3000 static const struct got_error *
3001 revert_progress(void *arg, unsigned char status, const char *path)
3003 while (path[0] == '/')
3004 path++;
3005 printf("%c %s\n", status, path);
3006 return NULL;
3009 static const struct got_error *
3010 cmd_revert(int argc, char *argv[])
3012 const struct got_error *error = NULL;
3013 struct got_worktree *worktree = NULL;
3014 struct got_repository *repo = NULL;
3015 char *cwd = NULL, *path = NULL;
3016 struct got_pathlist_head paths;
3017 struct got_pathlist_entry *pe;
3018 int ch, i;
3020 TAILQ_INIT(&paths);
3022 while ((ch = getopt(argc, argv, "")) != -1) {
3023 switch (ch) {
3024 default:
3025 usage_revert();
3026 /* NOTREACHED */
3030 argc -= optind;
3031 argv += optind;
3033 #ifndef PROFILE
3034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3035 "unveil", NULL) == -1)
3036 err(1, "pledge");
3037 #endif
3038 if (argc < 1)
3039 usage_revert();
3041 for (i = 0; i < argc; i++) {
3042 char *path = realpath(argv[i], NULL);
3043 if (path == NULL) {
3044 error = got_error_from_errno2("realpath", argv[i]);
3045 goto done;
3048 got_path_strip_trailing_slashes(path);
3049 error = got_pathlist_insert(&pe, &paths, path, NULL);
3050 if (error) {
3051 free(path);
3052 goto done;
3056 cwd = getcwd(NULL, 0);
3057 if (cwd == NULL) {
3058 error = got_error_from_errno("getcwd");
3059 goto done;
3061 error = got_worktree_open(&worktree, cwd);
3062 if (error)
3063 goto done;
3065 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3066 if (error != NULL)
3067 goto done;
3069 error = apply_unveil(got_repo_get_path(repo), 1,
3070 got_worktree_get_root_path(worktree));
3071 if (error)
3072 goto done;
3074 error = got_worktree_revert(worktree, &paths,
3075 revert_progress, NULL, repo);
3076 if (error)
3077 goto done;
3078 done:
3079 if (repo)
3080 got_repo_close(repo);
3081 if (worktree)
3082 got_worktree_close(worktree);
3083 free(path);
3084 free(cwd);
3085 return error;
3088 __dead static void
3089 usage_commit(void)
3091 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3092 exit(1);
3095 struct collect_commit_logmsg_arg {
3096 const char *cmdline_log;
3097 const char *editor;
3098 const char *worktree_path;
3099 const char *branch_name;
3100 const char *repo_path;
3101 char *logmsg_path;
3105 static const struct got_error *
3106 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3107 void *arg)
3109 char *initial_content = NULL;
3110 struct got_pathlist_entry *pe;
3111 const struct got_error *err = NULL;
3112 char *template = NULL;
3113 struct collect_commit_logmsg_arg *a = arg;
3114 int fd;
3115 size_t len;
3117 /* if a message was specified on the command line, just use it */
3118 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3119 len = strlen(a->cmdline_log) + 1;
3120 *logmsg = malloc(len + 1);
3121 if (*logmsg == NULL)
3122 return got_error_from_errno("malloc");
3123 strlcpy(*logmsg, a->cmdline_log, len);
3124 return NULL;
3127 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3128 return got_error_from_errno("asprintf");
3130 if (asprintf(&initial_content,
3131 "\n# changes to be committed on branch %s:\n",
3132 a->branch_name) == -1)
3133 return got_error_from_errno("asprintf");
3135 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3136 if (err)
3137 goto done;
3139 dprintf(fd, initial_content);
3141 TAILQ_FOREACH(pe, commitable_paths, entry) {
3142 struct got_commitable *ct = pe->data;
3143 dprintf(fd, "# %c %s\n",
3144 got_commitable_get_status(ct),
3145 got_commitable_get_path(ct));
3147 close(fd);
3149 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3150 done:
3151 unlink(a->logmsg_path);
3152 free(a->logmsg_path);
3153 free(initial_content);
3154 free(template);
3156 /* Editor is done; we can now apply unveil(2) */
3157 if (err == NULL) {
3158 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3159 if (err) {
3160 free(*logmsg);
3161 *logmsg = NULL;
3164 return err;
3167 static const struct got_error *
3168 cmd_commit(int argc, char *argv[])
3170 const struct got_error *error = NULL;
3171 struct got_worktree *worktree = NULL;
3172 struct got_repository *repo = NULL;
3173 char *cwd = NULL, *path = NULL, *id_str = NULL;
3174 struct got_object_id *id = NULL;
3175 const char *logmsg = NULL;
3176 const char *got_author = getenv("GOT_AUTHOR");
3177 struct collect_commit_logmsg_arg cl_arg;
3178 char *editor = NULL;
3179 int ch, rebase_in_progress;
3181 while ((ch = getopt(argc, argv, "m:")) != -1) {
3182 switch (ch) {
3183 case 'm':
3184 logmsg = optarg;
3185 break;
3186 default:
3187 usage_commit();
3188 /* NOTREACHED */
3192 argc -= optind;
3193 argv += optind;
3195 #ifndef PROFILE
3196 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3197 "unveil", NULL) == -1)
3198 err(1, "pledge");
3199 #endif
3200 if (argc == 1) {
3201 path = realpath(argv[0], NULL);
3202 if (path == NULL) {
3203 error = got_error_from_errno2("realpath", argv[0]);
3204 goto done;
3206 got_path_strip_trailing_slashes(path);
3207 } else if (argc != 0)
3208 usage_commit();
3210 if (got_author == NULL) {
3211 /* TODO: Look current user up in password database */
3212 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3213 goto done;
3216 cwd = getcwd(NULL, 0);
3217 if (cwd == NULL) {
3218 error = got_error_from_errno("getcwd");
3219 goto done;
3221 error = got_worktree_open(&worktree, cwd);
3222 if (error)
3223 goto done;
3225 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3226 if (error)
3227 goto done;
3228 if (rebase_in_progress) {
3229 error = got_error(GOT_ERR_REBASING);
3230 goto done;
3233 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3234 if (error != NULL)
3235 goto done;
3238 * unveil(2) traverses exec(2); if an editor is used we have
3239 * to apply unveil after the log message has been written.
3241 if (logmsg == NULL || strlen(logmsg) == 0)
3242 error = get_editor(&editor);
3243 else
3244 error = apply_unveil(got_repo_get_path(repo), 0,
3245 got_worktree_get_root_path(worktree));
3246 if (error)
3247 goto done;
3249 cl_arg.editor = editor;
3250 cl_arg.cmdline_log = logmsg;
3251 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3252 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3253 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3254 cl_arg.branch_name += 5;
3255 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3256 cl_arg.branch_name += 6;
3257 cl_arg.repo_path = got_repo_get_path(repo);
3258 cl_arg.logmsg_path = NULL;
3259 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3260 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3261 if (error) {
3262 if (cl_arg.logmsg_path)
3263 fprintf(stderr, "%s: log message preserved in %s\n",
3264 getprogname(), cl_arg.logmsg_path);
3265 goto done;
3268 if (cl_arg.logmsg_path)
3269 unlink(cl_arg.logmsg_path);
3271 error = got_object_id_str(&id_str, id);
3272 if (error)
3273 goto done;
3274 printf("Created commit %s\n", id_str);
3275 done:
3276 if (repo)
3277 got_repo_close(repo);
3278 if (worktree)
3279 got_worktree_close(worktree);
3280 free(path);
3281 free(cwd);
3282 free(id_str);
3283 free(editor);
3284 return error;
3287 __dead static void
3288 usage_cherrypick(void)
3290 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3291 exit(1);
3294 static const struct got_error *
3295 cmd_cherrypick(int argc, char *argv[])
3297 const struct got_error *error = NULL;
3298 struct got_worktree *worktree = NULL;
3299 struct got_repository *repo = NULL;
3300 char *cwd = NULL, *commit_id_str = NULL;
3301 struct got_object_id *commit_id = NULL;
3302 struct got_commit_object *commit = NULL;
3303 struct got_object_qid *pid;
3304 struct got_reference *head_ref = NULL;
3305 int ch, did_something = 0;
3307 while ((ch = getopt(argc, argv, "")) != -1) {
3308 switch (ch) {
3309 default:
3310 usage_cherrypick();
3311 /* NOTREACHED */
3315 argc -= optind;
3316 argv += optind;
3318 #ifndef PROFILE
3319 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3320 "unveil", NULL) == -1)
3321 err(1, "pledge");
3322 #endif
3323 if (argc != 1)
3324 usage_cherrypick();
3326 cwd = getcwd(NULL, 0);
3327 if (cwd == NULL) {
3328 error = got_error_from_errno("getcwd");
3329 goto done;
3331 error = got_worktree_open(&worktree, cwd);
3332 if (error)
3333 goto done;
3335 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3336 if (error != NULL)
3337 goto done;
3339 error = apply_unveil(got_repo_get_path(repo), 0,
3340 got_worktree_get_root_path(worktree));
3341 if (error)
3342 goto done;
3344 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3345 GOT_OBJ_TYPE_COMMIT, repo);
3346 if (error != NULL) {
3347 struct got_reference *ref;
3348 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3349 goto done;
3350 error = got_ref_open(&ref, repo, argv[0], 0);
3351 if (error != NULL)
3352 goto done;
3353 error = got_ref_resolve(&commit_id, repo, ref);
3354 got_ref_close(ref);
3355 if (error != NULL)
3356 goto done;
3358 error = got_object_id_str(&commit_id_str, commit_id);
3359 if (error)
3360 goto done;
3362 error = got_ref_open(&head_ref, repo,
3363 got_worktree_get_head_ref_name(worktree), 0);
3364 if (error != NULL)
3365 goto done;
3367 error = check_same_branch(commit_id, head_ref, repo);
3368 if (error) {
3369 if (error->code != GOT_ERR_ANCESTRY)
3370 goto done;
3371 error = NULL;
3372 } else {
3373 error = got_error(GOT_ERR_SAME_BRANCH);
3374 goto done;
3377 error = got_object_open_as_commit(&commit, repo, commit_id);
3378 if (error)
3379 goto done;
3380 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3381 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3382 commit_id, repo, update_progress, &did_something, check_cancelled,
3383 NULL);
3384 if (error != NULL)
3385 goto done;
3387 if (did_something)
3388 printf("Merged commit %s\n", commit_id_str);
3389 done:
3390 if (commit)
3391 got_object_commit_close(commit);
3392 free(commit_id_str);
3393 if (head_ref)
3394 got_ref_close(head_ref);
3395 if (worktree)
3396 got_worktree_close(worktree);
3397 if (repo)
3398 got_repo_close(repo);
3399 return error;
3402 __dead static void
3403 usage_backout(void)
3405 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3406 exit(1);
3409 static const struct got_error *
3410 cmd_backout(int argc, char *argv[])
3412 const struct got_error *error = NULL;
3413 struct got_worktree *worktree = NULL;
3414 struct got_repository *repo = NULL;
3415 char *cwd = NULL, *commit_id_str = NULL;
3416 struct got_object_id *commit_id = NULL;
3417 struct got_commit_object *commit = NULL;
3418 struct got_object_qid *pid;
3419 struct got_reference *head_ref = NULL;
3420 int ch, did_something = 0;
3422 while ((ch = getopt(argc, argv, "")) != -1) {
3423 switch (ch) {
3424 default:
3425 usage_backout();
3426 /* NOTREACHED */
3430 argc -= optind;
3431 argv += optind;
3433 #ifndef PROFILE
3434 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3435 "unveil", NULL) == -1)
3436 err(1, "pledge");
3437 #endif
3438 if (argc != 1)
3439 usage_backout();
3441 cwd = getcwd(NULL, 0);
3442 if (cwd == NULL) {
3443 error = got_error_from_errno("getcwd");
3444 goto done;
3446 error = got_worktree_open(&worktree, cwd);
3447 if (error)
3448 goto done;
3450 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3451 if (error != NULL)
3452 goto done;
3454 error = apply_unveil(got_repo_get_path(repo), 0,
3455 got_worktree_get_root_path(worktree));
3456 if (error)
3457 goto done;
3459 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3460 GOT_OBJ_TYPE_COMMIT, repo);
3461 if (error != NULL) {
3462 struct got_reference *ref;
3463 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3464 goto done;
3465 error = got_ref_open(&ref, repo, argv[0], 0);
3466 if (error != NULL)
3467 goto done;
3468 error = got_ref_resolve(&commit_id, repo, ref);
3469 got_ref_close(ref);
3470 if (error != NULL)
3471 goto done;
3473 error = got_object_id_str(&commit_id_str, commit_id);
3474 if (error)
3475 goto done;
3477 error = got_ref_open(&head_ref, repo,
3478 got_worktree_get_head_ref_name(worktree), 0);
3479 if (error != NULL)
3480 goto done;
3482 error = check_same_branch(commit_id, head_ref, repo);
3483 if (error)
3484 goto done;
3486 error = got_object_open_as_commit(&commit, repo, commit_id);
3487 if (error)
3488 goto done;
3489 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3490 if (pid == NULL) {
3491 error = got_error(GOT_ERR_ROOT_COMMIT);
3492 goto done;
3495 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3496 update_progress, &did_something, check_cancelled, NULL);
3497 if (error != NULL)
3498 goto done;
3500 if (did_something)
3501 printf("Backed out commit %s\n", commit_id_str);
3502 done:
3503 if (commit)
3504 got_object_commit_close(commit);
3505 free(commit_id_str);
3506 if (head_ref)
3507 got_ref_close(head_ref);
3508 if (worktree)
3509 got_worktree_close(worktree);
3510 if (repo)
3511 got_repo_close(repo);
3512 return error;
3515 __dead static void
3516 usage_rebase(void)
3518 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3519 getprogname());
3520 exit(1);
3523 void
3524 trim_logmsg(char *logmsg, int limit)
3526 char *nl;
3527 size_t len;
3529 len = strlen(logmsg);
3530 if (len > limit)
3531 len = limit;
3532 logmsg[len] = '\0';
3533 nl = strchr(logmsg, '\n');
3534 if (nl)
3535 *nl = '\0';
3538 static const struct got_error *
3539 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3541 const char *logmsg0 = NULL;
3543 logmsg0 = got_object_commit_get_logmsg(commit);
3545 while (isspace((unsigned char)logmsg0[0]))
3546 logmsg0++;
3548 *logmsg = strdup(logmsg0);
3549 if (*logmsg == NULL)
3550 return got_error_from_errno("strdup");
3552 trim_logmsg(*logmsg, limit);
3553 return NULL;
3556 static const struct got_error *
3557 show_rebase_progress(struct got_commit_object *commit,
3558 struct got_object_id *old_id, struct got_object_id *new_id)
3560 const struct got_error *err;
3561 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3563 err = got_object_id_str(&old_id_str, old_id);
3564 if (err)
3565 goto done;
3567 if (new_id) {
3568 err = got_object_id_str(&new_id_str, new_id);
3569 if (err)
3570 goto done;
3573 old_id_str[12] = '\0';
3574 if (new_id_str)
3575 new_id_str[12] = '\0';
3577 err = get_short_logmsg(&logmsg, 42, commit);
3578 if (err)
3579 goto done;
3581 printf("%s -> %s: %s\n", old_id_str,
3582 new_id_str ? new_id_str : "no-op change", logmsg);
3583 done:
3584 free(old_id_str);
3585 free(new_id_str);
3586 return err;
3589 static const struct got_error *
3590 rebase_progress(void *arg, unsigned char status, const char *path)
3592 unsigned char *rebase_status = arg;
3594 while (path[0] == '/')
3595 path++;
3596 printf("%c %s\n", status, path);
3598 if (*rebase_status == GOT_STATUS_CONFLICT)
3599 return NULL;
3600 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3601 *rebase_status = status;
3602 return NULL;
3605 static const struct got_error *
3606 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3607 struct got_reference *branch, struct got_reference *new_base_branch,
3608 struct got_reference *tmp_branch, struct got_repository *repo)
3610 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3611 return got_worktree_rebase_complete(worktree, fileindex,
3612 new_base_branch, tmp_branch, branch, repo);
3615 static const struct got_error *
3616 rebase_commit(struct got_pathlist_head *merged_paths,
3617 struct got_worktree *worktree, struct got_fileindex *fileindex,
3618 struct got_reference *tmp_branch,
3619 struct got_object_id *commit_id, struct got_repository *repo)
3621 const struct got_error *error;
3622 struct got_commit_object *commit;
3623 struct got_object_id *new_commit_id;
3625 error = got_object_open_as_commit(&commit, repo, commit_id);
3626 if (error)
3627 return error;
3629 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3630 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3631 if (error) {
3632 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3633 goto done;
3634 error = show_rebase_progress(commit, commit_id, NULL);
3635 } else {
3636 error = show_rebase_progress(commit, commit_id, new_commit_id);
3637 free(new_commit_id);
3639 done:
3640 got_object_commit_close(commit);
3641 return error;
3644 struct check_path_prefix_arg {
3645 const char *path_prefix;
3646 size_t len;
3647 int errcode;
3650 static const struct got_error *
3651 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3652 struct got_blob_object *blob2, struct got_object_id *id1,
3653 struct got_object_id *id2, const char *path1, const char *path2,
3654 struct got_repository *repo)
3656 struct check_path_prefix_arg *a = arg;
3658 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3659 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3660 return got_error(a->errcode);
3662 return NULL;
3665 static const struct got_error *
3666 check_path_prefix(struct got_object_id *parent_id,
3667 struct got_object_id *commit_id, const char *path_prefix,
3668 int errcode, struct got_repository *repo)
3670 const struct got_error *err;
3671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3672 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3673 struct check_path_prefix_arg cpp_arg;
3675 if (got_path_is_root_dir(path_prefix))
3676 return NULL;
3678 err = got_object_open_as_commit(&commit, repo, commit_id);
3679 if (err)
3680 goto done;
3682 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3683 if (err)
3684 goto done;
3686 err = got_object_open_as_tree(&tree1, repo,
3687 got_object_commit_get_tree_id(parent_commit));
3688 if (err)
3689 goto done;
3691 err = got_object_open_as_tree(&tree2, repo,
3692 got_object_commit_get_tree_id(commit));
3693 if (err)
3694 goto done;
3696 cpp_arg.path_prefix = path_prefix;
3697 while (cpp_arg.path_prefix[0] == '/')
3698 cpp_arg.path_prefix++;
3699 cpp_arg.len = strlen(cpp_arg.path_prefix);
3700 cpp_arg.errcode = errcode;
3701 err = got_diff_tree(tree1, tree2, "", "", repo,
3702 check_path_prefix_in_diff, &cpp_arg);
3703 done:
3704 if (tree1)
3705 got_object_tree_close(tree1);
3706 if (tree2)
3707 got_object_tree_close(tree2);
3708 if (commit)
3709 got_object_commit_close(commit);
3710 if (parent_commit)
3711 got_object_commit_close(parent_commit);
3712 return err;
3715 static const struct got_error *
3716 collect_commits(struct got_object_id_queue *commits,
3717 struct got_object_id *initial_commit_id,
3718 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3719 const char *path_prefix, int path_prefix_errcode,
3720 struct got_repository *repo)
3722 const struct got_error *err = NULL;
3723 struct got_commit_graph *graph = NULL;
3724 struct got_object_id *parent_id = NULL;
3725 struct got_object_qid *qid;
3726 struct got_object_id *commit_id = initial_commit_id;
3728 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3729 if (err)
3730 return err;
3732 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3733 if (err)
3734 goto done;
3735 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3736 err = got_commit_graph_iter_next(&parent_id, graph);
3737 if (err) {
3738 if (err->code == GOT_ERR_ITER_COMPLETED) {
3739 err = got_error_msg(GOT_ERR_ANCESTRY,
3740 "ran out of commits to rebase before "
3741 "youngest common ancestor commit has "
3742 "been reached?!?");
3743 goto done;
3744 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3745 goto done;
3746 err = got_commit_graph_fetch_commits(graph, 1, repo);
3747 if (err)
3748 goto done;
3749 } else {
3750 err = check_path_prefix(parent_id, commit_id,
3751 path_prefix, path_prefix_errcode, repo);
3752 if (err)
3753 goto done;
3755 err = got_object_qid_alloc(&qid, commit_id);
3756 if (err)
3757 goto done;
3758 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3759 commit_id = parent_id;
3762 done:
3763 got_commit_graph_close(graph);
3764 return err;
3767 static const struct got_error *
3768 cmd_rebase(int argc, char *argv[])
3770 const struct got_error *error = NULL;
3771 struct got_worktree *worktree = NULL;
3772 struct got_repository *repo = NULL;
3773 struct got_fileindex *fileindex = NULL;
3774 char *cwd = NULL;
3775 struct got_reference *branch = NULL;
3776 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3777 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3778 struct got_object_id *resume_commit_id = NULL;
3779 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3780 struct got_commit_object *commit = NULL;
3781 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3782 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3783 struct got_object_id_queue commits;
3784 struct got_pathlist_head merged_paths;
3785 const struct got_object_id_queue *parent_ids;
3786 struct got_object_qid *qid, *pid;
3788 SIMPLEQ_INIT(&commits);
3789 TAILQ_INIT(&merged_paths);
3791 while ((ch = getopt(argc, argv, "ac")) != -1) {
3792 switch (ch) {
3793 case 'a':
3794 abort_rebase = 1;
3795 break;
3796 case 'c':
3797 continue_rebase = 1;
3798 break;
3799 default:
3800 usage_rebase();
3801 /* NOTREACHED */
3805 argc -= optind;
3806 argv += optind;
3808 #ifndef PROFILE
3809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3810 "unveil", NULL) == -1)
3811 err(1, "pledge");
3812 #endif
3813 if (abort_rebase && continue_rebase)
3814 usage_rebase();
3815 else if (abort_rebase || continue_rebase) {
3816 if (argc != 0)
3817 usage_rebase();
3818 } else if (argc != 1)
3819 usage_rebase();
3821 cwd = getcwd(NULL, 0);
3822 if (cwd == NULL) {
3823 error = got_error_from_errno("getcwd");
3824 goto done;
3826 error = got_worktree_open(&worktree, cwd);
3827 if (error)
3828 goto done;
3830 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3831 if (error != NULL)
3832 goto done;
3834 error = apply_unveil(got_repo_get_path(repo), 0,
3835 got_worktree_get_root_path(worktree));
3836 if (error)
3837 goto done;
3839 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3840 if (error)
3841 goto done;
3843 if (abort_rebase) {
3844 int did_something;
3845 if (!rebase_in_progress) {
3846 error = got_error(GOT_ERR_NOT_REBASING);
3847 goto done;
3849 error = got_worktree_rebase_continue(&resume_commit_id,
3850 &new_base_branch, &tmp_branch, &branch, &fileindex,
3851 worktree, repo);
3852 if (error)
3853 goto done;
3854 printf("Switching work tree to %s\n",
3855 got_ref_get_symref_target(new_base_branch));
3856 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3857 new_base_branch, update_progress, &did_something);
3858 if (error)
3859 goto done;
3860 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3861 goto done; /* nothing else to do */
3864 if (continue_rebase) {
3865 if (!rebase_in_progress) {
3866 error = got_error(GOT_ERR_NOT_REBASING);
3867 goto done;
3869 error = got_worktree_rebase_continue(&resume_commit_id,
3870 &new_base_branch, &tmp_branch, &branch, &fileindex,
3871 worktree, repo);
3872 if (error)
3873 goto done;
3875 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3876 resume_commit_id, repo);
3877 if (error)
3878 goto done;
3880 yca_id = got_object_id_dup(resume_commit_id);
3881 if (yca_id == NULL) {
3882 error = got_error_from_errno("got_object_id_dup");
3883 goto done;
3885 } else {
3886 error = got_ref_open(&branch, repo, argv[0], 0);
3887 if (error != NULL)
3888 goto done;
3890 error = check_same_branch(
3891 got_worktree_get_base_commit_id(worktree), branch, repo);
3892 if (error) {
3893 if (error->code != GOT_ERR_ANCESTRY)
3894 goto done;
3895 error = NULL;
3896 } else {
3897 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3898 "specified branch resolves to a commit which "
3899 "is already contained in work tree's branch");
3900 goto done;
3904 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3905 if (error)
3906 goto done;
3908 if (!continue_rebase) {
3909 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3910 got_worktree_get_base_commit_id(worktree),
3911 branch_head_commit_id, repo);
3912 if (error)
3913 goto done;
3914 if (yca_id == NULL) {
3915 error = got_error_msg(GOT_ERR_ANCESTRY,
3916 "specified branch shares no common ancestry "
3917 "with work tree's branch");
3918 goto done;
3921 error = got_worktree_rebase_prepare(&new_base_branch,
3922 &tmp_branch, &fileindex, worktree, branch, repo);
3923 if (error)
3924 goto done;
3927 commit_id = branch_head_commit_id;
3928 error = got_object_open_as_commit(&commit, repo, commit_id);
3929 if (error)
3930 goto done;
3932 parent_ids = got_object_commit_get_parent_ids(commit);
3933 pid = SIMPLEQ_FIRST(parent_ids);
3934 error = collect_commits(&commits, commit_id, pid->id,
3935 yca_id, got_worktree_get_path_prefix(worktree),
3936 GOT_ERR_REBASE_PATH, repo);
3937 got_object_commit_close(commit);
3938 commit = NULL;
3939 if (error)
3940 goto done;
3942 if (SIMPLEQ_EMPTY(&commits)) {
3943 if (continue_rebase)
3944 error = rebase_complete(worktree, fileindex,
3945 branch, new_base_branch, tmp_branch, repo);
3946 else
3947 error = got_error(GOT_ERR_EMPTY_REBASE);
3948 goto done;
3951 pid = NULL;
3952 SIMPLEQ_FOREACH(qid, &commits, entry) {
3953 commit_id = qid->id;
3954 parent_id = pid ? pid->id : yca_id;
3955 pid = qid;
3957 error = got_worktree_rebase_merge_files(&merged_paths,
3958 worktree, fileindex, parent_id, commit_id, repo,
3959 rebase_progress, &rebase_status, check_cancelled, NULL);
3960 if (error)
3961 goto done;
3963 if (rebase_status == GOT_STATUS_CONFLICT) {
3964 got_worktree_rebase_pathlist_free(&merged_paths);
3965 break;
3968 error = rebase_commit(&merged_paths, worktree, fileindex,
3969 tmp_branch, commit_id, repo);
3970 got_worktree_rebase_pathlist_free(&merged_paths);
3971 if (error)
3972 goto done;
3975 if (rebase_status == GOT_STATUS_CONFLICT) {
3976 error = got_worktree_rebase_postpone(worktree, fileindex);
3977 if (error)
3978 goto done;
3979 error = got_error_msg(GOT_ERR_CONFLICTS,
3980 "conflicts must be resolved before rebasing can continue");
3981 } else
3982 error = rebase_complete(worktree, fileindex, branch,
3983 new_base_branch, tmp_branch, repo);
3984 done:
3985 got_object_id_queue_free(&commits);
3986 free(branch_head_commit_id);
3987 free(resume_commit_id);
3988 free(yca_id);
3989 if (commit)
3990 got_object_commit_close(commit);
3991 if (branch)
3992 got_ref_close(branch);
3993 if (new_base_branch)
3994 got_ref_close(new_base_branch);
3995 if (tmp_branch)
3996 got_ref_close(tmp_branch);
3997 if (worktree)
3998 got_worktree_close(worktree);
3999 if (repo)
4000 got_repo_close(repo);
4001 return error;
4004 __dead static void
4005 usage_histedit(void)
4007 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4008 getprogname());
4009 exit(1);
4012 #define GOT_HISTEDIT_PICK 'p'
4013 #define GOT_HISTEDIT_EDIT 'e'
4014 #define GOT_HISTEDIT_FOLD 'f'
4015 #define GOT_HISTEDIT_DROP 'd'
4016 #define GOT_HISTEDIT_MESG 'm'
4018 static struct got_histedit_cmd {
4019 unsigned char code;
4020 const char *name;
4021 const char *desc;
4022 } got_histedit_cmds[] = {
4023 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4024 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4025 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4026 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4027 { GOT_HISTEDIT_MESG, "mesg",
4028 "single-line log message for commit above (open editor if empty)" },
4031 struct got_histedit_list_entry {
4032 TAILQ_ENTRY(got_histedit_list_entry) entry;
4033 struct got_object_id *commit_id;
4034 const struct got_histedit_cmd *cmd;
4035 char *logmsg;
4037 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4039 static const struct got_error *
4040 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4041 FILE *f, struct got_repository *repo)
4043 const struct got_error *err = NULL;
4044 char *logmsg = NULL, *id_str = NULL;
4045 struct got_commit_object *commit = NULL;
4046 size_t n;
4048 err = got_object_open_as_commit(&commit, repo, commit_id);
4049 if (err)
4050 goto done;
4052 err = get_short_logmsg(&logmsg, 34, commit);
4053 if (err)
4054 goto done;
4056 err = got_object_id_str(&id_str, commit_id);
4057 if (err)
4058 goto done;
4060 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4061 if (n < 0)
4062 err = got_ferror(f, GOT_ERR_IO);
4063 done:
4064 if (commit)
4065 got_object_commit_close(commit);
4066 free(id_str);
4067 free(logmsg);
4068 return err;
4071 static const struct got_error *
4072 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4073 struct got_repository *repo)
4075 const struct got_error *err = NULL;
4076 struct got_object_qid *qid;
4078 if (SIMPLEQ_EMPTY(commits))
4079 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4081 SIMPLEQ_FOREACH(qid, commits, entry) {
4082 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4083 f, repo);
4084 if (err)
4085 break;
4088 return err;
4091 static const struct got_error *
4092 write_cmd_list(FILE *f)
4094 const struct got_error *err = NULL;
4095 int n, i;
4097 n = fprintf(f, "# Available histedit commands:\n");
4098 if (n < 0)
4099 return got_ferror(f, GOT_ERR_IO);
4101 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4102 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4103 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4104 cmd->desc);
4105 if (n < 0) {
4106 err = got_ferror(f, GOT_ERR_IO);
4107 break;
4110 n = fprintf(f, "# Commits will be processed in order from top to "
4111 "bottom of this file.\n");
4112 if (n < 0)
4113 return got_ferror(f, GOT_ERR_IO);
4114 return err;
4117 static const struct got_error *
4118 histedit_syntax_error(int lineno)
4120 static char msg[42];
4121 int ret;
4123 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4124 lineno);
4125 if (ret == -1 || ret >= sizeof(msg))
4126 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4128 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4131 static const struct got_error *
4132 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4133 char *logmsg, struct got_repository *repo)
4135 const struct got_error *err;
4136 struct got_commit_object *folded_commit = NULL;
4137 char *id_str;
4139 err = got_object_id_str(&id_str, hle->commit_id);
4140 if (err)
4141 return err;
4143 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4144 if (err)
4145 goto done;
4147 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4148 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4149 got_object_commit_get_logmsg(folded_commit)) == -1) {
4150 err = got_error_from_errno("asprintf");
4151 goto done;
4153 done:
4154 if (folded_commit)
4155 got_object_commit_close(folded_commit);
4156 free(id_str);
4157 return err;
4160 static struct got_histedit_list_entry *
4161 get_folded_commits(struct got_histedit_list_entry *hle)
4163 struct got_histedit_list_entry *prev, *folded = NULL;
4165 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4166 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4167 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4168 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4169 folded = prev;
4170 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4173 return folded;
4176 static const struct got_error *
4177 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4178 struct got_repository *repo)
4180 char *logmsg_path = NULL, *id_str = NULL;
4181 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4182 const struct got_error *err = NULL;
4183 struct got_commit_object *commit = NULL;
4184 int fd;
4185 struct got_histedit_list_entry *folded = NULL;
4187 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4188 if (err)
4189 return err;
4191 folded = get_folded_commits(hle);
4192 if (folded) {
4193 while (folded != hle) {
4194 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4195 folded = TAILQ_NEXT(folded, entry);
4196 continue;
4198 err = append_folded_commit_msg(&new_msg, folded,
4199 logmsg, repo);
4200 if (err)
4201 goto done;
4202 free(logmsg);
4203 logmsg = new_msg;
4204 folded = TAILQ_NEXT(folded, entry);
4208 err = got_object_id_str(&id_str, hle->commit_id);
4209 if (err)
4210 goto done;
4211 if (asprintf(&new_msg,
4212 "%s\n# original log message of commit %s: %s",
4213 logmsg ? logmsg : "", id_str,
4214 got_object_commit_get_logmsg(commit)) == -1) {
4215 err = got_error_from_errno("asprintf");
4216 goto done;
4218 free(logmsg);
4219 logmsg = new_msg;
4221 err = got_object_id_str(&id_str, hle->commit_id);
4222 if (err)
4223 goto done;
4225 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4226 if (err)
4227 goto done;
4229 dprintf(fd, logmsg);
4230 close(fd);
4232 err = get_editor(&editor);
4233 if (err)
4234 goto done;
4236 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4237 if (err) {
4238 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4239 goto done;
4240 err = NULL;
4241 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4242 if (hle->logmsg == NULL)
4243 err = got_error_from_errno("strdup");
4245 done:
4246 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4247 err = got_error_from_errno2("unlink", logmsg_path);
4248 free(logmsg_path);
4249 free(logmsg);
4250 free(editor);
4251 if (commit)
4252 got_object_commit_close(commit);
4253 return err;
4256 static const struct got_error *
4257 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4258 FILE *f, struct got_repository *repo)
4260 const struct got_error *err = NULL;
4261 char *line = NULL, *p, *end;
4262 size_t size;
4263 ssize_t len;
4264 int lineno = 0, i;
4265 const struct got_histedit_cmd *cmd;
4266 struct got_object_id *commit_id = NULL;
4267 struct got_histedit_list_entry *hle = NULL;
4269 for (;;) {
4270 len = getline(&line, &size, f);
4271 if (len == -1) {
4272 const struct got_error *getline_err;
4273 if (feof(f))
4274 break;
4275 getline_err = got_error_from_errno("getline");
4276 err = got_ferror(f, getline_err->code);
4277 break;
4279 lineno++;
4280 p = line;
4281 while (isspace((unsigned char)p[0]))
4282 p++;
4283 if (p[0] == '#' || p[0] == '\0') {
4284 free(line);
4285 line = NULL;
4286 continue;
4288 cmd = NULL;
4289 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4290 cmd = &got_histedit_cmds[i];
4291 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4292 isspace((unsigned char)p[strlen(cmd->name)])) {
4293 p += strlen(cmd->name);
4294 break;
4296 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4297 p++;
4298 break;
4301 if (i == nitems(got_histedit_cmds)) {
4302 err = histedit_syntax_error(lineno);
4303 break;
4305 while (isspace((unsigned char)p[0]))
4306 p++;
4307 if (cmd->code == GOT_HISTEDIT_MESG) {
4308 if (hle == NULL || hle->logmsg != NULL) {
4309 err = got_error(GOT_ERR_HISTEDIT_CMD);
4310 break;
4312 if (p[0] == '\0') {
4313 err = histedit_edit_logmsg(hle, repo);
4314 if (err)
4315 break;
4316 } else {
4317 hle->logmsg = strdup(p);
4318 if (hle->logmsg == NULL) {
4319 err = got_error_from_errno("strdup");
4320 break;
4323 free(line);
4324 line = NULL;
4325 continue;
4326 } else {
4327 end = p;
4328 while (end[0] && !isspace((unsigned char)end[0]))
4329 end++;
4330 *end = '\0';
4332 err = got_object_resolve_id_str(&commit_id, repo, p);
4333 if (err) {
4334 /* override error code */
4335 err = histedit_syntax_error(lineno);
4336 break;
4339 hle = malloc(sizeof(*hle));
4340 if (hle == NULL) {
4341 err = got_error_from_errno("malloc");
4342 break;
4344 hle->cmd = cmd;
4345 hle->commit_id = commit_id;
4346 hle->logmsg = NULL;
4347 commit_id = NULL;
4348 free(line);
4349 line = NULL;
4350 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4353 free(line);
4354 free(commit_id);
4355 return err;
4358 static const struct got_error *
4359 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4360 const char *path, struct got_repository *repo)
4362 const struct got_error *err = NULL;
4363 char *editor;
4364 FILE *f = NULL;
4366 err = get_editor(&editor);
4367 if (err)
4368 return err;
4370 if (spawn_editor(editor, path) == -1) {
4371 err = got_error_from_errno("failed spawning editor");
4372 goto done;
4375 f = fopen(path, "r");
4376 if (f == NULL) {
4377 err = got_error_from_errno("fopen");
4378 goto done;
4380 err = histedit_parse_list(histedit_cmds, f, repo);
4381 done:
4382 if (f && fclose(f) != 0 && err == NULL)
4383 err = got_error_from_errno("fclose");
4384 free(editor);
4385 return err;
4388 static const struct got_error *
4389 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4390 struct got_object_id_queue *, const char *, struct got_repository *);
4392 static const struct got_error *
4393 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4394 struct got_object_id_queue *commits, struct got_repository *repo)
4396 const struct got_error *err;
4397 FILE *f = NULL;
4398 char *path = NULL;
4400 err = got_opentemp_named(&path, &f, "got-histedit");
4401 if (err)
4402 return err;
4404 err = write_cmd_list(f);
4405 if (err)
4406 goto done;
4408 err = histedit_write_commit_list(commits, f, repo);
4409 if (err)
4410 goto done;
4412 if (fclose(f) != 0) {
4413 err = got_error_from_errno("fclose");
4414 goto done;
4416 f = NULL;
4418 err = histedit_run_editor(histedit_cmds, path, repo);
4419 if (err) {
4420 const char *errmsg = err->msg;
4421 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4422 goto done;
4423 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4424 commits, path, repo);
4426 done:
4427 if (f && fclose(f) != 0 && err == NULL)
4428 err = got_error_from_errno("fclose");
4429 if (path && unlink(path) != 0 && err == NULL)
4430 err = got_error_from_errno2("unlink", path);
4431 free(path);
4432 return err;
4435 static const struct got_error *
4436 histedit_save_list(struct got_histedit_list *histedit_cmds,
4437 struct got_worktree *worktree, struct got_repository *repo)
4439 const struct got_error *err = NULL;
4440 char *path = NULL;
4441 FILE *f = NULL;
4442 struct got_histedit_list_entry *hle;
4443 struct got_commit_object *commit = NULL;
4445 err = got_worktree_get_histedit_list_path(&path, worktree);
4446 if (err)
4447 return err;
4449 f = fopen(path, "w");
4450 if (f == NULL) {
4451 err = got_error_from_errno2("fopen", path);
4452 goto done;
4454 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4455 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4456 repo);
4457 if (err)
4458 break;
4460 if (hle->logmsg) {
4461 int n = fprintf(f, "%c %s\n",
4462 GOT_HISTEDIT_MESG, hle->logmsg);
4463 if (n < 0) {
4464 err = got_ferror(f, GOT_ERR_IO);
4465 break;
4469 done:
4470 if (f && fclose(f) != 0 && err == NULL)
4471 err = got_error_from_errno("fclose");
4472 free(path);
4473 if (commit)
4474 got_object_commit_close(commit);
4475 return err;
4478 static const struct got_error *
4479 histedit_load_list(struct got_histedit_list *histedit_cmds,
4480 const char *path, struct got_repository *repo)
4482 const struct got_error *err = NULL;
4483 FILE *f = NULL;
4485 f = fopen(path, "r");
4486 if (f == NULL) {
4487 err = got_error_from_errno2("fopen", path);
4488 goto done;
4491 err = histedit_parse_list(histedit_cmds, f, repo);
4492 done:
4493 if (f && fclose(f) != 0 && err == NULL)
4494 err = got_error_from_errno("fclose");
4495 return err;
4498 static const struct got_error *
4499 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4500 const char *errmsg, struct got_object_id_queue *commits,
4501 const char *path, struct got_repository *repo)
4503 const struct got_error *err = NULL;
4504 int resp = ' ';
4506 while (resp != 'c' && resp != 'r' && resp != 'a') {
4507 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4508 "or (a)bort: ", getprogname(), errmsg);
4509 resp = getchar();
4510 if (resp == 'c') {
4511 err = histedit_run_editor(histedit_cmds, path, repo);
4512 if (err) {
4513 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4514 break;
4515 resp = ' ';
4516 continue;
4518 } else if (resp == 'r') {
4519 err = histedit_edit_script(histedit_cmds,
4520 commits, repo);
4521 if (err) {
4522 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4523 break;
4524 resp = ' ';
4525 continue;
4527 } else if (resp == 'a') {
4528 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4529 break;
4530 } else
4531 printf("invalid response '%c'\n", resp);
4534 return err;
4537 static const struct got_error *
4538 histedit_complete(struct got_worktree *worktree,
4539 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4540 struct got_reference *branch, struct got_repository *repo)
4542 printf("Switching work tree to %s\n",
4543 got_ref_get_symref_target(branch));
4544 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4545 branch, repo);
4548 static const struct got_error *
4549 show_histedit_progress(struct got_commit_object *commit,
4550 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4552 const struct got_error *err;
4553 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4555 err = got_object_id_str(&old_id_str, hle->commit_id);
4556 if (err)
4557 goto done;
4559 if (new_id) {
4560 err = got_object_id_str(&new_id_str, new_id);
4561 if (err)
4562 goto done;
4565 old_id_str[12] = '\0';
4566 if (new_id_str)
4567 new_id_str[12] = '\0';
4569 if (hle->logmsg) {
4570 logmsg = strdup(hle->logmsg);
4571 if (logmsg == NULL) {
4572 err = got_error_from_errno("strdup");
4573 goto done;
4575 trim_logmsg(logmsg, 42);
4576 } else {
4577 err = get_short_logmsg(&logmsg, 42, commit);
4578 if (err)
4579 goto done;
4582 switch (hle->cmd->code) {
4583 case GOT_HISTEDIT_PICK:
4584 case GOT_HISTEDIT_EDIT:
4585 printf("%s -> %s: %s\n", old_id_str,
4586 new_id_str ? new_id_str : "no-op change", logmsg);
4587 break;
4588 case GOT_HISTEDIT_DROP:
4589 case GOT_HISTEDIT_FOLD:
4590 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4591 logmsg);
4592 break;
4593 default:
4594 break;
4597 done:
4598 free(old_id_str);
4599 free(new_id_str);
4600 return err;
4603 static const struct got_error *
4604 histedit_commit(struct got_pathlist_head *merged_paths,
4605 struct got_worktree *worktree, struct got_fileindex *fileindex,
4606 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4607 struct got_repository *repo)
4609 const struct got_error *err;
4610 struct got_commit_object *commit;
4611 struct got_object_id *new_commit_id;
4613 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4614 && hle->logmsg == NULL) {
4615 err = histedit_edit_logmsg(hle, repo);
4616 if (err)
4617 return err;
4620 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4621 if (err)
4622 return err;
4624 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4625 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4626 hle->logmsg, repo);
4627 if (err) {
4628 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4629 goto done;
4630 err = show_histedit_progress(commit, hle, NULL);
4631 } else {
4632 err = show_histedit_progress(commit, hle, new_commit_id);
4633 free(new_commit_id);
4635 done:
4636 got_object_commit_close(commit);
4637 return err;
4640 static const struct got_error *
4641 histedit_skip_commit(struct got_histedit_list_entry *hle,
4642 struct got_worktree *worktree, struct got_repository *repo)
4644 const struct got_error *error;
4645 struct got_commit_object *commit;
4647 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4648 repo);
4649 if (error)
4650 return error;
4652 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4653 if (error)
4654 return error;
4656 error = show_histedit_progress(commit, hle, NULL);
4657 got_object_commit_close(commit);
4658 return error;
4661 static const struct got_error *
4662 histedit_check_script(struct got_histedit_list *histedit_cmds,
4663 struct got_object_id_queue *commits, struct got_repository *repo)
4665 const struct got_error *err = NULL;
4666 struct got_object_qid *qid;
4667 struct got_histedit_list_entry *hle;
4668 static char msg[80];
4669 char *id_str;
4671 if (TAILQ_EMPTY(histedit_cmds))
4672 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4673 "histedit script contains no commands");
4675 SIMPLEQ_FOREACH(qid, commits, entry) {
4676 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4677 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4678 break;
4680 if (hle == NULL) {
4681 err = got_object_id_str(&id_str, qid->id);
4682 if (err)
4683 return err;
4684 snprintf(msg, sizeof(msg),
4685 "commit %s missing from histedit script", id_str);
4686 free(id_str);
4687 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4691 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4692 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4693 "last commit in histedit script cannot be folded");
4695 return NULL;
4698 static const struct got_error *
4699 cmd_histedit(int argc, char *argv[])
4701 const struct got_error *error = NULL;
4702 struct got_worktree *worktree = NULL;
4703 struct got_fileindex *fileindex = NULL;
4704 struct got_repository *repo = NULL;
4705 char *cwd = NULL;
4706 struct got_reference *branch = NULL;
4707 struct got_reference *tmp_branch = NULL;
4708 struct got_object_id *resume_commit_id = NULL;
4709 struct got_object_id *base_commit_id = NULL;
4710 struct got_object_id *head_commit_id = NULL;
4711 struct got_commit_object *commit = NULL;
4712 int ch, rebase_in_progress = 0;
4713 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4714 const char *edit_script_path = NULL;
4715 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4716 struct got_object_id_queue commits;
4717 struct got_pathlist_head merged_paths;
4718 const struct got_object_id_queue *parent_ids;
4719 struct got_object_qid *pid;
4720 struct got_histedit_list histedit_cmds;
4721 struct got_histedit_list_entry *hle;
4723 SIMPLEQ_INIT(&commits);
4724 TAILQ_INIT(&histedit_cmds);
4725 TAILQ_INIT(&merged_paths);
4727 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4728 switch (ch) {
4729 case 'a':
4730 abort_edit = 1;
4731 break;
4732 case 'c':
4733 continue_edit = 1;
4734 break;
4735 case 'F':
4736 edit_script_path = optarg;
4737 break;
4738 default:
4739 usage_histedit();
4740 /* NOTREACHED */
4744 argc -= optind;
4745 argv += optind;
4747 #ifndef PROFILE
4748 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4749 "unveil", NULL) == -1)
4750 err(1, "pledge");
4751 #endif
4752 if (abort_edit && continue_edit)
4753 usage_histedit();
4754 if (argc != 0)
4755 usage_histedit();
4758 * This command cannot apply unveil(2) in all cases because the
4759 * user may choose to run an editor to edit the histedit script
4760 * and to edit individual commit log messages.
4761 * unveil(2) traverses exec(2); if an editor is used we have to
4762 * apply unveil after edit script and log messages have been written.
4763 * XXX TODO: Make use of unveil(2) where possible.
4766 cwd = getcwd(NULL, 0);
4767 if (cwd == NULL) {
4768 error = got_error_from_errno("getcwd");
4769 goto done;
4771 error = got_worktree_open(&worktree, cwd);
4772 if (error)
4773 goto done;
4775 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4776 if (error != NULL)
4777 goto done;
4779 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4780 if (error)
4781 goto done;
4782 if (rebase_in_progress) {
4783 error = got_error(GOT_ERR_REBASING);
4784 goto done;
4787 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4788 if (error)
4789 goto done;
4791 if (edit_in_progress && abort_edit) {
4792 int did_something;
4793 error = got_worktree_histedit_continue(&resume_commit_id,
4794 &tmp_branch, &branch, &base_commit_id, &fileindex,
4795 worktree, repo);
4796 if (error)
4797 goto done;
4798 printf("Switching work tree to %s\n",
4799 got_ref_get_symref_target(branch));
4800 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4801 branch, base_commit_id, update_progress, &did_something);
4802 if (error)
4803 goto done;
4804 printf("Histedit of %s aborted\n",
4805 got_ref_get_symref_target(branch));
4806 goto done; /* nothing else to do */
4807 } else if (abort_edit) {
4808 error = got_error(GOT_ERR_NOT_HISTEDIT);
4809 goto done;
4812 if (continue_edit) {
4813 char *path;
4815 if (!edit_in_progress) {
4816 error = got_error(GOT_ERR_NOT_HISTEDIT);
4817 goto done;
4820 error = got_worktree_get_histedit_list_path(&path, worktree);
4821 if (error)
4822 goto done;
4824 error = histedit_load_list(&histedit_cmds, path, repo);
4825 free(path);
4826 if (error)
4827 goto done;
4829 error = got_worktree_histedit_continue(&resume_commit_id,
4830 &tmp_branch, &branch, &base_commit_id, &fileindex,
4831 worktree, repo);
4832 if (error)
4833 goto done;
4835 error = got_ref_resolve(&head_commit_id, repo, branch);
4836 if (error)
4837 goto done;
4839 error = got_object_open_as_commit(&commit, repo,
4840 head_commit_id);
4841 if (error)
4842 goto done;
4843 parent_ids = got_object_commit_get_parent_ids(commit);
4844 pid = SIMPLEQ_FIRST(parent_ids);
4845 if (pid == NULL) {
4846 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4847 goto done;
4849 error = collect_commits(&commits, head_commit_id, pid->id,
4850 base_commit_id, got_worktree_get_path_prefix(worktree),
4851 GOT_ERR_HISTEDIT_PATH, repo);
4852 got_object_commit_close(commit);
4853 commit = NULL;
4854 if (error)
4855 goto done;
4856 } else {
4857 if (edit_in_progress) {
4858 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4859 goto done;
4862 error = got_ref_open(&branch, repo,
4863 got_worktree_get_head_ref_name(worktree), 0);
4864 if (error != NULL)
4865 goto done;
4867 error = got_ref_resolve(&head_commit_id, repo, branch);
4868 if (error)
4869 goto done;
4871 error = got_object_open_as_commit(&commit, repo,
4872 head_commit_id);
4873 if (error)
4874 goto done;
4875 parent_ids = got_object_commit_get_parent_ids(commit);
4876 pid = SIMPLEQ_FIRST(parent_ids);
4877 if (pid == NULL) {
4878 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4879 goto done;
4881 error = collect_commits(&commits, head_commit_id, pid->id,
4882 got_worktree_get_base_commit_id(worktree),
4883 got_worktree_get_path_prefix(worktree),
4884 GOT_ERR_HISTEDIT_PATH, repo);
4885 got_object_commit_close(commit);
4886 commit = NULL;
4887 if (error)
4888 goto done;
4890 if (edit_script_path) {
4891 error = histedit_load_list(&histedit_cmds,
4892 edit_script_path, repo);
4893 if (error)
4894 goto done;
4895 } else {
4896 error = histedit_edit_script(&histedit_cmds, &commits,
4897 repo);
4898 if (error)
4899 goto done;
4903 error = histedit_save_list(&histedit_cmds, worktree,
4904 repo);
4905 if (error)
4906 goto done;
4908 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4909 &base_commit_id, &fileindex, worktree, repo);
4910 if (error)
4911 goto done;
4915 error = histedit_check_script(&histedit_cmds, &commits, repo);
4916 if (error)
4917 goto done;
4919 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4920 if (resume_commit_id) {
4921 if (got_object_id_cmp(hle->commit_id,
4922 resume_commit_id) != 0)
4923 continue;
4925 resume_commit_id = NULL;
4926 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4927 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4928 error = histedit_skip_commit(hle, worktree,
4929 repo);
4930 } else {
4931 error = histedit_commit(NULL, worktree,
4932 fileindex, tmp_branch, hle, repo);
4934 if (error)
4935 goto done;
4936 continue;
4939 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4940 error = histedit_skip_commit(hle, worktree, repo);
4941 if (error)
4942 goto done;
4943 continue;
4946 error = got_object_open_as_commit(&commit, repo,
4947 hle->commit_id);
4948 if (error)
4949 goto done;
4950 parent_ids = got_object_commit_get_parent_ids(commit);
4951 pid = SIMPLEQ_FIRST(parent_ids);
4953 error = got_worktree_histedit_merge_files(&merged_paths,
4954 worktree, fileindex, pid->id, hle->commit_id, repo,
4955 rebase_progress, &rebase_status, check_cancelled, NULL);
4956 if (error)
4957 goto done;
4958 got_object_commit_close(commit);
4959 commit = NULL;
4961 if (rebase_status == GOT_STATUS_CONFLICT) {
4962 got_worktree_rebase_pathlist_free(&merged_paths);
4963 break;
4966 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4967 char *id_str;
4968 error = got_object_id_str(&id_str, hle->commit_id);
4969 if (error)
4970 goto done;
4971 printf("Stopping histedit for amending commit %s\n",
4972 id_str);
4973 free(id_str);
4974 got_worktree_rebase_pathlist_free(&merged_paths);
4975 error = got_worktree_histedit_postpone(worktree,
4976 fileindex);
4977 goto done;
4980 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4981 error = histedit_skip_commit(hle, worktree, repo);
4982 if (error)
4983 goto done;
4984 continue;
4987 error = histedit_commit(&merged_paths, worktree, fileindex,
4988 tmp_branch, hle, repo);
4989 got_worktree_rebase_pathlist_free(&merged_paths);
4990 if (error)
4991 goto done;
4994 if (rebase_status == GOT_STATUS_CONFLICT) {
4995 error = got_worktree_histedit_postpone(worktree, fileindex);
4996 if (error)
4997 goto done;
4998 error = got_error_msg(GOT_ERR_CONFLICTS,
4999 "conflicts must be resolved before rebasing can continue");
5000 } else
5001 error = histedit_complete(worktree, fileindex, tmp_branch,
5002 branch, repo);
5003 done:
5004 got_object_id_queue_free(&commits);
5005 free(head_commit_id);
5006 free(base_commit_id);
5007 free(resume_commit_id);
5008 if (commit)
5009 got_object_commit_close(commit);
5010 if (branch)
5011 got_ref_close(branch);
5012 if (tmp_branch)
5013 got_ref_close(tmp_branch);
5014 if (worktree)
5015 got_worktree_close(worktree);
5016 if (repo)
5017 got_repo_close(repo);
5018 return error;