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 error = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1125 if (error)
1126 goto done;
1128 printf("Connected to %s:%s\n", host, port);
1130 fpa.last_scaled_size[0] = '\0';
1131 fpa.last_p_indexed = -1;
1132 fpa.last_p_resolved = -1;
1133 error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1134 repo, fetch_progress, &fpa);
1135 if (error)
1136 goto done;
1138 error = got_object_id_str(&id_str, pack_hash);
1139 if (error)
1140 goto done;
1141 printf("Fetched %s.pack\n", id_str);
1142 free(id_str);
1144 /* Set up references provided with the pack file. */
1145 TAILQ_FOREACH(pe, &refs, entry) {
1146 const char *refname = pe->path;
1147 struct got_object_id *id = pe->data;
1148 struct got_reference *ref;
1151 error = got_ref_alloc(&ref, refname, id);
1152 if (error)
1153 goto done;
1155 #if 0
1156 error = got_object_id_str(&id_str, id);
1157 if (error)
1158 goto done;
1159 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1160 free(id_str);
1161 #endif
1162 error = got_ref_write(ref, repo);
1163 got_ref_close(ref);
1164 if (error)
1165 goto done;
1168 /* Set the HEAD reference if the server provided one. */
1169 TAILQ_FOREACH(pe, &symrefs, entry) {
1170 struct got_reference *symref, *target_ref;
1171 const char *refname = pe->path;
1172 const char *target = pe->data;
1174 if (strcmp(refname, GOT_REF_HEAD) != 0)
1175 continue;
1177 error = got_ref_open(&target_ref, repo, target, 0);
1178 if (error) {
1179 if (error->code == GOT_ERR_NOT_REF)
1180 continue;
1181 goto done;
1184 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1185 got_ref_close(target_ref);
1186 if (error)
1187 goto done;
1189 printf("Setting %s to %s\n", GOT_REF_HEAD,
1190 got_ref_get_symref_target(symref));
1192 error = got_ref_write(symref, repo);
1193 got_ref_close(symref);
1194 break;
1197 done:
1198 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1199 error = got_error_from_errno("close");
1200 if (repo)
1201 got_repo_close(repo);
1202 TAILQ_FOREACH(pe, &refs, entry) {
1203 free((void *)pe->path);
1204 free(pe->data);
1206 got_pathlist_free(&refs);
1207 TAILQ_FOREACH(pe, &symrefs, entry) {
1208 free((void *)pe->path);
1209 free(pe->data);
1211 got_pathlist_free(&symrefs);
1212 free(pack_hash);
1213 free(proto);
1214 free(host);
1215 free(port);
1216 free(server_path);
1217 free(repo_name);
1218 free(default_destdir);
1219 return error;
1222 static const struct got_error *
1223 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1225 static char msg[512];
1226 const char *branch_name;
1228 if (got_ref_is_symbolic(ref))
1229 branch_name = got_ref_get_symref_target(ref);
1230 else
1231 branch_name = got_ref_get_name(ref);
1233 if (strncmp("refs/heads/", branch_name, 11) == 0)
1234 branch_name += 11;
1236 snprintf(msg, sizeof(msg),
1237 "target commit is not contained in branch '%s'; "
1238 "the branch to use must be specified with -b; "
1239 "if necessary a new branch can be created for "
1240 "this commit with 'got branch -c %s BRANCH_NAME'",
1241 branch_name, commit_id_str);
1243 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1246 static const struct got_error *
1247 cmd_checkout(int argc, char *argv[])
1249 const struct got_error *error = NULL;
1250 struct got_repository *repo = NULL;
1251 struct got_reference *head_ref = NULL;
1252 struct got_worktree *worktree = NULL;
1253 char *repo_path = NULL;
1254 char *worktree_path = NULL;
1255 const char *path_prefix = "";
1256 const char *branch_name = GOT_REF_HEAD;
1257 char *commit_id_str = NULL;
1258 int ch, same_path_prefix, allow_nonempty = 0;
1259 struct got_pathlist_head paths;
1260 struct got_checkout_progress_arg cpa;
1262 TAILQ_INIT(&paths);
1264 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1265 switch (ch) {
1266 case 'b':
1267 branch_name = optarg;
1268 break;
1269 case 'c':
1270 commit_id_str = strdup(optarg);
1271 if (commit_id_str == NULL)
1272 return got_error_from_errno("strdup");
1273 break;
1274 case 'E':
1275 allow_nonempty = 1;
1276 break;
1277 case 'p':
1278 path_prefix = optarg;
1279 break;
1280 default:
1281 usage_checkout();
1282 /* NOTREACHED */
1286 argc -= optind;
1287 argv += optind;
1289 #ifndef PROFILE
1290 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1291 "unveil", NULL) == -1)
1292 err(1, "pledge");
1293 #endif
1294 if (argc == 1) {
1295 char *cwd, *base, *dotgit;
1296 repo_path = realpath(argv[0], NULL);
1297 if (repo_path == NULL)
1298 return got_error_from_errno2("realpath", argv[0]);
1299 cwd = getcwd(NULL, 0);
1300 if (cwd == NULL) {
1301 error = got_error_from_errno("getcwd");
1302 goto done;
1304 if (path_prefix[0]) {
1305 base = basename(path_prefix);
1306 if (base == NULL) {
1307 error = got_error_from_errno2("basename",
1308 path_prefix);
1309 goto done;
1311 } else {
1312 base = basename(repo_path);
1313 if (base == NULL) {
1314 error = got_error_from_errno2("basename",
1315 repo_path);
1316 goto done;
1319 dotgit = strstr(base, ".git");
1320 if (dotgit)
1321 *dotgit = '\0';
1322 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1323 error = got_error_from_errno("asprintf");
1324 free(cwd);
1325 goto done;
1327 free(cwd);
1328 } else if (argc == 2) {
1329 repo_path = realpath(argv[0], NULL);
1330 if (repo_path == NULL) {
1331 error = got_error_from_errno2("realpath", argv[0]);
1332 goto done;
1334 worktree_path = realpath(argv[1], NULL);
1335 if (worktree_path == NULL) {
1336 if (errno != ENOENT) {
1337 error = got_error_from_errno2("realpath",
1338 argv[1]);
1339 goto done;
1341 worktree_path = strdup(argv[1]);
1342 if (worktree_path == NULL) {
1343 error = got_error_from_errno("strdup");
1344 goto done;
1347 } else
1348 usage_checkout();
1350 got_path_strip_trailing_slashes(repo_path);
1351 got_path_strip_trailing_slashes(worktree_path);
1353 error = got_repo_open(&repo, repo_path, NULL);
1354 if (error != NULL)
1355 goto done;
1357 /* Pre-create work tree path for unveil(2) */
1358 error = got_path_mkdir(worktree_path);
1359 if (error) {
1360 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1361 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1362 goto done;
1363 if (!allow_nonempty &&
1364 !got_path_dir_is_empty(worktree_path)) {
1365 error = got_error_path(worktree_path,
1366 GOT_ERR_DIR_NOT_EMPTY);
1367 goto done;
1371 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1372 if (error)
1373 goto done;
1375 error = got_ref_open(&head_ref, repo, branch_name, 0);
1376 if (error != NULL)
1377 goto done;
1379 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1380 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1381 goto done;
1383 error = got_worktree_open(&worktree, worktree_path);
1384 if (error != NULL)
1385 goto done;
1387 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1388 path_prefix);
1389 if (error != NULL)
1390 goto done;
1391 if (!same_path_prefix) {
1392 error = got_error(GOT_ERR_PATH_PREFIX);
1393 goto done;
1396 if (commit_id_str) {
1397 struct got_object_id *commit_id;
1398 error = got_repo_match_object_id(&commit_id, NULL,
1399 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1400 if (error)
1401 goto done;
1402 error = check_linear_ancestry(commit_id,
1403 got_worktree_get_base_commit_id(worktree), 0, repo);
1404 if (error != NULL) {
1405 free(commit_id);
1406 if (error->code == GOT_ERR_ANCESTRY) {
1407 error = checkout_ancestry_error(
1408 head_ref, commit_id_str);
1410 goto done;
1412 error = check_same_branch(commit_id, head_ref, NULL, repo);
1413 if (error) {
1414 if (error->code == GOT_ERR_ANCESTRY) {
1415 error = checkout_ancestry_error(
1416 head_ref, commit_id_str);
1418 goto done;
1420 error = got_worktree_set_base_commit_id(worktree, repo,
1421 commit_id);
1422 free(commit_id);
1423 if (error)
1424 goto done;
1427 error = got_pathlist_append(&paths, "", NULL);
1428 if (error)
1429 goto done;
1430 cpa.worktree_path = worktree_path;
1431 cpa.had_base_commit_ref_error = 0;
1432 error = got_worktree_checkout_files(worktree, &paths, repo,
1433 checkout_progress, &cpa, check_cancelled, NULL);
1434 if (error != NULL)
1435 goto done;
1437 printf("Now shut up and hack\n");
1438 if (cpa.had_base_commit_ref_error)
1439 show_worktree_base_ref_warning();
1440 done:
1441 got_pathlist_free(&paths);
1442 free(commit_id_str);
1443 free(repo_path);
1444 free(worktree_path);
1445 return error;
1448 __dead static void
1449 usage_update(void)
1451 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1452 getprogname());
1453 exit(1);
1456 static const struct got_error *
1457 update_progress(void *arg, unsigned char status, const char *path)
1459 int *did_something = arg;
1461 if (status == GOT_STATUS_EXISTS ||
1462 status == GOT_STATUS_BASE_REF_ERR)
1463 return NULL;
1465 *did_something = 1;
1467 /* Base commit bump happens silently. */
1468 if (status == GOT_STATUS_BUMP_BASE)
1469 return NULL;
1471 while (path[0] == '/')
1472 path++;
1473 printf("%c %s\n", status, path);
1474 return NULL;
1477 static const struct got_error *
1478 switch_head_ref(struct got_reference *head_ref,
1479 struct got_object_id *commit_id, struct got_worktree *worktree,
1480 struct got_repository *repo)
1482 const struct got_error *err = NULL;
1483 char *base_id_str;
1484 int ref_has_moved = 0;
1486 /* Trivial case: switching between two different references. */
1487 if (strcmp(got_ref_get_name(head_ref),
1488 got_worktree_get_head_ref_name(worktree)) != 0) {
1489 printf("Switching work tree from %s to %s\n",
1490 got_worktree_get_head_ref_name(worktree),
1491 got_ref_get_name(head_ref));
1492 return got_worktree_set_head_ref(worktree, head_ref);
1495 err = check_linear_ancestry(commit_id,
1496 got_worktree_get_base_commit_id(worktree), 0, repo);
1497 if (err) {
1498 if (err->code != GOT_ERR_ANCESTRY)
1499 return err;
1500 ref_has_moved = 1;
1502 if (!ref_has_moved)
1503 return NULL;
1505 /* Switching to a rebased branch with the same reference name. */
1506 err = got_object_id_str(&base_id_str,
1507 got_worktree_get_base_commit_id(worktree));
1508 if (err)
1509 return err;
1510 printf("Reference %s now points at a different branch\n",
1511 got_worktree_get_head_ref_name(worktree));
1512 printf("Switching work tree from %s to %s\n", base_id_str,
1513 got_worktree_get_head_ref_name(worktree));
1514 return NULL;
1517 static const struct got_error *
1518 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1520 const struct got_error *err;
1521 int in_progress;
1523 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1524 if (err)
1525 return err;
1526 if (in_progress)
1527 return got_error(GOT_ERR_REBASING);
1529 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1530 if (err)
1531 return err;
1532 if (in_progress)
1533 return got_error(GOT_ERR_HISTEDIT_BUSY);
1535 return NULL;
1538 static const struct got_error *
1539 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1540 char *argv[], struct got_worktree *worktree)
1542 const struct got_error *err = NULL;
1543 char *path;
1544 int i;
1546 if (argc == 0) {
1547 path = strdup("");
1548 if (path == NULL)
1549 return got_error_from_errno("strdup");
1550 return got_pathlist_append(paths, path, NULL);
1553 for (i = 0; i < argc; i++) {
1554 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1555 if (err)
1556 break;
1557 err = got_pathlist_append(paths, path, NULL);
1558 if (err) {
1559 free(path);
1560 break;
1564 return err;
1567 static const struct got_error *
1568 cmd_update(int argc, char *argv[])
1570 const struct got_error *error = NULL;
1571 struct got_repository *repo = NULL;
1572 struct got_worktree *worktree = NULL;
1573 char *worktree_path = NULL;
1574 struct got_object_id *commit_id = NULL;
1575 char *commit_id_str = NULL;
1576 const char *branch_name = NULL;
1577 struct got_reference *head_ref = NULL;
1578 struct got_pathlist_head paths;
1579 struct got_pathlist_entry *pe;
1580 int ch, did_something = 0;
1582 TAILQ_INIT(&paths);
1584 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1585 switch (ch) {
1586 case 'b':
1587 branch_name = optarg;
1588 break;
1589 case 'c':
1590 commit_id_str = strdup(optarg);
1591 if (commit_id_str == NULL)
1592 return got_error_from_errno("strdup");
1593 break;
1594 default:
1595 usage_update();
1596 /* NOTREACHED */
1600 argc -= optind;
1601 argv += optind;
1603 #ifndef PROFILE
1604 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1605 "unveil", NULL) == -1)
1606 err(1, "pledge");
1607 #endif
1608 worktree_path = getcwd(NULL, 0);
1609 if (worktree_path == NULL) {
1610 error = got_error_from_errno("getcwd");
1611 goto done;
1613 error = got_worktree_open(&worktree, worktree_path);
1614 if (error)
1615 goto done;
1617 error = check_rebase_or_histedit_in_progress(worktree);
1618 if (error)
1619 goto done;
1621 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1622 NULL);
1623 if (error != NULL)
1624 goto done;
1626 error = apply_unveil(got_repo_get_path(repo), 0,
1627 got_worktree_get_root_path(worktree));
1628 if (error)
1629 goto done;
1631 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1632 if (error)
1633 goto done;
1635 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1636 got_worktree_get_head_ref_name(worktree), 0);
1637 if (error != NULL)
1638 goto done;
1639 if (commit_id_str == NULL) {
1640 error = got_ref_resolve(&commit_id, repo, head_ref);
1641 if (error != NULL)
1642 goto done;
1643 error = got_object_id_str(&commit_id_str, commit_id);
1644 if (error != NULL)
1645 goto done;
1646 } else {
1647 error = got_repo_match_object_id(&commit_id, NULL,
1648 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1649 free(commit_id_str);
1650 commit_id_str = NULL;
1651 if (error)
1652 goto done;
1653 error = got_object_id_str(&commit_id_str, commit_id);
1654 if (error)
1655 goto done;
1658 if (branch_name) {
1659 struct got_object_id *head_commit_id;
1660 TAILQ_FOREACH(pe, &paths, entry) {
1661 if (pe->path_len == 0)
1662 continue;
1663 error = got_error_msg(GOT_ERR_BAD_PATH,
1664 "switching between branches requires that "
1665 "the entire work tree gets updated");
1666 goto done;
1668 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1669 if (error)
1670 goto done;
1671 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1672 repo);
1673 free(head_commit_id);
1674 if (error != NULL)
1675 goto done;
1676 error = check_same_branch(commit_id, head_ref, NULL, repo);
1677 if (error)
1678 goto done;
1679 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1680 if (error)
1681 goto done;
1682 } else {
1683 error = check_linear_ancestry(commit_id,
1684 got_worktree_get_base_commit_id(worktree), 0, repo);
1685 if (error != NULL) {
1686 if (error->code == GOT_ERR_ANCESTRY)
1687 error = got_error(GOT_ERR_BRANCH_MOVED);
1688 goto done;
1690 error = check_same_branch(commit_id, head_ref, NULL, repo);
1691 if (error)
1692 goto done;
1695 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1696 commit_id) != 0) {
1697 error = got_worktree_set_base_commit_id(worktree, repo,
1698 commit_id);
1699 if (error)
1700 goto done;
1703 error = got_worktree_checkout_files(worktree, &paths, repo,
1704 update_progress, &did_something, check_cancelled, NULL);
1705 if (error != NULL)
1706 goto done;
1708 if (did_something)
1709 printf("Updated to commit %s\n", commit_id_str);
1710 else
1711 printf("Already up-to-date\n");
1712 done:
1713 free(worktree_path);
1714 TAILQ_FOREACH(pe, &paths, entry)
1715 free((char *)pe->path);
1716 got_pathlist_free(&paths);
1717 free(commit_id);
1718 free(commit_id_str);
1719 return error;
1722 static const struct got_error *
1723 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1724 const char *path, int diff_context, int ignore_whitespace,
1725 struct got_repository *repo)
1727 const struct got_error *err = NULL;
1728 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1730 if (blob_id1) {
1731 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1732 if (err)
1733 goto done;
1736 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1737 if (err)
1738 goto done;
1740 while (path[0] == '/')
1741 path++;
1742 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1743 ignore_whitespace, stdout);
1744 done:
1745 if (blob1)
1746 got_object_blob_close(blob1);
1747 got_object_blob_close(blob2);
1748 return err;
1751 static const struct got_error *
1752 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1753 const char *path, int diff_context, int ignore_whitespace,
1754 struct got_repository *repo)
1756 const struct got_error *err = NULL;
1757 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1758 struct got_diff_blob_output_unidiff_arg arg;
1760 if (tree_id1) {
1761 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1762 if (err)
1763 goto done;
1766 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1767 if (err)
1768 goto done;
1770 arg.diff_context = diff_context;
1771 arg.ignore_whitespace = ignore_whitespace;
1772 arg.outfile = stdout;
1773 while (path[0] == '/')
1774 path++;
1775 err = got_diff_tree(tree1, tree2, path, path, repo,
1776 got_diff_blob_output_unidiff, &arg, 1);
1777 done:
1778 if (tree1)
1779 got_object_tree_close(tree1);
1780 if (tree2)
1781 got_object_tree_close(tree2);
1782 return err;
1785 static const struct got_error *
1786 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1787 const char *path, int diff_context, struct got_repository *repo)
1789 const struct got_error *err = NULL;
1790 struct got_commit_object *pcommit = NULL;
1791 char *id_str1 = NULL, *id_str2 = NULL;
1792 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1793 struct got_object_qid *qid;
1795 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1796 if (qid != NULL) {
1797 err = got_object_open_as_commit(&pcommit, repo,
1798 qid->id);
1799 if (err)
1800 return err;
1803 if (path && path[0] != '\0') {
1804 int obj_type;
1805 err = got_object_id_by_path(&obj_id2, repo, id, path);
1806 if (err)
1807 goto done;
1808 err = got_object_id_str(&id_str2, obj_id2);
1809 if (err) {
1810 free(obj_id2);
1811 goto done;
1813 if (pcommit) {
1814 err = got_object_id_by_path(&obj_id1, repo,
1815 qid->id, path);
1816 if (err) {
1817 free(obj_id2);
1818 goto done;
1820 err = got_object_id_str(&id_str1, obj_id1);
1821 if (err) {
1822 free(obj_id2);
1823 goto done;
1826 err = got_object_get_type(&obj_type, repo, obj_id2);
1827 if (err) {
1828 free(obj_id2);
1829 goto done;
1831 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1832 switch (obj_type) {
1833 case GOT_OBJ_TYPE_BLOB:
1834 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1835 0, repo);
1836 break;
1837 case GOT_OBJ_TYPE_TREE:
1838 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1839 0, repo);
1840 break;
1841 default:
1842 err = got_error(GOT_ERR_OBJ_TYPE);
1843 break;
1845 free(obj_id1);
1846 free(obj_id2);
1847 } else {
1848 obj_id2 = got_object_commit_get_tree_id(commit);
1849 err = got_object_id_str(&id_str2, obj_id2);
1850 if (err)
1851 goto done;
1852 obj_id1 = got_object_commit_get_tree_id(pcommit);
1853 err = got_object_id_str(&id_str1, obj_id1);
1854 if (err)
1855 goto done;
1856 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1857 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1859 done:
1860 free(id_str1);
1861 free(id_str2);
1862 if (pcommit)
1863 got_object_commit_close(pcommit);
1864 return err;
1867 static char *
1868 get_datestr(time_t *time, char *datebuf)
1870 struct tm mytm, *tm;
1871 char *p, *s;
1873 tm = gmtime_r(time, &mytm);
1874 if (tm == NULL)
1875 return NULL;
1876 s = asctime_r(tm, datebuf);
1877 if (s == NULL)
1878 return NULL;
1879 p = strchr(s, '\n');
1880 if (p)
1881 *p = '\0';
1882 return s;
1885 static const struct got_error *
1886 match_logmsg(int *have_match, struct got_object_id *id,
1887 struct got_commit_object *commit, regex_t *regex)
1889 const struct got_error *err = NULL;
1890 regmatch_t regmatch;
1891 char *id_str = NULL, *logmsg = NULL;
1893 *have_match = 0;
1895 err = got_object_id_str(&id_str, id);
1896 if (err)
1897 return err;
1899 err = got_object_commit_get_logmsg(&logmsg, commit);
1900 if (err)
1901 goto done;
1903 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1904 *have_match = 1;
1905 done:
1906 free(id_str);
1907 free(logmsg);
1908 return err;
1911 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1913 static const struct got_error *
1914 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1915 struct got_repository *repo, const char *path, int show_patch,
1916 int diff_context, struct got_reflist_head *refs)
1918 const struct got_error *err = NULL;
1919 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1920 char datebuf[26];
1921 time_t committer_time;
1922 const char *author, *committer;
1923 char *refs_str = NULL;
1924 struct got_reflist_entry *re;
1926 SIMPLEQ_FOREACH(re, refs, entry) {
1927 char *s;
1928 const char *name;
1929 struct got_tag_object *tag = NULL;
1930 int cmp;
1932 name = got_ref_get_name(re->ref);
1933 if (strcmp(name, GOT_REF_HEAD) == 0)
1934 continue;
1935 if (strncmp(name, "refs/", 5) == 0)
1936 name += 5;
1937 if (strncmp(name, "got/", 4) == 0)
1938 continue;
1939 if (strncmp(name, "heads/", 6) == 0)
1940 name += 6;
1941 if (strncmp(name, "remotes/", 8) == 0)
1942 name += 8;
1943 if (strncmp(name, "tags/", 5) == 0) {
1944 err = got_object_open_as_tag(&tag, repo, re->id);
1945 if (err) {
1946 if (err->code != GOT_ERR_OBJ_TYPE)
1947 return err;
1948 /* Ref points at something other than a tag. */
1949 err = NULL;
1950 tag = NULL;
1953 cmp = got_object_id_cmp(tag ?
1954 got_object_tag_get_object_id(tag) : re->id, id);
1955 if (tag)
1956 got_object_tag_close(tag);
1957 if (cmp != 0)
1958 continue;
1959 s = refs_str;
1960 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1961 name) == -1) {
1962 err = got_error_from_errno("asprintf");
1963 free(s);
1964 return err;
1966 free(s);
1968 err = got_object_id_str(&id_str, id);
1969 if (err)
1970 return err;
1972 printf(GOT_COMMIT_SEP_STR);
1973 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1974 refs_str ? refs_str : "", refs_str ? ")" : "");
1975 free(id_str);
1976 id_str = NULL;
1977 free(refs_str);
1978 refs_str = NULL;
1979 printf("from: %s\n", got_object_commit_get_author(commit));
1980 committer_time = got_object_commit_get_committer_time(commit);
1981 datestr = get_datestr(&committer_time, datebuf);
1982 if (datestr)
1983 printf("date: %s UTC\n", datestr);
1984 author = got_object_commit_get_author(commit);
1985 committer = got_object_commit_get_committer(commit);
1986 if (strcmp(author, committer) != 0)
1987 printf("via: %s\n", committer);
1988 if (got_object_commit_get_nparents(commit) > 1) {
1989 const struct got_object_id_queue *parent_ids;
1990 struct got_object_qid *qid;
1991 int n = 1;
1992 parent_ids = got_object_commit_get_parent_ids(commit);
1993 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1994 err = got_object_id_str(&id_str, qid->id);
1995 if (err)
1996 return err;
1997 printf("parent %d: %s\n", n++, id_str);
1998 free(id_str);
2002 err = got_object_commit_get_logmsg(&logmsg0, commit);
2003 if (err)
2004 return err;
2006 logmsg = logmsg0;
2007 do {
2008 line = strsep(&logmsg, "\n");
2009 if (line)
2010 printf(" %s\n", line);
2011 } while (line);
2012 free(logmsg0);
2014 if (show_patch) {
2015 err = print_patch(commit, id, path, diff_context, repo);
2016 if (err == 0)
2017 printf("\n");
2020 if (fflush(stdout) != 0 && err == NULL)
2021 err = got_error_from_errno("fflush");
2022 return err;
2025 static const struct got_error *
2026 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2027 const char *path, int show_patch, const char *search_pattern,
2028 int diff_context, int limit, int log_branches,
2029 struct got_reflist_head *refs)
2031 const struct got_error *err;
2032 struct got_commit_graph *graph;
2033 regex_t regex;
2034 int have_match;
2036 if (search_pattern &&
2037 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2038 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2040 err = got_commit_graph_open(&graph, path, !log_branches);
2041 if (err)
2042 return err;
2043 err = got_commit_graph_iter_start(graph, root_id, repo,
2044 check_cancelled, NULL);
2045 if (err)
2046 goto done;
2047 for (;;) {
2048 struct got_commit_object *commit;
2049 struct got_object_id *id;
2051 if (sigint_received || sigpipe_received)
2052 break;
2054 err = got_commit_graph_iter_next(&id, graph, repo,
2055 check_cancelled, NULL);
2056 if (err) {
2057 if (err->code == GOT_ERR_ITER_COMPLETED)
2058 err = NULL;
2059 break;
2061 if (id == NULL)
2062 break;
2064 err = got_object_open_as_commit(&commit, repo, id);
2065 if (err)
2066 break;
2068 if (search_pattern) {
2069 err = match_logmsg(&have_match, id, commit, &regex);
2070 if (err) {
2071 got_object_commit_close(commit);
2072 break;
2074 if (have_match == 0) {
2075 got_object_commit_close(commit);
2076 continue;
2080 err = print_commit(commit, id, repo, path, show_patch,
2081 diff_context, refs);
2082 got_object_commit_close(commit);
2083 if (err || (limit && --limit == 0))
2084 break;
2086 done:
2087 if (search_pattern)
2088 regfree(&regex);
2089 got_commit_graph_close(graph);
2090 return err;
2093 __dead static void
2094 usage_log(void)
2096 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2097 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2098 exit(1);
2101 static int
2102 get_default_log_limit(void)
2104 const char *got_default_log_limit;
2105 long long n;
2106 const char *errstr;
2108 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2109 if (got_default_log_limit == NULL)
2110 return 0;
2111 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2112 if (errstr != NULL)
2113 return 0;
2114 return n;
2117 static const struct got_error *
2118 cmd_log(int argc, char *argv[])
2120 const struct got_error *error;
2121 struct got_repository *repo = NULL;
2122 struct got_worktree *worktree = NULL;
2123 struct got_commit_object *commit = NULL;
2124 struct got_object_id *id = NULL;
2125 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2126 const char *start_commit = NULL, *search_pattern = NULL;
2127 int diff_context = -1, ch;
2128 int show_patch = 0, limit = 0, log_branches = 0;
2129 const char *errstr;
2130 struct got_reflist_head refs;
2132 SIMPLEQ_INIT(&refs);
2134 #ifndef PROFILE
2135 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2136 NULL)
2137 == -1)
2138 err(1, "pledge");
2139 #endif
2141 limit = get_default_log_limit();
2143 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2144 switch (ch) {
2145 case 'p':
2146 show_patch = 1;
2147 break;
2148 case 'c':
2149 start_commit = optarg;
2150 break;
2151 case 'C':
2152 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2153 &errstr);
2154 if (errstr != NULL)
2155 err(1, "-C option %s", errstr);
2156 break;
2157 case 'l':
2158 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2159 if (errstr != NULL)
2160 err(1, "-l option %s", errstr);
2161 break;
2162 case 'b':
2163 log_branches = 1;
2164 break;
2165 case 'r':
2166 repo_path = realpath(optarg, NULL);
2167 if (repo_path == NULL)
2168 return got_error_from_errno2("realpath",
2169 optarg);
2170 got_path_strip_trailing_slashes(repo_path);
2171 break;
2172 case 's':
2173 search_pattern = optarg;
2174 break;
2175 default:
2176 usage_log();
2177 /* NOTREACHED */
2181 argc -= optind;
2182 argv += optind;
2184 if (diff_context == -1)
2185 diff_context = 3;
2186 else if (!show_patch)
2187 errx(1, "-C reguires -p");
2189 cwd = getcwd(NULL, 0);
2190 if (cwd == NULL) {
2191 error = got_error_from_errno("getcwd");
2192 goto done;
2195 error = got_worktree_open(&worktree, cwd);
2196 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2197 goto done;
2198 error = NULL;
2200 if (argc == 0) {
2201 path = strdup("");
2202 if (path == NULL) {
2203 error = got_error_from_errno("strdup");
2204 goto done;
2206 } else if (argc == 1) {
2207 if (worktree) {
2208 error = got_worktree_resolve_path(&path, worktree,
2209 argv[0]);
2210 if (error)
2211 goto done;
2212 } else {
2213 path = strdup(argv[0]);
2214 if (path == NULL) {
2215 error = got_error_from_errno("strdup");
2216 goto done;
2219 } else
2220 usage_log();
2222 if (repo_path == NULL) {
2223 repo_path = worktree ?
2224 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2226 if (repo_path == NULL) {
2227 error = got_error_from_errno("strdup");
2228 goto done;
2231 error = got_repo_open(&repo, repo_path, NULL);
2232 if (error != NULL)
2233 goto done;
2235 error = apply_unveil(got_repo_get_path(repo), 1,
2236 worktree ? got_worktree_get_root_path(worktree) : NULL);
2237 if (error)
2238 goto done;
2240 if (start_commit == NULL) {
2241 struct got_reference *head_ref;
2242 error = got_ref_open(&head_ref, repo,
2243 worktree ? got_worktree_get_head_ref_name(worktree)
2244 : GOT_REF_HEAD, 0);
2245 if (error != NULL)
2246 return error;
2247 error = got_ref_resolve(&id, repo, head_ref);
2248 got_ref_close(head_ref);
2249 if (error != NULL)
2250 return error;
2251 error = got_object_open_as_commit(&commit, repo, id);
2252 } else {
2253 struct got_reference *ref;
2254 error = got_ref_open(&ref, repo, start_commit, 0);
2255 if (error == NULL) {
2256 int obj_type;
2257 error = got_ref_resolve(&id, repo, ref);
2258 got_ref_close(ref);
2259 if (error != NULL)
2260 goto done;
2261 error = got_object_get_type(&obj_type, repo, id);
2262 if (error != NULL)
2263 goto done;
2264 if (obj_type == GOT_OBJ_TYPE_TAG) {
2265 struct got_tag_object *tag;
2266 error = got_object_open_as_tag(&tag, repo, id);
2267 if (error != NULL)
2268 goto done;
2269 if (got_object_tag_get_object_type(tag) !=
2270 GOT_OBJ_TYPE_COMMIT) {
2271 got_object_tag_close(tag);
2272 error = got_error(GOT_ERR_OBJ_TYPE);
2273 goto done;
2275 free(id);
2276 id = got_object_id_dup(
2277 got_object_tag_get_object_id(tag));
2278 if (id == NULL)
2279 error = got_error_from_errno(
2280 "got_object_id_dup");
2281 got_object_tag_close(tag);
2282 if (error)
2283 goto done;
2284 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2285 error = got_error(GOT_ERR_OBJ_TYPE);
2286 goto done;
2288 error = got_object_open_as_commit(&commit, repo, id);
2289 if (error != NULL)
2290 goto done;
2292 if (commit == NULL) {
2293 error = got_repo_match_object_id_prefix(&id,
2294 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2295 if (error != NULL)
2296 return error;
2299 if (error != NULL)
2300 goto done;
2302 if (worktree) {
2303 const char *prefix = got_worktree_get_path_prefix(worktree);
2304 char *p;
2305 if (asprintf(&p, "%s%s%s", prefix,
2306 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2307 error = got_error_from_errno("asprintf");
2308 goto done;
2310 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2311 free(p);
2312 } else
2313 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2314 if (error != NULL)
2315 goto done;
2316 if (in_repo_path) {
2317 free(path);
2318 path = in_repo_path;
2321 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2322 if (error)
2323 goto done;
2325 error = print_commits(id, repo, path, show_patch, search_pattern,
2326 diff_context, limit, log_branches, &refs);
2327 done:
2328 free(path);
2329 free(repo_path);
2330 free(cwd);
2331 free(id);
2332 if (worktree)
2333 got_worktree_close(worktree);
2334 if (repo) {
2335 const struct got_error *repo_error;
2336 repo_error = got_repo_close(repo);
2337 if (error == NULL)
2338 error = repo_error;
2340 got_ref_list_free(&refs);
2341 return error;
2344 __dead static void
2345 usage_diff(void)
2347 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2348 "[-w] [object1 object2 | path]\n", getprogname());
2349 exit(1);
2352 struct print_diff_arg {
2353 struct got_repository *repo;
2354 struct got_worktree *worktree;
2355 int diff_context;
2356 const char *id_str;
2357 int header_shown;
2358 int diff_staged;
2359 int ignore_whitespace;
2362 static const struct got_error *
2363 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2364 const char *path, struct got_object_id *blob_id,
2365 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2366 int dirfd, const char *de_name)
2368 struct print_diff_arg *a = arg;
2369 const struct got_error *err = NULL;
2370 struct got_blob_object *blob1 = NULL;
2371 int fd = -1;
2372 FILE *f2 = NULL;
2373 char *abspath = NULL, *label1 = NULL;
2374 struct stat sb;
2376 if (a->diff_staged) {
2377 if (staged_status != GOT_STATUS_MODIFY &&
2378 staged_status != GOT_STATUS_ADD &&
2379 staged_status != GOT_STATUS_DELETE)
2380 return NULL;
2381 } else {
2382 if (staged_status == GOT_STATUS_DELETE)
2383 return NULL;
2384 if (status == GOT_STATUS_NONEXISTENT)
2385 return got_error_set_errno(ENOENT, path);
2386 if (status != GOT_STATUS_MODIFY &&
2387 status != GOT_STATUS_ADD &&
2388 status != GOT_STATUS_DELETE &&
2389 status != GOT_STATUS_CONFLICT)
2390 return NULL;
2393 if (!a->header_shown) {
2394 printf("diff %s %s%s\n", a->id_str,
2395 got_worktree_get_root_path(a->worktree),
2396 a->diff_staged ? " (staged changes)" : "");
2397 a->header_shown = 1;
2400 if (a->diff_staged) {
2401 const char *label1 = NULL, *label2 = NULL;
2402 switch (staged_status) {
2403 case GOT_STATUS_MODIFY:
2404 label1 = path;
2405 label2 = path;
2406 break;
2407 case GOT_STATUS_ADD:
2408 label2 = path;
2409 break;
2410 case GOT_STATUS_DELETE:
2411 label1 = path;
2412 break;
2413 default:
2414 return got_error(GOT_ERR_FILE_STATUS);
2416 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2417 label1, label2, a->diff_context, a->ignore_whitespace,
2418 a->repo, stdout);
2421 if (staged_status == GOT_STATUS_ADD ||
2422 staged_status == GOT_STATUS_MODIFY) {
2423 char *id_str;
2424 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2425 8192);
2426 if (err)
2427 goto done;
2428 err = got_object_id_str(&id_str, staged_blob_id);
2429 if (err)
2430 goto done;
2431 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2432 err = got_error_from_errno("asprintf");
2433 free(id_str);
2434 goto done;
2436 free(id_str);
2437 } else if (status != GOT_STATUS_ADD) {
2438 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2439 if (err)
2440 goto done;
2443 if (status != GOT_STATUS_DELETE) {
2444 if (asprintf(&abspath, "%s/%s",
2445 got_worktree_get_root_path(a->worktree), path) == -1) {
2446 err = got_error_from_errno("asprintf");
2447 goto done;
2450 if (dirfd != -1) {
2451 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2452 if (fd == -1) {
2453 err = got_error_from_errno2("openat", abspath);
2454 goto done;
2456 } else {
2457 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2458 if (fd == -1) {
2459 err = got_error_from_errno2("open", abspath);
2460 goto done;
2463 if (fstat(fd, &sb) == -1) {
2464 err = got_error_from_errno2("fstat", abspath);
2465 goto done;
2467 f2 = fdopen(fd, "r");
2468 if (f2 == NULL) {
2469 err = got_error_from_errno2("fdopen", abspath);
2470 goto done;
2472 fd = -1;
2473 } else
2474 sb.st_size = 0;
2476 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2477 a->diff_context, a->ignore_whitespace, stdout);
2478 done:
2479 if (blob1)
2480 got_object_blob_close(blob1);
2481 if (f2 && fclose(f2) == EOF && err == NULL)
2482 err = got_error_from_errno("fclose");
2483 if (fd != -1 && close(fd) == -1 && err == NULL)
2484 err = got_error_from_errno("close");
2485 free(abspath);
2486 return err;
2489 static const struct got_error *
2490 cmd_diff(int argc, char *argv[])
2492 const struct got_error *error;
2493 struct got_repository *repo = NULL;
2494 struct got_worktree *worktree = NULL;
2495 char *cwd = NULL, *repo_path = NULL;
2496 struct got_object_id *id1 = NULL, *id2 = NULL;
2497 const char *id_str1 = NULL, *id_str2 = NULL;
2498 char *label1 = NULL, *label2 = NULL;
2499 int type1, type2;
2500 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2501 const char *errstr;
2502 char *path = NULL;
2504 #ifndef PROFILE
2505 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2506 NULL) == -1)
2507 err(1, "pledge");
2508 #endif
2510 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2511 switch (ch) {
2512 case 'C':
2513 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2514 &errstr);
2515 if (errstr != NULL)
2516 err(1, "-C option %s", errstr);
2517 break;
2518 case 'r':
2519 repo_path = realpath(optarg, NULL);
2520 if (repo_path == NULL)
2521 return got_error_from_errno2("realpath",
2522 optarg);
2523 got_path_strip_trailing_slashes(repo_path);
2524 break;
2525 case 's':
2526 diff_staged = 1;
2527 break;
2528 case 'w':
2529 ignore_whitespace = 1;
2530 break;
2531 default:
2532 usage_diff();
2533 /* NOTREACHED */
2537 argc -= optind;
2538 argv += optind;
2540 cwd = getcwd(NULL, 0);
2541 if (cwd == NULL) {
2542 error = got_error_from_errno("getcwd");
2543 goto done;
2545 error = got_worktree_open(&worktree, cwd);
2546 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2547 goto done;
2548 if (argc <= 1) {
2549 if (worktree == NULL) {
2550 error = got_error(GOT_ERR_NOT_WORKTREE);
2551 goto done;
2553 if (repo_path)
2554 errx(1,
2555 "-r option can't be used when diffing a work tree");
2556 repo_path = strdup(got_worktree_get_repo_path(worktree));
2557 if (repo_path == NULL) {
2558 error = got_error_from_errno("strdup");
2559 goto done;
2561 if (argc == 1) {
2562 error = got_worktree_resolve_path(&path, worktree,
2563 argv[0]);
2564 if (error)
2565 goto done;
2566 } else {
2567 path = strdup("");
2568 if (path == NULL) {
2569 error = got_error_from_errno("strdup");
2570 goto done;
2573 } else if (argc == 2) {
2574 if (diff_staged)
2575 errx(1, "-s option can't be used when diffing "
2576 "objects in repository");
2577 id_str1 = argv[0];
2578 id_str2 = argv[1];
2579 if (worktree && repo_path == NULL) {
2580 repo_path =
2581 strdup(got_worktree_get_repo_path(worktree));
2582 if (repo_path == NULL) {
2583 error = got_error_from_errno("strdup");
2584 goto done;
2587 } else
2588 usage_diff();
2590 if (repo_path == NULL) {
2591 repo_path = getcwd(NULL, 0);
2592 if (repo_path == NULL)
2593 return got_error_from_errno("getcwd");
2596 error = got_repo_open(&repo, repo_path, NULL);
2597 free(repo_path);
2598 if (error != NULL)
2599 goto done;
2601 error = apply_unveil(got_repo_get_path(repo), 1,
2602 worktree ? got_worktree_get_root_path(worktree) : NULL);
2603 if (error)
2604 goto done;
2606 if (argc <= 1) {
2607 struct print_diff_arg arg;
2608 struct got_pathlist_head paths;
2609 char *id_str;
2611 TAILQ_INIT(&paths);
2613 error = got_object_id_str(&id_str,
2614 got_worktree_get_base_commit_id(worktree));
2615 if (error)
2616 goto done;
2617 arg.repo = repo;
2618 arg.worktree = worktree;
2619 arg.diff_context = diff_context;
2620 arg.id_str = id_str;
2621 arg.header_shown = 0;
2622 arg.diff_staged = diff_staged;
2623 arg.ignore_whitespace = ignore_whitespace;
2625 error = got_pathlist_append(&paths, path, NULL);
2626 if (error)
2627 goto done;
2629 error = got_worktree_status(worktree, &paths, repo, print_diff,
2630 &arg, check_cancelled, NULL);
2631 free(id_str);
2632 got_pathlist_free(&paths);
2633 goto done;
2636 error = got_repo_match_object_id(&id1, &label1, id_str1,
2637 GOT_OBJ_TYPE_ANY, 1, repo);
2638 if (error)
2639 goto done;
2641 error = got_repo_match_object_id(&id2, &label2, id_str2,
2642 GOT_OBJ_TYPE_ANY, 1, repo);
2643 if (error)
2644 goto done;
2646 error = got_object_get_type(&type1, repo, id1);
2647 if (error)
2648 goto done;
2650 error = got_object_get_type(&type2, repo, id2);
2651 if (error)
2652 goto done;
2654 if (type1 != type2) {
2655 error = got_error(GOT_ERR_OBJ_TYPE);
2656 goto done;
2659 switch (type1) {
2660 case GOT_OBJ_TYPE_BLOB:
2661 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2662 diff_context, ignore_whitespace, repo, stdout);
2663 break;
2664 case GOT_OBJ_TYPE_TREE:
2665 error = got_diff_objects_as_trees(id1, id2, "", "",
2666 diff_context, ignore_whitespace, repo, stdout);
2667 break;
2668 case GOT_OBJ_TYPE_COMMIT:
2669 printf("diff %s %s\n", label1, label2);
2670 error = got_diff_objects_as_commits(id1, id2, diff_context,
2671 ignore_whitespace, repo, stdout);
2672 break;
2673 default:
2674 error = got_error(GOT_ERR_OBJ_TYPE);
2676 done:
2677 free(label1);
2678 free(label2);
2679 free(id1);
2680 free(id2);
2681 free(path);
2682 if (worktree)
2683 got_worktree_close(worktree);
2684 if (repo) {
2685 const struct got_error *repo_error;
2686 repo_error = got_repo_close(repo);
2687 if (error == NULL)
2688 error = repo_error;
2690 return error;
2693 __dead static void
2694 usage_blame(void)
2696 fprintf(stderr,
2697 "usage: %s blame [-c commit] [-r repository-path] path\n",
2698 getprogname());
2699 exit(1);
2702 struct blame_line {
2703 int annotated;
2704 char *id_str;
2705 char *committer;
2706 char datebuf[11]; /* YYYY-MM-DD + NUL */
2709 struct blame_cb_args {
2710 struct blame_line *lines;
2711 int nlines;
2712 int nlines_prec;
2713 int lineno_cur;
2714 off_t *line_offsets;
2715 FILE *f;
2716 struct got_repository *repo;
2719 static const struct got_error *
2720 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2722 const struct got_error *err = NULL;
2723 struct blame_cb_args *a = arg;
2724 struct blame_line *bline;
2725 char *line = NULL;
2726 size_t linesize = 0;
2727 struct got_commit_object *commit = NULL;
2728 off_t offset;
2729 struct tm tm;
2730 time_t committer_time;
2732 if (nlines != a->nlines ||
2733 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2734 return got_error(GOT_ERR_RANGE);
2736 if (sigint_received)
2737 return got_error(GOT_ERR_ITER_COMPLETED);
2739 if (lineno == -1)
2740 return NULL; /* no change in this commit */
2742 /* Annotate this line. */
2743 bline = &a->lines[lineno - 1];
2744 if (bline->annotated)
2745 return NULL;
2746 err = got_object_id_str(&bline->id_str, id);
2747 if (err)
2748 return err;
2750 err = got_object_open_as_commit(&commit, a->repo, id);
2751 if (err)
2752 goto done;
2754 bline->committer = strdup(got_object_commit_get_committer(commit));
2755 if (bline->committer == NULL) {
2756 err = got_error_from_errno("strdup");
2757 goto done;
2760 committer_time = got_object_commit_get_committer_time(commit);
2761 if (localtime_r(&committer_time, &tm) == NULL)
2762 return got_error_from_errno("localtime_r");
2763 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2764 &tm) >= sizeof(bline->datebuf)) {
2765 err = got_error(GOT_ERR_NO_SPACE);
2766 goto done;
2768 bline->annotated = 1;
2770 /* Print lines annotated so far. */
2771 bline = &a->lines[a->lineno_cur - 1];
2772 if (!bline->annotated)
2773 goto done;
2775 offset = a->line_offsets[a->lineno_cur - 1];
2776 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2777 err = got_error_from_errno("fseeko");
2778 goto done;
2781 while (bline->annotated) {
2782 char *smallerthan, *at, *nl, *committer;
2783 size_t len;
2785 if (getline(&line, &linesize, a->f) == -1) {
2786 if (ferror(a->f))
2787 err = got_error_from_errno("getline");
2788 break;
2791 committer = bline->committer;
2792 smallerthan = strchr(committer, '<');
2793 if (smallerthan && smallerthan[1] != '\0')
2794 committer = smallerthan + 1;
2795 at = strchr(committer, '@');
2796 if (at)
2797 *at = '\0';
2798 len = strlen(committer);
2799 if (len >= 9)
2800 committer[8] = '\0';
2802 nl = strchr(line, '\n');
2803 if (nl)
2804 *nl = '\0';
2805 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2806 bline->id_str, bline->datebuf, committer, line);
2808 a->lineno_cur++;
2809 bline = &a->lines[a->lineno_cur - 1];
2811 done:
2812 if (commit)
2813 got_object_commit_close(commit);
2814 free(line);
2815 return err;
2818 static const struct got_error *
2819 cmd_blame(int argc, char *argv[])
2821 const struct got_error *error;
2822 struct got_repository *repo = NULL;
2823 struct got_worktree *worktree = NULL;
2824 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2825 struct got_object_id *obj_id = NULL;
2826 struct got_object_id *commit_id = NULL;
2827 struct got_blob_object *blob = NULL;
2828 char *commit_id_str = NULL;
2829 struct blame_cb_args bca;
2830 int ch, obj_type, i;
2831 size_t filesize;
2833 memset(&bca, 0, sizeof(bca));
2835 #ifndef PROFILE
2836 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2837 NULL) == -1)
2838 err(1, "pledge");
2839 #endif
2841 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2842 switch (ch) {
2843 case 'c':
2844 commit_id_str = optarg;
2845 break;
2846 case 'r':
2847 repo_path = realpath(optarg, NULL);
2848 if (repo_path == NULL)
2849 return got_error_from_errno2("realpath",
2850 optarg);
2851 got_path_strip_trailing_slashes(repo_path);
2852 break;
2853 default:
2854 usage_blame();
2855 /* NOTREACHED */
2859 argc -= optind;
2860 argv += optind;
2862 if (argc == 1)
2863 path = argv[0];
2864 else
2865 usage_blame();
2867 cwd = getcwd(NULL, 0);
2868 if (cwd == NULL) {
2869 error = got_error_from_errno("getcwd");
2870 goto done;
2872 if (repo_path == NULL) {
2873 error = got_worktree_open(&worktree, cwd);
2874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2875 goto done;
2876 else
2877 error = NULL;
2878 if (worktree) {
2879 repo_path =
2880 strdup(got_worktree_get_repo_path(worktree));
2881 if (repo_path == NULL)
2882 error = got_error_from_errno("strdup");
2883 if (error)
2884 goto done;
2885 } else {
2886 repo_path = strdup(cwd);
2887 if (repo_path == NULL) {
2888 error = got_error_from_errno("strdup");
2889 goto done;
2894 error = got_repo_open(&repo, repo_path, NULL);
2895 if (error != NULL)
2896 goto done;
2898 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2899 if (error)
2900 goto done;
2902 if (worktree) {
2903 const char *prefix = got_worktree_get_path_prefix(worktree);
2904 char *p, *worktree_subdir = cwd +
2905 strlen(got_worktree_get_root_path(worktree));
2906 if (asprintf(&p, "%s%s%s%s%s",
2907 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2908 worktree_subdir, worktree_subdir[0] ? "/" : "",
2909 path) == -1) {
2910 error = got_error_from_errno("asprintf");
2911 goto done;
2913 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2914 free(p);
2915 } else {
2916 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2918 if (error)
2919 goto done;
2921 if (commit_id_str == NULL) {
2922 struct got_reference *head_ref;
2923 error = got_ref_open(&head_ref, repo, worktree ?
2924 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2925 if (error != NULL)
2926 goto done;
2927 error = got_ref_resolve(&commit_id, repo, head_ref);
2928 got_ref_close(head_ref);
2929 if (error != NULL)
2930 goto done;
2931 } else {
2932 error = got_repo_match_object_id(&commit_id, NULL,
2933 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2934 if (error)
2935 goto done;
2938 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2939 if (error)
2940 goto done;
2942 error = got_object_get_type(&obj_type, repo, obj_id);
2943 if (error)
2944 goto done;
2946 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2947 error = got_error(GOT_ERR_OBJ_TYPE);
2948 goto done;
2951 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2952 if (error)
2953 goto done;
2954 bca.f = got_opentemp();
2955 if (bca.f == NULL) {
2956 error = got_error_from_errno("got_opentemp");
2957 goto done;
2959 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2960 &bca.line_offsets, bca.f, blob);
2961 if (error || bca.nlines == 0)
2962 goto done;
2964 /* Don't include \n at EOF in the blame line count. */
2965 if (bca.line_offsets[bca.nlines - 1] == filesize)
2966 bca.nlines--;
2968 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2969 if (bca.lines == NULL) {
2970 error = got_error_from_errno("calloc");
2971 goto done;
2973 bca.lineno_cur = 1;
2974 bca.nlines_prec = 0;
2975 i = bca.nlines;
2976 while (i > 0) {
2977 i /= 10;
2978 bca.nlines_prec++;
2980 bca.repo = repo;
2982 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2983 check_cancelled, NULL);
2984 done:
2985 free(in_repo_path);
2986 free(repo_path);
2987 free(cwd);
2988 free(commit_id);
2989 free(obj_id);
2990 if (blob)
2991 got_object_blob_close(blob);
2992 if (worktree)
2993 got_worktree_close(worktree);
2994 if (repo) {
2995 const struct got_error *repo_error;
2996 repo_error = got_repo_close(repo);
2997 if (error == NULL)
2998 error = repo_error;
3000 if (bca.lines) {
3001 for (i = 0; i < bca.nlines; i++) {
3002 struct blame_line *bline = &bca.lines[i];
3003 free(bline->id_str);
3004 free(bline->committer);
3006 free(bca.lines);
3008 free(bca.line_offsets);
3009 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3010 error = got_error_from_errno("fclose");
3011 return error;
3014 __dead static void
3015 usage_tree(void)
3017 fprintf(stderr,
3018 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3019 getprogname());
3020 exit(1);
3023 static void
3024 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3025 const char *root_path)
3027 int is_root_path = (strcmp(path, root_path) == 0);
3028 const char *modestr = "";
3029 mode_t mode = got_tree_entry_get_mode(te);
3031 path += strlen(root_path);
3032 while (path[0] == '/')
3033 path++;
3035 if (got_object_tree_entry_is_submodule(te))
3036 modestr = "$";
3037 else if (S_ISLNK(mode))
3038 modestr = "@";
3039 else if (S_ISDIR(mode))
3040 modestr = "/";
3041 else if (mode & S_IXUSR)
3042 modestr = "*";
3044 printf("%s%s%s%s%s\n", id ? id : "", path,
3045 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3048 static const struct got_error *
3049 print_tree(const char *path, struct got_object_id *commit_id,
3050 int show_ids, int recurse, const char *root_path,
3051 struct got_repository *repo)
3053 const struct got_error *err = NULL;
3054 struct got_object_id *tree_id = NULL;
3055 struct got_tree_object *tree = NULL;
3056 int nentries, i;
3058 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3059 if (err)
3060 goto done;
3062 err = got_object_open_as_tree(&tree, repo, tree_id);
3063 if (err)
3064 goto done;
3065 nentries = got_object_tree_get_nentries(tree);
3066 for (i = 0; i < nentries; i++) {
3067 struct got_tree_entry *te;
3068 char *id = NULL;
3070 if (sigint_received || sigpipe_received)
3071 break;
3073 te = got_object_tree_get_entry(tree, i);
3074 if (show_ids) {
3075 char *id_str;
3076 err = got_object_id_str(&id_str,
3077 got_tree_entry_get_id(te));
3078 if (err)
3079 goto done;
3080 if (asprintf(&id, "%s ", id_str) == -1) {
3081 err = got_error_from_errno("asprintf");
3082 free(id_str);
3083 goto done;
3085 free(id_str);
3087 print_entry(te, id, path, root_path);
3088 free(id);
3090 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3091 char *child_path;
3092 if (asprintf(&child_path, "%s%s%s", path,
3093 path[0] == '/' && path[1] == '\0' ? "" : "/",
3094 got_tree_entry_get_name(te)) == -1) {
3095 err = got_error_from_errno("asprintf");
3096 goto done;
3098 err = print_tree(child_path, commit_id, show_ids, 1,
3099 root_path, repo);
3100 free(child_path);
3101 if (err)
3102 goto done;
3105 done:
3106 if (tree)
3107 got_object_tree_close(tree);
3108 free(tree_id);
3109 return err;
3112 static const struct got_error *
3113 cmd_tree(int argc, char *argv[])
3115 const struct got_error *error;
3116 struct got_repository *repo = NULL;
3117 struct got_worktree *worktree = NULL;
3118 const char *path;
3119 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3120 struct got_object_id *commit_id = NULL;
3121 char *commit_id_str = NULL;
3122 int show_ids = 0, recurse = 0;
3123 int ch;
3125 #ifndef PROFILE
3126 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3127 NULL) == -1)
3128 err(1, "pledge");
3129 #endif
3131 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3132 switch (ch) {
3133 case 'c':
3134 commit_id_str = optarg;
3135 break;
3136 case 'r':
3137 repo_path = realpath(optarg, NULL);
3138 if (repo_path == NULL)
3139 return got_error_from_errno2("realpath",
3140 optarg);
3141 got_path_strip_trailing_slashes(repo_path);
3142 break;
3143 case 'i':
3144 show_ids = 1;
3145 break;
3146 case 'R':
3147 recurse = 1;
3148 break;
3149 default:
3150 usage_tree();
3151 /* NOTREACHED */
3155 argc -= optind;
3156 argv += optind;
3158 if (argc == 1)
3159 path = argv[0];
3160 else if (argc > 1)
3161 usage_tree();
3162 else
3163 path = NULL;
3165 cwd = getcwd(NULL, 0);
3166 if (cwd == NULL) {
3167 error = got_error_from_errno("getcwd");
3168 goto done;
3170 if (repo_path == NULL) {
3171 error = got_worktree_open(&worktree, cwd);
3172 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3173 goto done;
3174 else
3175 error = NULL;
3176 if (worktree) {
3177 repo_path =
3178 strdup(got_worktree_get_repo_path(worktree));
3179 if (repo_path == NULL)
3180 error = got_error_from_errno("strdup");
3181 if (error)
3182 goto done;
3183 } else {
3184 repo_path = strdup(cwd);
3185 if (repo_path == NULL) {
3186 error = got_error_from_errno("strdup");
3187 goto done;
3192 error = got_repo_open(&repo, repo_path, NULL);
3193 if (error != NULL)
3194 goto done;
3196 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3197 if (error)
3198 goto done;
3200 if (path == NULL) {
3201 if (worktree) {
3202 char *p, *worktree_subdir = cwd +
3203 strlen(got_worktree_get_root_path(worktree));
3204 if (asprintf(&p, "%s/%s",
3205 got_worktree_get_path_prefix(worktree),
3206 worktree_subdir) == -1) {
3207 error = got_error_from_errno("asprintf");
3208 goto done;
3210 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3211 free(p);
3212 if (error)
3213 goto done;
3214 } else
3215 path = "/";
3217 if (in_repo_path == NULL) {
3218 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3219 if (error != NULL)
3220 goto done;
3223 if (commit_id_str == NULL) {
3224 struct got_reference *head_ref;
3225 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3226 if (error != NULL)
3227 goto done;
3228 error = got_ref_resolve(&commit_id, repo, head_ref);
3229 got_ref_close(head_ref);
3230 if (error != NULL)
3231 goto done;
3232 } else {
3233 error = got_repo_match_object_id(&commit_id, NULL,
3234 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3235 if (error)
3236 goto done;
3239 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3240 in_repo_path, repo);
3241 done:
3242 free(in_repo_path);
3243 free(repo_path);
3244 free(cwd);
3245 free(commit_id);
3246 if (worktree)
3247 got_worktree_close(worktree);
3248 if (repo) {
3249 const struct got_error *repo_error;
3250 repo_error = got_repo_close(repo);
3251 if (error == NULL)
3252 error = repo_error;
3254 return error;
3257 __dead static void
3258 usage_status(void)
3260 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3261 exit(1);
3264 static const struct got_error *
3265 print_status(void *arg, unsigned char status, unsigned char staged_status,
3266 const char *path, struct got_object_id *blob_id,
3267 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3268 int dirfd, const char *de_name)
3270 if (status == staged_status && (status == GOT_STATUS_DELETE))
3271 status = GOT_STATUS_NO_CHANGE;
3272 printf("%c%c %s\n", status, staged_status, path);
3273 return NULL;
3276 static const struct got_error *
3277 cmd_status(int argc, char *argv[])
3279 const struct got_error *error = NULL;
3280 struct got_repository *repo = NULL;
3281 struct got_worktree *worktree = NULL;
3282 char *cwd = NULL;
3283 struct got_pathlist_head paths;
3284 struct got_pathlist_entry *pe;
3285 int ch;
3287 TAILQ_INIT(&paths);
3289 while ((ch = getopt(argc, argv, "")) != -1) {
3290 switch (ch) {
3291 default:
3292 usage_status();
3293 /* NOTREACHED */
3297 argc -= optind;
3298 argv += optind;
3300 #ifndef PROFILE
3301 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3302 NULL) == -1)
3303 err(1, "pledge");
3304 #endif
3305 cwd = getcwd(NULL, 0);
3306 if (cwd == NULL) {
3307 error = got_error_from_errno("getcwd");
3308 goto done;
3311 error = got_worktree_open(&worktree, cwd);
3312 if (error != NULL)
3313 goto done;
3315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3316 NULL);
3317 if (error != NULL)
3318 goto done;
3320 error = apply_unveil(got_repo_get_path(repo), 1,
3321 got_worktree_get_root_path(worktree));
3322 if (error)
3323 goto done;
3325 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3326 if (error)
3327 goto done;
3329 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3330 check_cancelled, NULL);
3331 done:
3332 TAILQ_FOREACH(pe, &paths, entry)
3333 free((char *)pe->path);
3334 got_pathlist_free(&paths);
3335 free(cwd);
3336 return error;
3339 __dead static void
3340 usage_ref(void)
3342 fprintf(stderr,
3343 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3344 getprogname());
3345 exit(1);
3348 static const struct got_error *
3349 list_refs(struct got_repository *repo)
3351 static const struct got_error *err = NULL;
3352 struct got_reflist_head refs;
3353 struct got_reflist_entry *re;
3355 SIMPLEQ_INIT(&refs);
3356 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3357 if (err)
3358 return err;
3360 SIMPLEQ_FOREACH(re, &refs, entry) {
3361 char *refstr;
3362 refstr = got_ref_to_str(re->ref);
3363 if (refstr == NULL)
3364 return got_error_from_errno("got_ref_to_str");
3365 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3366 free(refstr);
3369 got_ref_list_free(&refs);
3370 return NULL;
3373 static const struct got_error *
3374 delete_ref(struct got_repository *repo, const char *refname)
3376 const struct got_error *err = NULL;
3377 struct got_reference *ref;
3379 err = got_ref_open(&ref, repo, refname, 0);
3380 if (err)
3381 return err;
3383 err = got_ref_delete(ref, repo);
3384 got_ref_close(ref);
3385 return err;
3388 static const struct got_error *
3389 add_ref(struct got_repository *repo, const char *refname, const char *target)
3391 const struct got_error *err = NULL;
3392 struct got_object_id *id;
3393 struct got_reference *ref = NULL;
3396 * Don't let the user create a reference name with a leading '-'.
3397 * While technically a valid reference name, this case is usually
3398 * an unintended typo.
3400 if (refname[0] == '-')
3401 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3403 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3404 repo);
3405 if (err) {
3406 struct got_reference *target_ref;
3408 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3409 return err;
3410 err = got_ref_open(&target_ref, repo, target, 0);
3411 if (err)
3412 return err;
3413 err = got_ref_resolve(&id, repo, target_ref);
3414 got_ref_close(target_ref);
3415 if (err)
3416 return err;
3419 err = got_ref_alloc(&ref, refname, id);
3420 if (err)
3421 goto done;
3423 err = got_ref_write(ref, repo);
3424 done:
3425 if (ref)
3426 got_ref_close(ref);
3427 free(id);
3428 return err;
3431 static const struct got_error *
3432 add_symref(struct got_repository *repo, const char *refname, const char *target)
3434 const struct got_error *err = NULL;
3435 struct got_reference *ref = NULL;
3436 struct got_reference *target_ref = NULL;
3439 * Don't let the user create a reference name with a leading '-'.
3440 * While technically a valid reference name, this case is usually
3441 * an unintended typo.
3443 if (refname[0] == '-')
3444 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3446 err = got_ref_open(&target_ref, repo, target, 0);
3447 if (err)
3448 return err;
3450 err = got_ref_alloc_symref(&ref, refname, target_ref);
3451 if (err)
3452 goto done;
3454 err = got_ref_write(ref, repo);
3455 done:
3456 if (target_ref)
3457 got_ref_close(target_ref);
3458 if (ref)
3459 got_ref_close(ref);
3460 return err;
3463 static const struct got_error *
3464 cmd_ref(int argc, char *argv[])
3466 const struct got_error *error = NULL;
3467 struct got_repository *repo = NULL;
3468 struct got_worktree *worktree = NULL;
3469 char *cwd = NULL, *repo_path = NULL;
3470 int ch, do_list = 0, create_symref = 0;
3471 const char *delref = NULL;
3473 /* TODO: Add -s option for adding symbolic references. */
3474 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3475 switch (ch) {
3476 case 'd':
3477 delref = optarg;
3478 break;
3479 case 'r':
3480 repo_path = realpath(optarg, NULL);
3481 if (repo_path == NULL)
3482 return got_error_from_errno2("realpath",
3483 optarg);
3484 got_path_strip_trailing_slashes(repo_path);
3485 break;
3486 case 'l':
3487 do_list = 1;
3488 break;
3489 case 's':
3490 create_symref = 1;
3491 break;
3492 default:
3493 usage_ref();
3494 /* NOTREACHED */
3498 if (do_list && delref)
3499 errx(1, "-l and -d options are mutually exclusive\n");
3501 argc -= optind;
3502 argv += optind;
3504 if (do_list || delref) {
3505 if (create_symref)
3506 errx(1, "-s option cannot be used together with the "
3507 "-l or -d options");
3508 if (argc > 0)
3509 usage_ref();
3510 } else if (argc != 2)
3511 usage_ref();
3513 #ifndef PROFILE
3514 if (do_list) {
3515 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3516 NULL) == -1)
3517 err(1, "pledge");
3518 } else {
3519 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3520 "sendfd unveil", NULL) == -1)
3521 err(1, "pledge");
3523 #endif
3524 cwd = getcwd(NULL, 0);
3525 if (cwd == NULL) {
3526 error = got_error_from_errno("getcwd");
3527 goto done;
3530 if (repo_path == NULL) {
3531 error = got_worktree_open(&worktree, cwd);
3532 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3533 goto done;
3534 else
3535 error = NULL;
3536 if (worktree) {
3537 repo_path =
3538 strdup(got_worktree_get_repo_path(worktree));
3539 if (repo_path == NULL)
3540 error = got_error_from_errno("strdup");
3541 if (error)
3542 goto done;
3543 } else {
3544 repo_path = strdup(cwd);
3545 if (repo_path == NULL) {
3546 error = got_error_from_errno("strdup");
3547 goto done;
3552 error = got_repo_open(&repo, repo_path, NULL);
3553 if (error != NULL)
3554 goto done;
3556 error = apply_unveil(got_repo_get_path(repo), do_list,
3557 worktree ? got_worktree_get_root_path(worktree) : NULL);
3558 if (error)
3559 goto done;
3561 if (do_list)
3562 error = list_refs(repo);
3563 else if (delref)
3564 error = delete_ref(repo, delref);
3565 else if (create_symref)
3566 error = add_symref(repo, argv[0], argv[1]);
3567 else
3568 error = add_ref(repo, argv[0], argv[1]);
3569 done:
3570 if (repo)
3571 got_repo_close(repo);
3572 if (worktree)
3573 got_worktree_close(worktree);
3574 free(cwd);
3575 free(repo_path);
3576 return error;
3579 __dead static void
3580 usage_branch(void)
3582 fprintf(stderr,
3583 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3584 "[name]\n", getprogname());
3585 exit(1);
3588 static const struct got_error *
3589 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3590 struct got_reference *ref)
3592 const struct got_error *err = NULL;
3593 const char *refname, *marker = " ";
3594 char *refstr;
3596 refname = got_ref_get_name(ref);
3597 if (worktree && strcmp(refname,
3598 got_worktree_get_head_ref_name(worktree)) == 0) {
3599 struct got_object_id *id = NULL;
3601 err = got_ref_resolve(&id, repo, ref);
3602 if (err)
3603 return err;
3604 if (got_object_id_cmp(id,
3605 got_worktree_get_base_commit_id(worktree)) == 0)
3606 marker = "* ";
3607 else
3608 marker = "~ ";
3609 free(id);
3612 if (strncmp(refname, "refs/heads/", 11) == 0)
3613 refname += 11;
3614 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3615 refname += 18;
3617 refstr = got_ref_to_str(ref);
3618 if (refstr == NULL)
3619 return got_error_from_errno("got_ref_to_str");
3621 printf("%s%s: %s\n", marker, refname, refstr);
3622 free(refstr);
3623 return NULL;
3626 static const struct got_error *
3627 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3629 const char *refname;
3631 if (worktree == NULL)
3632 return got_error(GOT_ERR_NOT_WORKTREE);
3634 refname = got_worktree_get_head_ref_name(worktree);
3636 if (strncmp(refname, "refs/heads/", 11) == 0)
3637 refname += 11;
3638 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3639 refname += 18;
3641 printf("%s\n", refname);
3643 return NULL;
3646 static const struct got_error *
3647 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3649 static const struct got_error *err = NULL;
3650 struct got_reflist_head refs;
3651 struct got_reflist_entry *re;
3652 struct got_reference *temp_ref = NULL;
3653 int rebase_in_progress, histedit_in_progress;
3655 SIMPLEQ_INIT(&refs);
3657 if (worktree) {
3658 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3659 worktree);
3660 if (err)
3661 return err;
3663 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3664 worktree);
3665 if (err)
3666 return err;
3668 if (rebase_in_progress || histedit_in_progress) {
3669 err = got_ref_open(&temp_ref, repo,
3670 got_worktree_get_head_ref_name(worktree), 0);
3671 if (err)
3672 return err;
3673 list_branch(repo, worktree, temp_ref);
3674 got_ref_close(temp_ref);
3678 err = got_ref_list(&refs, repo, "refs/heads",
3679 got_ref_cmp_by_name, NULL);
3680 if (err)
3681 return err;
3683 SIMPLEQ_FOREACH(re, &refs, entry)
3684 list_branch(repo, worktree, re->ref);
3686 got_ref_list_free(&refs);
3687 return NULL;
3690 static const struct got_error *
3691 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3692 const char *branch_name)
3694 const struct got_error *err = NULL;
3695 struct got_reference *ref = NULL;
3696 char *refname;
3698 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3699 return got_error_from_errno("asprintf");
3701 err = got_ref_open(&ref, repo, refname, 0);
3702 if (err)
3703 goto done;
3705 if (worktree &&
3706 strcmp(got_worktree_get_head_ref_name(worktree),
3707 got_ref_get_name(ref)) == 0) {
3708 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3709 "will not delete this work tree's current branch");
3710 goto done;
3713 err = got_ref_delete(ref, repo);
3714 done:
3715 if (ref)
3716 got_ref_close(ref);
3717 free(refname);
3718 return err;
3721 static const struct got_error *
3722 add_branch(struct got_repository *repo, const char *branch_name,
3723 struct got_object_id *base_commit_id)
3725 const struct got_error *err = NULL;
3726 struct got_reference *ref = NULL;
3727 char *base_refname = NULL, *refname = NULL;
3730 * Don't let the user create a branch name with a leading '-'.
3731 * While technically a valid reference name, this case is usually
3732 * an unintended typo.
3734 if (branch_name[0] == '-')
3735 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3737 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3738 err = got_error_from_errno("asprintf");
3739 goto done;
3742 err = got_ref_open(&ref, repo, refname, 0);
3743 if (err == NULL) {
3744 err = got_error(GOT_ERR_BRANCH_EXISTS);
3745 goto done;
3746 } else if (err->code != GOT_ERR_NOT_REF)
3747 goto done;
3749 err = got_ref_alloc(&ref, refname, base_commit_id);
3750 if (err)
3751 goto done;
3753 err = got_ref_write(ref, repo);
3754 done:
3755 if (ref)
3756 got_ref_close(ref);
3757 free(base_refname);
3758 free(refname);
3759 return err;
3762 static const struct got_error *
3763 cmd_branch(int argc, char *argv[])
3765 const struct got_error *error = NULL;
3766 struct got_repository *repo = NULL;
3767 struct got_worktree *worktree = NULL;
3768 char *cwd = NULL, *repo_path = NULL;
3769 int ch, do_list = 0, do_show = 0, do_update = 1;
3770 const char *delref = NULL, *commit_id_arg = NULL;
3771 struct got_reference *ref = NULL;
3772 struct got_pathlist_head paths;
3773 struct got_pathlist_entry *pe;
3774 struct got_object_id *commit_id = NULL;
3775 char *commit_id_str = NULL;
3777 TAILQ_INIT(&paths);
3779 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3780 switch (ch) {
3781 case 'c':
3782 commit_id_arg = optarg;
3783 break;
3784 case 'd':
3785 delref = optarg;
3786 break;
3787 case 'r':
3788 repo_path = realpath(optarg, NULL);
3789 if (repo_path == NULL)
3790 return got_error_from_errno2("realpath",
3791 optarg);
3792 got_path_strip_trailing_slashes(repo_path);
3793 break;
3794 case 'l':
3795 do_list = 1;
3796 break;
3797 case 'n':
3798 do_update = 0;
3799 break;
3800 default:
3801 usage_branch();
3802 /* NOTREACHED */
3806 if (do_list && delref)
3807 errx(1, "-l and -d options are mutually exclusive\n");
3809 argc -= optind;
3810 argv += optind;
3812 if (!do_list && !delref && argc == 0)
3813 do_show = 1;
3815 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3816 errx(1, "-c option can only be used when creating a branch");
3818 if (do_list || delref) {
3819 if (argc > 0)
3820 usage_branch();
3821 } else if (!do_show && argc != 1)
3822 usage_branch();
3824 #ifndef PROFILE
3825 if (do_list || do_show) {
3826 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3827 NULL) == -1)
3828 err(1, "pledge");
3829 } else {
3830 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3831 "sendfd unveil", NULL) == -1)
3832 err(1, "pledge");
3834 #endif
3835 cwd = getcwd(NULL, 0);
3836 if (cwd == NULL) {
3837 error = got_error_from_errno("getcwd");
3838 goto done;
3841 if (repo_path == NULL) {
3842 error = got_worktree_open(&worktree, cwd);
3843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3844 goto done;
3845 else
3846 error = NULL;
3847 if (worktree) {
3848 repo_path =
3849 strdup(got_worktree_get_repo_path(worktree));
3850 if (repo_path == NULL)
3851 error = got_error_from_errno("strdup");
3852 if (error)
3853 goto done;
3854 } else {
3855 repo_path = strdup(cwd);
3856 if (repo_path == NULL) {
3857 error = got_error_from_errno("strdup");
3858 goto done;
3863 error = got_repo_open(&repo, repo_path, NULL);
3864 if (error != NULL)
3865 goto done;
3867 error = apply_unveil(got_repo_get_path(repo), do_list,
3868 worktree ? got_worktree_get_root_path(worktree) : NULL);
3869 if (error)
3870 goto done;
3872 if (do_show)
3873 error = show_current_branch(repo, worktree);
3874 else if (do_list)
3875 error = list_branches(repo, worktree);
3876 else if (delref)
3877 error = delete_branch(repo, worktree, delref);
3878 else {
3879 if (commit_id_arg == NULL)
3880 commit_id_arg = worktree ?
3881 got_worktree_get_head_ref_name(worktree) :
3882 GOT_REF_HEAD;
3883 error = got_repo_match_object_id(&commit_id, NULL,
3884 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3885 if (error)
3886 goto done;
3887 error = add_branch(repo, argv[0], commit_id);
3888 if (error)
3889 goto done;
3890 if (worktree && do_update) {
3891 int did_something = 0;
3892 char *branch_refname = NULL;
3894 error = got_object_id_str(&commit_id_str, commit_id);
3895 if (error)
3896 goto done;
3897 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3898 worktree);
3899 if (error)
3900 goto done;
3901 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3902 == -1) {
3903 error = got_error_from_errno("asprintf");
3904 goto done;
3906 error = got_ref_open(&ref, repo, branch_refname, 0);
3907 free(branch_refname);
3908 if (error)
3909 goto done;
3910 error = switch_head_ref(ref, commit_id, worktree,
3911 repo);
3912 if (error)
3913 goto done;
3914 error = got_worktree_set_base_commit_id(worktree, repo,
3915 commit_id);
3916 if (error)
3917 goto done;
3918 error = got_worktree_checkout_files(worktree, &paths,
3919 repo, update_progress, &did_something,
3920 check_cancelled, NULL);
3921 if (error)
3922 goto done;
3923 if (did_something)
3924 printf("Updated to commit %s\n", commit_id_str);
3927 done:
3928 if (ref)
3929 got_ref_close(ref);
3930 if (repo)
3931 got_repo_close(repo);
3932 if (worktree)
3933 got_worktree_close(worktree);
3934 free(cwd);
3935 free(repo_path);
3936 free(commit_id);
3937 free(commit_id_str);
3938 TAILQ_FOREACH(pe, &paths, entry)
3939 free((char *)pe->path);
3940 got_pathlist_free(&paths);
3941 return error;
3945 __dead static void
3946 usage_tag(void)
3948 fprintf(stderr,
3949 "usage: %s tag [-c commit] [-r repository] [-l] "
3950 "[-m message] name\n", getprogname());
3951 exit(1);
3954 #if 0
3955 static const struct got_error *
3956 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3958 const struct got_error *err = NULL;
3959 struct got_reflist_entry *re, *se, *new;
3960 struct got_object_id *re_id, *se_id;
3961 struct got_tag_object *re_tag, *se_tag;
3962 time_t re_time, se_time;
3964 SIMPLEQ_FOREACH(re, tags, entry) {
3965 se = SIMPLEQ_FIRST(sorted);
3966 if (se == NULL) {
3967 err = got_reflist_entry_dup(&new, re);
3968 if (err)
3969 return err;
3970 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3971 continue;
3972 } else {
3973 err = got_ref_resolve(&re_id, repo, re->ref);
3974 if (err)
3975 break;
3976 err = got_object_open_as_tag(&re_tag, repo, re_id);
3977 free(re_id);
3978 if (err)
3979 break;
3980 re_time = got_object_tag_get_tagger_time(re_tag);
3981 got_object_tag_close(re_tag);
3984 while (se) {
3985 err = got_ref_resolve(&se_id, repo, re->ref);
3986 if (err)
3987 break;
3988 err = got_object_open_as_tag(&se_tag, repo, se_id);
3989 free(se_id);
3990 if (err)
3991 break;
3992 se_time = got_object_tag_get_tagger_time(se_tag);
3993 got_object_tag_close(se_tag);
3995 if (se_time > re_time) {
3996 err = got_reflist_entry_dup(&new, re);
3997 if (err)
3998 return err;
3999 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4000 break;
4002 se = SIMPLEQ_NEXT(se, entry);
4003 continue;
4006 done:
4007 return err;
4009 #endif
4011 static const struct got_error *
4012 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4014 static const struct got_error *err = NULL;
4015 struct got_reflist_head refs;
4016 struct got_reflist_entry *re;
4018 SIMPLEQ_INIT(&refs);
4020 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4021 if (err)
4022 return err;
4024 SIMPLEQ_FOREACH(re, &refs, entry) {
4025 const char *refname;
4026 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4027 char datebuf[26];
4028 const char *tagger;
4029 time_t tagger_time;
4030 struct got_object_id *id;
4031 struct got_tag_object *tag;
4032 struct got_commit_object *commit = NULL;
4034 refname = got_ref_get_name(re->ref);
4035 if (strncmp(refname, "refs/tags/", 10) != 0)
4036 continue;
4037 refname += 10;
4038 refstr = got_ref_to_str(re->ref);
4039 if (refstr == NULL) {
4040 err = got_error_from_errno("got_ref_to_str");
4041 break;
4043 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4044 free(refstr);
4046 err = got_ref_resolve(&id, repo, re->ref);
4047 if (err)
4048 break;
4049 err = got_object_open_as_tag(&tag, repo, id);
4050 if (err) {
4051 if (err->code != GOT_ERR_OBJ_TYPE) {
4052 free(id);
4053 break;
4055 /* "lightweight" tag */
4056 err = got_object_open_as_commit(&commit, repo, id);
4057 if (err) {
4058 free(id);
4059 break;
4061 tagger = got_object_commit_get_committer(commit);
4062 tagger_time =
4063 got_object_commit_get_committer_time(commit);
4064 err = got_object_id_str(&id_str, id);
4065 free(id);
4066 if (err)
4067 break;
4068 } else {
4069 free(id);
4070 tagger = got_object_tag_get_tagger(tag);
4071 tagger_time = got_object_tag_get_tagger_time(tag);
4072 err = got_object_id_str(&id_str,
4073 got_object_tag_get_object_id(tag));
4074 if (err)
4075 break;
4077 printf("from: %s\n", tagger);
4078 datestr = get_datestr(&tagger_time, datebuf);
4079 if (datestr)
4080 printf("date: %s UTC\n", datestr);
4081 if (commit)
4082 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4083 else {
4084 switch (got_object_tag_get_object_type(tag)) {
4085 case GOT_OBJ_TYPE_BLOB:
4086 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4087 id_str);
4088 break;
4089 case GOT_OBJ_TYPE_TREE:
4090 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4091 id_str);
4092 break;
4093 case GOT_OBJ_TYPE_COMMIT:
4094 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4095 id_str);
4096 break;
4097 case GOT_OBJ_TYPE_TAG:
4098 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4099 id_str);
4100 break;
4101 default:
4102 break;
4105 free(id_str);
4106 if (commit) {
4107 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4108 if (err)
4109 break;
4110 got_object_commit_close(commit);
4111 } else {
4112 tagmsg0 = strdup(got_object_tag_get_message(tag));
4113 got_object_tag_close(tag);
4114 if (tagmsg0 == NULL) {
4115 err = got_error_from_errno("strdup");
4116 break;
4120 tagmsg = tagmsg0;
4121 do {
4122 line = strsep(&tagmsg, "\n");
4123 if (line)
4124 printf(" %s\n", line);
4125 } while (line);
4126 free(tagmsg0);
4129 got_ref_list_free(&refs);
4130 return NULL;
4133 static const struct got_error *
4134 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4135 const char *tag_name, const char *repo_path)
4137 const struct got_error *err = NULL;
4138 char *template = NULL, *initial_content = NULL;
4139 char *editor = NULL;
4140 int fd = -1;
4142 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4143 err = got_error_from_errno("asprintf");
4144 goto done;
4147 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4148 commit_id_str, tag_name) == -1) {
4149 err = got_error_from_errno("asprintf");
4150 goto done;
4153 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4154 if (err)
4155 goto done;
4157 dprintf(fd, initial_content);
4158 close(fd);
4160 err = get_editor(&editor);
4161 if (err)
4162 goto done;
4163 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4164 done:
4165 free(initial_content);
4166 free(template);
4167 free(editor);
4169 /* Editor is done; we can now apply unveil(2) */
4170 if (err == NULL) {
4171 err = apply_unveil(repo_path, 0, NULL);
4172 if (err) {
4173 free(*tagmsg);
4174 *tagmsg = NULL;
4177 return err;
4180 static const struct got_error *
4181 add_tag(struct got_repository *repo, const char *tag_name,
4182 const char *commit_arg, const char *tagmsg_arg)
4184 const struct got_error *err = NULL;
4185 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4186 char *label = NULL, *commit_id_str = NULL;
4187 struct got_reference *ref = NULL;
4188 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4189 char *tagmsg_path = NULL, *tag_id_str = NULL;
4190 int preserve_tagmsg = 0;
4193 * Don't let the user create a tag name with a leading '-'.
4194 * While technically a valid reference name, this case is usually
4195 * an unintended typo.
4197 if (tag_name[0] == '-')
4198 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4200 err = get_author(&tagger, repo);
4201 if (err)
4202 return err;
4204 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4205 GOT_OBJ_TYPE_COMMIT, 1, repo);
4206 if (err)
4207 goto done;
4209 err = got_object_id_str(&commit_id_str, commit_id);
4210 if (err)
4211 goto done;
4213 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4214 refname = strdup(tag_name);
4215 if (refname == NULL) {
4216 err = got_error_from_errno("strdup");
4217 goto done;
4219 tag_name += 10;
4220 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4221 err = got_error_from_errno("asprintf");
4222 goto done;
4225 err = got_ref_open(&ref, repo, refname, 0);
4226 if (err == NULL) {
4227 err = got_error(GOT_ERR_TAG_EXISTS);
4228 goto done;
4229 } else if (err->code != GOT_ERR_NOT_REF)
4230 goto done;
4232 if (tagmsg_arg == NULL) {
4233 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4234 tag_name, got_repo_get_path(repo));
4235 if (err) {
4236 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4237 tagmsg_path != NULL)
4238 preserve_tagmsg = 1;
4239 goto done;
4243 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4244 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4245 if (err) {
4246 if (tagmsg_path)
4247 preserve_tagmsg = 1;
4248 goto done;
4251 err = got_ref_alloc(&ref, refname, tag_id);
4252 if (err) {
4253 if (tagmsg_path)
4254 preserve_tagmsg = 1;
4255 goto done;
4258 err = got_ref_write(ref, repo);
4259 if (err) {
4260 if (tagmsg_path)
4261 preserve_tagmsg = 1;
4262 goto done;
4265 err = got_object_id_str(&tag_id_str, tag_id);
4266 if (err) {
4267 if (tagmsg_path)
4268 preserve_tagmsg = 1;
4269 goto done;
4271 printf("Created tag %s\n", tag_id_str);
4272 done:
4273 if (preserve_tagmsg) {
4274 fprintf(stderr, "%s: tag message preserved in %s\n",
4275 getprogname(), tagmsg_path);
4276 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4277 err = got_error_from_errno2("unlink", tagmsg_path);
4278 free(tag_id_str);
4279 if (ref)
4280 got_ref_close(ref);
4281 free(commit_id);
4282 free(commit_id_str);
4283 free(refname);
4284 free(tagmsg);
4285 free(tagmsg_path);
4286 free(tagger);
4287 return err;
4290 static const struct got_error *
4291 cmd_tag(int argc, char *argv[])
4293 const struct got_error *error = NULL;
4294 struct got_repository *repo = NULL;
4295 struct got_worktree *worktree = NULL;
4296 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4297 char *gitconfig_path = NULL;
4298 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4299 int ch, do_list = 0;
4301 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4302 switch (ch) {
4303 case 'c':
4304 commit_id_arg = optarg;
4305 break;
4306 case 'm':
4307 tagmsg = optarg;
4308 break;
4309 case 'r':
4310 repo_path = realpath(optarg, NULL);
4311 if (repo_path == NULL)
4312 return got_error_from_errno2("realpath",
4313 optarg);
4314 got_path_strip_trailing_slashes(repo_path);
4315 break;
4316 case 'l':
4317 do_list = 1;
4318 break;
4319 default:
4320 usage_tag();
4321 /* NOTREACHED */
4325 argc -= optind;
4326 argv += optind;
4328 if (do_list) {
4329 if (commit_id_arg != NULL)
4330 errx(1, "-c option can only be used when creating a tag");
4331 if (tagmsg)
4332 errx(1, "-l and -m options are mutually exclusive");
4333 if (argc > 0)
4334 usage_tag();
4335 } else if (argc != 1)
4336 usage_tag();
4338 tag_name = argv[0];
4340 #ifndef PROFILE
4341 if (do_list) {
4342 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4343 NULL) == -1)
4344 err(1, "pledge");
4345 } else {
4346 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4347 "sendfd unveil", NULL) == -1)
4348 err(1, "pledge");
4350 #endif
4351 cwd = getcwd(NULL, 0);
4352 if (cwd == NULL) {
4353 error = got_error_from_errno("getcwd");
4354 goto done;
4357 if (repo_path == NULL) {
4358 error = got_worktree_open(&worktree, cwd);
4359 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4360 goto done;
4361 else
4362 error = NULL;
4363 if (worktree) {
4364 repo_path =
4365 strdup(got_worktree_get_repo_path(worktree));
4366 if (repo_path == NULL)
4367 error = got_error_from_errno("strdup");
4368 if (error)
4369 goto done;
4370 } else {
4371 repo_path = strdup(cwd);
4372 if (repo_path == NULL) {
4373 error = got_error_from_errno("strdup");
4374 goto done;
4379 if (do_list) {
4380 error = got_repo_open(&repo, repo_path, NULL);
4381 if (error != NULL)
4382 goto done;
4383 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4384 if (error)
4385 goto done;
4386 error = list_tags(repo, worktree);
4387 } else {
4388 error = get_gitconfig_path(&gitconfig_path);
4389 if (error)
4390 goto done;
4391 error = got_repo_open(&repo, repo_path, gitconfig_path);
4392 if (error != NULL)
4393 goto done;
4395 if (tagmsg) {
4396 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4397 if (error)
4398 goto done;
4401 if (commit_id_arg == NULL) {
4402 struct got_reference *head_ref;
4403 struct got_object_id *commit_id;
4404 error = got_ref_open(&head_ref, repo,
4405 worktree ? got_worktree_get_head_ref_name(worktree)
4406 : GOT_REF_HEAD, 0);
4407 if (error)
4408 goto done;
4409 error = got_ref_resolve(&commit_id, repo, head_ref);
4410 got_ref_close(head_ref);
4411 if (error)
4412 goto done;
4413 error = got_object_id_str(&commit_id_str, commit_id);
4414 free(commit_id);
4415 if (error)
4416 goto done;
4419 error = add_tag(repo, tag_name,
4420 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4422 done:
4423 if (repo)
4424 got_repo_close(repo);
4425 if (worktree)
4426 got_worktree_close(worktree);
4427 free(cwd);
4428 free(repo_path);
4429 free(gitconfig_path);
4430 free(commit_id_str);
4431 return error;
4434 __dead static void
4435 usage_add(void)
4437 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4438 getprogname());
4439 exit(1);
4442 static const struct got_error *
4443 add_progress(void *arg, unsigned char status, const char *path)
4445 while (path[0] == '/')
4446 path++;
4447 printf("%c %s\n", status, path);
4448 return NULL;
4451 static const struct got_error *
4452 cmd_add(int argc, char *argv[])
4454 const struct got_error *error = NULL;
4455 struct got_repository *repo = NULL;
4456 struct got_worktree *worktree = NULL;
4457 char *cwd = NULL;
4458 struct got_pathlist_head paths;
4459 struct got_pathlist_entry *pe;
4460 int ch, can_recurse = 0, no_ignores = 0;
4462 TAILQ_INIT(&paths);
4464 while ((ch = getopt(argc, argv, "IR")) != -1) {
4465 switch (ch) {
4466 case 'I':
4467 no_ignores = 1;
4468 break;
4469 case 'R':
4470 can_recurse = 1;
4471 break;
4472 default:
4473 usage_add();
4474 /* NOTREACHED */
4478 argc -= optind;
4479 argv += optind;
4481 #ifndef PROFILE
4482 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4483 NULL) == -1)
4484 err(1, "pledge");
4485 #endif
4486 if (argc < 1)
4487 usage_add();
4489 cwd = getcwd(NULL, 0);
4490 if (cwd == NULL) {
4491 error = got_error_from_errno("getcwd");
4492 goto done;
4495 error = got_worktree_open(&worktree, cwd);
4496 if (error)
4497 goto done;
4499 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4500 NULL);
4501 if (error != NULL)
4502 goto done;
4504 error = apply_unveil(got_repo_get_path(repo), 1,
4505 got_worktree_get_root_path(worktree));
4506 if (error)
4507 goto done;
4509 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4510 if (error)
4511 goto done;
4513 if (!can_recurse && no_ignores) {
4514 error = got_error_msg(GOT_ERR_BAD_PATH,
4515 "disregarding ignores requires -R option");
4516 goto done;
4520 if (!can_recurse) {
4521 char *ondisk_path;
4522 struct stat sb;
4523 TAILQ_FOREACH(pe, &paths, entry) {
4524 if (asprintf(&ondisk_path, "%s/%s",
4525 got_worktree_get_root_path(worktree),
4526 pe->path) == -1) {
4527 error = got_error_from_errno("asprintf");
4528 goto done;
4530 if (lstat(ondisk_path, &sb) == -1) {
4531 if (errno == ENOENT) {
4532 free(ondisk_path);
4533 continue;
4535 error = got_error_from_errno2("lstat",
4536 ondisk_path);
4537 free(ondisk_path);
4538 goto done;
4540 free(ondisk_path);
4541 if (S_ISDIR(sb.st_mode)) {
4542 error = got_error_msg(GOT_ERR_BAD_PATH,
4543 "adding directories requires -R option");
4544 goto done;
4549 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4550 NULL, repo, no_ignores);
4551 done:
4552 if (repo)
4553 got_repo_close(repo);
4554 if (worktree)
4555 got_worktree_close(worktree);
4556 TAILQ_FOREACH(pe, &paths, entry)
4557 free((char *)pe->path);
4558 got_pathlist_free(&paths);
4559 free(cwd);
4560 return error;
4563 __dead static void
4564 usage_remove(void)
4566 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4567 getprogname());
4568 exit(1);
4571 static const struct got_error *
4572 print_remove_status(void *arg, unsigned char status,
4573 unsigned char staged_status, const char *path)
4575 while (path[0] == '/')
4576 path++;
4577 if (status == GOT_STATUS_NONEXISTENT)
4578 return NULL;
4579 if (status == staged_status && (status == GOT_STATUS_DELETE))
4580 status = GOT_STATUS_NO_CHANGE;
4581 printf("%c%c %s\n", status, staged_status, path);
4582 return NULL;
4585 static const struct got_error *
4586 cmd_remove(int argc, char *argv[])
4588 const struct got_error *error = NULL;
4589 struct got_worktree *worktree = NULL;
4590 struct got_repository *repo = NULL;
4591 char *cwd = NULL;
4592 struct got_pathlist_head paths;
4593 struct got_pathlist_entry *pe;
4594 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4596 TAILQ_INIT(&paths);
4598 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4599 switch (ch) {
4600 case 'f':
4601 delete_local_mods = 1;
4602 break;
4603 case 'k':
4604 keep_on_disk = 1;
4605 break;
4606 case 'R':
4607 can_recurse = 1;
4608 break;
4609 default:
4610 usage_remove();
4611 /* NOTREACHED */
4615 argc -= optind;
4616 argv += optind;
4618 #ifndef PROFILE
4619 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4620 NULL) == -1)
4621 err(1, "pledge");
4622 #endif
4623 if (argc < 1)
4624 usage_remove();
4626 cwd = getcwd(NULL, 0);
4627 if (cwd == NULL) {
4628 error = got_error_from_errno("getcwd");
4629 goto done;
4631 error = got_worktree_open(&worktree, cwd);
4632 if (error)
4633 goto done;
4635 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4636 NULL);
4637 if (error)
4638 goto done;
4640 error = apply_unveil(got_repo_get_path(repo), 1,
4641 got_worktree_get_root_path(worktree));
4642 if (error)
4643 goto done;
4645 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4646 if (error)
4647 goto done;
4649 if (!can_recurse) {
4650 char *ondisk_path;
4651 struct stat sb;
4652 TAILQ_FOREACH(pe, &paths, entry) {
4653 if (asprintf(&ondisk_path, "%s/%s",
4654 got_worktree_get_root_path(worktree),
4655 pe->path) == -1) {
4656 error = got_error_from_errno("asprintf");
4657 goto done;
4659 if (lstat(ondisk_path, &sb) == -1) {
4660 if (errno == ENOENT) {
4661 free(ondisk_path);
4662 continue;
4664 error = got_error_from_errno2("lstat",
4665 ondisk_path);
4666 free(ondisk_path);
4667 goto done;
4669 free(ondisk_path);
4670 if (S_ISDIR(sb.st_mode)) {
4671 error = got_error_msg(GOT_ERR_BAD_PATH,
4672 "removing directories requires -R option");
4673 goto done;
4678 error = got_worktree_schedule_delete(worktree, &paths,
4679 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4680 done:
4681 if (repo)
4682 got_repo_close(repo);
4683 if (worktree)
4684 got_worktree_close(worktree);
4685 TAILQ_FOREACH(pe, &paths, entry)
4686 free((char *)pe->path);
4687 got_pathlist_free(&paths);
4688 free(cwd);
4689 return error;
4692 __dead static void
4693 usage_revert(void)
4695 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4696 "path ...\n", getprogname());
4697 exit(1);
4700 static const struct got_error *
4701 revert_progress(void *arg, unsigned char status, const char *path)
4703 if (status == GOT_STATUS_UNVERSIONED)
4704 return NULL;
4706 while (path[0] == '/')
4707 path++;
4708 printf("%c %s\n", status, path);
4709 return NULL;
4712 struct choose_patch_arg {
4713 FILE *patch_script_file;
4714 const char *action;
4717 static const struct got_error *
4718 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4719 int nchanges, const char *action)
4721 char *line = NULL;
4722 size_t linesize = 0;
4723 ssize_t linelen;
4725 switch (status) {
4726 case GOT_STATUS_ADD:
4727 printf("A %s\n%s this addition? [y/n] ", path, action);
4728 break;
4729 case GOT_STATUS_DELETE:
4730 printf("D %s\n%s this deletion? [y/n] ", path, action);
4731 break;
4732 case GOT_STATUS_MODIFY:
4733 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4734 return got_error_from_errno("fseek");
4735 printf(GOT_COMMIT_SEP_STR);
4736 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4737 printf("%s", line);
4738 if (ferror(patch_file))
4739 return got_error_from_errno("getline");
4740 printf(GOT_COMMIT_SEP_STR);
4741 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4742 path, n, nchanges, action);
4743 break;
4744 default:
4745 return got_error_path(path, GOT_ERR_FILE_STATUS);
4748 return NULL;
4751 static const struct got_error *
4752 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4753 FILE *patch_file, int n, int nchanges)
4755 const struct got_error *err = NULL;
4756 char *line = NULL;
4757 size_t linesize = 0;
4758 ssize_t linelen;
4759 int resp = ' ';
4760 struct choose_patch_arg *a = arg;
4762 *choice = GOT_PATCH_CHOICE_NONE;
4764 if (a->patch_script_file) {
4765 char *nl;
4766 err = show_change(status, path, patch_file, n, nchanges,
4767 a->action);
4768 if (err)
4769 return err;
4770 linelen = getline(&line, &linesize, a->patch_script_file);
4771 if (linelen == -1) {
4772 if (ferror(a->patch_script_file))
4773 return got_error_from_errno("getline");
4774 return NULL;
4776 nl = strchr(line, '\n');
4777 if (nl)
4778 *nl = '\0';
4779 if (strcmp(line, "y") == 0) {
4780 *choice = GOT_PATCH_CHOICE_YES;
4781 printf("y\n");
4782 } else if (strcmp(line, "n") == 0) {
4783 *choice = GOT_PATCH_CHOICE_NO;
4784 printf("n\n");
4785 } else if (strcmp(line, "q") == 0 &&
4786 status == GOT_STATUS_MODIFY) {
4787 *choice = GOT_PATCH_CHOICE_QUIT;
4788 printf("q\n");
4789 } else
4790 printf("invalid response '%s'\n", line);
4791 free(line);
4792 return NULL;
4795 while (resp != 'y' && resp != 'n' && resp != 'q') {
4796 err = show_change(status, path, patch_file, n, nchanges,
4797 a->action);
4798 if (err)
4799 return err;
4800 resp = getchar();
4801 if (resp == '\n')
4802 resp = getchar();
4803 if (status == GOT_STATUS_MODIFY) {
4804 if (resp != 'y' && resp != 'n' && resp != 'q') {
4805 printf("invalid response '%c'\n", resp);
4806 resp = ' ';
4808 } else if (resp != 'y' && resp != 'n') {
4809 printf("invalid response '%c'\n", resp);
4810 resp = ' ';
4814 if (resp == 'y')
4815 *choice = GOT_PATCH_CHOICE_YES;
4816 else if (resp == 'n')
4817 *choice = GOT_PATCH_CHOICE_NO;
4818 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4819 *choice = GOT_PATCH_CHOICE_QUIT;
4821 return NULL;
4825 static const struct got_error *
4826 cmd_revert(int argc, char *argv[])
4828 const struct got_error *error = NULL;
4829 struct got_worktree *worktree = NULL;
4830 struct got_repository *repo = NULL;
4831 char *cwd = NULL, *path = NULL;
4832 struct got_pathlist_head paths;
4833 struct got_pathlist_entry *pe;
4834 int ch, can_recurse = 0, pflag = 0;
4835 FILE *patch_script_file = NULL;
4836 const char *patch_script_path = NULL;
4837 struct choose_patch_arg cpa;
4839 TAILQ_INIT(&paths);
4841 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4842 switch (ch) {
4843 case 'p':
4844 pflag = 1;
4845 break;
4846 case 'F':
4847 patch_script_path = optarg;
4848 break;
4849 case 'R':
4850 can_recurse = 1;
4851 break;
4852 default:
4853 usage_revert();
4854 /* NOTREACHED */
4858 argc -= optind;
4859 argv += optind;
4861 #ifndef PROFILE
4862 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4863 "unveil", NULL) == -1)
4864 err(1, "pledge");
4865 #endif
4866 if (argc < 1)
4867 usage_revert();
4868 if (patch_script_path && !pflag)
4869 errx(1, "-F option can only be used together with -p option");
4871 cwd = getcwd(NULL, 0);
4872 if (cwd == NULL) {
4873 error = got_error_from_errno("getcwd");
4874 goto done;
4876 error = got_worktree_open(&worktree, cwd);
4877 if (error)
4878 goto done;
4880 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4881 NULL);
4882 if (error != NULL)
4883 goto done;
4885 if (patch_script_path) {
4886 patch_script_file = fopen(patch_script_path, "r");
4887 if (patch_script_file == NULL) {
4888 error = got_error_from_errno2("fopen",
4889 patch_script_path);
4890 goto done;
4893 error = apply_unveil(got_repo_get_path(repo), 1,
4894 got_worktree_get_root_path(worktree));
4895 if (error)
4896 goto done;
4898 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4899 if (error)
4900 goto done;
4902 if (!can_recurse) {
4903 char *ondisk_path;
4904 struct stat sb;
4905 TAILQ_FOREACH(pe, &paths, entry) {
4906 if (asprintf(&ondisk_path, "%s/%s",
4907 got_worktree_get_root_path(worktree),
4908 pe->path) == -1) {
4909 error = got_error_from_errno("asprintf");
4910 goto done;
4912 if (lstat(ondisk_path, &sb) == -1) {
4913 if (errno == ENOENT) {
4914 free(ondisk_path);
4915 continue;
4917 error = got_error_from_errno2("lstat",
4918 ondisk_path);
4919 free(ondisk_path);
4920 goto done;
4922 free(ondisk_path);
4923 if (S_ISDIR(sb.st_mode)) {
4924 error = got_error_msg(GOT_ERR_BAD_PATH,
4925 "reverting directories requires -R option");
4926 goto done;
4931 cpa.patch_script_file = patch_script_file;
4932 cpa.action = "revert";
4933 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4934 pflag ? choose_patch : NULL, &cpa, repo);
4935 done:
4936 if (patch_script_file && fclose(patch_script_file) == EOF &&
4937 error == NULL)
4938 error = got_error_from_errno2("fclose", patch_script_path);
4939 if (repo)
4940 got_repo_close(repo);
4941 if (worktree)
4942 got_worktree_close(worktree);
4943 free(path);
4944 free(cwd);
4945 return error;
4948 __dead static void
4949 usage_commit(void)
4951 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4952 getprogname());
4953 exit(1);
4956 struct collect_commit_logmsg_arg {
4957 const char *cmdline_log;
4958 const char *editor;
4959 const char *worktree_path;
4960 const char *branch_name;
4961 const char *repo_path;
4962 char *logmsg_path;
4966 static const struct got_error *
4967 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4968 void *arg)
4970 char *initial_content = NULL;
4971 struct got_pathlist_entry *pe;
4972 const struct got_error *err = NULL;
4973 char *template = NULL;
4974 struct collect_commit_logmsg_arg *a = arg;
4975 int fd;
4976 size_t len;
4978 /* if a message was specified on the command line, just use it */
4979 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4980 len = strlen(a->cmdline_log) + 1;
4981 *logmsg = malloc(len + 1);
4982 if (*logmsg == NULL)
4983 return got_error_from_errno("malloc");
4984 strlcpy(*logmsg, a->cmdline_log, len);
4985 return NULL;
4988 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4989 return got_error_from_errno("asprintf");
4991 if (asprintf(&initial_content,
4992 "\n# changes to be committed on branch %s:\n",
4993 a->branch_name) == -1)
4994 return got_error_from_errno("asprintf");
4996 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4997 if (err)
4998 goto done;
5000 dprintf(fd, initial_content);
5002 TAILQ_FOREACH(pe, commitable_paths, entry) {
5003 struct got_commitable *ct = pe->data;
5004 dprintf(fd, "# %c %s\n",
5005 got_commitable_get_status(ct),
5006 got_commitable_get_path(ct));
5008 close(fd);
5010 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5011 done:
5012 free(initial_content);
5013 free(template);
5015 /* Editor is done; we can now apply unveil(2) */
5016 if (err == NULL) {
5017 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5018 if (err) {
5019 free(*logmsg);
5020 *logmsg = NULL;
5023 return err;
5026 static const struct got_error *
5027 cmd_commit(int argc, char *argv[])
5029 const struct got_error *error = NULL;
5030 struct got_worktree *worktree = NULL;
5031 struct got_repository *repo = NULL;
5032 char *cwd = NULL, *id_str = NULL;
5033 struct got_object_id *id = NULL;
5034 const char *logmsg = NULL;
5035 struct collect_commit_logmsg_arg cl_arg;
5036 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5037 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5038 struct got_pathlist_head paths;
5040 TAILQ_INIT(&paths);
5041 cl_arg.logmsg_path = NULL;
5043 while ((ch = getopt(argc, argv, "m:")) != -1) {
5044 switch (ch) {
5045 case 'm':
5046 logmsg = optarg;
5047 break;
5048 default:
5049 usage_commit();
5050 /* NOTREACHED */
5054 argc -= optind;
5055 argv += optind;
5057 #ifndef PROFILE
5058 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5059 "unveil", NULL) == -1)
5060 err(1, "pledge");
5061 #endif
5062 cwd = getcwd(NULL, 0);
5063 if (cwd == NULL) {
5064 error = got_error_from_errno("getcwd");
5065 goto done;
5067 error = got_worktree_open(&worktree, cwd);
5068 if (error)
5069 goto done;
5071 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5072 if (error)
5073 goto done;
5074 if (rebase_in_progress) {
5075 error = got_error(GOT_ERR_REBASING);
5076 goto done;
5079 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5080 worktree);
5081 if (error)
5082 goto done;
5084 error = get_gitconfig_path(&gitconfig_path);
5085 if (error)
5086 goto done;
5087 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5088 gitconfig_path);
5089 if (error != NULL)
5090 goto done;
5092 error = get_author(&author, repo);
5093 if (error)
5094 return error;
5097 * unveil(2) traverses exec(2); if an editor is used we have
5098 * to apply unveil after the log message has been written.
5100 if (logmsg == NULL || strlen(logmsg) == 0)
5101 error = get_editor(&editor);
5102 else
5103 error = apply_unveil(got_repo_get_path(repo), 0,
5104 got_worktree_get_root_path(worktree));
5105 if (error)
5106 goto done;
5108 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5109 if (error)
5110 goto done;
5112 cl_arg.editor = editor;
5113 cl_arg.cmdline_log = logmsg;
5114 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5115 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5116 if (!histedit_in_progress) {
5117 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5118 error = got_error(GOT_ERR_COMMIT_BRANCH);
5119 goto done;
5121 cl_arg.branch_name += 11;
5123 cl_arg.repo_path = got_repo_get_path(repo);
5124 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5125 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5126 if (error) {
5127 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5128 cl_arg.logmsg_path != NULL)
5129 preserve_logmsg = 1;
5130 goto done;
5133 error = got_object_id_str(&id_str, id);
5134 if (error)
5135 goto done;
5136 printf("Created commit %s\n", id_str);
5137 done:
5138 if (preserve_logmsg) {
5139 fprintf(stderr, "%s: log message preserved in %s\n",
5140 getprogname(), cl_arg.logmsg_path);
5141 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5142 error == NULL)
5143 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5144 free(cl_arg.logmsg_path);
5145 if (repo)
5146 got_repo_close(repo);
5147 if (worktree)
5148 got_worktree_close(worktree);
5149 free(cwd);
5150 free(id_str);
5151 free(gitconfig_path);
5152 free(editor);
5153 free(author);
5154 return error;
5157 __dead static void
5158 usage_cherrypick(void)
5160 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5161 exit(1);
5164 static const struct got_error *
5165 cmd_cherrypick(int argc, char *argv[])
5167 const struct got_error *error = NULL;
5168 struct got_worktree *worktree = NULL;
5169 struct got_repository *repo = NULL;
5170 char *cwd = NULL, *commit_id_str = NULL;
5171 struct got_object_id *commit_id = NULL;
5172 struct got_commit_object *commit = NULL;
5173 struct got_object_qid *pid;
5174 struct got_reference *head_ref = NULL;
5175 int ch, did_something = 0;
5177 while ((ch = getopt(argc, argv, "")) != -1) {
5178 switch (ch) {
5179 default:
5180 usage_cherrypick();
5181 /* NOTREACHED */
5185 argc -= optind;
5186 argv += optind;
5188 #ifndef PROFILE
5189 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5190 "unveil", NULL) == -1)
5191 err(1, "pledge");
5192 #endif
5193 if (argc != 1)
5194 usage_cherrypick();
5196 cwd = getcwd(NULL, 0);
5197 if (cwd == NULL) {
5198 error = got_error_from_errno("getcwd");
5199 goto done;
5201 error = got_worktree_open(&worktree, cwd);
5202 if (error)
5203 goto done;
5205 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5206 NULL);
5207 if (error != NULL)
5208 goto done;
5210 error = apply_unveil(got_repo_get_path(repo), 0,
5211 got_worktree_get_root_path(worktree));
5212 if (error)
5213 goto done;
5215 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5216 GOT_OBJ_TYPE_COMMIT, repo);
5217 if (error != NULL) {
5218 struct got_reference *ref;
5219 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5220 goto done;
5221 error = got_ref_open(&ref, repo, argv[0], 0);
5222 if (error != NULL)
5223 goto done;
5224 error = got_ref_resolve(&commit_id, repo, ref);
5225 got_ref_close(ref);
5226 if (error != NULL)
5227 goto done;
5229 error = got_object_id_str(&commit_id_str, commit_id);
5230 if (error)
5231 goto done;
5233 error = got_ref_open(&head_ref, repo,
5234 got_worktree_get_head_ref_name(worktree), 0);
5235 if (error != NULL)
5236 goto done;
5238 error = check_same_branch(commit_id, head_ref, NULL, repo);
5239 if (error) {
5240 if (error->code != GOT_ERR_ANCESTRY)
5241 goto done;
5242 error = NULL;
5243 } else {
5244 error = got_error(GOT_ERR_SAME_BRANCH);
5245 goto done;
5248 error = got_object_open_as_commit(&commit, repo, commit_id);
5249 if (error)
5250 goto done;
5251 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5252 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5253 commit_id, repo, update_progress, &did_something, check_cancelled,
5254 NULL);
5255 if (error != NULL)
5256 goto done;
5258 if (did_something)
5259 printf("Merged commit %s\n", commit_id_str);
5260 done:
5261 if (commit)
5262 got_object_commit_close(commit);
5263 free(commit_id_str);
5264 if (head_ref)
5265 got_ref_close(head_ref);
5266 if (worktree)
5267 got_worktree_close(worktree);
5268 if (repo)
5269 got_repo_close(repo);
5270 return error;
5273 __dead static void
5274 usage_backout(void)
5276 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5277 exit(1);
5280 static const struct got_error *
5281 cmd_backout(int argc, char *argv[])
5283 const struct got_error *error = NULL;
5284 struct got_worktree *worktree = NULL;
5285 struct got_repository *repo = NULL;
5286 char *cwd = NULL, *commit_id_str = NULL;
5287 struct got_object_id *commit_id = NULL;
5288 struct got_commit_object *commit = NULL;
5289 struct got_object_qid *pid;
5290 struct got_reference *head_ref = NULL;
5291 int ch, did_something = 0;
5293 while ((ch = getopt(argc, argv, "")) != -1) {
5294 switch (ch) {
5295 default:
5296 usage_backout();
5297 /* NOTREACHED */
5301 argc -= optind;
5302 argv += optind;
5304 #ifndef PROFILE
5305 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5306 "unveil", NULL) == -1)
5307 err(1, "pledge");
5308 #endif
5309 if (argc != 1)
5310 usage_backout();
5312 cwd = getcwd(NULL, 0);
5313 if (cwd == NULL) {
5314 error = got_error_from_errno("getcwd");
5315 goto done;
5317 error = got_worktree_open(&worktree, cwd);
5318 if (error)
5319 goto done;
5321 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5322 NULL);
5323 if (error != NULL)
5324 goto done;
5326 error = apply_unveil(got_repo_get_path(repo), 0,
5327 got_worktree_get_root_path(worktree));
5328 if (error)
5329 goto done;
5331 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5332 GOT_OBJ_TYPE_COMMIT, repo);
5333 if (error != NULL) {
5334 struct got_reference *ref;
5335 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5336 goto done;
5337 error = got_ref_open(&ref, repo, argv[0], 0);
5338 if (error != NULL)
5339 goto done;
5340 error = got_ref_resolve(&commit_id, repo, ref);
5341 got_ref_close(ref);
5342 if (error != NULL)
5343 goto done;
5345 error = got_object_id_str(&commit_id_str, commit_id);
5346 if (error)
5347 goto done;
5349 error = got_ref_open(&head_ref, repo,
5350 got_worktree_get_head_ref_name(worktree), 0);
5351 if (error != NULL)
5352 goto done;
5354 error = check_same_branch(commit_id, head_ref, NULL, repo);
5355 if (error)
5356 goto done;
5358 error = got_object_open_as_commit(&commit, repo, commit_id);
5359 if (error)
5360 goto done;
5361 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5362 if (pid == NULL) {
5363 error = got_error(GOT_ERR_ROOT_COMMIT);
5364 goto done;
5367 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5368 update_progress, &did_something, check_cancelled, NULL);
5369 if (error != NULL)
5370 goto done;
5372 if (did_something)
5373 printf("Backed out commit %s\n", commit_id_str);
5374 done:
5375 if (commit)
5376 got_object_commit_close(commit);
5377 free(commit_id_str);
5378 if (head_ref)
5379 got_ref_close(head_ref);
5380 if (worktree)
5381 got_worktree_close(worktree);
5382 if (repo)
5383 got_repo_close(repo);
5384 return error;
5387 __dead static void
5388 usage_rebase(void)
5390 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5391 getprogname());
5392 exit(1);
5395 void
5396 trim_logmsg(char *logmsg, int limit)
5398 char *nl;
5399 size_t len;
5401 len = strlen(logmsg);
5402 if (len > limit)
5403 len = limit;
5404 logmsg[len] = '\0';
5405 nl = strchr(logmsg, '\n');
5406 if (nl)
5407 *nl = '\0';
5410 static const struct got_error *
5411 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5413 const struct got_error *err;
5414 char *logmsg0 = NULL;
5415 const char *s;
5417 err = got_object_commit_get_logmsg(&logmsg0, commit);
5418 if (err)
5419 return err;
5421 s = logmsg0;
5422 while (isspace((unsigned char)s[0]))
5423 s++;
5425 *logmsg = strdup(s);
5426 if (*logmsg == NULL) {
5427 err = got_error_from_errno("strdup");
5428 goto done;
5431 trim_logmsg(*logmsg, limit);
5432 done:
5433 free(logmsg0);
5434 return err;
5437 static const struct got_error *
5438 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5440 const struct got_error *err;
5441 struct got_commit_object *commit = NULL;
5442 char *id_str = NULL, *logmsg = NULL;
5444 err = got_object_open_as_commit(&commit, repo, id);
5445 if (err)
5446 return err;
5448 err = got_object_id_str(&id_str, id);
5449 if (err)
5450 goto done;
5452 id_str[12] = '\0';
5454 err = get_short_logmsg(&logmsg, 42, commit);
5455 if (err)
5456 goto done;
5458 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5459 done:
5460 free(id_str);
5461 got_object_commit_close(commit);
5462 free(logmsg);
5463 return err;
5466 static const struct got_error *
5467 show_rebase_progress(struct got_commit_object *commit,
5468 struct got_object_id *old_id, struct got_object_id *new_id)
5470 const struct got_error *err;
5471 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5473 err = got_object_id_str(&old_id_str, old_id);
5474 if (err)
5475 goto done;
5477 if (new_id) {
5478 err = got_object_id_str(&new_id_str, new_id);
5479 if (err)
5480 goto done;
5483 old_id_str[12] = '\0';
5484 if (new_id_str)
5485 new_id_str[12] = '\0';
5487 err = get_short_logmsg(&logmsg, 42, commit);
5488 if (err)
5489 goto done;
5491 printf("%s -> %s: %s\n", old_id_str,
5492 new_id_str ? new_id_str : "no-op change", logmsg);
5493 done:
5494 free(old_id_str);
5495 free(new_id_str);
5496 free(logmsg);
5497 return err;
5500 static const struct got_error *
5501 rebase_progress(void *arg, unsigned char status, const char *path)
5503 unsigned char *rebase_status = arg;
5505 while (path[0] == '/')
5506 path++;
5507 printf("%c %s\n", status, path);
5509 if (*rebase_status == GOT_STATUS_CONFLICT)
5510 return NULL;
5511 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5512 *rebase_status = status;
5513 return NULL;
5516 static const struct got_error *
5517 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5518 struct got_reference *branch, struct got_reference *new_base_branch,
5519 struct got_reference *tmp_branch, struct got_repository *repo)
5521 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5522 return got_worktree_rebase_complete(worktree, fileindex,
5523 new_base_branch, tmp_branch, branch, repo);
5526 static const struct got_error *
5527 rebase_commit(struct got_pathlist_head *merged_paths,
5528 struct got_worktree *worktree, struct got_fileindex *fileindex,
5529 struct got_reference *tmp_branch,
5530 struct got_object_id *commit_id, struct got_repository *repo)
5532 const struct got_error *error;
5533 struct got_commit_object *commit;
5534 struct got_object_id *new_commit_id;
5536 error = got_object_open_as_commit(&commit, repo, commit_id);
5537 if (error)
5538 return error;
5540 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5541 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5542 if (error) {
5543 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5544 goto done;
5545 error = show_rebase_progress(commit, commit_id, NULL);
5546 } else {
5547 error = show_rebase_progress(commit, commit_id, new_commit_id);
5548 free(new_commit_id);
5550 done:
5551 got_object_commit_close(commit);
5552 return error;
5555 struct check_path_prefix_arg {
5556 const char *path_prefix;
5557 size_t len;
5558 int errcode;
5561 static const struct got_error *
5562 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5563 struct got_blob_object *blob2, struct got_object_id *id1,
5564 struct got_object_id *id2, const char *path1, const char *path2,
5565 mode_t mode1, mode_t mode2, struct got_repository *repo)
5567 struct check_path_prefix_arg *a = arg;
5569 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5570 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5571 return got_error(a->errcode);
5573 return NULL;
5576 static const struct got_error *
5577 check_path_prefix(struct got_object_id *parent_id,
5578 struct got_object_id *commit_id, const char *path_prefix,
5579 int errcode, struct got_repository *repo)
5581 const struct got_error *err;
5582 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5583 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5584 struct check_path_prefix_arg cpp_arg;
5586 if (got_path_is_root_dir(path_prefix))
5587 return NULL;
5589 err = got_object_open_as_commit(&commit, repo, commit_id);
5590 if (err)
5591 goto done;
5593 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5594 if (err)
5595 goto done;
5597 err = got_object_open_as_tree(&tree1, repo,
5598 got_object_commit_get_tree_id(parent_commit));
5599 if (err)
5600 goto done;
5602 err = got_object_open_as_tree(&tree2, repo,
5603 got_object_commit_get_tree_id(commit));
5604 if (err)
5605 goto done;
5607 cpp_arg.path_prefix = path_prefix;
5608 while (cpp_arg.path_prefix[0] == '/')
5609 cpp_arg.path_prefix++;
5610 cpp_arg.len = strlen(cpp_arg.path_prefix);
5611 cpp_arg.errcode = errcode;
5612 err = got_diff_tree(tree1, tree2, "", "", repo,
5613 check_path_prefix_in_diff, &cpp_arg, 0);
5614 done:
5615 if (tree1)
5616 got_object_tree_close(tree1);
5617 if (tree2)
5618 got_object_tree_close(tree2);
5619 if (commit)
5620 got_object_commit_close(commit);
5621 if (parent_commit)
5622 got_object_commit_close(parent_commit);
5623 return err;
5626 static const struct got_error *
5627 collect_commits(struct got_object_id_queue *commits,
5628 struct got_object_id *initial_commit_id,
5629 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5630 const char *path_prefix, int path_prefix_errcode,
5631 struct got_repository *repo)
5633 const struct got_error *err = NULL;
5634 struct got_commit_graph *graph = NULL;
5635 struct got_object_id *parent_id = NULL;
5636 struct got_object_qid *qid;
5637 struct got_object_id *commit_id = initial_commit_id;
5639 err = got_commit_graph_open(&graph, "/", 1);
5640 if (err)
5641 return err;
5643 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5644 check_cancelled, NULL);
5645 if (err)
5646 goto done;
5647 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5648 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5649 check_cancelled, NULL);
5650 if (err) {
5651 if (err->code == GOT_ERR_ITER_COMPLETED) {
5652 err = got_error_msg(GOT_ERR_ANCESTRY,
5653 "ran out of commits to rebase before "
5654 "youngest common ancestor commit has "
5655 "been reached?!?");
5657 goto done;
5658 } else {
5659 err = check_path_prefix(parent_id, commit_id,
5660 path_prefix, path_prefix_errcode, repo);
5661 if (err)
5662 goto done;
5664 err = got_object_qid_alloc(&qid, commit_id);
5665 if (err)
5666 goto done;
5667 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5668 commit_id = parent_id;
5671 done:
5672 got_commit_graph_close(graph);
5673 return err;
5676 static const struct got_error *
5677 cmd_rebase(int argc, char *argv[])
5679 const struct got_error *error = NULL;
5680 struct got_worktree *worktree = NULL;
5681 struct got_repository *repo = NULL;
5682 struct got_fileindex *fileindex = NULL;
5683 char *cwd = NULL;
5684 struct got_reference *branch = NULL;
5685 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5686 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5687 struct got_object_id *resume_commit_id = NULL;
5688 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5689 struct got_commit_object *commit = NULL;
5690 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5691 int histedit_in_progress = 0;
5692 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5693 struct got_object_id_queue commits;
5694 struct got_pathlist_head merged_paths;
5695 const struct got_object_id_queue *parent_ids;
5696 struct got_object_qid *qid, *pid;
5698 SIMPLEQ_INIT(&commits);
5699 TAILQ_INIT(&merged_paths);
5701 while ((ch = getopt(argc, argv, "ac")) != -1) {
5702 switch (ch) {
5703 case 'a':
5704 abort_rebase = 1;
5705 break;
5706 case 'c':
5707 continue_rebase = 1;
5708 break;
5709 default:
5710 usage_rebase();
5711 /* NOTREACHED */
5715 argc -= optind;
5716 argv += optind;
5718 #ifndef PROFILE
5719 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5720 "unveil", NULL) == -1)
5721 err(1, "pledge");
5722 #endif
5723 if (abort_rebase && continue_rebase)
5724 usage_rebase();
5725 else if (abort_rebase || continue_rebase) {
5726 if (argc != 0)
5727 usage_rebase();
5728 } else if (argc != 1)
5729 usage_rebase();
5731 cwd = getcwd(NULL, 0);
5732 if (cwd == NULL) {
5733 error = got_error_from_errno("getcwd");
5734 goto done;
5736 error = got_worktree_open(&worktree, cwd);
5737 if (error)
5738 goto done;
5740 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5741 NULL);
5742 if (error != NULL)
5743 goto done;
5745 error = apply_unveil(got_repo_get_path(repo), 0,
5746 got_worktree_get_root_path(worktree));
5747 if (error)
5748 goto done;
5750 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5751 worktree);
5752 if (error)
5753 goto done;
5754 if (histedit_in_progress) {
5755 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5756 goto done;
5759 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5760 if (error)
5761 goto done;
5763 if (abort_rebase) {
5764 int did_something;
5765 if (!rebase_in_progress) {
5766 error = got_error(GOT_ERR_NOT_REBASING);
5767 goto done;
5769 error = got_worktree_rebase_continue(&resume_commit_id,
5770 &new_base_branch, &tmp_branch, &branch, &fileindex,
5771 worktree, repo);
5772 if (error)
5773 goto done;
5774 printf("Switching work tree to %s\n",
5775 got_ref_get_symref_target(new_base_branch));
5776 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5777 new_base_branch, update_progress, &did_something);
5778 if (error)
5779 goto done;
5780 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5781 goto done; /* nothing else to do */
5784 if (continue_rebase) {
5785 if (!rebase_in_progress) {
5786 error = got_error(GOT_ERR_NOT_REBASING);
5787 goto done;
5789 error = got_worktree_rebase_continue(&resume_commit_id,
5790 &new_base_branch, &tmp_branch, &branch, &fileindex,
5791 worktree, repo);
5792 if (error)
5793 goto done;
5795 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5796 resume_commit_id, repo);
5797 if (error)
5798 goto done;
5800 yca_id = got_object_id_dup(resume_commit_id);
5801 if (yca_id == NULL) {
5802 error = got_error_from_errno("got_object_id_dup");
5803 goto done;
5805 } else {
5806 error = got_ref_open(&branch, repo, argv[0], 0);
5807 if (error != NULL)
5808 goto done;
5811 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5812 if (error)
5813 goto done;
5815 if (!continue_rebase) {
5816 struct got_object_id *base_commit_id;
5818 base_commit_id = got_worktree_get_base_commit_id(worktree);
5819 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5820 base_commit_id, branch_head_commit_id, repo,
5821 check_cancelled, NULL);
5822 if (error)
5823 goto done;
5824 if (yca_id == NULL) {
5825 error = got_error_msg(GOT_ERR_ANCESTRY,
5826 "specified branch shares no common ancestry "
5827 "with work tree's branch");
5828 goto done;
5831 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5832 if (error) {
5833 if (error->code != GOT_ERR_ANCESTRY)
5834 goto done;
5835 error = NULL;
5836 } else {
5837 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5838 "specified branch resolves to a commit which "
5839 "is already contained in work tree's branch");
5840 goto done;
5842 error = got_worktree_rebase_prepare(&new_base_branch,
5843 &tmp_branch, &fileindex, worktree, branch, repo);
5844 if (error)
5845 goto done;
5848 commit_id = branch_head_commit_id;
5849 error = got_object_open_as_commit(&commit, repo, commit_id);
5850 if (error)
5851 goto done;
5853 parent_ids = got_object_commit_get_parent_ids(commit);
5854 pid = SIMPLEQ_FIRST(parent_ids);
5855 if (pid == NULL) {
5856 if (!continue_rebase) {
5857 int did_something;
5858 error = got_worktree_rebase_abort(worktree, fileindex,
5859 repo, new_base_branch, update_progress,
5860 &did_something);
5861 if (error)
5862 goto done;
5863 printf("Rebase of %s aborted\n",
5864 got_ref_get_name(branch));
5866 error = got_error(GOT_ERR_EMPTY_REBASE);
5867 goto done;
5869 error = collect_commits(&commits, commit_id, pid->id,
5870 yca_id, got_worktree_get_path_prefix(worktree),
5871 GOT_ERR_REBASE_PATH, repo);
5872 got_object_commit_close(commit);
5873 commit = NULL;
5874 if (error)
5875 goto done;
5877 if (SIMPLEQ_EMPTY(&commits)) {
5878 if (continue_rebase) {
5879 error = rebase_complete(worktree, fileindex,
5880 branch, new_base_branch, tmp_branch, repo);
5881 goto done;
5882 } else {
5883 /* Fast-forward the reference of the branch. */
5884 struct got_object_id *new_head_commit_id;
5885 char *id_str;
5886 error = got_ref_resolve(&new_head_commit_id, repo,
5887 new_base_branch);
5888 if (error)
5889 goto done;
5890 error = got_object_id_str(&id_str, new_head_commit_id);
5891 printf("Forwarding %s to commit %s\n",
5892 got_ref_get_name(branch), id_str);
5893 free(id_str);
5894 error = got_ref_change_ref(branch,
5895 new_head_commit_id);
5896 if (error)
5897 goto done;
5901 pid = NULL;
5902 SIMPLEQ_FOREACH(qid, &commits, entry) {
5903 commit_id = qid->id;
5904 parent_id = pid ? pid->id : yca_id;
5905 pid = qid;
5907 error = got_worktree_rebase_merge_files(&merged_paths,
5908 worktree, fileindex, parent_id, commit_id, repo,
5909 rebase_progress, &rebase_status, check_cancelled, NULL);
5910 if (error)
5911 goto done;
5913 if (rebase_status == GOT_STATUS_CONFLICT) {
5914 error = show_rebase_merge_conflict(qid->id, repo);
5915 if (error)
5916 goto done;
5917 got_worktree_rebase_pathlist_free(&merged_paths);
5918 break;
5921 error = rebase_commit(&merged_paths, worktree, fileindex,
5922 tmp_branch, commit_id, repo);
5923 got_worktree_rebase_pathlist_free(&merged_paths);
5924 if (error)
5925 goto done;
5928 if (rebase_status == GOT_STATUS_CONFLICT) {
5929 error = got_worktree_rebase_postpone(worktree, fileindex);
5930 if (error)
5931 goto done;
5932 error = got_error_msg(GOT_ERR_CONFLICTS,
5933 "conflicts must be resolved before rebasing can continue");
5934 } else
5935 error = rebase_complete(worktree, fileindex, branch,
5936 new_base_branch, tmp_branch, repo);
5937 done:
5938 got_object_id_queue_free(&commits);
5939 free(branch_head_commit_id);
5940 free(resume_commit_id);
5941 free(yca_id);
5942 if (commit)
5943 got_object_commit_close(commit);
5944 if (branch)
5945 got_ref_close(branch);
5946 if (new_base_branch)
5947 got_ref_close(new_base_branch);
5948 if (tmp_branch)
5949 got_ref_close(tmp_branch);
5950 if (worktree)
5951 got_worktree_close(worktree);
5952 if (repo)
5953 got_repo_close(repo);
5954 return error;
5957 __dead static void
5958 usage_histedit(void)
5960 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5961 getprogname());
5962 exit(1);
5965 #define GOT_HISTEDIT_PICK 'p'
5966 #define GOT_HISTEDIT_EDIT 'e'
5967 #define GOT_HISTEDIT_FOLD 'f'
5968 #define GOT_HISTEDIT_DROP 'd'
5969 #define GOT_HISTEDIT_MESG 'm'
5971 static struct got_histedit_cmd {
5972 unsigned char code;
5973 const char *name;
5974 const char *desc;
5975 } got_histedit_cmds[] = {
5976 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5977 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5978 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5979 "be used" },
5980 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5981 { GOT_HISTEDIT_MESG, "mesg",
5982 "single-line log message for commit above (open editor if empty)" },
5985 struct got_histedit_list_entry {
5986 TAILQ_ENTRY(got_histedit_list_entry) entry;
5987 struct got_object_id *commit_id;
5988 const struct got_histedit_cmd *cmd;
5989 char *logmsg;
5991 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5993 static const struct got_error *
5994 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5995 FILE *f, struct got_repository *repo)
5997 const struct got_error *err = NULL;
5998 char *logmsg = NULL, *id_str = NULL;
5999 struct got_commit_object *commit = NULL;
6000 int n;
6002 err = got_object_open_as_commit(&commit, repo, commit_id);
6003 if (err)
6004 goto done;
6006 err = get_short_logmsg(&logmsg, 34, commit);
6007 if (err)
6008 goto done;
6010 err = got_object_id_str(&id_str, commit_id);
6011 if (err)
6012 goto done;
6014 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6015 if (n < 0)
6016 err = got_ferror(f, GOT_ERR_IO);
6017 done:
6018 if (commit)
6019 got_object_commit_close(commit);
6020 free(id_str);
6021 free(logmsg);
6022 return err;
6025 static const struct got_error *
6026 histedit_write_commit_list(struct got_object_id_queue *commits,
6027 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6029 const struct got_error *err = NULL;
6030 struct got_object_qid *qid;
6032 if (SIMPLEQ_EMPTY(commits))
6033 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6035 SIMPLEQ_FOREACH(qid, commits, entry) {
6036 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6037 f, repo);
6038 if (err)
6039 break;
6040 if (edit_logmsg_only) {
6041 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6042 if (n < 0) {
6043 err = got_ferror(f, GOT_ERR_IO);
6044 break;
6049 return err;
6052 static const struct got_error *
6053 write_cmd_list(FILE *f, const char *branch_name,
6054 struct got_object_id_queue *commits)
6056 const struct got_error *err = NULL;
6057 int n, i;
6058 char *id_str;
6059 struct got_object_qid *qid;
6061 qid = SIMPLEQ_FIRST(commits);
6062 err = got_object_id_str(&id_str, qid->id);
6063 if (err)
6064 return err;
6066 n = fprintf(f,
6067 "# Editing the history of branch '%s' starting at\n"
6068 "# commit %s\n"
6069 "# Commits will be processed in order from top to "
6070 "bottom of this file.\n", branch_name, id_str);
6071 if (n < 0) {
6072 err = got_ferror(f, GOT_ERR_IO);
6073 goto done;
6076 n = fprintf(f, "# Available histedit commands:\n");
6077 if (n < 0) {
6078 err = got_ferror(f, GOT_ERR_IO);
6079 goto done;
6082 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6083 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6084 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6085 cmd->desc);
6086 if (n < 0) {
6087 err = got_ferror(f, GOT_ERR_IO);
6088 break;
6091 done:
6092 free(id_str);
6093 return err;
6096 static const struct got_error *
6097 histedit_syntax_error(int lineno)
6099 static char msg[42];
6100 int ret;
6102 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6103 lineno);
6104 if (ret == -1 || ret >= sizeof(msg))
6105 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6107 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6110 static const struct got_error *
6111 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6112 char *logmsg, struct got_repository *repo)
6114 const struct got_error *err;
6115 struct got_commit_object *folded_commit = NULL;
6116 char *id_str, *folded_logmsg = NULL;
6118 err = got_object_id_str(&id_str, hle->commit_id);
6119 if (err)
6120 return err;
6122 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6123 if (err)
6124 goto done;
6126 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6127 if (err)
6128 goto done;
6129 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6130 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6131 folded_logmsg) == -1) {
6132 err = got_error_from_errno("asprintf");
6134 done:
6135 if (folded_commit)
6136 got_object_commit_close(folded_commit);
6137 free(id_str);
6138 free(folded_logmsg);
6139 return err;
6142 static struct got_histedit_list_entry *
6143 get_folded_commits(struct got_histedit_list_entry *hle)
6145 struct got_histedit_list_entry *prev, *folded = NULL;
6147 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6148 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6149 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6150 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6151 folded = prev;
6152 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6155 return folded;
6158 static const struct got_error *
6159 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6160 struct got_repository *repo)
6162 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6163 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6164 const struct got_error *err = NULL;
6165 struct got_commit_object *commit = NULL;
6166 int fd;
6167 struct got_histedit_list_entry *folded = NULL;
6169 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6170 if (err)
6171 return err;
6173 folded = get_folded_commits(hle);
6174 if (folded) {
6175 while (folded != hle) {
6176 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6177 folded = TAILQ_NEXT(folded, entry);
6178 continue;
6180 err = append_folded_commit_msg(&new_msg, folded,
6181 logmsg, repo);
6182 if (err)
6183 goto done;
6184 free(logmsg);
6185 logmsg = new_msg;
6186 folded = TAILQ_NEXT(folded, entry);
6190 err = got_object_id_str(&id_str, hle->commit_id);
6191 if (err)
6192 goto done;
6193 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6194 if (err)
6195 goto done;
6196 if (asprintf(&new_msg,
6197 "%s\n# original log message of commit %s: %s",
6198 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6199 err = got_error_from_errno("asprintf");
6200 goto done;
6202 free(logmsg);
6203 logmsg = new_msg;
6205 err = got_object_id_str(&id_str, hle->commit_id);
6206 if (err)
6207 goto done;
6209 err = got_opentemp_named_fd(&logmsg_path, &fd,
6210 GOT_TMPDIR_STR "/got-logmsg");
6211 if (err)
6212 goto done;
6214 dprintf(fd, logmsg);
6215 close(fd);
6217 err = get_editor(&editor);
6218 if (err)
6219 goto done;
6221 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6222 if (err) {
6223 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6224 goto done;
6225 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6227 done:
6228 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6229 err = got_error_from_errno2("unlink", logmsg_path);
6230 free(logmsg_path);
6231 free(logmsg);
6232 free(orig_logmsg);
6233 free(editor);
6234 if (commit)
6235 got_object_commit_close(commit);
6236 return err;
6239 static const struct got_error *
6240 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6241 FILE *f, struct got_repository *repo)
6243 const struct got_error *err = NULL;
6244 char *line = NULL, *p, *end;
6245 size_t size;
6246 ssize_t len;
6247 int lineno = 0, i;
6248 const struct got_histedit_cmd *cmd;
6249 struct got_object_id *commit_id = NULL;
6250 struct got_histedit_list_entry *hle = NULL;
6252 for (;;) {
6253 len = getline(&line, &size, f);
6254 if (len == -1) {
6255 const struct got_error *getline_err;
6256 if (feof(f))
6257 break;
6258 getline_err = got_error_from_errno("getline");
6259 err = got_ferror(f, getline_err->code);
6260 break;
6262 lineno++;
6263 p = line;
6264 while (isspace((unsigned char)p[0]))
6265 p++;
6266 if (p[0] == '#' || p[0] == '\0') {
6267 free(line);
6268 line = NULL;
6269 continue;
6271 cmd = NULL;
6272 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6273 cmd = &got_histedit_cmds[i];
6274 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6275 isspace((unsigned char)p[strlen(cmd->name)])) {
6276 p += strlen(cmd->name);
6277 break;
6279 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6280 p++;
6281 break;
6284 if (i == nitems(got_histedit_cmds)) {
6285 err = histedit_syntax_error(lineno);
6286 break;
6288 while (isspace((unsigned char)p[0]))
6289 p++;
6290 if (cmd->code == GOT_HISTEDIT_MESG) {
6291 if (hle == NULL || hle->logmsg != NULL) {
6292 err = got_error(GOT_ERR_HISTEDIT_CMD);
6293 break;
6295 if (p[0] == '\0') {
6296 err = histedit_edit_logmsg(hle, repo);
6297 if (err)
6298 break;
6299 } else {
6300 hle->logmsg = strdup(p);
6301 if (hle->logmsg == NULL) {
6302 err = got_error_from_errno("strdup");
6303 break;
6306 free(line);
6307 line = NULL;
6308 continue;
6309 } else {
6310 end = p;
6311 while (end[0] && !isspace((unsigned char)end[0]))
6312 end++;
6313 *end = '\0';
6315 err = got_object_resolve_id_str(&commit_id, repo, p);
6316 if (err) {
6317 /* override error code */
6318 err = histedit_syntax_error(lineno);
6319 break;
6322 hle = malloc(sizeof(*hle));
6323 if (hle == NULL) {
6324 err = got_error_from_errno("malloc");
6325 break;
6327 hle->cmd = cmd;
6328 hle->commit_id = commit_id;
6329 hle->logmsg = NULL;
6330 commit_id = NULL;
6331 free(line);
6332 line = NULL;
6333 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6336 free(line);
6337 free(commit_id);
6338 return err;
6341 static const struct got_error *
6342 histedit_check_script(struct got_histedit_list *histedit_cmds,
6343 struct got_object_id_queue *commits, struct got_repository *repo)
6345 const struct got_error *err = NULL;
6346 struct got_object_qid *qid;
6347 struct got_histedit_list_entry *hle;
6348 static char msg[92];
6349 char *id_str;
6351 if (TAILQ_EMPTY(histedit_cmds))
6352 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6353 "histedit script contains no commands");
6354 if (SIMPLEQ_EMPTY(commits))
6355 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6357 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6358 struct got_histedit_list_entry *hle2;
6359 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6360 if (hle == hle2)
6361 continue;
6362 if (got_object_id_cmp(hle->commit_id,
6363 hle2->commit_id) != 0)
6364 continue;
6365 err = got_object_id_str(&id_str, hle->commit_id);
6366 if (err)
6367 return err;
6368 snprintf(msg, sizeof(msg), "commit %s is listed "
6369 "more than once in histedit script", id_str);
6370 free(id_str);
6371 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6375 SIMPLEQ_FOREACH(qid, commits, entry) {
6376 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6377 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6378 break;
6380 if (hle == NULL) {
6381 err = got_object_id_str(&id_str, qid->id);
6382 if (err)
6383 return err;
6384 snprintf(msg, sizeof(msg),
6385 "commit %s missing from histedit script", id_str);
6386 free(id_str);
6387 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6391 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6392 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6393 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6394 "last commit in histedit script cannot be folded");
6396 return NULL;
6399 static const struct got_error *
6400 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6401 const char *path, struct got_object_id_queue *commits,
6402 struct got_repository *repo)
6404 const struct got_error *err = NULL;
6405 char *editor;
6406 FILE *f = NULL;
6408 err = get_editor(&editor);
6409 if (err)
6410 return err;
6412 if (spawn_editor(editor, path) == -1) {
6413 err = got_error_from_errno("failed spawning editor");
6414 goto done;
6417 f = fopen(path, "r");
6418 if (f == NULL) {
6419 err = got_error_from_errno("fopen");
6420 goto done;
6422 err = histedit_parse_list(histedit_cmds, f, repo);
6423 if (err)
6424 goto done;
6426 err = histedit_check_script(histedit_cmds, commits, repo);
6427 done:
6428 if (f && fclose(f) != 0 && err == NULL)
6429 err = got_error_from_errno("fclose");
6430 free(editor);
6431 return err;
6434 static const struct got_error *
6435 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6436 struct got_object_id_queue *, const char *, const char *,
6437 struct got_repository *);
6439 static const struct got_error *
6440 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6441 struct got_object_id_queue *commits, const char *branch_name,
6442 int edit_logmsg_only, struct got_repository *repo)
6444 const struct got_error *err;
6445 FILE *f = NULL;
6446 char *path = NULL;
6448 err = got_opentemp_named(&path, &f, "got-histedit");
6449 if (err)
6450 return err;
6452 err = write_cmd_list(f, branch_name, commits);
6453 if (err)
6454 goto done;
6456 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6457 if (err)
6458 goto done;
6460 if (edit_logmsg_only) {
6461 rewind(f);
6462 err = histedit_parse_list(histedit_cmds, f, repo);
6463 } else {
6464 if (fclose(f) != 0) {
6465 err = got_error_from_errno("fclose");
6466 goto done;
6468 f = NULL;
6469 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6470 if (err) {
6471 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6472 err->code != GOT_ERR_HISTEDIT_CMD)
6473 goto done;
6474 err = histedit_edit_list_retry(histedit_cmds, err,
6475 commits, path, branch_name, repo);
6478 done:
6479 if (f && fclose(f) != 0 && err == NULL)
6480 err = got_error_from_errno("fclose");
6481 if (path && unlink(path) != 0 && err == NULL)
6482 err = got_error_from_errno2("unlink", path);
6483 free(path);
6484 return err;
6487 static const struct got_error *
6488 histedit_save_list(struct got_histedit_list *histedit_cmds,
6489 struct got_worktree *worktree, struct got_repository *repo)
6491 const struct got_error *err = NULL;
6492 char *path = NULL;
6493 FILE *f = NULL;
6494 struct got_histedit_list_entry *hle;
6495 struct got_commit_object *commit = NULL;
6497 err = got_worktree_get_histedit_script_path(&path, worktree);
6498 if (err)
6499 return err;
6501 f = fopen(path, "w");
6502 if (f == NULL) {
6503 err = got_error_from_errno2("fopen", path);
6504 goto done;
6506 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6507 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6508 repo);
6509 if (err)
6510 break;
6512 if (hle->logmsg) {
6513 int n = fprintf(f, "%c %s\n",
6514 GOT_HISTEDIT_MESG, hle->logmsg);
6515 if (n < 0) {
6516 err = got_ferror(f, GOT_ERR_IO);
6517 break;
6521 done:
6522 if (f && fclose(f) != 0 && err == NULL)
6523 err = got_error_from_errno("fclose");
6524 free(path);
6525 if (commit)
6526 got_object_commit_close(commit);
6527 return err;
6530 void
6531 histedit_free_list(struct got_histedit_list *histedit_cmds)
6533 struct got_histedit_list_entry *hle;
6535 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6536 TAILQ_REMOVE(histedit_cmds, hle, entry);
6537 free(hle);
6541 static const struct got_error *
6542 histedit_load_list(struct got_histedit_list *histedit_cmds,
6543 const char *path, struct got_repository *repo)
6545 const struct got_error *err = NULL;
6546 FILE *f = NULL;
6548 f = fopen(path, "r");
6549 if (f == NULL) {
6550 err = got_error_from_errno2("fopen", path);
6551 goto done;
6554 err = histedit_parse_list(histedit_cmds, f, repo);
6555 done:
6556 if (f && fclose(f) != 0 && err == NULL)
6557 err = got_error_from_errno("fclose");
6558 return err;
6561 static const struct got_error *
6562 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6563 const struct got_error *edit_err, struct got_object_id_queue *commits,
6564 const char *path, const char *branch_name, struct got_repository *repo)
6566 const struct got_error *err = NULL, *prev_err = edit_err;
6567 int resp = ' ';
6569 while (resp != 'c' && resp != 'r' && resp != 'a') {
6570 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6571 "or (a)bort: ", getprogname(), prev_err->msg);
6572 resp = getchar();
6573 if (resp == '\n')
6574 resp = getchar();
6575 if (resp == 'c') {
6576 histedit_free_list(histedit_cmds);
6577 err = histedit_run_editor(histedit_cmds, path, commits,
6578 repo);
6579 if (err) {
6580 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6581 err->code != GOT_ERR_HISTEDIT_CMD)
6582 break;
6583 prev_err = err;
6584 resp = ' ';
6585 continue;
6587 break;
6588 } else if (resp == 'r') {
6589 histedit_free_list(histedit_cmds);
6590 err = histedit_edit_script(histedit_cmds,
6591 commits, branch_name, 0, repo);
6592 if (err) {
6593 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6594 err->code != GOT_ERR_HISTEDIT_CMD)
6595 break;
6596 prev_err = err;
6597 resp = ' ';
6598 continue;
6600 break;
6601 } else if (resp == 'a') {
6602 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6603 break;
6604 } else
6605 printf("invalid response '%c'\n", resp);
6608 return err;
6611 static const struct got_error *
6612 histedit_complete(struct got_worktree *worktree,
6613 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6614 struct got_reference *branch, struct got_repository *repo)
6616 printf("Switching work tree to %s\n",
6617 got_ref_get_symref_target(branch));
6618 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6619 branch, repo);
6622 static const struct got_error *
6623 show_histedit_progress(struct got_commit_object *commit,
6624 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6626 const struct got_error *err;
6627 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6629 err = got_object_id_str(&old_id_str, hle->commit_id);
6630 if (err)
6631 goto done;
6633 if (new_id) {
6634 err = got_object_id_str(&new_id_str, new_id);
6635 if (err)
6636 goto done;
6639 old_id_str[12] = '\0';
6640 if (new_id_str)
6641 new_id_str[12] = '\0';
6643 if (hle->logmsg) {
6644 logmsg = strdup(hle->logmsg);
6645 if (logmsg == NULL) {
6646 err = got_error_from_errno("strdup");
6647 goto done;
6649 trim_logmsg(logmsg, 42);
6650 } else {
6651 err = get_short_logmsg(&logmsg, 42, commit);
6652 if (err)
6653 goto done;
6656 switch (hle->cmd->code) {
6657 case GOT_HISTEDIT_PICK:
6658 case GOT_HISTEDIT_EDIT:
6659 printf("%s -> %s: %s\n", old_id_str,
6660 new_id_str ? new_id_str : "no-op change", logmsg);
6661 break;
6662 case GOT_HISTEDIT_DROP:
6663 case GOT_HISTEDIT_FOLD:
6664 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6665 logmsg);
6666 break;
6667 default:
6668 break;
6670 done:
6671 free(old_id_str);
6672 free(new_id_str);
6673 return err;
6676 static const struct got_error *
6677 histedit_commit(struct got_pathlist_head *merged_paths,
6678 struct got_worktree *worktree, struct got_fileindex *fileindex,
6679 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6680 struct got_repository *repo)
6682 const struct got_error *err;
6683 struct got_commit_object *commit;
6684 struct got_object_id *new_commit_id;
6686 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6687 && hle->logmsg == NULL) {
6688 err = histedit_edit_logmsg(hle, repo);
6689 if (err)
6690 return err;
6693 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6694 if (err)
6695 return err;
6697 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6698 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6699 hle->logmsg, repo);
6700 if (err) {
6701 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6702 goto done;
6703 err = show_histedit_progress(commit, hle, NULL);
6704 } else {
6705 err = show_histedit_progress(commit, hle, new_commit_id);
6706 free(new_commit_id);
6708 done:
6709 got_object_commit_close(commit);
6710 return err;
6713 static const struct got_error *
6714 histedit_skip_commit(struct got_histedit_list_entry *hle,
6715 struct got_worktree *worktree, struct got_repository *repo)
6717 const struct got_error *error;
6718 struct got_commit_object *commit;
6720 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6721 repo);
6722 if (error)
6723 return error;
6725 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6726 if (error)
6727 return error;
6729 error = show_histedit_progress(commit, hle, NULL);
6730 got_object_commit_close(commit);
6731 return error;
6734 static const struct got_error *
6735 check_local_changes(void *arg, unsigned char status,
6736 unsigned char staged_status, const char *path,
6737 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6738 struct got_object_id *commit_id, int dirfd, const char *de_name)
6740 int *have_local_changes = arg;
6742 switch (status) {
6743 case GOT_STATUS_ADD:
6744 case GOT_STATUS_DELETE:
6745 case GOT_STATUS_MODIFY:
6746 case GOT_STATUS_CONFLICT:
6747 *have_local_changes = 1;
6748 return got_error(GOT_ERR_CANCELLED);
6749 default:
6750 break;
6753 switch (staged_status) {
6754 case GOT_STATUS_ADD:
6755 case GOT_STATUS_DELETE:
6756 case GOT_STATUS_MODIFY:
6757 *have_local_changes = 1;
6758 return got_error(GOT_ERR_CANCELLED);
6759 default:
6760 break;
6763 return NULL;
6766 static const struct got_error *
6767 cmd_histedit(int argc, char *argv[])
6769 const struct got_error *error = NULL;
6770 struct got_worktree *worktree = NULL;
6771 struct got_fileindex *fileindex = NULL;
6772 struct got_repository *repo = NULL;
6773 char *cwd = NULL;
6774 struct got_reference *branch = NULL;
6775 struct got_reference *tmp_branch = NULL;
6776 struct got_object_id *resume_commit_id = NULL;
6777 struct got_object_id *base_commit_id = NULL;
6778 struct got_object_id *head_commit_id = NULL;
6779 struct got_commit_object *commit = NULL;
6780 int ch, rebase_in_progress = 0, did_something;
6781 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6782 int edit_logmsg_only = 0;
6783 const char *edit_script_path = NULL;
6784 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6785 struct got_object_id_queue commits;
6786 struct got_pathlist_head merged_paths;
6787 const struct got_object_id_queue *parent_ids;
6788 struct got_object_qid *pid;
6789 struct got_histedit_list histedit_cmds;
6790 struct got_histedit_list_entry *hle;
6792 SIMPLEQ_INIT(&commits);
6793 TAILQ_INIT(&histedit_cmds);
6794 TAILQ_INIT(&merged_paths);
6796 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6797 switch (ch) {
6798 case 'a':
6799 abort_edit = 1;
6800 break;
6801 case 'c':
6802 continue_edit = 1;
6803 break;
6804 case 'F':
6805 edit_script_path = optarg;
6806 break;
6807 case 'm':
6808 edit_logmsg_only = 1;
6809 break;
6810 default:
6811 usage_histedit();
6812 /* NOTREACHED */
6816 argc -= optind;
6817 argv += optind;
6819 #ifndef PROFILE
6820 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6821 "unveil", NULL) == -1)
6822 err(1, "pledge");
6823 #endif
6824 if (abort_edit && continue_edit)
6825 errx(1, "histedit's -a and -c options are mutually exclusive");
6826 if (edit_script_path && edit_logmsg_only)
6827 errx(1, "histedit's -F and -m options are mutually exclusive");
6828 if (abort_edit && edit_logmsg_only)
6829 errx(1, "histedit's -a and -m options are mutually exclusive");
6830 if (continue_edit && edit_logmsg_only)
6831 errx(1, "histedit's -c and -m options are mutually exclusive");
6832 if (argc != 0)
6833 usage_histedit();
6836 * This command cannot apply unveil(2) in all cases because the
6837 * user may choose to run an editor to edit the histedit script
6838 * and to edit individual commit log messages.
6839 * unveil(2) traverses exec(2); if an editor is used we have to
6840 * apply unveil after edit script and log messages have been written.
6841 * XXX TODO: Make use of unveil(2) where possible.
6844 cwd = getcwd(NULL, 0);
6845 if (cwd == NULL) {
6846 error = got_error_from_errno("getcwd");
6847 goto done;
6849 error = got_worktree_open(&worktree, cwd);
6850 if (error)
6851 goto done;
6853 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6854 NULL);
6855 if (error != NULL)
6856 goto done;
6858 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6859 if (error)
6860 goto done;
6861 if (rebase_in_progress) {
6862 error = got_error(GOT_ERR_REBASING);
6863 goto done;
6866 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6867 if (error)
6868 goto done;
6870 if (edit_in_progress && edit_logmsg_only) {
6871 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6872 "histedit operation is in progress in this "
6873 "work tree and must be continued or aborted "
6874 "before the -m option can be used");
6875 goto done;
6878 if (edit_in_progress && abort_edit) {
6879 error = got_worktree_histedit_continue(&resume_commit_id,
6880 &tmp_branch, &branch, &base_commit_id, &fileindex,
6881 worktree, repo);
6882 if (error)
6883 goto done;
6884 printf("Switching work tree to %s\n",
6885 got_ref_get_symref_target(branch));
6886 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6887 branch, base_commit_id, update_progress, &did_something);
6888 if (error)
6889 goto done;
6890 printf("Histedit of %s aborted\n",
6891 got_ref_get_symref_target(branch));
6892 goto done; /* nothing else to do */
6893 } else if (abort_edit) {
6894 error = got_error(GOT_ERR_NOT_HISTEDIT);
6895 goto done;
6898 if (continue_edit) {
6899 char *path;
6901 if (!edit_in_progress) {
6902 error = got_error(GOT_ERR_NOT_HISTEDIT);
6903 goto done;
6906 error = got_worktree_get_histedit_script_path(&path, worktree);
6907 if (error)
6908 goto done;
6910 error = histedit_load_list(&histedit_cmds, path, repo);
6911 free(path);
6912 if (error)
6913 goto done;
6915 error = got_worktree_histedit_continue(&resume_commit_id,
6916 &tmp_branch, &branch, &base_commit_id, &fileindex,
6917 worktree, repo);
6918 if (error)
6919 goto done;
6921 error = got_ref_resolve(&head_commit_id, repo, branch);
6922 if (error)
6923 goto done;
6925 error = got_object_open_as_commit(&commit, repo,
6926 head_commit_id);
6927 if (error)
6928 goto done;
6929 parent_ids = got_object_commit_get_parent_ids(commit);
6930 pid = SIMPLEQ_FIRST(parent_ids);
6931 if (pid == NULL) {
6932 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6933 goto done;
6935 error = collect_commits(&commits, head_commit_id, pid->id,
6936 base_commit_id, got_worktree_get_path_prefix(worktree),
6937 GOT_ERR_HISTEDIT_PATH, repo);
6938 got_object_commit_close(commit);
6939 commit = NULL;
6940 if (error)
6941 goto done;
6942 } else {
6943 if (edit_in_progress) {
6944 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6945 goto done;
6948 error = got_ref_open(&branch, repo,
6949 got_worktree_get_head_ref_name(worktree), 0);
6950 if (error != NULL)
6951 goto done;
6953 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6954 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6955 "will not edit commit history of a branch outside "
6956 "the \"refs/heads/\" reference namespace");
6957 goto done;
6960 error = got_ref_resolve(&head_commit_id, repo, branch);
6961 got_ref_close(branch);
6962 branch = NULL;
6963 if (error)
6964 goto done;
6966 error = got_object_open_as_commit(&commit, repo,
6967 head_commit_id);
6968 if (error)
6969 goto done;
6970 parent_ids = got_object_commit_get_parent_ids(commit);
6971 pid = SIMPLEQ_FIRST(parent_ids);
6972 if (pid == NULL) {
6973 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6974 goto done;
6976 error = collect_commits(&commits, head_commit_id, pid->id,
6977 got_worktree_get_base_commit_id(worktree),
6978 got_worktree_get_path_prefix(worktree),
6979 GOT_ERR_HISTEDIT_PATH, repo);
6980 got_object_commit_close(commit);
6981 commit = NULL;
6982 if (error)
6983 goto done;
6985 if (SIMPLEQ_EMPTY(&commits)) {
6986 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6987 goto done;
6990 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6991 &base_commit_id, &fileindex, worktree, repo);
6992 if (error)
6993 goto done;
6995 if (edit_script_path) {
6996 error = histedit_load_list(&histedit_cmds,
6997 edit_script_path, repo);
6998 if (error) {
6999 got_worktree_histedit_abort(worktree, fileindex,
7000 repo, branch, base_commit_id,
7001 update_progress, &did_something);
7002 goto done;
7004 } else {
7005 const char *branch_name;
7006 branch_name = got_ref_get_symref_target(branch);
7007 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7008 branch_name += 11;
7009 error = histedit_edit_script(&histedit_cmds, &commits,
7010 branch_name, edit_logmsg_only, repo);
7011 if (error) {
7012 got_worktree_histedit_abort(worktree, fileindex,
7013 repo, branch, base_commit_id,
7014 update_progress, &did_something);
7015 goto done;
7020 error = histedit_save_list(&histedit_cmds, worktree,
7021 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_check_script(&histedit_cmds, &commits, repo);
7032 if (error)
7033 goto done;
7035 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7036 if (resume_commit_id) {
7037 if (got_object_id_cmp(hle->commit_id,
7038 resume_commit_id) != 0)
7039 continue;
7041 resume_commit_id = NULL;
7042 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7043 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7044 error = histedit_skip_commit(hle, worktree,
7045 repo);
7046 if (error)
7047 goto done;
7048 } else {
7049 struct got_pathlist_head paths;
7050 int have_changes = 0;
7052 TAILQ_INIT(&paths);
7053 error = got_pathlist_append(&paths, "", NULL);
7054 if (error)
7055 goto done;
7056 error = got_worktree_status(worktree, &paths,
7057 repo, check_local_changes, &have_changes,
7058 check_cancelled, NULL);
7059 got_pathlist_free(&paths);
7060 if (error) {
7061 if (error->code != GOT_ERR_CANCELLED)
7062 goto done;
7063 if (sigint_received || sigpipe_received)
7064 goto done;
7066 if (have_changes) {
7067 error = histedit_commit(NULL, worktree,
7068 fileindex, tmp_branch, hle, repo);
7069 if (error)
7070 goto done;
7071 } else {
7072 error = got_object_open_as_commit(
7073 &commit, repo, hle->commit_id);
7074 if (error)
7075 goto done;
7076 error = show_histedit_progress(commit,
7077 hle, NULL);
7078 got_object_commit_close(commit);
7079 commit = NULL;
7080 if (error)
7081 goto done;
7084 continue;
7087 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7088 error = histedit_skip_commit(hle, worktree, repo);
7089 if (error)
7090 goto done;
7091 continue;
7094 error = got_object_open_as_commit(&commit, repo,
7095 hle->commit_id);
7096 if (error)
7097 goto done;
7098 parent_ids = got_object_commit_get_parent_ids(commit);
7099 pid = SIMPLEQ_FIRST(parent_ids);
7101 error = got_worktree_histedit_merge_files(&merged_paths,
7102 worktree, fileindex, pid->id, hle->commit_id, repo,
7103 rebase_progress, &rebase_status, check_cancelled, NULL);
7104 if (error)
7105 goto done;
7106 got_object_commit_close(commit);
7107 commit = NULL;
7109 if (rebase_status == GOT_STATUS_CONFLICT) {
7110 error = show_rebase_merge_conflict(hle->commit_id,
7111 repo);
7112 if (error)
7113 goto done;
7114 got_worktree_rebase_pathlist_free(&merged_paths);
7115 break;
7118 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7119 char *id_str;
7120 error = got_object_id_str(&id_str, hle->commit_id);
7121 if (error)
7122 goto done;
7123 printf("Stopping histedit for amending commit %s\n",
7124 id_str);
7125 free(id_str);
7126 got_worktree_rebase_pathlist_free(&merged_paths);
7127 error = got_worktree_histedit_postpone(worktree,
7128 fileindex);
7129 goto done;
7132 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7133 error = histedit_skip_commit(hle, worktree, repo);
7134 if (error)
7135 goto done;
7136 continue;
7139 error = histedit_commit(&merged_paths, worktree, fileindex,
7140 tmp_branch, hle, repo);
7141 got_worktree_rebase_pathlist_free(&merged_paths);
7142 if (error)
7143 goto done;
7146 if (rebase_status == GOT_STATUS_CONFLICT) {
7147 error = got_worktree_histedit_postpone(worktree, fileindex);
7148 if (error)
7149 goto done;
7150 error = got_error_msg(GOT_ERR_CONFLICTS,
7151 "conflicts must be resolved before histedit can continue");
7152 } else
7153 error = histedit_complete(worktree, fileindex, tmp_branch,
7154 branch, repo);
7155 done:
7156 got_object_id_queue_free(&commits);
7157 histedit_free_list(&histedit_cmds);
7158 free(head_commit_id);
7159 free(base_commit_id);
7160 free(resume_commit_id);
7161 if (commit)
7162 got_object_commit_close(commit);
7163 if (branch)
7164 got_ref_close(branch);
7165 if (tmp_branch)
7166 got_ref_close(tmp_branch);
7167 if (worktree)
7168 got_worktree_close(worktree);
7169 if (repo)
7170 got_repo_close(repo);
7171 return error;
7174 __dead static void
7175 usage_integrate(void)
7177 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7178 exit(1);
7181 static const struct got_error *
7182 cmd_integrate(int argc, char *argv[])
7184 const struct got_error *error = NULL;
7185 struct got_repository *repo = NULL;
7186 struct got_worktree *worktree = NULL;
7187 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7188 const char *branch_arg = NULL;
7189 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7190 struct got_fileindex *fileindex = NULL;
7191 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7192 int ch, did_something = 0;
7194 while ((ch = getopt(argc, argv, "")) != -1) {
7195 switch (ch) {
7196 default:
7197 usage_integrate();
7198 /* NOTREACHED */
7202 argc -= optind;
7203 argv += optind;
7205 if (argc != 1)
7206 usage_integrate();
7207 branch_arg = argv[0];
7209 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7210 "unveil", NULL) == -1)
7211 err(1, "pledge");
7213 cwd = getcwd(NULL, 0);
7214 if (cwd == NULL) {
7215 error = got_error_from_errno("getcwd");
7216 goto done;
7219 error = got_worktree_open(&worktree, cwd);
7220 if (error)
7221 goto done;
7223 error = check_rebase_or_histedit_in_progress(worktree);
7224 if (error)
7225 goto done;
7227 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7228 NULL);
7229 if (error != NULL)
7230 goto done;
7232 error = apply_unveil(got_repo_get_path(repo), 0,
7233 got_worktree_get_root_path(worktree));
7234 if (error)
7235 goto done;
7237 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7238 error = got_error_from_errno("asprintf");
7239 goto done;
7242 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7243 &base_branch_ref, worktree, refname, repo);
7244 if (error)
7245 goto done;
7247 refname = strdup(got_ref_get_name(branch_ref));
7248 if (refname == NULL) {
7249 error = got_error_from_errno("strdup");
7250 got_worktree_integrate_abort(worktree, fileindex, repo,
7251 branch_ref, base_branch_ref);
7252 goto done;
7254 base_refname = strdup(got_ref_get_name(base_branch_ref));
7255 if (base_refname == NULL) {
7256 error = got_error_from_errno("strdup");
7257 got_worktree_integrate_abort(worktree, fileindex, repo,
7258 branch_ref, base_branch_ref);
7259 goto done;
7262 error = got_ref_resolve(&commit_id, repo, branch_ref);
7263 if (error)
7264 goto done;
7266 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7267 if (error)
7268 goto done;
7270 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7271 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7272 "specified branch has already been integrated");
7273 got_worktree_integrate_abort(worktree, fileindex, repo,
7274 branch_ref, base_branch_ref);
7275 goto done;
7278 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7279 if (error) {
7280 if (error->code == GOT_ERR_ANCESTRY)
7281 error = got_error(GOT_ERR_REBASE_REQUIRED);
7282 got_worktree_integrate_abort(worktree, fileindex, repo,
7283 branch_ref, base_branch_ref);
7284 goto done;
7287 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7288 branch_ref, base_branch_ref, update_progress, &did_something,
7289 check_cancelled, NULL);
7290 if (error)
7291 goto done;
7293 printf("Integrated %s into %s\n", refname, base_refname);
7294 done:
7295 if (repo)
7296 got_repo_close(repo);
7297 if (worktree)
7298 got_worktree_close(worktree);
7299 free(cwd);
7300 free(base_commit_id);
7301 free(commit_id);
7302 free(refname);
7303 free(base_refname);
7304 return error;
7307 __dead static void
7308 usage_stage(void)
7310 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7311 "[file-path ...]\n",
7312 getprogname());
7313 exit(1);
7316 static const struct got_error *
7317 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7318 const char *path, struct got_object_id *blob_id,
7319 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7320 int dirfd, const char *de_name)
7322 const struct got_error *err = NULL;
7323 char *id_str = NULL;
7325 if (staged_status != GOT_STATUS_ADD &&
7326 staged_status != GOT_STATUS_MODIFY &&
7327 staged_status != GOT_STATUS_DELETE)
7328 return NULL;
7330 if (staged_status == GOT_STATUS_ADD ||
7331 staged_status == GOT_STATUS_MODIFY)
7332 err = got_object_id_str(&id_str, staged_blob_id);
7333 else
7334 err = got_object_id_str(&id_str, blob_id);
7335 if (err)
7336 return err;
7338 printf("%s %c %s\n", id_str, staged_status, path);
7339 free(id_str);
7340 return NULL;
7343 static const struct got_error *
7344 cmd_stage(int argc, char *argv[])
7346 const struct got_error *error = NULL;
7347 struct got_repository *repo = NULL;
7348 struct got_worktree *worktree = NULL;
7349 char *cwd = NULL;
7350 struct got_pathlist_head paths;
7351 struct got_pathlist_entry *pe;
7352 int ch, list_stage = 0, pflag = 0;
7353 FILE *patch_script_file = NULL;
7354 const char *patch_script_path = NULL;
7355 struct choose_patch_arg cpa;
7357 TAILQ_INIT(&paths);
7359 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7360 switch (ch) {
7361 case 'l':
7362 list_stage = 1;
7363 break;
7364 case 'p':
7365 pflag = 1;
7366 break;
7367 case 'F':
7368 patch_script_path = optarg;
7369 break;
7370 default:
7371 usage_stage();
7372 /* NOTREACHED */
7376 argc -= optind;
7377 argv += optind;
7379 #ifndef PROFILE
7380 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7381 "unveil", NULL) == -1)
7382 err(1, "pledge");
7383 #endif
7384 if (list_stage && (pflag || patch_script_path))
7385 errx(1, "-l option cannot be used with other options");
7386 if (patch_script_path && !pflag)
7387 errx(1, "-F option can only be used together with -p option");
7389 cwd = getcwd(NULL, 0);
7390 if (cwd == NULL) {
7391 error = got_error_from_errno("getcwd");
7392 goto done;
7395 error = got_worktree_open(&worktree, cwd);
7396 if (error)
7397 goto done;
7399 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7400 NULL);
7401 if (error != NULL)
7402 goto done;
7404 if (patch_script_path) {
7405 patch_script_file = fopen(patch_script_path, "r");
7406 if (patch_script_file == NULL) {
7407 error = got_error_from_errno2("fopen",
7408 patch_script_path);
7409 goto done;
7412 error = apply_unveil(got_repo_get_path(repo), 0,
7413 got_worktree_get_root_path(worktree));
7414 if (error)
7415 goto done;
7417 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7418 if (error)
7419 goto done;
7421 if (list_stage)
7422 error = got_worktree_status(worktree, &paths, repo,
7423 print_stage, NULL, check_cancelled, NULL);
7424 else {
7425 cpa.patch_script_file = patch_script_file;
7426 cpa.action = "stage";
7427 error = got_worktree_stage(worktree, &paths,
7428 pflag ? NULL : print_status, NULL,
7429 pflag ? choose_patch : NULL, &cpa, repo);
7431 done:
7432 if (patch_script_file && fclose(patch_script_file) == EOF &&
7433 error == NULL)
7434 error = got_error_from_errno2("fclose", patch_script_path);
7435 if (repo)
7436 got_repo_close(repo);
7437 if (worktree)
7438 got_worktree_close(worktree);
7439 TAILQ_FOREACH(pe, &paths, entry)
7440 free((char *)pe->path);
7441 got_pathlist_free(&paths);
7442 free(cwd);
7443 return error;
7446 __dead static void
7447 usage_unstage(void)
7449 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7450 "[file-path ...]\n",
7451 getprogname());
7452 exit(1);
7456 static const struct got_error *
7457 cmd_unstage(int argc, char *argv[])
7459 const struct got_error *error = NULL;
7460 struct got_repository *repo = NULL;
7461 struct got_worktree *worktree = NULL;
7462 char *cwd = NULL;
7463 struct got_pathlist_head paths;
7464 struct got_pathlist_entry *pe;
7465 int ch, did_something = 0, pflag = 0;
7466 FILE *patch_script_file = NULL;
7467 const char *patch_script_path = NULL;
7468 struct choose_patch_arg cpa;
7470 TAILQ_INIT(&paths);
7472 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7473 switch (ch) {
7474 case 'p':
7475 pflag = 1;
7476 break;
7477 case 'F':
7478 patch_script_path = optarg;
7479 break;
7480 default:
7481 usage_unstage();
7482 /* NOTREACHED */
7486 argc -= optind;
7487 argv += optind;
7489 #ifndef PROFILE
7490 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7491 "unveil", NULL) == -1)
7492 err(1, "pledge");
7493 #endif
7494 if (patch_script_path && !pflag)
7495 errx(1, "-F option can only be used together with -p option");
7497 cwd = getcwd(NULL, 0);
7498 if (cwd == NULL) {
7499 error = got_error_from_errno("getcwd");
7500 goto done;
7503 error = got_worktree_open(&worktree, cwd);
7504 if (error)
7505 goto done;
7507 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7508 NULL);
7509 if (error != NULL)
7510 goto done;
7512 if (patch_script_path) {
7513 patch_script_file = fopen(patch_script_path, "r");
7514 if (patch_script_file == NULL) {
7515 error = got_error_from_errno2("fopen",
7516 patch_script_path);
7517 goto done;
7521 error = apply_unveil(got_repo_get_path(repo), 0,
7522 got_worktree_get_root_path(worktree));
7523 if (error)
7524 goto done;
7526 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7527 if (error)
7528 goto done;
7530 cpa.patch_script_file = patch_script_file;
7531 cpa.action = "unstage";
7532 error = got_worktree_unstage(worktree, &paths, update_progress,
7533 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7534 done:
7535 if (patch_script_file && fclose(patch_script_file) == EOF &&
7536 error == NULL)
7537 error = got_error_from_errno2("fclose", patch_script_path);
7538 if (repo)
7539 got_repo_close(repo);
7540 if (worktree)
7541 got_worktree_close(worktree);
7542 TAILQ_FOREACH(pe, &paths, entry)
7543 free((char *)pe->path);
7544 got_pathlist_free(&paths);
7545 free(cwd);
7546 return error;
7549 __dead static void
7550 usage_cat(void)
7552 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7553 "arg1 [arg2 ...]\n", getprogname());
7554 exit(1);
7557 static const struct got_error *
7558 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7560 const struct got_error *err;
7561 struct got_blob_object *blob;
7563 err = got_object_open_as_blob(&blob, repo, id, 8192);
7564 if (err)
7565 return err;
7567 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7568 got_object_blob_close(blob);
7569 return err;
7572 static const struct got_error *
7573 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7575 const struct got_error *err;
7576 struct got_tree_object *tree;
7577 int nentries, i;
7579 err = got_object_open_as_tree(&tree, repo, id);
7580 if (err)
7581 return err;
7583 nentries = got_object_tree_get_nentries(tree);
7584 for (i = 0; i < nentries; i++) {
7585 struct got_tree_entry *te;
7586 char *id_str;
7587 if (sigint_received || sigpipe_received)
7588 break;
7589 te = got_object_tree_get_entry(tree, i);
7590 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7591 if (err)
7592 break;
7593 fprintf(outfile, "%s %.7o %s\n", id_str,
7594 got_tree_entry_get_mode(te),
7595 got_tree_entry_get_name(te));
7596 free(id_str);
7599 got_object_tree_close(tree);
7600 return err;
7603 static const struct got_error *
7604 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7606 const struct got_error *err;
7607 struct got_commit_object *commit;
7608 const struct got_object_id_queue *parent_ids;
7609 struct got_object_qid *pid;
7610 char *id_str = NULL;
7611 const char *logmsg = NULL;
7613 err = got_object_open_as_commit(&commit, repo, id);
7614 if (err)
7615 return err;
7617 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7618 if (err)
7619 goto done;
7621 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7622 parent_ids = got_object_commit_get_parent_ids(commit);
7623 fprintf(outfile, "numparents %d\n",
7624 got_object_commit_get_nparents(commit));
7625 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7626 char *pid_str;
7627 err = got_object_id_str(&pid_str, pid->id);
7628 if (err)
7629 goto done;
7630 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7631 free(pid_str);
7633 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7634 got_object_commit_get_author(commit),
7635 got_object_commit_get_author_time(commit));
7637 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7638 got_object_commit_get_author(commit),
7639 got_object_commit_get_committer_time(commit));
7641 logmsg = got_object_commit_get_logmsg_raw(commit);
7642 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7643 fprintf(outfile, "%s", logmsg);
7644 done:
7645 free(id_str);
7646 got_object_commit_close(commit);
7647 return err;
7650 static const struct got_error *
7651 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7653 const struct got_error *err;
7654 struct got_tag_object *tag;
7655 char *id_str = NULL;
7656 const char *tagmsg = NULL;
7658 err = got_object_open_as_tag(&tag, repo, id);
7659 if (err)
7660 return err;
7662 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7663 if (err)
7664 goto done;
7666 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7668 switch (got_object_tag_get_object_type(tag)) {
7669 case GOT_OBJ_TYPE_BLOB:
7670 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7671 GOT_OBJ_LABEL_BLOB);
7672 break;
7673 case GOT_OBJ_TYPE_TREE:
7674 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7675 GOT_OBJ_LABEL_TREE);
7676 break;
7677 case GOT_OBJ_TYPE_COMMIT:
7678 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7679 GOT_OBJ_LABEL_COMMIT);
7680 break;
7681 case GOT_OBJ_TYPE_TAG:
7682 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7683 GOT_OBJ_LABEL_TAG);
7684 break;
7685 default:
7686 break;
7689 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7690 got_object_tag_get_name(tag));
7692 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7693 got_object_tag_get_tagger(tag),
7694 got_object_tag_get_tagger_time(tag));
7696 tagmsg = got_object_tag_get_message(tag);
7697 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7698 fprintf(outfile, "%s", tagmsg);
7699 done:
7700 free(id_str);
7701 got_object_tag_close(tag);
7702 return err;
7705 static const struct got_error *
7706 cmd_cat(int argc, char *argv[])
7708 const struct got_error *error;
7709 struct got_repository *repo = NULL;
7710 struct got_worktree *worktree = NULL;
7711 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7712 const char *commit_id_str = NULL;
7713 struct got_object_id *id = NULL, *commit_id = NULL;
7714 int ch, obj_type, i, force_path = 0;
7716 #ifndef PROFILE
7717 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7718 NULL) == -1)
7719 err(1, "pledge");
7720 #endif
7722 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7723 switch (ch) {
7724 case 'c':
7725 commit_id_str = optarg;
7726 break;
7727 case 'r':
7728 repo_path = realpath(optarg, NULL);
7729 if (repo_path == NULL)
7730 return got_error_from_errno2("realpath",
7731 optarg);
7732 got_path_strip_trailing_slashes(repo_path);
7733 break;
7734 case 'P':
7735 force_path = 1;
7736 break;
7737 default:
7738 usage_cat();
7739 /* NOTREACHED */
7743 argc -= optind;
7744 argv += optind;
7746 cwd = getcwd(NULL, 0);
7747 if (cwd == NULL) {
7748 error = got_error_from_errno("getcwd");
7749 goto done;
7751 error = got_worktree_open(&worktree, cwd);
7752 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7753 goto done;
7754 if (worktree) {
7755 if (repo_path == NULL) {
7756 repo_path = strdup(
7757 got_worktree_get_repo_path(worktree));
7758 if (repo_path == NULL) {
7759 error = got_error_from_errno("strdup");
7760 goto done;
7765 if (repo_path == NULL) {
7766 repo_path = getcwd(NULL, 0);
7767 if (repo_path == NULL)
7768 return got_error_from_errno("getcwd");
7771 error = got_repo_open(&repo, repo_path, NULL);
7772 free(repo_path);
7773 if (error != NULL)
7774 goto done;
7776 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7777 if (error)
7778 goto done;
7780 if (commit_id_str == NULL)
7781 commit_id_str = GOT_REF_HEAD;
7782 error = got_repo_match_object_id(&commit_id, NULL,
7783 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7784 if (error)
7785 goto done;
7787 for (i = 0; i < argc; i++) {
7788 if (force_path) {
7789 error = got_object_id_by_path(&id, repo, commit_id,
7790 argv[i]);
7791 if (error)
7792 break;
7793 } else {
7794 error = got_repo_match_object_id(&id, &label, argv[i],
7795 GOT_OBJ_TYPE_ANY, 0, repo);
7796 if (error) {
7797 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7798 error->code != GOT_ERR_NOT_REF)
7799 break;
7800 error = got_object_id_by_path(&id, repo,
7801 commit_id, argv[i]);
7802 if (error)
7803 break;
7807 error = got_object_get_type(&obj_type, repo, id);
7808 if (error)
7809 break;
7811 switch (obj_type) {
7812 case GOT_OBJ_TYPE_BLOB:
7813 error = cat_blob(id, repo, stdout);
7814 break;
7815 case GOT_OBJ_TYPE_TREE:
7816 error = cat_tree(id, repo, stdout);
7817 break;
7818 case GOT_OBJ_TYPE_COMMIT:
7819 error = cat_commit(id, repo, stdout);
7820 break;
7821 case GOT_OBJ_TYPE_TAG:
7822 error = cat_tag(id, repo, stdout);
7823 break;
7824 default:
7825 error = got_error(GOT_ERR_OBJ_TYPE);
7826 break;
7828 if (error)
7829 break;
7830 free(label);
7831 label = NULL;
7832 free(id);
7833 id = NULL;
7835 done:
7836 free(label);
7837 free(id);
7838 free(commit_id);
7839 if (worktree)
7840 got_worktree_close(worktree);
7841 if (repo) {
7842 const struct got_error *repo_error;
7843 repo_error = got_repo_close(repo);
7844 if (error == NULL)
7845 error = repo_error;
7847 return error;