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 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1321 const char *path, int diff_context, struct got_repository *repo)
1323 const struct got_error *err = NULL;
1324 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1326 if (blob_id1) {
1327 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1328 if (err)
1329 goto done;
1332 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1333 if (err)
1334 goto done;
1336 while (path[0] == '/')
1337 path++;
1338 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1339 stdout);
1340 done:
1341 if (blob1)
1342 got_object_blob_close(blob1);
1343 got_object_blob_close(blob2);
1344 return err;
1347 static const struct got_error *
1348 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1349 const char *path, int diff_context, struct got_repository *repo)
1351 const struct got_error *err = NULL;
1352 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1353 struct got_diff_blob_output_unidiff_arg arg;
1355 if (tree_id1) {
1356 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1357 if (err)
1358 goto done;
1361 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1362 if (err)
1363 goto done;
1365 arg.diff_context = diff_context;
1366 arg.outfile = stdout;
1367 while (path[0] == '/')
1368 path++;
1369 err = got_diff_tree(tree1, tree2, path, path, repo,
1370 got_diff_blob_output_unidiff, &arg, 1);
1371 done:
1372 if (tree1)
1373 got_object_tree_close(tree1);
1374 got_object_tree_close(tree2);
1375 return err;
1378 static const struct got_error *
1379 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1380 const char *path, int diff_context, struct got_repository *repo)
1382 const struct got_error *err = NULL;
1383 struct got_commit_object *pcommit = NULL;
1384 char *id_str1 = NULL, *id_str2 = NULL;
1385 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1386 struct got_object_qid *qid;
1388 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1389 if (qid != NULL) {
1390 err = got_object_open_as_commit(&pcommit, repo,
1391 qid->id);
1392 if (err)
1393 return err;
1396 if (path && path[0] != '\0') {
1397 int obj_type;
1398 err = got_object_id_by_path(&obj_id2, repo, id, path);
1399 if (err)
1400 goto done;
1401 err = got_object_id_str(&id_str2, obj_id2);
1402 if (err) {
1403 free(obj_id2);
1404 goto done;
1406 if (pcommit) {
1407 err = got_object_id_by_path(&obj_id1, repo,
1408 qid->id, path);
1409 if (err) {
1410 free(obj_id2);
1411 goto done;
1413 err = got_object_id_str(&id_str1, obj_id1);
1414 if (err) {
1415 free(obj_id2);
1416 goto done;
1419 err = got_object_get_type(&obj_type, repo, obj_id2);
1420 if (err) {
1421 free(obj_id2);
1422 goto done;
1424 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1425 switch (obj_type) {
1426 case GOT_OBJ_TYPE_BLOB:
1427 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1428 repo);
1429 break;
1430 case GOT_OBJ_TYPE_TREE:
1431 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1432 repo);
1433 break;
1434 default:
1435 err = got_error(GOT_ERR_OBJ_TYPE);
1436 break;
1438 free(obj_id1);
1439 free(obj_id2);
1440 } else {
1441 obj_id2 = got_object_commit_get_tree_id(commit);
1442 err = got_object_id_str(&id_str2, obj_id2);
1443 if (err)
1444 goto done;
1445 obj_id1 = got_object_commit_get_tree_id(pcommit);
1446 err = got_object_id_str(&id_str1, obj_id1);
1447 if (err)
1448 goto done;
1449 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1450 err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1453 done:
1454 free(id_str1);
1455 free(id_str2);
1456 if (pcommit)
1457 got_object_commit_close(pcommit);
1458 return err;
1461 static char *
1462 get_datestr(time_t *time, char *datebuf)
1464 struct tm mytm, *tm;
1465 char *p, *s;
1467 tm = gmtime_r(time, &mytm);
1468 if (tm == NULL)
1469 return NULL;
1470 s = asctime_r(tm, datebuf);
1471 if (s == NULL)
1472 return NULL;
1473 p = strchr(s, '\n');
1474 if (p)
1475 *p = '\0';
1476 return s;
1479 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1481 static const struct got_error *
1482 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1483 struct got_repository *repo, const char *path, int show_patch,
1484 int diff_context, struct got_reflist_head *refs)
1486 const struct got_error *err = NULL;
1487 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1488 char datebuf[26];
1489 time_t committer_time;
1490 const char *author, *committer;
1491 char *refs_str = NULL;
1492 struct got_reflist_entry *re;
1494 SIMPLEQ_FOREACH(re, refs, entry) {
1495 char *s;
1496 const char *name;
1497 struct got_tag_object *tag = NULL;
1498 int cmp;
1500 name = got_ref_get_name(re->ref);
1501 if (strcmp(name, GOT_REF_HEAD) == 0)
1502 continue;
1503 if (strncmp(name, "refs/", 5) == 0)
1504 name += 5;
1505 if (strncmp(name, "got/", 4) == 0)
1506 continue;
1507 if (strncmp(name, "heads/", 6) == 0)
1508 name += 6;
1509 if (strncmp(name, "remotes/", 8) == 0)
1510 name += 8;
1511 if (strncmp(name, "tags/", 5) == 0) {
1512 err = got_object_open_as_tag(&tag, repo, re->id);
1513 if (err) {
1514 if (err->code != GOT_ERR_OBJ_TYPE)
1515 return err;
1516 /* Ref points at something other than a tag. */
1517 err = NULL;
1518 tag = NULL;
1521 cmp = got_object_id_cmp(tag ?
1522 got_object_tag_get_object_id(tag) : re->id, id);
1523 if (tag)
1524 got_object_tag_close(tag);
1525 if (cmp != 0)
1526 continue;
1527 s = refs_str;
1528 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1529 name) == -1) {
1530 err = got_error_from_errno("asprintf");
1531 free(s);
1532 return err;
1534 free(s);
1536 err = got_object_id_str(&id_str, id);
1537 if (err)
1538 return err;
1540 printf(GOT_COMMIT_SEP_STR);
1541 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1542 refs_str ? refs_str : "", refs_str ? ")" : "");
1543 free(id_str);
1544 id_str = NULL;
1545 free(refs_str);
1546 refs_str = NULL;
1547 printf("from: %s\n", got_object_commit_get_author(commit));
1548 committer_time = got_object_commit_get_committer_time(commit);
1549 datestr = get_datestr(&committer_time, datebuf);
1550 if (datestr)
1551 printf("date: %s UTC\n", datestr);
1552 author = got_object_commit_get_author(commit);
1553 committer = got_object_commit_get_committer(commit);
1554 if (strcmp(author, committer) != 0)
1555 printf("via: %s\n", committer);
1556 if (got_object_commit_get_nparents(commit) > 1) {
1557 const struct got_object_id_queue *parent_ids;
1558 struct got_object_qid *qid;
1559 int n = 1;
1560 parent_ids = got_object_commit_get_parent_ids(commit);
1561 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1562 err = got_object_id_str(&id_str, qid->id);
1563 if (err)
1564 return err;
1565 printf("parent %d: %s\n", n++, id_str);
1566 free(id_str);
1570 err = got_object_commit_get_logmsg(&logmsg0, commit);
1571 if (err)
1572 return err;
1574 logmsg = logmsg0;
1575 do {
1576 line = strsep(&logmsg, "\n");
1577 if (line)
1578 printf(" %s\n", line);
1579 } while (line);
1580 free(logmsg0);
1582 if (show_patch) {
1583 err = print_patch(commit, id, path, diff_context, repo);
1584 if (err == 0)
1585 printf("\n");
1588 if (fflush(stdout) != 0 && err == NULL)
1589 err = got_error_from_errno("fflush");
1590 return err;
1593 static const struct got_error *
1594 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1595 char *path, int show_patch, int diff_context, int limit,
1596 int first_parent_traversal, struct got_reflist_head *refs)
1598 const struct got_error *err;
1599 struct got_commit_graph *graph;
1601 err = got_commit_graph_open(&graph, root_id, path,
1602 first_parent_traversal, repo);
1603 if (err)
1604 return err;
1605 err = got_commit_graph_iter_start(graph, root_id, repo,
1606 check_cancelled, NULL);
1607 if (err)
1608 goto done;
1609 for (;;) {
1610 struct got_commit_object *commit;
1611 struct got_object_id *id;
1613 if (sigint_received || sigpipe_received)
1614 break;
1616 err = got_commit_graph_iter_next(&id, graph);
1617 if (err) {
1618 if (err->code == GOT_ERR_ITER_COMPLETED) {
1619 err = NULL;
1620 break;
1622 if (err->code != GOT_ERR_ITER_NEED_MORE)
1623 break;
1624 err = got_commit_graph_fetch_commits(graph, 1, repo,
1625 check_cancelled, NULL);
1626 if (err)
1627 break;
1628 else
1629 continue;
1631 if (id == NULL)
1632 break;
1634 err = got_object_open_as_commit(&commit, repo, id);
1635 if (err)
1636 break;
1637 err = print_commit(commit, id, repo, path, show_patch,
1638 diff_context, refs);
1639 got_object_commit_close(commit);
1640 if (err || (limit && --limit == 0))
1641 break;
1643 done:
1644 got_commit_graph_close(graph);
1645 return err;
1648 __dead static void
1649 usage_log(void)
1651 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1652 "[-r repository-path] [path]\n", getprogname());
1653 exit(1);
1656 static int
1657 get_default_log_limit(void)
1659 const char *got_default_log_limit;
1660 long long n;
1661 const char *errstr;
1663 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1664 if (got_default_log_limit == NULL)
1665 return 0;
1666 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1667 if (errstr != NULL)
1668 return 0;
1669 return n;
1672 static const struct got_error *
1673 cmd_log(int argc, char *argv[])
1675 const struct got_error *error;
1676 struct got_repository *repo = NULL;
1677 struct got_worktree *worktree = NULL;
1678 struct got_commit_object *commit = NULL;
1679 struct got_object_id *id = NULL;
1680 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1681 char *start_commit = NULL;
1682 int diff_context = 3, ch;
1683 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1684 const char *errstr;
1685 struct got_reflist_head refs;
1687 SIMPLEQ_INIT(&refs);
1689 #ifndef PROFILE
1690 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1691 NULL)
1692 == -1)
1693 err(1, "pledge");
1694 #endif
1696 limit = get_default_log_limit();
1698 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1699 switch (ch) {
1700 case 'p':
1701 show_patch = 1;
1702 break;
1703 case 'c':
1704 start_commit = optarg;
1705 break;
1706 case 'C':
1707 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1708 &errstr);
1709 if (errstr != NULL)
1710 err(1, "-C option %s", errstr);
1711 break;
1712 case 'l':
1713 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1714 if (errstr != NULL)
1715 err(1, "-l option %s", errstr);
1716 break;
1717 case 'f':
1718 first_parent_traversal = 1;
1719 break;
1720 case 'r':
1721 repo_path = realpath(optarg, NULL);
1722 if (repo_path == NULL)
1723 err(1, "-r option");
1724 got_path_strip_trailing_slashes(repo_path);
1725 break;
1726 default:
1727 usage_log();
1728 /* NOTREACHED */
1732 argc -= optind;
1733 argv += optind;
1735 cwd = getcwd(NULL, 0);
1736 if (cwd == NULL) {
1737 error = got_error_from_errno("getcwd");
1738 goto done;
1741 error = got_worktree_open(&worktree, cwd);
1742 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1743 goto done;
1744 error = NULL;
1746 if (argc == 0) {
1747 path = strdup("");
1748 if (path == NULL) {
1749 error = got_error_from_errno("strdup");
1750 goto done;
1752 } else if (argc == 1) {
1753 if (worktree) {
1754 error = got_worktree_resolve_path(&path, worktree,
1755 argv[0]);
1756 if (error)
1757 goto done;
1758 } else {
1759 path = strdup(argv[0]);
1760 if (path == NULL) {
1761 error = got_error_from_errno("strdup");
1762 goto done;
1765 } else
1766 usage_log();
1768 if (repo_path == NULL) {
1769 repo_path = worktree ?
1770 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1772 if (repo_path == NULL) {
1773 error = got_error_from_errno("strdup");
1774 goto done;
1777 error = got_repo_open(&repo, repo_path);
1778 if (error != NULL)
1779 goto done;
1781 error = apply_unveil(got_repo_get_path(repo), 1,
1782 worktree ? got_worktree_get_root_path(worktree) : NULL);
1783 if (error)
1784 goto done;
1786 if (start_commit == NULL) {
1787 struct got_reference *head_ref;
1788 error = got_ref_open(&head_ref, repo,
1789 worktree ? got_worktree_get_head_ref_name(worktree)
1790 : GOT_REF_HEAD, 0);
1791 if (error != NULL)
1792 return error;
1793 error = got_ref_resolve(&id, repo, head_ref);
1794 got_ref_close(head_ref);
1795 if (error != NULL)
1796 return error;
1797 error = got_object_open_as_commit(&commit, repo, id);
1798 } else {
1799 struct got_reference *ref;
1800 error = got_ref_open(&ref, repo, start_commit, 0);
1801 if (error == NULL) {
1802 int obj_type;
1803 error = got_ref_resolve(&id, repo, ref);
1804 got_ref_close(ref);
1805 if (error != NULL)
1806 goto done;
1807 error = got_object_get_type(&obj_type, repo, id);
1808 if (error != NULL)
1809 goto done;
1810 if (obj_type == GOT_OBJ_TYPE_TAG) {
1811 struct got_tag_object *tag;
1812 error = got_object_open_as_tag(&tag, repo, id);
1813 if (error != NULL)
1814 goto done;
1815 if (got_object_tag_get_object_type(tag) !=
1816 GOT_OBJ_TYPE_COMMIT) {
1817 got_object_tag_close(tag);
1818 error = got_error(GOT_ERR_OBJ_TYPE);
1819 goto done;
1821 free(id);
1822 id = got_object_id_dup(
1823 got_object_tag_get_object_id(tag));
1824 if (id == NULL)
1825 error = got_error_from_errno(
1826 "got_object_id_dup");
1827 got_object_tag_close(tag);
1828 if (error)
1829 goto done;
1830 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1831 error = got_error(GOT_ERR_OBJ_TYPE);
1832 goto done;
1834 error = got_object_open_as_commit(&commit, repo, id);
1835 if (error != NULL)
1836 goto done;
1838 if (commit == NULL) {
1839 error = got_repo_match_object_id_prefix(&id,
1840 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1841 if (error != NULL)
1842 return error;
1845 if (error != NULL)
1846 goto done;
1848 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1849 if (error != NULL)
1850 goto done;
1851 if (in_repo_path) {
1852 free(path);
1853 path = in_repo_path;
1856 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1857 if (error)
1858 goto done;
1860 error = print_commits(id, repo, path, show_patch,
1861 diff_context, limit, first_parent_traversal, &refs);
1862 done:
1863 free(path);
1864 free(repo_path);
1865 free(cwd);
1866 free(id);
1867 if (worktree)
1868 got_worktree_close(worktree);
1869 if (repo) {
1870 const struct got_error *repo_error;
1871 repo_error = got_repo_close(repo);
1872 if (error == NULL)
1873 error = repo_error;
1875 got_ref_list_free(&refs);
1876 return error;
1879 __dead static void
1880 usage_diff(void)
1882 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1883 "[object1 object2 | path]\n", getprogname());
1884 exit(1);
1887 struct print_diff_arg {
1888 struct got_repository *repo;
1889 struct got_worktree *worktree;
1890 int diff_context;
1891 const char *id_str;
1892 int header_shown;
1893 int diff_staged;
1896 static const struct got_error *
1897 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1898 const char *path, struct got_object_id *blob_id,
1899 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1901 struct print_diff_arg *a = arg;
1902 const struct got_error *err = NULL;
1903 struct got_blob_object *blob1 = NULL;
1904 FILE *f2 = NULL;
1905 char *abspath = NULL, *label1 = NULL;
1906 struct stat sb;
1908 if (a->diff_staged) {
1909 if (staged_status != GOT_STATUS_MODIFY &&
1910 staged_status != GOT_STATUS_ADD &&
1911 staged_status != GOT_STATUS_DELETE)
1912 return NULL;
1913 } else {
1914 if (staged_status == GOT_STATUS_DELETE)
1915 return NULL;
1916 if (status == GOT_STATUS_NONEXISTENT)
1917 return got_error_set_errno(ENOENT, path);
1918 if (status != GOT_STATUS_MODIFY &&
1919 status != GOT_STATUS_ADD &&
1920 status != GOT_STATUS_DELETE &&
1921 status != GOT_STATUS_CONFLICT)
1922 return NULL;
1925 if (!a->header_shown) {
1926 printf("diff %s %s%s\n", a->id_str,
1927 got_worktree_get_root_path(a->worktree),
1928 a->diff_staged ? " (staged changes)" : "");
1929 a->header_shown = 1;
1932 if (a->diff_staged) {
1933 const char *label1 = NULL, *label2 = NULL;
1934 switch (staged_status) {
1935 case GOT_STATUS_MODIFY:
1936 label1 = path;
1937 label2 = path;
1938 break;
1939 case GOT_STATUS_ADD:
1940 label2 = path;
1941 break;
1942 case GOT_STATUS_DELETE:
1943 label1 = path;
1944 break;
1945 default:
1946 return got_error(GOT_ERR_FILE_STATUS);
1948 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1949 label1, label2, a->diff_context, a->repo, stdout);
1952 if (staged_status == GOT_STATUS_ADD ||
1953 staged_status == GOT_STATUS_MODIFY) {
1954 char *id_str;
1955 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1956 8192);
1957 if (err)
1958 goto done;
1959 err = got_object_id_str(&id_str, staged_blob_id);
1960 if (err)
1961 goto done;
1962 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1963 err = got_error_from_errno("asprintf");
1964 free(id_str);
1965 goto done;
1967 free(id_str);
1968 } else if (status != GOT_STATUS_ADD) {
1969 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1970 if (err)
1971 goto done;
1974 if (status != GOT_STATUS_DELETE) {
1975 if (asprintf(&abspath, "%s/%s",
1976 got_worktree_get_root_path(a->worktree), path) == -1) {
1977 err = got_error_from_errno("asprintf");
1978 goto done;
1981 f2 = fopen(abspath, "r");
1982 if (f2 == NULL) {
1983 err = got_error_from_errno2("fopen", abspath);
1984 goto done;
1986 if (lstat(abspath, &sb) == -1) {
1987 err = got_error_from_errno2("lstat", abspath);
1988 goto done;
1990 } else
1991 sb.st_size = 0;
1993 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1994 a->diff_context, stdout);
1995 done:
1996 if (blob1)
1997 got_object_blob_close(blob1);
1998 if (f2 && fclose(f2) != 0 && err == NULL)
1999 err = got_error_from_errno("fclose");
2000 free(abspath);
2001 return err;
2004 static const struct got_error *
2005 match_object_id(struct got_object_id **id, char **label,
2006 const char *id_str, int obj_type, int resolve_tags,
2007 struct got_repository *repo)
2009 const struct got_error *err;
2010 struct got_tag_object *tag;
2011 struct got_reference *ref = NULL;
2013 *id = NULL;
2014 *label = NULL;
2016 if (resolve_tags) {
2017 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2018 repo);
2019 if (err == NULL) {
2020 *id = got_object_id_dup(
2021 got_object_tag_get_object_id(tag));
2022 if (*id == NULL)
2023 err = got_error_from_errno("got_object_id_dup");
2024 else if (asprintf(label, "refs/tags/%s",
2025 got_object_tag_get_name(tag)) == -1) {
2026 err = got_error_from_errno("asprintf");
2027 free(id);
2028 *id = NULL;
2030 got_object_tag_close(tag);
2031 return err;
2032 } else if (err->code != GOT_ERR_NO_OBJ)
2033 return err;
2036 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2037 if (err) {
2038 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2039 return err;
2040 err = got_ref_open(&ref, repo, id_str, 0);
2041 if (err != NULL)
2042 goto done;
2043 *label = strdup(got_ref_get_name(ref));
2044 if (*label == NULL) {
2045 err = got_error_from_errno("strdup");
2046 goto done;
2048 err = got_ref_resolve(id, repo, ref);
2049 } else {
2050 err = got_object_id_str(label, *id);
2051 if (*label == NULL) {
2052 err = got_error_from_errno("strdup");
2053 goto done;
2056 done:
2057 if (ref)
2058 got_ref_close(ref);
2059 return err;
2063 static const struct got_error *
2064 cmd_diff(int argc, char *argv[])
2066 const struct got_error *error;
2067 struct got_repository *repo = NULL;
2068 struct got_worktree *worktree = NULL;
2069 char *cwd = NULL, *repo_path = NULL;
2070 struct got_object_id *id1 = NULL, *id2 = NULL;
2071 const char *id_str1 = NULL, *id_str2 = NULL;
2072 char *label1 = NULL, *label2 = NULL;
2073 int type1, type2;
2074 int diff_context = 3, diff_staged = 0, ch;
2075 const char *errstr;
2076 char *path = NULL;
2078 #ifndef PROFILE
2079 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2080 NULL) == -1)
2081 err(1, "pledge");
2082 #endif
2084 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2085 switch (ch) {
2086 case 'C':
2087 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2088 if (errstr != NULL)
2089 err(1, "-C option %s", errstr);
2090 break;
2091 case 'r':
2092 repo_path = realpath(optarg, NULL);
2093 if (repo_path == NULL)
2094 err(1, "-r option");
2095 got_path_strip_trailing_slashes(repo_path);
2096 break;
2097 case 's':
2098 diff_staged = 1;
2099 break;
2100 default:
2101 usage_diff();
2102 /* NOTREACHED */
2106 argc -= optind;
2107 argv += optind;
2109 cwd = getcwd(NULL, 0);
2110 if (cwd == NULL) {
2111 error = got_error_from_errno("getcwd");
2112 goto done;
2114 error = got_worktree_open(&worktree, cwd);
2115 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2116 goto done;
2117 if (argc <= 1) {
2118 if (worktree == NULL) {
2119 error = got_error(GOT_ERR_NOT_WORKTREE);
2120 goto done;
2122 if (repo_path)
2123 errx(1,
2124 "-r option can't be used when diffing a work tree");
2125 repo_path = strdup(got_worktree_get_repo_path(worktree));
2126 if (repo_path == NULL) {
2127 error = got_error_from_errno("strdup");
2128 goto done;
2130 if (argc == 1) {
2131 error = got_worktree_resolve_path(&path, worktree,
2132 argv[0]);
2133 if (error)
2134 goto done;
2135 } else {
2136 path = strdup("");
2137 if (path == NULL) {
2138 error = got_error_from_errno("strdup");
2139 goto done;
2142 } else if (argc == 2) {
2143 if (diff_staged)
2144 errx(1, "-s option can't be used when diffing "
2145 "objects in repository");
2146 id_str1 = argv[0];
2147 id_str2 = argv[1];
2148 if (worktree && repo_path == NULL) {
2149 repo_path =
2150 strdup(got_worktree_get_repo_path(worktree));
2151 if (repo_path == NULL) {
2152 error = got_error_from_errno("strdup");
2153 goto done;
2156 } else
2157 usage_diff();
2159 if (repo_path == NULL) {
2160 repo_path = getcwd(NULL, 0);
2161 if (repo_path == NULL)
2162 return got_error_from_errno("getcwd");
2165 error = got_repo_open(&repo, repo_path);
2166 free(repo_path);
2167 if (error != NULL)
2168 goto done;
2170 error = apply_unveil(got_repo_get_path(repo), 1,
2171 worktree ? got_worktree_get_root_path(worktree) : NULL);
2172 if (error)
2173 goto done;
2175 if (argc <= 1) {
2176 struct print_diff_arg arg;
2177 struct got_pathlist_head paths;
2178 char *id_str;
2180 TAILQ_INIT(&paths);
2182 error = got_object_id_str(&id_str,
2183 got_worktree_get_base_commit_id(worktree));
2184 if (error)
2185 goto done;
2186 arg.repo = repo;
2187 arg.worktree = worktree;
2188 arg.diff_context = diff_context;
2189 arg.id_str = id_str;
2190 arg.header_shown = 0;
2191 arg.diff_staged = diff_staged;
2193 error = got_pathlist_append(&paths, path, NULL);
2194 if (error)
2195 goto done;
2197 error = got_worktree_status(worktree, &paths, repo, print_diff,
2198 &arg, check_cancelled, NULL);
2199 free(id_str);
2200 got_pathlist_free(&paths);
2201 goto done;
2204 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2205 repo);
2206 if (error)
2207 goto done;
2209 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2210 repo);
2211 if (error)
2212 goto done;
2214 error = got_object_get_type(&type1, repo, id1);
2215 if (error)
2216 goto done;
2218 error = got_object_get_type(&type2, repo, id2);
2219 if (error)
2220 goto done;
2222 if (type1 != type2) {
2223 error = got_error(GOT_ERR_OBJ_TYPE);
2224 goto done;
2227 switch (type1) {
2228 case GOT_OBJ_TYPE_BLOB:
2229 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2230 diff_context, repo, stdout);
2231 break;
2232 case GOT_OBJ_TYPE_TREE:
2233 error = got_diff_objects_as_trees(id1, id2, "", "",
2234 diff_context, repo, stdout);
2235 break;
2236 case GOT_OBJ_TYPE_COMMIT:
2237 printf("diff %s %s\n", label1, label2);
2238 error = got_diff_objects_as_commits(id1, id2, diff_context,
2239 repo, stdout);
2240 break;
2241 default:
2242 error = got_error(GOT_ERR_OBJ_TYPE);
2245 done:
2246 free(label1);
2247 free(label2);
2248 free(id1);
2249 free(id2);
2250 free(path);
2251 if (worktree)
2252 got_worktree_close(worktree);
2253 if (repo) {
2254 const struct got_error *repo_error;
2255 repo_error = got_repo_close(repo);
2256 if (error == NULL)
2257 error = repo_error;
2259 return error;
2262 __dead static void
2263 usage_blame(void)
2265 fprintf(stderr,
2266 "usage: %s blame [-c commit] [-r repository-path] path\n",
2267 getprogname());
2268 exit(1);
2271 struct blame_line {
2272 int annotated;
2273 char *id_str;
2274 char *committer;
2275 char datebuf[9]; /* YY-MM-DD + NUL */
2278 struct blame_cb_args {
2279 struct blame_line *lines;
2280 int nlines;
2281 int nlines_prec;
2282 int lineno_cur;
2283 off_t *line_offsets;
2284 FILE *f;
2285 struct got_repository *repo;
2288 static const struct got_error *
2289 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2291 const struct got_error *err = NULL;
2292 struct blame_cb_args *a = arg;
2293 struct blame_line *bline;
2294 char *line = NULL;
2295 size_t linesize = 0;
2296 struct got_commit_object *commit = NULL;
2297 off_t offset;
2298 struct tm tm;
2299 time_t committer_time;
2301 if (nlines != a->nlines ||
2302 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2303 return got_error(GOT_ERR_RANGE);
2305 if (sigint_received)
2306 return got_error(GOT_ERR_ITER_COMPLETED);
2308 if (lineno == -1)
2309 return NULL; /* no change in this commit */
2311 /* Annotate this line. */
2312 bline = &a->lines[lineno - 1];
2313 if (bline->annotated)
2314 return NULL;
2315 err = got_object_id_str(&bline->id_str, id);
2316 if (err)
2317 return err;
2319 err = got_object_open_as_commit(&commit, a->repo, id);
2320 if (err)
2321 goto done;
2323 bline->committer = strdup(got_object_commit_get_committer(commit));
2324 if (bline->committer == NULL) {
2325 err = got_error_from_errno("strdup");
2326 goto done;
2329 committer_time = got_object_commit_get_committer_time(commit);
2330 if (localtime_r(&committer_time, &tm) == NULL)
2331 return got_error_from_errno("localtime_r");
2332 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2333 &tm) >= sizeof(bline->datebuf)) {
2334 err = got_error(GOT_ERR_NO_SPACE);
2335 goto done;
2337 bline->annotated = 1;
2339 /* Print lines annotated so far. */
2340 bline = &a->lines[a->lineno_cur - 1];
2341 if (!bline->annotated)
2342 goto done;
2344 offset = a->line_offsets[a->lineno_cur - 1];
2345 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2346 err = got_error_from_errno("fseeko");
2347 goto done;
2350 while (bline->annotated) {
2351 char *smallerthan, *at, *nl, *committer;
2352 size_t len;
2354 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2355 if (ferror(a->f))
2356 err = got_error_from_errno("getline");
2357 break;
2360 committer = bline->committer;
2361 smallerthan = strchr(committer, '<');
2362 if (smallerthan && smallerthan[1] != '\0')
2363 committer = smallerthan + 1;
2364 at = strchr(committer, '@');
2365 if (at)
2366 *at = '\0';
2367 len = strlen(committer);
2368 if (len >= 9)
2369 committer[8] = '\0';
2371 nl = strchr(line, '\n');
2372 if (nl)
2373 *nl = '\0';
2374 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2375 bline->id_str, bline->datebuf, committer, line);
2377 a->lineno_cur++;
2378 bline = &a->lines[a->lineno_cur - 1];
2380 done:
2381 if (commit)
2382 got_object_commit_close(commit);
2383 free(line);
2384 return err;
2387 static const struct got_error *
2388 cmd_blame(int argc, char *argv[])
2390 const struct got_error *error;
2391 struct got_repository *repo = NULL;
2392 struct got_worktree *worktree = NULL;
2393 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2394 struct got_object_id *obj_id = NULL;
2395 struct got_object_id *commit_id = NULL;
2396 struct got_blob_object *blob = NULL;
2397 char *commit_id_str = NULL;
2398 struct blame_cb_args bca;
2399 int ch, obj_type, i;
2400 size_t filesize;
2402 memset(&bca, 0, sizeof(bca));
2404 #ifndef PROFILE
2405 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2406 NULL) == -1)
2407 err(1, "pledge");
2408 #endif
2410 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2411 switch (ch) {
2412 case 'c':
2413 commit_id_str = optarg;
2414 break;
2415 case 'r':
2416 repo_path = realpath(optarg, NULL);
2417 if (repo_path == NULL)
2418 err(1, "-r option");
2419 got_path_strip_trailing_slashes(repo_path);
2420 break;
2421 default:
2422 usage_blame();
2423 /* NOTREACHED */
2427 argc -= optind;
2428 argv += optind;
2430 if (argc == 1)
2431 path = argv[0];
2432 else
2433 usage_blame();
2435 cwd = getcwd(NULL, 0);
2436 if (cwd == NULL) {
2437 error = got_error_from_errno("getcwd");
2438 goto done;
2440 if (repo_path == NULL) {
2441 error = got_worktree_open(&worktree, cwd);
2442 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2443 goto done;
2444 else
2445 error = NULL;
2446 if (worktree) {
2447 repo_path =
2448 strdup(got_worktree_get_repo_path(worktree));
2449 if (repo_path == NULL)
2450 error = got_error_from_errno("strdup");
2451 if (error)
2452 goto done;
2453 } else {
2454 repo_path = strdup(cwd);
2455 if (repo_path == NULL) {
2456 error = got_error_from_errno("strdup");
2457 goto done;
2462 error = got_repo_open(&repo, repo_path);
2463 if (error != NULL)
2464 goto done;
2466 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2467 if (error)
2468 goto done;
2470 if (worktree) {
2471 const char *prefix = got_worktree_get_path_prefix(worktree);
2472 char *p, *worktree_subdir = cwd +
2473 strlen(got_worktree_get_root_path(worktree));
2474 if (asprintf(&p, "%s%s%s%s%s",
2475 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2476 worktree_subdir, worktree_subdir[0] ? "/" : "",
2477 path) == -1) {
2478 error = got_error_from_errno("asprintf");
2479 goto done;
2481 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2482 free(p);
2483 } else {
2484 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2486 if (error)
2487 goto done;
2489 if (commit_id_str == NULL) {
2490 struct got_reference *head_ref;
2491 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2492 if (error != NULL)
2493 goto done;
2494 error = got_ref_resolve(&commit_id, repo, head_ref);
2495 got_ref_close(head_ref);
2496 if (error != NULL)
2497 goto done;
2498 } else {
2499 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2500 if (error)
2501 goto done;
2504 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2505 if (error)
2506 goto done;
2507 if (obj_id == NULL) {
2508 error = got_error(GOT_ERR_NO_OBJ);
2509 goto done;
2512 error = got_object_get_type(&obj_type, repo, obj_id);
2513 if (error)
2514 goto done;
2516 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2517 error = got_error(GOT_ERR_OBJ_TYPE);
2518 goto done;
2521 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2522 if (error)
2523 goto done;
2524 bca.f = got_opentemp();
2525 if (bca.f == NULL) {
2526 error = got_error_from_errno("got_opentemp");
2527 goto done;
2529 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2530 &bca.line_offsets, bca.f, blob);
2531 if (error || bca.nlines == 0)
2532 goto done;
2534 /* Don't include \n at EOF in the blame line count. */
2535 if (bca.line_offsets[bca.nlines - 1] == filesize)
2536 bca.nlines--;
2538 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2539 if (bca.lines == NULL) {
2540 error = got_error_from_errno("calloc");
2541 goto done;
2543 bca.lineno_cur = 1;
2544 bca.nlines_prec = 0;
2545 i = bca.nlines;
2546 while (i > 0) {
2547 i /= 10;
2548 bca.nlines_prec++;
2550 bca.repo = repo;
2552 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2553 check_cancelled, NULL);
2554 if (error)
2555 goto done;
2556 done:
2557 free(in_repo_path);
2558 free(repo_path);
2559 free(cwd);
2560 free(commit_id);
2561 free(obj_id);
2562 if (blob)
2563 got_object_blob_close(blob);
2564 if (worktree)
2565 got_worktree_close(worktree);
2566 if (repo) {
2567 const struct got_error *repo_error;
2568 repo_error = got_repo_close(repo);
2569 if (error == NULL)
2570 error = repo_error;
2572 for (i = 0; i < bca.nlines; i++) {
2573 struct blame_line *bline = &bca.lines[i];
2574 free(bline->id_str);
2575 free(bline->committer);
2577 free(bca.lines);
2578 free(bca.line_offsets);
2579 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2580 error = got_error_from_errno("fclose");
2581 return error;
2584 __dead static void
2585 usage_tree(void)
2587 fprintf(stderr,
2588 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2589 getprogname());
2590 exit(1);
2593 static void
2594 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2595 const char *root_path)
2597 int is_root_path = (strcmp(path, root_path) == 0);
2598 const char *modestr = "";
2600 path += strlen(root_path);
2601 while (path[0] == '/')
2602 path++;
2604 if (got_object_tree_entry_is_submodule(te))
2605 modestr = "$";
2606 else if (S_ISLNK(te->mode))
2607 modestr = "@";
2608 else if (S_ISDIR(te->mode))
2609 modestr = "/";
2610 else if (te->mode & S_IXUSR)
2611 modestr = "*";
2613 printf("%s%s%s%s%s\n", id ? id : "", path,
2614 is_root_path ? "" : "/", te->name, modestr);
2617 static const struct got_error *
2618 print_tree(const char *path, struct got_object_id *commit_id,
2619 int show_ids, int recurse, const char *root_path,
2620 struct got_repository *repo)
2622 const struct got_error *err = NULL;
2623 struct got_object_id *tree_id = NULL;
2624 struct got_tree_object *tree = NULL;
2625 const struct got_tree_entries *entries;
2626 struct got_tree_entry *te;
2628 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2629 if (err)
2630 goto done;
2632 err = got_object_open_as_tree(&tree, repo, tree_id);
2633 if (err)
2634 goto done;
2635 entries = got_object_tree_get_entries(tree);
2636 te = SIMPLEQ_FIRST(&entries->head);
2637 while (te) {
2638 char *id = NULL;
2640 if (sigint_received || sigpipe_received)
2641 break;
2643 if (show_ids) {
2644 char *id_str;
2645 err = got_object_id_str(&id_str, te->id);
2646 if (err)
2647 goto done;
2648 if (asprintf(&id, "%s ", id_str) == -1) {
2649 err = got_error_from_errno("asprintf");
2650 free(id_str);
2651 goto done;
2653 free(id_str);
2655 print_entry(te, id, path, root_path);
2656 free(id);
2658 if (recurse && S_ISDIR(te->mode)) {
2659 char *child_path;
2660 if (asprintf(&child_path, "%s%s%s", path,
2661 path[0] == '/' && path[1] == '\0' ? "" : "/",
2662 te->name) == -1) {
2663 err = got_error_from_errno("asprintf");
2664 goto done;
2666 err = print_tree(child_path, commit_id, show_ids, 1,
2667 root_path, repo);
2668 free(child_path);
2669 if (err)
2670 goto done;
2673 te = SIMPLEQ_NEXT(te, entry);
2675 done:
2676 if (tree)
2677 got_object_tree_close(tree);
2678 free(tree_id);
2679 return err;
2682 static const struct got_error *
2683 cmd_tree(int argc, char *argv[])
2685 const struct got_error *error;
2686 struct got_repository *repo = NULL;
2687 struct got_worktree *worktree = NULL;
2688 const char *path;
2689 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2690 struct got_object_id *commit_id = NULL;
2691 char *commit_id_str = NULL;
2692 int show_ids = 0, recurse = 0;
2693 int ch;
2695 #ifndef PROFILE
2696 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2697 NULL) == -1)
2698 err(1, "pledge");
2699 #endif
2701 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2702 switch (ch) {
2703 case 'c':
2704 commit_id_str = optarg;
2705 break;
2706 case 'r':
2707 repo_path = realpath(optarg, NULL);
2708 if (repo_path == NULL)
2709 err(1, "-r option");
2710 got_path_strip_trailing_slashes(repo_path);
2711 break;
2712 case 'i':
2713 show_ids = 1;
2714 break;
2715 case 'R':
2716 recurse = 1;
2717 break;
2718 default:
2719 usage_tree();
2720 /* NOTREACHED */
2724 argc -= optind;
2725 argv += optind;
2727 if (argc == 1)
2728 path = argv[0];
2729 else if (argc > 1)
2730 usage_tree();
2731 else
2732 path = NULL;
2734 cwd = getcwd(NULL, 0);
2735 if (cwd == NULL) {
2736 error = got_error_from_errno("getcwd");
2737 goto done;
2739 if (repo_path == NULL) {
2740 error = got_worktree_open(&worktree, cwd);
2741 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2742 goto done;
2743 else
2744 error = NULL;
2745 if (worktree) {
2746 repo_path =
2747 strdup(got_worktree_get_repo_path(worktree));
2748 if (repo_path == NULL)
2749 error = got_error_from_errno("strdup");
2750 if (error)
2751 goto done;
2752 } else {
2753 repo_path = strdup(cwd);
2754 if (repo_path == NULL) {
2755 error = got_error_from_errno("strdup");
2756 goto done;
2761 error = got_repo_open(&repo, repo_path);
2762 if (error != NULL)
2763 goto done;
2765 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2766 if (error)
2767 goto done;
2769 if (path == NULL) {
2770 if (worktree) {
2771 char *p, *worktree_subdir = cwd +
2772 strlen(got_worktree_get_root_path(worktree));
2773 if (asprintf(&p, "%s/%s",
2774 got_worktree_get_path_prefix(worktree),
2775 worktree_subdir) == -1) {
2776 error = got_error_from_errno("asprintf");
2777 goto done;
2779 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2780 free(p);
2781 if (error)
2782 goto done;
2783 } else
2784 path = "/";
2786 if (in_repo_path == NULL) {
2787 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2788 if (error != NULL)
2789 goto done;
2792 if (commit_id_str == NULL) {
2793 struct got_reference *head_ref;
2794 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2795 if (error != NULL)
2796 goto done;
2797 error = got_ref_resolve(&commit_id, repo, head_ref);
2798 got_ref_close(head_ref);
2799 if (error != NULL)
2800 goto done;
2801 } else {
2802 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2803 if (error)
2804 goto done;
2807 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2808 in_repo_path, repo);
2809 done:
2810 free(in_repo_path);
2811 free(repo_path);
2812 free(cwd);
2813 free(commit_id);
2814 if (worktree)
2815 got_worktree_close(worktree);
2816 if (repo) {
2817 const struct got_error *repo_error;
2818 repo_error = got_repo_close(repo);
2819 if (error == NULL)
2820 error = repo_error;
2822 return error;
2825 __dead static void
2826 usage_status(void)
2828 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2829 exit(1);
2832 static const struct got_error *
2833 print_status(void *arg, unsigned char status, unsigned char staged_status,
2834 const char *path, struct got_object_id *blob_id,
2835 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2837 if (status == staged_status && (status == GOT_STATUS_DELETE))
2838 status = GOT_STATUS_NO_CHANGE;
2839 printf("%c%c %s\n", status, staged_status, path);
2840 return NULL;
2843 static const struct got_error *
2844 cmd_status(int argc, char *argv[])
2846 const struct got_error *error = NULL;
2847 struct got_repository *repo = NULL;
2848 struct got_worktree *worktree = NULL;
2849 char *cwd = NULL;
2850 struct got_pathlist_head paths;
2851 struct got_pathlist_entry *pe;
2852 int ch;
2854 TAILQ_INIT(&paths);
2856 while ((ch = getopt(argc, argv, "")) != -1) {
2857 switch (ch) {
2858 default:
2859 usage_status();
2860 /* NOTREACHED */
2864 argc -= optind;
2865 argv += optind;
2867 #ifndef PROFILE
2868 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2869 NULL) == -1)
2870 err(1, "pledge");
2871 #endif
2872 cwd = getcwd(NULL, 0);
2873 if (cwd == NULL) {
2874 error = got_error_from_errno("getcwd");
2875 goto done;
2878 error = got_worktree_open(&worktree, cwd);
2879 if (error != NULL)
2880 goto done;
2882 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2883 if (error != NULL)
2884 goto done;
2886 error = apply_unveil(got_repo_get_path(repo), 1,
2887 got_worktree_get_root_path(worktree));
2888 if (error)
2889 goto done;
2891 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2892 if (error)
2893 goto done;
2895 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2896 check_cancelled, NULL);
2897 done:
2898 TAILQ_FOREACH(pe, &paths, entry)
2899 free((char *)pe->path);
2900 got_pathlist_free(&paths);
2901 free(cwd);
2902 return error;
2905 __dead static void
2906 usage_ref(void)
2908 fprintf(stderr,
2909 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2910 getprogname());
2911 exit(1);
2914 static const struct got_error *
2915 list_refs(struct got_repository *repo)
2917 static const struct got_error *err = NULL;
2918 struct got_reflist_head refs;
2919 struct got_reflist_entry *re;
2921 SIMPLEQ_INIT(&refs);
2922 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2923 if (err)
2924 return err;
2926 SIMPLEQ_FOREACH(re, &refs, entry) {
2927 char *refstr;
2928 refstr = got_ref_to_str(re->ref);
2929 if (refstr == NULL)
2930 return got_error_from_errno("got_ref_to_str");
2931 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2932 free(refstr);
2935 got_ref_list_free(&refs);
2936 return NULL;
2939 static const struct got_error *
2940 delete_ref(struct got_repository *repo, const char *refname)
2942 const struct got_error *err = NULL;
2943 struct got_reference *ref;
2945 err = got_ref_open(&ref, repo, refname, 0);
2946 if (err)
2947 return err;
2949 err = got_ref_delete(ref, repo);
2950 got_ref_close(ref);
2951 return err;
2954 static const struct got_error *
2955 add_ref(struct got_repository *repo, const char *refname, const char *target)
2957 const struct got_error *err = NULL;
2958 struct got_object_id *id;
2959 struct got_reference *ref = NULL;
2962 * Don't let the user create a reference named '-'.
2963 * While technically a valid reference name, this case is usually
2964 * an unintended typo.
2966 if (refname[0] == '-' && refname[1] == '\0')
2967 return got_error(GOT_ERR_BAD_REF_NAME);
2969 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2970 repo);
2971 if (err) {
2972 struct got_reference *target_ref;
2974 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2975 return err;
2976 err = got_ref_open(&target_ref, repo, target, 0);
2977 if (err)
2978 return err;
2979 err = got_ref_resolve(&id, repo, target_ref);
2980 got_ref_close(target_ref);
2981 if (err)
2982 return err;
2985 err = got_ref_alloc(&ref, refname, id);
2986 if (err)
2987 goto done;
2989 err = got_ref_write(ref, repo);
2990 done:
2991 if (ref)
2992 got_ref_close(ref);
2993 free(id);
2994 return err;
2997 static const struct got_error *
2998 add_symref(struct got_repository *repo, const char *refname, const char *target)
3000 const struct got_error *err = NULL;
3001 struct got_reference *ref = NULL;
3002 struct got_reference *target_ref = NULL;
3005 * Don't let the user create a reference named '-'.
3006 * While technically a valid reference name, this case is usually
3007 * an unintended typo.
3009 if (refname[0] == '-' && refname[1] == '\0')
3010 return got_error(GOT_ERR_BAD_REF_NAME);
3012 err = got_ref_open(&target_ref, repo, target, 0);
3013 if (err)
3014 return err;
3016 err = got_ref_alloc_symref(&ref, refname, target_ref);
3017 if (err)
3018 goto done;
3020 err = got_ref_write(ref, repo);
3021 done:
3022 if (target_ref)
3023 got_ref_close(target_ref);
3024 if (ref)
3025 got_ref_close(ref);
3026 return err;
3029 static const struct got_error *
3030 cmd_ref(int argc, char *argv[])
3032 const struct got_error *error = NULL;
3033 struct got_repository *repo = NULL;
3034 struct got_worktree *worktree = NULL;
3035 char *cwd = NULL, *repo_path = NULL;
3036 int ch, do_list = 0, create_symref = 0;
3037 const char *delref = NULL;
3039 /* TODO: Add -s option for adding symbolic references. */
3040 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3041 switch (ch) {
3042 case 'd':
3043 delref = optarg;
3044 break;
3045 case 'r':
3046 repo_path = realpath(optarg, NULL);
3047 if (repo_path == NULL)
3048 err(1, "-r option");
3049 got_path_strip_trailing_slashes(repo_path);
3050 break;
3051 case 'l':
3052 do_list = 1;
3053 break;
3054 case 's':
3055 create_symref = 1;
3056 break;
3057 default:
3058 usage_ref();
3059 /* NOTREACHED */
3063 if (do_list && delref)
3064 errx(1, "-l and -d options are mutually exclusive\n");
3066 argc -= optind;
3067 argv += optind;
3069 if (do_list || delref) {
3070 if (create_symref)
3071 errx(1, "-s option cannot be used together with the "
3072 "-l or -d options");
3073 if (argc > 0)
3074 usage_ref();
3075 } else if (argc != 2)
3076 usage_ref();
3078 #ifndef PROFILE
3079 if (do_list) {
3080 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3081 NULL) == -1)
3082 err(1, "pledge");
3083 } else {
3084 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3085 "sendfd unveil", NULL) == -1)
3086 err(1, "pledge");
3088 #endif
3089 cwd = getcwd(NULL, 0);
3090 if (cwd == NULL) {
3091 error = got_error_from_errno("getcwd");
3092 goto done;
3095 if (repo_path == NULL) {
3096 error = got_worktree_open(&worktree, cwd);
3097 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3098 goto done;
3099 else
3100 error = NULL;
3101 if (worktree) {
3102 repo_path =
3103 strdup(got_worktree_get_repo_path(worktree));
3104 if (repo_path == NULL)
3105 error = got_error_from_errno("strdup");
3106 if (error)
3107 goto done;
3108 } else {
3109 repo_path = strdup(cwd);
3110 if (repo_path == NULL) {
3111 error = got_error_from_errno("strdup");
3112 goto done;
3117 error = got_repo_open(&repo, repo_path);
3118 if (error != NULL)
3119 goto done;
3121 error = apply_unveil(got_repo_get_path(repo), do_list,
3122 worktree ? got_worktree_get_root_path(worktree) : NULL);
3123 if (error)
3124 goto done;
3126 if (do_list)
3127 error = list_refs(repo);
3128 else if (delref)
3129 error = delete_ref(repo, delref);
3130 else if (create_symref)
3131 error = add_symref(repo, argv[0], argv[1]);
3132 else
3133 error = add_ref(repo, argv[0], argv[1]);
3134 done:
3135 if (repo)
3136 got_repo_close(repo);
3137 if (worktree)
3138 got_worktree_close(worktree);
3139 free(cwd);
3140 free(repo_path);
3141 return error;
3144 __dead static void
3145 usage_branch(void)
3147 fprintf(stderr,
3148 "usage: %s branch [-r repository] -l | -d name | "
3149 "name [commit]\n", getprogname());
3150 exit(1);
3153 static const struct got_error *
3154 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3156 static const struct got_error *err = NULL;
3157 struct got_reflist_head refs;
3158 struct got_reflist_entry *re;
3160 SIMPLEQ_INIT(&refs);
3162 err = got_ref_list(&refs, repo, "refs/heads",
3163 got_ref_cmp_by_name, NULL);
3164 if (err)
3165 return err;
3167 SIMPLEQ_FOREACH(re, &refs, entry) {
3168 const char *refname, *marker = " ";
3169 char *refstr;
3170 refname = got_ref_get_name(re->ref);
3171 if (worktree && strcmp(refname,
3172 got_worktree_get_head_ref_name(worktree)) == 0) {
3173 struct got_object_id *id = NULL;
3174 err = got_ref_resolve(&id, repo, re->ref);
3175 if (err)
3176 return err;
3177 if (got_object_id_cmp(id,
3178 got_worktree_get_base_commit_id(worktree)) == 0)
3179 marker = "* ";
3180 else
3181 marker = "~ ";
3182 free(id);
3184 refname += strlen("refs/heads/");
3185 refstr = got_ref_to_str(re->ref);
3186 if (refstr == NULL)
3187 return got_error_from_errno("got_ref_to_str");
3188 printf("%s%s: %s\n", marker, refname, refstr);
3189 free(refstr);
3192 got_ref_list_free(&refs);
3193 return NULL;
3196 static const struct got_error *
3197 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3198 const char *branch_name)
3200 const struct got_error *err = NULL;
3201 struct got_reference *ref = NULL;
3202 char *refname;
3204 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3205 return got_error_from_errno("asprintf");
3207 err = got_ref_open(&ref, repo, refname, 0);
3208 if (err)
3209 goto done;
3211 if (worktree &&
3212 strcmp(got_worktree_get_head_ref_name(worktree),
3213 got_ref_get_name(ref)) == 0) {
3214 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3215 "will not delete this work tree's current branch");
3216 goto done;
3219 err = got_ref_delete(ref, repo);
3220 done:
3221 if (ref)
3222 got_ref_close(ref);
3223 free(refname);
3224 return err;
3227 static const struct got_error *
3228 add_branch(struct got_repository *repo, const char *branch_name,
3229 const char *base_branch)
3231 const struct got_error *err = NULL;
3232 struct got_object_id *id = NULL;
3233 char *label;
3234 struct got_reference *ref = NULL;
3235 char *base_refname = NULL, *refname = NULL;
3238 * Don't let the user create a branch named '-'.
3239 * While technically a valid reference name, this case is usually
3240 * an unintended typo.
3242 if (branch_name[0] == '-' && branch_name[1] == '\0')
3243 return got_error(GOT_ERR_BAD_REF_NAME);
3245 err = match_object_id(&id, &label, base_branch,
3246 GOT_OBJ_TYPE_COMMIT, 1, repo);
3247 if (err)
3248 return err;
3250 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3251 err = got_error_from_errno("asprintf");
3252 goto done;
3255 err = got_ref_open(&ref, repo, refname, 0);
3256 if (err == NULL) {
3257 err = got_error(GOT_ERR_BRANCH_EXISTS);
3258 goto done;
3259 } else if (err->code != GOT_ERR_NOT_REF)
3260 goto done;
3262 err = got_ref_alloc(&ref, refname, id);
3263 if (err)
3264 goto done;
3266 err = got_ref_write(ref, repo);
3267 done:
3268 if (ref)
3269 got_ref_close(ref);
3270 free(id);
3271 free(base_refname);
3272 free(refname);
3273 return err;
3276 static const struct got_error *
3277 cmd_branch(int argc, char *argv[])
3279 const struct got_error *error = NULL;
3280 struct got_repository *repo = NULL;
3281 struct got_worktree *worktree = NULL;
3282 char *cwd = NULL, *repo_path = NULL;
3283 int ch, do_list = 0;
3284 const char *delref = NULL;
3286 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3287 switch (ch) {
3288 case 'd':
3289 delref = optarg;
3290 break;
3291 case 'r':
3292 repo_path = realpath(optarg, NULL);
3293 if (repo_path == NULL)
3294 err(1, "-r option");
3295 got_path_strip_trailing_slashes(repo_path);
3296 break;
3297 case 'l':
3298 do_list = 1;
3299 break;
3300 default:
3301 usage_branch();
3302 /* NOTREACHED */
3306 if (do_list && delref)
3307 errx(1, "-l and -d options are mutually exclusive\n");
3309 argc -= optind;
3310 argv += optind;
3312 if (do_list || delref) {
3313 if (argc > 0)
3314 usage_branch();
3315 } else if (argc < 1 || argc > 2)
3316 usage_branch();
3318 #ifndef PROFILE
3319 if (do_list) {
3320 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3321 NULL) == -1)
3322 err(1, "pledge");
3323 } else {
3324 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3325 "sendfd unveil", NULL) == -1)
3326 err(1, "pledge");
3328 #endif
3329 cwd = getcwd(NULL, 0);
3330 if (cwd == NULL) {
3331 error = got_error_from_errno("getcwd");
3332 goto done;
3335 if (repo_path == NULL) {
3336 error = got_worktree_open(&worktree, cwd);
3337 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3338 goto done;
3339 else
3340 error = NULL;
3341 if (worktree) {
3342 repo_path =
3343 strdup(got_worktree_get_repo_path(worktree));
3344 if (repo_path == NULL)
3345 error = got_error_from_errno("strdup");
3346 if (error)
3347 goto done;
3348 } else {
3349 repo_path = strdup(cwd);
3350 if (repo_path == NULL) {
3351 error = got_error_from_errno("strdup");
3352 goto done;
3357 error = got_repo_open(&repo, repo_path);
3358 if (error != NULL)
3359 goto done;
3361 error = apply_unveil(got_repo_get_path(repo), do_list,
3362 worktree ? got_worktree_get_root_path(worktree) : NULL);
3363 if (error)
3364 goto done;
3366 if (do_list)
3367 error = list_branches(repo, worktree);
3368 else if (delref)
3369 error = delete_branch(repo, worktree, delref);
3370 else {
3371 const char *base_branch;
3372 if (argc == 1) {
3373 base_branch = worktree ?
3374 got_worktree_get_head_ref_name(worktree) :
3375 GOT_REF_HEAD;
3376 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3377 base_branch += 11;
3378 } else
3379 base_branch = argv[1];
3380 error = add_branch(repo, argv[0], base_branch);
3382 done:
3383 if (repo)
3384 got_repo_close(repo);
3385 if (worktree)
3386 got_worktree_close(worktree);
3387 free(cwd);
3388 free(repo_path);
3389 return error;
3393 __dead static void
3394 usage_tag(void)
3396 fprintf(stderr,
3397 "usage: %s tag [-r repository] | -l | "
3398 "[-m message] name [commit]\n", getprogname());
3399 exit(1);
3402 #if 0
3403 static const struct got_error *
3404 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3406 const struct got_error *err = NULL;
3407 struct got_reflist_entry *re, *se, *new;
3408 struct got_object_id *re_id, *se_id;
3409 struct got_tag_object *re_tag, *se_tag;
3410 time_t re_time, se_time;
3412 SIMPLEQ_FOREACH(re, tags, entry) {
3413 se = SIMPLEQ_FIRST(sorted);
3414 if (se == NULL) {
3415 err = got_reflist_entry_dup(&new, re);
3416 if (err)
3417 return err;
3418 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3419 continue;
3420 } else {
3421 err = got_ref_resolve(&re_id, repo, re->ref);
3422 if (err)
3423 break;
3424 err = got_object_open_as_tag(&re_tag, repo, re_id);
3425 free(re_id);
3426 if (err)
3427 break;
3428 re_time = got_object_tag_get_tagger_time(re_tag);
3429 got_object_tag_close(re_tag);
3432 while (se) {
3433 err = got_ref_resolve(&se_id, repo, re->ref);
3434 if (err)
3435 break;
3436 err = got_object_open_as_tag(&se_tag, repo, se_id);
3437 free(se_id);
3438 if (err)
3439 break;
3440 se_time = got_object_tag_get_tagger_time(se_tag);
3441 got_object_tag_close(se_tag);
3443 if (se_time > re_time) {
3444 err = got_reflist_entry_dup(&new, re);
3445 if (err)
3446 return err;
3447 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3448 break;
3450 se = SIMPLEQ_NEXT(se, entry);
3451 continue;
3454 done:
3455 return err;
3457 #endif
3459 static const struct got_error *
3460 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3461 struct got_reference *ref2)
3463 const struct got_error *err = NULL;
3464 struct got_repository *repo = arg;
3465 struct got_object_id *id1, *id2 = NULL;
3466 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3467 time_t time1, time2;
3469 *cmp = 0;
3471 err = got_ref_resolve(&id1, repo, ref1);
3472 if (err)
3473 return err;
3474 err = got_object_open_as_tag(&tag1, repo, id1);
3475 if (err)
3476 goto done;
3478 err = got_ref_resolve(&id2, repo, ref2);
3479 if (err)
3480 goto done;
3481 err = got_object_open_as_tag(&tag2, repo, id2);
3482 if (err)
3483 goto done;
3485 time1 = got_object_tag_get_tagger_time(tag1);
3486 time2 = got_object_tag_get_tagger_time(tag2);
3488 /* Put latest tags first. */
3489 if (time1 < time2)
3490 *cmp = 1;
3491 else if (time1 > time2)
3492 *cmp = -1;
3493 else
3494 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3495 done:
3496 free(id1);
3497 free(id2);
3498 if (tag1)
3499 got_object_tag_close(tag1);
3500 if (tag2)
3501 got_object_tag_close(tag2);
3502 return err;
3505 static const struct got_error *
3506 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3508 static const struct got_error *err = NULL;
3509 struct got_reflist_head refs;
3510 struct got_reflist_entry *re;
3512 SIMPLEQ_INIT(&refs);
3514 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3515 if (err)
3516 return err;
3518 SIMPLEQ_FOREACH(re, &refs, entry) {
3519 const char *refname;
3520 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3521 char datebuf[26];
3522 time_t tagger_time;
3523 struct got_object_id *id;
3524 struct got_tag_object *tag;
3526 refname = got_ref_get_name(re->ref);
3527 if (strncmp(refname, "refs/tags/", 10) != 0)
3528 continue;
3529 refname += 10;
3530 refstr = got_ref_to_str(re->ref);
3531 if (refstr == NULL) {
3532 err = got_error_from_errno("got_ref_to_str");
3533 break;
3535 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3536 free(refstr);
3538 err = got_ref_resolve(&id, repo, re->ref);
3539 if (err)
3540 break;
3541 err = got_object_open_as_tag(&tag, repo, id);
3542 free(id);
3543 if (err)
3544 break;
3545 printf("from: %s\n", got_object_tag_get_tagger(tag));
3546 tagger_time = got_object_tag_get_tagger_time(tag);
3547 datestr = get_datestr(&tagger_time, datebuf);
3548 if (datestr)
3549 printf("date: %s UTC\n", datestr);
3550 err = got_object_id_str(&id_str,
3551 got_object_tag_get_object_id(tag));
3552 if (err)
3553 break;
3554 switch (got_object_tag_get_object_type(tag)) {
3555 case GOT_OBJ_TYPE_BLOB:
3556 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3557 break;
3558 case GOT_OBJ_TYPE_TREE:
3559 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3560 break;
3561 case GOT_OBJ_TYPE_COMMIT:
3562 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3563 break;
3564 case GOT_OBJ_TYPE_TAG:
3565 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3566 break;
3567 default:
3568 break;
3570 free(id_str);
3571 tagmsg0 = strdup(got_object_tag_get_message(tag));
3572 got_object_tag_close(tag);
3573 if (tagmsg0 == NULL) {
3574 err = got_error_from_errno("strdup");
3575 break;
3578 tagmsg = tagmsg0;
3579 do {
3580 line = strsep(&tagmsg, "\n");
3581 if (line)
3582 printf(" %s\n", line);
3583 } while (line);
3584 free(tagmsg0);
3587 got_ref_list_free(&refs);
3588 return NULL;
3591 static const struct got_error *
3592 get_tag_message(char **tagmsg, const char *commit_id_str,
3593 const char *tag_name, const char *repo_path)
3595 const struct got_error *err = NULL;
3596 char *template = NULL, *initial_content = NULL;
3597 char *tagmsg_path = NULL, *editor = NULL;
3598 int fd = -1;
3600 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3601 err = got_error_from_errno("asprintf");
3602 goto done;
3605 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3606 commit_id_str, tag_name) == -1) {
3607 err = got_error_from_errno("asprintf");
3608 goto done;
3611 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3612 if (err)
3613 goto done;
3615 dprintf(fd, initial_content);
3616 close(fd);
3618 err = get_editor(&editor);
3619 if (err)
3620 goto done;
3621 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3622 done:
3623 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3624 unlink(tagmsg_path);
3625 free(tagmsg_path);
3626 tagmsg_path = NULL;
3628 free(initial_content);
3629 free(template);
3630 free(editor);
3632 /* Editor is done; we can now apply unveil(2) */
3633 if (err == NULL) {
3634 err = apply_unveil(repo_path, 0, NULL);
3635 if (err) {
3636 free(*tagmsg);
3637 *tagmsg = NULL;
3640 return err;
3643 static const struct got_error *
3644 add_tag(struct got_repository *repo, const char *tag_name,
3645 const char *commit_arg, const char *tagmsg_arg)
3647 const struct got_error *err = NULL;
3648 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3649 char *label = NULL, *commit_id_str = NULL;
3650 struct got_reference *ref = NULL;
3651 char *refname = NULL, *tagmsg = NULL;
3652 const char *tagger;
3655 * Don't let the user create a tag named '-'.
3656 * While technically a valid reference name, this case is usually
3657 * an unintended typo.
3659 if (tag_name[0] == '-' && tag_name[1] == '\0')
3660 return got_error(GOT_ERR_BAD_REF_NAME);
3662 err = get_author(&tagger);
3663 if (err)
3664 return err;
3666 err = match_object_id(&commit_id, &label, commit_arg,
3667 GOT_OBJ_TYPE_COMMIT, 1, repo);
3668 if (err)
3669 goto done;
3671 err = got_object_id_str(&commit_id_str, commit_id);
3672 if (err)
3673 goto done;
3675 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3676 refname = strdup(tag_name);
3677 if (refname == NULL) {
3678 err = got_error_from_errno("strdup");
3679 goto done;
3681 tag_name += 10;
3682 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3683 err = got_error_from_errno("asprintf");
3684 goto done;
3687 err = got_ref_open(&ref, repo, refname, 0);
3688 if (err == NULL) {
3689 err = got_error(GOT_ERR_TAG_EXISTS);
3690 goto done;
3691 } else if (err->code != GOT_ERR_NOT_REF)
3692 goto done;
3694 if (tagmsg_arg == NULL) {
3695 err = get_tag_message(&tagmsg, commit_id_str,
3696 tag_name, got_repo_get_path(repo));
3697 if (err)
3698 goto done;
3701 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3702 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3703 if (err)
3704 goto done;
3706 err = got_ref_alloc(&ref, refname, tag_id);
3707 if (err)
3708 goto done;
3710 err = got_ref_write(ref, repo);
3712 if (err == NULL) {
3713 char *tag_id_str;
3714 err = got_object_id_str(&tag_id_str, tag_id);
3715 printf("Created tag %s\n", tag_id_str);
3716 free(tag_id_str);
3718 done:
3719 if (ref)
3720 got_ref_close(ref);
3721 free(commit_id);
3722 free(commit_id_str);
3723 free(refname);
3724 free(tagmsg);
3725 return err;
3728 static const struct got_error *
3729 cmd_tag(int argc, char *argv[])
3731 const struct got_error *error = NULL;
3732 struct got_repository *repo = NULL;
3733 struct got_worktree *worktree = NULL;
3734 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3735 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3736 int ch, do_list = 0;
3738 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3739 switch (ch) {
3740 case 'm':
3741 tagmsg = optarg;
3742 break;
3743 case 'r':
3744 repo_path = realpath(optarg, NULL);
3745 if (repo_path == NULL)
3746 err(1, "-r option");
3747 got_path_strip_trailing_slashes(repo_path);
3748 break;
3749 case 'l':
3750 do_list = 1;
3751 break;
3752 default:
3753 usage_tag();
3754 /* NOTREACHED */
3758 argc -= optind;
3759 argv += optind;
3761 if (do_list) {
3762 if (tagmsg)
3763 errx(1, "-l and -m options are mutually exclusive\n");
3764 if (argc > 0)
3765 usage_tag();
3766 } else if (argc < 1 || argc > 2)
3767 usage_tag();
3768 else if (argc > 1)
3769 commit_id_arg = argv[1];
3770 tag_name = argv[0];
3772 #ifndef PROFILE
3773 if (do_list) {
3774 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3775 NULL) == -1)
3776 err(1, "pledge");
3777 } else {
3778 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3779 "sendfd unveil", NULL) == -1)
3780 err(1, "pledge");
3782 #endif
3783 cwd = getcwd(NULL, 0);
3784 if (cwd == NULL) {
3785 error = got_error_from_errno("getcwd");
3786 goto done;
3789 if (repo_path == NULL) {
3790 error = got_worktree_open(&worktree, cwd);
3791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3792 goto done;
3793 else
3794 error = NULL;
3795 if (worktree) {
3796 repo_path =
3797 strdup(got_worktree_get_repo_path(worktree));
3798 if (repo_path == NULL)
3799 error = got_error_from_errno("strdup");
3800 if (error)
3801 goto done;
3802 } else {
3803 repo_path = strdup(cwd);
3804 if (repo_path == NULL) {
3805 error = got_error_from_errno("strdup");
3806 goto done;
3811 error = got_repo_open(&repo, repo_path);
3812 if (error != NULL)
3813 goto done;
3816 if (do_list) {
3817 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3818 if (error)
3819 goto done;
3820 error = list_tags(repo, worktree);
3821 } else {
3822 if (tagmsg) {
3823 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3824 if (error)
3825 goto done;
3828 if (commit_id_arg == NULL) {
3829 struct got_reference *head_ref;
3830 struct got_object_id *commit_id;
3831 error = got_ref_open(&head_ref, repo,
3832 worktree ? got_worktree_get_head_ref_name(worktree)
3833 : GOT_REF_HEAD, 0);
3834 if (error)
3835 goto done;
3836 error = got_ref_resolve(&commit_id, repo, head_ref);
3837 got_ref_close(head_ref);
3838 if (error)
3839 goto done;
3840 error = got_object_id_str(&commit_id_str, commit_id);
3841 free(commit_id);
3842 if (error)
3843 goto done;
3846 error = add_tag(repo, tag_name,
3847 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3849 done:
3850 if (repo)
3851 got_repo_close(repo);
3852 if (worktree)
3853 got_worktree_close(worktree);
3854 free(cwd);
3855 free(repo_path);
3856 free(commit_id_str);
3857 return error;
3860 __dead static void
3861 usage_add(void)
3863 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3864 exit(1);
3867 static const struct got_error *
3868 cmd_add(int argc, char *argv[])
3870 const struct got_error *error = NULL;
3871 struct got_repository *repo = NULL;
3872 struct got_worktree *worktree = NULL;
3873 char *cwd = NULL;
3874 struct got_pathlist_head paths;
3875 struct got_pathlist_entry *pe;
3876 int ch;
3878 TAILQ_INIT(&paths);
3880 while ((ch = getopt(argc, argv, "")) != -1) {
3881 switch (ch) {
3882 default:
3883 usage_add();
3884 /* NOTREACHED */
3888 argc -= optind;
3889 argv += optind;
3891 #ifndef PROFILE
3892 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3893 NULL) == -1)
3894 err(1, "pledge");
3895 #endif
3896 if (argc < 1)
3897 usage_add();
3899 cwd = getcwd(NULL, 0);
3900 if (cwd == NULL) {
3901 error = got_error_from_errno("getcwd");
3902 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 != NULL)
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_add(worktree, &paths, print_status,
3923 NULL, repo);
3924 done:
3925 if (repo)
3926 got_repo_close(repo);
3927 if (worktree)
3928 got_worktree_close(worktree);
3929 TAILQ_FOREACH(pe, &paths, entry)
3930 free((char *)pe->path);
3931 got_pathlist_free(&paths);
3932 free(cwd);
3933 return error;
3936 __dead static void
3937 usage_remove(void)
3939 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3940 exit(1);
3943 static const struct got_error *
3944 print_remove_status(void *arg, unsigned char status,
3945 unsigned char staged_status, const char *path,
3946 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3947 struct got_object_id *commit_id)
3949 if (status == GOT_STATUS_NONEXISTENT)
3950 return NULL;
3951 if (status == staged_status && (status == GOT_STATUS_DELETE))
3952 status = GOT_STATUS_NO_CHANGE;
3953 printf("%c%c %s\n", status, staged_status, path);
3954 return NULL;
3957 static const struct got_error *
3958 cmd_remove(int argc, char *argv[])
3960 const struct got_error *error = NULL;
3961 struct got_worktree *worktree = NULL;
3962 struct got_repository *repo = NULL;
3963 char *cwd = NULL;
3964 struct got_pathlist_head paths;
3965 struct got_pathlist_entry *pe;
3966 int ch, delete_local_mods = 0;
3968 TAILQ_INIT(&paths);
3970 while ((ch = getopt(argc, argv, "f")) != -1) {
3971 switch (ch) {
3972 case 'f':
3973 delete_local_mods = 1;
3974 break;
3975 default:
3976 usage_add();
3977 /* NOTREACHED */
3981 argc -= optind;
3982 argv += optind;
3984 #ifndef PROFILE
3985 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3986 NULL) == -1)
3987 err(1, "pledge");
3988 #endif
3989 if (argc < 1)
3990 usage_remove();
3992 cwd = getcwd(NULL, 0);
3993 if (cwd == NULL) {
3994 error = got_error_from_errno("getcwd");
3995 goto done;
3997 error = got_worktree_open(&worktree, cwd);
3998 if (error)
3999 goto done;
4001 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4002 if (error)
4003 goto done;
4005 error = apply_unveil(got_repo_get_path(repo), 1,
4006 got_worktree_get_root_path(worktree));
4007 if (error)
4008 goto done;
4010 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4011 if (error)
4012 goto done;
4014 error = got_worktree_schedule_delete(worktree, &paths,
4015 delete_local_mods, print_remove_status, NULL, repo);
4016 if (error)
4017 goto done;
4018 done:
4019 if (repo)
4020 got_repo_close(repo);
4021 if (worktree)
4022 got_worktree_close(worktree);
4023 TAILQ_FOREACH(pe, &paths, entry)
4024 free((char *)pe->path);
4025 got_pathlist_free(&paths);
4026 free(cwd);
4027 return error;
4030 __dead static void
4031 usage_revert(void)
4033 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4034 "path ...\n", getprogname());
4035 exit(1);
4038 static const struct got_error *
4039 revert_progress(void *arg, unsigned char status, const char *path)
4041 while (path[0] == '/')
4042 path++;
4043 printf("%c %s\n", status, path);
4044 return NULL;
4047 struct choose_patch_arg {
4048 FILE *patch_script_file;
4049 const char *action;
4052 static const struct got_error *
4053 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4054 int nchanges, const char *action)
4056 char *line = NULL;
4057 size_t linesize = 0;
4058 ssize_t linelen;
4060 switch (status) {
4061 case GOT_STATUS_ADD:
4062 printf("A %s\n%s this addition? [y/n] ", path, action);
4063 break;
4064 case GOT_STATUS_DELETE:
4065 printf("D %s\n%s this deletion? [y/n] ", path, action);
4066 break;
4067 case GOT_STATUS_MODIFY:
4068 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4069 return got_error_from_errno("fseek");
4070 printf(GOT_COMMIT_SEP_STR);
4071 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4072 printf("%s", line);
4073 if (ferror(patch_file))
4074 return got_error_from_errno("getline");
4075 printf(GOT_COMMIT_SEP_STR);
4076 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4077 path, n, nchanges, action);
4078 break;
4079 default:
4080 return got_error_path(path, GOT_ERR_FILE_STATUS);
4083 return NULL;
4086 static const struct got_error *
4087 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4088 FILE *patch_file, int n, int nchanges)
4090 const struct got_error *err = NULL;
4091 char *line = NULL;
4092 size_t linesize = 0;
4093 ssize_t linelen;
4094 int resp = ' ';
4095 struct choose_patch_arg *a = arg;
4097 *choice = GOT_PATCH_CHOICE_NONE;
4099 if (a->patch_script_file) {
4100 char *nl;
4101 err = show_change(status, path, patch_file, n, nchanges,
4102 a->action);
4103 if (err)
4104 return err;
4105 linelen = getline(&line, &linesize, a->patch_script_file);
4106 if (linelen == -1) {
4107 if (ferror(a->patch_script_file))
4108 return got_error_from_errno("getline");
4109 return NULL;
4111 nl = strchr(line, '\n');
4112 if (nl)
4113 *nl = '\0';
4114 if (strcmp(line, "y") == 0) {
4115 *choice = GOT_PATCH_CHOICE_YES;
4116 printf("y\n");
4117 } else if (strcmp(line, "n") == 0) {
4118 *choice = GOT_PATCH_CHOICE_NO;
4119 printf("n\n");
4120 } else if (strcmp(line, "q") == 0 &&
4121 status == GOT_STATUS_MODIFY) {
4122 *choice = GOT_PATCH_CHOICE_QUIT;
4123 printf("q\n");
4124 } else
4125 printf("invalid response '%s'\n", line);
4126 free(line);
4127 return NULL;
4130 while (resp != 'y' && resp != 'n' && resp != 'q') {
4131 err = show_change(status, path, patch_file, n, nchanges,
4132 a->action);
4133 if (err)
4134 return err;
4135 resp = getchar();
4136 if (resp == '\n')
4137 resp = getchar();
4138 if (status == GOT_STATUS_MODIFY) {
4139 if (resp != 'y' && resp != 'n' && resp != 'q') {
4140 printf("invalid response '%c'\n", resp);
4141 resp = ' ';
4143 } else if (resp != 'y' && resp != 'n') {
4144 printf("invalid response '%c'\n", resp);
4145 resp = ' ';
4149 if (resp == 'y')
4150 *choice = GOT_PATCH_CHOICE_YES;
4151 else if (resp == 'n')
4152 *choice = GOT_PATCH_CHOICE_NO;
4153 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4154 *choice = GOT_PATCH_CHOICE_QUIT;
4156 return NULL;
4160 static const struct got_error *
4161 cmd_revert(int argc, char *argv[])
4163 const struct got_error *error = NULL;
4164 struct got_worktree *worktree = NULL;
4165 struct got_repository *repo = NULL;
4166 char *cwd = NULL, *path = NULL;
4167 struct got_pathlist_head paths;
4168 struct got_pathlist_entry *pe;
4169 int ch, can_recurse = 0, pflag = 0;
4170 FILE *patch_script_file = NULL;
4171 const char *patch_script_path = NULL;
4172 struct choose_patch_arg cpa;
4174 TAILQ_INIT(&paths);
4176 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4177 switch (ch) {
4178 case 'p':
4179 pflag = 1;
4180 break;
4181 case 'F':
4182 patch_script_path = optarg;
4183 break;
4184 case 'R':
4185 can_recurse = 1;
4186 break;
4187 default:
4188 usage_revert();
4189 /* NOTREACHED */
4193 argc -= optind;
4194 argv += optind;
4196 #ifndef PROFILE
4197 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4198 "unveil", NULL) == -1)
4199 err(1, "pledge");
4200 #endif
4201 if (argc < 1)
4202 usage_revert();
4203 if (patch_script_path && !pflag)
4204 errx(1, "-F option can only be used together with -p option");
4206 cwd = getcwd(NULL, 0);
4207 if (cwd == NULL) {
4208 error = got_error_from_errno("getcwd");
4209 goto done;
4211 error = got_worktree_open(&worktree, cwd);
4212 if (error)
4213 goto done;
4215 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4216 if (error != NULL)
4217 goto done;
4219 if (patch_script_path) {
4220 patch_script_file = fopen(patch_script_path, "r");
4221 if (patch_script_file == NULL) {
4222 error = got_error_from_errno2("fopen",
4223 patch_script_path);
4224 goto done;
4227 error = apply_unveil(got_repo_get_path(repo), 1,
4228 got_worktree_get_root_path(worktree));
4229 if (error)
4230 goto done;
4232 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4233 if (error)
4234 goto done;
4236 if (!can_recurse) {
4237 char *ondisk_path;
4238 struct stat sb;
4239 TAILQ_FOREACH(pe, &paths, entry) {
4240 if (asprintf(&ondisk_path, "%s/%s",
4241 got_worktree_get_root_path(worktree),
4242 pe->path) == -1) {
4243 error = got_error_from_errno("asprintf");
4244 goto done;
4246 if (lstat(ondisk_path, &sb) == -1) {
4247 if (errno == ENOENT) {
4248 free(ondisk_path);
4249 continue;
4251 error = got_error_from_errno2("lstat",
4252 ondisk_path);
4253 free(ondisk_path);
4254 goto done;
4256 free(ondisk_path);
4257 if (S_ISDIR(sb.st_mode)) {
4258 error = got_error_msg(GOT_ERR_BAD_PATH,
4259 "reverting directories requires -R option");
4260 goto done;
4265 cpa.patch_script_file = patch_script_file;
4266 cpa.action = "revert";
4267 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4268 pflag ? choose_patch : NULL, &cpa, repo);
4269 if (error)
4270 goto done;
4271 done:
4272 if (patch_script_file && fclose(patch_script_file) == EOF &&
4273 error == NULL)
4274 error = got_error_from_errno2("fclose", patch_script_path);
4275 if (repo)
4276 got_repo_close(repo);
4277 if (worktree)
4278 got_worktree_close(worktree);
4279 free(path);
4280 free(cwd);
4281 return error;
4284 __dead static void
4285 usage_commit(void)
4287 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4288 getprogname());
4289 exit(1);
4292 struct collect_commit_logmsg_arg {
4293 const char *cmdline_log;
4294 const char *editor;
4295 const char *worktree_path;
4296 const char *branch_name;
4297 const char *repo_path;
4298 char *logmsg_path;
4302 static const struct got_error *
4303 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4304 void *arg)
4306 char *initial_content = NULL;
4307 struct got_pathlist_entry *pe;
4308 const struct got_error *err = NULL;
4309 char *template = NULL;
4310 struct collect_commit_logmsg_arg *a = arg;
4311 int fd;
4312 size_t len;
4314 /* if a message was specified on the command line, just use it */
4315 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4316 len = strlen(a->cmdline_log) + 1;
4317 *logmsg = malloc(len + 1);
4318 if (*logmsg == NULL)
4319 return got_error_from_errno("malloc");
4320 strlcpy(*logmsg, a->cmdline_log, len);
4321 return NULL;
4324 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4325 return got_error_from_errno("asprintf");
4327 if (asprintf(&initial_content,
4328 "\n# changes to be committed on branch %s:\n",
4329 a->branch_name) == -1)
4330 return got_error_from_errno("asprintf");
4332 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4333 if (err)
4334 goto done;
4336 dprintf(fd, initial_content);
4338 TAILQ_FOREACH(pe, commitable_paths, entry) {
4339 struct got_commitable *ct = pe->data;
4340 dprintf(fd, "# %c %s\n",
4341 got_commitable_get_status(ct),
4342 got_commitable_get_path(ct));
4344 close(fd);
4346 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4347 done:
4348 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4349 unlink(a->logmsg_path);
4350 free(a->logmsg_path);
4351 a->logmsg_path = NULL;
4353 free(initial_content);
4354 free(template);
4356 /* Editor is done; we can now apply unveil(2) */
4357 if (err == NULL) {
4358 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4359 if (err) {
4360 free(*logmsg);
4361 *logmsg = NULL;
4364 return err;
4367 static const struct got_error *
4368 cmd_commit(int argc, char *argv[])
4370 const struct got_error *error = NULL;
4371 struct got_worktree *worktree = NULL;
4372 struct got_repository *repo = NULL;
4373 char *cwd = NULL, *id_str = NULL;
4374 struct got_object_id *id = NULL;
4375 const char *logmsg = NULL;
4376 const char *author;
4377 struct collect_commit_logmsg_arg cl_arg;
4378 char *editor = NULL;
4379 int ch, rebase_in_progress, histedit_in_progress;
4380 struct got_pathlist_head paths;
4382 TAILQ_INIT(&paths);
4383 cl_arg.logmsg_path = NULL;
4385 while ((ch = getopt(argc, argv, "m:")) != -1) {
4386 switch (ch) {
4387 case 'm':
4388 logmsg = optarg;
4389 break;
4390 default:
4391 usage_commit();
4392 /* NOTREACHED */
4396 argc -= optind;
4397 argv += optind;
4399 #ifndef PROFILE
4400 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4401 "unveil", NULL) == -1)
4402 err(1, "pledge");
4403 #endif
4404 error = get_author(&author);
4405 if (error)
4406 return error;
4408 cwd = getcwd(NULL, 0);
4409 if (cwd == NULL) {
4410 error = got_error_from_errno("getcwd");
4411 goto done;
4413 error = got_worktree_open(&worktree, cwd);
4414 if (error)
4415 goto done;
4417 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4418 if (error)
4419 goto done;
4420 if (rebase_in_progress) {
4421 error = got_error(GOT_ERR_REBASING);
4422 goto done;
4425 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4426 worktree);
4427 if (error)
4428 goto done;
4430 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4431 if (error != NULL)
4432 goto done;
4435 * unveil(2) traverses exec(2); if an editor is used we have
4436 * to apply unveil after the log message has been written.
4438 if (logmsg == NULL || strlen(logmsg) == 0)
4439 error = get_editor(&editor);
4440 else
4441 error = apply_unveil(got_repo_get_path(repo), 0,
4442 got_worktree_get_root_path(worktree));
4443 if (error)
4444 goto done;
4446 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4447 if (error)
4448 goto done;
4450 cl_arg.editor = editor;
4451 cl_arg.cmdline_log = logmsg;
4452 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4453 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4454 if (!histedit_in_progress) {
4455 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4456 error = got_error(GOT_ERR_COMMIT_BRANCH);
4457 goto done;
4459 cl_arg.branch_name += 11;
4461 cl_arg.repo_path = got_repo_get_path(repo);
4462 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4463 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4464 if (error) {
4465 if (cl_arg.logmsg_path)
4466 fprintf(stderr, "%s: log message preserved in %s\n",
4467 getprogname(), cl_arg.logmsg_path);
4468 goto done;
4471 if (cl_arg.logmsg_path)
4472 unlink(cl_arg.logmsg_path);
4474 error = got_object_id_str(&id_str, id);
4475 if (error)
4476 goto done;
4477 printf("Created commit %s\n", id_str);
4478 done:
4479 free(cl_arg.logmsg_path);
4480 if (repo)
4481 got_repo_close(repo);
4482 if (worktree)
4483 got_worktree_close(worktree);
4484 free(cwd);
4485 free(id_str);
4486 free(editor);
4487 return error;
4490 __dead static void
4491 usage_cherrypick(void)
4493 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4494 exit(1);
4497 static const struct got_error *
4498 cmd_cherrypick(int argc, char *argv[])
4500 const struct got_error *error = NULL;
4501 struct got_worktree *worktree = NULL;
4502 struct got_repository *repo = NULL;
4503 char *cwd = NULL, *commit_id_str = NULL;
4504 struct got_object_id *commit_id = NULL;
4505 struct got_commit_object *commit = NULL;
4506 struct got_object_qid *pid;
4507 struct got_reference *head_ref = NULL;
4508 int ch, did_something = 0;
4510 while ((ch = getopt(argc, argv, "")) != -1) {
4511 switch (ch) {
4512 default:
4513 usage_cherrypick();
4514 /* NOTREACHED */
4518 argc -= optind;
4519 argv += optind;
4521 #ifndef PROFILE
4522 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4523 "unveil", NULL) == -1)
4524 err(1, "pledge");
4525 #endif
4526 if (argc != 1)
4527 usage_cherrypick();
4529 cwd = getcwd(NULL, 0);
4530 if (cwd == NULL) {
4531 error = got_error_from_errno("getcwd");
4532 goto done;
4534 error = got_worktree_open(&worktree, cwd);
4535 if (error)
4536 goto done;
4538 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4539 if (error != NULL)
4540 goto done;
4542 error = apply_unveil(got_repo_get_path(repo), 0,
4543 got_worktree_get_root_path(worktree));
4544 if (error)
4545 goto done;
4547 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4548 GOT_OBJ_TYPE_COMMIT, repo);
4549 if (error != NULL) {
4550 struct got_reference *ref;
4551 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4552 goto done;
4553 error = got_ref_open(&ref, repo, argv[0], 0);
4554 if (error != NULL)
4555 goto done;
4556 error = got_ref_resolve(&commit_id, repo, ref);
4557 got_ref_close(ref);
4558 if (error != NULL)
4559 goto done;
4561 error = got_object_id_str(&commit_id_str, commit_id);
4562 if (error)
4563 goto done;
4565 error = got_ref_open(&head_ref, repo,
4566 got_worktree_get_head_ref_name(worktree), 0);
4567 if (error != NULL)
4568 goto done;
4570 error = check_same_branch(commit_id, head_ref, NULL, repo);
4571 if (error) {
4572 if (error->code != GOT_ERR_ANCESTRY)
4573 goto done;
4574 error = NULL;
4575 } else {
4576 error = got_error(GOT_ERR_SAME_BRANCH);
4577 goto done;
4580 error = got_object_open_as_commit(&commit, repo, commit_id);
4581 if (error)
4582 goto done;
4583 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4584 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4585 commit_id, repo, update_progress, &did_something, check_cancelled,
4586 NULL);
4587 if (error != NULL)
4588 goto done;
4590 if (did_something)
4591 printf("Merged commit %s\n", commit_id_str);
4592 done:
4593 if (commit)
4594 got_object_commit_close(commit);
4595 free(commit_id_str);
4596 if (head_ref)
4597 got_ref_close(head_ref);
4598 if (worktree)
4599 got_worktree_close(worktree);
4600 if (repo)
4601 got_repo_close(repo);
4602 return error;
4605 __dead static void
4606 usage_backout(void)
4608 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4609 exit(1);
4612 static const struct got_error *
4613 cmd_backout(int argc, char *argv[])
4615 const struct got_error *error = NULL;
4616 struct got_worktree *worktree = NULL;
4617 struct got_repository *repo = NULL;
4618 char *cwd = NULL, *commit_id_str = NULL;
4619 struct got_object_id *commit_id = NULL;
4620 struct got_commit_object *commit = NULL;
4621 struct got_object_qid *pid;
4622 struct got_reference *head_ref = NULL;
4623 int ch, did_something = 0;
4625 while ((ch = getopt(argc, argv, "")) != -1) {
4626 switch (ch) {
4627 default:
4628 usage_backout();
4629 /* NOTREACHED */
4633 argc -= optind;
4634 argv += optind;
4636 #ifndef PROFILE
4637 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4638 "unveil", NULL) == -1)
4639 err(1, "pledge");
4640 #endif
4641 if (argc != 1)
4642 usage_backout();
4644 cwd = getcwd(NULL, 0);
4645 if (cwd == NULL) {
4646 error = got_error_from_errno("getcwd");
4647 goto done;
4649 error = got_worktree_open(&worktree, cwd);
4650 if (error)
4651 goto done;
4653 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4654 if (error != NULL)
4655 goto done;
4657 error = apply_unveil(got_repo_get_path(repo), 0,
4658 got_worktree_get_root_path(worktree));
4659 if (error)
4660 goto done;
4662 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4663 GOT_OBJ_TYPE_COMMIT, repo);
4664 if (error != NULL) {
4665 struct got_reference *ref;
4666 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4667 goto done;
4668 error = got_ref_open(&ref, repo, argv[0], 0);
4669 if (error != NULL)
4670 goto done;
4671 error = got_ref_resolve(&commit_id, repo, ref);
4672 got_ref_close(ref);
4673 if (error != NULL)
4674 goto done;
4676 error = got_object_id_str(&commit_id_str, commit_id);
4677 if (error)
4678 goto done;
4680 error = got_ref_open(&head_ref, repo,
4681 got_worktree_get_head_ref_name(worktree), 0);
4682 if (error != NULL)
4683 goto done;
4685 error = check_same_branch(commit_id, head_ref, NULL, repo);
4686 if (error)
4687 goto done;
4689 error = got_object_open_as_commit(&commit, repo, commit_id);
4690 if (error)
4691 goto done;
4692 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4693 if (pid == NULL) {
4694 error = got_error(GOT_ERR_ROOT_COMMIT);
4695 goto done;
4698 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4699 update_progress, &did_something, check_cancelled, NULL);
4700 if (error != NULL)
4701 goto done;
4703 if (did_something)
4704 printf("Backed out commit %s\n", commit_id_str);
4705 done:
4706 if (commit)
4707 got_object_commit_close(commit);
4708 free(commit_id_str);
4709 if (head_ref)
4710 got_ref_close(head_ref);
4711 if (worktree)
4712 got_worktree_close(worktree);
4713 if (repo)
4714 got_repo_close(repo);
4715 return error;
4718 __dead static void
4719 usage_rebase(void)
4721 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4722 getprogname());
4723 exit(1);
4726 void
4727 trim_logmsg(char *logmsg, int limit)
4729 char *nl;
4730 size_t len;
4732 len = strlen(logmsg);
4733 if (len > limit)
4734 len = limit;
4735 logmsg[len] = '\0';
4736 nl = strchr(logmsg, '\n');
4737 if (nl)
4738 *nl = '\0';
4741 static const struct got_error *
4742 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4744 const struct got_error *err;
4745 char *logmsg0 = NULL;
4746 const char *s;
4748 err = got_object_commit_get_logmsg(&logmsg0, commit);
4749 if (err)
4750 return err;
4752 s = logmsg0;
4753 while (isspace((unsigned char)s[0]))
4754 s++;
4756 *logmsg = strdup(s);
4757 if (*logmsg == NULL) {
4758 err = got_error_from_errno("strdup");
4759 goto done;
4762 trim_logmsg(*logmsg, limit);
4763 done:
4764 free(logmsg0);
4765 return err;
4768 static const struct got_error *
4769 show_rebase_progress(struct got_commit_object *commit,
4770 struct got_object_id *old_id, struct got_object_id *new_id)
4772 const struct got_error *err;
4773 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4775 err = got_object_id_str(&old_id_str, old_id);
4776 if (err)
4777 goto done;
4779 if (new_id) {
4780 err = got_object_id_str(&new_id_str, new_id);
4781 if (err)
4782 goto done;
4785 old_id_str[12] = '\0';
4786 if (new_id_str)
4787 new_id_str[12] = '\0';
4789 err = get_short_logmsg(&logmsg, 42, commit);
4790 if (err)
4791 goto done;
4793 printf("%s -> %s: %s\n", old_id_str,
4794 new_id_str ? new_id_str : "no-op change", logmsg);
4795 done:
4796 free(old_id_str);
4797 free(new_id_str);
4798 return err;
4801 static const struct got_error *
4802 rebase_progress(void *arg, unsigned char status, const char *path)
4804 unsigned char *rebase_status = arg;
4806 while (path[0] == '/')
4807 path++;
4808 printf("%c %s\n", status, path);
4810 if (*rebase_status == GOT_STATUS_CONFLICT)
4811 return NULL;
4812 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4813 *rebase_status = status;
4814 return NULL;
4817 static const struct got_error *
4818 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4819 struct got_reference *branch, struct got_reference *new_base_branch,
4820 struct got_reference *tmp_branch, struct got_repository *repo)
4822 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4823 return got_worktree_rebase_complete(worktree, fileindex,
4824 new_base_branch, tmp_branch, branch, repo);
4827 static const struct got_error *
4828 rebase_commit(struct got_pathlist_head *merged_paths,
4829 struct got_worktree *worktree, struct got_fileindex *fileindex,
4830 struct got_reference *tmp_branch,
4831 struct got_object_id *commit_id, struct got_repository *repo)
4833 const struct got_error *error;
4834 struct got_commit_object *commit;
4835 struct got_object_id *new_commit_id;
4837 error = got_object_open_as_commit(&commit, repo, commit_id);
4838 if (error)
4839 return error;
4841 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4842 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4843 if (error) {
4844 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4845 goto done;
4846 error = show_rebase_progress(commit, commit_id, NULL);
4847 } else {
4848 error = show_rebase_progress(commit, commit_id, new_commit_id);
4849 free(new_commit_id);
4851 done:
4852 got_object_commit_close(commit);
4853 return error;
4856 struct check_path_prefix_arg {
4857 const char *path_prefix;
4858 size_t len;
4859 int errcode;
4862 static const struct got_error *
4863 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4864 struct got_blob_object *blob2, struct got_object_id *id1,
4865 struct got_object_id *id2, const char *path1, const char *path2,
4866 struct got_repository *repo)
4868 struct check_path_prefix_arg *a = arg;
4870 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4871 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4872 return got_error(a->errcode);
4874 return NULL;
4877 static const struct got_error *
4878 check_path_prefix(struct got_object_id *parent_id,
4879 struct got_object_id *commit_id, const char *path_prefix,
4880 int errcode, struct got_repository *repo)
4882 const struct got_error *err;
4883 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4884 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4885 struct check_path_prefix_arg cpp_arg;
4887 if (got_path_is_root_dir(path_prefix))
4888 return NULL;
4890 err = got_object_open_as_commit(&commit, repo, commit_id);
4891 if (err)
4892 goto done;
4894 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4895 if (err)
4896 goto done;
4898 err = got_object_open_as_tree(&tree1, repo,
4899 got_object_commit_get_tree_id(parent_commit));
4900 if (err)
4901 goto done;
4903 err = got_object_open_as_tree(&tree2, repo,
4904 got_object_commit_get_tree_id(commit));
4905 if (err)
4906 goto done;
4908 cpp_arg.path_prefix = path_prefix;
4909 while (cpp_arg.path_prefix[0] == '/')
4910 cpp_arg.path_prefix++;
4911 cpp_arg.len = strlen(cpp_arg.path_prefix);
4912 cpp_arg.errcode = errcode;
4913 err = got_diff_tree(tree1, tree2, "", "", repo,
4914 check_path_prefix_in_diff, &cpp_arg, 0);
4915 done:
4916 if (tree1)
4917 got_object_tree_close(tree1);
4918 if (tree2)
4919 got_object_tree_close(tree2);
4920 if (commit)
4921 got_object_commit_close(commit);
4922 if (parent_commit)
4923 got_object_commit_close(parent_commit);
4924 return err;
4927 static const struct got_error *
4928 collect_commits(struct got_object_id_queue *commits,
4929 struct got_object_id *initial_commit_id,
4930 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4931 const char *path_prefix, int path_prefix_errcode,
4932 struct got_repository *repo)
4934 const struct got_error *err = NULL;
4935 struct got_commit_graph *graph = NULL;
4936 struct got_object_id *parent_id = NULL;
4937 struct got_object_qid *qid;
4938 struct got_object_id *commit_id = initial_commit_id;
4940 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4941 if (err)
4942 return err;
4944 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4945 check_cancelled, NULL);
4946 if (err)
4947 goto done;
4948 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4949 err = got_commit_graph_iter_next(&parent_id, graph);
4950 if (err) {
4951 if (err->code == GOT_ERR_ITER_COMPLETED) {
4952 err = got_error_msg(GOT_ERR_ANCESTRY,
4953 "ran out of commits to rebase before "
4954 "youngest common ancestor commit has "
4955 "been reached?!?");
4956 goto done;
4957 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4958 goto done;
4959 err = got_commit_graph_fetch_commits(graph, 1, repo,
4960 check_cancelled, NULL);
4961 if (err)
4962 goto done;
4963 } else {
4964 err = check_path_prefix(parent_id, commit_id,
4965 path_prefix, path_prefix_errcode, repo);
4966 if (err)
4967 goto done;
4969 err = got_object_qid_alloc(&qid, commit_id);
4970 if (err)
4971 goto done;
4972 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4973 commit_id = parent_id;
4976 done:
4977 got_commit_graph_close(graph);
4978 return err;
4981 static const struct got_error *
4982 cmd_rebase(int argc, char *argv[])
4984 const struct got_error *error = NULL;
4985 struct got_worktree *worktree = NULL;
4986 struct got_repository *repo = NULL;
4987 struct got_fileindex *fileindex = NULL;
4988 char *cwd = NULL;
4989 struct got_reference *branch = NULL;
4990 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4991 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4992 struct got_object_id *resume_commit_id = NULL;
4993 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4994 struct got_commit_object *commit = NULL;
4995 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4996 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4997 struct got_object_id_queue commits;
4998 struct got_pathlist_head merged_paths;
4999 const struct got_object_id_queue *parent_ids;
5000 struct got_object_qid *qid, *pid;
5002 SIMPLEQ_INIT(&commits);
5003 TAILQ_INIT(&merged_paths);
5005 while ((ch = getopt(argc, argv, "ac")) != -1) {
5006 switch (ch) {
5007 case 'a':
5008 abort_rebase = 1;
5009 break;
5010 case 'c':
5011 continue_rebase = 1;
5012 break;
5013 default:
5014 usage_rebase();
5015 /* NOTREACHED */
5019 argc -= optind;
5020 argv += optind;
5022 #ifndef PROFILE
5023 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5024 "unveil", NULL) == -1)
5025 err(1, "pledge");
5026 #endif
5027 if (abort_rebase && continue_rebase)
5028 usage_rebase();
5029 else if (abort_rebase || continue_rebase) {
5030 if (argc != 0)
5031 usage_rebase();
5032 } else if (argc != 1)
5033 usage_rebase();
5035 cwd = getcwd(NULL, 0);
5036 if (cwd == NULL) {
5037 error = got_error_from_errno("getcwd");
5038 goto done;
5040 error = got_worktree_open(&worktree, cwd);
5041 if (error)
5042 goto done;
5044 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5045 if (error != NULL)
5046 goto done;
5048 error = apply_unveil(got_repo_get_path(repo), 0,
5049 got_worktree_get_root_path(worktree));
5050 if (error)
5051 goto done;
5053 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5054 if (error)
5055 goto done;
5057 if (abort_rebase) {
5058 int did_something;
5059 if (!rebase_in_progress) {
5060 error = got_error(GOT_ERR_NOT_REBASING);
5061 goto done;
5063 error = got_worktree_rebase_continue(&resume_commit_id,
5064 &new_base_branch, &tmp_branch, &branch, &fileindex,
5065 worktree, repo);
5066 if (error)
5067 goto done;
5068 printf("Switching work tree to %s\n",
5069 got_ref_get_symref_target(new_base_branch));
5070 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5071 new_base_branch, update_progress, &did_something);
5072 if (error)
5073 goto done;
5074 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5075 goto done; /* nothing else to do */
5078 if (continue_rebase) {
5079 if (!rebase_in_progress) {
5080 error = got_error(GOT_ERR_NOT_REBASING);
5081 goto done;
5083 error = got_worktree_rebase_continue(&resume_commit_id,
5084 &new_base_branch, &tmp_branch, &branch, &fileindex,
5085 worktree, repo);
5086 if (error)
5087 goto done;
5089 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5090 resume_commit_id, repo);
5091 if (error)
5092 goto done;
5094 yca_id = got_object_id_dup(resume_commit_id);
5095 if (yca_id == NULL) {
5096 error = got_error_from_errno("got_object_id_dup");
5097 goto done;
5099 } else {
5100 error = got_ref_open(&branch, repo, argv[0], 0);
5101 if (error != NULL)
5102 goto done;
5105 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5106 if (error)
5107 goto done;
5109 if (!continue_rebase) {
5110 struct got_object_id *base_commit_id;
5112 base_commit_id = got_worktree_get_base_commit_id(worktree);
5113 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5114 base_commit_id, branch_head_commit_id, repo,
5115 check_cancelled, NULL);
5116 if (error)
5117 goto done;
5118 if (yca_id == NULL) {
5119 error = got_error_msg(GOT_ERR_ANCESTRY,
5120 "specified branch shares no common ancestry "
5121 "with work tree's branch");
5122 goto done;
5125 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5126 if (error) {
5127 if (error->code != GOT_ERR_ANCESTRY)
5128 goto done;
5129 error = NULL;
5130 } else {
5131 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5132 "specified branch resolves to a commit which "
5133 "is already contained in work tree's branch");
5134 goto done;
5136 error = got_worktree_rebase_prepare(&new_base_branch,
5137 &tmp_branch, &fileindex, worktree, branch, repo);
5138 if (error)
5139 goto done;
5142 commit_id = branch_head_commit_id;
5143 error = got_object_open_as_commit(&commit, repo, commit_id);
5144 if (error)
5145 goto done;
5147 parent_ids = got_object_commit_get_parent_ids(commit);
5148 pid = SIMPLEQ_FIRST(parent_ids);
5149 if (pid == NULL) {
5150 if (!continue_rebase) {
5151 int did_something;
5152 error = got_worktree_rebase_abort(worktree, fileindex,
5153 repo, new_base_branch, update_progress,
5154 &did_something);
5155 if (error)
5156 goto done;
5157 printf("Rebase of %s aborted\n",
5158 got_ref_get_name(branch));
5160 error = got_error(GOT_ERR_EMPTY_REBASE);
5161 goto done;
5163 error = collect_commits(&commits, commit_id, pid->id,
5164 yca_id, got_worktree_get_path_prefix(worktree),
5165 GOT_ERR_REBASE_PATH, repo);
5166 got_object_commit_close(commit);
5167 commit = NULL;
5168 if (error)
5169 goto done;
5171 if (SIMPLEQ_EMPTY(&commits)) {
5172 if (continue_rebase)
5173 error = rebase_complete(worktree, fileindex,
5174 branch, new_base_branch, tmp_branch, repo);
5175 else
5176 error = got_error(GOT_ERR_EMPTY_REBASE);
5177 goto done;
5180 pid = NULL;
5181 SIMPLEQ_FOREACH(qid, &commits, entry) {
5182 commit_id = qid->id;
5183 parent_id = pid ? pid->id : yca_id;
5184 pid = qid;
5186 error = got_worktree_rebase_merge_files(&merged_paths,
5187 worktree, fileindex, parent_id, commit_id, repo,
5188 rebase_progress, &rebase_status, check_cancelled, NULL);
5189 if (error)
5190 goto done;
5192 if (rebase_status == GOT_STATUS_CONFLICT) {
5193 got_worktree_rebase_pathlist_free(&merged_paths);
5194 break;
5197 error = rebase_commit(&merged_paths, worktree, fileindex,
5198 tmp_branch, commit_id, repo);
5199 got_worktree_rebase_pathlist_free(&merged_paths);
5200 if (error)
5201 goto done;
5204 if (rebase_status == GOT_STATUS_CONFLICT) {
5205 error = got_worktree_rebase_postpone(worktree, fileindex);
5206 if (error)
5207 goto done;
5208 error = got_error_msg(GOT_ERR_CONFLICTS,
5209 "conflicts must be resolved before rebasing can continue");
5210 } else
5211 error = rebase_complete(worktree, fileindex, branch,
5212 new_base_branch, tmp_branch, repo);
5213 done:
5214 got_object_id_queue_free(&commits);
5215 free(branch_head_commit_id);
5216 free(resume_commit_id);
5217 free(yca_id);
5218 if (commit)
5219 got_object_commit_close(commit);
5220 if (branch)
5221 got_ref_close(branch);
5222 if (new_base_branch)
5223 got_ref_close(new_base_branch);
5224 if (tmp_branch)
5225 got_ref_close(tmp_branch);
5226 if (worktree)
5227 got_worktree_close(worktree);
5228 if (repo)
5229 got_repo_close(repo);
5230 return error;
5233 __dead static void
5234 usage_histedit(void)
5236 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5237 getprogname());
5238 exit(1);
5241 #define GOT_HISTEDIT_PICK 'p'
5242 #define GOT_HISTEDIT_EDIT 'e'
5243 #define GOT_HISTEDIT_FOLD 'f'
5244 #define GOT_HISTEDIT_DROP 'd'
5245 #define GOT_HISTEDIT_MESG 'm'
5247 static struct got_histedit_cmd {
5248 unsigned char code;
5249 const char *name;
5250 const char *desc;
5251 } got_histedit_cmds[] = {
5252 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5253 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5254 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5255 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5256 { GOT_HISTEDIT_MESG, "mesg",
5257 "single-line log message for commit above (open editor if empty)" },
5260 struct got_histedit_list_entry {
5261 TAILQ_ENTRY(got_histedit_list_entry) entry;
5262 struct got_object_id *commit_id;
5263 const struct got_histedit_cmd *cmd;
5264 char *logmsg;
5266 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5268 static const struct got_error *
5269 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5270 FILE *f, struct got_repository *repo)
5272 const struct got_error *err = NULL;
5273 char *logmsg = NULL, *id_str = NULL;
5274 struct got_commit_object *commit = NULL;
5275 int n;
5277 err = got_object_open_as_commit(&commit, repo, commit_id);
5278 if (err)
5279 goto done;
5281 err = get_short_logmsg(&logmsg, 34, commit);
5282 if (err)
5283 goto done;
5285 err = got_object_id_str(&id_str, commit_id);
5286 if (err)
5287 goto done;
5289 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5290 if (n < 0)
5291 err = got_ferror(f, GOT_ERR_IO);
5292 done:
5293 if (commit)
5294 got_object_commit_close(commit);
5295 free(id_str);
5296 free(logmsg);
5297 return err;
5300 static const struct got_error *
5301 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5302 struct got_repository *repo)
5304 const struct got_error *err = NULL;
5305 struct got_object_qid *qid;
5307 if (SIMPLEQ_EMPTY(commits))
5308 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5310 SIMPLEQ_FOREACH(qid, commits, entry) {
5311 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5312 f, repo);
5313 if (err)
5314 break;
5317 return err;
5320 static const struct got_error *
5321 write_cmd_list(FILE *f)
5323 const struct got_error *err = NULL;
5324 int n, i;
5326 n = fprintf(f, "# Available histedit commands:\n");
5327 if (n < 0)
5328 return got_ferror(f, GOT_ERR_IO);
5330 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5331 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5332 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5333 cmd->desc);
5334 if (n < 0) {
5335 err = got_ferror(f, GOT_ERR_IO);
5336 break;
5339 n = fprintf(f, "# Commits will be processed in order from top to "
5340 "bottom of this file.\n");
5341 if (n < 0)
5342 return got_ferror(f, GOT_ERR_IO);
5343 return err;
5346 static const struct got_error *
5347 histedit_syntax_error(int lineno)
5349 static char msg[42];
5350 int ret;
5352 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5353 lineno);
5354 if (ret == -1 || ret >= sizeof(msg))
5355 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5357 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5360 static const struct got_error *
5361 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5362 char *logmsg, struct got_repository *repo)
5364 const struct got_error *err;
5365 struct got_commit_object *folded_commit = NULL;
5366 char *id_str, *folded_logmsg = NULL;
5368 err = got_object_id_str(&id_str, hle->commit_id);
5369 if (err)
5370 return err;
5372 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5373 if (err)
5374 goto done;
5376 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5377 if (err)
5378 goto done;
5379 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5380 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5381 folded_logmsg) == -1) {
5382 err = got_error_from_errno("asprintf");
5383 goto done;
5385 done:
5386 if (folded_commit)
5387 got_object_commit_close(folded_commit);
5388 free(id_str);
5389 free(folded_logmsg);
5390 return err;
5393 static struct got_histedit_list_entry *
5394 get_folded_commits(struct got_histedit_list_entry *hle)
5396 struct got_histedit_list_entry *prev, *folded = NULL;
5398 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5399 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5400 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5401 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5402 folded = prev;
5403 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5406 return folded;
5409 static const struct got_error *
5410 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5411 struct got_repository *repo)
5413 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5414 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5415 const struct got_error *err = NULL;
5416 struct got_commit_object *commit = NULL;
5417 int fd;
5418 struct got_histedit_list_entry *folded = NULL;
5420 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5421 if (err)
5422 return err;
5424 folded = get_folded_commits(hle);
5425 if (folded) {
5426 while (folded != hle) {
5427 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5428 folded = TAILQ_NEXT(folded, entry);
5429 continue;
5431 err = append_folded_commit_msg(&new_msg, folded,
5432 logmsg, repo);
5433 if (err)
5434 goto done;
5435 free(logmsg);
5436 logmsg = new_msg;
5437 folded = TAILQ_NEXT(folded, entry);
5441 err = got_object_id_str(&id_str, hle->commit_id);
5442 if (err)
5443 goto done;
5444 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5445 if (err)
5446 goto done;
5447 if (asprintf(&new_msg,
5448 "%s\n# original log message of commit %s: %s",
5449 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5450 err = got_error_from_errno("asprintf");
5451 goto done;
5453 free(logmsg);
5454 logmsg = new_msg;
5456 err = got_object_id_str(&id_str, hle->commit_id);
5457 if (err)
5458 goto done;
5460 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5461 if (err)
5462 goto done;
5464 dprintf(fd, logmsg);
5465 close(fd);
5467 err = get_editor(&editor);
5468 if (err)
5469 goto done;
5471 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5472 if (err) {
5473 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5474 goto done;
5475 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5477 done:
5478 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5479 err = got_error_from_errno2("unlink", logmsg_path);
5480 free(logmsg_path);
5481 free(logmsg);
5482 free(orig_logmsg);
5483 free(editor);
5484 if (commit)
5485 got_object_commit_close(commit);
5486 return err;
5489 static const struct got_error *
5490 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5491 FILE *f, struct got_repository *repo)
5493 const struct got_error *err = NULL;
5494 char *line = NULL, *p, *end;
5495 size_t size;
5496 ssize_t len;
5497 int lineno = 0, i;
5498 const struct got_histedit_cmd *cmd;
5499 struct got_object_id *commit_id = NULL;
5500 struct got_histedit_list_entry *hle = NULL;
5502 for (;;) {
5503 len = getline(&line, &size, f);
5504 if (len == -1) {
5505 const struct got_error *getline_err;
5506 if (feof(f))
5507 break;
5508 getline_err = got_error_from_errno("getline");
5509 err = got_ferror(f, getline_err->code);
5510 break;
5512 lineno++;
5513 p = line;
5514 while (isspace((unsigned char)p[0]))
5515 p++;
5516 if (p[0] == '#' || p[0] == '\0') {
5517 free(line);
5518 line = NULL;
5519 continue;
5521 cmd = NULL;
5522 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5523 cmd = &got_histedit_cmds[i];
5524 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5525 isspace((unsigned char)p[strlen(cmd->name)])) {
5526 p += strlen(cmd->name);
5527 break;
5529 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5530 p++;
5531 break;
5534 if (i == nitems(got_histedit_cmds)) {
5535 err = histedit_syntax_error(lineno);
5536 break;
5538 while (isspace((unsigned char)p[0]))
5539 p++;
5540 if (cmd->code == GOT_HISTEDIT_MESG) {
5541 if (hle == NULL || hle->logmsg != NULL) {
5542 err = got_error(GOT_ERR_HISTEDIT_CMD);
5543 break;
5545 if (p[0] == '\0') {
5546 err = histedit_edit_logmsg(hle, repo);
5547 if (err)
5548 break;
5549 } else {
5550 hle->logmsg = strdup(p);
5551 if (hle->logmsg == NULL) {
5552 err = got_error_from_errno("strdup");
5553 break;
5556 free(line);
5557 line = NULL;
5558 continue;
5559 } else {
5560 end = p;
5561 while (end[0] && !isspace((unsigned char)end[0]))
5562 end++;
5563 *end = '\0';
5565 err = got_object_resolve_id_str(&commit_id, repo, p);
5566 if (err) {
5567 /* override error code */
5568 err = histedit_syntax_error(lineno);
5569 break;
5572 hle = malloc(sizeof(*hle));
5573 if (hle == NULL) {
5574 err = got_error_from_errno("malloc");
5575 break;
5577 hle->cmd = cmd;
5578 hle->commit_id = commit_id;
5579 hle->logmsg = NULL;
5580 commit_id = NULL;
5581 free(line);
5582 line = NULL;
5583 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5586 free(line);
5587 free(commit_id);
5588 return err;
5591 static const struct got_error *
5592 histedit_check_script(struct got_histedit_list *histedit_cmds,
5593 struct got_object_id_queue *commits, struct got_repository *repo)
5595 const struct got_error *err = NULL;
5596 struct got_object_qid *qid;
5597 struct got_histedit_list_entry *hle;
5598 static char msg[80];
5599 char *id_str;
5601 if (TAILQ_EMPTY(histedit_cmds))
5602 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5603 "histedit script contains no commands");
5604 if (SIMPLEQ_EMPTY(commits))
5605 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5607 SIMPLEQ_FOREACH(qid, commits, entry) {
5608 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5609 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5610 break;
5612 if (hle == NULL) {
5613 err = got_object_id_str(&id_str, qid->id);
5614 if (err)
5615 return err;
5616 snprintf(msg, sizeof(msg),
5617 "commit %s missing from histedit script", id_str);
5618 free(id_str);
5619 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5623 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5624 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5625 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5626 "last commit in histedit script cannot be folded");
5628 return NULL;
5631 static const struct got_error *
5632 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5633 const char *path, struct got_object_id_queue *commits,
5634 struct got_repository *repo)
5636 const struct got_error *err = NULL;
5637 char *editor;
5638 FILE *f = NULL;
5640 err = get_editor(&editor);
5641 if (err)
5642 return err;
5644 if (spawn_editor(editor, path) == -1) {
5645 err = got_error_from_errno("failed spawning editor");
5646 goto done;
5649 f = fopen(path, "r");
5650 if (f == NULL) {
5651 err = got_error_from_errno("fopen");
5652 goto done;
5654 err = histedit_parse_list(histedit_cmds, f, repo);
5655 if (err)
5656 goto done;
5658 err = histedit_check_script(histedit_cmds, commits, repo);
5659 done:
5660 if (f && fclose(f) != 0 && err == NULL)
5661 err = got_error_from_errno("fclose");
5662 free(editor);
5663 return err;
5666 static const struct got_error *
5667 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5668 struct got_object_id_queue *, const char *, struct got_repository *);
5670 static const struct got_error *
5671 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5672 struct got_object_id_queue *commits, struct got_repository *repo)
5674 const struct got_error *err;
5675 FILE *f = NULL;
5676 char *path = NULL;
5678 err = got_opentemp_named(&path, &f, "got-histedit");
5679 if (err)
5680 return err;
5682 err = write_cmd_list(f);
5683 if (err)
5684 goto done;
5686 err = histedit_write_commit_list(commits, f, repo);
5687 if (err)
5688 goto done;
5690 if (fclose(f) != 0) {
5691 err = got_error_from_errno("fclose");
5692 goto done;
5694 f = NULL;
5696 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5697 if (err) {
5698 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5699 err->code != GOT_ERR_HISTEDIT_CMD)
5700 goto done;
5701 err = histedit_edit_list_retry(histedit_cmds, err,
5702 commits, path, repo);
5704 done:
5705 if (f && fclose(f) != 0 && err == NULL)
5706 err = got_error_from_errno("fclose");
5707 if (path && unlink(path) != 0 && err == NULL)
5708 err = got_error_from_errno2("unlink", path);
5709 free(path);
5710 return err;
5713 static const struct got_error *
5714 histedit_save_list(struct got_histedit_list *histedit_cmds,
5715 struct got_worktree *worktree, struct got_repository *repo)
5717 const struct got_error *err = NULL;
5718 char *path = NULL;
5719 FILE *f = NULL;
5720 struct got_histedit_list_entry *hle;
5721 struct got_commit_object *commit = NULL;
5723 err = got_worktree_get_histedit_script_path(&path, worktree);
5724 if (err)
5725 return err;
5727 f = fopen(path, "w");
5728 if (f == NULL) {
5729 err = got_error_from_errno2("fopen", path);
5730 goto done;
5732 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5733 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5734 repo);
5735 if (err)
5736 break;
5738 if (hle->logmsg) {
5739 int n = fprintf(f, "%c %s\n",
5740 GOT_HISTEDIT_MESG, hle->logmsg);
5741 if (n < 0) {
5742 err = got_ferror(f, GOT_ERR_IO);
5743 break;
5747 done:
5748 if (f && fclose(f) != 0 && err == NULL)
5749 err = got_error_from_errno("fclose");
5750 free(path);
5751 if (commit)
5752 got_object_commit_close(commit);
5753 return err;
5756 void
5757 histedit_free_list(struct got_histedit_list *histedit_cmds)
5759 struct got_histedit_list_entry *hle;
5761 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5762 TAILQ_REMOVE(histedit_cmds, hle, entry);
5763 free(hle);
5767 static const struct got_error *
5768 histedit_load_list(struct got_histedit_list *histedit_cmds,
5769 const char *path, struct got_repository *repo)
5771 const struct got_error *err = NULL;
5772 FILE *f = NULL;
5774 f = fopen(path, "r");
5775 if (f == NULL) {
5776 err = got_error_from_errno2("fopen", path);
5777 goto done;
5780 err = histedit_parse_list(histedit_cmds, f, repo);
5781 done:
5782 if (f && fclose(f) != 0 && err == NULL)
5783 err = got_error_from_errno("fclose");
5784 return err;
5787 static const struct got_error *
5788 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5789 const struct got_error *edit_err, struct got_object_id_queue *commits,
5790 const char *path, struct got_repository *repo)
5792 const struct got_error *err = NULL, *prev_err = edit_err;
5793 int resp = ' ';
5795 while (resp != 'c' && resp != 'r' && resp != 'a') {
5796 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5797 "or (a)bort: ", getprogname(), prev_err->msg);
5798 resp = getchar();
5799 if (resp == '\n')
5800 resp = getchar();
5801 if (resp == 'c') {
5802 histedit_free_list(histedit_cmds);
5803 err = histedit_run_editor(histedit_cmds, path, commits,
5804 repo);
5805 if (err) {
5806 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5807 err->code != GOT_ERR_HISTEDIT_CMD)
5808 break;
5809 prev_err = err;
5810 resp = ' ';
5811 continue;
5813 break;
5814 } else if (resp == 'r') {
5815 histedit_free_list(histedit_cmds);
5816 err = histedit_edit_script(histedit_cmds,
5817 commits, repo);
5818 if (err) {
5819 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5820 err->code != GOT_ERR_HISTEDIT_CMD)
5821 break;
5822 prev_err = err;
5823 resp = ' ';
5824 continue;
5826 break;
5827 } else if (resp == 'a') {
5828 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5829 break;
5830 } else
5831 printf("invalid response '%c'\n", resp);
5834 return err;
5837 static const struct got_error *
5838 histedit_complete(struct got_worktree *worktree,
5839 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5840 struct got_reference *branch, struct got_repository *repo)
5842 printf("Switching work tree to %s\n",
5843 got_ref_get_symref_target(branch));
5844 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5845 branch, repo);
5848 static const struct got_error *
5849 show_histedit_progress(struct got_commit_object *commit,
5850 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5852 const struct got_error *err;
5853 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5855 err = got_object_id_str(&old_id_str, hle->commit_id);
5856 if (err)
5857 goto done;
5859 if (new_id) {
5860 err = got_object_id_str(&new_id_str, new_id);
5861 if (err)
5862 goto done;
5865 old_id_str[12] = '\0';
5866 if (new_id_str)
5867 new_id_str[12] = '\0';
5869 if (hle->logmsg) {
5870 logmsg = strdup(hle->logmsg);
5871 if (logmsg == NULL) {
5872 err = got_error_from_errno("strdup");
5873 goto done;
5875 trim_logmsg(logmsg, 42);
5876 } else {
5877 err = get_short_logmsg(&logmsg, 42, commit);
5878 if (err)
5879 goto done;
5882 switch (hle->cmd->code) {
5883 case GOT_HISTEDIT_PICK:
5884 case GOT_HISTEDIT_EDIT:
5885 printf("%s -> %s: %s\n", old_id_str,
5886 new_id_str ? new_id_str : "no-op change", logmsg);
5887 break;
5888 case GOT_HISTEDIT_DROP:
5889 case GOT_HISTEDIT_FOLD:
5890 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5891 logmsg);
5892 break;
5893 default:
5894 break;
5897 done:
5898 free(old_id_str);
5899 free(new_id_str);
5900 return err;
5903 static const struct got_error *
5904 histedit_commit(struct got_pathlist_head *merged_paths,
5905 struct got_worktree *worktree, struct got_fileindex *fileindex,
5906 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5907 struct got_repository *repo)
5909 const struct got_error *err;
5910 struct got_commit_object *commit;
5911 struct got_object_id *new_commit_id;
5913 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5914 && hle->logmsg == NULL) {
5915 err = histedit_edit_logmsg(hle, repo);
5916 if (err)
5917 return err;
5920 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5921 if (err)
5922 return err;
5924 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5925 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5926 hle->logmsg, repo);
5927 if (err) {
5928 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5929 goto done;
5930 err = show_histedit_progress(commit, hle, NULL);
5931 } else {
5932 err = show_histedit_progress(commit, hle, new_commit_id);
5933 free(new_commit_id);
5935 done:
5936 got_object_commit_close(commit);
5937 return err;
5940 static const struct got_error *
5941 histedit_skip_commit(struct got_histedit_list_entry *hle,
5942 struct got_worktree *worktree, struct got_repository *repo)
5944 const struct got_error *error;
5945 struct got_commit_object *commit;
5947 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5948 repo);
5949 if (error)
5950 return error;
5952 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5953 if (error)
5954 return error;
5956 error = show_histedit_progress(commit, hle, NULL);
5957 got_object_commit_close(commit);
5958 return error;
5961 static const struct got_error *
5962 cmd_histedit(int argc, char *argv[])
5964 const struct got_error *error = NULL;
5965 struct got_worktree *worktree = NULL;
5966 struct got_fileindex *fileindex = NULL;
5967 struct got_repository *repo = NULL;
5968 char *cwd = NULL;
5969 struct got_reference *branch = NULL;
5970 struct got_reference *tmp_branch = NULL;
5971 struct got_object_id *resume_commit_id = NULL;
5972 struct got_object_id *base_commit_id = NULL;
5973 struct got_object_id *head_commit_id = NULL;
5974 struct got_commit_object *commit = NULL;
5975 int ch, rebase_in_progress = 0, did_something;
5976 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5977 const char *edit_script_path = NULL;
5978 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5979 struct got_object_id_queue commits;
5980 struct got_pathlist_head merged_paths;
5981 const struct got_object_id_queue *parent_ids;
5982 struct got_object_qid *pid;
5983 struct got_histedit_list histedit_cmds;
5984 struct got_histedit_list_entry *hle;
5986 SIMPLEQ_INIT(&commits);
5987 TAILQ_INIT(&histedit_cmds);
5988 TAILQ_INIT(&merged_paths);
5990 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5991 switch (ch) {
5992 case 'a':
5993 abort_edit = 1;
5994 break;
5995 case 'c':
5996 continue_edit = 1;
5997 break;
5998 case 'F':
5999 edit_script_path = optarg;
6000 break;
6001 default:
6002 usage_histedit();
6003 /* NOTREACHED */
6007 argc -= optind;
6008 argv += optind;
6010 #ifndef PROFILE
6011 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6012 "unveil", NULL) == -1)
6013 err(1, "pledge");
6014 #endif
6015 if (abort_edit && continue_edit)
6016 usage_histedit();
6017 if (argc != 0)
6018 usage_histedit();
6021 * This command cannot apply unveil(2) in all cases because the
6022 * user may choose to run an editor to edit the histedit script
6023 * and to edit individual commit log messages.
6024 * unveil(2) traverses exec(2); if an editor is used we have to
6025 * apply unveil after edit script and log messages have been written.
6026 * XXX TODO: Make use of unveil(2) where possible.
6029 cwd = getcwd(NULL, 0);
6030 if (cwd == NULL) {
6031 error = got_error_from_errno("getcwd");
6032 goto done;
6034 error = got_worktree_open(&worktree, cwd);
6035 if (error)
6036 goto done;
6038 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6039 if (error != NULL)
6040 goto done;
6042 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6043 if (error)
6044 goto done;
6045 if (rebase_in_progress) {
6046 error = got_error(GOT_ERR_REBASING);
6047 goto done;
6050 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6051 if (error)
6052 goto done;
6054 if (edit_in_progress && abort_edit) {
6055 error = got_worktree_histedit_continue(&resume_commit_id,
6056 &tmp_branch, &branch, &base_commit_id, &fileindex,
6057 worktree, repo);
6058 if (error)
6059 goto done;
6060 printf("Switching work tree to %s\n",
6061 got_ref_get_symref_target(branch));
6062 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6063 branch, base_commit_id, update_progress, &did_something);
6064 if (error)
6065 goto done;
6066 printf("Histedit of %s aborted\n",
6067 got_ref_get_symref_target(branch));
6068 goto done; /* nothing else to do */
6069 } else if (abort_edit) {
6070 error = got_error(GOT_ERR_NOT_HISTEDIT);
6071 goto done;
6074 if (continue_edit) {
6075 char *path;
6077 if (!edit_in_progress) {
6078 error = got_error(GOT_ERR_NOT_HISTEDIT);
6079 goto done;
6082 error = got_worktree_get_histedit_script_path(&path, worktree);
6083 if (error)
6084 goto done;
6086 error = histedit_load_list(&histedit_cmds, path, repo);
6087 free(path);
6088 if (error)
6089 goto done;
6091 error = got_worktree_histedit_continue(&resume_commit_id,
6092 &tmp_branch, &branch, &base_commit_id, &fileindex,
6093 worktree, repo);
6094 if (error)
6095 goto done;
6097 error = got_ref_resolve(&head_commit_id, repo, branch);
6098 if (error)
6099 goto done;
6101 error = got_object_open_as_commit(&commit, repo,
6102 head_commit_id);
6103 if (error)
6104 goto done;
6105 parent_ids = got_object_commit_get_parent_ids(commit);
6106 pid = SIMPLEQ_FIRST(parent_ids);
6107 if (pid == NULL) {
6108 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6109 goto done;
6111 error = collect_commits(&commits, head_commit_id, pid->id,
6112 base_commit_id, got_worktree_get_path_prefix(worktree),
6113 GOT_ERR_HISTEDIT_PATH, repo);
6114 got_object_commit_close(commit);
6115 commit = NULL;
6116 if (error)
6117 goto done;
6118 } else {
6119 if (edit_in_progress) {
6120 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6121 goto done;
6124 error = got_ref_open(&branch, repo,
6125 got_worktree_get_head_ref_name(worktree), 0);
6126 if (error != NULL)
6127 goto done;
6129 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6130 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6131 "will not edit commit history of a branch outside "
6132 "the \"refs/heads/\" reference namespace");
6133 goto done;
6136 error = got_ref_resolve(&head_commit_id, repo, branch);
6137 got_ref_close(branch);
6138 branch = NULL;
6139 if (error)
6140 goto done;
6142 error = got_object_open_as_commit(&commit, repo,
6143 head_commit_id);
6144 if (error)
6145 goto done;
6146 parent_ids = got_object_commit_get_parent_ids(commit);
6147 pid = SIMPLEQ_FIRST(parent_ids);
6148 if (pid == NULL) {
6149 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6150 goto done;
6152 error = collect_commits(&commits, head_commit_id, pid->id,
6153 got_worktree_get_base_commit_id(worktree),
6154 got_worktree_get_path_prefix(worktree),
6155 GOT_ERR_HISTEDIT_PATH, repo);
6156 got_object_commit_close(commit);
6157 commit = NULL;
6158 if (error)
6159 goto done;
6161 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6162 &base_commit_id, &fileindex, worktree, repo);
6163 if (error)
6164 goto done;
6166 if (edit_script_path) {
6167 error = histedit_load_list(&histedit_cmds,
6168 edit_script_path, repo);
6169 if (error) {
6170 got_worktree_histedit_abort(worktree, fileindex,
6171 repo, branch, base_commit_id,
6172 update_progress, &did_something);
6173 goto done;
6175 } else {
6176 error = histedit_edit_script(&histedit_cmds, &commits,
6177 repo);
6178 if (error) {
6179 got_worktree_histedit_abort(worktree, fileindex,
6180 repo, branch, base_commit_id,
6181 update_progress, &did_something);
6182 goto done;
6187 error = histedit_save_list(&histedit_cmds, worktree,
6188 repo);
6189 if (error) {
6190 got_worktree_histedit_abort(worktree, fileindex,
6191 repo, branch, base_commit_id,
6192 update_progress, &did_something);
6193 goto done;
6198 error = histedit_check_script(&histedit_cmds, &commits, repo);
6199 if (error)
6200 goto done;
6202 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6203 if (resume_commit_id) {
6204 if (got_object_id_cmp(hle->commit_id,
6205 resume_commit_id) != 0)
6206 continue;
6208 resume_commit_id = NULL;
6209 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6210 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6211 error = histedit_skip_commit(hle, worktree,
6212 repo);
6213 } else {
6214 error = histedit_commit(NULL, worktree,
6215 fileindex, tmp_branch, hle, repo);
6217 if (error)
6218 goto done;
6219 continue;
6222 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6223 error = histedit_skip_commit(hle, worktree, repo);
6224 if (error)
6225 goto done;
6226 continue;
6229 error = got_object_open_as_commit(&commit, repo,
6230 hle->commit_id);
6231 if (error)
6232 goto done;
6233 parent_ids = got_object_commit_get_parent_ids(commit);
6234 pid = SIMPLEQ_FIRST(parent_ids);
6236 error = got_worktree_histedit_merge_files(&merged_paths,
6237 worktree, fileindex, pid->id, hle->commit_id, repo,
6238 rebase_progress, &rebase_status, check_cancelled, NULL);
6239 if (error)
6240 goto done;
6241 got_object_commit_close(commit);
6242 commit = NULL;
6244 if (rebase_status == GOT_STATUS_CONFLICT) {
6245 got_worktree_rebase_pathlist_free(&merged_paths);
6246 break;
6249 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6250 char *id_str;
6251 error = got_object_id_str(&id_str, hle->commit_id);
6252 if (error)
6253 goto done;
6254 printf("Stopping histedit for amending commit %s\n",
6255 id_str);
6256 free(id_str);
6257 got_worktree_rebase_pathlist_free(&merged_paths);
6258 error = got_worktree_histedit_postpone(worktree,
6259 fileindex);
6260 goto done;
6263 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6264 error = histedit_skip_commit(hle, worktree, repo);
6265 if (error)
6266 goto done;
6267 continue;
6270 error = histedit_commit(&merged_paths, worktree, fileindex,
6271 tmp_branch, hle, repo);
6272 got_worktree_rebase_pathlist_free(&merged_paths);
6273 if (error)
6274 goto done;
6277 if (rebase_status == GOT_STATUS_CONFLICT) {
6278 error = got_worktree_histedit_postpone(worktree, fileindex);
6279 if (error)
6280 goto done;
6281 error = got_error_msg(GOT_ERR_CONFLICTS,
6282 "conflicts must be resolved before rebasing can continue");
6283 } else
6284 error = histedit_complete(worktree, fileindex, tmp_branch,
6285 branch, repo);
6286 done:
6287 got_object_id_queue_free(&commits);
6288 histedit_free_list(&histedit_cmds);
6289 free(head_commit_id);
6290 free(base_commit_id);
6291 free(resume_commit_id);
6292 if (commit)
6293 got_object_commit_close(commit);
6294 if (branch)
6295 got_ref_close(branch);
6296 if (tmp_branch)
6297 got_ref_close(tmp_branch);
6298 if (worktree)
6299 got_worktree_close(worktree);
6300 if (repo)
6301 got_repo_close(repo);
6302 return error;
6305 __dead static void
6306 usage_stage(void)
6308 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6309 "[file-path ...]\n",
6310 getprogname());
6311 exit(1);
6314 static const struct got_error *
6315 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6316 const char *path, struct got_object_id *blob_id,
6317 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6319 const struct got_error *err = NULL;
6320 char *id_str = NULL;
6322 if (staged_status != GOT_STATUS_ADD &&
6323 staged_status != GOT_STATUS_MODIFY &&
6324 staged_status != GOT_STATUS_DELETE)
6325 return NULL;
6327 if (staged_status == GOT_STATUS_ADD ||
6328 staged_status == GOT_STATUS_MODIFY)
6329 err = got_object_id_str(&id_str, staged_blob_id);
6330 else
6331 err = got_object_id_str(&id_str, blob_id);
6332 if (err)
6333 return err;
6335 printf("%s %c %s\n", id_str, staged_status, path);
6336 free(id_str);
6337 return NULL;
6340 static const struct got_error *
6341 cmd_stage(int argc, char *argv[])
6343 const struct got_error *error = NULL;
6344 struct got_repository *repo = NULL;
6345 struct got_worktree *worktree = NULL;
6346 char *cwd = NULL;
6347 struct got_pathlist_head paths;
6348 struct got_pathlist_entry *pe;
6349 int ch, list_stage = 0, pflag = 0;
6350 FILE *patch_script_file = NULL;
6351 const char *patch_script_path = NULL;
6352 struct choose_patch_arg cpa;
6354 TAILQ_INIT(&paths);
6356 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6357 switch (ch) {
6358 case 'l':
6359 list_stage = 1;
6360 break;
6361 case 'p':
6362 pflag = 1;
6363 break;
6364 case 'F':
6365 patch_script_path = optarg;
6366 break;
6367 default:
6368 usage_stage();
6369 /* NOTREACHED */
6373 argc -= optind;
6374 argv += optind;
6376 #ifndef PROFILE
6377 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6378 "unveil", NULL) == -1)
6379 err(1, "pledge");
6380 #endif
6381 if (list_stage && (pflag || patch_script_path))
6382 errx(1, "-l option cannot be used with other options");
6383 if (patch_script_path && !pflag)
6384 errx(1, "-F option can only be used together with -p option");
6386 cwd = getcwd(NULL, 0);
6387 if (cwd == NULL) {
6388 error = got_error_from_errno("getcwd");
6389 goto done;
6392 error = got_worktree_open(&worktree, cwd);
6393 if (error)
6394 goto done;
6396 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6397 if (error != NULL)
6398 goto done;
6400 if (patch_script_path) {
6401 patch_script_file = fopen(patch_script_path, "r");
6402 if (patch_script_file == NULL) {
6403 error = got_error_from_errno2("fopen",
6404 patch_script_path);
6405 goto done;
6408 error = apply_unveil(got_repo_get_path(repo), 1,
6409 got_worktree_get_root_path(worktree));
6410 if (error)
6411 goto done;
6413 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6414 if (error)
6415 goto done;
6417 if (list_stage)
6418 error = got_worktree_status(worktree, &paths, repo,
6419 print_stage, NULL, check_cancelled, NULL);
6420 else {
6421 cpa.patch_script_file = patch_script_file;
6422 cpa.action = "stage";
6423 error = got_worktree_stage(worktree, &paths,
6424 pflag ? NULL : print_status, NULL,
6425 pflag ? choose_patch : NULL, &cpa, repo);
6427 done:
6428 if (patch_script_file && fclose(patch_script_file) == EOF &&
6429 error == NULL)
6430 error = got_error_from_errno2("fclose", patch_script_path);
6431 if (repo)
6432 got_repo_close(repo);
6433 if (worktree)
6434 got_worktree_close(worktree);
6435 TAILQ_FOREACH(pe, &paths, entry)
6436 free((char *)pe->path);
6437 got_pathlist_free(&paths);
6438 free(cwd);
6439 return error;
6442 __dead static void
6443 usage_unstage(void)
6445 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6446 "[file-path ...]\n",
6447 getprogname());
6448 exit(1);
6452 static const struct got_error *
6453 cmd_unstage(int argc, char *argv[])
6455 const struct got_error *error = NULL;
6456 struct got_repository *repo = NULL;
6457 struct got_worktree *worktree = NULL;
6458 char *cwd = NULL;
6459 struct got_pathlist_head paths;
6460 struct got_pathlist_entry *pe;
6461 int ch, did_something = 0, pflag = 0;
6462 FILE *patch_script_file = NULL;
6463 const char *patch_script_path = NULL;
6464 struct choose_patch_arg cpa;
6466 TAILQ_INIT(&paths);
6468 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6469 switch (ch) {
6470 case 'p':
6471 pflag = 1;
6472 break;
6473 case 'F':
6474 patch_script_path = optarg;
6475 break;
6476 default:
6477 usage_unstage();
6478 /* NOTREACHED */
6482 argc -= optind;
6483 argv += optind;
6485 #ifndef PROFILE
6486 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6487 "unveil", NULL) == -1)
6488 err(1, "pledge");
6489 #endif
6490 if (patch_script_path && !pflag)
6491 errx(1, "-F option can only be used together with -p option");
6493 cwd = getcwd(NULL, 0);
6494 if (cwd == NULL) {
6495 error = got_error_from_errno("getcwd");
6496 goto done;
6499 error = got_worktree_open(&worktree, cwd);
6500 if (error)
6501 goto done;
6503 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6504 if (error != NULL)
6505 goto done;
6507 if (patch_script_path) {
6508 patch_script_file = fopen(patch_script_path, "r");
6509 if (patch_script_file == NULL) {
6510 error = got_error_from_errno2("fopen",
6511 patch_script_path);
6512 goto done;
6516 error = apply_unveil(got_repo_get_path(repo), 1,
6517 got_worktree_get_root_path(worktree));
6518 if (error)
6519 goto done;
6521 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6522 if (error)
6523 goto done;
6525 cpa.patch_script_file = patch_script_file;
6526 cpa.action = "unstage";
6527 error = got_worktree_unstage(worktree, &paths, update_progress,
6528 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6529 done:
6530 if (patch_script_file && fclose(patch_script_file) == EOF &&
6531 error == NULL)
6532 error = got_error_from_errno2("fclose", patch_script_path);
6533 if (repo)
6534 got_repo_close(repo);
6535 if (worktree)
6536 got_worktree_close(worktree);
6537 TAILQ_FOREACH(pe, &paths, entry)
6538 free((char *)pe->path);
6539 got_pathlist_free(&paths);
6540 free(cwd);
6541 return error;
6544 __dead static void
6545 usage_cat(void)
6547 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
6548 "[object2 ...]\n", getprogname());
6549 exit(1);
6552 static const struct got_error *
6553 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6555 const struct got_error *err;
6556 struct got_blob_object *blob;
6558 err = got_object_open_as_blob(&blob, repo, id, 8192);
6559 if (err)
6560 return err;
6562 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6563 got_object_blob_close(blob);
6564 return err;
6567 static const struct got_error *
6568 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6570 const struct got_error *err;
6571 struct got_tree_object *tree;
6572 const struct got_tree_entries *entries;
6573 struct got_tree_entry *te;
6575 err = got_object_open_as_tree(&tree, repo, id);
6576 if (err)
6577 return err;
6579 entries = got_object_tree_get_entries(tree);
6580 te = SIMPLEQ_FIRST(&entries->head);
6581 while (te) {
6582 char *id_str;
6583 if (sigint_received || sigpipe_received)
6584 break;
6585 err = got_object_id_str(&id_str, te->id);
6586 if (err)
6587 break;
6588 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6589 free(id_str);
6590 te = SIMPLEQ_NEXT(te, entry);
6593 got_object_tree_close(tree);
6594 return err;
6597 static const struct got_error *
6598 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6600 const struct got_error *err;
6601 struct got_commit_object *commit;
6602 const struct got_object_id_queue *parent_ids;
6603 struct got_object_qid *pid;
6604 char *id_str = NULL;
6605 const char *logmsg = NULL;
6607 err = got_object_open_as_commit(&commit, repo, id);
6608 if (err)
6609 return err;
6611 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6612 if (err)
6613 goto done;
6615 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6616 parent_ids = got_object_commit_get_parent_ids(commit);
6617 fprintf(outfile, "numparents %d\n",
6618 got_object_commit_get_nparents(commit));
6619 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6620 char *pid_str;
6621 err = got_object_id_str(&pid_str, pid->id);
6622 if (err)
6623 goto done;
6624 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6625 free(pid_str);
6627 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6628 got_object_commit_get_author(commit),
6629 got_object_commit_get_author_time(commit));
6631 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6632 got_object_commit_get_author(commit),
6633 got_object_commit_get_committer_time(commit));
6635 logmsg = got_object_commit_get_logmsg_raw(commit);
6636 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6637 fprintf(outfile, "%s", logmsg);
6638 done:
6639 free(id_str);
6640 got_object_commit_close(commit);
6641 return err;
6644 static const struct got_error *
6645 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6647 const struct got_error *err;
6648 struct got_tag_object *tag;
6649 char *id_str = NULL;
6650 const char *tagmsg = NULL;
6652 err = got_object_open_as_tag(&tag, repo, id);
6653 if (err)
6654 return err;
6656 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6657 if (err)
6658 goto done;
6660 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6662 switch (got_object_tag_get_object_type(tag)) {
6663 case GOT_OBJ_TYPE_BLOB:
6664 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6665 GOT_OBJ_LABEL_BLOB);
6666 break;
6667 case GOT_OBJ_TYPE_TREE:
6668 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6669 GOT_OBJ_LABEL_TREE);
6670 break;
6671 case GOT_OBJ_TYPE_COMMIT:
6672 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6673 GOT_OBJ_LABEL_COMMIT);
6674 break;
6675 case GOT_OBJ_TYPE_TAG:
6676 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6677 GOT_OBJ_LABEL_TAG);
6678 break;
6679 default:
6680 break;
6683 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6684 got_object_tag_get_name(tag));
6686 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6687 got_object_tag_get_tagger(tag),
6688 got_object_tag_get_tagger_time(tag));
6690 tagmsg = got_object_tag_get_message(tag);
6691 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6692 fprintf(outfile, "%s", tagmsg);
6693 done:
6694 free(id_str);
6695 got_object_tag_close(tag);
6696 return err;
6699 static const struct got_error *
6700 cmd_cat(int argc, char *argv[])
6702 const struct got_error *error;
6703 struct got_repository *repo = NULL;
6704 struct got_worktree *worktree = NULL;
6705 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6706 struct got_object_id *id = NULL;
6707 int ch, obj_type, i;
6709 #ifndef PROFILE
6710 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6711 NULL) == -1)
6712 err(1, "pledge");
6713 #endif
6715 while ((ch = getopt(argc, argv, "r:")) != -1) {
6716 switch (ch) {
6717 case 'r':
6718 repo_path = realpath(optarg, NULL);
6719 if (repo_path == NULL)
6720 err(1, "-r option");
6721 got_path_strip_trailing_slashes(repo_path);
6722 break;
6723 default:
6724 usage_cat();
6725 /* NOTREACHED */
6729 argc -= optind;
6730 argv += optind;
6732 cwd = getcwd(NULL, 0);
6733 if (cwd == NULL) {
6734 error = got_error_from_errno("getcwd");
6735 goto done;
6737 error = got_worktree_open(&worktree, cwd);
6738 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6739 goto done;
6740 if (worktree) {
6741 if (repo_path == NULL) {
6742 repo_path = strdup(
6743 got_worktree_get_repo_path(worktree));
6744 if (repo_path == NULL) {
6745 error = got_error_from_errno("strdup");
6746 goto done;
6751 if (repo_path == NULL) {
6752 repo_path = getcwd(NULL, 0);
6753 if (repo_path == NULL)
6754 return got_error_from_errno("getcwd");
6757 error = got_repo_open(&repo, repo_path);
6758 free(repo_path);
6759 if (error != NULL)
6760 goto done;
6762 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6763 if (error)
6764 goto done;
6766 for (i = 0; i < argc; i++) {
6767 error = match_object_id(&id, &label, argv[i],
6768 GOT_OBJ_TYPE_ANY, 0, repo);
6769 if (error)
6770 break;
6772 error = got_object_get_type(&obj_type, repo, id);
6773 if (error)
6774 break;
6776 switch (obj_type) {
6777 case GOT_OBJ_TYPE_BLOB:
6778 error = cat_blob(id, repo, stdout);
6779 break;
6780 case GOT_OBJ_TYPE_TREE:
6781 error = cat_tree(id, repo, stdout);
6782 break;
6783 case GOT_OBJ_TYPE_COMMIT:
6784 error = cat_commit(id, repo, stdout);
6785 break;
6786 case GOT_OBJ_TYPE_TAG:
6787 error = cat_tag(id, repo, stdout);
6788 break;
6789 default:
6790 error = got_error(GOT_ERR_OBJ_TYPE);
6791 break;
6793 if (error)
6794 break;
6795 free(label);
6796 label = NULL;
6797 free(id);
6798 id = NULL;
6801 done:
6802 free(label);
6803 free(id);
6804 if (worktree)
6805 got_worktree_close(worktree);
6806 if (repo) {
6807 const struct got_error *repo_error;
6808 repo_error = got_repo_close(repo);
6809 if (error == NULL)
6810 error = repo_error;
6812 return error;