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(const char **author)
497 const char *got_author;
499 *author = NULL;
501 got_author = getenv("GOT_AUTHOR");
502 if (got_author == NULL) {
503 /* TODO: Look up user in password database? */
504 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
507 *author = got_author;
509 /*
510 * Really dumb email address check; we're only doing this to
511 * avoid git's object parser breaking on commits we create.
512 */
513 while (*got_author && *got_author != '<')
514 got_author++;
515 if (*got_author != '<')
516 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 while (*got_author && *got_author != '@')
518 got_author++;
519 if (*got_author != '@')
520 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
521 while (*got_author && *got_author != '>')
522 got_author++;
523 if (*got_author != '>')
524 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
526 return NULL;
529 static const struct got_error *
530 cmd_import(int argc, char *argv[])
532 const struct got_error *error = NULL;
533 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
534 char *editor = NULL;
535 const char *author;
536 const char *branch_name = "master";
537 char *refname = NULL, *id_str = NULL;
538 struct got_repository *repo = NULL;
539 struct got_reference *branch_ref = NULL, *head_ref = NULL;
540 struct got_object_id *new_commit_id = NULL;
541 int ch;
542 struct got_pathlist_head ignores;
543 struct got_pathlist_entry *pe;
545 TAILQ_INIT(&ignores);
547 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
548 switch (ch) {
549 case 'b':
550 branch_name = optarg;
551 break;
552 case 'm':
553 logmsg = strdup(optarg);
554 if (logmsg == NULL) {
555 error = got_error_from_errno("strdup");
556 goto done;
558 break;
559 case 'r':
560 repo_path = realpath(optarg, NULL);
561 if (repo_path == NULL) {
562 error = got_error_from_errno("realpath");
563 goto done;
565 break;
566 case 'I':
567 if (optarg[0] == '\0')
568 break;
569 error = got_pathlist_insert(&pe, &ignores, optarg,
570 NULL);
571 if (error)
572 goto done;
573 break;
574 default:
575 usage_import();
576 /* NOTREACHED */
580 argc -= optind;
581 argv += optind;
583 #ifndef PROFILE
584 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
585 NULL) == -1)
586 err(1, "pledge");
587 #endif
588 if (argc != 1)
589 usage_import();
591 error = get_author(&author);
592 if (error)
593 return error;
595 if (repo_path == NULL) {
596 repo_path = getcwd(NULL, 0);
597 if (repo_path == NULL)
598 return got_error_from_errno("getcwd");
600 got_path_strip_trailing_slashes(repo_path);
601 error = got_repo_open(&repo, repo_path);
602 if (error)
603 goto done;
605 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
606 error = got_error_from_errno("asprintf");
607 goto done;
610 error = got_ref_open(&branch_ref, repo, refname, 0);
611 if (error) {
612 if (error->code != GOT_ERR_NOT_REF)
613 goto done;
614 } else {
615 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
616 "import target branch already exists");
617 goto done;
620 path_dir = realpath(argv[0], NULL);
621 if (path_dir == NULL) {
622 error = got_error_from_errno("realpath");
623 goto done;
625 got_path_strip_trailing_slashes(path_dir);
627 /*
628 * unveil(2) traverses exec(2); if an editor is used we have
629 * to apply unveil after the log message has been written.
630 */
631 if (logmsg == NULL || strlen(logmsg) == 0) {
632 error = get_editor(&editor);
633 if (error)
634 goto done;
635 error = collect_import_msg(&logmsg, editor, path_dir, refname);
636 if (error)
637 goto done;
640 if (unveil(path_dir, "r") != 0)
641 return got_error_from_errno2("unveil", path_dir);
643 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
644 if (error)
645 goto done;
647 error = got_repo_import(&new_commit_id, path_dir, logmsg,
648 author, &ignores, repo, import_progress, NULL);
649 if (error)
650 goto done;
652 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
653 if (error)
654 goto done;
656 error = got_ref_write(branch_ref, repo);
657 if (error)
658 goto done;
660 error = got_object_id_str(&id_str, new_commit_id);
661 if (error)
662 goto done;
664 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
665 if (error) {
666 if (error->code != GOT_ERR_NOT_REF)
667 goto done;
669 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
670 branch_ref);
671 if (error)
672 goto done;
674 error = got_ref_write(head_ref, repo);
675 if (error)
676 goto done;
679 printf("Created branch %s with commit %s\n",
680 got_ref_get_name(branch_ref), id_str);
681 done:
682 free(repo_path);
683 free(editor);
684 free(refname);
685 free(new_commit_id);
686 free(id_str);
687 if (branch_ref)
688 got_ref_close(branch_ref);
689 if (head_ref)
690 got_ref_close(head_ref);
691 return error;
694 __dead static void
695 usage_checkout(void)
697 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
698 "[-p prefix] repository-path [worktree-path]\n", getprogname());
699 exit(1);
702 static const struct got_error *
703 checkout_progress(void *arg, unsigned char status, const char *path)
705 char *worktree_path = arg;
707 /* Base commit bump happens silently. */
708 if (status == GOT_STATUS_BUMP_BASE)
709 return NULL;
711 while (path[0] == '/')
712 path++;
714 printf("%c %s/%s\n", status, worktree_path, path);
715 return NULL;
718 static const struct got_error *
719 check_cancelled(void *arg)
721 if (sigint_received || sigpipe_received)
722 return got_error(GOT_ERR_CANCELLED);
723 return NULL;
726 static const struct got_error *
727 check_linear_ancestry(struct got_object_id *commit_id,
728 struct got_object_id *base_commit_id, struct got_repository *repo)
730 const struct got_error *err = NULL;
731 struct got_object_id *yca_id;
733 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
734 commit_id, base_commit_id, repo, check_cancelled, NULL);
735 if (err)
736 return err;
738 if (yca_id == NULL)
739 return got_error(GOT_ERR_ANCESTRY);
741 /*
742 * Require a straight line of history between the target commit
743 * and the work tree's base commit.
745 * Non-linear situations such as this require a rebase:
747 * (commit) D F (base_commit)
748 * \ /
749 * C E
750 * \ /
751 * B (yca)
752 * |
753 * A
755 * 'got update' only handles linear cases:
756 * Update forwards in time: A (base/yca) - B - C - D (commit)
757 * Update backwards in time: D (base) - C - B - A (commit/yca)
758 */
759 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
760 got_object_id_cmp(base_commit_id, yca_id) != 0)
761 return got_error(GOT_ERR_ANCESTRY);
763 free(yca_id);
764 return NULL;
767 static const struct got_error *
768 check_same_branch(struct got_object_id *commit_id,
769 struct got_reference *head_ref, struct got_object_id *yca_id,
770 struct got_repository *repo)
772 const struct got_error *err = NULL;
773 struct got_commit_graph *graph = NULL;
774 struct got_object_id *head_commit_id = NULL;
775 int is_same_branch = 0;
777 err = got_ref_resolve(&head_commit_id, repo, head_ref);
778 if (err)
779 goto done;
781 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
782 is_same_branch = 1;
783 goto done;
785 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
786 is_same_branch = 1;
787 goto done;
790 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
791 if (err)
792 goto done;
794 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
795 check_cancelled, NULL);
796 if (err)
797 goto done;
799 for (;;) {
800 struct got_object_id *id;
801 err = got_commit_graph_iter_next(&id, graph);
802 if (err) {
803 if (err->code == GOT_ERR_ITER_COMPLETED) {
804 err = NULL;
805 break;
806 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
807 break;
808 err = got_commit_graph_fetch_commits(graph, 1,
809 repo, check_cancelled, NULL);
810 if (err)
811 break;
814 if (id) {
815 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
816 break;
817 if (got_object_id_cmp(id, commit_id) == 0) {
818 is_same_branch = 1;
819 break;
823 done:
824 if (graph)
825 got_commit_graph_close(graph);
826 free(head_commit_id);
827 if (!err && !is_same_branch)
828 err = got_error(GOT_ERR_ANCESTRY);
829 return err;
832 static const struct got_error *
833 resolve_commit_arg(struct got_object_id **commit_id,
834 const char *commit_id_arg, struct got_repository *repo)
836 const struct got_error *err;
837 struct got_reference *ref;
838 struct got_tag_object *tag;
840 err = got_repo_object_match_tag(&tag, commit_id_arg,
841 GOT_OBJ_TYPE_COMMIT, repo);
842 if (err == NULL) {
843 *commit_id = got_object_id_dup(
844 got_object_tag_get_object_id(tag));
845 if (*commit_id == NULL)
846 err = got_error_from_errno("got_object_id_dup");
847 got_object_tag_close(tag);
848 return err;
849 } else if (err->code != GOT_ERR_NO_OBJ)
850 return err;
852 err = got_ref_open(&ref, repo, commit_id_arg, 0);
853 if (err == NULL) {
854 err = got_ref_resolve(commit_id, repo, ref);
855 got_ref_close(ref);
856 } else {
857 if (err->code != GOT_ERR_NOT_REF)
858 return err;
859 err = got_repo_match_object_id_prefix(commit_id,
860 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
862 return err;
865 static const struct got_error *
866 cmd_checkout(int argc, char *argv[])
868 const struct got_error *error = NULL;
869 struct got_repository *repo = NULL;
870 struct got_reference *head_ref = NULL;
871 struct got_worktree *worktree = NULL;
872 char *repo_path = NULL;
873 char *worktree_path = NULL;
874 const char *path_prefix = "";
875 const char *branch_name = GOT_REF_HEAD;
876 char *commit_id_str = NULL;
877 int ch, same_path_prefix;
878 struct got_pathlist_head paths;
880 TAILQ_INIT(&paths);
882 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
883 switch (ch) {
884 case 'b':
885 branch_name = optarg;
886 break;
887 case 'c':
888 commit_id_str = strdup(optarg);
889 if (commit_id_str == NULL)
890 return got_error_from_errno("strdup");
891 break;
892 case 'p':
893 path_prefix = optarg;
894 break;
895 default:
896 usage_checkout();
897 /* NOTREACHED */
901 argc -= optind;
902 argv += optind;
904 #ifndef PROFILE
905 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
906 "unveil", NULL) == -1)
907 err(1, "pledge");
908 #endif
909 if (argc == 1) {
910 char *cwd, *base, *dotgit;
911 repo_path = realpath(argv[0], NULL);
912 if (repo_path == NULL)
913 return got_error_from_errno2("realpath", argv[0]);
914 cwd = getcwd(NULL, 0);
915 if (cwd == NULL) {
916 error = got_error_from_errno("getcwd");
917 goto done;
919 if (path_prefix[0]) {
920 base = basename(path_prefix);
921 if (base == NULL) {
922 error = got_error_from_errno2("basename",
923 path_prefix);
924 goto done;
926 } else {
927 base = basename(repo_path);
928 if (base == NULL) {
929 error = got_error_from_errno2("basename",
930 repo_path);
931 goto done;
934 dotgit = strstr(base, ".git");
935 if (dotgit)
936 *dotgit = '\0';
937 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
938 error = got_error_from_errno("asprintf");
939 free(cwd);
940 goto done;
942 free(cwd);
943 } else if (argc == 2) {
944 repo_path = realpath(argv[0], NULL);
945 if (repo_path == NULL) {
946 error = got_error_from_errno2("realpath", argv[0]);
947 goto done;
949 worktree_path = realpath(argv[1], NULL);
950 if (worktree_path == NULL) {
951 if (errno != ENOENT) {
952 error = got_error_from_errno2("realpath",
953 argv[1]);
954 goto done;
956 worktree_path = strdup(argv[1]);
957 if (worktree_path == NULL) {
958 error = got_error_from_errno("strdup");
959 goto done;
962 } else
963 usage_checkout();
965 got_path_strip_trailing_slashes(repo_path);
966 got_path_strip_trailing_slashes(worktree_path);
968 error = got_repo_open(&repo, repo_path);
969 if (error != NULL)
970 goto done;
972 /* Pre-create work tree path for unveil(2) */
973 error = got_path_mkdir(worktree_path);
974 if (error) {
975 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
976 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
977 goto done;
978 if (!got_path_dir_is_empty(worktree_path)) {
979 error = got_error_path(worktree_path,
980 GOT_ERR_DIR_NOT_EMPTY);
981 goto done;
985 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
986 if (error)
987 goto done;
989 error = got_ref_open(&head_ref, repo, branch_name, 0);
990 if (error != NULL)
991 goto done;
993 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
994 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
995 goto done;
997 error = got_worktree_open(&worktree, worktree_path);
998 if (error != NULL)
999 goto done;
1001 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1002 path_prefix);
1003 if (error != NULL)
1004 goto done;
1005 if (!same_path_prefix) {
1006 error = got_error(GOT_ERR_PATH_PREFIX);
1007 goto done;
1010 if (commit_id_str) {
1011 struct got_object_id *commit_id;
1012 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1013 if (error)
1014 goto done;
1015 error = check_linear_ancestry(commit_id,
1016 got_worktree_get_base_commit_id(worktree), repo);
1017 if (error != NULL) {
1018 free(commit_id);
1019 goto done;
1021 error = check_same_branch(commit_id, head_ref, NULL, repo);
1022 if (error)
1023 goto done;
1024 error = got_worktree_set_base_commit_id(worktree, repo,
1025 commit_id);
1026 free(commit_id);
1027 if (error)
1028 goto done;
1031 error = got_pathlist_append(&paths, "", NULL);
1032 if (error)
1033 goto done;
1034 error = got_worktree_checkout_files(worktree, &paths, repo,
1035 checkout_progress, worktree_path, check_cancelled, NULL);
1036 if (error != NULL)
1037 goto done;
1039 printf("Now shut up and hack\n");
1041 done:
1042 got_pathlist_free(&paths);
1043 free(commit_id_str);
1044 free(repo_path);
1045 free(worktree_path);
1046 return error;
1049 __dead static void
1050 usage_update(void)
1052 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1053 getprogname());
1054 exit(1);
1057 static const struct got_error *
1058 update_progress(void *arg, unsigned char status, const char *path)
1060 int *did_something = arg;
1062 if (status == GOT_STATUS_EXISTS)
1063 return NULL;
1065 *did_something = 1;
1067 /* Base commit bump happens silently. */
1068 if (status == GOT_STATUS_BUMP_BASE)
1069 return NULL;
1071 while (path[0] == '/')
1072 path++;
1073 printf("%c %s\n", status, path);
1074 return NULL;
1077 static const struct got_error *
1078 switch_head_ref(struct got_reference *head_ref,
1079 struct got_object_id *commit_id, struct got_worktree *worktree,
1080 struct got_repository *repo)
1082 const struct got_error *err = NULL;
1083 char *base_id_str;
1084 int ref_has_moved = 0;
1086 /* Trivial case: switching between two different references. */
1087 if (strcmp(got_ref_get_name(head_ref),
1088 got_worktree_get_head_ref_name(worktree)) != 0) {
1089 printf("Switching work tree from %s to %s\n",
1090 got_worktree_get_head_ref_name(worktree),
1091 got_ref_get_name(head_ref));
1092 return got_worktree_set_head_ref(worktree, head_ref);
1095 err = check_linear_ancestry(commit_id,
1096 got_worktree_get_base_commit_id(worktree), repo);
1097 if (err) {
1098 if (err->code != GOT_ERR_ANCESTRY)
1099 return err;
1100 ref_has_moved = 1;
1102 if (!ref_has_moved)
1103 return NULL;
1105 /* Switching to a rebased branch with the same reference name. */
1106 err = got_object_id_str(&base_id_str,
1107 got_worktree_get_base_commit_id(worktree));
1108 if (err)
1109 return err;
1110 printf("Reference %s now points at a different branch\n",
1111 got_worktree_get_head_ref_name(worktree));
1112 printf("Switching work tree from %s to %s\n", base_id_str,
1113 got_worktree_get_head_ref_name(worktree));
1114 return NULL;
1117 static const struct got_error *
1118 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1120 const struct got_error *err;
1121 int in_progress;
1123 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1124 if (err)
1125 return err;
1126 if (in_progress)
1127 return got_error(GOT_ERR_REBASING);
1129 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1130 if (err)
1131 return err;
1132 if (in_progress)
1133 return got_error(GOT_ERR_HISTEDIT_BUSY);
1135 return NULL;
1138 static const struct got_error *
1139 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1140 char *argv[], struct got_worktree *worktree)
1142 const struct got_error *err = NULL;
1143 char *path;
1144 int i;
1146 if (argc == 0) {
1147 path = strdup("");
1148 if (path == NULL)
1149 return got_error_from_errno("strdup");
1150 return got_pathlist_append(paths, path, NULL);
1153 for (i = 0; i < argc; i++) {
1154 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1155 if (err)
1156 break;
1157 err = got_pathlist_append(paths, path, NULL);
1158 if (err) {
1159 free(path);
1160 break;
1164 return err;
1167 static const struct got_error *
1168 cmd_update(int argc, char *argv[])
1170 const struct got_error *error = NULL;
1171 struct got_repository *repo = NULL;
1172 struct got_worktree *worktree = NULL;
1173 char *worktree_path = NULL;
1174 struct got_object_id *commit_id = NULL;
1175 char *commit_id_str = NULL;
1176 const char *branch_name = NULL;
1177 struct got_reference *head_ref = NULL;
1178 struct got_pathlist_head paths;
1179 struct got_pathlist_entry *pe;
1180 int ch, did_something = 0;
1182 TAILQ_INIT(&paths);
1184 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1185 switch (ch) {
1186 case 'b':
1187 branch_name = optarg;
1188 break;
1189 case 'c':
1190 commit_id_str = strdup(optarg);
1191 if (commit_id_str == NULL)
1192 return got_error_from_errno("strdup");
1193 break;
1194 default:
1195 usage_update();
1196 /* NOTREACHED */
1200 argc -= optind;
1201 argv += optind;
1203 #ifndef PROFILE
1204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 "unveil", NULL) == -1)
1206 err(1, "pledge");
1207 #endif
1208 worktree_path = getcwd(NULL, 0);
1209 if (worktree_path == NULL) {
1210 error = got_error_from_errno("getcwd");
1211 goto done;
1213 error = got_worktree_open(&worktree, worktree_path);
1214 if (error)
1215 goto done;
1217 error = check_rebase_or_histedit_in_progress(worktree);
1218 if (error)
1219 goto done;
1221 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1222 if (error != NULL)
1223 goto done;
1225 error = apply_unveil(got_repo_get_path(repo), 0,
1226 got_worktree_get_root_path(worktree));
1227 if (error)
1228 goto done;
1230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1231 if (error)
1232 goto done;
1234 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1235 got_worktree_get_head_ref_name(worktree), 0);
1236 if (error != NULL)
1237 goto done;
1238 if (commit_id_str == NULL) {
1239 error = got_ref_resolve(&commit_id, repo, head_ref);
1240 if (error != NULL)
1241 goto done;
1242 error = got_object_id_str(&commit_id_str, commit_id);
1243 if (error != NULL)
1244 goto done;
1245 } else {
1246 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1247 free(commit_id_str);
1248 commit_id_str = NULL;
1249 if (error)
1250 goto done;
1251 error = got_object_id_str(&commit_id_str, commit_id);
1252 if (error)
1253 goto done;
1256 if (branch_name) {
1257 struct got_object_id *head_commit_id;
1258 TAILQ_FOREACH(pe, &paths, entry) {
1259 if (pe->path_len == 0)
1260 continue;
1261 error = got_error_msg(GOT_ERR_BAD_PATH,
1262 "switching between branches requires that "
1263 "the entire work tree gets updated");
1264 goto done;
1266 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1267 if (error)
1268 goto done;
1269 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1270 free(head_commit_id);
1271 if (error != NULL)
1272 goto done;
1273 error = check_same_branch(commit_id, head_ref, NULL, repo);
1274 if (error)
1275 goto done;
1276 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1277 if (error)
1278 goto done;
1279 } else {
1280 error = check_linear_ancestry(commit_id,
1281 got_worktree_get_base_commit_id(worktree), repo);
1282 if (error != NULL) {
1283 if (error->code == GOT_ERR_ANCESTRY)
1284 error = got_error(GOT_ERR_BRANCH_MOVED);
1285 goto done;
1287 error = check_same_branch(commit_id, head_ref, NULL, repo);
1288 if (error)
1289 goto done;
1292 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1293 commit_id) != 0) {
1294 error = got_worktree_set_base_commit_id(worktree, repo,
1295 commit_id);
1296 if (error)
1297 goto done;
1300 error = got_worktree_checkout_files(worktree, &paths, repo,
1301 update_progress, &did_something, check_cancelled, NULL);
1302 if (error != NULL)
1303 goto done;
1305 if (did_something)
1306 printf("Updated to commit %s\n", commit_id_str);
1307 else
1308 printf("Already up-to-date\n");
1309 done:
1310 free(worktree_path);
1311 TAILQ_FOREACH(pe, &paths, entry)
1312 free((char *)pe->path);
1313 got_pathlist_free(&paths);
1314 free(commit_id);
1315 free(commit_id_str);
1316 return error;
1319 static const struct got_error *
1320 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1321 int diff_context, struct got_repository *repo)
1323 const struct got_error *err = NULL;
1324 struct got_tree_object *tree1 = NULL, *tree2;
1325 struct got_object_qid *qid;
1326 char *id_str1 = NULL, *id_str2;
1327 struct got_diff_blob_output_unidiff_arg arg;
1329 err = got_object_open_as_tree(&tree2, repo,
1330 got_object_commit_get_tree_id(commit));
1331 if (err)
1332 return err;
1334 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1335 if (qid != NULL) {
1336 struct got_commit_object *pcommit;
1338 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1339 if (err)
1340 return err;
1342 err = got_object_open_as_tree(&tree1, repo,
1343 got_object_commit_get_tree_id(pcommit));
1344 got_object_commit_close(pcommit);
1345 if (err)
1346 return err;
1348 err = got_object_id_str(&id_str1, qid->id);
1349 if (err)
1350 return err;
1353 err = got_object_id_str(&id_str2, id);
1354 if (err)
1355 goto done;
1357 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1358 arg.diff_context = diff_context;
1359 arg.outfile = stdout;
1360 err = got_diff_tree(tree1, tree2, "", "", repo,
1361 got_diff_blob_output_unidiff, &arg, 1);
1362 done:
1363 if (tree1)
1364 got_object_tree_close(tree1);
1365 got_object_tree_close(tree2);
1366 free(id_str1);
1367 free(id_str2);
1368 return err;
1371 static char *
1372 get_datestr(time_t *time, char *datebuf)
1374 struct tm mytm, *tm;
1375 char *p, *s;
1377 tm = gmtime_r(time, &mytm);
1378 if (tm == NULL)
1379 return NULL;
1380 s = asctime_r(tm, datebuf);
1381 if (s == NULL)
1382 return NULL;
1383 p = strchr(s, '\n');
1384 if (p)
1385 *p = '\0';
1386 return s;
1389 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1391 static const struct got_error *
1392 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1393 struct got_repository *repo, int show_patch, int diff_context,
1394 struct got_reflist_head *refs)
1396 const struct got_error *err = NULL;
1397 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1398 char datebuf[26];
1399 time_t committer_time;
1400 const char *author, *committer;
1401 char *refs_str = NULL;
1402 struct got_reflist_entry *re;
1404 SIMPLEQ_FOREACH(re, refs, entry) {
1405 char *s;
1406 const char *name;
1407 struct got_tag_object *tag = NULL;
1408 int cmp;
1410 name = got_ref_get_name(re->ref);
1411 if (strcmp(name, GOT_REF_HEAD) == 0)
1412 continue;
1413 if (strncmp(name, "refs/", 5) == 0)
1414 name += 5;
1415 if (strncmp(name, "got/", 4) == 0)
1416 continue;
1417 if (strncmp(name, "heads/", 6) == 0)
1418 name += 6;
1419 if (strncmp(name, "remotes/", 8) == 0)
1420 name += 8;
1421 if (strncmp(name, "tags/", 5) == 0) {
1422 err = got_object_open_as_tag(&tag, repo, re->id);
1423 if (err) {
1424 if (err->code != GOT_ERR_OBJ_TYPE)
1425 return err;
1426 /* Ref points at something other than a tag. */
1427 err = NULL;
1428 tag = NULL;
1431 cmp = got_object_id_cmp(tag ?
1432 got_object_tag_get_object_id(tag) : re->id, id);
1433 if (tag)
1434 got_object_tag_close(tag);
1435 if (cmp != 0)
1436 continue;
1437 s = refs_str;
1438 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1439 name) == -1) {
1440 err = got_error_from_errno("asprintf");
1441 free(s);
1442 return err;
1444 free(s);
1446 err = got_object_id_str(&id_str, id);
1447 if (err)
1448 return err;
1450 printf(GOT_COMMIT_SEP_STR);
1451 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1452 refs_str ? refs_str : "", refs_str ? ")" : "");
1453 free(id_str);
1454 id_str = NULL;
1455 free(refs_str);
1456 refs_str = NULL;
1457 printf("from: %s\n", got_object_commit_get_author(commit));
1458 committer_time = got_object_commit_get_committer_time(commit);
1459 datestr = get_datestr(&committer_time, datebuf);
1460 if (datestr)
1461 printf("date: %s UTC\n", datestr);
1462 author = got_object_commit_get_author(commit);
1463 committer = got_object_commit_get_committer(commit);
1464 if (strcmp(author, committer) != 0)
1465 printf("via: %s\n", committer);
1466 if (got_object_commit_get_nparents(commit) > 1) {
1467 const struct got_object_id_queue *parent_ids;
1468 struct got_object_qid *qid;
1469 int n = 1;
1470 parent_ids = got_object_commit_get_parent_ids(commit);
1471 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1472 err = got_object_id_str(&id_str, qid->id);
1473 if (err)
1474 return err;
1475 printf("parent %d: %s\n", n++, id_str);
1476 free(id_str);
1480 err = got_object_commit_get_logmsg(&logmsg0, commit);
1481 if (err)
1482 return err;
1484 logmsg = logmsg0;
1485 do {
1486 line = strsep(&logmsg, "\n");
1487 if (line)
1488 printf(" %s\n", line);
1489 } while (line);
1490 free(logmsg0);
1492 if (show_patch) {
1493 err = print_patch(commit, id, diff_context, repo);
1494 if (err == 0)
1495 printf("\n");
1498 if (fflush(stdout) != 0 && err == NULL)
1499 err = got_error_from_errno("fflush");
1500 return err;
1503 static const struct got_error *
1504 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1505 char *path, int show_patch, int diff_context, int limit,
1506 int first_parent_traversal, struct got_reflist_head *refs)
1508 const struct got_error *err;
1509 struct got_commit_graph *graph;
1511 err = got_commit_graph_open(&graph, root_id, path,
1512 first_parent_traversal, repo);
1513 if (err)
1514 return err;
1515 err = got_commit_graph_iter_start(graph, root_id, repo,
1516 check_cancelled, NULL);
1517 if (err)
1518 goto done;
1519 for (;;) {
1520 struct got_commit_object *commit;
1521 struct got_object_id *id;
1523 if (sigint_received || sigpipe_received)
1524 break;
1526 err = got_commit_graph_iter_next(&id, graph);
1527 if (err) {
1528 if (err->code == GOT_ERR_ITER_COMPLETED) {
1529 err = NULL;
1530 break;
1532 if (err->code != GOT_ERR_ITER_NEED_MORE)
1533 break;
1534 err = got_commit_graph_fetch_commits(graph, 1, repo,
1535 check_cancelled, NULL);
1536 if (err)
1537 break;
1538 else
1539 continue;
1541 if (id == NULL)
1542 break;
1544 err = got_object_open_as_commit(&commit, repo, id);
1545 if (err)
1546 break;
1547 err = print_commit(commit, id, repo, show_patch, diff_context,
1548 refs);
1549 got_object_commit_close(commit);
1550 if (err || (limit && --limit == 0))
1551 break;
1553 done:
1554 got_commit_graph_close(graph);
1555 return err;
1558 __dead static void
1559 usage_log(void)
1561 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1562 "[-r repository-path] [path]\n", getprogname());
1563 exit(1);
1566 static int
1567 get_default_log_limit(void)
1569 const char *got_default_log_limit;
1570 long long n;
1571 const char *errstr;
1573 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1574 if (got_default_log_limit == NULL)
1575 return 0;
1576 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1577 if (errstr != NULL)
1578 return 0;
1579 return n;
1582 static const struct got_error *
1583 cmd_log(int argc, char *argv[])
1585 const struct got_error *error;
1586 struct got_repository *repo = NULL;
1587 struct got_worktree *worktree = NULL;
1588 struct got_commit_object *commit = NULL;
1589 struct got_object_id *id = NULL;
1590 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1591 char *start_commit = NULL;
1592 int diff_context = 3, ch;
1593 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1594 const char *errstr;
1595 struct got_reflist_head refs;
1597 SIMPLEQ_INIT(&refs);
1599 #ifndef PROFILE
1600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1601 NULL)
1602 == -1)
1603 err(1, "pledge");
1604 #endif
1606 limit = get_default_log_limit();
1608 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1609 switch (ch) {
1610 case 'p':
1611 show_patch = 1;
1612 break;
1613 case 'c':
1614 start_commit = optarg;
1615 break;
1616 case 'C':
1617 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1618 &errstr);
1619 if (errstr != NULL)
1620 err(1, "-C option %s", errstr);
1621 break;
1622 case 'l':
1623 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1624 if (errstr != NULL)
1625 err(1, "-l option %s", errstr);
1626 break;
1627 case 'f':
1628 first_parent_traversal = 1;
1629 break;
1630 case 'r':
1631 repo_path = realpath(optarg, NULL);
1632 if (repo_path == NULL)
1633 err(1, "-r option");
1634 got_path_strip_trailing_slashes(repo_path);
1635 break;
1636 default:
1637 usage_log();
1638 /* NOTREACHED */
1642 argc -= optind;
1643 argv += optind;
1645 cwd = getcwd(NULL, 0);
1646 if (cwd == NULL) {
1647 error = got_error_from_errno("getcwd");
1648 goto done;
1651 error = got_worktree_open(&worktree, cwd);
1652 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1653 goto done;
1654 error = NULL;
1656 if (argc == 0) {
1657 path = strdup("");
1658 if (path == NULL) {
1659 error = got_error_from_errno("strdup");
1660 goto done;
1662 } else if (argc == 1) {
1663 if (worktree) {
1664 error = got_worktree_resolve_path(&path, worktree,
1665 argv[0]);
1666 if (error)
1667 goto done;
1668 } else {
1669 path = strdup(argv[0]);
1670 if (path == NULL) {
1671 error = got_error_from_errno("strdup");
1672 goto done;
1675 } else
1676 usage_log();
1678 if (repo_path == NULL) {
1679 repo_path = worktree ?
1680 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1682 if (repo_path == NULL) {
1683 error = got_error_from_errno("strdup");
1684 goto done;
1687 error = got_repo_open(&repo, repo_path);
1688 if (error != NULL)
1689 goto done;
1691 error = apply_unveil(got_repo_get_path(repo), 1,
1692 worktree ? got_worktree_get_root_path(worktree) : NULL);
1693 if (error)
1694 goto done;
1696 if (start_commit == NULL) {
1697 struct got_reference *head_ref;
1698 error = got_ref_open(&head_ref, repo,
1699 worktree ? got_worktree_get_head_ref_name(worktree)
1700 : GOT_REF_HEAD, 0);
1701 if (error != NULL)
1702 return error;
1703 error = got_ref_resolve(&id, repo, head_ref);
1704 got_ref_close(head_ref);
1705 if (error != NULL)
1706 return error;
1707 error = got_object_open_as_commit(&commit, repo, id);
1708 } else {
1709 struct got_reference *ref;
1710 error = got_ref_open(&ref, repo, start_commit, 0);
1711 if (error == NULL) {
1712 int obj_type;
1713 error = got_ref_resolve(&id, repo, ref);
1714 got_ref_close(ref);
1715 if (error != NULL)
1716 goto done;
1717 error = got_object_get_type(&obj_type, repo, id);
1718 if (error != NULL)
1719 goto done;
1720 if (obj_type == GOT_OBJ_TYPE_TAG) {
1721 struct got_tag_object *tag;
1722 error = got_object_open_as_tag(&tag, repo, id);
1723 if (error != NULL)
1724 goto done;
1725 if (got_object_tag_get_object_type(tag) !=
1726 GOT_OBJ_TYPE_COMMIT) {
1727 got_object_tag_close(tag);
1728 error = got_error(GOT_ERR_OBJ_TYPE);
1729 goto done;
1731 free(id);
1732 id = got_object_id_dup(
1733 got_object_tag_get_object_id(tag));
1734 if (id == NULL)
1735 error = got_error_from_errno(
1736 "got_object_id_dup");
1737 got_object_tag_close(tag);
1738 if (error)
1739 goto done;
1740 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1741 error = got_error(GOT_ERR_OBJ_TYPE);
1742 goto done;
1744 error = got_object_open_as_commit(&commit, repo, id);
1745 if (error != NULL)
1746 goto done;
1748 if (commit == NULL) {
1749 error = got_repo_match_object_id_prefix(&id,
1750 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1751 if (error != NULL)
1752 return error;
1755 if (error != NULL)
1756 goto done;
1758 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1759 if (error != NULL)
1760 goto done;
1761 if (in_repo_path) {
1762 free(path);
1763 path = in_repo_path;
1766 error = got_ref_list(&refs, repo);
1767 if (error)
1768 goto done;
1770 error = print_commits(id, repo, path, show_patch,
1771 diff_context, limit, first_parent_traversal, &refs);
1772 done:
1773 free(path);
1774 free(repo_path);
1775 free(cwd);
1776 free(id);
1777 if (worktree)
1778 got_worktree_close(worktree);
1779 if (repo) {
1780 const struct got_error *repo_error;
1781 repo_error = got_repo_close(repo);
1782 if (error == NULL)
1783 error = repo_error;
1785 got_ref_list_free(&refs);
1786 return error;
1789 __dead static void
1790 usage_diff(void)
1792 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1793 "[object1 object2 | path]\n", getprogname());
1794 exit(1);
1797 struct print_diff_arg {
1798 struct got_repository *repo;
1799 struct got_worktree *worktree;
1800 int diff_context;
1801 const char *id_str;
1802 int header_shown;
1803 int diff_staged;
1806 static const struct got_error *
1807 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1808 const char *path, struct got_object_id *blob_id,
1809 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1811 struct print_diff_arg *a = arg;
1812 const struct got_error *err = NULL;
1813 struct got_blob_object *blob1 = NULL;
1814 FILE *f2 = NULL;
1815 char *abspath = NULL, *label1 = NULL;
1816 struct stat sb;
1818 if (a->diff_staged) {
1819 if (staged_status != GOT_STATUS_MODIFY &&
1820 staged_status != GOT_STATUS_ADD &&
1821 staged_status != GOT_STATUS_DELETE)
1822 return NULL;
1823 } else {
1824 if (staged_status == GOT_STATUS_DELETE)
1825 return NULL;
1826 if (status != GOT_STATUS_MODIFY &&
1827 status != GOT_STATUS_ADD &&
1828 status != GOT_STATUS_DELETE &&
1829 status != GOT_STATUS_CONFLICT)
1830 return NULL;
1833 if (!a->header_shown) {
1834 printf("diff %s %s%s\n", a->id_str,
1835 got_worktree_get_root_path(a->worktree),
1836 a->diff_staged ? " (staged changes)" : "");
1837 a->header_shown = 1;
1840 if (a->diff_staged) {
1841 const char *label1 = NULL, *label2 = NULL;
1842 switch (staged_status) {
1843 case GOT_STATUS_MODIFY:
1844 label1 = path;
1845 label2 = path;
1846 break;
1847 case GOT_STATUS_ADD:
1848 label2 = path;
1849 break;
1850 case GOT_STATUS_DELETE:
1851 label1 = path;
1852 break;
1853 default:
1854 return got_error(GOT_ERR_FILE_STATUS);
1856 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1857 label1, label2, a->diff_context, a->repo, stdout);
1860 if (staged_status == GOT_STATUS_ADD ||
1861 staged_status == GOT_STATUS_MODIFY) {
1862 char *id_str;
1863 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1864 8192);
1865 if (err)
1866 goto done;
1867 err = got_object_id_str(&id_str, staged_blob_id);
1868 if (err)
1869 goto done;
1870 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1871 err = got_error_from_errno("asprintf");
1872 free(id_str);
1873 goto done;
1875 free(id_str);
1876 } else if (status != GOT_STATUS_ADD) {
1877 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1878 if (err)
1879 goto done;
1882 if (status != GOT_STATUS_DELETE) {
1883 if (asprintf(&abspath, "%s/%s",
1884 got_worktree_get_root_path(a->worktree), path) == -1) {
1885 err = got_error_from_errno("asprintf");
1886 goto done;
1889 f2 = fopen(abspath, "r");
1890 if (f2 == NULL) {
1891 err = got_error_from_errno2("fopen", abspath);
1892 goto done;
1894 if (lstat(abspath, &sb) == -1) {
1895 err = got_error_from_errno2("lstat", abspath);
1896 goto done;
1898 } else
1899 sb.st_size = 0;
1901 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1902 a->diff_context, stdout);
1903 done:
1904 if (blob1)
1905 got_object_blob_close(blob1);
1906 if (f2 && fclose(f2) != 0 && err == NULL)
1907 err = got_error_from_errno("fclose");
1908 free(abspath);
1909 return err;
1912 static const struct got_error *
1913 match_object_id(struct got_object_id **id, char **label,
1914 const char *id_str, int obj_type, int resolve_tags,
1915 struct got_repository *repo)
1917 const struct got_error *err;
1918 struct got_tag_object *tag;
1919 struct got_reference *ref = NULL;
1921 *id = NULL;
1922 *label = NULL;
1924 if (resolve_tags) {
1925 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1926 repo);
1927 if (err == NULL) {
1928 *id = got_object_id_dup(
1929 got_object_tag_get_object_id(tag));
1930 if (*id == NULL)
1931 err = got_error_from_errno("got_object_id_dup");
1932 else if (asprintf(label, "refs/tags/%s",
1933 got_object_tag_get_name(tag)) == -1) {
1934 err = got_error_from_errno("asprintf");
1935 free(id);
1936 *id = NULL;
1938 got_object_tag_close(tag);
1939 return err;
1940 } else if (err->code != GOT_ERR_NO_OBJ)
1941 return err;
1944 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1945 if (err) {
1946 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1947 return err;
1948 err = got_ref_open(&ref, repo, id_str, 0);
1949 if (err != NULL)
1950 goto done;
1951 *label = strdup(got_ref_get_name(ref));
1952 if (*label == NULL) {
1953 err = got_error_from_errno("strdup");
1954 goto done;
1956 err = got_ref_resolve(id, repo, ref);
1957 } else {
1958 err = got_object_id_str(label, *id);
1959 if (*label == NULL) {
1960 err = got_error_from_errno("strdup");
1961 goto done;
1964 done:
1965 if (ref)
1966 got_ref_close(ref);
1967 return err;
1971 static const struct got_error *
1972 cmd_diff(int argc, char *argv[])
1974 const struct got_error *error;
1975 struct got_repository *repo = NULL;
1976 struct got_worktree *worktree = NULL;
1977 char *cwd = NULL, *repo_path = NULL;
1978 struct got_object_id *id1 = NULL, *id2 = NULL;
1979 const char *id_str1 = NULL, *id_str2 = NULL;
1980 char *label1 = NULL, *label2 = NULL;
1981 int type1, type2;
1982 int diff_context = 3, diff_staged = 0, ch;
1983 const char *errstr;
1984 char *path = NULL;
1986 #ifndef PROFILE
1987 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1988 NULL) == -1)
1989 err(1, "pledge");
1990 #endif
1992 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1993 switch (ch) {
1994 case 'C':
1995 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1996 if (errstr != NULL)
1997 err(1, "-C option %s", errstr);
1998 break;
1999 case 'r':
2000 repo_path = realpath(optarg, NULL);
2001 if (repo_path == NULL)
2002 err(1, "-r option");
2003 got_path_strip_trailing_slashes(repo_path);
2004 break;
2005 case 's':
2006 diff_staged = 1;
2007 break;
2008 default:
2009 usage_diff();
2010 /* NOTREACHED */
2014 argc -= optind;
2015 argv += optind;
2017 cwd = getcwd(NULL, 0);
2018 if (cwd == NULL) {
2019 error = got_error_from_errno("getcwd");
2020 goto done;
2022 error = got_worktree_open(&worktree, cwd);
2023 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2024 goto done;
2025 if (argc <= 1) {
2026 if (worktree == NULL) {
2027 error = got_error(GOT_ERR_NOT_WORKTREE);
2028 goto done;
2030 if (repo_path)
2031 errx(1,
2032 "-r option can't be used when diffing a work tree");
2033 repo_path = strdup(got_worktree_get_repo_path(worktree));
2034 if (repo_path == NULL) {
2035 error = got_error_from_errno("strdup");
2036 goto done;
2038 if (argc == 1) {
2039 error = got_worktree_resolve_path(&path, worktree,
2040 argv[0]);
2041 if (error)
2042 goto done;
2043 } else {
2044 path = strdup("");
2045 if (path == NULL) {
2046 error = got_error_from_errno("strdup");
2047 goto done;
2050 } else if (argc == 2) {
2051 if (diff_staged)
2052 errx(1, "-s option can't be used when diffing "
2053 "objects in repository");
2054 id_str1 = argv[0];
2055 id_str2 = argv[1];
2056 if (worktree && repo_path == NULL) {
2057 repo_path =
2058 strdup(got_worktree_get_repo_path(worktree));
2059 if (repo_path == NULL) {
2060 error = got_error_from_errno("strdup");
2061 goto done;
2064 } else
2065 usage_diff();
2067 if (repo_path == NULL) {
2068 repo_path = getcwd(NULL, 0);
2069 if (repo_path == NULL)
2070 return got_error_from_errno("getcwd");
2073 error = got_repo_open(&repo, repo_path);
2074 free(repo_path);
2075 if (error != NULL)
2076 goto done;
2078 error = apply_unveil(got_repo_get_path(repo), 1,
2079 worktree ? got_worktree_get_root_path(worktree) : NULL);
2080 if (error)
2081 goto done;
2083 if (argc <= 1) {
2084 struct print_diff_arg arg;
2085 struct got_pathlist_head paths;
2086 char *id_str;
2088 TAILQ_INIT(&paths);
2090 error = got_object_id_str(&id_str,
2091 got_worktree_get_base_commit_id(worktree));
2092 if (error)
2093 goto done;
2094 arg.repo = repo;
2095 arg.worktree = worktree;
2096 arg.diff_context = diff_context;
2097 arg.id_str = id_str;
2098 arg.header_shown = 0;
2099 arg.diff_staged = diff_staged;
2101 error = got_pathlist_append(&paths, path, NULL);
2102 if (error)
2103 goto done;
2105 error = got_worktree_status(worktree, &paths, repo, print_diff,
2106 &arg, check_cancelled, NULL);
2107 free(id_str);
2108 got_pathlist_free(&paths);
2109 goto done;
2112 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2113 repo);
2114 if (error)
2115 goto done;
2117 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2118 repo);
2119 if (error)
2120 goto done;
2122 error = got_object_get_type(&type1, repo, id1);
2123 if (error)
2124 goto done;
2126 error = got_object_get_type(&type2, repo, id2);
2127 if (error)
2128 goto done;
2130 if (type1 != type2) {
2131 error = got_error(GOT_ERR_OBJ_TYPE);
2132 goto done;
2135 switch (type1) {
2136 case GOT_OBJ_TYPE_BLOB:
2137 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2138 diff_context, repo, stdout);
2139 break;
2140 case GOT_OBJ_TYPE_TREE:
2141 error = got_diff_objects_as_trees(id1, id2, "", "",
2142 diff_context, repo, stdout);
2143 break;
2144 case GOT_OBJ_TYPE_COMMIT:
2145 printf("diff %s %s\n", label1, label2);
2146 error = got_diff_objects_as_commits(id1, id2, diff_context,
2147 repo, stdout);
2148 break;
2149 default:
2150 error = got_error(GOT_ERR_OBJ_TYPE);
2153 done:
2154 free(label1);
2155 free(label2);
2156 free(id1);
2157 free(id2);
2158 free(path);
2159 if (worktree)
2160 got_worktree_close(worktree);
2161 if (repo) {
2162 const struct got_error *repo_error;
2163 repo_error = got_repo_close(repo);
2164 if (error == NULL)
2165 error = repo_error;
2167 return error;
2170 __dead static void
2171 usage_blame(void)
2173 fprintf(stderr,
2174 "usage: %s blame [-c commit] [-r repository-path] path\n",
2175 getprogname());
2176 exit(1);
2179 struct blame_line {
2180 int annotated;
2181 char *id_str;
2182 char *committer;
2183 char datebuf[9]; /* YY-MM-DD + NUL */
2186 struct blame_cb_args {
2187 struct blame_line *lines;
2188 int nlines;
2189 int nlines_prec;
2190 int lineno_cur;
2191 off_t *line_offsets;
2192 FILE *f;
2193 struct got_repository *repo;
2196 static const struct got_error *
2197 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2199 const struct got_error *err = NULL;
2200 struct blame_cb_args *a = arg;
2201 struct blame_line *bline;
2202 char *line = NULL;
2203 size_t linesize = 0;
2204 struct got_commit_object *commit = NULL;
2205 off_t offset;
2206 struct tm tm;
2207 time_t committer_time;
2209 if (nlines != a->nlines ||
2210 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2211 return got_error(GOT_ERR_RANGE);
2213 if (sigint_received)
2214 return got_error(GOT_ERR_ITER_COMPLETED);
2216 if (lineno == -1)
2217 return NULL; /* no change in this commit */
2219 /* Annotate this line. */
2220 bline = &a->lines[lineno - 1];
2221 if (bline->annotated)
2222 return NULL;
2223 err = got_object_id_str(&bline->id_str, id);
2224 if (err)
2225 return err;
2227 err = got_object_open_as_commit(&commit, a->repo, id);
2228 if (err)
2229 goto done;
2231 bline->committer = strdup(got_object_commit_get_committer(commit));
2232 if (bline->committer == NULL) {
2233 err = got_error_from_errno("strdup");
2234 goto done;
2237 committer_time = got_object_commit_get_committer_time(commit);
2238 if (localtime_r(&committer_time, &tm) == NULL)
2239 return got_error_from_errno("localtime_r");
2240 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2241 &tm) >= sizeof(bline->datebuf)) {
2242 err = got_error(GOT_ERR_NO_SPACE);
2243 goto done;
2245 bline->annotated = 1;
2247 /* Print lines annotated so far. */
2248 bline = &a->lines[a->lineno_cur - 1];
2249 if (!bline->annotated)
2250 goto done;
2252 offset = a->line_offsets[a->lineno_cur - 1];
2253 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2254 err = got_error_from_errno("fseeko");
2255 goto done;
2258 while (bline->annotated) {
2259 char *smallerthan, *at, *nl, *committer;
2260 size_t len;
2262 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2263 if (ferror(a->f))
2264 err = got_error_from_errno("getline");
2265 break;
2268 committer = bline->committer;
2269 smallerthan = strchr(committer, '<');
2270 if (smallerthan && smallerthan[1] != '\0')
2271 committer = smallerthan + 1;
2272 at = strchr(committer, '@');
2273 if (at)
2274 *at = '\0';
2275 len = strlen(committer);
2276 if (len >= 9)
2277 committer[8] = '\0';
2279 nl = strchr(line, '\n');
2280 if (nl)
2281 *nl = '\0';
2282 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2283 bline->id_str, bline->datebuf, committer, line);
2285 a->lineno_cur++;
2286 bline = &a->lines[a->lineno_cur - 1];
2288 done:
2289 if (commit)
2290 got_object_commit_close(commit);
2291 free(line);
2292 return err;
2295 static const struct got_error *
2296 cmd_blame(int argc, char *argv[])
2298 const struct got_error *error;
2299 struct got_repository *repo = NULL;
2300 struct got_worktree *worktree = NULL;
2301 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2302 struct got_object_id *obj_id = NULL;
2303 struct got_object_id *commit_id = NULL;
2304 struct got_blob_object *blob = NULL;
2305 char *commit_id_str = NULL;
2306 struct blame_cb_args bca;
2307 int ch, obj_type, i;
2308 size_t filesize;
2310 memset(&bca, 0, sizeof(bca));
2312 #ifndef PROFILE
2313 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2314 NULL) == -1)
2315 err(1, "pledge");
2316 #endif
2318 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2319 switch (ch) {
2320 case 'c':
2321 commit_id_str = optarg;
2322 break;
2323 case 'r':
2324 repo_path = realpath(optarg, NULL);
2325 if (repo_path == NULL)
2326 err(1, "-r option");
2327 got_path_strip_trailing_slashes(repo_path);
2328 break;
2329 default:
2330 usage_blame();
2331 /* NOTREACHED */
2335 argc -= optind;
2336 argv += optind;
2338 if (argc == 1)
2339 path = argv[0];
2340 else
2341 usage_blame();
2343 cwd = getcwd(NULL, 0);
2344 if (cwd == NULL) {
2345 error = got_error_from_errno("getcwd");
2346 goto done;
2348 if (repo_path == NULL) {
2349 error = got_worktree_open(&worktree, cwd);
2350 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2351 goto done;
2352 else
2353 error = NULL;
2354 if (worktree) {
2355 repo_path =
2356 strdup(got_worktree_get_repo_path(worktree));
2357 if (repo_path == NULL)
2358 error = got_error_from_errno("strdup");
2359 if (error)
2360 goto done;
2361 } else {
2362 repo_path = strdup(cwd);
2363 if (repo_path == NULL) {
2364 error = got_error_from_errno("strdup");
2365 goto done;
2370 error = got_repo_open(&repo, repo_path);
2371 if (error != NULL)
2372 goto done;
2374 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2375 if (error)
2376 goto done;
2378 if (worktree) {
2379 const char *prefix = got_worktree_get_path_prefix(worktree);
2380 char *p, *worktree_subdir = cwd +
2381 strlen(got_worktree_get_root_path(worktree));
2382 if (asprintf(&p, "%s%s%s%s%s",
2383 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2384 worktree_subdir, worktree_subdir[0] ? "/" : "",
2385 path) == -1) {
2386 error = got_error_from_errno("asprintf");
2387 goto done;
2389 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2390 free(p);
2391 } else {
2392 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2394 if (error)
2395 goto done;
2397 if (commit_id_str == NULL) {
2398 struct got_reference *head_ref;
2399 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2400 if (error != NULL)
2401 goto done;
2402 error = got_ref_resolve(&commit_id, repo, head_ref);
2403 got_ref_close(head_ref);
2404 if (error != NULL)
2405 goto done;
2406 } else {
2407 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2408 if (error)
2409 goto done;
2412 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2413 if (error)
2414 goto done;
2415 if (obj_id == NULL) {
2416 error = got_error(GOT_ERR_NO_OBJ);
2417 goto done;
2420 error = got_object_get_type(&obj_type, repo, obj_id);
2421 if (error)
2422 goto done;
2424 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2425 error = got_error(GOT_ERR_OBJ_TYPE);
2426 goto done;
2429 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2430 if (error)
2431 goto done;
2432 bca.f = got_opentemp();
2433 if (bca.f == NULL) {
2434 error = got_error_from_errno("got_opentemp");
2435 goto done;
2437 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2438 &bca.line_offsets, bca.f, blob);
2439 if (error || bca.nlines == 0)
2440 goto done;
2442 /* Don't include \n at EOF in the blame line count. */
2443 if (bca.line_offsets[bca.nlines - 1] == filesize)
2444 bca.nlines--;
2446 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2447 if (bca.lines == NULL) {
2448 error = got_error_from_errno("calloc");
2449 goto done;
2451 bca.lineno_cur = 1;
2452 bca.nlines_prec = 0;
2453 i = bca.nlines;
2454 while (i > 0) {
2455 i /= 10;
2456 bca.nlines_prec++;
2458 bca.repo = repo;
2460 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2461 check_cancelled, NULL);
2462 if (error)
2463 goto done;
2464 done:
2465 free(in_repo_path);
2466 free(repo_path);
2467 free(cwd);
2468 free(commit_id);
2469 free(obj_id);
2470 if (blob)
2471 got_object_blob_close(blob);
2472 if (worktree)
2473 got_worktree_close(worktree);
2474 if (repo) {
2475 const struct got_error *repo_error;
2476 repo_error = got_repo_close(repo);
2477 if (error == NULL)
2478 error = repo_error;
2480 for (i = 0; i < bca.nlines; i++) {
2481 struct blame_line *bline = &bca.lines[i];
2482 free(bline->id_str);
2483 free(bline->committer);
2485 free(bca.lines);
2486 free(bca.line_offsets);
2487 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2488 error = got_error_from_errno("fclose");
2489 return error;
2492 __dead static void
2493 usage_tree(void)
2495 fprintf(stderr,
2496 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2497 getprogname());
2498 exit(1);
2501 static void
2502 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2503 const char *root_path)
2505 int is_root_path = (strcmp(path, root_path) == 0);
2506 const char *modestr = "";
2508 path += strlen(root_path);
2509 while (path[0] == '/')
2510 path++;
2512 if (S_ISLNK(te->mode))
2513 modestr = "@";
2514 else if (S_ISDIR(te->mode))
2515 modestr = "/";
2516 else if (te->mode & S_IXUSR)
2517 modestr = "*";
2519 printf("%s%s%s%s%s\n", id ? id : "", path,
2520 is_root_path ? "" : "/", te->name, modestr);
2523 static const struct got_error *
2524 print_tree(const char *path, struct got_object_id *commit_id,
2525 int show_ids, int recurse, const char *root_path,
2526 struct got_repository *repo)
2528 const struct got_error *err = NULL;
2529 struct got_object_id *tree_id = NULL;
2530 struct got_tree_object *tree = NULL;
2531 const struct got_tree_entries *entries;
2532 struct got_tree_entry *te;
2534 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2535 if (err)
2536 goto done;
2538 err = got_object_open_as_tree(&tree, repo, tree_id);
2539 if (err)
2540 goto done;
2541 entries = got_object_tree_get_entries(tree);
2542 te = SIMPLEQ_FIRST(&entries->head);
2543 while (te) {
2544 char *id = NULL;
2546 if (sigint_received || sigpipe_received)
2547 break;
2549 if (show_ids) {
2550 char *id_str;
2551 err = got_object_id_str(&id_str, te->id);
2552 if (err)
2553 goto done;
2554 if (asprintf(&id, "%s ", id_str) == -1) {
2555 err = got_error_from_errno("asprintf");
2556 free(id_str);
2557 goto done;
2559 free(id_str);
2561 print_entry(te, id, path, root_path);
2562 free(id);
2564 if (recurse && S_ISDIR(te->mode)) {
2565 char *child_path;
2566 if (asprintf(&child_path, "%s%s%s", path,
2567 path[0] == '/' && path[1] == '\0' ? "" : "/",
2568 te->name) == -1) {
2569 err = got_error_from_errno("asprintf");
2570 goto done;
2572 err = print_tree(child_path, commit_id, show_ids, 1,
2573 root_path, repo);
2574 free(child_path);
2575 if (err)
2576 goto done;
2579 te = SIMPLEQ_NEXT(te, entry);
2581 done:
2582 if (tree)
2583 got_object_tree_close(tree);
2584 free(tree_id);
2585 return err;
2588 static const struct got_error *
2589 cmd_tree(int argc, char *argv[])
2591 const struct got_error *error;
2592 struct got_repository *repo = NULL;
2593 struct got_worktree *worktree = NULL;
2594 const char *path;
2595 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2596 struct got_object_id *commit_id = NULL;
2597 char *commit_id_str = NULL;
2598 int show_ids = 0, recurse = 0;
2599 int ch;
2601 #ifndef PROFILE
2602 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2603 NULL) == -1)
2604 err(1, "pledge");
2605 #endif
2607 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2608 switch (ch) {
2609 case 'c':
2610 commit_id_str = optarg;
2611 break;
2612 case 'r':
2613 repo_path = realpath(optarg, NULL);
2614 if (repo_path == NULL)
2615 err(1, "-r option");
2616 got_path_strip_trailing_slashes(repo_path);
2617 break;
2618 case 'i':
2619 show_ids = 1;
2620 break;
2621 case 'R':
2622 recurse = 1;
2623 break;
2624 default:
2625 usage_tree();
2626 /* NOTREACHED */
2630 argc -= optind;
2631 argv += optind;
2633 if (argc == 1)
2634 path = argv[0];
2635 else if (argc > 1)
2636 usage_tree();
2637 else
2638 path = NULL;
2640 cwd = getcwd(NULL, 0);
2641 if (cwd == NULL) {
2642 error = got_error_from_errno("getcwd");
2643 goto done;
2645 if (repo_path == NULL) {
2646 error = got_worktree_open(&worktree, cwd);
2647 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2648 goto done;
2649 else
2650 error = NULL;
2651 if (worktree) {
2652 repo_path =
2653 strdup(got_worktree_get_repo_path(worktree));
2654 if (repo_path == NULL)
2655 error = got_error_from_errno("strdup");
2656 if (error)
2657 goto done;
2658 } else {
2659 repo_path = strdup(cwd);
2660 if (repo_path == NULL) {
2661 error = got_error_from_errno("strdup");
2662 goto done;
2667 error = got_repo_open(&repo, repo_path);
2668 if (error != NULL)
2669 goto done;
2671 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2672 if (error)
2673 goto done;
2675 if (path == NULL) {
2676 if (worktree) {
2677 char *p, *worktree_subdir = cwd +
2678 strlen(got_worktree_get_root_path(worktree));
2679 if (asprintf(&p, "%s/%s",
2680 got_worktree_get_path_prefix(worktree),
2681 worktree_subdir) == -1) {
2682 error = got_error_from_errno("asprintf");
2683 goto done;
2685 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2686 free(p);
2687 if (error)
2688 goto done;
2689 } else
2690 path = "/";
2692 if (in_repo_path == NULL) {
2693 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2694 if (error != NULL)
2695 goto done;
2698 if (commit_id_str == NULL) {
2699 struct got_reference *head_ref;
2700 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2701 if (error != NULL)
2702 goto done;
2703 error = got_ref_resolve(&commit_id, repo, head_ref);
2704 got_ref_close(head_ref);
2705 if (error != NULL)
2706 goto done;
2707 } else {
2708 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2709 if (error)
2710 goto done;
2713 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2714 in_repo_path, repo);
2715 done:
2716 free(in_repo_path);
2717 free(repo_path);
2718 free(cwd);
2719 free(commit_id);
2720 if (worktree)
2721 got_worktree_close(worktree);
2722 if (repo) {
2723 const struct got_error *repo_error;
2724 repo_error = got_repo_close(repo);
2725 if (error == NULL)
2726 error = repo_error;
2728 return error;
2731 __dead static void
2732 usage_status(void)
2734 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2735 exit(1);
2738 static const struct got_error *
2739 print_status(void *arg, unsigned char status, unsigned char staged_status,
2740 const char *path, struct got_object_id *blob_id,
2741 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2743 if (status == staged_status && (status == GOT_STATUS_DELETE))
2744 status = GOT_STATUS_NO_CHANGE;
2745 printf("%c%c %s\n", status, staged_status, path);
2746 return NULL;
2749 static const struct got_error *
2750 cmd_status(int argc, char *argv[])
2752 const struct got_error *error = NULL;
2753 struct got_repository *repo = NULL;
2754 struct got_worktree *worktree = NULL;
2755 char *cwd = NULL;
2756 struct got_pathlist_head paths;
2757 struct got_pathlist_entry *pe;
2758 int ch;
2760 TAILQ_INIT(&paths);
2762 while ((ch = getopt(argc, argv, "")) != -1) {
2763 switch (ch) {
2764 default:
2765 usage_status();
2766 /* NOTREACHED */
2770 argc -= optind;
2771 argv += optind;
2773 #ifndef PROFILE
2774 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2775 NULL) == -1)
2776 err(1, "pledge");
2777 #endif
2778 cwd = getcwd(NULL, 0);
2779 if (cwd == NULL) {
2780 error = got_error_from_errno("getcwd");
2781 goto done;
2784 error = got_worktree_open(&worktree, cwd);
2785 if (error != NULL)
2786 goto done;
2788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2789 if (error != NULL)
2790 goto done;
2792 error = apply_unveil(got_repo_get_path(repo), 1,
2793 got_worktree_get_root_path(worktree));
2794 if (error)
2795 goto done;
2797 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2798 if (error)
2799 goto done;
2801 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2802 check_cancelled, NULL);
2803 done:
2804 TAILQ_FOREACH(pe, &paths, entry)
2805 free((char *)pe->path);
2806 got_pathlist_free(&paths);
2807 free(cwd);
2808 return error;
2811 __dead static void
2812 usage_ref(void)
2814 fprintf(stderr,
2815 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2816 getprogname());
2817 exit(1);
2820 static const struct got_error *
2821 list_refs(struct got_repository *repo)
2823 static const struct got_error *err = NULL;
2824 struct got_reflist_head refs;
2825 struct got_reflist_entry *re;
2827 SIMPLEQ_INIT(&refs);
2828 err = got_ref_list(&refs, repo);
2829 if (err)
2830 return err;
2832 SIMPLEQ_FOREACH(re, &refs, entry) {
2833 char *refstr;
2834 refstr = got_ref_to_str(re->ref);
2835 if (refstr == NULL)
2836 return got_error_from_errno("got_ref_to_str");
2837 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2838 free(refstr);
2841 got_ref_list_free(&refs);
2842 return NULL;
2845 static const struct got_error *
2846 delete_ref(struct got_repository *repo, const char *refname)
2848 const struct got_error *err = NULL;
2849 struct got_reference *ref;
2851 err = got_ref_open(&ref, repo, refname, 0);
2852 if (err)
2853 return err;
2855 err = got_ref_delete(ref, repo);
2856 got_ref_close(ref);
2857 return err;
2860 static const struct got_error *
2861 add_ref(struct got_repository *repo, const char *refname, const char *target)
2863 const struct got_error *err = NULL;
2864 struct got_object_id *id;
2865 struct got_reference *ref = NULL;
2868 * Don't let the user create a reference named '-'.
2869 * While technically a valid reference name, this case is usually
2870 * an unintended typo.
2872 if (refname[0] == '-' && refname[1] == '\0')
2873 return got_error(GOT_ERR_BAD_REF_NAME);
2875 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2876 repo);
2877 if (err) {
2878 struct got_reference *target_ref;
2880 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2881 return err;
2882 err = got_ref_open(&target_ref, repo, target, 0);
2883 if (err)
2884 return err;
2885 err = got_ref_resolve(&id, repo, target_ref);
2886 got_ref_close(target_ref);
2887 if (err)
2888 return err;
2891 err = got_ref_alloc(&ref, refname, id);
2892 if (err)
2893 goto done;
2895 err = got_ref_write(ref, repo);
2896 done:
2897 if (ref)
2898 got_ref_close(ref);
2899 free(id);
2900 return err;
2903 static const struct got_error *
2904 add_symref(struct got_repository *repo, const char *refname, const char *target)
2906 const struct got_error *err = NULL;
2907 struct got_reference *ref = NULL;
2908 struct got_reference *target_ref = NULL;
2911 * Don't let the user create a reference named '-'.
2912 * While technically a valid reference name, this case is usually
2913 * an unintended typo.
2915 if (refname[0] == '-' && refname[1] == '\0')
2916 return got_error(GOT_ERR_BAD_REF_NAME);
2918 err = got_ref_open(&target_ref, repo, target, 0);
2919 if (err)
2920 return err;
2922 err = got_ref_alloc_symref(&ref, refname, target_ref);
2923 if (err)
2924 goto done;
2926 err = got_ref_write(ref, repo);
2927 done:
2928 if (target_ref)
2929 got_ref_close(target_ref);
2930 if (ref)
2931 got_ref_close(ref);
2932 return err;
2935 static const struct got_error *
2936 cmd_ref(int argc, char *argv[])
2938 const struct got_error *error = NULL;
2939 struct got_repository *repo = NULL;
2940 struct got_worktree *worktree = NULL;
2941 char *cwd = NULL, *repo_path = NULL;
2942 int ch, do_list = 0, create_symref = 0;
2943 const char *delref = NULL;
2945 /* TODO: Add -s option for adding symbolic references. */
2946 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2947 switch (ch) {
2948 case 'd':
2949 delref = optarg;
2950 break;
2951 case 'r':
2952 repo_path = realpath(optarg, NULL);
2953 if (repo_path == NULL)
2954 err(1, "-r option");
2955 got_path_strip_trailing_slashes(repo_path);
2956 break;
2957 case 'l':
2958 do_list = 1;
2959 break;
2960 case 's':
2961 create_symref = 1;
2962 break;
2963 default:
2964 usage_ref();
2965 /* NOTREACHED */
2969 if (do_list && delref)
2970 errx(1, "-l and -d options are mutually exclusive\n");
2972 argc -= optind;
2973 argv += optind;
2975 if (do_list || delref) {
2976 if (create_symref)
2977 errx(1, "-s option cannot be used together with the "
2978 "-l or -d options");
2979 if (argc > 0)
2980 usage_ref();
2981 } else if (argc != 2)
2982 usage_ref();
2984 #ifndef PROFILE
2985 if (do_list) {
2986 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2987 NULL) == -1)
2988 err(1, "pledge");
2989 } else {
2990 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2991 "sendfd unveil", NULL) == -1)
2992 err(1, "pledge");
2994 #endif
2995 cwd = getcwd(NULL, 0);
2996 if (cwd == NULL) {
2997 error = got_error_from_errno("getcwd");
2998 goto done;
3001 if (repo_path == NULL) {
3002 error = got_worktree_open(&worktree, cwd);
3003 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3004 goto done;
3005 else
3006 error = NULL;
3007 if (worktree) {
3008 repo_path =
3009 strdup(got_worktree_get_repo_path(worktree));
3010 if (repo_path == NULL)
3011 error = got_error_from_errno("strdup");
3012 if (error)
3013 goto done;
3014 } else {
3015 repo_path = strdup(cwd);
3016 if (repo_path == NULL) {
3017 error = got_error_from_errno("strdup");
3018 goto done;
3023 error = got_repo_open(&repo, repo_path);
3024 if (error != NULL)
3025 goto done;
3027 error = apply_unveil(got_repo_get_path(repo), do_list,
3028 worktree ? got_worktree_get_root_path(worktree) : NULL);
3029 if (error)
3030 goto done;
3032 if (do_list)
3033 error = list_refs(repo);
3034 else if (delref)
3035 error = delete_ref(repo, delref);
3036 else if (create_symref)
3037 error = add_symref(repo, argv[0], argv[1]);
3038 else
3039 error = add_ref(repo, argv[0], argv[1]);
3040 done:
3041 if (repo)
3042 got_repo_close(repo);
3043 if (worktree)
3044 got_worktree_close(worktree);
3045 free(cwd);
3046 free(repo_path);
3047 return error;
3050 __dead static void
3051 usage_branch(void)
3053 fprintf(stderr,
3054 "usage: %s branch [-r repository] -l | -d name | "
3055 "name [base-branch]\n", getprogname());
3056 exit(1);
3059 static const struct got_error *
3060 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3062 static const struct got_error *err = NULL;
3063 struct got_reflist_head refs;
3064 struct got_reflist_entry *re;
3066 SIMPLEQ_INIT(&refs);
3068 err = got_ref_list(&refs, repo);
3069 if (err)
3070 return err;
3072 SIMPLEQ_FOREACH(re, &refs, entry) {
3073 const char *refname, *marker = " ";
3074 char *refstr;
3075 refname = got_ref_get_name(re->ref);
3076 if (strncmp(refname, "refs/heads/", 11) != 0)
3077 continue;
3078 if (worktree && strcmp(refname,
3079 got_worktree_get_head_ref_name(worktree)) == 0) {
3080 struct got_object_id *id = NULL;
3081 err = got_ref_resolve(&id, repo, re->ref);
3082 if (err)
3083 return err;
3084 if (got_object_id_cmp(id,
3085 got_worktree_get_base_commit_id(worktree)) == 0)
3086 marker = "* ";
3087 else
3088 marker = "~ ";
3089 free(id);
3091 refname += 11;
3092 refstr = got_ref_to_str(re->ref);
3093 if (refstr == NULL)
3094 return got_error_from_errno("got_ref_to_str");
3095 printf("%s%s: %s\n", marker, refname, refstr);
3096 free(refstr);
3099 got_ref_list_free(&refs);
3100 return NULL;
3103 static const struct got_error *
3104 delete_branch(struct got_repository *repo, const char *branch_name)
3106 const struct got_error *err = NULL;
3107 struct got_reference *ref;
3108 char *refname;
3110 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3111 return got_error_from_errno("asprintf");
3113 err = got_ref_open(&ref, repo, refname, 0);
3114 if (err)
3115 goto done;
3117 err = got_ref_delete(ref, repo);
3118 got_ref_close(ref);
3119 done:
3120 free(refname);
3121 return err;
3124 static const struct got_error *
3125 add_branch(struct got_repository *repo, const char *branch_name,
3126 const char *base_branch)
3128 const struct got_error *err = NULL;
3129 struct got_object_id *id = NULL;
3130 struct got_reference *ref = NULL;
3131 char *base_refname = NULL, *refname = NULL;
3132 struct got_reference *base_ref;
3135 * Don't let the user create a branch named '-'.
3136 * While technically a valid reference name, this case is usually
3137 * an unintended typo.
3139 if (branch_name[0] == '-' && branch_name[1] == '\0')
3140 return got_error(GOT_ERR_BAD_REF_NAME);
3142 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3143 base_refname = strdup(GOT_REF_HEAD);
3144 if (base_refname == NULL)
3145 return got_error_from_errno("strdup");
3146 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3147 return got_error_from_errno("asprintf");
3149 err = got_ref_open(&base_ref, repo, base_refname, 0);
3150 if (err)
3151 goto done;
3152 err = got_ref_resolve(&id, repo, base_ref);
3153 got_ref_close(base_ref);
3154 if (err)
3155 goto done;
3157 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3158 err = got_error_from_errno("asprintf");
3159 goto done;
3162 err = got_ref_open(&ref, repo, refname, 0);
3163 if (err == NULL) {
3164 err = got_error(GOT_ERR_BRANCH_EXISTS);
3165 goto done;
3166 } else if (err->code != GOT_ERR_NOT_REF)
3167 goto done;
3169 err = got_ref_alloc(&ref, refname, id);
3170 if (err)
3171 goto done;
3173 err = got_ref_write(ref, repo);
3174 done:
3175 if (ref)
3176 got_ref_close(ref);
3177 free(id);
3178 free(base_refname);
3179 free(refname);
3180 return err;
3183 static const struct got_error *
3184 cmd_branch(int argc, char *argv[])
3186 const struct got_error *error = NULL;
3187 struct got_repository *repo = NULL;
3188 struct got_worktree *worktree = NULL;
3189 char *cwd = NULL, *repo_path = NULL;
3190 int ch, do_list = 0;
3191 const char *delref = NULL;
3193 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3194 switch (ch) {
3195 case 'd':
3196 delref = optarg;
3197 break;
3198 case 'r':
3199 repo_path = realpath(optarg, NULL);
3200 if (repo_path == NULL)
3201 err(1, "-r option");
3202 got_path_strip_trailing_slashes(repo_path);
3203 break;
3204 case 'l':
3205 do_list = 1;
3206 break;
3207 default:
3208 usage_branch();
3209 /* NOTREACHED */
3213 if (do_list && delref)
3214 errx(1, "-l and -d options are mutually exclusive\n");
3216 argc -= optind;
3217 argv += optind;
3219 if (do_list || delref) {
3220 if (argc > 0)
3221 usage_branch();
3222 } else if (argc < 1 || argc > 2)
3223 usage_branch();
3225 #ifndef PROFILE
3226 if (do_list) {
3227 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3228 NULL) == -1)
3229 err(1, "pledge");
3230 } else {
3231 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3232 "sendfd unveil", NULL) == -1)
3233 err(1, "pledge");
3235 #endif
3236 cwd = getcwd(NULL, 0);
3237 if (cwd == NULL) {
3238 error = got_error_from_errno("getcwd");
3239 goto done;
3242 if (repo_path == NULL) {
3243 error = got_worktree_open(&worktree, cwd);
3244 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3245 goto done;
3246 else
3247 error = NULL;
3248 if (worktree) {
3249 repo_path =
3250 strdup(got_worktree_get_repo_path(worktree));
3251 if (repo_path == NULL)
3252 error = got_error_from_errno("strdup");
3253 if (error)
3254 goto done;
3255 } else {
3256 repo_path = strdup(cwd);
3257 if (repo_path == NULL) {
3258 error = got_error_from_errno("strdup");
3259 goto done;
3264 error = got_repo_open(&repo, repo_path);
3265 if (error != NULL)
3266 goto done;
3268 error = apply_unveil(got_repo_get_path(repo), do_list,
3269 worktree ? got_worktree_get_root_path(worktree) : NULL);
3270 if (error)
3271 goto done;
3273 if (do_list)
3274 error = list_branches(repo, worktree);
3275 else if (delref)
3276 error = delete_branch(repo, delref);
3277 else {
3278 const char *base_branch;
3279 if (argc == 1) {
3280 base_branch = worktree ?
3281 got_worktree_get_head_ref_name(worktree) :
3282 GOT_REF_HEAD;
3283 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3284 base_branch += 11;
3285 } else
3286 base_branch = argv[1];
3287 error = add_branch(repo, argv[0], base_branch);
3289 done:
3290 if (repo)
3291 got_repo_close(repo);
3292 if (worktree)
3293 got_worktree_close(worktree);
3294 free(cwd);
3295 free(repo_path);
3296 return error;
3300 __dead static void
3301 usage_tag(void)
3303 fprintf(stderr,
3304 "usage: %s tag [-r repository] | -l | "
3305 "[-m message] name [commit]\n", getprogname());
3306 exit(1);
3309 static const struct got_error *
3310 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3312 static const struct got_error *err = NULL;
3313 struct got_reflist_head refs;
3314 struct got_reflist_entry *re;
3316 SIMPLEQ_INIT(&refs);
3318 err = got_ref_list(&refs, repo);
3319 if (err)
3320 return err;
3322 SIMPLEQ_FOREACH(re, &refs, entry) {
3323 const char *refname;
3324 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3325 char datebuf[26];
3326 time_t tagger_time;
3327 struct got_object_id *id;
3328 struct got_tag_object *tag;
3330 refname = got_ref_get_name(re->ref);
3331 if (strncmp(refname, "refs/tags/", 10) != 0)
3332 continue;
3333 refname += 10;
3334 refstr = got_ref_to_str(re->ref);
3335 if (refstr == NULL) {
3336 err = got_error_from_errno("got_ref_to_str");
3337 break;
3339 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3340 free(refstr);
3342 err = got_ref_resolve(&id, repo, re->ref);
3343 if (err)
3344 break;
3345 err = got_object_open_as_tag(&tag, repo, id);
3346 free(id);
3347 if (err)
3348 break;
3349 printf("from: %s\n", got_object_tag_get_tagger(tag));
3350 tagger_time = got_object_tag_get_tagger_time(tag);
3351 datestr = get_datestr(&tagger_time, datebuf);
3352 if (datestr)
3353 printf("date: %s UTC\n", datestr);
3354 err = got_object_id_str(&id_str,
3355 got_object_tag_get_object_id(tag));
3356 if (err)
3357 break;
3358 switch (got_object_tag_get_object_type(tag)) {
3359 case GOT_OBJ_TYPE_BLOB:
3360 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3361 break;
3362 case GOT_OBJ_TYPE_TREE:
3363 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3364 break;
3365 case GOT_OBJ_TYPE_COMMIT:
3366 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3367 break;
3368 case GOT_OBJ_TYPE_TAG:
3369 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3370 break;
3371 default:
3372 break;
3374 free(id_str);
3375 tagmsg0 = strdup(got_object_tag_get_message(tag));
3376 got_object_tag_close(tag);
3377 if (tagmsg0 == NULL) {
3378 err = got_error_from_errno("strdup");
3379 break;
3382 tagmsg = tagmsg0;
3383 do {
3384 line = strsep(&tagmsg, "\n");
3385 if (line)
3386 printf(" %s\n", line);
3387 } while (line);
3388 free(tagmsg0);
3391 got_ref_list_free(&refs);
3392 return NULL;
3395 static const struct got_error *
3396 get_tag_message(char **tagmsg, const char *commit_id_str,
3397 const char *tag_name, const char *repo_path)
3399 const struct got_error *err = NULL;
3400 char *template = NULL, *initial_content = NULL;
3401 char *tagmsg_path = NULL, *editor = NULL;
3402 int fd = -1;
3404 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3405 err = got_error_from_errno("asprintf");
3406 goto done;
3409 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3410 commit_id_str, tag_name) == -1) {
3411 err = got_error_from_errno("asprintf");
3412 goto done;
3415 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3416 if (err)
3417 goto done;
3419 dprintf(fd, initial_content);
3420 close(fd);
3422 err = get_editor(&editor);
3423 if (err)
3424 goto done;
3425 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3426 done:
3427 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3428 unlink(tagmsg_path);
3429 free(tagmsg_path);
3430 tagmsg_path = NULL;
3432 free(initial_content);
3433 free(template);
3434 free(editor);
3436 /* Editor is done; we can now apply unveil(2) */
3437 if (err == NULL) {
3438 err = apply_unveil(repo_path, 0, NULL);
3439 if (err) {
3440 free(*tagmsg);
3441 *tagmsg = NULL;
3444 return err;
3447 static const struct got_error *
3448 add_tag(struct got_repository *repo, const char *tag_name,
3449 const char *commit_arg, const char *tagmsg_arg)
3451 const struct got_error *err = NULL;
3452 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3453 char *label = NULL, *commit_id_str = NULL;
3454 struct got_reference *ref = NULL;
3455 char *refname = NULL, *tagmsg = NULL;
3456 const char *tagger;
3459 * Don't let the user create a tag named '-'.
3460 * While technically a valid reference name, this case is usually
3461 * an unintended typo.
3463 if (tag_name[0] == '-' && tag_name[1] == '\0')
3464 return got_error(GOT_ERR_BAD_REF_NAME);
3466 err = get_author(&tagger);
3467 if (err)
3468 return err;
3470 err = match_object_id(&commit_id, &label, commit_arg,
3471 GOT_OBJ_TYPE_COMMIT, 1, repo);
3472 if (err)
3473 goto done;
3475 err = got_object_id_str(&commit_id_str, commit_id);
3476 if (err)
3477 goto done;
3479 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3480 refname = strdup(tag_name);
3481 if (refname == NULL) {
3482 err = got_error_from_errno("strdup");
3483 goto done;
3485 tag_name += 10;
3486 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3487 err = got_error_from_errno("asprintf");
3488 goto done;
3491 err = got_ref_open(&ref, repo, refname, 0);
3492 if (err == NULL) {
3493 err = got_error(GOT_ERR_TAG_EXISTS);
3494 goto done;
3495 } else if (err->code != GOT_ERR_NOT_REF)
3496 goto done;
3498 if (tagmsg_arg == NULL) {
3499 err = get_tag_message(&tagmsg, commit_id_str,
3500 tag_name, got_repo_get_path(repo));
3501 if (err)
3502 goto done;
3505 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3506 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3507 if (err)
3508 goto done;
3510 err = got_ref_alloc(&ref, refname, tag_id);
3511 if (err)
3512 goto done;
3514 err = got_ref_write(ref, repo);
3516 if (err == NULL) {
3517 char *tag_id_str;
3518 err = got_object_id_str(&tag_id_str, tag_id);
3519 printf("Created tag %s\n", tag_id_str);
3520 free(tag_id_str);
3522 done:
3523 if (ref)
3524 got_ref_close(ref);
3525 free(commit_id);
3526 free(commit_id_str);
3527 free(refname);
3528 free(tagmsg);
3529 return err;
3532 static const struct got_error *
3533 cmd_tag(int argc, char *argv[])
3535 const struct got_error *error = NULL;
3536 struct got_repository *repo = NULL;
3537 struct got_worktree *worktree = NULL;
3538 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3539 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3540 int ch, do_list = 0;
3542 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3543 switch (ch) {
3544 case 'm':
3545 tagmsg = optarg;
3546 break;
3547 case 'r':
3548 repo_path = realpath(optarg, NULL);
3549 if (repo_path == NULL)
3550 err(1, "-r option");
3551 got_path_strip_trailing_slashes(repo_path);
3552 break;
3553 case 'l':
3554 do_list = 1;
3555 break;
3556 default:
3557 usage_tag();
3558 /* NOTREACHED */
3562 argc -= optind;
3563 argv += optind;
3565 if (do_list) {
3566 if (tagmsg)
3567 errx(1, "-l and -m options are mutually exclusive\n");
3568 if (argc > 0)
3569 usage_tag();
3570 } else if (argc < 1 || argc > 2)
3571 usage_tag();
3572 else {
3573 tag_name = argv[0];
3574 if (argc > 1)
3575 commit_id_arg = argv[1];
3578 #ifndef PROFILE
3579 if (do_list) {
3580 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3581 NULL) == -1)
3582 err(1, "pledge");
3583 } else {
3584 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3585 "sendfd unveil", NULL) == -1)
3586 err(1, "pledge");
3588 #endif
3589 cwd = getcwd(NULL, 0);
3590 if (cwd == NULL) {
3591 error = got_error_from_errno("getcwd");
3592 goto done;
3595 if (repo_path == NULL) {
3596 error = got_worktree_open(&worktree, cwd);
3597 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3598 goto done;
3599 else
3600 error = NULL;
3601 if (worktree) {
3602 repo_path =
3603 strdup(got_worktree_get_repo_path(worktree));
3604 if (repo_path == NULL)
3605 error = got_error_from_errno("strdup");
3606 if (error)
3607 goto done;
3608 } else {
3609 repo_path = strdup(cwd);
3610 if (repo_path == NULL) {
3611 error = got_error_from_errno("strdup");
3612 goto done;
3617 error = got_repo_open(&repo, repo_path);
3618 if (error != NULL)
3619 goto done;
3622 if (do_list) {
3623 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3624 if (error)
3625 goto done;
3626 error = list_tags(repo, worktree);
3627 } else {
3628 if (tagmsg) {
3629 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3630 if (error)
3631 goto done;
3634 if (commit_id_arg == NULL) {
3635 struct got_reference *head_ref;
3636 struct got_object_id *commit_id;
3637 error = got_ref_open(&head_ref, repo,
3638 worktree ? got_worktree_get_head_ref_name(worktree)
3639 : GOT_REF_HEAD, 0);
3640 if (error)
3641 goto done;
3642 error = got_ref_resolve(&commit_id, repo, head_ref);
3643 got_ref_close(head_ref);
3644 if (error)
3645 goto done;
3646 error = got_object_id_str(&commit_id_str, commit_id);
3647 free(commit_id);
3648 if (error)
3649 goto done;
3652 error = add_tag(repo, tag_name,
3653 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3655 done:
3656 if (repo)
3657 got_repo_close(repo);
3658 if (worktree)
3659 got_worktree_close(worktree);
3660 free(cwd);
3661 free(repo_path);
3662 free(commit_id_str);
3663 return error;
3666 __dead static void
3667 usage_add(void)
3669 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3670 exit(1);
3673 static const struct got_error *
3674 cmd_add(int argc, char *argv[])
3676 const struct got_error *error = NULL;
3677 struct got_repository *repo = NULL;
3678 struct got_worktree *worktree = NULL;
3679 char *cwd = NULL;
3680 struct got_pathlist_head paths;
3681 struct got_pathlist_entry *pe;
3682 int ch;
3684 TAILQ_INIT(&paths);
3686 while ((ch = getopt(argc, argv, "")) != -1) {
3687 switch (ch) {
3688 default:
3689 usage_add();
3690 /* NOTREACHED */
3694 argc -= optind;
3695 argv += optind;
3697 #ifndef PROFILE
3698 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3699 NULL) == -1)
3700 err(1, "pledge");
3701 #endif
3702 if (argc < 1)
3703 usage_add();
3705 cwd = getcwd(NULL, 0);
3706 if (cwd == NULL) {
3707 error = got_error_from_errno("getcwd");
3708 goto done;
3711 error = got_worktree_open(&worktree, cwd);
3712 if (error)
3713 goto done;
3715 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3716 if (error != NULL)
3717 goto done;
3719 error = apply_unveil(got_repo_get_path(repo), 1,
3720 got_worktree_get_root_path(worktree));
3721 if (error)
3722 goto done;
3724 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3725 if (error)
3726 goto done;
3728 error = got_worktree_schedule_add(worktree, &paths, print_status,
3729 NULL, repo);
3730 done:
3731 if (repo)
3732 got_repo_close(repo);
3733 if (worktree)
3734 got_worktree_close(worktree);
3735 TAILQ_FOREACH(pe, &paths, entry)
3736 free((char *)pe->path);
3737 got_pathlist_free(&paths);
3738 free(cwd);
3739 return error;
3742 __dead static void
3743 usage_remove(void)
3745 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3746 exit(1);
3749 static const struct got_error *
3750 cmd_remove(int argc, char *argv[])
3752 const struct got_error *error = NULL;
3753 struct got_worktree *worktree = NULL;
3754 struct got_repository *repo = NULL;
3755 char *cwd = NULL;
3756 struct got_pathlist_head paths;
3757 struct got_pathlist_entry *pe;
3758 int ch, delete_local_mods = 0;
3760 TAILQ_INIT(&paths);
3762 while ((ch = getopt(argc, argv, "f")) != -1) {
3763 switch (ch) {
3764 case 'f':
3765 delete_local_mods = 1;
3766 break;
3767 default:
3768 usage_add();
3769 /* NOTREACHED */
3773 argc -= optind;
3774 argv += optind;
3776 #ifndef PROFILE
3777 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3778 NULL) == -1)
3779 err(1, "pledge");
3780 #endif
3781 if (argc < 1)
3782 usage_remove();
3784 cwd = getcwd(NULL, 0);
3785 if (cwd == NULL) {
3786 error = got_error_from_errno("getcwd");
3787 goto done;
3789 error = got_worktree_open(&worktree, cwd);
3790 if (error)
3791 goto done;
3793 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3794 if (error)
3795 goto done;
3797 error = apply_unveil(got_repo_get_path(repo), 1,
3798 got_worktree_get_root_path(worktree));
3799 if (error)
3800 goto done;
3802 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3803 if (error)
3804 goto done;
3806 error = got_worktree_schedule_delete(worktree, &paths,
3807 delete_local_mods, print_status, NULL, repo);
3808 if (error)
3809 goto done;
3810 done:
3811 if (repo)
3812 got_repo_close(repo);
3813 if (worktree)
3814 got_worktree_close(worktree);
3815 TAILQ_FOREACH(pe, &paths, entry)
3816 free((char *)pe->path);
3817 got_pathlist_free(&paths);
3818 free(cwd);
3819 return error;
3822 __dead static void
3823 usage_revert(void)
3825 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3826 "path ...\n", getprogname());
3827 exit(1);
3830 static const struct got_error *
3831 revert_progress(void *arg, unsigned char status, const char *path)
3833 while (path[0] == '/')
3834 path++;
3835 printf("%c %s\n", status, path);
3836 return NULL;
3839 struct choose_patch_arg {
3840 FILE *patch_script_file;
3841 const char *action;
3844 static const struct got_error *
3845 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3846 int nchanges, const char *action)
3848 char *line = NULL;
3849 size_t linesize = 0;
3850 ssize_t linelen;
3852 switch (status) {
3853 case GOT_STATUS_ADD:
3854 printf("A %s\n%s this addition? [y/n] ", path, action);
3855 break;
3856 case GOT_STATUS_DELETE:
3857 printf("D %s\n%s this deletion? [y/n] ", path, action);
3858 break;
3859 case GOT_STATUS_MODIFY:
3860 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3861 return got_error_from_errno("fseek");
3862 printf(GOT_COMMIT_SEP_STR);
3863 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3864 printf("%s", line);
3865 if (ferror(patch_file))
3866 return got_error_from_errno("getline");
3867 printf(GOT_COMMIT_SEP_STR);
3868 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3869 path, n, nchanges, action);
3870 break;
3871 default:
3872 return got_error_path(path, GOT_ERR_FILE_STATUS);
3875 return NULL;
3878 static const struct got_error *
3879 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3880 FILE *patch_file, int n, int nchanges)
3882 const struct got_error *err = NULL;
3883 char *line = NULL;
3884 size_t linesize = 0;
3885 ssize_t linelen;
3886 int resp = ' ';
3887 struct choose_patch_arg *a = arg;
3889 *choice = GOT_PATCH_CHOICE_NONE;
3891 if (a->patch_script_file) {
3892 char *nl;
3893 err = show_change(status, path, patch_file, n, nchanges,
3894 a->action);
3895 if (err)
3896 return err;
3897 linelen = getline(&line, &linesize, a->patch_script_file);
3898 if (linelen == -1) {
3899 if (ferror(a->patch_script_file))
3900 return got_error_from_errno("getline");
3901 return NULL;
3903 nl = strchr(line, '\n');
3904 if (nl)
3905 *nl = '\0';
3906 if (strcmp(line, "y") == 0) {
3907 *choice = GOT_PATCH_CHOICE_YES;
3908 printf("y\n");
3909 } else if (strcmp(line, "n") == 0) {
3910 *choice = GOT_PATCH_CHOICE_NO;
3911 printf("n\n");
3912 } else if (strcmp(line, "q") == 0 &&
3913 status == GOT_STATUS_MODIFY) {
3914 *choice = GOT_PATCH_CHOICE_QUIT;
3915 printf("q\n");
3916 } else
3917 printf("invalid response '%s'\n", line);
3918 free(line);
3919 return NULL;
3922 while (resp != 'y' && resp != 'n' && resp != 'q') {
3923 err = show_change(status, path, patch_file, n, nchanges,
3924 a->action);
3925 if (err)
3926 return err;
3927 resp = getchar();
3928 if (resp == '\n')
3929 resp = getchar();
3930 if (status == GOT_STATUS_MODIFY) {
3931 if (resp != 'y' && resp != 'n' && resp != 'q') {
3932 printf("invalid response '%c'\n", resp);
3933 resp = ' ';
3935 } else if (resp != 'y' && resp != 'n') {
3936 printf("invalid response '%c'\n", resp);
3937 resp = ' ';
3941 if (resp == 'y')
3942 *choice = GOT_PATCH_CHOICE_YES;
3943 else if (resp == 'n')
3944 *choice = GOT_PATCH_CHOICE_NO;
3945 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3946 *choice = GOT_PATCH_CHOICE_QUIT;
3948 return NULL;
3952 static const struct got_error *
3953 cmd_revert(int argc, char *argv[])
3955 const struct got_error *error = NULL;
3956 struct got_worktree *worktree = NULL;
3957 struct got_repository *repo = NULL;
3958 char *cwd = NULL, *path = NULL;
3959 struct got_pathlist_head paths;
3960 struct got_pathlist_entry *pe;
3961 int ch, can_recurse = 0, pflag = 0;
3962 FILE *patch_script_file = NULL;
3963 const char *patch_script_path = NULL;
3964 struct choose_patch_arg cpa;
3966 TAILQ_INIT(&paths);
3968 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3969 switch (ch) {
3970 case 'p':
3971 pflag = 1;
3972 break;
3973 case 'F':
3974 patch_script_path = optarg;
3975 break;
3976 case 'R':
3977 can_recurse = 1;
3978 break;
3979 default:
3980 usage_revert();
3981 /* NOTREACHED */
3985 argc -= optind;
3986 argv += optind;
3988 #ifndef PROFILE
3989 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3990 "unveil", NULL) == -1)
3991 err(1, "pledge");
3992 #endif
3993 if (argc < 1)
3994 usage_revert();
3995 if (patch_script_path && !pflag)
3996 errx(1, "-F option can only be used together with -p option");
3998 cwd = getcwd(NULL, 0);
3999 if (cwd == NULL) {
4000 error = got_error_from_errno("getcwd");
4001 goto done;
4003 error = got_worktree_open(&worktree, cwd);
4004 if (error)
4005 goto done;
4007 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4008 if (error != NULL)
4009 goto done;
4011 if (patch_script_path) {
4012 patch_script_file = fopen(patch_script_path, "r");
4013 if (patch_script_file == NULL) {
4014 error = got_error_from_errno2("fopen",
4015 patch_script_path);
4016 goto done;
4019 error = apply_unveil(got_repo_get_path(repo), 1,
4020 got_worktree_get_root_path(worktree));
4021 if (error)
4022 goto done;
4024 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4025 if (error)
4026 goto done;
4028 if (!can_recurse) {
4029 char *ondisk_path;
4030 struct stat sb;
4031 TAILQ_FOREACH(pe, &paths, entry) {
4032 if (asprintf(&ondisk_path, "%s/%s",
4033 got_worktree_get_root_path(worktree),
4034 pe->path) == -1) {
4035 error = got_error_from_errno("asprintf");
4036 goto done;
4038 if (lstat(ondisk_path, &sb) == -1) {
4039 if (errno == ENOENT) {
4040 free(ondisk_path);
4041 continue;
4043 error = got_error_from_errno2("lstat",
4044 ondisk_path);
4045 free(ondisk_path);
4046 goto done;
4048 free(ondisk_path);
4049 if (S_ISDIR(sb.st_mode)) {
4050 error = got_error_msg(GOT_ERR_BAD_PATH,
4051 "reverting directories requires -R option");
4052 goto done;
4057 cpa.patch_script_file = patch_script_file;
4058 cpa.action = "revert";
4059 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4060 pflag ? choose_patch : NULL, &cpa, repo);
4061 if (error)
4062 goto done;
4063 done:
4064 if (patch_script_file && fclose(patch_script_file) == EOF &&
4065 error == NULL)
4066 error = got_error_from_errno2("fclose", patch_script_path);
4067 if (repo)
4068 got_repo_close(repo);
4069 if (worktree)
4070 got_worktree_close(worktree);
4071 free(path);
4072 free(cwd);
4073 return error;
4076 __dead static void
4077 usage_commit(void)
4079 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4080 getprogname());
4081 exit(1);
4084 struct collect_commit_logmsg_arg {
4085 const char *cmdline_log;
4086 const char *editor;
4087 const char *worktree_path;
4088 const char *branch_name;
4089 const char *repo_path;
4090 char *logmsg_path;
4094 static const struct got_error *
4095 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4096 void *arg)
4098 char *initial_content = NULL;
4099 struct got_pathlist_entry *pe;
4100 const struct got_error *err = NULL;
4101 char *template = NULL;
4102 struct collect_commit_logmsg_arg *a = arg;
4103 int fd;
4104 size_t len;
4106 /* if a message was specified on the command line, just use it */
4107 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4108 len = strlen(a->cmdline_log) + 1;
4109 *logmsg = malloc(len + 1);
4110 if (*logmsg == NULL)
4111 return got_error_from_errno("malloc");
4112 strlcpy(*logmsg, a->cmdline_log, len);
4113 return NULL;
4116 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4117 return got_error_from_errno("asprintf");
4119 if (asprintf(&initial_content,
4120 "\n# changes to be committed on branch %s:\n",
4121 a->branch_name) == -1)
4122 return got_error_from_errno("asprintf");
4124 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4125 if (err)
4126 goto done;
4128 dprintf(fd, initial_content);
4130 TAILQ_FOREACH(pe, commitable_paths, entry) {
4131 struct got_commitable *ct = pe->data;
4132 dprintf(fd, "# %c %s\n",
4133 got_commitable_get_status(ct),
4134 got_commitable_get_path(ct));
4136 close(fd);
4138 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4139 done:
4140 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4141 unlink(a->logmsg_path);
4142 free(a->logmsg_path);
4143 a->logmsg_path = NULL;
4145 free(initial_content);
4146 free(template);
4148 /* Editor is done; we can now apply unveil(2) */
4149 if (err == NULL) {
4150 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4151 if (err) {
4152 free(*logmsg);
4153 *logmsg = NULL;
4156 return err;
4159 static const struct got_error *
4160 cmd_commit(int argc, char *argv[])
4162 const struct got_error *error = NULL;
4163 struct got_worktree *worktree = NULL;
4164 struct got_repository *repo = NULL;
4165 char *cwd = NULL, *id_str = NULL;
4166 struct got_object_id *id = NULL;
4167 const char *logmsg = NULL;
4168 const char *author;
4169 struct collect_commit_logmsg_arg cl_arg;
4170 char *editor = NULL;
4171 int ch, rebase_in_progress, histedit_in_progress;
4172 struct got_pathlist_head paths;
4174 TAILQ_INIT(&paths);
4175 cl_arg.logmsg_path = NULL;
4177 while ((ch = getopt(argc, argv, "m:")) != -1) {
4178 switch (ch) {
4179 case 'm':
4180 logmsg = optarg;
4181 break;
4182 default:
4183 usage_commit();
4184 /* NOTREACHED */
4188 argc -= optind;
4189 argv += optind;
4191 #ifndef PROFILE
4192 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4193 "unveil", NULL) == -1)
4194 err(1, "pledge");
4195 #endif
4196 error = get_author(&author);
4197 if (error)
4198 return error;
4200 cwd = getcwd(NULL, 0);
4201 if (cwd == NULL) {
4202 error = got_error_from_errno("getcwd");
4203 goto done;
4205 error = got_worktree_open(&worktree, cwd);
4206 if (error)
4207 goto done;
4209 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4210 if (error)
4211 goto done;
4212 if (rebase_in_progress) {
4213 error = got_error(GOT_ERR_REBASING);
4214 goto done;
4217 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4218 worktree);
4219 if (error)
4220 goto done;
4222 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4223 if (error != NULL)
4224 goto done;
4227 * unveil(2) traverses exec(2); if an editor is used we have
4228 * to apply unveil after the log message has been written.
4230 if (logmsg == NULL || strlen(logmsg) == 0)
4231 error = get_editor(&editor);
4232 else
4233 error = apply_unveil(got_repo_get_path(repo), 0,
4234 got_worktree_get_root_path(worktree));
4235 if (error)
4236 goto done;
4238 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4239 if (error)
4240 goto done;
4242 cl_arg.editor = editor;
4243 cl_arg.cmdline_log = logmsg;
4244 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4245 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4246 if (!histedit_in_progress) {
4247 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4248 error = got_error(GOT_ERR_COMMIT_BRANCH);
4249 goto done;
4251 cl_arg.branch_name += 11;
4253 cl_arg.repo_path = got_repo_get_path(repo);
4254 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4255 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4256 if (error) {
4257 if (cl_arg.logmsg_path)
4258 fprintf(stderr, "%s: log message preserved in %s\n",
4259 getprogname(), cl_arg.logmsg_path);
4260 goto done;
4263 if (cl_arg.logmsg_path)
4264 unlink(cl_arg.logmsg_path);
4266 error = got_object_id_str(&id_str, id);
4267 if (error)
4268 goto done;
4269 printf("Created commit %s\n", id_str);
4270 done:
4271 free(cl_arg.logmsg_path);
4272 if (repo)
4273 got_repo_close(repo);
4274 if (worktree)
4275 got_worktree_close(worktree);
4276 free(cwd);
4277 free(id_str);
4278 free(editor);
4279 return error;
4282 __dead static void
4283 usage_cherrypick(void)
4285 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4286 exit(1);
4289 static const struct got_error *
4290 cmd_cherrypick(int argc, char *argv[])
4292 const struct got_error *error = NULL;
4293 struct got_worktree *worktree = NULL;
4294 struct got_repository *repo = NULL;
4295 char *cwd = NULL, *commit_id_str = NULL;
4296 struct got_object_id *commit_id = NULL;
4297 struct got_commit_object *commit = NULL;
4298 struct got_object_qid *pid;
4299 struct got_reference *head_ref = NULL;
4300 int ch, did_something = 0;
4302 while ((ch = getopt(argc, argv, "")) != -1) {
4303 switch (ch) {
4304 default:
4305 usage_cherrypick();
4306 /* NOTREACHED */
4310 argc -= optind;
4311 argv += optind;
4313 #ifndef PROFILE
4314 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4315 "unveil", NULL) == -1)
4316 err(1, "pledge");
4317 #endif
4318 if (argc != 1)
4319 usage_cherrypick();
4321 cwd = getcwd(NULL, 0);
4322 if (cwd == NULL) {
4323 error = got_error_from_errno("getcwd");
4324 goto done;
4326 error = got_worktree_open(&worktree, cwd);
4327 if (error)
4328 goto done;
4330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4331 if (error != NULL)
4332 goto done;
4334 error = apply_unveil(got_repo_get_path(repo), 0,
4335 got_worktree_get_root_path(worktree));
4336 if (error)
4337 goto done;
4339 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4340 GOT_OBJ_TYPE_COMMIT, repo);
4341 if (error != NULL) {
4342 struct got_reference *ref;
4343 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4344 goto done;
4345 error = got_ref_open(&ref, repo, argv[0], 0);
4346 if (error != NULL)
4347 goto done;
4348 error = got_ref_resolve(&commit_id, repo, ref);
4349 got_ref_close(ref);
4350 if (error != NULL)
4351 goto done;
4353 error = got_object_id_str(&commit_id_str, commit_id);
4354 if (error)
4355 goto done;
4357 error = got_ref_open(&head_ref, repo,
4358 got_worktree_get_head_ref_name(worktree), 0);
4359 if (error != NULL)
4360 goto done;
4362 error = check_same_branch(commit_id, head_ref, NULL, repo);
4363 if (error) {
4364 if (error->code != GOT_ERR_ANCESTRY)
4365 goto done;
4366 error = NULL;
4367 } else {
4368 error = got_error(GOT_ERR_SAME_BRANCH);
4369 goto done;
4372 error = got_object_open_as_commit(&commit, repo, commit_id);
4373 if (error)
4374 goto done;
4375 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4376 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4377 commit_id, repo, update_progress, &did_something, check_cancelled,
4378 NULL);
4379 if (error != NULL)
4380 goto done;
4382 if (did_something)
4383 printf("Merged commit %s\n", commit_id_str);
4384 done:
4385 if (commit)
4386 got_object_commit_close(commit);
4387 free(commit_id_str);
4388 if (head_ref)
4389 got_ref_close(head_ref);
4390 if (worktree)
4391 got_worktree_close(worktree);
4392 if (repo)
4393 got_repo_close(repo);
4394 return error;
4397 __dead static void
4398 usage_backout(void)
4400 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4401 exit(1);
4404 static const struct got_error *
4405 cmd_backout(int argc, char *argv[])
4407 const struct got_error *error = NULL;
4408 struct got_worktree *worktree = NULL;
4409 struct got_repository *repo = NULL;
4410 char *cwd = NULL, *commit_id_str = NULL;
4411 struct got_object_id *commit_id = NULL;
4412 struct got_commit_object *commit = NULL;
4413 struct got_object_qid *pid;
4414 struct got_reference *head_ref = NULL;
4415 int ch, did_something = 0;
4417 while ((ch = getopt(argc, argv, "")) != -1) {
4418 switch (ch) {
4419 default:
4420 usage_backout();
4421 /* NOTREACHED */
4425 argc -= optind;
4426 argv += optind;
4428 #ifndef PROFILE
4429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4430 "unveil", NULL) == -1)
4431 err(1, "pledge");
4432 #endif
4433 if (argc != 1)
4434 usage_backout();
4436 cwd = getcwd(NULL, 0);
4437 if (cwd == NULL) {
4438 error = got_error_from_errno("getcwd");
4439 goto done;
4441 error = got_worktree_open(&worktree, cwd);
4442 if (error)
4443 goto done;
4445 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4446 if (error != NULL)
4447 goto done;
4449 error = apply_unveil(got_repo_get_path(repo), 0,
4450 got_worktree_get_root_path(worktree));
4451 if (error)
4452 goto done;
4454 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4455 GOT_OBJ_TYPE_COMMIT, repo);
4456 if (error != NULL) {
4457 struct got_reference *ref;
4458 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4459 goto done;
4460 error = got_ref_open(&ref, repo, argv[0], 0);
4461 if (error != NULL)
4462 goto done;
4463 error = got_ref_resolve(&commit_id, repo, ref);
4464 got_ref_close(ref);
4465 if (error != NULL)
4466 goto done;
4468 error = got_object_id_str(&commit_id_str, commit_id);
4469 if (error)
4470 goto done;
4472 error = got_ref_open(&head_ref, repo,
4473 got_worktree_get_head_ref_name(worktree), 0);
4474 if (error != NULL)
4475 goto done;
4477 error = check_same_branch(commit_id, head_ref, NULL, repo);
4478 if (error)
4479 goto done;
4481 error = got_object_open_as_commit(&commit, repo, commit_id);
4482 if (error)
4483 goto done;
4484 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4485 if (pid == NULL) {
4486 error = got_error(GOT_ERR_ROOT_COMMIT);
4487 goto done;
4490 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4491 update_progress, &did_something, check_cancelled, NULL);
4492 if (error != NULL)
4493 goto done;
4495 if (did_something)
4496 printf("Backed out commit %s\n", commit_id_str);
4497 done:
4498 if (commit)
4499 got_object_commit_close(commit);
4500 free(commit_id_str);
4501 if (head_ref)
4502 got_ref_close(head_ref);
4503 if (worktree)
4504 got_worktree_close(worktree);
4505 if (repo)
4506 got_repo_close(repo);
4507 return error;
4510 __dead static void
4511 usage_rebase(void)
4513 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4514 getprogname());
4515 exit(1);
4518 void
4519 trim_logmsg(char *logmsg, int limit)
4521 char *nl;
4522 size_t len;
4524 len = strlen(logmsg);
4525 if (len > limit)
4526 len = limit;
4527 logmsg[len] = '\0';
4528 nl = strchr(logmsg, '\n');
4529 if (nl)
4530 *nl = '\0';
4533 static const struct got_error *
4534 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4536 const struct got_error *err;
4537 char *logmsg0 = NULL;
4538 const char *s;
4540 err = got_object_commit_get_logmsg(&logmsg0, commit);
4541 if (err)
4542 return err;
4544 s = logmsg0;
4545 while (isspace((unsigned char)s[0]))
4546 s++;
4548 *logmsg = strdup(s);
4549 if (*logmsg == NULL) {
4550 err = got_error_from_errno("strdup");
4551 goto done;
4554 trim_logmsg(*logmsg, limit);
4555 done:
4556 free(logmsg0);
4557 return err;
4560 static const struct got_error *
4561 show_rebase_progress(struct got_commit_object *commit,
4562 struct got_object_id *old_id, struct got_object_id *new_id)
4564 const struct got_error *err;
4565 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4567 err = got_object_id_str(&old_id_str, old_id);
4568 if (err)
4569 goto done;
4571 if (new_id) {
4572 err = got_object_id_str(&new_id_str, new_id);
4573 if (err)
4574 goto done;
4577 old_id_str[12] = '\0';
4578 if (new_id_str)
4579 new_id_str[12] = '\0';
4581 err = get_short_logmsg(&logmsg, 42, commit);
4582 if (err)
4583 goto done;
4585 printf("%s -> %s: %s\n", old_id_str,
4586 new_id_str ? new_id_str : "no-op change", logmsg);
4587 done:
4588 free(old_id_str);
4589 free(new_id_str);
4590 return err;
4593 static const struct got_error *
4594 rebase_progress(void *arg, unsigned char status, const char *path)
4596 unsigned char *rebase_status = arg;
4598 while (path[0] == '/')
4599 path++;
4600 printf("%c %s\n", status, path);
4602 if (*rebase_status == GOT_STATUS_CONFLICT)
4603 return NULL;
4604 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4605 *rebase_status = status;
4606 return NULL;
4609 static const struct got_error *
4610 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4611 struct got_reference *branch, struct got_reference *new_base_branch,
4612 struct got_reference *tmp_branch, struct got_repository *repo)
4614 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4615 return got_worktree_rebase_complete(worktree, fileindex,
4616 new_base_branch, tmp_branch, branch, repo);
4619 static const struct got_error *
4620 rebase_commit(struct got_pathlist_head *merged_paths,
4621 struct got_worktree *worktree, struct got_fileindex *fileindex,
4622 struct got_reference *tmp_branch,
4623 struct got_object_id *commit_id, struct got_repository *repo)
4625 const struct got_error *error;
4626 struct got_commit_object *commit;
4627 struct got_object_id *new_commit_id;
4629 error = got_object_open_as_commit(&commit, repo, commit_id);
4630 if (error)
4631 return error;
4633 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4634 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4635 if (error) {
4636 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4637 goto done;
4638 error = show_rebase_progress(commit, commit_id, NULL);
4639 } else {
4640 error = show_rebase_progress(commit, commit_id, new_commit_id);
4641 free(new_commit_id);
4643 done:
4644 got_object_commit_close(commit);
4645 return error;
4648 struct check_path_prefix_arg {
4649 const char *path_prefix;
4650 size_t len;
4651 int errcode;
4654 static const struct got_error *
4655 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4656 struct got_blob_object *blob2, struct got_object_id *id1,
4657 struct got_object_id *id2, const char *path1, const char *path2,
4658 struct got_repository *repo)
4660 struct check_path_prefix_arg *a = arg;
4662 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4663 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4664 return got_error(a->errcode);
4666 return NULL;
4669 static const struct got_error *
4670 check_path_prefix(struct got_object_id *parent_id,
4671 struct got_object_id *commit_id, const char *path_prefix,
4672 int errcode, struct got_repository *repo)
4674 const struct got_error *err;
4675 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4676 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4677 struct check_path_prefix_arg cpp_arg;
4679 if (got_path_is_root_dir(path_prefix))
4680 return NULL;
4682 err = got_object_open_as_commit(&commit, repo, commit_id);
4683 if (err)
4684 goto done;
4686 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4687 if (err)
4688 goto done;
4690 err = got_object_open_as_tree(&tree1, repo,
4691 got_object_commit_get_tree_id(parent_commit));
4692 if (err)
4693 goto done;
4695 err = got_object_open_as_tree(&tree2, repo,
4696 got_object_commit_get_tree_id(commit));
4697 if (err)
4698 goto done;
4700 cpp_arg.path_prefix = path_prefix;
4701 while (cpp_arg.path_prefix[0] == '/')
4702 cpp_arg.path_prefix++;
4703 cpp_arg.len = strlen(cpp_arg.path_prefix);
4704 cpp_arg.errcode = errcode;
4705 err = got_diff_tree(tree1, tree2, "", "", repo,
4706 check_path_prefix_in_diff, &cpp_arg, 0);
4707 done:
4708 if (tree1)
4709 got_object_tree_close(tree1);
4710 if (tree2)
4711 got_object_tree_close(tree2);
4712 if (commit)
4713 got_object_commit_close(commit);
4714 if (parent_commit)
4715 got_object_commit_close(parent_commit);
4716 return err;
4719 static const struct got_error *
4720 collect_commits(struct got_object_id_queue *commits,
4721 struct got_object_id *initial_commit_id,
4722 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4723 const char *path_prefix, int path_prefix_errcode,
4724 struct got_repository *repo)
4726 const struct got_error *err = NULL;
4727 struct got_commit_graph *graph = NULL;
4728 struct got_object_id *parent_id = NULL;
4729 struct got_object_qid *qid;
4730 struct got_object_id *commit_id = initial_commit_id;
4732 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4733 if (err)
4734 return err;
4736 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4737 check_cancelled, NULL);
4738 if (err)
4739 goto done;
4740 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4741 err = got_commit_graph_iter_next(&parent_id, graph);
4742 if (err) {
4743 if (err->code == GOT_ERR_ITER_COMPLETED) {
4744 err = got_error_msg(GOT_ERR_ANCESTRY,
4745 "ran out of commits to rebase before "
4746 "youngest common ancestor commit has "
4747 "been reached?!?");
4748 goto done;
4749 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4750 goto done;
4751 err = got_commit_graph_fetch_commits(graph, 1, repo,
4752 check_cancelled, NULL);
4753 if (err)
4754 goto done;
4755 } else {
4756 err = check_path_prefix(parent_id, commit_id,
4757 path_prefix, path_prefix_errcode, repo);
4758 if (err)
4759 goto done;
4761 err = got_object_qid_alloc(&qid, commit_id);
4762 if (err)
4763 goto done;
4764 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4765 commit_id = parent_id;
4768 done:
4769 got_commit_graph_close(graph);
4770 return err;
4773 static const struct got_error *
4774 cmd_rebase(int argc, char *argv[])
4776 const struct got_error *error = NULL;
4777 struct got_worktree *worktree = NULL;
4778 struct got_repository *repo = NULL;
4779 struct got_fileindex *fileindex = NULL;
4780 char *cwd = NULL;
4781 struct got_reference *branch = NULL;
4782 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4783 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4784 struct got_object_id *resume_commit_id = NULL;
4785 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4786 struct got_commit_object *commit = NULL;
4787 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4788 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4789 struct got_object_id_queue commits;
4790 struct got_pathlist_head merged_paths;
4791 const struct got_object_id_queue *parent_ids;
4792 struct got_object_qid *qid, *pid;
4794 SIMPLEQ_INIT(&commits);
4795 TAILQ_INIT(&merged_paths);
4797 while ((ch = getopt(argc, argv, "ac")) != -1) {
4798 switch (ch) {
4799 case 'a':
4800 abort_rebase = 1;
4801 break;
4802 case 'c':
4803 continue_rebase = 1;
4804 break;
4805 default:
4806 usage_rebase();
4807 /* NOTREACHED */
4811 argc -= optind;
4812 argv += optind;
4814 #ifndef PROFILE
4815 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4816 "unveil", NULL) == -1)
4817 err(1, "pledge");
4818 #endif
4819 if (abort_rebase && continue_rebase)
4820 usage_rebase();
4821 else if (abort_rebase || continue_rebase) {
4822 if (argc != 0)
4823 usage_rebase();
4824 } else if (argc != 1)
4825 usage_rebase();
4827 cwd = getcwd(NULL, 0);
4828 if (cwd == NULL) {
4829 error = got_error_from_errno("getcwd");
4830 goto done;
4832 error = got_worktree_open(&worktree, cwd);
4833 if (error)
4834 goto done;
4836 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4837 if (error != NULL)
4838 goto done;
4840 error = apply_unveil(got_repo_get_path(repo), 0,
4841 got_worktree_get_root_path(worktree));
4842 if (error)
4843 goto done;
4845 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4846 if (error)
4847 goto done;
4849 if (abort_rebase) {
4850 int did_something;
4851 if (!rebase_in_progress) {
4852 error = got_error(GOT_ERR_NOT_REBASING);
4853 goto done;
4855 error = got_worktree_rebase_continue(&resume_commit_id,
4856 &new_base_branch, &tmp_branch, &branch, &fileindex,
4857 worktree, repo);
4858 if (error)
4859 goto done;
4860 printf("Switching work tree to %s\n",
4861 got_ref_get_symref_target(new_base_branch));
4862 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4863 new_base_branch, update_progress, &did_something);
4864 if (error)
4865 goto done;
4866 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4867 goto done; /* nothing else to do */
4870 if (continue_rebase) {
4871 if (!rebase_in_progress) {
4872 error = got_error(GOT_ERR_NOT_REBASING);
4873 goto done;
4875 error = got_worktree_rebase_continue(&resume_commit_id,
4876 &new_base_branch, &tmp_branch, &branch, &fileindex,
4877 worktree, repo);
4878 if (error)
4879 goto done;
4881 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4882 resume_commit_id, repo);
4883 if (error)
4884 goto done;
4886 yca_id = got_object_id_dup(resume_commit_id);
4887 if (yca_id == NULL) {
4888 error = got_error_from_errno("got_object_id_dup");
4889 goto done;
4891 } else {
4892 error = got_ref_open(&branch, repo, argv[0], 0);
4893 if (error != NULL)
4894 goto done;
4897 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4898 if (error)
4899 goto done;
4901 if (!continue_rebase) {
4902 struct got_object_id *base_commit_id;
4904 base_commit_id = got_worktree_get_base_commit_id(worktree);
4905 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4906 base_commit_id, branch_head_commit_id, repo,
4907 check_cancelled, NULL);
4908 if (error)
4909 goto done;
4910 if (yca_id == NULL) {
4911 error = got_error_msg(GOT_ERR_ANCESTRY,
4912 "specified branch shares no common ancestry "
4913 "with work tree's branch");
4914 goto done;
4917 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4918 if (error) {
4919 if (error->code != GOT_ERR_ANCESTRY)
4920 goto done;
4921 error = NULL;
4922 } else {
4923 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4924 "specified branch resolves to a commit which "
4925 "is already contained in work tree's branch");
4926 goto done;
4928 error = got_worktree_rebase_prepare(&new_base_branch,
4929 &tmp_branch, &fileindex, worktree, branch, repo);
4930 if (error)
4931 goto done;
4934 commit_id = branch_head_commit_id;
4935 error = got_object_open_as_commit(&commit, repo, commit_id);
4936 if (error)
4937 goto done;
4939 parent_ids = got_object_commit_get_parent_ids(commit);
4940 pid = SIMPLEQ_FIRST(parent_ids);
4941 if (pid == NULL) {
4942 if (!continue_rebase) {
4943 int did_something;
4944 error = got_worktree_rebase_abort(worktree, fileindex,
4945 repo, new_base_branch, update_progress,
4946 &did_something);
4947 if (error)
4948 goto done;
4949 printf("Rebase of %s aborted\n",
4950 got_ref_get_name(branch));
4952 error = got_error(GOT_ERR_EMPTY_REBASE);
4953 goto done;
4955 error = collect_commits(&commits, commit_id, pid->id,
4956 yca_id, got_worktree_get_path_prefix(worktree),
4957 GOT_ERR_REBASE_PATH, repo);
4958 got_object_commit_close(commit);
4959 commit = NULL;
4960 if (error)
4961 goto done;
4963 if (SIMPLEQ_EMPTY(&commits)) {
4964 if (continue_rebase)
4965 error = rebase_complete(worktree, fileindex,
4966 branch, new_base_branch, tmp_branch, repo);
4967 else
4968 error = got_error(GOT_ERR_EMPTY_REBASE);
4969 goto done;
4972 pid = NULL;
4973 SIMPLEQ_FOREACH(qid, &commits, entry) {
4974 commit_id = qid->id;
4975 parent_id = pid ? pid->id : yca_id;
4976 pid = qid;
4978 error = got_worktree_rebase_merge_files(&merged_paths,
4979 worktree, fileindex, parent_id, commit_id, repo,
4980 rebase_progress, &rebase_status, check_cancelled, NULL);
4981 if (error)
4982 goto done;
4984 if (rebase_status == GOT_STATUS_CONFLICT) {
4985 got_worktree_rebase_pathlist_free(&merged_paths);
4986 break;
4989 error = rebase_commit(&merged_paths, worktree, fileindex,
4990 tmp_branch, commit_id, repo);
4991 got_worktree_rebase_pathlist_free(&merged_paths);
4992 if (error)
4993 goto done;
4996 if (rebase_status == GOT_STATUS_CONFLICT) {
4997 error = got_worktree_rebase_postpone(worktree, fileindex);
4998 if (error)
4999 goto done;
5000 error = got_error_msg(GOT_ERR_CONFLICTS,
5001 "conflicts must be resolved before rebasing can continue");
5002 } else
5003 error = rebase_complete(worktree, fileindex, branch,
5004 new_base_branch, tmp_branch, repo);
5005 done:
5006 got_object_id_queue_free(&commits);
5007 free(branch_head_commit_id);
5008 free(resume_commit_id);
5009 free(yca_id);
5010 if (commit)
5011 got_object_commit_close(commit);
5012 if (branch)
5013 got_ref_close(branch);
5014 if (new_base_branch)
5015 got_ref_close(new_base_branch);
5016 if (tmp_branch)
5017 got_ref_close(tmp_branch);
5018 if (worktree)
5019 got_worktree_close(worktree);
5020 if (repo)
5021 got_repo_close(repo);
5022 return error;
5025 __dead static void
5026 usage_histedit(void)
5028 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5029 getprogname());
5030 exit(1);
5033 #define GOT_HISTEDIT_PICK 'p'
5034 #define GOT_HISTEDIT_EDIT 'e'
5035 #define GOT_HISTEDIT_FOLD 'f'
5036 #define GOT_HISTEDIT_DROP 'd'
5037 #define GOT_HISTEDIT_MESG 'm'
5039 static struct got_histedit_cmd {
5040 unsigned char code;
5041 const char *name;
5042 const char *desc;
5043 } got_histedit_cmds[] = {
5044 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5045 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5046 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5047 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5048 { GOT_HISTEDIT_MESG, "mesg",
5049 "single-line log message for commit above (open editor if empty)" },
5052 struct got_histedit_list_entry {
5053 TAILQ_ENTRY(got_histedit_list_entry) entry;
5054 struct got_object_id *commit_id;
5055 const struct got_histedit_cmd *cmd;
5056 char *logmsg;
5058 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5060 static const struct got_error *
5061 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5062 FILE *f, struct got_repository *repo)
5064 const struct got_error *err = NULL;
5065 char *logmsg = NULL, *id_str = NULL;
5066 struct got_commit_object *commit = NULL;
5067 int n;
5069 err = got_object_open_as_commit(&commit, repo, commit_id);
5070 if (err)
5071 goto done;
5073 err = get_short_logmsg(&logmsg, 34, commit);
5074 if (err)
5075 goto done;
5077 err = got_object_id_str(&id_str, commit_id);
5078 if (err)
5079 goto done;
5081 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5082 if (n < 0)
5083 err = got_ferror(f, GOT_ERR_IO);
5084 done:
5085 if (commit)
5086 got_object_commit_close(commit);
5087 free(id_str);
5088 free(logmsg);
5089 return err;
5092 static const struct got_error *
5093 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5094 struct got_repository *repo)
5096 const struct got_error *err = NULL;
5097 struct got_object_qid *qid;
5099 if (SIMPLEQ_EMPTY(commits))
5100 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5102 SIMPLEQ_FOREACH(qid, commits, entry) {
5103 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5104 f, repo);
5105 if (err)
5106 break;
5109 return err;
5112 static const struct got_error *
5113 write_cmd_list(FILE *f)
5115 const struct got_error *err = NULL;
5116 int n, i;
5118 n = fprintf(f, "# Available histedit commands:\n");
5119 if (n < 0)
5120 return got_ferror(f, GOT_ERR_IO);
5122 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5123 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5124 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5125 cmd->desc);
5126 if (n < 0) {
5127 err = got_ferror(f, GOT_ERR_IO);
5128 break;
5131 n = fprintf(f, "# Commits will be processed in order from top to "
5132 "bottom of this file.\n");
5133 if (n < 0)
5134 return got_ferror(f, GOT_ERR_IO);
5135 return err;
5138 static const struct got_error *
5139 histedit_syntax_error(int lineno)
5141 static char msg[42];
5142 int ret;
5144 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5145 lineno);
5146 if (ret == -1 || ret >= sizeof(msg))
5147 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5149 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5152 static const struct got_error *
5153 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5154 char *logmsg, struct got_repository *repo)
5156 const struct got_error *err;
5157 struct got_commit_object *folded_commit = NULL;
5158 char *id_str, *folded_logmsg = NULL;
5160 err = got_object_id_str(&id_str, hle->commit_id);
5161 if (err)
5162 return err;
5164 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5165 if (err)
5166 goto done;
5168 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5169 if (err)
5170 goto done;
5171 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5172 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5173 folded_logmsg) == -1) {
5174 err = got_error_from_errno("asprintf");
5175 goto done;
5177 done:
5178 if (folded_commit)
5179 got_object_commit_close(folded_commit);
5180 free(id_str);
5181 free(folded_logmsg);
5182 return err;
5185 static struct got_histedit_list_entry *
5186 get_folded_commits(struct got_histedit_list_entry *hle)
5188 struct got_histedit_list_entry *prev, *folded = NULL;
5190 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5191 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5192 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5193 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5194 folded = prev;
5195 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5198 return folded;
5201 static const struct got_error *
5202 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5203 struct got_repository *repo)
5205 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5206 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5207 const struct got_error *err = NULL;
5208 struct got_commit_object *commit = NULL;
5209 int fd;
5210 struct got_histedit_list_entry *folded = NULL;
5212 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5213 if (err)
5214 return err;
5216 folded = get_folded_commits(hle);
5217 if (folded) {
5218 while (folded != hle) {
5219 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5220 folded = TAILQ_NEXT(folded, entry);
5221 continue;
5223 err = append_folded_commit_msg(&new_msg, folded,
5224 logmsg, repo);
5225 if (err)
5226 goto done;
5227 free(logmsg);
5228 logmsg = new_msg;
5229 folded = TAILQ_NEXT(folded, entry);
5233 err = got_object_id_str(&id_str, hle->commit_id);
5234 if (err)
5235 goto done;
5236 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5237 if (err)
5238 goto done;
5239 if (asprintf(&new_msg,
5240 "%s\n# original log message of commit %s: %s",
5241 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5242 err = got_error_from_errno("asprintf");
5243 goto done;
5245 free(logmsg);
5246 logmsg = new_msg;
5248 err = got_object_id_str(&id_str, hle->commit_id);
5249 if (err)
5250 goto done;
5252 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5253 if (err)
5254 goto done;
5256 dprintf(fd, logmsg);
5257 close(fd);
5259 err = get_editor(&editor);
5260 if (err)
5261 goto done;
5263 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5264 if (err) {
5265 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5266 goto done;
5267 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5269 done:
5270 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5271 err = got_error_from_errno2("unlink", logmsg_path);
5272 free(logmsg_path);
5273 free(logmsg);
5274 free(orig_logmsg);
5275 free(editor);
5276 if (commit)
5277 got_object_commit_close(commit);
5278 return err;
5281 static const struct got_error *
5282 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5283 FILE *f, struct got_repository *repo)
5285 const struct got_error *err = NULL;
5286 char *line = NULL, *p, *end;
5287 size_t size;
5288 ssize_t len;
5289 int lineno = 0, i;
5290 const struct got_histedit_cmd *cmd;
5291 struct got_object_id *commit_id = NULL;
5292 struct got_histedit_list_entry *hle = NULL;
5294 for (;;) {
5295 len = getline(&line, &size, f);
5296 if (len == -1) {
5297 const struct got_error *getline_err;
5298 if (feof(f))
5299 break;
5300 getline_err = got_error_from_errno("getline");
5301 err = got_ferror(f, getline_err->code);
5302 break;
5304 lineno++;
5305 p = line;
5306 while (isspace((unsigned char)p[0]))
5307 p++;
5308 if (p[0] == '#' || p[0] == '\0') {
5309 free(line);
5310 line = NULL;
5311 continue;
5313 cmd = NULL;
5314 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5315 cmd = &got_histedit_cmds[i];
5316 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5317 isspace((unsigned char)p[strlen(cmd->name)])) {
5318 p += strlen(cmd->name);
5319 break;
5321 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5322 p++;
5323 break;
5326 if (i == nitems(got_histedit_cmds)) {
5327 err = histedit_syntax_error(lineno);
5328 break;
5330 while (isspace((unsigned char)p[0]))
5331 p++;
5332 if (cmd->code == GOT_HISTEDIT_MESG) {
5333 if (hle == NULL || hle->logmsg != NULL) {
5334 err = got_error(GOT_ERR_HISTEDIT_CMD);
5335 break;
5337 if (p[0] == '\0') {
5338 err = histedit_edit_logmsg(hle, repo);
5339 if (err)
5340 break;
5341 } else {
5342 hle->logmsg = strdup(p);
5343 if (hle->logmsg == NULL) {
5344 err = got_error_from_errno("strdup");
5345 break;
5348 free(line);
5349 line = NULL;
5350 continue;
5351 } else {
5352 end = p;
5353 while (end[0] && !isspace((unsigned char)end[0]))
5354 end++;
5355 *end = '\0';
5357 err = got_object_resolve_id_str(&commit_id, repo, p);
5358 if (err) {
5359 /* override error code */
5360 err = histedit_syntax_error(lineno);
5361 break;
5364 hle = malloc(sizeof(*hle));
5365 if (hle == NULL) {
5366 err = got_error_from_errno("malloc");
5367 break;
5369 hle->cmd = cmd;
5370 hle->commit_id = commit_id;
5371 hle->logmsg = NULL;
5372 commit_id = NULL;
5373 free(line);
5374 line = NULL;
5375 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5378 free(line);
5379 free(commit_id);
5380 return err;
5383 static const struct got_error *
5384 histedit_check_script(struct got_histedit_list *histedit_cmds,
5385 struct got_object_id_queue *commits, struct got_repository *repo)
5387 const struct got_error *err = NULL;
5388 struct got_object_qid *qid;
5389 struct got_histedit_list_entry *hle;
5390 static char msg[80];
5391 char *id_str;
5393 if (TAILQ_EMPTY(histedit_cmds))
5394 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5395 "histedit script contains no commands");
5396 if (SIMPLEQ_EMPTY(commits))
5397 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5399 SIMPLEQ_FOREACH(qid, commits, entry) {
5400 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5401 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5402 break;
5404 if (hle == NULL) {
5405 err = got_object_id_str(&id_str, qid->id);
5406 if (err)
5407 return err;
5408 snprintf(msg, sizeof(msg),
5409 "commit %s missing from histedit script", id_str);
5410 free(id_str);
5411 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5415 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5416 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5417 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5418 "last commit in histedit script cannot be folded");
5420 return NULL;
5423 static const struct got_error *
5424 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5425 const char *path, struct got_object_id_queue *commits,
5426 struct got_repository *repo)
5428 const struct got_error *err = NULL;
5429 char *editor;
5430 FILE *f = NULL;
5432 err = get_editor(&editor);
5433 if (err)
5434 return err;
5436 if (spawn_editor(editor, path) == -1) {
5437 err = got_error_from_errno("failed spawning editor");
5438 goto done;
5441 f = fopen(path, "r");
5442 if (f == NULL) {
5443 err = got_error_from_errno("fopen");
5444 goto done;
5446 err = histedit_parse_list(histedit_cmds, f, repo);
5447 if (err)
5448 goto done;
5450 err = histedit_check_script(histedit_cmds, commits, repo);
5451 done:
5452 if (f && fclose(f) != 0 && err == NULL)
5453 err = got_error_from_errno("fclose");
5454 free(editor);
5455 return err;
5458 static const struct got_error *
5459 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5460 struct got_object_id_queue *, const char *, struct got_repository *);
5462 static const struct got_error *
5463 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5464 struct got_object_id_queue *commits, struct got_repository *repo)
5466 const struct got_error *err;
5467 FILE *f = NULL;
5468 char *path = NULL;
5470 err = got_opentemp_named(&path, &f, "got-histedit");
5471 if (err)
5472 return err;
5474 err = write_cmd_list(f);
5475 if (err)
5476 goto done;
5478 err = histedit_write_commit_list(commits, f, repo);
5479 if (err)
5480 goto done;
5482 if (fclose(f) != 0) {
5483 err = got_error_from_errno("fclose");
5484 goto done;
5486 f = NULL;
5488 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5489 if (err) {
5490 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5491 err->code != GOT_ERR_HISTEDIT_CMD)
5492 goto done;
5493 err = histedit_edit_list_retry(histedit_cmds, err,
5494 commits, path, repo);
5496 done:
5497 if (f && fclose(f) != 0 && err == NULL)
5498 err = got_error_from_errno("fclose");
5499 if (path && unlink(path) != 0 && err == NULL)
5500 err = got_error_from_errno2("unlink", path);
5501 free(path);
5502 return err;
5505 static const struct got_error *
5506 histedit_save_list(struct got_histedit_list *histedit_cmds,
5507 struct got_worktree *worktree, struct got_repository *repo)
5509 const struct got_error *err = NULL;
5510 char *path = NULL;
5511 FILE *f = NULL;
5512 struct got_histedit_list_entry *hle;
5513 struct got_commit_object *commit = NULL;
5515 err = got_worktree_get_histedit_script_path(&path, worktree);
5516 if (err)
5517 return err;
5519 f = fopen(path, "w");
5520 if (f == NULL) {
5521 err = got_error_from_errno2("fopen", path);
5522 goto done;
5524 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5525 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5526 repo);
5527 if (err)
5528 break;
5530 if (hle->logmsg) {
5531 int n = fprintf(f, "%c %s\n",
5532 GOT_HISTEDIT_MESG, hle->logmsg);
5533 if (n < 0) {
5534 err = got_ferror(f, GOT_ERR_IO);
5535 break;
5539 done:
5540 if (f && fclose(f) != 0 && err == NULL)
5541 err = got_error_from_errno("fclose");
5542 free(path);
5543 if (commit)
5544 got_object_commit_close(commit);
5545 return err;
5548 void
5549 histedit_free_list(struct got_histedit_list *histedit_cmds)
5551 struct got_histedit_list_entry *hle;
5553 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5554 TAILQ_REMOVE(histedit_cmds, hle, entry);
5555 free(hle);
5559 static const struct got_error *
5560 histedit_load_list(struct got_histedit_list *histedit_cmds,
5561 const char *path, struct got_repository *repo)
5563 const struct got_error *err = NULL;
5564 FILE *f = NULL;
5566 f = fopen(path, "r");
5567 if (f == NULL) {
5568 err = got_error_from_errno2("fopen", path);
5569 goto done;
5572 err = histedit_parse_list(histedit_cmds, f, repo);
5573 done:
5574 if (f && fclose(f) != 0 && err == NULL)
5575 err = got_error_from_errno("fclose");
5576 return err;
5579 static const struct got_error *
5580 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5581 const struct got_error *edit_err, struct got_object_id_queue *commits,
5582 const char *path, struct got_repository *repo)
5584 const struct got_error *err = NULL, *prev_err = edit_err;
5585 int resp = ' ';
5587 while (resp != 'c' && resp != 'r' && resp != 'a') {
5588 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5589 "or (a)bort: ", getprogname(), prev_err->msg);
5590 resp = getchar();
5591 if (resp == '\n')
5592 resp = getchar();
5593 if (resp == 'c') {
5594 histedit_free_list(histedit_cmds);
5595 err = histedit_run_editor(histedit_cmds, path, commits,
5596 repo);
5597 if (err) {
5598 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5599 err->code != GOT_ERR_HISTEDIT_CMD)
5600 break;
5601 prev_err = err;
5602 resp = ' ';
5603 continue;
5605 break;
5606 } else if (resp == 'r') {
5607 histedit_free_list(histedit_cmds);
5608 err = histedit_edit_script(histedit_cmds,
5609 commits, repo);
5610 if (err) {
5611 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5612 err->code != GOT_ERR_HISTEDIT_CMD)
5613 break;
5614 prev_err = err;
5615 resp = ' ';
5616 continue;
5618 break;
5619 } else if (resp == 'a') {
5620 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5621 break;
5622 } else
5623 printf("invalid response '%c'\n", resp);
5626 return err;
5629 static const struct got_error *
5630 histedit_complete(struct got_worktree *worktree,
5631 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5632 struct got_reference *branch, struct got_repository *repo)
5634 printf("Switching work tree to %s\n",
5635 got_ref_get_symref_target(branch));
5636 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5637 branch, repo);
5640 static const struct got_error *
5641 show_histedit_progress(struct got_commit_object *commit,
5642 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5644 const struct got_error *err;
5645 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5647 err = got_object_id_str(&old_id_str, hle->commit_id);
5648 if (err)
5649 goto done;
5651 if (new_id) {
5652 err = got_object_id_str(&new_id_str, new_id);
5653 if (err)
5654 goto done;
5657 old_id_str[12] = '\0';
5658 if (new_id_str)
5659 new_id_str[12] = '\0';
5661 if (hle->logmsg) {
5662 logmsg = strdup(hle->logmsg);
5663 if (logmsg == NULL) {
5664 err = got_error_from_errno("strdup");
5665 goto done;
5667 trim_logmsg(logmsg, 42);
5668 } else {
5669 err = get_short_logmsg(&logmsg, 42, commit);
5670 if (err)
5671 goto done;
5674 switch (hle->cmd->code) {
5675 case GOT_HISTEDIT_PICK:
5676 case GOT_HISTEDIT_EDIT:
5677 printf("%s -> %s: %s\n", old_id_str,
5678 new_id_str ? new_id_str : "no-op change", logmsg);
5679 break;
5680 case GOT_HISTEDIT_DROP:
5681 case GOT_HISTEDIT_FOLD:
5682 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5683 logmsg);
5684 break;
5685 default:
5686 break;
5689 done:
5690 free(old_id_str);
5691 free(new_id_str);
5692 return err;
5695 static const struct got_error *
5696 histedit_commit(struct got_pathlist_head *merged_paths,
5697 struct got_worktree *worktree, struct got_fileindex *fileindex,
5698 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5699 struct got_repository *repo)
5701 const struct got_error *err;
5702 struct got_commit_object *commit;
5703 struct got_object_id *new_commit_id;
5705 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5706 && hle->logmsg == NULL) {
5707 err = histedit_edit_logmsg(hle, repo);
5708 if (err)
5709 return err;
5712 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5713 if (err)
5714 return err;
5716 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5717 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5718 hle->logmsg, repo);
5719 if (err) {
5720 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5721 goto done;
5722 err = show_histedit_progress(commit, hle, NULL);
5723 } else {
5724 err = show_histedit_progress(commit, hle, new_commit_id);
5725 free(new_commit_id);
5727 done:
5728 got_object_commit_close(commit);
5729 return err;
5732 static const struct got_error *
5733 histedit_skip_commit(struct got_histedit_list_entry *hle,
5734 struct got_worktree *worktree, struct got_repository *repo)
5736 const struct got_error *error;
5737 struct got_commit_object *commit;
5739 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5740 repo);
5741 if (error)
5742 return error;
5744 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5745 if (error)
5746 return error;
5748 error = show_histedit_progress(commit, hle, NULL);
5749 got_object_commit_close(commit);
5750 return error;
5753 static const struct got_error *
5754 cmd_histedit(int argc, char *argv[])
5756 const struct got_error *error = NULL;
5757 struct got_worktree *worktree = NULL;
5758 struct got_fileindex *fileindex = NULL;
5759 struct got_repository *repo = NULL;
5760 char *cwd = NULL;
5761 struct got_reference *branch = NULL;
5762 struct got_reference *tmp_branch = NULL;
5763 struct got_object_id *resume_commit_id = NULL;
5764 struct got_object_id *base_commit_id = NULL;
5765 struct got_object_id *head_commit_id = NULL;
5766 struct got_commit_object *commit = NULL;
5767 int ch, rebase_in_progress = 0, did_something;
5768 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5769 const char *edit_script_path = NULL;
5770 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5771 struct got_object_id_queue commits;
5772 struct got_pathlist_head merged_paths;
5773 const struct got_object_id_queue *parent_ids;
5774 struct got_object_qid *pid;
5775 struct got_histedit_list histedit_cmds;
5776 struct got_histedit_list_entry *hle;
5778 SIMPLEQ_INIT(&commits);
5779 TAILQ_INIT(&histedit_cmds);
5780 TAILQ_INIT(&merged_paths);
5782 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5783 switch (ch) {
5784 case 'a':
5785 abort_edit = 1;
5786 break;
5787 case 'c':
5788 continue_edit = 1;
5789 break;
5790 case 'F':
5791 edit_script_path = optarg;
5792 break;
5793 default:
5794 usage_histedit();
5795 /* NOTREACHED */
5799 argc -= optind;
5800 argv += optind;
5802 #ifndef PROFILE
5803 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5804 "unveil", NULL) == -1)
5805 err(1, "pledge");
5806 #endif
5807 if (abort_edit && continue_edit)
5808 usage_histedit();
5809 if (argc != 0)
5810 usage_histedit();
5813 * This command cannot apply unveil(2) in all cases because the
5814 * user may choose to run an editor to edit the histedit script
5815 * and to edit individual commit log messages.
5816 * unveil(2) traverses exec(2); if an editor is used we have to
5817 * apply unveil after edit script and log messages have been written.
5818 * XXX TODO: Make use of unveil(2) where possible.
5821 cwd = getcwd(NULL, 0);
5822 if (cwd == NULL) {
5823 error = got_error_from_errno("getcwd");
5824 goto done;
5826 error = got_worktree_open(&worktree, cwd);
5827 if (error)
5828 goto done;
5830 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5831 if (error != NULL)
5832 goto done;
5834 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5835 if (error)
5836 goto done;
5837 if (rebase_in_progress) {
5838 error = got_error(GOT_ERR_REBASING);
5839 goto done;
5842 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5843 if (error)
5844 goto done;
5846 if (edit_in_progress && abort_edit) {
5847 error = got_worktree_histedit_continue(&resume_commit_id,
5848 &tmp_branch, &branch, &base_commit_id, &fileindex,
5849 worktree, repo);
5850 if (error)
5851 goto done;
5852 printf("Switching work tree to %s\n",
5853 got_ref_get_symref_target(branch));
5854 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5855 branch, base_commit_id, update_progress, &did_something);
5856 if (error)
5857 goto done;
5858 printf("Histedit of %s aborted\n",
5859 got_ref_get_symref_target(branch));
5860 goto done; /* nothing else to do */
5861 } else if (abort_edit) {
5862 error = got_error(GOT_ERR_NOT_HISTEDIT);
5863 goto done;
5866 if (continue_edit) {
5867 char *path;
5869 if (!edit_in_progress) {
5870 error = got_error(GOT_ERR_NOT_HISTEDIT);
5871 goto done;
5874 error = got_worktree_get_histedit_script_path(&path, worktree);
5875 if (error)
5876 goto done;
5878 error = histedit_load_list(&histedit_cmds, path, repo);
5879 free(path);
5880 if (error)
5881 goto done;
5883 error = got_worktree_histedit_continue(&resume_commit_id,
5884 &tmp_branch, &branch, &base_commit_id, &fileindex,
5885 worktree, repo);
5886 if (error)
5887 goto done;
5889 error = got_ref_resolve(&head_commit_id, repo, branch);
5890 if (error)
5891 goto done;
5893 error = got_object_open_as_commit(&commit, repo,
5894 head_commit_id);
5895 if (error)
5896 goto done;
5897 parent_ids = got_object_commit_get_parent_ids(commit);
5898 pid = SIMPLEQ_FIRST(parent_ids);
5899 if (pid == NULL) {
5900 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5901 goto done;
5903 error = collect_commits(&commits, head_commit_id, pid->id,
5904 base_commit_id, got_worktree_get_path_prefix(worktree),
5905 GOT_ERR_HISTEDIT_PATH, repo);
5906 got_object_commit_close(commit);
5907 commit = NULL;
5908 if (error)
5909 goto done;
5910 } else {
5911 if (edit_in_progress) {
5912 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5913 goto done;
5916 error = got_ref_open(&branch, repo,
5917 got_worktree_get_head_ref_name(worktree), 0);
5918 if (error != NULL)
5919 goto done;
5921 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5922 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5923 "will not edit commit history of a branch outside "
5924 "the \"refs/heads/\" reference namespace");
5925 goto done;
5928 error = got_ref_resolve(&head_commit_id, repo, branch);
5929 got_ref_close(branch);
5930 branch = NULL;
5931 if (error)
5932 goto done;
5934 error = got_object_open_as_commit(&commit, repo,
5935 head_commit_id);
5936 if (error)
5937 goto done;
5938 parent_ids = got_object_commit_get_parent_ids(commit);
5939 pid = SIMPLEQ_FIRST(parent_ids);
5940 if (pid == NULL) {
5941 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5942 goto done;
5944 error = collect_commits(&commits, head_commit_id, pid->id,
5945 got_worktree_get_base_commit_id(worktree),
5946 got_worktree_get_path_prefix(worktree),
5947 GOT_ERR_HISTEDIT_PATH, repo);
5948 got_object_commit_close(commit);
5949 commit = NULL;
5950 if (error)
5951 goto done;
5953 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5954 &base_commit_id, &fileindex, worktree, repo);
5955 if (error)
5956 goto done;
5958 if (edit_script_path) {
5959 error = histedit_load_list(&histedit_cmds,
5960 edit_script_path, repo);
5961 if (error) {
5962 got_worktree_histedit_abort(worktree, fileindex,
5963 repo, branch, base_commit_id,
5964 update_progress, &did_something);
5965 goto done;
5967 } else {
5968 error = histedit_edit_script(&histedit_cmds, &commits,
5969 repo);
5970 if (error) {
5971 got_worktree_histedit_abort(worktree, fileindex,
5972 repo, branch, base_commit_id,
5973 update_progress, &did_something);
5974 goto done;
5979 error = histedit_save_list(&histedit_cmds, worktree,
5980 repo);
5981 if (error) {
5982 got_worktree_histedit_abort(worktree, fileindex,
5983 repo, branch, base_commit_id,
5984 update_progress, &did_something);
5985 goto done;
5990 error = histedit_check_script(&histedit_cmds, &commits, repo);
5991 if (error)
5992 goto done;
5994 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5995 if (resume_commit_id) {
5996 if (got_object_id_cmp(hle->commit_id,
5997 resume_commit_id) != 0)
5998 continue;
6000 resume_commit_id = NULL;
6001 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6002 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6003 error = histedit_skip_commit(hle, worktree,
6004 repo);
6005 } else {
6006 error = histedit_commit(NULL, worktree,
6007 fileindex, tmp_branch, hle, repo);
6009 if (error)
6010 goto done;
6011 continue;
6014 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6015 error = histedit_skip_commit(hle, worktree, repo);
6016 if (error)
6017 goto done;
6018 continue;
6021 error = got_object_open_as_commit(&commit, repo,
6022 hle->commit_id);
6023 if (error)
6024 goto done;
6025 parent_ids = got_object_commit_get_parent_ids(commit);
6026 pid = SIMPLEQ_FIRST(parent_ids);
6028 error = got_worktree_histedit_merge_files(&merged_paths,
6029 worktree, fileindex, pid->id, hle->commit_id, repo,
6030 rebase_progress, &rebase_status, check_cancelled, NULL);
6031 if (error)
6032 goto done;
6033 got_object_commit_close(commit);
6034 commit = NULL;
6036 if (rebase_status == GOT_STATUS_CONFLICT) {
6037 got_worktree_rebase_pathlist_free(&merged_paths);
6038 break;
6041 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6042 char *id_str;
6043 error = got_object_id_str(&id_str, hle->commit_id);
6044 if (error)
6045 goto done;
6046 printf("Stopping histedit for amending commit %s\n",
6047 id_str);
6048 free(id_str);
6049 got_worktree_rebase_pathlist_free(&merged_paths);
6050 error = got_worktree_histedit_postpone(worktree,
6051 fileindex);
6052 goto done;
6055 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6056 error = histedit_skip_commit(hle, worktree, repo);
6057 if (error)
6058 goto done;
6059 continue;
6062 error = histedit_commit(&merged_paths, worktree, fileindex,
6063 tmp_branch, hle, repo);
6064 got_worktree_rebase_pathlist_free(&merged_paths);
6065 if (error)
6066 goto done;
6069 if (rebase_status == GOT_STATUS_CONFLICT) {
6070 error = got_worktree_histedit_postpone(worktree, fileindex);
6071 if (error)
6072 goto done;
6073 error = got_error_msg(GOT_ERR_CONFLICTS,
6074 "conflicts must be resolved before rebasing can continue");
6075 } else
6076 error = histedit_complete(worktree, fileindex, tmp_branch,
6077 branch, repo);
6078 done:
6079 got_object_id_queue_free(&commits);
6080 histedit_free_list(&histedit_cmds);
6081 free(head_commit_id);
6082 free(base_commit_id);
6083 free(resume_commit_id);
6084 if (commit)
6085 got_object_commit_close(commit);
6086 if (branch)
6087 got_ref_close(branch);
6088 if (tmp_branch)
6089 got_ref_close(tmp_branch);
6090 if (worktree)
6091 got_worktree_close(worktree);
6092 if (repo)
6093 got_repo_close(repo);
6094 return error;
6097 __dead static void
6098 usage_stage(void)
6100 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6101 "[file-path ...]\n",
6102 getprogname());
6103 exit(1);
6106 static const struct got_error *
6107 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6108 const char *path, struct got_object_id *blob_id,
6109 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6111 const struct got_error *err = NULL;
6112 char *id_str = NULL;
6114 if (staged_status != GOT_STATUS_ADD &&
6115 staged_status != GOT_STATUS_MODIFY &&
6116 staged_status != GOT_STATUS_DELETE)
6117 return NULL;
6119 if (staged_status == GOT_STATUS_ADD ||
6120 staged_status == GOT_STATUS_MODIFY)
6121 err = got_object_id_str(&id_str, staged_blob_id);
6122 else
6123 err = got_object_id_str(&id_str, blob_id);
6124 if (err)
6125 return err;
6127 printf("%s %c %s\n", id_str, staged_status, path);
6128 free(id_str);
6129 return NULL;
6132 static const struct got_error *
6133 cmd_stage(int argc, char *argv[])
6135 const struct got_error *error = NULL;
6136 struct got_repository *repo = NULL;
6137 struct got_worktree *worktree = NULL;
6138 char *cwd = NULL;
6139 struct got_pathlist_head paths;
6140 struct got_pathlist_entry *pe;
6141 int ch, list_stage = 0, pflag = 0;
6142 FILE *patch_script_file = NULL;
6143 const char *patch_script_path = NULL;
6144 struct choose_patch_arg cpa;
6146 TAILQ_INIT(&paths);
6148 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6149 switch (ch) {
6150 case 'l':
6151 list_stage = 1;
6152 break;
6153 case 'p':
6154 pflag = 1;
6155 break;
6156 case 'F':
6157 patch_script_path = optarg;
6158 break;
6159 default:
6160 usage_stage();
6161 /* NOTREACHED */
6165 argc -= optind;
6166 argv += optind;
6168 #ifndef PROFILE
6169 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6170 "unveil", NULL) == -1)
6171 err(1, "pledge");
6172 #endif
6173 if (list_stage && (pflag || patch_script_path))
6174 errx(1, "-l option cannot be used with other options");
6175 if (patch_script_path && !pflag)
6176 errx(1, "-F option can only be used together with -p option");
6178 cwd = getcwd(NULL, 0);
6179 if (cwd == NULL) {
6180 error = got_error_from_errno("getcwd");
6181 goto done;
6184 error = got_worktree_open(&worktree, cwd);
6185 if (error)
6186 goto done;
6188 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6189 if (error != NULL)
6190 goto done;
6192 if (patch_script_path) {
6193 patch_script_file = fopen(patch_script_path, "r");
6194 if (patch_script_file == NULL) {
6195 error = got_error_from_errno2("fopen",
6196 patch_script_path);
6197 goto done;
6200 error = apply_unveil(got_repo_get_path(repo), 1,
6201 got_worktree_get_root_path(worktree));
6202 if (error)
6203 goto done;
6205 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6206 if (error)
6207 goto done;
6209 if (list_stage)
6210 error = got_worktree_status(worktree, &paths, repo,
6211 print_stage, NULL, check_cancelled, NULL);
6212 else {
6213 cpa.patch_script_file = patch_script_file;
6214 cpa.action = "stage";
6215 error = got_worktree_stage(worktree, &paths,
6216 pflag ? NULL : print_status, NULL,
6217 pflag ? choose_patch : NULL, &cpa, repo);
6219 done:
6220 if (patch_script_file && fclose(patch_script_file) == EOF &&
6221 error == NULL)
6222 error = got_error_from_errno2("fclose", patch_script_path);
6223 if (repo)
6224 got_repo_close(repo);
6225 if (worktree)
6226 got_worktree_close(worktree);
6227 TAILQ_FOREACH(pe, &paths, entry)
6228 free((char *)pe->path);
6229 got_pathlist_free(&paths);
6230 free(cwd);
6231 return error;
6234 __dead static void
6235 usage_unstage(void)
6237 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6238 "[file-path ...]\n",
6239 getprogname());
6240 exit(1);
6244 static const struct got_error *
6245 cmd_unstage(int argc, char *argv[])
6247 const struct got_error *error = NULL;
6248 struct got_repository *repo = NULL;
6249 struct got_worktree *worktree = NULL;
6250 char *cwd = NULL;
6251 struct got_pathlist_head paths;
6252 struct got_pathlist_entry *pe;
6253 int ch, did_something = 0, pflag = 0;
6254 FILE *patch_script_file = NULL;
6255 const char *patch_script_path = NULL;
6256 struct choose_patch_arg cpa;
6258 TAILQ_INIT(&paths);
6260 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6261 switch (ch) {
6262 case 'p':
6263 pflag = 1;
6264 break;
6265 case 'F':
6266 patch_script_path = optarg;
6267 break;
6268 default:
6269 usage_unstage();
6270 /* NOTREACHED */
6274 argc -= optind;
6275 argv += optind;
6277 #ifndef PROFILE
6278 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6279 "unveil", NULL) == -1)
6280 err(1, "pledge");
6281 #endif
6282 if (patch_script_path && !pflag)
6283 errx(1, "-F option can only be used together with -p option");
6285 cwd = getcwd(NULL, 0);
6286 if (cwd == NULL) {
6287 error = got_error_from_errno("getcwd");
6288 goto done;
6291 error = got_worktree_open(&worktree, cwd);
6292 if (error)
6293 goto done;
6295 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6296 if (error != NULL)
6297 goto done;
6299 if (patch_script_path) {
6300 patch_script_file = fopen(patch_script_path, "r");
6301 if (patch_script_file == NULL) {
6302 error = got_error_from_errno2("fopen",
6303 patch_script_path);
6304 goto done;
6308 error = apply_unveil(got_repo_get_path(repo), 1,
6309 got_worktree_get_root_path(worktree));
6310 if (error)
6311 goto done;
6313 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6314 if (error)
6315 goto done;
6317 cpa.patch_script_file = patch_script_file;
6318 cpa.action = "unstage";
6319 error = got_worktree_unstage(worktree, &paths, update_progress,
6320 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6321 done:
6322 if (patch_script_file && fclose(patch_script_file) == EOF &&
6323 error == NULL)
6324 error = got_error_from_errno2("fclose", patch_script_path);
6325 if (repo)
6326 got_repo_close(repo);
6327 if (worktree)
6328 got_worktree_close(worktree);
6329 TAILQ_FOREACH(pe, &paths, entry)
6330 free((char *)pe->path);
6331 got_pathlist_free(&paths);
6332 free(cwd);
6333 return error;
6336 __dead static void
6337 usage_cat(void)
6339 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
6340 "[object2 ...]\n", getprogname());
6341 exit(1);
6344 static const struct got_error *
6345 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6347 const struct got_error *err;
6348 struct got_blob_object *blob;
6350 err = got_object_open_as_blob(&blob, repo, id, 8192);
6351 if (err)
6352 return err;
6354 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6355 got_object_blob_close(blob);
6356 return err;
6359 static const struct got_error *
6360 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6362 const struct got_error *err;
6363 struct got_tree_object *tree;
6364 const struct got_tree_entries *entries;
6365 struct got_tree_entry *te;
6367 err = got_object_open_as_tree(&tree, repo, id);
6368 if (err)
6369 return err;
6371 entries = got_object_tree_get_entries(tree);
6372 te = SIMPLEQ_FIRST(&entries->head);
6373 while (te) {
6374 char *id_str;
6375 if (sigint_received || sigpipe_received)
6376 break;
6377 err = got_object_id_str(&id_str, te->id);
6378 if (err)
6379 break;
6380 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6381 free(id_str);
6382 te = SIMPLEQ_NEXT(te, entry);
6385 got_object_tree_close(tree);
6386 return err;
6389 static const struct got_error *
6390 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6392 const struct got_error *err;
6393 struct got_commit_object *commit;
6394 const struct got_object_id_queue *parent_ids;
6395 struct got_object_qid *pid;
6396 char *id_str = NULL;
6397 const char *logmsg = NULL;
6399 err = got_object_open_as_commit(&commit, repo, id);
6400 if (err)
6401 return err;
6403 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6404 if (err)
6405 goto done;
6407 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6408 parent_ids = got_object_commit_get_parent_ids(commit);
6409 fprintf(outfile, "numparents %d\n",
6410 got_object_commit_get_nparents(commit));
6411 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6412 char *pid_str;
6413 err = got_object_id_str(&pid_str, pid->id);
6414 if (err)
6415 goto done;
6416 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6417 free(pid_str);
6419 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6420 got_object_commit_get_author(commit),
6421 got_object_commit_get_author_time(commit));
6423 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6424 got_object_commit_get_author(commit),
6425 got_object_commit_get_committer_time(commit));
6427 logmsg = got_object_commit_get_logmsg_raw(commit);
6428 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6429 fprintf(outfile, "%s", logmsg);
6430 done:
6431 free(id_str);
6432 got_object_commit_close(commit);
6433 return err;
6436 static const struct got_error *
6437 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6439 const struct got_error *err;
6440 struct got_tag_object *tag;
6441 char *id_str = NULL;
6442 const char *tagmsg = NULL;
6444 err = got_object_open_as_tag(&tag, repo, id);
6445 if (err)
6446 return err;
6448 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6449 if (err)
6450 goto done;
6452 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6454 switch (got_object_tag_get_object_type(tag)) {
6455 case GOT_OBJ_TYPE_BLOB:
6456 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6457 GOT_OBJ_LABEL_BLOB);
6458 break;
6459 case GOT_OBJ_TYPE_TREE:
6460 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6461 GOT_OBJ_LABEL_TREE);
6462 break;
6463 case GOT_OBJ_TYPE_COMMIT:
6464 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6465 GOT_OBJ_LABEL_COMMIT);
6466 break;
6467 case GOT_OBJ_TYPE_TAG:
6468 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6469 GOT_OBJ_LABEL_TAG);
6470 break;
6471 default:
6472 break;
6475 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6476 got_object_tag_get_name(tag));
6478 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6479 got_object_tag_get_tagger(tag),
6480 got_object_tag_get_tagger_time(tag));
6482 tagmsg = got_object_tag_get_message(tag);
6483 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6484 fprintf(outfile, "%s", tagmsg);
6485 done:
6486 free(id_str);
6487 got_object_tag_close(tag);
6488 return err;
6491 static const struct got_error *
6492 cmd_cat(int argc, char *argv[])
6494 const struct got_error *error;
6495 struct got_repository *repo = NULL;
6496 struct got_worktree *worktree = NULL;
6497 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6498 struct got_object_id *id = NULL;
6499 int ch, obj_type, i;
6501 #ifndef PROFILE
6502 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6503 NULL) == -1)
6504 err(1, "pledge");
6505 #endif
6507 while ((ch = getopt(argc, argv, "r:")) != -1) {
6508 switch (ch) {
6509 case 'r':
6510 repo_path = realpath(optarg, NULL);
6511 if (repo_path == NULL)
6512 err(1, "-r option");
6513 got_path_strip_trailing_slashes(repo_path);
6514 break;
6515 default:
6516 usage_cat();
6517 /* NOTREACHED */
6521 argc -= optind;
6522 argv += optind;
6524 cwd = getcwd(NULL, 0);
6525 if (cwd == NULL) {
6526 error = got_error_from_errno("getcwd");
6527 goto done;
6529 error = got_worktree_open(&worktree, cwd);
6530 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6531 goto done;
6532 if (worktree) {
6533 if (repo_path == NULL) {
6534 repo_path = strdup(
6535 got_worktree_get_repo_path(worktree));
6536 if (repo_path == NULL) {
6537 error = got_error_from_errno("strdup");
6538 goto done;
6543 if (repo_path == NULL) {
6544 repo_path = getcwd(NULL, 0);
6545 if (repo_path == NULL)
6546 return got_error_from_errno("getcwd");
6549 error = got_repo_open(&repo, repo_path);
6550 free(repo_path);
6551 if (error != NULL)
6552 goto done;
6554 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6555 if (error)
6556 goto done;
6558 for (i = 0; i < argc; i++) {
6559 error = match_object_id(&id, &label, argv[i],
6560 GOT_OBJ_TYPE_ANY, 0, repo);
6561 if (error)
6562 break;
6564 error = got_object_get_type(&obj_type, repo, id);
6565 if (error)
6566 break;
6568 switch (obj_type) {
6569 case GOT_OBJ_TYPE_BLOB:
6570 error = cat_blob(id, repo, stdout);
6571 break;
6572 case GOT_OBJ_TYPE_TREE:
6573 error = cat_tree(id, repo, stdout);
6574 break;
6575 case GOT_OBJ_TYPE_COMMIT:
6576 error = cat_commit(id, repo, stdout);
6577 break;
6578 case GOT_OBJ_TYPE_TAG:
6579 error = cat_tag(id, repo, stdout);
6580 break;
6581 default:
6582 error = got_error(GOT_ERR_OBJ_TYPE);
6583 break;
6585 if (error)
6586 break;
6587 free(label);
6588 label = NULL;
6589 free(id);
6590 id = NULL;
6593 done:
6594 free(label);
6595 free(id);
6596 if (worktree)
6597 got_worktree_close(worktree);
6598 if (repo) {
6599 const struct got_error *repo_error;
6600 repo_error = got_repo_close(repo);
6601 if (error == NULL)
6602 error = repo_error;
6604 return error;