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 error = collect_import_msg(&logmsg, editor, path_dir, refname);
679 if (error)
680 goto done;
683 if (unveil(path_dir, "r") != 0)
684 return got_error_from_errno2("unveil", path_dir);
686 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
687 if (error)
688 goto done;
690 error = got_repo_import(&new_commit_id, path_dir, logmsg,
691 author, &ignores, repo, import_progress, NULL);
692 if (error)
693 goto done;
695 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
696 if (error)
697 goto done;
699 error = got_ref_write(branch_ref, repo);
700 if (error)
701 goto done;
703 error = got_object_id_str(&id_str, new_commit_id);
704 if (error)
705 goto done;
707 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
708 if (error) {
709 if (error->code != GOT_ERR_NOT_REF)
710 goto done;
712 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
713 branch_ref);
714 if (error)
715 goto done;
717 error = got_ref_write(head_ref, repo);
718 if (error)
719 goto done;
722 printf("Created branch %s with commit %s\n",
723 got_ref_get_name(branch_ref), id_str);
724 done:
725 free(repo_path);
726 free(editor);
727 free(refname);
728 free(new_commit_id);
729 free(id_str);
730 free(author);
731 free(gitconfig_path);
732 if (branch_ref)
733 got_ref_close(branch_ref);
734 if (head_ref)
735 got_ref_close(head_ref);
736 return error;
739 __dead static void
740 usage_checkout(void)
742 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
743 "[-p prefix] repository-path [worktree-path]\n", getprogname());
744 exit(1);
747 static const struct got_error *
748 checkout_progress(void *arg, unsigned char status, const char *path)
750 char *worktree_path = arg;
752 /* Base commit bump happens silently. */
753 if (status == GOT_STATUS_BUMP_BASE)
754 return NULL;
756 while (path[0] == '/')
757 path++;
759 printf("%c %s/%s\n", status, worktree_path, path);
760 return NULL;
763 static const struct got_error *
764 check_cancelled(void *arg)
766 if (sigint_received || sigpipe_received)
767 return got_error(GOT_ERR_CANCELLED);
768 return NULL;
771 static const struct got_error *
772 check_linear_ancestry(struct got_object_id *commit_id,
773 struct got_object_id *base_commit_id, struct got_repository *repo)
775 const struct got_error *err = NULL;
776 struct got_object_id *yca_id;
778 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
779 commit_id, base_commit_id, repo, check_cancelled, NULL);
780 if (err)
781 return err;
783 if (yca_id == NULL)
784 return got_error(GOT_ERR_ANCESTRY);
786 /*
787 * Require a straight line of history between the target commit
788 * and the work tree's base commit.
790 * Non-linear situations such as this require a rebase:
792 * (commit) D F (base_commit)
793 * \ /
794 * C E
795 * \ /
796 * B (yca)
797 * |
798 * A
800 * 'got update' only handles linear cases:
801 * Update forwards in time: A (base/yca) - B - C - D (commit)
802 * Update backwards in time: D (base) - C - B - A (commit/yca)
803 */
804 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
805 got_object_id_cmp(base_commit_id, yca_id) != 0)
806 return got_error(GOT_ERR_ANCESTRY);
808 free(yca_id);
809 return NULL;
812 static const struct got_error *
813 check_same_branch(struct got_object_id *commit_id,
814 struct got_reference *head_ref, struct got_object_id *yca_id,
815 struct got_repository *repo)
817 const struct got_error *err = NULL;
818 struct got_commit_graph *graph = NULL;
819 struct got_object_id *head_commit_id = NULL;
820 int is_same_branch = 0;
822 err = got_ref_resolve(&head_commit_id, repo, head_ref);
823 if (err)
824 goto done;
826 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
827 is_same_branch = 1;
828 goto done;
830 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
831 is_same_branch = 1;
832 goto done;
835 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
836 if (err)
837 goto done;
839 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
840 check_cancelled, NULL);
841 if (err)
842 goto done;
844 for (;;) {
845 struct got_object_id *id;
846 err = got_commit_graph_iter_next(&id, graph);
847 if (err) {
848 if (err->code == GOT_ERR_ITER_COMPLETED) {
849 err = NULL;
850 break;
851 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
852 break;
853 err = got_commit_graph_fetch_commits(graph, 1,
854 repo, check_cancelled, NULL);
855 if (err)
856 break;
859 if (id) {
860 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
861 break;
862 if (got_object_id_cmp(id, commit_id) == 0) {
863 is_same_branch = 1;
864 break;
868 done:
869 if (graph)
870 got_commit_graph_close(graph);
871 free(head_commit_id);
872 if (!err && !is_same_branch)
873 err = got_error(GOT_ERR_ANCESTRY);
874 return err;
877 static const struct got_error *
878 resolve_commit_arg(struct got_object_id **commit_id,
879 const char *commit_id_arg, struct got_repository *repo)
881 const struct got_error *err;
882 struct got_reference *ref;
883 struct got_tag_object *tag;
885 err = got_repo_object_match_tag(&tag, commit_id_arg,
886 GOT_OBJ_TYPE_COMMIT, repo);
887 if (err == NULL) {
888 *commit_id = got_object_id_dup(
889 got_object_tag_get_object_id(tag));
890 if (*commit_id == NULL)
891 err = got_error_from_errno("got_object_id_dup");
892 got_object_tag_close(tag);
893 return err;
894 } else if (err->code != GOT_ERR_NO_OBJ)
895 return err;
897 err = got_ref_open(&ref, repo, commit_id_arg, 0);
898 if (err == NULL) {
899 err = got_ref_resolve(commit_id, repo, ref);
900 got_ref_close(ref);
901 } else {
902 if (err->code != GOT_ERR_NOT_REF)
903 return err;
904 err = got_repo_match_object_id_prefix(commit_id,
905 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
907 return err;
910 static const struct got_error *
911 cmd_checkout(int argc, char *argv[])
913 const struct got_error *error = NULL;
914 struct got_repository *repo = NULL;
915 struct got_reference *head_ref = NULL;
916 struct got_worktree *worktree = NULL;
917 char *repo_path = NULL;
918 char *worktree_path = NULL;
919 const char *path_prefix = "";
920 const char *branch_name = GOT_REF_HEAD;
921 char *commit_id_str = NULL;
922 int ch, same_path_prefix;
923 struct got_pathlist_head paths;
925 TAILQ_INIT(&paths);
927 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
928 switch (ch) {
929 case 'b':
930 branch_name = optarg;
931 break;
932 case 'c':
933 commit_id_str = strdup(optarg);
934 if (commit_id_str == NULL)
935 return got_error_from_errno("strdup");
936 break;
937 case 'p':
938 path_prefix = optarg;
939 break;
940 default:
941 usage_checkout();
942 /* NOTREACHED */
946 argc -= optind;
947 argv += optind;
949 #ifndef PROFILE
950 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
951 "unveil", NULL) == -1)
952 err(1, "pledge");
953 #endif
954 if (argc == 1) {
955 char *cwd, *base, *dotgit;
956 repo_path = realpath(argv[0], NULL);
957 if (repo_path == NULL)
958 return got_error_from_errno2("realpath", argv[0]);
959 cwd = getcwd(NULL, 0);
960 if (cwd == NULL) {
961 error = got_error_from_errno("getcwd");
962 goto done;
964 if (path_prefix[0]) {
965 base = basename(path_prefix);
966 if (base == NULL) {
967 error = got_error_from_errno2("basename",
968 path_prefix);
969 goto done;
971 } else {
972 base = basename(repo_path);
973 if (base == NULL) {
974 error = got_error_from_errno2("basename",
975 repo_path);
976 goto done;
979 dotgit = strstr(base, ".git");
980 if (dotgit)
981 *dotgit = '\0';
982 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
983 error = got_error_from_errno("asprintf");
984 free(cwd);
985 goto done;
987 free(cwd);
988 } else if (argc == 2) {
989 repo_path = realpath(argv[0], NULL);
990 if (repo_path == NULL) {
991 error = got_error_from_errno2("realpath", argv[0]);
992 goto done;
994 worktree_path = realpath(argv[1], NULL);
995 if (worktree_path == NULL) {
996 if (errno != ENOENT) {
997 error = got_error_from_errno2("realpath",
998 argv[1]);
999 goto done;
1001 worktree_path = strdup(argv[1]);
1002 if (worktree_path == NULL) {
1003 error = got_error_from_errno("strdup");
1004 goto done;
1007 } else
1008 usage_checkout();
1010 got_path_strip_trailing_slashes(repo_path);
1011 got_path_strip_trailing_slashes(worktree_path);
1013 error = got_repo_open(&repo, repo_path, NULL);
1014 if (error != NULL)
1015 goto done;
1017 /* Pre-create work tree path for unveil(2) */
1018 error = got_path_mkdir(worktree_path);
1019 if (error) {
1020 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1021 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1022 goto done;
1023 if (!got_path_dir_is_empty(worktree_path)) {
1024 error = got_error_path(worktree_path,
1025 GOT_ERR_DIR_NOT_EMPTY);
1026 goto done;
1030 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1031 if (error)
1032 goto done;
1034 error = got_ref_open(&head_ref, repo, branch_name, 0);
1035 if (error != NULL)
1036 goto done;
1038 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1039 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1040 goto done;
1042 error = got_worktree_open(&worktree, worktree_path);
1043 if (error != NULL)
1044 goto done;
1046 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1047 path_prefix);
1048 if (error != NULL)
1049 goto done;
1050 if (!same_path_prefix) {
1051 error = got_error(GOT_ERR_PATH_PREFIX);
1052 goto done;
1055 if (commit_id_str) {
1056 struct got_object_id *commit_id;
1057 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1058 if (error)
1059 goto done;
1060 error = check_linear_ancestry(commit_id,
1061 got_worktree_get_base_commit_id(worktree), repo);
1062 if (error != NULL) {
1063 free(commit_id);
1064 goto done;
1066 error = check_same_branch(commit_id, head_ref, NULL, repo);
1067 if (error)
1068 goto done;
1069 error = got_worktree_set_base_commit_id(worktree, repo,
1070 commit_id);
1071 free(commit_id);
1072 if (error)
1073 goto done;
1076 error = got_pathlist_append(&paths, "", NULL);
1077 if (error)
1078 goto done;
1079 error = got_worktree_checkout_files(worktree, &paths, repo,
1080 checkout_progress, worktree_path, check_cancelled, NULL);
1081 if (error != NULL)
1082 goto done;
1084 printf("Now shut up and hack\n");
1086 done:
1087 got_pathlist_free(&paths);
1088 free(commit_id_str);
1089 free(repo_path);
1090 free(worktree_path);
1091 return error;
1094 __dead static void
1095 usage_update(void)
1097 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1098 getprogname());
1099 exit(1);
1102 static const struct got_error *
1103 update_progress(void *arg, unsigned char status, const char *path)
1105 int *did_something = arg;
1107 if (status == GOT_STATUS_EXISTS)
1108 return NULL;
1110 *did_something = 1;
1112 /* Base commit bump happens silently. */
1113 if (status == GOT_STATUS_BUMP_BASE)
1114 return NULL;
1116 while (path[0] == '/')
1117 path++;
1118 printf("%c %s\n", status, path);
1119 return NULL;
1122 static const struct got_error *
1123 switch_head_ref(struct got_reference *head_ref,
1124 struct got_object_id *commit_id, struct got_worktree *worktree,
1125 struct got_repository *repo)
1127 const struct got_error *err = NULL;
1128 char *base_id_str;
1129 int ref_has_moved = 0;
1131 /* Trivial case: switching between two different references. */
1132 if (strcmp(got_ref_get_name(head_ref),
1133 got_worktree_get_head_ref_name(worktree)) != 0) {
1134 printf("Switching work tree from %s to %s\n",
1135 got_worktree_get_head_ref_name(worktree),
1136 got_ref_get_name(head_ref));
1137 return got_worktree_set_head_ref(worktree, head_ref);
1140 err = check_linear_ancestry(commit_id,
1141 got_worktree_get_base_commit_id(worktree), repo);
1142 if (err) {
1143 if (err->code != GOT_ERR_ANCESTRY)
1144 return err;
1145 ref_has_moved = 1;
1147 if (!ref_has_moved)
1148 return NULL;
1150 /* Switching to a rebased branch with the same reference name. */
1151 err = got_object_id_str(&base_id_str,
1152 got_worktree_get_base_commit_id(worktree));
1153 if (err)
1154 return err;
1155 printf("Reference %s now points at a different branch\n",
1156 got_worktree_get_head_ref_name(worktree));
1157 printf("Switching work tree from %s to %s\n", base_id_str,
1158 got_worktree_get_head_ref_name(worktree));
1159 return NULL;
1162 static const struct got_error *
1163 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1165 const struct got_error *err;
1166 int in_progress;
1168 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1169 if (err)
1170 return err;
1171 if (in_progress)
1172 return got_error(GOT_ERR_REBASING);
1174 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1175 if (err)
1176 return err;
1177 if (in_progress)
1178 return got_error(GOT_ERR_HISTEDIT_BUSY);
1180 return NULL;
1183 static const struct got_error *
1184 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1185 char *argv[], struct got_worktree *worktree)
1187 const struct got_error *err = NULL;
1188 char *path;
1189 int i;
1191 if (argc == 0) {
1192 path = strdup("");
1193 if (path == NULL)
1194 return got_error_from_errno("strdup");
1195 return got_pathlist_append(paths, path, NULL);
1198 for (i = 0; i < argc; i++) {
1199 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1200 if (err)
1201 break;
1202 err = got_pathlist_append(paths, path, NULL);
1203 if (err) {
1204 free(path);
1205 break;
1209 return err;
1212 static const struct got_error *
1213 cmd_update(int argc, char *argv[])
1215 const struct got_error *error = NULL;
1216 struct got_repository *repo = NULL;
1217 struct got_worktree *worktree = NULL;
1218 char *worktree_path = NULL;
1219 struct got_object_id *commit_id = NULL;
1220 char *commit_id_str = NULL;
1221 const char *branch_name = NULL;
1222 struct got_reference *head_ref = NULL;
1223 struct got_pathlist_head paths;
1224 struct got_pathlist_entry *pe;
1225 int ch, did_something = 0;
1227 TAILQ_INIT(&paths);
1229 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1230 switch (ch) {
1231 case 'b':
1232 branch_name = optarg;
1233 break;
1234 case 'c':
1235 commit_id_str = strdup(optarg);
1236 if (commit_id_str == NULL)
1237 return got_error_from_errno("strdup");
1238 break;
1239 default:
1240 usage_update();
1241 /* NOTREACHED */
1245 argc -= optind;
1246 argv += optind;
1248 #ifndef PROFILE
1249 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1250 "unveil", NULL) == -1)
1251 err(1, "pledge");
1252 #endif
1253 worktree_path = getcwd(NULL, 0);
1254 if (worktree_path == NULL) {
1255 error = got_error_from_errno("getcwd");
1256 goto done;
1258 error = got_worktree_open(&worktree, worktree_path);
1259 if (error)
1260 goto done;
1262 error = check_rebase_or_histedit_in_progress(worktree);
1263 if (error)
1264 goto done;
1266 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1267 NULL);
1268 if (error != NULL)
1269 goto done;
1271 error = apply_unveil(got_repo_get_path(repo), 0,
1272 got_worktree_get_root_path(worktree));
1273 if (error)
1274 goto done;
1276 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1277 if (error)
1278 goto done;
1280 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1281 got_worktree_get_head_ref_name(worktree), 0);
1282 if (error != NULL)
1283 goto done;
1284 if (commit_id_str == NULL) {
1285 error = got_ref_resolve(&commit_id, repo, head_ref);
1286 if (error != NULL)
1287 goto done;
1288 error = got_object_id_str(&commit_id_str, commit_id);
1289 if (error != NULL)
1290 goto done;
1291 } else {
1292 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1293 free(commit_id_str);
1294 commit_id_str = NULL;
1295 if (error)
1296 goto done;
1297 error = got_object_id_str(&commit_id_str, commit_id);
1298 if (error)
1299 goto done;
1302 if (branch_name) {
1303 struct got_object_id *head_commit_id;
1304 TAILQ_FOREACH(pe, &paths, entry) {
1305 if (pe->path_len == 0)
1306 continue;
1307 error = got_error_msg(GOT_ERR_BAD_PATH,
1308 "switching between branches requires that "
1309 "the entire work tree gets updated");
1310 goto done;
1312 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1313 if (error)
1314 goto done;
1315 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1316 free(head_commit_id);
1317 if (error != NULL)
1318 goto done;
1319 error = check_same_branch(commit_id, head_ref, NULL, repo);
1320 if (error)
1321 goto done;
1322 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1323 if (error)
1324 goto done;
1325 } else {
1326 error = check_linear_ancestry(commit_id,
1327 got_worktree_get_base_commit_id(worktree), repo);
1328 if (error != NULL) {
1329 if (error->code == GOT_ERR_ANCESTRY)
1330 error = got_error(GOT_ERR_BRANCH_MOVED);
1331 goto done;
1333 error = check_same_branch(commit_id, head_ref, NULL, repo);
1334 if (error)
1335 goto done;
1338 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1339 commit_id) != 0) {
1340 error = got_worktree_set_base_commit_id(worktree, repo,
1341 commit_id);
1342 if (error)
1343 goto done;
1346 error = got_worktree_checkout_files(worktree, &paths, repo,
1347 update_progress, &did_something, check_cancelled, NULL);
1348 if (error != NULL)
1349 goto done;
1351 if (did_something)
1352 printf("Updated to commit %s\n", commit_id_str);
1353 else
1354 printf("Already up-to-date\n");
1355 done:
1356 free(worktree_path);
1357 TAILQ_FOREACH(pe, &paths, entry)
1358 free((char *)pe->path);
1359 got_pathlist_free(&paths);
1360 free(commit_id);
1361 free(commit_id_str);
1362 return error;
1365 static const struct got_error *
1366 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1367 const char *path, int diff_context, struct got_repository *repo)
1369 const struct got_error *err = NULL;
1370 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1372 if (blob_id1) {
1373 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1374 if (err)
1375 goto done;
1378 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1379 if (err)
1380 goto done;
1382 while (path[0] == '/')
1383 path++;
1384 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1385 stdout);
1386 done:
1387 if (blob1)
1388 got_object_blob_close(blob1);
1389 got_object_blob_close(blob2);
1390 return err;
1393 static const struct got_error *
1394 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1395 const char *path, int diff_context, struct got_repository *repo)
1397 const struct got_error *err = NULL;
1398 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1399 struct got_diff_blob_output_unidiff_arg arg;
1401 if (tree_id1) {
1402 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1403 if (err)
1404 goto done;
1407 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1408 if (err)
1409 goto done;
1411 arg.diff_context = diff_context;
1412 arg.outfile = stdout;
1413 while (path[0] == '/')
1414 path++;
1415 err = got_diff_tree(tree1, tree2, path, path, repo,
1416 got_diff_blob_output_unidiff, &arg, 1);
1417 done:
1418 if (tree1)
1419 got_object_tree_close(tree1);
1420 got_object_tree_close(tree2);
1421 return err;
1424 static const struct got_error *
1425 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1426 const char *path, int diff_context, struct got_repository *repo)
1428 const struct got_error *err = NULL;
1429 struct got_commit_object *pcommit = NULL;
1430 char *id_str1 = NULL, *id_str2 = NULL;
1431 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1432 struct got_object_qid *qid;
1434 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1435 if (qid != NULL) {
1436 err = got_object_open_as_commit(&pcommit, repo,
1437 qid->id);
1438 if (err)
1439 return err;
1442 if (path && path[0] != '\0') {
1443 int obj_type;
1444 err = got_object_id_by_path(&obj_id2, repo, id, path);
1445 if (err)
1446 goto done;
1447 err = got_object_id_str(&id_str2, obj_id2);
1448 if (err) {
1449 free(obj_id2);
1450 goto done;
1452 if (pcommit) {
1453 err = got_object_id_by_path(&obj_id1, repo,
1454 qid->id, path);
1455 if (err) {
1456 free(obj_id2);
1457 goto done;
1459 err = got_object_id_str(&id_str1, obj_id1);
1460 if (err) {
1461 free(obj_id2);
1462 goto done;
1465 err = got_object_get_type(&obj_type, repo, obj_id2);
1466 if (err) {
1467 free(obj_id2);
1468 goto done;
1470 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1471 switch (obj_type) {
1472 case GOT_OBJ_TYPE_BLOB:
1473 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1474 repo);
1475 break;
1476 case GOT_OBJ_TYPE_TREE:
1477 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1478 repo);
1479 break;
1480 default:
1481 err = got_error(GOT_ERR_OBJ_TYPE);
1482 break;
1484 free(obj_id1);
1485 free(obj_id2);
1486 } else {
1487 obj_id2 = got_object_commit_get_tree_id(commit);
1488 err = got_object_id_str(&id_str2, obj_id2);
1489 if (err)
1490 goto done;
1491 obj_id1 = got_object_commit_get_tree_id(pcommit);
1492 err = got_object_id_str(&id_str1, obj_id1);
1493 if (err)
1494 goto done;
1495 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1496 err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1499 done:
1500 free(id_str1);
1501 free(id_str2);
1502 if (pcommit)
1503 got_object_commit_close(pcommit);
1504 return err;
1507 static char *
1508 get_datestr(time_t *time, char *datebuf)
1510 struct tm mytm, *tm;
1511 char *p, *s;
1513 tm = gmtime_r(time, &mytm);
1514 if (tm == NULL)
1515 return NULL;
1516 s = asctime_r(tm, datebuf);
1517 if (s == NULL)
1518 return NULL;
1519 p = strchr(s, '\n');
1520 if (p)
1521 *p = '\0';
1522 return s;
1525 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1527 static const struct got_error *
1528 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1529 struct got_repository *repo, const char *path, int show_patch,
1530 int diff_context, struct got_reflist_head *refs)
1532 const struct got_error *err = NULL;
1533 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1534 char datebuf[26];
1535 time_t committer_time;
1536 const char *author, *committer;
1537 char *refs_str = NULL;
1538 struct got_reflist_entry *re;
1540 SIMPLEQ_FOREACH(re, refs, entry) {
1541 char *s;
1542 const char *name;
1543 struct got_tag_object *tag = NULL;
1544 int cmp;
1546 name = got_ref_get_name(re->ref);
1547 if (strcmp(name, GOT_REF_HEAD) == 0)
1548 continue;
1549 if (strncmp(name, "refs/", 5) == 0)
1550 name += 5;
1551 if (strncmp(name, "got/", 4) == 0)
1552 continue;
1553 if (strncmp(name, "heads/", 6) == 0)
1554 name += 6;
1555 if (strncmp(name, "remotes/", 8) == 0)
1556 name += 8;
1557 if (strncmp(name, "tags/", 5) == 0) {
1558 err = got_object_open_as_tag(&tag, repo, re->id);
1559 if (err) {
1560 if (err->code != GOT_ERR_OBJ_TYPE)
1561 return err;
1562 /* Ref points at something other than a tag. */
1563 err = NULL;
1564 tag = NULL;
1567 cmp = got_object_id_cmp(tag ?
1568 got_object_tag_get_object_id(tag) : re->id, id);
1569 if (tag)
1570 got_object_tag_close(tag);
1571 if (cmp != 0)
1572 continue;
1573 s = refs_str;
1574 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1575 name) == -1) {
1576 err = got_error_from_errno("asprintf");
1577 free(s);
1578 return err;
1580 free(s);
1582 err = got_object_id_str(&id_str, id);
1583 if (err)
1584 return err;
1586 printf(GOT_COMMIT_SEP_STR);
1587 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1588 refs_str ? refs_str : "", refs_str ? ")" : "");
1589 free(id_str);
1590 id_str = NULL;
1591 free(refs_str);
1592 refs_str = NULL;
1593 printf("from: %s\n", got_object_commit_get_author(commit));
1594 committer_time = got_object_commit_get_committer_time(commit);
1595 datestr = get_datestr(&committer_time, datebuf);
1596 if (datestr)
1597 printf("date: %s UTC\n", datestr);
1598 author = got_object_commit_get_author(commit);
1599 committer = got_object_commit_get_committer(commit);
1600 if (strcmp(author, committer) != 0)
1601 printf("via: %s\n", committer);
1602 if (got_object_commit_get_nparents(commit) > 1) {
1603 const struct got_object_id_queue *parent_ids;
1604 struct got_object_qid *qid;
1605 int n = 1;
1606 parent_ids = got_object_commit_get_parent_ids(commit);
1607 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1608 err = got_object_id_str(&id_str, qid->id);
1609 if (err)
1610 return err;
1611 printf("parent %d: %s\n", n++, id_str);
1612 free(id_str);
1616 err = got_object_commit_get_logmsg(&logmsg0, commit);
1617 if (err)
1618 return err;
1620 logmsg = logmsg0;
1621 do {
1622 line = strsep(&logmsg, "\n");
1623 if (line)
1624 printf(" %s\n", line);
1625 } while (line);
1626 free(logmsg0);
1628 if (show_patch) {
1629 err = print_patch(commit, id, path, diff_context, repo);
1630 if (err == 0)
1631 printf("\n");
1634 if (fflush(stdout) != 0 && err == NULL)
1635 err = got_error_from_errno("fflush");
1636 return err;
1639 static const struct got_error *
1640 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1641 char *path, int show_patch, int diff_context, int limit,
1642 int first_parent_traversal, struct got_reflist_head *refs)
1644 const struct got_error *err;
1645 struct got_commit_graph *graph;
1647 err = got_commit_graph_open(&graph, root_id, path,
1648 first_parent_traversal, repo);
1649 if (err)
1650 return err;
1651 err = got_commit_graph_iter_start(graph, root_id, repo,
1652 check_cancelled, NULL);
1653 if (err)
1654 goto done;
1655 for (;;) {
1656 struct got_commit_object *commit;
1657 struct got_object_id *id;
1659 if (sigint_received || sigpipe_received)
1660 break;
1662 err = got_commit_graph_iter_next(&id, graph);
1663 if (err) {
1664 if (err->code == GOT_ERR_ITER_COMPLETED) {
1665 err = NULL;
1666 break;
1668 if (err->code != GOT_ERR_ITER_NEED_MORE)
1669 break;
1670 err = got_commit_graph_fetch_commits(graph, 1, repo,
1671 check_cancelled, NULL);
1672 if (err)
1673 break;
1674 else
1675 continue;
1677 if (id == NULL)
1678 break;
1680 err = got_object_open_as_commit(&commit, repo, id);
1681 if (err)
1682 break;
1683 err = print_commit(commit, id, repo, path, show_patch,
1684 diff_context, refs);
1685 got_object_commit_close(commit);
1686 if (err || (limit && --limit == 0))
1687 break;
1689 done:
1690 got_commit_graph_close(graph);
1691 return err;
1694 __dead static void
1695 usage_log(void)
1697 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1698 "[-r repository-path] [path]\n", getprogname());
1699 exit(1);
1702 static int
1703 get_default_log_limit(void)
1705 const char *got_default_log_limit;
1706 long long n;
1707 const char *errstr;
1709 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1710 if (got_default_log_limit == NULL)
1711 return 0;
1712 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1713 if (errstr != NULL)
1714 return 0;
1715 return n;
1718 static const struct got_error *
1719 cmd_log(int argc, char *argv[])
1721 const struct got_error *error;
1722 struct got_repository *repo = NULL;
1723 struct got_worktree *worktree = NULL;
1724 struct got_commit_object *commit = NULL;
1725 struct got_object_id *id = NULL;
1726 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1727 char *start_commit = NULL;
1728 int diff_context = 3, ch;
1729 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1730 const char *errstr;
1731 struct got_reflist_head refs;
1733 SIMPLEQ_INIT(&refs);
1735 #ifndef PROFILE
1736 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1737 NULL)
1738 == -1)
1739 err(1, "pledge");
1740 #endif
1742 limit = get_default_log_limit();
1744 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1745 switch (ch) {
1746 case 'p':
1747 show_patch = 1;
1748 break;
1749 case 'c':
1750 start_commit = optarg;
1751 break;
1752 case 'C':
1753 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1754 &errstr);
1755 if (errstr != NULL)
1756 err(1, "-C option %s", errstr);
1757 break;
1758 case 'l':
1759 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1760 if (errstr != NULL)
1761 err(1, "-l option %s", errstr);
1762 break;
1763 case 'f':
1764 first_parent_traversal = 1;
1765 break;
1766 case 'r':
1767 repo_path = realpath(optarg, NULL);
1768 if (repo_path == NULL)
1769 err(1, "-r option");
1770 got_path_strip_trailing_slashes(repo_path);
1771 break;
1772 default:
1773 usage_log();
1774 /* NOTREACHED */
1778 argc -= optind;
1779 argv += optind;
1781 cwd = getcwd(NULL, 0);
1782 if (cwd == NULL) {
1783 error = got_error_from_errno("getcwd");
1784 goto done;
1787 error = got_worktree_open(&worktree, cwd);
1788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1789 goto done;
1790 error = NULL;
1792 if (argc == 0) {
1793 path = strdup("");
1794 if (path == NULL) {
1795 error = got_error_from_errno("strdup");
1796 goto done;
1798 } else if (argc == 1) {
1799 if (worktree) {
1800 error = got_worktree_resolve_path(&path, worktree,
1801 argv[0]);
1802 if (error)
1803 goto done;
1804 } else {
1805 path = strdup(argv[0]);
1806 if (path == NULL) {
1807 error = got_error_from_errno("strdup");
1808 goto done;
1811 } else
1812 usage_log();
1814 if (repo_path == NULL) {
1815 repo_path = worktree ?
1816 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1818 if (repo_path == NULL) {
1819 error = got_error_from_errno("strdup");
1820 goto done;
1823 error = got_repo_open(&repo, repo_path, NULL);
1824 if (error != NULL)
1825 goto done;
1827 error = apply_unveil(got_repo_get_path(repo), 1,
1828 worktree ? got_worktree_get_root_path(worktree) : NULL);
1829 if (error)
1830 goto done;
1832 if (start_commit == NULL) {
1833 struct got_reference *head_ref;
1834 error = got_ref_open(&head_ref, repo,
1835 worktree ? got_worktree_get_head_ref_name(worktree)
1836 : GOT_REF_HEAD, 0);
1837 if (error != NULL)
1838 return error;
1839 error = got_ref_resolve(&id, repo, head_ref);
1840 got_ref_close(head_ref);
1841 if (error != NULL)
1842 return error;
1843 error = got_object_open_as_commit(&commit, repo, id);
1844 } else {
1845 struct got_reference *ref;
1846 error = got_ref_open(&ref, repo, start_commit, 0);
1847 if (error == NULL) {
1848 int obj_type;
1849 error = got_ref_resolve(&id, repo, ref);
1850 got_ref_close(ref);
1851 if (error != NULL)
1852 goto done;
1853 error = got_object_get_type(&obj_type, repo, id);
1854 if (error != NULL)
1855 goto done;
1856 if (obj_type == GOT_OBJ_TYPE_TAG) {
1857 struct got_tag_object *tag;
1858 error = got_object_open_as_tag(&tag, repo, id);
1859 if (error != NULL)
1860 goto done;
1861 if (got_object_tag_get_object_type(tag) !=
1862 GOT_OBJ_TYPE_COMMIT) {
1863 got_object_tag_close(tag);
1864 error = got_error(GOT_ERR_OBJ_TYPE);
1865 goto done;
1867 free(id);
1868 id = got_object_id_dup(
1869 got_object_tag_get_object_id(tag));
1870 if (id == NULL)
1871 error = got_error_from_errno(
1872 "got_object_id_dup");
1873 got_object_tag_close(tag);
1874 if (error)
1875 goto done;
1876 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1877 error = got_error(GOT_ERR_OBJ_TYPE);
1878 goto done;
1880 error = got_object_open_as_commit(&commit, repo, id);
1881 if (error != NULL)
1882 goto done;
1884 if (commit == NULL) {
1885 error = got_repo_match_object_id_prefix(&id,
1886 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1887 if (error != NULL)
1888 return error;
1891 if (error != NULL)
1892 goto done;
1894 if (worktree) {
1895 const char *prefix = got_worktree_get_path_prefix(worktree);
1896 char *p;
1897 if (asprintf(&p, "%s%s%s", prefix,
1898 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1899 error = got_error_from_errno("asprintf");
1900 goto done;
1902 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1903 free(p);
1904 } else
1905 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1906 if (error != NULL)
1907 goto done;
1908 if (in_repo_path) {
1909 free(path);
1910 path = in_repo_path;
1913 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1914 if (error)
1915 goto done;
1917 error = print_commits(id, repo, path, show_patch,
1918 diff_context, limit, first_parent_traversal, &refs);
1919 done:
1920 free(path);
1921 free(repo_path);
1922 free(cwd);
1923 free(id);
1924 if (worktree)
1925 got_worktree_close(worktree);
1926 if (repo) {
1927 const struct got_error *repo_error;
1928 repo_error = got_repo_close(repo);
1929 if (error == NULL)
1930 error = repo_error;
1932 got_ref_list_free(&refs);
1933 return error;
1936 __dead static void
1937 usage_diff(void)
1939 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1940 "[object1 object2 | path]\n", getprogname());
1941 exit(1);
1944 struct print_diff_arg {
1945 struct got_repository *repo;
1946 struct got_worktree *worktree;
1947 int diff_context;
1948 const char *id_str;
1949 int header_shown;
1950 int diff_staged;
1953 static const struct got_error *
1954 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1955 const char *path, struct got_object_id *blob_id,
1956 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1958 struct print_diff_arg *a = arg;
1959 const struct got_error *err = NULL;
1960 struct got_blob_object *blob1 = NULL;
1961 FILE *f2 = NULL;
1962 char *abspath = NULL, *label1 = NULL;
1963 struct stat sb;
1965 if (a->diff_staged) {
1966 if (staged_status != GOT_STATUS_MODIFY &&
1967 staged_status != GOT_STATUS_ADD &&
1968 staged_status != GOT_STATUS_DELETE)
1969 return NULL;
1970 } else {
1971 if (staged_status == GOT_STATUS_DELETE)
1972 return NULL;
1973 if (status == GOT_STATUS_NONEXISTENT)
1974 return got_error_set_errno(ENOENT, path);
1975 if (status != GOT_STATUS_MODIFY &&
1976 status != GOT_STATUS_ADD &&
1977 status != GOT_STATUS_DELETE &&
1978 status != GOT_STATUS_CONFLICT)
1979 return NULL;
1982 if (!a->header_shown) {
1983 printf("diff %s %s%s\n", a->id_str,
1984 got_worktree_get_root_path(a->worktree),
1985 a->diff_staged ? " (staged changes)" : "");
1986 a->header_shown = 1;
1989 if (a->diff_staged) {
1990 const char *label1 = NULL, *label2 = NULL;
1991 switch (staged_status) {
1992 case GOT_STATUS_MODIFY:
1993 label1 = path;
1994 label2 = path;
1995 break;
1996 case GOT_STATUS_ADD:
1997 label2 = path;
1998 break;
1999 case GOT_STATUS_DELETE:
2000 label1 = path;
2001 break;
2002 default:
2003 return got_error(GOT_ERR_FILE_STATUS);
2005 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2006 label1, label2, a->diff_context, a->repo, stdout);
2009 if (staged_status == GOT_STATUS_ADD ||
2010 staged_status == GOT_STATUS_MODIFY) {
2011 char *id_str;
2012 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2013 8192);
2014 if (err)
2015 goto done;
2016 err = got_object_id_str(&id_str, staged_blob_id);
2017 if (err)
2018 goto done;
2019 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2020 err = got_error_from_errno("asprintf");
2021 free(id_str);
2022 goto done;
2024 free(id_str);
2025 } else if (status != GOT_STATUS_ADD) {
2026 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2027 if (err)
2028 goto done;
2031 if (status != GOT_STATUS_DELETE) {
2032 if (asprintf(&abspath, "%s/%s",
2033 got_worktree_get_root_path(a->worktree), path) == -1) {
2034 err = got_error_from_errno("asprintf");
2035 goto done;
2038 f2 = fopen(abspath, "r");
2039 if (f2 == NULL) {
2040 err = got_error_from_errno2("fopen", abspath);
2041 goto done;
2043 if (lstat(abspath, &sb) == -1) {
2044 err = got_error_from_errno2("lstat", abspath);
2045 goto done;
2047 } else
2048 sb.st_size = 0;
2050 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2051 a->diff_context, stdout);
2052 done:
2053 if (blob1)
2054 got_object_blob_close(blob1);
2055 if (f2 && fclose(f2) != 0 && err == NULL)
2056 err = got_error_from_errno("fclose");
2057 free(abspath);
2058 return err;
2061 static const struct got_error *
2062 match_object_id(struct got_object_id **id, char **label,
2063 const char *id_str, int obj_type, int resolve_tags,
2064 struct got_repository *repo)
2066 const struct got_error *err;
2067 struct got_tag_object *tag;
2068 struct got_reference *ref = NULL;
2070 *id = NULL;
2071 *label = NULL;
2073 if (resolve_tags) {
2074 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2075 repo);
2076 if (err == NULL) {
2077 *id = got_object_id_dup(
2078 got_object_tag_get_object_id(tag));
2079 if (*id == NULL)
2080 err = got_error_from_errno("got_object_id_dup");
2081 else if (asprintf(label, "refs/tags/%s",
2082 got_object_tag_get_name(tag)) == -1) {
2083 err = got_error_from_errno("asprintf");
2084 free(*id);
2085 *id = NULL;
2087 got_object_tag_close(tag);
2088 return err;
2089 } else if (err->code != GOT_ERR_NO_OBJ)
2090 return err;
2093 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2094 if (err) {
2095 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2096 return err;
2097 err = got_ref_open(&ref, repo, id_str, 0);
2098 if (err != NULL)
2099 goto done;
2100 *label = strdup(got_ref_get_name(ref));
2101 if (*label == NULL) {
2102 err = got_error_from_errno("strdup");
2103 goto done;
2105 err = got_ref_resolve(id, repo, ref);
2106 } else {
2107 err = got_object_id_str(label, *id);
2108 if (*label == NULL) {
2109 err = got_error_from_errno("strdup");
2110 goto done;
2113 done:
2114 if (ref)
2115 got_ref_close(ref);
2116 return err;
2120 static const struct got_error *
2121 cmd_diff(int argc, char *argv[])
2123 const struct got_error *error;
2124 struct got_repository *repo = NULL;
2125 struct got_worktree *worktree = NULL;
2126 char *cwd = NULL, *repo_path = NULL;
2127 struct got_object_id *id1 = NULL, *id2 = NULL;
2128 const char *id_str1 = NULL, *id_str2 = NULL;
2129 char *label1 = NULL, *label2 = NULL;
2130 int type1, type2;
2131 int diff_context = 3, diff_staged = 0, ch;
2132 const char *errstr;
2133 char *path = NULL;
2135 #ifndef PROFILE
2136 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2137 NULL) == -1)
2138 err(1, "pledge");
2139 #endif
2141 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2142 switch (ch) {
2143 case 'C':
2144 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2145 if (errstr != NULL)
2146 err(1, "-C option %s", errstr);
2147 break;
2148 case 'r':
2149 repo_path = realpath(optarg, NULL);
2150 if (repo_path == NULL)
2151 err(1, "-r option");
2152 got_path_strip_trailing_slashes(repo_path);
2153 break;
2154 case 's':
2155 diff_staged = 1;
2156 break;
2157 default:
2158 usage_diff();
2159 /* NOTREACHED */
2163 argc -= optind;
2164 argv += optind;
2166 cwd = getcwd(NULL, 0);
2167 if (cwd == NULL) {
2168 error = got_error_from_errno("getcwd");
2169 goto done;
2171 error = got_worktree_open(&worktree, cwd);
2172 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2173 goto done;
2174 if (argc <= 1) {
2175 if (worktree == NULL) {
2176 error = got_error(GOT_ERR_NOT_WORKTREE);
2177 goto done;
2179 if (repo_path)
2180 errx(1,
2181 "-r option can't be used when diffing a work tree");
2182 repo_path = strdup(got_worktree_get_repo_path(worktree));
2183 if (repo_path == NULL) {
2184 error = got_error_from_errno("strdup");
2185 goto done;
2187 if (argc == 1) {
2188 error = got_worktree_resolve_path(&path, worktree,
2189 argv[0]);
2190 if (error)
2191 goto done;
2192 } else {
2193 path = strdup("");
2194 if (path == NULL) {
2195 error = got_error_from_errno("strdup");
2196 goto done;
2199 } else if (argc == 2) {
2200 if (diff_staged)
2201 errx(1, "-s option can't be used when diffing "
2202 "objects in repository");
2203 id_str1 = argv[0];
2204 id_str2 = argv[1];
2205 if (worktree && repo_path == NULL) {
2206 repo_path =
2207 strdup(got_worktree_get_repo_path(worktree));
2208 if (repo_path == NULL) {
2209 error = got_error_from_errno("strdup");
2210 goto done;
2213 } else
2214 usage_diff();
2216 if (repo_path == NULL) {
2217 repo_path = getcwd(NULL, 0);
2218 if (repo_path == NULL)
2219 return got_error_from_errno("getcwd");
2222 error = got_repo_open(&repo, repo_path, NULL);
2223 free(repo_path);
2224 if (error != NULL)
2225 goto done;
2227 error = apply_unveil(got_repo_get_path(repo), 1,
2228 worktree ? got_worktree_get_root_path(worktree) : NULL);
2229 if (error)
2230 goto done;
2232 if (argc <= 1) {
2233 struct print_diff_arg arg;
2234 struct got_pathlist_head paths;
2235 char *id_str;
2237 TAILQ_INIT(&paths);
2239 error = got_object_id_str(&id_str,
2240 got_worktree_get_base_commit_id(worktree));
2241 if (error)
2242 goto done;
2243 arg.repo = repo;
2244 arg.worktree = worktree;
2245 arg.diff_context = diff_context;
2246 arg.id_str = id_str;
2247 arg.header_shown = 0;
2248 arg.diff_staged = diff_staged;
2250 error = got_pathlist_append(&paths, path, NULL);
2251 if (error)
2252 goto done;
2254 error = got_worktree_status(worktree, &paths, repo, print_diff,
2255 &arg, check_cancelled, NULL);
2256 free(id_str);
2257 got_pathlist_free(&paths);
2258 goto done;
2261 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2262 repo);
2263 if (error)
2264 goto done;
2266 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2267 repo);
2268 if (error)
2269 goto done;
2271 error = got_object_get_type(&type1, repo, id1);
2272 if (error)
2273 goto done;
2275 error = got_object_get_type(&type2, repo, id2);
2276 if (error)
2277 goto done;
2279 if (type1 != type2) {
2280 error = got_error(GOT_ERR_OBJ_TYPE);
2281 goto done;
2284 switch (type1) {
2285 case GOT_OBJ_TYPE_BLOB:
2286 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2287 diff_context, repo, stdout);
2288 break;
2289 case GOT_OBJ_TYPE_TREE:
2290 error = got_diff_objects_as_trees(id1, id2, "", "",
2291 diff_context, repo, stdout);
2292 break;
2293 case GOT_OBJ_TYPE_COMMIT:
2294 printf("diff %s %s\n", label1, label2);
2295 error = got_diff_objects_as_commits(id1, id2, diff_context,
2296 repo, stdout);
2297 break;
2298 default:
2299 error = got_error(GOT_ERR_OBJ_TYPE);
2302 done:
2303 free(label1);
2304 free(label2);
2305 free(id1);
2306 free(id2);
2307 free(path);
2308 if (worktree)
2309 got_worktree_close(worktree);
2310 if (repo) {
2311 const struct got_error *repo_error;
2312 repo_error = got_repo_close(repo);
2313 if (error == NULL)
2314 error = repo_error;
2316 return error;
2319 __dead static void
2320 usage_blame(void)
2322 fprintf(stderr,
2323 "usage: %s blame [-c commit] [-r repository-path] path\n",
2324 getprogname());
2325 exit(1);
2328 struct blame_line {
2329 int annotated;
2330 char *id_str;
2331 char *committer;
2332 char datebuf[9]; /* YY-MM-DD + NUL */
2335 struct blame_cb_args {
2336 struct blame_line *lines;
2337 int nlines;
2338 int nlines_prec;
2339 int lineno_cur;
2340 off_t *line_offsets;
2341 FILE *f;
2342 struct got_repository *repo;
2345 static const struct got_error *
2346 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2348 const struct got_error *err = NULL;
2349 struct blame_cb_args *a = arg;
2350 struct blame_line *bline;
2351 char *line = NULL;
2352 size_t linesize = 0;
2353 struct got_commit_object *commit = NULL;
2354 off_t offset;
2355 struct tm tm;
2356 time_t committer_time;
2358 if (nlines != a->nlines ||
2359 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2360 return got_error(GOT_ERR_RANGE);
2362 if (sigint_received)
2363 return got_error(GOT_ERR_ITER_COMPLETED);
2365 if (lineno == -1)
2366 return NULL; /* no change in this commit */
2368 /* Annotate this line. */
2369 bline = &a->lines[lineno - 1];
2370 if (bline->annotated)
2371 return NULL;
2372 err = got_object_id_str(&bline->id_str, id);
2373 if (err)
2374 return err;
2376 err = got_object_open_as_commit(&commit, a->repo, id);
2377 if (err)
2378 goto done;
2380 bline->committer = strdup(got_object_commit_get_committer(commit));
2381 if (bline->committer == NULL) {
2382 err = got_error_from_errno("strdup");
2383 goto done;
2386 committer_time = got_object_commit_get_committer_time(commit);
2387 if (localtime_r(&committer_time, &tm) == NULL)
2388 return got_error_from_errno("localtime_r");
2389 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2390 &tm) >= sizeof(bline->datebuf)) {
2391 err = got_error(GOT_ERR_NO_SPACE);
2392 goto done;
2394 bline->annotated = 1;
2396 /* Print lines annotated so far. */
2397 bline = &a->lines[a->lineno_cur - 1];
2398 if (!bline->annotated)
2399 goto done;
2401 offset = a->line_offsets[a->lineno_cur - 1];
2402 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2403 err = got_error_from_errno("fseeko");
2404 goto done;
2407 while (bline->annotated) {
2408 char *smallerthan, *at, *nl, *committer;
2409 size_t len;
2411 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2412 if (ferror(a->f))
2413 err = got_error_from_errno("getline");
2414 break;
2417 committer = bline->committer;
2418 smallerthan = strchr(committer, '<');
2419 if (smallerthan && smallerthan[1] != '\0')
2420 committer = smallerthan + 1;
2421 at = strchr(committer, '@');
2422 if (at)
2423 *at = '\0';
2424 len = strlen(committer);
2425 if (len >= 9)
2426 committer[8] = '\0';
2428 nl = strchr(line, '\n');
2429 if (nl)
2430 *nl = '\0';
2431 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2432 bline->id_str, bline->datebuf, committer, line);
2434 a->lineno_cur++;
2435 bline = &a->lines[a->lineno_cur - 1];
2437 done:
2438 if (commit)
2439 got_object_commit_close(commit);
2440 free(line);
2441 return err;
2444 static const struct got_error *
2445 cmd_blame(int argc, char *argv[])
2447 const struct got_error *error;
2448 struct got_repository *repo = NULL;
2449 struct got_worktree *worktree = NULL;
2450 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2451 struct got_object_id *obj_id = NULL;
2452 struct got_object_id *commit_id = NULL;
2453 struct got_blob_object *blob = NULL;
2454 char *commit_id_str = NULL;
2455 struct blame_cb_args bca;
2456 int ch, obj_type, i;
2457 size_t filesize;
2459 memset(&bca, 0, sizeof(bca));
2461 #ifndef PROFILE
2462 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2463 NULL) == -1)
2464 err(1, "pledge");
2465 #endif
2467 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2468 switch (ch) {
2469 case 'c':
2470 commit_id_str = optarg;
2471 break;
2472 case 'r':
2473 repo_path = realpath(optarg, NULL);
2474 if (repo_path == NULL)
2475 err(1, "-r option");
2476 got_path_strip_trailing_slashes(repo_path);
2477 break;
2478 default:
2479 usage_blame();
2480 /* NOTREACHED */
2484 argc -= optind;
2485 argv += optind;
2487 if (argc == 1)
2488 path = argv[0];
2489 else
2490 usage_blame();
2492 cwd = getcwd(NULL, 0);
2493 if (cwd == NULL) {
2494 error = got_error_from_errno("getcwd");
2495 goto done;
2497 if (repo_path == NULL) {
2498 error = got_worktree_open(&worktree, cwd);
2499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2500 goto done;
2501 else
2502 error = NULL;
2503 if (worktree) {
2504 repo_path =
2505 strdup(got_worktree_get_repo_path(worktree));
2506 if (repo_path == NULL)
2507 error = got_error_from_errno("strdup");
2508 if (error)
2509 goto done;
2510 } else {
2511 repo_path = strdup(cwd);
2512 if (repo_path == NULL) {
2513 error = got_error_from_errno("strdup");
2514 goto done;
2519 error = got_repo_open(&repo, repo_path, NULL);
2520 if (error != NULL)
2521 goto done;
2523 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2524 if (error)
2525 goto done;
2527 if (worktree) {
2528 const char *prefix = got_worktree_get_path_prefix(worktree);
2529 char *p, *worktree_subdir = cwd +
2530 strlen(got_worktree_get_root_path(worktree));
2531 if (asprintf(&p, "%s%s%s%s%s",
2532 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2533 worktree_subdir, worktree_subdir[0] ? "/" : "",
2534 path) == -1) {
2535 error = got_error_from_errno("asprintf");
2536 goto done;
2538 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2539 free(p);
2540 } else {
2541 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2543 if (error)
2544 goto done;
2546 if (commit_id_str == NULL) {
2547 struct got_reference *head_ref;
2548 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2549 if (error != NULL)
2550 goto done;
2551 error = got_ref_resolve(&commit_id, repo, head_ref);
2552 got_ref_close(head_ref);
2553 if (error != NULL)
2554 goto done;
2555 } else {
2556 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2557 if (error)
2558 goto done;
2561 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2562 if (error)
2563 goto done;
2564 if (obj_id == NULL) {
2565 error = got_error(GOT_ERR_NO_OBJ);
2566 goto done;
2569 error = got_object_get_type(&obj_type, repo, obj_id);
2570 if (error)
2571 goto done;
2573 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2574 error = got_error(GOT_ERR_OBJ_TYPE);
2575 goto done;
2578 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2579 if (error)
2580 goto done;
2581 bca.f = got_opentemp();
2582 if (bca.f == NULL) {
2583 error = got_error_from_errno("got_opentemp");
2584 goto done;
2586 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2587 &bca.line_offsets, bca.f, blob);
2588 if (error || bca.nlines == 0)
2589 goto done;
2591 /* Don't include \n at EOF in the blame line count. */
2592 if (bca.line_offsets[bca.nlines - 1] == filesize)
2593 bca.nlines--;
2595 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2596 if (bca.lines == NULL) {
2597 error = got_error_from_errno("calloc");
2598 goto done;
2600 bca.lineno_cur = 1;
2601 bca.nlines_prec = 0;
2602 i = bca.nlines;
2603 while (i > 0) {
2604 i /= 10;
2605 bca.nlines_prec++;
2607 bca.repo = repo;
2609 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2610 check_cancelled, NULL);
2611 if (error)
2612 goto done;
2613 done:
2614 free(in_repo_path);
2615 free(repo_path);
2616 free(cwd);
2617 free(commit_id);
2618 free(obj_id);
2619 if (blob)
2620 got_object_blob_close(blob);
2621 if (worktree)
2622 got_worktree_close(worktree);
2623 if (repo) {
2624 const struct got_error *repo_error;
2625 repo_error = got_repo_close(repo);
2626 if (error == NULL)
2627 error = repo_error;
2629 if (bca.lines) {
2630 for (i = 0; i < bca.nlines; i++) {
2631 struct blame_line *bline = &bca.lines[i];
2632 free(bline->id_str);
2633 free(bline->committer);
2635 free(bca.lines);
2637 free(bca.line_offsets);
2638 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2639 error = got_error_from_errno("fclose");
2640 return error;
2643 __dead static void
2644 usage_tree(void)
2646 fprintf(stderr,
2647 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2648 getprogname());
2649 exit(1);
2652 static void
2653 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2654 const char *root_path)
2656 int is_root_path = (strcmp(path, root_path) == 0);
2657 const char *modestr = "";
2659 path += strlen(root_path);
2660 while (path[0] == '/')
2661 path++;
2663 if (got_object_tree_entry_is_submodule(te))
2664 modestr = "$";
2665 else if (S_ISLNK(te->mode))
2666 modestr = "@";
2667 else if (S_ISDIR(te->mode))
2668 modestr = "/";
2669 else if (te->mode & S_IXUSR)
2670 modestr = "*";
2672 printf("%s%s%s%s%s\n", id ? id : "", path,
2673 is_root_path ? "" : "/", te->name, modestr);
2676 static const struct got_error *
2677 print_tree(const char *path, struct got_object_id *commit_id,
2678 int show_ids, int recurse, const char *root_path,
2679 struct got_repository *repo)
2681 const struct got_error *err = NULL;
2682 struct got_object_id *tree_id = NULL;
2683 struct got_tree_object *tree = NULL;
2684 const struct got_tree_entries *entries;
2685 struct got_tree_entry *te;
2687 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2688 if (err)
2689 goto done;
2691 err = got_object_open_as_tree(&tree, repo, tree_id);
2692 if (err)
2693 goto done;
2694 entries = got_object_tree_get_entries(tree);
2695 te = SIMPLEQ_FIRST(&entries->head);
2696 while (te) {
2697 char *id = NULL;
2699 if (sigint_received || sigpipe_received)
2700 break;
2702 if (show_ids) {
2703 char *id_str;
2704 err = got_object_id_str(&id_str, te->id);
2705 if (err)
2706 goto done;
2707 if (asprintf(&id, "%s ", id_str) == -1) {
2708 err = got_error_from_errno("asprintf");
2709 free(id_str);
2710 goto done;
2712 free(id_str);
2714 print_entry(te, id, path, root_path);
2715 free(id);
2717 if (recurse && S_ISDIR(te->mode)) {
2718 char *child_path;
2719 if (asprintf(&child_path, "%s%s%s", path,
2720 path[0] == '/' && path[1] == '\0' ? "" : "/",
2721 te->name) == -1) {
2722 err = got_error_from_errno("asprintf");
2723 goto done;
2725 err = print_tree(child_path, commit_id, show_ids, 1,
2726 root_path, repo);
2727 free(child_path);
2728 if (err)
2729 goto done;
2732 te = SIMPLEQ_NEXT(te, entry);
2734 done:
2735 if (tree)
2736 got_object_tree_close(tree);
2737 free(tree_id);
2738 return err;
2741 static const struct got_error *
2742 cmd_tree(int argc, char *argv[])
2744 const struct got_error *error;
2745 struct got_repository *repo = NULL;
2746 struct got_worktree *worktree = NULL;
2747 const char *path;
2748 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2749 struct got_object_id *commit_id = NULL;
2750 char *commit_id_str = NULL;
2751 int show_ids = 0, recurse = 0;
2752 int ch;
2754 #ifndef PROFILE
2755 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2756 NULL) == -1)
2757 err(1, "pledge");
2758 #endif
2760 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2761 switch (ch) {
2762 case 'c':
2763 commit_id_str = optarg;
2764 break;
2765 case 'r':
2766 repo_path = realpath(optarg, NULL);
2767 if (repo_path == NULL)
2768 err(1, "-r option");
2769 got_path_strip_trailing_slashes(repo_path);
2770 break;
2771 case 'i':
2772 show_ids = 1;
2773 break;
2774 case 'R':
2775 recurse = 1;
2776 break;
2777 default:
2778 usage_tree();
2779 /* NOTREACHED */
2783 argc -= optind;
2784 argv += optind;
2786 if (argc == 1)
2787 path = argv[0];
2788 else if (argc > 1)
2789 usage_tree();
2790 else
2791 path = NULL;
2793 cwd = getcwd(NULL, 0);
2794 if (cwd == NULL) {
2795 error = got_error_from_errno("getcwd");
2796 goto done;
2798 if (repo_path == NULL) {
2799 error = got_worktree_open(&worktree, cwd);
2800 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2801 goto done;
2802 else
2803 error = NULL;
2804 if (worktree) {
2805 repo_path =
2806 strdup(got_worktree_get_repo_path(worktree));
2807 if (repo_path == NULL)
2808 error = got_error_from_errno("strdup");
2809 if (error)
2810 goto done;
2811 } else {
2812 repo_path = strdup(cwd);
2813 if (repo_path == NULL) {
2814 error = got_error_from_errno("strdup");
2815 goto done;
2820 error = got_repo_open(&repo, repo_path, NULL);
2821 if (error != NULL)
2822 goto done;
2824 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2825 if (error)
2826 goto done;
2828 if (path == NULL) {
2829 if (worktree) {
2830 char *p, *worktree_subdir = cwd +
2831 strlen(got_worktree_get_root_path(worktree));
2832 if (asprintf(&p, "%s/%s",
2833 got_worktree_get_path_prefix(worktree),
2834 worktree_subdir) == -1) {
2835 error = got_error_from_errno("asprintf");
2836 goto done;
2838 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2839 free(p);
2840 if (error)
2841 goto done;
2842 } else
2843 path = "/";
2845 if (in_repo_path == NULL) {
2846 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2847 if (error != NULL)
2848 goto done;
2851 if (commit_id_str == NULL) {
2852 struct got_reference *head_ref;
2853 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2854 if (error != NULL)
2855 goto done;
2856 error = got_ref_resolve(&commit_id, repo, head_ref);
2857 got_ref_close(head_ref);
2858 if (error != NULL)
2859 goto done;
2860 } else {
2861 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2862 if (error)
2863 goto done;
2866 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2867 in_repo_path, repo);
2868 done:
2869 free(in_repo_path);
2870 free(repo_path);
2871 free(cwd);
2872 free(commit_id);
2873 if (worktree)
2874 got_worktree_close(worktree);
2875 if (repo) {
2876 const struct got_error *repo_error;
2877 repo_error = got_repo_close(repo);
2878 if (error == NULL)
2879 error = repo_error;
2881 return error;
2884 __dead static void
2885 usage_status(void)
2887 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2888 exit(1);
2891 static const struct got_error *
2892 print_status(void *arg, unsigned char status, unsigned char staged_status,
2893 const char *path, struct got_object_id *blob_id,
2894 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2896 if (status == staged_status && (status == GOT_STATUS_DELETE))
2897 status = GOT_STATUS_NO_CHANGE;
2898 printf("%c%c %s\n", status, staged_status, path);
2899 return NULL;
2902 static const struct got_error *
2903 cmd_status(int argc, char *argv[])
2905 const struct got_error *error = NULL;
2906 struct got_repository *repo = NULL;
2907 struct got_worktree *worktree = NULL;
2908 char *cwd = NULL;
2909 struct got_pathlist_head paths;
2910 struct got_pathlist_entry *pe;
2911 int ch;
2913 TAILQ_INIT(&paths);
2915 while ((ch = getopt(argc, argv, "")) != -1) {
2916 switch (ch) {
2917 default:
2918 usage_status();
2919 /* NOTREACHED */
2923 argc -= optind;
2924 argv += optind;
2926 #ifndef PROFILE
2927 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2928 NULL) == -1)
2929 err(1, "pledge");
2930 #endif
2931 cwd = getcwd(NULL, 0);
2932 if (cwd == NULL) {
2933 error = got_error_from_errno("getcwd");
2934 goto done;
2937 error = got_worktree_open(&worktree, cwd);
2938 if (error != NULL)
2939 goto done;
2941 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2942 NULL);
2943 if (error != NULL)
2944 goto done;
2946 error = apply_unveil(got_repo_get_path(repo), 1,
2947 got_worktree_get_root_path(worktree));
2948 if (error)
2949 goto done;
2951 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2952 if (error)
2953 goto done;
2955 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2956 check_cancelled, NULL);
2957 done:
2958 TAILQ_FOREACH(pe, &paths, entry)
2959 free((char *)pe->path);
2960 got_pathlist_free(&paths);
2961 free(cwd);
2962 return error;
2965 __dead static void
2966 usage_ref(void)
2968 fprintf(stderr,
2969 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2970 getprogname());
2971 exit(1);
2974 static const struct got_error *
2975 list_refs(struct got_repository *repo)
2977 static const struct got_error *err = NULL;
2978 struct got_reflist_head refs;
2979 struct got_reflist_entry *re;
2981 SIMPLEQ_INIT(&refs);
2982 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2983 if (err)
2984 return err;
2986 SIMPLEQ_FOREACH(re, &refs, entry) {
2987 char *refstr;
2988 refstr = got_ref_to_str(re->ref);
2989 if (refstr == NULL)
2990 return got_error_from_errno("got_ref_to_str");
2991 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2992 free(refstr);
2995 got_ref_list_free(&refs);
2996 return NULL;
2999 static const struct got_error *
3000 delete_ref(struct got_repository *repo, const char *refname)
3002 const struct got_error *err = NULL;
3003 struct got_reference *ref;
3005 err = got_ref_open(&ref, repo, refname, 0);
3006 if (err)
3007 return err;
3009 err = got_ref_delete(ref, repo);
3010 got_ref_close(ref);
3011 return err;
3014 static const struct got_error *
3015 add_ref(struct got_repository *repo, const char *refname, const char *target)
3017 const struct got_error *err = NULL;
3018 struct got_object_id *id;
3019 struct got_reference *ref = NULL;
3022 * Don't let the user create a reference named '-'.
3023 * While technically a valid reference name, this case is usually
3024 * an unintended typo.
3026 if (refname[0] == '-' && refname[1] == '\0')
3027 return got_error(GOT_ERR_BAD_REF_NAME);
3029 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3030 repo);
3031 if (err) {
3032 struct got_reference *target_ref;
3034 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3035 return err;
3036 err = got_ref_open(&target_ref, repo, target, 0);
3037 if (err)
3038 return err;
3039 err = got_ref_resolve(&id, repo, target_ref);
3040 got_ref_close(target_ref);
3041 if (err)
3042 return err;
3045 err = got_ref_alloc(&ref, refname, id);
3046 if (err)
3047 goto done;
3049 err = got_ref_write(ref, repo);
3050 done:
3051 if (ref)
3052 got_ref_close(ref);
3053 free(id);
3054 return err;
3057 static const struct got_error *
3058 add_symref(struct got_repository *repo, const char *refname, const char *target)
3060 const struct got_error *err = NULL;
3061 struct got_reference *ref = NULL;
3062 struct got_reference *target_ref = NULL;
3065 * Don't let the user create a reference named '-'.
3066 * While technically a valid reference name, this case is usually
3067 * an unintended typo.
3069 if (refname[0] == '-' && refname[1] == '\0')
3070 return got_error(GOT_ERR_BAD_REF_NAME);
3072 err = got_ref_open(&target_ref, repo, target, 0);
3073 if (err)
3074 return err;
3076 err = got_ref_alloc_symref(&ref, refname, target_ref);
3077 if (err)
3078 goto done;
3080 err = got_ref_write(ref, repo);
3081 done:
3082 if (target_ref)
3083 got_ref_close(target_ref);
3084 if (ref)
3085 got_ref_close(ref);
3086 return err;
3089 static const struct got_error *
3090 cmd_ref(int argc, char *argv[])
3092 const struct got_error *error = NULL;
3093 struct got_repository *repo = NULL;
3094 struct got_worktree *worktree = NULL;
3095 char *cwd = NULL, *repo_path = NULL;
3096 int ch, do_list = 0, create_symref = 0;
3097 const char *delref = NULL;
3099 /* TODO: Add -s option for adding symbolic references. */
3100 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3101 switch (ch) {
3102 case 'd':
3103 delref = optarg;
3104 break;
3105 case 'r':
3106 repo_path = realpath(optarg, NULL);
3107 if (repo_path == NULL)
3108 err(1, "-r option");
3109 got_path_strip_trailing_slashes(repo_path);
3110 break;
3111 case 'l':
3112 do_list = 1;
3113 break;
3114 case 's':
3115 create_symref = 1;
3116 break;
3117 default:
3118 usage_ref();
3119 /* NOTREACHED */
3123 if (do_list && delref)
3124 errx(1, "-l and -d options are mutually exclusive\n");
3126 argc -= optind;
3127 argv += optind;
3129 if (do_list || delref) {
3130 if (create_symref)
3131 errx(1, "-s option cannot be used together with the "
3132 "-l or -d options");
3133 if (argc > 0)
3134 usage_ref();
3135 } else if (argc != 2)
3136 usage_ref();
3138 #ifndef PROFILE
3139 if (do_list) {
3140 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3141 NULL) == -1)
3142 err(1, "pledge");
3143 } else {
3144 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3145 "sendfd unveil", NULL) == -1)
3146 err(1, "pledge");
3148 #endif
3149 cwd = getcwd(NULL, 0);
3150 if (cwd == NULL) {
3151 error = got_error_from_errno("getcwd");
3152 goto done;
3155 if (repo_path == NULL) {
3156 error = got_worktree_open(&worktree, cwd);
3157 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3158 goto done;
3159 else
3160 error = NULL;
3161 if (worktree) {
3162 repo_path =
3163 strdup(got_worktree_get_repo_path(worktree));
3164 if (repo_path == NULL)
3165 error = got_error_from_errno("strdup");
3166 if (error)
3167 goto done;
3168 } else {
3169 repo_path = strdup(cwd);
3170 if (repo_path == NULL) {
3171 error = got_error_from_errno("strdup");
3172 goto done;
3177 error = got_repo_open(&repo, repo_path, NULL);
3178 if (error != NULL)
3179 goto done;
3181 error = apply_unveil(got_repo_get_path(repo), do_list,
3182 worktree ? got_worktree_get_root_path(worktree) : NULL);
3183 if (error)
3184 goto done;
3186 if (do_list)
3187 error = list_refs(repo);
3188 else if (delref)
3189 error = delete_ref(repo, delref);
3190 else if (create_symref)
3191 error = add_symref(repo, argv[0], argv[1]);
3192 else
3193 error = add_ref(repo, argv[0], argv[1]);
3194 done:
3195 if (repo)
3196 got_repo_close(repo);
3197 if (worktree)
3198 got_worktree_close(worktree);
3199 free(cwd);
3200 free(repo_path);
3201 return error;
3204 __dead static void
3205 usage_branch(void)
3207 fprintf(stderr,
3208 "usage: %s branch [-r repository] -l | -d name | "
3209 "name [commit]\n", getprogname());
3210 exit(1);
3213 static const struct got_error *
3214 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3216 static const struct got_error *err = NULL;
3217 struct got_reflist_head refs;
3218 struct got_reflist_entry *re;
3220 SIMPLEQ_INIT(&refs);
3222 err = got_ref_list(&refs, repo, "refs/heads",
3223 got_ref_cmp_by_name, NULL);
3224 if (err)
3225 return err;
3227 SIMPLEQ_FOREACH(re, &refs, entry) {
3228 const char *refname, *marker = " ";
3229 char *refstr;
3230 refname = got_ref_get_name(re->ref);
3231 if (worktree && strcmp(refname,
3232 got_worktree_get_head_ref_name(worktree)) == 0) {
3233 struct got_object_id *id = NULL;
3234 err = got_ref_resolve(&id, repo, re->ref);
3235 if (err)
3236 return err;
3237 if (got_object_id_cmp(id,
3238 got_worktree_get_base_commit_id(worktree)) == 0)
3239 marker = "* ";
3240 else
3241 marker = "~ ";
3242 free(id);
3244 refname += strlen("refs/heads/");
3245 refstr = got_ref_to_str(re->ref);
3246 if (refstr == NULL)
3247 return got_error_from_errno("got_ref_to_str");
3248 printf("%s%s: %s\n", marker, refname, refstr);
3249 free(refstr);
3252 got_ref_list_free(&refs);
3253 return NULL;
3256 static const struct got_error *
3257 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3258 const char *branch_name)
3260 const struct got_error *err = NULL;
3261 struct got_reference *ref = NULL;
3262 char *refname;
3264 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3265 return got_error_from_errno("asprintf");
3267 err = got_ref_open(&ref, repo, refname, 0);
3268 if (err)
3269 goto done;
3271 if (worktree &&
3272 strcmp(got_worktree_get_head_ref_name(worktree),
3273 got_ref_get_name(ref)) == 0) {
3274 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3275 "will not delete this work tree's current branch");
3276 goto done;
3279 err = got_ref_delete(ref, repo);
3280 done:
3281 if (ref)
3282 got_ref_close(ref);
3283 free(refname);
3284 return err;
3287 static const struct got_error *
3288 add_branch(struct got_repository *repo, const char *branch_name,
3289 const char *base_branch)
3291 const struct got_error *err = NULL;
3292 struct got_object_id *id = NULL;
3293 char *label;
3294 struct got_reference *ref = NULL;
3295 char *base_refname = NULL, *refname = NULL;
3298 * Don't let the user create a branch named '-'.
3299 * While technically a valid reference name, this case is usually
3300 * an unintended typo.
3302 if (branch_name[0] == '-' && branch_name[1] == '\0')
3303 return got_error(GOT_ERR_BAD_REF_NAME);
3305 err = match_object_id(&id, &label, base_branch,
3306 GOT_OBJ_TYPE_COMMIT, 1, repo);
3307 if (err)
3308 return err;
3310 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3311 err = got_error_from_errno("asprintf");
3312 goto done;
3315 err = got_ref_open(&ref, repo, refname, 0);
3316 if (err == NULL) {
3317 err = got_error(GOT_ERR_BRANCH_EXISTS);
3318 goto done;
3319 } else if (err->code != GOT_ERR_NOT_REF)
3320 goto done;
3322 err = got_ref_alloc(&ref, refname, id);
3323 if (err)
3324 goto done;
3326 err = got_ref_write(ref, repo);
3327 done:
3328 if (ref)
3329 got_ref_close(ref);
3330 free(id);
3331 free(base_refname);
3332 free(refname);
3333 return err;
3336 static const struct got_error *
3337 cmd_branch(int argc, char *argv[])
3339 const struct got_error *error = NULL;
3340 struct got_repository *repo = NULL;
3341 struct got_worktree *worktree = NULL;
3342 char *cwd = NULL, *repo_path = NULL;
3343 int ch, do_list = 0;
3344 const char *delref = NULL;
3346 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3347 switch (ch) {
3348 case 'd':
3349 delref = optarg;
3350 break;
3351 case 'r':
3352 repo_path = realpath(optarg, NULL);
3353 if (repo_path == NULL)
3354 err(1, "-r option");
3355 got_path_strip_trailing_slashes(repo_path);
3356 break;
3357 case 'l':
3358 do_list = 1;
3359 break;
3360 default:
3361 usage_branch();
3362 /* NOTREACHED */
3366 if (do_list && delref)
3367 errx(1, "-l and -d options are mutually exclusive\n");
3369 argc -= optind;
3370 argv += optind;
3372 if (do_list || delref) {
3373 if (argc > 0)
3374 usage_branch();
3375 } else if (argc < 1 || argc > 2)
3376 usage_branch();
3378 #ifndef PROFILE
3379 if (do_list) {
3380 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3381 NULL) == -1)
3382 err(1, "pledge");
3383 } else {
3384 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3385 "sendfd unveil", NULL) == -1)
3386 err(1, "pledge");
3388 #endif
3389 cwd = getcwd(NULL, 0);
3390 if (cwd == NULL) {
3391 error = got_error_from_errno("getcwd");
3392 goto done;
3395 if (repo_path == NULL) {
3396 error = got_worktree_open(&worktree, cwd);
3397 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3398 goto done;
3399 else
3400 error = NULL;
3401 if (worktree) {
3402 repo_path =
3403 strdup(got_worktree_get_repo_path(worktree));
3404 if (repo_path == NULL)
3405 error = got_error_from_errno("strdup");
3406 if (error)
3407 goto done;
3408 } else {
3409 repo_path = strdup(cwd);
3410 if (repo_path == NULL) {
3411 error = got_error_from_errno("strdup");
3412 goto done;
3417 error = got_repo_open(&repo, repo_path, NULL);
3418 if (error != NULL)
3419 goto done;
3421 error = apply_unveil(got_repo_get_path(repo), do_list,
3422 worktree ? got_worktree_get_root_path(worktree) : NULL);
3423 if (error)
3424 goto done;
3426 if (do_list)
3427 error = list_branches(repo, worktree);
3428 else if (delref)
3429 error = delete_branch(repo, worktree, delref);
3430 else {
3431 const char *base_branch;
3432 if (argc == 1) {
3433 base_branch = worktree ?
3434 got_worktree_get_head_ref_name(worktree) :
3435 GOT_REF_HEAD;
3436 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3437 base_branch += 11;
3438 } else
3439 base_branch = argv[1];
3440 error = add_branch(repo, argv[0], base_branch);
3442 done:
3443 if (repo)
3444 got_repo_close(repo);
3445 if (worktree)
3446 got_worktree_close(worktree);
3447 free(cwd);
3448 free(repo_path);
3449 return error;
3453 __dead static void
3454 usage_tag(void)
3456 fprintf(stderr,
3457 "usage: %s tag [-r repository] | -l | "
3458 "[-m message] name [commit]\n", getprogname());
3459 exit(1);
3462 #if 0
3463 static const struct got_error *
3464 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3466 const struct got_error *err = NULL;
3467 struct got_reflist_entry *re, *se, *new;
3468 struct got_object_id *re_id, *se_id;
3469 struct got_tag_object *re_tag, *se_tag;
3470 time_t re_time, se_time;
3472 SIMPLEQ_FOREACH(re, tags, entry) {
3473 se = SIMPLEQ_FIRST(sorted);
3474 if (se == NULL) {
3475 err = got_reflist_entry_dup(&new, re);
3476 if (err)
3477 return err;
3478 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3479 continue;
3480 } else {
3481 err = got_ref_resolve(&re_id, repo, re->ref);
3482 if (err)
3483 break;
3484 err = got_object_open_as_tag(&re_tag, repo, re_id);
3485 free(re_id);
3486 if (err)
3487 break;
3488 re_time = got_object_tag_get_tagger_time(re_tag);
3489 got_object_tag_close(re_tag);
3492 while (se) {
3493 err = got_ref_resolve(&se_id, repo, re->ref);
3494 if (err)
3495 break;
3496 err = got_object_open_as_tag(&se_tag, repo, se_id);
3497 free(se_id);
3498 if (err)
3499 break;
3500 se_time = got_object_tag_get_tagger_time(se_tag);
3501 got_object_tag_close(se_tag);
3503 if (se_time > re_time) {
3504 err = got_reflist_entry_dup(&new, re);
3505 if (err)
3506 return err;
3507 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3508 break;
3510 se = SIMPLEQ_NEXT(se, entry);
3511 continue;
3514 done:
3515 return err;
3517 #endif
3519 static const struct got_error *
3520 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3521 struct got_reference *ref2)
3523 const struct got_error *err = NULL;
3524 struct got_repository *repo = arg;
3525 struct got_object_id *id1, *id2 = NULL;
3526 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3527 time_t time1, time2;
3529 *cmp = 0;
3531 err = got_ref_resolve(&id1, repo, ref1);
3532 if (err)
3533 return err;
3534 err = got_object_open_as_tag(&tag1, repo, id1);
3535 if (err)
3536 goto done;
3538 err = got_ref_resolve(&id2, repo, ref2);
3539 if (err)
3540 goto done;
3541 err = got_object_open_as_tag(&tag2, repo, id2);
3542 if (err)
3543 goto done;
3545 time1 = got_object_tag_get_tagger_time(tag1);
3546 time2 = got_object_tag_get_tagger_time(tag2);
3548 /* Put latest tags first. */
3549 if (time1 < time2)
3550 *cmp = 1;
3551 else if (time1 > time2)
3552 *cmp = -1;
3553 else
3554 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3555 done:
3556 free(id1);
3557 free(id2);
3558 if (tag1)
3559 got_object_tag_close(tag1);
3560 if (tag2)
3561 got_object_tag_close(tag2);
3562 return err;
3565 static const struct got_error *
3566 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3568 static const struct got_error *err = NULL;
3569 struct got_reflist_head refs;
3570 struct got_reflist_entry *re;
3572 SIMPLEQ_INIT(&refs);
3574 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3575 if (err)
3576 return err;
3578 SIMPLEQ_FOREACH(re, &refs, entry) {
3579 const char *refname;
3580 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3581 char datebuf[26];
3582 time_t tagger_time;
3583 struct got_object_id *id;
3584 struct got_tag_object *tag;
3586 refname = got_ref_get_name(re->ref);
3587 if (strncmp(refname, "refs/tags/", 10) != 0)
3588 continue;
3589 refname += 10;
3590 refstr = got_ref_to_str(re->ref);
3591 if (refstr == NULL) {
3592 err = got_error_from_errno("got_ref_to_str");
3593 break;
3595 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3596 free(refstr);
3598 err = got_ref_resolve(&id, repo, re->ref);
3599 if (err)
3600 break;
3601 err = got_object_open_as_tag(&tag, repo, id);
3602 free(id);
3603 if (err)
3604 break;
3605 printf("from: %s\n", got_object_tag_get_tagger(tag));
3606 tagger_time = got_object_tag_get_tagger_time(tag);
3607 datestr = get_datestr(&tagger_time, datebuf);
3608 if (datestr)
3609 printf("date: %s UTC\n", datestr);
3610 err = got_object_id_str(&id_str,
3611 got_object_tag_get_object_id(tag));
3612 if (err)
3613 break;
3614 switch (got_object_tag_get_object_type(tag)) {
3615 case GOT_OBJ_TYPE_BLOB:
3616 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3617 break;
3618 case GOT_OBJ_TYPE_TREE:
3619 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3620 break;
3621 case GOT_OBJ_TYPE_COMMIT:
3622 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3623 break;
3624 case GOT_OBJ_TYPE_TAG:
3625 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3626 break;
3627 default:
3628 break;
3630 free(id_str);
3631 tagmsg0 = strdup(got_object_tag_get_message(tag));
3632 got_object_tag_close(tag);
3633 if (tagmsg0 == NULL) {
3634 err = got_error_from_errno("strdup");
3635 break;
3638 tagmsg = tagmsg0;
3639 do {
3640 line = strsep(&tagmsg, "\n");
3641 if (line)
3642 printf(" %s\n", line);
3643 } while (line);
3644 free(tagmsg0);
3647 got_ref_list_free(&refs);
3648 return NULL;
3651 static const struct got_error *
3652 get_tag_message(char **tagmsg, const char *commit_id_str,
3653 const char *tag_name, const char *repo_path)
3655 const struct got_error *err = NULL;
3656 char *template = NULL, *initial_content = NULL;
3657 char *tagmsg_path = NULL, *editor = NULL;
3658 int fd = -1;
3660 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3661 err = got_error_from_errno("asprintf");
3662 goto done;
3665 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3666 commit_id_str, tag_name) == -1) {
3667 err = got_error_from_errno("asprintf");
3668 goto done;
3671 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3672 if (err)
3673 goto done;
3675 dprintf(fd, initial_content);
3676 close(fd);
3678 err = get_editor(&editor);
3679 if (err)
3680 goto done;
3681 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3682 done:
3683 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3684 unlink(tagmsg_path);
3685 free(tagmsg_path);
3686 tagmsg_path = NULL;
3688 free(initial_content);
3689 free(template);
3690 free(editor);
3692 /* Editor is done; we can now apply unveil(2) */
3693 if (err == NULL) {
3694 err = apply_unveil(repo_path, 0, NULL);
3695 if (err) {
3696 free(*tagmsg);
3697 *tagmsg = NULL;
3700 return err;
3703 static const struct got_error *
3704 add_tag(struct got_repository *repo, const char *tag_name,
3705 const char *commit_arg, const char *tagmsg_arg)
3707 const struct got_error *err = NULL;
3708 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3709 char *label = NULL, *commit_id_str = NULL;
3710 struct got_reference *ref = NULL;
3711 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3714 * Don't let the user create a tag named '-'.
3715 * While technically a valid reference name, this case is usually
3716 * an unintended typo.
3718 if (tag_name[0] == '-' && tag_name[1] == '\0')
3719 return got_error(GOT_ERR_BAD_REF_NAME);
3721 err = get_author(&tagger, repo);
3722 if (err)
3723 return err;
3725 err = match_object_id(&commit_id, &label, commit_arg,
3726 GOT_OBJ_TYPE_COMMIT, 1, repo);
3727 if (err)
3728 goto done;
3730 err = got_object_id_str(&commit_id_str, commit_id);
3731 if (err)
3732 goto done;
3734 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3735 refname = strdup(tag_name);
3736 if (refname == NULL) {
3737 err = got_error_from_errno("strdup");
3738 goto done;
3740 tag_name += 10;
3741 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3742 err = got_error_from_errno("asprintf");
3743 goto done;
3746 err = got_ref_open(&ref, repo, refname, 0);
3747 if (err == NULL) {
3748 err = got_error(GOT_ERR_TAG_EXISTS);
3749 goto done;
3750 } else if (err->code != GOT_ERR_NOT_REF)
3751 goto done;
3753 if (tagmsg_arg == NULL) {
3754 err = get_tag_message(&tagmsg, commit_id_str,
3755 tag_name, got_repo_get_path(repo));
3756 if (err)
3757 goto done;
3760 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3761 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3762 if (err)
3763 goto done;
3765 err = got_ref_alloc(&ref, refname, tag_id);
3766 if (err)
3767 goto done;
3769 err = got_ref_write(ref, repo);
3771 if (err == NULL) {
3772 char *tag_id_str;
3773 err = got_object_id_str(&tag_id_str, tag_id);
3774 printf("Created tag %s\n", tag_id_str);
3775 free(tag_id_str);
3777 done:
3778 if (ref)
3779 got_ref_close(ref);
3780 free(commit_id);
3781 free(commit_id_str);
3782 free(refname);
3783 free(tagmsg);
3784 free(tagger);
3785 return err;
3788 static const struct got_error *
3789 cmd_tag(int argc, char *argv[])
3791 const struct got_error *error = NULL;
3792 struct got_repository *repo = NULL;
3793 struct got_worktree *worktree = NULL;
3794 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3795 char *gitconfig_path = NULL;
3796 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3797 int ch, do_list = 0;
3799 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3800 switch (ch) {
3801 case 'm':
3802 tagmsg = optarg;
3803 break;
3804 case 'r':
3805 repo_path = realpath(optarg, NULL);
3806 if (repo_path == NULL)
3807 err(1, "-r option");
3808 got_path_strip_trailing_slashes(repo_path);
3809 break;
3810 case 'l':
3811 do_list = 1;
3812 break;
3813 default:
3814 usage_tag();
3815 /* NOTREACHED */
3819 argc -= optind;
3820 argv += optind;
3822 if (do_list) {
3823 if (tagmsg)
3824 errx(1, "-l and -m options are mutually exclusive\n");
3825 if (argc > 0)
3826 usage_tag();
3827 } else if (argc < 1 || argc > 2)
3828 usage_tag();
3829 else if (argc > 1)
3830 commit_id_arg = argv[1];
3831 tag_name = argv[0];
3833 #ifndef PROFILE
3834 if (do_list) {
3835 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3836 NULL) == -1)
3837 err(1, "pledge");
3838 } else {
3839 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3840 "sendfd unveil", NULL) == -1)
3841 err(1, "pledge");
3843 #endif
3844 cwd = getcwd(NULL, 0);
3845 if (cwd == NULL) {
3846 error = got_error_from_errno("getcwd");
3847 goto done;
3850 if (repo_path == NULL) {
3851 error = got_worktree_open(&worktree, cwd);
3852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3853 goto done;
3854 else
3855 error = NULL;
3856 if (worktree) {
3857 repo_path =
3858 strdup(got_worktree_get_repo_path(worktree));
3859 if (repo_path == NULL)
3860 error = got_error_from_errno("strdup");
3861 if (error)
3862 goto done;
3863 } else {
3864 repo_path = strdup(cwd);
3865 if (repo_path == NULL) {
3866 error = got_error_from_errno("strdup");
3867 goto done;
3872 if (do_list) {
3873 error = got_repo_open(&repo, repo_path, NULL);
3874 if (error != NULL)
3875 goto done;
3876 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3877 if (error)
3878 goto done;
3879 error = list_tags(repo, worktree);
3880 } else {
3881 error = get_gitconfig_path(&gitconfig_path);
3882 if (error)
3883 goto done;
3884 error = got_repo_open(&repo, repo_path, gitconfig_path);
3885 if (error != NULL)
3886 goto done;
3888 if (tagmsg) {
3889 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3890 if (error)
3891 goto done;
3894 if (commit_id_arg == NULL) {
3895 struct got_reference *head_ref;
3896 struct got_object_id *commit_id;
3897 error = got_ref_open(&head_ref, repo,
3898 worktree ? got_worktree_get_head_ref_name(worktree)
3899 : GOT_REF_HEAD, 0);
3900 if (error)
3901 goto done;
3902 error = got_ref_resolve(&commit_id, repo, head_ref);
3903 got_ref_close(head_ref);
3904 if (error)
3905 goto done;
3906 error = got_object_id_str(&commit_id_str, commit_id);
3907 free(commit_id);
3908 if (error)
3909 goto done;
3912 error = add_tag(repo, tag_name,
3913 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3915 done:
3916 if (repo)
3917 got_repo_close(repo);
3918 if (worktree)
3919 got_worktree_close(worktree);
3920 free(cwd);
3921 free(repo_path);
3922 free(gitconfig_path);
3923 free(commit_id_str);
3924 return error;
3927 __dead static void
3928 usage_add(void)
3930 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3931 exit(1);
3934 static const struct got_error *
3935 cmd_add(int argc, char *argv[])
3937 const struct got_error *error = NULL;
3938 struct got_repository *repo = NULL;
3939 struct got_worktree *worktree = NULL;
3940 char *cwd = NULL;
3941 struct got_pathlist_head paths;
3942 struct got_pathlist_entry *pe;
3943 int ch;
3945 TAILQ_INIT(&paths);
3947 while ((ch = getopt(argc, argv, "")) != -1) {
3948 switch (ch) {
3949 default:
3950 usage_add();
3951 /* NOTREACHED */
3955 argc -= optind;
3956 argv += optind;
3958 #ifndef PROFILE
3959 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3960 NULL) == -1)
3961 err(1, "pledge");
3962 #endif
3963 if (argc < 1)
3964 usage_add();
3966 cwd = getcwd(NULL, 0);
3967 if (cwd == NULL) {
3968 error = got_error_from_errno("getcwd");
3969 goto done;
3972 error = got_worktree_open(&worktree, cwd);
3973 if (error)
3974 goto done;
3976 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3977 NULL);
3978 if (error != NULL)
3979 goto done;
3981 error = apply_unveil(got_repo_get_path(repo), 1,
3982 got_worktree_get_root_path(worktree));
3983 if (error)
3984 goto done;
3986 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3987 if (error)
3988 goto done;
3990 error = got_worktree_schedule_add(worktree, &paths, print_status,
3991 NULL, repo);
3992 done:
3993 if (repo)
3994 got_repo_close(repo);
3995 if (worktree)
3996 got_worktree_close(worktree);
3997 TAILQ_FOREACH(pe, &paths, entry)
3998 free((char *)pe->path);
3999 got_pathlist_free(&paths);
4000 free(cwd);
4001 return error;
4004 __dead static void
4005 usage_remove(void)
4007 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4008 exit(1);
4011 static const struct got_error *
4012 print_remove_status(void *arg, unsigned char status,
4013 unsigned char staged_status, const char *path,
4014 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4015 struct got_object_id *commit_id)
4017 if (status == GOT_STATUS_NONEXISTENT)
4018 return NULL;
4019 if (status == staged_status && (status == GOT_STATUS_DELETE))
4020 status = GOT_STATUS_NO_CHANGE;
4021 printf("%c%c %s\n", status, staged_status, path);
4022 return NULL;
4025 static const struct got_error *
4026 cmd_remove(int argc, char *argv[])
4028 const struct got_error *error = NULL;
4029 struct got_worktree *worktree = NULL;
4030 struct got_repository *repo = NULL;
4031 char *cwd = NULL;
4032 struct got_pathlist_head paths;
4033 struct got_pathlist_entry *pe;
4034 int ch, delete_local_mods = 0;
4036 TAILQ_INIT(&paths);
4038 while ((ch = getopt(argc, argv, "f")) != -1) {
4039 switch (ch) {
4040 case 'f':
4041 delete_local_mods = 1;
4042 break;
4043 default:
4044 usage_add();
4045 /* NOTREACHED */
4049 argc -= optind;
4050 argv += optind;
4052 #ifndef PROFILE
4053 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4054 NULL) == -1)
4055 err(1, "pledge");
4056 #endif
4057 if (argc < 1)
4058 usage_remove();
4060 cwd = getcwd(NULL, 0);
4061 if (cwd == NULL) {
4062 error = got_error_from_errno("getcwd");
4063 goto done;
4065 error = got_worktree_open(&worktree, cwd);
4066 if (error)
4067 goto done;
4069 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4070 NULL);
4071 if (error)
4072 goto done;
4074 error = apply_unveil(got_repo_get_path(repo), 1,
4075 got_worktree_get_root_path(worktree));
4076 if (error)
4077 goto done;
4079 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4080 if (error)
4081 goto done;
4083 error = got_worktree_schedule_delete(worktree, &paths,
4084 delete_local_mods, print_remove_status, NULL, repo);
4085 if (error)
4086 goto done;
4087 done:
4088 if (repo)
4089 got_repo_close(repo);
4090 if (worktree)
4091 got_worktree_close(worktree);
4092 TAILQ_FOREACH(pe, &paths, entry)
4093 free((char *)pe->path);
4094 got_pathlist_free(&paths);
4095 free(cwd);
4096 return error;
4099 __dead static void
4100 usage_revert(void)
4102 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4103 "path ...\n", getprogname());
4104 exit(1);
4107 static const struct got_error *
4108 revert_progress(void *arg, unsigned char status, const char *path)
4110 while (path[0] == '/')
4111 path++;
4112 printf("%c %s\n", status, path);
4113 return NULL;
4116 struct choose_patch_arg {
4117 FILE *patch_script_file;
4118 const char *action;
4121 static const struct got_error *
4122 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4123 int nchanges, const char *action)
4125 char *line = NULL;
4126 size_t linesize = 0;
4127 ssize_t linelen;
4129 switch (status) {
4130 case GOT_STATUS_ADD:
4131 printf("A %s\n%s this addition? [y/n] ", path, action);
4132 break;
4133 case GOT_STATUS_DELETE:
4134 printf("D %s\n%s this deletion? [y/n] ", path, action);
4135 break;
4136 case GOT_STATUS_MODIFY:
4137 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4138 return got_error_from_errno("fseek");
4139 printf(GOT_COMMIT_SEP_STR);
4140 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4141 printf("%s", line);
4142 if (ferror(patch_file))
4143 return got_error_from_errno("getline");
4144 printf(GOT_COMMIT_SEP_STR);
4145 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4146 path, n, nchanges, action);
4147 break;
4148 default:
4149 return got_error_path(path, GOT_ERR_FILE_STATUS);
4152 return NULL;
4155 static const struct got_error *
4156 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4157 FILE *patch_file, int n, int nchanges)
4159 const struct got_error *err = NULL;
4160 char *line = NULL;
4161 size_t linesize = 0;
4162 ssize_t linelen;
4163 int resp = ' ';
4164 struct choose_patch_arg *a = arg;
4166 *choice = GOT_PATCH_CHOICE_NONE;
4168 if (a->patch_script_file) {
4169 char *nl;
4170 err = show_change(status, path, patch_file, n, nchanges,
4171 a->action);
4172 if (err)
4173 return err;
4174 linelen = getline(&line, &linesize, a->patch_script_file);
4175 if (linelen == -1) {
4176 if (ferror(a->patch_script_file))
4177 return got_error_from_errno("getline");
4178 return NULL;
4180 nl = strchr(line, '\n');
4181 if (nl)
4182 *nl = '\0';
4183 if (strcmp(line, "y") == 0) {
4184 *choice = GOT_PATCH_CHOICE_YES;
4185 printf("y\n");
4186 } else if (strcmp(line, "n") == 0) {
4187 *choice = GOT_PATCH_CHOICE_NO;
4188 printf("n\n");
4189 } else if (strcmp(line, "q") == 0 &&
4190 status == GOT_STATUS_MODIFY) {
4191 *choice = GOT_PATCH_CHOICE_QUIT;
4192 printf("q\n");
4193 } else
4194 printf("invalid response '%s'\n", line);
4195 free(line);
4196 return NULL;
4199 while (resp != 'y' && resp != 'n' && resp != 'q') {
4200 err = show_change(status, path, patch_file, n, nchanges,
4201 a->action);
4202 if (err)
4203 return err;
4204 resp = getchar();
4205 if (resp == '\n')
4206 resp = getchar();
4207 if (status == GOT_STATUS_MODIFY) {
4208 if (resp != 'y' && resp != 'n' && resp != 'q') {
4209 printf("invalid response '%c'\n", resp);
4210 resp = ' ';
4212 } else if (resp != 'y' && resp != 'n') {
4213 printf("invalid response '%c'\n", resp);
4214 resp = ' ';
4218 if (resp == 'y')
4219 *choice = GOT_PATCH_CHOICE_YES;
4220 else if (resp == 'n')
4221 *choice = GOT_PATCH_CHOICE_NO;
4222 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4223 *choice = GOT_PATCH_CHOICE_QUIT;
4225 return NULL;
4229 static const struct got_error *
4230 cmd_revert(int argc, char *argv[])
4232 const struct got_error *error = NULL;
4233 struct got_worktree *worktree = NULL;
4234 struct got_repository *repo = NULL;
4235 char *cwd = NULL, *path = NULL;
4236 struct got_pathlist_head paths;
4237 struct got_pathlist_entry *pe;
4238 int ch, can_recurse = 0, pflag = 0;
4239 FILE *patch_script_file = NULL;
4240 const char *patch_script_path = NULL;
4241 struct choose_patch_arg cpa;
4243 TAILQ_INIT(&paths);
4245 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4246 switch (ch) {
4247 case 'p':
4248 pflag = 1;
4249 break;
4250 case 'F':
4251 patch_script_path = optarg;
4252 break;
4253 case 'R':
4254 can_recurse = 1;
4255 break;
4256 default:
4257 usage_revert();
4258 /* NOTREACHED */
4262 argc -= optind;
4263 argv += optind;
4265 #ifndef PROFILE
4266 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4267 "unveil", NULL) == -1)
4268 err(1, "pledge");
4269 #endif
4270 if (argc < 1)
4271 usage_revert();
4272 if (patch_script_path && !pflag)
4273 errx(1, "-F option can only be used together with -p option");
4275 cwd = getcwd(NULL, 0);
4276 if (cwd == NULL) {
4277 error = got_error_from_errno("getcwd");
4278 goto done;
4280 error = got_worktree_open(&worktree, cwd);
4281 if (error)
4282 goto done;
4284 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4285 NULL);
4286 if (error != NULL)
4287 goto done;
4289 if (patch_script_path) {
4290 patch_script_file = fopen(patch_script_path, "r");
4291 if (patch_script_file == NULL) {
4292 error = got_error_from_errno2("fopen",
4293 patch_script_path);
4294 goto done;
4297 error = apply_unveil(got_repo_get_path(repo), 1,
4298 got_worktree_get_root_path(worktree));
4299 if (error)
4300 goto done;
4302 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4303 if (error)
4304 goto done;
4306 if (!can_recurse) {
4307 char *ondisk_path;
4308 struct stat sb;
4309 TAILQ_FOREACH(pe, &paths, entry) {
4310 if (asprintf(&ondisk_path, "%s/%s",
4311 got_worktree_get_root_path(worktree),
4312 pe->path) == -1) {
4313 error = got_error_from_errno("asprintf");
4314 goto done;
4316 if (lstat(ondisk_path, &sb) == -1) {
4317 if (errno == ENOENT) {
4318 free(ondisk_path);
4319 continue;
4321 error = got_error_from_errno2("lstat",
4322 ondisk_path);
4323 free(ondisk_path);
4324 goto done;
4326 free(ondisk_path);
4327 if (S_ISDIR(sb.st_mode)) {
4328 error = got_error_msg(GOT_ERR_BAD_PATH,
4329 "reverting directories requires -R option");
4330 goto done;
4335 cpa.patch_script_file = patch_script_file;
4336 cpa.action = "revert";
4337 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4338 pflag ? choose_patch : NULL, &cpa, repo);
4339 if (error)
4340 goto done;
4341 done:
4342 if (patch_script_file && fclose(patch_script_file) == EOF &&
4343 error == NULL)
4344 error = got_error_from_errno2("fclose", patch_script_path);
4345 if (repo)
4346 got_repo_close(repo);
4347 if (worktree)
4348 got_worktree_close(worktree);
4349 free(path);
4350 free(cwd);
4351 return error;
4354 __dead static void
4355 usage_commit(void)
4357 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4358 getprogname());
4359 exit(1);
4362 struct collect_commit_logmsg_arg {
4363 const char *cmdline_log;
4364 const char *editor;
4365 const char *worktree_path;
4366 const char *branch_name;
4367 const char *repo_path;
4368 char *logmsg_path;
4372 static const struct got_error *
4373 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4374 void *arg)
4376 char *initial_content = NULL;
4377 struct got_pathlist_entry *pe;
4378 const struct got_error *err = NULL;
4379 char *template = NULL;
4380 struct collect_commit_logmsg_arg *a = arg;
4381 int fd;
4382 size_t len;
4384 /* if a message was specified on the command line, just use it */
4385 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4386 len = strlen(a->cmdline_log) + 1;
4387 *logmsg = malloc(len + 1);
4388 if (*logmsg == NULL)
4389 return got_error_from_errno("malloc");
4390 strlcpy(*logmsg, a->cmdline_log, len);
4391 return NULL;
4394 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4395 return got_error_from_errno("asprintf");
4397 if (asprintf(&initial_content,
4398 "\n# changes to be committed on branch %s:\n",
4399 a->branch_name) == -1)
4400 return got_error_from_errno("asprintf");
4402 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4403 if (err)
4404 goto done;
4406 dprintf(fd, initial_content);
4408 TAILQ_FOREACH(pe, commitable_paths, entry) {
4409 struct got_commitable *ct = pe->data;
4410 dprintf(fd, "# %c %s\n",
4411 got_commitable_get_status(ct),
4412 got_commitable_get_path(ct));
4414 close(fd);
4416 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4417 done:
4418 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4419 unlink(a->logmsg_path);
4420 free(a->logmsg_path);
4421 a->logmsg_path = NULL;
4423 free(initial_content);
4424 free(template);
4426 /* Editor is done; we can now apply unveil(2) */
4427 if (err == NULL) {
4428 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4429 if (err) {
4430 free(*logmsg);
4431 *logmsg = NULL;
4434 return err;
4437 static const struct got_error *
4438 cmd_commit(int argc, char *argv[])
4440 const struct got_error *error = NULL;
4441 struct got_worktree *worktree = NULL;
4442 struct got_repository *repo = NULL;
4443 char *cwd = NULL, *id_str = NULL;
4444 struct got_object_id *id = NULL;
4445 const char *logmsg = NULL;
4446 struct collect_commit_logmsg_arg cl_arg;
4447 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4448 int ch, rebase_in_progress, histedit_in_progress;
4449 struct got_pathlist_head paths;
4451 TAILQ_INIT(&paths);
4452 cl_arg.logmsg_path = NULL;
4454 while ((ch = getopt(argc, argv, "m:")) != -1) {
4455 switch (ch) {
4456 case 'm':
4457 logmsg = optarg;
4458 break;
4459 default:
4460 usage_commit();
4461 /* NOTREACHED */
4465 argc -= optind;
4466 argv += optind;
4468 #ifndef PROFILE
4469 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4470 "unveil", NULL) == -1)
4471 err(1, "pledge");
4472 #endif
4473 cwd = getcwd(NULL, 0);
4474 if (cwd == NULL) {
4475 error = got_error_from_errno("getcwd");
4476 goto done;
4478 error = got_worktree_open(&worktree, cwd);
4479 if (error)
4480 goto done;
4482 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4483 if (error)
4484 goto done;
4485 if (rebase_in_progress) {
4486 error = got_error(GOT_ERR_REBASING);
4487 goto done;
4490 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4491 worktree);
4492 if (error)
4493 goto done;
4495 error = get_gitconfig_path(&gitconfig_path);
4496 if (error)
4497 goto done;
4498 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4499 gitconfig_path);
4500 if (error != NULL)
4501 goto done;
4503 error = get_author(&author, repo);
4504 if (error)
4505 return error;
4508 * unveil(2) traverses exec(2); if an editor is used we have
4509 * to apply unveil after the log message has been written.
4511 if (logmsg == NULL || strlen(logmsg) == 0)
4512 error = get_editor(&editor);
4513 else
4514 error = apply_unveil(got_repo_get_path(repo), 0,
4515 got_worktree_get_root_path(worktree));
4516 if (error)
4517 goto done;
4519 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4520 if (error)
4521 goto done;
4523 cl_arg.editor = editor;
4524 cl_arg.cmdline_log = logmsg;
4525 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4526 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4527 if (!histedit_in_progress) {
4528 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4529 error = got_error(GOT_ERR_COMMIT_BRANCH);
4530 goto done;
4532 cl_arg.branch_name += 11;
4534 cl_arg.repo_path = got_repo_get_path(repo);
4535 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4536 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4537 if (error) {
4538 if (cl_arg.logmsg_path)
4539 fprintf(stderr, "%s: log message preserved in %s\n",
4540 getprogname(), cl_arg.logmsg_path);
4541 goto done;
4544 if (cl_arg.logmsg_path)
4545 unlink(cl_arg.logmsg_path);
4547 error = got_object_id_str(&id_str, id);
4548 if (error)
4549 goto done;
4550 printf("Created commit %s\n", id_str);
4551 done:
4552 free(cl_arg.logmsg_path);
4553 if (repo)
4554 got_repo_close(repo);
4555 if (worktree)
4556 got_worktree_close(worktree);
4557 free(cwd);
4558 free(id_str);
4559 free(gitconfig_path);
4560 free(editor);
4561 free(author);
4562 return error;
4565 __dead static void
4566 usage_cherrypick(void)
4568 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4569 exit(1);
4572 static const struct got_error *
4573 cmd_cherrypick(int argc, char *argv[])
4575 const struct got_error *error = NULL;
4576 struct got_worktree *worktree = NULL;
4577 struct got_repository *repo = NULL;
4578 char *cwd = NULL, *commit_id_str = NULL;
4579 struct got_object_id *commit_id = NULL;
4580 struct got_commit_object *commit = NULL;
4581 struct got_object_qid *pid;
4582 struct got_reference *head_ref = NULL;
4583 int ch, did_something = 0;
4585 while ((ch = getopt(argc, argv, "")) != -1) {
4586 switch (ch) {
4587 default:
4588 usage_cherrypick();
4589 /* NOTREACHED */
4593 argc -= optind;
4594 argv += optind;
4596 #ifndef PROFILE
4597 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4598 "unveil", NULL) == -1)
4599 err(1, "pledge");
4600 #endif
4601 if (argc != 1)
4602 usage_cherrypick();
4604 cwd = getcwd(NULL, 0);
4605 if (cwd == NULL) {
4606 error = got_error_from_errno("getcwd");
4607 goto done;
4609 error = got_worktree_open(&worktree, cwd);
4610 if (error)
4611 goto done;
4613 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4614 NULL);
4615 if (error != NULL)
4616 goto done;
4618 error = apply_unveil(got_repo_get_path(repo), 0,
4619 got_worktree_get_root_path(worktree));
4620 if (error)
4621 goto done;
4623 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4624 GOT_OBJ_TYPE_COMMIT, repo);
4625 if (error != NULL) {
4626 struct got_reference *ref;
4627 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4628 goto done;
4629 error = got_ref_open(&ref, repo, argv[0], 0);
4630 if (error != NULL)
4631 goto done;
4632 error = got_ref_resolve(&commit_id, repo, ref);
4633 got_ref_close(ref);
4634 if (error != NULL)
4635 goto done;
4637 error = got_object_id_str(&commit_id_str, commit_id);
4638 if (error)
4639 goto done;
4641 error = got_ref_open(&head_ref, repo,
4642 got_worktree_get_head_ref_name(worktree), 0);
4643 if (error != NULL)
4644 goto done;
4646 error = check_same_branch(commit_id, head_ref, NULL, repo);
4647 if (error) {
4648 if (error->code != GOT_ERR_ANCESTRY)
4649 goto done;
4650 error = NULL;
4651 } else {
4652 error = got_error(GOT_ERR_SAME_BRANCH);
4653 goto done;
4656 error = got_object_open_as_commit(&commit, repo, commit_id);
4657 if (error)
4658 goto done;
4659 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4660 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4661 commit_id, repo, update_progress, &did_something, check_cancelled,
4662 NULL);
4663 if (error != NULL)
4664 goto done;
4666 if (did_something)
4667 printf("Merged commit %s\n", commit_id_str);
4668 done:
4669 if (commit)
4670 got_object_commit_close(commit);
4671 free(commit_id_str);
4672 if (head_ref)
4673 got_ref_close(head_ref);
4674 if (worktree)
4675 got_worktree_close(worktree);
4676 if (repo)
4677 got_repo_close(repo);
4678 return error;
4681 __dead static void
4682 usage_backout(void)
4684 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4685 exit(1);
4688 static const struct got_error *
4689 cmd_backout(int argc, char *argv[])
4691 const struct got_error *error = NULL;
4692 struct got_worktree *worktree = NULL;
4693 struct got_repository *repo = NULL;
4694 char *cwd = NULL, *commit_id_str = NULL;
4695 struct got_object_id *commit_id = NULL;
4696 struct got_commit_object *commit = NULL;
4697 struct got_object_qid *pid;
4698 struct got_reference *head_ref = NULL;
4699 int ch, did_something = 0;
4701 while ((ch = getopt(argc, argv, "")) != -1) {
4702 switch (ch) {
4703 default:
4704 usage_backout();
4705 /* NOTREACHED */
4709 argc -= optind;
4710 argv += optind;
4712 #ifndef PROFILE
4713 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4714 "unveil", NULL) == -1)
4715 err(1, "pledge");
4716 #endif
4717 if (argc != 1)
4718 usage_backout();
4720 cwd = getcwd(NULL, 0);
4721 if (cwd == NULL) {
4722 error = got_error_from_errno("getcwd");
4723 goto done;
4725 error = got_worktree_open(&worktree, cwd);
4726 if (error)
4727 goto done;
4729 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4730 NULL);
4731 if (error != NULL)
4732 goto done;
4734 error = apply_unveil(got_repo_get_path(repo), 0,
4735 got_worktree_get_root_path(worktree));
4736 if (error)
4737 goto done;
4739 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4740 GOT_OBJ_TYPE_COMMIT, repo);
4741 if (error != NULL) {
4742 struct got_reference *ref;
4743 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4744 goto done;
4745 error = got_ref_open(&ref, repo, argv[0], 0);
4746 if (error != NULL)
4747 goto done;
4748 error = got_ref_resolve(&commit_id, repo, ref);
4749 got_ref_close(ref);
4750 if (error != NULL)
4751 goto done;
4753 error = got_object_id_str(&commit_id_str, commit_id);
4754 if (error)
4755 goto done;
4757 error = got_ref_open(&head_ref, repo,
4758 got_worktree_get_head_ref_name(worktree), 0);
4759 if (error != NULL)
4760 goto done;
4762 error = check_same_branch(commit_id, head_ref, NULL, repo);
4763 if (error)
4764 goto done;
4766 error = got_object_open_as_commit(&commit, repo, commit_id);
4767 if (error)
4768 goto done;
4769 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4770 if (pid == NULL) {
4771 error = got_error(GOT_ERR_ROOT_COMMIT);
4772 goto done;
4775 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4776 update_progress, &did_something, check_cancelled, NULL);
4777 if (error != NULL)
4778 goto done;
4780 if (did_something)
4781 printf("Backed out commit %s\n", commit_id_str);
4782 done:
4783 if (commit)
4784 got_object_commit_close(commit);
4785 free(commit_id_str);
4786 if (head_ref)
4787 got_ref_close(head_ref);
4788 if (worktree)
4789 got_worktree_close(worktree);
4790 if (repo)
4791 got_repo_close(repo);
4792 return error;
4795 __dead static void
4796 usage_rebase(void)
4798 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4799 getprogname());
4800 exit(1);
4803 void
4804 trim_logmsg(char *logmsg, int limit)
4806 char *nl;
4807 size_t len;
4809 len = strlen(logmsg);
4810 if (len > limit)
4811 len = limit;
4812 logmsg[len] = '\0';
4813 nl = strchr(logmsg, '\n');
4814 if (nl)
4815 *nl = '\0';
4818 static const struct got_error *
4819 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4821 const struct got_error *err;
4822 char *logmsg0 = NULL;
4823 const char *s;
4825 err = got_object_commit_get_logmsg(&logmsg0, commit);
4826 if (err)
4827 return err;
4829 s = logmsg0;
4830 while (isspace((unsigned char)s[0]))
4831 s++;
4833 *logmsg = strdup(s);
4834 if (*logmsg == NULL) {
4835 err = got_error_from_errno("strdup");
4836 goto done;
4839 trim_logmsg(*logmsg, limit);
4840 done:
4841 free(logmsg0);
4842 return err;
4845 static const struct got_error *
4846 show_rebase_progress(struct got_commit_object *commit,
4847 struct got_object_id *old_id, struct got_object_id *new_id)
4849 const struct got_error *err;
4850 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4852 err = got_object_id_str(&old_id_str, old_id);
4853 if (err)
4854 goto done;
4856 if (new_id) {
4857 err = got_object_id_str(&new_id_str, new_id);
4858 if (err)
4859 goto done;
4862 old_id_str[12] = '\0';
4863 if (new_id_str)
4864 new_id_str[12] = '\0';
4866 err = get_short_logmsg(&logmsg, 42, commit);
4867 if (err)
4868 goto done;
4870 printf("%s -> %s: %s\n", old_id_str,
4871 new_id_str ? new_id_str : "no-op change", logmsg);
4872 done:
4873 free(old_id_str);
4874 free(new_id_str);
4875 return err;
4878 static const struct got_error *
4879 rebase_progress(void *arg, unsigned char status, const char *path)
4881 unsigned char *rebase_status = arg;
4883 while (path[0] == '/')
4884 path++;
4885 printf("%c %s\n", status, path);
4887 if (*rebase_status == GOT_STATUS_CONFLICT)
4888 return NULL;
4889 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4890 *rebase_status = status;
4891 return NULL;
4894 static const struct got_error *
4895 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4896 struct got_reference *branch, struct got_reference *new_base_branch,
4897 struct got_reference *tmp_branch, struct got_repository *repo)
4899 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4900 return got_worktree_rebase_complete(worktree, fileindex,
4901 new_base_branch, tmp_branch, branch, repo);
4904 static const struct got_error *
4905 rebase_commit(struct got_pathlist_head *merged_paths,
4906 struct got_worktree *worktree, struct got_fileindex *fileindex,
4907 struct got_reference *tmp_branch,
4908 struct got_object_id *commit_id, struct got_repository *repo)
4910 const struct got_error *error;
4911 struct got_commit_object *commit;
4912 struct got_object_id *new_commit_id;
4914 error = got_object_open_as_commit(&commit, repo, commit_id);
4915 if (error)
4916 return error;
4918 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4919 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4920 if (error) {
4921 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4922 goto done;
4923 error = show_rebase_progress(commit, commit_id, NULL);
4924 } else {
4925 error = show_rebase_progress(commit, commit_id, new_commit_id);
4926 free(new_commit_id);
4928 done:
4929 got_object_commit_close(commit);
4930 return error;
4933 struct check_path_prefix_arg {
4934 const char *path_prefix;
4935 size_t len;
4936 int errcode;
4939 static const struct got_error *
4940 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4941 struct got_blob_object *blob2, struct got_object_id *id1,
4942 struct got_object_id *id2, const char *path1, const char *path2,
4943 struct got_repository *repo)
4945 struct check_path_prefix_arg *a = arg;
4947 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4948 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4949 return got_error(a->errcode);
4951 return NULL;
4954 static const struct got_error *
4955 check_path_prefix(struct got_object_id *parent_id,
4956 struct got_object_id *commit_id, const char *path_prefix,
4957 int errcode, struct got_repository *repo)
4959 const struct got_error *err;
4960 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4961 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4962 struct check_path_prefix_arg cpp_arg;
4964 if (got_path_is_root_dir(path_prefix))
4965 return NULL;
4967 err = got_object_open_as_commit(&commit, repo, commit_id);
4968 if (err)
4969 goto done;
4971 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4972 if (err)
4973 goto done;
4975 err = got_object_open_as_tree(&tree1, repo,
4976 got_object_commit_get_tree_id(parent_commit));
4977 if (err)
4978 goto done;
4980 err = got_object_open_as_tree(&tree2, repo,
4981 got_object_commit_get_tree_id(commit));
4982 if (err)
4983 goto done;
4985 cpp_arg.path_prefix = path_prefix;
4986 while (cpp_arg.path_prefix[0] == '/')
4987 cpp_arg.path_prefix++;
4988 cpp_arg.len = strlen(cpp_arg.path_prefix);
4989 cpp_arg.errcode = errcode;
4990 err = got_diff_tree(tree1, tree2, "", "", repo,
4991 check_path_prefix_in_diff, &cpp_arg, 0);
4992 done:
4993 if (tree1)
4994 got_object_tree_close(tree1);
4995 if (tree2)
4996 got_object_tree_close(tree2);
4997 if (commit)
4998 got_object_commit_close(commit);
4999 if (parent_commit)
5000 got_object_commit_close(parent_commit);
5001 return err;
5004 static const struct got_error *
5005 collect_commits(struct got_object_id_queue *commits,
5006 struct got_object_id *initial_commit_id,
5007 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5008 const char *path_prefix, int path_prefix_errcode,
5009 struct got_repository *repo)
5011 const struct got_error *err = NULL;
5012 struct got_commit_graph *graph = NULL;
5013 struct got_object_id *parent_id = NULL;
5014 struct got_object_qid *qid;
5015 struct got_object_id *commit_id = initial_commit_id;
5017 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5018 if (err)
5019 return err;
5021 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5022 check_cancelled, NULL);
5023 if (err)
5024 goto done;
5025 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5026 err = got_commit_graph_iter_next(&parent_id, graph);
5027 if (err) {
5028 if (err->code == GOT_ERR_ITER_COMPLETED) {
5029 err = got_error_msg(GOT_ERR_ANCESTRY,
5030 "ran out of commits to rebase before "
5031 "youngest common ancestor commit has "
5032 "been reached?!?");
5033 goto done;
5034 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5035 goto done;
5036 err = got_commit_graph_fetch_commits(graph, 1, repo,
5037 check_cancelled, NULL);
5038 if (err)
5039 goto done;
5040 } else {
5041 err = check_path_prefix(parent_id, commit_id,
5042 path_prefix, path_prefix_errcode, repo);
5043 if (err)
5044 goto done;
5046 err = got_object_qid_alloc(&qid, commit_id);
5047 if (err)
5048 goto done;
5049 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5050 commit_id = parent_id;
5053 done:
5054 got_commit_graph_close(graph);
5055 return err;
5058 static const struct got_error *
5059 cmd_rebase(int argc, char *argv[])
5061 const struct got_error *error = NULL;
5062 struct got_worktree *worktree = NULL;
5063 struct got_repository *repo = NULL;
5064 struct got_fileindex *fileindex = NULL;
5065 char *cwd = NULL;
5066 struct got_reference *branch = NULL;
5067 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5068 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5069 struct got_object_id *resume_commit_id = NULL;
5070 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5071 struct got_commit_object *commit = NULL;
5072 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5073 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5074 struct got_object_id_queue commits;
5075 struct got_pathlist_head merged_paths;
5076 const struct got_object_id_queue *parent_ids;
5077 struct got_object_qid *qid, *pid;
5079 SIMPLEQ_INIT(&commits);
5080 TAILQ_INIT(&merged_paths);
5082 while ((ch = getopt(argc, argv, "ac")) != -1) {
5083 switch (ch) {
5084 case 'a':
5085 abort_rebase = 1;
5086 break;
5087 case 'c':
5088 continue_rebase = 1;
5089 break;
5090 default:
5091 usage_rebase();
5092 /* NOTREACHED */
5096 argc -= optind;
5097 argv += optind;
5099 #ifndef PROFILE
5100 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5101 "unveil", NULL) == -1)
5102 err(1, "pledge");
5103 #endif
5104 if (abort_rebase && continue_rebase)
5105 usage_rebase();
5106 else if (abort_rebase || continue_rebase) {
5107 if (argc != 0)
5108 usage_rebase();
5109 } else if (argc != 1)
5110 usage_rebase();
5112 cwd = getcwd(NULL, 0);
5113 if (cwd == NULL) {
5114 error = got_error_from_errno("getcwd");
5115 goto done;
5117 error = got_worktree_open(&worktree, cwd);
5118 if (error)
5119 goto done;
5121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5122 NULL);
5123 if (error != NULL)
5124 goto done;
5126 error = apply_unveil(got_repo_get_path(repo), 0,
5127 got_worktree_get_root_path(worktree));
5128 if (error)
5129 goto done;
5131 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5132 if (error)
5133 goto done;
5135 if (abort_rebase) {
5136 int did_something;
5137 if (!rebase_in_progress) {
5138 error = got_error(GOT_ERR_NOT_REBASING);
5139 goto done;
5141 error = got_worktree_rebase_continue(&resume_commit_id,
5142 &new_base_branch, &tmp_branch, &branch, &fileindex,
5143 worktree, repo);
5144 if (error)
5145 goto done;
5146 printf("Switching work tree to %s\n",
5147 got_ref_get_symref_target(new_base_branch));
5148 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5149 new_base_branch, update_progress, &did_something);
5150 if (error)
5151 goto done;
5152 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5153 goto done; /* nothing else to do */
5156 if (continue_rebase) {
5157 if (!rebase_in_progress) {
5158 error = got_error(GOT_ERR_NOT_REBASING);
5159 goto done;
5161 error = got_worktree_rebase_continue(&resume_commit_id,
5162 &new_base_branch, &tmp_branch, &branch, &fileindex,
5163 worktree, repo);
5164 if (error)
5165 goto done;
5167 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5168 resume_commit_id, repo);
5169 if (error)
5170 goto done;
5172 yca_id = got_object_id_dup(resume_commit_id);
5173 if (yca_id == NULL) {
5174 error = got_error_from_errno("got_object_id_dup");
5175 goto done;
5177 } else {
5178 error = got_ref_open(&branch, repo, argv[0], 0);
5179 if (error != NULL)
5180 goto done;
5183 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5184 if (error)
5185 goto done;
5187 if (!continue_rebase) {
5188 struct got_object_id *base_commit_id;
5190 base_commit_id = got_worktree_get_base_commit_id(worktree);
5191 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5192 base_commit_id, branch_head_commit_id, repo,
5193 check_cancelled, NULL);
5194 if (error)
5195 goto done;
5196 if (yca_id == NULL) {
5197 error = got_error_msg(GOT_ERR_ANCESTRY,
5198 "specified branch shares no common ancestry "
5199 "with work tree's branch");
5200 goto done;
5203 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5204 if (error) {
5205 if (error->code != GOT_ERR_ANCESTRY)
5206 goto done;
5207 error = NULL;
5208 } else {
5209 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5210 "specified branch resolves to a commit which "
5211 "is already contained in work tree's branch");
5212 goto done;
5214 error = got_worktree_rebase_prepare(&new_base_branch,
5215 &tmp_branch, &fileindex, worktree, branch, repo);
5216 if (error)
5217 goto done;
5220 commit_id = branch_head_commit_id;
5221 error = got_object_open_as_commit(&commit, repo, commit_id);
5222 if (error)
5223 goto done;
5225 parent_ids = got_object_commit_get_parent_ids(commit);
5226 pid = SIMPLEQ_FIRST(parent_ids);
5227 if (pid == NULL) {
5228 if (!continue_rebase) {
5229 int did_something;
5230 error = got_worktree_rebase_abort(worktree, fileindex,
5231 repo, new_base_branch, update_progress,
5232 &did_something);
5233 if (error)
5234 goto done;
5235 printf("Rebase of %s aborted\n",
5236 got_ref_get_name(branch));
5238 error = got_error(GOT_ERR_EMPTY_REBASE);
5239 goto done;
5241 error = collect_commits(&commits, commit_id, pid->id,
5242 yca_id, got_worktree_get_path_prefix(worktree),
5243 GOT_ERR_REBASE_PATH, repo);
5244 got_object_commit_close(commit);
5245 commit = NULL;
5246 if (error)
5247 goto done;
5249 if (SIMPLEQ_EMPTY(&commits)) {
5250 if (continue_rebase)
5251 error = rebase_complete(worktree, fileindex,
5252 branch, new_base_branch, tmp_branch, repo);
5253 else
5254 error = got_error(GOT_ERR_EMPTY_REBASE);
5255 goto done;
5258 pid = NULL;
5259 SIMPLEQ_FOREACH(qid, &commits, entry) {
5260 commit_id = qid->id;
5261 parent_id = pid ? pid->id : yca_id;
5262 pid = qid;
5264 error = got_worktree_rebase_merge_files(&merged_paths,
5265 worktree, fileindex, parent_id, commit_id, repo,
5266 rebase_progress, &rebase_status, check_cancelled, NULL);
5267 if (error)
5268 goto done;
5270 if (rebase_status == GOT_STATUS_CONFLICT) {
5271 got_worktree_rebase_pathlist_free(&merged_paths);
5272 break;
5275 error = rebase_commit(&merged_paths, worktree, fileindex,
5276 tmp_branch, commit_id, repo);
5277 got_worktree_rebase_pathlist_free(&merged_paths);
5278 if (error)
5279 goto done;
5282 if (rebase_status == GOT_STATUS_CONFLICT) {
5283 error = got_worktree_rebase_postpone(worktree, fileindex);
5284 if (error)
5285 goto done;
5286 error = got_error_msg(GOT_ERR_CONFLICTS,
5287 "conflicts must be resolved before rebasing can continue");
5288 } else
5289 error = rebase_complete(worktree, fileindex, branch,
5290 new_base_branch, tmp_branch, repo);
5291 done:
5292 got_object_id_queue_free(&commits);
5293 free(branch_head_commit_id);
5294 free(resume_commit_id);
5295 free(yca_id);
5296 if (commit)
5297 got_object_commit_close(commit);
5298 if (branch)
5299 got_ref_close(branch);
5300 if (new_base_branch)
5301 got_ref_close(new_base_branch);
5302 if (tmp_branch)
5303 got_ref_close(tmp_branch);
5304 if (worktree)
5305 got_worktree_close(worktree);
5306 if (repo)
5307 got_repo_close(repo);
5308 return error;
5311 __dead static void
5312 usage_histedit(void)
5314 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5315 getprogname());
5316 exit(1);
5319 #define GOT_HISTEDIT_PICK 'p'
5320 #define GOT_HISTEDIT_EDIT 'e'
5321 #define GOT_HISTEDIT_FOLD 'f'
5322 #define GOT_HISTEDIT_DROP 'd'
5323 #define GOT_HISTEDIT_MESG 'm'
5325 static struct got_histedit_cmd {
5326 unsigned char code;
5327 const char *name;
5328 const char *desc;
5329 } got_histedit_cmds[] = {
5330 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5331 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5332 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5333 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5334 { GOT_HISTEDIT_MESG, "mesg",
5335 "single-line log message for commit above (open editor if empty)" },
5338 struct got_histedit_list_entry {
5339 TAILQ_ENTRY(got_histedit_list_entry) entry;
5340 struct got_object_id *commit_id;
5341 const struct got_histedit_cmd *cmd;
5342 char *logmsg;
5344 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5346 static const struct got_error *
5347 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5348 FILE *f, struct got_repository *repo)
5350 const struct got_error *err = NULL;
5351 char *logmsg = NULL, *id_str = NULL;
5352 struct got_commit_object *commit = NULL;
5353 int n;
5355 err = got_object_open_as_commit(&commit, repo, commit_id);
5356 if (err)
5357 goto done;
5359 err = get_short_logmsg(&logmsg, 34, commit);
5360 if (err)
5361 goto done;
5363 err = got_object_id_str(&id_str, commit_id);
5364 if (err)
5365 goto done;
5367 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5368 if (n < 0)
5369 err = got_ferror(f, GOT_ERR_IO);
5370 done:
5371 if (commit)
5372 got_object_commit_close(commit);
5373 free(id_str);
5374 free(logmsg);
5375 return err;
5378 static const struct got_error *
5379 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5380 struct got_repository *repo)
5382 const struct got_error *err = NULL;
5383 struct got_object_qid *qid;
5385 if (SIMPLEQ_EMPTY(commits))
5386 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5388 SIMPLEQ_FOREACH(qid, commits, entry) {
5389 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5390 f, repo);
5391 if (err)
5392 break;
5395 return err;
5398 static const struct got_error *
5399 write_cmd_list(FILE *f)
5401 const struct got_error *err = NULL;
5402 int n, i;
5404 n = fprintf(f, "# Available histedit commands:\n");
5405 if (n < 0)
5406 return got_ferror(f, GOT_ERR_IO);
5408 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5409 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5410 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5411 cmd->desc);
5412 if (n < 0) {
5413 err = got_ferror(f, GOT_ERR_IO);
5414 break;
5417 n = fprintf(f, "# Commits will be processed in order from top to "
5418 "bottom of this file.\n");
5419 if (n < 0)
5420 return got_ferror(f, GOT_ERR_IO);
5421 return err;
5424 static const struct got_error *
5425 histedit_syntax_error(int lineno)
5427 static char msg[42];
5428 int ret;
5430 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5431 lineno);
5432 if (ret == -1 || ret >= sizeof(msg))
5433 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5435 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5438 static const struct got_error *
5439 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5440 char *logmsg, struct got_repository *repo)
5442 const struct got_error *err;
5443 struct got_commit_object *folded_commit = NULL;
5444 char *id_str, *folded_logmsg = NULL;
5446 err = got_object_id_str(&id_str, hle->commit_id);
5447 if (err)
5448 return err;
5450 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5451 if (err)
5452 goto done;
5454 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5455 if (err)
5456 goto done;
5457 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5458 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5459 folded_logmsg) == -1) {
5460 err = got_error_from_errno("asprintf");
5461 goto done;
5463 done:
5464 if (folded_commit)
5465 got_object_commit_close(folded_commit);
5466 free(id_str);
5467 free(folded_logmsg);
5468 return err;
5471 static struct got_histedit_list_entry *
5472 get_folded_commits(struct got_histedit_list_entry *hle)
5474 struct got_histedit_list_entry *prev, *folded = NULL;
5476 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5477 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5478 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5479 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5480 folded = prev;
5481 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5484 return folded;
5487 static const struct got_error *
5488 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5489 struct got_repository *repo)
5491 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5492 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5493 const struct got_error *err = NULL;
5494 struct got_commit_object *commit = NULL;
5495 int fd;
5496 struct got_histedit_list_entry *folded = NULL;
5498 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5499 if (err)
5500 return err;
5502 folded = get_folded_commits(hle);
5503 if (folded) {
5504 while (folded != hle) {
5505 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5506 folded = TAILQ_NEXT(folded, entry);
5507 continue;
5509 err = append_folded_commit_msg(&new_msg, folded,
5510 logmsg, repo);
5511 if (err)
5512 goto done;
5513 free(logmsg);
5514 logmsg = new_msg;
5515 folded = TAILQ_NEXT(folded, entry);
5519 err = got_object_id_str(&id_str, hle->commit_id);
5520 if (err)
5521 goto done;
5522 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5523 if (err)
5524 goto done;
5525 if (asprintf(&new_msg,
5526 "%s\n# original log message of commit %s: %s",
5527 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5528 err = got_error_from_errno("asprintf");
5529 goto done;
5531 free(logmsg);
5532 logmsg = new_msg;
5534 err = got_object_id_str(&id_str, hle->commit_id);
5535 if (err)
5536 goto done;
5538 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5539 if (err)
5540 goto done;
5542 dprintf(fd, logmsg);
5543 close(fd);
5545 err = get_editor(&editor);
5546 if (err)
5547 goto done;
5549 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5550 if (err) {
5551 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5552 goto done;
5553 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5555 done:
5556 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5557 err = got_error_from_errno2("unlink", logmsg_path);
5558 free(logmsg_path);
5559 free(logmsg);
5560 free(orig_logmsg);
5561 free(editor);
5562 if (commit)
5563 got_object_commit_close(commit);
5564 return err;
5567 static const struct got_error *
5568 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5569 FILE *f, struct got_repository *repo)
5571 const struct got_error *err = NULL;
5572 char *line = NULL, *p, *end;
5573 size_t size;
5574 ssize_t len;
5575 int lineno = 0, i;
5576 const struct got_histedit_cmd *cmd;
5577 struct got_object_id *commit_id = NULL;
5578 struct got_histedit_list_entry *hle = NULL;
5580 for (;;) {
5581 len = getline(&line, &size, f);
5582 if (len == -1) {
5583 const struct got_error *getline_err;
5584 if (feof(f))
5585 break;
5586 getline_err = got_error_from_errno("getline");
5587 err = got_ferror(f, getline_err->code);
5588 break;
5590 lineno++;
5591 p = line;
5592 while (isspace((unsigned char)p[0]))
5593 p++;
5594 if (p[0] == '#' || p[0] == '\0') {
5595 free(line);
5596 line = NULL;
5597 continue;
5599 cmd = NULL;
5600 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5601 cmd = &got_histedit_cmds[i];
5602 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5603 isspace((unsigned char)p[strlen(cmd->name)])) {
5604 p += strlen(cmd->name);
5605 break;
5607 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5608 p++;
5609 break;
5612 if (i == nitems(got_histedit_cmds)) {
5613 err = histedit_syntax_error(lineno);
5614 break;
5616 while (isspace((unsigned char)p[0]))
5617 p++;
5618 if (cmd->code == GOT_HISTEDIT_MESG) {
5619 if (hle == NULL || hle->logmsg != NULL) {
5620 err = got_error(GOT_ERR_HISTEDIT_CMD);
5621 break;
5623 if (p[0] == '\0') {
5624 err = histedit_edit_logmsg(hle, repo);
5625 if (err)
5626 break;
5627 } else {
5628 hle->logmsg = strdup(p);
5629 if (hle->logmsg == NULL) {
5630 err = got_error_from_errno("strdup");
5631 break;
5634 free(line);
5635 line = NULL;
5636 continue;
5637 } else {
5638 end = p;
5639 while (end[0] && !isspace((unsigned char)end[0]))
5640 end++;
5641 *end = '\0';
5643 err = got_object_resolve_id_str(&commit_id, repo, p);
5644 if (err) {
5645 /* override error code */
5646 err = histedit_syntax_error(lineno);
5647 break;
5650 hle = malloc(sizeof(*hle));
5651 if (hle == NULL) {
5652 err = got_error_from_errno("malloc");
5653 break;
5655 hle->cmd = cmd;
5656 hle->commit_id = commit_id;
5657 hle->logmsg = NULL;
5658 commit_id = NULL;
5659 free(line);
5660 line = NULL;
5661 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5664 free(line);
5665 free(commit_id);
5666 return err;
5669 static const struct got_error *
5670 histedit_check_script(struct got_histedit_list *histedit_cmds,
5671 struct got_object_id_queue *commits, struct got_repository *repo)
5673 const struct got_error *err = NULL;
5674 struct got_object_qid *qid;
5675 struct got_histedit_list_entry *hle;
5676 static char msg[80];
5677 char *id_str;
5679 if (TAILQ_EMPTY(histedit_cmds))
5680 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5681 "histedit script contains no commands");
5682 if (SIMPLEQ_EMPTY(commits))
5683 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5685 SIMPLEQ_FOREACH(qid, commits, entry) {
5686 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5687 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5688 break;
5690 if (hle == NULL) {
5691 err = got_object_id_str(&id_str, qid->id);
5692 if (err)
5693 return err;
5694 snprintf(msg, sizeof(msg),
5695 "commit %s missing from histedit script", id_str);
5696 free(id_str);
5697 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5701 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5702 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5703 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5704 "last commit in histedit script cannot be folded");
5706 return NULL;
5709 static const struct got_error *
5710 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5711 const char *path, struct got_object_id_queue *commits,
5712 struct got_repository *repo)
5714 const struct got_error *err = NULL;
5715 char *editor;
5716 FILE *f = NULL;
5718 err = get_editor(&editor);
5719 if (err)
5720 return err;
5722 if (spawn_editor(editor, path) == -1) {
5723 err = got_error_from_errno("failed spawning editor");
5724 goto done;
5727 f = fopen(path, "r");
5728 if (f == NULL) {
5729 err = got_error_from_errno("fopen");
5730 goto done;
5732 err = histedit_parse_list(histedit_cmds, f, repo);
5733 if (err)
5734 goto done;
5736 err = histedit_check_script(histedit_cmds, commits, repo);
5737 done:
5738 if (f && fclose(f) != 0 && err == NULL)
5739 err = got_error_from_errno("fclose");
5740 free(editor);
5741 return err;
5744 static const struct got_error *
5745 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5746 struct got_object_id_queue *, const char *, struct got_repository *);
5748 static const struct got_error *
5749 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5750 struct got_object_id_queue *commits, struct got_repository *repo)
5752 const struct got_error *err;
5753 FILE *f = NULL;
5754 char *path = NULL;
5756 err = got_opentemp_named(&path, &f, "got-histedit");
5757 if (err)
5758 return err;
5760 err = write_cmd_list(f);
5761 if (err)
5762 goto done;
5764 err = histedit_write_commit_list(commits, f, repo);
5765 if (err)
5766 goto done;
5768 if (fclose(f) != 0) {
5769 err = got_error_from_errno("fclose");
5770 goto done;
5772 f = NULL;
5774 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5775 if (err) {
5776 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5777 err->code != GOT_ERR_HISTEDIT_CMD)
5778 goto done;
5779 err = histedit_edit_list_retry(histedit_cmds, err,
5780 commits, path, repo);
5782 done:
5783 if (f && fclose(f) != 0 && err == NULL)
5784 err = got_error_from_errno("fclose");
5785 if (path && unlink(path) != 0 && err == NULL)
5786 err = got_error_from_errno2("unlink", path);
5787 free(path);
5788 return err;
5791 static const struct got_error *
5792 histedit_save_list(struct got_histedit_list *histedit_cmds,
5793 struct got_worktree *worktree, struct got_repository *repo)
5795 const struct got_error *err = NULL;
5796 char *path = NULL;
5797 FILE *f = NULL;
5798 struct got_histedit_list_entry *hle;
5799 struct got_commit_object *commit = NULL;
5801 err = got_worktree_get_histedit_script_path(&path, worktree);
5802 if (err)
5803 return err;
5805 f = fopen(path, "w");
5806 if (f == NULL) {
5807 err = got_error_from_errno2("fopen", path);
5808 goto done;
5810 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5811 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5812 repo);
5813 if (err)
5814 break;
5816 if (hle->logmsg) {
5817 int n = fprintf(f, "%c %s\n",
5818 GOT_HISTEDIT_MESG, hle->logmsg);
5819 if (n < 0) {
5820 err = got_ferror(f, GOT_ERR_IO);
5821 break;
5825 done:
5826 if (f && fclose(f) != 0 && err == NULL)
5827 err = got_error_from_errno("fclose");
5828 free(path);
5829 if (commit)
5830 got_object_commit_close(commit);
5831 return err;
5834 void
5835 histedit_free_list(struct got_histedit_list *histedit_cmds)
5837 struct got_histedit_list_entry *hle;
5839 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5840 TAILQ_REMOVE(histedit_cmds, hle, entry);
5841 free(hle);
5845 static const struct got_error *
5846 histedit_load_list(struct got_histedit_list *histedit_cmds,
5847 const char *path, struct got_repository *repo)
5849 const struct got_error *err = NULL;
5850 FILE *f = NULL;
5852 f = fopen(path, "r");
5853 if (f == NULL) {
5854 err = got_error_from_errno2("fopen", path);
5855 goto done;
5858 err = histedit_parse_list(histedit_cmds, f, repo);
5859 done:
5860 if (f && fclose(f) != 0 && err == NULL)
5861 err = got_error_from_errno("fclose");
5862 return err;
5865 static const struct got_error *
5866 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5867 const struct got_error *edit_err, struct got_object_id_queue *commits,
5868 const char *path, struct got_repository *repo)
5870 const struct got_error *err = NULL, *prev_err = edit_err;
5871 int resp = ' ';
5873 while (resp != 'c' && resp != 'r' && resp != 'a') {
5874 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5875 "or (a)bort: ", getprogname(), prev_err->msg);
5876 resp = getchar();
5877 if (resp == '\n')
5878 resp = getchar();
5879 if (resp == 'c') {
5880 histedit_free_list(histedit_cmds);
5881 err = histedit_run_editor(histedit_cmds, path, commits,
5882 repo);
5883 if (err) {
5884 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5885 err->code != GOT_ERR_HISTEDIT_CMD)
5886 break;
5887 prev_err = err;
5888 resp = ' ';
5889 continue;
5891 break;
5892 } else if (resp == 'r') {
5893 histedit_free_list(histedit_cmds);
5894 err = histedit_edit_script(histedit_cmds,
5895 commits, repo);
5896 if (err) {
5897 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5898 err->code != GOT_ERR_HISTEDIT_CMD)
5899 break;
5900 prev_err = err;
5901 resp = ' ';
5902 continue;
5904 break;
5905 } else if (resp == 'a') {
5906 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5907 break;
5908 } else
5909 printf("invalid response '%c'\n", resp);
5912 return err;
5915 static const struct got_error *
5916 histedit_complete(struct got_worktree *worktree,
5917 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5918 struct got_reference *branch, struct got_repository *repo)
5920 printf("Switching work tree to %s\n",
5921 got_ref_get_symref_target(branch));
5922 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5923 branch, repo);
5926 static const struct got_error *
5927 show_histedit_progress(struct got_commit_object *commit,
5928 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5930 const struct got_error *err;
5931 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5933 err = got_object_id_str(&old_id_str, hle->commit_id);
5934 if (err)
5935 goto done;
5937 if (new_id) {
5938 err = got_object_id_str(&new_id_str, new_id);
5939 if (err)
5940 goto done;
5943 old_id_str[12] = '\0';
5944 if (new_id_str)
5945 new_id_str[12] = '\0';
5947 if (hle->logmsg) {
5948 logmsg = strdup(hle->logmsg);
5949 if (logmsg == NULL) {
5950 err = got_error_from_errno("strdup");
5951 goto done;
5953 trim_logmsg(logmsg, 42);
5954 } else {
5955 err = get_short_logmsg(&logmsg, 42, commit);
5956 if (err)
5957 goto done;
5960 switch (hle->cmd->code) {
5961 case GOT_HISTEDIT_PICK:
5962 case GOT_HISTEDIT_EDIT:
5963 printf("%s -> %s: %s\n", old_id_str,
5964 new_id_str ? new_id_str : "no-op change", logmsg);
5965 break;
5966 case GOT_HISTEDIT_DROP:
5967 case GOT_HISTEDIT_FOLD:
5968 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5969 logmsg);
5970 break;
5971 default:
5972 break;
5975 done:
5976 free(old_id_str);
5977 free(new_id_str);
5978 return err;
5981 static const struct got_error *
5982 histedit_commit(struct got_pathlist_head *merged_paths,
5983 struct got_worktree *worktree, struct got_fileindex *fileindex,
5984 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5985 struct got_repository *repo)
5987 const struct got_error *err;
5988 struct got_commit_object *commit;
5989 struct got_object_id *new_commit_id;
5991 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5992 && hle->logmsg == NULL) {
5993 err = histedit_edit_logmsg(hle, repo);
5994 if (err)
5995 return err;
5998 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5999 if (err)
6000 return err;
6002 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6003 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6004 hle->logmsg, repo);
6005 if (err) {
6006 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6007 goto done;
6008 err = show_histedit_progress(commit, hle, NULL);
6009 } else {
6010 err = show_histedit_progress(commit, hle, new_commit_id);
6011 free(new_commit_id);
6013 done:
6014 got_object_commit_close(commit);
6015 return err;
6018 static const struct got_error *
6019 histedit_skip_commit(struct got_histedit_list_entry *hle,
6020 struct got_worktree *worktree, struct got_repository *repo)
6022 const struct got_error *error;
6023 struct got_commit_object *commit;
6025 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6026 repo);
6027 if (error)
6028 return error;
6030 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6031 if (error)
6032 return error;
6034 error = show_histedit_progress(commit, hle, NULL);
6035 got_object_commit_close(commit);
6036 return error;
6039 static const struct got_error *
6040 cmd_histedit(int argc, char *argv[])
6042 const struct got_error *error = NULL;
6043 struct got_worktree *worktree = NULL;
6044 struct got_fileindex *fileindex = NULL;
6045 struct got_repository *repo = NULL;
6046 char *cwd = NULL;
6047 struct got_reference *branch = NULL;
6048 struct got_reference *tmp_branch = NULL;
6049 struct got_object_id *resume_commit_id = NULL;
6050 struct got_object_id *base_commit_id = NULL;
6051 struct got_object_id *head_commit_id = NULL;
6052 struct got_commit_object *commit = NULL;
6053 int ch, rebase_in_progress = 0, did_something;
6054 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6055 const char *edit_script_path = NULL;
6056 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6057 struct got_object_id_queue commits;
6058 struct got_pathlist_head merged_paths;
6059 const struct got_object_id_queue *parent_ids;
6060 struct got_object_qid *pid;
6061 struct got_histedit_list histedit_cmds;
6062 struct got_histedit_list_entry *hle;
6064 SIMPLEQ_INIT(&commits);
6065 TAILQ_INIT(&histedit_cmds);
6066 TAILQ_INIT(&merged_paths);
6068 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6069 switch (ch) {
6070 case 'a':
6071 abort_edit = 1;
6072 break;
6073 case 'c':
6074 continue_edit = 1;
6075 break;
6076 case 'F':
6077 edit_script_path = optarg;
6078 break;
6079 default:
6080 usage_histedit();
6081 /* NOTREACHED */
6085 argc -= optind;
6086 argv += optind;
6088 #ifndef PROFILE
6089 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6090 "unveil", NULL) == -1)
6091 err(1, "pledge");
6092 #endif
6093 if (abort_edit && continue_edit)
6094 usage_histedit();
6095 if (argc != 0)
6096 usage_histedit();
6099 * This command cannot apply unveil(2) in all cases because the
6100 * user may choose to run an editor to edit the histedit script
6101 * and to edit individual commit log messages.
6102 * unveil(2) traverses exec(2); if an editor is used we have to
6103 * apply unveil after edit script and log messages have been written.
6104 * XXX TODO: Make use of unveil(2) where possible.
6107 cwd = getcwd(NULL, 0);
6108 if (cwd == NULL) {
6109 error = got_error_from_errno("getcwd");
6110 goto done;
6112 error = got_worktree_open(&worktree, cwd);
6113 if (error)
6114 goto done;
6116 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6117 NULL);
6118 if (error != NULL)
6119 goto done;
6121 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6122 if (error)
6123 goto done;
6124 if (rebase_in_progress) {
6125 error = got_error(GOT_ERR_REBASING);
6126 goto done;
6129 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6130 if (error)
6131 goto done;
6133 if (edit_in_progress && abort_edit) {
6134 error = got_worktree_histedit_continue(&resume_commit_id,
6135 &tmp_branch, &branch, &base_commit_id, &fileindex,
6136 worktree, repo);
6137 if (error)
6138 goto done;
6139 printf("Switching work tree to %s\n",
6140 got_ref_get_symref_target(branch));
6141 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6142 branch, base_commit_id, update_progress, &did_something);
6143 if (error)
6144 goto done;
6145 printf("Histedit of %s aborted\n",
6146 got_ref_get_symref_target(branch));
6147 goto done; /* nothing else to do */
6148 } else if (abort_edit) {
6149 error = got_error(GOT_ERR_NOT_HISTEDIT);
6150 goto done;
6153 if (continue_edit) {
6154 char *path;
6156 if (!edit_in_progress) {
6157 error = got_error(GOT_ERR_NOT_HISTEDIT);
6158 goto done;
6161 error = got_worktree_get_histedit_script_path(&path, worktree);
6162 if (error)
6163 goto done;
6165 error = histedit_load_list(&histedit_cmds, path, repo);
6166 free(path);
6167 if (error)
6168 goto done;
6170 error = got_worktree_histedit_continue(&resume_commit_id,
6171 &tmp_branch, &branch, &base_commit_id, &fileindex,
6172 worktree, repo);
6173 if (error)
6174 goto done;
6176 error = got_ref_resolve(&head_commit_id, repo, branch);
6177 if (error)
6178 goto done;
6180 error = got_object_open_as_commit(&commit, repo,
6181 head_commit_id);
6182 if (error)
6183 goto done;
6184 parent_ids = got_object_commit_get_parent_ids(commit);
6185 pid = SIMPLEQ_FIRST(parent_ids);
6186 if (pid == NULL) {
6187 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6188 goto done;
6190 error = collect_commits(&commits, head_commit_id, pid->id,
6191 base_commit_id, got_worktree_get_path_prefix(worktree),
6192 GOT_ERR_HISTEDIT_PATH, repo);
6193 got_object_commit_close(commit);
6194 commit = NULL;
6195 if (error)
6196 goto done;
6197 } else {
6198 if (edit_in_progress) {
6199 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6200 goto done;
6203 error = got_ref_open(&branch, repo,
6204 got_worktree_get_head_ref_name(worktree), 0);
6205 if (error != NULL)
6206 goto done;
6208 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6209 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6210 "will not edit commit history of a branch outside "
6211 "the \"refs/heads/\" reference namespace");
6212 goto done;
6215 error = got_ref_resolve(&head_commit_id, repo, branch);
6216 got_ref_close(branch);
6217 branch = NULL;
6218 if (error)
6219 goto done;
6221 error = got_object_open_as_commit(&commit, repo,
6222 head_commit_id);
6223 if (error)
6224 goto done;
6225 parent_ids = got_object_commit_get_parent_ids(commit);
6226 pid = SIMPLEQ_FIRST(parent_ids);
6227 if (pid == NULL) {
6228 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6229 goto done;
6231 error = collect_commits(&commits, head_commit_id, pid->id,
6232 got_worktree_get_base_commit_id(worktree),
6233 got_worktree_get_path_prefix(worktree),
6234 GOT_ERR_HISTEDIT_PATH, repo);
6235 got_object_commit_close(commit);
6236 commit = NULL;
6237 if (error)
6238 goto done;
6240 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6241 &base_commit_id, &fileindex, worktree, repo);
6242 if (error)
6243 goto done;
6245 if (edit_script_path) {
6246 error = histedit_load_list(&histedit_cmds,
6247 edit_script_path, repo);
6248 if (error) {
6249 got_worktree_histedit_abort(worktree, fileindex,
6250 repo, branch, base_commit_id,
6251 update_progress, &did_something);
6252 goto done;
6254 } else {
6255 error = histedit_edit_script(&histedit_cmds, &commits,
6256 repo);
6257 if (error) {
6258 got_worktree_histedit_abort(worktree, fileindex,
6259 repo, branch, base_commit_id,
6260 update_progress, &did_something);
6261 goto done;
6266 error = histedit_save_list(&histedit_cmds, worktree,
6267 repo);
6268 if (error) {
6269 got_worktree_histedit_abort(worktree, fileindex,
6270 repo, branch, base_commit_id,
6271 update_progress, &did_something);
6272 goto done;
6277 error = histedit_check_script(&histedit_cmds, &commits, repo);
6278 if (error)
6279 goto done;
6281 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6282 if (resume_commit_id) {
6283 if (got_object_id_cmp(hle->commit_id,
6284 resume_commit_id) != 0)
6285 continue;
6287 resume_commit_id = NULL;
6288 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6289 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6290 error = histedit_skip_commit(hle, worktree,
6291 repo);
6292 } else {
6293 error = histedit_commit(NULL, worktree,
6294 fileindex, tmp_branch, hle, repo);
6296 if (error)
6297 goto done;
6298 continue;
6301 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6302 error = histedit_skip_commit(hle, worktree, repo);
6303 if (error)
6304 goto done;
6305 continue;
6308 error = got_object_open_as_commit(&commit, repo,
6309 hle->commit_id);
6310 if (error)
6311 goto done;
6312 parent_ids = got_object_commit_get_parent_ids(commit);
6313 pid = SIMPLEQ_FIRST(parent_ids);
6315 error = got_worktree_histedit_merge_files(&merged_paths,
6316 worktree, fileindex, pid->id, hle->commit_id, repo,
6317 rebase_progress, &rebase_status, check_cancelled, NULL);
6318 if (error)
6319 goto done;
6320 got_object_commit_close(commit);
6321 commit = NULL;
6323 if (rebase_status == GOT_STATUS_CONFLICT) {
6324 got_worktree_rebase_pathlist_free(&merged_paths);
6325 break;
6328 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6329 char *id_str;
6330 error = got_object_id_str(&id_str, hle->commit_id);
6331 if (error)
6332 goto done;
6333 printf("Stopping histedit for amending commit %s\n",
6334 id_str);
6335 free(id_str);
6336 got_worktree_rebase_pathlist_free(&merged_paths);
6337 error = got_worktree_histedit_postpone(worktree,
6338 fileindex);
6339 goto done;
6342 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6343 error = histedit_skip_commit(hle, worktree, repo);
6344 if (error)
6345 goto done;
6346 continue;
6349 error = histedit_commit(&merged_paths, worktree, fileindex,
6350 tmp_branch, hle, repo);
6351 got_worktree_rebase_pathlist_free(&merged_paths);
6352 if (error)
6353 goto done;
6356 if (rebase_status == GOT_STATUS_CONFLICT) {
6357 error = got_worktree_histedit_postpone(worktree, fileindex);
6358 if (error)
6359 goto done;
6360 error = got_error_msg(GOT_ERR_CONFLICTS,
6361 "conflicts must be resolved before rebasing can continue");
6362 } else
6363 error = histedit_complete(worktree, fileindex, tmp_branch,
6364 branch, repo);
6365 done:
6366 got_object_id_queue_free(&commits);
6367 histedit_free_list(&histedit_cmds);
6368 free(head_commit_id);
6369 free(base_commit_id);
6370 free(resume_commit_id);
6371 if (commit)
6372 got_object_commit_close(commit);
6373 if (branch)
6374 got_ref_close(branch);
6375 if (tmp_branch)
6376 got_ref_close(tmp_branch);
6377 if (worktree)
6378 got_worktree_close(worktree);
6379 if (repo)
6380 got_repo_close(repo);
6381 return error;
6384 __dead static void
6385 usage_stage(void)
6387 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6388 "[file-path ...]\n",
6389 getprogname());
6390 exit(1);
6393 static const struct got_error *
6394 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6395 const char *path, struct got_object_id *blob_id,
6396 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6398 const struct got_error *err = NULL;
6399 char *id_str = NULL;
6401 if (staged_status != GOT_STATUS_ADD &&
6402 staged_status != GOT_STATUS_MODIFY &&
6403 staged_status != GOT_STATUS_DELETE)
6404 return NULL;
6406 if (staged_status == GOT_STATUS_ADD ||
6407 staged_status == GOT_STATUS_MODIFY)
6408 err = got_object_id_str(&id_str, staged_blob_id);
6409 else
6410 err = got_object_id_str(&id_str, blob_id);
6411 if (err)
6412 return err;
6414 printf("%s %c %s\n", id_str, staged_status, path);
6415 free(id_str);
6416 return NULL;
6419 static const struct got_error *
6420 cmd_stage(int argc, char *argv[])
6422 const struct got_error *error = NULL;
6423 struct got_repository *repo = NULL;
6424 struct got_worktree *worktree = NULL;
6425 char *cwd = NULL;
6426 struct got_pathlist_head paths;
6427 struct got_pathlist_entry *pe;
6428 int ch, list_stage = 0, pflag = 0;
6429 FILE *patch_script_file = NULL;
6430 const char *patch_script_path = NULL;
6431 struct choose_patch_arg cpa;
6433 TAILQ_INIT(&paths);
6435 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6436 switch (ch) {
6437 case 'l':
6438 list_stage = 1;
6439 break;
6440 case 'p':
6441 pflag = 1;
6442 break;
6443 case 'F':
6444 patch_script_path = optarg;
6445 break;
6446 default:
6447 usage_stage();
6448 /* NOTREACHED */
6452 argc -= optind;
6453 argv += optind;
6455 #ifndef PROFILE
6456 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6457 "unveil", NULL) == -1)
6458 err(1, "pledge");
6459 #endif
6460 if (list_stage && (pflag || patch_script_path))
6461 errx(1, "-l option cannot be used with other options");
6462 if (patch_script_path && !pflag)
6463 errx(1, "-F option can only be used together with -p option");
6465 cwd = getcwd(NULL, 0);
6466 if (cwd == NULL) {
6467 error = got_error_from_errno("getcwd");
6468 goto done;
6471 error = got_worktree_open(&worktree, cwd);
6472 if (error)
6473 goto done;
6475 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6476 NULL);
6477 if (error != NULL)
6478 goto done;
6480 if (patch_script_path) {
6481 patch_script_file = fopen(patch_script_path, "r");
6482 if (patch_script_file == NULL) {
6483 error = got_error_from_errno2("fopen",
6484 patch_script_path);
6485 goto done;
6488 error = apply_unveil(got_repo_get_path(repo), 0,
6489 got_worktree_get_root_path(worktree));
6490 if (error)
6491 goto done;
6493 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6494 if (error)
6495 goto done;
6497 if (list_stage)
6498 error = got_worktree_status(worktree, &paths, repo,
6499 print_stage, NULL, check_cancelled, NULL);
6500 else {
6501 cpa.patch_script_file = patch_script_file;
6502 cpa.action = "stage";
6503 error = got_worktree_stage(worktree, &paths,
6504 pflag ? NULL : print_status, NULL,
6505 pflag ? choose_patch : NULL, &cpa, repo);
6507 done:
6508 if (patch_script_file && fclose(patch_script_file) == EOF &&
6509 error == NULL)
6510 error = got_error_from_errno2("fclose", patch_script_path);
6511 if (repo)
6512 got_repo_close(repo);
6513 if (worktree)
6514 got_worktree_close(worktree);
6515 TAILQ_FOREACH(pe, &paths, entry)
6516 free((char *)pe->path);
6517 got_pathlist_free(&paths);
6518 free(cwd);
6519 return error;
6522 __dead static void
6523 usage_unstage(void)
6525 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6526 "[file-path ...]\n",
6527 getprogname());
6528 exit(1);
6532 static const struct got_error *
6533 cmd_unstage(int argc, char *argv[])
6535 const struct got_error *error = NULL;
6536 struct got_repository *repo = NULL;
6537 struct got_worktree *worktree = NULL;
6538 char *cwd = NULL;
6539 struct got_pathlist_head paths;
6540 struct got_pathlist_entry *pe;
6541 int ch, did_something = 0, pflag = 0;
6542 FILE *patch_script_file = NULL;
6543 const char *patch_script_path = NULL;
6544 struct choose_patch_arg cpa;
6546 TAILQ_INIT(&paths);
6548 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6549 switch (ch) {
6550 case 'p':
6551 pflag = 1;
6552 break;
6553 case 'F':
6554 patch_script_path = optarg;
6555 break;
6556 default:
6557 usage_unstage();
6558 /* NOTREACHED */
6562 argc -= optind;
6563 argv += optind;
6565 #ifndef PROFILE
6566 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6567 "unveil", NULL) == -1)
6568 err(1, "pledge");
6569 #endif
6570 if (patch_script_path && !pflag)
6571 errx(1, "-F option can only be used together with -p option");
6573 cwd = getcwd(NULL, 0);
6574 if (cwd == NULL) {
6575 error = got_error_from_errno("getcwd");
6576 goto done;
6579 error = got_worktree_open(&worktree, cwd);
6580 if (error)
6581 goto done;
6583 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6584 NULL);
6585 if (error != NULL)
6586 goto done;
6588 if (patch_script_path) {
6589 patch_script_file = fopen(patch_script_path, "r");
6590 if (patch_script_file == NULL) {
6591 error = got_error_from_errno2("fopen",
6592 patch_script_path);
6593 goto done;
6597 error = apply_unveil(got_repo_get_path(repo), 0,
6598 got_worktree_get_root_path(worktree));
6599 if (error)
6600 goto done;
6602 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6603 if (error)
6604 goto done;
6606 cpa.patch_script_file = patch_script_file;
6607 cpa.action = "unstage";
6608 error = got_worktree_unstage(worktree, &paths, update_progress,
6609 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6610 done:
6611 if (patch_script_file && fclose(patch_script_file) == EOF &&
6612 error == NULL)
6613 error = got_error_from_errno2("fclose", patch_script_path);
6614 if (repo)
6615 got_repo_close(repo);
6616 if (worktree)
6617 got_worktree_close(worktree);
6618 TAILQ_FOREACH(pe, &paths, entry)
6619 free((char *)pe->path);
6620 got_pathlist_free(&paths);
6621 free(cwd);
6622 return error;
6625 __dead static void
6626 usage_cat(void)
6628 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6629 "arg1 [arg2 ...]\n", getprogname());
6630 exit(1);
6633 static const struct got_error *
6634 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6636 const struct got_error *err;
6637 struct got_blob_object *blob;
6639 err = got_object_open_as_blob(&blob, repo, id, 8192);
6640 if (err)
6641 return err;
6643 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6644 got_object_blob_close(blob);
6645 return err;
6648 static const struct got_error *
6649 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6651 const struct got_error *err;
6652 struct got_tree_object *tree;
6653 const struct got_tree_entries *entries;
6654 struct got_tree_entry *te;
6656 err = got_object_open_as_tree(&tree, repo, id);
6657 if (err)
6658 return err;
6660 entries = got_object_tree_get_entries(tree);
6661 te = SIMPLEQ_FIRST(&entries->head);
6662 while (te) {
6663 char *id_str;
6664 if (sigint_received || sigpipe_received)
6665 break;
6666 err = got_object_id_str(&id_str, te->id);
6667 if (err)
6668 break;
6669 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6670 free(id_str);
6671 te = SIMPLEQ_NEXT(te, entry);
6674 got_object_tree_close(tree);
6675 return err;
6678 static const struct got_error *
6679 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6681 const struct got_error *err;
6682 struct got_commit_object *commit;
6683 const struct got_object_id_queue *parent_ids;
6684 struct got_object_qid *pid;
6685 char *id_str = NULL;
6686 const char *logmsg = NULL;
6688 err = got_object_open_as_commit(&commit, repo, id);
6689 if (err)
6690 return err;
6692 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6693 if (err)
6694 goto done;
6696 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6697 parent_ids = got_object_commit_get_parent_ids(commit);
6698 fprintf(outfile, "numparents %d\n",
6699 got_object_commit_get_nparents(commit));
6700 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6701 char *pid_str;
6702 err = got_object_id_str(&pid_str, pid->id);
6703 if (err)
6704 goto done;
6705 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6706 free(pid_str);
6708 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6709 got_object_commit_get_author(commit),
6710 got_object_commit_get_author_time(commit));
6712 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6713 got_object_commit_get_author(commit),
6714 got_object_commit_get_committer_time(commit));
6716 logmsg = got_object_commit_get_logmsg_raw(commit);
6717 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6718 fprintf(outfile, "%s", logmsg);
6719 done:
6720 free(id_str);
6721 got_object_commit_close(commit);
6722 return err;
6725 static const struct got_error *
6726 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6728 const struct got_error *err;
6729 struct got_tag_object *tag;
6730 char *id_str = NULL;
6731 const char *tagmsg = NULL;
6733 err = got_object_open_as_tag(&tag, repo, id);
6734 if (err)
6735 return err;
6737 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6738 if (err)
6739 goto done;
6741 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6743 switch (got_object_tag_get_object_type(tag)) {
6744 case GOT_OBJ_TYPE_BLOB:
6745 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6746 GOT_OBJ_LABEL_BLOB);
6747 break;
6748 case GOT_OBJ_TYPE_TREE:
6749 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6750 GOT_OBJ_LABEL_TREE);
6751 break;
6752 case GOT_OBJ_TYPE_COMMIT:
6753 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6754 GOT_OBJ_LABEL_COMMIT);
6755 break;
6756 case GOT_OBJ_TYPE_TAG:
6757 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6758 GOT_OBJ_LABEL_TAG);
6759 break;
6760 default:
6761 break;
6764 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6765 got_object_tag_get_name(tag));
6767 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6768 got_object_tag_get_tagger(tag),
6769 got_object_tag_get_tagger_time(tag));
6771 tagmsg = got_object_tag_get_message(tag);
6772 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6773 fprintf(outfile, "%s", tagmsg);
6774 done:
6775 free(id_str);
6776 got_object_tag_close(tag);
6777 return err;
6780 static const struct got_error *
6781 cmd_cat(int argc, char *argv[])
6783 const struct got_error *error;
6784 struct got_repository *repo = NULL;
6785 struct got_worktree *worktree = NULL;
6786 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6787 const char *commit_id_str = NULL;
6788 struct got_object_id *id = NULL, *commit_id = NULL;
6789 int ch, obj_type, i, force_path = 0;
6791 #ifndef PROFILE
6792 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6793 NULL) == -1)
6794 err(1, "pledge");
6795 #endif
6797 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6798 switch (ch) {
6799 case 'c':
6800 commit_id_str = optarg;
6801 break;
6802 case 'r':
6803 repo_path = realpath(optarg, NULL);
6804 if (repo_path == NULL)
6805 err(1, "-r option");
6806 got_path_strip_trailing_slashes(repo_path);
6807 break;
6808 case 'P':
6809 force_path = 1;
6810 break;
6811 default:
6812 usage_cat();
6813 /* NOTREACHED */
6817 argc -= optind;
6818 argv += optind;
6820 cwd = getcwd(NULL, 0);
6821 if (cwd == NULL) {
6822 error = got_error_from_errno("getcwd");
6823 goto done;
6825 error = got_worktree_open(&worktree, cwd);
6826 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6827 goto done;
6828 if (worktree) {
6829 if (repo_path == NULL) {
6830 repo_path = strdup(
6831 got_worktree_get_repo_path(worktree));
6832 if (repo_path == NULL) {
6833 error = got_error_from_errno("strdup");
6834 goto done;
6839 if (repo_path == NULL) {
6840 repo_path = getcwd(NULL, 0);
6841 if (repo_path == NULL)
6842 return got_error_from_errno("getcwd");
6845 error = got_repo_open(&repo, repo_path, NULL);
6846 free(repo_path);
6847 if (error != NULL)
6848 goto done;
6850 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6851 if (error)
6852 goto done;
6854 if (commit_id_str == NULL)
6855 commit_id_str = GOT_REF_HEAD;
6856 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6857 if (error)
6858 goto done;
6860 for (i = 0; i < argc; i++) {
6861 if (force_path) {
6862 error = got_object_id_by_path(&id, repo, commit_id,
6863 argv[i]);
6864 if (error)
6865 break;
6866 } else {
6867 error = match_object_id(&id, &label, argv[i],
6868 GOT_OBJ_TYPE_ANY, 0, repo);
6869 if (error) {
6870 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6871 error->code != GOT_ERR_NOT_REF)
6872 break;
6873 error = got_object_id_by_path(&id, repo,
6874 commit_id, argv[i]);
6875 if (error)
6876 break;
6880 error = got_object_get_type(&obj_type, repo, id);
6881 if (error)
6882 break;
6884 switch (obj_type) {
6885 case GOT_OBJ_TYPE_BLOB:
6886 error = cat_blob(id, repo, stdout);
6887 break;
6888 case GOT_OBJ_TYPE_TREE:
6889 error = cat_tree(id, repo, stdout);
6890 break;
6891 case GOT_OBJ_TYPE_COMMIT:
6892 error = cat_commit(id, repo, stdout);
6893 break;
6894 case GOT_OBJ_TYPE_TAG:
6895 error = cat_tag(id, repo, stdout);
6896 break;
6897 default:
6898 error = got_error(GOT_ERR_OBJ_TYPE);
6899 break;
6901 if (error)
6902 break;
6903 free(label);
6904 label = NULL;
6905 free(id);
6906 id = NULL;
6909 done:
6910 free(label);
6911 free(id);
6912 free(commit_id);
6913 if (worktree)
6914 got_worktree_close(worktree);
6915 if (repo) {
6916 const struct got_error *repo_error;
6917 repo_error = got_repo_close(repo);
6918 if (error == NULL)
6919 error = repo_error;
6921 return error;