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, NULL, got_ref_cmp_by_name, NULL);
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_NONEXISTENT)
1827 return got_error_set_errno(ENOENT, path);
1828 if (status != GOT_STATUS_MODIFY &&
1829 status != GOT_STATUS_ADD &&
1830 status != GOT_STATUS_DELETE &&
1831 status != GOT_STATUS_CONFLICT)
1832 return NULL;
1835 if (!a->header_shown) {
1836 printf("diff %s %s%s\n", a->id_str,
1837 got_worktree_get_root_path(a->worktree),
1838 a->diff_staged ? " (staged changes)" : "");
1839 a->header_shown = 1;
1842 if (a->diff_staged) {
1843 const char *label1 = NULL, *label2 = NULL;
1844 switch (staged_status) {
1845 case GOT_STATUS_MODIFY:
1846 label1 = path;
1847 label2 = path;
1848 break;
1849 case GOT_STATUS_ADD:
1850 label2 = path;
1851 break;
1852 case GOT_STATUS_DELETE:
1853 label1 = path;
1854 break;
1855 default:
1856 return got_error(GOT_ERR_FILE_STATUS);
1858 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1859 label1, label2, a->diff_context, a->repo, stdout);
1862 if (staged_status == GOT_STATUS_ADD ||
1863 staged_status == GOT_STATUS_MODIFY) {
1864 char *id_str;
1865 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1866 8192);
1867 if (err)
1868 goto done;
1869 err = got_object_id_str(&id_str, staged_blob_id);
1870 if (err)
1871 goto done;
1872 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1873 err = got_error_from_errno("asprintf");
1874 free(id_str);
1875 goto done;
1877 free(id_str);
1878 } else if (status != GOT_STATUS_ADD) {
1879 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1880 if (err)
1881 goto done;
1884 if (status != GOT_STATUS_DELETE) {
1885 if (asprintf(&abspath, "%s/%s",
1886 got_worktree_get_root_path(a->worktree), path) == -1) {
1887 err = got_error_from_errno("asprintf");
1888 goto done;
1891 f2 = fopen(abspath, "r");
1892 if (f2 == NULL) {
1893 err = got_error_from_errno2("fopen", abspath);
1894 goto done;
1896 if (lstat(abspath, &sb) == -1) {
1897 err = got_error_from_errno2("lstat", abspath);
1898 goto done;
1900 } else
1901 sb.st_size = 0;
1903 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1904 a->diff_context, stdout);
1905 done:
1906 if (blob1)
1907 got_object_blob_close(blob1);
1908 if (f2 && fclose(f2) != 0 && err == NULL)
1909 err = got_error_from_errno("fclose");
1910 free(abspath);
1911 return err;
1914 static const struct got_error *
1915 match_object_id(struct got_object_id **id, char **label,
1916 const char *id_str, int obj_type, int resolve_tags,
1917 struct got_repository *repo)
1919 const struct got_error *err;
1920 struct got_tag_object *tag;
1921 struct got_reference *ref = NULL;
1923 *id = NULL;
1924 *label = NULL;
1926 if (resolve_tags) {
1927 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1928 repo);
1929 if (err == NULL) {
1930 *id = got_object_id_dup(
1931 got_object_tag_get_object_id(tag));
1932 if (*id == NULL)
1933 err = got_error_from_errno("got_object_id_dup");
1934 else if (asprintf(label, "refs/tags/%s",
1935 got_object_tag_get_name(tag)) == -1) {
1936 err = got_error_from_errno("asprintf");
1937 free(id);
1938 *id = NULL;
1940 got_object_tag_close(tag);
1941 return err;
1942 } else if (err->code != GOT_ERR_NO_OBJ)
1943 return err;
1946 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1947 if (err) {
1948 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1949 return err;
1950 err = got_ref_open(&ref, repo, id_str, 0);
1951 if (err != NULL)
1952 goto done;
1953 *label = strdup(got_ref_get_name(ref));
1954 if (*label == NULL) {
1955 err = got_error_from_errno("strdup");
1956 goto done;
1958 err = got_ref_resolve(id, repo, ref);
1959 } else {
1960 err = got_object_id_str(label, *id);
1961 if (*label == NULL) {
1962 err = got_error_from_errno("strdup");
1963 goto done;
1966 done:
1967 if (ref)
1968 got_ref_close(ref);
1969 return err;
1973 static const struct got_error *
1974 cmd_diff(int argc, char *argv[])
1976 const struct got_error *error;
1977 struct got_repository *repo = NULL;
1978 struct got_worktree *worktree = NULL;
1979 char *cwd = NULL, *repo_path = NULL;
1980 struct got_object_id *id1 = NULL, *id2 = NULL;
1981 const char *id_str1 = NULL, *id_str2 = NULL;
1982 char *label1 = NULL, *label2 = NULL;
1983 int type1, type2;
1984 int diff_context = 3, diff_staged = 0, ch;
1985 const char *errstr;
1986 char *path = NULL;
1988 #ifndef PROFILE
1989 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1990 NULL) == -1)
1991 err(1, "pledge");
1992 #endif
1994 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1995 switch (ch) {
1996 case 'C':
1997 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1998 if (errstr != NULL)
1999 err(1, "-C option %s", errstr);
2000 break;
2001 case 'r':
2002 repo_path = realpath(optarg, NULL);
2003 if (repo_path == NULL)
2004 err(1, "-r option");
2005 got_path_strip_trailing_slashes(repo_path);
2006 break;
2007 case 's':
2008 diff_staged = 1;
2009 break;
2010 default:
2011 usage_diff();
2012 /* NOTREACHED */
2016 argc -= optind;
2017 argv += optind;
2019 cwd = getcwd(NULL, 0);
2020 if (cwd == NULL) {
2021 error = got_error_from_errno("getcwd");
2022 goto done;
2024 error = got_worktree_open(&worktree, cwd);
2025 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2026 goto done;
2027 if (argc <= 1) {
2028 if (worktree == NULL) {
2029 error = got_error(GOT_ERR_NOT_WORKTREE);
2030 goto done;
2032 if (repo_path)
2033 errx(1,
2034 "-r option can't be used when diffing a work tree");
2035 repo_path = strdup(got_worktree_get_repo_path(worktree));
2036 if (repo_path == NULL) {
2037 error = got_error_from_errno("strdup");
2038 goto done;
2040 if (argc == 1) {
2041 error = got_worktree_resolve_path(&path, worktree,
2042 argv[0]);
2043 if (error)
2044 goto done;
2045 } else {
2046 path = strdup("");
2047 if (path == NULL) {
2048 error = got_error_from_errno("strdup");
2049 goto done;
2052 } else if (argc == 2) {
2053 if (diff_staged)
2054 errx(1, "-s option can't be used when diffing "
2055 "objects in repository");
2056 id_str1 = argv[0];
2057 id_str2 = argv[1];
2058 if (worktree && repo_path == NULL) {
2059 repo_path =
2060 strdup(got_worktree_get_repo_path(worktree));
2061 if (repo_path == NULL) {
2062 error = got_error_from_errno("strdup");
2063 goto done;
2066 } else
2067 usage_diff();
2069 if (repo_path == NULL) {
2070 repo_path = getcwd(NULL, 0);
2071 if (repo_path == NULL)
2072 return got_error_from_errno("getcwd");
2075 error = got_repo_open(&repo, repo_path);
2076 free(repo_path);
2077 if (error != NULL)
2078 goto done;
2080 error = apply_unveil(got_repo_get_path(repo), 1,
2081 worktree ? got_worktree_get_root_path(worktree) : NULL);
2082 if (error)
2083 goto done;
2085 if (argc <= 1) {
2086 struct print_diff_arg arg;
2087 struct got_pathlist_head paths;
2088 char *id_str;
2090 TAILQ_INIT(&paths);
2092 error = got_object_id_str(&id_str,
2093 got_worktree_get_base_commit_id(worktree));
2094 if (error)
2095 goto done;
2096 arg.repo = repo;
2097 arg.worktree = worktree;
2098 arg.diff_context = diff_context;
2099 arg.id_str = id_str;
2100 arg.header_shown = 0;
2101 arg.diff_staged = diff_staged;
2103 error = got_pathlist_append(&paths, path, NULL);
2104 if (error)
2105 goto done;
2107 error = got_worktree_status(worktree, &paths, repo, print_diff,
2108 &arg, check_cancelled, NULL);
2109 free(id_str);
2110 got_pathlist_free(&paths);
2111 goto done;
2114 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2115 repo);
2116 if (error)
2117 goto done;
2119 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2120 repo);
2121 if (error)
2122 goto done;
2124 error = got_object_get_type(&type1, repo, id1);
2125 if (error)
2126 goto done;
2128 error = got_object_get_type(&type2, repo, id2);
2129 if (error)
2130 goto done;
2132 if (type1 != type2) {
2133 error = got_error(GOT_ERR_OBJ_TYPE);
2134 goto done;
2137 switch (type1) {
2138 case GOT_OBJ_TYPE_BLOB:
2139 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2140 diff_context, repo, stdout);
2141 break;
2142 case GOT_OBJ_TYPE_TREE:
2143 error = got_diff_objects_as_trees(id1, id2, "", "",
2144 diff_context, repo, stdout);
2145 break;
2146 case GOT_OBJ_TYPE_COMMIT:
2147 printf("diff %s %s\n", label1, label2);
2148 error = got_diff_objects_as_commits(id1, id2, diff_context,
2149 repo, stdout);
2150 break;
2151 default:
2152 error = got_error(GOT_ERR_OBJ_TYPE);
2155 done:
2156 free(label1);
2157 free(label2);
2158 free(id1);
2159 free(id2);
2160 free(path);
2161 if (worktree)
2162 got_worktree_close(worktree);
2163 if (repo) {
2164 const struct got_error *repo_error;
2165 repo_error = got_repo_close(repo);
2166 if (error == NULL)
2167 error = repo_error;
2169 return error;
2172 __dead static void
2173 usage_blame(void)
2175 fprintf(stderr,
2176 "usage: %s blame [-c commit] [-r repository-path] path\n",
2177 getprogname());
2178 exit(1);
2181 struct blame_line {
2182 int annotated;
2183 char *id_str;
2184 char *committer;
2185 char datebuf[9]; /* YY-MM-DD + NUL */
2188 struct blame_cb_args {
2189 struct blame_line *lines;
2190 int nlines;
2191 int nlines_prec;
2192 int lineno_cur;
2193 off_t *line_offsets;
2194 FILE *f;
2195 struct got_repository *repo;
2198 static const struct got_error *
2199 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2201 const struct got_error *err = NULL;
2202 struct blame_cb_args *a = arg;
2203 struct blame_line *bline;
2204 char *line = NULL;
2205 size_t linesize = 0;
2206 struct got_commit_object *commit = NULL;
2207 off_t offset;
2208 struct tm tm;
2209 time_t committer_time;
2211 if (nlines != a->nlines ||
2212 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2213 return got_error(GOT_ERR_RANGE);
2215 if (sigint_received)
2216 return got_error(GOT_ERR_ITER_COMPLETED);
2218 if (lineno == -1)
2219 return NULL; /* no change in this commit */
2221 /* Annotate this line. */
2222 bline = &a->lines[lineno - 1];
2223 if (bline->annotated)
2224 return NULL;
2225 err = got_object_id_str(&bline->id_str, id);
2226 if (err)
2227 return err;
2229 err = got_object_open_as_commit(&commit, a->repo, id);
2230 if (err)
2231 goto done;
2233 bline->committer = strdup(got_object_commit_get_committer(commit));
2234 if (bline->committer == NULL) {
2235 err = got_error_from_errno("strdup");
2236 goto done;
2239 committer_time = got_object_commit_get_committer_time(commit);
2240 if (localtime_r(&committer_time, &tm) == NULL)
2241 return got_error_from_errno("localtime_r");
2242 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2243 &tm) >= sizeof(bline->datebuf)) {
2244 err = got_error(GOT_ERR_NO_SPACE);
2245 goto done;
2247 bline->annotated = 1;
2249 /* Print lines annotated so far. */
2250 bline = &a->lines[a->lineno_cur - 1];
2251 if (!bline->annotated)
2252 goto done;
2254 offset = a->line_offsets[a->lineno_cur - 1];
2255 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2256 err = got_error_from_errno("fseeko");
2257 goto done;
2260 while (bline->annotated) {
2261 char *smallerthan, *at, *nl, *committer;
2262 size_t len;
2264 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2265 if (ferror(a->f))
2266 err = got_error_from_errno("getline");
2267 break;
2270 committer = bline->committer;
2271 smallerthan = strchr(committer, '<');
2272 if (smallerthan && smallerthan[1] != '\0')
2273 committer = smallerthan + 1;
2274 at = strchr(committer, '@');
2275 if (at)
2276 *at = '\0';
2277 len = strlen(committer);
2278 if (len >= 9)
2279 committer[8] = '\0';
2281 nl = strchr(line, '\n');
2282 if (nl)
2283 *nl = '\0';
2284 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2285 bline->id_str, bline->datebuf, committer, line);
2287 a->lineno_cur++;
2288 bline = &a->lines[a->lineno_cur - 1];
2290 done:
2291 if (commit)
2292 got_object_commit_close(commit);
2293 free(line);
2294 return err;
2297 static const struct got_error *
2298 cmd_blame(int argc, char *argv[])
2300 const struct got_error *error;
2301 struct got_repository *repo = NULL;
2302 struct got_worktree *worktree = NULL;
2303 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2304 struct got_object_id *obj_id = NULL;
2305 struct got_object_id *commit_id = NULL;
2306 struct got_blob_object *blob = NULL;
2307 char *commit_id_str = NULL;
2308 struct blame_cb_args bca;
2309 int ch, obj_type, i;
2310 size_t filesize;
2312 memset(&bca, 0, sizeof(bca));
2314 #ifndef PROFILE
2315 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2316 NULL) == -1)
2317 err(1, "pledge");
2318 #endif
2320 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2321 switch (ch) {
2322 case 'c':
2323 commit_id_str = optarg;
2324 break;
2325 case 'r':
2326 repo_path = realpath(optarg, NULL);
2327 if (repo_path == NULL)
2328 err(1, "-r option");
2329 got_path_strip_trailing_slashes(repo_path);
2330 break;
2331 default:
2332 usage_blame();
2333 /* NOTREACHED */
2337 argc -= optind;
2338 argv += optind;
2340 if (argc == 1)
2341 path = argv[0];
2342 else
2343 usage_blame();
2345 cwd = getcwd(NULL, 0);
2346 if (cwd == NULL) {
2347 error = got_error_from_errno("getcwd");
2348 goto done;
2350 if (repo_path == NULL) {
2351 error = got_worktree_open(&worktree, cwd);
2352 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2353 goto done;
2354 else
2355 error = NULL;
2356 if (worktree) {
2357 repo_path =
2358 strdup(got_worktree_get_repo_path(worktree));
2359 if (repo_path == NULL)
2360 error = got_error_from_errno("strdup");
2361 if (error)
2362 goto done;
2363 } else {
2364 repo_path = strdup(cwd);
2365 if (repo_path == NULL) {
2366 error = got_error_from_errno("strdup");
2367 goto done;
2372 error = got_repo_open(&repo, repo_path);
2373 if (error != NULL)
2374 goto done;
2376 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2377 if (error)
2378 goto done;
2380 if (worktree) {
2381 const char *prefix = got_worktree_get_path_prefix(worktree);
2382 char *p, *worktree_subdir = cwd +
2383 strlen(got_worktree_get_root_path(worktree));
2384 if (asprintf(&p, "%s%s%s%s%s",
2385 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2386 worktree_subdir, worktree_subdir[0] ? "/" : "",
2387 path) == -1) {
2388 error = got_error_from_errno("asprintf");
2389 goto done;
2391 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2392 free(p);
2393 } else {
2394 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2396 if (error)
2397 goto done;
2399 if (commit_id_str == NULL) {
2400 struct got_reference *head_ref;
2401 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2402 if (error != NULL)
2403 goto done;
2404 error = got_ref_resolve(&commit_id, repo, head_ref);
2405 got_ref_close(head_ref);
2406 if (error != NULL)
2407 goto done;
2408 } else {
2409 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2410 if (error)
2411 goto done;
2414 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2415 if (error)
2416 goto done;
2417 if (obj_id == NULL) {
2418 error = got_error(GOT_ERR_NO_OBJ);
2419 goto done;
2422 error = got_object_get_type(&obj_type, repo, obj_id);
2423 if (error)
2424 goto done;
2426 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2427 error = got_error(GOT_ERR_OBJ_TYPE);
2428 goto done;
2431 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2432 if (error)
2433 goto done;
2434 bca.f = got_opentemp();
2435 if (bca.f == NULL) {
2436 error = got_error_from_errno("got_opentemp");
2437 goto done;
2439 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2440 &bca.line_offsets, bca.f, blob);
2441 if (error || bca.nlines == 0)
2442 goto done;
2444 /* Don't include \n at EOF in the blame line count. */
2445 if (bca.line_offsets[bca.nlines - 1] == filesize)
2446 bca.nlines--;
2448 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2449 if (bca.lines == NULL) {
2450 error = got_error_from_errno("calloc");
2451 goto done;
2453 bca.lineno_cur = 1;
2454 bca.nlines_prec = 0;
2455 i = bca.nlines;
2456 while (i > 0) {
2457 i /= 10;
2458 bca.nlines_prec++;
2460 bca.repo = repo;
2462 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2463 check_cancelled, NULL);
2464 if (error)
2465 goto done;
2466 done:
2467 free(in_repo_path);
2468 free(repo_path);
2469 free(cwd);
2470 free(commit_id);
2471 free(obj_id);
2472 if (blob)
2473 got_object_blob_close(blob);
2474 if (worktree)
2475 got_worktree_close(worktree);
2476 if (repo) {
2477 const struct got_error *repo_error;
2478 repo_error = got_repo_close(repo);
2479 if (error == NULL)
2480 error = repo_error;
2482 for (i = 0; i < bca.nlines; i++) {
2483 struct blame_line *bline = &bca.lines[i];
2484 free(bline->id_str);
2485 free(bline->committer);
2487 free(bca.lines);
2488 free(bca.line_offsets);
2489 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2490 error = got_error_from_errno("fclose");
2491 return error;
2494 __dead static void
2495 usage_tree(void)
2497 fprintf(stderr,
2498 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2499 getprogname());
2500 exit(1);
2503 static void
2504 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2505 const char *root_path)
2507 int is_root_path = (strcmp(path, root_path) == 0);
2508 const char *modestr = "";
2510 path += strlen(root_path);
2511 while (path[0] == '/')
2512 path++;
2514 if (S_ISLNK(te->mode))
2515 modestr = "@";
2516 else if (S_ISDIR(te->mode))
2517 modestr = "/";
2518 else if (te->mode & S_IXUSR)
2519 modestr = "*";
2521 printf("%s%s%s%s%s\n", id ? id : "", path,
2522 is_root_path ? "" : "/", te->name, modestr);
2525 static const struct got_error *
2526 print_tree(const char *path, struct got_object_id *commit_id,
2527 int show_ids, int recurse, const char *root_path,
2528 struct got_repository *repo)
2530 const struct got_error *err = NULL;
2531 struct got_object_id *tree_id = NULL;
2532 struct got_tree_object *tree = NULL;
2533 const struct got_tree_entries *entries;
2534 struct got_tree_entry *te;
2536 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2537 if (err)
2538 goto done;
2540 err = got_object_open_as_tree(&tree, repo, tree_id);
2541 if (err)
2542 goto done;
2543 entries = got_object_tree_get_entries(tree);
2544 te = SIMPLEQ_FIRST(&entries->head);
2545 while (te) {
2546 char *id = NULL;
2548 if (sigint_received || sigpipe_received)
2549 break;
2551 if (show_ids) {
2552 char *id_str;
2553 err = got_object_id_str(&id_str, te->id);
2554 if (err)
2555 goto done;
2556 if (asprintf(&id, "%s ", id_str) == -1) {
2557 err = got_error_from_errno("asprintf");
2558 free(id_str);
2559 goto done;
2561 free(id_str);
2563 print_entry(te, id, path, root_path);
2564 free(id);
2566 if (recurse && S_ISDIR(te->mode)) {
2567 char *child_path;
2568 if (asprintf(&child_path, "%s%s%s", path,
2569 path[0] == '/' && path[1] == '\0' ? "" : "/",
2570 te->name) == -1) {
2571 err = got_error_from_errno("asprintf");
2572 goto done;
2574 err = print_tree(child_path, commit_id, show_ids, 1,
2575 root_path, repo);
2576 free(child_path);
2577 if (err)
2578 goto done;
2581 te = SIMPLEQ_NEXT(te, entry);
2583 done:
2584 if (tree)
2585 got_object_tree_close(tree);
2586 free(tree_id);
2587 return err;
2590 static const struct got_error *
2591 cmd_tree(int argc, char *argv[])
2593 const struct got_error *error;
2594 struct got_repository *repo = NULL;
2595 struct got_worktree *worktree = NULL;
2596 const char *path;
2597 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2598 struct got_object_id *commit_id = NULL;
2599 char *commit_id_str = NULL;
2600 int show_ids = 0, recurse = 0;
2601 int ch;
2603 #ifndef PROFILE
2604 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2605 NULL) == -1)
2606 err(1, "pledge");
2607 #endif
2609 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2610 switch (ch) {
2611 case 'c':
2612 commit_id_str = optarg;
2613 break;
2614 case 'r':
2615 repo_path = realpath(optarg, NULL);
2616 if (repo_path == NULL)
2617 err(1, "-r option");
2618 got_path_strip_trailing_slashes(repo_path);
2619 break;
2620 case 'i':
2621 show_ids = 1;
2622 break;
2623 case 'R':
2624 recurse = 1;
2625 break;
2626 default:
2627 usage_tree();
2628 /* NOTREACHED */
2632 argc -= optind;
2633 argv += optind;
2635 if (argc == 1)
2636 path = argv[0];
2637 else if (argc > 1)
2638 usage_tree();
2639 else
2640 path = NULL;
2642 cwd = getcwd(NULL, 0);
2643 if (cwd == NULL) {
2644 error = got_error_from_errno("getcwd");
2645 goto done;
2647 if (repo_path == NULL) {
2648 error = got_worktree_open(&worktree, cwd);
2649 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2650 goto done;
2651 else
2652 error = NULL;
2653 if (worktree) {
2654 repo_path =
2655 strdup(got_worktree_get_repo_path(worktree));
2656 if (repo_path == NULL)
2657 error = got_error_from_errno("strdup");
2658 if (error)
2659 goto done;
2660 } else {
2661 repo_path = strdup(cwd);
2662 if (repo_path == NULL) {
2663 error = got_error_from_errno("strdup");
2664 goto done;
2669 error = got_repo_open(&repo, repo_path);
2670 if (error != NULL)
2671 goto done;
2673 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2674 if (error)
2675 goto done;
2677 if (path == NULL) {
2678 if (worktree) {
2679 char *p, *worktree_subdir = cwd +
2680 strlen(got_worktree_get_root_path(worktree));
2681 if (asprintf(&p, "%s/%s",
2682 got_worktree_get_path_prefix(worktree),
2683 worktree_subdir) == -1) {
2684 error = got_error_from_errno("asprintf");
2685 goto done;
2687 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2688 free(p);
2689 if (error)
2690 goto done;
2691 } else
2692 path = "/";
2694 if (in_repo_path == NULL) {
2695 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2696 if (error != NULL)
2697 goto done;
2700 if (commit_id_str == NULL) {
2701 struct got_reference *head_ref;
2702 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2703 if (error != NULL)
2704 goto done;
2705 error = got_ref_resolve(&commit_id, repo, head_ref);
2706 got_ref_close(head_ref);
2707 if (error != NULL)
2708 goto done;
2709 } else {
2710 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2711 if (error)
2712 goto done;
2715 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2716 in_repo_path, repo);
2717 done:
2718 free(in_repo_path);
2719 free(repo_path);
2720 free(cwd);
2721 free(commit_id);
2722 if (worktree)
2723 got_worktree_close(worktree);
2724 if (repo) {
2725 const struct got_error *repo_error;
2726 repo_error = got_repo_close(repo);
2727 if (error == NULL)
2728 error = repo_error;
2730 return error;
2733 __dead static void
2734 usage_status(void)
2736 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2737 exit(1);
2740 static const struct got_error *
2741 print_status(void *arg, unsigned char status, unsigned char staged_status,
2742 const char *path, struct got_object_id *blob_id,
2743 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2745 if (status == staged_status && (status == GOT_STATUS_DELETE))
2746 status = GOT_STATUS_NO_CHANGE;
2747 printf("%c%c %s\n", status, staged_status, path);
2748 return NULL;
2751 static const struct got_error *
2752 cmd_status(int argc, char *argv[])
2754 const struct got_error *error = NULL;
2755 struct got_repository *repo = NULL;
2756 struct got_worktree *worktree = NULL;
2757 char *cwd = NULL;
2758 struct got_pathlist_head paths;
2759 struct got_pathlist_entry *pe;
2760 int ch;
2762 TAILQ_INIT(&paths);
2764 while ((ch = getopt(argc, argv, "")) != -1) {
2765 switch (ch) {
2766 default:
2767 usage_status();
2768 /* NOTREACHED */
2772 argc -= optind;
2773 argv += optind;
2775 #ifndef PROFILE
2776 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2777 NULL) == -1)
2778 err(1, "pledge");
2779 #endif
2780 cwd = getcwd(NULL, 0);
2781 if (cwd == NULL) {
2782 error = got_error_from_errno("getcwd");
2783 goto done;
2786 error = got_worktree_open(&worktree, cwd);
2787 if (error != NULL)
2788 goto done;
2790 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2791 if (error != NULL)
2792 goto done;
2794 error = apply_unveil(got_repo_get_path(repo), 1,
2795 got_worktree_get_root_path(worktree));
2796 if (error)
2797 goto done;
2799 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2800 if (error)
2801 goto done;
2803 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2804 check_cancelled, NULL);
2805 done:
2806 TAILQ_FOREACH(pe, &paths, entry)
2807 free((char *)pe->path);
2808 got_pathlist_free(&paths);
2809 free(cwd);
2810 return error;
2813 __dead static void
2814 usage_ref(void)
2816 fprintf(stderr,
2817 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2818 getprogname());
2819 exit(1);
2822 static const struct got_error *
2823 list_refs(struct got_repository *repo)
2825 static const struct got_error *err = NULL;
2826 struct got_reflist_head refs;
2827 struct got_reflist_entry *re;
2829 SIMPLEQ_INIT(&refs);
2830 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2831 if (err)
2832 return err;
2834 SIMPLEQ_FOREACH(re, &refs, entry) {
2835 char *refstr;
2836 refstr = got_ref_to_str(re->ref);
2837 if (refstr == NULL)
2838 return got_error_from_errno("got_ref_to_str");
2839 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2840 free(refstr);
2843 got_ref_list_free(&refs);
2844 return NULL;
2847 static const struct got_error *
2848 delete_ref(struct got_repository *repo, const char *refname)
2850 const struct got_error *err = NULL;
2851 struct got_reference *ref;
2853 err = got_ref_open(&ref, repo, refname, 0);
2854 if (err)
2855 return err;
2857 err = got_ref_delete(ref, repo);
2858 got_ref_close(ref);
2859 return err;
2862 static const struct got_error *
2863 add_ref(struct got_repository *repo, const char *refname, const char *target)
2865 const struct got_error *err = NULL;
2866 struct got_object_id *id;
2867 struct got_reference *ref = NULL;
2870 * Don't let the user create a reference named '-'.
2871 * While technically a valid reference name, this case is usually
2872 * an unintended typo.
2874 if (refname[0] == '-' && refname[1] == '\0')
2875 return got_error(GOT_ERR_BAD_REF_NAME);
2877 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2878 repo);
2879 if (err) {
2880 struct got_reference *target_ref;
2882 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2883 return err;
2884 err = got_ref_open(&target_ref, repo, target, 0);
2885 if (err)
2886 return err;
2887 err = got_ref_resolve(&id, repo, target_ref);
2888 got_ref_close(target_ref);
2889 if (err)
2890 return err;
2893 err = got_ref_alloc(&ref, refname, id);
2894 if (err)
2895 goto done;
2897 err = got_ref_write(ref, repo);
2898 done:
2899 if (ref)
2900 got_ref_close(ref);
2901 free(id);
2902 return err;
2905 static const struct got_error *
2906 add_symref(struct got_repository *repo, const char *refname, const char *target)
2908 const struct got_error *err = NULL;
2909 struct got_reference *ref = NULL;
2910 struct got_reference *target_ref = NULL;
2913 * Don't let the user create a reference named '-'.
2914 * While technically a valid reference name, this case is usually
2915 * an unintended typo.
2917 if (refname[0] == '-' && refname[1] == '\0')
2918 return got_error(GOT_ERR_BAD_REF_NAME);
2920 err = got_ref_open(&target_ref, repo, target, 0);
2921 if (err)
2922 return err;
2924 err = got_ref_alloc_symref(&ref, refname, target_ref);
2925 if (err)
2926 goto done;
2928 err = got_ref_write(ref, repo);
2929 done:
2930 if (target_ref)
2931 got_ref_close(target_ref);
2932 if (ref)
2933 got_ref_close(ref);
2934 return err;
2937 static const struct got_error *
2938 cmd_ref(int argc, char *argv[])
2940 const struct got_error *error = NULL;
2941 struct got_repository *repo = NULL;
2942 struct got_worktree *worktree = NULL;
2943 char *cwd = NULL, *repo_path = NULL;
2944 int ch, do_list = 0, create_symref = 0;
2945 const char *delref = NULL;
2947 /* TODO: Add -s option for adding symbolic references. */
2948 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2949 switch (ch) {
2950 case 'd':
2951 delref = optarg;
2952 break;
2953 case 'r':
2954 repo_path = realpath(optarg, NULL);
2955 if (repo_path == NULL)
2956 err(1, "-r option");
2957 got_path_strip_trailing_slashes(repo_path);
2958 break;
2959 case 'l':
2960 do_list = 1;
2961 break;
2962 case 's':
2963 create_symref = 1;
2964 break;
2965 default:
2966 usage_ref();
2967 /* NOTREACHED */
2971 if (do_list && delref)
2972 errx(1, "-l and -d options are mutually exclusive\n");
2974 argc -= optind;
2975 argv += optind;
2977 if (do_list || delref) {
2978 if (create_symref)
2979 errx(1, "-s option cannot be used together with the "
2980 "-l or -d options");
2981 if (argc > 0)
2982 usage_ref();
2983 } else if (argc != 2)
2984 usage_ref();
2986 #ifndef PROFILE
2987 if (do_list) {
2988 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2989 NULL) == -1)
2990 err(1, "pledge");
2991 } else {
2992 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2993 "sendfd unveil", NULL) == -1)
2994 err(1, "pledge");
2996 #endif
2997 cwd = getcwd(NULL, 0);
2998 if (cwd == NULL) {
2999 error = got_error_from_errno("getcwd");
3000 goto done;
3003 if (repo_path == NULL) {
3004 error = got_worktree_open(&worktree, cwd);
3005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3006 goto done;
3007 else
3008 error = NULL;
3009 if (worktree) {
3010 repo_path =
3011 strdup(got_worktree_get_repo_path(worktree));
3012 if (repo_path == NULL)
3013 error = got_error_from_errno("strdup");
3014 if (error)
3015 goto done;
3016 } else {
3017 repo_path = strdup(cwd);
3018 if (repo_path == NULL) {
3019 error = got_error_from_errno("strdup");
3020 goto done;
3025 error = got_repo_open(&repo, repo_path);
3026 if (error != NULL)
3027 goto done;
3029 error = apply_unveil(got_repo_get_path(repo), do_list,
3030 worktree ? got_worktree_get_root_path(worktree) : NULL);
3031 if (error)
3032 goto done;
3034 if (do_list)
3035 error = list_refs(repo);
3036 else if (delref)
3037 error = delete_ref(repo, delref);
3038 else if (create_symref)
3039 error = add_symref(repo, argv[0], argv[1]);
3040 else
3041 error = add_ref(repo, argv[0], argv[1]);
3042 done:
3043 if (repo)
3044 got_repo_close(repo);
3045 if (worktree)
3046 got_worktree_close(worktree);
3047 free(cwd);
3048 free(repo_path);
3049 return error;
3052 __dead static void
3053 usage_branch(void)
3055 fprintf(stderr,
3056 "usage: %s branch [-r repository] -l | -d name | "
3057 "name [base-branch]\n", getprogname());
3058 exit(1);
3061 static const struct got_error *
3062 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3064 static const struct got_error *err = NULL;
3065 struct got_reflist_head refs;
3066 struct got_reflist_entry *re;
3068 SIMPLEQ_INIT(&refs);
3070 err = got_ref_list(&refs, repo, "refs/heads",
3071 got_ref_cmp_by_name, NULL);
3072 if (err)
3073 return err;
3075 SIMPLEQ_FOREACH(re, &refs, entry) {
3076 const char *refname, *marker = " ";
3077 char *refstr;
3078 refname = got_ref_get_name(re->ref);
3079 if (worktree && strcmp(refname,
3080 got_worktree_get_head_ref_name(worktree)) == 0) {
3081 struct got_object_id *id = NULL;
3082 err = got_ref_resolve(&id, repo, re->ref);
3083 if (err)
3084 return err;
3085 if (got_object_id_cmp(id,
3086 got_worktree_get_base_commit_id(worktree)) == 0)
3087 marker = "* ";
3088 else
3089 marker = "~ ";
3090 free(id);
3092 refname += strlen("refs/heads/");
3093 refstr = got_ref_to_str(re->ref);
3094 if (refstr == NULL)
3095 return got_error_from_errno("got_ref_to_str");
3096 printf("%s%s: %s\n", marker, refname, refstr);
3097 free(refstr);
3100 got_ref_list_free(&refs);
3101 return NULL;
3104 static const struct got_error *
3105 delete_branch(struct got_repository *repo, const char *branch_name)
3107 const struct got_error *err = NULL;
3108 struct got_reference *ref;
3109 char *refname;
3111 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3112 return got_error_from_errno("asprintf");
3114 err = got_ref_open(&ref, repo, refname, 0);
3115 if (err)
3116 goto done;
3118 err = got_ref_delete(ref, repo);
3119 got_ref_close(ref);
3120 done:
3121 free(refname);
3122 return err;
3125 static const struct got_error *
3126 add_branch(struct got_repository *repo, const char *branch_name,
3127 const char *base_branch)
3129 const struct got_error *err = NULL;
3130 struct got_object_id *id = NULL;
3131 struct got_reference *ref = NULL;
3132 char *base_refname = NULL, *refname = NULL;
3133 struct got_reference *base_ref;
3136 * Don't let the user create a branch named '-'.
3137 * While technically a valid reference name, this case is usually
3138 * an unintended typo.
3140 if (branch_name[0] == '-' && branch_name[1] == '\0')
3141 return got_error(GOT_ERR_BAD_REF_NAME);
3143 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3144 base_refname = strdup(GOT_REF_HEAD);
3145 if (base_refname == NULL)
3146 return got_error_from_errno("strdup");
3147 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3148 return got_error_from_errno("asprintf");
3150 err = got_ref_open(&base_ref, repo, base_refname, 0);
3151 if (err)
3152 goto done;
3153 err = got_ref_resolve(&id, repo, base_ref);
3154 got_ref_close(base_ref);
3155 if (err)
3156 goto done;
3158 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3159 err = got_error_from_errno("asprintf");
3160 goto done;
3163 err = got_ref_open(&ref, repo, refname, 0);
3164 if (err == NULL) {
3165 err = got_error(GOT_ERR_BRANCH_EXISTS);
3166 goto done;
3167 } else if (err->code != GOT_ERR_NOT_REF)
3168 goto done;
3170 err = got_ref_alloc(&ref, refname, id);
3171 if (err)
3172 goto done;
3174 err = got_ref_write(ref, repo);
3175 done:
3176 if (ref)
3177 got_ref_close(ref);
3178 free(id);
3179 free(base_refname);
3180 free(refname);
3181 return err;
3184 static const struct got_error *
3185 cmd_branch(int argc, char *argv[])
3187 const struct got_error *error = NULL;
3188 struct got_repository *repo = NULL;
3189 struct got_worktree *worktree = NULL;
3190 char *cwd = NULL, *repo_path = NULL;
3191 int ch, do_list = 0;
3192 const char *delref = NULL;
3194 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3195 switch (ch) {
3196 case 'd':
3197 delref = optarg;
3198 break;
3199 case 'r':
3200 repo_path = realpath(optarg, NULL);
3201 if (repo_path == NULL)
3202 err(1, "-r option");
3203 got_path_strip_trailing_slashes(repo_path);
3204 break;
3205 case 'l':
3206 do_list = 1;
3207 break;
3208 default:
3209 usage_branch();
3210 /* NOTREACHED */
3214 if (do_list && delref)
3215 errx(1, "-l and -d options are mutually exclusive\n");
3217 argc -= optind;
3218 argv += optind;
3220 if (do_list || delref) {
3221 if (argc > 0)
3222 usage_branch();
3223 } else if (argc < 1 || argc > 2)
3224 usage_branch();
3226 #ifndef PROFILE
3227 if (do_list) {
3228 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3229 NULL) == -1)
3230 err(1, "pledge");
3231 } else {
3232 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3233 "sendfd unveil", NULL) == -1)
3234 err(1, "pledge");
3236 #endif
3237 cwd = getcwd(NULL, 0);
3238 if (cwd == NULL) {
3239 error = got_error_from_errno("getcwd");
3240 goto done;
3243 if (repo_path == NULL) {
3244 error = got_worktree_open(&worktree, cwd);
3245 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3246 goto done;
3247 else
3248 error = NULL;
3249 if (worktree) {
3250 repo_path =
3251 strdup(got_worktree_get_repo_path(worktree));
3252 if (repo_path == NULL)
3253 error = got_error_from_errno("strdup");
3254 if (error)
3255 goto done;
3256 } else {
3257 repo_path = strdup(cwd);
3258 if (repo_path == NULL) {
3259 error = got_error_from_errno("strdup");
3260 goto done;
3265 error = got_repo_open(&repo, repo_path);
3266 if (error != NULL)
3267 goto done;
3269 error = apply_unveil(got_repo_get_path(repo), do_list,
3270 worktree ? got_worktree_get_root_path(worktree) : NULL);
3271 if (error)
3272 goto done;
3274 if (do_list)
3275 error = list_branches(repo, worktree);
3276 else if (delref)
3277 error = delete_branch(repo, delref);
3278 else {
3279 const char *base_branch;
3280 if (argc == 1) {
3281 base_branch = worktree ?
3282 got_worktree_get_head_ref_name(worktree) :
3283 GOT_REF_HEAD;
3284 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3285 base_branch += 11;
3286 } else
3287 base_branch = argv[1];
3288 error = add_branch(repo, argv[0], base_branch);
3290 done:
3291 if (repo)
3292 got_repo_close(repo);
3293 if (worktree)
3294 got_worktree_close(worktree);
3295 free(cwd);
3296 free(repo_path);
3297 return error;
3301 __dead static void
3302 usage_tag(void)
3304 fprintf(stderr,
3305 "usage: %s tag [-r repository] | -l | "
3306 "[-m message] name [commit]\n", getprogname());
3307 exit(1);
3310 #if 0
3311 static const struct got_error *
3312 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3314 const struct got_error *err = NULL;
3315 struct got_reflist_entry *re, *se, *new;
3316 struct got_object_id *re_id, *se_id;
3317 struct got_tag_object *re_tag, *se_tag;
3318 time_t re_time, se_time;
3320 SIMPLEQ_FOREACH(re, tags, entry) {
3321 se = SIMPLEQ_FIRST(sorted);
3322 if (se == NULL) {
3323 err = got_reflist_entry_dup(&new, re);
3324 if (err)
3325 return err;
3326 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3327 continue;
3328 } else {
3329 err = got_ref_resolve(&re_id, repo, re->ref);
3330 if (err)
3331 break;
3332 err = got_object_open_as_tag(&re_tag, repo, re_id);
3333 free(re_id);
3334 if (err)
3335 break;
3336 re_time = got_object_tag_get_tagger_time(re_tag);
3337 got_object_tag_close(re_tag);
3340 while (se) {
3341 err = got_ref_resolve(&se_id, repo, re->ref);
3342 if (err)
3343 break;
3344 err = got_object_open_as_tag(&se_tag, repo, se_id);
3345 free(se_id);
3346 if (err)
3347 break;
3348 se_time = got_object_tag_get_tagger_time(se_tag);
3349 got_object_tag_close(se_tag);
3351 if (se_time > re_time) {
3352 err = got_reflist_entry_dup(&new, re);
3353 if (err)
3354 return err;
3355 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3356 break;
3358 se = SIMPLEQ_NEXT(se, entry);
3359 continue;
3362 done:
3363 return err;
3365 #endif
3367 static const struct got_error *
3368 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3369 struct got_reference *ref2)
3371 const struct got_error *err = NULL;
3372 struct got_repository *repo = arg;
3373 struct got_object_id *id1, *id2 = NULL;
3374 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3375 time_t time1, time2;
3377 *cmp = 0;
3379 err = got_ref_resolve(&id1, repo, ref1);
3380 if (err)
3381 return err;
3382 err = got_object_open_as_tag(&tag1, repo, id1);
3383 if (err)
3384 goto done;
3386 err = got_ref_resolve(&id2, repo, ref2);
3387 if (err)
3388 goto done;
3389 err = got_object_open_as_tag(&tag2, repo, id2);
3390 if (err)
3391 goto done;
3393 time1 = got_object_tag_get_tagger_time(tag1);
3394 time2 = got_object_tag_get_tagger_time(tag2);
3396 /* Put latest tags first. */
3397 if (time1 < time2)
3398 *cmp = 1;
3399 else if (time1 > time2)
3400 *cmp = -1;
3401 else
3402 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3403 done:
3404 free(id1);
3405 free(id2);
3406 if (tag1)
3407 got_object_tag_close(tag1);
3408 if (tag2)
3409 got_object_tag_close(tag2);
3410 return err;
3413 static const struct got_error *
3414 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3416 static const struct got_error *err = NULL;
3417 struct got_reflist_head refs;
3418 struct got_reflist_entry *re;
3420 SIMPLEQ_INIT(&refs);
3422 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3423 if (err)
3424 return err;
3426 SIMPLEQ_FOREACH(re, &refs, entry) {
3427 const char *refname;
3428 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3429 char datebuf[26];
3430 time_t tagger_time;
3431 struct got_object_id *id;
3432 struct got_tag_object *tag;
3434 refname = got_ref_get_name(re->ref);
3435 if (strncmp(refname, "refs/tags/", 10) != 0)
3436 continue;
3437 refname += 10;
3438 refstr = got_ref_to_str(re->ref);
3439 if (refstr == NULL) {
3440 err = got_error_from_errno("got_ref_to_str");
3441 break;
3443 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3444 free(refstr);
3446 err = got_ref_resolve(&id, repo, re->ref);
3447 if (err)
3448 break;
3449 err = got_object_open_as_tag(&tag, repo, id);
3450 free(id);
3451 if (err)
3452 break;
3453 printf("from: %s\n", got_object_tag_get_tagger(tag));
3454 tagger_time = got_object_tag_get_tagger_time(tag);
3455 datestr = get_datestr(&tagger_time, datebuf);
3456 if (datestr)
3457 printf("date: %s UTC\n", datestr);
3458 err = got_object_id_str(&id_str,
3459 got_object_tag_get_object_id(tag));
3460 if (err)
3461 break;
3462 switch (got_object_tag_get_object_type(tag)) {
3463 case GOT_OBJ_TYPE_BLOB:
3464 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3465 break;
3466 case GOT_OBJ_TYPE_TREE:
3467 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3468 break;
3469 case GOT_OBJ_TYPE_COMMIT:
3470 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3471 break;
3472 case GOT_OBJ_TYPE_TAG:
3473 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3474 break;
3475 default:
3476 break;
3478 free(id_str);
3479 tagmsg0 = strdup(got_object_tag_get_message(tag));
3480 got_object_tag_close(tag);
3481 if (tagmsg0 == NULL) {
3482 err = got_error_from_errno("strdup");
3483 break;
3486 tagmsg = tagmsg0;
3487 do {
3488 line = strsep(&tagmsg, "\n");
3489 if (line)
3490 printf(" %s\n", line);
3491 } while (line);
3492 free(tagmsg0);
3495 got_ref_list_free(&refs);
3496 return NULL;
3499 static const struct got_error *
3500 get_tag_message(char **tagmsg, const char *commit_id_str,
3501 const char *tag_name, const char *repo_path)
3503 const struct got_error *err = NULL;
3504 char *template = NULL, *initial_content = NULL;
3505 char *tagmsg_path = NULL, *editor = NULL;
3506 int fd = -1;
3508 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3509 err = got_error_from_errno("asprintf");
3510 goto done;
3513 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3514 commit_id_str, tag_name) == -1) {
3515 err = got_error_from_errno("asprintf");
3516 goto done;
3519 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3520 if (err)
3521 goto done;
3523 dprintf(fd, initial_content);
3524 close(fd);
3526 err = get_editor(&editor);
3527 if (err)
3528 goto done;
3529 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3530 done:
3531 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3532 unlink(tagmsg_path);
3533 free(tagmsg_path);
3534 tagmsg_path = NULL;
3536 free(initial_content);
3537 free(template);
3538 free(editor);
3540 /* Editor is done; we can now apply unveil(2) */
3541 if (err == NULL) {
3542 err = apply_unveil(repo_path, 0, NULL);
3543 if (err) {
3544 free(*tagmsg);
3545 *tagmsg = NULL;
3548 return err;
3551 static const struct got_error *
3552 add_tag(struct got_repository *repo, const char *tag_name,
3553 const char *commit_arg, const char *tagmsg_arg)
3555 const struct got_error *err = NULL;
3556 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3557 char *label = NULL, *commit_id_str = NULL;
3558 struct got_reference *ref = NULL;
3559 char *refname = NULL, *tagmsg = NULL;
3560 const char *tagger;
3563 * Don't let the user create a tag named '-'.
3564 * While technically a valid reference name, this case is usually
3565 * an unintended typo.
3567 if (tag_name[0] == '-' && tag_name[1] == '\0')
3568 return got_error(GOT_ERR_BAD_REF_NAME);
3570 err = get_author(&tagger);
3571 if (err)
3572 return err;
3574 err = match_object_id(&commit_id, &label, commit_arg,
3575 GOT_OBJ_TYPE_COMMIT, 1, repo);
3576 if (err)
3577 goto done;
3579 err = got_object_id_str(&commit_id_str, commit_id);
3580 if (err)
3581 goto done;
3583 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3584 refname = strdup(tag_name);
3585 if (refname == NULL) {
3586 err = got_error_from_errno("strdup");
3587 goto done;
3589 tag_name += 10;
3590 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3591 err = got_error_from_errno("asprintf");
3592 goto done;
3595 err = got_ref_open(&ref, repo, refname, 0);
3596 if (err == NULL) {
3597 err = got_error(GOT_ERR_TAG_EXISTS);
3598 goto done;
3599 } else if (err->code != GOT_ERR_NOT_REF)
3600 goto done;
3602 if (tagmsg_arg == NULL) {
3603 err = get_tag_message(&tagmsg, commit_id_str,
3604 tag_name, got_repo_get_path(repo));
3605 if (err)
3606 goto done;
3609 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3610 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3611 if (err)
3612 goto done;
3614 err = got_ref_alloc(&ref, refname, tag_id);
3615 if (err)
3616 goto done;
3618 err = got_ref_write(ref, repo);
3620 if (err == NULL) {
3621 char *tag_id_str;
3622 err = got_object_id_str(&tag_id_str, tag_id);
3623 printf("Created tag %s\n", tag_id_str);
3624 free(tag_id_str);
3626 done:
3627 if (ref)
3628 got_ref_close(ref);
3629 free(commit_id);
3630 free(commit_id_str);
3631 free(refname);
3632 free(tagmsg);
3633 return err;
3636 static const struct got_error *
3637 cmd_tag(int argc, char *argv[])
3639 const struct got_error *error = NULL;
3640 struct got_repository *repo = NULL;
3641 struct got_worktree *worktree = NULL;
3642 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3643 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3644 int ch, do_list = 0;
3646 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3647 switch (ch) {
3648 case 'm':
3649 tagmsg = optarg;
3650 break;
3651 case 'r':
3652 repo_path = realpath(optarg, NULL);
3653 if (repo_path == NULL)
3654 err(1, "-r option");
3655 got_path_strip_trailing_slashes(repo_path);
3656 break;
3657 case 'l':
3658 do_list = 1;
3659 break;
3660 default:
3661 usage_tag();
3662 /* NOTREACHED */
3666 argc -= optind;
3667 argv += optind;
3669 if (do_list) {
3670 if (tagmsg)
3671 errx(1, "-l and -m options are mutually exclusive\n");
3672 if (argc > 0)
3673 usage_tag();
3674 } else if (argc < 1 || argc > 2)
3675 usage_tag();
3676 else if (argc > 1)
3677 commit_id_arg = argv[1];
3678 tag_name = argv[0];
3680 #ifndef PROFILE
3681 if (do_list) {
3682 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3683 NULL) == -1)
3684 err(1, "pledge");
3685 } else {
3686 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3687 "sendfd unveil", NULL) == -1)
3688 err(1, "pledge");
3690 #endif
3691 cwd = getcwd(NULL, 0);
3692 if (cwd == NULL) {
3693 error = got_error_from_errno("getcwd");
3694 goto done;
3697 if (repo_path == NULL) {
3698 error = got_worktree_open(&worktree, cwd);
3699 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3700 goto done;
3701 else
3702 error = NULL;
3703 if (worktree) {
3704 repo_path =
3705 strdup(got_worktree_get_repo_path(worktree));
3706 if (repo_path == NULL)
3707 error = got_error_from_errno("strdup");
3708 if (error)
3709 goto done;
3710 } else {
3711 repo_path = strdup(cwd);
3712 if (repo_path == NULL) {
3713 error = got_error_from_errno("strdup");
3714 goto done;
3719 error = got_repo_open(&repo, repo_path);
3720 if (error != NULL)
3721 goto done;
3724 if (do_list) {
3725 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3726 if (error)
3727 goto done;
3728 error = list_tags(repo, worktree);
3729 } else {
3730 if (tagmsg) {
3731 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3732 if (error)
3733 goto done;
3736 if (commit_id_arg == NULL) {
3737 struct got_reference *head_ref;
3738 struct got_object_id *commit_id;
3739 error = got_ref_open(&head_ref, repo,
3740 worktree ? got_worktree_get_head_ref_name(worktree)
3741 : GOT_REF_HEAD, 0);
3742 if (error)
3743 goto done;
3744 error = got_ref_resolve(&commit_id, repo, head_ref);
3745 got_ref_close(head_ref);
3746 if (error)
3747 goto done;
3748 error = got_object_id_str(&commit_id_str, commit_id);
3749 free(commit_id);
3750 if (error)
3751 goto done;
3754 error = add_tag(repo, tag_name,
3755 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3757 done:
3758 if (repo)
3759 got_repo_close(repo);
3760 if (worktree)
3761 got_worktree_close(worktree);
3762 free(cwd);
3763 free(repo_path);
3764 free(commit_id_str);
3765 return error;
3768 __dead static void
3769 usage_add(void)
3771 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3772 exit(1);
3775 static const struct got_error *
3776 cmd_add(int argc, char *argv[])
3778 const struct got_error *error = NULL;
3779 struct got_repository *repo = NULL;
3780 struct got_worktree *worktree = NULL;
3781 char *cwd = NULL;
3782 struct got_pathlist_head paths;
3783 struct got_pathlist_entry *pe;
3784 int ch;
3786 TAILQ_INIT(&paths);
3788 while ((ch = getopt(argc, argv, "")) != -1) {
3789 switch (ch) {
3790 default:
3791 usage_add();
3792 /* NOTREACHED */
3796 argc -= optind;
3797 argv += optind;
3799 #ifndef PROFILE
3800 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3801 NULL) == -1)
3802 err(1, "pledge");
3803 #endif
3804 if (argc < 1)
3805 usage_add();
3807 cwd = getcwd(NULL, 0);
3808 if (cwd == NULL) {
3809 error = got_error_from_errno("getcwd");
3810 goto done;
3813 error = got_worktree_open(&worktree, cwd);
3814 if (error)
3815 goto done;
3817 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3818 if (error != NULL)
3819 goto done;
3821 error = apply_unveil(got_repo_get_path(repo), 1,
3822 got_worktree_get_root_path(worktree));
3823 if (error)
3824 goto done;
3826 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3827 if (error)
3828 goto done;
3830 error = got_worktree_schedule_add(worktree, &paths, print_status,
3831 NULL, repo);
3832 done:
3833 if (repo)
3834 got_repo_close(repo);
3835 if (worktree)
3836 got_worktree_close(worktree);
3837 TAILQ_FOREACH(pe, &paths, entry)
3838 free((char *)pe->path);
3839 got_pathlist_free(&paths);
3840 free(cwd);
3841 return error;
3844 __dead static void
3845 usage_remove(void)
3847 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3848 exit(1);
3851 static const struct got_error *
3852 print_remove_status(void *arg, unsigned char status,
3853 unsigned char staged_status, const char *path,
3854 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3855 struct got_object_id *commit_id)
3857 if (status == GOT_STATUS_NONEXISTENT)
3858 return NULL;
3859 if (status == staged_status && (status == GOT_STATUS_DELETE))
3860 status = GOT_STATUS_NO_CHANGE;
3861 printf("%c%c %s\n", status, staged_status, path);
3862 return NULL;
3865 static const struct got_error *
3866 cmd_remove(int argc, char *argv[])
3868 const struct got_error *error = NULL;
3869 struct got_worktree *worktree = NULL;
3870 struct got_repository *repo = NULL;
3871 char *cwd = NULL;
3872 struct got_pathlist_head paths;
3873 struct got_pathlist_entry *pe;
3874 int ch, delete_local_mods = 0;
3876 TAILQ_INIT(&paths);
3878 while ((ch = getopt(argc, argv, "f")) != -1) {
3879 switch (ch) {
3880 case 'f':
3881 delete_local_mods = 1;
3882 break;
3883 default:
3884 usage_add();
3885 /* NOTREACHED */
3889 argc -= optind;
3890 argv += optind;
3892 #ifndef PROFILE
3893 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3894 NULL) == -1)
3895 err(1, "pledge");
3896 #endif
3897 if (argc < 1)
3898 usage_remove();
3900 cwd = getcwd(NULL, 0);
3901 if (cwd == NULL) {
3902 error = got_error_from_errno("getcwd");
3903 goto done;
3905 error = got_worktree_open(&worktree, cwd);
3906 if (error)
3907 goto done;
3909 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3910 if (error)
3911 goto done;
3913 error = apply_unveil(got_repo_get_path(repo), 1,
3914 got_worktree_get_root_path(worktree));
3915 if (error)
3916 goto done;
3918 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3919 if (error)
3920 goto done;
3922 error = got_worktree_schedule_delete(worktree, &paths,
3923 delete_local_mods, print_remove_status, NULL, repo);
3924 if (error)
3925 goto done;
3926 done:
3927 if (repo)
3928 got_repo_close(repo);
3929 if (worktree)
3930 got_worktree_close(worktree);
3931 TAILQ_FOREACH(pe, &paths, entry)
3932 free((char *)pe->path);
3933 got_pathlist_free(&paths);
3934 free(cwd);
3935 return error;
3938 __dead static void
3939 usage_revert(void)
3941 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3942 "path ...\n", getprogname());
3943 exit(1);
3946 static const struct got_error *
3947 revert_progress(void *arg, unsigned char status, const char *path)
3949 while (path[0] == '/')
3950 path++;
3951 printf("%c %s\n", status, path);
3952 return NULL;
3955 struct choose_patch_arg {
3956 FILE *patch_script_file;
3957 const char *action;
3960 static const struct got_error *
3961 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3962 int nchanges, const char *action)
3964 char *line = NULL;
3965 size_t linesize = 0;
3966 ssize_t linelen;
3968 switch (status) {
3969 case GOT_STATUS_ADD:
3970 printf("A %s\n%s this addition? [y/n] ", path, action);
3971 break;
3972 case GOT_STATUS_DELETE:
3973 printf("D %s\n%s this deletion? [y/n] ", path, action);
3974 break;
3975 case GOT_STATUS_MODIFY:
3976 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3977 return got_error_from_errno("fseek");
3978 printf(GOT_COMMIT_SEP_STR);
3979 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3980 printf("%s", line);
3981 if (ferror(patch_file))
3982 return got_error_from_errno("getline");
3983 printf(GOT_COMMIT_SEP_STR);
3984 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3985 path, n, nchanges, action);
3986 break;
3987 default:
3988 return got_error_path(path, GOT_ERR_FILE_STATUS);
3991 return NULL;
3994 static const struct got_error *
3995 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3996 FILE *patch_file, int n, int nchanges)
3998 const struct got_error *err = NULL;
3999 char *line = NULL;
4000 size_t linesize = 0;
4001 ssize_t linelen;
4002 int resp = ' ';
4003 struct choose_patch_arg *a = arg;
4005 *choice = GOT_PATCH_CHOICE_NONE;
4007 if (a->patch_script_file) {
4008 char *nl;
4009 err = show_change(status, path, patch_file, n, nchanges,
4010 a->action);
4011 if (err)
4012 return err;
4013 linelen = getline(&line, &linesize, a->patch_script_file);
4014 if (linelen == -1) {
4015 if (ferror(a->patch_script_file))
4016 return got_error_from_errno("getline");
4017 return NULL;
4019 nl = strchr(line, '\n');
4020 if (nl)
4021 *nl = '\0';
4022 if (strcmp(line, "y") == 0) {
4023 *choice = GOT_PATCH_CHOICE_YES;
4024 printf("y\n");
4025 } else if (strcmp(line, "n") == 0) {
4026 *choice = GOT_PATCH_CHOICE_NO;
4027 printf("n\n");
4028 } else if (strcmp(line, "q") == 0 &&
4029 status == GOT_STATUS_MODIFY) {
4030 *choice = GOT_PATCH_CHOICE_QUIT;
4031 printf("q\n");
4032 } else
4033 printf("invalid response '%s'\n", line);
4034 free(line);
4035 return NULL;
4038 while (resp != 'y' && resp != 'n' && resp != 'q') {
4039 err = show_change(status, path, patch_file, n, nchanges,
4040 a->action);
4041 if (err)
4042 return err;
4043 resp = getchar();
4044 if (resp == '\n')
4045 resp = getchar();
4046 if (status == GOT_STATUS_MODIFY) {
4047 if (resp != 'y' && resp != 'n' && resp != 'q') {
4048 printf("invalid response '%c'\n", resp);
4049 resp = ' ';
4051 } else if (resp != 'y' && resp != 'n') {
4052 printf("invalid response '%c'\n", resp);
4053 resp = ' ';
4057 if (resp == 'y')
4058 *choice = GOT_PATCH_CHOICE_YES;
4059 else if (resp == 'n')
4060 *choice = GOT_PATCH_CHOICE_NO;
4061 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4062 *choice = GOT_PATCH_CHOICE_QUIT;
4064 return NULL;
4068 static const struct got_error *
4069 cmd_revert(int argc, char *argv[])
4071 const struct got_error *error = NULL;
4072 struct got_worktree *worktree = NULL;
4073 struct got_repository *repo = NULL;
4074 char *cwd = NULL, *path = NULL;
4075 struct got_pathlist_head paths;
4076 struct got_pathlist_entry *pe;
4077 int ch, can_recurse = 0, pflag = 0;
4078 FILE *patch_script_file = NULL;
4079 const char *patch_script_path = NULL;
4080 struct choose_patch_arg cpa;
4082 TAILQ_INIT(&paths);
4084 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4085 switch (ch) {
4086 case 'p':
4087 pflag = 1;
4088 break;
4089 case 'F':
4090 patch_script_path = optarg;
4091 break;
4092 case 'R':
4093 can_recurse = 1;
4094 break;
4095 default:
4096 usage_revert();
4097 /* NOTREACHED */
4101 argc -= optind;
4102 argv += optind;
4104 #ifndef PROFILE
4105 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4106 "unveil", NULL) == -1)
4107 err(1, "pledge");
4108 #endif
4109 if (argc < 1)
4110 usage_revert();
4111 if (patch_script_path && !pflag)
4112 errx(1, "-F option can only be used together with -p option");
4114 cwd = getcwd(NULL, 0);
4115 if (cwd == NULL) {
4116 error = got_error_from_errno("getcwd");
4117 goto done;
4119 error = got_worktree_open(&worktree, cwd);
4120 if (error)
4121 goto done;
4123 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4124 if (error != NULL)
4125 goto done;
4127 if (patch_script_path) {
4128 patch_script_file = fopen(patch_script_path, "r");
4129 if (patch_script_file == NULL) {
4130 error = got_error_from_errno2("fopen",
4131 patch_script_path);
4132 goto done;
4135 error = apply_unveil(got_repo_get_path(repo), 1,
4136 got_worktree_get_root_path(worktree));
4137 if (error)
4138 goto done;
4140 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4141 if (error)
4142 goto done;
4144 if (!can_recurse) {
4145 char *ondisk_path;
4146 struct stat sb;
4147 TAILQ_FOREACH(pe, &paths, entry) {
4148 if (asprintf(&ondisk_path, "%s/%s",
4149 got_worktree_get_root_path(worktree),
4150 pe->path) == -1) {
4151 error = got_error_from_errno("asprintf");
4152 goto done;
4154 if (lstat(ondisk_path, &sb) == -1) {
4155 if (errno == ENOENT) {
4156 free(ondisk_path);
4157 continue;
4159 error = got_error_from_errno2("lstat",
4160 ondisk_path);
4161 free(ondisk_path);
4162 goto done;
4164 free(ondisk_path);
4165 if (S_ISDIR(sb.st_mode)) {
4166 error = got_error_msg(GOT_ERR_BAD_PATH,
4167 "reverting directories requires -R option");
4168 goto done;
4173 cpa.patch_script_file = patch_script_file;
4174 cpa.action = "revert";
4175 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4176 pflag ? choose_patch : NULL, &cpa, repo);
4177 if (error)
4178 goto done;
4179 done:
4180 if (patch_script_file && fclose(patch_script_file) == EOF &&
4181 error == NULL)
4182 error = got_error_from_errno2("fclose", patch_script_path);
4183 if (repo)
4184 got_repo_close(repo);
4185 if (worktree)
4186 got_worktree_close(worktree);
4187 free(path);
4188 free(cwd);
4189 return error;
4192 __dead static void
4193 usage_commit(void)
4195 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4196 getprogname());
4197 exit(1);
4200 struct collect_commit_logmsg_arg {
4201 const char *cmdline_log;
4202 const char *editor;
4203 const char *worktree_path;
4204 const char *branch_name;
4205 const char *repo_path;
4206 char *logmsg_path;
4210 static const struct got_error *
4211 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4212 void *arg)
4214 char *initial_content = NULL;
4215 struct got_pathlist_entry *pe;
4216 const struct got_error *err = NULL;
4217 char *template = NULL;
4218 struct collect_commit_logmsg_arg *a = arg;
4219 int fd;
4220 size_t len;
4222 /* if a message was specified on the command line, just use it */
4223 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4224 len = strlen(a->cmdline_log) + 1;
4225 *logmsg = malloc(len + 1);
4226 if (*logmsg == NULL)
4227 return got_error_from_errno("malloc");
4228 strlcpy(*logmsg, a->cmdline_log, len);
4229 return NULL;
4232 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4233 return got_error_from_errno("asprintf");
4235 if (asprintf(&initial_content,
4236 "\n# changes to be committed on branch %s:\n",
4237 a->branch_name) == -1)
4238 return got_error_from_errno("asprintf");
4240 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4241 if (err)
4242 goto done;
4244 dprintf(fd, initial_content);
4246 TAILQ_FOREACH(pe, commitable_paths, entry) {
4247 struct got_commitable *ct = pe->data;
4248 dprintf(fd, "# %c %s\n",
4249 got_commitable_get_status(ct),
4250 got_commitable_get_path(ct));
4252 close(fd);
4254 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4255 done:
4256 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4257 unlink(a->logmsg_path);
4258 free(a->logmsg_path);
4259 a->logmsg_path = NULL;
4261 free(initial_content);
4262 free(template);
4264 /* Editor is done; we can now apply unveil(2) */
4265 if (err == NULL) {
4266 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4267 if (err) {
4268 free(*logmsg);
4269 *logmsg = NULL;
4272 return err;
4275 static const struct got_error *
4276 cmd_commit(int argc, char *argv[])
4278 const struct got_error *error = NULL;
4279 struct got_worktree *worktree = NULL;
4280 struct got_repository *repo = NULL;
4281 char *cwd = NULL, *id_str = NULL;
4282 struct got_object_id *id = NULL;
4283 const char *logmsg = NULL;
4284 const char *author;
4285 struct collect_commit_logmsg_arg cl_arg;
4286 char *editor = NULL;
4287 int ch, rebase_in_progress, histedit_in_progress;
4288 struct got_pathlist_head paths;
4290 TAILQ_INIT(&paths);
4291 cl_arg.logmsg_path = NULL;
4293 while ((ch = getopt(argc, argv, "m:")) != -1) {
4294 switch (ch) {
4295 case 'm':
4296 logmsg = optarg;
4297 break;
4298 default:
4299 usage_commit();
4300 /* NOTREACHED */
4304 argc -= optind;
4305 argv += optind;
4307 #ifndef PROFILE
4308 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4309 "unveil", NULL) == -1)
4310 err(1, "pledge");
4311 #endif
4312 error = get_author(&author);
4313 if (error)
4314 return error;
4316 cwd = getcwd(NULL, 0);
4317 if (cwd == NULL) {
4318 error = got_error_from_errno("getcwd");
4319 goto done;
4321 error = got_worktree_open(&worktree, cwd);
4322 if (error)
4323 goto done;
4325 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4326 if (error)
4327 goto done;
4328 if (rebase_in_progress) {
4329 error = got_error(GOT_ERR_REBASING);
4330 goto done;
4333 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4334 worktree);
4335 if (error)
4336 goto done;
4338 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4339 if (error != NULL)
4340 goto done;
4343 * unveil(2) traverses exec(2); if an editor is used we have
4344 * to apply unveil after the log message has been written.
4346 if (logmsg == NULL || strlen(logmsg) == 0)
4347 error = get_editor(&editor);
4348 else
4349 error = apply_unveil(got_repo_get_path(repo), 0,
4350 got_worktree_get_root_path(worktree));
4351 if (error)
4352 goto done;
4354 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4355 if (error)
4356 goto done;
4358 cl_arg.editor = editor;
4359 cl_arg.cmdline_log = logmsg;
4360 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4361 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4362 if (!histedit_in_progress) {
4363 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4364 error = got_error(GOT_ERR_COMMIT_BRANCH);
4365 goto done;
4367 cl_arg.branch_name += 11;
4369 cl_arg.repo_path = got_repo_get_path(repo);
4370 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4371 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4372 if (error) {
4373 if (cl_arg.logmsg_path)
4374 fprintf(stderr, "%s: log message preserved in %s\n",
4375 getprogname(), cl_arg.logmsg_path);
4376 goto done;
4379 if (cl_arg.logmsg_path)
4380 unlink(cl_arg.logmsg_path);
4382 error = got_object_id_str(&id_str, id);
4383 if (error)
4384 goto done;
4385 printf("Created commit %s\n", id_str);
4386 done:
4387 free(cl_arg.logmsg_path);
4388 if (repo)
4389 got_repo_close(repo);
4390 if (worktree)
4391 got_worktree_close(worktree);
4392 free(cwd);
4393 free(id_str);
4394 free(editor);
4395 return error;
4398 __dead static void
4399 usage_cherrypick(void)
4401 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4402 exit(1);
4405 static const struct got_error *
4406 cmd_cherrypick(int argc, char *argv[])
4408 const struct got_error *error = NULL;
4409 struct got_worktree *worktree = NULL;
4410 struct got_repository *repo = NULL;
4411 char *cwd = NULL, *commit_id_str = NULL;
4412 struct got_object_id *commit_id = NULL;
4413 struct got_commit_object *commit = NULL;
4414 struct got_object_qid *pid;
4415 struct got_reference *head_ref = NULL;
4416 int ch, did_something = 0;
4418 while ((ch = getopt(argc, argv, "")) != -1) {
4419 switch (ch) {
4420 default:
4421 usage_cherrypick();
4422 /* NOTREACHED */
4426 argc -= optind;
4427 argv += optind;
4429 #ifndef PROFILE
4430 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4431 "unveil", NULL) == -1)
4432 err(1, "pledge");
4433 #endif
4434 if (argc != 1)
4435 usage_cherrypick();
4437 cwd = getcwd(NULL, 0);
4438 if (cwd == NULL) {
4439 error = got_error_from_errno("getcwd");
4440 goto done;
4442 error = got_worktree_open(&worktree, cwd);
4443 if (error)
4444 goto done;
4446 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4447 if (error != NULL)
4448 goto done;
4450 error = apply_unveil(got_repo_get_path(repo), 0,
4451 got_worktree_get_root_path(worktree));
4452 if (error)
4453 goto done;
4455 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4456 GOT_OBJ_TYPE_COMMIT, repo);
4457 if (error != NULL) {
4458 struct got_reference *ref;
4459 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4460 goto done;
4461 error = got_ref_open(&ref, repo, argv[0], 0);
4462 if (error != NULL)
4463 goto done;
4464 error = got_ref_resolve(&commit_id, repo, ref);
4465 got_ref_close(ref);
4466 if (error != NULL)
4467 goto done;
4469 error = got_object_id_str(&commit_id_str, commit_id);
4470 if (error)
4471 goto done;
4473 error = got_ref_open(&head_ref, repo,
4474 got_worktree_get_head_ref_name(worktree), 0);
4475 if (error != NULL)
4476 goto done;
4478 error = check_same_branch(commit_id, head_ref, NULL, repo);
4479 if (error) {
4480 if (error->code != GOT_ERR_ANCESTRY)
4481 goto done;
4482 error = NULL;
4483 } else {
4484 error = got_error(GOT_ERR_SAME_BRANCH);
4485 goto done;
4488 error = got_object_open_as_commit(&commit, repo, commit_id);
4489 if (error)
4490 goto done;
4491 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4492 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4493 commit_id, repo, update_progress, &did_something, check_cancelled,
4494 NULL);
4495 if (error != NULL)
4496 goto done;
4498 if (did_something)
4499 printf("Merged commit %s\n", commit_id_str);
4500 done:
4501 if (commit)
4502 got_object_commit_close(commit);
4503 free(commit_id_str);
4504 if (head_ref)
4505 got_ref_close(head_ref);
4506 if (worktree)
4507 got_worktree_close(worktree);
4508 if (repo)
4509 got_repo_close(repo);
4510 return error;
4513 __dead static void
4514 usage_backout(void)
4516 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4517 exit(1);
4520 static const struct got_error *
4521 cmd_backout(int argc, char *argv[])
4523 const struct got_error *error = NULL;
4524 struct got_worktree *worktree = NULL;
4525 struct got_repository *repo = NULL;
4526 char *cwd = NULL, *commit_id_str = NULL;
4527 struct got_object_id *commit_id = NULL;
4528 struct got_commit_object *commit = NULL;
4529 struct got_object_qid *pid;
4530 struct got_reference *head_ref = NULL;
4531 int ch, did_something = 0;
4533 while ((ch = getopt(argc, argv, "")) != -1) {
4534 switch (ch) {
4535 default:
4536 usage_backout();
4537 /* NOTREACHED */
4541 argc -= optind;
4542 argv += optind;
4544 #ifndef PROFILE
4545 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4546 "unveil", NULL) == -1)
4547 err(1, "pledge");
4548 #endif
4549 if (argc != 1)
4550 usage_backout();
4552 cwd = getcwd(NULL, 0);
4553 if (cwd == NULL) {
4554 error = got_error_from_errno("getcwd");
4555 goto done;
4557 error = got_worktree_open(&worktree, cwd);
4558 if (error)
4559 goto done;
4561 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4562 if (error != NULL)
4563 goto done;
4565 error = apply_unveil(got_repo_get_path(repo), 0,
4566 got_worktree_get_root_path(worktree));
4567 if (error)
4568 goto done;
4570 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4571 GOT_OBJ_TYPE_COMMIT, repo);
4572 if (error != NULL) {
4573 struct got_reference *ref;
4574 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4575 goto done;
4576 error = got_ref_open(&ref, repo, argv[0], 0);
4577 if (error != NULL)
4578 goto done;
4579 error = got_ref_resolve(&commit_id, repo, ref);
4580 got_ref_close(ref);
4581 if (error != NULL)
4582 goto done;
4584 error = got_object_id_str(&commit_id_str, commit_id);
4585 if (error)
4586 goto done;
4588 error = got_ref_open(&head_ref, repo,
4589 got_worktree_get_head_ref_name(worktree), 0);
4590 if (error != NULL)
4591 goto done;
4593 error = check_same_branch(commit_id, head_ref, NULL, repo);
4594 if (error)
4595 goto done;
4597 error = got_object_open_as_commit(&commit, repo, commit_id);
4598 if (error)
4599 goto done;
4600 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4601 if (pid == NULL) {
4602 error = got_error(GOT_ERR_ROOT_COMMIT);
4603 goto done;
4606 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4607 update_progress, &did_something, check_cancelled, NULL);
4608 if (error != NULL)
4609 goto done;
4611 if (did_something)
4612 printf("Backed out commit %s\n", commit_id_str);
4613 done:
4614 if (commit)
4615 got_object_commit_close(commit);
4616 free(commit_id_str);
4617 if (head_ref)
4618 got_ref_close(head_ref);
4619 if (worktree)
4620 got_worktree_close(worktree);
4621 if (repo)
4622 got_repo_close(repo);
4623 return error;
4626 __dead static void
4627 usage_rebase(void)
4629 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4630 getprogname());
4631 exit(1);
4634 void
4635 trim_logmsg(char *logmsg, int limit)
4637 char *nl;
4638 size_t len;
4640 len = strlen(logmsg);
4641 if (len > limit)
4642 len = limit;
4643 logmsg[len] = '\0';
4644 nl = strchr(logmsg, '\n');
4645 if (nl)
4646 *nl = '\0';
4649 static const struct got_error *
4650 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4652 const struct got_error *err;
4653 char *logmsg0 = NULL;
4654 const char *s;
4656 err = got_object_commit_get_logmsg(&logmsg0, commit);
4657 if (err)
4658 return err;
4660 s = logmsg0;
4661 while (isspace((unsigned char)s[0]))
4662 s++;
4664 *logmsg = strdup(s);
4665 if (*logmsg == NULL) {
4666 err = got_error_from_errno("strdup");
4667 goto done;
4670 trim_logmsg(*logmsg, limit);
4671 done:
4672 free(logmsg0);
4673 return err;
4676 static const struct got_error *
4677 show_rebase_progress(struct got_commit_object *commit,
4678 struct got_object_id *old_id, struct got_object_id *new_id)
4680 const struct got_error *err;
4681 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4683 err = got_object_id_str(&old_id_str, old_id);
4684 if (err)
4685 goto done;
4687 if (new_id) {
4688 err = got_object_id_str(&new_id_str, new_id);
4689 if (err)
4690 goto done;
4693 old_id_str[12] = '\0';
4694 if (new_id_str)
4695 new_id_str[12] = '\0';
4697 err = get_short_logmsg(&logmsg, 42, commit);
4698 if (err)
4699 goto done;
4701 printf("%s -> %s: %s\n", old_id_str,
4702 new_id_str ? new_id_str : "no-op change", logmsg);
4703 done:
4704 free(old_id_str);
4705 free(new_id_str);
4706 return err;
4709 static const struct got_error *
4710 rebase_progress(void *arg, unsigned char status, const char *path)
4712 unsigned char *rebase_status = arg;
4714 while (path[0] == '/')
4715 path++;
4716 printf("%c %s\n", status, path);
4718 if (*rebase_status == GOT_STATUS_CONFLICT)
4719 return NULL;
4720 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4721 *rebase_status = status;
4722 return NULL;
4725 static const struct got_error *
4726 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4727 struct got_reference *branch, struct got_reference *new_base_branch,
4728 struct got_reference *tmp_branch, struct got_repository *repo)
4730 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4731 return got_worktree_rebase_complete(worktree, fileindex,
4732 new_base_branch, tmp_branch, branch, repo);
4735 static const struct got_error *
4736 rebase_commit(struct got_pathlist_head *merged_paths,
4737 struct got_worktree *worktree, struct got_fileindex *fileindex,
4738 struct got_reference *tmp_branch,
4739 struct got_object_id *commit_id, struct got_repository *repo)
4741 const struct got_error *error;
4742 struct got_commit_object *commit;
4743 struct got_object_id *new_commit_id;
4745 error = got_object_open_as_commit(&commit, repo, commit_id);
4746 if (error)
4747 return error;
4749 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4750 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4751 if (error) {
4752 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4753 goto done;
4754 error = show_rebase_progress(commit, commit_id, NULL);
4755 } else {
4756 error = show_rebase_progress(commit, commit_id, new_commit_id);
4757 free(new_commit_id);
4759 done:
4760 got_object_commit_close(commit);
4761 return error;
4764 struct check_path_prefix_arg {
4765 const char *path_prefix;
4766 size_t len;
4767 int errcode;
4770 static const struct got_error *
4771 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4772 struct got_blob_object *blob2, struct got_object_id *id1,
4773 struct got_object_id *id2, const char *path1, const char *path2,
4774 struct got_repository *repo)
4776 struct check_path_prefix_arg *a = arg;
4778 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4779 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4780 return got_error(a->errcode);
4782 return NULL;
4785 static const struct got_error *
4786 check_path_prefix(struct got_object_id *parent_id,
4787 struct got_object_id *commit_id, const char *path_prefix,
4788 int errcode, struct got_repository *repo)
4790 const struct got_error *err;
4791 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4792 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4793 struct check_path_prefix_arg cpp_arg;
4795 if (got_path_is_root_dir(path_prefix))
4796 return NULL;
4798 err = got_object_open_as_commit(&commit, repo, commit_id);
4799 if (err)
4800 goto done;
4802 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4803 if (err)
4804 goto done;
4806 err = got_object_open_as_tree(&tree1, repo,
4807 got_object_commit_get_tree_id(parent_commit));
4808 if (err)
4809 goto done;
4811 err = got_object_open_as_tree(&tree2, repo,
4812 got_object_commit_get_tree_id(commit));
4813 if (err)
4814 goto done;
4816 cpp_arg.path_prefix = path_prefix;
4817 while (cpp_arg.path_prefix[0] == '/')
4818 cpp_arg.path_prefix++;
4819 cpp_arg.len = strlen(cpp_arg.path_prefix);
4820 cpp_arg.errcode = errcode;
4821 err = got_diff_tree(tree1, tree2, "", "", repo,
4822 check_path_prefix_in_diff, &cpp_arg, 0);
4823 done:
4824 if (tree1)
4825 got_object_tree_close(tree1);
4826 if (tree2)
4827 got_object_tree_close(tree2);
4828 if (commit)
4829 got_object_commit_close(commit);
4830 if (parent_commit)
4831 got_object_commit_close(parent_commit);
4832 return err;
4835 static const struct got_error *
4836 collect_commits(struct got_object_id_queue *commits,
4837 struct got_object_id *initial_commit_id,
4838 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4839 const char *path_prefix, int path_prefix_errcode,
4840 struct got_repository *repo)
4842 const struct got_error *err = NULL;
4843 struct got_commit_graph *graph = NULL;
4844 struct got_object_id *parent_id = NULL;
4845 struct got_object_qid *qid;
4846 struct got_object_id *commit_id = initial_commit_id;
4848 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4849 if (err)
4850 return err;
4852 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4853 check_cancelled, NULL);
4854 if (err)
4855 goto done;
4856 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4857 err = got_commit_graph_iter_next(&parent_id, graph);
4858 if (err) {
4859 if (err->code == GOT_ERR_ITER_COMPLETED) {
4860 err = got_error_msg(GOT_ERR_ANCESTRY,
4861 "ran out of commits to rebase before "
4862 "youngest common ancestor commit has "
4863 "been reached?!?");
4864 goto done;
4865 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4866 goto done;
4867 err = got_commit_graph_fetch_commits(graph, 1, repo,
4868 check_cancelled, NULL);
4869 if (err)
4870 goto done;
4871 } else {
4872 err = check_path_prefix(parent_id, commit_id,
4873 path_prefix, path_prefix_errcode, repo);
4874 if (err)
4875 goto done;
4877 err = got_object_qid_alloc(&qid, commit_id);
4878 if (err)
4879 goto done;
4880 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4881 commit_id = parent_id;
4884 done:
4885 got_commit_graph_close(graph);
4886 return err;
4889 static const struct got_error *
4890 cmd_rebase(int argc, char *argv[])
4892 const struct got_error *error = NULL;
4893 struct got_worktree *worktree = NULL;
4894 struct got_repository *repo = NULL;
4895 struct got_fileindex *fileindex = NULL;
4896 char *cwd = NULL;
4897 struct got_reference *branch = NULL;
4898 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4899 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4900 struct got_object_id *resume_commit_id = NULL;
4901 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4902 struct got_commit_object *commit = NULL;
4903 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4904 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4905 struct got_object_id_queue commits;
4906 struct got_pathlist_head merged_paths;
4907 const struct got_object_id_queue *parent_ids;
4908 struct got_object_qid *qid, *pid;
4910 SIMPLEQ_INIT(&commits);
4911 TAILQ_INIT(&merged_paths);
4913 while ((ch = getopt(argc, argv, "ac")) != -1) {
4914 switch (ch) {
4915 case 'a':
4916 abort_rebase = 1;
4917 break;
4918 case 'c':
4919 continue_rebase = 1;
4920 break;
4921 default:
4922 usage_rebase();
4923 /* NOTREACHED */
4927 argc -= optind;
4928 argv += optind;
4930 #ifndef PROFILE
4931 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4932 "unveil", NULL) == -1)
4933 err(1, "pledge");
4934 #endif
4935 if (abort_rebase && continue_rebase)
4936 usage_rebase();
4937 else if (abort_rebase || continue_rebase) {
4938 if (argc != 0)
4939 usage_rebase();
4940 } else if (argc != 1)
4941 usage_rebase();
4943 cwd = getcwd(NULL, 0);
4944 if (cwd == NULL) {
4945 error = got_error_from_errno("getcwd");
4946 goto done;
4948 error = got_worktree_open(&worktree, cwd);
4949 if (error)
4950 goto done;
4952 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4953 if (error != NULL)
4954 goto done;
4956 error = apply_unveil(got_repo_get_path(repo), 0,
4957 got_worktree_get_root_path(worktree));
4958 if (error)
4959 goto done;
4961 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4962 if (error)
4963 goto done;
4965 if (abort_rebase) {
4966 int did_something;
4967 if (!rebase_in_progress) {
4968 error = got_error(GOT_ERR_NOT_REBASING);
4969 goto done;
4971 error = got_worktree_rebase_continue(&resume_commit_id,
4972 &new_base_branch, &tmp_branch, &branch, &fileindex,
4973 worktree, repo);
4974 if (error)
4975 goto done;
4976 printf("Switching work tree to %s\n",
4977 got_ref_get_symref_target(new_base_branch));
4978 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4979 new_base_branch, update_progress, &did_something);
4980 if (error)
4981 goto done;
4982 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4983 goto done; /* nothing else to do */
4986 if (continue_rebase) {
4987 if (!rebase_in_progress) {
4988 error = got_error(GOT_ERR_NOT_REBASING);
4989 goto done;
4991 error = got_worktree_rebase_continue(&resume_commit_id,
4992 &new_base_branch, &tmp_branch, &branch, &fileindex,
4993 worktree, repo);
4994 if (error)
4995 goto done;
4997 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4998 resume_commit_id, repo);
4999 if (error)
5000 goto done;
5002 yca_id = got_object_id_dup(resume_commit_id);
5003 if (yca_id == NULL) {
5004 error = got_error_from_errno("got_object_id_dup");
5005 goto done;
5007 } else {
5008 error = got_ref_open(&branch, repo, argv[0], 0);
5009 if (error != NULL)
5010 goto done;
5013 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5014 if (error)
5015 goto done;
5017 if (!continue_rebase) {
5018 struct got_object_id *base_commit_id;
5020 base_commit_id = got_worktree_get_base_commit_id(worktree);
5021 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5022 base_commit_id, branch_head_commit_id, repo,
5023 check_cancelled, NULL);
5024 if (error)
5025 goto done;
5026 if (yca_id == NULL) {
5027 error = got_error_msg(GOT_ERR_ANCESTRY,
5028 "specified branch shares no common ancestry "
5029 "with work tree's branch");
5030 goto done;
5033 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5034 if (error) {
5035 if (error->code != GOT_ERR_ANCESTRY)
5036 goto done;
5037 error = NULL;
5038 } else {
5039 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5040 "specified branch resolves to a commit which "
5041 "is already contained in work tree's branch");
5042 goto done;
5044 error = got_worktree_rebase_prepare(&new_base_branch,
5045 &tmp_branch, &fileindex, worktree, branch, repo);
5046 if (error)
5047 goto done;
5050 commit_id = branch_head_commit_id;
5051 error = got_object_open_as_commit(&commit, repo, commit_id);
5052 if (error)
5053 goto done;
5055 parent_ids = got_object_commit_get_parent_ids(commit);
5056 pid = SIMPLEQ_FIRST(parent_ids);
5057 if (pid == NULL) {
5058 if (!continue_rebase) {
5059 int did_something;
5060 error = got_worktree_rebase_abort(worktree, fileindex,
5061 repo, new_base_branch, update_progress,
5062 &did_something);
5063 if (error)
5064 goto done;
5065 printf("Rebase of %s aborted\n",
5066 got_ref_get_name(branch));
5068 error = got_error(GOT_ERR_EMPTY_REBASE);
5069 goto done;
5071 error = collect_commits(&commits, commit_id, pid->id,
5072 yca_id, got_worktree_get_path_prefix(worktree),
5073 GOT_ERR_REBASE_PATH, repo);
5074 got_object_commit_close(commit);
5075 commit = NULL;
5076 if (error)
5077 goto done;
5079 if (SIMPLEQ_EMPTY(&commits)) {
5080 if (continue_rebase)
5081 error = rebase_complete(worktree, fileindex,
5082 branch, new_base_branch, tmp_branch, repo);
5083 else
5084 error = got_error(GOT_ERR_EMPTY_REBASE);
5085 goto done;
5088 pid = NULL;
5089 SIMPLEQ_FOREACH(qid, &commits, entry) {
5090 commit_id = qid->id;
5091 parent_id = pid ? pid->id : yca_id;
5092 pid = qid;
5094 error = got_worktree_rebase_merge_files(&merged_paths,
5095 worktree, fileindex, parent_id, commit_id, repo,
5096 rebase_progress, &rebase_status, check_cancelled, NULL);
5097 if (error)
5098 goto done;
5100 if (rebase_status == GOT_STATUS_CONFLICT) {
5101 got_worktree_rebase_pathlist_free(&merged_paths);
5102 break;
5105 error = rebase_commit(&merged_paths, worktree, fileindex,
5106 tmp_branch, commit_id, repo);
5107 got_worktree_rebase_pathlist_free(&merged_paths);
5108 if (error)
5109 goto done;
5112 if (rebase_status == GOT_STATUS_CONFLICT) {
5113 error = got_worktree_rebase_postpone(worktree, fileindex);
5114 if (error)
5115 goto done;
5116 error = got_error_msg(GOT_ERR_CONFLICTS,
5117 "conflicts must be resolved before rebasing can continue");
5118 } else
5119 error = rebase_complete(worktree, fileindex, branch,
5120 new_base_branch, tmp_branch, repo);
5121 done:
5122 got_object_id_queue_free(&commits);
5123 free(branch_head_commit_id);
5124 free(resume_commit_id);
5125 free(yca_id);
5126 if (commit)
5127 got_object_commit_close(commit);
5128 if (branch)
5129 got_ref_close(branch);
5130 if (new_base_branch)
5131 got_ref_close(new_base_branch);
5132 if (tmp_branch)
5133 got_ref_close(tmp_branch);
5134 if (worktree)
5135 got_worktree_close(worktree);
5136 if (repo)
5137 got_repo_close(repo);
5138 return error;
5141 __dead static void
5142 usage_histedit(void)
5144 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5145 getprogname());
5146 exit(1);
5149 #define GOT_HISTEDIT_PICK 'p'
5150 #define GOT_HISTEDIT_EDIT 'e'
5151 #define GOT_HISTEDIT_FOLD 'f'
5152 #define GOT_HISTEDIT_DROP 'd'
5153 #define GOT_HISTEDIT_MESG 'm'
5155 static struct got_histedit_cmd {
5156 unsigned char code;
5157 const char *name;
5158 const char *desc;
5159 } got_histedit_cmds[] = {
5160 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5161 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5162 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5163 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5164 { GOT_HISTEDIT_MESG, "mesg",
5165 "single-line log message for commit above (open editor if empty)" },
5168 struct got_histedit_list_entry {
5169 TAILQ_ENTRY(got_histedit_list_entry) entry;
5170 struct got_object_id *commit_id;
5171 const struct got_histedit_cmd *cmd;
5172 char *logmsg;
5174 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5176 static const struct got_error *
5177 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5178 FILE *f, struct got_repository *repo)
5180 const struct got_error *err = NULL;
5181 char *logmsg = NULL, *id_str = NULL;
5182 struct got_commit_object *commit = NULL;
5183 int n;
5185 err = got_object_open_as_commit(&commit, repo, commit_id);
5186 if (err)
5187 goto done;
5189 err = get_short_logmsg(&logmsg, 34, commit);
5190 if (err)
5191 goto done;
5193 err = got_object_id_str(&id_str, commit_id);
5194 if (err)
5195 goto done;
5197 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5198 if (n < 0)
5199 err = got_ferror(f, GOT_ERR_IO);
5200 done:
5201 if (commit)
5202 got_object_commit_close(commit);
5203 free(id_str);
5204 free(logmsg);
5205 return err;
5208 static const struct got_error *
5209 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5210 struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 struct got_object_qid *qid;
5215 if (SIMPLEQ_EMPTY(commits))
5216 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5218 SIMPLEQ_FOREACH(qid, commits, entry) {
5219 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5220 f, repo);
5221 if (err)
5222 break;
5225 return err;
5228 static const struct got_error *
5229 write_cmd_list(FILE *f)
5231 const struct got_error *err = NULL;
5232 int n, i;
5234 n = fprintf(f, "# Available histedit commands:\n");
5235 if (n < 0)
5236 return got_ferror(f, GOT_ERR_IO);
5238 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5239 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5240 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5241 cmd->desc);
5242 if (n < 0) {
5243 err = got_ferror(f, GOT_ERR_IO);
5244 break;
5247 n = fprintf(f, "# Commits will be processed in order from top to "
5248 "bottom of this file.\n");
5249 if (n < 0)
5250 return got_ferror(f, GOT_ERR_IO);
5251 return err;
5254 static const struct got_error *
5255 histedit_syntax_error(int lineno)
5257 static char msg[42];
5258 int ret;
5260 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5261 lineno);
5262 if (ret == -1 || ret >= sizeof(msg))
5263 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5265 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5268 static const struct got_error *
5269 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5270 char *logmsg, struct got_repository *repo)
5272 const struct got_error *err;
5273 struct got_commit_object *folded_commit = NULL;
5274 char *id_str, *folded_logmsg = NULL;
5276 err = got_object_id_str(&id_str, hle->commit_id);
5277 if (err)
5278 return err;
5280 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5281 if (err)
5282 goto done;
5284 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5285 if (err)
5286 goto done;
5287 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5288 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5289 folded_logmsg) == -1) {
5290 err = got_error_from_errno("asprintf");
5291 goto done;
5293 done:
5294 if (folded_commit)
5295 got_object_commit_close(folded_commit);
5296 free(id_str);
5297 free(folded_logmsg);
5298 return err;
5301 static struct got_histedit_list_entry *
5302 get_folded_commits(struct got_histedit_list_entry *hle)
5304 struct got_histedit_list_entry *prev, *folded = NULL;
5306 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5307 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5308 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5309 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5310 folded = prev;
5311 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5314 return folded;
5317 static const struct got_error *
5318 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5319 struct got_repository *repo)
5321 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5322 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5323 const struct got_error *err = NULL;
5324 struct got_commit_object *commit = NULL;
5325 int fd;
5326 struct got_histedit_list_entry *folded = NULL;
5328 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5329 if (err)
5330 return err;
5332 folded = get_folded_commits(hle);
5333 if (folded) {
5334 while (folded != hle) {
5335 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5336 folded = TAILQ_NEXT(folded, entry);
5337 continue;
5339 err = append_folded_commit_msg(&new_msg, folded,
5340 logmsg, repo);
5341 if (err)
5342 goto done;
5343 free(logmsg);
5344 logmsg = new_msg;
5345 folded = TAILQ_NEXT(folded, entry);
5349 err = got_object_id_str(&id_str, hle->commit_id);
5350 if (err)
5351 goto done;
5352 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5353 if (err)
5354 goto done;
5355 if (asprintf(&new_msg,
5356 "%s\n# original log message of commit %s: %s",
5357 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5358 err = got_error_from_errno("asprintf");
5359 goto done;
5361 free(logmsg);
5362 logmsg = new_msg;
5364 err = got_object_id_str(&id_str, hle->commit_id);
5365 if (err)
5366 goto done;
5368 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5369 if (err)
5370 goto done;
5372 dprintf(fd, logmsg);
5373 close(fd);
5375 err = get_editor(&editor);
5376 if (err)
5377 goto done;
5379 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5380 if (err) {
5381 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5382 goto done;
5383 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5385 done:
5386 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5387 err = got_error_from_errno2("unlink", logmsg_path);
5388 free(logmsg_path);
5389 free(logmsg);
5390 free(orig_logmsg);
5391 free(editor);
5392 if (commit)
5393 got_object_commit_close(commit);
5394 return err;
5397 static const struct got_error *
5398 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5399 FILE *f, struct got_repository *repo)
5401 const struct got_error *err = NULL;
5402 char *line = NULL, *p, *end;
5403 size_t size;
5404 ssize_t len;
5405 int lineno = 0, i;
5406 const struct got_histedit_cmd *cmd;
5407 struct got_object_id *commit_id = NULL;
5408 struct got_histedit_list_entry *hle = NULL;
5410 for (;;) {
5411 len = getline(&line, &size, f);
5412 if (len == -1) {
5413 const struct got_error *getline_err;
5414 if (feof(f))
5415 break;
5416 getline_err = got_error_from_errno("getline");
5417 err = got_ferror(f, getline_err->code);
5418 break;
5420 lineno++;
5421 p = line;
5422 while (isspace((unsigned char)p[0]))
5423 p++;
5424 if (p[0] == '#' || p[0] == '\0') {
5425 free(line);
5426 line = NULL;
5427 continue;
5429 cmd = NULL;
5430 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5431 cmd = &got_histedit_cmds[i];
5432 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5433 isspace((unsigned char)p[strlen(cmd->name)])) {
5434 p += strlen(cmd->name);
5435 break;
5437 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5438 p++;
5439 break;
5442 if (i == nitems(got_histedit_cmds)) {
5443 err = histedit_syntax_error(lineno);
5444 break;
5446 while (isspace((unsigned char)p[0]))
5447 p++;
5448 if (cmd->code == GOT_HISTEDIT_MESG) {
5449 if (hle == NULL || hle->logmsg != NULL) {
5450 err = got_error(GOT_ERR_HISTEDIT_CMD);
5451 break;
5453 if (p[0] == '\0') {
5454 err = histedit_edit_logmsg(hle, repo);
5455 if (err)
5456 break;
5457 } else {
5458 hle->logmsg = strdup(p);
5459 if (hle->logmsg == NULL) {
5460 err = got_error_from_errno("strdup");
5461 break;
5464 free(line);
5465 line = NULL;
5466 continue;
5467 } else {
5468 end = p;
5469 while (end[0] && !isspace((unsigned char)end[0]))
5470 end++;
5471 *end = '\0';
5473 err = got_object_resolve_id_str(&commit_id, repo, p);
5474 if (err) {
5475 /* override error code */
5476 err = histedit_syntax_error(lineno);
5477 break;
5480 hle = malloc(sizeof(*hle));
5481 if (hle == NULL) {
5482 err = got_error_from_errno("malloc");
5483 break;
5485 hle->cmd = cmd;
5486 hle->commit_id = commit_id;
5487 hle->logmsg = NULL;
5488 commit_id = NULL;
5489 free(line);
5490 line = NULL;
5491 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5494 free(line);
5495 free(commit_id);
5496 return err;
5499 static const struct got_error *
5500 histedit_check_script(struct got_histedit_list *histedit_cmds,
5501 struct got_object_id_queue *commits, struct got_repository *repo)
5503 const struct got_error *err = NULL;
5504 struct got_object_qid *qid;
5505 struct got_histedit_list_entry *hle;
5506 static char msg[80];
5507 char *id_str;
5509 if (TAILQ_EMPTY(histedit_cmds))
5510 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5511 "histedit script contains no commands");
5512 if (SIMPLEQ_EMPTY(commits))
5513 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5515 SIMPLEQ_FOREACH(qid, commits, entry) {
5516 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5517 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5518 break;
5520 if (hle == NULL) {
5521 err = got_object_id_str(&id_str, qid->id);
5522 if (err)
5523 return err;
5524 snprintf(msg, sizeof(msg),
5525 "commit %s missing from histedit script", id_str);
5526 free(id_str);
5527 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5531 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5532 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5533 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5534 "last commit in histedit script cannot be folded");
5536 return NULL;
5539 static const struct got_error *
5540 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5541 const char *path, struct got_object_id_queue *commits,
5542 struct got_repository *repo)
5544 const struct got_error *err = NULL;
5545 char *editor;
5546 FILE *f = NULL;
5548 err = get_editor(&editor);
5549 if (err)
5550 return err;
5552 if (spawn_editor(editor, path) == -1) {
5553 err = got_error_from_errno("failed spawning editor");
5554 goto done;
5557 f = fopen(path, "r");
5558 if (f == NULL) {
5559 err = got_error_from_errno("fopen");
5560 goto done;
5562 err = histedit_parse_list(histedit_cmds, f, repo);
5563 if (err)
5564 goto done;
5566 err = histedit_check_script(histedit_cmds, commits, repo);
5567 done:
5568 if (f && fclose(f) != 0 && err == NULL)
5569 err = got_error_from_errno("fclose");
5570 free(editor);
5571 return err;
5574 static const struct got_error *
5575 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5576 struct got_object_id_queue *, const char *, struct got_repository *);
5578 static const struct got_error *
5579 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5580 struct got_object_id_queue *commits, struct got_repository *repo)
5582 const struct got_error *err;
5583 FILE *f = NULL;
5584 char *path = NULL;
5586 err = got_opentemp_named(&path, &f, "got-histedit");
5587 if (err)
5588 return err;
5590 err = write_cmd_list(f);
5591 if (err)
5592 goto done;
5594 err = histedit_write_commit_list(commits, f, repo);
5595 if (err)
5596 goto done;
5598 if (fclose(f) != 0) {
5599 err = got_error_from_errno("fclose");
5600 goto done;
5602 f = NULL;
5604 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5605 if (err) {
5606 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5607 err->code != GOT_ERR_HISTEDIT_CMD)
5608 goto done;
5609 err = histedit_edit_list_retry(histedit_cmds, err,
5610 commits, path, repo);
5612 done:
5613 if (f && fclose(f) != 0 && err == NULL)
5614 err = got_error_from_errno("fclose");
5615 if (path && unlink(path) != 0 && err == NULL)
5616 err = got_error_from_errno2("unlink", path);
5617 free(path);
5618 return err;
5621 static const struct got_error *
5622 histedit_save_list(struct got_histedit_list *histedit_cmds,
5623 struct got_worktree *worktree, struct got_repository *repo)
5625 const struct got_error *err = NULL;
5626 char *path = NULL;
5627 FILE *f = NULL;
5628 struct got_histedit_list_entry *hle;
5629 struct got_commit_object *commit = NULL;
5631 err = got_worktree_get_histedit_script_path(&path, worktree);
5632 if (err)
5633 return err;
5635 f = fopen(path, "w");
5636 if (f == NULL) {
5637 err = got_error_from_errno2("fopen", path);
5638 goto done;
5640 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5641 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5642 repo);
5643 if (err)
5644 break;
5646 if (hle->logmsg) {
5647 int n = fprintf(f, "%c %s\n",
5648 GOT_HISTEDIT_MESG, hle->logmsg);
5649 if (n < 0) {
5650 err = got_ferror(f, GOT_ERR_IO);
5651 break;
5655 done:
5656 if (f && fclose(f) != 0 && err == NULL)
5657 err = got_error_from_errno("fclose");
5658 free(path);
5659 if (commit)
5660 got_object_commit_close(commit);
5661 return err;
5664 void
5665 histedit_free_list(struct got_histedit_list *histedit_cmds)
5667 struct got_histedit_list_entry *hle;
5669 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5670 TAILQ_REMOVE(histedit_cmds, hle, entry);
5671 free(hle);
5675 static const struct got_error *
5676 histedit_load_list(struct got_histedit_list *histedit_cmds,
5677 const char *path, struct got_repository *repo)
5679 const struct got_error *err = NULL;
5680 FILE *f = NULL;
5682 f = fopen(path, "r");
5683 if (f == NULL) {
5684 err = got_error_from_errno2("fopen", path);
5685 goto done;
5688 err = histedit_parse_list(histedit_cmds, f, repo);
5689 done:
5690 if (f && fclose(f) != 0 && err == NULL)
5691 err = got_error_from_errno("fclose");
5692 return err;
5695 static const struct got_error *
5696 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5697 const struct got_error *edit_err, struct got_object_id_queue *commits,
5698 const char *path, struct got_repository *repo)
5700 const struct got_error *err = NULL, *prev_err = edit_err;
5701 int resp = ' ';
5703 while (resp != 'c' && resp != 'r' && resp != 'a') {
5704 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5705 "or (a)bort: ", getprogname(), prev_err->msg);
5706 resp = getchar();
5707 if (resp == '\n')
5708 resp = getchar();
5709 if (resp == 'c') {
5710 histedit_free_list(histedit_cmds);
5711 err = histedit_run_editor(histedit_cmds, path, commits,
5712 repo);
5713 if (err) {
5714 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5715 err->code != GOT_ERR_HISTEDIT_CMD)
5716 break;
5717 prev_err = err;
5718 resp = ' ';
5719 continue;
5721 break;
5722 } else if (resp == 'r') {
5723 histedit_free_list(histedit_cmds);
5724 err = histedit_edit_script(histedit_cmds,
5725 commits, repo);
5726 if (err) {
5727 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5728 err->code != GOT_ERR_HISTEDIT_CMD)
5729 break;
5730 prev_err = err;
5731 resp = ' ';
5732 continue;
5734 break;
5735 } else if (resp == 'a') {
5736 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5737 break;
5738 } else
5739 printf("invalid response '%c'\n", resp);
5742 return err;
5745 static const struct got_error *
5746 histedit_complete(struct got_worktree *worktree,
5747 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5748 struct got_reference *branch, struct got_repository *repo)
5750 printf("Switching work tree to %s\n",
5751 got_ref_get_symref_target(branch));
5752 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5753 branch, repo);
5756 static const struct got_error *
5757 show_histedit_progress(struct got_commit_object *commit,
5758 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5760 const struct got_error *err;
5761 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5763 err = got_object_id_str(&old_id_str, hle->commit_id);
5764 if (err)
5765 goto done;
5767 if (new_id) {
5768 err = got_object_id_str(&new_id_str, new_id);
5769 if (err)
5770 goto done;
5773 old_id_str[12] = '\0';
5774 if (new_id_str)
5775 new_id_str[12] = '\0';
5777 if (hle->logmsg) {
5778 logmsg = strdup(hle->logmsg);
5779 if (logmsg == NULL) {
5780 err = got_error_from_errno("strdup");
5781 goto done;
5783 trim_logmsg(logmsg, 42);
5784 } else {
5785 err = get_short_logmsg(&logmsg, 42, commit);
5786 if (err)
5787 goto done;
5790 switch (hle->cmd->code) {
5791 case GOT_HISTEDIT_PICK:
5792 case GOT_HISTEDIT_EDIT:
5793 printf("%s -> %s: %s\n", old_id_str,
5794 new_id_str ? new_id_str : "no-op change", logmsg);
5795 break;
5796 case GOT_HISTEDIT_DROP:
5797 case GOT_HISTEDIT_FOLD:
5798 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5799 logmsg);
5800 break;
5801 default:
5802 break;
5805 done:
5806 free(old_id_str);
5807 free(new_id_str);
5808 return err;
5811 static const struct got_error *
5812 histedit_commit(struct got_pathlist_head *merged_paths,
5813 struct got_worktree *worktree, struct got_fileindex *fileindex,
5814 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5815 struct got_repository *repo)
5817 const struct got_error *err;
5818 struct got_commit_object *commit;
5819 struct got_object_id *new_commit_id;
5821 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5822 && hle->logmsg == NULL) {
5823 err = histedit_edit_logmsg(hle, repo);
5824 if (err)
5825 return err;
5828 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5829 if (err)
5830 return err;
5832 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5833 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5834 hle->logmsg, repo);
5835 if (err) {
5836 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5837 goto done;
5838 err = show_histedit_progress(commit, hle, NULL);
5839 } else {
5840 err = show_histedit_progress(commit, hle, new_commit_id);
5841 free(new_commit_id);
5843 done:
5844 got_object_commit_close(commit);
5845 return err;
5848 static const struct got_error *
5849 histedit_skip_commit(struct got_histedit_list_entry *hle,
5850 struct got_worktree *worktree, struct got_repository *repo)
5852 const struct got_error *error;
5853 struct got_commit_object *commit;
5855 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5856 repo);
5857 if (error)
5858 return error;
5860 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5861 if (error)
5862 return error;
5864 error = show_histedit_progress(commit, hle, NULL);
5865 got_object_commit_close(commit);
5866 return error;
5869 static const struct got_error *
5870 cmd_histedit(int argc, char *argv[])
5872 const struct got_error *error = NULL;
5873 struct got_worktree *worktree = NULL;
5874 struct got_fileindex *fileindex = NULL;
5875 struct got_repository *repo = NULL;
5876 char *cwd = NULL;
5877 struct got_reference *branch = NULL;
5878 struct got_reference *tmp_branch = NULL;
5879 struct got_object_id *resume_commit_id = NULL;
5880 struct got_object_id *base_commit_id = NULL;
5881 struct got_object_id *head_commit_id = NULL;
5882 struct got_commit_object *commit = NULL;
5883 int ch, rebase_in_progress = 0, did_something;
5884 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5885 const char *edit_script_path = NULL;
5886 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5887 struct got_object_id_queue commits;
5888 struct got_pathlist_head merged_paths;
5889 const struct got_object_id_queue *parent_ids;
5890 struct got_object_qid *pid;
5891 struct got_histedit_list histedit_cmds;
5892 struct got_histedit_list_entry *hle;
5894 SIMPLEQ_INIT(&commits);
5895 TAILQ_INIT(&histedit_cmds);
5896 TAILQ_INIT(&merged_paths);
5898 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5899 switch (ch) {
5900 case 'a':
5901 abort_edit = 1;
5902 break;
5903 case 'c':
5904 continue_edit = 1;
5905 break;
5906 case 'F':
5907 edit_script_path = optarg;
5908 break;
5909 default:
5910 usage_histedit();
5911 /* NOTREACHED */
5915 argc -= optind;
5916 argv += optind;
5918 #ifndef PROFILE
5919 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5920 "unveil", NULL) == -1)
5921 err(1, "pledge");
5922 #endif
5923 if (abort_edit && continue_edit)
5924 usage_histedit();
5925 if (argc != 0)
5926 usage_histedit();
5929 * This command cannot apply unveil(2) in all cases because the
5930 * user may choose to run an editor to edit the histedit script
5931 * and to edit individual commit log messages.
5932 * unveil(2) traverses exec(2); if an editor is used we have to
5933 * apply unveil after edit script and log messages have been written.
5934 * XXX TODO: Make use of unveil(2) where possible.
5937 cwd = getcwd(NULL, 0);
5938 if (cwd == NULL) {
5939 error = got_error_from_errno("getcwd");
5940 goto done;
5942 error = got_worktree_open(&worktree, cwd);
5943 if (error)
5944 goto done;
5946 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5947 if (error != NULL)
5948 goto done;
5950 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5951 if (error)
5952 goto done;
5953 if (rebase_in_progress) {
5954 error = got_error(GOT_ERR_REBASING);
5955 goto done;
5958 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5959 if (error)
5960 goto done;
5962 if (edit_in_progress && abort_edit) {
5963 error = got_worktree_histedit_continue(&resume_commit_id,
5964 &tmp_branch, &branch, &base_commit_id, &fileindex,
5965 worktree, repo);
5966 if (error)
5967 goto done;
5968 printf("Switching work tree to %s\n",
5969 got_ref_get_symref_target(branch));
5970 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5971 branch, base_commit_id, update_progress, &did_something);
5972 if (error)
5973 goto done;
5974 printf("Histedit of %s aborted\n",
5975 got_ref_get_symref_target(branch));
5976 goto done; /* nothing else to do */
5977 } else if (abort_edit) {
5978 error = got_error(GOT_ERR_NOT_HISTEDIT);
5979 goto done;
5982 if (continue_edit) {
5983 char *path;
5985 if (!edit_in_progress) {
5986 error = got_error(GOT_ERR_NOT_HISTEDIT);
5987 goto done;
5990 error = got_worktree_get_histedit_script_path(&path, worktree);
5991 if (error)
5992 goto done;
5994 error = histedit_load_list(&histedit_cmds, path, repo);
5995 free(path);
5996 if (error)
5997 goto done;
5999 error = got_worktree_histedit_continue(&resume_commit_id,
6000 &tmp_branch, &branch, &base_commit_id, &fileindex,
6001 worktree, repo);
6002 if (error)
6003 goto done;
6005 error = got_ref_resolve(&head_commit_id, repo, branch);
6006 if (error)
6007 goto done;
6009 error = got_object_open_as_commit(&commit, repo,
6010 head_commit_id);
6011 if (error)
6012 goto done;
6013 parent_ids = got_object_commit_get_parent_ids(commit);
6014 pid = SIMPLEQ_FIRST(parent_ids);
6015 if (pid == NULL) {
6016 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6017 goto done;
6019 error = collect_commits(&commits, head_commit_id, pid->id,
6020 base_commit_id, got_worktree_get_path_prefix(worktree),
6021 GOT_ERR_HISTEDIT_PATH, repo);
6022 got_object_commit_close(commit);
6023 commit = NULL;
6024 if (error)
6025 goto done;
6026 } else {
6027 if (edit_in_progress) {
6028 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6029 goto done;
6032 error = got_ref_open(&branch, repo,
6033 got_worktree_get_head_ref_name(worktree), 0);
6034 if (error != NULL)
6035 goto done;
6037 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6038 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6039 "will not edit commit history of a branch outside "
6040 "the \"refs/heads/\" reference namespace");
6041 goto done;
6044 error = got_ref_resolve(&head_commit_id, repo, branch);
6045 got_ref_close(branch);
6046 branch = NULL;
6047 if (error)
6048 goto done;
6050 error = got_object_open_as_commit(&commit, repo,
6051 head_commit_id);
6052 if (error)
6053 goto done;
6054 parent_ids = got_object_commit_get_parent_ids(commit);
6055 pid = SIMPLEQ_FIRST(parent_ids);
6056 if (pid == NULL) {
6057 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6058 goto done;
6060 error = collect_commits(&commits, head_commit_id, pid->id,
6061 got_worktree_get_base_commit_id(worktree),
6062 got_worktree_get_path_prefix(worktree),
6063 GOT_ERR_HISTEDIT_PATH, repo);
6064 got_object_commit_close(commit);
6065 commit = NULL;
6066 if (error)
6067 goto done;
6069 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6070 &base_commit_id, &fileindex, worktree, repo);
6071 if (error)
6072 goto done;
6074 if (edit_script_path) {
6075 error = histedit_load_list(&histedit_cmds,
6076 edit_script_path, repo);
6077 if (error) {
6078 got_worktree_histedit_abort(worktree, fileindex,
6079 repo, branch, base_commit_id,
6080 update_progress, &did_something);
6081 goto done;
6083 } else {
6084 error = histedit_edit_script(&histedit_cmds, &commits,
6085 repo);
6086 if (error) {
6087 got_worktree_histedit_abort(worktree, fileindex,
6088 repo, branch, base_commit_id,
6089 update_progress, &did_something);
6090 goto done;
6095 error = histedit_save_list(&histedit_cmds, worktree,
6096 repo);
6097 if (error) {
6098 got_worktree_histedit_abort(worktree, fileindex,
6099 repo, branch, base_commit_id,
6100 update_progress, &did_something);
6101 goto done;
6106 error = histedit_check_script(&histedit_cmds, &commits, repo);
6107 if (error)
6108 goto done;
6110 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6111 if (resume_commit_id) {
6112 if (got_object_id_cmp(hle->commit_id,
6113 resume_commit_id) != 0)
6114 continue;
6116 resume_commit_id = NULL;
6117 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6118 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6119 error = histedit_skip_commit(hle, worktree,
6120 repo);
6121 } else {
6122 error = histedit_commit(NULL, worktree,
6123 fileindex, tmp_branch, hle, repo);
6125 if (error)
6126 goto done;
6127 continue;
6130 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6131 error = histedit_skip_commit(hle, worktree, repo);
6132 if (error)
6133 goto done;
6134 continue;
6137 error = got_object_open_as_commit(&commit, repo,
6138 hle->commit_id);
6139 if (error)
6140 goto done;
6141 parent_ids = got_object_commit_get_parent_ids(commit);
6142 pid = SIMPLEQ_FIRST(parent_ids);
6144 error = got_worktree_histedit_merge_files(&merged_paths,
6145 worktree, fileindex, pid->id, hle->commit_id, repo,
6146 rebase_progress, &rebase_status, check_cancelled, NULL);
6147 if (error)
6148 goto done;
6149 got_object_commit_close(commit);
6150 commit = NULL;
6152 if (rebase_status == GOT_STATUS_CONFLICT) {
6153 got_worktree_rebase_pathlist_free(&merged_paths);
6154 break;
6157 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6158 char *id_str;
6159 error = got_object_id_str(&id_str, hle->commit_id);
6160 if (error)
6161 goto done;
6162 printf("Stopping histedit for amending commit %s\n",
6163 id_str);
6164 free(id_str);
6165 got_worktree_rebase_pathlist_free(&merged_paths);
6166 error = got_worktree_histedit_postpone(worktree,
6167 fileindex);
6168 goto done;
6171 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6172 error = histedit_skip_commit(hle, worktree, repo);
6173 if (error)
6174 goto done;
6175 continue;
6178 error = histedit_commit(&merged_paths, worktree, fileindex,
6179 tmp_branch, hle, repo);
6180 got_worktree_rebase_pathlist_free(&merged_paths);
6181 if (error)
6182 goto done;
6185 if (rebase_status == GOT_STATUS_CONFLICT) {
6186 error = got_worktree_histedit_postpone(worktree, fileindex);
6187 if (error)
6188 goto done;
6189 error = got_error_msg(GOT_ERR_CONFLICTS,
6190 "conflicts must be resolved before rebasing can continue");
6191 } else
6192 error = histedit_complete(worktree, fileindex, tmp_branch,
6193 branch, repo);
6194 done:
6195 got_object_id_queue_free(&commits);
6196 histedit_free_list(&histedit_cmds);
6197 free(head_commit_id);
6198 free(base_commit_id);
6199 free(resume_commit_id);
6200 if (commit)
6201 got_object_commit_close(commit);
6202 if (branch)
6203 got_ref_close(branch);
6204 if (tmp_branch)
6205 got_ref_close(tmp_branch);
6206 if (worktree)
6207 got_worktree_close(worktree);
6208 if (repo)
6209 got_repo_close(repo);
6210 return error;
6213 __dead static void
6214 usage_stage(void)
6216 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6217 "[file-path ...]\n",
6218 getprogname());
6219 exit(1);
6222 static const struct got_error *
6223 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6224 const char *path, struct got_object_id *blob_id,
6225 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6227 const struct got_error *err = NULL;
6228 char *id_str = NULL;
6230 if (staged_status != GOT_STATUS_ADD &&
6231 staged_status != GOT_STATUS_MODIFY &&
6232 staged_status != GOT_STATUS_DELETE)
6233 return NULL;
6235 if (staged_status == GOT_STATUS_ADD ||
6236 staged_status == GOT_STATUS_MODIFY)
6237 err = got_object_id_str(&id_str, staged_blob_id);
6238 else
6239 err = got_object_id_str(&id_str, blob_id);
6240 if (err)
6241 return err;
6243 printf("%s %c %s\n", id_str, staged_status, path);
6244 free(id_str);
6245 return NULL;
6248 static const struct got_error *
6249 cmd_stage(int argc, char *argv[])
6251 const struct got_error *error = NULL;
6252 struct got_repository *repo = NULL;
6253 struct got_worktree *worktree = NULL;
6254 char *cwd = NULL;
6255 struct got_pathlist_head paths;
6256 struct got_pathlist_entry *pe;
6257 int ch, list_stage = 0, pflag = 0;
6258 FILE *patch_script_file = NULL;
6259 const char *patch_script_path = NULL;
6260 struct choose_patch_arg cpa;
6262 TAILQ_INIT(&paths);
6264 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6265 switch (ch) {
6266 case 'l':
6267 list_stage = 1;
6268 break;
6269 case 'p':
6270 pflag = 1;
6271 break;
6272 case 'F':
6273 patch_script_path = optarg;
6274 break;
6275 default:
6276 usage_stage();
6277 /* NOTREACHED */
6281 argc -= optind;
6282 argv += optind;
6284 #ifndef PROFILE
6285 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6286 "unveil", NULL) == -1)
6287 err(1, "pledge");
6288 #endif
6289 if (list_stage && (pflag || patch_script_path))
6290 errx(1, "-l option cannot be used with other options");
6291 if (patch_script_path && !pflag)
6292 errx(1, "-F option can only be used together with -p option");
6294 cwd = getcwd(NULL, 0);
6295 if (cwd == NULL) {
6296 error = got_error_from_errno("getcwd");
6297 goto done;
6300 error = got_worktree_open(&worktree, cwd);
6301 if (error)
6302 goto done;
6304 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6305 if (error != NULL)
6306 goto done;
6308 if (patch_script_path) {
6309 patch_script_file = fopen(patch_script_path, "r");
6310 if (patch_script_file == NULL) {
6311 error = got_error_from_errno2("fopen",
6312 patch_script_path);
6313 goto done;
6316 error = apply_unveil(got_repo_get_path(repo), 1,
6317 got_worktree_get_root_path(worktree));
6318 if (error)
6319 goto done;
6321 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6322 if (error)
6323 goto done;
6325 if (list_stage)
6326 error = got_worktree_status(worktree, &paths, repo,
6327 print_stage, NULL, check_cancelled, NULL);
6328 else {
6329 cpa.patch_script_file = patch_script_file;
6330 cpa.action = "stage";
6331 error = got_worktree_stage(worktree, &paths,
6332 pflag ? NULL : print_status, NULL,
6333 pflag ? choose_patch : NULL, &cpa, repo);
6335 done:
6336 if (patch_script_file && fclose(patch_script_file) == EOF &&
6337 error == NULL)
6338 error = got_error_from_errno2("fclose", patch_script_path);
6339 if (repo)
6340 got_repo_close(repo);
6341 if (worktree)
6342 got_worktree_close(worktree);
6343 TAILQ_FOREACH(pe, &paths, entry)
6344 free((char *)pe->path);
6345 got_pathlist_free(&paths);
6346 free(cwd);
6347 return error;
6350 __dead static void
6351 usage_unstage(void)
6353 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6354 "[file-path ...]\n",
6355 getprogname());
6356 exit(1);
6360 static const struct got_error *
6361 cmd_unstage(int argc, char *argv[])
6363 const struct got_error *error = NULL;
6364 struct got_repository *repo = NULL;
6365 struct got_worktree *worktree = NULL;
6366 char *cwd = NULL;
6367 struct got_pathlist_head paths;
6368 struct got_pathlist_entry *pe;
6369 int ch, did_something = 0, pflag = 0;
6370 FILE *patch_script_file = NULL;
6371 const char *patch_script_path = NULL;
6372 struct choose_patch_arg cpa;
6374 TAILQ_INIT(&paths);
6376 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6377 switch (ch) {
6378 case 'p':
6379 pflag = 1;
6380 break;
6381 case 'F':
6382 patch_script_path = optarg;
6383 break;
6384 default:
6385 usage_unstage();
6386 /* NOTREACHED */
6390 argc -= optind;
6391 argv += optind;
6393 #ifndef PROFILE
6394 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6395 "unveil", NULL) == -1)
6396 err(1, "pledge");
6397 #endif
6398 if (patch_script_path && !pflag)
6399 errx(1, "-F option can only be used together with -p option");
6401 cwd = getcwd(NULL, 0);
6402 if (cwd == NULL) {
6403 error = got_error_from_errno("getcwd");
6404 goto done;
6407 error = got_worktree_open(&worktree, cwd);
6408 if (error)
6409 goto done;
6411 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6412 if (error != NULL)
6413 goto done;
6415 if (patch_script_path) {
6416 patch_script_file = fopen(patch_script_path, "r");
6417 if (patch_script_file == NULL) {
6418 error = got_error_from_errno2("fopen",
6419 patch_script_path);
6420 goto done;
6424 error = apply_unveil(got_repo_get_path(repo), 1,
6425 got_worktree_get_root_path(worktree));
6426 if (error)
6427 goto done;
6429 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6430 if (error)
6431 goto done;
6433 cpa.patch_script_file = patch_script_file;
6434 cpa.action = "unstage";
6435 error = got_worktree_unstage(worktree, &paths, update_progress,
6436 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6437 done:
6438 if (patch_script_file && fclose(patch_script_file) == EOF &&
6439 error == NULL)
6440 error = got_error_from_errno2("fclose", patch_script_path);
6441 if (repo)
6442 got_repo_close(repo);
6443 if (worktree)
6444 got_worktree_close(worktree);
6445 TAILQ_FOREACH(pe, &paths, entry)
6446 free((char *)pe->path);
6447 got_pathlist_free(&paths);
6448 free(cwd);
6449 return error;
6452 __dead static void
6453 usage_cat(void)
6455 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
6456 "[object2 ...]\n", getprogname());
6457 exit(1);
6460 static const struct got_error *
6461 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6463 const struct got_error *err;
6464 struct got_blob_object *blob;
6466 err = got_object_open_as_blob(&blob, repo, id, 8192);
6467 if (err)
6468 return err;
6470 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6471 got_object_blob_close(blob);
6472 return err;
6475 static const struct got_error *
6476 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6478 const struct got_error *err;
6479 struct got_tree_object *tree;
6480 const struct got_tree_entries *entries;
6481 struct got_tree_entry *te;
6483 err = got_object_open_as_tree(&tree, repo, id);
6484 if (err)
6485 return err;
6487 entries = got_object_tree_get_entries(tree);
6488 te = SIMPLEQ_FIRST(&entries->head);
6489 while (te) {
6490 char *id_str;
6491 if (sigint_received || sigpipe_received)
6492 break;
6493 err = got_object_id_str(&id_str, te->id);
6494 if (err)
6495 break;
6496 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6497 free(id_str);
6498 te = SIMPLEQ_NEXT(te, entry);
6501 got_object_tree_close(tree);
6502 return err;
6505 static const struct got_error *
6506 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6508 const struct got_error *err;
6509 struct got_commit_object *commit;
6510 const struct got_object_id_queue *parent_ids;
6511 struct got_object_qid *pid;
6512 char *id_str = NULL;
6513 const char *logmsg = NULL;
6515 err = got_object_open_as_commit(&commit, repo, id);
6516 if (err)
6517 return err;
6519 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6520 if (err)
6521 goto done;
6523 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6524 parent_ids = got_object_commit_get_parent_ids(commit);
6525 fprintf(outfile, "numparents %d\n",
6526 got_object_commit_get_nparents(commit));
6527 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6528 char *pid_str;
6529 err = got_object_id_str(&pid_str, pid->id);
6530 if (err)
6531 goto done;
6532 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6533 free(pid_str);
6535 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6536 got_object_commit_get_author(commit),
6537 got_object_commit_get_author_time(commit));
6539 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6540 got_object_commit_get_author(commit),
6541 got_object_commit_get_committer_time(commit));
6543 logmsg = got_object_commit_get_logmsg_raw(commit);
6544 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6545 fprintf(outfile, "%s", logmsg);
6546 done:
6547 free(id_str);
6548 got_object_commit_close(commit);
6549 return err;
6552 static const struct got_error *
6553 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6555 const struct got_error *err;
6556 struct got_tag_object *tag;
6557 char *id_str = NULL;
6558 const char *tagmsg = NULL;
6560 err = got_object_open_as_tag(&tag, repo, id);
6561 if (err)
6562 return err;
6564 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6565 if (err)
6566 goto done;
6568 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6570 switch (got_object_tag_get_object_type(tag)) {
6571 case GOT_OBJ_TYPE_BLOB:
6572 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6573 GOT_OBJ_LABEL_BLOB);
6574 break;
6575 case GOT_OBJ_TYPE_TREE:
6576 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6577 GOT_OBJ_LABEL_TREE);
6578 break;
6579 case GOT_OBJ_TYPE_COMMIT:
6580 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6581 GOT_OBJ_LABEL_COMMIT);
6582 break;
6583 case GOT_OBJ_TYPE_TAG:
6584 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6585 GOT_OBJ_LABEL_TAG);
6586 break;
6587 default:
6588 break;
6591 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6592 got_object_tag_get_name(tag));
6594 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6595 got_object_tag_get_tagger(tag),
6596 got_object_tag_get_tagger_time(tag));
6598 tagmsg = got_object_tag_get_message(tag);
6599 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6600 fprintf(outfile, "%s", tagmsg);
6601 done:
6602 free(id_str);
6603 got_object_tag_close(tag);
6604 return err;
6607 static const struct got_error *
6608 cmd_cat(int argc, char *argv[])
6610 const struct got_error *error;
6611 struct got_repository *repo = NULL;
6612 struct got_worktree *worktree = NULL;
6613 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6614 struct got_object_id *id = NULL;
6615 int ch, obj_type, i;
6617 #ifndef PROFILE
6618 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6619 NULL) == -1)
6620 err(1, "pledge");
6621 #endif
6623 while ((ch = getopt(argc, argv, "r:")) != -1) {
6624 switch (ch) {
6625 case 'r':
6626 repo_path = realpath(optarg, NULL);
6627 if (repo_path == NULL)
6628 err(1, "-r option");
6629 got_path_strip_trailing_slashes(repo_path);
6630 break;
6631 default:
6632 usage_cat();
6633 /* NOTREACHED */
6637 argc -= optind;
6638 argv += optind;
6640 cwd = getcwd(NULL, 0);
6641 if (cwd == NULL) {
6642 error = got_error_from_errno("getcwd");
6643 goto done;
6645 error = got_worktree_open(&worktree, cwd);
6646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6647 goto done;
6648 if (worktree) {
6649 if (repo_path == NULL) {
6650 repo_path = strdup(
6651 got_worktree_get_repo_path(worktree));
6652 if (repo_path == NULL) {
6653 error = got_error_from_errno("strdup");
6654 goto done;
6659 if (repo_path == NULL) {
6660 repo_path = getcwd(NULL, 0);
6661 if (repo_path == NULL)
6662 return got_error_from_errno("getcwd");
6665 error = got_repo_open(&repo, repo_path);
6666 free(repo_path);
6667 if (error != NULL)
6668 goto done;
6670 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6671 if (error)
6672 goto done;
6674 for (i = 0; i < argc; i++) {
6675 error = match_object_id(&id, &label, argv[i],
6676 GOT_OBJ_TYPE_ANY, 0, repo);
6677 if (error)
6678 break;
6680 error = got_object_get_type(&obj_type, repo, id);
6681 if (error)
6682 break;
6684 switch (obj_type) {
6685 case GOT_OBJ_TYPE_BLOB:
6686 error = cat_blob(id, repo, stdout);
6687 break;
6688 case GOT_OBJ_TYPE_TREE:
6689 error = cat_tree(id, repo, stdout);
6690 break;
6691 case GOT_OBJ_TYPE_COMMIT:
6692 error = cat_commit(id, repo, stdout);
6693 break;
6694 case GOT_OBJ_TYPE_TAG:
6695 error = cat_tag(id, repo, stdout);
6696 break;
6697 default:
6698 error = got_error(GOT_ERR_OBJ_TYPE);
6699 break;
6701 if (error)
6702 break;
6703 free(label);
6704 label = NULL;
6705 free(id);
6706 id = NULL;
6709 done:
6710 free(label);
6711 free(id);
6712 if (worktree)
6713 got_worktree_close(worktree);
6714 if (repo) {
6715 const struct got_error *repo_error;
6716 repo_error = got_repo_close(repo);
6717 if (error == NULL)
6718 error = repo_error;
6720 return error;