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 int
1555 get_default_log_limit(void)
1557 const char *got_default_log_limit;
1558 long long n;
1559 const char *errstr;
1561 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1562 if (got_default_log_limit == NULL)
1563 return 0;
1564 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1565 if (errstr != NULL)
1566 return 0;
1567 return n;
1570 static const struct got_error *
1571 cmd_log(int argc, char *argv[])
1573 const struct got_error *error;
1574 struct got_repository *repo = NULL;
1575 struct got_worktree *worktree = NULL;
1576 struct got_commit_object *commit = NULL;
1577 struct got_object_id *id = NULL;
1578 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1579 char *start_commit = NULL;
1580 int diff_context = 3, ch;
1581 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1582 const char *errstr;
1583 struct got_reflist_head refs;
1585 SIMPLEQ_INIT(&refs);
1587 #ifndef PROFILE
1588 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1589 NULL)
1590 == -1)
1591 err(1, "pledge");
1592 #endif
1594 limit = get_default_log_limit();
1596 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1597 switch (ch) {
1598 case 'p':
1599 show_patch = 1;
1600 break;
1601 case 'c':
1602 start_commit = optarg;
1603 break;
1604 case 'C':
1605 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1606 &errstr);
1607 if (errstr != NULL)
1608 err(1, "-C option %s", errstr);
1609 break;
1610 case 'l':
1611 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1612 if (errstr != NULL)
1613 err(1, "-l option %s", errstr);
1614 break;
1615 case 'f':
1616 first_parent_traversal = 1;
1617 break;
1618 case 'r':
1619 repo_path = realpath(optarg, NULL);
1620 if (repo_path == NULL)
1621 err(1, "-r option");
1622 got_path_strip_trailing_slashes(repo_path);
1623 break;
1624 default:
1625 usage_log();
1626 /* NOTREACHED */
1630 argc -= optind;
1631 argv += optind;
1633 cwd = getcwd(NULL, 0);
1634 if (cwd == NULL) {
1635 error = got_error_from_errno("getcwd");
1636 goto done;
1639 error = got_worktree_open(&worktree, cwd);
1640 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1641 goto done;
1642 error = NULL;
1644 if (argc == 0) {
1645 path = strdup("");
1646 if (path == NULL) {
1647 error = got_error_from_errno("strdup");
1648 goto done;
1650 } else if (argc == 1) {
1651 if (worktree) {
1652 error = got_worktree_resolve_path(&path, worktree,
1653 argv[0]);
1654 if (error)
1655 goto done;
1656 } else {
1657 path = strdup(argv[0]);
1658 if (path == NULL) {
1659 error = got_error_from_errno("strdup");
1660 goto done;
1663 } else
1664 usage_log();
1666 if (repo_path == NULL) {
1667 repo_path = worktree ?
1668 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1670 if (repo_path == NULL) {
1671 error = got_error_from_errno("strdup");
1672 goto done;
1675 error = got_repo_open(&repo, repo_path);
1676 if (error != NULL)
1677 goto done;
1679 error = apply_unveil(got_repo_get_path(repo), 1,
1680 worktree ? got_worktree_get_root_path(worktree) : NULL);
1681 if (error)
1682 goto done;
1684 if (start_commit == NULL) {
1685 struct got_reference *head_ref;
1686 error = got_ref_open(&head_ref, repo,
1687 worktree ? got_worktree_get_head_ref_name(worktree)
1688 : GOT_REF_HEAD, 0);
1689 if (error != NULL)
1690 return error;
1691 error = got_ref_resolve(&id, repo, head_ref);
1692 got_ref_close(head_ref);
1693 if (error != NULL)
1694 return error;
1695 error = got_object_open_as_commit(&commit, repo, id);
1696 } else {
1697 struct got_reference *ref;
1698 error = got_ref_open(&ref, repo, start_commit, 0);
1699 if (error == NULL) {
1700 int obj_type;
1701 error = got_ref_resolve(&id, repo, ref);
1702 got_ref_close(ref);
1703 if (error != NULL)
1704 goto done;
1705 error = got_object_get_type(&obj_type, repo, id);
1706 if (error != NULL)
1707 goto done;
1708 if (obj_type == GOT_OBJ_TYPE_TAG) {
1709 struct got_tag_object *tag;
1710 error = got_object_open_as_tag(&tag, repo, id);
1711 if (error != NULL)
1712 goto done;
1713 if (got_object_tag_get_object_type(tag) !=
1714 GOT_OBJ_TYPE_COMMIT) {
1715 got_object_tag_close(tag);
1716 error = got_error(GOT_ERR_OBJ_TYPE);
1717 goto done;
1719 free(id);
1720 id = got_object_id_dup(
1721 got_object_tag_get_object_id(tag));
1722 if (id == NULL)
1723 error = got_error_from_errno(
1724 "got_object_id_dup");
1725 got_object_tag_close(tag);
1726 if (error)
1727 goto done;
1728 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1729 error = got_error(GOT_ERR_OBJ_TYPE);
1730 goto done;
1732 error = got_object_open_as_commit(&commit, repo, id);
1733 if (error != NULL)
1734 goto done;
1736 if (commit == NULL) {
1737 error = got_repo_match_object_id_prefix(&id,
1738 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1739 if (error != NULL)
1740 return error;
1743 if (error != NULL)
1744 goto done;
1746 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1747 if (error != NULL)
1748 goto done;
1749 if (in_repo_path) {
1750 free(path);
1751 path = in_repo_path;
1754 error = got_ref_list(&refs, repo);
1755 if (error)
1756 goto done;
1758 error = print_commits(id, repo, path, show_patch,
1759 diff_context, limit, first_parent_traversal, &refs);
1760 done:
1761 free(path);
1762 free(repo_path);
1763 free(cwd);
1764 free(id);
1765 if (worktree)
1766 got_worktree_close(worktree);
1767 if (repo) {
1768 const struct got_error *repo_error;
1769 repo_error = got_repo_close(repo);
1770 if (error == NULL)
1771 error = repo_error;
1773 got_ref_list_free(&refs);
1774 return error;
1777 __dead static void
1778 usage_diff(void)
1780 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1781 "[object1 object2 | path]\n", getprogname());
1782 exit(1);
1785 struct print_diff_arg {
1786 struct got_repository *repo;
1787 struct got_worktree *worktree;
1788 int diff_context;
1789 const char *id_str;
1790 int header_shown;
1791 int diff_staged;
1794 static const struct got_error *
1795 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1796 const char *path, struct got_object_id *blob_id,
1797 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1799 struct print_diff_arg *a = arg;
1800 const struct got_error *err = NULL;
1801 struct got_blob_object *blob1 = NULL;
1802 FILE *f2 = NULL;
1803 char *abspath = NULL, *label1 = NULL;
1804 struct stat sb;
1806 if (a->diff_staged) {
1807 if (staged_status != GOT_STATUS_MODIFY &&
1808 staged_status != GOT_STATUS_ADD &&
1809 staged_status != GOT_STATUS_DELETE)
1810 return NULL;
1811 } else {
1812 if (staged_status == GOT_STATUS_DELETE)
1813 return NULL;
1814 if (status != GOT_STATUS_MODIFY &&
1815 status != GOT_STATUS_ADD &&
1816 status != GOT_STATUS_DELETE &&
1817 status != GOT_STATUS_CONFLICT)
1818 return NULL;
1821 if (!a->header_shown) {
1822 printf("diff %s %s%s\n", a->id_str,
1823 got_worktree_get_root_path(a->worktree),
1824 a->diff_staged ? " (staged changes)" : "");
1825 a->header_shown = 1;
1828 if (a->diff_staged) {
1829 const char *label1 = NULL, *label2 = NULL;
1830 switch (staged_status) {
1831 case GOT_STATUS_MODIFY:
1832 label1 = path;
1833 label2 = path;
1834 break;
1835 case GOT_STATUS_ADD:
1836 label2 = path;
1837 break;
1838 case GOT_STATUS_DELETE:
1839 label1 = path;
1840 break;
1841 default:
1842 return got_error(GOT_ERR_FILE_STATUS);
1844 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1845 label1, label2, a->diff_context, a->repo, stdout);
1848 if (staged_status == GOT_STATUS_ADD ||
1849 staged_status == GOT_STATUS_MODIFY) {
1850 char *id_str;
1851 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1852 8192);
1853 if (err)
1854 goto done;
1855 err = got_object_id_str(&id_str, staged_blob_id);
1856 if (err)
1857 goto done;
1858 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1859 err = got_error_from_errno("asprintf");
1860 free(id_str);
1861 goto done;
1863 free(id_str);
1864 } else if (status != GOT_STATUS_ADD) {
1865 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1866 if (err)
1867 goto done;
1870 if (status != GOT_STATUS_DELETE) {
1871 if (asprintf(&abspath, "%s/%s",
1872 got_worktree_get_root_path(a->worktree), path) == -1) {
1873 err = got_error_from_errno("asprintf");
1874 goto done;
1877 f2 = fopen(abspath, "r");
1878 if (f2 == NULL) {
1879 err = got_error_from_errno2("fopen", abspath);
1880 goto done;
1882 if (lstat(abspath, &sb) == -1) {
1883 err = got_error_from_errno2("lstat", abspath);
1884 goto done;
1886 } else
1887 sb.st_size = 0;
1889 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1890 a->diff_context, stdout);
1891 done:
1892 if (blob1)
1893 got_object_blob_close(blob1);
1894 if (f2 && fclose(f2) != 0 && err == NULL)
1895 err = got_error_from_errno("fclose");
1896 free(abspath);
1897 return err;
1900 static const struct got_error *
1901 match_object_id(struct got_object_id **id, char **label,
1902 const char *id_str, int obj_type, struct got_repository *repo)
1904 const struct got_error *err;
1905 struct got_tag_object *tag;
1906 struct got_reference *ref = NULL;
1908 *id = NULL;
1909 *label = NULL;
1911 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1912 if (err == NULL) {
1913 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1914 if (*id == NULL)
1915 err = got_error_from_errno("got_object_id_dup");
1916 if (asprintf(label, "refs/tags/%s",
1917 got_object_tag_get_name(tag)) == -1)
1918 err = got_error_from_errno("asprintf");
1919 got_object_tag_close(tag);
1920 return err;
1921 } else if (err->code != GOT_ERR_NO_OBJ)
1922 return err;
1924 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1925 if (err) {
1926 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1927 return err;
1928 err = got_ref_open(&ref, repo, id_str, 0);
1929 if (err != NULL)
1930 goto done;
1931 *label = strdup(got_ref_get_name(ref));
1932 if (*label == NULL) {
1933 err = got_error_from_errno("strdup");
1934 goto done;
1936 err = got_ref_resolve(id, repo, ref);
1937 } else {
1938 err = got_object_id_str(label, *id);
1939 if (*label == NULL) {
1940 err = got_error_from_errno("strdup");
1941 goto done;
1944 done:
1945 if (ref)
1946 got_ref_close(ref);
1947 return err;
1951 static const struct got_error *
1952 cmd_diff(int argc, char *argv[])
1954 const struct got_error *error;
1955 struct got_repository *repo = NULL;
1956 struct got_worktree *worktree = NULL;
1957 char *cwd = NULL, *repo_path = NULL;
1958 struct got_object_id *id1 = NULL, *id2 = NULL;
1959 const char *id_str1 = NULL, *id_str2 = NULL;
1960 char *label1 = NULL, *label2 = NULL;
1961 int type1, type2;
1962 int diff_context = 3, diff_staged = 0, ch;
1963 const char *errstr;
1964 char *path = NULL;
1966 #ifndef PROFILE
1967 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1968 NULL) == -1)
1969 err(1, "pledge");
1970 #endif
1972 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1973 switch (ch) {
1974 case 'C':
1975 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1976 if (errstr != NULL)
1977 err(1, "-C option %s", errstr);
1978 break;
1979 case 'r':
1980 repo_path = realpath(optarg, NULL);
1981 if (repo_path == NULL)
1982 err(1, "-r option");
1983 got_path_strip_trailing_slashes(repo_path);
1984 break;
1985 case 's':
1986 diff_staged = 1;
1987 break;
1988 default:
1989 usage_diff();
1990 /* NOTREACHED */
1994 argc -= optind;
1995 argv += optind;
1997 cwd = getcwd(NULL, 0);
1998 if (cwd == NULL) {
1999 error = got_error_from_errno("getcwd");
2000 goto done;
2002 error = got_worktree_open(&worktree, cwd);
2003 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2004 goto done;
2005 if (argc <= 1) {
2006 if (worktree == NULL) {
2007 error = got_error(GOT_ERR_NOT_WORKTREE);
2008 goto done;
2010 if (repo_path)
2011 errx(1,
2012 "-r option can't be used when diffing a work tree");
2013 repo_path = strdup(got_worktree_get_repo_path(worktree));
2014 if (repo_path == NULL) {
2015 error = got_error_from_errno("strdup");
2016 goto done;
2018 if (argc == 1) {
2019 error = got_worktree_resolve_path(&path, worktree,
2020 argv[0]);
2021 if (error)
2022 goto done;
2023 } else {
2024 path = strdup("");
2025 if (path == NULL) {
2026 error = got_error_from_errno("strdup");
2027 goto done;
2030 } else if (argc == 2) {
2031 if (diff_staged)
2032 errx(1, "-s option can't be used when diffing "
2033 "objects in repository");
2034 id_str1 = argv[0];
2035 id_str2 = argv[1];
2036 if (worktree && repo_path == NULL) {
2037 repo_path =
2038 strdup(got_worktree_get_repo_path(worktree));
2039 if (repo_path == NULL) {
2040 error = got_error_from_errno("strdup");
2041 goto done;
2044 } else
2045 usage_diff();
2047 if (repo_path == NULL) {
2048 repo_path = getcwd(NULL, 0);
2049 if (repo_path == NULL)
2050 return got_error_from_errno("getcwd");
2053 error = got_repo_open(&repo, repo_path);
2054 free(repo_path);
2055 if (error != NULL)
2056 goto done;
2058 error = apply_unveil(got_repo_get_path(repo), 1,
2059 worktree ? got_worktree_get_root_path(worktree) : NULL);
2060 if (error)
2061 goto done;
2063 if (argc <= 1) {
2064 struct print_diff_arg arg;
2065 struct got_pathlist_head paths;
2066 char *id_str;
2068 TAILQ_INIT(&paths);
2070 error = got_object_id_str(&id_str,
2071 got_worktree_get_base_commit_id(worktree));
2072 if (error)
2073 goto done;
2074 arg.repo = repo;
2075 arg.worktree = worktree;
2076 arg.diff_context = diff_context;
2077 arg.id_str = id_str;
2078 arg.header_shown = 0;
2079 arg.diff_staged = diff_staged;
2081 error = got_pathlist_append(&paths, path, NULL);
2082 if (error)
2083 goto done;
2085 error = got_worktree_status(worktree, &paths, repo, print_diff,
2086 &arg, check_cancelled, NULL);
2087 free(id_str);
2088 got_pathlist_free(&paths);
2089 goto done;
2092 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2093 if (error)
2094 goto done;
2096 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2097 if (error)
2098 goto done;
2100 error = got_object_get_type(&type1, repo, id1);
2101 if (error)
2102 goto done;
2104 error = got_object_get_type(&type2, repo, id2);
2105 if (error)
2106 goto done;
2108 if (type1 != type2) {
2109 error = got_error(GOT_ERR_OBJ_TYPE);
2110 goto done;
2113 switch (type1) {
2114 case GOT_OBJ_TYPE_BLOB:
2115 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2116 diff_context, repo, stdout);
2117 break;
2118 case GOT_OBJ_TYPE_TREE:
2119 error = got_diff_objects_as_trees(id1, id2, "", "",
2120 diff_context, repo, stdout);
2121 break;
2122 case GOT_OBJ_TYPE_COMMIT:
2123 printf("diff %s %s\n", label1, label2);
2124 error = got_diff_objects_as_commits(id1, id2, diff_context,
2125 repo, stdout);
2126 break;
2127 default:
2128 error = got_error(GOT_ERR_OBJ_TYPE);
2131 done:
2132 free(label1);
2133 free(label2);
2134 free(id1);
2135 free(id2);
2136 free(path);
2137 if (worktree)
2138 got_worktree_close(worktree);
2139 if (repo) {
2140 const struct got_error *repo_error;
2141 repo_error = got_repo_close(repo);
2142 if (error == NULL)
2143 error = repo_error;
2145 return error;
2148 __dead static void
2149 usage_blame(void)
2151 fprintf(stderr,
2152 "usage: %s blame [-c commit] [-r repository-path] path\n",
2153 getprogname());
2154 exit(1);
2157 struct blame_line {
2158 int annotated;
2159 char *id_str;
2160 char *committer;
2161 char datebuf[9]; /* YY-MM-DD + NUL */
2164 struct blame_cb_args {
2165 struct blame_line *lines;
2166 int nlines;
2167 int nlines_prec;
2168 int lineno_cur;
2169 off_t *line_offsets;
2170 FILE *f;
2171 struct got_repository *repo;
2174 static const struct got_error *
2175 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2177 const struct got_error *err = NULL;
2178 struct blame_cb_args *a = arg;
2179 struct blame_line *bline;
2180 char *line = NULL;
2181 size_t linesize = 0;
2182 struct got_commit_object *commit = NULL;
2183 off_t offset;
2184 struct tm tm;
2185 time_t committer_time;
2187 if (nlines != a->nlines ||
2188 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2189 return got_error(GOT_ERR_RANGE);
2191 if (sigint_received)
2192 return got_error(GOT_ERR_ITER_COMPLETED);
2194 if (lineno == -1)
2195 return NULL; /* no change in this commit */
2197 /* Annotate this line. */
2198 bline = &a->lines[lineno - 1];
2199 if (bline->annotated)
2200 return NULL;
2201 err = got_object_id_str(&bline->id_str, id);
2202 if (err)
2203 return err;
2205 err = got_object_open_as_commit(&commit, a->repo, id);
2206 if (err)
2207 goto done;
2209 bline->committer = strdup(got_object_commit_get_committer(commit));
2210 if (bline->committer == NULL) {
2211 err = got_error_from_errno("strdup");
2212 goto done;
2215 committer_time = got_object_commit_get_committer_time(commit);
2216 if (localtime_r(&committer_time, &tm) == NULL)
2217 return got_error_from_errno("localtime_r");
2218 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2219 &tm) >= sizeof(bline->datebuf)) {
2220 err = got_error(GOT_ERR_NO_SPACE);
2221 goto done;
2223 bline->annotated = 1;
2225 /* Print lines annotated so far. */
2226 bline = &a->lines[a->lineno_cur - 1];
2227 if (!bline->annotated)
2228 goto done;
2230 offset = a->line_offsets[a->lineno_cur - 1];
2231 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2232 err = got_error_from_errno("fseeko");
2233 goto done;
2236 while (bline->annotated) {
2237 char *smallerthan, *at, *nl, *committer;
2238 size_t len;
2240 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2241 if (ferror(a->f))
2242 err = got_error_from_errno("getline");
2243 break;
2246 committer = bline->committer;
2247 smallerthan = strchr(committer, '<');
2248 if (smallerthan && smallerthan[1] != '\0')
2249 committer = smallerthan + 1;
2250 at = strchr(committer, '@');
2251 if (at)
2252 *at = '\0';
2253 len = strlen(committer);
2254 if (len >= 9)
2255 committer[8] = '\0';
2257 nl = strchr(line, '\n');
2258 if (nl)
2259 *nl = '\0';
2260 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2261 bline->id_str, bline->datebuf, committer, line);
2263 a->lineno_cur++;
2264 bline = &a->lines[a->lineno_cur - 1];
2266 done:
2267 if (commit)
2268 got_object_commit_close(commit);
2269 free(line);
2270 return err;
2273 static const struct got_error *
2274 cmd_blame(int argc, char *argv[])
2276 const struct got_error *error;
2277 struct got_repository *repo = NULL;
2278 struct got_worktree *worktree = NULL;
2279 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2280 struct got_object_id *obj_id = NULL;
2281 struct got_object_id *commit_id = NULL;
2282 struct got_blob_object *blob = NULL;
2283 char *commit_id_str = NULL;
2284 struct blame_cb_args bca;
2285 int ch, obj_type, i;
2287 #ifndef PROFILE
2288 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2289 NULL) == -1)
2290 err(1, "pledge");
2291 #endif
2293 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2294 switch (ch) {
2295 case 'c':
2296 commit_id_str = optarg;
2297 break;
2298 case 'r':
2299 repo_path = realpath(optarg, NULL);
2300 if (repo_path == NULL)
2301 err(1, "-r option");
2302 got_path_strip_trailing_slashes(repo_path);
2303 break;
2304 default:
2305 usage_blame();
2306 /* NOTREACHED */
2310 argc -= optind;
2311 argv += optind;
2313 if (argc == 1)
2314 path = argv[0];
2315 else
2316 usage_blame();
2318 cwd = getcwd(NULL, 0);
2319 if (cwd == NULL) {
2320 error = got_error_from_errno("getcwd");
2321 goto done;
2323 if (repo_path == NULL) {
2324 error = got_worktree_open(&worktree, cwd);
2325 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2326 goto done;
2327 else
2328 error = NULL;
2329 if (worktree) {
2330 repo_path =
2331 strdup(got_worktree_get_repo_path(worktree));
2332 if (repo_path == NULL)
2333 error = got_error_from_errno("strdup");
2334 if (error)
2335 goto done;
2336 } else {
2337 repo_path = strdup(cwd);
2338 if (repo_path == NULL) {
2339 error = got_error_from_errno("strdup");
2340 goto done;
2345 error = got_repo_open(&repo, repo_path);
2346 if (error != NULL)
2347 goto done;
2349 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2350 if (error)
2351 goto done;
2353 if (worktree) {
2354 const char *prefix = got_worktree_get_path_prefix(worktree);
2355 char *p, *worktree_subdir = cwd +
2356 strlen(got_worktree_get_root_path(worktree));
2357 if (asprintf(&p, "%s%s%s%s%s",
2358 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2359 worktree_subdir, worktree_subdir[0] ? "/" : "",
2360 path) == -1) {
2361 error = got_error_from_errno("asprintf");
2362 goto done;
2364 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2365 free(p);
2366 } else {
2367 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2369 if (error)
2370 goto done;
2372 if (commit_id_str == NULL) {
2373 struct got_reference *head_ref;
2374 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2375 if (error != NULL)
2376 goto done;
2377 error = got_ref_resolve(&commit_id, repo, head_ref);
2378 got_ref_close(head_ref);
2379 if (error != NULL)
2380 goto done;
2381 } else {
2382 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2383 if (error)
2384 goto done;
2387 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2388 if (error)
2389 goto done;
2390 if (obj_id == NULL) {
2391 error = got_error(GOT_ERR_NO_OBJ);
2392 goto done;
2395 error = got_object_get_type(&obj_type, repo, obj_id);
2396 if (error)
2397 goto done;
2399 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2400 error = got_error(GOT_ERR_OBJ_TYPE);
2401 goto done;
2404 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2405 if (error)
2406 goto done;
2407 bca.f = got_opentemp();
2408 if (bca.f == NULL) {
2409 error = got_error_from_errno("got_opentemp");
2410 goto done;
2412 error = got_object_blob_dump_to_file(NULL, &bca.nlines,
2413 &bca.line_offsets, bca.f, blob);
2414 if (error || bca.nlines == 0)
2415 goto done;
2417 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2418 if (bca.lines == NULL) {
2419 error = got_error_from_errno("calloc");
2420 goto done;
2422 bca.lineno_cur = 1;
2423 bca.nlines_prec = 0;
2424 i = bca.nlines;
2425 while (i > 0) {
2426 i /= 10;
2427 bca.nlines_prec++;
2429 bca.repo = repo;
2431 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2432 if (error)
2433 goto done;
2434 done:
2435 free(in_repo_path);
2436 free(repo_path);
2437 free(cwd);
2438 free(commit_id);
2439 free(obj_id);
2440 if (blob)
2441 got_object_blob_close(blob);
2442 if (worktree)
2443 got_worktree_close(worktree);
2444 if (repo) {
2445 const struct got_error *repo_error;
2446 repo_error = got_repo_close(repo);
2447 if (error == NULL)
2448 error = repo_error;
2450 for (i = 0; i < bca.nlines; i++) {
2451 struct blame_line *bline = &bca.lines[i];
2452 free(bline->id_str);
2453 free(bline->committer);
2455 free(bca.lines);
2456 free(bca.line_offsets);
2457 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2458 error = got_error_from_errno("fclose");
2459 return error;
2462 __dead static void
2463 usage_tree(void)
2465 fprintf(stderr,
2466 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2467 getprogname());
2468 exit(1);
2471 static void
2472 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2473 const char *root_path)
2475 int is_root_path = (strcmp(path, root_path) == 0);
2476 const char *modestr = "";
2478 path += strlen(root_path);
2479 while (path[0] == '/')
2480 path++;
2482 if (S_ISLNK(te->mode))
2483 modestr = "@";
2484 else if (S_ISDIR(te->mode))
2485 modestr = "/";
2486 else if (te->mode & S_IXUSR)
2487 modestr = "*";
2489 printf("%s%s%s%s%s\n", id ? id : "", path,
2490 is_root_path ? "" : "/", te->name, modestr);
2493 static const struct got_error *
2494 print_tree(const char *path, struct got_object_id *commit_id,
2495 int show_ids, int recurse, const char *root_path,
2496 struct got_repository *repo)
2498 const struct got_error *err = NULL;
2499 struct got_object_id *tree_id = NULL;
2500 struct got_tree_object *tree = NULL;
2501 const struct got_tree_entries *entries;
2502 struct got_tree_entry *te;
2504 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2505 if (err)
2506 goto done;
2508 err = got_object_open_as_tree(&tree, repo, tree_id);
2509 if (err)
2510 goto done;
2511 entries = got_object_tree_get_entries(tree);
2512 te = SIMPLEQ_FIRST(&entries->head);
2513 while (te) {
2514 char *id = NULL;
2516 if (sigint_received || sigpipe_received)
2517 break;
2519 if (show_ids) {
2520 char *id_str;
2521 err = got_object_id_str(&id_str, te->id);
2522 if (err)
2523 goto done;
2524 if (asprintf(&id, "%s ", id_str) == -1) {
2525 err = got_error_from_errno("asprintf");
2526 free(id_str);
2527 goto done;
2529 free(id_str);
2531 print_entry(te, id, path, root_path);
2532 free(id);
2534 if (recurse && S_ISDIR(te->mode)) {
2535 char *child_path;
2536 if (asprintf(&child_path, "%s%s%s", path,
2537 path[0] == '/' && path[1] == '\0' ? "" : "/",
2538 te->name) == -1) {
2539 err = got_error_from_errno("asprintf");
2540 goto done;
2542 err = print_tree(child_path, commit_id, show_ids, 1,
2543 root_path, repo);
2544 free(child_path);
2545 if (err)
2546 goto done;
2549 te = SIMPLEQ_NEXT(te, entry);
2551 done:
2552 if (tree)
2553 got_object_tree_close(tree);
2554 free(tree_id);
2555 return err;
2558 static const struct got_error *
2559 cmd_tree(int argc, char *argv[])
2561 const struct got_error *error;
2562 struct got_repository *repo = NULL;
2563 struct got_worktree *worktree = NULL;
2564 const char *path;
2565 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2566 struct got_object_id *commit_id = NULL;
2567 char *commit_id_str = NULL;
2568 int show_ids = 0, recurse = 0;
2569 int ch;
2571 #ifndef PROFILE
2572 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2573 NULL) == -1)
2574 err(1, "pledge");
2575 #endif
2577 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2578 switch (ch) {
2579 case 'c':
2580 commit_id_str = optarg;
2581 break;
2582 case 'r':
2583 repo_path = realpath(optarg, NULL);
2584 if (repo_path == NULL)
2585 err(1, "-r option");
2586 got_path_strip_trailing_slashes(repo_path);
2587 break;
2588 case 'i':
2589 show_ids = 1;
2590 break;
2591 case 'R':
2592 recurse = 1;
2593 break;
2594 default:
2595 usage_tree();
2596 /* NOTREACHED */
2600 argc -= optind;
2601 argv += optind;
2603 if (argc == 1)
2604 path = argv[0];
2605 else if (argc > 1)
2606 usage_tree();
2607 else
2608 path = NULL;
2610 cwd = getcwd(NULL, 0);
2611 if (cwd == NULL) {
2612 error = got_error_from_errno("getcwd");
2613 goto done;
2615 if (repo_path == NULL) {
2616 error = got_worktree_open(&worktree, cwd);
2617 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2618 goto done;
2619 else
2620 error = NULL;
2621 if (worktree) {
2622 repo_path =
2623 strdup(got_worktree_get_repo_path(worktree));
2624 if (repo_path == NULL)
2625 error = got_error_from_errno("strdup");
2626 if (error)
2627 goto done;
2628 } else {
2629 repo_path = strdup(cwd);
2630 if (repo_path == NULL) {
2631 error = got_error_from_errno("strdup");
2632 goto done;
2637 error = got_repo_open(&repo, repo_path);
2638 if (error != NULL)
2639 goto done;
2641 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2642 if (error)
2643 goto done;
2645 if (path == NULL) {
2646 if (worktree) {
2647 char *p, *worktree_subdir = cwd +
2648 strlen(got_worktree_get_root_path(worktree));
2649 if (asprintf(&p, "%s/%s",
2650 got_worktree_get_path_prefix(worktree),
2651 worktree_subdir) == -1) {
2652 error = got_error_from_errno("asprintf");
2653 goto done;
2655 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2656 free(p);
2657 if (error)
2658 goto done;
2659 } else
2660 path = "/";
2662 if (in_repo_path == NULL) {
2663 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2664 if (error != NULL)
2665 goto done;
2668 if (commit_id_str == NULL) {
2669 struct got_reference *head_ref;
2670 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2671 if (error != NULL)
2672 goto done;
2673 error = got_ref_resolve(&commit_id, repo, head_ref);
2674 got_ref_close(head_ref);
2675 if (error != NULL)
2676 goto done;
2677 } else {
2678 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2679 if (error)
2680 goto done;
2683 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2684 in_repo_path, repo);
2685 done:
2686 free(in_repo_path);
2687 free(repo_path);
2688 free(cwd);
2689 free(commit_id);
2690 if (worktree)
2691 got_worktree_close(worktree);
2692 if (repo) {
2693 const struct got_error *repo_error;
2694 repo_error = got_repo_close(repo);
2695 if (error == NULL)
2696 error = repo_error;
2698 return error;
2701 __dead static void
2702 usage_status(void)
2704 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2705 exit(1);
2708 static const struct got_error *
2709 print_status(void *arg, unsigned char status, unsigned char staged_status,
2710 const char *path, struct got_object_id *blob_id,
2711 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2713 if (status == staged_status && (status == GOT_STATUS_DELETE))
2714 status = GOT_STATUS_NO_CHANGE;
2715 printf("%c%c %s\n", status, staged_status, path);
2716 return NULL;
2719 static const struct got_error *
2720 cmd_status(int argc, char *argv[])
2722 const struct got_error *error = NULL;
2723 struct got_repository *repo = NULL;
2724 struct got_worktree *worktree = NULL;
2725 char *cwd = NULL;
2726 struct got_pathlist_head paths;
2727 struct got_pathlist_entry *pe;
2728 int ch;
2730 TAILQ_INIT(&paths);
2732 while ((ch = getopt(argc, argv, "")) != -1) {
2733 switch (ch) {
2734 default:
2735 usage_status();
2736 /* NOTREACHED */
2740 argc -= optind;
2741 argv += optind;
2743 #ifndef PROFILE
2744 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2745 NULL) == -1)
2746 err(1, "pledge");
2747 #endif
2748 cwd = getcwd(NULL, 0);
2749 if (cwd == NULL) {
2750 error = got_error_from_errno("getcwd");
2751 goto done;
2754 error = got_worktree_open(&worktree, cwd);
2755 if (error != NULL)
2756 goto done;
2758 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2759 if (error != NULL)
2760 goto done;
2762 error = apply_unveil(got_repo_get_path(repo), 1,
2763 got_worktree_get_root_path(worktree));
2764 if (error)
2765 goto done;
2767 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2768 if (error)
2769 goto done;
2771 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2772 check_cancelled, NULL);
2773 done:
2774 TAILQ_FOREACH(pe, &paths, entry)
2775 free((char *)pe->path);
2776 got_pathlist_free(&paths);
2777 free(cwd);
2778 return error;
2781 __dead static void
2782 usage_ref(void)
2784 fprintf(stderr,
2785 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2786 getprogname());
2787 exit(1);
2790 static const struct got_error *
2791 list_refs(struct got_repository *repo)
2793 static const struct got_error *err = NULL;
2794 struct got_reflist_head refs;
2795 struct got_reflist_entry *re;
2797 SIMPLEQ_INIT(&refs);
2798 err = got_ref_list(&refs, repo);
2799 if (err)
2800 return err;
2802 SIMPLEQ_FOREACH(re, &refs, entry) {
2803 char *refstr;
2804 refstr = got_ref_to_str(re->ref);
2805 if (refstr == NULL)
2806 return got_error_from_errno("got_ref_to_str");
2807 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2808 free(refstr);
2811 got_ref_list_free(&refs);
2812 return NULL;
2815 static const struct got_error *
2816 delete_ref(struct got_repository *repo, const char *refname)
2818 const struct got_error *err = NULL;
2819 struct got_reference *ref;
2821 err = got_ref_open(&ref, repo, refname, 0);
2822 if (err)
2823 return err;
2825 err = got_ref_delete(ref, repo);
2826 got_ref_close(ref);
2827 return err;
2830 static const struct got_error *
2831 add_ref(struct got_repository *repo, const char *refname, const char *target)
2833 const struct got_error *err = NULL;
2834 struct got_object_id *id;
2835 struct got_reference *ref = NULL;
2838 * Don't let the user create a reference named '-'.
2839 * While technically a valid reference name, this case is usually
2840 * an unintended typo.
2842 if (refname[0] == '-' && refname[1] == '\0')
2843 return got_error(GOT_ERR_BAD_REF_NAME);
2845 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2846 repo);
2847 if (err) {
2848 struct got_reference *target_ref;
2850 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2851 return err;
2852 err = got_ref_open(&target_ref, repo, target, 0);
2853 if (err)
2854 return err;
2855 err = got_ref_resolve(&id, repo, target_ref);
2856 got_ref_close(target_ref);
2857 if (err)
2858 return err;
2861 err = got_ref_alloc(&ref, refname, id);
2862 if (err)
2863 goto done;
2865 err = got_ref_write(ref, repo);
2866 done:
2867 if (ref)
2868 got_ref_close(ref);
2869 free(id);
2870 return err;
2873 static const struct got_error *
2874 add_symref(struct got_repository *repo, const char *refname, const char *target)
2876 const struct got_error *err = NULL;
2877 struct got_reference *ref = NULL;
2878 struct got_reference *target_ref = NULL;
2881 * Don't let the user create a reference named '-'.
2882 * While technically a valid reference name, this case is usually
2883 * an unintended typo.
2885 if (refname[0] == '-' && refname[1] == '\0')
2886 return got_error(GOT_ERR_BAD_REF_NAME);
2888 err = got_ref_open(&target_ref, repo, target, 0);
2889 if (err)
2890 return err;
2892 err = got_ref_alloc_symref(&ref, refname, target_ref);
2893 if (err)
2894 goto done;
2896 err = got_ref_write(ref, repo);
2897 done:
2898 if (target_ref)
2899 got_ref_close(target_ref);
2900 if (ref)
2901 got_ref_close(ref);
2902 return err;
2905 static const struct got_error *
2906 cmd_ref(int argc, char *argv[])
2908 const struct got_error *error = NULL;
2909 struct got_repository *repo = NULL;
2910 struct got_worktree *worktree = NULL;
2911 char *cwd = NULL, *repo_path = NULL;
2912 int ch, do_list = 0, create_symref = 0;
2913 const char *delref = NULL;
2915 /* TODO: Add -s option for adding symbolic references. */
2916 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2917 switch (ch) {
2918 case 'd':
2919 delref = optarg;
2920 break;
2921 case 'r':
2922 repo_path = realpath(optarg, NULL);
2923 if (repo_path == NULL)
2924 err(1, "-r option");
2925 got_path_strip_trailing_slashes(repo_path);
2926 break;
2927 case 'l':
2928 do_list = 1;
2929 break;
2930 case 's':
2931 create_symref = 1;
2932 break;
2933 default:
2934 usage_ref();
2935 /* NOTREACHED */
2939 if (do_list && delref)
2940 errx(1, "-l and -d options are mutually exclusive\n");
2942 argc -= optind;
2943 argv += optind;
2945 if (do_list || delref) {
2946 if (create_symref)
2947 errx(1, "-s option cannot be used together with the "
2948 "-l or -d options");
2949 if (argc > 0)
2950 usage_ref();
2951 } else if (argc != 2)
2952 usage_ref();
2954 #ifndef PROFILE
2955 if (do_list) {
2956 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2957 NULL) == -1)
2958 err(1, "pledge");
2959 } else {
2960 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2961 "sendfd unveil", NULL) == -1)
2962 err(1, "pledge");
2964 #endif
2965 cwd = getcwd(NULL, 0);
2966 if (cwd == NULL) {
2967 error = got_error_from_errno("getcwd");
2968 goto done;
2971 if (repo_path == NULL) {
2972 error = got_worktree_open(&worktree, cwd);
2973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2974 goto done;
2975 else
2976 error = NULL;
2977 if (worktree) {
2978 repo_path =
2979 strdup(got_worktree_get_repo_path(worktree));
2980 if (repo_path == NULL)
2981 error = got_error_from_errno("strdup");
2982 if (error)
2983 goto done;
2984 } else {
2985 repo_path = strdup(cwd);
2986 if (repo_path == NULL) {
2987 error = got_error_from_errno("strdup");
2988 goto done;
2993 error = got_repo_open(&repo, repo_path);
2994 if (error != NULL)
2995 goto done;
2997 error = apply_unveil(got_repo_get_path(repo), do_list,
2998 worktree ? got_worktree_get_root_path(worktree) : NULL);
2999 if (error)
3000 goto done;
3002 if (do_list)
3003 error = list_refs(repo);
3004 else if (delref)
3005 error = delete_ref(repo, delref);
3006 else if (create_symref)
3007 error = add_symref(repo, argv[0], argv[1]);
3008 else
3009 error = add_ref(repo, argv[0], argv[1]);
3010 done:
3011 if (repo)
3012 got_repo_close(repo);
3013 if (worktree)
3014 got_worktree_close(worktree);
3015 free(cwd);
3016 free(repo_path);
3017 return error;
3020 __dead static void
3021 usage_branch(void)
3023 fprintf(stderr,
3024 "usage: %s branch [-r repository] -l | -d name | "
3025 "name [base-branch]\n", getprogname());
3026 exit(1);
3029 static const struct got_error *
3030 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3032 static const struct got_error *err = NULL;
3033 struct got_reflist_head refs;
3034 struct got_reflist_entry *re;
3036 SIMPLEQ_INIT(&refs);
3038 err = got_ref_list(&refs, repo);
3039 if (err)
3040 return err;
3042 SIMPLEQ_FOREACH(re, &refs, entry) {
3043 const char *refname, *marker = " ";
3044 char *refstr;
3045 refname = got_ref_get_name(re->ref);
3046 if (strncmp(refname, "refs/heads/", 11) != 0)
3047 continue;
3048 if (worktree && strcmp(refname,
3049 got_worktree_get_head_ref_name(worktree)) == 0) {
3050 struct got_object_id *id = NULL;
3051 err = got_ref_resolve(&id, repo, re->ref);
3052 if (err)
3053 return err;
3054 if (got_object_id_cmp(id,
3055 got_worktree_get_base_commit_id(worktree)) == 0)
3056 marker = "* ";
3057 else
3058 marker = "~ ";
3059 free(id);
3061 refname += 11;
3062 refstr = got_ref_to_str(re->ref);
3063 if (refstr == NULL)
3064 return got_error_from_errno("got_ref_to_str");
3065 printf("%s%s: %s\n", marker, refname, refstr);
3066 free(refstr);
3069 got_ref_list_free(&refs);
3070 return NULL;
3073 static const struct got_error *
3074 delete_branch(struct got_repository *repo, const char *branch_name)
3076 const struct got_error *err = NULL;
3077 struct got_reference *ref;
3078 char *refname;
3080 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3081 return got_error_from_errno("asprintf");
3083 err = got_ref_open(&ref, repo, refname, 0);
3084 if (err)
3085 goto done;
3087 err = got_ref_delete(ref, repo);
3088 got_ref_close(ref);
3089 done:
3090 free(refname);
3091 return err;
3094 static const struct got_error *
3095 add_branch(struct got_repository *repo, const char *branch_name,
3096 const char *base_branch)
3098 const struct got_error *err = NULL;
3099 struct got_object_id *id = NULL;
3100 struct got_reference *ref = NULL;
3101 char *base_refname = NULL, *refname = NULL;
3102 struct got_reference *base_ref;
3105 * Don't let the user create a branch named '-'.
3106 * While technically a valid reference name, this case is usually
3107 * an unintended typo.
3109 if (branch_name[0] == '-' && branch_name[1] == '\0')
3110 return got_error(GOT_ERR_BAD_REF_NAME);
3112 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3113 base_refname = strdup(GOT_REF_HEAD);
3114 if (base_refname == NULL)
3115 return got_error_from_errno("strdup");
3116 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3117 return got_error_from_errno("asprintf");
3119 err = got_ref_open(&base_ref, repo, base_refname, 0);
3120 if (err)
3121 goto done;
3122 err = got_ref_resolve(&id, repo, base_ref);
3123 got_ref_close(base_ref);
3124 if (err)
3125 goto done;
3127 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3128 err = got_error_from_errno("asprintf");
3129 goto done;
3132 err = got_ref_open(&ref, repo, refname, 0);
3133 if (err == NULL) {
3134 err = got_error(GOT_ERR_BRANCH_EXISTS);
3135 goto done;
3136 } else if (err->code != GOT_ERR_NOT_REF)
3137 goto done;
3139 err = got_ref_alloc(&ref, refname, id);
3140 if (err)
3141 goto done;
3143 err = got_ref_write(ref, repo);
3144 done:
3145 if (ref)
3146 got_ref_close(ref);
3147 free(id);
3148 free(base_refname);
3149 free(refname);
3150 return err;
3153 static const struct got_error *
3154 cmd_branch(int argc, char *argv[])
3156 const struct got_error *error = NULL;
3157 struct got_repository *repo = NULL;
3158 struct got_worktree *worktree = NULL;
3159 char *cwd = NULL, *repo_path = NULL;
3160 int ch, do_list = 0;
3161 const char *delref = NULL;
3163 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3164 switch (ch) {
3165 case 'd':
3166 delref = optarg;
3167 break;
3168 case 'r':
3169 repo_path = realpath(optarg, NULL);
3170 if (repo_path == NULL)
3171 err(1, "-r option");
3172 got_path_strip_trailing_slashes(repo_path);
3173 break;
3174 case 'l':
3175 do_list = 1;
3176 break;
3177 default:
3178 usage_branch();
3179 /* NOTREACHED */
3183 if (do_list && delref)
3184 errx(1, "-l and -d options are mutually exclusive\n");
3186 argc -= optind;
3187 argv += optind;
3189 if (do_list || delref) {
3190 if (argc > 0)
3191 usage_branch();
3192 } else if (argc < 1 || argc > 2)
3193 usage_branch();
3195 #ifndef PROFILE
3196 if (do_list) {
3197 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3198 NULL) == -1)
3199 err(1, "pledge");
3200 } else {
3201 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3202 "sendfd unveil", NULL) == -1)
3203 err(1, "pledge");
3205 #endif
3206 cwd = getcwd(NULL, 0);
3207 if (cwd == NULL) {
3208 error = got_error_from_errno("getcwd");
3209 goto done;
3212 if (repo_path == NULL) {
3213 error = got_worktree_open(&worktree, cwd);
3214 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3215 goto done;
3216 else
3217 error = NULL;
3218 if (worktree) {
3219 repo_path =
3220 strdup(got_worktree_get_repo_path(worktree));
3221 if (repo_path == NULL)
3222 error = got_error_from_errno("strdup");
3223 if (error)
3224 goto done;
3225 } else {
3226 repo_path = strdup(cwd);
3227 if (repo_path == NULL) {
3228 error = got_error_from_errno("strdup");
3229 goto done;
3234 error = got_repo_open(&repo, repo_path);
3235 if (error != NULL)
3236 goto done;
3238 error = apply_unveil(got_repo_get_path(repo), do_list,
3239 worktree ? got_worktree_get_root_path(worktree) : NULL);
3240 if (error)
3241 goto done;
3243 if (do_list)
3244 error = list_branches(repo, worktree);
3245 else if (delref)
3246 error = delete_branch(repo, delref);
3247 else {
3248 const char *base_branch;
3249 if (argc == 1) {
3250 base_branch = worktree ?
3251 got_worktree_get_head_ref_name(worktree) :
3252 GOT_REF_HEAD;
3253 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3254 base_branch += 11;
3255 } else
3256 base_branch = argv[1];
3257 error = add_branch(repo, argv[0], base_branch);
3259 done:
3260 if (repo)
3261 got_repo_close(repo);
3262 if (worktree)
3263 got_worktree_close(worktree);
3264 free(cwd);
3265 free(repo_path);
3266 return error;
3269 __dead static void
3270 usage_add(void)
3272 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3273 exit(1);
3276 static const struct got_error *
3277 cmd_add(int argc, char *argv[])
3279 const struct got_error *error = NULL;
3280 struct got_repository *repo = NULL;
3281 struct got_worktree *worktree = NULL;
3282 char *cwd = NULL;
3283 struct got_pathlist_head paths;
3284 struct got_pathlist_entry *pe;
3285 int ch;
3287 TAILQ_INIT(&paths);
3289 while ((ch = getopt(argc, argv, "")) != -1) {
3290 switch (ch) {
3291 default:
3292 usage_add();
3293 /* NOTREACHED */
3297 argc -= optind;
3298 argv += optind;
3300 #ifndef PROFILE
3301 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3302 NULL) == -1)
3303 err(1, "pledge");
3304 #endif
3305 if (argc < 1)
3306 usage_add();
3308 cwd = getcwd(NULL, 0);
3309 if (cwd == NULL) {
3310 error = got_error_from_errno("getcwd");
3311 goto done;
3314 error = got_worktree_open(&worktree, cwd);
3315 if (error)
3316 goto done;
3318 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3319 if (error != NULL)
3320 goto done;
3322 error = apply_unveil(got_repo_get_path(repo), 1,
3323 got_worktree_get_root_path(worktree));
3324 if (error)
3325 goto done;
3327 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3328 if (error)
3329 goto done;
3331 error = got_worktree_schedule_add(worktree, &paths, print_status,
3332 NULL, repo);
3333 done:
3334 if (repo)
3335 got_repo_close(repo);
3336 if (worktree)
3337 got_worktree_close(worktree);
3338 TAILQ_FOREACH(pe, &paths, entry)
3339 free((char *)pe->path);
3340 got_pathlist_free(&paths);
3341 free(cwd);
3342 return error;
3345 __dead static void
3346 usage_remove(void)
3348 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3349 exit(1);
3352 static const struct got_error *
3353 cmd_remove(int argc, char *argv[])
3355 const struct got_error *error = NULL;
3356 struct got_worktree *worktree = NULL;
3357 struct got_repository *repo = NULL;
3358 char *cwd = NULL;
3359 struct got_pathlist_head paths;
3360 struct got_pathlist_entry *pe;
3361 int ch, delete_local_mods = 0;
3363 TAILQ_INIT(&paths);
3365 while ((ch = getopt(argc, argv, "f")) != -1) {
3366 switch (ch) {
3367 case 'f':
3368 delete_local_mods = 1;
3369 break;
3370 default:
3371 usage_add();
3372 /* NOTREACHED */
3376 argc -= optind;
3377 argv += optind;
3379 #ifndef PROFILE
3380 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3381 NULL) == -1)
3382 err(1, "pledge");
3383 #endif
3384 if (argc < 1)
3385 usage_remove();
3387 cwd = getcwd(NULL, 0);
3388 if (cwd == NULL) {
3389 error = got_error_from_errno("getcwd");
3390 goto done;
3392 error = got_worktree_open(&worktree, cwd);
3393 if (error)
3394 goto done;
3396 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3397 if (error)
3398 goto done;
3400 error = apply_unveil(got_repo_get_path(repo), 1,
3401 got_worktree_get_root_path(worktree));
3402 if (error)
3403 goto done;
3405 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3406 if (error)
3407 goto done;
3409 error = got_worktree_schedule_delete(worktree, &paths,
3410 delete_local_mods, print_status, NULL, repo);
3411 if (error)
3412 goto done;
3413 done:
3414 if (repo)
3415 got_repo_close(repo);
3416 if (worktree)
3417 got_worktree_close(worktree);
3418 TAILQ_FOREACH(pe, &paths, entry)
3419 free((char *)pe->path);
3420 got_pathlist_free(&paths);
3421 free(cwd);
3422 return error;
3425 __dead static void
3426 usage_revert(void)
3428 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3429 "path ...\n", getprogname());
3430 exit(1);
3433 static const struct got_error *
3434 revert_progress(void *arg, unsigned char status, const char *path)
3436 while (path[0] == '/')
3437 path++;
3438 printf("%c %s\n", status, path);
3439 return NULL;
3442 struct choose_patch_arg {
3443 FILE *patch_script_file;
3444 const char *action;
3447 static const struct got_error *
3448 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3449 int nchanges, const char *action)
3451 char *line = NULL;
3452 size_t linesize = 0;
3453 ssize_t linelen;
3455 switch (status) {
3456 case GOT_STATUS_ADD:
3457 printf("A %s\n%s this addition? [y/n] ", path, action);
3458 break;
3459 case GOT_STATUS_DELETE:
3460 printf("D %s\n%s this deletion? [y/n] ", path, action);
3461 break;
3462 case GOT_STATUS_MODIFY:
3463 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3464 return got_error_from_errno("fseek");
3465 printf(GOT_COMMIT_SEP_STR);
3466 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3467 printf("%s", line);
3468 if (ferror(patch_file))
3469 return got_error_from_errno("getline");
3470 printf(GOT_COMMIT_SEP_STR);
3471 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3472 path, n, nchanges, action);
3473 break;
3474 default:
3475 return got_error_path(path, GOT_ERR_FILE_STATUS);
3478 return NULL;
3481 static const struct got_error *
3482 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3483 FILE *patch_file, int n, int nchanges)
3485 const struct got_error *err = NULL;
3486 char *line = NULL;
3487 size_t linesize = 0;
3488 ssize_t linelen;
3489 int resp = ' ';
3490 struct choose_patch_arg *a = arg;
3492 *choice = GOT_PATCH_CHOICE_NONE;
3494 if (a->patch_script_file) {
3495 char *nl;
3496 err = show_change(status, path, patch_file, n, nchanges,
3497 a->action);
3498 if (err)
3499 return err;
3500 linelen = getline(&line, &linesize, a->patch_script_file);
3501 if (linelen == -1) {
3502 if (ferror(a->patch_script_file))
3503 return got_error_from_errno("getline");
3504 return NULL;
3506 nl = strchr(line, '\n');
3507 if (nl)
3508 *nl = '\0';
3509 if (strcmp(line, "y") == 0) {
3510 *choice = GOT_PATCH_CHOICE_YES;
3511 printf("y\n");
3512 } else if (strcmp(line, "n") == 0) {
3513 *choice = GOT_PATCH_CHOICE_NO;
3514 printf("n\n");
3515 } else if (strcmp(line, "q") == 0 &&
3516 status == GOT_STATUS_MODIFY) {
3517 *choice = GOT_PATCH_CHOICE_QUIT;
3518 printf("q\n");
3519 } else
3520 printf("invalid response '%s'\n", line);
3521 free(line);
3522 return NULL;
3525 while (resp != 'y' && resp != 'n' && resp != 'q') {
3526 err = show_change(status, path, patch_file, n, nchanges,
3527 a->action);
3528 if (err)
3529 return err;
3530 resp = getchar();
3531 if (resp == '\n')
3532 resp = getchar();
3533 if (status == GOT_STATUS_MODIFY) {
3534 if (resp != 'y' && resp != 'n' && resp != 'q') {
3535 printf("invalid response '%c'\n", resp);
3536 resp = ' ';
3538 } else if (resp != 'y' && resp != 'n') {
3539 printf("invalid response '%c'\n", resp);
3540 resp = ' ';
3544 if (resp == 'y')
3545 *choice = GOT_PATCH_CHOICE_YES;
3546 else if (resp == 'n')
3547 *choice = GOT_PATCH_CHOICE_NO;
3548 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3549 *choice = GOT_PATCH_CHOICE_QUIT;
3551 return NULL;
3555 static const struct got_error *
3556 cmd_revert(int argc, char *argv[])
3558 const struct got_error *error = NULL;
3559 struct got_worktree *worktree = NULL;
3560 struct got_repository *repo = NULL;
3561 char *cwd = NULL, *path = NULL;
3562 struct got_pathlist_head paths;
3563 struct got_pathlist_entry *pe;
3564 int ch, can_recurse = 0, pflag = 0;
3565 FILE *patch_script_file = NULL;
3566 const char *patch_script_path = NULL;
3567 struct choose_patch_arg cpa;
3569 TAILQ_INIT(&paths);
3571 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3572 switch (ch) {
3573 case 'p':
3574 pflag = 1;
3575 break;
3576 case 'F':
3577 patch_script_path = optarg;
3578 break;
3579 case 'R':
3580 can_recurse = 1;
3581 break;
3582 default:
3583 usage_revert();
3584 /* NOTREACHED */
3588 argc -= optind;
3589 argv += optind;
3591 #ifndef PROFILE
3592 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3593 "unveil", NULL) == -1)
3594 err(1, "pledge");
3595 #endif
3596 if (argc < 1)
3597 usage_revert();
3598 if (patch_script_path && !pflag)
3599 errx(1, "-F option can only be used together with -p option");
3601 cwd = getcwd(NULL, 0);
3602 if (cwd == NULL) {
3603 error = got_error_from_errno("getcwd");
3604 goto done;
3606 error = got_worktree_open(&worktree, cwd);
3607 if (error)
3608 goto done;
3610 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3611 if (error != NULL)
3612 goto done;
3614 if (patch_script_path) {
3615 patch_script_file = fopen(patch_script_path, "r");
3616 if (patch_script_file == NULL) {
3617 error = got_error_from_errno2("fopen",
3618 patch_script_path);
3619 goto done;
3622 error = apply_unveil(got_repo_get_path(repo), 1,
3623 got_worktree_get_root_path(worktree));
3624 if (error)
3625 goto done;
3627 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3628 if (error)
3629 goto done;
3631 if (!can_recurse) {
3632 char *ondisk_path;
3633 struct stat sb;
3634 TAILQ_FOREACH(pe, &paths, entry) {
3635 if (asprintf(&ondisk_path, "%s/%s",
3636 got_worktree_get_root_path(worktree),
3637 pe->path) == -1) {
3638 error = got_error_from_errno("asprintf");
3639 goto done;
3641 if (lstat(ondisk_path, &sb) == -1) {
3642 if (errno == ENOENT) {
3643 free(ondisk_path);
3644 continue;
3646 error = got_error_from_errno2("lstat",
3647 ondisk_path);
3648 free(ondisk_path);
3649 goto done;
3651 free(ondisk_path);
3652 if (S_ISDIR(sb.st_mode)) {
3653 error = got_error_msg(GOT_ERR_BAD_PATH,
3654 "reverting directories requires -R option");
3655 goto done;
3660 cpa.patch_script_file = patch_script_file;
3661 cpa.action = "revert";
3662 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3663 pflag ? choose_patch : NULL, &cpa, repo);
3664 if (error)
3665 goto done;
3666 done:
3667 if (patch_script_file && fclose(patch_script_file) == EOF &&
3668 error == NULL)
3669 error = got_error_from_errno2("fclose", patch_script_path);
3670 if (repo)
3671 got_repo_close(repo);
3672 if (worktree)
3673 got_worktree_close(worktree);
3674 free(path);
3675 free(cwd);
3676 return error;
3679 __dead static void
3680 usage_commit(void)
3682 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3683 getprogname());
3684 exit(1);
3687 struct collect_commit_logmsg_arg {
3688 const char *cmdline_log;
3689 const char *editor;
3690 const char *worktree_path;
3691 const char *branch_name;
3692 const char *repo_path;
3693 char *logmsg_path;
3697 static const struct got_error *
3698 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3699 void *arg)
3701 char *initial_content = NULL;
3702 struct got_pathlist_entry *pe;
3703 const struct got_error *err = NULL;
3704 char *template = NULL;
3705 struct collect_commit_logmsg_arg *a = arg;
3706 int fd;
3707 size_t len;
3709 /* if a message was specified on the command line, just use it */
3710 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3711 len = strlen(a->cmdline_log) + 1;
3712 *logmsg = malloc(len + 1);
3713 if (*logmsg == NULL)
3714 return got_error_from_errno("malloc");
3715 strlcpy(*logmsg, a->cmdline_log, len);
3716 return NULL;
3719 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3720 return got_error_from_errno("asprintf");
3722 if (asprintf(&initial_content,
3723 "\n# changes to be committed on branch %s:\n",
3724 a->branch_name) == -1)
3725 return got_error_from_errno("asprintf");
3727 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3728 if (err)
3729 goto done;
3731 dprintf(fd, initial_content);
3733 TAILQ_FOREACH(pe, commitable_paths, entry) {
3734 struct got_commitable *ct = pe->data;
3735 dprintf(fd, "# %c %s\n",
3736 got_commitable_get_status(ct),
3737 got_commitable_get_path(ct));
3739 close(fd);
3741 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3742 done:
3743 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3744 unlink(a->logmsg_path);
3745 free(a->logmsg_path);
3746 a->logmsg_path = NULL;
3748 free(initial_content);
3749 free(template);
3751 /* Editor is done; we can now apply unveil(2) */
3752 if (err == NULL) {
3753 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3754 if (err) {
3755 free(*logmsg);
3756 *logmsg = NULL;
3759 return err;
3762 static const struct got_error *
3763 cmd_commit(int argc, char *argv[])
3765 const struct got_error *error = NULL;
3766 struct got_worktree *worktree = NULL;
3767 struct got_repository *repo = NULL;
3768 char *cwd = NULL, *id_str = NULL;
3769 struct got_object_id *id = NULL;
3770 const char *logmsg = NULL;
3771 const char *author;
3772 struct collect_commit_logmsg_arg cl_arg;
3773 char *editor = NULL;
3774 int ch, rebase_in_progress, histedit_in_progress;
3775 struct got_pathlist_head paths;
3777 TAILQ_INIT(&paths);
3778 cl_arg.logmsg_path = NULL;
3780 while ((ch = getopt(argc, argv, "m:")) != -1) {
3781 switch (ch) {
3782 case 'm':
3783 logmsg = optarg;
3784 break;
3785 default:
3786 usage_commit();
3787 /* NOTREACHED */
3791 argc -= optind;
3792 argv += optind;
3794 #ifndef PROFILE
3795 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3796 "unveil", NULL) == -1)
3797 err(1, "pledge");
3798 #endif
3799 error = get_author(&author);
3800 if (error)
3801 return error;
3803 cwd = getcwd(NULL, 0);
3804 if (cwd == NULL) {
3805 error = got_error_from_errno("getcwd");
3806 goto done;
3808 error = got_worktree_open(&worktree, cwd);
3809 if (error)
3810 goto done;
3812 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3813 if (error)
3814 goto done;
3815 if (rebase_in_progress) {
3816 error = got_error(GOT_ERR_REBASING);
3817 goto done;
3820 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3821 worktree);
3822 if (error)
3823 goto done;
3825 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3826 if (error != NULL)
3827 goto done;
3830 * unveil(2) traverses exec(2); if an editor is used we have
3831 * to apply unveil after the log message has been written.
3833 if (logmsg == NULL || strlen(logmsg) == 0)
3834 error = get_editor(&editor);
3835 else
3836 error = apply_unveil(got_repo_get_path(repo), 0,
3837 got_worktree_get_root_path(worktree));
3838 if (error)
3839 goto done;
3841 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3842 if (error)
3843 goto done;
3845 cl_arg.editor = editor;
3846 cl_arg.cmdline_log = logmsg;
3847 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3848 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3849 if (!histedit_in_progress) {
3850 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3851 error = got_error(GOT_ERR_COMMIT_BRANCH);
3852 goto done;
3854 cl_arg.branch_name += 11;
3856 cl_arg.repo_path = got_repo_get_path(repo);
3857 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3858 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3859 if (error) {
3860 if (cl_arg.logmsg_path)
3861 fprintf(stderr, "%s: log message preserved in %s\n",
3862 getprogname(), cl_arg.logmsg_path);
3863 goto done;
3866 if (cl_arg.logmsg_path)
3867 unlink(cl_arg.logmsg_path);
3869 error = got_object_id_str(&id_str, id);
3870 if (error)
3871 goto done;
3872 printf("Created commit %s\n", id_str);
3873 done:
3874 free(cl_arg.logmsg_path);
3875 if (repo)
3876 got_repo_close(repo);
3877 if (worktree)
3878 got_worktree_close(worktree);
3879 free(cwd);
3880 free(id_str);
3881 free(editor);
3882 return error;
3885 __dead static void
3886 usage_cherrypick(void)
3888 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3889 exit(1);
3892 static const struct got_error *
3893 cmd_cherrypick(int argc, char *argv[])
3895 const struct got_error *error = NULL;
3896 struct got_worktree *worktree = NULL;
3897 struct got_repository *repo = NULL;
3898 char *cwd = NULL, *commit_id_str = NULL;
3899 struct got_object_id *commit_id = NULL;
3900 struct got_commit_object *commit = NULL;
3901 struct got_object_qid *pid;
3902 struct got_reference *head_ref = NULL;
3903 int ch, did_something = 0;
3905 while ((ch = getopt(argc, argv, "")) != -1) {
3906 switch (ch) {
3907 default:
3908 usage_cherrypick();
3909 /* NOTREACHED */
3913 argc -= optind;
3914 argv += optind;
3916 #ifndef PROFILE
3917 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3918 "unveil", NULL) == -1)
3919 err(1, "pledge");
3920 #endif
3921 if (argc != 1)
3922 usage_cherrypick();
3924 cwd = getcwd(NULL, 0);
3925 if (cwd == NULL) {
3926 error = got_error_from_errno("getcwd");
3927 goto done;
3929 error = got_worktree_open(&worktree, cwd);
3930 if (error)
3931 goto done;
3933 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3934 if (error != NULL)
3935 goto done;
3937 error = apply_unveil(got_repo_get_path(repo), 0,
3938 got_worktree_get_root_path(worktree));
3939 if (error)
3940 goto done;
3942 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3943 GOT_OBJ_TYPE_COMMIT, repo);
3944 if (error != NULL) {
3945 struct got_reference *ref;
3946 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3947 goto done;
3948 error = got_ref_open(&ref, repo, argv[0], 0);
3949 if (error != NULL)
3950 goto done;
3951 error = got_ref_resolve(&commit_id, repo, ref);
3952 got_ref_close(ref);
3953 if (error != NULL)
3954 goto done;
3956 error = got_object_id_str(&commit_id_str, commit_id);
3957 if (error)
3958 goto done;
3960 error = got_ref_open(&head_ref, repo,
3961 got_worktree_get_head_ref_name(worktree), 0);
3962 if (error != NULL)
3963 goto done;
3965 error = check_same_branch(commit_id, head_ref, NULL, repo);
3966 if (error) {
3967 if (error->code != GOT_ERR_ANCESTRY)
3968 goto done;
3969 error = NULL;
3970 } else {
3971 error = got_error(GOT_ERR_SAME_BRANCH);
3972 goto done;
3975 error = got_object_open_as_commit(&commit, repo, commit_id);
3976 if (error)
3977 goto done;
3978 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3979 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3980 commit_id, repo, update_progress, &did_something, check_cancelled,
3981 NULL);
3982 if (error != NULL)
3983 goto done;
3985 if (did_something)
3986 printf("Merged commit %s\n", commit_id_str);
3987 done:
3988 if (commit)
3989 got_object_commit_close(commit);
3990 free(commit_id_str);
3991 if (head_ref)
3992 got_ref_close(head_ref);
3993 if (worktree)
3994 got_worktree_close(worktree);
3995 if (repo)
3996 got_repo_close(repo);
3997 return error;
4000 __dead static void
4001 usage_backout(void)
4003 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4004 exit(1);
4007 static const struct got_error *
4008 cmd_backout(int argc, char *argv[])
4010 const struct got_error *error = NULL;
4011 struct got_worktree *worktree = NULL;
4012 struct got_repository *repo = NULL;
4013 char *cwd = NULL, *commit_id_str = NULL;
4014 struct got_object_id *commit_id = NULL;
4015 struct got_commit_object *commit = NULL;
4016 struct got_object_qid *pid;
4017 struct got_reference *head_ref = NULL;
4018 int ch, did_something = 0;
4020 while ((ch = getopt(argc, argv, "")) != -1) {
4021 switch (ch) {
4022 default:
4023 usage_backout();
4024 /* NOTREACHED */
4028 argc -= optind;
4029 argv += optind;
4031 #ifndef PROFILE
4032 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4033 "unveil", NULL) == -1)
4034 err(1, "pledge");
4035 #endif
4036 if (argc != 1)
4037 usage_backout();
4039 cwd = getcwd(NULL, 0);
4040 if (cwd == NULL) {
4041 error = got_error_from_errno("getcwd");
4042 goto done;
4044 error = got_worktree_open(&worktree, cwd);
4045 if (error)
4046 goto done;
4048 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4049 if (error != NULL)
4050 goto done;
4052 error = apply_unveil(got_repo_get_path(repo), 0,
4053 got_worktree_get_root_path(worktree));
4054 if (error)
4055 goto done;
4057 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4058 GOT_OBJ_TYPE_COMMIT, repo);
4059 if (error != NULL) {
4060 struct got_reference *ref;
4061 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4062 goto done;
4063 error = got_ref_open(&ref, repo, argv[0], 0);
4064 if (error != NULL)
4065 goto done;
4066 error = got_ref_resolve(&commit_id, repo, ref);
4067 got_ref_close(ref);
4068 if (error != NULL)
4069 goto done;
4071 error = got_object_id_str(&commit_id_str, commit_id);
4072 if (error)
4073 goto done;
4075 error = got_ref_open(&head_ref, repo,
4076 got_worktree_get_head_ref_name(worktree), 0);
4077 if (error != NULL)
4078 goto done;
4080 error = check_same_branch(commit_id, head_ref, NULL, repo);
4081 if (error)
4082 goto done;
4084 error = got_object_open_as_commit(&commit, repo, commit_id);
4085 if (error)
4086 goto done;
4087 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4088 if (pid == NULL) {
4089 error = got_error(GOT_ERR_ROOT_COMMIT);
4090 goto done;
4093 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4094 update_progress, &did_something, check_cancelled, NULL);
4095 if (error != NULL)
4096 goto done;
4098 if (did_something)
4099 printf("Backed out commit %s\n", commit_id_str);
4100 done:
4101 if (commit)
4102 got_object_commit_close(commit);
4103 free(commit_id_str);
4104 if (head_ref)
4105 got_ref_close(head_ref);
4106 if (worktree)
4107 got_worktree_close(worktree);
4108 if (repo)
4109 got_repo_close(repo);
4110 return error;
4113 __dead static void
4114 usage_rebase(void)
4116 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4117 getprogname());
4118 exit(1);
4121 void
4122 trim_logmsg(char *logmsg, int limit)
4124 char *nl;
4125 size_t len;
4127 len = strlen(logmsg);
4128 if (len > limit)
4129 len = limit;
4130 logmsg[len] = '\0';
4131 nl = strchr(logmsg, '\n');
4132 if (nl)
4133 *nl = '\0';
4136 static const struct got_error *
4137 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4139 const struct got_error *err;
4140 char *logmsg0 = NULL;
4141 const char *s;
4143 err = got_object_commit_get_logmsg(&logmsg0, commit);
4144 if (err)
4145 return err;
4147 s = logmsg0;
4148 while (isspace((unsigned char)s[0]))
4149 s++;
4151 *logmsg = strdup(s);
4152 if (*logmsg == NULL) {
4153 err = got_error_from_errno("strdup");
4154 goto done;
4157 trim_logmsg(*logmsg, limit);
4158 done:
4159 free(logmsg0);
4160 return err;
4163 static const struct got_error *
4164 show_rebase_progress(struct got_commit_object *commit,
4165 struct got_object_id *old_id, struct got_object_id *new_id)
4167 const struct got_error *err;
4168 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4170 err = got_object_id_str(&old_id_str, old_id);
4171 if (err)
4172 goto done;
4174 if (new_id) {
4175 err = got_object_id_str(&new_id_str, new_id);
4176 if (err)
4177 goto done;
4180 old_id_str[12] = '\0';
4181 if (new_id_str)
4182 new_id_str[12] = '\0';
4184 err = get_short_logmsg(&logmsg, 42, commit);
4185 if (err)
4186 goto done;
4188 printf("%s -> %s: %s\n", old_id_str,
4189 new_id_str ? new_id_str : "no-op change", logmsg);
4190 done:
4191 free(old_id_str);
4192 free(new_id_str);
4193 return err;
4196 static const struct got_error *
4197 rebase_progress(void *arg, unsigned char status, const char *path)
4199 unsigned char *rebase_status = arg;
4201 while (path[0] == '/')
4202 path++;
4203 printf("%c %s\n", status, path);
4205 if (*rebase_status == GOT_STATUS_CONFLICT)
4206 return NULL;
4207 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4208 *rebase_status = status;
4209 return NULL;
4212 static const struct got_error *
4213 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4214 struct got_reference *branch, struct got_reference *new_base_branch,
4215 struct got_reference *tmp_branch, struct got_repository *repo)
4217 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4218 return got_worktree_rebase_complete(worktree, fileindex,
4219 new_base_branch, tmp_branch, branch, repo);
4222 static const struct got_error *
4223 rebase_commit(struct got_pathlist_head *merged_paths,
4224 struct got_worktree *worktree, struct got_fileindex *fileindex,
4225 struct got_reference *tmp_branch,
4226 struct got_object_id *commit_id, struct got_repository *repo)
4228 const struct got_error *error;
4229 struct got_commit_object *commit;
4230 struct got_object_id *new_commit_id;
4232 error = got_object_open_as_commit(&commit, repo, commit_id);
4233 if (error)
4234 return error;
4236 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4237 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4238 if (error) {
4239 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4240 goto done;
4241 error = show_rebase_progress(commit, commit_id, NULL);
4242 } else {
4243 error = show_rebase_progress(commit, commit_id, new_commit_id);
4244 free(new_commit_id);
4246 done:
4247 got_object_commit_close(commit);
4248 return error;
4251 struct check_path_prefix_arg {
4252 const char *path_prefix;
4253 size_t len;
4254 int errcode;
4257 static const struct got_error *
4258 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4259 struct got_blob_object *blob2, struct got_object_id *id1,
4260 struct got_object_id *id2, const char *path1, const char *path2,
4261 struct got_repository *repo)
4263 struct check_path_prefix_arg *a = arg;
4265 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4266 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4267 return got_error(a->errcode);
4269 return NULL;
4272 static const struct got_error *
4273 check_path_prefix(struct got_object_id *parent_id,
4274 struct got_object_id *commit_id, const char *path_prefix,
4275 int errcode, struct got_repository *repo)
4277 const struct got_error *err;
4278 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4279 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4280 struct check_path_prefix_arg cpp_arg;
4282 if (got_path_is_root_dir(path_prefix))
4283 return NULL;
4285 err = got_object_open_as_commit(&commit, repo, commit_id);
4286 if (err)
4287 goto done;
4289 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4290 if (err)
4291 goto done;
4293 err = got_object_open_as_tree(&tree1, repo,
4294 got_object_commit_get_tree_id(parent_commit));
4295 if (err)
4296 goto done;
4298 err = got_object_open_as_tree(&tree2, repo,
4299 got_object_commit_get_tree_id(commit));
4300 if (err)
4301 goto done;
4303 cpp_arg.path_prefix = path_prefix;
4304 while (cpp_arg.path_prefix[0] == '/')
4305 cpp_arg.path_prefix++;
4306 cpp_arg.len = strlen(cpp_arg.path_prefix);
4307 cpp_arg.errcode = errcode;
4308 err = got_diff_tree(tree1, tree2, "", "", repo,
4309 check_path_prefix_in_diff, &cpp_arg, 0);
4310 done:
4311 if (tree1)
4312 got_object_tree_close(tree1);
4313 if (tree2)
4314 got_object_tree_close(tree2);
4315 if (commit)
4316 got_object_commit_close(commit);
4317 if (parent_commit)
4318 got_object_commit_close(parent_commit);
4319 return err;
4322 static const struct got_error *
4323 collect_commits(struct got_object_id_queue *commits,
4324 struct got_object_id *initial_commit_id,
4325 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4326 const char *path_prefix, int path_prefix_errcode,
4327 struct got_repository *repo)
4329 const struct got_error *err = NULL;
4330 struct got_commit_graph *graph = NULL;
4331 struct got_object_id *parent_id = NULL;
4332 struct got_object_qid *qid;
4333 struct got_object_id *commit_id = initial_commit_id;
4335 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4336 if (err)
4337 return err;
4339 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4340 if (err)
4341 goto done;
4342 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4343 err = got_commit_graph_iter_next(&parent_id, graph);
4344 if (err) {
4345 if (err->code == GOT_ERR_ITER_COMPLETED) {
4346 err = got_error_msg(GOT_ERR_ANCESTRY,
4347 "ran out of commits to rebase before "
4348 "youngest common ancestor commit has "
4349 "been reached?!?");
4350 goto done;
4351 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4352 goto done;
4353 err = got_commit_graph_fetch_commits(graph, 1, repo);
4354 if (err)
4355 goto done;
4356 } else {
4357 err = check_path_prefix(parent_id, commit_id,
4358 path_prefix, path_prefix_errcode, repo);
4359 if (err)
4360 goto done;
4362 err = got_object_qid_alloc(&qid, commit_id);
4363 if (err)
4364 goto done;
4365 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4366 commit_id = parent_id;
4369 done:
4370 got_commit_graph_close(graph);
4371 return err;
4374 static const struct got_error *
4375 cmd_rebase(int argc, char *argv[])
4377 const struct got_error *error = NULL;
4378 struct got_worktree *worktree = NULL;
4379 struct got_repository *repo = NULL;
4380 struct got_fileindex *fileindex = NULL;
4381 char *cwd = NULL;
4382 struct got_reference *branch = NULL;
4383 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4384 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4385 struct got_object_id *resume_commit_id = NULL;
4386 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4387 struct got_commit_object *commit = NULL;
4388 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4389 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4390 struct got_object_id_queue commits;
4391 struct got_pathlist_head merged_paths;
4392 const struct got_object_id_queue *parent_ids;
4393 struct got_object_qid *qid, *pid;
4395 SIMPLEQ_INIT(&commits);
4396 TAILQ_INIT(&merged_paths);
4398 while ((ch = getopt(argc, argv, "ac")) != -1) {
4399 switch (ch) {
4400 case 'a':
4401 abort_rebase = 1;
4402 break;
4403 case 'c':
4404 continue_rebase = 1;
4405 break;
4406 default:
4407 usage_rebase();
4408 /* NOTREACHED */
4412 argc -= optind;
4413 argv += optind;
4415 #ifndef PROFILE
4416 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4417 "unveil", NULL) == -1)
4418 err(1, "pledge");
4419 #endif
4420 if (abort_rebase && continue_rebase)
4421 usage_rebase();
4422 else if (abort_rebase || continue_rebase) {
4423 if (argc != 0)
4424 usage_rebase();
4425 } else if (argc != 1)
4426 usage_rebase();
4428 cwd = getcwd(NULL, 0);
4429 if (cwd == NULL) {
4430 error = got_error_from_errno("getcwd");
4431 goto done;
4433 error = got_worktree_open(&worktree, cwd);
4434 if (error)
4435 goto done;
4437 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4438 if (error != NULL)
4439 goto done;
4441 error = apply_unveil(got_repo_get_path(repo), 0,
4442 got_worktree_get_root_path(worktree));
4443 if (error)
4444 goto done;
4446 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4447 if (error)
4448 goto done;
4450 if (abort_rebase) {
4451 int did_something;
4452 if (!rebase_in_progress) {
4453 error = got_error(GOT_ERR_NOT_REBASING);
4454 goto done;
4456 error = got_worktree_rebase_continue(&resume_commit_id,
4457 &new_base_branch, &tmp_branch, &branch, &fileindex,
4458 worktree, repo);
4459 if (error)
4460 goto done;
4461 printf("Switching work tree to %s\n",
4462 got_ref_get_symref_target(new_base_branch));
4463 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4464 new_base_branch, update_progress, &did_something);
4465 if (error)
4466 goto done;
4467 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4468 goto done; /* nothing else to do */
4471 if (continue_rebase) {
4472 if (!rebase_in_progress) {
4473 error = got_error(GOT_ERR_NOT_REBASING);
4474 goto done;
4476 error = got_worktree_rebase_continue(&resume_commit_id,
4477 &new_base_branch, &tmp_branch, &branch, &fileindex,
4478 worktree, repo);
4479 if (error)
4480 goto done;
4482 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4483 resume_commit_id, repo);
4484 if (error)
4485 goto done;
4487 yca_id = got_object_id_dup(resume_commit_id);
4488 if (yca_id == NULL) {
4489 error = got_error_from_errno("got_object_id_dup");
4490 goto done;
4492 } else {
4493 error = got_ref_open(&branch, repo, argv[0], 0);
4494 if (error != NULL)
4495 goto done;
4498 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4499 if (error)
4500 goto done;
4502 if (!continue_rebase) {
4503 struct got_object_id *base_commit_id;
4505 base_commit_id = got_worktree_get_base_commit_id(worktree);
4506 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4507 base_commit_id, branch_head_commit_id, repo);
4508 if (error)
4509 goto done;
4510 if (yca_id == NULL) {
4511 error = got_error_msg(GOT_ERR_ANCESTRY,
4512 "specified branch shares no common ancestry "
4513 "with work tree's branch");
4514 goto done;
4517 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4518 if (error) {
4519 if (error->code != GOT_ERR_ANCESTRY)
4520 goto done;
4521 error = NULL;
4522 } else {
4523 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4524 "specified branch resolves to a commit which "
4525 "is already contained in work tree's branch");
4526 goto done;
4528 error = got_worktree_rebase_prepare(&new_base_branch,
4529 &tmp_branch, &fileindex, worktree, branch, repo);
4530 if (error)
4531 goto done;
4534 commit_id = branch_head_commit_id;
4535 error = got_object_open_as_commit(&commit, repo, commit_id);
4536 if (error)
4537 goto done;
4539 parent_ids = got_object_commit_get_parent_ids(commit);
4540 pid = SIMPLEQ_FIRST(parent_ids);
4541 if (pid == NULL) {
4542 if (!continue_rebase) {
4543 int did_something;
4544 error = got_worktree_rebase_abort(worktree, fileindex,
4545 repo, new_base_branch, update_progress,
4546 &did_something);
4547 if (error)
4548 goto done;
4549 printf("Rebase of %s aborted\n",
4550 got_ref_get_name(branch));
4552 error = got_error(GOT_ERR_EMPTY_REBASE);
4553 goto done;
4555 error = collect_commits(&commits, commit_id, pid->id,
4556 yca_id, got_worktree_get_path_prefix(worktree),
4557 GOT_ERR_REBASE_PATH, repo);
4558 got_object_commit_close(commit);
4559 commit = NULL;
4560 if (error)
4561 goto done;
4563 if (SIMPLEQ_EMPTY(&commits)) {
4564 if (continue_rebase)
4565 error = rebase_complete(worktree, fileindex,
4566 branch, new_base_branch, tmp_branch, repo);
4567 else
4568 error = got_error(GOT_ERR_EMPTY_REBASE);
4569 goto done;
4572 pid = NULL;
4573 SIMPLEQ_FOREACH(qid, &commits, entry) {
4574 commit_id = qid->id;
4575 parent_id = pid ? pid->id : yca_id;
4576 pid = qid;
4578 error = got_worktree_rebase_merge_files(&merged_paths,
4579 worktree, fileindex, parent_id, commit_id, repo,
4580 rebase_progress, &rebase_status, check_cancelled, NULL);
4581 if (error)
4582 goto done;
4584 if (rebase_status == GOT_STATUS_CONFLICT) {
4585 got_worktree_rebase_pathlist_free(&merged_paths);
4586 break;
4589 error = rebase_commit(&merged_paths, worktree, fileindex,
4590 tmp_branch, commit_id, repo);
4591 got_worktree_rebase_pathlist_free(&merged_paths);
4592 if (error)
4593 goto done;
4596 if (rebase_status == GOT_STATUS_CONFLICT) {
4597 error = got_worktree_rebase_postpone(worktree, fileindex);
4598 if (error)
4599 goto done;
4600 error = got_error_msg(GOT_ERR_CONFLICTS,
4601 "conflicts must be resolved before rebasing can continue");
4602 } else
4603 error = rebase_complete(worktree, fileindex, branch,
4604 new_base_branch, tmp_branch, repo);
4605 done:
4606 got_object_id_queue_free(&commits);
4607 free(branch_head_commit_id);
4608 free(resume_commit_id);
4609 free(yca_id);
4610 if (commit)
4611 got_object_commit_close(commit);
4612 if (branch)
4613 got_ref_close(branch);
4614 if (new_base_branch)
4615 got_ref_close(new_base_branch);
4616 if (tmp_branch)
4617 got_ref_close(tmp_branch);
4618 if (worktree)
4619 got_worktree_close(worktree);
4620 if (repo)
4621 got_repo_close(repo);
4622 return error;
4625 __dead static void
4626 usage_histedit(void)
4628 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4629 getprogname());
4630 exit(1);
4633 #define GOT_HISTEDIT_PICK 'p'
4634 #define GOT_HISTEDIT_EDIT 'e'
4635 #define GOT_HISTEDIT_FOLD 'f'
4636 #define GOT_HISTEDIT_DROP 'd'
4637 #define GOT_HISTEDIT_MESG 'm'
4639 static struct got_histedit_cmd {
4640 unsigned char code;
4641 const char *name;
4642 const char *desc;
4643 } got_histedit_cmds[] = {
4644 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4645 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4646 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4647 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4648 { GOT_HISTEDIT_MESG, "mesg",
4649 "single-line log message for commit above (open editor if empty)" },
4652 struct got_histedit_list_entry {
4653 TAILQ_ENTRY(got_histedit_list_entry) entry;
4654 struct got_object_id *commit_id;
4655 const struct got_histedit_cmd *cmd;
4656 char *logmsg;
4658 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4660 static const struct got_error *
4661 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4662 FILE *f, struct got_repository *repo)
4664 const struct got_error *err = NULL;
4665 char *logmsg = NULL, *id_str = NULL;
4666 struct got_commit_object *commit = NULL;
4667 int n;
4669 err = got_object_open_as_commit(&commit, repo, commit_id);
4670 if (err)
4671 goto done;
4673 err = get_short_logmsg(&logmsg, 34, commit);
4674 if (err)
4675 goto done;
4677 err = got_object_id_str(&id_str, commit_id);
4678 if (err)
4679 goto done;
4681 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4682 if (n < 0)
4683 err = got_ferror(f, GOT_ERR_IO);
4684 done:
4685 if (commit)
4686 got_object_commit_close(commit);
4687 free(id_str);
4688 free(logmsg);
4689 return err;
4692 static const struct got_error *
4693 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4694 struct got_repository *repo)
4696 const struct got_error *err = NULL;
4697 struct got_object_qid *qid;
4699 if (SIMPLEQ_EMPTY(commits))
4700 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4702 SIMPLEQ_FOREACH(qid, commits, entry) {
4703 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4704 f, repo);
4705 if (err)
4706 break;
4709 return err;
4712 static const struct got_error *
4713 write_cmd_list(FILE *f)
4715 const struct got_error *err = NULL;
4716 int n, i;
4718 n = fprintf(f, "# Available histedit commands:\n");
4719 if (n < 0)
4720 return got_ferror(f, GOT_ERR_IO);
4722 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4723 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4724 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4725 cmd->desc);
4726 if (n < 0) {
4727 err = got_ferror(f, GOT_ERR_IO);
4728 break;
4731 n = fprintf(f, "# Commits will be processed in order from top to "
4732 "bottom of this file.\n");
4733 if (n < 0)
4734 return got_ferror(f, GOT_ERR_IO);
4735 return err;
4738 static const struct got_error *
4739 histedit_syntax_error(int lineno)
4741 static char msg[42];
4742 int ret;
4744 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4745 lineno);
4746 if (ret == -1 || ret >= sizeof(msg))
4747 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4749 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4752 static const struct got_error *
4753 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4754 char *logmsg, struct got_repository *repo)
4756 const struct got_error *err;
4757 struct got_commit_object *folded_commit = NULL;
4758 char *id_str, *folded_logmsg = NULL;
4760 err = got_object_id_str(&id_str, hle->commit_id);
4761 if (err)
4762 return err;
4764 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4765 if (err)
4766 goto done;
4768 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4769 if (err)
4770 goto done;
4771 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4772 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4773 folded_logmsg) == -1) {
4774 err = got_error_from_errno("asprintf");
4775 goto done;
4777 done:
4778 if (folded_commit)
4779 got_object_commit_close(folded_commit);
4780 free(id_str);
4781 free(folded_logmsg);
4782 return err;
4785 static struct got_histedit_list_entry *
4786 get_folded_commits(struct got_histedit_list_entry *hle)
4788 struct got_histedit_list_entry *prev, *folded = NULL;
4790 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4791 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4792 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4793 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4794 folded = prev;
4795 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4798 return folded;
4801 static const struct got_error *
4802 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4803 struct got_repository *repo)
4805 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4806 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4807 const struct got_error *err = NULL;
4808 struct got_commit_object *commit = NULL;
4809 int fd;
4810 struct got_histedit_list_entry *folded = NULL;
4812 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4813 if (err)
4814 return err;
4816 folded = get_folded_commits(hle);
4817 if (folded) {
4818 while (folded != hle) {
4819 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4820 folded = TAILQ_NEXT(folded, entry);
4821 continue;
4823 err = append_folded_commit_msg(&new_msg, folded,
4824 logmsg, repo);
4825 if (err)
4826 goto done;
4827 free(logmsg);
4828 logmsg = new_msg;
4829 folded = TAILQ_NEXT(folded, entry);
4833 err = got_object_id_str(&id_str, hle->commit_id);
4834 if (err)
4835 goto done;
4836 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4837 if (err)
4838 goto done;
4839 if (asprintf(&new_msg,
4840 "%s\n# original log message of commit %s: %s",
4841 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4842 err = got_error_from_errno("asprintf");
4843 goto done;
4845 free(logmsg);
4846 logmsg = new_msg;
4848 err = got_object_id_str(&id_str, hle->commit_id);
4849 if (err)
4850 goto done;
4852 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4853 if (err)
4854 goto done;
4856 dprintf(fd, logmsg);
4857 close(fd);
4859 err = get_editor(&editor);
4860 if (err)
4861 goto done;
4863 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4864 if (err) {
4865 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4866 goto done;
4867 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4869 done:
4870 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4871 err = got_error_from_errno2("unlink", logmsg_path);
4872 free(logmsg_path);
4873 free(logmsg);
4874 free(orig_logmsg);
4875 free(editor);
4876 if (commit)
4877 got_object_commit_close(commit);
4878 return err;
4881 static const struct got_error *
4882 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4883 FILE *f, struct got_repository *repo)
4885 const struct got_error *err = NULL;
4886 char *line = NULL, *p, *end;
4887 size_t size;
4888 ssize_t len;
4889 int lineno = 0, i;
4890 const struct got_histedit_cmd *cmd;
4891 struct got_object_id *commit_id = NULL;
4892 struct got_histedit_list_entry *hle = NULL;
4894 for (;;) {
4895 len = getline(&line, &size, f);
4896 if (len == -1) {
4897 const struct got_error *getline_err;
4898 if (feof(f))
4899 break;
4900 getline_err = got_error_from_errno("getline");
4901 err = got_ferror(f, getline_err->code);
4902 break;
4904 lineno++;
4905 p = line;
4906 while (isspace((unsigned char)p[0]))
4907 p++;
4908 if (p[0] == '#' || p[0] == '\0') {
4909 free(line);
4910 line = NULL;
4911 continue;
4913 cmd = NULL;
4914 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4915 cmd = &got_histedit_cmds[i];
4916 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4917 isspace((unsigned char)p[strlen(cmd->name)])) {
4918 p += strlen(cmd->name);
4919 break;
4921 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4922 p++;
4923 break;
4926 if (i == nitems(got_histedit_cmds)) {
4927 err = histedit_syntax_error(lineno);
4928 break;
4930 while (isspace((unsigned char)p[0]))
4931 p++;
4932 if (cmd->code == GOT_HISTEDIT_MESG) {
4933 if (hle == NULL || hle->logmsg != NULL) {
4934 err = got_error(GOT_ERR_HISTEDIT_CMD);
4935 break;
4937 if (p[0] == '\0') {
4938 err = histedit_edit_logmsg(hle, repo);
4939 if (err)
4940 break;
4941 } else {
4942 hle->logmsg = strdup(p);
4943 if (hle->logmsg == NULL) {
4944 err = got_error_from_errno("strdup");
4945 break;
4948 free(line);
4949 line = NULL;
4950 continue;
4951 } else {
4952 end = p;
4953 while (end[0] && !isspace((unsigned char)end[0]))
4954 end++;
4955 *end = '\0';
4957 err = got_object_resolve_id_str(&commit_id, repo, p);
4958 if (err) {
4959 /* override error code */
4960 err = histedit_syntax_error(lineno);
4961 break;
4964 hle = malloc(sizeof(*hle));
4965 if (hle == NULL) {
4966 err = got_error_from_errno("malloc");
4967 break;
4969 hle->cmd = cmd;
4970 hle->commit_id = commit_id;
4971 hle->logmsg = NULL;
4972 commit_id = NULL;
4973 free(line);
4974 line = NULL;
4975 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4978 free(line);
4979 free(commit_id);
4980 return err;
4983 static const struct got_error *
4984 histedit_check_script(struct got_histedit_list *histedit_cmds,
4985 struct got_object_id_queue *commits, struct got_repository *repo)
4987 const struct got_error *err = NULL;
4988 struct got_object_qid *qid;
4989 struct got_histedit_list_entry *hle;
4990 static char msg[80];
4991 char *id_str;
4993 if (TAILQ_EMPTY(histedit_cmds))
4994 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4995 "histedit script contains no commands");
4996 if (SIMPLEQ_EMPTY(commits))
4997 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4999 SIMPLEQ_FOREACH(qid, commits, entry) {
5000 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5001 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5002 break;
5004 if (hle == NULL) {
5005 err = got_object_id_str(&id_str, qid->id);
5006 if (err)
5007 return err;
5008 snprintf(msg, sizeof(msg),
5009 "commit %s missing from histedit script", id_str);
5010 free(id_str);
5011 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5015 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5016 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5017 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5018 "last commit in histedit script cannot be folded");
5020 return NULL;
5023 static const struct got_error *
5024 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5025 const char *path, struct got_object_id_queue *commits,
5026 struct got_repository *repo)
5028 const struct got_error *err = NULL;
5029 char *editor;
5030 FILE *f = NULL;
5032 err = get_editor(&editor);
5033 if (err)
5034 return err;
5036 if (spawn_editor(editor, path) == -1) {
5037 err = got_error_from_errno("failed spawning editor");
5038 goto done;
5041 f = fopen(path, "r");
5042 if (f == NULL) {
5043 err = got_error_from_errno("fopen");
5044 goto done;
5046 err = histedit_parse_list(histedit_cmds, f, repo);
5047 if (err)
5048 goto done;
5050 err = histedit_check_script(histedit_cmds, commits, repo);
5051 done:
5052 if (f && fclose(f) != 0 && err == NULL)
5053 err = got_error_from_errno("fclose");
5054 free(editor);
5055 return err;
5058 static const struct got_error *
5059 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5060 struct got_object_id_queue *, const char *, struct got_repository *);
5062 static const struct got_error *
5063 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5064 struct got_object_id_queue *commits, struct got_repository *repo)
5066 const struct got_error *err;
5067 FILE *f = NULL;
5068 char *path = NULL;
5070 err = got_opentemp_named(&path, &f, "got-histedit");
5071 if (err)
5072 return err;
5074 err = write_cmd_list(f);
5075 if (err)
5076 goto done;
5078 err = histedit_write_commit_list(commits, f, repo);
5079 if (err)
5080 goto done;
5082 if (fclose(f) != 0) {
5083 err = got_error_from_errno("fclose");
5084 goto done;
5086 f = NULL;
5088 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5089 if (err) {
5090 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5091 err->code != GOT_ERR_HISTEDIT_CMD)
5092 goto done;
5093 err = histedit_edit_list_retry(histedit_cmds, err,
5094 commits, path, repo);
5096 done:
5097 if (f && fclose(f) != 0 && err == NULL)
5098 err = got_error_from_errno("fclose");
5099 if (path && unlink(path) != 0 && err == NULL)
5100 err = got_error_from_errno2("unlink", path);
5101 free(path);
5102 return err;
5105 static const struct got_error *
5106 histedit_save_list(struct got_histedit_list *histedit_cmds,
5107 struct got_worktree *worktree, struct got_repository *repo)
5109 const struct got_error *err = NULL;
5110 char *path = NULL;
5111 FILE *f = NULL;
5112 struct got_histedit_list_entry *hle;
5113 struct got_commit_object *commit = NULL;
5115 err = got_worktree_get_histedit_script_path(&path, worktree);
5116 if (err)
5117 return err;
5119 f = fopen(path, "w");
5120 if (f == NULL) {
5121 err = got_error_from_errno2("fopen", path);
5122 goto done;
5124 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5125 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5126 repo);
5127 if (err)
5128 break;
5130 if (hle->logmsg) {
5131 int n = fprintf(f, "%c %s\n",
5132 GOT_HISTEDIT_MESG, hle->logmsg);
5133 if (n < 0) {
5134 err = got_ferror(f, GOT_ERR_IO);
5135 break;
5139 done:
5140 if (f && fclose(f) != 0 && err == NULL)
5141 err = got_error_from_errno("fclose");
5142 free(path);
5143 if (commit)
5144 got_object_commit_close(commit);
5145 return err;
5148 void
5149 histedit_free_list(struct got_histedit_list *histedit_cmds)
5151 struct got_histedit_list_entry *hle;
5153 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5154 TAILQ_REMOVE(histedit_cmds, hle, entry);
5155 free(hle);
5159 static const struct got_error *
5160 histedit_load_list(struct got_histedit_list *histedit_cmds,
5161 const char *path, struct got_repository *repo)
5163 const struct got_error *err = NULL;
5164 FILE *f = NULL;
5166 f = fopen(path, "r");
5167 if (f == NULL) {
5168 err = got_error_from_errno2("fopen", path);
5169 goto done;
5172 err = histedit_parse_list(histedit_cmds, f, repo);
5173 done:
5174 if (f && fclose(f) != 0 && err == NULL)
5175 err = got_error_from_errno("fclose");
5176 return err;
5179 static const struct got_error *
5180 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5181 const struct got_error *edit_err, struct got_object_id_queue *commits,
5182 const char *path, struct got_repository *repo)
5184 const struct got_error *err = NULL, *prev_err = edit_err;
5185 int resp = ' ';
5187 while (resp != 'c' && resp != 'r' && resp != 'a') {
5188 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5189 "or (a)bort: ", getprogname(), prev_err->msg);
5190 resp = getchar();
5191 if (resp == '\n')
5192 resp = getchar();
5193 if (resp == 'c') {
5194 histedit_free_list(histedit_cmds);
5195 err = histedit_run_editor(histedit_cmds, path, commits,
5196 repo);
5197 if (err) {
5198 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5199 err->code != GOT_ERR_HISTEDIT_CMD)
5200 break;
5201 prev_err = err;
5202 resp = ' ';
5203 continue;
5205 break;
5206 } else if (resp == 'r') {
5207 histedit_free_list(histedit_cmds);
5208 err = histedit_edit_script(histedit_cmds,
5209 commits, repo);
5210 if (err) {
5211 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5212 err->code != GOT_ERR_HISTEDIT_CMD)
5213 break;
5214 prev_err = err;
5215 resp = ' ';
5216 continue;
5218 break;
5219 } else if (resp == 'a') {
5220 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5221 break;
5222 } else
5223 printf("invalid response '%c'\n", resp);
5226 return err;
5229 static const struct got_error *
5230 histedit_complete(struct got_worktree *worktree,
5231 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5232 struct got_reference *branch, struct got_repository *repo)
5234 printf("Switching work tree to %s\n",
5235 got_ref_get_symref_target(branch));
5236 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5237 branch, repo);
5240 static const struct got_error *
5241 show_histedit_progress(struct got_commit_object *commit,
5242 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5244 const struct got_error *err;
5245 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5247 err = got_object_id_str(&old_id_str, hle->commit_id);
5248 if (err)
5249 goto done;
5251 if (new_id) {
5252 err = got_object_id_str(&new_id_str, new_id);
5253 if (err)
5254 goto done;
5257 old_id_str[12] = '\0';
5258 if (new_id_str)
5259 new_id_str[12] = '\0';
5261 if (hle->logmsg) {
5262 logmsg = strdup(hle->logmsg);
5263 if (logmsg == NULL) {
5264 err = got_error_from_errno("strdup");
5265 goto done;
5267 trim_logmsg(logmsg, 42);
5268 } else {
5269 err = get_short_logmsg(&logmsg, 42, commit);
5270 if (err)
5271 goto done;
5274 switch (hle->cmd->code) {
5275 case GOT_HISTEDIT_PICK:
5276 case GOT_HISTEDIT_EDIT:
5277 printf("%s -> %s: %s\n", old_id_str,
5278 new_id_str ? new_id_str : "no-op change", logmsg);
5279 break;
5280 case GOT_HISTEDIT_DROP:
5281 case GOT_HISTEDIT_FOLD:
5282 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5283 logmsg);
5284 break;
5285 default:
5286 break;
5289 done:
5290 free(old_id_str);
5291 free(new_id_str);
5292 return err;
5295 static const struct got_error *
5296 histedit_commit(struct got_pathlist_head *merged_paths,
5297 struct got_worktree *worktree, struct got_fileindex *fileindex,
5298 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5299 struct got_repository *repo)
5301 const struct got_error *err;
5302 struct got_commit_object *commit;
5303 struct got_object_id *new_commit_id;
5305 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5306 && hle->logmsg == NULL) {
5307 err = histedit_edit_logmsg(hle, repo);
5308 if (err)
5309 return err;
5312 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5313 if (err)
5314 return err;
5316 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5317 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5318 hle->logmsg, repo);
5319 if (err) {
5320 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5321 goto done;
5322 err = show_histedit_progress(commit, hle, NULL);
5323 } else {
5324 err = show_histedit_progress(commit, hle, new_commit_id);
5325 free(new_commit_id);
5327 done:
5328 got_object_commit_close(commit);
5329 return err;
5332 static const struct got_error *
5333 histedit_skip_commit(struct got_histedit_list_entry *hle,
5334 struct got_worktree *worktree, struct got_repository *repo)
5336 const struct got_error *error;
5337 struct got_commit_object *commit;
5339 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5340 repo);
5341 if (error)
5342 return error;
5344 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5345 if (error)
5346 return error;
5348 error = show_histedit_progress(commit, hle, NULL);
5349 got_object_commit_close(commit);
5350 return error;
5353 static const struct got_error *
5354 cmd_histedit(int argc, char *argv[])
5356 const struct got_error *error = NULL;
5357 struct got_worktree *worktree = NULL;
5358 struct got_fileindex *fileindex = NULL;
5359 struct got_repository *repo = NULL;
5360 char *cwd = NULL;
5361 struct got_reference *branch = NULL;
5362 struct got_reference *tmp_branch = NULL;
5363 struct got_object_id *resume_commit_id = NULL;
5364 struct got_object_id *base_commit_id = NULL;
5365 struct got_object_id *head_commit_id = NULL;
5366 struct got_commit_object *commit = NULL;
5367 int ch, rebase_in_progress = 0, did_something;
5368 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5369 const char *edit_script_path = NULL;
5370 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5371 struct got_object_id_queue commits;
5372 struct got_pathlist_head merged_paths;
5373 const struct got_object_id_queue *parent_ids;
5374 struct got_object_qid *pid;
5375 struct got_histedit_list histedit_cmds;
5376 struct got_histedit_list_entry *hle;
5378 SIMPLEQ_INIT(&commits);
5379 TAILQ_INIT(&histedit_cmds);
5380 TAILQ_INIT(&merged_paths);
5382 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5383 switch (ch) {
5384 case 'a':
5385 abort_edit = 1;
5386 break;
5387 case 'c':
5388 continue_edit = 1;
5389 break;
5390 case 'F':
5391 edit_script_path = optarg;
5392 break;
5393 default:
5394 usage_histedit();
5395 /* NOTREACHED */
5399 argc -= optind;
5400 argv += optind;
5402 #ifndef PROFILE
5403 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5404 "unveil", NULL) == -1)
5405 err(1, "pledge");
5406 #endif
5407 if (abort_edit && continue_edit)
5408 usage_histedit();
5409 if (argc != 0)
5410 usage_histedit();
5413 * This command cannot apply unveil(2) in all cases because the
5414 * user may choose to run an editor to edit the histedit script
5415 * and to edit individual commit log messages.
5416 * unveil(2) traverses exec(2); if an editor is used we have to
5417 * apply unveil after edit script and log messages have been written.
5418 * XXX TODO: Make use of unveil(2) where possible.
5421 cwd = getcwd(NULL, 0);
5422 if (cwd == NULL) {
5423 error = got_error_from_errno("getcwd");
5424 goto done;
5426 error = got_worktree_open(&worktree, cwd);
5427 if (error)
5428 goto done;
5430 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5431 if (error != NULL)
5432 goto done;
5434 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5435 if (error)
5436 goto done;
5437 if (rebase_in_progress) {
5438 error = got_error(GOT_ERR_REBASING);
5439 goto done;
5442 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5443 if (error)
5444 goto done;
5446 if (edit_in_progress && abort_edit) {
5447 error = got_worktree_histedit_continue(&resume_commit_id,
5448 &tmp_branch, &branch, &base_commit_id, &fileindex,
5449 worktree, repo);
5450 if (error)
5451 goto done;
5452 printf("Switching work tree to %s\n",
5453 got_ref_get_symref_target(branch));
5454 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5455 branch, base_commit_id, update_progress, &did_something);
5456 if (error)
5457 goto done;
5458 printf("Histedit of %s aborted\n",
5459 got_ref_get_symref_target(branch));
5460 goto done; /* nothing else to do */
5461 } else if (abort_edit) {
5462 error = got_error(GOT_ERR_NOT_HISTEDIT);
5463 goto done;
5466 if (continue_edit) {
5467 char *path;
5469 if (!edit_in_progress) {
5470 error = got_error(GOT_ERR_NOT_HISTEDIT);
5471 goto done;
5474 error = got_worktree_get_histedit_script_path(&path, worktree);
5475 if (error)
5476 goto done;
5478 error = histedit_load_list(&histedit_cmds, path, repo);
5479 free(path);
5480 if (error)
5481 goto done;
5483 error = got_worktree_histedit_continue(&resume_commit_id,
5484 &tmp_branch, &branch, &base_commit_id, &fileindex,
5485 worktree, repo);
5486 if (error)
5487 goto done;
5489 error = got_ref_resolve(&head_commit_id, repo, branch);
5490 if (error)
5491 goto done;
5493 error = got_object_open_as_commit(&commit, repo,
5494 head_commit_id);
5495 if (error)
5496 goto done;
5497 parent_ids = got_object_commit_get_parent_ids(commit);
5498 pid = SIMPLEQ_FIRST(parent_ids);
5499 if (pid == NULL) {
5500 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5501 goto done;
5503 error = collect_commits(&commits, head_commit_id, pid->id,
5504 base_commit_id, got_worktree_get_path_prefix(worktree),
5505 GOT_ERR_HISTEDIT_PATH, repo);
5506 got_object_commit_close(commit);
5507 commit = NULL;
5508 if (error)
5509 goto done;
5510 } else {
5511 if (edit_in_progress) {
5512 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5513 goto done;
5516 error = got_ref_open(&branch, repo,
5517 got_worktree_get_head_ref_name(worktree), 0);
5518 if (error != NULL)
5519 goto done;
5521 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5522 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5523 "will not edit commit history of a branch outside "
5524 "the \"refs/heads/\" reference namespace");
5525 goto done;
5528 error = got_ref_resolve(&head_commit_id, repo, branch);
5529 got_ref_close(branch);
5530 branch = NULL;
5531 if (error)
5532 goto done;
5534 error = got_object_open_as_commit(&commit, repo,
5535 head_commit_id);
5536 if (error)
5537 goto done;
5538 parent_ids = got_object_commit_get_parent_ids(commit);
5539 pid = SIMPLEQ_FIRST(parent_ids);
5540 if (pid == NULL) {
5541 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5542 goto done;
5544 error = collect_commits(&commits, head_commit_id, pid->id,
5545 got_worktree_get_base_commit_id(worktree),
5546 got_worktree_get_path_prefix(worktree),
5547 GOT_ERR_HISTEDIT_PATH, repo);
5548 got_object_commit_close(commit);
5549 commit = NULL;
5550 if (error)
5551 goto done;
5553 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5554 &base_commit_id, &fileindex, worktree, repo);
5555 if (error)
5556 goto done;
5558 if (edit_script_path) {
5559 error = histedit_load_list(&histedit_cmds,
5560 edit_script_path, repo);
5561 if (error) {
5562 got_worktree_histedit_abort(worktree, fileindex,
5563 repo, branch, base_commit_id,
5564 update_progress, &did_something);
5565 goto done;
5567 } else {
5568 error = histedit_edit_script(&histedit_cmds, &commits,
5569 repo);
5570 if (error) {
5571 got_worktree_histedit_abort(worktree, fileindex,
5572 repo, branch, base_commit_id,
5573 update_progress, &did_something);
5574 goto done;
5579 error = histedit_save_list(&histedit_cmds, worktree,
5580 repo);
5581 if (error) {
5582 got_worktree_histedit_abort(worktree, fileindex,
5583 repo, branch, base_commit_id,
5584 update_progress, &did_something);
5585 goto done;
5590 error = histedit_check_script(&histedit_cmds, &commits, repo);
5591 if (error)
5592 goto done;
5594 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5595 if (resume_commit_id) {
5596 if (got_object_id_cmp(hle->commit_id,
5597 resume_commit_id) != 0)
5598 continue;
5600 resume_commit_id = NULL;
5601 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5602 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5603 error = histedit_skip_commit(hle, worktree,
5604 repo);
5605 } else {
5606 error = histedit_commit(NULL, worktree,
5607 fileindex, tmp_branch, hle, repo);
5609 if (error)
5610 goto done;
5611 continue;
5614 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5615 error = histedit_skip_commit(hle, worktree, repo);
5616 if (error)
5617 goto done;
5618 continue;
5621 error = got_object_open_as_commit(&commit, repo,
5622 hle->commit_id);
5623 if (error)
5624 goto done;
5625 parent_ids = got_object_commit_get_parent_ids(commit);
5626 pid = SIMPLEQ_FIRST(parent_ids);
5628 error = got_worktree_histedit_merge_files(&merged_paths,
5629 worktree, fileindex, pid->id, hle->commit_id, repo,
5630 rebase_progress, &rebase_status, check_cancelled, NULL);
5631 if (error)
5632 goto done;
5633 got_object_commit_close(commit);
5634 commit = NULL;
5636 if (rebase_status == GOT_STATUS_CONFLICT) {
5637 got_worktree_rebase_pathlist_free(&merged_paths);
5638 break;
5641 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5642 char *id_str;
5643 error = got_object_id_str(&id_str, hle->commit_id);
5644 if (error)
5645 goto done;
5646 printf("Stopping histedit for amending commit %s\n",
5647 id_str);
5648 free(id_str);
5649 got_worktree_rebase_pathlist_free(&merged_paths);
5650 error = got_worktree_histedit_postpone(worktree,
5651 fileindex);
5652 goto done;
5655 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5656 error = histedit_skip_commit(hle, worktree, repo);
5657 if (error)
5658 goto done;
5659 continue;
5662 error = histedit_commit(&merged_paths, worktree, fileindex,
5663 tmp_branch, hle, repo);
5664 got_worktree_rebase_pathlist_free(&merged_paths);
5665 if (error)
5666 goto done;
5669 if (rebase_status == GOT_STATUS_CONFLICT) {
5670 error = got_worktree_histedit_postpone(worktree, fileindex);
5671 if (error)
5672 goto done;
5673 error = got_error_msg(GOT_ERR_CONFLICTS,
5674 "conflicts must be resolved before rebasing can continue");
5675 } else
5676 error = histedit_complete(worktree, fileindex, tmp_branch,
5677 branch, repo);
5678 done:
5679 got_object_id_queue_free(&commits);
5680 histedit_free_list(&histedit_cmds);
5681 free(head_commit_id);
5682 free(base_commit_id);
5683 free(resume_commit_id);
5684 if (commit)
5685 got_object_commit_close(commit);
5686 if (branch)
5687 got_ref_close(branch);
5688 if (tmp_branch)
5689 got_ref_close(tmp_branch);
5690 if (worktree)
5691 got_worktree_close(worktree);
5692 if (repo)
5693 got_repo_close(repo);
5694 return error;
5697 __dead static void
5698 usage_stage(void)
5700 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5701 "[file-path ...]\n",
5702 getprogname());
5703 exit(1);
5706 static const struct got_error *
5707 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5708 const char *path, struct got_object_id *blob_id,
5709 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5711 const struct got_error *err = NULL;
5712 char *id_str = NULL;
5714 if (staged_status != GOT_STATUS_ADD &&
5715 staged_status != GOT_STATUS_MODIFY &&
5716 staged_status != GOT_STATUS_DELETE)
5717 return NULL;
5719 if (staged_status == GOT_STATUS_ADD ||
5720 staged_status == GOT_STATUS_MODIFY)
5721 err = got_object_id_str(&id_str, staged_blob_id);
5722 else
5723 err = got_object_id_str(&id_str, blob_id);
5724 if (err)
5725 return err;
5727 printf("%s %c %s\n", id_str, staged_status, path);
5728 free(id_str);
5729 return NULL;
5732 static const struct got_error *
5733 cmd_stage(int argc, char *argv[])
5735 const struct got_error *error = NULL;
5736 struct got_repository *repo = NULL;
5737 struct got_worktree *worktree = NULL;
5738 char *cwd = NULL;
5739 struct got_pathlist_head paths;
5740 struct got_pathlist_entry *pe;
5741 int ch, list_stage = 0, pflag = 0;
5742 FILE *patch_script_file = NULL;
5743 const char *patch_script_path = NULL;
5744 struct choose_patch_arg cpa;
5746 TAILQ_INIT(&paths);
5748 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5749 switch (ch) {
5750 case 'l':
5751 list_stage = 1;
5752 break;
5753 case 'p':
5754 pflag = 1;
5755 break;
5756 case 'F':
5757 patch_script_path = optarg;
5758 break;
5759 default:
5760 usage_stage();
5761 /* NOTREACHED */
5765 argc -= optind;
5766 argv += optind;
5768 #ifndef PROFILE
5769 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5770 "unveil", NULL) == -1)
5771 err(1, "pledge");
5772 #endif
5773 if (list_stage && (pflag || patch_script_path))
5774 errx(1, "-l option cannot be used with other options");
5775 if (patch_script_path && !pflag)
5776 errx(1, "-F option can only be used together with -p option");
5778 cwd = getcwd(NULL, 0);
5779 if (cwd == NULL) {
5780 error = got_error_from_errno("getcwd");
5781 goto done;
5784 error = got_worktree_open(&worktree, cwd);
5785 if (error)
5786 goto done;
5788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5789 if (error != NULL)
5790 goto done;
5792 if (patch_script_path) {
5793 patch_script_file = fopen(patch_script_path, "r");
5794 if (patch_script_file == NULL) {
5795 error = got_error_from_errno2("fopen",
5796 patch_script_path);
5797 goto done;
5800 error = apply_unveil(got_repo_get_path(repo), 1,
5801 got_worktree_get_root_path(worktree));
5802 if (error)
5803 goto done;
5805 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5806 if (error)
5807 goto done;
5809 if (list_stage)
5810 error = got_worktree_status(worktree, &paths, repo,
5811 print_stage, NULL, check_cancelled, NULL);
5812 else {
5813 cpa.patch_script_file = patch_script_file;
5814 cpa.action = "stage";
5815 error = got_worktree_stage(worktree, &paths,
5816 pflag ? NULL : print_status, NULL,
5817 pflag ? choose_patch : NULL, &cpa, repo);
5819 done:
5820 if (patch_script_file && fclose(patch_script_file) == EOF &&
5821 error == NULL)
5822 error = got_error_from_errno2("fclose", patch_script_path);
5823 if (repo)
5824 got_repo_close(repo);
5825 if (worktree)
5826 got_worktree_close(worktree);
5827 TAILQ_FOREACH(pe, &paths, entry)
5828 free((char *)pe->path);
5829 got_pathlist_free(&paths);
5830 free(cwd);
5831 return error;
5834 __dead static void
5835 usage_unstage(void)
5837 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5838 "[file-path ...]\n",
5839 getprogname());
5840 exit(1);
5844 static const struct got_error *
5845 cmd_unstage(int argc, char *argv[])
5847 const struct got_error *error = NULL;
5848 struct got_repository *repo = NULL;
5849 struct got_worktree *worktree = NULL;
5850 char *cwd = NULL;
5851 struct got_pathlist_head paths;
5852 struct got_pathlist_entry *pe;
5853 int ch, did_something = 0, pflag = 0;
5854 FILE *patch_script_file = NULL;
5855 const char *patch_script_path = NULL;
5856 struct choose_patch_arg cpa;
5858 TAILQ_INIT(&paths);
5860 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5861 switch (ch) {
5862 case 'p':
5863 pflag = 1;
5864 break;
5865 case 'F':
5866 patch_script_path = optarg;
5867 break;
5868 default:
5869 usage_unstage();
5870 /* NOTREACHED */
5874 argc -= optind;
5875 argv += optind;
5877 #ifndef PROFILE
5878 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5879 "unveil", NULL) == -1)
5880 err(1, "pledge");
5881 #endif
5882 if (patch_script_path && !pflag)
5883 errx(1, "-F option can only be used together with -p option");
5885 cwd = getcwd(NULL, 0);
5886 if (cwd == NULL) {
5887 error = got_error_from_errno("getcwd");
5888 goto done;
5891 error = got_worktree_open(&worktree, cwd);
5892 if (error)
5893 goto done;
5895 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5896 if (error != NULL)
5897 goto done;
5899 if (patch_script_path) {
5900 patch_script_file = fopen(patch_script_path, "r");
5901 if (patch_script_file == NULL) {
5902 error = got_error_from_errno2("fopen",
5903 patch_script_path);
5904 goto done;
5908 error = apply_unveil(got_repo_get_path(repo), 1,
5909 got_worktree_get_root_path(worktree));
5910 if (error)
5911 goto done;
5913 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5914 if (error)
5915 goto done;
5917 cpa.patch_script_file = patch_script_file;
5918 cpa.action = "unstage";
5919 error = got_worktree_unstage(worktree, &paths, update_progress,
5920 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5921 done:
5922 if (patch_script_file && fclose(patch_script_file) == EOF &&
5923 error == NULL)
5924 error = got_error_from_errno2("fclose", patch_script_path);
5925 if (repo)
5926 got_repo_close(repo);
5927 if (worktree)
5928 got_worktree_close(worktree);
5929 TAILQ_FOREACH(pe, &paths, entry)
5930 free((char *)pe->path);
5931 got_pathlist_free(&paths);
5932 free(cwd);
5933 return error;