Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_stage(void);
101 __dead static void usage_unstage(void);
102 __dead static void usage_cat(void);
104 static const struct got_error* cmd_init(int, char *[]);
105 static const struct got_error* cmd_import(int, char *[]);
106 static const struct got_error* cmd_checkout(int, char *[]);
107 static const struct got_error* cmd_update(int, char *[]);
108 static const struct got_error* cmd_log(int, char *[]);
109 static const struct got_error* cmd_diff(int, char *[]);
110 static const struct got_error* cmd_blame(int, char *[]);
111 static const struct got_error* cmd_tree(int, char *[]);
112 static const struct got_error* cmd_status(int, char *[]);
113 static const struct got_error* cmd_ref(int, char *[]);
114 static const struct got_error* cmd_branch(int, char *[]);
115 static const struct got_error* cmd_tag(int, char *[]);
116 static const struct got_error* cmd_add(int, char *[]);
117 static const struct got_error* cmd_remove(int, char *[]);
118 static const struct got_error* cmd_revert(int, char *[]);
119 static const struct got_error* cmd_commit(int, char *[]);
120 static const struct got_error* cmd_cherrypick(int, char *[]);
121 static const struct got_error* cmd_backout(int, char *[]);
122 static const struct got_error* cmd_rebase(int, char *[]);
123 static const struct got_error* cmd_histedit(int, char *[]);
124 static const struct got_error* cmd_stage(int, char *[]);
125 static const struct got_error* cmd_unstage(int, char *[]);
126 static const struct got_error* cmd_cat(int, char *[]);
128 static struct got_cmd got_commands[] = {
129 { "init", cmd_init, usage_init, "in" },
130 { "import", cmd_import, usage_import, "im" },
131 { "checkout", cmd_checkout, usage_checkout, "co" },
132 { "update", cmd_update, usage_update, "up" },
133 { "log", cmd_log, usage_log, "" },
134 { "diff", cmd_diff, usage_diff, "di" },
135 { "blame", cmd_blame, usage_blame, "bl" },
136 { "tree", cmd_tree, usage_tree, "tr" },
137 { "status", cmd_status, usage_status, "st" },
138 { "ref", cmd_ref, usage_ref, "" },
139 { "branch", cmd_branch, usage_branch, "br" },
140 { "tag", cmd_tag, usage_tag, "" },
141 { "add", cmd_add, usage_add, "" },
142 { "remove", cmd_remove, usage_remove, "rm" },
143 { "revert", cmd_revert, usage_revert, "rv" },
144 { "commit", cmd_commit, usage_commit, "ci" },
145 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 { "backout", cmd_backout, usage_backout, "bo" },
147 { "rebase", cmd_rebase, usage_rebase, "rb" },
148 { "histedit", cmd_histedit, usage_histedit, "he" },
149 { "stage", cmd_stage, usage_stage, "sg" },
150 { "unstage", cmd_unstage, usage_unstage, "ug" },
151 { "cat", cmd_cat, usage_cat, "" },
152 };
154 static void
155 list_commands(void)
157 int i;
159 fprintf(stderr, "commands:");
160 for (i = 0; i < nitems(got_commands); i++) {
161 struct got_cmd *cmd = &got_commands[i];
162 fprintf(stderr, " %s", cmd->cmd_name);
164 fputc('\n', stderr);
167 int
168 main(int argc, char *argv[])
170 struct got_cmd *cmd;
171 unsigned int i;
172 int ch;
173 int hflag = 0, Vflag = 0;
175 setlocale(LC_CTYPE, "");
177 while ((ch = getopt(argc, argv, "hV")) != -1) {
178 switch (ch) {
179 case 'h':
180 hflag = 1;
181 break;
182 case 'V':
183 Vflag = 1;
184 break;
185 default:
186 usage(hflag);
187 /* NOTREACHED */
191 argc -= optind;
192 argv += optind;
193 optind = 0;
195 if (Vflag) {
196 got_version_print_str();
197 return 1;
200 if (argc <= 0)
201 usage(hflag);
203 signal(SIGINT, catch_sigint);
204 signal(SIGPIPE, catch_sigpipe);
206 for (i = 0; i < nitems(got_commands); i++) {
207 const struct got_error *error;
209 cmd = &got_commands[i];
211 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 strcmp(cmd->cmd_alias, argv[0]) != 0)
213 continue;
215 if (hflag)
216 got_commands[i].cmd_usage();
218 error = got_commands[i].cmd_main(argc, argv);
219 if (error && !(sigint_received || sigpipe_received)) {
220 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
221 return 1;
224 return 0;
227 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 list_commands();
229 return 1;
232 __dead static void
233 usage(int hflag)
235 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
236 getprogname());
237 if (hflag)
238 list_commands();
239 exit(1);
242 static const struct got_error *
243 get_editor(char **abspath)
245 const struct got_error *err = NULL;
246 const char *editor;
248 *abspath = NULL;
250 editor = getenv("VISUAL");
251 if (editor == NULL)
252 editor = getenv("EDITOR");
254 if (editor) {
255 err = got_path_find_prog(abspath, editor);
256 if (err)
257 return err;
260 if (*abspath == NULL) {
261 *abspath = strdup("/bin/ed");
262 if (*abspath == NULL)
263 return got_error_from_errno("strdup");
266 return NULL;
269 static const struct got_error *
270 apply_unveil(const char *repo_path, int repo_read_only,
271 const char *worktree_path)
273 const struct got_error *err;
275 #ifdef PROFILE
276 if (unveil("gmon.out", "rwc") != 0)
277 return got_error_from_errno2("unveil", "gmon.out");
278 #endif
279 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 return got_error_from_errno2("unveil", repo_path);
282 if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 return got_error_from_errno2("unveil", worktree_path);
285 if (unveil("/tmp", "rwc") != 0)
286 return got_error_from_errno2("unveil", "/tmp");
288 err = got_privsep_unveil_exec_helpers();
289 if (err != NULL)
290 return err;
292 if (unveil(NULL, NULL) != 0)
293 return got_error_from_errno("unveil");
295 return NULL;
298 __dead static void
299 usage_init(void)
301 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
302 exit(1);
305 static const struct got_error *
306 cmd_init(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 char *repo_path = NULL;
310 int ch;
312 while ((ch = getopt(argc, argv, "")) != -1) {
313 switch (ch) {
314 default:
315 usage_init();
316 /* NOTREACHED */
320 argc -= optind;
321 argv += optind;
323 #ifndef PROFILE
324 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
325 err(1, "pledge");
326 #endif
327 if (argc != 1)
328 usage_init();
330 repo_path = strdup(argv[0]);
331 if (repo_path == NULL)
332 return got_error_from_errno("strdup");
334 got_path_strip_trailing_slashes(repo_path);
336 error = got_path_mkdir(repo_path);
337 if (error &&
338 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
339 goto done;
341 error = apply_unveil(repo_path, 0, NULL);
342 if (error)
343 goto done;
345 error = got_repo_init(repo_path);
346 if (error != NULL)
347 goto done;
349 done:
350 free(repo_path);
351 return error;
354 __dead static void
355 usage_import(void)
357 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 "[-r repository-path] [-I pattern] path\n", getprogname());
359 exit(1);
362 int
363 spawn_editor(const char *editor, const char *file)
365 pid_t pid;
366 sig_t sighup, sigint, sigquit;
367 int st = -1;
369 sighup = signal(SIGHUP, SIG_IGN);
370 sigint = signal(SIGINT, SIG_IGN);
371 sigquit = signal(SIGQUIT, SIG_IGN);
373 switch (pid = fork()) {
374 case -1:
375 goto doneediting;
376 case 0:
377 execl(editor, editor, file, (char *)NULL);
378 _exit(127);
381 while (waitpid(pid, &st, 0) == -1)
382 if (errno != EINTR)
383 break;
385 doneediting:
386 (void)signal(SIGHUP, sighup);
387 (void)signal(SIGINT, sigint);
388 (void)signal(SIGQUIT, sigquit);
390 if (!WIFEXITED(st)) {
391 errno = EINTR;
392 return -1;
395 return WEXITSTATUS(st);
398 static const struct got_error *
399 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 const char *initial_content)
402 const struct got_error *err = NULL;
403 char buf[1024];
404 struct stat st, st2;
405 FILE *fp;
406 int content_changed = 0;
407 size_t len;
409 *logmsg = NULL;
411 if (stat(logmsg_path, &st) == -1)
412 return got_error_from_errno2("stat", logmsg_path);
414 if (spawn_editor(editor, logmsg_path) == -1)
415 return got_error_from_errno("failed spawning editor");
417 if (stat(logmsg_path, &st2) == -1)
418 return got_error_from_errno("stat");
420 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 "no changes made to commit message, aborting");
424 *logmsg = malloc(st2.st_size + 1);
425 if (*logmsg == NULL)
426 return got_error_from_errno("malloc");
427 (*logmsg)[0] = '\0';
428 len = 0;
430 fp = fopen(logmsg_path, "r");
431 if (fp == NULL) {
432 err = got_error_from_errno("fopen");
433 goto done;
435 while (fgets(buf, sizeof(buf), fp) != NULL) {
436 if (!content_changed && strcmp(buf, initial_content) != 0)
437 content_changed = 1;
438 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(*logmsg, buf, st2.st_size);
442 fclose(fp);
444 while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 (*logmsg)[len - 1] = '\0';
446 len--;
449 if (len == 0 || !content_changed)
450 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 "commit message cannot be empty, aborting");
452 done:
453 if (err) {
454 free(*logmsg);
455 *logmsg = NULL;
457 return err;
460 static const struct got_error *
461 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 const char *branch_name)
464 char *initial_content = NULL, *logmsg_path = NULL;
465 const struct got_error *err = NULL;
466 int fd;
468 if (asprintf(&initial_content,
469 "\n# %s to be imported to branch %s\n", path_dir,
470 branch_name) == -1)
471 return got_error_from_errno("asprintf");
473 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
474 if (err)
475 goto done;
477 dprintf(fd, initial_content);
478 close(fd);
480 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
481 done:
482 free(initial_content);
483 free(logmsg_path);
484 return err;
487 static const struct got_error *
488 import_progress(void *arg, const char *path)
490 printf("A %s\n", path);
491 return NULL;
494 static const struct got_error *
495 get_author(char **author, struct got_repository *repo)
497 const struct got_error *err = NULL;
498 const char *got_author, *name, *email;
500 *author = NULL;
502 name = got_repo_get_gitconfig_author_name(repo);
503 email = got_repo_get_gitconfig_author_email(repo);
504 if (name && email) {
505 if (asprintf(author, "%s <%s>", name, email) == -1)
506 return got_error_from_errno("asprintf");
507 return NULL;
510 got_author = getenv("GOT_AUTHOR");
511 if (got_author == NULL) {
512 name = got_repo_get_global_gitconfig_author_name(repo);
513 email = got_repo_get_global_gitconfig_author_email(repo);
514 if (name && email) {
515 if (asprintf(author, "%s <%s>", name, email) == -1)
516 return got_error_from_errno("asprintf");
517 return NULL;
519 /* TODO: Look up user in password database? */
520 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
523 *author = strdup(got_author);
524 if (*author == NULL)
525 return got_error_from_errno("strdup");
527 /*
528 * Really dumb email address check; we're only doing this to
529 * avoid git's object parser breaking on commits we create.
530 */
531 while (*got_author && *got_author != '<')
532 got_author++;
533 if (*got_author != '<') {
534 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
535 goto done;
537 while (*got_author && *got_author != '@')
538 got_author++;
539 if (*got_author != '@') {
540 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
541 goto done;
543 while (*got_author && *got_author != '>')
544 got_author++;
545 if (*got_author != '>')
546 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
547 done:
548 if (err) {
549 free(*author);
550 *author = NULL;
552 return err;
555 static const struct got_error *
556 get_gitconfig_path(char **gitconfig_path)
558 const char *homedir = getenv("HOME");
560 *gitconfig_path = NULL;
561 if (homedir) {
562 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
563 return got_error_from_errno("asprintf");
566 return NULL;
569 static const struct got_error *
570 cmd_import(int argc, char *argv[])
572 const struct got_error *error = NULL;
573 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
574 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
575 const char *branch_name = "master";
576 char *refname = NULL, *id_str = NULL;
577 struct got_repository *repo = NULL;
578 struct got_reference *branch_ref = NULL, *head_ref = NULL;
579 struct got_object_id *new_commit_id = NULL;
580 int ch;
581 struct got_pathlist_head ignores;
582 struct got_pathlist_entry *pe;
584 TAILQ_INIT(&ignores);
586 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
587 switch (ch) {
588 case 'b':
589 branch_name = optarg;
590 break;
591 case 'm':
592 logmsg = strdup(optarg);
593 if (logmsg == NULL) {
594 error = got_error_from_errno("strdup");
595 goto done;
597 break;
598 case 'r':
599 repo_path = realpath(optarg, NULL);
600 if (repo_path == NULL) {
601 error = got_error_from_errno("realpath");
602 goto done;
604 break;
605 case 'I':
606 if (optarg[0] == '\0')
607 break;
608 error = got_pathlist_insert(&pe, &ignores, optarg,
609 NULL);
610 if (error)
611 goto done;
612 break;
613 default:
614 usage_import();
615 /* NOTREACHED */
619 argc -= optind;
620 argv += optind;
622 #ifndef PROFILE
623 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
624 "unveil",
625 NULL) == -1)
626 err(1, "pledge");
627 #endif
628 if (argc != 1)
629 usage_import();
631 if (repo_path == NULL) {
632 repo_path = getcwd(NULL, 0);
633 if (repo_path == NULL)
634 return got_error_from_errno("getcwd");
636 got_path_strip_trailing_slashes(repo_path);
637 error = get_gitconfig_path(&gitconfig_path);
638 if (error)
639 goto done;
640 error = got_repo_open(&repo, repo_path, gitconfig_path);
641 if (error)
642 goto done;
644 error = get_author(&author, repo);
645 if (error)
646 return error;
648 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
649 error = got_error_from_errno("asprintf");
650 goto done;
653 error = got_ref_open(&branch_ref, repo, refname, 0);
654 if (error) {
655 if (error->code != GOT_ERR_NOT_REF)
656 goto done;
657 } else {
658 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
659 "import target branch already exists");
660 goto done;
663 path_dir = realpath(argv[0], NULL);
664 if (path_dir == NULL) {
665 error = got_error_from_errno("realpath");
666 goto done;
668 got_path_strip_trailing_slashes(path_dir);
670 /*
671 * unveil(2) traverses exec(2); if an editor is used we have
672 * to apply unveil after the log message has been written.
673 */
674 if (logmsg == NULL || strlen(logmsg) == 0) {
675 error = get_editor(&editor);
676 if (error)
677 goto done;
678 free(logmsg);
679 error = collect_import_msg(&logmsg, editor, path_dir, refname);
680 if (error)
681 goto done;
684 if (unveil(path_dir, "r") != 0)
685 return got_error_from_errno2("unveil", path_dir);
687 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
688 if (error)
689 goto done;
691 error = got_repo_import(&new_commit_id, path_dir, logmsg,
692 author, &ignores, repo, import_progress, NULL);
693 if (error)
694 goto done;
696 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
697 if (error)
698 goto done;
700 error = got_ref_write(branch_ref, repo);
701 if (error)
702 goto done;
704 error = got_object_id_str(&id_str, new_commit_id);
705 if (error)
706 goto done;
708 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
709 if (error) {
710 if (error->code != GOT_ERR_NOT_REF)
711 goto done;
713 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
714 branch_ref);
715 if (error)
716 goto done;
718 error = got_ref_write(head_ref, repo);
719 if (error)
720 goto done;
723 printf("Created branch %s with commit %s\n",
724 got_ref_get_name(branch_ref), id_str);
725 done:
726 free(logmsg);
727 free(repo_path);
728 free(editor);
729 free(refname);
730 free(new_commit_id);
731 free(id_str);
732 free(author);
733 free(gitconfig_path);
734 if (branch_ref)
735 got_ref_close(branch_ref);
736 if (head_ref)
737 got_ref_close(head_ref);
738 return error;
741 __dead static void
742 usage_checkout(void)
744 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
745 "[-p prefix] repository-path [worktree-path]\n", getprogname());
746 exit(1);
749 static const struct got_error *
750 checkout_progress(void *arg, unsigned char status, const char *path)
752 char *worktree_path = arg;
754 /* Base commit bump happens silently. */
755 if (status == GOT_STATUS_BUMP_BASE)
756 return NULL;
758 while (path[0] == '/')
759 path++;
761 printf("%c %s/%s\n", status, worktree_path, path);
762 return NULL;
765 static const struct got_error *
766 check_cancelled(void *arg)
768 if (sigint_received || sigpipe_received)
769 return got_error(GOT_ERR_CANCELLED);
770 return NULL;
773 static const struct got_error *
774 check_linear_ancestry(struct got_object_id *commit_id,
775 struct got_object_id *base_commit_id, struct got_repository *repo)
777 const struct got_error *err = NULL;
778 struct got_object_id *yca_id;
780 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
781 commit_id, base_commit_id, repo, check_cancelled, NULL);
782 if (err)
783 return err;
785 if (yca_id == NULL)
786 return got_error(GOT_ERR_ANCESTRY);
788 /*
789 * Require a straight line of history between the target commit
790 * and the work tree's base commit.
792 * Non-linear situations such as this require a rebase:
794 * (commit) D F (base_commit)
795 * \ /
796 * C E
797 * \ /
798 * B (yca)
799 * |
800 * A
802 * 'got update' only handles linear cases:
803 * Update forwards in time: A (base/yca) - B - C - D (commit)
804 * Update backwards in time: D (base) - C - B - A (commit/yca)
805 */
806 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
807 got_object_id_cmp(base_commit_id, yca_id) != 0)
808 return got_error(GOT_ERR_ANCESTRY);
810 free(yca_id);
811 return NULL;
814 static const struct got_error *
815 check_same_branch(struct got_object_id *commit_id,
816 struct got_reference *head_ref, struct got_object_id *yca_id,
817 struct got_repository *repo)
819 const struct got_error *err = NULL;
820 struct got_commit_graph *graph = NULL;
821 struct got_object_id *head_commit_id = NULL;
822 int is_same_branch = 0;
824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
825 if (err)
826 goto done;
828 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
829 is_same_branch = 1;
830 goto done;
832 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
833 is_same_branch = 1;
834 goto done;
837 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
838 if (err)
839 goto done;
841 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
842 check_cancelled, NULL);
843 if (err)
844 goto done;
846 for (;;) {
847 struct got_object_id *id;
848 err = got_commit_graph_iter_next(&id, graph);
849 if (err) {
850 if (err->code == GOT_ERR_ITER_COMPLETED) {
851 err = NULL;
852 break;
853 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
854 break;
855 err = got_commit_graph_fetch_commits(graph, 1,
856 repo, check_cancelled, NULL);
857 if (err)
858 break;
861 if (id) {
862 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
863 break;
864 if (got_object_id_cmp(id, commit_id) == 0) {
865 is_same_branch = 1;
866 break;
870 done:
871 if (graph)
872 got_commit_graph_close(graph);
873 free(head_commit_id);
874 if (!err && !is_same_branch)
875 err = got_error(GOT_ERR_ANCESTRY);
876 return err;
879 static const struct got_error *
880 resolve_commit_arg(struct got_object_id **commit_id,
881 const char *commit_id_arg, struct got_repository *repo)
883 const struct got_error *err;
884 struct got_reference *ref;
885 struct got_tag_object *tag;
887 err = got_repo_object_match_tag(&tag, commit_id_arg,
888 GOT_OBJ_TYPE_COMMIT, repo);
889 if (err == NULL) {
890 *commit_id = got_object_id_dup(
891 got_object_tag_get_object_id(tag));
892 if (*commit_id == NULL)
893 err = got_error_from_errno("got_object_id_dup");
894 got_object_tag_close(tag);
895 return err;
896 } else if (err->code != GOT_ERR_NO_OBJ)
897 return err;
899 err = got_ref_open(&ref, repo, commit_id_arg, 0);
900 if (err == NULL) {
901 err = got_ref_resolve(commit_id, repo, ref);
902 got_ref_close(ref);
903 } else {
904 if (err->code != GOT_ERR_NOT_REF)
905 return err;
906 err = got_repo_match_object_id_prefix(commit_id,
907 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
909 return err;
912 static const struct got_error *
913 cmd_checkout(int argc, char *argv[])
915 const struct got_error *error = NULL;
916 struct got_repository *repo = NULL;
917 struct got_reference *head_ref = NULL;
918 struct got_worktree *worktree = NULL;
919 char *repo_path = NULL;
920 char *worktree_path = NULL;
921 const char *path_prefix = "";
922 const char *branch_name = GOT_REF_HEAD;
923 char *commit_id_str = NULL;
924 int ch, same_path_prefix;
925 struct got_pathlist_head paths;
927 TAILQ_INIT(&paths);
929 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
930 switch (ch) {
931 case 'b':
932 branch_name = optarg;
933 break;
934 case 'c':
935 commit_id_str = strdup(optarg);
936 if (commit_id_str == NULL)
937 return got_error_from_errno("strdup");
938 break;
939 case 'p':
940 path_prefix = optarg;
941 break;
942 default:
943 usage_checkout();
944 /* NOTREACHED */
948 argc -= optind;
949 argv += optind;
951 #ifndef PROFILE
952 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
953 "unveil", NULL) == -1)
954 err(1, "pledge");
955 #endif
956 if (argc == 1) {
957 char *cwd, *base, *dotgit;
958 repo_path = realpath(argv[0], NULL);
959 if (repo_path == NULL)
960 return got_error_from_errno2("realpath", argv[0]);
961 cwd = getcwd(NULL, 0);
962 if (cwd == NULL) {
963 error = got_error_from_errno("getcwd");
964 goto done;
966 if (path_prefix[0]) {
967 base = basename(path_prefix);
968 if (base == NULL) {
969 error = got_error_from_errno2("basename",
970 path_prefix);
971 goto done;
973 } else {
974 base = basename(repo_path);
975 if (base == NULL) {
976 error = got_error_from_errno2("basename",
977 repo_path);
978 goto done;
981 dotgit = strstr(base, ".git");
982 if (dotgit)
983 *dotgit = '\0';
984 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
985 error = got_error_from_errno("asprintf");
986 free(cwd);
987 goto done;
989 free(cwd);
990 } else if (argc == 2) {
991 repo_path = realpath(argv[0], NULL);
992 if (repo_path == NULL) {
993 error = got_error_from_errno2("realpath", argv[0]);
994 goto done;
996 worktree_path = realpath(argv[1], NULL);
997 if (worktree_path == NULL) {
998 if (errno != ENOENT) {
999 error = got_error_from_errno2("realpath",
1000 argv[1]);
1001 goto done;
1003 worktree_path = strdup(argv[1]);
1004 if (worktree_path == NULL) {
1005 error = got_error_from_errno("strdup");
1006 goto done;
1009 } else
1010 usage_checkout();
1012 got_path_strip_trailing_slashes(repo_path);
1013 got_path_strip_trailing_slashes(worktree_path);
1015 error = got_repo_open(&repo, repo_path, NULL);
1016 if (error != NULL)
1017 goto done;
1019 /* Pre-create work tree path for unveil(2) */
1020 error = got_path_mkdir(worktree_path);
1021 if (error) {
1022 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1023 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1024 goto done;
1025 if (!got_path_dir_is_empty(worktree_path)) {
1026 error = got_error_path(worktree_path,
1027 GOT_ERR_DIR_NOT_EMPTY);
1028 goto done;
1032 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1033 if (error)
1034 goto done;
1036 error = got_ref_open(&head_ref, repo, branch_name, 0);
1037 if (error != NULL)
1038 goto done;
1040 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1041 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1042 goto done;
1044 error = got_worktree_open(&worktree, worktree_path);
1045 if (error != NULL)
1046 goto done;
1048 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1049 path_prefix);
1050 if (error != NULL)
1051 goto done;
1052 if (!same_path_prefix) {
1053 error = got_error(GOT_ERR_PATH_PREFIX);
1054 goto done;
1057 if (commit_id_str) {
1058 struct got_object_id *commit_id;
1059 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1060 if (error)
1061 goto done;
1062 error = check_linear_ancestry(commit_id,
1063 got_worktree_get_base_commit_id(worktree), repo);
1064 if (error != NULL) {
1065 free(commit_id);
1066 goto done;
1068 error = check_same_branch(commit_id, head_ref, NULL, repo);
1069 if (error)
1070 goto done;
1071 error = got_worktree_set_base_commit_id(worktree, repo,
1072 commit_id);
1073 free(commit_id);
1074 if (error)
1075 goto done;
1078 error = got_pathlist_append(&paths, "", NULL);
1079 if (error)
1080 goto done;
1081 error = got_worktree_checkout_files(worktree, &paths, repo,
1082 checkout_progress, worktree_path, check_cancelled, NULL);
1083 if (error != NULL)
1084 goto done;
1086 printf("Now shut up and hack\n");
1088 done:
1089 got_pathlist_free(&paths);
1090 free(commit_id_str);
1091 free(repo_path);
1092 free(worktree_path);
1093 return error;
1096 __dead static void
1097 usage_update(void)
1099 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1100 getprogname());
1101 exit(1);
1104 static const struct got_error *
1105 update_progress(void *arg, unsigned char status, const char *path)
1107 int *did_something = arg;
1109 if (status == GOT_STATUS_EXISTS)
1110 return NULL;
1112 *did_something = 1;
1114 /* Base commit bump happens silently. */
1115 if (status == GOT_STATUS_BUMP_BASE)
1116 return NULL;
1118 while (path[0] == '/')
1119 path++;
1120 printf("%c %s\n", status, path);
1121 return NULL;
1124 static const struct got_error *
1125 switch_head_ref(struct got_reference *head_ref,
1126 struct got_object_id *commit_id, struct got_worktree *worktree,
1127 struct got_repository *repo)
1129 const struct got_error *err = NULL;
1130 char *base_id_str;
1131 int ref_has_moved = 0;
1133 /* Trivial case: switching between two different references. */
1134 if (strcmp(got_ref_get_name(head_ref),
1135 got_worktree_get_head_ref_name(worktree)) != 0) {
1136 printf("Switching work tree from %s to %s\n",
1137 got_worktree_get_head_ref_name(worktree),
1138 got_ref_get_name(head_ref));
1139 return got_worktree_set_head_ref(worktree, head_ref);
1142 err = check_linear_ancestry(commit_id,
1143 got_worktree_get_base_commit_id(worktree), repo);
1144 if (err) {
1145 if (err->code != GOT_ERR_ANCESTRY)
1146 return err;
1147 ref_has_moved = 1;
1149 if (!ref_has_moved)
1150 return NULL;
1152 /* Switching to a rebased branch with the same reference name. */
1153 err = got_object_id_str(&base_id_str,
1154 got_worktree_get_base_commit_id(worktree));
1155 if (err)
1156 return err;
1157 printf("Reference %s now points at a different branch\n",
1158 got_worktree_get_head_ref_name(worktree));
1159 printf("Switching work tree from %s to %s\n", base_id_str,
1160 got_worktree_get_head_ref_name(worktree));
1161 return NULL;
1164 static const struct got_error *
1165 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1167 const struct got_error *err;
1168 int in_progress;
1170 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1171 if (err)
1172 return err;
1173 if (in_progress)
1174 return got_error(GOT_ERR_REBASING);
1176 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1177 if (err)
1178 return err;
1179 if (in_progress)
1180 return got_error(GOT_ERR_HISTEDIT_BUSY);
1182 return NULL;
1185 static const struct got_error *
1186 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1187 char *argv[], struct got_worktree *worktree)
1189 const struct got_error *err = NULL;
1190 char *path;
1191 int i;
1193 if (argc == 0) {
1194 path = strdup("");
1195 if (path == NULL)
1196 return got_error_from_errno("strdup");
1197 return got_pathlist_append(paths, path, NULL);
1200 for (i = 0; i < argc; i++) {
1201 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1202 if (err)
1203 break;
1204 err = got_pathlist_append(paths, path, NULL);
1205 if (err) {
1206 free(path);
1207 break;
1211 return err;
1214 static const struct got_error *
1215 cmd_update(int argc, char *argv[])
1217 const struct got_error *error = NULL;
1218 struct got_repository *repo = NULL;
1219 struct got_worktree *worktree = NULL;
1220 char *worktree_path = NULL;
1221 struct got_object_id *commit_id = NULL;
1222 char *commit_id_str = NULL;
1223 const char *branch_name = NULL;
1224 struct got_reference *head_ref = NULL;
1225 struct got_pathlist_head paths;
1226 struct got_pathlist_entry *pe;
1227 int ch, did_something = 0;
1229 TAILQ_INIT(&paths);
1231 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1232 switch (ch) {
1233 case 'b':
1234 branch_name = optarg;
1235 break;
1236 case 'c':
1237 commit_id_str = strdup(optarg);
1238 if (commit_id_str == NULL)
1239 return got_error_from_errno("strdup");
1240 break;
1241 default:
1242 usage_update();
1243 /* NOTREACHED */
1247 argc -= optind;
1248 argv += optind;
1250 #ifndef PROFILE
1251 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1252 "unveil", NULL) == -1)
1253 err(1, "pledge");
1254 #endif
1255 worktree_path = getcwd(NULL, 0);
1256 if (worktree_path == NULL) {
1257 error = got_error_from_errno("getcwd");
1258 goto done;
1260 error = got_worktree_open(&worktree, worktree_path);
1261 if (error)
1262 goto done;
1264 error = check_rebase_or_histedit_in_progress(worktree);
1265 if (error)
1266 goto done;
1268 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1269 NULL);
1270 if (error != NULL)
1271 goto done;
1273 error = apply_unveil(got_repo_get_path(repo), 0,
1274 got_worktree_get_root_path(worktree));
1275 if (error)
1276 goto done;
1278 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1279 if (error)
1280 goto done;
1282 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1283 got_worktree_get_head_ref_name(worktree), 0);
1284 if (error != NULL)
1285 goto done;
1286 if (commit_id_str == NULL) {
1287 error = got_ref_resolve(&commit_id, repo, head_ref);
1288 if (error != NULL)
1289 goto done;
1290 error = got_object_id_str(&commit_id_str, commit_id);
1291 if (error != NULL)
1292 goto done;
1293 } else {
1294 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1295 free(commit_id_str);
1296 commit_id_str = NULL;
1297 if (error)
1298 goto done;
1299 error = got_object_id_str(&commit_id_str, commit_id);
1300 if (error)
1301 goto done;
1304 if (branch_name) {
1305 struct got_object_id *head_commit_id;
1306 TAILQ_FOREACH(pe, &paths, entry) {
1307 if (pe->path_len == 0)
1308 continue;
1309 error = got_error_msg(GOT_ERR_BAD_PATH,
1310 "switching between branches requires that "
1311 "the entire work tree gets updated");
1312 goto done;
1314 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1315 if (error)
1316 goto done;
1317 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1318 free(head_commit_id);
1319 if (error != NULL)
1320 goto done;
1321 error = check_same_branch(commit_id, head_ref, NULL, repo);
1322 if (error)
1323 goto done;
1324 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1325 if (error)
1326 goto done;
1327 } else {
1328 error = check_linear_ancestry(commit_id,
1329 got_worktree_get_base_commit_id(worktree), repo);
1330 if (error != NULL) {
1331 if (error->code == GOT_ERR_ANCESTRY)
1332 error = got_error(GOT_ERR_BRANCH_MOVED);
1333 goto done;
1335 error = check_same_branch(commit_id, head_ref, NULL, repo);
1336 if (error)
1337 goto done;
1340 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1341 commit_id) != 0) {
1342 error = got_worktree_set_base_commit_id(worktree, repo,
1343 commit_id);
1344 if (error)
1345 goto done;
1348 error = got_worktree_checkout_files(worktree, &paths, repo,
1349 update_progress, &did_something, check_cancelled, NULL);
1350 if (error != NULL)
1351 goto done;
1353 if (did_something)
1354 printf("Updated to commit %s\n", commit_id_str);
1355 else
1356 printf("Already up-to-date\n");
1357 done:
1358 free(worktree_path);
1359 TAILQ_FOREACH(pe, &paths, entry)
1360 free((char *)pe->path);
1361 got_pathlist_free(&paths);
1362 free(commit_id);
1363 free(commit_id_str);
1364 return error;
1367 static const struct got_error *
1368 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1369 const char *path, int diff_context, int ignore_whitespace,
1370 struct got_repository *repo)
1372 const struct got_error *err = NULL;
1373 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1375 if (blob_id1) {
1376 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1377 if (err)
1378 goto done;
1381 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1382 if (err)
1383 goto done;
1385 while (path[0] == '/')
1386 path++;
1387 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1388 ignore_whitespace, stdout);
1389 done:
1390 if (blob1)
1391 got_object_blob_close(blob1);
1392 got_object_blob_close(blob2);
1393 return err;
1396 static const struct got_error *
1397 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1398 const char *path, int diff_context, int ignore_whitespace,
1399 struct got_repository *repo)
1401 const struct got_error *err = NULL;
1402 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1403 struct got_diff_blob_output_unidiff_arg arg;
1405 if (tree_id1) {
1406 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1407 if (err)
1408 goto done;
1411 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1412 if (err)
1413 goto done;
1415 arg.diff_context = diff_context;
1416 arg.ignore_whitespace = ignore_whitespace;
1417 arg.outfile = stdout;
1418 while (path[0] == '/')
1419 path++;
1420 err = got_diff_tree(tree1, tree2, path, path, repo,
1421 got_diff_blob_output_unidiff, &arg, 1);
1422 done:
1423 if (tree1)
1424 got_object_tree_close(tree1);
1425 got_object_tree_close(tree2);
1426 return err;
1429 static const struct got_error *
1430 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1431 const char *path, int diff_context, struct got_repository *repo)
1433 const struct got_error *err = NULL;
1434 struct got_commit_object *pcommit = NULL;
1435 char *id_str1 = NULL, *id_str2 = NULL;
1436 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1437 struct got_object_qid *qid;
1439 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1440 if (qid != NULL) {
1441 err = got_object_open_as_commit(&pcommit, repo,
1442 qid->id);
1443 if (err)
1444 return err;
1447 if (path && path[0] != '\0') {
1448 int obj_type;
1449 err = got_object_id_by_path(&obj_id2, repo, id, path);
1450 if (err)
1451 goto done;
1452 err = got_object_id_str(&id_str2, obj_id2);
1453 if (err) {
1454 free(obj_id2);
1455 goto done;
1457 if (pcommit) {
1458 err = got_object_id_by_path(&obj_id1, repo,
1459 qid->id, path);
1460 if (err) {
1461 free(obj_id2);
1462 goto done;
1464 err = got_object_id_str(&id_str1, obj_id1);
1465 if (err) {
1466 free(obj_id2);
1467 goto done;
1470 err = got_object_get_type(&obj_type, repo, obj_id2);
1471 if (err) {
1472 free(obj_id2);
1473 goto done;
1475 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1476 switch (obj_type) {
1477 case GOT_OBJ_TYPE_BLOB:
1478 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1479 0, repo);
1480 break;
1481 case GOT_OBJ_TYPE_TREE:
1482 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1483 0, repo);
1484 break;
1485 default:
1486 err = got_error(GOT_ERR_OBJ_TYPE);
1487 break;
1489 free(obj_id1);
1490 free(obj_id2);
1491 } else {
1492 obj_id2 = got_object_commit_get_tree_id(commit);
1493 err = got_object_id_str(&id_str2, obj_id2);
1494 if (err)
1495 goto done;
1496 obj_id1 = got_object_commit_get_tree_id(pcommit);
1497 err = got_object_id_str(&id_str1, obj_id1);
1498 if (err)
1499 goto done;
1500 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1501 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1504 done:
1505 free(id_str1);
1506 free(id_str2);
1507 if (pcommit)
1508 got_object_commit_close(pcommit);
1509 return err;
1512 static char *
1513 get_datestr(time_t *time, char *datebuf)
1515 struct tm mytm, *tm;
1516 char *p, *s;
1518 tm = gmtime_r(time, &mytm);
1519 if (tm == NULL)
1520 return NULL;
1521 s = asctime_r(tm, datebuf);
1522 if (s == NULL)
1523 return NULL;
1524 p = strchr(s, '\n');
1525 if (p)
1526 *p = '\0';
1527 return s;
1530 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1532 static const struct got_error *
1533 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1534 struct got_repository *repo, const char *path, int show_patch,
1535 int diff_context, struct got_reflist_head *refs)
1537 const struct got_error *err = NULL;
1538 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1539 char datebuf[26];
1540 time_t committer_time;
1541 const char *author, *committer;
1542 char *refs_str = NULL;
1543 struct got_reflist_entry *re;
1545 SIMPLEQ_FOREACH(re, refs, entry) {
1546 char *s;
1547 const char *name;
1548 struct got_tag_object *tag = NULL;
1549 int cmp;
1551 name = got_ref_get_name(re->ref);
1552 if (strcmp(name, GOT_REF_HEAD) == 0)
1553 continue;
1554 if (strncmp(name, "refs/", 5) == 0)
1555 name += 5;
1556 if (strncmp(name, "got/", 4) == 0)
1557 continue;
1558 if (strncmp(name, "heads/", 6) == 0)
1559 name += 6;
1560 if (strncmp(name, "remotes/", 8) == 0)
1561 name += 8;
1562 if (strncmp(name, "tags/", 5) == 0) {
1563 err = got_object_open_as_tag(&tag, repo, re->id);
1564 if (err) {
1565 if (err->code != GOT_ERR_OBJ_TYPE)
1566 return err;
1567 /* Ref points at something other than a tag. */
1568 err = NULL;
1569 tag = NULL;
1572 cmp = got_object_id_cmp(tag ?
1573 got_object_tag_get_object_id(tag) : re->id, id);
1574 if (tag)
1575 got_object_tag_close(tag);
1576 if (cmp != 0)
1577 continue;
1578 s = refs_str;
1579 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1580 name) == -1) {
1581 err = got_error_from_errno("asprintf");
1582 free(s);
1583 return err;
1585 free(s);
1587 err = got_object_id_str(&id_str, id);
1588 if (err)
1589 return err;
1591 printf(GOT_COMMIT_SEP_STR);
1592 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1593 refs_str ? refs_str : "", refs_str ? ")" : "");
1594 free(id_str);
1595 id_str = NULL;
1596 free(refs_str);
1597 refs_str = NULL;
1598 printf("from: %s\n", got_object_commit_get_author(commit));
1599 committer_time = got_object_commit_get_committer_time(commit);
1600 datestr = get_datestr(&committer_time, datebuf);
1601 if (datestr)
1602 printf("date: %s UTC\n", datestr);
1603 author = got_object_commit_get_author(commit);
1604 committer = got_object_commit_get_committer(commit);
1605 if (strcmp(author, committer) != 0)
1606 printf("via: %s\n", committer);
1607 if (got_object_commit_get_nparents(commit) > 1) {
1608 const struct got_object_id_queue *parent_ids;
1609 struct got_object_qid *qid;
1610 int n = 1;
1611 parent_ids = got_object_commit_get_parent_ids(commit);
1612 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1613 err = got_object_id_str(&id_str, qid->id);
1614 if (err)
1615 return err;
1616 printf("parent %d: %s\n", n++, id_str);
1617 free(id_str);
1621 err = got_object_commit_get_logmsg(&logmsg0, commit);
1622 if (err)
1623 return err;
1625 logmsg = logmsg0;
1626 do {
1627 line = strsep(&logmsg, "\n");
1628 if (line)
1629 printf(" %s\n", line);
1630 } while (line);
1631 free(logmsg0);
1633 if (show_patch) {
1634 err = print_patch(commit, id, path, diff_context, repo);
1635 if (err == 0)
1636 printf("\n");
1639 if (fflush(stdout) != 0 && err == NULL)
1640 err = got_error_from_errno("fflush");
1641 return err;
1644 static const struct got_error *
1645 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1646 char *path, int show_patch, int diff_context, int limit,
1647 int first_parent_traversal, struct got_reflist_head *refs)
1649 const struct got_error *err;
1650 struct got_commit_graph *graph;
1652 err = got_commit_graph_open(&graph, root_id, path,
1653 first_parent_traversal, repo);
1654 if (err)
1655 return err;
1656 err = got_commit_graph_iter_start(graph, root_id, repo,
1657 check_cancelled, NULL);
1658 if (err)
1659 goto done;
1660 for (;;) {
1661 struct got_commit_object *commit;
1662 struct got_object_id *id;
1664 if (sigint_received || sigpipe_received)
1665 break;
1667 err = got_commit_graph_iter_next(&id, graph);
1668 if (err) {
1669 if (err->code == GOT_ERR_ITER_COMPLETED) {
1670 err = NULL;
1671 break;
1673 if (err->code != GOT_ERR_ITER_NEED_MORE)
1674 break;
1675 err = got_commit_graph_fetch_commits(graph, 1, repo,
1676 check_cancelled, NULL);
1677 if (err)
1678 break;
1679 else
1680 continue;
1682 if (id == NULL)
1683 break;
1685 err = got_object_open_as_commit(&commit, repo, id);
1686 if (err)
1687 break;
1688 err = print_commit(commit, id, repo, path, show_patch,
1689 diff_context, refs);
1690 got_object_commit_close(commit);
1691 if (err || (limit && --limit == 0))
1692 break;
1694 done:
1695 got_commit_graph_close(graph);
1696 return err;
1699 __dead static void
1700 usage_log(void)
1702 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1703 "[-r repository-path] [path]\n", getprogname());
1704 exit(1);
1707 static int
1708 get_default_log_limit(void)
1710 const char *got_default_log_limit;
1711 long long n;
1712 const char *errstr;
1714 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1715 if (got_default_log_limit == NULL)
1716 return 0;
1717 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1718 if (errstr != NULL)
1719 return 0;
1720 return n;
1723 static const struct got_error *
1724 cmd_log(int argc, char *argv[])
1726 const struct got_error *error;
1727 struct got_repository *repo = NULL;
1728 struct got_worktree *worktree = NULL;
1729 struct got_commit_object *commit = NULL;
1730 struct got_object_id *id = NULL;
1731 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1732 char *start_commit = NULL;
1733 int diff_context = 3, ch;
1734 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1735 const char *errstr;
1736 struct got_reflist_head refs;
1738 SIMPLEQ_INIT(&refs);
1740 #ifndef PROFILE
1741 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1742 NULL)
1743 == -1)
1744 err(1, "pledge");
1745 #endif
1747 limit = get_default_log_limit();
1749 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1750 switch (ch) {
1751 case 'p':
1752 show_patch = 1;
1753 break;
1754 case 'c':
1755 start_commit = optarg;
1756 break;
1757 case 'C':
1758 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1759 &errstr);
1760 if (errstr != NULL)
1761 err(1, "-C option %s", errstr);
1762 break;
1763 case 'l':
1764 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1765 if (errstr != NULL)
1766 err(1, "-l option %s", errstr);
1767 break;
1768 case 'f':
1769 first_parent_traversal = 1;
1770 break;
1771 case 'r':
1772 repo_path = realpath(optarg, NULL);
1773 if (repo_path == NULL)
1774 err(1, "-r option");
1775 got_path_strip_trailing_slashes(repo_path);
1776 break;
1777 default:
1778 usage_log();
1779 /* NOTREACHED */
1783 argc -= optind;
1784 argv += optind;
1786 cwd = getcwd(NULL, 0);
1787 if (cwd == NULL) {
1788 error = got_error_from_errno("getcwd");
1789 goto done;
1792 error = got_worktree_open(&worktree, cwd);
1793 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1794 goto done;
1795 error = NULL;
1797 if (argc == 0) {
1798 path = strdup("");
1799 if (path == NULL) {
1800 error = got_error_from_errno("strdup");
1801 goto done;
1803 } else if (argc == 1) {
1804 if (worktree) {
1805 error = got_worktree_resolve_path(&path, worktree,
1806 argv[0]);
1807 if (error)
1808 goto done;
1809 } else {
1810 path = strdup(argv[0]);
1811 if (path == NULL) {
1812 error = got_error_from_errno("strdup");
1813 goto done;
1816 } else
1817 usage_log();
1819 if (repo_path == NULL) {
1820 repo_path = worktree ?
1821 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1823 if (repo_path == NULL) {
1824 error = got_error_from_errno("strdup");
1825 goto done;
1828 error = got_repo_open(&repo, repo_path, NULL);
1829 if (error != NULL)
1830 goto done;
1832 error = apply_unveil(got_repo_get_path(repo), 1,
1833 worktree ? got_worktree_get_root_path(worktree) : NULL);
1834 if (error)
1835 goto done;
1837 if (start_commit == NULL) {
1838 struct got_reference *head_ref;
1839 error = got_ref_open(&head_ref, repo,
1840 worktree ? got_worktree_get_head_ref_name(worktree)
1841 : GOT_REF_HEAD, 0);
1842 if (error != NULL)
1843 return error;
1844 error = got_ref_resolve(&id, repo, head_ref);
1845 got_ref_close(head_ref);
1846 if (error != NULL)
1847 return error;
1848 error = got_object_open_as_commit(&commit, repo, id);
1849 } else {
1850 struct got_reference *ref;
1851 error = got_ref_open(&ref, repo, start_commit, 0);
1852 if (error == NULL) {
1853 int obj_type;
1854 error = got_ref_resolve(&id, repo, ref);
1855 got_ref_close(ref);
1856 if (error != NULL)
1857 goto done;
1858 error = got_object_get_type(&obj_type, repo, id);
1859 if (error != NULL)
1860 goto done;
1861 if (obj_type == GOT_OBJ_TYPE_TAG) {
1862 struct got_tag_object *tag;
1863 error = got_object_open_as_tag(&tag, repo, id);
1864 if (error != NULL)
1865 goto done;
1866 if (got_object_tag_get_object_type(tag) !=
1867 GOT_OBJ_TYPE_COMMIT) {
1868 got_object_tag_close(tag);
1869 error = got_error(GOT_ERR_OBJ_TYPE);
1870 goto done;
1872 free(id);
1873 id = got_object_id_dup(
1874 got_object_tag_get_object_id(tag));
1875 if (id == NULL)
1876 error = got_error_from_errno(
1877 "got_object_id_dup");
1878 got_object_tag_close(tag);
1879 if (error)
1880 goto done;
1881 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1882 error = got_error(GOT_ERR_OBJ_TYPE);
1883 goto done;
1885 error = got_object_open_as_commit(&commit, repo, id);
1886 if (error != NULL)
1887 goto done;
1889 if (commit == NULL) {
1890 error = got_repo_match_object_id_prefix(&id,
1891 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1892 if (error != NULL)
1893 return error;
1896 if (error != NULL)
1897 goto done;
1899 if (worktree) {
1900 const char *prefix = got_worktree_get_path_prefix(worktree);
1901 char *p;
1902 if (asprintf(&p, "%s%s%s", prefix,
1903 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1904 error = got_error_from_errno("asprintf");
1905 goto done;
1907 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1908 free(p);
1909 } else
1910 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1911 if (error != NULL)
1912 goto done;
1913 if (in_repo_path) {
1914 free(path);
1915 path = in_repo_path;
1918 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1919 if (error)
1920 goto done;
1922 error = print_commits(id, repo, path, show_patch,
1923 diff_context, limit, first_parent_traversal, &refs);
1924 done:
1925 free(path);
1926 free(repo_path);
1927 free(cwd);
1928 free(id);
1929 if (worktree)
1930 got_worktree_close(worktree);
1931 if (repo) {
1932 const struct got_error *repo_error;
1933 repo_error = got_repo_close(repo);
1934 if (error == NULL)
1935 error = repo_error;
1937 got_ref_list_free(&refs);
1938 return error;
1941 __dead static void
1942 usage_diff(void)
1944 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1945 "[-w] [object1 object2 | path]\n", getprogname());
1946 exit(1);
1949 struct print_diff_arg {
1950 struct got_repository *repo;
1951 struct got_worktree *worktree;
1952 int diff_context;
1953 const char *id_str;
1954 int header_shown;
1955 int diff_staged;
1956 int ignore_whitespace;
1959 static const struct got_error *
1960 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1961 const char *path, struct got_object_id *blob_id,
1962 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1964 struct print_diff_arg *a = arg;
1965 const struct got_error *err = NULL;
1966 struct got_blob_object *blob1 = NULL;
1967 FILE *f2 = NULL;
1968 char *abspath = NULL, *label1 = NULL;
1969 struct stat sb;
1971 if (a->diff_staged) {
1972 if (staged_status != GOT_STATUS_MODIFY &&
1973 staged_status != GOT_STATUS_ADD &&
1974 staged_status != GOT_STATUS_DELETE)
1975 return NULL;
1976 } else {
1977 if (staged_status == GOT_STATUS_DELETE)
1978 return NULL;
1979 if (status == GOT_STATUS_NONEXISTENT)
1980 return got_error_set_errno(ENOENT, path);
1981 if (status != GOT_STATUS_MODIFY &&
1982 status != GOT_STATUS_ADD &&
1983 status != GOT_STATUS_DELETE &&
1984 status != GOT_STATUS_CONFLICT)
1985 return NULL;
1988 if (!a->header_shown) {
1989 printf("diff %s %s%s\n", a->id_str,
1990 got_worktree_get_root_path(a->worktree),
1991 a->diff_staged ? " (staged changes)" : "");
1992 a->header_shown = 1;
1995 if (a->diff_staged) {
1996 const char *label1 = NULL, *label2 = NULL;
1997 switch (staged_status) {
1998 case GOT_STATUS_MODIFY:
1999 label1 = path;
2000 label2 = path;
2001 break;
2002 case GOT_STATUS_ADD:
2003 label2 = path;
2004 break;
2005 case GOT_STATUS_DELETE:
2006 label1 = path;
2007 break;
2008 default:
2009 return got_error(GOT_ERR_FILE_STATUS);
2011 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2012 label1, label2, a->diff_context, a->ignore_whitespace,
2013 a->repo, stdout);
2016 if (staged_status == GOT_STATUS_ADD ||
2017 staged_status == GOT_STATUS_MODIFY) {
2018 char *id_str;
2019 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2020 8192);
2021 if (err)
2022 goto done;
2023 err = got_object_id_str(&id_str, staged_blob_id);
2024 if (err)
2025 goto done;
2026 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2027 err = got_error_from_errno("asprintf");
2028 free(id_str);
2029 goto done;
2031 free(id_str);
2032 } else if (status != GOT_STATUS_ADD) {
2033 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2034 if (err)
2035 goto done;
2038 if (status != GOT_STATUS_DELETE) {
2039 if (asprintf(&abspath, "%s/%s",
2040 got_worktree_get_root_path(a->worktree), path) == -1) {
2041 err = got_error_from_errno("asprintf");
2042 goto done;
2045 f2 = fopen(abspath, "r");
2046 if (f2 == NULL) {
2047 err = got_error_from_errno2("fopen", abspath);
2048 goto done;
2050 if (lstat(abspath, &sb) == -1) {
2051 err = got_error_from_errno2("lstat", abspath);
2052 goto done;
2054 } else
2055 sb.st_size = 0;
2057 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2058 a->diff_context, a->ignore_whitespace, stdout);
2059 done:
2060 if (blob1)
2061 got_object_blob_close(blob1);
2062 if (f2 && fclose(f2) != 0 && err == NULL)
2063 err = got_error_from_errno("fclose");
2064 free(abspath);
2065 return err;
2068 static const struct got_error *
2069 match_object_id(struct got_object_id **id, char **label,
2070 const char *id_str, int obj_type, int resolve_tags,
2071 struct got_repository *repo)
2073 const struct got_error *err;
2074 struct got_tag_object *tag;
2075 struct got_reference *ref = NULL;
2077 *id = NULL;
2078 *label = NULL;
2080 if (resolve_tags) {
2081 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2082 repo);
2083 if (err == NULL) {
2084 *id = got_object_id_dup(
2085 got_object_tag_get_object_id(tag));
2086 if (*id == NULL)
2087 err = got_error_from_errno("got_object_id_dup");
2088 else if (asprintf(label, "refs/tags/%s",
2089 got_object_tag_get_name(tag)) == -1) {
2090 err = got_error_from_errno("asprintf");
2091 free(*id);
2092 *id = NULL;
2094 got_object_tag_close(tag);
2095 return err;
2096 } else if (err->code != GOT_ERR_NO_OBJ)
2097 return err;
2100 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2101 if (err) {
2102 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2103 return err;
2104 err = got_ref_open(&ref, repo, id_str, 0);
2105 if (err != NULL)
2106 goto done;
2107 *label = strdup(got_ref_get_name(ref));
2108 if (*label == NULL) {
2109 err = got_error_from_errno("strdup");
2110 goto done;
2112 err = got_ref_resolve(id, repo, ref);
2113 } else {
2114 err = got_object_id_str(label, *id);
2115 if (*label == NULL) {
2116 err = got_error_from_errno("strdup");
2117 goto done;
2120 done:
2121 if (ref)
2122 got_ref_close(ref);
2123 return err;
2127 static const struct got_error *
2128 cmd_diff(int argc, char *argv[])
2130 const struct got_error *error;
2131 struct got_repository *repo = NULL;
2132 struct got_worktree *worktree = NULL;
2133 char *cwd = NULL, *repo_path = NULL;
2134 struct got_object_id *id1 = NULL, *id2 = NULL;
2135 const char *id_str1 = NULL, *id_str2 = NULL;
2136 char *label1 = NULL, *label2 = NULL;
2137 int type1, type2;
2138 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2139 const char *errstr;
2140 char *path = NULL;
2142 #ifndef PROFILE
2143 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2144 NULL) == -1)
2145 err(1, "pledge");
2146 #endif
2148 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2149 switch (ch) {
2150 case 'C':
2151 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2152 if (errstr != NULL)
2153 err(1, "-C option %s", errstr);
2154 break;
2155 case 'r':
2156 repo_path = realpath(optarg, NULL);
2157 if (repo_path == NULL)
2158 err(1, "-r option");
2159 got_path_strip_trailing_slashes(repo_path);
2160 break;
2161 case 's':
2162 diff_staged = 1;
2163 break;
2164 case 'w':
2165 ignore_whitespace = 1;
2166 break;
2167 default:
2168 usage_diff();
2169 /* NOTREACHED */
2173 argc -= optind;
2174 argv += optind;
2176 cwd = getcwd(NULL, 0);
2177 if (cwd == NULL) {
2178 error = got_error_from_errno("getcwd");
2179 goto done;
2181 error = got_worktree_open(&worktree, cwd);
2182 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2183 goto done;
2184 if (argc <= 1) {
2185 if (worktree == NULL) {
2186 error = got_error(GOT_ERR_NOT_WORKTREE);
2187 goto done;
2189 if (repo_path)
2190 errx(1,
2191 "-r option can't be used when diffing a work tree");
2192 repo_path = strdup(got_worktree_get_repo_path(worktree));
2193 if (repo_path == NULL) {
2194 error = got_error_from_errno("strdup");
2195 goto done;
2197 if (argc == 1) {
2198 error = got_worktree_resolve_path(&path, worktree,
2199 argv[0]);
2200 if (error)
2201 goto done;
2202 } else {
2203 path = strdup("");
2204 if (path == NULL) {
2205 error = got_error_from_errno("strdup");
2206 goto done;
2209 } else if (argc == 2) {
2210 if (diff_staged)
2211 errx(1, "-s option can't be used when diffing "
2212 "objects in repository");
2213 id_str1 = argv[0];
2214 id_str2 = argv[1];
2215 if (worktree && repo_path == NULL) {
2216 repo_path =
2217 strdup(got_worktree_get_repo_path(worktree));
2218 if (repo_path == NULL) {
2219 error = got_error_from_errno("strdup");
2220 goto done;
2223 } else
2224 usage_diff();
2226 if (repo_path == NULL) {
2227 repo_path = getcwd(NULL, 0);
2228 if (repo_path == NULL)
2229 return got_error_from_errno("getcwd");
2232 error = got_repo_open(&repo, repo_path, NULL);
2233 free(repo_path);
2234 if (error != NULL)
2235 goto done;
2237 error = apply_unveil(got_repo_get_path(repo), 1,
2238 worktree ? got_worktree_get_root_path(worktree) : NULL);
2239 if (error)
2240 goto done;
2242 if (argc <= 1) {
2243 struct print_diff_arg arg;
2244 struct got_pathlist_head paths;
2245 char *id_str;
2247 TAILQ_INIT(&paths);
2249 error = got_object_id_str(&id_str,
2250 got_worktree_get_base_commit_id(worktree));
2251 if (error)
2252 goto done;
2253 arg.repo = repo;
2254 arg.worktree = worktree;
2255 arg.diff_context = diff_context;
2256 arg.id_str = id_str;
2257 arg.header_shown = 0;
2258 arg.diff_staged = diff_staged;
2259 arg.ignore_whitespace = ignore_whitespace;
2261 error = got_pathlist_append(&paths, path, NULL);
2262 if (error)
2263 goto done;
2265 error = got_worktree_status(worktree, &paths, repo, print_diff,
2266 &arg, check_cancelled, NULL);
2267 free(id_str);
2268 got_pathlist_free(&paths);
2269 goto done;
2272 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2273 repo);
2274 if (error)
2275 goto done;
2277 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2278 repo);
2279 if (error)
2280 goto done;
2282 error = got_object_get_type(&type1, repo, id1);
2283 if (error)
2284 goto done;
2286 error = got_object_get_type(&type2, repo, id2);
2287 if (error)
2288 goto done;
2290 if (type1 != type2) {
2291 error = got_error(GOT_ERR_OBJ_TYPE);
2292 goto done;
2295 switch (type1) {
2296 case GOT_OBJ_TYPE_BLOB:
2297 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2298 diff_context, ignore_whitespace, repo, stdout);
2299 break;
2300 case GOT_OBJ_TYPE_TREE:
2301 error = got_diff_objects_as_trees(id1, id2, "", "",
2302 diff_context, ignore_whitespace, repo, stdout);
2303 break;
2304 case GOT_OBJ_TYPE_COMMIT:
2305 printf("diff %s %s\n", label1, label2);
2306 error = got_diff_objects_as_commits(id1, id2, diff_context,
2307 ignore_whitespace, repo, stdout);
2308 break;
2309 default:
2310 error = got_error(GOT_ERR_OBJ_TYPE);
2313 done:
2314 free(label1);
2315 free(label2);
2316 free(id1);
2317 free(id2);
2318 free(path);
2319 if (worktree)
2320 got_worktree_close(worktree);
2321 if (repo) {
2322 const struct got_error *repo_error;
2323 repo_error = got_repo_close(repo);
2324 if (error == NULL)
2325 error = repo_error;
2327 return error;
2330 __dead static void
2331 usage_blame(void)
2333 fprintf(stderr,
2334 "usage: %s blame [-c commit] [-r repository-path] path\n",
2335 getprogname());
2336 exit(1);
2339 struct blame_line {
2340 int annotated;
2341 char *id_str;
2342 char *committer;
2343 char datebuf[9]; /* YY-MM-DD + NUL */
2346 struct blame_cb_args {
2347 struct blame_line *lines;
2348 int nlines;
2349 int nlines_prec;
2350 int lineno_cur;
2351 off_t *line_offsets;
2352 FILE *f;
2353 struct got_repository *repo;
2356 static const struct got_error *
2357 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2359 const struct got_error *err = NULL;
2360 struct blame_cb_args *a = arg;
2361 struct blame_line *bline;
2362 char *line = NULL;
2363 size_t linesize = 0;
2364 struct got_commit_object *commit = NULL;
2365 off_t offset;
2366 struct tm tm;
2367 time_t committer_time;
2369 if (nlines != a->nlines ||
2370 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2371 return got_error(GOT_ERR_RANGE);
2373 if (sigint_received)
2374 return got_error(GOT_ERR_ITER_COMPLETED);
2376 if (lineno == -1)
2377 return NULL; /* no change in this commit */
2379 /* Annotate this line. */
2380 bline = &a->lines[lineno - 1];
2381 if (bline->annotated)
2382 return NULL;
2383 err = got_object_id_str(&bline->id_str, id);
2384 if (err)
2385 return err;
2387 err = got_object_open_as_commit(&commit, a->repo, id);
2388 if (err)
2389 goto done;
2391 bline->committer = strdup(got_object_commit_get_committer(commit));
2392 if (bline->committer == NULL) {
2393 err = got_error_from_errno("strdup");
2394 goto done;
2397 committer_time = got_object_commit_get_committer_time(commit);
2398 if (localtime_r(&committer_time, &tm) == NULL)
2399 return got_error_from_errno("localtime_r");
2400 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2401 &tm) >= sizeof(bline->datebuf)) {
2402 err = got_error(GOT_ERR_NO_SPACE);
2403 goto done;
2405 bline->annotated = 1;
2407 /* Print lines annotated so far. */
2408 bline = &a->lines[a->lineno_cur - 1];
2409 if (!bline->annotated)
2410 goto done;
2412 offset = a->line_offsets[a->lineno_cur - 1];
2413 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2414 err = got_error_from_errno("fseeko");
2415 goto done;
2418 while (bline->annotated) {
2419 char *smallerthan, *at, *nl, *committer;
2420 size_t len;
2422 if (getline(&line, &linesize, a->f) == -1) {
2423 if (ferror(a->f))
2424 err = got_error_from_errno("getline");
2425 break;
2428 committer = bline->committer;
2429 smallerthan = strchr(committer, '<');
2430 if (smallerthan && smallerthan[1] != '\0')
2431 committer = smallerthan + 1;
2432 at = strchr(committer, '@');
2433 if (at)
2434 *at = '\0';
2435 len = strlen(committer);
2436 if (len >= 9)
2437 committer[8] = '\0';
2439 nl = strchr(line, '\n');
2440 if (nl)
2441 *nl = '\0';
2442 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2443 bline->id_str, bline->datebuf, committer, line);
2445 a->lineno_cur++;
2446 bline = &a->lines[a->lineno_cur - 1];
2448 done:
2449 if (commit)
2450 got_object_commit_close(commit);
2451 free(line);
2452 return err;
2455 static const struct got_error *
2456 cmd_blame(int argc, char *argv[])
2458 const struct got_error *error;
2459 struct got_repository *repo = NULL;
2460 struct got_worktree *worktree = NULL;
2461 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2462 struct got_object_id *obj_id = NULL;
2463 struct got_object_id *commit_id = NULL;
2464 struct got_blob_object *blob = NULL;
2465 char *commit_id_str = NULL;
2466 struct blame_cb_args bca;
2467 int ch, obj_type, i;
2468 size_t filesize;
2470 memset(&bca, 0, sizeof(bca));
2472 #ifndef PROFILE
2473 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2474 NULL) == -1)
2475 err(1, "pledge");
2476 #endif
2478 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2479 switch (ch) {
2480 case 'c':
2481 commit_id_str = optarg;
2482 break;
2483 case 'r':
2484 repo_path = realpath(optarg, NULL);
2485 if (repo_path == NULL)
2486 err(1, "-r option");
2487 got_path_strip_trailing_slashes(repo_path);
2488 break;
2489 default:
2490 usage_blame();
2491 /* NOTREACHED */
2495 argc -= optind;
2496 argv += optind;
2498 if (argc == 1)
2499 path = argv[0];
2500 else
2501 usage_blame();
2503 cwd = getcwd(NULL, 0);
2504 if (cwd == NULL) {
2505 error = got_error_from_errno("getcwd");
2506 goto done;
2508 if (repo_path == NULL) {
2509 error = got_worktree_open(&worktree, cwd);
2510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2511 goto done;
2512 else
2513 error = NULL;
2514 if (worktree) {
2515 repo_path =
2516 strdup(got_worktree_get_repo_path(worktree));
2517 if (repo_path == NULL)
2518 error = got_error_from_errno("strdup");
2519 if (error)
2520 goto done;
2521 } else {
2522 repo_path = strdup(cwd);
2523 if (repo_path == NULL) {
2524 error = got_error_from_errno("strdup");
2525 goto done;
2530 error = got_repo_open(&repo, repo_path, NULL);
2531 if (error != NULL)
2532 goto done;
2534 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2535 if (error)
2536 goto done;
2538 if (worktree) {
2539 const char *prefix = got_worktree_get_path_prefix(worktree);
2540 char *p, *worktree_subdir = cwd +
2541 strlen(got_worktree_get_root_path(worktree));
2542 if (asprintf(&p, "%s%s%s%s%s",
2543 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2544 worktree_subdir, worktree_subdir[0] ? "/" : "",
2545 path) == -1) {
2546 error = got_error_from_errno("asprintf");
2547 goto done;
2549 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2550 free(p);
2551 } else {
2552 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2554 if (error)
2555 goto done;
2557 if (commit_id_str == NULL) {
2558 struct got_reference *head_ref;
2559 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2560 if (error != NULL)
2561 goto done;
2562 error = got_ref_resolve(&commit_id, repo, head_ref);
2563 got_ref_close(head_ref);
2564 if (error != NULL)
2565 goto done;
2566 } else {
2567 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2568 if (error)
2569 goto done;
2572 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2573 if (error)
2574 goto done;
2575 if (obj_id == NULL) {
2576 error = got_error(GOT_ERR_NO_OBJ);
2577 goto done;
2580 error = got_object_get_type(&obj_type, repo, obj_id);
2581 if (error)
2582 goto done;
2584 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2585 error = got_error(GOT_ERR_OBJ_TYPE);
2586 goto done;
2589 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2590 if (error)
2591 goto done;
2592 bca.f = got_opentemp();
2593 if (bca.f == NULL) {
2594 error = got_error_from_errno("got_opentemp");
2595 goto done;
2597 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2598 &bca.line_offsets, bca.f, blob);
2599 if (error || bca.nlines == 0)
2600 goto done;
2602 /* Don't include \n at EOF in the blame line count. */
2603 if (bca.line_offsets[bca.nlines - 1] == filesize)
2604 bca.nlines--;
2606 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2607 if (bca.lines == NULL) {
2608 error = got_error_from_errno("calloc");
2609 goto done;
2611 bca.lineno_cur = 1;
2612 bca.nlines_prec = 0;
2613 i = bca.nlines;
2614 while (i > 0) {
2615 i /= 10;
2616 bca.nlines_prec++;
2618 bca.repo = repo;
2620 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2621 check_cancelled, NULL);
2622 if (error)
2623 goto done;
2624 done:
2625 free(in_repo_path);
2626 free(repo_path);
2627 free(cwd);
2628 free(commit_id);
2629 free(obj_id);
2630 if (blob)
2631 got_object_blob_close(blob);
2632 if (worktree)
2633 got_worktree_close(worktree);
2634 if (repo) {
2635 const struct got_error *repo_error;
2636 repo_error = got_repo_close(repo);
2637 if (error == NULL)
2638 error = repo_error;
2640 if (bca.lines) {
2641 for (i = 0; i < bca.nlines; i++) {
2642 struct blame_line *bline = &bca.lines[i];
2643 free(bline->id_str);
2644 free(bline->committer);
2646 free(bca.lines);
2648 free(bca.line_offsets);
2649 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2650 error = got_error_from_errno("fclose");
2651 return error;
2654 __dead static void
2655 usage_tree(void)
2657 fprintf(stderr,
2658 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2659 getprogname());
2660 exit(1);
2663 static void
2664 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2665 const char *root_path)
2667 int is_root_path = (strcmp(path, root_path) == 0);
2668 const char *modestr = "";
2670 path += strlen(root_path);
2671 while (path[0] == '/')
2672 path++;
2674 if (got_object_tree_entry_is_submodule(te))
2675 modestr = "$";
2676 else if (S_ISLNK(te->mode))
2677 modestr = "@";
2678 else if (S_ISDIR(te->mode))
2679 modestr = "/";
2680 else if (te->mode & S_IXUSR)
2681 modestr = "*";
2683 printf("%s%s%s%s%s\n", id ? id : "", path,
2684 is_root_path ? "" : "/", te->name, modestr);
2687 static const struct got_error *
2688 print_tree(const char *path, struct got_object_id *commit_id,
2689 int show_ids, int recurse, const char *root_path,
2690 struct got_repository *repo)
2692 const struct got_error *err = NULL;
2693 struct got_object_id *tree_id = NULL;
2694 struct got_tree_object *tree = NULL;
2695 const struct got_tree_entries *entries;
2696 struct got_tree_entry *te;
2698 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2699 if (err)
2700 goto done;
2702 err = got_object_open_as_tree(&tree, repo, tree_id);
2703 if (err)
2704 goto done;
2705 entries = got_object_tree_get_entries(tree);
2706 te = SIMPLEQ_FIRST(&entries->head);
2707 while (te) {
2708 char *id = NULL;
2710 if (sigint_received || sigpipe_received)
2711 break;
2713 if (show_ids) {
2714 char *id_str;
2715 err = got_object_id_str(&id_str, te->id);
2716 if (err)
2717 goto done;
2718 if (asprintf(&id, "%s ", id_str) == -1) {
2719 err = got_error_from_errno("asprintf");
2720 free(id_str);
2721 goto done;
2723 free(id_str);
2725 print_entry(te, id, path, root_path);
2726 free(id);
2728 if (recurse && S_ISDIR(te->mode)) {
2729 char *child_path;
2730 if (asprintf(&child_path, "%s%s%s", path,
2731 path[0] == '/' && path[1] == '\0' ? "" : "/",
2732 te->name) == -1) {
2733 err = got_error_from_errno("asprintf");
2734 goto done;
2736 err = print_tree(child_path, commit_id, show_ids, 1,
2737 root_path, repo);
2738 free(child_path);
2739 if (err)
2740 goto done;
2743 te = SIMPLEQ_NEXT(te, entry);
2745 done:
2746 if (tree)
2747 got_object_tree_close(tree);
2748 free(tree_id);
2749 return err;
2752 static const struct got_error *
2753 cmd_tree(int argc, char *argv[])
2755 const struct got_error *error;
2756 struct got_repository *repo = NULL;
2757 struct got_worktree *worktree = NULL;
2758 const char *path;
2759 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2760 struct got_object_id *commit_id = NULL;
2761 char *commit_id_str = NULL;
2762 int show_ids = 0, recurse = 0;
2763 int ch;
2765 #ifndef PROFILE
2766 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2767 NULL) == -1)
2768 err(1, "pledge");
2769 #endif
2771 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2772 switch (ch) {
2773 case 'c':
2774 commit_id_str = optarg;
2775 break;
2776 case 'r':
2777 repo_path = realpath(optarg, NULL);
2778 if (repo_path == NULL)
2779 err(1, "-r option");
2780 got_path_strip_trailing_slashes(repo_path);
2781 break;
2782 case 'i':
2783 show_ids = 1;
2784 break;
2785 case 'R':
2786 recurse = 1;
2787 break;
2788 default:
2789 usage_tree();
2790 /* NOTREACHED */
2794 argc -= optind;
2795 argv += optind;
2797 if (argc == 1)
2798 path = argv[0];
2799 else if (argc > 1)
2800 usage_tree();
2801 else
2802 path = NULL;
2804 cwd = getcwd(NULL, 0);
2805 if (cwd == NULL) {
2806 error = got_error_from_errno("getcwd");
2807 goto done;
2809 if (repo_path == NULL) {
2810 error = got_worktree_open(&worktree, cwd);
2811 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2812 goto done;
2813 else
2814 error = NULL;
2815 if (worktree) {
2816 repo_path =
2817 strdup(got_worktree_get_repo_path(worktree));
2818 if (repo_path == NULL)
2819 error = got_error_from_errno("strdup");
2820 if (error)
2821 goto done;
2822 } else {
2823 repo_path = strdup(cwd);
2824 if (repo_path == NULL) {
2825 error = got_error_from_errno("strdup");
2826 goto done;
2831 error = got_repo_open(&repo, repo_path, NULL);
2832 if (error != NULL)
2833 goto done;
2835 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2836 if (error)
2837 goto done;
2839 if (path == NULL) {
2840 if (worktree) {
2841 char *p, *worktree_subdir = cwd +
2842 strlen(got_worktree_get_root_path(worktree));
2843 if (asprintf(&p, "%s/%s",
2844 got_worktree_get_path_prefix(worktree),
2845 worktree_subdir) == -1) {
2846 error = got_error_from_errno("asprintf");
2847 goto done;
2849 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2850 free(p);
2851 if (error)
2852 goto done;
2853 } else
2854 path = "/";
2856 if (in_repo_path == NULL) {
2857 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2858 if (error != NULL)
2859 goto done;
2862 if (commit_id_str == NULL) {
2863 struct got_reference *head_ref;
2864 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2865 if (error != NULL)
2866 goto done;
2867 error = got_ref_resolve(&commit_id, repo, head_ref);
2868 got_ref_close(head_ref);
2869 if (error != NULL)
2870 goto done;
2871 } else {
2872 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2873 if (error)
2874 goto done;
2877 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2878 in_repo_path, repo);
2879 done:
2880 free(in_repo_path);
2881 free(repo_path);
2882 free(cwd);
2883 free(commit_id);
2884 if (worktree)
2885 got_worktree_close(worktree);
2886 if (repo) {
2887 const struct got_error *repo_error;
2888 repo_error = got_repo_close(repo);
2889 if (error == NULL)
2890 error = repo_error;
2892 return error;
2895 __dead static void
2896 usage_status(void)
2898 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2899 exit(1);
2902 static const struct got_error *
2903 print_status(void *arg, unsigned char status, unsigned char staged_status,
2904 const char *path, struct got_object_id *blob_id,
2905 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2907 if (status == staged_status && (status == GOT_STATUS_DELETE))
2908 status = GOT_STATUS_NO_CHANGE;
2909 printf("%c%c %s\n", status, staged_status, path);
2910 return NULL;
2913 static const struct got_error *
2914 cmd_status(int argc, char *argv[])
2916 const struct got_error *error = NULL;
2917 struct got_repository *repo = NULL;
2918 struct got_worktree *worktree = NULL;
2919 char *cwd = NULL;
2920 struct got_pathlist_head paths;
2921 struct got_pathlist_entry *pe;
2922 int ch;
2924 TAILQ_INIT(&paths);
2926 while ((ch = getopt(argc, argv, "")) != -1) {
2927 switch (ch) {
2928 default:
2929 usage_status();
2930 /* NOTREACHED */
2934 argc -= optind;
2935 argv += optind;
2937 #ifndef PROFILE
2938 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2939 NULL) == -1)
2940 err(1, "pledge");
2941 #endif
2942 cwd = getcwd(NULL, 0);
2943 if (cwd == NULL) {
2944 error = got_error_from_errno("getcwd");
2945 goto done;
2948 error = got_worktree_open(&worktree, cwd);
2949 if (error != NULL)
2950 goto done;
2952 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2953 NULL);
2954 if (error != NULL)
2955 goto done;
2957 error = apply_unveil(got_repo_get_path(repo), 1,
2958 got_worktree_get_root_path(worktree));
2959 if (error)
2960 goto done;
2962 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2963 if (error)
2964 goto done;
2966 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2967 check_cancelled, NULL);
2968 done:
2969 TAILQ_FOREACH(pe, &paths, entry)
2970 free((char *)pe->path);
2971 got_pathlist_free(&paths);
2972 free(cwd);
2973 return error;
2976 __dead static void
2977 usage_ref(void)
2979 fprintf(stderr,
2980 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2981 getprogname());
2982 exit(1);
2985 static const struct got_error *
2986 list_refs(struct got_repository *repo)
2988 static const struct got_error *err = NULL;
2989 struct got_reflist_head refs;
2990 struct got_reflist_entry *re;
2992 SIMPLEQ_INIT(&refs);
2993 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2994 if (err)
2995 return err;
2997 SIMPLEQ_FOREACH(re, &refs, entry) {
2998 char *refstr;
2999 refstr = got_ref_to_str(re->ref);
3000 if (refstr == NULL)
3001 return got_error_from_errno("got_ref_to_str");
3002 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3003 free(refstr);
3006 got_ref_list_free(&refs);
3007 return NULL;
3010 static const struct got_error *
3011 delete_ref(struct got_repository *repo, const char *refname)
3013 const struct got_error *err = NULL;
3014 struct got_reference *ref;
3016 err = got_ref_open(&ref, repo, refname, 0);
3017 if (err)
3018 return err;
3020 err = got_ref_delete(ref, repo);
3021 got_ref_close(ref);
3022 return err;
3025 static const struct got_error *
3026 add_ref(struct got_repository *repo, const char *refname, const char *target)
3028 const struct got_error *err = NULL;
3029 struct got_object_id *id;
3030 struct got_reference *ref = NULL;
3033 * Don't let the user create a reference named '-'.
3034 * While technically a valid reference name, this case is usually
3035 * an unintended typo.
3037 if (refname[0] == '-' && refname[1] == '\0')
3038 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3040 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3041 repo);
3042 if (err) {
3043 struct got_reference *target_ref;
3045 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3046 return err;
3047 err = got_ref_open(&target_ref, repo, target, 0);
3048 if (err)
3049 return err;
3050 err = got_ref_resolve(&id, repo, target_ref);
3051 got_ref_close(target_ref);
3052 if (err)
3053 return err;
3056 err = got_ref_alloc(&ref, refname, id);
3057 if (err)
3058 goto done;
3060 err = got_ref_write(ref, repo);
3061 done:
3062 if (ref)
3063 got_ref_close(ref);
3064 free(id);
3065 return err;
3068 static const struct got_error *
3069 add_symref(struct got_repository *repo, const char *refname, const char *target)
3071 const struct got_error *err = NULL;
3072 struct got_reference *ref = NULL;
3073 struct got_reference *target_ref = NULL;
3076 * Don't let the user create a reference named '-'.
3077 * While technically a valid reference name, this case is usually
3078 * an unintended typo.
3080 if (refname[0] == '-' && refname[1] == '\0')
3081 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3083 err = got_ref_open(&target_ref, repo, target, 0);
3084 if (err)
3085 return err;
3087 err = got_ref_alloc_symref(&ref, refname, target_ref);
3088 if (err)
3089 goto done;
3091 err = got_ref_write(ref, repo);
3092 done:
3093 if (target_ref)
3094 got_ref_close(target_ref);
3095 if (ref)
3096 got_ref_close(ref);
3097 return err;
3100 static const struct got_error *
3101 cmd_ref(int argc, char *argv[])
3103 const struct got_error *error = NULL;
3104 struct got_repository *repo = NULL;
3105 struct got_worktree *worktree = NULL;
3106 char *cwd = NULL, *repo_path = NULL;
3107 int ch, do_list = 0, create_symref = 0;
3108 const char *delref = NULL;
3110 /* TODO: Add -s option for adding symbolic references. */
3111 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3112 switch (ch) {
3113 case 'd':
3114 delref = optarg;
3115 break;
3116 case 'r':
3117 repo_path = realpath(optarg, NULL);
3118 if (repo_path == NULL)
3119 err(1, "-r option");
3120 got_path_strip_trailing_slashes(repo_path);
3121 break;
3122 case 'l':
3123 do_list = 1;
3124 break;
3125 case 's':
3126 create_symref = 1;
3127 break;
3128 default:
3129 usage_ref();
3130 /* NOTREACHED */
3134 if (do_list && delref)
3135 errx(1, "-l and -d options are mutually exclusive\n");
3137 argc -= optind;
3138 argv += optind;
3140 if (do_list || delref) {
3141 if (create_symref)
3142 errx(1, "-s option cannot be used together with the "
3143 "-l or -d options");
3144 if (argc > 0)
3145 usage_ref();
3146 } else if (argc != 2)
3147 usage_ref();
3149 #ifndef PROFILE
3150 if (do_list) {
3151 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3152 NULL) == -1)
3153 err(1, "pledge");
3154 } else {
3155 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3156 "sendfd unveil", NULL) == -1)
3157 err(1, "pledge");
3159 #endif
3160 cwd = getcwd(NULL, 0);
3161 if (cwd == NULL) {
3162 error = got_error_from_errno("getcwd");
3163 goto done;
3166 if (repo_path == NULL) {
3167 error = got_worktree_open(&worktree, cwd);
3168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3169 goto done;
3170 else
3171 error = NULL;
3172 if (worktree) {
3173 repo_path =
3174 strdup(got_worktree_get_repo_path(worktree));
3175 if (repo_path == NULL)
3176 error = got_error_from_errno("strdup");
3177 if (error)
3178 goto done;
3179 } else {
3180 repo_path = strdup(cwd);
3181 if (repo_path == NULL) {
3182 error = got_error_from_errno("strdup");
3183 goto done;
3188 error = got_repo_open(&repo, repo_path, NULL);
3189 if (error != NULL)
3190 goto done;
3192 error = apply_unveil(got_repo_get_path(repo), do_list,
3193 worktree ? got_worktree_get_root_path(worktree) : NULL);
3194 if (error)
3195 goto done;
3197 if (do_list)
3198 error = list_refs(repo);
3199 else if (delref)
3200 error = delete_ref(repo, delref);
3201 else if (create_symref)
3202 error = add_symref(repo, argv[0], argv[1]);
3203 else
3204 error = add_ref(repo, argv[0], argv[1]);
3205 done:
3206 if (repo)
3207 got_repo_close(repo);
3208 if (worktree)
3209 got_worktree_close(worktree);
3210 free(cwd);
3211 free(repo_path);
3212 return error;
3215 __dead static void
3216 usage_branch(void)
3218 fprintf(stderr,
3219 "usage: %s branch [-r repository] [-l] | -d name | "
3220 "[name [commit]]\n", getprogname());
3221 exit(1);
3224 static const struct got_error *
3225 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3226 struct got_reference *ref)
3228 const struct got_error *err = NULL;
3229 const char *refname, *marker = " ";
3230 char *refstr;
3232 refname = got_ref_get_name(ref);
3233 if (worktree && strcmp(refname,
3234 got_worktree_get_head_ref_name(worktree)) == 0) {
3235 struct got_object_id *id = NULL;
3237 err = got_ref_resolve(&id, repo, ref);
3238 if (err)
3239 return err;
3240 if (got_object_id_cmp(id,
3241 got_worktree_get_base_commit_id(worktree)) == 0)
3242 marker = "* ";
3243 else
3244 marker = "~ ";
3245 free(id);
3248 if (strncmp(refname, "refs/heads/", 11) == 0)
3249 refname += 11;
3250 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3251 refname += 18;
3253 refstr = got_ref_to_str(ref);
3254 if (refstr == NULL)
3255 return got_error_from_errno("got_ref_to_str");
3257 printf("%s%s: %s\n", marker, refname, refstr);
3258 free(refstr);
3259 return NULL;
3262 static const struct got_error *
3263 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3265 const char *refname;
3267 if (worktree == NULL)
3268 return got_error(GOT_ERR_NOT_WORKTREE);
3270 refname = got_worktree_get_head_ref_name(worktree);
3272 if (strncmp(refname, "refs/heads/", 11) == 0)
3273 refname += 11;
3274 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3275 refname += 18;
3277 printf("%s\n", refname);
3279 return NULL;
3282 static const struct got_error *
3283 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3285 static const struct got_error *err = NULL;
3286 struct got_reflist_head refs;
3287 struct got_reflist_entry *re;
3288 struct got_reference *temp_ref = NULL;
3289 int rebase_in_progress, histedit_in_progress;
3291 SIMPLEQ_INIT(&refs);
3293 if (worktree) {
3294 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3295 worktree);
3296 if (err)
3297 return err;
3299 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3300 worktree);
3301 if (err)
3302 return err;
3304 if (rebase_in_progress || histedit_in_progress) {
3305 err = got_ref_open(&temp_ref, repo,
3306 got_worktree_get_head_ref_name(worktree), 0);
3307 if (err)
3308 return err;
3309 list_branch(repo, worktree, temp_ref);
3310 got_ref_close(temp_ref);
3314 err = got_ref_list(&refs, repo, "refs/heads",
3315 got_ref_cmp_by_name, NULL);
3316 if (err)
3317 return err;
3319 SIMPLEQ_FOREACH(re, &refs, entry)
3320 list_branch(repo, worktree, re->ref);
3322 got_ref_list_free(&refs);
3323 return NULL;
3326 static const struct got_error *
3327 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3328 const char *branch_name)
3330 const struct got_error *err = NULL;
3331 struct got_reference *ref = NULL;
3332 char *refname;
3334 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3335 return got_error_from_errno("asprintf");
3337 err = got_ref_open(&ref, repo, refname, 0);
3338 if (err)
3339 goto done;
3341 if (worktree &&
3342 strcmp(got_worktree_get_head_ref_name(worktree),
3343 got_ref_get_name(ref)) == 0) {
3344 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3345 "will not delete this work tree's current branch");
3346 goto done;
3349 err = got_ref_delete(ref, repo);
3350 done:
3351 if (ref)
3352 got_ref_close(ref);
3353 free(refname);
3354 return err;
3357 static const struct got_error *
3358 add_branch(struct got_repository *repo, const char *branch_name,
3359 const char *base_branch)
3361 const struct got_error *err = NULL;
3362 struct got_object_id *id = NULL;
3363 char *label;
3364 struct got_reference *ref = NULL;
3365 char *base_refname = NULL, *refname = NULL;
3368 * Don't let the user create a branch named '-'.
3369 * While technically a valid reference name, this case is usually
3370 * an unintended typo.
3372 if (branch_name[0] == '-' && branch_name[1] == '\0')
3373 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3375 err = match_object_id(&id, &label, base_branch,
3376 GOT_OBJ_TYPE_COMMIT, 1, repo);
3377 if (err)
3378 return err;
3380 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3381 err = got_error_from_errno("asprintf");
3382 goto done;
3385 err = got_ref_open(&ref, repo, refname, 0);
3386 if (err == NULL) {
3387 err = got_error(GOT_ERR_BRANCH_EXISTS);
3388 goto done;
3389 } else if (err->code != GOT_ERR_NOT_REF)
3390 goto done;
3392 err = got_ref_alloc(&ref, refname, id);
3393 if (err)
3394 goto done;
3396 err = got_ref_write(ref, repo);
3397 done:
3398 if (ref)
3399 got_ref_close(ref);
3400 free(id);
3401 free(base_refname);
3402 free(refname);
3403 return err;
3406 static const struct got_error *
3407 cmd_branch(int argc, char *argv[])
3409 const struct got_error *error = NULL;
3410 struct got_repository *repo = NULL;
3411 struct got_worktree *worktree = NULL;
3412 char *cwd = NULL, *repo_path = NULL;
3413 int ch, do_list = 0, do_show = 0;
3414 const char *delref = NULL;
3416 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3417 switch (ch) {
3418 case 'd':
3419 delref = optarg;
3420 break;
3421 case 'r':
3422 repo_path = realpath(optarg, NULL);
3423 if (repo_path == NULL)
3424 err(1, "-r option");
3425 got_path_strip_trailing_slashes(repo_path);
3426 break;
3427 case 'l':
3428 do_list = 1;
3429 break;
3430 default:
3431 usage_branch();
3432 /* NOTREACHED */
3436 if (do_list && delref)
3437 errx(1, "-l and -d options are mutually exclusive\n");
3439 argc -= optind;
3440 argv += optind;
3442 if (!do_list && !delref && argc == 0)
3443 do_show = 1;
3445 if (do_list || delref) {
3446 if (argc > 0)
3447 usage_branch();
3448 } else if (!do_show && (argc < 1 || argc > 2))
3449 usage_branch();
3451 #ifndef PROFILE
3452 if (do_list || do_show) {
3453 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3454 NULL) == -1)
3455 err(1, "pledge");
3456 } else {
3457 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3458 "sendfd unveil", NULL) == -1)
3459 err(1, "pledge");
3461 #endif
3462 cwd = getcwd(NULL, 0);
3463 if (cwd == NULL) {
3464 error = got_error_from_errno("getcwd");
3465 goto done;
3468 if (repo_path == NULL) {
3469 error = got_worktree_open(&worktree, cwd);
3470 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3471 goto done;
3472 else
3473 error = NULL;
3474 if (worktree) {
3475 repo_path =
3476 strdup(got_worktree_get_repo_path(worktree));
3477 if (repo_path == NULL)
3478 error = got_error_from_errno("strdup");
3479 if (error)
3480 goto done;
3481 } else {
3482 repo_path = strdup(cwd);
3483 if (repo_path == NULL) {
3484 error = got_error_from_errno("strdup");
3485 goto done;
3490 error = got_repo_open(&repo, repo_path, NULL);
3491 if (error != NULL)
3492 goto done;
3494 error = apply_unveil(got_repo_get_path(repo), do_list,
3495 worktree ? got_worktree_get_root_path(worktree) : NULL);
3496 if (error)
3497 goto done;
3499 if (do_show)
3500 error = show_current_branch(repo, worktree);
3501 else if (do_list)
3502 error = list_branches(repo, worktree);
3503 else if (delref)
3504 error = delete_branch(repo, worktree, delref);
3505 else {
3506 const char *base_branch;
3507 if (argc == 1) {
3508 base_branch = worktree ?
3509 got_worktree_get_head_ref_name(worktree) :
3510 GOT_REF_HEAD;
3511 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3512 base_branch += 11;
3513 } else
3514 base_branch = argv[1];
3515 error = add_branch(repo, argv[0], base_branch);
3517 done:
3518 if (repo)
3519 got_repo_close(repo);
3520 if (worktree)
3521 got_worktree_close(worktree);
3522 free(cwd);
3523 free(repo_path);
3524 return error;
3528 __dead static void
3529 usage_tag(void)
3531 fprintf(stderr,
3532 "usage: %s tag [-r repository] | -l | "
3533 "[-m message] name [commit]\n", getprogname());
3534 exit(1);
3537 #if 0
3538 static const struct got_error *
3539 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3541 const struct got_error *err = NULL;
3542 struct got_reflist_entry *re, *se, *new;
3543 struct got_object_id *re_id, *se_id;
3544 struct got_tag_object *re_tag, *se_tag;
3545 time_t re_time, se_time;
3547 SIMPLEQ_FOREACH(re, tags, entry) {
3548 se = SIMPLEQ_FIRST(sorted);
3549 if (se == NULL) {
3550 err = got_reflist_entry_dup(&new, re);
3551 if (err)
3552 return err;
3553 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3554 continue;
3555 } else {
3556 err = got_ref_resolve(&re_id, repo, re->ref);
3557 if (err)
3558 break;
3559 err = got_object_open_as_tag(&re_tag, repo, re_id);
3560 free(re_id);
3561 if (err)
3562 break;
3563 re_time = got_object_tag_get_tagger_time(re_tag);
3564 got_object_tag_close(re_tag);
3567 while (se) {
3568 err = got_ref_resolve(&se_id, repo, re->ref);
3569 if (err)
3570 break;
3571 err = got_object_open_as_tag(&se_tag, repo, se_id);
3572 free(se_id);
3573 if (err)
3574 break;
3575 se_time = got_object_tag_get_tagger_time(se_tag);
3576 got_object_tag_close(se_tag);
3578 if (se_time > re_time) {
3579 err = got_reflist_entry_dup(&new, re);
3580 if (err)
3581 return err;
3582 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3583 break;
3585 se = SIMPLEQ_NEXT(se, entry);
3586 continue;
3589 done:
3590 return err;
3592 #endif
3594 static const struct got_error *
3595 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3596 struct got_reference *ref2)
3598 const struct got_error *err = NULL;
3599 struct got_repository *repo = arg;
3600 struct got_object_id *id1, *id2 = NULL;
3601 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3602 time_t time1, time2;
3604 *cmp = 0;
3606 err = got_ref_resolve(&id1, repo, ref1);
3607 if (err)
3608 return err;
3609 err = got_object_open_as_tag(&tag1, repo, id1);
3610 if (err)
3611 goto done;
3613 err = got_ref_resolve(&id2, repo, ref2);
3614 if (err)
3615 goto done;
3616 err = got_object_open_as_tag(&tag2, repo, id2);
3617 if (err)
3618 goto done;
3620 time1 = got_object_tag_get_tagger_time(tag1);
3621 time2 = got_object_tag_get_tagger_time(tag2);
3623 /* Put latest tags first. */
3624 if (time1 < time2)
3625 *cmp = 1;
3626 else if (time1 > time2)
3627 *cmp = -1;
3628 else
3629 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3630 done:
3631 free(id1);
3632 free(id2);
3633 if (tag1)
3634 got_object_tag_close(tag1);
3635 if (tag2)
3636 got_object_tag_close(tag2);
3637 return err;
3640 static const struct got_error *
3641 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3643 static const struct got_error *err = NULL;
3644 struct got_reflist_head refs;
3645 struct got_reflist_entry *re;
3647 SIMPLEQ_INIT(&refs);
3649 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3650 if (err)
3651 return err;
3653 SIMPLEQ_FOREACH(re, &refs, entry) {
3654 const char *refname;
3655 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3656 char datebuf[26];
3657 time_t tagger_time;
3658 struct got_object_id *id;
3659 struct got_tag_object *tag;
3661 refname = got_ref_get_name(re->ref);
3662 if (strncmp(refname, "refs/tags/", 10) != 0)
3663 continue;
3664 refname += 10;
3665 refstr = got_ref_to_str(re->ref);
3666 if (refstr == NULL) {
3667 err = got_error_from_errno("got_ref_to_str");
3668 break;
3670 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3671 free(refstr);
3673 err = got_ref_resolve(&id, repo, re->ref);
3674 if (err)
3675 break;
3676 err = got_object_open_as_tag(&tag, repo, id);
3677 free(id);
3678 if (err)
3679 break;
3680 printf("from: %s\n", got_object_tag_get_tagger(tag));
3681 tagger_time = got_object_tag_get_tagger_time(tag);
3682 datestr = get_datestr(&tagger_time, datebuf);
3683 if (datestr)
3684 printf("date: %s UTC\n", datestr);
3685 err = got_object_id_str(&id_str,
3686 got_object_tag_get_object_id(tag));
3687 if (err)
3688 break;
3689 switch (got_object_tag_get_object_type(tag)) {
3690 case GOT_OBJ_TYPE_BLOB:
3691 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3692 break;
3693 case GOT_OBJ_TYPE_TREE:
3694 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3695 break;
3696 case GOT_OBJ_TYPE_COMMIT:
3697 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3698 break;
3699 case GOT_OBJ_TYPE_TAG:
3700 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3701 break;
3702 default:
3703 break;
3705 free(id_str);
3706 tagmsg0 = strdup(got_object_tag_get_message(tag));
3707 got_object_tag_close(tag);
3708 if (tagmsg0 == NULL) {
3709 err = got_error_from_errno("strdup");
3710 break;
3713 tagmsg = tagmsg0;
3714 do {
3715 line = strsep(&tagmsg, "\n");
3716 if (line)
3717 printf(" %s\n", line);
3718 } while (line);
3719 free(tagmsg0);
3722 got_ref_list_free(&refs);
3723 return NULL;
3726 static const struct got_error *
3727 get_tag_message(char **tagmsg, const char *commit_id_str,
3728 const char *tag_name, const char *repo_path)
3730 const struct got_error *err = NULL;
3731 char *template = NULL, *initial_content = NULL;
3732 char *tagmsg_path = NULL, *editor = NULL;
3733 int fd = -1;
3735 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3736 err = got_error_from_errno("asprintf");
3737 goto done;
3740 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3741 commit_id_str, tag_name) == -1) {
3742 err = got_error_from_errno("asprintf");
3743 goto done;
3746 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3747 if (err)
3748 goto done;
3750 dprintf(fd, initial_content);
3751 close(fd);
3753 err = get_editor(&editor);
3754 if (err)
3755 goto done;
3756 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3757 done:
3758 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3759 unlink(tagmsg_path);
3760 free(tagmsg_path);
3761 tagmsg_path = NULL;
3763 free(initial_content);
3764 free(template);
3765 free(editor);
3767 /* Editor is done; we can now apply unveil(2) */
3768 if (err == NULL) {
3769 err = apply_unveil(repo_path, 0, NULL);
3770 if (err) {
3771 free(*tagmsg);
3772 *tagmsg = NULL;
3775 return err;
3778 static const struct got_error *
3779 add_tag(struct got_repository *repo, const char *tag_name,
3780 const char *commit_arg, const char *tagmsg_arg)
3782 const struct got_error *err = NULL;
3783 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3784 char *label = NULL, *commit_id_str = NULL;
3785 struct got_reference *ref = NULL;
3786 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3789 * Don't let the user create a tag named '-'.
3790 * While technically a valid reference name, this case is usually
3791 * an unintended typo.
3793 if (tag_name[0] == '-' && tag_name[1] == '\0')
3794 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3796 err = get_author(&tagger, repo);
3797 if (err)
3798 return err;
3800 err = match_object_id(&commit_id, &label, commit_arg,
3801 GOT_OBJ_TYPE_COMMIT, 1, repo);
3802 if (err)
3803 goto done;
3805 err = got_object_id_str(&commit_id_str, commit_id);
3806 if (err)
3807 goto done;
3809 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3810 refname = strdup(tag_name);
3811 if (refname == NULL) {
3812 err = got_error_from_errno("strdup");
3813 goto done;
3815 tag_name += 10;
3816 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3817 err = got_error_from_errno("asprintf");
3818 goto done;
3821 err = got_ref_open(&ref, repo, refname, 0);
3822 if (err == NULL) {
3823 err = got_error(GOT_ERR_TAG_EXISTS);
3824 goto done;
3825 } else if (err->code != GOT_ERR_NOT_REF)
3826 goto done;
3828 if (tagmsg_arg == NULL) {
3829 err = get_tag_message(&tagmsg, commit_id_str,
3830 tag_name, got_repo_get_path(repo));
3831 if (err)
3832 goto done;
3835 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3836 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3837 if (err)
3838 goto done;
3840 err = got_ref_alloc(&ref, refname, tag_id);
3841 if (err)
3842 goto done;
3844 err = got_ref_write(ref, repo);
3846 if (err == NULL) {
3847 char *tag_id_str;
3848 err = got_object_id_str(&tag_id_str, tag_id);
3849 printf("Created tag %s\n", tag_id_str);
3850 free(tag_id_str);
3852 done:
3853 if (ref)
3854 got_ref_close(ref);
3855 free(commit_id);
3856 free(commit_id_str);
3857 free(refname);
3858 free(tagmsg);
3859 free(tagger);
3860 return err;
3863 static const struct got_error *
3864 cmd_tag(int argc, char *argv[])
3866 const struct got_error *error = NULL;
3867 struct got_repository *repo = NULL;
3868 struct got_worktree *worktree = NULL;
3869 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3870 char *gitconfig_path = NULL;
3871 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3872 int ch, do_list = 0;
3874 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3875 switch (ch) {
3876 case 'm':
3877 tagmsg = optarg;
3878 break;
3879 case 'r':
3880 repo_path = realpath(optarg, NULL);
3881 if (repo_path == NULL)
3882 err(1, "-r option");
3883 got_path_strip_trailing_slashes(repo_path);
3884 break;
3885 case 'l':
3886 do_list = 1;
3887 break;
3888 default:
3889 usage_tag();
3890 /* NOTREACHED */
3894 argc -= optind;
3895 argv += optind;
3897 if (do_list) {
3898 if (tagmsg)
3899 errx(1, "-l and -m options are mutually exclusive\n");
3900 if (argc > 0)
3901 usage_tag();
3902 } else if (argc < 1 || argc > 2)
3903 usage_tag();
3904 else if (argc > 1)
3905 commit_id_arg = argv[1];
3906 tag_name = argv[0];
3908 #ifndef PROFILE
3909 if (do_list) {
3910 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3911 NULL) == -1)
3912 err(1, "pledge");
3913 } else {
3914 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3915 "sendfd unveil", NULL) == -1)
3916 err(1, "pledge");
3918 #endif
3919 cwd = getcwd(NULL, 0);
3920 if (cwd == NULL) {
3921 error = got_error_from_errno("getcwd");
3922 goto done;
3925 if (repo_path == NULL) {
3926 error = got_worktree_open(&worktree, cwd);
3927 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3928 goto done;
3929 else
3930 error = NULL;
3931 if (worktree) {
3932 repo_path =
3933 strdup(got_worktree_get_repo_path(worktree));
3934 if (repo_path == NULL)
3935 error = got_error_from_errno("strdup");
3936 if (error)
3937 goto done;
3938 } else {
3939 repo_path = strdup(cwd);
3940 if (repo_path == NULL) {
3941 error = got_error_from_errno("strdup");
3942 goto done;
3947 if (do_list) {
3948 error = got_repo_open(&repo, repo_path, NULL);
3949 if (error != NULL)
3950 goto done;
3951 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3952 if (error)
3953 goto done;
3954 error = list_tags(repo, worktree);
3955 } else {
3956 error = get_gitconfig_path(&gitconfig_path);
3957 if (error)
3958 goto done;
3959 error = got_repo_open(&repo, repo_path, gitconfig_path);
3960 if (error != NULL)
3961 goto done;
3963 if (tagmsg) {
3964 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3965 if (error)
3966 goto done;
3969 if (commit_id_arg == NULL) {
3970 struct got_reference *head_ref;
3971 struct got_object_id *commit_id;
3972 error = got_ref_open(&head_ref, repo,
3973 worktree ? got_worktree_get_head_ref_name(worktree)
3974 : GOT_REF_HEAD, 0);
3975 if (error)
3976 goto done;
3977 error = got_ref_resolve(&commit_id, repo, head_ref);
3978 got_ref_close(head_ref);
3979 if (error)
3980 goto done;
3981 error = got_object_id_str(&commit_id_str, commit_id);
3982 free(commit_id);
3983 if (error)
3984 goto done;
3987 error = add_tag(repo, tag_name,
3988 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3990 done:
3991 if (repo)
3992 got_repo_close(repo);
3993 if (worktree)
3994 got_worktree_close(worktree);
3995 free(cwd);
3996 free(repo_path);
3997 free(gitconfig_path);
3998 free(commit_id_str);
3999 return error;
4002 __dead static void
4003 usage_add(void)
4005 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4006 exit(1);
4009 static const struct got_error *
4010 cmd_add(int argc, char *argv[])
4012 const struct got_error *error = NULL;
4013 struct got_repository *repo = NULL;
4014 struct got_worktree *worktree = NULL;
4015 char *cwd = NULL;
4016 struct got_pathlist_head paths;
4017 struct got_pathlist_entry *pe;
4018 int ch;
4020 TAILQ_INIT(&paths);
4022 while ((ch = getopt(argc, argv, "")) != -1) {
4023 switch (ch) {
4024 default:
4025 usage_add();
4026 /* NOTREACHED */
4030 argc -= optind;
4031 argv += optind;
4033 #ifndef PROFILE
4034 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4035 NULL) == -1)
4036 err(1, "pledge");
4037 #endif
4038 if (argc < 1)
4039 usage_add();
4041 cwd = getcwd(NULL, 0);
4042 if (cwd == NULL) {
4043 error = got_error_from_errno("getcwd");
4044 goto done;
4047 error = got_worktree_open(&worktree, cwd);
4048 if (error)
4049 goto done;
4051 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4052 NULL);
4053 if (error != NULL)
4054 goto done;
4056 error = apply_unveil(got_repo_get_path(repo), 1,
4057 got_worktree_get_root_path(worktree));
4058 if (error)
4059 goto done;
4061 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4062 if (error)
4063 goto done;
4065 error = got_worktree_schedule_add(worktree, &paths, print_status,
4066 NULL, repo);
4067 done:
4068 if (repo)
4069 got_repo_close(repo);
4070 if (worktree)
4071 got_worktree_close(worktree);
4072 TAILQ_FOREACH(pe, &paths, entry)
4073 free((char *)pe->path);
4074 got_pathlist_free(&paths);
4075 free(cwd);
4076 return error;
4079 __dead static void
4080 usage_remove(void)
4082 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4083 exit(1);
4086 static const struct got_error *
4087 print_remove_status(void *arg, unsigned char status,
4088 unsigned char staged_status, const char *path,
4089 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4090 struct got_object_id *commit_id)
4092 if (status == GOT_STATUS_NONEXISTENT)
4093 return NULL;
4094 if (status == staged_status && (status == GOT_STATUS_DELETE))
4095 status = GOT_STATUS_NO_CHANGE;
4096 printf("%c%c %s\n", status, staged_status, path);
4097 return NULL;
4100 static const struct got_error *
4101 cmd_remove(int argc, char *argv[])
4103 const struct got_error *error = NULL;
4104 struct got_worktree *worktree = NULL;
4105 struct got_repository *repo = NULL;
4106 char *cwd = NULL;
4107 struct got_pathlist_head paths;
4108 struct got_pathlist_entry *pe;
4109 int ch, delete_local_mods = 0;
4111 TAILQ_INIT(&paths);
4113 while ((ch = getopt(argc, argv, "f")) != -1) {
4114 switch (ch) {
4115 case 'f':
4116 delete_local_mods = 1;
4117 break;
4118 default:
4119 usage_add();
4120 /* NOTREACHED */
4124 argc -= optind;
4125 argv += optind;
4127 #ifndef PROFILE
4128 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4129 NULL) == -1)
4130 err(1, "pledge");
4131 #endif
4132 if (argc < 1)
4133 usage_remove();
4135 cwd = getcwd(NULL, 0);
4136 if (cwd == NULL) {
4137 error = got_error_from_errno("getcwd");
4138 goto done;
4140 error = got_worktree_open(&worktree, cwd);
4141 if (error)
4142 goto done;
4144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4145 NULL);
4146 if (error)
4147 goto done;
4149 error = apply_unveil(got_repo_get_path(repo), 1,
4150 got_worktree_get_root_path(worktree));
4151 if (error)
4152 goto done;
4154 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4155 if (error)
4156 goto done;
4158 error = got_worktree_schedule_delete(worktree, &paths,
4159 delete_local_mods, print_remove_status, NULL, repo);
4160 if (error)
4161 goto done;
4162 done:
4163 if (repo)
4164 got_repo_close(repo);
4165 if (worktree)
4166 got_worktree_close(worktree);
4167 TAILQ_FOREACH(pe, &paths, entry)
4168 free((char *)pe->path);
4169 got_pathlist_free(&paths);
4170 free(cwd);
4171 return error;
4174 __dead static void
4175 usage_revert(void)
4177 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4178 "path ...\n", getprogname());
4179 exit(1);
4182 static const struct got_error *
4183 revert_progress(void *arg, unsigned char status, const char *path)
4185 while (path[0] == '/')
4186 path++;
4187 printf("%c %s\n", status, path);
4188 return NULL;
4191 struct choose_patch_arg {
4192 FILE *patch_script_file;
4193 const char *action;
4196 static const struct got_error *
4197 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4198 int nchanges, const char *action)
4200 char *line = NULL;
4201 size_t linesize = 0;
4202 ssize_t linelen;
4204 switch (status) {
4205 case GOT_STATUS_ADD:
4206 printf("A %s\n%s this addition? [y/n] ", path, action);
4207 break;
4208 case GOT_STATUS_DELETE:
4209 printf("D %s\n%s this deletion? [y/n] ", path, action);
4210 break;
4211 case GOT_STATUS_MODIFY:
4212 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4213 return got_error_from_errno("fseek");
4214 printf(GOT_COMMIT_SEP_STR);
4215 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4216 printf("%s", line);
4217 if (ferror(patch_file))
4218 return got_error_from_errno("getline");
4219 printf(GOT_COMMIT_SEP_STR);
4220 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4221 path, n, nchanges, action);
4222 break;
4223 default:
4224 return got_error_path(path, GOT_ERR_FILE_STATUS);
4227 return NULL;
4230 static const struct got_error *
4231 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4232 FILE *patch_file, int n, int nchanges)
4234 const struct got_error *err = NULL;
4235 char *line = NULL;
4236 size_t linesize = 0;
4237 ssize_t linelen;
4238 int resp = ' ';
4239 struct choose_patch_arg *a = arg;
4241 *choice = GOT_PATCH_CHOICE_NONE;
4243 if (a->patch_script_file) {
4244 char *nl;
4245 err = show_change(status, path, patch_file, n, nchanges,
4246 a->action);
4247 if (err)
4248 return err;
4249 linelen = getline(&line, &linesize, a->patch_script_file);
4250 if (linelen == -1) {
4251 if (ferror(a->patch_script_file))
4252 return got_error_from_errno("getline");
4253 return NULL;
4255 nl = strchr(line, '\n');
4256 if (nl)
4257 *nl = '\0';
4258 if (strcmp(line, "y") == 0) {
4259 *choice = GOT_PATCH_CHOICE_YES;
4260 printf("y\n");
4261 } else if (strcmp(line, "n") == 0) {
4262 *choice = GOT_PATCH_CHOICE_NO;
4263 printf("n\n");
4264 } else if (strcmp(line, "q") == 0 &&
4265 status == GOT_STATUS_MODIFY) {
4266 *choice = GOT_PATCH_CHOICE_QUIT;
4267 printf("q\n");
4268 } else
4269 printf("invalid response '%s'\n", line);
4270 free(line);
4271 return NULL;
4274 while (resp != 'y' && resp != 'n' && resp != 'q') {
4275 err = show_change(status, path, patch_file, n, nchanges,
4276 a->action);
4277 if (err)
4278 return err;
4279 resp = getchar();
4280 if (resp == '\n')
4281 resp = getchar();
4282 if (status == GOT_STATUS_MODIFY) {
4283 if (resp != 'y' && resp != 'n' && resp != 'q') {
4284 printf("invalid response '%c'\n", resp);
4285 resp = ' ';
4287 } else if (resp != 'y' && resp != 'n') {
4288 printf("invalid response '%c'\n", resp);
4289 resp = ' ';
4293 if (resp == 'y')
4294 *choice = GOT_PATCH_CHOICE_YES;
4295 else if (resp == 'n')
4296 *choice = GOT_PATCH_CHOICE_NO;
4297 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4298 *choice = GOT_PATCH_CHOICE_QUIT;
4300 return NULL;
4304 static const struct got_error *
4305 cmd_revert(int argc, char *argv[])
4307 const struct got_error *error = NULL;
4308 struct got_worktree *worktree = NULL;
4309 struct got_repository *repo = NULL;
4310 char *cwd = NULL, *path = NULL;
4311 struct got_pathlist_head paths;
4312 struct got_pathlist_entry *pe;
4313 int ch, can_recurse = 0, pflag = 0;
4314 FILE *patch_script_file = NULL;
4315 const char *patch_script_path = NULL;
4316 struct choose_patch_arg cpa;
4318 TAILQ_INIT(&paths);
4320 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4321 switch (ch) {
4322 case 'p':
4323 pflag = 1;
4324 break;
4325 case 'F':
4326 patch_script_path = optarg;
4327 break;
4328 case 'R':
4329 can_recurse = 1;
4330 break;
4331 default:
4332 usage_revert();
4333 /* NOTREACHED */
4337 argc -= optind;
4338 argv += optind;
4340 #ifndef PROFILE
4341 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4342 "unveil", NULL) == -1)
4343 err(1, "pledge");
4344 #endif
4345 if (argc < 1)
4346 usage_revert();
4347 if (patch_script_path && !pflag)
4348 errx(1, "-F option can only be used together with -p option");
4350 cwd = getcwd(NULL, 0);
4351 if (cwd == NULL) {
4352 error = got_error_from_errno("getcwd");
4353 goto done;
4355 error = got_worktree_open(&worktree, cwd);
4356 if (error)
4357 goto done;
4359 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4360 NULL);
4361 if (error != NULL)
4362 goto done;
4364 if (patch_script_path) {
4365 patch_script_file = fopen(patch_script_path, "r");
4366 if (patch_script_file == NULL) {
4367 error = got_error_from_errno2("fopen",
4368 patch_script_path);
4369 goto done;
4372 error = apply_unveil(got_repo_get_path(repo), 1,
4373 got_worktree_get_root_path(worktree));
4374 if (error)
4375 goto done;
4377 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4378 if (error)
4379 goto done;
4381 if (!can_recurse) {
4382 char *ondisk_path;
4383 struct stat sb;
4384 TAILQ_FOREACH(pe, &paths, entry) {
4385 if (asprintf(&ondisk_path, "%s/%s",
4386 got_worktree_get_root_path(worktree),
4387 pe->path) == -1) {
4388 error = got_error_from_errno("asprintf");
4389 goto done;
4391 if (lstat(ondisk_path, &sb) == -1) {
4392 if (errno == ENOENT) {
4393 free(ondisk_path);
4394 continue;
4396 error = got_error_from_errno2("lstat",
4397 ondisk_path);
4398 free(ondisk_path);
4399 goto done;
4401 free(ondisk_path);
4402 if (S_ISDIR(sb.st_mode)) {
4403 error = got_error_msg(GOT_ERR_BAD_PATH,
4404 "reverting directories requires -R option");
4405 goto done;
4410 cpa.patch_script_file = patch_script_file;
4411 cpa.action = "revert";
4412 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4413 pflag ? choose_patch : NULL, &cpa, repo);
4414 if (error)
4415 goto done;
4416 done:
4417 if (patch_script_file && fclose(patch_script_file) == EOF &&
4418 error == NULL)
4419 error = got_error_from_errno2("fclose", patch_script_path);
4420 if (repo)
4421 got_repo_close(repo);
4422 if (worktree)
4423 got_worktree_close(worktree);
4424 free(path);
4425 free(cwd);
4426 return error;
4429 __dead static void
4430 usage_commit(void)
4432 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4433 getprogname());
4434 exit(1);
4437 struct collect_commit_logmsg_arg {
4438 const char *cmdline_log;
4439 const char *editor;
4440 const char *worktree_path;
4441 const char *branch_name;
4442 const char *repo_path;
4443 char *logmsg_path;
4447 static const struct got_error *
4448 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4449 void *arg)
4451 char *initial_content = NULL;
4452 struct got_pathlist_entry *pe;
4453 const struct got_error *err = NULL;
4454 char *template = NULL;
4455 struct collect_commit_logmsg_arg *a = arg;
4456 int fd;
4457 size_t len;
4459 /* if a message was specified on the command line, just use it */
4460 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4461 len = strlen(a->cmdline_log) + 1;
4462 *logmsg = malloc(len + 1);
4463 if (*logmsg == NULL)
4464 return got_error_from_errno("malloc");
4465 strlcpy(*logmsg, a->cmdline_log, len);
4466 return NULL;
4469 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4470 return got_error_from_errno("asprintf");
4472 if (asprintf(&initial_content,
4473 "\n# changes to be committed on branch %s:\n",
4474 a->branch_name) == -1)
4475 return got_error_from_errno("asprintf");
4477 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4478 if (err)
4479 goto done;
4481 dprintf(fd, initial_content);
4483 TAILQ_FOREACH(pe, commitable_paths, entry) {
4484 struct got_commitable *ct = pe->data;
4485 dprintf(fd, "# %c %s\n",
4486 got_commitable_get_status(ct),
4487 got_commitable_get_path(ct));
4489 close(fd);
4491 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4492 done:
4493 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4494 unlink(a->logmsg_path);
4495 free(a->logmsg_path);
4496 a->logmsg_path = NULL;
4498 free(initial_content);
4499 free(template);
4501 /* Editor is done; we can now apply unveil(2) */
4502 if (err == NULL) {
4503 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4504 if (err) {
4505 free(*logmsg);
4506 *logmsg = NULL;
4509 return err;
4512 static const struct got_error *
4513 cmd_commit(int argc, char *argv[])
4515 const struct got_error *error = NULL;
4516 struct got_worktree *worktree = NULL;
4517 struct got_repository *repo = NULL;
4518 char *cwd = NULL, *id_str = NULL;
4519 struct got_object_id *id = NULL;
4520 const char *logmsg = NULL;
4521 struct collect_commit_logmsg_arg cl_arg;
4522 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4523 int ch, rebase_in_progress, histedit_in_progress;
4524 struct got_pathlist_head paths;
4526 TAILQ_INIT(&paths);
4527 cl_arg.logmsg_path = NULL;
4529 while ((ch = getopt(argc, argv, "m:")) != -1) {
4530 switch (ch) {
4531 case 'm':
4532 logmsg = optarg;
4533 break;
4534 default:
4535 usage_commit();
4536 /* NOTREACHED */
4540 argc -= optind;
4541 argv += optind;
4543 #ifndef PROFILE
4544 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4545 "unveil", NULL) == -1)
4546 err(1, "pledge");
4547 #endif
4548 cwd = getcwd(NULL, 0);
4549 if (cwd == NULL) {
4550 error = got_error_from_errno("getcwd");
4551 goto done;
4553 error = got_worktree_open(&worktree, cwd);
4554 if (error)
4555 goto done;
4557 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4558 if (error)
4559 goto done;
4560 if (rebase_in_progress) {
4561 error = got_error(GOT_ERR_REBASING);
4562 goto done;
4565 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4566 worktree);
4567 if (error)
4568 goto done;
4570 error = get_gitconfig_path(&gitconfig_path);
4571 if (error)
4572 goto done;
4573 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4574 gitconfig_path);
4575 if (error != NULL)
4576 goto done;
4578 error = get_author(&author, repo);
4579 if (error)
4580 return error;
4583 * unveil(2) traverses exec(2); if an editor is used we have
4584 * to apply unveil after the log message has been written.
4586 if (logmsg == NULL || strlen(logmsg) == 0)
4587 error = get_editor(&editor);
4588 else
4589 error = apply_unveil(got_repo_get_path(repo), 0,
4590 got_worktree_get_root_path(worktree));
4591 if (error)
4592 goto done;
4594 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4595 if (error)
4596 goto done;
4598 cl_arg.editor = editor;
4599 cl_arg.cmdline_log = logmsg;
4600 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4601 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4602 if (!histedit_in_progress) {
4603 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4604 error = got_error(GOT_ERR_COMMIT_BRANCH);
4605 goto done;
4607 cl_arg.branch_name += 11;
4609 cl_arg.repo_path = got_repo_get_path(repo);
4610 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4611 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4612 if (error) {
4613 if (cl_arg.logmsg_path)
4614 fprintf(stderr, "%s: log message preserved in %s\n",
4615 getprogname(), cl_arg.logmsg_path);
4616 goto done;
4619 if (cl_arg.logmsg_path)
4620 unlink(cl_arg.logmsg_path);
4622 error = got_object_id_str(&id_str, id);
4623 if (error)
4624 goto done;
4625 printf("Created commit %s\n", id_str);
4626 done:
4627 free(cl_arg.logmsg_path);
4628 if (repo)
4629 got_repo_close(repo);
4630 if (worktree)
4631 got_worktree_close(worktree);
4632 free(cwd);
4633 free(id_str);
4634 free(gitconfig_path);
4635 free(editor);
4636 free(author);
4637 return error;
4640 __dead static void
4641 usage_cherrypick(void)
4643 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4644 exit(1);
4647 static const struct got_error *
4648 cmd_cherrypick(int argc, char *argv[])
4650 const struct got_error *error = NULL;
4651 struct got_worktree *worktree = NULL;
4652 struct got_repository *repo = NULL;
4653 char *cwd = NULL, *commit_id_str = NULL;
4654 struct got_object_id *commit_id = NULL;
4655 struct got_commit_object *commit = NULL;
4656 struct got_object_qid *pid;
4657 struct got_reference *head_ref = NULL;
4658 int ch, did_something = 0;
4660 while ((ch = getopt(argc, argv, "")) != -1) {
4661 switch (ch) {
4662 default:
4663 usage_cherrypick();
4664 /* NOTREACHED */
4668 argc -= optind;
4669 argv += optind;
4671 #ifndef PROFILE
4672 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4673 "unveil", NULL) == -1)
4674 err(1, "pledge");
4675 #endif
4676 if (argc != 1)
4677 usage_cherrypick();
4679 cwd = getcwd(NULL, 0);
4680 if (cwd == NULL) {
4681 error = got_error_from_errno("getcwd");
4682 goto done;
4684 error = got_worktree_open(&worktree, cwd);
4685 if (error)
4686 goto done;
4688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4689 NULL);
4690 if (error != NULL)
4691 goto done;
4693 error = apply_unveil(got_repo_get_path(repo), 0,
4694 got_worktree_get_root_path(worktree));
4695 if (error)
4696 goto done;
4698 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4699 GOT_OBJ_TYPE_COMMIT, repo);
4700 if (error != NULL) {
4701 struct got_reference *ref;
4702 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4703 goto done;
4704 error = got_ref_open(&ref, repo, argv[0], 0);
4705 if (error != NULL)
4706 goto done;
4707 error = got_ref_resolve(&commit_id, repo, ref);
4708 got_ref_close(ref);
4709 if (error != NULL)
4710 goto done;
4712 error = got_object_id_str(&commit_id_str, commit_id);
4713 if (error)
4714 goto done;
4716 error = got_ref_open(&head_ref, repo,
4717 got_worktree_get_head_ref_name(worktree), 0);
4718 if (error != NULL)
4719 goto done;
4721 error = check_same_branch(commit_id, head_ref, NULL, repo);
4722 if (error) {
4723 if (error->code != GOT_ERR_ANCESTRY)
4724 goto done;
4725 error = NULL;
4726 } else {
4727 error = got_error(GOT_ERR_SAME_BRANCH);
4728 goto done;
4731 error = got_object_open_as_commit(&commit, repo, commit_id);
4732 if (error)
4733 goto done;
4734 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4735 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4736 commit_id, repo, update_progress, &did_something, check_cancelled,
4737 NULL);
4738 if (error != NULL)
4739 goto done;
4741 if (did_something)
4742 printf("Merged commit %s\n", commit_id_str);
4743 done:
4744 if (commit)
4745 got_object_commit_close(commit);
4746 free(commit_id_str);
4747 if (head_ref)
4748 got_ref_close(head_ref);
4749 if (worktree)
4750 got_worktree_close(worktree);
4751 if (repo)
4752 got_repo_close(repo);
4753 return error;
4756 __dead static void
4757 usage_backout(void)
4759 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4760 exit(1);
4763 static const struct got_error *
4764 cmd_backout(int argc, char *argv[])
4766 const struct got_error *error = NULL;
4767 struct got_worktree *worktree = NULL;
4768 struct got_repository *repo = NULL;
4769 char *cwd = NULL, *commit_id_str = NULL;
4770 struct got_object_id *commit_id = NULL;
4771 struct got_commit_object *commit = NULL;
4772 struct got_object_qid *pid;
4773 struct got_reference *head_ref = NULL;
4774 int ch, did_something = 0;
4776 while ((ch = getopt(argc, argv, "")) != -1) {
4777 switch (ch) {
4778 default:
4779 usage_backout();
4780 /* NOTREACHED */
4784 argc -= optind;
4785 argv += optind;
4787 #ifndef PROFILE
4788 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4789 "unveil", NULL) == -1)
4790 err(1, "pledge");
4791 #endif
4792 if (argc != 1)
4793 usage_backout();
4795 cwd = getcwd(NULL, 0);
4796 if (cwd == NULL) {
4797 error = got_error_from_errno("getcwd");
4798 goto done;
4800 error = got_worktree_open(&worktree, cwd);
4801 if (error)
4802 goto done;
4804 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4805 NULL);
4806 if (error != NULL)
4807 goto done;
4809 error = apply_unveil(got_repo_get_path(repo), 0,
4810 got_worktree_get_root_path(worktree));
4811 if (error)
4812 goto done;
4814 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4815 GOT_OBJ_TYPE_COMMIT, repo);
4816 if (error != NULL) {
4817 struct got_reference *ref;
4818 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4819 goto done;
4820 error = got_ref_open(&ref, repo, argv[0], 0);
4821 if (error != NULL)
4822 goto done;
4823 error = got_ref_resolve(&commit_id, repo, ref);
4824 got_ref_close(ref);
4825 if (error != NULL)
4826 goto done;
4828 error = got_object_id_str(&commit_id_str, commit_id);
4829 if (error)
4830 goto done;
4832 error = got_ref_open(&head_ref, repo,
4833 got_worktree_get_head_ref_name(worktree), 0);
4834 if (error != NULL)
4835 goto done;
4837 error = check_same_branch(commit_id, head_ref, NULL, repo);
4838 if (error)
4839 goto done;
4841 error = got_object_open_as_commit(&commit, repo, commit_id);
4842 if (error)
4843 goto done;
4844 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4845 if (pid == NULL) {
4846 error = got_error(GOT_ERR_ROOT_COMMIT);
4847 goto done;
4850 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4851 update_progress, &did_something, check_cancelled, NULL);
4852 if (error != NULL)
4853 goto done;
4855 if (did_something)
4856 printf("Backed out commit %s\n", commit_id_str);
4857 done:
4858 if (commit)
4859 got_object_commit_close(commit);
4860 free(commit_id_str);
4861 if (head_ref)
4862 got_ref_close(head_ref);
4863 if (worktree)
4864 got_worktree_close(worktree);
4865 if (repo)
4866 got_repo_close(repo);
4867 return error;
4870 __dead static void
4871 usage_rebase(void)
4873 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4874 getprogname());
4875 exit(1);
4878 void
4879 trim_logmsg(char *logmsg, int limit)
4881 char *nl;
4882 size_t len;
4884 len = strlen(logmsg);
4885 if (len > limit)
4886 len = limit;
4887 logmsg[len] = '\0';
4888 nl = strchr(logmsg, '\n');
4889 if (nl)
4890 *nl = '\0';
4893 static const struct got_error *
4894 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4896 const struct got_error *err;
4897 char *logmsg0 = NULL;
4898 const char *s;
4900 err = got_object_commit_get_logmsg(&logmsg0, commit);
4901 if (err)
4902 return err;
4904 s = logmsg0;
4905 while (isspace((unsigned char)s[0]))
4906 s++;
4908 *logmsg = strdup(s);
4909 if (*logmsg == NULL) {
4910 err = got_error_from_errno("strdup");
4911 goto done;
4914 trim_logmsg(*logmsg, limit);
4915 done:
4916 free(logmsg0);
4917 return err;
4920 static const struct got_error *
4921 show_rebase_progress(struct got_commit_object *commit,
4922 struct got_object_id *old_id, struct got_object_id *new_id)
4924 const struct got_error *err;
4925 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4927 err = got_object_id_str(&old_id_str, old_id);
4928 if (err)
4929 goto done;
4931 if (new_id) {
4932 err = got_object_id_str(&new_id_str, new_id);
4933 if (err)
4934 goto done;
4937 old_id_str[12] = '\0';
4938 if (new_id_str)
4939 new_id_str[12] = '\0';
4941 err = get_short_logmsg(&logmsg, 42, commit);
4942 if (err)
4943 goto done;
4945 printf("%s -> %s: %s\n", old_id_str,
4946 new_id_str ? new_id_str : "no-op change", logmsg);
4947 done:
4948 free(old_id_str);
4949 free(new_id_str);
4950 return err;
4953 static const struct got_error *
4954 rebase_progress(void *arg, unsigned char status, const char *path)
4956 unsigned char *rebase_status = arg;
4958 while (path[0] == '/')
4959 path++;
4960 printf("%c %s\n", status, path);
4962 if (*rebase_status == GOT_STATUS_CONFLICT)
4963 return NULL;
4964 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4965 *rebase_status = status;
4966 return NULL;
4969 static const struct got_error *
4970 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4971 struct got_reference *branch, struct got_reference *new_base_branch,
4972 struct got_reference *tmp_branch, struct got_repository *repo)
4974 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4975 return got_worktree_rebase_complete(worktree, fileindex,
4976 new_base_branch, tmp_branch, branch, repo);
4979 static const struct got_error *
4980 rebase_commit(struct got_pathlist_head *merged_paths,
4981 struct got_worktree *worktree, struct got_fileindex *fileindex,
4982 struct got_reference *tmp_branch,
4983 struct got_object_id *commit_id, struct got_repository *repo)
4985 const struct got_error *error;
4986 struct got_commit_object *commit;
4987 struct got_object_id *new_commit_id;
4989 error = got_object_open_as_commit(&commit, repo, commit_id);
4990 if (error)
4991 return error;
4993 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4994 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4995 if (error) {
4996 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4997 goto done;
4998 error = show_rebase_progress(commit, commit_id, NULL);
4999 } else {
5000 error = show_rebase_progress(commit, commit_id, new_commit_id);
5001 free(new_commit_id);
5003 done:
5004 got_object_commit_close(commit);
5005 return error;
5008 struct check_path_prefix_arg {
5009 const char *path_prefix;
5010 size_t len;
5011 int errcode;
5014 static const struct got_error *
5015 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5016 struct got_blob_object *blob2, struct got_object_id *id1,
5017 struct got_object_id *id2, const char *path1, const char *path2,
5018 struct got_repository *repo)
5020 struct check_path_prefix_arg *a = arg;
5022 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5023 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5024 return got_error(a->errcode);
5026 return NULL;
5029 static const struct got_error *
5030 check_path_prefix(struct got_object_id *parent_id,
5031 struct got_object_id *commit_id, const char *path_prefix,
5032 int errcode, struct got_repository *repo)
5034 const struct got_error *err;
5035 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5036 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5037 struct check_path_prefix_arg cpp_arg;
5039 if (got_path_is_root_dir(path_prefix))
5040 return NULL;
5042 err = got_object_open_as_commit(&commit, repo, commit_id);
5043 if (err)
5044 goto done;
5046 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5047 if (err)
5048 goto done;
5050 err = got_object_open_as_tree(&tree1, repo,
5051 got_object_commit_get_tree_id(parent_commit));
5052 if (err)
5053 goto done;
5055 err = got_object_open_as_tree(&tree2, repo,
5056 got_object_commit_get_tree_id(commit));
5057 if (err)
5058 goto done;
5060 cpp_arg.path_prefix = path_prefix;
5061 while (cpp_arg.path_prefix[0] == '/')
5062 cpp_arg.path_prefix++;
5063 cpp_arg.len = strlen(cpp_arg.path_prefix);
5064 cpp_arg.errcode = errcode;
5065 err = got_diff_tree(tree1, tree2, "", "", repo,
5066 check_path_prefix_in_diff, &cpp_arg, 0);
5067 done:
5068 if (tree1)
5069 got_object_tree_close(tree1);
5070 if (tree2)
5071 got_object_tree_close(tree2);
5072 if (commit)
5073 got_object_commit_close(commit);
5074 if (parent_commit)
5075 got_object_commit_close(parent_commit);
5076 return err;
5079 static const struct got_error *
5080 collect_commits(struct got_object_id_queue *commits,
5081 struct got_object_id *initial_commit_id,
5082 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5083 const char *path_prefix, int path_prefix_errcode,
5084 struct got_repository *repo)
5086 const struct got_error *err = NULL;
5087 struct got_commit_graph *graph = NULL;
5088 struct got_object_id *parent_id = NULL;
5089 struct got_object_qid *qid;
5090 struct got_object_id *commit_id = initial_commit_id;
5092 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5093 if (err)
5094 return err;
5096 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5097 check_cancelled, NULL);
5098 if (err)
5099 goto done;
5100 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5101 err = got_commit_graph_iter_next(&parent_id, graph);
5102 if (err) {
5103 if (err->code == GOT_ERR_ITER_COMPLETED) {
5104 err = got_error_msg(GOT_ERR_ANCESTRY,
5105 "ran out of commits to rebase before "
5106 "youngest common ancestor commit has "
5107 "been reached?!?");
5108 goto done;
5109 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5110 goto done;
5111 err = got_commit_graph_fetch_commits(graph, 1, repo,
5112 check_cancelled, NULL);
5113 if (err)
5114 goto done;
5115 } else {
5116 err = check_path_prefix(parent_id, commit_id,
5117 path_prefix, path_prefix_errcode, repo);
5118 if (err)
5119 goto done;
5121 err = got_object_qid_alloc(&qid, commit_id);
5122 if (err)
5123 goto done;
5124 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5125 commit_id = parent_id;
5128 done:
5129 got_commit_graph_close(graph);
5130 return err;
5133 static const struct got_error *
5134 cmd_rebase(int argc, char *argv[])
5136 const struct got_error *error = NULL;
5137 struct got_worktree *worktree = NULL;
5138 struct got_repository *repo = NULL;
5139 struct got_fileindex *fileindex = NULL;
5140 char *cwd = NULL;
5141 struct got_reference *branch = NULL;
5142 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5143 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5144 struct got_object_id *resume_commit_id = NULL;
5145 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5146 struct got_commit_object *commit = NULL;
5147 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5148 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5149 struct got_object_id_queue commits;
5150 struct got_pathlist_head merged_paths;
5151 const struct got_object_id_queue *parent_ids;
5152 struct got_object_qid *qid, *pid;
5154 SIMPLEQ_INIT(&commits);
5155 TAILQ_INIT(&merged_paths);
5157 while ((ch = getopt(argc, argv, "ac")) != -1) {
5158 switch (ch) {
5159 case 'a':
5160 abort_rebase = 1;
5161 break;
5162 case 'c':
5163 continue_rebase = 1;
5164 break;
5165 default:
5166 usage_rebase();
5167 /* NOTREACHED */
5171 argc -= optind;
5172 argv += optind;
5174 #ifndef PROFILE
5175 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5176 "unveil", NULL) == -1)
5177 err(1, "pledge");
5178 #endif
5179 if (abort_rebase && continue_rebase)
5180 usage_rebase();
5181 else if (abort_rebase || continue_rebase) {
5182 if (argc != 0)
5183 usage_rebase();
5184 } else if (argc != 1)
5185 usage_rebase();
5187 cwd = getcwd(NULL, 0);
5188 if (cwd == NULL) {
5189 error = got_error_from_errno("getcwd");
5190 goto done;
5192 error = got_worktree_open(&worktree, cwd);
5193 if (error)
5194 goto done;
5196 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5197 NULL);
5198 if (error != NULL)
5199 goto done;
5201 error = apply_unveil(got_repo_get_path(repo), 0,
5202 got_worktree_get_root_path(worktree));
5203 if (error)
5204 goto done;
5206 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5207 if (error)
5208 goto done;
5210 if (abort_rebase) {
5211 int did_something;
5212 if (!rebase_in_progress) {
5213 error = got_error(GOT_ERR_NOT_REBASING);
5214 goto done;
5216 error = got_worktree_rebase_continue(&resume_commit_id,
5217 &new_base_branch, &tmp_branch, &branch, &fileindex,
5218 worktree, repo);
5219 if (error)
5220 goto done;
5221 printf("Switching work tree to %s\n",
5222 got_ref_get_symref_target(new_base_branch));
5223 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5224 new_base_branch, update_progress, &did_something);
5225 if (error)
5226 goto done;
5227 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5228 goto done; /* nothing else to do */
5231 if (continue_rebase) {
5232 if (!rebase_in_progress) {
5233 error = got_error(GOT_ERR_NOT_REBASING);
5234 goto done;
5236 error = got_worktree_rebase_continue(&resume_commit_id,
5237 &new_base_branch, &tmp_branch, &branch, &fileindex,
5238 worktree, repo);
5239 if (error)
5240 goto done;
5242 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5243 resume_commit_id, repo);
5244 if (error)
5245 goto done;
5247 yca_id = got_object_id_dup(resume_commit_id);
5248 if (yca_id == NULL) {
5249 error = got_error_from_errno("got_object_id_dup");
5250 goto done;
5252 } else {
5253 error = got_ref_open(&branch, repo, argv[0], 0);
5254 if (error != NULL)
5255 goto done;
5258 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5259 if (error)
5260 goto done;
5262 if (!continue_rebase) {
5263 struct got_object_id *base_commit_id;
5265 base_commit_id = got_worktree_get_base_commit_id(worktree);
5266 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5267 base_commit_id, branch_head_commit_id, repo,
5268 check_cancelled, NULL);
5269 if (error)
5270 goto done;
5271 if (yca_id == NULL) {
5272 error = got_error_msg(GOT_ERR_ANCESTRY,
5273 "specified branch shares no common ancestry "
5274 "with work tree's branch");
5275 goto done;
5278 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5279 if (error) {
5280 if (error->code != GOT_ERR_ANCESTRY)
5281 goto done;
5282 error = NULL;
5283 } else {
5284 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5285 "specified branch resolves to a commit which "
5286 "is already contained in work tree's branch");
5287 goto done;
5289 error = got_worktree_rebase_prepare(&new_base_branch,
5290 &tmp_branch, &fileindex, worktree, branch, repo);
5291 if (error)
5292 goto done;
5295 commit_id = branch_head_commit_id;
5296 error = got_object_open_as_commit(&commit, repo, commit_id);
5297 if (error)
5298 goto done;
5300 parent_ids = got_object_commit_get_parent_ids(commit);
5301 pid = SIMPLEQ_FIRST(parent_ids);
5302 if (pid == NULL) {
5303 if (!continue_rebase) {
5304 int did_something;
5305 error = got_worktree_rebase_abort(worktree, fileindex,
5306 repo, new_base_branch, update_progress,
5307 &did_something);
5308 if (error)
5309 goto done;
5310 printf("Rebase of %s aborted\n",
5311 got_ref_get_name(branch));
5313 error = got_error(GOT_ERR_EMPTY_REBASE);
5314 goto done;
5316 error = collect_commits(&commits, commit_id, pid->id,
5317 yca_id, got_worktree_get_path_prefix(worktree),
5318 GOT_ERR_REBASE_PATH, repo);
5319 got_object_commit_close(commit);
5320 commit = NULL;
5321 if (error)
5322 goto done;
5324 if (SIMPLEQ_EMPTY(&commits)) {
5325 if (continue_rebase)
5326 error = rebase_complete(worktree, fileindex,
5327 branch, new_base_branch, tmp_branch, repo);
5328 else
5329 error = got_error(GOT_ERR_EMPTY_REBASE);
5330 goto done;
5333 pid = NULL;
5334 SIMPLEQ_FOREACH(qid, &commits, entry) {
5335 commit_id = qid->id;
5336 parent_id = pid ? pid->id : yca_id;
5337 pid = qid;
5339 error = got_worktree_rebase_merge_files(&merged_paths,
5340 worktree, fileindex, parent_id, commit_id, repo,
5341 rebase_progress, &rebase_status, check_cancelled, NULL);
5342 if (error)
5343 goto done;
5345 if (rebase_status == GOT_STATUS_CONFLICT) {
5346 got_worktree_rebase_pathlist_free(&merged_paths);
5347 break;
5350 error = rebase_commit(&merged_paths, worktree, fileindex,
5351 tmp_branch, commit_id, repo);
5352 got_worktree_rebase_pathlist_free(&merged_paths);
5353 if (error)
5354 goto done;
5357 if (rebase_status == GOT_STATUS_CONFLICT) {
5358 error = got_worktree_rebase_postpone(worktree, fileindex);
5359 if (error)
5360 goto done;
5361 error = got_error_msg(GOT_ERR_CONFLICTS,
5362 "conflicts must be resolved before rebasing can continue");
5363 } else
5364 error = rebase_complete(worktree, fileindex, branch,
5365 new_base_branch, tmp_branch, repo);
5366 done:
5367 got_object_id_queue_free(&commits);
5368 free(branch_head_commit_id);
5369 free(resume_commit_id);
5370 free(yca_id);
5371 if (commit)
5372 got_object_commit_close(commit);
5373 if (branch)
5374 got_ref_close(branch);
5375 if (new_base_branch)
5376 got_ref_close(new_base_branch);
5377 if (tmp_branch)
5378 got_ref_close(tmp_branch);
5379 if (worktree)
5380 got_worktree_close(worktree);
5381 if (repo)
5382 got_repo_close(repo);
5383 return error;
5386 __dead static void
5387 usage_histedit(void)
5389 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5390 getprogname());
5391 exit(1);
5394 #define GOT_HISTEDIT_PICK 'p'
5395 #define GOT_HISTEDIT_EDIT 'e'
5396 #define GOT_HISTEDIT_FOLD 'f'
5397 #define GOT_HISTEDIT_DROP 'd'
5398 #define GOT_HISTEDIT_MESG 'm'
5400 static struct got_histedit_cmd {
5401 unsigned char code;
5402 const char *name;
5403 const char *desc;
5404 } got_histedit_cmds[] = {
5405 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5406 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5407 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5408 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5409 { GOT_HISTEDIT_MESG, "mesg",
5410 "single-line log message for commit above (open editor if empty)" },
5413 struct got_histedit_list_entry {
5414 TAILQ_ENTRY(got_histedit_list_entry) entry;
5415 struct got_object_id *commit_id;
5416 const struct got_histedit_cmd *cmd;
5417 char *logmsg;
5419 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5421 static const struct got_error *
5422 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5423 FILE *f, struct got_repository *repo)
5425 const struct got_error *err = NULL;
5426 char *logmsg = NULL, *id_str = NULL;
5427 struct got_commit_object *commit = NULL;
5428 int n;
5430 err = got_object_open_as_commit(&commit, repo, commit_id);
5431 if (err)
5432 goto done;
5434 err = get_short_logmsg(&logmsg, 34, commit);
5435 if (err)
5436 goto done;
5438 err = got_object_id_str(&id_str, commit_id);
5439 if (err)
5440 goto done;
5442 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5443 if (n < 0)
5444 err = got_ferror(f, GOT_ERR_IO);
5445 done:
5446 if (commit)
5447 got_object_commit_close(commit);
5448 free(id_str);
5449 free(logmsg);
5450 return err;
5453 static const struct got_error *
5454 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5455 struct got_repository *repo)
5457 const struct got_error *err = NULL;
5458 struct got_object_qid *qid;
5460 if (SIMPLEQ_EMPTY(commits))
5461 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5463 SIMPLEQ_FOREACH(qid, commits, entry) {
5464 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5465 f, repo);
5466 if (err)
5467 break;
5470 return err;
5473 static const struct got_error *
5474 write_cmd_list(FILE *f)
5476 const struct got_error *err = NULL;
5477 int n, i;
5479 n = fprintf(f, "# Available histedit commands:\n");
5480 if (n < 0)
5481 return got_ferror(f, GOT_ERR_IO);
5483 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5484 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5485 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5486 cmd->desc);
5487 if (n < 0) {
5488 err = got_ferror(f, GOT_ERR_IO);
5489 break;
5492 n = fprintf(f, "# Commits will be processed in order from top to "
5493 "bottom of this file.\n");
5494 if (n < 0)
5495 return got_ferror(f, GOT_ERR_IO);
5496 return err;
5499 static const struct got_error *
5500 histedit_syntax_error(int lineno)
5502 static char msg[42];
5503 int ret;
5505 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5506 lineno);
5507 if (ret == -1 || ret >= sizeof(msg))
5508 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5510 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5513 static const struct got_error *
5514 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5515 char *logmsg, struct got_repository *repo)
5517 const struct got_error *err;
5518 struct got_commit_object *folded_commit = NULL;
5519 char *id_str, *folded_logmsg = NULL;
5521 err = got_object_id_str(&id_str, hle->commit_id);
5522 if (err)
5523 return err;
5525 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5526 if (err)
5527 goto done;
5529 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5530 if (err)
5531 goto done;
5532 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5533 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5534 folded_logmsg) == -1) {
5535 err = got_error_from_errno("asprintf");
5536 goto done;
5538 done:
5539 if (folded_commit)
5540 got_object_commit_close(folded_commit);
5541 free(id_str);
5542 free(folded_logmsg);
5543 return err;
5546 static struct got_histedit_list_entry *
5547 get_folded_commits(struct got_histedit_list_entry *hle)
5549 struct got_histedit_list_entry *prev, *folded = NULL;
5551 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5552 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5553 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5554 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5555 folded = prev;
5556 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5559 return folded;
5562 static const struct got_error *
5563 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5564 struct got_repository *repo)
5566 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5567 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5568 const struct got_error *err = NULL;
5569 struct got_commit_object *commit = NULL;
5570 int fd;
5571 struct got_histedit_list_entry *folded = NULL;
5573 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5574 if (err)
5575 return err;
5577 folded = get_folded_commits(hle);
5578 if (folded) {
5579 while (folded != hle) {
5580 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5581 folded = TAILQ_NEXT(folded, entry);
5582 continue;
5584 err = append_folded_commit_msg(&new_msg, folded,
5585 logmsg, repo);
5586 if (err)
5587 goto done;
5588 free(logmsg);
5589 logmsg = new_msg;
5590 folded = TAILQ_NEXT(folded, entry);
5594 err = got_object_id_str(&id_str, hle->commit_id);
5595 if (err)
5596 goto done;
5597 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5598 if (err)
5599 goto done;
5600 if (asprintf(&new_msg,
5601 "%s\n# original log message of commit %s: %s",
5602 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5603 err = got_error_from_errno("asprintf");
5604 goto done;
5606 free(logmsg);
5607 logmsg = new_msg;
5609 err = got_object_id_str(&id_str, hle->commit_id);
5610 if (err)
5611 goto done;
5613 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5614 if (err)
5615 goto done;
5617 dprintf(fd, logmsg);
5618 close(fd);
5620 err = get_editor(&editor);
5621 if (err)
5622 goto done;
5624 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5625 if (err) {
5626 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5627 goto done;
5628 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5630 done:
5631 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5632 err = got_error_from_errno2("unlink", logmsg_path);
5633 free(logmsg_path);
5634 free(logmsg);
5635 free(orig_logmsg);
5636 free(editor);
5637 if (commit)
5638 got_object_commit_close(commit);
5639 return err;
5642 static const struct got_error *
5643 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5644 FILE *f, struct got_repository *repo)
5646 const struct got_error *err = NULL;
5647 char *line = NULL, *p, *end;
5648 size_t size;
5649 ssize_t len;
5650 int lineno = 0, i;
5651 const struct got_histedit_cmd *cmd;
5652 struct got_object_id *commit_id = NULL;
5653 struct got_histedit_list_entry *hle = NULL;
5655 for (;;) {
5656 len = getline(&line, &size, f);
5657 if (len == -1) {
5658 const struct got_error *getline_err;
5659 if (feof(f))
5660 break;
5661 getline_err = got_error_from_errno("getline");
5662 err = got_ferror(f, getline_err->code);
5663 break;
5665 lineno++;
5666 p = line;
5667 while (isspace((unsigned char)p[0]))
5668 p++;
5669 if (p[0] == '#' || p[0] == '\0') {
5670 free(line);
5671 line = NULL;
5672 continue;
5674 cmd = NULL;
5675 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5676 cmd = &got_histedit_cmds[i];
5677 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5678 isspace((unsigned char)p[strlen(cmd->name)])) {
5679 p += strlen(cmd->name);
5680 break;
5682 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5683 p++;
5684 break;
5687 if (i == nitems(got_histedit_cmds)) {
5688 err = histedit_syntax_error(lineno);
5689 break;
5691 while (isspace((unsigned char)p[0]))
5692 p++;
5693 if (cmd->code == GOT_HISTEDIT_MESG) {
5694 if (hle == NULL || hle->logmsg != NULL) {
5695 err = got_error(GOT_ERR_HISTEDIT_CMD);
5696 break;
5698 if (p[0] == '\0') {
5699 err = histedit_edit_logmsg(hle, repo);
5700 if (err)
5701 break;
5702 } else {
5703 hle->logmsg = strdup(p);
5704 if (hle->logmsg == NULL) {
5705 err = got_error_from_errno("strdup");
5706 break;
5709 free(line);
5710 line = NULL;
5711 continue;
5712 } else {
5713 end = p;
5714 while (end[0] && !isspace((unsigned char)end[0]))
5715 end++;
5716 *end = '\0';
5718 err = got_object_resolve_id_str(&commit_id, repo, p);
5719 if (err) {
5720 /* override error code */
5721 err = histedit_syntax_error(lineno);
5722 break;
5725 hle = malloc(sizeof(*hle));
5726 if (hle == NULL) {
5727 err = got_error_from_errno("malloc");
5728 break;
5730 hle->cmd = cmd;
5731 hle->commit_id = commit_id;
5732 hle->logmsg = NULL;
5733 commit_id = NULL;
5734 free(line);
5735 line = NULL;
5736 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5739 free(line);
5740 free(commit_id);
5741 return err;
5744 static const struct got_error *
5745 histedit_check_script(struct got_histedit_list *histedit_cmds,
5746 struct got_object_id_queue *commits, struct got_repository *repo)
5748 const struct got_error *err = NULL;
5749 struct got_object_qid *qid;
5750 struct got_histedit_list_entry *hle;
5751 static char msg[80];
5752 char *id_str;
5754 if (TAILQ_EMPTY(histedit_cmds))
5755 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5756 "histedit script contains no commands");
5757 if (SIMPLEQ_EMPTY(commits))
5758 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5760 SIMPLEQ_FOREACH(qid, commits, entry) {
5761 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5762 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5763 break;
5765 if (hle == NULL) {
5766 err = got_object_id_str(&id_str, qid->id);
5767 if (err)
5768 return err;
5769 snprintf(msg, sizeof(msg),
5770 "commit %s missing from histedit script", id_str);
5771 free(id_str);
5772 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5776 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5777 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5778 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5779 "last commit in histedit script cannot be folded");
5781 return NULL;
5784 static const struct got_error *
5785 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5786 const char *path, struct got_object_id_queue *commits,
5787 struct got_repository *repo)
5789 const struct got_error *err = NULL;
5790 char *editor;
5791 FILE *f = NULL;
5793 err = get_editor(&editor);
5794 if (err)
5795 return err;
5797 if (spawn_editor(editor, path) == -1) {
5798 err = got_error_from_errno("failed spawning editor");
5799 goto done;
5802 f = fopen(path, "r");
5803 if (f == NULL) {
5804 err = got_error_from_errno("fopen");
5805 goto done;
5807 err = histedit_parse_list(histedit_cmds, f, repo);
5808 if (err)
5809 goto done;
5811 err = histedit_check_script(histedit_cmds, commits, repo);
5812 done:
5813 if (f && fclose(f) != 0 && err == NULL)
5814 err = got_error_from_errno("fclose");
5815 free(editor);
5816 return err;
5819 static const struct got_error *
5820 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5821 struct got_object_id_queue *, const char *, struct got_repository *);
5823 static const struct got_error *
5824 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5825 struct got_object_id_queue *commits, struct got_repository *repo)
5827 const struct got_error *err;
5828 FILE *f = NULL;
5829 char *path = NULL;
5831 err = got_opentemp_named(&path, &f, "got-histedit");
5832 if (err)
5833 return err;
5835 err = write_cmd_list(f);
5836 if (err)
5837 goto done;
5839 err = histedit_write_commit_list(commits, f, repo);
5840 if (err)
5841 goto done;
5843 if (fclose(f) != 0) {
5844 err = got_error_from_errno("fclose");
5845 goto done;
5847 f = NULL;
5849 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5850 if (err) {
5851 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5852 err->code != GOT_ERR_HISTEDIT_CMD)
5853 goto done;
5854 err = histedit_edit_list_retry(histedit_cmds, err,
5855 commits, path, repo);
5857 done:
5858 if (f && fclose(f) != 0 && err == NULL)
5859 err = got_error_from_errno("fclose");
5860 if (path && unlink(path) != 0 && err == NULL)
5861 err = got_error_from_errno2("unlink", path);
5862 free(path);
5863 return err;
5866 static const struct got_error *
5867 histedit_save_list(struct got_histedit_list *histedit_cmds,
5868 struct got_worktree *worktree, struct got_repository *repo)
5870 const struct got_error *err = NULL;
5871 char *path = NULL;
5872 FILE *f = NULL;
5873 struct got_histedit_list_entry *hle;
5874 struct got_commit_object *commit = NULL;
5876 err = got_worktree_get_histedit_script_path(&path, worktree);
5877 if (err)
5878 return err;
5880 f = fopen(path, "w");
5881 if (f == NULL) {
5882 err = got_error_from_errno2("fopen", path);
5883 goto done;
5885 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5886 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5887 repo);
5888 if (err)
5889 break;
5891 if (hle->logmsg) {
5892 int n = fprintf(f, "%c %s\n",
5893 GOT_HISTEDIT_MESG, hle->logmsg);
5894 if (n < 0) {
5895 err = got_ferror(f, GOT_ERR_IO);
5896 break;
5900 done:
5901 if (f && fclose(f) != 0 && err == NULL)
5902 err = got_error_from_errno("fclose");
5903 free(path);
5904 if (commit)
5905 got_object_commit_close(commit);
5906 return err;
5909 void
5910 histedit_free_list(struct got_histedit_list *histedit_cmds)
5912 struct got_histedit_list_entry *hle;
5914 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5915 TAILQ_REMOVE(histedit_cmds, hle, entry);
5916 free(hle);
5920 static const struct got_error *
5921 histedit_load_list(struct got_histedit_list *histedit_cmds,
5922 const char *path, struct got_repository *repo)
5924 const struct got_error *err = NULL;
5925 FILE *f = NULL;
5927 f = fopen(path, "r");
5928 if (f == NULL) {
5929 err = got_error_from_errno2("fopen", path);
5930 goto done;
5933 err = histedit_parse_list(histedit_cmds, f, repo);
5934 done:
5935 if (f && fclose(f) != 0 && err == NULL)
5936 err = got_error_from_errno("fclose");
5937 return err;
5940 static const struct got_error *
5941 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5942 const struct got_error *edit_err, struct got_object_id_queue *commits,
5943 const char *path, struct got_repository *repo)
5945 const struct got_error *err = NULL, *prev_err = edit_err;
5946 int resp = ' ';
5948 while (resp != 'c' && resp != 'r' && resp != 'a') {
5949 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5950 "or (a)bort: ", getprogname(), prev_err->msg);
5951 resp = getchar();
5952 if (resp == '\n')
5953 resp = getchar();
5954 if (resp == 'c') {
5955 histedit_free_list(histedit_cmds);
5956 err = histedit_run_editor(histedit_cmds, path, commits,
5957 repo);
5958 if (err) {
5959 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5960 err->code != GOT_ERR_HISTEDIT_CMD)
5961 break;
5962 prev_err = err;
5963 resp = ' ';
5964 continue;
5966 break;
5967 } else if (resp == 'r') {
5968 histedit_free_list(histedit_cmds);
5969 err = histedit_edit_script(histedit_cmds,
5970 commits, repo);
5971 if (err) {
5972 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5973 err->code != GOT_ERR_HISTEDIT_CMD)
5974 break;
5975 prev_err = err;
5976 resp = ' ';
5977 continue;
5979 break;
5980 } else if (resp == 'a') {
5981 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5982 break;
5983 } else
5984 printf("invalid response '%c'\n", resp);
5987 return err;
5990 static const struct got_error *
5991 histedit_complete(struct got_worktree *worktree,
5992 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5993 struct got_reference *branch, struct got_repository *repo)
5995 printf("Switching work tree to %s\n",
5996 got_ref_get_symref_target(branch));
5997 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5998 branch, repo);
6001 static const struct got_error *
6002 show_histedit_progress(struct got_commit_object *commit,
6003 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6005 const struct got_error *err;
6006 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6008 err = got_object_id_str(&old_id_str, hle->commit_id);
6009 if (err)
6010 goto done;
6012 if (new_id) {
6013 err = got_object_id_str(&new_id_str, new_id);
6014 if (err)
6015 goto done;
6018 old_id_str[12] = '\0';
6019 if (new_id_str)
6020 new_id_str[12] = '\0';
6022 if (hle->logmsg) {
6023 logmsg = strdup(hle->logmsg);
6024 if (logmsg == NULL) {
6025 err = got_error_from_errno("strdup");
6026 goto done;
6028 trim_logmsg(logmsg, 42);
6029 } else {
6030 err = get_short_logmsg(&logmsg, 42, commit);
6031 if (err)
6032 goto done;
6035 switch (hle->cmd->code) {
6036 case GOT_HISTEDIT_PICK:
6037 case GOT_HISTEDIT_EDIT:
6038 printf("%s -> %s: %s\n", old_id_str,
6039 new_id_str ? new_id_str : "no-op change", logmsg);
6040 break;
6041 case GOT_HISTEDIT_DROP:
6042 case GOT_HISTEDIT_FOLD:
6043 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6044 logmsg);
6045 break;
6046 default:
6047 break;
6050 done:
6051 free(old_id_str);
6052 free(new_id_str);
6053 return err;
6056 static const struct got_error *
6057 histedit_commit(struct got_pathlist_head *merged_paths,
6058 struct got_worktree *worktree, struct got_fileindex *fileindex,
6059 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6060 struct got_repository *repo)
6062 const struct got_error *err;
6063 struct got_commit_object *commit;
6064 struct got_object_id *new_commit_id;
6066 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6067 && hle->logmsg == NULL) {
6068 err = histedit_edit_logmsg(hle, repo);
6069 if (err)
6070 return err;
6073 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6074 if (err)
6075 return err;
6077 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6078 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6079 hle->logmsg, repo);
6080 if (err) {
6081 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6082 goto done;
6083 err = show_histedit_progress(commit, hle, NULL);
6084 } else {
6085 err = show_histedit_progress(commit, hle, new_commit_id);
6086 free(new_commit_id);
6088 done:
6089 got_object_commit_close(commit);
6090 return err;
6093 static const struct got_error *
6094 histedit_skip_commit(struct got_histedit_list_entry *hle,
6095 struct got_worktree *worktree, struct got_repository *repo)
6097 const struct got_error *error;
6098 struct got_commit_object *commit;
6100 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6101 repo);
6102 if (error)
6103 return error;
6105 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6106 if (error)
6107 return error;
6109 error = show_histedit_progress(commit, hle, NULL);
6110 got_object_commit_close(commit);
6111 return error;
6114 static const struct got_error *
6115 cmd_histedit(int argc, char *argv[])
6117 const struct got_error *error = NULL;
6118 struct got_worktree *worktree = NULL;
6119 struct got_fileindex *fileindex = NULL;
6120 struct got_repository *repo = NULL;
6121 char *cwd = NULL;
6122 struct got_reference *branch = NULL;
6123 struct got_reference *tmp_branch = NULL;
6124 struct got_object_id *resume_commit_id = NULL;
6125 struct got_object_id *base_commit_id = NULL;
6126 struct got_object_id *head_commit_id = NULL;
6127 struct got_commit_object *commit = NULL;
6128 int ch, rebase_in_progress = 0, did_something;
6129 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6130 const char *edit_script_path = NULL;
6131 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6132 struct got_object_id_queue commits;
6133 struct got_pathlist_head merged_paths;
6134 const struct got_object_id_queue *parent_ids;
6135 struct got_object_qid *pid;
6136 struct got_histedit_list histedit_cmds;
6137 struct got_histedit_list_entry *hle;
6139 SIMPLEQ_INIT(&commits);
6140 TAILQ_INIT(&histedit_cmds);
6141 TAILQ_INIT(&merged_paths);
6143 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6144 switch (ch) {
6145 case 'a':
6146 abort_edit = 1;
6147 break;
6148 case 'c':
6149 continue_edit = 1;
6150 break;
6151 case 'F':
6152 edit_script_path = optarg;
6153 break;
6154 default:
6155 usage_histedit();
6156 /* NOTREACHED */
6160 argc -= optind;
6161 argv += optind;
6163 #ifndef PROFILE
6164 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6165 "unveil", NULL) == -1)
6166 err(1, "pledge");
6167 #endif
6168 if (abort_edit && continue_edit)
6169 usage_histedit();
6170 if (argc != 0)
6171 usage_histedit();
6174 * This command cannot apply unveil(2) in all cases because the
6175 * user may choose to run an editor to edit the histedit script
6176 * and to edit individual commit log messages.
6177 * unveil(2) traverses exec(2); if an editor is used we have to
6178 * apply unveil after edit script and log messages have been written.
6179 * XXX TODO: Make use of unveil(2) where possible.
6182 cwd = getcwd(NULL, 0);
6183 if (cwd == NULL) {
6184 error = got_error_from_errno("getcwd");
6185 goto done;
6187 error = got_worktree_open(&worktree, cwd);
6188 if (error)
6189 goto done;
6191 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6192 NULL);
6193 if (error != NULL)
6194 goto done;
6196 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6197 if (error)
6198 goto done;
6199 if (rebase_in_progress) {
6200 error = got_error(GOT_ERR_REBASING);
6201 goto done;
6204 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6205 if (error)
6206 goto done;
6208 if (edit_in_progress && abort_edit) {
6209 error = got_worktree_histedit_continue(&resume_commit_id,
6210 &tmp_branch, &branch, &base_commit_id, &fileindex,
6211 worktree, repo);
6212 if (error)
6213 goto done;
6214 printf("Switching work tree to %s\n",
6215 got_ref_get_symref_target(branch));
6216 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6217 branch, base_commit_id, update_progress, &did_something);
6218 if (error)
6219 goto done;
6220 printf("Histedit of %s aborted\n",
6221 got_ref_get_symref_target(branch));
6222 goto done; /* nothing else to do */
6223 } else if (abort_edit) {
6224 error = got_error(GOT_ERR_NOT_HISTEDIT);
6225 goto done;
6228 if (continue_edit) {
6229 char *path;
6231 if (!edit_in_progress) {
6232 error = got_error(GOT_ERR_NOT_HISTEDIT);
6233 goto done;
6236 error = got_worktree_get_histedit_script_path(&path, worktree);
6237 if (error)
6238 goto done;
6240 error = histedit_load_list(&histedit_cmds, path, repo);
6241 free(path);
6242 if (error)
6243 goto done;
6245 error = got_worktree_histedit_continue(&resume_commit_id,
6246 &tmp_branch, &branch, &base_commit_id, &fileindex,
6247 worktree, repo);
6248 if (error)
6249 goto done;
6251 error = got_ref_resolve(&head_commit_id, repo, branch);
6252 if (error)
6253 goto done;
6255 error = got_object_open_as_commit(&commit, repo,
6256 head_commit_id);
6257 if (error)
6258 goto done;
6259 parent_ids = got_object_commit_get_parent_ids(commit);
6260 pid = SIMPLEQ_FIRST(parent_ids);
6261 if (pid == NULL) {
6262 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6263 goto done;
6265 error = collect_commits(&commits, head_commit_id, pid->id,
6266 base_commit_id, got_worktree_get_path_prefix(worktree),
6267 GOT_ERR_HISTEDIT_PATH, repo);
6268 got_object_commit_close(commit);
6269 commit = NULL;
6270 if (error)
6271 goto done;
6272 } else {
6273 if (edit_in_progress) {
6274 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6275 goto done;
6278 error = got_ref_open(&branch, repo,
6279 got_worktree_get_head_ref_name(worktree), 0);
6280 if (error != NULL)
6281 goto done;
6283 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6284 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6285 "will not edit commit history of a branch outside "
6286 "the \"refs/heads/\" reference namespace");
6287 goto done;
6290 error = got_ref_resolve(&head_commit_id, repo, branch);
6291 got_ref_close(branch);
6292 branch = NULL;
6293 if (error)
6294 goto done;
6296 error = got_object_open_as_commit(&commit, repo,
6297 head_commit_id);
6298 if (error)
6299 goto done;
6300 parent_ids = got_object_commit_get_parent_ids(commit);
6301 pid = SIMPLEQ_FIRST(parent_ids);
6302 if (pid == NULL) {
6303 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6304 goto done;
6306 error = collect_commits(&commits, head_commit_id, pid->id,
6307 got_worktree_get_base_commit_id(worktree),
6308 got_worktree_get_path_prefix(worktree),
6309 GOT_ERR_HISTEDIT_PATH, repo);
6310 got_object_commit_close(commit);
6311 commit = NULL;
6312 if (error)
6313 goto done;
6315 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6316 &base_commit_id, &fileindex, worktree, repo);
6317 if (error)
6318 goto done;
6320 if (edit_script_path) {
6321 error = histedit_load_list(&histedit_cmds,
6322 edit_script_path, repo);
6323 if (error) {
6324 got_worktree_histedit_abort(worktree, fileindex,
6325 repo, branch, base_commit_id,
6326 update_progress, &did_something);
6327 goto done;
6329 } else {
6330 error = histedit_edit_script(&histedit_cmds, &commits,
6331 repo);
6332 if (error) {
6333 got_worktree_histedit_abort(worktree, fileindex,
6334 repo, branch, base_commit_id,
6335 update_progress, &did_something);
6336 goto done;
6341 error = histedit_save_list(&histedit_cmds, worktree,
6342 repo);
6343 if (error) {
6344 got_worktree_histedit_abort(worktree, fileindex,
6345 repo, branch, base_commit_id,
6346 update_progress, &did_something);
6347 goto done;
6352 error = histedit_check_script(&histedit_cmds, &commits, repo);
6353 if (error)
6354 goto done;
6356 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6357 if (resume_commit_id) {
6358 if (got_object_id_cmp(hle->commit_id,
6359 resume_commit_id) != 0)
6360 continue;
6362 resume_commit_id = NULL;
6363 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6364 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6365 error = histedit_skip_commit(hle, worktree,
6366 repo);
6367 } else {
6368 error = histedit_commit(NULL, worktree,
6369 fileindex, tmp_branch, hle, repo);
6371 if (error)
6372 goto done;
6373 continue;
6376 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6377 error = histedit_skip_commit(hle, worktree, repo);
6378 if (error)
6379 goto done;
6380 continue;
6383 error = got_object_open_as_commit(&commit, repo,
6384 hle->commit_id);
6385 if (error)
6386 goto done;
6387 parent_ids = got_object_commit_get_parent_ids(commit);
6388 pid = SIMPLEQ_FIRST(parent_ids);
6390 error = got_worktree_histedit_merge_files(&merged_paths,
6391 worktree, fileindex, pid->id, hle->commit_id, repo,
6392 rebase_progress, &rebase_status, check_cancelled, NULL);
6393 if (error)
6394 goto done;
6395 got_object_commit_close(commit);
6396 commit = NULL;
6398 if (rebase_status == GOT_STATUS_CONFLICT) {
6399 got_worktree_rebase_pathlist_free(&merged_paths);
6400 break;
6403 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6404 char *id_str;
6405 error = got_object_id_str(&id_str, hle->commit_id);
6406 if (error)
6407 goto done;
6408 printf("Stopping histedit for amending commit %s\n",
6409 id_str);
6410 free(id_str);
6411 got_worktree_rebase_pathlist_free(&merged_paths);
6412 error = got_worktree_histedit_postpone(worktree,
6413 fileindex);
6414 goto done;
6417 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6418 error = histedit_skip_commit(hle, worktree, repo);
6419 if (error)
6420 goto done;
6421 continue;
6424 error = histedit_commit(&merged_paths, worktree, fileindex,
6425 tmp_branch, hle, repo);
6426 got_worktree_rebase_pathlist_free(&merged_paths);
6427 if (error)
6428 goto done;
6431 if (rebase_status == GOT_STATUS_CONFLICT) {
6432 error = got_worktree_histedit_postpone(worktree, fileindex);
6433 if (error)
6434 goto done;
6435 error = got_error_msg(GOT_ERR_CONFLICTS,
6436 "conflicts must be resolved before rebasing can continue");
6437 } else
6438 error = histedit_complete(worktree, fileindex, tmp_branch,
6439 branch, repo);
6440 done:
6441 got_object_id_queue_free(&commits);
6442 histedit_free_list(&histedit_cmds);
6443 free(head_commit_id);
6444 free(base_commit_id);
6445 free(resume_commit_id);
6446 if (commit)
6447 got_object_commit_close(commit);
6448 if (branch)
6449 got_ref_close(branch);
6450 if (tmp_branch)
6451 got_ref_close(tmp_branch);
6452 if (worktree)
6453 got_worktree_close(worktree);
6454 if (repo)
6455 got_repo_close(repo);
6456 return error;
6459 __dead static void
6460 usage_stage(void)
6462 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6463 "[file-path ...]\n",
6464 getprogname());
6465 exit(1);
6468 static const struct got_error *
6469 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6470 const char *path, struct got_object_id *blob_id,
6471 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6473 const struct got_error *err = NULL;
6474 char *id_str = NULL;
6476 if (staged_status != GOT_STATUS_ADD &&
6477 staged_status != GOT_STATUS_MODIFY &&
6478 staged_status != GOT_STATUS_DELETE)
6479 return NULL;
6481 if (staged_status == GOT_STATUS_ADD ||
6482 staged_status == GOT_STATUS_MODIFY)
6483 err = got_object_id_str(&id_str, staged_blob_id);
6484 else
6485 err = got_object_id_str(&id_str, blob_id);
6486 if (err)
6487 return err;
6489 printf("%s %c %s\n", id_str, staged_status, path);
6490 free(id_str);
6491 return NULL;
6494 static const struct got_error *
6495 cmd_stage(int argc, char *argv[])
6497 const struct got_error *error = NULL;
6498 struct got_repository *repo = NULL;
6499 struct got_worktree *worktree = NULL;
6500 char *cwd = NULL;
6501 struct got_pathlist_head paths;
6502 struct got_pathlist_entry *pe;
6503 int ch, list_stage = 0, pflag = 0;
6504 FILE *patch_script_file = NULL;
6505 const char *patch_script_path = NULL;
6506 struct choose_patch_arg cpa;
6508 TAILQ_INIT(&paths);
6510 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6511 switch (ch) {
6512 case 'l':
6513 list_stage = 1;
6514 break;
6515 case 'p':
6516 pflag = 1;
6517 break;
6518 case 'F':
6519 patch_script_path = optarg;
6520 break;
6521 default:
6522 usage_stage();
6523 /* NOTREACHED */
6527 argc -= optind;
6528 argv += optind;
6530 #ifndef PROFILE
6531 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6532 "unveil", NULL) == -1)
6533 err(1, "pledge");
6534 #endif
6535 if (list_stage && (pflag || patch_script_path))
6536 errx(1, "-l option cannot be used with other options");
6537 if (patch_script_path && !pflag)
6538 errx(1, "-F option can only be used together with -p option");
6540 cwd = getcwd(NULL, 0);
6541 if (cwd == NULL) {
6542 error = got_error_from_errno("getcwd");
6543 goto done;
6546 error = got_worktree_open(&worktree, cwd);
6547 if (error)
6548 goto done;
6550 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6551 NULL);
6552 if (error != NULL)
6553 goto done;
6555 if (patch_script_path) {
6556 patch_script_file = fopen(patch_script_path, "r");
6557 if (patch_script_file == NULL) {
6558 error = got_error_from_errno2("fopen",
6559 patch_script_path);
6560 goto done;
6563 error = apply_unveil(got_repo_get_path(repo), 0,
6564 got_worktree_get_root_path(worktree));
6565 if (error)
6566 goto done;
6568 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6569 if (error)
6570 goto done;
6572 if (list_stage)
6573 error = got_worktree_status(worktree, &paths, repo,
6574 print_stage, NULL, check_cancelled, NULL);
6575 else {
6576 cpa.patch_script_file = patch_script_file;
6577 cpa.action = "stage";
6578 error = got_worktree_stage(worktree, &paths,
6579 pflag ? NULL : print_status, NULL,
6580 pflag ? choose_patch : NULL, &cpa, repo);
6582 done:
6583 if (patch_script_file && fclose(patch_script_file) == EOF &&
6584 error == NULL)
6585 error = got_error_from_errno2("fclose", patch_script_path);
6586 if (repo)
6587 got_repo_close(repo);
6588 if (worktree)
6589 got_worktree_close(worktree);
6590 TAILQ_FOREACH(pe, &paths, entry)
6591 free((char *)pe->path);
6592 got_pathlist_free(&paths);
6593 free(cwd);
6594 return error;
6597 __dead static void
6598 usage_unstage(void)
6600 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6601 "[file-path ...]\n",
6602 getprogname());
6603 exit(1);
6607 static const struct got_error *
6608 cmd_unstage(int argc, char *argv[])
6610 const struct got_error *error = NULL;
6611 struct got_repository *repo = NULL;
6612 struct got_worktree *worktree = NULL;
6613 char *cwd = NULL;
6614 struct got_pathlist_head paths;
6615 struct got_pathlist_entry *pe;
6616 int ch, did_something = 0, pflag = 0;
6617 FILE *patch_script_file = NULL;
6618 const char *patch_script_path = NULL;
6619 struct choose_patch_arg cpa;
6621 TAILQ_INIT(&paths);
6623 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6624 switch (ch) {
6625 case 'p':
6626 pflag = 1;
6627 break;
6628 case 'F':
6629 patch_script_path = optarg;
6630 break;
6631 default:
6632 usage_unstage();
6633 /* NOTREACHED */
6637 argc -= optind;
6638 argv += optind;
6640 #ifndef PROFILE
6641 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6642 "unveil", NULL) == -1)
6643 err(1, "pledge");
6644 #endif
6645 if (patch_script_path && !pflag)
6646 errx(1, "-F option can only be used together with -p option");
6648 cwd = getcwd(NULL, 0);
6649 if (cwd == NULL) {
6650 error = got_error_from_errno("getcwd");
6651 goto done;
6654 error = got_worktree_open(&worktree, cwd);
6655 if (error)
6656 goto done;
6658 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6659 NULL);
6660 if (error != NULL)
6661 goto done;
6663 if (patch_script_path) {
6664 patch_script_file = fopen(patch_script_path, "r");
6665 if (patch_script_file == NULL) {
6666 error = got_error_from_errno2("fopen",
6667 patch_script_path);
6668 goto done;
6672 error = apply_unveil(got_repo_get_path(repo), 0,
6673 got_worktree_get_root_path(worktree));
6674 if (error)
6675 goto done;
6677 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6678 if (error)
6679 goto done;
6681 cpa.patch_script_file = patch_script_file;
6682 cpa.action = "unstage";
6683 error = got_worktree_unstage(worktree, &paths, update_progress,
6684 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6685 done:
6686 if (patch_script_file && fclose(patch_script_file) == EOF &&
6687 error == NULL)
6688 error = got_error_from_errno2("fclose", patch_script_path);
6689 if (repo)
6690 got_repo_close(repo);
6691 if (worktree)
6692 got_worktree_close(worktree);
6693 TAILQ_FOREACH(pe, &paths, entry)
6694 free((char *)pe->path);
6695 got_pathlist_free(&paths);
6696 free(cwd);
6697 return error;
6700 __dead static void
6701 usage_cat(void)
6703 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6704 "arg1 [arg2 ...]\n", getprogname());
6705 exit(1);
6708 static const struct got_error *
6709 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6711 const struct got_error *err;
6712 struct got_blob_object *blob;
6714 err = got_object_open_as_blob(&blob, repo, id, 8192);
6715 if (err)
6716 return err;
6718 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6719 got_object_blob_close(blob);
6720 return err;
6723 static const struct got_error *
6724 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6726 const struct got_error *err;
6727 struct got_tree_object *tree;
6728 const struct got_tree_entries *entries;
6729 struct got_tree_entry *te;
6731 err = got_object_open_as_tree(&tree, repo, id);
6732 if (err)
6733 return err;
6735 entries = got_object_tree_get_entries(tree);
6736 te = SIMPLEQ_FIRST(&entries->head);
6737 while (te) {
6738 char *id_str;
6739 if (sigint_received || sigpipe_received)
6740 break;
6741 err = got_object_id_str(&id_str, te->id);
6742 if (err)
6743 break;
6744 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6745 free(id_str);
6746 te = SIMPLEQ_NEXT(te, entry);
6749 got_object_tree_close(tree);
6750 return err;
6753 static const struct got_error *
6754 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6756 const struct got_error *err;
6757 struct got_commit_object *commit;
6758 const struct got_object_id_queue *parent_ids;
6759 struct got_object_qid *pid;
6760 char *id_str = NULL;
6761 const char *logmsg = NULL;
6763 err = got_object_open_as_commit(&commit, repo, id);
6764 if (err)
6765 return err;
6767 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6768 if (err)
6769 goto done;
6771 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6772 parent_ids = got_object_commit_get_parent_ids(commit);
6773 fprintf(outfile, "numparents %d\n",
6774 got_object_commit_get_nparents(commit));
6775 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6776 char *pid_str;
6777 err = got_object_id_str(&pid_str, pid->id);
6778 if (err)
6779 goto done;
6780 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6781 free(pid_str);
6783 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6784 got_object_commit_get_author(commit),
6785 got_object_commit_get_author_time(commit));
6787 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6788 got_object_commit_get_author(commit),
6789 got_object_commit_get_committer_time(commit));
6791 logmsg = got_object_commit_get_logmsg_raw(commit);
6792 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6793 fprintf(outfile, "%s", logmsg);
6794 done:
6795 free(id_str);
6796 got_object_commit_close(commit);
6797 return err;
6800 static const struct got_error *
6801 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6803 const struct got_error *err;
6804 struct got_tag_object *tag;
6805 char *id_str = NULL;
6806 const char *tagmsg = NULL;
6808 err = got_object_open_as_tag(&tag, repo, id);
6809 if (err)
6810 return err;
6812 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6813 if (err)
6814 goto done;
6816 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6818 switch (got_object_tag_get_object_type(tag)) {
6819 case GOT_OBJ_TYPE_BLOB:
6820 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6821 GOT_OBJ_LABEL_BLOB);
6822 break;
6823 case GOT_OBJ_TYPE_TREE:
6824 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6825 GOT_OBJ_LABEL_TREE);
6826 break;
6827 case GOT_OBJ_TYPE_COMMIT:
6828 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6829 GOT_OBJ_LABEL_COMMIT);
6830 break;
6831 case GOT_OBJ_TYPE_TAG:
6832 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6833 GOT_OBJ_LABEL_TAG);
6834 break;
6835 default:
6836 break;
6839 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6840 got_object_tag_get_name(tag));
6842 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6843 got_object_tag_get_tagger(tag),
6844 got_object_tag_get_tagger_time(tag));
6846 tagmsg = got_object_tag_get_message(tag);
6847 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6848 fprintf(outfile, "%s", tagmsg);
6849 done:
6850 free(id_str);
6851 got_object_tag_close(tag);
6852 return err;
6855 static const struct got_error *
6856 cmd_cat(int argc, char *argv[])
6858 const struct got_error *error;
6859 struct got_repository *repo = NULL;
6860 struct got_worktree *worktree = NULL;
6861 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6862 const char *commit_id_str = NULL;
6863 struct got_object_id *id = NULL, *commit_id = NULL;
6864 int ch, obj_type, i, force_path = 0;
6866 #ifndef PROFILE
6867 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6868 NULL) == -1)
6869 err(1, "pledge");
6870 #endif
6872 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6873 switch (ch) {
6874 case 'c':
6875 commit_id_str = optarg;
6876 break;
6877 case 'r':
6878 repo_path = realpath(optarg, NULL);
6879 if (repo_path == NULL)
6880 err(1, "-r option");
6881 got_path_strip_trailing_slashes(repo_path);
6882 break;
6883 case 'P':
6884 force_path = 1;
6885 break;
6886 default:
6887 usage_cat();
6888 /* NOTREACHED */
6892 argc -= optind;
6893 argv += optind;
6895 cwd = getcwd(NULL, 0);
6896 if (cwd == NULL) {
6897 error = got_error_from_errno("getcwd");
6898 goto done;
6900 error = got_worktree_open(&worktree, cwd);
6901 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6902 goto done;
6903 if (worktree) {
6904 if (repo_path == NULL) {
6905 repo_path = strdup(
6906 got_worktree_get_repo_path(worktree));
6907 if (repo_path == NULL) {
6908 error = got_error_from_errno("strdup");
6909 goto done;
6914 if (repo_path == NULL) {
6915 repo_path = getcwd(NULL, 0);
6916 if (repo_path == NULL)
6917 return got_error_from_errno("getcwd");
6920 error = got_repo_open(&repo, repo_path, NULL);
6921 free(repo_path);
6922 if (error != NULL)
6923 goto done;
6925 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6926 if (error)
6927 goto done;
6929 if (commit_id_str == NULL)
6930 commit_id_str = GOT_REF_HEAD;
6931 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6932 if (error)
6933 goto done;
6935 for (i = 0; i < argc; i++) {
6936 if (force_path) {
6937 error = got_object_id_by_path(&id, repo, commit_id,
6938 argv[i]);
6939 if (error)
6940 break;
6941 } else {
6942 error = match_object_id(&id, &label, argv[i],
6943 GOT_OBJ_TYPE_ANY, 0, repo);
6944 if (error) {
6945 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6946 error->code != GOT_ERR_NOT_REF)
6947 break;
6948 error = got_object_id_by_path(&id, repo,
6949 commit_id, argv[i]);
6950 if (error)
6951 break;
6955 error = got_object_get_type(&obj_type, repo, id);
6956 if (error)
6957 break;
6959 switch (obj_type) {
6960 case GOT_OBJ_TYPE_BLOB:
6961 error = cat_blob(id, repo, stdout);
6962 break;
6963 case GOT_OBJ_TYPE_TREE:
6964 error = cat_tree(id, repo, stdout);
6965 break;
6966 case GOT_OBJ_TYPE_COMMIT:
6967 error = cat_commit(id, repo, stdout);
6968 break;
6969 case GOT_OBJ_TYPE_TAG:
6970 error = cat_tag(id, repo, stdout);
6971 break;
6972 default:
6973 error = got_error(GOT_ERR_OBJ_TYPE);
6974 break;
6976 if (error)
6977 break;
6978 free(label);
6979 label = NULL;
6980 free(id);
6981 id = NULL;
6984 done:
6985 free(label);
6986 free(id);
6987 free(commit_id);
6988 if (worktree)
6989 got_worktree_close(worktree);
6990 if (repo) {
6991 const struct got_error *repo_error;
6992 repo_error = got_repo_close(repo);
6993 if (error == NULL)
6994 error = repo_error;
6996 return error;