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 if (error)
1133 goto done;
1134 error = got_object_id_str(&commit_id_str, commit_id);
1135 if (error)
1136 goto done;
1139 if (branch_name) {
1140 struct got_object_id *head_commit_id;
1141 if (strlen(path) != 0) {
1142 fprintf(stderr, "%s: switching between branches "
1143 "requires that the entire work tree "
1144 "gets updated, not just '%s'\n",
1145 getprogname(), path);
1146 error = got_error(GOT_ERR_BAD_PATH);
1147 goto done;
1149 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1150 if (error)
1151 goto done;
1152 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1153 free(head_commit_id);
1154 if (error != NULL)
1155 goto done;
1156 error = check_same_branch(commit_id, head_ref, repo);
1157 if (error)
1158 goto done;
1159 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1160 if (error)
1161 goto done;
1162 } else {
1163 error = check_linear_ancestry(commit_id,
1164 got_worktree_get_base_commit_id(worktree), repo);
1165 if (error != NULL) {
1166 if (error->code == GOT_ERR_ANCESTRY)
1167 error = got_error(GOT_ERR_BRANCH_MOVED);
1168 goto done;
1170 error = check_same_branch(commit_id, head_ref, repo);
1171 if (error)
1172 goto done;
1175 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1176 commit_id) != 0) {
1177 error = got_worktree_set_base_commit_id(worktree, repo,
1178 commit_id);
1179 if (error)
1180 goto done;
1183 error = got_worktree_checkout_files(worktree, path, repo,
1184 update_progress, &did_something, check_cancelled, NULL);
1185 if (error != NULL)
1186 goto done;
1188 if (did_something)
1189 printf("Updated to commit %s\n", commit_id_str);
1190 else
1191 printf("Already up-to-date\n");
1192 done:
1193 free(worktree_path);
1194 free(path);
1195 free(commit_id);
1196 free(commit_id_str);
1197 return error;
1200 static const struct got_error *
1201 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1202 int diff_context, struct got_repository *repo)
1204 const struct got_error *err = NULL;
1205 struct got_tree_object *tree1 = NULL, *tree2;
1206 struct got_object_qid *qid;
1207 char *id_str1 = NULL, *id_str2;
1208 struct got_diff_blob_output_unidiff_arg arg;
1210 err = got_object_open_as_tree(&tree2, repo,
1211 got_object_commit_get_tree_id(commit));
1212 if (err)
1213 return err;
1215 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1216 if (qid != NULL) {
1217 struct got_commit_object *pcommit;
1219 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1220 if (err)
1221 return err;
1223 err = got_object_open_as_tree(&tree1, repo,
1224 got_object_commit_get_tree_id(pcommit));
1225 got_object_commit_close(pcommit);
1226 if (err)
1227 return err;
1229 err = got_object_id_str(&id_str1, qid->id);
1230 if (err)
1231 return err;
1234 err = got_object_id_str(&id_str2, id);
1235 if (err)
1236 goto done;
1238 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1239 arg.diff_context = diff_context;
1240 arg.outfile = stdout;
1241 err = got_diff_tree(tree1, tree2, "", "", repo,
1242 got_diff_blob_output_unidiff, &arg);
1243 done:
1244 if (tree1)
1245 got_object_tree_close(tree1);
1246 got_object_tree_close(tree2);
1247 free(id_str1);
1248 free(id_str2);
1249 return err;
1252 static char *
1253 get_datestr(time_t *time, char *datebuf)
1255 char *p, *s = ctime_r(time, datebuf);
1256 p = strchr(s, '\n');
1257 if (p)
1258 *p = '\0';
1259 return s;
1262 static const struct got_error *
1263 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1264 struct got_repository *repo, int show_patch, int diff_context,
1265 struct got_reflist_head *refs)
1267 const struct got_error *err = NULL;
1268 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1269 char datebuf[26];
1270 time_t committer_time;
1271 const char *author, *committer;
1272 char *refs_str = NULL;
1273 struct got_reflist_entry *re;
1275 SIMPLEQ_FOREACH(re, refs, entry) {
1276 char *s;
1277 const char *name;
1278 if (got_object_id_cmp(re->id, id) != 0)
1279 continue;
1280 name = got_ref_get_name(re->ref);
1281 if (strcmp(name, GOT_REF_HEAD) == 0)
1282 continue;
1283 if (strncmp(name, "refs/", 5) == 0)
1284 name += 5;
1285 if (strncmp(name, "got/", 4) == 0)
1286 continue;
1287 if (strncmp(name, "heads/", 6) == 0)
1288 name += 6;
1289 if (strncmp(name, "remotes/", 8) == 0)
1290 name += 8;
1291 s = refs_str;
1292 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1293 name) == -1) {
1294 err = got_error_from_errno("asprintf");
1295 free(s);
1296 break;
1298 free(s);
1300 err = got_object_id_str(&id_str, id);
1301 if (err)
1302 return err;
1304 printf("-----------------------------------------------\n");
1305 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1306 refs_str ? refs_str : "", refs_str ? ")" : "");
1307 free(id_str);
1308 id_str = NULL;
1309 free(refs_str);
1310 refs_str = NULL;
1311 printf("from: %s\n", got_object_commit_get_author(commit));
1312 committer_time = got_object_commit_get_committer_time(commit);
1313 datestr = get_datestr(&committer_time, datebuf);
1314 printf("date: %s UTC\n", datestr);
1315 author = got_object_commit_get_author(commit);
1316 committer = got_object_commit_get_committer(commit);
1317 if (strcmp(author, committer) != 0)
1318 printf("via: %s\n", committer);
1319 if (got_object_commit_get_nparents(commit) > 1) {
1320 const struct got_object_id_queue *parent_ids;
1321 struct got_object_qid *qid;
1322 int n = 1;
1323 parent_ids = got_object_commit_get_parent_ids(commit);
1324 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1325 err = got_object_id_str(&id_str, qid->id);
1326 if (err)
1327 return err;
1328 printf("parent %d: %s\n", n++, id_str);
1329 free(id_str);
1333 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1334 if (logmsg0 == NULL)
1335 return got_error_from_errno("strdup");
1337 logmsg = logmsg0;
1338 do {
1339 line = strsep(&logmsg, "\n");
1340 if (line)
1341 printf(" %s\n", line);
1342 } while (line);
1343 free(logmsg0);
1345 if (show_patch) {
1346 err = print_patch(commit, id, diff_context, repo);
1347 if (err == 0)
1348 printf("\n");
1351 if (fflush(stdout) != 0 && err == NULL)
1352 err = got_error_from_errno("fflush");
1353 return err;
1356 static const struct got_error *
1357 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1358 char *path, int show_patch, int diff_context, int limit,
1359 int first_parent_traversal, struct got_reflist_head *refs)
1361 const struct got_error *err;
1362 struct got_commit_graph *graph;
1364 err = got_commit_graph_open(&graph, root_id, path,
1365 first_parent_traversal, repo);
1366 if (err)
1367 return err;
1368 err = got_commit_graph_iter_start(graph, root_id, repo);
1369 if (err)
1370 goto done;
1371 for (;;) {
1372 struct got_commit_object *commit;
1373 struct got_object_id *id;
1375 if (sigint_received || sigpipe_received)
1376 break;
1378 err = got_commit_graph_iter_next(&id, graph);
1379 if (err) {
1380 if (err->code == GOT_ERR_ITER_COMPLETED) {
1381 err = NULL;
1382 break;
1384 if (err->code != GOT_ERR_ITER_NEED_MORE)
1385 break;
1386 err = got_commit_graph_fetch_commits(graph, 1, repo);
1387 if (err)
1388 break;
1389 else
1390 continue;
1392 if (id == NULL)
1393 break;
1395 err = got_object_open_as_commit(&commit, repo, id);
1396 if (err)
1397 break;
1398 err = print_commit(commit, id, repo, show_patch, diff_context,
1399 refs);
1400 got_object_commit_close(commit);
1401 if (err || (limit && --limit == 0))
1402 break;
1404 done:
1405 got_commit_graph_close(graph);
1406 return err;
1409 __dead static void
1410 usage_log(void)
1412 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1413 "[-r repository-path] [path]\n", getprogname());
1414 exit(1);
1417 static const struct got_error *
1418 cmd_log(int argc, char *argv[])
1420 const struct got_error *error;
1421 struct got_repository *repo = NULL;
1422 struct got_worktree *worktree = NULL;
1423 struct got_commit_object *commit = NULL;
1424 struct got_object_id *id = NULL;
1425 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1426 char *start_commit = NULL;
1427 int diff_context = 3, ch;
1428 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1429 const char *errstr;
1430 struct got_reflist_head refs;
1432 SIMPLEQ_INIT(&refs);
1434 #ifndef PROFILE
1435 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1436 NULL)
1437 == -1)
1438 err(1, "pledge");
1439 #endif
1441 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1442 switch (ch) {
1443 case 'p':
1444 show_patch = 1;
1445 break;
1446 case 'c':
1447 start_commit = optarg;
1448 break;
1449 case 'C':
1450 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1451 &errstr);
1452 if (errstr != NULL)
1453 err(1, "-C option %s", errstr);
1454 break;
1455 case 'l':
1456 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1457 if (errstr != NULL)
1458 err(1, "-l option %s", errstr);
1459 break;
1460 case 'f':
1461 first_parent_traversal = 1;
1462 break;
1463 case 'r':
1464 repo_path = realpath(optarg, NULL);
1465 if (repo_path == NULL)
1466 err(1, "-r option");
1467 got_path_strip_trailing_slashes(repo_path);
1468 break;
1469 default:
1470 usage_log();
1471 /* NOTREACHED */
1475 argc -= optind;
1476 argv += optind;
1478 cwd = getcwd(NULL, 0);
1479 if (cwd == NULL) {
1480 error = got_error_from_errno("getcwd");
1481 goto done;
1484 error = got_worktree_open(&worktree, cwd);
1485 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1486 goto done;
1487 error = NULL;
1489 if (argc == 0) {
1490 path = strdup("");
1491 if (path == NULL) {
1492 error = got_error_from_errno("strdup");
1493 goto done;
1495 } else if (argc == 1) {
1496 if (worktree) {
1497 error = got_worktree_resolve_path(&path, worktree,
1498 argv[0]);
1499 if (error)
1500 goto done;
1501 } else {
1502 path = strdup(argv[0]);
1503 if (path == NULL) {
1504 error = got_error_from_errno("strdup");
1505 goto done;
1508 } else
1509 usage_log();
1511 if (repo_path == NULL) {
1512 repo_path = worktree ?
1513 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1515 if (repo_path == NULL) {
1516 error = got_error_from_errno("strdup");
1517 goto done;
1520 error = got_repo_open(&repo, repo_path);
1521 if (error != NULL)
1522 goto done;
1524 error = apply_unveil(got_repo_get_path(repo), 1,
1525 worktree ? got_worktree_get_root_path(worktree) : NULL);
1526 if (error)
1527 goto done;
1529 if (start_commit == NULL) {
1530 struct got_reference *head_ref;
1531 error = got_ref_open(&head_ref, repo,
1532 worktree ? got_worktree_get_head_ref_name(worktree)
1533 : GOT_REF_HEAD, 0);
1534 if (error != NULL)
1535 return error;
1536 error = got_ref_resolve(&id, repo, head_ref);
1537 got_ref_close(head_ref);
1538 if (error != NULL)
1539 return error;
1540 error = got_object_open_as_commit(&commit, repo, id);
1541 } else {
1542 struct got_reference *ref;
1543 error = got_ref_open(&ref, repo, start_commit, 0);
1544 if (error == NULL) {
1545 int obj_type;
1546 error = got_ref_resolve(&id, repo, ref);
1547 got_ref_close(ref);
1548 if (error != NULL)
1549 goto done;
1550 error = got_object_get_type(&obj_type, repo, id);
1551 if (error != NULL)
1552 goto done;
1553 if (obj_type == GOT_OBJ_TYPE_TAG) {
1554 struct got_tag_object *tag;
1555 error = got_object_open_as_tag(&tag, repo, id);
1556 if (error != NULL)
1557 goto done;
1558 if (got_object_tag_get_object_type(tag) !=
1559 GOT_OBJ_TYPE_COMMIT) {
1560 got_object_tag_close(tag);
1561 error = got_error(GOT_ERR_OBJ_TYPE);
1562 goto done;
1564 free(id);
1565 id = got_object_id_dup(
1566 got_object_tag_get_object_id(tag));
1567 if (id == NULL)
1568 error = got_error_from_errno(
1569 "got_object_id_dup");
1570 got_object_tag_close(tag);
1571 if (error)
1572 goto done;
1573 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1574 error = got_error(GOT_ERR_OBJ_TYPE);
1575 goto done;
1577 error = got_object_open_as_commit(&commit, repo, id);
1578 if (error != NULL)
1579 goto done;
1581 if (commit == NULL) {
1582 error = got_repo_match_object_id_prefix(&id,
1583 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1584 if (error != NULL)
1585 return error;
1588 if (error != NULL)
1589 goto done;
1591 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1592 if (error != NULL)
1593 goto done;
1594 if (in_repo_path) {
1595 free(path);
1596 path = in_repo_path;
1599 error = got_ref_list(&refs, repo);
1600 if (error)
1601 goto done;
1603 error = print_commits(id, repo, path, show_patch,
1604 diff_context, limit, first_parent_traversal, &refs);
1605 done:
1606 free(path);
1607 free(repo_path);
1608 free(cwd);
1609 free(id);
1610 if (worktree)
1611 got_worktree_close(worktree);
1612 if (repo) {
1613 const struct got_error *repo_error;
1614 repo_error = got_repo_close(repo);
1615 if (error == NULL)
1616 error = repo_error;
1618 got_ref_list_free(&refs);
1619 return error;
1622 __dead static void
1623 usage_diff(void)
1625 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1626 "[object1 object2 | path]\n", getprogname());
1627 exit(1);
1630 struct print_diff_arg {
1631 struct got_repository *repo;
1632 struct got_worktree *worktree;
1633 int diff_context;
1634 const char *id_str;
1635 int header_shown;
1638 static const struct got_error *
1639 print_diff(void *arg, unsigned char status, const char *path,
1640 struct got_object_id *blob_id, struct got_object_id *commit_id)
1642 struct print_diff_arg *a = arg;
1643 const struct got_error *err = NULL;
1644 struct got_blob_object *blob1 = NULL;
1645 FILE *f2 = NULL;
1646 char *abspath = NULL;
1647 struct stat sb;
1649 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1650 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1651 return NULL;
1653 if (!a->header_shown) {
1654 printf("diff %s %s\n", a->id_str,
1655 got_worktree_get_root_path(a->worktree));
1656 a->header_shown = 1;
1659 if (status != GOT_STATUS_ADD) {
1660 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1661 if (err)
1662 goto done;
1666 if (status != GOT_STATUS_DELETE) {
1667 if (asprintf(&abspath, "%s/%s",
1668 got_worktree_get_root_path(a->worktree), path) == -1) {
1669 err = got_error_from_errno("asprintf");
1670 goto done;
1673 f2 = fopen(abspath, "r");
1674 if (f2 == NULL) {
1675 err = got_error_from_errno2("fopen", abspath);
1676 goto done;
1678 if (lstat(abspath, &sb) == -1) {
1679 err = got_error_from_errno2("lstat", abspath);
1680 goto done;
1682 } else
1683 sb.st_size = 0;
1685 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1686 stdout);
1687 done:
1688 if (blob1)
1689 got_object_blob_close(blob1);
1690 if (f2 && fclose(f2) != 0 && err == NULL)
1691 err = got_error_from_errno("fclose");
1692 free(abspath);
1693 return err;
1696 static const struct got_error *
1697 cmd_diff(int argc, char *argv[])
1699 const struct got_error *error;
1700 struct got_repository *repo = NULL;
1701 struct got_worktree *worktree = NULL;
1702 char *cwd = NULL, *repo_path = NULL;
1703 struct got_object_id *id1 = NULL, *id2 = NULL;
1704 const char *id_str1 = NULL, *id_str2 = NULL;
1705 char *label1 = NULL, *label2 = NULL;
1706 int type1, type2;
1707 int diff_context = 3, ch;
1708 const char *errstr;
1709 char *path = NULL;
1711 #ifndef PROFILE
1712 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1713 NULL) == -1)
1714 err(1, "pledge");
1715 #endif
1717 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1718 switch (ch) {
1719 case 'C':
1720 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1721 if (errstr != NULL)
1722 err(1, "-C option %s", errstr);
1723 break;
1724 case 'r':
1725 repo_path = realpath(optarg, NULL);
1726 if (repo_path == NULL)
1727 err(1, "-r option");
1728 got_path_strip_trailing_slashes(repo_path);
1729 break;
1730 default:
1731 usage_diff();
1732 /* NOTREACHED */
1736 argc -= optind;
1737 argv += optind;
1739 cwd = getcwd(NULL, 0);
1740 if (cwd == NULL) {
1741 error = got_error_from_errno("getcwd");
1742 goto done;
1744 error = got_worktree_open(&worktree, cwd);
1745 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1746 goto done;
1747 if (argc <= 1) {
1748 if (worktree == NULL) {
1749 error = got_error(GOT_ERR_NOT_WORKTREE);
1750 goto done;
1752 if (repo_path)
1753 errx(1,
1754 "-r option can't be used when diffing a work tree");
1755 repo_path = strdup(got_worktree_get_repo_path(worktree));
1756 if (repo_path == NULL) {
1757 error = got_error_from_errno("strdup");
1758 goto done;
1760 if (argc == 1) {
1761 error = got_worktree_resolve_path(&path, worktree,
1762 argv[0]);
1763 if (error)
1764 goto done;
1765 } else {
1766 path = strdup("");
1767 if (path == NULL) {
1768 error = got_error_from_errno("strdup");
1769 goto done;
1772 } else if (argc == 2) {
1773 id_str1 = argv[0];
1774 id_str2 = argv[1];
1775 if (worktree && repo_path == NULL) {
1776 repo_path =
1777 strdup(got_worktree_get_repo_path(worktree));
1778 if (repo_path == NULL) {
1779 error = got_error_from_errno("strdup");
1780 goto done;
1783 } else
1784 usage_diff();
1786 if (repo_path == NULL) {
1787 repo_path = getcwd(NULL, 0);
1788 if (repo_path == NULL)
1789 return got_error_from_errno("getcwd");
1792 error = got_repo_open(&repo, repo_path);
1793 free(repo_path);
1794 if (error != NULL)
1795 goto done;
1797 error = apply_unveil(got_repo_get_path(repo), 1,
1798 worktree ? got_worktree_get_root_path(worktree) : NULL);
1799 if (error)
1800 goto done;
1802 if (argc <= 1) {
1803 struct print_diff_arg arg;
1804 char *id_str;
1805 error = got_object_id_str(&id_str,
1806 got_worktree_get_base_commit_id(worktree));
1807 if (error)
1808 goto done;
1809 arg.repo = repo;
1810 arg.worktree = worktree;
1811 arg.diff_context = diff_context;
1812 arg.id_str = id_str;
1813 arg.header_shown = 0;
1815 error = got_worktree_status(worktree, path, repo, print_diff,
1816 &arg, check_cancelled, NULL);
1817 free(id_str);
1818 goto done;
1821 error = got_repo_match_object_id_prefix(&id1, id_str1,
1822 GOT_OBJ_TYPE_ANY, repo);
1823 if (error) {
1824 struct got_reference *ref;
1825 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1826 goto done;
1827 error = got_ref_open(&ref, repo, id_str1, 0);
1828 if (error != NULL)
1829 goto done;
1830 label1 = strdup(got_ref_get_name(ref));
1831 if (label1 == NULL) {
1832 error = got_error_from_errno("strdup");
1833 goto done;
1835 error = got_ref_resolve(&id1, repo, ref);
1836 got_ref_close(ref);
1837 if (error != NULL)
1838 goto done;
1839 } else {
1840 error = got_object_id_str(&label1, id1);
1841 if (label1 == NULL) {
1842 error = got_error_from_errno("strdup");
1843 goto done;
1847 error = got_repo_match_object_id_prefix(&id2, id_str2,
1848 GOT_OBJ_TYPE_ANY, repo);
1849 if (error) {
1850 struct got_reference *ref;
1851 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1852 goto done;
1853 error = got_ref_open(&ref, repo, id_str2, 0);
1854 if (error != NULL)
1855 goto done;
1856 label2 = strdup(got_ref_get_name(ref));
1857 if (label2 == NULL) {
1858 error = got_error_from_errno("strdup");
1859 goto done;
1861 error = got_ref_resolve(&id2, repo, ref);
1862 got_ref_close(ref);
1863 if (error != NULL)
1864 goto done;
1865 } else {
1866 error = got_object_id_str(&label2, id2);
1867 if (label2 == NULL) {
1868 error = got_error_from_errno("strdup");
1869 goto done;
1873 error = got_object_get_type(&type1, repo, id1);
1874 if (error)
1875 goto done;
1877 error = got_object_get_type(&type2, repo, id2);
1878 if (error)
1879 goto done;
1881 if (type1 != type2) {
1882 error = got_error(GOT_ERR_OBJ_TYPE);
1883 goto done;
1886 switch (type1) {
1887 case GOT_OBJ_TYPE_BLOB:
1888 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1889 diff_context, repo, stdout);
1890 break;
1891 case GOT_OBJ_TYPE_TREE:
1892 error = got_diff_objects_as_trees(id1, id2, "", "",
1893 diff_context, repo, stdout);
1894 break;
1895 case GOT_OBJ_TYPE_COMMIT:
1896 printf("diff %s %s\n", label1, label2);
1897 error = got_diff_objects_as_commits(id1, id2, diff_context,
1898 repo, stdout);
1899 break;
1900 default:
1901 error = got_error(GOT_ERR_OBJ_TYPE);
1904 done:
1905 free(label1);
1906 free(label2);
1907 free(id1);
1908 free(id2);
1909 free(path);
1910 if (worktree)
1911 got_worktree_close(worktree);
1912 if (repo) {
1913 const struct got_error *repo_error;
1914 repo_error = got_repo_close(repo);
1915 if (error == NULL)
1916 error = repo_error;
1918 return error;
1921 __dead static void
1922 usage_blame(void)
1924 fprintf(stderr,
1925 "usage: %s blame [-c commit] [-r repository-path] path\n",
1926 getprogname());
1927 exit(1);
1930 static const struct got_error *
1931 cmd_blame(int argc, char *argv[])
1933 const struct got_error *error;
1934 struct got_repository *repo = NULL;
1935 struct got_worktree *worktree = NULL;
1936 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1937 struct got_object_id *commit_id = NULL;
1938 char *commit_id_str = NULL;
1939 int ch;
1941 #ifndef PROFILE
1942 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1943 NULL) == -1)
1944 err(1, "pledge");
1945 #endif
1947 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1948 switch (ch) {
1949 case 'c':
1950 commit_id_str = optarg;
1951 break;
1952 case 'r':
1953 repo_path = realpath(optarg, NULL);
1954 if (repo_path == NULL)
1955 err(1, "-r option");
1956 got_path_strip_trailing_slashes(repo_path);
1957 break;
1958 default:
1959 usage_blame();
1960 /* NOTREACHED */
1964 argc -= optind;
1965 argv += optind;
1967 if (argc == 1)
1968 path = argv[0];
1969 else
1970 usage_blame();
1972 cwd = getcwd(NULL, 0);
1973 if (cwd == NULL) {
1974 error = got_error_from_errno("getcwd");
1975 goto done;
1977 if (repo_path == NULL) {
1978 error = got_worktree_open(&worktree, cwd);
1979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1980 goto done;
1981 else
1982 error = NULL;
1983 if (worktree) {
1984 repo_path =
1985 strdup(got_worktree_get_repo_path(worktree));
1986 if (repo_path == NULL)
1987 error = got_error_from_errno("strdup");
1988 if (error)
1989 goto done;
1990 } else {
1991 repo_path = strdup(cwd);
1992 if (repo_path == NULL) {
1993 error = got_error_from_errno("strdup");
1994 goto done;
1999 error = got_repo_open(&repo, repo_path);
2000 if (error != NULL)
2001 goto done;
2003 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2004 if (error)
2005 goto done;
2007 if (worktree) {
2008 const char *prefix = got_worktree_get_path_prefix(worktree);
2009 char *p, *worktree_subdir = cwd +
2010 strlen(got_worktree_get_root_path(worktree));
2011 if (asprintf(&p, "%s%s%s%s%s",
2012 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2013 worktree_subdir, worktree_subdir[0] ? "/" : "",
2014 path) == -1) {
2015 error = got_error_from_errno("asprintf");
2016 goto done;
2018 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2019 free(p);
2020 } else {
2021 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2023 if (error)
2024 goto done;
2026 if (commit_id_str == NULL) {
2027 struct got_reference *head_ref;
2028 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2029 if (error != NULL)
2030 goto done;
2031 error = got_ref_resolve(&commit_id, repo, head_ref);
2032 got_ref_close(head_ref);
2033 if (error != NULL)
2034 goto done;
2035 } else {
2036 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2037 if (error)
2038 goto done;
2041 error = got_blame(in_repo_path, commit_id, repo, stdout);
2042 done:
2043 free(in_repo_path);
2044 free(repo_path);
2045 free(cwd);
2046 free(commit_id);
2047 if (worktree)
2048 got_worktree_close(worktree);
2049 if (repo) {
2050 const struct got_error *repo_error;
2051 repo_error = got_repo_close(repo);
2052 if (error == NULL)
2053 error = repo_error;
2055 return error;
2058 __dead static void
2059 usage_tree(void)
2061 fprintf(stderr,
2062 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2063 getprogname());
2064 exit(1);
2067 static void
2068 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2069 const char *root_path)
2071 int is_root_path = (strcmp(path, root_path) == 0);
2073 path += strlen(root_path);
2074 while (path[0] == '/')
2075 path++;
2077 printf("%s%s%s%s%s\n", id ? id : "", path,
2078 is_root_path ? "" : "/", te->name,
2079 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2082 static const struct got_error *
2083 print_tree(const char *path, struct got_object_id *commit_id,
2084 int show_ids, int recurse, const char *root_path,
2085 struct got_repository *repo)
2087 const struct got_error *err = NULL;
2088 struct got_object_id *tree_id = NULL;
2089 struct got_tree_object *tree = NULL;
2090 const struct got_tree_entries *entries;
2091 struct got_tree_entry *te;
2093 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2094 if (err)
2095 goto done;
2097 err = got_object_open_as_tree(&tree, repo, tree_id);
2098 if (err)
2099 goto done;
2100 entries = got_object_tree_get_entries(tree);
2101 te = SIMPLEQ_FIRST(&entries->head);
2102 while (te) {
2103 char *id = NULL;
2105 if (sigint_received || sigpipe_received)
2106 break;
2108 if (show_ids) {
2109 char *id_str;
2110 err = got_object_id_str(&id_str, te->id);
2111 if (err)
2112 goto done;
2113 if (asprintf(&id, "%s ", id_str) == -1) {
2114 err = got_error_from_errno("asprintf");
2115 free(id_str);
2116 goto done;
2118 free(id_str);
2120 print_entry(te, id, path, root_path);
2121 free(id);
2123 if (recurse && S_ISDIR(te->mode)) {
2124 char *child_path;
2125 if (asprintf(&child_path, "%s%s%s", path,
2126 path[0] == '/' && path[1] == '\0' ? "" : "/",
2127 te->name) == -1) {
2128 err = got_error_from_errno("asprintf");
2129 goto done;
2131 err = print_tree(child_path, commit_id, show_ids, 1,
2132 root_path, repo);
2133 free(child_path);
2134 if (err)
2135 goto done;
2138 te = SIMPLEQ_NEXT(te, entry);
2140 done:
2141 if (tree)
2142 got_object_tree_close(tree);
2143 free(tree_id);
2144 return err;
2147 static const struct got_error *
2148 cmd_tree(int argc, char *argv[])
2150 const struct got_error *error;
2151 struct got_repository *repo = NULL;
2152 struct got_worktree *worktree = NULL;
2153 const char *path;
2154 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2155 struct got_object_id *commit_id = NULL;
2156 char *commit_id_str = NULL;
2157 int show_ids = 0, recurse = 0;
2158 int ch;
2160 #ifndef PROFILE
2161 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2162 NULL) == -1)
2163 err(1, "pledge");
2164 #endif
2166 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2167 switch (ch) {
2168 case 'c':
2169 commit_id_str = optarg;
2170 break;
2171 case 'r':
2172 repo_path = realpath(optarg, NULL);
2173 if (repo_path == NULL)
2174 err(1, "-r option");
2175 got_path_strip_trailing_slashes(repo_path);
2176 break;
2177 case 'i':
2178 show_ids = 1;
2179 break;
2180 case 'R':
2181 recurse = 1;
2182 break;
2183 default:
2184 usage_tree();
2185 /* NOTREACHED */
2189 argc -= optind;
2190 argv += optind;
2192 if (argc == 1)
2193 path = argv[0];
2194 else if (argc > 1)
2195 usage_tree();
2196 else
2197 path = NULL;
2199 cwd = getcwd(NULL, 0);
2200 if (cwd == NULL) {
2201 error = got_error_from_errno("getcwd");
2202 goto done;
2204 if (repo_path == NULL) {
2205 error = got_worktree_open(&worktree, cwd);
2206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2207 goto done;
2208 else
2209 error = NULL;
2210 if (worktree) {
2211 repo_path =
2212 strdup(got_worktree_get_repo_path(worktree));
2213 if (repo_path == NULL)
2214 error = got_error_from_errno("strdup");
2215 if (error)
2216 goto done;
2217 } else {
2218 repo_path = strdup(cwd);
2219 if (repo_path == NULL) {
2220 error = got_error_from_errno("strdup");
2221 goto done;
2226 error = got_repo_open(&repo, repo_path);
2227 if (error != NULL)
2228 goto done;
2230 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2231 if (error)
2232 goto done;
2234 if (path == NULL) {
2235 if (worktree) {
2236 char *p, *worktree_subdir = cwd +
2237 strlen(got_worktree_get_root_path(worktree));
2238 if (asprintf(&p, "%s/%s",
2239 got_worktree_get_path_prefix(worktree),
2240 worktree_subdir) == -1) {
2241 error = got_error_from_errno("asprintf");
2242 goto done;
2244 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2245 free(p);
2246 if (error)
2247 goto done;
2248 } else
2249 path = "/";
2251 if (in_repo_path == NULL) {
2252 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2253 if (error != NULL)
2254 goto done;
2257 if (commit_id_str == NULL) {
2258 struct got_reference *head_ref;
2259 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2260 if (error != NULL)
2261 goto done;
2262 error = got_ref_resolve(&commit_id, repo, head_ref);
2263 got_ref_close(head_ref);
2264 if (error != NULL)
2265 goto done;
2266 } else {
2267 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2268 if (error)
2269 goto done;
2272 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2273 in_repo_path, repo);
2274 done:
2275 free(in_repo_path);
2276 free(repo_path);
2277 free(cwd);
2278 free(commit_id);
2279 if (worktree)
2280 got_worktree_close(worktree);
2281 if (repo) {
2282 const struct got_error *repo_error;
2283 repo_error = got_repo_close(repo);
2284 if (error == NULL)
2285 error = repo_error;
2287 return error;
2290 __dead static void
2291 usage_status(void)
2293 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2294 exit(1);
2297 static const struct got_error *
2298 print_status(void *arg, unsigned char status, const char *path,
2299 struct got_object_id *blob_id, struct got_object_id *commit_id)
2301 printf("%c %s\n", status, path);
2302 return NULL;
2305 static const struct got_error *
2306 cmd_status(int argc, char *argv[])
2308 const struct got_error *error = NULL;
2309 struct got_repository *repo = NULL;
2310 struct got_worktree *worktree = NULL;
2311 char *cwd = NULL, *path = NULL;
2312 int ch;
2314 while ((ch = getopt(argc, argv, "")) != -1) {
2315 switch (ch) {
2316 default:
2317 usage_status();
2318 /* NOTREACHED */
2322 argc -= optind;
2323 argv += optind;
2325 #ifndef PROFILE
2326 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2327 NULL) == -1)
2328 err(1, "pledge");
2329 #endif
2330 cwd = getcwd(NULL, 0);
2331 if (cwd == NULL) {
2332 error = got_error_from_errno("getcwd");
2333 goto done;
2336 error = got_worktree_open(&worktree, cwd);
2337 if (error != NULL)
2338 goto done;
2340 if (argc == 0) {
2341 path = strdup("");
2342 if (path == NULL) {
2343 error = got_error_from_errno("strdup");
2344 goto done;
2346 } else if (argc == 1) {
2347 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2348 if (error)
2349 goto done;
2350 } else
2351 usage_status();
2353 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2354 if (error != NULL)
2355 goto done;
2357 error = apply_unveil(got_repo_get_path(repo), 1,
2358 got_worktree_get_root_path(worktree));
2359 if (error)
2360 goto done;
2362 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2363 check_cancelled, NULL);
2364 done:
2365 free(cwd);
2366 free(path);
2367 return error;
2370 __dead static void
2371 usage_ref(void)
2373 fprintf(stderr,
2374 "usage: %s ref [-r repository] -l | -d name | name target\n",
2375 getprogname());
2376 exit(1);
2379 static const struct got_error *
2380 list_refs(struct got_repository *repo)
2382 static const struct got_error *err = NULL;
2383 struct got_reflist_head refs;
2384 struct got_reflist_entry *re;
2386 SIMPLEQ_INIT(&refs);
2387 err = got_ref_list(&refs, repo);
2388 if (err)
2389 return err;
2391 SIMPLEQ_FOREACH(re, &refs, entry) {
2392 char *refstr;
2393 refstr = got_ref_to_str(re->ref);
2394 if (refstr == NULL)
2395 return got_error_from_errno("got_ref_to_str");
2396 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2397 free(refstr);
2400 got_ref_list_free(&refs);
2401 return NULL;
2404 static const struct got_error *
2405 delete_ref(struct got_repository *repo, const char *refname)
2407 const struct got_error *err = NULL;
2408 struct got_reference *ref;
2410 err = got_ref_open(&ref, repo, refname, 0);
2411 if (err)
2412 return err;
2414 err = got_ref_delete(ref, repo);
2415 got_ref_close(ref);
2416 return err;
2419 static const struct got_error *
2420 add_ref(struct got_repository *repo, const char *refname, const char *target)
2422 const struct got_error *err = NULL;
2423 struct got_object_id *id;
2424 struct got_reference *ref = NULL;
2427 * Don't let the user create a reference named '-'.
2428 * While technically a valid reference name, this case is usually
2429 * an unintended typo.
2431 if (refname[0] == '-' && refname[1] == '\0')
2432 return got_error(GOT_ERR_BAD_REF_NAME);
2434 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2435 repo);
2436 if (err) {
2437 struct got_reference *target_ref;
2439 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2440 return err;
2441 err = got_ref_open(&target_ref, repo, target, 0);
2442 if (err)
2443 return err;
2444 err = got_ref_resolve(&id, repo, target_ref);
2445 got_ref_close(target_ref);
2446 if (err)
2447 return err;
2450 err = got_ref_alloc(&ref, refname, id);
2451 if (err)
2452 goto done;
2454 err = got_ref_write(ref, repo);
2455 done:
2456 if (ref)
2457 got_ref_close(ref);
2458 free(id);
2459 return err;
2462 static const struct got_error *
2463 cmd_ref(int argc, char *argv[])
2465 const struct got_error *error = NULL;
2466 struct got_repository *repo = NULL;
2467 struct got_worktree *worktree = NULL;
2468 char *cwd = NULL, *repo_path = NULL;
2469 int ch, do_list = 0;
2470 const char *delref = NULL;
2472 /* TODO: Add -s option for adding symbolic references. */
2473 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2474 switch (ch) {
2475 case 'd':
2476 delref = optarg;
2477 break;
2478 case 'r':
2479 repo_path = realpath(optarg, NULL);
2480 if (repo_path == NULL)
2481 err(1, "-r option");
2482 got_path_strip_trailing_slashes(repo_path);
2483 break;
2484 case 'l':
2485 do_list = 1;
2486 break;
2487 default:
2488 usage_ref();
2489 /* NOTREACHED */
2493 if (do_list && delref)
2494 errx(1, "-l and -d options are mutually exclusive\n");
2496 argc -= optind;
2497 argv += optind;
2499 if (do_list || delref) {
2500 if (argc > 0)
2501 usage_ref();
2502 } else if (argc != 2)
2503 usage_ref();
2505 #ifndef PROFILE
2506 if (do_list) {
2507 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2508 NULL) == -1)
2509 err(1, "pledge");
2510 } else {
2511 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2512 "sendfd unveil", NULL) == -1)
2513 err(1, "pledge");
2515 #endif
2516 cwd = getcwd(NULL, 0);
2517 if (cwd == NULL) {
2518 error = got_error_from_errno("getcwd");
2519 goto done;
2522 if (repo_path == NULL) {
2523 error = got_worktree_open(&worktree, cwd);
2524 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2525 goto done;
2526 else
2527 error = NULL;
2528 if (worktree) {
2529 repo_path =
2530 strdup(got_worktree_get_repo_path(worktree));
2531 if (repo_path == NULL)
2532 error = got_error_from_errno("strdup");
2533 if (error)
2534 goto done;
2535 } else {
2536 repo_path = strdup(cwd);
2537 if (repo_path == NULL) {
2538 error = got_error_from_errno("strdup");
2539 goto done;
2544 error = got_repo_open(&repo, repo_path);
2545 if (error != NULL)
2546 goto done;
2548 error = apply_unveil(got_repo_get_path(repo), do_list,
2549 worktree ? got_worktree_get_root_path(worktree) : NULL);
2550 if (error)
2551 goto done;
2553 if (do_list)
2554 error = list_refs(repo);
2555 else if (delref)
2556 error = delete_ref(repo, delref);
2557 else
2558 error = add_ref(repo, argv[0], argv[1]);
2559 done:
2560 if (repo)
2561 got_repo_close(repo);
2562 if (worktree)
2563 got_worktree_close(worktree);
2564 free(cwd);
2565 free(repo_path);
2566 return error;
2569 __dead static void
2570 usage_branch(void)
2572 fprintf(stderr,
2573 "usage: %s branch [-r repository] -l | -d name | "
2574 "name [base-branch]\n", getprogname());
2575 exit(1);
2578 static const struct got_error *
2579 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2581 static const struct got_error *err = NULL;
2582 struct got_reflist_head refs;
2583 struct got_reflist_entry *re;
2585 SIMPLEQ_INIT(&refs);
2587 err = got_ref_list(&refs, repo);
2588 if (err)
2589 return err;
2591 SIMPLEQ_FOREACH(re, &refs, entry) {
2592 const char *refname, *marker = " ";
2593 char *refstr;
2594 refname = got_ref_get_name(re->ref);
2595 if (strncmp(refname, "refs/heads/", 11) != 0)
2596 continue;
2597 if (worktree && strcmp(refname,
2598 got_worktree_get_head_ref_name(worktree)) == 0) {
2599 struct got_object_id *id = NULL;
2600 err = got_ref_resolve(&id, repo, re->ref);
2601 if (err)
2602 return err;
2603 if (got_object_id_cmp(id,
2604 got_worktree_get_base_commit_id(worktree)) == 0)
2605 marker = "* ";
2606 else
2607 marker = "~ ";
2608 free(id);
2610 refname += 11;
2611 refstr = got_ref_to_str(re->ref);
2612 if (refstr == NULL)
2613 return got_error_from_errno("got_ref_to_str");
2614 printf("%s%s: %s\n", marker, refname, refstr);
2615 free(refstr);
2618 got_ref_list_free(&refs);
2619 return NULL;
2622 static const struct got_error *
2623 delete_branch(struct got_repository *repo, const char *branch_name)
2625 const struct got_error *err = NULL;
2626 struct got_reference *ref;
2627 char *refname;
2629 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2630 return got_error_from_errno("asprintf");
2632 err = got_ref_open(&ref, repo, refname, 0);
2633 if (err)
2634 goto done;
2636 err = got_ref_delete(ref, repo);
2637 got_ref_close(ref);
2638 done:
2639 free(refname);
2640 return err;
2643 static const struct got_error *
2644 add_branch(struct got_repository *repo, const char *branch_name,
2645 const char *base_branch)
2647 const struct got_error *err = NULL;
2648 struct got_object_id *id = NULL;
2649 struct got_reference *ref = NULL;
2650 char *base_refname = NULL, *refname = NULL;
2651 struct got_reference *base_ref;
2654 * Don't let the user create a branch named '-'.
2655 * While technically a valid reference name, this case is usually
2656 * an unintended typo.
2658 if (branch_name[0] == '-' && branch_name[1] == '\0')
2659 return got_error(GOT_ERR_BAD_REF_NAME);
2661 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2662 base_refname = strdup(GOT_REF_HEAD);
2663 if (base_refname == NULL)
2664 return got_error_from_errno("strdup");
2665 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2666 return got_error_from_errno("asprintf");
2668 err = got_ref_open(&base_ref, repo, base_refname, 0);
2669 if (err)
2670 goto done;
2671 err = got_ref_resolve(&id, repo, base_ref);
2672 got_ref_close(base_ref);
2673 if (err)
2674 goto done;
2676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2677 err = got_error_from_errno("asprintf");
2678 goto done;
2681 err = got_ref_open(&ref, repo, refname, 0);
2682 if (err == NULL) {
2683 err = got_error(GOT_ERR_BRANCH_EXISTS);
2684 goto done;
2685 } else if (err->code != GOT_ERR_NOT_REF)
2686 goto done;
2688 err = got_ref_alloc(&ref, refname, id);
2689 if (err)
2690 goto done;
2692 err = got_ref_write(ref, repo);
2693 done:
2694 if (ref)
2695 got_ref_close(ref);
2696 free(id);
2697 free(base_refname);
2698 free(refname);
2699 return err;
2702 static const struct got_error *
2703 cmd_branch(int argc, char *argv[])
2705 const struct got_error *error = NULL;
2706 struct got_repository *repo = NULL;
2707 struct got_worktree *worktree = NULL;
2708 char *cwd = NULL, *repo_path = NULL;
2709 int ch, do_list = 0;
2710 const char *delref = NULL;
2712 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2713 switch (ch) {
2714 case 'd':
2715 delref = optarg;
2716 break;
2717 case 'r':
2718 repo_path = realpath(optarg, NULL);
2719 if (repo_path == NULL)
2720 err(1, "-r option");
2721 got_path_strip_trailing_slashes(repo_path);
2722 break;
2723 case 'l':
2724 do_list = 1;
2725 break;
2726 default:
2727 usage_branch();
2728 /* NOTREACHED */
2732 if (do_list && delref)
2733 errx(1, "-l and -d options are mutually exclusive\n");
2735 argc -= optind;
2736 argv += optind;
2738 if (do_list || delref) {
2739 if (argc > 0)
2740 usage_branch();
2741 } else if (argc < 1 || argc > 2)
2742 usage_branch();
2744 #ifndef PROFILE
2745 if (do_list) {
2746 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2747 NULL) == -1)
2748 err(1, "pledge");
2749 } else {
2750 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2751 "sendfd unveil", NULL) == -1)
2752 err(1, "pledge");
2754 #endif
2755 cwd = getcwd(NULL, 0);
2756 if (cwd == NULL) {
2757 error = got_error_from_errno("getcwd");
2758 goto done;
2761 if (repo_path == NULL) {
2762 error = got_worktree_open(&worktree, cwd);
2763 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2764 goto done;
2765 else
2766 error = NULL;
2767 if (worktree) {
2768 repo_path =
2769 strdup(got_worktree_get_repo_path(worktree));
2770 if (repo_path == NULL)
2771 error = got_error_from_errno("strdup");
2772 if (error)
2773 goto done;
2774 } else {
2775 repo_path = strdup(cwd);
2776 if (repo_path == NULL) {
2777 error = got_error_from_errno("strdup");
2778 goto done;
2783 error = got_repo_open(&repo, repo_path);
2784 if (error != NULL)
2785 goto done;
2787 error = apply_unveil(got_repo_get_path(repo), do_list,
2788 worktree ? got_worktree_get_root_path(worktree) : NULL);
2789 if (error)
2790 goto done;
2792 if (do_list)
2793 error = list_branches(repo, worktree);
2794 else if (delref)
2795 error = delete_branch(repo, delref);
2796 else {
2797 const char *base_branch;
2798 if (argc == 1) {
2799 base_branch = worktree ?
2800 got_worktree_get_head_ref_name(worktree) :
2801 GOT_REF_HEAD;
2802 } else
2803 base_branch = argv[1];
2804 error = add_branch(repo, argv[0], base_branch);
2806 done:
2807 if (repo)
2808 got_repo_close(repo);
2809 if (worktree)
2810 got_worktree_close(worktree);
2811 free(cwd);
2812 free(repo_path);
2813 return error;
2816 __dead static void
2817 usage_add(void)
2819 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2820 exit(1);
2823 static const struct got_error *
2824 cmd_add(int argc, char *argv[])
2826 const struct got_error *error = NULL;
2827 struct got_repository *repo = NULL;
2828 struct got_worktree *worktree = NULL;
2829 char *cwd = NULL;
2830 struct got_pathlist_head paths;
2831 struct got_pathlist_entry *pe;
2832 int ch, x;
2834 TAILQ_INIT(&paths);
2836 while ((ch = getopt(argc, argv, "")) != -1) {
2837 switch (ch) {
2838 default:
2839 usage_add();
2840 /* NOTREACHED */
2844 argc -= optind;
2845 argv += optind;
2847 #ifndef PROFILE
2848 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2849 NULL) == -1)
2850 err(1, "pledge");
2851 #endif
2852 if (argc < 1)
2853 usage_add();
2855 cwd = getcwd(NULL, 0);
2856 if (cwd == NULL) {
2857 error = got_error_from_errno("getcwd");
2858 goto done;
2861 error = got_worktree_open(&worktree, cwd);
2862 if (error)
2863 goto done;
2865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2866 if (error != NULL)
2867 goto done;
2869 error = apply_unveil(got_repo_get_path(repo), 1,
2870 got_worktree_get_root_path(worktree));
2871 if (error)
2872 goto done;
2874 for (x = 0; x < argc; x++) {
2875 char *path = realpath(argv[x], NULL);
2876 if (path == NULL) {
2877 error = got_error_from_errno2("realpath", argv[x]);
2878 goto done;
2881 got_path_strip_trailing_slashes(path);
2882 error = got_pathlist_insert(&pe, &paths, path, NULL);
2883 if (error) {
2884 free(path);
2885 goto done;
2888 error = got_worktree_schedule_add(worktree, &paths, print_status,
2889 NULL, repo);
2890 done:
2891 if (repo)
2892 got_repo_close(repo);
2893 if (worktree)
2894 got_worktree_close(worktree);
2895 TAILQ_FOREACH(pe, &paths, entry)
2896 free((char *)pe->path);
2897 got_pathlist_free(&paths);
2898 free(cwd);
2899 return error;
2902 __dead static void
2903 usage_remove(void)
2905 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2906 exit(1);
2909 static const struct got_error *
2910 cmd_remove(int argc, char *argv[])
2912 const struct got_error *error = NULL;
2913 struct got_worktree *worktree = NULL;
2914 struct got_repository *repo = NULL;
2915 char *cwd = NULL;
2916 struct got_pathlist_head paths;
2917 struct got_pathlist_entry *pe;
2918 int ch, i, delete_local_mods = 0;
2920 TAILQ_INIT(&paths);
2922 while ((ch = getopt(argc, argv, "f")) != -1) {
2923 switch (ch) {
2924 case 'f':
2925 delete_local_mods = 1;
2926 break;
2927 default:
2928 usage_add();
2929 /* NOTREACHED */
2933 argc -= optind;
2934 argv += optind;
2936 #ifndef PROFILE
2937 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2938 NULL) == -1)
2939 err(1, "pledge");
2940 #endif
2941 if (argc < 1)
2942 usage_remove();
2944 cwd = getcwd(NULL, 0);
2945 if (cwd == NULL) {
2946 error = got_error_from_errno("getcwd");
2947 goto done;
2949 error = got_worktree_open(&worktree, cwd);
2950 if (error)
2951 goto done;
2953 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2954 if (error)
2955 goto done;
2957 error = apply_unveil(got_repo_get_path(repo), 1,
2958 got_worktree_get_root_path(worktree));
2959 if (error)
2960 goto done;
2962 for (i = 0; i < argc; i++) {
2963 char *path = realpath(argv[i], NULL);
2964 if (path == NULL) {
2965 error = got_error_from_errno2("realpath", argv[i]);
2966 goto done;
2969 got_path_strip_trailing_slashes(path);
2970 error = got_pathlist_insert(&pe, &paths, path, NULL);
2971 if (error) {
2972 free(path);
2973 goto done;
2976 error = got_worktree_schedule_delete(worktree, &paths,
2977 delete_local_mods, print_status, NULL, repo);
2978 if (error)
2979 goto done;
2980 done:
2981 if (repo)
2982 got_repo_close(repo);
2983 if (worktree)
2984 got_worktree_close(worktree);
2985 TAILQ_FOREACH(pe, &paths, entry)
2986 free((char *)pe->path);
2987 got_pathlist_free(&paths);
2988 free(cwd);
2989 return error;
2992 __dead static void
2993 usage_revert(void)
2995 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2996 exit(1);
2999 static const struct got_error *
3000 revert_progress(void *arg, unsigned char status, const char *path)
3002 while (path[0] == '/')
3003 path++;
3004 printf("%c %s\n", status, path);
3005 return NULL;
3008 static const struct got_error *
3009 cmd_revert(int argc, char *argv[])
3011 const struct got_error *error = NULL;
3012 struct got_worktree *worktree = NULL;
3013 struct got_repository *repo = NULL;
3014 char *cwd = NULL, *path = NULL;
3015 struct got_pathlist_head paths;
3016 struct got_pathlist_entry *pe;
3017 int ch, i;
3019 TAILQ_INIT(&paths);
3021 while ((ch = getopt(argc, argv, "")) != -1) {
3022 switch (ch) {
3023 default:
3024 usage_revert();
3025 /* NOTREACHED */
3029 argc -= optind;
3030 argv += optind;
3032 #ifndef PROFILE
3033 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3034 "unveil", NULL) == -1)
3035 err(1, "pledge");
3036 #endif
3037 if (argc < 1)
3038 usage_revert();
3040 for (i = 0; i < argc; i++) {
3041 char *path = realpath(argv[i], NULL);
3042 if (path == NULL) {
3043 error = got_error_from_errno2("realpath", argv[i]);
3044 goto done;
3047 got_path_strip_trailing_slashes(path);
3048 error = got_pathlist_insert(&pe, &paths, path, NULL);
3049 if (error) {
3050 free(path);
3051 goto done;
3055 cwd = getcwd(NULL, 0);
3056 if (cwd == NULL) {
3057 error = got_error_from_errno("getcwd");
3058 goto done;
3060 error = got_worktree_open(&worktree, cwd);
3061 if (error)
3062 goto done;
3064 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3065 if (error != NULL)
3066 goto done;
3068 error = apply_unveil(got_repo_get_path(repo), 1,
3069 got_worktree_get_root_path(worktree));
3070 if (error)
3071 goto done;
3073 error = got_worktree_revert(worktree, &paths,
3074 revert_progress, NULL, repo);
3075 if (error)
3076 goto done;
3077 done:
3078 if (repo)
3079 got_repo_close(repo);
3080 if (worktree)
3081 got_worktree_close(worktree);
3082 free(path);
3083 free(cwd);
3084 return error;
3087 __dead static void
3088 usage_commit(void)
3090 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3091 exit(1);
3094 struct collect_commit_logmsg_arg {
3095 const char *cmdline_log;
3096 const char *editor;
3097 const char *worktree_path;
3098 const char *branch_name;
3099 const char *repo_path;
3100 char *logmsg_path;
3104 static const struct got_error *
3105 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3106 void *arg)
3108 char *initial_content = NULL;
3109 struct got_pathlist_entry *pe;
3110 const struct got_error *err = NULL;
3111 char *template = NULL;
3112 struct collect_commit_logmsg_arg *a = arg;
3113 int fd;
3114 size_t len;
3116 /* if a message was specified on the command line, just use it */
3117 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3118 len = strlen(a->cmdline_log) + 1;
3119 *logmsg = malloc(len + 1);
3120 if (*logmsg == NULL)
3121 return got_error_from_errno("malloc");
3122 strlcpy(*logmsg, a->cmdline_log, len);
3123 return NULL;
3126 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3127 return got_error_from_errno("asprintf");
3129 if (asprintf(&initial_content,
3130 "\n# changes to be committed on branch %s:\n",
3131 a->branch_name) == -1)
3132 return got_error_from_errno("asprintf");
3134 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3135 if (err)
3136 goto done;
3138 dprintf(fd, initial_content);
3140 TAILQ_FOREACH(pe, commitable_paths, entry) {
3141 struct got_commitable *ct = pe->data;
3142 dprintf(fd, "# %c %s\n",
3143 got_commitable_get_status(ct),
3144 got_commitable_get_path(ct));
3146 close(fd);
3148 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3149 done:
3150 unlink(a->logmsg_path);
3151 free(a->logmsg_path);
3152 free(initial_content);
3153 free(template);
3155 /* Editor is done; we can now apply unveil(2) */
3156 if (err == NULL) {
3157 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3158 if (err) {
3159 free(*logmsg);
3160 *logmsg = NULL;
3163 return err;
3166 static const struct got_error *
3167 cmd_commit(int argc, char *argv[])
3169 const struct got_error *error = NULL;
3170 struct got_worktree *worktree = NULL;
3171 struct got_repository *repo = NULL;
3172 char *cwd = NULL, *path = NULL, *id_str = NULL;
3173 struct got_object_id *id = NULL;
3174 const char *logmsg = NULL;
3175 const char *got_author = getenv("GOT_AUTHOR");
3176 struct collect_commit_logmsg_arg cl_arg;
3177 char *editor = NULL;
3178 int ch;
3180 while ((ch = getopt(argc, argv, "m:")) != -1) {
3181 switch (ch) {
3182 case 'm':
3183 logmsg = optarg;
3184 break;
3185 default:
3186 usage_commit();
3187 /* NOTREACHED */
3191 argc -= optind;
3192 argv += optind;
3194 #ifndef PROFILE
3195 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3196 "unveil", NULL) == -1)
3197 err(1, "pledge");
3198 #endif
3199 if (argc == 1) {
3200 path = realpath(argv[0], NULL);
3201 if (path == NULL) {
3202 error = got_error_from_errno2("realpath", argv[0]);
3203 goto done;
3205 got_path_strip_trailing_slashes(path);
3206 } else if (argc != 0)
3207 usage_commit();
3209 if (got_author == NULL) {
3210 /* TODO: Look current user up in password database */
3211 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3212 goto done;
3215 cwd = getcwd(NULL, 0);
3216 if (cwd == NULL) {
3217 error = got_error_from_errno("getcwd");
3218 goto done;
3220 error = got_worktree_open(&worktree, cwd);
3221 if (error)
3222 goto done;
3224 error = check_rebase_or_histedit_in_progress(worktree);
3225 if (error)
3226 goto done;
3228 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3229 if (error != NULL)
3230 goto done;
3233 * unveil(2) traverses exec(2); if an editor is used we have
3234 * to apply unveil after the log message has been written.
3236 if (logmsg == NULL || strlen(logmsg) == 0)
3237 error = get_editor(&editor);
3238 else
3239 error = apply_unveil(got_repo_get_path(repo), 0,
3240 got_worktree_get_root_path(worktree));
3241 if (error)
3242 goto done;
3244 cl_arg.editor = editor;
3245 cl_arg.cmdline_log = logmsg;
3246 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3247 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3248 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3249 cl_arg.branch_name += 5;
3250 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3251 cl_arg.branch_name += 6;
3252 cl_arg.repo_path = got_repo_get_path(repo);
3253 cl_arg.logmsg_path = NULL;
3254 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3255 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3256 if (error) {
3257 if (cl_arg.logmsg_path)
3258 fprintf(stderr, "%s: log message preserved in %s\n",
3259 getprogname(), cl_arg.logmsg_path);
3260 goto done;
3263 if (cl_arg.logmsg_path)
3264 unlink(cl_arg.logmsg_path);
3266 error = got_object_id_str(&id_str, id);
3267 if (error)
3268 goto done;
3269 printf("Created commit %s\n", id_str);
3270 done:
3271 if (repo)
3272 got_repo_close(repo);
3273 if (worktree)
3274 got_worktree_close(worktree);
3275 free(path);
3276 free(cwd);
3277 free(id_str);
3278 free(editor);
3279 return error;
3282 __dead static void
3283 usage_cherrypick(void)
3285 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3286 exit(1);
3289 static const struct got_error *
3290 cmd_cherrypick(int argc, char *argv[])
3292 const struct got_error *error = NULL;
3293 struct got_worktree *worktree = NULL;
3294 struct got_repository *repo = NULL;
3295 char *cwd = NULL, *commit_id_str = NULL;
3296 struct got_object_id *commit_id = NULL;
3297 struct got_commit_object *commit = NULL;
3298 struct got_object_qid *pid;
3299 struct got_reference *head_ref = NULL;
3300 int ch, did_something = 0;
3302 while ((ch = getopt(argc, argv, "")) != -1) {
3303 switch (ch) {
3304 default:
3305 usage_cherrypick();
3306 /* NOTREACHED */
3310 argc -= optind;
3311 argv += optind;
3313 #ifndef PROFILE
3314 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3315 "unveil", NULL) == -1)
3316 err(1, "pledge");
3317 #endif
3318 if (argc != 1)
3319 usage_cherrypick();
3321 cwd = getcwd(NULL, 0);
3322 if (cwd == NULL) {
3323 error = got_error_from_errno("getcwd");
3324 goto done;
3326 error = got_worktree_open(&worktree, cwd);
3327 if (error)
3328 goto done;
3330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3331 if (error != NULL)
3332 goto done;
3334 error = apply_unveil(got_repo_get_path(repo), 0,
3335 got_worktree_get_root_path(worktree));
3336 if (error)
3337 goto done;
3339 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3340 GOT_OBJ_TYPE_COMMIT, repo);
3341 if (error != NULL) {
3342 struct got_reference *ref;
3343 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3344 goto done;
3345 error = got_ref_open(&ref, repo, argv[0], 0);
3346 if (error != NULL)
3347 goto done;
3348 error = got_ref_resolve(&commit_id, repo, ref);
3349 got_ref_close(ref);
3350 if (error != NULL)
3351 goto done;
3353 error = got_object_id_str(&commit_id_str, commit_id);
3354 if (error)
3355 goto done;
3357 error = got_ref_open(&head_ref, repo,
3358 got_worktree_get_head_ref_name(worktree), 0);
3359 if (error != NULL)
3360 goto done;
3362 error = check_same_branch(commit_id, head_ref, repo);
3363 if (error) {
3364 if (error->code != GOT_ERR_ANCESTRY)
3365 goto done;
3366 error = NULL;
3367 } else {
3368 error = got_error(GOT_ERR_SAME_BRANCH);
3369 goto done;
3372 error = got_object_open_as_commit(&commit, repo, commit_id);
3373 if (error)
3374 goto done;
3375 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3376 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3377 commit_id, repo, update_progress, &did_something, check_cancelled,
3378 NULL);
3379 if (error != NULL)
3380 goto done;
3382 if (did_something)
3383 printf("Merged commit %s\n", commit_id_str);
3384 done:
3385 if (commit)
3386 got_object_commit_close(commit);
3387 free(commit_id_str);
3388 if (head_ref)
3389 got_ref_close(head_ref);
3390 if (worktree)
3391 got_worktree_close(worktree);
3392 if (repo)
3393 got_repo_close(repo);
3394 return error;
3397 __dead static void
3398 usage_backout(void)
3400 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3401 exit(1);
3404 static const struct got_error *
3405 cmd_backout(int argc, char *argv[])
3407 const struct got_error *error = NULL;
3408 struct got_worktree *worktree = NULL;
3409 struct got_repository *repo = NULL;
3410 char *cwd = NULL, *commit_id_str = NULL;
3411 struct got_object_id *commit_id = NULL;
3412 struct got_commit_object *commit = NULL;
3413 struct got_object_qid *pid;
3414 struct got_reference *head_ref = NULL;
3415 int ch, did_something = 0;
3417 while ((ch = getopt(argc, argv, "")) != -1) {
3418 switch (ch) {
3419 default:
3420 usage_backout();
3421 /* NOTREACHED */
3425 argc -= optind;
3426 argv += optind;
3428 #ifndef PROFILE
3429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3430 "unveil", NULL) == -1)
3431 err(1, "pledge");
3432 #endif
3433 if (argc != 1)
3434 usage_backout();
3436 cwd = getcwd(NULL, 0);
3437 if (cwd == NULL) {
3438 error = got_error_from_errno("getcwd");
3439 goto done;
3441 error = got_worktree_open(&worktree, cwd);
3442 if (error)
3443 goto done;
3445 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3446 if (error != NULL)
3447 goto done;
3449 error = apply_unveil(got_repo_get_path(repo), 0,
3450 got_worktree_get_root_path(worktree));
3451 if (error)
3452 goto done;
3454 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3455 GOT_OBJ_TYPE_COMMIT, repo);
3456 if (error != NULL) {
3457 struct got_reference *ref;
3458 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3459 goto done;
3460 error = got_ref_open(&ref, repo, argv[0], 0);
3461 if (error != NULL)
3462 goto done;
3463 error = got_ref_resolve(&commit_id, repo, ref);
3464 got_ref_close(ref);
3465 if (error != NULL)
3466 goto done;
3468 error = got_object_id_str(&commit_id_str, commit_id);
3469 if (error)
3470 goto done;
3472 error = got_ref_open(&head_ref, repo,
3473 got_worktree_get_head_ref_name(worktree), 0);
3474 if (error != NULL)
3475 goto done;
3477 error = check_same_branch(commit_id, head_ref, repo);
3478 if (error)
3479 goto done;
3481 error = got_object_open_as_commit(&commit, repo, commit_id);
3482 if (error)
3483 goto done;
3484 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3485 if (pid == NULL) {
3486 error = got_error(GOT_ERR_ROOT_COMMIT);
3487 goto done;
3490 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3491 update_progress, &did_something, check_cancelled, NULL);
3492 if (error != NULL)
3493 goto done;
3495 if (did_something)
3496 printf("Backed out commit %s\n", commit_id_str);
3497 done:
3498 if (commit)
3499 got_object_commit_close(commit);
3500 free(commit_id_str);
3501 if (head_ref)
3502 got_ref_close(head_ref);
3503 if (worktree)
3504 got_worktree_close(worktree);
3505 if (repo)
3506 got_repo_close(repo);
3507 return error;
3510 __dead static void
3511 usage_rebase(void)
3513 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3514 getprogname());
3515 exit(1);
3518 void
3519 trim_logmsg(char *logmsg, int limit)
3521 char *nl;
3522 size_t len;
3524 len = strlen(logmsg);
3525 if (len > limit)
3526 len = limit;
3527 logmsg[len] = '\0';
3528 nl = strchr(logmsg, '\n');
3529 if (nl)
3530 *nl = '\0';
3533 static const struct got_error *
3534 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3536 const char *logmsg0 = NULL;
3538 logmsg0 = got_object_commit_get_logmsg(commit);
3540 while (isspace((unsigned char)logmsg0[0]))
3541 logmsg0++;
3543 *logmsg = strdup(logmsg0);
3544 if (*logmsg == NULL)
3545 return got_error_from_errno("strdup");
3547 trim_logmsg(*logmsg, limit);
3548 return NULL;
3551 static const struct got_error *
3552 show_rebase_progress(struct got_commit_object *commit,
3553 struct got_object_id *old_id, struct got_object_id *new_id)
3555 const struct got_error *err;
3556 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3558 err = got_object_id_str(&old_id_str, old_id);
3559 if (err)
3560 goto done;
3562 if (new_id) {
3563 err = got_object_id_str(&new_id_str, new_id);
3564 if (err)
3565 goto done;
3568 old_id_str[12] = '\0';
3569 if (new_id_str)
3570 new_id_str[12] = '\0';
3572 err = get_short_logmsg(&logmsg, 42, commit);
3573 if (err)
3574 goto done;
3576 printf("%s -> %s: %s\n", old_id_str,
3577 new_id_str ? new_id_str : "no-op change", logmsg);
3578 done:
3579 free(old_id_str);
3580 free(new_id_str);
3581 return err;
3584 static const struct got_error *
3585 rebase_progress(void *arg, unsigned char status, const char *path)
3587 unsigned char *rebase_status = arg;
3589 while (path[0] == '/')
3590 path++;
3591 printf("%c %s\n", status, path);
3593 if (*rebase_status == GOT_STATUS_CONFLICT)
3594 return NULL;
3595 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3596 *rebase_status = status;
3597 return NULL;
3600 static const struct got_error *
3601 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3602 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3603 struct got_repository *repo)
3605 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3606 return got_worktree_rebase_complete(worktree,
3607 new_base_branch, tmp_branch, branch, repo);
3610 static const struct got_error *
3611 rebase_commit(struct got_pathlist_head *merged_paths,
3612 struct got_worktree *worktree, struct got_reference *tmp_branch,
3613 struct got_object_id *commit_id, struct got_repository *repo)
3615 const struct got_error *error;
3616 struct got_commit_object *commit;
3617 struct got_object_id *new_commit_id;
3619 error = got_object_open_as_commit(&commit, repo, commit_id);
3620 if (error)
3621 return error;
3623 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3624 worktree, tmp_branch, commit, commit_id, repo);
3625 if (error) {
3626 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3627 goto done;
3628 error = show_rebase_progress(commit, commit_id, NULL);
3629 } else {
3630 error = show_rebase_progress(commit, commit_id, new_commit_id);
3631 free(new_commit_id);
3633 done:
3634 got_object_commit_close(commit);
3635 return error;
3638 struct check_path_prefix_arg {
3639 const char *path_prefix;
3640 size_t len;
3641 int errcode;
3644 static const struct got_error *
3645 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3646 struct got_blob_object *blob2, struct got_object_id *id1,
3647 struct got_object_id *id2, const char *path1, const char *path2,
3648 struct got_repository *repo)
3650 struct check_path_prefix_arg *a = arg;
3652 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3653 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3654 return got_error(a->errcode);
3656 return NULL;
3659 static const struct got_error *
3660 check_path_prefix(struct got_object_id *parent_id,
3661 struct got_object_id *commit_id, const char *path_prefix,
3662 int errcode, struct got_repository *repo)
3664 const struct got_error *err;
3665 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3666 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3667 struct check_path_prefix_arg cpp_arg;
3669 if (got_path_is_root_dir(path_prefix))
3670 return NULL;
3672 err = got_object_open_as_commit(&commit, repo, commit_id);
3673 if (err)
3674 goto done;
3676 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3677 if (err)
3678 goto done;
3680 err = got_object_open_as_tree(&tree1, repo,
3681 got_object_commit_get_tree_id(parent_commit));
3682 if (err)
3683 goto done;
3685 err = got_object_open_as_tree(&tree2, repo,
3686 got_object_commit_get_tree_id(commit));
3687 if (err)
3688 goto done;
3690 cpp_arg.path_prefix = path_prefix;
3691 while (cpp_arg.path_prefix[0] == '/')
3692 cpp_arg.path_prefix++;
3693 cpp_arg.len = strlen(cpp_arg.path_prefix);
3694 cpp_arg.errcode = errcode;
3695 err = got_diff_tree(tree1, tree2, "", "", repo,
3696 check_path_prefix_in_diff, &cpp_arg);
3697 done:
3698 if (tree1)
3699 got_object_tree_close(tree1);
3700 if (tree2)
3701 got_object_tree_close(tree2);
3702 if (commit)
3703 got_object_commit_close(commit);
3704 if (parent_commit)
3705 got_object_commit_close(parent_commit);
3706 return err;
3709 static const struct got_error *
3710 collect_commits(struct got_object_id_queue *commits,
3711 struct got_object_id *initial_commit_id,
3712 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3713 const char *path_prefix, int path_prefix_errcode,
3714 struct got_repository *repo)
3716 const struct got_error *err = NULL;
3717 struct got_commit_graph *graph = NULL;
3718 struct got_object_id *parent_id = NULL;
3719 struct got_object_qid *qid;
3720 struct got_object_id *commit_id = initial_commit_id;
3722 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3723 if (err)
3724 return err;
3726 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3727 if (err)
3728 goto done;
3729 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3730 err = got_commit_graph_iter_next(&parent_id, graph);
3731 if (err) {
3732 if (err->code == GOT_ERR_ITER_COMPLETED) {
3733 err = got_error_msg(GOT_ERR_ANCESTRY,
3734 "ran out of commits to rebase before "
3735 "youngest common ancestor commit has "
3736 "been reached?!?");
3737 goto done;
3738 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3739 goto done;
3740 err = got_commit_graph_fetch_commits(graph, 1, repo);
3741 if (err)
3742 goto done;
3743 } else {
3744 err = check_path_prefix(parent_id, commit_id,
3745 path_prefix, path_prefix_errcode, repo);
3746 if (err)
3747 goto done;
3749 err = got_object_qid_alloc(&qid, commit_id);
3750 if (err)
3751 goto done;
3752 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3753 commit_id = parent_id;
3756 done:
3757 got_commit_graph_close(graph);
3758 return err;
3761 static const struct got_error *
3762 cmd_rebase(int argc, char *argv[])
3764 const struct got_error *error = NULL;
3765 struct got_worktree *worktree = NULL;
3766 struct got_repository *repo = NULL;
3767 char *cwd = NULL;
3768 struct got_reference *branch = NULL;
3769 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3770 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3771 struct got_object_id *resume_commit_id = NULL;
3772 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3773 struct got_commit_object *commit = NULL;
3774 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3775 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3776 struct got_object_id_queue commits;
3777 struct got_pathlist_head merged_paths;
3778 const struct got_object_id_queue *parent_ids;
3779 struct got_object_qid *qid, *pid;
3781 SIMPLEQ_INIT(&commits);
3782 TAILQ_INIT(&merged_paths);
3784 while ((ch = getopt(argc, argv, "ac")) != -1) {
3785 switch (ch) {
3786 case 'a':
3787 abort_rebase = 1;
3788 break;
3789 case 'c':
3790 continue_rebase = 1;
3791 break;
3792 default:
3793 usage_rebase();
3794 /* NOTREACHED */
3798 argc -= optind;
3799 argv += optind;
3801 #ifndef PROFILE
3802 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3803 "unveil", NULL) == -1)
3804 err(1, "pledge");
3805 #endif
3806 if (abort_rebase && continue_rebase)
3807 usage_rebase();
3808 else if (abort_rebase || continue_rebase) {
3809 if (argc != 0)
3810 usage_rebase();
3811 } else if (argc != 1)
3812 usage_rebase();
3814 cwd = getcwd(NULL, 0);
3815 if (cwd == NULL) {
3816 error = got_error_from_errno("getcwd");
3817 goto done;
3819 error = got_worktree_open(&worktree, cwd);
3820 if (error)
3821 goto done;
3823 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3824 if (error != NULL)
3825 goto done;
3827 error = apply_unveil(got_repo_get_path(repo), 0,
3828 got_worktree_get_root_path(worktree));
3829 if (error)
3830 goto done;
3832 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3833 if (error)
3834 goto done;
3836 if (abort_rebase) {
3837 int did_something;
3838 if (!rebase_in_progress) {
3839 error = got_error(GOT_ERR_NOT_REBASING);
3840 goto done;
3842 error = got_worktree_rebase_continue(&resume_commit_id,
3843 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3844 if (error)
3845 goto done;
3846 printf("Switching work tree to %s\n",
3847 got_ref_get_symref_target(new_base_branch));
3848 error = got_worktree_rebase_abort(worktree, repo,
3849 new_base_branch, update_progress, &did_something);
3850 if (error)
3851 goto done;
3852 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3853 goto done; /* nothing else to do */
3856 if (continue_rebase) {
3857 if (!rebase_in_progress) {
3858 error = got_error(GOT_ERR_NOT_REBASING);
3859 goto done;
3861 error = got_worktree_rebase_continue(&resume_commit_id,
3862 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3863 if (error)
3864 goto done;
3866 error = rebase_commit(NULL, worktree, tmp_branch,
3867 resume_commit_id, repo);
3868 if (error)
3869 goto done;
3871 yca_id = got_object_id_dup(resume_commit_id);
3872 if (yca_id == NULL) {
3873 error = got_error_from_errno("got_object_id_dup");
3874 goto done;
3876 } else {
3877 error = got_ref_open(&branch, repo, argv[0], 0);
3878 if (error != NULL)
3879 goto done;
3881 error = check_same_branch(
3882 got_worktree_get_base_commit_id(worktree), branch, repo);
3883 if (error) {
3884 if (error->code != GOT_ERR_ANCESTRY)
3885 goto done;
3886 error = NULL;
3887 } else {
3888 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3889 "specified branch resolves to a commit which "
3890 "is already contained in work tree's branch");
3891 goto done;
3895 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3896 if (error)
3897 goto done;
3899 if (!continue_rebase) {
3900 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3901 got_worktree_get_base_commit_id(worktree),
3902 branch_head_commit_id, repo);
3903 if (error)
3904 goto done;
3905 if (yca_id == NULL) {
3906 error = got_error_msg(GOT_ERR_ANCESTRY,
3907 "specified branch shares no common ancestry "
3908 "with work tree's branch");
3909 goto done;
3912 error = got_worktree_rebase_prepare(&new_base_branch,
3913 &tmp_branch, worktree, branch, repo);
3914 if (error)
3915 goto done;
3918 commit_id = branch_head_commit_id;
3919 error = got_object_open_as_commit(&commit, repo, commit_id);
3920 if (error)
3921 goto done;
3923 parent_ids = got_object_commit_get_parent_ids(commit);
3924 pid = SIMPLEQ_FIRST(parent_ids);
3925 error = collect_commits(&commits, commit_id, pid->id,
3926 yca_id, got_worktree_get_path_prefix(worktree),
3927 GOT_ERR_REBASE_PATH, repo);
3928 got_object_commit_close(commit);
3929 commit = NULL;
3930 if (error)
3931 goto done;
3933 if (SIMPLEQ_EMPTY(&commits)) {
3934 if (continue_rebase)
3935 error = rebase_complete(worktree, branch,
3936 new_base_branch, tmp_branch, repo);
3937 else
3938 error = got_error(GOT_ERR_EMPTY_REBASE);
3939 goto done;
3942 pid = NULL;
3943 SIMPLEQ_FOREACH(qid, &commits, entry) {
3944 commit_id = qid->id;
3945 parent_id = pid ? pid->id : yca_id;
3946 pid = qid;
3948 error = got_worktree_rebase_merge_files(&merged_paths,
3949 worktree, parent_id, commit_id, repo, rebase_progress,
3950 &rebase_status, check_cancelled, NULL);
3951 if (error)
3952 goto done;
3954 if (rebase_status == GOT_STATUS_CONFLICT) {
3955 got_worktree_rebase_pathlist_free(&merged_paths);
3956 break;
3959 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3960 commit_id, repo);
3961 got_worktree_rebase_pathlist_free(&merged_paths);
3962 if (error)
3963 goto done;
3966 if (rebase_status == GOT_STATUS_CONFLICT) {
3967 error = got_worktree_rebase_postpone(worktree);
3968 if (error)
3969 goto done;
3970 error = got_error_msg(GOT_ERR_CONFLICTS,
3971 "conflicts must be resolved before rebasing can continue");
3972 } else
3973 error = rebase_complete(worktree, branch, new_base_branch,
3974 tmp_branch, repo);
3975 done:
3976 got_object_id_queue_free(&commits);
3977 free(branch_head_commit_id);
3978 free(resume_commit_id);
3979 free(yca_id);
3980 if (commit)
3981 got_object_commit_close(commit);
3982 if (branch)
3983 got_ref_close(branch);
3984 if (new_base_branch)
3985 got_ref_close(new_base_branch);
3986 if (tmp_branch)
3987 got_ref_close(tmp_branch);
3988 if (worktree)
3989 got_worktree_close(worktree);
3990 if (repo)
3991 got_repo_close(repo);
3992 return error;
3995 __dead static void
3996 usage_histedit(void)
3998 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
3999 getprogname());
4000 exit(1);
4003 #define GOT_HISTEDIT_PICK 'p'
4004 #define GOT_HISTEDIT_EDIT 'e'
4005 #define GOT_HISTEDIT_FOLD 'f'
4006 #define GOT_HISTEDIT_DROP 'd'
4007 #define GOT_HISTEDIT_MESG 'm'
4009 static struct got_histedit_cmd {
4010 unsigned char code;
4011 const char *name;
4012 const char *desc;
4013 } got_histedit_cmds[] = {
4014 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4015 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4016 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4017 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4018 { GOT_HISTEDIT_MESG, "mesg",
4019 "single-line log message for commit above (open editor if empty)" },
4022 struct got_histedit_list_entry {
4023 TAILQ_ENTRY(got_histedit_list_entry) entry;
4024 struct got_object_id *commit_id;
4025 const struct got_histedit_cmd *cmd;
4026 char *logmsg;
4028 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4030 static const struct got_error *
4031 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4032 FILE *f, struct got_repository *repo)
4034 const struct got_error *err = NULL;
4035 char *logmsg = NULL, *id_str = NULL;
4036 struct got_commit_object *commit = NULL;
4037 size_t n;
4039 err = got_object_open_as_commit(&commit, repo, commit_id);
4040 if (err)
4041 goto done;
4043 err = get_short_logmsg(&logmsg, 34, commit);
4044 if (err)
4045 goto done;
4047 err = got_object_id_str(&id_str, commit_id);
4048 if (err)
4049 goto done;
4051 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4052 if (n < 0)
4053 err = got_ferror(f, GOT_ERR_IO);
4054 done:
4055 if (commit)
4056 got_object_commit_close(commit);
4057 free(id_str);
4058 free(logmsg);
4059 return err;
4062 static const struct got_error *
4063 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4064 struct got_repository *repo)
4066 const struct got_error *err = NULL;
4067 struct got_object_qid *qid;
4069 if (SIMPLEQ_EMPTY(commits))
4070 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4072 SIMPLEQ_FOREACH(qid, commits, entry) {
4073 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4074 f, repo);
4075 if (err)
4076 break;
4079 return err;
4082 static const struct got_error *
4083 write_cmd_list(FILE *f)
4085 const struct got_error *err = NULL;
4086 int n, i;
4088 n = fprintf(f, "# Available histedit commands:\n");
4089 if (n < 0)
4090 return got_ferror(f, GOT_ERR_IO);
4092 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4093 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4094 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4095 cmd->desc);
4096 if (n < 0) {
4097 err = got_ferror(f, GOT_ERR_IO);
4098 break;
4101 n = fprintf(f, "# Commits will be processed in order from top to "
4102 "bottom of this file.\n");
4103 if (n < 0)
4104 return got_ferror(f, GOT_ERR_IO);
4105 return err;
4108 static const struct got_error *
4109 histedit_syntax_error(int lineno)
4111 static char msg[42];
4112 int ret;
4114 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4115 lineno);
4116 if (ret == -1 || ret >= sizeof(msg))
4117 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4119 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4122 static const struct got_error *
4123 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4124 char *logmsg, struct got_repository *repo)
4126 const struct got_error *err;
4127 struct got_commit_object *folded_commit = NULL;
4128 char *id_str;
4130 err = got_object_id_str(&id_str, hle->commit_id);
4131 if (err)
4132 return err;
4134 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4135 if (err)
4136 goto done;
4138 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4139 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4140 got_object_commit_get_logmsg(folded_commit)) == -1) {
4141 err = got_error_from_errno("asprintf");
4142 goto done;
4144 done:
4145 if (folded_commit)
4146 got_object_commit_close(folded_commit);
4147 free(id_str);
4148 return err;
4151 static struct got_histedit_list_entry *
4152 get_folded_commits(struct got_histedit_list_entry *hle)
4154 struct got_histedit_list_entry *prev, *folded = NULL;
4156 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4157 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4158 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4159 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4160 folded = prev;
4161 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4164 return folded;
4167 static const struct got_error *
4168 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4169 struct got_repository *repo)
4171 char *logmsg_path = NULL, *id_str = NULL;
4172 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4173 const struct got_error *err = NULL;
4174 struct got_commit_object *commit = NULL;
4175 int fd;
4176 struct got_histedit_list_entry *folded = NULL;
4178 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4179 if (err)
4180 return err;
4182 folded = get_folded_commits(hle);
4183 if (folded) {
4184 while (folded != hle) {
4185 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4186 folded = TAILQ_NEXT(folded, entry);
4187 continue;
4189 err = append_folded_commit_msg(&new_msg, folded,
4190 logmsg, repo);
4191 if (err)
4192 goto done;
4193 free(logmsg);
4194 logmsg = new_msg;
4195 folded = TAILQ_NEXT(folded, entry);
4199 err = got_object_id_str(&id_str, hle->commit_id);
4200 if (err)
4201 goto done;
4202 if (asprintf(&new_msg,
4203 "%s\n# original log message of commit %s: %s",
4204 logmsg ? logmsg : "", id_str,
4205 got_object_commit_get_logmsg(commit)) == -1) {
4206 err = got_error_from_errno("asprintf");
4207 goto done;
4209 free(logmsg);
4210 logmsg = new_msg;
4212 err = got_object_id_str(&id_str, hle->commit_id);
4213 if (err)
4214 goto done;
4216 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4217 if (err)
4218 goto done;
4220 dprintf(fd, logmsg);
4221 close(fd);
4223 err = get_editor(&editor);
4224 if (err)
4225 goto done;
4227 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4228 if (err) {
4229 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4230 goto done;
4231 err = NULL;
4232 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4233 if (hle->logmsg == NULL)
4234 err = got_error_from_errno("strdup");
4236 done:
4237 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4238 err = got_error_from_errno2("unlink", logmsg_path);
4239 free(logmsg_path);
4240 free(logmsg);
4241 free(editor);
4242 if (commit)
4243 got_object_commit_close(commit);
4244 return err;
4247 static const struct got_error *
4248 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4249 FILE *f, struct got_repository *repo)
4251 const struct got_error *err = NULL;
4252 char *line = NULL, *p, *end;
4253 size_t size;
4254 ssize_t len;
4255 int lineno = 0, i;
4256 const struct got_histedit_cmd *cmd;
4257 struct got_object_id *commit_id = NULL;
4258 struct got_histedit_list_entry *hle = NULL;
4260 for (;;) {
4261 len = getline(&line, &size, f);
4262 if (len == -1) {
4263 const struct got_error *getline_err;
4264 if (feof(f))
4265 break;
4266 getline_err = got_error_from_errno("getline");
4267 err = got_ferror(f, getline_err->code);
4268 break;
4270 lineno++;
4271 p = line;
4272 while (isspace((unsigned char)p[0]))
4273 p++;
4274 if (p[0] == '#' || p[0] == '\0') {
4275 free(line);
4276 line = NULL;
4277 continue;
4279 cmd = NULL;
4280 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4281 cmd = &got_histedit_cmds[i];
4282 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4283 isspace((unsigned char)p[strlen(cmd->name)])) {
4284 p += strlen(cmd->name);
4285 break;
4287 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4288 p++;
4289 break;
4292 if (cmd == NULL) {
4293 err = histedit_syntax_error(lineno);
4294 break;
4296 while (isspace((unsigned char)p[0]))
4297 p++;
4298 if (cmd->code == GOT_HISTEDIT_MESG) {
4299 if (hle == NULL || hle->logmsg != NULL) {
4300 err = got_error(GOT_ERR_HISTEDIT_CMD);
4301 break;
4303 if (p[0] == '\0') {
4304 err = histedit_edit_logmsg(hle, repo);
4305 if (err)
4306 break;
4307 } else {
4308 hle->logmsg = strdup(p);
4309 if (hle->logmsg == NULL) {
4310 err = got_error_from_errno("strdup");
4311 break;
4314 free(line);
4315 line = NULL;
4316 continue;
4317 } else {
4318 end = p;
4319 while (end[0] && !isspace((unsigned char)end[0]))
4320 end++;
4321 *end = '\0';
4323 err = got_object_resolve_id_str(&commit_id, repo, p);
4324 if (err) {
4325 /* override error code */
4326 err = histedit_syntax_error(lineno);
4327 break;
4330 hle = malloc(sizeof(*hle));
4331 if (hle == NULL) {
4332 err = got_error_from_errno("malloc");
4333 break;
4335 hle->cmd = cmd;
4336 hle->commit_id = commit_id;
4337 hle->logmsg = NULL;
4338 commit_id = NULL;
4339 free(line);
4340 line = NULL;
4341 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4344 free(line);
4345 free(commit_id);
4346 return err;
4349 static const struct got_error *
4350 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4351 const char *path, struct got_repository *repo)
4353 const struct got_error *err = NULL;
4354 char *editor;
4355 FILE *f = NULL;
4357 err = get_editor(&editor);
4358 if (err)
4359 return err;
4361 if (spawn_editor(editor, path) == -1) {
4362 err = got_error_from_errno("failed spawning editor");
4363 goto done;
4366 f = fopen(path, "r");
4367 if (f == NULL) {
4368 err = got_error_from_errno("fopen");
4369 goto done;
4371 err = histedit_parse_list(histedit_cmds, f, repo);
4372 done:
4373 if (f && fclose(f) != 0 && err == NULL)
4374 err = got_error_from_errno("fclose");
4375 free(editor);
4376 return err;
4379 static const struct got_error *
4380 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4381 struct got_object_id_queue *, const char *, struct got_repository *);
4383 static const struct got_error *
4384 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4385 struct got_object_id_queue *commits, struct got_repository *repo)
4387 const struct got_error *err;
4388 FILE *f = NULL;
4389 char *path = NULL;
4391 err = got_opentemp_named(&path, &f, "got-histedit");
4392 if (err)
4393 return err;
4395 err = write_cmd_list(f);
4396 if (err)
4397 goto done;
4399 err = histedit_write_commit_list(commits, f, repo);
4400 if (err)
4401 goto done;
4403 if (fclose(f) != 0) {
4404 err = got_error_from_errno("fclose");
4405 goto done;
4407 f = NULL;
4409 err = histedit_run_editor(histedit_cmds, path, repo);
4410 if (err) {
4411 const char *errmsg = err->msg;
4412 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4413 goto done;
4414 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4415 commits, path, repo);
4417 done:
4418 if (f && fclose(f) != 0 && err == NULL)
4419 err = got_error_from_errno("fclose");
4420 if (path && unlink(path) != 0 && err == NULL)
4421 err = got_error_from_errno2("unlink", path);
4422 free(path);
4423 return err;
4426 static const struct got_error *
4427 histedit_save_list(struct got_histedit_list *histedit_cmds,
4428 struct got_worktree *worktree, struct got_repository *repo)
4430 const struct got_error *err = NULL;
4431 char *path = NULL;
4432 FILE *f = NULL;
4433 struct got_histedit_list_entry *hle;
4434 struct got_commit_object *commit = NULL;
4436 err = got_worktree_get_histedit_list_path(&path, worktree);
4437 if (err)
4438 return err;
4440 f = fopen(path, "w");
4441 if (f == NULL) {
4442 err = got_error_from_errno2("fopen", path);
4443 goto done;
4445 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4446 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4447 repo);
4448 if (err)
4449 break;
4451 if (hle->logmsg) {
4452 int n = fprintf(f, "%c %s\n",
4453 GOT_HISTEDIT_MESG, hle->logmsg);
4454 if (n < 0) {
4455 err = got_ferror(f, GOT_ERR_IO);
4456 break;
4460 done:
4461 if (f && fclose(f) != 0 && err == NULL)
4462 err = got_error_from_errno("fclose");
4463 free(path);
4464 if (commit)
4465 got_object_commit_close(commit);
4466 return err;
4469 static const struct got_error *
4470 histedit_load_list(struct got_histedit_list *histedit_cmds,
4471 const char *path, struct got_repository *repo)
4473 const struct got_error *err = NULL;
4474 FILE *f = NULL;
4476 f = fopen(path, "r");
4477 if (f == NULL) {
4478 err = got_error_from_errno2("fopen", path);
4479 goto done;
4482 err = histedit_parse_list(histedit_cmds, f, repo);
4483 done:
4484 if (f && fclose(f) != 0 && err == NULL)
4485 err = got_error_from_errno("fclose");
4486 return err;
4489 static const struct got_error *
4490 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4491 const char *errmsg, struct got_object_id_queue *commits,
4492 const char *path, struct got_repository *repo)
4494 const struct got_error *err = NULL;
4495 int resp = ' ';
4497 while (resp != 'c' && resp != 'r' && resp != 'q') {
4498 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4499 "or (a)bort: ", getprogname(), errmsg);
4500 resp = getchar();
4501 switch (resp) {
4502 case 'c':
4503 err = histedit_run_editor(histedit_cmds, path, repo);
4504 break;
4505 case 'r':
4506 err = histedit_edit_script(histedit_cmds,
4507 commits, repo);
4508 break;
4509 case 'a':
4510 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4511 break;
4512 default:
4513 printf("invalid response '%c'\n", resp);
4514 break;
4518 return err;
4521 static const struct got_error *
4522 histedit_complete(struct got_worktree *worktree,
4523 struct got_reference *tmp_branch, struct got_reference *branch,
4524 struct got_repository *repo)
4526 printf("Switching work tree to %s\n",
4527 got_ref_get_symref_target(branch));
4528 return got_worktree_histedit_complete(worktree, tmp_branch, branch,
4529 repo);
4532 static const struct got_error *
4533 show_histedit_progress(struct got_commit_object *commit,
4534 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4536 const struct got_error *err;
4537 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4539 err = got_object_id_str(&old_id_str, hle->commit_id);
4540 if (err)
4541 goto done;
4543 if (new_id) {
4544 err = got_object_id_str(&new_id_str, new_id);
4545 if (err)
4546 goto done;
4549 old_id_str[12] = '\0';
4550 if (new_id_str)
4551 new_id_str[12] = '\0';
4553 if (hle->logmsg) {
4554 logmsg = strdup(hle->logmsg);
4555 if (logmsg == NULL) {
4556 err = got_error_from_errno("strdup");
4557 goto done;
4559 trim_logmsg(logmsg, 42);
4560 } else {
4561 err = get_short_logmsg(&logmsg, 42, commit);
4562 if (err)
4563 goto done;
4566 switch (hle->cmd->code) {
4567 case GOT_HISTEDIT_PICK:
4568 case GOT_HISTEDIT_EDIT:
4569 printf("%s -> %s: %s\n", old_id_str,
4570 new_id_str ? new_id_str : "no-op change", logmsg);
4571 break;
4572 case GOT_HISTEDIT_DROP:
4573 case GOT_HISTEDIT_FOLD:
4574 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4575 logmsg);
4576 break;
4577 default:
4578 break;
4581 done:
4582 free(old_id_str);
4583 free(new_id_str);
4584 return err;
4587 static const struct got_error *
4588 histedit_commit(struct got_pathlist_head *merged_paths,
4589 struct got_worktree *worktree, struct got_reference *tmp_branch,
4590 struct got_histedit_list_entry *hle, struct got_repository *repo)
4592 const struct got_error *err;
4593 struct got_commit_object *commit;
4594 struct got_object_id *new_commit_id;
4596 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4597 && hle->logmsg == NULL) {
4598 err = histedit_edit_logmsg(hle, repo);
4599 if (err)
4600 return err;
4603 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4604 if (err)
4605 return err;
4607 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4608 worktree, tmp_branch, commit, hle->commit_id, hle->logmsg, repo);
4609 if (err) {
4610 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4611 goto done;
4612 err = show_histedit_progress(commit, hle, NULL);
4613 } else {
4614 err = show_histedit_progress(commit, hle, new_commit_id);
4615 free(new_commit_id);
4617 done:
4618 got_object_commit_close(commit);
4619 return err;
4622 static const struct got_error *
4623 histedit_skip_commit(struct got_histedit_list_entry *hle,
4624 struct got_worktree *worktree, struct got_repository *repo)
4626 const struct got_error *error;
4627 struct got_commit_object *commit;
4629 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4630 repo);
4631 if (error)
4632 return error;
4634 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4635 if (error)
4636 return error;
4638 error = show_histedit_progress(commit, hle, NULL);
4639 got_object_commit_close(commit);
4640 return error;
4643 static const struct got_error *
4644 histedit_check_script(struct got_histedit_list *histedit_cmds,
4645 struct got_object_id_queue *commits, struct got_repository *repo)
4647 const struct got_error *err = NULL;
4648 struct got_object_qid *qid;
4649 struct got_histedit_list_entry *hle;
4650 static char msg[80];
4651 char *id_str;
4653 if (TAILQ_EMPTY(histedit_cmds))
4654 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4656 SIMPLEQ_FOREACH(qid, commits, entry) {
4657 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4658 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4659 break;
4661 if (hle == NULL) {
4662 err = got_object_id_str(&id_str, qid->id);
4663 if (err)
4664 return err;
4665 snprintf(msg, sizeof(msg),
4666 "commit %s missing from histedit script", id_str);
4667 free(id_str);
4668 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4672 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4673 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4674 "last commit in histedit script cannot be folded");
4676 return NULL;
4679 static const struct got_error *
4680 cmd_histedit(int argc, char *argv[])
4682 const struct got_error *error = NULL;
4683 struct got_worktree *worktree = NULL;
4684 struct got_repository *repo = NULL;
4685 char *cwd = NULL;
4686 struct got_reference *branch = NULL;
4687 struct got_reference *tmp_branch = NULL;
4688 struct got_object_id *resume_commit_id = NULL;
4689 struct got_object_id *base_commit_id = NULL;
4690 struct got_object_id *head_commit_id = NULL;
4691 struct got_commit_object *commit = NULL;
4692 int ch, rebase_in_progress = 0;
4693 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4694 const char *edit_script_path = NULL;
4695 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4696 struct got_object_id_queue commits;
4697 struct got_pathlist_head merged_paths;
4698 const struct got_object_id_queue *parent_ids;
4699 struct got_object_qid *pid;
4700 struct got_histedit_list histedit_cmds;
4701 struct got_histedit_list_entry *hle;
4703 SIMPLEQ_INIT(&commits);
4704 TAILQ_INIT(&histedit_cmds);
4705 TAILQ_INIT(&merged_paths);
4707 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4708 switch (ch) {
4709 case 'a':
4710 abort_edit = 1;
4711 break;
4712 case 'c':
4713 continue_edit = 1;
4714 break;
4715 case 'F':
4716 edit_script_path = optarg;
4717 break;
4718 default:
4719 usage_histedit();
4720 /* NOTREACHED */
4724 argc -= optind;
4725 argv += optind;
4727 #ifndef PROFILE
4728 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4729 "unveil", NULL) == -1)
4730 err(1, "pledge");
4731 #endif
4732 if (abort_edit && continue_edit)
4733 usage_histedit();
4734 if (argc != 0)
4735 usage_histedit();
4738 * This command cannot apply unveil(2) in all cases because the
4739 * user may choose to run an editor to edit the histedit script
4740 * and to edit individual commit log messages.
4741 * unveil(2) traverses exec(2); if an editor is used we have to
4742 * apply unveil after edit script and log messages have been written.
4743 * XXX TODO: Make use of unveil(2) where possible.
4746 cwd = getcwd(NULL, 0);
4747 if (cwd == NULL) {
4748 error = got_error_from_errno("getcwd");
4749 goto done;
4751 error = got_worktree_open(&worktree, cwd);
4752 if (error)
4753 goto done;
4755 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4756 if (error != NULL)
4757 goto done;
4759 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4760 if (error)
4761 goto done;
4762 if (rebase_in_progress) {
4763 error = got_error(GOT_ERR_REBASING);
4764 goto done;
4767 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4768 if (error)
4769 goto done;
4771 if (edit_in_progress && abort_edit) {
4772 int did_something;
4773 error = got_worktree_histedit_continue(&resume_commit_id,
4774 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4775 if (error)
4776 goto done;
4777 printf("Switching work tree to %s\n",
4778 got_ref_get_symref_target(branch));
4779 error = got_worktree_histedit_abort(worktree, repo,
4780 branch, base_commit_id, update_progress, &did_something);
4781 if (error)
4782 goto done;
4783 printf("Histedit of %s aborted\n",
4784 got_ref_get_symref_target(branch));
4785 goto done; /* nothing else to do */
4786 } else if (abort_edit) {
4787 error = got_error(GOT_ERR_NOT_HISTEDIT);
4788 goto done;
4791 if (continue_edit) {
4792 char *path;
4794 if (!edit_in_progress) {
4795 error = got_error(GOT_ERR_NOT_HISTEDIT);
4796 goto done;
4799 error = got_worktree_get_histedit_list_path(&path, worktree);
4800 if (error)
4801 goto done;
4803 error = histedit_load_list(&histedit_cmds, path, repo);
4804 free(path);
4805 if (error)
4806 goto done;
4808 error = got_worktree_histedit_continue(&resume_commit_id,
4809 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4810 if (error)
4811 goto done;
4813 error = got_ref_resolve(&head_commit_id, repo, branch);
4814 if (error)
4815 goto done;
4817 error = got_object_open_as_commit(&commit, repo,
4818 head_commit_id);
4819 if (error)
4820 goto done;
4821 parent_ids = got_object_commit_get_parent_ids(commit);
4822 pid = SIMPLEQ_FIRST(parent_ids);
4823 if (pid == NULL) {
4824 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4825 goto done;
4827 error = collect_commits(&commits, head_commit_id, pid->id,
4828 base_commit_id, got_worktree_get_path_prefix(worktree),
4829 GOT_ERR_HISTEDIT_PATH, repo);
4830 got_object_commit_close(commit);
4831 commit = NULL;
4832 if (error)
4833 goto done;
4834 } else {
4835 if (edit_in_progress) {
4836 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4837 goto done;
4840 error = got_ref_open(&branch, repo,
4841 got_worktree_get_head_ref_name(worktree), 0);
4842 if (error != NULL)
4843 goto done;
4845 error = got_ref_resolve(&head_commit_id, repo, branch);
4846 if (error)
4847 goto done;
4849 error = got_object_open_as_commit(&commit, repo,
4850 head_commit_id);
4851 if (error)
4852 goto done;
4853 parent_ids = got_object_commit_get_parent_ids(commit);
4854 pid = SIMPLEQ_FIRST(parent_ids);
4855 if (pid == NULL) {
4856 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4857 goto done;
4859 error = collect_commits(&commits, head_commit_id, pid->id,
4860 got_worktree_get_base_commit_id(worktree),
4861 got_worktree_get_path_prefix(worktree),
4862 GOT_ERR_HISTEDIT_PATH, repo);
4863 got_object_commit_close(commit);
4864 commit = NULL;
4865 if (error)
4866 goto done;
4868 if (edit_script_path) {
4869 error = histedit_load_list(&histedit_cmds,
4870 edit_script_path, repo);
4871 if (error)
4872 goto done;
4873 } else {
4874 error = histedit_edit_script(&histedit_cmds, &commits,
4875 repo);
4876 if (error)
4877 goto done;
4881 error = histedit_save_list(&histedit_cmds, worktree,
4882 repo);
4883 if (error)
4884 goto done;
4886 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4887 &base_commit_id, worktree, repo);
4888 if (error)
4889 goto done;
4893 error = histedit_check_script(&histedit_cmds, &commits, repo);
4894 if (error)
4895 goto done;
4897 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4898 if (resume_commit_id) {
4899 if (got_object_id_cmp(hle->commit_id,
4900 resume_commit_id) != 0)
4901 continue;
4903 resume_commit_id = NULL;
4904 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4905 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4906 error = histedit_skip_commit(hle, worktree,
4907 repo);
4908 } else {
4909 error = histedit_commit(NULL, worktree,
4910 tmp_branch, hle, repo);
4912 if (error)
4913 goto done;
4914 continue;
4917 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4918 error = histedit_skip_commit(hle, worktree, repo);
4919 if (error)
4920 goto done;
4921 continue;
4924 error = got_object_open_as_commit(&commit, repo,
4925 hle->commit_id);
4926 if (error)
4927 goto done;
4928 parent_ids = got_object_commit_get_parent_ids(commit);
4929 pid = SIMPLEQ_FIRST(parent_ids);
4931 error = got_worktree_histedit_merge_files(&merged_paths,
4932 worktree, pid->id, hle->commit_id, repo, rebase_progress,
4933 &rebase_status, check_cancelled, NULL);
4934 if (error)
4935 goto done;
4936 got_object_commit_close(commit);
4937 commit = NULL;
4939 if (rebase_status == GOT_STATUS_CONFLICT) {
4940 got_worktree_rebase_pathlist_free(&merged_paths);
4941 break;
4944 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4945 char *id_str;
4946 error = got_object_id_str(&id_str, hle->commit_id);
4947 if (error)
4948 goto done;
4949 printf("Stopping histedit for amending commit %s\n",
4950 id_str);
4951 free(id_str);
4952 got_worktree_rebase_pathlist_free(&merged_paths);
4953 error = got_worktree_histedit_postpone(worktree);
4954 goto done;
4957 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4958 error = histedit_skip_commit(hle, worktree, repo);
4959 if (error)
4960 goto done;
4961 continue;
4964 error = histedit_commit(&merged_paths, worktree, tmp_branch,
4965 hle, repo);
4966 got_worktree_rebase_pathlist_free(&merged_paths);
4967 if (error)
4968 goto done;
4971 if (rebase_status == GOT_STATUS_CONFLICT) {
4972 error = got_worktree_histedit_postpone(worktree);
4973 if (error)
4974 goto done;
4975 error = got_error_msg(GOT_ERR_CONFLICTS,
4976 "conflicts must be resolved before rebasing can continue");
4977 } else
4978 error = histedit_complete(worktree, tmp_branch, branch, repo);
4979 done:
4980 got_object_id_queue_free(&commits);
4981 free(head_commit_id);
4982 free(base_commit_id);
4983 free(resume_commit_id);
4984 if (commit)
4985 got_object_commit_close(commit);
4986 if (branch)
4987 got_ref_close(branch);
4988 if (tmp_branch)
4989 got_ref_close(tmp_branch);
4990 if (worktree)
4991 got_worktree_close(worktree);
4992 if (repo)
4993 got_repo_close(repo);
4994 return error;