Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_worktree.h"
44 #include "got_diff.h"
45 #include "got_commit_graph.h"
46 #include "got_blame.h"
47 #include "got_privsep.h"
48 #include "got_opentemp.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
57 static void
58 catch_sigint(int signo)
59 {
60 sigint_received = 1;
61 }
63 static void
64 catch_sigpipe(int signo)
65 {
66 sigpipe_received = 1;
67 }
70 struct got_cmd {
71 const char *cmd_name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 const char *cmd_alias;
75 };
77 __dead static void usage(int);
78 __dead static void usage_init(void);
79 __dead static void usage_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
96 __dead static void usage_histedit(void);
98 static const struct got_error* cmd_init(int, char *[]);
99 static const struct got_error* cmd_import(int, char *[]);
100 static const struct got_error* cmd_checkout(int, char *[]);
101 static const struct got_error* cmd_update(int, char *[]);
102 static const struct got_error* cmd_log(int, char *[]);
103 static const struct got_error* cmd_diff(int, char *[]);
104 static const struct got_error* cmd_blame(int, char *[]);
105 static const struct got_error* cmd_tree(int, char *[]);
106 static const struct got_error* cmd_status(int, char *[]);
107 static const struct got_error* cmd_ref(int, char *[]);
108 static const struct got_error* cmd_branch(int, char *[]);
109 static const struct got_error* cmd_add(int, char *[]);
110 static const struct got_error* cmd_remove(int, char *[]);
111 static const struct got_error* cmd_revert(int, char *[]);
112 static const struct got_error* cmd_commit(int, char *[]);
113 static const struct got_error* cmd_cherrypick(int, char *[]);
114 static const struct got_error* cmd_backout(int, char *[]);
115 static const struct got_error* cmd_rebase(int, char *[]);
116 static const struct got_error* cmd_histedit(int, char *[]);
118 static struct got_cmd got_commands[] = {
119 { "init", cmd_init, usage_init, "" },
120 { "import", cmd_import, usage_import, "" },
121 { "checkout", cmd_checkout, usage_checkout, "co" },
122 { "update", cmd_update, usage_update, "up" },
123 { "log", cmd_log, usage_log, "" },
124 { "diff", cmd_diff, usage_diff, "" },
125 { "blame", cmd_blame, usage_blame, "" },
126 { "tree", cmd_tree, usage_tree, "" },
127 { "status", cmd_status, usage_status, "st" },
128 { "ref", cmd_ref, usage_ref, "" },
129 { "branch", cmd_branch, usage_branch, "br" },
130 { "add", cmd_add, usage_add, "" },
131 { "remove", cmd_remove, usage_remove, "rm" },
132 { "revert", cmd_revert, usage_revert, "rv" },
133 { "commit", cmd_commit, usage_commit, "ci" },
134 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
135 { "backout", cmd_backout, usage_backout, "bo" },
136 { "rebase", cmd_rebase, usage_rebase, "rb" },
137 { "histedit", cmd_histedit, usage_histedit, "he" },
138 };
140 static void
141 list_commands(void)
143 int i;
145 fprintf(stderr, "commands:");
146 for (i = 0; i < nitems(got_commands); i++) {
147 struct got_cmd *cmd = &got_commands[i];
148 fprintf(stderr, " %s", cmd->cmd_name);
150 fputc('\n', stderr);
153 int
154 main(int argc, char *argv[])
156 struct got_cmd *cmd;
157 unsigned int i;
158 int ch;
159 int hflag = 0;
161 setlocale(LC_CTYPE, "");
163 while ((ch = getopt(argc, argv, "h")) != -1) {
164 switch (ch) {
165 case 'h':
166 hflag = 1;
167 break;
168 default:
169 usage(hflag);
170 /* NOTREACHED */
174 argc -= optind;
175 argv += optind;
176 optind = 0;
178 if (argc <= 0)
179 usage(hflag);
181 signal(SIGINT, catch_sigint);
182 signal(SIGPIPE, catch_sigpipe);
184 for (i = 0; i < nitems(got_commands); i++) {
185 const struct got_error *error;
187 cmd = &got_commands[i];
189 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
190 strcmp(cmd->cmd_alias, argv[0]) != 0)
191 continue;
193 if (hflag)
194 got_commands[i].cmd_usage();
196 error = got_commands[i].cmd_main(argc, argv);
197 if (error && !(sigint_received || sigpipe_received)) {
198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
199 return 1;
202 return 0;
205 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
206 list_commands();
207 return 1;
210 __dead static void
211 usage(int hflag)
213 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
214 if (hflag)
215 list_commands();
216 exit(1);
219 static const struct got_error *
220 get_editor(char **abspath)
222 const struct got_error *err = NULL;
223 const char *editor;
225 editor = getenv("VISUAL");
226 if (editor == NULL)
227 editor = getenv("EDITOR");
229 if (editor) {
230 err = got_path_find_prog(abspath, editor);
231 if (err)
232 return err;
235 if (*abspath == NULL) {
236 *abspath = strdup("/bin/ed");
237 if (*abspath == NULL)
238 return got_error_from_errno("strdup");
241 return NULL;
244 static const struct got_error *
245 apply_unveil(const char *repo_path, int repo_read_only,
246 const char *worktree_path)
248 const struct got_error *err;
250 #ifdef PROFILE
251 if (unveil("gmon.out", "rwc") != 0)
252 return got_error_from_errno2("unveil", "gmon.out");
253 #endif
254 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
255 return got_error_from_errno2("unveil", repo_path);
257 if (worktree_path && unveil(worktree_path, "rwc") != 0)
258 return got_error_from_errno2("unveil", worktree_path);
260 if (unveil("/tmp", "rwc") != 0)
261 return got_error_from_errno2("unveil", "/tmp");
263 err = got_privsep_unveil_exec_helpers();
264 if (err != NULL)
265 return err;
267 if (unveil(NULL, NULL) != 0)
268 return got_error_from_errno("unveil");
270 return NULL;
273 __dead static void
274 usage_init(void)
276 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
277 exit(1);
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 char *repo_path = NULL;
285 int ch;
287 while ((ch = getopt(argc, argv, "")) != -1) {
288 switch (ch) {
289 default:
290 usage_init();
291 /* NOTREACHED */
295 argc -= optind;
296 argv += optind;
298 #ifndef PROFILE
299 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
300 err(1, "pledge");
301 #endif
302 if (argc != 1)
303 usage_init();
305 repo_path = strdup(argv[0]);
306 if (repo_path == NULL)
307 return got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(repo_path);
311 error = got_path_mkdir(repo_path);
312 if (error &&
313 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
314 goto done;
316 error = apply_unveil(repo_path, 0, NULL);
317 if (error)
318 goto done;
320 error = got_repo_init(repo_path);
321 if (error != NULL)
322 goto done;
324 done:
325 free(repo_path);
326 return error;
329 __dead static void
330 usage_import(void)
332 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 "[-r repository-path] [-I pattern] path\n", getprogname());
334 exit(1);
337 int
338 spawn_editor(const char *editor, const char *file)
340 pid_t pid;
341 sig_t sighup, sigint, sigquit;
342 int st = -1;
344 sighup = signal(SIGHUP, SIG_IGN);
345 sigint = signal(SIGINT, SIG_IGN);
346 sigquit = signal(SIGQUIT, SIG_IGN);
348 switch (pid = fork()) {
349 case -1:
350 goto doneediting;
351 case 0:
352 execl(editor, editor, file, (char *)NULL);
353 _exit(127);
356 while (waitpid(pid, &st, 0) == -1)
357 if (errno != EINTR)
358 break;
360 doneediting:
361 (void)signal(SIGHUP, sighup);
362 (void)signal(SIGINT, sigint);
363 (void)signal(SIGQUIT, sigquit);
365 if (!WIFEXITED(st)) {
366 errno = EINTR;
367 return -1;
370 return WEXITSTATUS(st);
373 static const struct got_error *
374 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 const char *initial_content)
377 const struct got_error *err = NULL;
378 char buf[1024];
379 struct stat st, st2;
380 FILE *fp;
381 int content_changed = 0;
382 size_t len;
384 *logmsg = NULL;
386 if (stat(logmsg_path, &st) == -1)
387 return got_error_from_errno2("stat", logmsg_path);
389 if (spawn_editor(editor, logmsg_path) == -1)
390 return got_error_from_errno("failed spawning editor");
392 if (stat(logmsg_path, &st2) == -1)
393 return got_error_from_errno("stat");
395 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 "no changes made to commit message, aborting");
399 *logmsg = malloc(st2.st_size + 1);
400 if (*logmsg == NULL)
401 return got_error_from_errno("malloc");
402 (*logmsg)[0] = '\0';
403 len = 0;
405 fp = fopen(logmsg_path, "r");
406 if (fp == NULL) {
407 err = got_error_from_errno("fopen");
408 goto done;
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 if (!content_changed && strcmp(buf, initial_content) != 0)
412 content_changed = 1;
413 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 len = strlcat(*logmsg, buf, st2.st_size);
417 fclose(fp);
419 while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 (*logmsg)[len - 1] = '\0';
421 len--;
424 if (len == 0 || !content_changed)
425 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 "commit message cannot be empty, aborting");
427 done:
428 if (err) {
429 free(*logmsg);
430 *logmsg = NULL;
432 return err;
435 static const struct got_error *
436 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 const char *branch_name)
439 char *initial_content = NULL, *logmsg_path = NULL;
440 const struct got_error *err = NULL;
441 int fd;
443 if (asprintf(&initial_content,
444 "\n# %s to be imported to branch %s\n", path_dir,
445 branch_name) == -1)
446 return got_error_from_errno("asprintf");
448 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
449 if (err)
450 goto done;
452 dprintf(fd, initial_content);
453 close(fd);
455 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
456 done:
457 free(initial_content);
458 free(logmsg_path);
459 return err;
462 static const struct got_error *
463 import_progress(void *arg, const char *path)
465 printf("A %s\n", path);
466 return NULL;
469 static const struct got_error *
470 cmd_import(int argc, char *argv[])
472 const struct got_error *error = NULL;
473 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
474 char *editor = NULL;
475 const char *got_author = getenv("GOT_AUTHOR");
476 const char *branch_name = "master";
477 char *refname = NULL, *id_str = NULL;
478 struct got_repository *repo = NULL;
479 struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 struct got_object_id *new_commit_id = NULL;
481 int ch;
482 struct got_pathlist_head ignores;
483 struct got_pathlist_entry *pe;
485 TAILQ_INIT(&ignores);
487 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
488 switch (ch) {
489 case 'b':
490 branch_name = optarg;
491 break;
492 case 'm':
493 logmsg = strdup(optarg);
494 if (logmsg == NULL) {
495 error = got_error_from_errno("strdup");
496 goto done;
498 break;
499 case 'r':
500 repo_path = realpath(optarg, NULL);
501 if (repo_path == NULL) {
502 error = got_error_from_errno("realpath");
503 goto done;
505 break;
506 case 'I':
507 if (optarg[0] == '\0')
508 break;
509 error = got_pathlist_insert(&pe, &ignores, optarg,
510 NULL);
511 if (error)
512 goto done;
513 break;
514 default:
515 usage_init();
516 /* NOTREACHED */
520 argc -= optind;
521 argv += optind;
523 #ifndef PROFILE
524 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
525 NULL) == -1)
526 err(1, "pledge");
527 #endif
528 if (argc != 1)
529 usage_import();
531 if (got_author == NULL) {
532 /* TODO: Look current user up in password database */
533 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 goto done;
537 if (repo_path == NULL) {
538 repo_path = getcwd(NULL, 0);
539 if (repo_path == NULL)
540 return got_error_from_errno("getcwd");
542 got_path_strip_trailing_slashes(repo_path);
543 error = got_repo_open(&repo, repo_path);
544 if (error)
545 goto done;
547 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 error = got_error_from_errno("asprintf");
549 goto done;
552 error = got_ref_open(&branch_ref, repo, refname, 0);
553 if (error) {
554 if (error->code != GOT_ERR_NOT_REF)
555 goto done;
556 } else {
557 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 "import target branch already exists");
559 goto done;
562 path_dir = realpath(argv[0], NULL);
563 if (path_dir == NULL) {
564 error = got_error_from_errno("realpath");
565 goto done;
567 got_path_strip_trailing_slashes(path_dir);
569 /*
570 * unveil(2) traverses exec(2); if an editor is used we have
571 * to apply unveil after the log message has been written.
572 */
573 if (logmsg == NULL || strlen(logmsg) == 0) {
574 error = get_editor(&editor);
575 if (error)
576 goto done;
577 error = collect_import_msg(&logmsg, editor, path_dir, refname);
578 if (error)
579 goto done;
582 if (unveil(path_dir, "r") != 0)
583 return got_error_from_errno2("unveil", path_dir);
585 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
586 if (error)
587 goto done;
589 error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 got_author, &ignores, repo, import_progress, NULL);
591 if (error)
592 goto done;
594 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
595 if (error)
596 goto done;
598 error = got_ref_write(branch_ref, repo);
599 if (error)
600 goto done;
602 error = got_object_id_str(&id_str, new_commit_id);
603 if (error)
604 goto done;
606 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
607 if (error) {
608 if (error->code != GOT_ERR_NOT_REF)
609 goto done;
611 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
612 branch_ref);
613 if (error)
614 goto done;
616 error = got_ref_write(head_ref, repo);
617 if (error)
618 goto done;
621 printf("Created branch %s with commit %s\n",
622 got_ref_get_name(branch_ref), id_str);
623 done:
624 free(repo_path);
625 free(editor);
626 free(refname);
627 free(new_commit_id);
628 free(id_str);
629 if (branch_ref)
630 got_ref_close(branch_ref);
631 if (head_ref)
632 got_ref_close(head_ref);
633 return error;
636 __dead static void
637 usage_checkout(void)
639 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
641 exit(1);
644 static const struct got_error *
645 checkout_progress(void *arg, unsigned char status, const char *path)
647 char *worktree_path = arg;
649 /* Base commit bump happens silently. */
650 if (status == GOT_STATUS_BUMP_BASE)
651 return NULL;
653 while (path[0] == '/')
654 path++;
656 printf("%c %s/%s\n", status, worktree_path, path);
657 return NULL;
660 static const struct got_error *
661 check_cancelled(void *arg)
663 if (sigint_received || sigpipe_received)
664 return got_error(GOT_ERR_CANCELLED);
665 return NULL;
668 static const struct got_error *
669 check_linear_ancestry(struct got_object_id *commit_id,
670 struct got_object_id *base_commit_id, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 struct got_object_id *yca_id;
675 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 commit_id, base_commit_id, repo);
677 if (err)
678 return err;
680 if (yca_id == NULL)
681 return got_error(GOT_ERR_ANCESTRY);
683 /*
684 * Require a straight line of history between the target commit
685 * and the work tree's base commit.
687 * Non-linear situations such as this require a rebase:
689 * (commit) D F (base_commit)
690 * \ /
691 * C E
692 * \ /
693 * B (yca)
694 * |
695 * A
697 * 'got update' only handles linear cases:
698 * Update forwards in time: A (base/yca) - B - C - D (commit)
699 * Update backwards in time: D (base) - C - B - A (commit/yca)
700 */
701 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 got_object_id_cmp(base_commit_id, yca_id) != 0)
703 return got_error(GOT_ERR_ANCESTRY);
705 free(yca_id);
706 return NULL;
709 static const struct got_error *
710 check_same_branch(struct got_object_id *commit_id,
711 struct got_reference *head_ref, struct got_repository *repo)
713 const struct got_error *err = NULL;
714 struct got_commit_graph *graph = NULL;
715 struct got_object_id *head_commit_id = NULL;
716 int is_same_branch = 0;
718 err = got_ref_resolve(&head_commit_id, repo, head_ref);
719 if (err)
720 goto done;
722 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
723 if (err)
724 goto done;
726 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
727 if (err)
728 goto done;
730 for (;;) {
731 struct got_object_id *id;
732 err = got_commit_graph_iter_next(&id, graph);
733 if (err) {
734 if (err->code == GOT_ERR_ITER_COMPLETED) {
735 err = NULL;
736 break;
737 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
738 break;
739 err = got_commit_graph_fetch_commits(graph, 1,
740 repo);
741 if (err)
742 break;
745 if (id) {
746 if (got_object_id_cmp(id, commit_id) == 0) {
747 is_same_branch = 1;
748 break;
752 done:
753 if (graph)
754 got_commit_graph_close(graph);
755 free(head_commit_id);
756 if (!err && !is_same_branch)
757 err = got_error(GOT_ERR_ANCESTRY);
758 return err;
761 static const struct got_error *
762 resolve_commit_arg(struct got_object_id **commit_id,
763 const char *commit_id_arg, struct got_repository *repo)
765 const struct got_error *err;
766 struct got_reference *ref;
768 err = got_ref_open(&ref, repo, commit_id_arg, 0);
769 if (err == NULL) {
770 err = got_ref_resolve(commit_id, repo, ref);
771 got_ref_close(ref);
772 } else {
773 if (err->code != GOT_ERR_NOT_REF)
774 return err;
775 err = got_repo_match_object_id_prefix(commit_id,
776 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
778 return err;
781 static const struct got_error *
782 cmd_checkout(int argc, char *argv[])
784 const struct got_error *error = NULL;
785 struct got_repository *repo = NULL;
786 struct got_reference *head_ref = NULL;
787 struct got_worktree *worktree = NULL;
788 char *repo_path = NULL;
789 char *worktree_path = NULL;
790 const char *path_prefix = "";
791 const char *branch_name = GOT_REF_HEAD;
792 char *commit_id_str = NULL;
793 int ch, same_path_prefix;
794 struct got_pathlist_head paths;
796 TAILQ_INIT(&paths);
798 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
799 switch (ch) {
800 case 'b':
801 branch_name = optarg;
802 break;
803 case 'c':
804 commit_id_str = strdup(optarg);
805 if (commit_id_str == NULL)
806 return got_error_from_errno("strdup");
807 break;
808 case 'p':
809 path_prefix = optarg;
810 break;
811 default:
812 usage_checkout();
813 /* NOTREACHED */
817 argc -= optind;
818 argv += optind;
820 #ifndef PROFILE
821 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
822 "unveil", NULL) == -1)
823 err(1, "pledge");
824 #endif
825 if (argc == 1) {
826 char *cwd, *base, *dotgit;
827 repo_path = realpath(argv[0], NULL);
828 if (repo_path == NULL)
829 return got_error_from_errno2("realpath", argv[0]);
830 cwd = getcwd(NULL, 0);
831 if (cwd == NULL) {
832 error = got_error_from_errno("getcwd");
833 goto done;
835 if (path_prefix[0]) {
836 base = basename(path_prefix);
837 if (base == NULL) {
838 error = got_error_from_errno2("basename",
839 path_prefix);
840 goto done;
842 } else {
843 base = basename(repo_path);
844 if (base == NULL) {
845 error = got_error_from_errno2("basename",
846 repo_path);
847 goto done;
850 dotgit = strstr(base, ".git");
851 if (dotgit)
852 *dotgit = '\0';
853 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
854 error = got_error_from_errno("asprintf");
855 free(cwd);
856 goto done;
858 free(cwd);
859 } else if (argc == 2) {
860 repo_path = realpath(argv[0], NULL);
861 if (repo_path == NULL) {
862 error = got_error_from_errno2("realpath", argv[0]);
863 goto done;
865 worktree_path = realpath(argv[1], NULL);
866 if (worktree_path == NULL) {
867 if (errno != ENOENT) {
868 error = got_error_from_errno2("realpath",
869 argv[1]);
870 goto done;
872 worktree_path = strdup(argv[1]);
873 if (worktree_path == NULL) {
874 error = got_error_from_errno("strdup");
875 goto done;
878 } else
879 usage_checkout();
881 got_path_strip_trailing_slashes(repo_path);
882 got_path_strip_trailing_slashes(worktree_path);
884 error = got_repo_open(&repo, repo_path);
885 if (error != NULL)
886 goto done;
888 /* Pre-create work tree path for unveil(2) */
889 error = got_path_mkdir(worktree_path);
890 if (error) {
891 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
892 goto done;
893 if (!got_path_dir_is_empty(worktree_path)) {
894 error = got_error_path(worktree_path,
895 GOT_ERR_DIR_NOT_EMPTY);
896 goto done;
900 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
901 if (error)
902 goto done;
904 error = got_ref_open(&head_ref, repo, branch_name, 0);
905 if (error != NULL)
906 goto done;
908 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
909 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
910 goto done;
912 error = got_worktree_open(&worktree, worktree_path);
913 if (error != NULL)
914 goto done;
916 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
917 path_prefix);
918 if (error != NULL)
919 goto done;
920 if (!same_path_prefix) {
921 error = got_error(GOT_ERR_PATH_PREFIX);
922 goto done;
925 if (commit_id_str) {
926 struct got_object_id *commit_id;
927 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
928 if (error)
929 goto done;
930 error = check_linear_ancestry(commit_id,
931 got_worktree_get_base_commit_id(worktree), repo);
932 if (error != NULL) {
933 free(commit_id);
934 goto done;
936 error = check_same_branch(commit_id, head_ref, repo);
937 if (error)
938 goto done;
939 error = got_worktree_set_base_commit_id(worktree, repo,
940 commit_id);
941 free(commit_id);
942 if (error)
943 goto done;
946 error = got_pathlist_append(NULL, &paths, "", NULL);
947 if (error)
948 goto done;
949 error = got_worktree_checkout_files(worktree, &paths, repo,
950 checkout_progress, worktree_path, check_cancelled, NULL);
951 if (error != NULL)
952 goto done;
954 printf("Now shut up and hack\n");
956 done:
957 got_pathlist_free(&paths);
958 free(commit_id_str);
959 free(repo_path);
960 free(worktree_path);
961 return error;
964 __dead static void
965 usage_update(void)
967 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
968 getprogname());
969 exit(1);
972 static const struct got_error *
973 update_progress(void *arg, unsigned char status, const char *path)
975 int *did_something = arg;
977 if (status == GOT_STATUS_EXISTS)
978 return NULL;
980 *did_something = 1;
982 /* Base commit bump happens silently. */
983 if (status == GOT_STATUS_BUMP_BASE)
984 return NULL;
986 while (path[0] == '/')
987 path++;
988 printf("%c %s\n", status, path);
989 return NULL;
992 static const struct got_error *
993 switch_head_ref(struct got_reference *head_ref,
994 struct got_object_id *commit_id, struct got_worktree *worktree,
995 struct got_repository *repo)
997 const struct got_error *err = NULL;
998 char *base_id_str;
999 int ref_has_moved = 0;
1001 /* Trivial case: switching between two different references. */
1002 if (strcmp(got_ref_get_name(head_ref),
1003 got_worktree_get_head_ref_name(worktree)) != 0) {
1004 printf("Switching work tree from %s to %s\n",
1005 got_worktree_get_head_ref_name(worktree),
1006 got_ref_get_name(head_ref));
1007 return got_worktree_set_head_ref(worktree, head_ref);
1010 err = check_linear_ancestry(commit_id,
1011 got_worktree_get_base_commit_id(worktree), repo);
1012 if (err) {
1013 if (err->code != GOT_ERR_ANCESTRY)
1014 return err;
1015 ref_has_moved = 1;
1017 if (!ref_has_moved)
1018 return NULL;
1020 /* Switching to a rebased branch with the same reference name. */
1021 err = got_object_id_str(&base_id_str,
1022 got_worktree_get_base_commit_id(worktree));
1023 if (err)
1024 return err;
1025 printf("Reference %s now points at a different branch\n",
1026 got_worktree_get_head_ref_name(worktree));
1027 printf("Switching work tree from %s to %s\n", base_id_str,
1028 got_worktree_get_head_ref_name(worktree));
1029 return NULL;
1032 static const struct got_error *
1033 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1035 const struct got_error *err;
1036 int in_progress;
1038 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1039 if (err)
1040 return err;
1041 if (in_progress)
1042 return got_error(GOT_ERR_REBASING);
1044 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1045 if (err)
1046 return err;
1047 if (in_progress)
1048 return got_error(GOT_ERR_HISTEDIT_BUSY);
1050 return NULL;
1053 static const struct got_error *
1054 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1055 char *argv[], struct got_worktree *worktree)
1057 const struct got_error *err;
1058 char *path;
1059 int i;
1061 if (argc == 0) {
1062 path = strdup("");
1063 if (path == NULL)
1064 return got_error_from_errno("strdup");
1065 return got_pathlist_append(NULL, paths, path, NULL);
1068 for (i = 0; i < argc; i++) {
1069 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1070 if (err)
1071 break;
1072 err = got_pathlist_append(NULL, paths, path, NULL);
1073 if (err) {
1074 free(path);
1075 break;
1079 return err;
1082 static const struct got_error *
1083 cmd_update(int argc, char *argv[])
1085 const struct got_error *error = NULL;
1086 struct got_repository *repo = NULL;
1087 struct got_worktree *worktree = NULL;
1088 char *worktree_path = NULL;
1089 struct got_object_id *commit_id = NULL;
1090 char *commit_id_str = NULL;
1091 const char *branch_name = NULL;
1092 struct got_reference *head_ref = NULL;
1093 struct got_pathlist_head paths;
1094 struct got_pathlist_entry *pe;
1095 int ch, did_something = 0;
1097 TAILQ_INIT(&paths);
1099 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1100 switch (ch) {
1101 case 'b':
1102 branch_name = optarg;
1103 break;
1104 case 'c':
1105 commit_id_str = strdup(optarg);
1106 if (commit_id_str == NULL)
1107 return got_error_from_errno("strdup");
1108 break;
1109 default:
1110 usage_update();
1111 /* NOTREACHED */
1115 argc -= optind;
1116 argv += optind;
1118 #ifndef PROFILE
1119 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1120 "unveil", NULL) == -1)
1121 err(1, "pledge");
1122 #endif
1123 worktree_path = getcwd(NULL, 0);
1124 if (worktree_path == NULL) {
1125 error = got_error_from_errno("getcwd");
1126 goto done;
1128 error = got_worktree_open(&worktree, worktree_path);
1129 if (error)
1130 goto done;
1132 error = check_rebase_or_histedit_in_progress(worktree);
1133 if (error)
1134 goto done;
1136 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1137 if (error)
1138 goto done;
1140 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1141 if (error != NULL)
1142 goto done;
1144 error = apply_unveil(got_repo_get_path(repo), 0,
1145 got_worktree_get_root_path(worktree));
1146 if (error)
1147 goto done;
1149 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1150 got_worktree_get_head_ref_name(worktree), 0);
1151 if (error != NULL)
1152 goto done;
1153 if (commit_id_str == NULL) {
1154 error = got_ref_resolve(&commit_id, repo, head_ref);
1155 if (error != NULL)
1156 goto done;
1157 error = got_object_id_str(&commit_id_str, commit_id);
1158 if (error != NULL)
1159 goto done;
1160 } else {
1161 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1162 free(commit_id_str);
1163 commit_id_str = NULL;
1164 if (error)
1165 goto done;
1166 error = got_object_id_str(&commit_id_str, commit_id);
1167 if (error)
1168 goto done;
1171 if (branch_name) {
1172 struct got_object_id *head_commit_id;
1173 TAILQ_FOREACH(pe, &paths, entry) {
1174 if (strlen(pe->path) == 0)
1175 continue;
1176 error = got_error_msg(GOT_ERR_BAD_PATH,
1177 "switching between branches requires that "
1178 "the entire work tree gets updated");
1179 goto done;
1181 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1182 if (error)
1183 goto done;
1184 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1185 free(head_commit_id);
1186 if (error != NULL)
1187 goto done;
1188 error = check_same_branch(commit_id, head_ref, repo);
1189 if (error)
1190 goto done;
1191 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1192 if (error)
1193 goto done;
1194 } else {
1195 error = check_linear_ancestry(commit_id,
1196 got_worktree_get_base_commit_id(worktree), repo);
1197 if (error != NULL) {
1198 if (error->code == GOT_ERR_ANCESTRY)
1199 error = got_error(GOT_ERR_BRANCH_MOVED);
1200 goto done;
1202 error = check_same_branch(commit_id, head_ref, repo);
1203 if (error)
1204 goto done;
1207 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1208 commit_id) != 0) {
1209 error = got_worktree_set_base_commit_id(worktree, repo,
1210 commit_id);
1211 if (error)
1212 goto done;
1215 error = got_worktree_checkout_files(worktree, &paths, repo,
1216 update_progress, &did_something, check_cancelled, NULL);
1217 if (error != NULL)
1218 goto done;
1220 if (did_something)
1221 printf("Updated to commit %s\n", commit_id_str);
1222 else
1223 printf("Already up-to-date\n");
1224 done:
1225 free(worktree_path);
1226 TAILQ_FOREACH(pe, &paths, entry)
1227 free((char *)pe->path);
1228 got_pathlist_free(&paths);
1229 free(commit_id);
1230 free(commit_id_str);
1231 return error;
1234 static const struct got_error *
1235 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1236 int diff_context, struct got_repository *repo)
1238 const struct got_error *err = NULL;
1239 struct got_tree_object *tree1 = NULL, *tree2;
1240 struct got_object_qid *qid;
1241 char *id_str1 = NULL, *id_str2;
1242 struct got_diff_blob_output_unidiff_arg arg;
1244 err = got_object_open_as_tree(&tree2, repo,
1245 got_object_commit_get_tree_id(commit));
1246 if (err)
1247 return err;
1249 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1250 if (qid != NULL) {
1251 struct got_commit_object *pcommit;
1253 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1254 if (err)
1255 return err;
1257 err = got_object_open_as_tree(&tree1, repo,
1258 got_object_commit_get_tree_id(pcommit));
1259 got_object_commit_close(pcommit);
1260 if (err)
1261 return err;
1263 err = got_object_id_str(&id_str1, qid->id);
1264 if (err)
1265 return err;
1268 err = got_object_id_str(&id_str2, id);
1269 if (err)
1270 goto done;
1272 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1273 arg.diff_context = diff_context;
1274 arg.outfile = stdout;
1275 err = got_diff_tree(tree1, tree2, "", "", repo,
1276 got_diff_blob_output_unidiff, &arg, 1);
1277 done:
1278 if (tree1)
1279 got_object_tree_close(tree1);
1280 got_object_tree_close(tree2);
1281 free(id_str1);
1282 free(id_str2);
1283 return err;
1286 static char *
1287 get_datestr(time_t *time, char *datebuf)
1289 char *p, *s = ctime_r(time, datebuf);
1290 p = strchr(s, '\n');
1291 if (p)
1292 *p = '\0';
1293 return s;
1296 static const struct got_error *
1297 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1298 struct got_repository *repo, int show_patch, int diff_context,
1299 struct got_reflist_head *refs)
1301 const struct got_error *err = NULL;
1302 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1303 char datebuf[26];
1304 time_t committer_time;
1305 const char *author, *committer;
1306 char *refs_str = NULL;
1307 struct got_reflist_entry *re;
1309 SIMPLEQ_FOREACH(re, refs, entry) {
1310 char *s;
1311 const char *name;
1312 if (got_object_id_cmp(re->id, id) != 0)
1313 continue;
1314 name = got_ref_get_name(re->ref);
1315 if (strcmp(name, GOT_REF_HEAD) == 0)
1316 continue;
1317 if (strncmp(name, "refs/", 5) == 0)
1318 name += 5;
1319 if (strncmp(name, "got/", 4) == 0)
1320 continue;
1321 if (strncmp(name, "heads/", 6) == 0)
1322 name += 6;
1323 if (strncmp(name, "remotes/", 8) == 0)
1324 name += 8;
1325 s = refs_str;
1326 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1327 name) == -1) {
1328 err = got_error_from_errno("asprintf");
1329 free(s);
1330 break;
1332 free(s);
1334 err = got_object_id_str(&id_str, id);
1335 if (err)
1336 return err;
1338 printf("-----------------------------------------------\n");
1339 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1340 refs_str ? refs_str : "", refs_str ? ")" : "");
1341 free(id_str);
1342 id_str = NULL;
1343 free(refs_str);
1344 refs_str = NULL;
1345 printf("from: %s\n", got_object_commit_get_author(commit));
1346 committer_time = got_object_commit_get_committer_time(commit);
1347 datestr = get_datestr(&committer_time, datebuf);
1348 printf("date: %s UTC\n", datestr);
1349 author = got_object_commit_get_author(commit);
1350 committer = got_object_commit_get_committer(commit);
1351 if (strcmp(author, committer) != 0)
1352 printf("via: %s\n", committer);
1353 if (got_object_commit_get_nparents(commit) > 1) {
1354 const struct got_object_id_queue *parent_ids;
1355 struct got_object_qid *qid;
1356 int n = 1;
1357 parent_ids = got_object_commit_get_parent_ids(commit);
1358 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1359 err = got_object_id_str(&id_str, qid->id);
1360 if (err)
1361 return err;
1362 printf("parent %d: %s\n", n++, id_str);
1363 free(id_str);
1367 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1368 if (logmsg0 == NULL)
1369 return got_error_from_errno("strdup");
1371 logmsg = logmsg0;
1372 do {
1373 line = strsep(&logmsg, "\n");
1374 if (line)
1375 printf(" %s\n", line);
1376 } while (line);
1377 free(logmsg0);
1379 if (show_patch) {
1380 err = print_patch(commit, id, diff_context, repo);
1381 if (err == 0)
1382 printf("\n");
1385 if (fflush(stdout) != 0 && err == NULL)
1386 err = got_error_from_errno("fflush");
1387 return err;
1390 static const struct got_error *
1391 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1392 char *path, int show_patch, int diff_context, int limit,
1393 int first_parent_traversal, struct got_reflist_head *refs)
1395 const struct got_error *err;
1396 struct got_commit_graph *graph;
1398 err = got_commit_graph_open(&graph, root_id, path,
1399 first_parent_traversal, repo);
1400 if (err)
1401 return err;
1402 err = got_commit_graph_iter_start(graph, root_id, repo);
1403 if (err)
1404 goto done;
1405 for (;;) {
1406 struct got_commit_object *commit;
1407 struct got_object_id *id;
1409 if (sigint_received || sigpipe_received)
1410 break;
1412 err = got_commit_graph_iter_next(&id, graph);
1413 if (err) {
1414 if (err->code == GOT_ERR_ITER_COMPLETED) {
1415 err = NULL;
1416 break;
1418 if (err->code != GOT_ERR_ITER_NEED_MORE)
1419 break;
1420 err = got_commit_graph_fetch_commits(graph, 1, repo);
1421 if (err)
1422 break;
1423 else
1424 continue;
1426 if (id == NULL)
1427 break;
1429 err = got_object_open_as_commit(&commit, repo, id);
1430 if (err)
1431 break;
1432 err = print_commit(commit, id, repo, show_patch, diff_context,
1433 refs);
1434 got_object_commit_close(commit);
1435 if (err || (limit && --limit == 0))
1436 break;
1438 done:
1439 got_commit_graph_close(graph);
1440 return err;
1443 __dead static void
1444 usage_log(void)
1446 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1447 "[-r repository-path] [path]\n", getprogname());
1448 exit(1);
1451 static const struct got_error *
1452 cmd_log(int argc, char *argv[])
1454 const struct got_error *error;
1455 struct got_repository *repo = NULL;
1456 struct got_worktree *worktree = NULL;
1457 struct got_commit_object *commit = NULL;
1458 struct got_object_id *id = NULL;
1459 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1460 char *start_commit = NULL;
1461 int diff_context = 3, ch;
1462 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1463 const char *errstr;
1464 struct got_reflist_head refs;
1466 SIMPLEQ_INIT(&refs);
1468 #ifndef PROFILE
1469 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1470 NULL)
1471 == -1)
1472 err(1, "pledge");
1473 #endif
1475 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1476 switch (ch) {
1477 case 'p':
1478 show_patch = 1;
1479 break;
1480 case 'c':
1481 start_commit = optarg;
1482 break;
1483 case 'C':
1484 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1485 &errstr);
1486 if (errstr != NULL)
1487 err(1, "-C option %s", errstr);
1488 break;
1489 case 'l':
1490 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1491 if (errstr != NULL)
1492 err(1, "-l option %s", errstr);
1493 break;
1494 case 'f':
1495 first_parent_traversal = 1;
1496 break;
1497 case 'r':
1498 repo_path = realpath(optarg, NULL);
1499 if (repo_path == NULL)
1500 err(1, "-r option");
1501 got_path_strip_trailing_slashes(repo_path);
1502 break;
1503 default:
1504 usage_log();
1505 /* NOTREACHED */
1509 argc -= optind;
1510 argv += optind;
1512 cwd = getcwd(NULL, 0);
1513 if (cwd == NULL) {
1514 error = got_error_from_errno("getcwd");
1515 goto done;
1518 error = got_worktree_open(&worktree, cwd);
1519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1520 goto done;
1521 error = NULL;
1523 if (argc == 0) {
1524 path = strdup("");
1525 if (path == NULL) {
1526 error = got_error_from_errno("strdup");
1527 goto done;
1529 } else if (argc == 1) {
1530 if (worktree) {
1531 error = got_worktree_resolve_path(&path, worktree,
1532 argv[0]);
1533 if (error)
1534 goto done;
1535 } else {
1536 path = strdup(argv[0]);
1537 if (path == NULL) {
1538 error = got_error_from_errno("strdup");
1539 goto done;
1542 } else
1543 usage_log();
1545 if (repo_path == NULL) {
1546 repo_path = worktree ?
1547 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1549 if (repo_path == NULL) {
1550 error = got_error_from_errno("strdup");
1551 goto done;
1554 error = got_repo_open(&repo, repo_path);
1555 if (error != NULL)
1556 goto done;
1558 error = apply_unveil(got_repo_get_path(repo), 1,
1559 worktree ? got_worktree_get_root_path(worktree) : NULL);
1560 if (error)
1561 goto done;
1563 if (start_commit == NULL) {
1564 struct got_reference *head_ref;
1565 error = got_ref_open(&head_ref, repo,
1566 worktree ? got_worktree_get_head_ref_name(worktree)
1567 : GOT_REF_HEAD, 0);
1568 if (error != NULL)
1569 return error;
1570 error = got_ref_resolve(&id, repo, head_ref);
1571 got_ref_close(head_ref);
1572 if (error != NULL)
1573 return error;
1574 error = got_object_open_as_commit(&commit, repo, id);
1575 } else {
1576 struct got_reference *ref;
1577 error = got_ref_open(&ref, repo, start_commit, 0);
1578 if (error == NULL) {
1579 int obj_type;
1580 error = got_ref_resolve(&id, repo, ref);
1581 got_ref_close(ref);
1582 if (error != NULL)
1583 goto done;
1584 error = got_object_get_type(&obj_type, repo, id);
1585 if (error != NULL)
1586 goto done;
1587 if (obj_type == GOT_OBJ_TYPE_TAG) {
1588 struct got_tag_object *tag;
1589 error = got_object_open_as_tag(&tag, repo, id);
1590 if (error != NULL)
1591 goto done;
1592 if (got_object_tag_get_object_type(tag) !=
1593 GOT_OBJ_TYPE_COMMIT) {
1594 got_object_tag_close(tag);
1595 error = got_error(GOT_ERR_OBJ_TYPE);
1596 goto done;
1598 free(id);
1599 id = got_object_id_dup(
1600 got_object_tag_get_object_id(tag));
1601 if (id == NULL)
1602 error = got_error_from_errno(
1603 "got_object_id_dup");
1604 got_object_tag_close(tag);
1605 if (error)
1606 goto done;
1607 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1608 error = got_error(GOT_ERR_OBJ_TYPE);
1609 goto done;
1611 error = got_object_open_as_commit(&commit, repo, id);
1612 if (error != NULL)
1613 goto done;
1615 if (commit == NULL) {
1616 error = got_repo_match_object_id_prefix(&id,
1617 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1618 if (error != NULL)
1619 return error;
1622 if (error != NULL)
1623 goto done;
1625 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1626 if (error != NULL)
1627 goto done;
1628 if (in_repo_path) {
1629 free(path);
1630 path = in_repo_path;
1633 error = got_ref_list(&refs, repo);
1634 if (error)
1635 goto done;
1637 error = print_commits(id, repo, path, show_patch,
1638 diff_context, limit, first_parent_traversal, &refs);
1639 done:
1640 free(path);
1641 free(repo_path);
1642 free(cwd);
1643 free(id);
1644 if (worktree)
1645 got_worktree_close(worktree);
1646 if (repo) {
1647 const struct got_error *repo_error;
1648 repo_error = got_repo_close(repo);
1649 if (error == NULL)
1650 error = repo_error;
1652 got_ref_list_free(&refs);
1653 return error;
1656 __dead static void
1657 usage_diff(void)
1659 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1660 "[object1 object2 | path]\n", getprogname());
1661 exit(1);
1664 struct print_diff_arg {
1665 struct got_repository *repo;
1666 struct got_worktree *worktree;
1667 int diff_context;
1668 const char *id_str;
1669 int header_shown;
1672 static const struct got_error *
1673 print_diff(void *arg, unsigned char status, const char *path,
1674 struct got_object_id *blob_id, struct got_object_id *commit_id)
1676 struct print_diff_arg *a = arg;
1677 const struct got_error *err = NULL;
1678 struct got_blob_object *blob1 = NULL;
1679 FILE *f2 = NULL;
1680 char *abspath = NULL;
1681 struct stat sb;
1683 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1684 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1685 return NULL;
1687 if (!a->header_shown) {
1688 printf("diff %s %s\n", a->id_str,
1689 got_worktree_get_root_path(a->worktree));
1690 a->header_shown = 1;
1693 if (status != GOT_STATUS_ADD) {
1694 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1695 if (err)
1696 goto done;
1700 if (status != GOT_STATUS_DELETE) {
1701 if (asprintf(&abspath, "%s/%s",
1702 got_worktree_get_root_path(a->worktree), path) == -1) {
1703 err = got_error_from_errno("asprintf");
1704 goto done;
1707 f2 = fopen(abspath, "r");
1708 if (f2 == NULL) {
1709 err = got_error_from_errno2("fopen", abspath);
1710 goto done;
1712 if (lstat(abspath, &sb) == -1) {
1713 err = got_error_from_errno2("lstat", abspath);
1714 goto done;
1716 } else
1717 sb.st_size = 0;
1719 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1720 stdout);
1721 done:
1722 if (blob1)
1723 got_object_blob_close(blob1);
1724 if (f2 && fclose(f2) != 0 && err == NULL)
1725 err = got_error_from_errno("fclose");
1726 free(abspath);
1727 return err;
1730 static const struct got_error *
1731 cmd_diff(int argc, char *argv[])
1733 const struct got_error *error;
1734 struct got_repository *repo = NULL;
1735 struct got_worktree *worktree = NULL;
1736 char *cwd = NULL, *repo_path = NULL;
1737 struct got_object_id *id1 = NULL, *id2 = NULL;
1738 const char *id_str1 = NULL, *id_str2 = NULL;
1739 char *label1 = NULL, *label2 = NULL;
1740 int type1, type2;
1741 int diff_context = 3, ch;
1742 const char *errstr;
1743 char *path = NULL;
1745 #ifndef PROFILE
1746 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1747 NULL) == -1)
1748 err(1, "pledge");
1749 #endif
1751 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1752 switch (ch) {
1753 case 'C':
1754 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1755 if (errstr != NULL)
1756 err(1, "-C option %s", errstr);
1757 break;
1758 case 'r':
1759 repo_path = realpath(optarg, NULL);
1760 if (repo_path == NULL)
1761 err(1, "-r option");
1762 got_path_strip_trailing_slashes(repo_path);
1763 break;
1764 default:
1765 usage_diff();
1766 /* NOTREACHED */
1770 argc -= optind;
1771 argv += optind;
1773 cwd = getcwd(NULL, 0);
1774 if (cwd == NULL) {
1775 error = got_error_from_errno("getcwd");
1776 goto done;
1778 error = got_worktree_open(&worktree, cwd);
1779 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1780 goto done;
1781 if (argc <= 1) {
1782 if (worktree == NULL) {
1783 error = got_error(GOT_ERR_NOT_WORKTREE);
1784 goto done;
1786 if (repo_path)
1787 errx(1,
1788 "-r option can't be used when diffing a work tree");
1789 repo_path = strdup(got_worktree_get_repo_path(worktree));
1790 if (repo_path == NULL) {
1791 error = got_error_from_errno("strdup");
1792 goto done;
1794 if (argc == 1) {
1795 error = got_worktree_resolve_path(&path, worktree,
1796 argv[0]);
1797 if (error)
1798 goto done;
1799 } else {
1800 path = strdup("");
1801 if (path == NULL) {
1802 error = got_error_from_errno("strdup");
1803 goto done;
1806 } else if (argc == 2) {
1807 id_str1 = argv[0];
1808 id_str2 = argv[1];
1809 if (worktree && repo_path == NULL) {
1810 repo_path =
1811 strdup(got_worktree_get_repo_path(worktree));
1812 if (repo_path == NULL) {
1813 error = got_error_from_errno("strdup");
1814 goto done;
1817 } else
1818 usage_diff();
1820 if (repo_path == NULL) {
1821 repo_path = getcwd(NULL, 0);
1822 if (repo_path == NULL)
1823 return got_error_from_errno("getcwd");
1826 error = got_repo_open(&repo, repo_path);
1827 free(repo_path);
1828 if (error != NULL)
1829 goto done;
1831 error = apply_unveil(got_repo_get_path(repo), 1,
1832 worktree ? got_worktree_get_root_path(worktree) : NULL);
1833 if (error)
1834 goto done;
1836 if (argc <= 1) {
1837 struct print_diff_arg arg;
1838 struct got_pathlist_head paths;
1839 char *id_str;
1841 TAILQ_INIT(&paths);
1843 error = got_object_id_str(&id_str,
1844 got_worktree_get_base_commit_id(worktree));
1845 if (error)
1846 goto done;
1847 arg.repo = repo;
1848 arg.worktree = worktree;
1849 arg.diff_context = diff_context;
1850 arg.id_str = id_str;
1851 arg.header_shown = 0;
1853 error = got_pathlist_append(NULL, &paths, path, NULL);
1854 if (error)
1855 goto done;
1857 error = got_worktree_status(worktree, &paths, repo, print_diff,
1858 &arg, check_cancelled, NULL);
1859 free(id_str);
1860 got_pathlist_free(&paths);
1861 goto done;
1864 error = got_repo_match_object_id_prefix(&id1, id_str1,
1865 GOT_OBJ_TYPE_ANY, repo);
1866 if (error) {
1867 struct got_reference *ref;
1868 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1869 goto done;
1870 error = got_ref_open(&ref, repo, id_str1, 0);
1871 if (error != NULL)
1872 goto done;
1873 label1 = strdup(got_ref_get_name(ref));
1874 if (label1 == NULL) {
1875 error = got_error_from_errno("strdup");
1876 goto done;
1878 error = got_ref_resolve(&id1, repo, ref);
1879 got_ref_close(ref);
1880 if (error != NULL)
1881 goto done;
1882 } else {
1883 error = got_object_id_str(&label1, id1);
1884 if (label1 == NULL) {
1885 error = got_error_from_errno("strdup");
1886 goto done;
1890 error = got_repo_match_object_id_prefix(&id2, id_str2,
1891 GOT_OBJ_TYPE_ANY, repo);
1892 if (error) {
1893 struct got_reference *ref;
1894 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1895 goto done;
1896 error = got_ref_open(&ref, repo, id_str2, 0);
1897 if (error != NULL)
1898 goto done;
1899 label2 = strdup(got_ref_get_name(ref));
1900 if (label2 == NULL) {
1901 error = got_error_from_errno("strdup");
1902 goto done;
1904 error = got_ref_resolve(&id2, repo, ref);
1905 got_ref_close(ref);
1906 if (error != NULL)
1907 goto done;
1908 } else {
1909 error = got_object_id_str(&label2, id2);
1910 if (label2 == NULL) {
1911 error = got_error_from_errno("strdup");
1912 goto done;
1916 error = got_object_get_type(&type1, repo, id1);
1917 if (error)
1918 goto done;
1920 error = got_object_get_type(&type2, repo, id2);
1921 if (error)
1922 goto done;
1924 if (type1 != type2) {
1925 error = got_error(GOT_ERR_OBJ_TYPE);
1926 goto done;
1929 switch (type1) {
1930 case GOT_OBJ_TYPE_BLOB:
1931 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1932 diff_context, repo, stdout);
1933 break;
1934 case GOT_OBJ_TYPE_TREE:
1935 error = got_diff_objects_as_trees(id1, id2, "", "",
1936 diff_context, repo, stdout);
1937 break;
1938 case GOT_OBJ_TYPE_COMMIT:
1939 printf("diff %s %s\n", label1, label2);
1940 error = got_diff_objects_as_commits(id1, id2, diff_context,
1941 repo, stdout);
1942 break;
1943 default:
1944 error = got_error(GOT_ERR_OBJ_TYPE);
1947 done:
1948 free(label1);
1949 free(label2);
1950 free(id1);
1951 free(id2);
1952 free(path);
1953 if (worktree)
1954 got_worktree_close(worktree);
1955 if (repo) {
1956 const struct got_error *repo_error;
1957 repo_error = got_repo_close(repo);
1958 if (error == NULL)
1959 error = repo_error;
1961 return error;
1964 __dead static void
1965 usage_blame(void)
1967 fprintf(stderr,
1968 "usage: %s blame [-c commit] [-r repository-path] path\n",
1969 getprogname());
1970 exit(1);
1973 static const struct got_error *
1974 cmd_blame(int argc, char *argv[])
1976 const struct got_error *error;
1977 struct got_repository *repo = NULL;
1978 struct got_worktree *worktree = NULL;
1979 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1980 struct got_object_id *commit_id = NULL;
1981 char *commit_id_str = NULL;
1982 int ch;
1984 #ifndef PROFILE
1985 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1986 NULL) == -1)
1987 err(1, "pledge");
1988 #endif
1990 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1991 switch (ch) {
1992 case 'c':
1993 commit_id_str = optarg;
1994 break;
1995 case 'r':
1996 repo_path = realpath(optarg, NULL);
1997 if (repo_path == NULL)
1998 err(1, "-r option");
1999 got_path_strip_trailing_slashes(repo_path);
2000 break;
2001 default:
2002 usage_blame();
2003 /* NOTREACHED */
2007 argc -= optind;
2008 argv += optind;
2010 if (argc == 1)
2011 path = argv[0];
2012 else
2013 usage_blame();
2015 cwd = getcwd(NULL, 0);
2016 if (cwd == NULL) {
2017 error = got_error_from_errno("getcwd");
2018 goto done;
2020 if (repo_path == NULL) {
2021 error = got_worktree_open(&worktree, cwd);
2022 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2023 goto done;
2024 else
2025 error = NULL;
2026 if (worktree) {
2027 repo_path =
2028 strdup(got_worktree_get_repo_path(worktree));
2029 if (repo_path == NULL)
2030 error = got_error_from_errno("strdup");
2031 if (error)
2032 goto done;
2033 } else {
2034 repo_path = strdup(cwd);
2035 if (repo_path == NULL) {
2036 error = got_error_from_errno("strdup");
2037 goto done;
2042 error = got_repo_open(&repo, repo_path);
2043 if (error != NULL)
2044 goto done;
2046 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2047 if (error)
2048 goto done;
2050 if (worktree) {
2051 const char *prefix = got_worktree_get_path_prefix(worktree);
2052 char *p, *worktree_subdir = cwd +
2053 strlen(got_worktree_get_root_path(worktree));
2054 if (asprintf(&p, "%s%s%s%s%s",
2055 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2056 worktree_subdir, worktree_subdir[0] ? "/" : "",
2057 path) == -1) {
2058 error = got_error_from_errno("asprintf");
2059 goto done;
2061 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2062 free(p);
2063 } else {
2064 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2066 if (error)
2067 goto done;
2069 if (commit_id_str == NULL) {
2070 struct got_reference *head_ref;
2071 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2072 if (error != NULL)
2073 goto done;
2074 error = got_ref_resolve(&commit_id, repo, head_ref);
2075 got_ref_close(head_ref);
2076 if (error != NULL)
2077 goto done;
2078 } else {
2079 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2080 if (error)
2081 goto done;
2084 error = got_blame(in_repo_path, commit_id, repo, stdout);
2085 done:
2086 free(in_repo_path);
2087 free(repo_path);
2088 free(cwd);
2089 free(commit_id);
2090 if (worktree)
2091 got_worktree_close(worktree);
2092 if (repo) {
2093 const struct got_error *repo_error;
2094 repo_error = got_repo_close(repo);
2095 if (error == NULL)
2096 error = repo_error;
2098 return error;
2101 __dead static void
2102 usage_tree(void)
2104 fprintf(stderr,
2105 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2106 getprogname());
2107 exit(1);
2110 static void
2111 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2112 const char *root_path)
2114 int is_root_path = (strcmp(path, root_path) == 0);
2116 path += strlen(root_path);
2117 while (path[0] == '/')
2118 path++;
2120 printf("%s%s%s%s%s\n", id ? id : "", path,
2121 is_root_path ? "" : "/", te->name,
2122 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2125 static const struct got_error *
2126 print_tree(const char *path, struct got_object_id *commit_id,
2127 int show_ids, int recurse, const char *root_path,
2128 struct got_repository *repo)
2130 const struct got_error *err = NULL;
2131 struct got_object_id *tree_id = NULL;
2132 struct got_tree_object *tree = NULL;
2133 const struct got_tree_entries *entries;
2134 struct got_tree_entry *te;
2136 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2137 if (err)
2138 goto done;
2140 err = got_object_open_as_tree(&tree, repo, tree_id);
2141 if (err)
2142 goto done;
2143 entries = got_object_tree_get_entries(tree);
2144 te = SIMPLEQ_FIRST(&entries->head);
2145 while (te) {
2146 char *id = NULL;
2148 if (sigint_received || sigpipe_received)
2149 break;
2151 if (show_ids) {
2152 char *id_str;
2153 err = got_object_id_str(&id_str, te->id);
2154 if (err)
2155 goto done;
2156 if (asprintf(&id, "%s ", id_str) == -1) {
2157 err = got_error_from_errno("asprintf");
2158 free(id_str);
2159 goto done;
2161 free(id_str);
2163 print_entry(te, id, path, root_path);
2164 free(id);
2166 if (recurse && S_ISDIR(te->mode)) {
2167 char *child_path;
2168 if (asprintf(&child_path, "%s%s%s", path,
2169 path[0] == '/' && path[1] == '\0' ? "" : "/",
2170 te->name) == -1) {
2171 err = got_error_from_errno("asprintf");
2172 goto done;
2174 err = print_tree(child_path, commit_id, show_ids, 1,
2175 root_path, repo);
2176 free(child_path);
2177 if (err)
2178 goto done;
2181 te = SIMPLEQ_NEXT(te, entry);
2183 done:
2184 if (tree)
2185 got_object_tree_close(tree);
2186 free(tree_id);
2187 return err;
2190 static const struct got_error *
2191 cmd_tree(int argc, char *argv[])
2193 const struct got_error *error;
2194 struct got_repository *repo = NULL;
2195 struct got_worktree *worktree = NULL;
2196 const char *path;
2197 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2198 struct got_object_id *commit_id = NULL;
2199 char *commit_id_str = NULL;
2200 int show_ids = 0, recurse = 0;
2201 int ch;
2203 #ifndef PROFILE
2204 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2205 NULL) == -1)
2206 err(1, "pledge");
2207 #endif
2209 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2210 switch (ch) {
2211 case 'c':
2212 commit_id_str = optarg;
2213 break;
2214 case 'r':
2215 repo_path = realpath(optarg, NULL);
2216 if (repo_path == NULL)
2217 err(1, "-r option");
2218 got_path_strip_trailing_slashes(repo_path);
2219 break;
2220 case 'i':
2221 show_ids = 1;
2222 break;
2223 case 'R':
2224 recurse = 1;
2225 break;
2226 default:
2227 usage_tree();
2228 /* NOTREACHED */
2232 argc -= optind;
2233 argv += optind;
2235 if (argc == 1)
2236 path = argv[0];
2237 else if (argc > 1)
2238 usage_tree();
2239 else
2240 path = NULL;
2242 cwd = getcwd(NULL, 0);
2243 if (cwd == NULL) {
2244 error = got_error_from_errno("getcwd");
2245 goto done;
2247 if (repo_path == NULL) {
2248 error = got_worktree_open(&worktree, cwd);
2249 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2250 goto done;
2251 else
2252 error = NULL;
2253 if (worktree) {
2254 repo_path =
2255 strdup(got_worktree_get_repo_path(worktree));
2256 if (repo_path == NULL)
2257 error = got_error_from_errno("strdup");
2258 if (error)
2259 goto done;
2260 } else {
2261 repo_path = strdup(cwd);
2262 if (repo_path == NULL) {
2263 error = got_error_from_errno("strdup");
2264 goto done;
2269 error = got_repo_open(&repo, repo_path);
2270 if (error != NULL)
2271 goto done;
2273 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2274 if (error)
2275 goto done;
2277 if (path == NULL) {
2278 if (worktree) {
2279 char *p, *worktree_subdir = cwd +
2280 strlen(got_worktree_get_root_path(worktree));
2281 if (asprintf(&p, "%s/%s",
2282 got_worktree_get_path_prefix(worktree),
2283 worktree_subdir) == -1) {
2284 error = got_error_from_errno("asprintf");
2285 goto done;
2287 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2288 free(p);
2289 if (error)
2290 goto done;
2291 } else
2292 path = "/";
2294 if (in_repo_path == NULL) {
2295 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2296 if (error != NULL)
2297 goto done;
2300 if (commit_id_str == NULL) {
2301 struct got_reference *head_ref;
2302 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2303 if (error != NULL)
2304 goto done;
2305 error = got_ref_resolve(&commit_id, repo, head_ref);
2306 got_ref_close(head_ref);
2307 if (error != NULL)
2308 goto done;
2309 } else {
2310 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2311 if (error)
2312 goto done;
2315 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2316 in_repo_path, repo);
2317 done:
2318 free(in_repo_path);
2319 free(repo_path);
2320 free(cwd);
2321 free(commit_id);
2322 if (worktree)
2323 got_worktree_close(worktree);
2324 if (repo) {
2325 const struct got_error *repo_error;
2326 repo_error = got_repo_close(repo);
2327 if (error == NULL)
2328 error = repo_error;
2330 return error;
2333 __dead static void
2334 usage_status(void)
2336 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2337 exit(1);
2340 static const struct got_error *
2341 print_status(void *arg, unsigned char status, const char *path,
2342 struct got_object_id *blob_id, struct got_object_id *commit_id)
2344 printf("%c %s\n", status, path);
2345 return NULL;
2348 static const struct got_error *
2349 cmd_status(int argc, char *argv[])
2351 const struct got_error *error = NULL;
2352 struct got_repository *repo = NULL;
2353 struct got_worktree *worktree = NULL;
2354 char *cwd = NULL;
2355 struct got_pathlist_head paths;
2356 struct got_pathlist_entry *pe;
2357 int ch;
2359 TAILQ_INIT(&paths);
2361 while ((ch = getopt(argc, argv, "")) != -1) {
2362 switch (ch) {
2363 default:
2364 usage_status();
2365 /* NOTREACHED */
2369 argc -= optind;
2370 argv += optind;
2372 #ifndef PROFILE
2373 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2374 NULL) == -1)
2375 err(1, "pledge");
2376 #endif
2377 cwd = getcwd(NULL, 0);
2378 if (cwd == NULL) {
2379 error = got_error_from_errno("getcwd");
2380 goto done;
2383 error = got_worktree_open(&worktree, cwd);
2384 if (error != NULL)
2385 goto done;
2387 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2388 if (error)
2389 goto done;
2391 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2392 if (error != NULL)
2393 goto done;
2395 error = apply_unveil(got_repo_get_path(repo), 1,
2396 got_worktree_get_root_path(worktree));
2397 if (error)
2398 goto done;
2400 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2401 check_cancelled, NULL);
2402 done:
2403 TAILQ_FOREACH(pe, &paths, entry)
2404 free((char *)pe->path);
2405 got_pathlist_free(&paths);
2406 free(cwd);
2407 return error;
2410 __dead static void
2411 usage_ref(void)
2413 fprintf(stderr,
2414 "usage: %s ref [-r repository] -l | -d name | name target\n",
2415 getprogname());
2416 exit(1);
2419 static const struct got_error *
2420 list_refs(struct got_repository *repo)
2422 static const struct got_error *err = NULL;
2423 struct got_reflist_head refs;
2424 struct got_reflist_entry *re;
2426 SIMPLEQ_INIT(&refs);
2427 err = got_ref_list(&refs, repo);
2428 if (err)
2429 return err;
2431 SIMPLEQ_FOREACH(re, &refs, entry) {
2432 char *refstr;
2433 refstr = got_ref_to_str(re->ref);
2434 if (refstr == NULL)
2435 return got_error_from_errno("got_ref_to_str");
2436 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2437 free(refstr);
2440 got_ref_list_free(&refs);
2441 return NULL;
2444 static const struct got_error *
2445 delete_ref(struct got_repository *repo, const char *refname)
2447 const struct got_error *err = NULL;
2448 struct got_reference *ref;
2450 err = got_ref_open(&ref, repo, refname, 0);
2451 if (err)
2452 return err;
2454 err = got_ref_delete(ref, repo);
2455 got_ref_close(ref);
2456 return err;
2459 static const struct got_error *
2460 add_ref(struct got_repository *repo, const char *refname, const char *target)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id;
2464 struct got_reference *ref = NULL;
2467 * Don't let the user create a reference named '-'.
2468 * While technically a valid reference name, this case is usually
2469 * an unintended typo.
2471 if (refname[0] == '-' && refname[1] == '\0')
2472 return got_error(GOT_ERR_BAD_REF_NAME);
2474 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2475 repo);
2476 if (err) {
2477 struct got_reference *target_ref;
2479 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2480 return err;
2481 err = got_ref_open(&target_ref, repo, target, 0);
2482 if (err)
2483 return err;
2484 err = got_ref_resolve(&id, repo, target_ref);
2485 got_ref_close(target_ref);
2486 if (err)
2487 return err;
2490 err = got_ref_alloc(&ref, refname, id);
2491 if (err)
2492 goto done;
2494 err = got_ref_write(ref, repo);
2495 done:
2496 if (ref)
2497 got_ref_close(ref);
2498 free(id);
2499 return err;
2502 static const struct got_error *
2503 cmd_ref(int argc, char *argv[])
2505 const struct got_error *error = NULL;
2506 struct got_repository *repo = NULL;
2507 struct got_worktree *worktree = NULL;
2508 char *cwd = NULL, *repo_path = NULL;
2509 int ch, do_list = 0;
2510 const char *delref = NULL;
2512 /* TODO: Add -s option for adding symbolic references. */
2513 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2514 switch (ch) {
2515 case 'd':
2516 delref = optarg;
2517 break;
2518 case 'r':
2519 repo_path = realpath(optarg, NULL);
2520 if (repo_path == NULL)
2521 err(1, "-r option");
2522 got_path_strip_trailing_slashes(repo_path);
2523 break;
2524 case 'l':
2525 do_list = 1;
2526 break;
2527 default:
2528 usage_ref();
2529 /* NOTREACHED */
2533 if (do_list && delref)
2534 errx(1, "-l and -d options are mutually exclusive\n");
2536 argc -= optind;
2537 argv += optind;
2539 if (do_list || delref) {
2540 if (argc > 0)
2541 usage_ref();
2542 } else if (argc != 2)
2543 usage_ref();
2545 #ifndef PROFILE
2546 if (do_list) {
2547 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2548 NULL) == -1)
2549 err(1, "pledge");
2550 } else {
2551 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2552 "sendfd unveil", NULL) == -1)
2553 err(1, "pledge");
2555 #endif
2556 cwd = getcwd(NULL, 0);
2557 if (cwd == NULL) {
2558 error = got_error_from_errno("getcwd");
2559 goto done;
2562 if (repo_path == NULL) {
2563 error = got_worktree_open(&worktree, cwd);
2564 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2565 goto done;
2566 else
2567 error = NULL;
2568 if (worktree) {
2569 repo_path =
2570 strdup(got_worktree_get_repo_path(worktree));
2571 if (repo_path == NULL)
2572 error = got_error_from_errno("strdup");
2573 if (error)
2574 goto done;
2575 } else {
2576 repo_path = strdup(cwd);
2577 if (repo_path == NULL) {
2578 error = got_error_from_errno("strdup");
2579 goto done;
2584 error = got_repo_open(&repo, repo_path);
2585 if (error != NULL)
2586 goto done;
2588 error = apply_unveil(got_repo_get_path(repo), do_list,
2589 worktree ? got_worktree_get_root_path(worktree) : NULL);
2590 if (error)
2591 goto done;
2593 if (do_list)
2594 error = list_refs(repo);
2595 else if (delref)
2596 error = delete_ref(repo, delref);
2597 else
2598 error = add_ref(repo, argv[0], argv[1]);
2599 done:
2600 if (repo)
2601 got_repo_close(repo);
2602 if (worktree)
2603 got_worktree_close(worktree);
2604 free(cwd);
2605 free(repo_path);
2606 return error;
2609 __dead static void
2610 usage_branch(void)
2612 fprintf(stderr,
2613 "usage: %s branch [-r repository] -l | -d name | "
2614 "name [base-branch]\n", getprogname());
2615 exit(1);
2618 static const struct got_error *
2619 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2621 static const struct got_error *err = NULL;
2622 struct got_reflist_head refs;
2623 struct got_reflist_entry *re;
2625 SIMPLEQ_INIT(&refs);
2627 err = got_ref_list(&refs, repo);
2628 if (err)
2629 return err;
2631 SIMPLEQ_FOREACH(re, &refs, entry) {
2632 const char *refname, *marker = " ";
2633 char *refstr;
2634 refname = got_ref_get_name(re->ref);
2635 if (strncmp(refname, "refs/heads/", 11) != 0)
2636 continue;
2637 if (worktree && strcmp(refname,
2638 got_worktree_get_head_ref_name(worktree)) == 0) {
2639 struct got_object_id *id = NULL;
2640 err = got_ref_resolve(&id, repo, re->ref);
2641 if (err)
2642 return err;
2643 if (got_object_id_cmp(id,
2644 got_worktree_get_base_commit_id(worktree)) == 0)
2645 marker = "* ";
2646 else
2647 marker = "~ ";
2648 free(id);
2650 refname += 11;
2651 refstr = got_ref_to_str(re->ref);
2652 if (refstr == NULL)
2653 return got_error_from_errno("got_ref_to_str");
2654 printf("%s%s: %s\n", marker, refname, refstr);
2655 free(refstr);
2658 got_ref_list_free(&refs);
2659 return NULL;
2662 static const struct got_error *
2663 delete_branch(struct got_repository *repo, const char *branch_name)
2665 const struct got_error *err = NULL;
2666 struct got_reference *ref;
2667 char *refname;
2669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2670 return got_error_from_errno("asprintf");
2672 err = got_ref_open(&ref, repo, refname, 0);
2673 if (err)
2674 goto done;
2676 err = got_ref_delete(ref, repo);
2677 got_ref_close(ref);
2678 done:
2679 free(refname);
2680 return err;
2683 static const struct got_error *
2684 add_branch(struct got_repository *repo, const char *branch_name,
2685 const char *base_branch)
2687 const struct got_error *err = NULL;
2688 struct got_object_id *id = NULL;
2689 struct got_reference *ref = NULL;
2690 char *base_refname = NULL, *refname = NULL;
2691 struct got_reference *base_ref;
2694 * Don't let the user create a branch named '-'.
2695 * While technically a valid reference name, this case is usually
2696 * an unintended typo.
2698 if (branch_name[0] == '-' && branch_name[1] == '\0')
2699 return got_error(GOT_ERR_BAD_REF_NAME);
2701 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2702 base_refname = strdup(GOT_REF_HEAD);
2703 if (base_refname == NULL)
2704 return got_error_from_errno("strdup");
2705 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2706 return got_error_from_errno("asprintf");
2708 err = got_ref_open(&base_ref, repo, base_refname, 0);
2709 if (err)
2710 goto done;
2711 err = got_ref_resolve(&id, repo, base_ref);
2712 got_ref_close(base_ref);
2713 if (err)
2714 goto done;
2716 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2717 err = got_error_from_errno("asprintf");
2718 goto done;
2721 err = got_ref_open(&ref, repo, refname, 0);
2722 if (err == NULL) {
2723 err = got_error(GOT_ERR_BRANCH_EXISTS);
2724 goto done;
2725 } else if (err->code != GOT_ERR_NOT_REF)
2726 goto done;
2728 err = got_ref_alloc(&ref, refname, id);
2729 if (err)
2730 goto done;
2732 err = got_ref_write(ref, repo);
2733 done:
2734 if (ref)
2735 got_ref_close(ref);
2736 free(id);
2737 free(base_refname);
2738 free(refname);
2739 return err;
2742 static const struct got_error *
2743 cmd_branch(int argc, char *argv[])
2745 const struct got_error *error = NULL;
2746 struct got_repository *repo = NULL;
2747 struct got_worktree *worktree = NULL;
2748 char *cwd = NULL, *repo_path = NULL;
2749 int ch, do_list = 0;
2750 const char *delref = NULL;
2752 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2753 switch (ch) {
2754 case 'd':
2755 delref = optarg;
2756 break;
2757 case 'r':
2758 repo_path = realpath(optarg, NULL);
2759 if (repo_path == NULL)
2760 err(1, "-r option");
2761 got_path_strip_trailing_slashes(repo_path);
2762 break;
2763 case 'l':
2764 do_list = 1;
2765 break;
2766 default:
2767 usage_branch();
2768 /* NOTREACHED */
2772 if (do_list && delref)
2773 errx(1, "-l and -d options are mutually exclusive\n");
2775 argc -= optind;
2776 argv += optind;
2778 if (do_list || delref) {
2779 if (argc > 0)
2780 usage_branch();
2781 } else if (argc < 1 || argc > 2)
2782 usage_branch();
2784 #ifndef PROFILE
2785 if (do_list) {
2786 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2787 NULL) == -1)
2788 err(1, "pledge");
2789 } else {
2790 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2791 "sendfd unveil", NULL) == -1)
2792 err(1, "pledge");
2794 #endif
2795 cwd = getcwd(NULL, 0);
2796 if (cwd == NULL) {
2797 error = got_error_from_errno("getcwd");
2798 goto done;
2801 if (repo_path == NULL) {
2802 error = got_worktree_open(&worktree, cwd);
2803 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2804 goto done;
2805 else
2806 error = NULL;
2807 if (worktree) {
2808 repo_path =
2809 strdup(got_worktree_get_repo_path(worktree));
2810 if (repo_path == NULL)
2811 error = got_error_from_errno("strdup");
2812 if (error)
2813 goto done;
2814 } else {
2815 repo_path = strdup(cwd);
2816 if (repo_path == NULL) {
2817 error = got_error_from_errno("strdup");
2818 goto done;
2823 error = got_repo_open(&repo, repo_path);
2824 if (error != NULL)
2825 goto done;
2827 error = apply_unveil(got_repo_get_path(repo), do_list,
2828 worktree ? got_worktree_get_root_path(worktree) : NULL);
2829 if (error)
2830 goto done;
2832 if (do_list)
2833 error = list_branches(repo, worktree);
2834 else if (delref)
2835 error = delete_branch(repo, delref);
2836 else {
2837 const char *base_branch;
2838 if (argc == 1) {
2839 base_branch = worktree ?
2840 got_worktree_get_head_ref_name(worktree) :
2841 GOT_REF_HEAD;
2842 } else
2843 base_branch = argv[1];
2844 error = add_branch(repo, argv[0], base_branch);
2846 done:
2847 if (repo)
2848 got_repo_close(repo);
2849 if (worktree)
2850 got_worktree_close(worktree);
2851 free(cwd);
2852 free(repo_path);
2853 return error;
2856 __dead static void
2857 usage_add(void)
2859 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2860 exit(1);
2863 static const struct got_error *
2864 cmd_add(int argc, char *argv[])
2866 const struct got_error *error = NULL;
2867 struct got_repository *repo = NULL;
2868 struct got_worktree *worktree = NULL;
2869 char *cwd = NULL;
2870 struct got_pathlist_head paths;
2871 struct got_pathlist_entry *pe;
2872 int ch, x;
2874 TAILQ_INIT(&paths);
2876 while ((ch = getopt(argc, argv, "")) != -1) {
2877 switch (ch) {
2878 default:
2879 usage_add();
2880 /* NOTREACHED */
2884 argc -= optind;
2885 argv += optind;
2887 #ifndef PROFILE
2888 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2889 NULL) == -1)
2890 err(1, "pledge");
2891 #endif
2892 if (argc < 1)
2893 usage_add();
2895 cwd = getcwd(NULL, 0);
2896 if (cwd == NULL) {
2897 error = got_error_from_errno("getcwd");
2898 goto done;
2901 error = got_worktree_open(&worktree, cwd);
2902 if (error)
2903 goto done;
2905 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2906 if (error != NULL)
2907 goto done;
2909 error = apply_unveil(got_repo_get_path(repo), 1,
2910 got_worktree_get_root_path(worktree));
2911 if (error)
2912 goto done;
2914 for (x = 0; x < argc; x++) {
2915 char *path = realpath(argv[x], NULL);
2916 if (path == NULL) {
2917 error = got_error_from_errno2("realpath", argv[x]);
2918 goto done;
2921 got_path_strip_trailing_slashes(path);
2922 error = got_pathlist_insert(&pe, &paths, path, NULL);
2923 if (error) {
2924 free(path);
2925 goto done;
2928 error = got_worktree_schedule_add(worktree, &paths, print_status,
2929 NULL, repo);
2930 done:
2931 if (repo)
2932 got_repo_close(repo);
2933 if (worktree)
2934 got_worktree_close(worktree);
2935 TAILQ_FOREACH(pe, &paths, entry)
2936 free((char *)pe->path);
2937 got_pathlist_free(&paths);
2938 free(cwd);
2939 return error;
2942 __dead static void
2943 usage_remove(void)
2945 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2946 exit(1);
2949 static const struct got_error *
2950 cmd_remove(int argc, char *argv[])
2952 const struct got_error *error = NULL;
2953 struct got_worktree *worktree = NULL;
2954 struct got_repository *repo = NULL;
2955 char *cwd = NULL;
2956 struct got_pathlist_head paths;
2957 struct got_pathlist_entry *pe;
2958 int ch, i, delete_local_mods = 0;
2960 TAILQ_INIT(&paths);
2962 while ((ch = getopt(argc, argv, "f")) != -1) {
2963 switch (ch) {
2964 case 'f':
2965 delete_local_mods = 1;
2966 break;
2967 default:
2968 usage_add();
2969 /* NOTREACHED */
2973 argc -= optind;
2974 argv += optind;
2976 #ifndef PROFILE
2977 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2978 NULL) == -1)
2979 err(1, "pledge");
2980 #endif
2981 if (argc < 1)
2982 usage_remove();
2984 cwd = getcwd(NULL, 0);
2985 if (cwd == NULL) {
2986 error = got_error_from_errno("getcwd");
2987 goto done;
2989 error = got_worktree_open(&worktree, cwd);
2990 if (error)
2991 goto done;
2993 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2994 if (error)
2995 goto done;
2997 error = apply_unveil(got_repo_get_path(repo), 1,
2998 got_worktree_get_root_path(worktree));
2999 if (error)
3000 goto done;
3002 for (i = 0; i < argc; i++) {
3003 char *path = realpath(argv[i], NULL);
3004 if (path == NULL) {
3005 error = got_error_from_errno2("realpath", argv[i]);
3006 goto done;
3009 got_path_strip_trailing_slashes(path);
3010 error = got_pathlist_insert(&pe, &paths, path, NULL);
3011 if (error) {
3012 free(path);
3013 goto done;
3016 error = got_worktree_schedule_delete(worktree, &paths,
3017 delete_local_mods, print_status, NULL, repo);
3018 if (error)
3019 goto done;
3020 done:
3021 if (repo)
3022 got_repo_close(repo);
3023 if (worktree)
3024 got_worktree_close(worktree);
3025 TAILQ_FOREACH(pe, &paths, entry)
3026 free((char *)pe->path);
3027 got_pathlist_free(&paths);
3028 free(cwd);
3029 return error;
3032 __dead static void
3033 usage_revert(void)
3035 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3036 exit(1);
3039 static const struct got_error *
3040 revert_progress(void *arg, unsigned char status, const char *path)
3042 while (path[0] == '/')
3043 path++;
3044 printf("%c %s\n", status, path);
3045 return NULL;
3048 static const struct got_error *
3049 cmd_revert(int argc, char *argv[])
3051 const struct got_error *error = NULL;
3052 struct got_worktree *worktree = NULL;
3053 struct got_repository *repo = NULL;
3054 char *cwd = NULL, *path = NULL;
3055 struct got_pathlist_head paths;
3056 struct got_pathlist_entry *pe;
3057 int ch, i;
3059 TAILQ_INIT(&paths);
3061 while ((ch = getopt(argc, argv, "")) != -1) {
3062 switch (ch) {
3063 default:
3064 usage_revert();
3065 /* NOTREACHED */
3069 argc -= optind;
3070 argv += optind;
3072 #ifndef PROFILE
3073 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3074 "unveil", NULL) == -1)
3075 err(1, "pledge");
3076 #endif
3077 if (argc < 1)
3078 usage_revert();
3080 for (i = 0; i < argc; i++) {
3081 char *path = realpath(argv[i], NULL);
3082 if (path == NULL) {
3083 error = got_error_from_errno2("realpath", argv[i]);
3084 goto done;
3087 got_path_strip_trailing_slashes(path);
3088 error = got_pathlist_insert(&pe, &paths, path, NULL);
3089 if (error) {
3090 free(path);
3091 goto done;
3095 cwd = getcwd(NULL, 0);
3096 if (cwd == NULL) {
3097 error = got_error_from_errno("getcwd");
3098 goto done;
3100 error = got_worktree_open(&worktree, cwd);
3101 if (error)
3102 goto done;
3104 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3105 if (error != NULL)
3106 goto done;
3108 error = apply_unveil(got_repo_get_path(repo), 1,
3109 got_worktree_get_root_path(worktree));
3110 if (error)
3111 goto done;
3113 error = got_worktree_revert(worktree, &paths,
3114 revert_progress, NULL, repo);
3115 if (error)
3116 goto done;
3117 done:
3118 if (repo)
3119 got_repo_close(repo);
3120 if (worktree)
3121 got_worktree_close(worktree);
3122 free(path);
3123 free(cwd);
3124 return error;
3127 __dead static void
3128 usage_commit(void)
3130 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3131 exit(1);
3134 struct collect_commit_logmsg_arg {
3135 const char *cmdline_log;
3136 const char *editor;
3137 const char *worktree_path;
3138 const char *branch_name;
3139 const char *repo_path;
3140 char *logmsg_path;
3144 static const struct got_error *
3145 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3146 void *arg)
3148 char *initial_content = NULL;
3149 struct got_pathlist_entry *pe;
3150 const struct got_error *err = NULL;
3151 char *template = NULL;
3152 struct collect_commit_logmsg_arg *a = arg;
3153 int fd;
3154 size_t len;
3156 /* if a message was specified on the command line, just use it */
3157 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3158 len = strlen(a->cmdline_log) + 1;
3159 *logmsg = malloc(len + 1);
3160 if (*logmsg == NULL)
3161 return got_error_from_errno("malloc");
3162 strlcpy(*logmsg, a->cmdline_log, len);
3163 return NULL;
3166 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3167 return got_error_from_errno("asprintf");
3169 if (asprintf(&initial_content,
3170 "\n# changes to be committed on branch %s:\n",
3171 a->branch_name) == -1)
3172 return got_error_from_errno("asprintf");
3174 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3175 if (err)
3176 goto done;
3178 dprintf(fd, initial_content);
3180 TAILQ_FOREACH(pe, commitable_paths, entry) {
3181 struct got_commitable *ct = pe->data;
3182 dprintf(fd, "# %c %s\n",
3183 got_commitable_get_status(ct),
3184 got_commitable_get_path(ct));
3186 close(fd);
3188 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3189 done:
3190 unlink(a->logmsg_path);
3191 free(a->logmsg_path);
3192 free(initial_content);
3193 free(template);
3195 /* Editor is done; we can now apply unveil(2) */
3196 if (err == NULL) {
3197 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3198 if (err) {
3199 free(*logmsg);
3200 *logmsg = NULL;
3203 return err;
3206 static const struct got_error *
3207 cmd_commit(int argc, char *argv[])
3209 const struct got_error *error = NULL;
3210 struct got_worktree *worktree = NULL;
3211 struct got_repository *repo = NULL;
3212 char *cwd = NULL, *path = NULL, *id_str = NULL;
3213 struct got_object_id *id = NULL;
3214 const char *logmsg = NULL;
3215 const char *got_author = getenv("GOT_AUTHOR");
3216 struct collect_commit_logmsg_arg cl_arg;
3217 char *editor = NULL;
3218 int ch, rebase_in_progress;
3220 while ((ch = getopt(argc, argv, "m:")) != -1) {
3221 switch (ch) {
3222 case 'm':
3223 logmsg = optarg;
3224 break;
3225 default:
3226 usage_commit();
3227 /* NOTREACHED */
3231 argc -= optind;
3232 argv += optind;
3234 #ifndef PROFILE
3235 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3236 "unveil", NULL) == -1)
3237 err(1, "pledge");
3238 #endif
3239 if (argc == 1) {
3240 path = realpath(argv[0], NULL);
3241 if (path == NULL) {
3242 error = got_error_from_errno2("realpath", argv[0]);
3243 goto done;
3245 got_path_strip_trailing_slashes(path);
3246 } else if (argc != 0)
3247 usage_commit();
3249 if (got_author == NULL) {
3250 /* TODO: Look current user up in password database */
3251 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3252 goto done;
3255 cwd = getcwd(NULL, 0);
3256 if (cwd == NULL) {
3257 error = got_error_from_errno("getcwd");
3258 goto done;
3260 error = got_worktree_open(&worktree, cwd);
3261 if (error)
3262 goto done;
3264 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3265 if (error)
3266 goto done;
3267 if (rebase_in_progress) {
3268 error = got_error(GOT_ERR_REBASING);
3269 goto done;
3272 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3273 if (error != NULL)
3274 goto done;
3277 * unveil(2) traverses exec(2); if an editor is used we have
3278 * to apply unveil after the log message has been written.
3280 if (logmsg == NULL || strlen(logmsg) == 0)
3281 error = get_editor(&editor);
3282 else
3283 error = apply_unveil(got_repo_get_path(repo), 0,
3284 got_worktree_get_root_path(worktree));
3285 if (error)
3286 goto done;
3288 cl_arg.editor = editor;
3289 cl_arg.cmdline_log = logmsg;
3290 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3291 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3292 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3293 cl_arg.branch_name += 5;
3294 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3295 cl_arg.branch_name += 6;
3296 cl_arg.repo_path = got_repo_get_path(repo);
3297 cl_arg.logmsg_path = NULL;
3298 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3299 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3300 if (error) {
3301 if (cl_arg.logmsg_path)
3302 fprintf(stderr, "%s: log message preserved in %s\n",
3303 getprogname(), cl_arg.logmsg_path);
3304 goto done;
3307 if (cl_arg.logmsg_path)
3308 unlink(cl_arg.logmsg_path);
3310 error = got_object_id_str(&id_str, id);
3311 if (error)
3312 goto done;
3313 printf("Created commit %s\n", id_str);
3314 done:
3315 if (repo)
3316 got_repo_close(repo);
3317 if (worktree)
3318 got_worktree_close(worktree);
3319 free(path);
3320 free(cwd);
3321 free(id_str);
3322 free(editor);
3323 return error;
3326 __dead static void
3327 usage_cherrypick(void)
3329 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3330 exit(1);
3333 static const struct got_error *
3334 cmd_cherrypick(int argc, char *argv[])
3336 const struct got_error *error = NULL;
3337 struct got_worktree *worktree = NULL;
3338 struct got_repository *repo = NULL;
3339 char *cwd = NULL, *commit_id_str = NULL;
3340 struct got_object_id *commit_id = NULL;
3341 struct got_commit_object *commit = NULL;
3342 struct got_object_qid *pid;
3343 struct got_reference *head_ref = NULL;
3344 int ch, did_something = 0;
3346 while ((ch = getopt(argc, argv, "")) != -1) {
3347 switch (ch) {
3348 default:
3349 usage_cherrypick();
3350 /* NOTREACHED */
3354 argc -= optind;
3355 argv += optind;
3357 #ifndef PROFILE
3358 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3359 "unveil", NULL) == -1)
3360 err(1, "pledge");
3361 #endif
3362 if (argc != 1)
3363 usage_cherrypick();
3365 cwd = getcwd(NULL, 0);
3366 if (cwd == NULL) {
3367 error = got_error_from_errno("getcwd");
3368 goto done;
3370 error = got_worktree_open(&worktree, cwd);
3371 if (error)
3372 goto done;
3374 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3375 if (error != NULL)
3376 goto done;
3378 error = apply_unveil(got_repo_get_path(repo), 0,
3379 got_worktree_get_root_path(worktree));
3380 if (error)
3381 goto done;
3383 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3384 GOT_OBJ_TYPE_COMMIT, repo);
3385 if (error != NULL) {
3386 struct got_reference *ref;
3387 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3388 goto done;
3389 error = got_ref_open(&ref, repo, argv[0], 0);
3390 if (error != NULL)
3391 goto done;
3392 error = got_ref_resolve(&commit_id, repo, ref);
3393 got_ref_close(ref);
3394 if (error != NULL)
3395 goto done;
3397 error = got_object_id_str(&commit_id_str, commit_id);
3398 if (error)
3399 goto done;
3401 error = got_ref_open(&head_ref, repo,
3402 got_worktree_get_head_ref_name(worktree), 0);
3403 if (error != NULL)
3404 goto done;
3406 error = check_same_branch(commit_id, head_ref, repo);
3407 if (error) {
3408 if (error->code != GOT_ERR_ANCESTRY)
3409 goto done;
3410 error = NULL;
3411 } else {
3412 error = got_error(GOT_ERR_SAME_BRANCH);
3413 goto done;
3416 error = got_object_open_as_commit(&commit, repo, commit_id);
3417 if (error)
3418 goto done;
3419 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3420 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3421 commit_id, repo, update_progress, &did_something, check_cancelled,
3422 NULL);
3423 if (error != NULL)
3424 goto done;
3426 if (did_something)
3427 printf("Merged commit %s\n", commit_id_str);
3428 done:
3429 if (commit)
3430 got_object_commit_close(commit);
3431 free(commit_id_str);
3432 if (head_ref)
3433 got_ref_close(head_ref);
3434 if (worktree)
3435 got_worktree_close(worktree);
3436 if (repo)
3437 got_repo_close(repo);
3438 return error;
3441 __dead static void
3442 usage_backout(void)
3444 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3445 exit(1);
3448 static const struct got_error *
3449 cmd_backout(int argc, char *argv[])
3451 const struct got_error *error = NULL;
3452 struct got_worktree *worktree = NULL;
3453 struct got_repository *repo = NULL;
3454 char *cwd = NULL, *commit_id_str = NULL;
3455 struct got_object_id *commit_id = NULL;
3456 struct got_commit_object *commit = NULL;
3457 struct got_object_qid *pid;
3458 struct got_reference *head_ref = NULL;
3459 int ch, did_something = 0;
3461 while ((ch = getopt(argc, argv, "")) != -1) {
3462 switch (ch) {
3463 default:
3464 usage_backout();
3465 /* NOTREACHED */
3469 argc -= optind;
3470 argv += optind;
3472 #ifndef PROFILE
3473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3474 "unveil", NULL) == -1)
3475 err(1, "pledge");
3476 #endif
3477 if (argc != 1)
3478 usage_backout();
3480 cwd = getcwd(NULL, 0);
3481 if (cwd == NULL) {
3482 error = got_error_from_errno("getcwd");
3483 goto done;
3485 error = got_worktree_open(&worktree, cwd);
3486 if (error)
3487 goto done;
3489 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3490 if (error != NULL)
3491 goto done;
3493 error = apply_unveil(got_repo_get_path(repo), 0,
3494 got_worktree_get_root_path(worktree));
3495 if (error)
3496 goto done;
3498 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3499 GOT_OBJ_TYPE_COMMIT, repo);
3500 if (error != NULL) {
3501 struct got_reference *ref;
3502 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3503 goto done;
3504 error = got_ref_open(&ref, repo, argv[0], 0);
3505 if (error != NULL)
3506 goto done;
3507 error = got_ref_resolve(&commit_id, repo, ref);
3508 got_ref_close(ref);
3509 if (error != NULL)
3510 goto done;
3512 error = got_object_id_str(&commit_id_str, commit_id);
3513 if (error)
3514 goto done;
3516 error = got_ref_open(&head_ref, repo,
3517 got_worktree_get_head_ref_name(worktree), 0);
3518 if (error != NULL)
3519 goto done;
3521 error = check_same_branch(commit_id, head_ref, repo);
3522 if (error)
3523 goto done;
3525 error = got_object_open_as_commit(&commit, repo, commit_id);
3526 if (error)
3527 goto done;
3528 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3529 if (pid == NULL) {
3530 error = got_error(GOT_ERR_ROOT_COMMIT);
3531 goto done;
3534 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3535 update_progress, &did_something, check_cancelled, NULL);
3536 if (error != NULL)
3537 goto done;
3539 if (did_something)
3540 printf("Backed out commit %s\n", commit_id_str);
3541 done:
3542 if (commit)
3543 got_object_commit_close(commit);
3544 free(commit_id_str);
3545 if (head_ref)
3546 got_ref_close(head_ref);
3547 if (worktree)
3548 got_worktree_close(worktree);
3549 if (repo)
3550 got_repo_close(repo);
3551 return error;
3554 __dead static void
3555 usage_rebase(void)
3557 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3558 getprogname());
3559 exit(1);
3562 void
3563 trim_logmsg(char *logmsg, int limit)
3565 char *nl;
3566 size_t len;
3568 len = strlen(logmsg);
3569 if (len > limit)
3570 len = limit;
3571 logmsg[len] = '\0';
3572 nl = strchr(logmsg, '\n');
3573 if (nl)
3574 *nl = '\0';
3577 static const struct got_error *
3578 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3580 const char *logmsg0 = NULL;
3582 logmsg0 = got_object_commit_get_logmsg(commit);
3584 while (isspace((unsigned char)logmsg0[0]))
3585 logmsg0++;
3587 *logmsg = strdup(logmsg0);
3588 if (*logmsg == NULL)
3589 return got_error_from_errno("strdup");
3591 trim_logmsg(*logmsg, limit);
3592 return NULL;
3595 static const struct got_error *
3596 show_rebase_progress(struct got_commit_object *commit,
3597 struct got_object_id *old_id, struct got_object_id *new_id)
3599 const struct got_error *err;
3600 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3602 err = got_object_id_str(&old_id_str, old_id);
3603 if (err)
3604 goto done;
3606 if (new_id) {
3607 err = got_object_id_str(&new_id_str, new_id);
3608 if (err)
3609 goto done;
3612 old_id_str[12] = '\0';
3613 if (new_id_str)
3614 new_id_str[12] = '\0';
3616 err = get_short_logmsg(&logmsg, 42, commit);
3617 if (err)
3618 goto done;
3620 printf("%s -> %s: %s\n", old_id_str,
3621 new_id_str ? new_id_str : "no-op change", logmsg);
3622 done:
3623 free(old_id_str);
3624 free(new_id_str);
3625 return err;
3628 static const struct got_error *
3629 rebase_progress(void *arg, unsigned char status, const char *path)
3631 unsigned char *rebase_status = arg;
3633 while (path[0] == '/')
3634 path++;
3635 printf("%c %s\n", status, path);
3637 if (*rebase_status == GOT_STATUS_CONFLICT)
3638 return NULL;
3639 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3640 *rebase_status = status;
3641 return NULL;
3644 static const struct got_error *
3645 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3646 struct got_reference *branch, struct got_reference *new_base_branch,
3647 struct got_reference *tmp_branch, struct got_repository *repo)
3649 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3650 return got_worktree_rebase_complete(worktree, fileindex,
3651 new_base_branch, tmp_branch, branch, repo);
3654 static const struct got_error *
3655 rebase_commit(struct got_pathlist_head *merged_paths,
3656 struct got_worktree *worktree, struct got_fileindex *fileindex,
3657 struct got_reference *tmp_branch,
3658 struct got_object_id *commit_id, struct got_repository *repo)
3660 const struct got_error *error;
3661 struct got_commit_object *commit;
3662 struct got_object_id *new_commit_id;
3664 error = got_object_open_as_commit(&commit, repo, commit_id);
3665 if (error)
3666 return error;
3668 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3669 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3670 if (error) {
3671 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3672 goto done;
3673 error = show_rebase_progress(commit, commit_id, NULL);
3674 } else {
3675 error = show_rebase_progress(commit, commit_id, new_commit_id);
3676 free(new_commit_id);
3678 done:
3679 got_object_commit_close(commit);
3680 return error;
3683 struct check_path_prefix_arg {
3684 const char *path_prefix;
3685 size_t len;
3686 int errcode;
3689 static const struct got_error *
3690 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3691 struct got_blob_object *blob2, struct got_object_id *id1,
3692 struct got_object_id *id2, const char *path1, const char *path2,
3693 struct got_repository *repo)
3695 struct check_path_prefix_arg *a = arg;
3697 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3698 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3699 return got_error(a->errcode);
3701 return NULL;
3704 static const struct got_error *
3705 check_path_prefix(struct got_object_id *parent_id,
3706 struct got_object_id *commit_id, const char *path_prefix,
3707 int errcode, struct got_repository *repo)
3709 const struct got_error *err;
3710 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3711 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3712 struct check_path_prefix_arg cpp_arg;
3714 if (got_path_is_root_dir(path_prefix))
3715 return NULL;
3717 err = got_object_open_as_commit(&commit, repo, commit_id);
3718 if (err)
3719 goto done;
3721 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3722 if (err)
3723 goto done;
3725 err = got_object_open_as_tree(&tree1, repo,
3726 got_object_commit_get_tree_id(parent_commit));
3727 if (err)
3728 goto done;
3730 err = got_object_open_as_tree(&tree2, repo,
3731 got_object_commit_get_tree_id(commit));
3732 if (err)
3733 goto done;
3735 cpp_arg.path_prefix = path_prefix;
3736 while (cpp_arg.path_prefix[0] == '/')
3737 cpp_arg.path_prefix++;
3738 cpp_arg.len = strlen(cpp_arg.path_prefix);
3739 cpp_arg.errcode = errcode;
3740 err = got_diff_tree(tree1, tree2, "", "", repo,
3741 check_path_prefix_in_diff, &cpp_arg, 0);
3742 done:
3743 if (tree1)
3744 got_object_tree_close(tree1);
3745 if (tree2)
3746 got_object_tree_close(tree2);
3747 if (commit)
3748 got_object_commit_close(commit);
3749 if (parent_commit)
3750 got_object_commit_close(parent_commit);
3751 return err;
3754 static const struct got_error *
3755 collect_commits(struct got_object_id_queue *commits,
3756 struct got_object_id *initial_commit_id,
3757 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3758 const char *path_prefix, int path_prefix_errcode,
3759 struct got_repository *repo)
3761 const struct got_error *err = NULL;
3762 struct got_commit_graph *graph = NULL;
3763 struct got_object_id *parent_id = NULL;
3764 struct got_object_qid *qid;
3765 struct got_object_id *commit_id = initial_commit_id;
3767 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3768 if (err)
3769 return err;
3771 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3772 if (err)
3773 goto done;
3774 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3775 err = got_commit_graph_iter_next(&parent_id, graph);
3776 if (err) {
3777 if (err->code == GOT_ERR_ITER_COMPLETED) {
3778 err = got_error_msg(GOT_ERR_ANCESTRY,
3779 "ran out of commits to rebase before "
3780 "youngest common ancestor commit has "
3781 "been reached?!?");
3782 goto done;
3783 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3784 goto done;
3785 err = got_commit_graph_fetch_commits(graph, 1, repo);
3786 if (err)
3787 goto done;
3788 } else {
3789 err = check_path_prefix(parent_id, commit_id,
3790 path_prefix, path_prefix_errcode, repo);
3791 if (err)
3792 goto done;
3794 err = got_object_qid_alloc(&qid, commit_id);
3795 if (err)
3796 goto done;
3797 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3798 commit_id = parent_id;
3801 done:
3802 got_commit_graph_close(graph);
3803 return err;
3806 static const struct got_error *
3807 cmd_rebase(int argc, char *argv[])
3809 const struct got_error *error = NULL;
3810 struct got_worktree *worktree = NULL;
3811 struct got_repository *repo = NULL;
3812 struct got_fileindex *fileindex = NULL;
3813 char *cwd = NULL;
3814 struct got_reference *branch = NULL;
3815 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3816 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3817 struct got_object_id *resume_commit_id = NULL;
3818 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3819 struct got_commit_object *commit = NULL;
3820 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3821 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3822 struct got_object_id_queue commits;
3823 struct got_pathlist_head merged_paths;
3824 const struct got_object_id_queue *parent_ids;
3825 struct got_object_qid *qid, *pid;
3827 SIMPLEQ_INIT(&commits);
3828 TAILQ_INIT(&merged_paths);
3830 while ((ch = getopt(argc, argv, "ac")) != -1) {
3831 switch (ch) {
3832 case 'a':
3833 abort_rebase = 1;
3834 break;
3835 case 'c':
3836 continue_rebase = 1;
3837 break;
3838 default:
3839 usage_rebase();
3840 /* NOTREACHED */
3844 argc -= optind;
3845 argv += optind;
3847 #ifndef PROFILE
3848 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3849 "unveil", NULL) == -1)
3850 err(1, "pledge");
3851 #endif
3852 if (abort_rebase && continue_rebase)
3853 usage_rebase();
3854 else if (abort_rebase || continue_rebase) {
3855 if (argc != 0)
3856 usage_rebase();
3857 } else if (argc != 1)
3858 usage_rebase();
3860 cwd = getcwd(NULL, 0);
3861 if (cwd == NULL) {
3862 error = got_error_from_errno("getcwd");
3863 goto done;
3865 error = got_worktree_open(&worktree, cwd);
3866 if (error)
3867 goto done;
3869 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3870 if (error != NULL)
3871 goto done;
3873 error = apply_unveil(got_repo_get_path(repo), 0,
3874 got_worktree_get_root_path(worktree));
3875 if (error)
3876 goto done;
3878 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3879 if (error)
3880 goto done;
3882 if (abort_rebase) {
3883 int did_something;
3884 if (!rebase_in_progress) {
3885 error = got_error(GOT_ERR_NOT_REBASING);
3886 goto done;
3888 error = got_worktree_rebase_continue(&resume_commit_id,
3889 &new_base_branch, &tmp_branch, &branch, &fileindex,
3890 worktree, repo);
3891 if (error)
3892 goto done;
3893 printf("Switching work tree to %s\n",
3894 got_ref_get_symref_target(new_base_branch));
3895 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3896 new_base_branch, update_progress, &did_something);
3897 if (error)
3898 goto done;
3899 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3900 goto done; /* nothing else to do */
3903 if (continue_rebase) {
3904 if (!rebase_in_progress) {
3905 error = got_error(GOT_ERR_NOT_REBASING);
3906 goto done;
3908 error = got_worktree_rebase_continue(&resume_commit_id,
3909 &new_base_branch, &tmp_branch, &branch, &fileindex,
3910 worktree, repo);
3911 if (error)
3912 goto done;
3914 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3915 resume_commit_id, repo);
3916 if (error)
3917 goto done;
3919 yca_id = got_object_id_dup(resume_commit_id);
3920 if (yca_id == NULL) {
3921 error = got_error_from_errno("got_object_id_dup");
3922 goto done;
3924 } else {
3925 error = got_ref_open(&branch, repo, argv[0], 0);
3926 if (error != NULL)
3927 goto done;
3929 error = check_same_branch(
3930 got_worktree_get_base_commit_id(worktree), branch, repo);
3931 if (error) {
3932 if (error->code != GOT_ERR_ANCESTRY)
3933 goto done;
3934 error = NULL;
3935 } else {
3936 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3937 "specified branch resolves to a commit which "
3938 "is already contained in work tree's branch");
3939 goto done;
3943 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3944 if (error)
3945 goto done;
3947 if (!continue_rebase) {
3948 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3949 got_worktree_get_base_commit_id(worktree),
3950 branch_head_commit_id, repo);
3951 if (error)
3952 goto done;
3953 if (yca_id == NULL) {
3954 error = got_error_msg(GOT_ERR_ANCESTRY,
3955 "specified branch shares no common ancestry "
3956 "with work tree's branch");
3957 goto done;
3960 error = got_worktree_rebase_prepare(&new_base_branch,
3961 &tmp_branch, &fileindex, worktree, branch, repo);
3962 if (error)
3963 goto done;
3966 commit_id = branch_head_commit_id;
3967 error = got_object_open_as_commit(&commit, repo, commit_id);
3968 if (error)
3969 goto done;
3971 parent_ids = got_object_commit_get_parent_ids(commit);
3972 pid = SIMPLEQ_FIRST(parent_ids);
3973 error = collect_commits(&commits, commit_id, pid->id,
3974 yca_id, got_worktree_get_path_prefix(worktree),
3975 GOT_ERR_REBASE_PATH, repo);
3976 got_object_commit_close(commit);
3977 commit = NULL;
3978 if (error)
3979 goto done;
3981 if (SIMPLEQ_EMPTY(&commits)) {
3982 if (continue_rebase)
3983 error = rebase_complete(worktree, fileindex,
3984 branch, new_base_branch, tmp_branch, repo);
3985 else
3986 error = got_error(GOT_ERR_EMPTY_REBASE);
3987 goto done;
3990 pid = NULL;
3991 SIMPLEQ_FOREACH(qid, &commits, entry) {
3992 commit_id = qid->id;
3993 parent_id = pid ? pid->id : yca_id;
3994 pid = qid;
3996 error = got_worktree_rebase_merge_files(&merged_paths,
3997 worktree, fileindex, parent_id, commit_id, repo,
3998 rebase_progress, &rebase_status, check_cancelled, NULL);
3999 if (error)
4000 goto done;
4002 if (rebase_status == GOT_STATUS_CONFLICT) {
4003 got_worktree_rebase_pathlist_free(&merged_paths);
4004 break;
4007 error = rebase_commit(&merged_paths, worktree, fileindex,
4008 tmp_branch, commit_id, repo);
4009 got_worktree_rebase_pathlist_free(&merged_paths);
4010 if (error)
4011 goto done;
4014 if (rebase_status == GOT_STATUS_CONFLICT) {
4015 error = got_worktree_rebase_postpone(worktree, fileindex);
4016 if (error)
4017 goto done;
4018 error = got_error_msg(GOT_ERR_CONFLICTS,
4019 "conflicts must be resolved before rebasing can continue");
4020 } else
4021 error = rebase_complete(worktree, fileindex, branch,
4022 new_base_branch, tmp_branch, repo);
4023 done:
4024 got_object_id_queue_free(&commits);
4025 free(branch_head_commit_id);
4026 free(resume_commit_id);
4027 free(yca_id);
4028 if (commit)
4029 got_object_commit_close(commit);
4030 if (branch)
4031 got_ref_close(branch);
4032 if (new_base_branch)
4033 got_ref_close(new_base_branch);
4034 if (tmp_branch)
4035 got_ref_close(tmp_branch);
4036 if (worktree)
4037 got_worktree_close(worktree);
4038 if (repo)
4039 got_repo_close(repo);
4040 return error;
4043 __dead static void
4044 usage_histedit(void)
4046 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4047 getprogname());
4048 exit(1);
4051 #define GOT_HISTEDIT_PICK 'p'
4052 #define GOT_HISTEDIT_EDIT 'e'
4053 #define GOT_HISTEDIT_FOLD 'f'
4054 #define GOT_HISTEDIT_DROP 'd'
4055 #define GOT_HISTEDIT_MESG 'm'
4057 static struct got_histedit_cmd {
4058 unsigned char code;
4059 const char *name;
4060 const char *desc;
4061 } got_histedit_cmds[] = {
4062 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4063 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4064 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4065 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4066 { GOT_HISTEDIT_MESG, "mesg",
4067 "single-line log message for commit above (open editor if empty)" },
4070 struct got_histedit_list_entry {
4071 TAILQ_ENTRY(got_histedit_list_entry) entry;
4072 struct got_object_id *commit_id;
4073 const struct got_histedit_cmd *cmd;
4074 char *logmsg;
4076 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4078 static const struct got_error *
4079 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4080 FILE *f, struct got_repository *repo)
4082 const struct got_error *err = NULL;
4083 char *logmsg = NULL, *id_str = NULL;
4084 struct got_commit_object *commit = NULL;
4085 size_t n;
4087 err = got_object_open_as_commit(&commit, repo, commit_id);
4088 if (err)
4089 goto done;
4091 err = get_short_logmsg(&logmsg, 34, commit);
4092 if (err)
4093 goto done;
4095 err = got_object_id_str(&id_str, commit_id);
4096 if (err)
4097 goto done;
4099 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4100 if (n < 0)
4101 err = got_ferror(f, GOT_ERR_IO);
4102 done:
4103 if (commit)
4104 got_object_commit_close(commit);
4105 free(id_str);
4106 free(logmsg);
4107 return err;
4110 static const struct got_error *
4111 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4112 struct got_repository *repo)
4114 const struct got_error *err = NULL;
4115 struct got_object_qid *qid;
4117 if (SIMPLEQ_EMPTY(commits))
4118 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4120 SIMPLEQ_FOREACH(qid, commits, entry) {
4121 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4122 f, repo);
4123 if (err)
4124 break;
4127 return err;
4130 static const struct got_error *
4131 write_cmd_list(FILE *f)
4133 const struct got_error *err = NULL;
4134 int n, i;
4136 n = fprintf(f, "# Available histedit commands:\n");
4137 if (n < 0)
4138 return got_ferror(f, GOT_ERR_IO);
4140 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4141 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4142 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4143 cmd->desc);
4144 if (n < 0) {
4145 err = got_ferror(f, GOT_ERR_IO);
4146 break;
4149 n = fprintf(f, "# Commits will be processed in order from top to "
4150 "bottom of this file.\n");
4151 if (n < 0)
4152 return got_ferror(f, GOT_ERR_IO);
4153 return err;
4156 static const struct got_error *
4157 histedit_syntax_error(int lineno)
4159 static char msg[42];
4160 int ret;
4162 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4163 lineno);
4164 if (ret == -1 || ret >= sizeof(msg))
4165 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4167 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4170 static const struct got_error *
4171 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4172 char *logmsg, struct got_repository *repo)
4174 const struct got_error *err;
4175 struct got_commit_object *folded_commit = NULL;
4176 char *id_str;
4178 err = got_object_id_str(&id_str, hle->commit_id);
4179 if (err)
4180 return err;
4182 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4183 if (err)
4184 goto done;
4186 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4187 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4188 got_object_commit_get_logmsg(folded_commit)) == -1) {
4189 err = got_error_from_errno("asprintf");
4190 goto done;
4192 done:
4193 if (folded_commit)
4194 got_object_commit_close(folded_commit);
4195 free(id_str);
4196 return err;
4199 static struct got_histedit_list_entry *
4200 get_folded_commits(struct got_histedit_list_entry *hle)
4202 struct got_histedit_list_entry *prev, *folded = NULL;
4204 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4205 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4206 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4207 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4208 folded = prev;
4209 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4212 return folded;
4215 static const struct got_error *
4216 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4217 struct got_repository *repo)
4219 char *logmsg_path = NULL, *id_str = NULL;
4220 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4221 const struct got_error *err = NULL;
4222 struct got_commit_object *commit = NULL;
4223 int fd;
4224 struct got_histedit_list_entry *folded = NULL;
4226 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4227 if (err)
4228 return err;
4230 folded = get_folded_commits(hle);
4231 if (folded) {
4232 while (folded != hle) {
4233 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4234 folded = TAILQ_NEXT(folded, entry);
4235 continue;
4237 err = append_folded_commit_msg(&new_msg, folded,
4238 logmsg, repo);
4239 if (err)
4240 goto done;
4241 free(logmsg);
4242 logmsg = new_msg;
4243 folded = TAILQ_NEXT(folded, entry);
4247 err = got_object_id_str(&id_str, hle->commit_id);
4248 if (err)
4249 goto done;
4250 if (asprintf(&new_msg,
4251 "%s\n# original log message of commit %s: %s",
4252 logmsg ? logmsg : "", id_str,
4253 got_object_commit_get_logmsg(commit)) == -1) {
4254 err = got_error_from_errno("asprintf");
4255 goto done;
4257 free(logmsg);
4258 logmsg = new_msg;
4260 err = got_object_id_str(&id_str, hle->commit_id);
4261 if (err)
4262 goto done;
4264 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4265 if (err)
4266 goto done;
4268 dprintf(fd, logmsg);
4269 close(fd);
4271 err = get_editor(&editor);
4272 if (err)
4273 goto done;
4275 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4276 if (err) {
4277 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4278 goto done;
4279 err = NULL;
4280 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4281 if (hle->logmsg == NULL)
4282 err = got_error_from_errno("strdup");
4284 done:
4285 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4286 err = got_error_from_errno2("unlink", logmsg_path);
4287 free(logmsg_path);
4288 free(logmsg);
4289 free(editor);
4290 if (commit)
4291 got_object_commit_close(commit);
4292 return err;
4295 static const struct got_error *
4296 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4297 FILE *f, struct got_repository *repo)
4299 const struct got_error *err = NULL;
4300 char *line = NULL, *p, *end;
4301 size_t size;
4302 ssize_t len;
4303 int lineno = 0, i;
4304 const struct got_histedit_cmd *cmd;
4305 struct got_object_id *commit_id = NULL;
4306 struct got_histedit_list_entry *hle = NULL;
4308 for (;;) {
4309 len = getline(&line, &size, f);
4310 if (len == -1) {
4311 const struct got_error *getline_err;
4312 if (feof(f))
4313 break;
4314 getline_err = got_error_from_errno("getline");
4315 err = got_ferror(f, getline_err->code);
4316 break;
4318 lineno++;
4319 p = line;
4320 while (isspace((unsigned char)p[0]))
4321 p++;
4322 if (p[0] == '#' || p[0] == '\0') {
4323 free(line);
4324 line = NULL;
4325 continue;
4327 cmd = NULL;
4328 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4329 cmd = &got_histedit_cmds[i];
4330 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4331 isspace((unsigned char)p[strlen(cmd->name)])) {
4332 p += strlen(cmd->name);
4333 break;
4335 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4336 p++;
4337 break;
4340 if (i == nitems(got_histedit_cmds)) {
4341 err = histedit_syntax_error(lineno);
4342 break;
4344 while (isspace((unsigned char)p[0]))
4345 p++;
4346 if (cmd->code == GOT_HISTEDIT_MESG) {
4347 if (hle == NULL || hle->logmsg != NULL) {
4348 err = got_error(GOT_ERR_HISTEDIT_CMD);
4349 break;
4351 if (p[0] == '\0') {
4352 err = histedit_edit_logmsg(hle, repo);
4353 if (err)
4354 break;
4355 } else {
4356 hle->logmsg = strdup(p);
4357 if (hle->logmsg == NULL) {
4358 err = got_error_from_errno("strdup");
4359 break;
4362 free(line);
4363 line = NULL;
4364 continue;
4365 } else {
4366 end = p;
4367 while (end[0] && !isspace((unsigned char)end[0]))
4368 end++;
4369 *end = '\0';
4371 err = got_object_resolve_id_str(&commit_id, repo, p);
4372 if (err) {
4373 /* override error code */
4374 err = histedit_syntax_error(lineno);
4375 break;
4378 hle = malloc(sizeof(*hle));
4379 if (hle == NULL) {
4380 err = got_error_from_errno("malloc");
4381 break;
4383 hle->cmd = cmd;
4384 hle->commit_id = commit_id;
4385 hle->logmsg = NULL;
4386 commit_id = NULL;
4387 free(line);
4388 line = NULL;
4389 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4392 free(line);
4393 free(commit_id);
4394 return err;
4397 static const struct got_error *
4398 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4399 const char *path, struct got_repository *repo)
4401 const struct got_error *err = NULL;
4402 char *editor;
4403 FILE *f = NULL;
4405 err = get_editor(&editor);
4406 if (err)
4407 return err;
4409 if (spawn_editor(editor, path) == -1) {
4410 err = got_error_from_errno("failed spawning editor");
4411 goto done;
4414 f = fopen(path, "r");
4415 if (f == NULL) {
4416 err = got_error_from_errno("fopen");
4417 goto done;
4419 err = histedit_parse_list(histedit_cmds, f, repo);
4420 done:
4421 if (f && fclose(f) != 0 && err == NULL)
4422 err = got_error_from_errno("fclose");
4423 free(editor);
4424 return err;
4427 static const struct got_error *
4428 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4429 struct got_object_id_queue *, const char *, struct got_repository *);
4431 static const struct got_error *
4432 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4433 struct got_object_id_queue *commits, struct got_repository *repo)
4435 const struct got_error *err;
4436 FILE *f = NULL;
4437 char *path = NULL;
4439 err = got_opentemp_named(&path, &f, "got-histedit");
4440 if (err)
4441 return err;
4443 err = write_cmd_list(f);
4444 if (err)
4445 goto done;
4447 err = histedit_write_commit_list(commits, f, repo);
4448 if (err)
4449 goto done;
4451 if (fclose(f) != 0) {
4452 err = got_error_from_errno("fclose");
4453 goto done;
4455 f = NULL;
4457 err = histedit_run_editor(histedit_cmds, path, repo);
4458 if (err) {
4459 const char *errmsg = err->msg;
4460 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4461 goto done;
4462 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4463 commits, path, repo);
4465 done:
4466 if (f && fclose(f) != 0 && err == NULL)
4467 err = got_error_from_errno("fclose");
4468 if (path && unlink(path) != 0 && err == NULL)
4469 err = got_error_from_errno2("unlink", path);
4470 free(path);
4471 return err;
4474 static const struct got_error *
4475 histedit_save_list(struct got_histedit_list *histedit_cmds,
4476 struct got_worktree *worktree, struct got_repository *repo)
4478 const struct got_error *err = NULL;
4479 char *path = NULL;
4480 FILE *f = NULL;
4481 struct got_histedit_list_entry *hle;
4482 struct got_commit_object *commit = NULL;
4484 err = got_worktree_get_histedit_list_path(&path, worktree);
4485 if (err)
4486 return err;
4488 f = fopen(path, "w");
4489 if (f == NULL) {
4490 err = got_error_from_errno2("fopen", path);
4491 goto done;
4493 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4494 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4495 repo);
4496 if (err)
4497 break;
4499 if (hle->logmsg) {
4500 int n = fprintf(f, "%c %s\n",
4501 GOT_HISTEDIT_MESG, hle->logmsg);
4502 if (n < 0) {
4503 err = got_ferror(f, GOT_ERR_IO);
4504 break;
4508 done:
4509 if (f && fclose(f) != 0 && err == NULL)
4510 err = got_error_from_errno("fclose");
4511 free(path);
4512 if (commit)
4513 got_object_commit_close(commit);
4514 return err;
4517 static const struct got_error *
4518 histedit_load_list(struct got_histedit_list *histedit_cmds,
4519 const char *path, struct got_repository *repo)
4521 const struct got_error *err = NULL;
4522 FILE *f = NULL;
4524 f = fopen(path, "r");
4525 if (f == NULL) {
4526 err = got_error_from_errno2("fopen", path);
4527 goto done;
4530 err = histedit_parse_list(histedit_cmds, f, repo);
4531 done:
4532 if (f && fclose(f) != 0 && err == NULL)
4533 err = got_error_from_errno("fclose");
4534 return err;
4537 static const struct got_error *
4538 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4539 const char *errmsg, struct got_object_id_queue *commits,
4540 const char *path, struct got_repository *repo)
4542 const struct got_error *err = NULL;
4543 int resp = ' ';
4545 while (resp != 'c' && resp != 'r' && resp != 'a') {
4546 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4547 "or (a)bort: ", getprogname(), errmsg);
4548 resp = getchar();
4549 if (resp == 'c') {
4550 err = histedit_run_editor(histedit_cmds, path, repo);
4551 if (err) {
4552 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4553 break;
4554 resp = ' ';
4555 continue;
4557 } else if (resp == 'r') {
4558 err = histedit_edit_script(histedit_cmds,
4559 commits, repo);
4560 if (err) {
4561 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4562 break;
4563 resp = ' ';
4564 continue;
4566 } else if (resp == 'a') {
4567 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4568 break;
4569 } else
4570 printf("invalid response '%c'\n", resp);
4573 return err;
4576 static const struct got_error *
4577 histedit_complete(struct got_worktree *worktree,
4578 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4579 struct got_reference *branch, struct got_repository *repo)
4581 printf("Switching work tree to %s\n",
4582 got_ref_get_symref_target(branch));
4583 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4584 branch, repo);
4587 static const struct got_error *
4588 show_histedit_progress(struct got_commit_object *commit,
4589 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4591 const struct got_error *err;
4592 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4594 err = got_object_id_str(&old_id_str, hle->commit_id);
4595 if (err)
4596 goto done;
4598 if (new_id) {
4599 err = got_object_id_str(&new_id_str, new_id);
4600 if (err)
4601 goto done;
4604 old_id_str[12] = '\0';
4605 if (new_id_str)
4606 new_id_str[12] = '\0';
4608 if (hle->logmsg) {
4609 logmsg = strdup(hle->logmsg);
4610 if (logmsg == NULL) {
4611 err = got_error_from_errno("strdup");
4612 goto done;
4614 trim_logmsg(logmsg, 42);
4615 } else {
4616 err = get_short_logmsg(&logmsg, 42, commit);
4617 if (err)
4618 goto done;
4621 switch (hle->cmd->code) {
4622 case GOT_HISTEDIT_PICK:
4623 case GOT_HISTEDIT_EDIT:
4624 printf("%s -> %s: %s\n", old_id_str,
4625 new_id_str ? new_id_str : "no-op change", logmsg);
4626 break;
4627 case GOT_HISTEDIT_DROP:
4628 case GOT_HISTEDIT_FOLD:
4629 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4630 logmsg);
4631 break;
4632 default:
4633 break;
4636 done:
4637 free(old_id_str);
4638 free(new_id_str);
4639 return err;
4642 static const struct got_error *
4643 histedit_commit(struct got_pathlist_head *merged_paths,
4644 struct got_worktree *worktree, struct got_fileindex *fileindex,
4645 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4646 struct got_repository *repo)
4648 const struct got_error *err;
4649 struct got_commit_object *commit;
4650 struct got_object_id *new_commit_id;
4652 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4653 && hle->logmsg == NULL) {
4654 err = histedit_edit_logmsg(hle, repo);
4655 if (err)
4656 return err;
4659 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4660 if (err)
4661 return err;
4663 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4664 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4665 hle->logmsg, repo);
4666 if (err) {
4667 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4668 goto done;
4669 err = show_histedit_progress(commit, hle, NULL);
4670 } else {
4671 err = show_histedit_progress(commit, hle, new_commit_id);
4672 free(new_commit_id);
4674 done:
4675 got_object_commit_close(commit);
4676 return err;
4679 static const struct got_error *
4680 histedit_skip_commit(struct got_histedit_list_entry *hle,
4681 struct got_worktree *worktree, struct got_repository *repo)
4683 const struct got_error *error;
4684 struct got_commit_object *commit;
4686 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4687 repo);
4688 if (error)
4689 return error;
4691 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4692 if (error)
4693 return error;
4695 error = show_histedit_progress(commit, hle, NULL);
4696 got_object_commit_close(commit);
4697 return error;
4700 static const struct got_error *
4701 histedit_check_script(struct got_histedit_list *histedit_cmds,
4702 struct got_object_id_queue *commits, struct got_repository *repo)
4704 const struct got_error *err = NULL;
4705 struct got_object_qid *qid;
4706 struct got_histedit_list_entry *hle;
4707 static char msg[80];
4708 char *id_str;
4710 if (TAILQ_EMPTY(histedit_cmds))
4711 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4712 "histedit script contains no commands");
4714 SIMPLEQ_FOREACH(qid, commits, entry) {
4715 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4716 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4717 break;
4719 if (hle == NULL) {
4720 err = got_object_id_str(&id_str, qid->id);
4721 if (err)
4722 return err;
4723 snprintf(msg, sizeof(msg),
4724 "commit %s missing from histedit script", id_str);
4725 free(id_str);
4726 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4730 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4731 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4732 "last commit in histedit script cannot be folded");
4734 return NULL;
4737 static const struct got_error *
4738 cmd_histedit(int argc, char *argv[])
4740 const struct got_error *error = NULL;
4741 struct got_worktree *worktree = NULL;
4742 struct got_fileindex *fileindex = NULL;
4743 struct got_repository *repo = NULL;
4744 char *cwd = NULL;
4745 struct got_reference *branch = NULL;
4746 struct got_reference *tmp_branch = NULL;
4747 struct got_object_id *resume_commit_id = NULL;
4748 struct got_object_id *base_commit_id = NULL;
4749 struct got_object_id *head_commit_id = NULL;
4750 struct got_commit_object *commit = NULL;
4751 int ch, rebase_in_progress = 0;
4752 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4753 const char *edit_script_path = NULL;
4754 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4755 struct got_object_id_queue commits;
4756 struct got_pathlist_head merged_paths;
4757 const struct got_object_id_queue *parent_ids;
4758 struct got_object_qid *pid;
4759 struct got_histedit_list histedit_cmds;
4760 struct got_histedit_list_entry *hle;
4762 SIMPLEQ_INIT(&commits);
4763 TAILQ_INIT(&histedit_cmds);
4764 TAILQ_INIT(&merged_paths);
4766 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4767 switch (ch) {
4768 case 'a':
4769 abort_edit = 1;
4770 break;
4771 case 'c':
4772 continue_edit = 1;
4773 break;
4774 case 'F':
4775 edit_script_path = optarg;
4776 break;
4777 default:
4778 usage_histedit();
4779 /* NOTREACHED */
4783 argc -= optind;
4784 argv += optind;
4786 #ifndef PROFILE
4787 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4788 "unveil", NULL) == -1)
4789 err(1, "pledge");
4790 #endif
4791 if (abort_edit && continue_edit)
4792 usage_histedit();
4793 if (argc != 0)
4794 usage_histedit();
4797 * This command cannot apply unveil(2) in all cases because the
4798 * user may choose to run an editor to edit the histedit script
4799 * and to edit individual commit log messages.
4800 * unveil(2) traverses exec(2); if an editor is used we have to
4801 * apply unveil after edit script and log messages have been written.
4802 * XXX TODO: Make use of unveil(2) where possible.
4805 cwd = getcwd(NULL, 0);
4806 if (cwd == NULL) {
4807 error = got_error_from_errno("getcwd");
4808 goto done;
4810 error = got_worktree_open(&worktree, cwd);
4811 if (error)
4812 goto done;
4814 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4815 if (error != NULL)
4816 goto done;
4818 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4819 if (error)
4820 goto done;
4821 if (rebase_in_progress) {
4822 error = got_error(GOT_ERR_REBASING);
4823 goto done;
4826 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4827 if (error)
4828 goto done;
4830 if (edit_in_progress && abort_edit) {
4831 int did_something;
4832 error = got_worktree_histedit_continue(&resume_commit_id,
4833 &tmp_branch, &branch, &base_commit_id, &fileindex,
4834 worktree, repo);
4835 if (error)
4836 goto done;
4837 printf("Switching work tree to %s\n",
4838 got_ref_get_symref_target(branch));
4839 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4840 branch, base_commit_id, update_progress, &did_something);
4841 if (error)
4842 goto done;
4843 printf("Histedit of %s aborted\n",
4844 got_ref_get_symref_target(branch));
4845 goto done; /* nothing else to do */
4846 } else if (abort_edit) {
4847 error = got_error(GOT_ERR_NOT_HISTEDIT);
4848 goto done;
4851 if (continue_edit) {
4852 char *path;
4854 if (!edit_in_progress) {
4855 error = got_error(GOT_ERR_NOT_HISTEDIT);
4856 goto done;
4859 error = got_worktree_get_histedit_list_path(&path, worktree);
4860 if (error)
4861 goto done;
4863 error = histedit_load_list(&histedit_cmds, path, repo);
4864 free(path);
4865 if (error)
4866 goto done;
4868 error = got_worktree_histedit_continue(&resume_commit_id,
4869 &tmp_branch, &branch, &base_commit_id, &fileindex,
4870 worktree, repo);
4871 if (error)
4872 goto done;
4874 error = got_ref_resolve(&head_commit_id, repo, branch);
4875 if (error)
4876 goto done;
4878 error = got_object_open_as_commit(&commit, repo,
4879 head_commit_id);
4880 if (error)
4881 goto done;
4882 parent_ids = got_object_commit_get_parent_ids(commit);
4883 pid = SIMPLEQ_FIRST(parent_ids);
4884 if (pid == NULL) {
4885 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4886 goto done;
4888 error = collect_commits(&commits, head_commit_id, pid->id,
4889 base_commit_id, got_worktree_get_path_prefix(worktree),
4890 GOT_ERR_HISTEDIT_PATH, repo);
4891 got_object_commit_close(commit);
4892 commit = NULL;
4893 if (error)
4894 goto done;
4895 } else {
4896 if (edit_in_progress) {
4897 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4898 goto done;
4901 error = got_ref_open(&branch, repo,
4902 got_worktree_get_head_ref_name(worktree), 0);
4903 if (error != NULL)
4904 goto done;
4906 error = got_ref_resolve(&head_commit_id, repo, branch);
4907 if (error)
4908 goto done;
4910 error = got_object_open_as_commit(&commit, repo,
4911 head_commit_id);
4912 if (error)
4913 goto done;
4914 parent_ids = got_object_commit_get_parent_ids(commit);
4915 pid = SIMPLEQ_FIRST(parent_ids);
4916 if (pid == NULL) {
4917 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4918 goto done;
4920 error = collect_commits(&commits, head_commit_id, pid->id,
4921 got_worktree_get_base_commit_id(worktree),
4922 got_worktree_get_path_prefix(worktree),
4923 GOT_ERR_HISTEDIT_PATH, repo);
4924 got_object_commit_close(commit);
4925 commit = NULL;
4926 if (error)
4927 goto done;
4929 if (edit_script_path) {
4930 error = histedit_load_list(&histedit_cmds,
4931 edit_script_path, repo);
4932 if (error)
4933 goto done;
4934 } else {
4935 error = histedit_edit_script(&histedit_cmds, &commits,
4936 repo);
4937 if (error)
4938 goto done;
4942 error = histedit_save_list(&histedit_cmds, worktree,
4943 repo);
4944 if (error)
4945 goto done;
4947 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4948 &base_commit_id, &fileindex, worktree, repo);
4949 if (error)
4950 goto done;
4954 error = histedit_check_script(&histedit_cmds, &commits, repo);
4955 if (error)
4956 goto done;
4958 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4959 if (resume_commit_id) {
4960 if (got_object_id_cmp(hle->commit_id,
4961 resume_commit_id) != 0)
4962 continue;
4964 resume_commit_id = NULL;
4965 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4966 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4967 error = histedit_skip_commit(hle, worktree,
4968 repo);
4969 } else {
4970 error = histedit_commit(NULL, worktree,
4971 fileindex, tmp_branch, hle, repo);
4973 if (error)
4974 goto done;
4975 continue;
4978 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4979 error = histedit_skip_commit(hle, worktree, repo);
4980 if (error)
4981 goto done;
4982 continue;
4985 error = got_object_open_as_commit(&commit, repo,
4986 hle->commit_id);
4987 if (error)
4988 goto done;
4989 parent_ids = got_object_commit_get_parent_ids(commit);
4990 pid = SIMPLEQ_FIRST(parent_ids);
4992 error = got_worktree_histedit_merge_files(&merged_paths,
4993 worktree, fileindex, pid->id, hle->commit_id, repo,
4994 rebase_progress, &rebase_status, check_cancelled, NULL);
4995 if (error)
4996 goto done;
4997 got_object_commit_close(commit);
4998 commit = NULL;
5000 if (rebase_status == GOT_STATUS_CONFLICT) {
5001 got_worktree_rebase_pathlist_free(&merged_paths);
5002 break;
5005 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5006 char *id_str;
5007 error = got_object_id_str(&id_str, hle->commit_id);
5008 if (error)
5009 goto done;
5010 printf("Stopping histedit for amending commit %s\n",
5011 id_str);
5012 free(id_str);
5013 got_worktree_rebase_pathlist_free(&merged_paths);
5014 error = got_worktree_histedit_postpone(worktree,
5015 fileindex);
5016 goto done;
5019 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5020 error = histedit_skip_commit(hle, worktree, repo);
5021 if (error)
5022 goto done;
5023 continue;
5026 error = histedit_commit(&merged_paths, worktree, fileindex,
5027 tmp_branch, hle, repo);
5028 got_worktree_rebase_pathlist_free(&merged_paths);
5029 if (error)
5030 goto done;
5033 if (rebase_status == GOT_STATUS_CONFLICT) {
5034 error = got_worktree_histedit_postpone(worktree, fileindex);
5035 if (error)
5036 goto done;
5037 error = got_error_msg(GOT_ERR_CONFLICTS,
5038 "conflicts must be resolved before rebasing can continue");
5039 } else
5040 error = histedit_complete(worktree, fileindex, tmp_branch,
5041 branch, repo);
5042 done:
5043 got_object_id_queue_free(&commits);
5044 free(head_commit_id);
5045 free(base_commit_id);
5046 free(resume_commit_id);
5047 if (commit)
5048 got_object_commit_close(commit);
5049 if (branch)
5050 got_ref_close(branch);
5051 if (tmp_branch)
5052 got_ref_close(tmp_branch);
5053 if (worktree)
5054 got_worktree_close(worktree);
5055 if (repo)
5056 got_repo_close(repo);
5057 return error;