Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
41 #include <util.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
65 static void
66 catch_sigint(int signo)
67 {
68 sigint_received = 1;
69 }
71 static void
72 catch_sigpipe(int signo)
73 {
74 sigpipe_received = 1;
75 }
78 struct got_cmd {
79 const char *cmd_name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
83 };
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_checkout(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_update(void);
91 __dead static void usage_log(void);
92 __dead static void usage_diff(void);
93 __dead static void usage_blame(void);
94 __dead static void usage_tree(void);
95 __dead static void usage_status(void);
96 __dead static void usage_ref(void);
97 __dead static void usage_branch(void);
98 __dead static void usage_tag(void);
99 __dead static void usage_add(void);
100 __dead static void usage_remove(void);
101 __dead static void usage_revert(void);
102 __dead static void usage_commit(void);
103 __dead static void usage_cherrypick(void);
104 __dead static void usage_backout(void);
105 __dead static void usage_rebase(void);
106 __dead static void usage_histedit(void);
107 __dead static void usage_integrate(void);
108 __dead static void usage_stage(void);
109 __dead static void usage_unstage(void);
110 __dead static void usage_cat(void);
112 static const struct got_error* cmd_init(int, char *[]);
113 static const struct got_error* cmd_import(int, char *[]);
114 static const struct got_error* cmd_clone(int, char *[]);
115 static const struct got_error* cmd_checkout(int, char *[]);
116 static const struct got_error* cmd_update(int, char *[]);
117 static const struct got_error* cmd_log(int, char *[]);
118 static const struct got_error* cmd_diff(int, char *[]);
119 static const struct got_error* cmd_blame(int, char *[]);
120 static const struct got_error* cmd_tree(int, char *[]);
121 static const struct got_error* cmd_status(int, char *[]);
122 static const struct got_error* cmd_ref(int, char *[]);
123 static const struct got_error* cmd_branch(int, char *[]);
124 static const struct got_error* cmd_tag(int, char *[]);
125 static const struct got_error* cmd_add(int, char *[]);
126 static const struct got_error* cmd_remove(int, char *[]);
127 static const struct got_error* cmd_revert(int, char *[]);
128 static const struct got_error* cmd_commit(int, char *[]);
129 static const struct got_error* cmd_cherrypick(int, char *[]);
130 static const struct got_error* cmd_backout(int, char *[]);
131 static const struct got_error* cmd_rebase(int, char *[]);
132 static const struct got_error* cmd_histedit(int, char *[]);
133 static const struct got_error* cmd_integrate(int, char *[]);
134 static const struct got_error* cmd_stage(int, char *[]);
135 static const struct got_error* cmd_unstage(int, char *[]);
136 static const struct got_error* cmd_cat(int, char *[]);
138 static struct got_cmd got_commands[] = {
139 { "init", cmd_init, usage_init, "in" },
140 { "import", cmd_import, usage_import, "im" },
141 { "checkout", cmd_checkout, usage_checkout, "co" },
142 { "clone", cmd_clone, usage_clone, "cl" },
143 { "update", cmd_update, usage_update, "up" },
144 { "log", cmd_log, usage_log, "" },
145 { "diff", cmd_diff, usage_diff, "di" },
146 { "blame", cmd_blame, usage_blame, "bl" },
147 { "tree", cmd_tree, usage_tree, "tr" },
148 { "status", cmd_status, usage_status, "st" },
149 { "ref", cmd_ref, usage_ref, "" },
150 { "branch", cmd_branch, usage_branch, "br" },
151 { "tag", cmd_tag, usage_tag, "" },
152 { "add", cmd_add, usage_add, "" },
153 { "remove", cmd_remove, usage_remove, "rm" },
154 { "revert", cmd_revert, usage_revert, "rv" },
155 { "commit", cmd_commit, usage_commit, "ci" },
156 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 { "backout", cmd_backout, usage_backout, "bo" },
158 { "rebase", cmd_rebase, usage_rebase, "rb" },
159 { "histedit", cmd_histedit, usage_histedit, "he" },
160 { "integrate", cmd_integrate, usage_integrate,"ig" },
161 { "stage", cmd_stage, usage_stage, "sg" },
162 { "unstage", cmd_unstage, usage_unstage, "ug" },
163 { "cat", cmd_cat, usage_cat, "" },
164 };
166 static void
167 list_commands(void)
169 int i;
171 fprintf(stderr, "commands:");
172 for (i = 0; i < nitems(got_commands); i++) {
173 struct got_cmd *cmd = &got_commands[i];
174 fprintf(stderr, " %s", cmd->cmd_name);
176 fputc('\n', stderr);
179 int
180 main(int argc, char *argv[])
182 struct got_cmd *cmd;
183 unsigned int i;
184 int ch;
185 int hflag = 0, Vflag = 0;
186 static struct option longopts[] = {
187 { "version", no_argument, NULL, 'V' },
188 { NULL, 0, NULL, 0}
189 };
191 setlocale(LC_CTYPE, "");
193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 switch (ch) {
195 case 'h':
196 hflag = 1;
197 break;
198 case 'V':
199 Vflag = 1;
200 break;
201 default:
202 usage(hflag);
203 /* NOTREACHED */
207 argc -= optind;
208 argv += optind;
209 optind = 0;
211 if (Vflag) {
212 got_version_print_str();
213 return 1;
216 if (argc <= 0)
217 usage(hflag);
219 signal(SIGINT, catch_sigint);
220 signal(SIGPIPE, catch_sigpipe);
222 for (i = 0; i < nitems(got_commands); i++) {
223 const struct got_error *error;
225 cmd = &got_commands[i];
227 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 strcmp(cmd->cmd_alias, argv[0]) != 0)
229 continue;
231 if (hflag)
232 got_commands[i].cmd_usage();
234 error = got_commands[i].cmd_main(argc, argv);
235 if (error && error->code != GOT_ERR_CANCELLED &&
236 error->code != GOT_ERR_PRIVSEP_EXIT &&
237 !(sigpipe_received &&
238 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 !(sigint_received &&
240 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 return 1;
245 return 0;
248 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 list_commands();
250 return 1;
253 __dead static void
254 usage(int hflag)
256 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 getprogname());
258 if (hflag)
259 list_commands();
260 exit(1);
263 static const struct got_error *
264 get_editor(char **abspath)
266 const struct got_error *err = NULL;
267 const char *editor;
269 *abspath = NULL;
271 editor = getenv("VISUAL");
272 if (editor == NULL)
273 editor = getenv("EDITOR");
275 if (editor) {
276 err = got_path_find_prog(abspath, editor);
277 if (err)
278 return err;
281 if (*abspath == NULL) {
282 *abspath = strdup("/bin/ed");
283 if (*abspath == NULL)
284 return got_error_from_errno("strdup");
287 return NULL;
290 static const struct got_error *
291 apply_unveil(const char *repo_path, int repo_read_only,
292 const char *worktree_path)
294 const struct got_error *err;
296 #ifdef PROFILE
297 if (unveil("gmon.out", "rwc") != 0)
298 return got_error_from_errno2("unveil", "gmon.out");
299 #endif
300 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 return got_error_from_errno2("unveil", worktree_path);
306 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 err = got_privsep_unveil_exec_helpers();
310 if (err != NULL)
311 return err;
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
316 return NULL;
319 __dead static void
320 usage_init(void)
322 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
323 exit(1);
326 static const struct got_error *
327 cmd_init(int argc, char *argv[])
329 const struct got_error *error = NULL;
330 char *repo_path = NULL;
331 int ch;
333 while ((ch = getopt(argc, argv, "")) != -1) {
334 switch (ch) {
335 default:
336 usage_init();
337 /* NOTREACHED */
341 argc -= optind;
342 argv += optind;
344 #ifndef PROFILE
345 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 err(1, "pledge");
347 #endif
348 if (argc != 1)
349 usage_init();
351 repo_path = strdup(argv[0]);
352 if (repo_path == NULL)
353 return got_error_from_errno("strdup");
355 got_path_strip_trailing_slashes(repo_path);
357 error = got_path_mkdir(repo_path);
358 if (error &&
359 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
360 goto done;
362 error = apply_unveil(repo_path, 0, NULL);
363 if (error)
364 goto done;
366 error = got_repo_init(repo_path);
367 done:
368 free(repo_path);
369 return error;
372 __dead static void
373 usage_import(void)
375 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 "[-r repository-path] [-I pattern] path\n", getprogname());
377 exit(1);
380 int
381 spawn_editor(const char *editor, const char *file)
383 pid_t pid;
384 sig_t sighup, sigint, sigquit;
385 int st = -1;
387 sighup = signal(SIGHUP, SIG_IGN);
388 sigint = signal(SIGINT, SIG_IGN);
389 sigquit = signal(SIGQUIT, SIG_IGN);
391 switch (pid = fork()) {
392 case -1:
393 goto doneediting;
394 case 0:
395 execl(editor, editor, file, (char *)NULL);
396 _exit(127);
399 while (waitpid(pid, &st, 0) == -1)
400 if (errno != EINTR)
401 break;
403 doneediting:
404 (void)signal(SIGHUP, sighup);
405 (void)signal(SIGINT, sigint);
406 (void)signal(SIGQUIT, sigquit);
408 if (!WIFEXITED(st)) {
409 errno = EINTR;
410 return -1;
413 return WEXITSTATUS(st);
416 static const struct got_error *
417 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 const char *initial_content)
420 const struct got_error *err = NULL;
421 char buf[1024];
422 struct stat st, st2;
423 FILE *fp;
424 int content_changed = 0;
425 size_t len;
427 *logmsg = NULL;
429 if (stat(logmsg_path, &st) == -1)
430 return got_error_from_errno2("stat", logmsg_path);
432 if (spawn_editor(editor, logmsg_path) == -1)
433 return got_error_from_errno("failed spawning editor");
435 if (stat(logmsg_path, &st2) == -1)
436 return got_error_from_errno("stat");
438 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 "no changes made to commit message, aborting");
442 *logmsg = malloc(st2.st_size + 1);
443 if (*logmsg == NULL)
444 return got_error_from_errno("malloc");
445 (*logmsg)[0] = '\0';
446 len = 0;
448 fp = fopen(logmsg_path, "r");
449 if (fp == NULL) {
450 err = got_error_from_errno("fopen");
451 goto done;
453 while (fgets(buf, sizeof(buf), fp) != NULL) {
454 if (!content_changed && strcmp(buf, initial_content) != 0)
455 content_changed = 1;
456 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 continue; /* remove comments and leading empty lines */
458 len = strlcat(*logmsg, buf, st2.st_size);
460 fclose(fp);
462 while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 (*logmsg)[len - 1] = '\0';
464 len--;
467 if (len == 0 || !content_changed)
468 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "commit message cannot be empty, aborting");
470 done:
471 if (err) {
472 free(*logmsg);
473 *logmsg = NULL;
475 return err;
478 static const struct got_error *
479 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 const char *path_dir, const char *branch_name)
482 char *initial_content = NULL;
483 const struct got_error *err = NULL;
484 int fd;
486 if (asprintf(&initial_content,
487 "\n# %s to be imported to branch %s\n", path_dir,
488 branch_name) == -1)
489 return got_error_from_errno("asprintf");
491 err = got_opentemp_named_fd(logmsg_path, &fd,
492 GOT_TMPDIR_STR "/got-importmsg");
493 if (err)
494 goto done;
496 dprintf(fd, initial_content);
497 close(fd);
499 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 done:
501 free(initial_content);
502 return err;
505 static const struct got_error *
506 import_progress(void *arg, const char *path)
508 printf("A %s\n", path);
509 return NULL;
512 static const struct got_error *
513 get_author(char **author, struct got_repository *repo)
515 const struct got_error *err = NULL;
516 const char *got_author, *name, *email;
518 *author = NULL;
520 name = got_repo_get_gitconfig_author_name(repo);
521 email = got_repo_get_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
528 got_author = getenv("GOT_AUTHOR");
529 if (got_author == NULL) {
530 name = got_repo_get_global_gitconfig_author_name(repo);
531 email = got_repo_get_global_gitconfig_author_email(repo);
532 if (name && email) {
533 if (asprintf(author, "%s <%s>", name, email) == -1)
534 return got_error_from_errno("asprintf");
535 return NULL;
537 /* TODO: Look up user in password database? */
538 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
541 *author = strdup(got_author);
542 if (*author == NULL)
543 return got_error_from_errno("strdup");
545 /*
546 * Really dumb email address check; we're only doing this to
547 * avoid git's object parser breaking on commits we create.
548 */
549 while (*got_author && *got_author != '<')
550 got_author++;
551 if (*got_author != '<') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 goto done;
555 while (*got_author && *got_author != '@')
556 got_author++;
557 if (*got_author != '@') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 goto done;
561 while (*got_author && *got_author != '>')
562 got_author++;
563 if (*got_author != '>')
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 done:
566 if (err) {
567 free(*author);
568 *author = NULL;
570 return err;
573 static const struct got_error *
574 get_gitconfig_path(char **gitconfig_path)
576 const char *homedir = getenv("HOME");
578 *gitconfig_path = NULL;
579 if (homedir) {
580 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 return got_error_from_errno("asprintf");
584 return NULL;
587 static const struct got_error *
588 cmd_import(int argc, char *argv[])
590 const struct got_error *error = NULL;
591 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 const char *branch_name = "main";
594 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 struct got_repository *repo = NULL;
596 struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 struct got_object_id *new_commit_id = NULL;
598 int ch;
599 struct got_pathlist_head ignores;
600 struct got_pathlist_entry *pe;
601 int preserve_logmsg = 0;
603 TAILQ_INIT(&ignores);
605 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 switch (ch) {
607 case 'b':
608 branch_name = optarg;
609 break;
610 case 'm':
611 logmsg = strdup(optarg);
612 if (logmsg == NULL) {
613 error = got_error_from_errno("strdup");
614 goto done;
616 break;
617 case 'r':
618 repo_path = realpath(optarg, NULL);
619 if (repo_path == NULL) {
620 error = got_error_from_errno2("realpath",
621 optarg);
622 goto done;
624 break;
625 case 'I':
626 if (optarg[0] == '\0')
627 break;
628 error = got_pathlist_insert(&pe, &ignores, optarg,
629 NULL);
630 if (error)
631 goto done;
632 break;
633 default:
634 usage_import();
635 /* NOTREACHED */
639 argc -= optind;
640 argv += optind;
642 #ifndef PROFILE
643 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 "unveil",
645 NULL) == -1)
646 err(1, "pledge");
647 #endif
648 if (argc != 1)
649 usage_import();
651 if (repo_path == NULL) {
652 repo_path = getcwd(NULL, 0);
653 if (repo_path == NULL)
654 return got_error_from_errno("getcwd");
656 got_path_strip_trailing_slashes(repo_path);
657 error = get_gitconfig_path(&gitconfig_path);
658 if (error)
659 goto done;
660 error = got_repo_open(&repo, repo_path, gitconfig_path);
661 if (error)
662 goto done;
664 error = get_author(&author, repo);
665 if (error)
666 return error;
668 /*
669 * Don't let the user create a branch name with a leading '-'.
670 * While technically a valid reference name, this case is usually
671 * an unintended typo.
672 */
673 if (branch_name[0] == '-')
674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 error = got_error_from_errno("asprintf");
678 goto done;
681 error = got_ref_open(&branch_ref, repo, refname, 0);
682 if (error) {
683 if (error->code != GOT_ERR_NOT_REF)
684 goto done;
685 } else {
686 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 "import target branch already exists");
688 goto done;
691 path_dir = realpath(argv[0], NULL);
692 if (path_dir == NULL) {
693 error = got_error_from_errno2("realpath", argv[0]);
694 goto done;
696 got_path_strip_trailing_slashes(path_dir);
698 /*
699 * unveil(2) traverses exec(2); if an editor is used we have
700 * to apply unveil after the log message has been written.
701 */
702 if (logmsg == NULL || strlen(logmsg) == 0) {
703 error = get_editor(&editor);
704 if (error)
705 goto done;
706 free(logmsg);
707 error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 path_dir, refname);
709 if (error) {
710 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 logmsg_path != NULL)
712 preserve_logmsg = 1;
713 goto done;
717 if (unveil(path_dir, "r") != 0) {
718 error = got_error_from_errno2("unveil", path_dir);
719 if (logmsg_path)
720 preserve_logmsg = 1;
721 goto done;
724 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 if (error) {
726 if (logmsg_path)
727 preserve_logmsg = 1;
728 goto done;
731 error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 author, &ignores, repo, import_progress, NULL);
733 if (error) {
734 if (logmsg_path)
735 preserve_logmsg = 1;
736 goto done;
739 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 if (error) {
741 if (logmsg_path)
742 preserve_logmsg = 1;
743 goto done;
746 error = got_ref_write(branch_ref, repo);
747 if (error) {
748 if (logmsg_path)
749 preserve_logmsg = 1;
750 goto done;
753 error = got_object_id_str(&id_str, new_commit_id);
754 if (error) {
755 if (logmsg_path)
756 preserve_logmsg = 1;
757 goto done;
760 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 if (error) {
762 if (error->code != GOT_ERR_NOT_REF) {
763 if (logmsg_path)
764 preserve_logmsg = 1;
765 goto done;
768 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 branch_ref);
770 if (error) {
771 if (logmsg_path)
772 preserve_logmsg = 1;
773 goto done;
776 error = got_ref_write(head_ref, repo);
777 if (error) {
778 if (logmsg_path)
779 preserve_logmsg = 1;
780 goto done;
784 printf("Created branch %s with commit %s\n",
785 got_ref_get_name(branch_ref), id_str);
786 done:
787 if (preserve_logmsg) {
788 fprintf(stderr, "%s: log message preserved in %s\n",
789 getprogname(), logmsg_path);
790 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 error = got_error_from_errno2("unlink", logmsg_path);
792 free(logmsg);
793 free(logmsg_path);
794 free(repo_path);
795 free(editor);
796 free(refname);
797 free(new_commit_id);
798 free(id_str);
799 free(author);
800 free(gitconfig_path);
801 if (branch_ref)
802 got_ref_close(branch_ref);
803 if (head_ref)
804 got_ref_close(head_ref);
805 return error;
808 __dead static void
809 usage_clone(void)
811 fprintf(stderr, "usage: %s clone repo-url [target-directory]\n",
812 getprogname());
813 exit(1);
816 __dead static void
817 usage_checkout(void)
819 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
820 "[-p prefix] repository-path [worktree-path]\n", getprogname());
821 exit(1);
824 static void
825 show_worktree_base_ref_warning(void)
827 fprintf(stderr, "%s: warning: could not create a reference "
828 "to the work tree's base commit; the commit could be "
829 "garbage-collected by Git; making the repository "
830 "writable and running 'got update' will prevent this\n",
831 getprogname());
834 struct got_checkout_progress_arg {
835 const char *worktree_path;
836 int had_base_commit_ref_error;
837 };
839 static const struct got_error *
840 checkout_progress(void *arg, unsigned char status, const char *path)
842 struct got_checkout_progress_arg *a = arg;
844 /* Base commit bump happens silently. */
845 if (status == GOT_STATUS_BUMP_BASE)
846 return NULL;
848 if (status == GOT_STATUS_BASE_REF_ERR) {
849 a->had_base_commit_ref_error = 1;
850 return NULL;
853 while (path[0] == '/')
854 path++;
856 printf("%c %s/%s\n", status, a->worktree_path, path);
857 return NULL;
860 static const struct got_error *
861 check_cancelled(void *arg)
863 if (sigint_received || sigpipe_received)
864 return got_error(GOT_ERR_CANCELLED);
865 return NULL;
868 static const struct got_error *
869 check_linear_ancestry(struct got_object_id *commit_id,
870 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
871 struct got_repository *repo)
873 const struct got_error *err = NULL;
874 struct got_object_id *yca_id;
876 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
877 commit_id, base_commit_id, repo, check_cancelled, NULL);
878 if (err)
879 return err;
881 if (yca_id == NULL)
882 return got_error(GOT_ERR_ANCESTRY);
884 /*
885 * Require a straight line of history between the target commit
886 * and the work tree's base commit.
888 * Non-linear situations such as this require a rebase:
890 * (commit) D F (base_commit)
891 * \ /
892 * C E
893 * \ /
894 * B (yca)
895 * |
896 * A
898 * 'got update' only handles linear cases:
899 * Update forwards in time: A (base/yca) - B - C - D (commit)
900 * Update backwards in time: D (base) - C - B - A (commit/yca)
901 */
902 if (allow_forwards_in_time_only) {
903 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
904 return got_error(GOT_ERR_ANCESTRY);
905 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
906 got_object_id_cmp(base_commit_id, yca_id) != 0)
907 return got_error(GOT_ERR_ANCESTRY);
909 free(yca_id);
910 return NULL;
913 static const struct got_error *
914 check_same_branch(struct got_object_id *commit_id,
915 struct got_reference *head_ref, struct got_object_id *yca_id,
916 struct got_repository *repo)
918 const struct got_error *err = NULL;
919 struct got_commit_graph *graph = NULL;
920 struct got_object_id *head_commit_id = NULL;
921 int is_same_branch = 0;
923 err = got_ref_resolve(&head_commit_id, repo, head_ref);
924 if (err)
925 goto done;
927 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
928 is_same_branch = 1;
929 goto done;
931 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
932 is_same_branch = 1;
933 goto done;
936 err = got_commit_graph_open(&graph, "/", 1);
937 if (err)
938 goto done;
940 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
941 check_cancelled, NULL);
942 if (err)
943 goto done;
945 for (;;) {
946 struct got_object_id *id;
947 err = got_commit_graph_iter_next(&id, graph, repo,
948 check_cancelled, NULL);
949 if (err) {
950 if (err->code == GOT_ERR_ITER_COMPLETED)
951 err = NULL;
952 break;
955 if (id) {
956 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
957 break;
958 if (got_object_id_cmp(id, commit_id) == 0) {
959 is_same_branch = 1;
960 break;
964 done:
965 if (graph)
966 got_commit_graph_close(graph);
967 free(head_commit_id);
968 if (!err && !is_same_branch)
969 err = got_error(GOT_ERR_ANCESTRY);
970 return err;
973 struct got_fetch_progress_arg {
974 char last_scaled_size[FMT_SCALED_STRSIZE];
975 int last_p_indexed;
976 int last_p_resolved;
977 };
979 static const struct got_error *
980 fetch_progress(void *arg, const char *message, off_t packfile_size,
981 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
983 struct got_fetch_progress_arg *a = arg;
984 char scaled_size[FMT_SCALED_STRSIZE];
985 int p_indexed, p_resolved;
986 int print_size = 0, print_indexed = 0, print_resolved = 0;
988 if (message && message[0] != '\0' && message[0] != '\n') {
989 printf("\rserver: %s", message);
990 if (strchr(message, '\n') == 0)
991 printf("\n");
992 fflush(stdout);
995 if (packfile_size > 0 || nobj_indexed > 0) {
996 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
997 (a->last_scaled_size[0] == '\0' ||
998 strcmp(scaled_size, a->last_scaled_size)) != 0) {
999 print_size = 1;
1000 if (strlcpy(a->last_scaled_size, scaled_size,
1001 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1002 return got_error(GOT_ERR_NO_SPACE);
1004 if (nobj_indexed > 0) {
1005 p_indexed = (nobj_indexed * 100) / nobj_total;
1006 if (p_indexed != a->last_p_indexed) {
1007 a->last_p_indexed = p_indexed;
1008 print_indexed = 1;
1009 print_size = 1;
1012 if (nobj_resolved > 0) {
1013 p_resolved = (nobj_resolved * 100) /
1014 (nobj_total - nobj_loose);
1015 if (p_resolved != a->last_p_resolved) {
1016 a->last_p_resolved = p_resolved;
1017 print_resolved = 1;
1018 print_indexed = 1;
1019 print_size = 1;
1024 if (print_size || print_indexed || print_resolved)
1025 printf("\r");
1026 if (print_size)
1027 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1028 if (print_indexed)
1029 printf("; indexing %d%%", p_indexed);
1030 if (print_resolved)
1031 printf("; resolving deltas %d%%", p_resolved);
1032 if (print_size || print_indexed || print_resolved)
1033 fflush(stdout);
1035 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
1036 nobj_resolved == nobj_total - nobj_loose)
1037 printf("\nWriting pack index...\n");
1039 return NULL;
1042 static const struct got_error *
1043 cmd_clone(int argc, char *argv[])
1045 const struct got_error *error = NULL;
1046 const char *uri, *dirname;
1047 char *proto, *host, *port, *repo_name, *server_path;
1048 char *default_destdir = NULL, *id_str = NULL;
1049 const char *repo_path;
1050 struct got_repository *repo = NULL;
1051 struct got_pathlist_head refs, symrefs;
1052 struct got_pathlist_entry *pe;
1053 struct got_object_id *pack_hash = NULL;
1054 int ch, fetchfd = -1;
1055 struct got_fetch_progress_arg fpa;
1057 TAILQ_INIT(&refs);
1058 TAILQ_INIT(&symrefs);
1060 while ((ch = getopt(argc, argv, "")) != -1) {
1061 switch (ch) {
1062 default:
1063 usage_clone();
1064 break;
1067 argc -= optind;
1068 argv += optind;
1070 uri = argv[0];
1072 if (argc == 1)
1073 dirname = NULL;
1074 else if (argc == 2)
1075 dirname = argv[1];
1076 else
1077 usage_clone();
1079 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1080 &repo_name, argv[0]);
1081 if (error)
1082 goto done;
1084 #ifndef PROFILE
1085 if (strcmp(proto, "git") == 0) {
1086 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1087 "sendfd dns inet unveil", NULL) == -1)
1088 err(1, "pledge");
1089 } else if (strcmp(proto, "git+ssh") == 0 ||
1090 strcmp(proto, "ssh") == 0) {
1091 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1092 "sendfd unveil", NULL) == -1)
1093 err(1, "pledge");
1094 } else if (strcmp(proto, "http") == 0 ||
1095 strcmp(proto, "git+http") == 0) {
1096 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1097 goto done;
1098 } else {
1099 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1100 goto done;
1102 #endif
1103 if (dirname == NULL) {
1104 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1105 error = got_error_from_errno("asprintf");
1106 goto done;
1108 repo_path = default_destdir;
1109 } else
1110 repo_path = dirname;
1112 error = got_path_mkdir(repo_path);
1113 if (error)
1114 goto done;
1116 error = got_repo_init(repo_path);
1117 if (error)
1118 goto done;
1120 error = got_repo_open(&repo, repo_path, NULL);
1121 if (error)
1122 goto done;
1124 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1125 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1126 error = got_error_from_errno2("unveil",
1127 GOT_FETCH_PATH_SSH);
1128 goto done;
1131 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1132 if (error)
1133 goto done;
1135 error = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1136 if (error)
1137 goto done;
1139 printf("Connected to %s:%s\n", host, port);
1141 fpa.last_scaled_size[0] = '\0';
1142 fpa.last_p_indexed = -1;
1143 fpa.last_p_resolved = -1;
1144 error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1145 repo, fetch_progress, &fpa);
1146 if (error)
1147 goto done;
1149 error = got_object_id_str(&id_str, pack_hash);
1150 if (error)
1151 goto done;
1152 printf("Fetched %s.pack\n", id_str);
1153 free(id_str);
1155 /* Set up references provided with the pack file. */
1156 TAILQ_FOREACH(pe, &refs, entry) {
1157 const char *refname = pe->path;
1158 struct got_object_id *id = pe->data;
1159 struct got_reference *ref;
1162 error = got_ref_alloc(&ref, refname, id);
1163 if (error)
1164 goto done;
1166 #if 0
1167 error = got_object_id_str(&id_str, id);
1168 if (error)
1169 goto done;
1170 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1171 free(id_str);
1172 #endif
1173 error = got_ref_write(ref, repo);
1174 got_ref_close(ref);
1175 if (error)
1176 goto done;
1179 /* Set the HEAD reference if the server provided one. */
1180 TAILQ_FOREACH(pe, &symrefs, entry) {
1181 struct got_reference *symref, *target_ref;
1182 const char *refname = pe->path;
1183 const char *target = pe->data;
1185 if (strcmp(refname, GOT_REF_HEAD) != 0)
1186 continue;
1188 error = got_ref_open(&target_ref, repo, target, 0);
1189 if (error) {
1190 if (error->code == GOT_ERR_NOT_REF)
1191 continue;
1192 goto done;
1195 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1196 got_ref_close(target_ref);
1197 if (error)
1198 goto done;
1200 printf("Setting %s to %s\n", GOT_REF_HEAD,
1201 got_ref_get_symref_target(symref));
1203 error = got_ref_write(symref, repo);
1204 got_ref_close(symref);
1205 break;
1208 done:
1209 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1210 error = got_error_from_errno("close");
1211 if (repo)
1212 got_repo_close(repo);
1213 TAILQ_FOREACH(pe, &refs, entry) {
1214 free((void *)pe->path);
1215 free(pe->data);
1217 got_pathlist_free(&refs);
1218 TAILQ_FOREACH(pe, &symrefs, entry) {
1219 free((void *)pe->path);
1220 free(pe->data);
1222 got_pathlist_free(&symrefs);
1223 free(pack_hash);
1224 free(proto);
1225 free(host);
1226 free(port);
1227 free(server_path);
1228 free(repo_name);
1229 free(default_destdir);
1230 return error;
1233 static const struct got_error *
1234 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1236 static char msg[512];
1237 const char *branch_name;
1239 if (got_ref_is_symbolic(ref))
1240 branch_name = got_ref_get_symref_target(ref);
1241 else
1242 branch_name = got_ref_get_name(ref);
1244 if (strncmp("refs/heads/", branch_name, 11) == 0)
1245 branch_name += 11;
1247 snprintf(msg, sizeof(msg),
1248 "target commit is not contained in branch '%s'; "
1249 "the branch to use must be specified with -b; "
1250 "if necessary a new branch can be created for "
1251 "this commit with 'got branch -c %s BRANCH_NAME'",
1252 branch_name, commit_id_str);
1254 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1257 static const struct got_error *
1258 cmd_checkout(int argc, char *argv[])
1260 const struct got_error *error = NULL;
1261 struct got_repository *repo = NULL;
1262 struct got_reference *head_ref = NULL;
1263 struct got_worktree *worktree = NULL;
1264 char *repo_path = NULL;
1265 char *worktree_path = NULL;
1266 const char *path_prefix = "";
1267 const char *branch_name = GOT_REF_HEAD;
1268 char *commit_id_str = NULL;
1269 int ch, same_path_prefix, allow_nonempty = 0;
1270 struct got_pathlist_head paths;
1271 struct got_checkout_progress_arg cpa;
1273 TAILQ_INIT(&paths);
1275 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1276 switch (ch) {
1277 case 'b':
1278 branch_name = optarg;
1279 break;
1280 case 'c':
1281 commit_id_str = strdup(optarg);
1282 if (commit_id_str == NULL)
1283 return got_error_from_errno("strdup");
1284 break;
1285 case 'E':
1286 allow_nonempty = 1;
1287 break;
1288 case 'p':
1289 path_prefix = optarg;
1290 break;
1291 default:
1292 usage_checkout();
1293 /* NOTREACHED */
1297 argc -= optind;
1298 argv += optind;
1300 #ifndef PROFILE
1301 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1302 "unveil", NULL) == -1)
1303 err(1, "pledge");
1304 #endif
1305 if (argc == 1) {
1306 char *cwd, *base, *dotgit;
1307 repo_path = realpath(argv[0], NULL);
1308 if (repo_path == NULL)
1309 return got_error_from_errno2("realpath", argv[0]);
1310 cwd = getcwd(NULL, 0);
1311 if (cwd == NULL) {
1312 error = got_error_from_errno("getcwd");
1313 goto done;
1315 if (path_prefix[0]) {
1316 base = basename(path_prefix);
1317 if (base == NULL) {
1318 error = got_error_from_errno2("basename",
1319 path_prefix);
1320 goto done;
1322 } else {
1323 base = basename(repo_path);
1324 if (base == NULL) {
1325 error = got_error_from_errno2("basename",
1326 repo_path);
1327 goto done;
1330 dotgit = strstr(base, ".git");
1331 if (dotgit)
1332 *dotgit = '\0';
1333 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1334 error = got_error_from_errno("asprintf");
1335 free(cwd);
1336 goto done;
1338 free(cwd);
1339 } else if (argc == 2) {
1340 repo_path = realpath(argv[0], NULL);
1341 if (repo_path == NULL) {
1342 error = got_error_from_errno2("realpath", argv[0]);
1343 goto done;
1345 worktree_path = realpath(argv[1], NULL);
1346 if (worktree_path == NULL) {
1347 if (errno != ENOENT) {
1348 error = got_error_from_errno2("realpath",
1349 argv[1]);
1350 goto done;
1352 worktree_path = strdup(argv[1]);
1353 if (worktree_path == NULL) {
1354 error = got_error_from_errno("strdup");
1355 goto done;
1358 } else
1359 usage_checkout();
1361 got_path_strip_trailing_slashes(repo_path);
1362 got_path_strip_trailing_slashes(worktree_path);
1364 error = got_repo_open(&repo, repo_path, NULL);
1365 if (error != NULL)
1366 goto done;
1368 /* Pre-create work tree path for unveil(2) */
1369 error = got_path_mkdir(worktree_path);
1370 if (error) {
1371 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1372 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1373 goto done;
1374 if (!allow_nonempty &&
1375 !got_path_dir_is_empty(worktree_path)) {
1376 error = got_error_path(worktree_path,
1377 GOT_ERR_DIR_NOT_EMPTY);
1378 goto done;
1382 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1383 if (error)
1384 goto done;
1386 error = got_ref_open(&head_ref, repo, branch_name, 0);
1387 if (error != NULL)
1388 goto done;
1390 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1391 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1392 goto done;
1394 error = got_worktree_open(&worktree, worktree_path);
1395 if (error != NULL)
1396 goto done;
1398 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1399 path_prefix);
1400 if (error != NULL)
1401 goto done;
1402 if (!same_path_prefix) {
1403 error = got_error(GOT_ERR_PATH_PREFIX);
1404 goto done;
1407 if (commit_id_str) {
1408 struct got_object_id *commit_id;
1409 error = got_repo_match_object_id(&commit_id, NULL,
1410 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1411 if (error)
1412 goto done;
1413 error = check_linear_ancestry(commit_id,
1414 got_worktree_get_base_commit_id(worktree), 0, repo);
1415 if (error != NULL) {
1416 free(commit_id);
1417 if (error->code == GOT_ERR_ANCESTRY) {
1418 error = checkout_ancestry_error(
1419 head_ref, commit_id_str);
1421 goto done;
1423 error = check_same_branch(commit_id, head_ref, NULL, repo);
1424 if (error) {
1425 if (error->code == GOT_ERR_ANCESTRY) {
1426 error = checkout_ancestry_error(
1427 head_ref, commit_id_str);
1429 goto done;
1431 error = got_worktree_set_base_commit_id(worktree, repo,
1432 commit_id);
1433 free(commit_id);
1434 if (error)
1435 goto done;
1438 error = got_pathlist_append(&paths, "", NULL);
1439 if (error)
1440 goto done;
1441 cpa.worktree_path = worktree_path;
1442 cpa.had_base_commit_ref_error = 0;
1443 error = got_worktree_checkout_files(worktree, &paths, repo,
1444 checkout_progress, &cpa, check_cancelled, NULL);
1445 if (error != NULL)
1446 goto done;
1448 printf("Now shut up and hack\n");
1449 if (cpa.had_base_commit_ref_error)
1450 show_worktree_base_ref_warning();
1451 done:
1452 got_pathlist_free(&paths);
1453 free(commit_id_str);
1454 free(repo_path);
1455 free(worktree_path);
1456 return error;
1459 __dead static void
1460 usage_update(void)
1462 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1463 getprogname());
1464 exit(1);
1467 static const struct got_error *
1468 update_progress(void *arg, unsigned char status, const char *path)
1470 int *did_something = arg;
1472 if (status == GOT_STATUS_EXISTS ||
1473 status == GOT_STATUS_BASE_REF_ERR)
1474 return NULL;
1476 *did_something = 1;
1478 /* Base commit bump happens silently. */
1479 if (status == GOT_STATUS_BUMP_BASE)
1480 return NULL;
1482 while (path[0] == '/')
1483 path++;
1484 printf("%c %s\n", status, path);
1485 return NULL;
1488 static const struct got_error *
1489 switch_head_ref(struct got_reference *head_ref,
1490 struct got_object_id *commit_id, struct got_worktree *worktree,
1491 struct got_repository *repo)
1493 const struct got_error *err = NULL;
1494 char *base_id_str;
1495 int ref_has_moved = 0;
1497 /* Trivial case: switching between two different references. */
1498 if (strcmp(got_ref_get_name(head_ref),
1499 got_worktree_get_head_ref_name(worktree)) != 0) {
1500 printf("Switching work tree from %s to %s\n",
1501 got_worktree_get_head_ref_name(worktree),
1502 got_ref_get_name(head_ref));
1503 return got_worktree_set_head_ref(worktree, head_ref);
1506 err = check_linear_ancestry(commit_id,
1507 got_worktree_get_base_commit_id(worktree), 0, repo);
1508 if (err) {
1509 if (err->code != GOT_ERR_ANCESTRY)
1510 return err;
1511 ref_has_moved = 1;
1513 if (!ref_has_moved)
1514 return NULL;
1516 /* Switching to a rebased branch with the same reference name. */
1517 err = got_object_id_str(&base_id_str,
1518 got_worktree_get_base_commit_id(worktree));
1519 if (err)
1520 return err;
1521 printf("Reference %s now points at a different branch\n",
1522 got_worktree_get_head_ref_name(worktree));
1523 printf("Switching work tree from %s to %s\n", base_id_str,
1524 got_worktree_get_head_ref_name(worktree));
1525 return NULL;
1528 static const struct got_error *
1529 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1531 const struct got_error *err;
1532 int in_progress;
1534 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1535 if (err)
1536 return err;
1537 if (in_progress)
1538 return got_error(GOT_ERR_REBASING);
1540 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1541 if (err)
1542 return err;
1543 if (in_progress)
1544 return got_error(GOT_ERR_HISTEDIT_BUSY);
1546 return NULL;
1549 static const struct got_error *
1550 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1551 char *argv[], struct got_worktree *worktree)
1553 const struct got_error *err = NULL;
1554 char *path;
1555 int i;
1557 if (argc == 0) {
1558 path = strdup("");
1559 if (path == NULL)
1560 return got_error_from_errno("strdup");
1561 return got_pathlist_append(paths, path, NULL);
1564 for (i = 0; i < argc; i++) {
1565 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1566 if (err)
1567 break;
1568 err = got_pathlist_append(paths, path, NULL);
1569 if (err) {
1570 free(path);
1571 break;
1575 return err;
1578 static const struct got_error *
1579 cmd_update(int argc, char *argv[])
1581 const struct got_error *error = NULL;
1582 struct got_repository *repo = NULL;
1583 struct got_worktree *worktree = NULL;
1584 char *worktree_path = NULL;
1585 struct got_object_id *commit_id = NULL;
1586 char *commit_id_str = NULL;
1587 const char *branch_name = NULL;
1588 struct got_reference *head_ref = NULL;
1589 struct got_pathlist_head paths;
1590 struct got_pathlist_entry *pe;
1591 int ch, did_something = 0;
1593 TAILQ_INIT(&paths);
1595 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1596 switch (ch) {
1597 case 'b':
1598 branch_name = optarg;
1599 break;
1600 case 'c':
1601 commit_id_str = strdup(optarg);
1602 if (commit_id_str == NULL)
1603 return got_error_from_errno("strdup");
1604 break;
1605 default:
1606 usage_update();
1607 /* NOTREACHED */
1611 argc -= optind;
1612 argv += optind;
1614 #ifndef PROFILE
1615 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1616 "unveil", NULL) == -1)
1617 err(1, "pledge");
1618 #endif
1619 worktree_path = getcwd(NULL, 0);
1620 if (worktree_path == NULL) {
1621 error = got_error_from_errno("getcwd");
1622 goto done;
1624 error = got_worktree_open(&worktree, worktree_path);
1625 if (error)
1626 goto done;
1628 error = check_rebase_or_histedit_in_progress(worktree);
1629 if (error)
1630 goto done;
1632 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1633 NULL);
1634 if (error != NULL)
1635 goto done;
1637 error = apply_unveil(got_repo_get_path(repo), 0,
1638 got_worktree_get_root_path(worktree));
1639 if (error)
1640 goto done;
1642 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1643 if (error)
1644 goto done;
1646 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1647 got_worktree_get_head_ref_name(worktree), 0);
1648 if (error != NULL)
1649 goto done;
1650 if (commit_id_str == NULL) {
1651 error = got_ref_resolve(&commit_id, repo, head_ref);
1652 if (error != NULL)
1653 goto done;
1654 error = got_object_id_str(&commit_id_str, commit_id);
1655 if (error != NULL)
1656 goto done;
1657 } else {
1658 error = got_repo_match_object_id(&commit_id, NULL,
1659 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1660 free(commit_id_str);
1661 commit_id_str = NULL;
1662 if (error)
1663 goto done;
1664 error = got_object_id_str(&commit_id_str, commit_id);
1665 if (error)
1666 goto done;
1669 if (branch_name) {
1670 struct got_object_id *head_commit_id;
1671 TAILQ_FOREACH(pe, &paths, entry) {
1672 if (pe->path_len == 0)
1673 continue;
1674 error = got_error_msg(GOT_ERR_BAD_PATH,
1675 "switching between branches requires that "
1676 "the entire work tree gets updated");
1677 goto done;
1679 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1680 if (error)
1681 goto done;
1682 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1683 repo);
1684 free(head_commit_id);
1685 if (error != NULL)
1686 goto done;
1687 error = check_same_branch(commit_id, head_ref, NULL, repo);
1688 if (error)
1689 goto done;
1690 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1691 if (error)
1692 goto done;
1693 } else {
1694 error = check_linear_ancestry(commit_id,
1695 got_worktree_get_base_commit_id(worktree), 0, repo);
1696 if (error != NULL) {
1697 if (error->code == GOT_ERR_ANCESTRY)
1698 error = got_error(GOT_ERR_BRANCH_MOVED);
1699 goto done;
1701 error = check_same_branch(commit_id, head_ref, NULL, repo);
1702 if (error)
1703 goto done;
1706 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1707 commit_id) != 0) {
1708 error = got_worktree_set_base_commit_id(worktree, repo,
1709 commit_id);
1710 if (error)
1711 goto done;
1714 error = got_worktree_checkout_files(worktree, &paths, repo,
1715 update_progress, &did_something, check_cancelled, NULL);
1716 if (error != NULL)
1717 goto done;
1719 if (did_something)
1720 printf("Updated to commit %s\n", commit_id_str);
1721 else
1722 printf("Already up-to-date\n");
1723 done:
1724 free(worktree_path);
1725 TAILQ_FOREACH(pe, &paths, entry)
1726 free((char *)pe->path);
1727 got_pathlist_free(&paths);
1728 free(commit_id);
1729 free(commit_id_str);
1730 return error;
1733 static const struct got_error *
1734 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1735 const char *path, int diff_context, int ignore_whitespace,
1736 struct got_repository *repo)
1738 const struct got_error *err = NULL;
1739 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1741 if (blob_id1) {
1742 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1743 if (err)
1744 goto done;
1747 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1748 if (err)
1749 goto done;
1751 while (path[0] == '/')
1752 path++;
1753 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1754 ignore_whitespace, stdout);
1755 done:
1756 if (blob1)
1757 got_object_blob_close(blob1);
1758 got_object_blob_close(blob2);
1759 return err;
1762 static const struct got_error *
1763 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1764 const char *path, int diff_context, int ignore_whitespace,
1765 struct got_repository *repo)
1767 const struct got_error *err = NULL;
1768 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1769 struct got_diff_blob_output_unidiff_arg arg;
1771 if (tree_id1) {
1772 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1773 if (err)
1774 goto done;
1777 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1778 if (err)
1779 goto done;
1781 arg.diff_context = diff_context;
1782 arg.ignore_whitespace = ignore_whitespace;
1783 arg.outfile = stdout;
1784 while (path[0] == '/')
1785 path++;
1786 err = got_diff_tree(tree1, tree2, path, path, repo,
1787 got_diff_blob_output_unidiff, &arg, 1);
1788 done:
1789 if (tree1)
1790 got_object_tree_close(tree1);
1791 if (tree2)
1792 got_object_tree_close(tree2);
1793 return err;
1796 static const struct got_error *
1797 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1798 const char *path, int diff_context, struct got_repository *repo)
1800 const struct got_error *err = NULL;
1801 struct got_commit_object *pcommit = NULL;
1802 char *id_str1 = NULL, *id_str2 = NULL;
1803 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1804 struct got_object_qid *qid;
1806 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1807 if (qid != NULL) {
1808 err = got_object_open_as_commit(&pcommit, repo,
1809 qid->id);
1810 if (err)
1811 return err;
1814 if (path && path[0] != '\0') {
1815 int obj_type;
1816 err = got_object_id_by_path(&obj_id2, repo, id, path);
1817 if (err)
1818 goto done;
1819 err = got_object_id_str(&id_str2, obj_id2);
1820 if (err) {
1821 free(obj_id2);
1822 goto done;
1824 if (pcommit) {
1825 err = got_object_id_by_path(&obj_id1, repo,
1826 qid->id, path);
1827 if (err) {
1828 free(obj_id2);
1829 goto done;
1831 err = got_object_id_str(&id_str1, obj_id1);
1832 if (err) {
1833 free(obj_id2);
1834 goto done;
1837 err = got_object_get_type(&obj_type, repo, obj_id2);
1838 if (err) {
1839 free(obj_id2);
1840 goto done;
1842 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1843 switch (obj_type) {
1844 case GOT_OBJ_TYPE_BLOB:
1845 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1846 0, repo);
1847 break;
1848 case GOT_OBJ_TYPE_TREE:
1849 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1850 0, repo);
1851 break;
1852 default:
1853 err = got_error(GOT_ERR_OBJ_TYPE);
1854 break;
1856 free(obj_id1);
1857 free(obj_id2);
1858 } else {
1859 obj_id2 = got_object_commit_get_tree_id(commit);
1860 err = got_object_id_str(&id_str2, obj_id2);
1861 if (err)
1862 goto done;
1863 obj_id1 = got_object_commit_get_tree_id(pcommit);
1864 err = got_object_id_str(&id_str1, obj_id1);
1865 if (err)
1866 goto done;
1867 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1868 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1870 done:
1871 free(id_str1);
1872 free(id_str2);
1873 if (pcommit)
1874 got_object_commit_close(pcommit);
1875 return err;
1878 static char *
1879 get_datestr(time_t *time, char *datebuf)
1881 struct tm mytm, *tm;
1882 char *p, *s;
1884 tm = gmtime_r(time, &mytm);
1885 if (tm == NULL)
1886 return NULL;
1887 s = asctime_r(tm, datebuf);
1888 if (s == NULL)
1889 return NULL;
1890 p = strchr(s, '\n');
1891 if (p)
1892 *p = '\0';
1893 return s;
1896 static const struct got_error *
1897 match_logmsg(int *have_match, struct got_object_id *id,
1898 struct got_commit_object *commit, regex_t *regex)
1900 const struct got_error *err = NULL;
1901 regmatch_t regmatch;
1902 char *id_str = NULL, *logmsg = NULL;
1904 *have_match = 0;
1906 err = got_object_id_str(&id_str, id);
1907 if (err)
1908 return err;
1910 err = got_object_commit_get_logmsg(&logmsg, commit);
1911 if (err)
1912 goto done;
1914 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1915 *have_match = 1;
1916 done:
1917 free(id_str);
1918 free(logmsg);
1919 return err;
1922 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1924 static const struct got_error *
1925 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1926 struct got_repository *repo, const char *path, int show_patch,
1927 int diff_context, struct got_reflist_head *refs)
1929 const struct got_error *err = NULL;
1930 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1931 char datebuf[26];
1932 time_t committer_time;
1933 const char *author, *committer;
1934 char *refs_str = NULL;
1935 struct got_reflist_entry *re;
1937 SIMPLEQ_FOREACH(re, refs, entry) {
1938 char *s;
1939 const char *name;
1940 struct got_tag_object *tag = NULL;
1941 int cmp;
1943 name = got_ref_get_name(re->ref);
1944 if (strcmp(name, GOT_REF_HEAD) == 0)
1945 continue;
1946 if (strncmp(name, "refs/", 5) == 0)
1947 name += 5;
1948 if (strncmp(name, "got/", 4) == 0)
1949 continue;
1950 if (strncmp(name, "heads/", 6) == 0)
1951 name += 6;
1952 if (strncmp(name, "remotes/", 8) == 0)
1953 name += 8;
1954 if (strncmp(name, "tags/", 5) == 0) {
1955 err = got_object_open_as_tag(&tag, repo, re->id);
1956 if (err) {
1957 if (err->code != GOT_ERR_OBJ_TYPE)
1958 return err;
1959 /* Ref points at something other than a tag. */
1960 err = NULL;
1961 tag = NULL;
1964 cmp = got_object_id_cmp(tag ?
1965 got_object_tag_get_object_id(tag) : re->id, id);
1966 if (tag)
1967 got_object_tag_close(tag);
1968 if (cmp != 0)
1969 continue;
1970 s = refs_str;
1971 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1972 name) == -1) {
1973 err = got_error_from_errno("asprintf");
1974 free(s);
1975 return err;
1977 free(s);
1979 err = got_object_id_str(&id_str, id);
1980 if (err)
1981 return err;
1983 printf(GOT_COMMIT_SEP_STR);
1984 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1985 refs_str ? refs_str : "", refs_str ? ")" : "");
1986 free(id_str);
1987 id_str = NULL;
1988 free(refs_str);
1989 refs_str = NULL;
1990 printf("from: %s\n", got_object_commit_get_author(commit));
1991 committer_time = got_object_commit_get_committer_time(commit);
1992 datestr = get_datestr(&committer_time, datebuf);
1993 if (datestr)
1994 printf("date: %s UTC\n", datestr);
1995 author = got_object_commit_get_author(commit);
1996 committer = got_object_commit_get_committer(commit);
1997 if (strcmp(author, committer) != 0)
1998 printf("via: %s\n", committer);
1999 if (got_object_commit_get_nparents(commit) > 1) {
2000 const struct got_object_id_queue *parent_ids;
2001 struct got_object_qid *qid;
2002 int n = 1;
2003 parent_ids = got_object_commit_get_parent_ids(commit);
2004 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2005 err = got_object_id_str(&id_str, qid->id);
2006 if (err)
2007 return err;
2008 printf("parent %d: %s\n", n++, id_str);
2009 free(id_str);
2013 err = got_object_commit_get_logmsg(&logmsg0, commit);
2014 if (err)
2015 return err;
2017 logmsg = logmsg0;
2018 do {
2019 line = strsep(&logmsg, "\n");
2020 if (line)
2021 printf(" %s\n", line);
2022 } while (line);
2023 free(logmsg0);
2025 if (show_patch) {
2026 err = print_patch(commit, id, path, diff_context, repo);
2027 if (err == 0)
2028 printf("\n");
2031 if (fflush(stdout) != 0 && err == NULL)
2032 err = got_error_from_errno("fflush");
2033 return err;
2036 static const struct got_error *
2037 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2038 const char *path, int show_patch, const char *search_pattern,
2039 int diff_context, int limit, int log_branches,
2040 struct got_reflist_head *refs)
2042 const struct got_error *err;
2043 struct got_commit_graph *graph;
2044 regex_t regex;
2045 int have_match;
2047 if (search_pattern &&
2048 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2049 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2051 err = got_commit_graph_open(&graph, path, !log_branches);
2052 if (err)
2053 return err;
2054 err = got_commit_graph_iter_start(graph, root_id, repo,
2055 check_cancelled, NULL);
2056 if (err)
2057 goto done;
2058 for (;;) {
2059 struct got_commit_object *commit;
2060 struct got_object_id *id;
2062 if (sigint_received || sigpipe_received)
2063 break;
2065 err = got_commit_graph_iter_next(&id, graph, repo,
2066 check_cancelled, NULL);
2067 if (err) {
2068 if (err->code == GOT_ERR_ITER_COMPLETED)
2069 err = NULL;
2070 break;
2072 if (id == NULL)
2073 break;
2075 err = got_object_open_as_commit(&commit, repo, id);
2076 if (err)
2077 break;
2079 if (search_pattern) {
2080 err = match_logmsg(&have_match, id, commit, &regex);
2081 if (err) {
2082 got_object_commit_close(commit);
2083 break;
2085 if (have_match == 0) {
2086 got_object_commit_close(commit);
2087 continue;
2091 err = print_commit(commit, id, repo, path, show_patch,
2092 diff_context, refs);
2093 got_object_commit_close(commit);
2094 if (err || (limit && --limit == 0))
2095 break;
2097 done:
2098 if (search_pattern)
2099 regfree(&regex);
2100 got_commit_graph_close(graph);
2101 return err;
2104 __dead static void
2105 usage_log(void)
2107 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2108 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2109 exit(1);
2112 static int
2113 get_default_log_limit(void)
2115 const char *got_default_log_limit;
2116 long long n;
2117 const char *errstr;
2119 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2120 if (got_default_log_limit == NULL)
2121 return 0;
2122 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2123 if (errstr != NULL)
2124 return 0;
2125 return n;
2128 static const struct got_error *
2129 cmd_log(int argc, char *argv[])
2131 const struct got_error *error;
2132 struct got_repository *repo = NULL;
2133 struct got_worktree *worktree = NULL;
2134 struct got_commit_object *commit = NULL;
2135 struct got_object_id *id = NULL;
2136 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2137 const char *start_commit = NULL, *search_pattern = NULL;
2138 int diff_context = -1, ch;
2139 int show_patch = 0, limit = 0, log_branches = 0;
2140 const char *errstr;
2141 struct got_reflist_head refs;
2143 SIMPLEQ_INIT(&refs);
2145 #ifndef PROFILE
2146 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2147 NULL)
2148 == -1)
2149 err(1, "pledge");
2150 #endif
2152 limit = get_default_log_limit();
2154 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2155 switch (ch) {
2156 case 'p':
2157 show_patch = 1;
2158 break;
2159 case 'c':
2160 start_commit = optarg;
2161 break;
2162 case 'C':
2163 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2164 &errstr);
2165 if (errstr != NULL)
2166 err(1, "-C option %s", errstr);
2167 break;
2168 case 'l':
2169 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2170 if (errstr != NULL)
2171 err(1, "-l option %s", errstr);
2172 break;
2173 case 'b':
2174 log_branches = 1;
2175 break;
2176 case 'r':
2177 repo_path = realpath(optarg, NULL);
2178 if (repo_path == NULL)
2179 return got_error_from_errno2("realpath",
2180 optarg);
2181 got_path_strip_trailing_slashes(repo_path);
2182 break;
2183 case 's':
2184 search_pattern = optarg;
2185 break;
2186 default:
2187 usage_log();
2188 /* NOTREACHED */
2192 argc -= optind;
2193 argv += optind;
2195 if (diff_context == -1)
2196 diff_context = 3;
2197 else if (!show_patch)
2198 errx(1, "-C reguires -p");
2200 cwd = getcwd(NULL, 0);
2201 if (cwd == NULL) {
2202 error = got_error_from_errno("getcwd");
2203 goto done;
2206 error = got_worktree_open(&worktree, cwd);
2207 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2208 goto done;
2209 error = NULL;
2211 if (argc == 0) {
2212 path = strdup("");
2213 if (path == NULL) {
2214 error = got_error_from_errno("strdup");
2215 goto done;
2217 } else if (argc == 1) {
2218 if (worktree) {
2219 error = got_worktree_resolve_path(&path, worktree,
2220 argv[0]);
2221 if (error)
2222 goto done;
2223 } else {
2224 path = strdup(argv[0]);
2225 if (path == NULL) {
2226 error = got_error_from_errno("strdup");
2227 goto done;
2230 } else
2231 usage_log();
2233 if (repo_path == NULL) {
2234 repo_path = worktree ?
2235 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2237 if (repo_path == NULL) {
2238 error = got_error_from_errno("strdup");
2239 goto done;
2242 error = got_repo_open(&repo, repo_path, NULL);
2243 if (error != NULL)
2244 goto done;
2246 error = apply_unveil(got_repo_get_path(repo), 1,
2247 worktree ? got_worktree_get_root_path(worktree) : NULL);
2248 if (error)
2249 goto done;
2251 if (start_commit == NULL) {
2252 struct got_reference *head_ref;
2253 error = got_ref_open(&head_ref, repo,
2254 worktree ? got_worktree_get_head_ref_name(worktree)
2255 : GOT_REF_HEAD, 0);
2256 if (error != NULL)
2257 return error;
2258 error = got_ref_resolve(&id, repo, head_ref);
2259 got_ref_close(head_ref);
2260 if (error != NULL)
2261 return error;
2262 error = got_object_open_as_commit(&commit, repo, id);
2263 } else {
2264 struct got_reference *ref;
2265 error = got_ref_open(&ref, repo, start_commit, 0);
2266 if (error == NULL) {
2267 int obj_type;
2268 error = got_ref_resolve(&id, repo, ref);
2269 got_ref_close(ref);
2270 if (error != NULL)
2271 goto done;
2272 error = got_object_get_type(&obj_type, repo, id);
2273 if (error != NULL)
2274 goto done;
2275 if (obj_type == GOT_OBJ_TYPE_TAG) {
2276 struct got_tag_object *tag;
2277 error = got_object_open_as_tag(&tag, repo, id);
2278 if (error != NULL)
2279 goto done;
2280 if (got_object_tag_get_object_type(tag) !=
2281 GOT_OBJ_TYPE_COMMIT) {
2282 got_object_tag_close(tag);
2283 error = got_error(GOT_ERR_OBJ_TYPE);
2284 goto done;
2286 free(id);
2287 id = got_object_id_dup(
2288 got_object_tag_get_object_id(tag));
2289 if (id == NULL)
2290 error = got_error_from_errno(
2291 "got_object_id_dup");
2292 got_object_tag_close(tag);
2293 if (error)
2294 goto done;
2295 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2296 error = got_error(GOT_ERR_OBJ_TYPE);
2297 goto done;
2299 error = got_object_open_as_commit(&commit, repo, id);
2300 if (error != NULL)
2301 goto done;
2303 if (commit == NULL) {
2304 error = got_repo_match_object_id_prefix(&id,
2305 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2306 if (error != NULL)
2307 return error;
2310 if (error != NULL)
2311 goto done;
2313 if (worktree) {
2314 const char *prefix = got_worktree_get_path_prefix(worktree);
2315 char *p;
2316 if (asprintf(&p, "%s%s%s", prefix,
2317 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2318 error = got_error_from_errno("asprintf");
2319 goto done;
2321 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2322 free(p);
2323 } else
2324 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2325 if (error != NULL)
2326 goto done;
2327 if (in_repo_path) {
2328 free(path);
2329 path = in_repo_path;
2332 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2333 if (error)
2334 goto done;
2336 error = print_commits(id, repo, path, show_patch, search_pattern,
2337 diff_context, limit, log_branches, &refs);
2338 done:
2339 free(path);
2340 free(repo_path);
2341 free(cwd);
2342 free(id);
2343 if (worktree)
2344 got_worktree_close(worktree);
2345 if (repo) {
2346 const struct got_error *repo_error;
2347 repo_error = got_repo_close(repo);
2348 if (error == NULL)
2349 error = repo_error;
2351 got_ref_list_free(&refs);
2352 return error;
2355 __dead static void
2356 usage_diff(void)
2358 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2359 "[-w] [object1 object2 | path]\n", getprogname());
2360 exit(1);
2363 struct print_diff_arg {
2364 struct got_repository *repo;
2365 struct got_worktree *worktree;
2366 int diff_context;
2367 const char *id_str;
2368 int header_shown;
2369 int diff_staged;
2370 int ignore_whitespace;
2373 static const struct got_error *
2374 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2375 const char *path, struct got_object_id *blob_id,
2376 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2377 int dirfd, const char *de_name)
2379 struct print_diff_arg *a = arg;
2380 const struct got_error *err = NULL;
2381 struct got_blob_object *blob1 = NULL;
2382 int fd = -1;
2383 FILE *f2 = NULL;
2384 char *abspath = NULL, *label1 = NULL;
2385 struct stat sb;
2387 if (a->diff_staged) {
2388 if (staged_status != GOT_STATUS_MODIFY &&
2389 staged_status != GOT_STATUS_ADD &&
2390 staged_status != GOT_STATUS_DELETE)
2391 return NULL;
2392 } else {
2393 if (staged_status == GOT_STATUS_DELETE)
2394 return NULL;
2395 if (status == GOT_STATUS_NONEXISTENT)
2396 return got_error_set_errno(ENOENT, path);
2397 if (status != GOT_STATUS_MODIFY &&
2398 status != GOT_STATUS_ADD &&
2399 status != GOT_STATUS_DELETE &&
2400 status != GOT_STATUS_CONFLICT)
2401 return NULL;
2404 if (!a->header_shown) {
2405 printf("diff %s %s%s\n", a->id_str,
2406 got_worktree_get_root_path(a->worktree),
2407 a->diff_staged ? " (staged changes)" : "");
2408 a->header_shown = 1;
2411 if (a->diff_staged) {
2412 const char *label1 = NULL, *label2 = NULL;
2413 switch (staged_status) {
2414 case GOT_STATUS_MODIFY:
2415 label1 = path;
2416 label2 = path;
2417 break;
2418 case GOT_STATUS_ADD:
2419 label2 = path;
2420 break;
2421 case GOT_STATUS_DELETE:
2422 label1 = path;
2423 break;
2424 default:
2425 return got_error(GOT_ERR_FILE_STATUS);
2427 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2428 label1, label2, a->diff_context, a->ignore_whitespace,
2429 a->repo, stdout);
2432 if (staged_status == GOT_STATUS_ADD ||
2433 staged_status == GOT_STATUS_MODIFY) {
2434 char *id_str;
2435 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2436 8192);
2437 if (err)
2438 goto done;
2439 err = got_object_id_str(&id_str, staged_blob_id);
2440 if (err)
2441 goto done;
2442 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2443 err = got_error_from_errno("asprintf");
2444 free(id_str);
2445 goto done;
2447 free(id_str);
2448 } else if (status != GOT_STATUS_ADD) {
2449 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2450 if (err)
2451 goto done;
2454 if (status != GOT_STATUS_DELETE) {
2455 if (asprintf(&abspath, "%s/%s",
2456 got_worktree_get_root_path(a->worktree), path) == -1) {
2457 err = got_error_from_errno("asprintf");
2458 goto done;
2461 if (dirfd != -1) {
2462 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2463 if (fd == -1) {
2464 err = got_error_from_errno2("openat", abspath);
2465 goto done;
2467 } else {
2468 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2469 if (fd == -1) {
2470 err = got_error_from_errno2("open", abspath);
2471 goto done;
2474 if (fstat(fd, &sb) == -1) {
2475 err = got_error_from_errno2("fstat", abspath);
2476 goto done;
2478 f2 = fdopen(fd, "r");
2479 if (f2 == NULL) {
2480 err = got_error_from_errno2("fdopen", abspath);
2481 goto done;
2483 fd = -1;
2484 } else
2485 sb.st_size = 0;
2487 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2488 a->diff_context, a->ignore_whitespace, stdout);
2489 done:
2490 if (blob1)
2491 got_object_blob_close(blob1);
2492 if (f2 && fclose(f2) == EOF && err == NULL)
2493 err = got_error_from_errno("fclose");
2494 if (fd != -1 && close(fd) == -1 && err == NULL)
2495 err = got_error_from_errno("close");
2496 free(abspath);
2497 return err;
2500 static const struct got_error *
2501 cmd_diff(int argc, char *argv[])
2503 const struct got_error *error;
2504 struct got_repository *repo = NULL;
2505 struct got_worktree *worktree = NULL;
2506 char *cwd = NULL, *repo_path = NULL;
2507 struct got_object_id *id1 = NULL, *id2 = NULL;
2508 const char *id_str1 = NULL, *id_str2 = NULL;
2509 char *label1 = NULL, *label2 = NULL;
2510 int type1, type2;
2511 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2512 const char *errstr;
2513 char *path = NULL;
2515 #ifndef PROFILE
2516 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2517 NULL) == -1)
2518 err(1, "pledge");
2519 #endif
2521 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2522 switch (ch) {
2523 case 'C':
2524 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2525 &errstr);
2526 if (errstr != NULL)
2527 err(1, "-C option %s", errstr);
2528 break;
2529 case 'r':
2530 repo_path = realpath(optarg, NULL);
2531 if (repo_path == NULL)
2532 return got_error_from_errno2("realpath",
2533 optarg);
2534 got_path_strip_trailing_slashes(repo_path);
2535 break;
2536 case 's':
2537 diff_staged = 1;
2538 break;
2539 case 'w':
2540 ignore_whitespace = 1;
2541 break;
2542 default:
2543 usage_diff();
2544 /* NOTREACHED */
2548 argc -= optind;
2549 argv += optind;
2551 cwd = getcwd(NULL, 0);
2552 if (cwd == NULL) {
2553 error = got_error_from_errno("getcwd");
2554 goto done;
2556 error = got_worktree_open(&worktree, cwd);
2557 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2558 goto done;
2559 if (argc <= 1) {
2560 if (worktree == NULL) {
2561 error = got_error(GOT_ERR_NOT_WORKTREE);
2562 goto done;
2564 if (repo_path)
2565 errx(1,
2566 "-r option can't be used when diffing a work tree");
2567 repo_path = strdup(got_worktree_get_repo_path(worktree));
2568 if (repo_path == NULL) {
2569 error = got_error_from_errno("strdup");
2570 goto done;
2572 if (argc == 1) {
2573 error = got_worktree_resolve_path(&path, worktree,
2574 argv[0]);
2575 if (error)
2576 goto done;
2577 } else {
2578 path = strdup("");
2579 if (path == NULL) {
2580 error = got_error_from_errno("strdup");
2581 goto done;
2584 } else if (argc == 2) {
2585 if (diff_staged)
2586 errx(1, "-s option can't be used when diffing "
2587 "objects in repository");
2588 id_str1 = argv[0];
2589 id_str2 = argv[1];
2590 if (worktree && repo_path == NULL) {
2591 repo_path =
2592 strdup(got_worktree_get_repo_path(worktree));
2593 if (repo_path == NULL) {
2594 error = got_error_from_errno("strdup");
2595 goto done;
2598 } else
2599 usage_diff();
2601 if (repo_path == NULL) {
2602 repo_path = getcwd(NULL, 0);
2603 if (repo_path == NULL)
2604 return got_error_from_errno("getcwd");
2607 error = got_repo_open(&repo, repo_path, NULL);
2608 free(repo_path);
2609 if (error != NULL)
2610 goto done;
2612 error = apply_unveil(got_repo_get_path(repo), 1,
2613 worktree ? got_worktree_get_root_path(worktree) : NULL);
2614 if (error)
2615 goto done;
2617 if (argc <= 1) {
2618 struct print_diff_arg arg;
2619 struct got_pathlist_head paths;
2620 char *id_str;
2622 TAILQ_INIT(&paths);
2624 error = got_object_id_str(&id_str,
2625 got_worktree_get_base_commit_id(worktree));
2626 if (error)
2627 goto done;
2628 arg.repo = repo;
2629 arg.worktree = worktree;
2630 arg.diff_context = diff_context;
2631 arg.id_str = id_str;
2632 arg.header_shown = 0;
2633 arg.diff_staged = diff_staged;
2634 arg.ignore_whitespace = ignore_whitespace;
2636 error = got_pathlist_append(&paths, path, NULL);
2637 if (error)
2638 goto done;
2640 error = got_worktree_status(worktree, &paths, repo, print_diff,
2641 &arg, check_cancelled, NULL);
2642 free(id_str);
2643 got_pathlist_free(&paths);
2644 goto done;
2647 error = got_repo_match_object_id(&id1, &label1, id_str1,
2648 GOT_OBJ_TYPE_ANY, 1, repo);
2649 if (error)
2650 goto done;
2652 error = got_repo_match_object_id(&id2, &label2, id_str2,
2653 GOT_OBJ_TYPE_ANY, 1, repo);
2654 if (error)
2655 goto done;
2657 error = got_object_get_type(&type1, repo, id1);
2658 if (error)
2659 goto done;
2661 error = got_object_get_type(&type2, repo, id2);
2662 if (error)
2663 goto done;
2665 if (type1 != type2) {
2666 error = got_error(GOT_ERR_OBJ_TYPE);
2667 goto done;
2670 switch (type1) {
2671 case GOT_OBJ_TYPE_BLOB:
2672 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2673 diff_context, ignore_whitespace, repo, stdout);
2674 break;
2675 case GOT_OBJ_TYPE_TREE:
2676 error = got_diff_objects_as_trees(id1, id2, "", "",
2677 diff_context, ignore_whitespace, repo, stdout);
2678 break;
2679 case GOT_OBJ_TYPE_COMMIT:
2680 printf("diff %s %s\n", label1, label2);
2681 error = got_diff_objects_as_commits(id1, id2, diff_context,
2682 ignore_whitespace, repo, stdout);
2683 break;
2684 default:
2685 error = got_error(GOT_ERR_OBJ_TYPE);
2687 done:
2688 free(label1);
2689 free(label2);
2690 free(id1);
2691 free(id2);
2692 free(path);
2693 if (worktree)
2694 got_worktree_close(worktree);
2695 if (repo) {
2696 const struct got_error *repo_error;
2697 repo_error = got_repo_close(repo);
2698 if (error == NULL)
2699 error = repo_error;
2701 return error;
2704 __dead static void
2705 usage_blame(void)
2707 fprintf(stderr,
2708 "usage: %s blame [-c commit] [-r repository-path] path\n",
2709 getprogname());
2710 exit(1);
2713 struct blame_line {
2714 int annotated;
2715 char *id_str;
2716 char *committer;
2717 char datebuf[11]; /* YYYY-MM-DD + NUL */
2720 struct blame_cb_args {
2721 struct blame_line *lines;
2722 int nlines;
2723 int nlines_prec;
2724 int lineno_cur;
2725 off_t *line_offsets;
2726 FILE *f;
2727 struct got_repository *repo;
2730 static const struct got_error *
2731 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2733 const struct got_error *err = NULL;
2734 struct blame_cb_args *a = arg;
2735 struct blame_line *bline;
2736 char *line = NULL;
2737 size_t linesize = 0;
2738 struct got_commit_object *commit = NULL;
2739 off_t offset;
2740 struct tm tm;
2741 time_t committer_time;
2743 if (nlines != a->nlines ||
2744 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2745 return got_error(GOT_ERR_RANGE);
2747 if (sigint_received)
2748 return got_error(GOT_ERR_ITER_COMPLETED);
2750 if (lineno == -1)
2751 return NULL; /* no change in this commit */
2753 /* Annotate this line. */
2754 bline = &a->lines[lineno - 1];
2755 if (bline->annotated)
2756 return NULL;
2757 err = got_object_id_str(&bline->id_str, id);
2758 if (err)
2759 return err;
2761 err = got_object_open_as_commit(&commit, a->repo, id);
2762 if (err)
2763 goto done;
2765 bline->committer = strdup(got_object_commit_get_committer(commit));
2766 if (bline->committer == NULL) {
2767 err = got_error_from_errno("strdup");
2768 goto done;
2771 committer_time = got_object_commit_get_committer_time(commit);
2772 if (localtime_r(&committer_time, &tm) == NULL)
2773 return got_error_from_errno("localtime_r");
2774 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2775 &tm) >= sizeof(bline->datebuf)) {
2776 err = got_error(GOT_ERR_NO_SPACE);
2777 goto done;
2779 bline->annotated = 1;
2781 /* Print lines annotated so far. */
2782 bline = &a->lines[a->lineno_cur - 1];
2783 if (!bline->annotated)
2784 goto done;
2786 offset = a->line_offsets[a->lineno_cur - 1];
2787 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2788 err = got_error_from_errno("fseeko");
2789 goto done;
2792 while (bline->annotated) {
2793 char *smallerthan, *at, *nl, *committer;
2794 size_t len;
2796 if (getline(&line, &linesize, a->f) == -1) {
2797 if (ferror(a->f))
2798 err = got_error_from_errno("getline");
2799 break;
2802 committer = bline->committer;
2803 smallerthan = strchr(committer, '<');
2804 if (smallerthan && smallerthan[1] != '\0')
2805 committer = smallerthan + 1;
2806 at = strchr(committer, '@');
2807 if (at)
2808 *at = '\0';
2809 len = strlen(committer);
2810 if (len >= 9)
2811 committer[8] = '\0';
2813 nl = strchr(line, '\n');
2814 if (nl)
2815 *nl = '\0';
2816 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2817 bline->id_str, bline->datebuf, committer, line);
2819 a->lineno_cur++;
2820 bline = &a->lines[a->lineno_cur - 1];
2822 done:
2823 if (commit)
2824 got_object_commit_close(commit);
2825 free(line);
2826 return err;
2829 static const struct got_error *
2830 cmd_blame(int argc, char *argv[])
2832 const struct got_error *error;
2833 struct got_repository *repo = NULL;
2834 struct got_worktree *worktree = NULL;
2835 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2836 struct got_object_id *obj_id = NULL;
2837 struct got_object_id *commit_id = NULL;
2838 struct got_blob_object *blob = NULL;
2839 char *commit_id_str = NULL;
2840 struct blame_cb_args bca;
2841 int ch, obj_type, i;
2842 size_t filesize;
2844 memset(&bca, 0, sizeof(bca));
2846 #ifndef PROFILE
2847 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2848 NULL) == -1)
2849 err(1, "pledge");
2850 #endif
2852 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2853 switch (ch) {
2854 case 'c':
2855 commit_id_str = optarg;
2856 break;
2857 case 'r':
2858 repo_path = realpath(optarg, NULL);
2859 if (repo_path == NULL)
2860 return got_error_from_errno2("realpath",
2861 optarg);
2862 got_path_strip_trailing_slashes(repo_path);
2863 break;
2864 default:
2865 usage_blame();
2866 /* NOTREACHED */
2870 argc -= optind;
2871 argv += optind;
2873 if (argc == 1)
2874 path = argv[0];
2875 else
2876 usage_blame();
2878 cwd = getcwd(NULL, 0);
2879 if (cwd == NULL) {
2880 error = got_error_from_errno("getcwd");
2881 goto done;
2883 if (repo_path == NULL) {
2884 error = got_worktree_open(&worktree, cwd);
2885 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2886 goto done;
2887 else
2888 error = NULL;
2889 if (worktree) {
2890 repo_path =
2891 strdup(got_worktree_get_repo_path(worktree));
2892 if (repo_path == NULL)
2893 error = got_error_from_errno("strdup");
2894 if (error)
2895 goto done;
2896 } else {
2897 repo_path = strdup(cwd);
2898 if (repo_path == NULL) {
2899 error = got_error_from_errno("strdup");
2900 goto done;
2905 error = got_repo_open(&repo, repo_path, NULL);
2906 if (error != NULL)
2907 goto done;
2909 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2910 if (error)
2911 goto done;
2913 if (worktree) {
2914 const char *prefix = got_worktree_get_path_prefix(worktree);
2915 char *p, *worktree_subdir = cwd +
2916 strlen(got_worktree_get_root_path(worktree));
2917 if (asprintf(&p, "%s%s%s%s%s",
2918 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2919 worktree_subdir, worktree_subdir[0] ? "/" : "",
2920 path) == -1) {
2921 error = got_error_from_errno("asprintf");
2922 goto done;
2924 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2925 free(p);
2926 } else {
2927 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2929 if (error)
2930 goto done;
2932 if (commit_id_str == NULL) {
2933 struct got_reference *head_ref;
2934 error = got_ref_open(&head_ref, repo, worktree ?
2935 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2936 if (error != NULL)
2937 goto done;
2938 error = got_ref_resolve(&commit_id, repo, head_ref);
2939 got_ref_close(head_ref);
2940 if (error != NULL)
2941 goto done;
2942 } else {
2943 error = got_repo_match_object_id(&commit_id, NULL,
2944 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2945 if (error)
2946 goto done;
2949 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2950 if (error)
2951 goto done;
2953 error = got_object_get_type(&obj_type, repo, obj_id);
2954 if (error)
2955 goto done;
2957 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2958 error = got_error(GOT_ERR_OBJ_TYPE);
2959 goto done;
2962 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2963 if (error)
2964 goto done;
2965 bca.f = got_opentemp();
2966 if (bca.f == NULL) {
2967 error = got_error_from_errno("got_opentemp");
2968 goto done;
2970 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2971 &bca.line_offsets, bca.f, blob);
2972 if (error || bca.nlines == 0)
2973 goto done;
2975 /* Don't include \n at EOF in the blame line count. */
2976 if (bca.line_offsets[bca.nlines - 1] == filesize)
2977 bca.nlines--;
2979 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2980 if (bca.lines == NULL) {
2981 error = got_error_from_errno("calloc");
2982 goto done;
2984 bca.lineno_cur = 1;
2985 bca.nlines_prec = 0;
2986 i = bca.nlines;
2987 while (i > 0) {
2988 i /= 10;
2989 bca.nlines_prec++;
2991 bca.repo = repo;
2993 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2994 check_cancelled, NULL);
2995 done:
2996 free(in_repo_path);
2997 free(repo_path);
2998 free(cwd);
2999 free(commit_id);
3000 free(obj_id);
3001 if (blob)
3002 got_object_blob_close(blob);
3003 if (worktree)
3004 got_worktree_close(worktree);
3005 if (repo) {
3006 const struct got_error *repo_error;
3007 repo_error = got_repo_close(repo);
3008 if (error == NULL)
3009 error = repo_error;
3011 if (bca.lines) {
3012 for (i = 0; i < bca.nlines; i++) {
3013 struct blame_line *bline = &bca.lines[i];
3014 free(bline->id_str);
3015 free(bline->committer);
3017 free(bca.lines);
3019 free(bca.line_offsets);
3020 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3021 error = got_error_from_errno("fclose");
3022 return error;
3025 __dead static void
3026 usage_tree(void)
3028 fprintf(stderr,
3029 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3030 getprogname());
3031 exit(1);
3034 static void
3035 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3036 const char *root_path)
3038 int is_root_path = (strcmp(path, root_path) == 0);
3039 const char *modestr = "";
3040 mode_t mode = got_tree_entry_get_mode(te);
3042 path += strlen(root_path);
3043 while (path[0] == '/')
3044 path++;
3046 if (got_object_tree_entry_is_submodule(te))
3047 modestr = "$";
3048 else if (S_ISLNK(mode))
3049 modestr = "@";
3050 else if (S_ISDIR(mode))
3051 modestr = "/";
3052 else if (mode & S_IXUSR)
3053 modestr = "*";
3055 printf("%s%s%s%s%s\n", id ? id : "", path,
3056 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3059 static const struct got_error *
3060 print_tree(const char *path, struct got_object_id *commit_id,
3061 int show_ids, int recurse, const char *root_path,
3062 struct got_repository *repo)
3064 const struct got_error *err = NULL;
3065 struct got_object_id *tree_id = NULL;
3066 struct got_tree_object *tree = NULL;
3067 int nentries, i;
3069 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3070 if (err)
3071 goto done;
3073 err = got_object_open_as_tree(&tree, repo, tree_id);
3074 if (err)
3075 goto done;
3076 nentries = got_object_tree_get_nentries(tree);
3077 for (i = 0; i < nentries; i++) {
3078 struct got_tree_entry *te;
3079 char *id = NULL;
3081 if (sigint_received || sigpipe_received)
3082 break;
3084 te = got_object_tree_get_entry(tree, i);
3085 if (show_ids) {
3086 char *id_str;
3087 err = got_object_id_str(&id_str,
3088 got_tree_entry_get_id(te));
3089 if (err)
3090 goto done;
3091 if (asprintf(&id, "%s ", id_str) == -1) {
3092 err = got_error_from_errno("asprintf");
3093 free(id_str);
3094 goto done;
3096 free(id_str);
3098 print_entry(te, id, path, root_path);
3099 free(id);
3101 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3102 char *child_path;
3103 if (asprintf(&child_path, "%s%s%s", path,
3104 path[0] == '/' && path[1] == '\0' ? "" : "/",
3105 got_tree_entry_get_name(te)) == -1) {
3106 err = got_error_from_errno("asprintf");
3107 goto done;
3109 err = print_tree(child_path, commit_id, show_ids, 1,
3110 root_path, repo);
3111 free(child_path);
3112 if (err)
3113 goto done;
3116 done:
3117 if (tree)
3118 got_object_tree_close(tree);
3119 free(tree_id);
3120 return err;
3123 static const struct got_error *
3124 cmd_tree(int argc, char *argv[])
3126 const struct got_error *error;
3127 struct got_repository *repo = NULL;
3128 struct got_worktree *worktree = NULL;
3129 const char *path;
3130 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3131 struct got_object_id *commit_id = NULL;
3132 char *commit_id_str = NULL;
3133 int show_ids = 0, recurse = 0;
3134 int ch;
3136 #ifndef PROFILE
3137 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3138 NULL) == -1)
3139 err(1, "pledge");
3140 #endif
3142 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3143 switch (ch) {
3144 case 'c':
3145 commit_id_str = optarg;
3146 break;
3147 case 'r':
3148 repo_path = realpath(optarg, NULL);
3149 if (repo_path == NULL)
3150 return got_error_from_errno2("realpath",
3151 optarg);
3152 got_path_strip_trailing_slashes(repo_path);
3153 break;
3154 case 'i':
3155 show_ids = 1;
3156 break;
3157 case 'R':
3158 recurse = 1;
3159 break;
3160 default:
3161 usage_tree();
3162 /* NOTREACHED */
3166 argc -= optind;
3167 argv += optind;
3169 if (argc == 1)
3170 path = argv[0];
3171 else if (argc > 1)
3172 usage_tree();
3173 else
3174 path = NULL;
3176 cwd = getcwd(NULL, 0);
3177 if (cwd == NULL) {
3178 error = got_error_from_errno("getcwd");
3179 goto done;
3181 if (repo_path == NULL) {
3182 error = got_worktree_open(&worktree, cwd);
3183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3184 goto done;
3185 else
3186 error = NULL;
3187 if (worktree) {
3188 repo_path =
3189 strdup(got_worktree_get_repo_path(worktree));
3190 if (repo_path == NULL)
3191 error = got_error_from_errno("strdup");
3192 if (error)
3193 goto done;
3194 } else {
3195 repo_path = strdup(cwd);
3196 if (repo_path == NULL) {
3197 error = got_error_from_errno("strdup");
3198 goto done;
3203 error = got_repo_open(&repo, repo_path, NULL);
3204 if (error != NULL)
3205 goto done;
3207 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3208 if (error)
3209 goto done;
3211 if (path == NULL) {
3212 if (worktree) {
3213 char *p, *worktree_subdir = cwd +
3214 strlen(got_worktree_get_root_path(worktree));
3215 if (asprintf(&p, "%s/%s",
3216 got_worktree_get_path_prefix(worktree),
3217 worktree_subdir) == -1) {
3218 error = got_error_from_errno("asprintf");
3219 goto done;
3221 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3222 free(p);
3223 if (error)
3224 goto done;
3225 } else
3226 path = "/";
3228 if (in_repo_path == NULL) {
3229 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3230 if (error != NULL)
3231 goto done;
3234 if (commit_id_str == NULL) {
3235 struct got_reference *head_ref;
3236 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3237 if (error != NULL)
3238 goto done;
3239 error = got_ref_resolve(&commit_id, repo, head_ref);
3240 got_ref_close(head_ref);
3241 if (error != NULL)
3242 goto done;
3243 } else {
3244 error = got_repo_match_object_id(&commit_id, NULL,
3245 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3246 if (error)
3247 goto done;
3250 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3251 in_repo_path, repo);
3252 done:
3253 free(in_repo_path);
3254 free(repo_path);
3255 free(cwd);
3256 free(commit_id);
3257 if (worktree)
3258 got_worktree_close(worktree);
3259 if (repo) {
3260 const struct got_error *repo_error;
3261 repo_error = got_repo_close(repo);
3262 if (error == NULL)
3263 error = repo_error;
3265 return error;
3268 __dead static void
3269 usage_status(void)
3271 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3272 exit(1);
3275 static const struct got_error *
3276 print_status(void *arg, unsigned char status, unsigned char staged_status,
3277 const char *path, struct got_object_id *blob_id,
3278 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3279 int dirfd, const char *de_name)
3281 if (status == staged_status && (status == GOT_STATUS_DELETE))
3282 status = GOT_STATUS_NO_CHANGE;
3283 printf("%c%c %s\n", status, staged_status, path);
3284 return NULL;
3287 static const struct got_error *
3288 cmd_status(int argc, char *argv[])
3290 const struct got_error *error = NULL;
3291 struct got_repository *repo = NULL;
3292 struct got_worktree *worktree = NULL;
3293 char *cwd = NULL;
3294 struct got_pathlist_head paths;
3295 struct got_pathlist_entry *pe;
3296 int ch;
3298 TAILQ_INIT(&paths);
3300 while ((ch = getopt(argc, argv, "")) != -1) {
3301 switch (ch) {
3302 default:
3303 usage_status();
3304 /* NOTREACHED */
3308 argc -= optind;
3309 argv += optind;
3311 #ifndef PROFILE
3312 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3313 NULL) == -1)
3314 err(1, "pledge");
3315 #endif
3316 cwd = getcwd(NULL, 0);
3317 if (cwd == NULL) {
3318 error = got_error_from_errno("getcwd");
3319 goto done;
3322 error = got_worktree_open(&worktree, cwd);
3323 if (error != NULL)
3324 goto done;
3326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3327 NULL);
3328 if (error != NULL)
3329 goto done;
3331 error = apply_unveil(got_repo_get_path(repo), 1,
3332 got_worktree_get_root_path(worktree));
3333 if (error)
3334 goto done;
3336 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3337 if (error)
3338 goto done;
3340 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3341 check_cancelled, NULL);
3342 done:
3343 TAILQ_FOREACH(pe, &paths, entry)
3344 free((char *)pe->path);
3345 got_pathlist_free(&paths);
3346 free(cwd);
3347 return error;
3350 __dead static void
3351 usage_ref(void)
3353 fprintf(stderr,
3354 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3355 getprogname());
3356 exit(1);
3359 static const struct got_error *
3360 list_refs(struct got_repository *repo)
3362 static const struct got_error *err = NULL;
3363 struct got_reflist_head refs;
3364 struct got_reflist_entry *re;
3366 SIMPLEQ_INIT(&refs);
3367 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3368 if (err)
3369 return err;
3371 SIMPLEQ_FOREACH(re, &refs, entry) {
3372 char *refstr;
3373 refstr = got_ref_to_str(re->ref);
3374 if (refstr == NULL)
3375 return got_error_from_errno("got_ref_to_str");
3376 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3377 free(refstr);
3380 got_ref_list_free(&refs);
3381 return NULL;
3384 static const struct got_error *
3385 delete_ref(struct got_repository *repo, const char *refname)
3387 const struct got_error *err = NULL;
3388 struct got_reference *ref;
3390 err = got_ref_open(&ref, repo, refname, 0);
3391 if (err)
3392 return err;
3394 err = got_ref_delete(ref, repo);
3395 got_ref_close(ref);
3396 return err;
3399 static const struct got_error *
3400 add_ref(struct got_repository *repo, const char *refname, const char *target)
3402 const struct got_error *err = NULL;
3403 struct got_object_id *id;
3404 struct got_reference *ref = NULL;
3407 * Don't let the user create a reference name with a leading '-'.
3408 * While technically a valid reference name, this case is usually
3409 * an unintended typo.
3411 if (refname[0] == '-')
3412 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3414 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3415 repo);
3416 if (err) {
3417 struct got_reference *target_ref;
3419 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3420 return err;
3421 err = got_ref_open(&target_ref, repo, target, 0);
3422 if (err)
3423 return err;
3424 err = got_ref_resolve(&id, repo, target_ref);
3425 got_ref_close(target_ref);
3426 if (err)
3427 return err;
3430 err = got_ref_alloc(&ref, refname, id);
3431 if (err)
3432 goto done;
3434 err = got_ref_write(ref, repo);
3435 done:
3436 if (ref)
3437 got_ref_close(ref);
3438 free(id);
3439 return err;
3442 static const struct got_error *
3443 add_symref(struct got_repository *repo, const char *refname, const char *target)
3445 const struct got_error *err = NULL;
3446 struct got_reference *ref = NULL;
3447 struct got_reference *target_ref = NULL;
3450 * Don't let the user create a reference name with a leading '-'.
3451 * While technically a valid reference name, this case is usually
3452 * an unintended typo.
3454 if (refname[0] == '-')
3455 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3457 err = got_ref_open(&target_ref, repo, target, 0);
3458 if (err)
3459 return err;
3461 err = got_ref_alloc_symref(&ref, refname, target_ref);
3462 if (err)
3463 goto done;
3465 err = got_ref_write(ref, repo);
3466 done:
3467 if (target_ref)
3468 got_ref_close(target_ref);
3469 if (ref)
3470 got_ref_close(ref);
3471 return err;
3474 static const struct got_error *
3475 cmd_ref(int argc, char *argv[])
3477 const struct got_error *error = NULL;
3478 struct got_repository *repo = NULL;
3479 struct got_worktree *worktree = NULL;
3480 char *cwd = NULL, *repo_path = NULL;
3481 int ch, do_list = 0, create_symref = 0;
3482 const char *delref = NULL;
3484 /* TODO: Add -s option for adding symbolic references. */
3485 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3486 switch (ch) {
3487 case 'd':
3488 delref = optarg;
3489 break;
3490 case 'r':
3491 repo_path = realpath(optarg, NULL);
3492 if (repo_path == NULL)
3493 return got_error_from_errno2("realpath",
3494 optarg);
3495 got_path_strip_trailing_slashes(repo_path);
3496 break;
3497 case 'l':
3498 do_list = 1;
3499 break;
3500 case 's':
3501 create_symref = 1;
3502 break;
3503 default:
3504 usage_ref();
3505 /* NOTREACHED */
3509 if (do_list && delref)
3510 errx(1, "-l and -d options are mutually exclusive\n");
3512 argc -= optind;
3513 argv += optind;
3515 if (do_list || delref) {
3516 if (create_symref)
3517 errx(1, "-s option cannot be used together with the "
3518 "-l or -d options");
3519 if (argc > 0)
3520 usage_ref();
3521 } else if (argc != 2)
3522 usage_ref();
3524 #ifndef PROFILE
3525 if (do_list) {
3526 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3527 NULL) == -1)
3528 err(1, "pledge");
3529 } else {
3530 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3531 "sendfd unveil", NULL) == -1)
3532 err(1, "pledge");
3534 #endif
3535 cwd = getcwd(NULL, 0);
3536 if (cwd == NULL) {
3537 error = got_error_from_errno("getcwd");
3538 goto done;
3541 if (repo_path == NULL) {
3542 error = got_worktree_open(&worktree, cwd);
3543 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3544 goto done;
3545 else
3546 error = NULL;
3547 if (worktree) {
3548 repo_path =
3549 strdup(got_worktree_get_repo_path(worktree));
3550 if (repo_path == NULL)
3551 error = got_error_from_errno("strdup");
3552 if (error)
3553 goto done;
3554 } else {
3555 repo_path = strdup(cwd);
3556 if (repo_path == NULL) {
3557 error = got_error_from_errno("strdup");
3558 goto done;
3563 error = got_repo_open(&repo, repo_path, NULL);
3564 if (error != NULL)
3565 goto done;
3567 error = apply_unveil(got_repo_get_path(repo), do_list,
3568 worktree ? got_worktree_get_root_path(worktree) : NULL);
3569 if (error)
3570 goto done;
3572 if (do_list)
3573 error = list_refs(repo);
3574 else if (delref)
3575 error = delete_ref(repo, delref);
3576 else if (create_symref)
3577 error = add_symref(repo, argv[0], argv[1]);
3578 else
3579 error = add_ref(repo, argv[0], argv[1]);
3580 done:
3581 if (repo)
3582 got_repo_close(repo);
3583 if (worktree)
3584 got_worktree_close(worktree);
3585 free(cwd);
3586 free(repo_path);
3587 return error;
3590 __dead static void
3591 usage_branch(void)
3593 fprintf(stderr,
3594 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3595 "[name]\n", getprogname());
3596 exit(1);
3599 static const struct got_error *
3600 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3601 struct got_reference *ref)
3603 const struct got_error *err = NULL;
3604 const char *refname, *marker = " ";
3605 char *refstr;
3607 refname = got_ref_get_name(ref);
3608 if (worktree && strcmp(refname,
3609 got_worktree_get_head_ref_name(worktree)) == 0) {
3610 struct got_object_id *id = NULL;
3612 err = got_ref_resolve(&id, repo, ref);
3613 if (err)
3614 return err;
3615 if (got_object_id_cmp(id,
3616 got_worktree_get_base_commit_id(worktree)) == 0)
3617 marker = "* ";
3618 else
3619 marker = "~ ";
3620 free(id);
3623 if (strncmp(refname, "refs/heads/", 11) == 0)
3624 refname += 11;
3625 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3626 refname += 18;
3628 refstr = got_ref_to_str(ref);
3629 if (refstr == NULL)
3630 return got_error_from_errno("got_ref_to_str");
3632 printf("%s%s: %s\n", marker, refname, refstr);
3633 free(refstr);
3634 return NULL;
3637 static const struct got_error *
3638 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3640 const char *refname;
3642 if (worktree == NULL)
3643 return got_error(GOT_ERR_NOT_WORKTREE);
3645 refname = got_worktree_get_head_ref_name(worktree);
3647 if (strncmp(refname, "refs/heads/", 11) == 0)
3648 refname += 11;
3649 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3650 refname += 18;
3652 printf("%s\n", refname);
3654 return NULL;
3657 static const struct got_error *
3658 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3660 static const struct got_error *err = NULL;
3661 struct got_reflist_head refs;
3662 struct got_reflist_entry *re;
3663 struct got_reference *temp_ref = NULL;
3664 int rebase_in_progress, histedit_in_progress;
3666 SIMPLEQ_INIT(&refs);
3668 if (worktree) {
3669 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3670 worktree);
3671 if (err)
3672 return err;
3674 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3675 worktree);
3676 if (err)
3677 return err;
3679 if (rebase_in_progress || histedit_in_progress) {
3680 err = got_ref_open(&temp_ref, repo,
3681 got_worktree_get_head_ref_name(worktree), 0);
3682 if (err)
3683 return err;
3684 list_branch(repo, worktree, temp_ref);
3685 got_ref_close(temp_ref);
3689 err = got_ref_list(&refs, repo, "refs/heads",
3690 got_ref_cmp_by_name, NULL);
3691 if (err)
3692 return err;
3694 SIMPLEQ_FOREACH(re, &refs, entry)
3695 list_branch(repo, worktree, re->ref);
3697 got_ref_list_free(&refs);
3698 return NULL;
3701 static const struct got_error *
3702 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3703 const char *branch_name)
3705 const struct got_error *err = NULL;
3706 struct got_reference *ref = NULL;
3707 char *refname;
3709 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3710 return got_error_from_errno("asprintf");
3712 err = got_ref_open(&ref, repo, refname, 0);
3713 if (err)
3714 goto done;
3716 if (worktree &&
3717 strcmp(got_worktree_get_head_ref_name(worktree),
3718 got_ref_get_name(ref)) == 0) {
3719 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3720 "will not delete this work tree's current branch");
3721 goto done;
3724 err = got_ref_delete(ref, repo);
3725 done:
3726 if (ref)
3727 got_ref_close(ref);
3728 free(refname);
3729 return err;
3732 static const struct got_error *
3733 add_branch(struct got_repository *repo, const char *branch_name,
3734 struct got_object_id *base_commit_id)
3736 const struct got_error *err = NULL;
3737 struct got_reference *ref = NULL;
3738 char *base_refname = NULL, *refname = NULL;
3741 * Don't let the user create a branch name with a leading '-'.
3742 * While technically a valid reference name, this case is usually
3743 * an unintended typo.
3745 if (branch_name[0] == '-')
3746 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3748 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3749 err = got_error_from_errno("asprintf");
3750 goto done;
3753 err = got_ref_open(&ref, repo, refname, 0);
3754 if (err == NULL) {
3755 err = got_error(GOT_ERR_BRANCH_EXISTS);
3756 goto done;
3757 } else if (err->code != GOT_ERR_NOT_REF)
3758 goto done;
3760 err = got_ref_alloc(&ref, refname, base_commit_id);
3761 if (err)
3762 goto done;
3764 err = got_ref_write(ref, repo);
3765 done:
3766 if (ref)
3767 got_ref_close(ref);
3768 free(base_refname);
3769 free(refname);
3770 return err;
3773 static const struct got_error *
3774 cmd_branch(int argc, char *argv[])
3776 const struct got_error *error = NULL;
3777 struct got_repository *repo = NULL;
3778 struct got_worktree *worktree = NULL;
3779 char *cwd = NULL, *repo_path = NULL;
3780 int ch, do_list = 0, do_show = 0, do_update = 1;
3781 const char *delref = NULL, *commit_id_arg = NULL;
3782 struct got_reference *ref = NULL;
3783 struct got_pathlist_head paths;
3784 struct got_pathlist_entry *pe;
3785 struct got_object_id *commit_id = NULL;
3786 char *commit_id_str = NULL;
3788 TAILQ_INIT(&paths);
3790 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3791 switch (ch) {
3792 case 'c':
3793 commit_id_arg = optarg;
3794 break;
3795 case 'd':
3796 delref = optarg;
3797 break;
3798 case 'r':
3799 repo_path = realpath(optarg, NULL);
3800 if (repo_path == NULL)
3801 return got_error_from_errno2("realpath",
3802 optarg);
3803 got_path_strip_trailing_slashes(repo_path);
3804 break;
3805 case 'l':
3806 do_list = 1;
3807 break;
3808 case 'n':
3809 do_update = 0;
3810 break;
3811 default:
3812 usage_branch();
3813 /* NOTREACHED */
3817 if (do_list && delref)
3818 errx(1, "-l and -d options are mutually exclusive\n");
3820 argc -= optind;
3821 argv += optind;
3823 if (!do_list && !delref && argc == 0)
3824 do_show = 1;
3826 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3827 errx(1, "-c option can only be used when creating a branch");
3829 if (do_list || delref) {
3830 if (argc > 0)
3831 usage_branch();
3832 } else if (!do_show && argc != 1)
3833 usage_branch();
3835 #ifndef PROFILE
3836 if (do_list || do_show) {
3837 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3838 NULL) == -1)
3839 err(1, "pledge");
3840 } else {
3841 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3842 "sendfd unveil", NULL) == -1)
3843 err(1, "pledge");
3845 #endif
3846 cwd = getcwd(NULL, 0);
3847 if (cwd == NULL) {
3848 error = got_error_from_errno("getcwd");
3849 goto done;
3852 if (repo_path == NULL) {
3853 error = got_worktree_open(&worktree, cwd);
3854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3855 goto done;
3856 else
3857 error = NULL;
3858 if (worktree) {
3859 repo_path =
3860 strdup(got_worktree_get_repo_path(worktree));
3861 if (repo_path == NULL)
3862 error = got_error_from_errno("strdup");
3863 if (error)
3864 goto done;
3865 } else {
3866 repo_path = strdup(cwd);
3867 if (repo_path == NULL) {
3868 error = got_error_from_errno("strdup");
3869 goto done;
3874 error = got_repo_open(&repo, repo_path, NULL);
3875 if (error != NULL)
3876 goto done;
3878 error = apply_unveil(got_repo_get_path(repo), do_list,
3879 worktree ? got_worktree_get_root_path(worktree) : NULL);
3880 if (error)
3881 goto done;
3883 if (do_show)
3884 error = show_current_branch(repo, worktree);
3885 else if (do_list)
3886 error = list_branches(repo, worktree);
3887 else if (delref)
3888 error = delete_branch(repo, worktree, delref);
3889 else {
3890 if (commit_id_arg == NULL)
3891 commit_id_arg = worktree ?
3892 got_worktree_get_head_ref_name(worktree) :
3893 GOT_REF_HEAD;
3894 error = got_repo_match_object_id(&commit_id, NULL,
3895 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3896 if (error)
3897 goto done;
3898 error = add_branch(repo, argv[0], commit_id);
3899 if (error)
3900 goto done;
3901 if (worktree && do_update) {
3902 int did_something = 0;
3903 char *branch_refname = NULL;
3905 error = got_object_id_str(&commit_id_str, commit_id);
3906 if (error)
3907 goto done;
3908 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3909 worktree);
3910 if (error)
3911 goto done;
3912 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3913 == -1) {
3914 error = got_error_from_errno("asprintf");
3915 goto done;
3917 error = got_ref_open(&ref, repo, branch_refname, 0);
3918 free(branch_refname);
3919 if (error)
3920 goto done;
3921 error = switch_head_ref(ref, commit_id, worktree,
3922 repo);
3923 if (error)
3924 goto done;
3925 error = got_worktree_set_base_commit_id(worktree, repo,
3926 commit_id);
3927 if (error)
3928 goto done;
3929 error = got_worktree_checkout_files(worktree, &paths,
3930 repo, update_progress, &did_something,
3931 check_cancelled, NULL);
3932 if (error)
3933 goto done;
3934 if (did_something)
3935 printf("Updated to commit %s\n", commit_id_str);
3938 done:
3939 if (ref)
3940 got_ref_close(ref);
3941 if (repo)
3942 got_repo_close(repo);
3943 if (worktree)
3944 got_worktree_close(worktree);
3945 free(cwd);
3946 free(repo_path);
3947 free(commit_id);
3948 free(commit_id_str);
3949 TAILQ_FOREACH(pe, &paths, entry)
3950 free((char *)pe->path);
3951 got_pathlist_free(&paths);
3952 return error;
3956 __dead static void
3957 usage_tag(void)
3959 fprintf(stderr,
3960 "usage: %s tag [-c commit] [-r repository] [-l] "
3961 "[-m message] name\n", getprogname());
3962 exit(1);
3965 #if 0
3966 static const struct got_error *
3967 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3969 const struct got_error *err = NULL;
3970 struct got_reflist_entry *re, *se, *new;
3971 struct got_object_id *re_id, *se_id;
3972 struct got_tag_object *re_tag, *se_tag;
3973 time_t re_time, se_time;
3975 SIMPLEQ_FOREACH(re, tags, entry) {
3976 se = SIMPLEQ_FIRST(sorted);
3977 if (se == NULL) {
3978 err = got_reflist_entry_dup(&new, re);
3979 if (err)
3980 return err;
3981 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3982 continue;
3983 } else {
3984 err = got_ref_resolve(&re_id, repo, re->ref);
3985 if (err)
3986 break;
3987 err = got_object_open_as_tag(&re_tag, repo, re_id);
3988 free(re_id);
3989 if (err)
3990 break;
3991 re_time = got_object_tag_get_tagger_time(re_tag);
3992 got_object_tag_close(re_tag);
3995 while (se) {
3996 err = got_ref_resolve(&se_id, repo, re->ref);
3997 if (err)
3998 break;
3999 err = got_object_open_as_tag(&se_tag, repo, se_id);
4000 free(se_id);
4001 if (err)
4002 break;
4003 se_time = got_object_tag_get_tagger_time(se_tag);
4004 got_object_tag_close(se_tag);
4006 if (se_time > re_time) {
4007 err = got_reflist_entry_dup(&new, re);
4008 if (err)
4009 return err;
4010 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4011 break;
4013 se = SIMPLEQ_NEXT(se, entry);
4014 continue;
4017 done:
4018 return err;
4020 #endif
4022 static const struct got_error *
4023 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4025 static const struct got_error *err = NULL;
4026 struct got_reflist_head refs;
4027 struct got_reflist_entry *re;
4029 SIMPLEQ_INIT(&refs);
4031 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4032 if (err)
4033 return err;
4035 SIMPLEQ_FOREACH(re, &refs, entry) {
4036 const char *refname;
4037 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4038 char datebuf[26];
4039 const char *tagger;
4040 time_t tagger_time;
4041 struct got_object_id *id;
4042 struct got_tag_object *tag;
4043 struct got_commit_object *commit = NULL;
4045 refname = got_ref_get_name(re->ref);
4046 if (strncmp(refname, "refs/tags/", 10) != 0)
4047 continue;
4048 refname += 10;
4049 refstr = got_ref_to_str(re->ref);
4050 if (refstr == NULL) {
4051 err = got_error_from_errno("got_ref_to_str");
4052 break;
4054 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4055 free(refstr);
4057 err = got_ref_resolve(&id, repo, re->ref);
4058 if (err)
4059 break;
4060 err = got_object_open_as_tag(&tag, repo, id);
4061 if (err) {
4062 if (err->code != GOT_ERR_OBJ_TYPE) {
4063 free(id);
4064 break;
4066 /* "lightweight" tag */
4067 err = got_object_open_as_commit(&commit, repo, id);
4068 if (err) {
4069 free(id);
4070 break;
4072 tagger = got_object_commit_get_committer(commit);
4073 tagger_time =
4074 got_object_commit_get_committer_time(commit);
4075 err = got_object_id_str(&id_str, id);
4076 free(id);
4077 if (err)
4078 break;
4079 } else {
4080 free(id);
4081 tagger = got_object_tag_get_tagger(tag);
4082 tagger_time = got_object_tag_get_tagger_time(tag);
4083 err = got_object_id_str(&id_str,
4084 got_object_tag_get_object_id(tag));
4085 if (err)
4086 break;
4088 printf("from: %s\n", tagger);
4089 datestr = get_datestr(&tagger_time, datebuf);
4090 if (datestr)
4091 printf("date: %s UTC\n", datestr);
4092 if (commit)
4093 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4094 else {
4095 switch (got_object_tag_get_object_type(tag)) {
4096 case GOT_OBJ_TYPE_BLOB:
4097 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4098 id_str);
4099 break;
4100 case GOT_OBJ_TYPE_TREE:
4101 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4102 id_str);
4103 break;
4104 case GOT_OBJ_TYPE_COMMIT:
4105 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4106 id_str);
4107 break;
4108 case GOT_OBJ_TYPE_TAG:
4109 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4110 id_str);
4111 break;
4112 default:
4113 break;
4116 free(id_str);
4117 if (commit) {
4118 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4119 if (err)
4120 break;
4121 got_object_commit_close(commit);
4122 } else {
4123 tagmsg0 = strdup(got_object_tag_get_message(tag));
4124 got_object_tag_close(tag);
4125 if (tagmsg0 == NULL) {
4126 err = got_error_from_errno("strdup");
4127 break;
4131 tagmsg = tagmsg0;
4132 do {
4133 line = strsep(&tagmsg, "\n");
4134 if (line)
4135 printf(" %s\n", line);
4136 } while (line);
4137 free(tagmsg0);
4140 got_ref_list_free(&refs);
4141 return NULL;
4144 static const struct got_error *
4145 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4146 const char *tag_name, const char *repo_path)
4148 const struct got_error *err = NULL;
4149 char *template = NULL, *initial_content = NULL;
4150 char *editor = NULL;
4151 int fd = -1;
4153 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4154 err = got_error_from_errno("asprintf");
4155 goto done;
4158 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4159 commit_id_str, tag_name) == -1) {
4160 err = got_error_from_errno("asprintf");
4161 goto done;
4164 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4165 if (err)
4166 goto done;
4168 dprintf(fd, initial_content);
4169 close(fd);
4171 err = get_editor(&editor);
4172 if (err)
4173 goto done;
4174 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4175 done:
4176 free(initial_content);
4177 free(template);
4178 free(editor);
4180 /* Editor is done; we can now apply unveil(2) */
4181 if (err == NULL) {
4182 err = apply_unveil(repo_path, 0, NULL);
4183 if (err) {
4184 free(*tagmsg);
4185 *tagmsg = NULL;
4188 return err;
4191 static const struct got_error *
4192 add_tag(struct got_repository *repo, const char *tag_name,
4193 const char *commit_arg, const char *tagmsg_arg)
4195 const struct got_error *err = NULL;
4196 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4197 char *label = NULL, *commit_id_str = NULL;
4198 struct got_reference *ref = NULL;
4199 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4200 char *tagmsg_path = NULL, *tag_id_str = NULL;
4201 int preserve_tagmsg = 0;
4204 * Don't let the user create a tag name with a leading '-'.
4205 * While technically a valid reference name, this case is usually
4206 * an unintended typo.
4208 if (tag_name[0] == '-')
4209 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4211 err = get_author(&tagger, repo);
4212 if (err)
4213 return err;
4215 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4216 GOT_OBJ_TYPE_COMMIT, 1, repo);
4217 if (err)
4218 goto done;
4220 err = got_object_id_str(&commit_id_str, commit_id);
4221 if (err)
4222 goto done;
4224 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4225 refname = strdup(tag_name);
4226 if (refname == NULL) {
4227 err = got_error_from_errno("strdup");
4228 goto done;
4230 tag_name += 10;
4231 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4232 err = got_error_from_errno("asprintf");
4233 goto done;
4236 err = got_ref_open(&ref, repo, refname, 0);
4237 if (err == NULL) {
4238 err = got_error(GOT_ERR_TAG_EXISTS);
4239 goto done;
4240 } else if (err->code != GOT_ERR_NOT_REF)
4241 goto done;
4243 if (tagmsg_arg == NULL) {
4244 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4245 tag_name, got_repo_get_path(repo));
4246 if (err) {
4247 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4248 tagmsg_path != NULL)
4249 preserve_tagmsg = 1;
4250 goto done;
4254 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4255 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4256 if (err) {
4257 if (tagmsg_path)
4258 preserve_tagmsg = 1;
4259 goto done;
4262 err = got_ref_alloc(&ref, refname, tag_id);
4263 if (err) {
4264 if (tagmsg_path)
4265 preserve_tagmsg = 1;
4266 goto done;
4269 err = got_ref_write(ref, repo);
4270 if (err) {
4271 if (tagmsg_path)
4272 preserve_tagmsg = 1;
4273 goto done;
4276 err = got_object_id_str(&tag_id_str, tag_id);
4277 if (err) {
4278 if (tagmsg_path)
4279 preserve_tagmsg = 1;
4280 goto done;
4282 printf("Created tag %s\n", tag_id_str);
4283 done:
4284 if (preserve_tagmsg) {
4285 fprintf(stderr, "%s: tag message preserved in %s\n",
4286 getprogname(), tagmsg_path);
4287 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4288 err = got_error_from_errno2("unlink", tagmsg_path);
4289 free(tag_id_str);
4290 if (ref)
4291 got_ref_close(ref);
4292 free(commit_id);
4293 free(commit_id_str);
4294 free(refname);
4295 free(tagmsg);
4296 free(tagmsg_path);
4297 free(tagger);
4298 return err;
4301 static const struct got_error *
4302 cmd_tag(int argc, char *argv[])
4304 const struct got_error *error = NULL;
4305 struct got_repository *repo = NULL;
4306 struct got_worktree *worktree = NULL;
4307 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4308 char *gitconfig_path = NULL;
4309 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4310 int ch, do_list = 0;
4312 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4313 switch (ch) {
4314 case 'c':
4315 commit_id_arg = optarg;
4316 break;
4317 case 'm':
4318 tagmsg = optarg;
4319 break;
4320 case 'r':
4321 repo_path = realpath(optarg, NULL);
4322 if (repo_path == NULL)
4323 return got_error_from_errno2("realpath",
4324 optarg);
4325 got_path_strip_trailing_slashes(repo_path);
4326 break;
4327 case 'l':
4328 do_list = 1;
4329 break;
4330 default:
4331 usage_tag();
4332 /* NOTREACHED */
4336 argc -= optind;
4337 argv += optind;
4339 if (do_list) {
4340 if (commit_id_arg != NULL)
4341 errx(1, "-c option can only be used when creating a tag");
4342 if (tagmsg)
4343 errx(1, "-l and -m options are mutually exclusive");
4344 if (argc > 0)
4345 usage_tag();
4346 } else if (argc != 1)
4347 usage_tag();
4349 tag_name = argv[0];
4351 #ifndef PROFILE
4352 if (do_list) {
4353 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4354 NULL) == -1)
4355 err(1, "pledge");
4356 } else {
4357 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4358 "sendfd unveil", NULL) == -1)
4359 err(1, "pledge");
4361 #endif
4362 cwd = getcwd(NULL, 0);
4363 if (cwd == NULL) {
4364 error = got_error_from_errno("getcwd");
4365 goto done;
4368 if (repo_path == NULL) {
4369 error = got_worktree_open(&worktree, cwd);
4370 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4371 goto done;
4372 else
4373 error = NULL;
4374 if (worktree) {
4375 repo_path =
4376 strdup(got_worktree_get_repo_path(worktree));
4377 if (repo_path == NULL)
4378 error = got_error_from_errno("strdup");
4379 if (error)
4380 goto done;
4381 } else {
4382 repo_path = strdup(cwd);
4383 if (repo_path == NULL) {
4384 error = got_error_from_errno("strdup");
4385 goto done;
4390 if (do_list) {
4391 error = got_repo_open(&repo, repo_path, NULL);
4392 if (error != NULL)
4393 goto done;
4394 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4395 if (error)
4396 goto done;
4397 error = list_tags(repo, worktree);
4398 } else {
4399 error = get_gitconfig_path(&gitconfig_path);
4400 if (error)
4401 goto done;
4402 error = got_repo_open(&repo, repo_path, gitconfig_path);
4403 if (error != NULL)
4404 goto done;
4406 if (tagmsg) {
4407 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4408 if (error)
4409 goto done;
4412 if (commit_id_arg == NULL) {
4413 struct got_reference *head_ref;
4414 struct got_object_id *commit_id;
4415 error = got_ref_open(&head_ref, repo,
4416 worktree ? got_worktree_get_head_ref_name(worktree)
4417 : GOT_REF_HEAD, 0);
4418 if (error)
4419 goto done;
4420 error = got_ref_resolve(&commit_id, repo, head_ref);
4421 got_ref_close(head_ref);
4422 if (error)
4423 goto done;
4424 error = got_object_id_str(&commit_id_str, commit_id);
4425 free(commit_id);
4426 if (error)
4427 goto done;
4430 error = add_tag(repo, tag_name,
4431 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4433 done:
4434 if (repo)
4435 got_repo_close(repo);
4436 if (worktree)
4437 got_worktree_close(worktree);
4438 free(cwd);
4439 free(repo_path);
4440 free(gitconfig_path);
4441 free(commit_id_str);
4442 return error;
4445 __dead static void
4446 usage_add(void)
4448 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4449 getprogname());
4450 exit(1);
4453 static const struct got_error *
4454 add_progress(void *arg, unsigned char status, const char *path)
4456 while (path[0] == '/')
4457 path++;
4458 printf("%c %s\n", status, path);
4459 return NULL;
4462 static const struct got_error *
4463 cmd_add(int argc, char *argv[])
4465 const struct got_error *error = NULL;
4466 struct got_repository *repo = NULL;
4467 struct got_worktree *worktree = NULL;
4468 char *cwd = NULL;
4469 struct got_pathlist_head paths;
4470 struct got_pathlist_entry *pe;
4471 int ch, can_recurse = 0, no_ignores = 0;
4473 TAILQ_INIT(&paths);
4475 while ((ch = getopt(argc, argv, "IR")) != -1) {
4476 switch (ch) {
4477 case 'I':
4478 no_ignores = 1;
4479 break;
4480 case 'R':
4481 can_recurse = 1;
4482 break;
4483 default:
4484 usage_add();
4485 /* NOTREACHED */
4489 argc -= optind;
4490 argv += optind;
4492 #ifndef PROFILE
4493 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4494 NULL) == -1)
4495 err(1, "pledge");
4496 #endif
4497 if (argc < 1)
4498 usage_add();
4500 cwd = getcwd(NULL, 0);
4501 if (cwd == NULL) {
4502 error = got_error_from_errno("getcwd");
4503 goto done;
4506 error = got_worktree_open(&worktree, cwd);
4507 if (error)
4508 goto done;
4510 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4511 NULL);
4512 if (error != NULL)
4513 goto done;
4515 error = apply_unveil(got_repo_get_path(repo), 1,
4516 got_worktree_get_root_path(worktree));
4517 if (error)
4518 goto done;
4520 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4521 if (error)
4522 goto done;
4524 if (!can_recurse && no_ignores) {
4525 error = got_error_msg(GOT_ERR_BAD_PATH,
4526 "disregarding ignores requires -R option");
4527 goto done;
4531 if (!can_recurse) {
4532 char *ondisk_path;
4533 struct stat sb;
4534 TAILQ_FOREACH(pe, &paths, entry) {
4535 if (asprintf(&ondisk_path, "%s/%s",
4536 got_worktree_get_root_path(worktree),
4537 pe->path) == -1) {
4538 error = got_error_from_errno("asprintf");
4539 goto done;
4541 if (lstat(ondisk_path, &sb) == -1) {
4542 if (errno == ENOENT) {
4543 free(ondisk_path);
4544 continue;
4546 error = got_error_from_errno2("lstat",
4547 ondisk_path);
4548 free(ondisk_path);
4549 goto done;
4551 free(ondisk_path);
4552 if (S_ISDIR(sb.st_mode)) {
4553 error = got_error_msg(GOT_ERR_BAD_PATH,
4554 "adding directories requires -R option");
4555 goto done;
4560 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4561 NULL, repo, no_ignores);
4562 done:
4563 if (repo)
4564 got_repo_close(repo);
4565 if (worktree)
4566 got_worktree_close(worktree);
4567 TAILQ_FOREACH(pe, &paths, entry)
4568 free((char *)pe->path);
4569 got_pathlist_free(&paths);
4570 free(cwd);
4571 return error;
4574 __dead static void
4575 usage_remove(void)
4577 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4578 getprogname());
4579 exit(1);
4582 static const struct got_error *
4583 print_remove_status(void *arg, unsigned char status,
4584 unsigned char staged_status, const char *path)
4586 while (path[0] == '/')
4587 path++;
4588 if (status == GOT_STATUS_NONEXISTENT)
4589 return NULL;
4590 if (status == staged_status && (status == GOT_STATUS_DELETE))
4591 status = GOT_STATUS_NO_CHANGE;
4592 printf("%c%c %s\n", status, staged_status, path);
4593 return NULL;
4596 static const struct got_error *
4597 cmd_remove(int argc, char *argv[])
4599 const struct got_error *error = NULL;
4600 struct got_worktree *worktree = NULL;
4601 struct got_repository *repo = NULL;
4602 char *cwd = NULL;
4603 struct got_pathlist_head paths;
4604 struct got_pathlist_entry *pe;
4605 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4607 TAILQ_INIT(&paths);
4609 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4610 switch (ch) {
4611 case 'f':
4612 delete_local_mods = 1;
4613 break;
4614 case 'k':
4615 keep_on_disk = 1;
4616 break;
4617 case 'R':
4618 can_recurse = 1;
4619 break;
4620 default:
4621 usage_remove();
4622 /* NOTREACHED */
4626 argc -= optind;
4627 argv += optind;
4629 #ifndef PROFILE
4630 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4631 NULL) == -1)
4632 err(1, "pledge");
4633 #endif
4634 if (argc < 1)
4635 usage_remove();
4637 cwd = getcwd(NULL, 0);
4638 if (cwd == NULL) {
4639 error = got_error_from_errno("getcwd");
4640 goto done;
4642 error = got_worktree_open(&worktree, cwd);
4643 if (error)
4644 goto done;
4646 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4647 NULL);
4648 if (error)
4649 goto done;
4651 error = apply_unveil(got_repo_get_path(repo), 1,
4652 got_worktree_get_root_path(worktree));
4653 if (error)
4654 goto done;
4656 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4657 if (error)
4658 goto done;
4660 if (!can_recurse) {
4661 char *ondisk_path;
4662 struct stat sb;
4663 TAILQ_FOREACH(pe, &paths, entry) {
4664 if (asprintf(&ondisk_path, "%s/%s",
4665 got_worktree_get_root_path(worktree),
4666 pe->path) == -1) {
4667 error = got_error_from_errno("asprintf");
4668 goto done;
4670 if (lstat(ondisk_path, &sb) == -1) {
4671 if (errno == ENOENT) {
4672 free(ondisk_path);
4673 continue;
4675 error = got_error_from_errno2("lstat",
4676 ondisk_path);
4677 free(ondisk_path);
4678 goto done;
4680 free(ondisk_path);
4681 if (S_ISDIR(sb.st_mode)) {
4682 error = got_error_msg(GOT_ERR_BAD_PATH,
4683 "removing directories requires -R option");
4684 goto done;
4689 error = got_worktree_schedule_delete(worktree, &paths,
4690 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4691 done:
4692 if (repo)
4693 got_repo_close(repo);
4694 if (worktree)
4695 got_worktree_close(worktree);
4696 TAILQ_FOREACH(pe, &paths, entry)
4697 free((char *)pe->path);
4698 got_pathlist_free(&paths);
4699 free(cwd);
4700 return error;
4703 __dead static void
4704 usage_revert(void)
4706 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4707 "path ...\n", getprogname());
4708 exit(1);
4711 static const struct got_error *
4712 revert_progress(void *arg, unsigned char status, const char *path)
4714 if (status == GOT_STATUS_UNVERSIONED)
4715 return NULL;
4717 while (path[0] == '/')
4718 path++;
4719 printf("%c %s\n", status, path);
4720 return NULL;
4723 struct choose_patch_arg {
4724 FILE *patch_script_file;
4725 const char *action;
4728 static const struct got_error *
4729 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4730 int nchanges, const char *action)
4732 char *line = NULL;
4733 size_t linesize = 0;
4734 ssize_t linelen;
4736 switch (status) {
4737 case GOT_STATUS_ADD:
4738 printf("A %s\n%s this addition? [y/n] ", path, action);
4739 break;
4740 case GOT_STATUS_DELETE:
4741 printf("D %s\n%s this deletion? [y/n] ", path, action);
4742 break;
4743 case GOT_STATUS_MODIFY:
4744 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4745 return got_error_from_errno("fseek");
4746 printf(GOT_COMMIT_SEP_STR);
4747 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4748 printf("%s", line);
4749 if (ferror(patch_file))
4750 return got_error_from_errno("getline");
4751 printf(GOT_COMMIT_SEP_STR);
4752 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4753 path, n, nchanges, action);
4754 break;
4755 default:
4756 return got_error_path(path, GOT_ERR_FILE_STATUS);
4759 return NULL;
4762 static const struct got_error *
4763 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4764 FILE *patch_file, int n, int nchanges)
4766 const struct got_error *err = NULL;
4767 char *line = NULL;
4768 size_t linesize = 0;
4769 ssize_t linelen;
4770 int resp = ' ';
4771 struct choose_patch_arg *a = arg;
4773 *choice = GOT_PATCH_CHOICE_NONE;
4775 if (a->patch_script_file) {
4776 char *nl;
4777 err = show_change(status, path, patch_file, n, nchanges,
4778 a->action);
4779 if (err)
4780 return err;
4781 linelen = getline(&line, &linesize, a->patch_script_file);
4782 if (linelen == -1) {
4783 if (ferror(a->patch_script_file))
4784 return got_error_from_errno("getline");
4785 return NULL;
4787 nl = strchr(line, '\n');
4788 if (nl)
4789 *nl = '\0';
4790 if (strcmp(line, "y") == 0) {
4791 *choice = GOT_PATCH_CHOICE_YES;
4792 printf("y\n");
4793 } else if (strcmp(line, "n") == 0) {
4794 *choice = GOT_PATCH_CHOICE_NO;
4795 printf("n\n");
4796 } else if (strcmp(line, "q") == 0 &&
4797 status == GOT_STATUS_MODIFY) {
4798 *choice = GOT_PATCH_CHOICE_QUIT;
4799 printf("q\n");
4800 } else
4801 printf("invalid response '%s'\n", line);
4802 free(line);
4803 return NULL;
4806 while (resp != 'y' && resp != 'n' && resp != 'q') {
4807 err = show_change(status, path, patch_file, n, nchanges,
4808 a->action);
4809 if (err)
4810 return err;
4811 resp = getchar();
4812 if (resp == '\n')
4813 resp = getchar();
4814 if (status == GOT_STATUS_MODIFY) {
4815 if (resp != 'y' && resp != 'n' && resp != 'q') {
4816 printf("invalid response '%c'\n", resp);
4817 resp = ' ';
4819 } else if (resp != 'y' && resp != 'n') {
4820 printf("invalid response '%c'\n", resp);
4821 resp = ' ';
4825 if (resp == 'y')
4826 *choice = GOT_PATCH_CHOICE_YES;
4827 else if (resp == 'n')
4828 *choice = GOT_PATCH_CHOICE_NO;
4829 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4830 *choice = GOT_PATCH_CHOICE_QUIT;
4832 return NULL;
4836 static const struct got_error *
4837 cmd_revert(int argc, char *argv[])
4839 const struct got_error *error = NULL;
4840 struct got_worktree *worktree = NULL;
4841 struct got_repository *repo = NULL;
4842 char *cwd = NULL, *path = NULL;
4843 struct got_pathlist_head paths;
4844 struct got_pathlist_entry *pe;
4845 int ch, can_recurse = 0, pflag = 0;
4846 FILE *patch_script_file = NULL;
4847 const char *patch_script_path = NULL;
4848 struct choose_patch_arg cpa;
4850 TAILQ_INIT(&paths);
4852 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4853 switch (ch) {
4854 case 'p':
4855 pflag = 1;
4856 break;
4857 case 'F':
4858 patch_script_path = optarg;
4859 break;
4860 case 'R':
4861 can_recurse = 1;
4862 break;
4863 default:
4864 usage_revert();
4865 /* NOTREACHED */
4869 argc -= optind;
4870 argv += optind;
4872 #ifndef PROFILE
4873 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4874 "unveil", NULL) == -1)
4875 err(1, "pledge");
4876 #endif
4877 if (argc < 1)
4878 usage_revert();
4879 if (patch_script_path && !pflag)
4880 errx(1, "-F option can only be used together with -p option");
4882 cwd = getcwd(NULL, 0);
4883 if (cwd == NULL) {
4884 error = got_error_from_errno("getcwd");
4885 goto done;
4887 error = got_worktree_open(&worktree, cwd);
4888 if (error)
4889 goto done;
4891 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4892 NULL);
4893 if (error != NULL)
4894 goto done;
4896 if (patch_script_path) {
4897 patch_script_file = fopen(patch_script_path, "r");
4898 if (patch_script_file == NULL) {
4899 error = got_error_from_errno2("fopen",
4900 patch_script_path);
4901 goto done;
4904 error = apply_unveil(got_repo_get_path(repo), 1,
4905 got_worktree_get_root_path(worktree));
4906 if (error)
4907 goto done;
4909 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4910 if (error)
4911 goto done;
4913 if (!can_recurse) {
4914 char *ondisk_path;
4915 struct stat sb;
4916 TAILQ_FOREACH(pe, &paths, entry) {
4917 if (asprintf(&ondisk_path, "%s/%s",
4918 got_worktree_get_root_path(worktree),
4919 pe->path) == -1) {
4920 error = got_error_from_errno("asprintf");
4921 goto done;
4923 if (lstat(ondisk_path, &sb) == -1) {
4924 if (errno == ENOENT) {
4925 free(ondisk_path);
4926 continue;
4928 error = got_error_from_errno2("lstat",
4929 ondisk_path);
4930 free(ondisk_path);
4931 goto done;
4933 free(ondisk_path);
4934 if (S_ISDIR(sb.st_mode)) {
4935 error = got_error_msg(GOT_ERR_BAD_PATH,
4936 "reverting directories requires -R option");
4937 goto done;
4942 cpa.patch_script_file = patch_script_file;
4943 cpa.action = "revert";
4944 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4945 pflag ? choose_patch : NULL, &cpa, repo);
4946 done:
4947 if (patch_script_file && fclose(patch_script_file) == EOF &&
4948 error == NULL)
4949 error = got_error_from_errno2("fclose", patch_script_path);
4950 if (repo)
4951 got_repo_close(repo);
4952 if (worktree)
4953 got_worktree_close(worktree);
4954 free(path);
4955 free(cwd);
4956 return error;
4959 __dead static void
4960 usage_commit(void)
4962 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4963 getprogname());
4964 exit(1);
4967 struct collect_commit_logmsg_arg {
4968 const char *cmdline_log;
4969 const char *editor;
4970 const char *worktree_path;
4971 const char *branch_name;
4972 const char *repo_path;
4973 char *logmsg_path;
4977 static const struct got_error *
4978 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4979 void *arg)
4981 char *initial_content = NULL;
4982 struct got_pathlist_entry *pe;
4983 const struct got_error *err = NULL;
4984 char *template = NULL;
4985 struct collect_commit_logmsg_arg *a = arg;
4986 int fd;
4987 size_t len;
4989 /* if a message was specified on the command line, just use it */
4990 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4991 len = strlen(a->cmdline_log) + 1;
4992 *logmsg = malloc(len + 1);
4993 if (*logmsg == NULL)
4994 return got_error_from_errno("malloc");
4995 strlcpy(*logmsg, a->cmdline_log, len);
4996 return NULL;
4999 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5000 return got_error_from_errno("asprintf");
5002 if (asprintf(&initial_content,
5003 "\n# changes to be committed on branch %s:\n",
5004 a->branch_name) == -1)
5005 return got_error_from_errno("asprintf");
5007 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5008 if (err)
5009 goto done;
5011 dprintf(fd, initial_content);
5013 TAILQ_FOREACH(pe, commitable_paths, entry) {
5014 struct got_commitable *ct = pe->data;
5015 dprintf(fd, "# %c %s\n",
5016 got_commitable_get_status(ct),
5017 got_commitable_get_path(ct));
5019 close(fd);
5021 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5022 done:
5023 free(initial_content);
5024 free(template);
5026 /* Editor is done; we can now apply unveil(2) */
5027 if (err == NULL) {
5028 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5029 if (err) {
5030 free(*logmsg);
5031 *logmsg = NULL;
5034 return err;
5037 static const struct got_error *
5038 cmd_commit(int argc, char *argv[])
5040 const struct got_error *error = NULL;
5041 struct got_worktree *worktree = NULL;
5042 struct got_repository *repo = NULL;
5043 char *cwd = NULL, *id_str = NULL;
5044 struct got_object_id *id = NULL;
5045 const char *logmsg = NULL;
5046 struct collect_commit_logmsg_arg cl_arg;
5047 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5048 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5049 struct got_pathlist_head paths;
5051 TAILQ_INIT(&paths);
5052 cl_arg.logmsg_path = NULL;
5054 while ((ch = getopt(argc, argv, "m:")) != -1) {
5055 switch (ch) {
5056 case 'm':
5057 logmsg = optarg;
5058 break;
5059 default:
5060 usage_commit();
5061 /* NOTREACHED */
5065 argc -= optind;
5066 argv += optind;
5068 #ifndef PROFILE
5069 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5070 "unveil", NULL) == -1)
5071 err(1, "pledge");
5072 #endif
5073 cwd = getcwd(NULL, 0);
5074 if (cwd == NULL) {
5075 error = got_error_from_errno("getcwd");
5076 goto done;
5078 error = got_worktree_open(&worktree, cwd);
5079 if (error)
5080 goto done;
5082 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5083 if (error)
5084 goto done;
5085 if (rebase_in_progress) {
5086 error = got_error(GOT_ERR_REBASING);
5087 goto done;
5090 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5091 worktree);
5092 if (error)
5093 goto done;
5095 error = get_gitconfig_path(&gitconfig_path);
5096 if (error)
5097 goto done;
5098 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5099 gitconfig_path);
5100 if (error != NULL)
5101 goto done;
5103 error = get_author(&author, repo);
5104 if (error)
5105 return error;
5108 * unveil(2) traverses exec(2); if an editor is used we have
5109 * to apply unveil after the log message has been written.
5111 if (logmsg == NULL || strlen(logmsg) == 0)
5112 error = get_editor(&editor);
5113 else
5114 error = apply_unveil(got_repo_get_path(repo), 0,
5115 got_worktree_get_root_path(worktree));
5116 if (error)
5117 goto done;
5119 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5120 if (error)
5121 goto done;
5123 cl_arg.editor = editor;
5124 cl_arg.cmdline_log = logmsg;
5125 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5126 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5127 if (!histedit_in_progress) {
5128 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5129 error = got_error(GOT_ERR_COMMIT_BRANCH);
5130 goto done;
5132 cl_arg.branch_name += 11;
5134 cl_arg.repo_path = got_repo_get_path(repo);
5135 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5136 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5137 if (error) {
5138 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5139 cl_arg.logmsg_path != NULL)
5140 preserve_logmsg = 1;
5141 goto done;
5144 error = got_object_id_str(&id_str, id);
5145 if (error)
5146 goto done;
5147 printf("Created commit %s\n", id_str);
5148 done:
5149 if (preserve_logmsg) {
5150 fprintf(stderr, "%s: log message preserved in %s\n",
5151 getprogname(), cl_arg.logmsg_path);
5152 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5153 error == NULL)
5154 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5155 free(cl_arg.logmsg_path);
5156 if (repo)
5157 got_repo_close(repo);
5158 if (worktree)
5159 got_worktree_close(worktree);
5160 free(cwd);
5161 free(id_str);
5162 free(gitconfig_path);
5163 free(editor);
5164 free(author);
5165 return error;
5168 __dead static void
5169 usage_cherrypick(void)
5171 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5172 exit(1);
5175 static const struct got_error *
5176 cmd_cherrypick(int argc, char *argv[])
5178 const struct got_error *error = NULL;
5179 struct got_worktree *worktree = NULL;
5180 struct got_repository *repo = NULL;
5181 char *cwd = NULL, *commit_id_str = NULL;
5182 struct got_object_id *commit_id = NULL;
5183 struct got_commit_object *commit = NULL;
5184 struct got_object_qid *pid;
5185 struct got_reference *head_ref = NULL;
5186 int ch, did_something = 0;
5188 while ((ch = getopt(argc, argv, "")) != -1) {
5189 switch (ch) {
5190 default:
5191 usage_cherrypick();
5192 /* NOTREACHED */
5196 argc -= optind;
5197 argv += optind;
5199 #ifndef PROFILE
5200 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5201 "unveil", NULL) == -1)
5202 err(1, "pledge");
5203 #endif
5204 if (argc != 1)
5205 usage_cherrypick();
5207 cwd = getcwd(NULL, 0);
5208 if (cwd == NULL) {
5209 error = got_error_from_errno("getcwd");
5210 goto done;
5212 error = got_worktree_open(&worktree, cwd);
5213 if (error)
5214 goto done;
5216 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5217 NULL);
5218 if (error != NULL)
5219 goto done;
5221 error = apply_unveil(got_repo_get_path(repo), 0,
5222 got_worktree_get_root_path(worktree));
5223 if (error)
5224 goto done;
5226 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5227 GOT_OBJ_TYPE_COMMIT, repo);
5228 if (error != NULL) {
5229 struct got_reference *ref;
5230 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5231 goto done;
5232 error = got_ref_open(&ref, repo, argv[0], 0);
5233 if (error != NULL)
5234 goto done;
5235 error = got_ref_resolve(&commit_id, repo, ref);
5236 got_ref_close(ref);
5237 if (error != NULL)
5238 goto done;
5240 error = got_object_id_str(&commit_id_str, commit_id);
5241 if (error)
5242 goto done;
5244 error = got_ref_open(&head_ref, repo,
5245 got_worktree_get_head_ref_name(worktree), 0);
5246 if (error != NULL)
5247 goto done;
5249 error = check_same_branch(commit_id, head_ref, NULL, repo);
5250 if (error) {
5251 if (error->code != GOT_ERR_ANCESTRY)
5252 goto done;
5253 error = NULL;
5254 } else {
5255 error = got_error(GOT_ERR_SAME_BRANCH);
5256 goto done;
5259 error = got_object_open_as_commit(&commit, repo, commit_id);
5260 if (error)
5261 goto done;
5262 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5263 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5264 commit_id, repo, update_progress, &did_something, check_cancelled,
5265 NULL);
5266 if (error != NULL)
5267 goto done;
5269 if (did_something)
5270 printf("Merged commit %s\n", commit_id_str);
5271 done:
5272 if (commit)
5273 got_object_commit_close(commit);
5274 free(commit_id_str);
5275 if (head_ref)
5276 got_ref_close(head_ref);
5277 if (worktree)
5278 got_worktree_close(worktree);
5279 if (repo)
5280 got_repo_close(repo);
5281 return error;
5284 __dead static void
5285 usage_backout(void)
5287 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5288 exit(1);
5291 static const struct got_error *
5292 cmd_backout(int argc, char *argv[])
5294 const struct got_error *error = NULL;
5295 struct got_worktree *worktree = NULL;
5296 struct got_repository *repo = NULL;
5297 char *cwd = NULL, *commit_id_str = NULL;
5298 struct got_object_id *commit_id = NULL;
5299 struct got_commit_object *commit = NULL;
5300 struct got_object_qid *pid;
5301 struct got_reference *head_ref = NULL;
5302 int ch, did_something = 0;
5304 while ((ch = getopt(argc, argv, "")) != -1) {
5305 switch (ch) {
5306 default:
5307 usage_backout();
5308 /* NOTREACHED */
5312 argc -= optind;
5313 argv += optind;
5315 #ifndef PROFILE
5316 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5317 "unveil", NULL) == -1)
5318 err(1, "pledge");
5319 #endif
5320 if (argc != 1)
5321 usage_backout();
5323 cwd = getcwd(NULL, 0);
5324 if (cwd == NULL) {
5325 error = got_error_from_errno("getcwd");
5326 goto done;
5328 error = got_worktree_open(&worktree, cwd);
5329 if (error)
5330 goto done;
5332 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5333 NULL);
5334 if (error != NULL)
5335 goto done;
5337 error = apply_unveil(got_repo_get_path(repo), 0,
5338 got_worktree_get_root_path(worktree));
5339 if (error)
5340 goto done;
5342 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5343 GOT_OBJ_TYPE_COMMIT, repo);
5344 if (error != NULL) {
5345 struct got_reference *ref;
5346 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5347 goto done;
5348 error = got_ref_open(&ref, repo, argv[0], 0);
5349 if (error != NULL)
5350 goto done;
5351 error = got_ref_resolve(&commit_id, repo, ref);
5352 got_ref_close(ref);
5353 if (error != NULL)
5354 goto done;
5356 error = got_object_id_str(&commit_id_str, commit_id);
5357 if (error)
5358 goto done;
5360 error = got_ref_open(&head_ref, repo,
5361 got_worktree_get_head_ref_name(worktree), 0);
5362 if (error != NULL)
5363 goto done;
5365 error = check_same_branch(commit_id, head_ref, NULL, repo);
5366 if (error)
5367 goto done;
5369 error = got_object_open_as_commit(&commit, repo, commit_id);
5370 if (error)
5371 goto done;
5372 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5373 if (pid == NULL) {
5374 error = got_error(GOT_ERR_ROOT_COMMIT);
5375 goto done;
5378 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5379 update_progress, &did_something, check_cancelled, NULL);
5380 if (error != NULL)
5381 goto done;
5383 if (did_something)
5384 printf("Backed out commit %s\n", commit_id_str);
5385 done:
5386 if (commit)
5387 got_object_commit_close(commit);
5388 free(commit_id_str);
5389 if (head_ref)
5390 got_ref_close(head_ref);
5391 if (worktree)
5392 got_worktree_close(worktree);
5393 if (repo)
5394 got_repo_close(repo);
5395 return error;
5398 __dead static void
5399 usage_rebase(void)
5401 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5402 getprogname());
5403 exit(1);
5406 void
5407 trim_logmsg(char *logmsg, int limit)
5409 char *nl;
5410 size_t len;
5412 len = strlen(logmsg);
5413 if (len > limit)
5414 len = limit;
5415 logmsg[len] = '\0';
5416 nl = strchr(logmsg, '\n');
5417 if (nl)
5418 *nl = '\0';
5421 static const struct got_error *
5422 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5424 const struct got_error *err;
5425 char *logmsg0 = NULL;
5426 const char *s;
5428 err = got_object_commit_get_logmsg(&logmsg0, commit);
5429 if (err)
5430 return err;
5432 s = logmsg0;
5433 while (isspace((unsigned char)s[0]))
5434 s++;
5436 *logmsg = strdup(s);
5437 if (*logmsg == NULL) {
5438 err = got_error_from_errno("strdup");
5439 goto done;
5442 trim_logmsg(*logmsg, limit);
5443 done:
5444 free(logmsg0);
5445 return err;
5448 static const struct got_error *
5449 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5451 const struct got_error *err;
5452 struct got_commit_object *commit = NULL;
5453 char *id_str = NULL, *logmsg = NULL;
5455 err = got_object_open_as_commit(&commit, repo, id);
5456 if (err)
5457 return err;
5459 err = got_object_id_str(&id_str, id);
5460 if (err)
5461 goto done;
5463 id_str[12] = '\0';
5465 err = get_short_logmsg(&logmsg, 42, commit);
5466 if (err)
5467 goto done;
5469 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5470 done:
5471 free(id_str);
5472 got_object_commit_close(commit);
5473 free(logmsg);
5474 return err;
5477 static const struct got_error *
5478 show_rebase_progress(struct got_commit_object *commit,
5479 struct got_object_id *old_id, struct got_object_id *new_id)
5481 const struct got_error *err;
5482 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5484 err = got_object_id_str(&old_id_str, old_id);
5485 if (err)
5486 goto done;
5488 if (new_id) {
5489 err = got_object_id_str(&new_id_str, new_id);
5490 if (err)
5491 goto done;
5494 old_id_str[12] = '\0';
5495 if (new_id_str)
5496 new_id_str[12] = '\0';
5498 err = get_short_logmsg(&logmsg, 42, commit);
5499 if (err)
5500 goto done;
5502 printf("%s -> %s: %s\n", old_id_str,
5503 new_id_str ? new_id_str : "no-op change", logmsg);
5504 done:
5505 free(old_id_str);
5506 free(new_id_str);
5507 free(logmsg);
5508 return err;
5511 static const struct got_error *
5512 rebase_progress(void *arg, unsigned char status, const char *path)
5514 unsigned char *rebase_status = arg;
5516 while (path[0] == '/')
5517 path++;
5518 printf("%c %s\n", status, path);
5520 if (*rebase_status == GOT_STATUS_CONFLICT)
5521 return NULL;
5522 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5523 *rebase_status = status;
5524 return NULL;
5527 static const struct got_error *
5528 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5529 struct got_reference *branch, struct got_reference *new_base_branch,
5530 struct got_reference *tmp_branch, struct got_repository *repo)
5532 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5533 return got_worktree_rebase_complete(worktree, fileindex,
5534 new_base_branch, tmp_branch, branch, repo);
5537 static const struct got_error *
5538 rebase_commit(struct got_pathlist_head *merged_paths,
5539 struct got_worktree *worktree, struct got_fileindex *fileindex,
5540 struct got_reference *tmp_branch,
5541 struct got_object_id *commit_id, struct got_repository *repo)
5543 const struct got_error *error;
5544 struct got_commit_object *commit;
5545 struct got_object_id *new_commit_id;
5547 error = got_object_open_as_commit(&commit, repo, commit_id);
5548 if (error)
5549 return error;
5551 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5552 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5553 if (error) {
5554 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5555 goto done;
5556 error = show_rebase_progress(commit, commit_id, NULL);
5557 } else {
5558 error = show_rebase_progress(commit, commit_id, new_commit_id);
5559 free(new_commit_id);
5561 done:
5562 got_object_commit_close(commit);
5563 return error;
5566 struct check_path_prefix_arg {
5567 const char *path_prefix;
5568 size_t len;
5569 int errcode;
5572 static const struct got_error *
5573 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5574 struct got_blob_object *blob2, struct got_object_id *id1,
5575 struct got_object_id *id2, const char *path1, const char *path2,
5576 mode_t mode1, mode_t mode2, struct got_repository *repo)
5578 struct check_path_prefix_arg *a = arg;
5580 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5581 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5582 return got_error(a->errcode);
5584 return NULL;
5587 static const struct got_error *
5588 check_path_prefix(struct got_object_id *parent_id,
5589 struct got_object_id *commit_id, const char *path_prefix,
5590 int errcode, struct got_repository *repo)
5592 const struct got_error *err;
5593 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5594 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5595 struct check_path_prefix_arg cpp_arg;
5597 if (got_path_is_root_dir(path_prefix))
5598 return NULL;
5600 err = got_object_open_as_commit(&commit, repo, commit_id);
5601 if (err)
5602 goto done;
5604 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5605 if (err)
5606 goto done;
5608 err = got_object_open_as_tree(&tree1, repo,
5609 got_object_commit_get_tree_id(parent_commit));
5610 if (err)
5611 goto done;
5613 err = got_object_open_as_tree(&tree2, repo,
5614 got_object_commit_get_tree_id(commit));
5615 if (err)
5616 goto done;
5618 cpp_arg.path_prefix = path_prefix;
5619 while (cpp_arg.path_prefix[0] == '/')
5620 cpp_arg.path_prefix++;
5621 cpp_arg.len = strlen(cpp_arg.path_prefix);
5622 cpp_arg.errcode = errcode;
5623 err = got_diff_tree(tree1, tree2, "", "", repo,
5624 check_path_prefix_in_diff, &cpp_arg, 0);
5625 done:
5626 if (tree1)
5627 got_object_tree_close(tree1);
5628 if (tree2)
5629 got_object_tree_close(tree2);
5630 if (commit)
5631 got_object_commit_close(commit);
5632 if (parent_commit)
5633 got_object_commit_close(parent_commit);
5634 return err;
5637 static const struct got_error *
5638 collect_commits(struct got_object_id_queue *commits,
5639 struct got_object_id *initial_commit_id,
5640 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5641 const char *path_prefix, int path_prefix_errcode,
5642 struct got_repository *repo)
5644 const struct got_error *err = NULL;
5645 struct got_commit_graph *graph = NULL;
5646 struct got_object_id *parent_id = NULL;
5647 struct got_object_qid *qid;
5648 struct got_object_id *commit_id = initial_commit_id;
5650 err = got_commit_graph_open(&graph, "/", 1);
5651 if (err)
5652 return err;
5654 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5655 check_cancelled, NULL);
5656 if (err)
5657 goto done;
5658 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5659 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5660 check_cancelled, NULL);
5661 if (err) {
5662 if (err->code == GOT_ERR_ITER_COMPLETED) {
5663 err = got_error_msg(GOT_ERR_ANCESTRY,
5664 "ran out of commits to rebase before "
5665 "youngest common ancestor commit has "
5666 "been reached?!?");
5668 goto done;
5669 } else {
5670 err = check_path_prefix(parent_id, commit_id,
5671 path_prefix, path_prefix_errcode, repo);
5672 if (err)
5673 goto done;
5675 err = got_object_qid_alloc(&qid, commit_id);
5676 if (err)
5677 goto done;
5678 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5679 commit_id = parent_id;
5682 done:
5683 got_commit_graph_close(graph);
5684 return err;
5687 static const struct got_error *
5688 cmd_rebase(int argc, char *argv[])
5690 const struct got_error *error = NULL;
5691 struct got_worktree *worktree = NULL;
5692 struct got_repository *repo = NULL;
5693 struct got_fileindex *fileindex = NULL;
5694 char *cwd = NULL;
5695 struct got_reference *branch = NULL;
5696 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5697 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5698 struct got_object_id *resume_commit_id = NULL;
5699 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5700 struct got_commit_object *commit = NULL;
5701 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5702 int histedit_in_progress = 0;
5703 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5704 struct got_object_id_queue commits;
5705 struct got_pathlist_head merged_paths;
5706 const struct got_object_id_queue *parent_ids;
5707 struct got_object_qid *qid, *pid;
5709 SIMPLEQ_INIT(&commits);
5710 TAILQ_INIT(&merged_paths);
5712 while ((ch = getopt(argc, argv, "ac")) != -1) {
5713 switch (ch) {
5714 case 'a':
5715 abort_rebase = 1;
5716 break;
5717 case 'c':
5718 continue_rebase = 1;
5719 break;
5720 default:
5721 usage_rebase();
5722 /* NOTREACHED */
5726 argc -= optind;
5727 argv += optind;
5729 #ifndef PROFILE
5730 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5731 "unveil", NULL) == -1)
5732 err(1, "pledge");
5733 #endif
5734 if (abort_rebase && continue_rebase)
5735 usage_rebase();
5736 else if (abort_rebase || continue_rebase) {
5737 if (argc != 0)
5738 usage_rebase();
5739 } else if (argc != 1)
5740 usage_rebase();
5742 cwd = getcwd(NULL, 0);
5743 if (cwd == NULL) {
5744 error = got_error_from_errno("getcwd");
5745 goto done;
5747 error = got_worktree_open(&worktree, cwd);
5748 if (error)
5749 goto done;
5751 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5752 NULL);
5753 if (error != NULL)
5754 goto done;
5756 error = apply_unveil(got_repo_get_path(repo), 0,
5757 got_worktree_get_root_path(worktree));
5758 if (error)
5759 goto done;
5761 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5762 worktree);
5763 if (error)
5764 goto done;
5765 if (histedit_in_progress) {
5766 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5767 goto done;
5770 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5771 if (error)
5772 goto done;
5774 if (abort_rebase) {
5775 int did_something;
5776 if (!rebase_in_progress) {
5777 error = got_error(GOT_ERR_NOT_REBASING);
5778 goto done;
5780 error = got_worktree_rebase_continue(&resume_commit_id,
5781 &new_base_branch, &tmp_branch, &branch, &fileindex,
5782 worktree, repo);
5783 if (error)
5784 goto done;
5785 printf("Switching work tree to %s\n",
5786 got_ref_get_symref_target(new_base_branch));
5787 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5788 new_base_branch, update_progress, &did_something);
5789 if (error)
5790 goto done;
5791 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5792 goto done; /* nothing else to do */
5795 if (continue_rebase) {
5796 if (!rebase_in_progress) {
5797 error = got_error(GOT_ERR_NOT_REBASING);
5798 goto done;
5800 error = got_worktree_rebase_continue(&resume_commit_id,
5801 &new_base_branch, &tmp_branch, &branch, &fileindex,
5802 worktree, repo);
5803 if (error)
5804 goto done;
5806 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5807 resume_commit_id, repo);
5808 if (error)
5809 goto done;
5811 yca_id = got_object_id_dup(resume_commit_id);
5812 if (yca_id == NULL) {
5813 error = got_error_from_errno("got_object_id_dup");
5814 goto done;
5816 } else {
5817 error = got_ref_open(&branch, repo, argv[0], 0);
5818 if (error != NULL)
5819 goto done;
5822 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5823 if (error)
5824 goto done;
5826 if (!continue_rebase) {
5827 struct got_object_id *base_commit_id;
5829 base_commit_id = got_worktree_get_base_commit_id(worktree);
5830 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5831 base_commit_id, branch_head_commit_id, repo,
5832 check_cancelled, NULL);
5833 if (error)
5834 goto done;
5835 if (yca_id == NULL) {
5836 error = got_error_msg(GOT_ERR_ANCESTRY,
5837 "specified branch shares no common ancestry "
5838 "with work tree's branch");
5839 goto done;
5842 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5843 if (error) {
5844 if (error->code != GOT_ERR_ANCESTRY)
5845 goto done;
5846 error = NULL;
5847 } else {
5848 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5849 "specified branch resolves to a commit which "
5850 "is already contained in work tree's branch");
5851 goto done;
5853 error = got_worktree_rebase_prepare(&new_base_branch,
5854 &tmp_branch, &fileindex, worktree, branch, repo);
5855 if (error)
5856 goto done;
5859 commit_id = branch_head_commit_id;
5860 error = got_object_open_as_commit(&commit, repo, commit_id);
5861 if (error)
5862 goto done;
5864 parent_ids = got_object_commit_get_parent_ids(commit);
5865 pid = SIMPLEQ_FIRST(parent_ids);
5866 if (pid == NULL) {
5867 if (!continue_rebase) {
5868 int did_something;
5869 error = got_worktree_rebase_abort(worktree, fileindex,
5870 repo, new_base_branch, update_progress,
5871 &did_something);
5872 if (error)
5873 goto done;
5874 printf("Rebase of %s aborted\n",
5875 got_ref_get_name(branch));
5877 error = got_error(GOT_ERR_EMPTY_REBASE);
5878 goto done;
5880 error = collect_commits(&commits, commit_id, pid->id,
5881 yca_id, got_worktree_get_path_prefix(worktree),
5882 GOT_ERR_REBASE_PATH, repo);
5883 got_object_commit_close(commit);
5884 commit = NULL;
5885 if (error)
5886 goto done;
5888 if (SIMPLEQ_EMPTY(&commits)) {
5889 if (continue_rebase) {
5890 error = rebase_complete(worktree, fileindex,
5891 branch, new_base_branch, tmp_branch, repo);
5892 goto done;
5893 } else {
5894 /* Fast-forward the reference of the branch. */
5895 struct got_object_id *new_head_commit_id;
5896 char *id_str;
5897 error = got_ref_resolve(&new_head_commit_id, repo,
5898 new_base_branch);
5899 if (error)
5900 goto done;
5901 error = got_object_id_str(&id_str, new_head_commit_id);
5902 printf("Forwarding %s to commit %s\n",
5903 got_ref_get_name(branch), id_str);
5904 free(id_str);
5905 error = got_ref_change_ref(branch,
5906 new_head_commit_id);
5907 if (error)
5908 goto done;
5912 pid = NULL;
5913 SIMPLEQ_FOREACH(qid, &commits, entry) {
5914 commit_id = qid->id;
5915 parent_id = pid ? pid->id : yca_id;
5916 pid = qid;
5918 error = got_worktree_rebase_merge_files(&merged_paths,
5919 worktree, fileindex, parent_id, commit_id, repo,
5920 rebase_progress, &rebase_status, check_cancelled, NULL);
5921 if (error)
5922 goto done;
5924 if (rebase_status == GOT_STATUS_CONFLICT) {
5925 error = show_rebase_merge_conflict(qid->id, repo);
5926 if (error)
5927 goto done;
5928 got_worktree_rebase_pathlist_free(&merged_paths);
5929 break;
5932 error = rebase_commit(&merged_paths, worktree, fileindex,
5933 tmp_branch, commit_id, repo);
5934 got_worktree_rebase_pathlist_free(&merged_paths);
5935 if (error)
5936 goto done;
5939 if (rebase_status == GOT_STATUS_CONFLICT) {
5940 error = got_worktree_rebase_postpone(worktree, fileindex);
5941 if (error)
5942 goto done;
5943 error = got_error_msg(GOT_ERR_CONFLICTS,
5944 "conflicts must be resolved before rebasing can continue");
5945 } else
5946 error = rebase_complete(worktree, fileindex, branch,
5947 new_base_branch, tmp_branch, repo);
5948 done:
5949 got_object_id_queue_free(&commits);
5950 free(branch_head_commit_id);
5951 free(resume_commit_id);
5952 free(yca_id);
5953 if (commit)
5954 got_object_commit_close(commit);
5955 if (branch)
5956 got_ref_close(branch);
5957 if (new_base_branch)
5958 got_ref_close(new_base_branch);
5959 if (tmp_branch)
5960 got_ref_close(tmp_branch);
5961 if (worktree)
5962 got_worktree_close(worktree);
5963 if (repo)
5964 got_repo_close(repo);
5965 return error;
5968 __dead static void
5969 usage_histedit(void)
5971 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5972 getprogname());
5973 exit(1);
5976 #define GOT_HISTEDIT_PICK 'p'
5977 #define GOT_HISTEDIT_EDIT 'e'
5978 #define GOT_HISTEDIT_FOLD 'f'
5979 #define GOT_HISTEDIT_DROP 'd'
5980 #define GOT_HISTEDIT_MESG 'm'
5982 static struct got_histedit_cmd {
5983 unsigned char code;
5984 const char *name;
5985 const char *desc;
5986 } got_histedit_cmds[] = {
5987 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5988 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5989 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5990 "be used" },
5991 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5992 { GOT_HISTEDIT_MESG, "mesg",
5993 "single-line log message for commit above (open editor if empty)" },
5996 struct got_histedit_list_entry {
5997 TAILQ_ENTRY(got_histedit_list_entry) entry;
5998 struct got_object_id *commit_id;
5999 const struct got_histedit_cmd *cmd;
6000 char *logmsg;
6002 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6004 static const struct got_error *
6005 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6006 FILE *f, struct got_repository *repo)
6008 const struct got_error *err = NULL;
6009 char *logmsg = NULL, *id_str = NULL;
6010 struct got_commit_object *commit = NULL;
6011 int n;
6013 err = got_object_open_as_commit(&commit, repo, commit_id);
6014 if (err)
6015 goto done;
6017 err = get_short_logmsg(&logmsg, 34, commit);
6018 if (err)
6019 goto done;
6021 err = got_object_id_str(&id_str, commit_id);
6022 if (err)
6023 goto done;
6025 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6026 if (n < 0)
6027 err = got_ferror(f, GOT_ERR_IO);
6028 done:
6029 if (commit)
6030 got_object_commit_close(commit);
6031 free(id_str);
6032 free(logmsg);
6033 return err;
6036 static const struct got_error *
6037 histedit_write_commit_list(struct got_object_id_queue *commits,
6038 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6040 const struct got_error *err = NULL;
6041 struct got_object_qid *qid;
6043 if (SIMPLEQ_EMPTY(commits))
6044 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6046 SIMPLEQ_FOREACH(qid, commits, entry) {
6047 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6048 f, repo);
6049 if (err)
6050 break;
6051 if (edit_logmsg_only) {
6052 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6053 if (n < 0) {
6054 err = got_ferror(f, GOT_ERR_IO);
6055 break;
6060 return err;
6063 static const struct got_error *
6064 write_cmd_list(FILE *f, const char *branch_name,
6065 struct got_object_id_queue *commits)
6067 const struct got_error *err = NULL;
6068 int n, i;
6069 char *id_str;
6070 struct got_object_qid *qid;
6072 qid = SIMPLEQ_FIRST(commits);
6073 err = got_object_id_str(&id_str, qid->id);
6074 if (err)
6075 return err;
6077 n = fprintf(f,
6078 "# Editing the history of branch '%s' starting at\n"
6079 "# commit %s\n"
6080 "# Commits will be processed in order from top to "
6081 "bottom of this file.\n", branch_name, id_str);
6082 if (n < 0) {
6083 err = got_ferror(f, GOT_ERR_IO);
6084 goto done;
6087 n = fprintf(f, "# Available histedit commands:\n");
6088 if (n < 0) {
6089 err = got_ferror(f, GOT_ERR_IO);
6090 goto done;
6093 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6094 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6095 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6096 cmd->desc);
6097 if (n < 0) {
6098 err = got_ferror(f, GOT_ERR_IO);
6099 break;
6102 done:
6103 free(id_str);
6104 return err;
6107 static const struct got_error *
6108 histedit_syntax_error(int lineno)
6110 static char msg[42];
6111 int ret;
6113 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6114 lineno);
6115 if (ret == -1 || ret >= sizeof(msg))
6116 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6118 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6121 static const struct got_error *
6122 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6123 char *logmsg, struct got_repository *repo)
6125 const struct got_error *err;
6126 struct got_commit_object *folded_commit = NULL;
6127 char *id_str, *folded_logmsg = NULL;
6129 err = got_object_id_str(&id_str, hle->commit_id);
6130 if (err)
6131 return err;
6133 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6134 if (err)
6135 goto done;
6137 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6138 if (err)
6139 goto done;
6140 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6141 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6142 folded_logmsg) == -1) {
6143 err = got_error_from_errno("asprintf");
6145 done:
6146 if (folded_commit)
6147 got_object_commit_close(folded_commit);
6148 free(id_str);
6149 free(folded_logmsg);
6150 return err;
6153 static struct got_histedit_list_entry *
6154 get_folded_commits(struct got_histedit_list_entry *hle)
6156 struct got_histedit_list_entry *prev, *folded = NULL;
6158 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6159 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6160 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6161 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6162 folded = prev;
6163 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6166 return folded;
6169 static const struct got_error *
6170 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6171 struct got_repository *repo)
6173 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6174 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6175 const struct got_error *err = NULL;
6176 struct got_commit_object *commit = NULL;
6177 int fd;
6178 struct got_histedit_list_entry *folded = NULL;
6180 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6181 if (err)
6182 return err;
6184 folded = get_folded_commits(hle);
6185 if (folded) {
6186 while (folded != hle) {
6187 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6188 folded = TAILQ_NEXT(folded, entry);
6189 continue;
6191 err = append_folded_commit_msg(&new_msg, folded,
6192 logmsg, repo);
6193 if (err)
6194 goto done;
6195 free(logmsg);
6196 logmsg = new_msg;
6197 folded = TAILQ_NEXT(folded, entry);
6201 err = got_object_id_str(&id_str, hle->commit_id);
6202 if (err)
6203 goto done;
6204 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6205 if (err)
6206 goto done;
6207 if (asprintf(&new_msg,
6208 "%s\n# original log message of commit %s: %s",
6209 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6210 err = got_error_from_errno("asprintf");
6211 goto done;
6213 free(logmsg);
6214 logmsg = new_msg;
6216 err = got_object_id_str(&id_str, hle->commit_id);
6217 if (err)
6218 goto done;
6220 err = got_opentemp_named_fd(&logmsg_path, &fd,
6221 GOT_TMPDIR_STR "/got-logmsg");
6222 if (err)
6223 goto done;
6225 dprintf(fd, logmsg);
6226 close(fd);
6228 err = get_editor(&editor);
6229 if (err)
6230 goto done;
6232 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6233 if (err) {
6234 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6235 goto done;
6236 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6238 done:
6239 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6240 err = got_error_from_errno2("unlink", logmsg_path);
6241 free(logmsg_path);
6242 free(logmsg);
6243 free(orig_logmsg);
6244 free(editor);
6245 if (commit)
6246 got_object_commit_close(commit);
6247 return err;
6250 static const struct got_error *
6251 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6252 FILE *f, struct got_repository *repo)
6254 const struct got_error *err = NULL;
6255 char *line = NULL, *p, *end;
6256 size_t size;
6257 ssize_t len;
6258 int lineno = 0, i;
6259 const struct got_histedit_cmd *cmd;
6260 struct got_object_id *commit_id = NULL;
6261 struct got_histedit_list_entry *hle = NULL;
6263 for (;;) {
6264 len = getline(&line, &size, f);
6265 if (len == -1) {
6266 const struct got_error *getline_err;
6267 if (feof(f))
6268 break;
6269 getline_err = got_error_from_errno("getline");
6270 err = got_ferror(f, getline_err->code);
6271 break;
6273 lineno++;
6274 p = line;
6275 while (isspace((unsigned char)p[0]))
6276 p++;
6277 if (p[0] == '#' || p[0] == '\0') {
6278 free(line);
6279 line = NULL;
6280 continue;
6282 cmd = NULL;
6283 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6284 cmd = &got_histedit_cmds[i];
6285 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6286 isspace((unsigned char)p[strlen(cmd->name)])) {
6287 p += strlen(cmd->name);
6288 break;
6290 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6291 p++;
6292 break;
6295 if (i == nitems(got_histedit_cmds)) {
6296 err = histedit_syntax_error(lineno);
6297 break;
6299 while (isspace((unsigned char)p[0]))
6300 p++;
6301 if (cmd->code == GOT_HISTEDIT_MESG) {
6302 if (hle == NULL || hle->logmsg != NULL) {
6303 err = got_error(GOT_ERR_HISTEDIT_CMD);
6304 break;
6306 if (p[0] == '\0') {
6307 err = histedit_edit_logmsg(hle, repo);
6308 if (err)
6309 break;
6310 } else {
6311 hle->logmsg = strdup(p);
6312 if (hle->logmsg == NULL) {
6313 err = got_error_from_errno("strdup");
6314 break;
6317 free(line);
6318 line = NULL;
6319 continue;
6320 } else {
6321 end = p;
6322 while (end[0] && !isspace((unsigned char)end[0]))
6323 end++;
6324 *end = '\0';
6326 err = got_object_resolve_id_str(&commit_id, repo, p);
6327 if (err) {
6328 /* override error code */
6329 err = histedit_syntax_error(lineno);
6330 break;
6333 hle = malloc(sizeof(*hle));
6334 if (hle == NULL) {
6335 err = got_error_from_errno("malloc");
6336 break;
6338 hle->cmd = cmd;
6339 hle->commit_id = commit_id;
6340 hle->logmsg = NULL;
6341 commit_id = NULL;
6342 free(line);
6343 line = NULL;
6344 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6347 free(line);
6348 free(commit_id);
6349 return err;
6352 static const struct got_error *
6353 histedit_check_script(struct got_histedit_list *histedit_cmds,
6354 struct got_object_id_queue *commits, struct got_repository *repo)
6356 const struct got_error *err = NULL;
6357 struct got_object_qid *qid;
6358 struct got_histedit_list_entry *hle;
6359 static char msg[92];
6360 char *id_str;
6362 if (TAILQ_EMPTY(histedit_cmds))
6363 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6364 "histedit script contains no commands");
6365 if (SIMPLEQ_EMPTY(commits))
6366 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6368 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6369 struct got_histedit_list_entry *hle2;
6370 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6371 if (hle == hle2)
6372 continue;
6373 if (got_object_id_cmp(hle->commit_id,
6374 hle2->commit_id) != 0)
6375 continue;
6376 err = got_object_id_str(&id_str, hle->commit_id);
6377 if (err)
6378 return err;
6379 snprintf(msg, sizeof(msg), "commit %s is listed "
6380 "more than once in histedit script", id_str);
6381 free(id_str);
6382 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6386 SIMPLEQ_FOREACH(qid, commits, entry) {
6387 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6388 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6389 break;
6391 if (hle == NULL) {
6392 err = got_object_id_str(&id_str, qid->id);
6393 if (err)
6394 return err;
6395 snprintf(msg, sizeof(msg),
6396 "commit %s missing from histedit script", id_str);
6397 free(id_str);
6398 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6402 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6403 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6404 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6405 "last commit in histedit script cannot be folded");
6407 return NULL;
6410 static const struct got_error *
6411 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6412 const char *path, struct got_object_id_queue *commits,
6413 struct got_repository *repo)
6415 const struct got_error *err = NULL;
6416 char *editor;
6417 FILE *f = NULL;
6419 err = get_editor(&editor);
6420 if (err)
6421 return err;
6423 if (spawn_editor(editor, path) == -1) {
6424 err = got_error_from_errno("failed spawning editor");
6425 goto done;
6428 f = fopen(path, "r");
6429 if (f == NULL) {
6430 err = got_error_from_errno("fopen");
6431 goto done;
6433 err = histedit_parse_list(histedit_cmds, f, repo);
6434 if (err)
6435 goto done;
6437 err = histedit_check_script(histedit_cmds, commits, repo);
6438 done:
6439 if (f && fclose(f) != 0 && err == NULL)
6440 err = got_error_from_errno("fclose");
6441 free(editor);
6442 return err;
6445 static const struct got_error *
6446 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6447 struct got_object_id_queue *, const char *, const char *,
6448 struct got_repository *);
6450 static const struct got_error *
6451 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6452 struct got_object_id_queue *commits, const char *branch_name,
6453 int edit_logmsg_only, struct got_repository *repo)
6455 const struct got_error *err;
6456 FILE *f = NULL;
6457 char *path = NULL;
6459 err = got_opentemp_named(&path, &f, "got-histedit");
6460 if (err)
6461 return err;
6463 err = write_cmd_list(f, branch_name, commits);
6464 if (err)
6465 goto done;
6467 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6468 if (err)
6469 goto done;
6471 if (edit_logmsg_only) {
6472 rewind(f);
6473 err = histedit_parse_list(histedit_cmds, f, repo);
6474 } else {
6475 if (fclose(f) != 0) {
6476 err = got_error_from_errno("fclose");
6477 goto done;
6479 f = NULL;
6480 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6481 if (err) {
6482 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6483 err->code != GOT_ERR_HISTEDIT_CMD)
6484 goto done;
6485 err = histedit_edit_list_retry(histedit_cmds, err,
6486 commits, path, branch_name, repo);
6489 done:
6490 if (f && fclose(f) != 0 && err == NULL)
6491 err = got_error_from_errno("fclose");
6492 if (path && unlink(path) != 0 && err == NULL)
6493 err = got_error_from_errno2("unlink", path);
6494 free(path);
6495 return err;
6498 static const struct got_error *
6499 histedit_save_list(struct got_histedit_list *histedit_cmds,
6500 struct got_worktree *worktree, struct got_repository *repo)
6502 const struct got_error *err = NULL;
6503 char *path = NULL;
6504 FILE *f = NULL;
6505 struct got_histedit_list_entry *hle;
6506 struct got_commit_object *commit = NULL;
6508 err = got_worktree_get_histedit_script_path(&path, worktree);
6509 if (err)
6510 return err;
6512 f = fopen(path, "w");
6513 if (f == NULL) {
6514 err = got_error_from_errno2("fopen", path);
6515 goto done;
6517 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6518 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6519 repo);
6520 if (err)
6521 break;
6523 if (hle->logmsg) {
6524 int n = fprintf(f, "%c %s\n",
6525 GOT_HISTEDIT_MESG, hle->logmsg);
6526 if (n < 0) {
6527 err = got_ferror(f, GOT_ERR_IO);
6528 break;
6532 done:
6533 if (f && fclose(f) != 0 && err == NULL)
6534 err = got_error_from_errno("fclose");
6535 free(path);
6536 if (commit)
6537 got_object_commit_close(commit);
6538 return err;
6541 void
6542 histedit_free_list(struct got_histedit_list *histedit_cmds)
6544 struct got_histedit_list_entry *hle;
6546 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6547 TAILQ_REMOVE(histedit_cmds, hle, entry);
6548 free(hle);
6552 static const struct got_error *
6553 histedit_load_list(struct got_histedit_list *histedit_cmds,
6554 const char *path, struct got_repository *repo)
6556 const struct got_error *err = NULL;
6557 FILE *f = NULL;
6559 f = fopen(path, "r");
6560 if (f == NULL) {
6561 err = got_error_from_errno2("fopen", path);
6562 goto done;
6565 err = histedit_parse_list(histedit_cmds, f, repo);
6566 done:
6567 if (f && fclose(f) != 0 && err == NULL)
6568 err = got_error_from_errno("fclose");
6569 return err;
6572 static const struct got_error *
6573 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6574 const struct got_error *edit_err, struct got_object_id_queue *commits,
6575 const char *path, const char *branch_name, struct got_repository *repo)
6577 const struct got_error *err = NULL, *prev_err = edit_err;
6578 int resp = ' ';
6580 while (resp != 'c' && resp != 'r' && resp != 'a') {
6581 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6582 "or (a)bort: ", getprogname(), prev_err->msg);
6583 resp = getchar();
6584 if (resp == '\n')
6585 resp = getchar();
6586 if (resp == 'c') {
6587 histedit_free_list(histedit_cmds);
6588 err = histedit_run_editor(histedit_cmds, path, commits,
6589 repo);
6590 if (err) {
6591 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6592 err->code != GOT_ERR_HISTEDIT_CMD)
6593 break;
6594 prev_err = err;
6595 resp = ' ';
6596 continue;
6598 break;
6599 } else if (resp == 'r') {
6600 histedit_free_list(histedit_cmds);
6601 err = histedit_edit_script(histedit_cmds,
6602 commits, branch_name, 0, repo);
6603 if (err) {
6604 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6605 err->code != GOT_ERR_HISTEDIT_CMD)
6606 break;
6607 prev_err = err;
6608 resp = ' ';
6609 continue;
6611 break;
6612 } else if (resp == 'a') {
6613 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6614 break;
6615 } else
6616 printf("invalid response '%c'\n", resp);
6619 return err;
6622 static const struct got_error *
6623 histedit_complete(struct got_worktree *worktree,
6624 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6625 struct got_reference *branch, struct got_repository *repo)
6627 printf("Switching work tree to %s\n",
6628 got_ref_get_symref_target(branch));
6629 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6630 branch, repo);
6633 static const struct got_error *
6634 show_histedit_progress(struct got_commit_object *commit,
6635 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6637 const struct got_error *err;
6638 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6640 err = got_object_id_str(&old_id_str, hle->commit_id);
6641 if (err)
6642 goto done;
6644 if (new_id) {
6645 err = got_object_id_str(&new_id_str, new_id);
6646 if (err)
6647 goto done;
6650 old_id_str[12] = '\0';
6651 if (new_id_str)
6652 new_id_str[12] = '\0';
6654 if (hle->logmsg) {
6655 logmsg = strdup(hle->logmsg);
6656 if (logmsg == NULL) {
6657 err = got_error_from_errno("strdup");
6658 goto done;
6660 trim_logmsg(logmsg, 42);
6661 } else {
6662 err = get_short_logmsg(&logmsg, 42, commit);
6663 if (err)
6664 goto done;
6667 switch (hle->cmd->code) {
6668 case GOT_HISTEDIT_PICK:
6669 case GOT_HISTEDIT_EDIT:
6670 printf("%s -> %s: %s\n", old_id_str,
6671 new_id_str ? new_id_str : "no-op change", logmsg);
6672 break;
6673 case GOT_HISTEDIT_DROP:
6674 case GOT_HISTEDIT_FOLD:
6675 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6676 logmsg);
6677 break;
6678 default:
6679 break;
6681 done:
6682 free(old_id_str);
6683 free(new_id_str);
6684 return err;
6687 static const struct got_error *
6688 histedit_commit(struct got_pathlist_head *merged_paths,
6689 struct got_worktree *worktree, struct got_fileindex *fileindex,
6690 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6691 struct got_repository *repo)
6693 const struct got_error *err;
6694 struct got_commit_object *commit;
6695 struct got_object_id *new_commit_id;
6697 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6698 && hle->logmsg == NULL) {
6699 err = histedit_edit_logmsg(hle, repo);
6700 if (err)
6701 return err;
6704 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6705 if (err)
6706 return err;
6708 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6709 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6710 hle->logmsg, repo);
6711 if (err) {
6712 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6713 goto done;
6714 err = show_histedit_progress(commit, hle, NULL);
6715 } else {
6716 err = show_histedit_progress(commit, hle, new_commit_id);
6717 free(new_commit_id);
6719 done:
6720 got_object_commit_close(commit);
6721 return err;
6724 static const struct got_error *
6725 histedit_skip_commit(struct got_histedit_list_entry *hle,
6726 struct got_worktree *worktree, struct got_repository *repo)
6728 const struct got_error *error;
6729 struct got_commit_object *commit;
6731 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6732 repo);
6733 if (error)
6734 return error;
6736 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6737 if (error)
6738 return error;
6740 error = show_histedit_progress(commit, hle, NULL);
6741 got_object_commit_close(commit);
6742 return error;
6745 static const struct got_error *
6746 check_local_changes(void *arg, unsigned char status,
6747 unsigned char staged_status, const char *path,
6748 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6749 struct got_object_id *commit_id, int dirfd, const char *de_name)
6751 int *have_local_changes = arg;
6753 switch (status) {
6754 case GOT_STATUS_ADD:
6755 case GOT_STATUS_DELETE:
6756 case GOT_STATUS_MODIFY:
6757 case GOT_STATUS_CONFLICT:
6758 *have_local_changes = 1;
6759 return got_error(GOT_ERR_CANCELLED);
6760 default:
6761 break;
6764 switch (staged_status) {
6765 case GOT_STATUS_ADD:
6766 case GOT_STATUS_DELETE:
6767 case GOT_STATUS_MODIFY:
6768 *have_local_changes = 1;
6769 return got_error(GOT_ERR_CANCELLED);
6770 default:
6771 break;
6774 return NULL;
6777 static const struct got_error *
6778 cmd_histedit(int argc, char *argv[])
6780 const struct got_error *error = NULL;
6781 struct got_worktree *worktree = NULL;
6782 struct got_fileindex *fileindex = NULL;
6783 struct got_repository *repo = NULL;
6784 char *cwd = NULL;
6785 struct got_reference *branch = NULL;
6786 struct got_reference *tmp_branch = NULL;
6787 struct got_object_id *resume_commit_id = NULL;
6788 struct got_object_id *base_commit_id = NULL;
6789 struct got_object_id *head_commit_id = NULL;
6790 struct got_commit_object *commit = NULL;
6791 int ch, rebase_in_progress = 0, did_something;
6792 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6793 int edit_logmsg_only = 0;
6794 const char *edit_script_path = NULL;
6795 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6796 struct got_object_id_queue commits;
6797 struct got_pathlist_head merged_paths;
6798 const struct got_object_id_queue *parent_ids;
6799 struct got_object_qid *pid;
6800 struct got_histedit_list histedit_cmds;
6801 struct got_histedit_list_entry *hle;
6803 SIMPLEQ_INIT(&commits);
6804 TAILQ_INIT(&histedit_cmds);
6805 TAILQ_INIT(&merged_paths);
6807 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6808 switch (ch) {
6809 case 'a':
6810 abort_edit = 1;
6811 break;
6812 case 'c':
6813 continue_edit = 1;
6814 break;
6815 case 'F':
6816 edit_script_path = optarg;
6817 break;
6818 case 'm':
6819 edit_logmsg_only = 1;
6820 break;
6821 default:
6822 usage_histedit();
6823 /* NOTREACHED */
6827 argc -= optind;
6828 argv += optind;
6830 #ifndef PROFILE
6831 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6832 "unveil", NULL) == -1)
6833 err(1, "pledge");
6834 #endif
6835 if (abort_edit && continue_edit)
6836 errx(1, "histedit's -a and -c options are mutually exclusive");
6837 if (edit_script_path && edit_logmsg_only)
6838 errx(1, "histedit's -F and -m options are mutually exclusive");
6839 if (abort_edit && edit_logmsg_only)
6840 errx(1, "histedit's -a and -m options are mutually exclusive");
6841 if (continue_edit && edit_logmsg_only)
6842 errx(1, "histedit's -c and -m options are mutually exclusive");
6843 if (argc != 0)
6844 usage_histedit();
6847 * This command cannot apply unveil(2) in all cases because the
6848 * user may choose to run an editor to edit the histedit script
6849 * and to edit individual commit log messages.
6850 * unveil(2) traverses exec(2); if an editor is used we have to
6851 * apply unveil after edit script and log messages have been written.
6852 * XXX TODO: Make use of unveil(2) where possible.
6855 cwd = getcwd(NULL, 0);
6856 if (cwd == NULL) {
6857 error = got_error_from_errno("getcwd");
6858 goto done;
6860 error = got_worktree_open(&worktree, cwd);
6861 if (error)
6862 goto done;
6864 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6865 NULL);
6866 if (error != NULL)
6867 goto done;
6869 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6870 if (error)
6871 goto done;
6872 if (rebase_in_progress) {
6873 error = got_error(GOT_ERR_REBASING);
6874 goto done;
6877 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6878 if (error)
6879 goto done;
6881 if (edit_in_progress && edit_logmsg_only) {
6882 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6883 "histedit operation is in progress in this "
6884 "work tree and must be continued or aborted "
6885 "before the -m option can be used");
6886 goto done;
6889 if (edit_in_progress && abort_edit) {
6890 error = got_worktree_histedit_continue(&resume_commit_id,
6891 &tmp_branch, &branch, &base_commit_id, &fileindex,
6892 worktree, repo);
6893 if (error)
6894 goto done;
6895 printf("Switching work tree to %s\n",
6896 got_ref_get_symref_target(branch));
6897 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6898 branch, base_commit_id, update_progress, &did_something);
6899 if (error)
6900 goto done;
6901 printf("Histedit of %s aborted\n",
6902 got_ref_get_symref_target(branch));
6903 goto done; /* nothing else to do */
6904 } else if (abort_edit) {
6905 error = got_error(GOT_ERR_NOT_HISTEDIT);
6906 goto done;
6909 if (continue_edit) {
6910 char *path;
6912 if (!edit_in_progress) {
6913 error = got_error(GOT_ERR_NOT_HISTEDIT);
6914 goto done;
6917 error = got_worktree_get_histedit_script_path(&path, worktree);
6918 if (error)
6919 goto done;
6921 error = histedit_load_list(&histedit_cmds, path, repo);
6922 free(path);
6923 if (error)
6924 goto done;
6926 error = got_worktree_histedit_continue(&resume_commit_id,
6927 &tmp_branch, &branch, &base_commit_id, &fileindex,
6928 worktree, repo);
6929 if (error)
6930 goto done;
6932 error = got_ref_resolve(&head_commit_id, repo, branch);
6933 if (error)
6934 goto done;
6936 error = got_object_open_as_commit(&commit, repo,
6937 head_commit_id);
6938 if (error)
6939 goto done;
6940 parent_ids = got_object_commit_get_parent_ids(commit);
6941 pid = SIMPLEQ_FIRST(parent_ids);
6942 if (pid == NULL) {
6943 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6944 goto done;
6946 error = collect_commits(&commits, head_commit_id, pid->id,
6947 base_commit_id, got_worktree_get_path_prefix(worktree),
6948 GOT_ERR_HISTEDIT_PATH, repo);
6949 got_object_commit_close(commit);
6950 commit = NULL;
6951 if (error)
6952 goto done;
6953 } else {
6954 if (edit_in_progress) {
6955 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6956 goto done;
6959 error = got_ref_open(&branch, repo,
6960 got_worktree_get_head_ref_name(worktree), 0);
6961 if (error != NULL)
6962 goto done;
6964 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6965 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6966 "will not edit commit history of a branch outside "
6967 "the \"refs/heads/\" reference namespace");
6968 goto done;
6971 error = got_ref_resolve(&head_commit_id, repo, branch);
6972 got_ref_close(branch);
6973 branch = NULL;
6974 if (error)
6975 goto done;
6977 error = got_object_open_as_commit(&commit, repo,
6978 head_commit_id);
6979 if (error)
6980 goto done;
6981 parent_ids = got_object_commit_get_parent_ids(commit);
6982 pid = SIMPLEQ_FIRST(parent_ids);
6983 if (pid == NULL) {
6984 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6985 goto done;
6987 error = collect_commits(&commits, head_commit_id, pid->id,
6988 got_worktree_get_base_commit_id(worktree),
6989 got_worktree_get_path_prefix(worktree),
6990 GOT_ERR_HISTEDIT_PATH, repo);
6991 got_object_commit_close(commit);
6992 commit = NULL;
6993 if (error)
6994 goto done;
6996 if (SIMPLEQ_EMPTY(&commits)) {
6997 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6998 goto done;
7001 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7002 &base_commit_id, &fileindex, worktree, repo);
7003 if (error)
7004 goto done;
7006 if (edit_script_path) {
7007 error = histedit_load_list(&histedit_cmds,
7008 edit_script_path, repo);
7009 if (error) {
7010 got_worktree_histedit_abort(worktree, fileindex,
7011 repo, branch, base_commit_id,
7012 update_progress, &did_something);
7013 goto done;
7015 } else {
7016 const char *branch_name;
7017 branch_name = got_ref_get_symref_target(branch);
7018 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7019 branch_name += 11;
7020 error = histedit_edit_script(&histedit_cmds, &commits,
7021 branch_name, edit_logmsg_only, repo);
7022 if (error) {
7023 got_worktree_histedit_abort(worktree, fileindex,
7024 repo, branch, base_commit_id,
7025 update_progress, &did_something);
7026 goto done;
7031 error = histedit_save_list(&histedit_cmds, worktree,
7032 repo);
7033 if (error) {
7034 got_worktree_histedit_abort(worktree, fileindex,
7035 repo, branch, base_commit_id,
7036 update_progress, &did_something);
7037 goto done;
7042 error = histedit_check_script(&histedit_cmds, &commits, repo);
7043 if (error)
7044 goto done;
7046 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7047 if (resume_commit_id) {
7048 if (got_object_id_cmp(hle->commit_id,
7049 resume_commit_id) != 0)
7050 continue;
7052 resume_commit_id = NULL;
7053 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7054 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7055 error = histedit_skip_commit(hle, worktree,
7056 repo);
7057 if (error)
7058 goto done;
7059 } else {
7060 struct got_pathlist_head paths;
7061 int have_changes = 0;
7063 TAILQ_INIT(&paths);
7064 error = got_pathlist_append(&paths, "", NULL);
7065 if (error)
7066 goto done;
7067 error = got_worktree_status(worktree, &paths,
7068 repo, check_local_changes, &have_changes,
7069 check_cancelled, NULL);
7070 got_pathlist_free(&paths);
7071 if (error) {
7072 if (error->code != GOT_ERR_CANCELLED)
7073 goto done;
7074 if (sigint_received || sigpipe_received)
7075 goto done;
7077 if (have_changes) {
7078 error = histedit_commit(NULL, worktree,
7079 fileindex, tmp_branch, hle, repo);
7080 if (error)
7081 goto done;
7082 } else {
7083 error = got_object_open_as_commit(
7084 &commit, repo, hle->commit_id);
7085 if (error)
7086 goto done;
7087 error = show_histedit_progress(commit,
7088 hle, NULL);
7089 got_object_commit_close(commit);
7090 commit = NULL;
7091 if (error)
7092 goto done;
7095 continue;
7098 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7099 error = histedit_skip_commit(hle, worktree, repo);
7100 if (error)
7101 goto done;
7102 continue;
7105 error = got_object_open_as_commit(&commit, repo,
7106 hle->commit_id);
7107 if (error)
7108 goto done;
7109 parent_ids = got_object_commit_get_parent_ids(commit);
7110 pid = SIMPLEQ_FIRST(parent_ids);
7112 error = got_worktree_histedit_merge_files(&merged_paths,
7113 worktree, fileindex, pid->id, hle->commit_id, repo,
7114 rebase_progress, &rebase_status, check_cancelled, NULL);
7115 if (error)
7116 goto done;
7117 got_object_commit_close(commit);
7118 commit = NULL;
7120 if (rebase_status == GOT_STATUS_CONFLICT) {
7121 error = show_rebase_merge_conflict(hle->commit_id,
7122 repo);
7123 if (error)
7124 goto done;
7125 got_worktree_rebase_pathlist_free(&merged_paths);
7126 break;
7129 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7130 char *id_str;
7131 error = got_object_id_str(&id_str, hle->commit_id);
7132 if (error)
7133 goto done;
7134 printf("Stopping histedit for amending commit %s\n",
7135 id_str);
7136 free(id_str);
7137 got_worktree_rebase_pathlist_free(&merged_paths);
7138 error = got_worktree_histedit_postpone(worktree,
7139 fileindex);
7140 goto done;
7143 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7144 error = histedit_skip_commit(hle, worktree, repo);
7145 if (error)
7146 goto done;
7147 continue;
7150 error = histedit_commit(&merged_paths, worktree, fileindex,
7151 tmp_branch, hle, repo);
7152 got_worktree_rebase_pathlist_free(&merged_paths);
7153 if (error)
7154 goto done;
7157 if (rebase_status == GOT_STATUS_CONFLICT) {
7158 error = got_worktree_histedit_postpone(worktree, fileindex);
7159 if (error)
7160 goto done;
7161 error = got_error_msg(GOT_ERR_CONFLICTS,
7162 "conflicts must be resolved before histedit can continue");
7163 } else
7164 error = histedit_complete(worktree, fileindex, tmp_branch,
7165 branch, repo);
7166 done:
7167 got_object_id_queue_free(&commits);
7168 histedit_free_list(&histedit_cmds);
7169 free(head_commit_id);
7170 free(base_commit_id);
7171 free(resume_commit_id);
7172 if (commit)
7173 got_object_commit_close(commit);
7174 if (branch)
7175 got_ref_close(branch);
7176 if (tmp_branch)
7177 got_ref_close(tmp_branch);
7178 if (worktree)
7179 got_worktree_close(worktree);
7180 if (repo)
7181 got_repo_close(repo);
7182 return error;
7185 __dead static void
7186 usage_integrate(void)
7188 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7189 exit(1);
7192 static const struct got_error *
7193 cmd_integrate(int argc, char *argv[])
7195 const struct got_error *error = NULL;
7196 struct got_repository *repo = NULL;
7197 struct got_worktree *worktree = NULL;
7198 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7199 const char *branch_arg = NULL;
7200 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7201 struct got_fileindex *fileindex = NULL;
7202 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7203 int ch, did_something = 0;
7205 while ((ch = getopt(argc, argv, "")) != -1) {
7206 switch (ch) {
7207 default:
7208 usage_integrate();
7209 /* NOTREACHED */
7213 argc -= optind;
7214 argv += optind;
7216 if (argc != 1)
7217 usage_integrate();
7218 branch_arg = argv[0];
7220 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7221 "unveil", NULL) == -1)
7222 err(1, "pledge");
7224 cwd = getcwd(NULL, 0);
7225 if (cwd == NULL) {
7226 error = got_error_from_errno("getcwd");
7227 goto done;
7230 error = got_worktree_open(&worktree, cwd);
7231 if (error)
7232 goto done;
7234 error = check_rebase_or_histedit_in_progress(worktree);
7235 if (error)
7236 goto done;
7238 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7239 NULL);
7240 if (error != NULL)
7241 goto done;
7243 error = apply_unveil(got_repo_get_path(repo), 0,
7244 got_worktree_get_root_path(worktree));
7245 if (error)
7246 goto done;
7248 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7249 error = got_error_from_errno("asprintf");
7250 goto done;
7253 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7254 &base_branch_ref, worktree, refname, repo);
7255 if (error)
7256 goto done;
7258 refname = strdup(got_ref_get_name(branch_ref));
7259 if (refname == NULL) {
7260 error = got_error_from_errno("strdup");
7261 got_worktree_integrate_abort(worktree, fileindex, repo,
7262 branch_ref, base_branch_ref);
7263 goto done;
7265 base_refname = strdup(got_ref_get_name(base_branch_ref));
7266 if (base_refname == NULL) {
7267 error = got_error_from_errno("strdup");
7268 got_worktree_integrate_abort(worktree, fileindex, repo,
7269 branch_ref, base_branch_ref);
7270 goto done;
7273 error = got_ref_resolve(&commit_id, repo, branch_ref);
7274 if (error)
7275 goto done;
7277 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7278 if (error)
7279 goto done;
7281 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7282 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7283 "specified branch has already been integrated");
7284 got_worktree_integrate_abort(worktree, fileindex, repo,
7285 branch_ref, base_branch_ref);
7286 goto done;
7289 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7290 if (error) {
7291 if (error->code == GOT_ERR_ANCESTRY)
7292 error = got_error(GOT_ERR_REBASE_REQUIRED);
7293 got_worktree_integrate_abort(worktree, fileindex, repo,
7294 branch_ref, base_branch_ref);
7295 goto done;
7298 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7299 branch_ref, base_branch_ref, update_progress, &did_something,
7300 check_cancelled, NULL);
7301 if (error)
7302 goto done;
7304 printf("Integrated %s into %s\n", refname, base_refname);
7305 done:
7306 if (repo)
7307 got_repo_close(repo);
7308 if (worktree)
7309 got_worktree_close(worktree);
7310 free(cwd);
7311 free(base_commit_id);
7312 free(commit_id);
7313 free(refname);
7314 free(base_refname);
7315 return error;
7318 __dead static void
7319 usage_stage(void)
7321 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7322 "[file-path ...]\n",
7323 getprogname());
7324 exit(1);
7327 static const struct got_error *
7328 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7329 const char *path, struct got_object_id *blob_id,
7330 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7331 int dirfd, const char *de_name)
7333 const struct got_error *err = NULL;
7334 char *id_str = NULL;
7336 if (staged_status != GOT_STATUS_ADD &&
7337 staged_status != GOT_STATUS_MODIFY &&
7338 staged_status != GOT_STATUS_DELETE)
7339 return NULL;
7341 if (staged_status == GOT_STATUS_ADD ||
7342 staged_status == GOT_STATUS_MODIFY)
7343 err = got_object_id_str(&id_str, staged_blob_id);
7344 else
7345 err = got_object_id_str(&id_str, blob_id);
7346 if (err)
7347 return err;
7349 printf("%s %c %s\n", id_str, staged_status, path);
7350 free(id_str);
7351 return NULL;
7354 static const struct got_error *
7355 cmd_stage(int argc, char *argv[])
7357 const struct got_error *error = NULL;
7358 struct got_repository *repo = NULL;
7359 struct got_worktree *worktree = NULL;
7360 char *cwd = NULL;
7361 struct got_pathlist_head paths;
7362 struct got_pathlist_entry *pe;
7363 int ch, list_stage = 0, pflag = 0;
7364 FILE *patch_script_file = NULL;
7365 const char *patch_script_path = NULL;
7366 struct choose_patch_arg cpa;
7368 TAILQ_INIT(&paths);
7370 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7371 switch (ch) {
7372 case 'l':
7373 list_stage = 1;
7374 break;
7375 case 'p':
7376 pflag = 1;
7377 break;
7378 case 'F':
7379 patch_script_path = optarg;
7380 break;
7381 default:
7382 usage_stage();
7383 /* NOTREACHED */
7387 argc -= optind;
7388 argv += optind;
7390 #ifndef PROFILE
7391 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7392 "unveil", NULL) == -1)
7393 err(1, "pledge");
7394 #endif
7395 if (list_stage && (pflag || patch_script_path))
7396 errx(1, "-l option cannot be used with other options");
7397 if (patch_script_path && !pflag)
7398 errx(1, "-F option can only be used together with -p option");
7400 cwd = getcwd(NULL, 0);
7401 if (cwd == NULL) {
7402 error = got_error_from_errno("getcwd");
7403 goto done;
7406 error = got_worktree_open(&worktree, cwd);
7407 if (error)
7408 goto done;
7410 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7411 NULL);
7412 if (error != NULL)
7413 goto done;
7415 if (patch_script_path) {
7416 patch_script_file = fopen(patch_script_path, "r");
7417 if (patch_script_file == NULL) {
7418 error = got_error_from_errno2("fopen",
7419 patch_script_path);
7420 goto done;
7423 error = apply_unveil(got_repo_get_path(repo), 0,
7424 got_worktree_get_root_path(worktree));
7425 if (error)
7426 goto done;
7428 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7429 if (error)
7430 goto done;
7432 if (list_stage)
7433 error = got_worktree_status(worktree, &paths, repo,
7434 print_stage, NULL, check_cancelled, NULL);
7435 else {
7436 cpa.patch_script_file = patch_script_file;
7437 cpa.action = "stage";
7438 error = got_worktree_stage(worktree, &paths,
7439 pflag ? NULL : print_status, NULL,
7440 pflag ? choose_patch : NULL, &cpa, repo);
7442 done:
7443 if (patch_script_file && fclose(patch_script_file) == EOF &&
7444 error == NULL)
7445 error = got_error_from_errno2("fclose", patch_script_path);
7446 if (repo)
7447 got_repo_close(repo);
7448 if (worktree)
7449 got_worktree_close(worktree);
7450 TAILQ_FOREACH(pe, &paths, entry)
7451 free((char *)pe->path);
7452 got_pathlist_free(&paths);
7453 free(cwd);
7454 return error;
7457 __dead static void
7458 usage_unstage(void)
7460 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7461 "[file-path ...]\n",
7462 getprogname());
7463 exit(1);
7467 static const struct got_error *
7468 cmd_unstage(int argc, char *argv[])
7470 const struct got_error *error = NULL;
7471 struct got_repository *repo = NULL;
7472 struct got_worktree *worktree = NULL;
7473 char *cwd = NULL;
7474 struct got_pathlist_head paths;
7475 struct got_pathlist_entry *pe;
7476 int ch, did_something = 0, pflag = 0;
7477 FILE *patch_script_file = NULL;
7478 const char *patch_script_path = NULL;
7479 struct choose_patch_arg cpa;
7481 TAILQ_INIT(&paths);
7483 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7484 switch (ch) {
7485 case 'p':
7486 pflag = 1;
7487 break;
7488 case 'F':
7489 patch_script_path = optarg;
7490 break;
7491 default:
7492 usage_unstage();
7493 /* NOTREACHED */
7497 argc -= optind;
7498 argv += optind;
7500 #ifndef PROFILE
7501 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7502 "unveil", NULL) == -1)
7503 err(1, "pledge");
7504 #endif
7505 if (patch_script_path && !pflag)
7506 errx(1, "-F option can only be used together with -p option");
7508 cwd = getcwd(NULL, 0);
7509 if (cwd == NULL) {
7510 error = got_error_from_errno("getcwd");
7511 goto done;
7514 error = got_worktree_open(&worktree, cwd);
7515 if (error)
7516 goto done;
7518 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7519 NULL);
7520 if (error != NULL)
7521 goto done;
7523 if (patch_script_path) {
7524 patch_script_file = fopen(patch_script_path, "r");
7525 if (patch_script_file == NULL) {
7526 error = got_error_from_errno2("fopen",
7527 patch_script_path);
7528 goto done;
7532 error = apply_unveil(got_repo_get_path(repo), 0,
7533 got_worktree_get_root_path(worktree));
7534 if (error)
7535 goto done;
7537 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7538 if (error)
7539 goto done;
7541 cpa.patch_script_file = patch_script_file;
7542 cpa.action = "unstage";
7543 error = got_worktree_unstage(worktree, &paths, update_progress,
7544 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7545 done:
7546 if (patch_script_file && fclose(patch_script_file) == EOF &&
7547 error == NULL)
7548 error = got_error_from_errno2("fclose", patch_script_path);
7549 if (repo)
7550 got_repo_close(repo);
7551 if (worktree)
7552 got_worktree_close(worktree);
7553 TAILQ_FOREACH(pe, &paths, entry)
7554 free((char *)pe->path);
7555 got_pathlist_free(&paths);
7556 free(cwd);
7557 return error;
7560 __dead static void
7561 usage_cat(void)
7563 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7564 "arg1 [arg2 ...]\n", getprogname());
7565 exit(1);
7568 static const struct got_error *
7569 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7571 const struct got_error *err;
7572 struct got_blob_object *blob;
7574 err = got_object_open_as_blob(&blob, repo, id, 8192);
7575 if (err)
7576 return err;
7578 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7579 got_object_blob_close(blob);
7580 return err;
7583 static const struct got_error *
7584 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7586 const struct got_error *err;
7587 struct got_tree_object *tree;
7588 int nentries, i;
7590 err = got_object_open_as_tree(&tree, repo, id);
7591 if (err)
7592 return err;
7594 nentries = got_object_tree_get_nentries(tree);
7595 for (i = 0; i < nentries; i++) {
7596 struct got_tree_entry *te;
7597 char *id_str;
7598 if (sigint_received || sigpipe_received)
7599 break;
7600 te = got_object_tree_get_entry(tree, i);
7601 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7602 if (err)
7603 break;
7604 fprintf(outfile, "%s %.7o %s\n", id_str,
7605 got_tree_entry_get_mode(te),
7606 got_tree_entry_get_name(te));
7607 free(id_str);
7610 got_object_tree_close(tree);
7611 return err;
7614 static const struct got_error *
7615 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7617 const struct got_error *err;
7618 struct got_commit_object *commit;
7619 const struct got_object_id_queue *parent_ids;
7620 struct got_object_qid *pid;
7621 char *id_str = NULL;
7622 const char *logmsg = NULL;
7624 err = got_object_open_as_commit(&commit, repo, id);
7625 if (err)
7626 return err;
7628 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7629 if (err)
7630 goto done;
7632 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7633 parent_ids = got_object_commit_get_parent_ids(commit);
7634 fprintf(outfile, "numparents %d\n",
7635 got_object_commit_get_nparents(commit));
7636 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7637 char *pid_str;
7638 err = got_object_id_str(&pid_str, pid->id);
7639 if (err)
7640 goto done;
7641 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7642 free(pid_str);
7644 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7645 got_object_commit_get_author(commit),
7646 got_object_commit_get_author_time(commit));
7648 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7649 got_object_commit_get_author(commit),
7650 got_object_commit_get_committer_time(commit));
7652 logmsg = got_object_commit_get_logmsg_raw(commit);
7653 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7654 fprintf(outfile, "%s", logmsg);
7655 done:
7656 free(id_str);
7657 got_object_commit_close(commit);
7658 return err;
7661 static const struct got_error *
7662 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7664 const struct got_error *err;
7665 struct got_tag_object *tag;
7666 char *id_str = NULL;
7667 const char *tagmsg = NULL;
7669 err = got_object_open_as_tag(&tag, repo, id);
7670 if (err)
7671 return err;
7673 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7674 if (err)
7675 goto done;
7677 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7679 switch (got_object_tag_get_object_type(tag)) {
7680 case GOT_OBJ_TYPE_BLOB:
7681 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7682 GOT_OBJ_LABEL_BLOB);
7683 break;
7684 case GOT_OBJ_TYPE_TREE:
7685 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7686 GOT_OBJ_LABEL_TREE);
7687 break;
7688 case GOT_OBJ_TYPE_COMMIT:
7689 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7690 GOT_OBJ_LABEL_COMMIT);
7691 break;
7692 case GOT_OBJ_TYPE_TAG:
7693 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7694 GOT_OBJ_LABEL_TAG);
7695 break;
7696 default:
7697 break;
7700 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7701 got_object_tag_get_name(tag));
7703 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7704 got_object_tag_get_tagger(tag),
7705 got_object_tag_get_tagger_time(tag));
7707 tagmsg = got_object_tag_get_message(tag);
7708 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7709 fprintf(outfile, "%s", tagmsg);
7710 done:
7711 free(id_str);
7712 got_object_tag_close(tag);
7713 return err;
7716 static const struct got_error *
7717 cmd_cat(int argc, char *argv[])
7719 const struct got_error *error;
7720 struct got_repository *repo = NULL;
7721 struct got_worktree *worktree = NULL;
7722 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7723 const char *commit_id_str = NULL;
7724 struct got_object_id *id = NULL, *commit_id = NULL;
7725 int ch, obj_type, i, force_path = 0;
7727 #ifndef PROFILE
7728 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7729 NULL) == -1)
7730 err(1, "pledge");
7731 #endif
7733 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7734 switch (ch) {
7735 case 'c':
7736 commit_id_str = optarg;
7737 break;
7738 case 'r':
7739 repo_path = realpath(optarg, NULL);
7740 if (repo_path == NULL)
7741 return got_error_from_errno2("realpath",
7742 optarg);
7743 got_path_strip_trailing_slashes(repo_path);
7744 break;
7745 case 'P':
7746 force_path = 1;
7747 break;
7748 default:
7749 usage_cat();
7750 /* NOTREACHED */
7754 argc -= optind;
7755 argv += optind;
7757 cwd = getcwd(NULL, 0);
7758 if (cwd == NULL) {
7759 error = got_error_from_errno("getcwd");
7760 goto done;
7762 error = got_worktree_open(&worktree, cwd);
7763 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7764 goto done;
7765 if (worktree) {
7766 if (repo_path == NULL) {
7767 repo_path = strdup(
7768 got_worktree_get_repo_path(worktree));
7769 if (repo_path == NULL) {
7770 error = got_error_from_errno("strdup");
7771 goto done;
7776 if (repo_path == NULL) {
7777 repo_path = getcwd(NULL, 0);
7778 if (repo_path == NULL)
7779 return got_error_from_errno("getcwd");
7782 error = got_repo_open(&repo, repo_path, NULL);
7783 free(repo_path);
7784 if (error != NULL)
7785 goto done;
7787 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7788 if (error)
7789 goto done;
7791 if (commit_id_str == NULL)
7792 commit_id_str = GOT_REF_HEAD;
7793 error = got_repo_match_object_id(&commit_id, NULL,
7794 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7795 if (error)
7796 goto done;
7798 for (i = 0; i < argc; i++) {
7799 if (force_path) {
7800 error = got_object_id_by_path(&id, repo, commit_id,
7801 argv[i]);
7802 if (error)
7803 break;
7804 } else {
7805 error = got_repo_match_object_id(&id, &label, argv[i],
7806 GOT_OBJ_TYPE_ANY, 0, repo);
7807 if (error) {
7808 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7809 error->code != GOT_ERR_NOT_REF)
7810 break;
7811 error = got_object_id_by_path(&id, repo,
7812 commit_id, argv[i]);
7813 if (error)
7814 break;
7818 error = got_object_get_type(&obj_type, repo, id);
7819 if (error)
7820 break;
7822 switch (obj_type) {
7823 case GOT_OBJ_TYPE_BLOB:
7824 error = cat_blob(id, repo, stdout);
7825 break;
7826 case GOT_OBJ_TYPE_TREE:
7827 error = cat_tree(id, repo, stdout);
7828 break;
7829 case GOT_OBJ_TYPE_COMMIT:
7830 error = cat_commit(id, repo, stdout);
7831 break;
7832 case GOT_OBJ_TYPE_TAG:
7833 error = cat_tag(id, repo, stdout);
7834 break;
7835 default:
7836 error = got_error(GOT_ERR_OBJ_TYPE);
7837 break;
7839 if (error)
7840 break;
7841 free(label);
7842 label = NULL;
7843 free(id);
7844 id = NULL;
7846 done:
7847 free(label);
7848 free(id);
7849 free(commit_id);
7850 if (worktree)
7851 got_worktree_close(worktree);
7852 if (repo) {
7853 const struct got_error *repo_error;
7854 repo_error = got_repo_close(repo);
7855 if (error == NULL)
7856 error = repo_error;
7858 return error;