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_clone(void);
89 __dead static void usage_checkout(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 { "clone", cmd_clone, usage_clone, "cl" },
142 { "checkout", cmd_checkout, usage_checkout, "co" },
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 [-q] [-v] repository-url [target-directory]\n",
812 getprogname());
813 exit(1);
816 struct got_fetch_progress_arg {
817 char last_scaled_size[FMT_SCALED_STRSIZE];
818 int last_p_indexed;
819 int last_p_resolved;
820 int verbosity;
821 };
823 static const struct got_error *
824 fetch_progress(void *arg, const char *message, off_t packfile_size,
825 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
827 struct got_fetch_progress_arg *a = arg;
828 char scaled_size[FMT_SCALED_STRSIZE];
829 int p_indexed, p_resolved;
830 int print_size = 0, print_indexed = 0, print_resolved = 0;
832 if (a->verbosity < 0)
833 return NULL;
835 if (message && message[0] != '\0') {
836 printf("\rserver: %s", message);
837 fflush(stdout);
840 if (packfile_size > 0 || nobj_indexed > 0) {
841 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
842 (a->last_scaled_size[0] == '\0' ||
843 strcmp(scaled_size, a->last_scaled_size)) != 0) {
844 print_size = 1;
845 if (strlcpy(a->last_scaled_size, scaled_size,
846 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
847 return got_error(GOT_ERR_NO_SPACE);
849 if (nobj_indexed > 0) {
850 p_indexed = (nobj_indexed * 100) / nobj_total;
851 if (p_indexed != a->last_p_indexed) {
852 a->last_p_indexed = p_indexed;
853 print_indexed = 1;
854 print_size = 1;
857 if (nobj_resolved > 0) {
858 p_resolved = (nobj_resolved * 100) /
859 (nobj_total - nobj_loose);
860 if (p_resolved != a->last_p_resolved) {
861 a->last_p_resolved = p_resolved;
862 print_resolved = 1;
863 print_indexed = 1;
864 print_size = 1;
869 if (print_size || print_indexed || print_resolved)
870 printf("\r");
871 if (print_size)
872 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
873 if (print_indexed) {
874 printf("; indexing %d%%", p_indexed);
875 if (a->verbosity > 0)
876 printf(" (%d/%d)", nobj_indexed, nobj_total);
878 if (print_resolved) {
879 printf("; resolving deltas %d%%", p_resolved);
880 if (a->verbosity > 0)
881 printf(" (%d/%d)", nobj_resolved,
882 nobj_total - nobj_loose);
884 if (print_size || print_indexed || print_resolved)
885 fflush(stdout);
887 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
888 nobj_resolved == nobj_total - nobj_loose)
889 printf("\nWriting pack index...\n");
891 return NULL;
894 static const struct got_error *
895 cmd_clone(int argc, char *argv[])
897 const struct got_error *error = NULL;
898 const char *uri, *dirname;
899 char *proto, *host, *port, *repo_name, *server_path;
900 char *default_destdir = NULL, *id_str = NULL;
901 const char *repo_path;
902 struct got_repository *repo = NULL;
903 struct got_pathlist_head refs, symrefs;
904 struct got_pathlist_entry *pe;
905 struct got_object_id *pack_hash = NULL;
906 int ch, fetchfd = -1;
907 struct got_fetch_progress_arg fpa;
908 char *git_url = NULL;
909 char *gitconfig_path = NULL;
910 char *gitconfig = NULL;
911 FILE *gitconfig_file = NULL;
912 ssize_t n;
913 int verbosity = 0;
915 TAILQ_INIT(&refs);
916 TAILQ_INIT(&symrefs);
918 while ((ch = getopt(argc, argv, "vq")) != -1) {
919 switch (ch) {
920 case 'v':
921 if (verbosity < 0)
922 verbosity = 0;
923 else if (verbosity < 3)
924 verbosity++;
925 break;
926 case 'q':
927 verbosity = -1;
928 break;
929 default:
930 usage_clone();
931 break;
934 argc -= optind;
935 argv += optind;
937 uri = argv[0];
939 if (argc == 1)
940 dirname = NULL;
941 else if (argc == 2)
942 dirname = argv[1];
943 else
944 usage_clone();
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
948 if (error)
949 goto done;
951 if (strcmp(proto, "git") == 0) {
952 #ifndef PROFILE
953 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
954 "sendfd dns inet unveil", NULL) == -1)
955 err(1, "pledge");
956 #endif
957 git_url = strdup(argv[0]);
958 if (git_url == NULL) {
959 error = got_error_from_errno("strdup");
960 goto done;
962 } else if (strcmp(proto, "git+ssh") == 0 ||
963 strcmp(proto, "ssh") == 0) {
964 #ifndef PROFILE
965 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
966 "sendfd unveil", NULL) == -1)
967 err(1, "pledge");
968 #endif
969 if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
970 error = got_error_from_errno("asprintf");
971 goto done;
973 } else if (strcmp(proto, "http") == 0 ||
974 strcmp(proto, "git+http") == 0) {
975 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
976 goto done;
977 } else {
978 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
979 goto done;
981 if (dirname == NULL) {
982 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
983 error = got_error_from_errno("asprintf");
984 goto done;
986 repo_path = default_destdir;
987 } else
988 repo_path = dirname;
990 error = got_path_mkdir(repo_path);
991 if (error)
992 goto done;
994 error = got_repo_init(repo_path);
995 if (error)
996 goto done;
998 error = got_repo_open(&repo, repo_path, NULL);
999 if (error)
1000 goto done;
1002 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1003 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1004 error = got_error_from_errno2("unveil",
1005 GOT_FETCH_PATH_SSH);
1006 goto done;
1009 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1010 if (error)
1011 goto done;
1013 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1014 verbosity);
1015 if (error)
1016 goto done;
1018 if (verbosity >= 0)
1019 printf("Connected to %s:%s\n", host, port);
1021 fpa.last_scaled_size[0] = '\0';
1022 fpa.last_p_indexed = -1;
1023 fpa.last_p_resolved = -1;
1024 fpa.verbosity = verbosity;
1025 error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1026 repo, fetch_progress, &fpa);
1027 if (error)
1028 goto done;
1030 error = got_object_id_str(&id_str, pack_hash);
1031 if (error)
1032 goto done;
1033 if (verbosity >= 0)
1034 printf("Fetched %s.pack\n", id_str);
1035 free(id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1045 if (error)
1046 goto done;
1047 error = got_ref_write(ref, repo);
1048 got_ref_close(ref);
1049 if (error)
1050 goto done;
1052 if (strncmp("refs/heads/", refname, 11) != 0)
1053 continue;
1055 if (asprintf(&remote_refname,
1056 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1057 refname + 11) == -1) {
1058 error = got_error_from_errno("asprintf");
1059 goto done;
1061 error = got_ref_alloc(&ref, remote_refname, id);
1062 if (error)
1063 goto done;
1064 error = got_ref_write(ref, repo);
1065 got_ref_close(ref);
1066 if (error)
1067 goto done;
1070 /* Set the HEAD reference if the server provided one. */
1071 TAILQ_FOREACH(pe, &symrefs, entry) {
1072 struct got_reference *symref, *target_ref;
1073 const char *refname = pe->path;
1074 const char *target = pe->data;
1076 if (strcmp(refname, GOT_REF_HEAD) != 0)
1077 continue;
1079 error = got_ref_open(&target_ref, repo, target, 0);
1080 if (error) {
1081 if (error->code == GOT_ERR_NOT_REF)
1082 continue;
1083 goto done;
1086 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1087 got_ref_close(target_ref);
1088 if (error)
1089 goto done;
1091 if (verbosity > 1) {
1092 printf("Setting %s to %s\n", GOT_REF_HEAD,
1093 got_ref_get_symref_target(symref));
1095 if (verbosity >= 0)
1096 printf("Created cloned repository '%s'\n", repo_path);
1098 error = got_ref_write(symref, repo);
1099 got_ref_close(symref);
1100 break;
1103 /* Create a config file so Git can understand this repository. */
1104 gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 if (gitconfig_path == NULL) {
1106 error = got_error_from_errno("got_repo_get_path_gitconfig");
1107 goto done;
1109 gitconfig_file = fopen(gitconfig_path, "w");
1110 if (gitconfig_file == NULL) {
1111 error = got_error_from_errno2("fopen", gitconfig_path);
1112 goto done;
1114 if (asprintf(&gitconfig,
1115 "[core]\n"
1116 "\trepositoryformatversion = 0\n"
1117 "\tbare = true\n"
1118 "[remote \"%s\"]\n"
1119 "\turl = %s\n"
1120 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1121 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1122 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1123 error = got_error_from_errno("asprintf");
1124 goto done;
1126 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1127 if (n != strlen(gitconfig))
1128 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1129 done:
1130 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1131 error = got_error_from_errno("close");
1132 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1133 error = got_error_from_errno("fclose");
1134 if (repo)
1135 got_repo_close(repo);
1136 TAILQ_FOREACH(pe, &refs, entry) {
1137 free((void *)pe->path);
1138 free(pe->data);
1140 got_pathlist_free(&refs);
1141 TAILQ_FOREACH(pe, &symrefs, entry) {
1142 free((void *)pe->path);
1143 free(pe->data);
1145 got_pathlist_free(&symrefs);
1146 free(pack_hash);
1147 free(proto);
1148 free(host);
1149 free(port);
1150 free(server_path);
1151 free(repo_name);
1152 free(default_destdir);
1153 free(gitconfig_path);
1154 free(git_url);
1155 return error;
1158 __dead static void
1159 usage_checkout(void)
1161 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1162 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1163 exit(1);
1166 static void
1167 show_worktree_base_ref_warning(void)
1169 fprintf(stderr, "%s: warning: could not create a reference "
1170 "to the work tree's base commit; the commit could be "
1171 "garbage-collected by Git; making the repository "
1172 "writable and running 'got update' will prevent this\n",
1173 getprogname());
1176 struct got_checkout_progress_arg {
1177 const char *worktree_path;
1178 int had_base_commit_ref_error;
1181 static const struct got_error *
1182 checkout_progress(void *arg, unsigned char status, const char *path)
1184 struct got_checkout_progress_arg *a = arg;
1186 /* Base commit bump happens silently. */
1187 if (status == GOT_STATUS_BUMP_BASE)
1188 return NULL;
1190 if (status == GOT_STATUS_BASE_REF_ERR) {
1191 a->had_base_commit_ref_error = 1;
1192 return NULL;
1195 while (path[0] == '/')
1196 path++;
1198 printf("%c %s/%s\n", status, a->worktree_path, path);
1199 return NULL;
1202 static const struct got_error *
1203 check_cancelled(void *arg)
1205 if (sigint_received || sigpipe_received)
1206 return got_error(GOT_ERR_CANCELLED);
1207 return NULL;
1210 static const struct got_error *
1211 check_linear_ancestry(struct got_object_id *commit_id,
1212 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1213 struct got_repository *repo)
1215 const struct got_error *err = NULL;
1216 struct got_object_id *yca_id;
1218 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1219 commit_id, base_commit_id, repo, check_cancelled, NULL);
1220 if (err)
1221 return err;
1223 if (yca_id == NULL)
1224 return got_error(GOT_ERR_ANCESTRY);
1227 * Require a straight line of history between the target commit
1228 * and the work tree's base commit.
1230 * Non-linear situations such as this require a rebase:
1232 * (commit) D F (base_commit)
1233 * \ /
1234 * C E
1235 * \ /
1236 * B (yca)
1237 * |
1238 * A
1240 * 'got update' only handles linear cases:
1241 * Update forwards in time: A (base/yca) - B - C - D (commit)
1242 * Update backwards in time: D (base) - C - B - A (commit/yca)
1244 if (allow_forwards_in_time_only) {
1245 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1246 return got_error(GOT_ERR_ANCESTRY);
1247 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1248 got_object_id_cmp(base_commit_id, yca_id) != 0)
1249 return got_error(GOT_ERR_ANCESTRY);
1251 free(yca_id);
1252 return NULL;
1255 static const struct got_error *
1256 check_same_branch(struct got_object_id *commit_id,
1257 struct got_reference *head_ref, struct got_object_id *yca_id,
1258 struct got_repository *repo)
1260 const struct got_error *err = NULL;
1261 struct got_commit_graph *graph = NULL;
1262 struct got_object_id *head_commit_id = NULL;
1263 int is_same_branch = 0;
1265 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1266 if (err)
1267 goto done;
1269 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1270 is_same_branch = 1;
1271 goto done;
1273 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1274 is_same_branch = 1;
1275 goto done;
1278 err = got_commit_graph_open(&graph, "/", 1);
1279 if (err)
1280 goto done;
1282 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1283 check_cancelled, NULL);
1284 if (err)
1285 goto done;
1287 for (;;) {
1288 struct got_object_id *id;
1289 err = got_commit_graph_iter_next(&id, graph, repo,
1290 check_cancelled, NULL);
1291 if (err) {
1292 if (err->code == GOT_ERR_ITER_COMPLETED)
1293 err = NULL;
1294 break;
1297 if (id) {
1298 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1299 break;
1300 if (got_object_id_cmp(id, commit_id) == 0) {
1301 is_same_branch = 1;
1302 break;
1306 done:
1307 if (graph)
1308 got_commit_graph_close(graph);
1309 free(head_commit_id);
1310 if (!err && !is_same_branch)
1311 err = got_error(GOT_ERR_ANCESTRY);
1312 return err;
1315 static const struct got_error *
1316 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1318 static char msg[512];
1319 const char *branch_name;
1321 if (got_ref_is_symbolic(ref))
1322 branch_name = got_ref_get_symref_target(ref);
1323 else
1324 branch_name = got_ref_get_name(ref);
1326 if (strncmp("refs/heads/", branch_name, 11) == 0)
1327 branch_name += 11;
1329 snprintf(msg, sizeof(msg),
1330 "target commit is not contained in branch '%s'; "
1331 "the branch to use must be specified with -b; "
1332 "if necessary a new branch can be created for "
1333 "this commit with 'got branch -c %s BRANCH_NAME'",
1334 branch_name, commit_id_str);
1336 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1339 static const struct got_error *
1340 cmd_checkout(int argc, char *argv[])
1342 const struct got_error *error = NULL;
1343 struct got_repository *repo = NULL;
1344 struct got_reference *head_ref = NULL;
1345 struct got_worktree *worktree = NULL;
1346 char *repo_path = NULL;
1347 char *worktree_path = NULL;
1348 const char *path_prefix = "";
1349 const char *branch_name = GOT_REF_HEAD;
1350 char *commit_id_str = NULL;
1351 int ch, same_path_prefix, allow_nonempty = 0;
1352 struct got_pathlist_head paths;
1353 struct got_checkout_progress_arg cpa;
1355 TAILQ_INIT(&paths);
1357 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1358 switch (ch) {
1359 case 'b':
1360 branch_name = optarg;
1361 break;
1362 case 'c':
1363 commit_id_str = strdup(optarg);
1364 if (commit_id_str == NULL)
1365 return got_error_from_errno("strdup");
1366 break;
1367 case 'E':
1368 allow_nonempty = 1;
1369 break;
1370 case 'p':
1371 path_prefix = optarg;
1372 break;
1373 default:
1374 usage_checkout();
1375 /* NOTREACHED */
1379 argc -= optind;
1380 argv += optind;
1382 #ifndef PROFILE
1383 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1384 "unveil", NULL) == -1)
1385 err(1, "pledge");
1386 #endif
1387 if (argc == 1) {
1388 char *cwd, *base, *dotgit;
1389 repo_path = realpath(argv[0], NULL);
1390 if (repo_path == NULL)
1391 return got_error_from_errno2("realpath", argv[0]);
1392 cwd = getcwd(NULL, 0);
1393 if (cwd == NULL) {
1394 error = got_error_from_errno("getcwd");
1395 goto done;
1397 if (path_prefix[0]) {
1398 base = basename(path_prefix);
1399 if (base == NULL) {
1400 error = got_error_from_errno2("basename",
1401 path_prefix);
1402 goto done;
1404 } else {
1405 base = basename(repo_path);
1406 if (base == NULL) {
1407 error = got_error_from_errno2("basename",
1408 repo_path);
1409 goto done;
1412 dotgit = strstr(base, ".git");
1413 if (dotgit)
1414 *dotgit = '\0';
1415 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1416 error = got_error_from_errno("asprintf");
1417 free(cwd);
1418 goto done;
1420 free(cwd);
1421 } else if (argc == 2) {
1422 repo_path = realpath(argv[0], NULL);
1423 if (repo_path == NULL) {
1424 error = got_error_from_errno2("realpath", argv[0]);
1425 goto done;
1427 worktree_path = realpath(argv[1], NULL);
1428 if (worktree_path == NULL) {
1429 if (errno != ENOENT) {
1430 error = got_error_from_errno2("realpath",
1431 argv[1]);
1432 goto done;
1434 worktree_path = strdup(argv[1]);
1435 if (worktree_path == NULL) {
1436 error = got_error_from_errno("strdup");
1437 goto done;
1440 } else
1441 usage_checkout();
1443 got_path_strip_trailing_slashes(repo_path);
1444 got_path_strip_trailing_slashes(worktree_path);
1446 error = got_repo_open(&repo, repo_path, NULL);
1447 if (error != NULL)
1448 goto done;
1450 /* Pre-create work tree path for unveil(2) */
1451 error = got_path_mkdir(worktree_path);
1452 if (error) {
1453 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1454 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1455 goto done;
1456 if (!allow_nonempty &&
1457 !got_path_dir_is_empty(worktree_path)) {
1458 error = got_error_path(worktree_path,
1459 GOT_ERR_DIR_NOT_EMPTY);
1460 goto done;
1464 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1465 if (error)
1466 goto done;
1468 error = got_ref_open(&head_ref, repo, branch_name, 0);
1469 if (error != NULL)
1470 goto done;
1472 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1473 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1474 goto done;
1476 error = got_worktree_open(&worktree, worktree_path);
1477 if (error != NULL)
1478 goto done;
1480 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1481 path_prefix);
1482 if (error != NULL)
1483 goto done;
1484 if (!same_path_prefix) {
1485 error = got_error(GOT_ERR_PATH_PREFIX);
1486 goto done;
1489 if (commit_id_str) {
1490 struct got_object_id *commit_id;
1491 error = got_repo_match_object_id(&commit_id, NULL,
1492 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1493 if (error)
1494 goto done;
1495 error = check_linear_ancestry(commit_id,
1496 got_worktree_get_base_commit_id(worktree), 0, repo);
1497 if (error != NULL) {
1498 free(commit_id);
1499 if (error->code == GOT_ERR_ANCESTRY) {
1500 error = checkout_ancestry_error(
1501 head_ref, commit_id_str);
1503 goto done;
1505 error = check_same_branch(commit_id, head_ref, NULL, repo);
1506 if (error) {
1507 if (error->code == GOT_ERR_ANCESTRY) {
1508 error = checkout_ancestry_error(
1509 head_ref, commit_id_str);
1511 goto done;
1513 error = got_worktree_set_base_commit_id(worktree, repo,
1514 commit_id);
1515 free(commit_id);
1516 if (error)
1517 goto done;
1520 error = got_pathlist_append(&paths, "", NULL);
1521 if (error)
1522 goto done;
1523 cpa.worktree_path = worktree_path;
1524 cpa.had_base_commit_ref_error = 0;
1525 error = got_worktree_checkout_files(worktree, &paths, repo,
1526 checkout_progress, &cpa, check_cancelled, NULL);
1527 if (error != NULL)
1528 goto done;
1530 printf("Now shut up and hack\n");
1531 if (cpa.had_base_commit_ref_error)
1532 show_worktree_base_ref_warning();
1533 done:
1534 got_pathlist_free(&paths);
1535 free(commit_id_str);
1536 free(repo_path);
1537 free(worktree_path);
1538 return error;
1541 __dead static void
1542 usage_update(void)
1544 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1545 getprogname());
1546 exit(1);
1549 static const struct got_error *
1550 update_progress(void *arg, unsigned char status, const char *path)
1552 int *did_something = arg;
1554 if (status == GOT_STATUS_EXISTS ||
1555 status == GOT_STATUS_BASE_REF_ERR)
1556 return NULL;
1558 *did_something = 1;
1560 /* Base commit bump happens silently. */
1561 if (status == GOT_STATUS_BUMP_BASE)
1562 return NULL;
1564 while (path[0] == '/')
1565 path++;
1566 printf("%c %s\n", status, path);
1567 return NULL;
1570 static const struct got_error *
1571 switch_head_ref(struct got_reference *head_ref,
1572 struct got_object_id *commit_id, struct got_worktree *worktree,
1573 struct got_repository *repo)
1575 const struct got_error *err = NULL;
1576 char *base_id_str;
1577 int ref_has_moved = 0;
1579 /* Trivial case: switching between two different references. */
1580 if (strcmp(got_ref_get_name(head_ref),
1581 got_worktree_get_head_ref_name(worktree)) != 0) {
1582 printf("Switching work tree from %s to %s\n",
1583 got_worktree_get_head_ref_name(worktree),
1584 got_ref_get_name(head_ref));
1585 return got_worktree_set_head_ref(worktree, head_ref);
1588 err = check_linear_ancestry(commit_id,
1589 got_worktree_get_base_commit_id(worktree), 0, repo);
1590 if (err) {
1591 if (err->code != GOT_ERR_ANCESTRY)
1592 return err;
1593 ref_has_moved = 1;
1595 if (!ref_has_moved)
1596 return NULL;
1598 /* Switching to a rebased branch with the same reference name. */
1599 err = got_object_id_str(&base_id_str,
1600 got_worktree_get_base_commit_id(worktree));
1601 if (err)
1602 return err;
1603 printf("Reference %s now points at a different branch\n",
1604 got_worktree_get_head_ref_name(worktree));
1605 printf("Switching work tree from %s to %s\n", base_id_str,
1606 got_worktree_get_head_ref_name(worktree));
1607 return NULL;
1610 static const struct got_error *
1611 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1613 const struct got_error *err;
1614 int in_progress;
1616 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1617 if (err)
1618 return err;
1619 if (in_progress)
1620 return got_error(GOT_ERR_REBASING);
1622 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1623 if (err)
1624 return err;
1625 if (in_progress)
1626 return got_error(GOT_ERR_HISTEDIT_BUSY);
1628 return NULL;
1631 static const struct got_error *
1632 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1633 char *argv[], struct got_worktree *worktree)
1635 const struct got_error *err = NULL;
1636 char *path;
1637 int i;
1639 if (argc == 0) {
1640 path = strdup("");
1641 if (path == NULL)
1642 return got_error_from_errno("strdup");
1643 return got_pathlist_append(paths, path, NULL);
1646 for (i = 0; i < argc; i++) {
1647 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1648 if (err)
1649 break;
1650 err = got_pathlist_append(paths, path, NULL);
1651 if (err) {
1652 free(path);
1653 break;
1657 return err;
1660 static const struct got_error *
1661 cmd_update(int argc, char *argv[])
1663 const struct got_error *error = NULL;
1664 struct got_repository *repo = NULL;
1665 struct got_worktree *worktree = NULL;
1666 char *worktree_path = NULL;
1667 struct got_object_id *commit_id = NULL;
1668 char *commit_id_str = NULL;
1669 const char *branch_name = NULL;
1670 struct got_reference *head_ref = NULL;
1671 struct got_pathlist_head paths;
1672 struct got_pathlist_entry *pe;
1673 int ch, did_something = 0;
1675 TAILQ_INIT(&paths);
1677 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1678 switch (ch) {
1679 case 'b':
1680 branch_name = optarg;
1681 break;
1682 case 'c':
1683 commit_id_str = strdup(optarg);
1684 if (commit_id_str == NULL)
1685 return got_error_from_errno("strdup");
1686 break;
1687 default:
1688 usage_update();
1689 /* NOTREACHED */
1693 argc -= optind;
1694 argv += optind;
1696 #ifndef PROFILE
1697 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1698 "unveil", NULL) == -1)
1699 err(1, "pledge");
1700 #endif
1701 worktree_path = getcwd(NULL, 0);
1702 if (worktree_path == NULL) {
1703 error = got_error_from_errno("getcwd");
1704 goto done;
1706 error = got_worktree_open(&worktree, worktree_path);
1707 if (error)
1708 goto done;
1710 error = check_rebase_or_histedit_in_progress(worktree);
1711 if (error)
1712 goto done;
1714 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1715 NULL);
1716 if (error != NULL)
1717 goto done;
1719 error = apply_unveil(got_repo_get_path(repo), 0,
1720 got_worktree_get_root_path(worktree));
1721 if (error)
1722 goto done;
1724 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1725 if (error)
1726 goto done;
1728 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1729 got_worktree_get_head_ref_name(worktree), 0);
1730 if (error != NULL)
1731 goto done;
1732 if (commit_id_str == NULL) {
1733 error = got_ref_resolve(&commit_id, repo, head_ref);
1734 if (error != NULL)
1735 goto done;
1736 error = got_object_id_str(&commit_id_str, commit_id);
1737 if (error != NULL)
1738 goto done;
1739 } else {
1740 error = got_repo_match_object_id(&commit_id, NULL,
1741 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1742 free(commit_id_str);
1743 commit_id_str = NULL;
1744 if (error)
1745 goto done;
1746 error = got_object_id_str(&commit_id_str, commit_id);
1747 if (error)
1748 goto done;
1751 if (branch_name) {
1752 struct got_object_id *head_commit_id;
1753 TAILQ_FOREACH(pe, &paths, entry) {
1754 if (pe->path_len == 0)
1755 continue;
1756 error = got_error_msg(GOT_ERR_BAD_PATH,
1757 "switching between branches requires that "
1758 "the entire work tree gets updated");
1759 goto done;
1761 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1762 if (error)
1763 goto done;
1764 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1765 repo);
1766 free(head_commit_id);
1767 if (error != NULL)
1768 goto done;
1769 error = check_same_branch(commit_id, head_ref, NULL, repo);
1770 if (error)
1771 goto done;
1772 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1773 if (error)
1774 goto done;
1775 } else {
1776 error = check_linear_ancestry(commit_id,
1777 got_worktree_get_base_commit_id(worktree), 0, repo);
1778 if (error != NULL) {
1779 if (error->code == GOT_ERR_ANCESTRY)
1780 error = got_error(GOT_ERR_BRANCH_MOVED);
1781 goto done;
1783 error = check_same_branch(commit_id, head_ref, NULL, repo);
1784 if (error)
1785 goto done;
1788 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1789 commit_id) != 0) {
1790 error = got_worktree_set_base_commit_id(worktree, repo,
1791 commit_id);
1792 if (error)
1793 goto done;
1796 error = got_worktree_checkout_files(worktree, &paths, repo,
1797 update_progress, &did_something, check_cancelled, NULL);
1798 if (error != NULL)
1799 goto done;
1801 if (did_something)
1802 printf("Updated to commit %s\n", commit_id_str);
1803 else
1804 printf("Already up-to-date\n");
1805 done:
1806 free(worktree_path);
1807 TAILQ_FOREACH(pe, &paths, entry)
1808 free((char *)pe->path);
1809 got_pathlist_free(&paths);
1810 free(commit_id);
1811 free(commit_id_str);
1812 return error;
1815 static const struct got_error *
1816 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1817 const char *path, int diff_context, int ignore_whitespace,
1818 struct got_repository *repo)
1820 const struct got_error *err = NULL;
1821 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1823 if (blob_id1) {
1824 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1825 if (err)
1826 goto done;
1829 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1830 if (err)
1831 goto done;
1833 while (path[0] == '/')
1834 path++;
1835 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1836 ignore_whitespace, stdout);
1837 done:
1838 if (blob1)
1839 got_object_blob_close(blob1);
1840 got_object_blob_close(blob2);
1841 return err;
1844 static const struct got_error *
1845 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1846 const char *path, int diff_context, int ignore_whitespace,
1847 struct got_repository *repo)
1849 const struct got_error *err = NULL;
1850 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 struct got_diff_blob_output_unidiff_arg arg;
1853 if (tree_id1) {
1854 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1855 if (err)
1856 goto done;
1859 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1860 if (err)
1861 goto done;
1863 arg.diff_context = diff_context;
1864 arg.ignore_whitespace = ignore_whitespace;
1865 arg.outfile = stdout;
1866 while (path[0] == '/')
1867 path++;
1868 err = got_diff_tree(tree1, tree2, path, path, repo,
1869 got_diff_blob_output_unidiff, &arg, 1);
1870 done:
1871 if (tree1)
1872 got_object_tree_close(tree1);
1873 if (tree2)
1874 got_object_tree_close(tree2);
1875 return err;
1878 static const struct got_error *
1879 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1880 const char *path, int diff_context, struct got_repository *repo)
1882 const struct got_error *err = NULL;
1883 struct got_commit_object *pcommit = NULL;
1884 char *id_str1 = NULL, *id_str2 = NULL;
1885 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1886 struct got_object_qid *qid;
1888 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1889 if (qid != NULL) {
1890 err = got_object_open_as_commit(&pcommit, repo,
1891 qid->id);
1892 if (err)
1893 return err;
1896 if (path && path[0] != '\0') {
1897 int obj_type;
1898 err = got_object_id_by_path(&obj_id2, repo, id, path);
1899 if (err)
1900 goto done;
1901 err = got_object_id_str(&id_str2, obj_id2);
1902 if (err) {
1903 free(obj_id2);
1904 goto done;
1906 if (pcommit) {
1907 err = got_object_id_by_path(&obj_id1, repo,
1908 qid->id, path);
1909 if (err) {
1910 free(obj_id2);
1911 goto done;
1913 err = got_object_id_str(&id_str1, obj_id1);
1914 if (err) {
1915 free(obj_id2);
1916 goto done;
1919 err = got_object_get_type(&obj_type, repo, obj_id2);
1920 if (err) {
1921 free(obj_id2);
1922 goto done;
1924 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1925 switch (obj_type) {
1926 case GOT_OBJ_TYPE_BLOB:
1927 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1928 0, repo);
1929 break;
1930 case GOT_OBJ_TYPE_TREE:
1931 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1932 0, repo);
1933 break;
1934 default:
1935 err = got_error(GOT_ERR_OBJ_TYPE);
1936 break;
1938 free(obj_id1);
1939 free(obj_id2);
1940 } else {
1941 obj_id2 = got_object_commit_get_tree_id(commit);
1942 err = got_object_id_str(&id_str2, obj_id2);
1943 if (err)
1944 goto done;
1945 obj_id1 = got_object_commit_get_tree_id(pcommit);
1946 err = got_object_id_str(&id_str1, obj_id1);
1947 if (err)
1948 goto done;
1949 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1950 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1952 done:
1953 free(id_str1);
1954 free(id_str2);
1955 if (pcommit)
1956 got_object_commit_close(pcommit);
1957 return err;
1960 static char *
1961 get_datestr(time_t *time, char *datebuf)
1963 struct tm mytm, *tm;
1964 char *p, *s;
1966 tm = gmtime_r(time, &mytm);
1967 if (tm == NULL)
1968 return NULL;
1969 s = asctime_r(tm, datebuf);
1970 if (s == NULL)
1971 return NULL;
1972 p = strchr(s, '\n');
1973 if (p)
1974 *p = '\0';
1975 return s;
1978 static const struct got_error *
1979 match_logmsg(int *have_match, struct got_object_id *id,
1980 struct got_commit_object *commit, regex_t *regex)
1982 const struct got_error *err = NULL;
1983 regmatch_t regmatch;
1984 char *id_str = NULL, *logmsg = NULL;
1986 *have_match = 0;
1988 err = got_object_id_str(&id_str, id);
1989 if (err)
1990 return err;
1992 err = got_object_commit_get_logmsg(&logmsg, commit);
1993 if (err)
1994 goto done;
1996 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1997 *have_match = 1;
1998 done:
1999 free(id_str);
2000 free(logmsg);
2001 return err;
2004 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2006 static const struct got_error *
2007 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2008 struct got_repository *repo, const char *path, int show_patch,
2009 int diff_context, struct got_reflist_head *refs)
2011 const struct got_error *err = NULL;
2012 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2013 char datebuf[26];
2014 time_t committer_time;
2015 const char *author, *committer;
2016 char *refs_str = NULL;
2017 struct got_reflist_entry *re;
2019 SIMPLEQ_FOREACH(re, refs, entry) {
2020 char *s;
2021 const char *name;
2022 struct got_tag_object *tag = NULL;
2023 int cmp;
2025 name = got_ref_get_name(re->ref);
2026 if (strcmp(name, GOT_REF_HEAD) == 0)
2027 continue;
2028 if (strncmp(name, "refs/", 5) == 0)
2029 name += 5;
2030 if (strncmp(name, "got/", 4) == 0)
2031 continue;
2032 if (strncmp(name, "heads/", 6) == 0)
2033 name += 6;
2034 if (strncmp(name, "remotes/", 8) == 0)
2035 name += 8;
2036 if (strncmp(name, "tags/", 5) == 0) {
2037 err = got_object_open_as_tag(&tag, repo, re->id);
2038 if (err) {
2039 if (err->code != GOT_ERR_OBJ_TYPE)
2040 return err;
2041 /* Ref points at something other than a tag. */
2042 err = NULL;
2043 tag = NULL;
2046 cmp = got_object_id_cmp(tag ?
2047 got_object_tag_get_object_id(tag) : re->id, id);
2048 if (tag)
2049 got_object_tag_close(tag);
2050 if (cmp != 0)
2051 continue;
2052 s = refs_str;
2053 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2054 name) == -1) {
2055 err = got_error_from_errno("asprintf");
2056 free(s);
2057 return err;
2059 free(s);
2061 err = got_object_id_str(&id_str, id);
2062 if (err)
2063 return err;
2065 printf(GOT_COMMIT_SEP_STR);
2066 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2067 refs_str ? refs_str : "", refs_str ? ")" : "");
2068 free(id_str);
2069 id_str = NULL;
2070 free(refs_str);
2071 refs_str = NULL;
2072 printf("from: %s\n", got_object_commit_get_author(commit));
2073 committer_time = got_object_commit_get_committer_time(commit);
2074 datestr = get_datestr(&committer_time, datebuf);
2075 if (datestr)
2076 printf("date: %s UTC\n", datestr);
2077 author = got_object_commit_get_author(commit);
2078 committer = got_object_commit_get_committer(commit);
2079 if (strcmp(author, committer) != 0)
2080 printf("via: %s\n", committer);
2081 if (got_object_commit_get_nparents(commit) > 1) {
2082 const struct got_object_id_queue *parent_ids;
2083 struct got_object_qid *qid;
2084 int n = 1;
2085 parent_ids = got_object_commit_get_parent_ids(commit);
2086 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2087 err = got_object_id_str(&id_str, qid->id);
2088 if (err)
2089 return err;
2090 printf("parent %d: %s\n", n++, id_str);
2091 free(id_str);
2095 err = got_object_commit_get_logmsg(&logmsg0, commit);
2096 if (err)
2097 return err;
2099 logmsg = logmsg0;
2100 do {
2101 line = strsep(&logmsg, "\n");
2102 if (line)
2103 printf(" %s\n", line);
2104 } while (line);
2105 free(logmsg0);
2107 if (show_patch) {
2108 err = print_patch(commit, id, path, diff_context, repo);
2109 if (err == 0)
2110 printf("\n");
2113 if (fflush(stdout) != 0 && err == NULL)
2114 err = got_error_from_errno("fflush");
2115 return err;
2118 static const struct got_error *
2119 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2120 const char *path, int show_patch, const char *search_pattern,
2121 int diff_context, int limit, int log_branches,
2122 struct got_reflist_head *refs)
2124 const struct got_error *err;
2125 struct got_commit_graph *graph;
2126 regex_t regex;
2127 int have_match;
2129 if (search_pattern &&
2130 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2131 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2133 err = got_commit_graph_open(&graph, path, !log_branches);
2134 if (err)
2135 return err;
2136 err = got_commit_graph_iter_start(graph, root_id, repo,
2137 check_cancelled, NULL);
2138 if (err)
2139 goto done;
2140 for (;;) {
2141 struct got_commit_object *commit;
2142 struct got_object_id *id;
2144 if (sigint_received || sigpipe_received)
2145 break;
2147 err = got_commit_graph_iter_next(&id, graph, repo,
2148 check_cancelled, NULL);
2149 if (err) {
2150 if (err->code == GOT_ERR_ITER_COMPLETED)
2151 err = NULL;
2152 break;
2154 if (id == NULL)
2155 break;
2157 err = got_object_open_as_commit(&commit, repo, id);
2158 if (err)
2159 break;
2161 if (search_pattern) {
2162 err = match_logmsg(&have_match, id, commit, &regex);
2163 if (err) {
2164 got_object_commit_close(commit);
2165 break;
2167 if (have_match == 0) {
2168 got_object_commit_close(commit);
2169 continue;
2173 err = print_commit(commit, id, repo, path, show_patch,
2174 diff_context, refs);
2175 got_object_commit_close(commit);
2176 if (err || (limit && --limit == 0))
2177 break;
2179 done:
2180 if (search_pattern)
2181 regfree(&regex);
2182 got_commit_graph_close(graph);
2183 return err;
2186 __dead static void
2187 usage_log(void)
2189 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2190 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2191 exit(1);
2194 static int
2195 get_default_log_limit(void)
2197 const char *got_default_log_limit;
2198 long long n;
2199 const char *errstr;
2201 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2202 if (got_default_log_limit == NULL)
2203 return 0;
2204 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2205 if (errstr != NULL)
2206 return 0;
2207 return n;
2210 static const struct got_error *
2211 cmd_log(int argc, char *argv[])
2213 const struct got_error *error;
2214 struct got_repository *repo = NULL;
2215 struct got_worktree *worktree = NULL;
2216 struct got_commit_object *commit = NULL;
2217 struct got_object_id *id = NULL;
2218 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2219 const char *start_commit = NULL, *search_pattern = NULL;
2220 int diff_context = -1, ch;
2221 int show_patch = 0, limit = 0, log_branches = 0;
2222 const char *errstr;
2223 struct got_reflist_head refs;
2225 SIMPLEQ_INIT(&refs);
2227 #ifndef PROFILE
2228 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2229 NULL)
2230 == -1)
2231 err(1, "pledge");
2232 #endif
2234 limit = get_default_log_limit();
2236 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2237 switch (ch) {
2238 case 'p':
2239 show_patch = 1;
2240 break;
2241 case 'c':
2242 start_commit = optarg;
2243 break;
2244 case 'C':
2245 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2246 &errstr);
2247 if (errstr != NULL)
2248 err(1, "-C option %s", errstr);
2249 break;
2250 case 'l':
2251 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2252 if (errstr != NULL)
2253 err(1, "-l option %s", errstr);
2254 break;
2255 case 'b':
2256 log_branches = 1;
2257 break;
2258 case 'r':
2259 repo_path = realpath(optarg, NULL);
2260 if (repo_path == NULL)
2261 return got_error_from_errno2("realpath",
2262 optarg);
2263 got_path_strip_trailing_slashes(repo_path);
2264 break;
2265 case 's':
2266 search_pattern = optarg;
2267 break;
2268 default:
2269 usage_log();
2270 /* NOTREACHED */
2274 argc -= optind;
2275 argv += optind;
2277 if (diff_context == -1)
2278 diff_context = 3;
2279 else if (!show_patch)
2280 errx(1, "-C reguires -p");
2282 cwd = getcwd(NULL, 0);
2283 if (cwd == NULL) {
2284 error = got_error_from_errno("getcwd");
2285 goto done;
2288 error = got_worktree_open(&worktree, cwd);
2289 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2290 goto done;
2291 error = NULL;
2293 if (argc == 0) {
2294 path = strdup("");
2295 if (path == NULL) {
2296 error = got_error_from_errno("strdup");
2297 goto done;
2299 } else if (argc == 1) {
2300 if (worktree) {
2301 error = got_worktree_resolve_path(&path, worktree,
2302 argv[0]);
2303 if (error)
2304 goto done;
2305 } else {
2306 path = strdup(argv[0]);
2307 if (path == NULL) {
2308 error = got_error_from_errno("strdup");
2309 goto done;
2312 } else
2313 usage_log();
2315 if (repo_path == NULL) {
2316 repo_path = worktree ?
2317 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2319 if (repo_path == NULL) {
2320 error = got_error_from_errno("strdup");
2321 goto done;
2324 error = got_repo_open(&repo, repo_path, NULL);
2325 if (error != NULL)
2326 goto done;
2328 error = apply_unveil(got_repo_get_path(repo), 1,
2329 worktree ? got_worktree_get_root_path(worktree) : NULL);
2330 if (error)
2331 goto done;
2333 if (start_commit == NULL) {
2334 struct got_reference *head_ref;
2335 error = got_ref_open(&head_ref, repo,
2336 worktree ? got_worktree_get_head_ref_name(worktree)
2337 : GOT_REF_HEAD, 0);
2338 if (error != NULL)
2339 return error;
2340 error = got_ref_resolve(&id, repo, head_ref);
2341 got_ref_close(head_ref);
2342 if (error != NULL)
2343 return error;
2344 error = got_object_open_as_commit(&commit, repo, id);
2345 } else {
2346 struct got_reference *ref;
2347 error = got_ref_open(&ref, repo, start_commit, 0);
2348 if (error == NULL) {
2349 int obj_type;
2350 error = got_ref_resolve(&id, repo, ref);
2351 got_ref_close(ref);
2352 if (error != NULL)
2353 goto done;
2354 error = got_object_get_type(&obj_type, repo, id);
2355 if (error != NULL)
2356 goto done;
2357 if (obj_type == GOT_OBJ_TYPE_TAG) {
2358 struct got_tag_object *tag;
2359 error = got_object_open_as_tag(&tag, repo, id);
2360 if (error != NULL)
2361 goto done;
2362 if (got_object_tag_get_object_type(tag) !=
2363 GOT_OBJ_TYPE_COMMIT) {
2364 got_object_tag_close(tag);
2365 error = got_error(GOT_ERR_OBJ_TYPE);
2366 goto done;
2368 free(id);
2369 id = got_object_id_dup(
2370 got_object_tag_get_object_id(tag));
2371 if (id == NULL)
2372 error = got_error_from_errno(
2373 "got_object_id_dup");
2374 got_object_tag_close(tag);
2375 if (error)
2376 goto done;
2377 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2378 error = got_error(GOT_ERR_OBJ_TYPE);
2379 goto done;
2381 error = got_object_open_as_commit(&commit, repo, id);
2382 if (error != NULL)
2383 goto done;
2385 if (commit == NULL) {
2386 error = got_repo_match_object_id_prefix(&id,
2387 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2388 if (error != NULL)
2389 return error;
2392 if (error != NULL)
2393 goto done;
2395 if (worktree) {
2396 const char *prefix = got_worktree_get_path_prefix(worktree);
2397 char *p;
2398 if (asprintf(&p, "%s%s%s", prefix,
2399 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2400 error = got_error_from_errno("asprintf");
2401 goto done;
2403 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2404 free(p);
2405 } else
2406 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2407 if (error != NULL)
2408 goto done;
2409 if (in_repo_path) {
2410 free(path);
2411 path = in_repo_path;
2414 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2415 if (error)
2416 goto done;
2418 error = print_commits(id, repo, path, show_patch, search_pattern,
2419 diff_context, limit, log_branches, &refs);
2420 done:
2421 free(path);
2422 free(repo_path);
2423 free(cwd);
2424 free(id);
2425 if (worktree)
2426 got_worktree_close(worktree);
2427 if (repo) {
2428 const struct got_error *repo_error;
2429 repo_error = got_repo_close(repo);
2430 if (error == NULL)
2431 error = repo_error;
2433 got_ref_list_free(&refs);
2434 return error;
2437 __dead static void
2438 usage_diff(void)
2440 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2441 "[-w] [object1 object2 | path]\n", getprogname());
2442 exit(1);
2445 struct print_diff_arg {
2446 struct got_repository *repo;
2447 struct got_worktree *worktree;
2448 int diff_context;
2449 const char *id_str;
2450 int header_shown;
2451 int diff_staged;
2452 int ignore_whitespace;
2455 static const struct got_error *
2456 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2457 const char *path, struct got_object_id *blob_id,
2458 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2459 int dirfd, const char *de_name)
2461 struct print_diff_arg *a = arg;
2462 const struct got_error *err = NULL;
2463 struct got_blob_object *blob1 = NULL;
2464 int fd = -1;
2465 FILE *f2 = NULL;
2466 char *abspath = NULL, *label1 = NULL;
2467 struct stat sb;
2469 if (a->diff_staged) {
2470 if (staged_status != GOT_STATUS_MODIFY &&
2471 staged_status != GOT_STATUS_ADD &&
2472 staged_status != GOT_STATUS_DELETE)
2473 return NULL;
2474 } else {
2475 if (staged_status == GOT_STATUS_DELETE)
2476 return NULL;
2477 if (status == GOT_STATUS_NONEXISTENT)
2478 return got_error_set_errno(ENOENT, path);
2479 if (status != GOT_STATUS_MODIFY &&
2480 status != GOT_STATUS_ADD &&
2481 status != GOT_STATUS_DELETE &&
2482 status != GOT_STATUS_CONFLICT)
2483 return NULL;
2486 if (!a->header_shown) {
2487 printf("diff %s %s%s\n", a->id_str,
2488 got_worktree_get_root_path(a->worktree),
2489 a->diff_staged ? " (staged changes)" : "");
2490 a->header_shown = 1;
2493 if (a->diff_staged) {
2494 const char *label1 = NULL, *label2 = NULL;
2495 switch (staged_status) {
2496 case GOT_STATUS_MODIFY:
2497 label1 = path;
2498 label2 = path;
2499 break;
2500 case GOT_STATUS_ADD:
2501 label2 = path;
2502 break;
2503 case GOT_STATUS_DELETE:
2504 label1 = path;
2505 break;
2506 default:
2507 return got_error(GOT_ERR_FILE_STATUS);
2509 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2510 label1, label2, a->diff_context, a->ignore_whitespace,
2511 a->repo, stdout);
2514 if (staged_status == GOT_STATUS_ADD ||
2515 staged_status == GOT_STATUS_MODIFY) {
2516 char *id_str;
2517 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2518 8192);
2519 if (err)
2520 goto done;
2521 err = got_object_id_str(&id_str, staged_blob_id);
2522 if (err)
2523 goto done;
2524 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2525 err = got_error_from_errno("asprintf");
2526 free(id_str);
2527 goto done;
2529 free(id_str);
2530 } else if (status != GOT_STATUS_ADD) {
2531 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2532 if (err)
2533 goto done;
2536 if (status != GOT_STATUS_DELETE) {
2537 if (asprintf(&abspath, "%s/%s",
2538 got_worktree_get_root_path(a->worktree), path) == -1) {
2539 err = got_error_from_errno("asprintf");
2540 goto done;
2543 if (dirfd != -1) {
2544 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2545 if (fd == -1) {
2546 err = got_error_from_errno2("openat", abspath);
2547 goto done;
2549 } else {
2550 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2551 if (fd == -1) {
2552 err = got_error_from_errno2("open", abspath);
2553 goto done;
2556 if (fstat(fd, &sb) == -1) {
2557 err = got_error_from_errno2("fstat", abspath);
2558 goto done;
2560 f2 = fdopen(fd, "r");
2561 if (f2 == NULL) {
2562 err = got_error_from_errno2("fdopen", abspath);
2563 goto done;
2565 fd = -1;
2566 } else
2567 sb.st_size = 0;
2569 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2570 a->diff_context, a->ignore_whitespace, stdout);
2571 done:
2572 if (blob1)
2573 got_object_blob_close(blob1);
2574 if (f2 && fclose(f2) == EOF && err == NULL)
2575 err = got_error_from_errno("fclose");
2576 if (fd != -1 && close(fd) == -1 && err == NULL)
2577 err = got_error_from_errno("close");
2578 free(abspath);
2579 return err;
2582 static const struct got_error *
2583 cmd_diff(int argc, char *argv[])
2585 const struct got_error *error;
2586 struct got_repository *repo = NULL;
2587 struct got_worktree *worktree = NULL;
2588 char *cwd = NULL, *repo_path = NULL;
2589 struct got_object_id *id1 = NULL, *id2 = NULL;
2590 const char *id_str1 = NULL, *id_str2 = NULL;
2591 char *label1 = NULL, *label2 = NULL;
2592 int type1, type2;
2593 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2594 const char *errstr;
2595 char *path = NULL;
2597 #ifndef PROFILE
2598 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2599 NULL) == -1)
2600 err(1, "pledge");
2601 #endif
2603 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2604 switch (ch) {
2605 case 'C':
2606 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2607 &errstr);
2608 if (errstr != NULL)
2609 err(1, "-C option %s", errstr);
2610 break;
2611 case 'r':
2612 repo_path = realpath(optarg, NULL);
2613 if (repo_path == NULL)
2614 return got_error_from_errno2("realpath",
2615 optarg);
2616 got_path_strip_trailing_slashes(repo_path);
2617 break;
2618 case 's':
2619 diff_staged = 1;
2620 break;
2621 case 'w':
2622 ignore_whitespace = 1;
2623 break;
2624 default:
2625 usage_diff();
2626 /* NOTREACHED */
2630 argc -= optind;
2631 argv += optind;
2633 cwd = getcwd(NULL, 0);
2634 if (cwd == NULL) {
2635 error = got_error_from_errno("getcwd");
2636 goto done;
2638 error = got_worktree_open(&worktree, cwd);
2639 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2640 goto done;
2641 if (argc <= 1) {
2642 if (worktree == NULL) {
2643 error = got_error(GOT_ERR_NOT_WORKTREE);
2644 goto done;
2646 if (repo_path)
2647 errx(1,
2648 "-r option can't be used when diffing a work tree");
2649 repo_path = strdup(got_worktree_get_repo_path(worktree));
2650 if (repo_path == NULL) {
2651 error = got_error_from_errno("strdup");
2652 goto done;
2654 if (argc == 1) {
2655 error = got_worktree_resolve_path(&path, worktree,
2656 argv[0]);
2657 if (error)
2658 goto done;
2659 } else {
2660 path = strdup("");
2661 if (path == NULL) {
2662 error = got_error_from_errno("strdup");
2663 goto done;
2666 } else if (argc == 2) {
2667 if (diff_staged)
2668 errx(1, "-s option can't be used when diffing "
2669 "objects in repository");
2670 id_str1 = argv[0];
2671 id_str2 = argv[1];
2672 if (worktree && repo_path == NULL) {
2673 repo_path =
2674 strdup(got_worktree_get_repo_path(worktree));
2675 if (repo_path == NULL) {
2676 error = got_error_from_errno("strdup");
2677 goto done;
2680 } else
2681 usage_diff();
2683 if (repo_path == NULL) {
2684 repo_path = getcwd(NULL, 0);
2685 if (repo_path == NULL)
2686 return got_error_from_errno("getcwd");
2689 error = got_repo_open(&repo, repo_path, NULL);
2690 free(repo_path);
2691 if (error != NULL)
2692 goto done;
2694 error = apply_unveil(got_repo_get_path(repo), 1,
2695 worktree ? got_worktree_get_root_path(worktree) : NULL);
2696 if (error)
2697 goto done;
2699 if (argc <= 1) {
2700 struct print_diff_arg arg;
2701 struct got_pathlist_head paths;
2702 char *id_str;
2704 TAILQ_INIT(&paths);
2706 error = got_object_id_str(&id_str,
2707 got_worktree_get_base_commit_id(worktree));
2708 if (error)
2709 goto done;
2710 arg.repo = repo;
2711 arg.worktree = worktree;
2712 arg.diff_context = diff_context;
2713 arg.id_str = id_str;
2714 arg.header_shown = 0;
2715 arg.diff_staged = diff_staged;
2716 arg.ignore_whitespace = ignore_whitespace;
2718 error = got_pathlist_append(&paths, path, NULL);
2719 if (error)
2720 goto done;
2722 error = got_worktree_status(worktree, &paths, repo, print_diff,
2723 &arg, check_cancelled, NULL);
2724 free(id_str);
2725 got_pathlist_free(&paths);
2726 goto done;
2729 error = got_repo_match_object_id(&id1, &label1, id_str1,
2730 GOT_OBJ_TYPE_ANY, 1, repo);
2731 if (error)
2732 goto done;
2734 error = got_repo_match_object_id(&id2, &label2, id_str2,
2735 GOT_OBJ_TYPE_ANY, 1, repo);
2736 if (error)
2737 goto done;
2739 error = got_object_get_type(&type1, repo, id1);
2740 if (error)
2741 goto done;
2743 error = got_object_get_type(&type2, repo, id2);
2744 if (error)
2745 goto done;
2747 if (type1 != type2) {
2748 error = got_error(GOT_ERR_OBJ_TYPE);
2749 goto done;
2752 switch (type1) {
2753 case GOT_OBJ_TYPE_BLOB:
2754 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2755 diff_context, ignore_whitespace, repo, stdout);
2756 break;
2757 case GOT_OBJ_TYPE_TREE:
2758 error = got_diff_objects_as_trees(id1, id2, "", "",
2759 diff_context, ignore_whitespace, repo, stdout);
2760 break;
2761 case GOT_OBJ_TYPE_COMMIT:
2762 printf("diff %s %s\n", label1, label2);
2763 error = got_diff_objects_as_commits(id1, id2, diff_context,
2764 ignore_whitespace, repo, stdout);
2765 break;
2766 default:
2767 error = got_error(GOT_ERR_OBJ_TYPE);
2769 done:
2770 free(label1);
2771 free(label2);
2772 free(id1);
2773 free(id2);
2774 free(path);
2775 if (worktree)
2776 got_worktree_close(worktree);
2777 if (repo) {
2778 const struct got_error *repo_error;
2779 repo_error = got_repo_close(repo);
2780 if (error == NULL)
2781 error = repo_error;
2783 return error;
2786 __dead static void
2787 usage_blame(void)
2789 fprintf(stderr,
2790 "usage: %s blame [-c commit] [-r repository-path] path\n",
2791 getprogname());
2792 exit(1);
2795 struct blame_line {
2796 int annotated;
2797 char *id_str;
2798 char *committer;
2799 char datebuf[11]; /* YYYY-MM-DD + NUL */
2802 struct blame_cb_args {
2803 struct blame_line *lines;
2804 int nlines;
2805 int nlines_prec;
2806 int lineno_cur;
2807 off_t *line_offsets;
2808 FILE *f;
2809 struct got_repository *repo;
2812 static const struct got_error *
2813 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2815 const struct got_error *err = NULL;
2816 struct blame_cb_args *a = arg;
2817 struct blame_line *bline;
2818 char *line = NULL;
2819 size_t linesize = 0;
2820 struct got_commit_object *commit = NULL;
2821 off_t offset;
2822 struct tm tm;
2823 time_t committer_time;
2825 if (nlines != a->nlines ||
2826 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2827 return got_error(GOT_ERR_RANGE);
2829 if (sigint_received)
2830 return got_error(GOT_ERR_ITER_COMPLETED);
2832 if (lineno == -1)
2833 return NULL; /* no change in this commit */
2835 /* Annotate this line. */
2836 bline = &a->lines[lineno - 1];
2837 if (bline->annotated)
2838 return NULL;
2839 err = got_object_id_str(&bline->id_str, id);
2840 if (err)
2841 return err;
2843 err = got_object_open_as_commit(&commit, a->repo, id);
2844 if (err)
2845 goto done;
2847 bline->committer = strdup(got_object_commit_get_committer(commit));
2848 if (bline->committer == NULL) {
2849 err = got_error_from_errno("strdup");
2850 goto done;
2853 committer_time = got_object_commit_get_committer_time(commit);
2854 if (localtime_r(&committer_time, &tm) == NULL)
2855 return got_error_from_errno("localtime_r");
2856 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2857 &tm) >= sizeof(bline->datebuf)) {
2858 err = got_error(GOT_ERR_NO_SPACE);
2859 goto done;
2861 bline->annotated = 1;
2863 /* Print lines annotated so far. */
2864 bline = &a->lines[a->lineno_cur - 1];
2865 if (!bline->annotated)
2866 goto done;
2868 offset = a->line_offsets[a->lineno_cur - 1];
2869 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2870 err = got_error_from_errno("fseeko");
2871 goto done;
2874 while (bline->annotated) {
2875 char *smallerthan, *at, *nl, *committer;
2876 size_t len;
2878 if (getline(&line, &linesize, a->f) == -1) {
2879 if (ferror(a->f))
2880 err = got_error_from_errno("getline");
2881 break;
2884 committer = bline->committer;
2885 smallerthan = strchr(committer, '<');
2886 if (smallerthan && smallerthan[1] != '\0')
2887 committer = smallerthan + 1;
2888 at = strchr(committer, '@');
2889 if (at)
2890 *at = '\0';
2891 len = strlen(committer);
2892 if (len >= 9)
2893 committer[8] = '\0';
2895 nl = strchr(line, '\n');
2896 if (nl)
2897 *nl = '\0';
2898 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2899 bline->id_str, bline->datebuf, committer, line);
2901 a->lineno_cur++;
2902 bline = &a->lines[a->lineno_cur - 1];
2904 done:
2905 if (commit)
2906 got_object_commit_close(commit);
2907 free(line);
2908 return err;
2911 static const struct got_error *
2912 cmd_blame(int argc, char *argv[])
2914 const struct got_error *error;
2915 struct got_repository *repo = NULL;
2916 struct got_worktree *worktree = NULL;
2917 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2918 struct got_object_id *obj_id = NULL;
2919 struct got_object_id *commit_id = NULL;
2920 struct got_blob_object *blob = NULL;
2921 char *commit_id_str = NULL;
2922 struct blame_cb_args bca;
2923 int ch, obj_type, i;
2924 size_t filesize;
2926 memset(&bca, 0, sizeof(bca));
2928 #ifndef PROFILE
2929 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2930 NULL) == -1)
2931 err(1, "pledge");
2932 #endif
2934 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2935 switch (ch) {
2936 case 'c':
2937 commit_id_str = optarg;
2938 break;
2939 case 'r':
2940 repo_path = realpath(optarg, NULL);
2941 if (repo_path == NULL)
2942 return got_error_from_errno2("realpath",
2943 optarg);
2944 got_path_strip_trailing_slashes(repo_path);
2945 break;
2946 default:
2947 usage_blame();
2948 /* NOTREACHED */
2952 argc -= optind;
2953 argv += optind;
2955 if (argc == 1)
2956 path = argv[0];
2957 else
2958 usage_blame();
2960 cwd = getcwd(NULL, 0);
2961 if (cwd == NULL) {
2962 error = got_error_from_errno("getcwd");
2963 goto done;
2965 if (repo_path == NULL) {
2966 error = got_worktree_open(&worktree, cwd);
2967 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2968 goto done;
2969 else
2970 error = NULL;
2971 if (worktree) {
2972 repo_path =
2973 strdup(got_worktree_get_repo_path(worktree));
2974 if (repo_path == NULL)
2975 error = got_error_from_errno("strdup");
2976 if (error)
2977 goto done;
2978 } else {
2979 repo_path = strdup(cwd);
2980 if (repo_path == NULL) {
2981 error = got_error_from_errno("strdup");
2982 goto done;
2987 error = got_repo_open(&repo, repo_path, NULL);
2988 if (error != NULL)
2989 goto done;
2991 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2992 if (error)
2993 goto done;
2995 if (worktree) {
2996 const char *prefix = got_worktree_get_path_prefix(worktree);
2997 char *p, *worktree_subdir = cwd +
2998 strlen(got_worktree_get_root_path(worktree));
2999 if (asprintf(&p, "%s%s%s%s%s",
3000 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3001 worktree_subdir, worktree_subdir[0] ? "/" : "",
3002 path) == -1) {
3003 error = got_error_from_errno("asprintf");
3004 goto done;
3006 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3007 free(p);
3008 } else {
3009 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3011 if (error)
3012 goto done;
3014 if (commit_id_str == NULL) {
3015 struct got_reference *head_ref;
3016 error = got_ref_open(&head_ref, repo, worktree ?
3017 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3018 if (error != NULL)
3019 goto done;
3020 error = got_ref_resolve(&commit_id, repo, head_ref);
3021 got_ref_close(head_ref);
3022 if (error != NULL)
3023 goto done;
3024 } else {
3025 error = got_repo_match_object_id(&commit_id, NULL,
3026 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3027 if (error)
3028 goto done;
3031 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3032 if (error)
3033 goto done;
3035 error = got_object_get_type(&obj_type, repo, obj_id);
3036 if (error)
3037 goto done;
3039 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3040 error = got_error(GOT_ERR_OBJ_TYPE);
3041 goto done;
3044 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3045 if (error)
3046 goto done;
3047 bca.f = got_opentemp();
3048 if (bca.f == NULL) {
3049 error = got_error_from_errno("got_opentemp");
3050 goto done;
3052 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3053 &bca.line_offsets, bca.f, blob);
3054 if (error || bca.nlines == 0)
3055 goto done;
3057 /* Don't include \n at EOF in the blame line count. */
3058 if (bca.line_offsets[bca.nlines - 1] == filesize)
3059 bca.nlines--;
3061 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3062 if (bca.lines == NULL) {
3063 error = got_error_from_errno("calloc");
3064 goto done;
3066 bca.lineno_cur = 1;
3067 bca.nlines_prec = 0;
3068 i = bca.nlines;
3069 while (i > 0) {
3070 i /= 10;
3071 bca.nlines_prec++;
3073 bca.repo = repo;
3075 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3076 check_cancelled, NULL);
3077 done:
3078 free(in_repo_path);
3079 free(repo_path);
3080 free(cwd);
3081 free(commit_id);
3082 free(obj_id);
3083 if (blob)
3084 got_object_blob_close(blob);
3085 if (worktree)
3086 got_worktree_close(worktree);
3087 if (repo) {
3088 const struct got_error *repo_error;
3089 repo_error = got_repo_close(repo);
3090 if (error == NULL)
3091 error = repo_error;
3093 if (bca.lines) {
3094 for (i = 0; i < bca.nlines; i++) {
3095 struct blame_line *bline = &bca.lines[i];
3096 free(bline->id_str);
3097 free(bline->committer);
3099 free(bca.lines);
3101 free(bca.line_offsets);
3102 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3103 error = got_error_from_errno("fclose");
3104 return error;
3107 __dead static void
3108 usage_tree(void)
3110 fprintf(stderr,
3111 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3112 getprogname());
3113 exit(1);
3116 static void
3117 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3118 const char *root_path)
3120 int is_root_path = (strcmp(path, root_path) == 0);
3121 const char *modestr = "";
3122 mode_t mode = got_tree_entry_get_mode(te);
3124 path += strlen(root_path);
3125 while (path[0] == '/')
3126 path++;
3128 if (got_object_tree_entry_is_submodule(te))
3129 modestr = "$";
3130 else if (S_ISLNK(mode))
3131 modestr = "@";
3132 else if (S_ISDIR(mode))
3133 modestr = "/";
3134 else if (mode & S_IXUSR)
3135 modestr = "*";
3137 printf("%s%s%s%s%s\n", id ? id : "", path,
3138 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3141 static const struct got_error *
3142 print_tree(const char *path, struct got_object_id *commit_id,
3143 int show_ids, int recurse, const char *root_path,
3144 struct got_repository *repo)
3146 const struct got_error *err = NULL;
3147 struct got_object_id *tree_id = NULL;
3148 struct got_tree_object *tree = NULL;
3149 int nentries, i;
3151 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3152 if (err)
3153 goto done;
3155 err = got_object_open_as_tree(&tree, repo, tree_id);
3156 if (err)
3157 goto done;
3158 nentries = got_object_tree_get_nentries(tree);
3159 for (i = 0; i < nentries; i++) {
3160 struct got_tree_entry *te;
3161 char *id = NULL;
3163 if (sigint_received || sigpipe_received)
3164 break;
3166 te = got_object_tree_get_entry(tree, i);
3167 if (show_ids) {
3168 char *id_str;
3169 err = got_object_id_str(&id_str,
3170 got_tree_entry_get_id(te));
3171 if (err)
3172 goto done;
3173 if (asprintf(&id, "%s ", id_str) == -1) {
3174 err = got_error_from_errno("asprintf");
3175 free(id_str);
3176 goto done;
3178 free(id_str);
3180 print_entry(te, id, path, root_path);
3181 free(id);
3183 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3184 char *child_path;
3185 if (asprintf(&child_path, "%s%s%s", path,
3186 path[0] == '/' && path[1] == '\0' ? "" : "/",
3187 got_tree_entry_get_name(te)) == -1) {
3188 err = got_error_from_errno("asprintf");
3189 goto done;
3191 err = print_tree(child_path, commit_id, show_ids, 1,
3192 root_path, repo);
3193 free(child_path);
3194 if (err)
3195 goto done;
3198 done:
3199 if (tree)
3200 got_object_tree_close(tree);
3201 free(tree_id);
3202 return err;
3205 static const struct got_error *
3206 cmd_tree(int argc, char *argv[])
3208 const struct got_error *error;
3209 struct got_repository *repo = NULL;
3210 struct got_worktree *worktree = NULL;
3211 const char *path;
3212 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3213 struct got_object_id *commit_id = NULL;
3214 char *commit_id_str = NULL;
3215 int show_ids = 0, recurse = 0;
3216 int ch;
3218 #ifndef PROFILE
3219 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3220 NULL) == -1)
3221 err(1, "pledge");
3222 #endif
3224 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3225 switch (ch) {
3226 case 'c':
3227 commit_id_str = optarg;
3228 break;
3229 case 'r':
3230 repo_path = realpath(optarg, NULL);
3231 if (repo_path == NULL)
3232 return got_error_from_errno2("realpath",
3233 optarg);
3234 got_path_strip_trailing_slashes(repo_path);
3235 break;
3236 case 'i':
3237 show_ids = 1;
3238 break;
3239 case 'R':
3240 recurse = 1;
3241 break;
3242 default:
3243 usage_tree();
3244 /* NOTREACHED */
3248 argc -= optind;
3249 argv += optind;
3251 if (argc == 1)
3252 path = argv[0];
3253 else if (argc > 1)
3254 usage_tree();
3255 else
3256 path = NULL;
3258 cwd = getcwd(NULL, 0);
3259 if (cwd == NULL) {
3260 error = got_error_from_errno("getcwd");
3261 goto done;
3263 if (repo_path == NULL) {
3264 error = got_worktree_open(&worktree, cwd);
3265 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3266 goto done;
3267 else
3268 error = NULL;
3269 if (worktree) {
3270 repo_path =
3271 strdup(got_worktree_get_repo_path(worktree));
3272 if (repo_path == NULL)
3273 error = got_error_from_errno("strdup");
3274 if (error)
3275 goto done;
3276 } else {
3277 repo_path = strdup(cwd);
3278 if (repo_path == NULL) {
3279 error = got_error_from_errno("strdup");
3280 goto done;
3285 error = got_repo_open(&repo, repo_path, NULL);
3286 if (error != NULL)
3287 goto done;
3289 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3290 if (error)
3291 goto done;
3293 if (path == NULL) {
3294 if (worktree) {
3295 char *p, *worktree_subdir = cwd +
3296 strlen(got_worktree_get_root_path(worktree));
3297 if (asprintf(&p, "%s/%s",
3298 got_worktree_get_path_prefix(worktree),
3299 worktree_subdir) == -1) {
3300 error = got_error_from_errno("asprintf");
3301 goto done;
3303 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3304 free(p);
3305 if (error)
3306 goto done;
3307 } else
3308 path = "/";
3310 if (in_repo_path == NULL) {
3311 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3312 if (error != NULL)
3313 goto done;
3316 if (commit_id_str == NULL) {
3317 struct got_reference *head_ref;
3318 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3319 if (error != NULL)
3320 goto done;
3321 error = got_ref_resolve(&commit_id, repo, head_ref);
3322 got_ref_close(head_ref);
3323 if (error != NULL)
3324 goto done;
3325 } else {
3326 error = got_repo_match_object_id(&commit_id, NULL,
3327 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3328 if (error)
3329 goto done;
3332 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3333 in_repo_path, repo);
3334 done:
3335 free(in_repo_path);
3336 free(repo_path);
3337 free(cwd);
3338 free(commit_id);
3339 if (worktree)
3340 got_worktree_close(worktree);
3341 if (repo) {
3342 const struct got_error *repo_error;
3343 repo_error = got_repo_close(repo);
3344 if (error == NULL)
3345 error = repo_error;
3347 return error;
3350 __dead static void
3351 usage_status(void)
3353 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3354 exit(1);
3357 static const struct got_error *
3358 print_status(void *arg, unsigned char status, unsigned char staged_status,
3359 const char *path, struct got_object_id *blob_id,
3360 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3361 int dirfd, const char *de_name)
3363 if (status == staged_status && (status == GOT_STATUS_DELETE))
3364 status = GOT_STATUS_NO_CHANGE;
3365 printf("%c%c %s\n", status, staged_status, path);
3366 return NULL;
3369 static const struct got_error *
3370 cmd_status(int argc, char *argv[])
3372 const struct got_error *error = NULL;
3373 struct got_repository *repo = NULL;
3374 struct got_worktree *worktree = NULL;
3375 char *cwd = NULL;
3376 struct got_pathlist_head paths;
3377 struct got_pathlist_entry *pe;
3378 int ch;
3380 TAILQ_INIT(&paths);
3382 while ((ch = getopt(argc, argv, "")) != -1) {
3383 switch (ch) {
3384 default:
3385 usage_status();
3386 /* NOTREACHED */
3390 argc -= optind;
3391 argv += optind;
3393 #ifndef PROFILE
3394 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3395 NULL) == -1)
3396 err(1, "pledge");
3397 #endif
3398 cwd = getcwd(NULL, 0);
3399 if (cwd == NULL) {
3400 error = got_error_from_errno("getcwd");
3401 goto done;
3404 error = got_worktree_open(&worktree, cwd);
3405 if (error != NULL)
3406 goto done;
3408 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3409 NULL);
3410 if (error != NULL)
3411 goto done;
3413 error = apply_unveil(got_repo_get_path(repo), 1,
3414 got_worktree_get_root_path(worktree));
3415 if (error)
3416 goto done;
3418 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3419 if (error)
3420 goto done;
3422 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3423 check_cancelled, NULL);
3424 done:
3425 TAILQ_FOREACH(pe, &paths, entry)
3426 free((char *)pe->path);
3427 got_pathlist_free(&paths);
3428 free(cwd);
3429 return error;
3432 __dead static void
3433 usage_ref(void)
3435 fprintf(stderr,
3436 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3437 getprogname());
3438 exit(1);
3441 static const struct got_error *
3442 list_refs(struct got_repository *repo)
3444 static const struct got_error *err = NULL;
3445 struct got_reflist_head refs;
3446 struct got_reflist_entry *re;
3448 SIMPLEQ_INIT(&refs);
3449 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3450 if (err)
3451 return err;
3453 SIMPLEQ_FOREACH(re, &refs, entry) {
3454 char *refstr;
3455 refstr = got_ref_to_str(re->ref);
3456 if (refstr == NULL)
3457 return got_error_from_errno("got_ref_to_str");
3458 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3459 free(refstr);
3462 got_ref_list_free(&refs);
3463 return NULL;
3466 static const struct got_error *
3467 delete_ref(struct got_repository *repo, const char *refname)
3469 const struct got_error *err = NULL;
3470 struct got_reference *ref;
3472 err = got_ref_open(&ref, repo, refname, 0);
3473 if (err)
3474 return err;
3476 err = got_ref_delete(ref, repo);
3477 got_ref_close(ref);
3478 return err;
3481 static const struct got_error *
3482 add_ref(struct got_repository *repo, const char *refname, const char *target)
3484 const struct got_error *err = NULL;
3485 struct got_object_id *id;
3486 struct got_reference *ref = NULL;
3489 * Don't let the user create a reference name with a leading '-'.
3490 * While technically a valid reference name, this case is usually
3491 * an unintended typo.
3493 if (refname[0] == '-')
3494 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3496 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3497 repo);
3498 if (err) {
3499 struct got_reference *target_ref;
3501 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3502 return err;
3503 err = got_ref_open(&target_ref, repo, target, 0);
3504 if (err)
3505 return err;
3506 err = got_ref_resolve(&id, repo, target_ref);
3507 got_ref_close(target_ref);
3508 if (err)
3509 return err;
3512 err = got_ref_alloc(&ref, refname, id);
3513 if (err)
3514 goto done;
3516 err = got_ref_write(ref, repo);
3517 done:
3518 if (ref)
3519 got_ref_close(ref);
3520 free(id);
3521 return err;
3524 static const struct got_error *
3525 add_symref(struct got_repository *repo, const char *refname, const char *target)
3527 const struct got_error *err = NULL;
3528 struct got_reference *ref = NULL;
3529 struct got_reference *target_ref = NULL;
3532 * Don't let the user create a reference name with a leading '-'.
3533 * While technically a valid reference name, this case is usually
3534 * an unintended typo.
3536 if (refname[0] == '-')
3537 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3539 err = got_ref_open(&target_ref, repo, target, 0);
3540 if (err)
3541 return err;
3543 err = got_ref_alloc_symref(&ref, refname, target_ref);
3544 if (err)
3545 goto done;
3547 err = got_ref_write(ref, repo);
3548 done:
3549 if (target_ref)
3550 got_ref_close(target_ref);
3551 if (ref)
3552 got_ref_close(ref);
3553 return err;
3556 static const struct got_error *
3557 cmd_ref(int argc, char *argv[])
3559 const struct got_error *error = NULL;
3560 struct got_repository *repo = NULL;
3561 struct got_worktree *worktree = NULL;
3562 char *cwd = NULL, *repo_path = NULL;
3563 int ch, do_list = 0, create_symref = 0;
3564 const char *delref = NULL;
3566 /* TODO: Add -s option for adding symbolic references. */
3567 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3568 switch (ch) {
3569 case 'd':
3570 delref = optarg;
3571 break;
3572 case 'r':
3573 repo_path = realpath(optarg, NULL);
3574 if (repo_path == NULL)
3575 return got_error_from_errno2("realpath",
3576 optarg);
3577 got_path_strip_trailing_slashes(repo_path);
3578 break;
3579 case 'l':
3580 do_list = 1;
3581 break;
3582 case 's':
3583 create_symref = 1;
3584 break;
3585 default:
3586 usage_ref();
3587 /* NOTREACHED */
3591 if (do_list && delref)
3592 errx(1, "-l and -d options are mutually exclusive\n");
3594 argc -= optind;
3595 argv += optind;
3597 if (do_list || delref) {
3598 if (create_symref)
3599 errx(1, "-s option cannot be used together with the "
3600 "-l or -d options");
3601 if (argc > 0)
3602 usage_ref();
3603 } else if (argc != 2)
3604 usage_ref();
3606 #ifndef PROFILE
3607 if (do_list) {
3608 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3609 NULL) == -1)
3610 err(1, "pledge");
3611 } else {
3612 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3613 "sendfd unveil", NULL) == -1)
3614 err(1, "pledge");
3616 #endif
3617 cwd = getcwd(NULL, 0);
3618 if (cwd == NULL) {
3619 error = got_error_from_errno("getcwd");
3620 goto done;
3623 if (repo_path == NULL) {
3624 error = got_worktree_open(&worktree, cwd);
3625 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3626 goto done;
3627 else
3628 error = NULL;
3629 if (worktree) {
3630 repo_path =
3631 strdup(got_worktree_get_repo_path(worktree));
3632 if (repo_path == NULL)
3633 error = got_error_from_errno("strdup");
3634 if (error)
3635 goto done;
3636 } else {
3637 repo_path = strdup(cwd);
3638 if (repo_path == NULL) {
3639 error = got_error_from_errno("strdup");
3640 goto done;
3645 error = got_repo_open(&repo, repo_path, NULL);
3646 if (error != NULL)
3647 goto done;
3649 error = apply_unveil(got_repo_get_path(repo), do_list,
3650 worktree ? got_worktree_get_root_path(worktree) : NULL);
3651 if (error)
3652 goto done;
3654 if (do_list)
3655 error = list_refs(repo);
3656 else if (delref)
3657 error = delete_ref(repo, delref);
3658 else if (create_symref)
3659 error = add_symref(repo, argv[0], argv[1]);
3660 else
3661 error = add_ref(repo, argv[0], argv[1]);
3662 done:
3663 if (repo)
3664 got_repo_close(repo);
3665 if (worktree)
3666 got_worktree_close(worktree);
3667 free(cwd);
3668 free(repo_path);
3669 return error;
3672 __dead static void
3673 usage_branch(void)
3675 fprintf(stderr,
3676 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3677 "[name]\n", getprogname());
3678 exit(1);
3681 static const struct got_error *
3682 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3683 struct got_reference *ref)
3685 const struct got_error *err = NULL;
3686 const char *refname, *marker = " ";
3687 char *refstr;
3689 refname = got_ref_get_name(ref);
3690 if (worktree && strcmp(refname,
3691 got_worktree_get_head_ref_name(worktree)) == 0) {
3692 struct got_object_id *id = NULL;
3694 err = got_ref_resolve(&id, repo, ref);
3695 if (err)
3696 return err;
3697 if (got_object_id_cmp(id,
3698 got_worktree_get_base_commit_id(worktree)) == 0)
3699 marker = "* ";
3700 else
3701 marker = "~ ";
3702 free(id);
3705 if (strncmp(refname, "refs/heads/", 11) == 0)
3706 refname += 11;
3707 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3708 refname += 18;
3710 refstr = got_ref_to_str(ref);
3711 if (refstr == NULL)
3712 return got_error_from_errno("got_ref_to_str");
3714 printf("%s%s: %s\n", marker, refname, refstr);
3715 free(refstr);
3716 return NULL;
3719 static const struct got_error *
3720 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3722 const char *refname;
3724 if (worktree == NULL)
3725 return got_error(GOT_ERR_NOT_WORKTREE);
3727 refname = got_worktree_get_head_ref_name(worktree);
3729 if (strncmp(refname, "refs/heads/", 11) == 0)
3730 refname += 11;
3731 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3732 refname += 18;
3734 printf("%s\n", refname);
3736 return NULL;
3739 static const struct got_error *
3740 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3742 static const struct got_error *err = NULL;
3743 struct got_reflist_head refs;
3744 struct got_reflist_entry *re;
3745 struct got_reference *temp_ref = NULL;
3746 int rebase_in_progress, histedit_in_progress;
3748 SIMPLEQ_INIT(&refs);
3750 if (worktree) {
3751 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3752 worktree);
3753 if (err)
3754 return err;
3756 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3757 worktree);
3758 if (err)
3759 return err;
3761 if (rebase_in_progress || histedit_in_progress) {
3762 err = got_ref_open(&temp_ref, repo,
3763 got_worktree_get_head_ref_name(worktree), 0);
3764 if (err)
3765 return err;
3766 list_branch(repo, worktree, temp_ref);
3767 got_ref_close(temp_ref);
3771 err = got_ref_list(&refs, repo, "refs/heads",
3772 got_ref_cmp_by_name, NULL);
3773 if (err)
3774 return err;
3776 SIMPLEQ_FOREACH(re, &refs, entry)
3777 list_branch(repo, worktree, re->ref);
3779 got_ref_list_free(&refs);
3780 return NULL;
3783 static const struct got_error *
3784 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3785 const char *branch_name)
3787 const struct got_error *err = NULL;
3788 struct got_reference *ref = NULL;
3789 char *refname;
3791 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3792 return got_error_from_errno("asprintf");
3794 err = got_ref_open(&ref, repo, refname, 0);
3795 if (err)
3796 goto done;
3798 if (worktree &&
3799 strcmp(got_worktree_get_head_ref_name(worktree),
3800 got_ref_get_name(ref)) == 0) {
3801 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3802 "will not delete this work tree's current branch");
3803 goto done;
3806 err = got_ref_delete(ref, repo);
3807 done:
3808 if (ref)
3809 got_ref_close(ref);
3810 free(refname);
3811 return err;
3814 static const struct got_error *
3815 add_branch(struct got_repository *repo, const char *branch_name,
3816 struct got_object_id *base_commit_id)
3818 const struct got_error *err = NULL;
3819 struct got_reference *ref = NULL;
3820 char *base_refname = NULL, *refname = NULL;
3823 * Don't let the user create a branch name with a leading '-'.
3824 * While technically a valid reference name, this case is usually
3825 * an unintended typo.
3827 if (branch_name[0] == '-')
3828 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3830 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3831 err = got_error_from_errno("asprintf");
3832 goto done;
3835 err = got_ref_open(&ref, repo, refname, 0);
3836 if (err == NULL) {
3837 err = got_error(GOT_ERR_BRANCH_EXISTS);
3838 goto done;
3839 } else if (err->code != GOT_ERR_NOT_REF)
3840 goto done;
3842 err = got_ref_alloc(&ref, refname, base_commit_id);
3843 if (err)
3844 goto done;
3846 err = got_ref_write(ref, repo);
3847 done:
3848 if (ref)
3849 got_ref_close(ref);
3850 free(base_refname);
3851 free(refname);
3852 return err;
3855 static const struct got_error *
3856 cmd_branch(int argc, char *argv[])
3858 const struct got_error *error = NULL;
3859 struct got_repository *repo = NULL;
3860 struct got_worktree *worktree = NULL;
3861 char *cwd = NULL, *repo_path = NULL;
3862 int ch, do_list = 0, do_show = 0, do_update = 1;
3863 const char *delref = NULL, *commit_id_arg = NULL;
3864 struct got_reference *ref = NULL;
3865 struct got_pathlist_head paths;
3866 struct got_pathlist_entry *pe;
3867 struct got_object_id *commit_id = NULL;
3868 char *commit_id_str = NULL;
3870 TAILQ_INIT(&paths);
3872 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3873 switch (ch) {
3874 case 'c':
3875 commit_id_arg = optarg;
3876 break;
3877 case 'd':
3878 delref = optarg;
3879 break;
3880 case 'r':
3881 repo_path = realpath(optarg, NULL);
3882 if (repo_path == NULL)
3883 return got_error_from_errno2("realpath",
3884 optarg);
3885 got_path_strip_trailing_slashes(repo_path);
3886 break;
3887 case 'l':
3888 do_list = 1;
3889 break;
3890 case 'n':
3891 do_update = 0;
3892 break;
3893 default:
3894 usage_branch();
3895 /* NOTREACHED */
3899 if (do_list && delref)
3900 errx(1, "-l and -d options are mutually exclusive\n");
3902 argc -= optind;
3903 argv += optind;
3905 if (!do_list && !delref && argc == 0)
3906 do_show = 1;
3908 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3909 errx(1, "-c option can only be used when creating a branch");
3911 if (do_list || delref) {
3912 if (argc > 0)
3913 usage_branch();
3914 } else if (!do_show && argc != 1)
3915 usage_branch();
3917 #ifndef PROFILE
3918 if (do_list || do_show) {
3919 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3920 NULL) == -1)
3921 err(1, "pledge");
3922 } else {
3923 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3924 "sendfd unveil", NULL) == -1)
3925 err(1, "pledge");
3927 #endif
3928 cwd = getcwd(NULL, 0);
3929 if (cwd == NULL) {
3930 error = got_error_from_errno("getcwd");
3931 goto done;
3934 if (repo_path == NULL) {
3935 error = got_worktree_open(&worktree, cwd);
3936 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3937 goto done;
3938 else
3939 error = NULL;
3940 if (worktree) {
3941 repo_path =
3942 strdup(got_worktree_get_repo_path(worktree));
3943 if (repo_path == NULL)
3944 error = got_error_from_errno("strdup");
3945 if (error)
3946 goto done;
3947 } else {
3948 repo_path = strdup(cwd);
3949 if (repo_path == NULL) {
3950 error = got_error_from_errno("strdup");
3951 goto done;
3956 error = got_repo_open(&repo, repo_path, NULL);
3957 if (error != NULL)
3958 goto done;
3960 error = apply_unveil(got_repo_get_path(repo), do_list,
3961 worktree ? got_worktree_get_root_path(worktree) : NULL);
3962 if (error)
3963 goto done;
3965 if (do_show)
3966 error = show_current_branch(repo, worktree);
3967 else if (do_list)
3968 error = list_branches(repo, worktree);
3969 else if (delref)
3970 error = delete_branch(repo, worktree, delref);
3971 else {
3972 if (commit_id_arg == NULL)
3973 commit_id_arg = worktree ?
3974 got_worktree_get_head_ref_name(worktree) :
3975 GOT_REF_HEAD;
3976 error = got_repo_match_object_id(&commit_id, NULL,
3977 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3978 if (error)
3979 goto done;
3980 error = add_branch(repo, argv[0], commit_id);
3981 if (error)
3982 goto done;
3983 if (worktree && do_update) {
3984 int did_something = 0;
3985 char *branch_refname = NULL;
3987 error = got_object_id_str(&commit_id_str, commit_id);
3988 if (error)
3989 goto done;
3990 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3991 worktree);
3992 if (error)
3993 goto done;
3994 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3995 == -1) {
3996 error = got_error_from_errno("asprintf");
3997 goto done;
3999 error = got_ref_open(&ref, repo, branch_refname, 0);
4000 free(branch_refname);
4001 if (error)
4002 goto done;
4003 error = switch_head_ref(ref, commit_id, worktree,
4004 repo);
4005 if (error)
4006 goto done;
4007 error = got_worktree_set_base_commit_id(worktree, repo,
4008 commit_id);
4009 if (error)
4010 goto done;
4011 error = got_worktree_checkout_files(worktree, &paths,
4012 repo, update_progress, &did_something,
4013 check_cancelled, NULL);
4014 if (error)
4015 goto done;
4016 if (did_something)
4017 printf("Updated to commit %s\n", commit_id_str);
4020 done:
4021 if (ref)
4022 got_ref_close(ref);
4023 if (repo)
4024 got_repo_close(repo);
4025 if (worktree)
4026 got_worktree_close(worktree);
4027 free(cwd);
4028 free(repo_path);
4029 free(commit_id);
4030 free(commit_id_str);
4031 TAILQ_FOREACH(pe, &paths, entry)
4032 free((char *)pe->path);
4033 got_pathlist_free(&paths);
4034 return error;
4038 __dead static void
4039 usage_tag(void)
4041 fprintf(stderr,
4042 "usage: %s tag [-c commit] [-r repository] [-l] "
4043 "[-m message] name\n", getprogname());
4044 exit(1);
4047 #if 0
4048 static const struct got_error *
4049 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4051 const struct got_error *err = NULL;
4052 struct got_reflist_entry *re, *se, *new;
4053 struct got_object_id *re_id, *se_id;
4054 struct got_tag_object *re_tag, *se_tag;
4055 time_t re_time, se_time;
4057 SIMPLEQ_FOREACH(re, tags, entry) {
4058 se = SIMPLEQ_FIRST(sorted);
4059 if (se == NULL) {
4060 err = got_reflist_entry_dup(&new, re);
4061 if (err)
4062 return err;
4063 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4064 continue;
4065 } else {
4066 err = got_ref_resolve(&re_id, repo, re->ref);
4067 if (err)
4068 break;
4069 err = got_object_open_as_tag(&re_tag, repo, re_id);
4070 free(re_id);
4071 if (err)
4072 break;
4073 re_time = got_object_tag_get_tagger_time(re_tag);
4074 got_object_tag_close(re_tag);
4077 while (se) {
4078 err = got_ref_resolve(&se_id, repo, re->ref);
4079 if (err)
4080 break;
4081 err = got_object_open_as_tag(&se_tag, repo, se_id);
4082 free(se_id);
4083 if (err)
4084 break;
4085 se_time = got_object_tag_get_tagger_time(se_tag);
4086 got_object_tag_close(se_tag);
4088 if (se_time > re_time) {
4089 err = got_reflist_entry_dup(&new, re);
4090 if (err)
4091 return err;
4092 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4093 break;
4095 se = SIMPLEQ_NEXT(se, entry);
4096 continue;
4099 done:
4100 return err;
4102 #endif
4104 static const struct got_error *
4105 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4107 static const struct got_error *err = NULL;
4108 struct got_reflist_head refs;
4109 struct got_reflist_entry *re;
4111 SIMPLEQ_INIT(&refs);
4113 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4114 if (err)
4115 return err;
4117 SIMPLEQ_FOREACH(re, &refs, entry) {
4118 const char *refname;
4119 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4120 char datebuf[26];
4121 const char *tagger;
4122 time_t tagger_time;
4123 struct got_object_id *id;
4124 struct got_tag_object *tag;
4125 struct got_commit_object *commit = NULL;
4127 refname = got_ref_get_name(re->ref);
4128 if (strncmp(refname, "refs/tags/", 10) != 0)
4129 continue;
4130 refname += 10;
4131 refstr = got_ref_to_str(re->ref);
4132 if (refstr == NULL) {
4133 err = got_error_from_errno("got_ref_to_str");
4134 break;
4136 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4137 free(refstr);
4139 err = got_ref_resolve(&id, repo, re->ref);
4140 if (err)
4141 break;
4142 err = got_object_open_as_tag(&tag, repo, id);
4143 if (err) {
4144 if (err->code != GOT_ERR_OBJ_TYPE) {
4145 free(id);
4146 break;
4148 /* "lightweight" tag */
4149 err = got_object_open_as_commit(&commit, repo, id);
4150 if (err) {
4151 free(id);
4152 break;
4154 tagger = got_object_commit_get_committer(commit);
4155 tagger_time =
4156 got_object_commit_get_committer_time(commit);
4157 err = got_object_id_str(&id_str, id);
4158 free(id);
4159 if (err)
4160 break;
4161 } else {
4162 free(id);
4163 tagger = got_object_tag_get_tagger(tag);
4164 tagger_time = got_object_tag_get_tagger_time(tag);
4165 err = got_object_id_str(&id_str,
4166 got_object_tag_get_object_id(tag));
4167 if (err)
4168 break;
4170 printf("from: %s\n", tagger);
4171 datestr = get_datestr(&tagger_time, datebuf);
4172 if (datestr)
4173 printf("date: %s UTC\n", datestr);
4174 if (commit)
4175 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4176 else {
4177 switch (got_object_tag_get_object_type(tag)) {
4178 case GOT_OBJ_TYPE_BLOB:
4179 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4180 id_str);
4181 break;
4182 case GOT_OBJ_TYPE_TREE:
4183 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4184 id_str);
4185 break;
4186 case GOT_OBJ_TYPE_COMMIT:
4187 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4188 id_str);
4189 break;
4190 case GOT_OBJ_TYPE_TAG:
4191 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4192 id_str);
4193 break;
4194 default:
4195 break;
4198 free(id_str);
4199 if (commit) {
4200 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4201 if (err)
4202 break;
4203 got_object_commit_close(commit);
4204 } else {
4205 tagmsg0 = strdup(got_object_tag_get_message(tag));
4206 got_object_tag_close(tag);
4207 if (tagmsg0 == NULL) {
4208 err = got_error_from_errno("strdup");
4209 break;
4213 tagmsg = tagmsg0;
4214 do {
4215 line = strsep(&tagmsg, "\n");
4216 if (line)
4217 printf(" %s\n", line);
4218 } while (line);
4219 free(tagmsg0);
4222 got_ref_list_free(&refs);
4223 return NULL;
4226 static const struct got_error *
4227 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4228 const char *tag_name, const char *repo_path)
4230 const struct got_error *err = NULL;
4231 char *template = NULL, *initial_content = NULL;
4232 char *editor = NULL;
4233 int fd = -1;
4235 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4236 err = got_error_from_errno("asprintf");
4237 goto done;
4240 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4241 commit_id_str, tag_name) == -1) {
4242 err = got_error_from_errno("asprintf");
4243 goto done;
4246 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4247 if (err)
4248 goto done;
4250 dprintf(fd, initial_content);
4251 close(fd);
4253 err = get_editor(&editor);
4254 if (err)
4255 goto done;
4256 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4257 done:
4258 free(initial_content);
4259 free(template);
4260 free(editor);
4262 /* Editor is done; we can now apply unveil(2) */
4263 if (err == NULL) {
4264 err = apply_unveil(repo_path, 0, NULL);
4265 if (err) {
4266 free(*tagmsg);
4267 *tagmsg = NULL;
4270 return err;
4273 static const struct got_error *
4274 add_tag(struct got_repository *repo, const char *tag_name,
4275 const char *commit_arg, const char *tagmsg_arg)
4277 const struct got_error *err = NULL;
4278 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4279 char *label = NULL, *commit_id_str = NULL;
4280 struct got_reference *ref = NULL;
4281 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4282 char *tagmsg_path = NULL, *tag_id_str = NULL;
4283 int preserve_tagmsg = 0;
4286 * Don't let the user create a tag name with a leading '-'.
4287 * While technically a valid reference name, this case is usually
4288 * an unintended typo.
4290 if (tag_name[0] == '-')
4291 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4293 err = get_author(&tagger, repo);
4294 if (err)
4295 return err;
4297 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4298 GOT_OBJ_TYPE_COMMIT, 1, repo);
4299 if (err)
4300 goto done;
4302 err = got_object_id_str(&commit_id_str, commit_id);
4303 if (err)
4304 goto done;
4306 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4307 refname = strdup(tag_name);
4308 if (refname == NULL) {
4309 err = got_error_from_errno("strdup");
4310 goto done;
4312 tag_name += 10;
4313 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4314 err = got_error_from_errno("asprintf");
4315 goto done;
4318 err = got_ref_open(&ref, repo, refname, 0);
4319 if (err == NULL) {
4320 err = got_error(GOT_ERR_TAG_EXISTS);
4321 goto done;
4322 } else if (err->code != GOT_ERR_NOT_REF)
4323 goto done;
4325 if (tagmsg_arg == NULL) {
4326 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4327 tag_name, got_repo_get_path(repo));
4328 if (err) {
4329 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4330 tagmsg_path != NULL)
4331 preserve_tagmsg = 1;
4332 goto done;
4336 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4337 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4338 if (err) {
4339 if (tagmsg_path)
4340 preserve_tagmsg = 1;
4341 goto done;
4344 err = got_ref_alloc(&ref, refname, tag_id);
4345 if (err) {
4346 if (tagmsg_path)
4347 preserve_tagmsg = 1;
4348 goto done;
4351 err = got_ref_write(ref, repo);
4352 if (err) {
4353 if (tagmsg_path)
4354 preserve_tagmsg = 1;
4355 goto done;
4358 err = got_object_id_str(&tag_id_str, tag_id);
4359 if (err) {
4360 if (tagmsg_path)
4361 preserve_tagmsg = 1;
4362 goto done;
4364 printf("Created tag %s\n", tag_id_str);
4365 done:
4366 if (preserve_tagmsg) {
4367 fprintf(stderr, "%s: tag message preserved in %s\n",
4368 getprogname(), tagmsg_path);
4369 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4370 err = got_error_from_errno2("unlink", tagmsg_path);
4371 free(tag_id_str);
4372 if (ref)
4373 got_ref_close(ref);
4374 free(commit_id);
4375 free(commit_id_str);
4376 free(refname);
4377 free(tagmsg);
4378 free(tagmsg_path);
4379 free(tagger);
4380 return err;
4383 static const struct got_error *
4384 cmd_tag(int argc, char *argv[])
4386 const struct got_error *error = NULL;
4387 struct got_repository *repo = NULL;
4388 struct got_worktree *worktree = NULL;
4389 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4390 char *gitconfig_path = NULL;
4391 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4392 int ch, do_list = 0;
4394 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4395 switch (ch) {
4396 case 'c':
4397 commit_id_arg = optarg;
4398 break;
4399 case 'm':
4400 tagmsg = optarg;
4401 break;
4402 case 'r':
4403 repo_path = realpath(optarg, NULL);
4404 if (repo_path == NULL)
4405 return got_error_from_errno2("realpath",
4406 optarg);
4407 got_path_strip_trailing_slashes(repo_path);
4408 break;
4409 case 'l':
4410 do_list = 1;
4411 break;
4412 default:
4413 usage_tag();
4414 /* NOTREACHED */
4418 argc -= optind;
4419 argv += optind;
4421 if (do_list) {
4422 if (commit_id_arg != NULL)
4423 errx(1, "-c option can only be used when creating a tag");
4424 if (tagmsg)
4425 errx(1, "-l and -m options are mutually exclusive");
4426 if (argc > 0)
4427 usage_tag();
4428 } else if (argc != 1)
4429 usage_tag();
4431 tag_name = argv[0];
4433 #ifndef PROFILE
4434 if (do_list) {
4435 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4436 NULL) == -1)
4437 err(1, "pledge");
4438 } else {
4439 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4440 "sendfd unveil", NULL) == -1)
4441 err(1, "pledge");
4443 #endif
4444 cwd = getcwd(NULL, 0);
4445 if (cwd == NULL) {
4446 error = got_error_from_errno("getcwd");
4447 goto done;
4450 if (repo_path == NULL) {
4451 error = got_worktree_open(&worktree, cwd);
4452 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4453 goto done;
4454 else
4455 error = NULL;
4456 if (worktree) {
4457 repo_path =
4458 strdup(got_worktree_get_repo_path(worktree));
4459 if (repo_path == NULL)
4460 error = got_error_from_errno("strdup");
4461 if (error)
4462 goto done;
4463 } else {
4464 repo_path = strdup(cwd);
4465 if (repo_path == NULL) {
4466 error = got_error_from_errno("strdup");
4467 goto done;
4472 if (do_list) {
4473 error = got_repo_open(&repo, repo_path, NULL);
4474 if (error != NULL)
4475 goto done;
4476 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4477 if (error)
4478 goto done;
4479 error = list_tags(repo, worktree);
4480 } else {
4481 error = get_gitconfig_path(&gitconfig_path);
4482 if (error)
4483 goto done;
4484 error = got_repo_open(&repo, repo_path, gitconfig_path);
4485 if (error != NULL)
4486 goto done;
4488 if (tagmsg) {
4489 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4490 if (error)
4491 goto done;
4494 if (commit_id_arg == NULL) {
4495 struct got_reference *head_ref;
4496 struct got_object_id *commit_id;
4497 error = got_ref_open(&head_ref, repo,
4498 worktree ? got_worktree_get_head_ref_name(worktree)
4499 : GOT_REF_HEAD, 0);
4500 if (error)
4501 goto done;
4502 error = got_ref_resolve(&commit_id, repo, head_ref);
4503 got_ref_close(head_ref);
4504 if (error)
4505 goto done;
4506 error = got_object_id_str(&commit_id_str, commit_id);
4507 free(commit_id);
4508 if (error)
4509 goto done;
4512 error = add_tag(repo, tag_name,
4513 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4515 done:
4516 if (repo)
4517 got_repo_close(repo);
4518 if (worktree)
4519 got_worktree_close(worktree);
4520 free(cwd);
4521 free(repo_path);
4522 free(gitconfig_path);
4523 free(commit_id_str);
4524 return error;
4527 __dead static void
4528 usage_add(void)
4530 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4531 getprogname());
4532 exit(1);
4535 static const struct got_error *
4536 add_progress(void *arg, unsigned char status, const char *path)
4538 while (path[0] == '/')
4539 path++;
4540 printf("%c %s\n", status, path);
4541 return NULL;
4544 static const struct got_error *
4545 cmd_add(int argc, char *argv[])
4547 const struct got_error *error = NULL;
4548 struct got_repository *repo = NULL;
4549 struct got_worktree *worktree = NULL;
4550 char *cwd = NULL;
4551 struct got_pathlist_head paths;
4552 struct got_pathlist_entry *pe;
4553 int ch, can_recurse = 0, no_ignores = 0;
4555 TAILQ_INIT(&paths);
4557 while ((ch = getopt(argc, argv, "IR")) != -1) {
4558 switch (ch) {
4559 case 'I':
4560 no_ignores = 1;
4561 break;
4562 case 'R':
4563 can_recurse = 1;
4564 break;
4565 default:
4566 usage_add();
4567 /* NOTREACHED */
4571 argc -= optind;
4572 argv += optind;
4574 #ifndef PROFILE
4575 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4576 NULL) == -1)
4577 err(1, "pledge");
4578 #endif
4579 if (argc < 1)
4580 usage_add();
4582 cwd = getcwd(NULL, 0);
4583 if (cwd == NULL) {
4584 error = got_error_from_errno("getcwd");
4585 goto done;
4588 error = got_worktree_open(&worktree, cwd);
4589 if (error)
4590 goto done;
4592 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4593 NULL);
4594 if (error != NULL)
4595 goto done;
4597 error = apply_unveil(got_repo_get_path(repo), 1,
4598 got_worktree_get_root_path(worktree));
4599 if (error)
4600 goto done;
4602 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4603 if (error)
4604 goto done;
4606 if (!can_recurse && no_ignores) {
4607 error = got_error_msg(GOT_ERR_BAD_PATH,
4608 "disregarding ignores requires -R option");
4609 goto done;
4613 if (!can_recurse) {
4614 char *ondisk_path;
4615 struct stat sb;
4616 TAILQ_FOREACH(pe, &paths, entry) {
4617 if (asprintf(&ondisk_path, "%s/%s",
4618 got_worktree_get_root_path(worktree),
4619 pe->path) == -1) {
4620 error = got_error_from_errno("asprintf");
4621 goto done;
4623 if (lstat(ondisk_path, &sb) == -1) {
4624 if (errno == ENOENT) {
4625 free(ondisk_path);
4626 continue;
4628 error = got_error_from_errno2("lstat",
4629 ondisk_path);
4630 free(ondisk_path);
4631 goto done;
4633 free(ondisk_path);
4634 if (S_ISDIR(sb.st_mode)) {
4635 error = got_error_msg(GOT_ERR_BAD_PATH,
4636 "adding directories requires -R option");
4637 goto done;
4642 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4643 NULL, repo, no_ignores);
4644 done:
4645 if (repo)
4646 got_repo_close(repo);
4647 if (worktree)
4648 got_worktree_close(worktree);
4649 TAILQ_FOREACH(pe, &paths, entry)
4650 free((char *)pe->path);
4651 got_pathlist_free(&paths);
4652 free(cwd);
4653 return error;
4656 __dead static void
4657 usage_remove(void)
4659 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4660 getprogname());
4661 exit(1);
4664 static const struct got_error *
4665 print_remove_status(void *arg, unsigned char status,
4666 unsigned char staged_status, const char *path)
4668 while (path[0] == '/')
4669 path++;
4670 if (status == GOT_STATUS_NONEXISTENT)
4671 return NULL;
4672 if (status == staged_status && (status == GOT_STATUS_DELETE))
4673 status = GOT_STATUS_NO_CHANGE;
4674 printf("%c%c %s\n", status, staged_status, path);
4675 return NULL;
4678 static const struct got_error *
4679 cmd_remove(int argc, char *argv[])
4681 const struct got_error *error = NULL;
4682 struct got_worktree *worktree = NULL;
4683 struct got_repository *repo = NULL;
4684 char *cwd = NULL;
4685 struct got_pathlist_head paths;
4686 struct got_pathlist_entry *pe;
4687 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4689 TAILQ_INIT(&paths);
4691 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4692 switch (ch) {
4693 case 'f':
4694 delete_local_mods = 1;
4695 break;
4696 case 'k':
4697 keep_on_disk = 1;
4698 break;
4699 case 'R':
4700 can_recurse = 1;
4701 break;
4702 default:
4703 usage_remove();
4704 /* NOTREACHED */
4708 argc -= optind;
4709 argv += optind;
4711 #ifndef PROFILE
4712 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4713 NULL) == -1)
4714 err(1, "pledge");
4715 #endif
4716 if (argc < 1)
4717 usage_remove();
4719 cwd = getcwd(NULL, 0);
4720 if (cwd == NULL) {
4721 error = got_error_from_errno("getcwd");
4722 goto done;
4724 error = got_worktree_open(&worktree, cwd);
4725 if (error)
4726 goto done;
4728 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4729 NULL);
4730 if (error)
4731 goto done;
4733 error = apply_unveil(got_repo_get_path(repo), 1,
4734 got_worktree_get_root_path(worktree));
4735 if (error)
4736 goto done;
4738 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4739 if (error)
4740 goto done;
4742 if (!can_recurse) {
4743 char *ondisk_path;
4744 struct stat sb;
4745 TAILQ_FOREACH(pe, &paths, entry) {
4746 if (asprintf(&ondisk_path, "%s/%s",
4747 got_worktree_get_root_path(worktree),
4748 pe->path) == -1) {
4749 error = got_error_from_errno("asprintf");
4750 goto done;
4752 if (lstat(ondisk_path, &sb) == -1) {
4753 if (errno == ENOENT) {
4754 free(ondisk_path);
4755 continue;
4757 error = got_error_from_errno2("lstat",
4758 ondisk_path);
4759 free(ondisk_path);
4760 goto done;
4762 free(ondisk_path);
4763 if (S_ISDIR(sb.st_mode)) {
4764 error = got_error_msg(GOT_ERR_BAD_PATH,
4765 "removing directories requires -R option");
4766 goto done;
4771 error = got_worktree_schedule_delete(worktree, &paths,
4772 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4773 done:
4774 if (repo)
4775 got_repo_close(repo);
4776 if (worktree)
4777 got_worktree_close(worktree);
4778 TAILQ_FOREACH(pe, &paths, entry)
4779 free((char *)pe->path);
4780 got_pathlist_free(&paths);
4781 free(cwd);
4782 return error;
4785 __dead static void
4786 usage_revert(void)
4788 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4789 "path ...\n", getprogname());
4790 exit(1);
4793 static const struct got_error *
4794 revert_progress(void *arg, unsigned char status, const char *path)
4796 if (status == GOT_STATUS_UNVERSIONED)
4797 return NULL;
4799 while (path[0] == '/')
4800 path++;
4801 printf("%c %s\n", status, path);
4802 return NULL;
4805 struct choose_patch_arg {
4806 FILE *patch_script_file;
4807 const char *action;
4810 static const struct got_error *
4811 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4812 int nchanges, const char *action)
4814 char *line = NULL;
4815 size_t linesize = 0;
4816 ssize_t linelen;
4818 switch (status) {
4819 case GOT_STATUS_ADD:
4820 printf("A %s\n%s this addition? [y/n] ", path, action);
4821 break;
4822 case GOT_STATUS_DELETE:
4823 printf("D %s\n%s this deletion? [y/n] ", path, action);
4824 break;
4825 case GOT_STATUS_MODIFY:
4826 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4827 return got_error_from_errno("fseek");
4828 printf(GOT_COMMIT_SEP_STR);
4829 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4830 printf("%s", line);
4831 if (ferror(patch_file))
4832 return got_error_from_errno("getline");
4833 printf(GOT_COMMIT_SEP_STR);
4834 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4835 path, n, nchanges, action);
4836 break;
4837 default:
4838 return got_error_path(path, GOT_ERR_FILE_STATUS);
4841 return NULL;
4844 static const struct got_error *
4845 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4846 FILE *patch_file, int n, int nchanges)
4848 const struct got_error *err = NULL;
4849 char *line = NULL;
4850 size_t linesize = 0;
4851 ssize_t linelen;
4852 int resp = ' ';
4853 struct choose_patch_arg *a = arg;
4855 *choice = GOT_PATCH_CHOICE_NONE;
4857 if (a->patch_script_file) {
4858 char *nl;
4859 err = show_change(status, path, patch_file, n, nchanges,
4860 a->action);
4861 if (err)
4862 return err;
4863 linelen = getline(&line, &linesize, a->patch_script_file);
4864 if (linelen == -1) {
4865 if (ferror(a->patch_script_file))
4866 return got_error_from_errno("getline");
4867 return NULL;
4869 nl = strchr(line, '\n');
4870 if (nl)
4871 *nl = '\0';
4872 if (strcmp(line, "y") == 0) {
4873 *choice = GOT_PATCH_CHOICE_YES;
4874 printf("y\n");
4875 } else if (strcmp(line, "n") == 0) {
4876 *choice = GOT_PATCH_CHOICE_NO;
4877 printf("n\n");
4878 } else if (strcmp(line, "q") == 0 &&
4879 status == GOT_STATUS_MODIFY) {
4880 *choice = GOT_PATCH_CHOICE_QUIT;
4881 printf("q\n");
4882 } else
4883 printf("invalid response '%s'\n", line);
4884 free(line);
4885 return NULL;
4888 while (resp != 'y' && resp != 'n' && resp != 'q') {
4889 err = show_change(status, path, patch_file, n, nchanges,
4890 a->action);
4891 if (err)
4892 return err;
4893 resp = getchar();
4894 if (resp == '\n')
4895 resp = getchar();
4896 if (status == GOT_STATUS_MODIFY) {
4897 if (resp != 'y' && resp != 'n' && resp != 'q') {
4898 printf("invalid response '%c'\n", resp);
4899 resp = ' ';
4901 } else if (resp != 'y' && resp != 'n') {
4902 printf("invalid response '%c'\n", resp);
4903 resp = ' ';
4907 if (resp == 'y')
4908 *choice = GOT_PATCH_CHOICE_YES;
4909 else if (resp == 'n')
4910 *choice = GOT_PATCH_CHOICE_NO;
4911 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4912 *choice = GOT_PATCH_CHOICE_QUIT;
4914 return NULL;
4918 static const struct got_error *
4919 cmd_revert(int argc, char *argv[])
4921 const struct got_error *error = NULL;
4922 struct got_worktree *worktree = NULL;
4923 struct got_repository *repo = NULL;
4924 char *cwd = NULL, *path = NULL;
4925 struct got_pathlist_head paths;
4926 struct got_pathlist_entry *pe;
4927 int ch, can_recurse = 0, pflag = 0;
4928 FILE *patch_script_file = NULL;
4929 const char *patch_script_path = NULL;
4930 struct choose_patch_arg cpa;
4932 TAILQ_INIT(&paths);
4934 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4935 switch (ch) {
4936 case 'p':
4937 pflag = 1;
4938 break;
4939 case 'F':
4940 patch_script_path = optarg;
4941 break;
4942 case 'R':
4943 can_recurse = 1;
4944 break;
4945 default:
4946 usage_revert();
4947 /* NOTREACHED */
4951 argc -= optind;
4952 argv += optind;
4954 #ifndef PROFILE
4955 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4956 "unveil", NULL) == -1)
4957 err(1, "pledge");
4958 #endif
4959 if (argc < 1)
4960 usage_revert();
4961 if (patch_script_path && !pflag)
4962 errx(1, "-F option can only be used together with -p option");
4964 cwd = getcwd(NULL, 0);
4965 if (cwd == NULL) {
4966 error = got_error_from_errno("getcwd");
4967 goto done;
4969 error = got_worktree_open(&worktree, cwd);
4970 if (error)
4971 goto done;
4973 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4974 NULL);
4975 if (error != NULL)
4976 goto done;
4978 if (patch_script_path) {
4979 patch_script_file = fopen(patch_script_path, "r");
4980 if (patch_script_file == NULL) {
4981 error = got_error_from_errno2("fopen",
4982 patch_script_path);
4983 goto done;
4986 error = apply_unveil(got_repo_get_path(repo), 1,
4987 got_worktree_get_root_path(worktree));
4988 if (error)
4989 goto done;
4991 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4992 if (error)
4993 goto done;
4995 if (!can_recurse) {
4996 char *ondisk_path;
4997 struct stat sb;
4998 TAILQ_FOREACH(pe, &paths, entry) {
4999 if (asprintf(&ondisk_path, "%s/%s",
5000 got_worktree_get_root_path(worktree),
5001 pe->path) == -1) {
5002 error = got_error_from_errno("asprintf");
5003 goto done;
5005 if (lstat(ondisk_path, &sb) == -1) {
5006 if (errno == ENOENT) {
5007 free(ondisk_path);
5008 continue;
5010 error = got_error_from_errno2("lstat",
5011 ondisk_path);
5012 free(ondisk_path);
5013 goto done;
5015 free(ondisk_path);
5016 if (S_ISDIR(sb.st_mode)) {
5017 error = got_error_msg(GOT_ERR_BAD_PATH,
5018 "reverting directories requires -R option");
5019 goto done;
5024 cpa.patch_script_file = patch_script_file;
5025 cpa.action = "revert";
5026 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5027 pflag ? choose_patch : NULL, &cpa, repo);
5028 done:
5029 if (patch_script_file && fclose(patch_script_file) == EOF &&
5030 error == NULL)
5031 error = got_error_from_errno2("fclose", patch_script_path);
5032 if (repo)
5033 got_repo_close(repo);
5034 if (worktree)
5035 got_worktree_close(worktree);
5036 free(path);
5037 free(cwd);
5038 return error;
5041 __dead static void
5042 usage_commit(void)
5044 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5045 getprogname());
5046 exit(1);
5049 struct collect_commit_logmsg_arg {
5050 const char *cmdline_log;
5051 const char *editor;
5052 const char *worktree_path;
5053 const char *branch_name;
5054 const char *repo_path;
5055 char *logmsg_path;
5059 static const struct got_error *
5060 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5061 void *arg)
5063 char *initial_content = NULL;
5064 struct got_pathlist_entry *pe;
5065 const struct got_error *err = NULL;
5066 char *template = NULL;
5067 struct collect_commit_logmsg_arg *a = arg;
5068 int fd;
5069 size_t len;
5071 /* if a message was specified on the command line, just use it */
5072 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5073 len = strlen(a->cmdline_log) + 1;
5074 *logmsg = malloc(len + 1);
5075 if (*logmsg == NULL)
5076 return got_error_from_errno("malloc");
5077 strlcpy(*logmsg, a->cmdline_log, len);
5078 return NULL;
5081 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5082 return got_error_from_errno("asprintf");
5084 if (asprintf(&initial_content,
5085 "\n# changes to be committed on branch %s:\n",
5086 a->branch_name) == -1)
5087 return got_error_from_errno("asprintf");
5089 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5090 if (err)
5091 goto done;
5093 dprintf(fd, initial_content);
5095 TAILQ_FOREACH(pe, commitable_paths, entry) {
5096 struct got_commitable *ct = pe->data;
5097 dprintf(fd, "# %c %s\n",
5098 got_commitable_get_status(ct),
5099 got_commitable_get_path(ct));
5101 close(fd);
5103 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5104 done:
5105 free(initial_content);
5106 free(template);
5108 /* Editor is done; we can now apply unveil(2) */
5109 if (err == NULL) {
5110 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5111 if (err) {
5112 free(*logmsg);
5113 *logmsg = NULL;
5116 return err;
5119 static const struct got_error *
5120 cmd_commit(int argc, char *argv[])
5122 const struct got_error *error = NULL;
5123 struct got_worktree *worktree = NULL;
5124 struct got_repository *repo = NULL;
5125 char *cwd = NULL, *id_str = NULL;
5126 struct got_object_id *id = NULL;
5127 const char *logmsg = NULL;
5128 struct collect_commit_logmsg_arg cl_arg;
5129 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5130 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5131 struct got_pathlist_head paths;
5133 TAILQ_INIT(&paths);
5134 cl_arg.logmsg_path = NULL;
5136 while ((ch = getopt(argc, argv, "m:")) != -1) {
5137 switch (ch) {
5138 case 'm':
5139 logmsg = optarg;
5140 break;
5141 default:
5142 usage_commit();
5143 /* NOTREACHED */
5147 argc -= optind;
5148 argv += optind;
5150 #ifndef PROFILE
5151 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5152 "unveil", NULL) == -1)
5153 err(1, "pledge");
5154 #endif
5155 cwd = getcwd(NULL, 0);
5156 if (cwd == NULL) {
5157 error = got_error_from_errno("getcwd");
5158 goto done;
5160 error = got_worktree_open(&worktree, cwd);
5161 if (error)
5162 goto done;
5164 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5165 if (error)
5166 goto done;
5167 if (rebase_in_progress) {
5168 error = got_error(GOT_ERR_REBASING);
5169 goto done;
5172 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5173 worktree);
5174 if (error)
5175 goto done;
5177 error = get_gitconfig_path(&gitconfig_path);
5178 if (error)
5179 goto done;
5180 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5181 gitconfig_path);
5182 if (error != NULL)
5183 goto done;
5185 error = get_author(&author, repo);
5186 if (error)
5187 return error;
5190 * unveil(2) traverses exec(2); if an editor is used we have
5191 * to apply unveil after the log message has been written.
5193 if (logmsg == NULL || strlen(logmsg) == 0)
5194 error = get_editor(&editor);
5195 else
5196 error = apply_unveil(got_repo_get_path(repo), 0,
5197 got_worktree_get_root_path(worktree));
5198 if (error)
5199 goto done;
5201 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5202 if (error)
5203 goto done;
5205 cl_arg.editor = editor;
5206 cl_arg.cmdline_log = logmsg;
5207 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5208 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5209 if (!histedit_in_progress) {
5210 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5211 error = got_error(GOT_ERR_COMMIT_BRANCH);
5212 goto done;
5214 cl_arg.branch_name += 11;
5216 cl_arg.repo_path = got_repo_get_path(repo);
5217 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5218 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5219 if (error) {
5220 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5221 cl_arg.logmsg_path != NULL)
5222 preserve_logmsg = 1;
5223 goto done;
5226 error = got_object_id_str(&id_str, id);
5227 if (error)
5228 goto done;
5229 printf("Created commit %s\n", id_str);
5230 done:
5231 if (preserve_logmsg) {
5232 fprintf(stderr, "%s: log message preserved in %s\n",
5233 getprogname(), cl_arg.logmsg_path);
5234 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5235 error == NULL)
5236 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5237 free(cl_arg.logmsg_path);
5238 if (repo)
5239 got_repo_close(repo);
5240 if (worktree)
5241 got_worktree_close(worktree);
5242 free(cwd);
5243 free(id_str);
5244 free(gitconfig_path);
5245 free(editor);
5246 free(author);
5247 return error;
5250 __dead static void
5251 usage_cherrypick(void)
5253 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5254 exit(1);
5257 static const struct got_error *
5258 cmd_cherrypick(int argc, char *argv[])
5260 const struct got_error *error = NULL;
5261 struct got_worktree *worktree = NULL;
5262 struct got_repository *repo = NULL;
5263 char *cwd = NULL, *commit_id_str = NULL;
5264 struct got_object_id *commit_id = NULL;
5265 struct got_commit_object *commit = NULL;
5266 struct got_object_qid *pid;
5267 struct got_reference *head_ref = NULL;
5268 int ch, did_something = 0;
5270 while ((ch = getopt(argc, argv, "")) != -1) {
5271 switch (ch) {
5272 default:
5273 usage_cherrypick();
5274 /* NOTREACHED */
5278 argc -= optind;
5279 argv += optind;
5281 #ifndef PROFILE
5282 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5283 "unveil", NULL) == -1)
5284 err(1, "pledge");
5285 #endif
5286 if (argc != 1)
5287 usage_cherrypick();
5289 cwd = getcwd(NULL, 0);
5290 if (cwd == NULL) {
5291 error = got_error_from_errno("getcwd");
5292 goto done;
5294 error = got_worktree_open(&worktree, cwd);
5295 if (error)
5296 goto done;
5298 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5299 NULL);
5300 if (error != NULL)
5301 goto done;
5303 error = apply_unveil(got_repo_get_path(repo), 0,
5304 got_worktree_get_root_path(worktree));
5305 if (error)
5306 goto done;
5308 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5309 GOT_OBJ_TYPE_COMMIT, repo);
5310 if (error != NULL) {
5311 struct got_reference *ref;
5312 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5313 goto done;
5314 error = got_ref_open(&ref, repo, argv[0], 0);
5315 if (error != NULL)
5316 goto done;
5317 error = got_ref_resolve(&commit_id, repo, ref);
5318 got_ref_close(ref);
5319 if (error != NULL)
5320 goto done;
5322 error = got_object_id_str(&commit_id_str, commit_id);
5323 if (error)
5324 goto done;
5326 error = got_ref_open(&head_ref, repo,
5327 got_worktree_get_head_ref_name(worktree), 0);
5328 if (error != NULL)
5329 goto done;
5331 error = check_same_branch(commit_id, head_ref, NULL, repo);
5332 if (error) {
5333 if (error->code != GOT_ERR_ANCESTRY)
5334 goto done;
5335 error = NULL;
5336 } else {
5337 error = got_error(GOT_ERR_SAME_BRANCH);
5338 goto done;
5341 error = got_object_open_as_commit(&commit, repo, commit_id);
5342 if (error)
5343 goto done;
5344 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5345 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5346 commit_id, repo, update_progress, &did_something, check_cancelled,
5347 NULL);
5348 if (error != NULL)
5349 goto done;
5351 if (did_something)
5352 printf("Merged commit %s\n", commit_id_str);
5353 done:
5354 if (commit)
5355 got_object_commit_close(commit);
5356 free(commit_id_str);
5357 if (head_ref)
5358 got_ref_close(head_ref);
5359 if (worktree)
5360 got_worktree_close(worktree);
5361 if (repo)
5362 got_repo_close(repo);
5363 return error;
5366 __dead static void
5367 usage_backout(void)
5369 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5370 exit(1);
5373 static const struct got_error *
5374 cmd_backout(int argc, char *argv[])
5376 const struct got_error *error = NULL;
5377 struct got_worktree *worktree = NULL;
5378 struct got_repository *repo = NULL;
5379 char *cwd = NULL, *commit_id_str = NULL;
5380 struct got_object_id *commit_id = NULL;
5381 struct got_commit_object *commit = NULL;
5382 struct got_object_qid *pid;
5383 struct got_reference *head_ref = NULL;
5384 int ch, did_something = 0;
5386 while ((ch = getopt(argc, argv, "")) != -1) {
5387 switch (ch) {
5388 default:
5389 usage_backout();
5390 /* NOTREACHED */
5394 argc -= optind;
5395 argv += optind;
5397 #ifndef PROFILE
5398 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5399 "unveil", NULL) == -1)
5400 err(1, "pledge");
5401 #endif
5402 if (argc != 1)
5403 usage_backout();
5405 cwd = getcwd(NULL, 0);
5406 if (cwd == NULL) {
5407 error = got_error_from_errno("getcwd");
5408 goto done;
5410 error = got_worktree_open(&worktree, cwd);
5411 if (error)
5412 goto done;
5414 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5415 NULL);
5416 if (error != NULL)
5417 goto done;
5419 error = apply_unveil(got_repo_get_path(repo), 0,
5420 got_worktree_get_root_path(worktree));
5421 if (error)
5422 goto done;
5424 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5425 GOT_OBJ_TYPE_COMMIT, repo);
5426 if (error != NULL) {
5427 struct got_reference *ref;
5428 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5429 goto done;
5430 error = got_ref_open(&ref, repo, argv[0], 0);
5431 if (error != NULL)
5432 goto done;
5433 error = got_ref_resolve(&commit_id, repo, ref);
5434 got_ref_close(ref);
5435 if (error != NULL)
5436 goto done;
5438 error = got_object_id_str(&commit_id_str, commit_id);
5439 if (error)
5440 goto done;
5442 error = got_ref_open(&head_ref, repo,
5443 got_worktree_get_head_ref_name(worktree), 0);
5444 if (error != NULL)
5445 goto done;
5447 error = check_same_branch(commit_id, head_ref, NULL, repo);
5448 if (error)
5449 goto done;
5451 error = got_object_open_as_commit(&commit, repo, commit_id);
5452 if (error)
5453 goto done;
5454 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5455 if (pid == NULL) {
5456 error = got_error(GOT_ERR_ROOT_COMMIT);
5457 goto done;
5460 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5461 update_progress, &did_something, check_cancelled, NULL);
5462 if (error != NULL)
5463 goto done;
5465 if (did_something)
5466 printf("Backed out commit %s\n", commit_id_str);
5467 done:
5468 if (commit)
5469 got_object_commit_close(commit);
5470 free(commit_id_str);
5471 if (head_ref)
5472 got_ref_close(head_ref);
5473 if (worktree)
5474 got_worktree_close(worktree);
5475 if (repo)
5476 got_repo_close(repo);
5477 return error;
5480 __dead static void
5481 usage_rebase(void)
5483 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5484 getprogname());
5485 exit(1);
5488 void
5489 trim_logmsg(char *logmsg, int limit)
5491 char *nl;
5492 size_t len;
5494 len = strlen(logmsg);
5495 if (len > limit)
5496 len = limit;
5497 logmsg[len] = '\0';
5498 nl = strchr(logmsg, '\n');
5499 if (nl)
5500 *nl = '\0';
5503 static const struct got_error *
5504 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5506 const struct got_error *err;
5507 char *logmsg0 = NULL;
5508 const char *s;
5510 err = got_object_commit_get_logmsg(&logmsg0, commit);
5511 if (err)
5512 return err;
5514 s = logmsg0;
5515 while (isspace((unsigned char)s[0]))
5516 s++;
5518 *logmsg = strdup(s);
5519 if (*logmsg == NULL) {
5520 err = got_error_from_errno("strdup");
5521 goto done;
5524 trim_logmsg(*logmsg, limit);
5525 done:
5526 free(logmsg0);
5527 return err;
5530 static const struct got_error *
5531 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5533 const struct got_error *err;
5534 struct got_commit_object *commit = NULL;
5535 char *id_str = NULL, *logmsg = NULL;
5537 err = got_object_open_as_commit(&commit, repo, id);
5538 if (err)
5539 return err;
5541 err = got_object_id_str(&id_str, id);
5542 if (err)
5543 goto done;
5545 id_str[12] = '\0';
5547 err = get_short_logmsg(&logmsg, 42, commit);
5548 if (err)
5549 goto done;
5551 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5552 done:
5553 free(id_str);
5554 got_object_commit_close(commit);
5555 free(logmsg);
5556 return err;
5559 static const struct got_error *
5560 show_rebase_progress(struct got_commit_object *commit,
5561 struct got_object_id *old_id, struct got_object_id *new_id)
5563 const struct got_error *err;
5564 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5566 err = got_object_id_str(&old_id_str, old_id);
5567 if (err)
5568 goto done;
5570 if (new_id) {
5571 err = got_object_id_str(&new_id_str, new_id);
5572 if (err)
5573 goto done;
5576 old_id_str[12] = '\0';
5577 if (new_id_str)
5578 new_id_str[12] = '\0';
5580 err = get_short_logmsg(&logmsg, 42, commit);
5581 if (err)
5582 goto done;
5584 printf("%s -> %s: %s\n", old_id_str,
5585 new_id_str ? new_id_str : "no-op change", logmsg);
5586 done:
5587 free(old_id_str);
5588 free(new_id_str);
5589 free(logmsg);
5590 return err;
5593 static const struct got_error *
5594 rebase_progress(void *arg, unsigned char status, const char *path)
5596 unsigned char *rebase_status = arg;
5598 while (path[0] == '/')
5599 path++;
5600 printf("%c %s\n", status, path);
5602 if (*rebase_status == GOT_STATUS_CONFLICT)
5603 return NULL;
5604 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5605 *rebase_status = status;
5606 return NULL;
5609 static const struct got_error *
5610 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5611 struct got_reference *branch, struct got_reference *new_base_branch,
5612 struct got_reference *tmp_branch, struct got_repository *repo)
5614 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5615 return got_worktree_rebase_complete(worktree, fileindex,
5616 new_base_branch, tmp_branch, branch, repo);
5619 static const struct got_error *
5620 rebase_commit(struct got_pathlist_head *merged_paths,
5621 struct got_worktree *worktree, struct got_fileindex *fileindex,
5622 struct got_reference *tmp_branch,
5623 struct got_object_id *commit_id, struct got_repository *repo)
5625 const struct got_error *error;
5626 struct got_commit_object *commit;
5627 struct got_object_id *new_commit_id;
5629 error = got_object_open_as_commit(&commit, repo, commit_id);
5630 if (error)
5631 return error;
5633 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5634 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5635 if (error) {
5636 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5637 goto done;
5638 error = show_rebase_progress(commit, commit_id, NULL);
5639 } else {
5640 error = show_rebase_progress(commit, commit_id, new_commit_id);
5641 free(new_commit_id);
5643 done:
5644 got_object_commit_close(commit);
5645 return error;
5648 struct check_path_prefix_arg {
5649 const char *path_prefix;
5650 size_t len;
5651 int errcode;
5654 static const struct got_error *
5655 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5656 struct got_blob_object *blob2, struct got_object_id *id1,
5657 struct got_object_id *id2, const char *path1, const char *path2,
5658 mode_t mode1, mode_t mode2, struct got_repository *repo)
5660 struct check_path_prefix_arg *a = arg;
5662 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5663 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5664 return got_error(a->errcode);
5666 return NULL;
5669 static const struct got_error *
5670 check_path_prefix(struct got_object_id *parent_id,
5671 struct got_object_id *commit_id, const char *path_prefix,
5672 int errcode, struct got_repository *repo)
5674 const struct got_error *err;
5675 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5676 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5677 struct check_path_prefix_arg cpp_arg;
5679 if (got_path_is_root_dir(path_prefix))
5680 return NULL;
5682 err = got_object_open_as_commit(&commit, repo, commit_id);
5683 if (err)
5684 goto done;
5686 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5687 if (err)
5688 goto done;
5690 err = got_object_open_as_tree(&tree1, repo,
5691 got_object_commit_get_tree_id(parent_commit));
5692 if (err)
5693 goto done;
5695 err = got_object_open_as_tree(&tree2, repo,
5696 got_object_commit_get_tree_id(commit));
5697 if (err)
5698 goto done;
5700 cpp_arg.path_prefix = path_prefix;
5701 while (cpp_arg.path_prefix[0] == '/')
5702 cpp_arg.path_prefix++;
5703 cpp_arg.len = strlen(cpp_arg.path_prefix);
5704 cpp_arg.errcode = errcode;
5705 err = got_diff_tree(tree1, tree2, "", "", repo,
5706 check_path_prefix_in_diff, &cpp_arg, 0);
5707 done:
5708 if (tree1)
5709 got_object_tree_close(tree1);
5710 if (tree2)
5711 got_object_tree_close(tree2);
5712 if (commit)
5713 got_object_commit_close(commit);
5714 if (parent_commit)
5715 got_object_commit_close(parent_commit);
5716 return err;
5719 static const struct got_error *
5720 collect_commits(struct got_object_id_queue *commits,
5721 struct got_object_id *initial_commit_id,
5722 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5723 const char *path_prefix, int path_prefix_errcode,
5724 struct got_repository *repo)
5726 const struct got_error *err = NULL;
5727 struct got_commit_graph *graph = NULL;
5728 struct got_object_id *parent_id = NULL;
5729 struct got_object_qid *qid;
5730 struct got_object_id *commit_id = initial_commit_id;
5732 err = got_commit_graph_open(&graph, "/", 1);
5733 if (err)
5734 return err;
5736 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5737 check_cancelled, NULL);
5738 if (err)
5739 goto done;
5740 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5741 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5742 check_cancelled, NULL);
5743 if (err) {
5744 if (err->code == GOT_ERR_ITER_COMPLETED) {
5745 err = got_error_msg(GOT_ERR_ANCESTRY,
5746 "ran out of commits to rebase before "
5747 "youngest common ancestor commit has "
5748 "been reached?!?");
5750 goto done;
5751 } else {
5752 err = check_path_prefix(parent_id, commit_id,
5753 path_prefix, path_prefix_errcode, repo);
5754 if (err)
5755 goto done;
5757 err = got_object_qid_alloc(&qid, commit_id);
5758 if (err)
5759 goto done;
5760 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5761 commit_id = parent_id;
5764 done:
5765 got_commit_graph_close(graph);
5766 return err;
5769 static const struct got_error *
5770 cmd_rebase(int argc, char *argv[])
5772 const struct got_error *error = NULL;
5773 struct got_worktree *worktree = NULL;
5774 struct got_repository *repo = NULL;
5775 struct got_fileindex *fileindex = NULL;
5776 char *cwd = NULL;
5777 struct got_reference *branch = NULL;
5778 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5779 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5780 struct got_object_id *resume_commit_id = NULL;
5781 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5782 struct got_commit_object *commit = NULL;
5783 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5784 int histedit_in_progress = 0;
5785 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5786 struct got_object_id_queue commits;
5787 struct got_pathlist_head merged_paths;
5788 const struct got_object_id_queue *parent_ids;
5789 struct got_object_qid *qid, *pid;
5791 SIMPLEQ_INIT(&commits);
5792 TAILQ_INIT(&merged_paths);
5794 while ((ch = getopt(argc, argv, "ac")) != -1) {
5795 switch (ch) {
5796 case 'a':
5797 abort_rebase = 1;
5798 break;
5799 case 'c':
5800 continue_rebase = 1;
5801 break;
5802 default:
5803 usage_rebase();
5804 /* NOTREACHED */
5808 argc -= optind;
5809 argv += optind;
5811 #ifndef PROFILE
5812 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5813 "unveil", NULL) == -1)
5814 err(1, "pledge");
5815 #endif
5816 if (abort_rebase && continue_rebase)
5817 usage_rebase();
5818 else if (abort_rebase || continue_rebase) {
5819 if (argc != 0)
5820 usage_rebase();
5821 } else if (argc != 1)
5822 usage_rebase();
5824 cwd = getcwd(NULL, 0);
5825 if (cwd == NULL) {
5826 error = got_error_from_errno("getcwd");
5827 goto done;
5829 error = got_worktree_open(&worktree, cwd);
5830 if (error)
5831 goto done;
5833 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5834 NULL);
5835 if (error != NULL)
5836 goto done;
5838 error = apply_unveil(got_repo_get_path(repo), 0,
5839 got_worktree_get_root_path(worktree));
5840 if (error)
5841 goto done;
5843 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5844 worktree);
5845 if (error)
5846 goto done;
5847 if (histedit_in_progress) {
5848 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5849 goto done;
5852 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5853 if (error)
5854 goto done;
5856 if (abort_rebase) {
5857 int did_something;
5858 if (!rebase_in_progress) {
5859 error = got_error(GOT_ERR_NOT_REBASING);
5860 goto done;
5862 error = got_worktree_rebase_continue(&resume_commit_id,
5863 &new_base_branch, &tmp_branch, &branch, &fileindex,
5864 worktree, repo);
5865 if (error)
5866 goto done;
5867 printf("Switching work tree to %s\n",
5868 got_ref_get_symref_target(new_base_branch));
5869 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5870 new_base_branch, update_progress, &did_something);
5871 if (error)
5872 goto done;
5873 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5874 goto done; /* nothing else to do */
5877 if (continue_rebase) {
5878 if (!rebase_in_progress) {
5879 error = got_error(GOT_ERR_NOT_REBASING);
5880 goto done;
5882 error = got_worktree_rebase_continue(&resume_commit_id,
5883 &new_base_branch, &tmp_branch, &branch, &fileindex,
5884 worktree, repo);
5885 if (error)
5886 goto done;
5888 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5889 resume_commit_id, repo);
5890 if (error)
5891 goto done;
5893 yca_id = got_object_id_dup(resume_commit_id);
5894 if (yca_id == NULL) {
5895 error = got_error_from_errno("got_object_id_dup");
5896 goto done;
5898 } else {
5899 error = got_ref_open(&branch, repo, argv[0], 0);
5900 if (error != NULL)
5901 goto done;
5904 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5905 if (error)
5906 goto done;
5908 if (!continue_rebase) {
5909 struct got_object_id *base_commit_id;
5911 base_commit_id = got_worktree_get_base_commit_id(worktree);
5912 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5913 base_commit_id, branch_head_commit_id, repo,
5914 check_cancelled, NULL);
5915 if (error)
5916 goto done;
5917 if (yca_id == NULL) {
5918 error = got_error_msg(GOT_ERR_ANCESTRY,
5919 "specified branch shares no common ancestry "
5920 "with work tree's branch");
5921 goto done;
5924 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5925 if (error) {
5926 if (error->code != GOT_ERR_ANCESTRY)
5927 goto done;
5928 error = NULL;
5929 } else {
5930 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5931 "specified branch resolves to a commit which "
5932 "is already contained in work tree's branch");
5933 goto done;
5935 error = got_worktree_rebase_prepare(&new_base_branch,
5936 &tmp_branch, &fileindex, worktree, branch, repo);
5937 if (error)
5938 goto done;
5941 commit_id = branch_head_commit_id;
5942 error = got_object_open_as_commit(&commit, repo, commit_id);
5943 if (error)
5944 goto done;
5946 parent_ids = got_object_commit_get_parent_ids(commit);
5947 pid = SIMPLEQ_FIRST(parent_ids);
5948 if (pid == NULL) {
5949 if (!continue_rebase) {
5950 int did_something;
5951 error = got_worktree_rebase_abort(worktree, fileindex,
5952 repo, new_base_branch, update_progress,
5953 &did_something);
5954 if (error)
5955 goto done;
5956 printf("Rebase of %s aborted\n",
5957 got_ref_get_name(branch));
5959 error = got_error(GOT_ERR_EMPTY_REBASE);
5960 goto done;
5962 error = collect_commits(&commits, commit_id, pid->id,
5963 yca_id, got_worktree_get_path_prefix(worktree),
5964 GOT_ERR_REBASE_PATH, repo);
5965 got_object_commit_close(commit);
5966 commit = NULL;
5967 if (error)
5968 goto done;
5970 if (SIMPLEQ_EMPTY(&commits)) {
5971 if (continue_rebase) {
5972 error = rebase_complete(worktree, fileindex,
5973 branch, new_base_branch, tmp_branch, repo);
5974 goto done;
5975 } else {
5976 /* Fast-forward the reference of the branch. */
5977 struct got_object_id *new_head_commit_id;
5978 char *id_str;
5979 error = got_ref_resolve(&new_head_commit_id, repo,
5980 new_base_branch);
5981 if (error)
5982 goto done;
5983 error = got_object_id_str(&id_str, new_head_commit_id);
5984 printf("Forwarding %s to commit %s\n",
5985 got_ref_get_name(branch), id_str);
5986 free(id_str);
5987 error = got_ref_change_ref(branch,
5988 new_head_commit_id);
5989 if (error)
5990 goto done;
5994 pid = NULL;
5995 SIMPLEQ_FOREACH(qid, &commits, entry) {
5996 commit_id = qid->id;
5997 parent_id = pid ? pid->id : yca_id;
5998 pid = qid;
6000 error = got_worktree_rebase_merge_files(&merged_paths,
6001 worktree, fileindex, parent_id, commit_id, repo,
6002 rebase_progress, &rebase_status, check_cancelled, NULL);
6003 if (error)
6004 goto done;
6006 if (rebase_status == GOT_STATUS_CONFLICT) {
6007 error = show_rebase_merge_conflict(qid->id, repo);
6008 if (error)
6009 goto done;
6010 got_worktree_rebase_pathlist_free(&merged_paths);
6011 break;
6014 error = rebase_commit(&merged_paths, worktree, fileindex,
6015 tmp_branch, commit_id, repo);
6016 got_worktree_rebase_pathlist_free(&merged_paths);
6017 if (error)
6018 goto done;
6021 if (rebase_status == GOT_STATUS_CONFLICT) {
6022 error = got_worktree_rebase_postpone(worktree, fileindex);
6023 if (error)
6024 goto done;
6025 error = got_error_msg(GOT_ERR_CONFLICTS,
6026 "conflicts must be resolved before rebasing can continue");
6027 } else
6028 error = rebase_complete(worktree, fileindex, branch,
6029 new_base_branch, tmp_branch, repo);
6030 done:
6031 got_object_id_queue_free(&commits);
6032 free(branch_head_commit_id);
6033 free(resume_commit_id);
6034 free(yca_id);
6035 if (commit)
6036 got_object_commit_close(commit);
6037 if (branch)
6038 got_ref_close(branch);
6039 if (new_base_branch)
6040 got_ref_close(new_base_branch);
6041 if (tmp_branch)
6042 got_ref_close(tmp_branch);
6043 if (worktree)
6044 got_worktree_close(worktree);
6045 if (repo)
6046 got_repo_close(repo);
6047 return error;
6050 __dead static void
6051 usage_histedit(void)
6053 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6054 getprogname());
6055 exit(1);
6058 #define GOT_HISTEDIT_PICK 'p'
6059 #define GOT_HISTEDIT_EDIT 'e'
6060 #define GOT_HISTEDIT_FOLD 'f'
6061 #define GOT_HISTEDIT_DROP 'd'
6062 #define GOT_HISTEDIT_MESG 'm'
6064 static struct got_histedit_cmd {
6065 unsigned char code;
6066 const char *name;
6067 const char *desc;
6068 } got_histedit_cmds[] = {
6069 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6070 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6071 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6072 "be used" },
6073 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6074 { GOT_HISTEDIT_MESG, "mesg",
6075 "single-line log message for commit above (open editor if empty)" },
6078 struct got_histedit_list_entry {
6079 TAILQ_ENTRY(got_histedit_list_entry) entry;
6080 struct got_object_id *commit_id;
6081 const struct got_histedit_cmd *cmd;
6082 char *logmsg;
6084 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6086 static const struct got_error *
6087 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6088 FILE *f, struct got_repository *repo)
6090 const struct got_error *err = NULL;
6091 char *logmsg = NULL, *id_str = NULL;
6092 struct got_commit_object *commit = NULL;
6093 int n;
6095 err = got_object_open_as_commit(&commit, repo, commit_id);
6096 if (err)
6097 goto done;
6099 err = get_short_logmsg(&logmsg, 34, commit);
6100 if (err)
6101 goto done;
6103 err = got_object_id_str(&id_str, commit_id);
6104 if (err)
6105 goto done;
6107 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6108 if (n < 0)
6109 err = got_ferror(f, GOT_ERR_IO);
6110 done:
6111 if (commit)
6112 got_object_commit_close(commit);
6113 free(id_str);
6114 free(logmsg);
6115 return err;
6118 static const struct got_error *
6119 histedit_write_commit_list(struct got_object_id_queue *commits,
6120 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6122 const struct got_error *err = NULL;
6123 struct got_object_qid *qid;
6125 if (SIMPLEQ_EMPTY(commits))
6126 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6128 SIMPLEQ_FOREACH(qid, commits, entry) {
6129 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6130 f, repo);
6131 if (err)
6132 break;
6133 if (edit_logmsg_only) {
6134 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6135 if (n < 0) {
6136 err = got_ferror(f, GOT_ERR_IO);
6137 break;
6142 return err;
6145 static const struct got_error *
6146 write_cmd_list(FILE *f, const char *branch_name,
6147 struct got_object_id_queue *commits)
6149 const struct got_error *err = NULL;
6150 int n, i;
6151 char *id_str;
6152 struct got_object_qid *qid;
6154 qid = SIMPLEQ_FIRST(commits);
6155 err = got_object_id_str(&id_str, qid->id);
6156 if (err)
6157 return err;
6159 n = fprintf(f,
6160 "# Editing the history of branch '%s' starting at\n"
6161 "# commit %s\n"
6162 "# Commits will be processed in order from top to "
6163 "bottom of this file.\n", branch_name, id_str);
6164 if (n < 0) {
6165 err = got_ferror(f, GOT_ERR_IO);
6166 goto done;
6169 n = fprintf(f, "# Available histedit commands:\n");
6170 if (n < 0) {
6171 err = got_ferror(f, GOT_ERR_IO);
6172 goto done;
6175 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6176 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6177 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6178 cmd->desc);
6179 if (n < 0) {
6180 err = got_ferror(f, GOT_ERR_IO);
6181 break;
6184 done:
6185 free(id_str);
6186 return err;
6189 static const struct got_error *
6190 histedit_syntax_error(int lineno)
6192 static char msg[42];
6193 int ret;
6195 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6196 lineno);
6197 if (ret == -1 || ret >= sizeof(msg))
6198 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6200 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6203 static const struct got_error *
6204 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6205 char *logmsg, struct got_repository *repo)
6207 const struct got_error *err;
6208 struct got_commit_object *folded_commit = NULL;
6209 char *id_str, *folded_logmsg = NULL;
6211 err = got_object_id_str(&id_str, hle->commit_id);
6212 if (err)
6213 return err;
6215 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6216 if (err)
6217 goto done;
6219 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6220 if (err)
6221 goto done;
6222 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6223 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6224 folded_logmsg) == -1) {
6225 err = got_error_from_errno("asprintf");
6227 done:
6228 if (folded_commit)
6229 got_object_commit_close(folded_commit);
6230 free(id_str);
6231 free(folded_logmsg);
6232 return err;
6235 static struct got_histedit_list_entry *
6236 get_folded_commits(struct got_histedit_list_entry *hle)
6238 struct got_histedit_list_entry *prev, *folded = NULL;
6240 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6241 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6242 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6243 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6244 folded = prev;
6245 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6248 return folded;
6251 static const struct got_error *
6252 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6253 struct got_repository *repo)
6255 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6256 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6257 const struct got_error *err = NULL;
6258 struct got_commit_object *commit = NULL;
6259 int fd;
6260 struct got_histedit_list_entry *folded = NULL;
6262 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6263 if (err)
6264 return err;
6266 folded = get_folded_commits(hle);
6267 if (folded) {
6268 while (folded != hle) {
6269 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6270 folded = TAILQ_NEXT(folded, entry);
6271 continue;
6273 err = append_folded_commit_msg(&new_msg, folded,
6274 logmsg, repo);
6275 if (err)
6276 goto done;
6277 free(logmsg);
6278 logmsg = new_msg;
6279 folded = TAILQ_NEXT(folded, entry);
6283 err = got_object_id_str(&id_str, hle->commit_id);
6284 if (err)
6285 goto done;
6286 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6287 if (err)
6288 goto done;
6289 if (asprintf(&new_msg,
6290 "%s\n# original log message of commit %s: %s",
6291 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6292 err = got_error_from_errno("asprintf");
6293 goto done;
6295 free(logmsg);
6296 logmsg = new_msg;
6298 err = got_object_id_str(&id_str, hle->commit_id);
6299 if (err)
6300 goto done;
6302 err = got_opentemp_named_fd(&logmsg_path, &fd,
6303 GOT_TMPDIR_STR "/got-logmsg");
6304 if (err)
6305 goto done;
6307 dprintf(fd, logmsg);
6308 close(fd);
6310 err = get_editor(&editor);
6311 if (err)
6312 goto done;
6314 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6315 if (err) {
6316 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6317 goto done;
6318 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6320 done:
6321 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6322 err = got_error_from_errno2("unlink", logmsg_path);
6323 free(logmsg_path);
6324 free(logmsg);
6325 free(orig_logmsg);
6326 free(editor);
6327 if (commit)
6328 got_object_commit_close(commit);
6329 return err;
6332 static const struct got_error *
6333 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6334 FILE *f, struct got_repository *repo)
6336 const struct got_error *err = NULL;
6337 char *line = NULL, *p, *end;
6338 size_t size;
6339 ssize_t len;
6340 int lineno = 0, i;
6341 const struct got_histedit_cmd *cmd;
6342 struct got_object_id *commit_id = NULL;
6343 struct got_histedit_list_entry *hle = NULL;
6345 for (;;) {
6346 len = getline(&line, &size, f);
6347 if (len == -1) {
6348 const struct got_error *getline_err;
6349 if (feof(f))
6350 break;
6351 getline_err = got_error_from_errno("getline");
6352 err = got_ferror(f, getline_err->code);
6353 break;
6355 lineno++;
6356 p = line;
6357 while (isspace((unsigned char)p[0]))
6358 p++;
6359 if (p[0] == '#' || p[0] == '\0') {
6360 free(line);
6361 line = NULL;
6362 continue;
6364 cmd = NULL;
6365 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6366 cmd = &got_histedit_cmds[i];
6367 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6368 isspace((unsigned char)p[strlen(cmd->name)])) {
6369 p += strlen(cmd->name);
6370 break;
6372 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6373 p++;
6374 break;
6377 if (i == nitems(got_histedit_cmds)) {
6378 err = histedit_syntax_error(lineno);
6379 break;
6381 while (isspace((unsigned char)p[0]))
6382 p++;
6383 if (cmd->code == GOT_HISTEDIT_MESG) {
6384 if (hle == NULL || hle->logmsg != NULL) {
6385 err = got_error(GOT_ERR_HISTEDIT_CMD);
6386 break;
6388 if (p[0] == '\0') {
6389 err = histedit_edit_logmsg(hle, repo);
6390 if (err)
6391 break;
6392 } else {
6393 hle->logmsg = strdup(p);
6394 if (hle->logmsg == NULL) {
6395 err = got_error_from_errno("strdup");
6396 break;
6399 free(line);
6400 line = NULL;
6401 continue;
6402 } else {
6403 end = p;
6404 while (end[0] && !isspace((unsigned char)end[0]))
6405 end++;
6406 *end = '\0';
6408 err = got_object_resolve_id_str(&commit_id, repo, p);
6409 if (err) {
6410 /* override error code */
6411 err = histedit_syntax_error(lineno);
6412 break;
6415 hle = malloc(sizeof(*hle));
6416 if (hle == NULL) {
6417 err = got_error_from_errno("malloc");
6418 break;
6420 hle->cmd = cmd;
6421 hle->commit_id = commit_id;
6422 hle->logmsg = NULL;
6423 commit_id = NULL;
6424 free(line);
6425 line = NULL;
6426 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6429 free(line);
6430 free(commit_id);
6431 return err;
6434 static const struct got_error *
6435 histedit_check_script(struct got_histedit_list *histedit_cmds,
6436 struct got_object_id_queue *commits, struct got_repository *repo)
6438 const struct got_error *err = NULL;
6439 struct got_object_qid *qid;
6440 struct got_histedit_list_entry *hle;
6441 static char msg[92];
6442 char *id_str;
6444 if (TAILQ_EMPTY(histedit_cmds))
6445 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6446 "histedit script contains no commands");
6447 if (SIMPLEQ_EMPTY(commits))
6448 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6450 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6451 struct got_histedit_list_entry *hle2;
6452 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6453 if (hle == hle2)
6454 continue;
6455 if (got_object_id_cmp(hle->commit_id,
6456 hle2->commit_id) != 0)
6457 continue;
6458 err = got_object_id_str(&id_str, hle->commit_id);
6459 if (err)
6460 return err;
6461 snprintf(msg, sizeof(msg), "commit %s is listed "
6462 "more than once in histedit script", id_str);
6463 free(id_str);
6464 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6468 SIMPLEQ_FOREACH(qid, commits, entry) {
6469 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6470 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6471 break;
6473 if (hle == NULL) {
6474 err = got_object_id_str(&id_str, qid->id);
6475 if (err)
6476 return err;
6477 snprintf(msg, sizeof(msg),
6478 "commit %s missing from histedit script", id_str);
6479 free(id_str);
6480 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6484 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6485 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6486 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6487 "last commit in histedit script cannot be folded");
6489 return NULL;
6492 static const struct got_error *
6493 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6494 const char *path, struct got_object_id_queue *commits,
6495 struct got_repository *repo)
6497 const struct got_error *err = NULL;
6498 char *editor;
6499 FILE *f = NULL;
6501 err = get_editor(&editor);
6502 if (err)
6503 return err;
6505 if (spawn_editor(editor, path) == -1) {
6506 err = got_error_from_errno("failed spawning editor");
6507 goto done;
6510 f = fopen(path, "r");
6511 if (f == NULL) {
6512 err = got_error_from_errno("fopen");
6513 goto done;
6515 err = histedit_parse_list(histedit_cmds, f, repo);
6516 if (err)
6517 goto done;
6519 err = histedit_check_script(histedit_cmds, commits, repo);
6520 done:
6521 if (f && fclose(f) != 0 && err == NULL)
6522 err = got_error_from_errno("fclose");
6523 free(editor);
6524 return err;
6527 static const struct got_error *
6528 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6529 struct got_object_id_queue *, const char *, const char *,
6530 struct got_repository *);
6532 static const struct got_error *
6533 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6534 struct got_object_id_queue *commits, const char *branch_name,
6535 int edit_logmsg_only, struct got_repository *repo)
6537 const struct got_error *err;
6538 FILE *f = NULL;
6539 char *path = NULL;
6541 err = got_opentemp_named(&path, &f, "got-histedit");
6542 if (err)
6543 return err;
6545 err = write_cmd_list(f, branch_name, commits);
6546 if (err)
6547 goto done;
6549 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6550 if (err)
6551 goto done;
6553 if (edit_logmsg_only) {
6554 rewind(f);
6555 err = histedit_parse_list(histedit_cmds, f, repo);
6556 } else {
6557 if (fclose(f) != 0) {
6558 err = got_error_from_errno("fclose");
6559 goto done;
6561 f = NULL;
6562 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6563 if (err) {
6564 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6565 err->code != GOT_ERR_HISTEDIT_CMD)
6566 goto done;
6567 err = histedit_edit_list_retry(histedit_cmds, err,
6568 commits, path, branch_name, repo);
6571 done:
6572 if (f && fclose(f) != 0 && err == NULL)
6573 err = got_error_from_errno("fclose");
6574 if (path && unlink(path) != 0 && err == NULL)
6575 err = got_error_from_errno2("unlink", path);
6576 free(path);
6577 return err;
6580 static const struct got_error *
6581 histedit_save_list(struct got_histedit_list *histedit_cmds,
6582 struct got_worktree *worktree, struct got_repository *repo)
6584 const struct got_error *err = NULL;
6585 char *path = NULL;
6586 FILE *f = NULL;
6587 struct got_histedit_list_entry *hle;
6588 struct got_commit_object *commit = NULL;
6590 err = got_worktree_get_histedit_script_path(&path, worktree);
6591 if (err)
6592 return err;
6594 f = fopen(path, "w");
6595 if (f == NULL) {
6596 err = got_error_from_errno2("fopen", path);
6597 goto done;
6599 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6600 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6601 repo);
6602 if (err)
6603 break;
6605 if (hle->logmsg) {
6606 int n = fprintf(f, "%c %s\n",
6607 GOT_HISTEDIT_MESG, hle->logmsg);
6608 if (n < 0) {
6609 err = got_ferror(f, GOT_ERR_IO);
6610 break;
6614 done:
6615 if (f && fclose(f) != 0 && err == NULL)
6616 err = got_error_from_errno("fclose");
6617 free(path);
6618 if (commit)
6619 got_object_commit_close(commit);
6620 return err;
6623 void
6624 histedit_free_list(struct got_histedit_list *histedit_cmds)
6626 struct got_histedit_list_entry *hle;
6628 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6629 TAILQ_REMOVE(histedit_cmds, hle, entry);
6630 free(hle);
6634 static const struct got_error *
6635 histedit_load_list(struct got_histedit_list *histedit_cmds,
6636 const char *path, struct got_repository *repo)
6638 const struct got_error *err = NULL;
6639 FILE *f = NULL;
6641 f = fopen(path, "r");
6642 if (f == NULL) {
6643 err = got_error_from_errno2("fopen", path);
6644 goto done;
6647 err = histedit_parse_list(histedit_cmds, f, repo);
6648 done:
6649 if (f && fclose(f) != 0 && err == NULL)
6650 err = got_error_from_errno("fclose");
6651 return err;
6654 static const struct got_error *
6655 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6656 const struct got_error *edit_err, struct got_object_id_queue *commits,
6657 const char *path, const char *branch_name, struct got_repository *repo)
6659 const struct got_error *err = NULL, *prev_err = edit_err;
6660 int resp = ' ';
6662 while (resp != 'c' && resp != 'r' && resp != 'a') {
6663 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6664 "or (a)bort: ", getprogname(), prev_err->msg);
6665 resp = getchar();
6666 if (resp == '\n')
6667 resp = getchar();
6668 if (resp == 'c') {
6669 histedit_free_list(histedit_cmds);
6670 err = histedit_run_editor(histedit_cmds, path, commits,
6671 repo);
6672 if (err) {
6673 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6674 err->code != GOT_ERR_HISTEDIT_CMD)
6675 break;
6676 prev_err = err;
6677 resp = ' ';
6678 continue;
6680 break;
6681 } else if (resp == 'r') {
6682 histedit_free_list(histedit_cmds);
6683 err = histedit_edit_script(histedit_cmds,
6684 commits, branch_name, 0, repo);
6685 if (err) {
6686 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6687 err->code != GOT_ERR_HISTEDIT_CMD)
6688 break;
6689 prev_err = err;
6690 resp = ' ';
6691 continue;
6693 break;
6694 } else if (resp == 'a') {
6695 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6696 break;
6697 } else
6698 printf("invalid response '%c'\n", resp);
6701 return err;
6704 static const struct got_error *
6705 histedit_complete(struct got_worktree *worktree,
6706 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6707 struct got_reference *branch, struct got_repository *repo)
6709 printf("Switching work tree to %s\n",
6710 got_ref_get_symref_target(branch));
6711 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6712 branch, repo);
6715 static const struct got_error *
6716 show_histedit_progress(struct got_commit_object *commit,
6717 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6719 const struct got_error *err;
6720 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6722 err = got_object_id_str(&old_id_str, hle->commit_id);
6723 if (err)
6724 goto done;
6726 if (new_id) {
6727 err = got_object_id_str(&new_id_str, new_id);
6728 if (err)
6729 goto done;
6732 old_id_str[12] = '\0';
6733 if (new_id_str)
6734 new_id_str[12] = '\0';
6736 if (hle->logmsg) {
6737 logmsg = strdup(hle->logmsg);
6738 if (logmsg == NULL) {
6739 err = got_error_from_errno("strdup");
6740 goto done;
6742 trim_logmsg(logmsg, 42);
6743 } else {
6744 err = get_short_logmsg(&logmsg, 42, commit);
6745 if (err)
6746 goto done;
6749 switch (hle->cmd->code) {
6750 case GOT_HISTEDIT_PICK:
6751 case GOT_HISTEDIT_EDIT:
6752 printf("%s -> %s: %s\n", old_id_str,
6753 new_id_str ? new_id_str : "no-op change", logmsg);
6754 break;
6755 case GOT_HISTEDIT_DROP:
6756 case GOT_HISTEDIT_FOLD:
6757 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6758 logmsg);
6759 break;
6760 default:
6761 break;
6763 done:
6764 free(old_id_str);
6765 free(new_id_str);
6766 return err;
6769 static const struct got_error *
6770 histedit_commit(struct got_pathlist_head *merged_paths,
6771 struct got_worktree *worktree, struct got_fileindex *fileindex,
6772 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6773 struct got_repository *repo)
6775 const struct got_error *err;
6776 struct got_commit_object *commit;
6777 struct got_object_id *new_commit_id;
6779 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6780 && hle->logmsg == NULL) {
6781 err = histedit_edit_logmsg(hle, repo);
6782 if (err)
6783 return err;
6786 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6787 if (err)
6788 return err;
6790 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6791 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6792 hle->logmsg, repo);
6793 if (err) {
6794 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6795 goto done;
6796 err = show_histedit_progress(commit, hle, NULL);
6797 } else {
6798 err = show_histedit_progress(commit, hle, new_commit_id);
6799 free(new_commit_id);
6801 done:
6802 got_object_commit_close(commit);
6803 return err;
6806 static const struct got_error *
6807 histedit_skip_commit(struct got_histedit_list_entry *hle,
6808 struct got_worktree *worktree, struct got_repository *repo)
6810 const struct got_error *error;
6811 struct got_commit_object *commit;
6813 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6814 repo);
6815 if (error)
6816 return error;
6818 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6819 if (error)
6820 return error;
6822 error = show_histedit_progress(commit, hle, NULL);
6823 got_object_commit_close(commit);
6824 return error;
6827 static const struct got_error *
6828 check_local_changes(void *arg, unsigned char status,
6829 unsigned char staged_status, const char *path,
6830 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6831 struct got_object_id *commit_id, int dirfd, const char *de_name)
6833 int *have_local_changes = arg;
6835 switch (status) {
6836 case GOT_STATUS_ADD:
6837 case GOT_STATUS_DELETE:
6838 case GOT_STATUS_MODIFY:
6839 case GOT_STATUS_CONFLICT:
6840 *have_local_changes = 1;
6841 return got_error(GOT_ERR_CANCELLED);
6842 default:
6843 break;
6846 switch (staged_status) {
6847 case GOT_STATUS_ADD:
6848 case GOT_STATUS_DELETE:
6849 case GOT_STATUS_MODIFY:
6850 *have_local_changes = 1;
6851 return got_error(GOT_ERR_CANCELLED);
6852 default:
6853 break;
6856 return NULL;
6859 static const struct got_error *
6860 cmd_histedit(int argc, char *argv[])
6862 const struct got_error *error = NULL;
6863 struct got_worktree *worktree = NULL;
6864 struct got_fileindex *fileindex = NULL;
6865 struct got_repository *repo = NULL;
6866 char *cwd = NULL;
6867 struct got_reference *branch = NULL;
6868 struct got_reference *tmp_branch = NULL;
6869 struct got_object_id *resume_commit_id = NULL;
6870 struct got_object_id *base_commit_id = NULL;
6871 struct got_object_id *head_commit_id = NULL;
6872 struct got_commit_object *commit = NULL;
6873 int ch, rebase_in_progress = 0, did_something;
6874 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6875 int edit_logmsg_only = 0;
6876 const char *edit_script_path = NULL;
6877 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6878 struct got_object_id_queue commits;
6879 struct got_pathlist_head merged_paths;
6880 const struct got_object_id_queue *parent_ids;
6881 struct got_object_qid *pid;
6882 struct got_histedit_list histedit_cmds;
6883 struct got_histedit_list_entry *hle;
6885 SIMPLEQ_INIT(&commits);
6886 TAILQ_INIT(&histedit_cmds);
6887 TAILQ_INIT(&merged_paths);
6889 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6890 switch (ch) {
6891 case 'a':
6892 abort_edit = 1;
6893 break;
6894 case 'c':
6895 continue_edit = 1;
6896 break;
6897 case 'F':
6898 edit_script_path = optarg;
6899 break;
6900 case 'm':
6901 edit_logmsg_only = 1;
6902 break;
6903 default:
6904 usage_histedit();
6905 /* NOTREACHED */
6909 argc -= optind;
6910 argv += optind;
6912 #ifndef PROFILE
6913 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6914 "unveil", NULL) == -1)
6915 err(1, "pledge");
6916 #endif
6917 if (abort_edit && continue_edit)
6918 errx(1, "histedit's -a and -c options are mutually exclusive");
6919 if (edit_script_path && edit_logmsg_only)
6920 errx(1, "histedit's -F and -m options are mutually exclusive");
6921 if (abort_edit && edit_logmsg_only)
6922 errx(1, "histedit's -a and -m options are mutually exclusive");
6923 if (continue_edit && edit_logmsg_only)
6924 errx(1, "histedit's -c and -m options are mutually exclusive");
6925 if (argc != 0)
6926 usage_histedit();
6929 * This command cannot apply unveil(2) in all cases because the
6930 * user may choose to run an editor to edit the histedit script
6931 * and to edit individual commit log messages.
6932 * unveil(2) traverses exec(2); if an editor is used we have to
6933 * apply unveil after edit script and log messages have been written.
6934 * XXX TODO: Make use of unveil(2) where possible.
6937 cwd = getcwd(NULL, 0);
6938 if (cwd == NULL) {
6939 error = got_error_from_errno("getcwd");
6940 goto done;
6942 error = got_worktree_open(&worktree, cwd);
6943 if (error)
6944 goto done;
6946 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6947 NULL);
6948 if (error != NULL)
6949 goto done;
6951 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6952 if (error)
6953 goto done;
6954 if (rebase_in_progress) {
6955 error = got_error(GOT_ERR_REBASING);
6956 goto done;
6959 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6960 if (error)
6961 goto done;
6963 if (edit_in_progress && edit_logmsg_only) {
6964 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6965 "histedit operation is in progress in this "
6966 "work tree and must be continued or aborted "
6967 "before the -m option can be used");
6968 goto done;
6971 if (edit_in_progress && abort_edit) {
6972 error = got_worktree_histedit_continue(&resume_commit_id,
6973 &tmp_branch, &branch, &base_commit_id, &fileindex,
6974 worktree, repo);
6975 if (error)
6976 goto done;
6977 printf("Switching work tree to %s\n",
6978 got_ref_get_symref_target(branch));
6979 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6980 branch, base_commit_id, update_progress, &did_something);
6981 if (error)
6982 goto done;
6983 printf("Histedit of %s aborted\n",
6984 got_ref_get_symref_target(branch));
6985 goto done; /* nothing else to do */
6986 } else if (abort_edit) {
6987 error = got_error(GOT_ERR_NOT_HISTEDIT);
6988 goto done;
6991 if (continue_edit) {
6992 char *path;
6994 if (!edit_in_progress) {
6995 error = got_error(GOT_ERR_NOT_HISTEDIT);
6996 goto done;
6999 error = got_worktree_get_histedit_script_path(&path, worktree);
7000 if (error)
7001 goto done;
7003 error = histedit_load_list(&histedit_cmds, path, repo);
7004 free(path);
7005 if (error)
7006 goto done;
7008 error = got_worktree_histedit_continue(&resume_commit_id,
7009 &tmp_branch, &branch, &base_commit_id, &fileindex,
7010 worktree, repo);
7011 if (error)
7012 goto done;
7014 error = got_ref_resolve(&head_commit_id, repo, branch);
7015 if (error)
7016 goto done;
7018 error = got_object_open_as_commit(&commit, repo,
7019 head_commit_id);
7020 if (error)
7021 goto done;
7022 parent_ids = got_object_commit_get_parent_ids(commit);
7023 pid = SIMPLEQ_FIRST(parent_ids);
7024 if (pid == NULL) {
7025 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7026 goto done;
7028 error = collect_commits(&commits, head_commit_id, pid->id,
7029 base_commit_id, got_worktree_get_path_prefix(worktree),
7030 GOT_ERR_HISTEDIT_PATH, repo);
7031 got_object_commit_close(commit);
7032 commit = NULL;
7033 if (error)
7034 goto done;
7035 } else {
7036 if (edit_in_progress) {
7037 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7038 goto done;
7041 error = got_ref_open(&branch, repo,
7042 got_worktree_get_head_ref_name(worktree), 0);
7043 if (error != NULL)
7044 goto done;
7046 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7047 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7048 "will not edit commit history of a branch outside "
7049 "the \"refs/heads/\" reference namespace");
7050 goto done;
7053 error = got_ref_resolve(&head_commit_id, repo, branch);
7054 got_ref_close(branch);
7055 branch = NULL;
7056 if (error)
7057 goto done;
7059 error = got_object_open_as_commit(&commit, repo,
7060 head_commit_id);
7061 if (error)
7062 goto done;
7063 parent_ids = got_object_commit_get_parent_ids(commit);
7064 pid = SIMPLEQ_FIRST(parent_ids);
7065 if (pid == NULL) {
7066 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7067 goto done;
7069 error = collect_commits(&commits, head_commit_id, pid->id,
7070 got_worktree_get_base_commit_id(worktree),
7071 got_worktree_get_path_prefix(worktree),
7072 GOT_ERR_HISTEDIT_PATH, repo);
7073 got_object_commit_close(commit);
7074 commit = NULL;
7075 if (error)
7076 goto done;
7078 if (SIMPLEQ_EMPTY(&commits)) {
7079 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7080 goto done;
7083 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7084 &base_commit_id, &fileindex, worktree, repo);
7085 if (error)
7086 goto done;
7088 if (edit_script_path) {
7089 error = histedit_load_list(&histedit_cmds,
7090 edit_script_path, repo);
7091 if (error) {
7092 got_worktree_histedit_abort(worktree, fileindex,
7093 repo, branch, base_commit_id,
7094 update_progress, &did_something);
7095 goto done;
7097 } else {
7098 const char *branch_name;
7099 branch_name = got_ref_get_symref_target(branch);
7100 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7101 branch_name += 11;
7102 error = histedit_edit_script(&histedit_cmds, &commits,
7103 branch_name, edit_logmsg_only, repo);
7104 if (error) {
7105 got_worktree_histedit_abort(worktree, fileindex,
7106 repo, branch, base_commit_id,
7107 update_progress, &did_something);
7108 goto done;
7113 error = histedit_save_list(&histedit_cmds, worktree,
7114 repo);
7115 if (error) {
7116 got_worktree_histedit_abort(worktree, fileindex,
7117 repo, branch, base_commit_id,
7118 update_progress, &did_something);
7119 goto done;
7124 error = histedit_check_script(&histedit_cmds, &commits, repo);
7125 if (error)
7126 goto done;
7128 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7129 if (resume_commit_id) {
7130 if (got_object_id_cmp(hle->commit_id,
7131 resume_commit_id) != 0)
7132 continue;
7134 resume_commit_id = NULL;
7135 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7136 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7137 error = histedit_skip_commit(hle, worktree,
7138 repo);
7139 if (error)
7140 goto done;
7141 } else {
7142 struct got_pathlist_head paths;
7143 int have_changes = 0;
7145 TAILQ_INIT(&paths);
7146 error = got_pathlist_append(&paths, "", NULL);
7147 if (error)
7148 goto done;
7149 error = got_worktree_status(worktree, &paths,
7150 repo, check_local_changes, &have_changes,
7151 check_cancelled, NULL);
7152 got_pathlist_free(&paths);
7153 if (error) {
7154 if (error->code != GOT_ERR_CANCELLED)
7155 goto done;
7156 if (sigint_received || sigpipe_received)
7157 goto done;
7159 if (have_changes) {
7160 error = histedit_commit(NULL, worktree,
7161 fileindex, tmp_branch, hle, repo);
7162 if (error)
7163 goto done;
7164 } else {
7165 error = got_object_open_as_commit(
7166 &commit, repo, hle->commit_id);
7167 if (error)
7168 goto done;
7169 error = show_histedit_progress(commit,
7170 hle, NULL);
7171 got_object_commit_close(commit);
7172 commit = NULL;
7173 if (error)
7174 goto done;
7177 continue;
7180 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7181 error = histedit_skip_commit(hle, worktree, repo);
7182 if (error)
7183 goto done;
7184 continue;
7187 error = got_object_open_as_commit(&commit, repo,
7188 hle->commit_id);
7189 if (error)
7190 goto done;
7191 parent_ids = got_object_commit_get_parent_ids(commit);
7192 pid = SIMPLEQ_FIRST(parent_ids);
7194 error = got_worktree_histedit_merge_files(&merged_paths,
7195 worktree, fileindex, pid->id, hle->commit_id, repo,
7196 rebase_progress, &rebase_status, check_cancelled, NULL);
7197 if (error)
7198 goto done;
7199 got_object_commit_close(commit);
7200 commit = NULL;
7202 if (rebase_status == GOT_STATUS_CONFLICT) {
7203 error = show_rebase_merge_conflict(hle->commit_id,
7204 repo);
7205 if (error)
7206 goto done;
7207 got_worktree_rebase_pathlist_free(&merged_paths);
7208 break;
7211 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7212 char *id_str;
7213 error = got_object_id_str(&id_str, hle->commit_id);
7214 if (error)
7215 goto done;
7216 printf("Stopping histedit for amending commit %s\n",
7217 id_str);
7218 free(id_str);
7219 got_worktree_rebase_pathlist_free(&merged_paths);
7220 error = got_worktree_histedit_postpone(worktree,
7221 fileindex);
7222 goto done;
7225 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7226 error = histedit_skip_commit(hle, worktree, repo);
7227 if (error)
7228 goto done;
7229 continue;
7232 error = histedit_commit(&merged_paths, worktree, fileindex,
7233 tmp_branch, hle, repo);
7234 got_worktree_rebase_pathlist_free(&merged_paths);
7235 if (error)
7236 goto done;
7239 if (rebase_status == GOT_STATUS_CONFLICT) {
7240 error = got_worktree_histedit_postpone(worktree, fileindex);
7241 if (error)
7242 goto done;
7243 error = got_error_msg(GOT_ERR_CONFLICTS,
7244 "conflicts must be resolved before histedit can continue");
7245 } else
7246 error = histedit_complete(worktree, fileindex, tmp_branch,
7247 branch, repo);
7248 done:
7249 got_object_id_queue_free(&commits);
7250 histedit_free_list(&histedit_cmds);
7251 free(head_commit_id);
7252 free(base_commit_id);
7253 free(resume_commit_id);
7254 if (commit)
7255 got_object_commit_close(commit);
7256 if (branch)
7257 got_ref_close(branch);
7258 if (tmp_branch)
7259 got_ref_close(tmp_branch);
7260 if (worktree)
7261 got_worktree_close(worktree);
7262 if (repo)
7263 got_repo_close(repo);
7264 return error;
7267 __dead static void
7268 usage_integrate(void)
7270 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7271 exit(1);
7274 static const struct got_error *
7275 cmd_integrate(int argc, char *argv[])
7277 const struct got_error *error = NULL;
7278 struct got_repository *repo = NULL;
7279 struct got_worktree *worktree = NULL;
7280 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7281 const char *branch_arg = NULL;
7282 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7283 struct got_fileindex *fileindex = NULL;
7284 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7285 int ch, did_something = 0;
7287 while ((ch = getopt(argc, argv, "")) != -1) {
7288 switch (ch) {
7289 default:
7290 usage_integrate();
7291 /* NOTREACHED */
7295 argc -= optind;
7296 argv += optind;
7298 if (argc != 1)
7299 usage_integrate();
7300 branch_arg = argv[0];
7302 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7303 "unveil", NULL) == -1)
7304 err(1, "pledge");
7306 cwd = getcwd(NULL, 0);
7307 if (cwd == NULL) {
7308 error = got_error_from_errno("getcwd");
7309 goto done;
7312 error = got_worktree_open(&worktree, cwd);
7313 if (error)
7314 goto done;
7316 error = check_rebase_or_histedit_in_progress(worktree);
7317 if (error)
7318 goto done;
7320 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7321 NULL);
7322 if (error != NULL)
7323 goto done;
7325 error = apply_unveil(got_repo_get_path(repo), 0,
7326 got_worktree_get_root_path(worktree));
7327 if (error)
7328 goto done;
7330 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7331 error = got_error_from_errno("asprintf");
7332 goto done;
7335 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7336 &base_branch_ref, worktree, refname, repo);
7337 if (error)
7338 goto done;
7340 refname = strdup(got_ref_get_name(branch_ref));
7341 if (refname == NULL) {
7342 error = got_error_from_errno("strdup");
7343 got_worktree_integrate_abort(worktree, fileindex, repo,
7344 branch_ref, base_branch_ref);
7345 goto done;
7347 base_refname = strdup(got_ref_get_name(base_branch_ref));
7348 if (base_refname == NULL) {
7349 error = got_error_from_errno("strdup");
7350 got_worktree_integrate_abort(worktree, fileindex, repo,
7351 branch_ref, base_branch_ref);
7352 goto done;
7355 error = got_ref_resolve(&commit_id, repo, branch_ref);
7356 if (error)
7357 goto done;
7359 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7360 if (error)
7361 goto done;
7363 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7364 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7365 "specified branch has already been integrated");
7366 got_worktree_integrate_abort(worktree, fileindex, repo,
7367 branch_ref, base_branch_ref);
7368 goto done;
7371 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7372 if (error) {
7373 if (error->code == GOT_ERR_ANCESTRY)
7374 error = got_error(GOT_ERR_REBASE_REQUIRED);
7375 got_worktree_integrate_abort(worktree, fileindex, repo,
7376 branch_ref, base_branch_ref);
7377 goto done;
7380 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7381 branch_ref, base_branch_ref, update_progress, &did_something,
7382 check_cancelled, NULL);
7383 if (error)
7384 goto done;
7386 printf("Integrated %s into %s\n", refname, base_refname);
7387 done:
7388 if (repo)
7389 got_repo_close(repo);
7390 if (worktree)
7391 got_worktree_close(worktree);
7392 free(cwd);
7393 free(base_commit_id);
7394 free(commit_id);
7395 free(refname);
7396 free(base_refname);
7397 return error;
7400 __dead static void
7401 usage_stage(void)
7403 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7404 "[file-path ...]\n",
7405 getprogname());
7406 exit(1);
7409 static const struct got_error *
7410 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7411 const char *path, struct got_object_id *blob_id,
7412 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7413 int dirfd, const char *de_name)
7415 const struct got_error *err = NULL;
7416 char *id_str = NULL;
7418 if (staged_status != GOT_STATUS_ADD &&
7419 staged_status != GOT_STATUS_MODIFY &&
7420 staged_status != GOT_STATUS_DELETE)
7421 return NULL;
7423 if (staged_status == GOT_STATUS_ADD ||
7424 staged_status == GOT_STATUS_MODIFY)
7425 err = got_object_id_str(&id_str, staged_blob_id);
7426 else
7427 err = got_object_id_str(&id_str, blob_id);
7428 if (err)
7429 return err;
7431 printf("%s %c %s\n", id_str, staged_status, path);
7432 free(id_str);
7433 return NULL;
7436 static const struct got_error *
7437 cmd_stage(int argc, char *argv[])
7439 const struct got_error *error = NULL;
7440 struct got_repository *repo = NULL;
7441 struct got_worktree *worktree = NULL;
7442 char *cwd = NULL;
7443 struct got_pathlist_head paths;
7444 struct got_pathlist_entry *pe;
7445 int ch, list_stage = 0, pflag = 0;
7446 FILE *patch_script_file = NULL;
7447 const char *patch_script_path = NULL;
7448 struct choose_patch_arg cpa;
7450 TAILQ_INIT(&paths);
7452 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7453 switch (ch) {
7454 case 'l':
7455 list_stage = 1;
7456 break;
7457 case 'p':
7458 pflag = 1;
7459 break;
7460 case 'F':
7461 patch_script_path = optarg;
7462 break;
7463 default:
7464 usage_stage();
7465 /* NOTREACHED */
7469 argc -= optind;
7470 argv += optind;
7472 #ifndef PROFILE
7473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7474 "unveil", NULL) == -1)
7475 err(1, "pledge");
7476 #endif
7477 if (list_stage && (pflag || patch_script_path))
7478 errx(1, "-l option cannot be used with other options");
7479 if (patch_script_path && !pflag)
7480 errx(1, "-F option can only be used together with -p option");
7482 cwd = getcwd(NULL, 0);
7483 if (cwd == NULL) {
7484 error = got_error_from_errno("getcwd");
7485 goto done;
7488 error = got_worktree_open(&worktree, cwd);
7489 if (error)
7490 goto done;
7492 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7493 NULL);
7494 if (error != NULL)
7495 goto done;
7497 if (patch_script_path) {
7498 patch_script_file = fopen(patch_script_path, "r");
7499 if (patch_script_file == NULL) {
7500 error = got_error_from_errno2("fopen",
7501 patch_script_path);
7502 goto done;
7505 error = apply_unveil(got_repo_get_path(repo), 0,
7506 got_worktree_get_root_path(worktree));
7507 if (error)
7508 goto done;
7510 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7511 if (error)
7512 goto done;
7514 if (list_stage)
7515 error = got_worktree_status(worktree, &paths, repo,
7516 print_stage, NULL, check_cancelled, NULL);
7517 else {
7518 cpa.patch_script_file = patch_script_file;
7519 cpa.action = "stage";
7520 error = got_worktree_stage(worktree, &paths,
7521 pflag ? NULL : print_status, NULL,
7522 pflag ? choose_patch : NULL, &cpa, repo);
7524 done:
7525 if (patch_script_file && fclose(patch_script_file) == EOF &&
7526 error == NULL)
7527 error = got_error_from_errno2("fclose", patch_script_path);
7528 if (repo)
7529 got_repo_close(repo);
7530 if (worktree)
7531 got_worktree_close(worktree);
7532 TAILQ_FOREACH(pe, &paths, entry)
7533 free((char *)pe->path);
7534 got_pathlist_free(&paths);
7535 free(cwd);
7536 return error;
7539 __dead static void
7540 usage_unstage(void)
7542 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7543 "[file-path ...]\n",
7544 getprogname());
7545 exit(1);
7549 static const struct got_error *
7550 cmd_unstage(int argc, char *argv[])
7552 const struct got_error *error = NULL;
7553 struct got_repository *repo = NULL;
7554 struct got_worktree *worktree = NULL;
7555 char *cwd = NULL;
7556 struct got_pathlist_head paths;
7557 struct got_pathlist_entry *pe;
7558 int ch, did_something = 0, pflag = 0;
7559 FILE *patch_script_file = NULL;
7560 const char *patch_script_path = NULL;
7561 struct choose_patch_arg cpa;
7563 TAILQ_INIT(&paths);
7565 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7566 switch (ch) {
7567 case 'p':
7568 pflag = 1;
7569 break;
7570 case 'F':
7571 patch_script_path = optarg;
7572 break;
7573 default:
7574 usage_unstage();
7575 /* NOTREACHED */
7579 argc -= optind;
7580 argv += optind;
7582 #ifndef PROFILE
7583 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7584 "unveil", NULL) == -1)
7585 err(1, "pledge");
7586 #endif
7587 if (patch_script_path && !pflag)
7588 errx(1, "-F option can only be used together with -p option");
7590 cwd = getcwd(NULL, 0);
7591 if (cwd == NULL) {
7592 error = got_error_from_errno("getcwd");
7593 goto done;
7596 error = got_worktree_open(&worktree, cwd);
7597 if (error)
7598 goto done;
7600 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7601 NULL);
7602 if (error != NULL)
7603 goto done;
7605 if (patch_script_path) {
7606 patch_script_file = fopen(patch_script_path, "r");
7607 if (patch_script_file == NULL) {
7608 error = got_error_from_errno2("fopen",
7609 patch_script_path);
7610 goto done;
7614 error = apply_unveil(got_repo_get_path(repo), 0,
7615 got_worktree_get_root_path(worktree));
7616 if (error)
7617 goto done;
7619 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7620 if (error)
7621 goto done;
7623 cpa.patch_script_file = patch_script_file;
7624 cpa.action = "unstage";
7625 error = got_worktree_unstage(worktree, &paths, update_progress,
7626 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7627 done:
7628 if (patch_script_file && fclose(patch_script_file) == EOF &&
7629 error == NULL)
7630 error = got_error_from_errno2("fclose", patch_script_path);
7631 if (repo)
7632 got_repo_close(repo);
7633 if (worktree)
7634 got_worktree_close(worktree);
7635 TAILQ_FOREACH(pe, &paths, entry)
7636 free((char *)pe->path);
7637 got_pathlist_free(&paths);
7638 free(cwd);
7639 return error;
7642 __dead static void
7643 usage_cat(void)
7645 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7646 "arg1 [arg2 ...]\n", getprogname());
7647 exit(1);
7650 static const struct got_error *
7651 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7653 const struct got_error *err;
7654 struct got_blob_object *blob;
7656 err = got_object_open_as_blob(&blob, repo, id, 8192);
7657 if (err)
7658 return err;
7660 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7661 got_object_blob_close(blob);
7662 return err;
7665 static const struct got_error *
7666 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7668 const struct got_error *err;
7669 struct got_tree_object *tree;
7670 int nentries, i;
7672 err = got_object_open_as_tree(&tree, repo, id);
7673 if (err)
7674 return err;
7676 nentries = got_object_tree_get_nentries(tree);
7677 for (i = 0; i < nentries; i++) {
7678 struct got_tree_entry *te;
7679 char *id_str;
7680 if (sigint_received || sigpipe_received)
7681 break;
7682 te = got_object_tree_get_entry(tree, i);
7683 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7684 if (err)
7685 break;
7686 fprintf(outfile, "%s %.7o %s\n", id_str,
7687 got_tree_entry_get_mode(te),
7688 got_tree_entry_get_name(te));
7689 free(id_str);
7692 got_object_tree_close(tree);
7693 return err;
7696 static const struct got_error *
7697 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7699 const struct got_error *err;
7700 struct got_commit_object *commit;
7701 const struct got_object_id_queue *parent_ids;
7702 struct got_object_qid *pid;
7703 char *id_str = NULL;
7704 const char *logmsg = NULL;
7706 err = got_object_open_as_commit(&commit, repo, id);
7707 if (err)
7708 return err;
7710 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7711 if (err)
7712 goto done;
7714 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7715 parent_ids = got_object_commit_get_parent_ids(commit);
7716 fprintf(outfile, "numparents %d\n",
7717 got_object_commit_get_nparents(commit));
7718 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7719 char *pid_str;
7720 err = got_object_id_str(&pid_str, pid->id);
7721 if (err)
7722 goto done;
7723 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7724 free(pid_str);
7726 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7727 got_object_commit_get_author(commit),
7728 got_object_commit_get_author_time(commit));
7730 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7731 got_object_commit_get_author(commit),
7732 got_object_commit_get_committer_time(commit));
7734 logmsg = got_object_commit_get_logmsg_raw(commit);
7735 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7736 fprintf(outfile, "%s", logmsg);
7737 done:
7738 free(id_str);
7739 got_object_commit_close(commit);
7740 return err;
7743 static const struct got_error *
7744 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7746 const struct got_error *err;
7747 struct got_tag_object *tag;
7748 char *id_str = NULL;
7749 const char *tagmsg = NULL;
7751 err = got_object_open_as_tag(&tag, repo, id);
7752 if (err)
7753 return err;
7755 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7756 if (err)
7757 goto done;
7759 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7761 switch (got_object_tag_get_object_type(tag)) {
7762 case GOT_OBJ_TYPE_BLOB:
7763 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7764 GOT_OBJ_LABEL_BLOB);
7765 break;
7766 case GOT_OBJ_TYPE_TREE:
7767 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7768 GOT_OBJ_LABEL_TREE);
7769 break;
7770 case GOT_OBJ_TYPE_COMMIT:
7771 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7772 GOT_OBJ_LABEL_COMMIT);
7773 break;
7774 case GOT_OBJ_TYPE_TAG:
7775 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7776 GOT_OBJ_LABEL_TAG);
7777 break;
7778 default:
7779 break;
7782 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7783 got_object_tag_get_name(tag));
7785 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7786 got_object_tag_get_tagger(tag),
7787 got_object_tag_get_tagger_time(tag));
7789 tagmsg = got_object_tag_get_message(tag);
7790 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7791 fprintf(outfile, "%s", tagmsg);
7792 done:
7793 free(id_str);
7794 got_object_tag_close(tag);
7795 return err;
7798 static const struct got_error *
7799 cmd_cat(int argc, char *argv[])
7801 const struct got_error *error;
7802 struct got_repository *repo = NULL;
7803 struct got_worktree *worktree = NULL;
7804 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7805 const char *commit_id_str = NULL;
7806 struct got_object_id *id = NULL, *commit_id = NULL;
7807 int ch, obj_type, i, force_path = 0;
7809 #ifndef PROFILE
7810 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7811 NULL) == -1)
7812 err(1, "pledge");
7813 #endif
7815 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7816 switch (ch) {
7817 case 'c':
7818 commit_id_str = optarg;
7819 break;
7820 case 'r':
7821 repo_path = realpath(optarg, NULL);
7822 if (repo_path == NULL)
7823 return got_error_from_errno2("realpath",
7824 optarg);
7825 got_path_strip_trailing_slashes(repo_path);
7826 break;
7827 case 'P':
7828 force_path = 1;
7829 break;
7830 default:
7831 usage_cat();
7832 /* NOTREACHED */
7836 argc -= optind;
7837 argv += optind;
7839 cwd = getcwd(NULL, 0);
7840 if (cwd == NULL) {
7841 error = got_error_from_errno("getcwd");
7842 goto done;
7844 error = got_worktree_open(&worktree, cwd);
7845 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7846 goto done;
7847 if (worktree) {
7848 if (repo_path == NULL) {
7849 repo_path = strdup(
7850 got_worktree_get_repo_path(worktree));
7851 if (repo_path == NULL) {
7852 error = got_error_from_errno("strdup");
7853 goto done;
7858 if (repo_path == NULL) {
7859 repo_path = getcwd(NULL, 0);
7860 if (repo_path == NULL)
7861 return got_error_from_errno("getcwd");
7864 error = got_repo_open(&repo, repo_path, NULL);
7865 free(repo_path);
7866 if (error != NULL)
7867 goto done;
7869 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7870 if (error)
7871 goto done;
7873 if (commit_id_str == NULL)
7874 commit_id_str = GOT_REF_HEAD;
7875 error = got_repo_match_object_id(&commit_id, NULL,
7876 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7877 if (error)
7878 goto done;
7880 for (i = 0; i < argc; i++) {
7881 if (force_path) {
7882 error = got_object_id_by_path(&id, repo, commit_id,
7883 argv[i]);
7884 if (error)
7885 break;
7886 } else {
7887 error = got_repo_match_object_id(&id, &label, argv[i],
7888 GOT_OBJ_TYPE_ANY, 0, repo);
7889 if (error) {
7890 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7891 error->code != GOT_ERR_NOT_REF)
7892 break;
7893 error = got_object_id_by_path(&id, repo,
7894 commit_id, argv[i]);
7895 if (error)
7896 break;
7900 error = got_object_get_type(&obj_type, repo, id);
7901 if (error)
7902 break;
7904 switch (obj_type) {
7905 case GOT_OBJ_TYPE_BLOB:
7906 error = cat_blob(id, repo, stdout);
7907 break;
7908 case GOT_OBJ_TYPE_TREE:
7909 error = cat_tree(id, repo, stdout);
7910 break;
7911 case GOT_OBJ_TYPE_COMMIT:
7912 error = cat_commit(id, repo, stdout);
7913 break;
7914 case GOT_OBJ_TYPE_TAG:
7915 error = cat_tag(id, repo, stdout);
7916 break;
7917 default:
7918 error = got_error(GOT_ERR_OBJ_TYPE);
7919 break;
7921 if (error)
7922 break;
7923 free(label);
7924 label = NULL;
7925 free(id);
7926 id = NULL;
7928 done:
7929 free(label);
7930 free(id);
7931 free(commit_id);
7932 if (worktree)
7933 got_worktree_close(worktree);
7934 if (repo) {
7935 const struct got_error *repo_error;
7936 repo_error = got_repo_close(repo);
7937 if (error == NULL)
7938 error = repo_error;
7940 return error;