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 *abspath = NULL;
243 editor = getenv("VISUAL");
244 if (editor == NULL)
245 editor = getenv("EDITOR");
247 if (editor) {
248 err = got_path_find_prog(abspath, editor);
249 if (err)
250 return err;
253 if (*abspath == NULL) {
254 *abspath = strdup("/bin/ed");
255 if (*abspath == NULL)
256 return got_error_from_errno("strdup");
259 return NULL;
262 static const struct got_error *
263 apply_unveil(const char *repo_path, int repo_read_only,
264 const char *worktree_path)
266 const struct got_error *err;
268 #ifdef PROFILE
269 if (unveil("gmon.out", "rwc") != 0)
270 return got_error_from_errno2("unveil", "gmon.out");
271 #endif
272 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
273 return got_error_from_errno2("unveil", repo_path);
275 if (worktree_path && unveil(worktree_path, "rwc") != 0)
276 return got_error_from_errno2("unveil", worktree_path);
278 if (unveil("/tmp", "rwc") != 0)
279 return got_error_from_errno2("unveil", "/tmp");
281 err = got_privsep_unveil_exec_helpers();
282 if (err != NULL)
283 return err;
285 if (unveil(NULL, NULL) != 0)
286 return got_error_from_errno("unveil");
288 return NULL;
291 __dead static void
292 usage_init(void)
294 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
295 exit(1);
298 static const struct got_error *
299 cmd_init(int argc, char *argv[])
301 const struct got_error *error = NULL;
302 char *repo_path = NULL;
303 int ch;
305 while ((ch = getopt(argc, argv, "")) != -1) {
306 switch (ch) {
307 default:
308 usage_init();
309 /* NOTREACHED */
313 argc -= optind;
314 argv += optind;
316 #ifndef PROFILE
317 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
318 err(1, "pledge");
319 #endif
320 if (argc != 1)
321 usage_init();
323 repo_path = strdup(argv[0]);
324 if (repo_path == NULL)
325 return got_error_from_errno("strdup");
327 got_path_strip_trailing_slashes(repo_path);
329 error = got_path_mkdir(repo_path);
330 if (error &&
331 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
332 goto done;
334 error = apply_unveil(repo_path, 0, NULL);
335 if (error)
336 goto done;
338 error = got_repo_init(repo_path);
339 if (error != NULL)
340 goto done;
342 done:
343 free(repo_path);
344 return error;
347 __dead static void
348 usage_import(void)
350 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
351 "[-r repository-path] [-I pattern] path\n", getprogname());
352 exit(1);
355 int
356 spawn_editor(const char *editor, const char *file)
358 pid_t pid;
359 sig_t sighup, sigint, sigquit;
360 int st = -1;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
367 case -1:
368 goto doneediting;
369 case 0:
370 execl(editor, editor, file, (char *)NULL);
371 _exit(127);
374 while (waitpid(pid, &st, 0) == -1)
375 if (errno != EINTR)
376 break;
378 doneediting:
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
384 errno = EINTR;
385 return -1;
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content)
395 const struct got_error *err = NULL;
396 char buf[1024];
397 struct stat st, st2;
398 FILE *fp;
399 int content_changed = 0;
400 size_t len;
402 *logmsg = NULL;
404 if (stat(logmsg_path, &st) == -1)
405 return got_error_from_errno2("stat", logmsg_path);
407 if (spawn_editor(editor, logmsg_path) == -1)
408 return got_error_from_errno("failed spawning editor");
410 if (stat(logmsg_path, &st2) == -1)
411 return got_error_from_errno("stat");
413 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
414 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
415 "no changes made to commit message, aborting");
417 *logmsg = malloc(st2.st_size + 1);
418 if (*logmsg == NULL)
419 return got_error_from_errno("malloc");
420 (*logmsg)[0] = '\0';
421 len = 0;
423 fp = fopen(logmsg_path, "r");
424 if (fp == NULL) {
425 err = got_error_from_errno("fopen");
426 goto done;
428 while (fgets(buf, sizeof(buf), fp) != NULL) {
429 if (!content_changed && strcmp(buf, initial_content) != 0)
430 content_changed = 1;
431 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
432 continue; /* remove comments and leading empty lines */
433 len = strlcat(*logmsg, buf, st2.st_size);
435 fclose(fp);
437 while (len > 0 && (*logmsg)[len - 1] == '\n') {
438 (*logmsg)[len - 1] = '\0';
439 len--;
442 if (len == 0 || !content_changed)
443 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
444 "commit message cannot be empty, aborting");
445 done:
446 if (err) {
447 free(*logmsg);
448 *logmsg = NULL;
450 return err;
453 static const struct got_error *
454 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
455 const char *branch_name)
457 char *initial_content = NULL, *logmsg_path = NULL;
458 const struct got_error *err = NULL;
459 int fd;
461 if (asprintf(&initial_content,
462 "\n# %s to be imported to branch %s\n", path_dir,
463 branch_name) == -1)
464 return got_error_from_errno("asprintf");
466 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
467 if (err)
468 goto done;
470 dprintf(fd, initial_content);
471 close(fd);
473 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
474 done:
475 free(initial_content);
476 free(logmsg_path);
477 return err;
480 static const struct got_error *
481 import_progress(void *arg, const char *path)
483 printf("A %s\n", path);
484 return NULL;
487 static const struct got_error *
488 get_author(const char **author)
490 const char *got_author;
492 *author = NULL;
494 got_author = getenv("GOT_AUTHOR");
495 if (got_author == NULL) {
496 /* TODO: Look up user in password database? */
497 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
500 *author = got_author;
502 /*
503 * Really dumb email address check; we're only doing this to
504 * avoid git's object parser breaking on commits we create.
505 */
506 while (*got_author && *got_author != '<')
507 got_author++;
508 if (*got_author != '<')
509 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
510 while (*got_author && *got_author != '@')
511 got_author++;
512 if (*got_author != '@')
513 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
514 while (*got_author && *got_author != '>')
515 got_author++;
516 if (*got_author != '>')
517 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
519 return NULL;
522 static const struct got_error *
523 cmd_import(int argc, char *argv[])
525 const struct got_error *error = NULL;
526 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
527 char *editor = NULL;
528 const char *author;
529 const char *branch_name = "master";
530 char *refname = NULL, *id_str = NULL;
531 struct got_repository *repo = NULL;
532 struct got_reference *branch_ref = NULL, *head_ref = NULL;
533 struct got_object_id *new_commit_id = NULL;
534 int ch;
535 struct got_pathlist_head ignores;
536 struct got_pathlist_entry *pe;
538 TAILQ_INIT(&ignores);
540 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
541 switch (ch) {
542 case 'b':
543 branch_name = optarg;
544 break;
545 case 'm':
546 logmsg = strdup(optarg);
547 if (logmsg == NULL) {
548 error = got_error_from_errno("strdup");
549 goto done;
551 break;
552 case 'r':
553 repo_path = realpath(optarg, NULL);
554 if (repo_path == NULL) {
555 error = got_error_from_errno("realpath");
556 goto done;
558 break;
559 case 'I':
560 if (optarg[0] == '\0')
561 break;
562 error = got_pathlist_insert(&pe, &ignores, optarg,
563 NULL);
564 if (error)
565 goto done;
566 break;
567 default:
568 usage_init();
569 /* NOTREACHED */
573 argc -= optind;
574 argv += optind;
576 #ifndef PROFILE
577 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
578 NULL) == -1)
579 err(1, "pledge");
580 #endif
581 if (argc != 1)
582 usage_import();
584 error = get_author(&author);
585 if (error)
586 return error;
588 if (repo_path == NULL) {
589 repo_path = getcwd(NULL, 0);
590 if (repo_path == NULL)
591 return got_error_from_errno("getcwd");
593 got_path_strip_trailing_slashes(repo_path);
594 error = got_repo_open(&repo, repo_path);
595 if (error)
596 goto done;
598 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
599 error = got_error_from_errno("asprintf");
600 goto done;
603 error = got_ref_open(&branch_ref, repo, refname, 0);
604 if (error) {
605 if (error->code != GOT_ERR_NOT_REF)
606 goto done;
607 } else {
608 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
609 "import target branch already exists");
610 goto done;
613 path_dir = realpath(argv[0], NULL);
614 if (path_dir == NULL) {
615 error = got_error_from_errno("realpath");
616 goto done;
618 got_path_strip_trailing_slashes(path_dir);
620 /*
621 * unveil(2) traverses exec(2); if an editor is used we have
622 * to apply unveil after the log message has been written.
623 */
624 if (logmsg == NULL || strlen(logmsg) == 0) {
625 error = get_editor(&editor);
626 if (error)
627 goto done;
628 error = collect_import_msg(&logmsg, editor, path_dir, refname);
629 if (error)
630 goto done;
633 if (unveil(path_dir, "r") != 0)
634 return got_error_from_errno2("unveil", path_dir);
636 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
637 if (error)
638 goto done;
640 error = got_repo_import(&new_commit_id, path_dir, logmsg,
641 author, &ignores, repo, import_progress, NULL);
642 if (error)
643 goto done;
645 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
646 if (error)
647 goto done;
649 error = got_ref_write(branch_ref, repo);
650 if (error)
651 goto done;
653 error = got_object_id_str(&id_str, new_commit_id);
654 if (error)
655 goto done;
657 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
658 if (error) {
659 if (error->code != GOT_ERR_NOT_REF)
660 goto done;
662 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
663 branch_ref);
664 if (error)
665 goto done;
667 error = got_ref_write(head_ref, repo);
668 if (error)
669 goto done;
672 printf("Created branch %s with commit %s\n",
673 got_ref_get_name(branch_ref), id_str);
674 done:
675 free(repo_path);
676 free(editor);
677 free(refname);
678 free(new_commit_id);
679 free(id_str);
680 if (branch_ref)
681 got_ref_close(branch_ref);
682 if (head_ref)
683 got_ref_close(head_ref);
684 return error;
687 __dead static void
688 usage_checkout(void)
690 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
691 "[-p prefix] repository-path [worktree-path]\n", getprogname());
692 exit(1);
695 static const struct got_error *
696 checkout_progress(void *arg, unsigned char status, const char *path)
698 char *worktree_path = arg;
700 /* Base commit bump happens silently. */
701 if (status == GOT_STATUS_BUMP_BASE)
702 return NULL;
704 while (path[0] == '/')
705 path++;
707 printf("%c %s/%s\n", status, worktree_path, path);
708 return NULL;
711 static const struct got_error *
712 check_cancelled(void *arg)
714 if (sigint_received || sigpipe_received)
715 return got_error(GOT_ERR_CANCELLED);
716 return NULL;
719 static const struct got_error *
720 check_linear_ancestry(struct got_object_id *commit_id,
721 struct got_object_id *base_commit_id, struct got_repository *repo)
723 const struct got_error *err = NULL;
724 struct got_object_id *yca_id;
726 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
727 commit_id, base_commit_id, repo);
728 if (err)
729 return err;
731 if (yca_id == NULL)
732 return got_error(GOT_ERR_ANCESTRY);
734 /*
735 * Require a straight line of history between the target commit
736 * and the work tree's base commit.
738 * Non-linear situations such as this require a rebase:
740 * (commit) D F (base_commit)
741 * \ /
742 * C E
743 * \ /
744 * B (yca)
745 * |
746 * A
748 * 'got update' only handles linear cases:
749 * Update forwards in time: A (base/yca) - B - C - D (commit)
750 * Update backwards in time: D (base) - C - B - A (commit/yca)
751 */
752 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
753 got_object_id_cmp(base_commit_id, yca_id) != 0)
754 return got_error(GOT_ERR_ANCESTRY);
756 free(yca_id);
757 return NULL;
760 static const struct got_error *
761 check_same_branch(struct got_object_id *commit_id,
762 struct got_reference *head_ref, struct got_object_id *yca_id,
763 struct got_repository *repo)
765 const struct got_error *err = NULL;
766 struct got_commit_graph *graph = NULL;
767 struct got_object_id *head_commit_id = NULL;
768 int is_same_branch = 0;
770 err = got_ref_resolve(&head_commit_id, repo, head_ref);
771 if (err)
772 goto done;
774 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
775 is_same_branch = 1;
776 goto done;
778 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
779 is_same_branch = 1;
780 goto done;
783 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
784 if (err)
785 goto done;
787 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
788 if (err)
789 goto done;
791 for (;;) {
792 struct got_object_id *id;
793 err = got_commit_graph_iter_next(&id, graph);
794 if (err) {
795 if (err->code == GOT_ERR_ITER_COMPLETED) {
796 err = NULL;
797 break;
798 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
799 break;
800 err = got_commit_graph_fetch_commits(graph, 1,
801 repo);
802 if (err)
803 break;
806 if (id) {
807 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
808 break;
809 if (got_object_id_cmp(id, commit_id) == 0) {
810 is_same_branch = 1;
811 break;
815 done:
816 if (graph)
817 got_commit_graph_close(graph);
818 free(head_commit_id);
819 if (!err && !is_same_branch)
820 err = got_error(GOT_ERR_ANCESTRY);
821 return err;
824 static const struct got_error *
825 resolve_commit_arg(struct got_object_id **commit_id,
826 const char *commit_id_arg, struct got_repository *repo)
828 const struct got_error *err;
829 struct got_reference *ref;
830 struct got_tag_object *tag;
832 err = got_repo_object_match_tag(&tag, commit_id_arg,
833 GOT_OBJ_TYPE_COMMIT, repo);
834 if (err == NULL) {
835 *commit_id = got_object_id_dup(
836 got_object_tag_get_object_id(tag));
837 if (*commit_id == NULL)
838 err = got_error_from_errno("got_object_id_dup");
839 got_object_tag_close(tag);
840 return err;
841 } else if (err->code != GOT_ERR_NO_OBJ)
842 return err;
844 err = got_ref_open(&ref, repo, commit_id_arg, 0);
845 if (err == NULL) {
846 err = got_ref_resolve(commit_id, repo, ref);
847 got_ref_close(ref);
848 } else {
849 if (err->code != GOT_ERR_NOT_REF)
850 return err;
851 err = got_repo_match_object_id_prefix(commit_id,
852 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
854 return err;
857 static const struct got_error *
858 cmd_checkout(int argc, char *argv[])
860 const struct got_error *error = NULL;
861 struct got_repository *repo = NULL;
862 struct got_reference *head_ref = NULL;
863 struct got_worktree *worktree = NULL;
864 char *repo_path = NULL;
865 char *worktree_path = NULL;
866 const char *path_prefix = "";
867 const char *branch_name = GOT_REF_HEAD;
868 char *commit_id_str = NULL;
869 int ch, same_path_prefix;
870 struct got_pathlist_head paths;
872 TAILQ_INIT(&paths);
874 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
875 switch (ch) {
876 case 'b':
877 branch_name = optarg;
878 break;
879 case 'c':
880 commit_id_str = strdup(optarg);
881 if (commit_id_str == NULL)
882 return got_error_from_errno("strdup");
883 break;
884 case 'p':
885 path_prefix = optarg;
886 break;
887 default:
888 usage_checkout();
889 /* NOTREACHED */
893 argc -= optind;
894 argv += optind;
896 #ifndef PROFILE
897 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
898 "unveil", NULL) == -1)
899 err(1, "pledge");
900 #endif
901 if (argc == 1) {
902 char *cwd, *base, *dotgit;
903 repo_path = realpath(argv[0], NULL);
904 if (repo_path == NULL)
905 return got_error_from_errno2("realpath", argv[0]);
906 cwd = getcwd(NULL, 0);
907 if (cwd == NULL) {
908 error = got_error_from_errno("getcwd");
909 goto done;
911 if (path_prefix[0]) {
912 base = basename(path_prefix);
913 if (base == NULL) {
914 error = got_error_from_errno2("basename",
915 path_prefix);
916 goto done;
918 } else {
919 base = basename(repo_path);
920 if (base == NULL) {
921 error = got_error_from_errno2("basename",
922 repo_path);
923 goto done;
926 dotgit = strstr(base, ".git");
927 if (dotgit)
928 *dotgit = '\0';
929 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
930 error = got_error_from_errno("asprintf");
931 free(cwd);
932 goto done;
934 free(cwd);
935 } else if (argc == 2) {
936 repo_path = realpath(argv[0], NULL);
937 if (repo_path == NULL) {
938 error = got_error_from_errno2("realpath", argv[0]);
939 goto done;
941 worktree_path = realpath(argv[1], NULL);
942 if (worktree_path == NULL) {
943 if (errno != ENOENT) {
944 error = got_error_from_errno2("realpath",
945 argv[1]);
946 goto done;
948 worktree_path = strdup(argv[1]);
949 if (worktree_path == NULL) {
950 error = got_error_from_errno("strdup");
951 goto done;
954 } else
955 usage_checkout();
957 got_path_strip_trailing_slashes(repo_path);
958 got_path_strip_trailing_slashes(worktree_path);
960 error = got_repo_open(&repo, repo_path);
961 if (error != NULL)
962 goto done;
964 /* Pre-create work tree path for unveil(2) */
965 error = got_path_mkdir(worktree_path);
966 if (error) {
967 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
968 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
969 goto done;
970 if (!got_path_dir_is_empty(worktree_path)) {
971 error = got_error_path(worktree_path,
972 GOT_ERR_DIR_NOT_EMPTY);
973 goto done;
977 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
978 if (error)
979 goto done;
981 error = got_ref_open(&head_ref, repo, branch_name, 0);
982 if (error != NULL)
983 goto done;
985 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
986 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
987 goto done;
989 error = got_worktree_open(&worktree, worktree_path);
990 if (error != NULL)
991 goto done;
993 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
994 path_prefix);
995 if (error != NULL)
996 goto done;
997 if (!same_path_prefix) {
998 error = got_error(GOT_ERR_PATH_PREFIX);
999 goto done;
1002 if (commit_id_str) {
1003 struct got_object_id *commit_id;
1004 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1005 if (error)
1006 goto done;
1007 error = check_linear_ancestry(commit_id,
1008 got_worktree_get_base_commit_id(worktree), repo);
1009 if (error != NULL) {
1010 free(commit_id);
1011 goto done;
1013 error = check_same_branch(commit_id, head_ref, NULL, repo);
1014 if (error)
1015 goto done;
1016 error = got_worktree_set_base_commit_id(worktree, repo,
1017 commit_id);
1018 free(commit_id);
1019 if (error)
1020 goto done;
1023 error = got_pathlist_append(&paths, "", NULL);
1024 if (error)
1025 goto done;
1026 error = got_worktree_checkout_files(worktree, &paths, repo,
1027 checkout_progress, worktree_path, check_cancelled, NULL);
1028 if (error != NULL)
1029 goto done;
1031 printf("Now shut up and hack\n");
1033 done:
1034 got_pathlist_free(&paths);
1035 free(commit_id_str);
1036 free(repo_path);
1037 free(worktree_path);
1038 return error;
1041 __dead static void
1042 usage_update(void)
1044 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1045 getprogname());
1046 exit(1);
1049 static const struct got_error *
1050 update_progress(void *arg, unsigned char status, const char *path)
1052 int *did_something = arg;
1054 if (status == GOT_STATUS_EXISTS)
1055 return NULL;
1057 *did_something = 1;
1059 /* Base commit bump happens silently. */
1060 if (status == GOT_STATUS_BUMP_BASE)
1061 return NULL;
1063 while (path[0] == '/')
1064 path++;
1065 printf("%c %s\n", status, path);
1066 return NULL;
1069 static const struct got_error *
1070 switch_head_ref(struct got_reference *head_ref,
1071 struct got_object_id *commit_id, struct got_worktree *worktree,
1072 struct got_repository *repo)
1074 const struct got_error *err = NULL;
1075 char *base_id_str;
1076 int ref_has_moved = 0;
1078 /* Trivial case: switching between two different references. */
1079 if (strcmp(got_ref_get_name(head_ref),
1080 got_worktree_get_head_ref_name(worktree)) != 0) {
1081 printf("Switching work tree from %s to %s\n",
1082 got_worktree_get_head_ref_name(worktree),
1083 got_ref_get_name(head_ref));
1084 return got_worktree_set_head_ref(worktree, head_ref);
1087 err = check_linear_ancestry(commit_id,
1088 got_worktree_get_base_commit_id(worktree), repo);
1089 if (err) {
1090 if (err->code != GOT_ERR_ANCESTRY)
1091 return err;
1092 ref_has_moved = 1;
1094 if (!ref_has_moved)
1095 return NULL;
1097 /* Switching to a rebased branch with the same reference name. */
1098 err = got_object_id_str(&base_id_str,
1099 got_worktree_get_base_commit_id(worktree));
1100 if (err)
1101 return err;
1102 printf("Reference %s now points at a different branch\n",
1103 got_worktree_get_head_ref_name(worktree));
1104 printf("Switching work tree from %s to %s\n", base_id_str,
1105 got_worktree_get_head_ref_name(worktree));
1106 return NULL;
1109 static const struct got_error *
1110 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1112 const struct got_error *err;
1113 int in_progress;
1115 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1116 if (err)
1117 return err;
1118 if (in_progress)
1119 return got_error(GOT_ERR_REBASING);
1121 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1122 if (err)
1123 return err;
1124 if (in_progress)
1125 return got_error(GOT_ERR_HISTEDIT_BUSY);
1127 return NULL;
1130 static const struct got_error *
1131 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1132 char *argv[], struct got_worktree *worktree)
1134 const struct got_error *err = NULL;
1135 char *path;
1136 int i;
1138 if (argc == 0) {
1139 path = strdup("");
1140 if (path == NULL)
1141 return got_error_from_errno("strdup");
1142 return got_pathlist_append(paths, path, NULL);
1145 for (i = 0; i < argc; i++) {
1146 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1147 if (err)
1148 break;
1149 err = got_pathlist_append(paths, path, NULL);
1150 if (err) {
1151 free(path);
1152 break;
1156 return err;
1159 static const struct got_error *
1160 cmd_update(int argc, char *argv[])
1162 const struct got_error *error = NULL;
1163 struct got_repository *repo = NULL;
1164 struct got_worktree *worktree = NULL;
1165 char *worktree_path = NULL;
1166 struct got_object_id *commit_id = NULL;
1167 char *commit_id_str = NULL;
1168 const char *branch_name = NULL;
1169 struct got_reference *head_ref = NULL;
1170 struct got_pathlist_head paths;
1171 struct got_pathlist_entry *pe;
1172 int ch, did_something = 0;
1174 TAILQ_INIT(&paths);
1176 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1177 switch (ch) {
1178 case 'b':
1179 branch_name = optarg;
1180 break;
1181 case 'c':
1182 commit_id_str = strdup(optarg);
1183 if (commit_id_str == NULL)
1184 return got_error_from_errno("strdup");
1185 break;
1186 default:
1187 usage_update();
1188 /* NOTREACHED */
1192 argc -= optind;
1193 argv += optind;
1195 #ifndef PROFILE
1196 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1197 "unveil", NULL) == -1)
1198 err(1, "pledge");
1199 #endif
1200 worktree_path = getcwd(NULL, 0);
1201 if (worktree_path == NULL) {
1202 error = got_error_from_errno("getcwd");
1203 goto done;
1205 error = got_worktree_open(&worktree, worktree_path);
1206 if (error)
1207 goto done;
1209 error = check_rebase_or_histedit_in_progress(worktree);
1210 if (error)
1211 goto done;
1213 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1214 if (error != NULL)
1215 goto done;
1217 error = apply_unveil(got_repo_get_path(repo), 0,
1218 got_worktree_get_root_path(worktree));
1219 if (error)
1220 goto done;
1222 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1223 if (error)
1224 goto done;
1226 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1227 got_worktree_get_head_ref_name(worktree), 0);
1228 if (error != NULL)
1229 goto done;
1230 if (commit_id_str == NULL) {
1231 error = got_ref_resolve(&commit_id, repo, head_ref);
1232 if (error != NULL)
1233 goto done;
1234 error = got_object_id_str(&commit_id_str, commit_id);
1235 if (error != NULL)
1236 goto done;
1237 } else {
1238 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1239 free(commit_id_str);
1240 commit_id_str = NULL;
1241 if (error)
1242 goto done;
1243 error = got_object_id_str(&commit_id_str, commit_id);
1244 if (error)
1245 goto done;
1248 if (branch_name) {
1249 struct got_object_id *head_commit_id;
1250 TAILQ_FOREACH(pe, &paths, entry) {
1251 if (pe->path_len == 0)
1252 continue;
1253 error = got_error_msg(GOT_ERR_BAD_PATH,
1254 "switching between branches requires that "
1255 "the entire work tree gets updated");
1256 goto done;
1258 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1259 if (error)
1260 goto done;
1261 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1262 free(head_commit_id);
1263 if (error != NULL)
1264 goto done;
1265 error = check_same_branch(commit_id, head_ref, NULL, repo);
1266 if (error)
1267 goto done;
1268 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1269 if (error)
1270 goto done;
1271 } else {
1272 error = check_linear_ancestry(commit_id,
1273 got_worktree_get_base_commit_id(worktree), repo);
1274 if (error != NULL) {
1275 if (error->code == GOT_ERR_ANCESTRY)
1276 error = got_error(GOT_ERR_BRANCH_MOVED);
1277 goto done;
1279 error = check_same_branch(commit_id, head_ref, NULL, repo);
1280 if (error)
1281 goto done;
1284 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1285 commit_id) != 0) {
1286 error = got_worktree_set_base_commit_id(worktree, repo,
1287 commit_id);
1288 if (error)
1289 goto done;
1292 error = got_worktree_checkout_files(worktree, &paths, repo,
1293 update_progress, &did_something, check_cancelled, NULL);
1294 if (error != NULL)
1295 goto done;
1297 if (did_something)
1298 printf("Updated to commit %s\n", commit_id_str);
1299 else
1300 printf("Already up-to-date\n");
1301 done:
1302 free(worktree_path);
1303 TAILQ_FOREACH(pe, &paths, entry)
1304 free((char *)pe->path);
1305 got_pathlist_free(&paths);
1306 free(commit_id);
1307 free(commit_id_str);
1308 return error;
1311 static const struct got_error *
1312 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1313 int diff_context, struct got_repository *repo)
1315 const struct got_error *err = NULL;
1316 struct got_tree_object *tree1 = NULL, *tree2;
1317 struct got_object_qid *qid;
1318 char *id_str1 = NULL, *id_str2;
1319 struct got_diff_blob_output_unidiff_arg arg;
1321 err = got_object_open_as_tree(&tree2, repo,
1322 got_object_commit_get_tree_id(commit));
1323 if (err)
1324 return err;
1326 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1327 if (qid != NULL) {
1328 struct got_commit_object *pcommit;
1330 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1331 if (err)
1332 return err;
1334 err = got_object_open_as_tree(&tree1, repo,
1335 got_object_commit_get_tree_id(pcommit));
1336 got_object_commit_close(pcommit);
1337 if (err)
1338 return err;
1340 err = got_object_id_str(&id_str1, qid->id);
1341 if (err)
1342 return err;
1345 err = got_object_id_str(&id_str2, id);
1346 if (err)
1347 goto done;
1349 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1350 arg.diff_context = diff_context;
1351 arg.outfile = stdout;
1352 err = got_diff_tree(tree1, tree2, "", "", repo,
1353 got_diff_blob_output_unidiff, &arg, 1);
1354 done:
1355 if (tree1)
1356 got_object_tree_close(tree1);
1357 got_object_tree_close(tree2);
1358 free(id_str1);
1359 free(id_str2);
1360 return err;
1363 static char *
1364 get_datestr(time_t *time, char *datebuf)
1366 struct tm mytm, *tm;
1367 char *p, *s;
1369 tm = gmtime_r(time, &mytm);
1370 if (tm == NULL)
1371 return NULL;
1372 s = asctime_r(tm, datebuf);
1373 if (s == NULL)
1374 return NULL;
1375 p = strchr(s, '\n');
1376 if (p)
1377 *p = '\0';
1378 return s;
1381 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1383 static const struct got_error *
1384 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1385 struct got_repository *repo, int show_patch, int diff_context,
1386 struct got_reflist_head *refs)
1388 const struct got_error *err = NULL;
1389 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1390 char datebuf[26];
1391 time_t committer_time;
1392 const char *author, *committer;
1393 char *refs_str = NULL;
1394 struct got_reflist_entry *re;
1396 SIMPLEQ_FOREACH(re, refs, entry) {
1397 char *s;
1398 const char *name;
1399 struct got_tag_object *tag = NULL;
1400 int cmp;
1402 name = got_ref_get_name(re->ref);
1403 if (strcmp(name, GOT_REF_HEAD) == 0)
1404 continue;
1405 if (strncmp(name, "refs/", 5) == 0)
1406 name += 5;
1407 if (strncmp(name, "got/", 4) == 0)
1408 continue;
1409 if (strncmp(name, "heads/", 6) == 0)
1410 name += 6;
1411 if (strncmp(name, "remotes/", 8) == 0)
1412 name += 8;
1413 if (strncmp(name, "tags/", 5) == 0) {
1414 err = got_object_open_as_tag(&tag, repo, re->id);
1415 if (err) {
1416 if (err->code != GOT_ERR_OBJ_TYPE)
1417 return err;
1418 /* Ref points at something other than a tag. */
1419 err = NULL;
1420 tag = NULL;
1423 cmp = got_object_id_cmp(tag ?
1424 got_object_tag_get_object_id(tag) : re->id, id);
1425 if (tag)
1426 got_object_tag_close(tag);
1427 if (cmp != 0)
1428 continue;
1429 s = refs_str;
1430 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1431 name) == -1) {
1432 err = got_error_from_errno("asprintf");
1433 free(s);
1434 return err;
1436 free(s);
1438 err = got_object_id_str(&id_str, id);
1439 if (err)
1440 return err;
1442 printf(GOT_COMMIT_SEP_STR);
1443 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1444 refs_str ? refs_str : "", refs_str ? ")" : "");
1445 free(id_str);
1446 id_str = NULL;
1447 free(refs_str);
1448 refs_str = NULL;
1449 printf("from: %s\n", got_object_commit_get_author(commit));
1450 committer_time = got_object_commit_get_committer_time(commit);
1451 datestr = get_datestr(&committer_time, datebuf);
1452 if (datestr)
1453 printf("date: %s UTC\n", datestr);
1454 author = got_object_commit_get_author(commit);
1455 committer = got_object_commit_get_committer(commit);
1456 if (strcmp(author, committer) != 0)
1457 printf("via: %s\n", committer);
1458 if (got_object_commit_get_nparents(commit) > 1) {
1459 const struct got_object_id_queue *parent_ids;
1460 struct got_object_qid *qid;
1461 int n = 1;
1462 parent_ids = got_object_commit_get_parent_ids(commit);
1463 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1464 err = got_object_id_str(&id_str, qid->id);
1465 if (err)
1466 return err;
1467 printf("parent %d: %s\n", n++, id_str);
1468 free(id_str);
1472 err = got_object_commit_get_logmsg(&logmsg0, commit);
1473 if (err)
1474 return err;
1476 logmsg = logmsg0;
1477 do {
1478 line = strsep(&logmsg, "\n");
1479 if (line)
1480 printf(" %s\n", line);
1481 } while (line);
1482 free(logmsg0);
1484 if (show_patch) {
1485 err = print_patch(commit, id, diff_context, repo);
1486 if (err == 0)
1487 printf("\n");
1490 if (fflush(stdout) != 0 && err == NULL)
1491 err = got_error_from_errno("fflush");
1492 return err;
1495 static const struct got_error *
1496 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1497 char *path, int show_patch, int diff_context, int limit,
1498 int first_parent_traversal, struct got_reflist_head *refs)
1500 const struct got_error *err;
1501 struct got_commit_graph *graph;
1503 err = got_commit_graph_open(&graph, root_id, path,
1504 first_parent_traversal, repo);
1505 if (err)
1506 return err;
1507 err = got_commit_graph_iter_start(graph, root_id, repo);
1508 if (err)
1509 goto done;
1510 for (;;) {
1511 struct got_commit_object *commit;
1512 struct got_object_id *id;
1514 if (sigint_received || sigpipe_received)
1515 break;
1517 err = got_commit_graph_iter_next(&id, graph);
1518 if (err) {
1519 if (err->code == GOT_ERR_ITER_COMPLETED) {
1520 err = NULL;
1521 break;
1523 if (err->code != GOT_ERR_ITER_NEED_MORE)
1524 break;
1525 err = got_commit_graph_fetch_commits(graph, 1, repo);
1526 if (err)
1527 break;
1528 else
1529 continue;
1531 if (id == NULL)
1532 break;
1534 err = got_object_open_as_commit(&commit, repo, id);
1535 if (err)
1536 break;
1537 err = print_commit(commit, id, repo, show_patch, diff_context,
1538 refs);
1539 got_object_commit_close(commit);
1540 if (err || (limit && --limit == 0))
1541 break;
1543 done:
1544 got_commit_graph_close(graph);
1545 return err;
1548 __dead static void
1549 usage_log(void)
1551 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1552 "[-r repository-path] [path]\n", getprogname());
1553 exit(1);
1556 static int
1557 get_default_log_limit(void)
1559 const char *got_default_log_limit;
1560 long long n;
1561 const char *errstr;
1563 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1564 if (got_default_log_limit == NULL)
1565 return 0;
1566 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1567 if (errstr != NULL)
1568 return 0;
1569 return n;
1572 static const struct got_error *
1573 cmd_log(int argc, char *argv[])
1575 const struct got_error *error;
1576 struct got_repository *repo = NULL;
1577 struct got_worktree *worktree = NULL;
1578 struct got_commit_object *commit = NULL;
1579 struct got_object_id *id = NULL;
1580 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1581 char *start_commit = NULL;
1582 int diff_context = 3, ch;
1583 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1584 const char *errstr;
1585 struct got_reflist_head refs;
1587 SIMPLEQ_INIT(&refs);
1589 #ifndef PROFILE
1590 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1591 NULL)
1592 == -1)
1593 err(1, "pledge");
1594 #endif
1596 limit = get_default_log_limit();
1598 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1599 switch (ch) {
1600 case 'p':
1601 show_patch = 1;
1602 break;
1603 case 'c':
1604 start_commit = optarg;
1605 break;
1606 case 'C':
1607 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1608 &errstr);
1609 if (errstr != NULL)
1610 err(1, "-C option %s", errstr);
1611 break;
1612 case 'l':
1613 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1614 if (errstr != NULL)
1615 err(1, "-l option %s", errstr);
1616 break;
1617 case 'f':
1618 first_parent_traversal = 1;
1619 break;
1620 case 'r':
1621 repo_path = realpath(optarg, NULL);
1622 if (repo_path == NULL)
1623 err(1, "-r option");
1624 got_path_strip_trailing_slashes(repo_path);
1625 break;
1626 default:
1627 usage_log();
1628 /* NOTREACHED */
1632 argc -= optind;
1633 argv += optind;
1635 cwd = getcwd(NULL, 0);
1636 if (cwd == NULL) {
1637 error = got_error_from_errno("getcwd");
1638 goto done;
1641 error = got_worktree_open(&worktree, cwd);
1642 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1643 goto done;
1644 error = NULL;
1646 if (argc == 0) {
1647 path = strdup("");
1648 if (path == NULL) {
1649 error = got_error_from_errno("strdup");
1650 goto done;
1652 } else if (argc == 1) {
1653 if (worktree) {
1654 error = got_worktree_resolve_path(&path, worktree,
1655 argv[0]);
1656 if (error)
1657 goto done;
1658 } else {
1659 path = strdup(argv[0]);
1660 if (path == NULL) {
1661 error = got_error_from_errno("strdup");
1662 goto done;
1665 } else
1666 usage_log();
1668 if (repo_path == NULL) {
1669 repo_path = worktree ?
1670 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1672 if (repo_path == NULL) {
1673 error = got_error_from_errno("strdup");
1674 goto done;
1677 error = got_repo_open(&repo, repo_path);
1678 if (error != NULL)
1679 goto done;
1681 error = apply_unveil(got_repo_get_path(repo), 1,
1682 worktree ? got_worktree_get_root_path(worktree) : NULL);
1683 if (error)
1684 goto done;
1686 if (start_commit == NULL) {
1687 struct got_reference *head_ref;
1688 error = got_ref_open(&head_ref, repo,
1689 worktree ? got_worktree_get_head_ref_name(worktree)
1690 : GOT_REF_HEAD, 0);
1691 if (error != NULL)
1692 return error;
1693 error = got_ref_resolve(&id, repo, head_ref);
1694 got_ref_close(head_ref);
1695 if (error != NULL)
1696 return error;
1697 error = got_object_open_as_commit(&commit, repo, id);
1698 } else {
1699 struct got_reference *ref;
1700 error = got_ref_open(&ref, repo, start_commit, 0);
1701 if (error == NULL) {
1702 int obj_type;
1703 error = got_ref_resolve(&id, repo, ref);
1704 got_ref_close(ref);
1705 if (error != NULL)
1706 goto done;
1707 error = got_object_get_type(&obj_type, repo, id);
1708 if (error != NULL)
1709 goto done;
1710 if (obj_type == GOT_OBJ_TYPE_TAG) {
1711 struct got_tag_object *tag;
1712 error = got_object_open_as_tag(&tag, repo, id);
1713 if (error != NULL)
1714 goto done;
1715 if (got_object_tag_get_object_type(tag) !=
1716 GOT_OBJ_TYPE_COMMIT) {
1717 got_object_tag_close(tag);
1718 error = got_error(GOT_ERR_OBJ_TYPE);
1719 goto done;
1721 free(id);
1722 id = got_object_id_dup(
1723 got_object_tag_get_object_id(tag));
1724 if (id == NULL)
1725 error = got_error_from_errno(
1726 "got_object_id_dup");
1727 got_object_tag_close(tag);
1728 if (error)
1729 goto done;
1730 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1731 error = got_error(GOT_ERR_OBJ_TYPE);
1732 goto done;
1734 error = got_object_open_as_commit(&commit, repo, id);
1735 if (error != NULL)
1736 goto done;
1738 if (commit == NULL) {
1739 error = got_repo_match_object_id_prefix(&id,
1740 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1741 if (error != NULL)
1742 return error;
1745 if (error != NULL)
1746 goto done;
1748 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1749 if (error != NULL)
1750 goto done;
1751 if (in_repo_path) {
1752 free(path);
1753 path = in_repo_path;
1756 error = got_ref_list(&refs, repo);
1757 if (error)
1758 goto done;
1760 error = print_commits(id, repo, path, show_patch,
1761 diff_context, limit, first_parent_traversal, &refs);
1762 done:
1763 free(path);
1764 free(repo_path);
1765 free(cwd);
1766 free(id);
1767 if (worktree)
1768 got_worktree_close(worktree);
1769 if (repo) {
1770 const struct got_error *repo_error;
1771 repo_error = got_repo_close(repo);
1772 if (error == NULL)
1773 error = repo_error;
1775 got_ref_list_free(&refs);
1776 return error;
1779 __dead static void
1780 usage_diff(void)
1782 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1783 "[object1 object2 | path]\n", getprogname());
1784 exit(1);
1787 struct print_diff_arg {
1788 struct got_repository *repo;
1789 struct got_worktree *worktree;
1790 int diff_context;
1791 const char *id_str;
1792 int header_shown;
1793 int diff_staged;
1796 static const struct got_error *
1797 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1798 const char *path, struct got_object_id *blob_id,
1799 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1801 struct print_diff_arg *a = arg;
1802 const struct got_error *err = NULL;
1803 struct got_blob_object *blob1 = NULL;
1804 FILE *f2 = NULL;
1805 char *abspath = NULL, *label1 = NULL;
1806 struct stat sb;
1808 if (a->diff_staged) {
1809 if (staged_status != GOT_STATUS_MODIFY &&
1810 staged_status != GOT_STATUS_ADD &&
1811 staged_status != GOT_STATUS_DELETE)
1812 return NULL;
1813 } else {
1814 if (staged_status == GOT_STATUS_DELETE)
1815 return NULL;
1816 if (status != GOT_STATUS_MODIFY &&
1817 status != GOT_STATUS_ADD &&
1818 status != GOT_STATUS_DELETE &&
1819 status != GOT_STATUS_CONFLICT)
1820 return NULL;
1823 if (!a->header_shown) {
1824 printf("diff %s %s%s\n", a->id_str,
1825 got_worktree_get_root_path(a->worktree),
1826 a->diff_staged ? " (staged changes)" : "");
1827 a->header_shown = 1;
1830 if (a->diff_staged) {
1831 const char *label1 = NULL, *label2 = NULL;
1832 switch (staged_status) {
1833 case GOT_STATUS_MODIFY:
1834 label1 = path;
1835 label2 = path;
1836 break;
1837 case GOT_STATUS_ADD:
1838 label2 = path;
1839 break;
1840 case GOT_STATUS_DELETE:
1841 label1 = path;
1842 break;
1843 default:
1844 return got_error(GOT_ERR_FILE_STATUS);
1846 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1847 label1, label2, a->diff_context, a->repo, stdout);
1850 if (staged_status == GOT_STATUS_ADD ||
1851 staged_status == GOT_STATUS_MODIFY) {
1852 char *id_str;
1853 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1854 8192);
1855 if (err)
1856 goto done;
1857 err = got_object_id_str(&id_str, staged_blob_id);
1858 if (err)
1859 goto done;
1860 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1861 err = got_error_from_errno("asprintf");
1862 free(id_str);
1863 goto done;
1865 free(id_str);
1866 } else if (status != GOT_STATUS_ADD) {
1867 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1868 if (err)
1869 goto done;
1872 if (status != GOT_STATUS_DELETE) {
1873 if (asprintf(&abspath, "%s/%s",
1874 got_worktree_get_root_path(a->worktree), path) == -1) {
1875 err = got_error_from_errno("asprintf");
1876 goto done;
1879 f2 = fopen(abspath, "r");
1880 if (f2 == NULL) {
1881 err = got_error_from_errno2("fopen", abspath);
1882 goto done;
1884 if (lstat(abspath, &sb) == -1) {
1885 err = got_error_from_errno2("lstat", abspath);
1886 goto done;
1888 } else
1889 sb.st_size = 0;
1891 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1892 a->diff_context, stdout);
1893 done:
1894 if (blob1)
1895 got_object_blob_close(blob1);
1896 if (f2 && fclose(f2) != 0 && err == NULL)
1897 err = got_error_from_errno("fclose");
1898 free(abspath);
1899 return err;
1902 static const struct got_error *
1903 match_object_id(struct got_object_id **id, char **label,
1904 const char *id_str, int obj_type, struct got_repository *repo)
1906 const struct got_error *err;
1907 struct got_tag_object *tag;
1908 struct got_reference *ref = NULL;
1910 *id = NULL;
1911 *label = NULL;
1913 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1914 if (err == NULL) {
1915 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1916 if (*id == NULL)
1917 err = got_error_from_errno("got_object_id_dup");
1918 if (asprintf(label, "refs/tags/%s",
1919 got_object_tag_get_name(tag)) == -1)
1920 err = got_error_from_errno("asprintf");
1921 got_object_tag_close(tag);
1922 return err;
1923 } else if (err->code != GOT_ERR_NO_OBJ)
1924 return err;
1926 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1927 if (err) {
1928 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1929 return err;
1930 err = got_ref_open(&ref, repo, id_str, 0);
1931 if (err != NULL)
1932 goto done;
1933 *label = strdup(got_ref_get_name(ref));
1934 if (*label == NULL) {
1935 err = got_error_from_errno("strdup");
1936 goto done;
1938 err = got_ref_resolve(id, repo, ref);
1939 } else {
1940 err = got_object_id_str(label, *id);
1941 if (*label == NULL) {
1942 err = got_error_from_errno("strdup");
1943 goto done;
1946 done:
1947 if (ref)
1948 got_ref_close(ref);
1949 return err;
1953 static const struct got_error *
1954 cmd_diff(int argc, char *argv[])
1956 const struct got_error *error;
1957 struct got_repository *repo = NULL;
1958 struct got_worktree *worktree = NULL;
1959 char *cwd = NULL, *repo_path = NULL;
1960 struct got_object_id *id1 = NULL, *id2 = NULL;
1961 const char *id_str1 = NULL, *id_str2 = NULL;
1962 char *label1 = NULL, *label2 = NULL;
1963 int type1, type2;
1964 int diff_context = 3, diff_staged = 0, ch;
1965 const char *errstr;
1966 char *path = NULL;
1968 #ifndef PROFILE
1969 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1970 NULL) == -1)
1971 err(1, "pledge");
1972 #endif
1974 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1975 switch (ch) {
1976 case 'C':
1977 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1978 if (errstr != NULL)
1979 err(1, "-C option %s", errstr);
1980 break;
1981 case 'r':
1982 repo_path = realpath(optarg, NULL);
1983 if (repo_path == NULL)
1984 err(1, "-r option");
1985 got_path_strip_trailing_slashes(repo_path);
1986 break;
1987 case 's':
1988 diff_staged = 1;
1989 break;
1990 default:
1991 usage_diff();
1992 /* NOTREACHED */
1996 argc -= optind;
1997 argv += optind;
1999 cwd = getcwd(NULL, 0);
2000 if (cwd == NULL) {
2001 error = got_error_from_errno("getcwd");
2002 goto done;
2004 error = got_worktree_open(&worktree, cwd);
2005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2006 goto done;
2007 if (argc <= 1) {
2008 if (worktree == NULL) {
2009 error = got_error(GOT_ERR_NOT_WORKTREE);
2010 goto done;
2012 if (repo_path)
2013 errx(1,
2014 "-r option can't be used when diffing a work tree");
2015 repo_path = strdup(got_worktree_get_repo_path(worktree));
2016 if (repo_path == NULL) {
2017 error = got_error_from_errno("strdup");
2018 goto done;
2020 if (argc == 1) {
2021 error = got_worktree_resolve_path(&path, worktree,
2022 argv[0]);
2023 if (error)
2024 goto done;
2025 } else {
2026 path = strdup("");
2027 if (path == NULL) {
2028 error = got_error_from_errno("strdup");
2029 goto done;
2032 } else if (argc == 2) {
2033 if (diff_staged)
2034 errx(1, "-s option can't be used when diffing "
2035 "objects in repository");
2036 id_str1 = argv[0];
2037 id_str2 = argv[1];
2038 if (worktree && repo_path == NULL) {
2039 repo_path =
2040 strdup(got_worktree_get_repo_path(worktree));
2041 if (repo_path == NULL) {
2042 error = got_error_from_errno("strdup");
2043 goto done;
2046 } else
2047 usage_diff();
2049 if (repo_path == NULL) {
2050 repo_path = getcwd(NULL, 0);
2051 if (repo_path == NULL)
2052 return got_error_from_errno("getcwd");
2055 error = got_repo_open(&repo, repo_path);
2056 free(repo_path);
2057 if (error != NULL)
2058 goto done;
2060 error = apply_unveil(got_repo_get_path(repo), 1,
2061 worktree ? got_worktree_get_root_path(worktree) : NULL);
2062 if (error)
2063 goto done;
2065 if (argc <= 1) {
2066 struct print_diff_arg arg;
2067 struct got_pathlist_head paths;
2068 char *id_str;
2070 TAILQ_INIT(&paths);
2072 error = got_object_id_str(&id_str,
2073 got_worktree_get_base_commit_id(worktree));
2074 if (error)
2075 goto done;
2076 arg.repo = repo;
2077 arg.worktree = worktree;
2078 arg.diff_context = diff_context;
2079 arg.id_str = id_str;
2080 arg.header_shown = 0;
2081 arg.diff_staged = diff_staged;
2083 error = got_pathlist_append(&paths, path, NULL);
2084 if (error)
2085 goto done;
2087 error = got_worktree_status(worktree, &paths, repo, print_diff,
2088 &arg, check_cancelled, NULL);
2089 free(id_str);
2090 got_pathlist_free(&paths);
2091 goto done;
2094 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2095 if (error)
2096 goto done;
2098 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2099 if (error)
2100 goto done;
2102 error = got_object_get_type(&type1, repo, id1);
2103 if (error)
2104 goto done;
2106 error = got_object_get_type(&type2, repo, id2);
2107 if (error)
2108 goto done;
2110 if (type1 != type2) {
2111 error = got_error(GOT_ERR_OBJ_TYPE);
2112 goto done;
2115 switch (type1) {
2116 case GOT_OBJ_TYPE_BLOB:
2117 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2118 diff_context, repo, stdout);
2119 break;
2120 case GOT_OBJ_TYPE_TREE:
2121 error = got_diff_objects_as_trees(id1, id2, "", "",
2122 diff_context, repo, stdout);
2123 break;
2124 case GOT_OBJ_TYPE_COMMIT:
2125 printf("diff %s %s\n", label1, label2);
2126 error = got_diff_objects_as_commits(id1, id2, diff_context,
2127 repo, stdout);
2128 break;
2129 default:
2130 error = got_error(GOT_ERR_OBJ_TYPE);
2133 done:
2134 free(label1);
2135 free(label2);
2136 free(id1);
2137 free(id2);
2138 free(path);
2139 if (worktree)
2140 got_worktree_close(worktree);
2141 if (repo) {
2142 const struct got_error *repo_error;
2143 repo_error = got_repo_close(repo);
2144 if (error == NULL)
2145 error = repo_error;
2147 return error;
2150 __dead static void
2151 usage_blame(void)
2153 fprintf(stderr,
2154 "usage: %s blame [-c commit] [-r repository-path] path\n",
2155 getprogname());
2156 exit(1);
2159 struct blame_line {
2160 int annotated;
2161 char *id_str;
2162 char *committer;
2163 char datebuf[9]; /* YY-MM-DD + NUL */
2166 struct blame_cb_args {
2167 struct blame_line *lines;
2168 int nlines;
2169 int nlines_prec;
2170 int lineno_cur;
2171 off_t *line_offsets;
2172 FILE *f;
2173 struct got_repository *repo;
2176 static const struct got_error *
2177 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2179 const struct got_error *err = NULL;
2180 struct blame_cb_args *a = arg;
2181 struct blame_line *bline;
2182 char *line = NULL;
2183 size_t linesize = 0;
2184 struct got_commit_object *commit = NULL;
2185 off_t offset;
2186 struct tm tm;
2187 time_t committer_time;
2189 if (nlines != a->nlines ||
2190 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2191 return got_error(GOT_ERR_RANGE);
2193 if (sigint_received)
2194 return got_error(GOT_ERR_ITER_COMPLETED);
2196 if (lineno == -1)
2197 return NULL; /* no change in this commit */
2199 /* Annotate this line. */
2200 bline = &a->lines[lineno - 1];
2201 if (bline->annotated)
2202 return NULL;
2203 err = got_object_id_str(&bline->id_str, id);
2204 if (err)
2205 return err;
2207 err = got_object_open_as_commit(&commit, a->repo, id);
2208 if (err)
2209 goto done;
2211 bline->committer = strdup(got_object_commit_get_committer(commit));
2212 if (bline->committer == NULL) {
2213 err = got_error_from_errno("strdup");
2214 goto done;
2217 committer_time = got_object_commit_get_committer_time(commit);
2218 if (localtime_r(&committer_time, &tm) == NULL)
2219 return got_error_from_errno("localtime_r");
2220 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2221 &tm) >= sizeof(bline->datebuf)) {
2222 err = got_error(GOT_ERR_NO_SPACE);
2223 goto done;
2225 bline->annotated = 1;
2227 /* Print lines annotated so far. */
2228 bline = &a->lines[a->lineno_cur - 1];
2229 if (!bline->annotated)
2230 goto done;
2232 offset = a->line_offsets[a->lineno_cur - 1];
2233 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2234 err = got_error_from_errno("fseeko");
2235 goto done;
2238 while (bline->annotated) {
2239 char *smallerthan, *at, *nl, *committer;
2240 size_t len;
2242 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2243 if (ferror(a->f))
2244 err = got_error_from_errno("getline");
2245 break;
2248 committer = bline->committer;
2249 smallerthan = strchr(committer, '<');
2250 if (smallerthan && smallerthan[1] != '\0')
2251 committer = smallerthan + 1;
2252 at = strchr(committer, '@');
2253 if (at)
2254 *at = '\0';
2255 len = strlen(committer);
2256 if (len >= 9)
2257 committer[8] = '\0';
2259 nl = strchr(line, '\n');
2260 if (nl)
2261 *nl = '\0';
2262 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2263 bline->id_str, bline->datebuf, committer, line);
2265 a->lineno_cur++;
2266 bline = &a->lines[a->lineno_cur - 1];
2268 done:
2269 if (commit)
2270 got_object_commit_close(commit);
2271 free(line);
2272 return err;
2275 static const struct got_error *
2276 cmd_blame(int argc, char *argv[])
2278 const struct got_error *error;
2279 struct got_repository *repo = NULL;
2280 struct got_worktree *worktree = NULL;
2281 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2282 struct got_object_id *obj_id = NULL;
2283 struct got_object_id *commit_id = NULL;
2284 struct got_blob_object *blob = NULL;
2285 char *commit_id_str = NULL;
2286 struct blame_cb_args bca;
2287 int ch, obj_type, i;
2289 #ifndef PROFILE
2290 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2291 NULL) == -1)
2292 err(1, "pledge");
2293 #endif
2295 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2296 switch (ch) {
2297 case 'c':
2298 commit_id_str = optarg;
2299 break;
2300 case 'r':
2301 repo_path = realpath(optarg, NULL);
2302 if (repo_path == NULL)
2303 err(1, "-r option");
2304 got_path_strip_trailing_slashes(repo_path);
2305 break;
2306 default:
2307 usage_blame();
2308 /* NOTREACHED */
2312 argc -= optind;
2313 argv += optind;
2315 if (argc == 1)
2316 path = argv[0];
2317 else
2318 usage_blame();
2320 cwd = getcwd(NULL, 0);
2321 if (cwd == NULL) {
2322 error = got_error_from_errno("getcwd");
2323 goto done;
2325 if (repo_path == NULL) {
2326 error = got_worktree_open(&worktree, cwd);
2327 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2328 goto done;
2329 else
2330 error = NULL;
2331 if (worktree) {
2332 repo_path =
2333 strdup(got_worktree_get_repo_path(worktree));
2334 if (repo_path == NULL)
2335 error = got_error_from_errno("strdup");
2336 if (error)
2337 goto done;
2338 } else {
2339 repo_path = strdup(cwd);
2340 if (repo_path == NULL) {
2341 error = got_error_from_errno("strdup");
2342 goto done;
2347 error = got_repo_open(&repo, repo_path);
2348 if (error != NULL)
2349 goto done;
2351 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2352 if (error)
2353 goto done;
2355 if (worktree) {
2356 const char *prefix = got_worktree_get_path_prefix(worktree);
2357 char *p, *worktree_subdir = cwd +
2358 strlen(got_worktree_get_root_path(worktree));
2359 if (asprintf(&p, "%s%s%s%s%s",
2360 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2361 worktree_subdir, worktree_subdir[0] ? "/" : "",
2362 path) == -1) {
2363 error = got_error_from_errno("asprintf");
2364 goto done;
2366 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2367 free(p);
2368 } else {
2369 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2371 if (error)
2372 goto done;
2374 if (commit_id_str == NULL) {
2375 struct got_reference *head_ref;
2376 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2377 if (error != NULL)
2378 goto done;
2379 error = got_ref_resolve(&commit_id, repo, head_ref);
2380 got_ref_close(head_ref);
2381 if (error != NULL)
2382 goto done;
2383 } else {
2384 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2385 if (error)
2386 goto done;
2389 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2390 if (error)
2391 goto done;
2392 if (obj_id == NULL) {
2393 error = got_error(GOT_ERR_NO_OBJ);
2394 goto done;
2397 error = got_object_get_type(&obj_type, repo, obj_id);
2398 if (error)
2399 goto done;
2401 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2402 error = got_error(GOT_ERR_OBJ_TYPE);
2403 goto done;
2406 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2407 if (error)
2408 goto done;
2409 bca.f = got_opentemp();
2410 if (bca.f == NULL) {
2411 error = got_error_from_errno("got_opentemp");
2412 goto done;
2414 error = got_object_blob_dump_to_file(NULL, &bca.nlines,
2415 &bca.line_offsets, bca.f, blob);
2416 if (error || bca.nlines == 0)
2417 goto done;
2419 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2420 if (bca.lines == NULL) {
2421 error = got_error_from_errno("calloc");
2422 goto done;
2424 bca.lineno_cur = 1;
2425 bca.nlines_prec = 0;
2426 i = bca.nlines;
2427 while (i > 0) {
2428 i /= 10;
2429 bca.nlines_prec++;
2431 bca.repo = repo;
2433 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2434 if (error)
2435 goto done;
2436 done:
2437 free(in_repo_path);
2438 free(repo_path);
2439 free(cwd);
2440 free(commit_id);
2441 free(obj_id);
2442 if (blob)
2443 got_object_blob_close(blob);
2444 if (worktree)
2445 got_worktree_close(worktree);
2446 if (repo) {
2447 const struct got_error *repo_error;
2448 repo_error = got_repo_close(repo);
2449 if (error == NULL)
2450 error = repo_error;
2452 for (i = 0; i < bca.nlines; i++) {
2453 struct blame_line *bline = &bca.lines[i];
2454 free(bline->id_str);
2455 free(bline->committer);
2457 free(bca.lines);
2458 free(bca.line_offsets);
2459 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2460 error = got_error_from_errno("fclose");
2461 return error;
2464 __dead static void
2465 usage_tree(void)
2467 fprintf(stderr,
2468 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2469 getprogname());
2470 exit(1);
2473 static void
2474 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2475 const char *root_path)
2477 int is_root_path = (strcmp(path, root_path) == 0);
2478 const char *modestr = "";
2480 path += strlen(root_path);
2481 while (path[0] == '/')
2482 path++;
2484 if (S_ISLNK(te->mode))
2485 modestr = "@";
2486 else if (S_ISDIR(te->mode))
2487 modestr = "/";
2488 else if (te->mode & S_IXUSR)
2489 modestr = "*";
2491 printf("%s%s%s%s%s\n", id ? id : "", path,
2492 is_root_path ? "" : "/", te->name, modestr);
2495 static const struct got_error *
2496 print_tree(const char *path, struct got_object_id *commit_id,
2497 int show_ids, int recurse, const char *root_path,
2498 struct got_repository *repo)
2500 const struct got_error *err = NULL;
2501 struct got_object_id *tree_id = NULL;
2502 struct got_tree_object *tree = NULL;
2503 const struct got_tree_entries *entries;
2504 struct got_tree_entry *te;
2506 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2507 if (err)
2508 goto done;
2510 err = got_object_open_as_tree(&tree, repo, tree_id);
2511 if (err)
2512 goto done;
2513 entries = got_object_tree_get_entries(tree);
2514 te = SIMPLEQ_FIRST(&entries->head);
2515 while (te) {
2516 char *id = NULL;
2518 if (sigint_received || sigpipe_received)
2519 break;
2521 if (show_ids) {
2522 char *id_str;
2523 err = got_object_id_str(&id_str, te->id);
2524 if (err)
2525 goto done;
2526 if (asprintf(&id, "%s ", id_str) == -1) {
2527 err = got_error_from_errno("asprintf");
2528 free(id_str);
2529 goto done;
2531 free(id_str);
2533 print_entry(te, id, path, root_path);
2534 free(id);
2536 if (recurse && S_ISDIR(te->mode)) {
2537 char *child_path;
2538 if (asprintf(&child_path, "%s%s%s", path,
2539 path[0] == '/' && path[1] == '\0' ? "" : "/",
2540 te->name) == -1) {
2541 err = got_error_from_errno("asprintf");
2542 goto done;
2544 err = print_tree(child_path, commit_id, show_ids, 1,
2545 root_path, repo);
2546 free(child_path);
2547 if (err)
2548 goto done;
2551 te = SIMPLEQ_NEXT(te, entry);
2553 done:
2554 if (tree)
2555 got_object_tree_close(tree);
2556 free(tree_id);
2557 return err;
2560 static const struct got_error *
2561 cmd_tree(int argc, char *argv[])
2563 const struct got_error *error;
2564 struct got_repository *repo = NULL;
2565 struct got_worktree *worktree = NULL;
2566 const char *path;
2567 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2568 struct got_object_id *commit_id = NULL;
2569 char *commit_id_str = NULL;
2570 int show_ids = 0, recurse = 0;
2571 int ch;
2573 #ifndef PROFILE
2574 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2575 NULL) == -1)
2576 err(1, "pledge");
2577 #endif
2579 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2580 switch (ch) {
2581 case 'c':
2582 commit_id_str = optarg;
2583 break;
2584 case 'r':
2585 repo_path = realpath(optarg, NULL);
2586 if (repo_path == NULL)
2587 err(1, "-r option");
2588 got_path_strip_trailing_slashes(repo_path);
2589 break;
2590 case 'i':
2591 show_ids = 1;
2592 break;
2593 case 'R':
2594 recurse = 1;
2595 break;
2596 default:
2597 usage_tree();
2598 /* NOTREACHED */
2602 argc -= optind;
2603 argv += optind;
2605 if (argc == 1)
2606 path = argv[0];
2607 else if (argc > 1)
2608 usage_tree();
2609 else
2610 path = NULL;
2612 cwd = getcwd(NULL, 0);
2613 if (cwd == NULL) {
2614 error = got_error_from_errno("getcwd");
2615 goto done;
2617 if (repo_path == NULL) {
2618 error = got_worktree_open(&worktree, cwd);
2619 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2620 goto done;
2621 else
2622 error = NULL;
2623 if (worktree) {
2624 repo_path =
2625 strdup(got_worktree_get_repo_path(worktree));
2626 if (repo_path == NULL)
2627 error = got_error_from_errno("strdup");
2628 if (error)
2629 goto done;
2630 } else {
2631 repo_path = strdup(cwd);
2632 if (repo_path == NULL) {
2633 error = got_error_from_errno("strdup");
2634 goto done;
2639 error = got_repo_open(&repo, repo_path);
2640 if (error != NULL)
2641 goto done;
2643 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2644 if (error)
2645 goto done;
2647 if (path == NULL) {
2648 if (worktree) {
2649 char *p, *worktree_subdir = cwd +
2650 strlen(got_worktree_get_root_path(worktree));
2651 if (asprintf(&p, "%s/%s",
2652 got_worktree_get_path_prefix(worktree),
2653 worktree_subdir) == -1) {
2654 error = got_error_from_errno("asprintf");
2655 goto done;
2657 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2658 free(p);
2659 if (error)
2660 goto done;
2661 } else
2662 path = "/";
2664 if (in_repo_path == NULL) {
2665 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2666 if (error != NULL)
2667 goto done;
2670 if (commit_id_str == NULL) {
2671 struct got_reference *head_ref;
2672 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2673 if (error != NULL)
2674 goto done;
2675 error = got_ref_resolve(&commit_id, repo, head_ref);
2676 got_ref_close(head_ref);
2677 if (error != NULL)
2678 goto done;
2679 } else {
2680 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2681 if (error)
2682 goto done;
2685 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2686 in_repo_path, repo);
2687 done:
2688 free(in_repo_path);
2689 free(repo_path);
2690 free(cwd);
2691 free(commit_id);
2692 if (worktree)
2693 got_worktree_close(worktree);
2694 if (repo) {
2695 const struct got_error *repo_error;
2696 repo_error = got_repo_close(repo);
2697 if (error == NULL)
2698 error = repo_error;
2700 return error;
2703 __dead static void
2704 usage_status(void)
2706 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2707 exit(1);
2710 static const struct got_error *
2711 print_status(void *arg, unsigned char status, unsigned char staged_status,
2712 const char *path, struct got_object_id *blob_id,
2713 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2715 if (status == staged_status && (status == GOT_STATUS_DELETE))
2716 status = GOT_STATUS_NO_CHANGE;
2717 printf("%c%c %s\n", status, staged_status, path);
2718 return NULL;
2721 static const struct got_error *
2722 cmd_status(int argc, char *argv[])
2724 const struct got_error *error = NULL;
2725 struct got_repository *repo = NULL;
2726 struct got_worktree *worktree = NULL;
2727 char *cwd = NULL;
2728 struct got_pathlist_head paths;
2729 struct got_pathlist_entry *pe;
2730 int ch;
2732 TAILQ_INIT(&paths);
2734 while ((ch = getopt(argc, argv, "")) != -1) {
2735 switch (ch) {
2736 default:
2737 usage_status();
2738 /* NOTREACHED */
2742 argc -= optind;
2743 argv += optind;
2745 #ifndef PROFILE
2746 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2747 NULL) == -1)
2748 err(1, "pledge");
2749 #endif
2750 cwd = getcwd(NULL, 0);
2751 if (cwd == NULL) {
2752 error = got_error_from_errno("getcwd");
2753 goto done;
2756 error = got_worktree_open(&worktree, cwd);
2757 if (error != NULL)
2758 goto done;
2760 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2761 if (error != NULL)
2762 goto done;
2764 error = apply_unveil(got_repo_get_path(repo), 1,
2765 got_worktree_get_root_path(worktree));
2766 if (error)
2767 goto done;
2769 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2770 if (error)
2771 goto done;
2773 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2774 check_cancelled, NULL);
2775 done:
2776 TAILQ_FOREACH(pe, &paths, entry)
2777 free((char *)pe->path);
2778 got_pathlist_free(&paths);
2779 free(cwd);
2780 return error;
2783 __dead static void
2784 usage_ref(void)
2786 fprintf(stderr,
2787 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2788 getprogname());
2789 exit(1);
2792 static const struct got_error *
2793 list_refs(struct got_repository *repo)
2795 static const struct got_error *err = NULL;
2796 struct got_reflist_head refs;
2797 struct got_reflist_entry *re;
2799 SIMPLEQ_INIT(&refs);
2800 err = got_ref_list(&refs, repo);
2801 if (err)
2802 return err;
2804 SIMPLEQ_FOREACH(re, &refs, entry) {
2805 char *refstr;
2806 refstr = got_ref_to_str(re->ref);
2807 if (refstr == NULL)
2808 return got_error_from_errno("got_ref_to_str");
2809 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2810 free(refstr);
2813 got_ref_list_free(&refs);
2814 return NULL;
2817 static const struct got_error *
2818 delete_ref(struct got_repository *repo, const char *refname)
2820 const struct got_error *err = NULL;
2821 struct got_reference *ref;
2823 err = got_ref_open(&ref, repo, refname, 0);
2824 if (err)
2825 return err;
2827 err = got_ref_delete(ref, repo);
2828 got_ref_close(ref);
2829 return err;
2832 static const struct got_error *
2833 add_ref(struct got_repository *repo, const char *refname, const char *target)
2835 const struct got_error *err = NULL;
2836 struct got_object_id *id;
2837 struct got_reference *ref = NULL;
2840 * Don't let the user create a reference named '-'.
2841 * While technically a valid reference name, this case is usually
2842 * an unintended typo.
2844 if (refname[0] == '-' && refname[1] == '\0')
2845 return got_error(GOT_ERR_BAD_REF_NAME);
2847 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2848 repo);
2849 if (err) {
2850 struct got_reference *target_ref;
2852 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2853 return err;
2854 err = got_ref_open(&target_ref, repo, target, 0);
2855 if (err)
2856 return err;
2857 err = got_ref_resolve(&id, repo, target_ref);
2858 got_ref_close(target_ref);
2859 if (err)
2860 return err;
2863 err = got_ref_alloc(&ref, refname, id);
2864 if (err)
2865 goto done;
2867 err = got_ref_write(ref, repo);
2868 done:
2869 if (ref)
2870 got_ref_close(ref);
2871 free(id);
2872 return err;
2875 static const struct got_error *
2876 add_symref(struct got_repository *repo, const char *refname, const char *target)
2878 const struct got_error *err = NULL;
2879 struct got_reference *ref = NULL;
2880 struct got_reference *target_ref = NULL;
2883 * Don't let the user create a reference named '-'.
2884 * While technically a valid reference name, this case is usually
2885 * an unintended typo.
2887 if (refname[0] == '-' && refname[1] == '\0')
2888 return got_error(GOT_ERR_BAD_REF_NAME);
2890 err = got_ref_open(&target_ref, repo, target, 0);
2891 if (err)
2892 return err;
2894 err = got_ref_alloc_symref(&ref, refname, target_ref);
2895 if (err)
2896 goto done;
2898 err = got_ref_write(ref, repo);
2899 done:
2900 if (target_ref)
2901 got_ref_close(target_ref);
2902 if (ref)
2903 got_ref_close(ref);
2904 return err;
2907 static const struct got_error *
2908 cmd_ref(int argc, char *argv[])
2910 const struct got_error *error = NULL;
2911 struct got_repository *repo = NULL;
2912 struct got_worktree *worktree = NULL;
2913 char *cwd = NULL, *repo_path = NULL;
2914 int ch, do_list = 0, create_symref = 0;
2915 const char *delref = NULL;
2917 /* TODO: Add -s option for adding symbolic references. */
2918 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2919 switch (ch) {
2920 case 'd':
2921 delref = optarg;
2922 break;
2923 case 'r':
2924 repo_path = realpath(optarg, NULL);
2925 if (repo_path == NULL)
2926 err(1, "-r option");
2927 got_path_strip_trailing_slashes(repo_path);
2928 break;
2929 case 'l':
2930 do_list = 1;
2931 break;
2932 case 's':
2933 create_symref = 1;
2934 break;
2935 default:
2936 usage_ref();
2937 /* NOTREACHED */
2941 if (do_list && delref)
2942 errx(1, "-l and -d options are mutually exclusive\n");
2944 argc -= optind;
2945 argv += optind;
2947 if (do_list || delref) {
2948 if (create_symref)
2949 errx(1, "-s option cannot be used together with the "
2950 "-l or -d options");
2951 if (argc > 0)
2952 usage_ref();
2953 } else if (argc != 2)
2954 usage_ref();
2956 #ifndef PROFILE
2957 if (do_list) {
2958 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2959 NULL) == -1)
2960 err(1, "pledge");
2961 } else {
2962 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2963 "sendfd unveil", NULL) == -1)
2964 err(1, "pledge");
2966 #endif
2967 cwd = getcwd(NULL, 0);
2968 if (cwd == NULL) {
2969 error = got_error_from_errno("getcwd");
2970 goto done;
2973 if (repo_path == NULL) {
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2976 goto done;
2977 else
2978 error = NULL;
2979 if (worktree) {
2980 repo_path =
2981 strdup(got_worktree_get_repo_path(worktree));
2982 if (repo_path == NULL)
2983 error = got_error_from_errno("strdup");
2984 if (error)
2985 goto done;
2986 } else {
2987 repo_path = strdup(cwd);
2988 if (repo_path == NULL) {
2989 error = got_error_from_errno("strdup");
2990 goto done;
2995 error = got_repo_open(&repo, repo_path);
2996 if (error != NULL)
2997 goto done;
2999 error = apply_unveil(got_repo_get_path(repo), do_list,
3000 worktree ? got_worktree_get_root_path(worktree) : NULL);
3001 if (error)
3002 goto done;
3004 if (do_list)
3005 error = list_refs(repo);
3006 else if (delref)
3007 error = delete_ref(repo, delref);
3008 else if (create_symref)
3009 error = add_symref(repo, argv[0], argv[1]);
3010 else
3011 error = add_ref(repo, argv[0], argv[1]);
3012 done:
3013 if (repo)
3014 got_repo_close(repo);
3015 if (worktree)
3016 got_worktree_close(worktree);
3017 free(cwd);
3018 free(repo_path);
3019 return error;
3022 __dead static void
3023 usage_branch(void)
3025 fprintf(stderr,
3026 "usage: %s branch [-r repository] -l | -d name | "
3027 "name [base-branch]\n", getprogname());
3028 exit(1);
3031 static const struct got_error *
3032 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3034 static const struct got_error *err = NULL;
3035 struct got_reflist_head refs;
3036 struct got_reflist_entry *re;
3038 SIMPLEQ_INIT(&refs);
3040 err = got_ref_list(&refs, repo);
3041 if (err)
3042 return err;
3044 SIMPLEQ_FOREACH(re, &refs, entry) {
3045 const char *refname, *marker = " ";
3046 char *refstr;
3047 refname = got_ref_get_name(re->ref);
3048 if (strncmp(refname, "refs/heads/", 11) != 0)
3049 continue;
3050 if (worktree && strcmp(refname,
3051 got_worktree_get_head_ref_name(worktree)) == 0) {
3052 struct got_object_id *id = NULL;
3053 err = got_ref_resolve(&id, repo, re->ref);
3054 if (err)
3055 return err;
3056 if (got_object_id_cmp(id,
3057 got_worktree_get_base_commit_id(worktree)) == 0)
3058 marker = "* ";
3059 else
3060 marker = "~ ";
3061 free(id);
3063 refname += 11;
3064 refstr = got_ref_to_str(re->ref);
3065 if (refstr == NULL)
3066 return got_error_from_errno("got_ref_to_str");
3067 printf("%s%s: %s\n", marker, refname, refstr);
3068 free(refstr);
3071 got_ref_list_free(&refs);
3072 return NULL;
3075 static const struct got_error *
3076 delete_branch(struct got_repository *repo, const char *branch_name)
3078 const struct got_error *err = NULL;
3079 struct got_reference *ref;
3080 char *refname;
3082 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3083 return got_error_from_errno("asprintf");
3085 err = got_ref_open(&ref, repo, refname, 0);
3086 if (err)
3087 goto done;
3089 err = got_ref_delete(ref, repo);
3090 got_ref_close(ref);
3091 done:
3092 free(refname);
3093 return err;
3096 static const struct got_error *
3097 add_branch(struct got_repository *repo, const char *branch_name,
3098 const char *base_branch)
3100 const struct got_error *err = NULL;
3101 struct got_object_id *id = NULL;
3102 struct got_reference *ref = NULL;
3103 char *base_refname = NULL, *refname = NULL;
3104 struct got_reference *base_ref;
3107 * Don't let the user create a branch named '-'.
3108 * While technically a valid reference name, this case is usually
3109 * an unintended typo.
3111 if (branch_name[0] == '-' && branch_name[1] == '\0')
3112 return got_error(GOT_ERR_BAD_REF_NAME);
3114 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3115 base_refname = strdup(GOT_REF_HEAD);
3116 if (base_refname == NULL)
3117 return got_error_from_errno("strdup");
3118 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3119 return got_error_from_errno("asprintf");
3121 err = got_ref_open(&base_ref, repo, base_refname, 0);
3122 if (err)
3123 goto done;
3124 err = got_ref_resolve(&id, repo, base_ref);
3125 got_ref_close(base_ref);
3126 if (err)
3127 goto done;
3129 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3130 err = got_error_from_errno("asprintf");
3131 goto done;
3134 err = got_ref_open(&ref, repo, refname, 0);
3135 if (err == NULL) {
3136 err = got_error(GOT_ERR_BRANCH_EXISTS);
3137 goto done;
3138 } else if (err->code != GOT_ERR_NOT_REF)
3139 goto done;
3141 err = got_ref_alloc(&ref, refname, id);
3142 if (err)
3143 goto done;
3145 err = got_ref_write(ref, repo);
3146 done:
3147 if (ref)
3148 got_ref_close(ref);
3149 free(id);
3150 free(base_refname);
3151 free(refname);
3152 return err;
3155 static const struct got_error *
3156 cmd_branch(int argc, char *argv[])
3158 const struct got_error *error = NULL;
3159 struct got_repository *repo = NULL;
3160 struct got_worktree *worktree = NULL;
3161 char *cwd = NULL, *repo_path = NULL;
3162 int ch, do_list = 0;
3163 const char *delref = NULL;
3165 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3166 switch (ch) {
3167 case 'd':
3168 delref = optarg;
3169 break;
3170 case 'r':
3171 repo_path = realpath(optarg, NULL);
3172 if (repo_path == NULL)
3173 err(1, "-r option");
3174 got_path_strip_trailing_slashes(repo_path);
3175 break;
3176 case 'l':
3177 do_list = 1;
3178 break;
3179 default:
3180 usage_branch();
3181 /* NOTREACHED */
3185 if (do_list && delref)
3186 errx(1, "-l and -d options are mutually exclusive\n");
3188 argc -= optind;
3189 argv += optind;
3191 if (do_list || delref) {
3192 if (argc > 0)
3193 usage_branch();
3194 } else if (argc < 1 || argc > 2)
3195 usage_branch();
3197 #ifndef PROFILE
3198 if (do_list) {
3199 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3200 NULL) == -1)
3201 err(1, "pledge");
3202 } else {
3203 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3204 "sendfd unveil", NULL) == -1)
3205 err(1, "pledge");
3207 #endif
3208 cwd = getcwd(NULL, 0);
3209 if (cwd == NULL) {
3210 error = got_error_from_errno("getcwd");
3211 goto done;
3214 if (repo_path == NULL) {
3215 error = got_worktree_open(&worktree, cwd);
3216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3217 goto done;
3218 else
3219 error = NULL;
3220 if (worktree) {
3221 repo_path =
3222 strdup(got_worktree_get_repo_path(worktree));
3223 if (repo_path == NULL)
3224 error = got_error_from_errno("strdup");
3225 if (error)
3226 goto done;
3227 } else {
3228 repo_path = strdup(cwd);
3229 if (repo_path == NULL) {
3230 error = got_error_from_errno("strdup");
3231 goto done;
3236 error = got_repo_open(&repo, repo_path);
3237 if (error != NULL)
3238 goto done;
3240 error = apply_unveil(got_repo_get_path(repo), do_list,
3241 worktree ? got_worktree_get_root_path(worktree) : NULL);
3242 if (error)
3243 goto done;
3245 if (do_list)
3246 error = list_branches(repo, worktree);
3247 else if (delref)
3248 error = delete_branch(repo, delref);
3249 else {
3250 const char *base_branch;
3251 if (argc == 1) {
3252 base_branch = worktree ?
3253 got_worktree_get_head_ref_name(worktree) :
3254 GOT_REF_HEAD;
3255 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3256 base_branch += 11;
3257 } else
3258 base_branch = argv[1];
3259 error = add_branch(repo, argv[0], base_branch);
3261 done:
3262 if (repo)
3263 got_repo_close(repo);
3264 if (worktree)
3265 got_worktree_close(worktree);
3266 free(cwd);
3267 free(repo_path);
3268 return error;
3271 __dead static void
3272 usage_add(void)
3274 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3275 exit(1);
3278 static const struct got_error *
3279 cmd_add(int argc, char *argv[])
3281 const struct got_error *error = NULL;
3282 struct got_repository *repo = NULL;
3283 struct got_worktree *worktree = NULL;
3284 char *cwd = NULL;
3285 struct got_pathlist_head paths;
3286 struct got_pathlist_entry *pe;
3287 int ch;
3289 TAILQ_INIT(&paths);
3291 while ((ch = getopt(argc, argv, "")) != -1) {
3292 switch (ch) {
3293 default:
3294 usage_add();
3295 /* NOTREACHED */
3299 argc -= optind;
3300 argv += optind;
3302 #ifndef PROFILE
3303 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3304 NULL) == -1)
3305 err(1, "pledge");
3306 #endif
3307 if (argc < 1)
3308 usage_add();
3310 cwd = getcwd(NULL, 0);
3311 if (cwd == NULL) {
3312 error = got_error_from_errno("getcwd");
3313 goto done;
3316 error = got_worktree_open(&worktree, cwd);
3317 if (error)
3318 goto done;
3320 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3321 if (error != NULL)
3322 goto done;
3324 error = apply_unveil(got_repo_get_path(repo), 1,
3325 got_worktree_get_root_path(worktree));
3326 if (error)
3327 goto done;
3329 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3330 if (error)
3331 goto done;
3333 error = got_worktree_schedule_add(worktree, &paths, print_status,
3334 NULL, repo);
3335 done:
3336 if (repo)
3337 got_repo_close(repo);
3338 if (worktree)
3339 got_worktree_close(worktree);
3340 TAILQ_FOREACH(pe, &paths, entry)
3341 free((char *)pe->path);
3342 got_pathlist_free(&paths);
3343 free(cwd);
3344 return error;
3347 __dead static void
3348 usage_remove(void)
3350 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3351 exit(1);
3354 static const struct got_error *
3355 cmd_remove(int argc, char *argv[])
3357 const struct got_error *error = NULL;
3358 struct got_worktree *worktree = NULL;
3359 struct got_repository *repo = NULL;
3360 char *cwd = NULL;
3361 struct got_pathlist_head paths;
3362 struct got_pathlist_entry *pe;
3363 int ch, delete_local_mods = 0;
3365 TAILQ_INIT(&paths);
3367 while ((ch = getopt(argc, argv, "f")) != -1) {
3368 switch (ch) {
3369 case 'f':
3370 delete_local_mods = 1;
3371 break;
3372 default:
3373 usage_add();
3374 /* NOTREACHED */
3378 argc -= optind;
3379 argv += optind;
3381 #ifndef PROFILE
3382 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3383 NULL) == -1)
3384 err(1, "pledge");
3385 #endif
3386 if (argc < 1)
3387 usage_remove();
3389 cwd = getcwd(NULL, 0);
3390 if (cwd == NULL) {
3391 error = got_error_from_errno("getcwd");
3392 goto done;
3394 error = got_worktree_open(&worktree, cwd);
3395 if (error)
3396 goto done;
3398 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3399 if (error)
3400 goto done;
3402 error = apply_unveil(got_repo_get_path(repo), 1,
3403 got_worktree_get_root_path(worktree));
3404 if (error)
3405 goto done;
3407 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3408 if (error)
3409 goto done;
3411 error = got_worktree_schedule_delete(worktree, &paths,
3412 delete_local_mods, print_status, NULL, repo);
3413 if (error)
3414 goto done;
3415 done:
3416 if (repo)
3417 got_repo_close(repo);
3418 if (worktree)
3419 got_worktree_close(worktree);
3420 TAILQ_FOREACH(pe, &paths, entry)
3421 free((char *)pe->path);
3422 got_pathlist_free(&paths);
3423 free(cwd);
3424 return error;
3427 __dead static void
3428 usage_revert(void)
3430 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3431 "path ...\n", getprogname());
3432 exit(1);
3435 static const struct got_error *
3436 revert_progress(void *arg, unsigned char status, const char *path)
3438 while (path[0] == '/')
3439 path++;
3440 printf("%c %s\n", status, path);
3441 return NULL;
3444 struct choose_patch_arg {
3445 FILE *patch_script_file;
3446 const char *action;
3449 static const struct got_error *
3450 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3451 int nchanges, const char *action)
3453 char *line = NULL;
3454 size_t linesize = 0;
3455 ssize_t linelen;
3457 switch (status) {
3458 case GOT_STATUS_ADD:
3459 printf("A %s\n%s this addition? [y/n] ", path, action);
3460 break;
3461 case GOT_STATUS_DELETE:
3462 printf("D %s\n%s this deletion? [y/n] ", path, action);
3463 break;
3464 case GOT_STATUS_MODIFY:
3465 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3466 return got_error_from_errno("fseek");
3467 printf(GOT_COMMIT_SEP_STR);
3468 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3469 printf("%s", line);
3470 if (ferror(patch_file))
3471 return got_error_from_errno("getline");
3472 printf(GOT_COMMIT_SEP_STR);
3473 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3474 path, n, nchanges, action);
3475 break;
3476 default:
3477 return got_error_path(path, GOT_ERR_FILE_STATUS);
3480 return NULL;
3483 static const struct got_error *
3484 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3485 FILE *patch_file, int n, int nchanges)
3487 const struct got_error *err = NULL;
3488 char *line = NULL;
3489 size_t linesize = 0;
3490 ssize_t linelen;
3491 int resp = ' ';
3492 struct choose_patch_arg *a = arg;
3494 *choice = GOT_PATCH_CHOICE_NONE;
3496 if (a->patch_script_file) {
3497 char *nl;
3498 err = show_change(status, path, patch_file, n, nchanges,
3499 a->action);
3500 if (err)
3501 return err;
3502 linelen = getline(&line, &linesize, a->patch_script_file);
3503 if (linelen == -1) {
3504 if (ferror(a->patch_script_file))
3505 return got_error_from_errno("getline");
3506 return NULL;
3508 nl = strchr(line, '\n');
3509 if (nl)
3510 *nl = '\0';
3511 if (strcmp(line, "y") == 0) {
3512 *choice = GOT_PATCH_CHOICE_YES;
3513 printf("y\n");
3514 } else if (strcmp(line, "n") == 0) {
3515 *choice = GOT_PATCH_CHOICE_NO;
3516 printf("n\n");
3517 } else if (strcmp(line, "q") == 0 &&
3518 status == GOT_STATUS_MODIFY) {
3519 *choice = GOT_PATCH_CHOICE_QUIT;
3520 printf("q\n");
3521 } else
3522 printf("invalid response '%s'\n", line);
3523 free(line);
3524 return NULL;
3527 while (resp != 'y' && resp != 'n' && resp != 'q') {
3528 err = show_change(status, path, patch_file, n, nchanges,
3529 a->action);
3530 if (err)
3531 return err;
3532 resp = getchar();
3533 if (resp == '\n')
3534 resp = getchar();
3535 if (status == GOT_STATUS_MODIFY) {
3536 if (resp != 'y' && resp != 'n' && resp != 'q') {
3537 printf("invalid response '%c'\n", resp);
3538 resp = ' ';
3540 } else if (resp != 'y' && resp != 'n') {
3541 printf("invalid response '%c'\n", resp);
3542 resp = ' ';
3546 if (resp == 'y')
3547 *choice = GOT_PATCH_CHOICE_YES;
3548 else if (resp == 'n')
3549 *choice = GOT_PATCH_CHOICE_NO;
3550 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3551 *choice = GOT_PATCH_CHOICE_QUIT;
3553 return NULL;
3557 static const struct got_error *
3558 cmd_revert(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct got_worktree *worktree = NULL;
3562 struct got_repository *repo = NULL;
3563 char *cwd = NULL, *path = NULL;
3564 struct got_pathlist_head paths;
3565 struct got_pathlist_entry *pe;
3566 int ch, can_recurse = 0, pflag = 0;
3567 FILE *patch_script_file = NULL;
3568 const char *patch_script_path = NULL;
3569 struct choose_patch_arg cpa;
3571 TAILQ_INIT(&paths);
3573 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3574 switch (ch) {
3575 case 'p':
3576 pflag = 1;
3577 break;
3578 case 'F':
3579 patch_script_path = optarg;
3580 break;
3581 case 'R':
3582 can_recurse = 1;
3583 break;
3584 default:
3585 usage_revert();
3586 /* NOTREACHED */
3590 argc -= optind;
3591 argv += optind;
3593 #ifndef PROFILE
3594 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3595 "unveil", NULL) == -1)
3596 err(1, "pledge");
3597 #endif
3598 if (argc < 1)
3599 usage_revert();
3600 if (patch_script_path && !pflag)
3601 errx(1, "-F option can only be used together with -p option");
3603 cwd = getcwd(NULL, 0);
3604 if (cwd == NULL) {
3605 error = got_error_from_errno("getcwd");
3606 goto done;
3608 error = got_worktree_open(&worktree, cwd);
3609 if (error)
3610 goto done;
3612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3613 if (error != NULL)
3614 goto done;
3616 if (patch_script_path) {
3617 patch_script_file = fopen(patch_script_path, "r");
3618 if (patch_script_file == NULL) {
3619 error = got_error_from_errno2("fopen",
3620 patch_script_path);
3621 goto done;
3624 error = apply_unveil(got_repo_get_path(repo), 1,
3625 got_worktree_get_root_path(worktree));
3626 if (error)
3627 goto done;
3629 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3630 if (error)
3631 goto done;
3633 if (!can_recurse) {
3634 char *ondisk_path;
3635 struct stat sb;
3636 TAILQ_FOREACH(pe, &paths, entry) {
3637 if (asprintf(&ondisk_path, "%s/%s",
3638 got_worktree_get_root_path(worktree),
3639 pe->path) == -1) {
3640 error = got_error_from_errno("asprintf");
3641 goto done;
3643 if (lstat(ondisk_path, &sb) == -1) {
3644 if (errno == ENOENT) {
3645 free(ondisk_path);
3646 continue;
3648 error = got_error_from_errno2("lstat",
3649 ondisk_path);
3650 free(ondisk_path);
3651 goto done;
3653 free(ondisk_path);
3654 if (S_ISDIR(sb.st_mode)) {
3655 error = got_error_msg(GOT_ERR_BAD_PATH,
3656 "reverting directories requires -R option");
3657 goto done;
3662 cpa.patch_script_file = patch_script_file;
3663 cpa.action = "revert";
3664 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3665 pflag ? choose_patch : NULL, &cpa, repo);
3666 if (error)
3667 goto done;
3668 done:
3669 if (patch_script_file && fclose(patch_script_file) == EOF &&
3670 error == NULL)
3671 error = got_error_from_errno2("fclose", patch_script_path);
3672 if (repo)
3673 got_repo_close(repo);
3674 if (worktree)
3675 got_worktree_close(worktree);
3676 free(path);
3677 free(cwd);
3678 return error;
3681 __dead static void
3682 usage_commit(void)
3684 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3685 getprogname());
3686 exit(1);
3689 struct collect_commit_logmsg_arg {
3690 const char *cmdline_log;
3691 const char *editor;
3692 const char *worktree_path;
3693 const char *branch_name;
3694 const char *repo_path;
3695 char *logmsg_path;
3699 static const struct got_error *
3700 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3701 void *arg)
3703 char *initial_content = NULL;
3704 struct got_pathlist_entry *pe;
3705 const struct got_error *err = NULL;
3706 char *template = NULL;
3707 struct collect_commit_logmsg_arg *a = arg;
3708 int fd;
3709 size_t len;
3711 /* if a message was specified on the command line, just use it */
3712 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3713 len = strlen(a->cmdline_log) + 1;
3714 *logmsg = malloc(len + 1);
3715 if (*logmsg == NULL)
3716 return got_error_from_errno("malloc");
3717 strlcpy(*logmsg, a->cmdline_log, len);
3718 return NULL;
3721 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3722 return got_error_from_errno("asprintf");
3724 if (asprintf(&initial_content,
3725 "\n# changes to be committed on branch %s:\n",
3726 a->branch_name) == -1)
3727 return got_error_from_errno("asprintf");
3729 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3730 if (err)
3731 goto done;
3733 dprintf(fd, initial_content);
3735 TAILQ_FOREACH(pe, commitable_paths, entry) {
3736 struct got_commitable *ct = pe->data;
3737 dprintf(fd, "# %c %s\n",
3738 got_commitable_get_status(ct),
3739 got_commitable_get_path(ct));
3741 close(fd);
3743 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3744 done:
3745 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3746 unlink(a->logmsg_path);
3747 free(a->logmsg_path);
3748 a->logmsg_path = NULL;
3750 free(initial_content);
3751 free(template);
3753 /* Editor is done; we can now apply unveil(2) */
3754 if (err == NULL) {
3755 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3756 if (err) {
3757 free(*logmsg);
3758 *logmsg = NULL;
3761 return err;
3764 static const struct got_error *
3765 cmd_commit(int argc, char *argv[])
3767 const struct got_error *error = NULL;
3768 struct got_worktree *worktree = NULL;
3769 struct got_repository *repo = NULL;
3770 char *cwd = NULL, *id_str = NULL;
3771 struct got_object_id *id = NULL;
3772 const char *logmsg = NULL;
3773 const char *author;
3774 struct collect_commit_logmsg_arg cl_arg;
3775 char *editor = NULL;
3776 int ch, rebase_in_progress, histedit_in_progress;
3777 struct got_pathlist_head paths;
3779 TAILQ_INIT(&paths);
3780 cl_arg.logmsg_path = NULL;
3782 while ((ch = getopt(argc, argv, "m:")) != -1) {
3783 switch (ch) {
3784 case 'm':
3785 logmsg = optarg;
3786 break;
3787 default:
3788 usage_commit();
3789 /* NOTREACHED */
3793 argc -= optind;
3794 argv += optind;
3796 #ifndef PROFILE
3797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3798 "unveil", NULL) == -1)
3799 err(1, "pledge");
3800 #endif
3801 error = get_author(&author);
3802 if (error)
3803 return error;
3805 cwd = getcwd(NULL, 0);
3806 if (cwd == NULL) {
3807 error = got_error_from_errno("getcwd");
3808 goto done;
3810 error = got_worktree_open(&worktree, cwd);
3811 if (error)
3812 goto done;
3814 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3815 if (error)
3816 goto done;
3817 if (rebase_in_progress) {
3818 error = got_error(GOT_ERR_REBASING);
3819 goto done;
3822 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3823 worktree);
3824 if (error)
3825 goto done;
3827 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3828 if (error != NULL)
3829 goto done;
3832 * unveil(2) traverses exec(2); if an editor is used we have
3833 * to apply unveil after the log message has been written.
3835 if (logmsg == NULL || strlen(logmsg) == 0)
3836 error = get_editor(&editor);
3837 else
3838 error = apply_unveil(got_repo_get_path(repo), 0,
3839 got_worktree_get_root_path(worktree));
3840 if (error)
3841 goto done;
3843 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3844 if (error)
3845 goto done;
3847 cl_arg.editor = editor;
3848 cl_arg.cmdline_log = logmsg;
3849 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3850 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3851 if (!histedit_in_progress) {
3852 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3853 error = got_error(GOT_ERR_COMMIT_BRANCH);
3854 goto done;
3856 cl_arg.branch_name += 11;
3858 cl_arg.repo_path = got_repo_get_path(repo);
3859 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3860 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3861 if (error) {
3862 if (cl_arg.logmsg_path)
3863 fprintf(stderr, "%s: log message preserved in %s\n",
3864 getprogname(), cl_arg.logmsg_path);
3865 goto done;
3868 if (cl_arg.logmsg_path)
3869 unlink(cl_arg.logmsg_path);
3871 error = got_object_id_str(&id_str, id);
3872 if (error)
3873 goto done;
3874 printf("Created commit %s\n", id_str);
3875 done:
3876 free(cl_arg.logmsg_path);
3877 if (repo)
3878 got_repo_close(repo);
3879 if (worktree)
3880 got_worktree_close(worktree);
3881 free(cwd);
3882 free(id_str);
3883 free(editor);
3884 return error;
3887 __dead static void
3888 usage_cherrypick(void)
3890 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3891 exit(1);
3894 static const struct got_error *
3895 cmd_cherrypick(int argc, char *argv[])
3897 const struct got_error *error = NULL;
3898 struct got_worktree *worktree = NULL;
3899 struct got_repository *repo = NULL;
3900 char *cwd = NULL, *commit_id_str = NULL;
3901 struct got_object_id *commit_id = NULL;
3902 struct got_commit_object *commit = NULL;
3903 struct got_object_qid *pid;
3904 struct got_reference *head_ref = NULL;
3905 int ch, did_something = 0;
3907 while ((ch = getopt(argc, argv, "")) != -1) {
3908 switch (ch) {
3909 default:
3910 usage_cherrypick();
3911 /* NOTREACHED */
3915 argc -= optind;
3916 argv += optind;
3918 #ifndef PROFILE
3919 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3920 "unveil", NULL) == -1)
3921 err(1, "pledge");
3922 #endif
3923 if (argc != 1)
3924 usage_cherrypick();
3926 cwd = getcwd(NULL, 0);
3927 if (cwd == NULL) {
3928 error = got_error_from_errno("getcwd");
3929 goto done;
3931 error = got_worktree_open(&worktree, cwd);
3932 if (error)
3933 goto done;
3935 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3936 if (error != NULL)
3937 goto done;
3939 error = apply_unveil(got_repo_get_path(repo), 0,
3940 got_worktree_get_root_path(worktree));
3941 if (error)
3942 goto done;
3944 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3945 GOT_OBJ_TYPE_COMMIT, repo);
3946 if (error != NULL) {
3947 struct got_reference *ref;
3948 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3949 goto done;
3950 error = got_ref_open(&ref, repo, argv[0], 0);
3951 if (error != NULL)
3952 goto done;
3953 error = got_ref_resolve(&commit_id, repo, ref);
3954 got_ref_close(ref);
3955 if (error != NULL)
3956 goto done;
3958 error = got_object_id_str(&commit_id_str, commit_id);
3959 if (error)
3960 goto done;
3962 error = got_ref_open(&head_ref, repo,
3963 got_worktree_get_head_ref_name(worktree), 0);
3964 if (error != NULL)
3965 goto done;
3967 error = check_same_branch(commit_id, head_ref, NULL, repo);
3968 if (error) {
3969 if (error->code != GOT_ERR_ANCESTRY)
3970 goto done;
3971 error = NULL;
3972 } else {
3973 error = got_error(GOT_ERR_SAME_BRANCH);
3974 goto done;
3977 error = got_object_open_as_commit(&commit, repo, commit_id);
3978 if (error)
3979 goto done;
3980 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3981 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3982 commit_id, repo, update_progress, &did_something, check_cancelled,
3983 NULL);
3984 if (error != NULL)
3985 goto done;
3987 if (did_something)
3988 printf("Merged commit %s\n", commit_id_str);
3989 done:
3990 if (commit)
3991 got_object_commit_close(commit);
3992 free(commit_id_str);
3993 if (head_ref)
3994 got_ref_close(head_ref);
3995 if (worktree)
3996 got_worktree_close(worktree);
3997 if (repo)
3998 got_repo_close(repo);
3999 return error;
4002 __dead static void
4003 usage_backout(void)
4005 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4006 exit(1);
4009 static const struct got_error *
4010 cmd_backout(int argc, char *argv[])
4012 const struct got_error *error = NULL;
4013 struct got_worktree *worktree = NULL;
4014 struct got_repository *repo = NULL;
4015 char *cwd = NULL, *commit_id_str = NULL;
4016 struct got_object_id *commit_id = NULL;
4017 struct got_commit_object *commit = NULL;
4018 struct got_object_qid *pid;
4019 struct got_reference *head_ref = NULL;
4020 int ch, did_something = 0;
4022 while ((ch = getopt(argc, argv, "")) != -1) {
4023 switch (ch) {
4024 default:
4025 usage_backout();
4026 /* NOTREACHED */
4030 argc -= optind;
4031 argv += optind;
4033 #ifndef PROFILE
4034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4035 "unveil", NULL) == -1)
4036 err(1, "pledge");
4037 #endif
4038 if (argc != 1)
4039 usage_backout();
4041 cwd = getcwd(NULL, 0);
4042 if (cwd == NULL) {
4043 error = got_error_from_errno("getcwd");
4044 goto done;
4046 error = got_worktree_open(&worktree, cwd);
4047 if (error)
4048 goto done;
4050 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4051 if (error != NULL)
4052 goto done;
4054 error = apply_unveil(got_repo_get_path(repo), 0,
4055 got_worktree_get_root_path(worktree));
4056 if (error)
4057 goto done;
4059 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4060 GOT_OBJ_TYPE_COMMIT, repo);
4061 if (error != NULL) {
4062 struct got_reference *ref;
4063 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4064 goto done;
4065 error = got_ref_open(&ref, repo, argv[0], 0);
4066 if (error != NULL)
4067 goto done;
4068 error = got_ref_resolve(&commit_id, repo, ref);
4069 got_ref_close(ref);
4070 if (error != NULL)
4071 goto done;
4073 error = got_object_id_str(&commit_id_str, commit_id);
4074 if (error)
4075 goto done;
4077 error = got_ref_open(&head_ref, repo,
4078 got_worktree_get_head_ref_name(worktree), 0);
4079 if (error != NULL)
4080 goto done;
4082 error = check_same_branch(commit_id, head_ref, NULL, repo);
4083 if (error)
4084 goto done;
4086 error = got_object_open_as_commit(&commit, repo, commit_id);
4087 if (error)
4088 goto done;
4089 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4090 if (pid == NULL) {
4091 error = got_error(GOT_ERR_ROOT_COMMIT);
4092 goto done;
4095 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4096 update_progress, &did_something, check_cancelled, NULL);
4097 if (error != NULL)
4098 goto done;
4100 if (did_something)
4101 printf("Backed out commit %s\n", commit_id_str);
4102 done:
4103 if (commit)
4104 got_object_commit_close(commit);
4105 free(commit_id_str);
4106 if (head_ref)
4107 got_ref_close(head_ref);
4108 if (worktree)
4109 got_worktree_close(worktree);
4110 if (repo)
4111 got_repo_close(repo);
4112 return error;
4115 __dead static void
4116 usage_rebase(void)
4118 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4119 getprogname());
4120 exit(1);
4123 void
4124 trim_logmsg(char *logmsg, int limit)
4126 char *nl;
4127 size_t len;
4129 len = strlen(logmsg);
4130 if (len > limit)
4131 len = limit;
4132 logmsg[len] = '\0';
4133 nl = strchr(logmsg, '\n');
4134 if (nl)
4135 *nl = '\0';
4138 static const struct got_error *
4139 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4141 const struct got_error *err;
4142 char *logmsg0 = NULL;
4143 const char *s;
4145 err = got_object_commit_get_logmsg(&logmsg0, commit);
4146 if (err)
4147 return err;
4149 s = logmsg0;
4150 while (isspace((unsigned char)s[0]))
4151 s++;
4153 *logmsg = strdup(s);
4154 if (*logmsg == NULL) {
4155 err = got_error_from_errno("strdup");
4156 goto done;
4159 trim_logmsg(*logmsg, limit);
4160 done:
4161 free(logmsg0);
4162 return err;
4165 static const struct got_error *
4166 show_rebase_progress(struct got_commit_object *commit,
4167 struct got_object_id *old_id, struct got_object_id *new_id)
4169 const struct got_error *err;
4170 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4172 err = got_object_id_str(&old_id_str, old_id);
4173 if (err)
4174 goto done;
4176 if (new_id) {
4177 err = got_object_id_str(&new_id_str, new_id);
4178 if (err)
4179 goto done;
4182 old_id_str[12] = '\0';
4183 if (new_id_str)
4184 new_id_str[12] = '\0';
4186 err = get_short_logmsg(&logmsg, 42, commit);
4187 if (err)
4188 goto done;
4190 printf("%s -> %s: %s\n", old_id_str,
4191 new_id_str ? new_id_str : "no-op change", logmsg);
4192 done:
4193 free(old_id_str);
4194 free(new_id_str);
4195 return err;
4198 static const struct got_error *
4199 rebase_progress(void *arg, unsigned char status, const char *path)
4201 unsigned char *rebase_status = arg;
4203 while (path[0] == '/')
4204 path++;
4205 printf("%c %s\n", status, path);
4207 if (*rebase_status == GOT_STATUS_CONFLICT)
4208 return NULL;
4209 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4210 *rebase_status = status;
4211 return NULL;
4214 static const struct got_error *
4215 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4216 struct got_reference *branch, struct got_reference *new_base_branch,
4217 struct got_reference *tmp_branch, struct got_repository *repo)
4219 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4220 return got_worktree_rebase_complete(worktree, fileindex,
4221 new_base_branch, tmp_branch, branch, repo);
4224 static const struct got_error *
4225 rebase_commit(struct got_pathlist_head *merged_paths,
4226 struct got_worktree *worktree, struct got_fileindex *fileindex,
4227 struct got_reference *tmp_branch,
4228 struct got_object_id *commit_id, struct got_repository *repo)
4230 const struct got_error *error;
4231 struct got_commit_object *commit;
4232 struct got_object_id *new_commit_id;
4234 error = got_object_open_as_commit(&commit, repo, commit_id);
4235 if (error)
4236 return error;
4238 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4239 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4240 if (error) {
4241 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4242 goto done;
4243 error = show_rebase_progress(commit, commit_id, NULL);
4244 } else {
4245 error = show_rebase_progress(commit, commit_id, new_commit_id);
4246 free(new_commit_id);
4248 done:
4249 got_object_commit_close(commit);
4250 return error;
4253 struct check_path_prefix_arg {
4254 const char *path_prefix;
4255 size_t len;
4256 int errcode;
4259 static const struct got_error *
4260 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4261 struct got_blob_object *blob2, struct got_object_id *id1,
4262 struct got_object_id *id2, const char *path1, const char *path2,
4263 struct got_repository *repo)
4265 struct check_path_prefix_arg *a = arg;
4267 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4268 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4269 return got_error(a->errcode);
4271 return NULL;
4274 static const struct got_error *
4275 check_path_prefix(struct got_object_id *parent_id,
4276 struct got_object_id *commit_id, const char *path_prefix,
4277 int errcode, struct got_repository *repo)
4279 const struct got_error *err;
4280 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4281 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4282 struct check_path_prefix_arg cpp_arg;
4284 if (got_path_is_root_dir(path_prefix))
4285 return NULL;
4287 err = got_object_open_as_commit(&commit, repo, commit_id);
4288 if (err)
4289 goto done;
4291 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4292 if (err)
4293 goto done;
4295 err = got_object_open_as_tree(&tree1, repo,
4296 got_object_commit_get_tree_id(parent_commit));
4297 if (err)
4298 goto done;
4300 err = got_object_open_as_tree(&tree2, repo,
4301 got_object_commit_get_tree_id(commit));
4302 if (err)
4303 goto done;
4305 cpp_arg.path_prefix = path_prefix;
4306 while (cpp_arg.path_prefix[0] == '/')
4307 cpp_arg.path_prefix++;
4308 cpp_arg.len = strlen(cpp_arg.path_prefix);
4309 cpp_arg.errcode = errcode;
4310 err = got_diff_tree(tree1, tree2, "", "", repo,
4311 check_path_prefix_in_diff, &cpp_arg, 0);
4312 done:
4313 if (tree1)
4314 got_object_tree_close(tree1);
4315 if (tree2)
4316 got_object_tree_close(tree2);
4317 if (commit)
4318 got_object_commit_close(commit);
4319 if (parent_commit)
4320 got_object_commit_close(parent_commit);
4321 return err;
4324 static const struct got_error *
4325 collect_commits(struct got_object_id_queue *commits,
4326 struct got_object_id *initial_commit_id,
4327 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4328 const char *path_prefix, int path_prefix_errcode,
4329 struct got_repository *repo)
4331 const struct got_error *err = NULL;
4332 struct got_commit_graph *graph = NULL;
4333 struct got_object_id *parent_id = NULL;
4334 struct got_object_qid *qid;
4335 struct got_object_id *commit_id = initial_commit_id;
4337 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4338 if (err)
4339 return err;
4341 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4342 if (err)
4343 goto done;
4344 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4345 err = got_commit_graph_iter_next(&parent_id, graph);
4346 if (err) {
4347 if (err->code == GOT_ERR_ITER_COMPLETED) {
4348 err = got_error_msg(GOT_ERR_ANCESTRY,
4349 "ran out of commits to rebase before "
4350 "youngest common ancestor commit has "
4351 "been reached?!?");
4352 goto done;
4353 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4354 goto done;
4355 err = got_commit_graph_fetch_commits(graph, 1, repo);
4356 if (err)
4357 goto done;
4358 } else {
4359 err = check_path_prefix(parent_id, commit_id,
4360 path_prefix, path_prefix_errcode, repo);
4361 if (err)
4362 goto done;
4364 err = got_object_qid_alloc(&qid, commit_id);
4365 if (err)
4366 goto done;
4367 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4368 commit_id = parent_id;
4371 done:
4372 got_commit_graph_close(graph);
4373 return err;
4376 static const struct got_error *
4377 cmd_rebase(int argc, char *argv[])
4379 const struct got_error *error = NULL;
4380 struct got_worktree *worktree = NULL;
4381 struct got_repository *repo = NULL;
4382 struct got_fileindex *fileindex = NULL;
4383 char *cwd = NULL;
4384 struct got_reference *branch = NULL;
4385 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4386 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4387 struct got_object_id *resume_commit_id = NULL;
4388 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4389 struct got_commit_object *commit = NULL;
4390 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4391 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4392 struct got_object_id_queue commits;
4393 struct got_pathlist_head merged_paths;
4394 const struct got_object_id_queue *parent_ids;
4395 struct got_object_qid *qid, *pid;
4397 SIMPLEQ_INIT(&commits);
4398 TAILQ_INIT(&merged_paths);
4400 while ((ch = getopt(argc, argv, "ac")) != -1) {
4401 switch (ch) {
4402 case 'a':
4403 abort_rebase = 1;
4404 break;
4405 case 'c':
4406 continue_rebase = 1;
4407 break;
4408 default:
4409 usage_rebase();
4410 /* NOTREACHED */
4414 argc -= optind;
4415 argv += optind;
4417 #ifndef PROFILE
4418 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4419 "unveil", NULL) == -1)
4420 err(1, "pledge");
4421 #endif
4422 if (abort_rebase && continue_rebase)
4423 usage_rebase();
4424 else if (abort_rebase || continue_rebase) {
4425 if (argc != 0)
4426 usage_rebase();
4427 } else if (argc != 1)
4428 usage_rebase();
4430 cwd = getcwd(NULL, 0);
4431 if (cwd == NULL) {
4432 error = got_error_from_errno("getcwd");
4433 goto done;
4435 error = got_worktree_open(&worktree, cwd);
4436 if (error)
4437 goto done;
4439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4440 if (error != NULL)
4441 goto done;
4443 error = apply_unveil(got_repo_get_path(repo), 0,
4444 got_worktree_get_root_path(worktree));
4445 if (error)
4446 goto done;
4448 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4449 if (error)
4450 goto done;
4452 if (abort_rebase) {
4453 int did_something;
4454 if (!rebase_in_progress) {
4455 error = got_error(GOT_ERR_NOT_REBASING);
4456 goto done;
4458 error = got_worktree_rebase_continue(&resume_commit_id,
4459 &new_base_branch, &tmp_branch, &branch, &fileindex,
4460 worktree, repo);
4461 if (error)
4462 goto done;
4463 printf("Switching work tree to %s\n",
4464 got_ref_get_symref_target(new_base_branch));
4465 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4466 new_base_branch, update_progress, &did_something);
4467 if (error)
4468 goto done;
4469 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4470 goto done; /* nothing else to do */
4473 if (continue_rebase) {
4474 if (!rebase_in_progress) {
4475 error = got_error(GOT_ERR_NOT_REBASING);
4476 goto done;
4478 error = got_worktree_rebase_continue(&resume_commit_id,
4479 &new_base_branch, &tmp_branch, &branch, &fileindex,
4480 worktree, repo);
4481 if (error)
4482 goto done;
4484 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4485 resume_commit_id, repo);
4486 if (error)
4487 goto done;
4489 yca_id = got_object_id_dup(resume_commit_id);
4490 if (yca_id == NULL) {
4491 error = got_error_from_errno("got_object_id_dup");
4492 goto done;
4494 } else {
4495 error = got_ref_open(&branch, repo, argv[0], 0);
4496 if (error != NULL)
4497 goto done;
4500 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4501 if (error)
4502 goto done;
4504 if (!continue_rebase) {
4505 struct got_object_id *base_commit_id;
4507 base_commit_id = got_worktree_get_base_commit_id(worktree);
4508 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4509 base_commit_id, branch_head_commit_id, repo);
4510 if (error)
4511 goto done;
4512 if (yca_id == NULL) {
4513 error = got_error_msg(GOT_ERR_ANCESTRY,
4514 "specified branch shares no common ancestry "
4515 "with work tree's branch");
4516 goto done;
4519 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4520 if (error) {
4521 if (error->code != GOT_ERR_ANCESTRY)
4522 goto done;
4523 error = NULL;
4524 } else {
4525 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4526 "specified branch resolves to a commit which "
4527 "is already contained in work tree's branch");
4528 goto done;
4530 error = got_worktree_rebase_prepare(&new_base_branch,
4531 &tmp_branch, &fileindex, worktree, branch, repo);
4532 if (error)
4533 goto done;
4536 commit_id = branch_head_commit_id;
4537 error = got_object_open_as_commit(&commit, repo, commit_id);
4538 if (error)
4539 goto done;
4541 parent_ids = got_object_commit_get_parent_ids(commit);
4542 pid = SIMPLEQ_FIRST(parent_ids);
4543 if (pid == NULL) {
4544 if (!continue_rebase) {
4545 int did_something;
4546 error = got_worktree_rebase_abort(worktree, fileindex,
4547 repo, new_base_branch, update_progress,
4548 &did_something);
4549 if (error)
4550 goto done;
4551 printf("Rebase of %s aborted\n",
4552 got_ref_get_name(branch));
4554 error = got_error(GOT_ERR_EMPTY_REBASE);
4555 goto done;
4557 error = collect_commits(&commits, commit_id, pid->id,
4558 yca_id, got_worktree_get_path_prefix(worktree),
4559 GOT_ERR_REBASE_PATH, repo);
4560 got_object_commit_close(commit);
4561 commit = NULL;
4562 if (error)
4563 goto done;
4565 if (SIMPLEQ_EMPTY(&commits)) {
4566 if (continue_rebase)
4567 error = rebase_complete(worktree, fileindex,
4568 branch, new_base_branch, tmp_branch, repo);
4569 else
4570 error = got_error(GOT_ERR_EMPTY_REBASE);
4571 goto done;
4574 pid = NULL;
4575 SIMPLEQ_FOREACH(qid, &commits, entry) {
4576 commit_id = qid->id;
4577 parent_id = pid ? pid->id : yca_id;
4578 pid = qid;
4580 error = got_worktree_rebase_merge_files(&merged_paths,
4581 worktree, fileindex, parent_id, commit_id, repo,
4582 rebase_progress, &rebase_status, check_cancelled, NULL);
4583 if (error)
4584 goto done;
4586 if (rebase_status == GOT_STATUS_CONFLICT) {
4587 got_worktree_rebase_pathlist_free(&merged_paths);
4588 break;
4591 error = rebase_commit(&merged_paths, worktree, fileindex,
4592 tmp_branch, commit_id, repo);
4593 got_worktree_rebase_pathlist_free(&merged_paths);
4594 if (error)
4595 goto done;
4598 if (rebase_status == GOT_STATUS_CONFLICT) {
4599 error = got_worktree_rebase_postpone(worktree, fileindex);
4600 if (error)
4601 goto done;
4602 error = got_error_msg(GOT_ERR_CONFLICTS,
4603 "conflicts must be resolved before rebasing can continue");
4604 } else
4605 error = rebase_complete(worktree, fileindex, branch,
4606 new_base_branch, tmp_branch, repo);
4607 done:
4608 got_object_id_queue_free(&commits);
4609 free(branch_head_commit_id);
4610 free(resume_commit_id);
4611 free(yca_id);
4612 if (commit)
4613 got_object_commit_close(commit);
4614 if (branch)
4615 got_ref_close(branch);
4616 if (new_base_branch)
4617 got_ref_close(new_base_branch);
4618 if (tmp_branch)
4619 got_ref_close(tmp_branch);
4620 if (worktree)
4621 got_worktree_close(worktree);
4622 if (repo)
4623 got_repo_close(repo);
4624 return error;
4627 __dead static void
4628 usage_histedit(void)
4630 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4631 getprogname());
4632 exit(1);
4635 #define GOT_HISTEDIT_PICK 'p'
4636 #define GOT_HISTEDIT_EDIT 'e'
4637 #define GOT_HISTEDIT_FOLD 'f'
4638 #define GOT_HISTEDIT_DROP 'd'
4639 #define GOT_HISTEDIT_MESG 'm'
4641 static struct got_histedit_cmd {
4642 unsigned char code;
4643 const char *name;
4644 const char *desc;
4645 } got_histedit_cmds[] = {
4646 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4647 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4648 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4649 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4650 { GOT_HISTEDIT_MESG, "mesg",
4651 "single-line log message for commit above (open editor if empty)" },
4654 struct got_histedit_list_entry {
4655 TAILQ_ENTRY(got_histedit_list_entry) entry;
4656 struct got_object_id *commit_id;
4657 const struct got_histedit_cmd *cmd;
4658 char *logmsg;
4660 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4662 static const struct got_error *
4663 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4664 FILE *f, struct got_repository *repo)
4666 const struct got_error *err = NULL;
4667 char *logmsg = NULL, *id_str = NULL;
4668 struct got_commit_object *commit = NULL;
4669 int n;
4671 err = got_object_open_as_commit(&commit, repo, commit_id);
4672 if (err)
4673 goto done;
4675 err = get_short_logmsg(&logmsg, 34, commit);
4676 if (err)
4677 goto done;
4679 err = got_object_id_str(&id_str, commit_id);
4680 if (err)
4681 goto done;
4683 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4684 if (n < 0)
4685 err = got_ferror(f, GOT_ERR_IO);
4686 done:
4687 if (commit)
4688 got_object_commit_close(commit);
4689 free(id_str);
4690 free(logmsg);
4691 return err;
4694 static const struct got_error *
4695 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4696 struct got_repository *repo)
4698 const struct got_error *err = NULL;
4699 struct got_object_qid *qid;
4701 if (SIMPLEQ_EMPTY(commits))
4702 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4704 SIMPLEQ_FOREACH(qid, commits, entry) {
4705 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4706 f, repo);
4707 if (err)
4708 break;
4711 return err;
4714 static const struct got_error *
4715 write_cmd_list(FILE *f)
4717 const struct got_error *err = NULL;
4718 int n, i;
4720 n = fprintf(f, "# Available histedit commands:\n");
4721 if (n < 0)
4722 return got_ferror(f, GOT_ERR_IO);
4724 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4725 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4726 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4727 cmd->desc);
4728 if (n < 0) {
4729 err = got_ferror(f, GOT_ERR_IO);
4730 break;
4733 n = fprintf(f, "# Commits will be processed in order from top to "
4734 "bottom of this file.\n");
4735 if (n < 0)
4736 return got_ferror(f, GOT_ERR_IO);
4737 return err;
4740 static const struct got_error *
4741 histedit_syntax_error(int lineno)
4743 static char msg[42];
4744 int ret;
4746 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4747 lineno);
4748 if (ret == -1 || ret >= sizeof(msg))
4749 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4751 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4754 static const struct got_error *
4755 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4756 char *logmsg, struct got_repository *repo)
4758 const struct got_error *err;
4759 struct got_commit_object *folded_commit = NULL;
4760 char *id_str, *folded_logmsg = NULL;
4762 err = got_object_id_str(&id_str, hle->commit_id);
4763 if (err)
4764 return err;
4766 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4767 if (err)
4768 goto done;
4770 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4771 if (err)
4772 goto done;
4773 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4774 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4775 folded_logmsg) == -1) {
4776 err = got_error_from_errno("asprintf");
4777 goto done;
4779 done:
4780 if (folded_commit)
4781 got_object_commit_close(folded_commit);
4782 free(id_str);
4783 free(folded_logmsg);
4784 return err;
4787 static struct got_histedit_list_entry *
4788 get_folded_commits(struct got_histedit_list_entry *hle)
4790 struct got_histedit_list_entry *prev, *folded = NULL;
4792 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4793 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4794 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4795 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4796 folded = prev;
4797 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4800 return folded;
4803 static const struct got_error *
4804 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4805 struct got_repository *repo)
4807 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4808 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4809 const struct got_error *err = NULL;
4810 struct got_commit_object *commit = NULL;
4811 int fd;
4812 struct got_histedit_list_entry *folded = NULL;
4814 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4815 if (err)
4816 return err;
4818 folded = get_folded_commits(hle);
4819 if (folded) {
4820 while (folded != hle) {
4821 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4822 folded = TAILQ_NEXT(folded, entry);
4823 continue;
4825 err = append_folded_commit_msg(&new_msg, folded,
4826 logmsg, repo);
4827 if (err)
4828 goto done;
4829 free(logmsg);
4830 logmsg = new_msg;
4831 folded = TAILQ_NEXT(folded, entry);
4835 err = got_object_id_str(&id_str, hle->commit_id);
4836 if (err)
4837 goto done;
4838 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4839 if (err)
4840 goto done;
4841 if (asprintf(&new_msg,
4842 "%s\n# original log message of commit %s: %s",
4843 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4844 err = got_error_from_errno("asprintf");
4845 goto done;
4847 free(logmsg);
4848 logmsg = new_msg;
4850 err = got_object_id_str(&id_str, hle->commit_id);
4851 if (err)
4852 goto done;
4854 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4855 if (err)
4856 goto done;
4858 dprintf(fd, logmsg);
4859 close(fd);
4861 err = get_editor(&editor);
4862 if (err)
4863 goto done;
4865 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4866 if (err) {
4867 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4868 goto done;
4869 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4871 done:
4872 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4873 err = got_error_from_errno2("unlink", logmsg_path);
4874 free(logmsg_path);
4875 free(logmsg);
4876 free(orig_logmsg);
4877 free(editor);
4878 if (commit)
4879 got_object_commit_close(commit);
4880 return err;
4883 static const struct got_error *
4884 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4885 FILE *f, struct got_repository *repo)
4887 const struct got_error *err = NULL;
4888 char *line = NULL, *p, *end;
4889 size_t size;
4890 ssize_t len;
4891 int lineno = 0, i;
4892 const struct got_histedit_cmd *cmd;
4893 struct got_object_id *commit_id = NULL;
4894 struct got_histedit_list_entry *hle = NULL;
4896 for (;;) {
4897 len = getline(&line, &size, f);
4898 if (len == -1) {
4899 const struct got_error *getline_err;
4900 if (feof(f))
4901 break;
4902 getline_err = got_error_from_errno("getline");
4903 err = got_ferror(f, getline_err->code);
4904 break;
4906 lineno++;
4907 p = line;
4908 while (isspace((unsigned char)p[0]))
4909 p++;
4910 if (p[0] == '#' || p[0] == '\0') {
4911 free(line);
4912 line = NULL;
4913 continue;
4915 cmd = NULL;
4916 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4917 cmd = &got_histedit_cmds[i];
4918 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4919 isspace((unsigned char)p[strlen(cmd->name)])) {
4920 p += strlen(cmd->name);
4921 break;
4923 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4924 p++;
4925 break;
4928 if (i == nitems(got_histedit_cmds)) {
4929 err = histedit_syntax_error(lineno);
4930 break;
4932 while (isspace((unsigned char)p[0]))
4933 p++;
4934 if (cmd->code == GOT_HISTEDIT_MESG) {
4935 if (hle == NULL || hle->logmsg != NULL) {
4936 err = got_error(GOT_ERR_HISTEDIT_CMD);
4937 break;
4939 if (p[0] == '\0') {
4940 err = histedit_edit_logmsg(hle, repo);
4941 if (err)
4942 break;
4943 } else {
4944 hle->logmsg = strdup(p);
4945 if (hle->logmsg == NULL) {
4946 err = got_error_from_errno("strdup");
4947 break;
4950 free(line);
4951 line = NULL;
4952 continue;
4953 } else {
4954 end = p;
4955 while (end[0] && !isspace((unsigned char)end[0]))
4956 end++;
4957 *end = '\0';
4959 err = got_object_resolve_id_str(&commit_id, repo, p);
4960 if (err) {
4961 /* override error code */
4962 err = histedit_syntax_error(lineno);
4963 break;
4966 hle = malloc(sizeof(*hle));
4967 if (hle == NULL) {
4968 err = got_error_from_errno("malloc");
4969 break;
4971 hle->cmd = cmd;
4972 hle->commit_id = commit_id;
4973 hle->logmsg = NULL;
4974 commit_id = NULL;
4975 free(line);
4976 line = NULL;
4977 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4980 free(line);
4981 free(commit_id);
4982 return err;
4985 static const struct got_error *
4986 histedit_check_script(struct got_histedit_list *histedit_cmds,
4987 struct got_object_id_queue *commits, struct got_repository *repo)
4989 const struct got_error *err = NULL;
4990 struct got_object_qid *qid;
4991 struct got_histedit_list_entry *hle;
4992 static char msg[80];
4993 char *id_str;
4995 if (TAILQ_EMPTY(histedit_cmds))
4996 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4997 "histedit script contains no commands");
4998 if (SIMPLEQ_EMPTY(commits))
4999 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5001 SIMPLEQ_FOREACH(qid, commits, entry) {
5002 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5003 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5004 break;
5006 if (hle == NULL) {
5007 err = got_object_id_str(&id_str, qid->id);
5008 if (err)
5009 return err;
5010 snprintf(msg, sizeof(msg),
5011 "commit %s missing from histedit script", id_str);
5012 free(id_str);
5013 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5017 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5018 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5019 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5020 "last commit in histedit script cannot be folded");
5022 return NULL;
5025 static const struct got_error *
5026 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5027 const char *path, struct got_object_id_queue *commits,
5028 struct got_repository *repo)
5030 const struct got_error *err = NULL;
5031 char *editor;
5032 FILE *f = NULL;
5034 err = get_editor(&editor);
5035 if (err)
5036 return err;
5038 if (spawn_editor(editor, path) == -1) {
5039 err = got_error_from_errno("failed spawning editor");
5040 goto done;
5043 f = fopen(path, "r");
5044 if (f == NULL) {
5045 err = got_error_from_errno("fopen");
5046 goto done;
5048 err = histedit_parse_list(histedit_cmds, f, repo);
5049 if (err)
5050 goto done;
5052 err = histedit_check_script(histedit_cmds, commits, repo);
5053 done:
5054 if (f && fclose(f) != 0 && err == NULL)
5055 err = got_error_from_errno("fclose");
5056 free(editor);
5057 return err;
5060 static const struct got_error *
5061 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5062 struct got_object_id_queue *, const char *, struct got_repository *);
5064 static const struct got_error *
5065 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5066 struct got_object_id_queue *commits, struct got_repository *repo)
5068 const struct got_error *err;
5069 FILE *f = NULL;
5070 char *path = NULL;
5072 err = got_opentemp_named(&path, &f, "got-histedit");
5073 if (err)
5074 return err;
5076 err = write_cmd_list(f);
5077 if (err)
5078 goto done;
5080 err = histedit_write_commit_list(commits, f, repo);
5081 if (err)
5082 goto done;
5084 if (fclose(f) != 0) {
5085 err = got_error_from_errno("fclose");
5086 goto done;
5088 f = NULL;
5090 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5091 if (err) {
5092 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5093 err->code != GOT_ERR_HISTEDIT_CMD)
5094 goto done;
5095 err = histedit_edit_list_retry(histedit_cmds, err,
5096 commits, path, repo);
5098 done:
5099 if (f && fclose(f) != 0 && err == NULL)
5100 err = got_error_from_errno("fclose");
5101 if (path && unlink(path) != 0 && err == NULL)
5102 err = got_error_from_errno2("unlink", path);
5103 free(path);
5104 return err;
5107 static const struct got_error *
5108 histedit_save_list(struct got_histedit_list *histedit_cmds,
5109 struct got_worktree *worktree, struct got_repository *repo)
5111 const struct got_error *err = NULL;
5112 char *path = NULL;
5113 FILE *f = NULL;
5114 struct got_histedit_list_entry *hle;
5115 struct got_commit_object *commit = NULL;
5117 err = got_worktree_get_histedit_script_path(&path, worktree);
5118 if (err)
5119 return err;
5121 f = fopen(path, "w");
5122 if (f == NULL) {
5123 err = got_error_from_errno2("fopen", path);
5124 goto done;
5126 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5127 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5128 repo);
5129 if (err)
5130 break;
5132 if (hle->logmsg) {
5133 int n = fprintf(f, "%c %s\n",
5134 GOT_HISTEDIT_MESG, hle->logmsg);
5135 if (n < 0) {
5136 err = got_ferror(f, GOT_ERR_IO);
5137 break;
5141 done:
5142 if (f && fclose(f) != 0 && err == NULL)
5143 err = got_error_from_errno("fclose");
5144 free(path);
5145 if (commit)
5146 got_object_commit_close(commit);
5147 return err;
5150 void
5151 histedit_free_list(struct got_histedit_list *histedit_cmds)
5153 struct got_histedit_list_entry *hle;
5155 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5156 TAILQ_REMOVE(histedit_cmds, hle, entry);
5157 free(hle);
5161 static const struct got_error *
5162 histedit_load_list(struct got_histedit_list *histedit_cmds,
5163 const char *path, struct got_repository *repo)
5165 const struct got_error *err = NULL;
5166 FILE *f = NULL;
5168 f = fopen(path, "r");
5169 if (f == NULL) {
5170 err = got_error_from_errno2("fopen", path);
5171 goto done;
5174 err = histedit_parse_list(histedit_cmds, f, repo);
5175 done:
5176 if (f && fclose(f) != 0 && err == NULL)
5177 err = got_error_from_errno("fclose");
5178 return err;
5181 static const struct got_error *
5182 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5183 const struct got_error *edit_err, struct got_object_id_queue *commits,
5184 const char *path, struct got_repository *repo)
5186 const struct got_error *err = NULL, *prev_err = edit_err;
5187 int resp = ' ';
5189 while (resp != 'c' && resp != 'r' && resp != 'a') {
5190 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5191 "or (a)bort: ", getprogname(), prev_err->msg);
5192 resp = getchar();
5193 if (resp == '\n')
5194 resp = getchar();
5195 if (resp == 'c') {
5196 histedit_free_list(histedit_cmds);
5197 err = histedit_run_editor(histedit_cmds, path, commits,
5198 repo);
5199 if (err) {
5200 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5201 err->code != GOT_ERR_HISTEDIT_CMD)
5202 break;
5203 prev_err = err;
5204 resp = ' ';
5205 continue;
5207 break;
5208 } else if (resp == 'r') {
5209 histedit_free_list(histedit_cmds);
5210 err = histedit_edit_script(histedit_cmds,
5211 commits, repo);
5212 if (err) {
5213 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5214 err->code != GOT_ERR_HISTEDIT_CMD)
5215 break;
5216 prev_err = err;
5217 resp = ' ';
5218 continue;
5220 break;
5221 } else if (resp == 'a') {
5222 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5223 break;
5224 } else
5225 printf("invalid response '%c'\n", resp);
5228 return err;
5231 static const struct got_error *
5232 histedit_complete(struct got_worktree *worktree,
5233 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5234 struct got_reference *branch, struct got_repository *repo)
5236 printf("Switching work tree to %s\n",
5237 got_ref_get_symref_target(branch));
5238 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5239 branch, repo);
5242 static const struct got_error *
5243 show_histedit_progress(struct got_commit_object *commit,
5244 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5246 const struct got_error *err;
5247 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5249 err = got_object_id_str(&old_id_str, hle->commit_id);
5250 if (err)
5251 goto done;
5253 if (new_id) {
5254 err = got_object_id_str(&new_id_str, new_id);
5255 if (err)
5256 goto done;
5259 old_id_str[12] = '\0';
5260 if (new_id_str)
5261 new_id_str[12] = '\0';
5263 if (hle->logmsg) {
5264 logmsg = strdup(hle->logmsg);
5265 if (logmsg == NULL) {
5266 err = got_error_from_errno("strdup");
5267 goto done;
5269 trim_logmsg(logmsg, 42);
5270 } else {
5271 err = get_short_logmsg(&logmsg, 42, commit);
5272 if (err)
5273 goto done;
5276 switch (hle->cmd->code) {
5277 case GOT_HISTEDIT_PICK:
5278 case GOT_HISTEDIT_EDIT:
5279 printf("%s -> %s: %s\n", old_id_str,
5280 new_id_str ? new_id_str : "no-op change", logmsg);
5281 break;
5282 case GOT_HISTEDIT_DROP:
5283 case GOT_HISTEDIT_FOLD:
5284 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5285 logmsg);
5286 break;
5287 default:
5288 break;
5291 done:
5292 free(old_id_str);
5293 free(new_id_str);
5294 return err;
5297 static const struct got_error *
5298 histedit_commit(struct got_pathlist_head *merged_paths,
5299 struct got_worktree *worktree, struct got_fileindex *fileindex,
5300 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5301 struct got_repository *repo)
5303 const struct got_error *err;
5304 struct got_commit_object *commit;
5305 struct got_object_id *new_commit_id;
5307 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5308 && hle->logmsg == NULL) {
5309 err = histedit_edit_logmsg(hle, repo);
5310 if (err)
5311 return err;
5314 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5315 if (err)
5316 return err;
5318 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5319 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5320 hle->logmsg, repo);
5321 if (err) {
5322 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5323 goto done;
5324 err = show_histedit_progress(commit, hle, NULL);
5325 } else {
5326 err = show_histedit_progress(commit, hle, new_commit_id);
5327 free(new_commit_id);
5329 done:
5330 got_object_commit_close(commit);
5331 return err;
5334 static const struct got_error *
5335 histedit_skip_commit(struct got_histedit_list_entry *hle,
5336 struct got_worktree *worktree, struct got_repository *repo)
5338 const struct got_error *error;
5339 struct got_commit_object *commit;
5341 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5342 repo);
5343 if (error)
5344 return error;
5346 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5347 if (error)
5348 return error;
5350 error = show_histedit_progress(commit, hle, NULL);
5351 got_object_commit_close(commit);
5352 return error;
5355 static const struct got_error *
5356 cmd_histedit(int argc, char *argv[])
5358 const struct got_error *error = NULL;
5359 struct got_worktree *worktree = NULL;
5360 struct got_fileindex *fileindex = NULL;
5361 struct got_repository *repo = NULL;
5362 char *cwd = NULL;
5363 struct got_reference *branch = NULL;
5364 struct got_reference *tmp_branch = NULL;
5365 struct got_object_id *resume_commit_id = NULL;
5366 struct got_object_id *base_commit_id = NULL;
5367 struct got_object_id *head_commit_id = NULL;
5368 struct got_commit_object *commit = NULL;
5369 int ch, rebase_in_progress = 0, did_something;
5370 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5371 const char *edit_script_path = NULL;
5372 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5373 struct got_object_id_queue commits;
5374 struct got_pathlist_head merged_paths;
5375 const struct got_object_id_queue *parent_ids;
5376 struct got_object_qid *pid;
5377 struct got_histedit_list histedit_cmds;
5378 struct got_histedit_list_entry *hle;
5380 SIMPLEQ_INIT(&commits);
5381 TAILQ_INIT(&histedit_cmds);
5382 TAILQ_INIT(&merged_paths);
5384 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5385 switch (ch) {
5386 case 'a':
5387 abort_edit = 1;
5388 break;
5389 case 'c':
5390 continue_edit = 1;
5391 break;
5392 case 'F':
5393 edit_script_path = optarg;
5394 break;
5395 default:
5396 usage_histedit();
5397 /* NOTREACHED */
5401 argc -= optind;
5402 argv += optind;
5404 #ifndef PROFILE
5405 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5406 "unveil", NULL) == -1)
5407 err(1, "pledge");
5408 #endif
5409 if (abort_edit && continue_edit)
5410 usage_histedit();
5411 if (argc != 0)
5412 usage_histedit();
5415 * This command cannot apply unveil(2) in all cases because the
5416 * user may choose to run an editor to edit the histedit script
5417 * and to edit individual commit log messages.
5418 * unveil(2) traverses exec(2); if an editor is used we have to
5419 * apply unveil after edit script and log messages have been written.
5420 * XXX TODO: Make use of unveil(2) where possible.
5423 cwd = getcwd(NULL, 0);
5424 if (cwd == NULL) {
5425 error = got_error_from_errno("getcwd");
5426 goto done;
5428 error = got_worktree_open(&worktree, cwd);
5429 if (error)
5430 goto done;
5432 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5433 if (error != NULL)
5434 goto done;
5436 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5437 if (error)
5438 goto done;
5439 if (rebase_in_progress) {
5440 error = got_error(GOT_ERR_REBASING);
5441 goto done;
5444 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5445 if (error)
5446 goto done;
5448 if (edit_in_progress && abort_edit) {
5449 error = got_worktree_histedit_continue(&resume_commit_id,
5450 &tmp_branch, &branch, &base_commit_id, &fileindex,
5451 worktree, repo);
5452 if (error)
5453 goto done;
5454 printf("Switching work tree to %s\n",
5455 got_ref_get_symref_target(branch));
5456 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5457 branch, base_commit_id, update_progress, &did_something);
5458 if (error)
5459 goto done;
5460 printf("Histedit of %s aborted\n",
5461 got_ref_get_symref_target(branch));
5462 goto done; /* nothing else to do */
5463 } else if (abort_edit) {
5464 error = got_error(GOT_ERR_NOT_HISTEDIT);
5465 goto done;
5468 if (continue_edit) {
5469 char *path;
5471 if (!edit_in_progress) {
5472 error = got_error(GOT_ERR_NOT_HISTEDIT);
5473 goto done;
5476 error = got_worktree_get_histedit_script_path(&path, worktree);
5477 if (error)
5478 goto done;
5480 error = histedit_load_list(&histedit_cmds, path, repo);
5481 free(path);
5482 if (error)
5483 goto done;
5485 error = got_worktree_histedit_continue(&resume_commit_id,
5486 &tmp_branch, &branch, &base_commit_id, &fileindex,
5487 worktree, repo);
5488 if (error)
5489 goto done;
5491 error = got_ref_resolve(&head_commit_id, repo, branch);
5492 if (error)
5493 goto done;
5495 error = got_object_open_as_commit(&commit, repo,
5496 head_commit_id);
5497 if (error)
5498 goto done;
5499 parent_ids = got_object_commit_get_parent_ids(commit);
5500 pid = SIMPLEQ_FIRST(parent_ids);
5501 if (pid == NULL) {
5502 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5503 goto done;
5505 error = collect_commits(&commits, head_commit_id, pid->id,
5506 base_commit_id, got_worktree_get_path_prefix(worktree),
5507 GOT_ERR_HISTEDIT_PATH, repo);
5508 got_object_commit_close(commit);
5509 commit = NULL;
5510 if (error)
5511 goto done;
5512 } else {
5513 if (edit_in_progress) {
5514 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5515 goto done;
5518 error = got_ref_open(&branch, repo,
5519 got_worktree_get_head_ref_name(worktree), 0);
5520 if (error != NULL)
5521 goto done;
5523 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5524 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5525 "will not edit commit history of a branch outside "
5526 "the \"refs/heads/\" reference namespace");
5527 goto done;
5530 error = got_ref_resolve(&head_commit_id, repo, branch);
5531 got_ref_close(branch);
5532 branch = NULL;
5533 if (error)
5534 goto done;
5536 error = got_object_open_as_commit(&commit, repo,
5537 head_commit_id);
5538 if (error)
5539 goto done;
5540 parent_ids = got_object_commit_get_parent_ids(commit);
5541 pid = SIMPLEQ_FIRST(parent_ids);
5542 if (pid == NULL) {
5543 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5544 goto done;
5546 error = collect_commits(&commits, head_commit_id, pid->id,
5547 got_worktree_get_base_commit_id(worktree),
5548 got_worktree_get_path_prefix(worktree),
5549 GOT_ERR_HISTEDIT_PATH, repo);
5550 got_object_commit_close(commit);
5551 commit = NULL;
5552 if (error)
5553 goto done;
5555 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5556 &base_commit_id, &fileindex, worktree, repo);
5557 if (error)
5558 goto done;
5560 if (edit_script_path) {
5561 error = histedit_load_list(&histedit_cmds,
5562 edit_script_path, repo);
5563 if (error) {
5564 got_worktree_histedit_abort(worktree, fileindex,
5565 repo, branch, base_commit_id,
5566 update_progress, &did_something);
5567 goto done;
5569 } else {
5570 error = histedit_edit_script(&histedit_cmds, &commits,
5571 repo);
5572 if (error) {
5573 got_worktree_histedit_abort(worktree, fileindex,
5574 repo, branch, base_commit_id,
5575 update_progress, &did_something);
5576 goto done;
5581 error = histedit_save_list(&histedit_cmds, worktree,
5582 repo);
5583 if (error) {
5584 got_worktree_histedit_abort(worktree, fileindex,
5585 repo, branch, base_commit_id,
5586 update_progress, &did_something);
5587 goto done;
5592 error = histedit_check_script(&histedit_cmds, &commits, repo);
5593 if (error)
5594 goto done;
5596 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5597 if (resume_commit_id) {
5598 if (got_object_id_cmp(hle->commit_id,
5599 resume_commit_id) != 0)
5600 continue;
5602 resume_commit_id = NULL;
5603 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5604 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5605 error = histedit_skip_commit(hle, worktree,
5606 repo);
5607 } else {
5608 error = histedit_commit(NULL, worktree,
5609 fileindex, tmp_branch, hle, repo);
5611 if (error)
5612 goto done;
5613 continue;
5616 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5617 error = histedit_skip_commit(hle, worktree, repo);
5618 if (error)
5619 goto done;
5620 continue;
5623 error = got_object_open_as_commit(&commit, repo,
5624 hle->commit_id);
5625 if (error)
5626 goto done;
5627 parent_ids = got_object_commit_get_parent_ids(commit);
5628 pid = SIMPLEQ_FIRST(parent_ids);
5630 error = got_worktree_histedit_merge_files(&merged_paths,
5631 worktree, fileindex, pid->id, hle->commit_id, repo,
5632 rebase_progress, &rebase_status, check_cancelled, NULL);
5633 if (error)
5634 goto done;
5635 got_object_commit_close(commit);
5636 commit = NULL;
5638 if (rebase_status == GOT_STATUS_CONFLICT) {
5639 got_worktree_rebase_pathlist_free(&merged_paths);
5640 break;
5643 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5644 char *id_str;
5645 error = got_object_id_str(&id_str, hle->commit_id);
5646 if (error)
5647 goto done;
5648 printf("Stopping histedit for amending commit %s\n",
5649 id_str);
5650 free(id_str);
5651 got_worktree_rebase_pathlist_free(&merged_paths);
5652 error = got_worktree_histedit_postpone(worktree,
5653 fileindex);
5654 goto done;
5657 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5658 error = histedit_skip_commit(hle, worktree, repo);
5659 if (error)
5660 goto done;
5661 continue;
5664 error = histedit_commit(&merged_paths, worktree, fileindex,
5665 tmp_branch, hle, repo);
5666 got_worktree_rebase_pathlist_free(&merged_paths);
5667 if (error)
5668 goto done;
5671 if (rebase_status == GOT_STATUS_CONFLICT) {
5672 error = got_worktree_histedit_postpone(worktree, fileindex);
5673 if (error)
5674 goto done;
5675 error = got_error_msg(GOT_ERR_CONFLICTS,
5676 "conflicts must be resolved before rebasing can continue");
5677 } else
5678 error = histedit_complete(worktree, fileindex, tmp_branch,
5679 branch, repo);
5680 done:
5681 got_object_id_queue_free(&commits);
5682 histedit_free_list(&histedit_cmds);
5683 free(head_commit_id);
5684 free(base_commit_id);
5685 free(resume_commit_id);
5686 if (commit)
5687 got_object_commit_close(commit);
5688 if (branch)
5689 got_ref_close(branch);
5690 if (tmp_branch)
5691 got_ref_close(tmp_branch);
5692 if (worktree)
5693 got_worktree_close(worktree);
5694 if (repo)
5695 got_repo_close(repo);
5696 return error;
5699 __dead static void
5700 usage_stage(void)
5702 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5703 "[file-path ...]\n",
5704 getprogname());
5705 exit(1);
5708 static const struct got_error *
5709 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5710 const char *path, struct got_object_id *blob_id,
5711 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5713 const struct got_error *err = NULL;
5714 char *id_str = NULL;
5716 if (staged_status != GOT_STATUS_ADD &&
5717 staged_status != GOT_STATUS_MODIFY &&
5718 staged_status != GOT_STATUS_DELETE)
5719 return NULL;
5721 if (staged_status == GOT_STATUS_ADD ||
5722 staged_status == GOT_STATUS_MODIFY)
5723 err = got_object_id_str(&id_str, staged_blob_id);
5724 else
5725 err = got_object_id_str(&id_str, blob_id);
5726 if (err)
5727 return err;
5729 printf("%s %c %s\n", id_str, staged_status, path);
5730 free(id_str);
5731 return NULL;
5734 static const struct got_error *
5735 cmd_stage(int argc, char *argv[])
5737 const struct got_error *error = NULL;
5738 struct got_repository *repo = NULL;
5739 struct got_worktree *worktree = NULL;
5740 char *cwd = NULL;
5741 struct got_pathlist_head paths;
5742 struct got_pathlist_entry *pe;
5743 int ch, list_stage = 0, pflag = 0;
5744 FILE *patch_script_file = NULL;
5745 const char *patch_script_path = NULL;
5746 struct choose_patch_arg cpa;
5748 TAILQ_INIT(&paths);
5750 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5751 switch (ch) {
5752 case 'l':
5753 list_stage = 1;
5754 break;
5755 case 'p':
5756 pflag = 1;
5757 break;
5758 case 'F':
5759 patch_script_path = optarg;
5760 break;
5761 default:
5762 usage_stage();
5763 /* NOTREACHED */
5767 argc -= optind;
5768 argv += optind;
5770 #ifndef PROFILE
5771 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5772 "unveil", NULL) == -1)
5773 err(1, "pledge");
5774 #endif
5775 if (list_stage && (pflag || patch_script_path))
5776 errx(1, "-l option cannot be used with other options");
5777 if (patch_script_path && !pflag)
5778 errx(1, "-F option can only be used together with -p option");
5780 cwd = getcwd(NULL, 0);
5781 if (cwd == NULL) {
5782 error = got_error_from_errno("getcwd");
5783 goto done;
5786 error = got_worktree_open(&worktree, cwd);
5787 if (error)
5788 goto done;
5790 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5791 if (error != NULL)
5792 goto done;
5794 if (patch_script_path) {
5795 patch_script_file = fopen(patch_script_path, "r");
5796 if (patch_script_file == NULL) {
5797 error = got_error_from_errno2("fopen",
5798 patch_script_path);
5799 goto done;
5802 error = apply_unveil(got_repo_get_path(repo), 1,
5803 got_worktree_get_root_path(worktree));
5804 if (error)
5805 goto done;
5807 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5808 if (error)
5809 goto done;
5811 if (list_stage)
5812 error = got_worktree_status(worktree, &paths, repo,
5813 print_stage, NULL, check_cancelled, NULL);
5814 else {
5815 cpa.patch_script_file = patch_script_file;
5816 cpa.action = "stage";
5817 error = got_worktree_stage(worktree, &paths,
5818 pflag ? NULL : print_status, NULL,
5819 pflag ? choose_patch : NULL, &cpa, repo);
5821 done:
5822 if (patch_script_file && fclose(patch_script_file) == EOF &&
5823 error == NULL)
5824 error = got_error_from_errno2("fclose", patch_script_path);
5825 if (repo)
5826 got_repo_close(repo);
5827 if (worktree)
5828 got_worktree_close(worktree);
5829 TAILQ_FOREACH(pe, &paths, entry)
5830 free((char *)pe->path);
5831 got_pathlist_free(&paths);
5832 free(cwd);
5833 return error;
5836 __dead static void
5837 usage_unstage(void)
5839 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5840 "[file-path ...]\n",
5841 getprogname());
5842 exit(1);
5846 static const struct got_error *
5847 cmd_unstage(int argc, char *argv[])
5849 const struct got_error *error = NULL;
5850 struct got_repository *repo = NULL;
5851 struct got_worktree *worktree = NULL;
5852 char *cwd = NULL;
5853 struct got_pathlist_head paths;
5854 struct got_pathlist_entry *pe;
5855 int ch, did_something = 0, pflag = 0;
5856 FILE *patch_script_file = NULL;
5857 const char *patch_script_path = NULL;
5858 struct choose_patch_arg cpa;
5860 TAILQ_INIT(&paths);
5862 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5863 switch (ch) {
5864 case 'p':
5865 pflag = 1;
5866 break;
5867 case 'F':
5868 patch_script_path = optarg;
5869 break;
5870 default:
5871 usage_unstage();
5872 /* NOTREACHED */
5876 argc -= optind;
5877 argv += optind;
5879 #ifndef PROFILE
5880 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5881 "unveil", NULL) == -1)
5882 err(1, "pledge");
5883 #endif
5884 if (patch_script_path && !pflag)
5885 errx(1, "-F option can only be used together with -p option");
5887 cwd = getcwd(NULL, 0);
5888 if (cwd == NULL) {
5889 error = got_error_from_errno("getcwd");
5890 goto done;
5893 error = got_worktree_open(&worktree, cwd);
5894 if (error)
5895 goto done;
5897 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5898 if (error != NULL)
5899 goto done;
5901 if (patch_script_path) {
5902 patch_script_file = fopen(patch_script_path, "r");
5903 if (patch_script_file == NULL) {
5904 error = got_error_from_errno2("fopen",
5905 patch_script_path);
5906 goto done;
5910 error = apply_unveil(got_repo_get_path(repo), 1,
5911 got_worktree_get_root_path(worktree));
5912 if (error)
5913 goto done;
5915 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5916 if (error)
5917 goto done;
5919 cpa.patch_script_file = patch_script_file;
5920 cpa.action = "unstage";
5921 error = got_worktree_unstage(worktree, &paths, update_progress,
5922 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5923 done:
5924 if (patch_script_file && fclose(patch_script_file) == EOF &&
5925 error == NULL)
5926 error = got_error_from_errno2("fclose", patch_script_path);
5927 if (repo)
5928 got_repo_close(repo);
5929 if (worktree)
5930 got_worktree_close(worktree);
5931 TAILQ_FOREACH(pe, &paths, entry)
5932 free((char *)pe->path);
5933 got_pathlist_free(&paths);
5934 free(cwd);
5935 return error;