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 get_author(const char **author)
488 const char *got_author;
490 *author = NULL;
492 got_author = getenv("GOT_AUTHOR");
493 if (got_author == NULL) {
494 /* TODO: Look up user in password database? */
495 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
498 *author = got_author;
500 /*
501 * Really dumb email address check; we're only doing this to
502 * avoid git's object parser breaking on commits we create.
503 */
504 while (*got_author && *got_author != '<')
505 got_author++;
506 if (*got_author != '<')
507 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
508 while (*got_author && *got_author != '@')
509 got_author++;
510 if (*got_author != '@')
511 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
512 while (*got_author && *got_author != '>')
513 got_author++;
514 if (*got_author != '>')
515 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 return NULL;
520 static const struct got_error *
521 cmd_import(int argc, char *argv[])
523 const struct got_error *error = NULL;
524 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
525 char *editor = NULL;
526 const char *author;
527 const char *branch_name = "master";
528 char *refname = NULL, *id_str = NULL;
529 struct got_repository *repo = NULL;
530 struct got_reference *branch_ref = NULL, *head_ref = NULL;
531 struct got_object_id *new_commit_id = NULL;
532 int ch;
533 struct got_pathlist_head ignores;
534 struct got_pathlist_entry *pe;
536 TAILQ_INIT(&ignores);
538 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
539 switch (ch) {
540 case 'b':
541 branch_name = optarg;
542 break;
543 case 'm':
544 logmsg = strdup(optarg);
545 if (logmsg == NULL) {
546 error = got_error_from_errno("strdup");
547 goto done;
549 break;
550 case 'r':
551 repo_path = realpath(optarg, NULL);
552 if (repo_path == NULL) {
553 error = got_error_from_errno("realpath");
554 goto done;
556 break;
557 case 'I':
558 if (optarg[0] == '\0')
559 break;
560 error = got_pathlist_insert(&pe, &ignores, optarg,
561 NULL);
562 if (error)
563 goto done;
564 break;
565 default:
566 usage_init();
567 /* NOTREACHED */
571 argc -= optind;
572 argv += optind;
574 #ifndef PROFILE
575 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
576 NULL) == -1)
577 err(1, "pledge");
578 #endif
579 if (argc != 1)
580 usage_import();
582 error = get_author(&author);
583 if (error)
584 return error;
586 if (repo_path == NULL) {
587 repo_path = getcwd(NULL, 0);
588 if (repo_path == NULL)
589 return got_error_from_errno("getcwd");
591 got_path_strip_trailing_slashes(repo_path);
592 error = got_repo_open(&repo, repo_path);
593 if (error)
594 goto done;
596 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
597 error = got_error_from_errno("asprintf");
598 goto done;
601 error = got_ref_open(&branch_ref, repo, refname, 0);
602 if (error) {
603 if (error->code != GOT_ERR_NOT_REF)
604 goto done;
605 } else {
606 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
607 "import target branch already exists");
608 goto done;
611 path_dir = realpath(argv[0], NULL);
612 if (path_dir == NULL) {
613 error = got_error_from_errno("realpath");
614 goto done;
616 got_path_strip_trailing_slashes(path_dir);
618 /*
619 * unveil(2) traverses exec(2); if an editor is used we have
620 * to apply unveil after the log message has been written.
621 */
622 if (logmsg == NULL || strlen(logmsg) == 0) {
623 error = get_editor(&editor);
624 if (error)
625 goto done;
626 error = collect_import_msg(&logmsg, editor, path_dir, refname);
627 if (error)
628 goto done;
631 if (unveil(path_dir, "r") != 0)
632 return got_error_from_errno2("unveil", path_dir);
634 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
635 if (error)
636 goto done;
638 error = got_repo_import(&new_commit_id, path_dir, logmsg,
639 author, &ignores, repo, import_progress, NULL);
640 if (error)
641 goto done;
643 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
644 if (error)
645 goto done;
647 error = got_ref_write(branch_ref, repo);
648 if (error)
649 goto done;
651 error = got_object_id_str(&id_str, new_commit_id);
652 if (error)
653 goto done;
655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
656 if (error) {
657 if (error->code != GOT_ERR_NOT_REF)
658 goto done;
660 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
661 branch_ref);
662 if (error)
663 goto done;
665 error = got_ref_write(head_ref, repo);
666 if (error)
667 goto done;
670 printf("Created branch %s with commit %s\n",
671 got_ref_get_name(branch_ref), id_str);
672 done:
673 free(repo_path);
674 free(editor);
675 free(refname);
676 free(new_commit_id);
677 free(id_str);
678 if (branch_ref)
679 got_ref_close(branch_ref);
680 if (head_ref)
681 got_ref_close(head_ref);
682 return error;
685 __dead static void
686 usage_checkout(void)
688 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
689 "[-p prefix] repository-path [worktree-path]\n", getprogname());
690 exit(1);
693 static const struct got_error *
694 checkout_progress(void *arg, unsigned char status, const char *path)
696 char *worktree_path = arg;
698 /* Base commit bump happens silently. */
699 if (status == GOT_STATUS_BUMP_BASE)
700 return NULL;
702 while (path[0] == '/')
703 path++;
705 printf("%c %s/%s\n", status, worktree_path, path);
706 return NULL;
709 static const struct got_error *
710 check_cancelled(void *arg)
712 if (sigint_received || sigpipe_received)
713 return got_error(GOT_ERR_CANCELLED);
714 return NULL;
717 static const struct got_error *
718 check_linear_ancestry(struct got_object_id *commit_id,
719 struct got_object_id *base_commit_id, struct got_repository *repo)
721 const struct got_error *err = NULL;
722 struct got_object_id *yca_id;
724 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
725 commit_id, base_commit_id, repo);
726 if (err)
727 return err;
729 if (yca_id == NULL)
730 return got_error(GOT_ERR_ANCESTRY);
732 /*
733 * Require a straight line of history between the target commit
734 * and the work tree's base commit.
736 * Non-linear situations such as this require a rebase:
738 * (commit) D F (base_commit)
739 * \ /
740 * C E
741 * \ /
742 * B (yca)
743 * |
744 * A
746 * 'got update' only handles linear cases:
747 * Update forwards in time: A (base/yca) - B - C - D (commit)
748 * Update backwards in time: D (base) - C - B - A (commit/yca)
749 */
750 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
751 got_object_id_cmp(base_commit_id, yca_id) != 0)
752 return got_error(GOT_ERR_ANCESTRY);
754 free(yca_id);
755 return NULL;
758 static const struct got_error *
759 check_same_branch(struct got_object_id *commit_id,
760 struct got_reference *head_ref, struct got_object_id *yca_id,
761 struct got_repository *repo)
763 const struct got_error *err = NULL;
764 struct got_commit_graph *graph = NULL;
765 struct got_object_id *head_commit_id = NULL;
766 int is_same_branch = 0;
768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
769 if (err)
770 goto done;
772 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
773 is_same_branch = 1;
774 goto done;
776 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
777 is_same_branch = 1;
778 goto done;
781 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
782 if (err)
783 goto done;
785 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
786 if (err)
787 goto done;
789 for (;;) {
790 struct got_object_id *id;
791 err = got_commit_graph_iter_next(&id, graph);
792 if (err) {
793 if (err->code == GOT_ERR_ITER_COMPLETED) {
794 err = NULL;
795 break;
796 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
797 break;
798 err = got_commit_graph_fetch_commits(graph, 1,
799 repo);
800 if (err)
801 break;
804 if (id) {
805 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
806 break;
807 if (got_object_id_cmp(id, commit_id) == 0) {
808 is_same_branch = 1;
809 break;
813 done:
814 if (graph)
815 got_commit_graph_close(graph);
816 free(head_commit_id);
817 if (!err && !is_same_branch)
818 err = got_error(GOT_ERR_ANCESTRY);
819 return err;
822 static const struct got_error *
823 resolve_commit_arg(struct got_object_id **commit_id,
824 const char *commit_id_arg, struct got_repository *repo)
826 const struct got_error *err;
827 struct got_reference *ref;
828 struct got_tag_object *tag;
830 err = got_repo_object_match_tag(&tag, commit_id_arg,
831 GOT_OBJ_TYPE_COMMIT, repo);
832 if (err == NULL) {
833 *commit_id = got_object_id_dup(
834 got_object_tag_get_object_id(tag));
835 if (*commit_id == NULL)
836 err = got_error_from_errno("got_object_id_dup");
837 got_object_tag_close(tag);
838 return err;
839 } else if (err->code != GOT_ERR_NO_OBJ)
840 return err;
842 err = got_ref_open(&ref, repo, commit_id_arg, 0);
843 if (err == NULL) {
844 err = got_ref_resolve(commit_id, repo, ref);
845 got_ref_close(ref);
846 } else {
847 if (err->code != GOT_ERR_NOT_REF)
848 return err;
849 err = got_repo_match_object_id_prefix(commit_id,
850 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
852 return err;
855 static const struct got_error *
856 cmd_checkout(int argc, char *argv[])
858 const struct got_error *error = NULL;
859 struct got_repository *repo = NULL;
860 struct got_reference *head_ref = NULL;
861 struct got_worktree *worktree = NULL;
862 char *repo_path = NULL;
863 char *worktree_path = NULL;
864 const char *path_prefix = "";
865 const char *branch_name = GOT_REF_HEAD;
866 char *commit_id_str = NULL;
867 int ch, same_path_prefix;
868 struct got_pathlist_head paths;
870 TAILQ_INIT(&paths);
872 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
873 switch (ch) {
874 case 'b':
875 branch_name = optarg;
876 break;
877 case 'c':
878 commit_id_str = strdup(optarg);
879 if (commit_id_str == NULL)
880 return got_error_from_errno("strdup");
881 break;
882 case 'p':
883 path_prefix = optarg;
884 break;
885 default:
886 usage_checkout();
887 /* NOTREACHED */
891 argc -= optind;
892 argv += optind;
894 #ifndef PROFILE
895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
896 "unveil", NULL) == -1)
897 err(1, "pledge");
898 #endif
899 if (argc == 1) {
900 char *cwd, *base, *dotgit;
901 repo_path = realpath(argv[0], NULL);
902 if (repo_path == NULL)
903 return got_error_from_errno2("realpath", argv[0]);
904 cwd = getcwd(NULL, 0);
905 if (cwd == NULL) {
906 error = got_error_from_errno("getcwd");
907 goto done;
909 if (path_prefix[0]) {
910 base = basename(path_prefix);
911 if (base == NULL) {
912 error = got_error_from_errno2("basename",
913 path_prefix);
914 goto done;
916 } else {
917 base = basename(repo_path);
918 if (base == NULL) {
919 error = got_error_from_errno2("basename",
920 repo_path);
921 goto done;
924 dotgit = strstr(base, ".git");
925 if (dotgit)
926 *dotgit = '\0';
927 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
928 error = got_error_from_errno("asprintf");
929 free(cwd);
930 goto done;
932 free(cwd);
933 } else if (argc == 2) {
934 repo_path = realpath(argv[0], NULL);
935 if (repo_path == NULL) {
936 error = got_error_from_errno2("realpath", argv[0]);
937 goto done;
939 worktree_path = realpath(argv[1], NULL);
940 if (worktree_path == NULL) {
941 if (errno != ENOENT) {
942 error = got_error_from_errno2("realpath",
943 argv[1]);
944 goto done;
946 worktree_path = strdup(argv[1]);
947 if (worktree_path == NULL) {
948 error = got_error_from_errno("strdup");
949 goto done;
952 } else
953 usage_checkout();
955 got_path_strip_trailing_slashes(repo_path);
956 got_path_strip_trailing_slashes(worktree_path);
958 error = got_repo_open(&repo, repo_path);
959 if (error != NULL)
960 goto done;
962 /* Pre-create work tree path for unveil(2) */
963 error = got_path_mkdir(worktree_path);
964 if (error) {
965 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
966 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
967 goto done;
968 if (!got_path_dir_is_empty(worktree_path)) {
969 error = got_error_path(worktree_path,
970 GOT_ERR_DIR_NOT_EMPTY);
971 goto done;
975 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
976 if (error)
977 goto done;
979 error = got_ref_open(&head_ref, repo, branch_name, 0);
980 if (error != NULL)
981 goto done;
983 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
984 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
985 goto done;
987 error = got_worktree_open(&worktree, worktree_path);
988 if (error != NULL)
989 goto done;
991 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
992 path_prefix);
993 if (error != NULL)
994 goto done;
995 if (!same_path_prefix) {
996 error = got_error(GOT_ERR_PATH_PREFIX);
997 goto done;
1000 if (commit_id_str) {
1001 struct got_object_id *commit_id;
1002 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1003 if (error)
1004 goto done;
1005 error = check_linear_ancestry(commit_id,
1006 got_worktree_get_base_commit_id(worktree), repo);
1007 if (error != NULL) {
1008 free(commit_id);
1009 goto done;
1011 error = check_same_branch(commit_id, head_ref, NULL, repo);
1012 if (error)
1013 goto done;
1014 error = got_worktree_set_base_commit_id(worktree, repo,
1015 commit_id);
1016 free(commit_id);
1017 if (error)
1018 goto done;
1021 error = got_pathlist_append(&paths, "", NULL);
1022 if (error)
1023 goto done;
1024 error = got_worktree_checkout_files(worktree, &paths, repo,
1025 checkout_progress, worktree_path, check_cancelled, NULL);
1026 if (error != NULL)
1027 goto done;
1029 printf("Now shut up and hack\n");
1031 done:
1032 got_pathlist_free(&paths);
1033 free(commit_id_str);
1034 free(repo_path);
1035 free(worktree_path);
1036 return error;
1039 __dead static void
1040 usage_update(void)
1042 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1043 getprogname());
1044 exit(1);
1047 static const struct got_error *
1048 update_progress(void *arg, unsigned char status, const char *path)
1050 int *did_something = arg;
1052 if (status == GOT_STATUS_EXISTS)
1053 return NULL;
1055 *did_something = 1;
1057 /* Base commit bump happens silently. */
1058 if (status == GOT_STATUS_BUMP_BASE)
1059 return NULL;
1061 while (path[0] == '/')
1062 path++;
1063 printf("%c %s\n", status, path);
1064 return NULL;
1067 static const struct got_error *
1068 switch_head_ref(struct got_reference *head_ref,
1069 struct got_object_id *commit_id, struct got_worktree *worktree,
1070 struct got_repository *repo)
1072 const struct got_error *err = NULL;
1073 char *base_id_str;
1074 int ref_has_moved = 0;
1076 /* Trivial case: switching between two different references. */
1077 if (strcmp(got_ref_get_name(head_ref),
1078 got_worktree_get_head_ref_name(worktree)) != 0) {
1079 printf("Switching work tree from %s to %s\n",
1080 got_worktree_get_head_ref_name(worktree),
1081 got_ref_get_name(head_ref));
1082 return got_worktree_set_head_ref(worktree, head_ref);
1085 err = check_linear_ancestry(commit_id,
1086 got_worktree_get_base_commit_id(worktree), repo);
1087 if (err) {
1088 if (err->code != GOT_ERR_ANCESTRY)
1089 return err;
1090 ref_has_moved = 1;
1092 if (!ref_has_moved)
1093 return NULL;
1095 /* Switching to a rebased branch with the same reference name. */
1096 err = got_object_id_str(&base_id_str,
1097 got_worktree_get_base_commit_id(worktree));
1098 if (err)
1099 return err;
1100 printf("Reference %s now points at a different branch\n",
1101 got_worktree_get_head_ref_name(worktree));
1102 printf("Switching work tree from %s to %s\n", base_id_str,
1103 got_worktree_get_head_ref_name(worktree));
1104 return NULL;
1107 static const struct got_error *
1108 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1110 const struct got_error *err;
1111 int in_progress;
1113 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1114 if (err)
1115 return err;
1116 if (in_progress)
1117 return got_error(GOT_ERR_REBASING);
1119 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1120 if (err)
1121 return err;
1122 if (in_progress)
1123 return got_error(GOT_ERR_HISTEDIT_BUSY);
1125 return NULL;
1128 static const struct got_error *
1129 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1130 char *argv[], struct got_worktree *worktree)
1132 const struct got_error *err = NULL;
1133 char *path;
1134 int i;
1136 if (argc == 0) {
1137 path = strdup("");
1138 if (path == NULL)
1139 return got_error_from_errno("strdup");
1140 return got_pathlist_append(paths, path, NULL);
1143 for (i = 0; i < argc; i++) {
1144 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1145 if (err)
1146 break;
1147 err = got_pathlist_append(paths, path, NULL);
1148 if (err) {
1149 free(path);
1150 break;
1154 return err;
1157 static const struct got_error *
1158 cmd_update(int argc, char *argv[])
1160 const struct got_error *error = NULL;
1161 struct got_repository *repo = NULL;
1162 struct got_worktree *worktree = NULL;
1163 char *worktree_path = NULL;
1164 struct got_object_id *commit_id = NULL;
1165 char *commit_id_str = NULL;
1166 const char *branch_name = NULL;
1167 struct got_reference *head_ref = NULL;
1168 struct got_pathlist_head paths;
1169 struct got_pathlist_entry *pe;
1170 int ch, did_something = 0;
1172 TAILQ_INIT(&paths);
1174 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1175 switch (ch) {
1176 case 'b':
1177 branch_name = optarg;
1178 break;
1179 case 'c':
1180 commit_id_str = strdup(optarg);
1181 if (commit_id_str == NULL)
1182 return got_error_from_errno("strdup");
1183 break;
1184 default:
1185 usage_update();
1186 /* NOTREACHED */
1190 argc -= optind;
1191 argv += optind;
1193 #ifndef PROFILE
1194 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1195 "unveil", NULL) == -1)
1196 err(1, "pledge");
1197 #endif
1198 worktree_path = getcwd(NULL, 0);
1199 if (worktree_path == NULL) {
1200 error = got_error_from_errno("getcwd");
1201 goto done;
1203 error = got_worktree_open(&worktree, worktree_path);
1204 if (error)
1205 goto done;
1207 error = check_rebase_or_histedit_in_progress(worktree);
1208 if (error)
1209 goto done;
1211 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1212 if (error != NULL)
1213 goto done;
1215 error = apply_unveil(got_repo_get_path(repo), 0,
1216 got_worktree_get_root_path(worktree));
1217 if (error)
1218 goto done;
1220 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1221 if (error)
1222 goto done;
1224 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1225 got_worktree_get_head_ref_name(worktree), 0);
1226 if (error != NULL)
1227 goto done;
1228 if (commit_id_str == NULL) {
1229 error = got_ref_resolve(&commit_id, repo, head_ref);
1230 if (error != NULL)
1231 goto done;
1232 error = got_object_id_str(&commit_id_str, commit_id);
1233 if (error != NULL)
1234 goto done;
1235 } else {
1236 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1237 free(commit_id_str);
1238 commit_id_str = NULL;
1239 if (error)
1240 goto done;
1241 error = got_object_id_str(&commit_id_str, commit_id);
1242 if (error)
1243 goto done;
1246 if (branch_name) {
1247 struct got_object_id *head_commit_id;
1248 TAILQ_FOREACH(pe, &paths, entry) {
1249 if (pe->path_len == 0)
1250 continue;
1251 error = got_error_msg(GOT_ERR_BAD_PATH,
1252 "switching between branches requires that "
1253 "the entire work tree gets updated");
1254 goto done;
1256 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1257 if (error)
1258 goto done;
1259 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1260 free(head_commit_id);
1261 if (error != NULL)
1262 goto done;
1263 error = check_same_branch(commit_id, head_ref, NULL, repo);
1264 if (error)
1265 goto done;
1266 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1267 if (error)
1268 goto done;
1269 } else {
1270 error = check_linear_ancestry(commit_id,
1271 got_worktree_get_base_commit_id(worktree), repo);
1272 if (error != NULL) {
1273 if (error->code == GOT_ERR_ANCESTRY)
1274 error = got_error(GOT_ERR_BRANCH_MOVED);
1275 goto done;
1277 error = check_same_branch(commit_id, head_ref, NULL, repo);
1278 if (error)
1279 goto done;
1282 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1283 commit_id) != 0) {
1284 error = got_worktree_set_base_commit_id(worktree, repo,
1285 commit_id);
1286 if (error)
1287 goto done;
1290 error = got_worktree_checkout_files(worktree, &paths, repo,
1291 update_progress, &did_something, check_cancelled, NULL);
1292 if (error != NULL)
1293 goto done;
1295 if (did_something)
1296 printf("Updated to commit %s\n", commit_id_str);
1297 else
1298 printf("Already up-to-date\n");
1299 done:
1300 free(worktree_path);
1301 TAILQ_FOREACH(pe, &paths, entry)
1302 free((char *)pe->path);
1303 got_pathlist_free(&paths);
1304 free(commit_id);
1305 free(commit_id_str);
1306 return error;
1309 static const struct got_error *
1310 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1311 int diff_context, struct got_repository *repo)
1313 const struct got_error *err = NULL;
1314 struct got_tree_object *tree1 = NULL, *tree2;
1315 struct got_object_qid *qid;
1316 char *id_str1 = NULL, *id_str2;
1317 struct got_diff_blob_output_unidiff_arg arg;
1319 err = got_object_open_as_tree(&tree2, repo,
1320 got_object_commit_get_tree_id(commit));
1321 if (err)
1322 return err;
1324 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1325 if (qid != NULL) {
1326 struct got_commit_object *pcommit;
1328 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1329 if (err)
1330 return err;
1332 err = got_object_open_as_tree(&tree1, repo,
1333 got_object_commit_get_tree_id(pcommit));
1334 got_object_commit_close(pcommit);
1335 if (err)
1336 return err;
1338 err = got_object_id_str(&id_str1, qid->id);
1339 if (err)
1340 return err;
1343 err = got_object_id_str(&id_str2, id);
1344 if (err)
1345 goto done;
1347 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1348 arg.diff_context = diff_context;
1349 arg.outfile = stdout;
1350 err = got_diff_tree(tree1, tree2, "", "", repo,
1351 got_diff_blob_output_unidiff, &arg, 1);
1352 done:
1353 if (tree1)
1354 got_object_tree_close(tree1);
1355 got_object_tree_close(tree2);
1356 free(id_str1);
1357 free(id_str2);
1358 return err;
1361 static char *
1362 get_datestr(time_t *time, char *datebuf)
1364 char *p, *s = ctime_r(time, datebuf);
1365 p = strchr(s, '\n');
1366 if (p)
1367 *p = '\0';
1368 return s;
1371 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1373 static const struct got_error *
1374 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1375 struct got_repository *repo, int show_patch, int diff_context,
1376 struct got_reflist_head *refs)
1378 const struct got_error *err = NULL;
1379 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1380 char datebuf[26];
1381 time_t committer_time;
1382 const char *author, *committer;
1383 char *refs_str = NULL;
1384 struct got_reflist_entry *re;
1386 SIMPLEQ_FOREACH(re, refs, entry) {
1387 char *s;
1388 const char *name;
1389 if (got_object_id_cmp(re->id, id) != 0)
1390 continue;
1391 name = got_ref_get_name(re->ref);
1392 if (strcmp(name, GOT_REF_HEAD) == 0)
1393 continue;
1394 if (strncmp(name, "refs/", 5) == 0)
1395 name += 5;
1396 if (strncmp(name, "got/", 4) == 0)
1397 continue;
1398 if (strncmp(name, "heads/", 6) == 0)
1399 name += 6;
1400 if (strncmp(name, "remotes/", 8) == 0)
1401 name += 8;
1402 s = refs_str;
1403 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1404 name) == -1) {
1405 err = got_error_from_errno("asprintf");
1406 free(s);
1407 break;
1409 free(s);
1411 err = got_object_id_str(&id_str, id);
1412 if (err)
1413 return err;
1415 printf(GOT_COMMIT_SEP_STR);
1416 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1417 refs_str ? refs_str : "", refs_str ? ")" : "");
1418 free(id_str);
1419 id_str = NULL;
1420 free(refs_str);
1421 refs_str = NULL;
1422 printf("from: %s\n", got_object_commit_get_author(commit));
1423 committer_time = got_object_commit_get_committer_time(commit);
1424 datestr = get_datestr(&committer_time, datebuf);
1425 printf("date: %s UTC\n", datestr);
1426 author = got_object_commit_get_author(commit);
1427 committer = got_object_commit_get_committer(commit);
1428 if (strcmp(author, committer) != 0)
1429 printf("via: %s\n", committer);
1430 if (got_object_commit_get_nparents(commit) > 1) {
1431 const struct got_object_id_queue *parent_ids;
1432 struct got_object_qid *qid;
1433 int n = 1;
1434 parent_ids = got_object_commit_get_parent_ids(commit);
1435 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1436 err = got_object_id_str(&id_str, qid->id);
1437 if (err)
1438 return err;
1439 printf("parent %d: %s\n", n++, id_str);
1440 free(id_str);
1444 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1445 if (logmsg0 == NULL)
1446 return got_error_from_errno("strdup");
1448 logmsg = logmsg0;
1449 do {
1450 line = strsep(&logmsg, "\n");
1451 if (line)
1452 printf(" %s\n", line);
1453 } while (line);
1454 free(logmsg0);
1456 if (show_patch) {
1457 err = print_patch(commit, id, diff_context, repo);
1458 if (err == 0)
1459 printf("\n");
1462 if (fflush(stdout) != 0 && err == NULL)
1463 err = got_error_from_errno("fflush");
1464 return err;
1467 static const struct got_error *
1468 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1469 char *path, int show_patch, int diff_context, int limit,
1470 int first_parent_traversal, struct got_reflist_head *refs)
1472 const struct got_error *err;
1473 struct got_commit_graph *graph;
1475 err = got_commit_graph_open(&graph, root_id, path,
1476 first_parent_traversal, repo);
1477 if (err)
1478 return err;
1479 err = got_commit_graph_iter_start(graph, root_id, repo);
1480 if (err)
1481 goto done;
1482 for (;;) {
1483 struct got_commit_object *commit;
1484 struct got_object_id *id;
1486 if (sigint_received || sigpipe_received)
1487 break;
1489 err = got_commit_graph_iter_next(&id, graph);
1490 if (err) {
1491 if (err->code == GOT_ERR_ITER_COMPLETED) {
1492 err = NULL;
1493 break;
1495 if (err->code != GOT_ERR_ITER_NEED_MORE)
1496 break;
1497 err = got_commit_graph_fetch_commits(graph, 1, repo);
1498 if (err)
1499 break;
1500 else
1501 continue;
1503 if (id == NULL)
1504 break;
1506 err = got_object_open_as_commit(&commit, repo, id);
1507 if (err)
1508 break;
1509 err = print_commit(commit, id, repo, show_patch, diff_context,
1510 refs);
1511 got_object_commit_close(commit);
1512 if (err || (limit && --limit == 0))
1513 break;
1515 done:
1516 got_commit_graph_close(graph);
1517 return err;
1520 __dead static void
1521 usage_log(void)
1523 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1524 "[-r repository-path] [path]\n", getprogname());
1525 exit(1);
1528 static const struct got_error *
1529 cmd_log(int argc, char *argv[])
1531 const struct got_error *error;
1532 struct got_repository *repo = NULL;
1533 struct got_worktree *worktree = NULL;
1534 struct got_commit_object *commit = NULL;
1535 struct got_object_id *id = NULL;
1536 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1537 char *start_commit = NULL;
1538 int diff_context = 3, ch;
1539 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1540 const char *errstr;
1541 struct got_reflist_head refs;
1543 SIMPLEQ_INIT(&refs);
1545 #ifndef PROFILE
1546 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1547 NULL)
1548 == -1)
1549 err(1, "pledge");
1550 #endif
1552 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1553 switch (ch) {
1554 case 'p':
1555 show_patch = 1;
1556 break;
1557 case 'c':
1558 start_commit = optarg;
1559 break;
1560 case 'C':
1561 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1562 &errstr);
1563 if (errstr != NULL)
1564 err(1, "-C option %s", errstr);
1565 break;
1566 case 'l':
1567 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1568 if (errstr != NULL)
1569 err(1, "-l option %s", errstr);
1570 break;
1571 case 'f':
1572 first_parent_traversal = 1;
1573 break;
1574 case 'r':
1575 repo_path = realpath(optarg, NULL);
1576 if (repo_path == NULL)
1577 err(1, "-r option");
1578 got_path_strip_trailing_slashes(repo_path);
1579 break;
1580 default:
1581 usage_log();
1582 /* NOTREACHED */
1586 argc -= optind;
1587 argv += optind;
1589 cwd = getcwd(NULL, 0);
1590 if (cwd == NULL) {
1591 error = got_error_from_errno("getcwd");
1592 goto done;
1595 error = got_worktree_open(&worktree, cwd);
1596 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1597 goto done;
1598 error = NULL;
1600 if (argc == 0) {
1601 path = strdup("");
1602 if (path == NULL) {
1603 error = got_error_from_errno("strdup");
1604 goto done;
1606 } else if (argc == 1) {
1607 if (worktree) {
1608 error = got_worktree_resolve_path(&path, worktree,
1609 argv[0]);
1610 if (error)
1611 goto done;
1612 } else {
1613 path = strdup(argv[0]);
1614 if (path == NULL) {
1615 error = got_error_from_errno("strdup");
1616 goto done;
1619 } else
1620 usage_log();
1622 if (repo_path == NULL) {
1623 repo_path = worktree ?
1624 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1626 if (repo_path == NULL) {
1627 error = got_error_from_errno("strdup");
1628 goto done;
1631 error = got_repo_open(&repo, repo_path);
1632 if (error != NULL)
1633 goto done;
1635 error = apply_unveil(got_repo_get_path(repo), 1,
1636 worktree ? got_worktree_get_root_path(worktree) : NULL);
1637 if (error)
1638 goto done;
1640 if (start_commit == NULL) {
1641 struct got_reference *head_ref;
1642 error = got_ref_open(&head_ref, repo,
1643 worktree ? got_worktree_get_head_ref_name(worktree)
1644 : GOT_REF_HEAD, 0);
1645 if (error != NULL)
1646 return error;
1647 error = got_ref_resolve(&id, repo, head_ref);
1648 got_ref_close(head_ref);
1649 if (error != NULL)
1650 return error;
1651 error = got_object_open_as_commit(&commit, repo, id);
1652 } else {
1653 struct got_reference *ref;
1654 error = got_ref_open(&ref, repo, start_commit, 0);
1655 if (error == NULL) {
1656 int obj_type;
1657 error = got_ref_resolve(&id, repo, ref);
1658 got_ref_close(ref);
1659 if (error != NULL)
1660 goto done;
1661 error = got_object_get_type(&obj_type, repo, id);
1662 if (error != NULL)
1663 goto done;
1664 if (obj_type == GOT_OBJ_TYPE_TAG) {
1665 struct got_tag_object *tag;
1666 error = got_object_open_as_tag(&tag, repo, id);
1667 if (error != NULL)
1668 goto done;
1669 if (got_object_tag_get_object_type(tag) !=
1670 GOT_OBJ_TYPE_COMMIT) {
1671 got_object_tag_close(tag);
1672 error = got_error(GOT_ERR_OBJ_TYPE);
1673 goto done;
1675 free(id);
1676 id = got_object_id_dup(
1677 got_object_tag_get_object_id(tag));
1678 if (id == NULL)
1679 error = got_error_from_errno(
1680 "got_object_id_dup");
1681 got_object_tag_close(tag);
1682 if (error)
1683 goto done;
1684 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1685 error = got_error(GOT_ERR_OBJ_TYPE);
1686 goto done;
1688 error = got_object_open_as_commit(&commit, repo, id);
1689 if (error != NULL)
1690 goto done;
1692 if (commit == NULL) {
1693 error = got_repo_match_object_id_prefix(&id,
1694 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1695 if (error != NULL)
1696 return error;
1699 if (error != NULL)
1700 goto done;
1702 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1703 if (error != NULL)
1704 goto done;
1705 if (in_repo_path) {
1706 free(path);
1707 path = in_repo_path;
1710 error = got_ref_list(&refs, repo);
1711 if (error)
1712 goto done;
1714 error = print_commits(id, repo, path, show_patch,
1715 diff_context, limit, first_parent_traversal, &refs);
1716 done:
1717 free(path);
1718 free(repo_path);
1719 free(cwd);
1720 free(id);
1721 if (worktree)
1722 got_worktree_close(worktree);
1723 if (repo) {
1724 const struct got_error *repo_error;
1725 repo_error = got_repo_close(repo);
1726 if (error == NULL)
1727 error = repo_error;
1729 got_ref_list_free(&refs);
1730 return error;
1733 __dead static void
1734 usage_diff(void)
1736 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1737 "[object1 object2 | path]\n", getprogname());
1738 exit(1);
1741 struct print_diff_arg {
1742 struct got_repository *repo;
1743 struct got_worktree *worktree;
1744 int diff_context;
1745 const char *id_str;
1746 int header_shown;
1747 int diff_staged;
1750 static const struct got_error *
1751 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1752 const char *path, struct got_object_id *blob_id,
1753 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1755 struct print_diff_arg *a = arg;
1756 const struct got_error *err = NULL;
1757 struct got_blob_object *blob1 = NULL;
1758 FILE *f2 = NULL;
1759 char *abspath = NULL, *label1 = NULL;
1760 struct stat sb;
1762 if (a->diff_staged) {
1763 if (staged_status != GOT_STATUS_MODIFY &&
1764 staged_status != GOT_STATUS_ADD &&
1765 staged_status != GOT_STATUS_DELETE)
1766 return NULL;
1767 } else {
1768 if (staged_status == GOT_STATUS_DELETE)
1769 return NULL;
1770 if (status != GOT_STATUS_MODIFY &&
1771 status != GOT_STATUS_ADD &&
1772 status != GOT_STATUS_DELETE &&
1773 status != GOT_STATUS_CONFLICT)
1774 return NULL;
1777 if (!a->header_shown) {
1778 printf("diff %s %s%s\n", a->id_str,
1779 got_worktree_get_root_path(a->worktree),
1780 a->diff_staged ? " (staged changes)" : "");
1781 a->header_shown = 1;
1784 if (a->diff_staged) {
1785 const char *label1 = NULL, *label2 = NULL;
1786 switch (staged_status) {
1787 case GOT_STATUS_MODIFY:
1788 label1 = path;
1789 label2 = path;
1790 break;
1791 case GOT_STATUS_ADD:
1792 label2 = path;
1793 break;
1794 case GOT_STATUS_DELETE:
1795 label1 = path;
1796 break;
1797 default:
1798 return got_error(GOT_ERR_FILE_STATUS);
1800 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1801 label1, label2, a->diff_context, a->repo, stdout);
1804 if (staged_status == GOT_STATUS_ADD ||
1805 staged_status == GOT_STATUS_MODIFY) {
1806 char *id_str;
1807 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1808 8192);
1809 if (err)
1810 goto done;
1811 err = got_object_id_str(&id_str, staged_blob_id);
1812 if (err)
1813 goto done;
1814 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1816 free(id_str);
1817 goto done;
1819 free(id_str);
1820 } else if (status != GOT_STATUS_ADD) {
1821 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1822 if (err)
1823 goto done;
1826 if (status != GOT_STATUS_DELETE) {
1827 if (asprintf(&abspath, "%s/%s",
1828 got_worktree_get_root_path(a->worktree), path) == -1) {
1829 err = got_error_from_errno("asprintf");
1830 goto done;
1833 f2 = fopen(abspath, "r");
1834 if (f2 == NULL) {
1835 err = got_error_from_errno2("fopen", abspath);
1836 goto done;
1838 if (lstat(abspath, &sb) == -1) {
1839 err = got_error_from_errno2("lstat", abspath);
1840 goto done;
1842 } else
1843 sb.st_size = 0;
1845 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1846 a->diff_context, stdout);
1847 done:
1848 if (blob1)
1849 got_object_blob_close(blob1);
1850 if (f2 && fclose(f2) != 0 && err == NULL)
1851 err = got_error_from_errno("fclose");
1852 free(abspath);
1853 return err;
1856 static const struct got_error *
1857 cmd_diff(int argc, char *argv[])
1859 const struct got_error *error;
1860 struct got_repository *repo = NULL;
1861 struct got_worktree *worktree = NULL;
1862 char *cwd = NULL, *repo_path = NULL;
1863 struct got_object_id *id1 = NULL, *id2 = NULL;
1864 const char *id_str1 = NULL, *id_str2 = NULL;
1865 char *label1 = NULL, *label2 = NULL;
1866 int type1, type2;
1867 int diff_context = 3, diff_staged = 0, ch;
1868 const char *errstr;
1869 char *path = NULL;
1871 #ifndef PROFILE
1872 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1873 NULL) == -1)
1874 err(1, "pledge");
1875 #endif
1877 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1878 switch (ch) {
1879 case 'C':
1880 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1881 if (errstr != NULL)
1882 err(1, "-C option %s", errstr);
1883 break;
1884 case 'r':
1885 repo_path = realpath(optarg, NULL);
1886 if (repo_path == NULL)
1887 err(1, "-r option");
1888 got_path_strip_trailing_slashes(repo_path);
1889 break;
1890 case 's':
1891 diff_staged = 1;
1892 break;
1893 default:
1894 usage_diff();
1895 /* NOTREACHED */
1899 argc -= optind;
1900 argv += optind;
1902 cwd = getcwd(NULL, 0);
1903 if (cwd == NULL) {
1904 error = got_error_from_errno("getcwd");
1905 goto done;
1907 error = got_worktree_open(&worktree, cwd);
1908 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1909 goto done;
1910 if (argc <= 1) {
1911 if (worktree == NULL) {
1912 error = got_error(GOT_ERR_NOT_WORKTREE);
1913 goto done;
1915 if (repo_path)
1916 errx(1,
1917 "-r option can't be used when diffing a work tree");
1918 repo_path = strdup(got_worktree_get_repo_path(worktree));
1919 if (repo_path == NULL) {
1920 error = got_error_from_errno("strdup");
1921 goto done;
1923 if (argc == 1) {
1924 error = got_worktree_resolve_path(&path, worktree,
1925 argv[0]);
1926 if (error)
1927 goto done;
1928 } else {
1929 path = strdup("");
1930 if (path == NULL) {
1931 error = got_error_from_errno("strdup");
1932 goto done;
1935 } else if (argc == 2) {
1936 if (diff_staged)
1937 errx(1, "-s option can't be used when diffing "
1938 "objects in repository");
1939 id_str1 = argv[0];
1940 id_str2 = argv[1];
1941 if (worktree && repo_path == NULL) {
1942 repo_path =
1943 strdup(got_worktree_get_repo_path(worktree));
1944 if (repo_path == NULL) {
1945 error = got_error_from_errno("strdup");
1946 goto done;
1949 } else
1950 usage_diff();
1952 if (repo_path == NULL) {
1953 repo_path = getcwd(NULL, 0);
1954 if (repo_path == NULL)
1955 return got_error_from_errno("getcwd");
1958 error = got_repo_open(&repo, repo_path);
1959 free(repo_path);
1960 if (error != NULL)
1961 goto done;
1963 error = apply_unveil(got_repo_get_path(repo), 1,
1964 worktree ? got_worktree_get_root_path(worktree) : NULL);
1965 if (error)
1966 goto done;
1968 if (argc <= 1) {
1969 struct print_diff_arg arg;
1970 struct got_pathlist_head paths;
1971 char *id_str;
1973 TAILQ_INIT(&paths);
1975 error = got_object_id_str(&id_str,
1976 got_worktree_get_base_commit_id(worktree));
1977 if (error)
1978 goto done;
1979 arg.repo = repo;
1980 arg.worktree = worktree;
1981 arg.diff_context = diff_context;
1982 arg.id_str = id_str;
1983 arg.header_shown = 0;
1984 arg.diff_staged = diff_staged;
1986 error = got_pathlist_append(&paths, path, NULL);
1987 if (error)
1988 goto done;
1990 error = got_worktree_status(worktree, &paths, repo, print_diff,
1991 &arg, check_cancelled, NULL);
1992 free(id_str);
1993 got_pathlist_free(&paths);
1994 goto done;
1997 error = got_repo_match_object_id_prefix(&id1, id_str1,
1998 GOT_OBJ_TYPE_ANY, repo);
1999 if (error) {
2000 struct got_reference *ref;
2001 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2002 goto done;
2003 error = got_ref_open(&ref, repo, id_str1, 0);
2004 if (error != NULL)
2005 goto done;
2006 label1 = strdup(got_ref_get_name(ref));
2007 if (label1 == NULL) {
2008 error = got_error_from_errno("strdup");
2009 goto done;
2011 error = got_ref_resolve(&id1, repo, ref);
2012 got_ref_close(ref);
2013 if (error != NULL)
2014 goto done;
2015 } else {
2016 error = got_object_id_str(&label1, id1);
2017 if (label1 == NULL) {
2018 error = got_error_from_errno("strdup");
2019 goto done;
2023 error = got_repo_match_object_id_prefix(&id2, id_str2,
2024 GOT_OBJ_TYPE_ANY, repo);
2025 if (error) {
2026 struct got_reference *ref;
2027 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2028 goto done;
2029 error = got_ref_open(&ref, repo, id_str2, 0);
2030 if (error != NULL)
2031 goto done;
2032 label2 = strdup(got_ref_get_name(ref));
2033 if (label2 == NULL) {
2034 error = got_error_from_errno("strdup");
2035 goto done;
2037 error = got_ref_resolve(&id2, repo, ref);
2038 got_ref_close(ref);
2039 if (error != NULL)
2040 goto done;
2041 } else {
2042 error = got_object_id_str(&label2, id2);
2043 if (label2 == NULL) {
2044 error = got_error_from_errno("strdup");
2045 goto done;
2049 error = got_object_get_type(&type1, repo, id1);
2050 if (error)
2051 goto done;
2053 error = got_object_get_type(&type2, repo, id2);
2054 if (error)
2055 goto done;
2057 if (type1 != type2) {
2058 error = got_error(GOT_ERR_OBJ_TYPE);
2059 goto done;
2062 switch (type1) {
2063 case GOT_OBJ_TYPE_BLOB:
2064 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2065 diff_context, repo, stdout);
2066 break;
2067 case GOT_OBJ_TYPE_TREE:
2068 error = got_diff_objects_as_trees(id1, id2, "", "",
2069 diff_context, repo, stdout);
2070 break;
2071 case GOT_OBJ_TYPE_COMMIT:
2072 printf("diff %s %s\n", label1, label2);
2073 error = got_diff_objects_as_commits(id1, id2, diff_context,
2074 repo, stdout);
2075 break;
2076 default:
2077 error = got_error(GOT_ERR_OBJ_TYPE);
2080 done:
2081 free(label1);
2082 free(label2);
2083 free(id1);
2084 free(id2);
2085 free(path);
2086 if (worktree)
2087 got_worktree_close(worktree);
2088 if (repo) {
2089 const struct got_error *repo_error;
2090 repo_error = got_repo_close(repo);
2091 if (error == NULL)
2092 error = repo_error;
2094 return error;
2097 __dead static void
2098 usage_blame(void)
2100 fprintf(stderr,
2101 "usage: %s blame [-c commit] [-r repository-path] path\n",
2102 getprogname());
2103 exit(1);
2106 static const struct got_error *
2107 cmd_blame(int argc, char *argv[])
2109 const struct got_error *error;
2110 struct got_repository *repo = NULL;
2111 struct got_worktree *worktree = NULL;
2112 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2113 struct got_object_id *commit_id = NULL;
2114 char *commit_id_str = NULL;
2115 int ch;
2117 #ifndef PROFILE
2118 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2119 NULL) == -1)
2120 err(1, "pledge");
2121 #endif
2123 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2124 switch (ch) {
2125 case 'c':
2126 commit_id_str = optarg;
2127 break;
2128 case 'r':
2129 repo_path = realpath(optarg, NULL);
2130 if (repo_path == NULL)
2131 err(1, "-r option");
2132 got_path_strip_trailing_slashes(repo_path);
2133 break;
2134 default:
2135 usage_blame();
2136 /* NOTREACHED */
2140 argc -= optind;
2141 argv += optind;
2143 if (argc == 1)
2144 path = argv[0];
2145 else
2146 usage_blame();
2148 cwd = getcwd(NULL, 0);
2149 if (cwd == NULL) {
2150 error = got_error_from_errno("getcwd");
2151 goto done;
2153 if (repo_path == NULL) {
2154 error = got_worktree_open(&worktree, cwd);
2155 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2156 goto done;
2157 else
2158 error = NULL;
2159 if (worktree) {
2160 repo_path =
2161 strdup(got_worktree_get_repo_path(worktree));
2162 if (repo_path == NULL)
2163 error = got_error_from_errno("strdup");
2164 if (error)
2165 goto done;
2166 } else {
2167 repo_path = strdup(cwd);
2168 if (repo_path == NULL) {
2169 error = got_error_from_errno("strdup");
2170 goto done;
2175 error = got_repo_open(&repo, repo_path);
2176 if (error != NULL)
2177 goto done;
2179 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2180 if (error)
2181 goto done;
2183 if (worktree) {
2184 const char *prefix = got_worktree_get_path_prefix(worktree);
2185 char *p, *worktree_subdir = cwd +
2186 strlen(got_worktree_get_root_path(worktree));
2187 if (asprintf(&p, "%s%s%s%s%s",
2188 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2189 worktree_subdir, worktree_subdir[0] ? "/" : "",
2190 path) == -1) {
2191 error = got_error_from_errno("asprintf");
2192 goto done;
2194 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2195 free(p);
2196 } else {
2197 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2199 if (error)
2200 goto done;
2202 if (commit_id_str == NULL) {
2203 struct got_reference *head_ref;
2204 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2205 if (error != NULL)
2206 goto done;
2207 error = got_ref_resolve(&commit_id, repo, head_ref);
2208 got_ref_close(head_ref);
2209 if (error != NULL)
2210 goto done;
2211 } else {
2212 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2213 if (error)
2214 goto done;
2217 error = got_blame(in_repo_path, commit_id, repo, stdout);
2218 done:
2219 free(in_repo_path);
2220 free(repo_path);
2221 free(cwd);
2222 free(commit_id);
2223 if (worktree)
2224 got_worktree_close(worktree);
2225 if (repo) {
2226 const struct got_error *repo_error;
2227 repo_error = got_repo_close(repo);
2228 if (error == NULL)
2229 error = repo_error;
2231 return error;
2234 __dead static void
2235 usage_tree(void)
2237 fprintf(stderr,
2238 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2239 getprogname());
2240 exit(1);
2243 static void
2244 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2245 const char *root_path)
2247 int is_root_path = (strcmp(path, root_path) == 0);
2249 path += strlen(root_path);
2250 while (path[0] == '/')
2251 path++;
2253 printf("%s%s%s%s%s\n", id ? id : "", path,
2254 is_root_path ? "" : "/", te->name,
2255 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2258 static const struct got_error *
2259 print_tree(const char *path, struct got_object_id *commit_id,
2260 int show_ids, int recurse, const char *root_path,
2261 struct got_repository *repo)
2263 const struct got_error *err = NULL;
2264 struct got_object_id *tree_id = NULL;
2265 struct got_tree_object *tree = NULL;
2266 const struct got_tree_entries *entries;
2267 struct got_tree_entry *te;
2269 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2270 if (err)
2271 goto done;
2273 err = got_object_open_as_tree(&tree, repo, tree_id);
2274 if (err)
2275 goto done;
2276 entries = got_object_tree_get_entries(tree);
2277 te = SIMPLEQ_FIRST(&entries->head);
2278 while (te) {
2279 char *id = NULL;
2281 if (sigint_received || sigpipe_received)
2282 break;
2284 if (show_ids) {
2285 char *id_str;
2286 err = got_object_id_str(&id_str, te->id);
2287 if (err)
2288 goto done;
2289 if (asprintf(&id, "%s ", id_str) == -1) {
2290 err = got_error_from_errno("asprintf");
2291 free(id_str);
2292 goto done;
2294 free(id_str);
2296 print_entry(te, id, path, root_path);
2297 free(id);
2299 if (recurse && S_ISDIR(te->mode)) {
2300 char *child_path;
2301 if (asprintf(&child_path, "%s%s%s", path,
2302 path[0] == '/' && path[1] == '\0' ? "" : "/",
2303 te->name) == -1) {
2304 err = got_error_from_errno("asprintf");
2305 goto done;
2307 err = print_tree(child_path, commit_id, show_ids, 1,
2308 root_path, repo);
2309 free(child_path);
2310 if (err)
2311 goto done;
2314 te = SIMPLEQ_NEXT(te, entry);
2316 done:
2317 if (tree)
2318 got_object_tree_close(tree);
2319 free(tree_id);
2320 return err;
2323 static const struct got_error *
2324 cmd_tree(int argc, char *argv[])
2326 const struct got_error *error;
2327 struct got_repository *repo = NULL;
2328 struct got_worktree *worktree = NULL;
2329 const char *path;
2330 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2331 struct got_object_id *commit_id = NULL;
2332 char *commit_id_str = NULL;
2333 int show_ids = 0, recurse = 0;
2334 int ch;
2336 #ifndef PROFILE
2337 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2338 NULL) == -1)
2339 err(1, "pledge");
2340 #endif
2342 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2343 switch (ch) {
2344 case 'c':
2345 commit_id_str = optarg;
2346 break;
2347 case 'r':
2348 repo_path = realpath(optarg, NULL);
2349 if (repo_path == NULL)
2350 err(1, "-r option");
2351 got_path_strip_trailing_slashes(repo_path);
2352 break;
2353 case 'i':
2354 show_ids = 1;
2355 break;
2356 case 'R':
2357 recurse = 1;
2358 break;
2359 default:
2360 usage_tree();
2361 /* NOTREACHED */
2365 argc -= optind;
2366 argv += optind;
2368 if (argc == 1)
2369 path = argv[0];
2370 else if (argc > 1)
2371 usage_tree();
2372 else
2373 path = NULL;
2375 cwd = getcwd(NULL, 0);
2376 if (cwd == NULL) {
2377 error = got_error_from_errno("getcwd");
2378 goto done;
2380 if (repo_path == NULL) {
2381 error = got_worktree_open(&worktree, cwd);
2382 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2383 goto done;
2384 else
2385 error = NULL;
2386 if (worktree) {
2387 repo_path =
2388 strdup(got_worktree_get_repo_path(worktree));
2389 if (repo_path == NULL)
2390 error = got_error_from_errno("strdup");
2391 if (error)
2392 goto done;
2393 } else {
2394 repo_path = strdup(cwd);
2395 if (repo_path == NULL) {
2396 error = got_error_from_errno("strdup");
2397 goto done;
2402 error = got_repo_open(&repo, repo_path);
2403 if (error != NULL)
2404 goto done;
2406 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2407 if (error)
2408 goto done;
2410 if (path == NULL) {
2411 if (worktree) {
2412 char *p, *worktree_subdir = cwd +
2413 strlen(got_worktree_get_root_path(worktree));
2414 if (asprintf(&p, "%s/%s",
2415 got_worktree_get_path_prefix(worktree),
2416 worktree_subdir) == -1) {
2417 error = got_error_from_errno("asprintf");
2418 goto done;
2420 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2421 free(p);
2422 if (error)
2423 goto done;
2424 } else
2425 path = "/";
2427 if (in_repo_path == NULL) {
2428 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2429 if (error != NULL)
2430 goto done;
2433 if (commit_id_str == NULL) {
2434 struct got_reference *head_ref;
2435 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2436 if (error != NULL)
2437 goto done;
2438 error = got_ref_resolve(&commit_id, repo, head_ref);
2439 got_ref_close(head_ref);
2440 if (error != NULL)
2441 goto done;
2442 } else {
2443 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2444 if (error)
2445 goto done;
2448 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2449 in_repo_path, repo);
2450 done:
2451 free(in_repo_path);
2452 free(repo_path);
2453 free(cwd);
2454 free(commit_id);
2455 if (worktree)
2456 got_worktree_close(worktree);
2457 if (repo) {
2458 const struct got_error *repo_error;
2459 repo_error = got_repo_close(repo);
2460 if (error == NULL)
2461 error = repo_error;
2463 return error;
2466 __dead static void
2467 usage_status(void)
2469 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2470 exit(1);
2473 static const struct got_error *
2474 print_status(void *arg, unsigned char status, unsigned char staged_status,
2475 const char *path, struct got_object_id *blob_id,
2476 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2478 if (status == staged_status && (status == GOT_STATUS_DELETE))
2479 status = GOT_STATUS_NO_CHANGE;
2480 printf("%c%c %s\n", status, staged_status, path);
2481 return NULL;
2484 static const struct got_error *
2485 cmd_status(int argc, char *argv[])
2487 const struct got_error *error = NULL;
2488 struct got_repository *repo = NULL;
2489 struct got_worktree *worktree = NULL;
2490 char *cwd = NULL;
2491 struct got_pathlist_head paths;
2492 struct got_pathlist_entry *pe;
2493 int ch;
2495 TAILQ_INIT(&paths);
2497 while ((ch = getopt(argc, argv, "")) != -1) {
2498 switch (ch) {
2499 default:
2500 usage_status();
2501 /* NOTREACHED */
2505 argc -= optind;
2506 argv += optind;
2508 #ifndef PROFILE
2509 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2510 NULL) == -1)
2511 err(1, "pledge");
2512 #endif
2513 cwd = getcwd(NULL, 0);
2514 if (cwd == NULL) {
2515 error = got_error_from_errno("getcwd");
2516 goto done;
2519 error = got_worktree_open(&worktree, cwd);
2520 if (error != NULL)
2521 goto done;
2523 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2524 if (error != NULL)
2525 goto done;
2527 error = apply_unveil(got_repo_get_path(repo), 1,
2528 got_worktree_get_root_path(worktree));
2529 if (error)
2530 goto done;
2532 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2533 if (error)
2534 goto done;
2536 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2537 check_cancelled, NULL);
2538 done:
2539 TAILQ_FOREACH(pe, &paths, entry)
2540 free((char *)pe->path);
2541 got_pathlist_free(&paths);
2542 free(cwd);
2543 return error;
2546 __dead static void
2547 usage_ref(void)
2549 fprintf(stderr,
2550 "usage: %s ref [-r repository] -l | -d name | name target\n",
2551 getprogname());
2552 exit(1);
2555 static const struct got_error *
2556 list_refs(struct got_repository *repo)
2558 static const struct got_error *err = NULL;
2559 struct got_reflist_head refs;
2560 struct got_reflist_entry *re;
2562 SIMPLEQ_INIT(&refs);
2563 err = got_ref_list(&refs, repo);
2564 if (err)
2565 return err;
2567 SIMPLEQ_FOREACH(re, &refs, entry) {
2568 char *refstr;
2569 refstr = got_ref_to_str(re->ref);
2570 if (refstr == NULL)
2571 return got_error_from_errno("got_ref_to_str");
2572 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2573 free(refstr);
2576 got_ref_list_free(&refs);
2577 return NULL;
2580 static const struct got_error *
2581 delete_ref(struct got_repository *repo, const char *refname)
2583 const struct got_error *err = NULL;
2584 struct got_reference *ref;
2586 err = got_ref_open(&ref, repo, refname, 0);
2587 if (err)
2588 return err;
2590 err = got_ref_delete(ref, repo);
2591 got_ref_close(ref);
2592 return err;
2595 static const struct got_error *
2596 add_ref(struct got_repository *repo, const char *refname, const char *target)
2598 const struct got_error *err = NULL;
2599 struct got_object_id *id;
2600 struct got_reference *ref = NULL;
2603 * Don't let the user create a reference named '-'.
2604 * While technically a valid reference name, this case is usually
2605 * an unintended typo.
2607 if (refname[0] == '-' && refname[1] == '\0')
2608 return got_error(GOT_ERR_BAD_REF_NAME);
2610 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2611 repo);
2612 if (err) {
2613 struct got_reference *target_ref;
2615 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2616 return err;
2617 err = got_ref_open(&target_ref, repo, target, 0);
2618 if (err)
2619 return err;
2620 err = got_ref_resolve(&id, repo, target_ref);
2621 got_ref_close(target_ref);
2622 if (err)
2623 return err;
2626 err = got_ref_alloc(&ref, refname, id);
2627 if (err)
2628 goto done;
2630 err = got_ref_write(ref, repo);
2631 done:
2632 if (ref)
2633 got_ref_close(ref);
2634 free(id);
2635 return err;
2638 static const struct got_error *
2639 cmd_ref(int argc, char *argv[])
2641 const struct got_error *error = NULL;
2642 struct got_repository *repo = NULL;
2643 struct got_worktree *worktree = NULL;
2644 char *cwd = NULL, *repo_path = NULL;
2645 int ch, do_list = 0;
2646 const char *delref = NULL;
2648 /* TODO: Add -s option for adding symbolic references. */
2649 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2650 switch (ch) {
2651 case 'd':
2652 delref = optarg;
2653 break;
2654 case 'r':
2655 repo_path = realpath(optarg, NULL);
2656 if (repo_path == NULL)
2657 err(1, "-r option");
2658 got_path_strip_trailing_slashes(repo_path);
2659 break;
2660 case 'l':
2661 do_list = 1;
2662 break;
2663 default:
2664 usage_ref();
2665 /* NOTREACHED */
2669 if (do_list && delref)
2670 errx(1, "-l and -d options are mutually exclusive\n");
2672 argc -= optind;
2673 argv += optind;
2675 if (do_list || delref) {
2676 if (argc > 0)
2677 usage_ref();
2678 } else if (argc != 2)
2679 usage_ref();
2681 #ifndef PROFILE
2682 if (do_list) {
2683 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2684 NULL) == -1)
2685 err(1, "pledge");
2686 } else {
2687 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2688 "sendfd unveil", NULL) == -1)
2689 err(1, "pledge");
2691 #endif
2692 cwd = getcwd(NULL, 0);
2693 if (cwd == NULL) {
2694 error = got_error_from_errno("getcwd");
2695 goto done;
2698 if (repo_path == NULL) {
2699 error = got_worktree_open(&worktree, cwd);
2700 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2701 goto done;
2702 else
2703 error = NULL;
2704 if (worktree) {
2705 repo_path =
2706 strdup(got_worktree_get_repo_path(worktree));
2707 if (repo_path == NULL)
2708 error = got_error_from_errno("strdup");
2709 if (error)
2710 goto done;
2711 } else {
2712 repo_path = strdup(cwd);
2713 if (repo_path == NULL) {
2714 error = got_error_from_errno("strdup");
2715 goto done;
2720 error = got_repo_open(&repo, repo_path);
2721 if (error != NULL)
2722 goto done;
2724 error = apply_unveil(got_repo_get_path(repo), do_list,
2725 worktree ? got_worktree_get_root_path(worktree) : NULL);
2726 if (error)
2727 goto done;
2729 if (do_list)
2730 error = list_refs(repo);
2731 else if (delref)
2732 error = delete_ref(repo, delref);
2733 else
2734 error = add_ref(repo, argv[0], argv[1]);
2735 done:
2736 if (repo)
2737 got_repo_close(repo);
2738 if (worktree)
2739 got_worktree_close(worktree);
2740 free(cwd);
2741 free(repo_path);
2742 return error;
2745 __dead static void
2746 usage_branch(void)
2748 fprintf(stderr,
2749 "usage: %s branch [-r repository] -l | -d name | "
2750 "name [base-branch]\n", getprogname());
2751 exit(1);
2754 static const struct got_error *
2755 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2757 static const struct got_error *err = NULL;
2758 struct got_reflist_head refs;
2759 struct got_reflist_entry *re;
2761 SIMPLEQ_INIT(&refs);
2763 err = got_ref_list(&refs, repo);
2764 if (err)
2765 return err;
2767 SIMPLEQ_FOREACH(re, &refs, entry) {
2768 const char *refname, *marker = " ";
2769 char *refstr;
2770 refname = got_ref_get_name(re->ref);
2771 if (strncmp(refname, "refs/heads/", 11) != 0)
2772 continue;
2773 if (worktree && strcmp(refname,
2774 got_worktree_get_head_ref_name(worktree)) == 0) {
2775 struct got_object_id *id = NULL;
2776 err = got_ref_resolve(&id, repo, re->ref);
2777 if (err)
2778 return err;
2779 if (got_object_id_cmp(id,
2780 got_worktree_get_base_commit_id(worktree)) == 0)
2781 marker = "* ";
2782 else
2783 marker = "~ ";
2784 free(id);
2786 refname += 11;
2787 refstr = got_ref_to_str(re->ref);
2788 if (refstr == NULL)
2789 return got_error_from_errno("got_ref_to_str");
2790 printf("%s%s: %s\n", marker, refname, refstr);
2791 free(refstr);
2794 got_ref_list_free(&refs);
2795 return NULL;
2798 static const struct got_error *
2799 delete_branch(struct got_repository *repo, const char *branch_name)
2801 const struct got_error *err = NULL;
2802 struct got_reference *ref;
2803 char *refname;
2805 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2806 return got_error_from_errno("asprintf");
2808 err = got_ref_open(&ref, repo, refname, 0);
2809 if (err)
2810 goto done;
2812 err = got_ref_delete(ref, repo);
2813 got_ref_close(ref);
2814 done:
2815 free(refname);
2816 return err;
2819 static const struct got_error *
2820 add_branch(struct got_repository *repo, const char *branch_name,
2821 const char *base_branch)
2823 const struct got_error *err = NULL;
2824 struct got_object_id *id = NULL;
2825 struct got_reference *ref = NULL;
2826 char *base_refname = NULL, *refname = NULL;
2827 struct got_reference *base_ref;
2830 * Don't let the user create a branch named '-'.
2831 * While technically a valid reference name, this case is usually
2832 * an unintended typo.
2834 if (branch_name[0] == '-' && branch_name[1] == '\0')
2835 return got_error(GOT_ERR_BAD_REF_NAME);
2837 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2838 base_refname = strdup(GOT_REF_HEAD);
2839 if (base_refname == NULL)
2840 return got_error_from_errno("strdup");
2841 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2842 return got_error_from_errno("asprintf");
2844 err = got_ref_open(&base_ref, repo, base_refname, 0);
2845 if (err)
2846 goto done;
2847 err = got_ref_resolve(&id, repo, base_ref);
2848 got_ref_close(base_ref);
2849 if (err)
2850 goto done;
2852 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2853 err = got_error_from_errno("asprintf");
2854 goto done;
2857 err = got_ref_open(&ref, repo, refname, 0);
2858 if (err == NULL) {
2859 err = got_error(GOT_ERR_BRANCH_EXISTS);
2860 goto done;
2861 } else if (err->code != GOT_ERR_NOT_REF)
2862 goto done;
2864 err = got_ref_alloc(&ref, refname, id);
2865 if (err)
2866 goto done;
2868 err = got_ref_write(ref, repo);
2869 done:
2870 if (ref)
2871 got_ref_close(ref);
2872 free(id);
2873 free(base_refname);
2874 free(refname);
2875 return err;
2878 static const struct got_error *
2879 cmd_branch(int argc, char *argv[])
2881 const struct got_error *error = NULL;
2882 struct got_repository *repo = NULL;
2883 struct got_worktree *worktree = NULL;
2884 char *cwd = NULL, *repo_path = NULL;
2885 int ch, do_list = 0;
2886 const char *delref = NULL;
2888 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2889 switch (ch) {
2890 case 'd':
2891 delref = optarg;
2892 break;
2893 case 'r':
2894 repo_path = realpath(optarg, NULL);
2895 if (repo_path == NULL)
2896 err(1, "-r option");
2897 got_path_strip_trailing_slashes(repo_path);
2898 break;
2899 case 'l':
2900 do_list = 1;
2901 break;
2902 default:
2903 usage_branch();
2904 /* NOTREACHED */
2908 if (do_list && delref)
2909 errx(1, "-l and -d options are mutually exclusive\n");
2911 argc -= optind;
2912 argv += optind;
2914 if (do_list || delref) {
2915 if (argc > 0)
2916 usage_branch();
2917 } else if (argc < 1 || argc > 2)
2918 usage_branch();
2920 #ifndef PROFILE
2921 if (do_list) {
2922 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2923 NULL) == -1)
2924 err(1, "pledge");
2925 } else {
2926 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2927 "sendfd unveil", NULL) == -1)
2928 err(1, "pledge");
2930 #endif
2931 cwd = getcwd(NULL, 0);
2932 if (cwd == NULL) {
2933 error = got_error_from_errno("getcwd");
2934 goto done;
2937 if (repo_path == NULL) {
2938 error = got_worktree_open(&worktree, cwd);
2939 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2940 goto done;
2941 else
2942 error = NULL;
2943 if (worktree) {
2944 repo_path =
2945 strdup(got_worktree_get_repo_path(worktree));
2946 if (repo_path == NULL)
2947 error = got_error_from_errno("strdup");
2948 if (error)
2949 goto done;
2950 } else {
2951 repo_path = strdup(cwd);
2952 if (repo_path == NULL) {
2953 error = got_error_from_errno("strdup");
2954 goto done;
2959 error = got_repo_open(&repo, repo_path);
2960 if (error != NULL)
2961 goto done;
2963 error = apply_unveil(got_repo_get_path(repo), do_list,
2964 worktree ? got_worktree_get_root_path(worktree) : NULL);
2965 if (error)
2966 goto done;
2968 if (do_list)
2969 error = list_branches(repo, worktree);
2970 else if (delref)
2971 error = delete_branch(repo, delref);
2972 else {
2973 const char *base_branch;
2974 if (argc == 1) {
2975 base_branch = worktree ?
2976 got_worktree_get_head_ref_name(worktree) :
2977 GOT_REF_HEAD;
2978 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2979 base_branch += 11;
2980 } else
2981 base_branch = argv[1];
2982 error = add_branch(repo, argv[0], base_branch);
2984 done:
2985 if (repo)
2986 got_repo_close(repo);
2987 if (worktree)
2988 got_worktree_close(worktree);
2989 free(cwd);
2990 free(repo_path);
2991 return error;
2994 __dead static void
2995 usage_add(void)
2997 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2998 exit(1);
3001 static const struct got_error *
3002 cmd_add(int argc, char *argv[])
3004 const struct got_error *error = NULL;
3005 struct got_repository *repo = NULL;
3006 struct got_worktree *worktree = NULL;
3007 char *cwd = NULL;
3008 struct got_pathlist_head paths;
3009 struct got_pathlist_entry *pe;
3010 int ch;
3012 TAILQ_INIT(&paths);
3014 while ((ch = getopt(argc, argv, "")) != -1) {
3015 switch (ch) {
3016 default:
3017 usage_add();
3018 /* NOTREACHED */
3022 argc -= optind;
3023 argv += optind;
3025 #ifndef PROFILE
3026 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3027 NULL) == -1)
3028 err(1, "pledge");
3029 #endif
3030 if (argc < 1)
3031 usage_add();
3033 cwd = getcwd(NULL, 0);
3034 if (cwd == NULL) {
3035 error = got_error_from_errno("getcwd");
3036 goto done;
3039 error = got_worktree_open(&worktree, cwd);
3040 if (error)
3041 goto done;
3043 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3044 if (error != NULL)
3045 goto done;
3047 error = apply_unveil(got_repo_get_path(repo), 1,
3048 got_worktree_get_root_path(worktree));
3049 if (error)
3050 goto done;
3052 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3053 if (error)
3054 goto done;
3056 error = got_worktree_schedule_add(worktree, &paths, print_status,
3057 NULL, repo);
3058 done:
3059 if (repo)
3060 got_repo_close(repo);
3061 if (worktree)
3062 got_worktree_close(worktree);
3063 TAILQ_FOREACH(pe, &paths, entry)
3064 free((char *)pe->path);
3065 got_pathlist_free(&paths);
3066 free(cwd);
3067 return error;
3070 __dead static void
3071 usage_remove(void)
3073 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3074 exit(1);
3077 static const struct got_error *
3078 cmd_remove(int argc, char *argv[])
3080 const struct got_error *error = NULL;
3081 struct got_worktree *worktree = NULL;
3082 struct got_repository *repo = NULL;
3083 char *cwd = NULL;
3084 struct got_pathlist_head paths;
3085 struct got_pathlist_entry *pe;
3086 int ch, delete_local_mods = 0;
3088 TAILQ_INIT(&paths);
3090 while ((ch = getopt(argc, argv, "f")) != -1) {
3091 switch (ch) {
3092 case 'f':
3093 delete_local_mods = 1;
3094 break;
3095 default:
3096 usage_add();
3097 /* NOTREACHED */
3101 argc -= optind;
3102 argv += optind;
3104 #ifndef PROFILE
3105 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3106 NULL) == -1)
3107 err(1, "pledge");
3108 #endif
3109 if (argc < 1)
3110 usage_remove();
3112 cwd = getcwd(NULL, 0);
3113 if (cwd == NULL) {
3114 error = got_error_from_errno("getcwd");
3115 goto done;
3117 error = got_worktree_open(&worktree, cwd);
3118 if (error)
3119 goto done;
3121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3122 if (error)
3123 goto done;
3125 error = apply_unveil(got_repo_get_path(repo), 1,
3126 got_worktree_get_root_path(worktree));
3127 if (error)
3128 goto done;
3130 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3131 if (error)
3132 goto done;
3134 error = got_worktree_schedule_delete(worktree, &paths,
3135 delete_local_mods, print_status, NULL, repo);
3136 if (error)
3137 goto done;
3138 done:
3139 if (repo)
3140 got_repo_close(repo);
3141 if (worktree)
3142 got_worktree_close(worktree);
3143 TAILQ_FOREACH(pe, &paths, entry)
3144 free((char *)pe->path);
3145 got_pathlist_free(&paths);
3146 free(cwd);
3147 return error;
3150 __dead static void
3151 usage_revert(void)
3153 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3154 "path ...\n", getprogname());
3155 exit(1);
3158 static const struct got_error *
3159 revert_progress(void *arg, unsigned char status, const char *path)
3161 while (path[0] == '/')
3162 path++;
3163 printf("%c %s\n", status, path);
3164 return NULL;
3167 struct choose_patch_arg {
3168 FILE *patch_script_file;
3169 const char *action;
3172 static const struct got_error *
3173 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3174 int nchanges, const char *action)
3176 char *line = NULL;
3177 size_t linesize = 0;
3178 ssize_t linelen;
3180 switch (status) {
3181 case GOT_STATUS_ADD:
3182 printf("A %s\n%s this addition? [y/n] ", path, action);
3183 break;
3184 case GOT_STATUS_DELETE:
3185 printf("D %s\n%s this deletion? [y/n] ", path, action);
3186 break;
3187 case GOT_STATUS_MODIFY:
3188 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3189 return got_error_from_errno("fseek");
3190 printf(GOT_COMMIT_SEP_STR);
3191 while ((linelen = getline(&line, &linesize, patch_file) != -1))
3192 printf("%s", line);
3193 if (ferror(patch_file))
3194 return got_error_from_errno("getline");
3195 printf(GOT_COMMIT_SEP_STR);
3196 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3197 path, n, nchanges, action);
3198 break;
3199 default:
3200 return got_error_path(path, GOT_ERR_FILE_STATUS);
3203 return NULL;
3206 static const struct got_error *
3207 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3208 FILE *patch_file, int n, int nchanges)
3210 const struct got_error *err = NULL;
3211 char *line = NULL;
3212 size_t linesize = 0;
3213 ssize_t linelen;
3214 int resp = ' ';
3215 struct choose_patch_arg *a = arg;
3217 *choice = GOT_PATCH_CHOICE_NONE;
3219 if (a->patch_script_file) {
3220 char *nl;
3221 err = show_change(status, path, patch_file, n, nchanges,
3222 a->action);
3223 if (err)
3224 return err;
3225 linelen = getline(&line, &linesize, a->patch_script_file);
3226 if (linelen == -1) {
3227 if (ferror(a->patch_script_file))
3228 return got_error_from_errno("getline");
3229 return NULL;
3231 nl = strchr(line, '\n');
3232 if (nl)
3233 *nl = '\0';
3234 if (strcmp(line, "y") == 0) {
3235 *choice = GOT_PATCH_CHOICE_YES;
3236 printf("y\n");
3237 } else if (strcmp(line, "n") == 0) {
3238 *choice = GOT_PATCH_CHOICE_NO;
3239 printf("n\n");
3240 } else if (strcmp(line, "q") == 0 &&
3241 status == GOT_STATUS_MODIFY) {
3242 *choice = GOT_PATCH_CHOICE_QUIT;
3243 printf("q\n");
3244 } else
3245 printf("invalid response '%s'\n", line);
3246 free(line);
3247 return NULL;
3250 while (resp != 'y' && resp != 'n' && resp != 'q') {
3251 err = show_change(status, path, patch_file, n, nchanges,
3252 a->action);
3253 if (err)
3254 return err;
3255 resp = getchar();
3256 if (resp == '\n')
3257 resp = getchar();
3258 if (status == GOT_STATUS_MODIFY) {
3259 if (resp != 'y' && resp != 'n' && resp != 'q') {
3260 printf("invalid response '%c'\n", resp);
3261 resp = ' ';
3263 } else if (resp != 'y' && resp != 'n') {
3264 printf("invalid response '%c'\n", resp);
3265 resp = ' ';
3269 if (resp == 'y')
3270 *choice = GOT_PATCH_CHOICE_YES;
3271 else if (resp == 'n')
3272 *choice = GOT_PATCH_CHOICE_NO;
3273 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3274 *choice = GOT_PATCH_CHOICE_QUIT;
3276 return NULL;
3280 static const struct got_error *
3281 cmd_revert(int argc, char *argv[])
3283 const struct got_error *error = NULL;
3284 struct got_worktree *worktree = NULL;
3285 struct got_repository *repo = NULL;
3286 char *cwd = NULL, *path = NULL;
3287 struct got_pathlist_head paths;
3288 struct got_pathlist_entry *pe;
3289 int ch, can_recurse = 0, pflag = 0;
3290 FILE *patch_script_file = NULL;
3291 const char *patch_script_path = NULL;
3292 struct choose_patch_arg cpa;
3294 TAILQ_INIT(&paths);
3296 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3297 switch (ch) {
3298 case 'p':
3299 pflag = 1;
3300 break;
3301 case 'F':
3302 patch_script_path = optarg;
3303 break;
3304 case 'R':
3305 can_recurse = 1;
3306 break;
3307 default:
3308 usage_revert();
3309 /* NOTREACHED */
3313 argc -= optind;
3314 argv += optind;
3316 #ifndef PROFILE
3317 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3318 "unveil", NULL) == -1)
3319 err(1, "pledge");
3320 #endif
3321 if (argc < 1)
3322 usage_revert();
3323 if (patch_script_path && !pflag)
3324 errx(1, "-F option can only be used together with -p option");
3326 cwd = getcwd(NULL, 0);
3327 if (cwd == NULL) {
3328 error = got_error_from_errno("getcwd");
3329 goto done;
3331 error = got_worktree_open(&worktree, cwd);
3332 if (error)
3333 goto done;
3335 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3336 if (error != NULL)
3337 goto done;
3339 if (patch_script_path) {
3340 patch_script_file = fopen(patch_script_path, "r");
3341 if (patch_script_file == NULL) {
3342 error = got_error_from_errno2("fopen",
3343 patch_script_path);
3344 goto done;
3347 error = apply_unveil(got_repo_get_path(repo), 1,
3348 got_worktree_get_root_path(worktree));
3349 if (error)
3350 goto done;
3352 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3353 if (error)
3354 goto done;
3356 if (!can_recurse) {
3357 char *ondisk_path;
3358 struct stat sb;
3359 TAILQ_FOREACH(pe, &paths, entry) {
3360 if (asprintf(&ondisk_path, "%s/%s",
3361 got_worktree_get_root_path(worktree),
3362 pe->path) == -1) {
3363 error = got_error_from_errno("asprintf");
3364 goto done;
3366 if (lstat(ondisk_path, &sb) == -1) {
3367 if (errno == ENOENT) {
3368 free(ondisk_path);
3369 continue;
3371 error = got_error_from_errno2("lstat",
3372 ondisk_path);
3373 free(ondisk_path);
3374 goto done;
3376 free(ondisk_path);
3377 if (S_ISDIR(sb.st_mode)) {
3378 error = got_error_msg(GOT_ERR_BAD_PATH,
3379 "reverting directories requires -R option");
3380 goto done;
3385 cpa.patch_script_file = patch_script_file;
3386 cpa.action = "revert";
3387 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3388 pflag ? choose_patch : NULL, &cpa, repo);
3389 if (error)
3390 goto done;
3391 done:
3392 if (patch_script_file && fclose(patch_script_file) == EOF &&
3393 error == NULL)
3394 error = got_error_from_errno2("fclose", patch_script_path);
3395 if (repo)
3396 got_repo_close(repo);
3397 if (worktree)
3398 got_worktree_close(worktree);
3399 free(path);
3400 free(cwd);
3401 return error;
3404 __dead static void
3405 usage_commit(void)
3407 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3408 getprogname());
3409 exit(1);
3412 struct collect_commit_logmsg_arg {
3413 const char *cmdline_log;
3414 const char *editor;
3415 const char *worktree_path;
3416 const char *branch_name;
3417 const char *repo_path;
3418 char *logmsg_path;
3422 static const struct got_error *
3423 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3424 void *arg)
3426 char *initial_content = NULL;
3427 struct got_pathlist_entry *pe;
3428 const struct got_error *err = NULL;
3429 char *template = NULL;
3430 struct collect_commit_logmsg_arg *a = arg;
3431 int fd;
3432 size_t len;
3434 /* if a message was specified on the command line, just use it */
3435 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3436 len = strlen(a->cmdline_log) + 1;
3437 *logmsg = malloc(len + 1);
3438 if (*logmsg == NULL)
3439 return got_error_from_errno("malloc");
3440 strlcpy(*logmsg, a->cmdline_log, len);
3441 return NULL;
3444 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3445 return got_error_from_errno("asprintf");
3447 if (asprintf(&initial_content,
3448 "\n# changes to be committed on branch %s:\n",
3449 a->branch_name) == -1)
3450 return got_error_from_errno("asprintf");
3452 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3453 if (err)
3454 goto done;
3456 dprintf(fd, initial_content);
3458 TAILQ_FOREACH(pe, commitable_paths, entry) {
3459 struct got_commitable *ct = pe->data;
3460 dprintf(fd, "# %c %s\n",
3461 got_commitable_get_status(ct),
3462 got_commitable_get_path(ct));
3464 close(fd);
3466 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3467 done:
3468 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3469 unlink(a->logmsg_path);
3470 free(a->logmsg_path);
3471 a->logmsg_path = NULL;
3473 free(initial_content);
3474 free(template);
3476 /* Editor is done; we can now apply unveil(2) */
3477 if (err == NULL) {
3478 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3479 if (err) {
3480 free(*logmsg);
3481 *logmsg = NULL;
3484 return err;
3487 static const struct got_error *
3488 cmd_commit(int argc, char *argv[])
3490 const struct got_error *error = NULL;
3491 struct got_worktree *worktree = NULL;
3492 struct got_repository *repo = NULL;
3493 char *cwd = NULL, *id_str = NULL;
3494 struct got_object_id *id = NULL;
3495 const char *logmsg = NULL;
3496 const char *author;
3497 struct collect_commit_logmsg_arg cl_arg;
3498 char *editor = NULL;
3499 int ch, rebase_in_progress, histedit_in_progress;
3500 struct got_pathlist_head paths;
3502 TAILQ_INIT(&paths);
3503 cl_arg.logmsg_path = NULL;
3505 while ((ch = getopt(argc, argv, "m:")) != -1) {
3506 switch (ch) {
3507 case 'm':
3508 logmsg = optarg;
3509 break;
3510 default:
3511 usage_commit();
3512 /* NOTREACHED */
3516 argc -= optind;
3517 argv += optind;
3519 #ifndef PROFILE
3520 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3521 "unveil", NULL) == -1)
3522 err(1, "pledge");
3523 #endif
3524 error = get_author(&author);
3525 if (error)
3526 return error;
3528 cwd = getcwd(NULL, 0);
3529 if (cwd == NULL) {
3530 error = got_error_from_errno("getcwd");
3531 goto done;
3533 error = got_worktree_open(&worktree, cwd);
3534 if (error)
3535 goto done;
3537 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3538 if (error)
3539 goto done;
3540 if (rebase_in_progress) {
3541 error = got_error(GOT_ERR_REBASING);
3542 goto done;
3545 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3546 worktree);
3547 if (error)
3548 goto done;
3550 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3551 if (error != NULL)
3552 goto done;
3555 * unveil(2) traverses exec(2); if an editor is used we have
3556 * to apply unveil after the log message has been written.
3558 if (logmsg == NULL || strlen(logmsg) == 0)
3559 error = get_editor(&editor);
3560 else
3561 error = apply_unveil(got_repo_get_path(repo), 0,
3562 got_worktree_get_root_path(worktree));
3563 if (error)
3564 goto done;
3566 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3567 if (error)
3568 goto done;
3570 cl_arg.editor = editor;
3571 cl_arg.cmdline_log = logmsg;
3572 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3573 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3574 if (!histedit_in_progress) {
3575 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3576 error = got_error(GOT_ERR_COMMIT_BRANCH);
3577 goto done;
3579 cl_arg.branch_name += 11;
3581 cl_arg.repo_path = got_repo_get_path(repo);
3582 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3583 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3584 if (error) {
3585 if (cl_arg.logmsg_path)
3586 fprintf(stderr, "%s: log message preserved in %s\n",
3587 getprogname(), cl_arg.logmsg_path);
3588 goto done;
3591 if (cl_arg.logmsg_path)
3592 unlink(cl_arg.logmsg_path);
3594 error = got_object_id_str(&id_str, id);
3595 if (error)
3596 goto done;
3597 printf("Created commit %s\n", id_str);
3598 done:
3599 free(cl_arg.logmsg_path);
3600 if (repo)
3601 got_repo_close(repo);
3602 if (worktree)
3603 got_worktree_close(worktree);
3604 free(cwd);
3605 free(id_str);
3606 free(editor);
3607 return error;
3610 __dead static void
3611 usage_cherrypick(void)
3613 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3614 exit(1);
3617 static const struct got_error *
3618 cmd_cherrypick(int argc, char *argv[])
3620 const struct got_error *error = NULL;
3621 struct got_worktree *worktree = NULL;
3622 struct got_repository *repo = NULL;
3623 char *cwd = NULL, *commit_id_str = NULL;
3624 struct got_object_id *commit_id = NULL;
3625 struct got_commit_object *commit = NULL;
3626 struct got_object_qid *pid;
3627 struct got_reference *head_ref = NULL;
3628 int ch, did_something = 0;
3630 while ((ch = getopt(argc, argv, "")) != -1) {
3631 switch (ch) {
3632 default:
3633 usage_cherrypick();
3634 /* NOTREACHED */
3638 argc -= optind;
3639 argv += optind;
3641 #ifndef PROFILE
3642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3643 "unveil", NULL) == -1)
3644 err(1, "pledge");
3645 #endif
3646 if (argc != 1)
3647 usage_cherrypick();
3649 cwd = getcwd(NULL, 0);
3650 if (cwd == NULL) {
3651 error = got_error_from_errno("getcwd");
3652 goto done;
3654 error = got_worktree_open(&worktree, cwd);
3655 if (error)
3656 goto done;
3658 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3659 if (error != NULL)
3660 goto done;
3662 error = apply_unveil(got_repo_get_path(repo), 0,
3663 got_worktree_get_root_path(worktree));
3664 if (error)
3665 goto done;
3667 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3668 GOT_OBJ_TYPE_COMMIT, repo);
3669 if (error != NULL) {
3670 struct got_reference *ref;
3671 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3672 goto done;
3673 error = got_ref_open(&ref, repo, argv[0], 0);
3674 if (error != NULL)
3675 goto done;
3676 error = got_ref_resolve(&commit_id, repo, ref);
3677 got_ref_close(ref);
3678 if (error != NULL)
3679 goto done;
3681 error = got_object_id_str(&commit_id_str, commit_id);
3682 if (error)
3683 goto done;
3685 error = got_ref_open(&head_ref, repo,
3686 got_worktree_get_head_ref_name(worktree), 0);
3687 if (error != NULL)
3688 goto done;
3690 error = check_same_branch(commit_id, head_ref, NULL, repo);
3691 if (error) {
3692 if (error->code != GOT_ERR_ANCESTRY)
3693 goto done;
3694 error = NULL;
3695 } else {
3696 error = got_error(GOT_ERR_SAME_BRANCH);
3697 goto done;
3700 error = got_object_open_as_commit(&commit, repo, commit_id);
3701 if (error)
3702 goto done;
3703 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3704 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3705 commit_id, repo, update_progress, &did_something, check_cancelled,
3706 NULL);
3707 if (error != NULL)
3708 goto done;
3710 if (did_something)
3711 printf("Merged commit %s\n", commit_id_str);
3712 done:
3713 if (commit)
3714 got_object_commit_close(commit);
3715 free(commit_id_str);
3716 if (head_ref)
3717 got_ref_close(head_ref);
3718 if (worktree)
3719 got_worktree_close(worktree);
3720 if (repo)
3721 got_repo_close(repo);
3722 return error;
3725 __dead static void
3726 usage_backout(void)
3728 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3729 exit(1);
3732 static const struct got_error *
3733 cmd_backout(int argc, char *argv[])
3735 const struct got_error *error = NULL;
3736 struct got_worktree *worktree = NULL;
3737 struct got_repository *repo = NULL;
3738 char *cwd = NULL, *commit_id_str = NULL;
3739 struct got_object_id *commit_id = NULL;
3740 struct got_commit_object *commit = NULL;
3741 struct got_object_qid *pid;
3742 struct got_reference *head_ref = NULL;
3743 int ch, did_something = 0;
3745 while ((ch = getopt(argc, argv, "")) != -1) {
3746 switch (ch) {
3747 default:
3748 usage_backout();
3749 /* NOTREACHED */
3753 argc -= optind;
3754 argv += optind;
3756 #ifndef PROFILE
3757 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3758 "unveil", NULL) == -1)
3759 err(1, "pledge");
3760 #endif
3761 if (argc != 1)
3762 usage_backout();
3764 cwd = getcwd(NULL, 0);
3765 if (cwd == NULL) {
3766 error = got_error_from_errno("getcwd");
3767 goto done;
3769 error = got_worktree_open(&worktree, cwd);
3770 if (error)
3771 goto done;
3773 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3774 if (error != NULL)
3775 goto done;
3777 error = apply_unveil(got_repo_get_path(repo), 0,
3778 got_worktree_get_root_path(worktree));
3779 if (error)
3780 goto done;
3782 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3783 GOT_OBJ_TYPE_COMMIT, repo);
3784 if (error != NULL) {
3785 struct got_reference *ref;
3786 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3787 goto done;
3788 error = got_ref_open(&ref, repo, argv[0], 0);
3789 if (error != NULL)
3790 goto done;
3791 error = got_ref_resolve(&commit_id, repo, ref);
3792 got_ref_close(ref);
3793 if (error != NULL)
3794 goto done;
3796 error = got_object_id_str(&commit_id_str, commit_id);
3797 if (error)
3798 goto done;
3800 error = got_ref_open(&head_ref, repo,
3801 got_worktree_get_head_ref_name(worktree), 0);
3802 if (error != NULL)
3803 goto done;
3805 error = check_same_branch(commit_id, head_ref, NULL, repo);
3806 if (error)
3807 goto done;
3809 error = got_object_open_as_commit(&commit, repo, commit_id);
3810 if (error)
3811 goto done;
3812 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3813 if (pid == NULL) {
3814 error = got_error(GOT_ERR_ROOT_COMMIT);
3815 goto done;
3818 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3819 update_progress, &did_something, check_cancelled, NULL);
3820 if (error != NULL)
3821 goto done;
3823 if (did_something)
3824 printf("Backed out commit %s\n", commit_id_str);
3825 done:
3826 if (commit)
3827 got_object_commit_close(commit);
3828 free(commit_id_str);
3829 if (head_ref)
3830 got_ref_close(head_ref);
3831 if (worktree)
3832 got_worktree_close(worktree);
3833 if (repo)
3834 got_repo_close(repo);
3835 return error;
3838 __dead static void
3839 usage_rebase(void)
3841 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3842 getprogname());
3843 exit(1);
3846 void
3847 trim_logmsg(char *logmsg, int limit)
3849 char *nl;
3850 size_t len;
3852 len = strlen(logmsg);
3853 if (len > limit)
3854 len = limit;
3855 logmsg[len] = '\0';
3856 nl = strchr(logmsg, '\n');
3857 if (nl)
3858 *nl = '\0';
3861 static const struct got_error *
3862 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3864 const char *logmsg0 = NULL;
3866 logmsg0 = got_object_commit_get_logmsg(commit);
3868 while (isspace((unsigned char)logmsg0[0]))
3869 logmsg0++;
3871 *logmsg = strdup(logmsg0);
3872 if (*logmsg == NULL)
3873 return got_error_from_errno("strdup");
3875 trim_logmsg(*logmsg, limit);
3876 return NULL;
3879 static const struct got_error *
3880 show_rebase_progress(struct got_commit_object *commit,
3881 struct got_object_id *old_id, struct got_object_id *new_id)
3883 const struct got_error *err;
3884 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3886 err = got_object_id_str(&old_id_str, old_id);
3887 if (err)
3888 goto done;
3890 if (new_id) {
3891 err = got_object_id_str(&new_id_str, new_id);
3892 if (err)
3893 goto done;
3896 old_id_str[12] = '\0';
3897 if (new_id_str)
3898 new_id_str[12] = '\0';
3900 err = get_short_logmsg(&logmsg, 42, commit);
3901 if (err)
3902 goto done;
3904 printf("%s -> %s: %s\n", old_id_str,
3905 new_id_str ? new_id_str : "no-op change", logmsg);
3906 done:
3907 free(old_id_str);
3908 free(new_id_str);
3909 return err;
3912 static const struct got_error *
3913 rebase_progress(void *arg, unsigned char status, const char *path)
3915 unsigned char *rebase_status = arg;
3917 while (path[0] == '/')
3918 path++;
3919 printf("%c %s\n", status, path);
3921 if (*rebase_status == GOT_STATUS_CONFLICT)
3922 return NULL;
3923 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3924 *rebase_status = status;
3925 return NULL;
3928 static const struct got_error *
3929 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3930 struct got_reference *branch, struct got_reference *new_base_branch,
3931 struct got_reference *tmp_branch, struct got_repository *repo)
3933 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3934 return got_worktree_rebase_complete(worktree, fileindex,
3935 new_base_branch, tmp_branch, branch, repo);
3938 static const struct got_error *
3939 rebase_commit(struct got_pathlist_head *merged_paths,
3940 struct got_worktree *worktree, struct got_fileindex *fileindex,
3941 struct got_reference *tmp_branch,
3942 struct got_object_id *commit_id, struct got_repository *repo)
3944 const struct got_error *error;
3945 struct got_commit_object *commit;
3946 struct got_object_id *new_commit_id;
3948 error = got_object_open_as_commit(&commit, repo, commit_id);
3949 if (error)
3950 return error;
3952 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3953 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3954 if (error) {
3955 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3956 goto done;
3957 error = show_rebase_progress(commit, commit_id, NULL);
3958 } else {
3959 error = show_rebase_progress(commit, commit_id, new_commit_id);
3960 free(new_commit_id);
3962 done:
3963 got_object_commit_close(commit);
3964 return error;
3967 struct check_path_prefix_arg {
3968 const char *path_prefix;
3969 size_t len;
3970 int errcode;
3973 static const struct got_error *
3974 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3975 struct got_blob_object *blob2, struct got_object_id *id1,
3976 struct got_object_id *id2, const char *path1, const char *path2,
3977 struct got_repository *repo)
3979 struct check_path_prefix_arg *a = arg;
3981 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3982 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3983 return got_error(a->errcode);
3985 return NULL;
3988 static const struct got_error *
3989 check_path_prefix(struct got_object_id *parent_id,
3990 struct got_object_id *commit_id, const char *path_prefix,
3991 int errcode, struct got_repository *repo)
3993 const struct got_error *err;
3994 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3995 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3996 struct check_path_prefix_arg cpp_arg;
3998 if (got_path_is_root_dir(path_prefix))
3999 return NULL;
4001 err = got_object_open_as_commit(&commit, repo, commit_id);
4002 if (err)
4003 goto done;
4005 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4006 if (err)
4007 goto done;
4009 err = got_object_open_as_tree(&tree1, repo,
4010 got_object_commit_get_tree_id(parent_commit));
4011 if (err)
4012 goto done;
4014 err = got_object_open_as_tree(&tree2, repo,
4015 got_object_commit_get_tree_id(commit));
4016 if (err)
4017 goto done;
4019 cpp_arg.path_prefix = path_prefix;
4020 while (cpp_arg.path_prefix[0] == '/')
4021 cpp_arg.path_prefix++;
4022 cpp_arg.len = strlen(cpp_arg.path_prefix);
4023 cpp_arg.errcode = errcode;
4024 err = got_diff_tree(tree1, tree2, "", "", repo,
4025 check_path_prefix_in_diff, &cpp_arg, 0);
4026 done:
4027 if (tree1)
4028 got_object_tree_close(tree1);
4029 if (tree2)
4030 got_object_tree_close(tree2);
4031 if (commit)
4032 got_object_commit_close(commit);
4033 if (parent_commit)
4034 got_object_commit_close(parent_commit);
4035 return err;
4038 static const struct got_error *
4039 collect_commits(struct got_object_id_queue *commits,
4040 struct got_object_id *initial_commit_id,
4041 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4042 const char *path_prefix, int path_prefix_errcode,
4043 struct got_repository *repo)
4045 const struct got_error *err = NULL;
4046 struct got_commit_graph *graph = NULL;
4047 struct got_object_id *parent_id = NULL;
4048 struct got_object_qid *qid;
4049 struct got_object_id *commit_id = initial_commit_id;
4051 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4052 if (err)
4053 return err;
4055 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4056 if (err)
4057 goto done;
4058 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4059 err = got_commit_graph_iter_next(&parent_id, graph);
4060 if (err) {
4061 if (err->code == GOT_ERR_ITER_COMPLETED) {
4062 err = got_error_msg(GOT_ERR_ANCESTRY,
4063 "ran out of commits to rebase before "
4064 "youngest common ancestor commit has "
4065 "been reached?!?");
4066 goto done;
4067 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4068 goto done;
4069 err = got_commit_graph_fetch_commits(graph, 1, repo);
4070 if (err)
4071 goto done;
4072 } else {
4073 err = check_path_prefix(parent_id, commit_id,
4074 path_prefix, path_prefix_errcode, repo);
4075 if (err)
4076 goto done;
4078 err = got_object_qid_alloc(&qid, commit_id);
4079 if (err)
4080 goto done;
4081 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4082 commit_id = parent_id;
4085 done:
4086 got_commit_graph_close(graph);
4087 return err;
4090 static const struct got_error *
4091 cmd_rebase(int argc, char *argv[])
4093 const struct got_error *error = NULL;
4094 struct got_worktree *worktree = NULL;
4095 struct got_repository *repo = NULL;
4096 struct got_fileindex *fileindex = NULL;
4097 char *cwd = NULL;
4098 struct got_reference *branch = NULL;
4099 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4100 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4101 struct got_object_id *resume_commit_id = NULL;
4102 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4103 struct got_commit_object *commit = NULL;
4104 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4105 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4106 struct got_object_id_queue commits;
4107 struct got_pathlist_head merged_paths;
4108 const struct got_object_id_queue *parent_ids;
4109 struct got_object_qid *qid, *pid;
4111 SIMPLEQ_INIT(&commits);
4112 TAILQ_INIT(&merged_paths);
4114 while ((ch = getopt(argc, argv, "ac")) != -1) {
4115 switch (ch) {
4116 case 'a':
4117 abort_rebase = 1;
4118 break;
4119 case 'c':
4120 continue_rebase = 1;
4121 break;
4122 default:
4123 usage_rebase();
4124 /* NOTREACHED */
4128 argc -= optind;
4129 argv += optind;
4131 #ifndef PROFILE
4132 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4133 "unveil", NULL) == -1)
4134 err(1, "pledge");
4135 #endif
4136 if (abort_rebase && continue_rebase)
4137 usage_rebase();
4138 else if (abort_rebase || continue_rebase) {
4139 if (argc != 0)
4140 usage_rebase();
4141 } else if (argc != 1)
4142 usage_rebase();
4144 cwd = getcwd(NULL, 0);
4145 if (cwd == NULL) {
4146 error = got_error_from_errno("getcwd");
4147 goto done;
4149 error = got_worktree_open(&worktree, cwd);
4150 if (error)
4151 goto done;
4153 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4154 if (error != NULL)
4155 goto done;
4157 error = apply_unveil(got_repo_get_path(repo), 0,
4158 got_worktree_get_root_path(worktree));
4159 if (error)
4160 goto done;
4162 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4163 if (error)
4164 goto done;
4166 if (abort_rebase) {
4167 int did_something;
4168 if (!rebase_in_progress) {
4169 error = got_error(GOT_ERR_NOT_REBASING);
4170 goto done;
4172 error = got_worktree_rebase_continue(&resume_commit_id,
4173 &new_base_branch, &tmp_branch, &branch, &fileindex,
4174 worktree, repo);
4175 if (error)
4176 goto done;
4177 printf("Switching work tree to %s\n",
4178 got_ref_get_symref_target(new_base_branch));
4179 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4180 new_base_branch, update_progress, &did_something);
4181 if (error)
4182 goto done;
4183 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4184 goto done; /* nothing else to do */
4187 if (continue_rebase) {
4188 if (!rebase_in_progress) {
4189 error = got_error(GOT_ERR_NOT_REBASING);
4190 goto done;
4192 error = got_worktree_rebase_continue(&resume_commit_id,
4193 &new_base_branch, &tmp_branch, &branch, &fileindex,
4194 worktree, repo);
4195 if (error)
4196 goto done;
4198 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4199 resume_commit_id, repo);
4200 if (error)
4201 goto done;
4203 yca_id = got_object_id_dup(resume_commit_id);
4204 if (yca_id == NULL) {
4205 error = got_error_from_errno("got_object_id_dup");
4206 goto done;
4208 } else {
4209 error = got_ref_open(&branch, repo, argv[0], 0);
4210 if (error != NULL)
4211 goto done;
4214 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4215 if (error)
4216 goto done;
4218 if (!continue_rebase) {
4219 struct got_object_id *base_commit_id;
4221 base_commit_id = got_worktree_get_base_commit_id(worktree);
4222 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4223 base_commit_id, branch_head_commit_id, repo);
4224 if (error)
4225 goto done;
4226 if (yca_id == NULL) {
4227 error = got_error_msg(GOT_ERR_ANCESTRY,
4228 "specified branch shares no common ancestry "
4229 "with work tree's branch");
4230 goto done;
4233 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4234 if (error) {
4235 if (error->code != GOT_ERR_ANCESTRY)
4236 goto done;
4237 error = NULL;
4238 } else {
4239 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4240 "specified branch resolves to a commit which "
4241 "is already contained in work tree's branch");
4242 goto done;
4244 error = got_worktree_rebase_prepare(&new_base_branch,
4245 &tmp_branch, &fileindex, worktree, branch, repo);
4246 if (error)
4247 goto done;
4250 commit_id = branch_head_commit_id;
4251 error = got_object_open_as_commit(&commit, repo, commit_id);
4252 if (error)
4253 goto done;
4255 parent_ids = got_object_commit_get_parent_ids(commit);
4256 pid = SIMPLEQ_FIRST(parent_ids);
4257 error = collect_commits(&commits, commit_id, pid->id,
4258 yca_id, got_worktree_get_path_prefix(worktree),
4259 GOT_ERR_REBASE_PATH, repo);
4260 got_object_commit_close(commit);
4261 commit = NULL;
4262 if (error)
4263 goto done;
4265 if (SIMPLEQ_EMPTY(&commits)) {
4266 if (continue_rebase)
4267 error = rebase_complete(worktree, fileindex,
4268 branch, new_base_branch, tmp_branch, repo);
4269 else
4270 error = got_error(GOT_ERR_EMPTY_REBASE);
4271 goto done;
4274 pid = NULL;
4275 SIMPLEQ_FOREACH(qid, &commits, entry) {
4276 commit_id = qid->id;
4277 parent_id = pid ? pid->id : yca_id;
4278 pid = qid;
4280 error = got_worktree_rebase_merge_files(&merged_paths,
4281 worktree, fileindex, parent_id, commit_id, repo,
4282 rebase_progress, &rebase_status, check_cancelled, NULL);
4283 if (error)
4284 goto done;
4286 if (rebase_status == GOT_STATUS_CONFLICT) {
4287 got_worktree_rebase_pathlist_free(&merged_paths);
4288 break;
4291 error = rebase_commit(&merged_paths, worktree, fileindex,
4292 tmp_branch, commit_id, repo);
4293 got_worktree_rebase_pathlist_free(&merged_paths);
4294 if (error)
4295 goto done;
4298 if (rebase_status == GOT_STATUS_CONFLICT) {
4299 error = got_worktree_rebase_postpone(worktree, fileindex);
4300 if (error)
4301 goto done;
4302 error = got_error_msg(GOT_ERR_CONFLICTS,
4303 "conflicts must be resolved before rebasing can continue");
4304 } else
4305 error = rebase_complete(worktree, fileindex, branch,
4306 new_base_branch, tmp_branch, repo);
4307 done:
4308 got_object_id_queue_free(&commits);
4309 free(branch_head_commit_id);
4310 free(resume_commit_id);
4311 free(yca_id);
4312 if (commit)
4313 got_object_commit_close(commit);
4314 if (branch)
4315 got_ref_close(branch);
4316 if (new_base_branch)
4317 got_ref_close(new_base_branch);
4318 if (tmp_branch)
4319 got_ref_close(tmp_branch);
4320 if (worktree)
4321 got_worktree_close(worktree);
4322 if (repo)
4323 got_repo_close(repo);
4324 return error;
4327 __dead static void
4328 usage_histedit(void)
4330 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4331 getprogname());
4332 exit(1);
4335 #define GOT_HISTEDIT_PICK 'p'
4336 #define GOT_HISTEDIT_EDIT 'e'
4337 #define GOT_HISTEDIT_FOLD 'f'
4338 #define GOT_HISTEDIT_DROP 'd'
4339 #define GOT_HISTEDIT_MESG 'm'
4341 static struct got_histedit_cmd {
4342 unsigned char code;
4343 const char *name;
4344 const char *desc;
4345 } got_histedit_cmds[] = {
4346 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4347 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4348 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4349 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4350 { GOT_HISTEDIT_MESG, "mesg",
4351 "single-line log message for commit above (open editor if empty)" },
4354 struct got_histedit_list_entry {
4355 TAILQ_ENTRY(got_histedit_list_entry) entry;
4356 struct got_object_id *commit_id;
4357 const struct got_histedit_cmd *cmd;
4358 char *logmsg;
4360 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4362 static const struct got_error *
4363 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4364 FILE *f, struct got_repository *repo)
4366 const struct got_error *err = NULL;
4367 char *logmsg = NULL, *id_str = NULL;
4368 struct got_commit_object *commit = NULL;
4369 size_t n;
4371 err = got_object_open_as_commit(&commit, repo, commit_id);
4372 if (err)
4373 goto done;
4375 err = get_short_logmsg(&logmsg, 34, commit);
4376 if (err)
4377 goto done;
4379 err = got_object_id_str(&id_str, commit_id);
4380 if (err)
4381 goto done;
4383 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4384 if (n < 0)
4385 err = got_ferror(f, GOT_ERR_IO);
4386 done:
4387 if (commit)
4388 got_object_commit_close(commit);
4389 free(id_str);
4390 free(logmsg);
4391 return err;
4394 static const struct got_error *
4395 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4396 struct got_repository *repo)
4398 const struct got_error *err = NULL;
4399 struct got_object_qid *qid;
4401 if (SIMPLEQ_EMPTY(commits))
4402 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4404 SIMPLEQ_FOREACH(qid, commits, entry) {
4405 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4406 f, repo);
4407 if (err)
4408 break;
4411 return err;
4414 static const struct got_error *
4415 write_cmd_list(FILE *f)
4417 const struct got_error *err = NULL;
4418 int n, i;
4420 n = fprintf(f, "# Available histedit commands:\n");
4421 if (n < 0)
4422 return got_ferror(f, GOT_ERR_IO);
4424 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4425 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4426 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4427 cmd->desc);
4428 if (n < 0) {
4429 err = got_ferror(f, GOT_ERR_IO);
4430 break;
4433 n = fprintf(f, "# Commits will be processed in order from top to "
4434 "bottom of this file.\n");
4435 if (n < 0)
4436 return got_ferror(f, GOT_ERR_IO);
4437 return err;
4440 static const struct got_error *
4441 histedit_syntax_error(int lineno)
4443 static char msg[42];
4444 int ret;
4446 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4447 lineno);
4448 if (ret == -1 || ret >= sizeof(msg))
4449 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4451 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4454 static const struct got_error *
4455 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4456 char *logmsg, struct got_repository *repo)
4458 const struct got_error *err;
4459 struct got_commit_object *folded_commit = NULL;
4460 char *id_str;
4462 err = got_object_id_str(&id_str, hle->commit_id);
4463 if (err)
4464 return err;
4466 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4467 if (err)
4468 goto done;
4470 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4471 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4472 got_object_commit_get_logmsg(folded_commit)) == -1) {
4473 err = got_error_from_errno("asprintf");
4474 goto done;
4476 done:
4477 if (folded_commit)
4478 got_object_commit_close(folded_commit);
4479 free(id_str);
4480 return err;
4483 static struct got_histedit_list_entry *
4484 get_folded_commits(struct got_histedit_list_entry *hle)
4486 struct got_histedit_list_entry *prev, *folded = NULL;
4488 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4489 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4490 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4491 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4492 folded = prev;
4493 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4496 return folded;
4499 static const struct got_error *
4500 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4501 struct got_repository *repo)
4503 char *logmsg_path = NULL, *id_str = NULL;
4504 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4505 const struct got_error *err = NULL;
4506 struct got_commit_object *commit = NULL;
4507 int fd;
4508 struct got_histedit_list_entry *folded = NULL;
4510 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4511 if (err)
4512 return err;
4514 folded = get_folded_commits(hle);
4515 if (folded) {
4516 while (folded != hle) {
4517 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4518 folded = TAILQ_NEXT(folded, entry);
4519 continue;
4521 err = append_folded_commit_msg(&new_msg, folded,
4522 logmsg, repo);
4523 if (err)
4524 goto done;
4525 free(logmsg);
4526 logmsg = new_msg;
4527 folded = TAILQ_NEXT(folded, entry);
4531 err = got_object_id_str(&id_str, hle->commit_id);
4532 if (err)
4533 goto done;
4534 if (asprintf(&new_msg,
4535 "%s\n# original log message of commit %s: %s",
4536 logmsg ? logmsg : "", id_str,
4537 got_object_commit_get_logmsg(commit)) == -1) {
4538 err = got_error_from_errno("asprintf");
4539 goto done;
4541 free(logmsg);
4542 logmsg = new_msg;
4544 err = got_object_id_str(&id_str, hle->commit_id);
4545 if (err)
4546 goto done;
4548 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4549 if (err)
4550 goto done;
4552 dprintf(fd, logmsg);
4553 close(fd);
4555 err = get_editor(&editor);
4556 if (err)
4557 goto done;
4559 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4560 if (err) {
4561 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4562 goto done;
4563 err = NULL;
4564 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4565 if (hle->logmsg == NULL)
4566 err = got_error_from_errno("strdup");
4568 done:
4569 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4570 err = got_error_from_errno2("unlink", logmsg_path);
4571 free(logmsg_path);
4572 free(logmsg);
4573 free(editor);
4574 if (commit)
4575 got_object_commit_close(commit);
4576 return err;
4579 static const struct got_error *
4580 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4581 FILE *f, struct got_repository *repo)
4583 const struct got_error *err = NULL;
4584 char *line = NULL, *p, *end;
4585 size_t size;
4586 ssize_t len;
4587 int lineno = 0, i;
4588 const struct got_histedit_cmd *cmd;
4589 struct got_object_id *commit_id = NULL;
4590 struct got_histedit_list_entry *hle = NULL;
4592 for (;;) {
4593 len = getline(&line, &size, f);
4594 if (len == -1) {
4595 const struct got_error *getline_err;
4596 if (feof(f))
4597 break;
4598 getline_err = got_error_from_errno("getline");
4599 err = got_ferror(f, getline_err->code);
4600 break;
4602 lineno++;
4603 p = line;
4604 while (isspace((unsigned char)p[0]))
4605 p++;
4606 if (p[0] == '#' || p[0] == '\0') {
4607 free(line);
4608 line = NULL;
4609 continue;
4611 cmd = NULL;
4612 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4613 cmd = &got_histedit_cmds[i];
4614 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4615 isspace((unsigned char)p[strlen(cmd->name)])) {
4616 p += strlen(cmd->name);
4617 break;
4619 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4620 p++;
4621 break;
4624 if (i == nitems(got_histedit_cmds)) {
4625 err = histedit_syntax_error(lineno);
4626 break;
4628 while (isspace((unsigned char)p[0]))
4629 p++;
4630 if (cmd->code == GOT_HISTEDIT_MESG) {
4631 if (hle == NULL || hle->logmsg != NULL) {
4632 err = got_error(GOT_ERR_HISTEDIT_CMD);
4633 break;
4635 if (p[0] == '\0') {
4636 err = histedit_edit_logmsg(hle, repo);
4637 if (err)
4638 break;
4639 } else {
4640 hle->logmsg = strdup(p);
4641 if (hle->logmsg == NULL) {
4642 err = got_error_from_errno("strdup");
4643 break;
4646 free(line);
4647 line = NULL;
4648 continue;
4649 } else {
4650 end = p;
4651 while (end[0] && !isspace((unsigned char)end[0]))
4652 end++;
4653 *end = '\0';
4655 err = got_object_resolve_id_str(&commit_id, repo, p);
4656 if (err) {
4657 /* override error code */
4658 err = histedit_syntax_error(lineno);
4659 break;
4662 hle = malloc(sizeof(*hle));
4663 if (hle == NULL) {
4664 err = got_error_from_errno("malloc");
4665 break;
4667 hle->cmd = cmd;
4668 hle->commit_id = commit_id;
4669 hle->logmsg = NULL;
4670 commit_id = NULL;
4671 free(line);
4672 line = NULL;
4673 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4676 free(line);
4677 free(commit_id);
4678 return err;
4681 static const struct got_error *
4682 histedit_check_script(struct got_histedit_list *histedit_cmds,
4683 struct got_object_id_queue *commits, struct got_repository *repo)
4685 const struct got_error *err = NULL;
4686 struct got_object_qid *qid;
4687 struct got_histedit_list_entry *hle;
4688 static char msg[80];
4689 char *id_str;
4691 if (TAILQ_EMPTY(histedit_cmds))
4692 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4693 "histedit script contains no commands");
4694 if (SIMPLEQ_EMPTY(commits))
4695 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4697 SIMPLEQ_FOREACH(qid, commits, entry) {
4698 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4699 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4700 break;
4702 if (hle == NULL) {
4703 err = got_object_id_str(&id_str, qid->id);
4704 if (err)
4705 return err;
4706 snprintf(msg, sizeof(msg),
4707 "commit %s missing from histedit script", id_str);
4708 free(id_str);
4709 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4713 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4714 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4715 "last commit in histedit script cannot be folded");
4717 return NULL;
4720 static const struct got_error *
4721 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4722 const char *path, struct got_object_id_queue *commits,
4723 struct got_repository *repo)
4725 const struct got_error *err = NULL;
4726 char *editor;
4727 FILE *f = NULL;
4729 err = get_editor(&editor);
4730 if (err)
4731 return err;
4733 if (spawn_editor(editor, path) == -1) {
4734 err = got_error_from_errno("failed spawning editor");
4735 goto done;
4738 f = fopen(path, "r");
4739 if (f == NULL) {
4740 err = got_error_from_errno("fopen");
4741 goto done;
4743 err = histedit_parse_list(histedit_cmds, f, repo);
4744 if (err)
4745 goto done;
4747 err = histedit_check_script(histedit_cmds, commits, repo);
4748 done:
4749 if (f && fclose(f) != 0 && err == NULL)
4750 err = got_error_from_errno("fclose");
4751 free(editor);
4752 return err;
4755 static const struct got_error *
4756 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4757 struct got_object_id_queue *, const char *, struct got_repository *);
4759 static const struct got_error *
4760 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4761 struct got_object_id_queue *commits, struct got_repository *repo)
4763 const struct got_error *err;
4764 FILE *f = NULL;
4765 char *path = NULL;
4767 err = got_opentemp_named(&path, &f, "got-histedit");
4768 if (err)
4769 return err;
4771 err = write_cmd_list(f);
4772 if (err)
4773 goto done;
4775 err = histedit_write_commit_list(commits, f, repo);
4776 if (err)
4777 goto done;
4779 if (fclose(f) != 0) {
4780 err = got_error_from_errno("fclose");
4781 goto done;
4783 f = NULL;
4785 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4786 if (err) {
4787 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4788 err->code != GOT_ERR_HISTEDIT_CMD)
4789 goto done;
4790 err = histedit_edit_list_retry(histedit_cmds, err,
4791 commits, path, repo);
4793 done:
4794 if (f && fclose(f) != 0 && err == NULL)
4795 err = got_error_from_errno("fclose");
4796 if (path && unlink(path) != 0 && err == NULL)
4797 err = got_error_from_errno2("unlink", path);
4798 free(path);
4799 return err;
4802 static const struct got_error *
4803 histedit_save_list(struct got_histedit_list *histedit_cmds,
4804 struct got_worktree *worktree, struct got_repository *repo)
4806 const struct got_error *err = NULL;
4807 char *path = NULL;
4808 FILE *f = NULL;
4809 struct got_histedit_list_entry *hle;
4810 struct got_commit_object *commit = NULL;
4812 err = got_worktree_get_histedit_script_path(&path, worktree);
4813 if (err)
4814 return err;
4816 f = fopen(path, "w");
4817 if (f == NULL) {
4818 err = got_error_from_errno2("fopen", path);
4819 goto done;
4821 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4822 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4823 repo);
4824 if (err)
4825 break;
4827 if (hle->logmsg) {
4828 int n = fprintf(f, "%c %s\n",
4829 GOT_HISTEDIT_MESG, hle->logmsg);
4830 if (n < 0) {
4831 err = got_ferror(f, GOT_ERR_IO);
4832 break;
4836 done:
4837 if (f && fclose(f) != 0 && err == NULL)
4838 err = got_error_from_errno("fclose");
4839 free(path);
4840 if (commit)
4841 got_object_commit_close(commit);
4842 return err;
4845 void
4846 histedit_free_list(struct got_histedit_list *histedit_cmds)
4848 struct got_histedit_list_entry *hle;
4850 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4851 TAILQ_REMOVE(histedit_cmds, hle, entry);
4852 free(hle);
4856 static const struct got_error *
4857 histedit_load_list(struct got_histedit_list *histedit_cmds,
4858 const char *path, struct got_repository *repo)
4860 const struct got_error *err = NULL;
4861 FILE *f = NULL;
4863 f = fopen(path, "r");
4864 if (f == NULL) {
4865 err = got_error_from_errno2("fopen", path);
4866 goto done;
4869 err = histedit_parse_list(histedit_cmds, f, repo);
4870 done:
4871 if (f && fclose(f) != 0 && err == NULL)
4872 err = got_error_from_errno("fclose");
4873 return err;
4876 static const struct got_error *
4877 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4878 const struct got_error *edit_err, struct got_object_id_queue *commits,
4879 const char *path, struct got_repository *repo)
4881 const struct got_error *err = NULL, *prev_err = edit_err;
4882 int resp = ' ';
4884 while (resp != 'c' && resp != 'r' && resp != 'a') {
4885 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4886 "or (a)bort: ", getprogname(), prev_err->msg);
4887 resp = getchar();
4888 if (resp == '\n')
4889 resp = getchar();
4890 if (resp == 'c') {
4891 histedit_free_list(histedit_cmds);
4892 err = histedit_run_editor(histedit_cmds, path, commits,
4893 repo);
4894 if (err) {
4895 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4896 err->code != GOT_ERR_HISTEDIT_CMD)
4897 break;
4898 prev_err = err;
4899 resp = ' ';
4900 continue;
4902 break;
4903 } else if (resp == 'r') {
4904 histedit_free_list(histedit_cmds);
4905 err = histedit_edit_script(histedit_cmds,
4906 commits, repo);
4907 if (err) {
4908 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4909 err->code != GOT_ERR_HISTEDIT_CMD)
4910 break;
4911 prev_err = err;
4912 resp = ' ';
4913 continue;
4915 break;
4916 } else if (resp == 'a') {
4917 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4918 break;
4919 } else
4920 printf("invalid response '%c'\n", resp);
4923 return err;
4926 static const struct got_error *
4927 histedit_complete(struct got_worktree *worktree,
4928 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4929 struct got_reference *branch, struct got_repository *repo)
4931 printf("Switching work tree to %s\n",
4932 got_ref_get_symref_target(branch));
4933 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4934 branch, repo);
4937 static const struct got_error *
4938 show_histedit_progress(struct got_commit_object *commit,
4939 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4941 const struct got_error *err;
4942 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4944 err = got_object_id_str(&old_id_str, hle->commit_id);
4945 if (err)
4946 goto done;
4948 if (new_id) {
4949 err = got_object_id_str(&new_id_str, new_id);
4950 if (err)
4951 goto done;
4954 old_id_str[12] = '\0';
4955 if (new_id_str)
4956 new_id_str[12] = '\0';
4958 if (hle->logmsg) {
4959 logmsg = strdup(hle->logmsg);
4960 if (logmsg == NULL) {
4961 err = got_error_from_errno("strdup");
4962 goto done;
4964 trim_logmsg(logmsg, 42);
4965 } else {
4966 err = get_short_logmsg(&logmsg, 42, commit);
4967 if (err)
4968 goto done;
4971 switch (hle->cmd->code) {
4972 case GOT_HISTEDIT_PICK:
4973 case GOT_HISTEDIT_EDIT:
4974 printf("%s -> %s: %s\n", old_id_str,
4975 new_id_str ? new_id_str : "no-op change", logmsg);
4976 break;
4977 case GOT_HISTEDIT_DROP:
4978 case GOT_HISTEDIT_FOLD:
4979 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4980 logmsg);
4981 break;
4982 default:
4983 break;
4986 done:
4987 free(old_id_str);
4988 free(new_id_str);
4989 return err;
4992 static const struct got_error *
4993 histedit_commit(struct got_pathlist_head *merged_paths,
4994 struct got_worktree *worktree, struct got_fileindex *fileindex,
4995 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4996 struct got_repository *repo)
4998 const struct got_error *err;
4999 struct got_commit_object *commit;
5000 struct got_object_id *new_commit_id;
5002 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5003 && hle->logmsg == NULL) {
5004 err = histedit_edit_logmsg(hle, repo);
5005 if (err)
5006 return err;
5009 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5010 if (err)
5011 return err;
5013 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5014 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5015 hle->logmsg, repo);
5016 if (err) {
5017 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5018 goto done;
5019 err = show_histedit_progress(commit, hle, NULL);
5020 } else {
5021 err = show_histedit_progress(commit, hle, new_commit_id);
5022 free(new_commit_id);
5024 done:
5025 got_object_commit_close(commit);
5026 return err;
5029 static const struct got_error *
5030 histedit_skip_commit(struct got_histedit_list_entry *hle,
5031 struct got_worktree *worktree, struct got_repository *repo)
5033 const struct got_error *error;
5034 struct got_commit_object *commit;
5036 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5037 repo);
5038 if (error)
5039 return error;
5041 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5042 if (error)
5043 return error;
5045 error = show_histedit_progress(commit, hle, NULL);
5046 got_object_commit_close(commit);
5047 return error;
5050 static const struct got_error *
5051 cmd_histedit(int argc, char *argv[])
5053 const struct got_error *error = NULL;
5054 struct got_worktree *worktree = NULL;
5055 struct got_fileindex *fileindex = NULL;
5056 struct got_repository *repo = NULL;
5057 char *cwd = NULL;
5058 struct got_reference *branch = NULL;
5059 struct got_reference *tmp_branch = NULL;
5060 struct got_object_id *resume_commit_id = NULL;
5061 struct got_object_id *base_commit_id = NULL;
5062 struct got_object_id *head_commit_id = NULL;
5063 struct got_commit_object *commit = NULL;
5064 int ch, rebase_in_progress = 0, did_something;
5065 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5066 const char *edit_script_path = NULL;
5067 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5068 struct got_object_id_queue commits;
5069 struct got_pathlist_head merged_paths;
5070 const struct got_object_id_queue *parent_ids;
5071 struct got_object_qid *pid;
5072 struct got_histedit_list histedit_cmds;
5073 struct got_histedit_list_entry *hle;
5075 SIMPLEQ_INIT(&commits);
5076 TAILQ_INIT(&histedit_cmds);
5077 TAILQ_INIT(&merged_paths);
5079 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5080 switch (ch) {
5081 case 'a':
5082 abort_edit = 1;
5083 break;
5084 case 'c':
5085 continue_edit = 1;
5086 break;
5087 case 'F':
5088 edit_script_path = optarg;
5089 break;
5090 default:
5091 usage_histedit();
5092 /* NOTREACHED */
5096 argc -= optind;
5097 argv += optind;
5099 #ifndef PROFILE
5100 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5101 "unveil", NULL) == -1)
5102 err(1, "pledge");
5103 #endif
5104 if (abort_edit && continue_edit)
5105 usage_histedit();
5106 if (argc != 0)
5107 usage_histedit();
5110 * This command cannot apply unveil(2) in all cases because the
5111 * user may choose to run an editor to edit the histedit script
5112 * and to edit individual commit log messages.
5113 * unveil(2) traverses exec(2); if an editor is used we have to
5114 * apply unveil after edit script and log messages have been written.
5115 * XXX TODO: Make use of unveil(2) where possible.
5118 cwd = getcwd(NULL, 0);
5119 if (cwd == NULL) {
5120 error = got_error_from_errno("getcwd");
5121 goto done;
5123 error = got_worktree_open(&worktree, cwd);
5124 if (error)
5125 goto done;
5127 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5128 if (error != NULL)
5129 goto done;
5131 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5132 if (error)
5133 goto done;
5134 if (rebase_in_progress) {
5135 error = got_error(GOT_ERR_REBASING);
5136 goto done;
5139 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5140 if (error)
5141 goto done;
5143 if (edit_in_progress && abort_edit) {
5144 error = got_worktree_histedit_continue(&resume_commit_id,
5145 &tmp_branch, &branch, &base_commit_id, &fileindex,
5146 worktree, repo);
5147 if (error)
5148 goto done;
5149 printf("Switching work tree to %s\n",
5150 got_ref_get_symref_target(branch));
5151 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5152 branch, base_commit_id, update_progress, &did_something);
5153 if (error)
5154 goto done;
5155 printf("Histedit of %s aborted\n",
5156 got_ref_get_symref_target(branch));
5157 goto done; /* nothing else to do */
5158 } else if (abort_edit) {
5159 error = got_error(GOT_ERR_NOT_HISTEDIT);
5160 goto done;
5163 if (continue_edit) {
5164 char *path;
5166 if (!edit_in_progress) {
5167 error = got_error(GOT_ERR_NOT_HISTEDIT);
5168 goto done;
5171 error = got_worktree_get_histedit_script_path(&path, worktree);
5172 if (error)
5173 goto done;
5175 error = histedit_load_list(&histedit_cmds, path, repo);
5176 free(path);
5177 if (error)
5178 goto done;
5180 error = got_worktree_histedit_continue(&resume_commit_id,
5181 &tmp_branch, &branch, &base_commit_id, &fileindex,
5182 worktree, repo);
5183 if (error)
5184 goto done;
5186 error = got_ref_resolve(&head_commit_id, repo, branch);
5187 if (error)
5188 goto done;
5190 error = got_object_open_as_commit(&commit, repo,
5191 head_commit_id);
5192 if (error)
5193 goto done;
5194 parent_ids = got_object_commit_get_parent_ids(commit);
5195 pid = SIMPLEQ_FIRST(parent_ids);
5196 if (pid == NULL) {
5197 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5198 goto done;
5200 error = collect_commits(&commits, head_commit_id, pid->id,
5201 base_commit_id, got_worktree_get_path_prefix(worktree),
5202 GOT_ERR_HISTEDIT_PATH, repo);
5203 got_object_commit_close(commit);
5204 commit = NULL;
5205 if (error)
5206 goto done;
5207 } else {
5208 if (edit_in_progress) {
5209 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5210 goto done;
5213 error = got_ref_open(&branch, repo,
5214 got_worktree_get_head_ref_name(worktree), 0);
5215 if (error != NULL)
5216 goto done;
5218 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5219 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5220 "will not edit commit history of a branch outside "
5221 "the \"refs/heads/\" reference namespace");
5222 goto done;
5225 error = got_ref_resolve(&head_commit_id, repo, branch);
5226 got_ref_close(branch);
5227 branch = NULL;
5228 if (error)
5229 goto done;
5231 error = got_object_open_as_commit(&commit, repo,
5232 head_commit_id);
5233 if (error)
5234 goto done;
5235 parent_ids = got_object_commit_get_parent_ids(commit);
5236 pid = SIMPLEQ_FIRST(parent_ids);
5237 if (pid == NULL) {
5238 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5239 goto done;
5241 error = collect_commits(&commits, head_commit_id, pid->id,
5242 got_worktree_get_base_commit_id(worktree),
5243 got_worktree_get_path_prefix(worktree),
5244 GOT_ERR_HISTEDIT_PATH, repo);
5245 got_object_commit_close(commit);
5246 commit = NULL;
5247 if (error)
5248 goto done;
5250 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5251 &base_commit_id, &fileindex, worktree, repo);
5252 if (error)
5253 goto done;
5255 if (edit_script_path) {
5256 error = histedit_load_list(&histedit_cmds,
5257 edit_script_path, repo);
5258 if (error) {
5259 got_worktree_histedit_abort(worktree, fileindex,
5260 repo, branch, base_commit_id,
5261 update_progress, &did_something);
5262 goto done;
5264 } else {
5265 error = histedit_edit_script(&histedit_cmds, &commits,
5266 repo);
5267 if (error) {
5268 got_worktree_histedit_abort(worktree, fileindex,
5269 repo, branch, base_commit_id,
5270 update_progress, &did_something);
5271 goto done;
5276 error = histedit_save_list(&histedit_cmds, worktree,
5277 repo);
5278 if (error) {
5279 got_worktree_histedit_abort(worktree, fileindex,
5280 repo, branch, base_commit_id,
5281 update_progress, &did_something);
5282 goto done;
5287 error = histedit_check_script(&histedit_cmds, &commits, repo);
5288 if (error)
5289 goto done;
5291 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5292 if (resume_commit_id) {
5293 if (got_object_id_cmp(hle->commit_id,
5294 resume_commit_id) != 0)
5295 continue;
5297 resume_commit_id = NULL;
5298 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5299 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5300 error = histedit_skip_commit(hle, worktree,
5301 repo);
5302 } else {
5303 error = histedit_commit(NULL, worktree,
5304 fileindex, tmp_branch, hle, repo);
5306 if (error)
5307 goto done;
5308 continue;
5311 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5312 error = histedit_skip_commit(hle, worktree, repo);
5313 if (error)
5314 goto done;
5315 continue;
5318 error = got_object_open_as_commit(&commit, repo,
5319 hle->commit_id);
5320 if (error)
5321 goto done;
5322 parent_ids = got_object_commit_get_parent_ids(commit);
5323 pid = SIMPLEQ_FIRST(parent_ids);
5325 error = got_worktree_histedit_merge_files(&merged_paths,
5326 worktree, fileindex, pid->id, hle->commit_id, repo,
5327 rebase_progress, &rebase_status, check_cancelled, NULL);
5328 if (error)
5329 goto done;
5330 got_object_commit_close(commit);
5331 commit = NULL;
5333 if (rebase_status == GOT_STATUS_CONFLICT) {
5334 got_worktree_rebase_pathlist_free(&merged_paths);
5335 break;
5338 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5339 char *id_str;
5340 error = got_object_id_str(&id_str, hle->commit_id);
5341 if (error)
5342 goto done;
5343 printf("Stopping histedit for amending commit %s\n",
5344 id_str);
5345 free(id_str);
5346 got_worktree_rebase_pathlist_free(&merged_paths);
5347 error = got_worktree_histedit_postpone(worktree,
5348 fileindex);
5349 goto done;
5352 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5353 error = histedit_skip_commit(hle, worktree, repo);
5354 if (error)
5355 goto done;
5356 continue;
5359 error = histedit_commit(&merged_paths, worktree, fileindex,
5360 tmp_branch, hle, repo);
5361 got_worktree_rebase_pathlist_free(&merged_paths);
5362 if (error)
5363 goto done;
5366 if (rebase_status == GOT_STATUS_CONFLICT) {
5367 error = got_worktree_histedit_postpone(worktree, fileindex);
5368 if (error)
5369 goto done;
5370 error = got_error_msg(GOT_ERR_CONFLICTS,
5371 "conflicts must be resolved before rebasing can continue");
5372 } else
5373 error = histedit_complete(worktree, fileindex, tmp_branch,
5374 branch, repo);
5375 done:
5376 got_object_id_queue_free(&commits);
5377 histedit_free_list(&histedit_cmds);
5378 free(head_commit_id);
5379 free(base_commit_id);
5380 free(resume_commit_id);
5381 if (commit)
5382 got_object_commit_close(commit);
5383 if (branch)
5384 got_ref_close(branch);
5385 if (tmp_branch)
5386 got_ref_close(tmp_branch);
5387 if (worktree)
5388 got_worktree_close(worktree);
5389 if (repo)
5390 got_repo_close(repo);
5391 return error;
5394 __dead static void
5395 usage_stage(void)
5397 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5398 "[file-path ...]\n",
5399 getprogname());
5400 exit(1);
5403 static const struct got_error *
5404 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5405 const char *path, struct got_object_id *blob_id,
5406 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5408 const struct got_error *err = NULL;
5409 char *id_str = NULL;
5411 if (staged_status != GOT_STATUS_ADD &&
5412 staged_status != GOT_STATUS_MODIFY &&
5413 staged_status != GOT_STATUS_DELETE)
5414 return NULL;
5416 if (staged_status == GOT_STATUS_ADD ||
5417 staged_status == GOT_STATUS_MODIFY)
5418 err = got_object_id_str(&id_str, staged_blob_id);
5419 else
5420 err = got_object_id_str(&id_str, blob_id);
5421 if (err)
5422 return err;
5424 printf("%s %c %s\n", id_str, staged_status, path);
5425 free(id_str);
5426 return NULL;
5429 static const struct got_error *
5430 cmd_stage(int argc, char *argv[])
5432 const struct got_error *error = NULL;
5433 struct got_repository *repo = NULL;
5434 struct got_worktree *worktree = NULL;
5435 char *cwd = NULL;
5436 struct got_pathlist_head paths;
5437 struct got_pathlist_entry *pe;
5438 int ch, list_stage = 0, pflag = 0;
5439 FILE *patch_script_file = NULL;
5440 const char *patch_script_path = NULL;
5441 struct choose_patch_arg cpa;
5443 TAILQ_INIT(&paths);
5445 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5446 switch (ch) {
5447 case 'l':
5448 list_stage = 1;
5449 break;
5450 case 'p':
5451 pflag = 1;
5452 break;
5453 case 'F':
5454 patch_script_path = optarg;
5455 break;
5456 default:
5457 usage_stage();
5458 /* NOTREACHED */
5462 argc -= optind;
5463 argv += optind;
5465 #ifndef PROFILE
5466 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5467 "unveil", NULL) == -1)
5468 err(1, "pledge");
5469 #endif
5470 if (list_stage && (pflag || patch_script_path))
5471 errx(1, "-l option cannot be used with other options");
5472 if (patch_script_path && !pflag)
5473 errx(1, "-F option can only be used together with -p option");
5475 cwd = getcwd(NULL, 0);
5476 if (cwd == NULL) {
5477 error = got_error_from_errno("getcwd");
5478 goto done;
5481 error = got_worktree_open(&worktree, cwd);
5482 if (error)
5483 goto done;
5485 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5486 if (error != NULL)
5487 goto done;
5489 if (patch_script_path) {
5490 patch_script_file = fopen(patch_script_path, "r");
5491 if (patch_script_file == NULL) {
5492 error = got_error_from_errno2("fopen",
5493 patch_script_path);
5494 goto done;
5497 error = apply_unveil(got_repo_get_path(repo), 1,
5498 got_worktree_get_root_path(worktree));
5499 if (error)
5500 goto done;
5502 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5503 if (error)
5504 goto done;
5506 if (list_stage)
5507 error = got_worktree_status(worktree, &paths, repo,
5508 print_stage, NULL, check_cancelled, NULL);
5509 else {
5510 cpa.patch_script_file = patch_script_file;
5511 cpa.action = "stage";
5512 error = got_worktree_stage(worktree, &paths,
5513 pflag ? NULL : print_status, NULL,
5514 pflag ? choose_patch : NULL, &cpa, repo);
5516 done:
5517 if (patch_script_file && fclose(patch_script_file) == EOF &&
5518 error == NULL)
5519 error = got_error_from_errno2("fclose", patch_script_path);
5520 if (repo)
5521 got_repo_close(repo);
5522 if (worktree)
5523 got_worktree_close(worktree);
5524 TAILQ_FOREACH(pe, &paths, entry)
5525 free((char *)pe->path);
5526 got_pathlist_free(&paths);
5527 free(cwd);
5528 return error;
5531 __dead static void
5532 usage_unstage(void)
5534 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5535 "[file-path ...]\n",
5536 getprogname());
5537 exit(1);
5541 static const struct got_error *
5542 cmd_unstage(int argc, char *argv[])
5544 const struct got_error *error = NULL;
5545 struct got_repository *repo = NULL;
5546 struct got_worktree *worktree = NULL;
5547 char *cwd = NULL;
5548 struct got_pathlist_head paths;
5549 struct got_pathlist_entry *pe;
5550 int ch, did_something = 0, pflag = 0;
5551 FILE *patch_script_file = NULL;
5552 const char *patch_script_path = NULL;
5553 struct choose_patch_arg cpa;
5555 TAILQ_INIT(&paths);
5557 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5558 switch (ch) {
5559 case 'p':
5560 pflag = 1;
5561 break;
5562 case 'F':
5563 patch_script_path = optarg;
5564 break;
5565 default:
5566 usage_unstage();
5567 /* NOTREACHED */
5571 argc -= optind;
5572 argv += optind;
5574 #ifndef PROFILE
5575 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5576 "unveil", NULL) == -1)
5577 err(1, "pledge");
5578 #endif
5579 if (patch_script_path && !pflag)
5580 errx(1, "-F option can only be used together with -p option");
5582 cwd = getcwd(NULL, 0);
5583 if (cwd == NULL) {
5584 error = got_error_from_errno("getcwd");
5585 goto done;
5588 error = got_worktree_open(&worktree, cwd);
5589 if (error)
5590 goto done;
5592 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5593 if (error != NULL)
5594 goto done;
5596 if (patch_script_path) {
5597 patch_script_file = fopen(patch_script_path, "r");
5598 if (patch_script_file == NULL) {
5599 error = got_error_from_errno2("fopen",
5600 patch_script_path);
5601 goto done;
5605 error = apply_unveil(got_repo_get_path(repo), 1,
5606 got_worktree_get_root_path(worktree));
5607 if (error)
5608 goto done;
5610 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5611 if (error)
5612 goto done;
5614 cpa.patch_script_file = patch_script_file;
5615 cpa.action = "unstage";
5616 error = got_worktree_unstage(worktree, &paths, update_progress,
5617 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5618 done:
5619 if (patch_script_file && fclose(patch_script_file) == EOF &&
5620 error == NULL)
5621 error = got_error_from_errno2("fclose", patch_script_path);
5622 if (repo)
5623 got_repo_close(repo);
5624 if (worktree)
5625 got_worktree_close(worktree);
5626 TAILQ_FOREACH(pe, &paths, entry)
5627 free((char *)pe->path);
5628 got_pathlist_free(&paths);
5629 free(cwd);
5630 return error;