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_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
58 static void
59 catch_sigint(int signo)
60 {
61 sigint_received = 1;
62 }
64 static void
65 catch_sigpipe(int signo)
66 {
67 sigpipe_received = 1;
68 }
71 struct got_cmd {
72 const char *cmd_name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
76 };
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
98 __dead static void usage_stage(void);
99 __dead static void usage_unstage(void);
101 static const struct got_error* cmd_init(int, char *[]);
102 static const struct got_error* cmd_import(int, char *[]);
103 static const struct got_error* cmd_checkout(int, char *[]);
104 static const struct got_error* cmd_update(int, char *[]);
105 static const struct got_error* cmd_log(int, char *[]);
106 static const struct got_error* cmd_diff(int, char *[]);
107 static const struct got_error* cmd_blame(int, char *[]);
108 static const struct got_error* cmd_tree(int, char *[]);
109 static const struct got_error* cmd_status(int, char *[]);
110 static const struct got_error* cmd_ref(int, char *[]);
111 static const struct got_error* cmd_branch(int, char *[]);
112 static const struct got_error* cmd_add(int, char *[]);
113 static const struct got_error* cmd_remove(int, char *[]);
114 static const struct got_error* cmd_revert(int, char *[]);
115 static const struct got_error* cmd_commit(int, char *[]);
116 static const struct got_error* cmd_cherrypick(int, char *[]);
117 static const struct got_error* cmd_backout(int, char *[]);
118 static const struct got_error* cmd_rebase(int, char *[]);
119 static const struct got_error* cmd_histedit(int, char *[]);
120 static const struct got_error* cmd_stage(int, char *[]);
121 static const struct got_error* cmd_unstage(int, char *[]);
123 static struct got_cmd got_commands[] = {
124 { "init", cmd_init, usage_init, "in" },
125 { "import", cmd_import, usage_import, "im" },
126 { "checkout", cmd_checkout, usage_checkout, "co" },
127 { "update", cmd_update, usage_update, "up" },
128 { "log", cmd_log, usage_log, "" },
129 { "diff", cmd_diff, usage_diff, "di" },
130 { "blame", cmd_blame, usage_blame, "bl" },
131 { "tree", cmd_tree, usage_tree, "tr" },
132 { "status", cmd_status, usage_status, "st" },
133 { "ref", cmd_ref, usage_ref, "" },
134 { "branch", cmd_branch, usage_branch, "br" },
135 { "add", cmd_add, usage_add, "" },
136 { "remove", cmd_remove, usage_remove, "rm" },
137 { "revert", cmd_revert, usage_revert, "rv" },
138 { "commit", cmd_commit, usage_commit, "ci" },
139 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 { "backout", cmd_backout, usage_backout, "bo" },
141 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 { "histedit", cmd_histedit, usage_histedit, "he" },
143 { "stage", cmd_stage, usage_stage, "sg" },
144 { "unstage", cmd_unstage, usage_unstage, "ug" },
145 };
147 static void
148 list_commands(void)
150 int i;
152 fprintf(stderr, "commands:");
153 for (i = 0; i < nitems(got_commands); i++) {
154 struct got_cmd *cmd = &got_commands[i];
155 fprintf(stderr, " %s", cmd->cmd_name);
157 fputc('\n', stderr);
160 int
161 main(int argc, char *argv[])
163 struct got_cmd *cmd;
164 unsigned int i;
165 int ch;
166 int hflag = 0, Vflag = 0;
168 setlocale(LC_CTYPE, "");
170 while ((ch = getopt(argc, argv, "hV")) != -1) {
171 switch (ch) {
172 case 'h':
173 hflag = 1;
174 break;
175 case 'V':
176 Vflag = 1;
177 break;
178 default:
179 usage(hflag);
180 /* NOTREACHED */
184 argc -= optind;
185 argv += optind;
186 optind = 0;
188 if (Vflag) {
189 got_version_print_str();
190 return 1;
193 if (argc <= 0)
194 usage(hflag);
196 signal(SIGINT, catch_sigint);
197 signal(SIGPIPE, catch_sigpipe);
199 for (i = 0; i < nitems(got_commands); i++) {
200 const struct got_error *error;
202 cmd = &got_commands[i];
204 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 strcmp(cmd->cmd_alias, argv[0]) != 0)
206 continue;
208 if (hflag)
209 got_commands[i].cmd_usage();
211 error = got_commands[i].cmd_main(argc, argv);
212 if (error && !(sigint_received || sigpipe_received)) {
213 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
214 return 1;
217 return 0;
220 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
221 list_commands();
222 return 1;
225 __dead static void
226 usage(int hflag)
228 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
229 getprogname());
230 if (hflag)
231 list_commands();
232 exit(1);
235 static const struct got_error *
236 get_editor(char **abspath)
238 const struct got_error *err = NULL;
239 const char *editor;
241 editor = getenv("VISUAL");
242 if (editor == NULL)
243 editor = getenv("EDITOR");
245 if (editor) {
246 err = got_path_find_prog(abspath, editor);
247 if (err)
248 return err;
251 if (*abspath == NULL) {
252 *abspath = strdup("/bin/ed");
253 if (*abspath == NULL)
254 return got_error_from_errno("strdup");
257 return NULL;
260 static const struct got_error *
261 apply_unveil(const char *repo_path, int repo_read_only,
262 const char *worktree_path)
264 const struct got_error *err;
266 #ifdef PROFILE
267 if (unveil("gmon.out", "rwc") != 0)
268 return got_error_from_errno2("unveil", "gmon.out");
269 #endif
270 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
271 return got_error_from_errno2("unveil", repo_path);
273 if (worktree_path && unveil(worktree_path, "rwc") != 0)
274 return got_error_from_errno2("unveil", worktree_path);
276 if (unveil("/tmp", "rwc") != 0)
277 return got_error_from_errno2("unveil", "/tmp");
279 err = got_privsep_unveil_exec_helpers();
280 if (err != NULL)
281 return err;
283 if (unveil(NULL, NULL) != 0)
284 return got_error_from_errno("unveil");
286 return NULL;
289 __dead static void
290 usage_init(void)
292 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
293 exit(1);
296 static const struct got_error *
297 cmd_init(int argc, char *argv[])
299 const struct got_error *error = NULL;
300 char *repo_path = NULL;
301 int ch;
303 while ((ch = getopt(argc, argv, "")) != -1) {
304 switch (ch) {
305 default:
306 usage_init();
307 /* NOTREACHED */
311 argc -= optind;
312 argv += optind;
314 #ifndef PROFILE
315 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
316 err(1, "pledge");
317 #endif
318 if (argc != 1)
319 usage_init();
321 repo_path = strdup(argv[0]);
322 if (repo_path == NULL)
323 return got_error_from_errno("strdup");
325 got_path_strip_trailing_slashes(repo_path);
327 error = got_path_mkdir(repo_path);
328 if (error &&
329 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
330 goto done;
332 error = apply_unveil(repo_path, 0, NULL);
333 if (error)
334 goto done;
336 error = got_repo_init(repo_path);
337 if (error != NULL)
338 goto done;
340 done:
341 free(repo_path);
342 return error;
345 __dead static void
346 usage_import(void)
348 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
349 "[-r repository-path] [-I pattern] path\n", getprogname());
350 exit(1);
353 int
354 spawn_editor(const char *editor, const char *file)
356 pid_t pid;
357 sig_t sighup, sigint, sigquit;
358 int st = -1;
360 sighup = signal(SIGHUP, SIG_IGN);
361 sigint = signal(SIGINT, SIG_IGN);
362 sigquit = signal(SIGQUIT, SIG_IGN);
364 switch (pid = fork()) {
365 case -1:
366 goto doneediting;
367 case 0:
368 execl(editor, editor, file, (char *)NULL);
369 _exit(127);
372 while (waitpid(pid, &st, 0) == -1)
373 if (errno != EINTR)
374 break;
376 doneediting:
377 (void)signal(SIGHUP, sighup);
378 (void)signal(SIGINT, sigint);
379 (void)signal(SIGQUIT, sigquit);
381 if (!WIFEXITED(st)) {
382 errno = EINTR;
383 return -1;
386 return WEXITSTATUS(st);
389 static const struct got_error *
390 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
391 const char *initial_content)
393 const struct got_error *err = NULL;
394 char buf[1024];
395 struct stat st, st2;
396 FILE *fp;
397 int content_changed = 0;
398 size_t len;
400 *logmsg = NULL;
402 if (stat(logmsg_path, &st) == -1)
403 return got_error_from_errno2("stat", logmsg_path);
405 if (spawn_editor(editor, logmsg_path) == -1)
406 return got_error_from_errno("failed spawning editor");
408 if (stat(logmsg_path, &st2) == -1)
409 return got_error_from_errno("stat");
411 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
412 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
413 "no changes made to commit message, aborting");
415 *logmsg = malloc(st2.st_size + 1);
416 if (*logmsg == NULL)
417 return got_error_from_errno("malloc");
418 (*logmsg)[0] = '\0';
419 len = 0;
421 fp = fopen(logmsg_path, "r");
422 if (fp == NULL) {
423 err = got_error_from_errno("fopen");
424 goto done;
426 while (fgets(buf, sizeof(buf), fp) != NULL) {
427 if (!content_changed && strcmp(buf, initial_content) != 0)
428 content_changed = 1;
429 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
430 continue; /* remove comments and leading empty lines */
431 len = strlcat(*logmsg, buf, st2.st_size);
433 fclose(fp);
435 while (len > 0 && (*logmsg)[len - 1] == '\n') {
436 (*logmsg)[len - 1] = '\0';
437 len--;
440 if (len == 0 || !content_changed)
441 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
442 "commit message cannot be empty, aborting");
443 done:
444 if (err) {
445 free(*logmsg);
446 *logmsg = NULL;
448 return err;
451 static const struct got_error *
452 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
453 const char *branch_name)
455 char *initial_content = NULL, *logmsg_path = NULL;
456 const struct got_error *err = NULL;
457 int fd;
459 if (asprintf(&initial_content,
460 "\n# %s to be imported to branch %s\n", path_dir,
461 branch_name) == -1)
462 return got_error_from_errno("asprintf");
464 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
465 if (err)
466 goto done;
468 dprintf(fd, initial_content);
469 close(fd);
471 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
472 done:
473 free(initial_content);
474 free(logmsg_path);
475 return err;
478 static const struct got_error *
479 import_progress(void *arg, const char *path)
481 printf("A %s\n", path);
482 return NULL;
485 static const struct got_error *
486 cmd_import(int argc, char *argv[])
488 const struct got_error *error = NULL;
489 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
490 char *editor = NULL;
491 const char *got_author = getenv("GOT_AUTHOR");
492 const char *branch_name = "master";
493 char *refname = NULL, *id_str = NULL;
494 struct got_repository *repo = NULL;
495 struct got_reference *branch_ref = NULL, *head_ref = NULL;
496 struct got_object_id *new_commit_id = NULL;
497 int ch;
498 struct got_pathlist_head ignores;
499 struct got_pathlist_entry *pe;
501 TAILQ_INIT(&ignores);
503 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
504 switch (ch) {
505 case 'b':
506 branch_name = optarg;
507 break;
508 case 'm':
509 logmsg = strdup(optarg);
510 if (logmsg == NULL) {
511 error = got_error_from_errno("strdup");
512 goto done;
514 break;
515 case 'r':
516 repo_path = realpath(optarg, NULL);
517 if (repo_path == NULL) {
518 error = got_error_from_errno("realpath");
519 goto done;
521 break;
522 case 'I':
523 if (optarg[0] == '\0')
524 break;
525 error = got_pathlist_insert(&pe, &ignores, optarg,
526 NULL);
527 if (error)
528 goto done;
529 break;
530 default:
531 usage_init();
532 /* NOTREACHED */
536 argc -= optind;
537 argv += optind;
539 #ifndef PROFILE
540 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
541 NULL) == -1)
542 err(1, "pledge");
543 #endif
544 if (argc != 1)
545 usage_import();
547 if (got_author == NULL) {
548 /* TODO: Look current user up in password database */
549 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
550 goto done;
553 if (repo_path == NULL) {
554 repo_path = getcwd(NULL, 0);
555 if (repo_path == NULL)
556 return got_error_from_errno("getcwd");
558 got_path_strip_trailing_slashes(repo_path);
559 error = got_repo_open(&repo, repo_path);
560 if (error)
561 goto done;
563 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
564 error = got_error_from_errno("asprintf");
565 goto done;
568 error = got_ref_open(&branch_ref, repo, refname, 0);
569 if (error) {
570 if (error->code != GOT_ERR_NOT_REF)
571 goto done;
572 } else {
573 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
574 "import target branch already exists");
575 goto done;
578 path_dir = realpath(argv[0], NULL);
579 if (path_dir == NULL) {
580 error = got_error_from_errno("realpath");
581 goto done;
583 got_path_strip_trailing_slashes(path_dir);
585 /*
586 * unveil(2) traverses exec(2); if an editor is used we have
587 * to apply unveil after the log message has been written.
588 */
589 if (logmsg == NULL || strlen(logmsg) == 0) {
590 error = get_editor(&editor);
591 if (error)
592 goto done;
593 error = collect_import_msg(&logmsg, editor, path_dir, refname);
594 if (error)
595 goto done;
598 if (unveil(path_dir, "r") != 0)
599 return got_error_from_errno2("unveil", path_dir);
601 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
602 if (error)
603 goto done;
605 error = got_repo_import(&new_commit_id, path_dir, logmsg,
606 got_author, &ignores, repo, import_progress, NULL);
607 if (error)
608 goto done;
610 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
611 if (error)
612 goto done;
614 error = got_ref_write(branch_ref, repo);
615 if (error)
616 goto done;
618 error = got_object_id_str(&id_str, new_commit_id);
619 if (error)
620 goto done;
622 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
623 if (error) {
624 if (error->code != GOT_ERR_NOT_REF)
625 goto done;
627 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
628 branch_ref);
629 if (error)
630 goto done;
632 error = got_ref_write(head_ref, repo);
633 if (error)
634 goto done;
637 printf("Created branch %s with commit %s\n",
638 got_ref_get_name(branch_ref), id_str);
639 done:
640 free(repo_path);
641 free(editor);
642 free(refname);
643 free(new_commit_id);
644 free(id_str);
645 if (branch_ref)
646 got_ref_close(branch_ref);
647 if (head_ref)
648 got_ref_close(head_ref);
649 return error;
652 __dead static void
653 usage_checkout(void)
655 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
656 "[-p prefix] repository-path [worktree-path]\n", getprogname());
657 exit(1);
660 static const struct got_error *
661 checkout_progress(void *arg, unsigned char status, const char *path)
663 char *worktree_path = arg;
665 /* Base commit bump happens silently. */
666 if (status == GOT_STATUS_BUMP_BASE)
667 return NULL;
669 while (path[0] == '/')
670 path++;
672 printf("%c %s/%s\n", status, worktree_path, path);
673 return NULL;
676 static const struct got_error *
677 check_cancelled(void *arg)
679 if (sigint_received || sigpipe_received)
680 return got_error(GOT_ERR_CANCELLED);
681 return NULL;
684 static const struct got_error *
685 check_linear_ancestry(struct got_object_id *commit_id,
686 struct got_object_id *base_commit_id, struct got_repository *repo)
688 const struct got_error *err = NULL;
689 struct got_object_id *yca_id;
691 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
692 commit_id, base_commit_id, repo);
693 if (err)
694 return err;
696 if (yca_id == NULL)
697 return got_error(GOT_ERR_ANCESTRY);
699 /*
700 * Require a straight line of history between the target commit
701 * and the work tree's base commit.
703 * Non-linear situations such as this require a rebase:
705 * (commit) D F (base_commit)
706 * \ /
707 * C E
708 * \ /
709 * B (yca)
710 * |
711 * A
713 * 'got update' only handles linear cases:
714 * Update forwards in time: A (base/yca) - B - C - D (commit)
715 * Update backwards in time: D (base) - C - B - A (commit/yca)
716 */
717 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
718 got_object_id_cmp(base_commit_id, yca_id) != 0)
719 return got_error(GOT_ERR_ANCESTRY);
721 free(yca_id);
722 return NULL;
725 static const struct got_error *
726 check_same_branch(struct got_object_id *commit_id,
727 struct got_reference *head_ref, struct got_object_id *yca_id,
728 struct got_repository *repo)
730 const struct got_error *err = NULL;
731 struct got_commit_graph *graph = NULL;
732 struct got_object_id *head_commit_id = NULL;
733 int is_same_branch = 0;
735 err = got_ref_resolve(&head_commit_id, repo, head_ref);
736 if (err)
737 goto done;
739 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
740 is_same_branch = 1;
741 goto done;
743 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
744 is_same_branch = 1;
745 goto done;
748 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
749 if (err)
750 goto done;
752 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
753 if (err)
754 goto done;
756 for (;;) {
757 struct got_object_id *id;
758 err = got_commit_graph_iter_next(&id, graph);
759 if (err) {
760 if (err->code == GOT_ERR_ITER_COMPLETED) {
761 err = NULL;
762 break;
763 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
764 break;
765 err = got_commit_graph_fetch_commits(graph, 1,
766 repo);
767 if (err)
768 break;
771 if (id) {
772 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
773 break;
774 if (got_object_id_cmp(id, commit_id) == 0) {
775 is_same_branch = 1;
776 break;
780 done:
781 if (graph)
782 got_commit_graph_close(graph);
783 free(head_commit_id);
784 if (!err && !is_same_branch)
785 err = got_error(GOT_ERR_ANCESTRY);
786 return err;
789 static const struct got_error *
790 resolve_commit_arg(struct got_object_id **commit_id,
791 const char *commit_id_arg, struct got_repository *repo)
793 const struct got_error *err;
794 struct got_reference *ref;
796 err = got_ref_open(&ref, repo, commit_id_arg, 0);
797 if (err == NULL) {
798 err = got_ref_resolve(commit_id, repo, ref);
799 got_ref_close(ref);
800 } else {
801 if (err->code != GOT_ERR_NOT_REF)
802 return err;
803 err = got_repo_match_object_id_prefix(commit_id,
804 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
806 return err;
809 static const struct got_error *
810 cmd_checkout(int argc, char *argv[])
812 const struct got_error *error = NULL;
813 struct got_repository *repo = NULL;
814 struct got_reference *head_ref = NULL;
815 struct got_worktree *worktree = NULL;
816 char *repo_path = NULL;
817 char *worktree_path = NULL;
818 const char *path_prefix = "";
819 const char *branch_name = GOT_REF_HEAD;
820 char *commit_id_str = NULL;
821 int ch, same_path_prefix;
822 struct got_pathlist_head paths;
824 TAILQ_INIT(&paths);
826 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
827 switch (ch) {
828 case 'b':
829 branch_name = optarg;
830 break;
831 case 'c':
832 commit_id_str = strdup(optarg);
833 if (commit_id_str == NULL)
834 return got_error_from_errno("strdup");
835 break;
836 case 'p':
837 path_prefix = optarg;
838 break;
839 default:
840 usage_checkout();
841 /* NOTREACHED */
845 argc -= optind;
846 argv += optind;
848 #ifndef PROFILE
849 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
850 "unveil", NULL) == -1)
851 err(1, "pledge");
852 #endif
853 if (argc == 1) {
854 char *cwd, *base, *dotgit;
855 repo_path = realpath(argv[0], NULL);
856 if (repo_path == NULL)
857 return got_error_from_errno2("realpath", argv[0]);
858 cwd = getcwd(NULL, 0);
859 if (cwd == NULL) {
860 error = got_error_from_errno("getcwd");
861 goto done;
863 if (path_prefix[0]) {
864 base = basename(path_prefix);
865 if (base == NULL) {
866 error = got_error_from_errno2("basename",
867 path_prefix);
868 goto done;
870 } else {
871 base = basename(repo_path);
872 if (base == NULL) {
873 error = got_error_from_errno2("basename",
874 repo_path);
875 goto done;
878 dotgit = strstr(base, ".git");
879 if (dotgit)
880 *dotgit = '\0';
881 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
882 error = got_error_from_errno("asprintf");
883 free(cwd);
884 goto done;
886 free(cwd);
887 } else if (argc == 2) {
888 repo_path = realpath(argv[0], NULL);
889 if (repo_path == NULL) {
890 error = got_error_from_errno2("realpath", argv[0]);
891 goto done;
893 worktree_path = realpath(argv[1], NULL);
894 if (worktree_path == NULL) {
895 if (errno != ENOENT) {
896 error = got_error_from_errno2("realpath",
897 argv[1]);
898 goto done;
900 worktree_path = strdup(argv[1]);
901 if (worktree_path == NULL) {
902 error = got_error_from_errno("strdup");
903 goto done;
906 } else
907 usage_checkout();
909 got_path_strip_trailing_slashes(repo_path);
910 got_path_strip_trailing_slashes(worktree_path);
912 error = got_repo_open(&repo, repo_path);
913 if (error != NULL)
914 goto done;
916 /* Pre-create work tree path for unveil(2) */
917 error = got_path_mkdir(worktree_path);
918 if (error) {
919 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
920 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
921 goto done;
922 if (!got_path_dir_is_empty(worktree_path)) {
923 error = got_error_path(worktree_path,
924 GOT_ERR_DIR_NOT_EMPTY);
925 goto done;
929 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
930 if (error)
931 goto done;
933 error = got_ref_open(&head_ref, repo, branch_name, 0);
934 if (error != NULL)
935 goto done;
937 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
938 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
939 goto done;
941 error = got_worktree_open(&worktree, worktree_path);
942 if (error != NULL)
943 goto done;
945 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
946 path_prefix);
947 if (error != NULL)
948 goto done;
949 if (!same_path_prefix) {
950 error = got_error(GOT_ERR_PATH_PREFIX);
951 goto done;
954 if (commit_id_str) {
955 struct got_object_id *commit_id;
956 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
957 if (error)
958 goto done;
959 error = check_linear_ancestry(commit_id,
960 got_worktree_get_base_commit_id(worktree), repo);
961 if (error != NULL) {
962 free(commit_id);
963 goto done;
965 error = check_same_branch(commit_id, head_ref, NULL, repo);
966 if (error)
967 goto done;
968 error = got_worktree_set_base_commit_id(worktree, repo,
969 commit_id);
970 free(commit_id);
971 if (error)
972 goto done;
975 error = got_pathlist_append(&paths, "", NULL);
976 if (error)
977 goto done;
978 error = got_worktree_checkout_files(worktree, &paths, repo,
979 checkout_progress, worktree_path, check_cancelled, NULL);
980 if (error != NULL)
981 goto done;
983 printf("Now shut up and hack\n");
985 done:
986 got_pathlist_free(&paths);
987 free(commit_id_str);
988 free(repo_path);
989 free(worktree_path);
990 return error;
993 __dead static void
994 usage_update(void)
996 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
997 getprogname());
998 exit(1);
1001 static const struct got_error *
1002 update_progress(void *arg, unsigned char status, const char *path)
1004 int *did_something = arg;
1006 if (status == GOT_STATUS_EXISTS)
1007 return NULL;
1009 *did_something = 1;
1011 /* Base commit bump happens silently. */
1012 if (status == GOT_STATUS_BUMP_BASE)
1013 return NULL;
1015 while (path[0] == '/')
1016 path++;
1017 printf("%c %s\n", status, path);
1018 return NULL;
1021 static const struct got_error *
1022 switch_head_ref(struct got_reference *head_ref,
1023 struct got_object_id *commit_id, struct got_worktree *worktree,
1024 struct got_repository *repo)
1026 const struct got_error *err = NULL;
1027 char *base_id_str;
1028 int ref_has_moved = 0;
1030 /* Trivial case: switching between two different references. */
1031 if (strcmp(got_ref_get_name(head_ref),
1032 got_worktree_get_head_ref_name(worktree)) != 0) {
1033 printf("Switching work tree from %s to %s\n",
1034 got_worktree_get_head_ref_name(worktree),
1035 got_ref_get_name(head_ref));
1036 return got_worktree_set_head_ref(worktree, head_ref);
1039 err = check_linear_ancestry(commit_id,
1040 got_worktree_get_base_commit_id(worktree), repo);
1041 if (err) {
1042 if (err->code != GOT_ERR_ANCESTRY)
1043 return err;
1044 ref_has_moved = 1;
1046 if (!ref_has_moved)
1047 return NULL;
1049 /* Switching to a rebased branch with the same reference name. */
1050 err = got_object_id_str(&base_id_str,
1051 got_worktree_get_base_commit_id(worktree));
1052 if (err)
1053 return err;
1054 printf("Reference %s now points at a different branch\n",
1055 got_worktree_get_head_ref_name(worktree));
1056 printf("Switching work tree from %s to %s\n", base_id_str,
1057 got_worktree_get_head_ref_name(worktree));
1058 return NULL;
1061 static const struct got_error *
1062 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1064 const struct got_error *err;
1065 int in_progress;
1067 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1068 if (err)
1069 return err;
1070 if (in_progress)
1071 return got_error(GOT_ERR_REBASING);
1073 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1074 if (err)
1075 return err;
1076 if (in_progress)
1077 return got_error(GOT_ERR_HISTEDIT_BUSY);
1079 return NULL;
1082 static const struct got_error *
1083 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1084 char *argv[], struct got_worktree *worktree)
1086 const struct got_error *err;
1087 char *path;
1088 int i;
1090 if (argc == 0) {
1091 path = strdup("");
1092 if (path == NULL)
1093 return got_error_from_errno("strdup");
1094 return got_pathlist_append(paths, path, NULL);
1097 for (i = 0; i < argc; i++) {
1098 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1099 if (err)
1100 break;
1101 err = got_pathlist_append(paths, path, NULL);
1102 if (err) {
1103 free(path);
1104 break;
1108 return err;
1111 static const struct got_error *
1112 cmd_update(int argc, char *argv[])
1114 const struct got_error *error = NULL;
1115 struct got_repository *repo = NULL;
1116 struct got_worktree *worktree = NULL;
1117 char *worktree_path = NULL;
1118 struct got_object_id *commit_id = NULL;
1119 char *commit_id_str = NULL;
1120 const char *branch_name = NULL;
1121 struct got_reference *head_ref = NULL;
1122 struct got_pathlist_head paths;
1123 struct got_pathlist_entry *pe;
1124 int ch, did_something = 0;
1126 TAILQ_INIT(&paths);
1128 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1129 switch (ch) {
1130 case 'b':
1131 branch_name = optarg;
1132 break;
1133 case 'c':
1134 commit_id_str = strdup(optarg);
1135 if (commit_id_str == NULL)
1136 return got_error_from_errno("strdup");
1137 break;
1138 default:
1139 usage_update();
1140 /* NOTREACHED */
1144 argc -= optind;
1145 argv += optind;
1147 #ifndef PROFILE
1148 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1149 "unveil", NULL) == -1)
1150 err(1, "pledge");
1151 #endif
1152 worktree_path = getcwd(NULL, 0);
1153 if (worktree_path == NULL) {
1154 error = got_error_from_errno("getcwd");
1155 goto done;
1157 error = got_worktree_open(&worktree, worktree_path);
1158 if (error)
1159 goto done;
1161 error = check_rebase_or_histedit_in_progress(worktree);
1162 if (error)
1163 goto done;
1165 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1166 if (error != NULL)
1167 goto done;
1169 error = apply_unveil(got_repo_get_path(repo), 0,
1170 got_worktree_get_root_path(worktree));
1171 if (error)
1172 goto done;
1174 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1175 if (error)
1176 goto done;
1178 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1179 got_worktree_get_head_ref_name(worktree), 0);
1180 if (error != NULL)
1181 goto done;
1182 if (commit_id_str == NULL) {
1183 error = got_ref_resolve(&commit_id, repo, head_ref);
1184 if (error != NULL)
1185 goto done;
1186 error = got_object_id_str(&commit_id_str, commit_id);
1187 if (error != NULL)
1188 goto done;
1189 } else {
1190 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1191 free(commit_id_str);
1192 commit_id_str = NULL;
1193 if (error)
1194 goto done;
1195 error = got_object_id_str(&commit_id_str, commit_id);
1196 if (error)
1197 goto done;
1200 if (branch_name) {
1201 struct got_object_id *head_commit_id;
1202 TAILQ_FOREACH(pe, &paths, entry) {
1203 if (pe->path_len == 0)
1204 continue;
1205 error = got_error_msg(GOT_ERR_BAD_PATH,
1206 "switching between branches requires that "
1207 "the entire work tree gets updated");
1208 goto done;
1210 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1211 if (error)
1212 goto done;
1213 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1214 free(head_commit_id);
1215 if (error != NULL)
1216 goto done;
1217 error = check_same_branch(commit_id, head_ref, NULL, repo);
1218 if (error)
1219 goto done;
1220 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1221 if (error)
1222 goto done;
1223 } else {
1224 error = check_linear_ancestry(commit_id,
1225 got_worktree_get_base_commit_id(worktree), repo);
1226 if (error != NULL) {
1227 if (error->code == GOT_ERR_ANCESTRY)
1228 error = got_error(GOT_ERR_BRANCH_MOVED);
1229 goto done;
1231 error = check_same_branch(commit_id, head_ref, NULL, repo);
1232 if (error)
1233 goto done;
1236 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1237 commit_id) != 0) {
1238 error = got_worktree_set_base_commit_id(worktree, repo,
1239 commit_id);
1240 if (error)
1241 goto done;
1244 error = got_worktree_checkout_files(worktree, &paths, repo,
1245 update_progress, &did_something, check_cancelled, NULL);
1246 if (error != NULL)
1247 goto done;
1249 if (did_something)
1250 printf("Updated to commit %s\n", commit_id_str);
1251 else
1252 printf("Already up-to-date\n");
1253 done:
1254 free(worktree_path);
1255 TAILQ_FOREACH(pe, &paths, entry)
1256 free((char *)pe->path);
1257 got_pathlist_free(&paths);
1258 free(commit_id);
1259 free(commit_id_str);
1260 return error;
1263 static const struct got_error *
1264 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1265 int diff_context, struct got_repository *repo)
1267 const struct got_error *err = NULL;
1268 struct got_tree_object *tree1 = NULL, *tree2;
1269 struct got_object_qid *qid;
1270 char *id_str1 = NULL, *id_str2;
1271 struct got_diff_blob_output_unidiff_arg arg;
1273 err = got_object_open_as_tree(&tree2, repo,
1274 got_object_commit_get_tree_id(commit));
1275 if (err)
1276 return err;
1278 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1279 if (qid != NULL) {
1280 struct got_commit_object *pcommit;
1282 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1283 if (err)
1284 return err;
1286 err = got_object_open_as_tree(&tree1, repo,
1287 got_object_commit_get_tree_id(pcommit));
1288 got_object_commit_close(pcommit);
1289 if (err)
1290 return err;
1292 err = got_object_id_str(&id_str1, qid->id);
1293 if (err)
1294 return err;
1297 err = got_object_id_str(&id_str2, id);
1298 if (err)
1299 goto done;
1301 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1302 arg.diff_context = diff_context;
1303 arg.outfile = stdout;
1304 err = got_diff_tree(tree1, tree2, "", "", repo,
1305 got_diff_blob_output_unidiff, &arg, 1);
1306 done:
1307 if (tree1)
1308 got_object_tree_close(tree1);
1309 got_object_tree_close(tree2);
1310 free(id_str1);
1311 free(id_str2);
1312 return err;
1315 static char *
1316 get_datestr(time_t *time, char *datebuf)
1318 char *p, *s = ctime_r(time, datebuf);
1319 p = strchr(s, '\n');
1320 if (p)
1321 *p = '\0';
1322 return s;
1325 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1327 static const struct got_error *
1328 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1329 struct got_repository *repo, int show_patch, int diff_context,
1330 struct got_reflist_head *refs)
1332 const struct got_error *err = NULL;
1333 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1334 char datebuf[26];
1335 time_t committer_time;
1336 const char *author, *committer;
1337 char *refs_str = NULL;
1338 struct got_reflist_entry *re;
1340 SIMPLEQ_FOREACH(re, refs, entry) {
1341 char *s;
1342 const char *name;
1343 if (got_object_id_cmp(re->id, id) != 0)
1344 continue;
1345 name = got_ref_get_name(re->ref);
1346 if (strcmp(name, GOT_REF_HEAD) == 0)
1347 continue;
1348 if (strncmp(name, "refs/", 5) == 0)
1349 name += 5;
1350 if (strncmp(name, "got/", 4) == 0)
1351 continue;
1352 if (strncmp(name, "heads/", 6) == 0)
1353 name += 6;
1354 if (strncmp(name, "remotes/", 8) == 0)
1355 name += 8;
1356 s = refs_str;
1357 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1358 name) == -1) {
1359 err = got_error_from_errno("asprintf");
1360 free(s);
1361 break;
1363 free(s);
1365 err = got_object_id_str(&id_str, id);
1366 if (err)
1367 return err;
1369 printf(GOT_COMMIT_SEP_STR);
1370 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1371 refs_str ? refs_str : "", refs_str ? ")" : "");
1372 free(id_str);
1373 id_str = NULL;
1374 free(refs_str);
1375 refs_str = NULL;
1376 printf("from: %s\n", got_object_commit_get_author(commit));
1377 committer_time = got_object_commit_get_committer_time(commit);
1378 datestr = get_datestr(&committer_time, datebuf);
1379 printf("date: %s UTC\n", datestr);
1380 author = got_object_commit_get_author(commit);
1381 committer = got_object_commit_get_committer(commit);
1382 if (strcmp(author, committer) != 0)
1383 printf("via: %s\n", committer);
1384 if (got_object_commit_get_nparents(commit) > 1) {
1385 const struct got_object_id_queue *parent_ids;
1386 struct got_object_qid *qid;
1387 int n = 1;
1388 parent_ids = got_object_commit_get_parent_ids(commit);
1389 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1390 err = got_object_id_str(&id_str, qid->id);
1391 if (err)
1392 return err;
1393 printf("parent %d: %s\n", n++, id_str);
1394 free(id_str);
1398 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1399 if (logmsg0 == NULL)
1400 return got_error_from_errno("strdup");
1402 logmsg = logmsg0;
1403 do {
1404 line = strsep(&logmsg, "\n");
1405 if (line)
1406 printf(" %s\n", line);
1407 } while (line);
1408 free(logmsg0);
1410 if (show_patch) {
1411 err = print_patch(commit, id, diff_context, repo);
1412 if (err == 0)
1413 printf("\n");
1416 if (fflush(stdout) != 0 && err == NULL)
1417 err = got_error_from_errno("fflush");
1418 return err;
1421 static const struct got_error *
1422 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1423 char *path, int show_patch, int diff_context, int limit,
1424 int first_parent_traversal, struct got_reflist_head *refs)
1426 const struct got_error *err;
1427 struct got_commit_graph *graph;
1429 err = got_commit_graph_open(&graph, root_id, path,
1430 first_parent_traversal, repo);
1431 if (err)
1432 return err;
1433 err = got_commit_graph_iter_start(graph, root_id, repo);
1434 if (err)
1435 goto done;
1436 for (;;) {
1437 struct got_commit_object *commit;
1438 struct got_object_id *id;
1440 if (sigint_received || sigpipe_received)
1441 break;
1443 err = got_commit_graph_iter_next(&id, graph);
1444 if (err) {
1445 if (err->code == GOT_ERR_ITER_COMPLETED) {
1446 err = NULL;
1447 break;
1449 if (err->code != GOT_ERR_ITER_NEED_MORE)
1450 break;
1451 err = got_commit_graph_fetch_commits(graph, 1, repo);
1452 if (err)
1453 break;
1454 else
1455 continue;
1457 if (id == NULL)
1458 break;
1460 err = got_object_open_as_commit(&commit, repo, id);
1461 if (err)
1462 break;
1463 err = print_commit(commit, id, repo, show_patch, diff_context,
1464 refs);
1465 got_object_commit_close(commit);
1466 if (err || (limit && --limit == 0))
1467 break;
1469 done:
1470 got_commit_graph_close(graph);
1471 return err;
1474 __dead static void
1475 usage_log(void)
1477 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1478 "[-r repository-path] [path]\n", getprogname());
1479 exit(1);
1482 static const struct got_error *
1483 cmd_log(int argc, char *argv[])
1485 const struct got_error *error;
1486 struct got_repository *repo = NULL;
1487 struct got_worktree *worktree = NULL;
1488 struct got_commit_object *commit = NULL;
1489 struct got_object_id *id = NULL;
1490 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1491 char *start_commit = NULL;
1492 int diff_context = 3, ch;
1493 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1494 const char *errstr;
1495 struct got_reflist_head refs;
1497 SIMPLEQ_INIT(&refs);
1499 #ifndef PROFILE
1500 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1501 NULL)
1502 == -1)
1503 err(1, "pledge");
1504 #endif
1506 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1507 switch (ch) {
1508 case 'p':
1509 show_patch = 1;
1510 break;
1511 case 'c':
1512 start_commit = optarg;
1513 break;
1514 case 'C':
1515 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1516 &errstr);
1517 if (errstr != NULL)
1518 err(1, "-C option %s", errstr);
1519 break;
1520 case 'l':
1521 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1522 if (errstr != NULL)
1523 err(1, "-l option %s", errstr);
1524 break;
1525 case 'f':
1526 first_parent_traversal = 1;
1527 break;
1528 case 'r':
1529 repo_path = realpath(optarg, NULL);
1530 if (repo_path == NULL)
1531 err(1, "-r option");
1532 got_path_strip_trailing_slashes(repo_path);
1533 break;
1534 default:
1535 usage_log();
1536 /* NOTREACHED */
1540 argc -= optind;
1541 argv += optind;
1543 cwd = getcwd(NULL, 0);
1544 if (cwd == NULL) {
1545 error = got_error_from_errno("getcwd");
1546 goto done;
1549 error = got_worktree_open(&worktree, cwd);
1550 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1551 goto done;
1552 error = NULL;
1554 if (argc == 0) {
1555 path = strdup("");
1556 if (path == NULL) {
1557 error = got_error_from_errno("strdup");
1558 goto done;
1560 } else if (argc == 1) {
1561 if (worktree) {
1562 error = got_worktree_resolve_path(&path, worktree,
1563 argv[0]);
1564 if (error)
1565 goto done;
1566 } else {
1567 path = strdup(argv[0]);
1568 if (path == NULL) {
1569 error = got_error_from_errno("strdup");
1570 goto done;
1573 } else
1574 usage_log();
1576 if (repo_path == NULL) {
1577 repo_path = worktree ?
1578 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1580 if (repo_path == NULL) {
1581 error = got_error_from_errno("strdup");
1582 goto done;
1585 error = got_repo_open(&repo, repo_path);
1586 if (error != NULL)
1587 goto done;
1589 error = apply_unveil(got_repo_get_path(repo), 1,
1590 worktree ? got_worktree_get_root_path(worktree) : NULL);
1591 if (error)
1592 goto done;
1594 if (start_commit == NULL) {
1595 struct got_reference *head_ref;
1596 error = got_ref_open(&head_ref, repo,
1597 worktree ? got_worktree_get_head_ref_name(worktree)
1598 : GOT_REF_HEAD, 0);
1599 if (error != NULL)
1600 return error;
1601 error = got_ref_resolve(&id, repo, head_ref);
1602 got_ref_close(head_ref);
1603 if (error != NULL)
1604 return error;
1605 error = got_object_open_as_commit(&commit, repo, id);
1606 } else {
1607 struct got_reference *ref;
1608 error = got_ref_open(&ref, repo, start_commit, 0);
1609 if (error == NULL) {
1610 int obj_type;
1611 error = got_ref_resolve(&id, repo, ref);
1612 got_ref_close(ref);
1613 if (error != NULL)
1614 goto done;
1615 error = got_object_get_type(&obj_type, repo, id);
1616 if (error != NULL)
1617 goto done;
1618 if (obj_type == GOT_OBJ_TYPE_TAG) {
1619 struct got_tag_object *tag;
1620 error = got_object_open_as_tag(&tag, repo, id);
1621 if (error != NULL)
1622 goto done;
1623 if (got_object_tag_get_object_type(tag) !=
1624 GOT_OBJ_TYPE_COMMIT) {
1625 got_object_tag_close(tag);
1626 error = got_error(GOT_ERR_OBJ_TYPE);
1627 goto done;
1629 free(id);
1630 id = got_object_id_dup(
1631 got_object_tag_get_object_id(tag));
1632 if (id == NULL)
1633 error = got_error_from_errno(
1634 "got_object_id_dup");
1635 got_object_tag_close(tag);
1636 if (error)
1637 goto done;
1638 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1639 error = got_error(GOT_ERR_OBJ_TYPE);
1640 goto done;
1642 error = got_object_open_as_commit(&commit, repo, id);
1643 if (error != NULL)
1644 goto done;
1646 if (commit == NULL) {
1647 error = got_repo_match_object_id_prefix(&id,
1648 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1649 if (error != NULL)
1650 return error;
1653 if (error != NULL)
1654 goto done;
1656 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1657 if (error != NULL)
1658 goto done;
1659 if (in_repo_path) {
1660 free(path);
1661 path = in_repo_path;
1664 error = got_ref_list(&refs, repo);
1665 if (error)
1666 goto done;
1668 error = print_commits(id, repo, path, show_patch,
1669 diff_context, limit, first_parent_traversal, &refs);
1670 done:
1671 free(path);
1672 free(repo_path);
1673 free(cwd);
1674 free(id);
1675 if (worktree)
1676 got_worktree_close(worktree);
1677 if (repo) {
1678 const struct got_error *repo_error;
1679 repo_error = got_repo_close(repo);
1680 if (error == NULL)
1681 error = repo_error;
1683 got_ref_list_free(&refs);
1684 return error;
1687 __dead static void
1688 usage_diff(void)
1690 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1691 "[object1 object2 | path]\n", getprogname());
1692 exit(1);
1695 struct print_diff_arg {
1696 struct got_repository *repo;
1697 struct got_worktree *worktree;
1698 int diff_context;
1699 const char *id_str;
1700 int header_shown;
1701 int diff_staged;
1704 static const struct got_error *
1705 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1706 const char *path, struct got_object_id *blob_id,
1707 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1709 struct print_diff_arg *a = arg;
1710 const struct got_error *err = NULL;
1711 struct got_blob_object *blob1 = NULL;
1712 FILE *f2 = NULL;
1713 char *abspath = NULL, *label1 = NULL;
1714 struct stat sb;
1716 if (a->diff_staged) {
1717 if (staged_status != GOT_STATUS_MODIFY &&
1718 staged_status != GOT_STATUS_ADD &&
1719 staged_status != GOT_STATUS_DELETE)
1720 return NULL;
1721 } else {
1722 if (staged_status == GOT_STATUS_DELETE)
1723 return NULL;
1724 if (status != GOT_STATUS_MODIFY &&
1725 status != GOT_STATUS_ADD &&
1726 status != GOT_STATUS_DELETE &&
1727 status != GOT_STATUS_CONFLICT)
1728 return NULL;
1731 if (!a->header_shown) {
1732 printf("diff %s %s%s\n", a->id_str,
1733 got_worktree_get_root_path(a->worktree),
1734 a->diff_staged ? " (staged changes)" : "");
1735 a->header_shown = 1;
1738 if (a->diff_staged) {
1739 const char *label1 = NULL, *label2 = NULL;
1740 switch (staged_status) {
1741 case GOT_STATUS_MODIFY:
1742 label1 = path;
1743 label2 = path;
1744 break;
1745 case GOT_STATUS_ADD:
1746 label2 = path;
1747 break;
1748 case GOT_STATUS_DELETE:
1749 label1 = path;
1750 break;
1751 default:
1752 return got_error(GOT_ERR_FILE_STATUS);
1754 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1755 label1, label2, a->diff_context, a->repo, stdout);
1758 if (staged_status == GOT_STATUS_ADD ||
1759 staged_status == GOT_STATUS_MODIFY) {
1760 char *id_str;
1761 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1762 8192);
1763 if (err)
1764 goto done;
1765 err = got_object_id_str(&id_str, staged_blob_id);
1766 if (err)
1767 goto done;
1768 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1769 err = got_error_from_errno("asprintf");
1770 free(id_str);
1771 goto done;
1773 free(id_str);
1774 } else if (status != GOT_STATUS_ADD) {
1775 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1776 if (err)
1777 goto done;
1780 if (status != GOT_STATUS_DELETE) {
1781 if (asprintf(&abspath, "%s/%s",
1782 got_worktree_get_root_path(a->worktree), path) == -1) {
1783 err = got_error_from_errno("asprintf");
1784 goto done;
1787 f2 = fopen(abspath, "r");
1788 if (f2 == NULL) {
1789 err = got_error_from_errno2("fopen", abspath);
1790 goto done;
1792 if (lstat(abspath, &sb) == -1) {
1793 err = got_error_from_errno2("lstat", abspath);
1794 goto done;
1796 } else
1797 sb.st_size = 0;
1799 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1800 a->diff_context, stdout);
1801 done:
1802 if (blob1)
1803 got_object_blob_close(blob1);
1804 if (f2 && fclose(f2) != 0 && err == NULL)
1805 err = got_error_from_errno("fclose");
1806 free(abspath);
1807 return err;
1810 static const struct got_error *
1811 cmd_diff(int argc, char *argv[])
1813 const struct got_error *error;
1814 struct got_repository *repo = NULL;
1815 struct got_worktree *worktree = NULL;
1816 char *cwd = NULL, *repo_path = NULL;
1817 struct got_object_id *id1 = NULL, *id2 = NULL;
1818 const char *id_str1 = NULL, *id_str2 = NULL;
1819 char *label1 = NULL, *label2 = NULL;
1820 int type1, type2;
1821 int diff_context = 3, diff_staged = 0, ch;
1822 const char *errstr;
1823 char *path = NULL;
1825 #ifndef PROFILE
1826 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1827 NULL) == -1)
1828 err(1, "pledge");
1829 #endif
1831 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1832 switch (ch) {
1833 case 'C':
1834 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1835 if (errstr != NULL)
1836 err(1, "-C option %s", errstr);
1837 break;
1838 case 'r':
1839 repo_path = realpath(optarg, NULL);
1840 if (repo_path == NULL)
1841 err(1, "-r option");
1842 got_path_strip_trailing_slashes(repo_path);
1843 break;
1844 case 's':
1845 diff_staged = 1;
1846 break;
1847 default:
1848 usage_diff();
1849 /* NOTREACHED */
1853 argc -= optind;
1854 argv += optind;
1856 cwd = getcwd(NULL, 0);
1857 if (cwd == NULL) {
1858 error = got_error_from_errno("getcwd");
1859 goto done;
1861 error = got_worktree_open(&worktree, cwd);
1862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1863 goto done;
1864 if (argc <= 1) {
1865 if (worktree == NULL) {
1866 error = got_error(GOT_ERR_NOT_WORKTREE);
1867 goto done;
1869 if (repo_path)
1870 errx(1,
1871 "-r option can't be used when diffing a work tree");
1872 repo_path = strdup(got_worktree_get_repo_path(worktree));
1873 if (repo_path == NULL) {
1874 error = got_error_from_errno("strdup");
1875 goto done;
1877 if (argc == 1) {
1878 error = got_worktree_resolve_path(&path, worktree,
1879 argv[0]);
1880 if (error)
1881 goto done;
1882 } else {
1883 path = strdup("");
1884 if (path == NULL) {
1885 error = got_error_from_errno("strdup");
1886 goto done;
1889 } else if (argc == 2) {
1890 if (diff_staged)
1891 errx(1, "-s option can't be used when diffing "
1892 "objects in repository");
1893 id_str1 = argv[0];
1894 id_str2 = argv[1];
1895 if (worktree && repo_path == NULL) {
1896 repo_path =
1897 strdup(got_worktree_get_repo_path(worktree));
1898 if (repo_path == NULL) {
1899 error = got_error_from_errno("strdup");
1900 goto done;
1903 } else
1904 usage_diff();
1906 if (repo_path == NULL) {
1907 repo_path = getcwd(NULL, 0);
1908 if (repo_path == NULL)
1909 return got_error_from_errno("getcwd");
1912 error = got_repo_open(&repo, repo_path);
1913 free(repo_path);
1914 if (error != NULL)
1915 goto done;
1917 error = apply_unveil(got_repo_get_path(repo), 1,
1918 worktree ? got_worktree_get_root_path(worktree) : NULL);
1919 if (error)
1920 goto done;
1922 if (argc <= 1) {
1923 struct print_diff_arg arg;
1924 struct got_pathlist_head paths;
1925 char *id_str;
1927 TAILQ_INIT(&paths);
1929 error = got_object_id_str(&id_str,
1930 got_worktree_get_base_commit_id(worktree));
1931 if (error)
1932 goto done;
1933 arg.repo = repo;
1934 arg.worktree = worktree;
1935 arg.diff_context = diff_context;
1936 arg.id_str = id_str;
1937 arg.header_shown = 0;
1938 arg.diff_staged = diff_staged;
1940 error = got_pathlist_append(&paths, path, NULL);
1941 if (error)
1942 goto done;
1944 error = got_worktree_status(worktree, &paths, repo, print_diff,
1945 &arg, check_cancelled, NULL);
1946 free(id_str);
1947 got_pathlist_free(&paths);
1948 goto done;
1951 error = got_repo_match_object_id_prefix(&id1, id_str1,
1952 GOT_OBJ_TYPE_ANY, repo);
1953 if (error) {
1954 struct got_reference *ref;
1955 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1956 goto done;
1957 error = got_ref_open(&ref, repo, id_str1, 0);
1958 if (error != NULL)
1959 goto done;
1960 label1 = strdup(got_ref_get_name(ref));
1961 if (label1 == NULL) {
1962 error = got_error_from_errno("strdup");
1963 goto done;
1965 error = got_ref_resolve(&id1, repo, ref);
1966 got_ref_close(ref);
1967 if (error != NULL)
1968 goto done;
1969 } else {
1970 error = got_object_id_str(&label1, id1);
1971 if (label1 == NULL) {
1972 error = got_error_from_errno("strdup");
1973 goto done;
1977 error = got_repo_match_object_id_prefix(&id2, id_str2,
1978 GOT_OBJ_TYPE_ANY, repo);
1979 if (error) {
1980 struct got_reference *ref;
1981 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1982 goto done;
1983 error = got_ref_open(&ref, repo, id_str2, 0);
1984 if (error != NULL)
1985 goto done;
1986 label2 = strdup(got_ref_get_name(ref));
1987 if (label2 == NULL) {
1988 error = got_error_from_errno("strdup");
1989 goto done;
1991 error = got_ref_resolve(&id2, repo, ref);
1992 got_ref_close(ref);
1993 if (error != NULL)
1994 goto done;
1995 } else {
1996 error = got_object_id_str(&label2, id2);
1997 if (label2 == NULL) {
1998 error = got_error_from_errno("strdup");
1999 goto done;
2003 error = got_object_get_type(&type1, repo, id1);
2004 if (error)
2005 goto done;
2007 error = got_object_get_type(&type2, repo, id2);
2008 if (error)
2009 goto done;
2011 if (type1 != type2) {
2012 error = got_error(GOT_ERR_OBJ_TYPE);
2013 goto done;
2016 switch (type1) {
2017 case GOT_OBJ_TYPE_BLOB:
2018 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2019 diff_context, repo, stdout);
2020 break;
2021 case GOT_OBJ_TYPE_TREE:
2022 error = got_diff_objects_as_trees(id1, id2, "", "",
2023 diff_context, repo, stdout);
2024 break;
2025 case GOT_OBJ_TYPE_COMMIT:
2026 printf("diff %s %s\n", label1, label2);
2027 error = got_diff_objects_as_commits(id1, id2, diff_context,
2028 repo, stdout);
2029 break;
2030 default:
2031 error = got_error(GOT_ERR_OBJ_TYPE);
2034 done:
2035 free(label1);
2036 free(label2);
2037 free(id1);
2038 free(id2);
2039 free(path);
2040 if (worktree)
2041 got_worktree_close(worktree);
2042 if (repo) {
2043 const struct got_error *repo_error;
2044 repo_error = got_repo_close(repo);
2045 if (error == NULL)
2046 error = repo_error;
2048 return error;
2051 __dead static void
2052 usage_blame(void)
2054 fprintf(stderr,
2055 "usage: %s blame [-c commit] [-r repository-path] path\n",
2056 getprogname());
2057 exit(1);
2060 static const struct got_error *
2061 cmd_blame(int argc, char *argv[])
2063 const struct got_error *error;
2064 struct got_repository *repo = NULL;
2065 struct got_worktree *worktree = NULL;
2066 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2067 struct got_object_id *commit_id = NULL;
2068 char *commit_id_str = NULL;
2069 int ch;
2071 #ifndef PROFILE
2072 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2073 NULL) == -1)
2074 err(1, "pledge");
2075 #endif
2077 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2078 switch (ch) {
2079 case 'c':
2080 commit_id_str = optarg;
2081 break;
2082 case 'r':
2083 repo_path = realpath(optarg, NULL);
2084 if (repo_path == NULL)
2085 err(1, "-r option");
2086 got_path_strip_trailing_slashes(repo_path);
2087 break;
2088 default:
2089 usage_blame();
2090 /* NOTREACHED */
2094 argc -= optind;
2095 argv += optind;
2097 if (argc == 1)
2098 path = argv[0];
2099 else
2100 usage_blame();
2102 cwd = getcwd(NULL, 0);
2103 if (cwd == NULL) {
2104 error = got_error_from_errno("getcwd");
2105 goto done;
2107 if (repo_path == NULL) {
2108 error = got_worktree_open(&worktree, cwd);
2109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2110 goto done;
2111 else
2112 error = NULL;
2113 if (worktree) {
2114 repo_path =
2115 strdup(got_worktree_get_repo_path(worktree));
2116 if (repo_path == NULL)
2117 error = got_error_from_errno("strdup");
2118 if (error)
2119 goto done;
2120 } else {
2121 repo_path = strdup(cwd);
2122 if (repo_path == NULL) {
2123 error = got_error_from_errno("strdup");
2124 goto done;
2129 error = got_repo_open(&repo, repo_path);
2130 if (error != NULL)
2131 goto done;
2133 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2134 if (error)
2135 goto done;
2137 if (worktree) {
2138 const char *prefix = got_worktree_get_path_prefix(worktree);
2139 char *p, *worktree_subdir = cwd +
2140 strlen(got_worktree_get_root_path(worktree));
2141 if (asprintf(&p, "%s%s%s%s%s",
2142 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2143 worktree_subdir, worktree_subdir[0] ? "/" : "",
2144 path) == -1) {
2145 error = got_error_from_errno("asprintf");
2146 goto done;
2148 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2149 free(p);
2150 } else {
2151 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2153 if (error)
2154 goto done;
2156 if (commit_id_str == NULL) {
2157 struct got_reference *head_ref;
2158 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2159 if (error != NULL)
2160 goto done;
2161 error = got_ref_resolve(&commit_id, repo, head_ref);
2162 got_ref_close(head_ref);
2163 if (error != NULL)
2164 goto done;
2165 } else {
2166 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2167 if (error)
2168 goto done;
2171 error = got_blame(in_repo_path, commit_id, repo, stdout);
2172 done:
2173 free(in_repo_path);
2174 free(repo_path);
2175 free(cwd);
2176 free(commit_id);
2177 if (worktree)
2178 got_worktree_close(worktree);
2179 if (repo) {
2180 const struct got_error *repo_error;
2181 repo_error = got_repo_close(repo);
2182 if (error == NULL)
2183 error = repo_error;
2185 return error;
2188 __dead static void
2189 usage_tree(void)
2191 fprintf(stderr,
2192 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2193 getprogname());
2194 exit(1);
2197 static void
2198 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2199 const char *root_path)
2201 int is_root_path = (strcmp(path, root_path) == 0);
2203 path += strlen(root_path);
2204 while (path[0] == '/')
2205 path++;
2207 printf("%s%s%s%s%s\n", id ? id : "", path,
2208 is_root_path ? "" : "/", te->name,
2209 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2212 static const struct got_error *
2213 print_tree(const char *path, struct got_object_id *commit_id,
2214 int show_ids, int recurse, const char *root_path,
2215 struct got_repository *repo)
2217 const struct got_error *err = NULL;
2218 struct got_object_id *tree_id = NULL;
2219 struct got_tree_object *tree = NULL;
2220 const struct got_tree_entries *entries;
2221 struct got_tree_entry *te;
2223 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2224 if (err)
2225 goto done;
2227 err = got_object_open_as_tree(&tree, repo, tree_id);
2228 if (err)
2229 goto done;
2230 entries = got_object_tree_get_entries(tree);
2231 te = SIMPLEQ_FIRST(&entries->head);
2232 while (te) {
2233 char *id = NULL;
2235 if (sigint_received || sigpipe_received)
2236 break;
2238 if (show_ids) {
2239 char *id_str;
2240 err = got_object_id_str(&id_str, te->id);
2241 if (err)
2242 goto done;
2243 if (asprintf(&id, "%s ", id_str) == -1) {
2244 err = got_error_from_errno("asprintf");
2245 free(id_str);
2246 goto done;
2248 free(id_str);
2250 print_entry(te, id, path, root_path);
2251 free(id);
2253 if (recurse && S_ISDIR(te->mode)) {
2254 char *child_path;
2255 if (asprintf(&child_path, "%s%s%s", path,
2256 path[0] == '/' && path[1] == '\0' ? "" : "/",
2257 te->name) == -1) {
2258 err = got_error_from_errno("asprintf");
2259 goto done;
2261 err = print_tree(child_path, commit_id, show_ids, 1,
2262 root_path, repo);
2263 free(child_path);
2264 if (err)
2265 goto done;
2268 te = SIMPLEQ_NEXT(te, entry);
2270 done:
2271 if (tree)
2272 got_object_tree_close(tree);
2273 free(tree_id);
2274 return err;
2277 static const struct got_error *
2278 cmd_tree(int argc, char *argv[])
2280 const struct got_error *error;
2281 struct got_repository *repo = NULL;
2282 struct got_worktree *worktree = NULL;
2283 const char *path;
2284 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2285 struct got_object_id *commit_id = NULL;
2286 char *commit_id_str = NULL;
2287 int show_ids = 0, recurse = 0;
2288 int ch;
2290 #ifndef PROFILE
2291 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2292 NULL) == -1)
2293 err(1, "pledge");
2294 #endif
2296 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2297 switch (ch) {
2298 case 'c':
2299 commit_id_str = optarg;
2300 break;
2301 case 'r':
2302 repo_path = realpath(optarg, NULL);
2303 if (repo_path == NULL)
2304 err(1, "-r option");
2305 got_path_strip_trailing_slashes(repo_path);
2306 break;
2307 case 'i':
2308 show_ids = 1;
2309 break;
2310 case 'R':
2311 recurse = 1;
2312 break;
2313 default:
2314 usage_tree();
2315 /* NOTREACHED */
2319 argc -= optind;
2320 argv += optind;
2322 if (argc == 1)
2323 path = argv[0];
2324 else if (argc > 1)
2325 usage_tree();
2326 else
2327 path = NULL;
2329 cwd = getcwd(NULL, 0);
2330 if (cwd == NULL) {
2331 error = got_error_from_errno("getcwd");
2332 goto done;
2334 if (repo_path == NULL) {
2335 error = got_worktree_open(&worktree, cwd);
2336 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2337 goto done;
2338 else
2339 error = NULL;
2340 if (worktree) {
2341 repo_path =
2342 strdup(got_worktree_get_repo_path(worktree));
2343 if (repo_path == NULL)
2344 error = got_error_from_errno("strdup");
2345 if (error)
2346 goto done;
2347 } else {
2348 repo_path = strdup(cwd);
2349 if (repo_path == NULL) {
2350 error = got_error_from_errno("strdup");
2351 goto done;
2356 error = got_repo_open(&repo, repo_path);
2357 if (error != NULL)
2358 goto done;
2360 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2361 if (error)
2362 goto done;
2364 if (path == NULL) {
2365 if (worktree) {
2366 char *p, *worktree_subdir = cwd +
2367 strlen(got_worktree_get_root_path(worktree));
2368 if (asprintf(&p, "%s/%s",
2369 got_worktree_get_path_prefix(worktree),
2370 worktree_subdir) == -1) {
2371 error = got_error_from_errno("asprintf");
2372 goto done;
2374 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2375 free(p);
2376 if (error)
2377 goto done;
2378 } else
2379 path = "/";
2381 if (in_repo_path == NULL) {
2382 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2383 if (error != NULL)
2384 goto done;
2387 if (commit_id_str == NULL) {
2388 struct got_reference *head_ref;
2389 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2390 if (error != NULL)
2391 goto done;
2392 error = got_ref_resolve(&commit_id, repo, head_ref);
2393 got_ref_close(head_ref);
2394 if (error != NULL)
2395 goto done;
2396 } else {
2397 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2398 if (error)
2399 goto done;
2402 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2403 in_repo_path, repo);
2404 done:
2405 free(in_repo_path);
2406 free(repo_path);
2407 free(cwd);
2408 free(commit_id);
2409 if (worktree)
2410 got_worktree_close(worktree);
2411 if (repo) {
2412 const struct got_error *repo_error;
2413 repo_error = got_repo_close(repo);
2414 if (error == NULL)
2415 error = repo_error;
2417 return error;
2420 __dead static void
2421 usage_status(void)
2423 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2424 exit(1);
2427 static const struct got_error *
2428 print_status(void *arg, unsigned char status, unsigned char staged_status,
2429 const char *path, struct got_object_id *blob_id,
2430 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2432 if (status == staged_status && (status == GOT_STATUS_DELETE))
2433 status = GOT_STATUS_NO_CHANGE;
2434 printf("%c%c %s\n", status, staged_status, path);
2435 return NULL;
2438 static const struct got_error *
2439 cmd_status(int argc, char *argv[])
2441 const struct got_error *error = NULL;
2442 struct got_repository *repo = NULL;
2443 struct got_worktree *worktree = NULL;
2444 char *cwd = NULL;
2445 struct got_pathlist_head paths;
2446 struct got_pathlist_entry *pe;
2447 int ch;
2449 TAILQ_INIT(&paths);
2451 while ((ch = getopt(argc, argv, "")) != -1) {
2452 switch (ch) {
2453 default:
2454 usage_status();
2455 /* NOTREACHED */
2459 argc -= optind;
2460 argv += optind;
2462 #ifndef PROFILE
2463 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2464 NULL) == -1)
2465 err(1, "pledge");
2466 #endif
2467 cwd = getcwd(NULL, 0);
2468 if (cwd == NULL) {
2469 error = got_error_from_errno("getcwd");
2470 goto done;
2473 error = got_worktree_open(&worktree, cwd);
2474 if (error != NULL)
2475 goto done;
2477 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2478 if (error != NULL)
2479 goto done;
2481 error = apply_unveil(got_repo_get_path(repo), 1,
2482 got_worktree_get_root_path(worktree));
2483 if (error)
2484 goto done;
2486 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2487 if (error)
2488 goto done;
2490 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2491 check_cancelled, NULL);
2492 done:
2493 TAILQ_FOREACH(pe, &paths, entry)
2494 free((char *)pe->path);
2495 got_pathlist_free(&paths);
2496 free(cwd);
2497 return error;
2500 __dead static void
2501 usage_ref(void)
2503 fprintf(stderr,
2504 "usage: %s ref [-r repository] -l | -d name | name target\n",
2505 getprogname());
2506 exit(1);
2509 static const struct got_error *
2510 list_refs(struct got_repository *repo)
2512 static const struct got_error *err = NULL;
2513 struct got_reflist_head refs;
2514 struct got_reflist_entry *re;
2516 SIMPLEQ_INIT(&refs);
2517 err = got_ref_list(&refs, repo);
2518 if (err)
2519 return err;
2521 SIMPLEQ_FOREACH(re, &refs, entry) {
2522 char *refstr;
2523 refstr = got_ref_to_str(re->ref);
2524 if (refstr == NULL)
2525 return got_error_from_errno("got_ref_to_str");
2526 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2527 free(refstr);
2530 got_ref_list_free(&refs);
2531 return NULL;
2534 static const struct got_error *
2535 delete_ref(struct got_repository *repo, const char *refname)
2537 const struct got_error *err = NULL;
2538 struct got_reference *ref;
2540 err = got_ref_open(&ref, repo, refname, 0);
2541 if (err)
2542 return err;
2544 err = got_ref_delete(ref, repo);
2545 got_ref_close(ref);
2546 return err;
2549 static const struct got_error *
2550 add_ref(struct got_repository *repo, const char *refname, const char *target)
2552 const struct got_error *err = NULL;
2553 struct got_object_id *id;
2554 struct got_reference *ref = NULL;
2557 * Don't let the user create a reference named '-'.
2558 * While technically a valid reference name, this case is usually
2559 * an unintended typo.
2561 if (refname[0] == '-' && refname[1] == '\0')
2562 return got_error(GOT_ERR_BAD_REF_NAME);
2564 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2565 repo);
2566 if (err) {
2567 struct got_reference *target_ref;
2569 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2570 return err;
2571 err = got_ref_open(&target_ref, repo, target, 0);
2572 if (err)
2573 return err;
2574 err = got_ref_resolve(&id, repo, target_ref);
2575 got_ref_close(target_ref);
2576 if (err)
2577 return err;
2580 err = got_ref_alloc(&ref, refname, id);
2581 if (err)
2582 goto done;
2584 err = got_ref_write(ref, repo);
2585 done:
2586 if (ref)
2587 got_ref_close(ref);
2588 free(id);
2589 return err;
2592 static const struct got_error *
2593 cmd_ref(int argc, char *argv[])
2595 const struct got_error *error = NULL;
2596 struct got_repository *repo = NULL;
2597 struct got_worktree *worktree = NULL;
2598 char *cwd = NULL, *repo_path = NULL;
2599 int ch, do_list = 0;
2600 const char *delref = NULL;
2602 /* TODO: Add -s option for adding symbolic references. */
2603 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2604 switch (ch) {
2605 case 'd':
2606 delref = optarg;
2607 break;
2608 case 'r':
2609 repo_path = realpath(optarg, NULL);
2610 if (repo_path == NULL)
2611 err(1, "-r option");
2612 got_path_strip_trailing_slashes(repo_path);
2613 break;
2614 case 'l':
2615 do_list = 1;
2616 break;
2617 default:
2618 usage_ref();
2619 /* NOTREACHED */
2623 if (do_list && delref)
2624 errx(1, "-l and -d options are mutually exclusive\n");
2626 argc -= optind;
2627 argv += optind;
2629 if (do_list || delref) {
2630 if (argc > 0)
2631 usage_ref();
2632 } else if (argc != 2)
2633 usage_ref();
2635 #ifndef PROFILE
2636 if (do_list) {
2637 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2638 NULL) == -1)
2639 err(1, "pledge");
2640 } else {
2641 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2642 "sendfd unveil", NULL) == -1)
2643 err(1, "pledge");
2645 #endif
2646 cwd = getcwd(NULL, 0);
2647 if (cwd == NULL) {
2648 error = got_error_from_errno("getcwd");
2649 goto done;
2652 if (repo_path == NULL) {
2653 error = got_worktree_open(&worktree, cwd);
2654 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2655 goto done;
2656 else
2657 error = NULL;
2658 if (worktree) {
2659 repo_path =
2660 strdup(got_worktree_get_repo_path(worktree));
2661 if (repo_path == NULL)
2662 error = got_error_from_errno("strdup");
2663 if (error)
2664 goto done;
2665 } else {
2666 repo_path = strdup(cwd);
2667 if (repo_path == NULL) {
2668 error = got_error_from_errno("strdup");
2669 goto done;
2674 error = got_repo_open(&repo, repo_path);
2675 if (error != NULL)
2676 goto done;
2678 error = apply_unveil(got_repo_get_path(repo), do_list,
2679 worktree ? got_worktree_get_root_path(worktree) : NULL);
2680 if (error)
2681 goto done;
2683 if (do_list)
2684 error = list_refs(repo);
2685 else if (delref)
2686 error = delete_ref(repo, delref);
2687 else
2688 error = add_ref(repo, argv[0], argv[1]);
2689 done:
2690 if (repo)
2691 got_repo_close(repo);
2692 if (worktree)
2693 got_worktree_close(worktree);
2694 free(cwd);
2695 free(repo_path);
2696 return error;
2699 __dead static void
2700 usage_branch(void)
2702 fprintf(stderr,
2703 "usage: %s branch [-r repository] -l | -d name | "
2704 "name [base-branch]\n", getprogname());
2705 exit(1);
2708 static const struct got_error *
2709 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2711 static const struct got_error *err = NULL;
2712 struct got_reflist_head refs;
2713 struct got_reflist_entry *re;
2715 SIMPLEQ_INIT(&refs);
2717 err = got_ref_list(&refs, repo);
2718 if (err)
2719 return err;
2721 SIMPLEQ_FOREACH(re, &refs, entry) {
2722 const char *refname, *marker = " ";
2723 char *refstr;
2724 refname = got_ref_get_name(re->ref);
2725 if (strncmp(refname, "refs/heads/", 11) != 0)
2726 continue;
2727 if (worktree && strcmp(refname,
2728 got_worktree_get_head_ref_name(worktree)) == 0) {
2729 struct got_object_id *id = NULL;
2730 err = got_ref_resolve(&id, repo, re->ref);
2731 if (err)
2732 return err;
2733 if (got_object_id_cmp(id,
2734 got_worktree_get_base_commit_id(worktree)) == 0)
2735 marker = "* ";
2736 else
2737 marker = "~ ";
2738 free(id);
2740 refname += 11;
2741 refstr = got_ref_to_str(re->ref);
2742 if (refstr == NULL)
2743 return got_error_from_errno("got_ref_to_str");
2744 printf("%s%s: %s\n", marker, refname, refstr);
2745 free(refstr);
2748 got_ref_list_free(&refs);
2749 return NULL;
2752 static const struct got_error *
2753 delete_branch(struct got_repository *repo, const char *branch_name)
2755 const struct got_error *err = NULL;
2756 struct got_reference *ref;
2757 char *refname;
2759 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2760 return got_error_from_errno("asprintf");
2762 err = got_ref_open(&ref, repo, refname, 0);
2763 if (err)
2764 goto done;
2766 err = got_ref_delete(ref, repo);
2767 got_ref_close(ref);
2768 done:
2769 free(refname);
2770 return err;
2773 static const struct got_error *
2774 add_branch(struct got_repository *repo, const char *branch_name,
2775 const char *base_branch)
2777 const struct got_error *err = NULL;
2778 struct got_object_id *id = NULL;
2779 struct got_reference *ref = NULL;
2780 char *base_refname = NULL, *refname = NULL;
2781 struct got_reference *base_ref;
2784 * Don't let the user create a branch named '-'.
2785 * While technically a valid reference name, this case is usually
2786 * an unintended typo.
2788 if (branch_name[0] == '-' && branch_name[1] == '\0')
2789 return got_error(GOT_ERR_BAD_REF_NAME);
2791 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2792 base_refname = strdup(GOT_REF_HEAD);
2793 if (base_refname == NULL)
2794 return got_error_from_errno("strdup");
2795 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2796 return got_error_from_errno("asprintf");
2798 err = got_ref_open(&base_ref, repo, base_refname, 0);
2799 if (err)
2800 goto done;
2801 err = got_ref_resolve(&id, repo, base_ref);
2802 got_ref_close(base_ref);
2803 if (err)
2804 goto done;
2806 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2807 err = got_error_from_errno("asprintf");
2808 goto done;
2811 err = got_ref_open(&ref, repo, refname, 0);
2812 if (err == NULL) {
2813 err = got_error(GOT_ERR_BRANCH_EXISTS);
2814 goto done;
2815 } else if (err->code != GOT_ERR_NOT_REF)
2816 goto done;
2818 err = got_ref_alloc(&ref, refname, id);
2819 if (err)
2820 goto done;
2822 err = got_ref_write(ref, repo);
2823 done:
2824 if (ref)
2825 got_ref_close(ref);
2826 free(id);
2827 free(base_refname);
2828 free(refname);
2829 return err;
2832 static const struct got_error *
2833 cmd_branch(int argc, char *argv[])
2835 const struct got_error *error = NULL;
2836 struct got_repository *repo = NULL;
2837 struct got_worktree *worktree = NULL;
2838 char *cwd = NULL, *repo_path = NULL;
2839 int ch, do_list = 0;
2840 const char *delref = NULL;
2842 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2843 switch (ch) {
2844 case 'd':
2845 delref = optarg;
2846 break;
2847 case 'r':
2848 repo_path = realpath(optarg, NULL);
2849 if (repo_path == NULL)
2850 err(1, "-r option");
2851 got_path_strip_trailing_slashes(repo_path);
2852 break;
2853 case 'l':
2854 do_list = 1;
2855 break;
2856 default:
2857 usage_branch();
2858 /* NOTREACHED */
2862 if (do_list && delref)
2863 errx(1, "-l and -d options are mutually exclusive\n");
2865 argc -= optind;
2866 argv += optind;
2868 if (do_list || delref) {
2869 if (argc > 0)
2870 usage_branch();
2871 } else if (argc < 1 || argc > 2)
2872 usage_branch();
2874 #ifndef PROFILE
2875 if (do_list) {
2876 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2877 NULL) == -1)
2878 err(1, "pledge");
2879 } else {
2880 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2881 "sendfd unveil", NULL) == -1)
2882 err(1, "pledge");
2884 #endif
2885 cwd = getcwd(NULL, 0);
2886 if (cwd == NULL) {
2887 error = got_error_from_errno("getcwd");
2888 goto done;
2891 if (repo_path == NULL) {
2892 error = got_worktree_open(&worktree, cwd);
2893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2894 goto done;
2895 else
2896 error = NULL;
2897 if (worktree) {
2898 repo_path =
2899 strdup(got_worktree_get_repo_path(worktree));
2900 if (repo_path == NULL)
2901 error = got_error_from_errno("strdup");
2902 if (error)
2903 goto done;
2904 } else {
2905 repo_path = strdup(cwd);
2906 if (repo_path == NULL) {
2907 error = got_error_from_errno("strdup");
2908 goto done;
2913 error = got_repo_open(&repo, repo_path);
2914 if (error != NULL)
2915 goto done;
2917 error = apply_unveil(got_repo_get_path(repo), do_list,
2918 worktree ? got_worktree_get_root_path(worktree) : NULL);
2919 if (error)
2920 goto done;
2922 if (do_list)
2923 error = list_branches(repo, worktree);
2924 else if (delref)
2925 error = delete_branch(repo, delref);
2926 else {
2927 const char *base_branch;
2928 if (argc == 1) {
2929 base_branch = worktree ?
2930 got_worktree_get_head_ref_name(worktree) :
2931 GOT_REF_HEAD;
2932 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2933 base_branch += 11;
2934 } else
2935 base_branch = argv[1];
2936 error = add_branch(repo, argv[0], base_branch);
2938 done:
2939 if (repo)
2940 got_repo_close(repo);
2941 if (worktree)
2942 got_worktree_close(worktree);
2943 free(cwd);
2944 free(repo_path);
2945 return error;
2948 __dead static void
2949 usage_add(void)
2951 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2952 exit(1);
2955 static const struct got_error *
2956 cmd_add(int argc, char *argv[])
2958 const struct got_error *error = NULL;
2959 struct got_repository *repo = NULL;
2960 struct got_worktree *worktree = NULL;
2961 char *cwd = NULL;
2962 struct got_pathlist_head paths;
2963 struct got_pathlist_entry *pe;
2964 int ch;
2966 TAILQ_INIT(&paths);
2968 while ((ch = getopt(argc, argv, "")) != -1) {
2969 switch (ch) {
2970 default:
2971 usage_add();
2972 /* NOTREACHED */
2976 argc -= optind;
2977 argv += optind;
2979 #ifndef PROFILE
2980 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2981 NULL) == -1)
2982 err(1, "pledge");
2983 #endif
2984 if (argc < 1)
2985 usage_add();
2987 cwd = getcwd(NULL, 0);
2988 if (cwd == NULL) {
2989 error = got_error_from_errno("getcwd");
2990 goto done;
2993 error = got_worktree_open(&worktree, cwd);
2994 if (error)
2995 goto done;
2997 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2998 if (error != NULL)
2999 goto done;
3001 error = apply_unveil(got_repo_get_path(repo), 1,
3002 got_worktree_get_root_path(worktree));
3003 if (error)
3004 goto done;
3006 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3007 if (error)
3008 goto done;
3010 error = got_worktree_schedule_add(worktree, &paths, print_status,
3011 NULL, repo);
3012 done:
3013 if (repo)
3014 got_repo_close(repo);
3015 if (worktree)
3016 got_worktree_close(worktree);
3017 TAILQ_FOREACH(pe, &paths, entry)
3018 free((char *)pe->path);
3019 got_pathlist_free(&paths);
3020 free(cwd);
3021 return error;
3024 __dead static void
3025 usage_remove(void)
3027 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3028 exit(1);
3031 static const struct got_error *
3032 cmd_remove(int argc, char *argv[])
3034 const struct got_error *error = NULL;
3035 struct got_worktree *worktree = NULL;
3036 struct got_repository *repo = NULL;
3037 char *cwd = NULL;
3038 struct got_pathlist_head paths;
3039 struct got_pathlist_entry *pe;
3040 int ch, delete_local_mods = 0;
3042 TAILQ_INIT(&paths);
3044 while ((ch = getopt(argc, argv, "f")) != -1) {
3045 switch (ch) {
3046 case 'f':
3047 delete_local_mods = 1;
3048 break;
3049 default:
3050 usage_add();
3051 /* NOTREACHED */
3055 argc -= optind;
3056 argv += optind;
3058 #ifndef PROFILE
3059 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3060 NULL) == -1)
3061 err(1, "pledge");
3062 #endif
3063 if (argc < 1)
3064 usage_remove();
3066 cwd = getcwd(NULL, 0);
3067 if (cwd == NULL) {
3068 error = got_error_from_errno("getcwd");
3069 goto done;
3071 error = got_worktree_open(&worktree, cwd);
3072 if (error)
3073 goto done;
3075 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3076 if (error)
3077 goto done;
3079 error = apply_unveil(got_repo_get_path(repo), 1,
3080 got_worktree_get_root_path(worktree));
3081 if (error)
3082 goto done;
3084 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3085 if (error)
3086 goto done;
3088 error = got_worktree_schedule_delete(worktree, &paths,
3089 delete_local_mods, print_status, NULL, repo);
3090 if (error)
3091 goto done;
3092 done:
3093 if (repo)
3094 got_repo_close(repo);
3095 if (worktree)
3096 got_worktree_close(worktree);
3097 TAILQ_FOREACH(pe, &paths, entry)
3098 free((char *)pe->path);
3099 got_pathlist_free(&paths);
3100 free(cwd);
3101 return error;
3104 __dead static void
3105 usage_revert(void)
3107 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3108 "path ...\n", getprogname());
3109 exit(1);
3112 static const struct got_error *
3113 revert_progress(void *arg, unsigned char status, const char *path)
3115 while (path[0] == '/')
3116 path++;
3117 printf("%c %s\n", status, path);
3118 return NULL;
3121 struct choose_patch_arg {
3122 FILE *patch_script_file;
3123 const char *action;
3126 static const struct got_error *
3127 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3128 int nchanges, const char *action)
3130 char *line = NULL;
3131 size_t linesize = 0;
3132 ssize_t linelen;
3134 switch (status) {
3135 case GOT_STATUS_ADD:
3136 printf("A %s\n%s this addition? [y/n] ", path, action);
3137 break;
3138 case GOT_STATUS_DELETE:
3139 printf("D %s\n%s this deletion? [y/n] ", path, action);
3140 break;
3141 case GOT_STATUS_MODIFY:
3142 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3143 return got_error_from_errno("fseek");
3144 printf(GOT_COMMIT_SEP_STR);
3145 while ((linelen = getline(&line, &linesize, patch_file) != -1))
3146 printf("%s", line);
3147 if (ferror(patch_file))
3148 return got_error_from_errno("getline");
3149 printf(GOT_COMMIT_SEP_STR);
3150 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3151 path, n, nchanges, action);
3152 break;
3153 default:
3154 return got_error_path(path, GOT_ERR_FILE_STATUS);
3157 return NULL;
3160 static const struct got_error *
3161 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3162 FILE *patch_file, int n, int nchanges)
3164 const struct got_error *err = NULL;
3165 char *line = NULL;
3166 size_t linesize = 0;
3167 ssize_t linelen;
3168 int resp = ' ';
3169 struct choose_patch_arg *a = arg;
3171 *choice = GOT_PATCH_CHOICE_NONE;
3173 if (a->patch_script_file) {
3174 char *nl;
3175 err = show_change(status, path, patch_file, n, nchanges,
3176 a->action);
3177 if (err)
3178 return err;
3179 linelen = getline(&line, &linesize, a->patch_script_file);
3180 if (linelen == -1) {
3181 if (ferror(a->patch_script_file))
3182 return got_error_from_errno("getline");
3183 return NULL;
3185 nl = strchr(line, '\n');
3186 if (nl)
3187 *nl = '\0';
3188 if (strcmp(line, "y") == 0) {
3189 *choice = GOT_PATCH_CHOICE_YES;
3190 printf("y\n");
3191 } else if (strcmp(line, "n") == 0) {
3192 *choice = GOT_PATCH_CHOICE_NO;
3193 printf("n\n");
3194 } else if (strcmp(line, "q") == 0 &&
3195 status == GOT_STATUS_MODIFY) {
3196 *choice = GOT_PATCH_CHOICE_QUIT;
3197 printf("q\n");
3198 } else
3199 printf("invalid response '%s'\n", line);
3200 free(line);
3201 return NULL;
3204 while (resp != 'y' && resp != 'n' && resp != 'q') {
3205 err = show_change(status, path, patch_file, n, nchanges,
3206 a->action);
3207 if (err)
3208 return err;
3209 resp = getchar();
3210 if (resp == '\n')
3211 resp = getchar();
3212 if (status == GOT_STATUS_MODIFY) {
3213 if (resp != 'y' && resp != 'n' && resp != 'q') {
3214 printf("invalid response '%c'\n", resp);
3215 resp = ' ';
3217 } else if (resp != 'y' && resp != 'n') {
3218 printf("invalid response '%c'\n", resp);
3219 resp = ' ';
3223 if (resp == 'y')
3224 *choice = GOT_PATCH_CHOICE_YES;
3225 else if (resp == 'n')
3226 *choice = GOT_PATCH_CHOICE_NO;
3227 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3228 *choice = GOT_PATCH_CHOICE_QUIT;
3230 return NULL;
3234 static const struct got_error *
3235 cmd_revert(int argc, char *argv[])
3237 const struct got_error *error = NULL;
3238 struct got_worktree *worktree = NULL;
3239 struct got_repository *repo = NULL;
3240 char *cwd = NULL, *path = NULL;
3241 struct got_pathlist_head paths;
3242 struct got_pathlist_entry *pe;
3243 int ch, can_recurse = 0, pflag = 0;
3244 FILE *patch_script_file = NULL;
3245 const char *patch_script_path = NULL;
3246 struct choose_patch_arg cpa;
3248 TAILQ_INIT(&paths);
3250 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3251 switch (ch) {
3252 case 'p':
3253 pflag = 1;
3254 break;
3255 case 'F':
3256 patch_script_path = optarg;
3257 break;
3258 case 'R':
3259 can_recurse = 1;
3260 break;
3261 default:
3262 usage_revert();
3263 /* NOTREACHED */
3267 argc -= optind;
3268 argv += optind;
3270 #ifndef PROFILE
3271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3272 "unveil", NULL) == -1)
3273 err(1, "pledge");
3274 #endif
3275 if (argc < 1)
3276 usage_revert();
3277 if (patch_script_path && !pflag)
3278 errx(1, "-F option can only be used together with -p option");
3280 cwd = getcwd(NULL, 0);
3281 if (cwd == NULL) {
3282 error = got_error_from_errno("getcwd");
3283 goto done;
3285 error = got_worktree_open(&worktree, cwd);
3286 if (error)
3287 goto done;
3289 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3290 if (error != NULL)
3291 goto done;
3293 if (patch_script_path) {
3294 patch_script_file = fopen(patch_script_path, "r");
3295 if (patch_script_file == NULL) {
3296 error = got_error_from_errno2("fopen",
3297 patch_script_path);
3298 goto done;
3301 error = apply_unveil(got_repo_get_path(repo), 1,
3302 got_worktree_get_root_path(worktree));
3303 if (error)
3304 goto done;
3306 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3307 if (error)
3308 goto done;
3310 if (!can_recurse) {
3311 char *ondisk_path;
3312 struct stat sb;
3313 TAILQ_FOREACH(pe, &paths, entry) {
3314 if (asprintf(&ondisk_path, "%s/%s",
3315 got_worktree_get_root_path(worktree),
3316 pe->path) == -1) {
3317 error = got_error_from_errno("asprintf");
3318 goto done;
3320 if (lstat(ondisk_path, &sb) == -1) {
3321 if (errno == ENOENT) {
3322 free(ondisk_path);
3323 continue;
3325 error = got_error_from_errno2("lstat",
3326 ondisk_path);
3327 free(ondisk_path);
3328 goto done;
3330 free(ondisk_path);
3331 if (S_ISDIR(sb.st_mode)) {
3332 error = got_error_msg(GOT_ERR_BAD_PATH,
3333 "reverting directories requires -R option");
3334 goto done;
3339 cpa.patch_script_file = patch_script_file;
3340 cpa.action = "revert";
3341 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3342 pflag ? choose_patch : NULL, &cpa, repo);
3343 if (error)
3344 goto done;
3345 done:
3346 if (patch_script_file && fclose(patch_script_file) == EOF &&
3347 error == NULL)
3348 error = got_error_from_errno2("fclose", patch_script_path);
3349 if (repo)
3350 got_repo_close(repo);
3351 if (worktree)
3352 got_worktree_close(worktree);
3353 free(path);
3354 free(cwd);
3355 return error;
3358 __dead static void
3359 usage_commit(void)
3361 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3362 getprogname());
3363 exit(1);
3366 struct collect_commit_logmsg_arg {
3367 const char *cmdline_log;
3368 const char *editor;
3369 const char *worktree_path;
3370 const char *branch_name;
3371 const char *repo_path;
3372 char *logmsg_path;
3376 static const struct got_error *
3377 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3378 void *arg)
3380 char *initial_content = NULL;
3381 struct got_pathlist_entry *pe;
3382 const struct got_error *err = NULL;
3383 char *template = NULL;
3384 struct collect_commit_logmsg_arg *a = arg;
3385 int fd;
3386 size_t len;
3388 /* if a message was specified on the command line, just use it */
3389 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3390 len = strlen(a->cmdline_log) + 1;
3391 *logmsg = malloc(len + 1);
3392 if (*logmsg == NULL)
3393 return got_error_from_errno("malloc");
3394 strlcpy(*logmsg, a->cmdline_log, len);
3395 return NULL;
3398 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3399 return got_error_from_errno("asprintf");
3401 if (asprintf(&initial_content,
3402 "\n# changes to be committed on branch %s:\n",
3403 a->branch_name) == -1)
3404 return got_error_from_errno("asprintf");
3406 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3407 if (err)
3408 goto done;
3410 dprintf(fd, initial_content);
3412 TAILQ_FOREACH(pe, commitable_paths, entry) {
3413 struct got_commitable *ct = pe->data;
3414 dprintf(fd, "# %c %s\n",
3415 got_commitable_get_status(ct),
3416 got_commitable_get_path(ct));
3418 close(fd);
3420 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3421 done:
3422 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3423 unlink(a->logmsg_path);
3424 free(a->logmsg_path);
3425 a->logmsg_path = NULL;
3427 free(initial_content);
3428 free(template);
3430 /* Editor is done; we can now apply unveil(2) */
3431 if (err == NULL) {
3432 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3433 if (err) {
3434 free(*logmsg);
3435 *logmsg = NULL;
3438 return err;
3441 static const struct got_error *
3442 cmd_commit(int argc, char *argv[])
3444 const struct got_error *error = NULL;
3445 struct got_worktree *worktree = NULL;
3446 struct got_repository *repo = NULL;
3447 char *cwd = NULL, *id_str = NULL;
3448 struct got_object_id *id = NULL;
3449 const char *logmsg = NULL;
3450 const char *got_author = getenv("GOT_AUTHOR");
3451 struct collect_commit_logmsg_arg cl_arg;
3452 char *editor = NULL;
3453 int ch, rebase_in_progress, histedit_in_progress;
3454 struct got_pathlist_head paths;
3456 TAILQ_INIT(&paths);
3457 cl_arg.logmsg_path = NULL;
3459 while ((ch = getopt(argc, argv, "m:")) != -1) {
3460 switch (ch) {
3461 case 'm':
3462 logmsg = optarg;
3463 break;
3464 default:
3465 usage_commit();
3466 /* NOTREACHED */
3470 argc -= optind;
3471 argv += optind;
3473 #ifndef PROFILE
3474 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3475 "unveil", NULL) == -1)
3476 err(1, "pledge");
3477 #endif
3478 if (got_author == NULL) {
3479 /* TODO: Look current user up in password database */
3480 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3481 goto done;
3484 cwd = getcwd(NULL, 0);
3485 if (cwd == NULL) {
3486 error = got_error_from_errno("getcwd");
3487 goto done;
3489 error = got_worktree_open(&worktree, cwd);
3490 if (error)
3491 goto done;
3493 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3494 if (error)
3495 goto done;
3496 if (rebase_in_progress) {
3497 error = got_error(GOT_ERR_REBASING);
3498 goto done;
3501 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3502 worktree);
3503 if (error)
3504 goto done;
3506 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3507 if (error != NULL)
3508 goto done;
3511 * unveil(2) traverses exec(2); if an editor is used we have
3512 * to apply unveil after the log message has been written.
3514 if (logmsg == NULL || strlen(logmsg) == 0)
3515 error = get_editor(&editor);
3516 else
3517 error = apply_unveil(got_repo_get_path(repo), 0,
3518 got_worktree_get_root_path(worktree));
3519 if (error)
3520 goto done;
3522 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3523 if (error)
3524 goto done;
3526 cl_arg.editor = editor;
3527 cl_arg.cmdline_log = logmsg;
3528 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3529 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3530 if (!histedit_in_progress) {
3531 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3532 error = got_error(GOT_ERR_COMMIT_BRANCH);
3533 goto done;
3535 cl_arg.branch_name += 11;
3537 cl_arg.repo_path = got_repo_get_path(repo);
3538 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3539 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3540 if (error) {
3541 if (cl_arg.logmsg_path)
3542 fprintf(stderr, "%s: log message preserved in %s\n",
3543 getprogname(), cl_arg.logmsg_path);
3544 goto done;
3547 if (cl_arg.logmsg_path)
3548 unlink(cl_arg.logmsg_path);
3550 error = got_object_id_str(&id_str, id);
3551 if (error)
3552 goto done;
3553 printf("Created commit %s\n", id_str);
3554 done:
3555 free(cl_arg.logmsg_path);
3556 if (repo)
3557 got_repo_close(repo);
3558 if (worktree)
3559 got_worktree_close(worktree);
3560 free(cwd);
3561 free(id_str);
3562 free(editor);
3563 return error;
3566 __dead static void
3567 usage_cherrypick(void)
3569 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3570 exit(1);
3573 static const struct got_error *
3574 cmd_cherrypick(int argc, char *argv[])
3576 const struct got_error *error = NULL;
3577 struct got_worktree *worktree = NULL;
3578 struct got_repository *repo = NULL;
3579 char *cwd = NULL, *commit_id_str = NULL;
3580 struct got_object_id *commit_id = NULL;
3581 struct got_commit_object *commit = NULL;
3582 struct got_object_qid *pid;
3583 struct got_reference *head_ref = NULL;
3584 int ch, did_something = 0;
3586 while ((ch = getopt(argc, argv, "")) != -1) {
3587 switch (ch) {
3588 default:
3589 usage_cherrypick();
3590 /* NOTREACHED */
3594 argc -= optind;
3595 argv += optind;
3597 #ifndef PROFILE
3598 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3599 "unveil", NULL) == -1)
3600 err(1, "pledge");
3601 #endif
3602 if (argc != 1)
3603 usage_cherrypick();
3605 cwd = getcwd(NULL, 0);
3606 if (cwd == NULL) {
3607 error = got_error_from_errno("getcwd");
3608 goto done;
3610 error = got_worktree_open(&worktree, cwd);
3611 if (error)
3612 goto done;
3614 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3615 if (error != NULL)
3616 goto done;
3618 error = apply_unveil(got_repo_get_path(repo), 0,
3619 got_worktree_get_root_path(worktree));
3620 if (error)
3621 goto done;
3623 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3624 GOT_OBJ_TYPE_COMMIT, repo);
3625 if (error != NULL) {
3626 struct got_reference *ref;
3627 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3628 goto done;
3629 error = got_ref_open(&ref, repo, argv[0], 0);
3630 if (error != NULL)
3631 goto done;
3632 error = got_ref_resolve(&commit_id, repo, ref);
3633 got_ref_close(ref);
3634 if (error != NULL)
3635 goto done;
3637 error = got_object_id_str(&commit_id_str, commit_id);
3638 if (error)
3639 goto done;
3641 error = got_ref_open(&head_ref, repo,
3642 got_worktree_get_head_ref_name(worktree), 0);
3643 if (error != NULL)
3644 goto done;
3646 error = check_same_branch(commit_id, head_ref, NULL, repo);
3647 if (error) {
3648 if (error->code != GOT_ERR_ANCESTRY)
3649 goto done;
3650 error = NULL;
3651 } else {
3652 error = got_error(GOT_ERR_SAME_BRANCH);
3653 goto done;
3656 error = got_object_open_as_commit(&commit, repo, commit_id);
3657 if (error)
3658 goto done;
3659 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3660 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3661 commit_id, repo, update_progress, &did_something, check_cancelled,
3662 NULL);
3663 if (error != NULL)
3664 goto done;
3666 if (did_something)
3667 printf("Merged commit %s\n", commit_id_str);
3668 done:
3669 if (commit)
3670 got_object_commit_close(commit);
3671 free(commit_id_str);
3672 if (head_ref)
3673 got_ref_close(head_ref);
3674 if (worktree)
3675 got_worktree_close(worktree);
3676 if (repo)
3677 got_repo_close(repo);
3678 return error;
3681 __dead static void
3682 usage_backout(void)
3684 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3685 exit(1);
3688 static const struct got_error *
3689 cmd_backout(int argc, char *argv[])
3691 const struct got_error *error = NULL;
3692 struct got_worktree *worktree = NULL;
3693 struct got_repository *repo = NULL;
3694 char *cwd = NULL, *commit_id_str = NULL;
3695 struct got_object_id *commit_id = NULL;
3696 struct got_commit_object *commit = NULL;
3697 struct got_object_qid *pid;
3698 struct got_reference *head_ref = NULL;
3699 int ch, did_something = 0;
3701 while ((ch = getopt(argc, argv, "")) != -1) {
3702 switch (ch) {
3703 default:
3704 usage_backout();
3705 /* NOTREACHED */
3709 argc -= optind;
3710 argv += optind;
3712 #ifndef PROFILE
3713 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3714 "unveil", NULL) == -1)
3715 err(1, "pledge");
3716 #endif
3717 if (argc != 1)
3718 usage_backout();
3720 cwd = getcwd(NULL, 0);
3721 if (cwd == NULL) {
3722 error = got_error_from_errno("getcwd");
3723 goto done;
3725 error = got_worktree_open(&worktree, cwd);
3726 if (error)
3727 goto done;
3729 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3730 if (error != NULL)
3731 goto done;
3733 error = apply_unveil(got_repo_get_path(repo), 0,
3734 got_worktree_get_root_path(worktree));
3735 if (error)
3736 goto done;
3738 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3739 GOT_OBJ_TYPE_COMMIT, repo);
3740 if (error != NULL) {
3741 struct got_reference *ref;
3742 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3743 goto done;
3744 error = got_ref_open(&ref, repo, argv[0], 0);
3745 if (error != NULL)
3746 goto done;
3747 error = got_ref_resolve(&commit_id, repo, ref);
3748 got_ref_close(ref);
3749 if (error != NULL)
3750 goto done;
3752 error = got_object_id_str(&commit_id_str, commit_id);
3753 if (error)
3754 goto done;
3756 error = got_ref_open(&head_ref, repo,
3757 got_worktree_get_head_ref_name(worktree), 0);
3758 if (error != NULL)
3759 goto done;
3761 error = check_same_branch(commit_id, head_ref, NULL, repo);
3762 if (error)
3763 goto done;
3765 error = got_object_open_as_commit(&commit, repo, commit_id);
3766 if (error)
3767 goto done;
3768 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3769 if (pid == NULL) {
3770 error = got_error(GOT_ERR_ROOT_COMMIT);
3771 goto done;
3774 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3775 update_progress, &did_something, check_cancelled, NULL);
3776 if (error != NULL)
3777 goto done;
3779 if (did_something)
3780 printf("Backed out commit %s\n", commit_id_str);
3781 done:
3782 if (commit)
3783 got_object_commit_close(commit);
3784 free(commit_id_str);
3785 if (head_ref)
3786 got_ref_close(head_ref);
3787 if (worktree)
3788 got_worktree_close(worktree);
3789 if (repo)
3790 got_repo_close(repo);
3791 return error;
3794 __dead static void
3795 usage_rebase(void)
3797 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3798 getprogname());
3799 exit(1);
3802 void
3803 trim_logmsg(char *logmsg, int limit)
3805 char *nl;
3806 size_t len;
3808 len = strlen(logmsg);
3809 if (len > limit)
3810 len = limit;
3811 logmsg[len] = '\0';
3812 nl = strchr(logmsg, '\n');
3813 if (nl)
3814 *nl = '\0';
3817 static const struct got_error *
3818 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3820 const char *logmsg0 = NULL;
3822 logmsg0 = got_object_commit_get_logmsg(commit);
3824 while (isspace((unsigned char)logmsg0[0]))
3825 logmsg0++;
3827 *logmsg = strdup(logmsg0);
3828 if (*logmsg == NULL)
3829 return got_error_from_errno("strdup");
3831 trim_logmsg(*logmsg, limit);
3832 return NULL;
3835 static const struct got_error *
3836 show_rebase_progress(struct got_commit_object *commit,
3837 struct got_object_id *old_id, struct got_object_id *new_id)
3839 const struct got_error *err;
3840 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3842 err = got_object_id_str(&old_id_str, old_id);
3843 if (err)
3844 goto done;
3846 if (new_id) {
3847 err = got_object_id_str(&new_id_str, new_id);
3848 if (err)
3849 goto done;
3852 old_id_str[12] = '\0';
3853 if (new_id_str)
3854 new_id_str[12] = '\0';
3856 err = get_short_logmsg(&logmsg, 42, commit);
3857 if (err)
3858 goto done;
3860 printf("%s -> %s: %s\n", old_id_str,
3861 new_id_str ? new_id_str : "no-op change", logmsg);
3862 done:
3863 free(old_id_str);
3864 free(new_id_str);
3865 return err;
3868 static const struct got_error *
3869 rebase_progress(void *arg, unsigned char status, const char *path)
3871 unsigned char *rebase_status = arg;
3873 while (path[0] == '/')
3874 path++;
3875 printf("%c %s\n", status, path);
3877 if (*rebase_status == GOT_STATUS_CONFLICT)
3878 return NULL;
3879 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3880 *rebase_status = status;
3881 return NULL;
3884 static const struct got_error *
3885 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3886 struct got_reference *branch, struct got_reference *new_base_branch,
3887 struct got_reference *tmp_branch, struct got_repository *repo)
3889 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3890 return got_worktree_rebase_complete(worktree, fileindex,
3891 new_base_branch, tmp_branch, branch, repo);
3894 static const struct got_error *
3895 rebase_commit(struct got_pathlist_head *merged_paths,
3896 struct got_worktree *worktree, struct got_fileindex *fileindex,
3897 struct got_reference *tmp_branch,
3898 struct got_object_id *commit_id, struct got_repository *repo)
3900 const struct got_error *error;
3901 struct got_commit_object *commit;
3902 struct got_object_id *new_commit_id;
3904 error = got_object_open_as_commit(&commit, repo, commit_id);
3905 if (error)
3906 return error;
3908 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3909 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3910 if (error) {
3911 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3912 goto done;
3913 error = show_rebase_progress(commit, commit_id, NULL);
3914 } else {
3915 error = show_rebase_progress(commit, commit_id, new_commit_id);
3916 free(new_commit_id);
3918 done:
3919 got_object_commit_close(commit);
3920 return error;
3923 struct check_path_prefix_arg {
3924 const char *path_prefix;
3925 size_t len;
3926 int errcode;
3929 static const struct got_error *
3930 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3931 struct got_blob_object *blob2, struct got_object_id *id1,
3932 struct got_object_id *id2, const char *path1, const char *path2,
3933 struct got_repository *repo)
3935 struct check_path_prefix_arg *a = arg;
3937 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3938 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3939 return got_error(a->errcode);
3941 return NULL;
3944 static const struct got_error *
3945 check_path_prefix(struct got_object_id *parent_id,
3946 struct got_object_id *commit_id, const char *path_prefix,
3947 int errcode, struct got_repository *repo)
3949 const struct got_error *err;
3950 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3951 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3952 struct check_path_prefix_arg cpp_arg;
3954 if (got_path_is_root_dir(path_prefix))
3955 return NULL;
3957 err = got_object_open_as_commit(&commit, repo, commit_id);
3958 if (err)
3959 goto done;
3961 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3962 if (err)
3963 goto done;
3965 err = got_object_open_as_tree(&tree1, repo,
3966 got_object_commit_get_tree_id(parent_commit));
3967 if (err)
3968 goto done;
3970 err = got_object_open_as_tree(&tree2, repo,
3971 got_object_commit_get_tree_id(commit));
3972 if (err)
3973 goto done;
3975 cpp_arg.path_prefix = path_prefix;
3976 while (cpp_arg.path_prefix[0] == '/')
3977 cpp_arg.path_prefix++;
3978 cpp_arg.len = strlen(cpp_arg.path_prefix);
3979 cpp_arg.errcode = errcode;
3980 err = got_diff_tree(tree1, tree2, "", "", repo,
3981 check_path_prefix_in_diff, &cpp_arg, 0);
3982 done:
3983 if (tree1)
3984 got_object_tree_close(tree1);
3985 if (tree2)
3986 got_object_tree_close(tree2);
3987 if (commit)
3988 got_object_commit_close(commit);
3989 if (parent_commit)
3990 got_object_commit_close(parent_commit);
3991 return err;
3994 static const struct got_error *
3995 collect_commits(struct got_object_id_queue *commits,
3996 struct got_object_id *initial_commit_id,
3997 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3998 const char *path_prefix, int path_prefix_errcode,
3999 struct got_repository *repo)
4001 const struct got_error *err = NULL;
4002 struct got_commit_graph *graph = NULL;
4003 struct got_object_id *parent_id = NULL;
4004 struct got_object_qid *qid;
4005 struct got_object_id *commit_id = initial_commit_id;
4007 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4008 if (err)
4009 return err;
4011 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4012 if (err)
4013 goto done;
4014 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4015 err = got_commit_graph_iter_next(&parent_id, graph);
4016 if (err) {
4017 if (err->code == GOT_ERR_ITER_COMPLETED) {
4018 err = got_error_msg(GOT_ERR_ANCESTRY,
4019 "ran out of commits to rebase before "
4020 "youngest common ancestor commit has "
4021 "been reached?!?");
4022 goto done;
4023 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4024 goto done;
4025 err = got_commit_graph_fetch_commits(graph, 1, repo);
4026 if (err)
4027 goto done;
4028 } else {
4029 err = check_path_prefix(parent_id, commit_id,
4030 path_prefix, path_prefix_errcode, repo);
4031 if (err)
4032 goto done;
4034 err = got_object_qid_alloc(&qid, commit_id);
4035 if (err)
4036 goto done;
4037 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4038 commit_id = parent_id;
4041 done:
4042 got_commit_graph_close(graph);
4043 return err;
4046 static const struct got_error *
4047 cmd_rebase(int argc, char *argv[])
4049 const struct got_error *error = NULL;
4050 struct got_worktree *worktree = NULL;
4051 struct got_repository *repo = NULL;
4052 struct got_fileindex *fileindex = NULL;
4053 char *cwd = NULL;
4054 struct got_reference *branch = NULL;
4055 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4056 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4057 struct got_object_id *resume_commit_id = NULL;
4058 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4059 struct got_commit_object *commit = NULL;
4060 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4061 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4062 struct got_object_id_queue commits;
4063 struct got_pathlist_head merged_paths;
4064 const struct got_object_id_queue *parent_ids;
4065 struct got_object_qid *qid, *pid;
4067 SIMPLEQ_INIT(&commits);
4068 TAILQ_INIT(&merged_paths);
4070 while ((ch = getopt(argc, argv, "ac")) != -1) {
4071 switch (ch) {
4072 case 'a':
4073 abort_rebase = 1;
4074 break;
4075 case 'c':
4076 continue_rebase = 1;
4077 break;
4078 default:
4079 usage_rebase();
4080 /* NOTREACHED */
4084 argc -= optind;
4085 argv += optind;
4087 #ifndef PROFILE
4088 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4089 "unveil", NULL) == -1)
4090 err(1, "pledge");
4091 #endif
4092 if (abort_rebase && continue_rebase)
4093 usage_rebase();
4094 else if (abort_rebase || continue_rebase) {
4095 if (argc != 0)
4096 usage_rebase();
4097 } else if (argc != 1)
4098 usage_rebase();
4100 cwd = getcwd(NULL, 0);
4101 if (cwd == NULL) {
4102 error = got_error_from_errno("getcwd");
4103 goto done;
4105 error = got_worktree_open(&worktree, cwd);
4106 if (error)
4107 goto done;
4109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4110 if (error != NULL)
4111 goto done;
4113 error = apply_unveil(got_repo_get_path(repo), 0,
4114 got_worktree_get_root_path(worktree));
4115 if (error)
4116 goto done;
4118 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4119 if (error)
4120 goto done;
4122 if (abort_rebase) {
4123 int did_something;
4124 if (!rebase_in_progress) {
4125 error = got_error(GOT_ERR_NOT_REBASING);
4126 goto done;
4128 error = got_worktree_rebase_continue(&resume_commit_id,
4129 &new_base_branch, &tmp_branch, &branch, &fileindex,
4130 worktree, repo);
4131 if (error)
4132 goto done;
4133 printf("Switching work tree to %s\n",
4134 got_ref_get_symref_target(new_base_branch));
4135 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4136 new_base_branch, update_progress, &did_something);
4137 if (error)
4138 goto done;
4139 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4140 goto done; /* nothing else to do */
4143 if (continue_rebase) {
4144 if (!rebase_in_progress) {
4145 error = got_error(GOT_ERR_NOT_REBASING);
4146 goto done;
4148 error = got_worktree_rebase_continue(&resume_commit_id,
4149 &new_base_branch, &tmp_branch, &branch, &fileindex,
4150 worktree, repo);
4151 if (error)
4152 goto done;
4154 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4155 resume_commit_id, repo);
4156 if (error)
4157 goto done;
4159 yca_id = got_object_id_dup(resume_commit_id);
4160 if (yca_id == NULL) {
4161 error = got_error_from_errno("got_object_id_dup");
4162 goto done;
4164 } else {
4165 error = got_ref_open(&branch, repo, argv[0], 0);
4166 if (error != NULL)
4167 goto done;
4170 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4171 if (error)
4172 goto done;
4174 if (!continue_rebase) {
4175 struct got_object_id *base_commit_id;
4177 base_commit_id = got_worktree_get_base_commit_id(worktree);
4178 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4179 base_commit_id, branch_head_commit_id, repo);
4180 if (error)
4181 goto done;
4182 if (yca_id == NULL) {
4183 error = got_error_msg(GOT_ERR_ANCESTRY,
4184 "specified branch shares no common ancestry "
4185 "with work tree's branch");
4186 goto done;
4189 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4190 if (error) {
4191 if (error->code != GOT_ERR_ANCESTRY)
4192 goto done;
4193 error = NULL;
4194 } else {
4195 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4196 "specified branch resolves to a commit which "
4197 "is already contained in work tree's branch");
4198 goto done;
4200 error = got_worktree_rebase_prepare(&new_base_branch,
4201 &tmp_branch, &fileindex, worktree, branch, repo);
4202 if (error)
4203 goto done;
4206 commit_id = branch_head_commit_id;
4207 error = got_object_open_as_commit(&commit, repo, commit_id);
4208 if (error)
4209 goto done;
4211 parent_ids = got_object_commit_get_parent_ids(commit);
4212 pid = SIMPLEQ_FIRST(parent_ids);
4213 error = collect_commits(&commits, commit_id, pid->id,
4214 yca_id, got_worktree_get_path_prefix(worktree),
4215 GOT_ERR_REBASE_PATH, repo);
4216 got_object_commit_close(commit);
4217 commit = NULL;
4218 if (error)
4219 goto done;
4221 if (SIMPLEQ_EMPTY(&commits)) {
4222 if (continue_rebase)
4223 error = rebase_complete(worktree, fileindex,
4224 branch, new_base_branch, tmp_branch, repo);
4225 else
4226 error = got_error(GOT_ERR_EMPTY_REBASE);
4227 goto done;
4230 pid = NULL;
4231 SIMPLEQ_FOREACH(qid, &commits, entry) {
4232 commit_id = qid->id;
4233 parent_id = pid ? pid->id : yca_id;
4234 pid = qid;
4236 error = got_worktree_rebase_merge_files(&merged_paths,
4237 worktree, fileindex, parent_id, commit_id, repo,
4238 rebase_progress, &rebase_status, check_cancelled, NULL);
4239 if (error)
4240 goto done;
4242 if (rebase_status == GOT_STATUS_CONFLICT) {
4243 got_worktree_rebase_pathlist_free(&merged_paths);
4244 break;
4247 error = rebase_commit(&merged_paths, worktree, fileindex,
4248 tmp_branch, commit_id, repo);
4249 got_worktree_rebase_pathlist_free(&merged_paths);
4250 if (error)
4251 goto done;
4254 if (rebase_status == GOT_STATUS_CONFLICT) {
4255 error = got_worktree_rebase_postpone(worktree, fileindex);
4256 if (error)
4257 goto done;
4258 error = got_error_msg(GOT_ERR_CONFLICTS,
4259 "conflicts must be resolved before rebasing can continue");
4260 } else
4261 error = rebase_complete(worktree, fileindex, branch,
4262 new_base_branch, tmp_branch, repo);
4263 done:
4264 got_object_id_queue_free(&commits);
4265 free(branch_head_commit_id);
4266 free(resume_commit_id);
4267 free(yca_id);
4268 if (commit)
4269 got_object_commit_close(commit);
4270 if (branch)
4271 got_ref_close(branch);
4272 if (new_base_branch)
4273 got_ref_close(new_base_branch);
4274 if (tmp_branch)
4275 got_ref_close(tmp_branch);
4276 if (worktree)
4277 got_worktree_close(worktree);
4278 if (repo)
4279 got_repo_close(repo);
4280 return error;
4283 __dead static void
4284 usage_histedit(void)
4286 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4287 getprogname());
4288 exit(1);
4291 #define GOT_HISTEDIT_PICK 'p'
4292 #define GOT_HISTEDIT_EDIT 'e'
4293 #define GOT_HISTEDIT_FOLD 'f'
4294 #define GOT_HISTEDIT_DROP 'd'
4295 #define GOT_HISTEDIT_MESG 'm'
4297 static struct got_histedit_cmd {
4298 unsigned char code;
4299 const char *name;
4300 const char *desc;
4301 } got_histedit_cmds[] = {
4302 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4303 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4304 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4305 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4306 { GOT_HISTEDIT_MESG, "mesg",
4307 "single-line log message for commit above (open editor if empty)" },
4310 struct got_histedit_list_entry {
4311 TAILQ_ENTRY(got_histedit_list_entry) entry;
4312 struct got_object_id *commit_id;
4313 const struct got_histedit_cmd *cmd;
4314 char *logmsg;
4316 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4318 static const struct got_error *
4319 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4320 FILE *f, struct got_repository *repo)
4322 const struct got_error *err = NULL;
4323 char *logmsg = NULL, *id_str = NULL;
4324 struct got_commit_object *commit = NULL;
4325 size_t n;
4327 err = got_object_open_as_commit(&commit, repo, commit_id);
4328 if (err)
4329 goto done;
4331 err = get_short_logmsg(&logmsg, 34, commit);
4332 if (err)
4333 goto done;
4335 err = got_object_id_str(&id_str, commit_id);
4336 if (err)
4337 goto done;
4339 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4340 if (n < 0)
4341 err = got_ferror(f, GOT_ERR_IO);
4342 done:
4343 if (commit)
4344 got_object_commit_close(commit);
4345 free(id_str);
4346 free(logmsg);
4347 return err;
4350 static const struct got_error *
4351 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4352 struct got_repository *repo)
4354 const struct got_error *err = NULL;
4355 struct got_object_qid *qid;
4357 if (SIMPLEQ_EMPTY(commits))
4358 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4360 SIMPLEQ_FOREACH(qid, commits, entry) {
4361 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4362 f, repo);
4363 if (err)
4364 break;
4367 return err;
4370 static const struct got_error *
4371 write_cmd_list(FILE *f)
4373 const struct got_error *err = NULL;
4374 int n, i;
4376 n = fprintf(f, "# Available histedit commands:\n");
4377 if (n < 0)
4378 return got_ferror(f, GOT_ERR_IO);
4380 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4381 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4382 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4383 cmd->desc);
4384 if (n < 0) {
4385 err = got_ferror(f, GOT_ERR_IO);
4386 break;
4389 n = fprintf(f, "# Commits will be processed in order from top to "
4390 "bottom of this file.\n");
4391 if (n < 0)
4392 return got_ferror(f, GOT_ERR_IO);
4393 return err;
4396 static const struct got_error *
4397 histedit_syntax_error(int lineno)
4399 static char msg[42];
4400 int ret;
4402 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4403 lineno);
4404 if (ret == -1 || ret >= sizeof(msg))
4405 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4407 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4410 static const struct got_error *
4411 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4412 char *logmsg, struct got_repository *repo)
4414 const struct got_error *err;
4415 struct got_commit_object *folded_commit = NULL;
4416 char *id_str;
4418 err = got_object_id_str(&id_str, hle->commit_id);
4419 if (err)
4420 return err;
4422 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4423 if (err)
4424 goto done;
4426 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4427 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4428 got_object_commit_get_logmsg(folded_commit)) == -1) {
4429 err = got_error_from_errno("asprintf");
4430 goto done;
4432 done:
4433 if (folded_commit)
4434 got_object_commit_close(folded_commit);
4435 free(id_str);
4436 return err;
4439 static struct got_histedit_list_entry *
4440 get_folded_commits(struct got_histedit_list_entry *hle)
4442 struct got_histedit_list_entry *prev, *folded = NULL;
4444 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4445 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4446 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4447 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4448 folded = prev;
4449 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4452 return folded;
4455 static const struct got_error *
4456 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4457 struct got_repository *repo)
4459 char *logmsg_path = NULL, *id_str = NULL;
4460 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4461 const struct got_error *err = NULL;
4462 struct got_commit_object *commit = NULL;
4463 int fd;
4464 struct got_histedit_list_entry *folded = NULL;
4466 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4467 if (err)
4468 return err;
4470 folded = get_folded_commits(hle);
4471 if (folded) {
4472 while (folded != hle) {
4473 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4474 folded = TAILQ_NEXT(folded, entry);
4475 continue;
4477 err = append_folded_commit_msg(&new_msg, folded,
4478 logmsg, repo);
4479 if (err)
4480 goto done;
4481 free(logmsg);
4482 logmsg = new_msg;
4483 folded = TAILQ_NEXT(folded, entry);
4487 err = got_object_id_str(&id_str, hle->commit_id);
4488 if (err)
4489 goto done;
4490 if (asprintf(&new_msg,
4491 "%s\n# original log message of commit %s: %s",
4492 logmsg ? logmsg : "", id_str,
4493 got_object_commit_get_logmsg(commit)) == -1) {
4494 err = got_error_from_errno("asprintf");
4495 goto done;
4497 free(logmsg);
4498 logmsg = new_msg;
4500 err = got_object_id_str(&id_str, hle->commit_id);
4501 if (err)
4502 goto done;
4504 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4505 if (err)
4506 goto done;
4508 dprintf(fd, logmsg);
4509 close(fd);
4511 err = get_editor(&editor);
4512 if (err)
4513 goto done;
4515 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4516 if (err) {
4517 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4518 goto done;
4519 err = NULL;
4520 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4521 if (hle->logmsg == NULL)
4522 err = got_error_from_errno("strdup");
4524 done:
4525 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4526 err = got_error_from_errno2("unlink", logmsg_path);
4527 free(logmsg_path);
4528 free(logmsg);
4529 free(editor);
4530 if (commit)
4531 got_object_commit_close(commit);
4532 return err;
4535 static const struct got_error *
4536 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4537 FILE *f, struct got_repository *repo)
4539 const struct got_error *err = NULL;
4540 char *line = NULL, *p, *end;
4541 size_t size;
4542 ssize_t len;
4543 int lineno = 0, i;
4544 const struct got_histedit_cmd *cmd;
4545 struct got_object_id *commit_id = NULL;
4546 struct got_histedit_list_entry *hle = NULL;
4548 for (;;) {
4549 len = getline(&line, &size, f);
4550 if (len == -1) {
4551 const struct got_error *getline_err;
4552 if (feof(f))
4553 break;
4554 getline_err = got_error_from_errno("getline");
4555 err = got_ferror(f, getline_err->code);
4556 break;
4558 lineno++;
4559 p = line;
4560 while (isspace((unsigned char)p[0]))
4561 p++;
4562 if (p[0] == '#' || p[0] == '\0') {
4563 free(line);
4564 line = NULL;
4565 continue;
4567 cmd = NULL;
4568 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4569 cmd = &got_histedit_cmds[i];
4570 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4571 isspace((unsigned char)p[strlen(cmd->name)])) {
4572 p += strlen(cmd->name);
4573 break;
4575 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4576 p++;
4577 break;
4580 if (i == nitems(got_histedit_cmds)) {
4581 err = histedit_syntax_error(lineno);
4582 break;
4584 while (isspace((unsigned char)p[0]))
4585 p++;
4586 if (cmd->code == GOT_HISTEDIT_MESG) {
4587 if (hle == NULL || hle->logmsg != NULL) {
4588 err = got_error(GOT_ERR_HISTEDIT_CMD);
4589 break;
4591 if (p[0] == '\0') {
4592 err = histedit_edit_logmsg(hle, repo);
4593 if (err)
4594 break;
4595 } else {
4596 hle->logmsg = strdup(p);
4597 if (hle->logmsg == NULL) {
4598 err = got_error_from_errno("strdup");
4599 break;
4602 free(line);
4603 line = NULL;
4604 continue;
4605 } else {
4606 end = p;
4607 while (end[0] && !isspace((unsigned char)end[0]))
4608 end++;
4609 *end = '\0';
4611 err = got_object_resolve_id_str(&commit_id, repo, p);
4612 if (err) {
4613 /* override error code */
4614 err = histedit_syntax_error(lineno);
4615 break;
4618 hle = malloc(sizeof(*hle));
4619 if (hle == NULL) {
4620 err = got_error_from_errno("malloc");
4621 break;
4623 hle->cmd = cmd;
4624 hle->commit_id = commit_id;
4625 hle->logmsg = NULL;
4626 commit_id = NULL;
4627 free(line);
4628 line = NULL;
4629 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4632 free(line);
4633 free(commit_id);
4634 return err;
4637 static const struct got_error *
4638 histedit_check_script(struct got_histedit_list *histedit_cmds,
4639 struct got_object_id_queue *commits, struct got_repository *repo)
4641 const struct got_error *err = NULL;
4642 struct got_object_qid *qid;
4643 struct got_histedit_list_entry *hle;
4644 static char msg[80];
4645 char *id_str;
4647 if (TAILQ_EMPTY(histedit_cmds))
4648 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4649 "histedit script contains no commands");
4651 SIMPLEQ_FOREACH(qid, commits, entry) {
4652 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4653 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4654 break;
4656 if (hle == NULL) {
4657 err = got_object_id_str(&id_str, qid->id);
4658 if (err)
4659 return err;
4660 snprintf(msg, sizeof(msg),
4661 "commit %s missing from histedit script", id_str);
4662 free(id_str);
4663 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4667 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4668 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4669 "last commit in histedit script cannot be folded");
4671 return NULL;
4674 static const struct got_error *
4675 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4676 const char *path, struct got_object_id_queue *commits,
4677 struct got_repository *repo)
4679 const struct got_error *err = NULL;
4680 char *editor;
4681 FILE *f = NULL;
4683 err = get_editor(&editor);
4684 if (err)
4685 return err;
4687 if (spawn_editor(editor, path) == -1) {
4688 err = got_error_from_errno("failed spawning editor");
4689 goto done;
4692 f = fopen(path, "r");
4693 if (f == NULL) {
4694 err = got_error_from_errno("fopen");
4695 goto done;
4697 err = histedit_parse_list(histedit_cmds, f, repo);
4698 if (err)
4699 goto done;
4701 err = histedit_check_script(histedit_cmds, commits, repo);
4702 done:
4703 if (f && fclose(f) != 0 && err == NULL)
4704 err = got_error_from_errno("fclose");
4705 free(editor);
4706 return err;
4709 static const struct got_error *
4710 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4711 struct got_object_id_queue *, const char *, struct got_repository *);
4713 static const struct got_error *
4714 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4715 struct got_object_id_queue *commits, struct got_repository *repo)
4717 const struct got_error *err;
4718 FILE *f = NULL;
4719 char *path = NULL;
4721 err = got_opentemp_named(&path, &f, "got-histedit");
4722 if (err)
4723 return err;
4725 err = write_cmd_list(f);
4726 if (err)
4727 goto done;
4729 err = histedit_write_commit_list(commits, f, repo);
4730 if (err)
4731 goto done;
4733 if (fclose(f) != 0) {
4734 err = got_error_from_errno("fclose");
4735 goto done;
4737 f = NULL;
4739 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4740 if (err) {
4741 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4742 err->code != GOT_ERR_HISTEDIT_CMD)
4743 goto done;
4744 err = histedit_edit_list_retry(histedit_cmds, err,
4745 commits, path, repo);
4747 done:
4748 if (f && fclose(f) != 0 && err == NULL)
4749 err = got_error_from_errno("fclose");
4750 if (path && unlink(path) != 0 && err == NULL)
4751 err = got_error_from_errno2("unlink", path);
4752 free(path);
4753 return err;
4756 static const struct got_error *
4757 histedit_save_list(struct got_histedit_list *histedit_cmds,
4758 struct got_worktree *worktree, struct got_repository *repo)
4760 const struct got_error *err = NULL;
4761 char *path = NULL;
4762 FILE *f = NULL;
4763 struct got_histedit_list_entry *hle;
4764 struct got_commit_object *commit = NULL;
4766 err = got_worktree_get_histedit_script_path(&path, worktree);
4767 if (err)
4768 return err;
4770 f = fopen(path, "w");
4771 if (f == NULL) {
4772 err = got_error_from_errno2("fopen", path);
4773 goto done;
4775 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4776 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4777 repo);
4778 if (err)
4779 break;
4781 if (hle->logmsg) {
4782 int n = fprintf(f, "%c %s\n",
4783 GOT_HISTEDIT_MESG, hle->logmsg);
4784 if (n < 0) {
4785 err = got_ferror(f, GOT_ERR_IO);
4786 break;
4790 done:
4791 if (f && fclose(f) != 0 && err == NULL)
4792 err = got_error_from_errno("fclose");
4793 free(path);
4794 if (commit)
4795 got_object_commit_close(commit);
4796 return err;
4799 void
4800 histedit_free_list(struct got_histedit_list *histedit_cmds)
4802 struct got_histedit_list_entry *hle;
4804 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4805 TAILQ_REMOVE(histedit_cmds, hle, entry);
4806 free(hle);
4810 static const struct got_error *
4811 histedit_load_list(struct got_histedit_list *histedit_cmds,
4812 const char *path, struct got_repository *repo)
4814 const struct got_error *err = NULL;
4815 FILE *f = NULL;
4817 f = fopen(path, "r");
4818 if (f == NULL) {
4819 err = got_error_from_errno2("fopen", path);
4820 goto done;
4823 err = histedit_parse_list(histedit_cmds, f, repo);
4824 done:
4825 if (f && fclose(f) != 0 && err == NULL)
4826 err = got_error_from_errno("fclose");
4827 return err;
4830 static const struct got_error *
4831 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4832 const struct got_error *edit_err, struct got_object_id_queue *commits,
4833 const char *path, struct got_repository *repo)
4835 const struct got_error *err = NULL, *prev_err = edit_err;
4836 int resp = ' ';
4838 while (resp != 'c' && resp != 'r' && resp != 'a') {
4839 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4840 "or (a)bort: ", getprogname(), prev_err->msg);
4841 resp = getchar();
4842 if (resp == '\n')
4843 resp = getchar();
4844 if (resp == 'c') {
4845 histedit_free_list(histedit_cmds);
4846 err = histedit_run_editor(histedit_cmds, path, commits,
4847 repo);
4848 if (err) {
4849 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4850 err->code != GOT_ERR_HISTEDIT_CMD)
4851 break;
4852 prev_err = err;
4853 resp = ' ';
4854 continue;
4856 break;
4857 } else if (resp == 'r') {
4858 histedit_free_list(histedit_cmds);
4859 err = histedit_edit_script(histedit_cmds,
4860 commits, repo);
4861 if (err) {
4862 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4863 err->code != GOT_ERR_HISTEDIT_CMD)
4864 break;
4865 prev_err = err;
4866 resp = ' ';
4867 continue;
4869 break;
4870 } else if (resp == 'a') {
4871 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4872 break;
4873 } else
4874 printf("invalid response '%c'\n", resp);
4877 return err;
4880 static const struct got_error *
4881 histedit_complete(struct got_worktree *worktree,
4882 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4883 struct got_reference *branch, struct got_repository *repo)
4885 printf("Switching work tree to %s\n",
4886 got_ref_get_symref_target(branch));
4887 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4888 branch, repo);
4891 static const struct got_error *
4892 show_histedit_progress(struct got_commit_object *commit,
4893 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4895 const struct got_error *err;
4896 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4898 err = got_object_id_str(&old_id_str, hle->commit_id);
4899 if (err)
4900 goto done;
4902 if (new_id) {
4903 err = got_object_id_str(&new_id_str, new_id);
4904 if (err)
4905 goto done;
4908 old_id_str[12] = '\0';
4909 if (new_id_str)
4910 new_id_str[12] = '\0';
4912 if (hle->logmsg) {
4913 logmsg = strdup(hle->logmsg);
4914 if (logmsg == NULL) {
4915 err = got_error_from_errno("strdup");
4916 goto done;
4918 trim_logmsg(logmsg, 42);
4919 } else {
4920 err = get_short_logmsg(&logmsg, 42, commit);
4921 if (err)
4922 goto done;
4925 switch (hle->cmd->code) {
4926 case GOT_HISTEDIT_PICK:
4927 case GOT_HISTEDIT_EDIT:
4928 printf("%s -> %s: %s\n", old_id_str,
4929 new_id_str ? new_id_str : "no-op change", logmsg);
4930 break;
4931 case GOT_HISTEDIT_DROP:
4932 case GOT_HISTEDIT_FOLD:
4933 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4934 logmsg);
4935 break;
4936 default:
4937 break;
4940 done:
4941 free(old_id_str);
4942 free(new_id_str);
4943 return err;
4946 static const struct got_error *
4947 histedit_commit(struct got_pathlist_head *merged_paths,
4948 struct got_worktree *worktree, struct got_fileindex *fileindex,
4949 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4950 struct got_repository *repo)
4952 const struct got_error *err;
4953 struct got_commit_object *commit;
4954 struct got_object_id *new_commit_id;
4956 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4957 && hle->logmsg == NULL) {
4958 err = histedit_edit_logmsg(hle, repo);
4959 if (err)
4960 return err;
4963 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4964 if (err)
4965 return err;
4967 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4968 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4969 hle->logmsg, repo);
4970 if (err) {
4971 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4972 goto done;
4973 err = show_histedit_progress(commit, hle, NULL);
4974 } else {
4975 err = show_histedit_progress(commit, hle, new_commit_id);
4976 free(new_commit_id);
4978 done:
4979 got_object_commit_close(commit);
4980 return err;
4983 static const struct got_error *
4984 histedit_skip_commit(struct got_histedit_list_entry *hle,
4985 struct got_worktree *worktree, struct got_repository *repo)
4987 const struct got_error *error;
4988 struct got_commit_object *commit;
4990 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4991 repo);
4992 if (error)
4993 return error;
4995 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4996 if (error)
4997 return error;
4999 error = show_histedit_progress(commit, hle, NULL);
5000 got_object_commit_close(commit);
5001 return error;
5004 static const struct got_error *
5005 cmd_histedit(int argc, char *argv[])
5007 const struct got_error *error = NULL;
5008 struct got_worktree *worktree = NULL;
5009 struct got_fileindex *fileindex = NULL;
5010 struct got_repository *repo = NULL;
5011 char *cwd = NULL;
5012 struct got_reference *branch = NULL;
5013 struct got_reference *tmp_branch = NULL;
5014 struct got_object_id *resume_commit_id = NULL;
5015 struct got_object_id *base_commit_id = NULL;
5016 struct got_object_id *head_commit_id = NULL;
5017 struct got_commit_object *commit = NULL;
5018 int ch, rebase_in_progress = 0, did_something;
5019 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5020 const char *edit_script_path = NULL;
5021 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5022 struct got_object_id_queue commits;
5023 struct got_pathlist_head merged_paths;
5024 const struct got_object_id_queue *parent_ids;
5025 struct got_object_qid *pid;
5026 struct got_histedit_list histedit_cmds;
5027 struct got_histedit_list_entry *hle;
5029 SIMPLEQ_INIT(&commits);
5030 TAILQ_INIT(&histedit_cmds);
5031 TAILQ_INIT(&merged_paths);
5033 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5034 switch (ch) {
5035 case 'a':
5036 abort_edit = 1;
5037 break;
5038 case 'c':
5039 continue_edit = 1;
5040 break;
5041 case 'F':
5042 edit_script_path = optarg;
5043 break;
5044 default:
5045 usage_histedit();
5046 /* NOTREACHED */
5050 argc -= optind;
5051 argv += optind;
5053 #ifndef PROFILE
5054 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5055 "unveil", NULL) == -1)
5056 err(1, "pledge");
5057 #endif
5058 if (abort_edit && continue_edit)
5059 usage_histedit();
5060 if (argc != 0)
5061 usage_histedit();
5064 * This command cannot apply unveil(2) in all cases because the
5065 * user may choose to run an editor to edit the histedit script
5066 * and to edit individual commit log messages.
5067 * unveil(2) traverses exec(2); if an editor is used we have to
5068 * apply unveil after edit script and log messages have been written.
5069 * XXX TODO: Make use of unveil(2) where possible.
5072 cwd = getcwd(NULL, 0);
5073 if (cwd == NULL) {
5074 error = got_error_from_errno("getcwd");
5075 goto done;
5077 error = got_worktree_open(&worktree, cwd);
5078 if (error)
5079 goto done;
5081 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5082 if (error != NULL)
5083 goto done;
5085 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5086 if (error)
5087 goto done;
5088 if (rebase_in_progress) {
5089 error = got_error(GOT_ERR_REBASING);
5090 goto done;
5093 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5094 if (error)
5095 goto done;
5097 if (edit_in_progress && abort_edit) {
5098 error = got_worktree_histedit_continue(&resume_commit_id,
5099 &tmp_branch, &branch, &base_commit_id, &fileindex,
5100 worktree, repo);
5101 if (error)
5102 goto done;
5103 printf("Switching work tree to %s\n",
5104 got_ref_get_symref_target(branch));
5105 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5106 branch, base_commit_id, update_progress, &did_something);
5107 if (error)
5108 goto done;
5109 printf("Histedit of %s aborted\n",
5110 got_ref_get_symref_target(branch));
5111 goto done; /* nothing else to do */
5112 } else if (abort_edit) {
5113 error = got_error(GOT_ERR_NOT_HISTEDIT);
5114 goto done;
5117 if (continue_edit) {
5118 char *path;
5120 if (!edit_in_progress) {
5121 error = got_error(GOT_ERR_NOT_HISTEDIT);
5122 goto done;
5125 error = got_worktree_get_histedit_script_path(&path, worktree);
5126 if (error)
5127 goto done;
5129 error = histedit_load_list(&histedit_cmds, path, repo);
5130 free(path);
5131 if (error)
5132 goto done;
5134 error = got_worktree_histedit_continue(&resume_commit_id,
5135 &tmp_branch, &branch, &base_commit_id, &fileindex,
5136 worktree, repo);
5137 if (error)
5138 goto done;
5140 error = got_ref_resolve(&head_commit_id, repo, branch);
5141 if (error)
5142 goto done;
5144 error = got_object_open_as_commit(&commit, repo,
5145 head_commit_id);
5146 if (error)
5147 goto done;
5148 parent_ids = got_object_commit_get_parent_ids(commit);
5149 pid = SIMPLEQ_FIRST(parent_ids);
5150 if (pid == NULL) {
5151 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5152 goto done;
5154 error = collect_commits(&commits, head_commit_id, pid->id,
5155 base_commit_id, got_worktree_get_path_prefix(worktree),
5156 GOT_ERR_HISTEDIT_PATH, repo);
5157 got_object_commit_close(commit);
5158 commit = NULL;
5159 if (error)
5160 goto done;
5161 } else {
5162 if (edit_in_progress) {
5163 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5164 goto done;
5167 error = got_ref_open(&branch, repo,
5168 got_worktree_get_head_ref_name(worktree), 0);
5169 if (error != NULL)
5170 goto done;
5172 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5173 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5174 "will not edit commit history of a branch outside "
5175 "the \"refs/heads/\" reference namespace");
5176 goto done;
5179 error = got_ref_resolve(&head_commit_id, repo, branch);
5180 got_ref_close(branch);
5181 branch = NULL;
5182 if (error)
5183 goto done;
5185 error = got_object_open_as_commit(&commit, repo,
5186 head_commit_id);
5187 if (error)
5188 goto done;
5189 parent_ids = got_object_commit_get_parent_ids(commit);
5190 pid = SIMPLEQ_FIRST(parent_ids);
5191 if (pid == NULL) {
5192 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5193 goto done;
5195 error = collect_commits(&commits, head_commit_id, pid->id,
5196 got_worktree_get_base_commit_id(worktree),
5197 got_worktree_get_path_prefix(worktree),
5198 GOT_ERR_HISTEDIT_PATH, repo);
5199 got_object_commit_close(commit);
5200 commit = NULL;
5201 if (error)
5202 goto done;
5204 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5205 &base_commit_id, &fileindex, worktree, repo);
5206 if (error)
5207 goto done;
5209 if (edit_script_path) {
5210 error = histedit_load_list(&histedit_cmds,
5211 edit_script_path, repo);
5212 if (error) {
5213 got_worktree_histedit_abort(worktree, fileindex,
5214 repo, branch, base_commit_id,
5215 update_progress, &did_something);
5216 goto done;
5218 } else {
5219 error = histedit_edit_script(&histedit_cmds, &commits,
5220 repo);
5221 if (error) {
5222 got_worktree_histedit_abort(worktree, fileindex,
5223 repo, branch, base_commit_id,
5224 update_progress, &did_something);
5225 goto done;
5230 error = histedit_save_list(&histedit_cmds, worktree,
5231 repo);
5232 if (error) {
5233 got_worktree_histedit_abort(worktree, fileindex,
5234 repo, branch, base_commit_id,
5235 update_progress, &did_something);
5236 goto done;
5241 error = histedit_check_script(&histedit_cmds, &commits, repo);
5242 if (error)
5243 goto done;
5245 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5246 if (resume_commit_id) {
5247 if (got_object_id_cmp(hle->commit_id,
5248 resume_commit_id) != 0)
5249 continue;
5251 resume_commit_id = NULL;
5252 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5253 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5254 error = histedit_skip_commit(hle, worktree,
5255 repo);
5256 } else {
5257 error = histedit_commit(NULL, worktree,
5258 fileindex, tmp_branch, hle, repo);
5260 if (error)
5261 goto done;
5262 continue;
5265 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5266 error = histedit_skip_commit(hle, worktree, repo);
5267 if (error)
5268 goto done;
5269 continue;
5272 error = got_object_open_as_commit(&commit, repo,
5273 hle->commit_id);
5274 if (error)
5275 goto done;
5276 parent_ids = got_object_commit_get_parent_ids(commit);
5277 pid = SIMPLEQ_FIRST(parent_ids);
5279 error = got_worktree_histedit_merge_files(&merged_paths,
5280 worktree, fileindex, pid->id, hle->commit_id, repo,
5281 rebase_progress, &rebase_status, check_cancelled, NULL);
5282 if (error)
5283 goto done;
5284 got_object_commit_close(commit);
5285 commit = NULL;
5287 if (rebase_status == GOT_STATUS_CONFLICT) {
5288 got_worktree_rebase_pathlist_free(&merged_paths);
5289 break;
5292 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5293 char *id_str;
5294 error = got_object_id_str(&id_str, hle->commit_id);
5295 if (error)
5296 goto done;
5297 printf("Stopping histedit for amending commit %s\n",
5298 id_str);
5299 free(id_str);
5300 got_worktree_rebase_pathlist_free(&merged_paths);
5301 error = got_worktree_histedit_postpone(worktree,
5302 fileindex);
5303 goto done;
5306 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5307 error = histedit_skip_commit(hle, worktree, repo);
5308 if (error)
5309 goto done;
5310 continue;
5313 error = histedit_commit(&merged_paths, worktree, fileindex,
5314 tmp_branch, hle, repo);
5315 got_worktree_rebase_pathlist_free(&merged_paths);
5316 if (error)
5317 goto done;
5320 if (rebase_status == GOT_STATUS_CONFLICT) {
5321 error = got_worktree_histedit_postpone(worktree, fileindex);
5322 if (error)
5323 goto done;
5324 error = got_error_msg(GOT_ERR_CONFLICTS,
5325 "conflicts must be resolved before rebasing can continue");
5326 } else
5327 error = histedit_complete(worktree, fileindex, tmp_branch,
5328 branch, repo);
5329 done:
5330 got_object_id_queue_free(&commits);
5331 histedit_free_list(&histedit_cmds);
5332 free(head_commit_id);
5333 free(base_commit_id);
5334 free(resume_commit_id);
5335 if (commit)
5336 got_object_commit_close(commit);
5337 if (branch)
5338 got_ref_close(branch);
5339 if (tmp_branch)
5340 got_ref_close(tmp_branch);
5341 if (worktree)
5342 got_worktree_close(worktree);
5343 if (repo)
5344 got_repo_close(repo);
5345 return error;
5348 __dead static void
5349 usage_stage(void)
5351 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5352 "[file-path ...]\n",
5353 getprogname());
5354 exit(1);
5357 static const struct got_error *
5358 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5359 const char *path, struct got_object_id *blob_id,
5360 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5362 const struct got_error *err = NULL;
5363 char *id_str = NULL;
5365 if (staged_status != GOT_STATUS_ADD &&
5366 staged_status != GOT_STATUS_MODIFY &&
5367 staged_status != GOT_STATUS_DELETE)
5368 return NULL;
5370 if (staged_status == GOT_STATUS_ADD ||
5371 staged_status == GOT_STATUS_MODIFY)
5372 err = got_object_id_str(&id_str, staged_blob_id);
5373 else
5374 err = got_object_id_str(&id_str, blob_id);
5375 if (err)
5376 return err;
5378 printf("%s %c %s\n", id_str, staged_status, path);
5379 free(id_str);
5380 return NULL;
5383 static const struct got_error *
5384 cmd_stage(int argc, char *argv[])
5386 const struct got_error *error = NULL;
5387 struct got_repository *repo = NULL;
5388 struct got_worktree *worktree = NULL;
5389 char *cwd = NULL;
5390 struct got_pathlist_head paths;
5391 struct got_pathlist_entry *pe;
5392 int ch, list_stage = 0, pflag = 0;
5393 FILE *patch_script_file = NULL;
5394 const char *patch_script_path = NULL;
5395 struct choose_patch_arg cpa;
5397 TAILQ_INIT(&paths);
5399 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5400 switch (ch) {
5401 case 'l':
5402 list_stage = 1;
5403 break;
5404 case 'p':
5405 pflag = 1;
5406 break;
5407 case 'F':
5408 patch_script_path = optarg;
5409 break;
5410 default:
5411 usage_stage();
5412 /* NOTREACHED */
5416 argc -= optind;
5417 argv += optind;
5419 #ifndef PROFILE
5420 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5421 "unveil", NULL) == -1)
5422 err(1, "pledge");
5423 #endif
5424 if (list_stage && (pflag || patch_script_path))
5425 errx(1, "-l option cannot be used with other options");
5426 if (patch_script_path && !pflag)
5427 errx(1, "-F option can only be used together with -p option");
5429 cwd = getcwd(NULL, 0);
5430 if (cwd == NULL) {
5431 error = got_error_from_errno("getcwd");
5432 goto done;
5435 error = got_worktree_open(&worktree, cwd);
5436 if (error)
5437 goto done;
5439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5440 if (error != NULL)
5441 goto done;
5443 if (patch_script_path) {
5444 patch_script_file = fopen(patch_script_path, "r");
5445 if (patch_script_file == NULL) {
5446 error = got_error_from_errno2("fopen",
5447 patch_script_path);
5448 goto done;
5451 error = apply_unveil(got_repo_get_path(repo), 1,
5452 got_worktree_get_root_path(worktree));
5453 if (error)
5454 goto done;
5456 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5457 if (error)
5458 goto done;
5460 if (list_stage)
5461 error = got_worktree_status(worktree, &paths, repo,
5462 print_stage, NULL, check_cancelled, NULL);
5463 else {
5464 cpa.patch_script_file = patch_script_file;
5465 cpa.action = "stage";
5466 error = got_worktree_stage(worktree, &paths,
5467 pflag ? NULL : print_status, NULL,
5468 pflag ? choose_patch : NULL, &cpa, repo);
5470 done:
5471 if (patch_script_file && fclose(patch_script_file) == EOF &&
5472 error == NULL)
5473 error = got_error_from_errno2("fclose", patch_script_path);
5474 if (repo)
5475 got_repo_close(repo);
5476 if (worktree)
5477 got_worktree_close(worktree);
5478 TAILQ_FOREACH(pe, &paths, entry)
5479 free((char *)pe->path);
5480 got_pathlist_free(&paths);
5481 free(cwd);
5482 return error;
5485 __dead static void
5486 usage_unstage(void)
5488 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5489 "[file-path ...]\n",
5490 getprogname());
5491 exit(1);
5495 static const struct got_error *
5496 cmd_unstage(int argc, char *argv[])
5498 const struct got_error *error = NULL;
5499 struct got_repository *repo = NULL;
5500 struct got_worktree *worktree = NULL;
5501 char *cwd = NULL;
5502 struct got_pathlist_head paths;
5503 struct got_pathlist_entry *pe;
5504 int ch, did_something = 0, pflag = 0;
5505 FILE *patch_script_file = NULL;
5506 const char *patch_script_path = NULL;
5507 struct choose_patch_arg cpa;
5509 TAILQ_INIT(&paths);
5511 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5512 switch (ch) {
5513 case 'p':
5514 pflag = 1;
5515 break;
5516 case 'F':
5517 patch_script_path = optarg;
5518 break;
5519 default:
5520 usage_unstage();
5521 /* NOTREACHED */
5525 argc -= optind;
5526 argv += optind;
5528 #ifndef PROFILE
5529 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5530 "unveil", NULL) == -1)
5531 err(1, "pledge");
5532 #endif
5533 if (patch_script_path && !pflag)
5534 errx(1, "-F option can only be used together with -p option");
5536 cwd = getcwd(NULL, 0);
5537 if (cwd == NULL) {
5538 error = got_error_from_errno("getcwd");
5539 goto done;
5542 error = got_worktree_open(&worktree, cwd);
5543 if (error)
5544 goto done;
5546 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5547 if (error != NULL)
5548 goto done;
5550 if (patch_script_path) {
5551 patch_script_file = fopen(patch_script_path, "r");
5552 if (patch_script_file == NULL) {
5553 error = got_error_from_errno2("fopen",
5554 patch_script_path);
5555 goto done;
5559 error = apply_unveil(got_repo_get_path(repo), 1,
5560 got_worktree_get_root_path(worktree));
5561 if (error)
5562 goto done;
5564 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5565 if (error)
5566 goto done;
5568 cpa.patch_script_file = patch_script_file;
5569 cpa.action = "unstage";
5570 error = got_worktree_unstage(worktree, &paths, update_progress,
5571 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5572 done:
5573 if (patch_script_file && fclose(patch_script_file) == EOF &&
5574 error == NULL)
5575 error = got_error_from_errno2("fclose", patch_script_path);
5576 if (repo)
5577 got_repo_close(repo);
5578 if (worktree)
5579 got_worktree_close(worktree);
5580 TAILQ_FOREACH(pe, &paths, entry)
5581 free((char *)pe->path);
5582 got_pathlist_free(&paths);
5583 free(cwd);
5584 return error;