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;
2288 size_t filesize;
2290 #ifndef PROFILE
2291 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2292 NULL) == -1)
2293 err(1, "pledge");
2294 #endif
2296 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2297 switch (ch) {
2298 case 'c':
2299 commit_id_str = optarg;
2300 break;
2301 case 'r':
2302 repo_path = realpath(optarg, NULL);
2303 if (repo_path == NULL)
2304 err(1, "-r option");
2305 got_path_strip_trailing_slashes(repo_path);
2306 break;
2307 default:
2308 usage_blame();
2309 /* NOTREACHED */
2313 argc -= optind;
2314 argv += optind;
2316 if (argc == 1)
2317 path = argv[0];
2318 else
2319 usage_blame();
2321 cwd = getcwd(NULL, 0);
2322 if (cwd == NULL) {
2323 error = got_error_from_errno("getcwd");
2324 goto done;
2326 if (repo_path == NULL) {
2327 error = got_worktree_open(&worktree, cwd);
2328 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2329 goto done;
2330 else
2331 error = NULL;
2332 if (worktree) {
2333 repo_path =
2334 strdup(got_worktree_get_repo_path(worktree));
2335 if (repo_path == NULL)
2336 error = got_error_from_errno("strdup");
2337 if (error)
2338 goto done;
2339 } else {
2340 repo_path = strdup(cwd);
2341 if (repo_path == NULL) {
2342 error = got_error_from_errno("strdup");
2343 goto done;
2348 error = got_repo_open(&repo, repo_path);
2349 if (error != NULL)
2350 goto done;
2352 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2353 if (error)
2354 goto done;
2356 if (worktree) {
2357 const char *prefix = got_worktree_get_path_prefix(worktree);
2358 char *p, *worktree_subdir = cwd +
2359 strlen(got_worktree_get_root_path(worktree));
2360 if (asprintf(&p, "%s%s%s%s%s",
2361 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2362 worktree_subdir, worktree_subdir[0] ? "/" : "",
2363 path) == -1) {
2364 error = got_error_from_errno("asprintf");
2365 goto done;
2367 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2368 free(p);
2369 } else {
2370 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2372 if (error)
2373 goto done;
2375 if (commit_id_str == NULL) {
2376 struct got_reference *head_ref;
2377 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2378 if (error != NULL)
2379 goto done;
2380 error = got_ref_resolve(&commit_id, repo, head_ref);
2381 got_ref_close(head_ref);
2382 if (error != NULL)
2383 goto done;
2384 } else {
2385 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2386 if (error)
2387 goto done;
2390 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2391 if (error)
2392 goto done;
2393 if (obj_id == NULL) {
2394 error = got_error(GOT_ERR_NO_OBJ);
2395 goto done;
2398 error = got_object_get_type(&obj_type, repo, obj_id);
2399 if (error)
2400 goto done;
2402 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2403 error = got_error(GOT_ERR_OBJ_TYPE);
2404 goto done;
2407 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2408 if (error)
2409 goto done;
2410 bca.f = got_opentemp();
2411 if (bca.f == NULL) {
2412 error = got_error_from_errno("got_opentemp");
2413 goto done;
2415 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2416 &bca.line_offsets, bca.f, blob);
2417 if (error || bca.nlines == 0)
2418 goto done;
2420 /* Don't include \n at EOF in the blame line count. */
2421 if (bca.line_offsets[bca.nlines - 1] == filesize)
2422 bca.nlines--;
2424 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2425 if (bca.lines == NULL) {
2426 error = got_error_from_errno("calloc");
2427 goto done;
2429 bca.lineno_cur = 1;
2430 bca.nlines_prec = 0;
2431 i = bca.nlines;
2432 while (i > 0) {
2433 i /= 10;
2434 bca.nlines_prec++;
2436 bca.repo = repo;
2438 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2439 if (error)
2440 goto done;
2441 done:
2442 free(in_repo_path);
2443 free(repo_path);
2444 free(cwd);
2445 free(commit_id);
2446 free(obj_id);
2447 if (blob)
2448 got_object_blob_close(blob);
2449 if (worktree)
2450 got_worktree_close(worktree);
2451 if (repo) {
2452 const struct got_error *repo_error;
2453 repo_error = got_repo_close(repo);
2454 if (error == NULL)
2455 error = repo_error;
2457 for (i = 0; i < bca.nlines; i++) {
2458 struct blame_line *bline = &bca.lines[i];
2459 free(bline->id_str);
2460 free(bline->committer);
2462 free(bca.lines);
2463 free(bca.line_offsets);
2464 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2465 error = got_error_from_errno("fclose");
2466 return error;
2469 __dead static void
2470 usage_tree(void)
2472 fprintf(stderr,
2473 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2474 getprogname());
2475 exit(1);
2478 static void
2479 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2480 const char *root_path)
2482 int is_root_path = (strcmp(path, root_path) == 0);
2483 const char *modestr = "";
2485 path += strlen(root_path);
2486 while (path[0] == '/')
2487 path++;
2489 if (S_ISLNK(te->mode))
2490 modestr = "@";
2491 else if (S_ISDIR(te->mode))
2492 modestr = "/";
2493 else if (te->mode & S_IXUSR)
2494 modestr = "*";
2496 printf("%s%s%s%s%s\n", id ? id : "", path,
2497 is_root_path ? "" : "/", te->name, modestr);
2500 static const struct got_error *
2501 print_tree(const char *path, struct got_object_id *commit_id,
2502 int show_ids, int recurse, const char *root_path,
2503 struct got_repository *repo)
2505 const struct got_error *err = NULL;
2506 struct got_object_id *tree_id = NULL;
2507 struct got_tree_object *tree = NULL;
2508 const struct got_tree_entries *entries;
2509 struct got_tree_entry *te;
2511 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2512 if (err)
2513 goto done;
2515 err = got_object_open_as_tree(&tree, repo, tree_id);
2516 if (err)
2517 goto done;
2518 entries = got_object_tree_get_entries(tree);
2519 te = SIMPLEQ_FIRST(&entries->head);
2520 while (te) {
2521 char *id = NULL;
2523 if (sigint_received || sigpipe_received)
2524 break;
2526 if (show_ids) {
2527 char *id_str;
2528 err = got_object_id_str(&id_str, te->id);
2529 if (err)
2530 goto done;
2531 if (asprintf(&id, "%s ", id_str) == -1) {
2532 err = got_error_from_errno("asprintf");
2533 free(id_str);
2534 goto done;
2536 free(id_str);
2538 print_entry(te, id, path, root_path);
2539 free(id);
2541 if (recurse && S_ISDIR(te->mode)) {
2542 char *child_path;
2543 if (asprintf(&child_path, "%s%s%s", path,
2544 path[0] == '/' && path[1] == '\0' ? "" : "/",
2545 te->name) == -1) {
2546 err = got_error_from_errno("asprintf");
2547 goto done;
2549 err = print_tree(child_path, commit_id, show_ids, 1,
2550 root_path, repo);
2551 free(child_path);
2552 if (err)
2553 goto done;
2556 te = SIMPLEQ_NEXT(te, entry);
2558 done:
2559 if (tree)
2560 got_object_tree_close(tree);
2561 free(tree_id);
2562 return err;
2565 static const struct got_error *
2566 cmd_tree(int argc, char *argv[])
2568 const struct got_error *error;
2569 struct got_repository *repo = NULL;
2570 struct got_worktree *worktree = NULL;
2571 const char *path;
2572 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2573 struct got_object_id *commit_id = NULL;
2574 char *commit_id_str = NULL;
2575 int show_ids = 0, recurse = 0;
2576 int ch;
2578 #ifndef PROFILE
2579 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2580 NULL) == -1)
2581 err(1, "pledge");
2582 #endif
2584 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2585 switch (ch) {
2586 case 'c':
2587 commit_id_str = optarg;
2588 break;
2589 case 'r':
2590 repo_path = realpath(optarg, NULL);
2591 if (repo_path == NULL)
2592 err(1, "-r option");
2593 got_path_strip_trailing_slashes(repo_path);
2594 break;
2595 case 'i':
2596 show_ids = 1;
2597 break;
2598 case 'R':
2599 recurse = 1;
2600 break;
2601 default:
2602 usage_tree();
2603 /* NOTREACHED */
2607 argc -= optind;
2608 argv += optind;
2610 if (argc == 1)
2611 path = argv[0];
2612 else if (argc > 1)
2613 usage_tree();
2614 else
2615 path = NULL;
2617 cwd = getcwd(NULL, 0);
2618 if (cwd == NULL) {
2619 error = got_error_from_errno("getcwd");
2620 goto done;
2622 if (repo_path == NULL) {
2623 error = got_worktree_open(&worktree, cwd);
2624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2625 goto done;
2626 else
2627 error = NULL;
2628 if (worktree) {
2629 repo_path =
2630 strdup(got_worktree_get_repo_path(worktree));
2631 if (repo_path == NULL)
2632 error = got_error_from_errno("strdup");
2633 if (error)
2634 goto done;
2635 } else {
2636 repo_path = strdup(cwd);
2637 if (repo_path == NULL) {
2638 error = got_error_from_errno("strdup");
2639 goto done;
2644 error = got_repo_open(&repo, repo_path);
2645 if (error != NULL)
2646 goto done;
2648 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2649 if (error)
2650 goto done;
2652 if (path == NULL) {
2653 if (worktree) {
2654 char *p, *worktree_subdir = cwd +
2655 strlen(got_worktree_get_root_path(worktree));
2656 if (asprintf(&p, "%s/%s",
2657 got_worktree_get_path_prefix(worktree),
2658 worktree_subdir) == -1) {
2659 error = got_error_from_errno("asprintf");
2660 goto done;
2662 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2663 free(p);
2664 if (error)
2665 goto done;
2666 } else
2667 path = "/";
2669 if (in_repo_path == NULL) {
2670 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2671 if (error != NULL)
2672 goto done;
2675 if (commit_id_str == NULL) {
2676 struct got_reference *head_ref;
2677 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2678 if (error != NULL)
2679 goto done;
2680 error = got_ref_resolve(&commit_id, repo, head_ref);
2681 got_ref_close(head_ref);
2682 if (error != NULL)
2683 goto done;
2684 } else {
2685 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2686 if (error)
2687 goto done;
2690 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2691 in_repo_path, repo);
2692 done:
2693 free(in_repo_path);
2694 free(repo_path);
2695 free(cwd);
2696 free(commit_id);
2697 if (worktree)
2698 got_worktree_close(worktree);
2699 if (repo) {
2700 const struct got_error *repo_error;
2701 repo_error = got_repo_close(repo);
2702 if (error == NULL)
2703 error = repo_error;
2705 return error;
2708 __dead static void
2709 usage_status(void)
2711 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2712 exit(1);
2715 static const struct got_error *
2716 print_status(void *arg, unsigned char status, unsigned char staged_status,
2717 const char *path, struct got_object_id *blob_id,
2718 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2720 if (status == staged_status && (status == GOT_STATUS_DELETE))
2721 status = GOT_STATUS_NO_CHANGE;
2722 printf("%c%c %s\n", status, staged_status, path);
2723 return NULL;
2726 static const struct got_error *
2727 cmd_status(int argc, char *argv[])
2729 const struct got_error *error = NULL;
2730 struct got_repository *repo = NULL;
2731 struct got_worktree *worktree = NULL;
2732 char *cwd = NULL;
2733 struct got_pathlist_head paths;
2734 struct got_pathlist_entry *pe;
2735 int ch;
2737 TAILQ_INIT(&paths);
2739 while ((ch = getopt(argc, argv, "")) != -1) {
2740 switch (ch) {
2741 default:
2742 usage_status();
2743 /* NOTREACHED */
2747 argc -= optind;
2748 argv += optind;
2750 #ifndef PROFILE
2751 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2752 NULL) == -1)
2753 err(1, "pledge");
2754 #endif
2755 cwd = getcwd(NULL, 0);
2756 if (cwd == NULL) {
2757 error = got_error_from_errno("getcwd");
2758 goto done;
2761 error = got_worktree_open(&worktree, cwd);
2762 if (error != NULL)
2763 goto done;
2765 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2766 if (error != NULL)
2767 goto done;
2769 error = apply_unveil(got_repo_get_path(repo), 1,
2770 got_worktree_get_root_path(worktree));
2771 if (error)
2772 goto done;
2774 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2775 if (error)
2776 goto done;
2778 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2779 check_cancelled, NULL);
2780 done:
2781 TAILQ_FOREACH(pe, &paths, entry)
2782 free((char *)pe->path);
2783 got_pathlist_free(&paths);
2784 free(cwd);
2785 return error;
2788 __dead static void
2789 usage_ref(void)
2791 fprintf(stderr,
2792 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2793 getprogname());
2794 exit(1);
2797 static const struct got_error *
2798 list_refs(struct got_repository *repo)
2800 static const struct got_error *err = NULL;
2801 struct got_reflist_head refs;
2802 struct got_reflist_entry *re;
2804 SIMPLEQ_INIT(&refs);
2805 err = got_ref_list(&refs, repo);
2806 if (err)
2807 return err;
2809 SIMPLEQ_FOREACH(re, &refs, entry) {
2810 char *refstr;
2811 refstr = got_ref_to_str(re->ref);
2812 if (refstr == NULL)
2813 return got_error_from_errno("got_ref_to_str");
2814 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2815 free(refstr);
2818 got_ref_list_free(&refs);
2819 return NULL;
2822 static const struct got_error *
2823 delete_ref(struct got_repository *repo, const char *refname)
2825 const struct got_error *err = NULL;
2826 struct got_reference *ref;
2828 err = got_ref_open(&ref, repo, refname, 0);
2829 if (err)
2830 return err;
2832 err = got_ref_delete(ref, repo);
2833 got_ref_close(ref);
2834 return err;
2837 static const struct got_error *
2838 add_ref(struct got_repository *repo, const char *refname, const char *target)
2840 const struct got_error *err = NULL;
2841 struct got_object_id *id;
2842 struct got_reference *ref = NULL;
2845 * Don't let the user create a reference named '-'.
2846 * While technically a valid reference name, this case is usually
2847 * an unintended typo.
2849 if (refname[0] == '-' && refname[1] == '\0')
2850 return got_error(GOT_ERR_BAD_REF_NAME);
2852 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2853 repo);
2854 if (err) {
2855 struct got_reference *target_ref;
2857 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2858 return err;
2859 err = got_ref_open(&target_ref, repo, target, 0);
2860 if (err)
2861 return err;
2862 err = got_ref_resolve(&id, repo, target_ref);
2863 got_ref_close(target_ref);
2864 if (err)
2865 return err;
2868 err = got_ref_alloc(&ref, refname, id);
2869 if (err)
2870 goto done;
2872 err = got_ref_write(ref, repo);
2873 done:
2874 if (ref)
2875 got_ref_close(ref);
2876 free(id);
2877 return err;
2880 static const struct got_error *
2881 add_symref(struct got_repository *repo, const char *refname, const char *target)
2883 const struct got_error *err = NULL;
2884 struct got_reference *ref = NULL;
2885 struct got_reference *target_ref = NULL;
2888 * Don't let the user create a reference named '-'.
2889 * While technically a valid reference name, this case is usually
2890 * an unintended typo.
2892 if (refname[0] == '-' && refname[1] == '\0')
2893 return got_error(GOT_ERR_BAD_REF_NAME);
2895 err = got_ref_open(&target_ref, repo, target, 0);
2896 if (err)
2897 return err;
2899 err = got_ref_alloc_symref(&ref, refname, target_ref);
2900 if (err)
2901 goto done;
2903 err = got_ref_write(ref, repo);
2904 done:
2905 if (target_ref)
2906 got_ref_close(target_ref);
2907 if (ref)
2908 got_ref_close(ref);
2909 return err;
2912 static const struct got_error *
2913 cmd_ref(int argc, char *argv[])
2915 const struct got_error *error = NULL;
2916 struct got_repository *repo = NULL;
2917 struct got_worktree *worktree = NULL;
2918 char *cwd = NULL, *repo_path = NULL;
2919 int ch, do_list = 0, create_symref = 0;
2920 const char *delref = NULL;
2922 /* TODO: Add -s option for adding symbolic references. */
2923 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2924 switch (ch) {
2925 case 'd':
2926 delref = optarg;
2927 break;
2928 case 'r':
2929 repo_path = realpath(optarg, NULL);
2930 if (repo_path == NULL)
2931 err(1, "-r option");
2932 got_path_strip_trailing_slashes(repo_path);
2933 break;
2934 case 'l':
2935 do_list = 1;
2936 break;
2937 case 's':
2938 create_symref = 1;
2939 break;
2940 default:
2941 usage_ref();
2942 /* NOTREACHED */
2946 if (do_list && delref)
2947 errx(1, "-l and -d options are mutually exclusive\n");
2949 argc -= optind;
2950 argv += optind;
2952 if (do_list || delref) {
2953 if (create_symref)
2954 errx(1, "-s option cannot be used together with the "
2955 "-l or -d options");
2956 if (argc > 0)
2957 usage_ref();
2958 } else if (argc != 2)
2959 usage_ref();
2961 #ifndef PROFILE
2962 if (do_list) {
2963 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2964 NULL) == -1)
2965 err(1, "pledge");
2966 } else {
2967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2968 "sendfd unveil", NULL) == -1)
2969 err(1, "pledge");
2971 #endif
2972 cwd = getcwd(NULL, 0);
2973 if (cwd == NULL) {
2974 error = got_error_from_errno("getcwd");
2975 goto done;
2978 if (repo_path == NULL) {
2979 error = got_worktree_open(&worktree, cwd);
2980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2981 goto done;
2982 else
2983 error = NULL;
2984 if (worktree) {
2985 repo_path =
2986 strdup(got_worktree_get_repo_path(worktree));
2987 if (repo_path == NULL)
2988 error = got_error_from_errno("strdup");
2989 if (error)
2990 goto done;
2991 } else {
2992 repo_path = strdup(cwd);
2993 if (repo_path == NULL) {
2994 error = got_error_from_errno("strdup");
2995 goto done;
3000 error = got_repo_open(&repo, repo_path);
3001 if (error != NULL)
3002 goto done;
3004 error = apply_unveil(got_repo_get_path(repo), do_list,
3005 worktree ? got_worktree_get_root_path(worktree) : NULL);
3006 if (error)
3007 goto done;
3009 if (do_list)
3010 error = list_refs(repo);
3011 else if (delref)
3012 error = delete_ref(repo, delref);
3013 else if (create_symref)
3014 error = add_symref(repo, argv[0], argv[1]);
3015 else
3016 error = add_ref(repo, argv[0], argv[1]);
3017 done:
3018 if (repo)
3019 got_repo_close(repo);
3020 if (worktree)
3021 got_worktree_close(worktree);
3022 free(cwd);
3023 free(repo_path);
3024 return error;
3027 __dead static void
3028 usage_branch(void)
3030 fprintf(stderr,
3031 "usage: %s branch [-r repository] -l | -d name | "
3032 "name [base-branch]\n", getprogname());
3033 exit(1);
3036 static const struct got_error *
3037 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3039 static const struct got_error *err = NULL;
3040 struct got_reflist_head refs;
3041 struct got_reflist_entry *re;
3043 SIMPLEQ_INIT(&refs);
3045 err = got_ref_list(&refs, repo);
3046 if (err)
3047 return err;
3049 SIMPLEQ_FOREACH(re, &refs, entry) {
3050 const char *refname, *marker = " ";
3051 char *refstr;
3052 refname = got_ref_get_name(re->ref);
3053 if (strncmp(refname, "refs/heads/", 11) != 0)
3054 continue;
3055 if (worktree && strcmp(refname,
3056 got_worktree_get_head_ref_name(worktree)) == 0) {
3057 struct got_object_id *id = NULL;
3058 err = got_ref_resolve(&id, repo, re->ref);
3059 if (err)
3060 return err;
3061 if (got_object_id_cmp(id,
3062 got_worktree_get_base_commit_id(worktree)) == 0)
3063 marker = "* ";
3064 else
3065 marker = "~ ";
3066 free(id);
3068 refname += 11;
3069 refstr = got_ref_to_str(re->ref);
3070 if (refstr == NULL)
3071 return got_error_from_errno("got_ref_to_str");
3072 printf("%s%s: %s\n", marker, refname, refstr);
3073 free(refstr);
3076 got_ref_list_free(&refs);
3077 return NULL;
3080 static const struct got_error *
3081 delete_branch(struct got_repository *repo, const char *branch_name)
3083 const struct got_error *err = NULL;
3084 struct got_reference *ref;
3085 char *refname;
3087 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3088 return got_error_from_errno("asprintf");
3090 err = got_ref_open(&ref, repo, refname, 0);
3091 if (err)
3092 goto done;
3094 err = got_ref_delete(ref, repo);
3095 got_ref_close(ref);
3096 done:
3097 free(refname);
3098 return err;
3101 static const struct got_error *
3102 add_branch(struct got_repository *repo, const char *branch_name,
3103 const char *base_branch)
3105 const struct got_error *err = NULL;
3106 struct got_object_id *id = NULL;
3107 struct got_reference *ref = NULL;
3108 char *base_refname = NULL, *refname = NULL;
3109 struct got_reference *base_ref;
3112 * Don't let the user create a branch named '-'.
3113 * While technically a valid reference name, this case is usually
3114 * an unintended typo.
3116 if (branch_name[0] == '-' && branch_name[1] == '\0')
3117 return got_error(GOT_ERR_BAD_REF_NAME);
3119 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3120 base_refname = strdup(GOT_REF_HEAD);
3121 if (base_refname == NULL)
3122 return got_error_from_errno("strdup");
3123 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3124 return got_error_from_errno("asprintf");
3126 err = got_ref_open(&base_ref, repo, base_refname, 0);
3127 if (err)
3128 goto done;
3129 err = got_ref_resolve(&id, repo, base_ref);
3130 got_ref_close(base_ref);
3131 if (err)
3132 goto done;
3134 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3135 err = got_error_from_errno("asprintf");
3136 goto done;
3139 err = got_ref_open(&ref, repo, refname, 0);
3140 if (err == NULL) {
3141 err = got_error(GOT_ERR_BRANCH_EXISTS);
3142 goto done;
3143 } else if (err->code != GOT_ERR_NOT_REF)
3144 goto done;
3146 err = got_ref_alloc(&ref, refname, id);
3147 if (err)
3148 goto done;
3150 err = got_ref_write(ref, repo);
3151 done:
3152 if (ref)
3153 got_ref_close(ref);
3154 free(id);
3155 free(base_refname);
3156 free(refname);
3157 return err;
3160 static const struct got_error *
3161 cmd_branch(int argc, char *argv[])
3163 const struct got_error *error = NULL;
3164 struct got_repository *repo = NULL;
3165 struct got_worktree *worktree = NULL;
3166 char *cwd = NULL, *repo_path = NULL;
3167 int ch, do_list = 0;
3168 const char *delref = NULL;
3170 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3171 switch (ch) {
3172 case 'd':
3173 delref = optarg;
3174 break;
3175 case 'r':
3176 repo_path = realpath(optarg, NULL);
3177 if (repo_path == NULL)
3178 err(1, "-r option");
3179 got_path_strip_trailing_slashes(repo_path);
3180 break;
3181 case 'l':
3182 do_list = 1;
3183 break;
3184 default:
3185 usage_branch();
3186 /* NOTREACHED */
3190 if (do_list && delref)
3191 errx(1, "-l and -d options are mutually exclusive\n");
3193 argc -= optind;
3194 argv += optind;
3196 if (do_list || delref) {
3197 if (argc > 0)
3198 usage_branch();
3199 } else if (argc < 1 || argc > 2)
3200 usage_branch();
3202 #ifndef PROFILE
3203 if (do_list) {
3204 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3205 NULL) == -1)
3206 err(1, "pledge");
3207 } else {
3208 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3209 "sendfd unveil", NULL) == -1)
3210 err(1, "pledge");
3212 #endif
3213 cwd = getcwd(NULL, 0);
3214 if (cwd == NULL) {
3215 error = got_error_from_errno("getcwd");
3216 goto done;
3219 if (repo_path == NULL) {
3220 error = got_worktree_open(&worktree, cwd);
3221 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3222 goto done;
3223 else
3224 error = NULL;
3225 if (worktree) {
3226 repo_path =
3227 strdup(got_worktree_get_repo_path(worktree));
3228 if (repo_path == NULL)
3229 error = got_error_from_errno("strdup");
3230 if (error)
3231 goto done;
3232 } else {
3233 repo_path = strdup(cwd);
3234 if (repo_path == NULL) {
3235 error = got_error_from_errno("strdup");
3236 goto done;
3241 error = got_repo_open(&repo, repo_path);
3242 if (error != NULL)
3243 goto done;
3245 error = apply_unveil(got_repo_get_path(repo), do_list,
3246 worktree ? got_worktree_get_root_path(worktree) : NULL);
3247 if (error)
3248 goto done;
3250 if (do_list)
3251 error = list_branches(repo, worktree);
3252 else if (delref)
3253 error = delete_branch(repo, delref);
3254 else {
3255 const char *base_branch;
3256 if (argc == 1) {
3257 base_branch = worktree ?
3258 got_worktree_get_head_ref_name(worktree) :
3259 GOT_REF_HEAD;
3260 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3261 base_branch += 11;
3262 } else
3263 base_branch = argv[1];
3264 error = add_branch(repo, argv[0], base_branch);
3266 done:
3267 if (repo)
3268 got_repo_close(repo);
3269 if (worktree)
3270 got_worktree_close(worktree);
3271 free(cwd);
3272 free(repo_path);
3273 return error;
3276 __dead static void
3277 usage_add(void)
3279 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3280 exit(1);
3283 static const struct got_error *
3284 cmd_add(int argc, char *argv[])
3286 const struct got_error *error = NULL;
3287 struct got_repository *repo = NULL;
3288 struct got_worktree *worktree = NULL;
3289 char *cwd = NULL;
3290 struct got_pathlist_head paths;
3291 struct got_pathlist_entry *pe;
3292 int ch;
3294 TAILQ_INIT(&paths);
3296 while ((ch = getopt(argc, argv, "")) != -1) {
3297 switch (ch) {
3298 default:
3299 usage_add();
3300 /* NOTREACHED */
3304 argc -= optind;
3305 argv += optind;
3307 #ifndef PROFILE
3308 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3309 NULL) == -1)
3310 err(1, "pledge");
3311 #endif
3312 if (argc < 1)
3313 usage_add();
3315 cwd = getcwd(NULL, 0);
3316 if (cwd == NULL) {
3317 error = got_error_from_errno("getcwd");
3318 goto done;
3321 error = got_worktree_open(&worktree, cwd);
3322 if (error)
3323 goto done;
3325 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3326 if (error != NULL)
3327 goto done;
3329 error = apply_unveil(got_repo_get_path(repo), 1,
3330 got_worktree_get_root_path(worktree));
3331 if (error)
3332 goto done;
3334 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3335 if (error)
3336 goto done;
3338 error = got_worktree_schedule_add(worktree, &paths, print_status,
3339 NULL, repo);
3340 done:
3341 if (repo)
3342 got_repo_close(repo);
3343 if (worktree)
3344 got_worktree_close(worktree);
3345 TAILQ_FOREACH(pe, &paths, entry)
3346 free((char *)pe->path);
3347 got_pathlist_free(&paths);
3348 free(cwd);
3349 return error;
3352 __dead static void
3353 usage_remove(void)
3355 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3356 exit(1);
3359 static const struct got_error *
3360 cmd_remove(int argc, char *argv[])
3362 const struct got_error *error = NULL;
3363 struct got_worktree *worktree = NULL;
3364 struct got_repository *repo = NULL;
3365 char *cwd = NULL;
3366 struct got_pathlist_head paths;
3367 struct got_pathlist_entry *pe;
3368 int ch, delete_local_mods = 0;
3370 TAILQ_INIT(&paths);
3372 while ((ch = getopt(argc, argv, "f")) != -1) {
3373 switch (ch) {
3374 case 'f':
3375 delete_local_mods = 1;
3376 break;
3377 default:
3378 usage_add();
3379 /* NOTREACHED */
3383 argc -= optind;
3384 argv += optind;
3386 #ifndef PROFILE
3387 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3388 NULL) == -1)
3389 err(1, "pledge");
3390 #endif
3391 if (argc < 1)
3392 usage_remove();
3394 cwd = getcwd(NULL, 0);
3395 if (cwd == NULL) {
3396 error = got_error_from_errno("getcwd");
3397 goto done;
3399 error = got_worktree_open(&worktree, cwd);
3400 if (error)
3401 goto done;
3403 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3404 if (error)
3405 goto done;
3407 error = apply_unveil(got_repo_get_path(repo), 1,
3408 got_worktree_get_root_path(worktree));
3409 if (error)
3410 goto done;
3412 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3413 if (error)
3414 goto done;
3416 error = got_worktree_schedule_delete(worktree, &paths,
3417 delete_local_mods, print_status, NULL, repo);
3418 if (error)
3419 goto done;
3420 done:
3421 if (repo)
3422 got_repo_close(repo);
3423 if (worktree)
3424 got_worktree_close(worktree);
3425 TAILQ_FOREACH(pe, &paths, entry)
3426 free((char *)pe->path);
3427 got_pathlist_free(&paths);
3428 free(cwd);
3429 return error;
3432 __dead static void
3433 usage_revert(void)
3435 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3436 "path ...\n", getprogname());
3437 exit(1);
3440 static const struct got_error *
3441 revert_progress(void *arg, unsigned char status, const char *path)
3443 while (path[0] == '/')
3444 path++;
3445 printf("%c %s\n", status, path);
3446 return NULL;
3449 struct choose_patch_arg {
3450 FILE *patch_script_file;
3451 const char *action;
3454 static const struct got_error *
3455 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3456 int nchanges, const char *action)
3458 char *line = NULL;
3459 size_t linesize = 0;
3460 ssize_t linelen;
3462 switch (status) {
3463 case GOT_STATUS_ADD:
3464 printf("A %s\n%s this addition? [y/n] ", path, action);
3465 break;
3466 case GOT_STATUS_DELETE:
3467 printf("D %s\n%s this deletion? [y/n] ", path, action);
3468 break;
3469 case GOT_STATUS_MODIFY:
3470 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3471 return got_error_from_errno("fseek");
3472 printf(GOT_COMMIT_SEP_STR);
3473 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3474 printf("%s", line);
3475 if (ferror(patch_file))
3476 return got_error_from_errno("getline");
3477 printf(GOT_COMMIT_SEP_STR);
3478 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3479 path, n, nchanges, action);
3480 break;
3481 default:
3482 return got_error_path(path, GOT_ERR_FILE_STATUS);
3485 return NULL;
3488 static const struct got_error *
3489 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3490 FILE *patch_file, int n, int nchanges)
3492 const struct got_error *err = NULL;
3493 char *line = NULL;
3494 size_t linesize = 0;
3495 ssize_t linelen;
3496 int resp = ' ';
3497 struct choose_patch_arg *a = arg;
3499 *choice = GOT_PATCH_CHOICE_NONE;
3501 if (a->patch_script_file) {
3502 char *nl;
3503 err = show_change(status, path, patch_file, n, nchanges,
3504 a->action);
3505 if (err)
3506 return err;
3507 linelen = getline(&line, &linesize, a->patch_script_file);
3508 if (linelen == -1) {
3509 if (ferror(a->patch_script_file))
3510 return got_error_from_errno("getline");
3511 return NULL;
3513 nl = strchr(line, '\n');
3514 if (nl)
3515 *nl = '\0';
3516 if (strcmp(line, "y") == 0) {
3517 *choice = GOT_PATCH_CHOICE_YES;
3518 printf("y\n");
3519 } else if (strcmp(line, "n") == 0) {
3520 *choice = GOT_PATCH_CHOICE_NO;
3521 printf("n\n");
3522 } else if (strcmp(line, "q") == 0 &&
3523 status == GOT_STATUS_MODIFY) {
3524 *choice = GOT_PATCH_CHOICE_QUIT;
3525 printf("q\n");
3526 } else
3527 printf("invalid response '%s'\n", line);
3528 free(line);
3529 return NULL;
3532 while (resp != 'y' && resp != 'n' && resp != 'q') {
3533 err = show_change(status, path, patch_file, n, nchanges,
3534 a->action);
3535 if (err)
3536 return err;
3537 resp = getchar();
3538 if (resp == '\n')
3539 resp = getchar();
3540 if (status == GOT_STATUS_MODIFY) {
3541 if (resp != 'y' && resp != 'n' && resp != 'q') {
3542 printf("invalid response '%c'\n", resp);
3543 resp = ' ';
3545 } else if (resp != 'y' && resp != 'n') {
3546 printf("invalid response '%c'\n", resp);
3547 resp = ' ';
3551 if (resp == 'y')
3552 *choice = GOT_PATCH_CHOICE_YES;
3553 else if (resp == 'n')
3554 *choice = GOT_PATCH_CHOICE_NO;
3555 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3556 *choice = GOT_PATCH_CHOICE_QUIT;
3558 return NULL;
3562 static const struct got_error *
3563 cmd_revert(int argc, char *argv[])
3565 const struct got_error *error = NULL;
3566 struct got_worktree *worktree = NULL;
3567 struct got_repository *repo = NULL;
3568 char *cwd = NULL, *path = NULL;
3569 struct got_pathlist_head paths;
3570 struct got_pathlist_entry *pe;
3571 int ch, can_recurse = 0, pflag = 0;
3572 FILE *patch_script_file = NULL;
3573 const char *patch_script_path = NULL;
3574 struct choose_patch_arg cpa;
3576 TAILQ_INIT(&paths);
3578 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3579 switch (ch) {
3580 case 'p':
3581 pflag = 1;
3582 break;
3583 case 'F':
3584 patch_script_path = optarg;
3585 break;
3586 case 'R':
3587 can_recurse = 1;
3588 break;
3589 default:
3590 usage_revert();
3591 /* NOTREACHED */
3595 argc -= optind;
3596 argv += optind;
3598 #ifndef PROFILE
3599 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3600 "unveil", NULL) == -1)
3601 err(1, "pledge");
3602 #endif
3603 if (argc < 1)
3604 usage_revert();
3605 if (patch_script_path && !pflag)
3606 errx(1, "-F option can only be used together with -p option");
3608 cwd = getcwd(NULL, 0);
3609 if (cwd == NULL) {
3610 error = got_error_from_errno("getcwd");
3611 goto done;
3613 error = got_worktree_open(&worktree, cwd);
3614 if (error)
3615 goto done;
3617 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3618 if (error != NULL)
3619 goto done;
3621 if (patch_script_path) {
3622 patch_script_file = fopen(patch_script_path, "r");
3623 if (patch_script_file == NULL) {
3624 error = got_error_from_errno2("fopen",
3625 patch_script_path);
3626 goto done;
3629 error = apply_unveil(got_repo_get_path(repo), 1,
3630 got_worktree_get_root_path(worktree));
3631 if (error)
3632 goto done;
3634 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3635 if (error)
3636 goto done;
3638 if (!can_recurse) {
3639 char *ondisk_path;
3640 struct stat sb;
3641 TAILQ_FOREACH(pe, &paths, entry) {
3642 if (asprintf(&ondisk_path, "%s/%s",
3643 got_worktree_get_root_path(worktree),
3644 pe->path) == -1) {
3645 error = got_error_from_errno("asprintf");
3646 goto done;
3648 if (lstat(ondisk_path, &sb) == -1) {
3649 if (errno == ENOENT) {
3650 free(ondisk_path);
3651 continue;
3653 error = got_error_from_errno2("lstat",
3654 ondisk_path);
3655 free(ondisk_path);
3656 goto done;
3658 free(ondisk_path);
3659 if (S_ISDIR(sb.st_mode)) {
3660 error = got_error_msg(GOT_ERR_BAD_PATH,
3661 "reverting directories requires -R option");
3662 goto done;
3667 cpa.patch_script_file = patch_script_file;
3668 cpa.action = "revert";
3669 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3670 pflag ? choose_patch : NULL, &cpa, repo);
3671 if (error)
3672 goto done;
3673 done:
3674 if (patch_script_file && fclose(patch_script_file) == EOF &&
3675 error == NULL)
3676 error = got_error_from_errno2("fclose", patch_script_path);
3677 if (repo)
3678 got_repo_close(repo);
3679 if (worktree)
3680 got_worktree_close(worktree);
3681 free(path);
3682 free(cwd);
3683 return error;
3686 __dead static void
3687 usage_commit(void)
3689 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3690 getprogname());
3691 exit(1);
3694 struct collect_commit_logmsg_arg {
3695 const char *cmdline_log;
3696 const char *editor;
3697 const char *worktree_path;
3698 const char *branch_name;
3699 const char *repo_path;
3700 char *logmsg_path;
3704 static const struct got_error *
3705 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3706 void *arg)
3708 char *initial_content = NULL;
3709 struct got_pathlist_entry *pe;
3710 const struct got_error *err = NULL;
3711 char *template = NULL;
3712 struct collect_commit_logmsg_arg *a = arg;
3713 int fd;
3714 size_t len;
3716 /* if a message was specified on the command line, just use it */
3717 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3718 len = strlen(a->cmdline_log) + 1;
3719 *logmsg = malloc(len + 1);
3720 if (*logmsg == NULL)
3721 return got_error_from_errno("malloc");
3722 strlcpy(*logmsg, a->cmdline_log, len);
3723 return NULL;
3726 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3727 return got_error_from_errno("asprintf");
3729 if (asprintf(&initial_content,
3730 "\n# changes to be committed on branch %s:\n",
3731 a->branch_name) == -1)
3732 return got_error_from_errno("asprintf");
3734 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3735 if (err)
3736 goto done;
3738 dprintf(fd, initial_content);
3740 TAILQ_FOREACH(pe, commitable_paths, entry) {
3741 struct got_commitable *ct = pe->data;
3742 dprintf(fd, "# %c %s\n",
3743 got_commitable_get_status(ct),
3744 got_commitable_get_path(ct));
3746 close(fd);
3748 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3749 done:
3750 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3751 unlink(a->logmsg_path);
3752 free(a->logmsg_path);
3753 a->logmsg_path = NULL;
3755 free(initial_content);
3756 free(template);
3758 /* Editor is done; we can now apply unveil(2) */
3759 if (err == NULL) {
3760 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3761 if (err) {
3762 free(*logmsg);
3763 *logmsg = NULL;
3766 return err;
3769 static const struct got_error *
3770 cmd_commit(int argc, char *argv[])
3772 const struct got_error *error = NULL;
3773 struct got_worktree *worktree = NULL;
3774 struct got_repository *repo = NULL;
3775 char *cwd = NULL, *id_str = NULL;
3776 struct got_object_id *id = NULL;
3777 const char *logmsg = NULL;
3778 const char *author;
3779 struct collect_commit_logmsg_arg cl_arg;
3780 char *editor = NULL;
3781 int ch, rebase_in_progress, histedit_in_progress;
3782 struct got_pathlist_head paths;
3784 TAILQ_INIT(&paths);
3785 cl_arg.logmsg_path = NULL;
3787 while ((ch = getopt(argc, argv, "m:")) != -1) {
3788 switch (ch) {
3789 case 'm':
3790 logmsg = optarg;
3791 break;
3792 default:
3793 usage_commit();
3794 /* NOTREACHED */
3798 argc -= optind;
3799 argv += optind;
3801 #ifndef PROFILE
3802 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3803 "unveil", NULL) == -1)
3804 err(1, "pledge");
3805 #endif
3806 error = get_author(&author);
3807 if (error)
3808 return error;
3810 cwd = getcwd(NULL, 0);
3811 if (cwd == NULL) {
3812 error = got_error_from_errno("getcwd");
3813 goto done;
3815 error = got_worktree_open(&worktree, cwd);
3816 if (error)
3817 goto done;
3819 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3820 if (error)
3821 goto done;
3822 if (rebase_in_progress) {
3823 error = got_error(GOT_ERR_REBASING);
3824 goto done;
3827 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3828 worktree);
3829 if (error)
3830 goto done;
3832 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3833 if (error != NULL)
3834 goto done;
3837 * unveil(2) traverses exec(2); if an editor is used we have
3838 * to apply unveil after the log message has been written.
3840 if (logmsg == NULL || strlen(logmsg) == 0)
3841 error = get_editor(&editor);
3842 else
3843 error = apply_unveil(got_repo_get_path(repo), 0,
3844 got_worktree_get_root_path(worktree));
3845 if (error)
3846 goto done;
3848 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3849 if (error)
3850 goto done;
3852 cl_arg.editor = editor;
3853 cl_arg.cmdline_log = logmsg;
3854 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3855 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3856 if (!histedit_in_progress) {
3857 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3858 error = got_error(GOT_ERR_COMMIT_BRANCH);
3859 goto done;
3861 cl_arg.branch_name += 11;
3863 cl_arg.repo_path = got_repo_get_path(repo);
3864 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3865 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3866 if (error) {
3867 if (cl_arg.logmsg_path)
3868 fprintf(stderr, "%s: log message preserved in %s\n",
3869 getprogname(), cl_arg.logmsg_path);
3870 goto done;
3873 if (cl_arg.logmsg_path)
3874 unlink(cl_arg.logmsg_path);
3876 error = got_object_id_str(&id_str, id);
3877 if (error)
3878 goto done;
3879 printf("Created commit %s\n", id_str);
3880 done:
3881 free(cl_arg.logmsg_path);
3882 if (repo)
3883 got_repo_close(repo);
3884 if (worktree)
3885 got_worktree_close(worktree);
3886 free(cwd);
3887 free(id_str);
3888 free(editor);
3889 return error;
3892 __dead static void
3893 usage_cherrypick(void)
3895 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3896 exit(1);
3899 static const struct got_error *
3900 cmd_cherrypick(int argc, char *argv[])
3902 const struct got_error *error = NULL;
3903 struct got_worktree *worktree = NULL;
3904 struct got_repository *repo = NULL;
3905 char *cwd = NULL, *commit_id_str = NULL;
3906 struct got_object_id *commit_id = NULL;
3907 struct got_commit_object *commit = NULL;
3908 struct got_object_qid *pid;
3909 struct got_reference *head_ref = NULL;
3910 int ch, did_something = 0;
3912 while ((ch = getopt(argc, argv, "")) != -1) {
3913 switch (ch) {
3914 default:
3915 usage_cherrypick();
3916 /* NOTREACHED */
3920 argc -= optind;
3921 argv += optind;
3923 #ifndef PROFILE
3924 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3925 "unveil", NULL) == -1)
3926 err(1, "pledge");
3927 #endif
3928 if (argc != 1)
3929 usage_cherrypick();
3931 cwd = getcwd(NULL, 0);
3932 if (cwd == NULL) {
3933 error = got_error_from_errno("getcwd");
3934 goto done;
3936 error = got_worktree_open(&worktree, cwd);
3937 if (error)
3938 goto done;
3940 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3941 if (error != NULL)
3942 goto done;
3944 error = apply_unveil(got_repo_get_path(repo), 0,
3945 got_worktree_get_root_path(worktree));
3946 if (error)
3947 goto done;
3949 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3950 GOT_OBJ_TYPE_COMMIT, repo);
3951 if (error != NULL) {
3952 struct got_reference *ref;
3953 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3954 goto done;
3955 error = got_ref_open(&ref, repo, argv[0], 0);
3956 if (error != NULL)
3957 goto done;
3958 error = got_ref_resolve(&commit_id, repo, ref);
3959 got_ref_close(ref);
3960 if (error != NULL)
3961 goto done;
3963 error = got_object_id_str(&commit_id_str, commit_id);
3964 if (error)
3965 goto done;
3967 error = got_ref_open(&head_ref, repo,
3968 got_worktree_get_head_ref_name(worktree), 0);
3969 if (error != NULL)
3970 goto done;
3972 error = check_same_branch(commit_id, head_ref, NULL, repo);
3973 if (error) {
3974 if (error->code != GOT_ERR_ANCESTRY)
3975 goto done;
3976 error = NULL;
3977 } else {
3978 error = got_error(GOT_ERR_SAME_BRANCH);
3979 goto done;
3982 error = got_object_open_as_commit(&commit, repo, commit_id);
3983 if (error)
3984 goto done;
3985 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3986 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3987 commit_id, repo, update_progress, &did_something, check_cancelled,
3988 NULL);
3989 if (error != NULL)
3990 goto done;
3992 if (did_something)
3993 printf("Merged commit %s\n", commit_id_str);
3994 done:
3995 if (commit)
3996 got_object_commit_close(commit);
3997 free(commit_id_str);
3998 if (head_ref)
3999 got_ref_close(head_ref);
4000 if (worktree)
4001 got_worktree_close(worktree);
4002 if (repo)
4003 got_repo_close(repo);
4004 return error;
4007 __dead static void
4008 usage_backout(void)
4010 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4011 exit(1);
4014 static const struct got_error *
4015 cmd_backout(int argc, char *argv[])
4017 const struct got_error *error = NULL;
4018 struct got_worktree *worktree = NULL;
4019 struct got_repository *repo = NULL;
4020 char *cwd = NULL, *commit_id_str = NULL;
4021 struct got_object_id *commit_id = NULL;
4022 struct got_commit_object *commit = NULL;
4023 struct got_object_qid *pid;
4024 struct got_reference *head_ref = NULL;
4025 int ch, did_something = 0;
4027 while ((ch = getopt(argc, argv, "")) != -1) {
4028 switch (ch) {
4029 default:
4030 usage_backout();
4031 /* NOTREACHED */
4035 argc -= optind;
4036 argv += optind;
4038 #ifndef PROFILE
4039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4040 "unveil", NULL) == -1)
4041 err(1, "pledge");
4042 #endif
4043 if (argc != 1)
4044 usage_backout();
4046 cwd = getcwd(NULL, 0);
4047 if (cwd == NULL) {
4048 error = got_error_from_errno("getcwd");
4049 goto done;
4051 error = got_worktree_open(&worktree, cwd);
4052 if (error)
4053 goto done;
4055 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4056 if (error != NULL)
4057 goto done;
4059 error = apply_unveil(got_repo_get_path(repo), 0,
4060 got_worktree_get_root_path(worktree));
4061 if (error)
4062 goto done;
4064 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4065 GOT_OBJ_TYPE_COMMIT, repo);
4066 if (error != NULL) {
4067 struct got_reference *ref;
4068 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4069 goto done;
4070 error = got_ref_open(&ref, repo, argv[0], 0);
4071 if (error != NULL)
4072 goto done;
4073 error = got_ref_resolve(&commit_id, repo, ref);
4074 got_ref_close(ref);
4075 if (error != NULL)
4076 goto done;
4078 error = got_object_id_str(&commit_id_str, commit_id);
4079 if (error)
4080 goto done;
4082 error = got_ref_open(&head_ref, repo,
4083 got_worktree_get_head_ref_name(worktree), 0);
4084 if (error != NULL)
4085 goto done;
4087 error = check_same_branch(commit_id, head_ref, NULL, repo);
4088 if (error)
4089 goto done;
4091 error = got_object_open_as_commit(&commit, repo, commit_id);
4092 if (error)
4093 goto done;
4094 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4095 if (pid == NULL) {
4096 error = got_error(GOT_ERR_ROOT_COMMIT);
4097 goto done;
4100 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4101 update_progress, &did_something, check_cancelled, NULL);
4102 if (error != NULL)
4103 goto done;
4105 if (did_something)
4106 printf("Backed out commit %s\n", commit_id_str);
4107 done:
4108 if (commit)
4109 got_object_commit_close(commit);
4110 free(commit_id_str);
4111 if (head_ref)
4112 got_ref_close(head_ref);
4113 if (worktree)
4114 got_worktree_close(worktree);
4115 if (repo)
4116 got_repo_close(repo);
4117 return error;
4120 __dead static void
4121 usage_rebase(void)
4123 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4124 getprogname());
4125 exit(1);
4128 void
4129 trim_logmsg(char *logmsg, int limit)
4131 char *nl;
4132 size_t len;
4134 len = strlen(logmsg);
4135 if (len > limit)
4136 len = limit;
4137 logmsg[len] = '\0';
4138 nl = strchr(logmsg, '\n');
4139 if (nl)
4140 *nl = '\0';
4143 static const struct got_error *
4144 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4146 const struct got_error *err;
4147 char *logmsg0 = NULL;
4148 const char *s;
4150 err = got_object_commit_get_logmsg(&logmsg0, commit);
4151 if (err)
4152 return err;
4154 s = logmsg0;
4155 while (isspace((unsigned char)s[0]))
4156 s++;
4158 *logmsg = strdup(s);
4159 if (*logmsg == NULL) {
4160 err = got_error_from_errno("strdup");
4161 goto done;
4164 trim_logmsg(*logmsg, limit);
4165 done:
4166 free(logmsg0);
4167 return err;
4170 static const struct got_error *
4171 show_rebase_progress(struct got_commit_object *commit,
4172 struct got_object_id *old_id, struct got_object_id *new_id)
4174 const struct got_error *err;
4175 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4177 err = got_object_id_str(&old_id_str, old_id);
4178 if (err)
4179 goto done;
4181 if (new_id) {
4182 err = got_object_id_str(&new_id_str, new_id);
4183 if (err)
4184 goto done;
4187 old_id_str[12] = '\0';
4188 if (new_id_str)
4189 new_id_str[12] = '\0';
4191 err = get_short_logmsg(&logmsg, 42, commit);
4192 if (err)
4193 goto done;
4195 printf("%s -> %s: %s\n", old_id_str,
4196 new_id_str ? new_id_str : "no-op change", logmsg);
4197 done:
4198 free(old_id_str);
4199 free(new_id_str);
4200 return err;
4203 static const struct got_error *
4204 rebase_progress(void *arg, unsigned char status, const char *path)
4206 unsigned char *rebase_status = arg;
4208 while (path[0] == '/')
4209 path++;
4210 printf("%c %s\n", status, path);
4212 if (*rebase_status == GOT_STATUS_CONFLICT)
4213 return NULL;
4214 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4215 *rebase_status = status;
4216 return NULL;
4219 static const struct got_error *
4220 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4221 struct got_reference *branch, struct got_reference *new_base_branch,
4222 struct got_reference *tmp_branch, struct got_repository *repo)
4224 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4225 return got_worktree_rebase_complete(worktree, fileindex,
4226 new_base_branch, tmp_branch, branch, repo);
4229 static const struct got_error *
4230 rebase_commit(struct got_pathlist_head *merged_paths,
4231 struct got_worktree *worktree, struct got_fileindex *fileindex,
4232 struct got_reference *tmp_branch,
4233 struct got_object_id *commit_id, struct got_repository *repo)
4235 const struct got_error *error;
4236 struct got_commit_object *commit;
4237 struct got_object_id *new_commit_id;
4239 error = got_object_open_as_commit(&commit, repo, commit_id);
4240 if (error)
4241 return error;
4243 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4244 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4245 if (error) {
4246 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4247 goto done;
4248 error = show_rebase_progress(commit, commit_id, NULL);
4249 } else {
4250 error = show_rebase_progress(commit, commit_id, new_commit_id);
4251 free(new_commit_id);
4253 done:
4254 got_object_commit_close(commit);
4255 return error;
4258 struct check_path_prefix_arg {
4259 const char *path_prefix;
4260 size_t len;
4261 int errcode;
4264 static const struct got_error *
4265 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4266 struct got_blob_object *blob2, struct got_object_id *id1,
4267 struct got_object_id *id2, const char *path1, const char *path2,
4268 struct got_repository *repo)
4270 struct check_path_prefix_arg *a = arg;
4272 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4273 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4274 return got_error(a->errcode);
4276 return NULL;
4279 static const struct got_error *
4280 check_path_prefix(struct got_object_id *parent_id,
4281 struct got_object_id *commit_id, const char *path_prefix,
4282 int errcode, struct got_repository *repo)
4284 const struct got_error *err;
4285 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4286 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4287 struct check_path_prefix_arg cpp_arg;
4289 if (got_path_is_root_dir(path_prefix))
4290 return NULL;
4292 err = got_object_open_as_commit(&commit, repo, commit_id);
4293 if (err)
4294 goto done;
4296 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4297 if (err)
4298 goto done;
4300 err = got_object_open_as_tree(&tree1, repo,
4301 got_object_commit_get_tree_id(parent_commit));
4302 if (err)
4303 goto done;
4305 err = got_object_open_as_tree(&tree2, repo,
4306 got_object_commit_get_tree_id(commit));
4307 if (err)
4308 goto done;
4310 cpp_arg.path_prefix = path_prefix;
4311 while (cpp_arg.path_prefix[0] == '/')
4312 cpp_arg.path_prefix++;
4313 cpp_arg.len = strlen(cpp_arg.path_prefix);
4314 cpp_arg.errcode = errcode;
4315 err = got_diff_tree(tree1, tree2, "", "", repo,
4316 check_path_prefix_in_diff, &cpp_arg, 0);
4317 done:
4318 if (tree1)
4319 got_object_tree_close(tree1);
4320 if (tree2)
4321 got_object_tree_close(tree2);
4322 if (commit)
4323 got_object_commit_close(commit);
4324 if (parent_commit)
4325 got_object_commit_close(parent_commit);
4326 return err;
4329 static const struct got_error *
4330 collect_commits(struct got_object_id_queue *commits,
4331 struct got_object_id *initial_commit_id,
4332 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4333 const char *path_prefix, int path_prefix_errcode,
4334 struct got_repository *repo)
4336 const struct got_error *err = NULL;
4337 struct got_commit_graph *graph = NULL;
4338 struct got_object_id *parent_id = NULL;
4339 struct got_object_qid *qid;
4340 struct got_object_id *commit_id = initial_commit_id;
4342 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4343 if (err)
4344 return err;
4346 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4347 if (err)
4348 goto done;
4349 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4350 err = got_commit_graph_iter_next(&parent_id, graph);
4351 if (err) {
4352 if (err->code == GOT_ERR_ITER_COMPLETED) {
4353 err = got_error_msg(GOT_ERR_ANCESTRY,
4354 "ran out of commits to rebase before "
4355 "youngest common ancestor commit has "
4356 "been reached?!?");
4357 goto done;
4358 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4359 goto done;
4360 err = got_commit_graph_fetch_commits(graph, 1, repo);
4361 if (err)
4362 goto done;
4363 } else {
4364 err = check_path_prefix(parent_id, commit_id,
4365 path_prefix, path_prefix_errcode, repo);
4366 if (err)
4367 goto done;
4369 err = got_object_qid_alloc(&qid, commit_id);
4370 if (err)
4371 goto done;
4372 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4373 commit_id = parent_id;
4376 done:
4377 got_commit_graph_close(graph);
4378 return err;
4381 static const struct got_error *
4382 cmd_rebase(int argc, char *argv[])
4384 const struct got_error *error = NULL;
4385 struct got_worktree *worktree = NULL;
4386 struct got_repository *repo = NULL;
4387 struct got_fileindex *fileindex = NULL;
4388 char *cwd = NULL;
4389 struct got_reference *branch = NULL;
4390 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4391 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4392 struct got_object_id *resume_commit_id = NULL;
4393 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4394 struct got_commit_object *commit = NULL;
4395 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4396 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4397 struct got_object_id_queue commits;
4398 struct got_pathlist_head merged_paths;
4399 const struct got_object_id_queue *parent_ids;
4400 struct got_object_qid *qid, *pid;
4402 SIMPLEQ_INIT(&commits);
4403 TAILQ_INIT(&merged_paths);
4405 while ((ch = getopt(argc, argv, "ac")) != -1) {
4406 switch (ch) {
4407 case 'a':
4408 abort_rebase = 1;
4409 break;
4410 case 'c':
4411 continue_rebase = 1;
4412 break;
4413 default:
4414 usage_rebase();
4415 /* NOTREACHED */
4419 argc -= optind;
4420 argv += optind;
4422 #ifndef PROFILE
4423 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4424 "unveil", NULL) == -1)
4425 err(1, "pledge");
4426 #endif
4427 if (abort_rebase && continue_rebase)
4428 usage_rebase();
4429 else if (abort_rebase || continue_rebase) {
4430 if (argc != 0)
4431 usage_rebase();
4432 } else if (argc != 1)
4433 usage_rebase();
4435 cwd = getcwd(NULL, 0);
4436 if (cwd == NULL) {
4437 error = got_error_from_errno("getcwd");
4438 goto done;
4440 error = got_worktree_open(&worktree, cwd);
4441 if (error)
4442 goto done;
4444 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4445 if (error != NULL)
4446 goto done;
4448 error = apply_unveil(got_repo_get_path(repo), 0,
4449 got_worktree_get_root_path(worktree));
4450 if (error)
4451 goto done;
4453 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4454 if (error)
4455 goto done;
4457 if (abort_rebase) {
4458 int did_something;
4459 if (!rebase_in_progress) {
4460 error = got_error(GOT_ERR_NOT_REBASING);
4461 goto done;
4463 error = got_worktree_rebase_continue(&resume_commit_id,
4464 &new_base_branch, &tmp_branch, &branch, &fileindex,
4465 worktree, repo);
4466 if (error)
4467 goto done;
4468 printf("Switching work tree to %s\n",
4469 got_ref_get_symref_target(new_base_branch));
4470 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4471 new_base_branch, update_progress, &did_something);
4472 if (error)
4473 goto done;
4474 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4475 goto done; /* nothing else to do */
4478 if (continue_rebase) {
4479 if (!rebase_in_progress) {
4480 error = got_error(GOT_ERR_NOT_REBASING);
4481 goto done;
4483 error = got_worktree_rebase_continue(&resume_commit_id,
4484 &new_base_branch, &tmp_branch, &branch, &fileindex,
4485 worktree, repo);
4486 if (error)
4487 goto done;
4489 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4490 resume_commit_id, repo);
4491 if (error)
4492 goto done;
4494 yca_id = got_object_id_dup(resume_commit_id);
4495 if (yca_id == NULL) {
4496 error = got_error_from_errno("got_object_id_dup");
4497 goto done;
4499 } else {
4500 error = got_ref_open(&branch, repo, argv[0], 0);
4501 if (error != NULL)
4502 goto done;
4505 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4506 if (error)
4507 goto done;
4509 if (!continue_rebase) {
4510 struct got_object_id *base_commit_id;
4512 base_commit_id = got_worktree_get_base_commit_id(worktree);
4513 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4514 base_commit_id, branch_head_commit_id, repo);
4515 if (error)
4516 goto done;
4517 if (yca_id == NULL) {
4518 error = got_error_msg(GOT_ERR_ANCESTRY,
4519 "specified branch shares no common ancestry "
4520 "with work tree's branch");
4521 goto done;
4524 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4525 if (error) {
4526 if (error->code != GOT_ERR_ANCESTRY)
4527 goto done;
4528 error = NULL;
4529 } else {
4530 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4531 "specified branch resolves to a commit which "
4532 "is already contained in work tree's branch");
4533 goto done;
4535 error = got_worktree_rebase_prepare(&new_base_branch,
4536 &tmp_branch, &fileindex, worktree, branch, repo);
4537 if (error)
4538 goto done;
4541 commit_id = branch_head_commit_id;
4542 error = got_object_open_as_commit(&commit, repo, commit_id);
4543 if (error)
4544 goto done;
4546 parent_ids = got_object_commit_get_parent_ids(commit);
4547 pid = SIMPLEQ_FIRST(parent_ids);
4548 if (pid == NULL) {
4549 if (!continue_rebase) {
4550 int did_something;
4551 error = got_worktree_rebase_abort(worktree, fileindex,
4552 repo, new_base_branch, update_progress,
4553 &did_something);
4554 if (error)
4555 goto done;
4556 printf("Rebase of %s aborted\n",
4557 got_ref_get_name(branch));
4559 error = got_error(GOT_ERR_EMPTY_REBASE);
4560 goto done;
4562 error = collect_commits(&commits, commit_id, pid->id,
4563 yca_id, got_worktree_get_path_prefix(worktree),
4564 GOT_ERR_REBASE_PATH, repo);
4565 got_object_commit_close(commit);
4566 commit = NULL;
4567 if (error)
4568 goto done;
4570 if (SIMPLEQ_EMPTY(&commits)) {
4571 if (continue_rebase)
4572 error = rebase_complete(worktree, fileindex,
4573 branch, new_base_branch, tmp_branch, repo);
4574 else
4575 error = got_error(GOT_ERR_EMPTY_REBASE);
4576 goto done;
4579 pid = NULL;
4580 SIMPLEQ_FOREACH(qid, &commits, entry) {
4581 commit_id = qid->id;
4582 parent_id = pid ? pid->id : yca_id;
4583 pid = qid;
4585 error = got_worktree_rebase_merge_files(&merged_paths,
4586 worktree, fileindex, parent_id, commit_id, repo,
4587 rebase_progress, &rebase_status, check_cancelled, NULL);
4588 if (error)
4589 goto done;
4591 if (rebase_status == GOT_STATUS_CONFLICT) {
4592 got_worktree_rebase_pathlist_free(&merged_paths);
4593 break;
4596 error = rebase_commit(&merged_paths, worktree, fileindex,
4597 tmp_branch, commit_id, repo);
4598 got_worktree_rebase_pathlist_free(&merged_paths);
4599 if (error)
4600 goto done;
4603 if (rebase_status == GOT_STATUS_CONFLICT) {
4604 error = got_worktree_rebase_postpone(worktree, fileindex);
4605 if (error)
4606 goto done;
4607 error = got_error_msg(GOT_ERR_CONFLICTS,
4608 "conflicts must be resolved before rebasing can continue");
4609 } else
4610 error = rebase_complete(worktree, fileindex, branch,
4611 new_base_branch, tmp_branch, repo);
4612 done:
4613 got_object_id_queue_free(&commits);
4614 free(branch_head_commit_id);
4615 free(resume_commit_id);
4616 free(yca_id);
4617 if (commit)
4618 got_object_commit_close(commit);
4619 if (branch)
4620 got_ref_close(branch);
4621 if (new_base_branch)
4622 got_ref_close(new_base_branch);
4623 if (tmp_branch)
4624 got_ref_close(tmp_branch);
4625 if (worktree)
4626 got_worktree_close(worktree);
4627 if (repo)
4628 got_repo_close(repo);
4629 return error;
4632 __dead static void
4633 usage_histedit(void)
4635 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4636 getprogname());
4637 exit(1);
4640 #define GOT_HISTEDIT_PICK 'p'
4641 #define GOT_HISTEDIT_EDIT 'e'
4642 #define GOT_HISTEDIT_FOLD 'f'
4643 #define GOT_HISTEDIT_DROP 'd'
4644 #define GOT_HISTEDIT_MESG 'm'
4646 static struct got_histedit_cmd {
4647 unsigned char code;
4648 const char *name;
4649 const char *desc;
4650 } got_histedit_cmds[] = {
4651 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4652 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4653 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4654 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4655 { GOT_HISTEDIT_MESG, "mesg",
4656 "single-line log message for commit above (open editor if empty)" },
4659 struct got_histedit_list_entry {
4660 TAILQ_ENTRY(got_histedit_list_entry) entry;
4661 struct got_object_id *commit_id;
4662 const struct got_histedit_cmd *cmd;
4663 char *logmsg;
4665 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4667 static const struct got_error *
4668 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4669 FILE *f, struct got_repository *repo)
4671 const struct got_error *err = NULL;
4672 char *logmsg = NULL, *id_str = NULL;
4673 struct got_commit_object *commit = NULL;
4674 int n;
4676 err = got_object_open_as_commit(&commit, repo, commit_id);
4677 if (err)
4678 goto done;
4680 err = get_short_logmsg(&logmsg, 34, commit);
4681 if (err)
4682 goto done;
4684 err = got_object_id_str(&id_str, commit_id);
4685 if (err)
4686 goto done;
4688 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4689 if (n < 0)
4690 err = got_ferror(f, GOT_ERR_IO);
4691 done:
4692 if (commit)
4693 got_object_commit_close(commit);
4694 free(id_str);
4695 free(logmsg);
4696 return err;
4699 static const struct got_error *
4700 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4701 struct got_repository *repo)
4703 const struct got_error *err = NULL;
4704 struct got_object_qid *qid;
4706 if (SIMPLEQ_EMPTY(commits))
4707 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4709 SIMPLEQ_FOREACH(qid, commits, entry) {
4710 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4711 f, repo);
4712 if (err)
4713 break;
4716 return err;
4719 static const struct got_error *
4720 write_cmd_list(FILE *f)
4722 const struct got_error *err = NULL;
4723 int n, i;
4725 n = fprintf(f, "# Available histedit commands:\n");
4726 if (n < 0)
4727 return got_ferror(f, GOT_ERR_IO);
4729 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4730 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4731 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4732 cmd->desc);
4733 if (n < 0) {
4734 err = got_ferror(f, GOT_ERR_IO);
4735 break;
4738 n = fprintf(f, "# Commits will be processed in order from top to "
4739 "bottom of this file.\n");
4740 if (n < 0)
4741 return got_ferror(f, GOT_ERR_IO);
4742 return err;
4745 static const struct got_error *
4746 histedit_syntax_error(int lineno)
4748 static char msg[42];
4749 int ret;
4751 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4752 lineno);
4753 if (ret == -1 || ret >= sizeof(msg))
4754 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4756 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4759 static const struct got_error *
4760 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4761 char *logmsg, struct got_repository *repo)
4763 const struct got_error *err;
4764 struct got_commit_object *folded_commit = NULL;
4765 char *id_str, *folded_logmsg = NULL;
4767 err = got_object_id_str(&id_str, hle->commit_id);
4768 if (err)
4769 return err;
4771 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4772 if (err)
4773 goto done;
4775 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4776 if (err)
4777 goto done;
4778 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4779 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4780 folded_logmsg) == -1) {
4781 err = got_error_from_errno("asprintf");
4782 goto done;
4784 done:
4785 if (folded_commit)
4786 got_object_commit_close(folded_commit);
4787 free(id_str);
4788 free(folded_logmsg);
4789 return err;
4792 static struct got_histedit_list_entry *
4793 get_folded_commits(struct got_histedit_list_entry *hle)
4795 struct got_histedit_list_entry *prev, *folded = NULL;
4797 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4798 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4799 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4800 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4801 folded = prev;
4802 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4805 return folded;
4808 static const struct got_error *
4809 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4810 struct got_repository *repo)
4812 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4813 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4814 const struct got_error *err = NULL;
4815 struct got_commit_object *commit = NULL;
4816 int fd;
4817 struct got_histedit_list_entry *folded = NULL;
4819 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4820 if (err)
4821 return err;
4823 folded = get_folded_commits(hle);
4824 if (folded) {
4825 while (folded != hle) {
4826 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4827 folded = TAILQ_NEXT(folded, entry);
4828 continue;
4830 err = append_folded_commit_msg(&new_msg, folded,
4831 logmsg, repo);
4832 if (err)
4833 goto done;
4834 free(logmsg);
4835 logmsg = new_msg;
4836 folded = TAILQ_NEXT(folded, entry);
4840 err = got_object_id_str(&id_str, hle->commit_id);
4841 if (err)
4842 goto done;
4843 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4844 if (err)
4845 goto done;
4846 if (asprintf(&new_msg,
4847 "%s\n# original log message of commit %s: %s",
4848 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4849 err = got_error_from_errno("asprintf");
4850 goto done;
4852 free(logmsg);
4853 logmsg = new_msg;
4855 err = got_object_id_str(&id_str, hle->commit_id);
4856 if (err)
4857 goto done;
4859 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4860 if (err)
4861 goto done;
4863 dprintf(fd, logmsg);
4864 close(fd);
4866 err = get_editor(&editor);
4867 if (err)
4868 goto done;
4870 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4871 if (err) {
4872 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4873 goto done;
4874 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4876 done:
4877 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4878 err = got_error_from_errno2("unlink", logmsg_path);
4879 free(logmsg_path);
4880 free(logmsg);
4881 free(orig_logmsg);
4882 free(editor);
4883 if (commit)
4884 got_object_commit_close(commit);
4885 return err;
4888 static const struct got_error *
4889 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4890 FILE *f, struct got_repository *repo)
4892 const struct got_error *err = NULL;
4893 char *line = NULL, *p, *end;
4894 size_t size;
4895 ssize_t len;
4896 int lineno = 0, i;
4897 const struct got_histedit_cmd *cmd;
4898 struct got_object_id *commit_id = NULL;
4899 struct got_histedit_list_entry *hle = NULL;
4901 for (;;) {
4902 len = getline(&line, &size, f);
4903 if (len == -1) {
4904 const struct got_error *getline_err;
4905 if (feof(f))
4906 break;
4907 getline_err = got_error_from_errno("getline");
4908 err = got_ferror(f, getline_err->code);
4909 break;
4911 lineno++;
4912 p = line;
4913 while (isspace((unsigned char)p[0]))
4914 p++;
4915 if (p[0] == '#' || p[0] == '\0') {
4916 free(line);
4917 line = NULL;
4918 continue;
4920 cmd = NULL;
4921 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4922 cmd = &got_histedit_cmds[i];
4923 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4924 isspace((unsigned char)p[strlen(cmd->name)])) {
4925 p += strlen(cmd->name);
4926 break;
4928 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4929 p++;
4930 break;
4933 if (i == nitems(got_histedit_cmds)) {
4934 err = histedit_syntax_error(lineno);
4935 break;
4937 while (isspace((unsigned char)p[0]))
4938 p++;
4939 if (cmd->code == GOT_HISTEDIT_MESG) {
4940 if (hle == NULL || hle->logmsg != NULL) {
4941 err = got_error(GOT_ERR_HISTEDIT_CMD);
4942 break;
4944 if (p[0] == '\0') {
4945 err = histedit_edit_logmsg(hle, repo);
4946 if (err)
4947 break;
4948 } else {
4949 hle->logmsg = strdup(p);
4950 if (hle->logmsg == NULL) {
4951 err = got_error_from_errno("strdup");
4952 break;
4955 free(line);
4956 line = NULL;
4957 continue;
4958 } else {
4959 end = p;
4960 while (end[0] && !isspace((unsigned char)end[0]))
4961 end++;
4962 *end = '\0';
4964 err = got_object_resolve_id_str(&commit_id, repo, p);
4965 if (err) {
4966 /* override error code */
4967 err = histedit_syntax_error(lineno);
4968 break;
4971 hle = malloc(sizeof(*hle));
4972 if (hle == NULL) {
4973 err = got_error_from_errno("malloc");
4974 break;
4976 hle->cmd = cmd;
4977 hle->commit_id = commit_id;
4978 hle->logmsg = NULL;
4979 commit_id = NULL;
4980 free(line);
4981 line = NULL;
4982 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4985 free(line);
4986 free(commit_id);
4987 return err;
4990 static const struct got_error *
4991 histedit_check_script(struct got_histedit_list *histedit_cmds,
4992 struct got_object_id_queue *commits, struct got_repository *repo)
4994 const struct got_error *err = NULL;
4995 struct got_object_qid *qid;
4996 struct got_histedit_list_entry *hle;
4997 static char msg[80];
4998 char *id_str;
5000 if (TAILQ_EMPTY(histedit_cmds))
5001 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5002 "histedit script contains no commands");
5003 if (SIMPLEQ_EMPTY(commits))
5004 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5006 SIMPLEQ_FOREACH(qid, commits, entry) {
5007 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5008 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5009 break;
5011 if (hle == NULL) {
5012 err = got_object_id_str(&id_str, qid->id);
5013 if (err)
5014 return err;
5015 snprintf(msg, sizeof(msg),
5016 "commit %s missing from histedit script", id_str);
5017 free(id_str);
5018 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5022 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5023 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5024 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5025 "last commit in histedit script cannot be folded");
5027 return NULL;
5030 static const struct got_error *
5031 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5032 const char *path, struct got_object_id_queue *commits,
5033 struct got_repository *repo)
5035 const struct got_error *err = NULL;
5036 char *editor;
5037 FILE *f = NULL;
5039 err = get_editor(&editor);
5040 if (err)
5041 return err;
5043 if (spawn_editor(editor, path) == -1) {
5044 err = got_error_from_errno("failed spawning editor");
5045 goto done;
5048 f = fopen(path, "r");
5049 if (f == NULL) {
5050 err = got_error_from_errno("fopen");
5051 goto done;
5053 err = histedit_parse_list(histedit_cmds, f, repo);
5054 if (err)
5055 goto done;
5057 err = histedit_check_script(histedit_cmds, commits, repo);
5058 done:
5059 if (f && fclose(f) != 0 && err == NULL)
5060 err = got_error_from_errno("fclose");
5061 free(editor);
5062 return err;
5065 static const struct got_error *
5066 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5067 struct got_object_id_queue *, const char *, struct got_repository *);
5069 static const struct got_error *
5070 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5071 struct got_object_id_queue *commits, struct got_repository *repo)
5073 const struct got_error *err;
5074 FILE *f = NULL;
5075 char *path = NULL;
5077 err = got_opentemp_named(&path, &f, "got-histedit");
5078 if (err)
5079 return err;
5081 err = write_cmd_list(f);
5082 if (err)
5083 goto done;
5085 err = histedit_write_commit_list(commits, f, repo);
5086 if (err)
5087 goto done;
5089 if (fclose(f) != 0) {
5090 err = got_error_from_errno("fclose");
5091 goto done;
5093 f = NULL;
5095 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5096 if (err) {
5097 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5098 err->code != GOT_ERR_HISTEDIT_CMD)
5099 goto done;
5100 err = histedit_edit_list_retry(histedit_cmds, err,
5101 commits, path, repo);
5103 done:
5104 if (f && fclose(f) != 0 && err == NULL)
5105 err = got_error_from_errno("fclose");
5106 if (path && unlink(path) != 0 && err == NULL)
5107 err = got_error_from_errno2("unlink", path);
5108 free(path);
5109 return err;
5112 static const struct got_error *
5113 histedit_save_list(struct got_histedit_list *histedit_cmds,
5114 struct got_worktree *worktree, struct got_repository *repo)
5116 const struct got_error *err = NULL;
5117 char *path = NULL;
5118 FILE *f = NULL;
5119 struct got_histedit_list_entry *hle;
5120 struct got_commit_object *commit = NULL;
5122 err = got_worktree_get_histedit_script_path(&path, worktree);
5123 if (err)
5124 return err;
5126 f = fopen(path, "w");
5127 if (f == NULL) {
5128 err = got_error_from_errno2("fopen", path);
5129 goto done;
5131 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5132 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5133 repo);
5134 if (err)
5135 break;
5137 if (hle->logmsg) {
5138 int n = fprintf(f, "%c %s\n",
5139 GOT_HISTEDIT_MESG, hle->logmsg);
5140 if (n < 0) {
5141 err = got_ferror(f, GOT_ERR_IO);
5142 break;
5146 done:
5147 if (f && fclose(f) != 0 && err == NULL)
5148 err = got_error_from_errno("fclose");
5149 free(path);
5150 if (commit)
5151 got_object_commit_close(commit);
5152 return err;
5155 void
5156 histedit_free_list(struct got_histedit_list *histedit_cmds)
5158 struct got_histedit_list_entry *hle;
5160 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5161 TAILQ_REMOVE(histedit_cmds, hle, entry);
5162 free(hle);
5166 static const struct got_error *
5167 histedit_load_list(struct got_histedit_list *histedit_cmds,
5168 const char *path, struct got_repository *repo)
5170 const struct got_error *err = NULL;
5171 FILE *f = NULL;
5173 f = fopen(path, "r");
5174 if (f == NULL) {
5175 err = got_error_from_errno2("fopen", path);
5176 goto done;
5179 err = histedit_parse_list(histedit_cmds, f, repo);
5180 done:
5181 if (f && fclose(f) != 0 && err == NULL)
5182 err = got_error_from_errno("fclose");
5183 return err;
5186 static const struct got_error *
5187 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5188 const struct got_error *edit_err, struct got_object_id_queue *commits,
5189 const char *path, struct got_repository *repo)
5191 const struct got_error *err = NULL, *prev_err = edit_err;
5192 int resp = ' ';
5194 while (resp != 'c' && resp != 'r' && resp != 'a') {
5195 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5196 "or (a)bort: ", getprogname(), prev_err->msg);
5197 resp = getchar();
5198 if (resp == '\n')
5199 resp = getchar();
5200 if (resp == 'c') {
5201 histedit_free_list(histedit_cmds);
5202 err = histedit_run_editor(histedit_cmds, path, commits,
5203 repo);
5204 if (err) {
5205 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5206 err->code != GOT_ERR_HISTEDIT_CMD)
5207 break;
5208 prev_err = err;
5209 resp = ' ';
5210 continue;
5212 break;
5213 } else if (resp == 'r') {
5214 histedit_free_list(histedit_cmds);
5215 err = histedit_edit_script(histedit_cmds,
5216 commits, repo);
5217 if (err) {
5218 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5219 err->code != GOT_ERR_HISTEDIT_CMD)
5220 break;
5221 prev_err = err;
5222 resp = ' ';
5223 continue;
5225 break;
5226 } else if (resp == 'a') {
5227 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5228 break;
5229 } else
5230 printf("invalid response '%c'\n", resp);
5233 return err;
5236 static const struct got_error *
5237 histedit_complete(struct got_worktree *worktree,
5238 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5239 struct got_reference *branch, struct got_repository *repo)
5241 printf("Switching work tree to %s\n",
5242 got_ref_get_symref_target(branch));
5243 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5244 branch, repo);
5247 static const struct got_error *
5248 show_histedit_progress(struct got_commit_object *commit,
5249 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5251 const struct got_error *err;
5252 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5254 err = got_object_id_str(&old_id_str, hle->commit_id);
5255 if (err)
5256 goto done;
5258 if (new_id) {
5259 err = got_object_id_str(&new_id_str, new_id);
5260 if (err)
5261 goto done;
5264 old_id_str[12] = '\0';
5265 if (new_id_str)
5266 new_id_str[12] = '\0';
5268 if (hle->logmsg) {
5269 logmsg = strdup(hle->logmsg);
5270 if (logmsg == NULL) {
5271 err = got_error_from_errno("strdup");
5272 goto done;
5274 trim_logmsg(logmsg, 42);
5275 } else {
5276 err = get_short_logmsg(&logmsg, 42, commit);
5277 if (err)
5278 goto done;
5281 switch (hle->cmd->code) {
5282 case GOT_HISTEDIT_PICK:
5283 case GOT_HISTEDIT_EDIT:
5284 printf("%s -> %s: %s\n", old_id_str,
5285 new_id_str ? new_id_str : "no-op change", logmsg);
5286 break;
5287 case GOT_HISTEDIT_DROP:
5288 case GOT_HISTEDIT_FOLD:
5289 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5290 logmsg);
5291 break;
5292 default:
5293 break;
5296 done:
5297 free(old_id_str);
5298 free(new_id_str);
5299 return err;
5302 static const struct got_error *
5303 histedit_commit(struct got_pathlist_head *merged_paths,
5304 struct got_worktree *worktree, struct got_fileindex *fileindex,
5305 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5306 struct got_repository *repo)
5308 const struct got_error *err;
5309 struct got_commit_object *commit;
5310 struct got_object_id *new_commit_id;
5312 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5313 && hle->logmsg == NULL) {
5314 err = histedit_edit_logmsg(hle, repo);
5315 if (err)
5316 return err;
5319 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5320 if (err)
5321 return err;
5323 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5324 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5325 hle->logmsg, repo);
5326 if (err) {
5327 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5328 goto done;
5329 err = show_histedit_progress(commit, hle, NULL);
5330 } else {
5331 err = show_histedit_progress(commit, hle, new_commit_id);
5332 free(new_commit_id);
5334 done:
5335 got_object_commit_close(commit);
5336 return err;
5339 static const struct got_error *
5340 histedit_skip_commit(struct got_histedit_list_entry *hle,
5341 struct got_worktree *worktree, struct got_repository *repo)
5343 const struct got_error *error;
5344 struct got_commit_object *commit;
5346 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5347 repo);
5348 if (error)
5349 return error;
5351 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5352 if (error)
5353 return error;
5355 error = show_histedit_progress(commit, hle, NULL);
5356 got_object_commit_close(commit);
5357 return error;
5360 static const struct got_error *
5361 cmd_histedit(int argc, char *argv[])
5363 const struct got_error *error = NULL;
5364 struct got_worktree *worktree = NULL;
5365 struct got_fileindex *fileindex = NULL;
5366 struct got_repository *repo = NULL;
5367 char *cwd = NULL;
5368 struct got_reference *branch = NULL;
5369 struct got_reference *tmp_branch = NULL;
5370 struct got_object_id *resume_commit_id = NULL;
5371 struct got_object_id *base_commit_id = NULL;
5372 struct got_object_id *head_commit_id = NULL;
5373 struct got_commit_object *commit = NULL;
5374 int ch, rebase_in_progress = 0, did_something;
5375 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5376 const char *edit_script_path = NULL;
5377 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5378 struct got_object_id_queue commits;
5379 struct got_pathlist_head merged_paths;
5380 const struct got_object_id_queue *parent_ids;
5381 struct got_object_qid *pid;
5382 struct got_histedit_list histedit_cmds;
5383 struct got_histedit_list_entry *hle;
5385 SIMPLEQ_INIT(&commits);
5386 TAILQ_INIT(&histedit_cmds);
5387 TAILQ_INIT(&merged_paths);
5389 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5390 switch (ch) {
5391 case 'a':
5392 abort_edit = 1;
5393 break;
5394 case 'c':
5395 continue_edit = 1;
5396 break;
5397 case 'F':
5398 edit_script_path = optarg;
5399 break;
5400 default:
5401 usage_histedit();
5402 /* NOTREACHED */
5406 argc -= optind;
5407 argv += optind;
5409 #ifndef PROFILE
5410 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5411 "unveil", NULL) == -1)
5412 err(1, "pledge");
5413 #endif
5414 if (abort_edit && continue_edit)
5415 usage_histedit();
5416 if (argc != 0)
5417 usage_histedit();
5420 * This command cannot apply unveil(2) in all cases because the
5421 * user may choose to run an editor to edit the histedit script
5422 * and to edit individual commit log messages.
5423 * unveil(2) traverses exec(2); if an editor is used we have to
5424 * apply unveil after edit script and log messages have been written.
5425 * XXX TODO: Make use of unveil(2) where possible.
5428 cwd = getcwd(NULL, 0);
5429 if (cwd == NULL) {
5430 error = got_error_from_errno("getcwd");
5431 goto done;
5433 error = got_worktree_open(&worktree, cwd);
5434 if (error)
5435 goto done;
5437 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5438 if (error != NULL)
5439 goto done;
5441 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5442 if (error)
5443 goto done;
5444 if (rebase_in_progress) {
5445 error = got_error(GOT_ERR_REBASING);
5446 goto done;
5449 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5450 if (error)
5451 goto done;
5453 if (edit_in_progress && abort_edit) {
5454 error = got_worktree_histedit_continue(&resume_commit_id,
5455 &tmp_branch, &branch, &base_commit_id, &fileindex,
5456 worktree, repo);
5457 if (error)
5458 goto done;
5459 printf("Switching work tree to %s\n",
5460 got_ref_get_symref_target(branch));
5461 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5462 branch, base_commit_id, update_progress, &did_something);
5463 if (error)
5464 goto done;
5465 printf("Histedit of %s aborted\n",
5466 got_ref_get_symref_target(branch));
5467 goto done; /* nothing else to do */
5468 } else if (abort_edit) {
5469 error = got_error(GOT_ERR_NOT_HISTEDIT);
5470 goto done;
5473 if (continue_edit) {
5474 char *path;
5476 if (!edit_in_progress) {
5477 error = got_error(GOT_ERR_NOT_HISTEDIT);
5478 goto done;
5481 error = got_worktree_get_histedit_script_path(&path, worktree);
5482 if (error)
5483 goto done;
5485 error = histedit_load_list(&histedit_cmds, path, repo);
5486 free(path);
5487 if (error)
5488 goto done;
5490 error = got_worktree_histedit_continue(&resume_commit_id,
5491 &tmp_branch, &branch, &base_commit_id, &fileindex,
5492 worktree, repo);
5493 if (error)
5494 goto done;
5496 error = got_ref_resolve(&head_commit_id, repo, branch);
5497 if (error)
5498 goto done;
5500 error = got_object_open_as_commit(&commit, repo,
5501 head_commit_id);
5502 if (error)
5503 goto done;
5504 parent_ids = got_object_commit_get_parent_ids(commit);
5505 pid = SIMPLEQ_FIRST(parent_ids);
5506 if (pid == NULL) {
5507 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5508 goto done;
5510 error = collect_commits(&commits, head_commit_id, pid->id,
5511 base_commit_id, got_worktree_get_path_prefix(worktree),
5512 GOT_ERR_HISTEDIT_PATH, repo);
5513 got_object_commit_close(commit);
5514 commit = NULL;
5515 if (error)
5516 goto done;
5517 } else {
5518 if (edit_in_progress) {
5519 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5520 goto done;
5523 error = got_ref_open(&branch, repo,
5524 got_worktree_get_head_ref_name(worktree), 0);
5525 if (error != NULL)
5526 goto done;
5528 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5529 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5530 "will not edit commit history of a branch outside "
5531 "the \"refs/heads/\" reference namespace");
5532 goto done;
5535 error = got_ref_resolve(&head_commit_id, repo, branch);
5536 got_ref_close(branch);
5537 branch = NULL;
5538 if (error)
5539 goto done;
5541 error = got_object_open_as_commit(&commit, repo,
5542 head_commit_id);
5543 if (error)
5544 goto done;
5545 parent_ids = got_object_commit_get_parent_ids(commit);
5546 pid = SIMPLEQ_FIRST(parent_ids);
5547 if (pid == NULL) {
5548 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5549 goto done;
5551 error = collect_commits(&commits, head_commit_id, pid->id,
5552 got_worktree_get_base_commit_id(worktree),
5553 got_worktree_get_path_prefix(worktree),
5554 GOT_ERR_HISTEDIT_PATH, repo);
5555 got_object_commit_close(commit);
5556 commit = NULL;
5557 if (error)
5558 goto done;
5560 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5561 &base_commit_id, &fileindex, worktree, repo);
5562 if (error)
5563 goto done;
5565 if (edit_script_path) {
5566 error = histedit_load_list(&histedit_cmds,
5567 edit_script_path, repo);
5568 if (error) {
5569 got_worktree_histedit_abort(worktree, fileindex,
5570 repo, branch, base_commit_id,
5571 update_progress, &did_something);
5572 goto done;
5574 } else {
5575 error = histedit_edit_script(&histedit_cmds, &commits,
5576 repo);
5577 if (error) {
5578 got_worktree_histedit_abort(worktree, fileindex,
5579 repo, branch, base_commit_id,
5580 update_progress, &did_something);
5581 goto done;
5586 error = histedit_save_list(&histedit_cmds, worktree,
5587 repo);
5588 if (error) {
5589 got_worktree_histedit_abort(worktree, fileindex,
5590 repo, branch, base_commit_id,
5591 update_progress, &did_something);
5592 goto done;
5597 error = histedit_check_script(&histedit_cmds, &commits, repo);
5598 if (error)
5599 goto done;
5601 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5602 if (resume_commit_id) {
5603 if (got_object_id_cmp(hle->commit_id,
5604 resume_commit_id) != 0)
5605 continue;
5607 resume_commit_id = NULL;
5608 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5609 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5610 error = histedit_skip_commit(hle, worktree,
5611 repo);
5612 } else {
5613 error = histedit_commit(NULL, worktree,
5614 fileindex, tmp_branch, hle, repo);
5616 if (error)
5617 goto done;
5618 continue;
5621 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5622 error = histedit_skip_commit(hle, worktree, repo);
5623 if (error)
5624 goto done;
5625 continue;
5628 error = got_object_open_as_commit(&commit, repo,
5629 hle->commit_id);
5630 if (error)
5631 goto done;
5632 parent_ids = got_object_commit_get_parent_ids(commit);
5633 pid = SIMPLEQ_FIRST(parent_ids);
5635 error = got_worktree_histedit_merge_files(&merged_paths,
5636 worktree, fileindex, pid->id, hle->commit_id, repo,
5637 rebase_progress, &rebase_status, check_cancelled, NULL);
5638 if (error)
5639 goto done;
5640 got_object_commit_close(commit);
5641 commit = NULL;
5643 if (rebase_status == GOT_STATUS_CONFLICT) {
5644 got_worktree_rebase_pathlist_free(&merged_paths);
5645 break;
5648 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5649 char *id_str;
5650 error = got_object_id_str(&id_str, hle->commit_id);
5651 if (error)
5652 goto done;
5653 printf("Stopping histedit for amending commit %s\n",
5654 id_str);
5655 free(id_str);
5656 got_worktree_rebase_pathlist_free(&merged_paths);
5657 error = got_worktree_histedit_postpone(worktree,
5658 fileindex);
5659 goto done;
5662 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5663 error = histedit_skip_commit(hle, worktree, repo);
5664 if (error)
5665 goto done;
5666 continue;
5669 error = histedit_commit(&merged_paths, worktree, fileindex,
5670 tmp_branch, hle, repo);
5671 got_worktree_rebase_pathlist_free(&merged_paths);
5672 if (error)
5673 goto done;
5676 if (rebase_status == GOT_STATUS_CONFLICT) {
5677 error = got_worktree_histedit_postpone(worktree, fileindex);
5678 if (error)
5679 goto done;
5680 error = got_error_msg(GOT_ERR_CONFLICTS,
5681 "conflicts must be resolved before rebasing can continue");
5682 } else
5683 error = histedit_complete(worktree, fileindex, tmp_branch,
5684 branch, repo);
5685 done:
5686 got_object_id_queue_free(&commits);
5687 histedit_free_list(&histedit_cmds);
5688 free(head_commit_id);
5689 free(base_commit_id);
5690 free(resume_commit_id);
5691 if (commit)
5692 got_object_commit_close(commit);
5693 if (branch)
5694 got_ref_close(branch);
5695 if (tmp_branch)
5696 got_ref_close(tmp_branch);
5697 if (worktree)
5698 got_worktree_close(worktree);
5699 if (repo)
5700 got_repo_close(repo);
5701 return error;
5704 __dead static void
5705 usage_stage(void)
5707 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5708 "[file-path ...]\n",
5709 getprogname());
5710 exit(1);
5713 static const struct got_error *
5714 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5715 const char *path, struct got_object_id *blob_id,
5716 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5718 const struct got_error *err = NULL;
5719 char *id_str = NULL;
5721 if (staged_status != GOT_STATUS_ADD &&
5722 staged_status != GOT_STATUS_MODIFY &&
5723 staged_status != GOT_STATUS_DELETE)
5724 return NULL;
5726 if (staged_status == GOT_STATUS_ADD ||
5727 staged_status == GOT_STATUS_MODIFY)
5728 err = got_object_id_str(&id_str, staged_blob_id);
5729 else
5730 err = got_object_id_str(&id_str, blob_id);
5731 if (err)
5732 return err;
5734 printf("%s %c %s\n", id_str, staged_status, path);
5735 free(id_str);
5736 return NULL;
5739 static const struct got_error *
5740 cmd_stage(int argc, char *argv[])
5742 const struct got_error *error = NULL;
5743 struct got_repository *repo = NULL;
5744 struct got_worktree *worktree = NULL;
5745 char *cwd = NULL;
5746 struct got_pathlist_head paths;
5747 struct got_pathlist_entry *pe;
5748 int ch, list_stage = 0, pflag = 0;
5749 FILE *patch_script_file = NULL;
5750 const char *patch_script_path = NULL;
5751 struct choose_patch_arg cpa;
5753 TAILQ_INIT(&paths);
5755 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5756 switch (ch) {
5757 case 'l':
5758 list_stage = 1;
5759 break;
5760 case 'p':
5761 pflag = 1;
5762 break;
5763 case 'F':
5764 patch_script_path = optarg;
5765 break;
5766 default:
5767 usage_stage();
5768 /* NOTREACHED */
5772 argc -= optind;
5773 argv += optind;
5775 #ifndef PROFILE
5776 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5777 "unveil", NULL) == -1)
5778 err(1, "pledge");
5779 #endif
5780 if (list_stage && (pflag || patch_script_path))
5781 errx(1, "-l option cannot be used with other options");
5782 if (patch_script_path && !pflag)
5783 errx(1, "-F option can only be used together with -p option");
5785 cwd = getcwd(NULL, 0);
5786 if (cwd == NULL) {
5787 error = got_error_from_errno("getcwd");
5788 goto done;
5791 error = got_worktree_open(&worktree, cwd);
5792 if (error)
5793 goto done;
5795 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5796 if (error != NULL)
5797 goto done;
5799 if (patch_script_path) {
5800 patch_script_file = fopen(patch_script_path, "r");
5801 if (patch_script_file == NULL) {
5802 error = got_error_from_errno2("fopen",
5803 patch_script_path);
5804 goto done;
5807 error = apply_unveil(got_repo_get_path(repo), 1,
5808 got_worktree_get_root_path(worktree));
5809 if (error)
5810 goto done;
5812 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5813 if (error)
5814 goto done;
5816 if (list_stage)
5817 error = got_worktree_status(worktree, &paths, repo,
5818 print_stage, NULL, check_cancelled, NULL);
5819 else {
5820 cpa.patch_script_file = patch_script_file;
5821 cpa.action = "stage";
5822 error = got_worktree_stage(worktree, &paths,
5823 pflag ? NULL : print_status, NULL,
5824 pflag ? choose_patch : NULL, &cpa, repo);
5826 done:
5827 if (patch_script_file && fclose(patch_script_file) == EOF &&
5828 error == NULL)
5829 error = got_error_from_errno2("fclose", patch_script_path);
5830 if (repo)
5831 got_repo_close(repo);
5832 if (worktree)
5833 got_worktree_close(worktree);
5834 TAILQ_FOREACH(pe, &paths, entry)
5835 free((char *)pe->path);
5836 got_pathlist_free(&paths);
5837 free(cwd);
5838 return error;
5841 __dead static void
5842 usage_unstage(void)
5844 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5845 "[file-path ...]\n",
5846 getprogname());
5847 exit(1);
5851 static const struct got_error *
5852 cmd_unstage(int argc, char *argv[])
5854 const struct got_error *error = NULL;
5855 struct got_repository *repo = NULL;
5856 struct got_worktree *worktree = NULL;
5857 char *cwd = NULL;
5858 struct got_pathlist_head paths;
5859 struct got_pathlist_entry *pe;
5860 int ch, did_something = 0, pflag = 0;
5861 FILE *patch_script_file = NULL;
5862 const char *patch_script_path = NULL;
5863 struct choose_patch_arg cpa;
5865 TAILQ_INIT(&paths);
5867 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5868 switch (ch) {
5869 case 'p':
5870 pflag = 1;
5871 break;
5872 case 'F':
5873 patch_script_path = optarg;
5874 break;
5875 default:
5876 usage_unstage();
5877 /* NOTREACHED */
5881 argc -= optind;
5882 argv += optind;
5884 #ifndef PROFILE
5885 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5886 "unveil", NULL) == -1)
5887 err(1, "pledge");
5888 #endif
5889 if (patch_script_path && !pflag)
5890 errx(1, "-F option can only be used together with -p option");
5892 cwd = getcwd(NULL, 0);
5893 if (cwd == NULL) {
5894 error = got_error_from_errno("getcwd");
5895 goto done;
5898 error = got_worktree_open(&worktree, cwd);
5899 if (error)
5900 goto done;
5902 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5903 if (error != NULL)
5904 goto done;
5906 if (patch_script_path) {
5907 patch_script_file = fopen(patch_script_path, "r");
5908 if (patch_script_file == NULL) {
5909 error = got_error_from_errno2("fopen",
5910 patch_script_path);
5911 goto done;
5915 error = apply_unveil(got_repo_get_path(repo), 1,
5916 got_worktree_get_root_path(worktree));
5917 if (error)
5918 goto done;
5920 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5921 if (error)
5922 goto done;
5924 cpa.patch_script_file = patch_script_file;
5925 cpa.action = "unstage";
5926 error = got_worktree_unstage(worktree, &paths, update_progress,
5927 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5928 done:
5929 if (patch_script_file && fclose(patch_script_file) == EOF &&
5930 error == NULL)
5931 error = got_error_from_errno2("fclose", patch_script_path);
5932 if (repo)
5933 got_repo_close(repo);
5934 if (worktree)
5935 got_worktree_close(worktree);
5936 TAILQ_FOREACH(pe, &paths, entry)
5937 free((char *)pe->path);
5938 got_pathlist_free(&paths);
5939 free(cwd);
5940 return error;