Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
39 #include <getopt.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_diff.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
62 static void
63 catch_sigint(int signo)
64 {
65 sigint_received = 1;
66 }
68 static void
69 catch_sigpipe(int signo)
70 {
71 sigpipe_received = 1;
72 }
75 struct got_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
158 };
160 static void
161 list_commands(void)
163 int i;
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
170 fputc('\n', stderr);
173 int
174 main(int argc, char *argv[])
176 struct got_cmd *cmd;
177 unsigned int i;
178 int ch;
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
182 { NULL, 0, NULL, 0}
183 };
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 switch (ch) {
189 case 'h':
190 hflag = 1;
191 break;
192 case 'V':
193 Vflag = 1;
194 break;
195 default:
196 usage(hflag);
197 /* NOTREACHED */
201 argc -= optind;
202 argv += optind;
203 optind = 0;
205 if (Vflag) {
206 got_version_print_str();
207 return 1;
210 if (argc <= 0)
211 usage(hflag);
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
223 continue;
225 if (hflag)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 !(sigint_received &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 return 1;
239 return 0;
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 list_commands();
244 return 1;
247 __dead static void
248 usage(int hflag)
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 getprogname());
252 if (hflag)
253 list_commands();
254 exit(1);
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
261 const char *editor;
263 *abspath = NULL;
265 editor = getenv("VISUAL");
266 if (editor == NULL)
267 editor = getenv("EDITOR");
269 if (editor) {
270 err = got_path_find_prog(abspath, editor);
271 if (err)
272 return err;
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
281 return NULL;
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
290 #ifdef PROFILE
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
293 #endif
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
304 if (err != NULL)
305 return err;
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
310 return NULL;
313 __dead static void
314 usage_init(void)
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 exit(1);
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
325 int ch;
327 while ((ch = getopt(argc, argv, "")) != -1) {
328 switch (ch) {
329 default:
330 usage_init();
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
338 #ifndef PROFILE
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 err(1, "pledge");
341 #endif
342 if (argc != 1)
343 usage_init();
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
352 if (error &&
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 goto done;
356 error = apply_unveil(repo_path, 0, NULL);
357 if (error)
358 goto done;
360 error = got_repo_init(repo_path);
361 if (error != NULL)
362 goto done;
364 done:
365 free(repo_path);
366 return error;
369 __dead static void
370 usage_import(void)
372 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
373 "[-r repository-path] [-I pattern] path\n", getprogname());
374 exit(1);
377 int
378 spawn_editor(const char *editor, const char *file)
380 pid_t pid;
381 sig_t sighup, sigint, sigquit;
382 int st = -1;
384 sighup = signal(SIGHUP, SIG_IGN);
385 sigint = signal(SIGINT, SIG_IGN);
386 sigquit = signal(SIGQUIT, SIG_IGN);
388 switch (pid = fork()) {
389 case -1:
390 goto doneediting;
391 case 0:
392 execl(editor, editor, file, (char *)NULL);
393 _exit(127);
396 while (waitpid(pid, &st, 0) == -1)
397 if (errno != EINTR)
398 break;
400 doneediting:
401 (void)signal(SIGHUP, sighup);
402 (void)signal(SIGINT, sigint);
403 (void)signal(SIGQUIT, sigquit);
405 if (!WIFEXITED(st)) {
406 errno = EINTR;
407 return -1;
410 return WEXITSTATUS(st);
413 static const struct got_error *
414 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
415 const char *initial_content)
417 const struct got_error *err = NULL;
418 char buf[1024];
419 struct stat st, st2;
420 FILE *fp;
421 int content_changed = 0;
422 size_t len;
424 *logmsg = NULL;
426 if (stat(logmsg_path, &st) == -1)
427 return got_error_from_errno2("stat", logmsg_path);
429 if (spawn_editor(editor, logmsg_path) == -1)
430 return got_error_from_errno("failed spawning editor");
432 if (stat(logmsg_path, &st2) == -1)
433 return got_error_from_errno("stat");
435 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
436 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 "no changes made to commit message, aborting");
439 *logmsg = malloc(st2.st_size + 1);
440 if (*logmsg == NULL)
441 return got_error_from_errno("malloc");
442 (*logmsg)[0] = '\0';
443 len = 0;
445 fp = fopen(logmsg_path, "r");
446 if (fp == NULL) {
447 err = got_error_from_errno("fopen");
448 goto done;
450 while (fgets(buf, sizeof(buf), fp) != NULL) {
451 if (!content_changed && strcmp(buf, initial_content) != 0)
452 content_changed = 1;
453 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
454 continue; /* remove comments and leading empty lines */
455 len = strlcat(*logmsg, buf, st2.st_size);
457 fclose(fp);
459 while (len > 0 && (*logmsg)[len - 1] == '\n') {
460 (*logmsg)[len - 1] = '\0';
461 len--;
464 if (len == 0 || !content_changed)
465 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
466 "commit message cannot be empty, aborting");
467 done:
468 if (err) {
469 free(*logmsg);
470 *logmsg = NULL;
472 return err;
475 static const struct got_error *
476 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
477 const char *path_dir, const char *branch_name)
479 char *initial_content = NULL;
480 const struct got_error *err = NULL;
481 int fd;
483 if (asprintf(&initial_content,
484 "\n# %s to be imported to branch %s\n", path_dir,
485 branch_name) == -1)
486 return got_error_from_errno("asprintf");
488 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
489 if (err)
490 goto done;
492 dprintf(fd, initial_content);
493 close(fd);
495 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
496 done:
497 free(initial_content);
498 return err;
501 static const struct got_error *
502 import_progress(void *arg, const char *path)
504 printf("A %s\n", path);
505 return NULL;
508 static const struct got_error *
509 get_author(char **author, struct got_repository *repo)
511 const struct got_error *err = NULL;
512 const char *got_author, *name, *email;
514 *author = NULL;
516 name = got_repo_get_gitconfig_author_name(repo);
517 email = got_repo_get_gitconfig_author_email(repo);
518 if (name && email) {
519 if (asprintf(author, "%s <%s>", name, email) == -1)
520 return got_error_from_errno("asprintf");
521 return NULL;
524 got_author = getenv("GOT_AUTHOR");
525 if (got_author == NULL) {
526 name = got_repo_get_global_gitconfig_author_name(repo);
527 email = got_repo_get_global_gitconfig_author_email(repo);
528 if (name && email) {
529 if (asprintf(author, "%s <%s>", name, email) == -1)
530 return got_error_from_errno("asprintf");
531 return NULL;
533 /* TODO: Look up user in password database? */
534 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 *author = strdup(got_author);
538 if (*author == NULL)
539 return got_error_from_errno("strdup");
541 /*
542 * Really dumb email address check; we're only doing this to
543 * avoid git's object parser breaking on commits we create.
544 */
545 while (*got_author && *got_author != '<')
546 got_author++;
547 if (*got_author != '<') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 goto done;
551 while (*got_author && *got_author != '@')
552 got_author++;
553 if (*got_author != '@') {
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 goto done;
557 while (*got_author && *got_author != '>')
558 got_author++;
559 if (*got_author != '>')
560 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
561 done:
562 if (err) {
563 free(*author);
564 *author = NULL;
566 return err;
569 static const struct got_error *
570 get_gitconfig_path(char **gitconfig_path)
572 const char *homedir = getenv("HOME");
574 *gitconfig_path = NULL;
575 if (homedir) {
576 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
577 return got_error_from_errno("asprintf");
580 return NULL;
583 static const struct got_error *
584 cmd_import(int argc, char *argv[])
586 const struct got_error *error = NULL;
587 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
588 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
589 const char *branch_name = "main";
590 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
591 struct got_repository *repo = NULL;
592 struct got_reference *branch_ref = NULL, *head_ref = NULL;
593 struct got_object_id *new_commit_id = NULL;
594 int ch;
595 struct got_pathlist_head ignores;
596 struct got_pathlist_entry *pe;
597 int preserve_logmsg = 0;
599 TAILQ_INIT(&ignores);
601 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
602 switch (ch) {
603 case 'b':
604 branch_name = optarg;
605 break;
606 case 'm':
607 logmsg = strdup(optarg);
608 if (logmsg == NULL) {
609 error = got_error_from_errno("strdup");
610 goto done;
612 break;
613 case 'r':
614 repo_path = realpath(optarg, NULL);
615 if (repo_path == NULL) {
616 error = got_error_from_errno2("realpath",
617 optarg);
618 goto done;
620 break;
621 case 'I':
622 if (optarg[0] == '\0')
623 break;
624 error = got_pathlist_insert(&pe, &ignores, optarg,
625 NULL);
626 if (error)
627 goto done;
628 break;
629 default:
630 usage_import();
631 /* NOTREACHED */
635 argc -= optind;
636 argv += optind;
638 #ifndef PROFILE
639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
640 "unveil",
641 NULL) == -1)
642 err(1, "pledge");
643 #endif
644 if (argc != 1)
645 usage_import();
647 if (repo_path == NULL) {
648 repo_path = getcwd(NULL, 0);
649 if (repo_path == NULL)
650 return got_error_from_errno("getcwd");
652 got_path_strip_trailing_slashes(repo_path);
653 error = get_gitconfig_path(&gitconfig_path);
654 if (error)
655 goto done;
656 error = got_repo_open(&repo, repo_path, gitconfig_path);
657 if (error)
658 goto done;
660 error = get_author(&author, repo);
661 if (error)
662 return error;
664 /*
665 * Don't let the user create a branch name with a leading '-'.
666 * While technically a valid reference name, this case is usually
667 * an unintended typo.
668 */
669 if (branch_name[0] == '-')
670 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
672 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
673 error = got_error_from_errno("asprintf");
674 goto done;
677 error = got_ref_open(&branch_ref, repo, refname, 0);
678 if (error) {
679 if (error->code != GOT_ERR_NOT_REF)
680 goto done;
681 } else {
682 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
683 "import target branch already exists");
684 goto done;
687 path_dir = realpath(argv[0], NULL);
688 if (path_dir == NULL) {
689 error = got_error_from_errno2("realpath", argv[0]);
690 goto done;
692 got_path_strip_trailing_slashes(path_dir);
694 /*
695 * unveil(2) traverses exec(2); if an editor is used we have
696 * to apply unveil after the log message has been written.
697 */
698 if (logmsg == NULL || strlen(logmsg) == 0) {
699 error = get_editor(&editor);
700 if (error)
701 goto done;
702 free(logmsg);
703 error = collect_import_msg(&logmsg, &logmsg_path, editor,
704 path_dir, refname);
705 if (error) {
706 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
707 logmsg_path != NULL)
708 preserve_logmsg = 1;
709 goto done;
713 if (unveil(path_dir, "r") != 0) {
714 error = got_error_from_errno2("unveil", path_dir);
715 if (logmsg_path)
716 preserve_logmsg = 1;
717 goto done;
720 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
721 if (error) {
722 if (logmsg_path)
723 preserve_logmsg = 1;
724 goto done;
727 error = got_repo_import(&new_commit_id, path_dir, logmsg,
728 author, &ignores, repo, import_progress, NULL);
729 if (error) {
730 if (logmsg_path)
731 preserve_logmsg = 1;
732 goto done;
735 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
736 if (error) {
737 if (logmsg_path)
738 preserve_logmsg = 1;
739 goto done;
742 error = got_ref_write(branch_ref, repo);
743 if (error) {
744 if (logmsg_path)
745 preserve_logmsg = 1;
746 goto done;
749 error = got_object_id_str(&id_str, new_commit_id);
750 if (error) {
751 if (logmsg_path)
752 preserve_logmsg = 1;
753 goto done;
756 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
757 if (error) {
758 if (error->code != GOT_ERR_NOT_REF) {
759 if (logmsg_path)
760 preserve_logmsg = 1;
761 goto done;
764 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
765 branch_ref);
766 if (error) {
767 if (logmsg_path)
768 preserve_logmsg = 1;
769 goto done;
772 error = got_ref_write(head_ref, repo);
773 if (error) {
774 if (logmsg_path)
775 preserve_logmsg = 1;
776 goto done;
780 printf("Created branch %s with commit %s\n",
781 got_ref_get_name(branch_ref), id_str);
782 done:
783 if (preserve_logmsg) {
784 fprintf(stderr, "%s: log message preserved in %s\n",
785 getprogname(), logmsg_path);
786 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
787 error = got_error_from_errno2("unlink", logmsg_path);
788 free(logmsg);
789 free(logmsg_path);
790 free(repo_path);
791 free(editor);
792 free(refname);
793 free(new_commit_id);
794 free(id_str);
795 free(author);
796 free(gitconfig_path);
797 if (branch_ref)
798 got_ref_close(branch_ref);
799 if (head_ref)
800 got_ref_close(head_ref);
801 return error;
804 __dead static void
805 usage_checkout(void)
807 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
808 "[-p prefix] repository-path [worktree-path]\n", getprogname());
809 exit(1);
812 static void
813 show_worktree_base_ref_warning(void)
815 fprintf(stderr, "%s: warning: could not create a reference "
816 "to the work tree's base commit; the commit could be "
817 "garbage-collected by Git; making the repository "
818 "writable and running 'got update' will prevent this\n",
819 getprogname());
822 struct got_checkout_progress_arg {
823 const char *worktree_path;
824 int had_base_commit_ref_error;
825 };
827 static const struct got_error *
828 checkout_progress(void *arg, unsigned char status, const char *path)
830 struct got_checkout_progress_arg *a = arg;
832 /* Base commit bump happens silently. */
833 if (status == GOT_STATUS_BUMP_BASE)
834 return NULL;
836 if (status == GOT_STATUS_BASE_REF_ERR) {
837 a->had_base_commit_ref_error = 1;
838 return NULL;
841 while (path[0] == '/')
842 path++;
844 printf("%c %s/%s\n", status, a->worktree_path, path);
845 return NULL;
848 static const struct got_error *
849 check_cancelled(void *arg)
851 if (sigint_received || sigpipe_received)
852 return got_error(GOT_ERR_CANCELLED);
853 return NULL;
856 static const struct got_error *
857 check_linear_ancestry(struct got_object_id *commit_id,
858 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
859 struct got_repository *repo)
861 const struct got_error *err = NULL;
862 struct got_object_id *yca_id;
864 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
865 commit_id, base_commit_id, repo, check_cancelled, NULL);
866 if (err)
867 return err;
869 if (yca_id == NULL)
870 return got_error(GOT_ERR_ANCESTRY);
872 /*
873 * Require a straight line of history between the target commit
874 * and the work tree's base commit.
876 * Non-linear situations such as this require a rebase:
878 * (commit) D F (base_commit)
879 * \ /
880 * C E
881 * \ /
882 * B (yca)
883 * |
884 * A
886 * 'got update' only handles linear cases:
887 * Update forwards in time: A (base/yca) - B - C - D (commit)
888 * Update backwards in time: D (base) - C - B - A (commit/yca)
889 */
890 if (allow_forwards_in_time_only) {
891 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
893 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
894 got_object_id_cmp(base_commit_id, yca_id) != 0)
895 return got_error(GOT_ERR_ANCESTRY);
897 free(yca_id);
898 return NULL;
901 static const struct got_error *
902 check_same_branch(struct got_object_id *commit_id,
903 struct got_reference *head_ref, struct got_object_id *yca_id,
904 struct got_repository *repo)
906 const struct got_error *err = NULL;
907 struct got_commit_graph *graph = NULL;
908 struct got_object_id *head_commit_id = NULL;
909 int is_same_branch = 0;
911 err = got_ref_resolve(&head_commit_id, repo, head_ref);
912 if (err)
913 goto done;
915 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
916 is_same_branch = 1;
917 goto done;
919 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
920 is_same_branch = 1;
921 goto done;
924 err = got_commit_graph_open(&graph, "/", 1);
925 if (err)
926 goto done;
928 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
929 check_cancelled, NULL);
930 if (err)
931 goto done;
933 for (;;) {
934 struct got_object_id *id;
935 err = got_commit_graph_iter_next(&id, graph, repo,
936 check_cancelled, NULL);
937 if (err) {
938 if (err->code == GOT_ERR_ITER_COMPLETED)
939 err = NULL;
940 break;
943 if (id) {
944 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
945 break;
946 if (got_object_id_cmp(id, commit_id) == 0) {
947 is_same_branch = 1;
948 break;
952 done:
953 if (graph)
954 got_commit_graph_close(graph);
955 free(head_commit_id);
956 if (!err && !is_same_branch)
957 err = got_error(GOT_ERR_ANCESTRY);
958 return err;
961 static const struct got_error *
962 resolve_commit_arg(struct got_object_id **commit_id,
963 const char *commit_id_arg, struct got_repository *repo)
965 const struct got_error *err;
966 struct got_reference *ref;
967 struct got_tag_object *tag;
969 err = got_repo_object_match_tag(&tag, commit_id_arg,
970 GOT_OBJ_TYPE_COMMIT, repo);
971 if (err == NULL) {
972 *commit_id = got_object_id_dup(
973 got_object_tag_get_object_id(tag));
974 if (*commit_id == NULL)
975 err = got_error_from_errno("got_object_id_dup");
976 got_object_tag_close(tag);
977 return err;
978 } else if (err->code != GOT_ERR_NO_OBJ)
979 return err;
981 err = got_ref_open(&ref, repo, commit_id_arg, 0);
982 if (err == NULL) {
983 err = got_ref_resolve(commit_id, repo, ref);
984 got_ref_close(ref);
985 } else {
986 if (err->code != GOT_ERR_NOT_REF)
987 return err;
988 err = got_repo_match_object_id_prefix(commit_id,
989 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
991 return err;
994 static const struct got_error *
995 cmd_checkout(int argc, char *argv[])
997 const struct got_error *error = NULL;
998 struct got_repository *repo = NULL;
999 struct got_reference *head_ref = NULL;
1000 struct got_worktree *worktree = NULL;
1001 char *repo_path = NULL;
1002 char *worktree_path = NULL;
1003 const char *path_prefix = "";
1004 const char *branch_name = GOT_REF_HEAD;
1005 char *commit_id_str = NULL;
1006 int ch, same_path_prefix, allow_nonempty = 0;
1007 struct got_pathlist_head paths;
1008 struct got_checkout_progress_arg cpa;
1010 TAILQ_INIT(&paths);
1012 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1013 switch (ch) {
1014 case 'b':
1015 branch_name = optarg;
1016 break;
1017 case 'c':
1018 commit_id_str = strdup(optarg);
1019 if (commit_id_str == NULL)
1020 return got_error_from_errno("strdup");
1021 break;
1022 case 'E':
1023 allow_nonempty = 1;
1024 break;
1025 case 'p':
1026 path_prefix = optarg;
1027 break;
1028 default:
1029 usage_checkout();
1030 /* NOTREACHED */
1034 argc -= optind;
1035 argv += optind;
1037 #ifndef PROFILE
1038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1039 "unveil", NULL) == -1)
1040 err(1, "pledge");
1041 #endif
1042 if (argc == 1) {
1043 char *cwd, *base, *dotgit;
1044 repo_path = realpath(argv[0], NULL);
1045 if (repo_path == NULL)
1046 return got_error_from_errno2("realpath", argv[0]);
1047 cwd = getcwd(NULL, 0);
1048 if (cwd == NULL) {
1049 error = got_error_from_errno("getcwd");
1050 goto done;
1052 if (path_prefix[0]) {
1053 base = basename(path_prefix);
1054 if (base == NULL) {
1055 error = got_error_from_errno2("basename",
1056 path_prefix);
1057 goto done;
1059 } else {
1060 base = basename(repo_path);
1061 if (base == NULL) {
1062 error = got_error_from_errno2("basename",
1063 repo_path);
1064 goto done;
1067 dotgit = strstr(base, ".git");
1068 if (dotgit)
1069 *dotgit = '\0';
1070 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1071 error = got_error_from_errno("asprintf");
1072 free(cwd);
1073 goto done;
1075 free(cwd);
1076 } else if (argc == 2) {
1077 repo_path = realpath(argv[0], NULL);
1078 if (repo_path == NULL) {
1079 error = got_error_from_errno2("realpath", argv[0]);
1080 goto done;
1082 worktree_path = realpath(argv[1], NULL);
1083 if (worktree_path == NULL) {
1084 if (errno != ENOENT) {
1085 error = got_error_from_errno2("realpath",
1086 argv[1]);
1087 goto done;
1089 worktree_path = strdup(argv[1]);
1090 if (worktree_path == NULL) {
1091 error = got_error_from_errno("strdup");
1092 goto done;
1095 } else
1096 usage_checkout();
1098 got_path_strip_trailing_slashes(repo_path);
1099 got_path_strip_trailing_slashes(worktree_path);
1101 error = got_repo_open(&repo, repo_path, NULL);
1102 if (error != NULL)
1103 goto done;
1105 /* Pre-create work tree path for unveil(2) */
1106 error = got_path_mkdir(worktree_path);
1107 if (error) {
1108 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1109 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1110 goto done;
1111 if (!allow_nonempty &&
1112 !got_path_dir_is_empty(worktree_path)) {
1113 error = got_error_path(worktree_path,
1114 GOT_ERR_DIR_NOT_EMPTY);
1115 goto done;
1119 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1120 if (error)
1121 goto done;
1123 error = got_ref_open(&head_ref, repo, branch_name, 0);
1124 if (error != NULL)
1125 goto done;
1127 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1128 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1129 goto done;
1131 error = got_worktree_open(&worktree, worktree_path);
1132 if (error != NULL)
1133 goto done;
1135 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1136 path_prefix);
1137 if (error != NULL)
1138 goto done;
1139 if (!same_path_prefix) {
1140 error = got_error(GOT_ERR_PATH_PREFIX);
1141 goto done;
1144 if (commit_id_str) {
1145 struct got_object_id *commit_id;
1146 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1147 if (error)
1148 goto done;
1149 error = check_linear_ancestry(commit_id,
1150 got_worktree_get_base_commit_id(worktree), 0, repo);
1151 if (error != NULL) {
1152 free(commit_id);
1153 goto done;
1155 error = check_same_branch(commit_id, head_ref, NULL, repo);
1156 if (error)
1157 goto done;
1158 error = got_worktree_set_base_commit_id(worktree, repo,
1159 commit_id);
1160 free(commit_id);
1161 if (error)
1162 goto done;
1165 error = got_pathlist_append(&paths, "", NULL);
1166 if (error)
1167 goto done;
1168 cpa.worktree_path = worktree_path;
1169 cpa.had_base_commit_ref_error = 0;
1170 error = got_worktree_checkout_files(worktree, &paths, repo,
1171 checkout_progress, &cpa, check_cancelled, NULL);
1172 if (error != NULL)
1173 goto done;
1175 printf("Now shut up and hack\n");
1176 if (cpa.had_base_commit_ref_error)
1177 show_worktree_base_ref_warning();
1179 done:
1180 got_pathlist_free(&paths);
1181 free(commit_id_str);
1182 free(repo_path);
1183 free(worktree_path);
1184 return error;
1187 __dead static void
1188 usage_update(void)
1190 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1191 getprogname());
1192 exit(1);
1195 static const struct got_error *
1196 update_progress(void *arg, unsigned char status, const char *path)
1198 int *did_something = arg;
1200 if (status == GOT_STATUS_EXISTS ||
1201 status == GOT_STATUS_BASE_REF_ERR)
1202 return NULL;
1204 *did_something = 1;
1206 /* Base commit bump happens silently. */
1207 if (status == GOT_STATUS_BUMP_BASE)
1208 return NULL;
1210 while (path[0] == '/')
1211 path++;
1212 printf("%c %s\n", status, path);
1213 return NULL;
1216 static const struct got_error *
1217 switch_head_ref(struct got_reference *head_ref,
1218 struct got_object_id *commit_id, struct got_worktree *worktree,
1219 struct got_repository *repo)
1221 const struct got_error *err = NULL;
1222 char *base_id_str;
1223 int ref_has_moved = 0;
1225 /* Trivial case: switching between two different references. */
1226 if (strcmp(got_ref_get_name(head_ref),
1227 got_worktree_get_head_ref_name(worktree)) != 0) {
1228 printf("Switching work tree from %s to %s\n",
1229 got_worktree_get_head_ref_name(worktree),
1230 got_ref_get_name(head_ref));
1231 return got_worktree_set_head_ref(worktree, head_ref);
1234 err = check_linear_ancestry(commit_id,
1235 got_worktree_get_base_commit_id(worktree), 0, repo);
1236 if (err) {
1237 if (err->code != GOT_ERR_ANCESTRY)
1238 return err;
1239 ref_has_moved = 1;
1241 if (!ref_has_moved)
1242 return NULL;
1244 /* Switching to a rebased branch with the same reference name. */
1245 err = got_object_id_str(&base_id_str,
1246 got_worktree_get_base_commit_id(worktree));
1247 if (err)
1248 return err;
1249 printf("Reference %s now points at a different branch\n",
1250 got_worktree_get_head_ref_name(worktree));
1251 printf("Switching work tree from %s to %s\n", base_id_str,
1252 got_worktree_get_head_ref_name(worktree));
1253 return NULL;
1256 static const struct got_error *
1257 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1259 const struct got_error *err;
1260 int in_progress;
1262 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1263 if (err)
1264 return err;
1265 if (in_progress)
1266 return got_error(GOT_ERR_REBASING);
1268 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1269 if (err)
1270 return err;
1271 if (in_progress)
1272 return got_error(GOT_ERR_HISTEDIT_BUSY);
1274 return NULL;
1277 static const struct got_error *
1278 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1279 char *argv[], struct got_worktree *worktree)
1281 const struct got_error *err = NULL;
1282 char *path;
1283 int i;
1285 if (argc == 0) {
1286 path = strdup("");
1287 if (path == NULL)
1288 return got_error_from_errno("strdup");
1289 return got_pathlist_append(paths, path, NULL);
1292 for (i = 0; i < argc; i++) {
1293 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1294 if (err)
1295 break;
1296 err = got_pathlist_append(paths, path, NULL);
1297 if (err) {
1298 free(path);
1299 break;
1303 return err;
1306 static const struct got_error *
1307 cmd_update(int argc, char *argv[])
1309 const struct got_error *error = NULL;
1310 struct got_repository *repo = NULL;
1311 struct got_worktree *worktree = NULL;
1312 char *worktree_path = NULL;
1313 struct got_object_id *commit_id = NULL;
1314 char *commit_id_str = NULL;
1315 const char *branch_name = NULL;
1316 struct got_reference *head_ref = NULL;
1317 struct got_pathlist_head paths;
1318 struct got_pathlist_entry *pe;
1319 int ch, did_something = 0;
1321 TAILQ_INIT(&paths);
1323 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1324 switch (ch) {
1325 case 'b':
1326 branch_name = optarg;
1327 break;
1328 case 'c':
1329 commit_id_str = strdup(optarg);
1330 if (commit_id_str == NULL)
1331 return got_error_from_errno("strdup");
1332 break;
1333 default:
1334 usage_update();
1335 /* NOTREACHED */
1339 argc -= optind;
1340 argv += optind;
1342 #ifndef PROFILE
1343 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1344 "unveil", NULL) == -1)
1345 err(1, "pledge");
1346 #endif
1347 worktree_path = getcwd(NULL, 0);
1348 if (worktree_path == NULL) {
1349 error = got_error_from_errno("getcwd");
1350 goto done;
1352 error = got_worktree_open(&worktree, worktree_path);
1353 if (error)
1354 goto done;
1356 error = check_rebase_or_histedit_in_progress(worktree);
1357 if (error)
1358 goto done;
1360 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1361 NULL);
1362 if (error != NULL)
1363 goto done;
1365 error = apply_unveil(got_repo_get_path(repo), 0,
1366 got_worktree_get_root_path(worktree));
1367 if (error)
1368 goto done;
1370 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1371 if (error)
1372 goto done;
1374 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1375 got_worktree_get_head_ref_name(worktree), 0);
1376 if (error != NULL)
1377 goto done;
1378 if (commit_id_str == NULL) {
1379 error = got_ref_resolve(&commit_id, repo, head_ref);
1380 if (error != NULL)
1381 goto done;
1382 error = got_object_id_str(&commit_id_str, commit_id);
1383 if (error != NULL)
1384 goto done;
1385 } else {
1386 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1387 free(commit_id_str);
1388 commit_id_str = NULL;
1389 if (error)
1390 goto done;
1391 error = got_object_id_str(&commit_id_str, commit_id);
1392 if (error)
1393 goto done;
1396 if (branch_name) {
1397 struct got_object_id *head_commit_id;
1398 TAILQ_FOREACH(pe, &paths, entry) {
1399 if (pe->path_len == 0)
1400 continue;
1401 error = got_error_msg(GOT_ERR_BAD_PATH,
1402 "switching between branches requires that "
1403 "the entire work tree gets updated");
1404 goto done;
1406 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1407 if (error)
1408 goto done;
1409 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1410 repo);
1411 free(head_commit_id);
1412 if (error != NULL)
1413 goto done;
1414 error = check_same_branch(commit_id, head_ref, NULL, repo);
1415 if (error)
1416 goto done;
1417 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1418 if (error)
1419 goto done;
1420 } else {
1421 error = check_linear_ancestry(commit_id,
1422 got_worktree_get_base_commit_id(worktree), 0, repo);
1423 if (error != NULL) {
1424 if (error->code == GOT_ERR_ANCESTRY)
1425 error = got_error(GOT_ERR_BRANCH_MOVED);
1426 goto done;
1428 error = check_same_branch(commit_id, head_ref, NULL, repo);
1429 if (error)
1430 goto done;
1433 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1434 commit_id) != 0) {
1435 error = got_worktree_set_base_commit_id(worktree, repo,
1436 commit_id);
1437 if (error)
1438 goto done;
1441 error = got_worktree_checkout_files(worktree, &paths, repo,
1442 update_progress, &did_something, check_cancelled, NULL);
1443 if (error != NULL)
1444 goto done;
1446 if (did_something)
1447 printf("Updated to commit %s\n", commit_id_str);
1448 else
1449 printf("Already up-to-date\n");
1450 done:
1451 free(worktree_path);
1452 TAILQ_FOREACH(pe, &paths, entry)
1453 free((char *)pe->path);
1454 got_pathlist_free(&paths);
1455 free(commit_id);
1456 free(commit_id_str);
1457 return error;
1460 static const struct got_error *
1461 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1462 const char *path, int diff_context, int ignore_whitespace,
1463 struct got_repository *repo)
1465 const struct got_error *err = NULL;
1466 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1468 if (blob_id1) {
1469 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1470 if (err)
1471 goto done;
1474 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1475 if (err)
1476 goto done;
1478 while (path[0] == '/')
1479 path++;
1480 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1481 ignore_whitespace, stdout);
1482 done:
1483 if (blob1)
1484 got_object_blob_close(blob1);
1485 got_object_blob_close(blob2);
1486 return err;
1489 static const struct got_error *
1490 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1491 const char *path, int diff_context, int ignore_whitespace,
1492 struct got_repository *repo)
1494 const struct got_error *err = NULL;
1495 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1496 struct got_diff_blob_output_unidiff_arg arg;
1498 if (tree_id1) {
1499 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1500 if (err)
1501 goto done;
1504 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1505 if (err)
1506 goto done;
1508 arg.diff_context = diff_context;
1509 arg.ignore_whitespace = ignore_whitespace;
1510 arg.outfile = stdout;
1511 while (path[0] == '/')
1512 path++;
1513 err = got_diff_tree(tree1, tree2, path, path, repo,
1514 got_diff_blob_output_unidiff, &arg, 1);
1515 done:
1516 if (tree1)
1517 got_object_tree_close(tree1);
1518 if (tree2)
1519 got_object_tree_close(tree2);
1520 return err;
1523 static const struct got_error *
1524 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1525 const char *path, int diff_context, struct got_repository *repo)
1527 const struct got_error *err = NULL;
1528 struct got_commit_object *pcommit = NULL;
1529 char *id_str1 = NULL, *id_str2 = NULL;
1530 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1531 struct got_object_qid *qid;
1533 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1534 if (qid != NULL) {
1535 err = got_object_open_as_commit(&pcommit, repo,
1536 qid->id);
1537 if (err)
1538 return err;
1541 if (path && path[0] != '\0') {
1542 int obj_type;
1543 err = got_object_id_by_path(&obj_id2, repo, id, path);
1544 if (err)
1545 goto done;
1546 err = got_object_id_str(&id_str2, obj_id2);
1547 if (err) {
1548 free(obj_id2);
1549 goto done;
1551 if (pcommit) {
1552 err = got_object_id_by_path(&obj_id1, repo,
1553 qid->id, path);
1554 if (err) {
1555 free(obj_id2);
1556 goto done;
1558 err = got_object_id_str(&id_str1, obj_id1);
1559 if (err) {
1560 free(obj_id2);
1561 goto done;
1564 err = got_object_get_type(&obj_type, repo, obj_id2);
1565 if (err) {
1566 free(obj_id2);
1567 goto done;
1569 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1570 switch (obj_type) {
1571 case GOT_OBJ_TYPE_BLOB:
1572 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1573 0, repo);
1574 break;
1575 case GOT_OBJ_TYPE_TREE:
1576 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1577 0, repo);
1578 break;
1579 default:
1580 err = got_error(GOT_ERR_OBJ_TYPE);
1581 break;
1583 free(obj_id1);
1584 free(obj_id2);
1585 } else {
1586 obj_id2 = got_object_commit_get_tree_id(commit);
1587 err = got_object_id_str(&id_str2, obj_id2);
1588 if (err)
1589 goto done;
1590 obj_id1 = got_object_commit_get_tree_id(pcommit);
1591 err = got_object_id_str(&id_str1, obj_id1);
1592 if (err)
1593 goto done;
1594 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1595 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1598 done:
1599 free(id_str1);
1600 free(id_str2);
1601 if (pcommit)
1602 got_object_commit_close(pcommit);
1603 return err;
1606 static char *
1607 get_datestr(time_t *time, char *datebuf)
1609 struct tm mytm, *tm;
1610 char *p, *s;
1612 tm = gmtime_r(time, &mytm);
1613 if (tm == NULL)
1614 return NULL;
1615 s = asctime_r(tm, datebuf);
1616 if (s == NULL)
1617 return NULL;
1618 p = strchr(s, '\n');
1619 if (p)
1620 *p = '\0';
1621 return s;
1624 static const struct got_error *
1625 match_logmsg(int *have_match, struct got_object_id *id,
1626 struct got_commit_object *commit, regex_t *regex)
1628 const struct got_error *err = NULL;
1629 regmatch_t regmatch;
1630 char *id_str = NULL, *logmsg = NULL;
1632 *have_match = 0;
1634 err = got_object_id_str(&id_str, id);
1635 if (err)
1636 return err;
1638 err = got_object_commit_get_logmsg(&logmsg, commit);
1639 if (err)
1640 goto done;
1642 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1643 *have_match = 1;
1644 done:
1645 free(id_str);
1646 free(logmsg);
1647 return err;
1650 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1652 static const struct got_error *
1653 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1654 struct got_repository *repo, const char *path, int show_patch,
1655 int diff_context, struct got_reflist_head *refs)
1657 const struct got_error *err = NULL;
1658 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1659 char datebuf[26];
1660 time_t committer_time;
1661 const char *author, *committer;
1662 char *refs_str = NULL;
1663 struct got_reflist_entry *re;
1665 SIMPLEQ_FOREACH(re, refs, entry) {
1666 char *s;
1667 const char *name;
1668 struct got_tag_object *tag = NULL;
1669 int cmp;
1671 name = got_ref_get_name(re->ref);
1672 if (strcmp(name, GOT_REF_HEAD) == 0)
1673 continue;
1674 if (strncmp(name, "refs/", 5) == 0)
1675 name += 5;
1676 if (strncmp(name, "got/", 4) == 0)
1677 continue;
1678 if (strncmp(name, "heads/", 6) == 0)
1679 name += 6;
1680 if (strncmp(name, "remotes/", 8) == 0)
1681 name += 8;
1682 if (strncmp(name, "tags/", 5) == 0) {
1683 err = got_object_open_as_tag(&tag, repo, re->id);
1684 if (err) {
1685 if (err->code != GOT_ERR_OBJ_TYPE)
1686 return err;
1687 /* Ref points at something other than a tag. */
1688 err = NULL;
1689 tag = NULL;
1692 cmp = got_object_id_cmp(tag ?
1693 got_object_tag_get_object_id(tag) : re->id, id);
1694 if (tag)
1695 got_object_tag_close(tag);
1696 if (cmp != 0)
1697 continue;
1698 s = refs_str;
1699 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1700 name) == -1) {
1701 err = got_error_from_errno("asprintf");
1702 free(s);
1703 return err;
1705 free(s);
1707 err = got_object_id_str(&id_str, id);
1708 if (err)
1709 return err;
1711 printf(GOT_COMMIT_SEP_STR);
1712 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1713 refs_str ? refs_str : "", refs_str ? ")" : "");
1714 free(id_str);
1715 id_str = NULL;
1716 free(refs_str);
1717 refs_str = NULL;
1718 printf("from: %s\n", got_object_commit_get_author(commit));
1719 committer_time = got_object_commit_get_committer_time(commit);
1720 datestr = get_datestr(&committer_time, datebuf);
1721 if (datestr)
1722 printf("date: %s UTC\n", datestr);
1723 author = got_object_commit_get_author(commit);
1724 committer = got_object_commit_get_committer(commit);
1725 if (strcmp(author, committer) != 0)
1726 printf("via: %s\n", committer);
1727 if (got_object_commit_get_nparents(commit) > 1) {
1728 const struct got_object_id_queue *parent_ids;
1729 struct got_object_qid *qid;
1730 int n = 1;
1731 parent_ids = got_object_commit_get_parent_ids(commit);
1732 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1733 err = got_object_id_str(&id_str, qid->id);
1734 if (err)
1735 return err;
1736 printf("parent %d: %s\n", n++, id_str);
1737 free(id_str);
1741 err = got_object_commit_get_logmsg(&logmsg0, commit);
1742 if (err)
1743 return err;
1745 logmsg = logmsg0;
1746 do {
1747 line = strsep(&logmsg, "\n");
1748 if (line)
1749 printf(" %s\n", line);
1750 } while (line);
1751 free(logmsg0);
1753 if (show_patch) {
1754 err = print_patch(commit, id, path, diff_context, repo);
1755 if (err == 0)
1756 printf("\n");
1759 if (fflush(stdout) != 0 && err == NULL)
1760 err = got_error_from_errno("fflush");
1761 return err;
1764 static const struct got_error *
1765 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1766 const char *path, int show_patch, const char *search_pattern,
1767 int diff_context, int limit, int first_parent_traversal,
1768 struct got_reflist_head *refs)
1770 const struct got_error *err;
1771 struct got_commit_graph *graph;
1772 regex_t regex;
1773 int have_match;
1775 if (search_pattern &&
1776 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1777 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1779 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1780 if (err)
1781 return err;
1782 err = got_commit_graph_iter_start(graph, root_id, repo,
1783 check_cancelled, NULL);
1784 if (err)
1785 goto done;
1786 for (;;) {
1787 struct got_commit_object *commit;
1788 struct got_object_id *id;
1790 if (sigint_received || sigpipe_received)
1791 break;
1793 err = got_commit_graph_iter_next(&id, graph, repo,
1794 check_cancelled, NULL);
1795 if (err) {
1796 if (err->code == GOT_ERR_ITER_COMPLETED)
1797 err = NULL;
1798 break;
1800 if (id == NULL)
1801 break;
1803 err = got_object_open_as_commit(&commit, repo, id);
1804 if (err)
1805 break;
1807 if (search_pattern) {
1808 err = match_logmsg(&have_match, id, commit, &regex);
1809 if (err) {
1810 got_object_commit_close(commit);
1811 break;
1813 if (have_match == 0) {
1814 got_object_commit_close(commit);
1815 continue;
1819 err = print_commit(commit, id, repo, path, show_patch,
1820 diff_context, refs);
1821 got_object_commit_close(commit);
1822 if (err || (limit && --limit == 0))
1823 break;
1825 done:
1826 if (search_pattern)
1827 regfree(&regex);
1828 got_commit_graph_close(graph);
1829 return err;
1832 __dead static void
1833 usage_log(void)
1835 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1836 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1837 exit(1);
1840 static int
1841 get_default_log_limit(void)
1843 const char *got_default_log_limit;
1844 long long n;
1845 const char *errstr;
1847 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1848 if (got_default_log_limit == NULL)
1849 return 0;
1850 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1851 if (errstr != NULL)
1852 return 0;
1853 return n;
1856 static const struct got_error *
1857 cmd_log(int argc, char *argv[])
1859 const struct got_error *error;
1860 struct got_repository *repo = NULL;
1861 struct got_worktree *worktree = NULL;
1862 struct got_commit_object *commit = NULL;
1863 struct got_object_id *id = NULL;
1864 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1865 const char *start_commit = NULL, *search_pattern = NULL;
1866 int diff_context = -1, ch;
1867 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1868 const char *errstr;
1869 struct got_reflist_head refs;
1871 SIMPLEQ_INIT(&refs);
1873 #ifndef PROFILE
1874 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1875 NULL)
1876 == -1)
1877 err(1, "pledge");
1878 #endif
1880 limit = get_default_log_limit();
1882 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1883 switch (ch) {
1884 case 'p':
1885 show_patch = 1;
1886 break;
1887 case 'c':
1888 start_commit = optarg;
1889 break;
1890 case 'C':
1891 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1892 &errstr);
1893 if (errstr != NULL)
1894 err(1, "-C option %s", errstr);
1895 break;
1896 case 'l':
1897 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1898 if (errstr != NULL)
1899 err(1, "-l option %s", errstr);
1900 break;
1901 case 'f':
1902 first_parent_traversal = 1;
1903 break;
1904 case 'r':
1905 repo_path = realpath(optarg, NULL);
1906 if (repo_path == NULL)
1907 return got_error_from_errno2("realpath",
1908 optarg);
1909 got_path_strip_trailing_slashes(repo_path);
1910 break;
1911 case 's':
1912 search_pattern = optarg;
1913 break;
1914 default:
1915 usage_log();
1916 /* NOTREACHED */
1920 argc -= optind;
1921 argv += optind;
1923 if (diff_context == -1)
1924 diff_context = 3;
1925 else if (!show_patch)
1926 errx(1, "-C reguires -p");
1928 cwd = getcwd(NULL, 0);
1929 if (cwd == NULL) {
1930 error = got_error_from_errno("getcwd");
1931 goto done;
1934 error = got_worktree_open(&worktree, cwd);
1935 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1936 goto done;
1937 error = NULL;
1939 if (argc == 0) {
1940 path = strdup("");
1941 if (path == NULL) {
1942 error = got_error_from_errno("strdup");
1943 goto done;
1945 } else if (argc == 1) {
1946 if (worktree) {
1947 error = got_worktree_resolve_path(&path, worktree,
1948 argv[0]);
1949 if (error)
1950 goto done;
1951 } else {
1952 path = strdup(argv[0]);
1953 if (path == NULL) {
1954 error = got_error_from_errno("strdup");
1955 goto done;
1958 } else
1959 usage_log();
1961 if (repo_path == NULL) {
1962 repo_path = worktree ?
1963 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1965 if (repo_path == NULL) {
1966 error = got_error_from_errno("strdup");
1967 goto done;
1970 error = got_repo_open(&repo, repo_path, NULL);
1971 if (error != NULL)
1972 goto done;
1974 error = apply_unveil(got_repo_get_path(repo), 1,
1975 worktree ? got_worktree_get_root_path(worktree) : NULL);
1976 if (error)
1977 goto done;
1979 if (start_commit == NULL) {
1980 struct got_reference *head_ref;
1981 error = got_ref_open(&head_ref, repo,
1982 worktree ? got_worktree_get_head_ref_name(worktree)
1983 : GOT_REF_HEAD, 0);
1984 if (error != NULL)
1985 return error;
1986 error = got_ref_resolve(&id, repo, head_ref);
1987 got_ref_close(head_ref);
1988 if (error != NULL)
1989 return error;
1990 error = got_object_open_as_commit(&commit, repo, id);
1991 } else {
1992 struct got_reference *ref;
1993 error = got_ref_open(&ref, repo, start_commit, 0);
1994 if (error == NULL) {
1995 int obj_type;
1996 error = got_ref_resolve(&id, repo, ref);
1997 got_ref_close(ref);
1998 if (error != NULL)
1999 goto done;
2000 error = got_object_get_type(&obj_type, repo, id);
2001 if (error != NULL)
2002 goto done;
2003 if (obj_type == GOT_OBJ_TYPE_TAG) {
2004 struct got_tag_object *tag;
2005 error = got_object_open_as_tag(&tag, repo, id);
2006 if (error != NULL)
2007 goto done;
2008 if (got_object_tag_get_object_type(tag) !=
2009 GOT_OBJ_TYPE_COMMIT) {
2010 got_object_tag_close(tag);
2011 error = got_error(GOT_ERR_OBJ_TYPE);
2012 goto done;
2014 free(id);
2015 id = got_object_id_dup(
2016 got_object_tag_get_object_id(tag));
2017 if (id == NULL)
2018 error = got_error_from_errno(
2019 "got_object_id_dup");
2020 got_object_tag_close(tag);
2021 if (error)
2022 goto done;
2023 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2024 error = got_error(GOT_ERR_OBJ_TYPE);
2025 goto done;
2027 error = got_object_open_as_commit(&commit, repo, id);
2028 if (error != NULL)
2029 goto done;
2031 if (commit == NULL) {
2032 error = got_repo_match_object_id_prefix(&id,
2033 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2034 if (error != NULL)
2035 return error;
2038 if (error != NULL)
2039 goto done;
2041 if (worktree) {
2042 const char *prefix = got_worktree_get_path_prefix(worktree);
2043 char *p;
2044 if (asprintf(&p, "%s%s%s", prefix,
2045 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2046 error = got_error_from_errno("asprintf");
2047 goto done;
2049 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2050 free(p);
2051 } else
2052 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2053 if (error != NULL)
2054 goto done;
2055 if (in_repo_path) {
2056 free(path);
2057 path = in_repo_path;
2060 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2061 if (error)
2062 goto done;
2064 error = print_commits(id, repo, path, show_patch, search_pattern,
2065 diff_context, limit, first_parent_traversal, &refs);
2066 done:
2067 free(path);
2068 free(repo_path);
2069 free(cwd);
2070 free(id);
2071 if (worktree)
2072 got_worktree_close(worktree);
2073 if (repo) {
2074 const struct got_error *repo_error;
2075 repo_error = got_repo_close(repo);
2076 if (error == NULL)
2077 error = repo_error;
2079 got_ref_list_free(&refs);
2080 return error;
2083 __dead static void
2084 usage_diff(void)
2086 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2087 "[-w] [object1 object2 | path]\n", getprogname());
2088 exit(1);
2091 struct print_diff_arg {
2092 struct got_repository *repo;
2093 struct got_worktree *worktree;
2094 int diff_context;
2095 const char *id_str;
2096 int header_shown;
2097 int diff_staged;
2098 int ignore_whitespace;
2101 static const struct got_error *
2102 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2103 const char *path, struct got_object_id *blob_id,
2104 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2105 int dirfd, const char *de_name)
2107 struct print_diff_arg *a = arg;
2108 const struct got_error *err = NULL;
2109 struct got_blob_object *blob1 = NULL;
2110 int fd = -1;
2111 FILE *f2 = NULL;
2112 char *abspath = NULL, *label1 = NULL;
2113 struct stat sb;
2115 if (a->diff_staged) {
2116 if (staged_status != GOT_STATUS_MODIFY &&
2117 staged_status != GOT_STATUS_ADD &&
2118 staged_status != GOT_STATUS_DELETE)
2119 return NULL;
2120 } else {
2121 if (staged_status == GOT_STATUS_DELETE)
2122 return NULL;
2123 if (status == GOT_STATUS_NONEXISTENT)
2124 return got_error_set_errno(ENOENT, path);
2125 if (status != GOT_STATUS_MODIFY &&
2126 status != GOT_STATUS_ADD &&
2127 status != GOT_STATUS_DELETE &&
2128 status != GOT_STATUS_CONFLICT)
2129 return NULL;
2132 if (!a->header_shown) {
2133 printf("diff %s %s%s\n", a->id_str,
2134 got_worktree_get_root_path(a->worktree),
2135 a->diff_staged ? " (staged changes)" : "");
2136 a->header_shown = 1;
2139 if (a->diff_staged) {
2140 const char *label1 = NULL, *label2 = NULL;
2141 switch (staged_status) {
2142 case GOT_STATUS_MODIFY:
2143 label1 = path;
2144 label2 = path;
2145 break;
2146 case GOT_STATUS_ADD:
2147 label2 = path;
2148 break;
2149 case GOT_STATUS_DELETE:
2150 label1 = path;
2151 break;
2152 default:
2153 return got_error(GOT_ERR_FILE_STATUS);
2155 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2156 label1, label2, a->diff_context, a->ignore_whitespace,
2157 a->repo, stdout);
2160 if (staged_status == GOT_STATUS_ADD ||
2161 staged_status == GOT_STATUS_MODIFY) {
2162 char *id_str;
2163 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2164 8192);
2165 if (err)
2166 goto done;
2167 err = got_object_id_str(&id_str, staged_blob_id);
2168 if (err)
2169 goto done;
2170 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2171 err = got_error_from_errno("asprintf");
2172 free(id_str);
2173 goto done;
2175 free(id_str);
2176 } else if (status != GOT_STATUS_ADD) {
2177 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2178 if (err)
2179 goto done;
2182 if (status != GOT_STATUS_DELETE) {
2183 if (asprintf(&abspath, "%s/%s",
2184 got_worktree_get_root_path(a->worktree), path) == -1) {
2185 err = got_error_from_errno("asprintf");
2186 goto done;
2189 if (dirfd != -1) {
2190 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2191 if (fd == -1) {
2192 err = got_error_from_errno2("openat", abspath);
2193 goto done;
2195 } else {
2196 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2197 if (fd == -1) {
2198 err = got_error_from_errno2("open", abspath);
2199 goto done;
2202 if (fstat(fd, &sb) == -1) {
2203 err = got_error_from_errno2("fstat", abspath);
2204 goto done;
2206 f2 = fdopen(fd, "r");
2207 if (f2 == NULL) {
2208 err = got_error_from_errno2("fdopen", abspath);
2209 goto done;
2211 fd = -1;
2212 } else
2213 sb.st_size = 0;
2215 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2216 a->diff_context, a->ignore_whitespace, stdout);
2217 done:
2218 if (blob1)
2219 got_object_blob_close(blob1);
2220 if (f2 && fclose(f2) == EOF && err == NULL)
2221 err = got_error_from_errno("fclose");
2222 if (fd != -1 && close(fd) == -1 && err == NULL)
2223 err = got_error_from_errno("close");
2224 free(abspath);
2225 return err;
2228 static const struct got_error *
2229 match_object_id(struct got_object_id **id, char **label,
2230 const char *id_str, int obj_type, int resolve_tags,
2231 struct got_repository *repo)
2233 const struct got_error *err;
2234 struct got_tag_object *tag;
2235 struct got_reference *ref = NULL;
2237 *id = NULL;
2238 *label = NULL;
2240 if (resolve_tags) {
2241 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2242 repo);
2243 if (err == NULL) {
2244 *id = got_object_id_dup(
2245 got_object_tag_get_object_id(tag));
2246 if (*id == NULL)
2247 err = got_error_from_errno("got_object_id_dup");
2248 else if (asprintf(label, "refs/tags/%s",
2249 got_object_tag_get_name(tag)) == -1) {
2250 err = got_error_from_errno("asprintf");
2251 free(*id);
2252 *id = NULL;
2254 got_object_tag_close(tag);
2255 return err;
2256 } else if (err->code != GOT_ERR_NO_OBJ)
2257 return err;
2260 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2261 if (err) {
2262 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2263 return err;
2264 err = got_ref_open(&ref, repo, id_str, 0);
2265 if (err != NULL)
2266 goto done;
2267 *label = strdup(got_ref_get_name(ref));
2268 if (*label == NULL) {
2269 err = got_error_from_errno("strdup");
2270 goto done;
2272 err = got_ref_resolve(id, repo, ref);
2273 } else {
2274 err = got_object_id_str(label, *id);
2275 if (*label == NULL) {
2276 err = got_error_from_errno("strdup");
2277 goto done;
2280 done:
2281 if (ref)
2282 got_ref_close(ref);
2283 return err;
2287 static const struct got_error *
2288 cmd_diff(int argc, char *argv[])
2290 const struct got_error *error;
2291 struct got_repository *repo = NULL;
2292 struct got_worktree *worktree = NULL;
2293 char *cwd = NULL, *repo_path = NULL;
2294 struct got_object_id *id1 = NULL, *id2 = NULL;
2295 const char *id_str1 = NULL, *id_str2 = NULL;
2296 char *label1 = NULL, *label2 = NULL;
2297 int type1, type2;
2298 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2299 const char *errstr;
2300 char *path = NULL;
2302 #ifndef PROFILE
2303 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2304 NULL) == -1)
2305 err(1, "pledge");
2306 #endif
2308 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2309 switch (ch) {
2310 case 'C':
2311 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2312 &errstr);
2313 if (errstr != NULL)
2314 err(1, "-C option %s", errstr);
2315 break;
2316 case 'r':
2317 repo_path = realpath(optarg, NULL);
2318 if (repo_path == NULL)
2319 return got_error_from_errno2("realpath",
2320 optarg);
2321 got_path_strip_trailing_slashes(repo_path);
2322 break;
2323 case 's':
2324 diff_staged = 1;
2325 break;
2326 case 'w':
2327 ignore_whitespace = 1;
2328 break;
2329 default:
2330 usage_diff();
2331 /* NOTREACHED */
2335 argc -= optind;
2336 argv += optind;
2338 cwd = getcwd(NULL, 0);
2339 if (cwd == NULL) {
2340 error = got_error_from_errno("getcwd");
2341 goto done;
2343 error = got_worktree_open(&worktree, cwd);
2344 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2345 goto done;
2346 if (argc <= 1) {
2347 if (worktree == NULL) {
2348 error = got_error(GOT_ERR_NOT_WORKTREE);
2349 goto done;
2351 if (repo_path)
2352 errx(1,
2353 "-r option can't be used when diffing a work tree");
2354 repo_path = strdup(got_worktree_get_repo_path(worktree));
2355 if (repo_path == NULL) {
2356 error = got_error_from_errno("strdup");
2357 goto done;
2359 if (argc == 1) {
2360 error = got_worktree_resolve_path(&path, worktree,
2361 argv[0]);
2362 if (error)
2363 goto done;
2364 } else {
2365 path = strdup("");
2366 if (path == NULL) {
2367 error = got_error_from_errno("strdup");
2368 goto done;
2371 } else if (argc == 2) {
2372 if (diff_staged)
2373 errx(1, "-s option can't be used when diffing "
2374 "objects in repository");
2375 id_str1 = argv[0];
2376 id_str2 = argv[1];
2377 if (worktree && repo_path == NULL) {
2378 repo_path =
2379 strdup(got_worktree_get_repo_path(worktree));
2380 if (repo_path == NULL) {
2381 error = got_error_from_errno("strdup");
2382 goto done;
2385 } else
2386 usage_diff();
2388 if (repo_path == NULL) {
2389 repo_path = getcwd(NULL, 0);
2390 if (repo_path == NULL)
2391 return got_error_from_errno("getcwd");
2394 error = got_repo_open(&repo, repo_path, NULL);
2395 free(repo_path);
2396 if (error != NULL)
2397 goto done;
2399 error = apply_unveil(got_repo_get_path(repo), 1,
2400 worktree ? got_worktree_get_root_path(worktree) : NULL);
2401 if (error)
2402 goto done;
2404 if (argc <= 1) {
2405 struct print_diff_arg arg;
2406 struct got_pathlist_head paths;
2407 char *id_str;
2409 TAILQ_INIT(&paths);
2411 error = got_object_id_str(&id_str,
2412 got_worktree_get_base_commit_id(worktree));
2413 if (error)
2414 goto done;
2415 arg.repo = repo;
2416 arg.worktree = worktree;
2417 arg.diff_context = diff_context;
2418 arg.id_str = id_str;
2419 arg.header_shown = 0;
2420 arg.diff_staged = diff_staged;
2421 arg.ignore_whitespace = ignore_whitespace;
2423 error = got_pathlist_append(&paths, path, NULL);
2424 if (error)
2425 goto done;
2427 error = got_worktree_status(worktree, &paths, repo, print_diff,
2428 &arg, check_cancelled, NULL);
2429 free(id_str);
2430 got_pathlist_free(&paths);
2431 goto done;
2434 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2435 repo);
2436 if (error)
2437 goto done;
2439 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2440 repo);
2441 if (error)
2442 goto done;
2444 error = got_object_get_type(&type1, repo, id1);
2445 if (error)
2446 goto done;
2448 error = got_object_get_type(&type2, repo, id2);
2449 if (error)
2450 goto done;
2452 if (type1 != type2) {
2453 error = got_error(GOT_ERR_OBJ_TYPE);
2454 goto done;
2457 switch (type1) {
2458 case GOT_OBJ_TYPE_BLOB:
2459 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2460 diff_context, ignore_whitespace, repo, stdout);
2461 break;
2462 case GOT_OBJ_TYPE_TREE:
2463 error = got_diff_objects_as_trees(id1, id2, "", "",
2464 diff_context, ignore_whitespace, repo, stdout);
2465 break;
2466 case GOT_OBJ_TYPE_COMMIT:
2467 printf("diff %s %s\n", label1, label2);
2468 error = got_diff_objects_as_commits(id1, id2, diff_context,
2469 ignore_whitespace, repo, stdout);
2470 break;
2471 default:
2472 error = got_error(GOT_ERR_OBJ_TYPE);
2475 done:
2476 free(label1);
2477 free(label2);
2478 free(id1);
2479 free(id2);
2480 free(path);
2481 if (worktree)
2482 got_worktree_close(worktree);
2483 if (repo) {
2484 const struct got_error *repo_error;
2485 repo_error = got_repo_close(repo);
2486 if (error == NULL)
2487 error = repo_error;
2489 return error;
2492 __dead static void
2493 usage_blame(void)
2495 fprintf(stderr,
2496 "usage: %s blame [-c commit] [-r repository-path] path\n",
2497 getprogname());
2498 exit(1);
2501 struct blame_line {
2502 int annotated;
2503 char *id_str;
2504 char *committer;
2505 char datebuf[11]; /* YYYY-MM-DD + NUL */
2508 struct blame_cb_args {
2509 struct blame_line *lines;
2510 int nlines;
2511 int nlines_prec;
2512 int lineno_cur;
2513 off_t *line_offsets;
2514 FILE *f;
2515 struct got_repository *repo;
2518 static const struct got_error *
2519 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2521 const struct got_error *err = NULL;
2522 struct blame_cb_args *a = arg;
2523 struct blame_line *bline;
2524 char *line = NULL;
2525 size_t linesize = 0;
2526 struct got_commit_object *commit = NULL;
2527 off_t offset;
2528 struct tm tm;
2529 time_t committer_time;
2531 if (nlines != a->nlines ||
2532 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2533 return got_error(GOT_ERR_RANGE);
2535 if (sigint_received)
2536 return got_error(GOT_ERR_ITER_COMPLETED);
2538 if (lineno == -1)
2539 return NULL; /* no change in this commit */
2541 /* Annotate this line. */
2542 bline = &a->lines[lineno - 1];
2543 if (bline->annotated)
2544 return NULL;
2545 err = got_object_id_str(&bline->id_str, id);
2546 if (err)
2547 return err;
2549 err = got_object_open_as_commit(&commit, a->repo, id);
2550 if (err)
2551 goto done;
2553 bline->committer = strdup(got_object_commit_get_committer(commit));
2554 if (bline->committer == NULL) {
2555 err = got_error_from_errno("strdup");
2556 goto done;
2559 committer_time = got_object_commit_get_committer_time(commit);
2560 if (localtime_r(&committer_time, &tm) == NULL)
2561 return got_error_from_errno("localtime_r");
2562 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2563 &tm) >= sizeof(bline->datebuf)) {
2564 err = got_error(GOT_ERR_NO_SPACE);
2565 goto done;
2567 bline->annotated = 1;
2569 /* Print lines annotated so far. */
2570 bline = &a->lines[a->lineno_cur - 1];
2571 if (!bline->annotated)
2572 goto done;
2574 offset = a->line_offsets[a->lineno_cur - 1];
2575 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2576 err = got_error_from_errno("fseeko");
2577 goto done;
2580 while (bline->annotated) {
2581 char *smallerthan, *at, *nl, *committer;
2582 size_t len;
2584 if (getline(&line, &linesize, a->f) == -1) {
2585 if (ferror(a->f))
2586 err = got_error_from_errno("getline");
2587 break;
2590 committer = bline->committer;
2591 smallerthan = strchr(committer, '<');
2592 if (smallerthan && smallerthan[1] != '\0')
2593 committer = smallerthan + 1;
2594 at = strchr(committer, '@');
2595 if (at)
2596 *at = '\0';
2597 len = strlen(committer);
2598 if (len >= 9)
2599 committer[8] = '\0';
2601 nl = strchr(line, '\n');
2602 if (nl)
2603 *nl = '\0';
2604 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2605 bline->id_str, bline->datebuf, committer, line);
2607 a->lineno_cur++;
2608 bline = &a->lines[a->lineno_cur - 1];
2610 done:
2611 if (commit)
2612 got_object_commit_close(commit);
2613 free(line);
2614 return err;
2617 static const struct got_error *
2618 cmd_blame(int argc, char *argv[])
2620 const struct got_error *error;
2621 struct got_repository *repo = NULL;
2622 struct got_worktree *worktree = NULL;
2623 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2624 struct got_object_id *obj_id = NULL;
2625 struct got_object_id *commit_id = NULL;
2626 struct got_blob_object *blob = NULL;
2627 char *commit_id_str = NULL;
2628 struct blame_cb_args bca;
2629 int ch, obj_type, i;
2630 size_t filesize;
2632 memset(&bca, 0, sizeof(bca));
2634 #ifndef PROFILE
2635 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2636 NULL) == -1)
2637 err(1, "pledge");
2638 #endif
2640 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2641 switch (ch) {
2642 case 'c':
2643 commit_id_str = optarg;
2644 break;
2645 case 'r':
2646 repo_path = realpath(optarg, NULL);
2647 if (repo_path == NULL)
2648 return got_error_from_errno2("realpath",
2649 optarg);
2650 got_path_strip_trailing_slashes(repo_path);
2651 break;
2652 default:
2653 usage_blame();
2654 /* NOTREACHED */
2658 argc -= optind;
2659 argv += optind;
2661 if (argc == 1)
2662 path = argv[0];
2663 else
2664 usage_blame();
2666 cwd = getcwd(NULL, 0);
2667 if (cwd == NULL) {
2668 error = got_error_from_errno("getcwd");
2669 goto done;
2671 if (repo_path == NULL) {
2672 error = got_worktree_open(&worktree, cwd);
2673 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2674 goto done;
2675 else
2676 error = NULL;
2677 if (worktree) {
2678 repo_path =
2679 strdup(got_worktree_get_repo_path(worktree));
2680 if (repo_path == NULL)
2681 error = got_error_from_errno("strdup");
2682 if (error)
2683 goto done;
2684 } else {
2685 repo_path = strdup(cwd);
2686 if (repo_path == NULL) {
2687 error = got_error_from_errno("strdup");
2688 goto done;
2693 error = got_repo_open(&repo, repo_path, NULL);
2694 if (error != NULL)
2695 goto done;
2697 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2698 if (error)
2699 goto done;
2701 if (worktree) {
2702 const char *prefix = got_worktree_get_path_prefix(worktree);
2703 char *p, *worktree_subdir = cwd +
2704 strlen(got_worktree_get_root_path(worktree));
2705 if (asprintf(&p, "%s%s%s%s%s",
2706 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2707 worktree_subdir, worktree_subdir[0] ? "/" : "",
2708 path) == -1) {
2709 error = got_error_from_errno("asprintf");
2710 goto done;
2712 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2713 free(p);
2714 } else {
2715 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2717 if (error)
2718 goto done;
2720 if (commit_id_str == NULL) {
2721 struct got_reference *head_ref;
2722 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2723 if (error != NULL)
2724 goto done;
2725 error = got_ref_resolve(&commit_id, repo, head_ref);
2726 got_ref_close(head_ref);
2727 if (error != NULL)
2728 goto done;
2729 } else {
2730 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2731 if (error)
2732 goto done;
2735 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2736 if (error)
2737 goto done;
2738 if (obj_id == NULL) {
2739 error = got_error(GOT_ERR_NO_OBJ);
2740 goto done;
2743 error = got_object_get_type(&obj_type, repo, obj_id);
2744 if (error)
2745 goto done;
2747 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2748 error = got_error(GOT_ERR_OBJ_TYPE);
2749 goto done;
2752 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2753 if (error)
2754 goto done;
2755 bca.f = got_opentemp();
2756 if (bca.f == NULL) {
2757 error = got_error_from_errno("got_opentemp");
2758 goto done;
2760 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2761 &bca.line_offsets, bca.f, blob);
2762 if (error || bca.nlines == 0)
2763 goto done;
2765 /* Don't include \n at EOF in the blame line count. */
2766 if (bca.line_offsets[bca.nlines - 1] == filesize)
2767 bca.nlines--;
2769 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2770 if (bca.lines == NULL) {
2771 error = got_error_from_errno("calloc");
2772 goto done;
2774 bca.lineno_cur = 1;
2775 bca.nlines_prec = 0;
2776 i = bca.nlines;
2777 while (i > 0) {
2778 i /= 10;
2779 bca.nlines_prec++;
2781 bca.repo = repo;
2783 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2784 check_cancelled, NULL);
2785 if (error)
2786 goto done;
2787 done:
2788 free(in_repo_path);
2789 free(repo_path);
2790 free(cwd);
2791 free(commit_id);
2792 free(obj_id);
2793 if (blob)
2794 got_object_blob_close(blob);
2795 if (worktree)
2796 got_worktree_close(worktree);
2797 if (repo) {
2798 const struct got_error *repo_error;
2799 repo_error = got_repo_close(repo);
2800 if (error == NULL)
2801 error = repo_error;
2803 if (bca.lines) {
2804 for (i = 0; i < bca.nlines; i++) {
2805 struct blame_line *bline = &bca.lines[i];
2806 free(bline->id_str);
2807 free(bline->committer);
2809 free(bca.lines);
2811 free(bca.line_offsets);
2812 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2813 error = got_error_from_errno("fclose");
2814 return error;
2817 __dead static void
2818 usage_tree(void)
2820 fprintf(stderr,
2821 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2822 getprogname());
2823 exit(1);
2826 static void
2827 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2828 const char *root_path)
2830 int is_root_path = (strcmp(path, root_path) == 0);
2831 const char *modestr = "";
2832 mode_t mode = got_tree_entry_get_mode(te);
2834 path += strlen(root_path);
2835 while (path[0] == '/')
2836 path++;
2838 if (got_object_tree_entry_is_submodule(te))
2839 modestr = "$";
2840 else if (S_ISLNK(mode))
2841 modestr = "@";
2842 else if (S_ISDIR(mode))
2843 modestr = "/";
2844 else if (mode & S_IXUSR)
2845 modestr = "*";
2847 printf("%s%s%s%s%s\n", id ? id : "", path,
2848 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2851 static const struct got_error *
2852 print_tree(const char *path, struct got_object_id *commit_id,
2853 int show_ids, int recurse, const char *root_path,
2854 struct got_repository *repo)
2856 const struct got_error *err = NULL;
2857 struct got_object_id *tree_id = NULL;
2858 struct got_tree_object *tree = NULL;
2859 int nentries, i;
2861 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2862 if (err)
2863 goto done;
2865 err = got_object_open_as_tree(&tree, repo, tree_id);
2866 if (err)
2867 goto done;
2868 nentries = got_object_tree_get_nentries(tree);
2869 for (i = 0; i < nentries; i++) {
2870 struct got_tree_entry *te;
2871 char *id = NULL;
2873 if (sigint_received || sigpipe_received)
2874 break;
2876 te = got_object_tree_get_entry(tree, i);
2877 if (show_ids) {
2878 char *id_str;
2879 err = got_object_id_str(&id_str,
2880 got_tree_entry_get_id(te));
2881 if (err)
2882 goto done;
2883 if (asprintf(&id, "%s ", id_str) == -1) {
2884 err = got_error_from_errno("asprintf");
2885 free(id_str);
2886 goto done;
2888 free(id_str);
2890 print_entry(te, id, path, root_path);
2891 free(id);
2893 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2894 char *child_path;
2895 if (asprintf(&child_path, "%s%s%s", path,
2896 path[0] == '/' && path[1] == '\0' ? "" : "/",
2897 got_tree_entry_get_name(te)) == -1) {
2898 err = got_error_from_errno("asprintf");
2899 goto done;
2901 err = print_tree(child_path, commit_id, show_ids, 1,
2902 root_path, repo);
2903 free(child_path);
2904 if (err)
2905 goto done;
2908 done:
2909 if (tree)
2910 got_object_tree_close(tree);
2911 free(tree_id);
2912 return err;
2915 static const struct got_error *
2916 cmd_tree(int argc, char *argv[])
2918 const struct got_error *error;
2919 struct got_repository *repo = NULL;
2920 struct got_worktree *worktree = NULL;
2921 const char *path;
2922 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2923 struct got_object_id *commit_id = NULL;
2924 char *commit_id_str = NULL;
2925 int show_ids = 0, recurse = 0;
2926 int ch;
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:iR")) != -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 case 'i':
2947 show_ids = 1;
2948 break;
2949 case 'R':
2950 recurse = 1;
2951 break;
2952 default:
2953 usage_tree();
2954 /* NOTREACHED */
2958 argc -= optind;
2959 argv += optind;
2961 if (argc == 1)
2962 path = argv[0];
2963 else if (argc > 1)
2964 usage_tree();
2965 else
2966 path = NULL;
2968 cwd = getcwd(NULL, 0);
2969 if (cwd == NULL) {
2970 error = got_error_from_errno("getcwd");
2971 goto done;
2973 if (repo_path == NULL) {
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2976 goto done;
2977 else
2978 error = NULL;
2979 if (worktree) {
2980 repo_path =
2981 strdup(got_worktree_get_repo_path(worktree));
2982 if (repo_path == NULL)
2983 error = got_error_from_errno("strdup");
2984 if (error)
2985 goto done;
2986 } else {
2987 repo_path = strdup(cwd);
2988 if (repo_path == NULL) {
2989 error = got_error_from_errno("strdup");
2990 goto done;
2995 error = got_repo_open(&repo, repo_path, NULL);
2996 if (error != NULL)
2997 goto done;
2999 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3000 if (error)
3001 goto done;
3003 if (path == NULL) {
3004 if (worktree) {
3005 char *p, *worktree_subdir = cwd +
3006 strlen(got_worktree_get_root_path(worktree));
3007 if (asprintf(&p, "%s/%s",
3008 got_worktree_get_path_prefix(worktree),
3009 worktree_subdir) == -1) {
3010 error = got_error_from_errno("asprintf");
3011 goto done;
3013 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3014 free(p);
3015 if (error)
3016 goto done;
3017 } else
3018 path = "/";
3020 if (in_repo_path == NULL) {
3021 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3022 if (error != NULL)
3023 goto done;
3026 if (commit_id_str == NULL) {
3027 struct got_reference *head_ref;
3028 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3029 if (error != NULL)
3030 goto done;
3031 error = got_ref_resolve(&commit_id, repo, head_ref);
3032 got_ref_close(head_ref);
3033 if (error != NULL)
3034 goto done;
3035 } else {
3036 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3037 if (error)
3038 goto done;
3041 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3042 in_repo_path, repo);
3043 done:
3044 free(in_repo_path);
3045 free(repo_path);
3046 free(cwd);
3047 free(commit_id);
3048 if (worktree)
3049 got_worktree_close(worktree);
3050 if (repo) {
3051 const struct got_error *repo_error;
3052 repo_error = got_repo_close(repo);
3053 if (error == NULL)
3054 error = repo_error;
3056 return error;
3059 __dead static void
3060 usage_status(void)
3062 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3063 exit(1);
3066 static const struct got_error *
3067 print_status(void *arg, unsigned char status, unsigned char staged_status,
3068 const char *path, struct got_object_id *blob_id,
3069 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3070 int dirfd, const char *de_name)
3072 if (status == staged_status && (status == GOT_STATUS_DELETE))
3073 status = GOT_STATUS_NO_CHANGE;
3074 printf("%c%c %s\n", status, staged_status, path);
3075 return NULL;
3078 static const struct got_error *
3079 cmd_status(int argc, char *argv[])
3081 const struct got_error *error = NULL;
3082 struct got_repository *repo = NULL;
3083 struct got_worktree *worktree = NULL;
3084 char *cwd = NULL;
3085 struct got_pathlist_head paths;
3086 struct got_pathlist_entry *pe;
3087 int ch;
3089 TAILQ_INIT(&paths);
3091 while ((ch = getopt(argc, argv, "")) != -1) {
3092 switch (ch) {
3093 default:
3094 usage_status();
3095 /* NOTREACHED */
3099 argc -= optind;
3100 argv += optind;
3102 #ifndef PROFILE
3103 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3104 NULL) == -1)
3105 err(1, "pledge");
3106 #endif
3107 cwd = getcwd(NULL, 0);
3108 if (cwd == NULL) {
3109 error = got_error_from_errno("getcwd");
3110 goto done;
3113 error = got_worktree_open(&worktree, cwd);
3114 if (error != NULL)
3115 goto done;
3117 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3118 NULL);
3119 if (error != NULL)
3120 goto done;
3122 error = apply_unveil(got_repo_get_path(repo), 1,
3123 got_worktree_get_root_path(worktree));
3124 if (error)
3125 goto done;
3127 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3128 if (error)
3129 goto done;
3131 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3132 check_cancelled, NULL);
3133 done:
3134 TAILQ_FOREACH(pe, &paths, entry)
3135 free((char *)pe->path);
3136 got_pathlist_free(&paths);
3137 free(cwd);
3138 return error;
3141 __dead static void
3142 usage_ref(void)
3144 fprintf(stderr,
3145 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3146 getprogname());
3147 exit(1);
3150 static const struct got_error *
3151 list_refs(struct got_repository *repo)
3153 static const struct got_error *err = NULL;
3154 struct got_reflist_head refs;
3155 struct got_reflist_entry *re;
3157 SIMPLEQ_INIT(&refs);
3158 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3159 if (err)
3160 return err;
3162 SIMPLEQ_FOREACH(re, &refs, entry) {
3163 char *refstr;
3164 refstr = got_ref_to_str(re->ref);
3165 if (refstr == NULL)
3166 return got_error_from_errno("got_ref_to_str");
3167 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3168 free(refstr);
3171 got_ref_list_free(&refs);
3172 return NULL;
3175 static const struct got_error *
3176 delete_ref(struct got_repository *repo, const char *refname)
3178 const struct got_error *err = NULL;
3179 struct got_reference *ref;
3181 err = got_ref_open(&ref, repo, refname, 0);
3182 if (err)
3183 return err;
3185 err = got_ref_delete(ref, repo);
3186 got_ref_close(ref);
3187 return err;
3190 static const struct got_error *
3191 add_ref(struct got_repository *repo, const char *refname, const char *target)
3193 const struct got_error *err = NULL;
3194 struct got_object_id *id;
3195 struct got_reference *ref = NULL;
3198 * Don't let the user create a reference name with a leading '-'.
3199 * While technically a valid reference name, this case is usually
3200 * an unintended typo.
3202 if (refname[0] == '-')
3203 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3205 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3206 repo);
3207 if (err) {
3208 struct got_reference *target_ref;
3210 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3211 return err;
3212 err = got_ref_open(&target_ref, repo, target, 0);
3213 if (err)
3214 return err;
3215 err = got_ref_resolve(&id, repo, target_ref);
3216 got_ref_close(target_ref);
3217 if (err)
3218 return err;
3221 err = got_ref_alloc(&ref, refname, id);
3222 if (err)
3223 goto done;
3225 err = got_ref_write(ref, repo);
3226 done:
3227 if (ref)
3228 got_ref_close(ref);
3229 free(id);
3230 return err;
3233 static const struct got_error *
3234 add_symref(struct got_repository *repo, const char *refname, const char *target)
3236 const struct got_error *err = NULL;
3237 struct got_reference *ref = NULL;
3238 struct got_reference *target_ref = NULL;
3241 * Don't let the user create a reference name with a leading '-'.
3242 * While technically a valid reference name, this case is usually
3243 * an unintended typo.
3245 if (refname[0] == '-')
3246 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3248 err = got_ref_open(&target_ref, repo, target, 0);
3249 if (err)
3250 return err;
3252 err = got_ref_alloc_symref(&ref, refname, target_ref);
3253 if (err)
3254 goto done;
3256 err = got_ref_write(ref, repo);
3257 done:
3258 if (target_ref)
3259 got_ref_close(target_ref);
3260 if (ref)
3261 got_ref_close(ref);
3262 return err;
3265 static const struct got_error *
3266 cmd_ref(int argc, char *argv[])
3268 const struct got_error *error = NULL;
3269 struct got_repository *repo = NULL;
3270 struct got_worktree *worktree = NULL;
3271 char *cwd = NULL, *repo_path = NULL;
3272 int ch, do_list = 0, create_symref = 0;
3273 const char *delref = NULL;
3275 /* TODO: Add -s option for adding symbolic references. */
3276 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3277 switch (ch) {
3278 case 'd':
3279 delref = optarg;
3280 break;
3281 case 'r':
3282 repo_path = realpath(optarg, NULL);
3283 if (repo_path == NULL)
3284 return got_error_from_errno2("realpath",
3285 optarg);
3286 got_path_strip_trailing_slashes(repo_path);
3287 break;
3288 case 'l':
3289 do_list = 1;
3290 break;
3291 case 's':
3292 create_symref = 1;
3293 break;
3294 default:
3295 usage_ref();
3296 /* NOTREACHED */
3300 if (do_list && delref)
3301 errx(1, "-l and -d options are mutually exclusive\n");
3303 argc -= optind;
3304 argv += optind;
3306 if (do_list || delref) {
3307 if (create_symref)
3308 errx(1, "-s option cannot be used together with the "
3309 "-l or -d options");
3310 if (argc > 0)
3311 usage_ref();
3312 } else if (argc != 2)
3313 usage_ref();
3315 #ifndef PROFILE
3316 if (do_list) {
3317 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3318 NULL) == -1)
3319 err(1, "pledge");
3320 } else {
3321 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3322 "sendfd unveil", NULL) == -1)
3323 err(1, "pledge");
3325 #endif
3326 cwd = getcwd(NULL, 0);
3327 if (cwd == NULL) {
3328 error = got_error_from_errno("getcwd");
3329 goto done;
3332 if (repo_path == NULL) {
3333 error = got_worktree_open(&worktree, cwd);
3334 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3335 goto done;
3336 else
3337 error = NULL;
3338 if (worktree) {
3339 repo_path =
3340 strdup(got_worktree_get_repo_path(worktree));
3341 if (repo_path == NULL)
3342 error = got_error_from_errno("strdup");
3343 if (error)
3344 goto done;
3345 } else {
3346 repo_path = strdup(cwd);
3347 if (repo_path == NULL) {
3348 error = got_error_from_errno("strdup");
3349 goto done;
3354 error = got_repo_open(&repo, repo_path, NULL);
3355 if (error != NULL)
3356 goto done;
3358 error = apply_unveil(got_repo_get_path(repo), do_list,
3359 worktree ? got_worktree_get_root_path(worktree) : NULL);
3360 if (error)
3361 goto done;
3363 if (do_list)
3364 error = list_refs(repo);
3365 else if (delref)
3366 error = delete_ref(repo, delref);
3367 else if (create_symref)
3368 error = add_symref(repo, argv[0], argv[1]);
3369 else
3370 error = add_ref(repo, argv[0], argv[1]);
3371 done:
3372 if (repo)
3373 got_repo_close(repo);
3374 if (worktree)
3375 got_worktree_close(worktree);
3376 free(cwd);
3377 free(repo_path);
3378 return error;
3381 __dead static void
3382 usage_branch(void)
3384 fprintf(stderr,
3385 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3386 "[name]\n", getprogname());
3387 exit(1);
3390 static const struct got_error *
3391 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3392 struct got_reference *ref)
3394 const struct got_error *err = NULL;
3395 const char *refname, *marker = " ";
3396 char *refstr;
3398 refname = got_ref_get_name(ref);
3399 if (worktree && strcmp(refname,
3400 got_worktree_get_head_ref_name(worktree)) == 0) {
3401 struct got_object_id *id = NULL;
3403 err = got_ref_resolve(&id, repo, ref);
3404 if (err)
3405 return err;
3406 if (got_object_id_cmp(id,
3407 got_worktree_get_base_commit_id(worktree)) == 0)
3408 marker = "* ";
3409 else
3410 marker = "~ ";
3411 free(id);
3414 if (strncmp(refname, "refs/heads/", 11) == 0)
3415 refname += 11;
3416 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3417 refname += 18;
3419 refstr = got_ref_to_str(ref);
3420 if (refstr == NULL)
3421 return got_error_from_errno("got_ref_to_str");
3423 printf("%s%s: %s\n", marker, refname, refstr);
3424 free(refstr);
3425 return NULL;
3428 static const struct got_error *
3429 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3431 const char *refname;
3433 if (worktree == NULL)
3434 return got_error(GOT_ERR_NOT_WORKTREE);
3436 refname = got_worktree_get_head_ref_name(worktree);
3438 if (strncmp(refname, "refs/heads/", 11) == 0)
3439 refname += 11;
3440 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3441 refname += 18;
3443 printf("%s\n", refname);
3445 return NULL;
3448 static const struct got_error *
3449 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3451 static const struct got_error *err = NULL;
3452 struct got_reflist_head refs;
3453 struct got_reflist_entry *re;
3454 struct got_reference *temp_ref = NULL;
3455 int rebase_in_progress, histedit_in_progress;
3457 SIMPLEQ_INIT(&refs);
3459 if (worktree) {
3460 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3461 worktree);
3462 if (err)
3463 return err;
3465 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3466 worktree);
3467 if (err)
3468 return err;
3470 if (rebase_in_progress || histedit_in_progress) {
3471 err = got_ref_open(&temp_ref, repo,
3472 got_worktree_get_head_ref_name(worktree), 0);
3473 if (err)
3474 return err;
3475 list_branch(repo, worktree, temp_ref);
3476 got_ref_close(temp_ref);
3480 err = got_ref_list(&refs, repo, "refs/heads",
3481 got_ref_cmp_by_name, NULL);
3482 if (err)
3483 return err;
3485 SIMPLEQ_FOREACH(re, &refs, entry)
3486 list_branch(repo, worktree, re->ref);
3488 got_ref_list_free(&refs);
3489 return NULL;
3492 static const struct got_error *
3493 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3494 const char *branch_name)
3496 const struct got_error *err = NULL;
3497 struct got_reference *ref = NULL;
3498 char *refname;
3500 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3501 return got_error_from_errno("asprintf");
3503 err = got_ref_open(&ref, repo, refname, 0);
3504 if (err)
3505 goto done;
3507 if (worktree &&
3508 strcmp(got_worktree_get_head_ref_name(worktree),
3509 got_ref_get_name(ref)) == 0) {
3510 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3511 "will not delete this work tree's current branch");
3512 goto done;
3515 err = got_ref_delete(ref, repo);
3516 done:
3517 if (ref)
3518 got_ref_close(ref);
3519 free(refname);
3520 return err;
3523 static const struct got_error *
3524 add_branch(struct got_repository *repo, const char *branch_name,
3525 struct got_object_id *base_commit_id)
3527 const struct got_error *err = NULL;
3528 struct got_reference *ref = NULL;
3529 char *base_refname = NULL, *refname = NULL;
3532 * Don't let the user create a branch name with a leading '-'.
3533 * While technically a valid reference name, this case is usually
3534 * an unintended typo.
3536 if (branch_name[0] == '-')
3537 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3539 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3540 err = got_error_from_errno("asprintf");
3541 goto done;
3544 err = got_ref_open(&ref, repo, refname, 0);
3545 if (err == NULL) {
3546 err = got_error(GOT_ERR_BRANCH_EXISTS);
3547 goto done;
3548 } else if (err->code != GOT_ERR_NOT_REF)
3549 goto done;
3551 err = got_ref_alloc(&ref, refname, base_commit_id);
3552 if (err)
3553 goto done;
3555 err = got_ref_write(ref, repo);
3556 done:
3557 if (ref)
3558 got_ref_close(ref);
3559 free(base_refname);
3560 free(refname);
3561 return err;
3564 static const struct got_error *
3565 cmd_branch(int argc, char *argv[])
3567 const struct got_error *error = NULL;
3568 struct got_repository *repo = NULL;
3569 struct got_worktree *worktree = NULL;
3570 char *cwd = NULL, *repo_path = NULL;
3571 int ch, do_list = 0, do_show = 0;
3572 const char *delref = NULL, *commit_id_arg = NULL;
3574 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3575 switch (ch) {
3576 case 'c':
3577 commit_id_arg = optarg;
3578 break;
3579 case 'd':
3580 delref = optarg;
3581 break;
3582 case 'r':
3583 repo_path = realpath(optarg, NULL);
3584 if (repo_path == NULL)
3585 return got_error_from_errno2("realpath",
3586 optarg);
3587 got_path_strip_trailing_slashes(repo_path);
3588 break;
3589 case 'l':
3590 do_list = 1;
3591 break;
3592 default:
3593 usage_branch();
3594 /* NOTREACHED */
3598 if (do_list && delref)
3599 errx(1, "-l and -d options are mutually exclusive\n");
3601 argc -= optind;
3602 argv += optind;
3604 if (!do_list && !delref && argc == 0)
3605 do_show = 1;
3607 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3608 errx(1, "-c option can only be used when creating a branch");
3610 if (do_list || delref) {
3611 if (argc > 0)
3612 usage_branch();
3613 } else if (!do_show && argc != 1)
3614 usage_branch();
3616 #ifndef PROFILE
3617 if (do_list || do_show) {
3618 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3619 NULL) == -1)
3620 err(1, "pledge");
3621 } else {
3622 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3623 "sendfd unveil", NULL) == -1)
3624 err(1, "pledge");
3626 #endif
3627 cwd = getcwd(NULL, 0);
3628 if (cwd == NULL) {
3629 error = got_error_from_errno("getcwd");
3630 goto done;
3633 if (repo_path == NULL) {
3634 error = got_worktree_open(&worktree, cwd);
3635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3636 goto done;
3637 else
3638 error = NULL;
3639 if (worktree) {
3640 repo_path =
3641 strdup(got_worktree_get_repo_path(worktree));
3642 if (repo_path == NULL)
3643 error = got_error_from_errno("strdup");
3644 if (error)
3645 goto done;
3646 } else {
3647 repo_path = strdup(cwd);
3648 if (repo_path == NULL) {
3649 error = got_error_from_errno("strdup");
3650 goto done;
3655 error = got_repo_open(&repo, repo_path, NULL);
3656 if (error != NULL)
3657 goto done;
3659 error = apply_unveil(got_repo_get_path(repo), do_list,
3660 worktree ? got_worktree_get_root_path(worktree) : NULL);
3661 if (error)
3662 goto done;
3664 if (do_show)
3665 error = show_current_branch(repo, worktree);
3666 else if (do_list)
3667 error = list_branches(repo, worktree);
3668 else if (delref)
3669 error = delete_branch(repo, worktree, delref);
3670 else {
3671 struct got_object_id *commit_id;
3672 if (commit_id_arg == NULL)
3673 commit_id_arg = worktree ?
3674 got_worktree_get_head_ref_name(worktree) :
3675 GOT_REF_HEAD;
3676 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3677 if (error)
3678 goto done;
3679 error = add_branch(repo, argv[0], commit_id);
3680 free(commit_id);
3682 done:
3683 if (repo)
3684 got_repo_close(repo);
3685 if (worktree)
3686 got_worktree_close(worktree);
3687 free(cwd);
3688 free(repo_path);
3689 return error;
3693 __dead static void
3694 usage_tag(void)
3696 fprintf(stderr,
3697 "usage: %s tag [-r repository] | -l | "
3698 "[-m message] name [commit]\n", getprogname());
3699 exit(1);
3702 #if 0
3703 static const struct got_error *
3704 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3706 const struct got_error *err = NULL;
3707 struct got_reflist_entry *re, *se, *new;
3708 struct got_object_id *re_id, *se_id;
3709 struct got_tag_object *re_tag, *se_tag;
3710 time_t re_time, se_time;
3712 SIMPLEQ_FOREACH(re, tags, entry) {
3713 se = SIMPLEQ_FIRST(sorted);
3714 if (se == NULL) {
3715 err = got_reflist_entry_dup(&new, re);
3716 if (err)
3717 return err;
3718 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3719 continue;
3720 } else {
3721 err = got_ref_resolve(&re_id, repo, re->ref);
3722 if (err)
3723 break;
3724 err = got_object_open_as_tag(&re_tag, repo, re_id);
3725 free(re_id);
3726 if (err)
3727 break;
3728 re_time = got_object_tag_get_tagger_time(re_tag);
3729 got_object_tag_close(re_tag);
3732 while (se) {
3733 err = got_ref_resolve(&se_id, repo, re->ref);
3734 if (err)
3735 break;
3736 err = got_object_open_as_tag(&se_tag, repo, se_id);
3737 free(se_id);
3738 if (err)
3739 break;
3740 se_time = got_object_tag_get_tagger_time(se_tag);
3741 got_object_tag_close(se_tag);
3743 if (se_time > re_time) {
3744 err = got_reflist_entry_dup(&new, re);
3745 if (err)
3746 return err;
3747 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3748 break;
3750 se = SIMPLEQ_NEXT(se, entry);
3751 continue;
3754 done:
3755 return err;
3757 #endif
3759 static const struct got_error *
3760 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3761 struct got_reference *ref2)
3763 const struct got_error *err = NULL;
3764 struct got_repository *repo = arg;
3765 struct got_object_id *id1, *id2 = NULL;
3766 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3767 time_t time1, time2;
3769 *cmp = 0;
3771 err = got_ref_resolve(&id1, repo, ref1);
3772 if (err)
3773 return err;
3774 err = got_object_open_as_tag(&tag1, repo, id1);
3775 if (err)
3776 goto done;
3778 err = got_ref_resolve(&id2, repo, ref2);
3779 if (err)
3780 goto done;
3781 err = got_object_open_as_tag(&tag2, repo, id2);
3782 if (err)
3783 goto done;
3785 time1 = got_object_tag_get_tagger_time(tag1);
3786 time2 = got_object_tag_get_tagger_time(tag2);
3788 /* Put latest tags first. */
3789 if (time1 < time2)
3790 *cmp = 1;
3791 else if (time1 > time2)
3792 *cmp = -1;
3793 else
3794 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3795 done:
3796 free(id1);
3797 free(id2);
3798 if (tag1)
3799 got_object_tag_close(tag1);
3800 if (tag2)
3801 got_object_tag_close(tag2);
3802 return err;
3805 static const struct got_error *
3806 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3808 static const struct got_error *err = NULL;
3809 struct got_reflist_head refs;
3810 struct got_reflist_entry *re;
3812 SIMPLEQ_INIT(&refs);
3814 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3815 if (err)
3816 return err;
3818 SIMPLEQ_FOREACH(re, &refs, entry) {
3819 const char *refname;
3820 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3821 char datebuf[26];
3822 time_t tagger_time;
3823 struct got_object_id *id;
3824 struct got_tag_object *tag;
3826 refname = got_ref_get_name(re->ref);
3827 if (strncmp(refname, "refs/tags/", 10) != 0)
3828 continue;
3829 refname += 10;
3830 refstr = got_ref_to_str(re->ref);
3831 if (refstr == NULL) {
3832 err = got_error_from_errno("got_ref_to_str");
3833 break;
3835 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3836 free(refstr);
3838 err = got_ref_resolve(&id, repo, re->ref);
3839 if (err)
3840 break;
3841 err = got_object_open_as_tag(&tag, repo, id);
3842 free(id);
3843 if (err)
3844 break;
3845 printf("from: %s\n", got_object_tag_get_tagger(tag));
3846 tagger_time = got_object_tag_get_tagger_time(tag);
3847 datestr = get_datestr(&tagger_time, datebuf);
3848 if (datestr)
3849 printf("date: %s UTC\n", datestr);
3850 err = got_object_id_str(&id_str,
3851 got_object_tag_get_object_id(tag));
3852 if (err)
3853 break;
3854 switch (got_object_tag_get_object_type(tag)) {
3855 case GOT_OBJ_TYPE_BLOB:
3856 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3857 break;
3858 case GOT_OBJ_TYPE_TREE:
3859 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3860 break;
3861 case GOT_OBJ_TYPE_COMMIT:
3862 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3863 break;
3864 case GOT_OBJ_TYPE_TAG:
3865 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3866 break;
3867 default:
3868 break;
3870 free(id_str);
3871 tagmsg0 = strdup(got_object_tag_get_message(tag));
3872 got_object_tag_close(tag);
3873 if (tagmsg0 == NULL) {
3874 err = got_error_from_errno("strdup");
3875 break;
3878 tagmsg = tagmsg0;
3879 do {
3880 line = strsep(&tagmsg, "\n");
3881 if (line)
3882 printf(" %s\n", line);
3883 } while (line);
3884 free(tagmsg0);
3887 got_ref_list_free(&refs);
3888 return NULL;
3891 static const struct got_error *
3892 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3893 const char *tag_name, const char *repo_path)
3895 const struct got_error *err = NULL;
3896 char *template = NULL, *initial_content = NULL;
3897 char *editor = NULL;
3898 int fd = -1;
3900 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3901 err = got_error_from_errno("asprintf");
3902 goto done;
3905 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3906 commit_id_str, tag_name) == -1) {
3907 err = got_error_from_errno("asprintf");
3908 goto done;
3911 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3912 if (err)
3913 goto done;
3915 dprintf(fd, initial_content);
3916 close(fd);
3918 err = get_editor(&editor);
3919 if (err)
3920 goto done;
3921 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3922 done:
3923 free(initial_content);
3924 free(template);
3925 free(editor);
3927 /* Editor is done; we can now apply unveil(2) */
3928 if (err == NULL) {
3929 err = apply_unveil(repo_path, 0, NULL);
3930 if (err) {
3931 free(*tagmsg);
3932 *tagmsg = NULL;
3935 return err;
3938 static const struct got_error *
3939 add_tag(struct got_repository *repo, const char *tag_name,
3940 const char *commit_arg, const char *tagmsg_arg)
3942 const struct got_error *err = NULL;
3943 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3944 char *label = NULL, *commit_id_str = NULL;
3945 struct got_reference *ref = NULL;
3946 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3947 char *tagmsg_path = NULL, *tag_id_str = NULL;
3948 int preserve_tagmsg = 0;
3951 * Don't let the user create a tag name with a leading '-'.
3952 * While technically a valid reference name, this case is usually
3953 * an unintended typo.
3955 if (tag_name[0] == '-')
3956 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3958 err = get_author(&tagger, repo);
3959 if (err)
3960 return err;
3962 err = match_object_id(&commit_id, &label, commit_arg,
3963 GOT_OBJ_TYPE_COMMIT, 1, repo);
3964 if (err)
3965 goto done;
3967 err = got_object_id_str(&commit_id_str, commit_id);
3968 if (err)
3969 goto done;
3971 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3972 refname = strdup(tag_name);
3973 if (refname == NULL) {
3974 err = got_error_from_errno("strdup");
3975 goto done;
3977 tag_name += 10;
3978 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3979 err = got_error_from_errno("asprintf");
3980 goto done;
3983 err = got_ref_open(&ref, repo, refname, 0);
3984 if (err == NULL) {
3985 err = got_error(GOT_ERR_TAG_EXISTS);
3986 goto done;
3987 } else if (err->code != GOT_ERR_NOT_REF)
3988 goto done;
3990 if (tagmsg_arg == NULL) {
3991 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3992 tag_name, got_repo_get_path(repo));
3993 if (err) {
3994 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3995 tagmsg_path != NULL)
3996 preserve_tagmsg = 1;
3997 goto done;
4001 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4002 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4003 if (err) {
4004 if (tagmsg_path)
4005 preserve_tagmsg = 1;
4006 goto done;
4009 err = got_ref_alloc(&ref, refname, tag_id);
4010 if (err) {
4011 if (tagmsg_path)
4012 preserve_tagmsg = 1;
4013 goto done;
4016 err = got_ref_write(ref, repo);
4017 if (err) {
4018 if (tagmsg_path)
4019 preserve_tagmsg = 1;
4020 goto done;
4023 err = got_object_id_str(&tag_id_str, tag_id);
4024 if (err) {
4025 if (tagmsg_path)
4026 preserve_tagmsg = 1;
4027 goto done;
4029 printf("Created tag %s\n", tag_id_str);
4030 done:
4031 if (preserve_tagmsg) {
4032 fprintf(stderr, "%s: tag message preserved in %s\n",
4033 getprogname(), tagmsg_path);
4034 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4035 err = got_error_from_errno2("unlink", tagmsg_path);
4036 free(tag_id_str);
4037 if (ref)
4038 got_ref_close(ref);
4039 free(commit_id);
4040 free(commit_id_str);
4041 free(refname);
4042 free(tagmsg);
4043 free(tagmsg_path);
4044 free(tagger);
4045 return err;
4048 static const struct got_error *
4049 cmd_tag(int argc, char *argv[])
4051 const struct got_error *error = NULL;
4052 struct got_repository *repo = NULL;
4053 struct got_worktree *worktree = NULL;
4054 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4055 char *gitconfig_path = NULL;
4056 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4057 int ch, do_list = 0;
4059 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4060 switch (ch) {
4061 case 'm':
4062 tagmsg = optarg;
4063 break;
4064 case 'r':
4065 repo_path = realpath(optarg, NULL);
4066 if (repo_path == NULL)
4067 return got_error_from_errno2("realpath",
4068 optarg);
4069 got_path_strip_trailing_slashes(repo_path);
4070 break;
4071 case 'l':
4072 do_list = 1;
4073 break;
4074 default:
4075 usage_tag();
4076 /* NOTREACHED */
4080 argc -= optind;
4081 argv += optind;
4083 if (do_list) {
4084 if (tagmsg)
4085 errx(1, "-l and -m options are mutually exclusive\n");
4086 if (argc > 0)
4087 usage_tag();
4088 } else if (argc < 1 || argc > 2)
4089 usage_tag();
4090 else if (argc > 1)
4091 commit_id_arg = argv[1];
4092 tag_name = argv[0];
4094 #ifndef PROFILE
4095 if (do_list) {
4096 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4097 NULL) == -1)
4098 err(1, "pledge");
4099 } else {
4100 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4101 "sendfd unveil", NULL) == -1)
4102 err(1, "pledge");
4104 #endif
4105 cwd = getcwd(NULL, 0);
4106 if (cwd == NULL) {
4107 error = got_error_from_errno("getcwd");
4108 goto done;
4111 if (repo_path == NULL) {
4112 error = got_worktree_open(&worktree, cwd);
4113 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4114 goto done;
4115 else
4116 error = NULL;
4117 if (worktree) {
4118 repo_path =
4119 strdup(got_worktree_get_repo_path(worktree));
4120 if (repo_path == NULL)
4121 error = got_error_from_errno("strdup");
4122 if (error)
4123 goto done;
4124 } else {
4125 repo_path = strdup(cwd);
4126 if (repo_path == NULL) {
4127 error = got_error_from_errno("strdup");
4128 goto done;
4133 if (do_list) {
4134 error = got_repo_open(&repo, repo_path, NULL);
4135 if (error != NULL)
4136 goto done;
4137 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4138 if (error)
4139 goto done;
4140 error = list_tags(repo, worktree);
4141 } else {
4142 error = get_gitconfig_path(&gitconfig_path);
4143 if (error)
4144 goto done;
4145 error = got_repo_open(&repo, repo_path, gitconfig_path);
4146 if (error != NULL)
4147 goto done;
4149 if (tagmsg) {
4150 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4151 if (error)
4152 goto done;
4155 if (commit_id_arg == NULL) {
4156 struct got_reference *head_ref;
4157 struct got_object_id *commit_id;
4158 error = got_ref_open(&head_ref, repo,
4159 worktree ? got_worktree_get_head_ref_name(worktree)
4160 : GOT_REF_HEAD, 0);
4161 if (error)
4162 goto done;
4163 error = got_ref_resolve(&commit_id, repo, head_ref);
4164 got_ref_close(head_ref);
4165 if (error)
4166 goto done;
4167 error = got_object_id_str(&commit_id_str, commit_id);
4168 free(commit_id);
4169 if (error)
4170 goto done;
4173 error = add_tag(repo, tag_name,
4174 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4176 done:
4177 if (repo)
4178 got_repo_close(repo);
4179 if (worktree)
4180 got_worktree_close(worktree);
4181 free(cwd);
4182 free(repo_path);
4183 free(gitconfig_path);
4184 free(commit_id_str);
4185 return error;
4188 __dead static void
4189 usage_add(void)
4191 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4192 getprogname());
4193 exit(1);
4196 static const struct got_error *
4197 add_progress(void *arg, unsigned char status, const char *path)
4199 while (path[0] == '/')
4200 path++;
4201 printf("%c %s\n", status, path);
4202 return NULL;
4205 static const struct got_error *
4206 cmd_add(int argc, char *argv[])
4208 const struct got_error *error = NULL;
4209 struct got_repository *repo = NULL;
4210 struct got_worktree *worktree = NULL;
4211 char *cwd = NULL;
4212 struct got_pathlist_head paths;
4213 struct got_pathlist_entry *pe;
4214 int ch, can_recurse = 0, no_ignores = 0;
4216 TAILQ_INIT(&paths);
4218 while ((ch = getopt(argc, argv, "IR")) != -1) {
4219 switch (ch) {
4220 case 'I':
4221 no_ignores = 1;
4222 break;
4223 case 'R':
4224 can_recurse = 1;
4225 break;
4226 default:
4227 usage_add();
4228 /* NOTREACHED */
4232 argc -= optind;
4233 argv += optind;
4235 #ifndef PROFILE
4236 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4237 NULL) == -1)
4238 err(1, "pledge");
4239 #endif
4240 if (argc < 1)
4241 usage_add();
4243 cwd = getcwd(NULL, 0);
4244 if (cwd == NULL) {
4245 error = got_error_from_errno("getcwd");
4246 goto done;
4249 error = got_worktree_open(&worktree, cwd);
4250 if (error)
4251 goto done;
4253 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4254 NULL);
4255 if (error != NULL)
4256 goto done;
4258 error = apply_unveil(got_repo_get_path(repo), 1,
4259 got_worktree_get_root_path(worktree));
4260 if (error)
4261 goto done;
4263 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4264 if (error)
4265 goto done;
4267 if (!can_recurse && no_ignores) {
4268 error = got_error_msg(GOT_ERR_BAD_PATH,
4269 "disregarding ignores requires -R option");
4270 goto done;
4274 if (!can_recurse) {
4275 char *ondisk_path;
4276 struct stat sb;
4277 TAILQ_FOREACH(pe, &paths, entry) {
4278 if (asprintf(&ondisk_path, "%s/%s",
4279 got_worktree_get_root_path(worktree),
4280 pe->path) == -1) {
4281 error = got_error_from_errno("asprintf");
4282 goto done;
4284 if (lstat(ondisk_path, &sb) == -1) {
4285 if (errno == ENOENT) {
4286 free(ondisk_path);
4287 continue;
4289 error = got_error_from_errno2("lstat",
4290 ondisk_path);
4291 free(ondisk_path);
4292 goto done;
4294 free(ondisk_path);
4295 if (S_ISDIR(sb.st_mode)) {
4296 error = got_error_msg(GOT_ERR_BAD_PATH,
4297 "adding directories requires -R option");
4298 goto done;
4303 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4304 NULL, repo, no_ignores);
4305 done:
4306 if (repo)
4307 got_repo_close(repo);
4308 if (worktree)
4309 got_worktree_close(worktree);
4310 TAILQ_FOREACH(pe, &paths, entry)
4311 free((char *)pe->path);
4312 got_pathlist_free(&paths);
4313 free(cwd);
4314 return error;
4317 __dead static void
4318 usage_remove(void)
4320 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4321 getprogname());
4322 exit(1);
4325 static const struct got_error *
4326 print_remove_status(void *arg, unsigned char status,
4327 unsigned char staged_status, const char *path)
4329 while (path[0] == '/')
4330 path++;
4331 if (status == GOT_STATUS_NONEXISTENT)
4332 return NULL;
4333 if (status == staged_status && (status == GOT_STATUS_DELETE))
4334 status = GOT_STATUS_NO_CHANGE;
4335 printf("%c%c %s\n", status, staged_status, path);
4336 return NULL;
4339 static const struct got_error *
4340 cmd_remove(int argc, char *argv[])
4342 const struct got_error *error = NULL;
4343 struct got_worktree *worktree = NULL;
4344 struct got_repository *repo = NULL;
4345 char *cwd = NULL;
4346 struct got_pathlist_head paths;
4347 struct got_pathlist_entry *pe;
4348 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4350 TAILQ_INIT(&paths);
4352 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4353 switch (ch) {
4354 case 'f':
4355 delete_local_mods = 1;
4356 break;
4357 case 'k':
4358 keep_on_disk = 1;
4359 break;
4360 case 'R':
4361 can_recurse = 1;
4362 break;
4363 default:
4364 usage_remove();
4365 /* NOTREACHED */
4369 argc -= optind;
4370 argv += optind;
4372 #ifndef PROFILE
4373 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4374 NULL) == -1)
4375 err(1, "pledge");
4376 #endif
4377 if (argc < 1)
4378 usage_remove();
4380 cwd = getcwd(NULL, 0);
4381 if (cwd == NULL) {
4382 error = got_error_from_errno("getcwd");
4383 goto done;
4385 error = got_worktree_open(&worktree, cwd);
4386 if (error)
4387 goto done;
4389 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4390 NULL);
4391 if (error)
4392 goto done;
4394 error = apply_unveil(got_repo_get_path(repo), 1,
4395 got_worktree_get_root_path(worktree));
4396 if (error)
4397 goto done;
4399 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4400 if (error)
4401 goto done;
4403 if (!can_recurse) {
4404 char *ondisk_path;
4405 struct stat sb;
4406 TAILQ_FOREACH(pe, &paths, entry) {
4407 if (asprintf(&ondisk_path, "%s/%s",
4408 got_worktree_get_root_path(worktree),
4409 pe->path) == -1) {
4410 error = got_error_from_errno("asprintf");
4411 goto done;
4413 if (lstat(ondisk_path, &sb) == -1) {
4414 if (errno == ENOENT) {
4415 free(ondisk_path);
4416 continue;
4418 error = got_error_from_errno2("lstat",
4419 ondisk_path);
4420 free(ondisk_path);
4421 goto done;
4423 free(ondisk_path);
4424 if (S_ISDIR(sb.st_mode)) {
4425 error = got_error_msg(GOT_ERR_BAD_PATH,
4426 "removing directories requires -R option");
4427 goto done;
4432 error = got_worktree_schedule_delete(worktree, &paths,
4433 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4434 if (error)
4435 goto done;
4436 done:
4437 if (repo)
4438 got_repo_close(repo);
4439 if (worktree)
4440 got_worktree_close(worktree);
4441 TAILQ_FOREACH(pe, &paths, entry)
4442 free((char *)pe->path);
4443 got_pathlist_free(&paths);
4444 free(cwd);
4445 return error;
4448 __dead static void
4449 usage_revert(void)
4451 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4452 "path ...\n", getprogname());
4453 exit(1);
4456 static const struct got_error *
4457 revert_progress(void *arg, unsigned char status, const char *path)
4459 while (path[0] == '/')
4460 path++;
4461 printf("%c %s\n", status, path);
4462 return NULL;
4465 struct choose_patch_arg {
4466 FILE *patch_script_file;
4467 const char *action;
4470 static const struct got_error *
4471 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4472 int nchanges, const char *action)
4474 char *line = NULL;
4475 size_t linesize = 0;
4476 ssize_t linelen;
4478 switch (status) {
4479 case GOT_STATUS_ADD:
4480 printf("A %s\n%s this addition? [y/n] ", path, action);
4481 break;
4482 case GOT_STATUS_DELETE:
4483 printf("D %s\n%s this deletion? [y/n] ", path, action);
4484 break;
4485 case GOT_STATUS_MODIFY:
4486 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4487 return got_error_from_errno("fseek");
4488 printf(GOT_COMMIT_SEP_STR);
4489 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4490 printf("%s", line);
4491 if (ferror(patch_file))
4492 return got_error_from_errno("getline");
4493 printf(GOT_COMMIT_SEP_STR);
4494 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4495 path, n, nchanges, action);
4496 break;
4497 default:
4498 return got_error_path(path, GOT_ERR_FILE_STATUS);
4501 return NULL;
4504 static const struct got_error *
4505 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4506 FILE *patch_file, int n, int nchanges)
4508 const struct got_error *err = NULL;
4509 char *line = NULL;
4510 size_t linesize = 0;
4511 ssize_t linelen;
4512 int resp = ' ';
4513 struct choose_patch_arg *a = arg;
4515 *choice = GOT_PATCH_CHOICE_NONE;
4517 if (a->patch_script_file) {
4518 char *nl;
4519 err = show_change(status, path, patch_file, n, nchanges,
4520 a->action);
4521 if (err)
4522 return err;
4523 linelen = getline(&line, &linesize, a->patch_script_file);
4524 if (linelen == -1) {
4525 if (ferror(a->patch_script_file))
4526 return got_error_from_errno("getline");
4527 return NULL;
4529 nl = strchr(line, '\n');
4530 if (nl)
4531 *nl = '\0';
4532 if (strcmp(line, "y") == 0) {
4533 *choice = GOT_PATCH_CHOICE_YES;
4534 printf("y\n");
4535 } else if (strcmp(line, "n") == 0) {
4536 *choice = GOT_PATCH_CHOICE_NO;
4537 printf("n\n");
4538 } else if (strcmp(line, "q") == 0 &&
4539 status == GOT_STATUS_MODIFY) {
4540 *choice = GOT_PATCH_CHOICE_QUIT;
4541 printf("q\n");
4542 } else
4543 printf("invalid response '%s'\n", line);
4544 free(line);
4545 return NULL;
4548 while (resp != 'y' && resp != 'n' && resp != 'q') {
4549 err = show_change(status, path, patch_file, n, nchanges,
4550 a->action);
4551 if (err)
4552 return err;
4553 resp = getchar();
4554 if (resp == '\n')
4555 resp = getchar();
4556 if (status == GOT_STATUS_MODIFY) {
4557 if (resp != 'y' && resp != 'n' && resp != 'q') {
4558 printf("invalid response '%c'\n", resp);
4559 resp = ' ';
4561 } else if (resp != 'y' && resp != 'n') {
4562 printf("invalid response '%c'\n", resp);
4563 resp = ' ';
4567 if (resp == 'y')
4568 *choice = GOT_PATCH_CHOICE_YES;
4569 else if (resp == 'n')
4570 *choice = GOT_PATCH_CHOICE_NO;
4571 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4572 *choice = GOT_PATCH_CHOICE_QUIT;
4574 return NULL;
4578 static const struct got_error *
4579 cmd_revert(int argc, char *argv[])
4581 const struct got_error *error = NULL;
4582 struct got_worktree *worktree = NULL;
4583 struct got_repository *repo = NULL;
4584 char *cwd = NULL, *path = NULL;
4585 struct got_pathlist_head paths;
4586 struct got_pathlist_entry *pe;
4587 int ch, can_recurse = 0, pflag = 0;
4588 FILE *patch_script_file = NULL;
4589 const char *patch_script_path = NULL;
4590 struct choose_patch_arg cpa;
4592 TAILQ_INIT(&paths);
4594 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4595 switch (ch) {
4596 case 'p':
4597 pflag = 1;
4598 break;
4599 case 'F':
4600 patch_script_path = optarg;
4601 break;
4602 case 'R':
4603 can_recurse = 1;
4604 break;
4605 default:
4606 usage_revert();
4607 /* NOTREACHED */
4611 argc -= optind;
4612 argv += optind;
4614 #ifndef PROFILE
4615 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4616 "unveil", NULL) == -1)
4617 err(1, "pledge");
4618 #endif
4619 if (argc < 1)
4620 usage_revert();
4621 if (patch_script_path && !pflag)
4622 errx(1, "-F option can only be used together with -p option");
4624 cwd = getcwd(NULL, 0);
4625 if (cwd == NULL) {
4626 error = got_error_from_errno("getcwd");
4627 goto done;
4629 error = got_worktree_open(&worktree, cwd);
4630 if (error)
4631 goto done;
4633 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4634 NULL);
4635 if (error != NULL)
4636 goto done;
4638 if (patch_script_path) {
4639 patch_script_file = fopen(patch_script_path, "r");
4640 if (patch_script_file == NULL) {
4641 error = got_error_from_errno2("fopen",
4642 patch_script_path);
4643 goto done;
4646 error = apply_unveil(got_repo_get_path(repo), 1,
4647 got_worktree_get_root_path(worktree));
4648 if (error)
4649 goto done;
4651 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4652 if (error)
4653 goto done;
4655 if (!can_recurse) {
4656 char *ondisk_path;
4657 struct stat sb;
4658 TAILQ_FOREACH(pe, &paths, entry) {
4659 if (asprintf(&ondisk_path, "%s/%s",
4660 got_worktree_get_root_path(worktree),
4661 pe->path) == -1) {
4662 error = got_error_from_errno("asprintf");
4663 goto done;
4665 if (lstat(ondisk_path, &sb) == -1) {
4666 if (errno == ENOENT) {
4667 free(ondisk_path);
4668 continue;
4670 error = got_error_from_errno2("lstat",
4671 ondisk_path);
4672 free(ondisk_path);
4673 goto done;
4675 free(ondisk_path);
4676 if (S_ISDIR(sb.st_mode)) {
4677 error = got_error_msg(GOT_ERR_BAD_PATH,
4678 "reverting directories requires -R option");
4679 goto done;
4684 cpa.patch_script_file = patch_script_file;
4685 cpa.action = "revert";
4686 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4687 pflag ? choose_patch : NULL, &cpa, repo);
4688 if (error)
4689 goto done;
4690 done:
4691 if (patch_script_file && fclose(patch_script_file) == EOF &&
4692 error == NULL)
4693 error = got_error_from_errno2("fclose", patch_script_path);
4694 if (repo)
4695 got_repo_close(repo);
4696 if (worktree)
4697 got_worktree_close(worktree);
4698 free(path);
4699 free(cwd);
4700 return error;
4703 __dead static void
4704 usage_commit(void)
4706 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4707 getprogname());
4708 exit(1);
4711 struct collect_commit_logmsg_arg {
4712 const char *cmdline_log;
4713 const char *editor;
4714 const char *worktree_path;
4715 const char *branch_name;
4716 const char *repo_path;
4717 char *logmsg_path;
4721 static const struct got_error *
4722 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4723 void *arg)
4725 char *initial_content = NULL;
4726 struct got_pathlist_entry *pe;
4727 const struct got_error *err = NULL;
4728 char *template = NULL;
4729 struct collect_commit_logmsg_arg *a = arg;
4730 int fd;
4731 size_t len;
4733 /* if a message was specified on the command line, just use it */
4734 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4735 len = strlen(a->cmdline_log) + 1;
4736 *logmsg = malloc(len + 1);
4737 if (*logmsg == NULL)
4738 return got_error_from_errno("malloc");
4739 strlcpy(*logmsg, a->cmdline_log, len);
4740 return NULL;
4743 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4744 return got_error_from_errno("asprintf");
4746 if (asprintf(&initial_content,
4747 "\n# changes to be committed on branch %s:\n",
4748 a->branch_name) == -1)
4749 return got_error_from_errno("asprintf");
4751 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4752 if (err)
4753 goto done;
4755 dprintf(fd, initial_content);
4757 TAILQ_FOREACH(pe, commitable_paths, entry) {
4758 struct got_commitable *ct = pe->data;
4759 dprintf(fd, "# %c %s\n",
4760 got_commitable_get_status(ct),
4761 got_commitable_get_path(ct));
4763 close(fd);
4765 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4766 done:
4767 free(initial_content);
4768 free(template);
4770 /* Editor is done; we can now apply unveil(2) */
4771 if (err == NULL) {
4772 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4773 if (err) {
4774 free(*logmsg);
4775 *logmsg = NULL;
4778 return err;
4781 static const struct got_error *
4782 cmd_commit(int argc, char *argv[])
4784 const struct got_error *error = NULL;
4785 struct got_worktree *worktree = NULL;
4786 struct got_repository *repo = NULL;
4787 char *cwd = NULL, *id_str = NULL;
4788 struct got_object_id *id = NULL;
4789 const char *logmsg = NULL;
4790 struct collect_commit_logmsg_arg cl_arg;
4791 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4792 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4793 struct got_pathlist_head paths;
4795 TAILQ_INIT(&paths);
4796 cl_arg.logmsg_path = NULL;
4798 while ((ch = getopt(argc, argv, "m:")) != -1) {
4799 switch (ch) {
4800 case 'm':
4801 logmsg = optarg;
4802 break;
4803 default:
4804 usage_commit();
4805 /* NOTREACHED */
4809 argc -= optind;
4810 argv += optind;
4812 #ifndef PROFILE
4813 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4814 "unveil", NULL) == -1)
4815 err(1, "pledge");
4816 #endif
4817 cwd = getcwd(NULL, 0);
4818 if (cwd == NULL) {
4819 error = got_error_from_errno("getcwd");
4820 goto done;
4822 error = got_worktree_open(&worktree, cwd);
4823 if (error)
4824 goto done;
4826 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4827 if (error)
4828 goto done;
4829 if (rebase_in_progress) {
4830 error = got_error(GOT_ERR_REBASING);
4831 goto done;
4834 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4835 worktree);
4836 if (error)
4837 goto done;
4839 error = get_gitconfig_path(&gitconfig_path);
4840 if (error)
4841 goto done;
4842 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4843 gitconfig_path);
4844 if (error != NULL)
4845 goto done;
4847 error = get_author(&author, repo);
4848 if (error)
4849 return error;
4852 * unveil(2) traverses exec(2); if an editor is used we have
4853 * to apply unveil after the log message has been written.
4855 if (logmsg == NULL || strlen(logmsg) == 0)
4856 error = get_editor(&editor);
4857 else
4858 error = apply_unveil(got_repo_get_path(repo), 0,
4859 got_worktree_get_root_path(worktree));
4860 if (error)
4861 goto done;
4863 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4864 if (error)
4865 goto done;
4867 cl_arg.editor = editor;
4868 cl_arg.cmdline_log = logmsg;
4869 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4870 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4871 if (!histedit_in_progress) {
4872 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4873 error = got_error(GOT_ERR_COMMIT_BRANCH);
4874 goto done;
4876 cl_arg.branch_name += 11;
4878 cl_arg.repo_path = got_repo_get_path(repo);
4879 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4880 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4881 if (error) {
4882 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4883 cl_arg.logmsg_path != NULL)
4884 preserve_logmsg = 1;
4885 goto done;
4888 error = got_object_id_str(&id_str, id);
4889 if (error)
4890 goto done;
4891 printf("Created commit %s\n", id_str);
4892 done:
4893 if (preserve_logmsg) {
4894 fprintf(stderr, "%s: log message preserved in %s\n",
4895 getprogname(), cl_arg.logmsg_path);
4896 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4897 error == NULL)
4898 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4899 free(cl_arg.logmsg_path);
4900 if (repo)
4901 got_repo_close(repo);
4902 if (worktree)
4903 got_worktree_close(worktree);
4904 free(cwd);
4905 free(id_str);
4906 free(gitconfig_path);
4907 free(editor);
4908 free(author);
4909 return error;
4912 __dead static void
4913 usage_cherrypick(void)
4915 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4916 exit(1);
4919 static const struct got_error *
4920 cmd_cherrypick(int argc, char *argv[])
4922 const struct got_error *error = NULL;
4923 struct got_worktree *worktree = NULL;
4924 struct got_repository *repo = NULL;
4925 char *cwd = NULL, *commit_id_str = NULL;
4926 struct got_object_id *commit_id = NULL;
4927 struct got_commit_object *commit = NULL;
4928 struct got_object_qid *pid;
4929 struct got_reference *head_ref = NULL;
4930 int ch, did_something = 0;
4932 while ((ch = getopt(argc, argv, "")) != -1) {
4933 switch (ch) {
4934 default:
4935 usage_cherrypick();
4936 /* NOTREACHED */
4940 argc -= optind;
4941 argv += optind;
4943 #ifndef PROFILE
4944 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4945 "unveil", NULL) == -1)
4946 err(1, "pledge");
4947 #endif
4948 if (argc != 1)
4949 usage_cherrypick();
4951 cwd = getcwd(NULL, 0);
4952 if (cwd == NULL) {
4953 error = got_error_from_errno("getcwd");
4954 goto done;
4956 error = got_worktree_open(&worktree, cwd);
4957 if (error)
4958 goto done;
4960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4961 NULL);
4962 if (error != NULL)
4963 goto done;
4965 error = apply_unveil(got_repo_get_path(repo), 0,
4966 got_worktree_get_root_path(worktree));
4967 if (error)
4968 goto done;
4970 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4971 GOT_OBJ_TYPE_COMMIT, repo);
4972 if (error != NULL) {
4973 struct got_reference *ref;
4974 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4975 goto done;
4976 error = got_ref_open(&ref, repo, argv[0], 0);
4977 if (error != NULL)
4978 goto done;
4979 error = got_ref_resolve(&commit_id, repo, ref);
4980 got_ref_close(ref);
4981 if (error != NULL)
4982 goto done;
4984 error = got_object_id_str(&commit_id_str, commit_id);
4985 if (error)
4986 goto done;
4988 error = got_ref_open(&head_ref, repo,
4989 got_worktree_get_head_ref_name(worktree), 0);
4990 if (error != NULL)
4991 goto done;
4993 error = check_same_branch(commit_id, head_ref, NULL, repo);
4994 if (error) {
4995 if (error->code != GOT_ERR_ANCESTRY)
4996 goto done;
4997 error = NULL;
4998 } else {
4999 error = got_error(GOT_ERR_SAME_BRANCH);
5000 goto done;
5003 error = got_object_open_as_commit(&commit, repo, commit_id);
5004 if (error)
5005 goto done;
5006 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5007 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5008 commit_id, repo, update_progress, &did_something, check_cancelled,
5009 NULL);
5010 if (error != NULL)
5011 goto done;
5013 if (did_something)
5014 printf("Merged commit %s\n", commit_id_str);
5015 done:
5016 if (commit)
5017 got_object_commit_close(commit);
5018 free(commit_id_str);
5019 if (head_ref)
5020 got_ref_close(head_ref);
5021 if (worktree)
5022 got_worktree_close(worktree);
5023 if (repo)
5024 got_repo_close(repo);
5025 return error;
5028 __dead static void
5029 usage_backout(void)
5031 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5032 exit(1);
5035 static const struct got_error *
5036 cmd_backout(int argc, char *argv[])
5038 const struct got_error *error = NULL;
5039 struct got_worktree *worktree = NULL;
5040 struct got_repository *repo = NULL;
5041 char *cwd = NULL, *commit_id_str = NULL;
5042 struct got_object_id *commit_id = NULL;
5043 struct got_commit_object *commit = NULL;
5044 struct got_object_qid *pid;
5045 struct got_reference *head_ref = NULL;
5046 int ch, did_something = 0;
5048 while ((ch = getopt(argc, argv, "")) != -1) {
5049 switch (ch) {
5050 default:
5051 usage_backout();
5052 /* NOTREACHED */
5056 argc -= optind;
5057 argv += optind;
5059 #ifndef PROFILE
5060 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5061 "unveil", NULL) == -1)
5062 err(1, "pledge");
5063 #endif
5064 if (argc != 1)
5065 usage_backout();
5067 cwd = getcwd(NULL, 0);
5068 if (cwd == NULL) {
5069 error = got_error_from_errno("getcwd");
5070 goto done;
5072 error = got_worktree_open(&worktree, cwd);
5073 if (error)
5074 goto done;
5076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5077 NULL);
5078 if (error != NULL)
5079 goto done;
5081 error = apply_unveil(got_repo_get_path(repo), 0,
5082 got_worktree_get_root_path(worktree));
5083 if (error)
5084 goto done;
5086 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5087 GOT_OBJ_TYPE_COMMIT, repo);
5088 if (error != NULL) {
5089 struct got_reference *ref;
5090 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5091 goto done;
5092 error = got_ref_open(&ref, repo, argv[0], 0);
5093 if (error != NULL)
5094 goto done;
5095 error = got_ref_resolve(&commit_id, repo, ref);
5096 got_ref_close(ref);
5097 if (error != NULL)
5098 goto done;
5100 error = got_object_id_str(&commit_id_str, commit_id);
5101 if (error)
5102 goto done;
5104 error = got_ref_open(&head_ref, repo,
5105 got_worktree_get_head_ref_name(worktree), 0);
5106 if (error != NULL)
5107 goto done;
5109 error = check_same_branch(commit_id, head_ref, NULL, repo);
5110 if (error)
5111 goto done;
5113 error = got_object_open_as_commit(&commit, repo, commit_id);
5114 if (error)
5115 goto done;
5116 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5117 if (pid == NULL) {
5118 error = got_error(GOT_ERR_ROOT_COMMIT);
5119 goto done;
5122 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5123 update_progress, &did_something, check_cancelled, NULL);
5124 if (error != NULL)
5125 goto done;
5127 if (did_something)
5128 printf("Backed out commit %s\n", commit_id_str);
5129 done:
5130 if (commit)
5131 got_object_commit_close(commit);
5132 free(commit_id_str);
5133 if (head_ref)
5134 got_ref_close(head_ref);
5135 if (worktree)
5136 got_worktree_close(worktree);
5137 if (repo)
5138 got_repo_close(repo);
5139 return error;
5142 __dead static void
5143 usage_rebase(void)
5145 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5146 getprogname());
5147 exit(1);
5150 void
5151 trim_logmsg(char *logmsg, int limit)
5153 char *nl;
5154 size_t len;
5156 len = strlen(logmsg);
5157 if (len > limit)
5158 len = limit;
5159 logmsg[len] = '\0';
5160 nl = strchr(logmsg, '\n');
5161 if (nl)
5162 *nl = '\0';
5165 static const struct got_error *
5166 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5168 const struct got_error *err;
5169 char *logmsg0 = NULL;
5170 const char *s;
5172 err = got_object_commit_get_logmsg(&logmsg0, commit);
5173 if (err)
5174 return err;
5176 s = logmsg0;
5177 while (isspace((unsigned char)s[0]))
5178 s++;
5180 *logmsg = strdup(s);
5181 if (*logmsg == NULL) {
5182 err = got_error_from_errno("strdup");
5183 goto done;
5186 trim_logmsg(*logmsg, limit);
5187 done:
5188 free(logmsg0);
5189 return err;
5192 static const struct got_error *
5193 show_rebase_progress(struct got_commit_object *commit,
5194 struct got_object_id *old_id, struct got_object_id *new_id)
5196 const struct got_error *err;
5197 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5199 err = got_object_id_str(&old_id_str, old_id);
5200 if (err)
5201 goto done;
5203 if (new_id) {
5204 err = got_object_id_str(&new_id_str, new_id);
5205 if (err)
5206 goto done;
5209 old_id_str[12] = '\0';
5210 if (new_id_str)
5211 new_id_str[12] = '\0';
5213 err = get_short_logmsg(&logmsg, 42, commit);
5214 if (err)
5215 goto done;
5217 printf("%s -> %s: %s\n", old_id_str,
5218 new_id_str ? new_id_str : "no-op change", logmsg);
5219 done:
5220 free(old_id_str);
5221 free(new_id_str);
5222 return err;
5225 static const struct got_error *
5226 rebase_progress(void *arg, unsigned char status, const char *path)
5228 unsigned char *rebase_status = arg;
5230 while (path[0] == '/')
5231 path++;
5232 printf("%c %s\n", status, path);
5234 if (*rebase_status == GOT_STATUS_CONFLICT)
5235 return NULL;
5236 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5237 *rebase_status = status;
5238 return NULL;
5241 static const struct got_error *
5242 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5243 struct got_reference *branch, struct got_reference *new_base_branch,
5244 struct got_reference *tmp_branch, struct got_repository *repo)
5246 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5247 return got_worktree_rebase_complete(worktree, fileindex,
5248 new_base_branch, tmp_branch, branch, repo);
5251 static const struct got_error *
5252 rebase_commit(struct got_pathlist_head *merged_paths,
5253 struct got_worktree *worktree, struct got_fileindex *fileindex,
5254 struct got_reference *tmp_branch,
5255 struct got_object_id *commit_id, struct got_repository *repo)
5257 const struct got_error *error;
5258 struct got_commit_object *commit;
5259 struct got_object_id *new_commit_id;
5261 error = got_object_open_as_commit(&commit, repo, commit_id);
5262 if (error)
5263 return error;
5265 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5266 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5267 if (error) {
5268 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5269 goto done;
5270 error = show_rebase_progress(commit, commit_id, NULL);
5271 } else {
5272 error = show_rebase_progress(commit, commit_id, new_commit_id);
5273 free(new_commit_id);
5275 done:
5276 got_object_commit_close(commit);
5277 return error;
5280 struct check_path_prefix_arg {
5281 const char *path_prefix;
5282 size_t len;
5283 int errcode;
5286 static const struct got_error *
5287 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5288 struct got_blob_object *blob2, struct got_object_id *id1,
5289 struct got_object_id *id2, const char *path1, const char *path2,
5290 mode_t mode1, mode_t mode2, struct got_repository *repo)
5292 struct check_path_prefix_arg *a = arg;
5294 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5295 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5296 return got_error(a->errcode);
5298 return NULL;
5301 static const struct got_error *
5302 check_path_prefix(struct got_object_id *parent_id,
5303 struct got_object_id *commit_id, const char *path_prefix,
5304 int errcode, struct got_repository *repo)
5306 const struct got_error *err;
5307 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5308 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5309 struct check_path_prefix_arg cpp_arg;
5311 if (got_path_is_root_dir(path_prefix))
5312 return NULL;
5314 err = got_object_open_as_commit(&commit, repo, commit_id);
5315 if (err)
5316 goto done;
5318 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5319 if (err)
5320 goto done;
5322 err = got_object_open_as_tree(&tree1, repo,
5323 got_object_commit_get_tree_id(parent_commit));
5324 if (err)
5325 goto done;
5327 err = got_object_open_as_tree(&tree2, repo,
5328 got_object_commit_get_tree_id(commit));
5329 if (err)
5330 goto done;
5332 cpp_arg.path_prefix = path_prefix;
5333 while (cpp_arg.path_prefix[0] == '/')
5334 cpp_arg.path_prefix++;
5335 cpp_arg.len = strlen(cpp_arg.path_prefix);
5336 cpp_arg.errcode = errcode;
5337 err = got_diff_tree(tree1, tree2, "", "", repo,
5338 check_path_prefix_in_diff, &cpp_arg, 0);
5339 done:
5340 if (tree1)
5341 got_object_tree_close(tree1);
5342 if (tree2)
5343 got_object_tree_close(tree2);
5344 if (commit)
5345 got_object_commit_close(commit);
5346 if (parent_commit)
5347 got_object_commit_close(parent_commit);
5348 return err;
5351 static const struct got_error *
5352 collect_commits(struct got_object_id_queue *commits,
5353 struct got_object_id *initial_commit_id,
5354 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5355 const char *path_prefix, int path_prefix_errcode,
5356 struct got_repository *repo)
5358 const struct got_error *err = NULL;
5359 struct got_commit_graph *graph = NULL;
5360 struct got_object_id *parent_id = NULL;
5361 struct got_object_qid *qid;
5362 struct got_object_id *commit_id = initial_commit_id;
5364 err = got_commit_graph_open(&graph, "/", 1);
5365 if (err)
5366 return err;
5368 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5369 check_cancelled, NULL);
5370 if (err)
5371 goto done;
5372 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5373 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5374 check_cancelled, NULL);
5375 if (err) {
5376 if (err->code == GOT_ERR_ITER_COMPLETED) {
5377 err = got_error_msg(GOT_ERR_ANCESTRY,
5378 "ran out of commits to rebase before "
5379 "youngest common ancestor commit has "
5380 "been reached?!?");
5382 goto done;
5383 } else {
5384 err = check_path_prefix(parent_id, commit_id,
5385 path_prefix, path_prefix_errcode, repo);
5386 if (err)
5387 goto done;
5389 err = got_object_qid_alloc(&qid, commit_id);
5390 if (err)
5391 goto done;
5392 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5393 commit_id = parent_id;
5396 done:
5397 got_commit_graph_close(graph);
5398 return err;
5401 static const struct got_error *
5402 cmd_rebase(int argc, char *argv[])
5404 const struct got_error *error = NULL;
5405 struct got_worktree *worktree = NULL;
5406 struct got_repository *repo = NULL;
5407 struct got_fileindex *fileindex = NULL;
5408 char *cwd = NULL;
5409 struct got_reference *branch = NULL;
5410 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5411 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5412 struct got_object_id *resume_commit_id = NULL;
5413 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5414 struct got_commit_object *commit = NULL;
5415 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5416 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5417 struct got_object_id_queue commits;
5418 struct got_pathlist_head merged_paths;
5419 const struct got_object_id_queue *parent_ids;
5420 struct got_object_qid *qid, *pid;
5422 SIMPLEQ_INIT(&commits);
5423 TAILQ_INIT(&merged_paths);
5425 while ((ch = getopt(argc, argv, "ac")) != -1) {
5426 switch (ch) {
5427 case 'a':
5428 abort_rebase = 1;
5429 break;
5430 case 'c':
5431 continue_rebase = 1;
5432 break;
5433 default:
5434 usage_rebase();
5435 /* NOTREACHED */
5439 argc -= optind;
5440 argv += optind;
5442 #ifndef PROFILE
5443 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5444 "unveil", NULL) == -1)
5445 err(1, "pledge");
5446 #endif
5447 if (abort_rebase && continue_rebase)
5448 usage_rebase();
5449 else if (abort_rebase || continue_rebase) {
5450 if (argc != 0)
5451 usage_rebase();
5452 } else if (argc != 1)
5453 usage_rebase();
5455 cwd = getcwd(NULL, 0);
5456 if (cwd == NULL) {
5457 error = got_error_from_errno("getcwd");
5458 goto done;
5460 error = got_worktree_open(&worktree, cwd);
5461 if (error)
5462 goto done;
5464 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5465 NULL);
5466 if (error != NULL)
5467 goto done;
5469 error = apply_unveil(got_repo_get_path(repo), 0,
5470 got_worktree_get_root_path(worktree));
5471 if (error)
5472 goto done;
5474 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5475 if (error)
5476 goto done;
5478 if (abort_rebase) {
5479 int did_something;
5480 if (!rebase_in_progress) {
5481 error = got_error(GOT_ERR_NOT_REBASING);
5482 goto done;
5484 error = got_worktree_rebase_continue(&resume_commit_id,
5485 &new_base_branch, &tmp_branch, &branch, &fileindex,
5486 worktree, repo);
5487 if (error)
5488 goto done;
5489 printf("Switching work tree to %s\n",
5490 got_ref_get_symref_target(new_base_branch));
5491 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5492 new_base_branch, update_progress, &did_something);
5493 if (error)
5494 goto done;
5495 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5496 goto done; /* nothing else to do */
5499 if (continue_rebase) {
5500 if (!rebase_in_progress) {
5501 error = got_error(GOT_ERR_NOT_REBASING);
5502 goto done;
5504 error = got_worktree_rebase_continue(&resume_commit_id,
5505 &new_base_branch, &tmp_branch, &branch, &fileindex,
5506 worktree, repo);
5507 if (error)
5508 goto done;
5510 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5511 resume_commit_id, repo);
5512 if (error)
5513 goto done;
5515 yca_id = got_object_id_dup(resume_commit_id);
5516 if (yca_id == NULL) {
5517 error = got_error_from_errno("got_object_id_dup");
5518 goto done;
5520 } else {
5521 error = got_ref_open(&branch, repo, argv[0], 0);
5522 if (error != NULL)
5523 goto done;
5526 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5527 if (error)
5528 goto done;
5530 if (!continue_rebase) {
5531 struct got_object_id *base_commit_id;
5533 base_commit_id = got_worktree_get_base_commit_id(worktree);
5534 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5535 base_commit_id, branch_head_commit_id, repo,
5536 check_cancelled, NULL);
5537 if (error)
5538 goto done;
5539 if (yca_id == NULL) {
5540 error = got_error_msg(GOT_ERR_ANCESTRY,
5541 "specified branch shares no common ancestry "
5542 "with work tree's branch");
5543 goto done;
5546 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5547 if (error) {
5548 if (error->code != GOT_ERR_ANCESTRY)
5549 goto done;
5550 error = NULL;
5551 } else {
5552 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5553 "specified branch resolves to a commit which "
5554 "is already contained in work tree's branch");
5555 goto done;
5557 error = got_worktree_rebase_prepare(&new_base_branch,
5558 &tmp_branch, &fileindex, worktree, branch, repo);
5559 if (error)
5560 goto done;
5563 commit_id = branch_head_commit_id;
5564 error = got_object_open_as_commit(&commit, repo, commit_id);
5565 if (error)
5566 goto done;
5568 parent_ids = got_object_commit_get_parent_ids(commit);
5569 pid = SIMPLEQ_FIRST(parent_ids);
5570 if (pid == NULL) {
5571 if (!continue_rebase) {
5572 int did_something;
5573 error = got_worktree_rebase_abort(worktree, fileindex,
5574 repo, new_base_branch, update_progress,
5575 &did_something);
5576 if (error)
5577 goto done;
5578 printf("Rebase of %s aborted\n",
5579 got_ref_get_name(branch));
5581 error = got_error(GOT_ERR_EMPTY_REBASE);
5582 goto done;
5584 error = collect_commits(&commits, commit_id, pid->id,
5585 yca_id, got_worktree_get_path_prefix(worktree),
5586 GOT_ERR_REBASE_PATH, repo);
5587 got_object_commit_close(commit);
5588 commit = NULL;
5589 if (error)
5590 goto done;
5592 if (SIMPLEQ_EMPTY(&commits)) {
5593 if (continue_rebase) {
5594 error = rebase_complete(worktree, fileindex,
5595 branch, new_base_branch, tmp_branch, repo);
5596 goto done;
5597 } else {
5598 /* Fast-forward the reference of the branch. */
5599 struct got_object_id *new_head_commit_id;
5600 char *id_str;
5601 error = got_ref_resolve(&new_head_commit_id, repo,
5602 new_base_branch);
5603 if (error)
5604 goto done;
5605 error = got_object_id_str(&id_str, new_head_commit_id);
5606 printf("Forwarding %s to commit %s\n",
5607 got_ref_get_name(branch), id_str);
5608 free(id_str);
5609 error = got_ref_change_ref(branch,
5610 new_head_commit_id);
5611 if (error)
5612 goto done;
5616 pid = NULL;
5617 SIMPLEQ_FOREACH(qid, &commits, entry) {
5618 commit_id = qid->id;
5619 parent_id = pid ? pid->id : yca_id;
5620 pid = qid;
5622 error = got_worktree_rebase_merge_files(&merged_paths,
5623 worktree, fileindex, parent_id, commit_id, repo,
5624 rebase_progress, &rebase_status, check_cancelled, NULL);
5625 if (error)
5626 goto done;
5628 if (rebase_status == GOT_STATUS_CONFLICT) {
5629 got_worktree_rebase_pathlist_free(&merged_paths);
5630 break;
5633 error = rebase_commit(&merged_paths, worktree, fileindex,
5634 tmp_branch, commit_id, repo);
5635 got_worktree_rebase_pathlist_free(&merged_paths);
5636 if (error)
5637 goto done;
5640 if (rebase_status == GOT_STATUS_CONFLICT) {
5641 error = got_worktree_rebase_postpone(worktree, fileindex);
5642 if (error)
5643 goto done;
5644 error = got_error_msg(GOT_ERR_CONFLICTS,
5645 "conflicts must be resolved before rebasing can continue");
5646 } else
5647 error = rebase_complete(worktree, fileindex, branch,
5648 new_base_branch, tmp_branch, repo);
5649 done:
5650 got_object_id_queue_free(&commits);
5651 free(branch_head_commit_id);
5652 free(resume_commit_id);
5653 free(yca_id);
5654 if (commit)
5655 got_object_commit_close(commit);
5656 if (branch)
5657 got_ref_close(branch);
5658 if (new_base_branch)
5659 got_ref_close(new_base_branch);
5660 if (tmp_branch)
5661 got_ref_close(tmp_branch);
5662 if (worktree)
5663 got_worktree_close(worktree);
5664 if (repo)
5665 got_repo_close(repo);
5666 return error;
5669 __dead static void
5670 usage_histedit(void)
5672 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5673 getprogname());
5674 exit(1);
5677 #define GOT_HISTEDIT_PICK 'p'
5678 #define GOT_HISTEDIT_EDIT 'e'
5679 #define GOT_HISTEDIT_FOLD 'f'
5680 #define GOT_HISTEDIT_DROP 'd'
5681 #define GOT_HISTEDIT_MESG 'm'
5683 static struct got_histedit_cmd {
5684 unsigned char code;
5685 const char *name;
5686 const char *desc;
5687 } got_histedit_cmds[] = {
5688 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5689 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5690 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5691 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5692 { GOT_HISTEDIT_MESG, "mesg",
5693 "single-line log message for commit above (open editor if empty)" },
5696 struct got_histedit_list_entry {
5697 TAILQ_ENTRY(got_histedit_list_entry) entry;
5698 struct got_object_id *commit_id;
5699 const struct got_histedit_cmd *cmd;
5700 char *logmsg;
5702 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5704 static const struct got_error *
5705 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5706 FILE *f, struct got_repository *repo)
5708 const struct got_error *err = NULL;
5709 char *logmsg = NULL, *id_str = NULL;
5710 struct got_commit_object *commit = NULL;
5711 int n;
5713 err = got_object_open_as_commit(&commit, repo, commit_id);
5714 if (err)
5715 goto done;
5717 err = get_short_logmsg(&logmsg, 34, commit);
5718 if (err)
5719 goto done;
5721 err = got_object_id_str(&id_str, commit_id);
5722 if (err)
5723 goto done;
5725 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5726 if (n < 0)
5727 err = got_ferror(f, GOT_ERR_IO);
5728 done:
5729 if (commit)
5730 got_object_commit_close(commit);
5731 free(id_str);
5732 free(logmsg);
5733 return err;
5736 static const struct got_error *
5737 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5738 struct got_repository *repo)
5740 const struct got_error *err = NULL;
5741 struct got_object_qid *qid;
5743 if (SIMPLEQ_EMPTY(commits))
5744 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5746 SIMPLEQ_FOREACH(qid, commits, entry) {
5747 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5748 f, repo);
5749 if (err)
5750 break;
5753 return err;
5756 static const struct got_error *
5757 write_cmd_list(FILE *f)
5759 const struct got_error *err = NULL;
5760 int n, i;
5762 n = fprintf(f, "# Available histedit commands:\n");
5763 if (n < 0)
5764 return got_ferror(f, GOT_ERR_IO);
5766 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5767 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5768 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5769 cmd->desc);
5770 if (n < 0) {
5771 err = got_ferror(f, GOT_ERR_IO);
5772 break;
5775 n = fprintf(f, "# Commits will be processed in order from top to "
5776 "bottom of this file.\n");
5777 if (n < 0)
5778 return got_ferror(f, GOT_ERR_IO);
5779 return err;
5782 static const struct got_error *
5783 histedit_syntax_error(int lineno)
5785 static char msg[42];
5786 int ret;
5788 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5789 lineno);
5790 if (ret == -1 || ret >= sizeof(msg))
5791 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5793 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5796 static const struct got_error *
5797 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5798 char *logmsg, struct got_repository *repo)
5800 const struct got_error *err;
5801 struct got_commit_object *folded_commit = NULL;
5802 char *id_str, *folded_logmsg = NULL;
5804 err = got_object_id_str(&id_str, hle->commit_id);
5805 if (err)
5806 return err;
5808 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5809 if (err)
5810 goto done;
5812 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5813 if (err)
5814 goto done;
5815 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5816 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5817 folded_logmsg) == -1) {
5818 err = got_error_from_errno("asprintf");
5819 goto done;
5821 done:
5822 if (folded_commit)
5823 got_object_commit_close(folded_commit);
5824 free(id_str);
5825 free(folded_logmsg);
5826 return err;
5829 static struct got_histedit_list_entry *
5830 get_folded_commits(struct got_histedit_list_entry *hle)
5832 struct got_histedit_list_entry *prev, *folded = NULL;
5834 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5835 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5836 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5837 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5838 folded = prev;
5839 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5842 return folded;
5845 static const struct got_error *
5846 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5847 struct got_repository *repo)
5849 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5850 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5851 const struct got_error *err = NULL;
5852 struct got_commit_object *commit = NULL;
5853 int fd;
5854 struct got_histedit_list_entry *folded = NULL;
5856 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5857 if (err)
5858 return err;
5860 folded = get_folded_commits(hle);
5861 if (folded) {
5862 while (folded != hle) {
5863 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5864 folded = TAILQ_NEXT(folded, entry);
5865 continue;
5867 err = append_folded_commit_msg(&new_msg, folded,
5868 logmsg, repo);
5869 if (err)
5870 goto done;
5871 free(logmsg);
5872 logmsg = new_msg;
5873 folded = TAILQ_NEXT(folded, entry);
5877 err = got_object_id_str(&id_str, hle->commit_id);
5878 if (err)
5879 goto done;
5880 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5881 if (err)
5882 goto done;
5883 if (asprintf(&new_msg,
5884 "%s\n# original log message of commit %s: %s",
5885 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5886 err = got_error_from_errno("asprintf");
5887 goto done;
5889 free(logmsg);
5890 logmsg = new_msg;
5892 err = got_object_id_str(&id_str, hle->commit_id);
5893 if (err)
5894 goto done;
5896 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5897 if (err)
5898 goto done;
5900 dprintf(fd, logmsg);
5901 close(fd);
5903 err = get_editor(&editor);
5904 if (err)
5905 goto done;
5907 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5908 if (err) {
5909 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5910 goto done;
5911 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5913 done:
5914 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5915 err = got_error_from_errno2("unlink", logmsg_path);
5916 free(logmsg_path);
5917 free(logmsg);
5918 free(orig_logmsg);
5919 free(editor);
5920 if (commit)
5921 got_object_commit_close(commit);
5922 return err;
5925 static const struct got_error *
5926 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5927 FILE *f, struct got_repository *repo)
5929 const struct got_error *err = NULL;
5930 char *line = NULL, *p, *end;
5931 size_t size;
5932 ssize_t len;
5933 int lineno = 0, i;
5934 const struct got_histedit_cmd *cmd;
5935 struct got_object_id *commit_id = NULL;
5936 struct got_histedit_list_entry *hle = NULL;
5938 for (;;) {
5939 len = getline(&line, &size, f);
5940 if (len == -1) {
5941 const struct got_error *getline_err;
5942 if (feof(f))
5943 break;
5944 getline_err = got_error_from_errno("getline");
5945 err = got_ferror(f, getline_err->code);
5946 break;
5948 lineno++;
5949 p = line;
5950 while (isspace((unsigned char)p[0]))
5951 p++;
5952 if (p[0] == '#' || p[0] == '\0') {
5953 free(line);
5954 line = NULL;
5955 continue;
5957 cmd = NULL;
5958 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5959 cmd = &got_histedit_cmds[i];
5960 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5961 isspace((unsigned char)p[strlen(cmd->name)])) {
5962 p += strlen(cmd->name);
5963 break;
5965 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5966 p++;
5967 break;
5970 if (i == nitems(got_histedit_cmds)) {
5971 err = histedit_syntax_error(lineno);
5972 break;
5974 while (isspace((unsigned char)p[0]))
5975 p++;
5976 if (cmd->code == GOT_HISTEDIT_MESG) {
5977 if (hle == NULL || hle->logmsg != NULL) {
5978 err = got_error(GOT_ERR_HISTEDIT_CMD);
5979 break;
5981 if (p[0] == '\0') {
5982 err = histedit_edit_logmsg(hle, repo);
5983 if (err)
5984 break;
5985 } else {
5986 hle->logmsg = strdup(p);
5987 if (hle->logmsg == NULL) {
5988 err = got_error_from_errno("strdup");
5989 break;
5992 free(line);
5993 line = NULL;
5994 continue;
5995 } else {
5996 end = p;
5997 while (end[0] && !isspace((unsigned char)end[0]))
5998 end++;
5999 *end = '\0';
6001 err = got_object_resolve_id_str(&commit_id, repo, p);
6002 if (err) {
6003 /* override error code */
6004 err = histedit_syntax_error(lineno);
6005 break;
6008 hle = malloc(sizeof(*hle));
6009 if (hle == NULL) {
6010 err = got_error_from_errno("malloc");
6011 break;
6013 hle->cmd = cmd;
6014 hle->commit_id = commit_id;
6015 hle->logmsg = NULL;
6016 commit_id = NULL;
6017 free(line);
6018 line = NULL;
6019 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6022 free(line);
6023 free(commit_id);
6024 return err;
6027 static const struct got_error *
6028 histedit_check_script(struct got_histedit_list *histedit_cmds,
6029 struct got_object_id_queue *commits, struct got_repository *repo)
6031 const struct got_error *err = NULL;
6032 struct got_object_qid *qid;
6033 struct got_histedit_list_entry *hle;
6034 static char msg[80];
6035 char *id_str;
6037 if (TAILQ_EMPTY(histedit_cmds))
6038 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6039 "histedit script contains no commands");
6040 if (SIMPLEQ_EMPTY(commits))
6041 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6043 SIMPLEQ_FOREACH(qid, commits, entry) {
6044 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6045 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6046 break;
6048 if (hle == NULL) {
6049 err = got_object_id_str(&id_str, qid->id);
6050 if (err)
6051 return err;
6052 snprintf(msg, sizeof(msg),
6053 "commit %s missing from histedit script", id_str);
6054 free(id_str);
6055 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6059 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6060 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6061 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6062 "last commit in histedit script cannot be folded");
6064 return NULL;
6067 static const struct got_error *
6068 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6069 const char *path, struct got_object_id_queue *commits,
6070 struct got_repository *repo)
6072 const struct got_error *err = NULL;
6073 char *editor;
6074 FILE *f = NULL;
6076 err = get_editor(&editor);
6077 if (err)
6078 return err;
6080 if (spawn_editor(editor, path) == -1) {
6081 err = got_error_from_errno("failed spawning editor");
6082 goto done;
6085 f = fopen(path, "r");
6086 if (f == NULL) {
6087 err = got_error_from_errno("fopen");
6088 goto done;
6090 err = histedit_parse_list(histedit_cmds, f, repo);
6091 if (err)
6092 goto done;
6094 err = histedit_check_script(histedit_cmds, commits, repo);
6095 done:
6096 if (f && fclose(f) != 0 && err == NULL)
6097 err = got_error_from_errno("fclose");
6098 free(editor);
6099 return err;
6102 static const struct got_error *
6103 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6104 struct got_object_id_queue *, const char *, struct got_repository *);
6106 static const struct got_error *
6107 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6108 struct got_object_id_queue *commits, struct got_repository *repo)
6110 const struct got_error *err;
6111 FILE *f = NULL;
6112 char *path = NULL;
6114 err = got_opentemp_named(&path, &f, "got-histedit");
6115 if (err)
6116 return err;
6118 err = write_cmd_list(f);
6119 if (err)
6120 goto done;
6122 err = histedit_write_commit_list(commits, f, repo);
6123 if (err)
6124 goto done;
6126 if (fclose(f) != 0) {
6127 err = got_error_from_errno("fclose");
6128 goto done;
6130 f = NULL;
6132 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6133 if (err) {
6134 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6135 err->code != GOT_ERR_HISTEDIT_CMD)
6136 goto done;
6137 err = histedit_edit_list_retry(histedit_cmds, err,
6138 commits, path, repo);
6140 done:
6141 if (f && fclose(f) != 0 && err == NULL)
6142 err = got_error_from_errno("fclose");
6143 if (path && unlink(path) != 0 && err == NULL)
6144 err = got_error_from_errno2("unlink", path);
6145 free(path);
6146 return err;
6149 static const struct got_error *
6150 histedit_save_list(struct got_histedit_list *histedit_cmds,
6151 struct got_worktree *worktree, struct got_repository *repo)
6153 const struct got_error *err = NULL;
6154 char *path = NULL;
6155 FILE *f = NULL;
6156 struct got_histedit_list_entry *hle;
6157 struct got_commit_object *commit = NULL;
6159 err = got_worktree_get_histedit_script_path(&path, worktree);
6160 if (err)
6161 return err;
6163 f = fopen(path, "w");
6164 if (f == NULL) {
6165 err = got_error_from_errno2("fopen", path);
6166 goto done;
6168 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6169 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6170 repo);
6171 if (err)
6172 break;
6174 if (hle->logmsg) {
6175 int n = fprintf(f, "%c %s\n",
6176 GOT_HISTEDIT_MESG, hle->logmsg);
6177 if (n < 0) {
6178 err = got_ferror(f, GOT_ERR_IO);
6179 break;
6183 done:
6184 if (f && fclose(f) != 0 && err == NULL)
6185 err = got_error_from_errno("fclose");
6186 free(path);
6187 if (commit)
6188 got_object_commit_close(commit);
6189 return err;
6192 void
6193 histedit_free_list(struct got_histedit_list *histedit_cmds)
6195 struct got_histedit_list_entry *hle;
6197 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6198 TAILQ_REMOVE(histedit_cmds, hle, entry);
6199 free(hle);
6203 static const struct got_error *
6204 histedit_load_list(struct got_histedit_list *histedit_cmds,
6205 const char *path, struct got_repository *repo)
6207 const struct got_error *err = NULL;
6208 FILE *f = NULL;
6210 f = fopen(path, "r");
6211 if (f == NULL) {
6212 err = got_error_from_errno2("fopen", path);
6213 goto done;
6216 err = histedit_parse_list(histedit_cmds, f, repo);
6217 done:
6218 if (f && fclose(f) != 0 && err == NULL)
6219 err = got_error_from_errno("fclose");
6220 return err;
6223 static const struct got_error *
6224 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6225 const struct got_error *edit_err, struct got_object_id_queue *commits,
6226 const char *path, struct got_repository *repo)
6228 const struct got_error *err = NULL, *prev_err = edit_err;
6229 int resp = ' ';
6231 while (resp != 'c' && resp != 'r' && resp != 'a') {
6232 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6233 "or (a)bort: ", getprogname(), prev_err->msg);
6234 resp = getchar();
6235 if (resp == '\n')
6236 resp = getchar();
6237 if (resp == 'c') {
6238 histedit_free_list(histedit_cmds);
6239 err = histedit_run_editor(histedit_cmds, path, commits,
6240 repo);
6241 if (err) {
6242 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6243 err->code != GOT_ERR_HISTEDIT_CMD)
6244 break;
6245 prev_err = err;
6246 resp = ' ';
6247 continue;
6249 break;
6250 } else if (resp == 'r') {
6251 histedit_free_list(histedit_cmds);
6252 err = histedit_edit_script(histedit_cmds,
6253 commits, repo);
6254 if (err) {
6255 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6256 err->code != GOT_ERR_HISTEDIT_CMD)
6257 break;
6258 prev_err = err;
6259 resp = ' ';
6260 continue;
6262 break;
6263 } else if (resp == 'a') {
6264 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6265 break;
6266 } else
6267 printf("invalid response '%c'\n", resp);
6270 return err;
6273 static const struct got_error *
6274 histedit_complete(struct got_worktree *worktree,
6275 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6276 struct got_reference *branch, struct got_repository *repo)
6278 printf("Switching work tree to %s\n",
6279 got_ref_get_symref_target(branch));
6280 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6281 branch, repo);
6284 static const struct got_error *
6285 show_histedit_progress(struct got_commit_object *commit,
6286 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6288 const struct got_error *err;
6289 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6291 err = got_object_id_str(&old_id_str, hle->commit_id);
6292 if (err)
6293 goto done;
6295 if (new_id) {
6296 err = got_object_id_str(&new_id_str, new_id);
6297 if (err)
6298 goto done;
6301 old_id_str[12] = '\0';
6302 if (new_id_str)
6303 new_id_str[12] = '\0';
6305 if (hle->logmsg) {
6306 logmsg = strdup(hle->logmsg);
6307 if (logmsg == NULL) {
6308 err = got_error_from_errno("strdup");
6309 goto done;
6311 trim_logmsg(logmsg, 42);
6312 } else {
6313 err = get_short_logmsg(&logmsg, 42, commit);
6314 if (err)
6315 goto done;
6318 switch (hle->cmd->code) {
6319 case GOT_HISTEDIT_PICK:
6320 case GOT_HISTEDIT_EDIT:
6321 printf("%s -> %s: %s\n", old_id_str,
6322 new_id_str ? new_id_str : "no-op change", logmsg);
6323 break;
6324 case GOT_HISTEDIT_DROP:
6325 case GOT_HISTEDIT_FOLD:
6326 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6327 logmsg);
6328 break;
6329 default:
6330 break;
6333 done:
6334 free(old_id_str);
6335 free(new_id_str);
6336 return err;
6339 static const struct got_error *
6340 histedit_commit(struct got_pathlist_head *merged_paths,
6341 struct got_worktree *worktree, struct got_fileindex *fileindex,
6342 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6343 struct got_repository *repo)
6345 const struct got_error *err;
6346 struct got_commit_object *commit;
6347 struct got_object_id *new_commit_id;
6349 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6350 && hle->logmsg == NULL) {
6351 err = histedit_edit_logmsg(hle, repo);
6352 if (err)
6353 return err;
6356 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6357 if (err)
6358 return err;
6360 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6361 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6362 hle->logmsg, repo);
6363 if (err) {
6364 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6365 goto done;
6366 err = show_histedit_progress(commit, hle, NULL);
6367 } else {
6368 err = show_histedit_progress(commit, hle, new_commit_id);
6369 free(new_commit_id);
6371 done:
6372 got_object_commit_close(commit);
6373 return err;
6376 static const struct got_error *
6377 histedit_skip_commit(struct got_histedit_list_entry *hle,
6378 struct got_worktree *worktree, struct got_repository *repo)
6380 const struct got_error *error;
6381 struct got_commit_object *commit;
6383 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6384 repo);
6385 if (error)
6386 return error;
6388 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6389 if (error)
6390 return error;
6392 error = show_histedit_progress(commit, hle, NULL);
6393 got_object_commit_close(commit);
6394 return error;
6397 static const struct got_error *
6398 cmd_histedit(int argc, char *argv[])
6400 const struct got_error *error = NULL;
6401 struct got_worktree *worktree = NULL;
6402 struct got_fileindex *fileindex = NULL;
6403 struct got_repository *repo = NULL;
6404 char *cwd = NULL;
6405 struct got_reference *branch = NULL;
6406 struct got_reference *tmp_branch = NULL;
6407 struct got_object_id *resume_commit_id = NULL;
6408 struct got_object_id *base_commit_id = NULL;
6409 struct got_object_id *head_commit_id = NULL;
6410 struct got_commit_object *commit = NULL;
6411 int ch, rebase_in_progress = 0, did_something;
6412 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6413 const char *edit_script_path = NULL;
6414 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6415 struct got_object_id_queue commits;
6416 struct got_pathlist_head merged_paths;
6417 const struct got_object_id_queue *parent_ids;
6418 struct got_object_qid *pid;
6419 struct got_histedit_list histedit_cmds;
6420 struct got_histedit_list_entry *hle;
6422 SIMPLEQ_INIT(&commits);
6423 TAILQ_INIT(&histedit_cmds);
6424 TAILQ_INIT(&merged_paths);
6426 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6427 switch (ch) {
6428 case 'a':
6429 abort_edit = 1;
6430 break;
6431 case 'c':
6432 continue_edit = 1;
6433 break;
6434 case 'F':
6435 edit_script_path = optarg;
6436 break;
6437 default:
6438 usage_histedit();
6439 /* NOTREACHED */
6443 argc -= optind;
6444 argv += optind;
6446 #ifndef PROFILE
6447 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6448 "unveil", NULL) == -1)
6449 err(1, "pledge");
6450 #endif
6451 if (abort_edit && continue_edit)
6452 usage_histedit();
6453 if (argc != 0)
6454 usage_histedit();
6457 * This command cannot apply unveil(2) in all cases because the
6458 * user may choose to run an editor to edit the histedit script
6459 * and to edit individual commit log messages.
6460 * unveil(2) traverses exec(2); if an editor is used we have to
6461 * apply unveil after edit script and log messages have been written.
6462 * XXX TODO: Make use of unveil(2) where possible.
6465 cwd = getcwd(NULL, 0);
6466 if (cwd == NULL) {
6467 error = got_error_from_errno("getcwd");
6468 goto done;
6470 error = got_worktree_open(&worktree, cwd);
6471 if (error)
6472 goto done;
6474 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6475 NULL);
6476 if (error != NULL)
6477 goto done;
6479 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6480 if (error)
6481 goto done;
6482 if (rebase_in_progress) {
6483 error = got_error(GOT_ERR_REBASING);
6484 goto done;
6487 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6488 if (error)
6489 goto done;
6491 if (edit_in_progress && abort_edit) {
6492 error = got_worktree_histedit_continue(&resume_commit_id,
6493 &tmp_branch, &branch, &base_commit_id, &fileindex,
6494 worktree, repo);
6495 if (error)
6496 goto done;
6497 printf("Switching work tree to %s\n",
6498 got_ref_get_symref_target(branch));
6499 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6500 branch, base_commit_id, update_progress, &did_something);
6501 if (error)
6502 goto done;
6503 printf("Histedit of %s aborted\n",
6504 got_ref_get_symref_target(branch));
6505 goto done; /* nothing else to do */
6506 } else if (abort_edit) {
6507 error = got_error(GOT_ERR_NOT_HISTEDIT);
6508 goto done;
6511 if (continue_edit) {
6512 char *path;
6514 if (!edit_in_progress) {
6515 error = got_error(GOT_ERR_NOT_HISTEDIT);
6516 goto done;
6519 error = got_worktree_get_histedit_script_path(&path, worktree);
6520 if (error)
6521 goto done;
6523 error = histedit_load_list(&histedit_cmds, path, repo);
6524 free(path);
6525 if (error)
6526 goto done;
6528 error = got_worktree_histedit_continue(&resume_commit_id,
6529 &tmp_branch, &branch, &base_commit_id, &fileindex,
6530 worktree, repo);
6531 if (error)
6532 goto done;
6534 error = got_ref_resolve(&head_commit_id, repo, branch);
6535 if (error)
6536 goto done;
6538 error = got_object_open_as_commit(&commit, repo,
6539 head_commit_id);
6540 if (error)
6541 goto done;
6542 parent_ids = got_object_commit_get_parent_ids(commit);
6543 pid = SIMPLEQ_FIRST(parent_ids);
6544 if (pid == NULL) {
6545 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6546 goto done;
6548 error = collect_commits(&commits, head_commit_id, pid->id,
6549 base_commit_id, got_worktree_get_path_prefix(worktree),
6550 GOT_ERR_HISTEDIT_PATH, repo);
6551 got_object_commit_close(commit);
6552 commit = NULL;
6553 if (error)
6554 goto done;
6555 } else {
6556 if (edit_in_progress) {
6557 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6558 goto done;
6561 error = got_ref_open(&branch, repo,
6562 got_worktree_get_head_ref_name(worktree), 0);
6563 if (error != NULL)
6564 goto done;
6566 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6567 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6568 "will not edit commit history of a branch outside "
6569 "the \"refs/heads/\" reference namespace");
6570 goto done;
6573 error = got_ref_resolve(&head_commit_id, repo, branch);
6574 got_ref_close(branch);
6575 branch = NULL;
6576 if (error)
6577 goto done;
6579 error = got_object_open_as_commit(&commit, repo,
6580 head_commit_id);
6581 if (error)
6582 goto done;
6583 parent_ids = got_object_commit_get_parent_ids(commit);
6584 pid = SIMPLEQ_FIRST(parent_ids);
6585 if (pid == NULL) {
6586 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6587 goto done;
6589 error = collect_commits(&commits, head_commit_id, pid->id,
6590 got_worktree_get_base_commit_id(worktree),
6591 got_worktree_get_path_prefix(worktree),
6592 GOT_ERR_HISTEDIT_PATH, repo);
6593 got_object_commit_close(commit);
6594 commit = NULL;
6595 if (error)
6596 goto done;
6598 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6599 &base_commit_id, &fileindex, worktree, repo);
6600 if (error)
6601 goto done;
6603 if (edit_script_path) {
6604 error = histedit_load_list(&histedit_cmds,
6605 edit_script_path, repo);
6606 if (error) {
6607 got_worktree_histedit_abort(worktree, fileindex,
6608 repo, branch, base_commit_id,
6609 update_progress, &did_something);
6610 goto done;
6612 } else {
6613 error = histedit_edit_script(&histedit_cmds, &commits,
6614 repo);
6615 if (error) {
6616 got_worktree_histedit_abort(worktree, fileindex,
6617 repo, branch, base_commit_id,
6618 update_progress, &did_something);
6619 goto done;
6624 error = histedit_save_list(&histedit_cmds, worktree,
6625 repo);
6626 if (error) {
6627 got_worktree_histedit_abort(worktree, fileindex,
6628 repo, branch, base_commit_id,
6629 update_progress, &did_something);
6630 goto done;
6635 error = histedit_check_script(&histedit_cmds, &commits, repo);
6636 if (error)
6637 goto done;
6639 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6640 if (resume_commit_id) {
6641 if (got_object_id_cmp(hle->commit_id,
6642 resume_commit_id) != 0)
6643 continue;
6645 resume_commit_id = NULL;
6646 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6647 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6648 error = histedit_skip_commit(hle, worktree,
6649 repo);
6650 } else {
6651 error = histedit_commit(NULL, worktree,
6652 fileindex, tmp_branch, hle, repo);
6654 if (error)
6655 goto done;
6656 continue;
6659 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6660 error = histedit_skip_commit(hle, worktree, repo);
6661 if (error)
6662 goto done;
6663 continue;
6666 error = got_object_open_as_commit(&commit, repo,
6667 hle->commit_id);
6668 if (error)
6669 goto done;
6670 parent_ids = got_object_commit_get_parent_ids(commit);
6671 pid = SIMPLEQ_FIRST(parent_ids);
6673 error = got_worktree_histedit_merge_files(&merged_paths,
6674 worktree, fileindex, pid->id, hle->commit_id, repo,
6675 rebase_progress, &rebase_status, check_cancelled, NULL);
6676 if (error)
6677 goto done;
6678 got_object_commit_close(commit);
6679 commit = NULL;
6681 if (rebase_status == GOT_STATUS_CONFLICT) {
6682 got_worktree_rebase_pathlist_free(&merged_paths);
6683 break;
6686 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6687 char *id_str;
6688 error = got_object_id_str(&id_str, hle->commit_id);
6689 if (error)
6690 goto done;
6691 printf("Stopping histedit for amending commit %s\n",
6692 id_str);
6693 free(id_str);
6694 got_worktree_rebase_pathlist_free(&merged_paths);
6695 error = got_worktree_histedit_postpone(worktree,
6696 fileindex);
6697 goto done;
6700 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6701 error = histedit_skip_commit(hle, worktree, repo);
6702 if (error)
6703 goto done;
6704 continue;
6707 error = histedit_commit(&merged_paths, worktree, fileindex,
6708 tmp_branch, hle, repo);
6709 got_worktree_rebase_pathlist_free(&merged_paths);
6710 if (error)
6711 goto done;
6714 if (rebase_status == GOT_STATUS_CONFLICT) {
6715 error = got_worktree_histedit_postpone(worktree, fileindex);
6716 if (error)
6717 goto done;
6718 error = got_error_msg(GOT_ERR_CONFLICTS,
6719 "conflicts must be resolved before rebasing can continue");
6720 } else
6721 error = histedit_complete(worktree, fileindex, tmp_branch,
6722 branch, repo);
6723 done:
6724 got_object_id_queue_free(&commits);
6725 histedit_free_list(&histedit_cmds);
6726 free(head_commit_id);
6727 free(base_commit_id);
6728 free(resume_commit_id);
6729 if (commit)
6730 got_object_commit_close(commit);
6731 if (branch)
6732 got_ref_close(branch);
6733 if (tmp_branch)
6734 got_ref_close(tmp_branch);
6735 if (worktree)
6736 got_worktree_close(worktree);
6737 if (repo)
6738 got_repo_close(repo);
6739 return error;
6742 __dead static void
6743 usage_integrate(void)
6745 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6746 exit(1);
6749 static const struct got_error *
6750 cmd_integrate(int argc, char *argv[])
6752 const struct got_error *error = NULL;
6753 struct got_repository *repo = NULL;
6754 struct got_worktree *worktree = NULL;
6755 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6756 const char *branch_arg = NULL;
6757 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6758 struct got_fileindex *fileindex = NULL;
6759 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6760 int ch, did_something = 0;
6762 while ((ch = getopt(argc, argv, "")) != -1) {
6763 switch (ch) {
6764 default:
6765 usage_integrate();
6766 /* NOTREACHED */
6770 argc -= optind;
6771 argv += optind;
6773 if (argc != 1)
6774 usage_integrate();
6775 branch_arg = argv[0];
6777 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6778 "unveil", NULL) == -1)
6779 err(1, "pledge");
6781 cwd = getcwd(NULL, 0);
6782 if (cwd == NULL) {
6783 error = got_error_from_errno("getcwd");
6784 goto done;
6787 error = got_worktree_open(&worktree, cwd);
6788 if (error)
6789 goto done;
6791 error = check_rebase_or_histedit_in_progress(worktree);
6792 if (error)
6793 goto done;
6795 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6796 NULL);
6797 if (error != NULL)
6798 goto done;
6800 error = apply_unveil(got_repo_get_path(repo), 0,
6801 got_worktree_get_root_path(worktree));
6802 if (error)
6803 goto done;
6805 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6806 error = got_error_from_errno("asprintf");
6807 goto done;
6810 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6811 &base_branch_ref, worktree, refname, repo);
6812 if (error)
6813 goto done;
6815 refname = strdup(got_ref_get_name(branch_ref));
6816 if (refname == NULL) {
6817 error = got_error_from_errno("strdup");
6818 got_worktree_integrate_abort(worktree, fileindex, repo,
6819 branch_ref, base_branch_ref);
6820 goto done;
6822 base_refname = strdup(got_ref_get_name(base_branch_ref));
6823 if (base_refname == NULL) {
6824 error = got_error_from_errno("strdup");
6825 got_worktree_integrate_abort(worktree, fileindex, repo,
6826 branch_ref, base_branch_ref);
6827 goto done;
6830 error = got_ref_resolve(&commit_id, repo, branch_ref);
6831 if (error)
6832 goto done;
6834 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6835 if (error)
6836 goto done;
6838 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6839 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6840 "specified branch has already been integrated");
6841 got_worktree_integrate_abort(worktree, fileindex, repo,
6842 branch_ref, base_branch_ref);
6843 goto done;
6846 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6847 if (error) {
6848 if (error->code == GOT_ERR_ANCESTRY)
6849 error = got_error(GOT_ERR_REBASE_REQUIRED);
6850 got_worktree_integrate_abort(worktree, fileindex, repo,
6851 branch_ref, base_branch_ref);
6852 goto done;
6855 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6856 branch_ref, base_branch_ref, update_progress, &did_something,
6857 check_cancelled, NULL);
6858 if (error)
6859 goto done;
6861 printf("Integrated %s into %s\n", refname, base_refname);
6862 done:
6863 if (repo)
6864 got_repo_close(repo);
6865 if (worktree)
6866 got_worktree_close(worktree);
6867 free(cwd);
6868 free(base_commit_id);
6869 free(commit_id);
6870 free(refname);
6871 free(base_refname);
6872 return error;
6875 __dead static void
6876 usage_stage(void)
6878 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6879 "[file-path ...]\n",
6880 getprogname());
6881 exit(1);
6884 static const struct got_error *
6885 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6886 const char *path, struct got_object_id *blob_id,
6887 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6888 int dirfd, const char *de_name)
6890 const struct got_error *err = NULL;
6891 char *id_str = NULL;
6893 if (staged_status != GOT_STATUS_ADD &&
6894 staged_status != GOT_STATUS_MODIFY &&
6895 staged_status != GOT_STATUS_DELETE)
6896 return NULL;
6898 if (staged_status == GOT_STATUS_ADD ||
6899 staged_status == GOT_STATUS_MODIFY)
6900 err = got_object_id_str(&id_str, staged_blob_id);
6901 else
6902 err = got_object_id_str(&id_str, blob_id);
6903 if (err)
6904 return err;
6906 printf("%s %c %s\n", id_str, staged_status, path);
6907 free(id_str);
6908 return NULL;
6911 static const struct got_error *
6912 cmd_stage(int argc, char *argv[])
6914 const struct got_error *error = NULL;
6915 struct got_repository *repo = NULL;
6916 struct got_worktree *worktree = NULL;
6917 char *cwd = NULL;
6918 struct got_pathlist_head paths;
6919 struct got_pathlist_entry *pe;
6920 int ch, list_stage = 0, pflag = 0;
6921 FILE *patch_script_file = NULL;
6922 const char *patch_script_path = NULL;
6923 struct choose_patch_arg cpa;
6925 TAILQ_INIT(&paths);
6927 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6928 switch (ch) {
6929 case 'l':
6930 list_stage = 1;
6931 break;
6932 case 'p':
6933 pflag = 1;
6934 break;
6935 case 'F':
6936 patch_script_path = optarg;
6937 break;
6938 default:
6939 usage_stage();
6940 /* NOTREACHED */
6944 argc -= optind;
6945 argv += optind;
6947 #ifndef PROFILE
6948 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6949 "unveil", NULL) == -1)
6950 err(1, "pledge");
6951 #endif
6952 if (list_stage && (pflag || patch_script_path))
6953 errx(1, "-l option cannot be used with other options");
6954 if (patch_script_path && !pflag)
6955 errx(1, "-F option can only be used together with -p option");
6957 cwd = getcwd(NULL, 0);
6958 if (cwd == NULL) {
6959 error = got_error_from_errno("getcwd");
6960 goto done;
6963 error = got_worktree_open(&worktree, cwd);
6964 if (error)
6965 goto done;
6967 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6968 NULL);
6969 if (error != NULL)
6970 goto done;
6972 if (patch_script_path) {
6973 patch_script_file = fopen(patch_script_path, "r");
6974 if (patch_script_file == NULL) {
6975 error = got_error_from_errno2("fopen",
6976 patch_script_path);
6977 goto done;
6980 error = apply_unveil(got_repo_get_path(repo), 0,
6981 got_worktree_get_root_path(worktree));
6982 if (error)
6983 goto done;
6985 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6986 if (error)
6987 goto done;
6989 if (list_stage)
6990 error = got_worktree_status(worktree, &paths, repo,
6991 print_stage, NULL, check_cancelled, NULL);
6992 else {
6993 cpa.patch_script_file = patch_script_file;
6994 cpa.action = "stage";
6995 error = got_worktree_stage(worktree, &paths,
6996 pflag ? NULL : print_status, NULL,
6997 pflag ? choose_patch : NULL, &cpa, repo);
6999 done:
7000 if (patch_script_file && fclose(patch_script_file) == EOF &&
7001 error == NULL)
7002 error = got_error_from_errno2("fclose", patch_script_path);
7003 if (repo)
7004 got_repo_close(repo);
7005 if (worktree)
7006 got_worktree_close(worktree);
7007 TAILQ_FOREACH(pe, &paths, entry)
7008 free((char *)pe->path);
7009 got_pathlist_free(&paths);
7010 free(cwd);
7011 return error;
7014 __dead static void
7015 usage_unstage(void)
7017 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7018 "[file-path ...]\n",
7019 getprogname());
7020 exit(1);
7024 static const struct got_error *
7025 cmd_unstage(int argc, char *argv[])
7027 const struct got_error *error = NULL;
7028 struct got_repository *repo = NULL;
7029 struct got_worktree *worktree = NULL;
7030 char *cwd = NULL;
7031 struct got_pathlist_head paths;
7032 struct got_pathlist_entry *pe;
7033 int ch, did_something = 0, pflag = 0;
7034 FILE *patch_script_file = NULL;
7035 const char *patch_script_path = NULL;
7036 struct choose_patch_arg cpa;
7038 TAILQ_INIT(&paths);
7040 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7041 switch (ch) {
7042 case 'p':
7043 pflag = 1;
7044 break;
7045 case 'F':
7046 patch_script_path = optarg;
7047 break;
7048 default:
7049 usage_unstage();
7050 /* NOTREACHED */
7054 argc -= optind;
7055 argv += optind;
7057 #ifndef PROFILE
7058 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7059 "unveil", NULL) == -1)
7060 err(1, "pledge");
7061 #endif
7062 if (patch_script_path && !pflag)
7063 errx(1, "-F option can only be used together with -p option");
7065 cwd = getcwd(NULL, 0);
7066 if (cwd == NULL) {
7067 error = got_error_from_errno("getcwd");
7068 goto done;
7071 error = got_worktree_open(&worktree, cwd);
7072 if (error)
7073 goto done;
7075 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7076 NULL);
7077 if (error != NULL)
7078 goto done;
7080 if (patch_script_path) {
7081 patch_script_file = fopen(patch_script_path, "r");
7082 if (patch_script_file == NULL) {
7083 error = got_error_from_errno2("fopen",
7084 patch_script_path);
7085 goto done;
7089 error = apply_unveil(got_repo_get_path(repo), 0,
7090 got_worktree_get_root_path(worktree));
7091 if (error)
7092 goto done;
7094 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7095 if (error)
7096 goto done;
7098 cpa.patch_script_file = patch_script_file;
7099 cpa.action = "unstage";
7100 error = got_worktree_unstage(worktree, &paths, update_progress,
7101 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7102 done:
7103 if (patch_script_file && fclose(patch_script_file) == EOF &&
7104 error == NULL)
7105 error = got_error_from_errno2("fclose", patch_script_path);
7106 if (repo)
7107 got_repo_close(repo);
7108 if (worktree)
7109 got_worktree_close(worktree);
7110 TAILQ_FOREACH(pe, &paths, entry)
7111 free((char *)pe->path);
7112 got_pathlist_free(&paths);
7113 free(cwd);
7114 return error;
7117 __dead static void
7118 usage_cat(void)
7120 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7121 "arg1 [arg2 ...]\n", getprogname());
7122 exit(1);
7125 static const struct got_error *
7126 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7128 const struct got_error *err;
7129 struct got_blob_object *blob;
7131 err = got_object_open_as_blob(&blob, repo, id, 8192);
7132 if (err)
7133 return err;
7135 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7136 got_object_blob_close(blob);
7137 return err;
7140 static const struct got_error *
7141 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7143 const struct got_error *err;
7144 struct got_tree_object *tree;
7145 int nentries, i;
7147 err = got_object_open_as_tree(&tree, repo, id);
7148 if (err)
7149 return err;
7151 nentries = got_object_tree_get_nentries(tree);
7152 for (i = 0; i < nentries; i++) {
7153 struct got_tree_entry *te;
7154 char *id_str;
7155 if (sigint_received || sigpipe_received)
7156 break;
7157 te = got_object_tree_get_entry(tree, i);
7158 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7159 if (err)
7160 break;
7161 fprintf(outfile, "%s %.7o %s\n", id_str,
7162 got_tree_entry_get_mode(te),
7163 got_tree_entry_get_name(te));
7164 free(id_str);
7167 got_object_tree_close(tree);
7168 return err;
7171 static const struct got_error *
7172 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7174 const struct got_error *err;
7175 struct got_commit_object *commit;
7176 const struct got_object_id_queue *parent_ids;
7177 struct got_object_qid *pid;
7178 char *id_str = NULL;
7179 const char *logmsg = NULL;
7181 err = got_object_open_as_commit(&commit, repo, id);
7182 if (err)
7183 return err;
7185 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7186 if (err)
7187 goto done;
7189 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7190 parent_ids = got_object_commit_get_parent_ids(commit);
7191 fprintf(outfile, "numparents %d\n",
7192 got_object_commit_get_nparents(commit));
7193 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7194 char *pid_str;
7195 err = got_object_id_str(&pid_str, pid->id);
7196 if (err)
7197 goto done;
7198 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7199 free(pid_str);
7201 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7202 got_object_commit_get_author(commit),
7203 got_object_commit_get_author_time(commit));
7205 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7206 got_object_commit_get_author(commit),
7207 got_object_commit_get_committer_time(commit));
7209 logmsg = got_object_commit_get_logmsg_raw(commit);
7210 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7211 fprintf(outfile, "%s", logmsg);
7212 done:
7213 free(id_str);
7214 got_object_commit_close(commit);
7215 return err;
7218 static const struct got_error *
7219 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7221 const struct got_error *err;
7222 struct got_tag_object *tag;
7223 char *id_str = NULL;
7224 const char *tagmsg = NULL;
7226 err = got_object_open_as_tag(&tag, repo, id);
7227 if (err)
7228 return err;
7230 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7231 if (err)
7232 goto done;
7234 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7236 switch (got_object_tag_get_object_type(tag)) {
7237 case GOT_OBJ_TYPE_BLOB:
7238 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7239 GOT_OBJ_LABEL_BLOB);
7240 break;
7241 case GOT_OBJ_TYPE_TREE:
7242 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7243 GOT_OBJ_LABEL_TREE);
7244 break;
7245 case GOT_OBJ_TYPE_COMMIT:
7246 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7247 GOT_OBJ_LABEL_COMMIT);
7248 break;
7249 case GOT_OBJ_TYPE_TAG:
7250 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7251 GOT_OBJ_LABEL_TAG);
7252 break;
7253 default:
7254 break;
7257 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7258 got_object_tag_get_name(tag));
7260 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7261 got_object_tag_get_tagger(tag),
7262 got_object_tag_get_tagger_time(tag));
7264 tagmsg = got_object_tag_get_message(tag);
7265 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7266 fprintf(outfile, "%s", tagmsg);
7267 done:
7268 free(id_str);
7269 got_object_tag_close(tag);
7270 return err;
7273 static const struct got_error *
7274 cmd_cat(int argc, char *argv[])
7276 const struct got_error *error;
7277 struct got_repository *repo = NULL;
7278 struct got_worktree *worktree = NULL;
7279 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7280 const char *commit_id_str = NULL;
7281 struct got_object_id *id = NULL, *commit_id = NULL;
7282 int ch, obj_type, i, force_path = 0;
7284 #ifndef PROFILE
7285 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7286 NULL) == -1)
7287 err(1, "pledge");
7288 #endif
7290 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7291 switch (ch) {
7292 case 'c':
7293 commit_id_str = optarg;
7294 break;
7295 case 'r':
7296 repo_path = realpath(optarg, NULL);
7297 if (repo_path == NULL)
7298 return got_error_from_errno2("realpath",
7299 optarg);
7300 got_path_strip_trailing_slashes(repo_path);
7301 break;
7302 case 'P':
7303 force_path = 1;
7304 break;
7305 default:
7306 usage_cat();
7307 /* NOTREACHED */
7311 argc -= optind;
7312 argv += optind;
7314 cwd = getcwd(NULL, 0);
7315 if (cwd == NULL) {
7316 error = got_error_from_errno("getcwd");
7317 goto done;
7319 error = got_worktree_open(&worktree, cwd);
7320 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7321 goto done;
7322 if (worktree) {
7323 if (repo_path == NULL) {
7324 repo_path = strdup(
7325 got_worktree_get_repo_path(worktree));
7326 if (repo_path == NULL) {
7327 error = got_error_from_errno("strdup");
7328 goto done;
7333 if (repo_path == NULL) {
7334 repo_path = getcwd(NULL, 0);
7335 if (repo_path == NULL)
7336 return got_error_from_errno("getcwd");
7339 error = got_repo_open(&repo, repo_path, NULL);
7340 free(repo_path);
7341 if (error != NULL)
7342 goto done;
7344 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7345 if (error)
7346 goto done;
7348 if (commit_id_str == NULL)
7349 commit_id_str = GOT_REF_HEAD;
7350 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7351 if (error)
7352 goto done;
7354 for (i = 0; i < argc; i++) {
7355 if (force_path) {
7356 error = got_object_id_by_path(&id, repo, commit_id,
7357 argv[i]);
7358 if (error)
7359 break;
7360 } else {
7361 error = match_object_id(&id, &label, argv[i],
7362 GOT_OBJ_TYPE_ANY, 0, repo);
7363 if (error) {
7364 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7365 error->code != GOT_ERR_NOT_REF)
7366 break;
7367 error = got_object_id_by_path(&id, repo,
7368 commit_id, argv[i]);
7369 if (error)
7370 break;
7374 error = got_object_get_type(&obj_type, repo, id);
7375 if (error)
7376 break;
7378 switch (obj_type) {
7379 case GOT_OBJ_TYPE_BLOB:
7380 error = cat_blob(id, repo, stdout);
7381 break;
7382 case GOT_OBJ_TYPE_TREE:
7383 error = cat_tree(id, repo, stdout);
7384 break;
7385 case GOT_OBJ_TYPE_COMMIT:
7386 error = cat_commit(id, repo, stdout);
7387 break;
7388 case GOT_OBJ_TYPE_TAG:
7389 error = cat_tag(id, repo, stdout);
7390 break;
7391 default:
7392 error = got_error(GOT_ERR_OBJ_TYPE);
7393 break;
7395 if (error)
7396 break;
7397 free(label);
7398 label = NULL;
7399 free(id);
7400 id = NULL;
7403 done:
7404 free(label);
7405 free(id);
7406 free(commit_id);
7407 if (worktree)
7408 got_worktree_close(worktree);
7409 if (repo) {
7410 const struct got_error *repo_error;
7411 repo_error = got_repo_close(repo);
7412 if (error == NULL)
7413 error = repo_error;
7415 return error;