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/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
58 static void
59 catch_sigint(int signo)
60 {
61 sigint_received = 1;
62 }
64 static void
65 catch_sigpipe(int signo)
66 {
67 sigpipe_received = 1;
68 }
71 struct got_cmd {
72 const char *cmd_name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
76 };
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
98 __dead static void usage_stage(void);
100 static const struct got_error* cmd_init(int, char *[]);
101 static const struct got_error* cmd_import(int, char *[]);
102 static const struct got_error* cmd_checkout(int, char *[]);
103 static const struct got_error* cmd_update(int, char *[]);
104 static const struct got_error* cmd_log(int, char *[]);
105 static const struct got_error* cmd_diff(int, char *[]);
106 static const struct got_error* cmd_blame(int, char *[]);
107 static const struct got_error* cmd_tree(int, char *[]);
108 static const struct got_error* cmd_status(int, char *[]);
109 static const struct got_error* cmd_ref(int, char *[]);
110 static const struct got_error* cmd_branch(int, char *[]);
111 static const struct got_error* cmd_add(int, char *[]);
112 static const struct got_error* cmd_remove(int, char *[]);
113 static const struct got_error* cmd_revert(int, char *[]);
114 static const struct got_error* cmd_commit(int, char *[]);
115 static const struct got_error* cmd_cherrypick(int, char *[]);
116 static const struct got_error* cmd_backout(int, char *[]);
117 static const struct got_error* cmd_rebase(int, char *[]);
118 static const struct got_error* cmd_histedit(int, char *[]);
119 static const struct got_error* cmd_stage(int, char *[]);
121 static struct got_cmd got_commands[] = {
122 { "init", cmd_init, usage_init, "" },
123 { "import", cmd_import, usage_import, "" },
124 { "checkout", cmd_checkout, usage_checkout, "co" },
125 { "update", cmd_update, usage_update, "up" },
126 { "log", cmd_log, usage_log, "" },
127 { "diff", cmd_diff, usage_diff, "" },
128 { "blame", cmd_blame, usage_blame, "" },
129 { "tree", cmd_tree, usage_tree, "" },
130 { "status", cmd_status, usage_status, "st" },
131 { "ref", cmd_ref, usage_ref, "" },
132 { "branch", cmd_branch, usage_branch, "br" },
133 { "add", cmd_add, usage_add, "" },
134 { "remove", cmd_remove, usage_remove, "rm" },
135 { "revert", cmd_revert, usage_revert, "rv" },
136 { "commit", cmd_commit, usage_commit, "ci" },
137 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
138 { "backout", cmd_backout, usage_backout, "bo" },
139 { "rebase", cmd_rebase, usage_rebase, "rb" },
140 { "histedit", cmd_histedit, usage_histedit, "he" },
141 { "stage", cmd_stage, usage_stage, "sg" },
142 };
144 static void
145 list_commands(void)
147 int i;
149 fprintf(stderr, "commands:");
150 for (i = 0; i < nitems(got_commands); i++) {
151 struct got_cmd *cmd = &got_commands[i];
152 fprintf(stderr, " %s", cmd->cmd_name);
154 fputc('\n', stderr);
157 int
158 main(int argc, char *argv[])
160 struct got_cmd *cmd;
161 unsigned int i;
162 int ch;
163 int hflag = 0, Vflag = 0;
165 setlocale(LC_CTYPE, "");
167 while ((ch = getopt(argc, argv, "hV")) != -1) {
168 switch (ch) {
169 case 'h':
170 hflag = 1;
171 break;
172 case 'V':
173 Vflag = 1;
174 break;
175 default:
176 usage(hflag);
177 /* NOTREACHED */
181 argc -= optind;
182 argv += optind;
183 optind = 0;
185 if (Vflag) {
186 got_version_print_str();
187 return 1;
190 if (argc <= 0)
191 usage(hflag);
193 signal(SIGINT, catch_sigint);
194 signal(SIGPIPE, catch_sigpipe);
196 for (i = 0; i < nitems(got_commands); i++) {
197 const struct got_error *error;
199 cmd = &got_commands[i];
201 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
202 strcmp(cmd->cmd_alias, argv[0]) != 0)
203 continue;
205 if (hflag)
206 got_commands[i].cmd_usage();
208 error = got_commands[i].cmd_main(argc, argv);
209 if (error && !(sigint_received || sigpipe_received)) {
210 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
211 return 1;
214 return 0;
217 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
218 list_commands();
219 return 1;
222 __dead static void
223 usage(int hflag)
225 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
226 getprogname());
227 if (hflag)
228 list_commands();
229 exit(1);
232 static const struct got_error *
233 get_editor(char **abspath)
235 const struct got_error *err = NULL;
236 const char *editor;
238 editor = getenv("VISUAL");
239 if (editor == NULL)
240 editor = getenv("EDITOR");
242 if (editor) {
243 err = got_path_find_prog(abspath, editor);
244 if (err)
245 return err;
248 if (*abspath == NULL) {
249 *abspath = strdup("/bin/ed");
250 if (*abspath == NULL)
251 return got_error_from_errno("strdup");
254 return NULL;
257 static const struct got_error *
258 apply_unveil(const char *repo_path, int repo_read_only,
259 const char *worktree_path)
261 const struct got_error *err;
263 #ifdef PROFILE
264 if (unveil("gmon.out", "rwc") != 0)
265 return got_error_from_errno2("unveil", "gmon.out");
266 #endif
267 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
268 return got_error_from_errno2("unveil", repo_path);
270 if (worktree_path && unveil(worktree_path, "rwc") != 0)
271 return got_error_from_errno2("unveil", worktree_path);
273 if (unveil("/tmp", "rwc") != 0)
274 return got_error_from_errno2("unveil", "/tmp");
276 err = got_privsep_unveil_exec_helpers();
277 if (err != NULL)
278 return err;
280 if (unveil(NULL, NULL) != 0)
281 return got_error_from_errno("unveil");
283 return NULL;
286 __dead static void
287 usage_init(void)
289 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
290 exit(1);
293 static const struct got_error *
294 cmd_init(int argc, char *argv[])
296 const struct got_error *error = NULL;
297 char *repo_path = NULL;
298 int ch;
300 while ((ch = getopt(argc, argv, "")) != -1) {
301 switch (ch) {
302 default:
303 usage_init();
304 /* NOTREACHED */
308 argc -= optind;
309 argv += optind;
311 #ifndef PROFILE
312 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
313 err(1, "pledge");
314 #endif
315 if (argc != 1)
316 usage_init();
318 repo_path = strdup(argv[0]);
319 if (repo_path == NULL)
320 return got_error_from_errno("strdup");
322 got_path_strip_trailing_slashes(repo_path);
324 error = got_path_mkdir(repo_path);
325 if (error &&
326 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
327 goto done;
329 error = apply_unveil(repo_path, 0, NULL);
330 if (error)
331 goto done;
333 error = got_repo_init(repo_path);
334 if (error != NULL)
335 goto done;
337 done:
338 free(repo_path);
339 return error;
342 __dead static void
343 usage_import(void)
345 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
346 "[-r repository-path] [-I pattern] path\n", getprogname());
347 exit(1);
350 int
351 spawn_editor(const char *editor, const char *file)
353 pid_t pid;
354 sig_t sighup, sigint, sigquit;
355 int st = -1;
357 sighup = signal(SIGHUP, SIG_IGN);
358 sigint = signal(SIGINT, SIG_IGN);
359 sigquit = signal(SIGQUIT, SIG_IGN);
361 switch (pid = fork()) {
362 case -1:
363 goto doneediting;
364 case 0:
365 execl(editor, editor, file, (char *)NULL);
366 _exit(127);
369 while (waitpid(pid, &st, 0) == -1)
370 if (errno != EINTR)
371 break;
373 doneediting:
374 (void)signal(SIGHUP, sighup);
375 (void)signal(SIGINT, sigint);
376 (void)signal(SIGQUIT, sigquit);
378 if (!WIFEXITED(st)) {
379 errno = EINTR;
380 return -1;
383 return WEXITSTATUS(st);
386 static const struct got_error *
387 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
388 const char *initial_content)
390 const struct got_error *err = NULL;
391 char buf[1024];
392 struct stat st, st2;
393 FILE *fp;
394 int content_changed = 0;
395 size_t len;
397 *logmsg = NULL;
399 if (stat(logmsg_path, &st) == -1)
400 return got_error_from_errno2("stat", logmsg_path);
402 if (spawn_editor(editor, logmsg_path) == -1)
403 return got_error_from_errno("failed spawning editor");
405 if (stat(logmsg_path, &st2) == -1)
406 return got_error_from_errno("stat");
408 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
409 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
410 "no changes made to commit message, aborting");
412 *logmsg = malloc(st2.st_size + 1);
413 if (*logmsg == NULL)
414 return got_error_from_errno("malloc");
415 (*logmsg)[0] = '\0';
416 len = 0;
418 fp = fopen(logmsg_path, "r");
419 if (fp == NULL) {
420 err = got_error_from_errno("fopen");
421 goto done;
423 while (fgets(buf, sizeof(buf), fp) != NULL) {
424 if (!content_changed && strcmp(buf, initial_content) != 0)
425 content_changed = 1;
426 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
427 continue; /* remove comments and leading empty lines */
428 len = strlcat(*logmsg, buf, st2.st_size);
430 fclose(fp);
432 while (len > 0 && (*logmsg)[len - 1] == '\n') {
433 (*logmsg)[len - 1] = '\0';
434 len--;
437 if (len == 0 || !content_changed)
438 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 "commit message cannot be empty, aborting");
440 done:
441 if (err) {
442 free(*logmsg);
443 *logmsg = NULL;
445 return err;
448 static const struct got_error *
449 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
450 const char *branch_name)
452 char *initial_content = NULL, *logmsg_path = NULL;
453 const struct got_error *err = NULL;
454 int fd;
456 if (asprintf(&initial_content,
457 "\n# %s to be imported to branch %s\n", path_dir,
458 branch_name) == -1)
459 return got_error_from_errno("asprintf");
461 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
462 if (err)
463 goto done;
465 dprintf(fd, initial_content);
466 close(fd);
468 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
469 done:
470 free(initial_content);
471 free(logmsg_path);
472 return err;
475 static const struct got_error *
476 import_progress(void *arg, const char *path)
478 printf("A %s\n", path);
479 return NULL;
482 static const struct got_error *
483 cmd_import(int argc, char *argv[])
485 const struct got_error *error = NULL;
486 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
487 char *editor = NULL;
488 const char *got_author = getenv("GOT_AUTHOR");
489 const char *branch_name = "master";
490 char *refname = NULL, *id_str = NULL;
491 struct got_repository *repo = NULL;
492 struct got_reference *branch_ref = NULL, *head_ref = NULL;
493 struct got_object_id *new_commit_id = NULL;
494 int ch;
495 struct got_pathlist_head ignores;
496 struct got_pathlist_entry *pe;
498 TAILQ_INIT(&ignores);
500 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
501 switch (ch) {
502 case 'b':
503 branch_name = optarg;
504 break;
505 case 'm':
506 logmsg = strdup(optarg);
507 if (logmsg == NULL) {
508 error = got_error_from_errno("strdup");
509 goto done;
511 break;
512 case 'r':
513 repo_path = realpath(optarg, NULL);
514 if (repo_path == NULL) {
515 error = got_error_from_errno("realpath");
516 goto done;
518 break;
519 case 'I':
520 if (optarg[0] == '\0')
521 break;
522 error = got_pathlist_insert(&pe, &ignores, optarg,
523 NULL);
524 if (error)
525 goto done;
526 break;
527 default:
528 usage_init();
529 /* NOTREACHED */
533 argc -= optind;
534 argv += optind;
536 #ifndef PROFILE
537 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
538 NULL) == -1)
539 err(1, "pledge");
540 #endif
541 if (argc != 1)
542 usage_import();
544 if (got_author == NULL) {
545 /* TODO: Look current user up in password database */
546 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
547 goto done;
550 if (repo_path == NULL) {
551 repo_path = getcwd(NULL, 0);
552 if (repo_path == NULL)
553 return got_error_from_errno("getcwd");
555 got_path_strip_trailing_slashes(repo_path);
556 error = got_repo_open(&repo, repo_path);
557 if (error)
558 goto done;
560 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
561 error = got_error_from_errno("asprintf");
562 goto done;
565 error = got_ref_open(&branch_ref, repo, refname, 0);
566 if (error) {
567 if (error->code != GOT_ERR_NOT_REF)
568 goto done;
569 } else {
570 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
571 "import target branch already exists");
572 goto done;
575 path_dir = realpath(argv[0], NULL);
576 if (path_dir == NULL) {
577 error = got_error_from_errno("realpath");
578 goto done;
580 got_path_strip_trailing_slashes(path_dir);
582 /*
583 * unveil(2) traverses exec(2); if an editor is used we have
584 * to apply unveil after the log message has been written.
585 */
586 if (logmsg == NULL || strlen(logmsg) == 0) {
587 error = get_editor(&editor);
588 if (error)
589 goto done;
590 error = collect_import_msg(&logmsg, editor, path_dir, refname);
591 if (error)
592 goto done;
595 if (unveil(path_dir, "r") != 0)
596 return got_error_from_errno2("unveil", path_dir);
598 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
599 if (error)
600 goto done;
602 error = got_repo_import(&new_commit_id, path_dir, logmsg,
603 got_author, &ignores, repo, import_progress, NULL);
604 if (error)
605 goto done;
607 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
608 if (error)
609 goto done;
611 error = got_ref_write(branch_ref, repo);
612 if (error)
613 goto done;
615 error = got_object_id_str(&id_str, new_commit_id);
616 if (error)
617 goto done;
619 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
620 if (error) {
621 if (error->code != GOT_ERR_NOT_REF)
622 goto done;
624 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
625 branch_ref);
626 if (error)
627 goto done;
629 error = got_ref_write(head_ref, repo);
630 if (error)
631 goto done;
634 printf("Created branch %s with commit %s\n",
635 got_ref_get_name(branch_ref), id_str);
636 done:
637 free(repo_path);
638 free(editor);
639 free(refname);
640 free(new_commit_id);
641 free(id_str);
642 if (branch_ref)
643 got_ref_close(branch_ref);
644 if (head_ref)
645 got_ref_close(head_ref);
646 return error;
649 __dead static void
650 usage_checkout(void)
652 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
653 "[-p prefix] repository-path [worktree-path]\n", getprogname());
654 exit(1);
657 static const struct got_error *
658 checkout_progress(void *arg, unsigned char status, const char *path)
660 char *worktree_path = arg;
662 /* Base commit bump happens silently. */
663 if (status == GOT_STATUS_BUMP_BASE)
664 return NULL;
666 while (path[0] == '/')
667 path++;
669 printf("%c %s/%s\n", status, worktree_path, path);
670 return NULL;
673 static const struct got_error *
674 check_cancelled(void *arg)
676 if (sigint_received || sigpipe_received)
677 return got_error(GOT_ERR_CANCELLED);
678 return NULL;
681 static const struct got_error *
682 check_linear_ancestry(struct got_object_id *commit_id,
683 struct got_object_id *base_commit_id, struct got_repository *repo)
685 const struct got_error *err = NULL;
686 struct got_object_id *yca_id;
688 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
689 commit_id, base_commit_id, repo);
690 if (err)
691 return err;
693 if (yca_id == NULL)
694 return got_error(GOT_ERR_ANCESTRY);
696 /*
697 * Require a straight line of history between the target commit
698 * and the work tree's base commit.
700 * Non-linear situations such as this require a rebase:
702 * (commit) D F (base_commit)
703 * \ /
704 * C E
705 * \ /
706 * B (yca)
707 * |
708 * A
710 * 'got update' only handles linear cases:
711 * Update forwards in time: A (base/yca) - B - C - D (commit)
712 * Update backwards in time: D (base) - C - B - A (commit/yca)
713 */
714 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
715 got_object_id_cmp(base_commit_id, yca_id) != 0)
716 return got_error(GOT_ERR_ANCESTRY);
718 free(yca_id);
719 return NULL;
722 static const struct got_error *
723 check_same_branch(struct got_object_id *commit_id,
724 struct got_reference *head_ref, struct got_object_id *yca_id,
725 struct got_repository *repo)
727 const struct got_error *err = NULL;
728 struct got_commit_graph *graph = NULL;
729 struct got_object_id *head_commit_id = NULL;
730 int is_same_branch = 0;
732 err = got_ref_resolve(&head_commit_id, repo, head_ref);
733 if (err)
734 goto done;
736 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
737 is_same_branch = 1;
738 goto done;
740 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
741 is_same_branch = 1;
742 goto done;
745 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
746 if (err)
747 goto done;
749 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
750 if (err)
751 goto done;
753 for (;;) {
754 struct got_object_id *id;
755 err = got_commit_graph_iter_next(&id, graph);
756 if (err) {
757 if (err->code == GOT_ERR_ITER_COMPLETED) {
758 err = NULL;
759 break;
760 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
761 break;
762 err = got_commit_graph_fetch_commits(graph, 1,
763 repo);
764 if (err)
765 break;
768 if (id) {
769 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
770 break;
771 if (got_object_id_cmp(id, commit_id) == 0) {
772 is_same_branch = 1;
773 break;
777 done:
778 if (graph)
779 got_commit_graph_close(graph);
780 free(head_commit_id);
781 if (!err && !is_same_branch)
782 err = got_error(GOT_ERR_ANCESTRY);
783 return err;
786 static const struct got_error *
787 resolve_commit_arg(struct got_object_id **commit_id,
788 const char *commit_id_arg, struct got_repository *repo)
790 const struct got_error *err;
791 struct got_reference *ref;
793 err = got_ref_open(&ref, repo, commit_id_arg, 0);
794 if (err == NULL) {
795 err = got_ref_resolve(commit_id, repo, ref);
796 got_ref_close(ref);
797 } else {
798 if (err->code != GOT_ERR_NOT_REF)
799 return err;
800 err = got_repo_match_object_id_prefix(commit_id,
801 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
803 return err;
806 static const struct got_error *
807 cmd_checkout(int argc, char *argv[])
809 const struct got_error *error = NULL;
810 struct got_repository *repo = NULL;
811 struct got_reference *head_ref = NULL;
812 struct got_worktree *worktree = NULL;
813 char *repo_path = NULL;
814 char *worktree_path = NULL;
815 const char *path_prefix = "";
816 const char *branch_name = GOT_REF_HEAD;
817 char *commit_id_str = NULL;
818 int ch, same_path_prefix;
819 struct got_pathlist_head paths;
821 TAILQ_INIT(&paths);
823 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
824 switch (ch) {
825 case 'b':
826 branch_name = optarg;
827 break;
828 case 'c':
829 commit_id_str = strdup(optarg);
830 if (commit_id_str == NULL)
831 return got_error_from_errno("strdup");
832 break;
833 case 'p':
834 path_prefix = optarg;
835 break;
836 default:
837 usage_checkout();
838 /* NOTREACHED */
842 argc -= optind;
843 argv += optind;
845 #ifndef PROFILE
846 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
847 "unveil", NULL) == -1)
848 err(1, "pledge");
849 #endif
850 if (argc == 1) {
851 char *cwd, *base, *dotgit;
852 repo_path = realpath(argv[0], NULL);
853 if (repo_path == NULL)
854 return got_error_from_errno2("realpath", argv[0]);
855 cwd = getcwd(NULL, 0);
856 if (cwd == NULL) {
857 error = got_error_from_errno("getcwd");
858 goto done;
860 if (path_prefix[0]) {
861 base = basename(path_prefix);
862 if (base == NULL) {
863 error = got_error_from_errno2("basename",
864 path_prefix);
865 goto done;
867 } else {
868 base = basename(repo_path);
869 if (base == NULL) {
870 error = got_error_from_errno2("basename",
871 repo_path);
872 goto done;
875 dotgit = strstr(base, ".git");
876 if (dotgit)
877 *dotgit = '\0';
878 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
879 error = got_error_from_errno("asprintf");
880 free(cwd);
881 goto done;
883 free(cwd);
884 } else if (argc == 2) {
885 repo_path = realpath(argv[0], NULL);
886 if (repo_path == NULL) {
887 error = got_error_from_errno2("realpath", argv[0]);
888 goto done;
890 worktree_path = realpath(argv[1], NULL);
891 if (worktree_path == NULL) {
892 if (errno != ENOENT) {
893 error = got_error_from_errno2("realpath",
894 argv[1]);
895 goto done;
897 worktree_path = strdup(argv[1]);
898 if (worktree_path == NULL) {
899 error = got_error_from_errno("strdup");
900 goto done;
903 } else
904 usage_checkout();
906 got_path_strip_trailing_slashes(repo_path);
907 got_path_strip_trailing_slashes(worktree_path);
909 error = got_repo_open(&repo, repo_path);
910 if (error != NULL)
911 goto done;
913 /* Pre-create work tree path for unveil(2) */
914 error = got_path_mkdir(worktree_path);
915 if (error) {
916 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
917 goto done;
918 if (!got_path_dir_is_empty(worktree_path)) {
919 error = got_error_path(worktree_path,
920 GOT_ERR_DIR_NOT_EMPTY);
921 goto done;
925 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
926 if (error)
927 goto done;
929 error = got_ref_open(&head_ref, repo, branch_name, 0);
930 if (error != NULL)
931 goto done;
933 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
934 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
935 goto done;
937 error = got_worktree_open(&worktree, worktree_path);
938 if (error != NULL)
939 goto done;
941 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
942 path_prefix);
943 if (error != NULL)
944 goto done;
945 if (!same_path_prefix) {
946 error = got_error(GOT_ERR_PATH_PREFIX);
947 goto done;
950 if (commit_id_str) {
951 struct got_object_id *commit_id;
952 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
953 if (error)
954 goto done;
955 error = check_linear_ancestry(commit_id,
956 got_worktree_get_base_commit_id(worktree), repo);
957 if (error != NULL) {
958 free(commit_id);
959 goto done;
961 error = check_same_branch(commit_id, head_ref, NULL, repo);
962 if (error)
963 goto done;
964 error = got_worktree_set_base_commit_id(worktree, repo,
965 commit_id);
966 free(commit_id);
967 if (error)
968 goto done;
971 error = got_pathlist_append(&paths, "", NULL);
972 if (error)
973 goto done;
974 error = got_worktree_checkout_files(worktree, &paths, repo,
975 checkout_progress, worktree_path, check_cancelled, NULL);
976 if (error != NULL)
977 goto done;
979 printf("Now shut up and hack\n");
981 done:
982 got_pathlist_free(&paths);
983 free(commit_id_str);
984 free(repo_path);
985 free(worktree_path);
986 return error;
989 __dead static void
990 usage_update(void)
992 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
993 getprogname());
994 exit(1);
997 static const struct got_error *
998 update_progress(void *arg, unsigned char status, const char *path)
1000 int *did_something = arg;
1002 if (status == GOT_STATUS_EXISTS)
1003 return NULL;
1005 *did_something = 1;
1007 /* Base commit bump happens silently. */
1008 if (status == GOT_STATUS_BUMP_BASE)
1009 return NULL;
1011 while (path[0] == '/')
1012 path++;
1013 printf("%c %s\n", status, path);
1014 return NULL;
1017 static const struct got_error *
1018 switch_head_ref(struct got_reference *head_ref,
1019 struct got_object_id *commit_id, struct got_worktree *worktree,
1020 struct got_repository *repo)
1022 const struct got_error *err = NULL;
1023 char *base_id_str;
1024 int ref_has_moved = 0;
1026 /* Trivial case: switching between two different references. */
1027 if (strcmp(got_ref_get_name(head_ref),
1028 got_worktree_get_head_ref_name(worktree)) != 0) {
1029 printf("Switching work tree from %s to %s\n",
1030 got_worktree_get_head_ref_name(worktree),
1031 got_ref_get_name(head_ref));
1032 return got_worktree_set_head_ref(worktree, head_ref);
1035 err = check_linear_ancestry(commit_id,
1036 got_worktree_get_base_commit_id(worktree), repo);
1037 if (err) {
1038 if (err->code != GOT_ERR_ANCESTRY)
1039 return err;
1040 ref_has_moved = 1;
1042 if (!ref_has_moved)
1043 return NULL;
1045 /* Switching to a rebased branch with the same reference name. */
1046 err = got_object_id_str(&base_id_str,
1047 got_worktree_get_base_commit_id(worktree));
1048 if (err)
1049 return err;
1050 printf("Reference %s now points at a different branch\n",
1051 got_worktree_get_head_ref_name(worktree));
1052 printf("Switching work tree from %s to %s\n", base_id_str,
1053 got_worktree_get_head_ref_name(worktree));
1054 return NULL;
1057 static const struct got_error *
1058 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1060 const struct got_error *err;
1061 int in_progress;
1063 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1064 if (err)
1065 return err;
1066 if (in_progress)
1067 return got_error(GOT_ERR_REBASING);
1069 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1070 if (err)
1071 return err;
1072 if (in_progress)
1073 return got_error(GOT_ERR_HISTEDIT_BUSY);
1075 return NULL;
1078 static const struct got_error *
1079 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1080 char *argv[], struct got_worktree *worktree)
1082 const struct got_error *err;
1083 char *path;
1084 int i;
1086 if (argc == 0) {
1087 path = strdup("");
1088 if (path == NULL)
1089 return got_error_from_errno("strdup");
1090 return got_pathlist_append(paths, path, NULL);
1093 for (i = 0; i < argc; i++) {
1094 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1095 if (err)
1096 break;
1097 err = got_pathlist_append(paths, path, NULL);
1098 if (err) {
1099 free(path);
1100 break;
1104 return err;
1107 static const struct got_error *
1108 cmd_update(int argc, char *argv[])
1110 const struct got_error *error = NULL;
1111 struct got_repository *repo = NULL;
1112 struct got_worktree *worktree = NULL;
1113 char *worktree_path = NULL;
1114 struct got_object_id *commit_id = NULL;
1115 char *commit_id_str = NULL;
1116 const char *branch_name = NULL;
1117 struct got_reference *head_ref = NULL;
1118 struct got_pathlist_head paths;
1119 struct got_pathlist_entry *pe;
1120 int ch, did_something = 0;
1122 TAILQ_INIT(&paths);
1124 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1125 switch (ch) {
1126 case 'b':
1127 branch_name = optarg;
1128 break;
1129 case 'c':
1130 commit_id_str = strdup(optarg);
1131 if (commit_id_str == NULL)
1132 return got_error_from_errno("strdup");
1133 break;
1134 default:
1135 usage_update();
1136 /* NOTREACHED */
1140 argc -= optind;
1141 argv += optind;
1143 #ifndef PROFILE
1144 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1145 "unveil", NULL) == -1)
1146 err(1, "pledge");
1147 #endif
1148 worktree_path = getcwd(NULL, 0);
1149 if (worktree_path == NULL) {
1150 error = got_error_from_errno("getcwd");
1151 goto done;
1153 error = got_worktree_open(&worktree, worktree_path);
1154 if (error)
1155 goto done;
1157 error = check_rebase_or_histedit_in_progress(worktree);
1158 if (error)
1159 goto done;
1161 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1162 if (error)
1163 goto done;
1165 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1166 if (error != NULL)
1167 goto done;
1169 error = apply_unveil(got_repo_get_path(repo), 0,
1170 got_worktree_get_root_path(worktree));
1171 if (error)
1172 goto done;
1174 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1175 got_worktree_get_head_ref_name(worktree), 0);
1176 if (error != NULL)
1177 goto done;
1178 if (commit_id_str == NULL) {
1179 error = got_ref_resolve(&commit_id, repo, head_ref);
1180 if (error != NULL)
1181 goto done;
1182 error = got_object_id_str(&commit_id_str, commit_id);
1183 if (error != NULL)
1184 goto done;
1185 } else {
1186 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1187 free(commit_id_str);
1188 commit_id_str = NULL;
1189 if (error)
1190 goto done;
1191 error = got_object_id_str(&commit_id_str, commit_id);
1192 if (error)
1193 goto done;
1196 if (branch_name) {
1197 struct got_object_id *head_commit_id;
1198 TAILQ_FOREACH(pe, &paths, entry) {
1199 if (pe->path_len == 0)
1200 continue;
1201 error = got_error_msg(GOT_ERR_BAD_PATH,
1202 "switching between branches requires that "
1203 "the entire work tree gets updated");
1204 goto done;
1206 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1207 if (error)
1208 goto done;
1209 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1210 free(head_commit_id);
1211 if (error != NULL)
1212 goto done;
1213 error = check_same_branch(commit_id, head_ref, NULL, repo);
1214 if (error)
1215 goto done;
1216 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1217 if (error)
1218 goto done;
1219 } else {
1220 error = check_linear_ancestry(commit_id,
1221 got_worktree_get_base_commit_id(worktree), repo);
1222 if (error != NULL) {
1223 if (error->code == GOT_ERR_ANCESTRY)
1224 error = got_error(GOT_ERR_BRANCH_MOVED);
1225 goto done;
1227 error = check_same_branch(commit_id, head_ref, NULL, repo);
1228 if (error)
1229 goto done;
1232 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1233 commit_id) != 0) {
1234 error = got_worktree_set_base_commit_id(worktree, repo,
1235 commit_id);
1236 if (error)
1237 goto done;
1240 error = got_worktree_checkout_files(worktree, &paths, repo,
1241 update_progress, &did_something, check_cancelled, NULL);
1242 if (error != NULL)
1243 goto done;
1245 if (did_something)
1246 printf("Updated to commit %s\n", commit_id_str);
1247 else
1248 printf("Already up-to-date\n");
1249 done:
1250 free(worktree_path);
1251 TAILQ_FOREACH(pe, &paths, entry)
1252 free((char *)pe->path);
1253 got_pathlist_free(&paths);
1254 free(commit_id);
1255 free(commit_id_str);
1256 return error;
1259 static const struct got_error *
1260 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1261 int diff_context, struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 struct got_tree_object *tree1 = NULL, *tree2;
1265 struct got_object_qid *qid;
1266 char *id_str1 = NULL, *id_str2;
1267 struct got_diff_blob_output_unidiff_arg arg;
1269 err = got_object_open_as_tree(&tree2, repo,
1270 got_object_commit_get_tree_id(commit));
1271 if (err)
1272 return err;
1274 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1275 if (qid != NULL) {
1276 struct got_commit_object *pcommit;
1278 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1279 if (err)
1280 return err;
1282 err = got_object_open_as_tree(&tree1, repo,
1283 got_object_commit_get_tree_id(pcommit));
1284 got_object_commit_close(pcommit);
1285 if (err)
1286 return err;
1288 err = got_object_id_str(&id_str1, qid->id);
1289 if (err)
1290 return err;
1293 err = got_object_id_str(&id_str2, id);
1294 if (err)
1295 goto done;
1297 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1298 arg.diff_context = diff_context;
1299 arg.outfile = stdout;
1300 err = got_diff_tree(tree1, tree2, "", "", repo,
1301 got_diff_blob_output_unidiff, &arg, 1);
1302 done:
1303 if (tree1)
1304 got_object_tree_close(tree1);
1305 got_object_tree_close(tree2);
1306 free(id_str1);
1307 free(id_str2);
1308 return err;
1311 static char *
1312 get_datestr(time_t *time, char *datebuf)
1314 char *p, *s = ctime_r(time, datebuf);
1315 p = strchr(s, '\n');
1316 if (p)
1317 *p = '\0';
1318 return s;
1321 static const struct got_error *
1322 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1323 struct got_repository *repo, int show_patch, int diff_context,
1324 struct got_reflist_head *refs)
1326 const struct got_error *err = NULL;
1327 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1328 char datebuf[26];
1329 time_t committer_time;
1330 const char *author, *committer;
1331 char *refs_str = NULL;
1332 struct got_reflist_entry *re;
1334 SIMPLEQ_FOREACH(re, refs, entry) {
1335 char *s;
1336 const char *name;
1337 if (got_object_id_cmp(re->id, id) != 0)
1338 continue;
1339 name = got_ref_get_name(re->ref);
1340 if (strcmp(name, GOT_REF_HEAD) == 0)
1341 continue;
1342 if (strncmp(name, "refs/", 5) == 0)
1343 name += 5;
1344 if (strncmp(name, "got/", 4) == 0)
1345 continue;
1346 if (strncmp(name, "heads/", 6) == 0)
1347 name += 6;
1348 if (strncmp(name, "remotes/", 8) == 0)
1349 name += 8;
1350 s = refs_str;
1351 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1352 name) == -1) {
1353 err = got_error_from_errno("asprintf");
1354 free(s);
1355 break;
1357 free(s);
1359 err = got_object_id_str(&id_str, id);
1360 if (err)
1361 return err;
1363 printf("-----------------------------------------------\n");
1364 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1365 refs_str ? refs_str : "", refs_str ? ")" : "");
1366 free(id_str);
1367 id_str = NULL;
1368 free(refs_str);
1369 refs_str = NULL;
1370 printf("from: %s\n", got_object_commit_get_author(commit));
1371 committer_time = got_object_commit_get_committer_time(commit);
1372 datestr = get_datestr(&committer_time, datebuf);
1373 printf("date: %s UTC\n", datestr);
1374 author = got_object_commit_get_author(commit);
1375 committer = got_object_commit_get_committer(commit);
1376 if (strcmp(author, committer) != 0)
1377 printf("via: %s\n", committer);
1378 if (got_object_commit_get_nparents(commit) > 1) {
1379 const struct got_object_id_queue *parent_ids;
1380 struct got_object_qid *qid;
1381 int n = 1;
1382 parent_ids = got_object_commit_get_parent_ids(commit);
1383 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1384 err = got_object_id_str(&id_str, qid->id);
1385 if (err)
1386 return err;
1387 printf("parent %d: %s\n", n++, id_str);
1388 free(id_str);
1392 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1393 if (logmsg0 == NULL)
1394 return got_error_from_errno("strdup");
1396 logmsg = logmsg0;
1397 do {
1398 line = strsep(&logmsg, "\n");
1399 if (line)
1400 printf(" %s\n", line);
1401 } while (line);
1402 free(logmsg0);
1404 if (show_patch) {
1405 err = print_patch(commit, id, diff_context, repo);
1406 if (err == 0)
1407 printf("\n");
1410 if (fflush(stdout) != 0 && err == NULL)
1411 err = got_error_from_errno("fflush");
1412 return err;
1415 static const struct got_error *
1416 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1417 char *path, int show_patch, int diff_context, int limit,
1418 int first_parent_traversal, struct got_reflist_head *refs)
1420 const struct got_error *err;
1421 struct got_commit_graph *graph;
1423 err = got_commit_graph_open(&graph, root_id, path,
1424 first_parent_traversal, repo);
1425 if (err)
1426 return err;
1427 err = got_commit_graph_iter_start(graph, root_id, repo);
1428 if (err)
1429 goto done;
1430 for (;;) {
1431 struct got_commit_object *commit;
1432 struct got_object_id *id;
1434 if (sigint_received || sigpipe_received)
1435 break;
1437 err = got_commit_graph_iter_next(&id, graph);
1438 if (err) {
1439 if (err->code == GOT_ERR_ITER_COMPLETED) {
1440 err = NULL;
1441 break;
1443 if (err->code != GOT_ERR_ITER_NEED_MORE)
1444 break;
1445 err = got_commit_graph_fetch_commits(graph, 1, repo);
1446 if (err)
1447 break;
1448 else
1449 continue;
1451 if (id == NULL)
1452 break;
1454 err = got_object_open_as_commit(&commit, repo, id);
1455 if (err)
1456 break;
1457 err = print_commit(commit, id, repo, show_patch, diff_context,
1458 refs);
1459 got_object_commit_close(commit);
1460 if (err || (limit && --limit == 0))
1461 break;
1463 done:
1464 got_commit_graph_close(graph);
1465 return err;
1468 __dead static void
1469 usage_log(void)
1471 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1472 "[-r repository-path] [path]\n", getprogname());
1473 exit(1);
1476 static const struct got_error *
1477 cmd_log(int argc, char *argv[])
1479 const struct got_error *error;
1480 struct got_repository *repo = NULL;
1481 struct got_worktree *worktree = NULL;
1482 struct got_commit_object *commit = NULL;
1483 struct got_object_id *id = NULL;
1484 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1485 char *start_commit = NULL;
1486 int diff_context = 3, ch;
1487 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1488 const char *errstr;
1489 struct got_reflist_head refs;
1491 SIMPLEQ_INIT(&refs);
1493 #ifndef PROFILE
1494 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1495 NULL)
1496 == -1)
1497 err(1, "pledge");
1498 #endif
1500 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1501 switch (ch) {
1502 case 'p':
1503 show_patch = 1;
1504 break;
1505 case 'c':
1506 start_commit = optarg;
1507 break;
1508 case 'C':
1509 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1510 &errstr);
1511 if (errstr != NULL)
1512 err(1, "-C option %s", errstr);
1513 break;
1514 case 'l':
1515 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1516 if (errstr != NULL)
1517 err(1, "-l option %s", errstr);
1518 break;
1519 case 'f':
1520 first_parent_traversal = 1;
1521 break;
1522 case 'r':
1523 repo_path = realpath(optarg, NULL);
1524 if (repo_path == NULL)
1525 err(1, "-r option");
1526 got_path_strip_trailing_slashes(repo_path);
1527 break;
1528 default:
1529 usage_log();
1530 /* NOTREACHED */
1534 argc -= optind;
1535 argv += optind;
1537 cwd = getcwd(NULL, 0);
1538 if (cwd == NULL) {
1539 error = got_error_from_errno("getcwd");
1540 goto done;
1543 error = got_worktree_open(&worktree, cwd);
1544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1545 goto done;
1546 error = NULL;
1548 if (argc == 0) {
1549 path = strdup("");
1550 if (path == NULL) {
1551 error = got_error_from_errno("strdup");
1552 goto done;
1554 } else if (argc == 1) {
1555 if (worktree) {
1556 error = got_worktree_resolve_path(&path, worktree,
1557 argv[0]);
1558 if (error)
1559 goto done;
1560 } else {
1561 path = strdup(argv[0]);
1562 if (path == NULL) {
1563 error = got_error_from_errno("strdup");
1564 goto done;
1567 } else
1568 usage_log();
1570 if (repo_path == NULL) {
1571 repo_path = worktree ?
1572 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1574 if (repo_path == NULL) {
1575 error = got_error_from_errno("strdup");
1576 goto done;
1579 error = got_repo_open(&repo, repo_path);
1580 if (error != NULL)
1581 goto done;
1583 error = apply_unveil(got_repo_get_path(repo), 1,
1584 worktree ? got_worktree_get_root_path(worktree) : NULL);
1585 if (error)
1586 goto done;
1588 if (start_commit == NULL) {
1589 struct got_reference *head_ref;
1590 error = got_ref_open(&head_ref, repo,
1591 worktree ? got_worktree_get_head_ref_name(worktree)
1592 : GOT_REF_HEAD, 0);
1593 if (error != NULL)
1594 return error;
1595 error = got_ref_resolve(&id, repo, head_ref);
1596 got_ref_close(head_ref);
1597 if (error != NULL)
1598 return error;
1599 error = got_object_open_as_commit(&commit, repo, id);
1600 } else {
1601 struct got_reference *ref;
1602 error = got_ref_open(&ref, repo, start_commit, 0);
1603 if (error == NULL) {
1604 int obj_type;
1605 error = got_ref_resolve(&id, repo, ref);
1606 got_ref_close(ref);
1607 if (error != NULL)
1608 goto done;
1609 error = got_object_get_type(&obj_type, repo, id);
1610 if (error != NULL)
1611 goto done;
1612 if (obj_type == GOT_OBJ_TYPE_TAG) {
1613 struct got_tag_object *tag;
1614 error = got_object_open_as_tag(&tag, repo, id);
1615 if (error != NULL)
1616 goto done;
1617 if (got_object_tag_get_object_type(tag) !=
1618 GOT_OBJ_TYPE_COMMIT) {
1619 got_object_tag_close(tag);
1620 error = got_error(GOT_ERR_OBJ_TYPE);
1621 goto done;
1623 free(id);
1624 id = got_object_id_dup(
1625 got_object_tag_get_object_id(tag));
1626 if (id == NULL)
1627 error = got_error_from_errno(
1628 "got_object_id_dup");
1629 got_object_tag_close(tag);
1630 if (error)
1631 goto done;
1632 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1633 error = got_error(GOT_ERR_OBJ_TYPE);
1634 goto done;
1636 error = got_object_open_as_commit(&commit, repo, id);
1637 if (error != NULL)
1638 goto done;
1640 if (commit == NULL) {
1641 error = got_repo_match_object_id_prefix(&id,
1642 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1643 if (error != NULL)
1644 return error;
1647 if (error != NULL)
1648 goto done;
1650 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1651 if (error != NULL)
1652 goto done;
1653 if (in_repo_path) {
1654 free(path);
1655 path = in_repo_path;
1658 error = got_ref_list(&refs, repo);
1659 if (error)
1660 goto done;
1662 error = print_commits(id, repo, path, show_patch,
1663 diff_context, limit, first_parent_traversal, &refs);
1664 done:
1665 free(path);
1666 free(repo_path);
1667 free(cwd);
1668 free(id);
1669 if (worktree)
1670 got_worktree_close(worktree);
1671 if (repo) {
1672 const struct got_error *repo_error;
1673 repo_error = got_repo_close(repo);
1674 if (error == NULL)
1675 error = repo_error;
1677 got_ref_list_free(&refs);
1678 return error;
1681 __dead static void
1682 usage_diff(void)
1684 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1685 "[object1 object2 | path]\n", getprogname());
1686 exit(1);
1689 struct print_diff_arg {
1690 struct got_repository *repo;
1691 struct got_worktree *worktree;
1692 int diff_context;
1693 const char *id_str;
1694 int header_shown;
1695 int diff_staged;
1698 static const struct got_error *
1699 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1700 const char *path, struct got_object_id *blob_id,
1701 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1703 struct print_diff_arg *a = arg;
1704 const struct got_error *err = NULL;
1705 struct got_blob_object *blob1 = NULL;
1706 FILE *f2 = NULL;
1707 char *abspath = NULL;
1708 struct stat sb;
1710 if (a->diff_staged) {
1711 if (staged_status != GOT_STATUS_MODIFY &&
1712 staged_status != GOT_STATUS_ADD &&
1713 staged_status != GOT_STATUS_DELETE)
1714 return NULL;
1715 } else {
1716 if (staged_status == GOT_STATUS_DELETE)
1717 return NULL;
1718 if (status != GOT_STATUS_MODIFY &&
1719 status != GOT_STATUS_ADD &&
1720 status != GOT_STATUS_DELETE &&
1721 status != GOT_STATUS_CONFLICT)
1722 return NULL;
1725 if (!a->header_shown) {
1726 printf("diff %s %s%s\n", a->id_str,
1727 got_worktree_get_root_path(a->worktree),
1728 a->diff_staged ? " (staged changes)" : "");
1729 a->header_shown = 1;
1732 if (a->diff_staged) {
1733 const char *label1 = NULL, *label2 = NULL;
1734 switch (staged_status) {
1735 case GOT_STATUS_MODIFY:
1736 label1 = path;
1737 label2 = path;
1738 break;
1739 case GOT_STATUS_ADD:
1740 label2 = path;
1741 break;
1742 case GOT_STATUS_DELETE:
1743 label1 = path;
1744 break;
1745 default:
1746 return got_error(GOT_ERR_FILE_STATUS);
1748 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1749 label1, label2, a->diff_context, a->repo, stdout);
1752 if (staged_status == GOT_STATUS_ADD ||
1753 staged_status == GOT_STATUS_MODIFY)
1754 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1755 8192);
1756 else if (status != GOT_STATUS_ADD)
1757 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1758 if (err)
1759 goto done;
1761 if (status != GOT_STATUS_DELETE) {
1762 if (asprintf(&abspath, "%s/%s",
1763 got_worktree_get_root_path(a->worktree), path) == -1) {
1764 err = got_error_from_errno("asprintf");
1765 goto done;
1768 f2 = fopen(abspath, "r");
1769 if (f2 == NULL) {
1770 err = got_error_from_errno2("fopen", abspath);
1771 goto done;
1773 if (lstat(abspath, &sb) == -1) {
1774 err = got_error_from_errno2("lstat", abspath);
1775 goto done;
1777 } else
1778 sb.st_size = 0;
1780 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1781 stdout);
1782 done:
1783 if (blob1)
1784 got_object_blob_close(blob1);
1785 if (f2 && fclose(f2) != 0 && err == NULL)
1786 err = got_error_from_errno("fclose");
1787 free(abspath);
1788 return err;
1791 static const struct got_error *
1792 cmd_diff(int argc, char *argv[])
1794 const struct got_error *error;
1795 struct got_repository *repo = NULL;
1796 struct got_worktree *worktree = NULL;
1797 char *cwd = NULL, *repo_path = NULL;
1798 struct got_object_id *id1 = NULL, *id2 = NULL;
1799 const char *id_str1 = NULL, *id_str2 = NULL;
1800 char *label1 = NULL, *label2 = NULL;
1801 int type1, type2;
1802 int diff_context = 3, diff_staged = 0, ch;
1803 const char *errstr;
1804 char *path = NULL;
1806 #ifndef PROFILE
1807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1808 NULL) == -1)
1809 err(1, "pledge");
1810 #endif
1812 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1813 switch (ch) {
1814 case 'C':
1815 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1816 if (errstr != NULL)
1817 err(1, "-C option %s", errstr);
1818 break;
1819 case 'r':
1820 repo_path = realpath(optarg, NULL);
1821 if (repo_path == NULL)
1822 err(1, "-r option");
1823 got_path_strip_trailing_slashes(repo_path);
1824 break;
1825 case 's':
1826 diff_staged = 1;
1827 break;
1828 default:
1829 usage_diff();
1830 /* NOTREACHED */
1834 argc -= optind;
1835 argv += optind;
1837 cwd = getcwd(NULL, 0);
1838 if (cwd == NULL) {
1839 error = got_error_from_errno("getcwd");
1840 goto done;
1842 error = got_worktree_open(&worktree, cwd);
1843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1844 goto done;
1845 if (argc <= 1) {
1846 if (worktree == NULL) {
1847 error = got_error(GOT_ERR_NOT_WORKTREE);
1848 goto done;
1850 if (repo_path)
1851 errx(1,
1852 "-r option can't be used when diffing a work tree");
1853 repo_path = strdup(got_worktree_get_repo_path(worktree));
1854 if (repo_path == NULL) {
1855 error = got_error_from_errno("strdup");
1856 goto done;
1858 if (argc == 1) {
1859 error = got_worktree_resolve_path(&path, worktree,
1860 argv[0]);
1861 if (error)
1862 goto done;
1863 } else {
1864 path = strdup("");
1865 if (path == NULL) {
1866 error = got_error_from_errno("strdup");
1867 goto done;
1870 } else if (argc == 2) {
1871 if (diff_staged)
1872 errx(1, "-s option can't be used when diffing "
1873 "objects in repository");
1874 id_str1 = argv[0];
1875 id_str2 = argv[1];
1876 if (worktree && repo_path == NULL) {
1877 repo_path =
1878 strdup(got_worktree_get_repo_path(worktree));
1879 if (repo_path == NULL) {
1880 error = got_error_from_errno("strdup");
1881 goto done;
1884 } else
1885 usage_diff();
1887 if (repo_path == NULL) {
1888 repo_path = getcwd(NULL, 0);
1889 if (repo_path == NULL)
1890 return got_error_from_errno("getcwd");
1893 error = got_repo_open(&repo, repo_path);
1894 free(repo_path);
1895 if (error != NULL)
1896 goto done;
1898 error = apply_unveil(got_repo_get_path(repo), 1,
1899 worktree ? got_worktree_get_root_path(worktree) : NULL);
1900 if (error)
1901 goto done;
1903 if (argc <= 1) {
1904 struct print_diff_arg arg;
1905 struct got_pathlist_head paths;
1906 char *id_str;
1908 TAILQ_INIT(&paths);
1910 error = got_object_id_str(&id_str,
1911 got_worktree_get_base_commit_id(worktree));
1912 if (error)
1913 goto done;
1914 arg.repo = repo;
1915 arg.worktree = worktree;
1916 arg.diff_context = diff_context;
1917 arg.id_str = id_str;
1918 arg.header_shown = 0;
1919 arg.diff_staged = diff_staged;
1921 error = got_pathlist_append(&paths, path, NULL);
1922 if (error)
1923 goto done;
1925 error = got_worktree_status(worktree, &paths, repo, print_diff,
1926 &arg, check_cancelled, NULL);
1927 free(id_str);
1928 got_pathlist_free(&paths);
1929 goto done;
1932 error = got_repo_match_object_id_prefix(&id1, id_str1,
1933 GOT_OBJ_TYPE_ANY, repo);
1934 if (error) {
1935 struct got_reference *ref;
1936 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1937 goto done;
1938 error = got_ref_open(&ref, repo, id_str1, 0);
1939 if (error != NULL)
1940 goto done;
1941 label1 = strdup(got_ref_get_name(ref));
1942 if (label1 == NULL) {
1943 error = got_error_from_errno("strdup");
1944 goto done;
1946 error = got_ref_resolve(&id1, repo, ref);
1947 got_ref_close(ref);
1948 if (error != NULL)
1949 goto done;
1950 } else {
1951 error = got_object_id_str(&label1, id1);
1952 if (label1 == NULL) {
1953 error = got_error_from_errno("strdup");
1954 goto done;
1958 error = got_repo_match_object_id_prefix(&id2, id_str2,
1959 GOT_OBJ_TYPE_ANY, repo);
1960 if (error) {
1961 struct got_reference *ref;
1962 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1963 goto done;
1964 error = got_ref_open(&ref, repo, id_str2, 0);
1965 if (error != NULL)
1966 goto done;
1967 label2 = strdup(got_ref_get_name(ref));
1968 if (label2 == NULL) {
1969 error = got_error_from_errno("strdup");
1970 goto done;
1972 error = got_ref_resolve(&id2, repo, ref);
1973 got_ref_close(ref);
1974 if (error != NULL)
1975 goto done;
1976 } else {
1977 error = got_object_id_str(&label2, id2);
1978 if (label2 == NULL) {
1979 error = got_error_from_errno("strdup");
1980 goto done;
1984 error = got_object_get_type(&type1, repo, id1);
1985 if (error)
1986 goto done;
1988 error = got_object_get_type(&type2, repo, id2);
1989 if (error)
1990 goto done;
1992 if (type1 != type2) {
1993 error = got_error(GOT_ERR_OBJ_TYPE);
1994 goto done;
1997 switch (type1) {
1998 case GOT_OBJ_TYPE_BLOB:
1999 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2000 diff_context, repo, stdout);
2001 break;
2002 case GOT_OBJ_TYPE_TREE:
2003 error = got_diff_objects_as_trees(id1, id2, "", "",
2004 diff_context, repo, stdout);
2005 break;
2006 case GOT_OBJ_TYPE_COMMIT:
2007 printf("diff %s %s\n", label1, label2);
2008 error = got_diff_objects_as_commits(id1, id2, diff_context,
2009 repo, stdout);
2010 break;
2011 default:
2012 error = got_error(GOT_ERR_OBJ_TYPE);
2015 done:
2016 free(label1);
2017 free(label2);
2018 free(id1);
2019 free(id2);
2020 free(path);
2021 if (worktree)
2022 got_worktree_close(worktree);
2023 if (repo) {
2024 const struct got_error *repo_error;
2025 repo_error = got_repo_close(repo);
2026 if (error == NULL)
2027 error = repo_error;
2029 return error;
2032 __dead static void
2033 usage_blame(void)
2035 fprintf(stderr,
2036 "usage: %s blame [-c commit] [-r repository-path] path\n",
2037 getprogname());
2038 exit(1);
2041 static const struct got_error *
2042 cmd_blame(int argc, char *argv[])
2044 const struct got_error *error;
2045 struct got_repository *repo = NULL;
2046 struct got_worktree *worktree = NULL;
2047 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2048 struct got_object_id *commit_id = NULL;
2049 char *commit_id_str = NULL;
2050 int ch;
2052 #ifndef PROFILE
2053 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2054 NULL) == -1)
2055 err(1, "pledge");
2056 #endif
2058 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2059 switch (ch) {
2060 case 'c':
2061 commit_id_str = optarg;
2062 break;
2063 case 'r':
2064 repo_path = realpath(optarg, NULL);
2065 if (repo_path == NULL)
2066 err(1, "-r option");
2067 got_path_strip_trailing_slashes(repo_path);
2068 break;
2069 default:
2070 usage_blame();
2071 /* NOTREACHED */
2075 argc -= optind;
2076 argv += optind;
2078 if (argc == 1)
2079 path = argv[0];
2080 else
2081 usage_blame();
2083 cwd = getcwd(NULL, 0);
2084 if (cwd == NULL) {
2085 error = got_error_from_errno("getcwd");
2086 goto done;
2088 if (repo_path == NULL) {
2089 error = got_worktree_open(&worktree, cwd);
2090 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2091 goto done;
2092 else
2093 error = NULL;
2094 if (worktree) {
2095 repo_path =
2096 strdup(got_worktree_get_repo_path(worktree));
2097 if (repo_path == NULL)
2098 error = got_error_from_errno("strdup");
2099 if (error)
2100 goto done;
2101 } else {
2102 repo_path = strdup(cwd);
2103 if (repo_path == NULL) {
2104 error = got_error_from_errno("strdup");
2105 goto done;
2110 error = got_repo_open(&repo, repo_path);
2111 if (error != NULL)
2112 goto done;
2114 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2115 if (error)
2116 goto done;
2118 if (worktree) {
2119 const char *prefix = got_worktree_get_path_prefix(worktree);
2120 char *p, *worktree_subdir = cwd +
2121 strlen(got_worktree_get_root_path(worktree));
2122 if (asprintf(&p, "%s%s%s%s%s",
2123 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2124 worktree_subdir, worktree_subdir[0] ? "/" : "",
2125 path) == -1) {
2126 error = got_error_from_errno("asprintf");
2127 goto done;
2129 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2130 free(p);
2131 } else {
2132 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2134 if (error)
2135 goto done;
2137 if (commit_id_str == NULL) {
2138 struct got_reference *head_ref;
2139 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2140 if (error != NULL)
2141 goto done;
2142 error = got_ref_resolve(&commit_id, repo, head_ref);
2143 got_ref_close(head_ref);
2144 if (error != NULL)
2145 goto done;
2146 } else {
2147 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2148 if (error)
2149 goto done;
2152 error = got_blame(in_repo_path, commit_id, repo, stdout);
2153 done:
2154 free(in_repo_path);
2155 free(repo_path);
2156 free(cwd);
2157 free(commit_id);
2158 if (worktree)
2159 got_worktree_close(worktree);
2160 if (repo) {
2161 const struct got_error *repo_error;
2162 repo_error = got_repo_close(repo);
2163 if (error == NULL)
2164 error = repo_error;
2166 return error;
2169 __dead static void
2170 usage_tree(void)
2172 fprintf(stderr,
2173 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2174 getprogname());
2175 exit(1);
2178 static void
2179 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2180 const char *root_path)
2182 int is_root_path = (strcmp(path, root_path) == 0);
2184 path += strlen(root_path);
2185 while (path[0] == '/')
2186 path++;
2188 printf("%s%s%s%s%s\n", id ? id : "", path,
2189 is_root_path ? "" : "/", te->name,
2190 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2193 static const struct got_error *
2194 print_tree(const char *path, struct got_object_id *commit_id,
2195 int show_ids, int recurse, const char *root_path,
2196 struct got_repository *repo)
2198 const struct got_error *err = NULL;
2199 struct got_object_id *tree_id = NULL;
2200 struct got_tree_object *tree = NULL;
2201 const struct got_tree_entries *entries;
2202 struct got_tree_entry *te;
2204 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2205 if (err)
2206 goto done;
2208 err = got_object_open_as_tree(&tree, repo, tree_id);
2209 if (err)
2210 goto done;
2211 entries = got_object_tree_get_entries(tree);
2212 te = SIMPLEQ_FIRST(&entries->head);
2213 while (te) {
2214 char *id = NULL;
2216 if (sigint_received || sigpipe_received)
2217 break;
2219 if (show_ids) {
2220 char *id_str;
2221 err = got_object_id_str(&id_str, te->id);
2222 if (err)
2223 goto done;
2224 if (asprintf(&id, "%s ", id_str) == -1) {
2225 err = got_error_from_errno("asprintf");
2226 free(id_str);
2227 goto done;
2229 free(id_str);
2231 print_entry(te, id, path, root_path);
2232 free(id);
2234 if (recurse && S_ISDIR(te->mode)) {
2235 char *child_path;
2236 if (asprintf(&child_path, "%s%s%s", path,
2237 path[0] == '/' && path[1] == '\0' ? "" : "/",
2238 te->name) == -1) {
2239 err = got_error_from_errno("asprintf");
2240 goto done;
2242 err = print_tree(child_path, commit_id, show_ids, 1,
2243 root_path, repo);
2244 free(child_path);
2245 if (err)
2246 goto done;
2249 te = SIMPLEQ_NEXT(te, entry);
2251 done:
2252 if (tree)
2253 got_object_tree_close(tree);
2254 free(tree_id);
2255 return err;
2258 static const struct got_error *
2259 cmd_tree(int argc, char *argv[])
2261 const struct got_error *error;
2262 struct got_repository *repo = NULL;
2263 struct got_worktree *worktree = NULL;
2264 const char *path;
2265 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2266 struct got_object_id *commit_id = NULL;
2267 char *commit_id_str = NULL;
2268 int show_ids = 0, recurse = 0;
2269 int ch;
2271 #ifndef PROFILE
2272 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2273 NULL) == -1)
2274 err(1, "pledge");
2275 #endif
2277 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2278 switch (ch) {
2279 case 'c':
2280 commit_id_str = optarg;
2281 break;
2282 case 'r':
2283 repo_path = realpath(optarg, NULL);
2284 if (repo_path == NULL)
2285 err(1, "-r option");
2286 got_path_strip_trailing_slashes(repo_path);
2287 break;
2288 case 'i':
2289 show_ids = 1;
2290 break;
2291 case 'R':
2292 recurse = 1;
2293 break;
2294 default:
2295 usage_tree();
2296 /* NOTREACHED */
2300 argc -= optind;
2301 argv += optind;
2303 if (argc == 1)
2304 path = argv[0];
2305 else if (argc > 1)
2306 usage_tree();
2307 else
2308 path = NULL;
2310 cwd = getcwd(NULL, 0);
2311 if (cwd == NULL) {
2312 error = got_error_from_errno("getcwd");
2313 goto done;
2315 if (repo_path == NULL) {
2316 error = got_worktree_open(&worktree, cwd);
2317 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2318 goto done;
2319 else
2320 error = NULL;
2321 if (worktree) {
2322 repo_path =
2323 strdup(got_worktree_get_repo_path(worktree));
2324 if (repo_path == NULL)
2325 error = got_error_from_errno("strdup");
2326 if (error)
2327 goto done;
2328 } else {
2329 repo_path = strdup(cwd);
2330 if (repo_path == NULL) {
2331 error = got_error_from_errno("strdup");
2332 goto done;
2337 error = got_repo_open(&repo, repo_path);
2338 if (error != NULL)
2339 goto done;
2341 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2342 if (error)
2343 goto done;
2345 if (path == NULL) {
2346 if (worktree) {
2347 char *p, *worktree_subdir = cwd +
2348 strlen(got_worktree_get_root_path(worktree));
2349 if (asprintf(&p, "%s/%s",
2350 got_worktree_get_path_prefix(worktree),
2351 worktree_subdir) == -1) {
2352 error = got_error_from_errno("asprintf");
2353 goto done;
2355 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2356 free(p);
2357 if (error)
2358 goto done;
2359 } else
2360 path = "/";
2362 if (in_repo_path == NULL) {
2363 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2364 if (error != NULL)
2365 goto done;
2368 if (commit_id_str == NULL) {
2369 struct got_reference *head_ref;
2370 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2371 if (error != NULL)
2372 goto done;
2373 error = got_ref_resolve(&commit_id, repo, head_ref);
2374 got_ref_close(head_ref);
2375 if (error != NULL)
2376 goto done;
2377 } else {
2378 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2379 if (error)
2380 goto done;
2383 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2384 in_repo_path, repo);
2385 done:
2386 free(in_repo_path);
2387 free(repo_path);
2388 free(cwd);
2389 free(commit_id);
2390 if (worktree)
2391 got_worktree_close(worktree);
2392 if (repo) {
2393 const struct got_error *repo_error;
2394 repo_error = got_repo_close(repo);
2395 if (error == NULL)
2396 error = repo_error;
2398 return error;
2401 __dead static void
2402 usage_status(void)
2404 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2405 exit(1);
2408 static const struct got_error *
2409 print_status(void *arg, unsigned char status, unsigned char staged_status,
2410 const char *path, struct got_object_id *blob_id,
2411 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2413 if (status == staged_status && (status == GOT_STATUS_DELETE))
2414 status = GOT_STATUS_NO_CHANGE;
2415 printf("%c%c %s\n", status, staged_status, path);
2416 return NULL;
2419 static const struct got_error *
2420 cmd_status(int argc, char *argv[])
2422 const struct got_error *error = NULL;
2423 struct got_repository *repo = NULL;
2424 struct got_worktree *worktree = NULL;
2425 char *cwd = NULL;
2426 struct got_pathlist_head paths;
2427 struct got_pathlist_entry *pe;
2428 int ch;
2430 TAILQ_INIT(&paths);
2432 while ((ch = getopt(argc, argv, "")) != -1) {
2433 switch (ch) {
2434 default:
2435 usage_status();
2436 /* NOTREACHED */
2440 argc -= optind;
2441 argv += optind;
2443 #ifndef PROFILE
2444 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2445 NULL) == -1)
2446 err(1, "pledge");
2447 #endif
2448 cwd = getcwd(NULL, 0);
2449 if (cwd == NULL) {
2450 error = got_error_from_errno("getcwd");
2451 goto done;
2454 error = got_worktree_open(&worktree, cwd);
2455 if (error != NULL)
2456 goto done;
2458 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2459 if (error)
2460 goto done;
2462 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2463 if (error != NULL)
2464 goto done;
2466 error = apply_unveil(got_repo_get_path(repo), 1,
2467 got_worktree_get_root_path(worktree));
2468 if (error)
2469 goto done;
2471 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2472 check_cancelled, NULL);
2473 done:
2474 TAILQ_FOREACH(pe, &paths, entry)
2475 free((char *)pe->path);
2476 got_pathlist_free(&paths);
2477 free(cwd);
2478 return error;
2481 __dead static void
2482 usage_ref(void)
2484 fprintf(stderr,
2485 "usage: %s ref [-r repository] -l | -d name | name target\n",
2486 getprogname());
2487 exit(1);
2490 static const struct got_error *
2491 list_refs(struct got_repository *repo)
2493 static const struct got_error *err = NULL;
2494 struct got_reflist_head refs;
2495 struct got_reflist_entry *re;
2497 SIMPLEQ_INIT(&refs);
2498 err = got_ref_list(&refs, repo);
2499 if (err)
2500 return err;
2502 SIMPLEQ_FOREACH(re, &refs, entry) {
2503 char *refstr;
2504 refstr = got_ref_to_str(re->ref);
2505 if (refstr == NULL)
2506 return got_error_from_errno("got_ref_to_str");
2507 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2508 free(refstr);
2511 got_ref_list_free(&refs);
2512 return NULL;
2515 static const struct got_error *
2516 delete_ref(struct got_repository *repo, const char *refname)
2518 const struct got_error *err = NULL;
2519 struct got_reference *ref;
2521 err = got_ref_open(&ref, repo, refname, 0);
2522 if (err)
2523 return err;
2525 err = got_ref_delete(ref, repo);
2526 got_ref_close(ref);
2527 return err;
2530 static const struct got_error *
2531 add_ref(struct got_repository *repo, const char *refname, const char *target)
2533 const struct got_error *err = NULL;
2534 struct got_object_id *id;
2535 struct got_reference *ref = NULL;
2538 * Don't let the user create a reference named '-'.
2539 * While technically a valid reference name, this case is usually
2540 * an unintended typo.
2542 if (refname[0] == '-' && refname[1] == '\0')
2543 return got_error(GOT_ERR_BAD_REF_NAME);
2545 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2546 repo);
2547 if (err) {
2548 struct got_reference *target_ref;
2550 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2551 return err;
2552 err = got_ref_open(&target_ref, repo, target, 0);
2553 if (err)
2554 return err;
2555 err = got_ref_resolve(&id, repo, target_ref);
2556 got_ref_close(target_ref);
2557 if (err)
2558 return err;
2561 err = got_ref_alloc(&ref, refname, id);
2562 if (err)
2563 goto done;
2565 err = got_ref_write(ref, repo);
2566 done:
2567 if (ref)
2568 got_ref_close(ref);
2569 free(id);
2570 return err;
2573 static const struct got_error *
2574 cmd_ref(int argc, char *argv[])
2576 const struct got_error *error = NULL;
2577 struct got_repository *repo = NULL;
2578 struct got_worktree *worktree = NULL;
2579 char *cwd = NULL, *repo_path = NULL;
2580 int ch, do_list = 0;
2581 const char *delref = NULL;
2583 /* TODO: Add -s option for adding symbolic references. */
2584 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2585 switch (ch) {
2586 case 'd':
2587 delref = optarg;
2588 break;
2589 case 'r':
2590 repo_path = realpath(optarg, NULL);
2591 if (repo_path == NULL)
2592 err(1, "-r option");
2593 got_path_strip_trailing_slashes(repo_path);
2594 break;
2595 case 'l':
2596 do_list = 1;
2597 break;
2598 default:
2599 usage_ref();
2600 /* NOTREACHED */
2604 if (do_list && delref)
2605 errx(1, "-l and -d options are mutually exclusive\n");
2607 argc -= optind;
2608 argv += optind;
2610 if (do_list || delref) {
2611 if (argc > 0)
2612 usage_ref();
2613 } else if (argc != 2)
2614 usage_ref();
2616 #ifndef PROFILE
2617 if (do_list) {
2618 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2619 NULL) == -1)
2620 err(1, "pledge");
2621 } else {
2622 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2623 "sendfd unveil", NULL) == -1)
2624 err(1, "pledge");
2626 #endif
2627 cwd = getcwd(NULL, 0);
2628 if (cwd == NULL) {
2629 error = got_error_from_errno("getcwd");
2630 goto done;
2633 if (repo_path == NULL) {
2634 error = got_worktree_open(&worktree, cwd);
2635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2636 goto done;
2637 else
2638 error = NULL;
2639 if (worktree) {
2640 repo_path =
2641 strdup(got_worktree_get_repo_path(worktree));
2642 if (repo_path == NULL)
2643 error = got_error_from_errno("strdup");
2644 if (error)
2645 goto done;
2646 } else {
2647 repo_path = strdup(cwd);
2648 if (repo_path == NULL) {
2649 error = got_error_from_errno("strdup");
2650 goto done;
2655 error = got_repo_open(&repo, repo_path);
2656 if (error != NULL)
2657 goto done;
2659 error = apply_unveil(got_repo_get_path(repo), do_list,
2660 worktree ? got_worktree_get_root_path(worktree) : NULL);
2661 if (error)
2662 goto done;
2664 if (do_list)
2665 error = list_refs(repo);
2666 else if (delref)
2667 error = delete_ref(repo, delref);
2668 else
2669 error = add_ref(repo, argv[0], argv[1]);
2670 done:
2671 if (repo)
2672 got_repo_close(repo);
2673 if (worktree)
2674 got_worktree_close(worktree);
2675 free(cwd);
2676 free(repo_path);
2677 return error;
2680 __dead static void
2681 usage_branch(void)
2683 fprintf(stderr,
2684 "usage: %s branch [-r repository] -l | -d name | "
2685 "name [base-branch]\n", getprogname());
2686 exit(1);
2689 static const struct got_error *
2690 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2692 static const struct got_error *err = NULL;
2693 struct got_reflist_head refs;
2694 struct got_reflist_entry *re;
2696 SIMPLEQ_INIT(&refs);
2698 err = got_ref_list(&refs, repo);
2699 if (err)
2700 return err;
2702 SIMPLEQ_FOREACH(re, &refs, entry) {
2703 const char *refname, *marker = " ";
2704 char *refstr;
2705 refname = got_ref_get_name(re->ref);
2706 if (strncmp(refname, "refs/heads/", 11) != 0)
2707 continue;
2708 if (worktree && strcmp(refname,
2709 got_worktree_get_head_ref_name(worktree)) == 0) {
2710 struct got_object_id *id = NULL;
2711 err = got_ref_resolve(&id, repo, re->ref);
2712 if (err)
2713 return err;
2714 if (got_object_id_cmp(id,
2715 got_worktree_get_base_commit_id(worktree)) == 0)
2716 marker = "* ";
2717 else
2718 marker = "~ ";
2719 free(id);
2721 refname += 11;
2722 refstr = got_ref_to_str(re->ref);
2723 if (refstr == NULL)
2724 return got_error_from_errno("got_ref_to_str");
2725 printf("%s%s: %s\n", marker, refname, refstr);
2726 free(refstr);
2729 got_ref_list_free(&refs);
2730 return NULL;
2733 static const struct got_error *
2734 delete_branch(struct got_repository *repo, const char *branch_name)
2736 const struct got_error *err = NULL;
2737 struct got_reference *ref;
2738 char *refname;
2740 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2741 return got_error_from_errno("asprintf");
2743 err = got_ref_open(&ref, repo, refname, 0);
2744 if (err)
2745 goto done;
2747 err = got_ref_delete(ref, repo);
2748 got_ref_close(ref);
2749 done:
2750 free(refname);
2751 return err;
2754 static const struct got_error *
2755 add_branch(struct got_repository *repo, const char *branch_name,
2756 const char *base_branch)
2758 const struct got_error *err = NULL;
2759 struct got_object_id *id = NULL;
2760 struct got_reference *ref = NULL;
2761 char *base_refname = NULL, *refname = NULL;
2762 struct got_reference *base_ref;
2765 * Don't let the user create a branch named '-'.
2766 * While technically a valid reference name, this case is usually
2767 * an unintended typo.
2769 if (branch_name[0] == '-' && branch_name[1] == '\0')
2770 return got_error(GOT_ERR_BAD_REF_NAME);
2772 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2773 base_refname = strdup(GOT_REF_HEAD);
2774 if (base_refname == NULL)
2775 return got_error_from_errno("strdup");
2776 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2777 return got_error_from_errno("asprintf");
2779 err = got_ref_open(&base_ref, repo, base_refname, 0);
2780 if (err)
2781 goto done;
2782 err = got_ref_resolve(&id, repo, base_ref);
2783 got_ref_close(base_ref);
2784 if (err)
2785 goto done;
2787 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2788 err = got_error_from_errno("asprintf");
2789 goto done;
2792 err = got_ref_open(&ref, repo, refname, 0);
2793 if (err == NULL) {
2794 err = got_error(GOT_ERR_BRANCH_EXISTS);
2795 goto done;
2796 } else if (err->code != GOT_ERR_NOT_REF)
2797 goto done;
2799 err = got_ref_alloc(&ref, refname, id);
2800 if (err)
2801 goto done;
2803 err = got_ref_write(ref, repo);
2804 done:
2805 if (ref)
2806 got_ref_close(ref);
2807 free(id);
2808 free(base_refname);
2809 free(refname);
2810 return err;
2813 static const struct got_error *
2814 cmd_branch(int argc, char *argv[])
2816 const struct got_error *error = NULL;
2817 struct got_repository *repo = NULL;
2818 struct got_worktree *worktree = NULL;
2819 char *cwd = NULL, *repo_path = NULL;
2820 int ch, do_list = 0;
2821 const char *delref = NULL;
2823 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2824 switch (ch) {
2825 case 'd':
2826 delref = optarg;
2827 break;
2828 case 'r':
2829 repo_path = realpath(optarg, NULL);
2830 if (repo_path == NULL)
2831 err(1, "-r option");
2832 got_path_strip_trailing_slashes(repo_path);
2833 break;
2834 case 'l':
2835 do_list = 1;
2836 break;
2837 default:
2838 usage_branch();
2839 /* NOTREACHED */
2843 if (do_list && delref)
2844 errx(1, "-l and -d options are mutually exclusive\n");
2846 argc -= optind;
2847 argv += optind;
2849 if (do_list || delref) {
2850 if (argc > 0)
2851 usage_branch();
2852 } else if (argc < 1 || argc > 2)
2853 usage_branch();
2855 #ifndef PROFILE
2856 if (do_list) {
2857 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2858 NULL) == -1)
2859 err(1, "pledge");
2860 } else {
2861 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2862 "sendfd unveil", NULL) == -1)
2863 err(1, "pledge");
2865 #endif
2866 cwd = getcwd(NULL, 0);
2867 if (cwd == NULL) {
2868 error = got_error_from_errno("getcwd");
2869 goto done;
2872 if (repo_path == NULL) {
2873 error = got_worktree_open(&worktree, cwd);
2874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2875 goto done;
2876 else
2877 error = NULL;
2878 if (worktree) {
2879 repo_path =
2880 strdup(got_worktree_get_repo_path(worktree));
2881 if (repo_path == NULL)
2882 error = got_error_from_errno("strdup");
2883 if (error)
2884 goto done;
2885 } else {
2886 repo_path = strdup(cwd);
2887 if (repo_path == NULL) {
2888 error = got_error_from_errno("strdup");
2889 goto done;
2894 error = got_repo_open(&repo, repo_path);
2895 if (error != NULL)
2896 goto done;
2898 error = apply_unveil(got_repo_get_path(repo), do_list,
2899 worktree ? got_worktree_get_root_path(worktree) : NULL);
2900 if (error)
2901 goto done;
2903 if (do_list)
2904 error = list_branches(repo, worktree);
2905 else if (delref)
2906 error = delete_branch(repo, delref);
2907 else {
2908 const char *base_branch;
2909 if (argc == 1) {
2910 base_branch = worktree ?
2911 got_worktree_get_head_ref_name(worktree) :
2912 GOT_REF_HEAD;
2913 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2914 base_branch += 11;
2915 } else
2916 base_branch = argv[1];
2917 error = add_branch(repo, argv[0], base_branch);
2919 done:
2920 if (repo)
2921 got_repo_close(repo);
2922 if (worktree)
2923 got_worktree_close(worktree);
2924 free(cwd);
2925 free(repo_path);
2926 return error;
2929 __dead static void
2930 usage_add(void)
2932 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2933 exit(1);
2936 static const struct got_error *
2937 cmd_add(int argc, char *argv[])
2939 const struct got_error *error = NULL;
2940 struct got_repository *repo = NULL;
2941 struct got_worktree *worktree = NULL;
2942 char *cwd = NULL;
2943 struct got_pathlist_head paths;
2944 struct got_pathlist_entry *pe;
2945 int ch, x;
2947 TAILQ_INIT(&paths);
2949 while ((ch = getopt(argc, argv, "")) != -1) {
2950 switch (ch) {
2951 default:
2952 usage_add();
2953 /* NOTREACHED */
2957 argc -= optind;
2958 argv += optind;
2960 #ifndef PROFILE
2961 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2962 NULL) == -1)
2963 err(1, "pledge");
2964 #endif
2965 if (argc < 1)
2966 usage_add();
2968 cwd = getcwd(NULL, 0);
2969 if (cwd == NULL) {
2970 error = got_error_from_errno("getcwd");
2971 goto done;
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error)
2976 goto done;
2978 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2979 if (error != NULL)
2980 goto done;
2982 error = apply_unveil(got_repo_get_path(repo), 1,
2983 got_worktree_get_root_path(worktree));
2984 if (error)
2985 goto done;
2987 for (x = 0; x < argc; x++) {
2988 char *path = realpath(argv[x], NULL);
2989 if (path == NULL) {
2990 error = got_error_from_errno2("realpath", argv[x]);
2991 goto done;
2994 got_path_strip_trailing_slashes(path);
2995 error = got_pathlist_insert(&pe, &paths, path, NULL);
2996 if (error) {
2997 free(path);
2998 goto done;
3001 error = got_worktree_schedule_add(worktree, &paths, print_status,
3002 NULL, repo);
3003 done:
3004 if (repo)
3005 got_repo_close(repo);
3006 if (worktree)
3007 got_worktree_close(worktree);
3008 TAILQ_FOREACH(pe, &paths, entry)
3009 free((char *)pe->path);
3010 got_pathlist_free(&paths);
3011 free(cwd);
3012 return error;
3015 __dead static void
3016 usage_remove(void)
3018 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3019 exit(1);
3022 static const struct got_error *
3023 cmd_remove(int argc, char *argv[])
3025 const struct got_error *error = NULL;
3026 struct got_worktree *worktree = NULL;
3027 struct got_repository *repo = NULL;
3028 char *cwd = NULL;
3029 struct got_pathlist_head paths;
3030 struct got_pathlist_entry *pe;
3031 int ch, i, delete_local_mods = 0;
3033 TAILQ_INIT(&paths);
3035 while ((ch = getopt(argc, argv, "f")) != -1) {
3036 switch (ch) {
3037 case 'f':
3038 delete_local_mods = 1;
3039 break;
3040 default:
3041 usage_add();
3042 /* NOTREACHED */
3046 argc -= optind;
3047 argv += optind;
3049 #ifndef PROFILE
3050 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3051 NULL) == -1)
3052 err(1, "pledge");
3053 #endif
3054 if (argc < 1)
3055 usage_remove();
3057 cwd = getcwd(NULL, 0);
3058 if (cwd == NULL) {
3059 error = got_error_from_errno("getcwd");
3060 goto done;
3062 error = got_worktree_open(&worktree, cwd);
3063 if (error)
3064 goto done;
3066 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3067 if (error)
3068 goto done;
3070 error = apply_unveil(got_repo_get_path(repo), 1,
3071 got_worktree_get_root_path(worktree));
3072 if (error)
3073 goto done;
3075 for (i = 0; i < argc; i++) {
3076 char *path = realpath(argv[i], NULL);
3077 if (path == NULL) {
3078 error = got_error_from_errno2("realpath", argv[i]);
3079 goto done;
3082 got_path_strip_trailing_slashes(path);
3083 error = got_pathlist_insert(&pe, &paths, path, NULL);
3084 if (error) {
3085 free(path);
3086 goto done;
3089 error = got_worktree_schedule_delete(worktree, &paths,
3090 delete_local_mods, print_status, NULL, repo);
3091 if (error)
3092 goto done;
3093 done:
3094 if (repo)
3095 got_repo_close(repo);
3096 if (worktree)
3097 got_worktree_close(worktree);
3098 TAILQ_FOREACH(pe, &paths, entry)
3099 free((char *)pe->path);
3100 got_pathlist_free(&paths);
3101 free(cwd);
3102 return error;
3105 __dead static void
3106 usage_revert(void)
3108 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3109 exit(1);
3112 static const struct got_error *
3113 revert_progress(void *arg, unsigned char status, const char *path)
3115 while (path[0] == '/')
3116 path++;
3117 printf("%c %s\n", status, path);
3118 return NULL;
3121 static const struct got_error *
3122 cmd_revert(int argc, char *argv[])
3124 const struct got_error *error = NULL;
3125 struct got_worktree *worktree = NULL;
3126 struct got_repository *repo = NULL;
3127 char *cwd = NULL, *path = NULL;
3128 struct got_pathlist_head paths;
3129 struct got_pathlist_entry *pe;
3130 const char *worktree_path;
3131 int ch, i;
3133 TAILQ_INIT(&paths);
3135 while ((ch = getopt(argc, argv, "")) != -1) {
3136 switch (ch) {
3137 default:
3138 usage_revert();
3139 /* NOTREACHED */
3143 argc -= optind;
3144 argv += optind;
3146 #ifndef PROFILE
3147 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3148 "unveil", NULL) == -1)
3149 err(1, "pledge");
3150 #endif
3151 if (argc < 1)
3152 usage_revert();
3154 cwd = getcwd(NULL, 0);
3155 if (cwd == NULL) {
3156 error = got_error_from_errno("getcwd");
3157 goto done;
3159 error = got_worktree_open(&worktree, cwd);
3160 if (error)
3161 goto done;
3163 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3164 if (error != NULL)
3165 goto done;
3167 error = apply_unveil(got_repo_get_path(repo), 1,
3168 got_worktree_get_root_path(worktree));
3169 if (error)
3170 goto done;
3172 worktree_path = got_worktree_get_root_path(worktree);
3173 for (i = 0; i < argc; i++) {
3174 char *path = realpath(argv[i], NULL);
3175 if (path == NULL) {
3176 if (errno != ENOENT) {
3177 error = got_error_from_errno2("realpath",
3178 argv[i]);
3179 goto done;
3181 if (got_path_is_child(argv[i], worktree_path,
3182 strlen(worktree_path))) {
3183 path = strdup(argv[i]);
3184 if (path == NULL) {
3185 error = got_error_from_errno("strdup");
3186 goto done;
3189 } else if (asprintf(&path, "%s/%s",
3190 got_worktree_get_root_path(worktree),
3191 argv[i]) == -1) {
3192 error = got_error_from_errno("asprintf");
3193 goto done;
3197 got_path_strip_trailing_slashes(path);
3198 error = got_pathlist_insert(&pe, &paths, path, NULL);
3199 if (error) {
3200 free(path);
3201 goto done;
3205 error = got_worktree_revert(worktree, &paths,
3206 revert_progress, NULL, repo);
3207 if (error)
3208 goto done;
3209 done:
3210 if (repo)
3211 got_repo_close(repo);
3212 if (worktree)
3213 got_worktree_close(worktree);
3214 free(path);
3215 free(cwd);
3216 return error;
3219 __dead static void
3220 usage_commit(void)
3222 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3223 getprogname());
3224 exit(1);
3227 struct collect_commit_logmsg_arg {
3228 const char *cmdline_log;
3229 const char *editor;
3230 const char *worktree_path;
3231 const char *branch_name;
3232 const char *repo_path;
3233 char *logmsg_path;
3237 static const struct got_error *
3238 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3239 void *arg)
3241 char *initial_content = NULL;
3242 struct got_pathlist_entry *pe;
3243 const struct got_error *err = NULL;
3244 char *template = NULL;
3245 struct collect_commit_logmsg_arg *a = arg;
3246 int fd;
3247 size_t len;
3249 /* if a message was specified on the command line, just use it */
3250 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3251 len = strlen(a->cmdline_log) + 1;
3252 *logmsg = malloc(len + 1);
3253 if (*logmsg == NULL)
3254 return got_error_from_errno("malloc");
3255 strlcpy(*logmsg, a->cmdline_log, len);
3256 return NULL;
3259 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3260 return got_error_from_errno("asprintf");
3262 if (asprintf(&initial_content,
3263 "\n# changes to be committed on branch %s:\n",
3264 a->branch_name) == -1)
3265 return got_error_from_errno("asprintf");
3267 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3268 if (err)
3269 goto done;
3271 dprintf(fd, initial_content);
3273 TAILQ_FOREACH(pe, commitable_paths, entry) {
3274 struct got_commitable *ct = pe->data;
3275 dprintf(fd, "# %c %s\n",
3276 got_commitable_get_status(ct),
3277 got_commitable_get_path(ct));
3279 close(fd);
3281 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3282 done:
3283 unlink(a->logmsg_path);
3284 free(a->logmsg_path);
3285 free(initial_content);
3286 free(template);
3288 /* Editor is done; we can now apply unveil(2) */
3289 if (err == NULL) {
3290 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3291 if (err) {
3292 free(*logmsg);
3293 *logmsg = NULL;
3296 return err;
3299 static const struct got_error *
3300 cmd_commit(int argc, char *argv[])
3302 const struct got_error *error = NULL;
3303 struct got_worktree *worktree = NULL;
3304 struct got_repository *repo = NULL;
3305 char *cwd = NULL, *id_str = NULL;
3306 struct got_object_id *id = NULL;
3307 const char *logmsg = NULL;
3308 const char *got_author = getenv("GOT_AUTHOR");
3309 struct collect_commit_logmsg_arg cl_arg;
3310 char *editor = NULL;
3311 int ch, rebase_in_progress, histedit_in_progress;
3312 struct got_pathlist_head paths;
3314 TAILQ_INIT(&paths);
3316 while ((ch = getopt(argc, argv, "m:")) != -1) {
3317 switch (ch) {
3318 case 'm':
3319 logmsg = optarg;
3320 break;
3321 default:
3322 usage_commit();
3323 /* NOTREACHED */
3327 argc -= optind;
3328 argv += optind;
3330 #ifndef PROFILE
3331 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3332 "unveil", NULL) == -1)
3333 err(1, "pledge");
3334 #endif
3335 if (got_author == NULL) {
3336 /* TODO: Look current user up in password database */
3337 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3338 goto done;
3341 cwd = getcwd(NULL, 0);
3342 if (cwd == NULL) {
3343 error = got_error_from_errno("getcwd");
3344 goto done;
3346 error = got_worktree_open(&worktree, cwd);
3347 if (error)
3348 goto done;
3350 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3351 if (error)
3352 goto done;
3353 if (rebase_in_progress) {
3354 error = got_error(GOT_ERR_REBASING);
3355 goto done;
3358 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3359 worktree);
3360 if (error)
3361 goto done;
3363 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3364 if (error)
3365 goto done;
3367 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3368 if (error != NULL)
3369 goto done;
3372 * unveil(2) traverses exec(2); if an editor is used we have
3373 * to apply unveil after the log message has been written.
3375 if (logmsg == NULL || strlen(logmsg) == 0)
3376 error = get_editor(&editor);
3377 else
3378 error = apply_unveil(got_repo_get_path(repo), 0,
3379 got_worktree_get_root_path(worktree));
3380 if (error)
3381 goto done;
3383 cl_arg.editor = editor;
3384 cl_arg.cmdline_log = logmsg;
3385 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3386 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3387 if (!histedit_in_progress) {
3388 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3389 error = got_error(GOT_ERR_COMMIT_BRANCH);
3390 goto done;
3392 cl_arg.branch_name += 11;
3394 cl_arg.repo_path = got_repo_get_path(repo);
3395 cl_arg.logmsg_path = NULL;
3396 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3397 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3398 if (error) {
3399 if (cl_arg.logmsg_path)
3400 fprintf(stderr, "%s: log message preserved in %s\n",
3401 getprogname(), cl_arg.logmsg_path);
3402 goto done;
3405 if (cl_arg.logmsg_path)
3406 unlink(cl_arg.logmsg_path);
3408 error = got_object_id_str(&id_str, id);
3409 if (error)
3410 goto done;
3411 printf("Created commit %s\n", id_str);
3412 done:
3413 if (repo)
3414 got_repo_close(repo);
3415 if (worktree)
3416 got_worktree_close(worktree);
3417 free(cwd);
3418 free(id_str);
3419 free(editor);
3420 return error;
3423 __dead static void
3424 usage_cherrypick(void)
3426 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3427 exit(1);
3430 static const struct got_error *
3431 cmd_cherrypick(int argc, char *argv[])
3433 const struct got_error *error = NULL;
3434 struct got_worktree *worktree = NULL;
3435 struct got_repository *repo = NULL;
3436 char *cwd = NULL, *commit_id_str = NULL;
3437 struct got_object_id *commit_id = NULL;
3438 struct got_commit_object *commit = NULL;
3439 struct got_object_qid *pid;
3440 struct got_reference *head_ref = NULL;
3441 int ch, did_something = 0;
3443 while ((ch = getopt(argc, argv, "")) != -1) {
3444 switch (ch) {
3445 default:
3446 usage_cherrypick();
3447 /* NOTREACHED */
3451 argc -= optind;
3452 argv += optind;
3454 #ifndef PROFILE
3455 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3456 "unveil", NULL) == -1)
3457 err(1, "pledge");
3458 #endif
3459 if (argc != 1)
3460 usage_cherrypick();
3462 cwd = getcwd(NULL, 0);
3463 if (cwd == NULL) {
3464 error = got_error_from_errno("getcwd");
3465 goto done;
3467 error = got_worktree_open(&worktree, cwd);
3468 if (error)
3469 goto done;
3471 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3472 if (error != NULL)
3473 goto done;
3475 error = apply_unveil(got_repo_get_path(repo), 0,
3476 got_worktree_get_root_path(worktree));
3477 if (error)
3478 goto done;
3480 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3481 GOT_OBJ_TYPE_COMMIT, repo);
3482 if (error != NULL) {
3483 struct got_reference *ref;
3484 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3485 goto done;
3486 error = got_ref_open(&ref, repo, argv[0], 0);
3487 if (error != NULL)
3488 goto done;
3489 error = got_ref_resolve(&commit_id, repo, ref);
3490 got_ref_close(ref);
3491 if (error != NULL)
3492 goto done;
3494 error = got_object_id_str(&commit_id_str, commit_id);
3495 if (error)
3496 goto done;
3498 error = got_ref_open(&head_ref, repo,
3499 got_worktree_get_head_ref_name(worktree), 0);
3500 if (error != NULL)
3501 goto done;
3503 error = check_same_branch(commit_id, head_ref, NULL, repo);
3504 if (error) {
3505 if (error->code != GOT_ERR_ANCESTRY)
3506 goto done;
3507 error = NULL;
3508 } else {
3509 error = got_error(GOT_ERR_SAME_BRANCH);
3510 goto done;
3513 error = got_object_open_as_commit(&commit, repo, commit_id);
3514 if (error)
3515 goto done;
3516 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3517 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3518 commit_id, repo, update_progress, &did_something, check_cancelled,
3519 NULL);
3520 if (error != NULL)
3521 goto done;
3523 if (did_something)
3524 printf("Merged commit %s\n", commit_id_str);
3525 done:
3526 if (commit)
3527 got_object_commit_close(commit);
3528 free(commit_id_str);
3529 if (head_ref)
3530 got_ref_close(head_ref);
3531 if (worktree)
3532 got_worktree_close(worktree);
3533 if (repo)
3534 got_repo_close(repo);
3535 return error;
3538 __dead static void
3539 usage_backout(void)
3541 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3542 exit(1);
3545 static const struct got_error *
3546 cmd_backout(int argc, char *argv[])
3548 const struct got_error *error = NULL;
3549 struct got_worktree *worktree = NULL;
3550 struct got_repository *repo = NULL;
3551 char *cwd = NULL, *commit_id_str = NULL;
3552 struct got_object_id *commit_id = NULL;
3553 struct got_commit_object *commit = NULL;
3554 struct got_object_qid *pid;
3555 struct got_reference *head_ref = NULL;
3556 int ch, did_something = 0;
3558 while ((ch = getopt(argc, argv, "")) != -1) {
3559 switch (ch) {
3560 default:
3561 usage_backout();
3562 /* NOTREACHED */
3566 argc -= optind;
3567 argv += optind;
3569 #ifndef PROFILE
3570 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3571 "unveil", NULL) == -1)
3572 err(1, "pledge");
3573 #endif
3574 if (argc != 1)
3575 usage_backout();
3577 cwd = getcwd(NULL, 0);
3578 if (cwd == NULL) {
3579 error = got_error_from_errno("getcwd");
3580 goto done;
3582 error = got_worktree_open(&worktree, cwd);
3583 if (error)
3584 goto done;
3586 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3587 if (error != NULL)
3588 goto done;
3590 error = apply_unveil(got_repo_get_path(repo), 0,
3591 got_worktree_get_root_path(worktree));
3592 if (error)
3593 goto done;
3595 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3596 GOT_OBJ_TYPE_COMMIT, repo);
3597 if (error != NULL) {
3598 struct got_reference *ref;
3599 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3600 goto done;
3601 error = got_ref_open(&ref, repo, argv[0], 0);
3602 if (error != NULL)
3603 goto done;
3604 error = got_ref_resolve(&commit_id, repo, ref);
3605 got_ref_close(ref);
3606 if (error != NULL)
3607 goto done;
3609 error = got_object_id_str(&commit_id_str, commit_id);
3610 if (error)
3611 goto done;
3613 error = got_ref_open(&head_ref, repo,
3614 got_worktree_get_head_ref_name(worktree), 0);
3615 if (error != NULL)
3616 goto done;
3618 error = check_same_branch(commit_id, head_ref, NULL, repo);
3619 if (error)
3620 goto done;
3622 error = got_object_open_as_commit(&commit, repo, commit_id);
3623 if (error)
3624 goto done;
3625 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3626 if (pid == NULL) {
3627 error = got_error(GOT_ERR_ROOT_COMMIT);
3628 goto done;
3631 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3632 update_progress, &did_something, check_cancelled, NULL);
3633 if (error != NULL)
3634 goto done;
3636 if (did_something)
3637 printf("Backed out commit %s\n", commit_id_str);
3638 done:
3639 if (commit)
3640 got_object_commit_close(commit);
3641 free(commit_id_str);
3642 if (head_ref)
3643 got_ref_close(head_ref);
3644 if (worktree)
3645 got_worktree_close(worktree);
3646 if (repo)
3647 got_repo_close(repo);
3648 return error;
3651 __dead static void
3652 usage_rebase(void)
3654 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3655 getprogname());
3656 exit(1);
3659 void
3660 trim_logmsg(char *logmsg, int limit)
3662 char *nl;
3663 size_t len;
3665 len = strlen(logmsg);
3666 if (len > limit)
3667 len = limit;
3668 logmsg[len] = '\0';
3669 nl = strchr(logmsg, '\n');
3670 if (nl)
3671 *nl = '\0';
3674 static const struct got_error *
3675 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3677 const char *logmsg0 = NULL;
3679 logmsg0 = got_object_commit_get_logmsg(commit);
3681 while (isspace((unsigned char)logmsg0[0]))
3682 logmsg0++;
3684 *logmsg = strdup(logmsg0);
3685 if (*logmsg == NULL)
3686 return got_error_from_errno("strdup");
3688 trim_logmsg(*logmsg, limit);
3689 return NULL;
3692 static const struct got_error *
3693 show_rebase_progress(struct got_commit_object *commit,
3694 struct got_object_id *old_id, struct got_object_id *new_id)
3696 const struct got_error *err;
3697 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3699 err = got_object_id_str(&old_id_str, old_id);
3700 if (err)
3701 goto done;
3703 if (new_id) {
3704 err = got_object_id_str(&new_id_str, new_id);
3705 if (err)
3706 goto done;
3709 old_id_str[12] = '\0';
3710 if (new_id_str)
3711 new_id_str[12] = '\0';
3713 err = get_short_logmsg(&logmsg, 42, commit);
3714 if (err)
3715 goto done;
3717 printf("%s -> %s: %s\n", old_id_str,
3718 new_id_str ? new_id_str : "no-op change", logmsg);
3719 done:
3720 free(old_id_str);
3721 free(new_id_str);
3722 return err;
3725 static const struct got_error *
3726 rebase_progress(void *arg, unsigned char status, const char *path)
3728 unsigned char *rebase_status = arg;
3730 while (path[0] == '/')
3731 path++;
3732 printf("%c %s\n", status, path);
3734 if (*rebase_status == GOT_STATUS_CONFLICT)
3735 return NULL;
3736 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3737 *rebase_status = status;
3738 return NULL;
3741 static const struct got_error *
3742 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3743 struct got_reference *branch, struct got_reference *new_base_branch,
3744 struct got_reference *tmp_branch, struct got_repository *repo)
3746 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3747 return got_worktree_rebase_complete(worktree, fileindex,
3748 new_base_branch, tmp_branch, branch, repo);
3751 static const struct got_error *
3752 rebase_commit(struct got_pathlist_head *merged_paths,
3753 struct got_worktree *worktree, struct got_fileindex *fileindex,
3754 struct got_reference *tmp_branch,
3755 struct got_object_id *commit_id, struct got_repository *repo)
3757 const struct got_error *error;
3758 struct got_commit_object *commit;
3759 struct got_object_id *new_commit_id;
3761 error = got_object_open_as_commit(&commit, repo, commit_id);
3762 if (error)
3763 return error;
3765 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3766 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3767 if (error) {
3768 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3769 goto done;
3770 error = show_rebase_progress(commit, commit_id, NULL);
3771 } else {
3772 error = show_rebase_progress(commit, commit_id, new_commit_id);
3773 free(new_commit_id);
3775 done:
3776 got_object_commit_close(commit);
3777 return error;
3780 struct check_path_prefix_arg {
3781 const char *path_prefix;
3782 size_t len;
3783 int errcode;
3786 static const struct got_error *
3787 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3788 struct got_blob_object *blob2, struct got_object_id *id1,
3789 struct got_object_id *id2, const char *path1, const char *path2,
3790 struct got_repository *repo)
3792 struct check_path_prefix_arg *a = arg;
3794 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3795 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3796 return got_error(a->errcode);
3798 return NULL;
3801 static const struct got_error *
3802 check_path_prefix(struct got_object_id *parent_id,
3803 struct got_object_id *commit_id, const char *path_prefix,
3804 int errcode, struct got_repository *repo)
3806 const struct got_error *err;
3807 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3808 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3809 struct check_path_prefix_arg cpp_arg;
3811 if (got_path_is_root_dir(path_prefix))
3812 return NULL;
3814 err = got_object_open_as_commit(&commit, repo, commit_id);
3815 if (err)
3816 goto done;
3818 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3819 if (err)
3820 goto done;
3822 err = got_object_open_as_tree(&tree1, repo,
3823 got_object_commit_get_tree_id(parent_commit));
3824 if (err)
3825 goto done;
3827 err = got_object_open_as_tree(&tree2, repo,
3828 got_object_commit_get_tree_id(commit));
3829 if (err)
3830 goto done;
3832 cpp_arg.path_prefix = path_prefix;
3833 while (cpp_arg.path_prefix[0] == '/')
3834 cpp_arg.path_prefix++;
3835 cpp_arg.len = strlen(cpp_arg.path_prefix);
3836 cpp_arg.errcode = errcode;
3837 err = got_diff_tree(tree1, tree2, "", "", repo,
3838 check_path_prefix_in_diff, &cpp_arg, 0);
3839 done:
3840 if (tree1)
3841 got_object_tree_close(tree1);
3842 if (tree2)
3843 got_object_tree_close(tree2);
3844 if (commit)
3845 got_object_commit_close(commit);
3846 if (parent_commit)
3847 got_object_commit_close(parent_commit);
3848 return err;
3851 static const struct got_error *
3852 collect_commits(struct got_object_id_queue *commits,
3853 struct got_object_id *initial_commit_id,
3854 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3855 const char *path_prefix, int path_prefix_errcode,
3856 struct got_repository *repo)
3858 const struct got_error *err = NULL;
3859 struct got_commit_graph *graph = NULL;
3860 struct got_object_id *parent_id = NULL;
3861 struct got_object_qid *qid;
3862 struct got_object_id *commit_id = initial_commit_id;
3864 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3865 if (err)
3866 return err;
3868 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3869 if (err)
3870 goto done;
3871 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3872 err = got_commit_graph_iter_next(&parent_id, graph);
3873 if (err) {
3874 if (err->code == GOT_ERR_ITER_COMPLETED) {
3875 err = got_error_msg(GOT_ERR_ANCESTRY,
3876 "ran out of commits to rebase before "
3877 "youngest common ancestor commit has "
3878 "been reached?!?");
3879 goto done;
3880 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3881 goto done;
3882 err = got_commit_graph_fetch_commits(graph, 1, repo);
3883 if (err)
3884 goto done;
3885 } else {
3886 err = check_path_prefix(parent_id, commit_id,
3887 path_prefix, path_prefix_errcode, repo);
3888 if (err)
3889 goto done;
3891 err = got_object_qid_alloc(&qid, commit_id);
3892 if (err)
3893 goto done;
3894 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3895 commit_id = parent_id;
3898 done:
3899 got_commit_graph_close(graph);
3900 return err;
3903 static const struct got_error *
3904 cmd_rebase(int argc, char *argv[])
3906 const struct got_error *error = NULL;
3907 struct got_worktree *worktree = NULL;
3908 struct got_repository *repo = NULL;
3909 struct got_fileindex *fileindex = NULL;
3910 char *cwd = NULL;
3911 struct got_reference *branch = NULL;
3912 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3913 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3914 struct got_object_id *resume_commit_id = NULL;
3915 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3916 struct got_commit_object *commit = NULL;
3917 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3918 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3919 struct got_object_id_queue commits;
3920 struct got_pathlist_head merged_paths;
3921 const struct got_object_id_queue *parent_ids;
3922 struct got_object_qid *qid, *pid;
3924 SIMPLEQ_INIT(&commits);
3925 TAILQ_INIT(&merged_paths);
3927 while ((ch = getopt(argc, argv, "ac")) != -1) {
3928 switch (ch) {
3929 case 'a':
3930 abort_rebase = 1;
3931 break;
3932 case 'c':
3933 continue_rebase = 1;
3934 break;
3935 default:
3936 usage_rebase();
3937 /* NOTREACHED */
3941 argc -= optind;
3942 argv += optind;
3944 #ifndef PROFILE
3945 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3946 "unveil", NULL) == -1)
3947 err(1, "pledge");
3948 #endif
3949 if (abort_rebase && continue_rebase)
3950 usage_rebase();
3951 else if (abort_rebase || continue_rebase) {
3952 if (argc != 0)
3953 usage_rebase();
3954 } else if (argc != 1)
3955 usage_rebase();
3957 cwd = getcwd(NULL, 0);
3958 if (cwd == NULL) {
3959 error = got_error_from_errno("getcwd");
3960 goto done;
3962 error = got_worktree_open(&worktree, cwd);
3963 if (error)
3964 goto done;
3966 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3967 if (error != NULL)
3968 goto done;
3970 error = apply_unveil(got_repo_get_path(repo), 0,
3971 got_worktree_get_root_path(worktree));
3972 if (error)
3973 goto done;
3975 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3976 if (error)
3977 goto done;
3979 if (abort_rebase) {
3980 int did_something;
3981 if (!rebase_in_progress) {
3982 error = got_error(GOT_ERR_NOT_REBASING);
3983 goto done;
3985 error = got_worktree_rebase_continue(&resume_commit_id,
3986 &new_base_branch, &tmp_branch, &branch, &fileindex,
3987 worktree, repo);
3988 if (error)
3989 goto done;
3990 printf("Switching work tree to %s\n",
3991 got_ref_get_symref_target(new_base_branch));
3992 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3993 new_base_branch, update_progress, &did_something);
3994 if (error)
3995 goto done;
3996 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3997 goto done; /* nothing else to do */
4000 if (continue_rebase) {
4001 if (!rebase_in_progress) {
4002 error = got_error(GOT_ERR_NOT_REBASING);
4003 goto done;
4005 error = got_worktree_rebase_continue(&resume_commit_id,
4006 &new_base_branch, &tmp_branch, &branch, &fileindex,
4007 worktree, repo);
4008 if (error)
4009 goto done;
4011 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4012 resume_commit_id, repo);
4013 if (error)
4014 goto done;
4016 yca_id = got_object_id_dup(resume_commit_id);
4017 if (yca_id == NULL) {
4018 error = got_error_from_errno("got_object_id_dup");
4019 goto done;
4021 } else {
4022 error = got_ref_open(&branch, repo, argv[0], 0);
4023 if (error != NULL)
4024 goto done;
4027 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4028 if (error)
4029 goto done;
4031 if (!continue_rebase) {
4032 struct got_object_id *base_commit_id;
4034 base_commit_id = got_worktree_get_base_commit_id(worktree);
4035 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4036 base_commit_id, branch_head_commit_id, repo);
4037 if (error)
4038 goto done;
4039 if (yca_id == NULL) {
4040 error = got_error_msg(GOT_ERR_ANCESTRY,
4041 "specified branch shares no common ancestry "
4042 "with work tree's branch");
4043 goto done;
4046 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4047 if (error) {
4048 if (error->code != GOT_ERR_ANCESTRY)
4049 goto done;
4050 error = NULL;
4051 } else {
4052 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4053 "specified branch resolves to a commit which "
4054 "is already contained in work tree's branch");
4055 goto done;
4057 error = got_worktree_rebase_prepare(&new_base_branch,
4058 &tmp_branch, &fileindex, worktree, branch, repo);
4059 if (error)
4060 goto done;
4063 commit_id = branch_head_commit_id;
4064 error = got_object_open_as_commit(&commit, repo, commit_id);
4065 if (error)
4066 goto done;
4068 parent_ids = got_object_commit_get_parent_ids(commit);
4069 pid = SIMPLEQ_FIRST(parent_ids);
4070 error = collect_commits(&commits, commit_id, pid->id,
4071 yca_id, got_worktree_get_path_prefix(worktree),
4072 GOT_ERR_REBASE_PATH, repo);
4073 got_object_commit_close(commit);
4074 commit = NULL;
4075 if (error)
4076 goto done;
4078 if (SIMPLEQ_EMPTY(&commits)) {
4079 if (continue_rebase)
4080 error = rebase_complete(worktree, fileindex,
4081 branch, new_base_branch, tmp_branch, repo);
4082 else
4083 error = got_error(GOT_ERR_EMPTY_REBASE);
4084 goto done;
4087 pid = NULL;
4088 SIMPLEQ_FOREACH(qid, &commits, entry) {
4089 commit_id = qid->id;
4090 parent_id = pid ? pid->id : yca_id;
4091 pid = qid;
4093 error = got_worktree_rebase_merge_files(&merged_paths,
4094 worktree, fileindex, parent_id, commit_id, repo,
4095 rebase_progress, &rebase_status, check_cancelled, NULL);
4096 if (error)
4097 goto done;
4099 if (rebase_status == GOT_STATUS_CONFLICT) {
4100 got_worktree_rebase_pathlist_free(&merged_paths);
4101 break;
4104 error = rebase_commit(&merged_paths, worktree, fileindex,
4105 tmp_branch, commit_id, repo);
4106 got_worktree_rebase_pathlist_free(&merged_paths);
4107 if (error)
4108 goto done;
4111 if (rebase_status == GOT_STATUS_CONFLICT) {
4112 error = got_worktree_rebase_postpone(worktree, fileindex);
4113 if (error)
4114 goto done;
4115 error = got_error_msg(GOT_ERR_CONFLICTS,
4116 "conflicts must be resolved before rebasing can continue");
4117 } else
4118 error = rebase_complete(worktree, fileindex, branch,
4119 new_base_branch, tmp_branch, repo);
4120 done:
4121 got_object_id_queue_free(&commits);
4122 free(branch_head_commit_id);
4123 free(resume_commit_id);
4124 free(yca_id);
4125 if (commit)
4126 got_object_commit_close(commit);
4127 if (branch)
4128 got_ref_close(branch);
4129 if (new_base_branch)
4130 got_ref_close(new_base_branch);
4131 if (tmp_branch)
4132 got_ref_close(tmp_branch);
4133 if (worktree)
4134 got_worktree_close(worktree);
4135 if (repo)
4136 got_repo_close(repo);
4137 return error;
4140 __dead static void
4141 usage_histedit(void)
4143 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4144 getprogname());
4145 exit(1);
4148 #define GOT_HISTEDIT_PICK 'p'
4149 #define GOT_HISTEDIT_EDIT 'e'
4150 #define GOT_HISTEDIT_FOLD 'f'
4151 #define GOT_HISTEDIT_DROP 'd'
4152 #define GOT_HISTEDIT_MESG 'm'
4154 static struct got_histedit_cmd {
4155 unsigned char code;
4156 const char *name;
4157 const char *desc;
4158 } got_histedit_cmds[] = {
4159 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4160 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4161 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4162 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4163 { GOT_HISTEDIT_MESG, "mesg",
4164 "single-line log message for commit above (open editor if empty)" },
4167 struct got_histedit_list_entry {
4168 TAILQ_ENTRY(got_histedit_list_entry) entry;
4169 struct got_object_id *commit_id;
4170 const struct got_histedit_cmd *cmd;
4171 char *logmsg;
4173 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4175 static const struct got_error *
4176 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4177 FILE *f, struct got_repository *repo)
4179 const struct got_error *err = NULL;
4180 char *logmsg = NULL, *id_str = NULL;
4181 struct got_commit_object *commit = NULL;
4182 size_t n;
4184 err = got_object_open_as_commit(&commit, repo, commit_id);
4185 if (err)
4186 goto done;
4188 err = get_short_logmsg(&logmsg, 34, commit);
4189 if (err)
4190 goto done;
4192 err = got_object_id_str(&id_str, commit_id);
4193 if (err)
4194 goto done;
4196 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4197 if (n < 0)
4198 err = got_ferror(f, GOT_ERR_IO);
4199 done:
4200 if (commit)
4201 got_object_commit_close(commit);
4202 free(id_str);
4203 free(logmsg);
4204 return err;
4207 static const struct got_error *
4208 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4209 struct got_repository *repo)
4211 const struct got_error *err = NULL;
4212 struct got_object_qid *qid;
4214 if (SIMPLEQ_EMPTY(commits))
4215 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4217 SIMPLEQ_FOREACH(qid, commits, entry) {
4218 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4219 f, repo);
4220 if (err)
4221 break;
4224 return err;
4227 static const struct got_error *
4228 write_cmd_list(FILE *f)
4230 const struct got_error *err = NULL;
4231 int n, i;
4233 n = fprintf(f, "# Available histedit commands:\n");
4234 if (n < 0)
4235 return got_ferror(f, GOT_ERR_IO);
4237 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4238 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4239 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4240 cmd->desc);
4241 if (n < 0) {
4242 err = got_ferror(f, GOT_ERR_IO);
4243 break;
4246 n = fprintf(f, "# Commits will be processed in order from top to "
4247 "bottom of this file.\n");
4248 if (n < 0)
4249 return got_ferror(f, GOT_ERR_IO);
4250 return err;
4253 static const struct got_error *
4254 histedit_syntax_error(int lineno)
4256 static char msg[42];
4257 int ret;
4259 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4260 lineno);
4261 if (ret == -1 || ret >= sizeof(msg))
4262 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4264 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4267 static const struct got_error *
4268 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4269 char *logmsg, struct got_repository *repo)
4271 const struct got_error *err;
4272 struct got_commit_object *folded_commit = NULL;
4273 char *id_str;
4275 err = got_object_id_str(&id_str, hle->commit_id);
4276 if (err)
4277 return err;
4279 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4280 if (err)
4281 goto done;
4283 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4284 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4285 got_object_commit_get_logmsg(folded_commit)) == -1) {
4286 err = got_error_from_errno("asprintf");
4287 goto done;
4289 done:
4290 if (folded_commit)
4291 got_object_commit_close(folded_commit);
4292 free(id_str);
4293 return err;
4296 static struct got_histedit_list_entry *
4297 get_folded_commits(struct got_histedit_list_entry *hle)
4299 struct got_histedit_list_entry *prev, *folded = NULL;
4301 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4302 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4303 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4304 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4305 folded = prev;
4306 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4309 return folded;
4312 static const struct got_error *
4313 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4314 struct got_repository *repo)
4316 char *logmsg_path = NULL, *id_str = NULL;
4317 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4318 const struct got_error *err = NULL;
4319 struct got_commit_object *commit = NULL;
4320 int fd;
4321 struct got_histedit_list_entry *folded = NULL;
4323 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4324 if (err)
4325 return err;
4327 folded = get_folded_commits(hle);
4328 if (folded) {
4329 while (folded != hle) {
4330 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4331 folded = TAILQ_NEXT(folded, entry);
4332 continue;
4334 err = append_folded_commit_msg(&new_msg, folded,
4335 logmsg, repo);
4336 if (err)
4337 goto done;
4338 free(logmsg);
4339 logmsg = new_msg;
4340 folded = TAILQ_NEXT(folded, entry);
4344 err = got_object_id_str(&id_str, hle->commit_id);
4345 if (err)
4346 goto done;
4347 if (asprintf(&new_msg,
4348 "%s\n# original log message of commit %s: %s",
4349 logmsg ? logmsg : "", id_str,
4350 got_object_commit_get_logmsg(commit)) == -1) {
4351 err = got_error_from_errno("asprintf");
4352 goto done;
4354 free(logmsg);
4355 logmsg = new_msg;
4357 err = got_object_id_str(&id_str, hle->commit_id);
4358 if (err)
4359 goto done;
4361 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4362 if (err)
4363 goto done;
4365 dprintf(fd, logmsg);
4366 close(fd);
4368 err = get_editor(&editor);
4369 if (err)
4370 goto done;
4372 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4373 if (err) {
4374 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4375 goto done;
4376 err = NULL;
4377 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4378 if (hle->logmsg == NULL)
4379 err = got_error_from_errno("strdup");
4381 done:
4382 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4383 err = got_error_from_errno2("unlink", logmsg_path);
4384 free(logmsg_path);
4385 free(logmsg);
4386 free(editor);
4387 if (commit)
4388 got_object_commit_close(commit);
4389 return err;
4392 static const struct got_error *
4393 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4394 FILE *f, struct got_repository *repo)
4396 const struct got_error *err = NULL;
4397 char *line = NULL, *p, *end;
4398 size_t size;
4399 ssize_t len;
4400 int lineno = 0, i;
4401 const struct got_histedit_cmd *cmd;
4402 struct got_object_id *commit_id = NULL;
4403 struct got_histedit_list_entry *hle = NULL;
4405 for (;;) {
4406 len = getline(&line, &size, f);
4407 if (len == -1) {
4408 const struct got_error *getline_err;
4409 if (feof(f))
4410 break;
4411 getline_err = got_error_from_errno("getline");
4412 err = got_ferror(f, getline_err->code);
4413 break;
4415 lineno++;
4416 p = line;
4417 while (isspace((unsigned char)p[0]))
4418 p++;
4419 if (p[0] == '#' || p[0] == '\0') {
4420 free(line);
4421 line = NULL;
4422 continue;
4424 cmd = NULL;
4425 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4426 cmd = &got_histedit_cmds[i];
4427 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4428 isspace((unsigned char)p[strlen(cmd->name)])) {
4429 p += strlen(cmd->name);
4430 break;
4432 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4433 p++;
4434 break;
4437 if (i == nitems(got_histedit_cmds)) {
4438 err = histedit_syntax_error(lineno);
4439 break;
4441 while (isspace((unsigned char)p[0]))
4442 p++;
4443 if (cmd->code == GOT_HISTEDIT_MESG) {
4444 if (hle == NULL || hle->logmsg != NULL) {
4445 err = got_error(GOT_ERR_HISTEDIT_CMD);
4446 break;
4448 if (p[0] == '\0') {
4449 err = histedit_edit_logmsg(hle, repo);
4450 if (err)
4451 break;
4452 } else {
4453 hle->logmsg = strdup(p);
4454 if (hle->logmsg == NULL) {
4455 err = got_error_from_errno("strdup");
4456 break;
4459 free(line);
4460 line = NULL;
4461 continue;
4462 } else {
4463 end = p;
4464 while (end[0] && !isspace((unsigned char)end[0]))
4465 end++;
4466 *end = '\0';
4468 err = got_object_resolve_id_str(&commit_id, repo, p);
4469 if (err) {
4470 /* override error code */
4471 err = histedit_syntax_error(lineno);
4472 break;
4475 hle = malloc(sizeof(*hle));
4476 if (hle == NULL) {
4477 err = got_error_from_errno("malloc");
4478 break;
4480 hle->cmd = cmd;
4481 hle->commit_id = commit_id;
4482 hle->logmsg = NULL;
4483 commit_id = NULL;
4484 free(line);
4485 line = NULL;
4486 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4489 free(line);
4490 free(commit_id);
4491 return err;
4494 static const struct got_error *
4495 histedit_check_script(struct got_histedit_list *histedit_cmds,
4496 struct got_object_id_queue *commits, struct got_repository *repo)
4498 const struct got_error *err = NULL;
4499 struct got_object_qid *qid;
4500 struct got_histedit_list_entry *hle;
4501 static char msg[80];
4502 char *id_str;
4504 if (TAILQ_EMPTY(histedit_cmds))
4505 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4506 "histedit script contains no commands");
4508 SIMPLEQ_FOREACH(qid, commits, entry) {
4509 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4510 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4511 break;
4513 if (hle == NULL) {
4514 err = got_object_id_str(&id_str, qid->id);
4515 if (err)
4516 return err;
4517 snprintf(msg, sizeof(msg),
4518 "commit %s missing from histedit script", id_str);
4519 free(id_str);
4520 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4524 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4525 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4526 "last commit in histedit script cannot be folded");
4528 return NULL;
4531 static const struct got_error *
4532 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4533 const char *path, struct got_object_id_queue *commits,
4534 struct got_repository *repo)
4536 const struct got_error *err = NULL;
4537 char *editor;
4538 FILE *f = NULL;
4540 err = get_editor(&editor);
4541 if (err)
4542 return err;
4544 if (spawn_editor(editor, path) == -1) {
4545 err = got_error_from_errno("failed spawning editor");
4546 goto done;
4549 f = fopen(path, "r");
4550 if (f == NULL) {
4551 err = got_error_from_errno("fopen");
4552 goto done;
4554 err = histedit_parse_list(histedit_cmds, f, repo);
4555 if (err)
4556 goto done;
4558 err = histedit_check_script(histedit_cmds, commits, repo);
4559 done:
4560 if (f && fclose(f) != 0 && err == NULL)
4561 err = got_error_from_errno("fclose");
4562 free(editor);
4563 return err;
4566 static const struct got_error *
4567 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4568 struct got_object_id_queue *, const char *, struct got_repository *);
4570 static const struct got_error *
4571 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4572 struct got_object_id_queue *commits, struct got_repository *repo)
4574 const struct got_error *err;
4575 FILE *f = NULL;
4576 char *path = NULL;
4578 err = got_opentemp_named(&path, &f, "got-histedit");
4579 if (err)
4580 return err;
4582 err = write_cmd_list(f);
4583 if (err)
4584 goto done;
4586 err = histedit_write_commit_list(commits, f, repo);
4587 if (err)
4588 goto done;
4590 if (fclose(f) != 0) {
4591 err = got_error_from_errno("fclose");
4592 goto done;
4594 f = NULL;
4596 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4597 if (err) {
4598 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4599 err->code != GOT_ERR_HISTEDIT_CMD)
4600 goto done;
4601 err = histedit_edit_list_retry(histedit_cmds, err,
4602 commits, path, repo);
4604 done:
4605 if (f && fclose(f) != 0 && err == NULL)
4606 err = got_error_from_errno("fclose");
4607 if (path && unlink(path) != 0 && err == NULL)
4608 err = got_error_from_errno2("unlink", path);
4609 free(path);
4610 return err;
4613 static const struct got_error *
4614 histedit_save_list(struct got_histedit_list *histedit_cmds,
4615 struct got_worktree *worktree, struct got_repository *repo)
4617 const struct got_error *err = NULL;
4618 char *path = NULL;
4619 FILE *f = NULL;
4620 struct got_histedit_list_entry *hle;
4621 struct got_commit_object *commit = NULL;
4623 err = got_worktree_get_histedit_script_path(&path, worktree);
4624 if (err)
4625 return err;
4627 f = fopen(path, "w");
4628 if (f == NULL) {
4629 err = got_error_from_errno2("fopen", path);
4630 goto done;
4632 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4633 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4634 repo);
4635 if (err)
4636 break;
4638 if (hle->logmsg) {
4639 int n = fprintf(f, "%c %s\n",
4640 GOT_HISTEDIT_MESG, hle->logmsg);
4641 if (n < 0) {
4642 err = got_ferror(f, GOT_ERR_IO);
4643 break;
4647 done:
4648 if (f && fclose(f) != 0 && err == NULL)
4649 err = got_error_from_errno("fclose");
4650 free(path);
4651 if (commit)
4652 got_object_commit_close(commit);
4653 return err;
4656 void
4657 histedit_free_list(struct got_histedit_list *histedit_cmds)
4659 struct got_histedit_list_entry *hle;
4661 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4662 TAILQ_REMOVE(histedit_cmds, hle, entry);
4663 free(hle);
4667 static const struct got_error *
4668 histedit_load_list(struct got_histedit_list *histedit_cmds,
4669 const char *path, struct got_repository *repo)
4671 const struct got_error *err = NULL;
4672 FILE *f = NULL;
4674 f = fopen(path, "r");
4675 if (f == NULL) {
4676 err = got_error_from_errno2("fopen", path);
4677 goto done;
4680 err = histedit_parse_list(histedit_cmds, f, repo);
4681 done:
4682 if (f && fclose(f) != 0 && err == NULL)
4683 err = got_error_from_errno("fclose");
4684 return err;
4687 static const struct got_error *
4688 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4689 const struct got_error *edit_err, struct got_object_id_queue *commits,
4690 const char *path, struct got_repository *repo)
4692 const struct got_error *err = NULL, *prev_err = edit_err;
4693 int resp = ' ';
4695 while (resp != 'c' && resp != 'r' && resp != 'a') {
4696 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4697 "or (a)bort: ", getprogname(), prev_err->msg);
4698 resp = getchar();
4699 if (resp == 'c') {
4700 histedit_free_list(histedit_cmds);
4701 err = histedit_run_editor(histedit_cmds, path, commits,
4702 repo);
4703 if (err) {
4704 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4705 err->code != GOT_ERR_HISTEDIT_CMD)
4706 break;
4707 prev_err = err;
4708 resp = ' ';
4709 continue;
4711 break;
4712 } else if (resp == 'r') {
4713 histedit_free_list(histedit_cmds);
4714 err = histedit_edit_script(histedit_cmds,
4715 commits, repo);
4716 if (err) {
4717 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4718 err->code != GOT_ERR_HISTEDIT_CMD)
4719 break;
4720 prev_err = err;
4721 resp = ' ';
4722 continue;
4724 break;
4725 } else if (resp == 'a') {
4726 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4727 break;
4728 } else
4729 printf("invalid response '%c'\n", resp);
4732 return err;
4735 static const struct got_error *
4736 histedit_complete(struct got_worktree *worktree,
4737 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4738 struct got_reference *branch, struct got_repository *repo)
4740 printf("Switching work tree to %s\n",
4741 got_ref_get_symref_target(branch));
4742 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4743 branch, repo);
4746 static const struct got_error *
4747 show_histedit_progress(struct got_commit_object *commit,
4748 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4750 const struct got_error *err;
4751 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4753 err = got_object_id_str(&old_id_str, hle->commit_id);
4754 if (err)
4755 goto done;
4757 if (new_id) {
4758 err = got_object_id_str(&new_id_str, new_id);
4759 if (err)
4760 goto done;
4763 old_id_str[12] = '\0';
4764 if (new_id_str)
4765 new_id_str[12] = '\0';
4767 if (hle->logmsg) {
4768 logmsg = strdup(hle->logmsg);
4769 if (logmsg == NULL) {
4770 err = got_error_from_errno("strdup");
4771 goto done;
4773 trim_logmsg(logmsg, 42);
4774 } else {
4775 err = get_short_logmsg(&logmsg, 42, commit);
4776 if (err)
4777 goto done;
4780 switch (hle->cmd->code) {
4781 case GOT_HISTEDIT_PICK:
4782 case GOT_HISTEDIT_EDIT:
4783 printf("%s -> %s: %s\n", old_id_str,
4784 new_id_str ? new_id_str : "no-op change", logmsg);
4785 break;
4786 case GOT_HISTEDIT_DROP:
4787 case GOT_HISTEDIT_FOLD:
4788 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4789 logmsg);
4790 break;
4791 default:
4792 break;
4795 done:
4796 free(old_id_str);
4797 free(new_id_str);
4798 return err;
4801 static const struct got_error *
4802 histedit_commit(struct got_pathlist_head *merged_paths,
4803 struct got_worktree *worktree, struct got_fileindex *fileindex,
4804 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4805 struct got_repository *repo)
4807 const struct got_error *err;
4808 struct got_commit_object *commit;
4809 struct got_object_id *new_commit_id;
4811 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4812 && hle->logmsg == NULL) {
4813 err = histedit_edit_logmsg(hle, repo);
4814 if (err)
4815 return err;
4818 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4819 if (err)
4820 return err;
4822 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4823 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4824 hle->logmsg, repo);
4825 if (err) {
4826 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4827 goto done;
4828 err = show_histedit_progress(commit, hle, NULL);
4829 } else {
4830 err = show_histedit_progress(commit, hle, new_commit_id);
4831 free(new_commit_id);
4833 done:
4834 got_object_commit_close(commit);
4835 return err;
4838 static const struct got_error *
4839 histedit_skip_commit(struct got_histedit_list_entry *hle,
4840 struct got_worktree *worktree, struct got_repository *repo)
4842 const struct got_error *error;
4843 struct got_commit_object *commit;
4845 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4846 repo);
4847 if (error)
4848 return error;
4850 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4851 if (error)
4852 return error;
4854 error = show_histedit_progress(commit, hle, NULL);
4855 got_object_commit_close(commit);
4856 return error;
4859 static const struct got_error *
4860 cmd_histedit(int argc, char *argv[])
4862 const struct got_error *error = NULL;
4863 struct got_worktree *worktree = NULL;
4864 struct got_fileindex *fileindex = NULL;
4865 struct got_repository *repo = NULL;
4866 char *cwd = NULL;
4867 struct got_reference *branch = NULL;
4868 struct got_reference *tmp_branch = NULL;
4869 struct got_object_id *resume_commit_id = NULL;
4870 struct got_object_id *base_commit_id = NULL;
4871 struct got_object_id *head_commit_id = NULL;
4872 struct got_commit_object *commit = NULL;
4873 int ch, rebase_in_progress = 0, did_something;
4874 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4875 const char *edit_script_path = NULL;
4876 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4877 struct got_object_id_queue commits;
4878 struct got_pathlist_head merged_paths;
4879 const struct got_object_id_queue *parent_ids;
4880 struct got_object_qid *pid;
4881 struct got_histedit_list histedit_cmds;
4882 struct got_histedit_list_entry *hle;
4884 SIMPLEQ_INIT(&commits);
4885 TAILQ_INIT(&histedit_cmds);
4886 TAILQ_INIT(&merged_paths);
4888 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4889 switch (ch) {
4890 case 'a':
4891 abort_edit = 1;
4892 break;
4893 case 'c':
4894 continue_edit = 1;
4895 break;
4896 case 'F':
4897 edit_script_path = optarg;
4898 break;
4899 default:
4900 usage_histedit();
4901 /* NOTREACHED */
4905 argc -= optind;
4906 argv += optind;
4908 #ifndef PROFILE
4909 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4910 "unveil", NULL) == -1)
4911 err(1, "pledge");
4912 #endif
4913 if (abort_edit && continue_edit)
4914 usage_histedit();
4915 if (argc != 0)
4916 usage_histedit();
4919 * This command cannot apply unveil(2) in all cases because the
4920 * user may choose to run an editor to edit the histedit script
4921 * and to edit individual commit log messages.
4922 * unveil(2) traverses exec(2); if an editor is used we have to
4923 * apply unveil after edit script and log messages have been written.
4924 * XXX TODO: Make use of unveil(2) where possible.
4927 cwd = getcwd(NULL, 0);
4928 if (cwd == NULL) {
4929 error = got_error_from_errno("getcwd");
4930 goto done;
4932 error = got_worktree_open(&worktree, cwd);
4933 if (error)
4934 goto done;
4936 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4937 if (error != NULL)
4938 goto done;
4940 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4941 if (error)
4942 goto done;
4943 if (rebase_in_progress) {
4944 error = got_error(GOT_ERR_REBASING);
4945 goto done;
4948 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4949 if (error)
4950 goto done;
4952 if (edit_in_progress && abort_edit) {
4953 error = got_worktree_histedit_continue(&resume_commit_id,
4954 &tmp_branch, &branch, &base_commit_id, &fileindex,
4955 worktree, repo);
4956 if (error)
4957 goto done;
4958 printf("Switching work tree to %s\n",
4959 got_ref_get_symref_target(branch));
4960 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4961 branch, base_commit_id, update_progress, &did_something);
4962 if (error)
4963 goto done;
4964 printf("Histedit of %s aborted\n",
4965 got_ref_get_symref_target(branch));
4966 goto done; /* nothing else to do */
4967 } else if (abort_edit) {
4968 error = got_error(GOT_ERR_NOT_HISTEDIT);
4969 goto done;
4972 if (continue_edit) {
4973 char *path;
4975 if (!edit_in_progress) {
4976 error = got_error(GOT_ERR_NOT_HISTEDIT);
4977 goto done;
4980 error = got_worktree_get_histedit_script_path(&path, worktree);
4981 if (error)
4982 goto done;
4984 error = histedit_load_list(&histedit_cmds, path, repo);
4985 free(path);
4986 if (error)
4987 goto done;
4989 error = got_worktree_histedit_continue(&resume_commit_id,
4990 &tmp_branch, &branch, &base_commit_id, &fileindex,
4991 worktree, repo);
4992 if (error)
4993 goto done;
4995 error = got_ref_resolve(&head_commit_id, repo, branch);
4996 if (error)
4997 goto done;
4999 error = got_object_open_as_commit(&commit, repo,
5000 head_commit_id);
5001 if (error)
5002 goto done;
5003 parent_ids = got_object_commit_get_parent_ids(commit);
5004 pid = SIMPLEQ_FIRST(parent_ids);
5005 if (pid == NULL) {
5006 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5007 goto done;
5009 error = collect_commits(&commits, head_commit_id, pid->id,
5010 base_commit_id, got_worktree_get_path_prefix(worktree),
5011 GOT_ERR_HISTEDIT_PATH, repo);
5012 got_object_commit_close(commit);
5013 commit = NULL;
5014 if (error)
5015 goto done;
5016 } else {
5017 if (edit_in_progress) {
5018 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5019 goto done;
5022 error = got_ref_open(&branch, repo,
5023 got_worktree_get_head_ref_name(worktree), 0);
5024 if (error != NULL)
5025 goto done;
5027 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5028 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5029 "will not edit commit history of a branch outside "
5030 "the \"refs/heads/\" reference namespace");
5031 goto done;
5034 error = got_ref_resolve(&head_commit_id, repo, branch);
5035 got_ref_close(branch);
5036 branch = NULL;
5037 if (error)
5038 goto done;
5040 error = got_object_open_as_commit(&commit, repo,
5041 head_commit_id);
5042 if (error)
5043 goto done;
5044 parent_ids = got_object_commit_get_parent_ids(commit);
5045 pid = SIMPLEQ_FIRST(parent_ids);
5046 if (pid == NULL) {
5047 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5048 goto done;
5050 error = collect_commits(&commits, head_commit_id, pid->id,
5051 got_worktree_get_base_commit_id(worktree),
5052 got_worktree_get_path_prefix(worktree),
5053 GOT_ERR_HISTEDIT_PATH, repo);
5054 got_object_commit_close(commit);
5055 commit = NULL;
5056 if (error)
5057 goto done;
5059 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5060 &base_commit_id, &fileindex, worktree, repo);
5061 if (error)
5062 goto done;
5064 if (edit_script_path) {
5065 error = histedit_load_list(&histedit_cmds,
5066 edit_script_path, repo);
5067 if (error) {
5068 got_worktree_histedit_abort(worktree, fileindex,
5069 repo, branch, base_commit_id,
5070 update_progress, &did_something);
5071 goto done;
5073 } else {
5074 error = histedit_edit_script(&histedit_cmds, &commits,
5075 repo);
5076 if (error) {
5077 got_worktree_histedit_abort(worktree, fileindex,
5078 repo, branch, base_commit_id,
5079 update_progress, &did_something);
5080 goto done;
5085 error = histedit_save_list(&histedit_cmds, worktree,
5086 repo);
5087 if (error) {
5088 got_worktree_histedit_abort(worktree, fileindex,
5089 repo, branch, base_commit_id,
5090 update_progress, &did_something);
5091 goto done;
5096 error = histedit_check_script(&histedit_cmds, &commits, repo);
5097 if (error)
5098 goto done;
5100 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5101 if (resume_commit_id) {
5102 if (got_object_id_cmp(hle->commit_id,
5103 resume_commit_id) != 0)
5104 continue;
5106 resume_commit_id = NULL;
5107 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5108 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5109 error = histedit_skip_commit(hle, worktree,
5110 repo);
5111 } else {
5112 error = histedit_commit(NULL, worktree,
5113 fileindex, tmp_branch, hle, repo);
5115 if (error)
5116 goto done;
5117 continue;
5120 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5121 error = histedit_skip_commit(hle, worktree, repo);
5122 if (error)
5123 goto done;
5124 continue;
5127 error = got_object_open_as_commit(&commit, repo,
5128 hle->commit_id);
5129 if (error)
5130 goto done;
5131 parent_ids = got_object_commit_get_parent_ids(commit);
5132 pid = SIMPLEQ_FIRST(parent_ids);
5134 error = got_worktree_histedit_merge_files(&merged_paths,
5135 worktree, fileindex, pid->id, hle->commit_id, repo,
5136 rebase_progress, &rebase_status, check_cancelled, NULL);
5137 if (error)
5138 goto done;
5139 got_object_commit_close(commit);
5140 commit = NULL;
5142 if (rebase_status == GOT_STATUS_CONFLICT) {
5143 got_worktree_rebase_pathlist_free(&merged_paths);
5144 break;
5147 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5148 char *id_str;
5149 error = got_object_id_str(&id_str, hle->commit_id);
5150 if (error)
5151 goto done;
5152 printf("Stopping histedit for amending commit %s\n",
5153 id_str);
5154 free(id_str);
5155 got_worktree_rebase_pathlist_free(&merged_paths);
5156 error = got_worktree_histedit_postpone(worktree,
5157 fileindex);
5158 goto done;
5161 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5162 error = histedit_skip_commit(hle, worktree, repo);
5163 if (error)
5164 goto done;
5165 continue;
5168 error = histedit_commit(&merged_paths, worktree, fileindex,
5169 tmp_branch, hle, repo);
5170 got_worktree_rebase_pathlist_free(&merged_paths);
5171 if (error)
5172 goto done;
5175 if (rebase_status == GOT_STATUS_CONFLICT) {
5176 error = got_worktree_histedit_postpone(worktree, fileindex);
5177 if (error)
5178 goto done;
5179 error = got_error_msg(GOT_ERR_CONFLICTS,
5180 "conflicts must be resolved before rebasing can continue");
5181 } else
5182 error = histedit_complete(worktree, fileindex, tmp_branch,
5183 branch, repo);
5184 done:
5185 got_object_id_queue_free(&commits);
5186 histedit_free_list(&histedit_cmds);
5187 free(head_commit_id);
5188 free(base_commit_id);
5189 free(resume_commit_id);
5190 if (commit)
5191 got_object_commit_close(commit);
5192 if (branch)
5193 got_ref_close(branch);
5194 if (tmp_branch)
5195 got_ref_close(tmp_branch);
5196 if (worktree)
5197 got_worktree_close(worktree);
5198 if (repo)
5199 got_repo_close(repo);
5200 return error;
5203 __dead static void
5204 usage_stage(void)
5206 fprintf(stderr, "usage: %s stage [-l] | file-path ...\n",
5207 getprogname());
5208 exit(1);
5211 static const struct got_error *
5212 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5213 const char *path, struct got_object_id *blob_id,
5214 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5216 const struct got_error *err = NULL;
5217 char *id_str = NULL;
5219 if (staged_status != GOT_STATUS_ADD &&
5220 staged_status != GOT_STATUS_MODIFY &&
5221 staged_status != GOT_STATUS_DELETE)
5222 return NULL;
5224 if (staged_status == GOT_STATUS_ADD ||
5225 staged_status == GOT_STATUS_MODIFY)
5226 err = got_object_id_str(&id_str, staged_blob_id);
5227 else
5228 err = got_object_id_str(&id_str, blob_id);
5229 if (err)
5230 return err;
5232 printf("%s %c %s\n", id_str, staged_status, path);
5233 free(id_str);
5234 return NULL;
5237 static const struct got_error *
5238 cmd_stage(int argc, char *argv[])
5240 const struct got_error *error = NULL;
5241 struct got_repository *repo = NULL;
5242 struct got_worktree *worktree = NULL;
5243 char *cwd = NULL;
5244 struct got_pathlist_head paths;
5245 struct got_pathlist_entry *pe;
5246 const char *worktree_path;
5247 int ch, x, list_stage = 0;
5249 TAILQ_INIT(&paths);
5251 while ((ch = getopt(argc, argv, "l")) != -1) {
5252 switch (ch) {
5253 case 'l':
5254 list_stage = 1;
5255 break;
5256 default:
5257 usage_stage();
5258 /* NOTREACHED */
5262 argc -= optind;
5263 argv += optind;
5265 #ifndef PROFILE
5266 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5267 "unveil", NULL) == -1)
5268 err(1, "pledge");
5269 #endif
5270 if (!list_stage && argc < 1)
5271 usage_stage();
5273 cwd = getcwd(NULL, 0);
5274 if (cwd == NULL) {
5275 error = got_error_from_errno("getcwd");
5276 goto done;
5279 error = got_worktree_open(&worktree, cwd);
5280 if (error)
5281 goto done;
5283 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5284 if (error != NULL)
5285 goto done;
5287 error = apply_unveil(got_repo_get_path(repo), 1,
5288 got_worktree_get_root_path(worktree));
5289 if (error)
5290 goto done;
5292 worktree_path = got_worktree_get_root_path(worktree);
5293 for (x = 0; x < argc; x++) {
5294 char *path;
5295 if (list_stage) {
5296 error = got_worktree_resolve_path(&path, worktree,
5297 argv[x]);
5298 if (error)
5299 break;
5300 } else
5301 path = realpath(argv[x], NULL);
5302 if (path == NULL) {
5303 if (errno != ENOENT) {
5304 error = got_error_from_errno2("realpath",
5305 argv[x]);
5306 goto done;
5308 if (got_path_is_child(argv[x], worktree_path,
5309 strlen(worktree_path))) {
5310 path = strdup(argv[x]);
5311 if (path == NULL) {
5312 error = got_error_from_errno("strdup");
5313 goto done;
5316 } else if (asprintf(&path, "%s/%s",
5317 got_worktree_get_root_path(worktree),
5318 argv[x]) == -1) {
5319 error = got_error_from_errno("asprintf");
5320 goto done;
5324 got_path_strip_trailing_slashes(path);
5325 error = got_pathlist_insert(&pe, &paths, path, NULL);
5326 if (error) {
5327 free(path);
5328 goto done;
5332 if (list_stage) {
5333 if (TAILQ_EMPTY(&paths)) {
5334 char *s = strdup("");
5335 if (s == NULL) {
5336 error = got_error_from_errno("strdup");
5337 goto done;
5339 error = got_pathlist_append(&paths, s, NULL);
5340 if (error)
5341 goto done;
5343 error = got_worktree_status(worktree, &paths, repo,
5344 print_stage, NULL, check_cancelled, NULL);
5345 } else
5346 error = got_worktree_stage(worktree, &paths, print_status,
5347 NULL, repo);
5348 done:
5349 if (repo)
5350 got_repo_close(repo);
5351 if (worktree)
5352 got_worktree_close(worktree);
5353 TAILQ_FOREACH(pe, &paths, entry)
5354 free((char *)pe->path);
5355 got_pathlist_free(&paths);
5356 free(cwd);
5357 return error;