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_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_add(void);
92 __dead static void usage_remove(void);
93 __dead static void usage_revert(void);
94 __dead static void usage_commit(void);
95 __dead static void usage_cherrypick(void);
96 __dead static void usage_backout(void);
97 __dead static void usage_rebase(void);
98 __dead static void usage_histedit(void);
99 __dead static void usage_stage(void);
100 __dead static void usage_unstage(void);
102 static const struct got_error* cmd_init(int, char *[]);
103 static const struct got_error* cmd_import(int, char *[]);
104 static const struct got_error* cmd_checkout(int, char *[]);
105 static const struct got_error* cmd_update(int, char *[]);
106 static const struct got_error* cmd_log(int, char *[]);
107 static const struct got_error* cmd_diff(int, char *[]);
108 static const struct got_error* cmd_blame(int, char *[]);
109 static const struct got_error* cmd_tree(int, char *[]);
110 static const struct got_error* cmd_status(int, char *[]);
111 static const struct got_error* cmd_ref(int, char *[]);
112 static const struct got_error* cmd_branch(int, char *[]);
113 static const struct got_error* cmd_add(int, char *[]);
114 static const struct got_error* cmd_remove(int, char *[]);
115 static const struct got_error* cmd_revert(int, char *[]);
116 static const struct got_error* cmd_commit(int, char *[]);
117 static const struct got_error* cmd_cherrypick(int, char *[]);
118 static const struct got_error* cmd_backout(int, char *[]);
119 static const struct got_error* cmd_rebase(int, char *[]);
120 static const struct got_error* cmd_histedit(int, char *[]);
121 static const struct got_error* cmd_stage(int, char *[]);
122 static const struct got_error* cmd_unstage(int, char *[]);
124 static struct got_cmd got_commands[] = {
125 { "init", cmd_init, usage_init, "in" },
126 { "import", cmd_import, usage_import, "im" },
127 { "checkout", cmd_checkout, usage_checkout, "co" },
128 { "update", cmd_update, usage_update, "up" },
129 { "log", cmd_log, usage_log, "" },
130 { "diff", cmd_diff, usage_diff, "di" },
131 { "blame", cmd_blame, usage_blame, "bl" },
132 { "tree", cmd_tree, usage_tree, "tr" },
133 { "status", cmd_status, usage_status, "st" },
134 { "ref", cmd_ref, usage_ref, "" },
135 { "branch", cmd_branch, usage_branch, "br" },
136 { "add", cmd_add, usage_add, "" },
137 { "remove", cmd_remove, usage_remove, "rm" },
138 { "revert", cmd_revert, usage_revert, "rv" },
139 { "commit", cmd_commit, usage_commit, "ci" },
140 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
141 { "backout", cmd_backout, usage_backout, "bo" },
142 { "rebase", cmd_rebase, usage_rebase, "rb" },
143 { "histedit", cmd_histedit, usage_histedit, "he" },
144 { "stage", cmd_stage, usage_stage, "sg" },
145 { "unstage", cmd_unstage, usage_unstage, "ug" },
146 };
148 static void
149 list_commands(void)
151 int i;
153 fprintf(stderr, "commands:");
154 for (i = 0; i < nitems(got_commands); i++) {
155 struct got_cmd *cmd = &got_commands[i];
156 fprintf(stderr, " %s", cmd->cmd_name);
158 fputc('\n', stderr);
161 int
162 main(int argc, char *argv[])
164 struct got_cmd *cmd;
165 unsigned int i;
166 int ch;
167 int hflag = 0, Vflag = 0;
169 setlocale(LC_CTYPE, "");
171 while ((ch = getopt(argc, argv, "hV")) != -1) {
172 switch (ch) {
173 case 'h':
174 hflag = 1;
175 break;
176 case 'V':
177 Vflag = 1;
178 break;
179 default:
180 usage(hflag);
181 /* NOTREACHED */
185 argc -= optind;
186 argv += optind;
187 optind = 0;
189 if (Vflag) {
190 got_version_print_str();
191 return 1;
194 if (argc <= 0)
195 usage(hflag);
197 signal(SIGINT, catch_sigint);
198 signal(SIGPIPE, catch_sigpipe);
200 for (i = 0; i < nitems(got_commands); i++) {
201 const struct got_error *error;
203 cmd = &got_commands[i];
205 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
206 strcmp(cmd->cmd_alias, argv[0]) != 0)
207 continue;
209 if (hflag)
210 got_commands[i].cmd_usage();
212 error = got_commands[i].cmd_main(argc, argv);
213 if (error && !(sigint_received || sigpipe_received)) {
214 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
215 return 1;
218 return 0;
221 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
222 list_commands();
223 return 1;
226 __dead static void
227 usage(int hflag)
229 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
230 getprogname());
231 if (hflag)
232 list_commands();
233 exit(1);
236 static const struct got_error *
237 get_editor(char **abspath)
239 const struct got_error *err = NULL;
240 const char *editor;
242 *abspath = NULL;
244 editor = getenv("VISUAL");
245 if (editor == NULL)
246 editor = getenv("EDITOR");
248 if (editor) {
249 err = got_path_find_prog(abspath, editor);
250 if (err)
251 return err;
254 if (*abspath == NULL) {
255 *abspath = strdup("/bin/ed");
256 if (*abspath == NULL)
257 return got_error_from_errno("strdup");
260 return NULL;
263 static const struct got_error *
264 apply_unveil(const char *repo_path, int repo_read_only,
265 const char *worktree_path)
267 const struct got_error *err;
269 #ifdef PROFILE
270 if (unveil("gmon.out", "rwc") != 0)
271 return got_error_from_errno2("unveil", "gmon.out");
272 #endif
273 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
274 return got_error_from_errno2("unveil", repo_path);
276 if (worktree_path && unveil(worktree_path, "rwc") != 0)
277 return got_error_from_errno2("unveil", worktree_path);
279 if (unveil("/tmp", "rwc") != 0)
280 return got_error_from_errno2("unveil", "/tmp");
282 err = got_privsep_unveil_exec_helpers();
283 if (err != NULL)
284 return err;
286 if (unveil(NULL, NULL) != 0)
287 return got_error_from_errno("unveil");
289 return NULL;
292 __dead static void
293 usage_init(void)
295 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
296 exit(1);
299 static const struct got_error *
300 cmd_init(int argc, char *argv[])
302 const struct got_error *error = NULL;
303 char *repo_path = NULL;
304 int ch;
306 while ((ch = getopt(argc, argv, "")) != -1) {
307 switch (ch) {
308 default:
309 usage_init();
310 /* NOTREACHED */
314 argc -= optind;
315 argv += optind;
317 #ifndef PROFILE
318 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
319 err(1, "pledge");
320 #endif
321 if (argc != 1)
322 usage_init();
324 repo_path = strdup(argv[0]);
325 if (repo_path == NULL)
326 return got_error_from_errno("strdup");
328 got_path_strip_trailing_slashes(repo_path);
330 error = got_path_mkdir(repo_path);
331 if (error &&
332 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
333 goto done;
335 error = apply_unveil(repo_path, 0, NULL);
336 if (error)
337 goto done;
339 error = got_repo_init(repo_path);
340 if (error != NULL)
341 goto done;
343 done:
344 free(repo_path);
345 return error;
348 __dead static void
349 usage_import(void)
351 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
352 "[-r repository-path] [-I pattern] path\n", getprogname());
353 exit(1);
356 int
357 spawn_editor(const char *editor, const char *file)
359 pid_t pid;
360 sig_t sighup, sigint, sigquit;
361 int st = -1;
363 sighup = signal(SIGHUP, SIG_IGN);
364 sigint = signal(SIGINT, SIG_IGN);
365 sigquit = signal(SIGQUIT, SIG_IGN);
367 switch (pid = fork()) {
368 case -1:
369 goto doneediting;
370 case 0:
371 execl(editor, editor, file, (char *)NULL);
372 _exit(127);
375 while (waitpid(pid, &st, 0) == -1)
376 if (errno != EINTR)
377 break;
379 doneediting:
380 (void)signal(SIGHUP, sighup);
381 (void)signal(SIGINT, sigint);
382 (void)signal(SIGQUIT, sigquit);
384 if (!WIFEXITED(st)) {
385 errno = EINTR;
386 return -1;
389 return WEXITSTATUS(st);
392 static const struct got_error *
393 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
394 const char *initial_content)
396 const struct got_error *err = NULL;
397 char buf[1024];
398 struct stat st, st2;
399 FILE *fp;
400 int content_changed = 0;
401 size_t len;
403 *logmsg = NULL;
405 if (stat(logmsg_path, &st) == -1)
406 return got_error_from_errno2("stat", logmsg_path);
408 if (spawn_editor(editor, logmsg_path) == -1)
409 return got_error_from_errno("failed spawning editor");
411 if (stat(logmsg_path, &st2) == -1)
412 return got_error_from_errno("stat");
414 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
415 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
416 "no changes made to commit message, aborting");
418 *logmsg = malloc(st2.st_size + 1);
419 if (*logmsg == NULL)
420 return got_error_from_errno("malloc");
421 (*logmsg)[0] = '\0';
422 len = 0;
424 fp = fopen(logmsg_path, "r");
425 if (fp == NULL) {
426 err = got_error_from_errno("fopen");
427 goto done;
429 while (fgets(buf, sizeof(buf), fp) != NULL) {
430 if (!content_changed && strcmp(buf, initial_content) != 0)
431 content_changed = 1;
432 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
433 continue; /* remove comments and leading empty lines */
434 len = strlcat(*logmsg, buf, st2.st_size);
436 fclose(fp);
438 while (len > 0 && (*logmsg)[len - 1] == '\n') {
439 (*logmsg)[len - 1] = '\0';
440 len--;
443 if (len == 0 || !content_changed)
444 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
445 "commit message cannot be empty, aborting");
446 done:
447 if (err) {
448 free(*logmsg);
449 *logmsg = NULL;
451 return err;
454 static const struct got_error *
455 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
456 const char *branch_name)
458 char *initial_content = NULL, *logmsg_path = NULL;
459 const struct got_error *err = NULL;
460 int fd;
462 if (asprintf(&initial_content,
463 "\n# %s to be imported to branch %s\n", path_dir,
464 branch_name) == -1)
465 return got_error_from_errno("asprintf");
467 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
468 if (err)
469 goto done;
471 dprintf(fd, initial_content);
472 close(fd);
474 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
475 done:
476 free(initial_content);
477 free(logmsg_path);
478 return err;
481 static const struct got_error *
482 import_progress(void *arg, const char *path)
484 printf("A %s\n", path);
485 return NULL;
488 static const struct got_error *
489 get_author(const char **author)
491 const char *got_author;
493 *author = NULL;
495 got_author = getenv("GOT_AUTHOR");
496 if (got_author == NULL) {
497 /* TODO: Look up user in password database? */
498 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
501 *author = got_author;
503 /*
504 * Really dumb email address check; we're only doing this to
505 * avoid git's object parser breaking on commits we create.
506 */
507 while (*got_author && *got_author != '<')
508 got_author++;
509 if (*got_author != '<')
510 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
511 while (*got_author && *got_author != '@')
512 got_author++;
513 if (*got_author != '@')
514 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
515 while (*got_author && *got_author != '>')
516 got_author++;
517 if (*got_author != '>')
518 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
520 return NULL;
523 static const struct got_error *
524 cmd_import(int argc, char *argv[])
526 const struct got_error *error = NULL;
527 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
528 char *editor = NULL;
529 const char *author;
530 const char *branch_name = "master";
531 char *refname = NULL, *id_str = NULL;
532 struct got_repository *repo = NULL;
533 struct got_reference *branch_ref = NULL, *head_ref = NULL;
534 struct got_object_id *new_commit_id = NULL;
535 int ch;
536 struct got_pathlist_head ignores;
537 struct got_pathlist_entry *pe;
539 TAILQ_INIT(&ignores);
541 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
542 switch (ch) {
543 case 'b':
544 branch_name = optarg;
545 break;
546 case 'm':
547 logmsg = strdup(optarg);
548 if (logmsg == NULL) {
549 error = got_error_from_errno("strdup");
550 goto done;
552 break;
553 case 'r':
554 repo_path = realpath(optarg, NULL);
555 if (repo_path == NULL) {
556 error = got_error_from_errno("realpath");
557 goto done;
559 break;
560 case 'I':
561 if (optarg[0] == '\0')
562 break;
563 error = got_pathlist_insert(&pe, &ignores, optarg,
564 NULL);
565 if (error)
566 goto done;
567 break;
568 default:
569 usage_init();
570 /* NOTREACHED */
574 argc -= optind;
575 argv += optind;
577 #ifndef PROFILE
578 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
579 NULL) == -1)
580 err(1, "pledge");
581 #endif
582 if (argc != 1)
583 usage_import();
585 error = get_author(&author);
586 if (error)
587 return error;
589 if (repo_path == NULL) {
590 repo_path = getcwd(NULL, 0);
591 if (repo_path == NULL)
592 return got_error_from_errno("getcwd");
594 got_path_strip_trailing_slashes(repo_path);
595 error = got_repo_open(&repo, repo_path);
596 if (error)
597 goto done;
599 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
600 error = got_error_from_errno("asprintf");
601 goto done;
604 error = got_ref_open(&branch_ref, repo, refname, 0);
605 if (error) {
606 if (error->code != GOT_ERR_NOT_REF)
607 goto done;
608 } else {
609 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
610 "import target branch already exists");
611 goto done;
614 path_dir = realpath(argv[0], NULL);
615 if (path_dir == NULL) {
616 error = got_error_from_errno("realpath");
617 goto done;
619 got_path_strip_trailing_slashes(path_dir);
621 /*
622 * unveil(2) traverses exec(2); if an editor is used we have
623 * to apply unveil after the log message has been written.
624 */
625 if (logmsg == NULL || strlen(logmsg) == 0) {
626 error = get_editor(&editor);
627 if (error)
628 goto done;
629 error = collect_import_msg(&logmsg, editor, path_dir, refname);
630 if (error)
631 goto done;
634 if (unveil(path_dir, "r") != 0)
635 return got_error_from_errno2("unveil", path_dir);
637 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
638 if (error)
639 goto done;
641 error = got_repo_import(&new_commit_id, path_dir, logmsg,
642 author, &ignores, repo, import_progress, NULL);
643 if (error)
644 goto done;
646 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
647 if (error)
648 goto done;
650 error = got_ref_write(branch_ref, repo);
651 if (error)
652 goto done;
654 error = got_object_id_str(&id_str, new_commit_id);
655 if (error)
656 goto done;
658 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
659 if (error) {
660 if (error->code != GOT_ERR_NOT_REF)
661 goto done;
663 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
664 branch_ref);
665 if (error)
666 goto done;
668 error = got_ref_write(head_ref, repo);
669 if (error)
670 goto done;
673 printf("Created branch %s with commit %s\n",
674 got_ref_get_name(branch_ref), id_str);
675 done:
676 free(repo_path);
677 free(editor);
678 free(refname);
679 free(new_commit_id);
680 free(id_str);
681 if (branch_ref)
682 got_ref_close(branch_ref);
683 if (head_ref)
684 got_ref_close(head_ref);
685 return error;
688 __dead static void
689 usage_checkout(void)
691 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
692 "[-p prefix] repository-path [worktree-path]\n", getprogname());
693 exit(1);
696 static const struct got_error *
697 checkout_progress(void *arg, unsigned char status, const char *path)
699 char *worktree_path = arg;
701 /* Base commit bump happens silently. */
702 if (status == GOT_STATUS_BUMP_BASE)
703 return NULL;
705 while (path[0] == '/')
706 path++;
708 printf("%c %s/%s\n", status, worktree_path, path);
709 return NULL;
712 static const struct got_error *
713 check_cancelled(void *arg)
715 if (sigint_received || sigpipe_received)
716 return got_error(GOT_ERR_CANCELLED);
717 return NULL;
720 static const struct got_error *
721 check_linear_ancestry(struct got_object_id *commit_id,
722 struct got_object_id *base_commit_id, struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_object_id *yca_id;
727 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
728 commit_id, base_commit_id, repo, check_cancelled, NULL);
729 if (err)
730 return err;
732 if (yca_id == NULL)
733 return got_error(GOT_ERR_ANCESTRY);
735 /*
736 * Require a straight line of history between the target commit
737 * and the work tree's base commit.
739 * Non-linear situations such as this require a rebase:
741 * (commit) D F (base_commit)
742 * \ /
743 * C E
744 * \ /
745 * B (yca)
746 * |
747 * A
749 * 'got update' only handles linear cases:
750 * Update forwards in time: A (base/yca) - B - C - D (commit)
751 * Update backwards in time: D (base) - C - B - A (commit/yca)
752 */
753 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
754 got_object_id_cmp(base_commit_id, yca_id) != 0)
755 return got_error(GOT_ERR_ANCESTRY);
757 free(yca_id);
758 return NULL;
761 static const struct got_error *
762 check_same_branch(struct got_object_id *commit_id,
763 struct got_reference *head_ref, struct got_object_id *yca_id,
764 struct got_repository *repo)
766 const struct got_error *err = NULL;
767 struct got_commit_graph *graph = NULL;
768 struct got_object_id *head_commit_id = NULL;
769 int is_same_branch = 0;
771 err = got_ref_resolve(&head_commit_id, repo, head_ref);
772 if (err)
773 goto done;
775 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
776 is_same_branch = 1;
777 goto done;
779 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
780 is_same_branch = 1;
781 goto done;
784 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
785 if (err)
786 goto done;
788 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
789 check_cancelled, NULL);
790 if (err)
791 goto done;
793 for (;;) {
794 struct got_object_id *id;
795 err = got_commit_graph_iter_next(&id, graph);
796 if (err) {
797 if (err->code == GOT_ERR_ITER_COMPLETED) {
798 err = NULL;
799 break;
800 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
801 break;
802 err = got_commit_graph_fetch_commits(graph, 1,
803 repo, check_cancelled, NULL);
804 if (err)
805 break;
808 if (id) {
809 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
810 break;
811 if (got_object_id_cmp(id, commit_id) == 0) {
812 is_same_branch = 1;
813 break;
817 done:
818 if (graph)
819 got_commit_graph_close(graph);
820 free(head_commit_id);
821 if (!err && !is_same_branch)
822 err = got_error(GOT_ERR_ANCESTRY);
823 return err;
826 static const struct got_error *
827 resolve_commit_arg(struct got_object_id **commit_id,
828 const char *commit_id_arg, struct got_repository *repo)
830 const struct got_error *err;
831 struct got_reference *ref;
832 struct got_tag_object *tag;
834 err = got_repo_object_match_tag(&tag, commit_id_arg,
835 GOT_OBJ_TYPE_COMMIT, repo);
836 if (err == NULL) {
837 *commit_id = got_object_id_dup(
838 got_object_tag_get_object_id(tag));
839 if (*commit_id == NULL)
840 err = got_error_from_errno("got_object_id_dup");
841 got_object_tag_close(tag);
842 return err;
843 } else if (err->code != GOT_ERR_NO_OBJ)
844 return err;
846 err = got_ref_open(&ref, repo, commit_id_arg, 0);
847 if (err == NULL) {
848 err = got_ref_resolve(commit_id, repo, ref);
849 got_ref_close(ref);
850 } else {
851 if (err->code != GOT_ERR_NOT_REF)
852 return err;
853 err = got_repo_match_object_id_prefix(commit_id,
854 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
856 return err;
859 static const struct got_error *
860 cmd_checkout(int argc, char *argv[])
862 const struct got_error *error = NULL;
863 struct got_repository *repo = NULL;
864 struct got_reference *head_ref = NULL;
865 struct got_worktree *worktree = NULL;
866 char *repo_path = NULL;
867 char *worktree_path = NULL;
868 const char *path_prefix = "";
869 const char *branch_name = GOT_REF_HEAD;
870 char *commit_id_str = NULL;
871 int ch, same_path_prefix;
872 struct got_pathlist_head paths;
874 TAILQ_INIT(&paths);
876 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
877 switch (ch) {
878 case 'b':
879 branch_name = optarg;
880 break;
881 case 'c':
882 commit_id_str = strdup(optarg);
883 if (commit_id_str == NULL)
884 return got_error_from_errno("strdup");
885 break;
886 case 'p':
887 path_prefix = optarg;
888 break;
889 default:
890 usage_checkout();
891 /* NOTREACHED */
895 argc -= optind;
896 argv += optind;
898 #ifndef PROFILE
899 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
900 "unveil", NULL) == -1)
901 err(1, "pledge");
902 #endif
903 if (argc == 1) {
904 char *cwd, *base, *dotgit;
905 repo_path = realpath(argv[0], NULL);
906 if (repo_path == NULL)
907 return got_error_from_errno2("realpath", argv[0]);
908 cwd = getcwd(NULL, 0);
909 if (cwd == NULL) {
910 error = got_error_from_errno("getcwd");
911 goto done;
913 if (path_prefix[0]) {
914 base = basename(path_prefix);
915 if (base == NULL) {
916 error = got_error_from_errno2("basename",
917 path_prefix);
918 goto done;
920 } else {
921 base = basename(repo_path);
922 if (base == NULL) {
923 error = got_error_from_errno2("basename",
924 repo_path);
925 goto done;
928 dotgit = strstr(base, ".git");
929 if (dotgit)
930 *dotgit = '\0';
931 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
932 error = got_error_from_errno("asprintf");
933 free(cwd);
934 goto done;
936 free(cwd);
937 } else if (argc == 2) {
938 repo_path = realpath(argv[0], NULL);
939 if (repo_path == NULL) {
940 error = got_error_from_errno2("realpath", argv[0]);
941 goto done;
943 worktree_path = realpath(argv[1], NULL);
944 if (worktree_path == NULL) {
945 if (errno != ENOENT) {
946 error = got_error_from_errno2("realpath",
947 argv[1]);
948 goto done;
950 worktree_path = strdup(argv[1]);
951 if (worktree_path == NULL) {
952 error = got_error_from_errno("strdup");
953 goto done;
956 } else
957 usage_checkout();
959 got_path_strip_trailing_slashes(repo_path);
960 got_path_strip_trailing_slashes(worktree_path);
962 error = got_repo_open(&repo, repo_path);
963 if (error != NULL)
964 goto done;
966 /* Pre-create work tree path for unveil(2) */
967 error = got_path_mkdir(worktree_path);
968 if (error) {
969 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
970 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
971 goto done;
972 if (!got_path_dir_is_empty(worktree_path)) {
973 error = got_error_path(worktree_path,
974 GOT_ERR_DIR_NOT_EMPTY);
975 goto done;
979 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
980 if (error)
981 goto done;
983 error = got_ref_open(&head_ref, repo, branch_name, 0);
984 if (error != NULL)
985 goto done;
987 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
988 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
989 goto done;
991 error = got_worktree_open(&worktree, worktree_path);
992 if (error != NULL)
993 goto done;
995 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
996 path_prefix);
997 if (error != NULL)
998 goto done;
999 if (!same_path_prefix) {
1000 error = got_error(GOT_ERR_PATH_PREFIX);
1001 goto done;
1004 if (commit_id_str) {
1005 struct got_object_id *commit_id;
1006 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1007 if (error)
1008 goto done;
1009 error = check_linear_ancestry(commit_id,
1010 got_worktree_get_base_commit_id(worktree), repo);
1011 if (error != NULL) {
1012 free(commit_id);
1013 goto done;
1015 error = check_same_branch(commit_id, head_ref, NULL, repo);
1016 if (error)
1017 goto done;
1018 error = got_worktree_set_base_commit_id(worktree, repo,
1019 commit_id);
1020 free(commit_id);
1021 if (error)
1022 goto done;
1025 error = got_pathlist_append(&paths, "", NULL);
1026 if (error)
1027 goto done;
1028 error = got_worktree_checkout_files(worktree, &paths, repo,
1029 checkout_progress, worktree_path, check_cancelled, NULL);
1030 if (error != NULL)
1031 goto done;
1033 printf("Now shut up and hack\n");
1035 done:
1036 got_pathlist_free(&paths);
1037 free(commit_id_str);
1038 free(repo_path);
1039 free(worktree_path);
1040 return error;
1043 __dead static void
1044 usage_update(void)
1046 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1047 getprogname());
1048 exit(1);
1051 static const struct got_error *
1052 update_progress(void *arg, unsigned char status, const char *path)
1054 int *did_something = arg;
1056 if (status == GOT_STATUS_EXISTS)
1057 return NULL;
1059 *did_something = 1;
1061 /* Base commit bump happens silently. */
1062 if (status == GOT_STATUS_BUMP_BASE)
1063 return NULL;
1065 while (path[0] == '/')
1066 path++;
1067 printf("%c %s\n", status, path);
1068 return NULL;
1071 static const struct got_error *
1072 switch_head_ref(struct got_reference *head_ref,
1073 struct got_object_id *commit_id, struct got_worktree *worktree,
1074 struct got_repository *repo)
1076 const struct got_error *err = NULL;
1077 char *base_id_str;
1078 int ref_has_moved = 0;
1080 /* Trivial case: switching between two different references. */
1081 if (strcmp(got_ref_get_name(head_ref),
1082 got_worktree_get_head_ref_name(worktree)) != 0) {
1083 printf("Switching work tree from %s to %s\n",
1084 got_worktree_get_head_ref_name(worktree),
1085 got_ref_get_name(head_ref));
1086 return got_worktree_set_head_ref(worktree, head_ref);
1089 err = check_linear_ancestry(commit_id,
1090 got_worktree_get_base_commit_id(worktree), repo);
1091 if (err) {
1092 if (err->code != GOT_ERR_ANCESTRY)
1093 return err;
1094 ref_has_moved = 1;
1096 if (!ref_has_moved)
1097 return NULL;
1099 /* Switching to a rebased branch with the same reference name. */
1100 err = got_object_id_str(&base_id_str,
1101 got_worktree_get_base_commit_id(worktree));
1102 if (err)
1103 return err;
1104 printf("Reference %s now points at a different branch\n",
1105 got_worktree_get_head_ref_name(worktree));
1106 printf("Switching work tree from %s to %s\n", base_id_str,
1107 got_worktree_get_head_ref_name(worktree));
1108 return NULL;
1111 static const struct got_error *
1112 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1114 const struct got_error *err;
1115 int in_progress;
1117 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1118 if (err)
1119 return err;
1120 if (in_progress)
1121 return got_error(GOT_ERR_REBASING);
1123 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1124 if (err)
1125 return err;
1126 if (in_progress)
1127 return got_error(GOT_ERR_HISTEDIT_BUSY);
1129 return NULL;
1132 static const struct got_error *
1133 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1134 char *argv[], struct got_worktree *worktree)
1136 const struct got_error *err = NULL;
1137 char *path;
1138 int i;
1140 if (argc == 0) {
1141 path = strdup("");
1142 if (path == NULL)
1143 return got_error_from_errno("strdup");
1144 return got_pathlist_append(paths, path, NULL);
1147 for (i = 0; i < argc; i++) {
1148 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1149 if (err)
1150 break;
1151 err = got_pathlist_append(paths, path, NULL);
1152 if (err) {
1153 free(path);
1154 break;
1158 return err;
1161 static const struct got_error *
1162 cmd_update(int argc, char *argv[])
1164 const struct got_error *error = NULL;
1165 struct got_repository *repo = NULL;
1166 struct got_worktree *worktree = NULL;
1167 char *worktree_path = NULL;
1168 struct got_object_id *commit_id = NULL;
1169 char *commit_id_str = NULL;
1170 const char *branch_name = NULL;
1171 struct got_reference *head_ref = NULL;
1172 struct got_pathlist_head paths;
1173 struct got_pathlist_entry *pe;
1174 int ch, did_something = 0;
1176 TAILQ_INIT(&paths);
1178 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1179 switch (ch) {
1180 case 'b':
1181 branch_name = optarg;
1182 break;
1183 case 'c':
1184 commit_id_str = strdup(optarg);
1185 if (commit_id_str == NULL)
1186 return got_error_from_errno("strdup");
1187 break;
1188 default:
1189 usage_update();
1190 /* NOTREACHED */
1194 argc -= optind;
1195 argv += optind;
1197 #ifndef PROFILE
1198 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1199 "unveil", NULL) == -1)
1200 err(1, "pledge");
1201 #endif
1202 worktree_path = getcwd(NULL, 0);
1203 if (worktree_path == NULL) {
1204 error = got_error_from_errno("getcwd");
1205 goto done;
1207 error = got_worktree_open(&worktree, worktree_path);
1208 if (error)
1209 goto done;
1211 error = check_rebase_or_histedit_in_progress(worktree);
1212 if (error)
1213 goto done;
1215 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1216 if (error != NULL)
1217 goto done;
1219 error = apply_unveil(got_repo_get_path(repo), 0,
1220 got_worktree_get_root_path(worktree));
1221 if (error)
1222 goto done;
1224 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1225 if (error)
1226 goto done;
1228 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1229 got_worktree_get_head_ref_name(worktree), 0);
1230 if (error != NULL)
1231 goto done;
1232 if (commit_id_str == NULL) {
1233 error = got_ref_resolve(&commit_id, repo, head_ref);
1234 if (error != NULL)
1235 goto done;
1236 error = got_object_id_str(&commit_id_str, commit_id);
1237 if (error != NULL)
1238 goto done;
1239 } else {
1240 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1241 free(commit_id_str);
1242 commit_id_str = NULL;
1243 if (error)
1244 goto done;
1245 error = got_object_id_str(&commit_id_str, commit_id);
1246 if (error)
1247 goto done;
1250 if (branch_name) {
1251 struct got_object_id *head_commit_id;
1252 TAILQ_FOREACH(pe, &paths, entry) {
1253 if (pe->path_len == 0)
1254 continue;
1255 error = got_error_msg(GOT_ERR_BAD_PATH,
1256 "switching between branches requires that "
1257 "the entire work tree gets updated");
1258 goto done;
1260 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1261 if (error)
1262 goto done;
1263 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1264 free(head_commit_id);
1265 if (error != NULL)
1266 goto done;
1267 error = check_same_branch(commit_id, head_ref, NULL, repo);
1268 if (error)
1269 goto done;
1270 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1271 if (error)
1272 goto done;
1273 } else {
1274 error = check_linear_ancestry(commit_id,
1275 got_worktree_get_base_commit_id(worktree), repo);
1276 if (error != NULL) {
1277 if (error->code == GOT_ERR_ANCESTRY)
1278 error = got_error(GOT_ERR_BRANCH_MOVED);
1279 goto done;
1281 error = check_same_branch(commit_id, head_ref, NULL, repo);
1282 if (error)
1283 goto done;
1286 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1287 commit_id) != 0) {
1288 error = got_worktree_set_base_commit_id(worktree, repo,
1289 commit_id);
1290 if (error)
1291 goto done;
1294 error = got_worktree_checkout_files(worktree, &paths, repo,
1295 update_progress, &did_something, check_cancelled, NULL);
1296 if (error != NULL)
1297 goto done;
1299 if (did_something)
1300 printf("Updated to commit %s\n", commit_id_str);
1301 else
1302 printf("Already up-to-date\n");
1303 done:
1304 free(worktree_path);
1305 TAILQ_FOREACH(pe, &paths, entry)
1306 free((char *)pe->path);
1307 got_pathlist_free(&paths);
1308 free(commit_id);
1309 free(commit_id_str);
1310 return error;
1313 static const struct got_error *
1314 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1315 int diff_context, struct got_repository *repo)
1317 const struct got_error *err = NULL;
1318 struct got_tree_object *tree1 = NULL, *tree2;
1319 struct got_object_qid *qid;
1320 char *id_str1 = NULL, *id_str2;
1321 struct got_diff_blob_output_unidiff_arg arg;
1323 err = got_object_open_as_tree(&tree2, repo,
1324 got_object_commit_get_tree_id(commit));
1325 if (err)
1326 return err;
1328 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1329 if (qid != NULL) {
1330 struct got_commit_object *pcommit;
1332 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1333 if (err)
1334 return err;
1336 err = got_object_open_as_tree(&tree1, repo,
1337 got_object_commit_get_tree_id(pcommit));
1338 got_object_commit_close(pcommit);
1339 if (err)
1340 return err;
1342 err = got_object_id_str(&id_str1, qid->id);
1343 if (err)
1344 return err;
1347 err = got_object_id_str(&id_str2, id);
1348 if (err)
1349 goto done;
1351 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1352 arg.diff_context = diff_context;
1353 arg.outfile = stdout;
1354 err = got_diff_tree(tree1, tree2, "", "", repo,
1355 got_diff_blob_output_unidiff, &arg, 1);
1356 done:
1357 if (tree1)
1358 got_object_tree_close(tree1);
1359 got_object_tree_close(tree2);
1360 free(id_str1);
1361 free(id_str2);
1362 return err;
1365 static char *
1366 get_datestr(time_t *time, char *datebuf)
1368 struct tm mytm, *tm;
1369 char *p, *s;
1371 tm = gmtime_r(time, &mytm);
1372 if (tm == NULL)
1373 return NULL;
1374 s = asctime_r(tm, datebuf);
1375 if (s == NULL)
1376 return NULL;
1377 p = strchr(s, '\n');
1378 if (p)
1379 *p = '\0';
1380 return s;
1383 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1385 static const struct got_error *
1386 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1387 struct got_repository *repo, int show_patch, int diff_context,
1388 struct got_reflist_head *refs)
1390 const struct got_error *err = NULL;
1391 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1392 char datebuf[26];
1393 time_t committer_time;
1394 const char *author, *committer;
1395 char *refs_str = NULL;
1396 struct got_reflist_entry *re;
1398 SIMPLEQ_FOREACH(re, refs, entry) {
1399 char *s;
1400 const char *name;
1401 struct got_tag_object *tag = NULL;
1402 int cmp;
1404 name = got_ref_get_name(re->ref);
1405 if (strcmp(name, GOT_REF_HEAD) == 0)
1406 continue;
1407 if (strncmp(name, "refs/", 5) == 0)
1408 name += 5;
1409 if (strncmp(name, "got/", 4) == 0)
1410 continue;
1411 if (strncmp(name, "heads/", 6) == 0)
1412 name += 6;
1413 if (strncmp(name, "remotes/", 8) == 0)
1414 name += 8;
1415 if (strncmp(name, "tags/", 5) == 0) {
1416 err = got_object_open_as_tag(&tag, repo, re->id);
1417 if (err) {
1418 if (err->code != GOT_ERR_OBJ_TYPE)
1419 return err;
1420 /* Ref points at something other than a tag. */
1421 err = NULL;
1422 tag = NULL;
1425 cmp = got_object_id_cmp(tag ?
1426 got_object_tag_get_object_id(tag) : re->id, id);
1427 if (tag)
1428 got_object_tag_close(tag);
1429 if (cmp != 0)
1430 continue;
1431 s = refs_str;
1432 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1433 name) == -1) {
1434 err = got_error_from_errno("asprintf");
1435 free(s);
1436 return err;
1438 free(s);
1440 err = got_object_id_str(&id_str, id);
1441 if (err)
1442 return err;
1444 printf(GOT_COMMIT_SEP_STR);
1445 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1446 refs_str ? refs_str : "", refs_str ? ")" : "");
1447 free(id_str);
1448 id_str = NULL;
1449 free(refs_str);
1450 refs_str = NULL;
1451 printf("from: %s\n", got_object_commit_get_author(commit));
1452 committer_time = got_object_commit_get_committer_time(commit);
1453 datestr = get_datestr(&committer_time, datebuf);
1454 if (datestr)
1455 printf("date: %s UTC\n", datestr);
1456 author = got_object_commit_get_author(commit);
1457 committer = got_object_commit_get_committer(commit);
1458 if (strcmp(author, committer) != 0)
1459 printf("via: %s\n", committer);
1460 if (got_object_commit_get_nparents(commit) > 1) {
1461 const struct got_object_id_queue *parent_ids;
1462 struct got_object_qid *qid;
1463 int n = 1;
1464 parent_ids = got_object_commit_get_parent_ids(commit);
1465 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1466 err = got_object_id_str(&id_str, qid->id);
1467 if (err)
1468 return err;
1469 printf("parent %d: %s\n", n++, id_str);
1470 free(id_str);
1474 err = got_object_commit_get_logmsg(&logmsg0, commit);
1475 if (err)
1476 return err;
1478 logmsg = logmsg0;
1479 do {
1480 line = strsep(&logmsg, "\n");
1481 if (line)
1482 printf(" %s\n", line);
1483 } while (line);
1484 free(logmsg0);
1486 if (show_patch) {
1487 err = print_patch(commit, id, diff_context, repo);
1488 if (err == 0)
1489 printf("\n");
1492 if (fflush(stdout) != 0 && err == NULL)
1493 err = got_error_from_errno("fflush");
1494 return err;
1497 static const struct got_error *
1498 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1499 char *path, int show_patch, int diff_context, int limit,
1500 int first_parent_traversal, struct got_reflist_head *refs)
1502 const struct got_error *err;
1503 struct got_commit_graph *graph;
1505 err = got_commit_graph_open(&graph, root_id, path,
1506 first_parent_traversal, repo);
1507 if (err)
1508 return err;
1509 err = got_commit_graph_iter_start(graph, root_id, repo,
1510 check_cancelled, NULL);
1511 if (err)
1512 goto done;
1513 for (;;) {
1514 struct got_commit_object *commit;
1515 struct got_object_id *id;
1517 if (sigint_received || sigpipe_received)
1518 break;
1520 err = got_commit_graph_iter_next(&id, graph);
1521 if (err) {
1522 if (err->code == GOT_ERR_ITER_COMPLETED) {
1523 err = NULL;
1524 break;
1526 if (err->code != GOT_ERR_ITER_NEED_MORE)
1527 break;
1528 err = got_commit_graph_fetch_commits(graph, 1, repo,
1529 check_cancelled, NULL);
1530 if (err)
1531 break;
1532 else
1533 continue;
1535 if (id == NULL)
1536 break;
1538 err = got_object_open_as_commit(&commit, repo, id);
1539 if (err)
1540 break;
1541 err = print_commit(commit, id, repo, show_patch, diff_context,
1542 refs);
1543 got_object_commit_close(commit);
1544 if (err || (limit && --limit == 0))
1545 break;
1547 done:
1548 got_commit_graph_close(graph);
1549 return err;
1552 __dead static void
1553 usage_log(void)
1555 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1556 "[-r repository-path] [path]\n", getprogname());
1557 exit(1);
1560 static int
1561 get_default_log_limit(void)
1563 const char *got_default_log_limit;
1564 long long n;
1565 const char *errstr;
1567 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1568 if (got_default_log_limit == NULL)
1569 return 0;
1570 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1571 if (errstr != NULL)
1572 return 0;
1573 return n;
1576 static const struct got_error *
1577 cmd_log(int argc, char *argv[])
1579 const struct got_error *error;
1580 struct got_repository *repo = NULL;
1581 struct got_worktree *worktree = NULL;
1582 struct got_commit_object *commit = NULL;
1583 struct got_object_id *id = NULL;
1584 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1585 char *start_commit = NULL;
1586 int diff_context = 3, ch;
1587 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1588 const char *errstr;
1589 struct got_reflist_head refs;
1591 SIMPLEQ_INIT(&refs);
1593 #ifndef PROFILE
1594 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1595 NULL)
1596 == -1)
1597 err(1, "pledge");
1598 #endif
1600 limit = get_default_log_limit();
1602 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1603 switch (ch) {
1604 case 'p':
1605 show_patch = 1;
1606 break;
1607 case 'c':
1608 start_commit = optarg;
1609 break;
1610 case 'C':
1611 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1612 &errstr);
1613 if (errstr != NULL)
1614 err(1, "-C option %s", errstr);
1615 break;
1616 case 'l':
1617 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1618 if (errstr != NULL)
1619 err(1, "-l option %s", errstr);
1620 break;
1621 case 'f':
1622 first_parent_traversal = 1;
1623 break;
1624 case 'r':
1625 repo_path = realpath(optarg, NULL);
1626 if (repo_path == NULL)
1627 err(1, "-r option");
1628 got_path_strip_trailing_slashes(repo_path);
1629 break;
1630 default:
1631 usage_log();
1632 /* NOTREACHED */
1636 argc -= optind;
1637 argv += optind;
1639 cwd = getcwd(NULL, 0);
1640 if (cwd == NULL) {
1641 error = got_error_from_errno("getcwd");
1642 goto done;
1645 error = got_worktree_open(&worktree, cwd);
1646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1647 goto done;
1648 error = NULL;
1650 if (argc == 0) {
1651 path = strdup("");
1652 if (path == NULL) {
1653 error = got_error_from_errno("strdup");
1654 goto done;
1656 } else if (argc == 1) {
1657 if (worktree) {
1658 error = got_worktree_resolve_path(&path, worktree,
1659 argv[0]);
1660 if (error)
1661 goto done;
1662 } else {
1663 path = strdup(argv[0]);
1664 if (path == NULL) {
1665 error = got_error_from_errno("strdup");
1666 goto done;
1669 } else
1670 usage_log();
1672 if (repo_path == NULL) {
1673 repo_path = worktree ?
1674 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1676 if (repo_path == NULL) {
1677 error = got_error_from_errno("strdup");
1678 goto done;
1681 error = got_repo_open(&repo, repo_path);
1682 if (error != NULL)
1683 goto done;
1685 error = apply_unveil(got_repo_get_path(repo), 1,
1686 worktree ? got_worktree_get_root_path(worktree) : NULL);
1687 if (error)
1688 goto done;
1690 if (start_commit == NULL) {
1691 struct got_reference *head_ref;
1692 error = got_ref_open(&head_ref, repo,
1693 worktree ? got_worktree_get_head_ref_name(worktree)
1694 : GOT_REF_HEAD, 0);
1695 if (error != NULL)
1696 return error;
1697 error = got_ref_resolve(&id, repo, head_ref);
1698 got_ref_close(head_ref);
1699 if (error != NULL)
1700 return error;
1701 error = got_object_open_as_commit(&commit, repo, id);
1702 } else {
1703 struct got_reference *ref;
1704 error = got_ref_open(&ref, repo, start_commit, 0);
1705 if (error == NULL) {
1706 int obj_type;
1707 error = got_ref_resolve(&id, repo, ref);
1708 got_ref_close(ref);
1709 if (error != NULL)
1710 goto done;
1711 error = got_object_get_type(&obj_type, repo, id);
1712 if (error != NULL)
1713 goto done;
1714 if (obj_type == GOT_OBJ_TYPE_TAG) {
1715 struct got_tag_object *tag;
1716 error = got_object_open_as_tag(&tag, repo, id);
1717 if (error != NULL)
1718 goto done;
1719 if (got_object_tag_get_object_type(tag) !=
1720 GOT_OBJ_TYPE_COMMIT) {
1721 got_object_tag_close(tag);
1722 error = got_error(GOT_ERR_OBJ_TYPE);
1723 goto done;
1725 free(id);
1726 id = got_object_id_dup(
1727 got_object_tag_get_object_id(tag));
1728 if (id == NULL)
1729 error = got_error_from_errno(
1730 "got_object_id_dup");
1731 got_object_tag_close(tag);
1732 if (error)
1733 goto done;
1734 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1735 error = got_error(GOT_ERR_OBJ_TYPE);
1736 goto done;
1738 error = got_object_open_as_commit(&commit, repo, id);
1739 if (error != NULL)
1740 goto done;
1742 if (commit == NULL) {
1743 error = got_repo_match_object_id_prefix(&id,
1744 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1745 if (error != NULL)
1746 return error;
1749 if (error != NULL)
1750 goto done;
1752 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1753 if (error != NULL)
1754 goto done;
1755 if (in_repo_path) {
1756 free(path);
1757 path = in_repo_path;
1760 error = got_ref_list(&refs, repo);
1761 if (error)
1762 goto done;
1764 error = print_commits(id, repo, path, show_patch,
1765 diff_context, limit, first_parent_traversal, &refs);
1766 done:
1767 free(path);
1768 free(repo_path);
1769 free(cwd);
1770 free(id);
1771 if (worktree)
1772 got_worktree_close(worktree);
1773 if (repo) {
1774 const struct got_error *repo_error;
1775 repo_error = got_repo_close(repo);
1776 if (error == NULL)
1777 error = repo_error;
1779 got_ref_list_free(&refs);
1780 return error;
1783 __dead static void
1784 usage_diff(void)
1786 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1787 "[object1 object2 | path]\n", getprogname());
1788 exit(1);
1791 struct print_diff_arg {
1792 struct got_repository *repo;
1793 struct got_worktree *worktree;
1794 int diff_context;
1795 const char *id_str;
1796 int header_shown;
1797 int diff_staged;
1800 static const struct got_error *
1801 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1802 const char *path, struct got_object_id *blob_id,
1803 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1805 struct print_diff_arg *a = arg;
1806 const struct got_error *err = NULL;
1807 struct got_blob_object *blob1 = NULL;
1808 FILE *f2 = NULL;
1809 char *abspath = NULL, *label1 = NULL;
1810 struct stat sb;
1812 if (a->diff_staged) {
1813 if (staged_status != GOT_STATUS_MODIFY &&
1814 staged_status != GOT_STATUS_ADD &&
1815 staged_status != GOT_STATUS_DELETE)
1816 return NULL;
1817 } else {
1818 if (staged_status == GOT_STATUS_DELETE)
1819 return NULL;
1820 if (status != GOT_STATUS_MODIFY &&
1821 status != GOT_STATUS_ADD &&
1822 status != GOT_STATUS_DELETE &&
1823 status != GOT_STATUS_CONFLICT)
1824 return NULL;
1827 if (!a->header_shown) {
1828 printf("diff %s %s%s\n", a->id_str,
1829 got_worktree_get_root_path(a->worktree),
1830 a->diff_staged ? " (staged changes)" : "");
1831 a->header_shown = 1;
1834 if (a->diff_staged) {
1835 const char *label1 = NULL, *label2 = NULL;
1836 switch (staged_status) {
1837 case GOT_STATUS_MODIFY:
1838 label1 = path;
1839 label2 = path;
1840 break;
1841 case GOT_STATUS_ADD:
1842 label2 = path;
1843 break;
1844 case GOT_STATUS_DELETE:
1845 label1 = path;
1846 break;
1847 default:
1848 return got_error(GOT_ERR_FILE_STATUS);
1850 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1851 label1, label2, a->diff_context, a->repo, stdout);
1854 if (staged_status == GOT_STATUS_ADD ||
1855 staged_status == GOT_STATUS_MODIFY) {
1856 char *id_str;
1857 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1858 8192);
1859 if (err)
1860 goto done;
1861 err = got_object_id_str(&id_str, staged_blob_id);
1862 if (err)
1863 goto done;
1864 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1865 err = got_error_from_errno("asprintf");
1866 free(id_str);
1867 goto done;
1869 free(id_str);
1870 } else if (status != GOT_STATUS_ADD) {
1871 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1872 if (err)
1873 goto done;
1876 if (status != GOT_STATUS_DELETE) {
1877 if (asprintf(&abspath, "%s/%s",
1878 got_worktree_get_root_path(a->worktree), path) == -1) {
1879 err = got_error_from_errno("asprintf");
1880 goto done;
1883 f2 = fopen(abspath, "r");
1884 if (f2 == NULL) {
1885 err = got_error_from_errno2("fopen", abspath);
1886 goto done;
1888 if (lstat(abspath, &sb) == -1) {
1889 err = got_error_from_errno2("lstat", abspath);
1890 goto done;
1892 } else
1893 sb.st_size = 0;
1895 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1896 a->diff_context, stdout);
1897 done:
1898 if (blob1)
1899 got_object_blob_close(blob1);
1900 if (f2 && fclose(f2) != 0 && err == NULL)
1901 err = got_error_from_errno("fclose");
1902 free(abspath);
1903 return err;
1906 static const struct got_error *
1907 match_object_id(struct got_object_id **id, char **label,
1908 const char *id_str, int obj_type, struct got_repository *repo)
1910 const struct got_error *err;
1911 struct got_tag_object *tag;
1912 struct got_reference *ref = NULL;
1914 *id = NULL;
1915 *label = NULL;
1917 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1918 if (err == NULL) {
1919 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1920 if (*id == NULL)
1921 err = got_error_from_errno("got_object_id_dup");
1922 if (asprintf(label, "refs/tags/%s",
1923 got_object_tag_get_name(tag)) == -1)
1924 err = got_error_from_errno("asprintf");
1925 got_object_tag_close(tag);
1926 return err;
1927 } else if (err->code != GOT_ERR_NO_OBJ)
1928 return err;
1930 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1931 if (err) {
1932 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1933 return err;
1934 err = got_ref_open(&ref, repo, id_str, 0);
1935 if (err != NULL)
1936 goto done;
1937 *label = strdup(got_ref_get_name(ref));
1938 if (*label == NULL) {
1939 err = got_error_from_errno("strdup");
1940 goto done;
1942 err = got_ref_resolve(id, repo, ref);
1943 } else {
1944 err = got_object_id_str(label, *id);
1945 if (*label == NULL) {
1946 err = got_error_from_errno("strdup");
1947 goto done;
1950 done:
1951 if (ref)
1952 got_ref_close(ref);
1953 return err;
1957 static const struct got_error *
1958 cmd_diff(int argc, char *argv[])
1960 const struct got_error *error;
1961 struct got_repository *repo = NULL;
1962 struct got_worktree *worktree = NULL;
1963 char *cwd = NULL, *repo_path = NULL;
1964 struct got_object_id *id1 = NULL, *id2 = NULL;
1965 const char *id_str1 = NULL, *id_str2 = NULL;
1966 char *label1 = NULL, *label2 = NULL;
1967 int type1, type2;
1968 int diff_context = 3, diff_staged = 0, ch;
1969 const char *errstr;
1970 char *path = NULL;
1972 #ifndef PROFILE
1973 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1974 NULL) == -1)
1975 err(1, "pledge");
1976 #endif
1978 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1979 switch (ch) {
1980 case 'C':
1981 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1982 if (errstr != NULL)
1983 err(1, "-C option %s", errstr);
1984 break;
1985 case 'r':
1986 repo_path = realpath(optarg, NULL);
1987 if (repo_path == NULL)
1988 err(1, "-r option");
1989 got_path_strip_trailing_slashes(repo_path);
1990 break;
1991 case 's':
1992 diff_staged = 1;
1993 break;
1994 default:
1995 usage_diff();
1996 /* NOTREACHED */
2000 argc -= optind;
2001 argv += optind;
2003 cwd = getcwd(NULL, 0);
2004 if (cwd == NULL) {
2005 error = got_error_from_errno("getcwd");
2006 goto done;
2008 error = got_worktree_open(&worktree, cwd);
2009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2010 goto done;
2011 if (argc <= 1) {
2012 if (worktree == NULL) {
2013 error = got_error(GOT_ERR_NOT_WORKTREE);
2014 goto done;
2016 if (repo_path)
2017 errx(1,
2018 "-r option can't be used when diffing a work tree");
2019 repo_path = strdup(got_worktree_get_repo_path(worktree));
2020 if (repo_path == NULL) {
2021 error = got_error_from_errno("strdup");
2022 goto done;
2024 if (argc == 1) {
2025 error = got_worktree_resolve_path(&path, worktree,
2026 argv[0]);
2027 if (error)
2028 goto done;
2029 } else {
2030 path = strdup("");
2031 if (path == NULL) {
2032 error = got_error_from_errno("strdup");
2033 goto done;
2036 } else if (argc == 2) {
2037 if (diff_staged)
2038 errx(1, "-s option can't be used when diffing "
2039 "objects in repository");
2040 id_str1 = argv[0];
2041 id_str2 = argv[1];
2042 if (worktree && repo_path == NULL) {
2043 repo_path =
2044 strdup(got_worktree_get_repo_path(worktree));
2045 if (repo_path == NULL) {
2046 error = got_error_from_errno("strdup");
2047 goto done;
2050 } else
2051 usage_diff();
2053 if (repo_path == NULL) {
2054 repo_path = getcwd(NULL, 0);
2055 if (repo_path == NULL)
2056 return got_error_from_errno("getcwd");
2059 error = got_repo_open(&repo, repo_path);
2060 free(repo_path);
2061 if (error != NULL)
2062 goto done;
2064 error = apply_unveil(got_repo_get_path(repo), 1,
2065 worktree ? got_worktree_get_root_path(worktree) : NULL);
2066 if (error)
2067 goto done;
2069 if (argc <= 1) {
2070 struct print_diff_arg arg;
2071 struct got_pathlist_head paths;
2072 char *id_str;
2074 TAILQ_INIT(&paths);
2076 error = got_object_id_str(&id_str,
2077 got_worktree_get_base_commit_id(worktree));
2078 if (error)
2079 goto done;
2080 arg.repo = repo;
2081 arg.worktree = worktree;
2082 arg.diff_context = diff_context;
2083 arg.id_str = id_str;
2084 arg.header_shown = 0;
2085 arg.diff_staged = diff_staged;
2087 error = got_pathlist_append(&paths, path, NULL);
2088 if (error)
2089 goto done;
2091 error = got_worktree_status(worktree, &paths, repo, print_diff,
2092 &arg, check_cancelled, NULL);
2093 free(id_str);
2094 got_pathlist_free(&paths);
2095 goto done;
2098 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2099 if (error)
2100 goto done;
2102 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2103 if (error)
2104 goto done;
2106 error = got_object_get_type(&type1, repo, id1);
2107 if (error)
2108 goto done;
2110 error = got_object_get_type(&type2, repo, id2);
2111 if (error)
2112 goto done;
2114 if (type1 != type2) {
2115 error = got_error(GOT_ERR_OBJ_TYPE);
2116 goto done;
2119 switch (type1) {
2120 case GOT_OBJ_TYPE_BLOB:
2121 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2122 diff_context, repo, stdout);
2123 break;
2124 case GOT_OBJ_TYPE_TREE:
2125 error = got_diff_objects_as_trees(id1, id2, "", "",
2126 diff_context, repo, stdout);
2127 break;
2128 case GOT_OBJ_TYPE_COMMIT:
2129 printf("diff %s %s\n", label1, label2);
2130 error = got_diff_objects_as_commits(id1, id2, diff_context,
2131 repo, stdout);
2132 break;
2133 default:
2134 error = got_error(GOT_ERR_OBJ_TYPE);
2137 done:
2138 free(label1);
2139 free(label2);
2140 free(id1);
2141 free(id2);
2142 free(path);
2143 if (worktree)
2144 got_worktree_close(worktree);
2145 if (repo) {
2146 const struct got_error *repo_error;
2147 repo_error = got_repo_close(repo);
2148 if (error == NULL)
2149 error = repo_error;
2151 return error;
2154 __dead static void
2155 usage_blame(void)
2157 fprintf(stderr,
2158 "usage: %s blame [-c commit] [-r repository-path] path\n",
2159 getprogname());
2160 exit(1);
2163 struct blame_line {
2164 int annotated;
2165 char *id_str;
2166 char *committer;
2167 char datebuf[9]; /* YY-MM-DD + NUL */
2170 struct blame_cb_args {
2171 struct blame_line *lines;
2172 int nlines;
2173 int nlines_prec;
2174 int lineno_cur;
2175 off_t *line_offsets;
2176 FILE *f;
2177 struct got_repository *repo;
2180 static const struct got_error *
2181 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2183 const struct got_error *err = NULL;
2184 struct blame_cb_args *a = arg;
2185 struct blame_line *bline;
2186 char *line = NULL;
2187 size_t linesize = 0;
2188 struct got_commit_object *commit = NULL;
2189 off_t offset;
2190 struct tm tm;
2191 time_t committer_time;
2193 if (nlines != a->nlines ||
2194 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2195 return got_error(GOT_ERR_RANGE);
2197 if (sigint_received)
2198 return got_error(GOT_ERR_ITER_COMPLETED);
2200 if (lineno == -1)
2201 return NULL; /* no change in this commit */
2203 /* Annotate this line. */
2204 bline = &a->lines[lineno - 1];
2205 if (bline->annotated)
2206 return NULL;
2207 err = got_object_id_str(&bline->id_str, id);
2208 if (err)
2209 return err;
2211 err = got_object_open_as_commit(&commit, a->repo, id);
2212 if (err)
2213 goto done;
2215 bline->committer = strdup(got_object_commit_get_committer(commit));
2216 if (bline->committer == NULL) {
2217 err = got_error_from_errno("strdup");
2218 goto done;
2221 committer_time = got_object_commit_get_committer_time(commit);
2222 if (localtime_r(&committer_time, &tm) == NULL)
2223 return got_error_from_errno("localtime_r");
2224 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2225 &tm) >= sizeof(bline->datebuf)) {
2226 err = got_error(GOT_ERR_NO_SPACE);
2227 goto done;
2229 bline->annotated = 1;
2231 /* Print lines annotated so far. */
2232 bline = &a->lines[a->lineno_cur - 1];
2233 if (!bline->annotated)
2234 goto done;
2236 offset = a->line_offsets[a->lineno_cur - 1];
2237 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2238 err = got_error_from_errno("fseeko");
2239 goto done;
2242 while (bline->annotated) {
2243 char *smallerthan, *at, *nl, *committer;
2244 size_t len;
2246 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2247 if (ferror(a->f))
2248 err = got_error_from_errno("getline");
2249 break;
2252 committer = bline->committer;
2253 smallerthan = strchr(committer, '<');
2254 if (smallerthan && smallerthan[1] != '\0')
2255 committer = smallerthan + 1;
2256 at = strchr(committer, '@');
2257 if (at)
2258 *at = '\0';
2259 len = strlen(committer);
2260 if (len >= 9)
2261 committer[8] = '\0';
2263 nl = strchr(line, '\n');
2264 if (nl)
2265 *nl = '\0';
2266 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2267 bline->id_str, bline->datebuf, committer, line);
2269 a->lineno_cur++;
2270 bline = &a->lines[a->lineno_cur - 1];
2272 done:
2273 if (commit)
2274 got_object_commit_close(commit);
2275 free(line);
2276 return err;
2279 static const struct got_error *
2280 cmd_blame(int argc, char *argv[])
2282 const struct got_error *error;
2283 struct got_repository *repo = NULL;
2284 struct got_worktree *worktree = NULL;
2285 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2286 struct got_object_id *obj_id = NULL;
2287 struct got_object_id *commit_id = NULL;
2288 struct got_blob_object *blob = NULL;
2289 char *commit_id_str = NULL;
2290 struct blame_cb_args bca;
2291 int ch, obj_type, i;
2292 size_t filesize;
2294 memset(&bca, 0, sizeof(bca));
2296 #ifndef PROFILE
2297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2298 NULL) == -1)
2299 err(1, "pledge");
2300 #endif
2302 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2303 switch (ch) {
2304 case 'c':
2305 commit_id_str = optarg;
2306 break;
2307 case 'r':
2308 repo_path = realpath(optarg, NULL);
2309 if (repo_path == NULL)
2310 err(1, "-r option");
2311 got_path_strip_trailing_slashes(repo_path);
2312 break;
2313 default:
2314 usage_blame();
2315 /* NOTREACHED */
2319 argc -= optind;
2320 argv += optind;
2322 if (argc == 1)
2323 path = argv[0];
2324 else
2325 usage_blame();
2327 cwd = getcwd(NULL, 0);
2328 if (cwd == NULL) {
2329 error = got_error_from_errno("getcwd");
2330 goto done;
2332 if (repo_path == NULL) {
2333 error = got_worktree_open(&worktree, cwd);
2334 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2335 goto done;
2336 else
2337 error = NULL;
2338 if (worktree) {
2339 repo_path =
2340 strdup(got_worktree_get_repo_path(worktree));
2341 if (repo_path == NULL)
2342 error = got_error_from_errno("strdup");
2343 if (error)
2344 goto done;
2345 } else {
2346 repo_path = strdup(cwd);
2347 if (repo_path == NULL) {
2348 error = got_error_from_errno("strdup");
2349 goto done;
2354 error = got_repo_open(&repo, repo_path);
2355 if (error != NULL)
2356 goto done;
2358 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2359 if (error)
2360 goto done;
2362 if (worktree) {
2363 const char *prefix = got_worktree_get_path_prefix(worktree);
2364 char *p, *worktree_subdir = cwd +
2365 strlen(got_worktree_get_root_path(worktree));
2366 if (asprintf(&p, "%s%s%s%s%s",
2367 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2368 worktree_subdir, worktree_subdir[0] ? "/" : "",
2369 path) == -1) {
2370 error = got_error_from_errno("asprintf");
2371 goto done;
2373 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2374 free(p);
2375 } else {
2376 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2378 if (error)
2379 goto done;
2381 if (commit_id_str == NULL) {
2382 struct got_reference *head_ref;
2383 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2384 if (error != NULL)
2385 goto done;
2386 error = got_ref_resolve(&commit_id, repo, head_ref);
2387 got_ref_close(head_ref);
2388 if (error != NULL)
2389 goto done;
2390 } else {
2391 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2392 if (error)
2393 goto done;
2396 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2397 if (error)
2398 goto done;
2399 if (obj_id == NULL) {
2400 error = got_error(GOT_ERR_NO_OBJ);
2401 goto done;
2404 error = got_object_get_type(&obj_type, repo, obj_id);
2405 if (error)
2406 goto done;
2408 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2409 error = got_error(GOT_ERR_OBJ_TYPE);
2410 goto done;
2413 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2414 if (error)
2415 goto done;
2416 bca.f = got_opentemp();
2417 if (bca.f == NULL) {
2418 error = got_error_from_errno("got_opentemp");
2419 goto done;
2421 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2422 &bca.line_offsets, bca.f, blob);
2423 if (error || bca.nlines == 0)
2424 goto done;
2426 /* Don't include \n at EOF in the blame line count. */
2427 if (bca.line_offsets[bca.nlines - 1] == filesize)
2428 bca.nlines--;
2430 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2431 if (bca.lines == NULL) {
2432 error = got_error_from_errno("calloc");
2433 goto done;
2435 bca.lineno_cur = 1;
2436 bca.nlines_prec = 0;
2437 i = bca.nlines;
2438 while (i > 0) {
2439 i /= 10;
2440 bca.nlines_prec++;
2442 bca.repo = repo;
2444 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2445 check_cancelled, NULL);
2446 if (error)
2447 goto done;
2448 done:
2449 free(in_repo_path);
2450 free(repo_path);
2451 free(cwd);
2452 free(commit_id);
2453 free(obj_id);
2454 if (blob)
2455 got_object_blob_close(blob);
2456 if (worktree)
2457 got_worktree_close(worktree);
2458 if (repo) {
2459 const struct got_error *repo_error;
2460 repo_error = got_repo_close(repo);
2461 if (error == NULL)
2462 error = repo_error;
2464 for (i = 0; i < bca.nlines; i++) {
2465 struct blame_line *bline = &bca.lines[i];
2466 free(bline->id_str);
2467 free(bline->committer);
2469 free(bca.lines);
2470 free(bca.line_offsets);
2471 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2472 error = got_error_from_errno("fclose");
2473 return error;
2476 __dead static void
2477 usage_tree(void)
2479 fprintf(stderr,
2480 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2481 getprogname());
2482 exit(1);
2485 static void
2486 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2487 const char *root_path)
2489 int is_root_path = (strcmp(path, root_path) == 0);
2490 const char *modestr = "";
2492 path += strlen(root_path);
2493 while (path[0] == '/')
2494 path++;
2496 if (S_ISLNK(te->mode))
2497 modestr = "@";
2498 else if (S_ISDIR(te->mode))
2499 modestr = "/";
2500 else if (te->mode & S_IXUSR)
2501 modestr = "*";
2503 printf("%s%s%s%s%s\n", id ? id : "", path,
2504 is_root_path ? "" : "/", te->name, modestr);
2507 static const struct got_error *
2508 print_tree(const char *path, struct got_object_id *commit_id,
2509 int show_ids, int recurse, const char *root_path,
2510 struct got_repository *repo)
2512 const struct got_error *err = NULL;
2513 struct got_object_id *tree_id = NULL;
2514 struct got_tree_object *tree = NULL;
2515 const struct got_tree_entries *entries;
2516 struct got_tree_entry *te;
2518 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2519 if (err)
2520 goto done;
2522 err = got_object_open_as_tree(&tree, repo, tree_id);
2523 if (err)
2524 goto done;
2525 entries = got_object_tree_get_entries(tree);
2526 te = SIMPLEQ_FIRST(&entries->head);
2527 while (te) {
2528 char *id = NULL;
2530 if (sigint_received || sigpipe_received)
2531 break;
2533 if (show_ids) {
2534 char *id_str;
2535 err = got_object_id_str(&id_str, te->id);
2536 if (err)
2537 goto done;
2538 if (asprintf(&id, "%s ", id_str) == -1) {
2539 err = got_error_from_errno("asprintf");
2540 free(id_str);
2541 goto done;
2543 free(id_str);
2545 print_entry(te, id, path, root_path);
2546 free(id);
2548 if (recurse && S_ISDIR(te->mode)) {
2549 char *child_path;
2550 if (asprintf(&child_path, "%s%s%s", path,
2551 path[0] == '/' && path[1] == '\0' ? "" : "/",
2552 te->name) == -1) {
2553 err = got_error_from_errno("asprintf");
2554 goto done;
2556 err = print_tree(child_path, commit_id, show_ids, 1,
2557 root_path, repo);
2558 free(child_path);
2559 if (err)
2560 goto done;
2563 te = SIMPLEQ_NEXT(te, entry);
2565 done:
2566 if (tree)
2567 got_object_tree_close(tree);
2568 free(tree_id);
2569 return err;
2572 static const struct got_error *
2573 cmd_tree(int argc, char *argv[])
2575 const struct got_error *error;
2576 struct got_repository *repo = NULL;
2577 struct got_worktree *worktree = NULL;
2578 const char *path;
2579 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2580 struct got_object_id *commit_id = NULL;
2581 char *commit_id_str = NULL;
2582 int show_ids = 0, recurse = 0;
2583 int ch;
2585 #ifndef PROFILE
2586 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2587 NULL) == -1)
2588 err(1, "pledge");
2589 #endif
2591 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2592 switch (ch) {
2593 case 'c':
2594 commit_id_str = optarg;
2595 break;
2596 case 'r':
2597 repo_path = realpath(optarg, NULL);
2598 if (repo_path == NULL)
2599 err(1, "-r option");
2600 got_path_strip_trailing_slashes(repo_path);
2601 break;
2602 case 'i':
2603 show_ids = 1;
2604 break;
2605 case 'R':
2606 recurse = 1;
2607 break;
2608 default:
2609 usage_tree();
2610 /* NOTREACHED */
2614 argc -= optind;
2615 argv += optind;
2617 if (argc == 1)
2618 path = argv[0];
2619 else if (argc > 1)
2620 usage_tree();
2621 else
2622 path = NULL;
2624 cwd = getcwd(NULL, 0);
2625 if (cwd == NULL) {
2626 error = got_error_from_errno("getcwd");
2627 goto done;
2629 if (repo_path == NULL) {
2630 error = got_worktree_open(&worktree, cwd);
2631 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2632 goto done;
2633 else
2634 error = NULL;
2635 if (worktree) {
2636 repo_path =
2637 strdup(got_worktree_get_repo_path(worktree));
2638 if (repo_path == NULL)
2639 error = got_error_from_errno("strdup");
2640 if (error)
2641 goto done;
2642 } else {
2643 repo_path = strdup(cwd);
2644 if (repo_path == NULL) {
2645 error = got_error_from_errno("strdup");
2646 goto done;
2651 error = got_repo_open(&repo, repo_path);
2652 if (error != NULL)
2653 goto done;
2655 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2656 if (error)
2657 goto done;
2659 if (path == NULL) {
2660 if (worktree) {
2661 char *p, *worktree_subdir = cwd +
2662 strlen(got_worktree_get_root_path(worktree));
2663 if (asprintf(&p, "%s/%s",
2664 got_worktree_get_path_prefix(worktree),
2665 worktree_subdir) == -1) {
2666 error = got_error_from_errno("asprintf");
2667 goto done;
2669 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2670 free(p);
2671 if (error)
2672 goto done;
2673 } else
2674 path = "/";
2676 if (in_repo_path == NULL) {
2677 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2678 if (error != NULL)
2679 goto done;
2682 if (commit_id_str == NULL) {
2683 struct got_reference *head_ref;
2684 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2685 if (error != NULL)
2686 goto done;
2687 error = got_ref_resolve(&commit_id, repo, head_ref);
2688 got_ref_close(head_ref);
2689 if (error != NULL)
2690 goto done;
2691 } else {
2692 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2693 if (error)
2694 goto done;
2697 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2698 in_repo_path, repo);
2699 done:
2700 free(in_repo_path);
2701 free(repo_path);
2702 free(cwd);
2703 free(commit_id);
2704 if (worktree)
2705 got_worktree_close(worktree);
2706 if (repo) {
2707 const struct got_error *repo_error;
2708 repo_error = got_repo_close(repo);
2709 if (error == NULL)
2710 error = repo_error;
2712 return error;
2715 __dead static void
2716 usage_status(void)
2718 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2719 exit(1);
2722 static const struct got_error *
2723 print_status(void *arg, unsigned char status, unsigned char staged_status,
2724 const char *path, struct got_object_id *blob_id,
2725 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2727 if (status == staged_status && (status == GOT_STATUS_DELETE))
2728 status = GOT_STATUS_NO_CHANGE;
2729 printf("%c%c %s\n", status, staged_status, path);
2730 return NULL;
2733 static const struct got_error *
2734 cmd_status(int argc, char *argv[])
2736 const struct got_error *error = NULL;
2737 struct got_repository *repo = NULL;
2738 struct got_worktree *worktree = NULL;
2739 char *cwd = NULL;
2740 struct got_pathlist_head paths;
2741 struct got_pathlist_entry *pe;
2742 int ch;
2744 TAILQ_INIT(&paths);
2746 while ((ch = getopt(argc, argv, "")) != -1) {
2747 switch (ch) {
2748 default:
2749 usage_status();
2750 /* NOTREACHED */
2754 argc -= optind;
2755 argv += optind;
2757 #ifndef PROFILE
2758 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2759 NULL) == -1)
2760 err(1, "pledge");
2761 #endif
2762 cwd = getcwd(NULL, 0);
2763 if (cwd == NULL) {
2764 error = got_error_from_errno("getcwd");
2765 goto done;
2768 error = got_worktree_open(&worktree, cwd);
2769 if (error != NULL)
2770 goto done;
2772 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2773 if (error != NULL)
2774 goto done;
2776 error = apply_unveil(got_repo_get_path(repo), 1,
2777 got_worktree_get_root_path(worktree));
2778 if (error)
2779 goto done;
2781 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2782 if (error)
2783 goto done;
2785 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2786 check_cancelled, NULL);
2787 done:
2788 TAILQ_FOREACH(pe, &paths, entry)
2789 free((char *)pe->path);
2790 got_pathlist_free(&paths);
2791 free(cwd);
2792 return error;
2795 __dead static void
2796 usage_ref(void)
2798 fprintf(stderr,
2799 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2800 getprogname());
2801 exit(1);
2804 static const struct got_error *
2805 list_refs(struct got_repository *repo)
2807 static const struct got_error *err = NULL;
2808 struct got_reflist_head refs;
2809 struct got_reflist_entry *re;
2811 SIMPLEQ_INIT(&refs);
2812 err = got_ref_list(&refs, repo);
2813 if (err)
2814 return err;
2816 SIMPLEQ_FOREACH(re, &refs, entry) {
2817 char *refstr;
2818 refstr = got_ref_to_str(re->ref);
2819 if (refstr == NULL)
2820 return got_error_from_errno("got_ref_to_str");
2821 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2822 free(refstr);
2825 got_ref_list_free(&refs);
2826 return NULL;
2829 static const struct got_error *
2830 delete_ref(struct got_repository *repo, const char *refname)
2832 const struct got_error *err = NULL;
2833 struct got_reference *ref;
2835 err = got_ref_open(&ref, repo, refname, 0);
2836 if (err)
2837 return err;
2839 err = got_ref_delete(ref, repo);
2840 got_ref_close(ref);
2841 return err;
2844 static const struct got_error *
2845 add_ref(struct got_repository *repo, const char *refname, const char *target)
2847 const struct got_error *err = NULL;
2848 struct got_object_id *id;
2849 struct got_reference *ref = NULL;
2852 * Don't let the user create a reference named '-'.
2853 * While technically a valid reference name, this case is usually
2854 * an unintended typo.
2856 if (refname[0] == '-' && refname[1] == '\0')
2857 return got_error(GOT_ERR_BAD_REF_NAME);
2859 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2860 repo);
2861 if (err) {
2862 struct got_reference *target_ref;
2864 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2865 return err;
2866 err = got_ref_open(&target_ref, repo, target, 0);
2867 if (err)
2868 return err;
2869 err = got_ref_resolve(&id, repo, target_ref);
2870 got_ref_close(target_ref);
2871 if (err)
2872 return err;
2875 err = got_ref_alloc(&ref, refname, id);
2876 if (err)
2877 goto done;
2879 err = got_ref_write(ref, repo);
2880 done:
2881 if (ref)
2882 got_ref_close(ref);
2883 free(id);
2884 return err;
2887 static const struct got_error *
2888 add_symref(struct got_repository *repo, const char *refname, const char *target)
2890 const struct got_error *err = NULL;
2891 struct got_reference *ref = NULL;
2892 struct got_reference *target_ref = NULL;
2895 * Don't let the user create a reference named '-'.
2896 * While technically a valid reference name, this case is usually
2897 * an unintended typo.
2899 if (refname[0] == '-' && refname[1] == '\0')
2900 return got_error(GOT_ERR_BAD_REF_NAME);
2902 err = got_ref_open(&target_ref, repo, target, 0);
2903 if (err)
2904 return err;
2906 err = got_ref_alloc_symref(&ref, refname, target_ref);
2907 if (err)
2908 goto done;
2910 err = got_ref_write(ref, repo);
2911 done:
2912 if (target_ref)
2913 got_ref_close(target_ref);
2914 if (ref)
2915 got_ref_close(ref);
2916 return err;
2919 static const struct got_error *
2920 cmd_ref(int argc, char *argv[])
2922 const struct got_error *error = NULL;
2923 struct got_repository *repo = NULL;
2924 struct got_worktree *worktree = NULL;
2925 char *cwd = NULL, *repo_path = NULL;
2926 int ch, do_list = 0, create_symref = 0;
2927 const char *delref = NULL;
2929 /* TODO: Add -s option for adding symbolic references. */
2930 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2931 switch (ch) {
2932 case 'd':
2933 delref = optarg;
2934 break;
2935 case 'r':
2936 repo_path = realpath(optarg, NULL);
2937 if (repo_path == NULL)
2938 err(1, "-r option");
2939 got_path_strip_trailing_slashes(repo_path);
2940 break;
2941 case 'l':
2942 do_list = 1;
2943 break;
2944 case 's':
2945 create_symref = 1;
2946 break;
2947 default:
2948 usage_ref();
2949 /* NOTREACHED */
2953 if (do_list && delref)
2954 errx(1, "-l and -d options are mutually exclusive\n");
2956 argc -= optind;
2957 argv += optind;
2959 if (do_list || delref) {
2960 if (create_symref)
2961 errx(1, "-s option cannot be used together with the "
2962 "-l or -d options");
2963 if (argc > 0)
2964 usage_ref();
2965 } else if (argc != 2)
2966 usage_ref();
2968 #ifndef PROFILE
2969 if (do_list) {
2970 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2971 NULL) == -1)
2972 err(1, "pledge");
2973 } else {
2974 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2975 "sendfd unveil", NULL) == -1)
2976 err(1, "pledge");
2978 #endif
2979 cwd = getcwd(NULL, 0);
2980 if (cwd == NULL) {
2981 error = got_error_from_errno("getcwd");
2982 goto done;
2985 if (repo_path == NULL) {
2986 error = got_worktree_open(&worktree, cwd);
2987 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2988 goto done;
2989 else
2990 error = NULL;
2991 if (worktree) {
2992 repo_path =
2993 strdup(got_worktree_get_repo_path(worktree));
2994 if (repo_path == NULL)
2995 error = got_error_from_errno("strdup");
2996 if (error)
2997 goto done;
2998 } else {
2999 repo_path = strdup(cwd);
3000 if (repo_path == NULL) {
3001 error = got_error_from_errno("strdup");
3002 goto done;
3007 error = got_repo_open(&repo, repo_path);
3008 if (error != NULL)
3009 goto done;
3011 error = apply_unveil(got_repo_get_path(repo), do_list,
3012 worktree ? got_worktree_get_root_path(worktree) : NULL);
3013 if (error)
3014 goto done;
3016 if (do_list)
3017 error = list_refs(repo);
3018 else if (delref)
3019 error = delete_ref(repo, delref);
3020 else if (create_symref)
3021 error = add_symref(repo, argv[0], argv[1]);
3022 else
3023 error = add_ref(repo, argv[0], argv[1]);
3024 done:
3025 if (repo)
3026 got_repo_close(repo);
3027 if (worktree)
3028 got_worktree_close(worktree);
3029 free(cwd);
3030 free(repo_path);
3031 return error;
3034 __dead static void
3035 usage_branch(void)
3037 fprintf(stderr,
3038 "usage: %s branch [-r repository] -l | -d name | "
3039 "name [base-branch]\n", getprogname());
3040 exit(1);
3043 static const struct got_error *
3044 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3046 static const struct got_error *err = NULL;
3047 struct got_reflist_head refs;
3048 struct got_reflist_entry *re;
3050 SIMPLEQ_INIT(&refs);
3052 err = got_ref_list(&refs, repo);
3053 if (err)
3054 return err;
3056 SIMPLEQ_FOREACH(re, &refs, entry) {
3057 const char *refname, *marker = " ";
3058 char *refstr;
3059 refname = got_ref_get_name(re->ref);
3060 if (strncmp(refname, "refs/heads/", 11) != 0)
3061 continue;
3062 if (worktree && strcmp(refname,
3063 got_worktree_get_head_ref_name(worktree)) == 0) {
3064 struct got_object_id *id = NULL;
3065 err = got_ref_resolve(&id, repo, re->ref);
3066 if (err)
3067 return err;
3068 if (got_object_id_cmp(id,
3069 got_worktree_get_base_commit_id(worktree)) == 0)
3070 marker = "* ";
3071 else
3072 marker = "~ ";
3073 free(id);
3075 refname += 11;
3076 refstr = got_ref_to_str(re->ref);
3077 if (refstr == NULL)
3078 return got_error_from_errno("got_ref_to_str");
3079 printf("%s%s: %s\n", marker, refname, refstr);
3080 free(refstr);
3083 got_ref_list_free(&refs);
3084 return NULL;
3087 static const struct got_error *
3088 delete_branch(struct got_repository *repo, const char *branch_name)
3090 const struct got_error *err = NULL;
3091 struct got_reference *ref;
3092 char *refname;
3094 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3095 return got_error_from_errno("asprintf");
3097 err = got_ref_open(&ref, repo, refname, 0);
3098 if (err)
3099 goto done;
3101 err = got_ref_delete(ref, repo);
3102 got_ref_close(ref);
3103 done:
3104 free(refname);
3105 return err;
3108 static const struct got_error *
3109 add_branch(struct got_repository *repo, const char *branch_name,
3110 const char *base_branch)
3112 const struct got_error *err = NULL;
3113 struct got_object_id *id = NULL;
3114 struct got_reference *ref = NULL;
3115 char *base_refname = NULL, *refname = NULL;
3116 struct got_reference *base_ref;
3119 * Don't let the user create a branch named '-'.
3120 * While technically a valid reference name, this case is usually
3121 * an unintended typo.
3123 if (branch_name[0] == '-' && branch_name[1] == '\0')
3124 return got_error(GOT_ERR_BAD_REF_NAME);
3126 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3127 base_refname = strdup(GOT_REF_HEAD);
3128 if (base_refname == NULL)
3129 return got_error_from_errno("strdup");
3130 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3131 return got_error_from_errno("asprintf");
3133 err = got_ref_open(&base_ref, repo, base_refname, 0);
3134 if (err)
3135 goto done;
3136 err = got_ref_resolve(&id, repo, base_ref);
3137 got_ref_close(base_ref);
3138 if (err)
3139 goto done;
3141 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3142 err = got_error_from_errno("asprintf");
3143 goto done;
3146 err = got_ref_open(&ref, repo, refname, 0);
3147 if (err == NULL) {
3148 err = got_error(GOT_ERR_BRANCH_EXISTS);
3149 goto done;
3150 } else if (err->code != GOT_ERR_NOT_REF)
3151 goto done;
3153 err = got_ref_alloc(&ref, refname, id);
3154 if (err)
3155 goto done;
3157 err = got_ref_write(ref, repo);
3158 done:
3159 if (ref)
3160 got_ref_close(ref);
3161 free(id);
3162 free(base_refname);
3163 free(refname);
3164 return err;
3167 static const struct got_error *
3168 cmd_branch(int argc, char *argv[])
3170 const struct got_error *error = NULL;
3171 struct got_repository *repo = NULL;
3172 struct got_worktree *worktree = NULL;
3173 char *cwd = NULL, *repo_path = NULL;
3174 int ch, do_list = 0;
3175 const char *delref = NULL;
3177 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3178 switch (ch) {
3179 case 'd':
3180 delref = optarg;
3181 break;
3182 case 'r':
3183 repo_path = realpath(optarg, NULL);
3184 if (repo_path == NULL)
3185 err(1, "-r option");
3186 got_path_strip_trailing_slashes(repo_path);
3187 break;
3188 case 'l':
3189 do_list = 1;
3190 break;
3191 default:
3192 usage_branch();
3193 /* NOTREACHED */
3197 if (do_list && delref)
3198 errx(1, "-l and -d options are mutually exclusive\n");
3200 argc -= optind;
3201 argv += optind;
3203 if (do_list || delref) {
3204 if (argc > 0)
3205 usage_branch();
3206 } else if (argc < 1 || argc > 2)
3207 usage_branch();
3209 #ifndef PROFILE
3210 if (do_list) {
3211 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3212 NULL) == -1)
3213 err(1, "pledge");
3214 } else {
3215 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3216 "sendfd unveil", NULL) == -1)
3217 err(1, "pledge");
3219 #endif
3220 cwd = getcwd(NULL, 0);
3221 if (cwd == NULL) {
3222 error = got_error_from_errno("getcwd");
3223 goto done;
3226 if (repo_path == NULL) {
3227 error = got_worktree_open(&worktree, cwd);
3228 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3229 goto done;
3230 else
3231 error = NULL;
3232 if (worktree) {
3233 repo_path =
3234 strdup(got_worktree_get_repo_path(worktree));
3235 if (repo_path == NULL)
3236 error = got_error_from_errno("strdup");
3237 if (error)
3238 goto done;
3239 } else {
3240 repo_path = strdup(cwd);
3241 if (repo_path == NULL) {
3242 error = got_error_from_errno("strdup");
3243 goto done;
3248 error = got_repo_open(&repo, repo_path);
3249 if (error != NULL)
3250 goto done;
3252 error = apply_unveil(got_repo_get_path(repo), do_list,
3253 worktree ? got_worktree_get_root_path(worktree) : NULL);
3254 if (error)
3255 goto done;
3257 if (do_list)
3258 error = list_branches(repo, worktree);
3259 else if (delref)
3260 error = delete_branch(repo, delref);
3261 else {
3262 const char *base_branch;
3263 if (argc == 1) {
3264 base_branch = worktree ?
3265 got_worktree_get_head_ref_name(worktree) :
3266 GOT_REF_HEAD;
3267 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3268 base_branch += 11;
3269 } else
3270 base_branch = argv[1];
3271 error = add_branch(repo, argv[0], base_branch);
3273 done:
3274 if (repo)
3275 got_repo_close(repo);
3276 if (worktree)
3277 got_worktree_close(worktree);
3278 free(cwd);
3279 free(repo_path);
3280 return error;
3283 __dead static void
3284 usage_add(void)
3286 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3287 exit(1);
3290 static const struct got_error *
3291 cmd_add(int argc, char *argv[])
3293 const struct got_error *error = NULL;
3294 struct got_repository *repo = NULL;
3295 struct got_worktree *worktree = NULL;
3296 char *cwd = NULL;
3297 struct got_pathlist_head paths;
3298 struct got_pathlist_entry *pe;
3299 int ch;
3301 TAILQ_INIT(&paths);
3303 while ((ch = getopt(argc, argv, "")) != -1) {
3304 switch (ch) {
3305 default:
3306 usage_add();
3307 /* NOTREACHED */
3311 argc -= optind;
3312 argv += optind;
3314 #ifndef PROFILE
3315 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3316 NULL) == -1)
3317 err(1, "pledge");
3318 #endif
3319 if (argc < 1)
3320 usage_add();
3322 cwd = getcwd(NULL, 0);
3323 if (cwd == NULL) {
3324 error = got_error_from_errno("getcwd");
3325 goto done;
3328 error = got_worktree_open(&worktree, cwd);
3329 if (error)
3330 goto done;
3332 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3333 if (error != NULL)
3334 goto done;
3336 error = apply_unveil(got_repo_get_path(repo), 1,
3337 got_worktree_get_root_path(worktree));
3338 if (error)
3339 goto done;
3341 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3342 if (error)
3343 goto done;
3345 error = got_worktree_schedule_add(worktree, &paths, print_status,
3346 NULL, repo);
3347 done:
3348 if (repo)
3349 got_repo_close(repo);
3350 if (worktree)
3351 got_worktree_close(worktree);
3352 TAILQ_FOREACH(pe, &paths, entry)
3353 free((char *)pe->path);
3354 got_pathlist_free(&paths);
3355 free(cwd);
3356 return error;
3359 __dead static void
3360 usage_remove(void)
3362 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3363 exit(1);
3366 static const struct got_error *
3367 cmd_remove(int argc, char *argv[])
3369 const struct got_error *error = NULL;
3370 struct got_worktree *worktree = NULL;
3371 struct got_repository *repo = NULL;
3372 char *cwd = NULL;
3373 struct got_pathlist_head paths;
3374 struct got_pathlist_entry *pe;
3375 int ch, delete_local_mods = 0;
3377 TAILQ_INIT(&paths);
3379 while ((ch = getopt(argc, argv, "f")) != -1) {
3380 switch (ch) {
3381 case 'f':
3382 delete_local_mods = 1;
3383 break;
3384 default:
3385 usage_add();
3386 /* NOTREACHED */
3390 argc -= optind;
3391 argv += optind;
3393 #ifndef PROFILE
3394 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3395 NULL) == -1)
3396 err(1, "pledge");
3397 #endif
3398 if (argc < 1)
3399 usage_remove();
3401 cwd = getcwd(NULL, 0);
3402 if (cwd == NULL) {
3403 error = got_error_from_errno("getcwd");
3404 goto done;
3406 error = got_worktree_open(&worktree, cwd);
3407 if (error)
3408 goto done;
3410 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3411 if (error)
3412 goto done;
3414 error = apply_unveil(got_repo_get_path(repo), 1,
3415 got_worktree_get_root_path(worktree));
3416 if (error)
3417 goto done;
3419 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3420 if (error)
3421 goto done;
3423 error = got_worktree_schedule_delete(worktree, &paths,
3424 delete_local_mods, print_status, NULL, repo);
3425 if (error)
3426 goto done;
3427 done:
3428 if (repo)
3429 got_repo_close(repo);
3430 if (worktree)
3431 got_worktree_close(worktree);
3432 TAILQ_FOREACH(pe, &paths, entry)
3433 free((char *)pe->path);
3434 got_pathlist_free(&paths);
3435 free(cwd);
3436 return error;
3439 __dead static void
3440 usage_revert(void)
3442 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3443 "path ...\n", getprogname());
3444 exit(1);
3447 static const struct got_error *
3448 revert_progress(void *arg, unsigned char status, const char *path)
3450 while (path[0] == '/')
3451 path++;
3452 printf("%c %s\n", status, path);
3453 return NULL;
3456 struct choose_patch_arg {
3457 FILE *patch_script_file;
3458 const char *action;
3461 static const struct got_error *
3462 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3463 int nchanges, const char *action)
3465 char *line = NULL;
3466 size_t linesize = 0;
3467 ssize_t linelen;
3469 switch (status) {
3470 case GOT_STATUS_ADD:
3471 printf("A %s\n%s this addition? [y/n] ", path, action);
3472 break;
3473 case GOT_STATUS_DELETE:
3474 printf("D %s\n%s this deletion? [y/n] ", path, action);
3475 break;
3476 case GOT_STATUS_MODIFY:
3477 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3478 return got_error_from_errno("fseek");
3479 printf(GOT_COMMIT_SEP_STR);
3480 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3481 printf("%s", line);
3482 if (ferror(patch_file))
3483 return got_error_from_errno("getline");
3484 printf(GOT_COMMIT_SEP_STR);
3485 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3486 path, n, nchanges, action);
3487 break;
3488 default:
3489 return got_error_path(path, GOT_ERR_FILE_STATUS);
3492 return NULL;
3495 static const struct got_error *
3496 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3497 FILE *patch_file, int n, int nchanges)
3499 const struct got_error *err = NULL;
3500 char *line = NULL;
3501 size_t linesize = 0;
3502 ssize_t linelen;
3503 int resp = ' ';
3504 struct choose_patch_arg *a = arg;
3506 *choice = GOT_PATCH_CHOICE_NONE;
3508 if (a->patch_script_file) {
3509 char *nl;
3510 err = show_change(status, path, patch_file, n, nchanges,
3511 a->action);
3512 if (err)
3513 return err;
3514 linelen = getline(&line, &linesize, a->patch_script_file);
3515 if (linelen == -1) {
3516 if (ferror(a->patch_script_file))
3517 return got_error_from_errno("getline");
3518 return NULL;
3520 nl = strchr(line, '\n');
3521 if (nl)
3522 *nl = '\0';
3523 if (strcmp(line, "y") == 0) {
3524 *choice = GOT_PATCH_CHOICE_YES;
3525 printf("y\n");
3526 } else if (strcmp(line, "n") == 0) {
3527 *choice = GOT_PATCH_CHOICE_NO;
3528 printf("n\n");
3529 } else if (strcmp(line, "q") == 0 &&
3530 status == GOT_STATUS_MODIFY) {
3531 *choice = GOT_PATCH_CHOICE_QUIT;
3532 printf("q\n");
3533 } else
3534 printf("invalid response '%s'\n", line);
3535 free(line);
3536 return NULL;
3539 while (resp != 'y' && resp != 'n' && resp != 'q') {
3540 err = show_change(status, path, patch_file, n, nchanges,
3541 a->action);
3542 if (err)
3543 return err;
3544 resp = getchar();
3545 if (resp == '\n')
3546 resp = getchar();
3547 if (status == GOT_STATUS_MODIFY) {
3548 if (resp != 'y' && resp != 'n' && resp != 'q') {
3549 printf("invalid response '%c'\n", resp);
3550 resp = ' ';
3552 } else if (resp != 'y' && resp != 'n') {
3553 printf("invalid response '%c'\n", resp);
3554 resp = ' ';
3558 if (resp == 'y')
3559 *choice = GOT_PATCH_CHOICE_YES;
3560 else if (resp == 'n')
3561 *choice = GOT_PATCH_CHOICE_NO;
3562 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3563 *choice = GOT_PATCH_CHOICE_QUIT;
3565 return NULL;
3569 static const struct got_error *
3570 cmd_revert(int argc, char *argv[])
3572 const struct got_error *error = NULL;
3573 struct got_worktree *worktree = NULL;
3574 struct got_repository *repo = NULL;
3575 char *cwd = NULL, *path = NULL;
3576 struct got_pathlist_head paths;
3577 struct got_pathlist_entry *pe;
3578 int ch, can_recurse = 0, pflag = 0;
3579 FILE *patch_script_file = NULL;
3580 const char *patch_script_path = NULL;
3581 struct choose_patch_arg cpa;
3583 TAILQ_INIT(&paths);
3585 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3586 switch (ch) {
3587 case 'p':
3588 pflag = 1;
3589 break;
3590 case 'F':
3591 patch_script_path = optarg;
3592 break;
3593 case 'R':
3594 can_recurse = 1;
3595 break;
3596 default:
3597 usage_revert();
3598 /* NOTREACHED */
3602 argc -= optind;
3603 argv += optind;
3605 #ifndef PROFILE
3606 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3607 "unveil", NULL) == -1)
3608 err(1, "pledge");
3609 #endif
3610 if (argc < 1)
3611 usage_revert();
3612 if (patch_script_path && !pflag)
3613 errx(1, "-F option can only be used together with -p option");
3615 cwd = getcwd(NULL, 0);
3616 if (cwd == NULL) {
3617 error = got_error_from_errno("getcwd");
3618 goto done;
3620 error = got_worktree_open(&worktree, cwd);
3621 if (error)
3622 goto done;
3624 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3625 if (error != NULL)
3626 goto done;
3628 if (patch_script_path) {
3629 patch_script_file = fopen(patch_script_path, "r");
3630 if (patch_script_file == NULL) {
3631 error = got_error_from_errno2("fopen",
3632 patch_script_path);
3633 goto done;
3636 error = apply_unveil(got_repo_get_path(repo), 1,
3637 got_worktree_get_root_path(worktree));
3638 if (error)
3639 goto done;
3641 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3642 if (error)
3643 goto done;
3645 if (!can_recurse) {
3646 char *ondisk_path;
3647 struct stat sb;
3648 TAILQ_FOREACH(pe, &paths, entry) {
3649 if (asprintf(&ondisk_path, "%s/%s",
3650 got_worktree_get_root_path(worktree),
3651 pe->path) == -1) {
3652 error = got_error_from_errno("asprintf");
3653 goto done;
3655 if (lstat(ondisk_path, &sb) == -1) {
3656 if (errno == ENOENT) {
3657 free(ondisk_path);
3658 continue;
3660 error = got_error_from_errno2("lstat",
3661 ondisk_path);
3662 free(ondisk_path);
3663 goto done;
3665 free(ondisk_path);
3666 if (S_ISDIR(sb.st_mode)) {
3667 error = got_error_msg(GOT_ERR_BAD_PATH,
3668 "reverting directories requires -R option");
3669 goto done;
3674 cpa.patch_script_file = patch_script_file;
3675 cpa.action = "revert";
3676 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3677 pflag ? choose_patch : NULL, &cpa, repo);
3678 if (error)
3679 goto done;
3680 done:
3681 if (patch_script_file && fclose(patch_script_file) == EOF &&
3682 error == NULL)
3683 error = got_error_from_errno2("fclose", patch_script_path);
3684 if (repo)
3685 got_repo_close(repo);
3686 if (worktree)
3687 got_worktree_close(worktree);
3688 free(path);
3689 free(cwd);
3690 return error;
3693 __dead static void
3694 usage_commit(void)
3696 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3697 getprogname());
3698 exit(1);
3701 struct collect_commit_logmsg_arg {
3702 const char *cmdline_log;
3703 const char *editor;
3704 const char *worktree_path;
3705 const char *branch_name;
3706 const char *repo_path;
3707 char *logmsg_path;
3711 static const struct got_error *
3712 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3713 void *arg)
3715 char *initial_content = NULL;
3716 struct got_pathlist_entry *pe;
3717 const struct got_error *err = NULL;
3718 char *template = NULL;
3719 struct collect_commit_logmsg_arg *a = arg;
3720 int fd;
3721 size_t len;
3723 /* if a message was specified on the command line, just use it */
3724 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3725 len = strlen(a->cmdline_log) + 1;
3726 *logmsg = malloc(len + 1);
3727 if (*logmsg == NULL)
3728 return got_error_from_errno("malloc");
3729 strlcpy(*logmsg, a->cmdline_log, len);
3730 return NULL;
3733 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3734 return got_error_from_errno("asprintf");
3736 if (asprintf(&initial_content,
3737 "\n# changes to be committed on branch %s:\n",
3738 a->branch_name) == -1)
3739 return got_error_from_errno("asprintf");
3741 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3742 if (err)
3743 goto done;
3745 dprintf(fd, initial_content);
3747 TAILQ_FOREACH(pe, commitable_paths, entry) {
3748 struct got_commitable *ct = pe->data;
3749 dprintf(fd, "# %c %s\n",
3750 got_commitable_get_status(ct),
3751 got_commitable_get_path(ct));
3753 close(fd);
3755 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3756 done:
3757 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3758 unlink(a->logmsg_path);
3759 free(a->logmsg_path);
3760 a->logmsg_path = NULL;
3762 free(initial_content);
3763 free(template);
3765 /* Editor is done; we can now apply unveil(2) */
3766 if (err == NULL) {
3767 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3768 if (err) {
3769 free(*logmsg);
3770 *logmsg = NULL;
3773 return err;
3776 static const struct got_error *
3777 cmd_commit(int argc, char *argv[])
3779 const struct got_error *error = NULL;
3780 struct got_worktree *worktree = NULL;
3781 struct got_repository *repo = NULL;
3782 char *cwd = NULL, *id_str = NULL;
3783 struct got_object_id *id = NULL;
3784 const char *logmsg = NULL;
3785 const char *author;
3786 struct collect_commit_logmsg_arg cl_arg;
3787 char *editor = NULL;
3788 int ch, rebase_in_progress, histedit_in_progress;
3789 struct got_pathlist_head paths;
3791 TAILQ_INIT(&paths);
3792 cl_arg.logmsg_path = NULL;
3794 while ((ch = getopt(argc, argv, "m:")) != -1) {
3795 switch (ch) {
3796 case 'm':
3797 logmsg = optarg;
3798 break;
3799 default:
3800 usage_commit();
3801 /* NOTREACHED */
3805 argc -= optind;
3806 argv += optind;
3808 #ifndef PROFILE
3809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3810 "unveil", NULL) == -1)
3811 err(1, "pledge");
3812 #endif
3813 error = get_author(&author);
3814 if (error)
3815 return error;
3817 cwd = getcwd(NULL, 0);
3818 if (cwd == NULL) {
3819 error = got_error_from_errno("getcwd");
3820 goto done;
3822 error = got_worktree_open(&worktree, cwd);
3823 if (error)
3824 goto done;
3826 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3827 if (error)
3828 goto done;
3829 if (rebase_in_progress) {
3830 error = got_error(GOT_ERR_REBASING);
3831 goto done;
3834 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3835 worktree);
3836 if (error)
3837 goto done;
3839 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3840 if (error != NULL)
3841 goto done;
3844 * unveil(2) traverses exec(2); if an editor is used we have
3845 * to apply unveil after the log message has been written.
3847 if (logmsg == NULL || strlen(logmsg) == 0)
3848 error = get_editor(&editor);
3849 else
3850 error = apply_unveil(got_repo_get_path(repo), 0,
3851 got_worktree_get_root_path(worktree));
3852 if (error)
3853 goto done;
3855 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3856 if (error)
3857 goto done;
3859 cl_arg.editor = editor;
3860 cl_arg.cmdline_log = logmsg;
3861 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3862 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3863 if (!histedit_in_progress) {
3864 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3865 error = got_error(GOT_ERR_COMMIT_BRANCH);
3866 goto done;
3868 cl_arg.branch_name += 11;
3870 cl_arg.repo_path = got_repo_get_path(repo);
3871 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3872 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3873 if (error) {
3874 if (cl_arg.logmsg_path)
3875 fprintf(stderr, "%s: log message preserved in %s\n",
3876 getprogname(), cl_arg.logmsg_path);
3877 goto done;
3880 if (cl_arg.logmsg_path)
3881 unlink(cl_arg.logmsg_path);
3883 error = got_object_id_str(&id_str, id);
3884 if (error)
3885 goto done;
3886 printf("Created commit %s\n", id_str);
3887 done:
3888 free(cl_arg.logmsg_path);
3889 if (repo)
3890 got_repo_close(repo);
3891 if (worktree)
3892 got_worktree_close(worktree);
3893 free(cwd);
3894 free(id_str);
3895 free(editor);
3896 return error;
3899 __dead static void
3900 usage_cherrypick(void)
3902 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3903 exit(1);
3906 static const struct got_error *
3907 cmd_cherrypick(int argc, char *argv[])
3909 const struct got_error *error = NULL;
3910 struct got_worktree *worktree = NULL;
3911 struct got_repository *repo = NULL;
3912 char *cwd = NULL, *commit_id_str = NULL;
3913 struct got_object_id *commit_id = NULL;
3914 struct got_commit_object *commit = NULL;
3915 struct got_object_qid *pid;
3916 struct got_reference *head_ref = NULL;
3917 int ch, did_something = 0;
3919 while ((ch = getopt(argc, argv, "")) != -1) {
3920 switch (ch) {
3921 default:
3922 usage_cherrypick();
3923 /* NOTREACHED */
3927 argc -= optind;
3928 argv += optind;
3930 #ifndef PROFILE
3931 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3932 "unveil", NULL) == -1)
3933 err(1, "pledge");
3934 #endif
3935 if (argc != 1)
3936 usage_cherrypick();
3938 cwd = getcwd(NULL, 0);
3939 if (cwd == NULL) {
3940 error = got_error_from_errno("getcwd");
3941 goto done;
3943 error = got_worktree_open(&worktree, cwd);
3944 if (error)
3945 goto done;
3947 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3948 if (error != NULL)
3949 goto done;
3951 error = apply_unveil(got_repo_get_path(repo), 0,
3952 got_worktree_get_root_path(worktree));
3953 if (error)
3954 goto done;
3956 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3957 GOT_OBJ_TYPE_COMMIT, repo);
3958 if (error != NULL) {
3959 struct got_reference *ref;
3960 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3961 goto done;
3962 error = got_ref_open(&ref, repo, argv[0], 0);
3963 if (error != NULL)
3964 goto done;
3965 error = got_ref_resolve(&commit_id, repo, ref);
3966 got_ref_close(ref);
3967 if (error != NULL)
3968 goto done;
3970 error = got_object_id_str(&commit_id_str, commit_id);
3971 if (error)
3972 goto done;
3974 error = got_ref_open(&head_ref, repo,
3975 got_worktree_get_head_ref_name(worktree), 0);
3976 if (error != NULL)
3977 goto done;
3979 error = check_same_branch(commit_id, head_ref, NULL, repo);
3980 if (error) {
3981 if (error->code != GOT_ERR_ANCESTRY)
3982 goto done;
3983 error = NULL;
3984 } else {
3985 error = got_error(GOT_ERR_SAME_BRANCH);
3986 goto done;
3989 error = got_object_open_as_commit(&commit, repo, commit_id);
3990 if (error)
3991 goto done;
3992 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3993 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3994 commit_id, repo, update_progress, &did_something, check_cancelled,
3995 NULL);
3996 if (error != NULL)
3997 goto done;
3999 if (did_something)
4000 printf("Merged commit %s\n", commit_id_str);
4001 done:
4002 if (commit)
4003 got_object_commit_close(commit);
4004 free(commit_id_str);
4005 if (head_ref)
4006 got_ref_close(head_ref);
4007 if (worktree)
4008 got_worktree_close(worktree);
4009 if (repo)
4010 got_repo_close(repo);
4011 return error;
4014 __dead static void
4015 usage_backout(void)
4017 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4018 exit(1);
4021 static const struct got_error *
4022 cmd_backout(int argc, char *argv[])
4024 const struct got_error *error = NULL;
4025 struct got_worktree *worktree = NULL;
4026 struct got_repository *repo = NULL;
4027 char *cwd = NULL, *commit_id_str = NULL;
4028 struct got_object_id *commit_id = NULL;
4029 struct got_commit_object *commit = NULL;
4030 struct got_object_qid *pid;
4031 struct got_reference *head_ref = NULL;
4032 int ch, did_something = 0;
4034 while ((ch = getopt(argc, argv, "")) != -1) {
4035 switch (ch) {
4036 default:
4037 usage_backout();
4038 /* NOTREACHED */
4042 argc -= optind;
4043 argv += optind;
4045 #ifndef PROFILE
4046 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4047 "unveil", NULL) == -1)
4048 err(1, "pledge");
4049 #endif
4050 if (argc != 1)
4051 usage_backout();
4053 cwd = getcwd(NULL, 0);
4054 if (cwd == NULL) {
4055 error = got_error_from_errno("getcwd");
4056 goto done;
4058 error = got_worktree_open(&worktree, cwd);
4059 if (error)
4060 goto done;
4062 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4063 if (error != NULL)
4064 goto done;
4066 error = apply_unveil(got_repo_get_path(repo), 0,
4067 got_worktree_get_root_path(worktree));
4068 if (error)
4069 goto done;
4071 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4072 GOT_OBJ_TYPE_COMMIT, repo);
4073 if (error != NULL) {
4074 struct got_reference *ref;
4075 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4076 goto done;
4077 error = got_ref_open(&ref, repo, argv[0], 0);
4078 if (error != NULL)
4079 goto done;
4080 error = got_ref_resolve(&commit_id, repo, ref);
4081 got_ref_close(ref);
4082 if (error != NULL)
4083 goto done;
4085 error = got_object_id_str(&commit_id_str, commit_id);
4086 if (error)
4087 goto done;
4089 error = got_ref_open(&head_ref, repo,
4090 got_worktree_get_head_ref_name(worktree), 0);
4091 if (error != NULL)
4092 goto done;
4094 error = check_same_branch(commit_id, head_ref, NULL, repo);
4095 if (error)
4096 goto done;
4098 error = got_object_open_as_commit(&commit, repo, commit_id);
4099 if (error)
4100 goto done;
4101 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4102 if (pid == NULL) {
4103 error = got_error(GOT_ERR_ROOT_COMMIT);
4104 goto done;
4107 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4108 update_progress, &did_something, check_cancelled, NULL);
4109 if (error != NULL)
4110 goto done;
4112 if (did_something)
4113 printf("Backed out commit %s\n", commit_id_str);
4114 done:
4115 if (commit)
4116 got_object_commit_close(commit);
4117 free(commit_id_str);
4118 if (head_ref)
4119 got_ref_close(head_ref);
4120 if (worktree)
4121 got_worktree_close(worktree);
4122 if (repo)
4123 got_repo_close(repo);
4124 return error;
4127 __dead static void
4128 usage_rebase(void)
4130 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4131 getprogname());
4132 exit(1);
4135 void
4136 trim_logmsg(char *logmsg, int limit)
4138 char *nl;
4139 size_t len;
4141 len = strlen(logmsg);
4142 if (len > limit)
4143 len = limit;
4144 logmsg[len] = '\0';
4145 nl = strchr(logmsg, '\n');
4146 if (nl)
4147 *nl = '\0';
4150 static const struct got_error *
4151 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4153 const struct got_error *err;
4154 char *logmsg0 = NULL;
4155 const char *s;
4157 err = got_object_commit_get_logmsg(&logmsg0, commit);
4158 if (err)
4159 return err;
4161 s = logmsg0;
4162 while (isspace((unsigned char)s[0]))
4163 s++;
4165 *logmsg = strdup(s);
4166 if (*logmsg == NULL) {
4167 err = got_error_from_errno("strdup");
4168 goto done;
4171 trim_logmsg(*logmsg, limit);
4172 done:
4173 free(logmsg0);
4174 return err;
4177 static const struct got_error *
4178 show_rebase_progress(struct got_commit_object *commit,
4179 struct got_object_id *old_id, struct got_object_id *new_id)
4181 const struct got_error *err;
4182 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4184 err = got_object_id_str(&old_id_str, old_id);
4185 if (err)
4186 goto done;
4188 if (new_id) {
4189 err = got_object_id_str(&new_id_str, new_id);
4190 if (err)
4191 goto done;
4194 old_id_str[12] = '\0';
4195 if (new_id_str)
4196 new_id_str[12] = '\0';
4198 err = get_short_logmsg(&logmsg, 42, commit);
4199 if (err)
4200 goto done;
4202 printf("%s -> %s: %s\n", old_id_str,
4203 new_id_str ? new_id_str : "no-op change", logmsg);
4204 done:
4205 free(old_id_str);
4206 free(new_id_str);
4207 return err;
4210 static const struct got_error *
4211 rebase_progress(void *arg, unsigned char status, const char *path)
4213 unsigned char *rebase_status = arg;
4215 while (path[0] == '/')
4216 path++;
4217 printf("%c %s\n", status, path);
4219 if (*rebase_status == GOT_STATUS_CONFLICT)
4220 return NULL;
4221 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4222 *rebase_status = status;
4223 return NULL;
4226 static const struct got_error *
4227 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4228 struct got_reference *branch, struct got_reference *new_base_branch,
4229 struct got_reference *tmp_branch, struct got_repository *repo)
4231 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4232 return got_worktree_rebase_complete(worktree, fileindex,
4233 new_base_branch, tmp_branch, branch, repo);
4236 static const struct got_error *
4237 rebase_commit(struct got_pathlist_head *merged_paths,
4238 struct got_worktree *worktree, struct got_fileindex *fileindex,
4239 struct got_reference *tmp_branch,
4240 struct got_object_id *commit_id, struct got_repository *repo)
4242 const struct got_error *error;
4243 struct got_commit_object *commit;
4244 struct got_object_id *new_commit_id;
4246 error = got_object_open_as_commit(&commit, repo, commit_id);
4247 if (error)
4248 return error;
4250 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4251 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4252 if (error) {
4253 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4254 goto done;
4255 error = show_rebase_progress(commit, commit_id, NULL);
4256 } else {
4257 error = show_rebase_progress(commit, commit_id, new_commit_id);
4258 free(new_commit_id);
4260 done:
4261 got_object_commit_close(commit);
4262 return error;
4265 struct check_path_prefix_arg {
4266 const char *path_prefix;
4267 size_t len;
4268 int errcode;
4271 static const struct got_error *
4272 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4273 struct got_blob_object *blob2, struct got_object_id *id1,
4274 struct got_object_id *id2, const char *path1, const char *path2,
4275 struct got_repository *repo)
4277 struct check_path_prefix_arg *a = arg;
4279 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4280 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4281 return got_error(a->errcode);
4283 return NULL;
4286 static const struct got_error *
4287 check_path_prefix(struct got_object_id *parent_id,
4288 struct got_object_id *commit_id, const char *path_prefix,
4289 int errcode, struct got_repository *repo)
4291 const struct got_error *err;
4292 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4293 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4294 struct check_path_prefix_arg cpp_arg;
4296 if (got_path_is_root_dir(path_prefix))
4297 return NULL;
4299 err = got_object_open_as_commit(&commit, repo, commit_id);
4300 if (err)
4301 goto done;
4303 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4304 if (err)
4305 goto done;
4307 err = got_object_open_as_tree(&tree1, repo,
4308 got_object_commit_get_tree_id(parent_commit));
4309 if (err)
4310 goto done;
4312 err = got_object_open_as_tree(&tree2, repo,
4313 got_object_commit_get_tree_id(commit));
4314 if (err)
4315 goto done;
4317 cpp_arg.path_prefix = path_prefix;
4318 while (cpp_arg.path_prefix[0] == '/')
4319 cpp_arg.path_prefix++;
4320 cpp_arg.len = strlen(cpp_arg.path_prefix);
4321 cpp_arg.errcode = errcode;
4322 err = got_diff_tree(tree1, tree2, "", "", repo,
4323 check_path_prefix_in_diff, &cpp_arg, 0);
4324 done:
4325 if (tree1)
4326 got_object_tree_close(tree1);
4327 if (tree2)
4328 got_object_tree_close(tree2);
4329 if (commit)
4330 got_object_commit_close(commit);
4331 if (parent_commit)
4332 got_object_commit_close(parent_commit);
4333 return err;
4336 static const struct got_error *
4337 collect_commits(struct got_object_id_queue *commits,
4338 struct got_object_id *initial_commit_id,
4339 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4340 const char *path_prefix, int path_prefix_errcode,
4341 struct got_repository *repo)
4343 const struct got_error *err = NULL;
4344 struct got_commit_graph *graph = NULL;
4345 struct got_object_id *parent_id = NULL;
4346 struct got_object_qid *qid;
4347 struct got_object_id *commit_id = initial_commit_id;
4349 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4350 if (err)
4351 return err;
4353 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4354 check_cancelled, NULL);
4355 if (err)
4356 goto done;
4357 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4358 err = got_commit_graph_iter_next(&parent_id, graph);
4359 if (err) {
4360 if (err->code == GOT_ERR_ITER_COMPLETED) {
4361 err = got_error_msg(GOT_ERR_ANCESTRY,
4362 "ran out of commits to rebase before "
4363 "youngest common ancestor commit has "
4364 "been reached?!?");
4365 goto done;
4366 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4367 goto done;
4368 err = got_commit_graph_fetch_commits(graph, 1, repo,
4369 check_cancelled, NULL);
4370 if (err)
4371 goto done;
4372 } else {
4373 err = check_path_prefix(parent_id, commit_id,
4374 path_prefix, path_prefix_errcode, repo);
4375 if (err)
4376 goto done;
4378 err = got_object_qid_alloc(&qid, commit_id);
4379 if (err)
4380 goto done;
4381 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4382 commit_id = parent_id;
4385 done:
4386 got_commit_graph_close(graph);
4387 return err;
4390 static const struct got_error *
4391 cmd_rebase(int argc, char *argv[])
4393 const struct got_error *error = NULL;
4394 struct got_worktree *worktree = NULL;
4395 struct got_repository *repo = NULL;
4396 struct got_fileindex *fileindex = NULL;
4397 char *cwd = NULL;
4398 struct got_reference *branch = NULL;
4399 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4400 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4401 struct got_object_id *resume_commit_id = NULL;
4402 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4403 struct got_commit_object *commit = NULL;
4404 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4405 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4406 struct got_object_id_queue commits;
4407 struct got_pathlist_head merged_paths;
4408 const struct got_object_id_queue *parent_ids;
4409 struct got_object_qid *qid, *pid;
4411 SIMPLEQ_INIT(&commits);
4412 TAILQ_INIT(&merged_paths);
4414 while ((ch = getopt(argc, argv, "ac")) != -1) {
4415 switch (ch) {
4416 case 'a':
4417 abort_rebase = 1;
4418 break;
4419 case 'c':
4420 continue_rebase = 1;
4421 break;
4422 default:
4423 usage_rebase();
4424 /* NOTREACHED */
4428 argc -= optind;
4429 argv += optind;
4431 #ifndef PROFILE
4432 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4433 "unveil", NULL) == -1)
4434 err(1, "pledge");
4435 #endif
4436 if (abort_rebase && continue_rebase)
4437 usage_rebase();
4438 else if (abort_rebase || continue_rebase) {
4439 if (argc != 0)
4440 usage_rebase();
4441 } else if (argc != 1)
4442 usage_rebase();
4444 cwd = getcwd(NULL, 0);
4445 if (cwd == NULL) {
4446 error = got_error_from_errno("getcwd");
4447 goto done;
4449 error = got_worktree_open(&worktree, cwd);
4450 if (error)
4451 goto done;
4453 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4454 if (error != NULL)
4455 goto done;
4457 error = apply_unveil(got_repo_get_path(repo), 0,
4458 got_worktree_get_root_path(worktree));
4459 if (error)
4460 goto done;
4462 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4463 if (error)
4464 goto done;
4466 if (abort_rebase) {
4467 int did_something;
4468 if (!rebase_in_progress) {
4469 error = got_error(GOT_ERR_NOT_REBASING);
4470 goto done;
4472 error = got_worktree_rebase_continue(&resume_commit_id,
4473 &new_base_branch, &tmp_branch, &branch, &fileindex,
4474 worktree, repo);
4475 if (error)
4476 goto done;
4477 printf("Switching work tree to %s\n",
4478 got_ref_get_symref_target(new_base_branch));
4479 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4480 new_base_branch, update_progress, &did_something);
4481 if (error)
4482 goto done;
4483 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4484 goto done; /* nothing else to do */
4487 if (continue_rebase) {
4488 if (!rebase_in_progress) {
4489 error = got_error(GOT_ERR_NOT_REBASING);
4490 goto done;
4492 error = got_worktree_rebase_continue(&resume_commit_id,
4493 &new_base_branch, &tmp_branch, &branch, &fileindex,
4494 worktree, repo);
4495 if (error)
4496 goto done;
4498 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4499 resume_commit_id, repo);
4500 if (error)
4501 goto done;
4503 yca_id = got_object_id_dup(resume_commit_id);
4504 if (yca_id == NULL) {
4505 error = got_error_from_errno("got_object_id_dup");
4506 goto done;
4508 } else {
4509 error = got_ref_open(&branch, repo, argv[0], 0);
4510 if (error != NULL)
4511 goto done;
4514 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4515 if (error)
4516 goto done;
4518 if (!continue_rebase) {
4519 struct got_object_id *base_commit_id;
4521 base_commit_id = got_worktree_get_base_commit_id(worktree);
4522 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4523 base_commit_id, branch_head_commit_id, repo,
4524 check_cancelled, NULL);
4525 if (error)
4526 goto done;
4527 if (yca_id == NULL) {
4528 error = got_error_msg(GOT_ERR_ANCESTRY,
4529 "specified branch shares no common ancestry "
4530 "with work tree's branch");
4531 goto done;
4534 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4535 if (error) {
4536 if (error->code != GOT_ERR_ANCESTRY)
4537 goto done;
4538 error = NULL;
4539 } else {
4540 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4541 "specified branch resolves to a commit which "
4542 "is already contained in work tree's branch");
4543 goto done;
4545 error = got_worktree_rebase_prepare(&new_base_branch,
4546 &tmp_branch, &fileindex, worktree, branch, repo);
4547 if (error)
4548 goto done;
4551 commit_id = branch_head_commit_id;
4552 error = got_object_open_as_commit(&commit, repo, commit_id);
4553 if (error)
4554 goto done;
4556 parent_ids = got_object_commit_get_parent_ids(commit);
4557 pid = SIMPLEQ_FIRST(parent_ids);
4558 if (pid == NULL) {
4559 if (!continue_rebase) {
4560 int did_something;
4561 error = got_worktree_rebase_abort(worktree, fileindex,
4562 repo, new_base_branch, update_progress,
4563 &did_something);
4564 if (error)
4565 goto done;
4566 printf("Rebase of %s aborted\n",
4567 got_ref_get_name(branch));
4569 error = got_error(GOT_ERR_EMPTY_REBASE);
4570 goto done;
4572 error = collect_commits(&commits, commit_id, pid->id,
4573 yca_id, got_worktree_get_path_prefix(worktree),
4574 GOT_ERR_REBASE_PATH, repo);
4575 got_object_commit_close(commit);
4576 commit = NULL;
4577 if (error)
4578 goto done;
4580 if (SIMPLEQ_EMPTY(&commits)) {
4581 if (continue_rebase)
4582 error = rebase_complete(worktree, fileindex,
4583 branch, new_base_branch, tmp_branch, repo);
4584 else
4585 error = got_error(GOT_ERR_EMPTY_REBASE);
4586 goto done;
4589 pid = NULL;
4590 SIMPLEQ_FOREACH(qid, &commits, entry) {
4591 commit_id = qid->id;
4592 parent_id = pid ? pid->id : yca_id;
4593 pid = qid;
4595 error = got_worktree_rebase_merge_files(&merged_paths,
4596 worktree, fileindex, parent_id, commit_id, repo,
4597 rebase_progress, &rebase_status, check_cancelled, NULL);
4598 if (error)
4599 goto done;
4601 if (rebase_status == GOT_STATUS_CONFLICT) {
4602 got_worktree_rebase_pathlist_free(&merged_paths);
4603 break;
4606 error = rebase_commit(&merged_paths, worktree, fileindex,
4607 tmp_branch, commit_id, repo);
4608 got_worktree_rebase_pathlist_free(&merged_paths);
4609 if (error)
4610 goto done;
4613 if (rebase_status == GOT_STATUS_CONFLICT) {
4614 error = got_worktree_rebase_postpone(worktree, fileindex);
4615 if (error)
4616 goto done;
4617 error = got_error_msg(GOT_ERR_CONFLICTS,
4618 "conflicts must be resolved before rebasing can continue");
4619 } else
4620 error = rebase_complete(worktree, fileindex, branch,
4621 new_base_branch, tmp_branch, repo);
4622 done:
4623 got_object_id_queue_free(&commits);
4624 free(branch_head_commit_id);
4625 free(resume_commit_id);
4626 free(yca_id);
4627 if (commit)
4628 got_object_commit_close(commit);
4629 if (branch)
4630 got_ref_close(branch);
4631 if (new_base_branch)
4632 got_ref_close(new_base_branch);
4633 if (tmp_branch)
4634 got_ref_close(tmp_branch);
4635 if (worktree)
4636 got_worktree_close(worktree);
4637 if (repo)
4638 got_repo_close(repo);
4639 return error;
4642 __dead static void
4643 usage_histedit(void)
4645 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4646 getprogname());
4647 exit(1);
4650 #define GOT_HISTEDIT_PICK 'p'
4651 #define GOT_HISTEDIT_EDIT 'e'
4652 #define GOT_HISTEDIT_FOLD 'f'
4653 #define GOT_HISTEDIT_DROP 'd'
4654 #define GOT_HISTEDIT_MESG 'm'
4656 static struct got_histedit_cmd {
4657 unsigned char code;
4658 const char *name;
4659 const char *desc;
4660 } got_histedit_cmds[] = {
4661 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4662 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4663 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4664 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4665 { GOT_HISTEDIT_MESG, "mesg",
4666 "single-line log message for commit above (open editor if empty)" },
4669 struct got_histedit_list_entry {
4670 TAILQ_ENTRY(got_histedit_list_entry) entry;
4671 struct got_object_id *commit_id;
4672 const struct got_histedit_cmd *cmd;
4673 char *logmsg;
4675 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4677 static const struct got_error *
4678 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4679 FILE *f, struct got_repository *repo)
4681 const struct got_error *err = NULL;
4682 char *logmsg = NULL, *id_str = NULL;
4683 struct got_commit_object *commit = NULL;
4684 int n;
4686 err = got_object_open_as_commit(&commit, repo, commit_id);
4687 if (err)
4688 goto done;
4690 err = get_short_logmsg(&logmsg, 34, commit);
4691 if (err)
4692 goto done;
4694 err = got_object_id_str(&id_str, commit_id);
4695 if (err)
4696 goto done;
4698 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4699 if (n < 0)
4700 err = got_ferror(f, GOT_ERR_IO);
4701 done:
4702 if (commit)
4703 got_object_commit_close(commit);
4704 free(id_str);
4705 free(logmsg);
4706 return err;
4709 static const struct got_error *
4710 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4711 struct got_repository *repo)
4713 const struct got_error *err = NULL;
4714 struct got_object_qid *qid;
4716 if (SIMPLEQ_EMPTY(commits))
4717 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4719 SIMPLEQ_FOREACH(qid, commits, entry) {
4720 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4721 f, repo);
4722 if (err)
4723 break;
4726 return err;
4729 static const struct got_error *
4730 write_cmd_list(FILE *f)
4732 const struct got_error *err = NULL;
4733 int n, i;
4735 n = fprintf(f, "# Available histedit commands:\n");
4736 if (n < 0)
4737 return got_ferror(f, GOT_ERR_IO);
4739 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4740 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4741 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4742 cmd->desc);
4743 if (n < 0) {
4744 err = got_ferror(f, GOT_ERR_IO);
4745 break;
4748 n = fprintf(f, "# Commits will be processed in order from top to "
4749 "bottom of this file.\n");
4750 if (n < 0)
4751 return got_ferror(f, GOT_ERR_IO);
4752 return err;
4755 static const struct got_error *
4756 histedit_syntax_error(int lineno)
4758 static char msg[42];
4759 int ret;
4761 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4762 lineno);
4763 if (ret == -1 || ret >= sizeof(msg))
4764 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4766 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4769 static const struct got_error *
4770 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4771 char *logmsg, struct got_repository *repo)
4773 const struct got_error *err;
4774 struct got_commit_object *folded_commit = NULL;
4775 char *id_str, *folded_logmsg = NULL;
4777 err = got_object_id_str(&id_str, hle->commit_id);
4778 if (err)
4779 return err;
4781 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4782 if (err)
4783 goto done;
4785 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4786 if (err)
4787 goto done;
4788 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4789 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4790 folded_logmsg) == -1) {
4791 err = got_error_from_errno("asprintf");
4792 goto done;
4794 done:
4795 if (folded_commit)
4796 got_object_commit_close(folded_commit);
4797 free(id_str);
4798 free(folded_logmsg);
4799 return err;
4802 static struct got_histedit_list_entry *
4803 get_folded_commits(struct got_histedit_list_entry *hle)
4805 struct got_histedit_list_entry *prev, *folded = NULL;
4807 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4808 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4809 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4810 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4811 folded = prev;
4812 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4815 return folded;
4818 static const struct got_error *
4819 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4820 struct got_repository *repo)
4822 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4823 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4824 const struct got_error *err = NULL;
4825 struct got_commit_object *commit = NULL;
4826 int fd;
4827 struct got_histedit_list_entry *folded = NULL;
4829 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4830 if (err)
4831 return err;
4833 folded = get_folded_commits(hle);
4834 if (folded) {
4835 while (folded != hle) {
4836 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4837 folded = TAILQ_NEXT(folded, entry);
4838 continue;
4840 err = append_folded_commit_msg(&new_msg, folded,
4841 logmsg, repo);
4842 if (err)
4843 goto done;
4844 free(logmsg);
4845 logmsg = new_msg;
4846 folded = TAILQ_NEXT(folded, entry);
4850 err = got_object_id_str(&id_str, hle->commit_id);
4851 if (err)
4852 goto done;
4853 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4854 if (err)
4855 goto done;
4856 if (asprintf(&new_msg,
4857 "%s\n# original log message of commit %s: %s",
4858 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4859 err = got_error_from_errno("asprintf");
4860 goto done;
4862 free(logmsg);
4863 logmsg = new_msg;
4865 err = got_object_id_str(&id_str, hle->commit_id);
4866 if (err)
4867 goto done;
4869 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4870 if (err)
4871 goto done;
4873 dprintf(fd, logmsg);
4874 close(fd);
4876 err = get_editor(&editor);
4877 if (err)
4878 goto done;
4880 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4881 if (err) {
4882 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4883 goto done;
4884 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4886 done:
4887 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4888 err = got_error_from_errno2("unlink", logmsg_path);
4889 free(logmsg_path);
4890 free(logmsg);
4891 free(orig_logmsg);
4892 free(editor);
4893 if (commit)
4894 got_object_commit_close(commit);
4895 return err;
4898 static const struct got_error *
4899 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4900 FILE *f, struct got_repository *repo)
4902 const struct got_error *err = NULL;
4903 char *line = NULL, *p, *end;
4904 size_t size;
4905 ssize_t len;
4906 int lineno = 0, i;
4907 const struct got_histedit_cmd *cmd;
4908 struct got_object_id *commit_id = NULL;
4909 struct got_histedit_list_entry *hle = NULL;
4911 for (;;) {
4912 len = getline(&line, &size, f);
4913 if (len == -1) {
4914 const struct got_error *getline_err;
4915 if (feof(f))
4916 break;
4917 getline_err = got_error_from_errno("getline");
4918 err = got_ferror(f, getline_err->code);
4919 break;
4921 lineno++;
4922 p = line;
4923 while (isspace((unsigned char)p[0]))
4924 p++;
4925 if (p[0] == '#' || p[0] == '\0') {
4926 free(line);
4927 line = NULL;
4928 continue;
4930 cmd = NULL;
4931 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4932 cmd = &got_histedit_cmds[i];
4933 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4934 isspace((unsigned char)p[strlen(cmd->name)])) {
4935 p += strlen(cmd->name);
4936 break;
4938 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4939 p++;
4940 break;
4943 if (i == nitems(got_histedit_cmds)) {
4944 err = histedit_syntax_error(lineno);
4945 break;
4947 while (isspace((unsigned char)p[0]))
4948 p++;
4949 if (cmd->code == GOT_HISTEDIT_MESG) {
4950 if (hle == NULL || hle->logmsg != NULL) {
4951 err = got_error(GOT_ERR_HISTEDIT_CMD);
4952 break;
4954 if (p[0] == '\0') {
4955 err = histedit_edit_logmsg(hle, repo);
4956 if (err)
4957 break;
4958 } else {
4959 hle->logmsg = strdup(p);
4960 if (hle->logmsg == NULL) {
4961 err = got_error_from_errno("strdup");
4962 break;
4965 free(line);
4966 line = NULL;
4967 continue;
4968 } else {
4969 end = p;
4970 while (end[0] && !isspace((unsigned char)end[0]))
4971 end++;
4972 *end = '\0';
4974 err = got_object_resolve_id_str(&commit_id, repo, p);
4975 if (err) {
4976 /* override error code */
4977 err = histedit_syntax_error(lineno);
4978 break;
4981 hle = malloc(sizeof(*hle));
4982 if (hle == NULL) {
4983 err = got_error_from_errno("malloc");
4984 break;
4986 hle->cmd = cmd;
4987 hle->commit_id = commit_id;
4988 hle->logmsg = NULL;
4989 commit_id = NULL;
4990 free(line);
4991 line = NULL;
4992 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4995 free(line);
4996 free(commit_id);
4997 return err;
5000 static const struct got_error *
5001 histedit_check_script(struct got_histedit_list *histedit_cmds,
5002 struct got_object_id_queue *commits, struct got_repository *repo)
5004 const struct got_error *err = NULL;
5005 struct got_object_qid *qid;
5006 struct got_histedit_list_entry *hle;
5007 static char msg[80];
5008 char *id_str;
5010 if (TAILQ_EMPTY(histedit_cmds))
5011 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5012 "histedit script contains no commands");
5013 if (SIMPLEQ_EMPTY(commits))
5014 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5016 SIMPLEQ_FOREACH(qid, commits, entry) {
5017 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5018 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5019 break;
5021 if (hle == NULL) {
5022 err = got_object_id_str(&id_str, qid->id);
5023 if (err)
5024 return err;
5025 snprintf(msg, sizeof(msg),
5026 "commit %s missing from histedit script", id_str);
5027 free(id_str);
5028 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5032 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5033 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5034 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5035 "last commit in histedit script cannot be folded");
5037 return NULL;
5040 static const struct got_error *
5041 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5042 const char *path, struct got_object_id_queue *commits,
5043 struct got_repository *repo)
5045 const struct got_error *err = NULL;
5046 char *editor;
5047 FILE *f = NULL;
5049 err = get_editor(&editor);
5050 if (err)
5051 return err;
5053 if (spawn_editor(editor, path) == -1) {
5054 err = got_error_from_errno("failed spawning editor");
5055 goto done;
5058 f = fopen(path, "r");
5059 if (f == NULL) {
5060 err = got_error_from_errno("fopen");
5061 goto done;
5063 err = histedit_parse_list(histedit_cmds, f, repo);
5064 if (err)
5065 goto done;
5067 err = histedit_check_script(histedit_cmds, commits, repo);
5068 done:
5069 if (f && fclose(f) != 0 && err == NULL)
5070 err = got_error_from_errno("fclose");
5071 free(editor);
5072 return err;
5075 static const struct got_error *
5076 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5077 struct got_object_id_queue *, const char *, struct got_repository *);
5079 static const struct got_error *
5080 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5081 struct got_object_id_queue *commits, struct got_repository *repo)
5083 const struct got_error *err;
5084 FILE *f = NULL;
5085 char *path = NULL;
5087 err = got_opentemp_named(&path, &f, "got-histedit");
5088 if (err)
5089 return err;
5091 err = write_cmd_list(f);
5092 if (err)
5093 goto done;
5095 err = histedit_write_commit_list(commits, f, repo);
5096 if (err)
5097 goto done;
5099 if (fclose(f) != 0) {
5100 err = got_error_from_errno("fclose");
5101 goto done;
5103 f = NULL;
5105 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5106 if (err) {
5107 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5108 err->code != GOT_ERR_HISTEDIT_CMD)
5109 goto done;
5110 err = histedit_edit_list_retry(histedit_cmds, err,
5111 commits, path, repo);
5113 done:
5114 if (f && fclose(f) != 0 && err == NULL)
5115 err = got_error_from_errno("fclose");
5116 if (path && unlink(path) != 0 && err == NULL)
5117 err = got_error_from_errno2("unlink", path);
5118 free(path);
5119 return err;
5122 static const struct got_error *
5123 histedit_save_list(struct got_histedit_list *histedit_cmds,
5124 struct got_worktree *worktree, struct got_repository *repo)
5126 const struct got_error *err = NULL;
5127 char *path = NULL;
5128 FILE *f = NULL;
5129 struct got_histedit_list_entry *hle;
5130 struct got_commit_object *commit = NULL;
5132 err = got_worktree_get_histedit_script_path(&path, worktree);
5133 if (err)
5134 return err;
5136 f = fopen(path, "w");
5137 if (f == NULL) {
5138 err = got_error_from_errno2("fopen", path);
5139 goto done;
5141 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5142 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5143 repo);
5144 if (err)
5145 break;
5147 if (hle->logmsg) {
5148 int n = fprintf(f, "%c %s\n",
5149 GOT_HISTEDIT_MESG, hle->logmsg);
5150 if (n < 0) {
5151 err = got_ferror(f, GOT_ERR_IO);
5152 break;
5156 done:
5157 if (f && fclose(f) != 0 && err == NULL)
5158 err = got_error_from_errno("fclose");
5159 free(path);
5160 if (commit)
5161 got_object_commit_close(commit);
5162 return err;
5165 void
5166 histedit_free_list(struct got_histedit_list *histedit_cmds)
5168 struct got_histedit_list_entry *hle;
5170 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5171 TAILQ_REMOVE(histedit_cmds, hle, entry);
5172 free(hle);
5176 static const struct got_error *
5177 histedit_load_list(struct got_histedit_list *histedit_cmds,
5178 const char *path, struct got_repository *repo)
5180 const struct got_error *err = NULL;
5181 FILE *f = NULL;
5183 f = fopen(path, "r");
5184 if (f == NULL) {
5185 err = got_error_from_errno2("fopen", path);
5186 goto done;
5189 err = histedit_parse_list(histedit_cmds, f, repo);
5190 done:
5191 if (f && fclose(f) != 0 && err == NULL)
5192 err = got_error_from_errno("fclose");
5193 return err;
5196 static const struct got_error *
5197 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5198 const struct got_error *edit_err, struct got_object_id_queue *commits,
5199 const char *path, struct got_repository *repo)
5201 const struct got_error *err = NULL, *prev_err = edit_err;
5202 int resp = ' ';
5204 while (resp != 'c' && resp != 'r' && resp != 'a') {
5205 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5206 "or (a)bort: ", getprogname(), prev_err->msg);
5207 resp = getchar();
5208 if (resp == '\n')
5209 resp = getchar();
5210 if (resp == 'c') {
5211 histedit_free_list(histedit_cmds);
5212 err = histedit_run_editor(histedit_cmds, path, commits,
5213 repo);
5214 if (err) {
5215 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5216 err->code != GOT_ERR_HISTEDIT_CMD)
5217 break;
5218 prev_err = err;
5219 resp = ' ';
5220 continue;
5222 break;
5223 } else if (resp == 'r') {
5224 histedit_free_list(histedit_cmds);
5225 err = histedit_edit_script(histedit_cmds,
5226 commits, repo);
5227 if (err) {
5228 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5229 err->code != GOT_ERR_HISTEDIT_CMD)
5230 break;
5231 prev_err = err;
5232 resp = ' ';
5233 continue;
5235 break;
5236 } else if (resp == 'a') {
5237 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5238 break;
5239 } else
5240 printf("invalid response '%c'\n", resp);
5243 return err;
5246 static const struct got_error *
5247 histedit_complete(struct got_worktree *worktree,
5248 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5249 struct got_reference *branch, struct got_repository *repo)
5251 printf("Switching work tree to %s\n",
5252 got_ref_get_symref_target(branch));
5253 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5254 branch, repo);
5257 static const struct got_error *
5258 show_histedit_progress(struct got_commit_object *commit,
5259 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5261 const struct got_error *err;
5262 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5264 err = got_object_id_str(&old_id_str, hle->commit_id);
5265 if (err)
5266 goto done;
5268 if (new_id) {
5269 err = got_object_id_str(&new_id_str, new_id);
5270 if (err)
5271 goto done;
5274 old_id_str[12] = '\0';
5275 if (new_id_str)
5276 new_id_str[12] = '\0';
5278 if (hle->logmsg) {
5279 logmsg = strdup(hle->logmsg);
5280 if (logmsg == NULL) {
5281 err = got_error_from_errno("strdup");
5282 goto done;
5284 trim_logmsg(logmsg, 42);
5285 } else {
5286 err = get_short_logmsg(&logmsg, 42, commit);
5287 if (err)
5288 goto done;
5291 switch (hle->cmd->code) {
5292 case GOT_HISTEDIT_PICK:
5293 case GOT_HISTEDIT_EDIT:
5294 printf("%s -> %s: %s\n", old_id_str,
5295 new_id_str ? new_id_str : "no-op change", logmsg);
5296 break;
5297 case GOT_HISTEDIT_DROP:
5298 case GOT_HISTEDIT_FOLD:
5299 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5300 logmsg);
5301 break;
5302 default:
5303 break;
5306 done:
5307 free(old_id_str);
5308 free(new_id_str);
5309 return err;
5312 static const struct got_error *
5313 histedit_commit(struct got_pathlist_head *merged_paths,
5314 struct got_worktree *worktree, struct got_fileindex *fileindex,
5315 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5316 struct got_repository *repo)
5318 const struct got_error *err;
5319 struct got_commit_object *commit;
5320 struct got_object_id *new_commit_id;
5322 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5323 && hle->logmsg == NULL) {
5324 err = histedit_edit_logmsg(hle, repo);
5325 if (err)
5326 return err;
5329 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5330 if (err)
5331 return err;
5333 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5334 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5335 hle->logmsg, repo);
5336 if (err) {
5337 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5338 goto done;
5339 err = show_histedit_progress(commit, hle, NULL);
5340 } else {
5341 err = show_histedit_progress(commit, hle, new_commit_id);
5342 free(new_commit_id);
5344 done:
5345 got_object_commit_close(commit);
5346 return err;
5349 static const struct got_error *
5350 histedit_skip_commit(struct got_histedit_list_entry *hle,
5351 struct got_worktree *worktree, struct got_repository *repo)
5353 const struct got_error *error;
5354 struct got_commit_object *commit;
5356 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5357 repo);
5358 if (error)
5359 return error;
5361 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5362 if (error)
5363 return error;
5365 error = show_histedit_progress(commit, hle, NULL);
5366 got_object_commit_close(commit);
5367 return error;
5370 static const struct got_error *
5371 cmd_histedit(int argc, char *argv[])
5373 const struct got_error *error = NULL;
5374 struct got_worktree *worktree = NULL;
5375 struct got_fileindex *fileindex = NULL;
5376 struct got_repository *repo = NULL;
5377 char *cwd = NULL;
5378 struct got_reference *branch = NULL;
5379 struct got_reference *tmp_branch = NULL;
5380 struct got_object_id *resume_commit_id = NULL;
5381 struct got_object_id *base_commit_id = NULL;
5382 struct got_object_id *head_commit_id = NULL;
5383 struct got_commit_object *commit = NULL;
5384 int ch, rebase_in_progress = 0, did_something;
5385 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5386 const char *edit_script_path = NULL;
5387 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5388 struct got_object_id_queue commits;
5389 struct got_pathlist_head merged_paths;
5390 const struct got_object_id_queue *parent_ids;
5391 struct got_object_qid *pid;
5392 struct got_histedit_list histedit_cmds;
5393 struct got_histedit_list_entry *hle;
5395 SIMPLEQ_INIT(&commits);
5396 TAILQ_INIT(&histedit_cmds);
5397 TAILQ_INIT(&merged_paths);
5399 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5400 switch (ch) {
5401 case 'a':
5402 abort_edit = 1;
5403 break;
5404 case 'c':
5405 continue_edit = 1;
5406 break;
5407 case 'F':
5408 edit_script_path = optarg;
5409 break;
5410 default:
5411 usage_histedit();
5412 /* NOTREACHED */
5416 argc -= optind;
5417 argv += optind;
5419 #ifndef PROFILE
5420 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5421 "unveil", NULL) == -1)
5422 err(1, "pledge");
5423 #endif
5424 if (abort_edit && continue_edit)
5425 usage_histedit();
5426 if (argc != 0)
5427 usage_histedit();
5430 * This command cannot apply unveil(2) in all cases because the
5431 * user may choose to run an editor to edit the histedit script
5432 * and to edit individual commit log messages.
5433 * unveil(2) traverses exec(2); if an editor is used we have to
5434 * apply unveil after edit script and log messages have been written.
5435 * XXX TODO: Make use of unveil(2) where possible.
5438 cwd = getcwd(NULL, 0);
5439 if (cwd == NULL) {
5440 error = got_error_from_errno("getcwd");
5441 goto done;
5443 error = got_worktree_open(&worktree, cwd);
5444 if (error)
5445 goto done;
5447 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5448 if (error != NULL)
5449 goto done;
5451 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5452 if (error)
5453 goto done;
5454 if (rebase_in_progress) {
5455 error = got_error(GOT_ERR_REBASING);
5456 goto done;
5459 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5460 if (error)
5461 goto done;
5463 if (edit_in_progress && abort_edit) {
5464 error = got_worktree_histedit_continue(&resume_commit_id,
5465 &tmp_branch, &branch, &base_commit_id, &fileindex,
5466 worktree, repo);
5467 if (error)
5468 goto done;
5469 printf("Switching work tree to %s\n",
5470 got_ref_get_symref_target(branch));
5471 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5472 branch, base_commit_id, update_progress, &did_something);
5473 if (error)
5474 goto done;
5475 printf("Histedit of %s aborted\n",
5476 got_ref_get_symref_target(branch));
5477 goto done; /* nothing else to do */
5478 } else if (abort_edit) {
5479 error = got_error(GOT_ERR_NOT_HISTEDIT);
5480 goto done;
5483 if (continue_edit) {
5484 char *path;
5486 if (!edit_in_progress) {
5487 error = got_error(GOT_ERR_NOT_HISTEDIT);
5488 goto done;
5491 error = got_worktree_get_histedit_script_path(&path, worktree);
5492 if (error)
5493 goto done;
5495 error = histedit_load_list(&histedit_cmds, path, repo);
5496 free(path);
5497 if (error)
5498 goto done;
5500 error = got_worktree_histedit_continue(&resume_commit_id,
5501 &tmp_branch, &branch, &base_commit_id, &fileindex,
5502 worktree, repo);
5503 if (error)
5504 goto done;
5506 error = got_ref_resolve(&head_commit_id, repo, branch);
5507 if (error)
5508 goto done;
5510 error = got_object_open_as_commit(&commit, repo,
5511 head_commit_id);
5512 if (error)
5513 goto done;
5514 parent_ids = got_object_commit_get_parent_ids(commit);
5515 pid = SIMPLEQ_FIRST(parent_ids);
5516 if (pid == NULL) {
5517 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5518 goto done;
5520 error = collect_commits(&commits, head_commit_id, pid->id,
5521 base_commit_id, got_worktree_get_path_prefix(worktree),
5522 GOT_ERR_HISTEDIT_PATH, repo);
5523 got_object_commit_close(commit);
5524 commit = NULL;
5525 if (error)
5526 goto done;
5527 } else {
5528 if (edit_in_progress) {
5529 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5530 goto done;
5533 error = got_ref_open(&branch, repo,
5534 got_worktree_get_head_ref_name(worktree), 0);
5535 if (error != NULL)
5536 goto done;
5538 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5539 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5540 "will not edit commit history of a branch outside "
5541 "the \"refs/heads/\" reference namespace");
5542 goto done;
5545 error = got_ref_resolve(&head_commit_id, repo, branch);
5546 got_ref_close(branch);
5547 branch = NULL;
5548 if (error)
5549 goto done;
5551 error = got_object_open_as_commit(&commit, repo,
5552 head_commit_id);
5553 if (error)
5554 goto done;
5555 parent_ids = got_object_commit_get_parent_ids(commit);
5556 pid = SIMPLEQ_FIRST(parent_ids);
5557 if (pid == NULL) {
5558 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5559 goto done;
5561 error = collect_commits(&commits, head_commit_id, pid->id,
5562 got_worktree_get_base_commit_id(worktree),
5563 got_worktree_get_path_prefix(worktree),
5564 GOT_ERR_HISTEDIT_PATH, repo);
5565 got_object_commit_close(commit);
5566 commit = NULL;
5567 if (error)
5568 goto done;
5570 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5571 &base_commit_id, &fileindex, worktree, repo);
5572 if (error)
5573 goto done;
5575 if (edit_script_path) {
5576 error = histedit_load_list(&histedit_cmds,
5577 edit_script_path, repo);
5578 if (error) {
5579 got_worktree_histedit_abort(worktree, fileindex,
5580 repo, branch, base_commit_id,
5581 update_progress, &did_something);
5582 goto done;
5584 } else {
5585 error = histedit_edit_script(&histedit_cmds, &commits,
5586 repo);
5587 if (error) {
5588 got_worktree_histedit_abort(worktree, fileindex,
5589 repo, branch, base_commit_id,
5590 update_progress, &did_something);
5591 goto done;
5596 error = histedit_save_list(&histedit_cmds, worktree,
5597 repo);
5598 if (error) {
5599 got_worktree_histedit_abort(worktree, fileindex,
5600 repo, branch, base_commit_id,
5601 update_progress, &did_something);
5602 goto done;
5607 error = histedit_check_script(&histedit_cmds, &commits, repo);
5608 if (error)
5609 goto done;
5611 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5612 if (resume_commit_id) {
5613 if (got_object_id_cmp(hle->commit_id,
5614 resume_commit_id) != 0)
5615 continue;
5617 resume_commit_id = NULL;
5618 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5619 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5620 error = histedit_skip_commit(hle, worktree,
5621 repo);
5622 } else {
5623 error = histedit_commit(NULL, worktree,
5624 fileindex, tmp_branch, hle, repo);
5626 if (error)
5627 goto done;
5628 continue;
5631 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5632 error = histedit_skip_commit(hle, worktree, repo);
5633 if (error)
5634 goto done;
5635 continue;
5638 error = got_object_open_as_commit(&commit, repo,
5639 hle->commit_id);
5640 if (error)
5641 goto done;
5642 parent_ids = got_object_commit_get_parent_ids(commit);
5643 pid = SIMPLEQ_FIRST(parent_ids);
5645 error = got_worktree_histedit_merge_files(&merged_paths,
5646 worktree, fileindex, pid->id, hle->commit_id, repo,
5647 rebase_progress, &rebase_status, check_cancelled, NULL);
5648 if (error)
5649 goto done;
5650 got_object_commit_close(commit);
5651 commit = NULL;
5653 if (rebase_status == GOT_STATUS_CONFLICT) {
5654 got_worktree_rebase_pathlist_free(&merged_paths);
5655 break;
5658 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5659 char *id_str;
5660 error = got_object_id_str(&id_str, hle->commit_id);
5661 if (error)
5662 goto done;
5663 printf("Stopping histedit for amending commit %s\n",
5664 id_str);
5665 free(id_str);
5666 got_worktree_rebase_pathlist_free(&merged_paths);
5667 error = got_worktree_histedit_postpone(worktree,
5668 fileindex);
5669 goto done;
5672 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5673 error = histedit_skip_commit(hle, worktree, repo);
5674 if (error)
5675 goto done;
5676 continue;
5679 error = histedit_commit(&merged_paths, worktree, fileindex,
5680 tmp_branch, hle, repo);
5681 got_worktree_rebase_pathlist_free(&merged_paths);
5682 if (error)
5683 goto done;
5686 if (rebase_status == GOT_STATUS_CONFLICT) {
5687 error = got_worktree_histedit_postpone(worktree, fileindex);
5688 if (error)
5689 goto done;
5690 error = got_error_msg(GOT_ERR_CONFLICTS,
5691 "conflicts must be resolved before rebasing can continue");
5692 } else
5693 error = histedit_complete(worktree, fileindex, tmp_branch,
5694 branch, repo);
5695 done:
5696 got_object_id_queue_free(&commits);
5697 histedit_free_list(&histedit_cmds);
5698 free(head_commit_id);
5699 free(base_commit_id);
5700 free(resume_commit_id);
5701 if (commit)
5702 got_object_commit_close(commit);
5703 if (branch)
5704 got_ref_close(branch);
5705 if (tmp_branch)
5706 got_ref_close(tmp_branch);
5707 if (worktree)
5708 got_worktree_close(worktree);
5709 if (repo)
5710 got_repo_close(repo);
5711 return error;
5714 __dead static void
5715 usage_stage(void)
5717 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5718 "[file-path ...]\n",
5719 getprogname());
5720 exit(1);
5723 static const struct got_error *
5724 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5725 const char *path, struct got_object_id *blob_id,
5726 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5728 const struct got_error *err = NULL;
5729 char *id_str = NULL;
5731 if (staged_status != GOT_STATUS_ADD &&
5732 staged_status != GOT_STATUS_MODIFY &&
5733 staged_status != GOT_STATUS_DELETE)
5734 return NULL;
5736 if (staged_status == GOT_STATUS_ADD ||
5737 staged_status == GOT_STATUS_MODIFY)
5738 err = got_object_id_str(&id_str, staged_blob_id);
5739 else
5740 err = got_object_id_str(&id_str, blob_id);
5741 if (err)
5742 return err;
5744 printf("%s %c %s\n", id_str, staged_status, path);
5745 free(id_str);
5746 return NULL;
5749 static const struct got_error *
5750 cmd_stage(int argc, char *argv[])
5752 const struct got_error *error = NULL;
5753 struct got_repository *repo = NULL;
5754 struct got_worktree *worktree = NULL;
5755 char *cwd = NULL;
5756 struct got_pathlist_head paths;
5757 struct got_pathlist_entry *pe;
5758 int ch, list_stage = 0, pflag = 0;
5759 FILE *patch_script_file = NULL;
5760 const char *patch_script_path = NULL;
5761 struct choose_patch_arg cpa;
5763 TAILQ_INIT(&paths);
5765 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5766 switch (ch) {
5767 case 'l':
5768 list_stage = 1;
5769 break;
5770 case 'p':
5771 pflag = 1;
5772 break;
5773 case 'F':
5774 patch_script_path = optarg;
5775 break;
5776 default:
5777 usage_stage();
5778 /* NOTREACHED */
5782 argc -= optind;
5783 argv += optind;
5785 #ifndef PROFILE
5786 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5787 "unveil", NULL) == -1)
5788 err(1, "pledge");
5789 #endif
5790 if (list_stage && (pflag || patch_script_path))
5791 errx(1, "-l option cannot be used with other options");
5792 if (patch_script_path && !pflag)
5793 errx(1, "-F option can only be used together with -p option");
5795 cwd = getcwd(NULL, 0);
5796 if (cwd == NULL) {
5797 error = got_error_from_errno("getcwd");
5798 goto done;
5801 error = got_worktree_open(&worktree, cwd);
5802 if (error)
5803 goto done;
5805 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5806 if (error != NULL)
5807 goto done;
5809 if (patch_script_path) {
5810 patch_script_file = fopen(patch_script_path, "r");
5811 if (patch_script_file == NULL) {
5812 error = got_error_from_errno2("fopen",
5813 patch_script_path);
5814 goto done;
5817 error = apply_unveil(got_repo_get_path(repo), 1,
5818 got_worktree_get_root_path(worktree));
5819 if (error)
5820 goto done;
5822 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5823 if (error)
5824 goto done;
5826 if (list_stage)
5827 error = got_worktree_status(worktree, &paths, repo,
5828 print_stage, NULL, check_cancelled, NULL);
5829 else {
5830 cpa.patch_script_file = patch_script_file;
5831 cpa.action = "stage";
5832 error = got_worktree_stage(worktree, &paths,
5833 pflag ? NULL : print_status, NULL,
5834 pflag ? choose_patch : NULL, &cpa, repo);
5836 done:
5837 if (patch_script_file && fclose(patch_script_file) == EOF &&
5838 error == NULL)
5839 error = got_error_from_errno2("fclose", patch_script_path);
5840 if (repo)
5841 got_repo_close(repo);
5842 if (worktree)
5843 got_worktree_close(worktree);
5844 TAILQ_FOREACH(pe, &paths, entry)
5845 free((char *)pe->path);
5846 got_pathlist_free(&paths);
5847 free(cwd);
5848 return error;
5851 __dead static void
5852 usage_unstage(void)
5854 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5855 "[file-path ...]\n",
5856 getprogname());
5857 exit(1);
5861 static const struct got_error *
5862 cmd_unstage(int argc, char *argv[])
5864 const struct got_error *error = NULL;
5865 struct got_repository *repo = NULL;
5866 struct got_worktree *worktree = NULL;
5867 char *cwd = NULL;
5868 struct got_pathlist_head paths;
5869 struct got_pathlist_entry *pe;
5870 int ch, did_something = 0, pflag = 0;
5871 FILE *patch_script_file = NULL;
5872 const char *patch_script_path = NULL;
5873 struct choose_patch_arg cpa;
5875 TAILQ_INIT(&paths);
5877 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5878 switch (ch) {
5879 case 'p':
5880 pflag = 1;
5881 break;
5882 case 'F':
5883 patch_script_path = optarg;
5884 break;
5885 default:
5886 usage_unstage();
5887 /* NOTREACHED */
5891 argc -= optind;
5892 argv += optind;
5894 #ifndef PROFILE
5895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5896 "unveil", NULL) == -1)
5897 err(1, "pledge");
5898 #endif
5899 if (patch_script_path && !pflag)
5900 errx(1, "-F option can only be used together with -p option");
5902 cwd = getcwd(NULL, 0);
5903 if (cwd == NULL) {
5904 error = got_error_from_errno("getcwd");
5905 goto done;
5908 error = got_worktree_open(&worktree, cwd);
5909 if (error)
5910 goto done;
5912 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5913 if (error != NULL)
5914 goto done;
5916 if (patch_script_path) {
5917 patch_script_file = fopen(patch_script_path, "r");
5918 if (patch_script_file == NULL) {
5919 error = got_error_from_errno2("fopen",
5920 patch_script_path);
5921 goto done;
5925 error = apply_unveil(got_repo_get_path(repo), 1,
5926 got_worktree_get_root_path(worktree));
5927 if (error)
5928 goto done;
5930 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5931 if (error)
5932 goto done;
5934 cpa.patch_script_file = patch_script_file;
5935 cpa.action = "unstage";
5936 error = got_worktree_unstage(worktree, &paths, update_progress,
5937 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5938 done:
5939 if (patch_script_file && fclose(patch_script_file) == EOF &&
5940 error == NULL)
5941 error = got_error_from_errno2("fclose", patch_script_path);
5942 if (repo)
5943 got_repo_close(repo);
5944 if (worktree)
5945 got_worktree_close(worktree);
5946 TAILQ_FOREACH(pe, &paths, entry)
5947 free((char *)pe->path);
5948 got_pathlist_free(&paths);
5949 free(cwd);
5950 return error;