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 struct tm mytm, *tm;
1365 char *p, *s;
1367 tm = gmtime_r(time, &mytm);
1368 if (tm == NULL)
1369 return NULL;
1370 s = asctime_r(tm, datebuf);
1371 if (s == NULL)
1372 return NULL;
1373 p = strchr(s, '\n');
1374 if (p)
1375 *p = '\0';
1376 return s;
1379 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1381 static const struct got_error *
1382 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1383 struct got_repository *repo, int show_patch, int diff_context,
1384 struct got_reflist_head *refs)
1386 const struct got_error *err = NULL;
1387 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1388 char datebuf[26];
1389 time_t committer_time;
1390 const char *author, *committer;
1391 char *refs_str = NULL;
1392 struct got_reflist_entry *re;
1394 SIMPLEQ_FOREACH(re, refs, entry) {
1395 char *s;
1396 const char *name;
1397 struct got_tag_object *tag = NULL;
1398 int cmp;
1400 name = got_ref_get_name(re->ref);
1401 if (strcmp(name, GOT_REF_HEAD) == 0)
1402 continue;
1403 if (strncmp(name, "refs/", 5) == 0)
1404 name += 5;
1405 if (strncmp(name, "got/", 4) == 0)
1406 continue;
1407 if (strncmp(name, "heads/", 6) == 0)
1408 name += 6;
1409 if (strncmp(name, "remotes/", 8) == 0)
1410 name += 8;
1411 if (strncmp(name, "tags/", 5) == 0) {
1412 err = got_object_open_as_tag(&tag, repo, re->id);
1413 if (err) {
1414 if (err->code != GOT_ERR_OBJ_TYPE)
1415 return err;
1416 /* Ref points at something other than a tag. */
1417 err = NULL;
1418 tag = NULL;
1421 cmp = got_object_id_cmp(tag ?
1422 got_object_tag_get_object_id(tag) : re->id, id);
1423 if (tag)
1424 got_object_tag_close(tag);
1425 if (cmp != 0)
1426 continue;
1427 s = refs_str;
1428 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1429 name) == -1) {
1430 err = got_error_from_errno("asprintf");
1431 free(s);
1432 return err;
1434 free(s);
1436 err = got_object_id_str(&id_str, id);
1437 if (err)
1438 return err;
1440 printf(GOT_COMMIT_SEP_STR);
1441 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1442 refs_str ? refs_str : "", refs_str ? ")" : "");
1443 free(id_str);
1444 id_str = NULL;
1445 free(refs_str);
1446 refs_str = NULL;
1447 printf("from: %s\n", got_object_commit_get_author(commit));
1448 committer_time = got_object_commit_get_committer_time(commit);
1449 datestr = get_datestr(&committer_time, datebuf);
1450 if (datestr)
1451 printf("date: %s UTC\n", datestr);
1452 author = got_object_commit_get_author(commit);
1453 committer = got_object_commit_get_committer(commit);
1454 if (strcmp(author, committer) != 0)
1455 printf("via: %s\n", committer);
1456 if (got_object_commit_get_nparents(commit) > 1) {
1457 const struct got_object_id_queue *parent_ids;
1458 struct got_object_qid *qid;
1459 int n = 1;
1460 parent_ids = got_object_commit_get_parent_ids(commit);
1461 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1462 err = got_object_id_str(&id_str, qid->id);
1463 if (err)
1464 return err;
1465 printf("parent %d: %s\n", n++, id_str);
1466 free(id_str);
1470 err = got_object_commit_get_logmsg(&logmsg0, commit);
1471 if (err)
1472 return err;
1474 logmsg = logmsg0;
1475 do {
1476 line = strsep(&logmsg, "\n");
1477 if (line)
1478 printf(" %s\n", line);
1479 } while (line);
1480 free(logmsg0);
1482 if (show_patch) {
1483 err = print_patch(commit, id, diff_context, repo);
1484 if (err == 0)
1485 printf("\n");
1488 if (fflush(stdout) != 0 && err == NULL)
1489 err = got_error_from_errno("fflush");
1490 return err;
1493 static const struct got_error *
1494 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1495 char *path, int show_patch, int diff_context, int limit,
1496 int first_parent_traversal, struct got_reflist_head *refs)
1498 const struct got_error *err;
1499 struct got_commit_graph *graph;
1501 err = got_commit_graph_open(&graph, root_id, path,
1502 first_parent_traversal, repo);
1503 if (err)
1504 return err;
1505 err = got_commit_graph_iter_start(graph, root_id, repo);
1506 if (err)
1507 goto done;
1508 for (;;) {
1509 struct got_commit_object *commit;
1510 struct got_object_id *id;
1512 if (sigint_received || sigpipe_received)
1513 break;
1515 err = got_commit_graph_iter_next(&id, graph);
1516 if (err) {
1517 if (err->code == GOT_ERR_ITER_COMPLETED) {
1518 err = NULL;
1519 break;
1521 if (err->code != GOT_ERR_ITER_NEED_MORE)
1522 break;
1523 err = got_commit_graph_fetch_commits(graph, 1, repo);
1524 if (err)
1525 break;
1526 else
1527 continue;
1529 if (id == NULL)
1530 break;
1532 err = got_object_open_as_commit(&commit, repo, id);
1533 if (err)
1534 break;
1535 err = print_commit(commit, id, repo, show_patch, diff_context,
1536 refs);
1537 got_object_commit_close(commit);
1538 if (err || (limit && --limit == 0))
1539 break;
1541 done:
1542 got_commit_graph_close(graph);
1543 return err;
1546 __dead static void
1547 usage_log(void)
1549 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1550 "[-r repository-path] [path]\n", getprogname());
1551 exit(1);
1554 static const struct got_error *
1555 cmd_log(int argc, char *argv[])
1557 const struct got_error *error;
1558 struct got_repository *repo = NULL;
1559 struct got_worktree *worktree = NULL;
1560 struct got_commit_object *commit = NULL;
1561 struct got_object_id *id = NULL;
1562 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1563 char *start_commit = NULL;
1564 int diff_context = 3, ch;
1565 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1566 const char *errstr;
1567 struct got_reflist_head refs;
1569 SIMPLEQ_INIT(&refs);
1571 #ifndef PROFILE
1572 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1573 NULL)
1574 == -1)
1575 err(1, "pledge");
1576 #endif
1578 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1579 switch (ch) {
1580 case 'p':
1581 show_patch = 1;
1582 break;
1583 case 'c':
1584 start_commit = optarg;
1585 break;
1586 case 'C':
1587 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1588 &errstr);
1589 if (errstr != NULL)
1590 err(1, "-C option %s", errstr);
1591 break;
1592 case 'l':
1593 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1594 if (errstr != NULL)
1595 err(1, "-l option %s", errstr);
1596 break;
1597 case 'f':
1598 first_parent_traversal = 1;
1599 break;
1600 case 'r':
1601 repo_path = realpath(optarg, NULL);
1602 if (repo_path == NULL)
1603 err(1, "-r option");
1604 got_path_strip_trailing_slashes(repo_path);
1605 break;
1606 default:
1607 usage_log();
1608 /* NOTREACHED */
1612 argc -= optind;
1613 argv += optind;
1615 cwd = getcwd(NULL, 0);
1616 if (cwd == NULL) {
1617 error = got_error_from_errno("getcwd");
1618 goto done;
1621 error = got_worktree_open(&worktree, cwd);
1622 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1623 goto done;
1624 error = NULL;
1626 if (argc == 0) {
1627 path = strdup("");
1628 if (path == NULL) {
1629 error = got_error_from_errno("strdup");
1630 goto done;
1632 } else if (argc == 1) {
1633 if (worktree) {
1634 error = got_worktree_resolve_path(&path, worktree,
1635 argv[0]);
1636 if (error)
1637 goto done;
1638 } else {
1639 path = strdup(argv[0]);
1640 if (path == NULL) {
1641 error = got_error_from_errno("strdup");
1642 goto done;
1645 } else
1646 usage_log();
1648 if (repo_path == NULL) {
1649 repo_path = worktree ?
1650 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1652 if (repo_path == NULL) {
1653 error = got_error_from_errno("strdup");
1654 goto done;
1657 error = got_repo_open(&repo, repo_path);
1658 if (error != NULL)
1659 goto done;
1661 error = apply_unveil(got_repo_get_path(repo), 1,
1662 worktree ? got_worktree_get_root_path(worktree) : NULL);
1663 if (error)
1664 goto done;
1666 if (start_commit == NULL) {
1667 struct got_reference *head_ref;
1668 error = got_ref_open(&head_ref, repo,
1669 worktree ? got_worktree_get_head_ref_name(worktree)
1670 : GOT_REF_HEAD, 0);
1671 if (error != NULL)
1672 return error;
1673 error = got_ref_resolve(&id, repo, head_ref);
1674 got_ref_close(head_ref);
1675 if (error != NULL)
1676 return error;
1677 error = got_object_open_as_commit(&commit, repo, id);
1678 } else {
1679 struct got_reference *ref;
1680 error = got_ref_open(&ref, repo, start_commit, 0);
1681 if (error == NULL) {
1682 int obj_type;
1683 error = got_ref_resolve(&id, repo, ref);
1684 got_ref_close(ref);
1685 if (error != NULL)
1686 goto done;
1687 error = got_object_get_type(&obj_type, repo, id);
1688 if (error != NULL)
1689 goto done;
1690 if (obj_type == GOT_OBJ_TYPE_TAG) {
1691 struct got_tag_object *tag;
1692 error = got_object_open_as_tag(&tag, repo, id);
1693 if (error != NULL)
1694 goto done;
1695 if (got_object_tag_get_object_type(tag) !=
1696 GOT_OBJ_TYPE_COMMIT) {
1697 got_object_tag_close(tag);
1698 error = got_error(GOT_ERR_OBJ_TYPE);
1699 goto done;
1701 free(id);
1702 id = got_object_id_dup(
1703 got_object_tag_get_object_id(tag));
1704 if (id == NULL)
1705 error = got_error_from_errno(
1706 "got_object_id_dup");
1707 got_object_tag_close(tag);
1708 if (error)
1709 goto done;
1710 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1711 error = got_error(GOT_ERR_OBJ_TYPE);
1712 goto done;
1714 error = got_object_open_as_commit(&commit, repo, id);
1715 if (error != NULL)
1716 goto done;
1718 if (commit == NULL) {
1719 error = got_repo_match_object_id_prefix(&id,
1720 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1721 if (error != NULL)
1722 return error;
1725 if (error != NULL)
1726 goto done;
1728 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1729 if (error != NULL)
1730 goto done;
1731 if (in_repo_path) {
1732 free(path);
1733 path = in_repo_path;
1736 error = got_ref_list(&refs, repo);
1737 if (error)
1738 goto done;
1740 error = print_commits(id, repo, path, show_patch,
1741 diff_context, limit, first_parent_traversal, &refs);
1742 done:
1743 free(path);
1744 free(repo_path);
1745 free(cwd);
1746 free(id);
1747 if (worktree)
1748 got_worktree_close(worktree);
1749 if (repo) {
1750 const struct got_error *repo_error;
1751 repo_error = got_repo_close(repo);
1752 if (error == NULL)
1753 error = repo_error;
1755 got_ref_list_free(&refs);
1756 return error;
1759 __dead static void
1760 usage_diff(void)
1762 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1763 "[object1 object2 | path]\n", getprogname());
1764 exit(1);
1767 struct print_diff_arg {
1768 struct got_repository *repo;
1769 struct got_worktree *worktree;
1770 int diff_context;
1771 const char *id_str;
1772 int header_shown;
1773 int diff_staged;
1776 static const struct got_error *
1777 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1778 const char *path, struct got_object_id *blob_id,
1779 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1781 struct print_diff_arg *a = arg;
1782 const struct got_error *err = NULL;
1783 struct got_blob_object *blob1 = NULL;
1784 FILE *f2 = NULL;
1785 char *abspath = NULL, *label1 = NULL;
1786 struct stat sb;
1788 if (a->diff_staged) {
1789 if (staged_status != GOT_STATUS_MODIFY &&
1790 staged_status != GOT_STATUS_ADD &&
1791 staged_status != GOT_STATUS_DELETE)
1792 return NULL;
1793 } else {
1794 if (staged_status == GOT_STATUS_DELETE)
1795 return NULL;
1796 if (status != GOT_STATUS_MODIFY &&
1797 status != GOT_STATUS_ADD &&
1798 status != GOT_STATUS_DELETE &&
1799 status != GOT_STATUS_CONFLICT)
1800 return NULL;
1803 if (!a->header_shown) {
1804 printf("diff %s %s%s\n", a->id_str,
1805 got_worktree_get_root_path(a->worktree),
1806 a->diff_staged ? " (staged changes)" : "");
1807 a->header_shown = 1;
1810 if (a->diff_staged) {
1811 const char *label1 = NULL, *label2 = NULL;
1812 switch (staged_status) {
1813 case GOT_STATUS_MODIFY:
1814 label1 = path;
1815 label2 = path;
1816 break;
1817 case GOT_STATUS_ADD:
1818 label2 = path;
1819 break;
1820 case GOT_STATUS_DELETE:
1821 label1 = path;
1822 break;
1823 default:
1824 return got_error(GOT_ERR_FILE_STATUS);
1826 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1827 label1, label2, a->diff_context, a->repo, stdout);
1830 if (staged_status == GOT_STATUS_ADD ||
1831 staged_status == GOT_STATUS_MODIFY) {
1832 char *id_str;
1833 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1834 8192);
1835 if (err)
1836 goto done;
1837 err = got_object_id_str(&id_str, staged_blob_id);
1838 if (err)
1839 goto done;
1840 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1841 err = got_error_from_errno("asprintf");
1842 free(id_str);
1843 goto done;
1845 free(id_str);
1846 } else if (status != GOT_STATUS_ADD) {
1847 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1848 if (err)
1849 goto done;
1852 if (status != GOT_STATUS_DELETE) {
1853 if (asprintf(&abspath, "%s/%s",
1854 got_worktree_get_root_path(a->worktree), path) == -1) {
1855 err = got_error_from_errno("asprintf");
1856 goto done;
1859 f2 = fopen(abspath, "r");
1860 if (f2 == NULL) {
1861 err = got_error_from_errno2("fopen", abspath);
1862 goto done;
1864 if (lstat(abspath, &sb) == -1) {
1865 err = got_error_from_errno2("lstat", abspath);
1866 goto done;
1868 } else
1869 sb.st_size = 0;
1871 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1872 a->diff_context, stdout);
1873 done:
1874 if (blob1)
1875 got_object_blob_close(blob1);
1876 if (f2 && fclose(f2) != 0 && err == NULL)
1877 err = got_error_from_errno("fclose");
1878 free(abspath);
1879 return err;
1882 static const struct got_error *
1883 match_object_id(struct got_object_id **id, char **label,
1884 const char *id_str, int obj_type, struct got_repository *repo)
1886 const struct got_error *err;
1887 struct got_tag_object *tag;
1888 struct got_reference *ref = NULL;
1890 *id = NULL;
1891 *label = NULL;
1893 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1894 if (err == NULL) {
1895 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1896 if (*id == NULL)
1897 err = got_error_from_errno("got_object_id_dup");
1898 if (asprintf(label, "refs/tags/%s",
1899 got_object_tag_get_name(tag)) == -1)
1900 err = got_error_from_errno("asprintf");
1901 got_object_tag_close(tag);
1902 return err;
1903 } else if (err->code != GOT_ERR_NO_OBJ)
1904 return err;
1906 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1907 if (err) {
1908 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1909 return err;
1910 err = got_ref_open(&ref, repo, id_str, 0);
1911 if (err != NULL)
1912 goto done;
1913 *label = strdup(got_ref_get_name(ref));
1914 if (*label == NULL) {
1915 err = got_error_from_errno("strdup");
1916 goto done;
1918 err = got_ref_resolve(id, repo, ref);
1919 } else {
1920 err = got_object_id_str(label, *id);
1921 if (*label == NULL) {
1922 err = got_error_from_errno("strdup");
1923 goto done;
1926 done:
1927 if (ref)
1928 got_ref_close(ref);
1929 return err;
1933 static const struct got_error *
1934 cmd_diff(int argc, char *argv[])
1936 const struct got_error *error;
1937 struct got_repository *repo = NULL;
1938 struct got_worktree *worktree = NULL;
1939 char *cwd = NULL, *repo_path = NULL;
1940 struct got_object_id *id1 = NULL, *id2 = NULL;
1941 const char *id_str1 = NULL, *id_str2 = NULL;
1942 char *label1 = NULL, *label2 = NULL;
1943 int type1, type2;
1944 int diff_context = 3, diff_staged = 0, ch;
1945 const char *errstr;
1946 char *path = NULL;
1948 #ifndef PROFILE
1949 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1950 NULL) == -1)
1951 err(1, "pledge");
1952 #endif
1954 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1955 switch (ch) {
1956 case 'C':
1957 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1958 if (errstr != NULL)
1959 err(1, "-C option %s", errstr);
1960 break;
1961 case 'r':
1962 repo_path = realpath(optarg, NULL);
1963 if (repo_path == NULL)
1964 err(1, "-r option");
1965 got_path_strip_trailing_slashes(repo_path);
1966 break;
1967 case 's':
1968 diff_staged = 1;
1969 break;
1970 default:
1971 usage_diff();
1972 /* NOTREACHED */
1976 argc -= optind;
1977 argv += optind;
1979 cwd = getcwd(NULL, 0);
1980 if (cwd == NULL) {
1981 error = got_error_from_errno("getcwd");
1982 goto done;
1984 error = got_worktree_open(&worktree, cwd);
1985 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1986 goto done;
1987 if (argc <= 1) {
1988 if (worktree == NULL) {
1989 error = got_error(GOT_ERR_NOT_WORKTREE);
1990 goto done;
1992 if (repo_path)
1993 errx(1,
1994 "-r option can't be used when diffing a work tree");
1995 repo_path = strdup(got_worktree_get_repo_path(worktree));
1996 if (repo_path == NULL) {
1997 error = got_error_from_errno("strdup");
1998 goto done;
2000 if (argc == 1) {
2001 error = got_worktree_resolve_path(&path, worktree,
2002 argv[0]);
2003 if (error)
2004 goto done;
2005 } else {
2006 path = strdup("");
2007 if (path == NULL) {
2008 error = got_error_from_errno("strdup");
2009 goto done;
2012 } else if (argc == 2) {
2013 if (diff_staged)
2014 errx(1, "-s option can't be used when diffing "
2015 "objects in repository");
2016 id_str1 = argv[0];
2017 id_str2 = argv[1];
2018 if (worktree && repo_path == NULL) {
2019 repo_path =
2020 strdup(got_worktree_get_repo_path(worktree));
2021 if (repo_path == NULL) {
2022 error = got_error_from_errno("strdup");
2023 goto done;
2026 } else
2027 usage_diff();
2029 if (repo_path == NULL) {
2030 repo_path = getcwd(NULL, 0);
2031 if (repo_path == NULL)
2032 return got_error_from_errno("getcwd");
2035 error = got_repo_open(&repo, repo_path);
2036 free(repo_path);
2037 if (error != NULL)
2038 goto done;
2040 error = apply_unveil(got_repo_get_path(repo), 1,
2041 worktree ? got_worktree_get_root_path(worktree) : NULL);
2042 if (error)
2043 goto done;
2045 if (argc <= 1) {
2046 struct print_diff_arg arg;
2047 struct got_pathlist_head paths;
2048 char *id_str;
2050 TAILQ_INIT(&paths);
2052 error = got_object_id_str(&id_str,
2053 got_worktree_get_base_commit_id(worktree));
2054 if (error)
2055 goto done;
2056 arg.repo = repo;
2057 arg.worktree = worktree;
2058 arg.diff_context = diff_context;
2059 arg.id_str = id_str;
2060 arg.header_shown = 0;
2061 arg.diff_staged = diff_staged;
2063 error = got_pathlist_append(&paths, path, NULL);
2064 if (error)
2065 goto done;
2067 error = got_worktree_status(worktree, &paths, repo, print_diff,
2068 &arg, check_cancelled, NULL);
2069 free(id_str);
2070 got_pathlist_free(&paths);
2071 goto done;
2074 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2075 if (error)
2076 goto done;
2078 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2079 if (error)
2080 goto done;
2082 error = got_object_get_type(&type1, repo, id1);
2083 if (error)
2084 goto done;
2086 error = got_object_get_type(&type2, repo, id2);
2087 if (error)
2088 goto done;
2090 if (type1 != type2) {
2091 error = got_error(GOT_ERR_OBJ_TYPE);
2092 goto done;
2095 switch (type1) {
2096 case GOT_OBJ_TYPE_BLOB:
2097 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2098 diff_context, repo, stdout);
2099 break;
2100 case GOT_OBJ_TYPE_TREE:
2101 error = got_diff_objects_as_trees(id1, id2, "", "",
2102 diff_context, repo, stdout);
2103 break;
2104 case GOT_OBJ_TYPE_COMMIT:
2105 printf("diff %s %s\n", label1, label2);
2106 error = got_diff_objects_as_commits(id1, id2, diff_context,
2107 repo, stdout);
2108 break;
2109 default:
2110 error = got_error(GOT_ERR_OBJ_TYPE);
2113 done:
2114 free(label1);
2115 free(label2);
2116 free(id1);
2117 free(id2);
2118 free(path);
2119 if (worktree)
2120 got_worktree_close(worktree);
2121 if (repo) {
2122 const struct got_error *repo_error;
2123 repo_error = got_repo_close(repo);
2124 if (error == NULL)
2125 error = repo_error;
2127 return error;
2130 __dead static void
2131 usage_blame(void)
2133 fprintf(stderr,
2134 "usage: %s blame [-c commit] [-r repository-path] path\n",
2135 getprogname());
2136 exit(1);
2139 static const struct got_error *
2140 cmd_blame(int argc, char *argv[])
2142 const struct got_error *error;
2143 struct got_repository *repo = NULL;
2144 struct got_worktree *worktree = NULL;
2145 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2146 struct got_object_id *commit_id = NULL;
2147 char *commit_id_str = NULL;
2148 int ch;
2150 #ifndef PROFILE
2151 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2152 NULL) == -1)
2153 err(1, "pledge");
2154 #endif
2156 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2157 switch (ch) {
2158 case 'c':
2159 commit_id_str = optarg;
2160 break;
2161 case 'r':
2162 repo_path = realpath(optarg, NULL);
2163 if (repo_path == NULL)
2164 err(1, "-r option");
2165 got_path_strip_trailing_slashes(repo_path);
2166 break;
2167 default:
2168 usage_blame();
2169 /* NOTREACHED */
2173 argc -= optind;
2174 argv += optind;
2176 if (argc == 1)
2177 path = argv[0];
2178 else
2179 usage_blame();
2181 cwd = getcwd(NULL, 0);
2182 if (cwd == NULL) {
2183 error = got_error_from_errno("getcwd");
2184 goto done;
2186 if (repo_path == NULL) {
2187 error = got_worktree_open(&worktree, cwd);
2188 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2189 goto done;
2190 else
2191 error = NULL;
2192 if (worktree) {
2193 repo_path =
2194 strdup(got_worktree_get_repo_path(worktree));
2195 if (repo_path == NULL)
2196 error = got_error_from_errno("strdup");
2197 if (error)
2198 goto done;
2199 } else {
2200 repo_path = strdup(cwd);
2201 if (repo_path == NULL) {
2202 error = got_error_from_errno("strdup");
2203 goto done;
2208 error = got_repo_open(&repo, repo_path);
2209 if (error != NULL)
2210 goto done;
2212 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2213 if (error)
2214 goto done;
2216 if (worktree) {
2217 const char *prefix = got_worktree_get_path_prefix(worktree);
2218 char *p, *worktree_subdir = cwd +
2219 strlen(got_worktree_get_root_path(worktree));
2220 if (asprintf(&p, "%s%s%s%s%s",
2221 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2222 worktree_subdir, worktree_subdir[0] ? "/" : "",
2223 path) == -1) {
2224 error = got_error_from_errno("asprintf");
2225 goto done;
2227 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2228 free(p);
2229 } else {
2230 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2232 if (error)
2233 goto done;
2235 if (commit_id_str == NULL) {
2236 struct got_reference *head_ref;
2237 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2238 if (error != NULL)
2239 goto done;
2240 error = got_ref_resolve(&commit_id, repo, head_ref);
2241 got_ref_close(head_ref);
2242 if (error != NULL)
2243 goto done;
2244 } else {
2245 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2246 if (error)
2247 goto done;
2250 error = got_blame(in_repo_path, commit_id, repo, stdout);
2251 done:
2252 free(in_repo_path);
2253 free(repo_path);
2254 free(cwd);
2255 free(commit_id);
2256 if (worktree)
2257 got_worktree_close(worktree);
2258 if (repo) {
2259 const struct got_error *repo_error;
2260 repo_error = got_repo_close(repo);
2261 if (error == NULL)
2262 error = repo_error;
2264 return error;
2267 __dead static void
2268 usage_tree(void)
2270 fprintf(stderr,
2271 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2272 getprogname());
2273 exit(1);
2276 static void
2277 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2278 const char *root_path)
2280 int is_root_path = (strcmp(path, root_path) == 0);
2281 const char *modestr = "";
2283 path += strlen(root_path);
2284 while (path[0] == '/')
2285 path++;
2287 if (S_ISLNK(te->mode))
2288 modestr = "@";
2289 else if (S_ISDIR(te->mode))
2290 modestr = "/";
2291 else if (te->mode & S_IXUSR)
2292 modestr = "*";
2294 printf("%s%s%s%s%s\n", id ? id : "", path,
2295 is_root_path ? "" : "/", te->name, modestr);
2298 static const struct got_error *
2299 print_tree(const char *path, struct got_object_id *commit_id,
2300 int show_ids, int recurse, const char *root_path,
2301 struct got_repository *repo)
2303 const struct got_error *err = NULL;
2304 struct got_object_id *tree_id = NULL;
2305 struct got_tree_object *tree = NULL;
2306 const struct got_tree_entries *entries;
2307 struct got_tree_entry *te;
2309 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2310 if (err)
2311 goto done;
2313 err = got_object_open_as_tree(&tree, repo, tree_id);
2314 if (err)
2315 goto done;
2316 entries = got_object_tree_get_entries(tree);
2317 te = SIMPLEQ_FIRST(&entries->head);
2318 while (te) {
2319 char *id = NULL;
2321 if (sigint_received || sigpipe_received)
2322 break;
2324 if (show_ids) {
2325 char *id_str;
2326 err = got_object_id_str(&id_str, te->id);
2327 if (err)
2328 goto done;
2329 if (asprintf(&id, "%s ", id_str) == -1) {
2330 err = got_error_from_errno("asprintf");
2331 free(id_str);
2332 goto done;
2334 free(id_str);
2336 print_entry(te, id, path, root_path);
2337 free(id);
2339 if (recurse && S_ISDIR(te->mode)) {
2340 char *child_path;
2341 if (asprintf(&child_path, "%s%s%s", path,
2342 path[0] == '/' && path[1] == '\0' ? "" : "/",
2343 te->name) == -1) {
2344 err = got_error_from_errno("asprintf");
2345 goto done;
2347 err = print_tree(child_path, commit_id, show_ids, 1,
2348 root_path, repo);
2349 free(child_path);
2350 if (err)
2351 goto done;
2354 te = SIMPLEQ_NEXT(te, entry);
2356 done:
2357 if (tree)
2358 got_object_tree_close(tree);
2359 free(tree_id);
2360 return err;
2363 static const struct got_error *
2364 cmd_tree(int argc, char *argv[])
2366 const struct got_error *error;
2367 struct got_repository *repo = NULL;
2368 struct got_worktree *worktree = NULL;
2369 const char *path;
2370 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2371 struct got_object_id *commit_id = NULL;
2372 char *commit_id_str = NULL;
2373 int show_ids = 0, recurse = 0;
2374 int ch;
2376 #ifndef PROFILE
2377 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2378 NULL) == -1)
2379 err(1, "pledge");
2380 #endif
2382 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2383 switch (ch) {
2384 case 'c':
2385 commit_id_str = optarg;
2386 break;
2387 case 'r':
2388 repo_path = realpath(optarg, NULL);
2389 if (repo_path == NULL)
2390 err(1, "-r option");
2391 got_path_strip_trailing_slashes(repo_path);
2392 break;
2393 case 'i':
2394 show_ids = 1;
2395 break;
2396 case 'R':
2397 recurse = 1;
2398 break;
2399 default:
2400 usage_tree();
2401 /* NOTREACHED */
2405 argc -= optind;
2406 argv += optind;
2408 if (argc == 1)
2409 path = argv[0];
2410 else if (argc > 1)
2411 usage_tree();
2412 else
2413 path = NULL;
2415 cwd = getcwd(NULL, 0);
2416 if (cwd == NULL) {
2417 error = got_error_from_errno("getcwd");
2418 goto done;
2420 if (repo_path == NULL) {
2421 error = got_worktree_open(&worktree, cwd);
2422 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2423 goto done;
2424 else
2425 error = NULL;
2426 if (worktree) {
2427 repo_path =
2428 strdup(got_worktree_get_repo_path(worktree));
2429 if (repo_path == NULL)
2430 error = got_error_from_errno("strdup");
2431 if (error)
2432 goto done;
2433 } else {
2434 repo_path = strdup(cwd);
2435 if (repo_path == NULL) {
2436 error = got_error_from_errno("strdup");
2437 goto done;
2442 error = got_repo_open(&repo, repo_path);
2443 if (error != NULL)
2444 goto done;
2446 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2447 if (error)
2448 goto done;
2450 if (path == NULL) {
2451 if (worktree) {
2452 char *p, *worktree_subdir = cwd +
2453 strlen(got_worktree_get_root_path(worktree));
2454 if (asprintf(&p, "%s/%s",
2455 got_worktree_get_path_prefix(worktree),
2456 worktree_subdir) == -1) {
2457 error = got_error_from_errno("asprintf");
2458 goto done;
2460 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2461 free(p);
2462 if (error)
2463 goto done;
2464 } else
2465 path = "/";
2467 if (in_repo_path == NULL) {
2468 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2469 if (error != NULL)
2470 goto done;
2473 if (commit_id_str == NULL) {
2474 struct got_reference *head_ref;
2475 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2476 if (error != NULL)
2477 goto done;
2478 error = got_ref_resolve(&commit_id, repo, head_ref);
2479 got_ref_close(head_ref);
2480 if (error != NULL)
2481 goto done;
2482 } else {
2483 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2484 if (error)
2485 goto done;
2488 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2489 in_repo_path, repo);
2490 done:
2491 free(in_repo_path);
2492 free(repo_path);
2493 free(cwd);
2494 free(commit_id);
2495 if (worktree)
2496 got_worktree_close(worktree);
2497 if (repo) {
2498 const struct got_error *repo_error;
2499 repo_error = got_repo_close(repo);
2500 if (error == NULL)
2501 error = repo_error;
2503 return error;
2506 __dead static void
2507 usage_status(void)
2509 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2510 exit(1);
2513 static const struct got_error *
2514 print_status(void *arg, unsigned char status, unsigned char staged_status,
2515 const char *path, struct got_object_id *blob_id,
2516 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2518 if (status == staged_status && (status == GOT_STATUS_DELETE))
2519 status = GOT_STATUS_NO_CHANGE;
2520 printf("%c%c %s\n", status, staged_status, path);
2521 return NULL;
2524 static const struct got_error *
2525 cmd_status(int argc, char *argv[])
2527 const struct got_error *error = NULL;
2528 struct got_repository *repo = NULL;
2529 struct got_worktree *worktree = NULL;
2530 char *cwd = NULL;
2531 struct got_pathlist_head paths;
2532 struct got_pathlist_entry *pe;
2533 int ch;
2535 TAILQ_INIT(&paths);
2537 while ((ch = getopt(argc, argv, "")) != -1) {
2538 switch (ch) {
2539 default:
2540 usage_status();
2541 /* NOTREACHED */
2545 argc -= optind;
2546 argv += optind;
2548 #ifndef PROFILE
2549 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2550 NULL) == -1)
2551 err(1, "pledge");
2552 #endif
2553 cwd = getcwd(NULL, 0);
2554 if (cwd == NULL) {
2555 error = got_error_from_errno("getcwd");
2556 goto done;
2559 error = got_worktree_open(&worktree, cwd);
2560 if (error != NULL)
2561 goto done;
2563 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2564 if (error != NULL)
2565 goto done;
2567 error = apply_unveil(got_repo_get_path(repo), 1,
2568 got_worktree_get_root_path(worktree));
2569 if (error)
2570 goto done;
2572 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2573 if (error)
2574 goto done;
2576 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2577 check_cancelled, NULL);
2578 done:
2579 TAILQ_FOREACH(pe, &paths, entry)
2580 free((char *)pe->path);
2581 got_pathlist_free(&paths);
2582 free(cwd);
2583 return error;
2586 __dead static void
2587 usage_ref(void)
2589 fprintf(stderr,
2590 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2591 getprogname());
2592 exit(1);
2595 static const struct got_error *
2596 list_refs(struct got_repository *repo)
2598 static const struct got_error *err = NULL;
2599 struct got_reflist_head refs;
2600 struct got_reflist_entry *re;
2602 SIMPLEQ_INIT(&refs);
2603 err = got_ref_list(&refs, repo);
2604 if (err)
2605 return err;
2607 SIMPLEQ_FOREACH(re, &refs, entry) {
2608 char *refstr;
2609 refstr = got_ref_to_str(re->ref);
2610 if (refstr == NULL)
2611 return got_error_from_errno("got_ref_to_str");
2612 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2613 free(refstr);
2616 got_ref_list_free(&refs);
2617 return NULL;
2620 static const struct got_error *
2621 delete_ref(struct got_repository *repo, const char *refname)
2623 const struct got_error *err = NULL;
2624 struct got_reference *ref;
2626 err = got_ref_open(&ref, repo, refname, 0);
2627 if (err)
2628 return err;
2630 err = got_ref_delete(ref, repo);
2631 got_ref_close(ref);
2632 return err;
2635 static const struct got_error *
2636 add_ref(struct got_repository *repo, const char *refname, const char *target)
2638 const struct got_error *err = NULL;
2639 struct got_object_id *id;
2640 struct got_reference *ref = NULL;
2643 * Don't let the user create a reference named '-'.
2644 * While technically a valid reference name, this case is usually
2645 * an unintended typo.
2647 if (refname[0] == '-' && refname[1] == '\0')
2648 return got_error(GOT_ERR_BAD_REF_NAME);
2650 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2651 repo);
2652 if (err) {
2653 struct got_reference *target_ref;
2655 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2656 return err;
2657 err = got_ref_open(&target_ref, repo, target, 0);
2658 if (err)
2659 return err;
2660 err = got_ref_resolve(&id, repo, target_ref);
2661 got_ref_close(target_ref);
2662 if (err)
2663 return err;
2666 err = got_ref_alloc(&ref, refname, id);
2667 if (err)
2668 goto done;
2670 err = got_ref_write(ref, repo);
2671 done:
2672 if (ref)
2673 got_ref_close(ref);
2674 free(id);
2675 return err;
2678 static const struct got_error *
2679 add_symref(struct got_repository *repo, const char *refname, const char *target)
2681 const struct got_error *err = NULL;
2682 struct got_reference *ref = NULL;
2683 struct got_reference *target_ref = NULL;
2686 * Don't let the user create a reference named '-'.
2687 * While technically a valid reference name, this case is usually
2688 * an unintended typo.
2690 if (refname[0] == '-' && refname[1] == '\0')
2691 return got_error(GOT_ERR_BAD_REF_NAME);
2693 err = got_ref_open(&target_ref, repo, target, 0);
2694 if (err)
2695 return err;
2697 err = got_ref_alloc_symref(&ref, refname, target_ref);
2698 if (err)
2699 goto done;
2701 err = got_ref_write(ref, repo);
2702 done:
2703 if (target_ref)
2704 got_ref_close(target_ref);
2705 if (ref)
2706 got_ref_close(ref);
2707 return err;
2710 static const struct got_error *
2711 cmd_ref(int argc, char *argv[])
2713 const struct got_error *error = NULL;
2714 struct got_repository *repo = NULL;
2715 struct got_worktree *worktree = NULL;
2716 char *cwd = NULL, *repo_path = NULL;
2717 int ch, do_list = 0, create_symref = 0;
2718 const char *delref = NULL;
2720 /* TODO: Add -s option for adding symbolic references. */
2721 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2722 switch (ch) {
2723 case 'd':
2724 delref = optarg;
2725 break;
2726 case 'r':
2727 repo_path = realpath(optarg, NULL);
2728 if (repo_path == NULL)
2729 err(1, "-r option");
2730 got_path_strip_trailing_slashes(repo_path);
2731 break;
2732 case 'l':
2733 do_list = 1;
2734 break;
2735 case 's':
2736 create_symref = 1;
2737 break;
2738 default:
2739 usage_ref();
2740 /* NOTREACHED */
2744 if (do_list && delref)
2745 errx(1, "-l and -d options are mutually exclusive\n");
2747 argc -= optind;
2748 argv += optind;
2750 if (do_list || delref) {
2751 if (create_symref)
2752 errx(1, "-s option cannot be used together with the "
2753 "-l or -d options");
2754 if (argc > 0)
2755 usage_ref();
2756 } else if (argc != 2)
2757 usage_ref();
2759 #ifndef PROFILE
2760 if (do_list) {
2761 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2762 NULL) == -1)
2763 err(1, "pledge");
2764 } else {
2765 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2766 "sendfd unveil", NULL) == -1)
2767 err(1, "pledge");
2769 #endif
2770 cwd = getcwd(NULL, 0);
2771 if (cwd == NULL) {
2772 error = got_error_from_errno("getcwd");
2773 goto done;
2776 if (repo_path == NULL) {
2777 error = got_worktree_open(&worktree, cwd);
2778 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2779 goto done;
2780 else
2781 error = NULL;
2782 if (worktree) {
2783 repo_path =
2784 strdup(got_worktree_get_repo_path(worktree));
2785 if (repo_path == NULL)
2786 error = got_error_from_errno("strdup");
2787 if (error)
2788 goto done;
2789 } else {
2790 repo_path = strdup(cwd);
2791 if (repo_path == NULL) {
2792 error = got_error_from_errno("strdup");
2793 goto done;
2798 error = got_repo_open(&repo, repo_path);
2799 if (error != NULL)
2800 goto done;
2802 error = apply_unveil(got_repo_get_path(repo), do_list,
2803 worktree ? got_worktree_get_root_path(worktree) : NULL);
2804 if (error)
2805 goto done;
2807 if (do_list)
2808 error = list_refs(repo);
2809 else if (delref)
2810 error = delete_ref(repo, delref);
2811 else if (create_symref)
2812 error = add_symref(repo, argv[0], argv[1]);
2813 else
2814 error = add_ref(repo, argv[0], argv[1]);
2815 done:
2816 if (repo)
2817 got_repo_close(repo);
2818 if (worktree)
2819 got_worktree_close(worktree);
2820 free(cwd);
2821 free(repo_path);
2822 return error;
2825 __dead static void
2826 usage_branch(void)
2828 fprintf(stderr,
2829 "usage: %s branch [-r repository] -l | -d name | "
2830 "name [base-branch]\n", getprogname());
2831 exit(1);
2834 static const struct got_error *
2835 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2837 static const struct got_error *err = NULL;
2838 struct got_reflist_head refs;
2839 struct got_reflist_entry *re;
2841 SIMPLEQ_INIT(&refs);
2843 err = got_ref_list(&refs, repo);
2844 if (err)
2845 return err;
2847 SIMPLEQ_FOREACH(re, &refs, entry) {
2848 const char *refname, *marker = " ";
2849 char *refstr;
2850 refname = got_ref_get_name(re->ref);
2851 if (strncmp(refname, "refs/heads/", 11) != 0)
2852 continue;
2853 if (worktree && strcmp(refname,
2854 got_worktree_get_head_ref_name(worktree)) == 0) {
2855 struct got_object_id *id = NULL;
2856 err = got_ref_resolve(&id, repo, re->ref);
2857 if (err)
2858 return err;
2859 if (got_object_id_cmp(id,
2860 got_worktree_get_base_commit_id(worktree)) == 0)
2861 marker = "* ";
2862 else
2863 marker = "~ ";
2864 free(id);
2866 refname += 11;
2867 refstr = got_ref_to_str(re->ref);
2868 if (refstr == NULL)
2869 return got_error_from_errno("got_ref_to_str");
2870 printf("%s%s: %s\n", marker, refname, refstr);
2871 free(refstr);
2874 got_ref_list_free(&refs);
2875 return NULL;
2878 static const struct got_error *
2879 delete_branch(struct got_repository *repo, const char *branch_name)
2881 const struct got_error *err = NULL;
2882 struct got_reference *ref;
2883 char *refname;
2885 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2886 return got_error_from_errno("asprintf");
2888 err = got_ref_open(&ref, repo, refname, 0);
2889 if (err)
2890 goto done;
2892 err = got_ref_delete(ref, repo);
2893 got_ref_close(ref);
2894 done:
2895 free(refname);
2896 return err;
2899 static const struct got_error *
2900 add_branch(struct got_repository *repo, const char *branch_name,
2901 const char *base_branch)
2903 const struct got_error *err = NULL;
2904 struct got_object_id *id = NULL;
2905 struct got_reference *ref = NULL;
2906 char *base_refname = NULL, *refname = NULL;
2907 struct got_reference *base_ref;
2910 * Don't let the user create a branch named '-'.
2911 * While technically a valid reference name, this case is usually
2912 * an unintended typo.
2914 if (branch_name[0] == '-' && branch_name[1] == '\0')
2915 return got_error(GOT_ERR_BAD_REF_NAME);
2917 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2918 base_refname = strdup(GOT_REF_HEAD);
2919 if (base_refname == NULL)
2920 return got_error_from_errno("strdup");
2921 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2922 return got_error_from_errno("asprintf");
2924 err = got_ref_open(&base_ref, repo, base_refname, 0);
2925 if (err)
2926 goto done;
2927 err = got_ref_resolve(&id, repo, base_ref);
2928 got_ref_close(base_ref);
2929 if (err)
2930 goto done;
2932 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2933 err = got_error_from_errno("asprintf");
2934 goto done;
2937 err = got_ref_open(&ref, repo, refname, 0);
2938 if (err == NULL) {
2939 err = got_error(GOT_ERR_BRANCH_EXISTS);
2940 goto done;
2941 } else if (err->code != GOT_ERR_NOT_REF)
2942 goto done;
2944 err = got_ref_alloc(&ref, refname, id);
2945 if (err)
2946 goto done;
2948 err = got_ref_write(ref, repo);
2949 done:
2950 if (ref)
2951 got_ref_close(ref);
2952 free(id);
2953 free(base_refname);
2954 free(refname);
2955 return err;
2958 static const struct got_error *
2959 cmd_branch(int argc, char *argv[])
2961 const struct got_error *error = NULL;
2962 struct got_repository *repo = NULL;
2963 struct got_worktree *worktree = NULL;
2964 char *cwd = NULL, *repo_path = NULL;
2965 int ch, do_list = 0;
2966 const char *delref = NULL;
2968 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2969 switch (ch) {
2970 case 'd':
2971 delref = optarg;
2972 break;
2973 case 'r':
2974 repo_path = realpath(optarg, NULL);
2975 if (repo_path == NULL)
2976 err(1, "-r option");
2977 got_path_strip_trailing_slashes(repo_path);
2978 break;
2979 case 'l':
2980 do_list = 1;
2981 break;
2982 default:
2983 usage_branch();
2984 /* NOTREACHED */
2988 if (do_list && delref)
2989 errx(1, "-l and -d options are mutually exclusive\n");
2991 argc -= optind;
2992 argv += optind;
2994 if (do_list || delref) {
2995 if (argc > 0)
2996 usage_branch();
2997 } else if (argc < 1 || argc > 2)
2998 usage_branch();
3000 #ifndef PROFILE
3001 if (do_list) {
3002 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3003 NULL) == -1)
3004 err(1, "pledge");
3005 } else {
3006 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3007 "sendfd unveil", NULL) == -1)
3008 err(1, "pledge");
3010 #endif
3011 cwd = getcwd(NULL, 0);
3012 if (cwd == NULL) {
3013 error = got_error_from_errno("getcwd");
3014 goto done;
3017 if (repo_path == NULL) {
3018 error = got_worktree_open(&worktree, cwd);
3019 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3020 goto done;
3021 else
3022 error = NULL;
3023 if (worktree) {
3024 repo_path =
3025 strdup(got_worktree_get_repo_path(worktree));
3026 if (repo_path == NULL)
3027 error = got_error_from_errno("strdup");
3028 if (error)
3029 goto done;
3030 } else {
3031 repo_path = strdup(cwd);
3032 if (repo_path == NULL) {
3033 error = got_error_from_errno("strdup");
3034 goto done;
3039 error = got_repo_open(&repo, repo_path);
3040 if (error != NULL)
3041 goto done;
3043 error = apply_unveil(got_repo_get_path(repo), do_list,
3044 worktree ? got_worktree_get_root_path(worktree) : NULL);
3045 if (error)
3046 goto done;
3048 if (do_list)
3049 error = list_branches(repo, worktree);
3050 else if (delref)
3051 error = delete_branch(repo, delref);
3052 else {
3053 const char *base_branch;
3054 if (argc == 1) {
3055 base_branch = worktree ?
3056 got_worktree_get_head_ref_name(worktree) :
3057 GOT_REF_HEAD;
3058 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3059 base_branch += 11;
3060 } else
3061 base_branch = argv[1];
3062 error = add_branch(repo, argv[0], base_branch);
3064 done:
3065 if (repo)
3066 got_repo_close(repo);
3067 if (worktree)
3068 got_worktree_close(worktree);
3069 free(cwd);
3070 free(repo_path);
3071 return error;
3074 __dead static void
3075 usage_add(void)
3077 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3078 exit(1);
3081 static const struct got_error *
3082 cmd_add(int argc, char *argv[])
3084 const struct got_error *error = NULL;
3085 struct got_repository *repo = NULL;
3086 struct got_worktree *worktree = NULL;
3087 char *cwd = NULL;
3088 struct got_pathlist_head paths;
3089 struct got_pathlist_entry *pe;
3090 int ch;
3092 TAILQ_INIT(&paths);
3094 while ((ch = getopt(argc, argv, "")) != -1) {
3095 switch (ch) {
3096 default:
3097 usage_add();
3098 /* NOTREACHED */
3102 argc -= optind;
3103 argv += optind;
3105 #ifndef PROFILE
3106 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3107 NULL) == -1)
3108 err(1, "pledge");
3109 #endif
3110 if (argc < 1)
3111 usage_add();
3113 cwd = getcwd(NULL, 0);
3114 if (cwd == NULL) {
3115 error = got_error_from_errno("getcwd");
3116 goto done;
3119 error = got_worktree_open(&worktree, cwd);
3120 if (error)
3121 goto done;
3123 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3124 if (error != NULL)
3125 goto done;
3127 error = apply_unveil(got_repo_get_path(repo), 1,
3128 got_worktree_get_root_path(worktree));
3129 if (error)
3130 goto done;
3132 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3133 if (error)
3134 goto done;
3136 error = got_worktree_schedule_add(worktree, &paths, print_status,
3137 NULL, repo);
3138 done:
3139 if (repo)
3140 got_repo_close(repo);
3141 if (worktree)
3142 got_worktree_close(worktree);
3143 TAILQ_FOREACH(pe, &paths, entry)
3144 free((char *)pe->path);
3145 got_pathlist_free(&paths);
3146 free(cwd);
3147 return error;
3150 __dead static void
3151 usage_remove(void)
3153 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3154 exit(1);
3157 static const struct got_error *
3158 cmd_remove(int argc, char *argv[])
3160 const struct got_error *error = NULL;
3161 struct got_worktree *worktree = NULL;
3162 struct got_repository *repo = NULL;
3163 char *cwd = NULL;
3164 struct got_pathlist_head paths;
3165 struct got_pathlist_entry *pe;
3166 int ch, delete_local_mods = 0;
3168 TAILQ_INIT(&paths);
3170 while ((ch = getopt(argc, argv, "f")) != -1) {
3171 switch (ch) {
3172 case 'f':
3173 delete_local_mods = 1;
3174 break;
3175 default:
3176 usage_add();
3177 /* NOTREACHED */
3181 argc -= optind;
3182 argv += optind;
3184 #ifndef PROFILE
3185 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3186 NULL) == -1)
3187 err(1, "pledge");
3188 #endif
3189 if (argc < 1)
3190 usage_remove();
3192 cwd = getcwd(NULL, 0);
3193 if (cwd == NULL) {
3194 error = got_error_from_errno("getcwd");
3195 goto done;
3197 error = got_worktree_open(&worktree, cwd);
3198 if (error)
3199 goto done;
3201 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3202 if (error)
3203 goto done;
3205 error = apply_unveil(got_repo_get_path(repo), 1,
3206 got_worktree_get_root_path(worktree));
3207 if (error)
3208 goto done;
3210 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3211 if (error)
3212 goto done;
3214 error = got_worktree_schedule_delete(worktree, &paths,
3215 delete_local_mods, print_status, NULL, repo);
3216 if (error)
3217 goto done;
3218 done:
3219 if (repo)
3220 got_repo_close(repo);
3221 if (worktree)
3222 got_worktree_close(worktree);
3223 TAILQ_FOREACH(pe, &paths, entry)
3224 free((char *)pe->path);
3225 got_pathlist_free(&paths);
3226 free(cwd);
3227 return error;
3230 __dead static void
3231 usage_revert(void)
3233 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3234 "path ...\n", getprogname());
3235 exit(1);
3238 static const struct got_error *
3239 revert_progress(void *arg, unsigned char status, const char *path)
3241 while (path[0] == '/')
3242 path++;
3243 printf("%c %s\n", status, path);
3244 return NULL;
3247 struct choose_patch_arg {
3248 FILE *patch_script_file;
3249 const char *action;
3252 static const struct got_error *
3253 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3254 int nchanges, const char *action)
3256 char *line = NULL;
3257 size_t linesize = 0;
3258 ssize_t linelen;
3260 switch (status) {
3261 case GOT_STATUS_ADD:
3262 printf("A %s\n%s this addition? [y/n] ", path, action);
3263 break;
3264 case GOT_STATUS_DELETE:
3265 printf("D %s\n%s this deletion? [y/n] ", path, action);
3266 break;
3267 case GOT_STATUS_MODIFY:
3268 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3269 return got_error_from_errno("fseek");
3270 printf(GOT_COMMIT_SEP_STR);
3271 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3272 printf("%s", line);
3273 if (ferror(patch_file))
3274 return got_error_from_errno("getline");
3275 printf(GOT_COMMIT_SEP_STR);
3276 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3277 path, n, nchanges, action);
3278 break;
3279 default:
3280 return got_error_path(path, GOT_ERR_FILE_STATUS);
3283 return NULL;
3286 static const struct got_error *
3287 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3288 FILE *patch_file, int n, int nchanges)
3290 const struct got_error *err = NULL;
3291 char *line = NULL;
3292 size_t linesize = 0;
3293 ssize_t linelen;
3294 int resp = ' ';
3295 struct choose_patch_arg *a = arg;
3297 *choice = GOT_PATCH_CHOICE_NONE;
3299 if (a->patch_script_file) {
3300 char *nl;
3301 err = show_change(status, path, patch_file, n, nchanges,
3302 a->action);
3303 if (err)
3304 return err;
3305 linelen = getline(&line, &linesize, a->patch_script_file);
3306 if (linelen == -1) {
3307 if (ferror(a->patch_script_file))
3308 return got_error_from_errno("getline");
3309 return NULL;
3311 nl = strchr(line, '\n');
3312 if (nl)
3313 *nl = '\0';
3314 if (strcmp(line, "y") == 0) {
3315 *choice = GOT_PATCH_CHOICE_YES;
3316 printf("y\n");
3317 } else if (strcmp(line, "n") == 0) {
3318 *choice = GOT_PATCH_CHOICE_NO;
3319 printf("n\n");
3320 } else if (strcmp(line, "q") == 0 &&
3321 status == GOT_STATUS_MODIFY) {
3322 *choice = GOT_PATCH_CHOICE_QUIT;
3323 printf("q\n");
3324 } else
3325 printf("invalid response '%s'\n", line);
3326 free(line);
3327 return NULL;
3330 while (resp != 'y' && resp != 'n' && resp != 'q') {
3331 err = show_change(status, path, patch_file, n, nchanges,
3332 a->action);
3333 if (err)
3334 return err;
3335 resp = getchar();
3336 if (resp == '\n')
3337 resp = getchar();
3338 if (status == GOT_STATUS_MODIFY) {
3339 if (resp != 'y' && resp != 'n' && resp != 'q') {
3340 printf("invalid response '%c'\n", resp);
3341 resp = ' ';
3343 } else if (resp != 'y' && resp != 'n') {
3344 printf("invalid response '%c'\n", resp);
3345 resp = ' ';
3349 if (resp == 'y')
3350 *choice = GOT_PATCH_CHOICE_YES;
3351 else if (resp == 'n')
3352 *choice = GOT_PATCH_CHOICE_NO;
3353 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3354 *choice = GOT_PATCH_CHOICE_QUIT;
3356 return NULL;
3360 static const struct got_error *
3361 cmd_revert(int argc, char *argv[])
3363 const struct got_error *error = NULL;
3364 struct got_worktree *worktree = NULL;
3365 struct got_repository *repo = NULL;
3366 char *cwd = NULL, *path = NULL;
3367 struct got_pathlist_head paths;
3368 struct got_pathlist_entry *pe;
3369 int ch, can_recurse = 0, pflag = 0;
3370 FILE *patch_script_file = NULL;
3371 const char *patch_script_path = NULL;
3372 struct choose_patch_arg cpa;
3374 TAILQ_INIT(&paths);
3376 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3377 switch (ch) {
3378 case 'p':
3379 pflag = 1;
3380 break;
3381 case 'F':
3382 patch_script_path = optarg;
3383 break;
3384 case 'R':
3385 can_recurse = 1;
3386 break;
3387 default:
3388 usage_revert();
3389 /* NOTREACHED */
3393 argc -= optind;
3394 argv += optind;
3396 #ifndef PROFILE
3397 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3398 "unveil", NULL) == -1)
3399 err(1, "pledge");
3400 #endif
3401 if (argc < 1)
3402 usage_revert();
3403 if (patch_script_path && !pflag)
3404 errx(1, "-F option can only be used together with -p option");
3406 cwd = getcwd(NULL, 0);
3407 if (cwd == NULL) {
3408 error = got_error_from_errno("getcwd");
3409 goto done;
3411 error = got_worktree_open(&worktree, cwd);
3412 if (error)
3413 goto done;
3415 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3416 if (error != NULL)
3417 goto done;
3419 if (patch_script_path) {
3420 patch_script_file = fopen(patch_script_path, "r");
3421 if (patch_script_file == NULL) {
3422 error = got_error_from_errno2("fopen",
3423 patch_script_path);
3424 goto done;
3427 error = apply_unveil(got_repo_get_path(repo), 1,
3428 got_worktree_get_root_path(worktree));
3429 if (error)
3430 goto done;
3432 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3433 if (error)
3434 goto done;
3436 if (!can_recurse) {
3437 char *ondisk_path;
3438 struct stat sb;
3439 TAILQ_FOREACH(pe, &paths, entry) {
3440 if (asprintf(&ondisk_path, "%s/%s",
3441 got_worktree_get_root_path(worktree),
3442 pe->path) == -1) {
3443 error = got_error_from_errno("asprintf");
3444 goto done;
3446 if (lstat(ondisk_path, &sb) == -1) {
3447 if (errno == ENOENT) {
3448 free(ondisk_path);
3449 continue;
3451 error = got_error_from_errno2("lstat",
3452 ondisk_path);
3453 free(ondisk_path);
3454 goto done;
3456 free(ondisk_path);
3457 if (S_ISDIR(sb.st_mode)) {
3458 error = got_error_msg(GOT_ERR_BAD_PATH,
3459 "reverting directories requires -R option");
3460 goto done;
3465 cpa.patch_script_file = patch_script_file;
3466 cpa.action = "revert";
3467 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3468 pflag ? choose_patch : NULL, &cpa, repo);
3469 if (error)
3470 goto done;
3471 done:
3472 if (patch_script_file && fclose(patch_script_file) == EOF &&
3473 error == NULL)
3474 error = got_error_from_errno2("fclose", patch_script_path);
3475 if (repo)
3476 got_repo_close(repo);
3477 if (worktree)
3478 got_worktree_close(worktree);
3479 free(path);
3480 free(cwd);
3481 return error;
3484 __dead static void
3485 usage_commit(void)
3487 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3488 getprogname());
3489 exit(1);
3492 struct collect_commit_logmsg_arg {
3493 const char *cmdline_log;
3494 const char *editor;
3495 const char *worktree_path;
3496 const char *branch_name;
3497 const char *repo_path;
3498 char *logmsg_path;
3502 static const struct got_error *
3503 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3504 void *arg)
3506 char *initial_content = NULL;
3507 struct got_pathlist_entry *pe;
3508 const struct got_error *err = NULL;
3509 char *template = NULL;
3510 struct collect_commit_logmsg_arg *a = arg;
3511 int fd;
3512 size_t len;
3514 /* if a message was specified on the command line, just use it */
3515 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3516 len = strlen(a->cmdline_log) + 1;
3517 *logmsg = malloc(len + 1);
3518 if (*logmsg == NULL)
3519 return got_error_from_errno("malloc");
3520 strlcpy(*logmsg, a->cmdline_log, len);
3521 return NULL;
3524 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3525 return got_error_from_errno("asprintf");
3527 if (asprintf(&initial_content,
3528 "\n# changes to be committed on branch %s:\n",
3529 a->branch_name) == -1)
3530 return got_error_from_errno("asprintf");
3532 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3533 if (err)
3534 goto done;
3536 dprintf(fd, initial_content);
3538 TAILQ_FOREACH(pe, commitable_paths, entry) {
3539 struct got_commitable *ct = pe->data;
3540 dprintf(fd, "# %c %s\n",
3541 got_commitable_get_status(ct),
3542 got_commitable_get_path(ct));
3544 close(fd);
3546 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3547 done:
3548 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3549 unlink(a->logmsg_path);
3550 free(a->logmsg_path);
3551 a->logmsg_path = NULL;
3553 free(initial_content);
3554 free(template);
3556 /* Editor is done; we can now apply unveil(2) */
3557 if (err == NULL) {
3558 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3559 if (err) {
3560 free(*logmsg);
3561 *logmsg = NULL;
3564 return err;
3567 static const struct got_error *
3568 cmd_commit(int argc, char *argv[])
3570 const struct got_error *error = NULL;
3571 struct got_worktree *worktree = NULL;
3572 struct got_repository *repo = NULL;
3573 char *cwd = NULL, *id_str = NULL;
3574 struct got_object_id *id = NULL;
3575 const char *logmsg = NULL;
3576 const char *author;
3577 struct collect_commit_logmsg_arg cl_arg;
3578 char *editor = NULL;
3579 int ch, rebase_in_progress, histedit_in_progress;
3580 struct got_pathlist_head paths;
3582 TAILQ_INIT(&paths);
3583 cl_arg.logmsg_path = NULL;
3585 while ((ch = getopt(argc, argv, "m:")) != -1) {
3586 switch (ch) {
3587 case 'm':
3588 logmsg = optarg;
3589 break;
3590 default:
3591 usage_commit();
3592 /* NOTREACHED */
3596 argc -= optind;
3597 argv += optind;
3599 #ifndef PROFILE
3600 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3601 "unveil", NULL) == -1)
3602 err(1, "pledge");
3603 #endif
3604 error = get_author(&author);
3605 if (error)
3606 return error;
3608 cwd = getcwd(NULL, 0);
3609 if (cwd == NULL) {
3610 error = got_error_from_errno("getcwd");
3611 goto done;
3613 error = got_worktree_open(&worktree, cwd);
3614 if (error)
3615 goto done;
3617 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3618 if (error)
3619 goto done;
3620 if (rebase_in_progress) {
3621 error = got_error(GOT_ERR_REBASING);
3622 goto done;
3625 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3626 worktree);
3627 if (error)
3628 goto done;
3630 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3631 if (error != NULL)
3632 goto done;
3635 * unveil(2) traverses exec(2); if an editor is used we have
3636 * to apply unveil after the log message has been written.
3638 if (logmsg == NULL || strlen(logmsg) == 0)
3639 error = get_editor(&editor);
3640 else
3641 error = apply_unveil(got_repo_get_path(repo), 0,
3642 got_worktree_get_root_path(worktree));
3643 if (error)
3644 goto done;
3646 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3647 if (error)
3648 goto done;
3650 cl_arg.editor = editor;
3651 cl_arg.cmdline_log = logmsg;
3652 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3653 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3654 if (!histedit_in_progress) {
3655 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3656 error = got_error(GOT_ERR_COMMIT_BRANCH);
3657 goto done;
3659 cl_arg.branch_name += 11;
3661 cl_arg.repo_path = got_repo_get_path(repo);
3662 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3663 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3664 if (error) {
3665 if (cl_arg.logmsg_path)
3666 fprintf(stderr, "%s: log message preserved in %s\n",
3667 getprogname(), cl_arg.logmsg_path);
3668 goto done;
3671 if (cl_arg.logmsg_path)
3672 unlink(cl_arg.logmsg_path);
3674 error = got_object_id_str(&id_str, id);
3675 if (error)
3676 goto done;
3677 printf("Created commit %s\n", id_str);
3678 done:
3679 free(cl_arg.logmsg_path);
3680 if (repo)
3681 got_repo_close(repo);
3682 if (worktree)
3683 got_worktree_close(worktree);
3684 free(cwd);
3685 free(id_str);
3686 free(editor);
3687 return error;
3690 __dead static void
3691 usage_cherrypick(void)
3693 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3694 exit(1);
3697 static const struct got_error *
3698 cmd_cherrypick(int argc, char *argv[])
3700 const struct got_error *error = NULL;
3701 struct got_worktree *worktree = NULL;
3702 struct got_repository *repo = NULL;
3703 char *cwd = NULL, *commit_id_str = NULL;
3704 struct got_object_id *commit_id = NULL;
3705 struct got_commit_object *commit = NULL;
3706 struct got_object_qid *pid;
3707 struct got_reference *head_ref = NULL;
3708 int ch, did_something = 0;
3710 while ((ch = getopt(argc, argv, "")) != -1) {
3711 switch (ch) {
3712 default:
3713 usage_cherrypick();
3714 /* NOTREACHED */
3718 argc -= optind;
3719 argv += optind;
3721 #ifndef PROFILE
3722 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3723 "unveil", NULL) == -1)
3724 err(1, "pledge");
3725 #endif
3726 if (argc != 1)
3727 usage_cherrypick();
3729 cwd = getcwd(NULL, 0);
3730 if (cwd == NULL) {
3731 error = got_error_from_errno("getcwd");
3732 goto done;
3734 error = got_worktree_open(&worktree, cwd);
3735 if (error)
3736 goto done;
3738 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3739 if (error != NULL)
3740 goto done;
3742 error = apply_unveil(got_repo_get_path(repo), 0,
3743 got_worktree_get_root_path(worktree));
3744 if (error)
3745 goto done;
3747 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3748 GOT_OBJ_TYPE_COMMIT, repo);
3749 if (error != NULL) {
3750 struct got_reference *ref;
3751 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3752 goto done;
3753 error = got_ref_open(&ref, repo, argv[0], 0);
3754 if (error != NULL)
3755 goto done;
3756 error = got_ref_resolve(&commit_id, repo, ref);
3757 got_ref_close(ref);
3758 if (error != NULL)
3759 goto done;
3761 error = got_object_id_str(&commit_id_str, commit_id);
3762 if (error)
3763 goto done;
3765 error = got_ref_open(&head_ref, repo,
3766 got_worktree_get_head_ref_name(worktree), 0);
3767 if (error != NULL)
3768 goto done;
3770 error = check_same_branch(commit_id, head_ref, NULL, repo);
3771 if (error) {
3772 if (error->code != GOT_ERR_ANCESTRY)
3773 goto done;
3774 error = NULL;
3775 } else {
3776 error = got_error(GOT_ERR_SAME_BRANCH);
3777 goto done;
3780 error = got_object_open_as_commit(&commit, repo, commit_id);
3781 if (error)
3782 goto done;
3783 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3784 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3785 commit_id, repo, update_progress, &did_something, check_cancelled,
3786 NULL);
3787 if (error != NULL)
3788 goto done;
3790 if (did_something)
3791 printf("Merged commit %s\n", commit_id_str);
3792 done:
3793 if (commit)
3794 got_object_commit_close(commit);
3795 free(commit_id_str);
3796 if (head_ref)
3797 got_ref_close(head_ref);
3798 if (worktree)
3799 got_worktree_close(worktree);
3800 if (repo)
3801 got_repo_close(repo);
3802 return error;
3805 __dead static void
3806 usage_backout(void)
3808 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3809 exit(1);
3812 static const struct got_error *
3813 cmd_backout(int argc, char *argv[])
3815 const struct got_error *error = NULL;
3816 struct got_worktree *worktree = NULL;
3817 struct got_repository *repo = NULL;
3818 char *cwd = NULL, *commit_id_str = NULL;
3819 struct got_object_id *commit_id = NULL;
3820 struct got_commit_object *commit = NULL;
3821 struct got_object_qid *pid;
3822 struct got_reference *head_ref = NULL;
3823 int ch, did_something = 0;
3825 while ((ch = getopt(argc, argv, "")) != -1) {
3826 switch (ch) {
3827 default:
3828 usage_backout();
3829 /* NOTREACHED */
3833 argc -= optind;
3834 argv += optind;
3836 #ifndef PROFILE
3837 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3838 "unveil", NULL) == -1)
3839 err(1, "pledge");
3840 #endif
3841 if (argc != 1)
3842 usage_backout();
3844 cwd = getcwd(NULL, 0);
3845 if (cwd == NULL) {
3846 error = got_error_from_errno("getcwd");
3847 goto done;
3849 error = got_worktree_open(&worktree, cwd);
3850 if (error)
3851 goto done;
3853 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3854 if (error != NULL)
3855 goto done;
3857 error = apply_unveil(got_repo_get_path(repo), 0,
3858 got_worktree_get_root_path(worktree));
3859 if (error)
3860 goto done;
3862 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3863 GOT_OBJ_TYPE_COMMIT, repo);
3864 if (error != NULL) {
3865 struct got_reference *ref;
3866 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3867 goto done;
3868 error = got_ref_open(&ref, repo, argv[0], 0);
3869 if (error != NULL)
3870 goto done;
3871 error = got_ref_resolve(&commit_id, repo, ref);
3872 got_ref_close(ref);
3873 if (error != NULL)
3874 goto done;
3876 error = got_object_id_str(&commit_id_str, commit_id);
3877 if (error)
3878 goto done;
3880 error = got_ref_open(&head_ref, repo,
3881 got_worktree_get_head_ref_name(worktree), 0);
3882 if (error != NULL)
3883 goto done;
3885 error = check_same_branch(commit_id, head_ref, NULL, repo);
3886 if (error)
3887 goto done;
3889 error = got_object_open_as_commit(&commit, repo, commit_id);
3890 if (error)
3891 goto done;
3892 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3893 if (pid == NULL) {
3894 error = got_error(GOT_ERR_ROOT_COMMIT);
3895 goto done;
3898 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3899 update_progress, &did_something, check_cancelled, NULL);
3900 if (error != NULL)
3901 goto done;
3903 if (did_something)
3904 printf("Backed out commit %s\n", commit_id_str);
3905 done:
3906 if (commit)
3907 got_object_commit_close(commit);
3908 free(commit_id_str);
3909 if (head_ref)
3910 got_ref_close(head_ref);
3911 if (worktree)
3912 got_worktree_close(worktree);
3913 if (repo)
3914 got_repo_close(repo);
3915 return error;
3918 __dead static void
3919 usage_rebase(void)
3921 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3922 getprogname());
3923 exit(1);
3926 void
3927 trim_logmsg(char *logmsg, int limit)
3929 char *nl;
3930 size_t len;
3932 len = strlen(logmsg);
3933 if (len > limit)
3934 len = limit;
3935 logmsg[len] = '\0';
3936 nl = strchr(logmsg, '\n');
3937 if (nl)
3938 *nl = '\0';
3941 static const struct got_error *
3942 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3944 const struct got_error *err;
3945 char *logmsg0 = NULL;
3946 const char *s;
3948 err = got_object_commit_get_logmsg(&logmsg0, commit);
3949 if (err)
3950 return err;
3952 s = logmsg0;
3953 while (isspace((unsigned char)s[0]))
3954 s++;
3956 *logmsg = strdup(s);
3957 if (*logmsg == NULL) {
3958 err = got_error_from_errno("strdup");
3959 goto done;
3962 trim_logmsg(*logmsg, limit);
3963 done:
3964 free(logmsg0);
3965 return err;
3968 static const struct got_error *
3969 show_rebase_progress(struct got_commit_object *commit,
3970 struct got_object_id *old_id, struct got_object_id *new_id)
3972 const struct got_error *err;
3973 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3975 err = got_object_id_str(&old_id_str, old_id);
3976 if (err)
3977 goto done;
3979 if (new_id) {
3980 err = got_object_id_str(&new_id_str, new_id);
3981 if (err)
3982 goto done;
3985 old_id_str[12] = '\0';
3986 if (new_id_str)
3987 new_id_str[12] = '\0';
3989 err = get_short_logmsg(&logmsg, 42, commit);
3990 if (err)
3991 goto done;
3993 printf("%s -> %s: %s\n", old_id_str,
3994 new_id_str ? new_id_str : "no-op change", logmsg);
3995 done:
3996 free(old_id_str);
3997 free(new_id_str);
3998 return err;
4001 static const struct got_error *
4002 rebase_progress(void *arg, unsigned char status, const char *path)
4004 unsigned char *rebase_status = arg;
4006 while (path[0] == '/')
4007 path++;
4008 printf("%c %s\n", status, path);
4010 if (*rebase_status == GOT_STATUS_CONFLICT)
4011 return NULL;
4012 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4013 *rebase_status = status;
4014 return NULL;
4017 static const struct got_error *
4018 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4019 struct got_reference *branch, struct got_reference *new_base_branch,
4020 struct got_reference *tmp_branch, struct got_repository *repo)
4022 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4023 return got_worktree_rebase_complete(worktree, fileindex,
4024 new_base_branch, tmp_branch, branch, repo);
4027 static const struct got_error *
4028 rebase_commit(struct got_pathlist_head *merged_paths,
4029 struct got_worktree *worktree, struct got_fileindex *fileindex,
4030 struct got_reference *tmp_branch,
4031 struct got_object_id *commit_id, struct got_repository *repo)
4033 const struct got_error *error;
4034 struct got_commit_object *commit;
4035 struct got_object_id *new_commit_id;
4037 error = got_object_open_as_commit(&commit, repo, commit_id);
4038 if (error)
4039 return error;
4041 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4042 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4043 if (error) {
4044 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4045 goto done;
4046 error = show_rebase_progress(commit, commit_id, NULL);
4047 } else {
4048 error = show_rebase_progress(commit, commit_id, new_commit_id);
4049 free(new_commit_id);
4051 done:
4052 got_object_commit_close(commit);
4053 return error;
4056 struct check_path_prefix_arg {
4057 const char *path_prefix;
4058 size_t len;
4059 int errcode;
4062 static const struct got_error *
4063 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4064 struct got_blob_object *blob2, struct got_object_id *id1,
4065 struct got_object_id *id2, const char *path1, const char *path2,
4066 struct got_repository *repo)
4068 struct check_path_prefix_arg *a = arg;
4070 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4071 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4072 return got_error(a->errcode);
4074 return NULL;
4077 static const struct got_error *
4078 check_path_prefix(struct got_object_id *parent_id,
4079 struct got_object_id *commit_id, const char *path_prefix,
4080 int errcode, struct got_repository *repo)
4082 const struct got_error *err;
4083 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4084 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4085 struct check_path_prefix_arg cpp_arg;
4087 if (got_path_is_root_dir(path_prefix))
4088 return NULL;
4090 err = got_object_open_as_commit(&commit, repo, commit_id);
4091 if (err)
4092 goto done;
4094 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4095 if (err)
4096 goto done;
4098 err = got_object_open_as_tree(&tree1, repo,
4099 got_object_commit_get_tree_id(parent_commit));
4100 if (err)
4101 goto done;
4103 err = got_object_open_as_tree(&tree2, repo,
4104 got_object_commit_get_tree_id(commit));
4105 if (err)
4106 goto done;
4108 cpp_arg.path_prefix = path_prefix;
4109 while (cpp_arg.path_prefix[0] == '/')
4110 cpp_arg.path_prefix++;
4111 cpp_arg.len = strlen(cpp_arg.path_prefix);
4112 cpp_arg.errcode = errcode;
4113 err = got_diff_tree(tree1, tree2, "", "", repo,
4114 check_path_prefix_in_diff, &cpp_arg, 0);
4115 done:
4116 if (tree1)
4117 got_object_tree_close(tree1);
4118 if (tree2)
4119 got_object_tree_close(tree2);
4120 if (commit)
4121 got_object_commit_close(commit);
4122 if (parent_commit)
4123 got_object_commit_close(parent_commit);
4124 return err;
4127 static const struct got_error *
4128 collect_commits(struct got_object_id_queue *commits,
4129 struct got_object_id *initial_commit_id,
4130 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4131 const char *path_prefix, int path_prefix_errcode,
4132 struct got_repository *repo)
4134 const struct got_error *err = NULL;
4135 struct got_commit_graph *graph = NULL;
4136 struct got_object_id *parent_id = NULL;
4137 struct got_object_qid *qid;
4138 struct got_object_id *commit_id = initial_commit_id;
4140 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4141 if (err)
4142 return err;
4144 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4145 if (err)
4146 goto done;
4147 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4148 err = got_commit_graph_iter_next(&parent_id, graph);
4149 if (err) {
4150 if (err->code == GOT_ERR_ITER_COMPLETED) {
4151 err = got_error_msg(GOT_ERR_ANCESTRY,
4152 "ran out of commits to rebase before "
4153 "youngest common ancestor commit has "
4154 "been reached?!?");
4155 goto done;
4156 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4157 goto done;
4158 err = got_commit_graph_fetch_commits(graph, 1, repo);
4159 if (err)
4160 goto done;
4161 } else {
4162 err = check_path_prefix(parent_id, commit_id,
4163 path_prefix, path_prefix_errcode, repo);
4164 if (err)
4165 goto done;
4167 err = got_object_qid_alloc(&qid, commit_id);
4168 if (err)
4169 goto done;
4170 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4171 commit_id = parent_id;
4174 done:
4175 got_commit_graph_close(graph);
4176 return err;
4179 static const struct got_error *
4180 cmd_rebase(int argc, char *argv[])
4182 const struct got_error *error = NULL;
4183 struct got_worktree *worktree = NULL;
4184 struct got_repository *repo = NULL;
4185 struct got_fileindex *fileindex = NULL;
4186 char *cwd = NULL;
4187 struct got_reference *branch = NULL;
4188 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4189 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4190 struct got_object_id *resume_commit_id = NULL;
4191 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4192 struct got_commit_object *commit = NULL;
4193 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4194 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4195 struct got_object_id_queue commits;
4196 struct got_pathlist_head merged_paths;
4197 const struct got_object_id_queue *parent_ids;
4198 struct got_object_qid *qid, *pid;
4200 SIMPLEQ_INIT(&commits);
4201 TAILQ_INIT(&merged_paths);
4203 while ((ch = getopt(argc, argv, "ac")) != -1) {
4204 switch (ch) {
4205 case 'a':
4206 abort_rebase = 1;
4207 break;
4208 case 'c':
4209 continue_rebase = 1;
4210 break;
4211 default:
4212 usage_rebase();
4213 /* NOTREACHED */
4217 argc -= optind;
4218 argv += optind;
4220 #ifndef PROFILE
4221 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4222 "unveil", NULL) == -1)
4223 err(1, "pledge");
4224 #endif
4225 if (abort_rebase && continue_rebase)
4226 usage_rebase();
4227 else if (abort_rebase || continue_rebase) {
4228 if (argc != 0)
4229 usage_rebase();
4230 } else if (argc != 1)
4231 usage_rebase();
4233 cwd = getcwd(NULL, 0);
4234 if (cwd == NULL) {
4235 error = got_error_from_errno("getcwd");
4236 goto done;
4238 error = got_worktree_open(&worktree, cwd);
4239 if (error)
4240 goto done;
4242 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4243 if (error != NULL)
4244 goto done;
4246 error = apply_unveil(got_repo_get_path(repo), 0,
4247 got_worktree_get_root_path(worktree));
4248 if (error)
4249 goto done;
4251 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4252 if (error)
4253 goto done;
4255 if (abort_rebase) {
4256 int did_something;
4257 if (!rebase_in_progress) {
4258 error = got_error(GOT_ERR_NOT_REBASING);
4259 goto done;
4261 error = got_worktree_rebase_continue(&resume_commit_id,
4262 &new_base_branch, &tmp_branch, &branch, &fileindex,
4263 worktree, repo);
4264 if (error)
4265 goto done;
4266 printf("Switching work tree to %s\n",
4267 got_ref_get_symref_target(new_base_branch));
4268 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4269 new_base_branch, update_progress, &did_something);
4270 if (error)
4271 goto done;
4272 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4273 goto done; /* nothing else to do */
4276 if (continue_rebase) {
4277 if (!rebase_in_progress) {
4278 error = got_error(GOT_ERR_NOT_REBASING);
4279 goto done;
4281 error = got_worktree_rebase_continue(&resume_commit_id,
4282 &new_base_branch, &tmp_branch, &branch, &fileindex,
4283 worktree, repo);
4284 if (error)
4285 goto done;
4287 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4288 resume_commit_id, repo);
4289 if (error)
4290 goto done;
4292 yca_id = got_object_id_dup(resume_commit_id);
4293 if (yca_id == NULL) {
4294 error = got_error_from_errno("got_object_id_dup");
4295 goto done;
4297 } else {
4298 error = got_ref_open(&branch, repo, argv[0], 0);
4299 if (error != NULL)
4300 goto done;
4303 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4304 if (error)
4305 goto done;
4307 if (!continue_rebase) {
4308 struct got_object_id *base_commit_id;
4310 base_commit_id = got_worktree_get_base_commit_id(worktree);
4311 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4312 base_commit_id, branch_head_commit_id, repo);
4313 if (error)
4314 goto done;
4315 if (yca_id == NULL) {
4316 error = got_error_msg(GOT_ERR_ANCESTRY,
4317 "specified branch shares no common ancestry "
4318 "with work tree's branch");
4319 goto done;
4322 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4323 if (error) {
4324 if (error->code != GOT_ERR_ANCESTRY)
4325 goto done;
4326 error = NULL;
4327 } else {
4328 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4329 "specified branch resolves to a commit which "
4330 "is already contained in work tree's branch");
4331 goto done;
4333 error = got_worktree_rebase_prepare(&new_base_branch,
4334 &tmp_branch, &fileindex, worktree, branch, repo);
4335 if (error)
4336 goto done;
4339 commit_id = branch_head_commit_id;
4340 error = got_object_open_as_commit(&commit, repo, commit_id);
4341 if (error)
4342 goto done;
4344 parent_ids = got_object_commit_get_parent_ids(commit);
4345 pid = SIMPLEQ_FIRST(parent_ids);
4346 if (pid == NULL) {
4347 if (!continue_rebase) {
4348 int did_something;
4349 error = got_worktree_rebase_abort(worktree, fileindex,
4350 repo, new_base_branch, update_progress,
4351 &did_something);
4352 if (error)
4353 goto done;
4354 printf("Rebase of %s aborted\n",
4355 got_ref_get_name(branch));
4357 error = got_error(GOT_ERR_EMPTY_REBASE);
4358 goto done;
4360 error = collect_commits(&commits, commit_id, pid->id,
4361 yca_id, got_worktree_get_path_prefix(worktree),
4362 GOT_ERR_REBASE_PATH, repo);
4363 got_object_commit_close(commit);
4364 commit = NULL;
4365 if (error)
4366 goto done;
4368 if (SIMPLEQ_EMPTY(&commits)) {
4369 if (continue_rebase)
4370 error = rebase_complete(worktree, fileindex,
4371 branch, new_base_branch, tmp_branch, repo);
4372 else
4373 error = got_error(GOT_ERR_EMPTY_REBASE);
4374 goto done;
4377 pid = NULL;
4378 SIMPLEQ_FOREACH(qid, &commits, entry) {
4379 commit_id = qid->id;
4380 parent_id = pid ? pid->id : yca_id;
4381 pid = qid;
4383 error = got_worktree_rebase_merge_files(&merged_paths,
4384 worktree, fileindex, parent_id, commit_id, repo,
4385 rebase_progress, &rebase_status, check_cancelled, NULL);
4386 if (error)
4387 goto done;
4389 if (rebase_status == GOT_STATUS_CONFLICT) {
4390 got_worktree_rebase_pathlist_free(&merged_paths);
4391 break;
4394 error = rebase_commit(&merged_paths, worktree, fileindex,
4395 tmp_branch, commit_id, repo);
4396 got_worktree_rebase_pathlist_free(&merged_paths);
4397 if (error)
4398 goto done;
4401 if (rebase_status == GOT_STATUS_CONFLICT) {
4402 error = got_worktree_rebase_postpone(worktree, fileindex);
4403 if (error)
4404 goto done;
4405 error = got_error_msg(GOT_ERR_CONFLICTS,
4406 "conflicts must be resolved before rebasing can continue");
4407 } else
4408 error = rebase_complete(worktree, fileindex, branch,
4409 new_base_branch, tmp_branch, repo);
4410 done:
4411 got_object_id_queue_free(&commits);
4412 free(branch_head_commit_id);
4413 free(resume_commit_id);
4414 free(yca_id);
4415 if (commit)
4416 got_object_commit_close(commit);
4417 if (branch)
4418 got_ref_close(branch);
4419 if (new_base_branch)
4420 got_ref_close(new_base_branch);
4421 if (tmp_branch)
4422 got_ref_close(tmp_branch);
4423 if (worktree)
4424 got_worktree_close(worktree);
4425 if (repo)
4426 got_repo_close(repo);
4427 return error;
4430 __dead static void
4431 usage_histedit(void)
4433 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4434 getprogname());
4435 exit(1);
4438 #define GOT_HISTEDIT_PICK 'p'
4439 #define GOT_HISTEDIT_EDIT 'e'
4440 #define GOT_HISTEDIT_FOLD 'f'
4441 #define GOT_HISTEDIT_DROP 'd'
4442 #define GOT_HISTEDIT_MESG 'm'
4444 static struct got_histedit_cmd {
4445 unsigned char code;
4446 const char *name;
4447 const char *desc;
4448 } got_histedit_cmds[] = {
4449 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4450 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4451 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4452 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4453 { GOT_HISTEDIT_MESG, "mesg",
4454 "single-line log message for commit above (open editor if empty)" },
4457 struct got_histedit_list_entry {
4458 TAILQ_ENTRY(got_histedit_list_entry) entry;
4459 struct got_object_id *commit_id;
4460 const struct got_histedit_cmd *cmd;
4461 char *logmsg;
4463 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4465 static const struct got_error *
4466 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4467 FILE *f, struct got_repository *repo)
4469 const struct got_error *err = NULL;
4470 char *logmsg = NULL, *id_str = NULL;
4471 struct got_commit_object *commit = NULL;
4472 int n;
4474 err = got_object_open_as_commit(&commit, repo, commit_id);
4475 if (err)
4476 goto done;
4478 err = get_short_logmsg(&logmsg, 34, commit);
4479 if (err)
4480 goto done;
4482 err = got_object_id_str(&id_str, commit_id);
4483 if (err)
4484 goto done;
4486 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4487 if (n < 0)
4488 err = got_ferror(f, GOT_ERR_IO);
4489 done:
4490 if (commit)
4491 got_object_commit_close(commit);
4492 free(id_str);
4493 free(logmsg);
4494 return err;
4497 static const struct got_error *
4498 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4499 struct got_repository *repo)
4501 const struct got_error *err = NULL;
4502 struct got_object_qid *qid;
4504 if (SIMPLEQ_EMPTY(commits))
4505 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4507 SIMPLEQ_FOREACH(qid, commits, entry) {
4508 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4509 f, repo);
4510 if (err)
4511 break;
4514 return err;
4517 static const struct got_error *
4518 write_cmd_list(FILE *f)
4520 const struct got_error *err = NULL;
4521 int n, i;
4523 n = fprintf(f, "# Available histedit commands:\n");
4524 if (n < 0)
4525 return got_ferror(f, GOT_ERR_IO);
4527 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4528 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4529 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4530 cmd->desc);
4531 if (n < 0) {
4532 err = got_ferror(f, GOT_ERR_IO);
4533 break;
4536 n = fprintf(f, "# Commits will be processed in order from top to "
4537 "bottom of this file.\n");
4538 if (n < 0)
4539 return got_ferror(f, GOT_ERR_IO);
4540 return err;
4543 static const struct got_error *
4544 histedit_syntax_error(int lineno)
4546 static char msg[42];
4547 int ret;
4549 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4550 lineno);
4551 if (ret == -1 || ret >= sizeof(msg))
4552 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4554 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4557 static const struct got_error *
4558 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4559 char *logmsg, struct got_repository *repo)
4561 const struct got_error *err;
4562 struct got_commit_object *folded_commit = NULL;
4563 char *id_str, *folded_logmsg = NULL;
4565 err = got_object_id_str(&id_str, hle->commit_id);
4566 if (err)
4567 return err;
4569 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4570 if (err)
4571 goto done;
4573 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4574 if (err)
4575 goto done;
4576 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4577 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4578 folded_logmsg) == -1) {
4579 err = got_error_from_errno("asprintf");
4580 goto done;
4582 done:
4583 if (folded_commit)
4584 got_object_commit_close(folded_commit);
4585 free(id_str);
4586 free(folded_logmsg);
4587 return err;
4590 static struct got_histedit_list_entry *
4591 get_folded_commits(struct got_histedit_list_entry *hle)
4593 struct got_histedit_list_entry *prev, *folded = NULL;
4595 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4596 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4597 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4598 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4599 folded = prev;
4600 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4603 return folded;
4606 static const struct got_error *
4607 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4608 struct got_repository *repo)
4610 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4611 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4612 const struct got_error *err = NULL;
4613 struct got_commit_object *commit = NULL;
4614 int fd;
4615 struct got_histedit_list_entry *folded = NULL;
4617 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4618 if (err)
4619 return err;
4621 folded = get_folded_commits(hle);
4622 if (folded) {
4623 while (folded != hle) {
4624 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4625 folded = TAILQ_NEXT(folded, entry);
4626 continue;
4628 err = append_folded_commit_msg(&new_msg, folded,
4629 logmsg, repo);
4630 if (err)
4631 goto done;
4632 free(logmsg);
4633 logmsg = new_msg;
4634 folded = TAILQ_NEXT(folded, entry);
4638 err = got_object_id_str(&id_str, hle->commit_id);
4639 if (err)
4640 goto done;
4641 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4642 if (err)
4643 goto done;
4644 if (asprintf(&new_msg,
4645 "%s\n# original log message of commit %s: %s",
4646 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4647 err = got_error_from_errno("asprintf");
4648 goto done;
4650 free(logmsg);
4651 logmsg = new_msg;
4653 err = got_object_id_str(&id_str, hle->commit_id);
4654 if (err)
4655 goto done;
4657 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4658 if (err)
4659 goto done;
4661 dprintf(fd, logmsg);
4662 close(fd);
4664 err = get_editor(&editor);
4665 if (err)
4666 goto done;
4668 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4669 if (err) {
4670 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4671 goto done;
4672 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4674 done:
4675 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4676 err = got_error_from_errno2("unlink", logmsg_path);
4677 free(logmsg_path);
4678 free(logmsg);
4679 free(orig_logmsg);
4680 free(editor);
4681 if (commit)
4682 got_object_commit_close(commit);
4683 return err;
4686 static const struct got_error *
4687 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4688 FILE *f, struct got_repository *repo)
4690 const struct got_error *err = NULL;
4691 char *line = NULL, *p, *end;
4692 size_t size;
4693 ssize_t len;
4694 int lineno = 0, i;
4695 const struct got_histedit_cmd *cmd;
4696 struct got_object_id *commit_id = NULL;
4697 struct got_histedit_list_entry *hle = NULL;
4699 for (;;) {
4700 len = getline(&line, &size, f);
4701 if (len == -1) {
4702 const struct got_error *getline_err;
4703 if (feof(f))
4704 break;
4705 getline_err = got_error_from_errno("getline");
4706 err = got_ferror(f, getline_err->code);
4707 break;
4709 lineno++;
4710 p = line;
4711 while (isspace((unsigned char)p[0]))
4712 p++;
4713 if (p[0] == '#' || p[0] == '\0') {
4714 free(line);
4715 line = NULL;
4716 continue;
4718 cmd = NULL;
4719 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4720 cmd = &got_histedit_cmds[i];
4721 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4722 isspace((unsigned char)p[strlen(cmd->name)])) {
4723 p += strlen(cmd->name);
4724 break;
4726 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4727 p++;
4728 break;
4731 if (i == nitems(got_histedit_cmds)) {
4732 err = histedit_syntax_error(lineno);
4733 break;
4735 while (isspace((unsigned char)p[0]))
4736 p++;
4737 if (cmd->code == GOT_HISTEDIT_MESG) {
4738 if (hle == NULL || hle->logmsg != NULL) {
4739 err = got_error(GOT_ERR_HISTEDIT_CMD);
4740 break;
4742 if (p[0] == '\0') {
4743 err = histedit_edit_logmsg(hle, repo);
4744 if (err)
4745 break;
4746 } else {
4747 hle->logmsg = strdup(p);
4748 if (hle->logmsg == NULL) {
4749 err = got_error_from_errno("strdup");
4750 break;
4753 free(line);
4754 line = NULL;
4755 continue;
4756 } else {
4757 end = p;
4758 while (end[0] && !isspace((unsigned char)end[0]))
4759 end++;
4760 *end = '\0';
4762 err = got_object_resolve_id_str(&commit_id, repo, p);
4763 if (err) {
4764 /* override error code */
4765 err = histedit_syntax_error(lineno);
4766 break;
4769 hle = malloc(sizeof(*hle));
4770 if (hle == NULL) {
4771 err = got_error_from_errno("malloc");
4772 break;
4774 hle->cmd = cmd;
4775 hle->commit_id = commit_id;
4776 hle->logmsg = NULL;
4777 commit_id = NULL;
4778 free(line);
4779 line = NULL;
4780 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4783 free(line);
4784 free(commit_id);
4785 return err;
4788 static const struct got_error *
4789 histedit_check_script(struct got_histedit_list *histedit_cmds,
4790 struct got_object_id_queue *commits, struct got_repository *repo)
4792 const struct got_error *err = NULL;
4793 struct got_object_qid *qid;
4794 struct got_histedit_list_entry *hle;
4795 static char msg[80];
4796 char *id_str;
4798 if (TAILQ_EMPTY(histedit_cmds))
4799 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4800 "histedit script contains no commands");
4801 if (SIMPLEQ_EMPTY(commits))
4802 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4804 SIMPLEQ_FOREACH(qid, commits, entry) {
4805 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4806 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4807 break;
4809 if (hle == NULL) {
4810 err = got_object_id_str(&id_str, qid->id);
4811 if (err)
4812 return err;
4813 snprintf(msg, sizeof(msg),
4814 "commit %s missing from histedit script", id_str);
4815 free(id_str);
4816 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4820 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4821 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4822 "last commit in histedit script cannot be folded");
4824 return NULL;
4827 static const struct got_error *
4828 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4829 const char *path, struct got_object_id_queue *commits,
4830 struct got_repository *repo)
4832 const struct got_error *err = NULL;
4833 char *editor;
4834 FILE *f = NULL;
4836 err = get_editor(&editor);
4837 if (err)
4838 return err;
4840 if (spawn_editor(editor, path) == -1) {
4841 err = got_error_from_errno("failed spawning editor");
4842 goto done;
4845 f = fopen(path, "r");
4846 if (f == NULL) {
4847 err = got_error_from_errno("fopen");
4848 goto done;
4850 err = histedit_parse_list(histedit_cmds, f, repo);
4851 if (err)
4852 goto done;
4854 err = histedit_check_script(histedit_cmds, commits, repo);
4855 done:
4856 if (f && fclose(f) != 0 && err == NULL)
4857 err = got_error_from_errno("fclose");
4858 free(editor);
4859 return err;
4862 static const struct got_error *
4863 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4864 struct got_object_id_queue *, const char *, struct got_repository *);
4866 static const struct got_error *
4867 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4868 struct got_object_id_queue *commits, struct got_repository *repo)
4870 const struct got_error *err;
4871 FILE *f = NULL;
4872 char *path = NULL;
4874 err = got_opentemp_named(&path, &f, "got-histedit");
4875 if (err)
4876 return err;
4878 err = write_cmd_list(f);
4879 if (err)
4880 goto done;
4882 err = histedit_write_commit_list(commits, f, repo);
4883 if (err)
4884 goto done;
4886 if (fclose(f) != 0) {
4887 err = got_error_from_errno("fclose");
4888 goto done;
4890 f = NULL;
4892 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4893 if (err) {
4894 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4895 err->code != GOT_ERR_HISTEDIT_CMD)
4896 goto done;
4897 err = histedit_edit_list_retry(histedit_cmds, err,
4898 commits, path, repo);
4900 done:
4901 if (f && fclose(f) != 0 && err == NULL)
4902 err = got_error_from_errno("fclose");
4903 if (path && unlink(path) != 0 && err == NULL)
4904 err = got_error_from_errno2("unlink", path);
4905 free(path);
4906 return err;
4909 static const struct got_error *
4910 histedit_save_list(struct got_histedit_list *histedit_cmds,
4911 struct got_worktree *worktree, struct got_repository *repo)
4913 const struct got_error *err = NULL;
4914 char *path = NULL;
4915 FILE *f = NULL;
4916 struct got_histedit_list_entry *hle;
4917 struct got_commit_object *commit = NULL;
4919 err = got_worktree_get_histedit_script_path(&path, worktree);
4920 if (err)
4921 return err;
4923 f = fopen(path, "w");
4924 if (f == NULL) {
4925 err = got_error_from_errno2("fopen", path);
4926 goto done;
4928 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4929 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4930 repo);
4931 if (err)
4932 break;
4934 if (hle->logmsg) {
4935 int n = fprintf(f, "%c %s\n",
4936 GOT_HISTEDIT_MESG, hle->logmsg);
4937 if (n < 0) {
4938 err = got_ferror(f, GOT_ERR_IO);
4939 break;
4943 done:
4944 if (f && fclose(f) != 0 && err == NULL)
4945 err = got_error_from_errno("fclose");
4946 free(path);
4947 if (commit)
4948 got_object_commit_close(commit);
4949 return err;
4952 void
4953 histedit_free_list(struct got_histedit_list *histedit_cmds)
4955 struct got_histedit_list_entry *hle;
4957 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4958 TAILQ_REMOVE(histedit_cmds, hle, entry);
4959 free(hle);
4963 static const struct got_error *
4964 histedit_load_list(struct got_histedit_list *histedit_cmds,
4965 const char *path, struct got_repository *repo)
4967 const struct got_error *err = NULL;
4968 FILE *f = NULL;
4970 f = fopen(path, "r");
4971 if (f == NULL) {
4972 err = got_error_from_errno2("fopen", path);
4973 goto done;
4976 err = histedit_parse_list(histedit_cmds, f, repo);
4977 done:
4978 if (f && fclose(f) != 0 && err == NULL)
4979 err = got_error_from_errno("fclose");
4980 return err;
4983 static const struct got_error *
4984 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4985 const struct got_error *edit_err, struct got_object_id_queue *commits,
4986 const char *path, struct got_repository *repo)
4988 const struct got_error *err = NULL, *prev_err = edit_err;
4989 int resp = ' ';
4991 while (resp != 'c' && resp != 'r' && resp != 'a') {
4992 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4993 "or (a)bort: ", getprogname(), prev_err->msg);
4994 resp = getchar();
4995 if (resp == '\n')
4996 resp = getchar();
4997 if (resp == 'c') {
4998 histedit_free_list(histedit_cmds);
4999 err = histedit_run_editor(histedit_cmds, path, commits,
5000 repo);
5001 if (err) {
5002 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5003 err->code != GOT_ERR_HISTEDIT_CMD)
5004 break;
5005 prev_err = err;
5006 resp = ' ';
5007 continue;
5009 break;
5010 } else if (resp == 'r') {
5011 histedit_free_list(histedit_cmds);
5012 err = histedit_edit_script(histedit_cmds,
5013 commits, repo);
5014 if (err) {
5015 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5016 err->code != GOT_ERR_HISTEDIT_CMD)
5017 break;
5018 prev_err = err;
5019 resp = ' ';
5020 continue;
5022 break;
5023 } else if (resp == 'a') {
5024 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5025 break;
5026 } else
5027 printf("invalid response '%c'\n", resp);
5030 return err;
5033 static const struct got_error *
5034 histedit_complete(struct got_worktree *worktree,
5035 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5036 struct got_reference *branch, struct got_repository *repo)
5038 printf("Switching work tree to %s\n",
5039 got_ref_get_symref_target(branch));
5040 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5041 branch, repo);
5044 static const struct got_error *
5045 show_histedit_progress(struct got_commit_object *commit,
5046 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5048 const struct got_error *err;
5049 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5051 err = got_object_id_str(&old_id_str, hle->commit_id);
5052 if (err)
5053 goto done;
5055 if (new_id) {
5056 err = got_object_id_str(&new_id_str, new_id);
5057 if (err)
5058 goto done;
5061 old_id_str[12] = '\0';
5062 if (new_id_str)
5063 new_id_str[12] = '\0';
5065 if (hle->logmsg) {
5066 logmsg = strdup(hle->logmsg);
5067 if (logmsg == NULL) {
5068 err = got_error_from_errno("strdup");
5069 goto done;
5071 trim_logmsg(logmsg, 42);
5072 } else {
5073 err = get_short_logmsg(&logmsg, 42, commit);
5074 if (err)
5075 goto done;
5078 switch (hle->cmd->code) {
5079 case GOT_HISTEDIT_PICK:
5080 case GOT_HISTEDIT_EDIT:
5081 printf("%s -> %s: %s\n", old_id_str,
5082 new_id_str ? new_id_str : "no-op change", logmsg);
5083 break;
5084 case GOT_HISTEDIT_DROP:
5085 case GOT_HISTEDIT_FOLD:
5086 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5087 logmsg);
5088 break;
5089 default:
5090 break;
5093 done:
5094 free(old_id_str);
5095 free(new_id_str);
5096 return err;
5099 static const struct got_error *
5100 histedit_commit(struct got_pathlist_head *merged_paths,
5101 struct got_worktree *worktree, struct got_fileindex *fileindex,
5102 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5103 struct got_repository *repo)
5105 const struct got_error *err;
5106 struct got_commit_object *commit;
5107 struct got_object_id *new_commit_id;
5109 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5110 && hle->logmsg == NULL) {
5111 err = histedit_edit_logmsg(hle, repo);
5112 if (err)
5113 return err;
5116 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5117 if (err)
5118 return err;
5120 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5121 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5122 hle->logmsg, repo);
5123 if (err) {
5124 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5125 goto done;
5126 err = show_histedit_progress(commit, hle, NULL);
5127 } else {
5128 err = show_histedit_progress(commit, hle, new_commit_id);
5129 free(new_commit_id);
5131 done:
5132 got_object_commit_close(commit);
5133 return err;
5136 static const struct got_error *
5137 histedit_skip_commit(struct got_histedit_list_entry *hle,
5138 struct got_worktree *worktree, struct got_repository *repo)
5140 const struct got_error *error;
5141 struct got_commit_object *commit;
5143 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5144 repo);
5145 if (error)
5146 return error;
5148 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5149 if (error)
5150 return error;
5152 error = show_histedit_progress(commit, hle, NULL);
5153 got_object_commit_close(commit);
5154 return error;
5157 static const struct got_error *
5158 cmd_histedit(int argc, char *argv[])
5160 const struct got_error *error = NULL;
5161 struct got_worktree *worktree = NULL;
5162 struct got_fileindex *fileindex = NULL;
5163 struct got_repository *repo = NULL;
5164 char *cwd = NULL;
5165 struct got_reference *branch = NULL;
5166 struct got_reference *tmp_branch = NULL;
5167 struct got_object_id *resume_commit_id = NULL;
5168 struct got_object_id *base_commit_id = NULL;
5169 struct got_object_id *head_commit_id = NULL;
5170 struct got_commit_object *commit = NULL;
5171 int ch, rebase_in_progress = 0, did_something;
5172 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5173 const char *edit_script_path = NULL;
5174 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5175 struct got_object_id_queue commits;
5176 struct got_pathlist_head merged_paths;
5177 const struct got_object_id_queue *parent_ids;
5178 struct got_object_qid *pid;
5179 struct got_histedit_list histedit_cmds;
5180 struct got_histedit_list_entry *hle;
5182 SIMPLEQ_INIT(&commits);
5183 TAILQ_INIT(&histedit_cmds);
5184 TAILQ_INIT(&merged_paths);
5186 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5187 switch (ch) {
5188 case 'a':
5189 abort_edit = 1;
5190 break;
5191 case 'c':
5192 continue_edit = 1;
5193 break;
5194 case 'F':
5195 edit_script_path = optarg;
5196 break;
5197 default:
5198 usage_histedit();
5199 /* NOTREACHED */
5203 argc -= optind;
5204 argv += optind;
5206 #ifndef PROFILE
5207 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5208 "unveil", NULL) == -1)
5209 err(1, "pledge");
5210 #endif
5211 if (abort_edit && continue_edit)
5212 usage_histedit();
5213 if (argc != 0)
5214 usage_histedit();
5217 * This command cannot apply unveil(2) in all cases because the
5218 * user may choose to run an editor to edit the histedit script
5219 * and to edit individual commit log messages.
5220 * unveil(2) traverses exec(2); if an editor is used we have to
5221 * apply unveil after edit script and log messages have been written.
5222 * XXX TODO: Make use of unveil(2) where possible.
5225 cwd = getcwd(NULL, 0);
5226 if (cwd == NULL) {
5227 error = got_error_from_errno("getcwd");
5228 goto done;
5230 error = got_worktree_open(&worktree, cwd);
5231 if (error)
5232 goto done;
5234 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5235 if (error != NULL)
5236 goto done;
5238 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5239 if (error)
5240 goto done;
5241 if (rebase_in_progress) {
5242 error = got_error(GOT_ERR_REBASING);
5243 goto done;
5246 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5247 if (error)
5248 goto done;
5250 if (edit_in_progress && abort_edit) {
5251 error = got_worktree_histedit_continue(&resume_commit_id,
5252 &tmp_branch, &branch, &base_commit_id, &fileindex,
5253 worktree, repo);
5254 if (error)
5255 goto done;
5256 printf("Switching work tree to %s\n",
5257 got_ref_get_symref_target(branch));
5258 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5259 branch, base_commit_id, update_progress, &did_something);
5260 if (error)
5261 goto done;
5262 printf("Histedit of %s aborted\n",
5263 got_ref_get_symref_target(branch));
5264 goto done; /* nothing else to do */
5265 } else if (abort_edit) {
5266 error = got_error(GOT_ERR_NOT_HISTEDIT);
5267 goto done;
5270 if (continue_edit) {
5271 char *path;
5273 if (!edit_in_progress) {
5274 error = got_error(GOT_ERR_NOT_HISTEDIT);
5275 goto done;
5278 error = got_worktree_get_histedit_script_path(&path, worktree);
5279 if (error)
5280 goto done;
5282 error = histedit_load_list(&histedit_cmds, path, repo);
5283 free(path);
5284 if (error)
5285 goto done;
5287 error = got_worktree_histedit_continue(&resume_commit_id,
5288 &tmp_branch, &branch, &base_commit_id, &fileindex,
5289 worktree, repo);
5290 if (error)
5291 goto done;
5293 error = got_ref_resolve(&head_commit_id, repo, branch);
5294 if (error)
5295 goto done;
5297 error = got_object_open_as_commit(&commit, repo,
5298 head_commit_id);
5299 if (error)
5300 goto done;
5301 parent_ids = got_object_commit_get_parent_ids(commit);
5302 pid = SIMPLEQ_FIRST(parent_ids);
5303 if (pid == NULL) {
5304 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5305 goto done;
5307 error = collect_commits(&commits, head_commit_id, pid->id,
5308 base_commit_id, got_worktree_get_path_prefix(worktree),
5309 GOT_ERR_HISTEDIT_PATH, repo);
5310 got_object_commit_close(commit);
5311 commit = NULL;
5312 if (error)
5313 goto done;
5314 } else {
5315 if (edit_in_progress) {
5316 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5317 goto done;
5320 error = got_ref_open(&branch, repo,
5321 got_worktree_get_head_ref_name(worktree), 0);
5322 if (error != NULL)
5323 goto done;
5325 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5326 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5327 "will not edit commit history of a branch outside "
5328 "the \"refs/heads/\" reference namespace");
5329 goto done;
5332 error = got_ref_resolve(&head_commit_id, repo, branch);
5333 got_ref_close(branch);
5334 branch = NULL;
5335 if (error)
5336 goto done;
5338 error = got_object_open_as_commit(&commit, repo,
5339 head_commit_id);
5340 if (error)
5341 goto done;
5342 parent_ids = got_object_commit_get_parent_ids(commit);
5343 pid = SIMPLEQ_FIRST(parent_ids);
5344 if (pid == NULL) {
5345 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5346 goto done;
5348 error = collect_commits(&commits, head_commit_id, pid->id,
5349 got_worktree_get_base_commit_id(worktree),
5350 got_worktree_get_path_prefix(worktree),
5351 GOT_ERR_HISTEDIT_PATH, repo);
5352 got_object_commit_close(commit);
5353 commit = NULL;
5354 if (error)
5355 goto done;
5357 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5358 &base_commit_id, &fileindex, worktree, repo);
5359 if (error)
5360 goto done;
5362 if (edit_script_path) {
5363 error = histedit_load_list(&histedit_cmds,
5364 edit_script_path, repo);
5365 if (error) {
5366 got_worktree_histedit_abort(worktree, fileindex,
5367 repo, branch, base_commit_id,
5368 update_progress, &did_something);
5369 goto done;
5371 } else {
5372 error = histedit_edit_script(&histedit_cmds, &commits,
5373 repo);
5374 if (error) {
5375 got_worktree_histedit_abort(worktree, fileindex,
5376 repo, branch, base_commit_id,
5377 update_progress, &did_something);
5378 goto done;
5383 error = histedit_save_list(&histedit_cmds, worktree,
5384 repo);
5385 if (error) {
5386 got_worktree_histedit_abort(worktree, fileindex,
5387 repo, branch, base_commit_id,
5388 update_progress, &did_something);
5389 goto done;
5394 error = histedit_check_script(&histedit_cmds, &commits, repo);
5395 if (error)
5396 goto done;
5398 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5399 if (resume_commit_id) {
5400 if (got_object_id_cmp(hle->commit_id,
5401 resume_commit_id) != 0)
5402 continue;
5404 resume_commit_id = NULL;
5405 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5406 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5407 error = histedit_skip_commit(hle, worktree,
5408 repo);
5409 } else {
5410 error = histedit_commit(NULL, worktree,
5411 fileindex, tmp_branch, hle, repo);
5413 if (error)
5414 goto done;
5415 continue;
5418 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5419 error = histedit_skip_commit(hle, worktree, repo);
5420 if (error)
5421 goto done;
5422 continue;
5425 error = got_object_open_as_commit(&commit, repo,
5426 hle->commit_id);
5427 if (error)
5428 goto done;
5429 parent_ids = got_object_commit_get_parent_ids(commit);
5430 pid = SIMPLEQ_FIRST(parent_ids);
5432 error = got_worktree_histedit_merge_files(&merged_paths,
5433 worktree, fileindex, pid->id, hle->commit_id, repo,
5434 rebase_progress, &rebase_status, check_cancelled, NULL);
5435 if (error)
5436 goto done;
5437 got_object_commit_close(commit);
5438 commit = NULL;
5440 if (rebase_status == GOT_STATUS_CONFLICT) {
5441 got_worktree_rebase_pathlist_free(&merged_paths);
5442 break;
5445 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5446 char *id_str;
5447 error = got_object_id_str(&id_str, hle->commit_id);
5448 if (error)
5449 goto done;
5450 printf("Stopping histedit for amending commit %s\n",
5451 id_str);
5452 free(id_str);
5453 got_worktree_rebase_pathlist_free(&merged_paths);
5454 error = got_worktree_histedit_postpone(worktree,
5455 fileindex);
5456 goto done;
5459 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5460 error = histedit_skip_commit(hle, worktree, repo);
5461 if (error)
5462 goto done;
5463 continue;
5466 error = histedit_commit(&merged_paths, worktree, fileindex,
5467 tmp_branch, hle, repo);
5468 got_worktree_rebase_pathlist_free(&merged_paths);
5469 if (error)
5470 goto done;
5473 if (rebase_status == GOT_STATUS_CONFLICT) {
5474 error = got_worktree_histedit_postpone(worktree, fileindex);
5475 if (error)
5476 goto done;
5477 error = got_error_msg(GOT_ERR_CONFLICTS,
5478 "conflicts must be resolved before rebasing can continue");
5479 } else
5480 error = histedit_complete(worktree, fileindex, tmp_branch,
5481 branch, repo);
5482 done:
5483 got_object_id_queue_free(&commits);
5484 histedit_free_list(&histedit_cmds);
5485 free(head_commit_id);
5486 free(base_commit_id);
5487 free(resume_commit_id);
5488 if (commit)
5489 got_object_commit_close(commit);
5490 if (branch)
5491 got_ref_close(branch);
5492 if (tmp_branch)
5493 got_ref_close(tmp_branch);
5494 if (worktree)
5495 got_worktree_close(worktree);
5496 if (repo)
5497 got_repo_close(repo);
5498 return error;
5501 __dead static void
5502 usage_stage(void)
5504 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5505 "[file-path ...]\n",
5506 getprogname());
5507 exit(1);
5510 static const struct got_error *
5511 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5512 const char *path, struct got_object_id *blob_id,
5513 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5515 const struct got_error *err = NULL;
5516 char *id_str = NULL;
5518 if (staged_status != GOT_STATUS_ADD &&
5519 staged_status != GOT_STATUS_MODIFY &&
5520 staged_status != GOT_STATUS_DELETE)
5521 return NULL;
5523 if (staged_status == GOT_STATUS_ADD ||
5524 staged_status == GOT_STATUS_MODIFY)
5525 err = got_object_id_str(&id_str, staged_blob_id);
5526 else
5527 err = got_object_id_str(&id_str, blob_id);
5528 if (err)
5529 return err;
5531 printf("%s %c %s\n", id_str, staged_status, path);
5532 free(id_str);
5533 return NULL;
5536 static const struct got_error *
5537 cmd_stage(int argc, char *argv[])
5539 const struct got_error *error = NULL;
5540 struct got_repository *repo = NULL;
5541 struct got_worktree *worktree = NULL;
5542 char *cwd = NULL;
5543 struct got_pathlist_head paths;
5544 struct got_pathlist_entry *pe;
5545 int ch, list_stage = 0, pflag = 0;
5546 FILE *patch_script_file = NULL;
5547 const char *patch_script_path = NULL;
5548 struct choose_patch_arg cpa;
5550 TAILQ_INIT(&paths);
5552 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5553 switch (ch) {
5554 case 'l':
5555 list_stage = 1;
5556 break;
5557 case 'p':
5558 pflag = 1;
5559 break;
5560 case 'F':
5561 patch_script_path = optarg;
5562 break;
5563 default:
5564 usage_stage();
5565 /* NOTREACHED */
5569 argc -= optind;
5570 argv += optind;
5572 #ifndef PROFILE
5573 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5574 "unveil", NULL) == -1)
5575 err(1, "pledge");
5576 #endif
5577 if (list_stage && (pflag || patch_script_path))
5578 errx(1, "-l option cannot be used with other options");
5579 if (patch_script_path && !pflag)
5580 errx(1, "-F option can only be used together with -p option");
5582 cwd = getcwd(NULL, 0);
5583 if (cwd == NULL) {
5584 error = got_error_from_errno("getcwd");
5585 goto done;
5588 error = got_worktree_open(&worktree, cwd);
5589 if (error)
5590 goto done;
5592 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5593 if (error != NULL)
5594 goto done;
5596 if (patch_script_path) {
5597 patch_script_file = fopen(patch_script_path, "r");
5598 if (patch_script_file == NULL) {
5599 error = got_error_from_errno2("fopen",
5600 patch_script_path);
5601 goto done;
5604 error = apply_unveil(got_repo_get_path(repo), 1,
5605 got_worktree_get_root_path(worktree));
5606 if (error)
5607 goto done;
5609 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5610 if (error)
5611 goto done;
5613 if (list_stage)
5614 error = got_worktree_status(worktree, &paths, repo,
5615 print_stage, NULL, check_cancelled, NULL);
5616 else {
5617 cpa.patch_script_file = patch_script_file;
5618 cpa.action = "stage";
5619 error = got_worktree_stage(worktree, &paths,
5620 pflag ? NULL : print_status, NULL,
5621 pflag ? choose_patch : NULL, &cpa, repo);
5623 done:
5624 if (patch_script_file && fclose(patch_script_file) == EOF &&
5625 error == NULL)
5626 error = got_error_from_errno2("fclose", patch_script_path);
5627 if (repo)
5628 got_repo_close(repo);
5629 if (worktree)
5630 got_worktree_close(worktree);
5631 TAILQ_FOREACH(pe, &paths, entry)
5632 free((char *)pe->path);
5633 got_pathlist_free(&paths);
5634 free(cwd);
5635 return error;
5638 __dead static void
5639 usage_unstage(void)
5641 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5642 "[file-path ...]\n",
5643 getprogname());
5644 exit(1);
5648 static const struct got_error *
5649 cmd_unstage(int argc, char *argv[])
5651 const struct got_error *error = NULL;
5652 struct got_repository *repo = NULL;
5653 struct got_worktree *worktree = NULL;
5654 char *cwd = NULL;
5655 struct got_pathlist_head paths;
5656 struct got_pathlist_entry *pe;
5657 int ch, did_something = 0, pflag = 0;
5658 FILE *patch_script_file = NULL;
5659 const char *patch_script_path = NULL;
5660 struct choose_patch_arg cpa;
5662 TAILQ_INIT(&paths);
5664 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5665 switch (ch) {
5666 case 'p':
5667 pflag = 1;
5668 break;
5669 case 'F':
5670 patch_script_path = optarg;
5671 break;
5672 default:
5673 usage_unstage();
5674 /* NOTREACHED */
5678 argc -= optind;
5679 argv += optind;
5681 #ifndef PROFILE
5682 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5683 "unveil", NULL) == -1)
5684 err(1, "pledge");
5685 #endif
5686 if (patch_script_path && !pflag)
5687 errx(1, "-F option can only be used together with -p option");
5689 cwd = getcwd(NULL, 0);
5690 if (cwd == NULL) {
5691 error = got_error_from_errno("getcwd");
5692 goto done;
5695 error = got_worktree_open(&worktree, cwd);
5696 if (error)
5697 goto done;
5699 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5700 if (error != NULL)
5701 goto done;
5703 if (patch_script_path) {
5704 patch_script_file = fopen(patch_script_path, "r");
5705 if (patch_script_file == NULL) {
5706 error = got_error_from_errno2("fopen",
5707 patch_script_path);
5708 goto done;
5712 error = apply_unveil(got_repo_get_path(repo), 1,
5713 got_worktree_get_root_path(worktree));
5714 if (error)
5715 goto done;
5717 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5718 if (error)
5719 goto done;
5721 cpa.patch_script_file = patch_script_file;
5722 cpa.action = "unstage";
5723 error = got_worktree_unstage(worktree, &paths, update_progress,
5724 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5725 done:
5726 if (patch_script_file && fclose(patch_script_file) == EOF &&
5727 error == NULL)
5728 error = got_error_from_errno2("fclose", patch_script_path);
5729 if (repo)
5730 got_repo_close(repo);
5731 if (worktree)
5732 got_worktree_close(worktree);
5733 TAILQ_FOREACH(pe, &paths, entry)
5734 free((char *)pe->path);
5735 got_pathlist_free(&paths);
5736 free(cwd);
5737 return error;