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/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.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 match_object_id(struct got_object_id **id, char **label,
1858 const char *id_str, int obj_type, struct got_repository *repo)
1860 const struct got_error *err;
1861 struct got_tag_object *tag;
1862 struct got_reference *ref = NULL;
1864 *id = NULL;
1865 *label = NULL;
1867 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1868 if (err == NULL) {
1869 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1870 if (*id == NULL)
1871 err = got_error_from_errno("got_object_id_dup");
1872 if (asprintf(label, "refs/tags/%s",
1873 got_object_tag_get_name(tag)) == -1)
1874 err = got_error_from_errno("asprintf");
1875 got_object_tag_close(tag);
1876 return err;
1877 } else if (err->code != GOT_ERR_NO_OBJ)
1878 return err;
1880 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1881 if (err) {
1882 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1883 return err;
1884 err = got_ref_open(&ref, repo, id_str, 0);
1885 if (err != NULL)
1886 goto done;
1887 *label = strdup(got_ref_get_name(ref));
1888 if (*label == NULL) {
1889 err = got_error_from_errno("strdup");
1890 goto done;
1892 err = got_ref_resolve(id, repo, ref);
1893 } else {
1894 err = got_object_id_str(label, *id);
1895 if (*label == NULL) {
1896 err = got_error_from_errno("strdup");
1897 goto done;
1900 done:
1901 if (ref)
1902 got_ref_close(ref);
1903 return err;
1907 static const struct got_error *
1908 cmd_diff(int argc, char *argv[])
1910 const struct got_error *error;
1911 struct got_repository *repo = NULL;
1912 struct got_worktree *worktree = NULL;
1913 char *cwd = NULL, *repo_path = NULL;
1914 struct got_object_id *id1 = NULL, *id2 = NULL;
1915 const char *id_str1 = NULL, *id_str2 = NULL;
1916 char *label1 = NULL, *label2 = NULL;
1917 int type1, type2;
1918 int diff_context = 3, diff_staged = 0, ch;
1919 const char *errstr;
1920 char *path = NULL;
1922 #ifndef PROFILE
1923 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1924 NULL) == -1)
1925 err(1, "pledge");
1926 #endif
1928 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1929 switch (ch) {
1930 case 'C':
1931 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1932 if (errstr != NULL)
1933 err(1, "-C option %s", errstr);
1934 break;
1935 case 'r':
1936 repo_path = realpath(optarg, NULL);
1937 if (repo_path == NULL)
1938 err(1, "-r option");
1939 got_path_strip_trailing_slashes(repo_path);
1940 break;
1941 case 's':
1942 diff_staged = 1;
1943 break;
1944 default:
1945 usage_diff();
1946 /* NOTREACHED */
1950 argc -= optind;
1951 argv += optind;
1953 cwd = getcwd(NULL, 0);
1954 if (cwd == NULL) {
1955 error = got_error_from_errno("getcwd");
1956 goto done;
1958 error = got_worktree_open(&worktree, cwd);
1959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1960 goto done;
1961 if (argc <= 1) {
1962 if (worktree == NULL) {
1963 error = got_error(GOT_ERR_NOT_WORKTREE);
1964 goto done;
1966 if (repo_path)
1967 errx(1,
1968 "-r option can't be used when diffing a work tree");
1969 repo_path = strdup(got_worktree_get_repo_path(worktree));
1970 if (repo_path == NULL) {
1971 error = got_error_from_errno("strdup");
1972 goto done;
1974 if (argc == 1) {
1975 error = got_worktree_resolve_path(&path, worktree,
1976 argv[0]);
1977 if (error)
1978 goto done;
1979 } else {
1980 path = strdup("");
1981 if (path == NULL) {
1982 error = got_error_from_errno("strdup");
1983 goto done;
1986 } else if (argc == 2) {
1987 if (diff_staged)
1988 errx(1, "-s option can't be used when diffing "
1989 "objects in repository");
1990 id_str1 = argv[0];
1991 id_str2 = argv[1];
1992 if (worktree && repo_path == NULL) {
1993 repo_path =
1994 strdup(got_worktree_get_repo_path(worktree));
1995 if (repo_path == NULL) {
1996 error = got_error_from_errno("strdup");
1997 goto done;
2000 } else
2001 usage_diff();
2003 if (repo_path == NULL) {
2004 repo_path = getcwd(NULL, 0);
2005 if (repo_path == NULL)
2006 return got_error_from_errno("getcwd");
2009 error = got_repo_open(&repo, repo_path);
2010 free(repo_path);
2011 if (error != NULL)
2012 goto done;
2014 error = apply_unveil(got_repo_get_path(repo), 1,
2015 worktree ? got_worktree_get_root_path(worktree) : NULL);
2016 if (error)
2017 goto done;
2019 if (argc <= 1) {
2020 struct print_diff_arg arg;
2021 struct got_pathlist_head paths;
2022 char *id_str;
2024 TAILQ_INIT(&paths);
2026 error = got_object_id_str(&id_str,
2027 got_worktree_get_base_commit_id(worktree));
2028 if (error)
2029 goto done;
2030 arg.repo = repo;
2031 arg.worktree = worktree;
2032 arg.diff_context = diff_context;
2033 arg.id_str = id_str;
2034 arg.header_shown = 0;
2035 arg.diff_staged = diff_staged;
2037 error = got_pathlist_append(&paths, path, NULL);
2038 if (error)
2039 goto done;
2041 error = got_worktree_status(worktree, &paths, repo, print_diff,
2042 &arg, check_cancelled, NULL);
2043 free(id_str);
2044 got_pathlist_free(&paths);
2045 goto done;
2048 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2049 if (error)
2050 goto done;
2052 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2053 if (error)
2054 goto done;
2056 error = got_object_get_type(&type1, repo, id1);
2057 if (error)
2058 goto done;
2060 error = got_object_get_type(&type2, repo, id2);
2061 if (error)
2062 goto done;
2064 if (type1 != type2) {
2065 error = got_error(GOT_ERR_OBJ_TYPE);
2066 goto done;
2069 switch (type1) {
2070 case GOT_OBJ_TYPE_BLOB:
2071 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2072 diff_context, repo, stdout);
2073 break;
2074 case GOT_OBJ_TYPE_TREE:
2075 error = got_diff_objects_as_trees(id1, id2, "", "",
2076 diff_context, repo, stdout);
2077 break;
2078 case GOT_OBJ_TYPE_COMMIT:
2079 printf("diff %s %s\n", label1, label2);
2080 error = got_diff_objects_as_commits(id1, id2, diff_context,
2081 repo, stdout);
2082 break;
2083 default:
2084 error = got_error(GOT_ERR_OBJ_TYPE);
2087 done:
2088 free(label1);
2089 free(label2);
2090 free(id1);
2091 free(id2);
2092 free(path);
2093 if (worktree)
2094 got_worktree_close(worktree);
2095 if (repo) {
2096 const struct got_error *repo_error;
2097 repo_error = got_repo_close(repo);
2098 if (error == NULL)
2099 error = repo_error;
2101 return error;
2104 __dead static void
2105 usage_blame(void)
2107 fprintf(stderr,
2108 "usage: %s blame [-c commit] [-r repository-path] path\n",
2109 getprogname());
2110 exit(1);
2113 static const struct got_error *
2114 cmd_blame(int argc, char *argv[])
2116 const struct got_error *error;
2117 struct got_repository *repo = NULL;
2118 struct got_worktree *worktree = NULL;
2119 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2120 struct got_object_id *commit_id = NULL;
2121 char *commit_id_str = NULL;
2122 int ch;
2124 #ifndef PROFILE
2125 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2126 NULL) == -1)
2127 err(1, "pledge");
2128 #endif
2130 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2131 switch (ch) {
2132 case 'c':
2133 commit_id_str = optarg;
2134 break;
2135 case 'r':
2136 repo_path = realpath(optarg, NULL);
2137 if (repo_path == NULL)
2138 err(1, "-r option");
2139 got_path_strip_trailing_slashes(repo_path);
2140 break;
2141 default:
2142 usage_blame();
2143 /* NOTREACHED */
2147 argc -= optind;
2148 argv += optind;
2150 if (argc == 1)
2151 path = argv[0];
2152 else
2153 usage_blame();
2155 cwd = getcwd(NULL, 0);
2156 if (cwd == NULL) {
2157 error = got_error_from_errno("getcwd");
2158 goto done;
2160 if (repo_path == NULL) {
2161 error = got_worktree_open(&worktree, cwd);
2162 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2163 goto done;
2164 else
2165 error = NULL;
2166 if (worktree) {
2167 repo_path =
2168 strdup(got_worktree_get_repo_path(worktree));
2169 if (repo_path == NULL)
2170 error = got_error_from_errno("strdup");
2171 if (error)
2172 goto done;
2173 } else {
2174 repo_path = strdup(cwd);
2175 if (repo_path == NULL) {
2176 error = got_error_from_errno("strdup");
2177 goto done;
2182 error = got_repo_open(&repo, repo_path);
2183 if (error != NULL)
2184 goto done;
2186 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2187 if (error)
2188 goto done;
2190 if (worktree) {
2191 const char *prefix = got_worktree_get_path_prefix(worktree);
2192 char *p, *worktree_subdir = cwd +
2193 strlen(got_worktree_get_root_path(worktree));
2194 if (asprintf(&p, "%s%s%s%s%s",
2195 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2196 worktree_subdir, worktree_subdir[0] ? "/" : "",
2197 path) == -1) {
2198 error = got_error_from_errno("asprintf");
2199 goto done;
2201 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2202 free(p);
2203 } else {
2204 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2206 if (error)
2207 goto done;
2209 if (commit_id_str == NULL) {
2210 struct got_reference *head_ref;
2211 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2212 if (error != NULL)
2213 goto done;
2214 error = got_ref_resolve(&commit_id, repo, head_ref);
2215 got_ref_close(head_ref);
2216 if (error != NULL)
2217 goto done;
2218 } else {
2219 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2220 if (error)
2221 goto done;
2224 error = got_blame(in_repo_path, commit_id, repo, stdout);
2225 done:
2226 free(in_repo_path);
2227 free(repo_path);
2228 free(cwd);
2229 free(commit_id);
2230 if (worktree)
2231 got_worktree_close(worktree);
2232 if (repo) {
2233 const struct got_error *repo_error;
2234 repo_error = got_repo_close(repo);
2235 if (error == NULL)
2236 error = repo_error;
2238 return error;
2241 __dead static void
2242 usage_tree(void)
2244 fprintf(stderr,
2245 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2246 getprogname());
2247 exit(1);
2250 static void
2251 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2252 const char *root_path)
2254 int is_root_path = (strcmp(path, root_path) == 0);
2255 const char *modestr = "";
2257 path += strlen(root_path);
2258 while (path[0] == '/')
2259 path++;
2261 if (S_ISLNK(te->mode))
2262 modestr = "@";
2263 else if (S_ISDIR(te->mode))
2264 modestr = "/";
2265 else if (te->mode & S_IXUSR)
2266 modestr = "*";
2268 printf("%s%s%s%s%s\n", id ? id : "", path,
2269 is_root_path ? "" : "/", te->name, modestr);
2272 static const struct got_error *
2273 print_tree(const char *path, struct got_object_id *commit_id,
2274 int show_ids, int recurse, const char *root_path,
2275 struct got_repository *repo)
2277 const struct got_error *err = NULL;
2278 struct got_object_id *tree_id = NULL;
2279 struct got_tree_object *tree = NULL;
2280 const struct got_tree_entries *entries;
2281 struct got_tree_entry *te;
2283 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2284 if (err)
2285 goto done;
2287 err = got_object_open_as_tree(&tree, repo, tree_id);
2288 if (err)
2289 goto done;
2290 entries = got_object_tree_get_entries(tree);
2291 te = SIMPLEQ_FIRST(&entries->head);
2292 while (te) {
2293 char *id = NULL;
2295 if (sigint_received || sigpipe_received)
2296 break;
2298 if (show_ids) {
2299 char *id_str;
2300 err = got_object_id_str(&id_str, te->id);
2301 if (err)
2302 goto done;
2303 if (asprintf(&id, "%s ", id_str) == -1) {
2304 err = got_error_from_errno("asprintf");
2305 free(id_str);
2306 goto done;
2308 free(id_str);
2310 print_entry(te, id, path, root_path);
2311 free(id);
2313 if (recurse && S_ISDIR(te->mode)) {
2314 char *child_path;
2315 if (asprintf(&child_path, "%s%s%s", path,
2316 path[0] == '/' && path[1] == '\0' ? "" : "/",
2317 te->name) == -1) {
2318 err = got_error_from_errno("asprintf");
2319 goto done;
2321 err = print_tree(child_path, commit_id, show_ids, 1,
2322 root_path, repo);
2323 free(child_path);
2324 if (err)
2325 goto done;
2328 te = SIMPLEQ_NEXT(te, entry);
2330 done:
2331 if (tree)
2332 got_object_tree_close(tree);
2333 free(tree_id);
2334 return err;
2337 static const struct got_error *
2338 cmd_tree(int argc, char *argv[])
2340 const struct got_error *error;
2341 struct got_repository *repo = NULL;
2342 struct got_worktree *worktree = NULL;
2343 const char *path;
2344 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2345 struct got_object_id *commit_id = NULL;
2346 char *commit_id_str = NULL;
2347 int show_ids = 0, recurse = 0;
2348 int ch;
2350 #ifndef PROFILE
2351 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2352 NULL) == -1)
2353 err(1, "pledge");
2354 #endif
2356 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2357 switch (ch) {
2358 case 'c':
2359 commit_id_str = optarg;
2360 break;
2361 case 'r':
2362 repo_path = realpath(optarg, NULL);
2363 if (repo_path == NULL)
2364 err(1, "-r option");
2365 got_path_strip_trailing_slashes(repo_path);
2366 break;
2367 case 'i':
2368 show_ids = 1;
2369 break;
2370 case 'R':
2371 recurse = 1;
2372 break;
2373 default:
2374 usage_tree();
2375 /* NOTREACHED */
2379 argc -= optind;
2380 argv += optind;
2382 if (argc == 1)
2383 path = argv[0];
2384 else if (argc > 1)
2385 usage_tree();
2386 else
2387 path = NULL;
2389 cwd = getcwd(NULL, 0);
2390 if (cwd == NULL) {
2391 error = got_error_from_errno("getcwd");
2392 goto done;
2394 if (repo_path == NULL) {
2395 error = got_worktree_open(&worktree, cwd);
2396 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2397 goto done;
2398 else
2399 error = NULL;
2400 if (worktree) {
2401 repo_path =
2402 strdup(got_worktree_get_repo_path(worktree));
2403 if (repo_path == NULL)
2404 error = got_error_from_errno("strdup");
2405 if (error)
2406 goto done;
2407 } else {
2408 repo_path = strdup(cwd);
2409 if (repo_path == NULL) {
2410 error = got_error_from_errno("strdup");
2411 goto done;
2416 error = got_repo_open(&repo, repo_path);
2417 if (error != NULL)
2418 goto done;
2420 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2421 if (error)
2422 goto done;
2424 if (path == NULL) {
2425 if (worktree) {
2426 char *p, *worktree_subdir = cwd +
2427 strlen(got_worktree_get_root_path(worktree));
2428 if (asprintf(&p, "%s/%s",
2429 got_worktree_get_path_prefix(worktree),
2430 worktree_subdir) == -1) {
2431 error = got_error_from_errno("asprintf");
2432 goto done;
2434 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2435 free(p);
2436 if (error)
2437 goto done;
2438 } else
2439 path = "/";
2441 if (in_repo_path == NULL) {
2442 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2443 if (error != NULL)
2444 goto done;
2447 if (commit_id_str == NULL) {
2448 struct got_reference *head_ref;
2449 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2450 if (error != NULL)
2451 goto done;
2452 error = got_ref_resolve(&commit_id, repo, head_ref);
2453 got_ref_close(head_ref);
2454 if (error != NULL)
2455 goto done;
2456 } else {
2457 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2458 if (error)
2459 goto done;
2462 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2463 in_repo_path, repo);
2464 done:
2465 free(in_repo_path);
2466 free(repo_path);
2467 free(cwd);
2468 free(commit_id);
2469 if (worktree)
2470 got_worktree_close(worktree);
2471 if (repo) {
2472 const struct got_error *repo_error;
2473 repo_error = got_repo_close(repo);
2474 if (error == NULL)
2475 error = repo_error;
2477 return error;
2480 __dead static void
2481 usage_status(void)
2483 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2484 exit(1);
2487 static const struct got_error *
2488 print_status(void *arg, unsigned char status, unsigned char staged_status,
2489 const char *path, struct got_object_id *blob_id,
2490 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2492 if (status == staged_status && (status == GOT_STATUS_DELETE))
2493 status = GOT_STATUS_NO_CHANGE;
2494 printf("%c%c %s\n", status, staged_status, path);
2495 return NULL;
2498 static const struct got_error *
2499 cmd_status(int argc, char *argv[])
2501 const struct got_error *error = NULL;
2502 struct got_repository *repo = NULL;
2503 struct got_worktree *worktree = NULL;
2504 char *cwd = NULL;
2505 struct got_pathlist_head paths;
2506 struct got_pathlist_entry *pe;
2507 int ch;
2509 TAILQ_INIT(&paths);
2511 while ((ch = getopt(argc, argv, "")) != -1) {
2512 switch (ch) {
2513 default:
2514 usage_status();
2515 /* NOTREACHED */
2519 argc -= optind;
2520 argv += optind;
2522 #ifndef PROFILE
2523 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2524 NULL) == -1)
2525 err(1, "pledge");
2526 #endif
2527 cwd = getcwd(NULL, 0);
2528 if (cwd == NULL) {
2529 error = got_error_from_errno("getcwd");
2530 goto done;
2533 error = got_worktree_open(&worktree, cwd);
2534 if (error != NULL)
2535 goto done;
2537 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2538 if (error != NULL)
2539 goto done;
2541 error = apply_unveil(got_repo_get_path(repo), 1,
2542 got_worktree_get_root_path(worktree));
2543 if (error)
2544 goto done;
2546 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2547 if (error)
2548 goto done;
2550 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2551 check_cancelled, NULL);
2552 done:
2553 TAILQ_FOREACH(pe, &paths, entry)
2554 free((char *)pe->path);
2555 got_pathlist_free(&paths);
2556 free(cwd);
2557 return error;
2560 __dead static void
2561 usage_ref(void)
2563 fprintf(stderr,
2564 "usage: %s ref [-r repository] -l | -d name | name target\n",
2565 getprogname());
2566 exit(1);
2569 static const struct got_error *
2570 list_refs(struct got_repository *repo)
2572 static const struct got_error *err = NULL;
2573 struct got_reflist_head refs;
2574 struct got_reflist_entry *re;
2576 SIMPLEQ_INIT(&refs);
2577 err = got_ref_list(&refs, repo);
2578 if (err)
2579 return err;
2581 SIMPLEQ_FOREACH(re, &refs, entry) {
2582 char *refstr;
2583 refstr = got_ref_to_str(re->ref);
2584 if (refstr == NULL)
2585 return got_error_from_errno("got_ref_to_str");
2586 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2587 free(refstr);
2590 got_ref_list_free(&refs);
2591 return NULL;
2594 static const struct got_error *
2595 delete_ref(struct got_repository *repo, const char *refname)
2597 const struct got_error *err = NULL;
2598 struct got_reference *ref;
2600 err = got_ref_open(&ref, repo, refname, 0);
2601 if (err)
2602 return err;
2604 err = got_ref_delete(ref, repo);
2605 got_ref_close(ref);
2606 return err;
2609 static const struct got_error *
2610 add_ref(struct got_repository *repo, const char *refname, const char *target)
2612 const struct got_error *err = NULL;
2613 struct got_object_id *id;
2614 struct got_reference *ref = NULL;
2617 * Don't let the user create a reference named '-'.
2618 * While technically a valid reference name, this case is usually
2619 * an unintended typo.
2621 if (refname[0] == '-' && refname[1] == '\0')
2622 return got_error(GOT_ERR_BAD_REF_NAME);
2624 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2625 repo);
2626 if (err) {
2627 struct got_reference *target_ref;
2629 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2630 return err;
2631 err = got_ref_open(&target_ref, repo, target, 0);
2632 if (err)
2633 return err;
2634 err = got_ref_resolve(&id, repo, target_ref);
2635 got_ref_close(target_ref);
2636 if (err)
2637 return err;
2640 err = got_ref_alloc(&ref, refname, id);
2641 if (err)
2642 goto done;
2644 err = got_ref_write(ref, repo);
2645 done:
2646 if (ref)
2647 got_ref_close(ref);
2648 free(id);
2649 return err;
2652 static const struct got_error *
2653 cmd_ref(int argc, char *argv[])
2655 const struct got_error *error = NULL;
2656 struct got_repository *repo = NULL;
2657 struct got_worktree *worktree = NULL;
2658 char *cwd = NULL, *repo_path = NULL;
2659 int ch, do_list = 0;
2660 const char *delref = NULL;
2662 /* TODO: Add -s option for adding symbolic references. */
2663 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2664 switch (ch) {
2665 case 'd':
2666 delref = optarg;
2667 break;
2668 case 'r':
2669 repo_path = realpath(optarg, NULL);
2670 if (repo_path == NULL)
2671 err(1, "-r option");
2672 got_path_strip_trailing_slashes(repo_path);
2673 break;
2674 case 'l':
2675 do_list = 1;
2676 break;
2677 default:
2678 usage_ref();
2679 /* NOTREACHED */
2683 if (do_list && delref)
2684 errx(1, "-l and -d options are mutually exclusive\n");
2686 argc -= optind;
2687 argv += optind;
2689 if (do_list || delref) {
2690 if (argc > 0)
2691 usage_ref();
2692 } else if (argc != 2)
2693 usage_ref();
2695 #ifndef PROFILE
2696 if (do_list) {
2697 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2698 NULL) == -1)
2699 err(1, "pledge");
2700 } else {
2701 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2702 "sendfd unveil", NULL) == -1)
2703 err(1, "pledge");
2705 #endif
2706 cwd = getcwd(NULL, 0);
2707 if (cwd == NULL) {
2708 error = got_error_from_errno("getcwd");
2709 goto done;
2712 if (repo_path == NULL) {
2713 error = got_worktree_open(&worktree, cwd);
2714 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2715 goto done;
2716 else
2717 error = NULL;
2718 if (worktree) {
2719 repo_path =
2720 strdup(got_worktree_get_repo_path(worktree));
2721 if (repo_path == NULL)
2722 error = got_error_from_errno("strdup");
2723 if (error)
2724 goto done;
2725 } else {
2726 repo_path = strdup(cwd);
2727 if (repo_path == NULL) {
2728 error = got_error_from_errno("strdup");
2729 goto done;
2734 error = got_repo_open(&repo, repo_path);
2735 if (error != NULL)
2736 goto done;
2738 error = apply_unveil(got_repo_get_path(repo), do_list,
2739 worktree ? got_worktree_get_root_path(worktree) : NULL);
2740 if (error)
2741 goto done;
2743 if (do_list)
2744 error = list_refs(repo);
2745 else if (delref)
2746 error = delete_ref(repo, delref);
2747 else
2748 error = add_ref(repo, argv[0], argv[1]);
2749 done:
2750 if (repo)
2751 got_repo_close(repo);
2752 if (worktree)
2753 got_worktree_close(worktree);
2754 free(cwd);
2755 free(repo_path);
2756 return error;
2759 __dead static void
2760 usage_branch(void)
2762 fprintf(stderr,
2763 "usage: %s branch [-r repository] -l | -d name | "
2764 "name [base-branch]\n", getprogname());
2765 exit(1);
2768 static const struct got_error *
2769 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2771 static const struct got_error *err = NULL;
2772 struct got_reflist_head refs;
2773 struct got_reflist_entry *re;
2775 SIMPLEQ_INIT(&refs);
2777 err = got_ref_list(&refs, repo);
2778 if (err)
2779 return err;
2781 SIMPLEQ_FOREACH(re, &refs, entry) {
2782 const char *refname, *marker = " ";
2783 char *refstr;
2784 refname = got_ref_get_name(re->ref);
2785 if (strncmp(refname, "refs/heads/", 11) != 0)
2786 continue;
2787 if (worktree && strcmp(refname,
2788 got_worktree_get_head_ref_name(worktree)) == 0) {
2789 struct got_object_id *id = NULL;
2790 err = got_ref_resolve(&id, repo, re->ref);
2791 if (err)
2792 return err;
2793 if (got_object_id_cmp(id,
2794 got_worktree_get_base_commit_id(worktree)) == 0)
2795 marker = "* ";
2796 else
2797 marker = "~ ";
2798 free(id);
2800 refname += 11;
2801 refstr = got_ref_to_str(re->ref);
2802 if (refstr == NULL)
2803 return got_error_from_errno("got_ref_to_str");
2804 printf("%s%s: %s\n", marker, refname, refstr);
2805 free(refstr);
2808 got_ref_list_free(&refs);
2809 return NULL;
2812 static const struct got_error *
2813 delete_branch(struct got_repository *repo, const char *branch_name)
2815 const struct got_error *err = NULL;
2816 struct got_reference *ref;
2817 char *refname;
2819 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2820 return got_error_from_errno("asprintf");
2822 err = got_ref_open(&ref, repo, refname, 0);
2823 if (err)
2824 goto done;
2826 err = got_ref_delete(ref, repo);
2827 got_ref_close(ref);
2828 done:
2829 free(refname);
2830 return err;
2833 static const struct got_error *
2834 add_branch(struct got_repository *repo, const char *branch_name,
2835 const char *base_branch)
2837 const struct got_error *err = NULL;
2838 struct got_object_id *id = NULL;
2839 struct got_reference *ref = NULL;
2840 char *base_refname = NULL, *refname = NULL;
2841 struct got_reference *base_ref;
2844 * Don't let the user create a branch named '-'.
2845 * While technically a valid reference name, this case is usually
2846 * an unintended typo.
2848 if (branch_name[0] == '-' && branch_name[1] == '\0')
2849 return got_error(GOT_ERR_BAD_REF_NAME);
2851 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2852 base_refname = strdup(GOT_REF_HEAD);
2853 if (base_refname == NULL)
2854 return got_error_from_errno("strdup");
2855 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2856 return got_error_from_errno("asprintf");
2858 err = got_ref_open(&base_ref, repo, base_refname, 0);
2859 if (err)
2860 goto done;
2861 err = got_ref_resolve(&id, repo, base_ref);
2862 got_ref_close(base_ref);
2863 if (err)
2864 goto done;
2866 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2867 err = got_error_from_errno("asprintf");
2868 goto done;
2871 err = got_ref_open(&ref, repo, refname, 0);
2872 if (err == NULL) {
2873 err = got_error(GOT_ERR_BRANCH_EXISTS);
2874 goto done;
2875 } else if (err->code != GOT_ERR_NOT_REF)
2876 goto done;
2878 err = got_ref_alloc(&ref, refname, id);
2879 if (err)
2880 goto done;
2882 err = got_ref_write(ref, repo);
2883 done:
2884 if (ref)
2885 got_ref_close(ref);
2886 free(id);
2887 free(base_refname);
2888 free(refname);
2889 return err;
2892 static const struct got_error *
2893 cmd_branch(int argc, char *argv[])
2895 const struct got_error *error = NULL;
2896 struct got_repository *repo = NULL;
2897 struct got_worktree *worktree = NULL;
2898 char *cwd = NULL, *repo_path = NULL;
2899 int ch, do_list = 0;
2900 const char *delref = NULL;
2902 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2903 switch (ch) {
2904 case 'd':
2905 delref = optarg;
2906 break;
2907 case 'r':
2908 repo_path = realpath(optarg, NULL);
2909 if (repo_path == NULL)
2910 err(1, "-r option");
2911 got_path_strip_trailing_slashes(repo_path);
2912 break;
2913 case 'l':
2914 do_list = 1;
2915 break;
2916 default:
2917 usage_branch();
2918 /* NOTREACHED */
2922 if (do_list && delref)
2923 errx(1, "-l and -d options are mutually exclusive\n");
2925 argc -= optind;
2926 argv += optind;
2928 if (do_list || delref) {
2929 if (argc > 0)
2930 usage_branch();
2931 } else if (argc < 1 || argc > 2)
2932 usage_branch();
2934 #ifndef PROFILE
2935 if (do_list) {
2936 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2937 NULL) == -1)
2938 err(1, "pledge");
2939 } else {
2940 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2941 "sendfd unveil", NULL) == -1)
2942 err(1, "pledge");
2944 #endif
2945 cwd = getcwd(NULL, 0);
2946 if (cwd == NULL) {
2947 error = got_error_from_errno("getcwd");
2948 goto done;
2951 if (repo_path == NULL) {
2952 error = got_worktree_open(&worktree, cwd);
2953 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2954 goto done;
2955 else
2956 error = NULL;
2957 if (worktree) {
2958 repo_path =
2959 strdup(got_worktree_get_repo_path(worktree));
2960 if (repo_path == NULL)
2961 error = got_error_from_errno("strdup");
2962 if (error)
2963 goto done;
2964 } else {
2965 repo_path = strdup(cwd);
2966 if (repo_path == NULL) {
2967 error = got_error_from_errno("strdup");
2968 goto done;
2973 error = got_repo_open(&repo, repo_path);
2974 if (error != NULL)
2975 goto done;
2977 error = apply_unveil(got_repo_get_path(repo), do_list,
2978 worktree ? got_worktree_get_root_path(worktree) : NULL);
2979 if (error)
2980 goto done;
2982 if (do_list)
2983 error = list_branches(repo, worktree);
2984 else if (delref)
2985 error = delete_branch(repo, delref);
2986 else {
2987 const char *base_branch;
2988 if (argc == 1) {
2989 base_branch = worktree ?
2990 got_worktree_get_head_ref_name(worktree) :
2991 GOT_REF_HEAD;
2992 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2993 base_branch += 11;
2994 } else
2995 base_branch = argv[1];
2996 error = add_branch(repo, argv[0], base_branch);
2998 done:
2999 if (repo)
3000 got_repo_close(repo);
3001 if (worktree)
3002 got_worktree_close(worktree);
3003 free(cwd);
3004 free(repo_path);
3005 return error;
3008 __dead static void
3009 usage_add(void)
3011 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3012 exit(1);
3015 static const struct got_error *
3016 cmd_add(int argc, char *argv[])
3018 const struct got_error *error = NULL;
3019 struct got_repository *repo = NULL;
3020 struct got_worktree *worktree = NULL;
3021 char *cwd = NULL;
3022 struct got_pathlist_head paths;
3023 struct got_pathlist_entry *pe;
3024 int ch;
3026 TAILQ_INIT(&paths);
3028 while ((ch = getopt(argc, argv, "")) != -1) {
3029 switch (ch) {
3030 default:
3031 usage_add();
3032 /* NOTREACHED */
3036 argc -= optind;
3037 argv += optind;
3039 #ifndef PROFILE
3040 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3041 NULL) == -1)
3042 err(1, "pledge");
3043 #endif
3044 if (argc < 1)
3045 usage_add();
3047 cwd = getcwd(NULL, 0);
3048 if (cwd == NULL) {
3049 error = got_error_from_errno("getcwd");
3050 goto done;
3053 error = got_worktree_open(&worktree, cwd);
3054 if (error)
3055 goto done;
3057 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3058 if (error != NULL)
3059 goto done;
3061 error = apply_unveil(got_repo_get_path(repo), 1,
3062 got_worktree_get_root_path(worktree));
3063 if (error)
3064 goto done;
3066 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3067 if (error)
3068 goto done;
3070 error = got_worktree_schedule_add(worktree, &paths, print_status,
3071 NULL, repo);
3072 done:
3073 if (repo)
3074 got_repo_close(repo);
3075 if (worktree)
3076 got_worktree_close(worktree);
3077 TAILQ_FOREACH(pe, &paths, entry)
3078 free((char *)pe->path);
3079 got_pathlist_free(&paths);
3080 free(cwd);
3081 return error;
3084 __dead static void
3085 usage_remove(void)
3087 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3088 exit(1);
3091 static const struct got_error *
3092 cmd_remove(int argc, char *argv[])
3094 const struct got_error *error = NULL;
3095 struct got_worktree *worktree = NULL;
3096 struct got_repository *repo = NULL;
3097 char *cwd = NULL;
3098 struct got_pathlist_head paths;
3099 struct got_pathlist_entry *pe;
3100 int ch, delete_local_mods = 0;
3102 TAILQ_INIT(&paths);
3104 while ((ch = getopt(argc, argv, "f")) != -1) {
3105 switch (ch) {
3106 case 'f':
3107 delete_local_mods = 1;
3108 break;
3109 default:
3110 usage_add();
3111 /* NOTREACHED */
3115 argc -= optind;
3116 argv += optind;
3118 #ifndef PROFILE
3119 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3120 NULL) == -1)
3121 err(1, "pledge");
3122 #endif
3123 if (argc < 1)
3124 usage_remove();
3126 cwd = getcwd(NULL, 0);
3127 if (cwd == NULL) {
3128 error = got_error_from_errno("getcwd");
3129 goto done;
3131 error = got_worktree_open(&worktree, cwd);
3132 if (error)
3133 goto done;
3135 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3136 if (error)
3137 goto done;
3139 error = apply_unveil(got_repo_get_path(repo), 1,
3140 got_worktree_get_root_path(worktree));
3141 if (error)
3142 goto done;
3144 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3145 if (error)
3146 goto done;
3148 error = got_worktree_schedule_delete(worktree, &paths,
3149 delete_local_mods, print_status, NULL, repo);
3150 if (error)
3151 goto done;
3152 done:
3153 if (repo)
3154 got_repo_close(repo);
3155 if (worktree)
3156 got_worktree_close(worktree);
3157 TAILQ_FOREACH(pe, &paths, entry)
3158 free((char *)pe->path);
3159 got_pathlist_free(&paths);
3160 free(cwd);
3161 return error;
3164 __dead static void
3165 usage_revert(void)
3167 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3168 "path ...\n", getprogname());
3169 exit(1);
3172 static const struct got_error *
3173 revert_progress(void *arg, unsigned char status, const char *path)
3175 while (path[0] == '/')
3176 path++;
3177 printf("%c %s\n", status, path);
3178 return NULL;
3181 struct choose_patch_arg {
3182 FILE *patch_script_file;
3183 const char *action;
3186 static const struct got_error *
3187 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3188 int nchanges, const char *action)
3190 char *line = NULL;
3191 size_t linesize = 0;
3192 ssize_t linelen;
3194 switch (status) {
3195 case GOT_STATUS_ADD:
3196 printf("A %s\n%s this addition? [y/n] ", path, action);
3197 break;
3198 case GOT_STATUS_DELETE:
3199 printf("D %s\n%s this deletion? [y/n] ", path, action);
3200 break;
3201 case GOT_STATUS_MODIFY:
3202 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3203 return got_error_from_errno("fseek");
3204 printf(GOT_COMMIT_SEP_STR);
3205 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3206 printf("%s", line);
3207 if (ferror(patch_file))
3208 return got_error_from_errno("getline");
3209 printf(GOT_COMMIT_SEP_STR);
3210 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3211 path, n, nchanges, action);
3212 break;
3213 default:
3214 return got_error_path(path, GOT_ERR_FILE_STATUS);
3217 return NULL;
3220 static const struct got_error *
3221 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3222 FILE *patch_file, int n, int nchanges)
3224 const struct got_error *err = NULL;
3225 char *line = NULL;
3226 size_t linesize = 0;
3227 ssize_t linelen;
3228 int resp = ' ';
3229 struct choose_patch_arg *a = arg;
3231 *choice = GOT_PATCH_CHOICE_NONE;
3233 if (a->patch_script_file) {
3234 char *nl;
3235 err = show_change(status, path, patch_file, n, nchanges,
3236 a->action);
3237 if (err)
3238 return err;
3239 linelen = getline(&line, &linesize, a->patch_script_file);
3240 if (linelen == -1) {
3241 if (ferror(a->patch_script_file))
3242 return got_error_from_errno("getline");
3243 return NULL;
3245 nl = strchr(line, '\n');
3246 if (nl)
3247 *nl = '\0';
3248 if (strcmp(line, "y") == 0) {
3249 *choice = GOT_PATCH_CHOICE_YES;
3250 printf("y\n");
3251 } else if (strcmp(line, "n") == 0) {
3252 *choice = GOT_PATCH_CHOICE_NO;
3253 printf("n\n");
3254 } else if (strcmp(line, "q") == 0 &&
3255 status == GOT_STATUS_MODIFY) {
3256 *choice = GOT_PATCH_CHOICE_QUIT;
3257 printf("q\n");
3258 } else
3259 printf("invalid response '%s'\n", line);
3260 free(line);
3261 return NULL;
3264 while (resp != 'y' && resp != 'n' && resp != 'q') {
3265 err = show_change(status, path, patch_file, n, nchanges,
3266 a->action);
3267 if (err)
3268 return err;
3269 resp = getchar();
3270 if (resp == '\n')
3271 resp = getchar();
3272 if (status == GOT_STATUS_MODIFY) {
3273 if (resp != 'y' && resp != 'n' && resp != 'q') {
3274 printf("invalid response '%c'\n", resp);
3275 resp = ' ';
3277 } else if (resp != 'y' && resp != 'n') {
3278 printf("invalid response '%c'\n", resp);
3279 resp = ' ';
3283 if (resp == 'y')
3284 *choice = GOT_PATCH_CHOICE_YES;
3285 else if (resp == 'n')
3286 *choice = GOT_PATCH_CHOICE_NO;
3287 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3288 *choice = GOT_PATCH_CHOICE_QUIT;
3290 return NULL;
3294 static const struct got_error *
3295 cmd_revert(int argc, char *argv[])
3297 const struct got_error *error = NULL;
3298 struct got_worktree *worktree = NULL;
3299 struct got_repository *repo = NULL;
3300 char *cwd = NULL, *path = NULL;
3301 struct got_pathlist_head paths;
3302 struct got_pathlist_entry *pe;
3303 int ch, can_recurse = 0, pflag = 0;
3304 FILE *patch_script_file = NULL;
3305 const char *patch_script_path = NULL;
3306 struct choose_patch_arg cpa;
3308 TAILQ_INIT(&paths);
3310 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3311 switch (ch) {
3312 case 'p':
3313 pflag = 1;
3314 break;
3315 case 'F':
3316 patch_script_path = optarg;
3317 break;
3318 case 'R':
3319 can_recurse = 1;
3320 break;
3321 default:
3322 usage_revert();
3323 /* NOTREACHED */
3327 argc -= optind;
3328 argv += optind;
3330 #ifndef PROFILE
3331 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3332 "unveil", NULL) == -1)
3333 err(1, "pledge");
3334 #endif
3335 if (argc < 1)
3336 usage_revert();
3337 if (patch_script_path && !pflag)
3338 errx(1, "-F option can only be used together with -p option");
3340 cwd = getcwd(NULL, 0);
3341 if (cwd == NULL) {
3342 error = got_error_from_errno("getcwd");
3343 goto done;
3345 error = got_worktree_open(&worktree, cwd);
3346 if (error)
3347 goto done;
3349 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3350 if (error != NULL)
3351 goto done;
3353 if (patch_script_path) {
3354 patch_script_file = fopen(patch_script_path, "r");
3355 if (patch_script_file == NULL) {
3356 error = got_error_from_errno2("fopen",
3357 patch_script_path);
3358 goto done;
3361 error = apply_unveil(got_repo_get_path(repo), 1,
3362 got_worktree_get_root_path(worktree));
3363 if (error)
3364 goto done;
3366 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3367 if (error)
3368 goto done;
3370 if (!can_recurse) {
3371 char *ondisk_path;
3372 struct stat sb;
3373 TAILQ_FOREACH(pe, &paths, entry) {
3374 if (asprintf(&ondisk_path, "%s/%s",
3375 got_worktree_get_root_path(worktree),
3376 pe->path) == -1) {
3377 error = got_error_from_errno("asprintf");
3378 goto done;
3380 if (lstat(ondisk_path, &sb) == -1) {
3381 if (errno == ENOENT) {
3382 free(ondisk_path);
3383 continue;
3385 error = got_error_from_errno2("lstat",
3386 ondisk_path);
3387 free(ondisk_path);
3388 goto done;
3390 free(ondisk_path);
3391 if (S_ISDIR(sb.st_mode)) {
3392 error = got_error_msg(GOT_ERR_BAD_PATH,
3393 "reverting directories requires -R option");
3394 goto done;
3399 cpa.patch_script_file = patch_script_file;
3400 cpa.action = "revert";
3401 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3402 pflag ? choose_patch : NULL, &cpa, repo);
3403 if (error)
3404 goto done;
3405 done:
3406 if (patch_script_file && fclose(patch_script_file) == EOF &&
3407 error == NULL)
3408 error = got_error_from_errno2("fclose", patch_script_path);
3409 if (repo)
3410 got_repo_close(repo);
3411 if (worktree)
3412 got_worktree_close(worktree);
3413 free(path);
3414 free(cwd);
3415 return error;
3418 __dead static void
3419 usage_commit(void)
3421 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3422 getprogname());
3423 exit(1);
3426 struct collect_commit_logmsg_arg {
3427 const char *cmdline_log;
3428 const char *editor;
3429 const char *worktree_path;
3430 const char *branch_name;
3431 const char *repo_path;
3432 char *logmsg_path;
3436 static const struct got_error *
3437 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3438 void *arg)
3440 char *initial_content = NULL;
3441 struct got_pathlist_entry *pe;
3442 const struct got_error *err = NULL;
3443 char *template = NULL;
3444 struct collect_commit_logmsg_arg *a = arg;
3445 int fd;
3446 size_t len;
3448 /* if a message was specified on the command line, just use it */
3449 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3450 len = strlen(a->cmdline_log) + 1;
3451 *logmsg = malloc(len + 1);
3452 if (*logmsg == NULL)
3453 return got_error_from_errno("malloc");
3454 strlcpy(*logmsg, a->cmdline_log, len);
3455 return NULL;
3458 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3459 return got_error_from_errno("asprintf");
3461 if (asprintf(&initial_content,
3462 "\n# changes to be committed on branch %s:\n",
3463 a->branch_name) == -1)
3464 return got_error_from_errno("asprintf");
3466 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3467 if (err)
3468 goto done;
3470 dprintf(fd, initial_content);
3472 TAILQ_FOREACH(pe, commitable_paths, entry) {
3473 struct got_commitable *ct = pe->data;
3474 dprintf(fd, "# %c %s\n",
3475 got_commitable_get_status(ct),
3476 got_commitable_get_path(ct));
3478 close(fd);
3480 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3481 done:
3482 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3483 unlink(a->logmsg_path);
3484 free(a->logmsg_path);
3485 a->logmsg_path = NULL;
3487 free(initial_content);
3488 free(template);
3490 /* Editor is done; we can now apply unveil(2) */
3491 if (err == NULL) {
3492 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3493 if (err) {
3494 free(*logmsg);
3495 *logmsg = NULL;
3498 return err;
3501 static const struct got_error *
3502 cmd_commit(int argc, char *argv[])
3504 const struct got_error *error = NULL;
3505 struct got_worktree *worktree = NULL;
3506 struct got_repository *repo = NULL;
3507 char *cwd = NULL, *id_str = NULL;
3508 struct got_object_id *id = NULL;
3509 const char *logmsg = NULL;
3510 const char *author;
3511 struct collect_commit_logmsg_arg cl_arg;
3512 char *editor = NULL;
3513 int ch, rebase_in_progress, histedit_in_progress;
3514 struct got_pathlist_head paths;
3516 TAILQ_INIT(&paths);
3517 cl_arg.logmsg_path = NULL;
3519 while ((ch = getopt(argc, argv, "m:")) != -1) {
3520 switch (ch) {
3521 case 'm':
3522 logmsg = optarg;
3523 break;
3524 default:
3525 usage_commit();
3526 /* NOTREACHED */
3530 argc -= optind;
3531 argv += optind;
3533 #ifndef PROFILE
3534 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3535 "unveil", NULL) == -1)
3536 err(1, "pledge");
3537 #endif
3538 error = get_author(&author);
3539 if (error)
3540 return error;
3542 cwd = getcwd(NULL, 0);
3543 if (cwd == NULL) {
3544 error = got_error_from_errno("getcwd");
3545 goto done;
3547 error = got_worktree_open(&worktree, cwd);
3548 if (error)
3549 goto done;
3551 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3552 if (error)
3553 goto done;
3554 if (rebase_in_progress) {
3555 error = got_error(GOT_ERR_REBASING);
3556 goto done;
3559 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3560 worktree);
3561 if (error)
3562 goto done;
3564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3565 if (error != NULL)
3566 goto done;
3569 * unveil(2) traverses exec(2); if an editor is used we have
3570 * to apply unveil after the log message has been written.
3572 if (logmsg == NULL || strlen(logmsg) == 0)
3573 error = get_editor(&editor);
3574 else
3575 error = apply_unveil(got_repo_get_path(repo), 0,
3576 got_worktree_get_root_path(worktree));
3577 if (error)
3578 goto done;
3580 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3581 if (error)
3582 goto done;
3584 cl_arg.editor = editor;
3585 cl_arg.cmdline_log = logmsg;
3586 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3587 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3588 if (!histedit_in_progress) {
3589 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3590 error = got_error(GOT_ERR_COMMIT_BRANCH);
3591 goto done;
3593 cl_arg.branch_name += 11;
3595 cl_arg.repo_path = got_repo_get_path(repo);
3596 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3597 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3598 if (error) {
3599 if (cl_arg.logmsg_path)
3600 fprintf(stderr, "%s: log message preserved in %s\n",
3601 getprogname(), cl_arg.logmsg_path);
3602 goto done;
3605 if (cl_arg.logmsg_path)
3606 unlink(cl_arg.logmsg_path);
3608 error = got_object_id_str(&id_str, id);
3609 if (error)
3610 goto done;
3611 printf("Created commit %s\n", id_str);
3612 done:
3613 free(cl_arg.logmsg_path);
3614 if (repo)
3615 got_repo_close(repo);
3616 if (worktree)
3617 got_worktree_close(worktree);
3618 free(cwd);
3619 free(id_str);
3620 free(editor);
3621 return error;
3624 __dead static void
3625 usage_cherrypick(void)
3627 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3628 exit(1);
3631 static const struct got_error *
3632 cmd_cherrypick(int argc, char *argv[])
3634 const struct got_error *error = NULL;
3635 struct got_worktree *worktree = NULL;
3636 struct got_repository *repo = NULL;
3637 char *cwd = NULL, *commit_id_str = NULL;
3638 struct got_object_id *commit_id = NULL;
3639 struct got_commit_object *commit = NULL;
3640 struct got_object_qid *pid;
3641 struct got_reference *head_ref = NULL;
3642 int ch, did_something = 0;
3644 while ((ch = getopt(argc, argv, "")) != -1) {
3645 switch (ch) {
3646 default:
3647 usage_cherrypick();
3648 /* NOTREACHED */
3652 argc -= optind;
3653 argv += optind;
3655 #ifndef PROFILE
3656 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3657 "unveil", NULL) == -1)
3658 err(1, "pledge");
3659 #endif
3660 if (argc != 1)
3661 usage_cherrypick();
3663 cwd = getcwd(NULL, 0);
3664 if (cwd == NULL) {
3665 error = got_error_from_errno("getcwd");
3666 goto done;
3668 error = got_worktree_open(&worktree, cwd);
3669 if (error)
3670 goto done;
3672 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3673 if (error != NULL)
3674 goto done;
3676 error = apply_unveil(got_repo_get_path(repo), 0,
3677 got_worktree_get_root_path(worktree));
3678 if (error)
3679 goto done;
3681 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3682 GOT_OBJ_TYPE_COMMIT, repo);
3683 if (error != NULL) {
3684 struct got_reference *ref;
3685 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3686 goto done;
3687 error = got_ref_open(&ref, repo, argv[0], 0);
3688 if (error != NULL)
3689 goto done;
3690 error = got_ref_resolve(&commit_id, repo, ref);
3691 got_ref_close(ref);
3692 if (error != NULL)
3693 goto done;
3695 error = got_object_id_str(&commit_id_str, commit_id);
3696 if (error)
3697 goto done;
3699 error = got_ref_open(&head_ref, repo,
3700 got_worktree_get_head_ref_name(worktree), 0);
3701 if (error != NULL)
3702 goto done;
3704 error = check_same_branch(commit_id, head_ref, NULL, repo);
3705 if (error) {
3706 if (error->code != GOT_ERR_ANCESTRY)
3707 goto done;
3708 error = NULL;
3709 } else {
3710 error = got_error(GOT_ERR_SAME_BRANCH);
3711 goto done;
3714 error = got_object_open_as_commit(&commit, repo, commit_id);
3715 if (error)
3716 goto done;
3717 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3718 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3719 commit_id, repo, update_progress, &did_something, check_cancelled,
3720 NULL);
3721 if (error != NULL)
3722 goto done;
3724 if (did_something)
3725 printf("Merged commit %s\n", commit_id_str);
3726 done:
3727 if (commit)
3728 got_object_commit_close(commit);
3729 free(commit_id_str);
3730 if (head_ref)
3731 got_ref_close(head_ref);
3732 if (worktree)
3733 got_worktree_close(worktree);
3734 if (repo)
3735 got_repo_close(repo);
3736 return error;
3739 __dead static void
3740 usage_backout(void)
3742 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3743 exit(1);
3746 static const struct got_error *
3747 cmd_backout(int argc, char *argv[])
3749 const struct got_error *error = NULL;
3750 struct got_worktree *worktree = NULL;
3751 struct got_repository *repo = NULL;
3752 char *cwd = NULL, *commit_id_str = NULL;
3753 struct got_object_id *commit_id = NULL;
3754 struct got_commit_object *commit = NULL;
3755 struct got_object_qid *pid;
3756 struct got_reference *head_ref = NULL;
3757 int ch, did_something = 0;
3759 while ((ch = getopt(argc, argv, "")) != -1) {
3760 switch (ch) {
3761 default:
3762 usage_backout();
3763 /* NOTREACHED */
3767 argc -= optind;
3768 argv += optind;
3770 #ifndef PROFILE
3771 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3772 "unveil", NULL) == -1)
3773 err(1, "pledge");
3774 #endif
3775 if (argc != 1)
3776 usage_backout();
3778 cwd = getcwd(NULL, 0);
3779 if (cwd == NULL) {
3780 error = got_error_from_errno("getcwd");
3781 goto done;
3783 error = got_worktree_open(&worktree, cwd);
3784 if (error)
3785 goto done;
3787 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3788 if (error != NULL)
3789 goto done;
3791 error = apply_unveil(got_repo_get_path(repo), 0,
3792 got_worktree_get_root_path(worktree));
3793 if (error)
3794 goto done;
3796 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3797 GOT_OBJ_TYPE_COMMIT, repo);
3798 if (error != NULL) {
3799 struct got_reference *ref;
3800 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3801 goto done;
3802 error = got_ref_open(&ref, repo, argv[0], 0);
3803 if (error != NULL)
3804 goto done;
3805 error = got_ref_resolve(&commit_id, repo, ref);
3806 got_ref_close(ref);
3807 if (error != NULL)
3808 goto done;
3810 error = got_object_id_str(&commit_id_str, commit_id);
3811 if (error)
3812 goto done;
3814 error = got_ref_open(&head_ref, repo,
3815 got_worktree_get_head_ref_name(worktree), 0);
3816 if (error != NULL)
3817 goto done;
3819 error = check_same_branch(commit_id, head_ref, NULL, repo);
3820 if (error)
3821 goto done;
3823 error = got_object_open_as_commit(&commit, repo, commit_id);
3824 if (error)
3825 goto done;
3826 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3827 if (pid == NULL) {
3828 error = got_error(GOT_ERR_ROOT_COMMIT);
3829 goto done;
3832 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3833 update_progress, &did_something, check_cancelled, NULL);
3834 if (error != NULL)
3835 goto done;
3837 if (did_something)
3838 printf("Backed out commit %s\n", commit_id_str);
3839 done:
3840 if (commit)
3841 got_object_commit_close(commit);
3842 free(commit_id_str);
3843 if (head_ref)
3844 got_ref_close(head_ref);
3845 if (worktree)
3846 got_worktree_close(worktree);
3847 if (repo)
3848 got_repo_close(repo);
3849 return error;
3852 __dead static void
3853 usage_rebase(void)
3855 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3856 getprogname());
3857 exit(1);
3860 void
3861 trim_logmsg(char *logmsg, int limit)
3863 char *nl;
3864 size_t len;
3866 len = strlen(logmsg);
3867 if (len > limit)
3868 len = limit;
3869 logmsg[len] = '\0';
3870 nl = strchr(logmsg, '\n');
3871 if (nl)
3872 *nl = '\0';
3875 static const struct got_error *
3876 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3878 const char *logmsg0 = NULL;
3880 logmsg0 = got_object_commit_get_logmsg(commit);
3882 while (isspace((unsigned char)logmsg0[0]))
3883 logmsg0++;
3885 *logmsg = strdup(logmsg0);
3886 if (*logmsg == NULL)
3887 return got_error_from_errno("strdup");
3889 trim_logmsg(*logmsg, limit);
3890 return NULL;
3893 static const struct got_error *
3894 show_rebase_progress(struct got_commit_object *commit,
3895 struct got_object_id *old_id, struct got_object_id *new_id)
3897 const struct got_error *err;
3898 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3900 err = got_object_id_str(&old_id_str, old_id);
3901 if (err)
3902 goto done;
3904 if (new_id) {
3905 err = got_object_id_str(&new_id_str, new_id);
3906 if (err)
3907 goto done;
3910 old_id_str[12] = '\0';
3911 if (new_id_str)
3912 new_id_str[12] = '\0';
3914 err = get_short_logmsg(&logmsg, 42, commit);
3915 if (err)
3916 goto done;
3918 printf("%s -> %s: %s\n", old_id_str,
3919 new_id_str ? new_id_str : "no-op change", logmsg);
3920 done:
3921 free(old_id_str);
3922 free(new_id_str);
3923 return err;
3926 static const struct got_error *
3927 rebase_progress(void *arg, unsigned char status, const char *path)
3929 unsigned char *rebase_status = arg;
3931 while (path[0] == '/')
3932 path++;
3933 printf("%c %s\n", status, path);
3935 if (*rebase_status == GOT_STATUS_CONFLICT)
3936 return NULL;
3937 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3938 *rebase_status = status;
3939 return NULL;
3942 static const struct got_error *
3943 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3944 struct got_reference *branch, struct got_reference *new_base_branch,
3945 struct got_reference *tmp_branch, struct got_repository *repo)
3947 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3948 return got_worktree_rebase_complete(worktree, fileindex,
3949 new_base_branch, tmp_branch, branch, repo);
3952 static const struct got_error *
3953 rebase_commit(struct got_pathlist_head *merged_paths,
3954 struct got_worktree *worktree, struct got_fileindex *fileindex,
3955 struct got_reference *tmp_branch,
3956 struct got_object_id *commit_id, struct got_repository *repo)
3958 const struct got_error *error;
3959 struct got_commit_object *commit;
3960 struct got_object_id *new_commit_id;
3962 error = got_object_open_as_commit(&commit, repo, commit_id);
3963 if (error)
3964 return error;
3966 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3967 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3968 if (error) {
3969 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3970 goto done;
3971 error = show_rebase_progress(commit, commit_id, NULL);
3972 } else {
3973 error = show_rebase_progress(commit, commit_id, new_commit_id);
3974 free(new_commit_id);
3976 done:
3977 got_object_commit_close(commit);
3978 return error;
3981 struct check_path_prefix_arg {
3982 const char *path_prefix;
3983 size_t len;
3984 int errcode;
3987 static const struct got_error *
3988 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3989 struct got_blob_object *blob2, struct got_object_id *id1,
3990 struct got_object_id *id2, const char *path1, const char *path2,
3991 struct got_repository *repo)
3993 struct check_path_prefix_arg *a = arg;
3995 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3996 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3997 return got_error(a->errcode);
3999 return NULL;
4002 static const struct got_error *
4003 check_path_prefix(struct got_object_id *parent_id,
4004 struct got_object_id *commit_id, const char *path_prefix,
4005 int errcode, struct got_repository *repo)
4007 const struct got_error *err;
4008 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4009 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4010 struct check_path_prefix_arg cpp_arg;
4012 if (got_path_is_root_dir(path_prefix))
4013 return NULL;
4015 err = got_object_open_as_commit(&commit, repo, commit_id);
4016 if (err)
4017 goto done;
4019 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4020 if (err)
4021 goto done;
4023 err = got_object_open_as_tree(&tree1, repo,
4024 got_object_commit_get_tree_id(parent_commit));
4025 if (err)
4026 goto done;
4028 err = got_object_open_as_tree(&tree2, repo,
4029 got_object_commit_get_tree_id(commit));
4030 if (err)
4031 goto done;
4033 cpp_arg.path_prefix = path_prefix;
4034 while (cpp_arg.path_prefix[0] == '/')
4035 cpp_arg.path_prefix++;
4036 cpp_arg.len = strlen(cpp_arg.path_prefix);
4037 cpp_arg.errcode = errcode;
4038 err = got_diff_tree(tree1, tree2, "", "", repo,
4039 check_path_prefix_in_diff, &cpp_arg, 0);
4040 done:
4041 if (tree1)
4042 got_object_tree_close(tree1);
4043 if (tree2)
4044 got_object_tree_close(tree2);
4045 if (commit)
4046 got_object_commit_close(commit);
4047 if (parent_commit)
4048 got_object_commit_close(parent_commit);
4049 return err;
4052 static const struct got_error *
4053 collect_commits(struct got_object_id_queue *commits,
4054 struct got_object_id *initial_commit_id,
4055 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4056 const char *path_prefix, int path_prefix_errcode,
4057 struct got_repository *repo)
4059 const struct got_error *err = NULL;
4060 struct got_commit_graph *graph = NULL;
4061 struct got_object_id *parent_id = NULL;
4062 struct got_object_qid *qid;
4063 struct got_object_id *commit_id = initial_commit_id;
4065 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4066 if (err)
4067 return err;
4069 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4070 if (err)
4071 goto done;
4072 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4073 err = got_commit_graph_iter_next(&parent_id, graph);
4074 if (err) {
4075 if (err->code == GOT_ERR_ITER_COMPLETED) {
4076 err = got_error_msg(GOT_ERR_ANCESTRY,
4077 "ran out of commits to rebase before "
4078 "youngest common ancestor commit has "
4079 "been reached?!?");
4080 goto done;
4081 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4082 goto done;
4083 err = got_commit_graph_fetch_commits(graph, 1, repo);
4084 if (err)
4085 goto done;
4086 } else {
4087 err = check_path_prefix(parent_id, commit_id,
4088 path_prefix, path_prefix_errcode, repo);
4089 if (err)
4090 goto done;
4092 err = got_object_qid_alloc(&qid, commit_id);
4093 if (err)
4094 goto done;
4095 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4096 commit_id = parent_id;
4099 done:
4100 got_commit_graph_close(graph);
4101 return err;
4104 static const struct got_error *
4105 cmd_rebase(int argc, char *argv[])
4107 const struct got_error *error = NULL;
4108 struct got_worktree *worktree = NULL;
4109 struct got_repository *repo = NULL;
4110 struct got_fileindex *fileindex = NULL;
4111 char *cwd = NULL;
4112 struct got_reference *branch = NULL;
4113 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4114 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4115 struct got_object_id *resume_commit_id = NULL;
4116 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4117 struct got_commit_object *commit = NULL;
4118 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4119 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4120 struct got_object_id_queue commits;
4121 struct got_pathlist_head merged_paths;
4122 const struct got_object_id_queue *parent_ids;
4123 struct got_object_qid *qid, *pid;
4125 SIMPLEQ_INIT(&commits);
4126 TAILQ_INIT(&merged_paths);
4128 while ((ch = getopt(argc, argv, "ac")) != -1) {
4129 switch (ch) {
4130 case 'a':
4131 abort_rebase = 1;
4132 break;
4133 case 'c':
4134 continue_rebase = 1;
4135 break;
4136 default:
4137 usage_rebase();
4138 /* NOTREACHED */
4142 argc -= optind;
4143 argv += optind;
4145 #ifndef PROFILE
4146 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4147 "unveil", NULL) == -1)
4148 err(1, "pledge");
4149 #endif
4150 if (abort_rebase && continue_rebase)
4151 usage_rebase();
4152 else if (abort_rebase || continue_rebase) {
4153 if (argc != 0)
4154 usage_rebase();
4155 } else if (argc != 1)
4156 usage_rebase();
4158 cwd = getcwd(NULL, 0);
4159 if (cwd == NULL) {
4160 error = got_error_from_errno("getcwd");
4161 goto done;
4163 error = got_worktree_open(&worktree, cwd);
4164 if (error)
4165 goto done;
4167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4168 if (error != NULL)
4169 goto done;
4171 error = apply_unveil(got_repo_get_path(repo), 0,
4172 got_worktree_get_root_path(worktree));
4173 if (error)
4174 goto done;
4176 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4177 if (error)
4178 goto done;
4180 if (abort_rebase) {
4181 int did_something;
4182 if (!rebase_in_progress) {
4183 error = got_error(GOT_ERR_NOT_REBASING);
4184 goto done;
4186 error = got_worktree_rebase_continue(&resume_commit_id,
4187 &new_base_branch, &tmp_branch, &branch, &fileindex,
4188 worktree, repo);
4189 if (error)
4190 goto done;
4191 printf("Switching work tree to %s\n",
4192 got_ref_get_symref_target(new_base_branch));
4193 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4194 new_base_branch, update_progress, &did_something);
4195 if (error)
4196 goto done;
4197 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4198 goto done; /* nothing else to do */
4201 if (continue_rebase) {
4202 if (!rebase_in_progress) {
4203 error = got_error(GOT_ERR_NOT_REBASING);
4204 goto done;
4206 error = got_worktree_rebase_continue(&resume_commit_id,
4207 &new_base_branch, &tmp_branch, &branch, &fileindex,
4208 worktree, repo);
4209 if (error)
4210 goto done;
4212 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4213 resume_commit_id, repo);
4214 if (error)
4215 goto done;
4217 yca_id = got_object_id_dup(resume_commit_id);
4218 if (yca_id == NULL) {
4219 error = got_error_from_errno("got_object_id_dup");
4220 goto done;
4222 } else {
4223 error = got_ref_open(&branch, repo, argv[0], 0);
4224 if (error != NULL)
4225 goto done;
4228 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4229 if (error)
4230 goto done;
4232 if (!continue_rebase) {
4233 struct got_object_id *base_commit_id;
4235 base_commit_id = got_worktree_get_base_commit_id(worktree);
4236 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4237 base_commit_id, branch_head_commit_id, repo);
4238 if (error)
4239 goto done;
4240 if (yca_id == NULL) {
4241 error = got_error_msg(GOT_ERR_ANCESTRY,
4242 "specified branch shares no common ancestry "
4243 "with work tree's branch");
4244 goto done;
4247 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4248 if (error) {
4249 if (error->code != GOT_ERR_ANCESTRY)
4250 goto done;
4251 error = NULL;
4252 } else {
4253 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4254 "specified branch resolves to a commit which "
4255 "is already contained in work tree's branch");
4256 goto done;
4258 error = got_worktree_rebase_prepare(&new_base_branch,
4259 &tmp_branch, &fileindex, worktree, branch, repo);
4260 if (error)
4261 goto done;
4264 commit_id = branch_head_commit_id;
4265 error = got_object_open_as_commit(&commit, repo, commit_id);
4266 if (error)
4267 goto done;
4269 parent_ids = got_object_commit_get_parent_ids(commit);
4270 pid = SIMPLEQ_FIRST(parent_ids);
4271 if (pid == NULL) {
4272 if (!continue_rebase) {
4273 int did_something;
4274 error = got_worktree_rebase_abort(worktree, fileindex,
4275 repo, new_base_branch, update_progress,
4276 &did_something);
4277 if (error)
4278 goto done;
4279 printf("Rebase of %s aborted\n",
4280 got_ref_get_name(branch));
4282 error = got_error(GOT_ERR_EMPTY_REBASE);
4283 goto done;
4285 error = collect_commits(&commits, commit_id, pid->id,
4286 yca_id, got_worktree_get_path_prefix(worktree),
4287 GOT_ERR_REBASE_PATH, repo);
4288 got_object_commit_close(commit);
4289 commit = NULL;
4290 if (error)
4291 goto done;
4293 if (SIMPLEQ_EMPTY(&commits)) {
4294 if (continue_rebase)
4295 error = rebase_complete(worktree, fileindex,
4296 branch, new_base_branch, tmp_branch, repo);
4297 else
4298 error = got_error(GOT_ERR_EMPTY_REBASE);
4299 goto done;
4302 pid = NULL;
4303 SIMPLEQ_FOREACH(qid, &commits, entry) {
4304 commit_id = qid->id;
4305 parent_id = pid ? pid->id : yca_id;
4306 pid = qid;
4308 error = got_worktree_rebase_merge_files(&merged_paths,
4309 worktree, fileindex, parent_id, commit_id, repo,
4310 rebase_progress, &rebase_status, check_cancelled, NULL);
4311 if (error)
4312 goto done;
4314 if (rebase_status == GOT_STATUS_CONFLICT) {
4315 got_worktree_rebase_pathlist_free(&merged_paths);
4316 break;
4319 error = rebase_commit(&merged_paths, worktree, fileindex,
4320 tmp_branch, commit_id, repo);
4321 got_worktree_rebase_pathlist_free(&merged_paths);
4322 if (error)
4323 goto done;
4326 if (rebase_status == GOT_STATUS_CONFLICT) {
4327 error = got_worktree_rebase_postpone(worktree, fileindex);
4328 if (error)
4329 goto done;
4330 error = got_error_msg(GOT_ERR_CONFLICTS,
4331 "conflicts must be resolved before rebasing can continue");
4332 } else
4333 error = rebase_complete(worktree, fileindex, branch,
4334 new_base_branch, tmp_branch, repo);
4335 done:
4336 got_object_id_queue_free(&commits);
4337 free(branch_head_commit_id);
4338 free(resume_commit_id);
4339 free(yca_id);
4340 if (commit)
4341 got_object_commit_close(commit);
4342 if (branch)
4343 got_ref_close(branch);
4344 if (new_base_branch)
4345 got_ref_close(new_base_branch);
4346 if (tmp_branch)
4347 got_ref_close(tmp_branch);
4348 if (worktree)
4349 got_worktree_close(worktree);
4350 if (repo)
4351 got_repo_close(repo);
4352 return error;
4355 __dead static void
4356 usage_histedit(void)
4358 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4359 getprogname());
4360 exit(1);
4363 #define GOT_HISTEDIT_PICK 'p'
4364 #define GOT_HISTEDIT_EDIT 'e'
4365 #define GOT_HISTEDIT_FOLD 'f'
4366 #define GOT_HISTEDIT_DROP 'd'
4367 #define GOT_HISTEDIT_MESG 'm'
4369 static struct got_histedit_cmd {
4370 unsigned char code;
4371 const char *name;
4372 const char *desc;
4373 } got_histedit_cmds[] = {
4374 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4375 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4376 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4377 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4378 { GOT_HISTEDIT_MESG, "mesg",
4379 "single-line log message for commit above (open editor if empty)" },
4382 struct got_histedit_list_entry {
4383 TAILQ_ENTRY(got_histedit_list_entry) entry;
4384 struct got_object_id *commit_id;
4385 const struct got_histedit_cmd *cmd;
4386 char *logmsg;
4388 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4390 static const struct got_error *
4391 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4392 FILE *f, struct got_repository *repo)
4394 const struct got_error *err = NULL;
4395 char *logmsg = NULL, *id_str = NULL;
4396 struct got_commit_object *commit = NULL;
4397 int n;
4399 err = got_object_open_as_commit(&commit, repo, commit_id);
4400 if (err)
4401 goto done;
4403 err = get_short_logmsg(&logmsg, 34, commit);
4404 if (err)
4405 goto done;
4407 err = got_object_id_str(&id_str, commit_id);
4408 if (err)
4409 goto done;
4411 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4412 if (n < 0)
4413 err = got_ferror(f, GOT_ERR_IO);
4414 done:
4415 if (commit)
4416 got_object_commit_close(commit);
4417 free(id_str);
4418 free(logmsg);
4419 return err;
4422 static const struct got_error *
4423 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4424 struct got_repository *repo)
4426 const struct got_error *err = NULL;
4427 struct got_object_qid *qid;
4429 if (SIMPLEQ_EMPTY(commits))
4430 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4432 SIMPLEQ_FOREACH(qid, commits, entry) {
4433 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4434 f, repo);
4435 if (err)
4436 break;
4439 return err;
4442 static const struct got_error *
4443 write_cmd_list(FILE *f)
4445 const struct got_error *err = NULL;
4446 int n, i;
4448 n = fprintf(f, "# Available histedit commands:\n");
4449 if (n < 0)
4450 return got_ferror(f, GOT_ERR_IO);
4452 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4453 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4454 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4455 cmd->desc);
4456 if (n < 0) {
4457 err = got_ferror(f, GOT_ERR_IO);
4458 break;
4461 n = fprintf(f, "# Commits will be processed in order from top to "
4462 "bottom of this file.\n");
4463 if (n < 0)
4464 return got_ferror(f, GOT_ERR_IO);
4465 return err;
4468 static const struct got_error *
4469 histedit_syntax_error(int lineno)
4471 static char msg[42];
4472 int ret;
4474 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4475 lineno);
4476 if (ret == -1 || ret >= sizeof(msg))
4477 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4479 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4482 static const struct got_error *
4483 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4484 char *logmsg, struct got_repository *repo)
4486 const struct got_error *err;
4487 struct got_commit_object *folded_commit = NULL;
4488 char *id_str;
4490 err = got_object_id_str(&id_str, hle->commit_id);
4491 if (err)
4492 return err;
4494 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4495 if (err)
4496 goto done;
4498 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4499 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4500 got_object_commit_get_logmsg(folded_commit)) == -1) {
4501 err = got_error_from_errno("asprintf");
4502 goto done;
4504 done:
4505 if (folded_commit)
4506 got_object_commit_close(folded_commit);
4507 free(id_str);
4508 return err;
4511 static struct got_histedit_list_entry *
4512 get_folded_commits(struct got_histedit_list_entry *hle)
4514 struct got_histedit_list_entry *prev, *folded = NULL;
4516 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4517 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4518 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4519 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4520 folded = prev;
4521 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4524 return folded;
4527 static const struct got_error *
4528 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4529 struct got_repository *repo)
4531 char *logmsg_path = NULL, *id_str = NULL;
4532 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4533 const struct got_error *err = NULL;
4534 struct got_commit_object *commit = NULL;
4535 int fd;
4536 struct got_histedit_list_entry *folded = NULL;
4538 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4539 if (err)
4540 return err;
4542 folded = get_folded_commits(hle);
4543 if (folded) {
4544 while (folded != hle) {
4545 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4546 folded = TAILQ_NEXT(folded, entry);
4547 continue;
4549 err = append_folded_commit_msg(&new_msg, folded,
4550 logmsg, repo);
4551 if (err)
4552 goto done;
4553 free(logmsg);
4554 logmsg = new_msg;
4555 folded = TAILQ_NEXT(folded, entry);
4559 err = got_object_id_str(&id_str, hle->commit_id);
4560 if (err)
4561 goto done;
4562 if (asprintf(&new_msg,
4563 "%s\n# original log message of commit %s: %s",
4564 logmsg ? logmsg : "", id_str,
4565 got_object_commit_get_logmsg(commit)) == -1) {
4566 err = got_error_from_errno("asprintf");
4567 goto done;
4569 free(logmsg);
4570 logmsg = new_msg;
4572 err = got_object_id_str(&id_str, hle->commit_id);
4573 if (err)
4574 goto done;
4576 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4577 if (err)
4578 goto done;
4580 dprintf(fd, logmsg);
4581 close(fd);
4583 err = get_editor(&editor);
4584 if (err)
4585 goto done;
4587 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4588 if (err) {
4589 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4590 goto done;
4591 err = NULL;
4592 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4593 if (hle->logmsg == NULL)
4594 err = got_error_from_errno("strdup");
4596 done:
4597 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4598 err = got_error_from_errno2("unlink", logmsg_path);
4599 free(logmsg_path);
4600 free(logmsg);
4601 free(editor);
4602 if (commit)
4603 got_object_commit_close(commit);
4604 return err;
4607 static const struct got_error *
4608 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4609 FILE *f, struct got_repository *repo)
4611 const struct got_error *err = NULL;
4612 char *line = NULL, *p, *end;
4613 size_t size;
4614 ssize_t len;
4615 int lineno = 0, i;
4616 const struct got_histedit_cmd *cmd;
4617 struct got_object_id *commit_id = NULL;
4618 struct got_histedit_list_entry *hle = NULL;
4620 for (;;) {
4621 len = getline(&line, &size, f);
4622 if (len == -1) {
4623 const struct got_error *getline_err;
4624 if (feof(f))
4625 break;
4626 getline_err = got_error_from_errno("getline");
4627 err = got_ferror(f, getline_err->code);
4628 break;
4630 lineno++;
4631 p = line;
4632 while (isspace((unsigned char)p[0]))
4633 p++;
4634 if (p[0] == '#' || p[0] == '\0') {
4635 free(line);
4636 line = NULL;
4637 continue;
4639 cmd = NULL;
4640 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4641 cmd = &got_histedit_cmds[i];
4642 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4643 isspace((unsigned char)p[strlen(cmd->name)])) {
4644 p += strlen(cmd->name);
4645 break;
4647 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4648 p++;
4649 break;
4652 if (i == nitems(got_histedit_cmds)) {
4653 err = histedit_syntax_error(lineno);
4654 break;
4656 while (isspace((unsigned char)p[0]))
4657 p++;
4658 if (cmd->code == GOT_HISTEDIT_MESG) {
4659 if (hle == NULL || hle->logmsg != NULL) {
4660 err = got_error(GOT_ERR_HISTEDIT_CMD);
4661 break;
4663 if (p[0] == '\0') {
4664 err = histedit_edit_logmsg(hle, repo);
4665 if (err)
4666 break;
4667 } else {
4668 hle->logmsg = strdup(p);
4669 if (hle->logmsg == NULL) {
4670 err = got_error_from_errno("strdup");
4671 break;
4674 free(line);
4675 line = NULL;
4676 continue;
4677 } else {
4678 end = p;
4679 while (end[0] && !isspace((unsigned char)end[0]))
4680 end++;
4681 *end = '\0';
4683 err = got_object_resolve_id_str(&commit_id, repo, p);
4684 if (err) {
4685 /* override error code */
4686 err = histedit_syntax_error(lineno);
4687 break;
4690 hle = malloc(sizeof(*hle));
4691 if (hle == NULL) {
4692 err = got_error_from_errno("malloc");
4693 break;
4695 hle->cmd = cmd;
4696 hle->commit_id = commit_id;
4697 hle->logmsg = NULL;
4698 commit_id = NULL;
4699 free(line);
4700 line = NULL;
4701 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4704 free(line);
4705 free(commit_id);
4706 return err;
4709 static const struct got_error *
4710 histedit_check_script(struct got_histedit_list *histedit_cmds,
4711 struct got_object_id_queue *commits, struct got_repository *repo)
4713 const struct got_error *err = NULL;
4714 struct got_object_qid *qid;
4715 struct got_histedit_list_entry *hle;
4716 static char msg[80];
4717 char *id_str;
4719 if (TAILQ_EMPTY(histedit_cmds))
4720 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4721 "histedit script contains no commands");
4722 if (SIMPLEQ_EMPTY(commits))
4723 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4725 SIMPLEQ_FOREACH(qid, commits, entry) {
4726 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4727 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4728 break;
4730 if (hle == NULL) {
4731 err = got_object_id_str(&id_str, qid->id);
4732 if (err)
4733 return err;
4734 snprintf(msg, sizeof(msg),
4735 "commit %s missing from histedit script", id_str);
4736 free(id_str);
4737 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4741 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4742 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4743 "last commit in histedit script cannot be folded");
4745 return NULL;
4748 static const struct got_error *
4749 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4750 const char *path, struct got_object_id_queue *commits,
4751 struct got_repository *repo)
4753 const struct got_error *err = NULL;
4754 char *editor;
4755 FILE *f = NULL;
4757 err = get_editor(&editor);
4758 if (err)
4759 return err;
4761 if (spawn_editor(editor, path) == -1) {
4762 err = got_error_from_errno("failed spawning editor");
4763 goto done;
4766 f = fopen(path, "r");
4767 if (f == NULL) {
4768 err = got_error_from_errno("fopen");
4769 goto done;
4771 err = histedit_parse_list(histedit_cmds, f, repo);
4772 if (err)
4773 goto done;
4775 err = histedit_check_script(histedit_cmds, commits, repo);
4776 done:
4777 if (f && fclose(f) != 0 && err == NULL)
4778 err = got_error_from_errno("fclose");
4779 free(editor);
4780 return err;
4783 static const struct got_error *
4784 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4785 struct got_object_id_queue *, const char *, struct got_repository *);
4787 static const struct got_error *
4788 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4789 struct got_object_id_queue *commits, struct got_repository *repo)
4791 const struct got_error *err;
4792 FILE *f = NULL;
4793 char *path = NULL;
4795 err = got_opentemp_named(&path, &f, "got-histedit");
4796 if (err)
4797 return err;
4799 err = write_cmd_list(f);
4800 if (err)
4801 goto done;
4803 err = histedit_write_commit_list(commits, f, repo);
4804 if (err)
4805 goto done;
4807 if (fclose(f) != 0) {
4808 err = got_error_from_errno("fclose");
4809 goto done;
4811 f = NULL;
4813 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4814 if (err) {
4815 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4816 err->code != GOT_ERR_HISTEDIT_CMD)
4817 goto done;
4818 err = histedit_edit_list_retry(histedit_cmds, err,
4819 commits, path, repo);
4821 done:
4822 if (f && fclose(f) != 0 && err == NULL)
4823 err = got_error_from_errno("fclose");
4824 if (path && unlink(path) != 0 && err == NULL)
4825 err = got_error_from_errno2("unlink", path);
4826 free(path);
4827 return err;
4830 static const struct got_error *
4831 histedit_save_list(struct got_histedit_list *histedit_cmds,
4832 struct got_worktree *worktree, struct got_repository *repo)
4834 const struct got_error *err = NULL;
4835 char *path = NULL;
4836 FILE *f = NULL;
4837 struct got_histedit_list_entry *hle;
4838 struct got_commit_object *commit = NULL;
4840 err = got_worktree_get_histedit_script_path(&path, worktree);
4841 if (err)
4842 return err;
4844 f = fopen(path, "w");
4845 if (f == NULL) {
4846 err = got_error_from_errno2("fopen", path);
4847 goto done;
4849 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4850 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4851 repo);
4852 if (err)
4853 break;
4855 if (hle->logmsg) {
4856 int n = fprintf(f, "%c %s\n",
4857 GOT_HISTEDIT_MESG, hle->logmsg);
4858 if (n < 0) {
4859 err = got_ferror(f, GOT_ERR_IO);
4860 break;
4864 done:
4865 if (f && fclose(f) != 0 && err == NULL)
4866 err = got_error_from_errno("fclose");
4867 free(path);
4868 if (commit)
4869 got_object_commit_close(commit);
4870 return err;
4873 void
4874 histedit_free_list(struct got_histedit_list *histedit_cmds)
4876 struct got_histedit_list_entry *hle;
4878 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4879 TAILQ_REMOVE(histedit_cmds, hle, entry);
4880 free(hle);
4884 static const struct got_error *
4885 histedit_load_list(struct got_histedit_list *histedit_cmds,
4886 const char *path, struct got_repository *repo)
4888 const struct got_error *err = NULL;
4889 FILE *f = NULL;
4891 f = fopen(path, "r");
4892 if (f == NULL) {
4893 err = got_error_from_errno2("fopen", path);
4894 goto done;
4897 err = histedit_parse_list(histedit_cmds, f, repo);
4898 done:
4899 if (f && fclose(f) != 0 && err == NULL)
4900 err = got_error_from_errno("fclose");
4901 return err;
4904 static const struct got_error *
4905 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4906 const struct got_error *edit_err, struct got_object_id_queue *commits,
4907 const char *path, struct got_repository *repo)
4909 const struct got_error *err = NULL, *prev_err = edit_err;
4910 int resp = ' ';
4912 while (resp != 'c' && resp != 'r' && resp != 'a') {
4913 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4914 "or (a)bort: ", getprogname(), prev_err->msg);
4915 resp = getchar();
4916 if (resp == '\n')
4917 resp = getchar();
4918 if (resp == 'c') {
4919 histedit_free_list(histedit_cmds);
4920 err = histedit_run_editor(histedit_cmds, path, commits,
4921 repo);
4922 if (err) {
4923 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4924 err->code != GOT_ERR_HISTEDIT_CMD)
4925 break;
4926 prev_err = err;
4927 resp = ' ';
4928 continue;
4930 break;
4931 } else if (resp == 'r') {
4932 histedit_free_list(histedit_cmds);
4933 err = histedit_edit_script(histedit_cmds,
4934 commits, repo);
4935 if (err) {
4936 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4937 err->code != GOT_ERR_HISTEDIT_CMD)
4938 break;
4939 prev_err = err;
4940 resp = ' ';
4941 continue;
4943 break;
4944 } else if (resp == 'a') {
4945 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4946 break;
4947 } else
4948 printf("invalid response '%c'\n", resp);
4951 return err;
4954 static const struct got_error *
4955 histedit_complete(struct got_worktree *worktree,
4956 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4957 struct got_reference *branch, struct got_repository *repo)
4959 printf("Switching work tree to %s\n",
4960 got_ref_get_symref_target(branch));
4961 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4962 branch, repo);
4965 static const struct got_error *
4966 show_histedit_progress(struct got_commit_object *commit,
4967 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4969 const struct got_error *err;
4970 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4972 err = got_object_id_str(&old_id_str, hle->commit_id);
4973 if (err)
4974 goto done;
4976 if (new_id) {
4977 err = got_object_id_str(&new_id_str, new_id);
4978 if (err)
4979 goto done;
4982 old_id_str[12] = '\0';
4983 if (new_id_str)
4984 new_id_str[12] = '\0';
4986 if (hle->logmsg) {
4987 logmsg = strdup(hle->logmsg);
4988 if (logmsg == NULL) {
4989 err = got_error_from_errno("strdup");
4990 goto done;
4992 trim_logmsg(logmsg, 42);
4993 } else {
4994 err = get_short_logmsg(&logmsg, 42, commit);
4995 if (err)
4996 goto done;
4999 switch (hle->cmd->code) {
5000 case GOT_HISTEDIT_PICK:
5001 case GOT_HISTEDIT_EDIT:
5002 printf("%s -> %s: %s\n", old_id_str,
5003 new_id_str ? new_id_str : "no-op change", logmsg);
5004 break;
5005 case GOT_HISTEDIT_DROP:
5006 case GOT_HISTEDIT_FOLD:
5007 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5008 logmsg);
5009 break;
5010 default:
5011 break;
5014 done:
5015 free(old_id_str);
5016 free(new_id_str);
5017 return err;
5020 static const struct got_error *
5021 histedit_commit(struct got_pathlist_head *merged_paths,
5022 struct got_worktree *worktree, struct got_fileindex *fileindex,
5023 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5024 struct got_repository *repo)
5026 const struct got_error *err;
5027 struct got_commit_object *commit;
5028 struct got_object_id *new_commit_id;
5030 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5031 && hle->logmsg == NULL) {
5032 err = histedit_edit_logmsg(hle, repo);
5033 if (err)
5034 return err;
5037 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5038 if (err)
5039 return err;
5041 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5042 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5043 hle->logmsg, repo);
5044 if (err) {
5045 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5046 goto done;
5047 err = show_histedit_progress(commit, hle, NULL);
5048 } else {
5049 err = show_histedit_progress(commit, hle, new_commit_id);
5050 free(new_commit_id);
5052 done:
5053 got_object_commit_close(commit);
5054 return err;
5057 static const struct got_error *
5058 histedit_skip_commit(struct got_histedit_list_entry *hle,
5059 struct got_worktree *worktree, struct got_repository *repo)
5061 const struct got_error *error;
5062 struct got_commit_object *commit;
5064 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5065 repo);
5066 if (error)
5067 return error;
5069 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5070 if (error)
5071 return error;
5073 error = show_histedit_progress(commit, hle, NULL);
5074 got_object_commit_close(commit);
5075 return error;
5078 static const struct got_error *
5079 cmd_histedit(int argc, char *argv[])
5081 const struct got_error *error = NULL;
5082 struct got_worktree *worktree = NULL;
5083 struct got_fileindex *fileindex = NULL;
5084 struct got_repository *repo = NULL;
5085 char *cwd = NULL;
5086 struct got_reference *branch = NULL;
5087 struct got_reference *tmp_branch = NULL;
5088 struct got_object_id *resume_commit_id = NULL;
5089 struct got_object_id *base_commit_id = NULL;
5090 struct got_object_id *head_commit_id = NULL;
5091 struct got_commit_object *commit = NULL;
5092 int ch, rebase_in_progress = 0, did_something;
5093 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5094 const char *edit_script_path = NULL;
5095 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5096 struct got_object_id_queue commits;
5097 struct got_pathlist_head merged_paths;
5098 const struct got_object_id_queue *parent_ids;
5099 struct got_object_qid *pid;
5100 struct got_histedit_list histedit_cmds;
5101 struct got_histedit_list_entry *hle;
5103 SIMPLEQ_INIT(&commits);
5104 TAILQ_INIT(&histedit_cmds);
5105 TAILQ_INIT(&merged_paths);
5107 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5108 switch (ch) {
5109 case 'a':
5110 abort_edit = 1;
5111 break;
5112 case 'c':
5113 continue_edit = 1;
5114 break;
5115 case 'F':
5116 edit_script_path = optarg;
5117 break;
5118 default:
5119 usage_histedit();
5120 /* NOTREACHED */
5124 argc -= optind;
5125 argv += optind;
5127 #ifndef PROFILE
5128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5129 "unveil", NULL) == -1)
5130 err(1, "pledge");
5131 #endif
5132 if (abort_edit && continue_edit)
5133 usage_histedit();
5134 if (argc != 0)
5135 usage_histedit();
5138 * This command cannot apply unveil(2) in all cases because the
5139 * user may choose to run an editor to edit the histedit script
5140 * and to edit individual commit log messages.
5141 * unveil(2) traverses exec(2); if an editor is used we have to
5142 * apply unveil after edit script and log messages have been written.
5143 * XXX TODO: Make use of unveil(2) where possible.
5146 cwd = getcwd(NULL, 0);
5147 if (cwd == NULL) {
5148 error = got_error_from_errno("getcwd");
5149 goto done;
5151 error = got_worktree_open(&worktree, cwd);
5152 if (error)
5153 goto done;
5155 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5156 if (error != NULL)
5157 goto done;
5159 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5160 if (error)
5161 goto done;
5162 if (rebase_in_progress) {
5163 error = got_error(GOT_ERR_REBASING);
5164 goto done;
5167 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5168 if (error)
5169 goto done;
5171 if (edit_in_progress && abort_edit) {
5172 error = got_worktree_histedit_continue(&resume_commit_id,
5173 &tmp_branch, &branch, &base_commit_id, &fileindex,
5174 worktree, repo);
5175 if (error)
5176 goto done;
5177 printf("Switching work tree to %s\n",
5178 got_ref_get_symref_target(branch));
5179 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5180 branch, base_commit_id, update_progress, &did_something);
5181 if (error)
5182 goto done;
5183 printf("Histedit of %s aborted\n",
5184 got_ref_get_symref_target(branch));
5185 goto done; /* nothing else to do */
5186 } else if (abort_edit) {
5187 error = got_error(GOT_ERR_NOT_HISTEDIT);
5188 goto done;
5191 if (continue_edit) {
5192 char *path;
5194 if (!edit_in_progress) {
5195 error = got_error(GOT_ERR_NOT_HISTEDIT);
5196 goto done;
5199 error = got_worktree_get_histedit_script_path(&path, worktree);
5200 if (error)
5201 goto done;
5203 error = histedit_load_list(&histedit_cmds, path, repo);
5204 free(path);
5205 if (error)
5206 goto done;
5208 error = got_worktree_histedit_continue(&resume_commit_id,
5209 &tmp_branch, &branch, &base_commit_id, &fileindex,
5210 worktree, repo);
5211 if (error)
5212 goto done;
5214 error = got_ref_resolve(&head_commit_id, repo, branch);
5215 if (error)
5216 goto done;
5218 error = got_object_open_as_commit(&commit, repo,
5219 head_commit_id);
5220 if (error)
5221 goto done;
5222 parent_ids = got_object_commit_get_parent_ids(commit);
5223 pid = SIMPLEQ_FIRST(parent_ids);
5224 if (pid == NULL) {
5225 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5226 goto done;
5228 error = collect_commits(&commits, head_commit_id, pid->id,
5229 base_commit_id, got_worktree_get_path_prefix(worktree),
5230 GOT_ERR_HISTEDIT_PATH, repo);
5231 got_object_commit_close(commit);
5232 commit = NULL;
5233 if (error)
5234 goto done;
5235 } else {
5236 if (edit_in_progress) {
5237 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5238 goto done;
5241 error = got_ref_open(&branch, repo,
5242 got_worktree_get_head_ref_name(worktree), 0);
5243 if (error != NULL)
5244 goto done;
5246 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5247 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5248 "will not edit commit history of a branch outside "
5249 "the \"refs/heads/\" reference namespace");
5250 goto done;
5253 error = got_ref_resolve(&head_commit_id, repo, branch);
5254 got_ref_close(branch);
5255 branch = NULL;
5256 if (error)
5257 goto done;
5259 error = got_object_open_as_commit(&commit, repo,
5260 head_commit_id);
5261 if (error)
5262 goto done;
5263 parent_ids = got_object_commit_get_parent_ids(commit);
5264 pid = SIMPLEQ_FIRST(parent_ids);
5265 if (pid == NULL) {
5266 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5267 goto done;
5269 error = collect_commits(&commits, head_commit_id, pid->id,
5270 got_worktree_get_base_commit_id(worktree),
5271 got_worktree_get_path_prefix(worktree),
5272 GOT_ERR_HISTEDIT_PATH, repo);
5273 got_object_commit_close(commit);
5274 commit = NULL;
5275 if (error)
5276 goto done;
5278 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5279 &base_commit_id, &fileindex, worktree, repo);
5280 if (error)
5281 goto done;
5283 if (edit_script_path) {
5284 error = histedit_load_list(&histedit_cmds,
5285 edit_script_path, repo);
5286 if (error) {
5287 got_worktree_histedit_abort(worktree, fileindex,
5288 repo, branch, base_commit_id,
5289 update_progress, &did_something);
5290 goto done;
5292 } else {
5293 error = histedit_edit_script(&histedit_cmds, &commits,
5294 repo);
5295 if (error) {
5296 got_worktree_histedit_abort(worktree, fileindex,
5297 repo, branch, base_commit_id,
5298 update_progress, &did_something);
5299 goto done;
5304 error = histedit_save_list(&histedit_cmds, worktree,
5305 repo);
5306 if (error) {
5307 got_worktree_histedit_abort(worktree, fileindex,
5308 repo, branch, base_commit_id,
5309 update_progress, &did_something);
5310 goto done;
5315 error = histedit_check_script(&histedit_cmds, &commits, repo);
5316 if (error)
5317 goto done;
5319 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5320 if (resume_commit_id) {
5321 if (got_object_id_cmp(hle->commit_id,
5322 resume_commit_id) != 0)
5323 continue;
5325 resume_commit_id = NULL;
5326 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5327 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5328 error = histedit_skip_commit(hle, worktree,
5329 repo);
5330 } else {
5331 error = histedit_commit(NULL, worktree,
5332 fileindex, tmp_branch, hle, repo);
5334 if (error)
5335 goto done;
5336 continue;
5339 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5340 error = histedit_skip_commit(hle, worktree, repo);
5341 if (error)
5342 goto done;
5343 continue;
5346 error = got_object_open_as_commit(&commit, repo,
5347 hle->commit_id);
5348 if (error)
5349 goto done;
5350 parent_ids = got_object_commit_get_parent_ids(commit);
5351 pid = SIMPLEQ_FIRST(parent_ids);
5353 error = got_worktree_histedit_merge_files(&merged_paths,
5354 worktree, fileindex, pid->id, hle->commit_id, repo,
5355 rebase_progress, &rebase_status, check_cancelled, NULL);
5356 if (error)
5357 goto done;
5358 got_object_commit_close(commit);
5359 commit = NULL;
5361 if (rebase_status == GOT_STATUS_CONFLICT) {
5362 got_worktree_rebase_pathlist_free(&merged_paths);
5363 break;
5366 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5367 char *id_str;
5368 error = got_object_id_str(&id_str, hle->commit_id);
5369 if (error)
5370 goto done;
5371 printf("Stopping histedit for amending commit %s\n",
5372 id_str);
5373 free(id_str);
5374 got_worktree_rebase_pathlist_free(&merged_paths);
5375 error = got_worktree_histedit_postpone(worktree,
5376 fileindex);
5377 goto done;
5380 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5381 error = histedit_skip_commit(hle, worktree, repo);
5382 if (error)
5383 goto done;
5384 continue;
5387 error = histedit_commit(&merged_paths, worktree, fileindex,
5388 tmp_branch, hle, repo);
5389 got_worktree_rebase_pathlist_free(&merged_paths);
5390 if (error)
5391 goto done;
5394 if (rebase_status == GOT_STATUS_CONFLICT) {
5395 error = got_worktree_histedit_postpone(worktree, fileindex);
5396 if (error)
5397 goto done;
5398 error = got_error_msg(GOT_ERR_CONFLICTS,
5399 "conflicts must be resolved before rebasing can continue");
5400 } else
5401 error = histedit_complete(worktree, fileindex, tmp_branch,
5402 branch, repo);
5403 done:
5404 got_object_id_queue_free(&commits);
5405 histedit_free_list(&histedit_cmds);
5406 free(head_commit_id);
5407 free(base_commit_id);
5408 free(resume_commit_id);
5409 if (commit)
5410 got_object_commit_close(commit);
5411 if (branch)
5412 got_ref_close(branch);
5413 if (tmp_branch)
5414 got_ref_close(tmp_branch);
5415 if (worktree)
5416 got_worktree_close(worktree);
5417 if (repo)
5418 got_repo_close(repo);
5419 return error;
5422 __dead static void
5423 usage_stage(void)
5425 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5426 "[file-path ...]\n",
5427 getprogname());
5428 exit(1);
5431 static const struct got_error *
5432 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5433 const char *path, struct got_object_id *blob_id,
5434 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5436 const struct got_error *err = NULL;
5437 char *id_str = NULL;
5439 if (staged_status != GOT_STATUS_ADD &&
5440 staged_status != GOT_STATUS_MODIFY &&
5441 staged_status != GOT_STATUS_DELETE)
5442 return NULL;
5444 if (staged_status == GOT_STATUS_ADD ||
5445 staged_status == GOT_STATUS_MODIFY)
5446 err = got_object_id_str(&id_str, staged_blob_id);
5447 else
5448 err = got_object_id_str(&id_str, blob_id);
5449 if (err)
5450 return err;
5452 printf("%s %c %s\n", id_str, staged_status, path);
5453 free(id_str);
5454 return NULL;
5457 static const struct got_error *
5458 cmd_stage(int argc, char *argv[])
5460 const struct got_error *error = NULL;
5461 struct got_repository *repo = NULL;
5462 struct got_worktree *worktree = NULL;
5463 char *cwd = NULL;
5464 struct got_pathlist_head paths;
5465 struct got_pathlist_entry *pe;
5466 int ch, list_stage = 0, pflag = 0;
5467 FILE *patch_script_file = NULL;
5468 const char *patch_script_path = NULL;
5469 struct choose_patch_arg cpa;
5471 TAILQ_INIT(&paths);
5473 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5474 switch (ch) {
5475 case 'l':
5476 list_stage = 1;
5477 break;
5478 case 'p':
5479 pflag = 1;
5480 break;
5481 case 'F':
5482 patch_script_path = optarg;
5483 break;
5484 default:
5485 usage_stage();
5486 /* NOTREACHED */
5490 argc -= optind;
5491 argv += optind;
5493 #ifndef PROFILE
5494 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5495 "unveil", NULL) == -1)
5496 err(1, "pledge");
5497 #endif
5498 if (list_stage && (pflag || patch_script_path))
5499 errx(1, "-l option cannot be used with other options");
5500 if (patch_script_path && !pflag)
5501 errx(1, "-F option can only be used together with -p option");
5503 cwd = getcwd(NULL, 0);
5504 if (cwd == NULL) {
5505 error = got_error_from_errno("getcwd");
5506 goto done;
5509 error = got_worktree_open(&worktree, cwd);
5510 if (error)
5511 goto done;
5513 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5514 if (error != NULL)
5515 goto done;
5517 if (patch_script_path) {
5518 patch_script_file = fopen(patch_script_path, "r");
5519 if (patch_script_file == NULL) {
5520 error = got_error_from_errno2("fopen",
5521 patch_script_path);
5522 goto done;
5525 error = apply_unveil(got_repo_get_path(repo), 1,
5526 got_worktree_get_root_path(worktree));
5527 if (error)
5528 goto done;
5530 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5531 if (error)
5532 goto done;
5534 if (list_stage)
5535 error = got_worktree_status(worktree, &paths, repo,
5536 print_stage, NULL, check_cancelled, NULL);
5537 else {
5538 cpa.patch_script_file = patch_script_file;
5539 cpa.action = "stage";
5540 error = got_worktree_stage(worktree, &paths,
5541 pflag ? NULL : print_status, NULL,
5542 pflag ? choose_patch : NULL, &cpa, repo);
5544 done:
5545 if (patch_script_file && fclose(patch_script_file) == EOF &&
5546 error == NULL)
5547 error = got_error_from_errno2("fclose", patch_script_path);
5548 if (repo)
5549 got_repo_close(repo);
5550 if (worktree)
5551 got_worktree_close(worktree);
5552 TAILQ_FOREACH(pe, &paths, entry)
5553 free((char *)pe->path);
5554 got_pathlist_free(&paths);
5555 free(cwd);
5556 return error;
5559 __dead static void
5560 usage_unstage(void)
5562 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5563 "[file-path ...]\n",
5564 getprogname());
5565 exit(1);
5569 static const struct got_error *
5570 cmd_unstage(int argc, char *argv[])
5572 const struct got_error *error = NULL;
5573 struct got_repository *repo = NULL;
5574 struct got_worktree *worktree = NULL;
5575 char *cwd = NULL;
5576 struct got_pathlist_head paths;
5577 struct got_pathlist_entry *pe;
5578 int ch, did_something = 0, pflag = 0;
5579 FILE *patch_script_file = NULL;
5580 const char *patch_script_path = NULL;
5581 struct choose_patch_arg cpa;
5583 TAILQ_INIT(&paths);
5585 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5586 switch (ch) {
5587 case 'p':
5588 pflag = 1;
5589 break;
5590 case 'F':
5591 patch_script_path = optarg;
5592 break;
5593 default:
5594 usage_unstage();
5595 /* NOTREACHED */
5599 argc -= optind;
5600 argv += optind;
5602 #ifndef PROFILE
5603 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5604 "unveil", NULL) == -1)
5605 err(1, "pledge");
5606 #endif
5607 if (patch_script_path && !pflag)
5608 errx(1, "-F option can only be used together with -p option");
5610 cwd = getcwd(NULL, 0);
5611 if (cwd == NULL) {
5612 error = got_error_from_errno("getcwd");
5613 goto done;
5616 error = got_worktree_open(&worktree, cwd);
5617 if (error)
5618 goto done;
5620 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5621 if (error != NULL)
5622 goto done;
5624 if (patch_script_path) {
5625 patch_script_file = fopen(patch_script_path, "r");
5626 if (patch_script_file == NULL) {
5627 error = got_error_from_errno2("fopen",
5628 patch_script_path);
5629 goto done;
5633 error = apply_unveil(got_repo_get_path(repo), 1,
5634 got_worktree_get_root_path(worktree));
5635 if (error)
5636 goto done;
5638 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5639 if (error)
5640 goto done;
5642 cpa.patch_script_file = patch_script_file;
5643 cpa.action = "unstage";
5644 error = got_worktree_unstage(worktree, &paths, update_progress,
5645 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5646 done:
5647 if (patch_script_file && fclose(patch_script_file) == EOF &&
5648 error == NULL)
5649 error = got_error_from_errno2("fclose", patch_script_path);
5650 if (repo)
5651 got_repo_close(repo);
5652 if (worktree)
5653 got_worktree_close(worktree);
5654 TAILQ_FOREACH(pe, &paths, entry)
5655 free((char *)pe->path);
5656 got_pathlist_free(&paths);
5657 free(cwd);
5658 return error;