Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_stage(void);
101 __dead static void usage_unstage(void);
102 __dead static void usage_cat(void);
104 static const struct got_error* cmd_init(int, char *[]);
105 static const struct got_error* cmd_import(int, char *[]);
106 static const struct got_error* cmd_checkout(int, char *[]);
107 static const struct got_error* cmd_update(int, char *[]);
108 static const struct got_error* cmd_log(int, char *[]);
109 static const struct got_error* cmd_diff(int, char *[]);
110 static const struct got_error* cmd_blame(int, char *[]);
111 static const struct got_error* cmd_tree(int, char *[]);
112 static const struct got_error* cmd_status(int, char *[]);
113 static const struct got_error* cmd_ref(int, char *[]);
114 static const struct got_error* cmd_branch(int, char *[]);
115 static const struct got_error* cmd_tag(int, char *[]);
116 static const struct got_error* cmd_add(int, char *[]);
117 static const struct got_error* cmd_remove(int, char *[]);
118 static const struct got_error* cmd_revert(int, char *[]);
119 static const struct got_error* cmd_commit(int, char *[]);
120 static const struct got_error* cmd_cherrypick(int, char *[]);
121 static const struct got_error* cmd_backout(int, char *[]);
122 static const struct got_error* cmd_rebase(int, char *[]);
123 static const struct got_error* cmd_histedit(int, char *[]);
124 static const struct got_error* cmd_stage(int, char *[]);
125 static const struct got_error* cmd_unstage(int, char *[]);
126 static const struct got_error* cmd_cat(int, char *[]);
128 static struct got_cmd got_commands[] = {
129 { "init", cmd_init, usage_init, "in" },
130 { "import", cmd_import, usage_import, "im" },
131 { "checkout", cmd_checkout, usage_checkout, "co" },
132 { "update", cmd_update, usage_update, "up" },
133 { "log", cmd_log, usage_log, "" },
134 { "diff", cmd_diff, usage_diff, "di" },
135 { "blame", cmd_blame, usage_blame, "bl" },
136 { "tree", cmd_tree, usage_tree, "tr" },
137 { "status", cmd_status, usage_status, "st" },
138 { "ref", cmd_ref, usage_ref, "" },
139 { "branch", cmd_branch, usage_branch, "br" },
140 { "tag", cmd_tag, usage_tag, "" },
141 { "add", cmd_add, usage_add, "" },
142 { "remove", cmd_remove, usage_remove, "rm" },
143 { "revert", cmd_revert, usage_revert, "rv" },
144 { "commit", cmd_commit, usage_commit, "ci" },
145 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 { "backout", cmd_backout, usage_backout, "bo" },
147 { "rebase", cmd_rebase, usage_rebase, "rb" },
148 { "histedit", cmd_histedit, usage_histedit, "he" },
149 { "stage", cmd_stage, usage_stage, "sg" },
150 { "unstage", cmd_unstage, usage_unstage, "ug" },
151 { "cat", cmd_cat, usage_cat, "" },
152 };
154 static void
155 list_commands(void)
157 int i;
159 fprintf(stderr, "commands:");
160 for (i = 0; i < nitems(got_commands); i++) {
161 struct got_cmd *cmd = &got_commands[i];
162 fprintf(stderr, " %s", cmd->cmd_name);
164 fputc('\n', stderr);
167 int
168 main(int argc, char *argv[])
170 struct got_cmd *cmd;
171 unsigned int i;
172 int ch;
173 int hflag = 0, Vflag = 0;
175 setlocale(LC_CTYPE, "");
177 while ((ch = getopt(argc, argv, "hV")) != -1) {
178 switch (ch) {
179 case 'h':
180 hflag = 1;
181 break;
182 case 'V':
183 Vflag = 1;
184 break;
185 default:
186 usage(hflag);
187 /* NOTREACHED */
191 argc -= optind;
192 argv += optind;
193 optind = 0;
195 if (Vflag) {
196 got_version_print_str();
197 return 1;
200 if (argc <= 0)
201 usage(hflag);
203 signal(SIGINT, catch_sigint);
204 signal(SIGPIPE, catch_sigpipe);
206 for (i = 0; i < nitems(got_commands); i++) {
207 const struct got_error *error;
209 cmd = &got_commands[i];
211 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 strcmp(cmd->cmd_alias, argv[0]) != 0)
213 continue;
215 if (hflag)
216 got_commands[i].cmd_usage();
218 error = got_commands[i].cmd_main(argc, argv);
219 if (error && !(sigint_received || sigpipe_received)) {
220 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
221 return 1;
224 return 0;
227 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 list_commands();
229 return 1;
232 __dead static void
233 usage(int hflag)
235 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
236 getprogname());
237 if (hflag)
238 list_commands();
239 exit(1);
242 static const struct got_error *
243 get_editor(char **abspath)
245 const struct got_error *err = NULL;
246 const char *editor;
248 *abspath = NULL;
250 editor = getenv("VISUAL");
251 if (editor == NULL)
252 editor = getenv("EDITOR");
254 if (editor) {
255 err = got_path_find_prog(abspath, editor);
256 if (err)
257 return err;
260 if (*abspath == NULL) {
261 *abspath = strdup("/bin/ed");
262 if (*abspath == NULL)
263 return got_error_from_errno("strdup");
266 return NULL;
269 static const struct got_error *
270 apply_unveil(const char *repo_path, int repo_read_only,
271 const char *worktree_path)
273 const struct got_error *err;
275 #ifdef PROFILE
276 if (unveil("gmon.out", "rwc") != 0)
277 return got_error_from_errno2("unveil", "gmon.out");
278 #endif
279 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 return got_error_from_errno2("unveil", repo_path);
282 if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 return got_error_from_errno2("unveil", worktree_path);
285 if (unveil("/tmp", "rwc") != 0)
286 return got_error_from_errno2("unveil", "/tmp");
288 err = got_privsep_unveil_exec_helpers();
289 if (err != NULL)
290 return err;
292 if (unveil(NULL, NULL) != 0)
293 return got_error_from_errno("unveil");
295 return NULL;
298 __dead static void
299 usage_init(void)
301 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
302 exit(1);
305 static const struct got_error *
306 cmd_init(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 char *repo_path = NULL;
310 int ch;
312 while ((ch = getopt(argc, argv, "")) != -1) {
313 switch (ch) {
314 default:
315 usage_init();
316 /* NOTREACHED */
320 argc -= optind;
321 argv += optind;
323 #ifndef PROFILE
324 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
325 err(1, "pledge");
326 #endif
327 if (argc != 1)
328 usage_init();
330 repo_path = strdup(argv[0]);
331 if (repo_path == NULL)
332 return got_error_from_errno("strdup");
334 got_path_strip_trailing_slashes(repo_path);
336 error = got_path_mkdir(repo_path);
337 if (error &&
338 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
339 goto done;
341 error = apply_unveil(repo_path, 0, NULL);
342 if (error)
343 goto done;
345 error = got_repo_init(repo_path);
346 if (error != NULL)
347 goto done;
349 done:
350 free(repo_path);
351 return error;
354 __dead static void
355 usage_import(void)
357 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 "[-r repository-path] [-I pattern] path\n", getprogname());
359 exit(1);
362 int
363 spawn_editor(const char *editor, const char *file)
365 pid_t pid;
366 sig_t sighup, sigint, sigquit;
367 int st = -1;
369 sighup = signal(SIGHUP, SIG_IGN);
370 sigint = signal(SIGINT, SIG_IGN);
371 sigquit = signal(SIGQUIT, SIG_IGN);
373 switch (pid = fork()) {
374 case -1:
375 goto doneediting;
376 case 0:
377 execl(editor, editor, file, (char *)NULL);
378 _exit(127);
381 while (waitpid(pid, &st, 0) == -1)
382 if (errno != EINTR)
383 break;
385 doneediting:
386 (void)signal(SIGHUP, sighup);
387 (void)signal(SIGINT, sigint);
388 (void)signal(SIGQUIT, sigquit);
390 if (!WIFEXITED(st)) {
391 errno = EINTR;
392 return -1;
395 return WEXITSTATUS(st);
398 static const struct got_error *
399 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 const char *initial_content)
402 const struct got_error *err = NULL;
403 char buf[1024];
404 struct stat st, st2;
405 FILE *fp;
406 int content_changed = 0;
407 size_t len;
409 *logmsg = NULL;
411 if (stat(logmsg_path, &st) == -1)
412 return got_error_from_errno2("stat", logmsg_path);
414 if (spawn_editor(editor, logmsg_path) == -1)
415 return got_error_from_errno("failed spawning editor");
417 if (stat(logmsg_path, &st2) == -1)
418 return got_error_from_errno("stat");
420 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 "no changes made to commit message, aborting");
424 *logmsg = malloc(st2.st_size + 1);
425 if (*logmsg == NULL)
426 return got_error_from_errno("malloc");
427 (*logmsg)[0] = '\0';
428 len = 0;
430 fp = fopen(logmsg_path, "r");
431 if (fp == NULL) {
432 err = got_error_from_errno("fopen");
433 goto done;
435 while (fgets(buf, sizeof(buf), fp) != NULL) {
436 if (!content_changed && strcmp(buf, initial_content) != 0)
437 content_changed = 1;
438 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(*logmsg, buf, st2.st_size);
442 fclose(fp);
444 while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 (*logmsg)[len - 1] = '\0';
446 len--;
449 if (len == 0 || !content_changed)
450 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 "commit message cannot be empty, aborting");
452 done:
453 if (err) {
454 free(*logmsg);
455 *logmsg = NULL;
457 return err;
460 static const struct got_error *
461 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 const char *branch_name)
464 char *initial_content = NULL, *logmsg_path = NULL;
465 const struct got_error *err = NULL;
466 int fd;
468 if (asprintf(&initial_content,
469 "\n# %s to be imported to branch %s\n", path_dir,
470 branch_name) == -1)
471 return got_error_from_errno("asprintf");
473 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
474 if (err)
475 goto done;
477 dprintf(fd, initial_content);
478 close(fd);
480 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
481 done:
482 free(initial_content);
483 free(logmsg_path);
484 return err;
487 static const struct got_error *
488 import_progress(void *arg, const char *path)
490 printf("A %s\n", path);
491 return NULL;
494 static const struct got_error *
495 get_author(char **author, struct got_repository *repo)
497 const struct got_error *err = NULL;
498 const char *got_author, *name, *email;
500 *author = NULL;
502 name = got_repo_get_gitconfig_author_name(repo);
503 email = got_repo_get_gitconfig_author_email(repo);
504 if (name && email) {
505 if (asprintf(author, "%s <%s>", name, email) == -1)
506 return got_error_from_errno("asprintf");
507 return NULL;
510 got_author = getenv("GOT_AUTHOR");
511 if (got_author == NULL) {
512 name = got_repo_get_global_gitconfig_author_name(repo);
513 email = got_repo_get_global_gitconfig_author_email(repo);
514 if (name && email) {
515 if (asprintf(author, "%s <%s>", name, email) == -1)
516 return got_error_from_errno("asprintf");
517 return NULL;
519 /* TODO: Look up user in password database? */
520 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
523 *author = strdup(got_author);
524 if (*author == NULL)
525 return got_error_from_errno("strdup");
527 /*
528 * Really dumb email address check; we're only doing this to
529 * avoid git's object parser breaking on commits we create.
530 */
531 while (*got_author && *got_author != '<')
532 got_author++;
533 if (*got_author != '<') {
534 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
535 goto done;
537 while (*got_author && *got_author != '@')
538 got_author++;
539 if (*got_author != '@') {
540 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
541 goto done;
543 while (*got_author && *got_author != '>')
544 got_author++;
545 if (*got_author != '>')
546 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
547 done:
548 if (err) {
549 free(*author);
550 *author = NULL;
552 return err;
555 static const struct got_error *
556 get_gitconfig_path(char **gitconfig_path)
558 const char *homedir = getenv("HOME");
560 *gitconfig_path = NULL;
561 if (homedir) {
562 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
563 return got_error_from_errno("asprintf");
566 return NULL;
569 static const struct got_error *
570 cmd_import(int argc, char *argv[])
572 const struct got_error *error = NULL;
573 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
574 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
575 const char *branch_name = "master";
576 char *refname = NULL, *id_str = NULL;
577 struct got_repository *repo = NULL;
578 struct got_reference *branch_ref = NULL, *head_ref = NULL;
579 struct got_object_id *new_commit_id = NULL;
580 int ch;
581 struct got_pathlist_head ignores;
582 struct got_pathlist_entry *pe;
584 TAILQ_INIT(&ignores);
586 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
587 switch (ch) {
588 case 'b':
589 branch_name = optarg;
590 break;
591 case 'm':
592 logmsg = strdup(optarg);
593 if (logmsg == NULL) {
594 error = got_error_from_errno("strdup");
595 goto done;
597 break;
598 case 'r':
599 repo_path = realpath(optarg, NULL);
600 if (repo_path == NULL) {
601 error = got_error_from_errno("realpath");
602 goto done;
604 break;
605 case 'I':
606 if (optarg[0] == '\0')
607 break;
608 error = got_pathlist_insert(&pe, &ignores, optarg,
609 NULL);
610 if (error)
611 goto done;
612 break;
613 default:
614 usage_import();
615 /* NOTREACHED */
619 argc -= optind;
620 argv += optind;
622 #ifndef PROFILE
623 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
624 "unveil",
625 NULL) == -1)
626 err(1, "pledge");
627 #endif
628 if (argc != 1)
629 usage_import();
631 if (repo_path == NULL) {
632 repo_path = getcwd(NULL, 0);
633 if (repo_path == NULL)
634 return got_error_from_errno("getcwd");
636 got_path_strip_trailing_slashes(repo_path);
637 error = get_gitconfig_path(&gitconfig_path);
638 if (error)
639 goto done;
640 error = got_repo_open(&repo, repo_path, gitconfig_path);
641 if (error)
642 goto done;
644 error = get_author(&author, repo);
645 if (error)
646 return error;
648 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
649 error = got_error_from_errno("asprintf");
650 goto done;
653 error = got_ref_open(&branch_ref, repo, refname, 0);
654 if (error) {
655 if (error->code != GOT_ERR_NOT_REF)
656 goto done;
657 } else {
658 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
659 "import target branch already exists");
660 goto done;
663 path_dir = realpath(argv[0], NULL);
664 if (path_dir == NULL) {
665 error = got_error_from_errno("realpath");
666 goto done;
668 got_path_strip_trailing_slashes(path_dir);
670 /*
671 * unveil(2) traverses exec(2); if an editor is used we have
672 * to apply unveil after the log message has been written.
673 */
674 if (logmsg == NULL || strlen(logmsg) == 0) {
675 error = get_editor(&editor);
676 if (error)
677 goto done;
678 free(logmsg);
679 error = collect_import_msg(&logmsg, editor, path_dir, refname);
680 if (error)
681 goto done;
684 if (unveil(path_dir, "r") != 0)
685 return got_error_from_errno2("unveil", path_dir);
687 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
688 if (error)
689 goto done;
691 error = got_repo_import(&new_commit_id, path_dir, logmsg,
692 author, &ignores, repo, import_progress, NULL);
693 if (error)
694 goto done;
696 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
697 if (error)
698 goto done;
700 error = got_ref_write(branch_ref, repo);
701 if (error)
702 goto done;
704 error = got_object_id_str(&id_str, new_commit_id);
705 if (error)
706 goto done;
708 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
709 if (error) {
710 if (error->code != GOT_ERR_NOT_REF)
711 goto done;
713 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
714 branch_ref);
715 if (error)
716 goto done;
718 error = got_ref_write(head_ref, repo);
719 if (error)
720 goto done;
723 printf("Created branch %s with commit %s\n",
724 got_ref_get_name(branch_ref), id_str);
725 done:
726 free(logmsg);
727 free(repo_path);
728 free(editor);
729 free(refname);
730 free(new_commit_id);
731 free(id_str);
732 free(author);
733 free(gitconfig_path);
734 if (branch_ref)
735 got_ref_close(branch_ref);
736 if (head_ref)
737 got_ref_close(head_ref);
738 return error;
741 __dead static void
742 usage_checkout(void)
744 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
745 "[-p prefix] repository-path [worktree-path]\n", getprogname());
746 exit(1);
749 static const struct got_error *
750 checkout_progress(void *arg, unsigned char status, const char *path)
752 char *worktree_path = arg;
754 /* Base commit bump happens silently. */
755 if (status == GOT_STATUS_BUMP_BASE)
756 return NULL;
758 while (path[0] == '/')
759 path++;
761 printf("%c %s/%s\n", status, worktree_path, path);
762 return NULL;
765 static const struct got_error *
766 check_cancelled(void *arg)
768 if (sigint_received || sigpipe_received)
769 return got_error(GOT_ERR_CANCELLED);
770 return NULL;
773 static const struct got_error *
774 check_linear_ancestry(struct got_object_id *commit_id,
775 struct got_object_id *base_commit_id, struct got_repository *repo)
777 const struct got_error *err = NULL;
778 struct got_object_id *yca_id;
780 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
781 commit_id, base_commit_id, repo, check_cancelled, NULL);
782 if (err)
783 return err;
785 if (yca_id == NULL)
786 return got_error(GOT_ERR_ANCESTRY);
788 /*
789 * Require a straight line of history between the target commit
790 * and the work tree's base commit.
792 * Non-linear situations such as this require a rebase:
794 * (commit) D F (base_commit)
795 * \ /
796 * C E
797 * \ /
798 * B (yca)
799 * |
800 * A
802 * 'got update' only handles linear cases:
803 * Update forwards in time: A (base/yca) - B - C - D (commit)
804 * Update backwards in time: D (base) - C - B - A (commit/yca)
805 */
806 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
807 got_object_id_cmp(base_commit_id, yca_id) != 0)
808 return got_error(GOT_ERR_ANCESTRY);
810 free(yca_id);
811 return NULL;
814 static const struct got_error *
815 check_same_branch(struct got_object_id *commit_id,
816 struct got_reference *head_ref, struct got_object_id *yca_id,
817 struct got_repository *repo)
819 const struct got_error *err = NULL;
820 struct got_commit_graph *graph = NULL;
821 struct got_object_id *head_commit_id = NULL;
822 int is_same_branch = 0;
824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
825 if (err)
826 goto done;
828 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
829 is_same_branch = 1;
830 goto done;
832 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
833 is_same_branch = 1;
834 goto done;
837 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
838 if (err)
839 goto done;
841 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
842 check_cancelled, NULL);
843 if (err)
844 goto done;
846 for (;;) {
847 struct got_object_id *id;
848 err = got_commit_graph_iter_next(&id, graph);
849 if (err) {
850 if (err->code == GOT_ERR_ITER_COMPLETED) {
851 err = NULL;
852 break;
853 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
854 break;
855 err = got_commit_graph_fetch_commits(graph, 1,
856 repo, check_cancelled, NULL);
857 if (err)
858 break;
861 if (id) {
862 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
863 break;
864 if (got_object_id_cmp(id, commit_id) == 0) {
865 is_same_branch = 1;
866 break;
870 done:
871 if (graph)
872 got_commit_graph_close(graph);
873 free(head_commit_id);
874 if (!err && !is_same_branch)
875 err = got_error(GOT_ERR_ANCESTRY);
876 return err;
879 static const struct got_error *
880 resolve_commit_arg(struct got_object_id **commit_id,
881 const char *commit_id_arg, struct got_repository *repo)
883 const struct got_error *err;
884 struct got_reference *ref;
885 struct got_tag_object *tag;
887 err = got_repo_object_match_tag(&tag, commit_id_arg,
888 GOT_OBJ_TYPE_COMMIT, repo);
889 if (err == NULL) {
890 *commit_id = got_object_id_dup(
891 got_object_tag_get_object_id(tag));
892 if (*commit_id == NULL)
893 err = got_error_from_errno("got_object_id_dup");
894 got_object_tag_close(tag);
895 return err;
896 } else if (err->code != GOT_ERR_NO_OBJ)
897 return err;
899 err = got_ref_open(&ref, repo, commit_id_arg, 0);
900 if (err == NULL) {
901 err = got_ref_resolve(commit_id, repo, ref);
902 got_ref_close(ref);
903 } else {
904 if (err->code != GOT_ERR_NOT_REF)
905 return err;
906 err = got_repo_match_object_id_prefix(commit_id,
907 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
909 return err;
912 static const struct got_error *
913 cmd_checkout(int argc, char *argv[])
915 const struct got_error *error = NULL;
916 struct got_repository *repo = NULL;
917 struct got_reference *head_ref = NULL;
918 struct got_worktree *worktree = NULL;
919 char *repo_path = NULL;
920 char *worktree_path = NULL;
921 const char *path_prefix = "";
922 const char *branch_name = GOT_REF_HEAD;
923 char *commit_id_str = NULL;
924 int ch, same_path_prefix;
925 struct got_pathlist_head paths;
927 TAILQ_INIT(&paths);
929 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
930 switch (ch) {
931 case 'b':
932 branch_name = optarg;
933 break;
934 case 'c':
935 commit_id_str = strdup(optarg);
936 if (commit_id_str == NULL)
937 return got_error_from_errno("strdup");
938 break;
939 case 'p':
940 path_prefix = optarg;
941 break;
942 default:
943 usage_checkout();
944 /* NOTREACHED */
948 argc -= optind;
949 argv += optind;
951 #ifndef PROFILE
952 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
953 "unveil", NULL) == -1)
954 err(1, "pledge");
955 #endif
956 if (argc == 1) {
957 char *cwd, *base, *dotgit;
958 repo_path = realpath(argv[0], NULL);
959 if (repo_path == NULL)
960 return got_error_from_errno2("realpath", argv[0]);
961 cwd = getcwd(NULL, 0);
962 if (cwd == NULL) {
963 error = got_error_from_errno("getcwd");
964 goto done;
966 if (path_prefix[0]) {
967 base = basename(path_prefix);
968 if (base == NULL) {
969 error = got_error_from_errno2("basename",
970 path_prefix);
971 goto done;
973 } else {
974 base = basename(repo_path);
975 if (base == NULL) {
976 error = got_error_from_errno2("basename",
977 repo_path);
978 goto done;
981 dotgit = strstr(base, ".git");
982 if (dotgit)
983 *dotgit = '\0';
984 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
985 error = got_error_from_errno("asprintf");
986 free(cwd);
987 goto done;
989 free(cwd);
990 } else if (argc == 2) {
991 repo_path = realpath(argv[0], NULL);
992 if (repo_path == NULL) {
993 error = got_error_from_errno2("realpath", argv[0]);
994 goto done;
996 worktree_path = realpath(argv[1], NULL);
997 if (worktree_path == NULL) {
998 if (errno != ENOENT) {
999 error = got_error_from_errno2("realpath",
1000 argv[1]);
1001 goto done;
1003 worktree_path = strdup(argv[1]);
1004 if (worktree_path == NULL) {
1005 error = got_error_from_errno("strdup");
1006 goto done;
1009 } else
1010 usage_checkout();
1012 got_path_strip_trailing_slashes(repo_path);
1013 got_path_strip_trailing_slashes(worktree_path);
1015 error = got_repo_open(&repo, repo_path, NULL);
1016 if (error != NULL)
1017 goto done;
1019 /* Pre-create work tree path for unveil(2) */
1020 error = got_path_mkdir(worktree_path);
1021 if (error) {
1022 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1023 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1024 goto done;
1025 if (!got_path_dir_is_empty(worktree_path)) {
1026 error = got_error_path(worktree_path,
1027 GOT_ERR_DIR_NOT_EMPTY);
1028 goto done;
1032 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1033 if (error)
1034 goto done;
1036 error = got_ref_open(&head_ref, repo, branch_name, 0);
1037 if (error != NULL)
1038 goto done;
1040 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1041 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1042 goto done;
1044 error = got_worktree_open(&worktree, worktree_path);
1045 if (error != NULL)
1046 goto done;
1048 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1049 path_prefix);
1050 if (error != NULL)
1051 goto done;
1052 if (!same_path_prefix) {
1053 error = got_error(GOT_ERR_PATH_PREFIX);
1054 goto done;
1057 if (commit_id_str) {
1058 struct got_object_id *commit_id;
1059 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1060 if (error)
1061 goto done;
1062 error = check_linear_ancestry(commit_id,
1063 got_worktree_get_base_commit_id(worktree), repo);
1064 if (error != NULL) {
1065 free(commit_id);
1066 goto done;
1068 error = check_same_branch(commit_id, head_ref, NULL, repo);
1069 if (error)
1070 goto done;
1071 error = got_worktree_set_base_commit_id(worktree, repo,
1072 commit_id);
1073 free(commit_id);
1074 if (error)
1075 goto done;
1078 error = got_pathlist_append(&paths, "", NULL);
1079 if (error)
1080 goto done;
1081 error = got_worktree_checkout_files(worktree, &paths, repo,
1082 checkout_progress, worktree_path, check_cancelled, NULL);
1083 if (error != NULL)
1084 goto done;
1086 printf("Now shut up and hack\n");
1088 done:
1089 got_pathlist_free(&paths);
1090 free(commit_id_str);
1091 free(repo_path);
1092 free(worktree_path);
1093 return error;
1096 __dead static void
1097 usage_update(void)
1099 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1100 getprogname());
1101 exit(1);
1104 static const struct got_error *
1105 update_progress(void *arg, unsigned char status, const char *path)
1107 int *did_something = arg;
1109 if (status == GOT_STATUS_EXISTS)
1110 return NULL;
1112 *did_something = 1;
1114 /* Base commit bump happens silently. */
1115 if (status == GOT_STATUS_BUMP_BASE)
1116 return NULL;
1118 while (path[0] == '/')
1119 path++;
1120 printf("%c %s\n", status, path);
1121 return NULL;
1124 static const struct got_error *
1125 switch_head_ref(struct got_reference *head_ref,
1126 struct got_object_id *commit_id, struct got_worktree *worktree,
1127 struct got_repository *repo)
1129 const struct got_error *err = NULL;
1130 char *base_id_str;
1131 int ref_has_moved = 0;
1133 /* Trivial case: switching between two different references. */
1134 if (strcmp(got_ref_get_name(head_ref),
1135 got_worktree_get_head_ref_name(worktree)) != 0) {
1136 printf("Switching work tree from %s to %s\n",
1137 got_worktree_get_head_ref_name(worktree),
1138 got_ref_get_name(head_ref));
1139 return got_worktree_set_head_ref(worktree, head_ref);
1142 err = check_linear_ancestry(commit_id,
1143 got_worktree_get_base_commit_id(worktree), repo);
1144 if (err) {
1145 if (err->code != GOT_ERR_ANCESTRY)
1146 return err;
1147 ref_has_moved = 1;
1149 if (!ref_has_moved)
1150 return NULL;
1152 /* Switching to a rebased branch with the same reference name. */
1153 err = got_object_id_str(&base_id_str,
1154 got_worktree_get_base_commit_id(worktree));
1155 if (err)
1156 return err;
1157 printf("Reference %s now points at a different branch\n",
1158 got_worktree_get_head_ref_name(worktree));
1159 printf("Switching work tree from %s to %s\n", base_id_str,
1160 got_worktree_get_head_ref_name(worktree));
1161 return NULL;
1164 static const struct got_error *
1165 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1167 const struct got_error *err;
1168 int in_progress;
1170 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1171 if (err)
1172 return err;
1173 if (in_progress)
1174 return got_error(GOT_ERR_REBASING);
1176 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1177 if (err)
1178 return err;
1179 if (in_progress)
1180 return got_error(GOT_ERR_HISTEDIT_BUSY);
1182 return NULL;
1185 static const struct got_error *
1186 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1187 char *argv[], struct got_worktree *worktree)
1189 const struct got_error *err = NULL;
1190 char *path;
1191 int i;
1193 if (argc == 0) {
1194 path = strdup("");
1195 if (path == NULL)
1196 return got_error_from_errno("strdup");
1197 return got_pathlist_append(paths, path, NULL);
1200 for (i = 0; i < argc; i++) {
1201 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1202 if (err)
1203 break;
1204 err = got_pathlist_append(paths, path, NULL);
1205 if (err) {
1206 free(path);
1207 break;
1211 return err;
1214 static const struct got_error *
1215 cmd_update(int argc, char *argv[])
1217 const struct got_error *error = NULL;
1218 struct got_repository *repo = NULL;
1219 struct got_worktree *worktree = NULL;
1220 char *worktree_path = NULL;
1221 struct got_object_id *commit_id = NULL;
1222 char *commit_id_str = NULL;
1223 const char *branch_name = NULL;
1224 struct got_reference *head_ref = NULL;
1225 struct got_pathlist_head paths;
1226 struct got_pathlist_entry *pe;
1227 int ch, did_something = 0;
1229 TAILQ_INIT(&paths);
1231 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1232 switch (ch) {
1233 case 'b':
1234 branch_name = optarg;
1235 break;
1236 case 'c':
1237 commit_id_str = strdup(optarg);
1238 if (commit_id_str == NULL)
1239 return got_error_from_errno("strdup");
1240 break;
1241 default:
1242 usage_update();
1243 /* NOTREACHED */
1247 argc -= optind;
1248 argv += optind;
1250 #ifndef PROFILE
1251 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1252 "unveil", NULL) == -1)
1253 err(1, "pledge");
1254 #endif
1255 worktree_path = getcwd(NULL, 0);
1256 if (worktree_path == NULL) {
1257 error = got_error_from_errno("getcwd");
1258 goto done;
1260 error = got_worktree_open(&worktree, worktree_path);
1261 if (error)
1262 goto done;
1264 error = check_rebase_or_histedit_in_progress(worktree);
1265 if (error)
1266 goto done;
1268 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1269 NULL);
1270 if (error != NULL)
1271 goto done;
1273 error = apply_unveil(got_repo_get_path(repo), 0,
1274 got_worktree_get_root_path(worktree));
1275 if (error)
1276 goto done;
1278 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1279 if (error)
1280 goto done;
1282 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1283 got_worktree_get_head_ref_name(worktree), 0);
1284 if (error != NULL)
1285 goto done;
1286 if (commit_id_str == NULL) {
1287 error = got_ref_resolve(&commit_id, repo, head_ref);
1288 if (error != NULL)
1289 goto done;
1290 error = got_object_id_str(&commit_id_str, commit_id);
1291 if (error != NULL)
1292 goto done;
1293 } else {
1294 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1295 free(commit_id_str);
1296 commit_id_str = NULL;
1297 if (error)
1298 goto done;
1299 error = got_object_id_str(&commit_id_str, commit_id);
1300 if (error)
1301 goto done;
1304 if (branch_name) {
1305 struct got_object_id *head_commit_id;
1306 TAILQ_FOREACH(pe, &paths, entry) {
1307 if (pe->path_len == 0)
1308 continue;
1309 error = got_error_msg(GOT_ERR_BAD_PATH,
1310 "switching between branches requires that "
1311 "the entire work tree gets updated");
1312 goto done;
1314 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1315 if (error)
1316 goto done;
1317 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1318 free(head_commit_id);
1319 if (error != NULL)
1320 goto done;
1321 error = check_same_branch(commit_id, head_ref, NULL, repo);
1322 if (error)
1323 goto done;
1324 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1325 if (error)
1326 goto done;
1327 } else {
1328 error = check_linear_ancestry(commit_id,
1329 got_worktree_get_base_commit_id(worktree), repo);
1330 if (error != NULL) {
1331 if (error->code == GOT_ERR_ANCESTRY)
1332 error = got_error(GOT_ERR_BRANCH_MOVED);
1333 goto done;
1335 error = check_same_branch(commit_id, head_ref, NULL, repo);
1336 if (error)
1337 goto done;
1340 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1341 commit_id) != 0) {
1342 error = got_worktree_set_base_commit_id(worktree, repo,
1343 commit_id);
1344 if (error)
1345 goto done;
1348 error = got_worktree_checkout_files(worktree, &paths, repo,
1349 update_progress, &did_something, check_cancelled, NULL);
1350 if (error != NULL)
1351 goto done;
1353 if (did_something)
1354 printf("Updated to commit %s\n", commit_id_str);
1355 else
1356 printf("Already up-to-date\n");
1357 done:
1358 free(worktree_path);
1359 TAILQ_FOREACH(pe, &paths, entry)
1360 free((char *)pe->path);
1361 got_pathlist_free(&paths);
1362 free(commit_id);
1363 free(commit_id_str);
1364 return error;
1367 static const struct got_error *
1368 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1369 const char *path, int diff_context, struct got_repository *repo)
1371 const struct got_error *err = NULL;
1372 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1374 if (blob_id1) {
1375 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1376 if (err)
1377 goto done;
1380 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1381 if (err)
1382 goto done;
1384 while (path[0] == '/')
1385 path++;
1386 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1387 stdout);
1388 done:
1389 if (blob1)
1390 got_object_blob_close(blob1);
1391 got_object_blob_close(blob2);
1392 return err;
1395 static const struct got_error *
1396 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1397 const char *path, int diff_context, struct got_repository *repo)
1399 const struct got_error *err = NULL;
1400 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1401 struct got_diff_blob_output_unidiff_arg arg;
1403 if (tree_id1) {
1404 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1405 if (err)
1406 goto done;
1409 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1410 if (err)
1411 goto done;
1413 arg.diff_context = diff_context;
1414 arg.outfile = stdout;
1415 while (path[0] == '/')
1416 path++;
1417 err = got_diff_tree(tree1, tree2, path, path, repo,
1418 got_diff_blob_output_unidiff, &arg, 1);
1419 done:
1420 if (tree1)
1421 got_object_tree_close(tree1);
1422 got_object_tree_close(tree2);
1423 return err;
1426 static const struct got_error *
1427 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1428 const char *path, int diff_context, struct got_repository *repo)
1430 const struct got_error *err = NULL;
1431 struct got_commit_object *pcommit = NULL;
1432 char *id_str1 = NULL, *id_str2 = NULL;
1433 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1434 struct got_object_qid *qid;
1436 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1437 if (qid != NULL) {
1438 err = got_object_open_as_commit(&pcommit, repo,
1439 qid->id);
1440 if (err)
1441 return err;
1444 if (path && path[0] != '\0') {
1445 int obj_type;
1446 err = got_object_id_by_path(&obj_id2, repo, id, path);
1447 if (err)
1448 goto done;
1449 err = got_object_id_str(&id_str2, obj_id2);
1450 if (err) {
1451 free(obj_id2);
1452 goto done;
1454 if (pcommit) {
1455 err = got_object_id_by_path(&obj_id1, repo,
1456 qid->id, path);
1457 if (err) {
1458 free(obj_id2);
1459 goto done;
1461 err = got_object_id_str(&id_str1, obj_id1);
1462 if (err) {
1463 free(obj_id2);
1464 goto done;
1467 err = got_object_get_type(&obj_type, repo, obj_id2);
1468 if (err) {
1469 free(obj_id2);
1470 goto done;
1472 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1473 switch (obj_type) {
1474 case GOT_OBJ_TYPE_BLOB:
1475 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1476 repo);
1477 break;
1478 case GOT_OBJ_TYPE_TREE:
1479 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1480 repo);
1481 break;
1482 default:
1483 err = got_error(GOT_ERR_OBJ_TYPE);
1484 break;
1486 free(obj_id1);
1487 free(obj_id2);
1488 } else {
1489 obj_id2 = got_object_commit_get_tree_id(commit);
1490 err = got_object_id_str(&id_str2, obj_id2);
1491 if (err)
1492 goto done;
1493 obj_id1 = got_object_commit_get_tree_id(pcommit);
1494 err = got_object_id_str(&id_str1, obj_id1);
1495 if (err)
1496 goto done;
1497 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1498 err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1501 done:
1502 free(id_str1);
1503 free(id_str2);
1504 if (pcommit)
1505 got_object_commit_close(pcommit);
1506 return err;
1509 static char *
1510 get_datestr(time_t *time, char *datebuf)
1512 struct tm mytm, *tm;
1513 char *p, *s;
1515 tm = gmtime_r(time, &mytm);
1516 if (tm == NULL)
1517 return NULL;
1518 s = asctime_r(tm, datebuf);
1519 if (s == NULL)
1520 return NULL;
1521 p = strchr(s, '\n');
1522 if (p)
1523 *p = '\0';
1524 return s;
1527 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1529 static const struct got_error *
1530 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1531 struct got_repository *repo, const char *path, int show_patch,
1532 int diff_context, struct got_reflist_head *refs)
1534 const struct got_error *err = NULL;
1535 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1536 char datebuf[26];
1537 time_t committer_time;
1538 const char *author, *committer;
1539 char *refs_str = NULL;
1540 struct got_reflist_entry *re;
1542 SIMPLEQ_FOREACH(re, refs, entry) {
1543 char *s;
1544 const char *name;
1545 struct got_tag_object *tag = NULL;
1546 int cmp;
1548 name = got_ref_get_name(re->ref);
1549 if (strcmp(name, GOT_REF_HEAD) == 0)
1550 continue;
1551 if (strncmp(name, "refs/", 5) == 0)
1552 name += 5;
1553 if (strncmp(name, "got/", 4) == 0)
1554 continue;
1555 if (strncmp(name, "heads/", 6) == 0)
1556 name += 6;
1557 if (strncmp(name, "remotes/", 8) == 0)
1558 name += 8;
1559 if (strncmp(name, "tags/", 5) == 0) {
1560 err = got_object_open_as_tag(&tag, repo, re->id);
1561 if (err) {
1562 if (err->code != GOT_ERR_OBJ_TYPE)
1563 return err;
1564 /* Ref points at something other than a tag. */
1565 err = NULL;
1566 tag = NULL;
1569 cmp = got_object_id_cmp(tag ?
1570 got_object_tag_get_object_id(tag) : re->id, id);
1571 if (tag)
1572 got_object_tag_close(tag);
1573 if (cmp != 0)
1574 continue;
1575 s = refs_str;
1576 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1577 name) == -1) {
1578 err = got_error_from_errno("asprintf");
1579 free(s);
1580 return err;
1582 free(s);
1584 err = got_object_id_str(&id_str, id);
1585 if (err)
1586 return err;
1588 printf(GOT_COMMIT_SEP_STR);
1589 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1590 refs_str ? refs_str : "", refs_str ? ")" : "");
1591 free(id_str);
1592 id_str = NULL;
1593 free(refs_str);
1594 refs_str = NULL;
1595 printf("from: %s\n", got_object_commit_get_author(commit));
1596 committer_time = got_object_commit_get_committer_time(commit);
1597 datestr = get_datestr(&committer_time, datebuf);
1598 if (datestr)
1599 printf("date: %s UTC\n", datestr);
1600 author = got_object_commit_get_author(commit);
1601 committer = got_object_commit_get_committer(commit);
1602 if (strcmp(author, committer) != 0)
1603 printf("via: %s\n", committer);
1604 if (got_object_commit_get_nparents(commit) > 1) {
1605 const struct got_object_id_queue *parent_ids;
1606 struct got_object_qid *qid;
1607 int n = 1;
1608 parent_ids = got_object_commit_get_parent_ids(commit);
1609 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1610 err = got_object_id_str(&id_str, qid->id);
1611 if (err)
1612 return err;
1613 printf("parent %d: %s\n", n++, id_str);
1614 free(id_str);
1618 err = got_object_commit_get_logmsg(&logmsg0, commit);
1619 if (err)
1620 return err;
1622 logmsg = logmsg0;
1623 do {
1624 line = strsep(&logmsg, "\n");
1625 if (line)
1626 printf(" %s\n", line);
1627 } while (line);
1628 free(logmsg0);
1630 if (show_patch) {
1631 err = print_patch(commit, id, path, diff_context, repo);
1632 if (err == 0)
1633 printf("\n");
1636 if (fflush(stdout) != 0 && err == NULL)
1637 err = got_error_from_errno("fflush");
1638 return err;
1641 static const struct got_error *
1642 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1643 char *path, int show_patch, int diff_context, int limit,
1644 int first_parent_traversal, struct got_reflist_head *refs)
1646 const struct got_error *err;
1647 struct got_commit_graph *graph;
1649 err = got_commit_graph_open(&graph, root_id, path,
1650 first_parent_traversal, repo);
1651 if (err)
1652 return err;
1653 err = got_commit_graph_iter_start(graph, root_id, repo,
1654 check_cancelled, NULL);
1655 if (err)
1656 goto done;
1657 for (;;) {
1658 struct got_commit_object *commit;
1659 struct got_object_id *id;
1661 if (sigint_received || sigpipe_received)
1662 break;
1664 err = got_commit_graph_iter_next(&id, graph);
1665 if (err) {
1666 if (err->code == GOT_ERR_ITER_COMPLETED) {
1667 err = NULL;
1668 break;
1670 if (err->code != GOT_ERR_ITER_NEED_MORE)
1671 break;
1672 err = got_commit_graph_fetch_commits(graph, 1, repo,
1673 check_cancelled, NULL);
1674 if (err)
1675 break;
1676 else
1677 continue;
1679 if (id == NULL)
1680 break;
1682 err = got_object_open_as_commit(&commit, repo, id);
1683 if (err)
1684 break;
1685 err = print_commit(commit, id, repo, path, show_patch,
1686 diff_context, refs);
1687 got_object_commit_close(commit);
1688 if (err || (limit && --limit == 0))
1689 break;
1691 done:
1692 got_commit_graph_close(graph);
1693 return err;
1696 __dead static void
1697 usage_log(void)
1699 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1700 "[-r repository-path] [path]\n", getprogname());
1701 exit(1);
1704 static int
1705 get_default_log_limit(void)
1707 const char *got_default_log_limit;
1708 long long n;
1709 const char *errstr;
1711 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1712 if (got_default_log_limit == NULL)
1713 return 0;
1714 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1715 if (errstr != NULL)
1716 return 0;
1717 return n;
1720 static const struct got_error *
1721 cmd_log(int argc, char *argv[])
1723 const struct got_error *error;
1724 struct got_repository *repo = NULL;
1725 struct got_worktree *worktree = NULL;
1726 struct got_commit_object *commit = NULL;
1727 struct got_object_id *id = NULL;
1728 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1729 char *start_commit = NULL;
1730 int diff_context = 3, ch;
1731 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1732 const char *errstr;
1733 struct got_reflist_head refs;
1735 SIMPLEQ_INIT(&refs);
1737 #ifndef PROFILE
1738 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1739 NULL)
1740 == -1)
1741 err(1, "pledge");
1742 #endif
1744 limit = get_default_log_limit();
1746 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1747 switch (ch) {
1748 case 'p':
1749 show_patch = 1;
1750 break;
1751 case 'c':
1752 start_commit = optarg;
1753 break;
1754 case 'C':
1755 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1756 &errstr);
1757 if (errstr != NULL)
1758 err(1, "-C option %s", errstr);
1759 break;
1760 case 'l':
1761 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1762 if (errstr != NULL)
1763 err(1, "-l option %s", errstr);
1764 break;
1765 case 'f':
1766 first_parent_traversal = 1;
1767 break;
1768 case 'r':
1769 repo_path = realpath(optarg, NULL);
1770 if (repo_path == NULL)
1771 err(1, "-r option");
1772 got_path_strip_trailing_slashes(repo_path);
1773 break;
1774 default:
1775 usage_log();
1776 /* NOTREACHED */
1780 argc -= optind;
1781 argv += optind;
1783 cwd = getcwd(NULL, 0);
1784 if (cwd == NULL) {
1785 error = got_error_from_errno("getcwd");
1786 goto done;
1789 error = got_worktree_open(&worktree, cwd);
1790 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1791 goto done;
1792 error = NULL;
1794 if (argc == 0) {
1795 path = strdup("");
1796 if (path == NULL) {
1797 error = got_error_from_errno("strdup");
1798 goto done;
1800 } else if (argc == 1) {
1801 if (worktree) {
1802 error = got_worktree_resolve_path(&path, worktree,
1803 argv[0]);
1804 if (error)
1805 goto done;
1806 } else {
1807 path = strdup(argv[0]);
1808 if (path == NULL) {
1809 error = got_error_from_errno("strdup");
1810 goto done;
1813 } else
1814 usage_log();
1816 if (repo_path == NULL) {
1817 repo_path = worktree ?
1818 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1820 if (repo_path == NULL) {
1821 error = got_error_from_errno("strdup");
1822 goto done;
1825 error = got_repo_open(&repo, repo_path, NULL);
1826 if (error != NULL)
1827 goto done;
1829 error = apply_unveil(got_repo_get_path(repo), 1,
1830 worktree ? got_worktree_get_root_path(worktree) : NULL);
1831 if (error)
1832 goto done;
1834 if (start_commit == NULL) {
1835 struct got_reference *head_ref;
1836 error = got_ref_open(&head_ref, repo,
1837 worktree ? got_worktree_get_head_ref_name(worktree)
1838 : GOT_REF_HEAD, 0);
1839 if (error != NULL)
1840 return error;
1841 error = got_ref_resolve(&id, repo, head_ref);
1842 got_ref_close(head_ref);
1843 if (error != NULL)
1844 return error;
1845 error = got_object_open_as_commit(&commit, repo, id);
1846 } else {
1847 struct got_reference *ref;
1848 error = got_ref_open(&ref, repo, start_commit, 0);
1849 if (error == NULL) {
1850 int obj_type;
1851 error = got_ref_resolve(&id, repo, ref);
1852 got_ref_close(ref);
1853 if (error != NULL)
1854 goto done;
1855 error = got_object_get_type(&obj_type, repo, id);
1856 if (error != NULL)
1857 goto done;
1858 if (obj_type == GOT_OBJ_TYPE_TAG) {
1859 struct got_tag_object *tag;
1860 error = got_object_open_as_tag(&tag, repo, id);
1861 if (error != NULL)
1862 goto done;
1863 if (got_object_tag_get_object_type(tag) !=
1864 GOT_OBJ_TYPE_COMMIT) {
1865 got_object_tag_close(tag);
1866 error = got_error(GOT_ERR_OBJ_TYPE);
1867 goto done;
1869 free(id);
1870 id = got_object_id_dup(
1871 got_object_tag_get_object_id(tag));
1872 if (id == NULL)
1873 error = got_error_from_errno(
1874 "got_object_id_dup");
1875 got_object_tag_close(tag);
1876 if (error)
1877 goto done;
1878 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1879 error = got_error(GOT_ERR_OBJ_TYPE);
1880 goto done;
1882 error = got_object_open_as_commit(&commit, repo, id);
1883 if (error != NULL)
1884 goto done;
1886 if (commit == NULL) {
1887 error = got_repo_match_object_id_prefix(&id,
1888 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1889 if (error != NULL)
1890 return error;
1893 if (error != NULL)
1894 goto done;
1896 if (worktree) {
1897 const char *prefix = got_worktree_get_path_prefix(worktree);
1898 char *p;
1899 if (asprintf(&p, "%s%s%s", prefix,
1900 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1901 error = got_error_from_errno("asprintf");
1902 goto done;
1904 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1905 free(p);
1906 } else
1907 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1908 if (error != NULL)
1909 goto done;
1910 if (in_repo_path) {
1911 free(path);
1912 path = in_repo_path;
1915 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1916 if (error)
1917 goto done;
1919 error = print_commits(id, repo, path, show_patch,
1920 diff_context, limit, first_parent_traversal, &refs);
1921 done:
1922 free(path);
1923 free(repo_path);
1924 free(cwd);
1925 free(id);
1926 if (worktree)
1927 got_worktree_close(worktree);
1928 if (repo) {
1929 const struct got_error *repo_error;
1930 repo_error = got_repo_close(repo);
1931 if (error == NULL)
1932 error = repo_error;
1934 got_ref_list_free(&refs);
1935 return error;
1938 __dead static void
1939 usage_diff(void)
1941 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1942 "[object1 object2 | path]\n", getprogname());
1943 exit(1);
1946 struct print_diff_arg {
1947 struct got_repository *repo;
1948 struct got_worktree *worktree;
1949 int diff_context;
1950 const char *id_str;
1951 int header_shown;
1952 int diff_staged;
1955 static const struct got_error *
1956 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1957 const char *path, struct got_object_id *blob_id,
1958 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1960 struct print_diff_arg *a = arg;
1961 const struct got_error *err = NULL;
1962 struct got_blob_object *blob1 = NULL;
1963 FILE *f2 = NULL;
1964 char *abspath = NULL, *label1 = NULL;
1965 struct stat sb;
1967 if (a->diff_staged) {
1968 if (staged_status != GOT_STATUS_MODIFY &&
1969 staged_status != GOT_STATUS_ADD &&
1970 staged_status != GOT_STATUS_DELETE)
1971 return NULL;
1972 } else {
1973 if (staged_status == GOT_STATUS_DELETE)
1974 return NULL;
1975 if (status == GOT_STATUS_NONEXISTENT)
1976 return got_error_set_errno(ENOENT, path);
1977 if (status != GOT_STATUS_MODIFY &&
1978 status != GOT_STATUS_ADD &&
1979 status != GOT_STATUS_DELETE &&
1980 status != GOT_STATUS_CONFLICT)
1981 return NULL;
1984 if (!a->header_shown) {
1985 printf("diff %s %s%s\n", a->id_str,
1986 got_worktree_get_root_path(a->worktree),
1987 a->diff_staged ? " (staged changes)" : "");
1988 a->header_shown = 1;
1991 if (a->diff_staged) {
1992 const char *label1 = NULL, *label2 = NULL;
1993 switch (staged_status) {
1994 case GOT_STATUS_MODIFY:
1995 label1 = path;
1996 label2 = path;
1997 break;
1998 case GOT_STATUS_ADD:
1999 label2 = path;
2000 break;
2001 case GOT_STATUS_DELETE:
2002 label1 = path;
2003 break;
2004 default:
2005 return got_error(GOT_ERR_FILE_STATUS);
2007 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2008 label1, label2, a->diff_context, a->repo, stdout);
2011 if (staged_status == GOT_STATUS_ADD ||
2012 staged_status == GOT_STATUS_MODIFY) {
2013 char *id_str;
2014 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2015 8192);
2016 if (err)
2017 goto done;
2018 err = got_object_id_str(&id_str, staged_blob_id);
2019 if (err)
2020 goto done;
2021 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2022 err = got_error_from_errno("asprintf");
2023 free(id_str);
2024 goto done;
2026 free(id_str);
2027 } else if (status != GOT_STATUS_ADD) {
2028 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2029 if (err)
2030 goto done;
2033 if (status != GOT_STATUS_DELETE) {
2034 if (asprintf(&abspath, "%s/%s",
2035 got_worktree_get_root_path(a->worktree), path) == -1) {
2036 err = got_error_from_errno("asprintf");
2037 goto done;
2040 f2 = fopen(abspath, "r");
2041 if (f2 == NULL) {
2042 err = got_error_from_errno2("fopen", abspath);
2043 goto done;
2045 if (lstat(abspath, &sb) == -1) {
2046 err = got_error_from_errno2("lstat", abspath);
2047 goto done;
2049 } else
2050 sb.st_size = 0;
2052 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2053 a->diff_context, stdout);
2054 done:
2055 if (blob1)
2056 got_object_blob_close(blob1);
2057 if (f2 && fclose(f2) != 0 && err == NULL)
2058 err = got_error_from_errno("fclose");
2059 free(abspath);
2060 return err;
2063 static const struct got_error *
2064 match_object_id(struct got_object_id **id, char **label,
2065 const char *id_str, int obj_type, int resolve_tags,
2066 struct got_repository *repo)
2068 const struct got_error *err;
2069 struct got_tag_object *tag;
2070 struct got_reference *ref = NULL;
2072 *id = NULL;
2073 *label = NULL;
2075 if (resolve_tags) {
2076 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2077 repo);
2078 if (err == NULL) {
2079 *id = got_object_id_dup(
2080 got_object_tag_get_object_id(tag));
2081 if (*id == NULL)
2082 err = got_error_from_errno("got_object_id_dup");
2083 else if (asprintf(label, "refs/tags/%s",
2084 got_object_tag_get_name(tag)) == -1) {
2085 err = got_error_from_errno("asprintf");
2086 free(*id);
2087 *id = NULL;
2089 got_object_tag_close(tag);
2090 return err;
2091 } else if (err->code != GOT_ERR_NO_OBJ)
2092 return err;
2095 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2096 if (err) {
2097 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2098 return err;
2099 err = got_ref_open(&ref, repo, id_str, 0);
2100 if (err != NULL)
2101 goto done;
2102 *label = strdup(got_ref_get_name(ref));
2103 if (*label == NULL) {
2104 err = got_error_from_errno("strdup");
2105 goto done;
2107 err = got_ref_resolve(id, repo, ref);
2108 } else {
2109 err = got_object_id_str(label, *id);
2110 if (*label == NULL) {
2111 err = got_error_from_errno("strdup");
2112 goto done;
2115 done:
2116 if (ref)
2117 got_ref_close(ref);
2118 return err;
2122 static const struct got_error *
2123 cmd_diff(int argc, char *argv[])
2125 const struct got_error *error;
2126 struct got_repository *repo = NULL;
2127 struct got_worktree *worktree = NULL;
2128 char *cwd = NULL, *repo_path = NULL;
2129 struct got_object_id *id1 = NULL, *id2 = NULL;
2130 const char *id_str1 = NULL, *id_str2 = NULL;
2131 char *label1 = NULL, *label2 = NULL;
2132 int type1, type2;
2133 int diff_context = 3, diff_staged = 0, ch;
2134 const char *errstr;
2135 char *path = NULL;
2137 #ifndef PROFILE
2138 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2139 NULL) == -1)
2140 err(1, "pledge");
2141 #endif
2143 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2144 switch (ch) {
2145 case 'C':
2146 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2147 if (errstr != NULL)
2148 err(1, "-C option %s", errstr);
2149 break;
2150 case 'r':
2151 repo_path = realpath(optarg, NULL);
2152 if (repo_path == NULL)
2153 err(1, "-r option");
2154 got_path_strip_trailing_slashes(repo_path);
2155 break;
2156 case 's':
2157 diff_staged = 1;
2158 break;
2159 default:
2160 usage_diff();
2161 /* NOTREACHED */
2165 argc -= optind;
2166 argv += optind;
2168 cwd = getcwd(NULL, 0);
2169 if (cwd == NULL) {
2170 error = got_error_from_errno("getcwd");
2171 goto done;
2173 error = got_worktree_open(&worktree, cwd);
2174 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2175 goto done;
2176 if (argc <= 1) {
2177 if (worktree == NULL) {
2178 error = got_error(GOT_ERR_NOT_WORKTREE);
2179 goto done;
2181 if (repo_path)
2182 errx(1,
2183 "-r option can't be used when diffing a work tree");
2184 repo_path = strdup(got_worktree_get_repo_path(worktree));
2185 if (repo_path == NULL) {
2186 error = got_error_from_errno("strdup");
2187 goto done;
2189 if (argc == 1) {
2190 error = got_worktree_resolve_path(&path, worktree,
2191 argv[0]);
2192 if (error)
2193 goto done;
2194 } else {
2195 path = strdup("");
2196 if (path == NULL) {
2197 error = got_error_from_errno("strdup");
2198 goto done;
2201 } else if (argc == 2) {
2202 if (diff_staged)
2203 errx(1, "-s option can't be used when diffing "
2204 "objects in repository");
2205 id_str1 = argv[0];
2206 id_str2 = argv[1];
2207 if (worktree && repo_path == NULL) {
2208 repo_path =
2209 strdup(got_worktree_get_repo_path(worktree));
2210 if (repo_path == NULL) {
2211 error = got_error_from_errno("strdup");
2212 goto done;
2215 } else
2216 usage_diff();
2218 if (repo_path == NULL) {
2219 repo_path = getcwd(NULL, 0);
2220 if (repo_path == NULL)
2221 return got_error_from_errno("getcwd");
2224 error = got_repo_open(&repo, repo_path, NULL);
2225 free(repo_path);
2226 if (error != NULL)
2227 goto done;
2229 error = apply_unveil(got_repo_get_path(repo), 1,
2230 worktree ? got_worktree_get_root_path(worktree) : NULL);
2231 if (error)
2232 goto done;
2234 if (argc <= 1) {
2235 struct print_diff_arg arg;
2236 struct got_pathlist_head paths;
2237 char *id_str;
2239 TAILQ_INIT(&paths);
2241 error = got_object_id_str(&id_str,
2242 got_worktree_get_base_commit_id(worktree));
2243 if (error)
2244 goto done;
2245 arg.repo = repo;
2246 arg.worktree = worktree;
2247 arg.diff_context = diff_context;
2248 arg.id_str = id_str;
2249 arg.header_shown = 0;
2250 arg.diff_staged = diff_staged;
2252 error = got_pathlist_append(&paths, path, NULL);
2253 if (error)
2254 goto done;
2256 error = got_worktree_status(worktree, &paths, repo, print_diff,
2257 &arg, check_cancelled, NULL);
2258 free(id_str);
2259 got_pathlist_free(&paths);
2260 goto done;
2263 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2264 repo);
2265 if (error)
2266 goto done;
2268 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2269 repo);
2270 if (error)
2271 goto done;
2273 error = got_object_get_type(&type1, repo, id1);
2274 if (error)
2275 goto done;
2277 error = got_object_get_type(&type2, repo, id2);
2278 if (error)
2279 goto done;
2281 if (type1 != type2) {
2282 error = got_error(GOT_ERR_OBJ_TYPE);
2283 goto done;
2286 switch (type1) {
2287 case GOT_OBJ_TYPE_BLOB:
2288 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2289 diff_context, repo, stdout);
2290 break;
2291 case GOT_OBJ_TYPE_TREE:
2292 error = got_diff_objects_as_trees(id1, id2, "", "",
2293 diff_context, repo, stdout);
2294 break;
2295 case GOT_OBJ_TYPE_COMMIT:
2296 printf("diff %s %s\n", label1, label2);
2297 error = got_diff_objects_as_commits(id1, id2, diff_context,
2298 repo, stdout);
2299 break;
2300 default:
2301 error = got_error(GOT_ERR_OBJ_TYPE);
2304 done:
2305 free(label1);
2306 free(label2);
2307 free(id1);
2308 free(id2);
2309 free(path);
2310 if (worktree)
2311 got_worktree_close(worktree);
2312 if (repo) {
2313 const struct got_error *repo_error;
2314 repo_error = got_repo_close(repo);
2315 if (error == NULL)
2316 error = repo_error;
2318 return error;
2321 __dead static void
2322 usage_blame(void)
2324 fprintf(stderr,
2325 "usage: %s blame [-c commit] [-r repository-path] path\n",
2326 getprogname());
2327 exit(1);
2330 struct blame_line {
2331 int annotated;
2332 char *id_str;
2333 char *committer;
2334 char datebuf[9]; /* YY-MM-DD + NUL */
2337 struct blame_cb_args {
2338 struct blame_line *lines;
2339 int nlines;
2340 int nlines_prec;
2341 int lineno_cur;
2342 off_t *line_offsets;
2343 FILE *f;
2344 struct got_repository *repo;
2347 static const struct got_error *
2348 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2350 const struct got_error *err = NULL;
2351 struct blame_cb_args *a = arg;
2352 struct blame_line *bline;
2353 char *line = NULL;
2354 size_t linesize = 0;
2355 struct got_commit_object *commit = NULL;
2356 off_t offset;
2357 struct tm tm;
2358 time_t committer_time;
2360 if (nlines != a->nlines ||
2361 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2362 return got_error(GOT_ERR_RANGE);
2364 if (sigint_received)
2365 return got_error(GOT_ERR_ITER_COMPLETED);
2367 if (lineno == -1)
2368 return NULL; /* no change in this commit */
2370 /* Annotate this line. */
2371 bline = &a->lines[lineno - 1];
2372 if (bline->annotated)
2373 return NULL;
2374 err = got_object_id_str(&bline->id_str, id);
2375 if (err)
2376 return err;
2378 err = got_object_open_as_commit(&commit, a->repo, id);
2379 if (err)
2380 goto done;
2382 bline->committer = strdup(got_object_commit_get_committer(commit));
2383 if (bline->committer == NULL) {
2384 err = got_error_from_errno("strdup");
2385 goto done;
2388 committer_time = got_object_commit_get_committer_time(commit);
2389 if (localtime_r(&committer_time, &tm) == NULL)
2390 return got_error_from_errno("localtime_r");
2391 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2392 &tm) >= sizeof(bline->datebuf)) {
2393 err = got_error(GOT_ERR_NO_SPACE);
2394 goto done;
2396 bline->annotated = 1;
2398 /* Print lines annotated so far. */
2399 bline = &a->lines[a->lineno_cur - 1];
2400 if (!bline->annotated)
2401 goto done;
2403 offset = a->line_offsets[a->lineno_cur - 1];
2404 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2405 err = got_error_from_errno("fseeko");
2406 goto done;
2409 while (bline->annotated) {
2410 char *smallerthan, *at, *nl, *committer;
2411 size_t len;
2413 if (getline(&line, &linesize, a->f) == -1) {
2414 if (ferror(a->f))
2415 err = got_error_from_errno("getline");
2416 break;
2419 committer = bline->committer;
2420 smallerthan = strchr(committer, '<');
2421 if (smallerthan && smallerthan[1] != '\0')
2422 committer = smallerthan + 1;
2423 at = strchr(committer, '@');
2424 if (at)
2425 *at = '\0';
2426 len = strlen(committer);
2427 if (len >= 9)
2428 committer[8] = '\0';
2430 nl = strchr(line, '\n');
2431 if (nl)
2432 *nl = '\0';
2433 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2434 bline->id_str, bline->datebuf, committer, line);
2436 a->lineno_cur++;
2437 bline = &a->lines[a->lineno_cur - 1];
2439 done:
2440 if (commit)
2441 got_object_commit_close(commit);
2442 free(line);
2443 return err;
2446 static const struct got_error *
2447 cmd_blame(int argc, char *argv[])
2449 const struct got_error *error;
2450 struct got_repository *repo = NULL;
2451 struct got_worktree *worktree = NULL;
2452 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2453 struct got_object_id *obj_id = NULL;
2454 struct got_object_id *commit_id = NULL;
2455 struct got_blob_object *blob = NULL;
2456 char *commit_id_str = NULL;
2457 struct blame_cb_args bca;
2458 int ch, obj_type, i;
2459 size_t filesize;
2461 memset(&bca, 0, sizeof(bca));
2463 #ifndef PROFILE
2464 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2465 NULL) == -1)
2466 err(1, "pledge");
2467 #endif
2469 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2470 switch (ch) {
2471 case 'c':
2472 commit_id_str = optarg;
2473 break;
2474 case 'r':
2475 repo_path = realpath(optarg, NULL);
2476 if (repo_path == NULL)
2477 err(1, "-r option");
2478 got_path_strip_trailing_slashes(repo_path);
2479 break;
2480 default:
2481 usage_blame();
2482 /* NOTREACHED */
2486 argc -= optind;
2487 argv += optind;
2489 if (argc == 1)
2490 path = argv[0];
2491 else
2492 usage_blame();
2494 cwd = getcwd(NULL, 0);
2495 if (cwd == NULL) {
2496 error = got_error_from_errno("getcwd");
2497 goto done;
2499 if (repo_path == NULL) {
2500 error = got_worktree_open(&worktree, cwd);
2501 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2502 goto done;
2503 else
2504 error = NULL;
2505 if (worktree) {
2506 repo_path =
2507 strdup(got_worktree_get_repo_path(worktree));
2508 if (repo_path == NULL)
2509 error = got_error_from_errno("strdup");
2510 if (error)
2511 goto done;
2512 } else {
2513 repo_path = strdup(cwd);
2514 if (repo_path == NULL) {
2515 error = got_error_from_errno("strdup");
2516 goto done;
2521 error = got_repo_open(&repo, repo_path, NULL);
2522 if (error != NULL)
2523 goto done;
2525 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2526 if (error)
2527 goto done;
2529 if (worktree) {
2530 const char *prefix = got_worktree_get_path_prefix(worktree);
2531 char *p, *worktree_subdir = cwd +
2532 strlen(got_worktree_get_root_path(worktree));
2533 if (asprintf(&p, "%s%s%s%s%s",
2534 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2535 worktree_subdir, worktree_subdir[0] ? "/" : "",
2536 path) == -1) {
2537 error = got_error_from_errno("asprintf");
2538 goto done;
2540 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2541 free(p);
2542 } else {
2543 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2545 if (error)
2546 goto done;
2548 if (commit_id_str == NULL) {
2549 struct got_reference *head_ref;
2550 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2551 if (error != NULL)
2552 goto done;
2553 error = got_ref_resolve(&commit_id, repo, head_ref);
2554 got_ref_close(head_ref);
2555 if (error != NULL)
2556 goto done;
2557 } else {
2558 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2559 if (error)
2560 goto done;
2563 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2564 if (error)
2565 goto done;
2566 if (obj_id == NULL) {
2567 error = got_error(GOT_ERR_NO_OBJ);
2568 goto done;
2571 error = got_object_get_type(&obj_type, repo, obj_id);
2572 if (error)
2573 goto done;
2575 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2576 error = got_error(GOT_ERR_OBJ_TYPE);
2577 goto done;
2580 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2581 if (error)
2582 goto done;
2583 bca.f = got_opentemp();
2584 if (bca.f == NULL) {
2585 error = got_error_from_errno("got_opentemp");
2586 goto done;
2588 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2589 &bca.line_offsets, bca.f, blob);
2590 if (error || bca.nlines == 0)
2591 goto done;
2593 /* Don't include \n at EOF in the blame line count. */
2594 if (bca.line_offsets[bca.nlines - 1] == filesize)
2595 bca.nlines--;
2597 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2598 if (bca.lines == NULL) {
2599 error = got_error_from_errno("calloc");
2600 goto done;
2602 bca.lineno_cur = 1;
2603 bca.nlines_prec = 0;
2604 i = bca.nlines;
2605 while (i > 0) {
2606 i /= 10;
2607 bca.nlines_prec++;
2609 bca.repo = repo;
2611 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2612 check_cancelled, NULL);
2613 if (error)
2614 goto done;
2615 done:
2616 free(in_repo_path);
2617 free(repo_path);
2618 free(cwd);
2619 free(commit_id);
2620 free(obj_id);
2621 if (blob)
2622 got_object_blob_close(blob);
2623 if (worktree)
2624 got_worktree_close(worktree);
2625 if (repo) {
2626 const struct got_error *repo_error;
2627 repo_error = got_repo_close(repo);
2628 if (error == NULL)
2629 error = repo_error;
2631 if (bca.lines) {
2632 for (i = 0; i < bca.nlines; i++) {
2633 struct blame_line *bline = &bca.lines[i];
2634 free(bline->id_str);
2635 free(bline->committer);
2637 free(bca.lines);
2639 free(bca.line_offsets);
2640 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2641 error = got_error_from_errno("fclose");
2642 return error;
2645 __dead static void
2646 usage_tree(void)
2648 fprintf(stderr,
2649 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2650 getprogname());
2651 exit(1);
2654 static void
2655 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2656 const char *root_path)
2658 int is_root_path = (strcmp(path, root_path) == 0);
2659 const char *modestr = "";
2661 path += strlen(root_path);
2662 while (path[0] == '/')
2663 path++;
2665 if (got_object_tree_entry_is_submodule(te))
2666 modestr = "$";
2667 else if (S_ISLNK(te->mode))
2668 modestr = "@";
2669 else if (S_ISDIR(te->mode))
2670 modestr = "/";
2671 else if (te->mode & S_IXUSR)
2672 modestr = "*";
2674 printf("%s%s%s%s%s\n", id ? id : "", path,
2675 is_root_path ? "" : "/", te->name, modestr);
2678 static const struct got_error *
2679 print_tree(const char *path, struct got_object_id *commit_id,
2680 int show_ids, int recurse, const char *root_path,
2681 struct got_repository *repo)
2683 const struct got_error *err = NULL;
2684 struct got_object_id *tree_id = NULL;
2685 struct got_tree_object *tree = NULL;
2686 const struct got_tree_entries *entries;
2687 struct got_tree_entry *te;
2689 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2690 if (err)
2691 goto done;
2693 err = got_object_open_as_tree(&tree, repo, tree_id);
2694 if (err)
2695 goto done;
2696 entries = got_object_tree_get_entries(tree);
2697 te = SIMPLEQ_FIRST(&entries->head);
2698 while (te) {
2699 char *id = NULL;
2701 if (sigint_received || sigpipe_received)
2702 break;
2704 if (show_ids) {
2705 char *id_str;
2706 err = got_object_id_str(&id_str, te->id);
2707 if (err)
2708 goto done;
2709 if (asprintf(&id, "%s ", id_str) == -1) {
2710 err = got_error_from_errno("asprintf");
2711 free(id_str);
2712 goto done;
2714 free(id_str);
2716 print_entry(te, id, path, root_path);
2717 free(id);
2719 if (recurse && S_ISDIR(te->mode)) {
2720 char *child_path;
2721 if (asprintf(&child_path, "%s%s%s", path,
2722 path[0] == '/' && path[1] == '\0' ? "" : "/",
2723 te->name) == -1) {
2724 err = got_error_from_errno("asprintf");
2725 goto done;
2727 err = print_tree(child_path, commit_id, show_ids, 1,
2728 root_path, repo);
2729 free(child_path);
2730 if (err)
2731 goto done;
2734 te = SIMPLEQ_NEXT(te, entry);
2736 done:
2737 if (tree)
2738 got_object_tree_close(tree);
2739 free(tree_id);
2740 return err;
2743 static const struct got_error *
2744 cmd_tree(int argc, char *argv[])
2746 const struct got_error *error;
2747 struct got_repository *repo = NULL;
2748 struct got_worktree *worktree = NULL;
2749 const char *path;
2750 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2751 struct got_object_id *commit_id = NULL;
2752 char *commit_id_str = NULL;
2753 int show_ids = 0, recurse = 0;
2754 int ch;
2756 #ifndef PROFILE
2757 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2758 NULL) == -1)
2759 err(1, "pledge");
2760 #endif
2762 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2763 switch (ch) {
2764 case 'c':
2765 commit_id_str = optarg;
2766 break;
2767 case 'r':
2768 repo_path = realpath(optarg, NULL);
2769 if (repo_path == NULL)
2770 err(1, "-r option");
2771 got_path_strip_trailing_slashes(repo_path);
2772 break;
2773 case 'i':
2774 show_ids = 1;
2775 break;
2776 case 'R':
2777 recurse = 1;
2778 break;
2779 default:
2780 usage_tree();
2781 /* NOTREACHED */
2785 argc -= optind;
2786 argv += optind;
2788 if (argc == 1)
2789 path = argv[0];
2790 else if (argc > 1)
2791 usage_tree();
2792 else
2793 path = NULL;
2795 cwd = getcwd(NULL, 0);
2796 if (cwd == NULL) {
2797 error = got_error_from_errno("getcwd");
2798 goto done;
2800 if (repo_path == NULL) {
2801 error = got_worktree_open(&worktree, cwd);
2802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2803 goto done;
2804 else
2805 error = NULL;
2806 if (worktree) {
2807 repo_path =
2808 strdup(got_worktree_get_repo_path(worktree));
2809 if (repo_path == NULL)
2810 error = got_error_from_errno("strdup");
2811 if (error)
2812 goto done;
2813 } else {
2814 repo_path = strdup(cwd);
2815 if (repo_path == NULL) {
2816 error = got_error_from_errno("strdup");
2817 goto done;
2822 error = got_repo_open(&repo, repo_path, NULL);
2823 if (error != NULL)
2824 goto done;
2826 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2827 if (error)
2828 goto done;
2830 if (path == NULL) {
2831 if (worktree) {
2832 char *p, *worktree_subdir = cwd +
2833 strlen(got_worktree_get_root_path(worktree));
2834 if (asprintf(&p, "%s/%s",
2835 got_worktree_get_path_prefix(worktree),
2836 worktree_subdir) == -1) {
2837 error = got_error_from_errno("asprintf");
2838 goto done;
2840 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2841 free(p);
2842 if (error)
2843 goto done;
2844 } else
2845 path = "/";
2847 if (in_repo_path == NULL) {
2848 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2849 if (error != NULL)
2850 goto done;
2853 if (commit_id_str == NULL) {
2854 struct got_reference *head_ref;
2855 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2856 if (error != NULL)
2857 goto done;
2858 error = got_ref_resolve(&commit_id, repo, head_ref);
2859 got_ref_close(head_ref);
2860 if (error != NULL)
2861 goto done;
2862 } else {
2863 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2864 if (error)
2865 goto done;
2868 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2869 in_repo_path, repo);
2870 done:
2871 free(in_repo_path);
2872 free(repo_path);
2873 free(cwd);
2874 free(commit_id);
2875 if (worktree)
2876 got_worktree_close(worktree);
2877 if (repo) {
2878 const struct got_error *repo_error;
2879 repo_error = got_repo_close(repo);
2880 if (error == NULL)
2881 error = repo_error;
2883 return error;
2886 __dead static void
2887 usage_status(void)
2889 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2890 exit(1);
2893 static const struct got_error *
2894 print_status(void *arg, unsigned char status, unsigned char staged_status,
2895 const char *path, struct got_object_id *blob_id,
2896 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2898 if (status == staged_status && (status == GOT_STATUS_DELETE))
2899 status = GOT_STATUS_NO_CHANGE;
2900 printf("%c%c %s\n", status, staged_status, path);
2901 return NULL;
2904 static const struct got_error *
2905 cmd_status(int argc, char *argv[])
2907 const struct got_error *error = NULL;
2908 struct got_repository *repo = NULL;
2909 struct got_worktree *worktree = NULL;
2910 char *cwd = NULL;
2911 struct got_pathlist_head paths;
2912 struct got_pathlist_entry *pe;
2913 int ch;
2915 TAILQ_INIT(&paths);
2917 while ((ch = getopt(argc, argv, "")) != -1) {
2918 switch (ch) {
2919 default:
2920 usage_status();
2921 /* NOTREACHED */
2925 argc -= optind;
2926 argv += optind;
2928 #ifndef PROFILE
2929 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2930 NULL) == -1)
2931 err(1, "pledge");
2932 #endif
2933 cwd = getcwd(NULL, 0);
2934 if (cwd == NULL) {
2935 error = got_error_from_errno("getcwd");
2936 goto done;
2939 error = got_worktree_open(&worktree, cwd);
2940 if (error != NULL)
2941 goto done;
2943 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2944 NULL);
2945 if (error != NULL)
2946 goto done;
2948 error = apply_unveil(got_repo_get_path(repo), 1,
2949 got_worktree_get_root_path(worktree));
2950 if (error)
2951 goto done;
2953 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2954 if (error)
2955 goto done;
2957 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2958 check_cancelled, NULL);
2959 done:
2960 TAILQ_FOREACH(pe, &paths, entry)
2961 free((char *)pe->path);
2962 got_pathlist_free(&paths);
2963 free(cwd);
2964 return error;
2967 __dead static void
2968 usage_ref(void)
2970 fprintf(stderr,
2971 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2972 getprogname());
2973 exit(1);
2976 static const struct got_error *
2977 list_refs(struct got_repository *repo)
2979 static const struct got_error *err = NULL;
2980 struct got_reflist_head refs;
2981 struct got_reflist_entry *re;
2983 SIMPLEQ_INIT(&refs);
2984 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2985 if (err)
2986 return err;
2988 SIMPLEQ_FOREACH(re, &refs, entry) {
2989 char *refstr;
2990 refstr = got_ref_to_str(re->ref);
2991 if (refstr == NULL)
2992 return got_error_from_errno("got_ref_to_str");
2993 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2994 free(refstr);
2997 got_ref_list_free(&refs);
2998 return NULL;
3001 static const struct got_error *
3002 delete_ref(struct got_repository *repo, const char *refname)
3004 const struct got_error *err = NULL;
3005 struct got_reference *ref;
3007 err = got_ref_open(&ref, repo, refname, 0);
3008 if (err)
3009 return err;
3011 err = got_ref_delete(ref, repo);
3012 got_ref_close(ref);
3013 return err;
3016 static const struct got_error *
3017 add_ref(struct got_repository *repo, const char *refname, const char *target)
3019 const struct got_error *err = NULL;
3020 struct got_object_id *id;
3021 struct got_reference *ref = NULL;
3024 * Don't let the user create a reference named '-'.
3025 * While technically a valid reference name, this case is usually
3026 * an unintended typo.
3028 if (refname[0] == '-' && refname[1] == '\0')
3029 return got_error(GOT_ERR_BAD_REF_NAME);
3031 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3032 repo);
3033 if (err) {
3034 struct got_reference *target_ref;
3036 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3037 return err;
3038 err = got_ref_open(&target_ref, repo, target, 0);
3039 if (err)
3040 return err;
3041 err = got_ref_resolve(&id, repo, target_ref);
3042 got_ref_close(target_ref);
3043 if (err)
3044 return err;
3047 err = got_ref_alloc(&ref, refname, id);
3048 if (err)
3049 goto done;
3051 err = got_ref_write(ref, repo);
3052 done:
3053 if (ref)
3054 got_ref_close(ref);
3055 free(id);
3056 return err;
3059 static const struct got_error *
3060 add_symref(struct got_repository *repo, const char *refname, const char *target)
3062 const struct got_error *err = NULL;
3063 struct got_reference *ref = NULL;
3064 struct got_reference *target_ref = NULL;
3067 * Don't let the user create a reference named '-'.
3068 * While technically a valid reference name, this case is usually
3069 * an unintended typo.
3071 if (refname[0] == '-' && refname[1] == '\0')
3072 return got_error(GOT_ERR_BAD_REF_NAME);
3074 err = got_ref_open(&target_ref, repo, target, 0);
3075 if (err)
3076 return err;
3078 err = got_ref_alloc_symref(&ref, refname, target_ref);
3079 if (err)
3080 goto done;
3082 err = got_ref_write(ref, repo);
3083 done:
3084 if (target_ref)
3085 got_ref_close(target_ref);
3086 if (ref)
3087 got_ref_close(ref);
3088 return err;
3091 static const struct got_error *
3092 cmd_ref(int argc, char *argv[])
3094 const struct got_error *error = NULL;
3095 struct got_repository *repo = NULL;
3096 struct got_worktree *worktree = NULL;
3097 char *cwd = NULL, *repo_path = NULL;
3098 int ch, do_list = 0, create_symref = 0;
3099 const char *delref = NULL;
3101 /* TODO: Add -s option for adding symbolic references. */
3102 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3103 switch (ch) {
3104 case 'd':
3105 delref = optarg;
3106 break;
3107 case 'r':
3108 repo_path = realpath(optarg, NULL);
3109 if (repo_path == NULL)
3110 err(1, "-r option");
3111 got_path_strip_trailing_slashes(repo_path);
3112 break;
3113 case 'l':
3114 do_list = 1;
3115 break;
3116 case 's':
3117 create_symref = 1;
3118 break;
3119 default:
3120 usage_ref();
3121 /* NOTREACHED */
3125 if (do_list && delref)
3126 errx(1, "-l and -d options are mutually exclusive\n");
3128 argc -= optind;
3129 argv += optind;
3131 if (do_list || delref) {
3132 if (create_symref)
3133 errx(1, "-s option cannot be used together with the "
3134 "-l or -d options");
3135 if (argc > 0)
3136 usage_ref();
3137 } else if (argc != 2)
3138 usage_ref();
3140 #ifndef PROFILE
3141 if (do_list) {
3142 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3143 NULL) == -1)
3144 err(1, "pledge");
3145 } else {
3146 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3147 "sendfd unveil", NULL) == -1)
3148 err(1, "pledge");
3150 #endif
3151 cwd = getcwd(NULL, 0);
3152 if (cwd == NULL) {
3153 error = got_error_from_errno("getcwd");
3154 goto done;
3157 if (repo_path == NULL) {
3158 error = got_worktree_open(&worktree, cwd);
3159 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3160 goto done;
3161 else
3162 error = NULL;
3163 if (worktree) {
3164 repo_path =
3165 strdup(got_worktree_get_repo_path(worktree));
3166 if (repo_path == NULL)
3167 error = got_error_from_errno("strdup");
3168 if (error)
3169 goto done;
3170 } else {
3171 repo_path = strdup(cwd);
3172 if (repo_path == NULL) {
3173 error = got_error_from_errno("strdup");
3174 goto done;
3179 error = got_repo_open(&repo, repo_path, NULL);
3180 if (error != NULL)
3181 goto done;
3183 error = apply_unveil(got_repo_get_path(repo), do_list,
3184 worktree ? got_worktree_get_root_path(worktree) : NULL);
3185 if (error)
3186 goto done;
3188 if (do_list)
3189 error = list_refs(repo);
3190 else if (delref)
3191 error = delete_ref(repo, delref);
3192 else if (create_symref)
3193 error = add_symref(repo, argv[0], argv[1]);
3194 else
3195 error = add_ref(repo, argv[0], argv[1]);
3196 done:
3197 if (repo)
3198 got_repo_close(repo);
3199 if (worktree)
3200 got_worktree_close(worktree);
3201 free(cwd);
3202 free(repo_path);
3203 return error;
3206 __dead static void
3207 usage_branch(void)
3209 fprintf(stderr,
3210 "usage: %s branch [-r repository] [-l] | -d name | "
3211 "[name [commit]]\n", getprogname());
3212 exit(1);
3215 static const struct got_error *
3216 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3217 struct got_reference *ref)
3219 const struct got_error *err = NULL;
3220 const char *refname, *marker = " ";
3221 char *refstr;
3223 refname = got_ref_get_name(ref);
3224 if (worktree && strcmp(refname,
3225 got_worktree_get_head_ref_name(worktree)) == 0) {
3226 struct got_object_id *id = NULL;
3228 err = got_ref_resolve(&id, repo, ref);
3229 if (err)
3230 return err;
3231 if (got_object_id_cmp(id,
3232 got_worktree_get_base_commit_id(worktree)) == 0)
3233 marker = "* ";
3234 else
3235 marker = "~ ";
3236 free(id);
3239 if (strncmp(refname, "refs/heads/", 11) == 0)
3240 refname += 11;
3241 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3242 refname += 18;
3244 refstr = got_ref_to_str(ref);
3245 if (refstr == NULL)
3246 return got_error_from_errno("got_ref_to_str");
3248 printf("%s%s: %s\n", marker, refname, refstr);
3249 free(refstr);
3250 return NULL;
3253 static const struct got_error *
3254 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3256 const char *refname;
3258 if (worktree == NULL)
3259 return got_error(GOT_ERR_NOT_WORKTREE);
3261 refname = got_worktree_get_head_ref_name(worktree);
3263 if (strncmp(refname, "refs/heads/", 11) == 0)
3264 refname += 11;
3265 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3266 refname += 18;
3268 printf("%s\n", refname);
3270 return NULL;
3273 static const struct got_error *
3274 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3276 static const struct got_error *err = NULL;
3277 struct got_reflist_head refs;
3278 struct got_reflist_entry *re;
3279 struct got_reference *temp_ref = NULL;
3280 int rebase_in_progress, histedit_in_progress;
3282 SIMPLEQ_INIT(&refs);
3284 if (worktree) {
3285 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3286 worktree);
3287 if (err)
3288 return err;
3290 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3291 worktree);
3292 if (err)
3293 return err;
3295 if (rebase_in_progress || histedit_in_progress) {
3296 err = got_ref_open(&temp_ref, repo,
3297 got_worktree_get_head_ref_name(worktree), 0);
3298 if (err)
3299 return err;
3300 list_branch(repo, worktree, temp_ref);
3301 got_ref_close(temp_ref);
3305 err = got_ref_list(&refs, repo, "refs/heads",
3306 got_ref_cmp_by_name, NULL);
3307 if (err)
3308 return err;
3310 SIMPLEQ_FOREACH(re, &refs, entry)
3311 list_branch(repo, worktree, re->ref);
3313 got_ref_list_free(&refs);
3314 return NULL;
3317 static const struct got_error *
3318 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3319 const char *branch_name)
3321 const struct got_error *err = NULL;
3322 struct got_reference *ref = NULL;
3323 char *refname;
3325 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3326 return got_error_from_errno("asprintf");
3328 err = got_ref_open(&ref, repo, refname, 0);
3329 if (err)
3330 goto done;
3332 if (worktree &&
3333 strcmp(got_worktree_get_head_ref_name(worktree),
3334 got_ref_get_name(ref)) == 0) {
3335 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3336 "will not delete this work tree's current branch");
3337 goto done;
3340 err = got_ref_delete(ref, repo);
3341 done:
3342 if (ref)
3343 got_ref_close(ref);
3344 free(refname);
3345 return err;
3348 static const struct got_error *
3349 add_branch(struct got_repository *repo, const char *branch_name,
3350 const char *base_branch)
3352 const struct got_error *err = NULL;
3353 struct got_object_id *id = NULL;
3354 char *label;
3355 struct got_reference *ref = NULL;
3356 char *base_refname = NULL, *refname = NULL;
3359 * Don't let the user create a branch named '-'.
3360 * While technically a valid reference name, this case is usually
3361 * an unintended typo.
3363 if (branch_name[0] == '-' && branch_name[1] == '\0')
3364 return got_error(GOT_ERR_BAD_REF_NAME);
3366 err = match_object_id(&id, &label, base_branch,
3367 GOT_OBJ_TYPE_COMMIT, 1, repo);
3368 if (err)
3369 return err;
3371 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3372 err = got_error_from_errno("asprintf");
3373 goto done;
3376 err = got_ref_open(&ref, repo, refname, 0);
3377 if (err == NULL) {
3378 err = got_error(GOT_ERR_BRANCH_EXISTS);
3379 goto done;
3380 } else if (err->code != GOT_ERR_NOT_REF)
3381 goto done;
3383 err = got_ref_alloc(&ref, refname, id);
3384 if (err)
3385 goto done;
3387 err = got_ref_write(ref, repo);
3388 done:
3389 if (ref)
3390 got_ref_close(ref);
3391 free(id);
3392 free(base_refname);
3393 free(refname);
3394 return err;
3397 static const struct got_error *
3398 cmd_branch(int argc, char *argv[])
3400 const struct got_error *error = NULL;
3401 struct got_repository *repo = NULL;
3402 struct got_worktree *worktree = NULL;
3403 char *cwd = NULL, *repo_path = NULL;
3404 int ch, do_list = 0, do_show = 0;
3405 const char *delref = NULL;
3407 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3408 switch (ch) {
3409 case 'd':
3410 delref = optarg;
3411 break;
3412 case 'r':
3413 repo_path = realpath(optarg, NULL);
3414 if (repo_path == NULL)
3415 err(1, "-r option");
3416 got_path_strip_trailing_slashes(repo_path);
3417 break;
3418 case 'l':
3419 do_list = 1;
3420 break;
3421 default:
3422 usage_branch();
3423 /* NOTREACHED */
3427 if (do_list && delref)
3428 errx(1, "-l and -d options are mutually exclusive\n");
3430 argc -= optind;
3431 argv += optind;
3433 if (!do_list && !delref && argc == 0)
3434 do_show = 1;
3436 if (do_list || delref) {
3437 if (argc > 0)
3438 usage_branch();
3439 } else if (!do_show && (argc < 1 || argc > 2))
3440 usage_branch();
3442 #ifndef PROFILE
3443 if (do_list || do_show) {
3444 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3445 NULL) == -1)
3446 err(1, "pledge");
3447 } else {
3448 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3449 "sendfd unveil", NULL) == -1)
3450 err(1, "pledge");
3452 #endif
3453 cwd = getcwd(NULL, 0);
3454 if (cwd == NULL) {
3455 error = got_error_from_errno("getcwd");
3456 goto done;
3459 if (repo_path == NULL) {
3460 error = got_worktree_open(&worktree, cwd);
3461 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3462 goto done;
3463 else
3464 error = NULL;
3465 if (worktree) {
3466 repo_path =
3467 strdup(got_worktree_get_repo_path(worktree));
3468 if (repo_path == NULL)
3469 error = got_error_from_errno("strdup");
3470 if (error)
3471 goto done;
3472 } else {
3473 repo_path = strdup(cwd);
3474 if (repo_path == NULL) {
3475 error = got_error_from_errno("strdup");
3476 goto done;
3481 error = got_repo_open(&repo, repo_path, NULL);
3482 if (error != NULL)
3483 goto done;
3485 error = apply_unveil(got_repo_get_path(repo), do_list,
3486 worktree ? got_worktree_get_root_path(worktree) : NULL);
3487 if (error)
3488 goto done;
3490 if (do_show)
3491 error = show_current_branch(repo, worktree);
3492 else if (do_list)
3493 error = list_branches(repo, worktree);
3494 else if (delref)
3495 error = delete_branch(repo, worktree, delref);
3496 else {
3497 const char *base_branch;
3498 if (argc == 1) {
3499 base_branch = worktree ?
3500 got_worktree_get_head_ref_name(worktree) :
3501 GOT_REF_HEAD;
3502 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3503 base_branch += 11;
3504 } else
3505 base_branch = argv[1];
3506 error = add_branch(repo, argv[0], base_branch);
3508 done:
3509 if (repo)
3510 got_repo_close(repo);
3511 if (worktree)
3512 got_worktree_close(worktree);
3513 free(cwd);
3514 free(repo_path);
3515 return error;
3519 __dead static void
3520 usage_tag(void)
3522 fprintf(stderr,
3523 "usage: %s tag [-r repository] | -l | "
3524 "[-m message] name [commit]\n", getprogname());
3525 exit(1);
3528 #if 0
3529 static const struct got_error *
3530 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3532 const struct got_error *err = NULL;
3533 struct got_reflist_entry *re, *se, *new;
3534 struct got_object_id *re_id, *se_id;
3535 struct got_tag_object *re_tag, *se_tag;
3536 time_t re_time, se_time;
3538 SIMPLEQ_FOREACH(re, tags, entry) {
3539 se = SIMPLEQ_FIRST(sorted);
3540 if (se == NULL) {
3541 err = got_reflist_entry_dup(&new, re);
3542 if (err)
3543 return err;
3544 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3545 continue;
3546 } else {
3547 err = got_ref_resolve(&re_id, repo, re->ref);
3548 if (err)
3549 break;
3550 err = got_object_open_as_tag(&re_tag, repo, re_id);
3551 free(re_id);
3552 if (err)
3553 break;
3554 re_time = got_object_tag_get_tagger_time(re_tag);
3555 got_object_tag_close(re_tag);
3558 while (se) {
3559 err = got_ref_resolve(&se_id, repo, re->ref);
3560 if (err)
3561 break;
3562 err = got_object_open_as_tag(&se_tag, repo, se_id);
3563 free(se_id);
3564 if (err)
3565 break;
3566 se_time = got_object_tag_get_tagger_time(se_tag);
3567 got_object_tag_close(se_tag);
3569 if (se_time > re_time) {
3570 err = got_reflist_entry_dup(&new, re);
3571 if (err)
3572 return err;
3573 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3574 break;
3576 se = SIMPLEQ_NEXT(se, entry);
3577 continue;
3580 done:
3581 return err;
3583 #endif
3585 static const struct got_error *
3586 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3587 struct got_reference *ref2)
3589 const struct got_error *err = NULL;
3590 struct got_repository *repo = arg;
3591 struct got_object_id *id1, *id2 = NULL;
3592 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3593 time_t time1, time2;
3595 *cmp = 0;
3597 err = got_ref_resolve(&id1, repo, ref1);
3598 if (err)
3599 return err;
3600 err = got_object_open_as_tag(&tag1, repo, id1);
3601 if (err)
3602 goto done;
3604 err = got_ref_resolve(&id2, repo, ref2);
3605 if (err)
3606 goto done;
3607 err = got_object_open_as_tag(&tag2, repo, id2);
3608 if (err)
3609 goto done;
3611 time1 = got_object_tag_get_tagger_time(tag1);
3612 time2 = got_object_tag_get_tagger_time(tag2);
3614 /* Put latest tags first. */
3615 if (time1 < time2)
3616 *cmp = 1;
3617 else if (time1 > time2)
3618 *cmp = -1;
3619 else
3620 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3621 done:
3622 free(id1);
3623 free(id2);
3624 if (tag1)
3625 got_object_tag_close(tag1);
3626 if (tag2)
3627 got_object_tag_close(tag2);
3628 return err;
3631 static const struct got_error *
3632 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3634 static const struct got_error *err = NULL;
3635 struct got_reflist_head refs;
3636 struct got_reflist_entry *re;
3638 SIMPLEQ_INIT(&refs);
3640 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3641 if (err)
3642 return err;
3644 SIMPLEQ_FOREACH(re, &refs, entry) {
3645 const char *refname;
3646 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3647 char datebuf[26];
3648 time_t tagger_time;
3649 struct got_object_id *id;
3650 struct got_tag_object *tag;
3652 refname = got_ref_get_name(re->ref);
3653 if (strncmp(refname, "refs/tags/", 10) != 0)
3654 continue;
3655 refname += 10;
3656 refstr = got_ref_to_str(re->ref);
3657 if (refstr == NULL) {
3658 err = got_error_from_errno("got_ref_to_str");
3659 break;
3661 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3662 free(refstr);
3664 err = got_ref_resolve(&id, repo, re->ref);
3665 if (err)
3666 break;
3667 err = got_object_open_as_tag(&tag, repo, id);
3668 free(id);
3669 if (err)
3670 break;
3671 printf("from: %s\n", got_object_tag_get_tagger(tag));
3672 tagger_time = got_object_tag_get_tagger_time(tag);
3673 datestr = get_datestr(&tagger_time, datebuf);
3674 if (datestr)
3675 printf("date: %s UTC\n", datestr);
3676 err = got_object_id_str(&id_str,
3677 got_object_tag_get_object_id(tag));
3678 if (err)
3679 break;
3680 switch (got_object_tag_get_object_type(tag)) {
3681 case GOT_OBJ_TYPE_BLOB:
3682 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3683 break;
3684 case GOT_OBJ_TYPE_TREE:
3685 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3686 break;
3687 case GOT_OBJ_TYPE_COMMIT:
3688 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3689 break;
3690 case GOT_OBJ_TYPE_TAG:
3691 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3692 break;
3693 default:
3694 break;
3696 free(id_str);
3697 tagmsg0 = strdup(got_object_tag_get_message(tag));
3698 got_object_tag_close(tag);
3699 if (tagmsg0 == NULL) {
3700 err = got_error_from_errno("strdup");
3701 break;
3704 tagmsg = tagmsg0;
3705 do {
3706 line = strsep(&tagmsg, "\n");
3707 if (line)
3708 printf(" %s\n", line);
3709 } while (line);
3710 free(tagmsg0);
3713 got_ref_list_free(&refs);
3714 return NULL;
3717 static const struct got_error *
3718 get_tag_message(char **tagmsg, const char *commit_id_str,
3719 const char *tag_name, const char *repo_path)
3721 const struct got_error *err = NULL;
3722 char *template = NULL, *initial_content = NULL;
3723 char *tagmsg_path = NULL, *editor = NULL;
3724 int fd = -1;
3726 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3727 err = got_error_from_errno("asprintf");
3728 goto done;
3731 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3732 commit_id_str, tag_name) == -1) {
3733 err = got_error_from_errno("asprintf");
3734 goto done;
3737 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3738 if (err)
3739 goto done;
3741 dprintf(fd, initial_content);
3742 close(fd);
3744 err = get_editor(&editor);
3745 if (err)
3746 goto done;
3747 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3748 done:
3749 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3750 unlink(tagmsg_path);
3751 free(tagmsg_path);
3752 tagmsg_path = NULL;
3754 free(initial_content);
3755 free(template);
3756 free(editor);
3758 /* Editor is done; we can now apply unveil(2) */
3759 if (err == NULL) {
3760 err = apply_unveil(repo_path, 0, NULL);
3761 if (err) {
3762 free(*tagmsg);
3763 *tagmsg = NULL;
3766 return err;
3769 static const struct got_error *
3770 add_tag(struct got_repository *repo, const char *tag_name,
3771 const char *commit_arg, const char *tagmsg_arg)
3773 const struct got_error *err = NULL;
3774 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3775 char *label = NULL, *commit_id_str = NULL;
3776 struct got_reference *ref = NULL;
3777 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3780 * Don't let the user create a tag named '-'.
3781 * While technically a valid reference name, this case is usually
3782 * an unintended typo.
3784 if (tag_name[0] == '-' && tag_name[1] == '\0')
3785 return got_error(GOT_ERR_BAD_REF_NAME);
3787 err = get_author(&tagger, repo);
3788 if (err)
3789 return err;
3791 err = match_object_id(&commit_id, &label, commit_arg,
3792 GOT_OBJ_TYPE_COMMIT, 1, repo);
3793 if (err)
3794 goto done;
3796 err = got_object_id_str(&commit_id_str, commit_id);
3797 if (err)
3798 goto done;
3800 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3801 refname = strdup(tag_name);
3802 if (refname == NULL) {
3803 err = got_error_from_errno("strdup");
3804 goto done;
3806 tag_name += 10;
3807 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3808 err = got_error_from_errno("asprintf");
3809 goto done;
3812 err = got_ref_open(&ref, repo, refname, 0);
3813 if (err == NULL) {
3814 err = got_error(GOT_ERR_TAG_EXISTS);
3815 goto done;
3816 } else if (err->code != GOT_ERR_NOT_REF)
3817 goto done;
3819 if (tagmsg_arg == NULL) {
3820 err = get_tag_message(&tagmsg, commit_id_str,
3821 tag_name, got_repo_get_path(repo));
3822 if (err)
3823 goto done;
3826 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3827 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3828 if (err)
3829 goto done;
3831 err = got_ref_alloc(&ref, refname, tag_id);
3832 if (err)
3833 goto done;
3835 err = got_ref_write(ref, repo);
3837 if (err == NULL) {
3838 char *tag_id_str;
3839 err = got_object_id_str(&tag_id_str, tag_id);
3840 printf("Created tag %s\n", tag_id_str);
3841 free(tag_id_str);
3843 done:
3844 if (ref)
3845 got_ref_close(ref);
3846 free(commit_id);
3847 free(commit_id_str);
3848 free(refname);
3849 free(tagmsg);
3850 free(tagger);
3851 return err;
3854 static const struct got_error *
3855 cmd_tag(int argc, char *argv[])
3857 const struct got_error *error = NULL;
3858 struct got_repository *repo = NULL;
3859 struct got_worktree *worktree = NULL;
3860 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3861 char *gitconfig_path = NULL;
3862 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3863 int ch, do_list = 0;
3865 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3866 switch (ch) {
3867 case 'm':
3868 tagmsg = optarg;
3869 break;
3870 case 'r':
3871 repo_path = realpath(optarg, NULL);
3872 if (repo_path == NULL)
3873 err(1, "-r option");
3874 got_path_strip_trailing_slashes(repo_path);
3875 break;
3876 case 'l':
3877 do_list = 1;
3878 break;
3879 default:
3880 usage_tag();
3881 /* NOTREACHED */
3885 argc -= optind;
3886 argv += optind;
3888 if (do_list) {
3889 if (tagmsg)
3890 errx(1, "-l and -m options are mutually exclusive\n");
3891 if (argc > 0)
3892 usage_tag();
3893 } else if (argc < 1 || argc > 2)
3894 usage_tag();
3895 else if (argc > 1)
3896 commit_id_arg = argv[1];
3897 tag_name = argv[0];
3899 #ifndef PROFILE
3900 if (do_list) {
3901 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3902 NULL) == -1)
3903 err(1, "pledge");
3904 } else {
3905 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3906 "sendfd unveil", NULL) == -1)
3907 err(1, "pledge");
3909 #endif
3910 cwd = getcwd(NULL, 0);
3911 if (cwd == NULL) {
3912 error = got_error_from_errno("getcwd");
3913 goto done;
3916 if (repo_path == NULL) {
3917 error = got_worktree_open(&worktree, cwd);
3918 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3919 goto done;
3920 else
3921 error = NULL;
3922 if (worktree) {
3923 repo_path =
3924 strdup(got_worktree_get_repo_path(worktree));
3925 if (repo_path == NULL)
3926 error = got_error_from_errno("strdup");
3927 if (error)
3928 goto done;
3929 } else {
3930 repo_path = strdup(cwd);
3931 if (repo_path == NULL) {
3932 error = got_error_from_errno("strdup");
3933 goto done;
3938 if (do_list) {
3939 error = got_repo_open(&repo, repo_path, NULL);
3940 if (error != NULL)
3941 goto done;
3942 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3943 if (error)
3944 goto done;
3945 error = list_tags(repo, worktree);
3946 } else {
3947 error = get_gitconfig_path(&gitconfig_path);
3948 if (error)
3949 goto done;
3950 error = got_repo_open(&repo, repo_path, gitconfig_path);
3951 if (error != NULL)
3952 goto done;
3954 if (tagmsg) {
3955 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3956 if (error)
3957 goto done;
3960 if (commit_id_arg == NULL) {
3961 struct got_reference *head_ref;
3962 struct got_object_id *commit_id;
3963 error = got_ref_open(&head_ref, repo,
3964 worktree ? got_worktree_get_head_ref_name(worktree)
3965 : GOT_REF_HEAD, 0);
3966 if (error)
3967 goto done;
3968 error = got_ref_resolve(&commit_id, repo, head_ref);
3969 got_ref_close(head_ref);
3970 if (error)
3971 goto done;
3972 error = got_object_id_str(&commit_id_str, commit_id);
3973 free(commit_id);
3974 if (error)
3975 goto done;
3978 error = add_tag(repo, tag_name,
3979 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3981 done:
3982 if (repo)
3983 got_repo_close(repo);
3984 if (worktree)
3985 got_worktree_close(worktree);
3986 free(cwd);
3987 free(repo_path);
3988 free(gitconfig_path);
3989 free(commit_id_str);
3990 return error;
3993 __dead static void
3994 usage_add(void)
3996 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3997 exit(1);
4000 static const struct got_error *
4001 cmd_add(int argc, char *argv[])
4003 const struct got_error *error = NULL;
4004 struct got_repository *repo = NULL;
4005 struct got_worktree *worktree = NULL;
4006 char *cwd = NULL;
4007 struct got_pathlist_head paths;
4008 struct got_pathlist_entry *pe;
4009 int ch;
4011 TAILQ_INIT(&paths);
4013 while ((ch = getopt(argc, argv, "")) != -1) {
4014 switch (ch) {
4015 default:
4016 usage_add();
4017 /* NOTREACHED */
4021 argc -= optind;
4022 argv += optind;
4024 #ifndef PROFILE
4025 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4026 NULL) == -1)
4027 err(1, "pledge");
4028 #endif
4029 if (argc < 1)
4030 usage_add();
4032 cwd = getcwd(NULL, 0);
4033 if (cwd == NULL) {
4034 error = got_error_from_errno("getcwd");
4035 goto done;
4038 error = got_worktree_open(&worktree, cwd);
4039 if (error)
4040 goto done;
4042 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4043 NULL);
4044 if (error != NULL)
4045 goto done;
4047 error = apply_unveil(got_repo_get_path(repo), 1,
4048 got_worktree_get_root_path(worktree));
4049 if (error)
4050 goto done;
4052 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4053 if (error)
4054 goto done;
4056 error = got_worktree_schedule_add(worktree, &paths, print_status,
4057 NULL, repo);
4058 done:
4059 if (repo)
4060 got_repo_close(repo);
4061 if (worktree)
4062 got_worktree_close(worktree);
4063 TAILQ_FOREACH(pe, &paths, entry)
4064 free((char *)pe->path);
4065 got_pathlist_free(&paths);
4066 free(cwd);
4067 return error;
4070 __dead static void
4071 usage_remove(void)
4073 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4074 exit(1);
4077 static const struct got_error *
4078 print_remove_status(void *arg, unsigned char status,
4079 unsigned char staged_status, const char *path,
4080 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4081 struct got_object_id *commit_id)
4083 if (status == GOT_STATUS_NONEXISTENT)
4084 return NULL;
4085 if (status == staged_status && (status == GOT_STATUS_DELETE))
4086 status = GOT_STATUS_NO_CHANGE;
4087 printf("%c%c %s\n", status, staged_status, path);
4088 return NULL;
4091 static const struct got_error *
4092 cmd_remove(int argc, char *argv[])
4094 const struct got_error *error = NULL;
4095 struct got_worktree *worktree = NULL;
4096 struct got_repository *repo = NULL;
4097 char *cwd = NULL;
4098 struct got_pathlist_head paths;
4099 struct got_pathlist_entry *pe;
4100 int ch, delete_local_mods = 0;
4102 TAILQ_INIT(&paths);
4104 while ((ch = getopt(argc, argv, "f")) != -1) {
4105 switch (ch) {
4106 case 'f':
4107 delete_local_mods = 1;
4108 break;
4109 default:
4110 usage_add();
4111 /* NOTREACHED */
4115 argc -= optind;
4116 argv += optind;
4118 #ifndef PROFILE
4119 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4120 NULL) == -1)
4121 err(1, "pledge");
4122 #endif
4123 if (argc < 1)
4124 usage_remove();
4126 cwd = getcwd(NULL, 0);
4127 if (cwd == NULL) {
4128 error = got_error_from_errno("getcwd");
4129 goto done;
4131 error = got_worktree_open(&worktree, cwd);
4132 if (error)
4133 goto done;
4135 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4136 NULL);
4137 if (error)
4138 goto done;
4140 error = apply_unveil(got_repo_get_path(repo), 1,
4141 got_worktree_get_root_path(worktree));
4142 if (error)
4143 goto done;
4145 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4146 if (error)
4147 goto done;
4149 error = got_worktree_schedule_delete(worktree, &paths,
4150 delete_local_mods, print_remove_status, NULL, repo);
4151 if (error)
4152 goto done;
4153 done:
4154 if (repo)
4155 got_repo_close(repo);
4156 if (worktree)
4157 got_worktree_close(worktree);
4158 TAILQ_FOREACH(pe, &paths, entry)
4159 free((char *)pe->path);
4160 got_pathlist_free(&paths);
4161 free(cwd);
4162 return error;
4165 __dead static void
4166 usage_revert(void)
4168 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4169 "path ...\n", getprogname());
4170 exit(1);
4173 static const struct got_error *
4174 revert_progress(void *arg, unsigned char status, const char *path)
4176 while (path[0] == '/')
4177 path++;
4178 printf("%c %s\n", status, path);
4179 return NULL;
4182 struct choose_patch_arg {
4183 FILE *patch_script_file;
4184 const char *action;
4187 static const struct got_error *
4188 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4189 int nchanges, const char *action)
4191 char *line = NULL;
4192 size_t linesize = 0;
4193 ssize_t linelen;
4195 switch (status) {
4196 case GOT_STATUS_ADD:
4197 printf("A %s\n%s this addition? [y/n] ", path, action);
4198 break;
4199 case GOT_STATUS_DELETE:
4200 printf("D %s\n%s this deletion? [y/n] ", path, action);
4201 break;
4202 case GOT_STATUS_MODIFY:
4203 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4204 return got_error_from_errno("fseek");
4205 printf(GOT_COMMIT_SEP_STR);
4206 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4207 printf("%s", line);
4208 if (ferror(patch_file))
4209 return got_error_from_errno("getline");
4210 printf(GOT_COMMIT_SEP_STR);
4211 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4212 path, n, nchanges, action);
4213 break;
4214 default:
4215 return got_error_path(path, GOT_ERR_FILE_STATUS);
4218 return NULL;
4221 static const struct got_error *
4222 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4223 FILE *patch_file, int n, int nchanges)
4225 const struct got_error *err = NULL;
4226 char *line = NULL;
4227 size_t linesize = 0;
4228 ssize_t linelen;
4229 int resp = ' ';
4230 struct choose_patch_arg *a = arg;
4232 *choice = GOT_PATCH_CHOICE_NONE;
4234 if (a->patch_script_file) {
4235 char *nl;
4236 err = show_change(status, path, patch_file, n, nchanges,
4237 a->action);
4238 if (err)
4239 return err;
4240 linelen = getline(&line, &linesize, a->patch_script_file);
4241 if (linelen == -1) {
4242 if (ferror(a->patch_script_file))
4243 return got_error_from_errno("getline");
4244 return NULL;
4246 nl = strchr(line, '\n');
4247 if (nl)
4248 *nl = '\0';
4249 if (strcmp(line, "y") == 0) {
4250 *choice = GOT_PATCH_CHOICE_YES;
4251 printf("y\n");
4252 } else if (strcmp(line, "n") == 0) {
4253 *choice = GOT_PATCH_CHOICE_NO;
4254 printf("n\n");
4255 } else if (strcmp(line, "q") == 0 &&
4256 status == GOT_STATUS_MODIFY) {
4257 *choice = GOT_PATCH_CHOICE_QUIT;
4258 printf("q\n");
4259 } else
4260 printf("invalid response '%s'\n", line);
4261 free(line);
4262 return NULL;
4265 while (resp != 'y' && resp != 'n' && resp != 'q') {
4266 err = show_change(status, path, patch_file, n, nchanges,
4267 a->action);
4268 if (err)
4269 return err;
4270 resp = getchar();
4271 if (resp == '\n')
4272 resp = getchar();
4273 if (status == GOT_STATUS_MODIFY) {
4274 if (resp != 'y' && resp != 'n' && resp != 'q') {
4275 printf("invalid response '%c'\n", resp);
4276 resp = ' ';
4278 } else if (resp != 'y' && resp != 'n') {
4279 printf("invalid response '%c'\n", resp);
4280 resp = ' ';
4284 if (resp == 'y')
4285 *choice = GOT_PATCH_CHOICE_YES;
4286 else if (resp == 'n')
4287 *choice = GOT_PATCH_CHOICE_NO;
4288 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4289 *choice = GOT_PATCH_CHOICE_QUIT;
4291 return NULL;
4295 static const struct got_error *
4296 cmd_revert(int argc, char *argv[])
4298 const struct got_error *error = NULL;
4299 struct got_worktree *worktree = NULL;
4300 struct got_repository *repo = NULL;
4301 char *cwd = NULL, *path = NULL;
4302 struct got_pathlist_head paths;
4303 struct got_pathlist_entry *pe;
4304 int ch, can_recurse = 0, pflag = 0;
4305 FILE *patch_script_file = NULL;
4306 const char *patch_script_path = NULL;
4307 struct choose_patch_arg cpa;
4309 TAILQ_INIT(&paths);
4311 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4312 switch (ch) {
4313 case 'p':
4314 pflag = 1;
4315 break;
4316 case 'F':
4317 patch_script_path = optarg;
4318 break;
4319 case 'R':
4320 can_recurse = 1;
4321 break;
4322 default:
4323 usage_revert();
4324 /* NOTREACHED */
4328 argc -= optind;
4329 argv += optind;
4331 #ifndef PROFILE
4332 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4333 "unveil", NULL) == -1)
4334 err(1, "pledge");
4335 #endif
4336 if (argc < 1)
4337 usage_revert();
4338 if (patch_script_path && !pflag)
4339 errx(1, "-F option can only be used together with -p option");
4341 cwd = getcwd(NULL, 0);
4342 if (cwd == NULL) {
4343 error = got_error_from_errno("getcwd");
4344 goto done;
4346 error = got_worktree_open(&worktree, cwd);
4347 if (error)
4348 goto done;
4350 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4351 NULL);
4352 if (error != NULL)
4353 goto done;
4355 if (patch_script_path) {
4356 patch_script_file = fopen(patch_script_path, "r");
4357 if (patch_script_file == NULL) {
4358 error = got_error_from_errno2("fopen",
4359 patch_script_path);
4360 goto done;
4363 error = apply_unveil(got_repo_get_path(repo), 1,
4364 got_worktree_get_root_path(worktree));
4365 if (error)
4366 goto done;
4368 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4369 if (error)
4370 goto done;
4372 if (!can_recurse) {
4373 char *ondisk_path;
4374 struct stat sb;
4375 TAILQ_FOREACH(pe, &paths, entry) {
4376 if (asprintf(&ondisk_path, "%s/%s",
4377 got_worktree_get_root_path(worktree),
4378 pe->path) == -1) {
4379 error = got_error_from_errno("asprintf");
4380 goto done;
4382 if (lstat(ondisk_path, &sb) == -1) {
4383 if (errno == ENOENT) {
4384 free(ondisk_path);
4385 continue;
4387 error = got_error_from_errno2("lstat",
4388 ondisk_path);
4389 free(ondisk_path);
4390 goto done;
4392 free(ondisk_path);
4393 if (S_ISDIR(sb.st_mode)) {
4394 error = got_error_msg(GOT_ERR_BAD_PATH,
4395 "reverting directories requires -R option");
4396 goto done;
4401 cpa.patch_script_file = patch_script_file;
4402 cpa.action = "revert";
4403 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4404 pflag ? choose_patch : NULL, &cpa, repo);
4405 if (error)
4406 goto done;
4407 done:
4408 if (patch_script_file && fclose(patch_script_file) == EOF &&
4409 error == NULL)
4410 error = got_error_from_errno2("fclose", patch_script_path);
4411 if (repo)
4412 got_repo_close(repo);
4413 if (worktree)
4414 got_worktree_close(worktree);
4415 free(path);
4416 free(cwd);
4417 return error;
4420 __dead static void
4421 usage_commit(void)
4423 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4424 getprogname());
4425 exit(1);
4428 struct collect_commit_logmsg_arg {
4429 const char *cmdline_log;
4430 const char *editor;
4431 const char *worktree_path;
4432 const char *branch_name;
4433 const char *repo_path;
4434 char *logmsg_path;
4438 static const struct got_error *
4439 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4440 void *arg)
4442 char *initial_content = NULL;
4443 struct got_pathlist_entry *pe;
4444 const struct got_error *err = NULL;
4445 char *template = NULL;
4446 struct collect_commit_logmsg_arg *a = arg;
4447 int fd;
4448 size_t len;
4450 /* if a message was specified on the command line, just use it */
4451 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4452 len = strlen(a->cmdline_log) + 1;
4453 *logmsg = malloc(len + 1);
4454 if (*logmsg == NULL)
4455 return got_error_from_errno("malloc");
4456 strlcpy(*logmsg, a->cmdline_log, len);
4457 return NULL;
4460 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4461 return got_error_from_errno("asprintf");
4463 if (asprintf(&initial_content,
4464 "\n# changes to be committed on branch %s:\n",
4465 a->branch_name) == -1)
4466 return got_error_from_errno("asprintf");
4468 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4469 if (err)
4470 goto done;
4472 dprintf(fd, initial_content);
4474 TAILQ_FOREACH(pe, commitable_paths, entry) {
4475 struct got_commitable *ct = pe->data;
4476 dprintf(fd, "# %c %s\n",
4477 got_commitable_get_status(ct),
4478 got_commitable_get_path(ct));
4480 close(fd);
4482 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4483 done:
4484 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4485 unlink(a->logmsg_path);
4486 free(a->logmsg_path);
4487 a->logmsg_path = NULL;
4489 free(initial_content);
4490 free(template);
4492 /* Editor is done; we can now apply unveil(2) */
4493 if (err == NULL) {
4494 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4495 if (err) {
4496 free(*logmsg);
4497 *logmsg = NULL;
4500 return err;
4503 static const struct got_error *
4504 cmd_commit(int argc, char *argv[])
4506 const struct got_error *error = NULL;
4507 struct got_worktree *worktree = NULL;
4508 struct got_repository *repo = NULL;
4509 char *cwd = NULL, *id_str = NULL;
4510 struct got_object_id *id = NULL;
4511 const char *logmsg = NULL;
4512 struct collect_commit_logmsg_arg cl_arg;
4513 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4514 int ch, rebase_in_progress, histedit_in_progress;
4515 struct got_pathlist_head paths;
4517 TAILQ_INIT(&paths);
4518 cl_arg.logmsg_path = NULL;
4520 while ((ch = getopt(argc, argv, "m:")) != -1) {
4521 switch (ch) {
4522 case 'm':
4523 logmsg = optarg;
4524 break;
4525 default:
4526 usage_commit();
4527 /* NOTREACHED */
4531 argc -= optind;
4532 argv += optind;
4534 #ifndef PROFILE
4535 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4536 "unveil", NULL) == -1)
4537 err(1, "pledge");
4538 #endif
4539 cwd = getcwd(NULL, 0);
4540 if (cwd == NULL) {
4541 error = got_error_from_errno("getcwd");
4542 goto done;
4544 error = got_worktree_open(&worktree, cwd);
4545 if (error)
4546 goto done;
4548 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4549 if (error)
4550 goto done;
4551 if (rebase_in_progress) {
4552 error = got_error(GOT_ERR_REBASING);
4553 goto done;
4556 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4557 worktree);
4558 if (error)
4559 goto done;
4561 error = get_gitconfig_path(&gitconfig_path);
4562 if (error)
4563 goto done;
4564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4565 gitconfig_path);
4566 if (error != NULL)
4567 goto done;
4569 error = get_author(&author, repo);
4570 if (error)
4571 return error;
4574 * unveil(2) traverses exec(2); if an editor is used we have
4575 * to apply unveil after the log message has been written.
4577 if (logmsg == NULL || strlen(logmsg) == 0)
4578 error = get_editor(&editor);
4579 else
4580 error = apply_unveil(got_repo_get_path(repo), 0,
4581 got_worktree_get_root_path(worktree));
4582 if (error)
4583 goto done;
4585 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4586 if (error)
4587 goto done;
4589 cl_arg.editor = editor;
4590 cl_arg.cmdline_log = logmsg;
4591 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4592 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4593 if (!histedit_in_progress) {
4594 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4595 error = got_error(GOT_ERR_COMMIT_BRANCH);
4596 goto done;
4598 cl_arg.branch_name += 11;
4600 cl_arg.repo_path = got_repo_get_path(repo);
4601 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4602 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4603 if (error) {
4604 if (cl_arg.logmsg_path)
4605 fprintf(stderr, "%s: log message preserved in %s\n",
4606 getprogname(), cl_arg.logmsg_path);
4607 goto done;
4610 if (cl_arg.logmsg_path)
4611 unlink(cl_arg.logmsg_path);
4613 error = got_object_id_str(&id_str, id);
4614 if (error)
4615 goto done;
4616 printf("Created commit %s\n", id_str);
4617 done:
4618 free(cl_arg.logmsg_path);
4619 if (repo)
4620 got_repo_close(repo);
4621 if (worktree)
4622 got_worktree_close(worktree);
4623 free(cwd);
4624 free(id_str);
4625 free(gitconfig_path);
4626 free(editor);
4627 free(author);
4628 return error;
4631 __dead static void
4632 usage_cherrypick(void)
4634 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4635 exit(1);
4638 static const struct got_error *
4639 cmd_cherrypick(int argc, char *argv[])
4641 const struct got_error *error = NULL;
4642 struct got_worktree *worktree = NULL;
4643 struct got_repository *repo = NULL;
4644 char *cwd = NULL, *commit_id_str = NULL;
4645 struct got_object_id *commit_id = NULL;
4646 struct got_commit_object *commit = NULL;
4647 struct got_object_qid *pid;
4648 struct got_reference *head_ref = NULL;
4649 int ch, did_something = 0;
4651 while ((ch = getopt(argc, argv, "")) != -1) {
4652 switch (ch) {
4653 default:
4654 usage_cherrypick();
4655 /* NOTREACHED */
4659 argc -= optind;
4660 argv += optind;
4662 #ifndef PROFILE
4663 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4664 "unveil", NULL) == -1)
4665 err(1, "pledge");
4666 #endif
4667 if (argc != 1)
4668 usage_cherrypick();
4670 cwd = getcwd(NULL, 0);
4671 if (cwd == NULL) {
4672 error = got_error_from_errno("getcwd");
4673 goto done;
4675 error = got_worktree_open(&worktree, cwd);
4676 if (error)
4677 goto done;
4679 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4680 NULL);
4681 if (error != NULL)
4682 goto done;
4684 error = apply_unveil(got_repo_get_path(repo), 0,
4685 got_worktree_get_root_path(worktree));
4686 if (error)
4687 goto done;
4689 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4690 GOT_OBJ_TYPE_COMMIT, repo);
4691 if (error != NULL) {
4692 struct got_reference *ref;
4693 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4694 goto done;
4695 error = got_ref_open(&ref, repo, argv[0], 0);
4696 if (error != NULL)
4697 goto done;
4698 error = got_ref_resolve(&commit_id, repo, ref);
4699 got_ref_close(ref);
4700 if (error != NULL)
4701 goto done;
4703 error = got_object_id_str(&commit_id_str, commit_id);
4704 if (error)
4705 goto done;
4707 error = got_ref_open(&head_ref, repo,
4708 got_worktree_get_head_ref_name(worktree), 0);
4709 if (error != NULL)
4710 goto done;
4712 error = check_same_branch(commit_id, head_ref, NULL, repo);
4713 if (error) {
4714 if (error->code != GOT_ERR_ANCESTRY)
4715 goto done;
4716 error = NULL;
4717 } else {
4718 error = got_error(GOT_ERR_SAME_BRANCH);
4719 goto done;
4722 error = got_object_open_as_commit(&commit, repo, commit_id);
4723 if (error)
4724 goto done;
4725 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4726 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4727 commit_id, repo, update_progress, &did_something, check_cancelled,
4728 NULL);
4729 if (error != NULL)
4730 goto done;
4732 if (did_something)
4733 printf("Merged commit %s\n", commit_id_str);
4734 done:
4735 if (commit)
4736 got_object_commit_close(commit);
4737 free(commit_id_str);
4738 if (head_ref)
4739 got_ref_close(head_ref);
4740 if (worktree)
4741 got_worktree_close(worktree);
4742 if (repo)
4743 got_repo_close(repo);
4744 return error;
4747 __dead static void
4748 usage_backout(void)
4750 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4751 exit(1);
4754 static const struct got_error *
4755 cmd_backout(int argc, char *argv[])
4757 const struct got_error *error = NULL;
4758 struct got_worktree *worktree = NULL;
4759 struct got_repository *repo = NULL;
4760 char *cwd = NULL, *commit_id_str = NULL;
4761 struct got_object_id *commit_id = NULL;
4762 struct got_commit_object *commit = NULL;
4763 struct got_object_qid *pid;
4764 struct got_reference *head_ref = NULL;
4765 int ch, did_something = 0;
4767 while ((ch = getopt(argc, argv, "")) != -1) {
4768 switch (ch) {
4769 default:
4770 usage_backout();
4771 /* NOTREACHED */
4775 argc -= optind;
4776 argv += optind;
4778 #ifndef PROFILE
4779 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4780 "unveil", NULL) == -1)
4781 err(1, "pledge");
4782 #endif
4783 if (argc != 1)
4784 usage_backout();
4786 cwd = getcwd(NULL, 0);
4787 if (cwd == NULL) {
4788 error = got_error_from_errno("getcwd");
4789 goto done;
4791 error = got_worktree_open(&worktree, cwd);
4792 if (error)
4793 goto done;
4795 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4796 NULL);
4797 if (error != NULL)
4798 goto done;
4800 error = apply_unveil(got_repo_get_path(repo), 0,
4801 got_worktree_get_root_path(worktree));
4802 if (error)
4803 goto done;
4805 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4806 GOT_OBJ_TYPE_COMMIT, repo);
4807 if (error != NULL) {
4808 struct got_reference *ref;
4809 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4810 goto done;
4811 error = got_ref_open(&ref, repo, argv[0], 0);
4812 if (error != NULL)
4813 goto done;
4814 error = got_ref_resolve(&commit_id, repo, ref);
4815 got_ref_close(ref);
4816 if (error != NULL)
4817 goto done;
4819 error = got_object_id_str(&commit_id_str, commit_id);
4820 if (error)
4821 goto done;
4823 error = got_ref_open(&head_ref, repo,
4824 got_worktree_get_head_ref_name(worktree), 0);
4825 if (error != NULL)
4826 goto done;
4828 error = check_same_branch(commit_id, head_ref, NULL, repo);
4829 if (error)
4830 goto done;
4832 error = got_object_open_as_commit(&commit, repo, commit_id);
4833 if (error)
4834 goto done;
4835 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4836 if (pid == NULL) {
4837 error = got_error(GOT_ERR_ROOT_COMMIT);
4838 goto done;
4841 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4842 update_progress, &did_something, check_cancelled, NULL);
4843 if (error != NULL)
4844 goto done;
4846 if (did_something)
4847 printf("Backed out commit %s\n", commit_id_str);
4848 done:
4849 if (commit)
4850 got_object_commit_close(commit);
4851 free(commit_id_str);
4852 if (head_ref)
4853 got_ref_close(head_ref);
4854 if (worktree)
4855 got_worktree_close(worktree);
4856 if (repo)
4857 got_repo_close(repo);
4858 return error;
4861 __dead static void
4862 usage_rebase(void)
4864 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4865 getprogname());
4866 exit(1);
4869 void
4870 trim_logmsg(char *logmsg, int limit)
4872 char *nl;
4873 size_t len;
4875 len = strlen(logmsg);
4876 if (len > limit)
4877 len = limit;
4878 logmsg[len] = '\0';
4879 nl = strchr(logmsg, '\n');
4880 if (nl)
4881 *nl = '\0';
4884 static const struct got_error *
4885 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4887 const struct got_error *err;
4888 char *logmsg0 = NULL;
4889 const char *s;
4891 err = got_object_commit_get_logmsg(&logmsg0, commit);
4892 if (err)
4893 return err;
4895 s = logmsg0;
4896 while (isspace((unsigned char)s[0]))
4897 s++;
4899 *logmsg = strdup(s);
4900 if (*logmsg == NULL) {
4901 err = got_error_from_errno("strdup");
4902 goto done;
4905 trim_logmsg(*logmsg, limit);
4906 done:
4907 free(logmsg0);
4908 return err;
4911 static const struct got_error *
4912 show_rebase_progress(struct got_commit_object *commit,
4913 struct got_object_id *old_id, struct got_object_id *new_id)
4915 const struct got_error *err;
4916 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4918 err = got_object_id_str(&old_id_str, old_id);
4919 if (err)
4920 goto done;
4922 if (new_id) {
4923 err = got_object_id_str(&new_id_str, new_id);
4924 if (err)
4925 goto done;
4928 old_id_str[12] = '\0';
4929 if (new_id_str)
4930 new_id_str[12] = '\0';
4932 err = get_short_logmsg(&logmsg, 42, commit);
4933 if (err)
4934 goto done;
4936 printf("%s -> %s: %s\n", old_id_str,
4937 new_id_str ? new_id_str : "no-op change", logmsg);
4938 done:
4939 free(old_id_str);
4940 free(new_id_str);
4941 return err;
4944 static const struct got_error *
4945 rebase_progress(void *arg, unsigned char status, const char *path)
4947 unsigned char *rebase_status = arg;
4949 while (path[0] == '/')
4950 path++;
4951 printf("%c %s\n", status, path);
4953 if (*rebase_status == GOT_STATUS_CONFLICT)
4954 return NULL;
4955 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4956 *rebase_status = status;
4957 return NULL;
4960 static const struct got_error *
4961 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4962 struct got_reference *branch, struct got_reference *new_base_branch,
4963 struct got_reference *tmp_branch, struct got_repository *repo)
4965 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4966 return got_worktree_rebase_complete(worktree, fileindex,
4967 new_base_branch, tmp_branch, branch, repo);
4970 static const struct got_error *
4971 rebase_commit(struct got_pathlist_head *merged_paths,
4972 struct got_worktree *worktree, struct got_fileindex *fileindex,
4973 struct got_reference *tmp_branch,
4974 struct got_object_id *commit_id, struct got_repository *repo)
4976 const struct got_error *error;
4977 struct got_commit_object *commit;
4978 struct got_object_id *new_commit_id;
4980 error = got_object_open_as_commit(&commit, repo, commit_id);
4981 if (error)
4982 return error;
4984 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4985 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4986 if (error) {
4987 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4988 goto done;
4989 error = show_rebase_progress(commit, commit_id, NULL);
4990 } else {
4991 error = show_rebase_progress(commit, commit_id, new_commit_id);
4992 free(new_commit_id);
4994 done:
4995 got_object_commit_close(commit);
4996 return error;
4999 struct check_path_prefix_arg {
5000 const char *path_prefix;
5001 size_t len;
5002 int errcode;
5005 static const struct got_error *
5006 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5007 struct got_blob_object *blob2, struct got_object_id *id1,
5008 struct got_object_id *id2, const char *path1, const char *path2,
5009 struct got_repository *repo)
5011 struct check_path_prefix_arg *a = arg;
5013 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5014 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5015 return got_error(a->errcode);
5017 return NULL;
5020 static const struct got_error *
5021 check_path_prefix(struct got_object_id *parent_id,
5022 struct got_object_id *commit_id, const char *path_prefix,
5023 int errcode, struct got_repository *repo)
5025 const struct got_error *err;
5026 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5027 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5028 struct check_path_prefix_arg cpp_arg;
5030 if (got_path_is_root_dir(path_prefix))
5031 return NULL;
5033 err = got_object_open_as_commit(&commit, repo, commit_id);
5034 if (err)
5035 goto done;
5037 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5038 if (err)
5039 goto done;
5041 err = got_object_open_as_tree(&tree1, repo,
5042 got_object_commit_get_tree_id(parent_commit));
5043 if (err)
5044 goto done;
5046 err = got_object_open_as_tree(&tree2, repo,
5047 got_object_commit_get_tree_id(commit));
5048 if (err)
5049 goto done;
5051 cpp_arg.path_prefix = path_prefix;
5052 while (cpp_arg.path_prefix[0] == '/')
5053 cpp_arg.path_prefix++;
5054 cpp_arg.len = strlen(cpp_arg.path_prefix);
5055 cpp_arg.errcode = errcode;
5056 err = got_diff_tree(tree1, tree2, "", "", repo,
5057 check_path_prefix_in_diff, &cpp_arg, 0);
5058 done:
5059 if (tree1)
5060 got_object_tree_close(tree1);
5061 if (tree2)
5062 got_object_tree_close(tree2);
5063 if (commit)
5064 got_object_commit_close(commit);
5065 if (parent_commit)
5066 got_object_commit_close(parent_commit);
5067 return err;
5070 static const struct got_error *
5071 collect_commits(struct got_object_id_queue *commits,
5072 struct got_object_id *initial_commit_id,
5073 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5074 const char *path_prefix, int path_prefix_errcode,
5075 struct got_repository *repo)
5077 const struct got_error *err = NULL;
5078 struct got_commit_graph *graph = NULL;
5079 struct got_object_id *parent_id = NULL;
5080 struct got_object_qid *qid;
5081 struct got_object_id *commit_id = initial_commit_id;
5083 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5084 if (err)
5085 return err;
5087 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5088 check_cancelled, NULL);
5089 if (err)
5090 goto done;
5091 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5092 err = got_commit_graph_iter_next(&parent_id, graph);
5093 if (err) {
5094 if (err->code == GOT_ERR_ITER_COMPLETED) {
5095 err = got_error_msg(GOT_ERR_ANCESTRY,
5096 "ran out of commits to rebase before "
5097 "youngest common ancestor commit has "
5098 "been reached?!?");
5099 goto done;
5100 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5101 goto done;
5102 err = got_commit_graph_fetch_commits(graph, 1, repo,
5103 check_cancelled, NULL);
5104 if (err)
5105 goto done;
5106 } else {
5107 err = check_path_prefix(parent_id, commit_id,
5108 path_prefix, path_prefix_errcode, repo);
5109 if (err)
5110 goto done;
5112 err = got_object_qid_alloc(&qid, commit_id);
5113 if (err)
5114 goto done;
5115 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5116 commit_id = parent_id;
5119 done:
5120 got_commit_graph_close(graph);
5121 return err;
5124 static const struct got_error *
5125 cmd_rebase(int argc, char *argv[])
5127 const struct got_error *error = NULL;
5128 struct got_worktree *worktree = NULL;
5129 struct got_repository *repo = NULL;
5130 struct got_fileindex *fileindex = NULL;
5131 char *cwd = NULL;
5132 struct got_reference *branch = NULL;
5133 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5134 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5135 struct got_object_id *resume_commit_id = NULL;
5136 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5137 struct got_commit_object *commit = NULL;
5138 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5139 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5140 struct got_object_id_queue commits;
5141 struct got_pathlist_head merged_paths;
5142 const struct got_object_id_queue *parent_ids;
5143 struct got_object_qid *qid, *pid;
5145 SIMPLEQ_INIT(&commits);
5146 TAILQ_INIT(&merged_paths);
5148 while ((ch = getopt(argc, argv, "ac")) != -1) {
5149 switch (ch) {
5150 case 'a':
5151 abort_rebase = 1;
5152 break;
5153 case 'c':
5154 continue_rebase = 1;
5155 break;
5156 default:
5157 usage_rebase();
5158 /* NOTREACHED */
5162 argc -= optind;
5163 argv += optind;
5165 #ifndef PROFILE
5166 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5167 "unveil", NULL) == -1)
5168 err(1, "pledge");
5169 #endif
5170 if (abort_rebase && continue_rebase)
5171 usage_rebase();
5172 else if (abort_rebase || continue_rebase) {
5173 if (argc != 0)
5174 usage_rebase();
5175 } else if (argc != 1)
5176 usage_rebase();
5178 cwd = getcwd(NULL, 0);
5179 if (cwd == NULL) {
5180 error = got_error_from_errno("getcwd");
5181 goto done;
5183 error = got_worktree_open(&worktree, cwd);
5184 if (error)
5185 goto done;
5187 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5188 NULL);
5189 if (error != NULL)
5190 goto done;
5192 error = apply_unveil(got_repo_get_path(repo), 0,
5193 got_worktree_get_root_path(worktree));
5194 if (error)
5195 goto done;
5197 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5198 if (error)
5199 goto done;
5201 if (abort_rebase) {
5202 int did_something;
5203 if (!rebase_in_progress) {
5204 error = got_error(GOT_ERR_NOT_REBASING);
5205 goto done;
5207 error = got_worktree_rebase_continue(&resume_commit_id,
5208 &new_base_branch, &tmp_branch, &branch, &fileindex,
5209 worktree, repo);
5210 if (error)
5211 goto done;
5212 printf("Switching work tree to %s\n",
5213 got_ref_get_symref_target(new_base_branch));
5214 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5215 new_base_branch, update_progress, &did_something);
5216 if (error)
5217 goto done;
5218 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5219 goto done; /* nothing else to do */
5222 if (continue_rebase) {
5223 if (!rebase_in_progress) {
5224 error = got_error(GOT_ERR_NOT_REBASING);
5225 goto done;
5227 error = got_worktree_rebase_continue(&resume_commit_id,
5228 &new_base_branch, &tmp_branch, &branch, &fileindex,
5229 worktree, repo);
5230 if (error)
5231 goto done;
5233 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5234 resume_commit_id, repo);
5235 if (error)
5236 goto done;
5238 yca_id = got_object_id_dup(resume_commit_id);
5239 if (yca_id == NULL) {
5240 error = got_error_from_errno("got_object_id_dup");
5241 goto done;
5243 } else {
5244 error = got_ref_open(&branch, repo, argv[0], 0);
5245 if (error != NULL)
5246 goto done;
5249 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5250 if (error)
5251 goto done;
5253 if (!continue_rebase) {
5254 struct got_object_id *base_commit_id;
5256 base_commit_id = got_worktree_get_base_commit_id(worktree);
5257 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5258 base_commit_id, branch_head_commit_id, repo,
5259 check_cancelled, NULL);
5260 if (error)
5261 goto done;
5262 if (yca_id == NULL) {
5263 error = got_error_msg(GOT_ERR_ANCESTRY,
5264 "specified branch shares no common ancestry "
5265 "with work tree's branch");
5266 goto done;
5269 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5270 if (error) {
5271 if (error->code != GOT_ERR_ANCESTRY)
5272 goto done;
5273 error = NULL;
5274 } else {
5275 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5276 "specified branch resolves to a commit which "
5277 "is already contained in work tree's branch");
5278 goto done;
5280 error = got_worktree_rebase_prepare(&new_base_branch,
5281 &tmp_branch, &fileindex, worktree, branch, repo);
5282 if (error)
5283 goto done;
5286 commit_id = branch_head_commit_id;
5287 error = got_object_open_as_commit(&commit, repo, commit_id);
5288 if (error)
5289 goto done;
5291 parent_ids = got_object_commit_get_parent_ids(commit);
5292 pid = SIMPLEQ_FIRST(parent_ids);
5293 if (pid == NULL) {
5294 if (!continue_rebase) {
5295 int did_something;
5296 error = got_worktree_rebase_abort(worktree, fileindex,
5297 repo, new_base_branch, update_progress,
5298 &did_something);
5299 if (error)
5300 goto done;
5301 printf("Rebase of %s aborted\n",
5302 got_ref_get_name(branch));
5304 error = got_error(GOT_ERR_EMPTY_REBASE);
5305 goto done;
5307 error = collect_commits(&commits, commit_id, pid->id,
5308 yca_id, got_worktree_get_path_prefix(worktree),
5309 GOT_ERR_REBASE_PATH, repo);
5310 got_object_commit_close(commit);
5311 commit = NULL;
5312 if (error)
5313 goto done;
5315 if (SIMPLEQ_EMPTY(&commits)) {
5316 if (continue_rebase)
5317 error = rebase_complete(worktree, fileindex,
5318 branch, new_base_branch, tmp_branch, repo);
5319 else
5320 error = got_error(GOT_ERR_EMPTY_REBASE);
5321 goto done;
5324 pid = NULL;
5325 SIMPLEQ_FOREACH(qid, &commits, entry) {
5326 commit_id = qid->id;
5327 parent_id = pid ? pid->id : yca_id;
5328 pid = qid;
5330 error = got_worktree_rebase_merge_files(&merged_paths,
5331 worktree, fileindex, parent_id, commit_id, repo,
5332 rebase_progress, &rebase_status, check_cancelled, NULL);
5333 if (error)
5334 goto done;
5336 if (rebase_status == GOT_STATUS_CONFLICT) {
5337 got_worktree_rebase_pathlist_free(&merged_paths);
5338 break;
5341 error = rebase_commit(&merged_paths, worktree, fileindex,
5342 tmp_branch, commit_id, repo);
5343 got_worktree_rebase_pathlist_free(&merged_paths);
5344 if (error)
5345 goto done;
5348 if (rebase_status == GOT_STATUS_CONFLICT) {
5349 error = got_worktree_rebase_postpone(worktree, fileindex);
5350 if (error)
5351 goto done;
5352 error = got_error_msg(GOT_ERR_CONFLICTS,
5353 "conflicts must be resolved before rebasing can continue");
5354 } else
5355 error = rebase_complete(worktree, fileindex, branch,
5356 new_base_branch, tmp_branch, repo);
5357 done:
5358 got_object_id_queue_free(&commits);
5359 free(branch_head_commit_id);
5360 free(resume_commit_id);
5361 free(yca_id);
5362 if (commit)
5363 got_object_commit_close(commit);
5364 if (branch)
5365 got_ref_close(branch);
5366 if (new_base_branch)
5367 got_ref_close(new_base_branch);
5368 if (tmp_branch)
5369 got_ref_close(tmp_branch);
5370 if (worktree)
5371 got_worktree_close(worktree);
5372 if (repo)
5373 got_repo_close(repo);
5374 return error;
5377 __dead static void
5378 usage_histedit(void)
5380 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5381 getprogname());
5382 exit(1);
5385 #define GOT_HISTEDIT_PICK 'p'
5386 #define GOT_HISTEDIT_EDIT 'e'
5387 #define GOT_HISTEDIT_FOLD 'f'
5388 #define GOT_HISTEDIT_DROP 'd'
5389 #define GOT_HISTEDIT_MESG 'm'
5391 static struct got_histedit_cmd {
5392 unsigned char code;
5393 const char *name;
5394 const char *desc;
5395 } got_histedit_cmds[] = {
5396 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5397 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5398 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5399 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5400 { GOT_HISTEDIT_MESG, "mesg",
5401 "single-line log message for commit above (open editor if empty)" },
5404 struct got_histedit_list_entry {
5405 TAILQ_ENTRY(got_histedit_list_entry) entry;
5406 struct got_object_id *commit_id;
5407 const struct got_histedit_cmd *cmd;
5408 char *logmsg;
5410 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5412 static const struct got_error *
5413 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5414 FILE *f, struct got_repository *repo)
5416 const struct got_error *err = NULL;
5417 char *logmsg = NULL, *id_str = NULL;
5418 struct got_commit_object *commit = NULL;
5419 int n;
5421 err = got_object_open_as_commit(&commit, repo, commit_id);
5422 if (err)
5423 goto done;
5425 err = get_short_logmsg(&logmsg, 34, commit);
5426 if (err)
5427 goto done;
5429 err = got_object_id_str(&id_str, commit_id);
5430 if (err)
5431 goto done;
5433 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5434 if (n < 0)
5435 err = got_ferror(f, GOT_ERR_IO);
5436 done:
5437 if (commit)
5438 got_object_commit_close(commit);
5439 free(id_str);
5440 free(logmsg);
5441 return err;
5444 static const struct got_error *
5445 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5446 struct got_repository *repo)
5448 const struct got_error *err = NULL;
5449 struct got_object_qid *qid;
5451 if (SIMPLEQ_EMPTY(commits))
5452 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5454 SIMPLEQ_FOREACH(qid, commits, entry) {
5455 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5456 f, repo);
5457 if (err)
5458 break;
5461 return err;
5464 static const struct got_error *
5465 write_cmd_list(FILE *f)
5467 const struct got_error *err = NULL;
5468 int n, i;
5470 n = fprintf(f, "# Available histedit commands:\n");
5471 if (n < 0)
5472 return got_ferror(f, GOT_ERR_IO);
5474 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5475 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5476 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5477 cmd->desc);
5478 if (n < 0) {
5479 err = got_ferror(f, GOT_ERR_IO);
5480 break;
5483 n = fprintf(f, "# Commits will be processed in order from top to "
5484 "bottom of this file.\n");
5485 if (n < 0)
5486 return got_ferror(f, GOT_ERR_IO);
5487 return err;
5490 static const struct got_error *
5491 histedit_syntax_error(int lineno)
5493 static char msg[42];
5494 int ret;
5496 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5497 lineno);
5498 if (ret == -1 || ret >= sizeof(msg))
5499 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5501 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5504 static const struct got_error *
5505 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5506 char *logmsg, struct got_repository *repo)
5508 const struct got_error *err;
5509 struct got_commit_object *folded_commit = NULL;
5510 char *id_str, *folded_logmsg = NULL;
5512 err = got_object_id_str(&id_str, hle->commit_id);
5513 if (err)
5514 return err;
5516 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5517 if (err)
5518 goto done;
5520 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5521 if (err)
5522 goto done;
5523 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5524 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5525 folded_logmsg) == -1) {
5526 err = got_error_from_errno("asprintf");
5527 goto done;
5529 done:
5530 if (folded_commit)
5531 got_object_commit_close(folded_commit);
5532 free(id_str);
5533 free(folded_logmsg);
5534 return err;
5537 static struct got_histedit_list_entry *
5538 get_folded_commits(struct got_histedit_list_entry *hle)
5540 struct got_histedit_list_entry *prev, *folded = NULL;
5542 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5543 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5544 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5545 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5546 folded = prev;
5547 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5550 return folded;
5553 static const struct got_error *
5554 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5555 struct got_repository *repo)
5557 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5558 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5559 const struct got_error *err = NULL;
5560 struct got_commit_object *commit = NULL;
5561 int fd;
5562 struct got_histedit_list_entry *folded = NULL;
5564 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5565 if (err)
5566 return err;
5568 folded = get_folded_commits(hle);
5569 if (folded) {
5570 while (folded != hle) {
5571 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5572 folded = TAILQ_NEXT(folded, entry);
5573 continue;
5575 err = append_folded_commit_msg(&new_msg, folded,
5576 logmsg, repo);
5577 if (err)
5578 goto done;
5579 free(logmsg);
5580 logmsg = new_msg;
5581 folded = TAILQ_NEXT(folded, entry);
5585 err = got_object_id_str(&id_str, hle->commit_id);
5586 if (err)
5587 goto done;
5588 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5589 if (err)
5590 goto done;
5591 if (asprintf(&new_msg,
5592 "%s\n# original log message of commit %s: %s",
5593 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5594 err = got_error_from_errno("asprintf");
5595 goto done;
5597 free(logmsg);
5598 logmsg = new_msg;
5600 err = got_object_id_str(&id_str, hle->commit_id);
5601 if (err)
5602 goto done;
5604 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5605 if (err)
5606 goto done;
5608 dprintf(fd, logmsg);
5609 close(fd);
5611 err = get_editor(&editor);
5612 if (err)
5613 goto done;
5615 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5616 if (err) {
5617 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5618 goto done;
5619 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5621 done:
5622 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5623 err = got_error_from_errno2("unlink", logmsg_path);
5624 free(logmsg_path);
5625 free(logmsg);
5626 free(orig_logmsg);
5627 free(editor);
5628 if (commit)
5629 got_object_commit_close(commit);
5630 return err;
5633 static const struct got_error *
5634 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5635 FILE *f, struct got_repository *repo)
5637 const struct got_error *err = NULL;
5638 char *line = NULL, *p, *end;
5639 size_t size;
5640 ssize_t len;
5641 int lineno = 0, i;
5642 const struct got_histedit_cmd *cmd;
5643 struct got_object_id *commit_id = NULL;
5644 struct got_histedit_list_entry *hle = NULL;
5646 for (;;) {
5647 len = getline(&line, &size, f);
5648 if (len == -1) {
5649 const struct got_error *getline_err;
5650 if (feof(f))
5651 break;
5652 getline_err = got_error_from_errno("getline");
5653 err = got_ferror(f, getline_err->code);
5654 break;
5656 lineno++;
5657 p = line;
5658 while (isspace((unsigned char)p[0]))
5659 p++;
5660 if (p[0] == '#' || p[0] == '\0') {
5661 free(line);
5662 line = NULL;
5663 continue;
5665 cmd = NULL;
5666 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5667 cmd = &got_histedit_cmds[i];
5668 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5669 isspace((unsigned char)p[strlen(cmd->name)])) {
5670 p += strlen(cmd->name);
5671 break;
5673 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5674 p++;
5675 break;
5678 if (i == nitems(got_histedit_cmds)) {
5679 err = histedit_syntax_error(lineno);
5680 break;
5682 while (isspace((unsigned char)p[0]))
5683 p++;
5684 if (cmd->code == GOT_HISTEDIT_MESG) {
5685 if (hle == NULL || hle->logmsg != NULL) {
5686 err = got_error(GOT_ERR_HISTEDIT_CMD);
5687 break;
5689 if (p[0] == '\0') {
5690 err = histedit_edit_logmsg(hle, repo);
5691 if (err)
5692 break;
5693 } else {
5694 hle->logmsg = strdup(p);
5695 if (hle->logmsg == NULL) {
5696 err = got_error_from_errno("strdup");
5697 break;
5700 free(line);
5701 line = NULL;
5702 continue;
5703 } else {
5704 end = p;
5705 while (end[0] && !isspace((unsigned char)end[0]))
5706 end++;
5707 *end = '\0';
5709 err = got_object_resolve_id_str(&commit_id, repo, p);
5710 if (err) {
5711 /* override error code */
5712 err = histedit_syntax_error(lineno);
5713 break;
5716 hle = malloc(sizeof(*hle));
5717 if (hle == NULL) {
5718 err = got_error_from_errno("malloc");
5719 break;
5721 hle->cmd = cmd;
5722 hle->commit_id = commit_id;
5723 hle->logmsg = NULL;
5724 commit_id = NULL;
5725 free(line);
5726 line = NULL;
5727 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5730 free(line);
5731 free(commit_id);
5732 return err;
5735 static const struct got_error *
5736 histedit_check_script(struct got_histedit_list *histedit_cmds,
5737 struct got_object_id_queue *commits, struct got_repository *repo)
5739 const struct got_error *err = NULL;
5740 struct got_object_qid *qid;
5741 struct got_histedit_list_entry *hle;
5742 static char msg[80];
5743 char *id_str;
5745 if (TAILQ_EMPTY(histedit_cmds))
5746 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5747 "histedit script contains no commands");
5748 if (SIMPLEQ_EMPTY(commits))
5749 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5751 SIMPLEQ_FOREACH(qid, commits, entry) {
5752 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5753 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5754 break;
5756 if (hle == NULL) {
5757 err = got_object_id_str(&id_str, qid->id);
5758 if (err)
5759 return err;
5760 snprintf(msg, sizeof(msg),
5761 "commit %s missing from histedit script", id_str);
5762 free(id_str);
5763 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5767 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5768 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5769 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5770 "last commit in histedit script cannot be folded");
5772 return NULL;
5775 static const struct got_error *
5776 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5777 const char *path, struct got_object_id_queue *commits,
5778 struct got_repository *repo)
5780 const struct got_error *err = NULL;
5781 char *editor;
5782 FILE *f = NULL;
5784 err = get_editor(&editor);
5785 if (err)
5786 return err;
5788 if (spawn_editor(editor, path) == -1) {
5789 err = got_error_from_errno("failed spawning editor");
5790 goto done;
5793 f = fopen(path, "r");
5794 if (f == NULL) {
5795 err = got_error_from_errno("fopen");
5796 goto done;
5798 err = histedit_parse_list(histedit_cmds, f, repo);
5799 if (err)
5800 goto done;
5802 err = histedit_check_script(histedit_cmds, commits, repo);
5803 done:
5804 if (f && fclose(f) != 0 && err == NULL)
5805 err = got_error_from_errno("fclose");
5806 free(editor);
5807 return err;
5810 static const struct got_error *
5811 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5812 struct got_object_id_queue *, const char *, struct got_repository *);
5814 static const struct got_error *
5815 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5816 struct got_object_id_queue *commits, struct got_repository *repo)
5818 const struct got_error *err;
5819 FILE *f = NULL;
5820 char *path = NULL;
5822 err = got_opentemp_named(&path, &f, "got-histedit");
5823 if (err)
5824 return err;
5826 err = write_cmd_list(f);
5827 if (err)
5828 goto done;
5830 err = histedit_write_commit_list(commits, f, repo);
5831 if (err)
5832 goto done;
5834 if (fclose(f) != 0) {
5835 err = got_error_from_errno("fclose");
5836 goto done;
5838 f = NULL;
5840 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5841 if (err) {
5842 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5843 err->code != GOT_ERR_HISTEDIT_CMD)
5844 goto done;
5845 err = histedit_edit_list_retry(histedit_cmds, err,
5846 commits, path, repo);
5848 done:
5849 if (f && fclose(f) != 0 && err == NULL)
5850 err = got_error_from_errno("fclose");
5851 if (path && unlink(path) != 0 && err == NULL)
5852 err = got_error_from_errno2("unlink", path);
5853 free(path);
5854 return err;
5857 static const struct got_error *
5858 histedit_save_list(struct got_histedit_list *histedit_cmds,
5859 struct got_worktree *worktree, struct got_repository *repo)
5861 const struct got_error *err = NULL;
5862 char *path = NULL;
5863 FILE *f = NULL;
5864 struct got_histedit_list_entry *hle;
5865 struct got_commit_object *commit = NULL;
5867 err = got_worktree_get_histedit_script_path(&path, worktree);
5868 if (err)
5869 return err;
5871 f = fopen(path, "w");
5872 if (f == NULL) {
5873 err = got_error_from_errno2("fopen", path);
5874 goto done;
5876 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5877 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5878 repo);
5879 if (err)
5880 break;
5882 if (hle->logmsg) {
5883 int n = fprintf(f, "%c %s\n",
5884 GOT_HISTEDIT_MESG, hle->logmsg);
5885 if (n < 0) {
5886 err = got_ferror(f, GOT_ERR_IO);
5887 break;
5891 done:
5892 if (f && fclose(f) != 0 && err == NULL)
5893 err = got_error_from_errno("fclose");
5894 free(path);
5895 if (commit)
5896 got_object_commit_close(commit);
5897 return err;
5900 void
5901 histedit_free_list(struct got_histedit_list *histedit_cmds)
5903 struct got_histedit_list_entry *hle;
5905 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5906 TAILQ_REMOVE(histedit_cmds, hle, entry);
5907 free(hle);
5911 static const struct got_error *
5912 histedit_load_list(struct got_histedit_list *histedit_cmds,
5913 const char *path, struct got_repository *repo)
5915 const struct got_error *err = NULL;
5916 FILE *f = NULL;
5918 f = fopen(path, "r");
5919 if (f == NULL) {
5920 err = got_error_from_errno2("fopen", path);
5921 goto done;
5924 err = histedit_parse_list(histedit_cmds, f, repo);
5925 done:
5926 if (f && fclose(f) != 0 && err == NULL)
5927 err = got_error_from_errno("fclose");
5928 return err;
5931 static const struct got_error *
5932 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5933 const struct got_error *edit_err, struct got_object_id_queue *commits,
5934 const char *path, struct got_repository *repo)
5936 const struct got_error *err = NULL, *prev_err = edit_err;
5937 int resp = ' ';
5939 while (resp != 'c' && resp != 'r' && resp != 'a') {
5940 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5941 "or (a)bort: ", getprogname(), prev_err->msg);
5942 resp = getchar();
5943 if (resp == '\n')
5944 resp = getchar();
5945 if (resp == 'c') {
5946 histedit_free_list(histedit_cmds);
5947 err = histedit_run_editor(histedit_cmds, path, commits,
5948 repo);
5949 if (err) {
5950 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5951 err->code != GOT_ERR_HISTEDIT_CMD)
5952 break;
5953 prev_err = err;
5954 resp = ' ';
5955 continue;
5957 break;
5958 } else if (resp == 'r') {
5959 histedit_free_list(histedit_cmds);
5960 err = histedit_edit_script(histedit_cmds,
5961 commits, repo);
5962 if (err) {
5963 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5964 err->code != GOT_ERR_HISTEDIT_CMD)
5965 break;
5966 prev_err = err;
5967 resp = ' ';
5968 continue;
5970 break;
5971 } else if (resp == 'a') {
5972 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5973 break;
5974 } else
5975 printf("invalid response '%c'\n", resp);
5978 return err;
5981 static const struct got_error *
5982 histedit_complete(struct got_worktree *worktree,
5983 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5984 struct got_reference *branch, struct got_repository *repo)
5986 printf("Switching work tree to %s\n",
5987 got_ref_get_symref_target(branch));
5988 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5989 branch, repo);
5992 static const struct got_error *
5993 show_histedit_progress(struct got_commit_object *commit,
5994 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5996 const struct got_error *err;
5997 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5999 err = got_object_id_str(&old_id_str, hle->commit_id);
6000 if (err)
6001 goto done;
6003 if (new_id) {
6004 err = got_object_id_str(&new_id_str, new_id);
6005 if (err)
6006 goto done;
6009 old_id_str[12] = '\0';
6010 if (new_id_str)
6011 new_id_str[12] = '\0';
6013 if (hle->logmsg) {
6014 logmsg = strdup(hle->logmsg);
6015 if (logmsg == NULL) {
6016 err = got_error_from_errno("strdup");
6017 goto done;
6019 trim_logmsg(logmsg, 42);
6020 } else {
6021 err = get_short_logmsg(&logmsg, 42, commit);
6022 if (err)
6023 goto done;
6026 switch (hle->cmd->code) {
6027 case GOT_HISTEDIT_PICK:
6028 case GOT_HISTEDIT_EDIT:
6029 printf("%s -> %s: %s\n", old_id_str,
6030 new_id_str ? new_id_str : "no-op change", logmsg);
6031 break;
6032 case GOT_HISTEDIT_DROP:
6033 case GOT_HISTEDIT_FOLD:
6034 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6035 logmsg);
6036 break;
6037 default:
6038 break;
6041 done:
6042 free(old_id_str);
6043 free(new_id_str);
6044 return err;
6047 static const struct got_error *
6048 histedit_commit(struct got_pathlist_head *merged_paths,
6049 struct got_worktree *worktree, struct got_fileindex *fileindex,
6050 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6051 struct got_repository *repo)
6053 const struct got_error *err;
6054 struct got_commit_object *commit;
6055 struct got_object_id *new_commit_id;
6057 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6058 && hle->logmsg == NULL) {
6059 err = histedit_edit_logmsg(hle, repo);
6060 if (err)
6061 return err;
6064 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6065 if (err)
6066 return err;
6068 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6069 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6070 hle->logmsg, repo);
6071 if (err) {
6072 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6073 goto done;
6074 err = show_histedit_progress(commit, hle, NULL);
6075 } else {
6076 err = show_histedit_progress(commit, hle, new_commit_id);
6077 free(new_commit_id);
6079 done:
6080 got_object_commit_close(commit);
6081 return err;
6084 static const struct got_error *
6085 histedit_skip_commit(struct got_histedit_list_entry *hle,
6086 struct got_worktree *worktree, struct got_repository *repo)
6088 const struct got_error *error;
6089 struct got_commit_object *commit;
6091 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6092 repo);
6093 if (error)
6094 return error;
6096 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6097 if (error)
6098 return error;
6100 error = show_histedit_progress(commit, hle, NULL);
6101 got_object_commit_close(commit);
6102 return error;
6105 static const struct got_error *
6106 cmd_histedit(int argc, char *argv[])
6108 const struct got_error *error = NULL;
6109 struct got_worktree *worktree = NULL;
6110 struct got_fileindex *fileindex = NULL;
6111 struct got_repository *repo = NULL;
6112 char *cwd = NULL;
6113 struct got_reference *branch = NULL;
6114 struct got_reference *tmp_branch = NULL;
6115 struct got_object_id *resume_commit_id = NULL;
6116 struct got_object_id *base_commit_id = NULL;
6117 struct got_object_id *head_commit_id = NULL;
6118 struct got_commit_object *commit = NULL;
6119 int ch, rebase_in_progress = 0, did_something;
6120 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6121 const char *edit_script_path = NULL;
6122 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6123 struct got_object_id_queue commits;
6124 struct got_pathlist_head merged_paths;
6125 const struct got_object_id_queue *parent_ids;
6126 struct got_object_qid *pid;
6127 struct got_histedit_list histedit_cmds;
6128 struct got_histedit_list_entry *hle;
6130 SIMPLEQ_INIT(&commits);
6131 TAILQ_INIT(&histedit_cmds);
6132 TAILQ_INIT(&merged_paths);
6134 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6135 switch (ch) {
6136 case 'a':
6137 abort_edit = 1;
6138 break;
6139 case 'c':
6140 continue_edit = 1;
6141 break;
6142 case 'F':
6143 edit_script_path = optarg;
6144 break;
6145 default:
6146 usage_histedit();
6147 /* NOTREACHED */
6151 argc -= optind;
6152 argv += optind;
6154 #ifndef PROFILE
6155 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6156 "unveil", NULL) == -1)
6157 err(1, "pledge");
6158 #endif
6159 if (abort_edit && continue_edit)
6160 usage_histedit();
6161 if (argc != 0)
6162 usage_histedit();
6165 * This command cannot apply unveil(2) in all cases because the
6166 * user may choose to run an editor to edit the histedit script
6167 * and to edit individual commit log messages.
6168 * unveil(2) traverses exec(2); if an editor is used we have to
6169 * apply unveil after edit script and log messages have been written.
6170 * XXX TODO: Make use of unveil(2) where possible.
6173 cwd = getcwd(NULL, 0);
6174 if (cwd == NULL) {
6175 error = got_error_from_errno("getcwd");
6176 goto done;
6178 error = got_worktree_open(&worktree, cwd);
6179 if (error)
6180 goto done;
6182 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6183 NULL);
6184 if (error != NULL)
6185 goto done;
6187 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6188 if (error)
6189 goto done;
6190 if (rebase_in_progress) {
6191 error = got_error(GOT_ERR_REBASING);
6192 goto done;
6195 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6196 if (error)
6197 goto done;
6199 if (edit_in_progress && abort_edit) {
6200 error = got_worktree_histedit_continue(&resume_commit_id,
6201 &tmp_branch, &branch, &base_commit_id, &fileindex,
6202 worktree, repo);
6203 if (error)
6204 goto done;
6205 printf("Switching work tree to %s\n",
6206 got_ref_get_symref_target(branch));
6207 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6208 branch, base_commit_id, update_progress, &did_something);
6209 if (error)
6210 goto done;
6211 printf("Histedit of %s aborted\n",
6212 got_ref_get_symref_target(branch));
6213 goto done; /* nothing else to do */
6214 } else if (abort_edit) {
6215 error = got_error(GOT_ERR_NOT_HISTEDIT);
6216 goto done;
6219 if (continue_edit) {
6220 char *path;
6222 if (!edit_in_progress) {
6223 error = got_error(GOT_ERR_NOT_HISTEDIT);
6224 goto done;
6227 error = got_worktree_get_histedit_script_path(&path, worktree);
6228 if (error)
6229 goto done;
6231 error = histedit_load_list(&histedit_cmds, path, repo);
6232 free(path);
6233 if (error)
6234 goto done;
6236 error = got_worktree_histedit_continue(&resume_commit_id,
6237 &tmp_branch, &branch, &base_commit_id, &fileindex,
6238 worktree, repo);
6239 if (error)
6240 goto done;
6242 error = got_ref_resolve(&head_commit_id, repo, branch);
6243 if (error)
6244 goto done;
6246 error = got_object_open_as_commit(&commit, repo,
6247 head_commit_id);
6248 if (error)
6249 goto done;
6250 parent_ids = got_object_commit_get_parent_ids(commit);
6251 pid = SIMPLEQ_FIRST(parent_ids);
6252 if (pid == NULL) {
6253 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6254 goto done;
6256 error = collect_commits(&commits, head_commit_id, pid->id,
6257 base_commit_id, got_worktree_get_path_prefix(worktree),
6258 GOT_ERR_HISTEDIT_PATH, repo);
6259 got_object_commit_close(commit);
6260 commit = NULL;
6261 if (error)
6262 goto done;
6263 } else {
6264 if (edit_in_progress) {
6265 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6266 goto done;
6269 error = got_ref_open(&branch, repo,
6270 got_worktree_get_head_ref_name(worktree), 0);
6271 if (error != NULL)
6272 goto done;
6274 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6275 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6276 "will not edit commit history of a branch outside "
6277 "the \"refs/heads/\" reference namespace");
6278 goto done;
6281 error = got_ref_resolve(&head_commit_id, repo, branch);
6282 got_ref_close(branch);
6283 branch = NULL;
6284 if (error)
6285 goto done;
6287 error = got_object_open_as_commit(&commit, repo,
6288 head_commit_id);
6289 if (error)
6290 goto done;
6291 parent_ids = got_object_commit_get_parent_ids(commit);
6292 pid = SIMPLEQ_FIRST(parent_ids);
6293 if (pid == NULL) {
6294 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6295 goto done;
6297 error = collect_commits(&commits, head_commit_id, pid->id,
6298 got_worktree_get_base_commit_id(worktree),
6299 got_worktree_get_path_prefix(worktree),
6300 GOT_ERR_HISTEDIT_PATH, repo);
6301 got_object_commit_close(commit);
6302 commit = NULL;
6303 if (error)
6304 goto done;
6306 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6307 &base_commit_id, &fileindex, worktree, repo);
6308 if (error)
6309 goto done;
6311 if (edit_script_path) {
6312 error = histedit_load_list(&histedit_cmds,
6313 edit_script_path, repo);
6314 if (error) {
6315 got_worktree_histedit_abort(worktree, fileindex,
6316 repo, branch, base_commit_id,
6317 update_progress, &did_something);
6318 goto done;
6320 } else {
6321 error = histedit_edit_script(&histedit_cmds, &commits,
6322 repo);
6323 if (error) {
6324 got_worktree_histedit_abort(worktree, fileindex,
6325 repo, branch, base_commit_id,
6326 update_progress, &did_something);
6327 goto done;
6332 error = histedit_save_list(&histedit_cmds, worktree,
6333 repo);
6334 if (error) {
6335 got_worktree_histedit_abort(worktree, fileindex,
6336 repo, branch, base_commit_id,
6337 update_progress, &did_something);
6338 goto done;
6343 error = histedit_check_script(&histedit_cmds, &commits, repo);
6344 if (error)
6345 goto done;
6347 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6348 if (resume_commit_id) {
6349 if (got_object_id_cmp(hle->commit_id,
6350 resume_commit_id) != 0)
6351 continue;
6353 resume_commit_id = NULL;
6354 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6355 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6356 error = histedit_skip_commit(hle, worktree,
6357 repo);
6358 } else {
6359 error = histedit_commit(NULL, worktree,
6360 fileindex, tmp_branch, hle, repo);
6362 if (error)
6363 goto done;
6364 continue;
6367 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6368 error = histedit_skip_commit(hle, worktree, repo);
6369 if (error)
6370 goto done;
6371 continue;
6374 error = got_object_open_as_commit(&commit, repo,
6375 hle->commit_id);
6376 if (error)
6377 goto done;
6378 parent_ids = got_object_commit_get_parent_ids(commit);
6379 pid = SIMPLEQ_FIRST(parent_ids);
6381 error = got_worktree_histedit_merge_files(&merged_paths,
6382 worktree, fileindex, pid->id, hle->commit_id, repo,
6383 rebase_progress, &rebase_status, check_cancelled, NULL);
6384 if (error)
6385 goto done;
6386 got_object_commit_close(commit);
6387 commit = NULL;
6389 if (rebase_status == GOT_STATUS_CONFLICT) {
6390 got_worktree_rebase_pathlist_free(&merged_paths);
6391 break;
6394 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6395 char *id_str;
6396 error = got_object_id_str(&id_str, hle->commit_id);
6397 if (error)
6398 goto done;
6399 printf("Stopping histedit for amending commit %s\n",
6400 id_str);
6401 free(id_str);
6402 got_worktree_rebase_pathlist_free(&merged_paths);
6403 error = got_worktree_histedit_postpone(worktree,
6404 fileindex);
6405 goto done;
6408 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6409 error = histedit_skip_commit(hle, worktree, repo);
6410 if (error)
6411 goto done;
6412 continue;
6415 error = histedit_commit(&merged_paths, worktree, fileindex,
6416 tmp_branch, hle, repo);
6417 got_worktree_rebase_pathlist_free(&merged_paths);
6418 if (error)
6419 goto done;
6422 if (rebase_status == GOT_STATUS_CONFLICT) {
6423 error = got_worktree_histedit_postpone(worktree, fileindex);
6424 if (error)
6425 goto done;
6426 error = got_error_msg(GOT_ERR_CONFLICTS,
6427 "conflicts must be resolved before rebasing can continue");
6428 } else
6429 error = histedit_complete(worktree, fileindex, tmp_branch,
6430 branch, repo);
6431 done:
6432 got_object_id_queue_free(&commits);
6433 histedit_free_list(&histedit_cmds);
6434 free(head_commit_id);
6435 free(base_commit_id);
6436 free(resume_commit_id);
6437 if (commit)
6438 got_object_commit_close(commit);
6439 if (branch)
6440 got_ref_close(branch);
6441 if (tmp_branch)
6442 got_ref_close(tmp_branch);
6443 if (worktree)
6444 got_worktree_close(worktree);
6445 if (repo)
6446 got_repo_close(repo);
6447 return error;
6450 __dead static void
6451 usage_stage(void)
6453 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6454 "[file-path ...]\n",
6455 getprogname());
6456 exit(1);
6459 static const struct got_error *
6460 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6461 const char *path, struct got_object_id *blob_id,
6462 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6464 const struct got_error *err = NULL;
6465 char *id_str = NULL;
6467 if (staged_status != GOT_STATUS_ADD &&
6468 staged_status != GOT_STATUS_MODIFY &&
6469 staged_status != GOT_STATUS_DELETE)
6470 return NULL;
6472 if (staged_status == GOT_STATUS_ADD ||
6473 staged_status == GOT_STATUS_MODIFY)
6474 err = got_object_id_str(&id_str, staged_blob_id);
6475 else
6476 err = got_object_id_str(&id_str, blob_id);
6477 if (err)
6478 return err;
6480 printf("%s %c %s\n", id_str, staged_status, path);
6481 free(id_str);
6482 return NULL;
6485 static const struct got_error *
6486 cmd_stage(int argc, char *argv[])
6488 const struct got_error *error = NULL;
6489 struct got_repository *repo = NULL;
6490 struct got_worktree *worktree = NULL;
6491 char *cwd = NULL;
6492 struct got_pathlist_head paths;
6493 struct got_pathlist_entry *pe;
6494 int ch, list_stage = 0, pflag = 0;
6495 FILE *patch_script_file = NULL;
6496 const char *patch_script_path = NULL;
6497 struct choose_patch_arg cpa;
6499 TAILQ_INIT(&paths);
6501 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6502 switch (ch) {
6503 case 'l':
6504 list_stage = 1;
6505 break;
6506 case 'p':
6507 pflag = 1;
6508 break;
6509 case 'F':
6510 patch_script_path = optarg;
6511 break;
6512 default:
6513 usage_stage();
6514 /* NOTREACHED */
6518 argc -= optind;
6519 argv += optind;
6521 #ifndef PROFILE
6522 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6523 "unveil", NULL) == -1)
6524 err(1, "pledge");
6525 #endif
6526 if (list_stage && (pflag || patch_script_path))
6527 errx(1, "-l option cannot be used with other options");
6528 if (patch_script_path && !pflag)
6529 errx(1, "-F option can only be used together with -p option");
6531 cwd = getcwd(NULL, 0);
6532 if (cwd == NULL) {
6533 error = got_error_from_errno("getcwd");
6534 goto done;
6537 error = got_worktree_open(&worktree, cwd);
6538 if (error)
6539 goto done;
6541 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6542 NULL);
6543 if (error != NULL)
6544 goto done;
6546 if (patch_script_path) {
6547 patch_script_file = fopen(patch_script_path, "r");
6548 if (patch_script_file == NULL) {
6549 error = got_error_from_errno2("fopen",
6550 patch_script_path);
6551 goto done;
6554 error = apply_unveil(got_repo_get_path(repo), 0,
6555 got_worktree_get_root_path(worktree));
6556 if (error)
6557 goto done;
6559 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6560 if (error)
6561 goto done;
6563 if (list_stage)
6564 error = got_worktree_status(worktree, &paths, repo,
6565 print_stage, NULL, check_cancelled, NULL);
6566 else {
6567 cpa.patch_script_file = patch_script_file;
6568 cpa.action = "stage";
6569 error = got_worktree_stage(worktree, &paths,
6570 pflag ? NULL : print_status, NULL,
6571 pflag ? choose_patch : NULL, &cpa, repo);
6573 done:
6574 if (patch_script_file && fclose(patch_script_file) == EOF &&
6575 error == NULL)
6576 error = got_error_from_errno2("fclose", patch_script_path);
6577 if (repo)
6578 got_repo_close(repo);
6579 if (worktree)
6580 got_worktree_close(worktree);
6581 TAILQ_FOREACH(pe, &paths, entry)
6582 free((char *)pe->path);
6583 got_pathlist_free(&paths);
6584 free(cwd);
6585 return error;
6588 __dead static void
6589 usage_unstage(void)
6591 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6592 "[file-path ...]\n",
6593 getprogname());
6594 exit(1);
6598 static const struct got_error *
6599 cmd_unstage(int argc, char *argv[])
6601 const struct got_error *error = NULL;
6602 struct got_repository *repo = NULL;
6603 struct got_worktree *worktree = NULL;
6604 char *cwd = NULL;
6605 struct got_pathlist_head paths;
6606 struct got_pathlist_entry *pe;
6607 int ch, did_something = 0, pflag = 0;
6608 FILE *patch_script_file = NULL;
6609 const char *patch_script_path = NULL;
6610 struct choose_patch_arg cpa;
6612 TAILQ_INIT(&paths);
6614 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6615 switch (ch) {
6616 case 'p':
6617 pflag = 1;
6618 break;
6619 case 'F':
6620 patch_script_path = optarg;
6621 break;
6622 default:
6623 usage_unstage();
6624 /* NOTREACHED */
6628 argc -= optind;
6629 argv += optind;
6631 #ifndef PROFILE
6632 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6633 "unveil", NULL) == -1)
6634 err(1, "pledge");
6635 #endif
6636 if (patch_script_path && !pflag)
6637 errx(1, "-F option can only be used together with -p option");
6639 cwd = getcwd(NULL, 0);
6640 if (cwd == NULL) {
6641 error = got_error_from_errno("getcwd");
6642 goto done;
6645 error = got_worktree_open(&worktree, cwd);
6646 if (error)
6647 goto done;
6649 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6650 NULL);
6651 if (error != NULL)
6652 goto done;
6654 if (patch_script_path) {
6655 patch_script_file = fopen(patch_script_path, "r");
6656 if (patch_script_file == NULL) {
6657 error = got_error_from_errno2("fopen",
6658 patch_script_path);
6659 goto done;
6663 error = apply_unveil(got_repo_get_path(repo), 0,
6664 got_worktree_get_root_path(worktree));
6665 if (error)
6666 goto done;
6668 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6669 if (error)
6670 goto done;
6672 cpa.patch_script_file = patch_script_file;
6673 cpa.action = "unstage";
6674 error = got_worktree_unstage(worktree, &paths, update_progress,
6675 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6676 done:
6677 if (patch_script_file && fclose(patch_script_file) == EOF &&
6678 error == NULL)
6679 error = got_error_from_errno2("fclose", patch_script_path);
6680 if (repo)
6681 got_repo_close(repo);
6682 if (worktree)
6683 got_worktree_close(worktree);
6684 TAILQ_FOREACH(pe, &paths, entry)
6685 free((char *)pe->path);
6686 got_pathlist_free(&paths);
6687 free(cwd);
6688 return error;
6691 __dead static void
6692 usage_cat(void)
6694 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6695 "arg1 [arg2 ...]\n", getprogname());
6696 exit(1);
6699 static const struct got_error *
6700 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6702 const struct got_error *err;
6703 struct got_blob_object *blob;
6705 err = got_object_open_as_blob(&blob, repo, id, 8192);
6706 if (err)
6707 return err;
6709 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6710 got_object_blob_close(blob);
6711 return err;
6714 static const struct got_error *
6715 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6717 const struct got_error *err;
6718 struct got_tree_object *tree;
6719 const struct got_tree_entries *entries;
6720 struct got_tree_entry *te;
6722 err = got_object_open_as_tree(&tree, repo, id);
6723 if (err)
6724 return err;
6726 entries = got_object_tree_get_entries(tree);
6727 te = SIMPLEQ_FIRST(&entries->head);
6728 while (te) {
6729 char *id_str;
6730 if (sigint_received || sigpipe_received)
6731 break;
6732 err = got_object_id_str(&id_str, te->id);
6733 if (err)
6734 break;
6735 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6736 free(id_str);
6737 te = SIMPLEQ_NEXT(te, entry);
6740 got_object_tree_close(tree);
6741 return err;
6744 static const struct got_error *
6745 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6747 const struct got_error *err;
6748 struct got_commit_object *commit;
6749 const struct got_object_id_queue *parent_ids;
6750 struct got_object_qid *pid;
6751 char *id_str = NULL;
6752 const char *logmsg = NULL;
6754 err = got_object_open_as_commit(&commit, repo, id);
6755 if (err)
6756 return err;
6758 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6759 if (err)
6760 goto done;
6762 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6763 parent_ids = got_object_commit_get_parent_ids(commit);
6764 fprintf(outfile, "numparents %d\n",
6765 got_object_commit_get_nparents(commit));
6766 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6767 char *pid_str;
6768 err = got_object_id_str(&pid_str, pid->id);
6769 if (err)
6770 goto done;
6771 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6772 free(pid_str);
6774 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6775 got_object_commit_get_author(commit),
6776 got_object_commit_get_author_time(commit));
6778 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6779 got_object_commit_get_author(commit),
6780 got_object_commit_get_committer_time(commit));
6782 logmsg = got_object_commit_get_logmsg_raw(commit);
6783 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6784 fprintf(outfile, "%s", logmsg);
6785 done:
6786 free(id_str);
6787 got_object_commit_close(commit);
6788 return err;
6791 static const struct got_error *
6792 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6794 const struct got_error *err;
6795 struct got_tag_object *tag;
6796 char *id_str = NULL;
6797 const char *tagmsg = NULL;
6799 err = got_object_open_as_tag(&tag, repo, id);
6800 if (err)
6801 return err;
6803 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6804 if (err)
6805 goto done;
6807 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6809 switch (got_object_tag_get_object_type(tag)) {
6810 case GOT_OBJ_TYPE_BLOB:
6811 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6812 GOT_OBJ_LABEL_BLOB);
6813 break;
6814 case GOT_OBJ_TYPE_TREE:
6815 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6816 GOT_OBJ_LABEL_TREE);
6817 break;
6818 case GOT_OBJ_TYPE_COMMIT:
6819 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6820 GOT_OBJ_LABEL_COMMIT);
6821 break;
6822 case GOT_OBJ_TYPE_TAG:
6823 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6824 GOT_OBJ_LABEL_TAG);
6825 break;
6826 default:
6827 break;
6830 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6831 got_object_tag_get_name(tag));
6833 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6834 got_object_tag_get_tagger(tag),
6835 got_object_tag_get_tagger_time(tag));
6837 tagmsg = got_object_tag_get_message(tag);
6838 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6839 fprintf(outfile, "%s", tagmsg);
6840 done:
6841 free(id_str);
6842 got_object_tag_close(tag);
6843 return err;
6846 static const struct got_error *
6847 cmd_cat(int argc, char *argv[])
6849 const struct got_error *error;
6850 struct got_repository *repo = NULL;
6851 struct got_worktree *worktree = NULL;
6852 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6853 const char *commit_id_str = NULL;
6854 struct got_object_id *id = NULL, *commit_id = NULL;
6855 int ch, obj_type, i, force_path = 0;
6857 #ifndef PROFILE
6858 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6859 NULL) == -1)
6860 err(1, "pledge");
6861 #endif
6863 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6864 switch (ch) {
6865 case 'c':
6866 commit_id_str = optarg;
6867 break;
6868 case 'r':
6869 repo_path = realpath(optarg, NULL);
6870 if (repo_path == NULL)
6871 err(1, "-r option");
6872 got_path_strip_trailing_slashes(repo_path);
6873 break;
6874 case 'P':
6875 force_path = 1;
6876 break;
6877 default:
6878 usage_cat();
6879 /* NOTREACHED */
6883 argc -= optind;
6884 argv += optind;
6886 cwd = getcwd(NULL, 0);
6887 if (cwd == NULL) {
6888 error = got_error_from_errno("getcwd");
6889 goto done;
6891 error = got_worktree_open(&worktree, cwd);
6892 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6893 goto done;
6894 if (worktree) {
6895 if (repo_path == NULL) {
6896 repo_path = strdup(
6897 got_worktree_get_repo_path(worktree));
6898 if (repo_path == NULL) {
6899 error = got_error_from_errno("strdup");
6900 goto done;
6905 if (repo_path == NULL) {
6906 repo_path = getcwd(NULL, 0);
6907 if (repo_path == NULL)
6908 return got_error_from_errno("getcwd");
6911 error = got_repo_open(&repo, repo_path, NULL);
6912 free(repo_path);
6913 if (error != NULL)
6914 goto done;
6916 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6917 if (error)
6918 goto done;
6920 if (commit_id_str == NULL)
6921 commit_id_str = GOT_REF_HEAD;
6922 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6923 if (error)
6924 goto done;
6926 for (i = 0; i < argc; i++) {
6927 if (force_path) {
6928 error = got_object_id_by_path(&id, repo, commit_id,
6929 argv[i]);
6930 if (error)
6931 break;
6932 } else {
6933 error = match_object_id(&id, &label, argv[i],
6934 GOT_OBJ_TYPE_ANY, 0, repo);
6935 if (error) {
6936 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6937 error->code != GOT_ERR_NOT_REF)
6938 break;
6939 error = got_object_id_by_path(&id, repo,
6940 commit_id, argv[i]);
6941 if (error)
6942 break;
6946 error = got_object_get_type(&obj_type, repo, id);
6947 if (error)
6948 break;
6950 switch (obj_type) {
6951 case GOT_OBJ_TYPE_BLOB:
6952 error = cat_blob(id, repo, stdout);
6953 break;
6954 case GOT_OBJ_TYPE_TREE:
6955 error = cat_tree(id, repo, stdout);
6956 break;
6957 case GOT_OBJ_TYPE_COMMIT:
6958 error = cat_commit(id, repo, stdout);
6959 break;
6960 case GOT_OBJ_TYPE_TAG:
6961 error = cat_tag(id, repo, stdout);
6962 break;
6963 default:
6964 error = got_error(GOT_ERR_OBJ_TYPE);
6965 break;
6967 if (error)
6968 break;
6969 free(label);
6970 label = NULL;
6971 free(id);
6972 id = NULL;
6975 done:
6976 free(label);
6977 free(id);
6978 free(commit_id);
6979 if (worktree)
6980 got_worktree_close(worktree);
6981 if (repo) {
6982 const struct got_error *repo_error;
6983 repo_error = got_repo_close(repo);
6984 if (error == NULL)
6985 error = repo_error;
6987 return error;