Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
41 #include <util.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
65 static void
66 catch_sigint(int signo)
67 {
68 sigint_received = 1;
69 }
71 static void
72 catch_sigpipe(int signo)
73 {
74 sigpipe_received = 1;
75 }
78 struct got_cmd {
79 const char *cmd_name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
83 };
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_checkout(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_update(void);
91 __dead static void usage_log(void);
92 __dead static void usage_diff(void);
93 __dead static void usage_blame(void);
94 __dead static void usage_tree(void);
95 __dead static void usage_status(void);
96 __dead static void usage_ref(void);
97 __dead static void usage_branch(void);
98 __dead static void usage_tag(void);
99 __dead static void usage_add(void);
100 __dead static void usage_remove(void);
101 __dead static void usage_revert(void);
102 __dead static void usage_commit(void);
103 __dead static void usage_cherrypick(void);
104 __dead static void usage_backout(void);
105 __dead static void usage_rebase(void);
106 __dead static void usage_histedit(void);
107 __dead static void usage_integrate(void);
108 __dead static void usage_stage(void);
109 __dead static void usage_unstage(void);
110 __dead static void usage_cat(void);
112 static const struct got_error* cmd_init(int, char *[]);
113 static const struct got_error* cmd_import(int, char *[]);
114 static const struct got_error* cmd_clone(int, char *[]);
115 static const struct got_error* cmd_checkout(int, char *[]);
116 static const struct got_error* cmd_update(int, char *[]);
117 static const struct got_error* cmd_log(int, char *[]);
118 static const struct got_error* cmd_diff(int, char *[]);
119 static const struct got_error* cmd_blame(int, char *[]);
120 static const struct got_error* cmd_tree(int, char *[]);
121 static const struct got_error* cmd_status(int, char *[]);
122 static const struct got_error* cmd_ref(int, char *[]);
123 static const struct got_error* cmd_branch(int, char *[]);
124 static const struct got_error* cmd_tag(int, char *[]);
125 static const struct got_error* cmd_add(int, char *[]);
126 static const struct got_error* cmd_remove(int, char *[]);
127 static const struct got_error* cmd_revert(int, char *[]);
128 static const struct got_error* cmd_commit(int, char *[]);
129 static const struct got_error* cmd_cherrypick(int, char *[]);
130 static const struct got_error* cmd_backout(int, char *[]);
131 static const struct got_error* cmd_rebase(int, char *[]);
132 static const struct got_error* cmd_histedit(int, char *[]);
133 static const struct got_error* cmd_integrate(int, char *[]);
134 static const struct got_error* cmd_stage(int, char *[]);
135 static const struct got_error* cmd_unstage(int, char *[]);
136 static const struct got_error* cmd_cat(int, char *[]);
138 static struct got_cmd got_commands[] = {
139 { "init", cmd_init, usage_init, "in" },
140 { "import", cmd_import, usage_import, "im" },
141 { "checkout", cmd_checkout, usage_checkout, "co" },
142 { "clone", cmd_clone, usage_clone, "cl" },
143 { "update", cmd_update, usage_update, "up" },
144 { "log", cmd_log, usage_log, "" },
145 { "diff", cmd_diff, usage_diff, "di" },
146 { "blame", cmd_blame, usage_blame, "bl" },
147 { "tree", cmd_tree, usage_tree, "tr" },
148 { "status", cmd_status, usage_status, "st" },
149 { "ref", cmd_ref, usage_ref, "" },
150 { "branch", cmd_branch, usage_branch, "br" },
151 { "tag", cmd_tag, usage_tag, "" },
152 { "add", cmd_add, usage_add, "" },
153 { "remove", cmd_remove, usage_remove, "rm" },
154 { "revert", cmd_revert, usage_revert, "rv" },
155 { "commit", cmd_commit, usage_commit, "ci" },
156 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 { "backout", cmd_backout, usage_backout, "bo" },
158 { "rebase", cmd_rebase, usage_rebase, "rb" },
159 { "histedit", cmd_histedit, usage_histedit, "he" },
160 { "integrate", cmd_integrate, usage_integrate,"ig" },
161 { "stage", cmd_stage, usage_stage, "sg" },
162 { "unstage", cmd_unstage, usage_unstage, "ug" },
163 { "cat", cmd_cat, usage_cat, "" },
164 };
166 static void
167 list_commands(void)
169 int i;
171 fprintf(stderr, "commands:");
172 for (i = 0; i < nitems(got_commands); i++) {
173 struct got_cmd *cmd = &got_commands[i];
174 fprintf(stderr, " %s", cmd->cmd_name);
176 fputc('\n', stderr);
179 int
180 main(int argc, char *argv[])
182 struct got_cmd *cmd;
183 unsigned int i;
184 int ch;
185 int hflag = 0, Vflag = 0;
186 static struct option longopts[] = {
187 { "version", no_argument, NULL, 'V' },
188 { NULL, 0, NULL, 0}
189 };
191 setlocale(LC_CTYPE, "");
193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 switch (ch) {
195 case 'h':
196 hflag = 1;
197 break;
198 case 'V':
199 Vflag = 1;
200 break;
201 default:
202 usage(hflag);
203 /* NOTREACHED */
207 argc -= optind;
208 argv += optind;
209 optind = 0;
211 if (Vflag) {
212 got_version_print_str();
213 return 1;
216 if (argc <= 0)
217 usage(hflag);
219 signal(SIGINT, catch_sigint);
220 signal(SIGPIPE, catch_sigpipe);
222 for (i = 0; i < nitems(got_commands); i++) {
223 const struct got_error *error;
225 cmd = &got_commands[i];
227 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 strcmp(cmd->cmd_alias, argv[0]) != 0)
229 continue;
231 if (hflag)
232 got_commands[i].cmd_usage();
234 error = got_commands[i].cmd_main(argc, argv);
235 if (error && error->code != GOT_ERR_CANCELLED &&
236 error->code != GOT_ERR_PRIVSEP_EXIT &&
237 !(sigpipe_received &&
238 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 !(sigint_received &&
240 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 return 1;
245 return 0;
248 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 list_commands();
250 return 1;
253 __dead static void
254 usage(int hflag)
256 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 getprogname());
258 if (hflag)
259 list_commands();
260 exit(1);
263 static const struct got_error *
264 get_editor(char **abspath)
266 const struct got_error *err = NULL;
267 const char *editor;
269 *abspath = NULL;
271 editor = getenv("VISUAL");
272 if (editor == NULL)
273 editor = getenv("EDITOR");
275 if (editor) {
276 err = got_path_find_prog(abspath, editor);
277 if (err)
278 return err;
281 if (*abspath == NULL) {
282 *abspath = strdup("/bin/ed");
283 if (*abspath == NULL)
284 return got_error_from_errno("strdup");
287 return NULL;
290 static const struct got_error *
291 apply_unveil(const char *repo_path, int repo_read_only,
292 const char *worktree_path)
294 const struct got_error *err;
296 #ifdef PROFILE
297 if (unveil("gmon.out", "rwc") != 0)
298 return got_error_from_errno2("unveil", "gmon.out");
299 #endif
300 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 return got_error_from_errno2("unveil", worktree_path);
306 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 err = got_privsep_unveil_exec_helpers();
310 if (err != NULL)
311 return err;
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
316 return NULL;
319 __dead static void
320 usage_init(void)
322 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
323 exit(1);
326 static const struct got_error *
327 cmd_init(int argc, char *argv[])
329 const struct got_error *error = NULL;
330 char *repo_path = NULL;
331 int ch;
333 while ((ch = getopt(argc, argv, "")) != -1) {
334 switch (ch) {
335 default:
336 usage_init();
337 /* NOTREACHED */
341 argc -= optind;
342 argv += optind;
344 #ifndef PROFILE
345 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 err(1, "pledge");
347 #endif
348 if (argc != 1)
349 usage_init();
351 repo_path = strdup(argv[0]);
352 if (repo_path == NULL)
353 return got_error_from_errno("strdup");
355 got_path_strip_trailing_slashes(repo_path);
357 error = got_path_mkdir(repo_path);
358 if (error &&
359 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
360 goto done;
362 error = apply_unveil(repo_path, 0, NULL);
363 if (error)
364 goto done;
366 error = got_repo_init(repo_path);
367 done:
368 free(repo_path);
369 return error;
372 __dead static void
373 usage_import(void)
375 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 "[-r repository-path] [-I pattern] path\n", getprogname());
377 exit(1);
380 int
381 spawn_editor(const char *editor, const char *file)
383 pid_t pid;
384 sig_t sighup, sigint, sigquit;
385 int st = -1;
387 sighup = signal(SIGHUP, SIG_IGN);
388 sigint = signal(SIGINT, SIG_IGN);
389 sigquit = signal(SIGQUIT, SIG_IGN);
391 switch (pid = fork()) {
392 case -1:
393 goto doneediting;
394 case 0:
395 execl(editor, editor, file, (char *)NULL);
396 _exit(127);
399 while (waitpid(pid, &st, 0) == -1)
400 if (errno != EINTR)
401 break;
403 doneediting:
404 (void)signal(SIGHUP, sighup);
405 (void)signal(SIGINT, sigint);
406 (void)signal(SIGQUIT, sigquit);
408 if (!WIFEXITED(st)) {
409 errno = EINTR;
410 return -1;
413 return WEXITSTATUS(st);
416 static const struct got_error *
417 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 const char *initial_content)
420 const struct got_error *err = NULL;
421 char buf[1024];
422 struct stat st, st2;
423 FILE *fp;
424 int content_changed = 0;
425 size_t len;
427 *logmsg = NULL;
429 if (stat(logmsg_path, &st) == -1)
430 return got_error_from_errno2("stat", logmsg_path);
432 if (spawn_editor(editor, logmsg_path) == -1)
433 return got_error_from_errno("failed spawning editor");
435 if (stat(logmsg_path, &st2) == -1)
436 return got_error_from_errno("stat");
438 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 "no changes made to commit message, aborting");
442 *logmsg = malloc(st2.st_size + 1);
443 if (*logmsg == NULL)
444 return got_error_from_errno("malloc");
445 (*logmsg)[0] = '\0';
446 len = 0;
448 fp = fopen(logmsg_path, "r");
449 if (fp == NULL) {
450 err = got_error_from_errno("fopen");
451 goto done;
453 while (fgets(buf, sizeof(buf), fp) != NULL) {
454 if (!content_changed && strcmp(buf, initial_content) != 0)
455 content_changed = 1;
456 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 continue; /* remove comments and leading empty lines */
458 len = strlcat(*logmsg, buf, st2.st_size);
460 fclose(fp);
462 while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 (*logmsg)[len - 1] = '\0';
464 len--;
467 if (len == 0 || !content_changed)
468 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "commit message cannot be empty, aborting");
470 done:
471 if (err) {
472 free(*logmsg);
473 *logmsg = NULL;
475 return err;
478 static const struct got_error *
479 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 const char *path_dir, const char *branch_name)
482 char *initial_content = NULL;
483 const struct got_error *err = NULL;
484 int fd;
486 if (asprintf(&initial_content,
487 "\n# %s to be imported to branch %s\n", path_dir,
488 branch_name) == -1)
489 return got_error_from_errno("asprintf");
491 err = got_opentemp_named_fd(logmsg_path, &fd,
492 GOT_TMPDIR_STR "/got-importmsg");
493 if (err)
494 goto done;
496 dprintf(fd, initial_content);
497 close(fd);
499 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 done:
501 free(initial_content);
502 return err;
505 static const struct got_error *
506 import_progress(void *arg, const char *path)
508 printf("A %s\n", path);
509 return NULL;
512 static const struct got_error *
513 get_author(char **author, struct got_repository *repo)
515 const struct got_error *err = NULL;
516 const char *got_author, *name, *email;
518 *author = NULL;
520 name = got_repo_get_gitconfig_author_name(repo);
521 email = got_repo_get_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
528 got_author = getenv("GOT_AUTHOR");
529 if (got_author == NULL) {
530 name = got_repo_get_global_gitconfig_author_name(repo);
531 email = got_repo_get_global_gitconfig_author_email(repo);
532 if (name && email) {
533 if (asprintf(author, "%s <%s>", name, email) == -1)
534 return got_error_from_errno("asprintf");
535 return NULL;
537 /* TODO: Look up user in password database? */
538 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
541 *author = strdup(got_author);
542 if (*author == NULL)
543 return got_error_from_errno("strdup");
545 /*
546 * Really dumb email address check; we're only doing this to
547 * avoid git's object parser breaking on commits we create.
548 */
549 while (*got_author && *got_author != '<')
550 got_author++;
551 if (*got_author != '<') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 goto done;
555 while (*got_author && *got_author != '@')
556 got_author++;
557 if (*got_author != '@') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 goto done;
561 while (*got_author && *got_author != '>')
562 got_author++;
563 if (*got_author != '>')
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 done:
566 if (err) {
567 free(*author);
568 *author = NULL;
570 return err;
573 static const struct got_error *
574 get_gitconfig_path(char **gitconfig_path)
576 const char *homedir = getenv("HOME");
578 *gitconfig_path = NULL;
579 if (homedir) {
580 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 return got_error_from_errno("asprintf");
584 return NULL;
587 static const struct got_error *
588 cmd_import(int argc, char *argv[])
590 const struct got_error *error = NULL;
591 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 const char *branch_name = "main";
594 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 struct got_repository *repo = NULL;
596 struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 struct got_object_id *new_commit_id = NULL;
598 int ch;
599 struct got_pathlist_head ignores;
600 struct got_pathlist_entry *pe;
601 int preserve_logmsg = 0;
603 TAILQ_INIT(&ignores);
605 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 switch (ch) {
607 case 'b':
608 branch_name = optarg;
609 break;
610 case 'm':
611 logmsg = strdup(optarg);
612 if (logmsg == NULL) {
613 error = got_error_from_errno("strdup");
614 goto done;
616 break;
617 case 'r':
618 repo_path = realpath(optarg, NULL);
619 if (repo_path == NULL) {
620 error = got_error_from_errno2("realpath",
621 optarg);
622 goto done;
624 break;
625 case 'I':
626 if (optarg[0] == '\0')
627 break;
628 error = got_pathlist_insert(&pe, &ignores, optarg,
629 NULL);
630 if (error)
631 goto done;
632 break;
633 default:
634 usage_import();
635 /* NOTREACHED */
639 argc -= optind;
640 argv += optind;
642 #ifndef PROFILE
643 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 "unveil",
645 NULL) == -1)
646 err(1, "pledge");
647 #endif
648 if (argc != 1)
649 usage_import();
651 if (repo_path == NULL) {
652 repo_path = getcwd(NULL, 0);
653 if (repo_path == NULL)
654 return got_error_from_errno("getcwd");
656 got_path_strip_trailing_slashes(repo_path);
657 error = get_gitconfig_path(&gitconfig_path);
658 if (error)
659 goto done;
660 error = got_repo_open(&repo, repo_path, gitconfig_path);
661 if (error)
662 goto done;
664 error = get_author(&author, repo);
665 if (error)
666 return error;
668 /*
669 * Don't let the user create a branch name with a leading '-'.
670 * While technically a valid reference name, this case is usually
671 * an unintended typo.
672 */
673 if (branch_name[0] == '-')
674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 error = got_error_from_errno("asprintf");
678 goto done;
681 error = got_ref_open(&branch_ref, repo, refname, 0);
682 if (error) {
683 if (error->code != GOT_ERR_NOT_REF)
684 goto done;
685 } else {
686 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 "import target branch already exists");
688 goto done;
691 path_dir = realpath(argv[0], NULL);
692 if (path_dir == NULL) {
693 error = got_error_from_errno2("realpath", argv[0]);
694 goto done;
696 got_path_strip_trailing_slashes(path_dir);
698 /*
699 * unveil(2) traverses exec(2); if an editor is used we have
700 * to apply unveil after the log message has been written.
701 */
702 if (logmsg == NULL || strlen(logmsg) == 0) {
703 error = get_editor(&editor);
704 if (error)
705 goto done;
706 free(logmsg);
707 error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 path_dir, refname);
709 if (error) {
710 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 logmsg_path != NULL)
712 preserve_logmsg = 1;
713 goto done;
717 if (unveil(path_dir, "r") != 0) {
718 error = got_error_from_errno2("unveil", path_dir);
719 if (logmsg_path)
720 preserve_logmsg = 1;
721 goto done;
724 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 if (error) {
726 if (logmsg_path)
727 preserve_logmsg = 1;
728 goto done;
731 error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 author, &ignores, repo, import_progress, NULL);
733 if (error) {
734 if (logmsg_path)
735 preserve_logmsg = 1;
736 goto done;
739 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 if (error) {
741 if (logmsg_path)
742 preserve_logmsg = 1;
743 goto done;
746 error = got_ref_write(branch_ref, repo);
747 if (error) {
748 if (logmsg_path)
749 preserve_logmsg = 1;
750 goto done;
753 error = got_object_id_str(&id_str, new_commit_id);
754 if (error) {
755 if (logmsg_path)
756 preserve_logmsg = 1;
757 goto done;
760 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 if (error) {
762 if (error->code != GOT_ERR_NOT_REF) {
763 if (logmsg_path)
764 preserve_logmsg = 1;
765 goto done;
768 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 branch_ref);
770 if (error) {
771 if (logmsg_path)
772 preserve_logmsg = 1;
773 goto done;
776 error = got_ref_write(head_ref, repo);
777 if (error) {
778 if (logmsg_path)
779 preserve_logmsg = 1;
780 goto done;
784 printf("Created branch %s with commit %s\n",
785 got_ref_get_name(branch_ref), id_str);
786 done:
787 if (preserve_logmsg) {
788 fprintf(stderr, "%s: log message preserved in %s\n",
789 getprogname(), logmsg_path);
790 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 error = got_error_from_errno2("unlink", logmsg_path);
792 free(logmsg);
793 free(logmsg_path);
794 free(repo_path);
795 free(editor);
796 free(refname);
797 free(new_commit_id);
798 free(id_str);
799 free(author);
800 free(gitconfig_path);
801 if (branch_ref)
802 got_ref_close(branch_ref);
803 if (head_ref)
804 got_ref_close(head_ref);
805 return error;
808 __dead static void
809 usage_clone(void)
811 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
812 exit(1);
815 __dead static void
816 usage_checkout(void)
818 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
819 "[-p prefix] repository-path [worktree-path]\n", getprogname());
820 exit(1);
823 static void
824 show_worktree_base_ref_warning(void)
826 fprintf(stderr, "%s: warning: could not create a reference "
827 "to the work tree's base commit; the commit could be "
828 "garbage-collected by Git; making the repository "
829 "writable and running 'got update' will prevent this\n",
830 getprogname());
833 struct got_checkout_progress_arg {
834 const char *worktree_path;
835 int had_base_commit_ref_error;
836 };
838 static const struct got_error *
839 checkout_progress(void *arg, unsigned char status, const char *path)
841 struct got_checkout_progress_arg *a = arg;
843 /* Base commit bump happens silently. */
844 if (status == GOT_STATUS_BUMP_BASE)
845 return NULL;
847 if (status == GOT_STATUS_BASE_REF_ERR) {
848 a->had_base_commit_ref_error = 1;
849 return NULL;
852 while (path[0] == '/')
853 path++;
855 printf("%c %s/%s\n", status, a->worktree_path, path);
856 return NULL;
859 static const struct got_error *
860 check_cancelled(void *arg)
862 if (sigint_received || sigpipe_received)
863 return got_error(GOT_ERR_CANCELLED);
864 return NULL;
867 static const struct got_error *
868 check_linear_ancestry(struct got_object_id *commit_id,
869 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
870 struct got_repository *repo)
872 const struct got_error *err = NULL;
873 struct got_object_id *yca_id;
875 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
876 commit_id, base_commit_id, repo, check_cancelled, NULL);
877 if (err)
878 return err;
880 if (yca_id == NULL)
881 return got_error(GOT_ERR_ANCESTRY);
883 /*
884 * Require a straight line of history between the target commit
885 * and the work tree's base commit.
887 * Non-linear situations such as this require a rebase:
889 * (commit) D F (base_commit)
890 * \ /
891 * C E
892 * \ /
893 * B (yca)
894 * |
895 * A
897 * 'got update' only handles linear cases:
898 * Update forwards in time: A (base/yca) - B - C - D (commit)
899 * Update backwards in time: D (base) - C - B - A (commit/yca)
900 */
901 if (allow_forwards_in_time_only) {
902 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
903 return got_error(GOT_ERR_ANCESTRY);
904 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
905 got_object_id_cmp(base_commit_id, yca_id) != 0)
906 return got_error(GOT_ERR_ANCESTRY);
908 free(yca_id);
909 return NULL;
912 static const struct got_error *
913 check_same_branch(struct got_object_id *commit_id,
914 struct got_reference *head_ref, struct got_object_id *yca_id,
915 struct got_repository *repo)
917 const struct got_error *err = NULL;
918 struct got_commit_graph *graph = NULL;
919 struct got_object_id *head_commit_id = NULL;
920 int is_same_branch = 0;
922 err = got_ref_resolve(&head_commit_id, repo, head_ref);
923 if (err)
924 goto done;
926 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
927 is_same_branch = 1;
928 goto done;
930 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
931 is_same_branch = 1;
932 goto done;
935 err = got_commit_graph_open(&graph, "/", 1);
936 if (err)
937 goto done;
939 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
940 check_cancelled, NULL);
941 if (err)
942 goto done;
944 for (;;) {
945 struct got_object_id *id;
946 err = got_commit_graph_iter_next(&id, graph, repo,
947 check_cancelled, NULL);
948 if (err) {
949 if (err->code == GOT_ERR_ITER_COMPLETED)
950 err = NULL;
951 break;
954 if (id) {
955 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 break;
957 if (got_object_id_cmp(id, commit_id) == 0) {
958 is_same_branch = 1;
959 break;
963 done:
964 if (graph)
965 got_commit_graph_close(graph);
966 free(head_commit_id);
967 if (!err && !is_same_branch)
968 err = got_error(GOT_ERR_ANCESTRY);
969 return err;
972 static const struct got_error *
973 fetch_progress(void *arg, const char *message, off_t packfile_size,
974 int nobjects_total, int nobjects_indexed)
976 int *did_something = arg;
977 char scaled[FMT_SCALED_STRSIZE];
979 if (message) {
980 printf("\rserver: %s", message);
981 *did_something = 1;
982 } else if (packfile_size > 0 || nobjects_indexed > 0) {
983 printf("\rfetching...");
984 if (fmt_scaled(packfile_size, scaled) == 0)
985 printf(" %*s", FMT_SCALED_STRSIZE, scaled);
986 if (nobjects_indexed > 0)
987 printf(" indexed %d/%d objects", nobjects_indexed,
988 nobjects_total);
989 *did_something = 1;
991 fflush(stdout);
992 return NULL;
995 static const struct got_error *
996 cmd_clone(int argc, char *argv[])
998 const struct got_error *err = NULL;
999 const char *uri, *branch_filter, *dirname;
1000 char *proto, *host, *port, *repo_name, *server_path;
1001 char *default_destdir = NULL, *id_str = NULL;
1002 const char *repo_path;
1003 struct got_repository *repo = NULL;
1004 struct got_pathlist_head refs, symrefs;
1005 struct got_pathlist_entry *pe;
1006 struct got_object_id *pack_hash = NULL;
1007 int ch, fetchfd = -1;
1008 int did_something = 0;
1010 TAILQ_INIT(&refs);
1011 TAILQ_INIT(&symrefs);
1013 while ((ch = getopt(argc, argv, "b:")) != -1) {
1014 switch (ch) {
1015 case 'b':
1016 branch_filter = optarg;
1017 break;
1018 default:
1019 usage_clone();
1020 break;
1023 argc -= optind;
1024 argv += optind;
1025 uri = argv[0];
1026 if(argc == 1)
1027 dirname = NULL;
1028 else if(argc == 2)
1029 dirname = argv[1];
1030 else
1031 usage_clone();
1033 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1034 &repo_name, argv[0]);
1035 if (err)
1036 goto done;
1038 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1039 if (err)
1040 goto done;
1042 if (dirname == NULL) {
1043 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1044 err = got_error_from_errno("asprintf");
1045 goto done;
1047 repo_path = default_destdir;
1048 } else
1049 repo_path = dirname;
1051 err = got_path_mkdir(repo_path);
1052 if (err)
1053 goto done;
1055 err = got_repo_init(repo_path);
1056 if (err != NULL)
1057 goto done;
1059 err = got_repo_open(&repo, repo_path, NULL);
1060 if (err)
1061 goto done;
1063 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1064 repo, fetch_progress, &did_something);
1065 if (err)
1066 goto done;
1067 if (did_something)
1068 printf("\n");
1070 err = got_object_id_str(&id_str, pack_hash);
1071 if (err)
1072 goto done;
1073 printf("Fetched %s.pack\n", id_str);
1074 free(id_str);
1076 /* Set up references provided with the pack file. */
1077 TAILQ_FOREACH(pe, &refs, entry) {
1078 const char *refname = pe->path;
1079 struct got_object_id *id = pe->data;
1080 struct got_reference *ref;
1082 err = got_object_id_str(&id_str, id);
1083 if (err)
1084 goto done;
1086 err = got_ref_alloc(&ref, refname, id);
1087 if (err) {
1088 free(id_str);
1089 goto done;
1092 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1093 free(id_str);
1094 err = got_ref_write(ref, repo);
1095 got_ref_close(ref);
1096 if (err)
1097 goto done;
1100 /* Set the HEAD reference if the server provided one. */
1101 TAILQ_FOREACH(pe, &symrefs, entry) {
1102 struct got_reference *symref, *target_ref;
1103 const char *refname = pe->path;
1104 const char *target = pe->data;
1106 if (strcmp(refname, GOT_REF_HEAD) != 0)
1107 continue;
1109 err = got_ref_open(&target_ref, repo, target, 0);
1110 if (err) {
1111 if (err->code == GOT_ERR_NOT_REF)
1112 continue;
1113 goto done;
1116 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1117 got_ref_close(target_ref);
1118 if (err)
1119 goto done;
1121 printf("Setting %s to %s\n", GOT_REF_HEAD,
1122 got_ref_get_symref_target(symref));
1124 err = got_ref_write(symref, repo);
1125 got_ref_close(symref);
1126 break;
1129 done:
1130 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1131 err = got_error_from_errno("close");
1132 if (repo)
1133 got_repo_close(repo);
1134 TAILQ_FOREACH(pe, &refs, entry) {
1135 free((void *)pe->path);
1136 free(pe->data);
1138 got_pathlist_free(&refs);
1139 TAILQ_FOREACH(pe, &symrefs, entry) {
1140 free((void *)pe->path);
1141 free(pe->data);
1143 got_pathlist_free(&symrefs);
1144 free(pack_hash);
1145 free(proto);
1146 free(host);
1147 free(port);
1148 free(server_path);
1149 free(repo_name);
1150 free(default_destdir);
1151 return err;
1154 static const struct got_error *
1155 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1157 static char msg[512];
1158 const char *branch_name;
1160 if (got_ref_is_symbolic(ref))
1161 branch_name = got_ref_get_symref_target(ref);
1162 else
1163 branch_name = got_ref_get_name(ref);
1165 if (strncmp("refs/heads/", branch_name, 11) == 0)
1166 branch_name += 11;
1168 snprintf(msg, sizeof(msg),
1169 "target commit is not contained in branch '%s'; "
1170 "the branch to use must be specified with -b; "
1171 "if necessary a new branch can be created for "
1172 "this commit with 'got branch -c %s BRANCH_NAME'",
1173 branch_name, commit_id_str);
1175 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1178 static const struct got_error *
1179 cmd_checkout(int argc, char *argv[])
1181 const struct got_error *error = NULL;
1182 struct got_repository *repo = NULL;
1183 struct got_reference *head_ref = NULL;
1184 struct got_worktree *worktree = NULL;
1185 char *repo_path = NULL;
1186 char *worktree_path = NULL;
1187 const char *path_prefix = "";
1188 const char *branch_name = GOT_REF_HEAD;
1189 char *commit_id_str = NULL;
1190 int ch, same_path_prefix, allow_nonempty = 0;
1191 struct got_pathlist_head paths;
1192 struct got_checkout_progress_arg cpa;
1194 TAILQ_INIT(&paths);
1196 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1197 switch (ch) {
1198 case 'b':
1199 branch_name = optarg;
1200 break;
1201 case 'c':
1202 commit_id_str = strdup(optarg);
1203 if (commit_id_str == NULL)
1204 return got_error_from_errno("strdup");
1205 break;
1206 case 'E':
1207 allow_nonempty = 1;
1208 break;
1209 case 'p':
1210 path_prefix = optarg;
1211 break;
1212 default:
1213 usage_checkout();
1214 /* NOTREACHED */
1218 argc -= optind;
1219 argv += optind;
1221 #ifndef PROFILE
1222 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1223 "unveil", NULL) == -1)
1224 err(1, "pledge");
1225 #endif
1226 if (argc == 1) {
1227 char *cwd, *base, *dotgit;
1228 repo_path = realpath(argv[0], NULL);
1229 if (repo_path == NULL)
1230 return got_error_from_errno2("realpath", argv[0]);
1231 cwd = getcwd(NULL, 0);
1232 if (cwd == NULL) {
1233 error = got_error_from_errno("getcwd");
1234 goto done;
1236 if (path_prefix[0]) {
1237 base = basename(path_prefix);
1238 if (base == NULL) {
1239 error = got_error_from_errno2("basename",
1240 path_prefix);
1241 goto done;
1243 } else {
1244 base = basename(repo_path);
1245 if (base == NULL) {
1246 error = got_error_from_errno2("basename",
1247 repo_path);
1248 goto done;
1251 dotgit = strstr(base, ".git");
1252 if (dotgit)
1253 *dotgit = '\0';
1254 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1255 error = got_error_from_errno("asprintf");
1256 free(cwd);
1257 goto done;
1259 free(cwd);
1260 } else if (argc == 2) {
1261 repo_path = realpath(argv[0], NULL);
1262 if (repo_path == NULL) {
1263 error = got_error_from_errno2("realpath", argv[0]);
1264 goto done;
1266 worktree_path = realpath(argv[1], NULL);
1267 if (worktree_path == NULL) {
1268 if (errno != ENOENT) {
1269 error = got_error_from_errno2("realpath",
1270 argv[1]);
1271 goto done;
1273 worktree_path = strdup(argv[1]);
1274 if (worktree_path == NULL) {
1275 error = got_error_from_errno("strdup");
1276 goto done;
1279 } else
1280 usage_checkout();
1282 got_path_strip_trailing_slashes(repo_path);
1283 got_path_strip_trailing_slashes(worktree_path);
1285 error = got_repo_open(&repo, repo_path, NULL);
1286 if (error != NULL)
1287 goto done;
1289 /* Pre-create work tree path for unveil(2) */
1290 error = got_path_mkdir(worktree_path);
1291 if (error) {
1292 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1293 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1294 goto done;
1295 if (!allow_nonempty &&
1296 !got_path_dir_is_empty(worktree_path)) {
1297 error = got_error_path(worktree_path,
1298 GOT_ERR_DIR_NOT_EMPTY);
1299 goto done;
1303 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1304 if (error)
1305 goto done;
1307 error = got_ref_open(&head_ref, repo, branch_name, 0);
1308 if (error != NULL)
1309 goto done;
1311 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1312 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1313 goto done;
1315 error = got_worktree_open(&worktree, worktree_path);
1316 if (error != NULL)
1317 goto done;
1319 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1320 path_prefix);
1321 if (error != NULL)
1322 goto done;
1323 if (!same_path_prefix) {
1324 error = got_error(GOT_ERR_PATH_PREFIX);
1325 goto done;
1328 if (commit_id_str) {
1329 struct got_object_id *commit_id;
1330 error = got_repo_match_object_id(&commit_id, NULL,
1331 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1332 if (error)
1333 goto done;
1334 error = check_linear_ancestry(commit_id,
1335 got_worktree_get_base_commit_id(worktree), 0, repo);
1336 if (error != NULL) {
1337 free(commit_id);
1338 if (error->code == GOT_ERR_ANCESTRY) {
1339 error = checkout_ancestry_error(
1340 head_ref, commit_id_str);
1342 goto done;
1344 error = check_same_branch(commit_id, head_ref, NULL, repo);
1345 if (error) {
1346 if (error->code == GOT_ERR_ANCESTRY) {
1347 error = checkout_ancestry_error(
1348 head_ref, commit_id_str);
1350 goto done;
1352 error = got_worktree_set_base_commit_id(worktree, repo,
1353 commit_id);
1354 free(commit_id);
1355 if (error)
1356 goto done;
1359 error = got_pathlist_append(&paths, "", NULL);
1360 if (error)
1361 goto done;
1362 cpa.worktree_path = worktree_path;
1363 cpa.had_base_commit_ref_error = 0;
1364 error = got_worktree_checkout_files(worktree, &paths, repo,
1365 checkout_progress, &cpa, check_cancelled, NULL);
1366 if (error != NULL)
1367 goto done;
1369 printf("Now shut up and hack\n");
1370 if (cpa.had_base_commit_ref_error)
1371 show_worktree_base_ref_warning();
1372 done:
1373 got_pathlist_free(&paths);
1374 free(commit_id_str);
1375 free(repo_path);
1376 free(worktree_path);
1377 return error;
1380 __dead static void
1381 usage_update(void)
1383 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1384 getprogname());
1385 exit(1);
1388 static const struct got_error *
1389 update_progress(void *arg, unsigned char status, const char *path)
1391 int *did_something = arg;
1393 if (status == GOT_STATUS_EXISTS ||
1394 status == GOT_STATUS_BASE_REF_ERR)
1395 return NULL;
1397 *did_something = 1;
1399 /* Base commit bump happens silently. */
1400 if (status == GOT_STATUS_BUMP_BASE)
1401 return NULL;
1403 while (path[0] == '/')
1404 path++;
1405 printf("%c %s\n", status, path);
1406 return NULL;
1409 static const struct got_error *
1410 switch_head_ref(struct got_reference *head_ref,
1411 struct got_object_id *commit_id, struct got_worktree *worktree,
1412 struct got_repository *repo)
1414 const struct got_error *err = NULL;
1415 char *base_id_str;
1416 int ref_has_moved = 0;
1418 /* Trivial case: switching between two different references. */
1419 if (strcmp(got_ref_get_name(head_ref),
1420 got_worktree_get_head_ref_name(worktree)) != 0) {
1421 printf("Switching work tree from %s to %s\n",
1422 got_worktree_get_head_ref_name(worktree),
1423 got_ref_get_name(head_ref));
1424 return got_worktree_set_head_ref(worktree, head_ref);
1427 err = check_linear_ancestry(commit_id,
1428 got_worktree_get_base_commit_id(worktree), 0, repo);
1429 if (err) {
1430 if (err->code != GOT_ERR_ANCESTRY)
1431 return err;
1432 ref_has_moved = 1;
1434 if (!ref_has_moved)
1435 return NULL;
1437 /* Switching to a rebased branch with the same reference name. */
1438 err = got_object_id_str(&base_id_str,
1439 got_worktree_get_base_commit_id(worktree));
1440 if (err)
1441 return err;
1442 printf("Reference %s now points at a different branch\n",
1443 got_worktree_get_head_ref_name(worktree));
1444 printf("Switching work tree from %s to %s\n", base_id_str,
1445 got_worktree_get_head_ref_name(worktree));
1446 return NULL;
1449 static const struct got_error *
1450 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1452 const struct got_error *err;
1453 int in_progress;
1455 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1456 if (err)
1457 return err;
1458 if (in_progress)
1459 return got_error(GOT_ERR_REBASING);
1461 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1462 if (err)
1463 return err;
1464 if (in_progress)
1465 return got_error(GOT_ERR_HISTEDIT_BUSY);
1467 return NULL;
1470 static const struct got_error *
1471 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1472 char *argv[], struct got_worktree *worktree)
1474 const struct got_error *err = NULL;
1475 char *path;
1476 int i;
1478 if (argc == 0) {
1479 path = strdup("");
1480 if (path == NULL)
1481 return got_error_from_errno("strdup");
1482 return got_pathlist_append(paths, path, NULL);
1485 for (i = 0; i < argc; i++) {
1486 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1487 if (err)
1488 break;
1489 err = got_pathlist_append(paths, path, NULL);
1490 if (err) {
1491 free(path);
1492 break;
1496 return err;
1499 static const struct got_error *
1500 cmd_update(int argc, char *argv[])
1502 const struct got_error *error = NULL;
1503 struct got_repository *repo = NULL;
1504 struct got_worktree *worktree = NULL;
1505 char *worktree_path = NULL;
1506 struct got_object_id *commit_id = NULL;
1507 char *commit_id_str = NULL;
1508 const char *branch_name = NULL;
1509 struct got_reference *head_ref = NULL;
1510 struct got_pathlist_head paths;
1511 struct got_pathlist_entry *pe;
1512 int ch, did_something = 0;
1514 TAILQ_INIT(&paths);
1516 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1517 switch (ch) {
1518 case 'b':
1519 branch_name = optarg;
1520 break;
1521 case 'c':
1522 commit_id_str = strdup(optarg);
1523 if (commit_id_str == NULL)
1524 return got_error_from_errno("strdup");
1525 break;
1526 default:
1527 usage_update();
1528 /* NOTREACHED */
1532 argc -= optind;
1533 argv += optind;
1535 #ifndef PROFILE
1536 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1537 "unveil", NULL) == -1)
1538 err(1, "pledge");
1539 #endif
1540 worktree_path = getcwd(NULL, 0);
1541 if (worktree_path == NULL) {
1542 error = got_error_from_errno("getcwd");
1543 goto done;
1545 error = got_worktree_open(&worktree, worktree_path);
1546 if (error)
1547 goto done;
1549 error = check_rebase_or_histedit_in_progress(worktree);
1550 if (error)
1551 goto done;
1553 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1554 NULL);
1555 if (error != NULL)
1556 goto done;
1558 error = apply_unveil(got_repo_get_path(repo), 0,
1559 got_worktree_get_root_path(worktree));
1560 if (error)
1561 goto done;
1563 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1564 if (error)
1565 goto done;
1567 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1568 got_worktree_get_head_ref_name(worktree), 0);
1569 if (error != NULL)
1570 goto done;
1571 if (commit_id_str == NULL) {
1572 error = got_ref_resolve(&commit_id, repo, head_ref);
1573 if (error != NULL)
1574 goto done;
1575 error = got_object_id_str(&commit_id_str, commit_id);
1576 if (error != NULL)
1577 goto done;
1578 } else {
1579 error = got_repo_match_object_id(&commit_id, NULL,
1580 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1581 free(commit_id_str);
1582 commit_id_str = NULL;
1583 if (error)
1584 goto done;
1585 error = got_object_id_str(&commit_id_str, commit_id);
1586 if (error)
1587 goto done;
1590 if (branch_name) {
1591 struct got_object_id *head_commit_id;
1592 TAILQ_FOREACH(pe, &paths, entry) {
1593 if (pe->path_len == 0)
1594 continue;
1595 error = got_error_msg(GOT_ERR_BAD_PATH,
1596 "switching between branches requires that "
1597 "the entire work tree gets updated");
1598 goto done;
1600 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1601 if (error)
1602 goto done;
1603 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1604 repo);
1605 free(head_commit_id);
1606 if (error != NULL)
1607 goto done;
1608 error = check_same_branch(commit_id, head_ref, NULL, repo);
1609 if (error)
1610 goto done;
1611 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1612 if (error)
1613 goto done;
1614 } else {
1615 error = check_linear_ancestry(commit_id,
1616 got_worktree_get_base_commit_id(worktree), 0, repo);
1617 if (error != NULL) {
1618 if (error->code == GOT_ERR_ANCESTRY)
1619 error = got_error(GOT_ERR_BRANCH_MOVED);
1620 goto done;
1622 error = check_same_branch(commit_id, head_ref, NULL, repo);
1623 if (error)
1624 goto done;
1627 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1628 commit_id) != 0) {
1629 error = got_worktree_set_base_commit_id(worktree, repo,
1630 commit_id);
1631 if (error)
1632 goto done;
1635 error = got_worktree_checkout_files(worktree, &paths, repo,
1636 update_progress, &did_something, check_cancelled, NULL);
1637 if (error != NULL)
1638 goto done;
1640 if (did_something)
1641 printf("Updated to commit %s\n", commit_id_str);
1642 else
1643 printf("Already up-to-date\n");
1644 done:
1645 free(worktree_path);
1646 TAILQ_FOREACH(pe, &paths, entry)
1647 free((char *)pe->path);
1648 got_pathlist_free(&paths);
1649 free(commit_id);
1650 free(commit_id_str);
1651 return error;
1654 static const struct got_error *
1655 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1656 const char *path, int diff_context, int ignore_whitespace,
1657 struct got_repository *repo)
1659 const struct got_error *err = NULL;
1660 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1662 if (blob_id1) {
1663 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1664 if (err)
1665 goto done;
1668 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1669 if (err)
1670 goto done;
1672 while (path[0] == '/')
1673 path++;
1674 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1675 ignore_whitespace, stdout);
1676 done:
1677 if (blob1)
1678 got_object_blob_close(blob1);
1679 got_object_blob_close(blob2);
1680 return err;
1683 static const struct got_error *
1684 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1685 const char *path, int diff_context, int ignore_whitespace,
1686 struct got_repository *repo)
1688 const struct got_error *err = NULL;
1689 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1690 struct got_diff_blob_output_unidiff_arg arg;
1692 if (tree_id1) {
1693 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1694 if (err)
1695 goto done;
1698 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1699 if (err)
1700 goto done;
1702 arg.diff_context = diff_context;
1703 arg.ignore_whitespace = ignore_whitespace;
1704 arg.outfile = stdout;
1705 while (path[0] == '/')
1706 path++;
1707 err = got_diff_tree(tree1, tree2, path, path, repo,
1708 got_diff_blob_output_unidiff, &arg, 1);
1709 done:
1710 if (tree1)
1711 got_object_tree_close(tree1);
1712 if (tree2)
1713 got_object_tree_close(tree2);
1714 return err;
1717 static const struct got_error *
1718 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1719 const char *path, int diff_context, struct got_repository *repo)
1721 const struct got_error *err = NULL;
1722 struct got_commit_object *pcommit = NULL;
1723 char *id_str1 = NULL, *id_str2 = NULL;
1724 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1725 struct got_object_qid *qid;
1727 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1728 if (qid != NULL) {
1729 err = got_object_open_as_commit(&pcommit, repo,
1730 qid->id);
1731 if (err)
1732 return err;
1735 if (path && path[0] != '\0') {
1736 int obj_type;
1737 err = got_object_id_by_path(&obj_id2, repo, id, path);
1738 if (err)
1739 goto done;
1740 err = got_object_id_str(&id_str2, obj_id2);
1741 if (err) {
1742 free(obj_id2);
1743 goto done;
1745 if (pcommit) {
1746 err = got_object_id_by_path(&obj_id1, repo,
1747 qid->id, path);
1748 if (err) {
1749 free(obj_id2);
1750 goto done;
1752 err = got_object_id_str(&id_str1, obj_id1);
1753 if (err) {
1754 free(obj_id2);
1755 goto done;
1758 err = got_object_get_type(&obj_type, repo, obj_id2);
1759 if (err) {
1760 free(obj_id2);
1761 goto done;
1763 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1764 switch (obj_type) {
1765 case GOT_OBJ_TYPE_BLOB:
1766 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1767 0, repo);
1768 break;
1769 case GOT_OBJ_TYPE_TREE:
1770 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1771 0, repo);
1772 break;
1773 default:
1774 err = got_error(GOT_ERR_OBJ_TYPE);
1775 break;
1777 free(obj_id1);
1778 free(obj_id2);
1779 } else {
1780 obj_id2 = got_object_commit_get_tree_id(commit);
1781 err = got_object_id_str(&id_str2, obj_id2);
1782 if (err)
1783 goto done;
1784 obj_id1 = got_object_commit_get_tree_id(pcommit);
1785 err = got_object_id_str(&id_str1, obj_id1);
1786 if (err)
1787 goto done;
1788 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1789 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1791 done:
1792 free(id_str1);
1793 free(id_str2);
1794 if (pcommit)
1795 got_object_commit_close(pcommit);
1796 return err;
1799 static char *
1800 get_datestr(time_t *time, char *datebuf)
1802 struct tm mytm, *tm;
1803 char *p, *s;
1805 tm = gmtime_r(time, &mytm);
1806 if (tm == NULL)
1807 return NULL;
1808 s = asctime_r(tm, datebuf);
1809 if (s == NULL)
1810 return NULL;
1811 p = strchr(s, '\n');
1812 if (p)
1813 *p = '\0';
1814 return s;
1817 static const struct got_error *
1818 match_logmsg(int *have_match, struct got_object_id *id,
1819 struct got_commit_object *commit, regex_t *regex)
1821 const struct got_error *err = NULL;
1822 regmatch_t regmatch;
1823 char *id_str = NULL, *logmsg = NULL;
1825 *have_match = 0;
1827 err = got_object_id_str(&id_str, id);
1828 if (err)
1829 return err;
1831 err = got_object_commit_get_logmsg(&logmsg, commit);
1832 if (err)
1833 goto done;
1835 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1836 *have_match = 1;
1837 done:
1838 free(id_str);
1839 free(logmsg);
1840 return err;
1843 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1845 static const struct got_error *
1846 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1847 struct got_repository *repo, const char *path, int show_patch,
1848 int diff_context, struct got_reflist_head *refs)
1850 const struct got_error *err = NULL;
1851 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1852 char datebuf[26];
1853 time_t committer_time;
1854 const char *author, *committer;
1855 char *refs_str = NULL;
1856 struct got_reflist_entry *re;
1858 SIMPLEQ_FOREACH(re, refs, entry) {
1859 char *s;
1860 const char *name;
1861 struct got_tag_object *tag = NULL;
1862 int cmp;
1864 name = got_ref_get_name(re->ref);
1865 if (strcmp(name, GOT_REF_HEAD) == 0)
1866 continue;
1867 if (strncmp(name, "refs/", 5) == 0)
1868 name += 5;
1869 if (strncmp(name, "got/", 4) == 0)
1870 continue;
1871 if (strncmp(name, "heads/", 6) == 0)
1872 name += 6;
1873 if (strncmp(name, "remotes/", 8) == 0)
1874 name += 8;
1875 if (strncmp(name, "tags/", 5) == 0) {
1876 err = got_object_open_as_tag(&tag, repo, re->id);
1877 if (err) {
1878 if (err->code != GOT_ERR_OBJ_TYPE)
1879 return err;
1880 /* Ref points at something other than a tag. */
1881 err = NULL;
1882 tag = NULL;
1885 cmp = got_object_id_cmp(tag ?
1886 got_object_tag_get_object_id(tag) : re->id, id);
1887 if (tag)
1888 got_object_tag_close(tag);
1889 if (cmp != 0)
1890 continue;
1891 s = refs_str;
1892 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1893 name) == -1) {
1894 err = got_error_from_errno("asprintf");
1895 free(s);
1896 return err;
1898 free(s);
1900 err = got_object_id_str(&id_str, id);
1901 if (err)
1902 return err;
1904 printf(GOT_COMMIT_SEP_STR);
1905 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1906 refs_str ? refs_str : "", refs_str ? ")" : "");
1907 free(id_str);
1908 id_str = NULL;
1909 free(refs_str);
1910 refs_str = NULL;
1911 printf("from: %s\n", got_object_commit_get_author(commit));
1912 committer_time = got_object_commit_get_committer_time(commit);
1913 datestr = get_datestr(&committer_time, datebuf);
1914 if (datestr)
1915 printf("date: %s UTC\n", datestr);
1916 author = got_object_commit_get_author(commit);
1917 committer = got_object_commit_get_committer(commit);
1918 if (strcmp(author, committer) != 0)
1919 printf("via: %s\n", committer);
1920 if (got_object_commit_get_nparents(commit) > 1) {
1921 const struct got_object_id_queue *parent_ids;
1922 struct got_object_qid *qid;
1923 int n = 1;
1924 parent_ids = got_object_commit_get_parent_ids(commit);
1925 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1926 err = got_object_id_str(&id_str, qid->id);
1927 if (err)
1928 return err;
1929 printf("parent %d: %s\n", n++, id_str);
1930 free(id_str);
1934 err = got_object_commit_get_logmsg(&logmsg0, commit);
1935 if (err)
1936 return err;
1938 logmsg = logmsg0;
1939 do {
1940 line = strsep(&logmsg, "\n");
1941 if (line)
1942 printf(" %s\n", line);
1943 } while (line);
1944 free(logmsg0);
1946 if (show_patch) {
1947 err = print_patch(commit, id, path, diff_context, repo);
1948 if (err == 0)
1949 printf("\n");
1952 if (fflush(stdout) != 0 && err == NULL)
1953 err = got_error_from_errno("fflush");
1954 return err;
1957 static const struct got_error *
1958 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1959 const char *path, int show_patch, const char *search_pattern,
1960 int diff_context, int limit, int log_branches,
1961 struct got_reflist_head *refs)
1963 const struct got_error *err;
1964 struct got_commit_graph *graph;
1965 regex_t regex;
1966 int have_match;
1968 if (search_pattern &&
1969 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1970 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1972 err = got_commit_graph_open(&graph, path, !log_branches);
1973 if (err)
1974 return err;
1975 err = got_commit_graph_iter_start(graph, root_id, repo,
1976 check_cancelled, NULL);
1977 if (err)
1978 goto done;
1979 for (;;) {
1980 struct got_commit_object *commit;
1981 struct got_object_id *id;
1983 if (sigint_received || sigpipe_received)
1984 break;
1986 err = got_commit_graph_iter_next(&id, graph, repo,
1987 check_cancelled, NULL);
1988 if (err) {
1989 if (err->code == GOT_ERR_ITER_COMPLETED)
1990 err = NULL;
1991 break;
1993 if (id == NULL)
1994 break;
1996 err = got_object_open_as_commit(&commit, repo, id);
1997 if (err)
1998 break;
2000 if (search_pattern) {
2001 err = match_logmsg(&have_match, id, commit, &regex);
2002 if (err) {
2003 got_object_commit_close(commit);
2004 break;
2006 if (have_match == 0) {
2007 got_object_commit_close(commit);
2008 continue;
2012 err = print_commit(commit, id, repo, path, show_patch,
2013 diff_context, refs);
2014 got_object_commit_close(commit);
2015 if (err || (limit && --limit == 0))
2016 break;
2018 done:
2019 if (search_pattern)
2020 regfree(&regex);
2021 got_commit_graph_close(graph);
2022 return err;
2025 __dead static void
2026 usage_log(void)
2028 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2029 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2030 exit(1);
2033 static int
2034 get_default_log_limit(void)
2036 const char *got_default_log_limit;
2037 long long n;
2038 const char *errstr;
2040 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2041 if (got_default_log_limit == NULL)
2042 return 0;
2043 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2044 if (errstr != NULL)
2045 return 0;
2046 return n;
2049 static const struct got_error *
2050 cmd_log(int argc, char *argv[])
2052 const struct got_error *error;
2053 struct got_repository *repo = NULL;
2054 struct got_worktree *worktree = NULL;
2055 struct got_commit_object *commit = NULL;
2056 struct got_object_id *id = NULL;
2057 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2058 const char *start_commit = NULL, *search_pattern = NULL;
2059 int diff_context = -1, ch;
2060 int show_patch = 0, limit = 0, log_branches = 0;
2061 const char *errstr;
2062 struct got_reflist_head refs;
2064 SIMPLEQ_INIT(&refs);
2066 #ifndef PROFILE
2067 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2068 NULL)
2069 == -1)
2070 err(1, "pledge");
2071 #endif
2073 limit = get_default_log_limit();
2075 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2076 switch (ch) {
2077 case 'p':
2078 show_patch = 1;
2079 break;
2080 case 'c':
2081 start_commit = optarg;
2082 break;
2083 case 'C':
2084 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2085 &errstr);
2086 if (errstr != NULL)
2087 err(1, "-C option %s", errstr);
2088 break;
2089 case 'l':
2090 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2091 if (errstr != NULL)
2092 err(1, "-l option %s", errstr);
2093 break;
2094 case 'b':
2095 log_branches = 1;
2096 break;
2097 case 'r':
2098 repo_path = realpath(optarg, NULL);
2099 if (repo_path == NULL)
2100 return got_error_from_errno2("realpath",
2101 optarg);
2102 got_path_strip_trailing_slashes(repo_path);
2103 break;
2104 case 's':
2105 search_pattern = optarg;
2106 break;
2107 default:
2108 usage_log();
2109 /* NOTREACHED */
2113 argc -= optind;
2114 argv += optind;
2116 if (diff_context == -1)
2117 diff_context = 3;
2118 else if (!show_patch)
2119 errx(1, "-C reguires -p");
2121 cwd = getcwd(NULL, 0);
2122 if (cwd == NULL) {
2123 error = got_error_from_errno("getcwd");
2124 goto done;
2127 error = got_worktree_open(&worktree, cwd);
2128 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2129 goto done;
2130 error = NULL;
2132 if (argc == 0) {
2133 path = strdup("");
2134 if (path == NULL) {
2135 error = got_error_from_errno("strdup");
2136 goto done;
2138 } else if (argc == 1) {
2139 if (worktree) {
2140 error = got_worktree_resolve_path(&path, worktree,
2141 argv[0]);
2142 if (error)
2143 goto done;
2144 } else {
2145 path = strdup(argv[0]);
2146 if (path == NULL) {
2147 error = got_error_from_errno("strdup");
2148 goto done;
2151 } else
2152 usage_log();
2154 if (repo_path == NULL) {
2155 repo_path = worktree ?
2156 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2158 if (repo_path == NULL) {
2159 error = got_error_from_errno("strdup");
2160 goto done;
2163 error = got_repo_open(&repo, repo_path, NULL);
2164 if (error != NULL)
2165 goto done;
2167 error = apply_unveil(got_repo_get_path(repo), 1,
2168 worktree ? got_worktree_get_root_path(worktree) : NULL);
2169 if (error)
2170 goto done;
2172 if (start_commit == NULL) {
2173 struct got_reference *head_ref;
2174 error = got_ref_open(&head_ref, repo,
2175 worktree ? got_worktree_get_head_ref_name(worktree)
2176 : GOT_REF_HEAD, 0);
2177 if (error != NULL)
2178 return error;
2179 error = got_ref_resolve(&id, repo, head_ref);
2180 got_ref_close(head_ref);
2181 if (error != NULL)
2182 return error;
2183 error = got_object_open_as_commit(&commit, repo, id);
2184 } else {
2185 struct got_reference *ref;
2186 error = got_ref_open(&ref, repo, start_commit, 0);
2187 if (error == NULL) {
2188 int obj_type;
2189 error = got_ref_resolve(&id, repo, ref);
2190 got_ref_close(ref);
2191 if (error != NULL)
2192 goto done;
2193 error = got_object_get_type(&obj_type, repo, id);
2194 if (error != NULL)
2195 goto done;
2196 if (obj_type == GOT_OBJ_TYPE_TAG) {
2197 struct got_tag_object *tag;
2198 error = got_object_open_as_tag(&tag, repo, id);
2199 if (error != NULL)
2200 goto done;
2201 if (got_object_tag_get_object_type(tag) !=
2202 GOT_OBJ_TYPE_COMMIT) {
2203 got_object_tag_close(tag);
2204 error = got_error(GOT_ERR_OBJ_TYPE);
2205 goto done;
2207 free(id);
2208 id = got_object_id_dup(
2209 got_object_tag_get_object_id(tag));
2210 if (id == NULL)
2211 error = got_error_from_errno(
2212 "got_object_id_dup");
2213 got_object_tag_close(tag);
2214 if (error)
2215 goto done;
2216 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2217 error = got_error(GOT_ERR_OBJ_TYPE);
2218 goto done;
2220 error = got_object_open_as_commit(&commit, repo, id);
2221 if (error != NULL)
2222 goto done;
2224 if (commit == NULL) {
2225 error = got_repo_match_object_id_prefix(&id,
2226 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2227 if (error != NULL)
2228 return error;
2231 if (error != NULL)
2232 goto done;
2234 if (worktree) {
2235 const char *prefix = got_worktree_get_path_prefix(worktree);
2236 char *p;
2237 if (asprintf(&p, "%s%s%s", prefix,
2238 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2239 error = got_error_from_errno("asprintf");
2240 goto done;
2242 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2243 free(p);
2244 } else
2245 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2246 if (error != NULL)
2247 goto done;
2248 if (in_repo_path) {
2249 free(path);
2250 path = in_repo_path;
2253 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2254 if (error)
2255 goto done;
2257 error = print_commits(id, repo, path, show_patch, search_pattern,
2258 diff_context, limit, log_branches, &refs);
2259 done:
2260 free(path);
2261 free(repo_path);
2262 free(cwd);
2263 free(id);
2264 if (worktree)
2265 got_worktree_close(worktree);
2266 if (repo) {
2267 const struct got_error *repo_error;
2268 repo_error = got_repo_close(repo);
2269 if (error == NULL)
2270 error = repo_error;
2272 got_ref_list_free(&refs);
2273 return error;
2276 __dead static void
2277 usage_diff(void)
2279 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2280 "[-w] [object1 object2 | path]\n", getprogname());
2281 exit(1);
2284 struct print_diff_arg {
2285 struct got_repository *repo;
2286 struct got_worktree *worktree;
2287 int diff_context;
2288 const char *id_str;
2289 int header_shown;
2290 int diff_staged;
2291 int ignore_whitespace;
2294 static const struct got_error *
2295 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2296 const char *path, struct got_object_id *blob_id,
2297 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2298 int dirfd, const char *de_name)
2300 struct print_diff_arg *a = arg;
2301 const struct got_error *err = NULL;
2302 struct got_blob_object *blob1 = NULL;
2303 int fd = -1;
2304 FILE *f2 = NULL;
2305 char *abspath = NULL, *label1 = NULL;
2306 struct stat sb;
2308 if (a->diff_staged) {
2309 if (staged_status != GOT_STATUS_MODIFY &&
2310 staged_status != GOT_STATUS_ADD &&
2311 staged_status != GOT_STATUS_DELETE)
2312 return NULL;
2313 } else {
2314 if (staged_status == GOT_STATUS_DELETE)
2315 return NULL;
2316 if (status == GOT_STATUS_NONEXISTENT)
2317 return got_error_set_errno(ENOENT, path);
2318 if (status != GOT_STATUS_MODIFY &&
2319 status != GOT_STATUS_ADD &&
2320 status != GOT_STATUS_DELETE &&
2321 status != GOT_STATUS_CONFLICT)
2322 return NULL;
2325 if (!a->header_shown) {
2326 printf("diff %s %s%s\n", a->id_str,
2327 got_worktree_get_root_path(a->worktree),
2328 a->diff_staged ? " (staged changes)" : "");
2329 a->header_shown = 1;
2332 if (a->diff_staged) {
2333 const char *label1 = NULL, *label2 = NULL;
2334 switch (staged_status) {
2335 case GOT_STATUS_MODIFY:
2336 label1 = path;
2337 label2 = path;
2338 break;
2339 case GOT_STATUS_ADD:
2340 label2 = path;
2341 break;
2342 case GOT_STATUS_DELETE:
2343 label1 = path;
2344 break;
2345 default:
2346 return got_error(GOT_ERR_FILE_STATUS);
2348 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2349 label1, label2, a->diff_context, a->ignore_whitespace,
2350 a->repo, stdout);
2353 if (staged_status == GOT_STATUS_ADD ||
2354 staged_status == GOT_STATUS_MODIFY) {
2355 char *id_str;
2356 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2357 8192);
2358 if (err)
2359 goto done;
2360 err = got_object_id_str(&id_str, staged_blob_id);
2361 if (err)
2362 goto done;
2363 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2364 err = got_error_from_errno("asprintf");
2365 free(id_str);
2366 goto done;
2368 free(id_str);
2369 } else if (status != GOT_STATUS_ADD) {
2370 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2371 if (err)
2372 goto done;
2375 if (status != GOT_STATUS_DELETE) {
2376 if (asprintf(&abspath, "%s/%s",
2377 got_worktree_get_root_path(a->worktree), path) == -1) {
2378 err = got_error_from_errno("asprintf");
2379 goto done;
2382 if (dirfd != -1) {
2383 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2384 if (fd == -1) {
2385 err = got_error_from_errno2("openat", abspath);
2386 goto done;
2388 } else {
2389 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2390 if (fd == -1) {
2391 err = got_error_from_errno2("open", abspath);
2392 goto done;
2395 if (fstat(fd, &sb) == -1) {
2396 err = got_error_from_errno2("fstat", abspath);
2397 goto done;
2399 f2 = fdopen(fd, "r");
2400 if (f2 == NULL) {
2401 err = got_error_from_errno2("fdopen", abspath);
2402 goto done;
2404 fd = -1;
2405 } else
2406 sb.st_size = 0;
2408 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2409 a->diff_context, a->ignore_whitespace, stdout);
2410 done:
2411 if (blob1)
2412 got_object_blob_close(blob1);
2413 if (f2 && fclose(f2) == EOF && err == NULL)
2414 err = got_error_from_errno("fclose");
2415 if (fd != -1 && close(fd) == -1 && err == NULL)
2416 err = got_error_from_errno("close");
2417 free(abspath);
2418 return err;
2421 static const struct got_error *
2422 cmd_diff(int argc, char *argv[])
2424 const struct got_error *error;
2425 struct got_repository *repo = NULL;
2426 struct got_worktree *worktree = NULL;
2427 char *cwd = NULL, *repo_path = NULL;
2428 struct got_object_id *id1 = NULL, *id2 = NULL;
2429 const char *id_str1 = NULL, *id_str2 = NULL;
2430 char *label1 = NULL, *label2 = NULL;
2431 int type1, type2;
2432 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2433 const char *errstr;
2434 char *path = NULL;
2436 #ifndef PROFILE
2437 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2438 NULL) == -1)
2439 err(1, "pledge");
2440 #endif
2442 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2443 switch (ch) {
2444 case 'C':
2445 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2446 &errstr);
2447 if (errstr != NULL)
2448 err(1, "-C option %s", errstr);
2449 break;
2450 case 'r':
2451 repo_path = realpath(optarg, NULL);
2452 if (repo_path == NULL)
2453 return got_error_from_errno2("realpath",
2454 optarg);
2455 got_path_strip_trailing_slashes(repo_path);
2456 break;
2457 case 's':
2458 diff_staged = 1;
2459 break;
2460 case 'w':
2461 ignore_whitespace = 1;
2462 break;
2463 default:
2464 usage_diff();
2465 /* NOTREACHED */
2469 argc -= optind;
2470 argv += optind;
2472 cwd = getcwd(NULL, 0);
2473 if (cwd == NULL) {
2474 error = got_error_from_errno("getcwd");
2475 goto done;
2477 error = got_worktree_open(&worktree, cwd);
2478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2479 goto done;
2480 if (argc <= 1) {
2481 if (worktree == NULL) {
2482 error = got_error(GOT_ERR_NOT_WORKTREE);
2483 goto done;
2485 if (repo_path)
2486 errx(1,
2487 "-r option can't be used when diffing a work tree");
2488 repo_path = strdup(got_worktree_get_repo_path(worktree));
2489 if (repo_path == NULL) {
2490 error = got_error_from_errno("strdup");
2491 goto done;
2493 if (argc == 1) {
2494 error = got_worktree_resolve_path(&path, worktree,
2495 argv[0]);
2496 if (error)
2497 goto done;
2498 } else {
2499 path = strdup("");
2500 if (path == NULL) {
2501 error = got_error_from_errno("strdup");
2502 goto done;
2505 } else if (argc == 2) {
2506 if (diff_staged)
2507 errx(1, "-s option can't be used when diffing "
2508 "objects in repository");
2509 id_str1 = argv[0];
2510 id_str2 = argv[1];
2511 if (worktree && repo_path == NULL) {
2512 repo_path =
2513 strdup(got_worktree_get_repo_path(worktree));
2514 if (repo_path == NULL) {
2515 error = got_error_from_errno("strdup");
2516 goto done;
2519 } else
2520 usage_diff();
2522 if (repo_path == NULL) {
2523 repo_path = getcwd(NULL, 0);
2524 if (repo_path == NULL)
2525 return got_error_from_errno("getcwd");
2528 error = got_repo_open(&repo, repo_path, NULL);
2529 free(repo_path);
2530 if (error != NULL)
2531 goto done;
2533 error = apply_unveil(got_repo_get_path(repo), 1,
2534 worktree ? got_worktree_get_root_path(worktree) : NULL);
2535 if (error)
2536 goto done;
2538 if (argc <= 1) {
2539 struct print_diff_arg arg;
2540 struct got_pathlist_head paths;
2541 char *id_str;
2543 TAILQ_INIT(&paths);
2545 error = got_object_id_str(&id_str,
2546 got_worktree_get_base_commit_id(worktree));
2547 if (error)
2548 goto done;
2549 arg.repo = repo;
2550 arg.worktree = worktree;
2551 arg.diff_context = diff_context;
2552 arg.id_str = id_str;
2553 arg.header_shown = 0;
2554 arg.diff_staged = diff_staged;
2555 arg.ignore_whitespace = ignore_whitespace;
2557 error = got_pathlist_append(&paths, path, NULL);
2558 if (error)
2559 goto done;
2561 error = got_worktree_status(worktree, &paths, repo, print_diff,
2562 &arg, check_cancelled, NULL);
2563 free(id_str);
2564 got_pathlist_free(&paths);
2565 goto done;
2568 error = got_repo_match_object_id(&id1, &label1, id_str1,
2569 GOT_OBJ_TYPE_ANY, 1, repo);
2570 if (error)
2571 goto done;
2573 error = got_repo_match_object_id(&id2, &label2, id_str2,
2574 GOT_OBJ_TYPE_ANY, 1, repo);
2575 if (error)
2576 goto done;
2578 error = got_object_get_type(&type1, repo, id1);
2579 if (error)
2580 goto done;
2582 error = got_object_get_type(&type2, repo, id2);
2583 if (error)
2584 goto done;
2586 if (type1 != type2) {
2587 error = got_error(GOT_ERR_OBJ_TYPE);
2588 goto done;
2591 switch (type1) {
2592 case GOT_OBJ_TYPE_BLOB:
2593 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2594 diff_context, ignore_whitespace, repo, stdout);
2595 break;
2596 case GOT_OBJ_TYPE_TREE:
2597 error = got_diff_objects_as_trees(id1, id2, "", "",
2598 diff_context, ignore_whitespace, repo, stdout);
2599 break;
2600 case GOT_OBJ_TYPE_COMMIT:
2601 printf("diff %s %s\n", label1, label2);
2602 error = got_diff_objects_as_commits(id1, id2, diff_context,
2603 ignore_whitespace, repo, stdout);
2604 break;
2605 default:
2606 error = got_error(GOT_ERR_OBJ_TYPE);
2608 done:
2609 free(label1);
2610 free(label2);
2611 free(id1);
2612 free(id2);
2613 free(path);
2614 if (worktree)
2615 got_worktree_close(worktree);
2616 if (repo) {
2617 const struct got_error *repo_error;
2618 repo_error = got_repo_close(repo);
2619 if (error == NULL)
2620 error = repo_error;
2622 return error;
2625 __dead static void
2626 usage_blame(void)
2628 fprintf(stderr,
2629 "usage: %s blame [-c commit] [-r repository-path] path\n",
2630 getprogname());
2631 exit(1);
2634 struct blame_line {
2635 int annotated;
2636 char *id_str;
2637 char *committer;
2638 char datebuf[11]; /* YYYY-MM-DD + NUL */
2641 struct blame_cb_args {
2642 struct blame_line *lines;
2643 int nlines;
2644 int nlines_prec;
2645 int lineno_cur;
2646 off_t *line_offsets;
2647 FILE *f;
2648 struct got_repository *repo;
2651 static const struct got_error *
2652 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2654 const struct got_error *err = NULL;
2655 struct blame_cb_args *a = arg;
2656 struct blame_line *bline;
2657 char *line = NULL;
2658 size_t linesize = 0;
2659 struct got_commit_object *commit = NULL;
2660 off_t offset;
2661 struct tm tm;
2662 time_t committer_time;
2664 if (nlines != a->nlines ||
2665 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2666 return got_error(GOT_ERR_RANGE);
2668 if (sigint_received)
2669 return got_error(GOT_ERR_ITER_COMPLETED);
2671 if (lineno == -1)
2672 return NULL; /* no change in this commit */
2674 /* Annotate this line. */
2675 bline = &a->lines[lineno - 1];
2676 if (bline->annotated)
2677 return NULL;
2678 err = got_object_id_str(&bline->id_str, id);
2679 if (err)
2680 return err;
2682 err = got_object_open_as_commit(&commit, a->repo, id);
2683 if (err)
2684 goto done;
2686 bline->committer = strdup(got_object_commit_get_committer(commit));
2687 if (bline->committer == NULL) {
2688 err = got_error_from_errno("strdup");
2689 goto done;
2692 committer_time = got_object_commit_get_committer_time(commit);
2693 if (localtime_r(&committer_time, &tm) == NULL)
2694 return got_error_from_errno("localtime_r");
2695 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2696 &tm) >= sizeof(bline->datebuf)) {
2697 err = got_error(GOT_ERR_NO_SPACE);
2698 goto done;
2700 bline->annotated = 1;
2702 /* Print lines annotated so far. */
2703 bline = &a->lines[a->lineno_cur - 1];
2704 if (!bline->annotated)
2705 goto done;
2707 offset = a->line_offsets[a->lineno_cur - 1];
2708 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2709 err = got_error_from_errno("fseeko");
2710 goto done;
2713 while (bline->annotated) {
2714 char *smallerthan, *at, *nl, *committer;
2715 size_t len;
2717 if (getline(&line, &linesize, a->f) == -1) {
2718 if (ferror(a->f))
2719 err = got_error_from_errno("getline");
2720 break;
2723 committer = bline->committer;
2724 smallerthan = strchr(committer, '<');
2725 if (smallerthan && smallerthan[1] != '\0')
2726 committer = smallerthan + 1;
2727 at = strchr(committer, '@');
2728 if (at)
2729 *at = '\0';
2730 len = strlen(committer);
2731 if (len >= 9)
2732 committer[8] = '\0';
2734 nl = strchr(line, '\n');
2735 if (nl)
2736 *nl = '\0';
2737 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2738 bline->id_str, bline->datebuf, committer, line);
2740 a->lineno_cur++;
2741 bline = &a->lines[a->lineno_cur - 1];
2743 done:
2744 if (commit)
2745 got_object_commit_close(commit);
2746 free(line);
2747 return err;
2750 static const struct got_error *
2751 cmd_blame(int argc, char *argv[])
2753 const struct got_error *error;
2754 struct got_repository *repo = NULL;
2755 struct got_worktree *worktree = NULL;
2756 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2757 struct got_object_id *obj_id = NULL;
2758 struct got_object_id *commit_id = NULL;
2759 struct got_blob_object *blob = NULL;
2760 char *commit_id_str = NULL;
2761 struct blame_cb_args bca;
2762 int ch, obj_type, i;
2763 size_t filesize;
2765 memset(&bca, 0, sizeof(bca));
2767 #ifndef PROFILE
2768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2769 NULL) == -1)
2770 err(1, "pledge");
2771 #endif
2773 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2774 switch (ch) {
2775 case 'c':
2776 commit_id_str = optarg;
2777 break;
2778 case 'r':
2779 repo_path = realpath(optarg, NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath",
2782 optarg);
2783 got_path_strip_trailing_slashes(repo_path);
2784 break;
2785 default:
2786 usage_blame();
2787 /* NOTREACHED */
2791 argc -= optind;
2792 argv += optind;
2794 if (argc == 1)
2795 path = argv[0];
2796 else
2797 usage_blame();
2799 cwd = getcwd(NULL, 0);
2800 if (cwd == NULL) {
2801 error = got_error_from_errno("getcwd");
2802 goto done;
2804 if (repo_path == NULL) {
2805 error = got_worktree_open(&worktree, cwd);
2806 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2807 goto done;
2808 else
2809 error = NULL;
2810 if (worktree) {
2811 repo_path =
2812 strdup(got_worktree_get_repo_path(worktree));
2813 if (repo_path == NULL)
2814 error = got_error_from_errno("strdup");
2815 if (error)
2816 goto done;
2817 } else {
2818 repo_path = strdup(cwd);
2819 if (repo_path == NULL) {
2820 error = got_error_from_errno("strdup");
2821 goto done;
2826 error = got_repo_open(&repo, repo_path, NULL);
2827 if (error != NULL)
2828 goto done;
2830 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2831 if (error)
2832 goto done;
2834 if (worktree) {
2835 const char *prefix = got_worktree_get_path_prefix(worktree);
2836 char *p, *worktree_subdir = cwd +
2837 strlen(got_worktree_get_root_path(worktree));
2838 if (asprintf(&p, "%s%s%s%s%s",
2839 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2840 worktree_subdir, worktree_subdir[0] ? "/" : "",
2841 path) == -1) {
2842 error = got_error_from_errno("asprintf");
2843 goto done;
2845 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2846 free(p);
2847 } else {
2848 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2850 if (error)
2851 goto done;
2853 if (commit_id_str == NULL) {
2854 struct got_reference *head_ref;
2855 error = got_ref_open(&head_ref, repo, worktree ?
2856 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2857 if (error != NULL)
2858 goto done;
2859 error = got_ref_resolve(&commit_id, repo, head_ref);
2860 got_ref_close(head_ref);
2861 if (error != NULL)
2862 goto done;
2863 } else {
2864 error = got_repo_match_object_id(&commit_id, NULL,
2865 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2866 if (error)
2867 goto done;
2870 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2871 if (error)
2872 goto done;
2874 error = got_object_get_type(&obj_type, repo, obj_id);
2875 if (error)
2876 goto done;
2878 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2879 error = got_error(GOT_ERR_OBJ_TYPE);
2880 goto done;
2883 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2884 if (error)
2885 goto done;
2886 bca.f = got_opentemp();
2887 if (bca.f == NULL) {
2888 error = got_error_from_errno("got_opentemp");
2889 goto done;
2891 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2892 &bca.line_offsets, bca.f, blob);
2893 if (error || bca.nlines == 0)
2894 goto done;
2896 /* Don't include \n at EOF in the blame line count. */
2897 if (bca.line_offsets[bca.nlines - 1] == filesize)
2898 bca.nlines--;
2900 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2901 if (bca.lines == NULL) {
2902 error = got_error_from_errno("calloc");
2903 goto done;
2905 bca.lineno_cur = 1;
2906 bca.nlines_prec = 0;
2907 i = bca.nlines;
2908 while (i > 0) {
2909 i /= 10;
2910 bca.nlines_prec++;
2912 bca.repo = repo;
2914 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2915 check_cancelled, NULL);
2916 done:
2917 free(in_repo_path);
2918 free(repo_path);
2919 free(cwd);
2920 free(commit_id);
2921 free(obj_id);
2922 if (blob)
2923 got_object_blob_close(blob);
2924 if (worktree)
2925 got_worktree_close(worktree);
2926 if (repo) {
2927 const struct got_error *repo_error;
2928 repo_error = got_repo_close(repo);
2929 if (error == NULL)
2930 error = repo_error;
2932 if (bca.lines) {
2933 for (i = 0; i < bca.nlines; i++) {
2934 struct blame_line *bline = &bca.lines[i];
2935 free(bline->id_str);
2936 free(bline->committer);
2938 free(bca.lines);
2940 free(bca.line_offsets);
2941 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2942 error = got_error_from_errno("fclose");
2943 return error;
2946 __dead static void
2947 usage_tree(void)
2949 fprintf(stderr,
2950 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2951 getprogname());
2952 exit(1);
2955 static void
2956 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2957 const char *root_path)
2959 int is_root_path = (strcmp(path, root_path) == 0);
2960 const char *modestr = "";
2961 mode_t mode = got_tree_entry_get_mode(te);
2963 path += strlen(root_path);
2964 while (path[0] == '/')
2965 path++;
2967 if (got_object_tree_entry_is_submodule(te))
2968 modestr = "$";
2969 else if (S_ISLNK(mode))
2970 modestr = "@";
2971 else if (S_ISDIR(mode))
2972 modestr = "/";
2973 else if (mode & S_IXUSR)
2974 modestr = "*";
2976 printf("%s%s%s%s%s\n", id ? id : "", path,
2977 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2980 static const struct got_error *
2981 print_tree(const char *path, struct got_object_id *commit_id,
2982 int show_ids, int recurse, const char *root_path,
2983 struct got_repository *repo)
2985 const struct got_error *err = NULL;
2986 struct got_object_id *tree_id = NULL;
2987 struct got_tree_object *tree = NULL;
2988 int nentries, i;
2990 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2991 if (err)
2992 goto done;
2994 err = got_object_open_as_tree(&tree, repo, tree_id);
2995 if (err)
2996 goto done;
2997 nentries = got_object_tree_get_nentries(tree);
2998 for (i = 0; i < nentries; i++) {
2999 struct got_tree_entry *te;
3000 char *id = NULL;
3002 if (sigint_received || sigpipe_received)
3003 break;
3005 te = got_object_tree_get_entry(tree, i);
3006 if (show_ids) {
3007 char *id_str;
3008 err = got_object_id_str(&id_str,
3009 got_tree_entry_get_id(te));
3010 if (err)
3011 goto done;
3012 if (asprintf(&id, "%s ", id_str) == -1) {
3013 err = got_error_from_errno("asprintf");
3014 free(id_str);
3015 goto done;
3017 free(id_str);
3019 print_entry(te, id, path, root_path);
3020 free(id);
3022 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3023 char *child_path;
3024 if (asprintf(&child_path, "%s%s%s", path,
3025 path[0] == '/' && path[1] == '\0' ? "" : "/",
3026 got_tree_entry_get_name(te)) == -1) {
3027 err = got_error_from_errno("asprintf");
3028 goto done;
3030 err = print_tree(child_path, commit_id, show_ids, 1,
3031 root_path, repo);
3032 free(child_path);
3033 if (err)
3034 goto done;
3037 done:
3038 if (tree)
3039 got_object_tree_close(tree);
3040 free(tree_id);
3041 return err;
3044 static const struct got_error *
3045 cmd_tree(int argc, char *argv[])
3047 const struct got_error *error;
3048 struct got_repository *repo = NULL;
3049 struct got_worktree *worktree = NULL;
3050 const char *path;
3051 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3052 struct got_object_id *commit_id = NULL;
3053 char *commit_id_str = NULL;
3054 int show_ids = 0, recurse = 0;
3055 int ch;
3057 #ifndef PROFILE
3058 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3059 NULL) == -1)
3060 err(1, "pledge");
3061 #endif
3063 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3064 switch (ch) {
3065 case 'c':
3066 commit_id_str = optarg;
3067 break;
3068 case 'r':
3069 repo_path = realpath(optarg, NULL);
3070 if (repo_path == NULL)
3071 return got_error_from_errno2("realpath",
3072 optarg);
3073 got_path_strip_trailing_slashes(repo_path);
3074 break;
3075 case 'i':
3076 show_ids = 1;
3077 break;
3078 case 'R':
3079 recurse = 1;
3080 break;
3081 default:
3082 usage_tree();
3083 /* NOTREACHED */
3087 argc -= optind;
3088 argv += optind;
3090 if (argc == 1)
3091 path = argv[0];
3092 else if (argc > 1)
3093 usage_tree();
3094 else
3095 path = NULL;
3097 cwd = getcwd(NULL, 0);
3098 if (cwd == NULL) {
3099 error = got_error_from_errno("getcwd");
3100 goto done;
3102 if (repo_path == NULL) {
3103 error = got_worktree_open(&worktree, cwd);
3104 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3105 goto done;
3106 else
3107 error = NULL;
3108 if (worktree) {
3109 repo_path =
3110 strdup(got_worktree_get_repo_path(worktree));
3111 if (repo_path == NULL)
3112 error = got_error_from_errno("strdup");
3113 if (error)
3114 goto done;
3115 } else {
3116 repo_path = strdup(cwd);
3117 if (repo_path == NULL) {
3118 error = got_error_from_errno("strdup");
3119 goto done;
3124 error = got_repo_open(&repo, repo_path, NULL);
3125 if (error != NULL)
3126 goto done;
3128 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3129 if (error)
3130 goto done;
3132 if (path == NULL) {
3133 if (worktree) {
3134 char *p, *worktree_subdir = cwd +
3135 strlen(got_worktree_get_root_path(worktree));
3136 if (asprintf(&p, "%s/%s",
3137 got_worktree_get_path_prefix(worktree),
3138 worktree_subdir) == -1) {
3139 error = got_error_from_errno("asprintf");
3140 goto done;
3142 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3143 free(p);
3144 if (error)
3145 goto done;
3146 } else
3147 path = "/";
3149 if (in_repo_path == NULL) {
3150 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3151 if (error != NULL)
3152 goto done;
3155 if (commit_id_str == NULL) {
3156 struct got_reference *head_ref;
3157 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3158 if (error != NULL)
3159 goto done;
3160 error = got_ref_resolve(&commit_id, repo, head_ref);
3161 got_ref_close(head_ref);
3162 if (error != NULL)
3163 goto done;
3164 } else {
3165 error = got_repo_match_object_id(&commit_id, NULL,
3166 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3167 if (error)
3168 goto done;
3171 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3172 in_repo_path, repo);
3173 done:
3174 free(in_repo_path);
3175 free(repo_path);
3176 free(cwd);
3177 free(commit_id);
3178 if (worktree)
3179 got_worktree_close(worktree);
3180 if (repo) {
3181 const struct got_error *repo_error;
3182 repo_error = got_repo_close(repo);
3183 if (error == NULL)
3184 error = repo_error;
3186 return error;
3189 __dead static void
3190 usage_status(void)
3192 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3193 exit(1);
3196 static const struct got_error *
3197 print_status(void *arg, unsigned char status, unsigned char staged_status,
3198 const char *path, struct got_object_id *blob_id,
3199 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3200 int dirfd, const char *de_name)
3202 if (status == staged_status && (status == GOT_STATUS_DELETE))
3203 status = GOT_STATUS_NO_CHANGE;
3204 printf("%c%c %s\n", status, staged_status, path);
3205 return NULL;
3208 static const struct got_error *
3209 cmd_status(int argc, char *argv[])
3211 const struct got_error *error = NULL;
3212 struct got_repository *repo = NULL;
3213 struct got_worktree *worktree = NULL;
3214 char *cwd = NULL;
3215 struct got_pathlist_head paths;
3216 struct got_pathlist_entry *pe;
3217 int ch;
3219 TAILQ_INIT(&paths);
3221 while ((ch = getopt(argc, argv, "")) != -1) {
3222 switch (ch) {
3223 default:
3224 usage_status();
3225 /* NOTREACHED */
3229 argc -= optind;
3230 argv += optind;
3232 #ifndef PROFILE
3233 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3234 NULL) == -1)
3235 err(1, "pledge");
3236 #endif
3237 cwd = getcwd(NULL, 0);
3238 if (cwd == NULL) {
3239 error = got_error_from_errno("getcwd");
3240 goto done;
3243 error = got_worktree_open(&worktree, cwd);
3244 if (error != NULL)
3245 goto done;
3247 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3248 NULL);
3249 if (error != NULL)
3250 goto done;
3252 error = apply_unveil(got_repo_get_path(repo), 1,
3253 got_worktree_get_root_path(worktree));
3254 if (error)
3255 goto done;
3257 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3258 if (error)
3259 goto done;
3261 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3262 check_cancelled, NULL);
3263 done:
3264 TAILQ_FOREACH(pe, &paths, entry)
3265 free((char *)pe->path);
3266 got_pathlist_free(&paths);
3267 free(cwd);
3268 return error;
3271 __dead static void
3272 usage_ref(void)
3274 fprintf(stderr,
3275 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3276 getprogname());
3277 exit(1);
3280 static const struct got_error *
3281 list_refs(struct got_repository *repo)
3283 static const struct got_error *err = NULL;
3284 struct got_reflist_head refs;
3285 struct got_reflist_entry *re;
3287 SIMPLEQ_INIT(&refs);
3288 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3289 if (err)
3290 return err;
3292 SIMPLEQ_FOREACH(re, &refs, entry) {
3293 char *refstr;
3294 refstr = got_ref_to_str(re->ref);
3295 if (refstr == NULL)
3296 return got_error_from_errno("got_ref_to_str");
3297 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3298 free(refstr);
3301 got_ref_list_free(&refs);
3302 return NULL;
3305 static const struct got_error *
3306 delete_ref(struct got_repository *repo, const char *refname)
3308 const struct got_error *err = NULL;
3309 struct got_reference *ref;
3311 err = got_ref_open(&ref, repo, refname, 0);
3312 if (err)
3313 return err;
3315 err = got_ref_delete(ref, repo);
3316 got_ref_close(ref);
3317 return err;
3320 static const struct got_error *
3321 add_ref(struct got_repository *repo, const char *refname, const char *target)
3323 const struct got_error *err = NULL;
3324 struct got_object_id *id;
3325 struct got_reference *ref = NULL;
3328 * Don't let the user create a reference name with a leading '-'.
3329 * While technically a valid reference name, this case is usually
3330 * an unintended typo.
3332 if (refname[0] == '-')
3333 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3335 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3336 repo);
3337 if (err) {
3338 struct got_reference *target_ref;
3340 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3341 return err;
3342 err = got_ref_open(&target_ref, repo, target, 0);
3343 if (err)
3344 return err;
3345 err = got_ref_resolve(&id, repo, target_ref);
3346 got_ref_close(target_ref);
3347 if (err)
3348 return err;
3351 err = got_ref_alloc(&ref, refname, id);
3352 if (err)
3353 goto done;
3355 err = got_ref_write(ref, repo);
3356 done:
3357 if (ref)
3358 got_ref_close(ref);
3359 free(id);
3360 return err;
3363 static const struct got_error *
3364 add_symref(struct got_repository *repo, const char *refname, const char *target)
3366 const struct got_error *err = NULL;
3367 struct got_reference *ref = NULL;
3368 struct got_reference *target_ref = NULL;
3371 * Don't let the user create a reference name with a leading '-'.
3372 * While technically a valid reference name, this case is usually
3373 * an unintended typo.
3375 if (refname[0] == '-')
3376 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3378 err = got_ref_open(&target_ref, repo, target, 0);
3379 if (err)
3380 return err;
3382 err = got_ref_alloc_symref(&ref, refname, target_ref);
3383 if (err)
3384 goto done;
3386 err = got_ref_write(ref, repo);
3387 done:
3388 if (target_ref)
3389 got_ref_close(target_ref);
3390 if (ref)
3391 got_ref_close(ref);
3392 return err;
3395 static const struct got_error *
3396 cmd_ref(int argc, char *argv[])
3398 const struct got_error *error = NULL;
3399 struct got_repository *repo = NULL;
3400 struct got_worktree *worktree = NULL;
3401 char *cwd = NULL, *repo_path = NULL;
3402 int ch, do_list = 0, create_symref = 0;
3403 const char *delref = NULL;
3405 /* TODO: Add -s option for adding symbolic references. */
3406 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3407 switch (ch) {
3408 case 'd':
3409 delref = optarg;
3410 break;
3411 case 'r':
3412 repo_path = realpath(optarg, NULL);
3413 if (repo_path == NULL)
3414 return got_error_from_errno2("realpath",
3415 optarg);
3416 got_path_strip_trailing_slashes(repo_path);
3417 break;
3418 case 'l':
3419 do_list = 1;
3420 break;
3421 case 's':
3422 create_symref = 1;
3423 break;
3424 default:
3425 usage_ref();
3426 /* NOTREACHED */
3430 if (do_list && delref)
3431 errx(1, "-l and -d options are mutually exclusive\n");
3433 argc -= optind;
3434 argv += optind;
3436 if (do_list || delref) {
3437 if (create_symref)
3438 errx(1, "-s option cannot be used together with the "
3439 "-l or -d options");
3440 if (argc > 0)
3441 usage_ref();
3442 } else if (argc != 2)
3443 usage_ref();
3445 #ifndef PROFILE
3446 if (do_list) {
3447 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3448 NULL) == -1)
3449 err(1, "pledge");
3450 } else {
3451 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3452 "sendfd unveil", NULL) == -1)
3453 err(1, "pledge");
3455 #endif
3456 cwd = getcwd(NULL, 0);
3457 if (cwd == NULL) {
3458 error = got_error_from_errno("getcwd");
3459 goto done;
3462 if (repo_path == NULL) {
3463 error = got_worktree_open(&worktree, cwd);
3464 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3465 goto done;
3466 else
3467 error = NULL;
3468 if (worktree) {
3469 repo_path =
3470 strdup(got_worktree_get_repo_path(worktree));
3471 if (repo_path == NULL)
3472 error = got_error_from_errno("strdup");
3473 if (error)
3474 goto done;
3475 } else {
3476 repo_path = strdup(cwd);
3477 if (repo_path == NULL) {
3478 error = got_error_from_errno("strdup");
3479 goto done;
3484 error = got_repo_open(&repo, repo_path, NULL);
3485 if (error != NULL)
3486 goto done;
3488 error = apply_unveil(got_repo_get_path(repo), do_list,
3489 worktree ? got_worktree_get_root_path(worktree) : NULL);
3490 if (error)
3491 goto done;
3493 if (do_list)
3494 error = list_refs(repo);
3495 else if (delref)
3496 error = delete_ref(repo, delref);
3497 else if (create_symref)
3498 error = add_symref(repo, argv[0], argv[1]);
3499 else
3500 error = add_ref(repo, argv[0], argv[1]);
3501 done:
3502 if (repo)
3503 got_repo_close(repo);
3504 if (worktree)
3505 got_worktree_close(worktree);
3506 free(cwd);
3507 free(repo_path);
3508 return error;
3511 __dead static void
3512 usage_branch(void)
3514 fprintf(stderr,
3515 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3516 "[name]\n", getprogname());
3517 exit(1);
3520 static const struct got_error *
3521 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3522 struct got_reference *ref)
3524 const struct got_error *err = NULL;
3525 const char *refname, *marker = " ";
3526 char *refstr;
3528 refname = got_ref_get_name(ref);
3529 if (worktree && strcmp(refname,
3530 got_worktree_get_head_ref_name(worktree)) == 0) {
3531 struct got_object_id *id = NULL;
3533 err = got_ref_resolve(&id, repo, ref);
3534 if (err)
3535 return err;
3536 if (got_object_id_cmp(id,
3537 got_worktree_get_base_commit_id(worktree)) == 0)
3538 marker = "* ";
3539 else
3540 marker = "~ ";
3541 free(id);
3544 if (strncmp(refname, "refs/heads/", 11) == 0)
3545 refname += 11;
3546 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3547 refname += 18;
3549 refstr = got_ref_to_str(ref);
3550 if (refstr == NULL)
3551 return got_error_from_errno("got_ref_to_str");
3553 printf("%s%s: %s\n", marker, refname, refstr);
3554 free(refstr);
3555 return NULL;
3558 static const struct got_error *
3559 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3561 const char *refname;
3563 if (worktree == NULL)
3564 return got_error(GOT_ERR_NOT_WORKTREE);
3566 refname = got_worktree_get_head_ref_name(worktree);
3568 if (strncmp(refname, "refs/heads/", 11) == 0)
3569 refname += 11;
3570 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3571 refname += 18;
3573 printf("%s\n", refname);
3575 return NULL;
3578 static const struct got_error *
3579 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3581 static const struct got_error *err = NULL;
3582 struct got_reflist_head refs;
3583 struct got_reflist_entry *re;
3584 struct got_reference *temp_ref = NULL;
3585 int rebase_in_progress, histedit_in_progress;
3587 SIMPLEQ_INIT(&refs);
3589 if (worktree) {
3590 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3591 worktree);
3592 if (err)
3593 return err;
3595 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3596 worktree);
3597 if (err)
3598 return err;
3600 if (rebase_in_progress || histedit_in_progress) {
3601 err = got_ref_open(&temp_ref, repo,
3602 got_worktree_get_head_ref_name(worktree), 0);
3603 if (err)
3604 return err;
3605 list_branch(repo, worktree, temp_ref);
3606 got_ref_close(temp_ref);
3610 err = got_ref_list(&refs, repo, "refs/heads",
3611 got_ref_cmp_by_name, NULL);
3612 if (err)
3613 return err;
3615 SIMPLEQ_FOREACH(re, &refs, entry)
3616 list_branch(repo, worktree, re->ref);
3618 got_ref_list_free(&refs);
3619 return NULL;
3622 static const struct got_error *
3623 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3624 const char *branch_name)
3626 const struct got_error *err = NULL;
3627 struct got_reference *ref = NULL;
3628 char *refname;
3630 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3631 return got_error_from_errno("asprintf");
3633 err = got_ref_open(&ref, repo, refname, 0);
3634 if (err)
3635 goto done;
3637 if (worktree &&
3638 strcmp(got_worktree_get_head_ref_name(worktree),
3639 got_ref_get_name(ref)) == 0) {
3640 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3641 "will not delete this work tree's current branch");
3642 goto done;
3645 err = got_ref_delete(ref, repo);
3646 done:
3647 if (ref)
3648 got_ref_close(ref);
3649 free(refname);
3650 return err;
3653 static const struct got_error *
3654 add_branch(struct got_repository *repo, const char *branch_name,
3655 struct got_object_id *base_commit_id)
3657 const struct got_error *err = NULL;
3658 struct got_reference *ref = NULL;
3659 char *base_refname = NULL, *refname = NULL;
3662 * Don't let the user create a branch name with a leading '-'.
3663 * While technically a valid reference name, this case is usually
3664 * an unintended typo.
3666 if (branch_name[0] == '-')
3667 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3670 err = got_error_from_errno("asprintf");
3671 goto done;
3674 err = got_ref_open(&ref, repo, refname, 0);
3675 if (err == NULL) {
3676 err = got_error(GOT_ERR_BRANCH_EXISTS);
3677 goto done;
3678 } else if (err->code != GOT_ERR_NOT_REF)
3679 goto done;
3681 err = got_ref_alloc(&ref, refname, base_commit_id);
3682 if (err)
3683 goto done;
3685 err = got_ref_write(ref, repo);
3686 done:
3687 if (ref)
3688 got_ref_close(ref);
3689 free(base_refname);
3690 free(refname);
3691 return err;
3694 static const struct got_error *
3695 cmd_branch(int argc, char *argv[])
3697 const struct got_error *error = NULL;
3698 struct got_repository *repo = NULL;
3699 struct got_worktree *worktree = NULL;
3700 char *cwd = NULL, *repo_path = NULL;
3701 int ch, do_list = 0, do_show = 0, do_update = 1;
3702 const char *delref = NULL, *commit_id_arg = NULL;
3703 struct got_reference *ref = NULL;
3704 struct got_pathlist_head paths;
3705 struct got_pathlist_entry *pe;
3706 struct got_object_id *commit_id = NULL;
3707 char *commit_id_str = NULL;
3709 TAILQ_INIT(&paths);
3711 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3712 switch (ch) {
3713 case 'c':
3714 commit_id_arg = optarg;
3715 break;
3716 case 'd':
3717 delref = optarg;
3718 break;
3719 case 'r':
3720 repo_path = realpath(optarg, NULL);
3721 if (repo_path == NULL)
3722 return got_error_from_errno2("realpath",
3723 optarg);
3724 got_path_strip_trailing_slashes(repo_path);
3725 break;
3726 case 'l':
3727 do_list = 1;
3728 break;
3729 case 'n':
3730 do_update = 0;
3731 break;
3732 default:
3733 usage_branch();
3734 /* NOTREACHED */
3738 if (do_list && delref)
3739 errx(1, "-l and -d options are mutually exclusive\n");
3741 argc -= optind;
3742 argv += optind;
3744 if (!do_list && !delref && argc == 0)
3745 do_show = 1;
3747 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3748 errx(1, "-c option can only be used when creating a branch");
3750 if (do_list || delref) {
3751 if (argc > 0)
3752 usage_branch();
3753 } else if (!do_show && argc != 1)
3754 usage_branch();
3756 #ifndef PROFILE
3757 if (do_list || do_show) {
3758 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3759 NULL) == -1)
3760 err(1, "pledge");
3761 } else {
3762 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3763 "sendfd unveil", NULL) == -1)
3764 err(1, "pledge");
3766 #endif
3767 cwd = getcwd(NULL, 0);
3768 if (cwd == NULL) {
3769 error = got_error_from_errno("getcwd");
3770 goto done;
3773 if (repo_path == NULL) {
3774 error = got_worktree_open(&worktree, cwd);
3775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3776 goto done;
3777 else
3778 error = NULL;
3779 if (worktree) {
3780 repo_path =
3781 strdup(got_worktree_get_repo_path(worktree));
3782 if (repo_path == NULL)
3783 error = got_error_from_errno("strdup");
3784 if (error)
3785 goto done;
3786 } else {
3787 repo_path = strdup(cwd);
3788 if (repo_path == NULL) {
3789 error = got_error_from_errno("strdup");
3790 goto done;
3795 error = got_repo_open(&repo, repo_path, NULL);
3796 if (error != NULL)
3797 goto done;
3799 error = apply_unveil(got_repo_get_path(repo), do_list,
3800 worktree ? got_worktree_get_root_path(worktree) : NULL);
3801 if (error)
3802 goto done;
3804 if (do_show)
3805 error = show_current_branch(repo, worktree);
3806 else if (do_list)
3807 error = list_branches(repo, worktree);
3808 else if (delref)
3809 error = delete_branch(repo, worktree, delref);
3810 else {
3811 if (commit_id_arg == NULL)
3812 commit_id_arg = worktree ?
3813 got_worktree_get_head_ref_name(worktree) :
3814 GOT_REF_HEAD;
3815 error = got_repo_match_object_id(&commit_id, NULL,
3816 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3817 if (error)
3818 goto done;
3819 error = add_branch(repo, argv[0], commit_id);
3820 if (error)
3821 goto done;
3822 if (worktree && do_update) {
3823 int did_something = 0;
3824 char *branch_refname = NULL;
3826 error = got_object_id_str(&commit_id_str, commit_id);
3827 if (error)
3828 goto done;
3829 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3830 worktree);
3831 if (error)
3832 goto done;
3833 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3834 == -1) {
3835 error = got_error_from_errno("asprintf");
3836 goto done;
3838 error = got_ref_open(&ref, repo, branch_refname, 0);
3839 free(branch_refname);
3840 if (error)
3841 goto done;
3842 error = switch_head_ref(ref, commit_id, worktree,
3843 repo);
3844 if (error)
3845 goto done;
3846 error = got_worktree_set_base_commit_id(worktree, repo,
3847 commit_id);
3848 if (error)
3849 goto done;
3850 error = got_worktree_checkout_files(worktree, &paths,
3851 repo, update_progress, &did_something,
3852 check_cancelled, NULL);
3853 if (error)
3854 goto done;
3855 if (did_something)
3856 printf("Updated to commit %s\n", commit_id_str);
3859 done:
3860 if (ref)
3861 got_ref_close(ref);
3862 if (repo)
3863 got_repo_close(repo);
3864 if (worktree)
3865 got_worktree_close(worktree);
3866 free(cwd);
3867 free(repo_path);
3868 free(commit_id);
3869 free(commit_id_str);
3870 TAILQ_FOREACH(pe, &paths, entry)
3871 free((char *)pe->path);
3872 got_pathlist_free(&paths);
3873 return error;
3877 __dead static void
3878 usage_tag(void)
3880 fprintf(stderr,
3881 "usage: %s tag [-c commit] [-r repository] [-l] "
3882 "[-m message] name\n", getprogname());
3883 exit(1);
3886 #if 0
3887 static const struct got_error *
3888 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3890 const struct got_error *err = NULL;
3891 struct got_reflist_entry *re, *se, *new;
3892 struct got_object_id *re_id, *se_id;
3893 struct got_tag_object *re_tag, *se_tag;
3894 time_t re_time, se_time;
3896 SIMPLEQ_FOREACH(re, tags, entry) {
3897 se = SIMPLEQ_FIRST(sorted);
3898 if (se == NULL) {
3899 err = got_reflist_entry_dup(&new, re);
3900 if (err)
3901 return err;
3902 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3903 continue;
3904 } else {
3905 err = got_ref_resolve(&re_id, repo, re->ref);
3906 if (err)
3907 break;
3908 err = got_object_open_as_tag(&re_tag, repo, re_id);
3909 free(re_id);
3910 if (err)
3911 break;
3912 re_time = got_object_tag_get_tagger_time(re_tag);
3913 got_object_tag_close(re_tag);
3916 while (se) {
3917 err = got_ref_resolve(&se_id, repo, re->ref);
3918 if (err)
3919 break;
3920 err = got_object_open_as_tag(&se_tag, repo, se_id);
3921 free(se_id);
3922 if (err)
3923 break;
3924 se_time = got_object_tag_get_tagger_time(se_tag);
3925 got_object_tag_close(se_tag);
3927 if (se_time > re_time) {
3928 err = got_reflist_entry_dup(&new, re);
3929 if (err)
3930 return err;
3931 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3932 break;
3934 se = SIMPLEQ_NEXT(se, entry);
3935 continue;
3938 done:
3939 return err;
3941 #endif
3943 static const struct got_error *
3944 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3946 static const struct got_error *err = NULL;
3947 struct got_reflist_head refs;
3948 struct got_reflist_entry *re;
3950 SIMPLEQ_INIT(&refs);
3952 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3953 if (err)
3954 return err;
3956 SIMPLEQ_FOREACH(re, &refs, entry) {
3957 const char *refname;
3958 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3959 char datebuf[26];
3960 const char *tagger;
3961 time_t tagger_time;
3962 struct got_object_id *id;
3963 struct got_tag_object *tag;
3964 struct got_commit_object *commit = NULL;
3966 refname = got_ref_get_name(re->ref);
3967 if (strncmp(refname, "refs/tags/", 10) != 0)
3968 continue;
3969 refname += 10;
3970 refstr = got_ref_to_str(re->ref);
3971 if (refstr == NULL) {
3972 err = got_error_from_errno("got_ref_to_str");
3973 break;
3975 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3976 free(refstr);
3978 err = got_ref_resolve(&id, repo, re->ref);
3979 if (err)
3980 break;
3981 err = got_object_open_as_tag(&tag, repo, id);
3982 if (err) {
3983 if (err->code != GOT_ERR_OBJ_TYPE) {
3984 free(id);
3985 break;
3987 /* "lightweight" tag */
3988 err = got_object_open_as_commit(&commit, repo, id);
3989 if (err) {
3990 free(id);
3991 break;
3993 tagger = got_object_commit_get_committer(commit);
3994 tagger_time =
3995 got_object_commit_get_committer_time(commit);
3996 err = got_object_id_str(&id_str, id);
3997 free(id);
3998 if (err)
3999 break;
4000 } else {
4001 free(id);
4002 tagger = got_object_tag_get_tagger(tag);
4003 tagger_time = got_object_tag_get_tagger_time(tag);
4004 err = got_object_id_str(&id_str,
4005 got_object_tag_get_object_id(tag));
4006 if (err)
4007 break;
4009 printf("from: %s\n", tagger);
4010 datestr = get_datestr(&tagger_time, datebuf);
4011 if (datestr)
4012 printf("date: %s UTC\n", datestr);
4013 if (commit)
4014 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4015 else {
4016 switch (got_object_tag_get_object_type(tag)) {
4017 case GOT_OBJ_TYPE_BLOB:
4018 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4019 id_str);
4020 break;
4021 case GOT_OBJ_TYPE_TREE:
4022 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4023 id_str);
4024 break;
4025 case GOT_OBJ_TYPE_COMMIT:
4026 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4027 id_str);
4028 break;
4029 case GOT_OBJ_TYPE_TAG:
4030 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4031 id_str);
4032 break;
4033 default:
4034 break;
4037 free(id_str);
4038 if (commit) {
4039 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4040 if (err)
4041 break;
4042 got_object_commit_close(commit);
4043 } else {
4044 tagmsg0 = strdup(got_object_tag_get_message(tag));
4045 got_object_tag_close(tag);
4046 if (tagmsg0 == NULL) {
4047 err = got_error_from_errno("strdup");
4048 break;
4052 tagmsg = tagmsg0;
4053 do {
4054 line = strsep(&tagmsg, "\n");
4055 if (line)
4056 printf(" %s\n", line);
4057 } while (line);
4058 free(tagmsg0);
4061 got_ref_list_free(&refs);
4062 return NULL;
4065 static const struct got_error *
4066 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4067 const char *tag_name, const char *repo_path)
4069 const struct got_error *err = NULL;
4070 char *template = NULL, *initial_content = NULL;
4071 char *editor = NULL;
4072 int fd = -1;
4074 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4075 err = got_error_from_errno("asprintf");
4076 goto done;
4079 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4080 commit_id_str, tag_name) == -1) {
4081 err = got_error_from_errno("asprintf");
4082 goto done;
4085 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4086 if (err)
4087 goto done;
4089 dprintf(fd, initial_content);
4090 close(fd);
4092 err = get_editor(&editor);
4093 if (err)
4094 goto done;
4095 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4096 done:
4097 free(initial_content);
4098 free(template);
4099 free(editor);
4101 /* Editor is done; we can now apply unveil(2) */
4102 if (err == NULL) {
4103 err = apply_unveil(repo_path, 0, NULL);
4104 if (err) {
4105 free(*tagmsg);
4106 *tagmsg = NULL;
4109 return err;
4112 static const struct got_error *
4113 add_tag(struct got_repository *repo, const char *tag_name,
4114 const char *commit_arg, const char *tagmsg_arg)
4116 const struct got_error *err = NULL;
4117 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4118 char *label = NULL, *commit_id_str = NULL;
4119 struct got_reference *ref = NULL;
4120 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4121 char *tagmsg_path = NULL, *tag_id_str = NULL;
4122 int preserve_tagmsg = 0;
4125 * Don't let the user create a tag name with a leading '-'.
4126 * While technically a valid reference name, this case is usually
4127 * an unintended typo.
4129 if (tag_name[0] == '-')
4130 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4132 err = get_author(&tagger, repo);
4133 if (err)
4134 return err;
4136 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4137 GOT_OBJ_TYPE_COMMIT, 1, repo);
4138 if (err)
4139 goto done;
4141 err = got_object_id_str(&commit_id_str, commit_id);
4142 if (err)
4143 goto done;
4145 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4146 refname = strdup(tag_name);
4147 if (refname == NULL) {
4148 err = got_error_from_errno("strdup");
4149 goto done;
4151 tag_name += 10;
4152 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4153 err = got_error_from_errno("asprintf");
4154 goto done;
4157 err = got_ref_open(&ref, repo, refname, 0);
4158 if (err == NULL) {
4159 err = got_error(GOT_ERR_TAG_EXISTS);
4160 goto done;
4161 } else if (err->code != GOT_ERR_NOT_REF)
4162 goto done;
4164 if (tagmsg_arg == NULL) {
4165 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4166 tag_name, got_repo_get_path(repo));
4167 if (err) {
4168 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4169 tagmsg_path != NULL)
4170 preserve_tagmsg = 1;
4171 goto done;
4175 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4176 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4177 if (err) {
4178 if (tagmsg_path)
4179 preserve_tagmsg = 1;
4180 goto done;
4183 err = got_ref_alloc(&ref, refname, tag_id);
4184 if (err) {
4185 if (tagmsg_path)
4186 preserve_tagmsg = 1;
4187 goto done;
4190 err = got_ref_write(ref, repo);
4191 if (err) {
4192 if (tagmsg_path)
4193 preserve_tagmsg = 1;
4194 goto done;
4197 err = got_object_id_str(&tag_id_str, tag_id);
4198 if (err) {
4199 if (tagmsg_path)
4200 preserve_tagmsg = 1;
4201 goto done;
4203 printf("Created tag %s\n", tag_id_str);
4204 done:
4205 if (preserve_tagmsg) {
4206 fprintf(stderr, "%s: tag message preserved in %s\n",
4207 getprogname(), tagmsg_path);
4208 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4209 err = got_error_from_errno2("unlink", tagmsg_path);
4210 free(tag_id_str);
4211 if (ref)
4212 got_ref_close(ref);
4213 free(commit_id);
4214 free(commit_id_str);
4215 free(refname);
4216 free(tagmsg);
4217 free(tagmsg_path);
4218 free(tagger);
4219 return err;
4222 static const struct got_error *
4223 cmd_tag(int argc, char *argv[])
4225 const struct got_error *error = NULL;
4226 struct got_repository *repo = NULL;
4227 struct got_worktree *worktree = NULL;
4228 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4229 char *gitconfig_path = NULL;
4230 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4231 int ch, do_list = 0;
4233 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4234 switch (ch) {
4235 case 'c':
4236 commit_id_arg = optarg;
4237 break;
4238 case 'm':
4239 tagmsg = optarg;
4240 break;
4241 case 'r':
4242 repo_path = realpath(optarg, NULL);
4243 if (repo_path == NULL)
4244 return got_error_from_errno2("realpath",
4245 optarg);
4246 got_path_strip_trailing_slashes(repo_path);
4247 break;
4248 case 'l':
4249 do_list = 1;
4250 break;
4251 default:
4252 usage_tag();
4253 /* NOTREACHED */
4257 argc -= optind;
4258 argv += optind;
4260 if (do_list) {
4261 if (commit_id_arg != NULL)
4262 errx(1, "-c option can only be used when creating a tag");
4263 if (tagmsg)
4264 errx(1, "-l and -m options are mutually exclusive");
4265 if (argc > 0)
4266 usage_tag();
4267 } else if (argc != 1)
4268 usage_tag();
4270 tag_name = argv[0];
4272 #ifndef PROFILE
4273 if (do_list) {
4274 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4275 NULL) == -1)
4276 err(1, "pledge");
4277 } else {
4278 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4279 "sendfd unveil", NULL) == -1)
4280 err(1, "pledge");
4282 #endif
4283 cwd = getcwd(NULL, 0);
4284 if (cwd == NULL) {
4285 error = got_error_from_errno("getcwd");
4286 goto done;
4289 if (repo_path == NULL) {
4290 error = got_worktree_open(&worktree, cwd);
4291 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4292 goto done;
4293 else
4294 error = NULL;
4295 if (worktree) {
4296 repo_path =
4297 strdup(got_worktree_get_repo_path(worktree));
4298 if (repo_path == NULL)
4299 error = got_error_from_errno("strdup");
4300 if (error)
4301 goto done;
4302 } else {
4303 repo_path = strdup(cwd);
4304 if (repo_path == NULL) {
4305 error = got_error_from_errno("strdup");
4306 goto done;
4311 if (do_list) {
4312 error = got_repo_open(&repo, repo_path, NULL);
4313 if (error != NULL)
4314 goto done;
4315 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4316 if (error)
4317 goto done;
4318 error = list_tags(repo, worktree);
4319 } else {
4320 error = get_gitconfig_path(&gitconfig_path);
4321 if (error)
4322 goto done;
4323 error = got_repo_open(&repo, repo_path, gitconfig_path);
4324 if (error != NULL)
4325 goto done;
4327 if (tagmsg) {
4328 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4329 if (error)
4330 goto done;
4333 if (commit_id_arg == NULL) {
4334 struct got_reference *head_ref;
4335 struct got_object_id *commit_id;
4336 error = got_ref_open(&head_ref, repo,
4337 worktree ? got_worktree_get_head_ref_name(worktree)
4338 : GOT_REF_HEAD, 0);
4339 if (error)
4340 goto done;
4341 error = got_ref_resolve(&commit_id, repo, head_ref);
4342 got_ref_close(head_ref);
4343 if (error)
4344 goto done;
4345 error = got_object_id_str(&commit_id_str, commit_id);
4346 free(commit_id);
4347 if (error)
4348 goto done;
4351 error = add_tag(repo, tag_name,
4352 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4354 done:
4355 if (repo)
4356 got_repo_close(repo);
4357 if (worktree)
4358 got_worktree_close(worktree);
4359 free(cwd);
4360 free(repo_path);
4361 free(gitconfig_path);
4362 free(commit_id_str);
4363 return error;
4366 __dead static void
4367 usage_add(void)
4369 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4370 getprogname());
4371 exit(1);
4374 static const struct got_error *
4375 add_progress(void *arg, unsigned char status, const char *path)
4377 while (path[0] == '/')
4378 path++;
4379 printf("%c %s\n", status, path);
4380 return NULL;
4383 static const struct got_error *
4384 cmd_add(int argc, char *argv[])
4386 const struct got_error *error = NULL;
4387 struct got_repository *repo = NULL;
4388 struct got_worktree *worktree = NULL;
4389 char *cwd = NULL;
4390 struct got_pathlist_head paths;
4391 struct got_pathlist_entry *pe;
4392 int ch, can_recurse = 0, no_ignores = 0;
4394 TAILQ_INIT(&paths);
4396 while ((ch = getopt(argc, argv, "IR")) != -1) {
4397 switch (ch) {
4398 case 'I':
4399 no_ignores = 1;
4400 break;
4401 case 'R':
4402 can_recurse = 1;
4403 break;
4404 default:
4405 usage_add();
4406 /* NOTREACHED */
4410 argc -= optind;
4411 argv += optind;
4413 #ifndef PROFILE
4414 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4415 NULL) == -1)
4416 err(1, "pledge");
4417 #endif
4418 if (argc < 1)
4419 usage_add();
4421 cwd = getcwd(NULL, 0);
4422 if (cwd == NULL) {
4423 error = got_error_from_errno("getcwd");
4424 goto done;
4427 error = got_worktree_open(&worktree, cwd);
4428 if (error)
4429 goto done;
4431 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4432 NULL);
4433 if (error != NULL)
4434 goto done;
4436 error = apply_unveil(got_repo_get_path(repo), 1,
4437 got_worktree_get_root_path(worktree));
4438 if (error)
4439 goto done;
4441 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4442 if (error)
4443 goto done;
4445 if (!can_recurse && no_ignores) {
4446 error = got_error_msg(GOT_ERR_BAD_PATH,
4447 "disregarding ignores requires -R option");
4448 goto done;
4452 if (!can_recurse) {
4453 char *ondisk_path;
4454 struct stat sb;
4455 TAILQ_FOREACH(pe, &paths, entry) {
4456 if (asprintf(&ondisk_path, "%s/%s",
4457 got_worktree_get_root_path(worktree),
4458 pe->path) == -1) {
4459 error = got_error_from_errno("asprintf");
4460 goto done;
4462 if (lstat(ondisk_path, &sb) == -1) {
4463 if (errno == ENOENT) {
4464 free(ondisk_path);
4465 continue;
4467 error = got_error_from_errno2("lstat",
4468 ondisk_path);
4469 free(ondisk_path);
4470 goto done;
4472 free(ondisk_path);
4473 if (S_ISDIR(sb.st_mode)) {
4474 error = got_error_msg(GOT_ERR_BAD_PATH,
4475 "adding directories requires -R option");
4476 goto done;
4481 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4482 NULL, repo, no_ignores);
4483 done:
4484 if (repo)
4485 got_repo_close(repo);
4486 if (worktree)
4487 got_worktree_close(worktree);
4488 TAILQ_FOREACH(pe, &paths, entry)
4489 free((char *)pe->path);
4490 got_pathlist_free(&paths);
4491 free(cwd);
4492 return error;
4495 __dead static void
4496 usage_remove(void)
4498 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4499 getprogname());
4500 exit(1);
4503 static const struct got_error *
4504 print_remove_status(void *arg, unsigned char status,
4505 unsigned char staged_status, const char *path)
4507 while (path[0] == '/')
4508 path++;
4509 if (status == GOT_STATUS_NONEXISTENT)
4510 return NULL;
4511 if (status == staged_status && (status == GOT_STATUS_DELETE))
4512 status = GOT_STATUS_NO_CHANGE;
4513 printf("%c%c %s\n", status, staged_status, path);
4514 return NULL;
4517 static const struct got_error *
4518 cmd_remove(int argc, char *argv[])
4520 const struct got_error *error = NULL;
4521 struct got_worktree *worktree = NULL;
4522 struct got_repository *repo = NULL;
4523 char *cwd = NULL;
4524 struct got_pathlist_head paths;
4525 struct got_pathlist_entry *pe;
4526 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4528 TAILQ_INIT(&paths);
4530 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4531 switch (ch) {
4532 case 'f':
4533 delete_local_mods = 1;
4534 break;
4535 case 'k':
4536 keep_on_disk = 1;
4537 break;
4538 case 'R':
4539 can_recurse = 1;
4540 break;
4541 default:
4542 usage_remove();
4543 /* NOTREACHED */
4547 argc -= optind;
4548 argv += optind;
4550 #ifndef PROFILE
4551 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4552 NULL) == -1)
4553 err(1, "pledge");
4554 #endif
4555 if (argc < 1)
4556 usage_remove();
4558 cwd = getcwd(NULL, 0);
4559 if (cwd == NULL) {
4560 error = got_error_from_errno("getcwd");
4561 goto done;
4563 error = got_worktree_open(&worktree, cwd);
4564 if (error)
4565 goto done;
4567 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4568 NULL);
4569 if (error)
4570 goto done;
4572 error = apply_unveil(got_repo_get_path(repo), 1,
4573 got_worktree_get_root_path(worktree));
4574 if (error)
4575 goto done;
4577 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4578 if (error)
4579 goto done;
4581 if (!can_recurse) {
4582 char *ondisk_path;
4583 struct stat sb;
4584 TAILQ_FOREACH(pe, &paths, entry) {
4585 if (asprintf(&ondisk_path, "%s/%s",
4586 got_worktree_get_root_path(worktree),
4587 pe->path) == -1) {
4588 error = got_error_from_errno("asprintf");
4589 goto done;
4591 if (lstat(ondisk_path, &sb) == -1) {
4592 if (errno == ENOENT) {
4593 free(ondisk_path);
4594 continue;
4596 error = got_error_from_errno2("lstat",
4597 ondisk_path);
4598 free(ondisk_path);
4599 goto done;
4601 free(ondisk_path);
4602 if (S_ISDIR(sb.st_mode)) {
4603 error = got_error_msg(GOT_ERR_BAD_PATH,
4604 "removing directories requires -R option");
4605 goto done;
4610 error = got_worktree_schedule_delete(worktree, &paths,
4611 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4612 done:
4613 if (repo)
4614 got_repo_close(repo);
4615 if (worktree)
4616 got_worktree_close(worktree);
4617 TAILQ_FOREACH(pe, &paths, entry)
4618 free((char *)pe->path);
4619 got_pathlist_free(&paths);
4620 free(cwd);
4621 return error;
4624 __dead static void
4625 usage_revert(void)
4627 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4628 "path ...\n", getprogname());
4629 exit(1);
4632 static const struct got_error *
4633 revert_progress(void *arg, unsigned char status, const char *path)
4635 if (status == GOT_STATUS_UNVERSIONED)
4636 return NULL;
4638 while (path[0] == '/')
4639 path++;
4640 printf("%c %s\n", status, path);
4641 return NULL;
4644 struct choose_patch_arg {
4645 FILE *patch_script_file;
4646 const char *action;
4649 static const struct got_error *
4650 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4651 int nchanges, const char *action)
4653 char *line = NULL;
4654 size_t linesize = 0;
4655 ssize_t linelen;
4657 switch (status) {
4658 case GOT_STATUS_ADD:
4659 printf("A %s\n%s this addition? [y/n] ", path, action);
4660 break;
4661 case GOT_STATUS_DELETE:
4662 printf("D %s\n%s this deletion? [y/n] ", path, action);
4663 break;
4664 case GOT_STATUS_MODIFY:
4665 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4666 return got_error_from_errno("fseek");
4667 printf(GOT_COMMIT_SEP_STR);
4668 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4669 printf("%s", line);
4670 if (ferror(patch_file))
4671 return got_error_from_errno("getline");
4672 printf(GOT_COMMIT_SEP_STR);
4673 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4674 path, n, nchanges, action);
4675 break;
4676 default:
4677 return got_error_path(path, GOT_ERR_FILE_STATUS);
4680 return NULL;
4683 static const struct got_error *
4684 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4685 FILE *patch_file, int n, int nchanges)
4687 const struct got_error *err = NULL;
4688 char *line = NULL;
4689 size_t linesize = 0;
4690 ssize_t linelen;
4691 int resp = ' ';
4692 struct choose_patch_arg *a = arg;
4694 *choice = GOT_PATCH_CHOICE_NONE;
4696 if (a->patch_script_file) {
4697 char *nl;
4698 err = show_change(status, path, patch_file, n, nchanges,
4699 a->action);
4700 if (err)
4701 return err;
4702 linelen = getline(&line, &linesize, a->patch_script_file);
4703 if (linelen == -1) {
4704 if (ferror(a->patch_script_file))
4705 return got_error_from_errno("getline");
4706 return NULL;
4708 nl = strchr(line, '\n');
4709 if (nl)
4710 *nl = '\0';
4711 if (strcmp(line, "y") == 0) {
4712 *choice = GOT_PATCH_CHOICE_YES;
4713 printf("y\n");
4714 } else if (strcmp(line, "n") == 0) {
4715 *choice = GOT_PATCH_CHOICE_NO;
4716 printf("n\n");
4717 } else if (strcmp(line, "q") == 0 &&
4718 status == GOT_STATUS_MODIFY) {
4719 *choice = GOT_PATCH_CHOICE_QUIT;
4720 printf("q\n");
4721 } else
4722 printf("invalid response '%s'\n", line);
4723 free(line);
4724 return NULL;
4727 while (resp != 'y' && resp != 'n' && resp != 'q') {
4728 err = show_change(status, path, patch_file, n, nchanges,
4729 a->action);
4730 if (err)
4731 return err;
4732 resp = getchar();
4733 if (resp == '\n')
4734 resp = getchar();
4735 if (status == GOT_STATUS_MODIFY) {
4736 if (resp != 'y' && resp != 'n' && resp != 'q') {
4737 printf("invalid response '%c'\n", resp);
4738 resp = ' ';
4740 } else if (resp != 'y' && resp != 'n') {
4741 printf("invalid response '%c'\n", resp);
4742 resp = ' ';
4746 if (resp == 'y')
4747 *choice = GOT_PATCH_CHOICE_YES;
4748 else if (resp == 'n')
4749 *choice = GOT_PATCH_CHOICE_NO;
4750 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4751 *choice = GOT_PATCH_CHOICE_QUIT;
4753 return NULL;
4757 static const struct got_error *
4758 cmd_revert(int argc, char *argv[])
4760 const struct got_error *error = NULL;
4761 struct got_worktree *worktree = NULL;
4762 struct got_repository *repo = NULL;
4763 char *cwd = NULL, *path = NULL;
4764 struct got_pathlist_head paths;
4765 struct got_pathlist_entry *pe;
4766 int ch, can_recurse = 0, pflag = 0;
4767 FILE *patch_script_file = NULL;
4768 const char *patch_script_path = NULL;
4769 struct choose_patch_arg cpa;
4771 TAILQ_INIT(&paths);
4773 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4774 switch (ch) {
4775 case 'p':
4776 pflag = 1;
4777 break;
4778 case 'F':
4779 patch_script_path = optarg;
4780 break;
4781 case 'R':
4782 can_recurse = 1;
4783 break;
4784 default:
4785 usage_revert();
4786 /* NOTREACHED */
4790 argc -= optind;
4791 argv += optind;
4793 #ifndef PROFILE
4794 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4795 "unveil", NULL) == -1)
4796 err(1, "pledge");
4797 #endif
4798 if (argc < 1)
4799 usage_revert();
4800 if (patch_script_path && !pflag)
4801 errx(1, "-F option can only be used together with -p option");
4803 cwd = getcwd(NULL, 0);
4804 if (cwd == NULL) {
4805 error = got_error_from_errno("getcwd");
4806 goto done;
4808 error = got_worktree_open(&worktree, cwd);
4809 if (error)
4810 goto done;
4812 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4813 NULL);
4814 if (error != NULL)
4815 goto done;
4817 if (patch_script_path) {
4818 patch_script_file = fopen(patch_script_path, "r");
4819 if (patch_script_file == NULL) {
4820 error = got_error_from_errno2("fopen",
4821 patch_script_path);
4822 goto done;
4825 error = apply_unveil(got_repo_get_path(repo), 1,
4826 got_worktree_get_root_path(worktree));
4827 if (error)
4828 goto done;
4830 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4831 if (error)
4832 goto done;
4834 if (!can_recurse) {
4835 char *ondisk_path;
4836 struct stat sb;
4837 TAILQ_FOREACH(pe, &paths, entry) {
4838 if (asprintf(&ondisk_path, "%s/%s",
4839 got_worktree_get_root_path(worktree),
4840 pe->path) == -1) {
4841 error = got_error_from_errno("asprintf");
4842 goto done;
4844 if (lstat(ondisk_path, &sb) == -1) {
4845 if (errno == ENOENT) {
4846 free(ondisk_path);
4847 continue;
4849 error = got_error_from_errno2("lstat",
4850 ondisk_path);
4851 free(ondisk_path);
4852 goto done;
4854 free(ondisk_path);
4855 if (S_ISDIR(sb.st_mode)) {
4856 error = got_error_msg(GOT_ERR_BAD_PATH,
4857 "reverting directories requires -R option");
4858 goto done;
4863 cpa.patch_script_file = patch_script_file;
4864 cpa.action = "revert";
4865 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4866 pflag ? choose_patch : NULL, &cpa, repo);
4867 done:
4868 if (patch_script_file && fclose(patch_script_file) == EOF &&
4869 error == NULL)
4870 error = got_error_from_errno2("fclose", patch_script_path);
4871 if (repo)
4872 got_repo_close(repo);
4873 if (worktree)
4874 got_worktree_close(worktree);
4875 free(path);
4876 free(cwd);
4877 return error;
4880 __dead static void
4881 usage_commit(void)
4883 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4884 getprogname());
4885 exit(1);
4888 struct collect_commit_logmsg_arg {
4889 const char *cmdline_log;
4890 const char *editor;
4891 const char *worktree_path;
4892 const char *branch_name;
4893 const char *repo_path;
4894 char *logmsg_path;
4898 static const struct got_error *
4899 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4900 void *arg)
4902 char *initial_content = NULL;
4903 struct got_pathlist_entry *pe;
4904 const struct got_error *err = NULL;
4905 char *template = NULL;
4906 struct collect_commit_logmsg_arg *a = arg;
4907 int fd;
4908 size_t len;
4910 /* if a message was specified on the command line, just use it */
4911 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4912 len = strlen(a->cmdline_log) + 1;
4913 *logmsg = malloc(len + 1);
4914 if (*logmsg == NULL)
4915 return got_error_from_errno("malloc");
4916 strlcpy(*logmsg, a->cmdline_log, len);
4917 return NULL;
4920 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4921 return got_error_from_errno("asprintf");
4923 if (asprintf(&initial_content,
4924 "\n# changes to be committed on branch %s:\n",
4925 a->branch_name) == -1)
4926 return got_error_from_errno("asprintf");
4928 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4929 if (err)
4930 goto done;
4932 dprintf(fd, initial_content);
4934 TAILQ_FOREACH(pe, commitable_paths, entry) {
4935 struct got_commitable *ct = pe->data;
4936 dprintf(fd, "# %c %s\n",
4937 got_commitable_get_status(ct),
4938 got_commitable_get_path(ct));
4940 close(fd);
4942 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4943 done:
4944 free(initial_content);
4945 free(template);
4947 /* Editor is done; we can now apply unveil(2) */
4948 if (err == NULL) {
4949 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4950 if (err) {
4951 free(*logmsg);
4952 *logmsg = NULL;
4955 return err;
4958 static const struct got_error *
4959 cmd_commit(int argc, char *argv[])
4961 const struct got_error *error = NULL;
4962 struct got_worktree *worktree = NULL;
4963 struct got_repository *repo = NULL;
4964 char *cwd = NULL, *id_str = NULL;
4965 struct got_object_id *id = NULL;
4966 const char *logmsg = NULL;
4967 struct collect_commit_logmsg_arg cl_arg;
4968 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4969 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4970 struct got_pathlist_head paths;
4972 TAILQ_INIT(&paths);
4973 cl_arg.logmsg_path = NULL;
4975 while ((ch = getopt(argc, argv, "m:")) != -1) {
4976 switch (ch) {
4977 case 'm':
4978 logmsg = optarg;
4979 break;
4980 default:
4981 usage_commit();
4982 /* NOTREACHED */
4986 argc -= optind;
4987 argv += optind;
4989 #ifndef PROFILE
4990 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4991 "unveil", NULL) == -1)
4992 err(1, "pledge");
4993 #endif
4994 cwd = getcwd(NULL, 0);
4995 if (cwd == NULL) {
4996 error = got_error_from_errno("getcwd");
4997 goto done;
4999 error = got_worktree_open(&worktree, cwd);
5000 if (error)
5001 goto done;
5003 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5004 if (error)
5005 goto done;
5006 if (rebase_in_progress) {
5007 error = got_error(GOT_ERR_REBASING);
5008 goto done;
5011 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5012 worktree);
5013 if (error)
5014 goto done;
5016 error = get_gitconfig_path(&gitconfig_path);
5017 if (error)
5018 goto done;
5019 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5020 gitconfig_path);
5021 if (error != NULL)
5022 goto done;
5024 error = get_author(&author, repo);
5025 if (error)
5026 return error;
5029 * unveil(2) traverses exec(2); if an editor is used we have
5030 * to apply unveil after the log message has been written.
5032 if (logmsg == NULL || strlen(logmsg) == 0)
5033 error = get_editor(&editor);
5034 else
5035 error = apply_unveil(got_repo_get_path(repo), 0,
5036 got_worktree_get_root_path(worktree));
5037 if (error)
5038 goto done;
5040 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5041 if (error)
5042 goto done;
5044 cl_arg.editor = editor;
5045 cl_arg.cmdline_log = logmsg;
5046 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5047 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5048 if (!histedit_in_progress) {
5049 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5050 error = got_error(GOT_ERR_COMMIT_BRANCH);
5051 goto done;
5053 cl_arg.branch_name += 11;
5055 cl_arg.repo_path = got_repo_get_path(repo);
5056 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5057 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5058 if (error) {
5059 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5060 cl_arg.logmsg_path != NULL)
5061 preserve_logmsg = 1;
5062 goto done;
5065 error = got_object_id_str(&id_str, id);
5066 if (error)
5067 goto done;
5068 printf("Created commit %s\n", id_str);
5069 done:
5070 if (preserve_logmsg) {
5071 fprintf(stderr, "%s: log message preserved in %s\n",
5072 getprogname(), cl_arg.logmsg_path);
5073 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5074 error == NULL)
5075 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5076 free(cl_arg.logmsg_path);
5077 if (repo)
5078 got_repo_close(repo);
5079 if (worktree)
5080 got_worktree_close(worktree);
5081 free(cwd);
5082 free(id_str);
5083 free(gitconfig_path);
5084 free(editor);
5085 free(author);
5086 return error;
5089 __dead static void
5090 usage_cherrypick(void)
5092 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5093 exit(1);
5096 static const struct got_error *
5097 cmd_cherrypick(int argc, char *argv[])
5099 const struct got_error *error = NULL;
5100 struct got_worktree *worktree = NULL;
5101 struct got_repository *repo = NULL;
5102 char *cwd = NULL, *commit_id_str = NULL;
5103 struct got_object_id *commit_id = NULL;
5104 struct got_commit_object *commit = NULL;
5105 struct got_object_qid *pid;
5106 struct got_reference *head_ref = NULL;
5107 int ch, did_something = 0;
5109 while ((ch = getopt(argc, argv, "")) != -1) {
5110 switch (ch) {
5111 default:
5112 usage_cherrypick();
5113 /* NOTREACHED */
5117 argc -= optind;
5118 argv += optind;
5120 #ifndef PROFILE
5121 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5122 "unveil", NULL) == -1)
5123 err(1, "pledge");
5124 #endif
5125 if (argc != 1)
5126 usage_cherrypick();
5128 cwd = getcwd(NULL, 0);
5129 if (cwd == NULL) {
5130 error = got_error_from_errno("getcwd");
5131 goto done;
5133 error = got_worktree_open(&worktree, cwd);
5134 if (error)
5135 goto done;
5137 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5138 NULL);
5139 if (error != NULL)
5140 goto done;
5142 error = apply_unveil(got_repo_get_path(repo), 0,
5143 got_worktree_get_root_path(worktree));
5144 if (error)
5145 goto done;
5147 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5148 GOT_OBJ_TYPE_COMMIT, repo);
5149 if (error != NULL) {
5150 struct got_reference *ref;
5151 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5152 goto done;
5153 error = got_ref_open(&ref, repo, argv[0], 0);
5154 if (error != NULL)
5155 goto done;
5156 error = got_ref_resolve(&commit_id, repo, ref);
5157 got_ref_close(ref);
5158 if (error != NULL)
5159 goto done;
5161 error = got_object_id_str(&commit_id_str, commit_id);
5162 if (error)
5163 goto done;
5165 error = got_ref_open(&head_ref, repo,
5166 got_worktree_get_head_ref_name(worktree), 0);
5167 if (error != NULL)
5168 goto done;
5170 error = check_same_branch(commit_id, head_ref, NULL, repo);
5171 if (error) {
5172 if (error->code != GOT_ERR_ANCESTRY)
5173 goto done;
5174 error = NULL;
5175 } else {
5176 error = got_error(GOT_ERR_SAME_BRANCH);
5177 goto done;
5180 error = got_object_open_as_commit(&commit, repo, commit_id);
5181 if (error)
5182 goto done;
5183 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5184 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5185 commit_id, repo, update_progress, &did_something, check_cancelled,
5186 NULL);
5187 if (error != NULL)
5188 goto done;
5190 if (did_something)
5191 printf("Merged commit %s\n", commit_id_str);
5192 done:
5193 if (commit)
5194 got_object_commit_close(commit);
5195 free(commit_id_str);
5196 if (head_ref)
5197 got_ref_close(head_ref);
5198 if (worktree)
5199 got_worktree_close(worktree);
5200 if (repo)
5201 got_repo_close(repo);
5202 return error;
5205 __dead static void
5206 usage_backout(void)
5208 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5209 exit(1);
5212 static const struct got_error *
5213 cmd_backout(int argc, char *argv[])
5215 const struct got_error *error = NULL;
5216 struct got_worktree *worktree = NULL;
5217 struct got_repository *repo = NULL;
5218 char *cwd = NULL, *commit_id_str = NULL;
5219 struct got_object_id *commit_id = NULL;
5220 struct got_commit_object *commit = NULL;
5221 struct got_object_qid *pid;
5222 struct got_reference *head_ref = NULL;
5223 int ch, did_something = 0;
5225 while ((ch = getopt(argc, argv, "")) != -1) {
5226 switch (ch) {
5227 default:
5228 usage_backout();
5229 /* NOTREACHED */
5233 argc -= optind;
5234 argv += optind;
5236 #ifndef PROFILE
5237 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5238 "unveil", NULL) == -1)
5239 err(1, "pledge");
5240 #endif
5241 if (argc != 1)
5242 usage_backout();
5244 cwd = getcwd(NULL, 0);
5245 if (cwd == NULL) {
5246 error = got_error_from_errno("getcwd");
5247 goto done;
5249 error = got_worktree_open(&worktree, cwd);
5250 if (error)
5251 goto done;
5253 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5254 NULL);
5255 if (error != NULL)
5256 goto done;
5258 error = apply_unveil(got_repo_get_path(repo), 0,
5259 got_worktree_get_root_path(worktree));
5260 if (error)
5261 goto done;
5263 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5264 GOT_OBJ_TYPE_COMMIT, repo);
5265 if (error != NULL) {
5266 struct got_reference *ref;
5267 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5268 goto done;
5269 error = got_ref_open(&ref, repo, argv[0], 0);
5270 if (error != NULL)
5271 goto done;
5272 error = got_ref_resolve(&commit_id, repo, ref);
5273 got_ref_close(ref);
5274 if (error != NULL)
5275 goto done;
5277 error = got_object_id_str(&commit_id_str, commit_id);
5278 if (error)
5279 goto done;
5281 error = got_ref_open(&head_ref, repo,
5282 got_worktree_get_head_ref_name(worktree), 0);
5283 if (error != NULL)
5284 goto done;
5286 error = check_same_branch(commit_id, head_ref, NULL, repo);
5287 if (error)
5288 goto done;
5290 error = got_object_open_as_commit(&commit, repo, commit_id);
5291 if (error)
5292 goto done;
5293 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5294 if (pid == NULL) {
5295 error = got_error(GOT_ERR_ROOT_COMMIT);
5296 goto done;
5299 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5300 update_progress, &did_something, check_cancelled, NULL);
5301 if (error != NULL)
5302 goto done;
5304 if (did_something)
5305 printf("Backed out commit %s\n", commit_id_str);
5306 done:
5307 if (commit)
5308 got_object_commit_close(commit);
5309 free(commit_id_str);
5310 if (head_ref)
5311 got_ref_close(head_ref);
5312 if (worktree)
5313 got_worktree_close(worktree);
5314 if (repo)
5315 got_repo_close(repo);
5316 return error;
5319 __dead static void
5320 usage_rebase(void)
5322 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5323 getprogname());
5324 exit(1);
5327 void
5328 trim_logmsg(char *logmsg, int limit)
5330 char *nl;
5331 size_t len;
5333 len = strlen(logmsg);
5334 if (len > limit)
5335 len = limit;
5336 logmsg[len] = '\0';
5337 nl = strchr(logmsg, '\n');
5338 if (nl)
5339 *nl = '\0';
5342 static const struct got_error *
5343 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5345 const struct got_error *err;
5346 char *logmsg0 = NULL;
5347 const char *s;
5349 err = got_object_commit_get_logmsg(&logmsg0, commit);
5350 if (err)
5351 return err;
5353 s = logmsg0;
5354 while (isspace((unsigned char)s[0]))
5355 s++;
5357 *logmsg = strdup(s);
5358 if (*logmsg == NULL) {
5359 err = got_error_from_errno("strdup");
5360 goto done;
5363 trim_logmsg(*logmsg, limit);
5364 done:
5365 free(logmsg0);
5366 return err;
5369 static const struct got_error *
5370 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5372 const struct got_error *err;
5373 struct got_commit_object *commit = NULL;
5374 char *id_str = NULL, *logmsg = NULL;
5376 err = got_object_open_as_commit(&commit, repo, id);
5377 if (err)
5378 return err;
5380 err = got_object_id_str(&id_str, id);
5381 if (err)
5382 goto done;
5384 id_str[12] = '\0';
5386 err = get_short_logmsg(&logmsg, 42, commit);
5387 if (err)
5388 goto done;
5390 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5391 done:
5392 free(id_str);
5393 got_object_commit_close(commit);
5394 free(logmsg);
5395 return err;
5398 static const struct got_error *
5399 show_rebase_progress(struct got_commit_object *commit,
5400 struct got_object_id *old_id, struct got_object_id *new_id)
5402 const struct got_error *err;
5403 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5405 err = got_object_id_str(&old_id_str, old_id);
5406 if (err)
5407 goto done;
5409 if (new_id) {
5410 err = got_object_id_str(&new_id_str, new_id);
5411 if (err)
5412 goto done;
5415 old_id_str[12] = '\0';
5416 if (new_id_str)
5417 new_id_str[12] = '\0';
5419 err = get_short_logmsg(&logmsg, 42, commit);
5420 if (err)
5421 goto done;
5423 printf("%s -> %s: %s\n", old_id_str,
5424 new_id_str ? new_id_str : "no-op change", logmsg);
5425 done:
5426 free(old_id_str);
5427 free(new_id_str);
5428 free(logmsg);
5429 return err;
5432 static const struct got_error *
5433 rebase_progress(void *arg, unsigned char status, const char *path)
5435 unsigned char *rebase_status = arg;
5437 while (path[0] == '/')
5438 path++;
5439 printf("%c %s\n", status, path);
5441 if (*rebase_status == GOT_STATUS_CONFLICT)
5442 return NULL;
5443 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5444 *rebase_status = status;
5445 return NULL;
5448 static const struct got_error *
5449 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5450 struct got_reference *branch, struct got_reference *new_base_branch,
5451 struct got_reference *tmp_branch, struct got_repository *repo)
5453 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5454 return got_worktree_rebase_complete(worktree, fileindex,
5455 new_base_branch, tmp_branch, branch, repo);
5458 static const struct got_error *
5459 rebase_commit(struct got_pathlist_head *merged_paths,
5460 struct got_worktree *worktree, struct got_fileindex *fileindex,
5461 struct got_reference *tmp_branch,
5462 struct got_object_id *commit_id, struct got_repository *repo)
5464 const struct got_error *error;
5465 struct got_commit_object *commit;
5466 struct got_object_id *new_commit_id;
5468 error = got_object_open_as_commit(&commit, repo, commit_id);
5469 if (error)
5470 return error;
5472 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5473 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5474 if (error) {
5475 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5476 goto done;
5477 error = show_rebase_progress(commit, commit_id, NULL);
5478 } else {
5479 error = show_rebase_progress(commit, commit_id, new_commit_id);
5480 free(new_commit_id);
5482 done:
5483 got_object_commit_close(commit);
5484 return error;
5487 struct check_path_prefix_arg {
5488 const char *path_prefix;
5489 size_t len;
5490 int errcode;
5493 static const struct got_error *
5494 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5495 struct got_blob_object *blob2, struct got_object_id *id1,
5496 struct got_object_id *id2, const char *path1, const char *path2,
5497 mode_t mode1, mode_t mode2, struct got_repository *repo)
5499 struct check_path_prefix_arg *a = arg;
5501 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5502 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5503 return got_error(a->errcode);
5505 return NULL;
5508 static const struct got_error *
5509 check_path_prefix(struct got_object_id *parent_id,
5510 struct got_object_id *commit_id, const char *path_prefix,
5511 int errcode, struct got_repository *repo)
5513 const struct got_error *err;
5514 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5515 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5516 struct check_path_prefix_arg cpp_arg;
5518 if (got_path_is_root_dir(path_prefix))
5519 return NULL;
5521 err = got_object_open_as_commit(&commit, repo, commit_id);
5522 if (err)
5523 goto done;
5525 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5526 if (err)
5527 goto done;
5529 err = got_object_open_as_tree(&tree1, repo,
5530 got_object_commit_get_tree_id(parent_commit));
5531 if (err)
5532 goto done;
5534 err = got_object_open_as_tree(&tree2, repo,
5535 got_object_commit_get_tree_id(commit));
5536 if (err)
5537 goto done;
5539 cpp_arg.path_prefix = path_prefix;
5540 while (cpp_arg.path_prefix[0] == '/')
5541 cpp_arg.path_prefix++;
5542 cpp_arg.len = strlen(cpp_arg.path_prefix);
5543 cpp_arg.errcode = errcode;
5544 err = got_diff_tree(tree1, tree2, "", "", repo,
5545 check_path_prefix_in_diff, &cpp_arg, 0);
5546 done:
5547 if (tree1)
5548 got_object_tree_close(tree1);
5549 if (tree2)
5550 got_object_tree_close(tree2);
5551 if (commit)
5552 got_object_commit_close(commit);
5553 if (parent_commit)
5554 got_object_commit_close(parent_commit);
5555 return err;
5558 static const struct got_error *
5559 collect_commits(struct got_object_id_queue *commits,
5560 struct got_object_id *initial_commit_id,
5561 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5562 const char *path_prefix, int path_prefix_errcode,
5563 struct got_repository *repo)
5565 const struct got_error *err = NULL;
5566 struct got_commit_graph *graph = NULL;
5567 struct got_object_id *parent_id = NULL;
5568 struct got_object_qid *qid;
5569 struct got_object_id *commit_id = initial_commit_id;
5571 err = got_commit_graph_open(&graph, "/", 1);
5572 if (err)
5573 return err;
5575 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5576 check_cancelled, NULL);
5577 if (err)
5578 goto done;
5579 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5580 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5581 check_cancelled, NULL);
5582 if (err) {
5583 if (err->code == GOT_ERR_ITER_COMPLETED) {
5584 err = got_error_msg(GOT_ERR_ANCESTRY,
5585 "ran out of commits to rebase before "
5586 "youngest common ancestor commit has "
5587 "been reached?!?");
5589 goto done;
5590 } else {
5591 err = check_path_prefix(parent_id, commit_id,
5592 path_prefix, path_prefix_errcode, repo);
5593 if (err)
5594 goto done;
5596 err = got_object_qid_alloc(&qid, commit_id);
5597 if (err)
5598 goto done;
5599 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5600 commit_id = parent_id;
5603 done:
5604 got_commit_graph_close(graph);
5605 return err;
5608 static const struct got_error *
5609 cmd_rebase(int argc, char *argv[])
5611 const struct got_error *error = NULL;
5612 struct got_worktree *worktree = NULL;
5613 struct got_repository *repo = NULL;
5614 struct got_fileindex *fileindex = NULL;
5615 char *cwd = NULL;
5616 struct got_reference *branch = NULL;
5617 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5618 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5619 struct got_object_id *resume_commit_id = NULL;
5620 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5621 struct got_commit_object *commit = NULL;
5622 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5623 int histedit_in_progress = 0;
5624 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5625 struct got_object_id_queue commits;
5626 struct got_pathlist_head merged_paths;
5627 const struct got_object_id_queue *parent_ids;
5628 struct got_object_qid *qid, *pid;
5630 SIMPLEQ_INIT(&commits);
5631 TAILQ_INIT(&merged_paths);
5633 while ((ch = getopt(argc, argv, "ac")) != -1) {
5634 switch (ch) {
5635 case 'a':
5636 abort_rebase = 1;
5637 break;
5638 case 'c':
5639 continue_rebase = 1;
5640 break;
5641 default:
5642 usage_rebase();
5643 /* NOTREACHED */
5647 argc -= optind;
5648 argv += optind;
5650 #ifndef PROFILE
5651 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5652 "unveil", NULL) == -1)
5653 err(1, "pledge");
5654 #endif
5655 if (abort_rebase && continue_rebase)
5656 usage_rebase();
5657 else if (abort_rebase || continue_rebase) {
5658 if (argc != 0)
5659 usage_rebase();
5660 } else if (argc != 1)
5661 usage_rebase();
5663 cwd = getcwd(NULL, 0);
5664 if (cwd == NULL) {
5665 error = got_error_from_errno("getcwd");
5666 goto done;
5668 error = got_worktree_open(&worktree, cwd);
5669 if (error)
5670 goto done;
5672 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5673 NULL);
5674 if (error != NULL)
5675 goto done;
5677 error = apply_unveil(got_repo_get_path(repo), 0,
5678 got_worktree_get_root_path(worktree));
5679 if (error)
5680 goto done;
5682 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5683 worktree);
5684 if (error)
5685 goto done;
5686 if (histedit_in_progress) {
5687 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5688 goto done;
5691 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5692 if (error)
5693 goto done;
5695 if (abort_rebase) {
5696 int did_something;
5697 if (!rebase_in_progress) {
5698 error = got_error(GOT_ERR_NOT_REBASING);
5699 goto done;
5701 error = got_worktree_rebase_continue(&resume_commit_id,
5702 &new_base_branch, &tmp_branch, &branch, &fileindex,
5703 worktree, repo);
5704 if (error)
5705 goto done;
5706 printf("Switching work tree to %s\n",
5707 got_ref_get_symref_target(new_base_branch));
5708 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5709 new_base_branch, update_progress, &did_something);
5710 if (error)
5711 goto done;
5712 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5713 goto done; /* nothing else to do */
5716 if (continue_rebase) {
5717 if (!rebase_in_progress) {
5718 error = got_error(GOT_ERR_NOT_REBASING);
5719 goto done;
5721 error = got_worktree_rebase_continue(&resume_commit_id,
5722 &new_base_branch, &tmp_branch, &branch, &fileindex,
5723 worktree, repo);
5724 if (error)
5725 goto done;
5727 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5728 resume_commit_id, repo);
5729 if (error)
5730 goto done;
5732 yca_id = got_object_id_dup(resume_commit_id);
5733 if (yca_id == NULL) {
5734 error = got_error_from_errno("got_object_id_dup");
5735 goto done;
5737 } else {
5738 error = got_ref_open(&branch, repo, argv[0], 0);
5739 if (error != NULL)
5740 goto done;
5743 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5744 if (error)
5745 goto done;
5747 if (!continue_rebase) {
5748 struct got_object_id *base_commit_id;
5750 base_commit_id = got_worktree_get_base_commit_id(worktree);
5751 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5752 base_commit_id, branch_head_commit_id, repo,
5753 check_cancelled, NULL);
5754 if (error)
5755 goto done;
5756 if (yca_id == NULL) {
5757 error = got_error_msg(GOT_ERR_ANCESTRY,
5758 "specified branch shares no common ancestry "
5759 "with work tree's branch");
5760 goto done;
5763 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5764 if (error) {
5765 if (error->code != GOT_ERR_ANCESTRY)
5766 goto done;
5767 error = NULL;
5768 } else {
5769 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5770 "specified branch resolves to a commit which "
5771 "is already contained in work tree's branch");
5772 goto done;
5774 error = got_worktree_rebase_prepare(&new_base_branch,
5775 &tmp_branch, &fileindex, worktree, branch, repo);
5776 if (error)
5777 goto done;
5780 commit_id = branch_head_commit_id;
5781 error = got_object_open_as_commit(&commit, repo, commit_id);
5782 if (error)
5783 goto done;
5785 parent_ids = got_object_commit_get_parent_ids(commit);
5786 pid = SIMPLEQ_FIRST(parent_ids);
5787 if (pid == NULL) {
5788 if (!continue_rebase) {
5789 int did_something;
5790 error = got_worktree_rebase_abort(worktree, fileindex,
5791 repo, new_base_branch, update_progress,
5792 &did_something);
5793 if (error)
5794 goto done;
5795 printf("Rebase of %s aborted\n",
5796 got_ref_get_name(branch));
5798 error = got_error(GOT_ERR_EMPTY_REBASE);
5799 goto done;
5801 error = collect_commits(&commits, commit_id, pid->id,
5802 yca_id, got_worktree_get_path_prefix(worktree),
5803 GOT_ERR_REBASE_PATH, repo);
5804 got_object_commit_close(commit);
5805 commit = NULL;
5806 if (error)
5807 goto done;
5809 if (SIMPLEQ_EMPTY(&commits)) {
5810 if (continue_rebase) {
5811 error = rebase_complete(worktree, fileindex,
5812 branch, new_base_branch, tmp_branch, repo);
5813 goto done;
5814 } else {
5815 /* Fast-forward the reference of the branch. */
5816 struct got_object_id *new_head_commit_id;
5817 char *id_str;
5818 error = got_ref_resolve(&new_head_commit_id, repo,
5819 new_base_branch);
5820 if (error)
5821 goto done;
5822 error = got_object_id_str(&id_str, new_head_commit_id);
5823 printf("Forwarding %s to commit %s\n",
5824 got_ref_get_name(branch), id_str);
5825 free(id_str);
5826 error = got_ref_change_ref(branch,
5827 new_head_commit_id);
5828 if (error)
5829 goto done;
5833 pid = NULL;
5834 SIMPLEQ_FOREACH(qid, &commits, entry) {
5835 commit_id = qid->id;
5836 parent_id = pid ? pid->id : yca_id;
5837 pid = qid;
5839 error = got_worktree_rebase_merge_files(&merged_paths,
5840 worktree, fileindex, parent_id, commit_id, repo,
5841 rebase_progress, &rebase_status, check_cancelled, NULL);
5842 if (error)
5843 goto done;
5845 if (rebase_status == GOT_STATUS_CONFLICT) {
5846 error = show_rebase_merge_conflict(qid->id, repo);
5847 if (error)
5848 goto done;
5849 got_worktree_rebase_pathlist_free(&merged_paths);
5850 break;
5853 error = rebase_commit(&merged_paths, worktree, fileindex,
5854 tmp_branch, commit_id, repo);
5855 got_worktree_rebase_pathlist_free(&merged_paths);
5856 if (error)
5857 goto done;
5860 if (rebase_status == GOT_STATUS_CONFLICT) {
5861 error = got_worktree_rebase_postpone(worktree, fileindex);
5862 if (error)
5863 goto done;
5864 error = got_error_msg(GOT_ERR_CONFLICTS,
5865 "conflicts must be resolved before rebasing can continue");
5866 } else
5867 error = rebase_complete(worktree, fileindex, branch,
5868 new_base_branch, tmp_branch, repo);
5869 done:
5870 got_object_id_queue_free(&commits);
5871 free(branch_head_commit_id);
5872 free(resume_commit_id);
5873 free(yca_id);
5874 if (commit)
5875 got_object_commit_close(commit);
5876 if (branch)
5877 got_ref_close(branch);
5878 if (new_base_branch)
5879 got_ref_close(new_base_branch);
5880 if (tmp_branch)
5881 got_ref_close(tmp_branch);
5882 if (worktree)
5883 got_worktree_close(worktree);
5884 if (repo)
5885 got_repo_close(repo);
5886 return error;
5889 __dead static void
5890 usage_histedit(void)
5892 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5893 getprogname());
5894 exit(1);
5897 #define GOT_HISTEDIT_PICK 'p'
5898 #define GOT_HISTEDIT_EDIT 'e'
5899 #define GOT_HISTEDIT_FOLD 'f'
5900 #define GOT_HISTEDIT_DROP 'd'
5901 #define GOT_HISTEDIT_MESG 'm'
5903 static struct got_histedit_cmd {
5904 unsigned char code;
5905 const char *name;
5906 const char *desc;
5907 } got_histedit_cmds[] = {
5908 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5909 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5910 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5911 "be used" },
5912 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5913 { GOT_HISTEDIT_MESG, "mesg",
5914 "single-line log message for commit above (open editor if empty)" },
5917 struct got_histedit_list_entry {
5918 TAILQ_ENTRY(got_histedit_list_entry) entry;
5919 struct got_object_id *commit_id;
5920 const struct got_histedit_cmd *cmd;
5921 char *logmsg;
5923 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5925 static const struct got_error *
5926 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5927 FILE *f, struct got_repository *repo)
5929 const struct got_error *err = NULL;
5930 char *logmsg = NULL, *id_str = NULL;
5931 struct got_commit_object *commit = NULL;
5932 int n;
5934 err = got_object_open_as_commit(&commit, repo, commit_id);
5935 if (err)
5936 goto done;
5938 err = get_short_logmsg(&logmsg, 34, commit);
5939 if (err)
5940 goto done;
5942 err = got_object_id_str(&id_str, commit_id);
5943 if (err)
5944 goto done;
5946 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5947 if (n < 0)
5948 err = got_ferror(f, GOT_ERR_IO);
5949 done:
5950 if (commit)
5951 got_object_commit_close(commit);
5952 free(id_str);
5953 free(logmsg);
5954 return err;
5957 static const struct got_error *
5958 histedit_write_commit_list(struct got_object_id_queue *commits,
5959 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5961 const struct got_error *err = NULL;
5962 struct got_object_qid *qid;
5964 if (SIMPLEQ_EMPTY(commits))
5965 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5967 SIMPLEQ_FOREACH(qid, commits, entry) {
5968 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5969 f, repo);
5970 if (err)
5971 break;
5972 if (edit_logmsg_only) {
5973 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5974 if (n < 0) {
5975 err = got_ferror(f, GOT_ERR_IO);
5976 break;
5981 return err;
5984 static const struct got_error *
5985 write_cmd_list(FILE *f, const char *branch_name,
5986 struct got_object_id_queue *commits)
5988 const struct got_error *err = NULL;
5989 int n, i;
5990 char *id_str;
5991 struct got_object_qid *qid;
5993 qid = SIMPLEQ_FIRST(commits);
5994 err = got_object_id_str(&id_str, qid->id);
5995 if (err)
5996 return err;
5998 n = fprintf(f,
5999 "# Editing the history of branch '%s' starting at\n"
6000 "# commit %s\n"
6001 "# Commits will be processed in order from top to "
6002 "bottom of this file.\n", branch_name, id_str);
6003 if (n < 0) {
6004 err = got_ferror(f, GOT_ERR_IO);
6005 goto done;
6008 n = fprintf(f, "# Available histedit commands:\n");
6009 if (n < 0) {
6010 err = got_ferror(f, GOT_ERR_IO);
6011 goto done;
6014 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6015 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6016 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6017 cmd->desc);
6018 if (n < 0) {
6019 err = got_ferror(f, GOT_ERR_IO);
6020 break;
6023 done:
6024 free(id_str);
6025 return err;
6028 static const struct got_error *
6029 histedit_syntax_error(int lineno)
6031 static char msg[42];
6032 int ret;
6034 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6035 lineno);
6036 if (ret == -1 || ret >= sizeof(msg))
6037 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6039 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6042 static const struct got_error *
6043 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6044 char *logmsg, struct got_repository *repo)
6046 const struct got_error *err;
6047 struct got_commit_object *folded_commit = NULL;
6048 char *id_str, *folded_logmsg = NULL;
6050 err = got_object_id_str(&id_str, hle->commit_id);
6051 if (err)
6052 return err;
6054 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6055 if (err)
6056 goto done;
6058 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6059 if (err)
6060 goto done;
6061 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6062 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6063 folded_logmsg) == -1) {
6064 err = got_error_from_errno("asprintf");
6066 done:
6067 if (folded_commit)
6068 got_object_commit_close(folded_commit);
6069 free(id_str);
6070 free(folded_logmsg);
6071 return err;
6074 static struct got_histedit_list_entry *
6075 get_folded_commits(struct got_histedit_list_entry *hle)
6077 struct got_histedit_list_entry *prev, *folded = NULL;
6079 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6080 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6081 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6082 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6083 folded = prev;
6084 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6087 return folded;
6090 static const struct got_error *
6091 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6092 struct got_repository *repo)
6094 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6095 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6096 const struct got_error *err = NULL;
6097 struct got_commit_object *commit = NULL;
6098 int fd;
6099 struct got_histedit_list_entry *folded = NULL;
6101 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6102 if (err)
6103 return err;
6105 folded = get_folded_commits(hle);
6106 if (folded) {
6107 while (folded != hle) {
6108 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6109 folded = TAILQ_NEXT(folded, entry);
6110 continue;
6112 err = append_folded_commit_msg(&new_msg, folded,
6113 logmsg, repo);
6114 if (err)
6115 goto done;
6116 free(logmsg);
6117 logmsg = new_msg;
6118 folded = TAILQ_NEXT(folded, entry);
6122 err = got_object_id_str(&id_str, hle->commit_id);
6123 if (err)
6124 goto done;
6125 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6126 if (err)
6127 goto done;
6128 if (asprintf(&new_msg,
6129 "%s\n# original log message of commit %s: %s",
6130 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6131 err = got_error_from_errno("asprintf");
6132 goto done;
6134 free(logmsg);
6135 logmsg = new_msg;
6137 err = got_object_id_str(&id_str, hle->commit_id);
6138 if (err)
6139 goto done;
6141 err = got_opentemp_named_fd(&logmsg_path, &fd,
6142 GOT_TMPDIR_STR "/got-logmsg");
6143 if (err)
6144 goto done;
6146 dprintf(fd, logmsg);
6147 close(fd);
6149 err = get_editor(&editor);
6150 if (err)
6151 goto done;
6153 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6154 if (err) {
6155 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6156 goto done;
6157 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6159 done:
6160 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6161 err = got_error_from_errno2("unlink", logmsg_path);
6162 free(logmsg_path);
6163 free(logmsg);
6164 free(orig_logmsg);
6165 free(editor);
6166 if (commit)
6167 got_object_commit_close(commit);
6168 return err;
6171 static const struct got_error *
6172 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6173 FILE *f, struct got_repository *repo)
6175 const struct got_error *err = NULL;
6176 char *line = NULL, *p, *end;
6177 size_t size;
6178 ssize_t len;
6179 int lineno = 0, i;
6180 const struct got_histedit_cmd *cmd;
6181 struct got_object_id *commit_id = NULL;
6182 struct got_histedit_list_entry *hle = NULL;
6184 for (;;) {
6185 len = getline(&line, &size, f);
6186 if (len == -1) {
6187 const struct got_error *getline_err;
6188 if (feof(f))
6189 break;
6190 getline_err = got_error_from_errno("getline");
6191 err = got_ferror(f, getline_err->code);
6192 break;
6194 lineno++;
6195 p = line;
6196 while (isspace((unsigned char)p[0]))
6197 p++;
6198 if (p[0] == '#' || p[0] == '\0') {
6199 free(line);
6200 line = NULL;
6201 continue;
6203 cmd = NULL;
6204 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6205 cmd = &got_histedit_cmds[i];
6206 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6207 isspace((unsigned char)p[strlen(cmd->name)])) {
6208 p += strlen(cmd->name);
6209 break;
6211 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6212 p++;
6213 break;
6216 if (i == nitems(got_histedit_cmds)) {
6217 err = histedit_syntax_error(lineno);
6218 break;
6220 while (isspace((unsigned char)p[0]))
6221 p++;
6222 if (cmd->code == GOT_HISTEDIT_MESG) {
6223 if (hle == NULL || hle->logmsg != NULL) {
6224 err = got_error(GOT_ERR_HISTEDIT_CMD);
6225 break;
6227 if (p[0] == '\0') {
6228 err = histedit_edit_logmsg(hle, repo);
6229 if (err)
6230 break;
6231 } else {
6232 hle->logmsg = strdup(p);
6233 if (hle->logmsg == NULL) {
6234 err = got_error_from_errno("strdup");
6235 break;
6238 free(line);
6239 line = NULL;
6240 continue;
6241 } else {
6242 end = p;
6243 while (end[0] && !isspace((unsigned char)end[0]))
6244 end++;
6245 *end = '\0';
6247 err = got_object_resolve_id_str(&commit_id, repo, p);
6248 if (err) {
6249 /* override error code */
6250 err = histedit_syntax_error(lineno);
6251 break;
6254 hle = malloc(sizeof(*hle));
6255 if (hle == NULL) {
6256 err = got_error_from_errno("malloc");
6257 break;
6259 hle->cmd = cmd;
6260 hle->commit_id = commit_id;
6261 hle->logmsg = NULL;
6262 commit_id = NULL;
6263 free(line);
6264 line = NULL;
6265 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6268 free(line);
6269 free(commit_id);
6270 return err;
6273 static const struct got_error *
6274 histedit_check_script(struct got_histedit_list *histedit_cmds,
6275 struct got_object_id_queue *commits, struct got_repository *repo)
6277 const struct got_error *err = NULL;
6278 struct got_object_qid *qid;
6279 struct got_histedit_list_entry *hle;
6280 static char msg[92];
6281 char *id_str;
6283 if (TAILQ_EMPTY(histedit_cmds))
6284 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6285 "histedit script contains no commands");
6286 if (SIMPLEQ_EMPTY(commits))
6287 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6289 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6290 struct got_histedit_list_entry *hle2;
6291 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6292 if (hle == hle2)
6293 continue;
6294 if (got_object_id_cmp(hle->commit_id,
6295 hle2->commit_id) != 0)
6296 continue;
6297 err = got_object_id_str(&id_str, hle->commit_id);
6298 if (err)
6299 return err;
6300 snprintf(msg, sizeof(msg), "commit %s is listed "
6301 "more than once in histedit script", id_str);
6302 free(id_str);
6303 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6307 SIMPLEQ_FOREACH(qid, commits, entry) {
6308 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6309 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6310 break;
6312 if (hle == NULL) {
6313 err = got_object_id_str(&id_str, qid->id);
6314 if (err)
6315 return err;
6316 snprintf(msg, sizeof(msg),
6317 "commit %s missing from histedit script", id_str);
6318 free(id_str);
6319 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6323 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6324 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6325 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6326 "last commit in histedit script cannot be folded");
6328 return NULL;
6331 static const struct got_error *
6332 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6333 const char *path, struct got_object_id_queue *commits,
6334 struct got_repository *repo)
6336 const struct got_error *err = NULL;
6337 char *editor;
6338 FILE *f = NULL;
6340 err = get_editor(&editor);
6341 if (err)
6342 return err;
6344 if (spawn_editor(editor, path) == -1) {
6345 err = got_error_from_errno("failed spawning editor");
6346 goto done;
6349 f = fopen(path, "r");
6350 if (f == NULL) {
6351 err = got_error_from_errno("fopen");
6352 goto done;
6354 err = histedit_parse_list(histedit_cmds, f, repo);
6355 if (err)
6356 goto done;
6358 err = histedit_check_script(histedit_cmds, commits, repo);
6359 done:
6360 if (f && fclose(f) != 0 && err == NULL)
6361 err = got_error_from_errno("fclose");
6362 free(editor);
6363 return err;
6366 static const struct got_error *
6367 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6368 struct got_object_id_queue *, const char *, const char *,
6369 struct got_repository *);
6371 static const struct got_error *
6372 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6373 struct got_object_id_queue *commits, const char *branch_name,
6374 int edit_logmsg_only, struct got_repository *repo)
6376 const struct got_error *err;
6377 FILE *f = NULL;
6378 char *path = NULL;
6380 err = got_opentemp_named(&path, &f, "got-histedit");
6381 if (err)
6382 return err;
6384 err = write_cmd_list(f, branch_name, commits);
6385 if (err)
6386 goto done;
6388 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6389 if (err)
6390 goto done;
6392 if (edit_logmsg_only) {
6393 rewind(f);
6394 err = histedit_parse_list(histedit_cmds, f, repo);
6395 } else {
6396 if (fclose(f) != 0) {
6397 err = got_error_from_errno("fclose");
6398 goto done;
6400 f = NULL;
6401 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6402 if (err) {
6403 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6404 err->code != GOT_ERR_HISTEDIT_CMD)
6405 goto done;
6406 err = histedit_edit_list_retry(histedit_cmds, err,
6407 commits, path, branch_name, repo);
6410 done:
6411 if (f && fclose(f) != 0 && err == NULL)
6412 err = got_error_from_errno("fclose");
6413 if (path && unlink(path) != 0 && err == NULL)
6414 err = got_error_from_errno2("unlink", path);
6415 free(path);
6416 return err;
6419 static const struct got_error *
6420 histedit_save_list(struct got_histedit_list *histedit_cmds,
6421 struct got_worktree *worktree, struct got_repository *repo)
6423 const struct got_error *err = NULL;
6424 char *path = NULL;
6425 FILE *f = NULL;
6426 struct got_histedit_list_entry *hle;
6427 struct got_commit_object *commit = NULL;
6429 err = got_worktree_get_histedit_script_path(&path, worktree);
6430 if (err)
6431 return err;
6433 f = fopen(path, "w");
6434 if (f == NULL) {
6435 err = got_error_from_errno2("fopen", path);
6436 goto done;
6438 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6439 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6440 repo);
6441 if (err)
6442 break;
6444 if (hle->logmsg) {
6445 int n = fprintf(f, "%c %s\n",
6446 GOT_HISTEDIT_MESG, hle->logmsg);
6447 if (n < 0) {
6448 err = got_ferror(f, GOT_ERR_IO);
6449 break;
6453 done:
6454 if (f && fclose(f) != 0 && err == NULL)
6455 err = got_error_from_errno("fclose");
6456 free(path);
6457 if (commit)
6458 got_object_commit_close(commit);
6459 return err;
6462 void
6463 histedit_free_list(struct got_histedit_list *histedit_cmds)
6465 struct got_histedit_list_entry *hle;
6467 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6468 TAILQ_REMOVE(histedit_cmds, hle, entry);
6469 free(hle);
6473 static const struct got_error *
6474 histedit_load_list(struct got_histedit_list *histedit_cmds,
6475 const char *path, struct got_repository *repo)
6477 const struct got_error *err = NULL;
6478 FILE *f = NULL;
6480 f = fopen(path, "r");
6481 if (f == NULL) {
6482 err = got_error_from_errno2("fopen", path);
6483 goto done;
6486 err = histedit_parse_list(histedit_cmds, f, repo);
6487 done:
6488 if (f && fclose(f) != 0 && err == NULL)
6489 err = got_error_from_errno("fclose");
6490 return err;
6493 static const struct got_error *
6494 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6495 const struct got_error *edit_err, struct got_object_id_queue *commits,
6496 const char *path, const char *branch_name, struct got_repository *repo)
6498 const struct got_error *err = NULL, *prev_err = edit_err;
6499 int resp = ' ';
6501 while (resp != 'c' && resp != 'r' && resp != 'a') {
6502 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6503 "or (a)bort: ", getprogname(), prev_err->msg);
6504 resp = getchar();
6505 if (resp == '\n')
6506 resp = getchar();
6507 if (resp == 'c') {
6508 histedit_free_list(histedit_cmds);
6509 err = histedit_run_editor(histedit_cmds, path, commits,
6510 repo);
6511 if (err) {
6512 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6513 err->code != GOT_ERR_HISTEDIT_CMD)
6514 break;
6515 prev_err = err;
6516 resp = ' ';
6517 continue;
6519 break;
6520 } else if (resp == 'r') {
6521 histedit_free_list(histedit_cmds);
6522 err = histedit_edit_script(histedit_cmds,
6523 commits, branch_name, 0, repo);
6524 if (err) {
6525 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6526 err->code != GOT_ERR_HISTEDIT_CMD)
6527 break;
6528 prev_err = err;
6529 resp = ' ';
6530 continue;
6532 break;
6533 } else if (resp == 'a') {
6534 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6535 break;
6536 } else
6537 printf("invalid response '%c'\n", resp);
6540 return err;
6543 static const struct got_error *
6544 histedit_complete(struct got_worktree *worktree,
6545 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6546 struct got_reference *branch, struct got_repository *repo)
6548 printf("Switching work tree to %s\n",
6549 got_ref_get_symref_target(branch));
6550 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6551 branch, repo);
6554 static const struct got_error *
6555 show_histedit_progress(struct got_commit_object *commit,
6556 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6558 const struct got_error *err;
6559 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6561 err = got_object_id_str(&old_id_str, hle->commit_id);
6562 if (err)
6563 goto done;
6565 if (new_id) {
6566 err = got_object_id_str(&new_id_str, new_id);
6567 if (err)
6568 goto done;
6571 old_id_str[12] = '\0';
6572 if (new_id_str)
6573 new_id_str[12] = '\0';
6575 if (hle->logmsg) {
6576 logmsg = strdup(hle->logmsg);
6577 if (logmsg == NULL) {
6578 err = got_error_from_errno("strdup");
6579 goto done;
6581 trim_logmsg(logmsg, 42);
6582 } else {
6583 err = get_short_logmsg(&logmsg, 42, commit);
6584 if (err)
6585 goto done;
6588 switch (hle->cmd->code) {
6589 case GOT_HISTEDIT_PICK:
6590 case GOT_HISTEDIT_EDIT:
6591 printf("%s -> %s: %s\n", old_id_str,
6592 new_id_str ? new_id_str : "no-op change", logmsg);
6593 break;
6594 case GOT_HISTEDIT_DROP:
6595 case GOT_HISTEDIT_FOLD:
6596 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6597 logmsg);
6598 break;
6599 default:
6600 break;
6602 done:
6603 free(old_id_str);
6604 free(new_id_str);
6605 return err;
6608 static const struct got_error *
6609 histedit_commit(struct got_pathlist_head *merged_paths,
6610 struct got_worktree *worktree, struct got_fileindex *fileindex,
6611 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6612 struct got_repository *repo)
6614 const struct got_error *err;
6615 struct got_commit_object *commit;
6616 struct got_object_id *new_commit_id;
6618 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6619 && hle->logmsg == NULL) {
6620 err = histedit_edit_logmsg(hle, repo);
6621 if (err)
6622 return err;
6625 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6626 if (err)
6627 return err;
6629 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6630 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6631 hle->logmsg, repo);
6632 if (err) {
6633 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6634 goto done;
6635 err = show_histedit_progress(commit, hle, NULL);
6636 } else {
6637 err = show_histedit_progress(commit, hle, new_commit_id);
6638 free(new_commit_id);
6640 done:
6641 got_object_commit_close(commit);
6642 return err;
6645 static const struct got_error *
6646 histedit_skip_commit(struct got_histedit_list_entry *hle,
6647 struct got_worktree *worktree, struct got_repository *repo)
6649 const struct got_error *error;
6650 struct got_commit_object *commit;
6652 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6653 repo);
6654 if (error)
6655 return error;
6657 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6658 if (error)
6659 return error;
6661 error = show_histedit_progress(commit, hle, NULL);
6662 got_object_commit_close(commit);
6663 return error;
6666 static const struct got_error *
6667 check_local_changes(void *arg, unsigned char status,
6668 unsigned char staged_status, const char *path,
6669 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6670 struct got_object_id *commit_id, int dirfd, const char *de_name)
6672 int *have_local_changes = arg;
6674 switch (status) {
6675 case GOT_STATUS_ADD:
6676 case GOT_STATUS_DELETE:
6677 case GOT_STATUS_MODIFY:
6678 case GOT_STATUS_CONFLICT:
6679 *have_local_changes = 1;
6680 return got_error(GOT_ERR_CANCELLED);
6681 default:
6682 break;
6685 switch (staged_status) {
6686 case GOT_STATUS_ADD:
6687 case GOT_STATUS_DELETE:
6688 case GOT_STATUS_MODIFY:
6689 *have_local_changes = 1;
6690 return got_error(GOT_ERR_CANCELLED);
6691 default:
6692 break;
6695 return NULL;
6698 static const struct got_error *
6699 cmd_histedit(int argc, char *argv[])
6701 const struct got_error *error = NULL;
6702 struct got_worktree *worktree = NULL;
6703 struct got_fileindex *fileindex = NULL;
6704 struct got_repository *repo = NULL;
6705 char *cwd = NULL;
6706 struct got_reference *branch = NULL;
6707 struct got_reference *tmp_branch = NULL;
6708 struct got_object_id *resume_commit_id = NULL;
6709 struct got_object_id *base_commit_id = NULL;
6710 struct got_object_id *head_commit_id = NULL;
6711 struct got_commit_object *commit = NULL;
6712 int ch, rebase_in_progress = 0, did_something;
6713 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6714 int edit_logmsg_only = 0;
6715 const char *edit_script_path = NULL;
6716 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6717 struct got_object_id_queue commits;
6718 struct got_pathlist_head merged_paths;
6719 const struct got_object_id_queue *parent_ids;
6720 struct got_object_qid *pid;
6721 struct got_histedit_list histedit_cmds;
6722 struct got_histedit_list_entry *hle;
6724 SIMPLEQ_INIT(&commits);
6725 TAILQ_INIT(&histedit_cmds);
6726 TAILQ_INIT(&merged_paths);
6728 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6729 switch (ch) {
6730 case 'a':
6731 abort_edit = 1;
6732 break;
6733 case 'c':
6734 continue_edit = 1;
6735 break;
6736 case 'F':
6737 edit_script_path = optarg;
6738 break;
6739 case 'm':
6740 edit_logmsg_only = 1;
6741 break;
6742 default:
6743 usage_histedit();
6744 /* NOTREACHED */
6748 argc -= optind;
6749 argv += optind;
6751 #ifndef PROFILE
6752 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6753 "unveil", NULL) == -1)
6754 err(1, "pledge");
6755 #endif
6756 if (abort_edit && continue_edit)
6757 errx(1, "histedit's -a and -c options are mutually exclusive");
6758 if (edit_script_path && edit_logmsg_only)
6759 errx(1, "histedit's -F and -m options are mutually exclusive");
6760 if (abort_edit && edit_logmsg_only)
6761 errx(1, "histedit's -a and -m options are mutually exclusive");
6762 if (continue_edit && edit_logmsg_only)
6763 errx(1, "histedit's -c and -m options are mutually exclusive");
6764 if (argc != 0)
6765 usage_histedit();
6768 * This command cannot apply unveil(2) in all cases because the
6769 * user may choose to run an editor to edit the histedit script
6770 * and to edit individual commit log messages.
6771 * unveil(2) traverses exec(2); if an editor is used we have to
6772 * apply unveil after edit script and log messages have been written.
6773 * XXX TODO: Make use of unveil(2) where possible.
6776 cwd = getcwd(NULL, 0);
6777 if (cwd == NULL) {
6778 error = got_error_from_errno("getcwd");
6779 goto done;
6781 error = got_worktree_open(&worktree, cwd);
6782 if (error)
6783 goto done;
6785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6786 NULL);
6787 if (error != NULL)
6788 goto done;
6790 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6791 if (error)
6792 goto done;
6793 if (rebase_in_progress) {
6794 error = got_error(GOT_ERR_REBASING);
6795 goto done;
6798 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6799 if (error)
6800 goto done;
6802 if (edit_in_progress && edit_logmsg_only) {
6803 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6804 "histedit operation is in progress in this "
6805 "work tree and must be continued or aborted "
6806 "before the -m option can be used");
6807 goto done;
6810 if (edit_in_progress && abort_edit) {
6811 error = got_worktree_histedit_continue(&resume_commit_id,
6812 &tmp_branch, &branch, &base_commit_id, &fileindex,
6813 worktree, repo);
6814 if (error)
6815 goto done;
6816 printf("Switching work tree to %s\n",
6817 got_ref_get_symref_target(branch));
6818 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6819 branch, base_commit_id, update_progress, &did_something);
6820 if (error)
6821 goto done;
6822 printf("Histedit of %s aborted\n",
6823 got_ref_get_symref_target(branch));
6824 goto done; /* nothing else to do */
6825 } else if (abort_edit) {
6826 error = got_error(GOT_ERR_NOT_HISTEDIT);
6827 goto done;
6830 if (continue_edit) {
6831 char *path;
6833 if (!edit_in_progress) {
6834 error = got_error(GOT_ERR_NOT_HISTEDIT);
6835 goto done;
6838 error = got_worktree_get_histedit_script_path(&path, worktree);
6839 if (error)
6840 goto done;
6842 error = histedit_load_list(&histedit_cmds, path, repo);
6843 free(path);
6844 if (error)
6845 goto done;
6847 error = got_worktree_histedit_continue(&resume_commit_id,
6848 &tmp_branch, &branch, &base_commit_id, &fileindex,
6849 worktree, repo);
6850 if (error)
6851 goto done;
6853 error = got_ref_resolve(&head_commit_id, repo, branch);
6854 if (error)
6855 goto done;
6857 error = got_object_open_as_commit(&commit, repo,
6858 head_commit_id);
6859 if (error)
6860 goto done;
6861 parent_ids = got_object_commit_get_parent_ids(commit);
6862 pid = SIMPLEQ_FIRST(parent_ids);
6863 if (pid == NULL) {
6864 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6865 goto done;
6867 error = collect_commits(&commits, head_commit_id, pid->id,
6868 base_commit_id, got_worktree_get_path_prefix(worktree),
6869 GOT_ERR_HISTEDIT_PATH, repo);
6870 got_object_commit_close(commit);
6871 commit = NULL;
6872 if (error)
6873 goto done;
6874 } else {
6875 if (edit_in_progress) {
6876 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6877 goto done;
6880 error = got_ref_open(&branch, repo,
6881 got_worktree_get_head_ref_name(worktree), 0);
6882 if (error != NULL)
6883 goto done;
6885 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6886 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6887 "will not edit commit history of a branch outside "
6888 "the \"refs/heads/\" reference namespace");
6889 goto done;
6892 error = got_ref_resolve(&head_commit_id, repo, branch);
6893 got_ref_close(branch);
6894 branch = NULL;
6895 if (error)
6896 goto done;
6898 error = got_object_open_as_commit(&commit, repo,
6899 head_commit_id);
6900 if (error)
6901 goto done;
6902 parent_ids = got_object_commit_get_parent_ids(commit);
6903 pid = SIMPLEQ_FIRST(parent_ids);
6904 if (pid == NULL) {
6905 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6906 goto done;
6908 error = collect_commits(&commits, head_commit_id, pid->id,
6909 got_worktree_get_base_commit_id(worktree),
6910 got_worktree_get_path_prefix(worktree),
6911 GOT_ERR_HISTEDIT_PATH, repo);
6912 got_object_commit_close(commit);
6913 commit = NULL;
6914 if (error)
6915 goto done;
6917 if (SIMPLEQ_EMPTY(&commits)) {
6918 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6919 goto done;
6922 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6923 &base_commit_id, &fileindex, worktree, repo);
6924 if (error)
6925 goto done;
6927 if (edit_script_path) {
6928 error = histedit_load_list(&histedit_cmds,
6929 edit_script_path, repo);
6930 if (error) {
6931 got_worktree_histedit_abort(worktree, fileindex,
6932 repo, branch, base_commit_id,
6933 update_progress, &did_something);
6934 goto done;
6936 } else {
6937 const char *branch_name;
6938 branch_name = got_ref_get_symref_target(branch);
6939 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6940 branch_name += 11;
6941 error = histedit_edit_script(&histedit_cmds, &commits,
6942 branch_name, edit_logmsg_only, repo);
6943 if (error) {
6944 got_worktree_histedit_abort(worktree, fileindex,
6945 repo, branch, base_commit_id,
6946 update_progress, &did_something);
6947 goto done;
6952 error = histedit_save_list(&histedit_cmds, worktree,
6953 repo);
6954 if (error) {
6955 got_worktree_histedit_abort(worktree, fileindex,
6956 repo, branch, base_commit_id,
6957 update_progress, &did_something);
6958 goto done;
6963 error = histedit_check_script(&histedit_cmds, &commits, repo);
6964 if (error)
6965 goto done;
6967 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6968 if (resume_commit_id) {
6969 if (got_object_id_cmp(hle->commit_id,
6970 resume_commit_id) != 0)
6971 continue;
6973 resume_commit_id = NULL;
6974 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6975 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6976 error = histedit_skip_commit(hle, worktree,
6977 repo);
6978 if (error)
6979 goto done;
6980 } else {
6981 struct got_pathlist_head paths;
6982 int have_changes = 0;
6984 TAILQ_INIT(&paths);
6985 error = got_pathlist_append(&paths, "", NULL);
6986 if (error)
6987 goto done;
6988 error = got_worktree_status(worktree, &paths,
6989 repo, check_local_changes, &have_changes,
6990 check_cancelled, NULL);
6991 got_pathlist_free(&paths);
6992 if (error) {
6993 if (error->code != GOT_ERR_CANCELLED)
6994 goto done;
6995 if (sigint_received || sigpipe_received)
6996 goto done;
6998 if (have_changes) {
6999 error = histedit_commit(NULL, worktree,
7000 fileindex, tmp_branch, hle, repo);
7001 if (error)
7002 goto done;
7003 } else {
7004 error = got_object_open_as_commit(
7005 &commit, repo, hle->commit_id);
7006 if (error)
7007 goto done;
7008 error = show_histedit_progress(commit,
7009 hle, NULL);
7010 got_object_commit_close(commit);
7011 commit = NULL;
7012 if (error)
7013 goto done;
7016 continue;
7019 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7020 error = histedit_skip_commit(hle, worktree, repo);
7021 if (error)
7022 goto done;
7023 continue;
7026 error = got_object_open_as_commit(&commit, repo,
7027 hle->commit_id);
7028 if (error)
7029 goto done;
7030 parent_ids = got_object_commit_get_parent_ids(commit);
7031 pid = SIMPLEQ_FIRST(parent_ids);
7033 error = got_worktree_histedit_merge_files(&merged_paths,
7034 worktree, fileindex, pid->id, hle->commit_id, repo,
7035 rebase_progress, &rebase_status, check_cancelled, NULL);
7036 if (error)
7037 goto done;
7038 got_object_commit_close(commit);
7039 commit = NULL;
7041 if (rebase_status == GOT_STATUS_CONFLICT) {
7042 error = show_rebase_merge_conflict(hle->commit_id,
7043 repo);
7044 if (error)
7045 goto done;
7046 got_worktree_rebase_pathlist_free(&merged_paths);
7047 break;
7050 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7051 char *id_str;
7052 error = got_object_id_str(&id_str, hle->commit_id);
7053 if (error)
7054 goto done;
7055 printf("Stopping histedit for amending commit %s\n",
7056 id_str);
7057 free(id_str);
7058 got_worktree_rebase_pathlist_free(&merged_paths);
7059 error = got_worktree_histedit_postpone(worktree,
7060 fileindex);
7061 goto done;
7064 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7065 error = histedit_skip_commit(hle, worktree, repo);
7066 if (error)
7067 goto done;
7068 continue;
7071 error = histedit_commit(&merged_paths, worktree, fileindex,
7072 tmp_branch, hle, repo);
7073 got_worktree_rebase_pathlist_free(&merged_paths);
7074 if (error)
7075 goto done;
7078 if (rebase_status == GOT_STATUS_CONFLICT) {
7079 error = got_worktree_histedit_postpone(worktree, fileindex);
7080 if (error)
7081 goto done;
7082 error = got_error_msg(GOT_ERR_CONFLICTS,
7083 "conflicts must be resolved before histedit can continue");
7084 } else
7085 error = histedit_complete(worktree, fileindex, tmp_branch,
7086 branch, repo);
7087 done:
7088 got_object_id_queue_free(&commits);
7089 histedit_free_list(&histedit_cmds);
7090 free(head_commit_id);
7091 free(base_commit_id);
7092 free(resume_commit_id);
7093 if (commit)
7094 got_object_commit_close(commit);
7095 if (branch)
7096 got_ref_close(branch);
7097 if (tmp_branch)
7098 got_ref_close(tmp_branch);
7099 if (worktree)
7100 got_worktree_close(worktree);
7101 if (repo)
7102 got_repo_close(repo);
7103 return error;
7106 __dead static void
7107 usage_integrate(void)
7109 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7110 exit(1);
7113 static const struct got_error *
7114 cmd_integrate(int argc, char *argv[])
7116 const struct got_error *error = NULL;
7117 struct got_repository *repo = NULL;
7118 struct got_worktree *worktree = NULL;
7119 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7120 const char *branch_arg = NULL;
7121 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7122 struct got_fileindex *fileindex = NULL;
7123 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7124 int ch, did_something = 0;
7126 while ((ch = getopt(argc, argv, "")) != -1) {
7127 switch (ch) {
7128 default:
7129 usage_integrate();
7130 /* NOTREACHED */
7134 argc -= optind;
7135 argv += optind;
7137 if (argc != 1)
7138 usage_integrate();
7139 branch_arg = argv[0];
7141 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7142 "unveil", NULL) == -1)
7143 err(1, "pledge");
7145 cwd = getcwd(NULL, 0);
7146 if (cwd == NULL) {
7147 error = got_error_from_errno("getcwd");
7148 goto done;
7151 error = got_worktree_open(&worktree, cwd);
7152 if (error)
7153 goto done;
7155 error = check_rebase_or_histedit_in_progress(worktree);
7156 if (error)
7157 goto done;
7159 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7160 NULL);
7161 if (error != NULL)
7162 goto done;
7164 error = apply_unveil(got_repo_get_path(repo), 0,
7165 got_worktree_get_root_path(worktree));
7166 if (error)
7167 goto done;
7169 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7170 error = got_error_from_errno("asprintf");
7171 goto done;
7174 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7175 &base_branch_ref, worktree, refname, repo);
7176 if (error)
7177 goto done;
7179 refname = strdup(got_ref_get_name(branch_ref));
7180 if (refname == NULL) {
7181 error = got_error_from_errno("strdup");
7182 got_worktree_integrate_abort(worktree, fileindex, repo,
7183 branch_ref, base_branch_ref);
7184 goto done;
7186 base_refname = strdup(got_ref_get_name(base_branch_ref));
7187 if (base_refname == NULL) {
7188 error = got_error_from_errno("strdup");
7189 got_worktree_integrate_abort(worktree, fileindex, repo,
7190 branch_ref, base_branch_ref);
7191 goto done;
7194 error = got_ref_resolve(&commit_id, repo, branch_ref);
7195 if (error)
7196 goto done;
7198 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7199 if (error)
7200 goto done;
7202 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7203 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7204 "specified branch has already been integrated");
7205 got_worktree_integrate_abort(worktree, fileindex, repo,
7206 branch_ref, base_branch_ref);
7207 goto done;
7210 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7211 if (error) {
7212 if (error->code == GOT_ERR_ANCESTRY)
7213 error = got_error(GOT_ERR_REBASE_REQUIRED);
7214 got_worktree_integrate_abort(worktree, fileindex, repo,
7215 branch_ref, base_branch_ref);
7216 goto done;
7219 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7220 branch_ref, base_branch_ref, update_progress, &did_something,
7221 check_cancelled, NULL);
7222 if (error)
7223 goto done;
7225 printf("Integrated %s into %s\n", refname, base_refname);
7226 done:
7227 if (repo)
7228 got_repo_close(repo);
7229 if (worktree)
7230 got_worktree_close(worktree);
7231 free(cwd);
7232 free(base_commit_id);
7233 free(commit_id);
7234 free(refname);
7235 free(base_refname);
7236 return error;
7239 __dead static void
7240 usage_stage(void)
7242 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7243 "[file-path ...]\n",
7244 getprogname());
7245 exit(1);
7248 static const struct got_error *
7249 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7250 const char *path, struct got_object_id *blob_id,
7251 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7252 int dirfd, const char *de_name)
7254 const struct got_error *err = NULL;
7255 char *id_str = NULL;
7257 if (staged_status != GOT_STATUS_ADD &&
7258 staged_status != GOT_STATUS_MODIFY &&
7259 staged_status != GOT_STATUS_DELETE)
7260 return NULL;
7262 if (staged_status == GOT_STATUS_ADD ||
7263 staged_status == GOT_STATUS_MODIFY)
7264 err = got_object_id_str(&id_str, staged_blob_id);
7265 else
7266 err = got_object_id_str(&id_str, blob_id);
7267 if (err)
7268 return err;
7270 printf("%s %c %s\n", id_str, staged_status, path);
7271 free(id_str);
7272 return NULL;
7275 static const struct got_error *
7276 cmd_stage(int argc, char *argv[])
7278 const struct got_error *error = NULL;
7279 struct got_repository *repo = NULL;
7280 struct got_worktree *worktree = NULL;
7281 char *cwd = NULL;
7282 struct got_pathlist_head paths;
7283 struct got_pathlist_entry *pe;
7284 int ch, list_stage = 0, pflag = 0;
7285 FILE *patch_script_file = NULL;
7286 const char *patch_script_path = NULL;
7287 struct choose_patch_arg cpa;
7289 TAILQ_INIT(&paths);
7291 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7292 switch (ch) {
7293 case 'l':
7294 list_stage = 1;
7295 break;
7296 case 'p':
7297 pflag = 1;
7298 break;
7299 case 'F':
7300 patch_script_path = optarg;
7301 break;
7302 default:
7303 usage_stage();
7304 /* NOTREACHED */
7308 argc -= optind;
7309 argv += optind;
7311 #ifndef PROFILE
7312 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7313 "unveil", NULL) == -1)
7314 err(1, "pledge");
7315 #endif
7316 if (list_stage && (pflag || patch_script_path))
7317 errx(1, "-l option cannot be used with other options");
7318 if (patch_script_path && !pflag)
7319 errx(1, "-F option can only be used together with -p option");
7321 cwd = getcwd(NULL, 0);
7322 if (cwd == NULL) {
7323 error = got_error_from_errno("getcwd");
7324 goto done;
7327 error = got_worktree_open(&worktree, cwd);
7328 if (error)
7329 goto done;
7331 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7332 NULL);
7333 if (error != NULL)
7334 goto done;
7336 if (patch_script_path) {
7337 patch_script_file = fopen(patch_script_path, "r");
7338 if (patch_script_file == NULL) {
7339 error = got_error_from_errno2("fopen",
7340 patch_script_path);
7341 goto done;
7344 error = apply_unveil(got_repo_get_path(repo), 0,
7345 got_worktree_get_root_path(worktree));
7346 if (error)
7347 goto done;
7349 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7350 if (error)
7351 goto done;
7353 if (list_stage)
7354 error = got_worktree_status(worktree, &paths, repo,
7355 print_stage, NULL, check_cancelled, NULL);
7356 else {
7357 cpa.patch_script_file = patch_script_file;
7358 cpa.action = "stage";
7359 error = got_worktree_stage(worktree, &paths,
7360 pflag ? NULL : print_status, NULL,
7361 pflag ? choose_patch : NULL, &cpa, repo);
7363 done:
7364 if (patch_script_file && fclose(patch_script_file) == EOF &&
7365 error == NULL)
7366 error = got_error_from_errno2("fclose", patch_script_path);
7367 if (repo)
7368 got_repo_close(repo);
7369 if (worktree)
7370 got_worktree_close(worktree);
7371 TAILQ_FOREACH(pe, &paths, entry)
7372 free((char *)pe->path);
7373 got_pathlist_free(&paths);
7374 free(cwd);
7375 return error;
7378 __dead static void
7379 usage_unstage(void)
7381 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7382 "[file-path ...]\n",
7383 getprogname());
7384 exit(1);
7388 static const struct got_error *
7389 cmd_unstage(int argc, char *argv[])
7391 const struct got_error *error = NULL;
7392 struct got_repository *repo = NULL;
7393 struct got_worktree *worktree = NULL;
7394 char *cwd = NULL;
7395 struct got_pathlist_head paths;
7396 struct got_pathlist_entry *pe;
7397 int ch, did_something = 0, pflag = 0;
7398 FILE *patch_script_file = NULL;
7399 const char *patch_script_path = NULL;
7400 struct choose_patch_arg cpa;
7402 TAILQ_INIT(&paths);
7404 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7405 switch (ch) {
7406 case 'p':
7407 pflag = 1;
7408 break;
7409 case 'F':
7410 patch_script_path = optarg;
7411 break;
7412 default:
7413 usage_unstage();
7414 /* NOTREACHED */
7418 argc -= optind;
7419 argv += optind;
7421 #ifndef PROFILE
7422 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7423 "unveil", NULL) == -1)
7424 err(1, "pledge");
7425 #endif
7426 if (patch_script_path && !pflag)
7427 errx(1, "-F option can only be used together with -p option");
7429 cwd = getcwd(NULL, 0);
7430 if (cwd == NULL) {
7431 error = got_error_from_errno("getcwd");
7432 goto done;
7435 error = got_worktree_open(&worktree, cwd);
7436 if (error)
7437 goto done;
7439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7440 NULL);
7441 if (error != NULL)
7442 goto done;
7444 if (patch_script_path) {
7445 patch_script_file = fopen(patch_script_path, "r");
7446 if (patch_script_file == NULL) {
7447 error = got_error_from_errno2("fopen",
7448 patch_script_path);
7449 goto done;
7453 error = apply_unveil(got_repo_get_path(repo), 0,
7454 got_worktree_get_root_path(worktree));
7455 if (error)
7456 goto done;
7458 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7459 if (error)
7460 goto done;
7462 cpa.patch_script_file = patch_script_file;
7463 cpa.action = "unstage";
7464 error = got_worktree_unstage(worktree, &paths, update_progress,
7465 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7466 done:
7467 if (patch_script_file && fclose(patch_script_file) == EOF &&
7468 error == NULL)
7469 error = got_error_from_errno2("fclose", patch_script_path);
7470 if (repo)
7471 got_repo_close(repo);
7472 if (worktree)
7473 got_worktree_close(worktree);
7474 TAILQ_FOREACH(pe, &paths, entry)
7475 free((char *)pe->path);
7476 got_pathlist_free(&paths);
7477 free(cwd);
7478 return error;
7481 __dead static void
7482 usage_cat(void)
7484 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7485 "arg1 [arg2 ...]\n", getprogname());
7486 exit(1);
7489 static const struct got_error *
7490 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7492 const struct got_error *err;
7493 struct got_blob_object *blob;
7495 err = got_object_open_as_blob(&blob, repo, id, 8192);
7496 if (err)
7497 return err;
7499 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7500 got_object_blob_close(blob);
7501 return err;
7504 static const struct got_error *
7505 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7507 const struct got_error *err;
7508 struct got_tree_object *tree;
7509 int nentries, i;
7511 err = got_object_open_as_tree(&tree, repo, id);
7512 if (err)
7513 return err;
7515 nentries = got_object_tree_get_nentries(tree);
7516 for (i = 0; i < nentries; i++) {
7517 struct got_tree_entry *te;
7518 char *id_str;
7519 if (sigint_received || sigpipe_received)
7520 break;
7521 te = got_object_tree_get_entry(tree, i);
7522 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7523 if (err)
7524 break;
7525 fprintf(outfile, "%s %.7o %s\n", id_str,
7526 got_tree_entry_get_mode(te),
7527 got_tree_entry_get_name(te));
7528 free(id_str);
7531 got_object_tree_close(tree);
7532 return err;
7535 static const struct got_error *
7536 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7538 const struct got_error *err;
7539 struct got_commit_object *commit;
7540 const struct got_object_id_queue *parent_ids;
7541 struct got_object_qid *pid;
7542 char *id_str = NULL;
7543 const char *logmsg = NULL;
7545 err = got_object_open_as_commit(&commit, repo, id);
7546 if (err)
7547 return err;
7549 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7550 if (err)
7551 goto done;
7553 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7554 parent_ids = got_object_commit_get_parent_ids(commit);
7555 fprintf(outfile, "numparents %d\n",
7556 got_object_commit_get_nparents(commit));
7557 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7558 char *pid_str;
7559 err = got_object_id_str(&pid_str, pid->id);
7560 if (err)
7561 goto done;
7562 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7563 free(pid_str);
7565 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7566 got_object_commit_get_author(commit),
7567 got_object_commit_get_author_time(commit));
7569 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7570 got_object_commit_get_author(commit),
7571 got_object_commit_get_committer_time(commit));
7573 logmsg = got_object_commit_get_logmsg_raw(commit);
7574 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7575 fprintf(outfile, "%s", logmsg);
7576 done:
7577 free(id_str);
7578 got_object_commit_close(commit);
7579 return err;
7582 static const struct got_error *
7583 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7585 const struct got_error *err;
7586 struct got_tag_object *tag;
7587 char *id_str = NULL;
7588 const char *tagmsg = NULL;
7590 err = got_object_open_as_tag(&tag, repo, id);
7591 if (err)
7592 return err;
7594 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7595 if (err)
7596 goto done;
7598 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7600 switch (got_object_tag_get_object_type(tag)) {
7601 case GOT_OBJ_TYPE_BLOB:
7602 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7603 GOT_OBJ_LABEL_BLOB);
7604 break;
7605 case GOT_OBJ_TYPE_TREE:
7606 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7607 GOT_OBJ_LABEL_TREE);
7608 break;
7609 case GOT_OBJ_TYPE_COMMIT:
7610 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7611 GOT_OBJ_LABEL_COMMIT);
7612 break;
7613 case GOT_OBJ_TYPE_TAG:
7614 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7615 GOT_OBJ_LABEL_TAG);
7616 break;
7617 default:
7618 break;
7621 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7622 got_object_tag_get_name(tag));
7624 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7625 got_object_tag_get_tagger(tag),
7626 got_object_tag_get_tagger_time(tag));
7628 tagmsg = got_object_tag_get_message(tag);
7629 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7630 fprintf(outfile, "%s", tagmsg);
7631 done:
7632 free(id_str);
7633 got_object_tag_close(tag);
7634 return err;
7637 static const struct got_error *
7638 cmd_cat(int argc, char *argv[])
7640 const struct got_error *error;
7641 struct got_repository *repo = NULL;
7642 struct got_worktree *worktree = NULL;
7643 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7644 const char *commit_id_str = NULL;
7645 struct got_object_id *id = NULL, *commit_id = NULL;
7646 int ch, obj_type, i, force_path = 0;
7648 #ifndef PROFILE
7649 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7650 NULL) == -1)
7651 err(1, "pledge");
7652 #endif
7654 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7655 switch (ch) {
7656 case 'c':
7657 commit_id_str = optarg;
7658 break;
7659 case 'r':
7660 repo_path = realpath(optarg, NULL);
7661 if (repo_path == NULL)
7662 return got_error_from_errno2("realpath",
7663 optarg);
7664 got_path_strip_trailing_slashes(repo_path);
7665 break;
7666 case 'P':
7667 force_path = 1;
7668 break;
7669 default:
7670 usage_cat();
7671 /* NOTREACHED */
7675 argc -= optind;
7676 argv += optind;
7678 cwd = getcwd(NULL, 0);
7679 if (cwd == NULL) {
7680 error = got_error_from_errno("getcwd");
7681 goto done;
7683 error = got_worktree_open(&worktree, cwd);
7684 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7685 goto done;
7686 if (worktree) {
7687 if (repo_path == NULL) {
7688 repo_path = strdup(
7689 got_worktree_get_repo_path(worktree));
7690 if (repo_path == NULL) {
7691 error = got_error_from_errno("strdup");
7692 goto done;
7697 if (repo_path == NULL) {
7698 repo_path = getcwd(NULL, 0);
7699 if (repo_path == NULL)
7700 return got_error_from_errno("getcwd");
7703 error = got_repo_open(&repo, repo_path, NULL);
7704 free(repo_path);
7705 if (error != NULL)
7706 goto done;
7708 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7709 if (error)
7710 goto done;
7712 if (commit_id_str == NULL)
7713 commit_id_str = GOT_REF_HEAD;
7714 error = got_repo_match_object_id(&commit_id, NULL,
7715 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7716 if (error)
7717 goto done;
7719 for (i = 0; i < argc; i++) {
7720 if (force_path) {
7721 error = got_object_id_by_path(&id, repo, commit_id,
7722 argv[i]);
7723 if (error)
7724 break;
7725 } else {
7726 error = got_repo_match_object_id(&id, &label, argv[i],
7727 GOT_OBJ_TYPE_ANY, 0, repo);
7728 if (error) {
7729 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7730 error->code != GOT_ERR_NOT_REF)
7731 break;
7732 error = got_object_id_by_path(&id, repo,
7733 commit_id, argv[i]);
7734 if (error)
7735 break;
7739 error = got_object_get_type(&obj_type, repo, id);
7740 if (error)
7741 break;
7743 switch (obj_type) {
7744 case GOT_OBJ_TYPE_BLOB:
7745 error = cat_blob(id, repo, stdout);
7746 break;
7747 case GOT_OBJ_TYPE_TREE:
7748 error = cat_tree(id, repo, stdout);
7749 break;
7750 case GOT_OBJ_TYPE_COMMIT:
7751 error = cat_commit(id, repo, stdout);
7752 break;
7753 case GOT_OBJ_TYPE_TAG:
7754 error = cat_tag(id, repo, stdout);
7755 break;
7756 default:
7757 error = got_error(GOT_ERR_OBJ_TYPE);
7758 break;
7760 if (error)
7761 break;
7762 free(label);
7763 label = NULL;
7764 free(id);
7765 id = NULL;
7767 done:
7768 free(label);
7769 free(id);
7770 free(commit_id);
7771 if (worktree)
7772 got_worktree_close(worktree);
7773 if (repo) {
7774 const struct got_error *repo_error;
7775 repo_error = got_repo_close(repo);
7776 if (error == NULL)
7777 error = repo_error;
7779 return error;