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 memset(&bca, 0, sizeof(bca));
2292 #ifndef PROFILE
2293 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2294 NULL) == -1)
2295 err(1, "pledge");
2296 #endif
2298 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2299 switch (ch) {
2300 case 'c':
2301 commit_id_str = optarg;
2302 break;
2303 case 'r':
2304 repo_path = realpath(optarg, NULL);
2305 if (repo_path == NULL)
2306 err(1, "-r option");
2307 got_path_strip_trailing_slashes(repo_path);
2308 break;
2309 default:
2310 usage_blame();
2311 /* NOTREACHED */
2315 argc -= optind;
2316 argv += optind;
2318 if (argc == 1)
2319 path = argv[0];
2320 else
2321 usage_blame();
2323 cwd = getcwd(NULL, 0);
2324 if (cwd == NULL) {
2325 error = got_error_from_errno("getcwd");
2326 goto done;
2328 if (repo_path == NULL) {
2329 error = got_worktree_open(&worktree, cwd);
2330 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2331 goto done;
2332 else
2333 error = NULL;
2334 if (worktree) {
2335 repo_path =
2336 strdup(got_worktree_get_repo_path(worktree));
2337 if (repo_path == NULL)
2338 error = got_error_from_errno("strdup");
2339 if (error)
2340 goto done;
2341 } else {
2342 repo_path = strdup(cwd);
2343 if (repo_path == NULL) {
2344 error = got_error_from_errno("strdup");
2345 goto done;
2350 error = got_repo_open(&repo, repo_path);
2351 if (error != NULL)
2352 goto done;
2354 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2355 if (error)
2356 goto done;
2358 if (worktree) {
2359 const char *prefix = got_worktree_get_path_prefix(worktree);
2360 char *p, *worktree_subdir = cwd +
2361 strlen(got_worktree_get_root_path(worktree));
2362 if (asprintf(&p, "%s%s%s%s%s",
2363 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2364 worktree_subdir, worktree_subdir[0] ? "/" : "",
2365 path) == -1) {
2366 error = got_error_from_errno("asprintf");
2367 goto done;
2369 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2370 free(p);
2371 } else {
2372 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2374 if (error)
2375 goto done;
2377 if (commit_id_str == NULL) {
2378 struct got_reference *head_ref;
2379 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2380 if (error != NULL)
2381 goto done;
2382 error = got_ref_resolve(&commit_id, repo, head_ref);
2383 got_ref_close(head_ref);
2384 if (error != NULL)
2385 goto done;
2386 } else {
2387 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2388 if (error)
2389 goto done;
2392 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2393 if (error)
2394 goto done;
2395 if (obj_id == NULL) {
2396 error = got_error(GOT_ERR_NO_OBJ);
2397 goto done;
2400 error = got_object_get_type(&obj_type, repo, obj_id);
2401 if (error)
2402 goto done;
2404 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2405 error = got_error(GOT_ERR_OBJ_TYPE);
2406 goto done;
2409 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2410 if (error)
2411 goto done;
2412 bca.f = got_opentemp();
2413 if (bca.f == NULL) {
2414 error = got_error_from_errno("got_opentemp");
2415 goto done;
2417 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2418 &bca.line_offsets, bca.f, blob);
2419 if (error || bca.nlines == 0)
2420 goto done;
2422 /* Don't include \n at EOF in the blame line count. */
2423 if (bca.line_offsets[bca.nlines - 1] == filesize)
2424 bca.nlines--;
2426 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2427 if (bca.lines == NULL) {
2428 error = got_error_from_errno("calloc");
2429 goto done;
2431 bca.lineno_cur = 1;
2432 bca.nlines_prec = 0;
2433 i = bca.nlines;
2434 while (i > 0) {
2435 i /= 10;
2436 bca.nlines_prec++;
2438 bca.repo = repo;
2440 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2441 if (error)
2442 goto done;
2443 done:
2444 free(in_repo_path);
2445 free(repo_path);
2446 free(cwd);
2447 free(commit_id);
2448 free(obj_id);
2449 if (blob)
2450 got_object_blob_close(blob);
2451 if (worktree)
2452 got_worktree_close(worktree);
2453 if (repo) {
2454 const struct got_error *repo_error;
2455 repo_error = got_repo_close(repo);
2456 if (error == NULL)
2457 error = repo_error;
2459 for (i = 0; i < bca.nlines; i++) {
2460 struct blame_line *bline = &bca.lines[i];
2461 free(bline->id_str);
2462 free(bline->committer);
2464 free(bca.lines);
2465 free(bca.line_offsets);
2466 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2467 error = got_error_from_errno("fclose");
2468 return error;
2471 __dead static void
2472 usage_tree(void)
2474 fprintf(stderr,
2475 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2476 getprogname());
2477 exit(1);
2480 static void
2481 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2482 const char *root_path)
2484 int is_root_path = (strcmp(path, root_path) == 0);
2485 const char *modestr = "";
2487 path += strlen(root_path);
2488 while (path[0] == '/')
2489 path++;
2491 if (S_ISLNK(te->mode))
2492 modestr = "@";
2493 else if (S_ISDIR(te->mode))
2494 modestr = "/";
2495 else if (te->mode & S_IXUSR)
2496 modestr = "*";
2498 printf("%s%s%s%s%s\n", id ? id : "", path,
2499 is_root_path ? "" : "/", te->name, modestr);
2502 static const struct got_error *
2503 print_tree(const char *path, struct got_object_id *commit_id,
2504 int show_ids, int recurse, const char *root_path,
2505 struct got_repository *repo)
2507 const struct got_error *err = NULL;
2508 struct got_object_id *tree_id = NULL;
2509 struct got_tree_object *tree = NULL;
2510 const struct got_tree_entries *entries;
2511 struct got_tree_entry *te;
2513 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2514 if (err)
2515 goto done;
2517 err = got_object_open_as_tree(&tree, repo, tree_id);
2518 if (err)
2519 goto done;
2520 entries = got_object_tree_get_entries(tree);
2521 te = SIMPLEQ_FIRST(&entries->head);
2522 while (te) {
2523 char *id = NULL;
2525 if (sigint_received || sigpipe_received)
2526 break;
2528 if (show_ids) {
2529 char *id_str;
2530 err = got_object_id_str(&id_str, te->id);
2531 if (err)
2532 goto done;
2533 if (asprintf(&id, "%s ", id_str) == -1) {
2534 err = got_error_from_errno("asprintf");
2535 free(id_str);
2536 goto done;
2538 free(id_str);
2540 print_entry(te, id, path, root_path);
2541 free(id);
2543 if (recurse && S_ISDIR(te->mode)) {
2544 char *child_path;
2545 if (asprintf(&child_path, "%s%s%s", path,
2546 path[0] == '/' && path[1] == '\0' ? "" : "/",
2547 te->name) == -1) {
2548 err = got_error_from_errno("asprintf");
2549 goto done;
2551 err = print_tree(child_path, commit_id, show_ids, 1,
2552 root_path, repo);
2553 free(child_path);
2554 if (err)
2555 goto done;
2558 te = SIMPLEQ_NEXT(te, entry);
2560 done:
2561 if (tree)
2562 got_object_tree_close(tree);
2563 free(tree_id);
2564 return err;
2567 static const struct got_error *
2568 cmd_tree(int argc, char *argv[])
2570 const struct got_error *error;
2571 struct got_repository *repo = NULL;
2572 struct got_worktree *worktree = NULL;
2573 const char *path;
2574 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2575 struct got_object_id *commit_id = NULL;
2576 char *commit_id_str = NULL;
2577 int show_ids = 0, recurse = 0;
2578 int ch;
2580 #ifndef PROFILE
2581 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2582 NULL) == -1)
2583 err(1, "pledge");
2584 #endif
2586 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2587 switch (ch) {
2588 case 'c':
2589 commit_id_str = optarg;
2590 break;
2591 case 'r':
2592 repo_path = realpath(optarg, NULL);
2593 if (repo_path == NULL)
2594 err(1, "-r option");
2595 got_path_strip_trailing_slashes(repo_path);
2596 break;
2597 case 'i':
2598 show_ids = 1;
2599 break;
2600 case 'R':
2601 recurse = 1;
2602 break;
2603 default:
2604 usage_tree();
2605 /* NOTREACHED */
2609 argc -= optind;
2610 argv += optind;
2612 if (argc == 1)
2613 path = argv[0];
2614 else if (argc > 1)
2615 usage_tree();
2616 else
2617 path = NULL;
2619 cwd = getcwd(NULL, 0);
2620 if (cwd == NULL) {
2621 error = got_error_from_errno("getcwd");
2622 goto done;
2624 if (repo_path == NULL) {
2625 error = got_worktree_open(&worktree, cwd);
2626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2627 goto done;
2628 else
2629 error = NULL;
2630 if (worktree) {
2631 repo_path =
2632 strdup(got_worktree_get_repo_path(worktree));
2633 if (repo_path == NULL)
2634 error = got_error_from_errno("strdup");
2635 if (error)
2636 goto done;
2637 } else {
2638 repo_path = strdup(cwd);
2639 if (repo_path == NULL) {
2640 error = got_error_from_errno("strdup");
2641 goto done;
2646 error = got_repo_open(&repo, repo_path);
2647 if (error != NULL)
2648 goto done;
2650 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2651 if (error)
2652 goto done;
2654 if (path == NULL) {
2655 if (worktree) {
2656 char *p, *worktree_subdir = cwd +
2657 strlen(got_worktree_get_root_path(worktree));
2658 if (asprintf(&p, "%s/%s",
2659 got_worktree_get_path_prefix(worktree),
2660 worktree_subdir) == -1) {
2661 error = got_error_from_errno("asprintf");
2662 goto done;
2664 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2665 free(p);
2666 if (error)
2667 goto done;
2668 } else
2669 path = "/";
2671 if (in_repo_path == NULL) {
2672 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2673 if (error != NULL)
2674 goto done;
2677 if (commit_id_str == NULL) {
2678 struct got_reference *head_ref;
2679 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2680 if (error != NULL)
2681 goto done;
2682 error = got_ref_resolve(&commit_id, repo, head_ref);
2683 got_ref_close(head_ref);
2684 if (error != NULL)
2685 goto done;
2686 } else {
2687 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2688 if (error)
2689 goto done;
2692 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2693 in_repo_path, repo);
2694 done:
2695 free(in_repo_path);
2696 free(repo_path);
2697 free(cwd);
2698 free(commit_id);
2699 if (worktree)
2700 got_worktree_close(worktree);
2701 if (repo) {
2702 const struct got_error *repo_error;
2703 repo_error = got_repo_close(repo);
2704 if (error == NULL)
2705 error = repo_error;
2707 return error;
2710 __dead static void
2711 usage_status(void)
2713 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2714 exit(1);
2717 static const struct got_error *
2718 print_status(void *arg, unsigned char status, unsigned char staged_status,
2719 const char *path, struct got_object_id *blob_id,
2720 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2722 if (status == staged_status && (status == GOT_STATUS_DELETE))
2723 status = GOT_STATUS_NO_CHANGE;
2724 printf("%c%c %s\n", status, staged_status, path);
2725 return NULL;
2728 static const struct got_error *
2729 cmd_status(int argc, char *argv[])
2731 const struct got_error *error = NULL;
2732 struct got_repository *repo = NULL;
2733 struct got_worktree *worktree = NULL;
2734 char *cwd = NULL;
2735 struct got_pathlist_head paths;
2736 struct got_pathlist_entry *pe;
2737 int ch;
2739 TAILQ_INIT(&paths);
2741 while ((ch = getopt(argc, argv, "")) != -1) {
2742 switch (ch) {
2743 default:
2744 usage_status();
2745 /* NOTREACHED */
2749 argc -= optind;
2750 argv += optind;
2752 #ifndef PROFILE
2753 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2754 NULL) == -1)
2755 err(1, "pledge");
2756 #endif
2757 cwd = getcwd(NULL, 0);
2758 if (cwd == NULL) {
2759 error = got_error_from_errno("getcwd");
2760 goto done;
2763 error = got_worktree_open(&worktree, cwd);
2764 if (error != NULL)
2765 goto done;
2767 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2768 if (error != NULL)
2769 goto done;
2771 error = apply_unveil(got_repo_get_path(repo), 1,
2772 got_worktree_get_root_path(worktree));
2773 if (error)
2774 goto done;
2776 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2777 if (error)
2778 goto done;
2780 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2781 check_cancelled, NULL);
2782 done:
2783 TAILQ_FOREACH(pe, &paths, entry)
2784 free((char *)pe->path);
2785 got_pathlist_free(&paths);
2786 free(cwd);
2787 return error;
2790 __dead static void
2791 usage_ref(void)
2793 fprintf(stderr,
2794 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2795 getprogname());
2796 exit(1);
2799 static const struct got_error *
2800 list_refs(struct got_repository *repo)
2802 static const struct got_error *err = NULL;
2803 struct got_reflist_head refs;
2804 struct got_reflist_entry *re;
2806 SIMPLEQ_INIT(&refs);
2807 err = got_ref_list(&refs, repo);
2808 if (err)
2809 return err;
2811 SIMPLEQ_FOREACH(re, &refs, entry) {
2812 char *refstr;
2813 refstr = got_ref_to_str(re->ref);
2814 if (refstr == NULL)
2815 return got_error_from_errno("got_ref_to_str");
2816 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2817 free(refstr);
2820 got_ref_list_free(&refs);
2821 return NULL;
2824 static const struct got_error *
2825 delete_ref(struct got_repository *repo, const char *refname)
2827 const struct got_error *err = NULL;
2828 struct got_reference *ref;
2830 err = got_ref_open(&ref, repo, refname, 0);
2831 if (err)
2832 return err;
2834 err = got_ref_delete(ref, repo);
2835 got_ref_close(ref);
2836 return err;
2839 static const struct got_error *
2840 add_ref(struct got_repository *repo, const char *refname, const char *target)
2842 const struct got_error *err = NULL;
2843 struct got_object_id *id;
2844 struct got_reference *ref = NULL;
2847 * Don't let the user create a reference named '-'.
2848 * While technically a valid reference name, this case is usually
2849 * an unintended typo.
2851 if (refname[0] == '-' && refname[1] == '\0')
2852 return got_error(GOT_ERR_BAD_REF_NAME);
2854 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2855 repo);
2856 if (err) {
2857 struct got_reference *target_ref;
2859 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2860 return err;
2861 err = got_ref_open(&target_ref, repo, target, 0);
2862 if (err)
2863 return err;
2864 err = got_ref_resolve(&id, repo, target_ref);
2865 got_ref_close(target_ref);
2866 if (err)
2867 return err;
2870 err = got_ref_alloc(&ref, refname, id);
2871 if (err)
2872 goto done;
2874 err = got_ref_write(ref, repo);
2875 done:
2876 if (ref)
2877 got_ref_close(ref);
2878 free(id);
2879 return err;
2882 static const struct got_error *
2883 add_symref(struct got_repository *repo, const char *refname, const char *target)
2885 const struct got_error *err = NULL;
2886 struct got_reference *ref = NULL;
2887 struct got_reference *target_ref = NULL;
2890 * Don't let the user create a reference named '-'.
2891 * While technically a valid reference name, this case is usually
2892 * an unintended typo.
2894 if (refname[0] == '-' && refname[1] == '\0')
2895 return got_error(GOT_ERR_BAD_REF_NAME);
2897 err = got_ref_open(&target_ref, repo, target, 0);
2898 if (err)
2899 return err;
2901 err = got_ref_alloc_symref(&ref, refname, target_ref);
2902 if (err)
2903 goto done;
2905 err = got_ref_write(ref, repo);
2906 done:
2907 if (target_ref)
2908 got_ref_close(target_ref);
2909 if (ref)
2910 got_ref_close(ref);
2911 return err;
2914 static const struct got_error *
2915 cmd_ref(int argc, char *argv[])
2917 const struct got_error *error = NULL;
2918 struct got_repository *repo = NULL;
2919 struct got_worktree *worktree = NULL;
2920 char *cwd = NULL, *repo_path = NULL;
2921 int ch, do_list = 0, create_symref = 0;
2922 const char *delref = NULL;
2924 /* TODO: Add -s option for adding symbolic references. */
2925 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2926 switch (ch) {
2927 case 'd':
2928 delref = optarg;
2929 break;
2930 case 'r':
2931 repo_path = realpath(optarg, NULL);
2932 if (repo_path == NULL)
2933 err(1, "-r option");
2934 got_path_strip_trailing_slashes(repo_path);
2935 break;
2936 case 'l':
2937 do_list = 1;
2938 break;
2939 case 's':
2940 create_symref = 1;
2941 break;
2942 default:
2943 usage_ref();
2944 /* NOTREACHED */
2948 if (do_list && delref)
2949 errx(1, "-l and -d options are mutually exclusive\n");
2951 argc -= optind;
2952 argv += optind;
2954 if (do_list || delref) {
2955 if (create_symref)
2956 errx(1, "-s option cannot be used together with the "
2957 "-l or -d options");
2958 if (argc > 0)
2959 usage_ref();
2960 } else if (argc != 2)
2961 usage_ref();
2963 #ifndef PROFILE
2964 if (do_list) {
2965 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2966 NULL) == -1)
2967 err(1, "pledge");
2968 } else {
2969 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2970 "sendfd unveil", NULL) == -1)
2971 err(1, "pledge");
2973 #endif
2974 cwd = getcwd(NULL, 0);
2975 if (cwd == NULL) {
2976 error = got_error_from_errno("getcwd");
2977 goto done;
2980 if (repo_path == NULL) {
2981 error = got_worktree_open(&worktree, cwd);
2982 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2983 goto done;
2984 else
2985 error = NULL;
2986 if (worktree) {
2987 repo_path =
2988 strdup(got_worktree_get_repo_path(worktree));
2989 if (repo_path == NULL)
2990 error = got_error_from_errno("strdup");
2991 if (error)
2992 goto done;
2993 } else {
2994 repo_path = strdup(cwd);
2995 if (repo_path == NULL) {
2996 error = got_error_from_errno("strdup");
2997 goto done;
3002 error = got_repo_open(&repo, repo_path);
3003 if (error != NULL)
3004 goto done;
3006 error = apply_unveil(got_repo_get_path(repo), do_list,
3007 worktree ? got_worktree_get_root_path(worktree) : NULL);
3008 if (error)
3009 goto done;
3011 if (do_list)
3012 error = list_refs(repo);
3013 else if (delref)
3014 error = delete_ref(repo, delref);
3015 else if (create_symref)
3016 error = add_symref(repo, argv[0], argv[1]);
3017 else
3018 error = add_ref(repo, argv[0], argv[1]);
3019 done:
3020 if (repo)
3021 got_repo_close(repo);
3022 if (worktree)
3023 got_worktree_close(worktree);
3024 free(cwd);
3025 free(repo_path);
3026 return error;
3029 __dead static void
3030 usage_branch(void)
3032 fprintf(stderr,
3033 "usage: %s branch [-r repository] -l | -d name | "
3034 "name [base-branch]\n", getprogname());
3035 exit(1);
3038 static const struct got_error *
3039 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3041 static const struct got_error *err = NULL;
3042 struct got_reflist_head refs;
3043 struct got_reflist_entry *re;
3045 SIMPLEQ_INIT(&refs);
3047 err = got_ref_list(&refs, repo);
3048 if (err)
3049 return err;
3051 SIMPLEQ_FOREACH(re, &refs, entry) {
3052 const char *refname, *marker = " ";
3053 char *refstr;
3054 refname = got_ref_get_name(re->ref);
3055 if (strncmp(refname, "refs/heads/", 11) != 0)
3056 continue;
3057 if (worktree && strcmp(refname,
3058 got_worktree_get_head_ref_name(worktree)) == 0) {
3059 struct got_object_id *id = NULL;
3060 err = got_ref_resolve(&id, repo, re->ref);
3061 if (err)
3062 return err;
3063 if (got_object_id_cmp(id,
3064 got_worktree_get_base_commit_id(worktree)) == 0)
3065 marker = "* ";
3066 else
3067 marker = "~ ";
3068 free(id);
3070 refname += 11;
3071 refstr = got_ref_to_str(re->ref);
3072 if (refstr == NULL)
3073 return got_error_from_errno("got_ref_to_str");
3074 printf("%s%s: %s\n", marker, refname, refstr);
3075 free(refstr);
3078 got_ref_list_free(&refs);
3079 return NULL;
3082 static const struct got_error *
3083 delete_branch(struct got_repository *repo, const char *branch_name)
3085 const struct got_error *err = NULL;
3086 struct got_reference *ref;
3087 char *refname;
3089 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3090 return got_error_from_errno("asprintf");
3092 err = got_ref_open(&ref, repo, refname, 0);
3093 if (err)
3094 goto done;
3096 err = got_ref_delete(ref, repo);
3097 got_ref_close(ref);
3098 done:
3099 free(refname);
3100 return err;
3103 static const struct got_error *
3104 add_branch(struct got_repository *repo, const char *branch_name,
3105 const char *base_branch)
3107 const struct got_error *err = NULL;
3108 struct got_object_id *id = NULL;
3109 struct got_reference *ref = NULL;
3110 char *base_refname = NULL, *refname = NULL;
3111 struct got_reference *base_ref;
3114 * Don't let the user create a branch named '-'.
3115 * While technically a valid reference name, this case is usually
3116 * an unintended typo.
3118 if (branch_name[0] == '-' && branch_name[1] == '\0')
3119 return got_error(GOT_ERR_BAD_REF_NAME);
3121 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3122 base_refname = strdup(GOT_REF_HEAD);
3123 if (base_refname == NULL)
3124 return got_error_from_errno("strdup");
3125 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3126 return got_error_from_errno("asprintf");
3128 err = got_ref_open(&base_ref, repo, base_refname, 0);
3129 if (err)
3130 goto done;
3131 err = got_ref_resolve(&id, repo, base_ref);
3132 got_ref_close(base_ref);
3133 if (err)
3134 goto done;
3136 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3137 err = got_error_from_errno("asprintf");
3138 goto done;
3141 err = got_ref_open(&ref, repo, refname, 0);
3142 if (err == NULL) {
3143 err = got_error(GOT_ERR_BRANCH_EXISTS);
3144 goto done;
3145 } else if (err->code != GOT_ERR_NOT_REF)
3146 goto done;
3148 err = got_ref_alloc(&ref, refname, id);
3149 if (err)
3150 goto done;
3152 err = got_ref_write(ref, repo);
3153 done:
3154 if (ref)
3155 got_ref_close(ref);
3156 free(id);
3157 free(base_refname);
3158 free(refname);
3159 return err;
3162 static const struct got_error *
3163 cmd_branch(int argc, char *argv[])
3165 const struct got_error *error = NULL;
3166 struct got_repository *repo = NULL;
3167 struct got_worktree *worktree = NULL;
3168 char *cwd = NULL, *repo_path = NULL;
3169 int ch, do_list = 0;
3170 const char *delref = NULL;
3172 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3173 switch (ch) {
3174 case 'd':
3175 delref = optarg;
3176 break;
3177 case 'r':
3178 repo_path = realpath(optarg, NULL);
3179 if (repo_path == NULL)
3180 err(1, "-r option");
3181 got_path_strip_trailing_slashes(repo_path);
3182 break;
3183 case 'l':
3184 do_list = 1;
3185 break;
3186 default:
3187 usage_branch();
3188 /* NOTREACHED */
3192 if (do_list && delref)
3193 errx(1, "-l and -d options are mutually exclusive\n");
3195 argc -= optind;
3196 argv += optind;
3198 if (do_list || delref) {
3199 if (argc > 0)
3200 usage_branch();
3201 } else if (argc < 1 || argc > 2)
3202 usage_branch();
3204 #ifndef PROFILE
3205 if (do_list) {
3206 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3207 NULL) == -1)
3208 err(1, "pledge");
3209 } else {
3210 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3211 "sendfd unveil", NULL) == -1)
3212 err(1, "pledge");
3214 #endif
3215 cwd = getcwd(NULL, 0);
3216 if (cwd == NULL) {
3217 error = got_error_from_errno("getcwd");
3218 goto done;
3221 if (repo_path == NULL) {
3222 error = got_worktree_open(&worktree, cwd);
3223 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3224 goto done;
3225 else
3226 error = NULL;
3227 if (worktree) {
3228 repo_path =
3229 strdup(got_worktree_get_repo_path(worktree));
3230 if (repo_path == NULL)
3231 error = got_error_from_errno("strdup");
3232 if (error)
3233 goto done;
3234 } else {
3235 repo_path = strdup(cwd);
3236 if (repo_path == NULL) {
3237 error = got_error_from_errno("strdup");
3238 goto done;
3243 error = got_repo_open(&repo, repo_path);
3244 if (error != NULL)
3245 goto done;
3247 error = apply_unveil(got_repo_get_path(repo), do_list,
3248 worktree ? got_worktree_get_root_path(worktree) : NULL);
3249 if (error)
3250 goto done;
3252 if (do_list)
3253 error = list_branches(repo, worktree);
3254 else if (delref)
3255 error = delete_branch(repo, delref);
3256 else {
3257 const char *base_branch;
3258 if (argc == 1) {
3259 base_branch = worktree ?
3260 got_worktree_get_head_ref_name(worktree) :
3261 GOT_REF_HEAD;
3262 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3263 base_branch += 11;
3264 } else
3265 base_branch = argv[1];
3266 error = add_branch(repo, argv[0], base_branch);
3268 done:
3269 if (repo)
3270 got_repo_close(repo);
3271 if (worktree)
3272 got_worktree_close(worktree);
3273 free(cwd);
3274 free(repo_path);
3275 return error;
3278 __dead static void
3279 usage_add(void)
3281 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3282 exit(1);
3285 static const struct got_error *
3286 cmd_add(int argc, char *argv[])
3288 const struct got_error *error = NULL;
3289 struct got_repository *repo = NULL;
3290 struct got_worktree *worktree = NULL;
3291 char *cwd = NULL;
3292 struct got_pathlist_head paths;
3293 struct got_pathlist_entry *pe;
3294 int ch;
3296 TAILQ_INIT(&paths);
3298 while ((ch = getopt(argc, argv, "")) != -1) {
3299 switch (ch) {
3300 default:
3301 usage_add();
3302 /* NOTREACHED */
3306 argc -= optind;
3307 argv += optind;
3309 #ifndef PROFILE
3310 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3311 NULL) == -1)
3312 err(1, "pledge");
3313 #endif
3314 if (argc < 1)
3315 usage_add();
3317 cwd = getcwd(NULL, 0);
3318 if (cwd == NULL) {
3319 error = got_error_from_errno("getcwd");
3320 goto done;
3323 error = got_worktree_open(&worktree, cwd);
3324 if (error)
3325 goto done;
3327 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3328 if (error != NULL)
3329 goto done;
3331 error = apply_unveil(got_repo_get_path(repo), 1,
3332 got_worktree_get_root_path(worktree));
3333 if (error)
3334 goto done;
3336 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3337 if (error)
3338 goto done;
3340 error = got_worktree_schedule_add(worktree, &paths, print_status,
3341 NULL, repo);
3342 done:
3343 if (repo)
3344 got_repo_close(repo);
3345 if (worktree)
3346 got_worktree_close(worktree);
3347 TAILQ_FOREACH(pe, &paths, entry)
3348 free((char *)pe->path);
3349 got_pathlist_free(&paths);
3350 free(cwd);
3351 return error;
3354 __dead static void
3355 usage_remove(void)
3357 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3358 exit(1);
3361 static const struct got_error *
3362 cmd_remove(int argc, char *argv[])
3364 const struct got_error *error = NULL;
3365 struct got_worktree *worktree = NULL;
3366 struct got_repository *repo = NULL;
3367 char *cwd = NULL;
3368 struct got_pathlist_head paths;
3369 struct got_pathlist_entry *pe;
3370 int ch, delete_local_mods = 0;
3372 TAILQ_INIT(&paths);
3374 while ((ch = getopt(argc, argv, "f")) != -1) {
3375 switch (ch) {
3376 case 'f':
3377 delete_local_mods = 1;
3378 break;
3379 default:
3380 usage_add();
3381 /* NOTREACHED */
3385 argc -= optind;
3386 argv += optind;
3388 #ifndef PROFILE
3389 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3390 NULL) == -1)
3391 err(1, "pledge");
3392 #endif
3393 if (argc < 1)
3394 usage_remove();
3396 cwd = getcwd(NULL, 0);
3397 if (cwd == NULL) {
3398 error = got_error_from_errno("getcwd");
3399 goto done;
3401 error = got_worktree_open(&worktree, cwd);
3402 if (error)
3403 goto done;
3405 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3406 if (error)
3407 goto done;
3409 error = apply_unveil(got_repo_get_path(repo), 1,
3410 got_worktree_get_root_path(worktree));
3411 if (error)
3412 goto done;
3414 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3415 if (error)
3416 goto done;
3418 error = got_worktree_schedule_delete(worktree, &paths,
3419 delete_local_mods, print_status, NULL, repo);
3420 if (error)
3421 goto done;
3422 done:
3423 if (repo)
3424 got_repo_close(repo);
3425 if (worktree)
3426 got_worktree_close(worktree);
3427 TAILQ_FOREACH(pe, &paths, entry)
3428 free((char *)pe->path);
3429 got_pathlist_free(&paths);
3430 free(cwd);
3431 return error;
3434 __dead static void
3435 usage_revert(void)
3437 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3438 "path ...\n", getprogname());
3439 exit(1);
3442 static const struct got_error *
3443 revert_progress(void *arg, unsigned char status, const char *path)
3445 while (path[0] == '/')
3446 path++;
3447 printf("%c %s\n", status, path);
3448 return NULL;
3451 struct choose_patch_arg {
3452 FILE *patch_script_file;
3453 const char *action;
3456 static const struct got_error *
3457 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3458 int nchanges, const char *action)
3460 char *line = NULL;
3461 size_t linesize = 0;
3462 ssize_t linelen;
3464 switch (status) {
3465 case GOT_STATUS_ADD:
3466 printf("A %s\n%s this addition? [y/n] ", path, action);
3467 break;
3468 case GOT_STATUS_DELETE:
3469 printf("D %s\n%s this deletion? [y/n] ", path, action);
3470 break;
3471 case GOT_STATUS_MODIFY:
3472 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3473 return got_error_from_errno("fseek");
3474 printf(GOT_COMMIT_SEP_STR);
3475 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3476 printf("%s", line);
3477 if (ferror(patch_file))
3478 return got_error_from_errno("getline");
3479 printf(GOT_COMMIT_SEP_STR);
3480 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3481 path, n, nchanges, action);
3482 break;
3483 default:
3484 return got_error_path(path, GOT_ERR_FILE_STATUS);
3487 return NULL;
3490 static const struct got_error *
3491 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3492 FILE *patch_file, int n, int nchanges)
3494 const struct got_error *err = NULL;
3495 char *line = NULL;
3496 size_t linesize = 0;
3497 ssize_t linelen;
3498 int resp = ' ';
3499 struct choose_patch_arg *a = arg;
3501 *choice = GOT_PATCH_CHOICE_NONE;
3503 if (a->patch_script_file) {
3504 char *nl;
3505 err = show_change(status, path, patch_file, n, nchanges,
3506 a->action);
3507 if (err)
3508 return err;
3509 linelen = getline(&line, &linesize, a->patch_script_file);
3510 if (linelen == -1) {
3511 if (ferror(a->patch_script_file))
3512 return got_error_from_errno("getline");
3513 return NULL;
3515 nl = strchr(line, '\n');
3516 if (nl)
3517 *nl = '\0';
3518 if (strcmp(line, "y") == 0) {
3519 *choice = GOT_PATCH_CHOICE_YES;
3520 printf("y\n");
3521 } else if (strcmp(line, "n") == 0) {
3522 *choice = GOT_PATCH_CHOICE_NO;
3523 printf("n\n");
3524 } else if (strcmp(line, "q") == 0 &&
3525 status == GOT_STATUS_MODIFY) {
3526 *choice = GOT_PATCH_CHOICE_QUIT;
3527 printf("q\n");
3528 } else
3529 printf("invalid response '%s'\n", line);
3530 free(line);
3531 return NULL;
3534 while (resp != 'y' && resp != 'n' && resp != 'q') {
3535 err = show_change(status, path, patch_file, n, nchanges,
3536 a->action);
3537 if (err)
3538 return err;
3539 resp = getchar();
3540 if (resp == '\n')
3541 resp = getchar();
3542 if (status == GOT_STATUS_MODIFY) {
3543 if (resp != 'y' && resp != 'n' && resp != 'q') {
3544 printf("invalid response '%c'\n", resp);
3545 resp = ' ';
3547 } else if (resp != 'y' && resp != 'n') {
3548 printf("invalid response '%c'\n", resp);
3549 resp = ' ';
3553 if (resp == 'y')
3554 *choice = GOT_PATCH_CHOICE_YES;
3555 else if (resp == 'n')
3556 *choice = GOT_PATCH_CHOICE_NO;
3557 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3558 *choice = GOT_PATCH_CHOICE_QUIT;
3560 return NULL;
3564 static const struct got_error *
3565 cmd_revert(int argc, char *argv[])
3567 const struct got_error *error = NULL;
3568 struct got_worktree *worktree = NULL;
3569 struct got_repository *repo = NULL;
3570 char *cwd = NULL, *path = NULL;
3571 struct got_pathlist_head paths;
3572 struct got_pathlist_entry *pe;
3573 int ch, can_recurse = 0, pflag = 0;
3574 FILE *patch_script_file = NULL;
3575 const char *patch_script_path = NULL;
3576 struct choose_patch_arg cpa;
3578 TAILQ_INIT(&paths);
3580 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3581 switch (ch) {
3582 case 'p':
3583 pflag = 1;
3584 break;
3585 case 'F':
3586 patch_script_path = optarg;
3587 break;
3588 case 'R':
3589 can_recurse = 1;
3590 break;
3591 default:
3592 usage_revert();
3593 /* NOTREACHED */
3597 argc -= optind;
3598 argv += optind;
3600 #ifndef PROFILE
3601 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3602 "unveil", NULL) == -1)
3603 err(1, "pledge");
3604 #endif
3605 if (argc < 1)
3606 usage_revert();
3607 if (patch_script_path && !pflag)
3608 errx(1, "-F option can only be used together with -p option");
3610 cwd = getcwd(NULL, 0);
3611 if (cwd == NULL) {
3612 error = got_error_from_errno("getcwd");
3613 goto done;
3615 error = got_worktree_open(&worktree, cwd);
3616 if (error)
3617 goto done;
3619 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3620 if (error != NULL)
3621 goto done;
3623 if (patch_script_path) {
3624 patch_script_file = fopen(patch_script_path, "r");
3625 if (patch_script_file == NULL) {
3626 error = got_error_from_errno2("fopen",
3627 patch_script_path);
3628 goto done;
3631 error = apply_unveil(got_repo_get_path(repo), 1,
3632 got_worktree_get_root_path(worktree));
3633 if (error)
3634 goto done;
3636 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3637 if (error)
3638 goto done;
3640 if (!can_recurse) {
3641 char *ondisk_path;
3642 struct stat sb;
3643 TAILQ_FOREACH(pe, &paths, entry) {
3644 if (asprintf(&ondisk_path, "%s/%s",
3645 got_worktree_get_root_path(worktree),
3646 pe->path) == -1) {
3647 error = got_error_from_errno("asprintf");
3648 goto done;
3650 if (lstat(ondisk_path, &sb) == -1) {
3651 if (errno == ENOENT) {
3652 free(ondisk_path);
3653 continue;
3655 error = got_error_from_errno2("lstat",
3656 ondisk_path);
3657 free(ondisk_path);
3658 goto done;
3660 free(ondisk_path);
3661 if (S_ISDIR(sb.st_mode)) {
3662 error = got_error_msg(GOT_ERR_BAD_PATH,
3663 "reverting directories requires -R option");
3664 goto done;
3669 cpa.patch_script_file = patch_script_file;
3670 cpa.action = "revert";
3671 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3672 pflag ? choose_patch : NULL, &cpa, repo);
3673 if (error)
3674 goto done;
3675 done:
3676 if (patch_script_file && fclose(patch_script_file) == EOF &&
3677 error == NULL)
3678 error = got_error_from_errno2("fclose", patch_script_path);
3679 if (repo)
3680 got_repo_close(repo);
3681 if (worktree)
3682 got_worktree_close(worktree);
3683 free(path);
3684 free(cwd);
3685 return error;
3688 __dead static void
3689 usage_commit(void)
3691 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3692 getprogname());
3693 exit(1);
3696 struct collect_commit_logmsg_arg {
3697 const char *cmdline_log;
3698 const char *editor;
3699 const char *worktree_path;
3700 const char *branch_name;
3701 const char *repo_path;
3702 char *logmsg_path;
3706 static const struct got_error *
3707 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3708 void *arg)
3710 char *initial_content = NULL;
3711 struct got_pathlist_entry *pe;
3712 const struct got_error *err = NULL;
3713 char *template = NULL;
3714 struct collect_commit_logmsg_arg *a = arg;
3715 int fd;
3716 size_t len;
3718 /* if a message was specified on the command line, just use it */
3719 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3720 len = strlen(a->cmdline_log) + 1;
3721 *logmsg = malloc(len + 1);
3722 if (*logmsg == NULL)
3723 return got_error_from_errno("malloc");
3724 strlcpy(*logmsg, a->cmdline_log, len);
3725 return NULL;
3728 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3729 return got_error_from_errno("asprintf");
3731 if (asprintf(&initial_content,
3732 "\n# changes to be committed on branch %s:\n",
3733 a->branch_name) == -1)
3734 return got_error_from_errno("asprintf");
3736 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3737 if (err)
3738 goto done;
3740 dprintf(fd, initial_content);
3742 TAILQ_FOREACH(pe, commitable_paths, entry) {
3743 struct got_commitable *ct = pe->data;
3744 dprintf(fd, "# %c %s\n",
3745 got_commitable_get_status(ct),
3746 got_commitable_get_path(ct));
3748 close(fd);
3750 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3751 done:
3752 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3753 unlink(a->logmsg_path);
3754 free(a->logmsg_path);
3755 a->logmsg_path = NULL;
3757 free(initial_content);
3758 free(template);
3760 /* Editor is done; we can now apply unveil(2) */
3761 if (err == NULL) {
3762 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3763 if (err) {
3764 free(*logmsg);
3765 *logmsg = NULL;
3768 return err;
3771 static const struct got_error *
3772 cmd_commit(int argc, char *argv[])
3774 const struct got_error *error = NULL;
3775 struct got_worktree *worktree = NULL;
3776 struct got_repository *repo = NULL;
3777 char *cwd = NULL, *id_str = NULL;
3778 struct got_object_id *id = NULL;
3779 const char *logmsg = NULL;
3780 const char *author;
3781 struct collect_commit_logmsg_arg cl_arg;
3782 char *editor = NULL;
3783 int ch, rebase_in_progress, histedit_in_progress;
3784 struct got_pathlist_head paths;
3786 TAILQ_INIT(&paths);
3787 cl_arg.logmsg_path = NULL;
3789 while ((ch = getopt(argc, argv, "m:")) != -1) {
3790 switch (ch) {
3791 case 'm':
3792 logmsg = optarg;
3793 break;
3794 default:
3795 usage_commit();
3796 /* NOTREACHED */
3800 argc -= optind;
3801 argv += optind;
3803 #ifndef PROFILE
3804 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3805 "unveil", NULL) == -1)
3806 err(1, "pledge");
3807 #endif
3808 error = get_author(&author);
3809 if (error)
3810 return error;
3812 cwd = getcwd(NULL, 0);
3813 if (cwd == NULL) {
3814 error = got_error_from_errno("getcwd");
3815 goto done;
3817 error = got_worktree_open(&worktree, cwd);
3818 if (error)
3819 goto done;
3821 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3822 if (error)
3823 goto done;
3824 if (rebase_in_progress) {
3825 error = got_error(GOT_ERR_REBASING);
3826 goto done;
3829 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3830 worktree);
3831 if (error)
3832 goto done;
3834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3835 if (error != NULL)
3836 goto done;
3839 * unveil(2) traverses exec(2); if an editor is used we have
3840 * to apply unveil after the log message has been written.
3842 if (logmsg == NULL || strlen(logmsg) == 0)
3843 error = get_editor(&editor);
3844 else
3845 error = apply_unveil(got_repo_get_path(repo), 0,
3846 got_worktree_get_root_path(worktree));
3847 if (error)
3848 goto done;
3850 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3851 if (error)
3852 goto done;
3854 cl_arg.editor = editor;
3855 cl_arg.cmdline_log = logmsg;
3856 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3857 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3858 if (!histedit_in_progress) {
3859 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3860 error = got_error(GOT_ERR_COMMIT_BRANCH);
3861 goto done;
3863 cl_arg.branch_name += 11;
3865 cl_arg.repo_path = got_repo_get_path(repo);
3866 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3867 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3868 if (error) {
3869 if (cl_arg.logmsg_path)
3870 fprintf(stderr, "%s: log message preserved in %s\n",
3871 getprogname(), cl_arg.logmsg_path);
3872 goto done;
3875 if (cl_arg.logmsg_path)
3876 unlink(cl_arg.logmsg_path);
3878 error = got_object_id_str(&id_str, id);
3879 if (error)
3880 goto done;
3881 printf("Created commit %s\n", id_str);
3882 done:
3883 free(cl_arg.logmsg_path);
3884 if (repo)
3885 got_repo_close(repo);
3886 if (worktree)
3887 got_worktree_close(worktree);
3888 free(cwd);
3889 free(id_str);
3890 free(editor);
3891 return error;
3894 __dead static void
3895 usage_cherrypick(void)
3897 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3898 exit(1);
3901 static const struct got_error *
3902 cmd_cherrypick(int argc, char *argv[])
3904 const struct got_error *error = NULL;
3905 struct got_worktree *worktree = NULL;
3906 struct got_repository *repo = NULL;
3907 char *cwd = NULL, *commit_id_str = NULL;
3908 struct got_object_id *commit_id = NULL;
3909 struct got_commit_object *commit = NULL;
3910 struct got_object_qid *pid;
3911 struct got_reference *head_ref = NULL;
3912 int ch, did_something = 0;
3914 while ((ch = getopt(argc, argv, "")) != -1) {
3915 switch (ch) {
3916 default:
3917 usage_cherrypick();
3918 /* NOTREACHED */
3922 argc -= optind;
3923 argv += optind;
3925 #ifndef PROFILE
3926 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3927 "unveil", NULL) == -1)
3928 err(1, "pledge");
3929 #endif
3930 if (argc != 1)
3931 usage_cherrypick();
3933 cwd = getcwd(NULL, 0);
3934 if (cwd == NULL) {
3935 error = got_error_from_errno("getcwd");
3936 goto done;
3938 error = got_worktree_open(&worktree, cwd);
3939 if (error)
3940 goto done;
3942 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3943 if (error != NULL)
3944 goto done;
3946 error = apply_unveil(got_repo_get_path(repo), 0,
3947 got_worktree_get_root_path(worktree));
3948 if (error)
3949 goto done;
3951 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3952 GOT_OBJ_TYPE_COMMIT, repo);
3953 if (error != NULL) {
3954 struct got_reference *ref;
3955 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3956 goto done;
3957 error = got_ref_open(&ref, repo, argv[0], 0);
3958 if (error != NULL)
3959 goto done;
3960 error = got_ref_resolve(&commit_id, repo, ref);
3961 got_ref_close(ref);
3962 if (error != NULL)
3963 goto done;
3965 error = got_object_id_str(&commit_id_str, commit_id);
3966 if (error)
3967 goto done;
3969 error = got_ref_open(&head_ref, repo,
3970 got_worktree_get_head_ref_name(worktree), 0);
3971 if (error != NULL)
3972 goto done;
3974 error = check_same_branch(commit_id, head_ref, NULL, repo);
3975 if (error) {
3976 if (error->code != GOT_ERR_ANCESTRY)
3977 goto done;
3978 error = NULL;
3979 } else {
3980 error = got_error(GOT_ERR_SAME_BRANCH);
3981 goto done;
3984 error = got_object_open_as_commit(&commit, repo, commit_id);
3985 if (error)
3986 goto done;
3987 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3988 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3989 commit_id, repo, update_progress, &did_something, check_cancelled,
3990 NULL);
3991 if (error != NULL)
3992 goto done;
3994 if (did_something)
3995 printf("Merged commit %s\n", commit_id_str);
3996 done:
3997 if (commit)
3998 got_object_commit_close(commit);
3999 free(commit_id_str);
4000 if (head_ref)
4001 got_ref_close(head_ref);
4002 if (worktree)
4003 got_worktree_close(worktree);
4004 if (repo)
4005 got_repo_close(repo);
4006 return error;
4009 __dead static void
4010 usage_backout(void)
4012 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4013 exit(1);
4016 static const struct got_error *
4017 cmd_backout(int argc, char *argv[])
4019 const struct got_error *error = NULL;
4020 struct got_worktree *worktree = NULL;
4021 struct got_repository *repo = NULL;
4022 char *cwd = NULL, *commit_id_str = NULL;
4023 struct got_object_id *commit_id = NULL;
4024 struct got_commit_object *commit = NULL;
4025 struct got_object_qid *pid;
4026 struct got_reference *head_ref = NULL;
4027 int ch, did_something = 0;
4029 while ((ch = getopt(argc, argv, "")) != -1) {
4030 switch (ch) {
4031 default:
4032 usage_backout();
4033 /* NOTREACHED */
4037 argc -= optind;
4038 argv += optind;
4040 #ifndef PROFILE
4041 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4042 "unveil", NULL) == -1)
4043 err(1, "pledge");
4044 #endif
4045 if (argc != 1)
4046 usage_backout();
4048 cwd = getcwd(NULL, 0);
4049 if (cwd == NULL) {
4050 error = got_error_from_errno("getcwd");
4051 goto done;
4053 error = got_worktree_open(&worktree, cwd);
4054 if (error)
4055 goto done;
4057 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4058 if (error != NULL)
4059 goto done;
4061 error = apply_unveil(got_repo_get_path(repo), 0,
4062 got_worktree_get_root_path(worktree));
4063 if (error)
4064 goto done;
4066 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4067 GOT_OBJ_TYPE_COMMIT, repo);
4068 if (error != NULL) {
4069 struct got_reference *ref;
4070 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4071 goto done;
4072 error = got_ref_open(&ref, repo, argv[0], 0);
4073 if (error != NULL)
4074 goto done;
4075 error = got_ref_resolve(&commit_id, repo, ref);
4076 got_ref_close(ref);
4077 if (error != NULL)
4078 goto done;
4080 error = got_object_id_str(&commit_id_str, commit_id);
4081 if (error)
4082 goto done;
4084 error = got_ref_open(&head_ref, repo,
4085 got_worktree_get_head_ref_name(worktree), 0);
4086 if (error != NULL)
4087 goto done;
4089 error = check_same_branch(commit_id, head_ref, NULL, repo);
4090 if (error)
4091 goto done;
4093 error = got_object_open_as_commit(&commit, repo, commit_id);
4094 if (error)
4095 goto done;
4096 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4097 if (pid == NULL) {
4098 error = got_error(GOT_ERR_ROOT_COMMIT);
4099 goto done;
4102 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4103 update_progress, &did_something, check_cancelled, NULL);
4104 if (error != NULL)
4105 goto done;
4107 if (did_something)
4108 printf("Backed out commit %s\n", commit_id_str);
4109 done:
4110 if (commit)
4111 got_object_commit_close(commit);
4112 free(commit_id_str);
4113 if (head_ref)
4114 got_ref_close(head_ref);
4115 if (worktree)
4116 got_worktree_close(worktree);
4117 if (repo)
4118 got_repo_close(repo);
4119 return error;
4122 __dead static void
4123 usage_rebase(void)
4125 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4126 getprogname());
4127 exit(1);
4130 void
4131 trim_logmsg(char *logmsg, int limit)
4133 char *nl;
4134 size_t len;
4136 len = strlen(logmsg);
4137 if (len > limit)
4138 len = limit;
4139 logmsg[len] = '\0';
4140 nl = strchr(logmsg, '\n');
4141 if (nl)
4142 *nl = '\0';
4145 static const struct got_error *
4146 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4148 const struct got_error *err;
4149 char *logmsg0 = NULL;
4150 const char *s;
4152 err = got_object_commit_get_logmsg(&logmsg0, commit);
4153 if (err)
4154 return err;
4156 s = logmsg0;
4157 while (isspace((unsigned char)s[0]))
4158 s++;
4160 *logmsg = strdup(s);
4161 if (*logmsg == NULL) {
4162 err = got_error_from_errno("strdup");
4163 goto done;
4166 trim_logmsg(*logmsg, limit);
4167 done:
4168 free(logmsg0);
4169 return err;
4172 static const struct got_error *
4173 show_rebase_progress(struct got_commit_object *commit,
4174 struct got_object_id *old_id, struct got_object_id *new_id)
4176 const struct got_error *err;
4177 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4179 err = got_object_id_str(&old_id_str, old_id);
4180 if (err)
4181 goto done;
4183 if (new_id) {
4184 err = got_object_id_str(&new_id_str, new_id);
4185 if (err)
4186 goto done;
4189 old_id_str[12] = '\0';
4190 if (new_id_str)
4191 new_id_str[12] = '\0';
4193 err = get_short_logmsg(&logmsg, 42, commit);
4194 if (err)
4195 goto done;
4197 printf("%s -> %s: %s\n", old_id_str,
4198 new_id_str ? new_id_str : "no-op change", logmsg);
4199 done:
4200 free(old_id_str);
4201 free(new_id_str);
4202 return err;
4205 static const struct got_error *
4206 rebase_progress(void *arg, unsigned char status, const char *path)
4208 unsigned char *rebase_status = arg;
4210 while (path[0] == '/')
4211 path++;
4212 printf("%c %s\n", status, path);
4214 if (*rebase_status == GOT_STATUS_CONFLICT)
4215 return NULL;
4216 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4217 *rebase_status = status;
4218 return NULL;
4221 static const struct got_error *
4222 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4223 struct got_reference *branch, struct got_reference *new_base_branch,
4224 struct got_reference *tmp_branch, struct got_repository *repo)
4226 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4227 return got_worktree_rebase_complete(worktree, fileindex,
4228 new_base_branch, tmp_branch, branch, repo);
4231 static const struct got_error *
4232 rebase_commit(struct got_pathlist_head *merged_paths,
4233 struct got_worktree *worktree, struct got_fileindex *fileindex,
4234 struct got_reference *tmp_branch,
4235 struct got_object_id *commit_id, struct got_repository *repo)
4237 const struct got_error *error;
4238 struct got_commit_object *commit;
4239 struct got_object_id *new_commit_id;
4241 error = got_object_open_as_commit(&commit, repo, commit_id);
4242 if (error)
4243 return error;
4245 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4246 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4247 if (error) {
4248 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4249 goto done;
4250 error = show_rebase_progress(commit, commit_id, NULL);
4251 } else {
4252 error = show_rebase_progress(commit, commit_id, new_commit_id);
4253 free(new_commit_id);
4255 done:
4256 got_object_commit_close(commit);
4257 return error;
4260 struct check_path_prefix_arg {
4261 const char *path_prefix;
4262 size_t len;
4263 int errcode;
4266 static const struct got_error *
4267 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4268 struct got_blob_object *blob2, struct got_object_id *id1,
4269 struct got_object_id *id2, const char *path1, const char *path2,
4270 struct got_repository *repo)
4272 struct check_path_prefix_arg *a = arg;
4274 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4275 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4276 return got_error(a->errcode);
4278 return NULL;
4281 static const struct got_error *
4282 check_path_prefix(struct got_object_id *parent_id,
4283 struct got_object_id *commit_id, const char *path_prefix,
4284 int errcode, struct got_repository *repo)
4286 const struct got_error *err;
4287 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4288 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4289 struct check_path_prefix_arg cpp_arg;
4291 if (got_path_is_root_dir(path_prefix))
4292 return NULL;
4294 err = got_object_open_as_commit(&commit, repo, commit_id);
4295 if (err)
4296 goto done;
4298 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4299 if (err)
4300 goto done;
4302 err = got_object_open_as_tree(&tree1, repo,
4303 got_object_commit_get_tree_id(parent_commit));
4304 if (err)
4305 goto done;
4307 err = got_object_open_as_tree(&tree2, repo,
4308 got_object_commit_get_tree_id(commit));
4309 if (err)
4310 goto done;
4312 cpp_arg.path_prefix = path_prefix;
4313 while (cpp_arg.path_prefix[0] == '/')
4314 cpp_arg.path_prefix++;
4315 cpp_arg.len = strlen(cpp_arg.path_prefix);
4316 cpp_arg.errcode = errcode;
4317 err = got_diff_tree(tree1, tree2, "", "", repo,
4318 check_path_prefix_in_diff, &cpp_arg, 0);
4319 done:
4320 if (tree1)
4321 got_object_tree_close(tree1);
4322 if (tree2)
4323 got_object_tree_close(tree2);
4324 if (commit)
4325 got_object_commit_close(commit);
4326 if (parent_commit)
4327 got_object_commit_close(parent_commit);
4328 return err;
4331 static const struct got_error *
4332 collect_commits(struct got_object_id_queue *commits,
4333 struct got_object_id *initial_commit_id,
4334 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4335 const char *path_prefix, int path_prefix_errcode,
4336 struct got_repository *repo)
4338 const struct got_error *err = NULL;
4339 struct got_commit_graph *graph = NULL;
4340 struct got_object_id *parent_id = NULL;
4341 struct got_object_qid *qid;
4342 struct got_object_id *commit_id = initial_commit_id;
4344 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4345 if (err)
4346 return err;
4348 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4349 if (err)
4350 goto done;
4351 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4352 err = got_commit_graph_iter_next(&parent_id, graph);
4353 if (err) {
4354 if (err->code == GOT_ERR_ITER_COMPLETED) {
4355 err = got_error_msg(GOT_ERR_ANCESTRY,
4356 "ran out of commits to rebase before "
4357 "youngest common ancestor commit has "
4358 "been reached?!?");
4359 goto done;
4360 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4361 goto done;
4362 err = got_commit_graph_fetch_commits(graph, 1, repo);
4363 if (err)
4364 goto done;
4365 } else {
4366 err = check_path_prefix(parent_id, commit_id,
4367 path_prefix, path_prefix_errcode, repo);
4368 if (err)
4369 goto done;
4371 err = got_object_qid_alloc(&qid, commit_id);
4372 if (err)
4373 goto done;
4374 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4375 commit_id = parent_id;
4378 done:
4379 got_commit_graph_close(graph);
4380 return err;
4383 static const struct got_error *
4384 cmd_rebase(int argc, char *argv[])
4386 const struct got_error *error = NULL;
4387 struct got_worktree *worktree = NULL;
4388 struct got_repository *repo = NULL;
4389 struct got_fileindex *fileindex = NULL;
4390 char *cwd = NULL;
4391 struct got_reference *branch = NULL;
4392 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4393 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4394 struct got_object_id *resume_commit_id = NULL;
4395 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4396 struct got_commit_object *commit = NULL;
4397 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4398 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4399 struct got_object_id_queue commits;
4400 struct got_pathlist_head merged_paths;
4401 const struct got_object_id_queue *parent_ids;
4402 struct got_object_qid *qid, *pid;
4404 SIMPLEQ_INIT(&commits);
4405 TAILQ_INIT(&merged_paths);
4407 while ((ch = getopt(argc, argv, "ac")) != -1) {
4408 switch (ch) {
4409 case 'a':
4410 abort_rebase = 1;
4411 break;
4412 case 'c':
4413 continue_rebase = 1;
4414 break;
4415 default:
4416 usage_rebase();
4417 /* NOTREACHED */
4421 argc -= optind;
4422 argv += optind;
4424 #ifndef PROFILE
4425 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4426 "unveil", NULL) == -1)
4427 err(1, "pledge");
4428 #endif
4429 if (abort_rebase && continue_rebase)
4430 usage_rebase();
4431 else if (abort_rebase || continue_rebase) {
4432 if (argc != 0)
4433 usage_rebase();
4434 } else if (argc != 1)
4435 usage_rebase();
4437 cwd = getcwd(NULL, 0);
4438 if (cwd == NULL) {
4439 error = got_error_from_errno("getcwd");
4440 goto done;
4442 error = got_worktree_open(&worktree, cwd);
4443 if (error)
4444 goto done;
4446 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4447 if (error != NULL)
4448 goto done;
4450 error = apply_unveil(got_repo_get_path(repo), 0,
4451 got_worktree_get_root_path(worktree));
4452 if (error)
4453 goto done;
4455 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4456 if (error)
4457 goto done;
4459 if (abort_rebase) {
4460 int did_something;
4461 if (!rebase_in_progress) {
4462 error = got_error(GOT_ERR_NOT_REBASING);
4463 goto done;
4465 error = got_worktree_rebase_continue(&resume_commit_id,
4466 &new_base_branch, &tmp_branch, &branch, &fileindex,
4467 worktree, repo);
4468 if (error)
4469 goto done;
4470 printf("Switching work tree to %s\n",
4471 got_ref_get_symref_target(new_base_branch));
4472 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4473 new_base_branch, update_progress, &did_something);
4474 if (error)
4475 goto done;
4476 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4477 goto done; /* nothing else to do */
4480 if (continue_rebase) {
4481 if (!rebase_in_progress) {
4482 error = got_error(GOT_ERR_NOT_REBASING);
4483 goto done;
4485 error = got_worktree_rebase_continue(&resume_commit_id,
4486 &new_base_branch, &tmp_branch, &branch, &fileindex,
4487 worktree, repo);
4488 if (error)
4489 goto done;
4491 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4492 resume_commit_id, repo);
4493 if (error)
4494 goto done;
4496 yca_id = got_object_id_dup(resume_commit_id);
4497 if (yca_id == NULL) {
4498 error = got_error_from_errno("got_object_id_dup");
4499 goto done;
4501 } else {
4502 error = got_ref_open(&branch, repo, argv[0], 0);
4503 if (error != NULL)
4504 goto done;
4507 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4508 if (error)
4509 goto done;
4511 if (!continue_rebase) {
4512 struct got_object_id *base_commit_id;
4514 base_commit_id = got_worktree_get_base_commit_id(worktree);
4515 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4516 base_commit_id, branch_head_commit_id, repo);
4517 if (error)
4518 goto done;
4519 if (yca_id == NULL) {
4520 error = got_error_msg(GOT_ERR_ANCESTRY,
4521 "specified branch shares no common ancestry "
4522 "with work tree's branch");
4523 goto done;
4526 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4527 if (error) {
4528 if (error->code != GOT_ERR_ANCESTRY)
4529 goto done;
4530 error = NULL;
4531 } else {
4532 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4533 "specified branch resolves to a commit which "
4534 "is already contained in work tree's branch");
4535 goto done;
4537 error = got_worktree_rebase_prepare(&new_base_branch,
4538 &tmp_branch, &fileindex, worktree, branch, repo);
4539 if (error)
4540 goto done;
4543 commit_id = branch_head_commit_id;
4544 error = got_object_open_as_commit(&commit, repo, commit_id);
4545 if (error)
4546 goto done;
4548 parent_ids = got_object_commit_get_parent_ids(commit);
4549 pid = SIMPLEQ_FIRST(parent_ids);
4550 if (pid == NULL) {
4551 if (!continue_rebase) {
4552 int did_something;
4553 error = got_worktree_rebase_abort(worktree, fileindex,
4554 repo, new_base_branch, update_progress,
4555 &did_something);
4556 if (error)
4557 goto done;
4558 printf("Rebase of %s aborted\n",
4559 got_ref_get_name(branch));
4561 error = got_error(GOT_ERR_EMPTY_REBASE);
4562 goto done;
4564 error = collect_commits(&commits, commit_id, pid->id,
4565 yca_id, got_worktree_get_path_prefix(worktree),
4566 GOT_ERR_REBASE_PATH, repo);
4567 got_object_commit_close(commit);
4568 commit = NULL;
4569 if (error)
4570 goto done;
4572 if (SIMPLEQ_EMPTY(&commits)) {
4573 if (continue_rebase)
4574 error = rebase_complete(worktree, fileindex,
4575 branch, new_base_branch, tmp_branch, repo);
4576 else
4577 error = got_error(GOT_ERR_EMPTY_REBASE);
4578 goto done;
4581 pid = NULL;
4582 SIMPLEQ_FOREACH(qid, &commits, entry) {
4583 commit_id = qid->id;
4584 parent_id = pid ? pid->id : yca_id;
4585 pid = qid;
4587 error = got_worktree_rebase_merge_files(&merged_paths,
4588 worktree, fileindex, parent_id, commit_id, repo,
4589 rebase_progress, &rebase_status, check_cancelled, NULL);
4590 if (error)
4591 goto done;
4593 if (rebase_status == GOT_STATUS_CONFLICT) {
4594 got_worktree_rebase_pathlist_free(&merged_paths);
4595 break;
4598 error = rebase_commit(&merged_paths, worktree, fileindex,
4599 tmp_branch, commit_id, repo);
4600 got_worktree_rebase_pathlist_free(&merged_paths);
4601 if (error)
4602 goto done;
4605 if (rebase_status == GOT_STATUS_CONFLICT) {
4606 error = got_worktree_rebase_postpone(worktree, fileindex);
4607 if (error)
4608 goto done;
4609 error = got_error_msg(GOT_ERR_CONFLICTS,
4610 "conflicts must be resolved before rebasing can continue");
4611 } else
4612 error = rebase_complete(worktree, fileindex, branch,
4613 new_base_branch, tmp_branch, repo);
4614 done:
4615 got_object_id_queue_free(&commits);
4616 free(branch_head_commit_id);
4617 free(resume_commit_id);
4618 free(yca_id);
4619 if (commit)
4620 got_object_commit_close(commit);
4621 if (branch)
4622 got_ref_close(branch);
4623 if (new_base_branch)
4624 got_ref_close(new_base_branch);
4625 if (tmp_branch)
4626 got_ref_close(tmp_branch);
4627 if (worktree)
4628 got_worktree_close(worktree);
4629 if (repo)
4630 got_repo_close(repo);
4631 return error;
4634 __dead static void
4635 usage_histedit(void)
4637 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4638 getprogname());
4639 exit(1);
4642 #define GOT_HISTEDIT_PICK 'p'
4643 #define GOT_HISTEDIT_EDIT 'e'
4644 #define GOT_HISTEDIT_FOLD 'f'
4645 #define GOT_HISTEDIT_DROP 'd'
4646 #define GOT_HISTEDIT_MESG 'm'
4648 static struct got_histedit_cmd {
4649 unsigned char code;
4650 const char *name;
4651 const char *desc;
4652 } got_histedit_cmds[] = {
4653 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4654 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4655 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4656 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4657 { GOT_HISTEDIT_MESG, "mesg",
4658 "single-line log message for commit above (open editor if empty)" },
4661 struct got_histedit_list_entry {
4662 TAILQ_ENTRY(got_histedit_list_entry) entry;
4663 struct got_object_id *commit_id;
4664 const struct got_histedit_cmd *cmd;
4665 char *logmsg;
4667 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4669 static const struct got_error *
4670 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4671 FILE *f, struct got_repository *repo)
4673 const struct got_error *err = NULL;
4674 char *logmsg = NULL, *id_str = NULL;
4675 struct got_commit_object *commit = NULL;
4676 int n;
4678 err = got_object_open_as_commit(&commit, repo, commit_id);
4679 if (err)
4680 goto done;
4682 err = get_short_logmsg(&logmsg, 34, commit);
4683 if (err)
4684 goto done;
4686 err = got_object_id_str(&id_str, commit_id);
4687 if (err)
4688 goto done;
4690 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4691 if (n < 0)
4692 err = got_ferror(f, GOT_ERR_IO);
4693 done:
4694 if (commit)
4695 got_object_commit_close(commit);
4696 free(id_str);
4697 free(logmsg);
4698 return err;
4701 static const struct got_error *
4702 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4703 struct got_repository *repo)
4705 const struct got_error *err = NULL;
4706 struct got_object_qid *qid;
4708 if (SIMPLEQ_EMPTY(commits))
4709 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4711 SIMPLEQ_FOREACH(qid, commits, entry) {
4712 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4713 f, repo);
4714 if (err)
4715 break;
4718 return err;
4721 static const struct got_error *
4722 write_cmd_list(FILE *f)
4724 const struct got_error *err = NULL;
4725 int n, i;
4727 n = fprintf(f, "# Available histedit commands:\n");
4728 if (n < 0)
4729 return got_ferror(f, GOT_ERR_IO);
4731 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4732 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4733 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4734 cmd->desc);
4735 if (n < 0) {
4736 err = got_ferror(f, GOT_ERR_IO);
4737 break;
4740 n = fprintf(f, "# Commits will be processed in order from top to "
4741 "bottom of this file.\n");
4742 if (n < 0)
4743 return got_ferror(f, GOT_ERR_IO);
4744 return err;
4747 static const struct got_error *
4748 histedit_syntax_error(int lineno)
4750 static char msg[42];
4751 int ret;
4753 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4754 lineno);
4755 if (ret == -1 || ret >= sizeof(msg))
4756 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4758 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4761 static const struct got_error *
4762 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4763 char *logmsg, struct got_repository *repo)
4765 const struct got_error *err;
4766 struct got_commit_object *folded_commit = NULL;
4767 char *id_str, *folded_logmsg = NULL;
4769 err = got_object_id_str(&id_str, hle->commit_id);
4770 if (err)
4771 return err;
4773 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4774 if (err)
4775 goto done;
4777 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4778 if (err)
4779 goto done;
4780 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4781 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4782 folded_logmsg) == -1) {
4783 err = got_error_from_errno("asprintf");
4784 goto done;
4786 done:
4787 if (folded_commit)
4788 got_object_commit_close(folded_commit);
4789 free(id_str);
4790 free(folded_logmsg);
4791 return err;
4794 static struct got_histedit_list_entry *
4795 get_folded_commits(struct got_histedit_list_entry *hle)
4797 struct got_histedit_list_entry *prev, *folded = NULL;
4799 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4800 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4801 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4802 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4803 folded = prev;
4804 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4807 return folded;
4810 static const struct got_error *
4811 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4812 struct got_repository *repo)
4814 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4815 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4816 const struct got_error *err = NULL;
4817 struct got_commit_object *commit = NULL;
4818 int fd;
4819 struct got_histedit_list_entry *folded = NULL;
4821 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4822 if (err)
4823 return err;
4825 folded = get_folded_commits(hle);
4826 if (folded) {
4827 while (folded != hle) {
4828 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4829 folded = TAILQ_NEXT(folded, entry);
4830 continue;
4832 err = append_folded_commit_msg(&new_msg, folded,
4833 logmsg, repo);
4834 if (err)
4835 goto done;
4836 free(logmsg);
4837 logmsg = new_msg;
4838 folded = TAILQ_NEXT(folded, entry);
4842 err = got_object_id_str(&id_str, hle->commit_id);
4843 if (err)
4844 goto done;
4845 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4846 if (err)
4847 goto done;
4848 if (asprintf(&new_msg,
4849 "%s\n# original log message of commit %s: %s",
4850 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4851 err = got_error_from_errno("asprintf");
4852 goto done;
4854 free(logmsg);
4855 logmsg = new_msg;
4857 err = got_object_id_str(&id_str, hle->commit_id);
4858 if (err)
4859 goto done;
4861 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4862 if (err)
4863 goto done;
4865 dprintf(fd, logmsg);
4866 close(fd);
4868 err = get_editor(&editor);
4869 if (err)
4870 goto done;
4872 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4873 if (err) {
4874 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4875 goto done;
4876 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4878 done:
4879 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4880 err = got_error_from_errno2("unlink", logmsg_path);
4881 free(logmsg_path);
4882 free(logmsg);
4883 free(orig_logmsg);
4884 free(editor);
4885 if (commit)
4886 got_object_commit_close(commit);
4887 return err;
4890 static const struct got_error *
4891 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4892 FILE *f, struct got_repository *repo)
4894 const struct got_error *err = NULL;
4895 char *line = NULL, *p, *end;
4896 size_t size;
4897 ssize_t len;
4898 int lineno = 0, i;
4899 const struct got_histedit_cmd *cmd;
4900 struct got_object_id *commit_id = NULL;
4901 struct got_histedit_list_entry *hle = NULL;
4903 for (;;) {
4904 len = getline(&line, &size, f);
4905 if (len == -1) {
4906 const struct got_error *getline_err;
4907 if (feof(f))
4908 break;
4909 getline_err = got_error_from_errno("getline");
4910 err = got_ferror(f, getline_err->code);
4911 break;
4913 lineno++;
4914 p = line;
4915 while (isspace((unsigned char)p[0]))
4916 p++;
4917 if (p[0] == '#' || p[0] == '\0') {
4918 free(line);
4919 line = NULL;
4920 continue;
4922 cmd = NULL;
4923 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4924 cmd = &got_histedit_cmds[i];
4925 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4926 isspace((unsigned char)p[strlen(cmd->name)])) {
4927 p += strlen(cmd->name);
4928 break;
4930 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4931 p++;
4932 break;
4935 if (i == nitems(got_histedit_cmds)) {
4936 err = histedit_syntax_error(lineno);
4937 break;
4939 while (isspace((unsigned char)p[0]))
4940 p++;
4941 if (cmd->code == GOT_HISTEDIT_MESG) {
4942 if (hle == NULL || hle->logmsg != NULL) {
4943 err = got_error(GOT_ERR_HISTEDIT_CMD);
4944 break;
4946 if (p[0] == '\0') {
4947 err = histedit_edit_logmsg(hle, repo);
4948 if (err)
4949 break;
4950 } else {
4951 hle->logmsg = strdup(p);
4952 if (hle->logmsg == NULL) {
4953 err = got_error_from_errno("strdup");
4954 break;
4957 free(line);
4958 line = NULL;
4959 continue;
4960 } else {
4961 end = p;
4962 while (end[0] && !isspace((unsigned char)end[0]))
4963 end++;
4964 *end = '\0';
4966 err = got_object_resolve_id_str(&commit_id, repo, p);
4967 if (err) {
4968 /* override error code */
4969 err = histedit_syntax_error(lineno);
4970 break;
4973 hle = malloc(sizeof(*hle));
4974 if (hle == NULL) {
4975 err = got_error_from_errno("malloc");
4976 break;
4978 hle->cmd = cmd;
4979 hle->commit_id = commit_id;
4980 hle->logmsg = NULL;
4981 commit_id = NULL;
4982 free(line);
4983 line = NULL;
4984 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4987 free(line);
4988 free(commit_id);
4989 return err;
4992 static const struct got_error *
4993 histedit_check_script(struct got_histedit_list *histedit_cmds,
4994 struct got_object_id_queue *commits, struct got_repository *repo)
4996 const struct got_error *err = NULL;
4997 struct got_object_qid *qid;
4998 struct got_histedit_list_entry *hle;
4999 static char msg[80];
5000 char *id_str;
5002 if (TAILQ_EMPTY(histedit_cmds))
5003 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5004 "histedit script contains no commands");
5005 if (SIMPLEQ_EMPTY(commits))
5006 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5008 SIMPLEQ_FOREACH(qid, commits, entry) {
5009 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5010 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5011 break;
5013 if (hle == NULL) {
5014 err = got_object_id_str(&id_str, qid->id);
5015 if (err)
5016 return err;
5017 snprintf(msg, sizeof(msg),
5018 "commit %s missing from histedit script", id_str);
5019 free(id_str);
5020 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5024 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5025 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5026 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5027 "last commit in histedit script cannot be folded");
5029 return NULL;
5032 static const struct got_error *
5033 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5034 const char *path, struct got_object_id_queue *commits,
5035 struct got_repository *repo)
5037 const struct got_error *err = NULL;
5038 char *editor;
5039 FILE *f = NULL;
5041 err = get_editor(&editor);
5042 if (err)
5043 return err;
5045 if (spawn_editor(editor, path) == -1) {
5046 err = got_error_from_errno("failed spawning editor");
5047 goto done;
5050 f = fopen(path, "r");
5051 if (f == NULL) {
5052 err = got_error_from_errno("fopen");
5053 goto done;
5055 err = histedit_parse_list(histedit_cmds, f, repo);
5056 if (err)
5057 goto done;
5059 err = histedit_check_script(histedit_cmds, commits, repo);
5060 done:
5061 if (f && fclose(f) != 0 && err == NULL)
5062 err = got_error_from_errno("fclose");
5063 free(editor);
5064 return err;
5067 static const struct got_error *
5068 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5069 struct got_object_id_queue *, const char *, struct got_repository *);
5071 static const struct got_error *
5072 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5073 struct got_object_id_queue *commits, struct got_repository *repo)
5075 const struct got_error *err;
5076 FILE *f = NULL;
5077 char *path = NULL;
5079 err = got_opentemp_named(&path, &f, "got-histedit");
5080 if (err)
5081 return err;
5083 err = write_cmd_list(f);
5084 if (err)
5085 goto done;
5087 err = histedit_write_commit_list(commits, f, repo);
5088 if (err)
5089 goto done;
5091 if (fclose(f) != 0) {
5092 err = got_error_from_errno("fclose");
5093 goto done;
5095 f = NULL;
5097 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5098 if (err) {
5099 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5100 err->code != GOT_ERR_HISTEDIT_CMD)
5101 goto done;
5102 err = histedit_edit_list_retry(histedit_cmds, err,
5103 commits, path, repo);
5105 done:
5106 if (f && fclose(f) != 0 && err == NULL)
5107 err = got_error_from_errno("fclose");
5108 if (path && unlink(path) != 0 && err == NULL)
5109 err = got_error_from_errno2("unlink", path);
5110 free(path);
5111 return err;
5114 static const struct got_error *
5115 histedit_save_list(struct got_histedit_list *histedit_cmds,
5116 struct got_worktree *worktree, struct got_repository *repo)
5118 const struct got_error *err = NULL;
5119 char *path = NULL;
5120 FILE *f = NULL;
5121 struct got_histedit_list_entry *hle;
5122 struct got_commit_object *commit = NULL;
5124 err = got_worktree_get_histedit_script_path(&path, worktree);
5125 if (err)
5126 return err;
5128 f = fopen(path, "w");
5129 if (f == NULL) {
5130 err = got_error_from_errno2("fopen", path);
5131 goto done;
5133 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5134 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5135 repo);
5136 if (err)
5137 break;
5139 if (hle->logmsg) {
5140 int n = fprintf(f, "%c %s\n",
5141 GOT_HISTEDIT_MESG, hle->logmsg);
5142 if (n < 0) {
5143 err = got_ferror(f, GOT_ERR_IO);
5144 break;
5148 done:
5149 if (f && fclose(f) != 0 && err == NULL)
5150 err = got_error_from_errno("fclose");
5151 free(path);
5152 if (commit)
5153 got_object_commit_close(commit);
5154 return err;
5157 void
5158 histedit_free_list(struct got_histedit_list *histedit_cmds)
5160 struct got_histedit_list_entry *hle;
5162 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5163 TAILQ_REMOVE(histedit_cmds, hle, entry);
5164 free(hle);
5168 static const struct got_error *
5169 histedit_load_list(struct got_histedit_list *histedit_cmds,
5170 const char *path, struct got_repository *repo)
5172 const struct got_error *err = NULL;
5173 FILE *f = NULL;
5175 f = fopen(path, "r");
5176 if (f == NULL) {
5177 err = got_error_from_errno2("fopen", path);
5178 goto done;
5181 err = histedit_parse_list(histedit_cmds, f, repo);
5182 done:
5183 if (f && fclose(f) != 0 && err == NULL)
5184 err = got_error_from_errno("fclose");
5185 return err;
5188 static const struct got_error *
5189 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5190 const struct got_error *edit_err, struct got_object_id_queue *commits,
5191 const char *path, struct got_repository *repo)
5193 const struct got_error *err = NULL, *prev_err = edit_err;
5194 int resp = ' ';
5196 while (resp != 'c' && resp != 'r' && resp != 'a') {
5197 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5198 "or (a)bort: ", getprogname(), prev_err->msg);
5199 resp = getchar();
5200 if (resp == '\n')
5201 resp = getchar();
5202 if (resp == 'c') {
5203 histedit_free_list(histedit_cmds);
5204 err = histedit_run_editor(histedit_cmds, path, commits,
5205 repo);
5206 if (err) {
5207 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5208 err->code != GOT_ERR_HISTEDIT_CMD)
5209 break;
5210 prev_err = err;
5211 resp = ' ';
5212 continue;
5214 break;
5215 } else if (resp == 'r') {
5216 histedit_free_list(histedit_cmds);
5217 err = histedit_edit_script(histedit_cmds,
5218 commits, repo);
5219 if (err) {
5220 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5221 err->code != GOT_ERR_HISTEDIT_CMD)
5222 break;
5223 prev_err = err;
5224 resp = ' ';
5225 continue;
5227 break;
5228 } else if (resp == 'a') {
5229 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5230 break;
5231 } else
5232 printf("invalid response '%c'\n", resp);
5235 return err;
5238 static const struct got_error *
5239 histedit_complete(struct got_worktree *worktree,
5240 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5241 struct got_reference *branch, struct got_repository *repo)
5243 printf("Switching work tree to %s\n",
5244 got_ref_get_symref_target(branch));
5245 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5246 branch, repo);
5249 static const struct got_error *
5250 show_histedit_progress(struct got_commit_object *commit,
5251 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5253 const struct got_error *err;
5254 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5256 err = got_object_id_str(&old_id_str, hle->commit_id);
5257 if (err)
5258 goto done;
5260 if (new_id) {
5261 err = got_object_id_str(&new_id_str, new_id);
5262 if (err)
5263 goto done;
5266 old_id_str[12] = '\0';
5267 if (new_id_str)
5268 new_id_str[12] = '\0';
5270 if (hle->logmsg) {
5271 logmsg = strdup(hle->logmsg);
5272 if (logmsg == NULL) {
5273 err = got_error_from_errno("strdup");
5274 goto done;
5276 trim_logmsg(logmsg, 42);
5277 } else {
5278 err = get_short_logmsg(&logmsg, 42, commit);
5279 if (err)
5280 goto done;
5283 switch (hle->cmd->code) {
5284 case GOT_HISTEDIT_PICK:
5285 case GOT_HISTEDIT_EDIT:
5286 printf("%s -> %s: %s\n", old_id_str,
5287 new_id_str ? new_id_str : "no-op change", logmsg);
5288 break;
5289 case GOT_HISTEDIT_DROP:
5290 case GOT_HISTEDIT_FOLD:
5291 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5292 logmsg);
5293 break;
5294 default:
5295 break;
5298 done:
5299 free(old_id_str);
5300 free(new_id_str);
5301 return err;
5304 static const struct got_error *
5305 histedit_commit(struct got_pathlist_head *merged_paths,
5306 struct got_worktree *worktree, struct got_fileindex *fileindex,
5307 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5308 struct got_repository *repo)
5310 const struct got_error *err;
5311 struct got_commit_object *commit;
5312 struct got_object_id *new_commit_id;
5314 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5315 && hle->logmsg == NULL) {
5316 err = histedit_edit_logmsg(hle, repo);
5317 if (err)
5318 return err;
5321 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5322 if (err)
5323 return err;
5325 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5326 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5327 hle->logmsg, repo);
5328 if (err) {
5329 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5330 goto done;
5331 err = show_histedit_progress(commit, hle, NULL);
5332 } else {
5333 err = show_histedit_progress(commit, hle, new_commit_id);
5334 free(new_commit_id);
5336 done:
5337 got_object_commit_close(commit);
5338 return err;
5341 static const struct got_error *
5342 histedit_skip_commit(struct got_histedit_list_entry *hle,
5343 struct got_worktree *worktree, struct got_repository *repo)
5345 const struct got_error *error;
5346 struct got_commit_object *commit;
5348 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5349 repo);
5350 if (error)
5351 return error;
5353 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5354 if (error)
5355 return error;
5357 error = show_histedit_progress(commit, hle, NULL);
5358 got_object_commit_close(commit);
5359 return error;
5362 static const struct got_error *
5363 cmd_histedit(int argc, char *argv[])
5365 const struct got_error *error = NULL;
5366 struct got_worktree *worktree = NULL;
5367 struct got_fileindex *fileindex = NULL;
5368 struct got_repository *repo = NULL;
5369 char *cwd = NULL;
5370 struct got_reference *branch = NULL;
5371 struct got_reference *tmp_branch = NULL;
5372 struct got_object_id *resume_commit_id = NULL;
5373 struct got_object_id *base_commit_id = NULL;
5374 struct got_object_id *head_commit_id = NULL;
5375 struct got_commit_object *commit = NULL;
5376 int ch, rebase_in_progress = 0, did_something;
5377 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5378 const char *edit_script_path = NULL;
5379 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5380 struct got_object_id_queue commits;
5381 struct got_pathlist_head merged_paths;
5382 const struct got_object_id_queue *parent_ids;
5383 struct got_object_qid *pid;
5384 struct got_histedit_list histedit_cmds;
5385 struct got_histedit_list_entry *hle;
5387 SIMPLEQ_INIT(&commits);
5388 TAILQ_INIT(&histedit_cmds);
5389 TAILQ_INIT(&merged_paths);
5391 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5392 switch (ch) {
5393 case 'a':
5394 abort_edit = 1;
5395 break;
5396 case 'c':
5397 continue_edit = 1;
5398 break;
5399 case 'F':
5400 edit_script_path = optarg;
5401 break;
5402 default:
5403 usage_histedit();
5404 /* NOTREACHED */
5408 argc -= optind;
5409 argv += optind;
5411 #ifndef PROFILE
5412 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5413 "unveil", NULL) == -1)
5414 err(1, "pledge");
5415 #endif
5416 if (abort_edit && continue_edit)
5417 usage_histedit();
5418 if (argc != 0)
5419 usage_histedit();
5422 * This command cannot apply unveil(2) in all cases because the
5423 * user may choose to run an editor to edit the histedit script
5424 * and to edit individual commit log messages.
5425 * unveil(2) traverses exec(2); if an editor is used we have to
5426 * apply unveil after edit script and log messages have been written.
5427 * XXX TODO: Make use of unveil(2) where possible.
5430 cwd = getcwd(NULL, 0);
5431 if (cwd == NULL) {
5432 error = got_error_from_errno("getcwd");
5433 goto done;
5435 error = got_worktree_open(&worktree, cwd);
5436 if (error)
5437 goto done;
5439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5440 if (error != NULL)
5441 goto done;
5443 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5444 if (error)
5445 goto done;
5446 if (rebase_in_progress) {
5447 error = got_error(GOT_ERR_REBASING);
5448 goto done;
5451 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5452 if (error)
5453 goto done;
5455 if (edit_in_progress && abort_edit) {
5456 error = got_worktree_histedit_continue(&resume_commit_id,
5457 &tmp_branch, &branch, &base_commit_id, &fileindex,
5458 worktree, repo);
5459 if (error)
5460 goto done;
5461 printf("Switching work tree to %s\n",
5462 got_ref_get_symref_target(branch));
5463 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5464 branch, base_commit_id, update_progress, &did_something);
5465 if (error)
5466 goto done;
5467 printf("Histedit of %s aborted\n",
5468 got_ref_get_symref_target(branch));
5469 goto done; /* nothing else to do */
5470 } else if (abort_edit) {
5471 error = got_error(GOT_ERR_NOT_HISTEDIT);
5472 goto done;
5475 if (continue_edit) {
5476 char *path;
5478 if (!edit_in_progress) {
5479 error = got_error(GOT_ERR_NOT_HISTEDIT);
5480 goto done;
5483 error = got_worktree_get_histedit_script_path(&path, worktree);
5484 if (error)
5485 goto done;
5487 error = histedit_load_list(&histedit_cmds, path, repo);
5488 free(path);
5489 if (error)
5490 goto done;
5492 error = got_worktree_histedit_continue(&resume_commit_id,
5493 &tmp_branch, &branch, &base_commit_id, &fileindex,
5494 worktree, repo);
5495 if (error)
5496 goto done;
5498 error = got_ref_resolve(&head_commit_id, repo, branch);
5499 if (error)
5500 goto done;
5502 error = got_object_open_as_commit(&commit, repo,
5503 head_commit_id);
5504 if (error)
5505 goto done;
5506 parent_ids = got_object_commit_get_parent_ids(commit);
5507 pid = SIMPLEQ_FIRST(parent_ids);
5508 if (pid == NULL) {
5509 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5510 goto done;
5512 error = collect_commits(&commits, head_commit_id, pid->id,
5513 base_commit_id, got_worktree_get_path_prefix(worktree),
5514 GOT_ERR_HISTEDIT_PATH, repo);
5515 got_object_commit_close(commit);
5516 commit = NULL;
5517 if (error)
5518 goto done;
5519 } else {
5520 if (edit_in_progress) {
5521 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5522 goto done;
5525 error = got_ref_open(&branch, repo,
5526 got_worktree_get_head_ref_name(worktree), 0);
5527 if (error != NULL)
5528 goto done;
5530 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5531 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5532 "will not edit commit history of a branch outside "
5533 "the \"refs/heads/\" reference namespace");
5534 goto done;
5537 error = got_ref_resolve(&head_commit_id, repo, branch);
5538 got_ref_close(branch);
5539 branch = NULL;
5540 if (error)
5541 goto done;
5543 error = got_object_open_as_commit(&commit, repo,
5544 head_commit_id);
5545 if (error)
5546 goto done;
5547 parent_ids = got_object_commit_get_parent_ids(commit);
5548 pid = SIMPLEQ_FIRST(parent_ids);
5549 if (pid == NULL) {
5550 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5551 goto done;
5553 error = collect_commits(&commits, head_commit_id, pid->id,
5554 got_worktree_get_base_commit_id(worktree),
5555 got_worktree_get_path_prefix(worktree),
5556 GOT_ERR_HISTEDIT_PATH, repo);
5557 got_object_commit_close(commit);
5558 commit = NULL;
5559 if (error)
5560 goto done;
5562 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5563 &base_commit_id, &fileindex, worktree, repo);
5564 if (error)
5565 goto done;
5567 if (edit_script_path) {
5568 error = histedit_load_list(&histedit_cmds,
5569 edit_script_path, repo);
5570 if (error) {
5571 got_worktree_histedit_abort(worktree, fileindex,
5572 repo, branch, base_commit_id,
5573 update_progress, &did_something);
5574 goto done;
5576 } else {
5577 error = histedit_edit_script(&histedit_cmds, &commits,
5578 repo);
5579 if (error) {
5580 got_worktree_histedit_abort(worktree, fileindex,
5581 repo, branch, base_commit_id,
5582 update_progress, &did_something);
5583 goto done;
5588 error = histedit_save_list(&histedit_cmds, worktree,
5589 repo);
5590 if (error) {
5591 got_worktree_histedit_abort(worktree, fileindex,
5592 repo, branch, base_commit_id,
5593 update_progress, &did_something);
5594 goto done;
5599 error = histedit_check_script(&histedit_cmds, &commits, repo);
5600 if (error)
5601 goto done;
5603 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5604 if (resume_commit_id) {
5605 if (got_object_id_cmp(hle->commit_id,
5606 resume_commit_id) != 0)
5607 continue;
5609 resume_commit_id = NULL;
5610 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5611 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5612 error = histedit_skip_commit(hle, worktree,
5613 repo);
5614 } else {
5615 error = histedit_commit(NULL, worktree,
5616 fileindex, tmp_branch, hle, repo);
5618 if (error)
5619 goto done;
5620 continue;
5623 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5624 error = histedit_skip_commit(hle, worktree, repo);
5625 if (error)
5626 goto done;
5627 continue;
5630 error = got_object_open_as_commit(&commit, repo,
5631 hle->commit_id);
5632 if (error)
5633 goto done;
5634 parent_ids = got_object_commit_get_parent_ids(commit);
5635 pid = SIMPLEQ_FIRST(parent_ids);
5637 error = got_worktree_histedit_merge_files(&merged_paths,
5638 worktree, fileindex, pid->id, hle->commit_id, repo,
5639 rebase_progress, &rebase_status, check_cancelled, NULL);
5640 if (error)
5641 goto done;
5642 got_object_commit_close(commit);
5643 commit = NULL;
5645 if (rebase_status == GOT_STATUS_CONFLICT) {
5646 got_worktree_rebase_pathlist_free(&merged_paths);
5647 break;
5650 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5651 char *id_str;
5652 error = got_object_id_str(&id_str, hle->commit_id);
5653 if (error)
5654 goto done;
5655 printf("Stopping histedit for amending commit %s\n",
5656 id_str);
5657 free(id_str);
5658 got_worktree_rebase_pathlist_free(&merged_paths);
5659 error = got_worktree_histedit_postpone(worktree,
5660 fileindex);
5661 goto done;
5664 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5665 error = histedit_skip_commit(hle, worktree, repo);
5666 if (error)
5667 goto done;
5668 continue;
5671 error = histedit_commit(&merged_paths, worktree, fileindex,
5672 tmp_branch, hle, repo);
5673 got_worktree_rebase_pathlist_free(&merged_paths);
5674 if (error)
5675 goto done;
5678 if (rebase_status == GOT_STATUS_CONFLICT) {
5679 error = got_worktree_histedit_postpone(worktree, fileindex);
5680 if (error)
5681 goto done;
5682 error = got_error_msg(GOT_ERR_CONFLICTS,
5683 "conflicts must be resolved before rebasing can continue");
5684 } else
5685 error = histedit_complete(worktree, fileindex, tmp_branch,
5686 branch, repo);
5687 done:
5688 got_object_id_queue_free(&commits);
5689 histedit_free_list(&histedit_cmds);
5690 free(head_commit_id);
5691 free(base_commit_id);
5692 free(resume_commit_id);
5693 if (commit)
5694 got_object_commit_close(commit);
5695 if (branch)
5696 got_ref_close(branch);
5697 if (tmp_branch)
5698 got_ref_close(tmp_branch);
5699 if (worktree)
5700 got_worktree_close(worktree);
5701 if (repo)
5702 got_repo_close(repo);
5703 return error;
5706 __dead static void
5707 usage_stage(void)
5709 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5710 "[file-path ...]\n",
5711 getprogname());
5712 exit(1);
5715 static const struct got_error *
5716 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5717 const char *path, struct got_object_id *blob_id,
5718 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5720 const struct got_error *err = NULL;
5721 char *id_str = NULL;
5723 if (staged_status != GOT_STATUS_ADD &&
5724 staged_status != GOT_STATUS_MODIFY &&
5725 staged_status != GOT_STATUS_DELETE)
5726 return NULL;
5728 if (staged_status == GOT_STATUS_ADD ||
5729 staged_status == GOT_STATUS_MODIFY)
5730 err = got_object_id_str(&id_str, staged_blob_id);
5731 else
5732 err = got_object_id_str(&id_str, blob_id);
5733 if (err)
5734 return err;
5736 printf("%s %c %s\n", id_str, staged_status, path);
5737 free(id_str);
5738 return NULL;
5741 static const struct got_error *
5742 cmd_stage(int argc, char *argv[])
5744 const struct got_error *error = NULL;
5745 struct got_repository *repo = NULL;
5746 struct got_worktree *worktree = NULL;
5747 char *cwd = NULL;
5748 struct got_pathlist_head paths;
5749 struct got_pathlist_entry *pe;
5750 int ch, list_stage = 0, pflag = 0;
5751 FILE *patch_script_file = NULL;
5752 const char *patch_script_path = NULL;
5753 struct choose_patch_arg cpa;
5755 TAILQ_INIT(&paths);
5757 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5758 switch (ch) {
5759 case 'l':
5760 list_stage = 1;
5761 break;
5762 case 'p':
5763 pflag = 1;
5764 break;
5765 case 'F':
5766 patch_script_path = optarg;
5767 break;
5768 default:
5769 usage_stage();
5770 /* NOTREACHED */
5774 argc -= optind;
5775 argv += optind;
5777 #ifndef PROFILE
5778 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5779 "unveil", NULL) == -1)
5780 err(1, "pledge");
5781 #endif
5782 if (list_stage && (pflag || patch_script_path))
5783 errx(1, "-l option cannot be used with other options");
5784 if (patch_script_path && !pflag)
5785 errx(1, "-F option can only be used together with -p option");
5787 cwd = getcwd(NULL, 0);
5788 if (cwd == NULL) {
5789 error = got_error_from_errno("getcwd");
5790 goto done;
5793 error = got_worktree_open(&worktree, cwd);
5794 if (error)
5795 goto done;
5797 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5798 if (error != NULL)
5799 goto done;
5801 if (patch_script_path) {
5802 patch_script_file = fopen(patch_script_path, "r");
5803 if (patch_script_file == NULL) {
5804 error = got_error_from_errno2("fopen",
5805 patch_script_path);
5806 goto done;
5809 error = apply_unveil(got_repo_get_path(repo), 1,
5810 got_worktree_get_root_path(worktree));
5811 if (error)
5812 goto done;
5814 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5815 if (error)
5816 goto done;
5818 if (list_stage)
5819 error = got_worktree_status(worktree, &paths, repo,
5820 print_stage, NULL, check_cancelled, NULL);
5821 else {
5822 cpa.patch_script_file = patch_script_file;
5823 cpa.action = "stage";
5824 error = got_worktree_stage(worktree, &paths,
5825 pflag ? NULL : print_status, NULL,
5826 pflag ? choose_patch : NULL, &cpa, repo);
5828 done:
5829 if (patch_script_file && fclose(patch_script_file) == EOF &&
5830 error == NULL)
5831 error = got_error_from_errno2("fclose", patch_script_path);
5832 if (repo)
5833 got_repo_close(repo);
5834 if (worktree)
5835 got_worktree_close(worktree);
5836 TAILQ_FOREACH(pe, &paths, entry)
5837 free((char *)pe->path);
5838 got_pathlist_free(&paths);
5839 free(cwd);
5840 return error;
5843 __dead static void
5844 usage_unstage(void)
5846 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5847 "[file-path ...]\n",
5848 getprogname());
5849 exit(1);
5853 static const struct got_error *
5854 cmd_unstage(int argc, char *argv[])
5856 const struct got_error *error = NULL;
5857 struct got_repository *repo = NULL;
5858 struct got_worktree *worktree = NULL;
5859 char *cwd = NULL;
5860 struct got_pathlist_head paths;
5861 struct got_pathlist_entry *pe;
5862 int ch, did_something = 0, pflag = 0;
5863 FILE *patch_script_file = NULL;
5864 const char *patch_script_path = NULL;
5865 struct choose_patch_arg cpa;
5867 TAILQ_INIT(&paths);
5869 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5870 switch (ch) {
5871 case 'p':
5872 pflag = 1;
5873 break;
5874 case 'F':
5875 patch_script_path = optarg;
5876 break;
5877 default:
5878 usage_unstage();
5879 /* NOTREACHED */
5883 argc -= optind;
5884 argv += optind;
5886 #ifndef PROFILE
5887 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5888 "unveil", NULL) == -1)
5889 err(1, "pledge");
5890 #endif
5891 if (patch_script_path && !pflag)
5892 errx(1, "-F option can only be used together with -p option");
5894 cwd = getcwd(NULL, 0);
5895 if (cwd == NULL) {
5896 error = got_error_from_errno("getcwd");
5897 goto done;
5900 error = got_worktree_open(&worktree, cwd);
5901 if (error)
5902 goto done;
5904 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5905 if (error != NULL)
5906 goto done;
5908 if (patch_script_path) {
5909 patch_script_file = fopen(patch_script_path, "r");
5910 if (patch_script_file == NULL) {
5911 error = got_error_from_errno2("fopen",
5912 patch_script_path);
5913 goto done;
5917 error = apply_unveil(got_repo_get_path(repo), 1,
5918 got_worktree_get_root_path(worktree));
5919 if (error)
5920 goto done;
5922 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5923 if (error)
5924 goto done;
5926 cpa.patch_script_file = patch_script_file;
5927 cpa.action = "unstage";
5928 error = got_worktree_unstage(worktree, &paths, update_progress,
5929 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5930 done:
5931 if (patch_script_file && fclose(patch_script_file) == EOF &&
5932 error == NULL)
5933 error = got_error_from_errno2("fclose", patch_script_path);
5934 if (repo)
5935 got_repo_close(repo);
5936 if (worktree)
5937 got_worktree_close(worktree);
5938 TAILQ_FOREACH(pe, &paths, entry)
5939 free((char *)pe->path);
5940 got_pathlist_free(&paths);
5941 free(cwd);
5942 return error;