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 if (tree2)
1426 got_object_tree_close(tree2);
1427 return err;
1430 static const struct got_error *
1431 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1432 const char *path, int diff_context, struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 struct got_commit_object *pcommit = NULL;
1436 char *id_str1 = NULL, *id_str2 = NULL;
1437 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1438 struct got_object_qid *qid;
1440 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1441 if (qid != NULL) {
1442 err = got_object_open_as_commit(&pcommit, repo,
1443 qid->id);
1444 if (err)
1445 return err;
1448 if (path && path[0] != '\0') {
1449 int obj_type;
1450 err = got_object_id_by_path(&obj_id2, repo, id, path);
1451 if (err)
1452 goto done;
1453 err = got_object_id_str(&id_str2, obj_id2);
1454 if (err) {
1455 free(obj_id2);
1456 goto done;
1458 if (pcommit) {
1459 err = got_object_id_by_path(&obj_id1, repo,
1460 qid->id, path);
1461 if (err) {
1462 free(obj_id2);
1463 goto done;
1465 err = got_object_id_str(&id_str1, obj_id1);
1466 if (err) {
1467 free(obj_id2);
1468 goto done;
1471 err = got_object_get_type(&obj_type, repo, obj_id2);
1472 if (err) {
1473 free(obj_id2);
1474 goto done;
1476 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1477 switch (obj_type) {
1478 case GOT_OBJ_TYPE_BLOB:
1479 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1480 0, repo);
1481 break;
1482 case GOT_OBJ_TYPE_TREE:
1483 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1484 0, repo);
1485 break;
1486 default:
1487 err = got_error(GOT_ERR_OBJ_TYPE);
1488 break;
1490 free(obj_id1);
1491 free(obj_id2);
1492 } else {
1493 obj_id2 = got_object_commit_get_tree_id(commit);
1494 err = got_object_id_str(&id_str2, obj_id2);
1495 if (err)
1496 goto done;
1497 obj_id1 = got_object_commit_get_tree_id(pcommit);
1498 err = got_object_id_str(&id_str1, obj_id1);
1499 if (err)
1500 goto done;
1501 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1502 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1505 done:
1506 free(id_str1);
1507 free(id_str2);
1508 if (pcommit)
1509 got_object_commit_close(pcommit);
1510 return err;
1513 static char *
1514 get_datestr(time_t *time, char *datebuf)
1516 struct tm mytm, *tm;
1517 char *p, *s;
1519 tm = gmtime_r(time, &mytm);
1520 if (tm == NULL)
1521 return NULL;
1522 s = asctime_r(tm, datebuf);
1523 if (s == NULL)
1524 return NULL;
1525 p = strchr(s, '\n');
1526 if (p)
1527 *p = '\0';
1528 return s;
1531 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1533 static const struct got_error *
1534 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1535 struct got_repository *repo, const char *path, int show_patch,
1536 int diff_context, struct got_reflist_head *refs)
1538 const struct got_error *err = NULL;
1539 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1540 char datebuf[26];
1541 time_t committer_time;
1542 const char *author, *committer;
1543 char *refs_str = NULL;
1544 struct got_reflist_entry *re;
1546 SIMPLEQ_FOREACH(re, refs, entry) {
1547 char *s;
1548 const char *name;
1549 struct got_tag_object *tag = NULL;
1550 int cmp;
1552 name = got_ref_get_name(re->ref);
1553 if (strcmp(name, GOT_REF_HEAD) == 0)
1554 continue;
1555 if (strncmp(name, "refs/", 5) == 0)
1556 name += 5;
1557 if (strncmp(name, "got/", 4) == 0)
1558 continue;
1559 if (strncmp(name, "heads/", 6) == 0)
1560 name += 6;
1561 if (strncmp(name, "remotes/", 8) == 0)
1562 name += 8;
1563 if (strncmp(name, "tags/", 5) == 0) {
1564 err = got_object_open_as_tag(&tag, repo, re->id);
1565 if (err) {
1566 if (err->code != GOT_ERR_OBJ_TYPE)
1567 return err;
1568 /* Ref points at something other than a tag. */
1569 err = NULL;
1570 tag = NULL;
1573 cmp = got_object_id_cmp(tag ?
1574 got_object_tag_get_object_id(tag) : re->id, id);
1575 if (tag)
1576 got_object_tag_close(tag);
1577 if (cmp != 0)
1578 continue;
1579 s = refs_str;
1580 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1581 name) == -1) {
1582 err = got_error_from_errno("asprintf");
1583 free(s);
1584 return err;
1586 free(s);
1588 err = got_object_id_str(&id_str, id);
1589 if (err)
1590 return err;
1592 printf(GOT_COMMIT_SEP_STR);
1593 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1594 refs_str ? refs_str : "", refs_str ? ")" : "");
1595 free(id_str);
1596 id_str = NULL;
1597 free(refs_str);
1598 refs_str = NULL;
1599 printf("from: %s\n", got_object_commit_get_author(commit));
1600 committer_time = got_object_commit_get_committer_time(commit);
1601 datestr = get_datestr(&committer_time, datebuf);
1602 if (datestr)
1603 printf("date: %s UTC\n", datestr);
1604 author = got_object_commit_get_author(commit);
1605 committer = got_object_commit_get_committer(commit);
1606 if (strcmp(author, committer) != 0)
1607 printf("via: %s\n", committer);
1608 if (got_object_commit_get_nparents(commit) > 1) {
1609 const struct got_object_id_queue *parent_ids;
1610 struct got_object_qid *qid;
1611 int n = 1;
1612 parent_ids = got_object_commit_get_parent_ids(commit);
1613 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1614 err = got_object_id_str(&id_str, qid->id);
1615 if (err)
1616 return err;
1617 printf("parent %d: %s\n", n++, id_str);
1618 free(id_str);
1622 err = got_object_commit_get_logmsg(&logmsg0, commit);
1623 if (err)
1624 return err;
1626 logmsg = logmsg0;
1627 do {
1628 line = strsep(&logmsg, "\n");
1629 if (line)
1630 printf(" %s\n", line);
1631 } while (line);
1632 free(logmsg0);
1634 if (show_patch) {
1635 err = print_patch(commit, id, path, diff_context, repo);
1636 if (err == 0)
1637 printf("\n");
1640 if (fflush(stdout) != 0 && err == NULL)
1641 err = got_error_from_errno("fflush");
1642 return err;
1645 static const struct got_error *
1646 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1647 char *path, int show_patch, int diff_context, int limit,
1648 int first_parent_traversal, struct got_reflist_head *refs)
1650 const struct got_error *err;
1651 struct got_commit_graph *graph;
1653 err = got_commit_graph_open(&graph, root_id, path,
1654 first_parent_traversal, repo);
1655 if (err)
1656 return err;
1657 err = got_commit_graph_iter_start(graph, root_id, repo,
1658 check_cancelled, NULL);
1659 if (err)
1660 goto done;
1661 for (;;) {
1662 struct got_commit_object *commit;
1663 struct got_object_id *id;
1665 if (sigint_received || sigpipe_received)
1666 break;
1668 err = got_commit_graph_iter_next(&id, graph);
1669 if (err) {
1670 if (err->code == GOT_ERR_ITER_COMPLETED) {
1671 err = NULL;
1672 break;
1674 if (err->code != GOT_ERR_ITER_NEED_MORE)
1675 break;
1676 err = got_commit_graph_fetch_commits(graph, 1, repo,
1677 check_cancelled, NULL);
1678 if (err)
1679 break;
1680 else
1681 continue;
1683 if (id == NULL)
1684 break;
1686 err = got_object_open_as_commit(&commit, repo, id);
1687 if (err)
1688 break;
1689 err = print_commit(commit, id, repo, path, show_patch,
1690 diff_context, refs);
1691 got_object_commit_close(commit);
1692 if (err || (limit && --limit == 0))
1693 break;
1695 done:
1696 got_commit_graph_close(graph);
1697 return err;
1700 __dead static void
1701 usage_log(void)
1703 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1704 "[-r repository-path] [path]\n", getprogname());
1705 exit(1);
1708 static int
1709 get_default_log_limit(void)
1711 const char *got_default_log_limit;
1712 long long n;
1713 const char *errstr;
1715 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1716 if (got_default_log_limit == NULL)
1717 return 0;
1718 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1719 if (errstr != NULL)
1720 return 0;
1721 return n;
1724 static const struct got_error *
1725 cmd_log(int argc, char *argv[])
1727 const struct got_error *error;
1728 struct got_repository *repo = NULL;
1729 struct got_worktree *worktree = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_object_id *id = NULL;
1732 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1733 char *start_commit = NULL;
1734 int diff_context = 3, ch;
1735 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1736 const char *errstr;
1737 struct got_reflist_head refs;
1739 SIMPLEQ_INIT(&refs);
1741 #ifndef PROFILE
1742 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1743 NULL)
1744 == -1)
1745 err(1, "pledge");
1746 #endif
1748 limit = get_default_log_limit();
1750 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1751 switch (ch) {
1752 case 'p':
1753 show_patch = 1;
1754 break;
1755 case 'c':
1756 start_commit = optarg;
1757 break;
1758 case 'C':
1759 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1760 &errstr);
1761 if (errstr != NULL)
1762 err(1, "-C option %s", errstr);
1763 break;
1764 case 'l':
1765 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1766 if (errstr != NULL)
1767 err(1, "-l option %s", errstr);
1768 break;
1769 case 'f':
1770 first_parent_traversal = 1;
1771 break;
1772 case 'r':
1773 repo_path = realpath(optarg, NULL);
1774 if (repo_path == NULL)
1775 err(1, "-r option");
1776 got_path_strip_trailing_slashes(repo_path);
1777 break;
1778 default:
1779 usage_log();
1780 /* NOTREACHED */
1784 argc -= optind;
1785 argv += optind;
1787 cwd = getcwd(NULL, 0);
1788 if (cwd == NULL) {
1789 error = got_error_from_errno("getcwd");
1790 goto done;
1793 error = got_worktree_open(&worktree, cwd);
1794 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1795 goto done;
1796 error = NULL;
1798 if (argc == 0) {
1799 path = strdup("");
1800 if (path == NULL) {
1801 error = got_error_from_errno("strdup");
1802 goto done;
1804 } else if (argc == 1) {
1805 if (worktree) {
1806 error = got_worktree_resolve_path(&path, worktree,
1807 argv[0]);
1808 if (error)
1809 goto done;
1810 } else {
1811 path = strdup(argv[0]);
1812 if (path == NULL) {
1813 error = got_error_from_errno("strdup");
1814 goto done;
1817 } else
1818 usage_log();
1820 if (repo_path == NULL) {
1821 repo_path = worktree ?
1822 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1824 if (repo_path == NULL) {
1825 error = got_error_from_errno("strdup");
1826 goto done;
1829 error = got_repo_open(&repo, repo_path, NULL);
1830 if (error != NULL)
1831 goto done;
1833 error = apply_unveil(got_repo_get_path(repo), 1,
1834 worktree ? got_worktree_get_root_path(worktree) : NULL);
1835 if (error)
1836 goto done;
1838 if (start_commit == NULL) {
1839 struct got_reference *head_ref;
1840 error = got_ref_open(&head_ref, repo,
1841 worktree ? got_worktree_get_head_ref_name(worktree)
1842 : GOT_REF_HEAD, 0);
1843 if (error != NULL)
1844 return error;
1845 error = got_ref_resolve(&id, repo, head_ref);
1846 got_ref_close(head_ref);
1847 if (error != NULL)
1848 return error;
1849 error = got_object_open_as_commit(&commit, repo, id);
1850 } else {
1851 struct got_reference *ref;
1852 error = got_ref_open(&ref, repo, start_commit, 0);
1853 if (error == NULL) {
1854 int obj_type;
1855 error = got_ref_resolve(&id, repo, ref);
1856 got_ref_close(ref);
1857 if (error != NULL)
1858 goto done;
1859 error = got_object_get_type(&obj_type, repo, id);
1860 if (error != NULL)
1861 goto done;
1862 if (obj_type == GOT_OBJ_TYPE_TAG) {
1863 struct got_tag_object *tag;
1864 error = got_object_open_as_tag(&tag, repo, id);
1865 if (error != NULL)
1866 goto done;
1867 if (got_object_tag_get_object_type(tag) !=
1868 GOT_OBJ_TYPE_COMMIT) {
1869 got_object_tag_close(tag);
1870 error = got_error(GOT_ERR_OBJ_TYPE);
1871 goto done;
1873 free(id);
1874 id = got_object_id_dup(
1875 got_object_tag_get_object_id(tag));
1876 if (id == NULL)
1877 error = got_error_from_errno(
1878 "got_object_id_dup");
1879 got_object_tag_close(tag);
1880 if (error)
1881 goto done;
1882 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1883 error = got_error(GOT_ERR_OBJ_TYPE);
1884 goto done;
1886 error = got_object_open_as_commit(&commit, repo, id);
1887 if (error != NULL)
1888 goto done;
1890 if (commit == NULL) {
1891 error = got_repo_match_object_id_prefix(&id,
1892 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1893 if (error != NULL)
1894 return error;
1897 if (error != NULL)
1898 goto done;
1900 if (worktree) {
1901 const char *prefix = got_worktree_get_path_prefix(worktree);
1902 char *p;
1903 if (asprintf(&p, "%s%s%s", prefix,
1904 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1905 error = got_error_from_errno("asprintf");
1906 goto done;
1908 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1909 free(p);
1910 } else
1911 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1912 if (error != NULL)
1913 goto done;
1914 if (in_repo_path) {
1915 free(path);
1916 path = in_repo_path;
1919 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1920 if (error)
1921 goto done;
1923 error = print_commits(id, repo, path, show_patch,
1924 diff_context, limit, first_parent_traversal, &refs);
1925 done:
1926 free(path);
1927 free(repo_path);
1928 free(cwd);
1929 free(id);
1930 if (worktree)
1931 got_worktree_close(worktree);
1932 if (repo) {
1933 const struct got_error *repo_error;
1934 repo_error = got_repo_close(repo);
1935 if (error == NULL)
1936 error = repo_error;
1938 got_ref_list_free(&refs);
1939 return error;
1942 __dead static void
1943 usage_diff(void)
1945 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1946 "[-w] [object1 object2 | path]\n", getprogname());
1947 exit(1);
1950 struct print_diff_arg {
1951 struct got_repository *repo;
1952 struct got_worktree *worktree;
1953 int diff_context;
1954 const char *id_str;
1955 int header_shown;
1956 int diff_staged;
1957 int ignore_whitespace;
1960 static const struct got_error *
1961 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1962 const char *path, struct got_object_id *blob_id,
1963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1965 struct print_diff_arg *a = arg;
1966 const struct got_error *err = NULL;
1967 struct got_blob_object *blob1 = NULL;
1968 FILE *f2 = NULL;
1969 char *abspath = NULL, *label1 = NULL;
1970 struct stat sb;
1972 if (a->diff_staged) {
1973 if (staged_status != GOT_STATUS_MODIFY &&
1974 staged_status != GOT_STATUS_ADD &&
1975 staged_status != GOT_STATUS_DELETE)
1976 return NULL;
1977 } else {
1978 if (staged_status == GOT_STATUS_DELETE)
1979 return NULL;
1980 if (status == GOT_STATUS_NONEXISTENT)
1981 return got_error_set_errno(ENOENT, path);
1982 if (status != GOT_STATUS_MODIFY &&
1983 status != GOT_STATUS_ADD &&
1984 status != GOT_STATUS_DELETE &&
1985 status != GOT_STATUS_CONFLICT)
1986 return NULL;
1989 if (!a->header_shown) {
1990 printf("diff %s %s%s\n", a->id_str,
1991 got_worktree_get_root_path(a->worktree),
1992 a->diff_staged ? " (staged changes)" : "");
1993 a->header_shown = 1;
1996 if (a->diff_staged) {
1997 const char *label1 = NULL, *label2 = NULL;
1998 switch (staged_status) {
1999 case GOT_STATUS_MODIFY:
2000 label1 = path;
2001 label2 = path;
2002 break;
2003 case GOT_STATUS_ADD:
2004 label2 = path;
2005 break;
2006 case GOT_STATUS_DELETE:
2007 label1 = path;
2008 break;
2009 default:
2010 return got_error(GOT_ERR_FILE_STATUS);
2012 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2013 label1, label2, a->diff_context, a->ignore_whitespace,
2014 a->repo, stdout);
2017 if (staged_status == GOT_STATUS_ADD ||
2018 staged_status == GOT_STATUS_MODIFY) {
2019 char *id_str;
2020 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2021 8192);
2022 if (err)
2023 goto done;
2024 err = got_object_id_str(&id_str, staged_blob_id);
2025 if (err)
2026 goto done;
2027 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2028 err = got_error_from_errno("asprintf");
2029 free(id_str);
2030 goto done;
2032 free(id_str);
2033 } else if (status != GOT_STATUS_ADD) {
2034 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2035 if (err)
2036 goto done;
2039 if (status != GOT_STATUS_DELETE) {
2040 if (asprintf(&abspath, "%s/%s",
2041 got_worktree_get_root_path(a->worktree), path) == -1) {
2042 err = got_error_from_errno("asprintf");
2043 goto done;
2046 f2 = fopen(abspath, "r");
2047 if (f2 == NULL) {
2048 err = got_error_from_errno2("fopen", abspath);
2049 goto done;
2051 if (lstat(abspath, &sb) == -1) {
2052 err = got_error_from_errno2("lstat", abspath);
2053 goto done;
2055 } else
2056 sb.st_size = 0;
2058 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2059 a->diff_context, a->ignore_whitespace, stdout);
2060 done:
2061 if (blob1)
2062 got_object_blob_close(blob1);
2063 if (f2 && fclose(f2) != 0 && err == NULL)
2064 err = got_error_from_errno("fclose");
2065 free(abspath);
2066 return err;
2069 static const struct got_error *
2070 match_object_id(struct got_object_id **id, char **label,
2071 const char *id_str, int obj_type, int resolve_tags,
2072 struct got_repository *repo)
2074 const struct got_error *err;
2075 struct got_tag_object *tag;
2076 struct got_reference *ref = NULL;
2078 *id = NULL;
2079 *label = NULL;
2081 if (resolve_tags) {
2082 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2083 repo);
2084 if (err == NULL) {
2085 *id = got_object_id_dup(
2086 got_object_tag_get_object_id(tag));
2087 if (*id == NULL)
2088 err = got_error_from_errno("got_object_id_dup");
2089 else if (asprintf(label, "refs/tags/%s",
2090 got_object_tag_get_name(tag)) == -1) {
2091 err = got_error_from_errno("asprintf");
2092 free(*id);
2093 *id = NULL;
2095 got_object_tag_close(tag);
2096 return err;
2097 } else if (err->code != GOT_ERR_NO_OBJ)
2098 return err;
2101 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2102 if (err) {
2103 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2104 return err;
2105 err = got_ref_open(&ref, repo, id_str, 0);
2106 if (err != NULL)
2107 goto done;
2108 *label = strdup(got_ref_get_name(ref));
2109 if (*label == NULL) {
2110 err = got_error_from_errno("strdup");
2111 goto done;
2113 err = got_ref_resolve(id, repo, ref);
2114 } else {
2115 err = got_object_id_str(label, *id);
2116 if (*label == NULL) {
2117 err = got_error_from_errno("strdup");
2118 goto done;
2121 done:
2122 if (ref)
2123 got_ref_close(ref);
2124 return err;
2128 static const struct got_error *
2129 cmd_diff(int argc, char *argv[])
2131 const struct got_error *error;
2132 struct got_repository *repo = NULL;
2133 struct got_worktree *worktree = NULL;
2134 char *cwd = NULL, *repo_path = NULL;
2135 struct got_object_id *id1 = NULL, *id2 = NULL;
2136 const char *id_str1 = NULL, *id_str2 = NULL;
2137 char *label1 = NULL, *label2 = NULL;
2138 int type1, type2;
2139 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2140 const char *errstr;
2141 char *path = NULL;
2143 #ifndef PROFILE
2144 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2145 NULL) == -1)
2146 err(1, "pledge");
2147 #endif
2149 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2150 switch (ch) {
2151 case 'C':
2152 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2153 if (errstr != NULL)
2154 err(1, "-C option %s", errstr);
2155 break;
2156 case 'r':
2157 repo_path = realpath(optarg, NULL);
2158 if (repo_path == NULL)
2159 err(1, "-r option");
2160 got_path_strip_trailing_slashes(repo_path);
2161 break;
2162 case 's':
2163 diff_staged = 1;
2164 break;
2165 case 'w':
2166 ignore_whitespace = 1;
2167 break;
2168 default:
2169 usage_diff();
2170 /* NOTREACHED */
2174 argc -= optind;
2175 argv += optind;
2177 cwd = getcwd(NULL, 0);
2178 if (cwd == NULL) {
2179 error = got_error_from_errno("getcwd");
2180 goto done;
2182 error = got_worktree_open(&worktree, cwd);
2183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2184 goto done;
2185 if (argc <= 1) {
2186 if (worktree == NULL) {
2187 error = got_error(GOT_ERR_NOT_WORKTREE);
2188 goto done;
2190 if (repo_path)
2191 errx(1,
2192 "-r option can't be used when diffing a work tree");
2193 repo_path = strdup(got_worktree_get_repo_path(worktree));
2194 if (repo_path == NULL) {
2195 error = got_error_from_errno("strdup");
2196 goto done;
2198 if (argc == 1) {
2199 error = got_worktree_resolve_path(&path, worktree,
2200 argv[0]);
2201 if (error)
2202 goto done;
2203 } else {
2204 path = strdup("");
2205 if (path == NULL) {
2206 error = got_error_from_errno("strdup");
2207 goto done;
2210 } else if (argc == 2) {
2211 if (diff_staged)
2212 errx(1, "-s option can't be used when diffing "
2213 "objects in repository");
2214 id_str1 = argv[0];
2215 id_str2 = argv[1];
2216 if (worktree && repo_path == NULL) {
2217 repo_path =
2218 strdup(got_worktree_get_repo_path(worktree));
2219 if (repo_path == NULL) {
2220 error = got_error_from_errno("strdup");
2221 goto done;
2224 } else
2225 usage_diff();
2227 if (repo_path == NULL) {
2228 repo_path = getcwd(NULL, 0);
2229 if (repo_path == NULL)
2230 return got_error_from_errno("getcwd");
2233 error = got_repo_open(&repo, repo_path, NULL);
2234 free(repo_path);
2235 if (error != NULL)
2236 goto done;
2238 error = apply_unveil(got_repo_get_path(repo), 1,
2239 worktree ? got_worktree_get_root_path(worktree) : NULL);
2240 if (error)
2241 goto done;
2243 if (argc <= 1) {
2244 struct print_diff_arg arg;
2245 struct got_pathlist_head paths;
2246 char *id_str;
2248 TAILQ_INIT(&paths);
2250 error = got_object_id_str(&id_str,
2251 got_worktree_get_base_commit_id(worktree));
2252 if (error)
2253 goto done;
2254 arg.repo = repo;
2255 arg.worktree = worktree;
2256 arg.diff_context = diff_context;
2257 arg.id_str = id_str;
2258 arg.header_shown = 0;
2259 arg.diff_staged = diff_staged;
2260 arg.ignore_whitespace = ignore_whitespace;
2262 error = got_pathlist_append(&paths, path, NULL);
2263 if (error)
2264 goto done;
2266 error = got_worktree_status(worktree, &paths, repo, print_diff,
2267 &arg, check_cancelled, NULL);
2268 free(id_str);
2269 got_pathlist_free(&paths);
2270 goto done;
2273 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2274 repo);
2275 if (error)
2276 goto done;
2278 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2279 repo);
2280 if (error)
2281 goto done;
2283 error = got_object_get_type(&type1, repo, id1);
2284 if (error)
2285 goto done;
2287 error = got_object_get_type(&type2, repo, id2);
2288 if (error)
2289 goto done;
2291 if (type1 != type2) {
2292 error = got_error(GOT_ERR_OBJ_TYPE);
2293 goto done;
2296 switch (type1) {
2297 case GOT_OBJ_TYPE_BLOB:
2298 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2299 diff_context, ignore_whitespace, repo, stdout);
2300 break;
2301 case GOT_OBJ_TYPE_TREE:
2302 error = got_diff_objects_as_trees(id1, id2, "", "",
2303 diff_context, ignore_whitespace, repo, stdout);
2304 break;
2305 case GOT_OBJ_TYPE_COMMIT:
2306 printf("diff %s %s\n", label1, label2);
2307 error = got_diff_objects_as_commits(id1, id2, diff_context,
2308 ignore_whitespace, repo, stdout);
2309 break;
2310 default:
2311 error = got_error(GOT_ERR_OBJ_TYPE);
2314 done:
2315 free(label1);
2316 free(label2);
2317 free(id1);
2318 free(id2);
2319 free(path);
2320 if (worktree)
2321 got_worktree_close(worktree);
2322 if (repo) {
2323 const struct got_error *repo_error;
2324 repo_error = got_repo_close(repo);
2325 if (error == NULL)
2326 error = repo_error;
2328 return error;
2331 __dead static void
2332 usage_blame(void)
2334 fprintf(stderr,
2335 "usage: %s blame [-c commit] [-r repository-path] path\n",
2336 getprogname());
2337 exit(1);
2340 struct blame_line {
2341 int annotated;
2342 char *id_str;
2343 char *committer;
2344 char datebuf[9]; /* YY-MM-DD + NUL */
2347 struct blame_cb_args {
2348 struct blame_line *lines;
2349 int nlines;
2350 int nlines_prec;
2351 int lineno_cur;
2352 off_t *line_offsets;
2353 FILE *f;
2354 struct got_repository *repo;
2357 static const struct got_error *
2358 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2360 const struct got_error *err = NULL;
2361 struct blame_cb_args *a = arg;
2362 struct blame_line *bline;
2363 char *line = NULL;
2364 size_t linesize = 0;
2365 struct got_commit_object *commit = NULL;
2366 off_t offset;
2367 struct tm tm;
2368 time_t committer_time;
2370 if (nlines != a->nlines ||
2371 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2372 return got_error(GOT_ERR_RANGE);
2374 if (sigint_received)
2375 return got_error(GOT_ERR_ITER_COMPLETED);
2377 if (lineno == -1)
2378 return NULL; /* no change in this commit */
2380 /* Annotate this line. */
2381 bline = &a->lines[lineno - 1];
2382 if (bline->annotated)
2383 return NULL;
2384 err = got_object_id_str(&bline->id_str, id);
2385 if (err)
2386 return err;
2388 err = got_object_open_as_commit(&commit, a->repo, id);
2389 if (err)
2390 goto done;
2392 bline->committer = strdup(got_object_commit_get_committer(commit));
2393 if (bline->committer == NULL) {
2394 err = got_error_from_errno("strdup");
2395 goto done;
2398 committer_time = got_object_commit_get_committer_time(commit);
2399 if (localtime_r(&committer_time, &tm) == NULL)
2400 return got_error_from_errno("localtime_r");
2401 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2402 &tm) >= sizeof(bline->datebuf)) {
2403 err = got_error(GOT_ERR_NO_SPACE);
2404 goto done;
2406 bline->annotated = 1;
2408 /* Print lines annotated so far. */
2409 bline = &a->lines[a->lineno_cur - 1];
2410 if (!bline->annotated)
2411 goto done;
2413 offset = a->line_offsets[a->lineno_cur - 1];
2414 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2415 err = got_error_from_errno("fseeko");
2416 goto done;
2419 while (bline->annotated) {
2420 char *smallerthan, *at, *nl, *committer;
2421 size_t len;
2423 if (getline(&line, &linesize, a->f) == -1) {
2424 if (ferror(a->f))
2425 err = got_error_from_errno("getline");
2426 break;
2429 committer = bline->committer;
2430 smallerthan = strchr(committer, '<');
2431 if (smallerthan && smallerthan[1] != '\0')
2432 committer = smallerthan + 1;
2433 at = strchr(committer, '@');
2434 if (at)
2435 *at = '\0';
2436 len = strlen(committer);
2437 if (len >= 9)
2438 committer[8] = '\0';
2440 nl = strchr(line, '\n');
2441 if (nl)
2442 *nl = '\0';
2443 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2444 bline->id_str, bline->datebuf, committer, line);
2446 a->lineno_cur++;
2447 bline = &a->lines[a->lineno_cur - 1];
2449 done:
2450 if (commit)
2451 got_object_commit_close(commit);
2452 free(line);
2453 return err;
2456 static const struct got_error *
2457 cmd_blame(int argc, char *argv[])
2459 const struct got_error *error;
2460 struct got_repository *repo = NULL;
2461 struct got_worktree *worktree = NULL;
2462 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2463 struct got_object_id *obj_id = NULL;
2464 struct got_object_id *commit_id = NULL;
2465 struct got_blob_object *blob = NULL;
2466 char *commit_id_str = NULL;
2467 struct blame_cb_args bca;
2468 int ch, obj_type, i;
2469 size_t filesize;
2471 memset(&bca, 0, sizeof(bca));
2473 #ifndef PROFILE
2474 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2475 NULL) == -1)
2476 err(1, "pledge");
2477 #endif
2479 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2480 switch (ch) {
2481 case 'c':
2482 commit_id_str = optarg;
2483 break;
2484 case 'r':
2485 repo_path = realpath(optarg, NULL);
2486 if (repo_path == NULL)
2487 err(1, "-r option");
2488 got_path_strip_trailing_slashes(repo_path);
2489 break;
2490 default:
2491 usage_blame();
2492 /* NOTREACHED */
2496 argc -= optind;
2497 argv += optind;
2499 if (argc == 1)
2500 path = argv[0];
2501 else
2502 usage_blame();
2504 cwd = getcwd(NULL, 0);
2505 if (cwd == NULL) {
2506 error = got_error_from_errno("getcwd");
2507 goto done;
2509 if (repo_path == NULL) {
2510 error = got_worktree_open(&worktree, cwd);
2511 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2512 goto done;
2513 else
2514 error = NULL;
2515 if (worktree) {
2516 repo_path =
2517 strdup(got_worktree_get_repo_path(worktree));
2518 if (repo_path == NULL)
2519 error = got_error_from_errno("strdup");
2520 if (error)
2521 goto done;
2522 } else {
2523 repo_path = strdup(cwd);
2524 if (repo_path == NULL) {
2525 error = got_error_from_errno("strdup");
2526 goto done;
2531 error = got_repo_open(&repo, repo_path, NULL);
2532 if (error != NULL)
2533 goto done;
2535 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2536 if (error)
2537 goto done;
2539 if (worktree) {
2540 const char *prefix = got_worktree_get_path_prefix(worktree);
2541 char *p, *worktree_subdir = cwd +
2542 strlen(got_worktree_get_root_path(worktree));
2543 if (asprintf(&p, "%s%s%s%s%s",
2544 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2545 worktree_subdir, worktree_subdir[0] ? "/" : "",
2546 path) == -1) {
2547 error = got_error_from_errno("asprintf");
2548 goto done;
2550 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2551 free(p);
2552 } else {
2553 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2555 if (error)
2556 goto done;
2558 if (commit_id_str == NULL) {
2559 struct got_reference *head_ref;
2560 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2561 if (error != NULL)
2562 goto done;
2563 error = got_ref_resolve(&commit_id, repo, head_ref);
2564 got_ref_close(head_ref);
2565 if (error != NULL)
2566 goto done;
2567 } else {
2568 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2569 if (error)
2570 goto done;
2573 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2574 if (error)
2575 goto done;
2576 if (obj_id == NULL) {
2577 error = got_error(GOT_ERR_NO_OBJ);
2578 goto done;
2581 error = got_object_get_type(&obj_type, repo, obj_id);
2582 if (error)
2583 goto done;
2585 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2586 error = got_error(GOT_ERR_OBJ_TYPE);
2587 goto done;
2590 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2591 if (error)
2592 goto done;
2593 bca.f = got_opentemp();
2594 if (bca.f == NULL) {
2595 error = got_error_from_errno("got_opentemp");
2596 goto done;
2598 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2599 &bca.line_offsets, bca.f, blob);
2600 if (error || bca.nlines == 0)
2601 goto done;
2603 /* Don't include \n at EOF in the blame line count. */
2604 if (bca.line_offsets[bca.nlines - 1] == filesize)
2605 bca.nlines--;
2607 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2608 if (bca.lines == NULL) {
2609 error = got_error_from_errno("calloc");
2610 goto done;
2612 bca.lineno_cur = 1;
2613 bca.nlines_prec = 0;
2614 i = bca.nlines;
2615 while (i > 0) {
2616 i /= 10;
2617 bca.nlines_prec++;
2619 bca.repo = repo;
2621 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2622 check_cancelled, NULL);
2623 if (error)
2624 goto done;
2625 done:
2626 free(in_repo_path);
2627 free(repo_path);
2628 free(cwd);
2629 free(commit_id);
2630 free(obj_id);
2631 if (blob)
2632 got_object_blob_close(blob);
2633 if (worktree)
2634 got_worktree_close(worktree);
2635 if (repo) {
2636 const struct got_error *repo_error;
2637 repo_error = got_repo_close(repo);
2638 if (error == NULL)
2639 error = repo_error;
2641 if (bca.lines) {
2642 for (i = 0; i < bca.nlines; i++) {
2643 struct blame_line *bline = &bca.lines[i];
2644 free(bline->id_str);
2645 free(bline->committer);
2647 free(bca.lines);
2649 free(bca.line_offsets);
2650 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2651 error = got_error_from_errno("fclose");
2652 return error;
2655 __dead static void
2656 usage_tree(void)
2658 fprintf(stderr,
2659 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2660 getprogname());
2661 exit(1);
2664 static void
2665 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2666 const char *root_path)
2668 int is_root_path = (strcmp(path, root_path) == 0);
2669 const char *modestr = "";
2671 path += strlen(root_path);
2672 while (path[0] == '/')
2673 path++;
2675 if (got_object_tree_entry_is_submodule(te))
2676 modestr = "$";
2677 else if (S_ISLNK(te->mode))
2678 modestr = "@";
2679 else if (S_ISDIR(te->mode))
2680 modestr = "/";
2681 else if (te->mode & S_IXUSR)
2682 modestr = "*";
2684 printf("%s%s%s%s%s\n", id ? id : "", path,
2685 is_root_path ? "" : "/", te->name, modestr);
2688 static const struct got_error *
2689 print_tree(const char *path, struct got_object_id *commit_id,
2690 int show_ids, int recurse, const char *root_path,
2691 struct got_repository *repo)
2693 const struct got_error *err = NULL;
2694 struct got_object_id *tree_id = NULL;
2695 struct got_tree_object *tree = NULL;
2696 const struct got_tree_entries *entries;
2697 struct got_tree_entry *te;
2699 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2700 if (err)
2701 goto done;
2703 err = got_object_open_as_tree(&tree, repo, tree_id);
2704 if (err)
2705 goto done;
2706 entries = got_object_tree_get_entries(tree);
2707 te = SIMPLEQ_FIRST(&entries->head);
2708 while (te) {
2709 char *id = NULL;
2711 if (sigint_received || sigpipe_received)
2712 break;
2714 if (show_ids) {
2715 char *id_str;
2716 err = got_object_id_str(&id_str, te->id);
2717 if (err)
2718 goto done;
2719 if (asprintf(&id, "%s ", id_str) == -1) {
2720 err = got_error_from_errno("asprintf");
2721 free(id_str);
2722 goto done;
2724 free(id_str);
2726 print_entry(te, id, path, root_path);
2727 free(id);
2729 if (recurse && S_ISDIR(te->mode)) {
2730 char *child_path;
2731 if (asprintf(&child_path, "%s%s%s", path,
2732 path[0] == '/' && path[1] == '\0' ? "" : "/",
2733 te->name) == -1) {
2734 err = got_error_from_errno("asprintf");
2735 goto done;
2737 err = print_tree(child_path, commit_id, show_ids, 1,
2738 root_path, repo);
2739 free(child_path);
2740 if (err)
2741 goto done;
2744 te = SIMPLEQ_NEXT(te, entry);
2746 done:
2747 if (tree)
2748 got_object_tree_close(tree);
2749 free(tree_id);
2750 return err;
2753 static const struct got_error *
2754 cmd_tree(int argc, char *argv[])
2756 const struct got_error *error;
2757 struct got_repository *repo = NULL;
2758 struct got_worktree *worktree = NULL;
2759 const char *path;
2760 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2761 struct got_object_id *commit_id = NULL;
2762 char *commit_id_str = NULL;
2763 int show_ids = 0, recurse = 0;
2764 int ch;
2766 #ifndef PROFILE
2767 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2768 NULL) == -1)
2769 err(1, "pledge");
2770 #endif
2772 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2773 switch (ch) {
2774 case 'c':
2775 commit_id_str = optarg;
2776 break;
2777 case 'r':
2778 repo_path = realpath(optarg, NULL);
2779 if (repo_path == NULL)
2780 err(1, "-r option");
2781 got_path_strip_trailing_slashes(repo_path);
2782 break;
2783 case 'i':
2784 show_ids = 1;
2785 break;
2786 case 'R':
2787 recurse = 1;
2788 break;
2789 default:
2790 usage_tree();
2791 /* NOTREACHED */
2795 argc -= optind;
2796 argv += optind;
2798 if (argc == 1)
2799 path = argv[0];
2800 else if (argc > 1)
2801 usage_tree();
2802 else
2803 path = NULL;
2805 cwd = getcwd(NULL, 0);
2806 if (cwd == NULL) {
2807 error = got_error_from_errno("getcwd");
2808 goto done;
2810 if (repo_path == NULL) {
2811 error = got_worktree_open(&worktree, cwd);
2812 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2813 goto done;
2814 else
2815 error = NULL;
2816 if (worktree) {
2817 repo_path =
2818 strdup(got_worktree_get_repo_path(worktree));
2819 if (repo_path == NULL)
2820 error = got_error_from_errno("strdup");
2821 if (error)
2822 goto done;
2823 } else {
2824 repo_path = strdup(cwd);
2825 if (repo_path == NULL) {
2826 error = got_error_from_errno("strdup");
2827 goto done;
2832 error = got_repo_open(&repo, repo_path, NULL);
2833 if (error != NULL)
2834 goto done;
2836 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2837 if (error)
2838 goto done;
2840 if (path == NULL) {
2841 if (worktree) {
2842 char *p, *worktree_subdir = cwd +
2843 strlen(got_worktree_get_root_path(worktree));
2844 if (asprintf(&p, "%s/%s",
2845 got_worktree_get_path_prefix(worktree),
2846 worktree_subdir) == -1) {
2847 error = got_error_from_errno("asprintf");
2848 goto done;
2850 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2851 free(p);
2852 if (error)
2853 goto done;
2854 } else
2855 path = "/";
2857 if (in_repo_path == NULL) {
2858 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2859 if (error != NULL)
2860 goto done;
2863 if (commit_id_str == NULL) {
2864 struct got_reference *head_ref;
2865 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2866 if (error != NULL)
2867 goto done;
2868 error = got_ref_resolve(&commit_id, repo, head_ref);
2869 got_ref_close(head_ref);
2870 if (error != NULL)
2871 goto done;
2872 } else {
2873 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2874 if (error)
2875 goto done;
2878 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2879 in_repo_path, repo);
2880 done:
2881 free(in_repo_path);
2882 free(repo_path);
2883 free(cwd);
2884 free(commit_id);
2885 if (worktree)
2886 got_worktree_close(worktree);
2887 if (repo) {
2888 const struct got_error *repo_error;
2889 repo_error = got_repo_close(repo);
2890 if (error == NULL)
2891 error = repo_error;
2893 return error;
2896 __dead static void
2897 usage_status(void)
2899 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2900 exit(1);
2903 static const struct got_error *
2904 print_status(void *arg, unsigned char status, unsigned char staged_status,
2905 const char *path, struct got_object_id *blob_id,
2906 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2908 if (status == staged_status && (status == GOT_STATUS_DELETE))
2909 status = GOT_STATUS_NO_CHANGE;
2910 printf("%c%c %s\n", status, staged_status, path);
2911 return NULL;
2914 static const struct got_error *
2915 cmd_status(int argc, char *argv[])
2917 const struct got_error *error = NULL;
2918 struct got_repository *repo = NULL;
2919 struct got_worktree *worktree = NULL;
2920 char *cwd = NULL;
2921 struct got_pathlist_head paths;
2922 struct got_pathlist_entry *pe;
2923 int ch;
2925 TAILQ_INIT(&paths);
2927 while ((ch = getopt(argc, argv, "")) != -1) {
2928 switch (ch) {
2929 default:
2930 usage_status();
2931 /* NOTREACHED */
2935 argc -= optind;
2936 argv += optind;
2938 #ifndef PROFILE
2939 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2940 NULL) == -1)
2941 err(1, "pledge");
2942 #endif
2943 cwd = getcwd(NULL, 0);
2944 if (cwd == NULL) {
2945 error = got_error_from_errno("getcwd");
2946 goto done;
2949 error = got_worktree_open(&worktree, cwd);
2950 if (error != NULL)
2951 goto done;
2953 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2954 NULL);
2955 if (error != NULL)
2956 goto done;
2958 error = apply_unveil(got_repo_get_path(repo), 1,
2959 got_worktree_get_root_path(worktree));
2960 if (error)
2961 goto done;
2963 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2964 if (error)
2965 goto done;
2967 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2968 check_cancelled, NULL);
2969 done:
2970 TAILQ_FOREACH(pe, &paths, entry)
2971 free((char *)pe->path);
2972 got_pathlist_free(&paths);
2973 free(cwd);
2974 return error;
2977 __dead static void
2978 usage_ref(void)
2980 fprintf(stderr,
2981 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2982 getprogname());
2983 exit(1);
2986 static const struct got_error *
2987 list_refs(struct got_repository *repo)
2989 static const struct got_error *err = NULL;
2990 struct got_reflist_head refs;
2991 struct got_reflist_entry *re;
2993 SIMPLEQ_INIT(&refs);
2994 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2995 if (err)
2996 return err;
2998 SIMPLEQ_FOREACH(re, &refs, entry) {
2999 char *refstr;
3000 refstr = got_ref_to_str(re->ref);
3001 if (refstr == NULL)
3002 return got_error_from_errno("got_ref_to_str");
3003 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3004 free(refstr);
3007 got_ref_list_free(&refs);
3008 return NULL;
3011 static const struct got_error *
3012 delete_ref(struct got_repository *repo, const char *refname)
3014 const struct got_error *err = NULL;
3015 struct got_reference *ref;
3017 err = got_ref_open(&ref, repo, refname, 0);
3018 if (err)
3019 return err;
3021 err = got_ref_delete(ref, repo);
3022 got_ref_close(ref);
3023 return err;
3026 static const struct got_error *
3027 add_ref(struct got_repository *repo, const char *refname, const char *target)
3029 const struct got_error *err = NULL;
3030 struct got_object_id *id;
3031 struct got_reference *ref = NULL;
3034 * Don't let the user create a reference named '-'.
3035 * While technically a valid reference name, this case is usually
3036 * an unintended typo.
3038 if (refname[0] == '-' && refname[1] == '\0')
3039 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3041 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3042 repo);
3043 if (err) {
3044 struct got_reference *target_ref;
3046 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3047 return err;
3048 err = got_ref_open(&target_ref, repo, target, 0);
3049 if (err)
3050 return err;
3051 err = got_ref_resolve(&id, repo, target_ref);
3052 got_ref_close(target_ref);
3053 if (err)
3054 return err;
3057 err = got_ref_alloc(&ref, refname, id);
3058 if (err)
3059 goto done;
3061 err = got_ref_write(ref, repo);
3062 done:
3063 if (ref)
3064 got_ref_close(ref);
3065 free(id);
3066 return err;
3069 static const struct got_error *
3070 add_symref(struct got_repository *repo, const char *refname, const char *target)
3072 const struct got_error *err = NULL;
3073 struct got_reference *ref = NULL;
3074 struct got_reference *target_ref = NULL;
3077 * Don't let the user create a reference named '-'.
3078 * While technically a valid reference name, this case is usually
3079 * an unintended typo.
3081 if (refname[0] == '-' && refname[1] == '\0')
3082 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3084 err = got_ref_open(&target_ref, repo, target, 0);
3085 if (err)
3086 return err;
3088 err = got_ref_alloc_symref(&ref, refname, target_ref);
3089 if (err)
3090 goto done;
3092 err = got_ref_write(ref, repo);
3093 done:
3094 if (target_ref)
3095 got_ref_close(target_ref);
3096 if (ref)
3097 got_ref_close(ref);
3098 return err;
3101 static const struct got_error *
3102 cmd_ref(int argc, char *argv[])
3104 const struct got_error *error = NULL;
3105 struct got_repository *repo = NULL;
3106 struct got_worktree *worktree = NULL;
3107 char *cwd = NULL, *repo_path = NULL;
3108 int ch, do_list = 0, create_symref = 0;
3109 const char *delref = NULL;
3111 /* TODO: Add -s option for adding symbolic references. */
3112 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3113 switch (ch) {
3114 case 'd':
3115 delref = optarg;
3116 break;
3117 case 'r':
3118 repo_path = realpath(optarg, NULL);
3119 if (repo_path == NULL)
3120 err(1, "-r option");
3121 got_path_strip_trailing_slashes(repo_path);
3122 break;
3123 case 'l':
3124 do_list = 1;
3125 break;
3126 case 's':
3127 create_symref = 1;
3128 break;
3129 default:
3130 usage_ref();
3131 /* NOTREACHED */
3135 if (do_list && delref)
3136 errx(1, "-l and -d options are mutually exclusive\n");
3138 argc -= optind;
3139 argv += optind;
3141 if (do_list || delref) {
3142 if (create_symref)
3143 errx(1, "-s option cannot be used together with the "
3144 "-l or -d options");
3145 if (argc > 0)
3146 usage_ref();
3147 } else if (argc != 2)
3148 usage_ref();
3150 #ifndef PROFILE
3151 if (do_list) {
3152 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3153 NULL) == -1)
3154 err(1, "pledge");
3155 } else {
3156 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3157 "sendfd unveil", NULL) == -1)
3158 err(1, "pledge");
3160 #endif
3161 cwd = getcwd(NULL, 0);
3162 if (cwd == NULL) {
3163 error = got_error_from_errno("getcwd");
3164 goto done;
3167 if (repo_path == NULL) {
3168 error = got_worktree_open(&worktree, cwd);
3169 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3170 goto done;
3171 else
3172 error = NULL;
3173 if (worktree) {
3174 repo_path =
3175 strdup(got_worktree_get_repo_path(worktree));
3176 if (repo_path == NULL)
3177 error = got_error_from_errno("strdup");
3178 if (error)
3179 goto done;
3180 } else {
3181 repo_path = strdup(cwd);
3182 if (repo_path == NULL) {
3183 error = got_error_from_errno("strdup");
3184 goto done;
3189 error = got_repo_open(&repo, repo_path, NULL);
3190 if (error != NULL)
3191 goto done;
3193 error = apply_unveil(got_repo_get_path(repo), do_list,
3194 worktree ? got_worktree_get_root_path(worktree) : NULL);
3195 if (error)
3196 goto done;
3198 if (do_list)
3199 error = list_refs(repo);
3200 else if (delref)
3201 error = delete_ref(repo, delref);
3202 else if (create_symref)
3203 error = add_symref(repo, argv[0], argv[1]);
3204 else
3205 error = add_ref(repo, argv[0], argv[1]);
3206 done:
3207 if (repo)
3208 got_repo_close(repo);
3209 if (worktree)
3210 got_worktree_close(worktree);
3211 free(cwd);
3212 free(repo_path);
3213 return error;
3216 __dead static void
3217 usage_branch(void)
3219 fprintf(stderr,
3220 "usage: %s branch [-r repository] [-l] | -d name | "
3221 "[name [commit]]\n", getprogname());
3222 exit(1);
3225 static const struct got_error *
3226 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3227 struct got_reference *ref)
3229 const struct got_error *err = NULL;
3230 const char *refname, *marker = " ";
3231 char *refstr;
3233 refname = got_ref_get_name(ref);
3234 if (worktree && strcmp(refname,
3235 got_worktree_get_head_ref_name(worktree)) == 0) {
3236 struct got_object_id *id = NULL;
3238 err = got_ref_resolve(&id, repo, ref);
3239 if (err)
3240 return err;
3241 if (got_object_id_cmp(id,
3242 got_worktree_get_base_commit_id(worktree)) == 0)
3243 marker = "* ";
3244 else
3245 marker = "~ ";
3246 free(id);
3249 if (strncmp(refname, "refs/heads/", 11) == 0)
3250 refname += 11;
3251 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3252 refname += 18;
3254 refstr = got_ref_to_str(ref);
3255 if (refstr == NULL)
3256 return got_error_from_errno("got_ref_to_str");
3258 printf("%s%s: %s\n", marker, refname, refstr);
3259 free(refstr);
3260 return NULL;
3263 static const struct got_error *
3264 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3266 const char *refname;
3268 if (worktree == NULL)
3269 return got_error(GOT_ERR_NOT_WORKTREE);
3271 refname = got_worktree_get_head_ref_name(worktree);
3273 if (strncmp(refname, "refs/heads/", 11) == 0)
3274 refname += 11;
3275 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3276 refname += 18;
3278 printf("%s\n", refname);
3280 return NULL;
3283 static const struct got_error *
3284 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3286 static const struct got_error *err = NULL;
3287 struct got_reflist_head refs;
3288 struct got_reflist_entry *re;
3289 struct got_reference *temp_ref = NULL;
3290 int rebase_in_progress, histedit_in_progress;
3292 SIMPLEQ_INIT(&refs);
3294 if (worktree) {
3295 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3296 worktree);
3297 if (err)
3298 return err;
3300 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3301 worktree);
3302 if (err)
3303 return err;
3305 if (rebase_in_progress || histedit_in_progress) {
3306 err = got_ref_open(&temp_ref, repo,
3307 got_worktree_get_head_ref_name(worktree), 0);
3308 if (err)
3309 return err;
3310 list_branch(repo, worktree, temp_ref);
3311 got_ref_close(temp_ref);
3315 err = got_ref_list(&refs, repo, "refs/heads",
3316 got_ref_cmp_by_name, NULL);
3317 if (err)
3318 return err;
3320 SIMPLEQ_FOREACH(re, &refs, entry)
3321 list_branch(repo, worktree, re->ref);
3323 got_ref_list_free(&refs);
3324 return NULL;
3327 static const struct got_error *
3328 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3329 const char *branch_name)
3331 const struct got_error *err = NULL;
3332 struct got_reference *ref = NULL;
3333 char *refname;
3335 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3336 return got_error_from_errno("asprintf");
3338 err = got_ref_open(&ref, repo, refname, 0);
3339 if (err)
3340 goto done;
3342 if (worktree &&
3343 strcmp(got_worktree_get_head_ref_name(worktree),
3344 got_ref_get_name(ref)) == 0) {
3345 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3346 "will not delete this work tree's current branch");
3347 goto done;
3350 err = got_ref_delete(ref, repo);
3351 done:
3352 if (ref)
3353 got_ref_close(ref);
3354 free(refname);
3355 return err;
3358 static const struct got_error *
3359 add_branch(struct got_repository *repo, const char *branch_name,
3360 const char *base_branch)
3362 const struct got_error *err = NULL;
3363 struct got_object_id *id = NULL;
3364 char *label;
3365 struct got_reference *ref = NULL;
3366 char *base_refname = NULL, *refname = NULL;
3369 * Don't let the user create a branch named '-'.
3370 * While technically a valid reference name, this case is usually
3371 * an unintended typo.
3373 if (branch_name[0] == '-' && branch_name[1] == '\0')
3374 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3376 err = match_object_id(&id, &label, base_branch,
3377 GOT_OBJ_TYPE_COMMIT, 1, repo);
3378 if (err)
3379 return err;
3381 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3382 err = got_error_from_errno("asprintf");
3383 goto done;
3386 err = got_ref_open(&ref, repo, refname, 0);
3387 if (err == NULL) {
3388 err = got_error(GOT_ERR_BRANCH_EXISTS);
3389 goto done;
3390 } else if (err->code != GOT_ERR_NOT_REF)
3391 goto done;
3393 err = got_ref_alloc(&ref, refname, id);
3394 if (err)
3395 goto done;
3397 err = got_ref_write(ref, repo);
3398 done:
3399 if (ref)
3400 got_ref_close(ref);
3401 free(id);
3402 free(base_refname);
3403 free(refname);
3404 return err;
3407 static const struct got_error *
3408 cmd_branch(int argc, char *argv[])
3410 const struct got_error *error = NULL;
3411 struct got_repository *repo = NULL;
3412 struct got_worktree *worktree = NULL;
3413 char *cwd = NULL, *repo_path = NULL;
3414 int ch, do_list = 0, do_show = 0;
3415 const char *delref = NULL;
3417 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3418 switch (ch) {
3419 case 'd':
3420 delref = optarg;
3421 break;
3422 case 'r':
3423 repo_path = realpath(optarg, NULL);
3424 if (repo_path == NULL)
3425 err(1, "-r option");
3426 got_path_strip_trailing_slashes(repo_path);
3427 break;
3428 case 'l':
3429 do_list = 1;
3430 break;
3431 default:
3432 usage_branch();
3433 /* NOTREACHED */
3437 if (do_list && delref)
3438 errx(1, "-l and -d options are mutually exclusive\n");
3440 argc -= optind;
3441 argv += optind;
3443 if (!do_list && !delref && argc == 0)
3444 do_show = 1;
3446 if (do_list || delref) {
3447 if (argc > 0)
3448 usage_branch();
3449 } else if (!do_show && (argc < 1 || argc > 2))
3450 usage_branch();
3452 #ifndef PROFILE
3453 if (do_list || do_show) {
3454 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3455 NULL) == -1)
3456 err(1, "pledge");
3457 } else {
3458 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3459 "sendfd unveil", NULL) == -1)
3460 err(1, "pledge");
3462 #endif
3463 cwd = getcwd(NULL, 0);
3464 if (cwd == NULL) {
3465 error = got_error_from_errno("getcwd");
3466 goto done;
3469 if (repo_path == NULL) {
3470 error = got_worktree_open(&worktree, cwd);
3471 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3472 goto done;
3473 else
3474 error = NULL;
3475 if (worktree) {
3476 repo_path =
3477 strdup(got_worktree_get_repo_path(worktree));
3478 if (repo_path == NULL)
3479 error = got_error_from_errno("strdup");
3480 if (error)
3481 goto done;
3482 } else {
3483 repo_path = strdup(cwd);
3484 if (repo_path == NULL) {
3485 error = got_error_from_errno("strdup");
3486 goto done;
3491 error = got_repo_open(&repo, repo_path, NULL);
3492 if (error != NULL)
3493 goto done;
3495 error = apply_unveil(got_repo_get_path(repo), do_list,
3496 worktree ? got_worktree_get_root_path(worktree) : NULL);
3497 if (error)
3498 goto done;
3500 if (do_show)
3501 error = show_current_branch(repo, worktree);
3502 else if (do_list)
3503 error = list_branches(repo, worktree);
3504 else if (delref)
3505 error = delete_branch(repo, worktree, delref);
3506 else {
3507 const char *base_branch;
3508 if (argc == 1) {
3509 base_branch = worktree ?
3510 got_worktree_get_head_ref_name(worktree) :
3511 GOT_REF_HEAD;
3512 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3513 base_branch += 11;
3514 } else
3515 base_branch = argv[1];
3516 error = add_branch(repo, argv[0], base_branch);
3518 done:
3519 if (repo)
3520 got_repo_close(repo);
3521 if (worktree)
3522 got_worktree_close(worktree);
3523 free(cwd);
3524 free(repo_path);
3525 return error;
3529 __dead static void
3530 usage_tag(void)
3532 fprintf(stderr,
3533 "usage: %s tag [-r repository] | -l | "
3534 "[-m message] name [commit]\n", getprogname());
3535 exit(1);
3538 #if 0
3539 static const struct got_error *
3540 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3542 const struct got_error *err = NULL;
3543 struct got_reflist_entry *re, *se, *new;
3544 struct got_object_id *re_id, *se_id;
3545 struct got_tag_object *re_tag, *se_tag;
3546 time_t re_time, se_time;
3548 SIMPLEQ_FOREACH(re, tags, entry) {
3549 se = SIMPLEQ_FIRST(sorted);
3550 if (se == NULL) {
3551 err = got_reflist_entry_dup(&new, re);
3552 if (err)
3553 return err;
3554 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3555 continue;
3556 } else {
3557 err = got_ref_resolve(&re_id, repo, re->ref);
3558 if (err)
3559 break;
3560 err = got_object_open_as_tag(&re_tag, repo, re_id);
3561 free(re_id);
3562 if (err)
3563 break;
3564 re_time = got_object_tag_get_tagger_time(re_tag);
3565 got_object_tag_close(re_tag);
3568 while (se) {
3569 err = got_ref_resolve(&se_id, repo, re->ref);
3570 if (err)
3571 break;
3572 err = got_object_open_as_tag(&se_tag, repo, se_id);
3573 free(se_id);
3574 if (err)
3575 break;
3576 se_time = got_object_tag_get_tagger_time(se_tag);
3577 got_object_tag_close(se_tag);
3579 if (se_time > re_time) {
3580 err = got_reflist_entry_dup(&new, re);
3581 if (err)
3582 return err;
3583 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3584 break;
3586 se = SIMPLEQ_NEXT(se, entry);
3587 continue;
3590 done:
3591 return err;
3593 #endif
3595 static const struct got_error *
3596 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3597 struct got_reference *ref2)
3599 const struct got_error *err = NULL;
3600 struct got_repository *repo = arg;
3601 struct got_object_id *id1, *id2 = NULL;
3602 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3603 time_t time1, time2;
3605 *cmp = 0;
3607 err = got_ref_resolve(&id1, repo, ref1);
3608 if (err)
3609 return err;
3610 err = got_object_open_as_tag(&tag1, repo, id1);
3611 if (err)
3612 goto done;
3614 err = got_ref_resolve(&id2, repo, ref2);
3615 if (err)
3616 goto done;
3617 err = got_object_open_as_tag(&tag2, repo, id2);
3618 if (err)
3619 goto done;
3621 time1 = got_object_tag_get_tagger_time(tag1);
3622 time2 = got_object_tag_get_tagger_time(tag2);
3624 /* Put latest tags first. */
3625 if (time1 < time2)
3626 *cmp = 1;
3627 else if (time1 > time2)
3628 *cmp = -1;
3629 else
3630 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3631 done:
3632 free(id1);
3633 free(id2);
3634 if (tag1)
3635 got_object_tag_close(tag1);
3636 if (tag2)
3637 got_object_tag_close(tag2);
3638 return err;
3641 static const struct got_error *
3642 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3644 static const struct got_error *err = NULL;
3645 struct got_reflist_head refs;
3646 struct got_reflist_entry *re;
3648 SIMPLEQ_INIT(&refs);
3650 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3651 if (err)
3652 return err;
3654 SIMPLEQ_FOREACH(re, &refs, entry) {
3655 const char *refname;
3656 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3657 char datebuf[26];
3658 time_t tagger_time;
3659 struct got_object_id *id;
3660 struct got_tag_object *tag;
3662 refname = got_ref_get_name(re->ref);
3663 if (strncmp(refname, "refs/tags/", 10) != 0)
3664 continue;
3665 refname += 10;
3666 refstr = got_ref_to_str(re->ref);
3667 if (refstr == NULL) {
3668 err = got_error_from_errno("got_ref_to_str");
3669 break;
3671 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3672 free(refstr);
3674 err = got_ref_resolve(&id, repo, re->ref);
3675 if (err)
3676 break;
3677 err = got_object_open_as_tag(&tag, repo, id);
3678 free(id);
3679 if (err)
3680 break;
3681 printf("from: %s\n", got_object_tag_get_tagger(tag));
3682 tagger_time = got_object_tag_get_tagger_time(tag);
3683 datestr = get_datestr(&tagger_time, datebuf);
3684 if (datestr)
3685 printf("date: %s UTC\n", datestr);
3686 err = got_object_id_str(&id_str,
3687 got_object_tag_get_object_id(tag));
3688 if (err)
3689 break;
3690 switch (got_object_tag_get_object_type(tag)) {
3691 case GOT_OBJ_TYPE_BLOB:
3692 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3693 break;
3694 case GOT_OBJ_TYPE_TREE:
3695 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3696 break;
3697 case GOT_OBJ_TYPE_COMMIT:
3698 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3699 break;
3700 case GOT_OBJ_TYPE_TAG:
3701 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3702 break;
3703 default:
3704 break;
3706 free(id_str);
3707 tagmsg0 = strdup(got_object_tag_get_message(tag));
3708 got_object_tag_close(tag);
3709 if (tagmsg0 == NULL) {
3710 err = got_error_from_errno("strdup");
3711 break;
3714 tagmsg = tagmsg0;
3715 do {
3716 line = strsep(&tagmsg, "\n");
3717 if (line)
3718 printf(" %s\n", line);
3719 } while (line);
3720 free(tagmsg0);
3723 got_ref_list_free(&refs);
3724 return NULL;
3727 static const struct got_error *
3728 get_tag_message(char **tagmsg, const char *commit_id_str,
3729 const char *tag_name, const char *repo_path)
3731 const struct got_error *err = NULL;
3732 char *template = NULL, *initial_content = NULL;
3733 char *tagmsg_path = NULL, *editor = NULL;
3734 int fd = -1;
3736 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3737 err = got_error_from_errno("asprintf");
3738 goto done;
3741 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3742 commit_id_str, tag_name) == -1) {
3743 err = got_error_from_errno("asprintf");
3744 goto done;
3747 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3748 if (err)
3749 goto done;
3751 dprintf(fd, initial_content);
3752 close(fd);
3754 err = get_editor(&editor);
3755 if (err)
3756 goto done;
3757 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3758 done:
3759 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3760 unlink(tagmsg_path);
3761 free(tagmsg_path);
3762 tagmsg_path = NULL;
3764 free(initial_content);
3765 free(template);
3766 free(editor);
3768 /* Editor is done; we can now apply unveil(2) */
3769 if (err == NULL) {
3770 err = apply_unveil(repo_path, 0, NULL);
3771 if (err) {
3772 free(*tagmsg);
3773 *tagmsg = NULL;
3776 return err;
3779 static const struct got_error *
3780 add_tag(struct got_repository *repo, const char *tag_name,
3781 const char *commit_arg, const char *tagmsg_arg)
3783 const struct got_error *err = NULL;
3784 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3785 char *label = NULL, *commit_id_str = NULL;
3786 struct got_reference *ref = NULL;
3787 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3790 * Don't let the user create a tag named '-'.
3791 * While technically a valid reference name, this case is usually
3792 * an unintended typo.
3794 if (tag_name[0] == '-' && tag_name[1] == '\0')
3795 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3797 err = get_author(&tagger, repo);
3798 if (err)
3799 return err;
3801 err = match_object_id(&commit_id, &label, commit_arg,
3802 GOT_OBJ_TYPE_COMMIT, 1, repo);
3803 if (err)
3804 goto done;
3806 err = got_object_id_str(&commit_id_str, commit_id);
3807 if (err)
3808 goto done;
3810 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3811 refname = strdup(tag_name);
3812 if (refname == NULL) {
3813 err = got_error_from_errno("strdup");
3814 goto done;
3816 tag_name += 10;
3817 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3818 err = got_error_from_errno("asprintf");
3819 goto done;
3822 err = got_ref_open(&ref, repo, refname, 0);
3823 if (err == NULL) {
3824 err = got_error(GOT_ERR_TAG_EXISTS);
3825 goto done;
3826 } else if (err->code != GOT_ERR_NOT_REF)
3827 goto done;
3829 if (tagmsg_arg == NULL) {
3830 err = get_tag_message(&tagmsg, commit_id_str,
3831 tag_name, got_repo_get_path(repo));
3832 if (err)
3833 goto done;
3836 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3837 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3838 if (err)
3839 goto done;
3841 err = got_ref_alloc(&ref, refname, tag_id);
3842 if (err)
3843 goto done;
3845 err = got_ref_write(ref, repo);
3847 if (err == NULL) {
3848 char *tag_id_str;
3849 err = got_object_id_str(&tag_id_str, tag_id);
3850 printf("Created tag %s\n", tag_id_str);
3851 free(tag_id_str);
3853 done:
3854 if (ref)
3855 got_ref_close(ref);
3856 free(commit_id);
3857 free(commit_id_str);
3858 free(refname);
3859 free(tagmsg);
3860 free(tagger);
3861 return err;
3864 static const struct got_error *
3865 cmd_tag(int argc, char *argv[])
3867 const struct got_error *error = NULL;
3868 struct got_repository *repo = NULL;
3869 struct got_worktree *worktree = NULL;
3870 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3871 char *gitconfig_path = NULL;
3872 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3873 int ch, do_list = 0;
3875 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3876 switch (ch) {
3877 case 'm':
3878 tagmsg = optarg;
3879 break;
3880 case 'r':
3881 repo_path = realpath(optarg, NULL);
3882 if (repo_path == NULL)
3883 err(1, "-r option");
3884 got_path_strip_trailing_slashes(repo_path);
3885 break;
3886 case 'l':
3887 do_list = 1;
3888 break;
3889 default:
3890 usage_tag();
3891 /* NOTREACHED */
3895 argc -= optind;
3896 argv += optind;
3898 if (do_list) {
3899 if (tagmsg)
3900 errx(1, "-l and -m options are mutually exclusive\n");
3901 if (argc > 0)
3902 usage_tag();
3903 } else if (argc < 1 || argc > 2)
3904 usage_tag();
3905 else if (argc > 1)
3906 commit_id_arg = argv[1];
3907 tag_name = argv[0];
3909 #ifndef PROFILE
3910 if (do_list) {
3911 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3912 NULL) == -1)
3913 err(1, "pledge");
3914 } else {
3915 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3916 "sendfd unveil", NULL) == -1)
3917 err(1, "pledge");
3919 #endif
3920 cwd = getcwd(NULL, 0);
3921 if (cwd == NULL) {
3922 error = got_error_from_errno("getcwd");
3923 goto done;
3926 if (repo_path == NULL) {
3927 error = got_worktree_open(&worktree, cwd);
3928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3929 goto done;
3930 else
3931 error = NULL;
3932 if (worktree) {
3933 repo_path =
3934 strdup(got_worktree_get_repo_path(worktree));
3935 if (repo_path == NULL)
3936 error = got_error_from_errno("strdup");
3937 if (error)
3938 goto done;
3939 } else {
3940 repo_path = strdup(cwd);
3941 if (repo_path == NULL) {
3942 error = got_error_from_errno("strdup");
3943 goto done;
3948 if (do_list) {
3949 error = got_repo_open(&repo, repo_path, NULL);
3950 if (error != NULL)
3951 goto done;
3952 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3953 if (error)
3954 goto done;
3955 error = list_tags(repo, worktree);
3956 } else {
3957 error = get_gitconfig_path(&gitconfig_path);
3958 if (error)
3959 goto done;
3960 error = got_repo_open(&repo, repo_path, gitconfig_path);
3961 if (error != NULL)
3962 goto done;
3964 if (tagmsg) {
3965 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3966 if (error)
3967 goto done;
3970 if (commit_id_arg == NULL) {
3971 struct got_reference *head_ref;
3972 struct got_object_id *commit_id;
3973 error = got_ref_open(&head_ref, repo,
3974 worktree ? got_worktree_get_head_ref_name(worktree)
3975 : GOT_REF_HEAD, 0);
3976 if (error)
3977 goto done;
3978 error = got_ref_resolve(&commit_id, repo, head_ref);
3979 got_ref_close(head_ref);
3980 if (error)
3981 goto done;
3982 error = got_object_id_str(&commit_id_str, commit_id);
3983 free(commit_id);
3984 if (error)
3985 goto done;
3988 error = add_tag(repo, tag_name,
3989 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3991 done:
3992 if (repo)
3993 got_repo_close(repo);
3994 if (worktree)
3995 got_worktree_close(worktree);
3996 free(cwd);
3997 free(repo_path);
3998 free(gitconfig_path);
3999 free(commit_id_str);
4000 return error;
4003 __dead static void
4004 usage_add(void)
4006 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4007 exit(1);
4010 static const struct got_error *
4011 cmd_add(int argc, char *argv[])
4013 const struct got_error *error = NULL;
4014 struct got_repository *repo = NULL;
4015 struct got_worktree *worktree = NULL;
4016 char *cwd = NULL;
4017 struct got_pathlist_head paths;
4018 struct got_pathlist_entry *pe;
4019 int ch;
4021 TAILQ_INIT(&paths);
4023 while ((ch = getopt(argc, argv, "")) != -1) {
4024 switch (ch) {
4025 default:
4026 usage_add();
4027 /* NOTREACHED */
4031 argc -= optind;
4032 argv += optind;
4034 #ifndef PROFILE
4035 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4036 NULL) == -1)
4037 err(1, "pledge");
4038 #endif
4039 if (argc < 1)
4040 usage_add();
4042 cwd = getcwd(NULL, 0);
4043 if (cwd == NULL) {
4044 error = got_error_from_errno("getcwd");
4045 goto done;
4048 error = got_worktree_open(&worktree, cwd);
4049 if (error)
4050 goto done;
4052 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4053 NULL);
4054 if (error != NULL)
4055 goto done;
4057 error = apply_unveil(got_repo_get_path(repo), 1,
4058 got_worktree_get_root_path(worktree));
4059 if (error)
4060 goto done;
4062 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4063 if (error)
4064 goto done;
4066 error = got_worktree_schedule_add(worktree, &paths, print_status,
4067 NULL, repo);
4068 done:
4069 if (repo)
4070 got_repo_close(repo);
4071 if (worktree)
4072 got_worktree_close(worktree);
4073 TAILQ_FOREACH(pe, &paths, entry)
4074 free((char *)pe->path);
4075 got_pathlist_free(&paths);
4076 free(cwd);
4077 return error;
4080 __dead static void
4081 usage_remove(void)
4083 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4084 exit(1);
4087 static const struct got_error *
4088 print_remove_status(void *arg, unsigned char status,
4089 unsigned char staged_status, const char *path,
4090 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4091 struct got_object_id *commit_id)
4093 if (status == GOT_STATUS_NONEXISTENT)
4094 return NULL;
4095 if (status == staged_status && (status == GOT_STATUS_DELETE))
4096 status = GOT_STATUS_NO_CHANGE;
4097 printf("%c%c %s\n", status, staged_status, path);
4098 return NULL;
4101 static const struct got_error *
4102 cmd_remove(int argc, char *argv[])
4104 const struct got_error *error = NULL;
4105 struct got_worktree *worktree = NULL;
4106 struct got_repository *repo = NULL;
4107 char *cwd = NULL;
4108 struct got_pathlist_head paths;
4109 struct got_pathlist_entry *pe;
4110 int ch, delete_local_mods = 0;
4112 TAILQ_INIT(&paths);
4114 while ((ch = getopt(argc, argv, "f")) != -1) {
4115 switch (ch) {
4116 case 'f':
4117 delete_local_mods = 1;
4118 break;
4119 default:
4120 usage_add();
4121 /* NOTREACHED */
4125 argc -= optind;
4126 argv += optind;
4128 #ifndef PROFILE
4129 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4130 NULL) == -1)
4131 err(1, "pledge");
4132 #endif
4133 if (argc < 1)
4134 usage_remove();
4136 cwd = getcwd(NULL, 0);
4137 if (cwd == NULL) {
4138 error = got_error_from_errno("getcwd");
4139 goto done;
4141 error = got_worktree_open(&worktree, cwd);
4142 if (error)
4143 goto done;
4145 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4146 NULL);
4147 if (error)
4148 goto done;
4150 error = apply_unveil(got_repo_get_path(repo), 1,
4151 got_worktree_get_root_path(worktree));
4152 if (error)
4153 goto done;
4155 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4156 if (error)
4157 goto done;
4159 error = got_worktree_schedule_delete(worktree, &paths,
4160 delete_local_mods, print_remove_status, NULL, repo);
4161 if (error)
4162 goto done;
4163 done:
4164 if (repo)
4165 got_repo_close(repo);
4166 if (worktree)
4167 got_worktree_close(worktree);
4168 TAILQ_FOREACH(pe, &paths, entry)
4169 free((char *)pe->path);
4170 got_pathlist_free(&paths);
4171 free(cwd);
4172 return error;
4175 __dead static void
4176 usage_revert(void)
4178 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4179 "path ...\n", getprogname());
4180 exit(1);
4183 static const struct got_error *
4184 revert_progress(void *arg, unsigned char status, const char *path)
4186 while (path[0] == '/')
4187 path++;
4188 printf("%c %s\n", status, path);
4189 return NULL;
4192 struct choose_patch_arg {
4193 FILE *patch_script_file;
4194 const char *action;
4197 static const struct got_error *
4198 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4199 int nchanges, const char *action)
4201 char *line = NULL;
4202 size_t linesize = 0;
4203 ssize_t linelen;
4205 switch (status) {
4206 case GOT_STATUS_ADD:
4207 printf("A %s\n%s this addition? [y/n] ", path, action);
4208 break;
4209 case GOT_STATUS_DELETE:
4210 printf("D %s\n%s this deletion? [y/n] ", path, action);
4211 break;
4212 case GOT_STATUS_MODIFY:
4213 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4214 return got_error_from_errno("fseek");
4215 printf(GOT_COMMIT_SEP_STR);
4216 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4217 printf("%s", line);
4218 if (ferror(patch_file))
4219 return got_error_from_errno("getline");
4220 printf(GOT_COMMIT_SEP_STR);
4221 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4222 path, n, nchanges, action);
4223 break;
4224 default:
4225 return got_error_path(path, GOT_ERR_FILE_STATUS);
4228 return NULL;
4231 static const struct got_error *
4232 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4233 FILE *patch_file, int n, int nchanges)
4235 const struct got_error *err = NULL;
4236 char *line = NULL;
4237 size_t linesize = 0;
4238 ssize_t linelen;
4239 int resp = ' ';
4240 struct choose_patch_arg *a = arg;
4242 *choice = GOT_PATCH_CHOICE_NONE;
4244 if (a->patch_script_file) {
4245 char *nl;
4246 err = show_change(status, path, patch_file, n, nchanges,
4247 a->action);
4248 if (err)
4249 return err;
4250 linelen = getline(&line, &linesize, a->patch_script_file);
4251 if (linelen == -1) {
4252 if (ferror(a->patch_script_file))
4253 return got_error_from_errno("getline");
4254 return NULL;
4256 nl = strchr(line, '\n');
4257 if (nl)
4258 *nl = '\0';
4259 if (strcmp(line, "y") == 0) {
4260 *choice = GOT_PATCH_CHOICE_YES;
4261 printf("y\n");
4262 } else if (strcmp(line, "n") == 0) {
4263 *choice = GOT_PATCH_CHOICE_NO;
4264 printf("n\n");
4265 } else if (strcmp(line, "q") == 0 &&
4266 status == GOT_STATUS_MODIFY) {
4267 *choice = GOT_PATCH_CHOICE_QUIT;
4268 printf("q\n");
4269 } else
4270 printf("invalid response '%s'\n", line);
4271 free(line);
4272 return NULL;
4275 while (resp != 'y' && resp != 'n' && resp != 'q') {
4276 err = show_change(status, path, patch_file, n, nchanges,
4277 a->action);
4278 if (err)
4279 return err;
4280 resp = getchar();
4281 if (resp == '\n')
4282 resp = getchar();
4283 if (status == GOT_STATUS_MODIFY) {
4284 if (resp != 'y' && resp != 'n' && resp != 'q') {
4285 printf("invalid response '%c'\n", resp);
4286 resp = ' ';
4288 } else if (resp != 'y' && resp != 'n') {
4289 printf("invalid response '%c'\n", resp);
4290 resp = ' ';
4294 if (resp == 'y')
4295 *choice = GOT_PATCH_CHOICE_YES;
4296 else if (resp == 'n')
4297 *choice = GOT_PATCH_CHOICE_NO;
4298 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4299 *choice = GOT_PATCH_CHOICE_QUIT;
4301 return NULL;
4305 static const struct got_error *
4306 cmd_revert(int argc, char *argv[])
4308 const struct got_error *error = NULL;
4309 struct got_worktree *worktree = NULL;
4310 struct got_repository *repo = NULL;
4311 char *cwd = NULL, *path = NULL;
4312 struct got_pathlist_head paths;
4313 struct got_pathlist_entry *pe;
4314 int ch, can_recurse = 0, pflag = 0;
4315 FILE *patch_script_file = NULL;
4316 const char *patch_script_path = NULL;
4317 struct choose_patch_arg cpa;
4319 TAILQ_INIT(&paths);
4321 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4322 switch (ch) {
4323 case 'p':
4324 pflag = 1;
4325 break;
4326 case 'F':
4327 patch_script_path = optarg;
4328 break;
4329 case 'R':
4330 can_recurse = 1;
4331 break;
4332 default:
4333 usage_revert();
4334 /* NOTREACHED */
4338 argc -= optind;
4339 argv += optind;
4341 #ifndef PROFILE
4342 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4343 "unveil", NULL) == -1)
4344 err(1, "pledge");
4345 #endif
4346 if (argc < 1)
4347 usage_revert();
4348 if (patch_script_path && !pflag)
4349 errx(1, "-F option can only be used together with -p option");
4351 cwd = getcwd(NULL, 0);
4352 if (cwd == NULL) {
4353 error = got_error_from_errno("getcwd");
4354 goto done;
4356 error = got_worktree_open(&worktree, cwd);
4357 if (error)
4358 goto done;
4360 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4361 NULL);
4362 if (error != NULL)
4363 goto done;
4365 if (patch_script_path) {
4366 patch_script_file = fopen(patch_script_path, "r");
4367 if (patch_script_file == NULL) {
4368 error = got_error_from_errno2("fopen",
4369 patch_script_path);
4370 goto done;
4373 error = apply_unveil(got_repo_get_path(repo), 1,
4374 got_worktree_get_root_path(worktree));
4375 if (error)
4376 goto done;
4378 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4379 if (error)
4380 goto done;
4382 if (!can_recurse) {
4383 char *ondisk_path;
4384 struct stat sb;
4385 TAILQ_FOREACH(pe, &paths, entry) {
4386 if (asprintf(&ondisk_path, "%s/%s",
4387 got_worktree_get_root_path(worktree),
4388 pe->path) == -1) {
4389 error = got_error_from_errno("asprintf");
4390 goto done;
4392 if (lstat(ondisk_path, &sb) == -1) {
4393 if (errno == ENOENT) {
4394 free(ondisk_path);
4395 continue;
4397 error = got_error_from_errno2("lstat",
4398 ondisk_path);
4399 free(ondisk_path);
4400 goto done;
4402 free(ondisk_path);
4403 if (S_ISDIR(sb.st_mode)) {
4404 error = got_error_msg(GOT_ERR_BAD_PATH,
4405 "reverting directories requires -R option");
4406 goto done;
4411 cpa.patch_script_file = patch_script_file;
4412 cpa.action = "revert";
4413 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4414 pflag ? choose_patch : NULL, &cpa, repo);
4415 if (error)
4416 goto done;
4417 done:
4418 if (patch_script_file && fclose(patch_script_file) == EOF &&
4419 error == NULL)
4420 error = got_error_from_errno2("fclose", patch_script_path);
4421 if (repo)
4422 got_repo_close(repo);
4423 if (worktree)
4424 got_worktree_close(worktree);
4425 free(path);
4426 free(cwd);
4427 return error;
4430 __dead static void
4431 usage_commit(void)
4433 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4434 getprogname());
4435 exit(1);
4438 struct collect_commit_logmsg_arg {
4439 const char *cmdline_log;
4440 const char *editor;
4441 const char *worktree_path;
4442 const char *branch_name;
4443 const char *repo_path;
4444 char *logmsg_path;
4448 static const struct got_error *
4449 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4450 void *arg)
4452 char *initial_content = NULL;
4453 struct got_pathlist_entry *pe;
4454 const struct got_error *err = NULL;
4455 char *template = NULL;
4456 struct collect_commit_logmsg_arg *a = arg;
4457 int fd;
4458 size_t len;
4460 /* if a message was specified on the command line, just use it */
4461 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4462 len = strlen(a->cmdline_log) + 1;
4463 *logmsg = malloc(len + 1);
4464 if (*logmsg == NULL)
4465 return got_error_from_errno("malloc");
4466 strlcpy(*logmsg, a->cmdline_log, len);
4467 return NULL;
4470 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4471 return got_error_from_errno("asprintf");
4473 if (asprintf(&initial_content,
4474 "\n# changes to be committed on branch %s:\n",
4475 a->branch_name) == -1)
4476 return got_error_from_errno("asprintf");
4478 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4479 if (err)
4480 goto done;
4482 dprintf(fd, initial_content);
4484 TAILQ_FOREACH(pe, commitable_paths, entry) {
4485 struct got_commitable *ct = pe->data;
4486 dprintf(fd, "# %c %s\n",
4487 got_commitable_get_status(ct),
4488 got_commitable_get_path(ct));
4490 close(fd);
4492 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4493 done:
4494 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4495 unlink(a->logmsg_path);
4496 free(a->logmsg_path);
4497 a->logmsg_path = NULL;
4499 free(initial_content);
4500 free(template);
4502 /* Editor is done; we can now apply unveil(2) */
4503 if (err == NULL) {
4504 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4505 if (err) {
4506 free(*logmsg);
4507 *logmsg = NULL;
4510 return err;
4513 static const struct got_error *
4514 cmd_commit(int argc, char *argv[])
4516 const struct got_error *error = NULL;
4517 struct got_worktree *worktree = NULL;
4518 struct got_repository *repo = NULL;
4519 char *cwd = NULL, *id_str = NULL;
4520 struct got_object_id *id = NULL;
4521 const char *logmsg = NULL;
4522 struct collect_commit_logmsg_arg cl_arg;
4523 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4524 int ch, rebase_in_progress, histedit_in_progress;
4525 struct got_pathlist_head paths;
4527 TAILQ_INIT(&paths);
4528 cl_arg.logmsg_path = NULL;
4530 while ((ch = getopt(argc, argv, "m:")) != -1) {
4531 switch (ch) {
4532 case 'm':
4533 logmsg = optarg;
4534 break;
4535 default:
4536 usage_commit();
4537 /* NOTREACHED */
4541 argc -= optind;
4542 argv += optind;
4544 #ifndef PROFILE
4545 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4546 "unveil", NULL) == -1)
4547 err(1, "pledge");
4548 #endif
4549 cwd = getcwd(NULL, 0);
4550 if (cwd == NULL) {
4551 error = got_error_from_errno("getcwd");
4552 goto done;
4554 error = got_worktree_open(&worktree, cwd);
4555 if (error)
4556 goto done;
4558 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4559 if (error)
4560 goto done;
4561 if (rebase_in_progress) {
4562 error = got_error(GOT_ERR_REBASING);
4563 goto done;
4566 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4567 worktree);
4568 if (error)
4569 goto done;
4571 error = get_gitconfig_path(&gitconfig_path);
4572 if (error)
4573 goto done;
4574 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4575 gitconfig_path);
4576 if (error != NULL)
4577 goto done;
4579 error = get_author(&author, repo);
4580 if (error)
4581 return error;
4584 * unveil(2) traverses exec(2); if an editor is used we have
4585 * to apply unveil after the log message has been written.
4587 if (logmsg == NULL || strlen(logmsg) == 0)
4588 error = get_editor(&editor);
4589 else
4590 error = apply_unveil(got_repo_get_path(repo), 0,
4591 got_worktree_get_root_path(worktree));
4592 if (error)
4593 goto done;
4595 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4596 if (error)
4597 goto done;
4599 cl_arg.editor = editor;
4600 cl_arg.cmdline_log = logmsg;
4601 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4602 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4603 if (!histedit_in_progress) {
4604 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4605 error = got_error(GOT_ERR_COMMIT_BRANCH);
4606 goto done;
4608 cl_arg.branch_name += 11;
4610 cl_arg.repo_path = got_repo_get_path(repo);
4611 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4612 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4613 if (error) {
4614 if (cl_arg.logmsg_path)
4615 fprintf(stderr, "%s: log message preserved in %s\n",
4616 getprogname(), cl_arg.logmsg_path);
4617 goto done;
4620 if (cl_arg.logmsg_path)
4621 unlink(cl_arg.logmsg_path);
4623 error = got_object_id_str(&id_str, id);
4624 if (error)
4625 goto done;
4626 printf("Created commit %s\n", id_str);
4627 done:
4628 free(cl_arg.logmsg_path);
4629 if (repo)
4630 got_repo_close(repo);
4631 if (worktree)
4632 got_worktree_close(worktree);
4633 free(cwd);
4634 free(id_str);
4635 free(gitconfig_path);
4636 free(editor);
4637 free(author);
4638 return error;
4641 __dead static void
4642 usage_cherrypick(void)
4644 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4645 exit(1);
4648 static const struct got_error *
4649 cmd_cherrypick(int argc, char *argv[])
4651 const struct got_error *error = NULL;
4652 struct got_worktree *worktree = NULL;
4653 struct got_repository *repo = NULL;
4654 char *cwd = NULL, *commit_id_str = NULL;
4655 struct got_object_id *commit_id = NULL;
4656 struct got_commit_object *commit = NULL;
4657 struct got_object_qid *pid;
4658 struct got_reference *head_ref = NULL;
4659 int ch, did_something = 0;
4661 while ((ch = getopt(argc, argv, "")) != -1) {
4662 switch (ch) {
4663 default:
4664 usage_cherrypick();
4665 /* NOTREACHED */
4669 argc -= optind;
4670 argv += optind;
4672 #ifndef PROFILE
4673 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4674 "unveil", NULL) == -1)
4675 err(1, "pledge");
4676 #endif
4677 if (argc != 1)
4678 usage_cherrypick();
4680 cwd = getcwd(NULL, 0);
4681 if (cwd == NULL) {
4682 error = got_error_from_errno("getcwd");
4683 goto done;
4685 error = got_worktree_open(&worktree, cwd);
4686 if (error)
4687 goto done;
4689 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4690 NULL);
4691 if (error != NULL)
4692 goto done;
4694 error = apply_unveil(got_repo_get_path(repo), 0,
4695 got_worktree_get_root_path(worktree));
4696 if (error)
4697 goto done;
4699 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4700 GOT_OBJ_TYPE_COMMIT, repo);
4701 if (error != NULL) {
4702 struct got_reference *ref;
4703 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4704 goto done;
4705 error = got_ref_open(&ref, repo, argv[0], 0);
4706 if (error != NULL)
4707 goto done;
4708 error = got_ref_resolve(&commit_id, repo, ref);
4709 got_ref_close(ref);
4710 if (error != NULL)
4711 goto done;
4713 error = got_object_id_str(&commit_id_str, commit_id);
4714 if (error)
4715 goto done;
4717 error = got_ref_open(&head_ref, repo,
4718 got_worktree_get_head_ref_name(worktree), 0);
4719 if (error != NULL)
4720 goto done;
4722 error = check_same_branch(commit_id, head_ref, NULL, repo);
4723 if (error) {
4724 if (error->code != GOT_ERR_ANCESTRY)
4725 goto done;
4726 error = NULL;
4727 } else {
4728 error = got_error(GOT_ERR_SAME_BRANCH);
4729 goto done;
4732 error = got_object_open_as_commit(&commit, repo, commit_id);
4733 if (error)
4734 goto done;
4735 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4736 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4737 commit_id, repo, update_progress, &did_something, check_cancelled,
4738 NULL);
4739 if (error != NULL)
4740 goto done;
4742 if (did_something)
4743 printf("Merged commit %s\n", commit_id_str);
4744 done:
4745 if (commit)
4746 got_object_commit_close(commit);
4747 free(commit_id_str);
4748 if (head_ref)
4749 got_ref_close(head_ref);
4750 if (worktree)
4751 got_worktree_close(worktree);
4752 if (repo)
4753 got_repo_close(repo);
4754 return error;
4757 __dead static void
4758 usage_backout(void)
4760 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4761 exit(1);
4764 static const struct got_error *
4765 cmd_backout(int argc, char *argv[])
4767 const struct got_error *error = NULL;
4768 struct got_worktree *worktree = NULL;
4769 struct got_repository *repo = NULL;
4770 char *cwd = NULL, *commit_id_str = NULL;
4771 struct got_object_id *commit_id = NULL;
4772 struct got_commit_object *commit = NULL;
4773 struct got_object_qid *pid;
4774 struct got_reference *head_ref = NULL;
4775 int ch, did_something = 0;
4777 while ((ch = getopt(argc, argv, "")) != -1) {
4778 switch (ch) {
4779 default:
4780 usage_backout();
4781 /* NOTREACHED */
4785 argc -= optind;
4786 argv += optind;
4788 #ifndef PROFILE
4789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4790 "unveil", NULL) == -1)
4791 err(1, "pledge");
4792 #endif
4793 if (argc != 1)
4794 usage_backout();
4796 cwd = getcwd(NULL, 0);
4797 if (cwd == NULL) {
4798 error = got_error_from_errno("getcwd");
4799 goto done;
4801 error = got_worktree_open(&worktree, cwd);
4802 if (error)
4803 goto done;
4805 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4806 NULL);
4807 if (error != NULL)
4808 goto done;
4810 error = apply_unveil(got_repo_get_path(repo), 0,
4811 got_worktree_get_root_path(worktree));
4812 if (error)
4813 goto done;
4815 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4816 GOT_OBJ_TYPE_COMMIT, repo);
4817 if (error != NULL) {
4818 struct got_reference *ref;
4819 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4820 goto done;
4821 error = got_ref_open(&ref, repo, argv[0], 0);
4822 if (error != NULL)
4823 goto done;
4824 error = got_ref_resolve(&commit_id, repo, ref);
4825 got_ref_close(ref);
4826 if (error != NULL)
4827 goto done;
4829 error = got_object_id_str(&commit_id_str, commit_id);
4830 if (error)
4831 goto done;
4833 error = got_ref_open(&head_ref, repo,
4834 got_worktree_get_head_ref_name(worktree), 0);
4835 if (error != NULL)
4836 goto done;
4838 error = check_same_branch(commit_id, head_ref, NULL, repo);
4839 if (error)
4840 goto done;
4842 error = got_object_open_as_commit(&commit, repo, commit_id);
4843 if (error)
4844 goto done;
4845 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4846 if (pid == NULL) {
4847 error = got_error(GOT_ERR_ROOT_COMMIT);
4848 goto done;
4851 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4852 update_progress, &did_something, check_cancelled, NULL);
4853 if (error != NULL)
4854 goto done;
4856 if (did_something)
4857 printf("Backed out commit %s\n", commit_id_str);
4858 done:
4859 if (commit)
4860 got_object_commit_close(commit);
4861 free(commit_id_str);
4862 if (head_ref)
4863 got_ref_close(head_ref);
4864 if (worktree)
4865 got_worktree_close(worktree);
4866 if (repo)
4867 got_repo_close(repo);
4868 return error;
4871 __dead static void
4872 usage_rebase(void)
4874 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4875 getprogname());
4876 exit(1);
4879 void
4880 trim_logmsg(char *logmsg, int limit)
4882 char *nl;
4883 size_t len;
4885 len = strlen(logmsg);
4886 if (len > limit)
4887 len = limit;
4888 logmsg[len] = '\0';
4889 nl = strchr(logmsg, '\n');
4890 if (nl)
4891 *nl = '\0';
4894 static const struct got_error *
4895 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4897 const struct got_error *err;
4898 char *logmsg0 = NULL;
4899 const char *s;
4901 err = got_object_commit_get_logmsg(&logmsg0, commit);
4902 if (err)
4903 return err;
4905 s = logmsg0;
4906 while (isspace((unsigned char)s[0]))
4907 s++;
4909 *logmsg = strdup(s);
4910 if (*logmsg == NULL) {
4911 err = got_error_from_errno("strdup");
4912 goto done;
4915 trim_logmsg(*logmsg, limit);
4916 done:
4917 free(logmsg0);
4918 return err;
4921 static const struct got_error *
4922 show_rebase_progress(struct got_commit_object *commit,
4923 struct got_object_id *old_id, struct got_object_id *new_id)
4925 const struct got_error *err;
4926 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4928 err = got_object_id_str(&old_id_str, old_id);
4929 if (err)
4930 goto done;
4932 if (new_id) {
4933 err = got_object_id_str(&new_id_str, new_id);
4934 if (err)
4935 goto done;
4938 old_id_str[12] = '\0';
4939 if (new_id_str)
4940 new_id_str[12] = '\0';
4942 err = get_short_logmsg(&logmsg, 42, commit);
4943 if (err)
4944 goto done;
4946 printf("%s -> %s: %s\n", old_id_str,
4947 new_id_str ? new_id_str : "no-op change", logmsg);
4948 done:
4949 free(old_id_str);
4950 free(new_id_str);
4951 return err;
4954 static const struct got_error *
4955 rebase_progress(void *arg, unsigned char status, const char *path)
4957 unsigned char *rebase_status = arg;
4959 while (path[0] == '/')
4960 path++;
4961 printf("%c %s\n", status, path);
4963 if (*rebase_status == GOT_STATUS_CONFLICT)
4964 return NULL;
4965 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4966 *rebase_status = status;
4967 return NULL;
4970 static const struct got_error *
4971 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4972 struct got_reference *branch, struct got_reference *new_base_branch,
4973 struct got_reference *tmp_branch, struct got_repository *repo)
4975 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4976 return got_worktree_rebase_complete(worktree, fileindex,
4977 new_base_branch, tmp_branch, branch, repo);
4980 static const struct got_error *
4981 rebase_commit(struct got_pathlist_head *merged_paths,
4982 struct got_worktree *worktree, struct got_fileindex *fileindex,
4983 struct got_reference *tmp_branch,
4984 struct got_object_id *commit_id, struct got_repository *repo)
4986 const struct got_error *error;
4987 struct got_commit_object *commit;
4988 struct got_object_id *new_commit_id;
4990 error = got_object_open_as_commit(&commit, repo, commit_id);
4991 if (error)
4992 return error;
4994 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4995 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4996 if (error) {
4997 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4998 goto done;
4999 error = show_rebase_progress(commit, commit_id, NULL);
5000 } else {
5001 error = show_rebase_progress(commit, commit_id, new_commit_id);
5002 free(new_commit_id);
5004 done:
5005 got_object_commit_close(commit);
5006 return error;
5009 struct check_path_prefix_arg {
5010 const char *path_prefix;
5011 size_t len;
5012 int errcode;
5015 static const struct got_error *
5016 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5017 struct got_blob_object *blob2, struct got_object_id *id1,
5018 struct got_object_id *id2, const char *path1, const char *path2,
5019 struct got_repository *repo)
5021 struct check_path_prefix_arg *a = arg;
5023 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5024 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5025 return got_error(a->errcode);
5027 return NULL;
5030 static const struct got_error *
5031 check_path_prefix(struct got_object_id *parent_id,
5032 struct got_object_id *commit_id, const char *path_prefix,
5033 int errcode, struct got_repository *repo)
5035 const struct got_error *err;
5036 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5037 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5038 struct check_path_prefix_arg cpp_arg;
5040 if (got_path_is_root_dir(path_prefix))
5041 return NULL;
5043 err = got_object_open_as_commit(&commit, repo, commit_id);
5044 if (err)
5045 goto done;
5047 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5048 if (err)
5049 goto done;
5051 err = got_object_open_as_tree(&tree1, repo,
5052 got_object_commit_get_tree_id(parent_commit));
5053 if (err)
5054 goto done;
5056 err = got_object_open_as_tree(&tree2, repo,
5057 got_object_commit_get_tree_id(commit));
5058 if (err)
5059 goto done;
5061 cpp_arg.path_prefix = path_prefix;
5062 while (cpp_arg.path_prefix[0] == '/')
5063 cpp_arg.path_prefix++;
5064 cpp_arg.len = strlen(cpp_arg.path_prefix);
5065 cpp_arg.errcode = errcode;
5066 err = got_diff_tree(tree1, tree2, "", "", repo,
5067 check_path_prefix_in_diff, &cpp_arg, 0);
5068 done:
5069 if (tree1)
5070 got_object_tree_close(tree1);
5071 if (tree2)
5072 got_object_tree_close(tree2);
5073 if (commit)
5074 got_object_commit_close(commit);
5075 if (parent_commit)
5076 got_object_commit_close(parent_commit);
5077 return err;
5080 static const struct got_error *
5081 collect_commits(struct got_object_id_queue *commits,
5082 struct got_object_id *initial_commit_id,
5083 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5084 const char *path_prefix, int path_prefix_errcode,
5085 struct got_repository *repo)
5087 const struct got_error *err = NULL;
5088 struct got_commit_graph *graph = NULL;
5089 struct got_object_id *parent_id = NULL;
5090 struct got_object_qid *qid;
5091 struct got_object_id *commit_id = initial_commit_id;
5093 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5094 if (err)
5095 return err;
5097 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5098 check_cancelled, NULL);
5099 if (err)
5100 goto done;
5101 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5102 err = got_commit_graph_iter_next(&parent_id, graph);
5103 if (err) {
5104 if (err->code == GOT_ERR_ITER_COMPLETED) {
5105 err = got_error_msg(GOT_ERR_ANCESTRY,
5106 "ran out of commits to rebase before "
5107 "youngest common ancestor commit has "
5108 "been reached?!?");
5109 goto done;
5110 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5111 goto done;
5112 err = got_commit_graph_fetch_commits(graph, 1, repo,
5113 check_cancelled, NULL);
5114 if (err)
5115 goto done;
5116 } else {
5117 err = check_path_prefix(parent_id, commit_id,
5118 path_prefix, path_prefix_errcode, repo);
5119 if (err)
5120 goto done;
5122 err = got_object_qid_alloc(&qid, commit_id);
5123 if (err)
5124 goto done;
5125 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5126 commit_id = parent_id;
5129 done:
5130 got_commit_graph_close(graph);
5131 return err;
5134 static const struct got_error *
5135 cmd_rebase(int argc, char *argv[])
5137 const struct got_error *error = NULL;
5138 struct got_worktree *worktree = NULL;
5139 struct got_repository *repo = NULL;
5140 struct got_fileindex *fileindex = NULL;
5141 char *cwd = NULL;
5142 struct got_reference *branch = NULL;
5143 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5144 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5145 struct got_object_id *resume_commit_id = NULL;
5146 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5147 struct got_commit_object *commit = NULL;
5148 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5149 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5150 struct got_object_id_queue commits;
5151 struct got_pathlist_head merged_paths;
5152 const struct got_object_id_queue *parent_ids;
5153 struct got_object_qid *qid, *pid;
5155 SIMPLEQ_INIT(&commits);
5156 TAILQ_INIT(&merged_paths);
5158 while ((ch = getopt(argc, argv, "ac")) != -1) {
5159 switch (ch) {
5160 case 'a':
5161 abort_rebase = 1;
5162 break;
5163 case 'c':
5164 continue_rebase = 1;
5165 break;
5166 default:
5167 usage_rebase();
5168 /* NOTREACHED */
5172 argc -= optind;
5173 argv += optind;
5175 #ifndef PROFILE
5176 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5177 "unveil", NULL) == -1)
5178 err(1, "pledge");
5179 #endif
5180 if (abort_rebase && continue_rebase)
5181 usage_rebase();
5182 else if (abort_rebase || continue_rebase) {
5183 if (argc != 0)
5184 usage_rebase();
5185 } else if (argc != 1)
5186 usage_rebase();
5188 cwd = getcwd(NULL, 0);
5189 if (cwd == NULL) {
5190 error = got_error_from_errno("getcwd");
5191 goto done;
5193 error = got_worktree_open(&worktree, cwd);
5194 if (error)
5195 goto done;
5197 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5198 NULL);
5199 if (error != NULL)
5200 goto done;
5202 error = apply_unveil(got_repo_get_path(repo), 0,
5203 got_worktree_get_root_path(worktree));
5204 if (error)
5205 goto done;
5207 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5208 if (error)
5209 goto done;
5211 if (abort_rebase) {
5212 int did_something;
5213 if (!rebase_in_progress) {
5214 error = got_error(GOT_ERR_NOT_REBASING);
5215 goto done;
5217 error = got_worktree_rebase_continue(&resume_commit_id,
5218 &new_base_branch, &tmp_branch, &branch, &fileindex,
5219 worktree, repo);
5220 if (error)
5221 goto done;
5222 printf("Switching work tree to %s\n",
5223 got_ref_get_symref_target(new_base_branch));
5224 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5225 new_base_branch, update_progress, &did_something);
5226 if (error)
5227 goto done;
5228 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5229 goto done; /* nothing else to do */
5232 if (continue_rebase) {
5233 if (!rebase_in_progress) {
5234 error = got_error(GOT_ERR_NOT_REBASING);
5235 goto done;
5237 error = got_worktree_rebase_continue(&resume_commit_id,
5238 &new_base_branch, &tmp_branch, &branch, &fileindex,
5239 worktree, repo);
5240 if (error)
5241 goto done;
5243 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5244 resume_commit_id, repo);
5245 if (error)
5246 goto done;
5248 yca_id = got_object_id_dup(resume_commit_id);
5249 if (yca_id == NULL) {
5250 error = got_error_from_errno("got_object_id_dup");
5251 goto done;
5253 } else {
5254 error = got_ref_open(&branch, repo, argv[0], 0);
5255 if (error != NULL)
5256 goto done;
5259 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5260 if (error)
5261 goto done;
5263 if (!continue_rebase) {
5264 struct got_object_id *base_commit_id;
5266 base_commit_id = got_worktree_get_base_commit_id(worktree);
5267 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5268 base_commit_id, branch_head_commit_id, repo,
5269 check_cancelled, NULL);
5270 if (error)
5271 goto done;
5272 if (yca_id == NULL) {
5273 error = got_error_msg(GOT_ERR_ANCESTRY,
5274 "specified branch shares no common ancestry "
5275 "with work tree's branch");
5276 goto done;
5279 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5280 if (error) {
5281 if (error->code != GOT_ERR_ANCESTRY)
5282 goto done;
5283 error = NULL;
5284 } else {
5285 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5286 "specified branch resolves to a commit which "
5287 "is already contained in work tree's branch");
5288 goto done;
5290 error = got_worktree_rebase_prepare(&new_base_branch,
5291 &tmp_branch, &fileindex, worktree, branch, repo);
5292 if (error)
5293 goto done;
5296 commit_id = branch_head_commit_id;
5297 error = got_object_open_as_commit(&commit, repo, commit_id);
5298 if (error)
5299 goto done;
5301 parent_ids = got_object_commit_get_parent_ids(commit);
5302 pid = SIMPLEQ_FIRST(parent_ids);
5303 if (pid == NULL) {
5304 if (!continue_rebase) {
5305 int did_something;
5306 error = got_worktree_rebase_abort(worktree, fileindex,
5307 repo, new_base_branch, update_progress,
5308 &did_something);
5309 if (error)
5310 goto done;
5311 printf("Rebase of %s aborted\n",
5312 got_ref_get_name(branch));
5314 error = got_error(GOT_ERR_EMPTY_REBASE);
5315 goto done;
5317 error = collect_commits(&commits, commit_id, pid->id,
5318 yca_id, got_worktree_get_path_prefix(worktree),
5319 GOT_ERR_REBASE_PATH, repo);
5320 got_object_commit_close(commit);
5321 commit = NULL;
5322 if (error)
5323 goto done;
5325 if (SIMPLEQ_EMPTY(&commits)) {
5326 if (continue_rebase)
5327 error = rebase_complete(worktree, fileindex,
5328 branch, new_base_branch, tmp_branch, repo);
5329 else
5330 error = got_error(GOT_ERR_EMPTY_REBASE);
5331 goto done;
5334 pid = NULL;
5335 SIMPLEQ_FOREACH(qid, &commits, entry) {
5336 commit_id = qid->id;
5337 parent_id = pid ? pid->id : yca_id;
5338 pid = qid;
5340 error = got_worktree_rebase_merge_files(&merged_paths,
5341 worktree, fileindex, parent_id, commit_id, repo,
5342 rebase_progress, &rebase_status, check_cancelled, NULL);
5343 if (error)
5344 goto done;
5346 if (rebase_status == GOT_STATUS_CONFLICT) {
5347 got_worktree_rebase_pathlist_free(&merged_paths);
5348 break;
5351 error = rebase_commit(&merged_paths, worktree, fileindex,
5352 tmp_branch, commit_id, repo);
5353 got_worktree_rebase_pathlist_free(&merged_paths);
5354 if (error)
5355 goto done;
5358 if (rebase_status == GOT_STATUS_CONFLICT) {
5359 error = got_worktree_rebase_postpone(worktree, fileindex);
5360 if (error)
5361 goto done;
5362 error = got_error_msg(GOT_ERR_CONFLICTS,
5363 "conflicts must be resolved before rebasing can continue");
5364 } else
5365 error = rebase_complete(worktree, fileindex, branch,
5366 new_base_branch, tmp_branch, repo);
5367 done:
5368 got_object_id_queue_free(&commits);
5369 free(branch_head_commit_id);
5370 free(resume_commit_id);
5371 free(yca_id);
5372 if (commit)
5373 got_object_commit_close(commit);
5374 if (branch)
5375 got_ref_close(branch);
5376 if (new_base_branch)
5377 got_ref_close(new_base_branch);
5378 if (tmp_branch)
5379 got_ref_close(tmp_branch);
5380 if (worktree)
5381 got_worktree_close(worktree);
5382 if (repo)
5383 got_repo_close(repo);
5384 return error;
5387 __dead static void
5388 usage_histedit(void)
5390 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5391 getprogname());
5392 exit(1);
5395 #define GOT_HISTEDIT_PICK 'p'
5396 #define GOT_HISTEDIT_EDIT 'e'
5397 #define GOT_HISTEDIT_FOLD 'f'
5398 #define GOT_HISTEDIT_DROP 'd'
5399 #define GOT_HISTEDIT_MESG 'm'
5401 static struct got_histedit_cmd {
5402 unsigned char code;
5403 const char *name;
5404 const char *desc;
5405 } got_histedit_cmds[] = {
5406 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5407 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5408 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5409 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5410 { GOT_HISTEDIT_MESG, "mesg",
5411 "single-line log message for commit above (open editor if empty)" },
5414 struct got_histedit_list_entry {
5415 TAILQ_ENTRY(got_histedit_list_entry) entry;
5416 struct got_object_id *commit_id;
5417 const struct got_histedit_cmd *cmd;
5418 char *logmsg;
5420 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5422 static const struct got_error *
5423 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5424 FILE *f, struct got_repository *repo)
5426 const struct got_error *err = NULL;
5427 char *logmsg = NULL, *id_str = NULL;
5428 struct got_commit_object *commit = NULL;
5429 int n;
5431 err = got_object_open_as_commit(&commit, repo, commit_id);
5432 if (err)
5433 goto done;
5435 err = get_short_logmsg(&logmsg, 34, commit);
5436 if (err)
5437 goto done;
5439 err = got_object_id_str(&id_str, commit_id);
5440 if (err)
5441 goto done;
5443 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5444 if (n < 0)
5445 err = got_ferror(f, GOT_ERR_IO);
5446 done:
5447 if (commit)
5448 got_object_commit_close(commit);
5449 free(id_str);
5450 free(logmsg);
5451 return err;
5454 static const struct got_error *
5455 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5456 struct got_repository *repo)
5458 const struct got_error *err = NULL;
5459 struct got_object_qid *qid;
5461 if (SIMPLEQ_EMPTY(commits))
5462 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5464 SIMPLEQ_FOREACH(qid, commits, entry) {
5465 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5466 f, repo);
5467 if (err)
5468 break;
5471 return err;
5474 static const struct got_error *
5475 write_cmd_list(FILE *f)
5477 const struct got_error *err = NULL;
5478 int n, i;
5480 n = fprintf(f, "# Available histedit commands:\n");
5481 if (n < 0)
5482 return got_ferror(f, GOT_ERR_IO);
5484 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5485 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5486 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5487 cmd->desc);
5488 if (n < 0) {
5489 err = got_ferror(f, GOT_ERR_IO);
5490 break;
5493 n = fprintf(f, "# Commits will be processed in order from top to "
5494 "bottom of this file.\n");
5495 if (n < 0)
5496 return got_ferror(f, GOT_ERR_IO);
5497 return err;
5500 static const struct got_error *
5501 histedit_syntax_error(int lineno)
5503 static char msg[42];
5504 int ret;
5506 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5507 lineno);
5508 if (ret == -1 || ret >= sizeof(msg))
5509 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5511 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5514 static const struct got_error *
5515 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5516 char *logmsg, struct got_repository *repo)
5518 const struct got_error *err;
5519 struct got_commit_object *folded_commit = NULL;
5520 char *id_str, *folded_logmsg = NULL;
5522 err = got_object_id_str(&id_str, hle->commit_id);
5523 if (err)
5524 return err;
5526 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5527 if (err)
5528 goto done;
5530 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5531 if (err)
5532 goto done;
5533 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5534 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5535 folded_logmsg) == -1) {
5536 err = got_error_from_errno("asprintf");
5537 goto done;
5539 done:
5540 if (folded_commit)
5541 got_object_commit_close(folded_commit);
5542 free(id_str);
5543 free(folded_logmsg);
5544 return err;
5547 static struct got_histedit_list_entry *
5548 get_folded_commits(struct got_histedit_list_entry *hle)
5550 struct got_histedit_list_entry *prev, *folded = NULL;
5552 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5553 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5554 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5555 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5556 folded = prev;
5557 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5560 return folded;
5563 static const struct got_error *
5564 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5565 struct got_repository *repo)
5567 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5568 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5569 const struct got_error *err = NULL;
5570 struct got_commit_object *commit = NULL;
5571 int fd;
5572 struct got_histedit_list_entry *folded = NULL;
5574 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5575 if (err)
5576 return err;
5578 folded = get_folded_commits(hle);
5579 if (folded) {
5580 while (folded != hle) {
5581 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5582 folded = TAILQ_NEXT(folded, entry);
5583 continue;
5585 err = append_folded_commit_msg(&new_msg, folded,
5586 logmsg, repo);
5587 if (err)
5588 goto done;
5589 free(logmsg);
5590 logmsg = new_msg;
5591 folded = TAILQ_NEXT(folded, entry);
5595 err = got_object_id_str(&id_str, hle->commit_id);
5596 if (err)
5597 goto done;
5598 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5599 if (err)
5600 goto done;
5601 if (asprintf(&new_msg,
5602 "%s\n# original log message of commit %s: %s",
5603 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5604 err = got_error_from_errno("asprintf");
5605 goto done;
5607 free(logmsg);
5608 logmsg = new_msg;
5610 err = got_object_id_str(&id_str, hle->commit_id);
5611 if (err)
5612 goto done;
5614 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5615 if (err)
5616 goto done;
5618 dprintf(fd, logmsg);
5619 close(fd);
5621 err = get_editor(&editor);
5622 if (err)
5623 goto done;
5625 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5626 if (err) {
5627 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5628 goto done;
5629 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5631 done:
5632 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5633 err = got_error_from_errno2("unlink", logmsg_path);
5634 free(logmsg_path);
5635 free(logmsg);
5636 free(orig_logmsg);
5637 free(editor);
5638 if (commit)
5639 got_object_commit_close(commit);
5640 return err;
5643 static const struct got_error *
5644 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5645 FILE *f, struct got_repository *repo)
5647 const struct got_error *err = NULL;
5648 char *line = NULL, *p, *end;
5649 size_t size;
5650 ssize_t len;
5651 int lineno = 0, i;
5652 const struct got_histedit_cmd *cmd;
5653 struct got_object_id *commit_id = NULL;
5654 struct got_histedit_list_entry *hle = NULL;
5656 for (;;) {
5657 len = getline(&line, &size, f);
5658 if (len == -1) {
5659 const struct got_error *getline_err;
5660 if (feof(f))
5661 break;
5662 getline_err = got_error_from_errno("getline");
5663 err = got_ferror(f, getline_err->code);
5664 break;
5666 lineno++;
5667 p = line;
5668 while (isspace((unsigned char)p[0]))
5669 p++;
5670 if (p[0] == '#' || p[0] == '\0') {
5671 free(line);
5672 line = NULL;
5673 continue;
5675 cmd = NULL;
5676 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5677 cmd = &got_histedit_cmds[i];
5678 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5679 isspace((unsigned char)p[strlen(cmd->name)])) {
5680 p += strlen(cmd->name);
5681 break;
5683 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5684 p++;
5685 break;
5688 if (i == nitems(got_histedit_cmds)) {
5689 err = histedit_syntax_error(lineno);
5690 break;
5692 while (isspace((unsigned char)p[0]))
5693 p++;
5694 if (cmd->code == GOT_HISTEDIT_MESG) {
5695 if (hle == NULL || hle->logmsg != NULL) {
5696 err = got_error(GOT_ERR_HISTEDIT_CMD);
5697 break;
5699 if (p[0] == '\0') {
5700 err = histedit_edit_logmsg(hle, repo);
5701 if (err)
5702 break;
5703 } else {
5704 hle->logmsg = strdup(p);
5705 if (hle->logmsg == NULL) {
5706 err = got_error_from_errno("strdup");
5707 break;
5710 free(line);
5711 line = NULL;
5712 continue;
5713 } else {
5714 end = p;
5715 while (end[0] && !isspace((unsigned char)end[0]))
5716 end++;
5717 *end = '\0';
5719 err = got_object_resolve_id_str(&commit_id, repo, p);
5720 if (err) {
5721 /* override error code */
5722 err = histedit_syntax_error(lineno);
5723 break;
5726 hle = malloc(sizeof(*hle));
5727 if (hle == NULL) {
5728 err = got_error_from_errno("malloc");
5729 break;
5731 hle->cmd = cmd;
5732 hle->commit_id = commit_id;
5733 hle->logmsg = NULL;
5734 commit_id = NULL;
5735 free(line);
5736 line = NULL;
5737 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5740 free(line);
5741 free(commit_id);
5742 return err;
5745 static const struct got_error *
5746 histedit_check_script(struct got_histedit_list *histedit_cmds,
5747 struct got_object_id_queue *commits, struct got_repository *repo)
5749 const struct got_error *err = NULL;
5750 struct got_object_qid *qid;
5751 struct got_histedit_list_entry *hle;
5752 static char msg[80];
5753 char *id_str;
5755 if (TAILQ_EMPTY(histedit_cmds))
5756 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5757 "histedit script contains no commands");
5758 if (SIMPLEQ_EMPTY(commits))
5759 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5761 SIMPLEQ_FOREACH(qid, commits, entry) {
5762 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5763 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5764 break;
5766 if (hle == NULL) {
5767 err = got_object_id_str(&id_str, qid->id);
5768 if (err)
5769 return err;
5770 snprintf(msg, sizeof(msg),
5771 "commit %s missing from histedit script", id_str);
5772 free(id_str);
5773 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5777 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5778 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5779 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5780 "last commit in histedit script cannot be folded");
5782 return NULL;
5785 static const struct got_error *
5786 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5787 const char *path, struct got_object_id_queue *commits,
5788 struct got_repository *repo)
5790 const struct got_error *err = NULL;
5791 char *editor;
5792 FILE *f = NULL;
5794 err = get_editor(&editor);
5795 if (err)
5796 return err;
5798 if (spawn_editor(editor, path) == -1) {
5799 err = got_error_from_errno("failed spawning editor");
5800 goto done;
5803 f = fopen(path, "r");
5804 if (f == NULL) {
5805 err = got_error_from_errno("fopen");
5806 goto done;
5808 err = histedit_parse_list(histedit_cmds, f, repo);
5809 if (err)
5810 goto done;
5812 err = histedit_check_script(histedit_cmds, commits, repo);
5813 done:
5814 if (f && fclose(f) != 0 && err == NULL)
5815 err = got_error_from_errno("fclose");
5816 free(editor);
5817 return err;
5820 static const struct got_error *
5821 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5822 struct got_object_id_queue *, const char *, struct got_repository *);
5824 static const struct got_error *
5825 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5826 struct got_object_id_queue *commits, struct got_repository *repo)
5828 const struct got_error *err;
5829 FILE *f = NULL;
5830 char *path = NULL;
5832 err = got_opentemp_named(&path, &f, "got-histedit");
5833 if (err)
5834 return err;
5836 err = write_cmd_list(f);
5837 if (err)
5838 goto done;
5840 err = histedit_write_commit_list(commits, f, repo);
5841 if (err)
5842 goto done;
5844 if (fclose(f) != 0) {
5845 err = got_error_from_errno("fclose");
5846 goto done;
5848 f = NULL;
5850 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5851 if (err) {
5852 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5853 err->code != GOT_ERR_HISTEDIT_CMD)
5854 goto done;
5855 err = histedit_edit_list_retry(histedit_cmds, err,
5856 commits, path, repo);
5858 done:
5859 if (f && fclose(f) != 0 && err == NULL)
5860 err = got_error_from_errno("fclose");
5861 if (path && unlink(path) != 0 && err == NULL)
5862 err = got_error_from_errno2("unlink", path);
5863 free(path);
5864 return err;
5867 static const struct got_error *
5868 histedit_save_list(struct got_histedit_list *histedit_cmds,
5869 struct got_worktree *worktree, struct got_repository *repo)
5871 const struct got_error *err = NULL;
5872 char *path = NULL;
5873 FILE *f = NULL;
5874 struct got_histedit_list_entry *hle;
5875 struct got_commit_object *commit = NULL;
5877 err = got_worktree_get_histedit_script_path(&path, worktree);
5878 if (err)
5879 return err;
5881 f = fopen(path, "w");
5882 if (f == NULL) {
5883 err = got_error_from_errno2("fopen", path);
5884 goto done;
5886 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5887 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5888 repo);
5889 if (err)
5890 break;
5892 if (hle->logmsg) {
5893 int n = fprintf(f, "%c %s\n",
5894 GOT_HISTEDIT_MESG, hle->logmsg);
5895 if (n < 0) {
5896 err = got_ferror(f, GOT_ERR_IO);
5897 break;
5901 done:
5902 if (f && fclose(f) != 0 && err == NULL)
5903 err = got_error_from_errno("fclose");
5904 free(path);
5905 if (commit)
5906 got_object_commit_close(commit);
5907 return err;
5910 void
5911 histedit_free_list(struct got_histedit_list *histedit_cmds)
5913 struct got_histedit_list_entry *hle;
5915 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5916 TAILQ_REMOVE(histedit_cmds, hle, entry);
5917 free(hle);
5921 static const struct got_error *
5922 histedit_load_list(struct got_histedit_list *histedit_cmds,
5923 const char *path, struct got_repository *repo)
5925 const struct got_error *err = NULL;
5926 FILE *f = NULL;
5928 f = fopen(path, "r");
5929 if (f == NULL) {
5930 err = got_error_from_errno2("fopen", path);
5931 goto done;
5934 err = histedit_parse_list(histedit_cmds, f, repo);
5935 done:
5936 if (f && fclose(f) != 0 && err == NULL)
5937 err = got_error_from_errno("fclose");
5938 return err;
5941 static const struct got_error *
5942 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5943 const struct got_error *edit_err, struct got_object_id_queue *commits,
5944 const char *path, struct got_repository *repo)
5946 const struct got_error *err = NULL, *prev_err = edit_err;
5947 int resp = ' ';
5949 while (resp != 'c' && resp != 'r' && resp != 'a') {
5950 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5951 "or (a)bort: ", getprogname(), prev_err->msg);
5952 resp = getchar();
5953 if (resp == '\n')
5954 resp = getchar();
5955 if (resp == 'c') {
5956 histedit_free_list(histedit_cmds);
5957 err = histedit_run_editor(histedit_cmds, path, commits,
5958 repo);
5959 if (err) {
5960 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5961 err->code != GOT_ERR_HISTEDIT_CMD)
5962 break;
5963 prev_err = err;
5964 resp = ' ';
5965 continue;
5967 break;
5968 } else if (resp == 'r') {
5969 histedit_free_list(histedit_cmds);
5970 err = histedit_edit_script(histedit_cmds,
5971 commits, repo);
5972 if (err) {
5973 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5974 err->code != GOT_ERR_HISTEDIT_CMD)
5975 break;
5976 prev_err = err;
5977 resp = ' ';
5978 continue;
5980 break;
5981 } else if (resp == 'a') {
5982 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5983 break;
5984 } else
5985 printf("invalid response '%c'\n", resp);
5988 return err;
5991 static const struct got_error *
5992 histedit_complete(struct got_worktree *worktree,
5993 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5994 struct got_reference *branch, struct got_repository *repo)
5996 printf("Switching work tree to %s\n",
5997 got_ref_get_symref_target(branch));
5998 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5999 branch, repo);
6002 static const struct got_error *
6003 show_histedit_progress(struct got_commit_object *commit,
6004 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6006 const struct got_error *err;
6007 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6009 err = got_object_id_str(&old_id_str, hle->commit_id);
6010 if (err)
6011 goto done;
6013 if (new_id) {
6014 err = got_object_id_str(&new_id_str, new_id);
6015 if (err)
6016 goto done;
6019 old_id_str[12] = '\0';
6020 if (new_id_str)
6021 new_id_str[12] = '\0';
6023 if (hle->logmsg) {
6024 logmsg = strdup(hle->logmsg);
6025 if (logmsg == NULL) {
6026 err = got_error_from_errno("strdup");
6027 goto done;
6029 trim_logmsg(logmsg, 42);
6030 } else {
6031 err = get_short_logmsg(&logmsg, 42, commit);
6032 if (err)
6033 goto done;
6036 switch (hle->cmd->code) {
6037 case GOT_HISTEDIT_PICK:
6038 case GOT_HISTEDIT_EDIT:
6039 printf("%s -> %s: %s\n", old_id_str,
6040 new_id_str ? new_id_str : "no-op change", logmsg);
6041 break;
6042 case GOT_HISTEDIT_DROP:
6043 case GOT_HISTEDIT_FOLD:
6044 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6045 logmsg);
6046 break;
6047 default:
6048 break;
6051 done:
6052 free(old_id_str);
6053 free(new_id_str);
6054 return err;
6057 static const struct got_error *
6058 histedit_commit(struct got_pathlist_head *merged_paths,
6059 struct got_worktree *worktree, struct got_fileindex *fileindex,
6060 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6061 struct got_repository *repo)
6063 const struct got_error *err;
6064 struct got_commit_object *commit;
6065 struct got_object_id *new_commit_id;
6067 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6068 && hle->logmsg == NULL) {
6069 err = histedit_edit_logmsg(hle, repo);
6070 if (err)
6071 return err;
6074 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6075 if (err)
6076 return err;
6078 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6079 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6080 hle->logmsg, repo);
6081 if (err) {
6082 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6083 goto done;
6084 err = show_histedit_progress(commit, hle, NULL);
6085 } else {
6086 err = show_histedit_progress(commit, hle, new_commit_id);
6087 free(new_commit_id);
6089 done:
6090 got_object_commit_close(commit);
6091 return err;
6094 static const struct got_error *
6095 histedit_skip_commit(struct got_histedit_list_entry *hle,
6096 struct got_worktree *worktree, struct got_repository *repo)
6098 const struct got_error *error;
6099 struct got_commit_object *commit;
6101 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6102 repo);
6103 if (error)
6104 return error;
6106 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6107 if (error)
6108 return error;
6110 error = show_histedit_progress(commit, hle, NULL);
6111 got_object_commit_close(commit);
6112 return error;
6115 static const struct got_error *
6116 cmd_histedit(int argc, char *argv[])
6118 const struct got_error *error = NULL;
6119 struct got_worktree *worktree = NULL;
6120 struct got_fileindex *fileindex = NULL;
6121 struct got_repository *repo = NULL;
6122 char *cwd = NULL;
6123 struct got_reference *branch = NULL;
6124 struct got_reference *tmp_branch = NULL;
6125 struct got_object_id *resume_commit_id = NULL;
6126 struct got_object_id *base_commit_id = NULL;
6127 struct got_object_id *head_commit_id = NULL;
6128 struct got_commit_object *commit = NULL;
6129 int ch, rebase_in_progress = 0, did_something;
6130 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6131 const char *edit_script_path = NULL;
6132 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6133 struct got_object_id_queue commits;
6134 struct got_pathlist_head merged_paths;
6135 const struct got_object_id_queue *parent_ids;
6136 struct got_object_qid *pid;
6137 struct got_histedit_list histedit_cmds;
6138 struct got_histedit_list_entry *hle;
6140 SIMPLEQ_INIT(&commits);
6141 TAILQ_INIT(&histedit_cmds);
6142 TAILQ_INIT(&merged_paths);
6144 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6145 switch (ch) {
6146 case 'a':
6147 abort_edit = 1;
6148 break;
6149 case 'c':
6150 continue_edit = 1;
6151 break;
6152 case 'F':
6153 edit_script_path = optarg;
6154 break;
6155 default:
6156 usage_histedit();
6157 /* NOTREACHED */
6161 argc -= optind;
6162 argv += optind;
6164 #ifndef PROFILE
6165 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6166 "unveil", NULL) == -1)
6167 err(1, "pledge");
6168 #endif
6169 if (abort_edit && continue_edit)
6170 usage_histedit();
6171 if (argc != 0)
6172 usage_histedit();
6175 * This command cannot apply unveil(2) in all cases because the
6176 * user may choose to run an editor to edit the histedit script
6177 * and to edit individual commit log messages.
6178 * unveil(2) traverses exec(2); if an editor is used we have to
6179 * apply unveil after edit script and log messages have been written.
6180 * XXX TODO: Make use of unveil(2) where possible.
6183 cwd = getcwd(NULL, 0);
6184 if (cwd == NULL) {
6185 error = got_error_from_errno("getcwd");
6186 goto done;
6188 error = got_worktree_open(&worktree, cwd);
6189 if (error)
6190 goto done;
6192 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6193 NULL);
6194 if (error != NULL)
6195 goto done;
6197 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6198 if (error)
6199 goto done;
6200 if (rebase_in_progress) {
6201 error = got_error(GOT_ERR_REBASING);
6202 goto done;
6205 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6206 if (error)
6207 goto done;
6209 if (edit_in_progress && abort_edit) {
6210 error = got_worktree_histedit_continue(&resume_commit_id,
6211 &tmp_branch, &branch, &base_commit_id, &fileindex,
6212 worktree, repo);
6213 if (error)
6214 goto done;
6215 printf("Switching work tree to %s\n",
6216 got_ref_get_symref_target(branch));
6217 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6218 branch, base_commit_id, update_progress, &did_something);
6219 if (error)
6220 goto done;
6221 printf("Histedit of %s aborted\n",
6222 got_ref_get_symref_target(branch));
6223 goto done; /* nothing else to do */
6224 } else if (abort_edit) {
6225 error = got_error(GOT_ERR_NOT_HISTEDIT);
6226 goto done;
6229 if (continue_edit) {
6230 char *path;
6232 if (!edit_in_progress) {
6233 error = got_error(GOT_ERR_NOT_HISTEDIT);
6234 goto done;
6237 error = got_worktree_get_histedit_script_path(&path, worktree);
6238 if (error)
6239 goto done;
6241 error = histedit_load_list(&histedit_cmds, path, repo);
6242 free(path);
6243 if (error)
6244 goto done;
6246 error = got_worktree_histedit_continue(&resume_commit_id,
6247 &tmp_branch, &branch, &base_commit_id, &fileindex,
6248 worktree, repo);
6249 if (error)
6250 goto done;
6252 error = got_ref_resolve(&head_commit_id, repo, branch);
6253 if (error)
6254 goto done;
6256 error = got_object_open_as_commit(&commit, repo,
6257 head_commit_id);
6258 if (error)
6259 goto done;
6260 parent_ids = got_object_commit_get_parent_ids(commit);
6261 pid = SIMPLEQ_FIRST(parent_ids);
6262 if (pid == NULL) {
6263 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6264 goto done;
6266 error = collect_commits(&commits, head_commit_id, pid->id,
6267 base_commit_id, got_worktree_get_path_prefix(worktree),
6268 GOT_ERR_HISTEDIT_PATH, repo);
6269 got_object_commit_close(commit);
6270 commit = NULL;
6271 if (error)
6272 goto done;
6273 } else {
6274 if (edit_in_progress) {
6275 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6276 goto done;
6279 error = got_ref_open(&branch, repo,
6280 got_worktree_get_head_ref_name(worktree), 0);
6281 if (error != NULL)
6282 goto done;
6284 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6285 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6286 "will not edit commit history of a branch outside "
6287 "the \"refs/heads/\" reference namespace");
6288 goto done;
6291 error = got_ref_resolve(&head_commit_id, repo, branch);
6292 got_ref_close(branch);
6293 branch = NULL;
6294 if (error)
6295 goto done;
6297 error = got_object_open_as_commit(&commit, repo,
6298 head_commit_id);
6299 if (error)
6300 goto done;
6301 parent_ids = got_object_commit_get_parent_ids(commit);
6302 pid = SIMPLEQ_FIRST(parent_ids);
6303 if (pid == NULL) {
6304 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6305 goto done;
6307 error = collect_commits(&commits, head_commit_id, pid->id,
6308 got_worktree_get_base_commit_id(worktree),
6309 got_worktree_get_path_prefix(worktree),
6310 GOT_ERR_HISTEDIT_PATH, repo);
6311 got_object_commit_close(commit);
6312 commit = NULL;
6313 if (error)
6314 goto done;
6316 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6317 &base_commit_id, &fileindex, worktree, repo);
6318 if (error)
6319 goto done;
6321 if (edit_script_path) {
6322 error = histedit_load_list(&histedit_cmds,
6323 edit_script_path, repo);
6324 if (error) {
6325 got_worktree_histedit_abort(worktree, fileindex,
6326 repo, branch, base_commit_id,
6327 update_progress, &did_something);
6328 goto done;
6330 } else {
6331 error = histedit_edit_script(&histedit_cmds, &commits,
6332 repo);
6333 if (error) {
6334 got_worktree_histedit_abort(worktree, fileindex,
6335 repo, branch, base_commit_id,
6336 update_progress, &did_something);
6337 goto done;
6342 error = histedit_save_list(&histedit_cmds, worktree,
6343 repo);
6344 if (error) {
6345 got_worktree_histedit_abort(worktree, fileindex,
6346 repo, branch, base_commit_id,
6347 update_progress, &did_something);
6348 goto done;
6353 error = histedit_check_script(&histedit_cmds, &commits, repo);
6354 if (error)
6355 goto done;
6357 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6358 if (resume_commit_id) {
6359 if (got_object_id_cmp(hle->commit_id,
6360 resume_commit_id) != 0)
6361 continue;
6363 resume_commit_id = NULL;
6364 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6365 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6366 error = histedit_skip_commit(hle, worktree,
6367 repo);
6368 } else {
6369 error = histedit_commit(NULL, worktree,
6370 fileindex, tmp_branch, hle, repo);
6372 if (error)
6373 goto done;
6374 continue;
6377 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6378 error = histedit_skip_commit(hle, worktree, repo);
6379 if (error)
6380 goto done;
6381 continue;
6384 error = got_object_open_as_commit(&commit, repo,
6385 hle->commit_id);
6386 if (error)
6387 goto done;
6388 parent_ids = got_object_commit_get_parent_ids(commit);
6389 pid = SIMPLEQ_FIRST(parent_ids);
6391 error = got_worktree_histedit_merge_files(&merged_paths,
6392 worktree, fileindex, pid->id, hle->commit_id, repo,
6393 rebase_progress, &rebase_status, check_cancelled, NULL);
6394 if (error)
6395 goto done;
6396 got_object_commit_close(commit);
6397 commit = NULL;
6399 if (rebase_status == GOT_STATUS_CONFLICT) {
6400 got_worktree_rebase_pathlist_free(&merged_paths);
6401 break;
6404 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6405 char *id_str;
6406 error = got_object_id_str(&id_str, hle->commit_id);
6407 if (error)
6408 goto done;
6409 printf("Stopping histedit for amending commit %s\n",
6410 id_str);
6411 free(id_str);
6412 got_worktree_rebase_pathlist_free(&merged_paths);
6413 error = got_worktree_histedit_postpone(worktree,
6414 fileindex);
6415 goto done;
6418 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6419 error = histedit_skip_commit(hle, worktree, repo);
6420 if (error)
6421 goto done;
6422 continue;
6425 error = histedit_commit(&merged_paths, worktree, fileindex,
6426 tmp_branch, hle, repo);
6427 got_worktree_rebase_pathlist_free(&merged_paths);
6428 if (error)
6429 goto done;
6432 if (rebase_status == GOT_STATUS_CONFLICT) {
6433 error = got_worktree_histedit_postpone(worktree, fileindex);
6434 if (error)
6435 goto done;
6436 error = got_error_msg(GOT_ERR_CONFLICTS,
6437 "conflicts must be resolved before rebasing can continue");
6438 } else
6439 error = histedit_complete(worktree, fileindex, tmp_branch,
6440 branch, repo);
6441 done:
6442 got_object_id_queue_free(&commits);
6443 histedit_free_list(&histedit_cmds);
6444 free(head_commit_id);
6445 free(base_commit_id);
6446 free(resume_commit_id);
6447 if (commit)
6448 got_object_commit_close(commit);
6449 if (branch)
6450 got_ref_close(branch);
6451 if (tmp_branch)
6452 got_ref_close(tmp_branch);
6453 if (worktree)
6454 got_worktree_close(worktree);
6455 if (repo)
6456 got_repo_close(repo);
6457 return error;
6460 __dead static void
6461 usage_stage(void)
6463 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6464 "[file-path ...]\n",
6465 getprogname());
6466 exit(1);
6469 static const struct got_error *
6470 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6471 const char *path, struct got_object_id *blob_id,
6472 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6474 const struct got_error *err = NULL;
6475 char *id_str = NULL;
6477 if (staged_status != GOT_STATUS_ADD &&
6478 staged_status != GOT_STATUS_MODIFY &&
6479 staged_status != GOT_STATUS_DELETE)
6480 return NULL;
6482 if (staged_status == GOT_STATUS_ADD ||
6483 staged_status == GOT_STATUS_MODIFY)
6484 err = got_object_id_str(&id_str, staged_blob_id);
6485 else
6486 err = got_object_id_str(&id_str, blob_id);
6487 if (err)
6488 return err;
6490 printf("%s %c %s\n", id_str, staged_status, path);
6491 free(id_str);
6492 return NULL;
6495 static const struct got_error *
6496 cmd_stage(int argc, char *argv[])
6498 const struct got_error *error = NULL;
6499 struct got_repository *repo = NULL;
6500 struct got_worktree *worktree = NULL;
6501 char *cwd = NULL;
6502 struct got_pathlist_head paths;
6503 struct got_pathlist_entry *pe;
6504 int ch, list_stage = 0, pflag = 0;
6505 FILE *patch_script_file = NULL;
6506 const char *patch_script_path = NULL;
6507 struct choose_patch_arg cpa;
6509 TAILQ_INIT(&paths);
6511 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6512 switch (ch) {
6513 case 'l':
6514 list_stage = 1;
6515 break;
6516 case 'p':
6517 pflag = 1;
6518 break;
6519 case 'F':
6520 patch_script_path = optarg;
6521 break;
6522 default:
6523 usage_stage();
6524 /* NOTREACHED */
6528 argc -= optind;
6529 argv += optind;
6531 #ifndef PROFILE
6532 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6533 "unveil", NULL) == -1)
6534 err(1, "pledge");
6535 #endif
6536 if (list_stage && (pflag || patch_script_path))
6537 errx(1, "-l option cannot be used with other options");
6538 if (patch_script_path && !pflag)
6539 errx(1, "-F option can only be used together with -p option");
6541 cwd = getcwd(NULL, 0);
6542 if (cwd == NULL) {
6543 error = got_error_from_errno("getcwd");
6544 goto done;
6547 error = got_worktree_open(&worktree, cwd);
6548 if (error)
6549 goto done;
6551 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6552 NULL);
6553 if (error != NULL)
6554 goto done;
6556 if (patch_script_path) {
6557 patch_script_file = fopen(patch_script_path, "r");
6558 if (patch_script_file == NULL) {
6559 error = got_error_from_errno2("fopen",
6560 patch_script_path);
6561 goto done;
6564 error = apply_unveil(got_repo_get_path(repo), 0,
6565 got_worktree_get_root_path(worktree));
6566 if (error)
6567 goto done;
6569 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6570 if (error)
6571 goto done;
6573 if (list_stage)
6574 error = got_worktree_status(worktree, &paths, repo,
6575 print_stage, NULL, check_cancelled, NULL);
6576 else {
6577 cpa.patch_script_file = patch_script_file;
6578 cpa.action = "stage";
6579 error = got_worktree_stage(worktree, &paths,
6580 pflag ? NULL : print_status, NULL,
6581 pflag ? choose_patch : NULL, &cpa, repo);
6583 done:
6584 if (patch_script_file && fclose(patch_script_file) == EOF &&
6585 error == NULL)
6586 error = got_error_from_errno2("fclose", patch_script_path);
6587 if (repo)
6588 got_repo_close(repo);
6589 if (worktree)
6590 got_worktree_close(worktree);
6591 TAILQ_FOREACH(pe, &paths, entry)
6592 free((char *)pe->path);
6593 got_pathlist_free(&paths);
6594 free(cwd);
6595 return error;
6598 __dead static void
6599 usage_unstage(void)
6601 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6602 "[file-path ...]\n",
6603 getprogname());
6604 exit(1);
6608 static const struct got_error *
6609 cmd_unstage(int argc, char *argv[])
6611 const struct got_error *error = NULL;
6612 struct got_repository *repo = NULL;
6613 struct got_worktree *worktree = NULL;
6614 char *cwd = NULL;
6615 struct got_pathlist_head paths;
6616 struct got_pathlist_entry *pe;
6617 int ch, did_something = 0, pflag = 0;
6618 FILE *patch_script_file = NULL;
6619 const char *patch_script_path = NULL;
6620 struct choose_patch_arg cpa;
6622 TAILQ_INIT(&paths);
6624 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6625 switch (ch) {
6626 case 'p':
6627 pflag = 1;
6628 break;
6629 case 'F':
6630 patch_script_path = optarg;
6631 break;
6632 default:
6633 usage_unstage();
6634 /* NOTREACHED */
6638 argc -= optind;
6639 argv += optind;
6641 #ifndef PROFILE
6642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6643 "unveil", NULL) == -1)
6644 err(1, "pledge");
6645 #endif
6646 if (patch_script_path && !pflag)
6647 errx(1, "-F option can only be used together with -p option");
6649 cwd = getcwd(NULL, 0);
6650 if (cwd == NULL) {
6651 error = got_error_from_errno("getcwd");
6652 goto done;
6655 error = got_worktree_open(&worktree, cwd);
6656 if (error)
6657 goto done;
6659 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6660 NULL);
6661 if (error != NULL)
6662 goto done;
6664 if (patch_script_path) {
6665 patch_script_file = fopen(patch_script_path, "r");
6666 if (patch_script_file == NULL) {
6667 error = got_error_from_errno2("fopen",
6668 patch_script_path);
6669 goto done;
6673 error = apply_unveil(got_repo_get_path(repo), 0,
6674 got_worktree_get_root_path(worktree));
6675 if (error)
6676 goto done;
6678 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6679 if (error)
6680 goto done;
6682 cpa.patch_script_file = patch_script_file;
6683 cpa.action = "unstage";
6684 error = got_worktree_unstage(worktree, &paths, update_progress,
6685 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6686 done:
6687 if (patch_script_file && fclose(patch_script_file) == EOF &&
6688 error == NULL)
6689 error = got_error_from_errno2("fclose", patch_script_path);
6690 if (repo)
6691 got_repo_close(repo);
6692 if (worktree)
6693 got_worktree_close(worktree);
6694 TAILQ_FOREACH(pe, &paths, entry)
6695 free((char *)pe->path);
6696 got_pathlist_free(&paths);
6697 free(cwd);
6698 return error;
6701 __dead static void
6702 usage_cat(void)
6704 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6705 "arg1 [arg2 ...]\n", getprogname());
6706 exit(1);
6709 static const struct got_error *
6710 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6712 const struct got_error *err;
6713 struct got_blob_object *blob;
6715 err = got_object_open_as_blob(&blob, repo, id, 8192);
6716 if (err)
6717 return err;
6719 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6720 got_object_blob_close(blob);
6721 return err;
6724 static const struct got_error *
6725 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6727 const struct got_error *err;
6728 struct got_tree_object *tree;
6729 const struct got_tree_entries *entries;
6730 struct got_tree_entry *te;
6732 err = got_object_open_as_tree(&tree, repo, id);
6733 if (err)
6734 return err;
6736 entries = got_object_tree_get_entries(tree);
6737 te = SIMPLEQ_FIRST(&entries->head);
6738 while (te) {
6739 char *id_str;
6740 if (sigint_received || sigpipe_received)
6741 break;
6742 err = got_object_id_str(&id_str, te->id);
6743 if (err)
6744 break;
6745 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6746 free(id_str);
6747 te = SIMPLEQ_NEXT(te, entry);
6750 got_object_tree_close(tree);
6751 return err;
6754 static const struct got_error *
6755 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6757 const struct got_error *err;
6758 struct got_commit_object *commit;
6759 const struct got_object_id_queue *parent_ids;
6760 struct got_object_qid *pid;
6761 char *id_str = NULL;
6762 const char *logmsg = NULL;
6764 err = got_object_open_as_commit(&commit, repo, id);
6765 if (err)
6766 return err;
6768 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6769 if (err)
6770 goto done;
6772 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6773 parent_ids = got_object_commit_get_parent_ids(commit);
6774 fprintf(outfile, "numparents %d\n",
6775 got_object_commit_get_nparents(commit));
6776 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6777 char *pid_str;
6778 err = got_object_id_str(&pid_str, pid->id);
6779 if (err)
6780 goto done;
6781 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6782 free(pid_str);
6784 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6785 got_object_commit_get_author(commit),
6786 got_object_commit_get_author_time(commit));
6788 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6789 got_object_commit_get_author(commit),
6790 got_object_commit_get_committer_time(commit));
6792 logmsg = got_object_commit_get_logmsg_raw(commit);
6793 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6794 fprintf(outfile, "%s", logmsg);
6795 done:
6796 free(id_str);
6797 got_object_commit_close(commit);
6798 return err;
6801 static const struct got_error *
6802 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6804 const struct got_error *err;
6805 struct got_tag_object *tag;
6806 char *id_str = NULL;
6807 const char *tagmsg = NULL;
6809 err = got_object_open_as_tag(&tag, repo, id);
6810 if (err)
6811 return err;
6813 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6814 if (err)
6815 goto done;
6817 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6819 switch (got_object_tag_get_object_type(tag)) {
6820 case GOT_OBJ_TYPE_BLOB:
6821 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6822 GOT_OBJ_LABEL_BLOB);
6823 break;
6824 case GOT_OBJ_TYPE_TREE:
6825 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6826 GOT_OBJ_LABEL_TREE);
6827 break;
6828 case GOT_OBJ_TYPE_COMMIT:
6829 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6830 GOT_OBJ_LABEL_COMMIT);
6831 break;
6832 case GOT_OBJ_TYPE_TAG:
6833 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6834 GOT_OBJ_LABEL_TAG);
6835 break;
6836 default:
6837 break;
6840 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6841 got_object_tag_get_name(tag));
6843 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6844 got_object_tag_get_tagger(tag),
6845 got_object_tag_get_tagger_time(tag));
6847 tagmsg = got_object_tag_get_message(tag);
6848 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6849 fprintf(outfile, "%s", tagmsg);
6850 done:
6851 free(id_str);
6852 got_object_tag_close(tag);
6853 return err;
6856 static const struct got_error *
6857 cmd_cat(int argc, char *argv[])
6859 const struct got_error *error;
6860 struct got_repository *repo = NULL;
6861 struct got_worktree *worktree = NULL;
6862 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6863 const char *commit_id_str = NULL;
6864 struct got_object_id *id = NULL, *commit_id = NULL;
6865 int ch, obj_type, i, force_path = 0;
6867 #ifndef PROFILE
6868 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6869 NULL) == -1)
6870 err(1, "pledge");
6871 #endif
6873 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6874 switch (ch) {
6875 case 'c':
6876 commit_id_str = optarg;
6877 break;
6878 case 'r':
6879 repo_path = realpath(optarg, NULL);
6880 if (repo_path == NULL)
6881 err(1, "-r option");
6882 got_path_strip_trailing_slashes(repo_path);
6883 break;
6884 case 'P':
6885 force_path = 1;
6886 break;
6887 default:
6888 usage_cat();
6889 /* NOTREACHED */
6893 argc -= optind;
6894 argv += optind;
6896 cwd = getcwd(NULL, 0);
6897 if (cwd == NULL) {
6898 error = got_error_from_errno("getcwd");
6899 goto done;
6901 error = got_worktree_open(&worktree, cwd);
6902 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6903 goto done;
6904 if (worktree) {
6905 if (repo_path == NULL) {
6906 repo_path = strdup(
6907 got_worktree_get_repo_path(worktree));
6908 if (repo_path == NULL) {
6909 error = got_error_from_errno("strdup");
6910 goto done;
6915 if (repo_path == NULL) {
6916 repo_path = getcwd(NULL, 0);
6917 if (repo_path == NULL)
6918 return got_error_from_errno("getcwd");
6921 error = got_repo_open(&repo, repo_path, NULL);
6922 free(repo_path);
6923 if (error != NULL)
6924 goto done;
6926 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6927 if (error)
6928 goto done;
6930 if (commit_id_str == NULL)
6931 commit_id_str = GOT_REF_HEAD;
6932 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6933 if (error)
6934 goto done;
6936 for (i = 0; i < argc; i++) {
6937 if (force_path) {
6938 error = got_object_id_by_path(&id, repo, commit_id,
6939 argv[i]);
6940 if (error)
6941 break;
6942 } else {
6943 error = match_object_id(&id, &label, argv[i],
6944 GOT_OBJ_TYPE_ANY, 0, repo);
6945 if (error) {
6946 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6947 error->code != GOT_ERR_NOT_REF)
6948 break;
6949 error = got_object_id_by_path(&id, repo,
6950 commit_id, argv[i]);
6951 if (error)
6952 break;
6956 error = got_object_get_type(&obj_type, repo, id);
6957 if (error)
6958 break;
6960 switch (obj_type) {
6961 case GOT_OBJ_TYPE_BLOB:
6962 error = cat_blob(id, repo, stdout);
6963 break;
6964 case GOT_OBJ_TYPE_TREE:
6965 error = cat_tree(id, repo, stdout);
6966 break;
6967 case GOT_OBJ_TYPE_COMMIT:
6968 error = cat_commit(id, repo, stdout);
6969 break;
6970 case GOT_OBJ_TYPE_TAG:
6971 error = cat_tag(id, repo, stdout);
6972 break;
6973 default:
6974 error = got_error(GOT_ERR_OBJ_TYPE);
6975 break;
6977 if (error)
6978 break;
6979 free(label);
6980 label = NULL;
6981 free(id);
6982 id = NULL;
6985 done:
6986 free(label);
6987 free(id);
6988 free(commit_id);
6989 if (worktree)
6990 got_worktree_close(worktree);
6991 if (repo) {
6992 const struct got_error *repo_error;
6993 repo_error = got_repo_close(repo);
6994 if (error == NULL)
6995 error = repo_error;
6997 return error;