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] "
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;
1697 static const struct got_error *
1698 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1699 const char *path, struct got_object_id *blob_id,
1700 struct got_object_id *commit_id)
1702 struct print_diff_arg *a = arg;
1703 const struct got_error *err = NULL;
1704 struct got_blob_object *blob1 = NULL;
1705 FILE *f2 = NULL;
1706 char *abspath = NULL;
1707 struct stat sb;
1709 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1710 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1711 return NULL;
1713 if (!a->header_shown) {
1714 printf("diff %s %s\n", a->id_str,
1715 got_worktree_get_root_path(a->worktree));
1716 a->header_shown = 1;
1719 if (status != GOT_STATUS_ADD) {
1720 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1721 if (err)
1722 goto done;
1726 if (status != GOT_STATUS_DELETE) {
1727 if (asprintf(&abspath, "%s/%s",
1728 got_worktree_get_root_path(a->worktree), path) == -1) {
1729 err = got_error_from_errno("asprintf");
1730 goto done;
1733 f2 = fopen(abspath, "r");
1734 if (f2 == NULL) {
1735 err = got_error_from_errno2("fopen", abspath);
1736 goto done;
1738 if (lstat(abspath, &sb) == -1) {
1739 err = got_error_from_errno2("lstat", abspath);
1740 goto done;
1742 } else
1743 sb.st_size = 0;
1745 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1746 stdout);
1747 done:
1748 if (blob1)
1749 got_object_blob_close(blob1);
1750 if (f2 && fclose(f2) != 0 && err == NULL)
1751 err = got_error_from_errno("fclose");
1752 free(abspath);
1753 return err;
1756 static const struct got_error *
1757 cmd_diff(int argc, char *argv[])
1759 const struct got_error *error;
1760 struct got_repository *repo = NULL;
1761 struct got_worktree *worktree = NULL;
1762 char *cwd = NULL, *repo_path = NULL;
1763 struct got_object_id *id1 = NULL, *id2 = NULL;
1764 const char *id_str1 = NULL, *id_str2 = NULL;
1765 char *label1 = NULL, *label2 = NULL;
1766 int type1, type2;
1767 int diff_context = 3, ch;
1768 const char *errstr;
1769 char *path = NULL;
1771 #ifndef PROFILE
1772 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1773 NULL) == -1)
1774 err(1, "pledge");
1775 #endif
1777 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1778 switch (ch) {
1779 case 'C':
1780 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1781 if (errstr != NULL)
1782 err(1, "-C option %s", errstr);
1783 break;
1784 case 'r':
1785 repo_path = realpath(optarg, NULL);
1786 if (repo_path == NULL)
1787 err(1, "-r option");
1788 got_path_strip_trailing_slashes(repo_path);
1789 break;
1790 default:
1791 usage_diff();
1792 /* NOTREACHED */
1796 argc -= optind;
1797 argv += optind;
1799 cwd = getcwd(NULL, 0);
1800 if (cwd == NULL) {
1801 error = got_error_from_errno("getcwd");
1802 goto done;
1804 error = got_worktree_open(&worktree, cwd);
1805 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1806 goto done;
1807 if (argc <= 1) {
1808 if (worktree == NULL) {
1809 error = got_error(GOT_ERR_NOT_WORKTREE);
1810 goto done;
1812 if (repo_path)
1813 errx(1,
1814 "-r option can't be used when diffing a work tree");
1815 repo_path = strdup(got_worktree_get_repo_path(worktree));
1816 if (repo_path == NULL) {
1817 error = got_error_from_errno("strdup");
1818 goto done;
1820 if (argc == 1) {
1821 error = got_worktree_resolve_path(&path, worktree,
1822 argv[0]);
1823 if (error)
1824 goto done;
1825 } else {
1826 path = strdup("");
1827 if (path == NULL) {
1828 error = got_error_from_errno("strdup");
1829 goto done;
1832 } else if (argc == 2) {
1833 id_str1 = argv[0];
1834 id_str2 = argv[1];
1835 if (worktree && repo_path == NULL) {
1836 repo_path =
1837 strdup(got_worktree_get_repo_path(worktree));
1838 if (repo_path == NULL) {
1839 error = got_error_from_errno("strdup");
1840 goto done;
1843 } else
1844 usage_diff();
1846 if (repo_path == NULL) {
1847 repo_path = getcwd(NULL, 0);
1848 if (repo_path == NULL)
1849 return got_error_from_errno("getcwd");
1852 error = got_repo_open(&repo, repo_path);
1853 free(repo_path);
1854 if (error != NULL)
1855 goto done;
1857 error = apply_unveil(got_repo_get_path(repo), 1,
1858 worktree ? got_worktree_get_root_path(worktree) : NULL);
1859 if (error)
1860 goto done;
1862 if (argc <= 1) {
1863 struct print_diff_arg arg;
1864 struct got_pathlist_head paths;
1865 char *id_str;
1867 TAILQ_INIT(&paths);
1869 error = got_object_id_str(&id_str,
1870 got_worktree_get_base_commit_id(worktree));
1871 if (error)
1872 goto done;
1873 arg.repo = repo;
1874 arg.worktree = worktree;
1875 arg.diff_context = diff_context;
1876 arg.id_str = id_str;
1877 arg.header_shown = 0;
1879 error = got_pathlist_append(&paths, path, NULL);
1880 if (error)
1881 goto done;
1883 error = got_worktree_status(worktree, &paths, repo, print_diff,
1884 &arg, check_cancelled, NULL);
1885 free(id_str);
1886 got_pathlist_free(&paths);
1887 goto done;
1890 error = got_repo_match_object_id_prefix(&id1, id_str1,
1891 GOT_OBJ_TYPE_ANY, repo);
1892 if (error) {
1893 struct got_reference *ref;
1894 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1895 goto done;
1896 error = got_ref_open(&ref, repo, id_str1, 0);
1897 if (error != NULL)
1898 goto done;
1899 label1 = strdup(got_ref_get_name(ref));
1900 if (label1 == NULL) {
1901 error = got_error_from_errno("strdup");
1902 goto done;
1904 error = got_ref_resolve(&id1, repo, ref);
1905 got_ref_close(ref);
1906 if (error != NULL)
1907 goto done;
1908 } else {
1909 error = got_object_id_str(&label1, id1);
1910 if (label1 == NULL) {
1911 error = got_error_from_errno("strdup");
1912 goto done;
1916 error = got_repo_match_object_id_prefix(&id2, id_str2,
1917 GOT_OBJ_TYPE_ANY, repo);
1918 if (error) {
1919 struct got_reference *ref;
1920 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1921 goto done;
1922 error = got_ref_open(&ref, repo, id_str2, 0);
1923 if (error != NULL)
1924 goto done;
1925 label2 = strdup(got_ref_get_name(ref));
1926 if (label2 == NULL) {
1927 error = got_error_from_errno("strdup");
1928 goto done;
1930 error = got_ref_resolve(&id2, repo, ref);
1931 got_ref_close(ref);
1932 if (error != NULL)
1933 goto done;
1934 } else {
1935 error = got_object_id_str(&label2, id2);
1936 if (label2 == NULL) {
1937 error = got_error_from_errno("strdup");
1938 goto done;
1942 error = got_object_get_type(&type1, repo, id1);
1943 if (error)
1944 goto done;
1946 error = got_object_get_type(&type2, repo, id2);
1947 if (error)
1948 goto done;
1950 if (type1 != type2) {
1951 error = got_error(GOT_ERR_OBJ_TYPE);
1952 goto done;
1955 switch (type1) {
1956 case GOT_OBJ_TYPE_BLOB:
1957 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1958 diff_context, repo, stdout);
1959 break;
1960 case GOT_OBJ_TYPE_TREE:
1961 error = got_diff_objects_as_trees(id1, id2, "", "",
1962 diff_context, repo, stdout);
1963 break;
1964 case GOT_OBJ_TYPE_COMMIT:
1965 printf("diff %s %s\n", label1, label2);
1966 error = got_diff_objects_as_commits(id1, id2, diff_context,
1967 repo, stdout);
1968 break;
1969 default:
1970 error = got_error(GOT_ERR_OBJ_TYPE);
1973 done:
1974 free(label1);
1975 free(label2);
1976 free(id1);
1977 free(id2);
1978 free(path);
1979 if (worktree)
1980 got_worktree_close(worktree);
1981 if (repo) {
1982 const struct got_error *repo_error;
1983 repo_error = got_repo_close(repo);
1984 if (error == NULL)
1985 error = repo_error;
1987 return error;
1990 __dead static void
1991 usage_blame(void)
1993 fprintf(stderr,
1994 "usage: %s blame [-c commit] [-r repository-path] path\n",
1995 getprogname());
1996 exit(1);
1999 static const struct got_error *
2000 cmd_blame(int argc, char *argv[])
2002 const struct got_error *error;
2003 struct got_repository *repo = NULL;
2004 struct got_worktree *worktree = NULL;
2005 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2006 struct got_object_id *commit_id = NULL;
2007 char *commit_id_str = NULL;
2008 int ch;
2010 #ifndef PROFILE
2011 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2012 NULL) == -1)
2013 err(1, "pledge");
2014 #endif
2016 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2017 switch (ch) {
2018 case 'c':
2019 commit_id_str = optarg;
2020 break;
2021 case 'r':
2022 repo_path = realpath(optarg, NULL);
2023 if (repo_path == NULL)
2024 err(1, "-r option");
2025 got_path_strip_trailing_slashes(repo_path);
2026 break;
2027 default:
2028 usage_blame();
2029 /* NOTREACHED */
2033 argc -= optind;
2034 argv += optind;
2036 if (argc == 1)
2037 path = argv[0];
2038 else
2039 usage_blame();
2041 cwd = getcwd(NULL, 0);
2042 if (cwd == NULL) {
2043 error = got_error_from_errno("getcwd");
2044 goto done;
2046 if (repo_path == NULL) {
2047 error = got_worktree_open(&worktree, cwd);
2048 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2049 goto done;
2050 else
2051 error = NULL;
2052 if (worktree) {
2053 repo_path =
2054 strdup(got_worktree_get_repo_path(worktree));
2055 if (repo_path == NULL)
2056 error = got_error_from_errno("strdup");
2057 if (error)
2058 goto done;
2059 } else {
2060 repo_path = strdup(cwd);
2061 if (repo_path == NULL) {
2062 error = got_error_from_errno("strdup");
2063 goto done;
2068 error = got_repo_open(&repo, repo_path);
2069 if (error != NULL)
2070 goto done;
2072 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2073 if (error)
2074 goto done;
2076 if (worktree) {
2077 const char *prefix = got_worktree_get_path_prefix(worktree);
2078 char *p, *worktree_subdir = cwd +
2079 strlen(got_worktree_get_root_path(worktree));
2080 if (asprintf(&p, "%s%s%s%s%s",
2081 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2082 worktree_subdir, worktree_subdir[0] ? "/" : "",
2083 path) == -1) {
2084 error = got_error_from_errno("asprintf");
2085 goto done;
2087 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2088 free(p);
2089 } else {
2090 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2092 if (error)
2093 goto done;
2095 if (commit_id_str == NULL) {
2096 struct got_reference *head_ref;
2097 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2098 if (error != NULL)
2099 goto done;
2100 error = got_ref_resolve(&commit_id, repo, head_ref);
2101 got_ref_close(head_ref);
2102 if (error != NULL)
2103 goto done;
2104 } else {
2105 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2106 if (error)
2107 goto done;
2110 error = got_blame(in_repo_path, commit_id, repo, stdout);
2111 done:
2112 free(in_repo_path);
2113 free(repo_path);
2114 free(cwd);
2115 free(commit_id);
2116 if (worktree)
2117 got_worktree_close(worktree);
2118 if (repo) {
2119 const struct got_error *repo_error;
2120 repo_error = got_repo_close(repo);
2121 if (error == NULL)
2122 error = repo_error;
2124 return error;
2127 __dead static void
2128 usage_tree(void)
2130 fprintf(stderr,
2131 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2132 getprogname());
2133 exit(1);
2136 static void
2137 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2138 const char *root_path)
2140 int is_root_path = (strcmp(path, root_path) == 0);
2142 path += strlen(root_path);
2143 while (path[0] == '/')
2144 path++;
2146 printf("%s%s%s%s%s\n", id ? id : "", path,
2147 is_root_path ? "" : "/", te->name,
2148 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2151 static const struct got_error *
2152 print_tree(const char *path, struct got_object_id *commit_id,
2153 int show_ids, int recurse, const char *root_path,
2154 struct got_repository *repo)
2156 const struct got_error *err = NULL;
2157 struct got_object_id *tree_id = NULL;
2158 struct got_tree_object *tree = NULL;
2159 const struct got_tree_entries *entries;
2160 struct got_tree_entry *te;
2162 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2163 if (err)
2164 goto done;
2166 err = got_object_open_as_tree(&tree, repo, tree_id);
2167 if (err)
2168 goto done;
2169 entries = got_object_tree_get_entries(tree);
2170 te = SIMPLEQ_FIRST(&entries->head);
2171 while (te) {
2172 char *id = NULL;
2174 if (sigint_received || sigpipe_received)
2175 break;
2177 if (show_ids) {
2178 char *id_str;
2179 err = got_object_id_str(&id_str, te->id);
2180 if (err)
2181 goto done;
2182 if (asprintf(&id, "%s ", id_str) == -1) {
2183 err = got_error_from_errno("asprintf");
2184 free(id_str);
2185 goto done;
2187 free(id_str);
2189 print_entry(te, id, path, root_path);
2190 free(id);
2192 if (recurse && S_ISDIR(te->mode)) {
2193 char *child_path;
2194 if (asprintf(&child_path, "%s%s%s", path,
2195 path[0] == '/' && path[1] == '\0' ? "" : "/",
2196 te->name) == -1) {
2197 err = got_error_from_errno("asprintf");
2198 goto done;
2200 err = print_tree(child_path, commit_id, show_ids, 1,
2201 root_path, repo);
2202 free(child_path);
2203 if (err)
2204 goto done;
2207 te = SIMPLEQ_NEXT(te, entry);
2209 done:
2210 if (tree)
2211 got_object_tree_close(tree);
2212 free(tree_id);
2213 return err;
2216 static const struct got_error *
2217 cmd_tree(int argc, char *argv[])
2219 const struct got_error *error;
2220 struct got_repository *repo = NULL;
2221 struct got_worktree *worktree = NULL;
2222 const char *path;
2223 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2224 struct got_object_id *commit_id = NULL;
2225 char *commit_id_str = NULL;
2226 int show_ids = 0, recurse = 0;
2227 int ch;
2229 #ifndef PROFILE
2230 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2231 NULL) == -1)
2232 err(1, "pledge");
2233 #endif
2235 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2236 switch (ch) {
2237 case 'c':
2238 commit_id_str = optarg;
2239 break;
2240 case 'r':
2241 repo_path = realpath(optarg, NULL);
2242 if (repo_path == NULL)
2243 err(1, "-r option");
2244 got_path_strip_trailing_slashes(repo_path);
2245 break;
2246 case 'i':
2247 show_ids = 1;
2248 break;
2249 case 'R':
2250 recurse = 1;
2251 break;
2252 default:
2253 usage_tree();
2254 /* NOTREACHED */
2258 argc -= optind;
2259 argv += optind;
2261 if (argc == 1)
2262 path = argv[0];
2263 else if (argc > 1)
2264 usage_tree();
2265 else
2266 path = NULL;
2268 cwd = getcwd(NULL, 0);
2269 if (cwd == NULL) {
2270 error = got_error_from_errno("getcwd");
2271 goto done;
2273 if (repo_path == NULL) {
2274 error = got_worktree_open(&worktree, cwd);
2275 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2276 goto done;
2277 else
2278 error = NULL;
2279 if (worktree) {
2280 repo_path =
2281 strdup(got_worktree_get_repo_path(worktree));
2282 if (repo_path == NULL)
2283 error = got_error_from_errno("strdup");
2284 if (error)
2285 goto done;
2286 } else {
2287 repo_path = strdup(cwd);
2288 if (repo_path == NULL) {
2289 error = got_error_from_errno("strdup");
2290 goto done;
2295 error = got_repo_open(&repo, repo_path);
2296 if (error != NULL)
2297 goto done;
2299 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2300 if (error)
2301 goto done;
2303 if (path == NULL) {
2304 if (worktree) {
2305 char *p, *worktree_subdir = cwd +
2306 strlen(got_worktree_get_root_path(worktree));
2307 if (asprintf(&p, "%s/%s",
2308 got_worktree_get_path_prefix(worktree),
2309 worktree_subdir) == -1) {
2310 error = got_error_from_errno("asprintf");
2311 goto done;
2313 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2314 free(p);
2315 if (error)
2316 goto done;
2317 } else
2318 path = "/";
2320 if (in_repo_path == NULL) {
2321 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2322 if (error != NULL)
2323 goto done;
2326 if (commit_id_str == NULL) {
2327 struct got_reference *head_ref;
2328 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2329 if (error != NULL)
2330 goto done;
2331 error = got_ref_resolve(&commit_id, repo, head_ref);
2332 got_ref_close(head_ref);
2333 if (error != NULL)
2334 goto done;
2335 } else {
2336 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2337 if (error)
2338 goto done;
2341 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2342 in_repo_path, repo);
2343 done:
2344 free(in_repo_path);
2345 free(repo_path);
2346 free(cwd);
2347 free(commit_id);
2348 if (worktree)
2349 got_worktree_close(worktree);
2350 if (repo) {
2351 const struct got_error *repo_error;
2352 repo_error = got_repo_close(repo);
2353 if (error == NULL)
2354 error = repo_error;
2356 return error;
2359 __dead static void
2360 usage_status(void)
2362 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2363 exit(1);
2366 static const struct got_error *
2367 print_status(void *arg, unsigned char status, unsigned char staged_status,
2368 const char *path, struct got_object_id *blob_id,
2369 struct got_object_id *commit_id)
2371 printf("%c%c %s\n", status, staged_status, path);
2372 return NULL;
2375 static const struct got_error *
2376 cmd_status(int argc, char *argv[])
2378 const struct got_error *error = NULL;
2379 struct got_repository *repo = NULL;
2380 struct got_worktree *worktree = NULL;
2381 char *cwd = NULL;
2382 struct got_pathlist_head paths;
2383 struct got_pathlist_entry *pe;
2384 int ch;
2386 TAILQ_INIT(&paths);
2388 while ((ch = getopt(argc, argv, "")) != -1) {
2389 switch (ch) {
2390 default:
2391 usage_status();
2392 /* NOTREACHED */
2396 argc -= optind;
2397 argv += optind;
2399 #ifndef PROFILE
2400 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2401 NULL) == -1)
2402 err(1, "pledge");
2403 #endif
2404 cwd = getcwd(NULL, 0);
2405 if (cwd == NULL) {
2406 error = got_error_from_errno("getcwd");
2407 goto done;
2410 error = got_worktree_open(&worktree, cwd);
2411 if (error != NULL)
2412 goto done;
2414 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2415 if (error)
2416 goto done;
2418 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2419 if (error != NULL)
2420 goto done;
2422 error = apply_unveil(got_repo_get_path(repo), 1,
2423 got_worktree_get_root_path(worktree));
2424 if (error)
2425 goto done;
2427 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2428 check_cancelled, NULL);
2429 done:
2430 TAILQ_FOREACH(pe, &paths, entry)
2431 free((char *)pe->path);
2432 got_pathlist_free(&paths);
2433 free(cwd);
2434 return error;
2437 __dead static void
2438 usage_ref(void)
2440 fprintf(stderr,
2441 "usage: %s ref [-r repository] -l | -d name | name target\n",
2442 getprogname());
2443 exit(1);
2446 static const struct got_error *
2447 list_refs(struct got_repository *repo)
2449 static const struct got_error *err = NULL;
2450 struct got_reflist_head refs;
2451 struct got_reflist_entry *re;
2453 SIMPLEQ_INIT(&refs);
2454 err = got_ref_list(&refs, repo);
2455 if (err)
2456 return err;
2458 SIMPLEQ_FOREACH(re, &refs, entry) {
2459 char *refstr;
2460 refstr = got_ref_to_str(re->ref);
2461 if (refstr == NULL)
2462 return got_error_from_errno("got_ref_to_str");
2463 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2464 free(refstr);
2467 got_ref_list_free(&refs);
2468 return NULL;
2471 static const struct got_error *
2472 delete_ref(struct got_repository *repo, const char *refname)
2474 const struct got_error *err = NULL;
2475 struct got_reference *ref;
2477 err = got_ref_open(&ref, repo, refname, 0);
2478 if (err)
2479 return err;
2481 err = got_ref_delete(ref, repo);
2482 got_ref_close(ref);
2483 return err;
2486 static const struct got_error *
2487 add_ref(struct got_repository *repo, const char *refname, const char *target)
2489 const struct got_error *err = NULL;
2490 struct got_object_id *id;
2491 struct got_reference *ref = NULL;
2494 * Don't let the user create a reference named '-'.
2495 * While technically a valid reference name, this case is usually
2496 * an unintended typo.
2498 if (refname[0] == '-' && refname[1] == '\0')
2499 return got_error(GOT_ERR_BAD_REF_NAME);
2501 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2502 repo);
2503 if (err) {
2504 struct got_reference *target_ref;
2506 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2507 return err;
2508 err = got_ref_open(&target_ref, repo, target, 0);
2509 if (err)
2510 return err;
2511 err = got_ref_resolve(&id, repo, target_ref);
2512 got_ref_close(target_ref);
2513 if (err)
2514 return err;
2517 err = got_ref_alloc(&ref, refname, id);
2518 if (err)
2519 goto done;
2521 err = got_ref_write(ref, repo);
2522 done:
2523 if (ref)
2524 got_ref_close(ref);
2525 free(id);
2526 return err;
2529 static const struct got_error *
2530 cmd_ref(int argc, char *argv[])
2532 const struct got_error *error = NULL;
2533 struct got_repository *repo = NULL;
2534 struct got_worktree *worktree = NULL;
2535 char *cwd = NULL, *repo_path = NULL;
2536 int ch, do_list = 0;
2537 const char *delref = NULL;
2539 /* TODO: Add -s option for adding symbolic references. */
2540 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2541 switch (ch) {
2542 case 'd':
2543 delref = optarg;
2544 break;
2545 case 'r':
2546 repo_path = realpath(optarg, NULL);
2547 if (repo_path == NULL)
2548 err(1, "-r option");
2549 got_path_strip_trailing_slashes(repo_path);
2550 break;
2551 case 'l':
2552 do_list = 1;
2553 break;
2554 default:
2555 usage_ref();
2556 /* NOTREACHED */
2560 if (do_list && delref)
2561 errx(1, "-l and -d options are mutually exclusive\n");
2563 argc -= optind;
2564 argv += optind;
2566 if (do_list || delref) {
2567 if (argc > 0)
2568 usage_ref();
2569 } else if (argc != 2)
2570 usage_ref();
2572 #ifndef PROFILE
2573 if (do_list) {
2574 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2575 NULL) == -1)
2576 err(1, "pledge");
2577 } else {
2578 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2579 "sendfd unveil", NULL) == -1)
2580 err(1, "pledge");
2582 #endif
2583 cwd = getcwd(NULL, 0);
2584 if (cwd == NULL) {
2585 error = got_error_from_errno("getcwd");
2586 goto done;
2589 if (repo_path == NULL) {
2590 error = got_worktree_open(&worktree, cwd);
2591 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2592 goto done;
2593 else
2594 error = NULL;
2595 if (worktree) {
2596 repo_path =
2597 strdup(got_worktree_get_repo_path(worktree));
2598 if (repo_path == NULL)
2599 error = got_error_from_errno("strdup");
2600 if (error)
2601 goto done;
2602 } else {
2603 repo_path = strdup(cwd);
2604 if (repo_path == NULL) {
2605 error = got_error_from_errno("strdup");
2606 goto done;
2611 error = got_repo_open(&repo, repo_path);
2612 if (error != NULL)
2613 goto done;
2615 error = apply_unveil(got_repo_get_path(repo), do_list,
2616 worktree ? got_worktree_get_root_path(worktree) : NULL);
2617 if (error)
2618 goto done;
2620 if (do_list)
2621 error = list_refs(repo);
2622 else if (delref)
2623 error = delete_ref(repo, delref);
2624 else
2625 error = add_ref(repo, argv[0], argv[1]);
2626 done:
2627 if (repo)
2628 got_repo_close(repo);
2629 if (worktree)
2630 got_worktree_close(worktree);
2631 free(cwd);
2632 free(repo_path);
2633 return error;
2636 __dead static void
2637 usage_branch(void)
2639 fprintf(stderr,
2640 "usage: %s branch [-r repository] -l | -d name | "
2641 "name [base-branch]\n", getprogname());
2642 exit(1);
2645 static const struct got_error *
2646 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2648 static const struct got_error *err = NULL;
2649 struct got_reflist_head refs;
2650 struct got_reflist_entry *re;
2652 SIMPLEQ_INIT(&refs);
2654 err = got_ref_list(&refs, repo);
2655 if (err)
2656 return err;
2658 SIMPLEQ_FOREACH(re, &refs, entry) {
2659 const char *refname, *marker = " ";
2660 char *refstr;
2661 refname = got_ref_get_name(re->ref);
2662 if (strncmp(refname, "refs/heads/", 11) != 0)
2663 continue;
2664 if (worktree && strcmp(refname,
2665 got_worktree_get_head_ref_name(worktree)) == 0) {
2666 struct got_object_id *id = NULL;
2667 err = got_ref_resolve(&id, repo, re->ref);
2668 if (err)
2669 return err;
2670 if (got_object_id_cmp(id,
2671 got_worktree_get_base_commit_id(worktree)) == 0)
2672 marker = "* ";
2673 else
2674 marker = "~ ";
2675 free(id);
2677 refname += 11;
2678 refstr = got_ref_to_str(re->ref);
2679 if (refstr == NULL)
2680 return got_error_from_errno("got_ref_to_str");
2681 printf("%s%s: %s\n", marker, refname, refstr);
2682 free(refstr);
2685 got_ref_list_free(&refs);
2686 return NULL;
2689 static const struct got_error *
2690 delete_branch(struct got_repository *repo, const char *branch_name)
2692 const struct got_error *err = NULL;
2693 struct got_reference *ref;
2694 char *refname;
2696 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2697 return got_error_from_errno("asprintf");
2699 err = got_ref_open(&ref, repo, refname, 0);
2700 if (err)
2701 goto done;
2703 err = got_ref_delete(ref, repo);
2704 got_ref_close(ref);
2705 done:
2706 free(refname);
2707 return err;
2710 static const struct got_error *
2711 add_branch(struct got_repository *repo, const char *branch_name,
2712 const char *base_branch)
2714 const struct got_error *err = NULL;
2715 struct got_object_id *id = NULL;
2716 struct got_reference *ref = NULL;
2717 char *base_refname = NULL, *refname = NULL;
2718 struct got_reference *base_ref;
2721 * Don't let the user create a branch named '-'.
2722 * While technically a valid reference name, this case is usually
2723 * an unintended typo.
2725 if (branch_name[0] == '-' && branch_name[1] == '\0')
2726 return got_error(GOT_ERR_BAD_REF_NAME);
2728 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2729 base_refname = strdup(GOT_REF_HEAD);
2730 if (base_refname == NULL)
2731 return got_error_from_errno("strdup");
2732 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2733 return got_error_from_errno("asprintf");
2735 err = got_ref_open(&base_ref, repo, base_refname, 0);
2736 if (err)
2737 goto done;
2738 err = got_ref_resolve(&id, repo, base_ref);
2739 got_ref_close(base_ref);
2740 if (err)
2741 goto done;
2743 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2744 err = got_error_from_errno("asprintf");
2745 goto done;
2748 err = got_ref_open(&ref, repo, refname, 0);
2749 if (err == NULL) {
2750 err = got_error(GOT_ERR_BRANCH_EXISTS);
2751 goto done;
2752 } else if (err->code != GOT_ERR_NOT_REF)
2753 goto done;
2755 err = got_ref_alloc(&ref, refname, id);
2756 if (err)
2757 goto done;
2759 err = got_ref_write(ref, repo);
2760 done:
2761 if (ref)
2762 got_ref_close(ref);
2763 free(id);
2764 free(base_refname);
2765 free(refname);
2766 return err;
2769 static const struct got_error *
2770 cmd_branch(int argc, char *argv[])
2772 const struct got_error *error = NULL;
2773 struct got_repository *repo = NULL;
2774 struct got_worktree *worktree = NULL;
2775 char *cwd = NULL, *repo_path = NULL;
2776 int ch, do_list = 0;
2777 const char *delref = NULL;
2779 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2780 switch (ch) {
2781 case 'd':
2782 delref = optarg;
2783 break;
2784 case 'r':
2785 repo_path = realpath(optarg, NULL);
2786 if (repo_path == NULL)
2787 err(1, "-r option");
2788 got_path_strip_trailing_slashes(repo_path);
2789 break;
2790 case 'l':
2791 do_list = 1;
2792 break;
2793 default:
2794 usage_branch();
2795 /* NOTREACHED */
2799 if (do_list && delref)
2800 errx(1, "-l and -d options are mutually exclusive\n");
2802 argc -= optind;
2803 argv += optind;
2805 if (do_list || delref) {
2806 if (argc > 0)
2807 usage_branch();
2808 } else if (argc < 1 || argc > 2)
2809 usage_branch();
2811 #ifndef PROFILE
2812 if (do_list) {
2813 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2814 NULL) == -1)
2815 err(1, "pledge");
2816 } else {
2817 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2818 "sendfd unveil", NULL) == -1)
2819 err(1, "pledge");
2821 #endif
2822 cwd = getcwd(NULL, 0);
2823 if (cwd == NULL) {
2824 error = got_error_from_errno("getcwd");
2825 goto done;
2828 if (repo_path == NULL) {
2829 error = got_worktree_open(&worktree, cwd);
2830 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2831 goto done;
2832 else
2833 error = NULL;
2834 if (worktree) {
2835 repo_path =
2836 strdup(got_worktree_get_repo_path(worktree));
2837 if (repo_path == NULL)
2838 error = got_error_from_errno("strdup");
2839 if (error)
2840 goto done;
2841 } else {
2842 repo_path = strdup(cwd);
2843 if (repo_path == NULL) {
2844 error = got_error_from_errno("strdup");
2845 goto done;
2850 error = got_repo_open(&repo, repo_path);
2851 if (error != NULL)
2852 goto done;
2854 error = apply_unveil(got_repo_get_path(repo), do_list,
2855 worktree ? got_worktree_get_root_path(worktree) : NULL);
2856 if (error)
2857 goto done;
2859 if (do_list)
2860 error = list_branches(repo, worktree);
2861 else if (delref)
2862 error = delete_branch(repo, delref);
2863 else {
2864 const char *base_branch;
2865 if (argc == 1) {
2866 base_branch = worktree ?
2867 got_worktree_get_head_ref_name(worktree) :
2868 GOT_REF_HEAD;
2869 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2870 base_branch += 11;
2871 } else
2872 base_branch = argv[1];
2873 error = add_branch(repo, argv[0], base_branch);
2875 done:
2876 if (repo)
2877 got_repo_close(repo);
2878 if (worktree)
2879 got_worktree_close(worktree);
2880 free(cwd);
2881 free(repo_path);
2882 return error;
2885 __dead static void
2886 usage_add(void)
2888 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2889 exit(1);
2892 static const struct got_error *
2893 cmd_add(int argc, char *argv[])
2895 const struct got_error *error = NULL;
2896 struct got_repository *repo = NULL;
2897 struct got_worktree *worktree = NULL;
2898 char *cwd = NULL;
2899 struct got_pathlist_head paths;
2900 struct got_pathlist_entry *pe;
2901 int ch, x;
2903 TAILQ_INIT(&paths);
2905 while ((ch = getopt(argc, argv, "")) != -1) {
2906 switch (ch) {
2907 default:
2908 usage_add();
2909 /* NOTREACHED */
2913 argc -= optind;
2914 argv += optind;
2916 #ifndef PROFILE
2917 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2918 NULL) == -1)
2919 err(1, "pledge");
2920 #endif
2921 if (argc < 1)
2922 usage_add();
2924 cwd = getcwd(NULL, 0);
2925 if (cwd == NULL) {
2926 error = got_error_from_errno("getcwd");
2927 goto done;
2930 error = got_worktree_open(&worktree, cwd);
2931 if (error)
2932 goto done;
2934 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2935 if (error != NULL)
2936 goto done;
2938 error = apply_unveil(got_repo_get_path(repo), 1,
2939 got_worktree_get_root_path(worktree));
2940 if (error)
2941 goto done;
2943 for (x = 0; x < argc; x++) {
2944 char *path = realpath(argv[x], NULL);
2945 if (path == NULL) {
2946 error = got_error_from_errno2("realpath", argv[x]);
2947 goto done;
2950 got_path_strip_trailing_slashes(path);
2951 error = got_pathlist_insert(&pe, &paths, path, NULL);
2952 if (error) {
2953 free(path);
2954 goto done;
2957 error = got_worktree_schedule_add(worktree, &paths, print_status,
2958 NULL, repo);
2959 done:
2960 if (repo)
2961 got_repo_close(repo);
2962 if (worktree)
2963 got_worktree_close(worktree);
2964 TAILQ_FOREACH(pe, &paths, entry)
2965 free((char *)pe->path);
2966 got_pathlist_free(&paths);
2967 free(cwd);
2968 return error;
2971 __dead static void
2972 usage_remove(void)
2974 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2975 exit(1);
2978 static const struct got_error *
2979 cmd_remove(int argc, char *argv[])
2981 const struct got_error *error = NULL;
2982 struct got_worktree *worktree = NULL;
2983 struct got_repository *repo = NULL;
2984 char *cwd = NULL;
2985 struct got_pathlist_head paths;
2986 struct got_pathlist_entry *pe;
2987 int ch, i, delete_local_mods = 0;
2989 TAILQ_INIT(&paths);
2991 while ((ch = getopt(argc, argv, "f")) != -1) {
2992 switch (ch) {
2993 case 'f':
2994 delete_local_mods = 1;
2995 break;
2996 default:
2997 usage_add();
2998 /* NOTREACHED */
3002 argc -= optind;
3003 argv += optind;
3005 #ifndef PROFILE
3006 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3007 NULL) == -1)
3008 err(1, "pledge");
3009 #endif
3010 if (argc < 1)
3011 usage_remove();
3013 cwd = getcwd(NULL, 0);
3014 if (cwd == NULL) {
3015 error = got_error_from_errno("getcwd");
3016 goto done;
3018 error = got_worktree_open(&worktree, cwd);
3019 if (error)
3020 goto done;
3022 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3023 if (error)
3024 goto done;
3026 error = apply_unveil(got_repo_get_path(repo), 1,
3027 got_worktree_get_root_path(worktree));
3028 if (error)
3029 goto done;
3031 for (i = 0; i < argc; i++) {
3032 char *path = realpath(argv[i], NULL);
3033 if (path == NULL) {
3034 error = got_error_from_errno2("realpath", argv[i]);
3035 goto done;
3038 got_path_strip_trailing_slashes(path);
3039 error = got_pathlist_insert(&pe, &paths, path, NULL);
3040 if (error) {
3041 free(path);
3042 goto done;
3045 error = got_worktree_schedule_delete(worktree, &paths,
3046 delete_local_mods, print_status, NULL, repo);
3047 if (error)
3048 goto done;
3049 done:
3050 if (repo)
3051 got_repo_close(repo);
3052 if (worktree)
3053 got_worktree_close(worktree);
3054 TAILQ_FOREACH(pe, &paths, entry)
3055 free((char *)pe->path);
3056 got_pathlist_free(&paths);
3057 free(cwd);
3058 return error;
3061 __dead static void
3062 usage_revert(void)
3064 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3065 exit(1);
3068 static const struct got_error *
3069 revert_progress(void *arg, unsigned char status, const char *path)
3071 while (path[0] == '/')
3072 path++;
3073 printf("%c %s\n", status, path);
3074 return NULL;
3077 static const struct got_error *
3078 cmd_revert(int argc, char *argv[])
3080 const struct got_error *error = NULL;
3081 struct got_worktree *worktree = NULL;
3082 struct got_repository *repo = NULL;
3083 char *cwd = NULL, *path = NULL;
3084 struct got_pathlist_head paths;
3085 struct got_pathlist_entry *pe;
3086 const char *worktree_path;
3087 int ch, i;
3089 TAILQ_INIT(&paths);
3091 while ((ch = getopt(argc, argv, "")) != -1) {
3092 switch (ch) {
3093 default:
3094 usage_revert();
3095 /* NOTREACHED */
3099 argc -= optind;
3100 argv += optind;
3102 #ifndef PROFILE
3103 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3104 "unveil", NULL) == -1)
3105 err(1, "pledge");
3106 #endif
3107 if (argc < 1)
3108 usage_revert();
3110 cwd = getcwd(NULL, 0);
3111 if (cwd == NULL) {
3112 error = got_error_from_errno("getcwd");
3113 goto done;
3115 error = got_worktree_open(&worktree, cwd);
3116 if (error)
3117 goto done;
3119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3120 if (error != NULL)
3121 goto done;
3123 error = apply_unveil(got_repo_get_path(repo), 1,
3124 got_worktree_get_root_path(worktree));
3125 if (error)
3126 goto done;
3128 worktree_path = got_worktree_get_root_path(worktree);
3129 for (i = 0; i < argc; i++) {
3130 char *path = realpath(argv[i], NULL);
3131 if (path == NULL) {
3132 if (errno != ENOENT) {
3133 error = got_error_from_errno2("realpath",
3134 argv[i]);
3135 goto done;
3137 if (got_path_is_child(argv[i], worktree_path,
3138 strlen(worktree_path))) {
3139 path = strdup(argv[i]);
3140 if (path == NULL) {
3141 error = got_error_from_errno("strdup");
3142 goto done;
3145 } else if (asprintf(&path, "%s/%s",
3146 got_worktree_get_root_path(worktree),
3147 argv[i]) == -1) {
3148 error = got_error_from_errno("asprintf");
3149 goto done;
3153 got_path_strip_trailing_slashes(path);
3154 error = got_pathlist_insert(&pe, &paths, path, NULL);
3155 if (error) {
3156 free(path);
3157 goto done;
3161 error = got_worktree_revert(worktree, &paths,
3162 revert_progress, NULL, repo);
3163 if (error)
3164 goto done;
3165 done:
3166 if (repo)
3167 got_repo_close(repo);
3168 if (worktree)
3169 got_worktree_close(worktree);
3170 free(path);
3171 free(cwd);
3172 return error;
3175 __dead static void
3176 usage_commit(void)
3178 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3179 getprogname());
3180 exit(1);
3183 struct collect_commit_logmsg_arg {
3184 const char *cmdline_log;
3185 const char *editor;
3186 const char *worktree_path;
3187 const char *branch_name;
3188 const char *repo_path;
3189 char *logmsg_path;
3193 static const struct got_error *
3194 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3195 void *arg)
3197 char *initial_content = NULL;
3198 struct got_pathlist_entry *pe;
3199 const struct got_error *err = NULL;
3200 char *template = NULL;
3201 struct collect_commit_logmsg_arg *a = arg;
3202 int fd;
3203 size_t len;
3205 /* if a message was specified on the command line, just use it */
3206 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3207 len = strlen(a->cmdline_log) + 1;
3208 *logmsg = malloc(len + 1);
3209 if (*logmsg == NULL)
3210 return got_error_from_errno("malloc");
3211 strlcpy(*logmsg, a->cmdline_log, len);
3212 return NULL;
3215 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3216 return got_error_from_errno("asprintf");
3218 if (asprintf(&initial_content,
3219 "\n# changes to be committed on branch %s:\n",
3220 a->branch_name) == -1)
3221 return got_error_from_errno("asprintf");
3223 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3224 if (err)
3225 goto done;
3227 dprintf(fd, initial_content);
3229 TAILQ_FOREACH(pe, commitable_paths, entry) {
3230 struct got_commitable *ct = pe->data;
3231 dprintf(fd, "# %c %s\n",
3232 got_commitable_get_status(ct),
3233 got_commitable_get_path(ct));
3235 close(fd);
3237 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3238 done:
3239 unlink(a->logmsg_path);
3240 free(a->logmsg_path);
3241 free(initial_content);
3242 free(template);
3244 /* Editor is done; we can now apply unveil(2) */
3245 if (err == NULL) {
3246 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3247 if (err) {
3248 free(*logmsg);
3249 *logmsg = NULL;
3252 return err;
3255 static const struct got_error *
3256 cmd_commit(int argc, char *argv[])
3258 const struct got_error *error = NULL;
3259 struct got_worktree *worktree = NULL;
3260 struct got_repository *repo = NULL;
3261 char *cwd = NULL, *id_str = NULL;
3262 struct got_object_id *id = NULL;
3263 const char *logmsg = NULL;
3264 const char *got_author = getenv("GOT_AUTHOR");
3265 struct collect_commit_logmsg_arg cl_arg;
3266 char *editor = NULL;
3267 int ch, rebase_in_progress, histedit_in_progress;
3268 struct got_pathlist_head paths;
3270 TAILQ_INIT(&paths);
3272 while ((ch = getopt(argc, argv, "m:")) != -1) {
3273 switch (ch) {
3274 case 'm':
3275 logmsg = optarg;
3276 break;
3277 default:
3278 usage_commit();
3279 /* NOTREACHED */
3283 argc -= optind;
3284 argv += optind;
3286 #ifndef PROFILE
3287 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3288 "unveil", NULL) == -1)
3289 err(1, "pledge");
3290 #endif
3291 if (got_author == NULL) {
3292 /* TODO: Look current user up in password database */
3293 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3294 goto done;
3297 cwd = getcwd(NULL, 0);
3298 if (cwd == NULL) {
3299 error = got_error_from_errno("getcwd");
3300 goto done;
3302 error = got_worktree_open(&worktree, cwd);
3303 if (error)
3304 goto done;
3306 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3307 if (error)
3308 goto done;
3309 if (rebase_in_progress) {
3310 error = got_error(GOT_ERR_REBASING);
3311 goto done;
3314 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3315 worktree);
3316 if (error)
3317 goto done;
3319 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3320 if (error)
3321 goto done;
3323 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3324 if (error != NULL)
3325 goto done;
3328 * unveil(2) traverses exec(2); if an editor is used we have
3329 * to apply unveil after the log message has been written.
3331 if (logmsg == NULL || strlen(logmsg) == 0)
3332 error = get_editor(&editor);
3333 else
3334 error = apply_unveil(got_repo_get_path(repo), 0,
3335 got_worktree_get_root_path(worktree));
3336 if (error)
3337 goto done;
3339 cl_arg.editor = editor;
3340 cl_arg.cmdline_log = logmsg;
3341 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3342 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3343 if (!histedit_in_progress) {
3344 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3345 error = got_error(GOT_ERR_COMMIT_BRANCH);
3346 goto done;
3348 cl_arg.branch_name += 11;
3350 cl_arg.repo_path = got_repo_get_path(repo);
3351 cl_arg.logmsg_path = NULL;
3352 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3353 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3354 if (error) {
3355 if (cl_arg.logmsg_path)
3356 fprintf(stderr, "%s: log message preserved in %s\n",
3357 getprogname(), cl_arg.logmsg_path);
3358 goto done;
3361 if (cl_arg.logmsg_path)
3362 unlink(cl_arg.logmsg_path);
3364 error = got_object_id_str(&id_str, id);
3365 if (error)
3366 goto done;
3367 printf("Created commit %s\n", id_str);
3368 done:
3369 if (repo)
3370 got_repo_close(repo);
3371 if (worktree)
3372 got_worktree_close(worktree);
3373 free(cwd);
3374 free(id_str);
3375 free(editor);
3376 return error;
3379 __dead static void
3380 usage_cherrypick(void)
3382 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3383 exit(1);
3386 static const struct got_error *
3387 cmd_cherrypick(int argc, char *argv[])
3389 const struct got_error *error = NULL;
3390 struct got_worktree *worktree = NULL;
3391 struct got_repository *repo = NULL;
3392 char *cwd = NULL, *commit_id_str = NULL;
3393 struct got_object_id *commit_id = NULL;
3394 struct got_commit_object *commit = NULL;
3395 struct got_object_qid *pid;
3396 struct got_reference *head_ref = NULL;
3397 int ch, did_something = 0;
3399 while ((ch = getopt(argc, argv, "")) != -1) {
3400 switch (ch) {
3401 default:
3402 usage_cherrypick();
3403 /* NOTREACHED */
3407 argc -= optind;
3408 argv += optind;
3410 #ifndef PROFILE
3411 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3412 "unveil", NULL) == -1)
3413 err(1, "pledge");
3414 #endif
3415 if (argc != 1)
3416 usage_cherrypick();
3418 cwd = getcwd(NULL, 0);
3419 if (cwd == NULL) {
3420 error = got_error_from_errno("getcwd");
3421 goto done;
3423 error = got_worktree_open(&worktree, cwd);
3424 if (error)
3425 goto done;
3427 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3428 if (error != NULL)
3429 goto done;
3431 error = apply_unveil(got_repo_get_path(repo), 0,
3432 got_worktree_get_root_path(worktree));
3433 if (error)
3434 goto done;
3436 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3437 GOT_OBJ_TYPE_COMMIT, repo);
3438 if (error != NULL) {
3439 struct got_reference *ref;
3440 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3441 goto done;
3442 error = got_ref_open(&ref, repo, argv[0], 0);
3443 if (error != NULL)
3444 goto done;
3445 error = got_ref_resolve(&commit_id, repo, ref);
3446 got_ref_close(ref);
3447 if (error != NULL)
3448 goto done;
3450 error = got_object_id_str(&commit_id_str, commit_id);
3451 if (error)
3452 goto done;
3454 error = got_ref_open(&head_ref, repo,
3455 got_worktree_get_head_ref_name(worktree), 0);
3456 if (error != NULL)
3457 goto done;
3459 error = check_same_branch(commit_id, head_ref, NULL, repo);
3460 if (error) {
3461 if (error->code != GOT_ERR_ANCESTRY)
3462 goto done;
3463 error = NULL;
3464 } else {
3465 error = got_error(GOT_ERR_SAME_BRANCH);
3466 goto done;
3469 error = got_object_open_as_commit(&commit, repo, commit_id);
3470 if (error)
3471 goto done;
3472 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3473 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3474 commit_id, repo, update_progress, &did_something, check_cancelled,
3475 NULL);
3476 if (error != NULL)
3477 goto done;
3479 if (did_something)
3480 printf("Merged commit %s\n", commit_id_str);
3481 done:
3482 if (commit)
3483 got_object_commit_close(commit);
3484 free(commit_id_str);
3485 if (head_ref)
3486 got_ref_close(head_ref);
3487 if (worktree)
3488 got_worktree_close(worktree);
3489 if (repo)
3490 got_repo_close(repo);
3491 return error;
3494 __dead static void
3495 usage_backout(void)
3497 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3498 exit(1);
3501 static const struct got_error *
3502 cmd_backout(int argc, char *argv[])
3504 const struct got_error *error = NULL;
3505 struct got_worktree *worktree = NULL;
3506 struct got_repository *repo = NULL;
3507 char *cwd = NULL, *commit_id_str = NULL;
3508 struct got_object_id *commit_id = NULL;
3509 struct got_commit_object *commit = NULL;
3510 struct got_object_qid *pid;
3511 struct got_reference *head_ref = NULL;
3512 int ch, did_something = 0;
3514 while ((ch = getopt(argc, argv, "")) != -1) {
3515 switch (ch) {
3516 default:
3517 usage_backout();
3518 /* NOTREACHED */
3522 argc -= optind;
3523 argv += optind;
3525 #ifndef PROFILE
3526 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3527 "unveil", NULL) == -1)
3528 err(1, "pledge");
3529 #endif
3530 if (argc != 1)
3531 usage_backout();
3533 cwd = getcwd(NULL, 0);
3534 if (cwd == NULL) {
3535 error = got_error_from_errno("getcwd");
3536 goto done;
3538 error = got_worktree_open(&worktree, cwd);
3539 if (error)
3540 goto done;
3542 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3543 if (error != NULL)
3544 goto done;
3546 error = apply_unveil(got_repo_get_path(repo), 0,
3547 got_worktree_get_root_path(worktree));
3548 if (error)
3549 goto done;
3551 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3552 GOT_OBJ_TYPE_COMMIT, repo);
3553 if (error != NULL) {
3554 struct got_reference *ref;
3555 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3556 goto done;
3557 error = got_ref_open(&ref, repo, argv[0], 0);
3558 if (error != NULL)
3559 goto done;
3560 error = got_ref_resolve(&commit_id, repo, ref);
3561 got_ref_close(ref);
3562 if (error != NULL)
3563 goto done;
3565 error = got_object_id_str(&commit_id_str, commit_id);
3566 if (error)
3567 goto done;
3569 error = got_ref_open(&head_ref, repo,
3570 got_worktree_get_head_ref_name(worktree), 0);
3571 if (error != NULL)
3572 goto done;
3574 error = check_same_branch(commit_id, head_ref, NULL, repo);
3575 if (error)
3576 goto done;
3578 error = got_object_open_as_commit(&commit, repo, commit_id);
3579 if (error)
3580 goto done;
3581 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3582 if (pid == NULL) {
3583 error = got_error(GOT_ERR_ROOT_COMMIT);
3584 goto done;
3587 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3588 update_progress, &did_something, check_cancelled, NULL);
3589 if (error != NULL)
3590 goto done;
3592 if (did_something)
3593 printf("Backed out commit %s\n", commit_id_str);
3594 done:
3595 if (commit)
3596 got_object_commit_close(commit);
3597 free(commit_id_str);
3598 if (head_ref)
3599 got_ref_close(head_ref);
3600 if (worktree)
3601 got_worktree_close(worktree);
3602 if (repo)
3603 got_repo_close(repo);
3604 return error;
3607 __dead static void
3608 usage_rebase(void)
3610 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3611 getprogname());
3612 exit(1);
3615 void
3616 trim_logmsg(char *logmsg, int limit)
3618 char *nl;
3619 size_t len;
3621 len = strlen(logmsg);
3622 if (len > limit)
3623 len = limit;
3624 logmsg[len] = '\0';
3625 nl = strchr(logmsg, '\n');
3626 if (nl)
3627 *nl = '\0';
3630 static const struct got_error *
3631 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3633 const char *logmsg0 = NULL;
3635 logmsg0 = got_object_commit_get_logmsg(commit);
3637 while (isspace((unsigned char)logmsg0[0]))
3638 logmsg0++;
3640 *logmsg = strdup(logmsg0);
3641 if (*logmsg == NULL)
3642 return got_error_from_errno("strdup");
3644 trim_logmsg(*logmsg, limit);
3645 return NULL;
3648 static const struct got_error *
3649 show_rebase_progress(struct got_commit_object *commit,
3650 struct got_object_id *old_id, struct got_object_id *new_id)
3652 const struct got_error *err;
3653 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3655 err = got_object_id_str(&old_id_str, old_id);
3656 if (err)
3657 goto done;
3659 if (new_id) {
3660 err = got_object_id_str(&new_id_str, new_id);
3661 if (err)
3662 goto done;
3665 old_id_str[12] = '\0';
3666 if (new_id_str)
3667 new_id_str[12] = '\0';
3669 err = get_short_logmsg(&logmsg, 42, commit);
3670 if (err)
3671 goto done;
3673 printf("%s -> %s: %s\n", old_id_str,
3674 new_id_str ? new_id_str : "no-op change", logmsg);
3675 done:
3676 free(old_id_str);
3677 free(new_id_str);
3678 return err;
3681 static const struct got_error *
3682 rebase_progress(void *arg, unsigned char status, const char *path)
3684 unsigned char *rebase_status = arg;
3686 while (path[0] == '/')
3687 path++;
3688 printf("%c %s\n", status, path);
3690 if (*rebase_status == GOT_STATUS_CONFLICT)
3691 return NULL;
3692 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3693 *rebase_status = status;
3694 return NULL;
3697 static const struct got_error *
3698 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3699 struct got_reference *branch, struct got_reference *new_base_branch,
3700 struct got_reference *tmp_branch, struct got_repository *repo)
3702 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3703 return got_worktree_rebase_complete(worktree, fileindex,
3704 new_base_branch, tmp_branch, branch, repo);
3707 static const struct got_error *
3708 rebase_commit(struct got_pathlist_head *merged_paths,
3709 struct got_worktree *worktree, struct got_fileindex *fileindex,
3710 struct got_reference *tmp_branch,
3711 struct got_object_id *commit_id, struct got_repository *repo)
3713 const struct got_error *error;
3714 struct got_commit_object *commit;
3715 struct got_object_id *new_commit_id;
3717 error = got_object_open_as_commit(&commit, repo, commit_id);
3718 if (error)
3719 return error;
3721 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3722 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3723 if (error) {
3724 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3725 goto done;
3726 error = show_rebase_progress(commit, commit_id, NULL);
3727 } else {
3728 error = show_rebase_progress(commit, commit_id, new_commit_id);
3729 free(new_commit_id);
3731 done:
3732 got_object_commit_close(commit);
3733 return error;
3736 struct check_path_prefix_arg {
3737 const char *path_prefix;
3738 size_t len;
3739 int errcode;
3742 static const struct got_error *
3743 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3744 struct got_blob_object *blob2, struct got_object_id *id1,
3745 struct got_object_id *id2, const char *path1, const char *path2,
3746 struct got_repository *repo)
3748 struct check_path_prefix_arg *a = arg;
3750 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3751 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3752 return got_error(a->errcode);
3754 return NULL;
3757 static const struct got_error *
3758 check_path_prefix(struct got_object_id *parent_id,
3759 struct got_object_id *commit_id, const char *path_prefix,
3760 int errcode, struct got_repository *repo)
3762 const struct got_error *err;
3763 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3764 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3765 struct check_path_prefix_arg cpp_arg;
3767 if (got_path_is_root_dir(path_prefix))
3768 return NULL;
3770 err = got_object_open_as_commit(&commit, repo, commit_id);
3771 if (err)
3772 goto done;
3774 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3775 if (err)
3776 goto done;
3778 err = got_object_open_as_tree(&tree1, repo,
3779 got_object_commit_get_tree_id(parent_commit));
3780 if (err)
3781 goto done;
3783 err = got_object_open_as_tree(&tree2, repo,
3784 got_object_commit_get_tree_id(commit));
3785 if (err)
3786 goto done;
3788 cpp_arg.path_prefix = path_prefix;
3789 while (cpp_arg.path_prefix[0] == '/')
3790 cpp_arg.path_prefix++;
3791 cpp_arg.len = strlen(cpp_arg.path_prefix);
3792 cpp_arg.errcode = errcode;
3793 err = got_diff_tree(tree1, tree2, "", "", repo,
3794 check_path_prefix_in_diff, &cpp_arg, 0);
3795 done:
3796 if (tree1)
3797 got_object_tree_close(tree1);
3798 if (tree2)
3799 got_object_tree_close(tree2);
3800 if (commit)
3801 got_object_commit_close(commit);
3802 if (parent_commit)
3803 got_object_commit_close(parent_commit);
3804 return err;
3807 static const struct got_error *
3808 collect_commits(struct got_object_id_queue *commits,
3809 struct got_object_id *initial_commit_id,
3810 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3811 const char *path_prefix, int path_prefix_errcode,
3812 struct got_repository *repo)
3814 const struct got_error *err = NULL;
3815 struct got_commit_graph *graph = NULL;
3816 struct got_object_id *parent_id = NULL;
3817 struct got_object_qid *qid;
3818 struct got_object_id *commit_id = initial_commit_id;
3820 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3821 if (err)
3822 return err;
3824 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3825 if (err)
3826 goto done;
3827 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3828 err = got_commit_graph_iter_next(&parent_id, graph);
3829 if (err) {
3830 if (err->code == GOT_ERR_ITER_COMPLETED) {
3831 err = got_error_msg(GOT_ERR_ANCESTRY,
3832 "ran out of commits to rebase before "
3833 "youngest common ancestor commit has "
3834 "been reached?!?");
3835 goto done;
3836 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3837 goto done;
3838 err = got_commit_graph_fetch_commits(graph, 1, repo);
3839 if (err)
3840 goto done;
3841 } else {
3842 err = check_path_prefix(parent_id, commit_id,
3843 path_prefix, path_prefix_errcode, repo);
3844 if (err)
3845 goto done;
3847 err = got_object_qid_alloc(&qid, commit_id);
3848 if (err)
3849 goto done;
3850 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3851 commit_id = parent_id;
3854 done:
3855 got_commit_graph_close(graph);
3856 return err;
3859 static const struct got_error *
3860 cmd_rebase(int argc, char *argv[])
3862 const struct got_error *error = NULL;
3863 struct got_worktree *worktree = NULL;
3864 struct got_repository *repo = NULL;
3865 struct got_fileindex *fileindex = NULL;
3866 char *cwd = NULL;
3867 struct got_reference *branch = NULL;
3868 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3869 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3870 struct got_object_id *resume_commit_id = NULL;
3871 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3872 struct got_commit_object *commit = NULL;
3873 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3874 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3875 struct got_object_id_queue commits;
3876 struct got_pathlist_head merged_paths;
3877 const struct got_object_id_queue *parent_ids;
3878 struct got_object_qid *qid, *pid;
3880 SIMPLEQ_INIT(&commits);
3881 TAILQ_INIT(&merged_paths);
3883 while ((ch = getopt(argc, argv, "ac")) != -1) {
3884 switch (ch) {
3885 case 'a':
3886 abort_rebase = 1;
3887 break;
3888 case 'c':
3889 continue_rebase = 1;
3890 break;
3891 default:
3892 usage_rebase();
3893 /* NOTREACHED */
3897 argc -= optind;
3898 argv += optind;
3900 #ifndef PROFILE
3901 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3902 "unveil", NULL) == -1)
3903 err(1, "pledge");
3904 #endif
3905 if (abort_rebase && continue_rebase)
3906 usage_rebase();
3907 else if (abort_rebase || continue_rebase) {
3908 if (argc != 0)
3909 usage_rebase();
3910 } else if (argc != 1)
3911 usage_rebase();
3913 cwd = getcwd(NULL, 0);
3914 if (cwd == NULL) {
3915 error = got_error_from_errno("getcwd");
3916 goto done;
3918 error = got_worktree_open(&worktree, cwd);
3919 if (error)
3920 goto done;
3922 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3923 if (error != NULL)
3924 goto done;
3926 error = apply_unveil(got_repo_get_path(repo), 0,
3927 got_worktree_get_root_path(worktree));
3928 if (error)
3929 goto done;
3931 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3932 if (error)
3933 goto done;
3935 if (abort_rebase) {
3936 int did_something;
3937 if (!rebase_in_progress) {
3938 error = got_error(GOT_ERR_NOT_REBASING);
3939 goto done;
3941 error = got_worktree_rebase_continue(&resume_commit_id,
3942 &new_base_branch, &tmp_branch, &branch, &fileindex,
3943 worktree, repo);
3944 if (error)
3945 goto done;
3946 printf("Switching work tree to %s\n",
3947 got_ref_get_symref_target(new_base_branch));
3948 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3949 new_base_branch, update_progress, &did_something);
3950 if (error)
3951 goto done;
3952 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3953 goto done; /* nothing else to do */
3956 if (continue_rebase) {
3957 if (!rebase_in_progress) {
3958 error = got_error(GOT_ERR_NOT_REBASING);
3959 goto done;
3961 error = got_worktree_rebase_continue(&resume_commit_id,
3962 &new_base_branch, &tmp_branch, &branch, &fileindex,
3963 worktree, repo);
3964 if (error)
3965 goto done;
3967 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3968 resume_commit_id, repo);
3969 if (error)
3970 goto done;
3972 yca_id = got_object_id_dup(resume_commit_id);
3973 if (yca_id == NULL) {
3974 error = got_error_from_errno("got_object_id_dup");
3975 goto done;
3977 } else {
3978 error = got_ref_open(&branch, repo, argv[0], 0);
3979 if (error != NULL)
3980 goto done;
3983 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3984 if (error)
3985 goto done;
3987 if (!continue_rebase) {
3988 struct got_object_id *base_commit_id;
3990 base_commit_id = got_worktree_get_base_commit_id(worktree);
3991 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3992 base_commit_id, branch_head_commit_id, repo);
3993 if (error)
3994 goto done;
3995 if (yca_id == NULL) {
3996 error = got_error_msg(GOT_ERR_ANCESTRY,
3997 "specified branch shares no common ancestry "
3998 "with work tree's branch");
3999 goto done;
4002 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4003 if (error) {
4004 if (error->code != GOT_ERR_ANCESTRY)
4005 goto done;
4006 error = NULL;
4007 } else {
4008 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4009 "specified branch resolves to a commit which "
4010 "is already contained in work tree's branch");
4011 goto done;
4013 error = got_worktree_rebase_prepare(&new_base_branch,
4014 &tmp_branch, &fileindex, worktree, branch, repo);
4015 if (error)
4016 goto done;
4019 commit_id = branch_head_commit_id;
4020 error = got_object_open_as_commit(&commit, repo, commit_id);
4021 if (error)
4022 goto done;
4024 parent_ids = got_object_commit_get_parent_ids(commit);
4025 pid = SIMPLEQ_FIRST(parent_ids);
4026 error = collect_commits(&commits, commit_id, pid->id,
4027 yca_id, got_worktree_get_path_prefix(worktree),
4028 GOT_ERR_REBASE_PATH, repo);
4029 got_object_commit_close(commit);
4030 commit = NULL;
4031 if (error)
4032 goto done;
4034 if (SIMPLEQ_EMPTY(&commits)) {
4035 if (continue_rebase)
4036 error = rebase_complete(worktree, fileindex,
4037 branch, new_base_branch, tmp_branch, repo);
4038 else
4039 error = got_error(GOT_ERR_EMPTY_REBASE);
4040 goto done;
4043 pid = NULL;
4044 SIMPLEQ_FOREACH(qid, &commits, entry) {
4045 commit_id = qid->id;
4046 parent_id = pid ? pid->id : yca_id;
4047 pid = qid;
4049 error = got_worktree_rebase_merge_files(&merged_paths,
4050 worktree, fileindex, parent_id, commit_id, repo,
4051 rebase_progress, &rebase_status, check_cancelled, NULL);
4052 if (error)
4053 goto done;
4055 if (rebase_status == GOT_STATUS_CONFLICT) {
4056 got_worktree_rebase_pathlist_free(&merged_paths);
4057 break;
4060 error = rebase_commit(&merged_paths, worktree, fileindex,
4061 tmp_branch, commit_id, repo);
4062 got_worktree_rebase_pathlist_free(&merged_paths);
4063 if (error)
4064 goto done;
4067 if (rebase_status == GOT_STATUS_CONFLICT) {
4068 error = got_worktree_rebase_postpone(worktree, fileindex);
4069 if (error)
4070 goto done;
4071 error = got_error_msg(GOT_ERR_CONFLICTS,
4072 "conflicts must be resolved before rebasing can continue");
4073 } else
4074 error = rebase_complete(worktree, fileindex, branch,
4075 new_base_branch, tmp_branch, repo);
4076 done:
4077 got_object_id_queue_free(&commits);
4078 free(branch_head_commit_id);
4079 free(resume_commit_id);
4080 free(yca_id);
4081 if (commit)
4082 got_object_commit_close(commit);
4083 if (branch)
4084 got_ref_close(branch);
4085 if (new_base_branch)
4086 got_ref_close(new_base_branch);
4087 if (tmp_branch)
4088 got_ref_close(tmp_branch);
4089 if (worktree)
4090 got_worktree_close(worktree);
4091 if (repo)
4092 got_repo_close(repo);
4093 return error;
4096 __dead static void
4097 usage_histedit(void)
4099 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4100 getprogname());
4101 exit(1);
4104 #define GOT_HISTEDIT_PICK 'p'
4105 #define GOT_HISTEDIT_EDIT 'e'
4106 #define GOT_HISTEDIT_FOLD 'f'
4107 #define GOT_HISTEDIT_DROP 'd'
4108 #define GOT_HISTEDIT_MESG 'm'
4110 static struct got_histedit_cmd {
4111 unsigned char code;
4112 const char *name;
4113 const char *desc;
4114 } got_histedit_cmds[] = {
4115 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4116 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4117 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4118 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4119 { GOT_HISTEDIT_MESG, "mesg",
4120 "single-line log message for commit above (open editor if empty)" },
4123 struct got_histedit_list_entry {
4124 TAILQ_ENTRY(got_histedit_list_entry) entry;
4125 struct got_object_id *commit_id;
4126 const struct got_histedit_cmd *cmd;
4127 char *logmsg;
4129 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4131 static const struct got_error *
4132 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4133 FILE *f, struct got_repository *repo)
4135 const struct got_error *err = NULL;
4136 char *logmsg = NULL, *id_str = NULL;
4137 struct got_commit_object *commit = NULL;
4138 size_t n;
4140 err = got_object_open_as_commit(&commit, repo, commit_id);
4141 if (err)
4142 goto done;
4144 err = get_short_logmsg(&logmsg, 34, commit);
4145 if (err)
4146 goto done;
4148 err = got_object_id_str(&id_str, commit_id);
4149 if (err)
4150 goto done;
4152 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4153 if (n < 0)
4154 err = got_ferror(f, GOT_ERR_IO);
4155 done:
4156 if (commit)
4157 got_object_commit_close(commit);
4158 free(id_str);
4159 free(logmsg);
4160 return err;
4163 static const struct got_error *
4164 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4165 struct got_repository *repo)
4167 const struct got_error *err = NULL;
4168 struct got_object_qid *qid;
4170 if (SIMPLEQ_EMPTY(commits))
4171 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4173 SIMPLEQ_FOREACH(qid, commits, entry) {
4174 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4175 f, repo);
4176 if (err)
4177 break;
4180 return err;
4183 static const struct got_error *
4184 write_cmd_list(FILE *f)
4186 const struct got_error *err = NULL;
4187 int n, i;
4189 n = fprintf(f, "# Available histedit commands:\n");
4190 if (n < 0)
4191 return got_ferror(f, GOT_ERR_IO);
4193 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4194 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4195 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4196 cmd->desc);
4197 if (n < 0) {
4198 err = got_ferror(f, GOT_ERR_IO);
4199 break;
4202 n = fprintf(f, "# Commits will be processed in order from top to "
4203 "bottom of this file.\n");
4204 if (n < 0)
4205 return got_ferror(f, GOT_ERR_IO);
4206 return err;
4209 static const struct got_error *
4210 histedit_syntax_error(int lineno)
4212 static char msg[42];
4213 int ret;
4215 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4216 lineno);
4217 if (ret == -1 || ret >= sizeof(msg))
4218 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4220 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4223 static const struct got_error *
4224 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4225 char *logmsg, struct got_repository *repo)
4227 const struct got_error *err;
4228 struct got_commit_object *folded_commit = NULL;
4229 char *id_str;
4231 err = got_object_id_str(&id_str, hle->commit_id);
4232 if (err)
4233 return err;
4235 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4236 if (err)
4237 goto done;
4239 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4240 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4241 got_object_commit_get_logmsg(folded_commit)) == -1) {
4242 err = got_error_from_errno("asprintf");
4243 goto done;
4245 done:
4246 if (folded_commit)
4247 got_object_commit_close(folded_commit);
4248 free(id_str);
4249 return err;
4252 static struct got_histedit_list_entry *
4253 get_folded_commits(struct got_histedit_list_entry *hle)
4255 struct got_histedit_list_entry *prev, *folded = NULL;
4257 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4258 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4259 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4260 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4261 folded = prev;
4262 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4265 return folded;
4268 static const struct got_error *
4269 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4270 struct got_repository *repo)
4272 char *logmsg_path = NULL, *id_str = NULL;
4273 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4274 const struct got_error *err = NULL;
4275 struct got_commit_object *commit = NULL;
4276 int fd;
4277 struct got_histedit_list_entry *folded = NULL;
4279 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4280 if (err)
4281 return err;
4283 folded = get_folded_commits(hle);
4284 if (folded) {
4285 while (folded != hle) {
4286 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4287 folded = TAILQ_NEXT(folded, entry);
4288 continue;
4290 err = append_folded_commit_msg(&new_msg, folded,
4291 logmsg, repo);
4292 if (err)
4293 goto done;
4294 free(logmsg);
4295 logmsg = new_msg;
4296 folded = TAILQ_NEXT(folded, entry);
4300 err = got_object_id_str(&id_str, hle->commit_id);
4301 if (err)
4302 goto done;
4303 if (asprintf(&new_msg,
4304 "%s\n# original log message of commit %s: %s",
4305 logmsg ? logmsg : "", id_str,
4306 got_object_commit_get_logmsg(commit)) == -1) {
4307 err = got_error_from_errno("asprintf");
4308 goto done;
4310 free(logmsg);
4311 logmsg = new_msg;
4313 err = got_object_id_str(&id_str, hle->commit_id);
4314 if (err)
4315 goto done;
4317 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4318 if (err)
4319 goto done;
4321 dprintf(fd, logmsg);
4322 close(fd);
4324 err = get_editor(&editor);
4325 if (err)
4326 goto done;
4328 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4329 if (err) {
4330 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4331 goto done;
4332 err = NULL;
4333 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4334 if (hle->logmsg == NULL)
4335 err = got_error_from_errno("strdup");
4337 done:
4338 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4339 err = got_error_from_errno2("unlink", logmsg_path);
4340 free(logmsg_path);
4341 free(logmsg);
4342 free(editor);
4343 if (commit)
4344 got_object_commit_close(commit);
4345 return err;
4348 static const struct got_error *
4349 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4350 FILE *f, struct got_repository *repo)
4352 const struct got_error *err = NULL;
4353 char *line = NULL, *p, *end;
4354 size_t size;
4355 ssize_t len;
4356 int lineno = 0, i;
4357 const struct got_histedit_cmd *cmd;
4358 struct got_object_id *commit_id = NULL;
4359 struct got_histedit_list_entry *hle = NULL;
4361 for (;;) {
4362 len = getline(&line, &size, f);
4363 if (len == -1) {
4364 const struct got_error *getline_err;
4365 if (feof(f))
4366 break;
4367 getline_err = got_error_from_errno("getline");
4368 err = got_ferror(f, getline_err->code);
4369 break;
4371 lineno++;
4372 p = line;
4373 while (isspace((unsigned char)p[0]))
4374 p++;
4375 if (p[0] == '#' || p[0] == '\0') {
4376 free(line);
4377 line = NULL;
4378 continue;
4380 cmd = NULL;
4381 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4382 cmd = &got_histedit_cmds[i];
4383 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4384 isspace((unsigned char)p[strlen(cmd->name)])) {
4385 p += strlen(cmd->name);
4386 break;
4388 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4389 p++;
4390 break;
4393 if (i == nitems(got_histedit_cmds)) {
4394 err = histedit_syntax_error(lineno);
4395 break;
4397 while (isspace((unsigned char)p[0]))
4398 p++;
4399 if (cmd->code == GOT_HISTEDIT_MESG) {
4400 if (hle == NULL || hle->logmsg != NULL) {
4401 err = got_error(GOT_ERR_HISTEDIT_CMD);
4402 break;
4404 if (p[0] == '\0') {
4405 err = histedit_edit_logmsg(hle, repo);
4406 if (err)
4407 break;
4408 } else {
4409 hle->logmsg = strdup(p);
4410 if (hle->logmsg == NULL) {
4411 err = got_error_from_errno("strdup");
4412 break;
4415 free(line);
4416 line = NULL;
4417 continue;
4418 } else {
4419 end = p;
4420 while (end[0] && !isspace((unsigned char)end[0]))
4421 end++;
4422 *end = '\0';
4424 err = got_object_resolve_id_str(&commit_id, repo, p);
4425 if (err) {
4426 /* override error code */
4427 err = histedit_syntax_error(lineno);
4428 break;
4431 hle = malloc(sizeof(*hle));
4432 if (hle == NULL) {
4433 err = got_error_from_errno("malloc");
4434 break;
4436 hle->cmd = cmd;
4437 hle->commit_id = commit_id;
4438 hle->logmsg = NULL;
4439 commit_id = NULL;
4440 free(line);
4441 line = NULL;
4442 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4445 free(line);
4446 free(commit_id);
4447 return err;
4450 static const struct got_error *
4451 histedit_check_script(struct got_histedit_list *histedit_cmds,
4452 struct got_object_id_queue *commits, struct got_repository *repo)
4454 const struct got_error *err = NULL;
4455 struct got_object_qid *qid;
4456 struct got_histedit_list_entry *hle;
4457 static char msg[80];
4458 char *id_str;
4460 if (TAILQ_EMPTY(histedit_cmds))
4461 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4462 "histedit script contains no commands");
4464 SIMPLEQ_FOREACH(qid, commits, entry) {
4465 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4466 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4467 break;
4469 if (hle == NULL) {
4470 err = got_object_id_str(&id_str, qid->id);
4471 if (err)
4472 return err;
4473 snprintf(msg, sizeof(msg),
4474 "commit %s missing from histedit script", id_str);
4475 free(id_str);
4476 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4480 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4481 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4482 "last commit in histedit script cannot be folded");
4484 return NULL;
4487 static const struct got_error *
4488 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4489 const char *path, struct got_object_id_queue *commits,
4490 struct got_repository *repo)
4492 const struct got_error *err = NULL;
4493 char *editor;
4494 FILE *f = NULL;
4496 err = get_editor(&editor);
4497 if (err)
4498 return err;
4500 if (spawn_editor(editor, path) == -1) {
4501 err = got_error_from_errno("failed spawning editor");
4502 goto done;
4505 f = fopen(path, "r");
4506 if (f == NULL) {
4507 err = got_error_from_errno("fopen");
4508 goto done;
4510 err = histedit_parse_list(histedit_cmds, f, repo);
4511 if (err)
4512 goto done;
4514 err = histedit_check_script(histedit_cmds, commits, repo);
4515 done:
4516 if (f && fclose(f) != 0 && err == NULL)
4517 err = got_error_from_errno("fclose");
4518 free(editor);
4519 return err;
4522 static const struct got_error *
4523 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4524 struct got_object_id_queue *, const char *, struct got_repository *);
4526 static const struct got_error *
4527 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4528 struct got_object_id_queue *commits, struct got_repository *repo)
4530 const struct got_error *err;
4531 FILE *f = NULL;
4532 char *path = NULL;
4534 err = got_opentemp_named(&path, &f, "got-histedit");
4535 if (err)
4536 return err;
4538 err = write_cmd_list(f);
4539 if (err)
4540 goto done;
4542 err = histedit_write_commit_list(commits, f, repo);
4543 if (err)
4544 goto done;
4546 if (fclose(f) != 0) {
4547 err = got_error_from_errno("fclose");
4548 goto done;
4550 f = NULL;
4552 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4553 if (err) {
4554 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4555 err->code != GOT_ERR_HISTEDIT_CMD)
4556 goto done;
4557 err = histedit_edit_list_retry(histedit_cmds, err,
4558 commits, path, repo);
4560 done:
4561 if (f && fclose(f) != 0 && err == NULL)
4562 err = got_error_from_errno("fclose");
4563 if (path && unlink(path) != 0 && err == NULL)
4564 err = got_error_from_errno2("unlink", path);
4565 free(path);
4566 return err;
4569 static const struct got_error *
4570 histedit_save_list(struct got_histedit_list *histedit_cmds,
4571 struct got_worktree *worktree, struct got_repository *repo)
4573 const struct got_error *err = NULL;
4574 char *path = NULL;
4575 FILE *f = NULL;
4576 struct got_histedit_list_entry *hle;
4577 struct got_commit_object *commit = NULL;
4579 err = got_worktree_get_histedit_script_path(&path, worktree);
4580 if (err)
4581 return err;
4583 f = fopen(path, "w");
4584 if (f == NULL) {
4585 err = got_error_from_errno2("fopen", path);
4586 goto done;
4588 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4589 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4590 repo);
4591 if (err)
4592 break;
4594 if (hle->logmsg) {
4595 int n = fprintf(f, "%c %s\n",
4596 GOT_HISTEDIT_MESG, hle->logmsg);
4597 if (n < 0) {
4598 err = got_ferror(f, GOT_ERR_IO);
4599 break;
4603 done:
4604 if (f && fclose(f) != 0 && err == NULL)
4605 err = got_error_from_errno("fclose");
4606 free(path);
4607 if (commit)
4608 got_object_commit_close(commit);
4609 return err;
4612 void
4613 histedit_free_list(struct got_histedit_list *histedit_cmds)
4615 struct got_histedit_list_entry *hle;
4617 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4618 TAILQ_REMOVE(histedit_cmds, hle, entry);
4619 free(hle);
4623 static const struct got_error *
4624 histedit_load_list(struct got_histedit_list *histedit_cmds,
4625 const char *path, struct got_repository *repo)
4627 const struct got_error *err = NULL;
4628 FILE *f = NULL;
4630 f = fopen(path, "r");
4631 if (f == NULL) {
4632 err = got_error_from_errno2("fopen", path);
4633 goto done;
4636 err = histedit_parse_list(histedit_cmds, f, repo);
4637 done:
4638 if (f && fclose(f) != 0 && err == NULL)
4639 err = got_error_from_errno("fclose");
4640 return err;
4643 static const struct got_error *
4644 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4645 const struct got_error *edit_err, struct got_object_id_queue *commits,
4646 const char *path, struct got_repository *repo)
4648 const struct got_error *err = NULL, *prev_err = edit_err;
4649 int resp = ' ';
4651 while (resp != 'c' && resp != 'r' && resp != 'a') {
4652 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4653 "or (a)bort: ", getprogname(), prev_err->msg);
4654 resp = getchar();
4655 if (resp == 'c') {
4656 histedit_free_list(histedit_cmds);
4657 err = histedit_run_editor(histedit_cmds, path, commits,
4658 repo);
4659 if (err) {
4660 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4661 err->code != GOT_ERR_HISTEDIT_CMD)
4662 break;
4663 prev_err = err;
4664 resp = ' ';
4665 continue;
4667 break;
4668 } else if (resp == 'r') {
4669 histedit_free_list(histedit_cmds);
4670 err = histedit_edit_script(histedit_cmds,
4671 commits, repo);
4672 if (err) {
4673 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4674 err->code != GOT_ERR_HISTEDIT_CMD)
4675 break;
4676 prev_err = err;
4677 resp = ' ';
4678 continue;
4680 break;
4681 } else if (resp == 'a') {
4682 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4683 break;
4684 } else
4685 printf("invalid response '%c'\n", resp);
4688 return err;
4691 static const struct got_error *
4692 histedit_complete(struct got_worktree *worktree,
4693 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4694 struct got_reference *branch, struct got_repository *repo)
4696 printf("Switching work tree to %s\n",
4697 got_ref_get_symref_target(branch));
4698 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4699 branch, repo);
4702 static const struct got_error *
4703 show_histedit_progress(struct got_commit_object *commit,
4704 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4706 const struct got_error *err;
4707 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4709 err = got_object_id_str(&old_id_str, hle->commit_id);
4710 if (err)
4711 goto done;
4713 if (new_id) {
4714 err = got_object_id_str(&new_id_str, new_id);
4715 if (err)
4716 goto done;
4719 old_id_str[12] = '\0';
4720 if (new_id_str)
4721 new_id_str[12] = '\0';
4723 if (hle->logmsg) {
4724 logmsg = strdup(hle->logmsg);
4725 if (logmsg == NULL) {
4726 err = got_error_from_errno("strdup");
4727 goto done;
4729 trim_logmsg(logmsg, 42);
4730 } else {
4731 err = get_short_logmsg(&logmsg, 42, commit);
4732 if (err)
4733 goto done;
4736 switch (hle->cmd->code) {
4737 case GOT_HISTEDIT_PICK:
4738 case GOT_HISTEDIT_EDIT:
4739 printf("%s -> %s: %s\n", old_id_str,
4740 new_id_str ? new_id_str : "no-op change", logmsg);
4741 break;
4742 case GOT_HISTEDIT_DROP:
4743 case GOT_HISTEDIT_FOLD:
4744 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4745 logmsg);
4746 break;
4747 default:
4748 break;
4751 done:
4752 free(old_id_str);
4753 free(new_id_str);
4754 return err;
4757 static const struct got_error *
4758 histedit_commit(struct got_pathlist_head *merged_paths,
4759 struct got_worktree *worktree, struct got_fileindex *fileindex,
4760 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4761 struct got_repository *repo)
4763 const struct got_error *err;
4764 struct got_commit_object *commit;
4765 struct got_object_id *new_commit_id;
4767 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4768 && hle->logmsg == NULL) {
4769 err = histedit_edit_logmsg(hle, repo);
4770 if (err)
4771 return err;
4774 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4775 if (err)
4776 return err;
4778 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4779 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4780 hle->logmsg, repo);
4781 if (err) {
4782 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4783 goto done;
4784 err = show_histedit_progress(commit, hle, NULL);
4785 } else {
4786 err = show_histedit_progress(commit, hle, new_commit_id);
4787 free(new_commit_id);
4789 done:
4790 got_object_commit_close(commit);
4791 return err;
4794 static const struct got_error *
4795 histedit_skip_commit(struct got_histedit_list_entry *hle,
4796 struct got_worktree *worktree, struct got_repository *repo)
4798 const struct got_error *error;
4799 struct got_commit_object *commit;
4801 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4802 repo);
4803 if (error)
4804 return error;
4806 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4807 if (error)
4808 return error;
4810 error = show_histedit_progress(commit, hle, NULL);
4811 got_object_commit_close(commit);
4812 return error;
4815 static const struct got_error *
4816 cmd_histedit(int argc, char *argv[])
4818 const struct got_error *error = NULL;
4819 struct got_worktree *worktree = NULL;
4820 struct got_fileindex *fileindex = NULL;
4821 struct got_repository *repo = NULL;
4822 char *cwd = NULL;
4823 struct got_reference *branch = NULL;
4824 struct got_reference *tmp_branch = NULL;
4825 struct got_object_id *resume_commit_id = NULL;
4826 struct got_object_id *base_commit_id = NULL;
4827 struct got_object_id *head_commit_id = NULL;
4828 struct got_commit_object *commit = NULL;
4829 int ch, rebase_in_progress = 0, did_something;
4830 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4831 const char *edit_script_path = NULL;
4832 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4833 struct got_object_id_queue commits;
4834 struct got_pathlist_head merged_paths;
4835 const struct got_object_id_queue *parent_ids;
4836 struct got_object_qid *pid;
4837 struct got_histedit_list histedit_cmds;
4838 struct got_histedit_list_entry *hle;
4840 SIMPLEQ_INIT(&commits);
4841 TAILQ_INIT(&histedit_cmds);
4842 TAILQ_INIT(&merged_paths);
4844 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4845 switch (ch) {
4846 case 'a':
4847 abort_edit = 1;
4848 break;
4849 case 'c':
4850 continue_edit = 1;
4851 break;
4852 case 'F':
4853 edit_script_path = optarg;
4854 break;
4855 default:
4856 usage_histedit();
4857 /* NOTREACHED */
4861 argc -= optind;
4862 argv += optind;
4864 #ifndef PROFILE
4865 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4866 "unveil", NULL) == -1)
4867 err(1, "pledge");
4868 #endif
4869 if (abort_edit && continue_edit)
4870 usage_histedit();
4871 if (argc != 0)
4872 usage_histedit();
4875 * This command cannot apply unveil(2) in all cases because the
4876 * user may choose to run an editor to edit the histedit script
4877 * and to edit individual commit log messages.
4878 * unveil(2) traverses exec(2); if an editor is used we have to
4879 * apply unveil after edit script and log messages have been written.
4880 * XXX TODO: Make use of unveil(2) where possible.
4883 cwd = getcwd(NULL, 0);
4884 if (cwd == NULL) {
4885 error = got_error_from_errno("getcwd");
4886 goto done;
4888 error = got_worktree_open(&worktree, cwd);
4889 if (error)
4890 goto done;
4892 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4893 if (error != NULL)
4894 goto done;
4896 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4897 if (error)
4898 goto done;
4899 if (rebase_in_progress) {
4900 error = got_error(GOT_ERR_REBASING);
4901 goto done;
4904 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4905 if (error)
4906 goto done;
4908 if (edit_in_progress && abort_edit) {
4909 error = got_worktree_histedit_continue(&resume_commit_id,
4910 &tmp_branch, &branch, &base_commit_id, &fileindex,
4911 worktree, repo);
4912 if (error)
4913 goto done;
4914 printf("Switching work tree to %s\n",
4915 got_ref_get_symref_target(branch));
4916 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4917 branch, base_commit_id, update_progress, &did_something);
4918 if (error)
4919 goto done;
4920 printf("Histedit of %s aborted\n",
4921 got_ref_get_symref_target(branch));
4922 goto done; /* nothing else to do */
4923 } else if (abort_edit) {
4924 error = got_error(GOT_ERR_NOT_HISTEDIT);
4925 goto done;
4928 if (continue_edit) {
4929 char *path;
4931 if (!edit_in_progress) {
4932 error = got_error(GOT_ERR_NOT_HISTEDIT);
4933 goto done;
4936 error = got_worktree_get_histedit_script_path(&path, worktree);
4937 if (error)
4938 goto done;
4940 error = histedit_load_list(&histedit_cmds, path, repo);
4941 free(path);
4942 if (error)
4943 goto done;
4945 error = got_worktree_histedit_continue(&resume_commit_id,
4946 &tmp_branch, &branch, &base_commit_id, &fileindex,
4947 worktree, repo);
4948 if (error)
4949 goto done;
4951 error = got_ref_resolve(&head_commit_id, repo, branch);
4952 if (error)
4953 goto done;
4955 error = got_object_open_as_commit(&commit, repo,
4956 head_commit_id);
4957 if (error)
4958 goto done;
4959 parent_ids = got_object_commit_get_parent_ids(commit);
4960 pid = SIMPLEQ_FIRST(parent_ids);
4961 if (pid == NULL) {
4962 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4963 goto done;
4965 error = collect_commits(&commits, head_commit_id, pid->id,
4966 base_commit_id, got_worktree_get_path_prefix(worktree),
4967 GOT_ERR_HISTEDIT_PATH, repo);
4968 got_object_commit_close(commit);
4969 commit = NULL;
4970 if (error)
4971 goto done;
4972 } else {
4973 if (edit_in_progress) {
4974 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4975 goto done;
4978 error = got_ref_open(&branch, repo,
4979 got_worktree_get_head_ref_name(worktree), 0);
4980 if (error != NULL)
4981 goto done;
4983 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4984 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4985 "will not edit commit history of a branch outside "
4986 "the \"refs/heads/\" reference namespace");
4987 goto done;
4990 error = got_ref_resolve(&head_commit_id, repo, branch);
4991 got_ref_close(branch);
4992 branch = NULL;
4993 if (error)
4994 goto done;
4996 error = got_object_open_as_commit(&commit, repo,
4997 head_commit_id);
4998 if (error)
4999 goto done;
5000 parent_ids = got_object_commit_get_parent_ids(commit);
5001 pid = SIMPLEQ_FIRST(parent_ids);
5002 if (pid == NULL) {
5003 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5004 goto done;
5006 error = collect_commits(&commits, head_commit_id, pid->id,
5007 got_worktree_get_base_commit_id(worktree),
5008 got_worktree_get_path_prefix(worktree),
5009 GOT_ERR_HISTEDIT_PATH, repo);
5010 got_object_commit_close(commit);
5011 commit = NULL;
5012 if (error)
5013 goto done;
5015 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5016 &base_commit_id, &fileindex, worktree, repo);
5017 if (error)
5018 goto done;
5020 if (edit_script_path) {
5021 error = histedit_load_list(&histedit_cmds,
5022 edit_script_path, repo);
5023 if (error) {
5024 got_worktree_histedit_abort(worktree, fileindex,
5025 repo, branch, base_commit_id,
5026 update_progress, &did_something);
5027 goto done;
5029 } else {
5030 error = histedit_edit_script(&histedit_cmds, &commits,
5031 repo);
5032 if (error) {
5033 got_worktree_histedit_abort(worktree, fileindex,
5034 repo, branch, base_commit_id,
5035 update_progress, &did_something);
5036 goto done;
5041 error = histedit_save_list(&histedit_cmds, worktree,
5042 repo);
5043 if (error) {
5044 got_worktree_histedit_abort(worktree, fileindex,
5045 repo, branch, base_commit_id,
5046 update_progress, &did_something);
5047 goto done;
5052 error = histedit_check_script(&histedit_cmds, &commits, repo);
5053 if (error)
5054 goto done;
5056 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5057 if (resume_commit_id) {
5058 if (got_object_id_cmp(hle->commit_id,
5059 resume_commit_id) != 0)
5060 continue;
5062 resume_commit_id = NULL;
5063 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5064 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5065 error = histedit_skip_commit(hle, worktree,
5066 repo);
5067 } else {
5068 error = histedit_commit(NULL, worktree,
5069 fileindex, tmp_branch, hle, repo);
5071 if (error)
5072 goto done;
5073 continue;
5076 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5077 error = histedit_skip_commit(hle, worktree, repo);
5078 if (error)
5079 goto done;
5080 continue;
5083 error = got_object_open_as_commit(&commit, repo,
5084 hle->commit_id);
5085 if (error)
5086 goto done;
5087 parent_ids = got_object_commit_get_parent_ids(commit);
5088 pid = SIMPLEQ_FIRST(parent_ids);
5090 error = got_worktree_histedit_merge_files(&merged_paths,
5091 worktree, fileindex, pid->id, hle->commit_id, repo,
5092 rebase_progress, &rebase_status, check_cancelled, NULL);
5093 if (error)
5094 goto done;
5095 got_object_commit_close(commit);
5096 commit = NULL;
5098 if (rebase_status == GOT_STATUS_CONFLICT) {
5099 got_worktree_rebase_pathlist_free(&merged_paths);
5100 break;
5103 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5104 char *id_str;
5105 error = got_object_id_str(&id_str, hle->commit_id);
5106 if (error)
5107 goto done;
5108 printf("Stopping histedit for amending commit %s\n",
5109 id_str);
5110 free(id_str);
5111 got_worktree_rebase_pathlist_free(&merged_paths);
5112 error = got_worktree_histedit_postpone(worktree,
5113 fileindex);
5114 goto done;
5117 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5118 error = histedit_skip_commit(hle, worktree, repo);
5119 if (error)
5120 goto done;
5121 continue;
5124 error = histedit_commit(&merged_paths, worktree, fileindex,
5125 tmp_branch, hle, repo);
5126 got_worktree_rebase_pathlist_free(&merged_paths);
5127 if (error)
5128 goto done;
5131 if (rebase_status == GOT_STATUS_CONFLICT) {
5132 error = got_worktree_histedit_postpone(worktree, fileindex);
5133 if (error)
5134 goto done;
5135 error = got_error_msg(GOT_ERR_CONFLICTS,
5136 "conflicts must be resolved before rebasing can continue");
5137 } else
5138 error = histedit_complete(worktree, fileindex, tmp_branch,
5139 branch, repo);
5140 done:
5141 got_object_id_queue_free(&commits);
5142 histedit_free_list(&histedit_cmds);
5143 free(head_commit_id);
5144 free(base_commit_id);
5145 free(resume_commit_id);
5146 if (commit)
5147 got_object_commit_close(commit);
5148 if (branch)
5149 got_ref_close(branch);
5150 if (tmp_branch)
5151 got_ref_close(tmp_branch);
5152 if (worktree)
5153 got_worktree_close(worktree);
5154 if (repo)
5155 got_repo_close(repo);
5156 return error;
5159 __dead static void
5160 usage_stage(void)
5162 fprintf(stderr, "usage: %s stage file-path ...\n",
5163 getprogname());
5164 exit(1);
5167 static const struct got_error *
5168 cmd_stage(int argc, char *argv[])
5170 const struct got_error *error = NULL;
5171 struct got_repository *repo = NULL;
5172 struct got_worktree *worktree = NULL;
5173 char *cwd = NULL;
5174 struct got_pathlist_head paths;
5175 struct got_pathlist_entry *pe;
5176 const char *worktree_path;
5177 int ch, x;
5179 TAILQ_INIT(&paths);
5181 while ((ch = getopt(argc, argv, "")) != -1) {
5182 switch (ch) {
5183 default:
5184 usage_stage();
5185 /* NOTREACHED */
5189 argc -= optind;
5190 argv += optind;
5192 #ifndef PROFILE
5193 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5194 "unveil", NULL) == -1)
5195 err(1, "pledge");
5196 #endif
5197 if (argc < 1)
5198 usage_stage();
5200 cwd = getcwd(NULL, 0);
5201 if (cwd == NULL) {
5202 error = got_error_from_errno("getcwd");
5203 goto done;
5206 error = got_worktree_open(&worktree, cwd);
5207 if (error)
5208 goto done;
5210 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5211 if (error != NULL)
5212 goto done;
5214 error = apply_unveil(got_repo_get_path(repo), 1,
5215 got_worktree_get_root_path(worktree));
5216 if (error)
5217 goto done;
5219 worktree_path = got_worktree_get_root_path(worktree);
5220 for (x = 0; x < argc; x++) {
5221 char *path = realpath(argv[x], NULL);
5222 if (path == NULL) {
5223 if (errno != ENOENT) {
5224 error = got_error_from_errno2("realpath",
5225 argv[x]);
5226 goto done;
5228 if (got_path_is_child(argv[x], worktree_path,
5229 strlen(worktree_path))) {
5230 path = strdup(argv[x]);
5231 if (path == NULL) {
5232 error = got_error_from_errno("strdup");
5233 goto done;
5236 } else if (asprintf(&path, "%s/%s",
5237 got_worktree_get_root_path(worktree),
5238 argv[x]) == -1) {
5239 error = got_error_from_errno("asprintf");
5240 goto done;
5244 got_path_strip_trailing_slashes(path);
5245 error = got_pathlist_insert(&pe, &paths, path, NULL);
5246 if (error) {
5247 free(path);
5248 goto done;
5251 error = got_worktree_stage(worktree, &paths, print_status, NULL, repo);
5252 done:
5253 if (repo)
5254 got_repo_close(repo);
5255 if (worktree)
5256 got_worktree_close(worktree);
5257 TAILQ_FOREACH(pe, &paths, entry)
5258 free((char *)pe->path);
5259 got_pathlist_free(&paths);
5260 free(cwd);
5261 return error;