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 struct got_fetch_progress_arg {
973 char last_scaled_size[FMT_SCALED_STRSIZE];
974 int last_p_indexed;
975 int last_p_resolved;
976 };
978 static const struct got_error *
979 fetch_progress(void *arg, const char *message, off_t packfile_size,
980 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
982 struct got_fetch_progress_arg *a = arg;
983 char scaled_size[FMT_SCALED_STRSIZE];
984 int p_indexed, p_resolved;
985 int print_size = 0, print_indexed = 0, print_resolved = 0;
987 if (message && message[0] != '\0' && message[0] != '\n') {
988 printf("\rserver: %s", message);
989 if (strchr(message, '\n') == 0)
990 printf("\n");
991 fflush(stdout);
994 if (packfile_size > 0 || nobj_indexed > 0) {
995 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
996 (a->last_scaled_size[0] == '\0' ||
997 strcmp(scaled_size, a->last_scaled_size)) != 0) {
998 print_size = 1;
999 if (strlcpy(a->last_scaled_size, scaled_size,
1000 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1001 return got_error(GOT_ERR_NO_SPACE);
1003 if (nobj_indexed > 0) {
1004 p_indexed = (nobj_indexed * 100) / nobj_total;
1005 if (p_indexed != a->last_p_indexed) {
1006 a->last_p_indexed = p_indexed;
1007 print_indexed = 1;
1008 print_size = 1;
1011 if (nobj_resolved > 0) {
1012 p_resolved = (nobj_resolved * 100) /
1013 (nobj_total - nobj_loose);
1014 if (p_resolved != a->last_p_resolved) {
1015 a->last_p_resolved = p_resolved;
1016 print_resolved = 1;
1017 print_indexed = 1;
1018 print_size = 1;
1023 if (print_size || print_indexed || print_resolved)
1024 printf("\r");
1025 if (print_size)
1026 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1027 if (print_indexed)
1028 printf("; indexing %d%%", p_indexed);
1029 if (print_resolved)
1030 printf("; resolving deltas %d%%", p_resolved);
1031 if (print_size || print_indexed || print_resolved)
1032 fflush(stdout);
1034 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
1035 nobj_resolved == nobj_total - nobj_loose)
1036 printf("\nWriting pack index...\n");
1038 return NULL;
1041 static const struct got_error *
1042 cmd_clone(int argc, char *argv[])
1044 const struct got_error *err = NULL;
1045 const char *uri, *branch_filter, *dirname;
1046 char *proto, *host, *port, *repo_name, *server_path;
1047 char *default_destdir = NULL, *id_str = NULL;
1048 const char *repo_path;
1049 struct got_repository *repo = NULL;
1050 struct got_pathlist_head refs, symrefs;
1051 struct got_pathlist_entry *pe;
1052 struct got_object_id *pack_hash = NULL;
1053 int ch, fetchfd = -1;
1054 struct got_fetch_progress_arg fpa;
1056 TAILQ_INIT(&refs);
1057 TAILQ_INIT(&symrefs);
1059 while ((ch = getopt(argc, argv, "b:")) != -1) {
1060 switch (ch) {
1061 case 'b':
1062 branch_filter = optarg;
1063 break;
1064 default:
1065 usage_clone();
1066 break;
1069 argc -= optind;
1070 argv += optind;
1071 uri = argv[0];
1072 if(argc == 1)
1073 dirname = NULL;
1074 else if(argc == 2)
1075 dirname = argv[1];
1076 else
1077 usage_clone();
1079 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1080 &repo_name, argv[0]);
1081 if (err)
1082 goto done;
1084 if (dirname == NULL) {
1085 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1086 err = got_error_from_errno("asprintf");
1087 goto done;
1089 repo_path = default_destdir;
1090 } else
1091 repo_path = dirname;
1093 err = got_path_mkdir(repo_path);
1094 if (err)
1095 goto done;
1097 err = got_repo_init(repo_path);
1098 if (err)
1099 goto done;
1101 err = got_repo_open(&repo, repo_path, NULL);
1102 if (err)
1103 goto done;
1105 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1106 if (err)
1107 goto done;
1109 printf("Connected to %s:%s\n", host, port);
1111 fpa.last_scaled_size[0] = '\0';
1112 fpa.last_p_indexed = -1;
1113 fpa.last_p_resolved = -1;
1114 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1115 repo, fetch_progress, &fpa);
1116 if (err)
1117 goto done;
1119 err = got_object_id_str(&id_str, pack_hash);
1120 if (err)
1121 goto done;
1122 printf("Fetched %s.pack\n", id_str);
1123 free(id_str);
1125 /* Set up references provided with the pack file. */
1126 TAILQ_FOREACH(pe, &refs, entry) {
1127 const char *refname = pe->path;
1128 struct got_object_id *id = pe->data;
1129 struct got_reference *ref;
1132 err = got_ref_alloc(&ref, refname, id);
1133 if (err)
1134 goto done;
1136 #if 0
1137 err = got_object_id_str(&id_str, id);
1138 if (err)
1139 goto done;
1140 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1141 free(id_str);
1142 #endif
1143 err = got_ref_write(ref, repo);
1144 got_ref_close(ref);
1145 if (err)
1146 goto done;
1149 /* Set the HEAD reference if the server provided one. */
1150 TAILQ_FOREACH(pe, &symrefs, entry) {
1151 struct got_reference *symref, *target_ref;
1152 const char *refname = pe->path;
1153 const char *target = pe->data;
1155 if (strcmp(refname, GOT_REF_HEAD) != 0)
1156 continue;
1158 err = got_ref_open(&target_ref, repo, target, 0);
1159 if (err) {
1160 if (err->code == GOT_ERR_NOT_REF)
1161 continue;
1162 goto done;
1165 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1166 got_ref_close(target_ref);
1167 if (err)
1168 goto done;
1170 printf("Setting %s to %s\n", GOT_REF_HEAD,
1171 got_ref_get_symref_target(symref));
1173 err = got_ref_write(symref, repo);
1174 got_ref_close(symref);
1175 break;
1178 done:
1179 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1180 err = got_error_from_errno("close");
1181 if (repo)
1182 got_repo_close(repo);
1183 TAILQ_FOREACH(pe, &refs, entry) {
1184 free((void *)pe->path);
1185 free(pe->data);
1187 got_pathlist_free(&refs);
1188 TAILQ_FOREACH(pe, &symrefs, entry) {
1189 free((void *)pe->path);
1190 free(pe->data);
1192 got_pathlist_free(&symrefs);
1193 free(pack_hash);
1194 free(proto);
1195 free(host);
1196 free(port);
1197 free(server_path);
1198 free(repo_name);
1199 free(default_destdir);
1200 return err;
1203 static const struct got_error *
1204 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1206 static char msg[512];
1207 const char *branch_name;
1209 if (got_ref_is_symbolic(ref))
1210 branch_name = got_ref_get_symref_target(ref);
1211 else
1212 branch_name = got_ref_get_name(ref);
1214 if (strncmp("refs/heads/", branch_name, 11) == 0)
1215 branch_name += 11;
1217 snprintf(msg, sizeof(msg),
1218 "target commit is not contained in branch '%s'; "
1219 "the branch to use must be specified with -b; "
1220 "if necessary a new branch can be created for "
1221 "this commit with 'got branch -c %s BRANCH_NAME'",
1222 branch_name, commit_id_str);
1224 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1227 static const struct got_error *
1228 cmd_checkout(int argc, char *argv[])
1230 const struct got_error *error = NULL;
1231 struct got_repository *repo = NULL;
1232 struct got_reference *head_ref = NULL;
1233 struct got_worktree *worktree = NULL;
1234 char *repo_path = NULL;
1235 char *worktree_path = NULL;
1236 const char *path_prefix = "";
1237 const char *branch_name = GOT_REF_HEAD;
1238 char *commit_id_str = NULL;
1239 int ch, same_path_prefix, allow_nonempty = 0;
1240 struct got_pathlist_head paths;
1241 struct got_checkout_progress_arg cpa;
1243 TAILQ_INIT(&paths);
1245 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1246 switch (ch) {
1247 case 'b':
1248 branch_name = optarg;
1249 break;
1250 case 'c':
1251 commit_id_str = strdup(optarg);
1252 if (commit_id_str == NULL)
1253 return got_error_from_errno("strdup");
1254 break;
1255 case 'E':
1256 allow_nonempty = 1;
1257 break;
1258 case 'p':
1259 path_prefix = optarg;
1260 break;
1261 default:
1262 usage_checkout();
1263 /* NOTREACHED */
1267 argc -= optind;
1268 argv += optind;
1270 #ifndef PROFILE
1271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1272 "unveil", NULL) == -1)
1273 err(1, "pledge");
1274 #endif
1275 if (argc == 1) {
1276 char *cwd, *base, *dotgit;
1277 repo_path = realpath(argv[0], NULL);
1278 if (repo_path == NULL)
1279 return got_error_from_errno2("realpath", argv[0]);
1280 cwd = getcwd(NULL, 0);
1281 if (cwd == NULL) {
1282 error = got_error_from_errno("getcwd");
1283 goto done;
1285 if (path_prefix[0]) {
1286 base = basename(path_prefix);
1287 if (base == NULL) {
1288 error = got_error_from_errno2("basename",
1289 path_prefix);
1290 goto done;
1292 } else {
1293 base = basename(repo_path);
1294 if (base == NULL) {
1295 error = got_error_from_errno2("basename",
1296 repo_path);
1297 goto done;
1300 dotgit = strstr(base, ".git");
1301 if (dotgit)
1302 *dotgit = '\0';
1303 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1304 error = got_error_from_errno("asprintf");
1305 free(cwd);
1306 goto done;
1308 free(cwd);
1309 } else if (argc == 2) {
1310 repo_path = realpath(argv[0], NULL);
1311 if (repo_path == NULL) {
1312 error = got_error_from_errno2("realpath", argv[0]);
1313 goto done;
1315 worktree_path = realpath(argv[1], NULL);
1316 if (worktree_path == NULL) {
1317 if (errno != ENOENT) {
1318 error = got_error_from_errno2("realpath",
1319 argv[1]);
1320 goto done;
1322 worktree_path = strdup(argv[1]);
1323 if (worktree_path == NULL) {
1324 error = got_error_from_errno("strdup");
1325 goto done;
1328 } else
1329 usage_checkout();
1331 got_path_strip_trailing_slashes(repo_path);
1332 got_path_strip_trailing_slashes(worktree_path);
1334 error = got_repo_open(&repo, repo_path, NULL);
1335 if (error != NULL)
1336 goto done;
1338 /* Pre-create work tree path for unveil(2) */
1339 error = got_path_mkdir(worktree_path);
1340 if (error) {
1341 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1342 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1343 goto done;
1344 if (!allow_nonempty &&
1345 !got_path_dir_is_empty(worktree_path)) {
1346 error = got_error_path(worktree_path,
1347 GOT_ERR_DIR_NOT_EMPTY);
1348 goto done;
1352 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1353 if (error)
1354 goto done;
1356 error = got_ref_open(&head_ref, repo, branch_name, 0);
1357 if (error != NULL)
1358 goto done;
1360 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1361 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1362 goto done;
1364 error = got_worktree_open(&worktree, worktree_path);
1365 if (error != NULL)
1366 goto done;
1368 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1369 path_prefix);
1370 if (error != NULL)
1371 goto done;
1372 if (!same_path_prefix) {
1373 error = got_error(GOT_ERR_PATH_PREFIX);
1374 goto done;
1377 if (commit_id_str) {
1378 struct got_object_id *commit_id;
1379 error = got_repo_match_object_id(&commit_id, NULL,
1380 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1381 if (error)
1382 goto done;
1383 error = check_linear_ancestry(commit_id,
1384 got_worktree_get_base_commit_id(worktree), 0, repo);
1385 if (error != NULL) {
1386 free(commit_id);
1387 if (error->code == GOT_ERR_ANCESTRY) {
1388 error = checkout_ancestry_error(
1389 head_ref, commit_id_str);
1391 goto done;
1393 error = check_same_branch(commit_id, head_ref, NULL, repo);
1394 if (error) {
1395 if (error->code == GOT_ERR_ANCESTRY) {
1396 error = checkout_ancestry_error(
1397 head_ref, commit_id_str);
1399 goto done;
1401 error = got_worktree_set_base_commit_id(worktree, repo,
1402 commit_id);
1403 free(commit_id);
1404 if (error)
1405 goto done;
1408 error = got_pathlist_append(&paths, "", NULL);
1409 if (error)
1410 goto done;
1411 cpa.worktree_path = worktree_path;
1412 cpa.had_base_commit_ref_error = 0;
1413 error = got_worktree_checkout_files(worktree, &paths, repo,
1414 checkout_progress, &cpa, check_cancelled, NULL);
1415 if (error != NULL)
1416 goto done;
1418 printf("Now shut up and hack\n");
1419 if (cpa.had_base_commit_ref_error)
1420 show_worktree_base_ref_warning();
1421 done:
1422 got_pathlist_free(&paths);
1423 free(commit_id_str);
1424 free(repo_path);
1425 free(worktree_path);
1426 return error;
1429 __dead static void
1430 usage_update(void)
1432 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1433 getprogname());
1434 exit(1);
1437 static const struct got_error *
1438 update_progress(void *arg, unsigned char status, const char *path)
1440 int *did_something = arg;
1442 if (status == GOT_STATUS_EXISTS ||
1443 status == GOT_STATUS_BASE_REF_ERR)
1444 return NULL;
1446 *did_something = 1;
1448 /* Base commit bump happens silently. */
1449 if (status == GOT_STATUS_BUMP_BASE)
1450 return NULL;
1452 while (path[0] == '/')
1453 path++;
1454 printf("%c %s\n", status, path);
1455 return NULL;
1458 static const struct got_error *
1459 switch_head_ref(struct got_reference *head_ref,
1460 struct got_object_id *commit_id, struct got_worktree *worktree,
1461 struct got_repository *repo)
1463 const struct got_error *err = NULL;
1464 char *base_id_str;
1465 int ref_has_moved = 0;
1467 /* Trivial case: switching between two different references. */
1468 if (strcmp(got_ref_get_name(head_ref),
1469 got_worktree_get_head_ref_name(worktree)) != 0) {
1470 printf("Switching work tree from %s to %s\n",
1471 got_worktree_get_head_ref_name(worktree),
1472 got_ref_get_name(head_ref));
1473 return got_worktree_set_head_ref(worktree, head_ref);
1476 err = check_linear_ancestry(commit_id,
1477 got_worktree_get_base_commit_id(worktree), 0, repo);
1478 if (err) {
1479 if (err->code != GOT_ERR_ANCESTRY)
1480 return err;
1481 ref_has_moved = 1;
1483 if (!ref_has_moved)
1484 return NULL;
1486 /* Switching to a rebased branch with the same reference name. */
1487 err = got_object_id_str(&base_id_str,
1488 got_worktree_get_base_commit_id(worktree));
1489 if (err)
1490 return err;
1491 printf("Reference %s now points at a different branch\n",
1492 got_worktree_get_head_ref_name(worktree));
1493 printf("Switching work tree from %s to %s\n", base_id_str,
1494 got_worktree_get_head_ref_name(worktree));
1495 return NULL;
1498 static const struct got_error *
1499 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1501 const struct got_error *err;
1502 int in_progress;
1504 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1505 if (err)
1506 return err;
1507 if (in_progress)
1508 return got_error(GOT_ERR_REBASING);
1510 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1511 if (err)
1512 return err;
1513 if (in_progress)
1514 return got_error(GOT_ERR_HISTEDIT_BUSY);
1516 return NULL;
1519 static const struct got_error *
1520 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1521 char *argv[], struct got_worktree *worktree)
1523 const struct got_error *err = NULL;
1524 char *path;
1525 int i;
1527 if (argc == 0) {
1528 path = strdup("");
1529 if (path == NULL)
1530 return got_error_from_errno("strdup");
1531 return got_pathlist_append(paths, path, NULL);
1534 for (i = 0; i < argc; i++) {
1535 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1536 if (err)
1537 break;
1538 err = got_pathlist_append(paths, path, NULL);
1539 if (err) {
1540 free(path);
1541 break;
1545 return err;
1548 static const struct got_error *
1549 cmd_update(int argc, char *argv[])
1551 const struct got_error *error = NULL;
1552 struct got_repository *repo = NULL;
1553 struct got_worktree *worktree = NULL;
1554 char *worktree_path = NULL;
1555 struct got_object_id *commit_id = NULL;
1556 char *commit_id_str = NULL;
1557 const char *branch_name = NULL;
1558 struct got_reference *head_ref = NULL;
1559 struct got_pathlist_head paths;
1560 struct got_pathlist_entry *pe;
1561 int ch, did_something = 0;
1563 TAILQ_INIT(&paths);
1565 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1566 switch (ch) {
1567 case 'b':
1568 branch_name = optarg;
1569 break;
1570 case 'c':
1571 commit_id_str = strdup(optarg);
1572 if (commit_id_str == NULL)
1573 return got_error_from_errno("strdup");
1574 break;
1575 default:
1576 usage_update();
1577 /* NOTREACHED */
1581 argc -= optind;
1582 argv += optind;
1584 #ifndef PROFILE
1585 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1586 "unveil", NULL) == -1)
1587 err(1, "pledge");
1588 #endif
1589 worktree_path = getcwd(NULL, 0);
1590 if (worktree_path == NULL) {
1591 error = got_error_from_errno("getcwd");
1592 goto done;
1594 error = got_worktree_open(&worktree, worktree_path);
1595 if (error)
1596 goto done;
1598 error = check_rebase_or_histedit_in_progress(worktree);
1599 if (error)
1600 goto done;
1602 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1603 NULL);
1604 if (error != NULL)
1605 goto done;
1607 error = apply_unveil(got_repo_get_path(repo), 0,
1608 got_worktree_get_root_path(worktree));
1609 if (error)
1610 goto done;
1612 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1613 if (error)
1614 goto done;
1616 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1617 got_worktree_get_head_ref_name(worktree), 0);
1618 if (error != NULL)
1619 goto done;
1620 if (commit_id_str == NULL) {
1621 error = got_ref_resolve(&commit_id, repo, head_ref);
1622 if (error != NULL)
1623 goto done;
1624 error = got_object_id_str(&commit_id_str, commit_id);
1625 if (error != NULL)
1626 goto done;
1627 } else {
1628 error = got_repo_match_object_id(&commit_id, NULL,
1629 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1630 free(commit_id_str);
1631 commit_id_str = NULL;
1632 if (error)
1633 goto done;
1634 error = got_object_id_str(&commit_id_str, commit_id);
1635 if (error)
1636 goto done;
1639 if (branch_name) {
1640 struct got_object_id *head_commit_id;
1641 TAILQ_FOREACH(pe, &paths, entry) {
1642 if (pe->path_len == 0)
1643 continue;
1644 error = got_error_msg(GOT_ERR_BAD_PATH,
1645 "switching between branches requires that "
1646 "the entire work tree gets updated");
1647 goto done;
1649 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1650 if (error)
1651 goto done;
1652 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1653 repo);
1654 free(head_commit_id);
1655 if (error != NULL)
1656 goto done;
1657 error = check_same_branch(commit_id, head_ref, NULL, repo);
1658 if (error)
1659 goto done;
1660 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1661 if (error)
1662 goto done;
1663 } else {
1664 error = check_linear_ancestry(commit_id,
1665 got_worktree_get_base_commit_id(worktree), 0, repo);
1666 if (error != NULL) {
1667 if (error->code == GOT_ERR_ANCESTRY)
1668 error = got_error(GOT_ERR_BRANCH_MOVED);
1669 goto done;
1671 error = check_same_branch(commit_id, head_ref, NULL, repo);
1672 if (error)
1673 goto done;
1676 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1677 commit_id) != 0) {
1678 error = got_worktree_set_base_commit_id(worktree, repo,
1679 commit_id);
1680 if (error)
1681 goto done;
1684 error = got_worktree_checkout_files(worktree, &paths, repo,
1685 update_progress, &did_something, check_cancelled, NULL);
1686 if (error != NULL)
1687 goto done;
1689 if (did_something)
1690 printf("Updated to commit %s\n", commit_id_str);
1691 else
1692 printf("Already up-to-date\n");
1693 done:
1694 free(worktree_path);
1695 TAILQ_FOREACH(pe, &paths, entry)
1696 free((char *)pe->path);
1697 got_pathlist_free(&paths);
1698 free(commit_id);
1699 free(commit_id_str);
1700 return error;
1703 static const struct got_error *
1704 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1705 const char *path, int diff_context, int ignore_whitespace,
1706 struct got_repository *repo)
1708 const struct got_error *err = NULL;
1709 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1711 if (blob_id1) {
1712 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1713 if (err)
1714 goto done;
1717 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1718 if (err)
1719 goto done;
1721 while (path[0] == '/')
1722 path++;
1723 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1724 ignore_whitespace, stdout);
1725 done:
1726 if (blob1)
1727 got_object_blob_close(blob1);
1728 got_object_blob_close(blob2);
1729 return err;
1732 static const struct got_error *
1733 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1734 const char *path, int diff_context, int ignore_whitespace,
1735 struct got_repository *repo)
1737 const struct got_error *err = NULL;
1738 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1739 struct got_diff_blob_output_unidiff_arg arg;
1741 if (tree_id1) {
1742 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1743 if (err)
1744 goto done;
1747 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1748 if (err)
1749 goto done;
1751 arg.diff_context = diff_context;
1752 arg.ignore_whitespace = ignore_whitespace;
1753 arg.outfile = stdout;
1754 while (path[0] == '/')
1755 path++;
1756 err = got_diff_tree(tree1, tree2, path, path, repo,
1757 got_diff_blob_output_unidiff, &arg, 1);
1758 done:
1759 if (tree1)
1760 got_object_tree_close(tree1);
1761 if (tree2)
1762 got_object_tree_close(tree2);
1763 return err;
1766 static const struct got_error *
1767 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1768 const char *path, int diff_context, struct got_repository *repo)
1770 const struct got_error *err = NULL;
1771 struct got_commit_object *pcommit = NULL;
1772 char *id_str1 = NULL, *id_str2 = NULL;
1773 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1774 struct got_object_qid *qid;
1776 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1777 if (qid != NULL) {
1778 err = got_object_open_as_commit(&pcommit, repo,
1779 qid->id);
1780 if (err)
1781 return err;
1784 if (path && path[0] != '\0') {
1785 int obj_type;
1786 err = got_object_id_by_path(&obj_id2, repo, id, path);
1787 if (err)
1788 goto done;
1789 err = got_object_id_str(&id_str2, obj_id2);
1790 if (err) {
1791 free(obj_id2);
1792 goto done;
1794 if (pcommit) {
1795 err = got_object_id_by_path(&obj_id1, repo,
1796 qid->id, path);
1797 if (err) {
1798 free(obj_id2);
1799 goto done;
1801 err = got_object_id_str(&id_str1, obj_id1);
1802 if (err) {
1803 free(obj_id2);
1804 goto done;
1807 err = got_object_get_type(&obj_type, repo, obj_id2);
1808 if (err) {
1809 free(obj_id2);
1810 goto done;
1812 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1813 switch (obj_type) {
1814 case GOT_OBJ_TYPE_BLOB:
1815 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1816 0, repo);
1817 break;
1818 case GOT_OBJ_TYPE_TREE:
1819 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1820 0, repo);
1821 break;
1822 default:
1823 err = got_error(GOT_ERR_OBJ_TYPE);
1824 break;
1826 free(obj_id1);
1827 free(obj_id2);
1828 } else {
1829 obj_id2 = got_object_commit_get_tree_id(commit);
1830 err = got_object_id_str(&id_str2, obj_id2);
1831 if (err)
1832 goto done;
1833 obj_id1 = got_object_commit_get_tree_id(pcommit);
1834 err = got_object_id_str(&id_str1, obj_id1);
1835 if (err)
1836 goto done;
1837 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1838 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1840 done:
1841 free(id_str1);
1842 free(id_str2);
1843 if (pcommit)
1844 got_object_commit_close(pcommit);
1845 return err;
1848 static char *
1849 get_datestr(time_t *time, char *datebuf)
1851 struct tm mytm, *tm;
1852 char *p, *s;
1854 tm = gmtime_r(time, &mytm);
1855 if (tm == NULL)
1856 return NULL;
1857 s = asctime_r(tm, datebuf);
1858 if (s == NULL)
1859 return NULL;
1860 p = strchr(s, '\n');
1861 if (p)
1862 *p = '\0';
1863 return s;
1866 static const struct got_error *
1867 match_logmsg(int *have_match, struct got_object_id *id,
1868 struct got_commit_object *commit, regex_t *regex)
1870 const struct got_error *err = NULL;
1871 regmatch_t regmatch;
1872 char *id_str = NULL, *logmsg = NULL;
1874 *have_match = 0;
1876 err = got_object_id_str(&id_str, id);
1877 if (err)
1878 return err;
1880 err = got_object_commit_get_logmsg(&logmsg, commit);
1881 if (err)
1882 goto done;
1884 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1885 *have_match = 1;
1886 done:
1887 free(id_str);
1888 free(logmsg);
1889 return err;
1892 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1894 static const struct got_error *
1895 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1896 struct got_repository *repo, const char *path, int show_patch,
1897 int diff_context, struct got_reflist_head *refs)
1899 const struct got_error *err = NULL;
1900 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1901 char datebuf[26];
1902 time_t committer_time;
1903 const char *author, *committer;
1904 char *refs_str = NULL;
1905 struct got_reflist_entry *re;
1907 SIMPLEQ_FOREACH(re, refs, entry) {
1908 char *s;
1909 const char *name;
1910 struct got_tag_object *tag = NULL;
1911 int cmp;
1913 name = got_ref_get_name(re->ref);
1914 if (strcmp(name, GOT_REF_HEAD) == 0)
1915 continue;
1916 if (strncmp(name, "refs/", 5) == 0)
1917 name += 5;
1918 if (strncmp(name, "got/", 4) == 0)
1919 continue;
1920 if (strncmp(name, "heads/", 6) == 0)
1921 name += 6;
1922 if (strncmp(name, "remotes/", 8) == 0)
1923 name += 8;
1924 if (strncmp(name, "tags/", 5) == 0) {
1925 err = got_object_open_as_tag(&tag, repo, re->id);
1926 if (err) {
1927 if (err->code != GOT_ERR_OBJ_TYPE)
1928 return err;
1929 /* Ref points at something other than a tag. */
1930 err = NULL;
1931 tag = NULL;
1934 cmp = got_object_id_cmp(tag ?
1935 got_object_tag_get_object_id(tag) : re->id, id);
1936 if (tag)
1937 got_object_tag_close(tag);
1938 if (cmp != 0)
1939 continue;
1940 s = refs_str;
1941 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1942 name) == -1) {
1943 err = got_error_from_errno("asprintf");
1944 free(s);
1945 return err;
1947 free(s);
1949 err = got_object_id_str(&id_str, id);
1950 if (err)
1951 return err;
1953 printf(GOT_COMMIT_SEP_STR);
1954 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1955 refs_str ? refs_str : "", refs_str ? ")" : "");
1956 free(id_str);
1957 id_str = NULL;
1958 free(refs_str);
1959 refs_str = NULL;
1960 printf("from: %s\n", got_object_commit_get_author(commit));
1961 committer_time = got_object_commit_get_committer_time(commit);
1962 datestr = get_datestr(&committer_time, datebuf);
1963 if (datestr)
1964 printf("date: %s UTC\n", datestr);
1965 author = got_object_commit_get_author(commit);
1966 committer = got_object_commit_get_committer(commit);
1967 if (strcmp(author, committer) != 0)
1968 printf("via: %s\n", committer);
1969 if (got_object_commit_get_nparents(commit) > 1) {
1970 const struct got_object_id_queue *parent_ids;
1971 struct got_object_qid *qid;
1972 int n = 1;
1973 parent_ids = got_object_commit_get_parent_ids(commit);
1974 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1975 err = got_object_id_str(&id_str, qid->id);
1976 if (err)
1977 return err;
1978 printf("parent %d: %s\n", n++, id_str);
1979 free(id_str);
1983 err = got_object_commit_get_logmsg(&logmsg0, commit);
1984 if (err)
1985 return err;
1987 logmsg = logmsg0;
1988 do {
1989 line = strsep(&logmsg, "\n");
1990 if (line)
1991 printf(" %s\n", line);
1992 } while (line);
1993 free(logmsg0);
1995 if (show_patch) {
1996 err = print_patch(commit, id, path, diff_context, repo);
1997 if (err == 0)
1998 printf("\n");
2001 if (fflush(stdout) != 0 && err == NULL)
2002 err = got_error_from_errno("fflush");
2003 return err;
2006 static const struct got_error *
2007 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2008 const char *path, int show_patch, const char *search_pattern,
2009 int diff_context, int limit, int log_branches,
2010 struct got_reflist_head *refs)
2012 const struct got_error *err;
2013 struct got_commit_graph *graph;
2014 regex_t regex;
2015 int have_match;
2017 if (search_pattern &&
2018 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2019 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2021 err = got_commit_graph_open(&graph, path, !log_branches);
2022 if (err)
2023 return err;
2024 err = got_commit_graph_iter_start(graph, root_id, repo,
2025 check_cancelled, NULL);
2026 if (err)
2027 goto done;
2028 for (;;) {
2029 struct got_commit_object *commit;
2030 struct got_object_id *id;
2032 if (sigint_received || sigpipe_received)
2033 break;
2035 err = got_commit_graph_iter_next(&id, graph, repo,
2036 check_cancelled, NULL);
2037 if (err) {
2038 if (err->code == GOT_ERR_ITER_COMPLETED)
2039 err = NULL;
2040 break;
2042 if (id == NULL)
2043 break;
2045 err = got_object_open_as_commit(&commit, repo, id);
2046 if (err)
2047 break;
2049 if (search_pattern) {
2050 err = match_logmsg(&have_match, id, commit, &regex);
2051 if (err) {
2052 got_object_commit_close(commit);
2053 break;
2055 if (have_match == 0) {
2056 got_object_commit_close(commit);
2057 continue;
2061 err = print_commit(commit, id, repo, path, show_patch,
2062 diff_context, refs);
2063 got_object_commit_close(commit);
2064 if (err || (limit && --limit == 0))
2065 break;
2067 done:
2068 if (search_pattern)
2069 regfree(&regex);
2070 got_commit_graph_close(graph);
2071 return err;
2074 __dead static void
2075 usage_log(void)
2077 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2078 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2079 exit(1);
2082 static int
2083 get_default_log_limit(void)
2085 const char *got_default_log_limit;
2086 long long n;
2087 const char *errstr;
2089 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2090 if (got_default_log_limit == NULL)
2091 return 0;
2092 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2093 if (errstr != NULL)
2094 return 0;
2095 return n;
2098 static const struct got_error *
2099 cmd_log(int argc, char *argv[])
2101 const struct got_error *error;
2102 struct got_repository *repo = NULL;
2103 struct got_worktree *worktree = NULL;
2104 struct got_commit_object *commit = NULL;
2105 struct got_object_id *id = NULL;
2106 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2107 const char *start_commit = NULL, *search_pattern = NULL;
2108 int diff_context = -1, ch;
2109 int show_patch = 0, limit = 0, log_branches = 0;
2110 const char *errstr;
2111 struct got_reflist_head refs;
2113 SIMPLEQ_INIT(&refs);
2115 #ifndef PROFILE
2116 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2117 NULL)
2118 == -1)
2119 err(1, "pledge");
2120 #endif
2122 limit = get_default_log_limit();
2124 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2125 switch (ch) {
2126 case 'p':
2127 show_patch = 1;
2128 break;
2129 case 'c':
2130 start_commit = optarg;
2131 break;
2132 case 'C':
2133 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2134 &errstr);
2135 if (errstr != NULL)
2136 err(1, "-C option %s", errstr);
2137 break;
2138 case 'l':
2139 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2140 if (errstr != NULL)
2141 err(1, "-l option %s", errstr);
2142 break;
2143 case 'b':
2144 log_branches = 1;
2145 break;
2146 case 'r':
2147 repo_path = realpath(optarg, NULL);
2148 if (repo_path == NULL)
2149 return got_error_from_errno2("realpath",
2150 optarg);
2151 got_path_strip_trailing_slashes(repo_path);
2152 break;
2153 case 's':
2154 search_pattern = optarg;
2155 break;
2156 default:
2157 usage_log();
2158 /* NOTREACHED */
2162 argc -= optind;
2163 argv += optind;
2165 if (diff_context == -1)
2166 diff_context = 3;
2167 else if (!show_patch)
2168 errx(1, "-C reguires -p");
2170 cwd = getcwd(NULL, 0);
2171 if (cwd == NULL) {
2172 error = got_error_from_errno("getcwd");
2173 goto done;
2176 error = got_worktree_open(&worktree, cwd);
2177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2178 goto done;
2179 error = NULL;
2181 if (argc == 0) {
2182 path = strdup("");
2183 if (path == NULL) {
2184 error = got_error_from_errno("strdup");
2185 goto done;
2187 } else if (argc == 1) {
2188 if (worktree) {
2189 error = got_worktree_resolve_path(&path, worktree,
2190 argv[0]);
2191 if (error)
2192 goto done;
2193 } else {
2194 path = strdup(argv[0]);
2195 if (path == NULL) {
2196 error = got_error_from_errno("strdup");
2197 goto done;
2200 } else
2201 usage_log();
2203 if (repo_path == NULL) {
2204 repo_path = worktree ?
2205 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2207 if (repo_path == NULL) {
2208 error = got_error_from_errno("strdup");
2209 goto done;
2212 error = got_repo_open(&repo, repo_path, NULL);
2213 if (error != NULL)
2214 goto done;
2216 error = apply_unveil(got_repo_get_path(repo), 1,
2217 worktree ? got_worktree_get_root_path(worktree) : NULL);
2218 if (error)
2219 goto done;
2221 if (start_commit == NULL) {
2222 struct got_reference *head_ref;
2223 error = got_ref_open(&head_ref, repo,
2224 worktree ? got_worktree_get_head_ref_name(worktree)
2225 : GOT_REF_HEAD, 0);
2226 if (error != NULL)
2227 return error;
2228 error = got_ref_resolve(&id, repo, head_ref);
2229 got_ref_close(head_ref);
2230 if (error != NULL)
2231 return error;
2232 error = got_object_open_as_commit(&commit, repo, id);
2233 } else {
2234 struct got_reference *ref;
2235 error = got_ref_open(&ref, repo, start_commit, 0);
2236 if (error == NULL) {
2237 int obj_type;
2238 error = got_ref_resolve(&id, repo, ref);
2239 got_ref_close(ref);
2240 if (error != NULL)
2241 goto done;
2242 error = got_object_get_type(&obj_type, repo, id);
2243 if (error != NULL)
2244 goto done;
2245 if (obj_type == GOT_OBJ_TYPE_TAG) {
2246 struct got_tag_object *tag;
2247 error = got_object_open_as_tag(&tag, repo, id);
2248 if (error != NULL)
2249 goto done;
2250 if (got_object_tag_get_object_type(tag) !=
2251 GOT_OBJ_TYPE_COMMIT) {
2252 got_object_tag_close(tag);
2253 error = got_error(GOT_ERR_OBJ_TYPE);
2254 goto done;
2256 free(id);
2257 id = got_object_id_dup(
2258 got_object_tag_get_object_id(tag));
2259 if (id == NULL)
2260 error = got_error_from_errno(
2261 "got_object_id_dup");
2262 got_object_tag_close(tag);
2263 if (error)
2264 goto done;
2265 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2266 error = got_error(GOT_ERR_OBJ_TYPE);
2267 goto done;
2269 error = got_object_open_as_commit(&commit, repo, id);
2270 if (error != NULL)
2271 goto done;
2273 if (commit == NULL) {
2274 error = got_repo_match_object_id_prefix(&id,
2275 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2276 if (error != NULL)
2277 return error;
2280 if (error != NULL)
2281 goto done;
2283 if (worktree) {
2284 const char *prefix = got_worktree_get_path_prefix(worktree);
2285 char *p;
2286 if (asprintf(&p, "%s%s%s", prefix,
2287 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2288 error = got_error_from_errno("asprintf");
2289 goto done;
2291 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2292 free(p);
2293 } else
2294 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2295 if (error != NULL)
2296 goto done;
2297 if (in_repo_path) {
2298 free(path);
2299 path = in_repo_path;
2302 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2303 if (error)
2304 goto done;
2306 error = print_commits(id, repo, path, show_patch, search_pattern,
2307 diff_context, limit, log_branches, &refs);
2308 done:
2309 free(path);
2310 free(repo_path);
2311 free(cwd);
2312 free(id);
2313 if (worktree)
2314 got_worktree_close(worktree);
2315 if (repo) {
2316 const struct got_error *repo_error;
2317 repo_error = got_repo_close(repo);
2318 if (error == NULL)
2319 error = repo_error;
2321 got_ref_list_free(&refs);
2322 return error;
2325 __dead static void
2326 usage_diff(void)
2328 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2329 "[-w] [object1 object2 | path]\n", getprogname());
2330 exit(1);
2333 struct print_diff_arg {
2334 struct got_repository *repo;
2335 struct got_worktree *worktree;
2336 int diff_context;
2337 const char *id_str;
2338 int header_shown;
2339 int diff_staged;
2340 int ignore_whitespace;
2343 static const struct got_error *
2344 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2345 const char *path, struct got_object_id *blob_id,
2346 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2347 int dirfd, const char *de_name)
2349 struct print_diff_arg *a = arg;
2350 const struct got_error *err = NULL;
2351 struct got_blob_object *blob1 = NULL;
2352 int fd = -1;
2353 FILE *f2 = NULL;
2354 char *abspath = NULL, *label1 = NULL;
2355 struct stat sb;
2357 if (a->diff_staged) {
2358 if (staged_status != GOT_STATUS_MODIFY &&
2359 staged_status != GOT_STATUS_ADD &&
2360 staged_status != GOT_STATUS_DELETE)
2361 return NULL;
2362 } else {
2363 if (staged_status == GOT_STATUS_DELETE)
2364 return NULL;
2365 if (status == GOT_STATUS_NONEXISTENT)
2366 return got_error_set_errno(ENOENT, path);
2367 if (status != GOT_STATUS_MODIFY &&
2368 status != GOT_STATUS_ADD &&
2369 status != GOT_STATUS_DELETE &&
2370 status != GOT_STATUS_CONFLICT)
2371 return NULL;
2374 if (!a->header_shown) {
2375 printf("diff %s %s%s\n", a->id_str,
2376 got_worktree_get_root_path(a->worktree),
2377 a->diff_staged ? " (staged changes)" : "");
2378 a->header_shown = 1;
2381 if (a->diff_staged) {
2382 const char *label1 = NULL, *label2 = NULL;
2383 switch (staged_status) {
2384 case GOT_STATUS_MODIFY:
2385 label1 = path;
2386 label2 = path;
2387 break;
2388 case GOT_STATUS_ADD:
2389 label2 = path;
2390 break;
2391 case GOT_STATUS_DELETE:
2392 label1 = path;
2393 break;
2394 default:
2395 return got_error(GOT_ERR_FILE_STATUS);
2397 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2398 label1, label2, a->diff_context, a->ignore_whitespace,
2399 a->repo, stdout);
2402 if (staged_status == GOT_STATUS_ADD ||
2403 staged_status == GOT_STATUS_MODIFY) {
2404 char *id_str;
2405 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2406 8192);
2407 if (err)
2408 goto done;
2409 err = got_object_id_str(&id_str, staged_blob_id);
2410 if (err)
2411 goto done;
2412 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2413 err = got_error_from_errno("asprintf");
2414 free(id_str);
2415 goto done;
2417 free(id_str);
2418 } else if (status != GOT_STATUS_ADD) {
2419 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2420 if (err)
2421 goto done;
2424 if (status != GOT_STATUS_DELETE) {
2425 if (asprintf(&abspath, "%s/%s",
2426 got_worktree_get_root_path(a->worktree), path) == -1) {
2427 err = got_error_from_errno("asprintf");
2428 goto done;
2431 if (dirfd != -1) {
2432 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2433 if (fd == -1) {
2434 err = got_error_from_errno2("openat", abspath);
2435 goto done;
2437 } else {
2438 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2439 if (fd == -1) {
2440 err = got_error_from_errno2("open", abspath);
2441 goto done;
2444 if (fstat(fd, &sb) == -1) {
2445 err = got_error_from_errno2("fstat", abspath);
2446 goto done;
2448 f2 = fdopen(fd, "r");
2449 if (f2 == NULL) {
2450 err = got_error_from_errno2("fdopen", abspath);
2451 goto done;
2453 fd = -1;
2454 } else
2455 sb.st_size = 0;
2457 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2458 a->diff_context, a->ignore_whitespace, stdout);
2459 done:
2460 if (blob1)
2461 got_object_blob_close(blob1);
2462 if (f2 && fclose(f2) == EOF && err == NULL)
2463 err = got_error_from_errno("fclose");
2464 if (fd != -1 && close(fd) == -1 && err == NULL)
2465 err = got_error_from_errno("close");
2466 free(abspath);
2467 return err;
2470 static const struct got_error *
2471 cmd_diff(int argc, char *argv[])
2473 const struct got_error *error;
2474 struct got_repository *repo = NULL;
2475 struct got_worktree *worktree = NULL;
2476 char *cwd = NULL, *repo_path = NULL;
2477 struct got_object_id *id1 = NULL, *id2 = NULL;
2478 const char *id_str1 = NULL, *id_str2 = NULL;
2479 char *label1 = NULL, *label2 = NULL;
2480 int type1, type2;
2481 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2482 const char *errstr;
2483 char *path = NULL;
2485 #ifndef PROFILE
2486 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2487 NULL) == -1)
2488 err(1, "pledge");
2489 #endif
2491 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2492 switch (ch) {
2493 case 'C':
2494 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2495 &errstr);
2496 if (errstr != NULL)
2497 err(1, "-C option %s", errstr);
2498 break;
2499 case 'r':
2500 repo_path = realpath(optarg, NULL);
2501 if (repo_path == NULL)
2502 return got_error_from_errno2("realpath",
2503 optarg);
2504 got_path_strip_trailing_slashes(repo_path);
2505 break;
2506 case 's':
2507 diff_staged = 1;
2508 break;
2509 case 'w':
2510 ignore_whitespace = 1;
2511 break;
2512 default:
2513 usage_diff();
2514 /* NOTREACHED */
2518 argc -= optind;
2519 argv += optind;
2521 cwd = getcwd(NULL, 0);
2522 if (cwd == NULL) {
2523 error = got_error_from_errno("getcwd");
2524 goto done;
2526 error = got_worktree_open(&worktree, cwd);
2527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2528 goto done;
2529 if (argc <= 1) {
2530 if (worktree == NULL) {
2531 error = got_error(GOT_ERR_NOT_WORKTREE);
2532 goto done;
2534 if (repo_path)
2535 errx(1,
2536 "-r option can't be used when diffing a work tree");
2537 repo_path = strdup(got_worktree_get_repo_path(worktree));
2538 if (repo_path == NULL) {
2539 error = got_error_from_errno("strdup");
2540 goto done;
2542 if (argc == 1) {
2543 error = got_worktree_resolve_path(&path, worktree,
2544 argv[0]);
2545 if (error)
2546 goto done;
2547 } else {
2548 path = strdup("");
2549 if (path == NULL) {
2550 error = got_error_from_errno("strdup");
2551 goto done;
2554 } else if (argc == 2) {
2555 if (diff_staged)
2556 errx(1, "-s option can't be used when diffing "
2557 "objects in repository");
2558 id_str1 = argv[0];
2559 id_str2 = argv[1];
2560 if (worktree && repo_path == NULL) {
2561 repo_path =
2562 strdup(got_worktree_get_repo_path(worktree));
2563 if (repo_path == NULL) {
2564 error = got_error_from_errno("strdup");
2565 goto done;
2568 } else
2569 usage_diff();
2571 if (repo_path == NULL) {
2572 repo_path = getcwd(NULL, 0);
2573 if (repo_path == NULL)
2574 return got_error_from_errno("getcwd");
2577 error = got_repo_open(&repo, repo_path, NULL);
2578 free(repo_path);
2579 if (error != NULL)
2580 goto done;
2582 error = apply_unveil(got_repo_get_path(repo), 1,
2583 worktree ? got_worktree_get_root_path(worktree) : NULL);
2584 if (error)
2585 goto done;
2587 if (argc <= 1) {
2588 struct print_diff_arg arg;
2589 struct got_pathlist_head paths;
2590 char *id_str;
2592 TAILQ_INIT(&paths);
2594 error = got_object_id_str(&id_str,
2595 got_worktree_get_base_commit_id(worktree));
2596 if (error)
2597 goto done;
2598 arg.repo = repo;
2599 arg.worktree = worktree;
2600 arg.diff_context = diff_context;
2601 arg.id_str = id_str;
2602 arg.header_shown = 0;
2603 arg.diff_staged = diff_staged;
2604 arg.ignore_whitespace = ignore_whitespace;
2606 error = got_pathlist_append(&paths, path, NULL);
2607 if (error)
2608 goto done;
2610 error = got_worktree_status(worktree, &paths, repo, print_diff,
2611 &arg, check_cancelled, NULL);
2612 free(id_str);
2613 got_pathlist_free(&paths);
2614 goto done;
2617 error = got_repo_match_object_id(&id1, &label1, id_str1,
2618 GOT_OBJ_TYPE_ANY, 1, repo);
2619 if (error)
2620 goto done;
2622 error = got_repo_match_object_id(&id2, &label2, id_str2,
2623 GOT_OBJ_TYPE_ANY, 1, repo);
2624 if (error)
2625 goto done;
2627 error = got_object_get_type(&type1, repo, id1);
2628 if (error)
2629 goto done;
2631 error = got_object_get_type(&type2, repo, id2);
2632 if (error)
2633 goto done;
2635 if (type1 != type2) {
2636 error = got_error(GOT_ERR_OBJ_TYPE);
2637 goto done;
2640 switch (type1) {
2641 case GOT_OBJ_TYPE_BLOB:
2642 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2643 diff_context, ignore_whitespace, repo, stdout);
2644 break;
2645 case GOT_OBJ_TYPE_TREE:
2646 error = got_diff_objects_as_trees(id1, id2, "", "",
2647 diff_context, ignore_whitespace, repo, stdout);
2648 break;
2649 case GOT_OBJ_TYPE_COMMIT:
2650 printf("diff %s %s\n", label1, label2);
2651 error = got_diff_objects_as_commits(id1, id2, diff_context,
2652 ignore_whitespace, repo, stdout);
2653 break;
2654 default:
2655 error = got_error(GOT_ERR_OBJ_TYPE);
2657 done:
2658 free(label1);
2659 free(label2);
2660 free(id1);
2661 free(id2);
2662 free(path);
2663 if (worktree)
2664 got_worktree_close(worktree);
2665 if (repo) {
2666 const struct got_error *repo_error;
2667 repo_error = got_repo_close(repo);
2668 if (error == NULL)
2669 error = repo_error;
2671 return error;
2674 __dead static void
2675 usage_blame(void)
2677 fprintf(stderr,
2678 "usage: %s blame [-c commit] [-r repository-path] path\n",
2679 getprogname());
2680 exit(1);
2683 struct blame_line {
2684 int annotated;
2685 char *id_str;
2686 char *committer;
2687 char datebuf[11]; /* YYYY-MM-DD + NUL */
2690 struct blame_cb_args {
2691 struct blame_line *lines;
2692 int nlines;
2693 int nlines_prec;
2694 int lineno_cur;
2695 off_t *line_offsets;
2696 FILE *f;
2697 struct got_repository *repo;
2700 static const struct got_error *
2701 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2703 const struct got_error *err = NULL;
2704 struct blame_cb_args *a = arg;
2705 struct blame_line *bline;
2706 char *line = NULL;
2707 size_t linesize = 0;
2708 struct got_commit_object *commit = NULL;
2709 off_t offset;
2710 struct tm tm;
2711 time_t committer_time;
2713 if (nlines != a->nlines ||
2714 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2715 return got_error(GOT_ERR_RANGE);
2717 if (sigint_received)
2718 return got_error(GOT_ERR_ITER_COMPLETED);
2720 if (lineno == -1)
2721 return NULL; /* no change in this commit */
2723 /* Annotate this line. */
2724 bline = &a->lines[lineno - 1];
2725 if (bline->annotated)
2726 return NULL;
2727 err = got_object_id_str(&bline->id_str, id);
2728 if (err)
2729 return err;
2731 err = got_object_open_as_commit(&commit, a->repo, id);
2732 if (err)
2733 goto done;
2735 bline->committer = strdup(got_object_commit_get_committer(commit));
2736 if (bline->committer == NULL) {
2737 err = got_error_from_errno("strdup");
2738 goto done;
2741 committer_time = got_object_commit_get_committer_time(commit);
2742 if (localtime_r(&committer_time, &tm) == NULL)
2743 return got_error_from_errno("localtime_r");
2744 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2745 &tm) >= sizeof(bline->datebuf)) {
2746 err = got_error(GOT_ERR_NO_SPACE);
2747 goto done;
2749 bline->annotated = 1;
2751 /* Print lines annotated so far. */
2752 bline = &a->lines[a->lineno_cur - 1];
2753 if (!bline->annotated)
2754 goto done;
2756 offset = a->line_offsets[a->lineno_cur - 1];
2757 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2758 err = got_error_from_errno("fseeko");
2759 goto done;
2762 while (bline->annotated) {
2763 char *smallerthan, *at, *nl, *committer;
2764 size_t len;
2766 if (getline(&line, &linesize, a->f) == -1) {
2767 if (ferror(a->f))
2768 err = got_error_from_errno("getline");
2769 break;
2772 committer = bline->committer;
2773 smallerthan = strchr(committer, '<');
2774 if (smallerthan && smallerthan[1] != '\0')
2775 committer = smallerthan + 1;
2776 at = strchr(committer, '@');
2777 if (at)
2778 *at = '\0';
2779 len = strlen(committer);
2780 if (len >= 9)
2781 committer[8] = '\0';
2783 nl = strchr(line, '\n');
2784 if (nl)
2785 *nl = '\0';
2786 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2787 bline->id_str, bline->datebuf, committer, line);
2789 a->lineno_cur++;
2790 bline = &a->lines[a->lineno_cur - 1];
2792 done:
2793 if (commit)
2794 got_object_commit_close(commit);
2795 free(line);
2796 return err;
2799 static const struct got_error *
2800 cmd_blame(int argc, char *argv[])
2802 const struct got_error *error;
2803 struct got_repository *repo = NULL;
2804 struct got_worktree *worktree = NULL;
2805 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2806 struct got_object_id *obj_id = NULL;
2807 struct got_object_id *commit_id = NULL;
2808 struct got_blob_object *blob = NULL;
2809 char *commit_id_str = NULL;
2810 struct blame_cb_args bca;
2811 int ch, obj_type, i;
2812 size_t filesize;
2814 memset(&bca, 0, sizeof(bca));
2816 #ifndef PROFILE
2817 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2818 NULL) == -1)
2819 err(1, "pledge");
2820 #endif
2822 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2823 switch (ch) {
2824 case 'c':
2825 commit_id_str = optarg;
2826 break;
2827 case 'r':
2828 repo_path = realpath(optarg, NULL);
2829 if (repo_path == NULL)
2830 return got_error_from_errno2("realpath",
2831 optarg);
2832 got_path_strip_trailing_slashes(repo_path);
2833 break;
2834 default:
2835 usage_blame();
2836 /* NOTREACHED */
2840 argc -= optind;
2841 argv += optind;
2843 if (argc == 1)
2844 path = argv[0];
2845 else
2846 usage_blame();
2848 cwd = getcwd(NULL, 0);
2849 if (cwd == NULL) {
2850 error = got_error_from_errno("getcwd");
2851 goto done;
2853 if (repo_path == NULL) {
2854 error = got_worktree_open(&worktree, cwd);
2855 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2856 goto done;
2857 else
2858 error = NULL;
2859 if (worktree) {
2860 repo_path =
2861 strdup(got_worktree_get_repo_path(worktree));
2862 if (repo_path == NULL)
2863 error = got_error_from_errno("strdup");
2864 if (error)
2865 goto done;
2866 } else {
2867 repo_path = strdup(cwd);
2868 if (repo_path == NULL) {
2869 error = got_error_from_errno("strdup");
2870 goto done;
2875 error = got_repo_open(&repo, repo_path, NULL);
2876 if (error != NULL)
2877 goto done;
2879 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2880 if (error)
2881 goto done;
2883 if (worktree) {
2884 const char *prefix = got_worktree_get_path_prefix(worktree);
2885 char *p, *worktree_subdir = cwd +
2886 strlen(got_worktree_get_root_path(worktree));
2887 if (asprintf(&p, "%s%s%s%s%s",
2888 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2889 worktree_subdir, worktree_subdir[0] ? "/" : "",
2890 path) == -1) {
2891 error = got_error_from_errno("asprintf");
2892 goto done;
2894 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2895 free(p);
2896 } else {
2897 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2899 if (error)
2900 goto done;
2902 if (commit_id_str == NULL) {
2903 struct got_reference *head_ref;
2904 error = got_ref_open(&head_ref, repo, worktree ?
2905 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2906 if (error != NULL)
2907 goto done;
2908 error = got_ref_resolve(&commit_id, repo, head_ref);
2909 got_ref_close(head_ref);
2910 if (error != NULL)
2911 goto done;
2912 } else {
2913 error = got_repo_match_object_id(&commit_id, NULL,
2914 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2915 if (error)
2916 goto done;
2919 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2920 if (error)
2921 goto done;
2923 error = got_object_get_type(&obj_type, repo, obj_id);
2924 if (error)
2925 goto done;
2927 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2928 error = got_error(GOT_ERR_OBJ_TYPE);
2929 goto done;
2932 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2933 if (error)
2934 goto done;
2935 bca.f = got_opentemp();
2936 if (bca.f == NULL) {
2937 error = got_error_from_errno("got_opentemp");
2938 goto done;
2940 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2941 &bca.line_offsets, bca.f, blob);
2942 if (error || bca.nlines == 0)
2943 goto done;
2945 /* Don't include \n at EOF in the blame line count. */
2946 if (bca.line_offsets[bca.nlines - 1] == filesize)
2947 bca.nlines--;
2949 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2950 if (bca.lines == NULL) {
2951 error = got_error_from_errno("calloc");
2952 goto done;
2954 bca.lineno_cur = 1;
2955 bca.nlines_prec = 0;
2956 i = bca.nlines;
2957 while (i > 0) {
2958 i /= 10;
2959 bca.nlines_prec++;
2961 bca.repo = repo;
2963 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2964 check_cancelled, NULL);
2965 done:
2966 free(in_repo_path);
2967 free(repo_path);
2968 free(cwd);
2969 free(commit_id);
2970 free(obj_id);
2971 if (blob)
2972 got_object_blob_close(blob);
2973 if (worktree)
2974 got_worktree_close(worktree);
2975 if (repo) {
2976 const struct got_error *repo_error;
2977 repo_error = got_repo_close(repo);
2978 if (error == NULL)
2979 error = repo_error;
2981 if (bca.lines) {
2982 for (i = 0; i < bca.nlines; i++) {
2983 struct blame_line *bline = &bca.lines[i];
2984 free(bline->id_str);
2985 free(bline->committer);
2987 free(bca.lines);
2989 free(bca.line_offsets);
2990 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2991 error = got_error_from_errno("fclose");
2992 return error;
2995 __dead static void
2996 usage_tree(void)
2998 fprintf(stderr,
2999 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3000 getprogname());
3001 exit(1);
3004 static void
3005 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3006 const char *root_path)
3008 int is_root_path = (strcmp(path, root_path) == 0);
3009 const char *modestr = "";
3010 mode_t mode = got_tree_entry_get_mode(te);
3012 path += strlen(root_path);
3013 while (path[0] == '/')
3014 path++;
3016 if (got_object_tree_entry_is_submodule(te))
3017 modestr = "$";
3018 else if (S_ISLNK(mode))
3019 modestr = "@";
3020 else if (S_ISDIR(mode))
3021 modestr = "/";
3022 else if (mode & S_IXUSR)
3023 modestr = "*";
3025 printf("%s%s%s%s%s\n", id ? id : "", path,
3026 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3029 static const struct got_error *
3030 print_tree(const char *path, struct got_object_id *commit_id,
3031 int show_ids, int recurse, const char *root_path,
3032 struct got_repository *repo)
3034 const struct got_error *err = NULL;
3035 struct got_object_id *tree_id = NULL;
3036 struct got_tree_object *tree = NULL;
3037 int nentries, i;
3039 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3040 if (err)
3041 goto done;
3043 err = got_object_open_as_tree(&tree, repo, tree_id);
3044 if (err)
3045 goto done;
3046 nentries = got_object_tree_get_nentries(tree);
3047 for (i = 0; i < nentries; i++) {
3048 struct got_tree_entry *te;
3049 char *id = NULL;
3051 if (sigint_received || sigpipe_received)
3052 break;
3054 te = got_object_tree_get_entry(tree, i);
3055 if (show_ids) {
3056 char *id_str;
3057 err = got_object_id_str(&id_str,
3058 got_tree_entry_get_id(te));
3059 if (err)
3060 goto done;
3061 if (asprintf(&id, "%s ", id_str) == -1) {
3062 err = got_error_from_errno("asprintf");
3063 free(id_str);
3064 goto done;
3066 free(id_str);
3068 print_entry(te, id, path, root_path);
3069 free(id);
3071 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3072 char *child_path;
3073 if (asprintf(&child_path, "%s%s%s", path,
3074 path[0] == '/' && path[1] == '\0' ? "" : "/",
3075 got_tree_entry_get_name(te)) == -1) {
3076 err = got_error_from_errno("asprintf");
3077 goto done;
3079 err = print_tree(child_path, commit_id, show_ids, 1,
3080 root_path, repo);
3081 free(child_path);
3082 if (err)
3083 goto done;
3086 done:
3087 if (tree)
3088 got_object_tree_close(tree);
3089 free(tree_id);
3090 return err;
3093 static const struct got_error *
3094 cmd_tree(int argc, char *argv[])
3096 const struct got_error *error;
3097 struct got_repository *repo = NULL;
3098 struct got_worktree *worktree = NULL;
3099 const char *path;
3100 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3101 struct got_object_id *commit_id = NULL;
3102 char *commit_id_str = NULL;
3103 int show_ids = 0, recurse = 0;
3104 int ch;
3106 #ifndef PROFILE
3107 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3108 NULL) == -1)
3109 err(1, "pledge");
3110 #endif
3112 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3113 switch (ch) {
3114 case 'c':
3115 commit_id_str = optarg;
3116 break;
3117 case 'r':
3118 repo_path = realpath(optarg, NULL);
3119 if (repo_path == NULL)
3120 return got_error_from_errno2("realpath",
3121 optarg);
3122 got_path_strip_trailing_slashes(repo_path);
3123 break;
3124 case 'i':
3125 show_ids = 1;
3126 break;
3127 case 'R':
3128 recurse = 1;
3129 break;
3130 default:
3131 usage_tree();
3132 /* NOTREACHED */
3136 argc -= optind;
3137 argv += optind;
3139 if (argc == 1)
3140 path = argv[0];
3141 else if (argc > 1)
3142 usage_tree();
3143 else
3144 path = NULL;
3146 cwd = getcwd(NULL, 0);
3147 if (cwd == NULL) {
3148 error = got_error_from_errno("getcwd");
3149 goto done;
3151 if (repo_path == NULL) {
3152 error = got_worktree_open(&worktree, cwd);
3153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3154 goto done;
3155 else
3156 error = NULL;
3157 if (worktree) {
3158 repo_path =
3159 strdup(got_worktree_get_repo_path(worktree));
3160 if (repo_path == NULL)
3161 error = got_error_from_errno("strdup");
3162 if (error)
3163 goto done;
3164 } else {
3165 repo_path = strdup(cwd);
3166 if (repo_path == NULL) {
3167 error = got_error_from_errno("strdup");
3168 goto done;
3173 error = got_repo_open(&repo, repo_path, NULL);
3174 if (error != NULL)
3175 goto done;
3177 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3178 if (error)
3179 goto done;
3181 if (path == NULL) {
3182 if (worktree) {
3183 char *p, *worktree_subdir = cwd +
3184 strlen(got_worktree_get_root_path(worktree));
3185 if (asprintf(&p, "%s/%s",
3186 got_worktree_get_path_prefix(worktree),
3187 worktree_subdir) == -1) {
3188 error = got_error_from_errno("asprintf");
3189 goto done;
3191 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3192 free(p);
3193 if (error)
3194 goto done;
3195 } else
3196 path = "/";
3198 if (in_repo_path == NULL) {
3199 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3200 if (error != NULL)
3201 goto done;
3204 if (commit_id_str == NULL) {
3205 struct got_reference *head_ref;
3206 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3207 if (error != NULL)
3208 goto done;
3209 error = got_ref_resolve(&commit_id, repo, head_ref);
3210 got_ref_close(head_ref);
3211 if (error != NULL)
3212 goto done;
3213 } else {
3214 error = got_repo_match_object_id(&commit_id, NULL,
3215 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3216 if (error)
3217 goto done;
3220 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3221 in_repo_path, repo);
3222 done:
3223 free(in_repo_path);
3224 free(repo_path);
3225 free(cwd);
3226 free(commit_id);
3227 if (worktree)
3228 got_worktree_close(worktree);
3229 if (repo) {
3230 const struct got_error *repo_error;
3231 repo_error = got_repo_close(repo);
3232 if (error == NULL)
3233 error = repo_error;
3235 return error;
3238 __dead static void
3239 usage_status(void)
3241 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3242 exit(1);
3245 static const struct got_error *
3246 print_status(void *arg, unsigned char status, unsigned char staged_status,
3247 const char *path, struct got_object_id *blob_id,
3248 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3249 int dirfd, const char *de_name)
3251 if (status == staged_status && (status == GOT_STATUS_DELETE))
3252 status = GOT_STATUS_NO_CHANGE;
3253 printf("%c%c %s\n", status, staged_status, path);
3254 return NULL;
3257 static const struct got_error *
3258 cmd_status(int argc, char *argv[])
3260 const struct got_error *error = NULL;
3261 struct got_repository *repo = NULL;
3262 struct got_worktree *worktree = NULL;
3263 char *cwd = NULL;
3264 struct got_pathlist_head paths;
3265 struct got_pathlist_entry *pe;
3266 int ch;
3268 TAILQ_INIT(&paths);
3270 while ((ch = getopt(argc, argv, "")) != -1) {
3271 switch (ch) {
3272 default:
3273 usage_status();
3274 /* NOTREACHED */
3278 argc -= optind;
3279 argv += optind;
3281 #ifndef PROFILE
3282 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3283 NULL) == -1)
3284 err(1, "pledge");
3285 #endif
3286 cwd = getcwd(NULL, 0);
3287 if (cwd == NULL) {
3288 error = got_error_from_errno("getcwd");
3289 goto done;
3292 error = got_worktree_open(&worktree, cwd);
3293 if (error != NULL)
3294 goto done;
3296 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3297 NULL);
3298 if (error != NULL)
3299 goto done;
3301 error = apply_unveil(got_repo_get_path(repo), 1,
3302 got_worktree_get_root_path(worktree));
3303 if (error)
3304 goto done;
3306 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3307 if (error)
3308 goto done;
3310 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3311 check_cancelled, NULL);
3312 done:
3313 TAILQ_FOREACH(pe, &paths, entry)
3314 free((char *)pe->path);
3315 got_pathlist_free(&paths);
3316 free(cwd);
3317 return error;
3320 __dead static void
3321 usage_ref(void)
3323 fprintf(stderr,
3324 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3325 getprogname());
3326 exit(1);
3329 static const struct got_error *
3330 list_refs(struct got_repository *repo)
3332 static const struct got_error *err = NULL;
3333 struct got_reflist_head refs;
3334 struct got_reflist_entry *re;
3336 SIMPLEQ_INIT(&refs);
3337 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3338 if (err)
3339 return err;
3341 SIMPLEQ_FOREACH(re, &refs, entry) {
3342 char *refstr;
3343 refstr = got_ref_to_str(re->ref);
3344 if (refstr == NULL)
3345 return got_error_from_errno("got_ref_to_str");
3346 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3347 free(refstr);
3350 got_ref_list_free(&refs);
3351 return NULL;
3354 static const struct got_error *
3355 delete_ref(struct got_repository *repo, const char *refname)
3357 const struct got_error *err = NULL;
3358 struct got_reference *ref;
3360 err = got_ref_open(&ref, repo, refname, 0);
3361 if (err)
3362 return err;
3364 err = got_ref_delete(ref, repo);
3365 got_ref_close(ref);
3366 return err;
3369 static const struct got_error *
3370 add_ref(struct got_repository *repo, const char *refname, const char *target)
3372 const struct got_error *err = NULL;
3373 struct got_object_id *id;
3374 struct got_reference *ref = NULL;
3377 * Don't let the user create a reference name with a leading '-'.
3378 * While technically a valid reference name, this case is usually
3379 * an unintended typo.
3381 if (refname[0] == '-')
3382 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3384 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3385 repo);
3386 if (err) {
3387 struct got_reference *target_ref;
3389 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3390 return err;
3391 err = got_ref_open(&target_ref, repo, target, 0);
3392 if (err)
3393 return err;
3394 err = got_ref_resolve(&id, repo, target_ref);
3395 got_ref_close(target_ref);
3396 if (err)
3397 return err;
3400 err = got_ref_alloc(&ref, refname, id);
3401 if (err)
3402 goto done;
3404 err = got_ref_write(ref, repo);
3405 done:
3406 if (ref)
3407 got_ref_close(ref);
3408 free(id);
3409 return err;
3412 static const struct got_error *
3413 add_symref(struct got_repository *repo, const char *refname, const char *target)
3415 const struct got_error *err = NULL;
3416 struct got_reference *ref = NULL;
3417 struct got_reference *target_ref = NULL;
3420 * Don't let the user create a reference name with a leading '-'.
3421 * While technically a valid reference name, this case is usually
3422 * an unintended typo.
3424 if (refname[0] == '-')
3425 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3427 err = got_ref_open(&target_ref, repo, target, 0);
3428 if (err)
3429 return err;
3431 err = got_ref_alloc_symref(&ref, refname, target_ref);
3432 if (err)
3433 goto done;
3435 err = got_ref_write(ref, repo);
3436 done:
3437 if (target_ref)
3438 got_ref_close(target_ref);
3439 if (ref)
3440 got_ref_close(ref);
3441 return err;
3444 static const struct got_error *
3445 cmd_ref(int argc, char *argv[])
3447 const struct got_error *error = NULL;
3448 struct got_repository *repo = NULL;
3449 struct got_worktree *worktree = NULL;
3450 char *cwd = NULL, *repo_path = NULL;
3451 int ch, do_list = 0, create_symref = 0;
3452 const char *delref = NULL;
3454 /* TODO: Add -s option for adding symbolic references. */
3455 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3456 switch (ch) {
3457 case 'd':
3458 delref = optarg;
3459 break;
3460 case 'r':
3461 repo_path = realpath(optarg, NULL);
3462 if (repo_path == NULL)
3463 return got_error_from_errno2("realpath",
3464 optarg);
3465 got_path_strip_trailing_slashes(repo_path);
3466 break;
3467 case 'l':
3468 do_list = 1;
3469 break;
3470 case 's':
3471 create_symref = 1;
3472 break;
3473 default:
3474 usage_ref();
3475 /* NOTREACHED */
3479 if (do_list && delref)
3480 errx(1, "-l and -d options are mutually exclusive\n");
3482 argc -= optind;
3483 argv += optind;
3485 if (do_list || delref) {
3486 if (create_symref)
3487 errx(1, "-s option cannot be used together with the "
3488 "-l or -d options");
3489 if (argc > 0)
3490 usage_ref();
3491 } else if (argc != 2)
3492 usage_ref();
3494 #ifndef PROFILE
3495 if (do_list) {
3496 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3497 NULL) == -1)
3498 err(1, "pledge");
3499 } else {
3500 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3501 "sendfd unveil", NULL) == -1)
3502 err(1, "pledge");
3504 #endif
3505 cwd = getcwd(NULL, 0);
3506 if (cwd == NULL) {
3507 error = got_error_from_errno("getcwd");
3508 goto done;
3511 if (repo_path == NULL) {
3512 error = got_worktree_open(&worktree, cwd);
3513 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3514 goto done;
3515 else
3516 error = NULL;
3517 if (worktree) {
3518 repo_path =
3519 strdup(got_worktree_get_repo_path(worktree));
3520 if (repo_path == NULL)
3521 error = got_error_from_errno("strdup");
3522 if (error)
3523 goto done;
3524 } else {
3525 repo_path = strdup(cwd);
3526 if (repo_path == NULL) {
3527 error = got_error_from_errno("strdup");
3528 goto done;
3533 error = got_repo_open(&repo, repo_path, NULL);
3534 if (error != NULL)
3535 goto done;
3537 error = apply_unveil(got_repo_get_path(repo), do_list,
3538 worktree ? got_worktree_get_root_path(worktree) : NULL);
3539 if (error)
3540 goto done;
3542 if (do_list)
3543 error = list_refs(repo);
3544 else if (delref)
3545 error = delete_ref(repo, delref);
3546 else if (create_symref)
3547 error = add_symref(repo, argv[0], argv[1]);
3548 else
3549 error = add_ref(repo, argv[0], argv[1]);
3550 done:
3551 if (repo)
3552 got_repo_close(repo);
3553 if (worktree)
3554 got_worktree_close(worktree);
3555 free(cwd);
3556 free(repo_path);
3557 return error;
3560 __dead static void
3561 usage_branch(void)
3563 fprintf(stderr,
3564 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3565 "[name]\n", getprogname());
3566 exit(1);
3569 static const struct got_error *
3570 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3571 struct got_reference *ref)
3573 const struct got_error *err = NULL;
3574 const char *refname, *marker = " ";
3575 char *refstr;
3577 refname = got_ref_get_name(ref);
3578 if (worktree && strcmp(refname,
3579 got_worktree_get_head_ref_name(worktree)) == 0) {
3580 struct got_object_id *id = NULL;
3582 err = got_ref_resolve(&id, repo, ref);
3583 if (err)
3584 return err;
3585 if (got_object_id_cmp(id,
3586 got_worktree_get_base_commit_id(worktree)) == 0)
3587 marker = "* ";
3588 else
3589 marker = "~ ";
3590 free(id);
3593 if (strncmp(refname, "refs/heads/", 11) == 0)
3594 refname += 11;
3595 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3596 refname += 18;
3598 refstr = got_ref_to_str(ref);
3599 if (refstr == NULL)
3600 return got_error_from_errno("got_ref_to_str");
3602 printf("%s%s: %s\n", marker, refname, refstr);
3603 free(refstr);
3604 return NULL;
3607 static const struct got_error *
3608 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3610 const char *refname;
3612 if (worktree == NULL)
3613 return got_error(GOT_ERR_NOT_WORKTREE);
3615 refname = got_worktree_get_head_ref_name(worktree);
3617 if (strncmp(refname, "refs/heads/", 11) == 0)
3618 refname += 11;
3619 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3620 refname += 18;
3622 printf("%s\n", refname);
3624 return NULL;
3627 static const struct got_error *
3628 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3630 static const struct got_error *err = NULL;
3631 struct got_reflist_head refs;
3632 struct got_reflist_entry *re;
3633 struct got_reference *temp_ref = NULL;
3634 int rebase_in_progress, histedit_in_progress;
3636 SIMPLEQ_INIT(&refs);
3638 if (worktree) {
3639 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3640 worktree);
3641 if (err)
3642 return err;
3644 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3645 worktree);
3646 if (err)
3647 return err;
3649 if (rebase_in_progress || histedit_in_progress) {
3650 err = got_ref_open(&temp_ref, repo,
3651 got_worktree_get_head_ref_name(worktree), 0);
3652 if (err)
3653 return err;
3654 list_branch(repo, worktree, temp_ref);
3655 got_ref_close(temp_ref);
3659 err = got_ref_list(&refs, repo, "refs/heads",
3660 got_ref_cmp_by_name, NULL);
3661 if (err)
3662 return err;
3664 SIMPLEQ_FOREACH(re, &refs, entry)
3665 list_branch(repo, worktree, re->ref);
3667 got_ref_list_free(&refs);
3668 return NULL;
3671 static const struct got_error *
3672 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3673 const char *branch_name)
3675 const struct got_error *err = NULL;
3676 struct got_reference *ref = NULL;
3677 char *refname;
3679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3680 return got_error_from_errno("asprintf");
3682 err = got_ref_open(&ref, repo, refname, 0);
3683 if (err)
3684 goto done;
3686 if (worktree &&
3687 strcmp(got_worktree_get_head_ref_name(worktree),
3688 got_ref_get_name(ref)) == 0) {
3689 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3690 "will not delete this work tree's current branch");
3691 goto done;
3694 err = got_ref_delete(ref, repo);
3695 done:
3696 if (ref)
3697 got_ref_close(ref);
3698 free(refname);
3699 return err;
3702 static const struct got_error *
3703 add_branch(struct got_repository *repo, const char *branch_name,
3704 struct got_object_id *base_commit_id)
3706 const struct got_error *err = NULL;
3707 struct got_reference *ref = NULL;
3708 char *base_refname = NULL, *refname = NULL;
3711 * Don't let the user create a branch name with a leading '-'.
3712 * While technically a valid reference name, this case is usually
3713 * an unintended typo.
3715 if (branch_name[0] == '-')
3716 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3718 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3719 err = got_error_from_errno("asprintf");
3720 goto done;
3723 err = got_ref_open(&ref, repo, refname, 0);
3724 if (err == NULL) {
3725 err = got_error(GOT_ERR_BRANCH_EXISTS);
3726 goto done;
3727 } else if (err->code != GOT_ERR_NOT_REF)
3728 goto done;
3730 err = got_ref_alloc(&ref, refname, base_commit_id);
3731 if (err)
3732 goto done;
3734 err = got_ref_write(ref, repo);
3735 done:
3736 if (ref)
3737 got_ref_close(ref);
3738 free(base_refname);
3739 free(refname);
3740 return err;
3743 static const struct got_error *
3744 cmd_branch(int argc, char *argv[])
3746 const struct got_error *error = NULL;
3747 struct got_repository *repo = NULL;
3748 struct got_worktree *worktree = NULL;
3749 char *cwd = NULL, *repo_path = NULL;
3750 int ch, do_list = 0, do_show = 0, do_update = 1;
3751 const char *delref = NULL, *commit_id_arg = NULL;
3752 struct got_reference *ref = NULL;
3753 struct got_pathlist_head paths;
3754 struct got_pathlist_entry *pe;
3755 struct got_object_id *commit_id = NULL;
3756 char *commit_id_str = NULL;
3758 TAILQ_INIT(&paths);
3760 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3761 switch (ch) {
3762 case 'c':
3763 commit_id_arg = optarg;
3764 break;
3765 case 'd':
3766 delref = optarg;
3767 break;
3768 case 'r':
3769 repo_path = realpath(optarg, NULL);
3770 if (repo_path == NULL)
3771 return got_error_from_errno2("realpath",
3772 optarg);
3773 got_path_strip_trailing_slashes(repo_path);
3774 break;
3775 case 'l':
3776 do_list = 1;
3777 break;
3778 case 'n':
3779 do_update = 0;
3780 break;
3781 default:
3782 usage_branch();
3783 /* NOTREACHED */
3787 if (do_list && delref)
3788 errx(1, "-l and -d options are mutually exclusive\n");
3790 argc -= optind;
3791 argv += optind;
3793 if (!do_list && !delref && argc == 0)
3794 do_show = 1;
3796 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3797 errx(1, "-c option can only be used when creating a branch");
3799 if (do_list || delref) {
3800 if (argc > 0)
3801 usage_branch();
3802 } else if (!do_show && argc != 1)
3803 usage_branch();
3805 #ifndef PROFILE
3806 if (do_list || do_show) {
3807 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3808 NULL) == -1)
3809 err(1, "pledge");
3810 } else {
3811 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3812 "sendfd unveil", NULL) == -1)
3813 err(1, "pledge");
3815 #endif
3816 cwd = getcwd(NULL, 0);
3817 if (cwd == NULL) {
3818 error = got_error_from_errno("getcwd");
3819 goto done;
3822 if (repo_path == NULL) {
3823 error = got_worktree_open(&worktree, cwd);
3824 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3825 goto done;
3826 else
3827 error = NULL;
3828 if (worktree) {
3829 repo_path =
3830 strdup(got_worktree_get_repo_path(worktree));
3831 if (repo_path == NULL)
3832 error = got_error_from_errno("strdup");
3833 if (error)
3834 goto done;
3835 } else {
3836 repo_path = strdup(cwd);
3837 if (repo_path == NULL) {
3838 error = got_error_from_errno("strdup");
3839 goto done;
3844 error = got_repo_open(&repo, repo_path, NULL);
3845 if (error != NULL)
3846 goto done;
3848 error = apply_unveil(got_repo_get_path(repo), do_list,
3849 worktree ? got_worktree_get_root_path(worktree) : NULL);
3850 if (error)
3851 goto done;
3853 if (do_show)
3854 error = show_current_branch(repo, worktree);
3855 else if (do_list)
3856 error = list_branches(repo, worktree);
3857 else if (delref)
3858 error = delete_branch(repo, worktree, delref);
3859 else {
3860 if (commit_id_arg == NULL)
3861 commit_id_arg = worktree ?
3862 got_worktree_get_head_ref_name(worktree) :
3863 GOT_REF_HEAD;
3864 error = got_repo_match_object_id(&commit_id, NULL,
3865 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3866 if (error)
3867 goto done;
3868 error = add_branch(repo, argv[0], commit_id);
3869 if (error)
3870 goto done;
3871 if (worktree && do_update) {
3872 int did_something = 0;
3873 char *branch_refname = NULL;
3875 error = got_object_id_str(&commit_id_str, commit_id);
3876 if (error)
3877 goto done;
3878 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3879 worktree);
3880 if (error)
3881 goto done;
3882 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3883 == -1) {
3884 error = got_error_from_errno("asprintf");
3885 goto done;
3887 error = got_ref_open(&ref, repo, branch_refname, 0);
3888 free(branch_refname);
3889 if (error)
3890 goto done;
3891 error = switch_head_ref(ref, commit_id, worktree,
3892 repo);
3893 if (error)
3894 goto done;
3895 error = got_worktree_set_base_commit_id(worktree, repo,
3896 commit_id);
3897 if (error)
3898 goto done;
3899 error = got_worktree_checkout_files(worktree, &paths,
3900 repo, update_progress, &did_something,
3901 check_cancelled, NULL);
3902 if (error)
3903 goto done;
3904 if (did_something)
3905 printf("Updated to commit %s\n", commit_id_str);
3908 done:
3909 if (ref)
3910 got_ref_close(ref);
3911 if (repo)
3912 got_repo_close(repo);
3913 if (worktree)
3914 got_worktree_close(worktree);
3915 free(cwd);
3916 free(repo_path);
3917 free(commit_id);
3918 free(commit_id_str);
3919 TAILQ_FOREACH(pe, &paths, entry)
3920 free((char *)pe->path);
3921 got_pathlist_free(&paths);
3922 return error;
3926 __dead static void
3927 usage_tag(void)
3929 fprintf(stderr,
3930 "usage: %s tag [-c commit] [-r repository] [-l] "
3931 "[-m message] name\n", getprogname());
3932 exit(1);
3935 #if 0
3936 static const struct got_error *
3937 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3939 const struct got_error *err = NULL;
3940 struct got_reflist_entry *re, *se, *new;
3941 struct got_object_id *re_id, *se_id;
3942 struct got_tag_object *re_tag, *se_tag;
3943 time_t re_time, se_time;
3945 SIMPLEQ_FOREACH(re, tags, entry) {
3946 se = SIMPLEQ_FIRST(sorted);
3947 if (se == NULL) {
3948 err = got_reflist_entry_dup(&new, re);
3949 if (err)
3950 return err;
3951 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3952 continue;
3953 } else {
3954 err = got_ref_resolve(&re_id, repo, re->ref);
3955 if (err)
3956 break;
3957 err = got_object_open_as_tag(&re_tag, repo, re_id);
3958 free(re_id);
3959 if (err)
3960 break;
3961 re_time = got_object_tag_get_tagger_time(re_tag);
3962 got_object_tag_close(re_tag);
3965 while (se) {
3966 err = got_ref_resolve(&se_id, repo, re->ref);
3967 if (err)
3968 break;
3969 err = got_object_open_as_tag(&se_tag, repo, se_id);
3970 free(se_id);
3971 if (err)
3972 break;
3973 se_time = got_object_tag_get_tagger_time(se_tag);
3974 got_object_tag_close(se_tag);
3976 if (se_time > re_time) {
3977 err = got_reflist_entry_dup(&new, re);
3978 if (err)
3979 return err;
3980 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3981 break;
3983 se = SIMPLEQ_NEXT(se, entry);
3984 continue;
3987 done:
3988 return err;
3990 #endif
3992 static const struct got_error *
3993 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3995 static const struct got_error *err = NULL;
3996 struct got_reflist_head refs;
3997 struct got_reflist_entry *re;
3999 SIMPLEQ_INIT(&refs);
4001 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4002 if (err)
4003 return err;
4005 SIMPLEQ_FOREACH(re, &refs, entry) {
4006 const char *refname;
4007 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4008 char datebuf[26];
4009 const char *tagger;
4010 time_t tagger_time;
4011 struct got_object_id *id;
4012 struct got_tag_object *tag;
4013 struct got_commit_object *commit = NULL;
4015 refname = got_ref_get_name(re->ref);
4016 if (strncmp(refname, "refs/tags/", 10) != 0)
4017 continue;
4018 refname += 10;
4019 refstr = got_ref_to_str(re->ref);
4020 if (refstr == NULL) {
4021 err = got_error_from_errno("got_ref_to_str");
4022 break;
4024 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4025 free(refstr);
4027 err = got_ref_resolve(&id, repo, re->ref);
4028 if (err)
4029 break;
4030 err = got_object_open_as_tag(&tag, repo, id);
4031 if (err) {
4032 if (err->code != GOT_ERR_OBJ_TYPE) {
4033 free(id);
4034 break;
4036 /* "lightweight" tag */
4037 err = got_object_open_as_commit(&commit, repo, id);
4038 if (err) {
4039 free(id);
4040 break;
4042 tagger = got_object_commit_get_committer(commit);
4043 tagger_time =
4044 got_object_commit_get_committer_time(commit);
4045 err = got_object_id_str(&id_str, id);
4046 free(id);
4047 if (err)
4048 break;
4049 } else {
4050 free(id);
4051 tagger = got_object_tag_get_tagger(tag);
4052 tagger_time = got_object_tag_get_tagger_time(tag);
4053 err = got_object_id_str(&id_str,
4054 got_object_tag_get_object_id(tag));
4055 if (err)
4056 break;
4058 printf("from: %s\n", tagger);
4059 datestr = get_datestr(&tagger_time, datebuf);
4060 if (datestr)
4061 printf("date: %s UTC\n", datestr);
4062 if (commit)
4063 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4064 else {
4065 switch (got_object_tag_get_object_type(tag)) {
4066 case GOT_OBJ_TYPE_BLOB:
4067 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4068 id_str);
4069 break;
4070 case GOT_OBJ_TYPE_TREE:
4071 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4072 id_str);
4073 break;
4074 case GOT_OBJ_TYPE_COMMIT:
4075 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4076 id_str);
4077 break;
4078 case GOT_OBJ_TYPE_TAG:
4079 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4080 id_str);
4081 break;
4082 default:
4083 break;
4086 free(id_str);
4087 if (commit) {
4088 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4089 if (err)
4090 break;
4091 got_object_commit_close(commit);
4092 } else {
4093 tagmsg0 = strdup(got_object_tag_get_message(tag));
4094 got_object_tag_close(tag);
4095 if (tagmsg0 == NULL) {
4096 err = got_error_from_errno("strdup");
4097 break;
4101 tagmsg = tagmsg0;
4102 do {
4103 line = strsep(&tagmsg, "\n");
4104 if (line)
4105 printf(" %s\n", line);
4106 } while (line);
4107 free(tagmsg0);
4110 got_ref_list_free(&refs);
4111 return NULL;
4114 static const struct got_error *
4115 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4116 const char *tag_name, const char *repo_path)
4118 const struct got_error *err = NULL;
4119 char *template = NULL, *initial_content = NULL;
4120 char *editor = NULL;
4121 int fd = -1;
4123 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4124 err = got_error_from_errno("asprintf");
4125 goto done;
4128 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4129 commit_id_str, tag_name) == -1) {
4130 err = got_error_from_errno("asprintf");
4131 goto done;
4134 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4135 if (err)
4136 goto done;
4138 dprintf(fd, initial_content);
4139 close(fd);
4141 err = get_editor(&editor);
4142 if (err)
4143 goto done;
4144 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4145 done:
4146 free(initial_content);
4147 free(template);
4148 free(editor);
4150 /* Editor is done; we can now apply unveil(2) */
4151 if (err == NULL) {
4152 err = apply_unveil(repo_path, 0, NULL);
4153 if (err) {
4154 free(*tagmsg);
4155 *tagmsg = NULL;
4158 return err;
4161 static const struct got_error *
4162 add_tag(struct got_repository *repo, const char *tag_name,
4163 const char *commit_arg, const char *tagmsg_arg)
4165 const struct got_error *err = NULL;
4166 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4167 char *label = NULL, *commit_id_str = NULL;
4168 struct got_reference *ref = NULL;
4169 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4170 char *tagmsg_path = NULL, *tag_id_str = NULL;
4171 int preserve_tagmsg = 0;
4174 * Don't let the user create a tag name with a leading '-'.
4175 * While technically a valid reference name, this case is usually
4176 * an unintended typo.
4178 if (tag_name[0] == '-')
4179 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4181 err = get_author(&tagger, repo);
4182 if (err)
4183 return err;
4185 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4186 GOT_OBJ_TYPE_COMMIT, 1, repo);
4187 if (err)
4188 goto done;
4190 err = got_object_id_str(&commit_id_str, commit_id);
4191 if (err)
4192 goto done;
4194 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4195 refname = strdup(tag_name);
4196 if (refname == NULL) {
4197 err = got_error_from_errno("strdup");
4198 goto done;
4200 tag_name += 10;
4201 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4202 err = got_error_from_errno("asprintf");
4203 goto done;
4206 err = got_ref_open(&ref, repo, refname, 0);
4207 if (err == NULL) {
4208 err = got_error(GOT_ERR_TAG_EXISTS);
4209 goto done;
4210 } else if (err->code != GOT_ERR_NOT_REF)
4211 goto done;
4213 if (tagmsg_arg == NULL) {
4214 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4215 tag_name, got_repo_get_path(repo));
4216 if (err) {
4217 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4218 tagmsg_path != NULL)
4219 preserve_tagmsg = 1;
4220 goto done;
4224 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4225 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4226 if (err) {
4227 if (tagmsg_path)
4228 preserve_tagmsg = 1;
4229 goto done;
4232 err = got_ref_alloc(&ref, refname, tag_id);
4233 if (err) {
4234 if (tagmsg_path)
4235 preserve_tagmsg = 1;
4236 goto done;
4239 err = got_ref_write(ref, repo);
4240 if (err) {
4241 if (tagmsg_path)
4242 preserve_tagmsg = 1;
4243 goto done;
4246 err = got_object_id_str(&tag_id_str, tag_id);
4247 if (err) {
4248 if (tagmsg_path)
4249 preserve_tagmsg = 1;
4250 goto done;
4252 printf("Created tag %s\n", tag_id_str);
4253 done:
4254 if (preserve_tagmsg) {
4255 fprintf(stderr, "%s: tag message preserved in %s\n",
4256 getprogname(), tagmsg_path);
4257 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4258 err = got_error_from_errno2("unlink", tagmsg_path);
4259 free(tag_id_str);
4260 if (ref)
4261 got_ref_close(ref);
4262 free(commit_id);
4263 free(commit_id_str);
4264 free(refname);
4265 free(tagmsg);
4266 free(tagmsg_path);
4267 free(tagger);
4268 return err;
4271 static const struct got_error *
4272 cmd_tag(int argc, char *argv[])
4274 const struct got_error *error = NULL;
4275 struct got_repository *repo = NULL;
4276 struct got_worktree *worktree = NULL;
4277 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4278 char *gitconfig_path = NULL;
4279 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4280 int ch, do_list = 0;
4282 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4283 switch (ch) {
4284 case 'c':
4285 commit_id_arg = optarg;
4286 break;
4287 case 'm':
4288 tagmsg = optarg;
4289 break;
4290 case 'r':
4291 repo_path = realpath(optarg, NULL);
4292 if (repo_path == NULL)
4293 return got_error_from_errno2("realpath",
4294 optarg);
4295 got_path_strip_trailing_slashes(repo_path);
4296 break;
4297 case 'l':
4298 do_list = 1;
4299 break;
4300 default:
4301 usage_tag();
4302 /* NOTREACHED */
4306 argc -= optind;
4307 argv += optind;
4309 if (do_list) {
4310 if (commit_id_arg != NULL)
4311 errx(1, "-c option can only be used when creating a tag");
4312 if (tagmsg)
4313 errx(1, "-l and -m options are mutually exclusive");
4314 if (argc > 0)
4315 usage_tag();
4316 } else if (argc != 1)
4317 usage_tag();
4319 tag_name = argv[0];
4321 #ifndef PROFILE
4322 if (do_list) {
4323 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4324 NULL) == -1)
4325 err(1, "pledge");
4326 } else {
4327 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4328 "sendfd unveil", NULL) == -1)
4329 err(1, "pledge");
4331 #endif
4332 cwd = getcwd(NULL, 0);
4333 if (cwd == NULL) {
4334 error = got_error_from_errno("getcwd");
4335 goto done;
4338 if (repo_path == NULL) {
4339 error = got_worktree_open(&worktree, cwd);
4340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4341 goto done;
4342 else
4343 error = NULL;
4344 if (worktree) {
4345 repo_path =
4346 strdup(got_worktree_get_repo_path(worktree));
4347 if (repo_path == NULL)
4348 error = got_error_from_errno("strdup");
4349 if (error)
4350 goto done;
4351 } else {
4352 repo_path = strdup(cwd);
4353 if (repo_path == NULL) {
4354 error = got_error_from_errno("strdup");
4355 goto done;
4360 if (do_list) {
4361 error = got_repo_open(&repo, repo_path, NULL);
4362 if (error != NULL)
4363 goto done;
4364 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4365 if (error)
4366 goto done;
4367 error = list_tags(repo, worktree);
4368 } else {
4369 error = get_gitconfig_path(&gitconfig_path);
4370 if (error)
4371 goto done;
4372 error = got_repo_open(&repo, repo_path, gitconfig_path);
4373 if (error != NULL)
4374 goto done;
4376 if (tagmsg) {
4377 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4378 if (error)
4379 goto done;
4382 if (commit_id_arg == NULL) {
4383 struct got_reference *head_ref;
4384 struct got_object_id *commit_id;
4385 error = got_ref_open(&head_ref, repo,
4386 worktree ? got_worktree_get_head_ref_name(worktree)
4387 : GOT_REF_HEAD, 0);
4388 if (error)
4389 goto done;
4390 error = got_ref_resolve(&commit_id, repo, head_ref);
4391 got_ref_close(head_ref);
4392 if (error)
4393 goto done;
4394 error = got_object_id_str(&commit_id_str, commit_id);
4395 free(commit_id);
4396 if (error)
4397 goto done;
4400 error = add_tag(repo, tag_name,
4401 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4403 done:
4404 if (repo)
4405 got_repo_close(repo);
4406 if (worktree)
4407 got_worktree_close(worktree);
4408 free(cwd);
4409 free(repo_path);
4410 free(gitconfig_path);
4411 free(commit_id_str);
4412 return error;
4415 __dead static void
4416 usage_add(void)
4418 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4419 getprogname());
4420 exit(1);
4423 static const struct got_error *
4424 add_progress(void *arg, unsigned char status, const char *path)
4426 while (path[0] == '/')
4427 path++;
4428 printf("%c %s\n", status, path);
4429 return NULL;
4432 static const struct got_error *
4433 cmd_add(int argc, char *argv[])
4435 const struct got_error *error = NULL;
4436 struct got_repository *repo = NULL;
4437 struct got_worktree *worktree = NULL;
4438 char *cwd = NULL;
4439 struct got_pathlist_head paths;
4440 struct got_pathlist_entry *pe;
4441 int ch, can_recurse = 0, no_ignores = 0;
4443 TAILQ_INIT(&paths);
4445 while ((ch = getopt(argc, argv, "IR")) != -1) {
4446 switch (ch) {
4447 case 'I':
4448 no_ignores = 1;
4449 break;
4450 case 'R':
4451 can_recurse = 1;
4452 break;
4453 default:
4454 usage_add();
4455 /* NOTREACHED */
4459 argc -= optind;
4460 argv += optind;
4462 #ifndef PROFILE
4463 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4464 NULL) == -1)
4465 err(1, "pledge");
4466 #endif
4467 if (argc < 1)
4468 usage_add();
4470 cwd = getcwd(NULL, 0);
4471 if (cwd == NULL) {
4472 error = got_error_from_errno("getcwd");
4473 goto done;
4476 error = got_worktree_open(&worktree, cwd);
4477 if (error)
4478 goto done;
4480 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4481 NULL);
4482 if (error != NULL)
4483 goto done;
4485 error = apply_unveil(got_repo_get_path(repo), 1,
4486 got_worktree_get_root_path(worktree));
4487 if (error)
4488 goto done;
4490 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4491 if (error)
4492 goto done;
4494 if (!can_recurse && no_ignores) {
4495 error = got_error_msg(GOT_ERR_BAD_PATH,
4496 "disregarding ignores requires -R option");
4497 goto done;
4501 if (!can_recurse) {
4502 char *ondisk_path;
4503 struct stat sb;
4504 TAILQ_FOREACH(pe, &paths, entry) {
4505 if (asprintf(&ondisk_path, "%s/%s",
4506 got_worktree_get_root_path(worktree),
4507 pe->path) == -1) {
4508 error = got_error_from_errno("asprintf");
4509 goto done;
4511 if (lstat(ondisk_path, &sb) == -1) {
4512 if (errno == ENOENT) {
4513 free(ondisk_path);
4514 continue;
4516 error = got_error_from_errno2("lstat",
4517 ondisk_path);
4518 free(ondisk_path);
4519 goto done;
4521 free(ondisk_path);
4522 if (S_ISDIR(sb.st_mode)) {
4523 error = got_error_msg(GOT_ERR_BAD_PATH,
4524 "adding directories requires -R option");
4525 goto done;
4530 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4531 NULL, repo, no_ignores);
4532 done:
4533 if (repo)
4534 got_repo_close(repo);
4535 if (worktree)
4536 got_worktree_close(worktree);
4537 TAILQ_FOREACH(pe, &paths, entry)
4538 free((char *)pe->path);
4539 got_pathlist_free(&paths);
4540 free(cwd);
4541 return error;
4544 __dead static void
4545 usage_remove(void)
4547 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4548 getprogname());
4549 exit(1);
4552 static const struct got_error *
4553 print_remove_status(void *arg, unsigned char status,
4554 unsigned char staged_status, const char *path)
4556 while (path[0] == '/')
4557 path++;
4558 if (status == GOT_STATUS_NONEXISTENT)
4559 return NULL;
4560 if (status == staged_status && (status == GOT_STATUS_DELETE))
4561 status = GOT_STATUS_NO_CHANGE;
4562 printf("%c%c %s\n", status, staged_status, path);
4563 return NULL;
4566 static const struct got_error *
4567 cmd_remove(int argc, char *argv[])
4569 const struct got_error *error = NULL;
4570 struct got_worktree *worktree = NULL;
4571 struct got_repository *repo = NULL;
4572 char *cwd = NULL;
4573 struct got_pathlist_head paths;
4574 struct got_pathlist_entry *pe;
4575 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4577 TAILQ_INIT(&paths);
4579 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4580 switch (ch) {
4581 case 'f':
4582 delete_local_mods = 1;
4583 break;
4584 case 'k':
4585 keep_on_disk = 1;
4586 break;
4587 case 'R':
4588 can_recurse = 1;
4589 break;
4590 default:
4591 usage_remove();
4592 /* NOTREACHED */
4596 argc -= optind;
4597 argv += optind;
4599 #ifndef PROFILE
4600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4601 NULL) == -1)
4602 err(1, "pledge");
4603 #endif
4604 if (argc < 1)
4605 usage_remove();
4607 cwd = getcwd(NULL, 0);
4608 if (cwd == NULL) {
4609 error = got_error_from_errno("getcwd");
4610 goto done;
4612 error = got_worktree_open(&worktree, cwd);
4613 if (error)
4614 goto done;
4616 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4617 NULL);
4618 if (error)
4619 goto done;
4621 error = apply_unveil(got_repo_get_path(repo), 1,
4622 got_worktree_get_root_path(worktree));
4623 if (error)
4624 goto done;
4626 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4627 if (error)
4628 goto done;
4630 if (!can_recurse) {
4631 char *ondisk_path;
4632 struct stat sb;
4633 TAILQ_FOREACH(pe, &paths, entry) {
4634 if (asprintf(&ondisk_path, "%s/%s",
4635 got_worktree_get_root_path(worktree),
4636 pe->path) == -1) {
4637 error = got_error_from_errno("asprintf");
4638 goto done;
4640 if (lstat(ondisk_path, &sb) == -1) {
4641 if (errno == ENOENT) {
4642 free(ondisk_path);
4643 continue;
4645 error = got_error_from_errno2("lstat",
4646 ondisk_path);
4647 free(ondisk_path);
4648 goto done;
4650 free(ondisk_path);
4651 if (S_ISDIR(sb.st_mode)) {
4652 error = got_error_msg(GOT_ERR_BAD_PATH,
4653 "removing directories requires -R option");
4654 goto done;
4659 error = got_worktree_schedule_delete(worktree, &paths,
4660 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4661 done:
4662 if (repo)
4663 got_repo_close(repo);
4664 if (worktree)
4665 got_worktree_close(worktree);
4666 TAILQ_FOREACH(pe, &paths, entry)
4667 free((char *)pe->path);
4668 got_pathlist_free(&paths);
4669 free(cwd);
4670 return error;
4673 __dead static void
4674 usage_revert(void)
4676 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4677 "path ...\n", getprogname());
4678 exit(1);
4681 static const struct got_error *
4682 revert_progress(void *arg, unsigned char status, const char *path)
4684 if (status == GOT_STATUS_UNVERSIONED)
4685 return NULL;
4687 while (path[0] == '/')
4688 path++;
4689 printf("%c %s\n", status, path);
4690 return NULL;
4693 struct choose_patch_arg {
4694 FILE *patch_script_file;
4695 const char *action;
4698 static const struct got_error *
4699 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4700 int nchanges, const char *action)
4702 char *line = NULL;
4703 size_t linesize = 0;
4704 ssize_t linelen;
4706 switch (status) {
4707 case GOT_STATUS_ADD:
4708 printf("A %s\n%s this addition? [y/n] ", path, action);
4709 break;
4710 case GOT_STATUS_DELETE:
4711 printf("D %s\n%s this deletion? [y/n] ", path, action);
4712 break;
4713 case GOT_STATUS_MODIFY:
4714 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4715 return got_error_from_errno("fseek");
4716 printf(GOT_COMMIT_SEP_STR);
4717 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4718 printf("%s", line);
4719 if (ferror(patch_file))
4720 return got_error_from_errno("getline");
4721 printf(GOT_COMMIT_SEP_STR);
4722 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4723 path, n, nchanges, action);
4724 break;
4725 default:
4726 return got_error_path(path, GOT_ERR_FILE_STATUS);
4729 return NULL;
4732 static const struct got_error *
4733 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4734 FILE *patch_file, int n, int nchanges)
4736 const struct got_error *err = NULL;
4737 char *line = NULL;
4738 size_t linesize = 0;
4739 ssize_t linelen;
4740 int resp = ' ';
4741 struct choose_patch_arg *a = arg;
4743 *choice = GOT_PATCH_CHOICE_NONE;
4745 if (a->patch_script_file) {
4746 char *nl;
4747 err = show_change(status, path, patch_file, n, nchanges,
4748 a->action);
4749 if (err)
4750 return err;
4751 linelen = getline(&line, &linesize, a->patch_script_file);
4752 if (linelen == -1) {
4753 if (ferror(a->patch_script_file))
4754 return got_error_from_errno("getline");
4755 return NULL;
4757 nl = strchr(line, '\n');
4758 if (nl)
4759 *nl = '\0';
4760 if (strcmp(line, "y") == 0) {
4761 *choice = GOT_PATCH_CHOICE_YES;
4762 printf("y\n");
4763 } else if (strcmp(line, "n") == 0) {
4764 *choice = GOT_PATCH_CHOICE_NO;
4765 printf("n\n");
4766 } else if (strcmp(line, "q") == 0 &&
4767 status == GOT_STATUS_MODIFY) {
4768 *choice = GOT_PATCH_CHOICE_QUIT;
4769 printf("q\n");
4770 } else
4771 printf("invalid response '%s'\n", line);
4772 free(line);
4773 return NULL;
4776 while (resp != 'y' && resp != 'n' && resp != 'q') {
4777 err = show_change(status, path, patch_file, n, nchanges,
4778 a->action);
4779 if (err)
4780 return err;
4781 resp = getchar();
4782 if (resp == '\n')
4783 resp = getchar();
4784 if (status == GOT_STATUS_MODIFY) {
4785 if (resp != 'y' && resp != 'n' && resp != 'q') {
4786 printf("invalid response '%c'\n", resp);
4787 resp = ' ';
4789 } else if (resp != 'y' && resp != 'n') {
4790 printf("invalid response '%c'\n", resp);
4791 resp = ' ';
4795 if (resp == 'y')
4796 *choice = GOT_PATCH_CHOICE_YES;
4797 else if (resp == 'n')
4798 *choice = GOT_PATCH_CHOICE_NO;
4799 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4800 *choice = GOT_PATCH_CHOICE_QUIT;
4802 return NULL;
4806 static const struct got_error *
4807 cmd_revert(int argc, char *argv[])
4809 const struct got_error *error = NULL;
4810 struct got_worktree *worktree = NULL;
4811 struct got_repository *repo = NULL;
4812 char *cwd = NULL, *path = NULL;
4813 struct got_pathlist_head paths;
4814 struct got_pathlist_entry *pe;
4815 int ch, can_recurse = 0, pflag = 0;
4816 FILE *patch_script_file = NULL;
4817 const char *patch_script_path = NULL;
4818 struct choose_patch_arg cpa;
4820 TAILQ_INIT(&paths);
4822 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4823 switch (ch) {
4824 case 'p':
4825 pflag = 1;
4826 break;
4827 case 'F':
4828 patch_script_path = optarg;
4829 break;
4830 case 'R':
4831 can_recurse = 1;
4832 break;
4833 default:
4834 usage_revert();
4835 /* NOTREACHED */
4839 argc -= optind;
4840 argv += optind;
4842 #ifndef PROFILE
4843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4844 "unveil", NULL) == -1)
4845 err(1, "pledge");
4846 #endif
4847 if (argc < 1)
4848 usage_revert();
4849 if (patch_script_path && !pflag)
4850 errx(1, "-F option can only be used together with -p option");
4852 cwd = getcwd(NULL, 0);
4853 if (cwd == NULL) {
4854 error = got_error_from_errno("getcwd");
4855 goto done;
4857 error = got_worktree_open(&worktree, cwd);
4858 if (error)
4859 goto done;
4861 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4862 NULL);
4863 if (error != NULL)
4864 goto done;
4866 if (patch_script_path) {
4867 patch_script_file = fopen(patch_script_path, "r");
4868 if (patch_script_file == NULL) {
4869 error = got_error_from_errno2("fopen",
4870 patch_script_path);
4871 goto done;
4874 error = apply_unveil(got_repo_get_path(repo), 1,
4875 got_worktree_get_root_path(worktree));
4876 if (error)
4877 goto done;
4879 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4880 if (error)
4881 goto done;
4883 if (!can_recurse) {
4884 char *ondisk_path;
4885 struct stat sb;
4886 TAILQ_FOREACH(pe, &paths, entry) {
4887 if (asprintf(&ondisk_path, "%s/%s",
4888 got_worktree_get_root_path(worktree),
4889 pe->path) == -1) {
4890 error = got_error_from_errno("asprintf");
4891 goto done;
4893 if (lstat(ondisk_path, &sb) == -1) {
4894 if (errno == ENOENT) {
4895 free(ondisk_path);
4896 continue;
4898 error = got_error_from_errno2("lstat",
4899 ondisk_path);
4900 free(ondisk_path);
4901 goto done;
4903 free(ondisk_path);
4904 if (S_ISDIR(sb.st_mode)) {
4905 error = got_error_msg(GOT_ERR_BAD_PATH,
4906 "reverting directories requires -R option");
4907 goto done;
4912 cpa.patch_script_file = patch_script_file;
4913 cpa.action = "revert";
4914 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4915 pflag ? choose_patch : NULL, &cpa, repo);
4916 done:
4917 if (patch_script_file && fclose(patch_script_file) == EOF &&
4918 error == NULL)
4919 error = got_error_from_errno2("fclose", patch_script_path);
4920 if (repo)
4921 got_repo_close(repo);
4922 if (worktree)
4923 got_worktree_close(worktree);
4924 free(path);
4925 free(cwd);
4926 return error;
4929 __dead static void
4930 usage_commit(void)
4932 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4933 getprogname());
4934 exit(1);
4937 struct collect_commit_logmsg_arg {
4938 const char *cmdline_log;
4939 const char *editor;
4940 const char *worktree_path;
4941 const char *branch_name;
4942 const char *repo_path;
4943 char *logmsg_path;
4947 static const struct got_error *
4948 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4949 void *arg)
4951 char *initial_content = NULL;
4952 struct got_pathlist_entry *pe;
4953 const struct got_error *err = NULL;
4954 char *template = NULL;
4955 struct collect_commit_logmsg_arg *a = arg;
4956 int fd;
4957 size_t len;
4959 /* if a message was specified on the command line, just use it */
4960 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4961 len = strlen(a->cmdline_log) + 1;
4962 *logmsg = malloc(len + 1);
4963 if (*logmsg == NULL)
4964 return got_error_from_errno("malloc");
4965 strlcpy(*logmsg, a->cmdline_log, len);
4966 return NULL;
4969 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4970 return got_error_from_errno("asprintf");
4972 if (asprintf(&initial_content,
4973 "\n# changes to be committed on branch %s:\n",
4974 a->branch_name) == -1)
4975 return got_error_from_errno("asprintf");
4977 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4978 if (err)
4979 goto done;
4981 dprintf(fd, initial_content);
4983 TAILQ_FOREACH(pe, commitable_paths, entry) {
4984 struct got_commitable *ct = pe->data;
4985 dprintf(fd, "# %c %s\n",
4986 got_commitable_get_status(ct),
4987 got_commitable_get_path(ct));
4989 close(fd);
4991 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4992 done:
4993 free(initial_content);
4994 free(template);
4996 /* Editor is done; we can now apply unveil(2) */
4997 if (err == NULL) {
4998 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4999 if (err) {
5000 free(*logmsg);
5001 *logmsg = NULL;
5004 return err;
5007 static const struct got_error *
5008 cmd_commit(int argc, char *argv[])
5010 const struct got_error *error = NULL;
5011 struct got_worktree *worktree = NULL;
5012 struct got_repository *repo = NULL;
5013 char *cwd = NULL, *id_str = NULL;
5014 struct got_object_id *id = NULL;
5015 const char *logmsg = NULL;
5016 struct collect_commit_logmsg_arg cl_arg;
5017 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5018 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5019 struct got_pathlist_head paths;
5021 TAILQ_INIT(&paths);
5022 cl_arg.logmsg_path = NULL;
5024 while ((ch = getopt(argc, argv, "m:")) != -1) {
5025 switch (ch) {
5026 case 'm':
5027 logmsg = optarg;
5028 break;
5029 default:
5030 usage_commit();
5031 /* NOTREACHED */
5035 argc -= optind;
5036 argv += optind;
5038 #ifndef PROFILE
5039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5040 "unveil", NULL) == -1)
5041 err(1, "pledge");
5042 #endif
5043 cwd = getcwd(NULL, 0);
5044 if (cwd == NULL) {
5045 error = got_error_from_errno("getcwd");
5046 goto done;
5048 error = got_worktree_open(&worktree, cwd);
5049 if (error)
5050 goto done;
5052 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5053 if (error)
5054 goto done;
5055 if (rebase_in_progress) {
5056 error = got_error(GOT_ERR_REBASING);
5057 goto done;
5060 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5061 worktree);
5062 if (error)
5063 goto done;
5065 error = get_gitconfig_path(&gitconfig_path);
5066 if (error)
5067 goto done;
5068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5069 gitconfig_path);
5070 if (error != NULL)
5071 goto done;
5073 error = get_author(&author, repo);
5074 if (error)
5075 return error;
5078 * unveil(2) traverses exec(2); if an editor is used we have
5079 * to apply unveil after the log message has been written.
5081 if (logmsg == NULL || strlen(logmsg) == 0)
5082 error = get_editor(&editor);
5083 else
5084 error = apply_unveil(got_repo_get_path(repo), 0,
5085 got_worktree_get_root_path(worktree));
5086 if (error)
5087 goto done;
5089 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5090 if (error)
5091 goto done;
5093 cl_arg.editor = editor;
5094 cl_arg.cmdline_log = logmsg;
5095 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5096 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5097 if (!histedit_in_progress) {
5098 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5099 error = got_error(GOT_ERR_COMMIT_BRANCH);
5100 goto done;
5102 cl_arg.branch_name += 11;
5104 cl_arg.repo_path = got_repo_get_path(repo);
5105 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5106 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5107 if (error) {
5108 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5109 cl_arg.logmsg_path != NULL)
5110 preserve_logmsg = 1;
5111 goto done;
5114 error = got_object_id_str(&id_str, id);
5115 if (error)
5116 goto done;
5117 printf("Created commit %s\n", id_str);
5118 done:
5119 if (preserve_logmsg) {
5120 fprintf(stderr, "%s: log message preserved in %s\n",
5121 getprogname(), cl_arg.logmsg_path);
5122 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5123 error == NULL)
5124 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5125 free(cl_arg.logmsg_path);
5126 if (repo)
5127 got_repo_close(repo);
5128 if (worktree)
5129 got_worktree_close(worktree);
5130 free(cwd);
5131 free(id_str);
5132 free(gitconfig_path);
5133 free(editor);
5134 free(author);
5135 return error;
5138 __dead static void
5139 usage_cherrypick(void)
5141 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5142 exit(1);
5145 static const struct got_error *
5146 cmd_cherrypick(int argc, char *argv[])
5148 const struct got_error *error = NULL;
5149 struct got_worktree *worktree = NULL;
5150 struct got_repository *repo = NULL;
5151 char *cwd = NULL, *commit_id_str = NULL;
5152 struct got_object_id *commit_id = NULL;
5153 struct got_commit_object *commit = NULL;
5154 struct got_object_qid *pid;
5155 struct got_reference *head_ref = NULL;
5156 int ch, did_something = 0;
5158 while ((ch = getopt(argc, argv, "")) != -1) {
5159 switch (ch) {
5160 default:
5161 usage_cherrypick();
5162 /* NOTREACHED */
5166 argc -= optind;
5167 argv += optind;
5169 #ifndef PROFILE
5170 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5171 "unveil", NULL) == -1)
5172 err(1, "pledge");
5173 #endif
5174 if (argc != 1)
5175 usage_cherrypick();
5177 cwd = getcwd(NULL, 0);
5178 if (cwd == NULL) {
5179 error = got_error_from_errno("getcwd");
5180 goto done;
5182 error = got_worktree_open(&worktree, cwd);
5183 if (error)
5184 goto done;
5186 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5187 NULL);
5188 if (error != NULL)
5189 goto done;
5191 error = apply_unveil(got_repo_get_path(repo), 0,
5192 got_worktree_get_root_path(worktree));
5193 if (error)
5194 goto done;
5196 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5197 GOT_OBJ_TYPE_COMMIT, repo);
5198 if (error != NULL) {
5199 struct got_reference *ref;
5200 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5201 goto done;
5202 error = got_ref_open(&ref, repo, argv[0], 0);
5203 if (error != NULL)
5204 goto done;
5205 error = got_ref_resolve(&commit_id, repo, ref);
5206 got_ref_close(ref);
5207 if (error != NULL)
5208 goto done;
5210 error = got_object_id_str(&commit_id_str, commit_id);
5211 if (error)
5212 goto done;
5214 error = got_ref_open(&head_ref, repo,
5215 got_worktree_get_head_ref_name(worktree), 0);
5216 if (error != NULL)
5217 goto done;
5219 error = check_same_branch(commit_id, head_ref, NULL, repo);
5220 if (error) {
5221 if (error->code != GOT_ERR_ANCESTRY)
5222 goto done;
5223 error = NULL;
5224 } else {
5225 error = got_error(GOT_ERR_SAME_BRANCH);
5226 goto done;
5229 error = got_object_open_as_commit(&commit, repo, commit_id);
5230 if (error)
5231 goto done;
5232 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5233 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5234 commit_id, repo, update_progress, &did_something, check_cancelled,
5235 NULL);
5236 if (error != NULL)
5237 goto done;
5239 if (did_something)
5240 printf("Merged commit %s\n", commit_id_str);
5241 done:
5242 if (commit)
5243 got_object_commit_close(commit);
5244 free(commit_id_str);
5245 if (head_ref)
5246 got_ref_close(head_ref);
5247 if (worktree)
5248 got_worktree_close(worktree);
5249 if (repo)
5250 got_repo_close(repo);
5251 return error;
5254 __dead static void
5255 usage_backout(void)
5257 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5258 exit(1);
5261 static const struct got_error *
5262 cmd_backout(int argc, char *argv[])
5264 const struct got_error *error = NULL;
5265 struct got_worktree *worktree = NULL;
5266 struct got_repository *repo = NULL;
5267 char *cwd = NULL, *commit_id_str = NULL;
5268 struct got_object_id *commit_id = NULL;
5269 struct got_commit_object *commit = NULL;
5270 struct got_object_qid *pid;
5271 struct got_reference *head_ref = NULL;
5272 int ch, did_something = 0;
5274 while ((ch = getopt(argc, argv, "")) != -1) {
5275 switch (ch) {
5276 default:
5277 usage_backout();
5278 /* NOTREACHED */
5282 argc -= optind;
5283 argv += optind;
5285 #ifndef PROFILE
5286 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5287 "unveil", NULL) == -1)
5288 err(1, "pledge");
5289 #endif
5290 if (argc != 1)
5291 usage_backout();
5293 cwd = getcwd(NULL, 0);
5294 if (cwd == NULL) {
5295 error = got_error_from_errno("getcwd");
5296 goto done;
5298 error = got_worktree_open(&worktree, cwd);
5299 if (error)
5300 goto done;
5302 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5303 NULL);
5304 if (error != NULL)
5305 goto done;
5307 error = apply_unveil(got_repo_get_path(repo), 0,
5308 got_worktree_get_root_path(worktree));
5309 if (error)
5310 goto done;
5312 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5313 GOT_OBJ_TYPE_COMMIT, repo);
5314 if (error != NULL) {
5315 struct got_reference *ref;
5316 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5317 goto done;
5318 error = got_ref_open(&ref, repo, argv[0], 0);
5319 if (error != NULL)
5320 goto done;
5321 error = got_ref_resolve(&commit_id, repo, ref);
5322 got_ref_close(ref);
5323 if (error != NULL)
5324 goto done;
5326 error = got_object_id_str(&commit_id_str, commit_id);
5327 if (error)
5328 goto done;
5330 error = got_ref_open(&head_ref, repo,
5331 got_worktree_get_head_ref_name(worktree), 0);
5332 if (error != NULL)
5333 goto done;
5335 error = check_same_branch(commit_id, head_ref, NULL, repo);
5336 if (error)
5337 goto done;
5339 error = got_object_open_as_commit(&commit, repo, commit_id);
5340 if (error)
5341 goto done;
5342 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5343 if (pid == NULL) {
5344 error = got_error(GOT_ERR_ROOT_COMMIT);
5345 goto done;
5348 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5349 update_progress, &did_something, check_cancelled, NULL);
5350 if (error != NULL)
5351 goto done;
5353 if (did_something)
5354 printf("Backed out commit %s\n", commit_id_str);
5355 done:
5356 if (commit)
5357 got_object_commit_close(commit);
5358 free(commit_id_str);
5359 if (head_ref)
5360 got_ref_close(head_ref);
5361 if (worktree)
5362 got_worktree_close(worktree);
5363 if (repo)
5364 got_repo_close(repo);
5365 return error;
5368 __dead static void
5369 usage_rebase(void)
5371 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5372 getprogname());
5373 exit(1);
5376 void
5377 trim_logmsg(char *logmsg, int limit)
5379 char *nl;
5380 size_t len;
5382 len = strlen(logmsg);
5383 if (len > limit)
5384 len = limit;
5385 logmsg[len] = '\0';
5386 nl = strchr(logmsg, '\n');
5387 if (nl)
5388 *nl = '\0';
5391 static const struct got_error *
5392 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5394 const struct got_error *err;
5395 char *logmsg0 = NULL;
5396 const char *s;
5398 err = got_object_commit_get_logmsg(&logmsg0, commit);
5399 if (err)
5400 return err;
5402 s = logmsg0;
5403 while (isspace((unsigned char)s[0]))
5404 s++;
5406 *logmsg = strdup(s);
5407 if (*logmsg == NULL) {
5408 err = got_error_from_errno("strdup");
5409 goto done;
5412 trim_logmsg(*logmsg, limit);
5413 done:
5414 free(logmsg0);
5415 return err;
5418 static const struct got_error *
5419 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5421 const struct got_error *err;
5422 struct got_commit_object *commit = NULL;
5423 char *id_str = NULL, *logmsg = NULL;
5425 err = got_object_open_as_commit(&commit, repo, id);
5426 if (err)
5427 return err;
5429 err = got_object_id_str(&id_str, id);
5430 if (err)
5431 goto done;
5433 id_str[12] = '\0';
5435 err = get_short_logmsg(&logmsg, 42, commit);
5436 if (err)
5437 goto done;
5439 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5440 done:
5441 free(id_str);
5442 got_object_commit_close(commit);
5443 free(logmsg);
5444 return err;
5447 static const struct got_error *
5448 show_rebase_progress(struct got_commit_object *commit,
5449 struct got_object_id *old_id, struct got_object_id *new_id)
5451 const struct got_error *err;
5452 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5454 err = got_object_id_str(&old_id_str, old_id);
5455 if (err)
5456 goto done;
5458 if (new_id) {
5459 err = got_object_id_str(&new_id_str, new_id);
5460 if (err)
5461 goto done;
5464 old_id_str[12] = '\0';
5465 if (new_id_str)
5466 new_id_str[12] = '\0';
5468 err = get_short_logmsg(&logmsg, 42, commit);
5469 if (err)
5470 goto done;
5472 printf("%s -> %s: %s\n", old_id_str,
5473 new_id_str ? new_id_str : "no-op change", logmsg);
5474 done:
5475 free(old_id_str);
5476 free(new_id_str);
5477 free(logmsg);
5478 return err;
5481 static const struct got_error *
5482 rebase_progress(void *arg, unsigned char status, const char *path)
5484 unsigned char *rebase_status = arg;
5486 while (path[0] == '/')
5487 path++;
5488 printf("%c %s\n", status, path);
5490 if (*rebase_status == GOT_STATUS_CONFLICT)
5491 return NULL;
5492 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5493 *rebase_status = status;
5494 return NULL;
5497 static const struct got_error *
5498 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5499 struct got_reference *branch, struct got_reference *new_base_branch,
5500 struct got_reference *tmp_branch, struct got_repository *repo)
5502 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5503 return got_worktree_rebase_complete(worktree, fileindex,
5504 new_base_branch, tmp_branch, branch, repo);
5507 static const struct got_error *
5508 rebase_commit(struct got_pathlist_head *merged_paths,
5509 struct got_worktree *worktree, struct got_fileindex *fileindex,
5510 struct got_reference *tmp_branch,
5511 struct got_object_id *commit_id, struct got_repository *repo)
5513 const struct got_error *error;
5514 struct got_commit_object *commit;
5515 struct got_object_id *new_commit_id;
5517 error = got_object_open_as_commit(&commit, repo, commit_id);
5518 if (error)
5519 return error;
5521 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5522 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5523 if (error) {
5524 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5525 goto done;
5526 error = show_rebase_progress(commit, commit_id, NULL);
5527 } else {
5528 error = show_rebase_progress(commit, commit_id, new_commit_id);
5529 free(new_commit_id);
5531 done:
5532 got_object_commit_close(commit);
5533 return error;
5536 struct check_path_prefix_arg {
5537 const char *path_prefix;
5538 size_t len;
5539 int errcode;
5542 static const struct got_error *
5543 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5544 struct got_blob_object *blob2, struct got_object_id *id1,
5545 struct got_object_id *id2, const char *path1, const char *path2,
5546 mode_t mode1, mode_t mode2, struct got_repository *repo)
5548 struct check_path_prefix_arg *a = arg;
5550 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5551 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5552 return got_error(a->errcode);
5554 return NULL;
5557 static const struct got_error *
5558 check_path_prefix(struct got_object_id *parent_id,
5559 struct got_object_id *commit_id, const char *path_prefix,
5560 int errcode, struct got_repository *repo)
5562 const struct got_error *err;
5563 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5564 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5565 struct check_path_prefix_arg cpp_arg;
5567 if (got_path_is_root_dir(path_prefix))
5568 return NULL;
5570 err = got_object_open_as_commit(&commit, repo, commit_id);
5571 if (err)
5572 goto done;
5574 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5575 if (err)
5576 goto done;
5578 err = got_object_open_as_tree(&tree1, repo,
5579 got_object_commit_get_tree_id(parent_commit));
5580 if (err)
5581 goto done;
5583 err = got_object_open_as_tree(&tree2, repo,
5584 got_object_commit_get_tree_id(commit));
5585 if (err)
5586 goto done;
5588 cpp_arg.path_prefix = path_prefix;
5589 while (cpp_arg.path_prefix[0] == '/')
5590 cpp_arg.path_prefix++;
5591 cpp_arg.len = strlen(cpp_arg.path_prefix);
5592 cpp_arg.errcode = errcode;
5593 err = got_diff_tree(tree1, tree2, "", "", repo,
5594 check_path_prefix_in_diff, &cpp_arg, 0);
5595 done:
5596 if (tree1)
5597 got_object_tree_close(tree1);
5598 if (tree2)
5599 got_object_tree_close(tree2);
5600 if (commit)
5601 got_object_commit_close(commit);
5602 if (parent_commit)
5603 got_object_commit_close(parent_commit);
5604 return err;
5607 static const struct got_error *
5608 collect_commits(struct got_object_id_queue *commits,
5609 struct got_object_id *initial_commit_id,
5610 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5611 const char *path_prefix, int path_prefix_errcode,
5612 struct got_repository *repo)
5614 const struct got_error *err = NULL;
5615 struct got_commit_graph *graph = NULL;
5616 struct got_object_id *parent_id = NULL;
5617 struct got_object_qid *qid;
5618 struct got_object_id *commit_id = initial_commit_id;
5620 err = got_commit_graph_open(&graph, "/", 1);
5621 if (err)
5622 return err;
5624 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5625 check_cancelled, NULL);
5626 if (err)
5627 goto done;
5628 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5629 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5630 check_cancelled, NULL);
5631 if (err) {
5632 if (err->code == GOT_ERR_ITER_COMPLETED) {
5633 err = got_error_msg(GOT_ERR_ANCESTRY,
5634 "ran out of commits to rebase before "
5635 "youngest common ancestor commit has "
5636 "been reached?!?");
5638 goto done;
5639 } else {
5640 err = check_path_prefix(parent_id, commit_id,
5641 path_prefix, path_prefix_errcode, repo);
5642 if (err)
5643 goto done;
5645 err = got_object_qid_alloc(&qid, commit_id);
5646 if (err)
5647 goto done;
5648 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5649 commit_id = parent_id;
5652 done:
5653 got_commit_graph_close(graph);
5654 return err;
5657 static const struct got_error *
5658 cmd_rebase(int argc, char *argv[])
5660 const struct got_error *error = NULL;
5661 struct got_worktree *worktree = NULL;
5662 struct got_repository *repo = NULL;
5663 struct got_fileindex *fileindex = NULL;
5664 char *cwd = NULL;
5665 struct got_reference *branch = NULL;
5666 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5667 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5668 struct got_object_id *resume_commit_id = NULL;
5669 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5670 struct got_commit_object *commit = NULL;
5671 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5672 int histedit_in_progress = 0;
5673 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5674 struct got_object_id_queue commits;
5675 struct got_pathlist_head merged_paths;
5676 const struct got_object_id_queue *parent_ids;
5677 struct got_object_qid *qid, *pid;
5679 SIMPLEQ_INIT(&commits);
5680 TAILQ_INIT(&merged_paths);
5682 while ((ch = getopt(argc, argv, "ac")) != -1) {
5683 switch (ch) {
5684 case 'a':
5685 abort_rebase = 1;
5686 break;
5687 case 'c':
5688 continue_rebase = 1;
5689 break;
5690 default:
5691 usage_rebase();
5692 /* NOTREACHED */
5696 argc -= optind;
5697 argv += optind;
5699 #ifndef PROFILE
5700 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5701 "unveil", NULL) == -1)
5702 err(1, "pledge");
5703 #endif
5704 if (abort_rebase && continue_rebase)
5705 usage_rebase();
5706 else if (abort_rebase || continue_rebase) {
5707 if (argc != 0)
5708 usage_rebase();
5709 } else if (argc != 1)
5710 usage_rebase();
5712 cwd = getcwd(NULL, 0);
5713 if (cwd == NULL) {
5714 error = got_error_from_errno("getcwd");
5715 goto done;
5717 error = got_worktree_open(&worktree, cwd);
5718 if (error)
5719 goto done;
5721 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5722 NULL);
5723 if (error != NULL)
5724 goto done;
5726 error = apply_unveil(got_repo_get_path(repo), 0,
5727 got_worktree_get_root_path(worktree));
5728 if (error)
5729 goto done;
5731 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5732 worktree);
5733 if (error)
5734 goto done;
5735 if (histedit_in_progress) {
5736 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5737 goto done;
5740 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5741 if (error)
5742 goto done;
5744 if (abort_rebase) {
5745 int did_something;
5746 if (!rebase_in_progress) {
5747 error = got_error(GOT_ERR_NOT_REBASING);
5748 goto done;
5750 error = got_worktree_rebase_continue(&resume_commit_id,
5751 &new_base_branch, &tmp_branch, &branch, &fileindex,
5752 worktree, repo);
5753 if (error)
5754 goto done;
5755 printf("Switching work tree to %s\n",
5756 got_ref_get_symref_target(new_base_branch));
5757 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5758 new_base_branch, update_progress, &did_something);
5759 if (error)
5760 goto done;
5761 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5762 goto done; /* nothing else to do */
5765 if (continue_rebase) {
5766 if (!rebase_in_progress) {
5767 error = got_error(GOT_ERR_NOT_REBASING);
5768 goto done;
5770 error = got_worktree_rebase_continue(&resume_commit_id,
5771 &new_base_branch, &tmp_branch, &branch, &fileindex,
5772 worktree, repo);
5773 if (error)
5774 goto done;
5776 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5777 resume_commit_id, repo);
5778 if (error)
5779 goto done;
5781 yca_id = got_object_id_dup(resume_commit_id);
5782 if (yca_id == NULL) {
5783 error = got_error_from_errno("got_object_id_dup");
5784 goto done;
5786 } else {
5787 error = got_ref_open(&branch, repo, argv[0], 0);
5788 if (error != NULL)
5789 goto done;
5792 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5793 if (error)
5794 goto done;
5796 if (!continue_rebase) {
5797 struct got_object_id *base_commit_id;
5799 base_commit_id = got_worktree_get_base_commit_id(worktree);
5800 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5801 base_commit_id, branch_head_commit_id, repo,
5802 check_cancelled, NULL);
5803 if (error)
5804 goto done;
5805 if (yca_id == NULL) {
5806 error = got_error_msg(GOT_ERR_ANCESTRY,
5807 "specified branch shares no common ancestry "
5808 "with work tree's branch");
5809 goto done;
5812 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5813 if (error) {
5814 if (error->code != GOT_ERR_ANCESTRY)
5815 goto done;
5816 error = NULL;
5817 } else {
5818 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5819 "specified branch resolves to a commit which "
5820 "is already contained in work tree's branch");
5821 goto done;
5823 error = got_worktree_rebase_prepare(&new_base_branch,
5824 &tmp_branch, &fileindex, worktree, branch, repo);
5825 if (error)
5826 goto done;
5829 commit_id = branch_head_commit_id;
5830 error = got_object_open_as_commit(&commit, repo, commit_id);
5831 if (error)
5832 goto done;
5834 parent_ids = got_object_commit_get_parent_ids(commit);
5835 pid = SIMPLEQ_FIRST(parent_ids);
5836 if (pid == NULL) {
5837 if (!continue_rebase) {
5838 int did_something;
5839 error = got_worktree_rebase_abort(worktree, fileindex,
5840 repo, new_base_branch, update_progress,
5841 &did_something);
5842 if (error)
5843 goto done;
5844 printf("Rebase of %s aborted\n",
5845 got_ref_get_name(branch));
5847 error = got_error(GOT_ERR_EMPTY_REBASE);
5848 goto done;
5850 error = collect_commits(&commits, commit_id, pid->id,
5851 yca_id, got_worktree_get_path_prefix(worktree),
5852 GOT_ERR_REBASE_PATH, repo);
5853 got_object_commit_close(commit);
5854 commit = NULL;
5855 if (error)
5856 goto done;
5858 if (SIMPLEQ_EMPTY(&commits)) {
5859 if (continue_rebase) {
5860 error = rebase_complete(worktree, fileindex,
5861 branch, new_base_branch, tmp_branch, repo);
5862 goto done;
5863 } else {
5864 /* Fast-forward the reference of the branch. */
5865 struct got_object_id *new_head_commit_id;
5866 char *id_str;
5867 error = got_ref_resolve(&new_head_commit_id, repo,
5868 new_base_branch);
5869 if (error)
5870 goto done;
5871 error = got_object_id_str(&id_str, new_head_commit_id);
5872 printf("Forwarding %s to commit %s\n",
5873 got_ref_get_name(branch), id_str);
5874 free(id_str);
5875 error = got_ref_change_ref(branch,
5876 new_head_commit_id);
5877 if (error)
5878 goto done;
5882 pid = NULL;
5883 SIMPLEQ_FOREACH(qid, &commits, entry) {
5884 commit_id = qid->id;
5885 parent_id = pid ? pid->id : yca_id;
5886 pid = qid;
5888 error = got_worktree_rebase_merge_files(&merged_paths,
5889 worktree, fileindex, parent_id, commit_id, repo,
5890 rebase_progress, &rebase_status, check_cancelled, NULL);
5891 if (error)
5892 goto done;
5894 if (rebase_status == GOT_STATUS_CONFLICT) {
5895 error = show_rebase_merge_conflict(qid->id, repo);
5896 if (error)
5897 goto done;
5898 got_worktree_rebase_pathlist_free(&merged_paths);
5899 break;
5902 error = rebase_commit(&merged_paths, worktree, fileindex,
5903 tmp_branch, commit_id, repo);
5904 got_worktree_rebase_pathlist_free(&merged_paths);
5905 if (error)
5906 goto done;
5909 if (rebase_status == GOT_STATUS_CONFLICT) {
5910 error = got_worktree_rebase_postpone(worktree, fileindex);
5911 if (error)
5912 goto done;
5913 error = got_error_msg(GOT_ERR_CONFLICTS,
5914 "conflicts must be resolved before rebasing can continue");
5915 } else
5916 error = rebase_complete(worktree, fileindex, branch,
5917 new_base_branch, tmp_branch, repo);
5918 done:
5919 got_object_id_queue_free(&commits);
5920 free(branch_head_commit_id);
5921 free(resume_commit_id);
5922 free(yca_id);
5923 if (commit)
5924 got_object_commit_close(commit);
5925 if (branch)
5926 got_ref_close(branch);
5927 if (new_base_branch)
5928 got_ref_close(new_base_branch);
5929 if (tmp_branch)
5930 got_ref_close(tmp_branch);
5931 if (worktree)
5932 got_worktree_close(worktree);
5933 if (repo)
5934 got_repo_close(repo);
5935 return error;
5938 __dead static void
5939 usage_histedit(void)
5941 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5942 getprogname());
5943 exit(1);
5946 #define GOT_HISTEDIT_PICK 'p'
5947 #define GOT_HISTEDIT_EDIT 'e'
5948 #define GOT_HISTEDIT_FOLD 'f'
5949 #define GOT_HISTEDIT_DROP 'd'
5950 #define GOT_HISTEDIT_MESG 'm'
5952 static struct got_histedit_cmd {
5953 unsigned char code;
5954 const char *name;
5955 const char *desc;
5956 } got_histedit_cmds[] = {
5957 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5958 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5959 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5960 "be used" },
5961 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5962 { GOT_HISTEDIT_MESG, "mesg",
5963 "single-line log message for commit above (open editor if empty)" },
5966 struct got_histedit_list_entry {
5967 TAILQ_ENTRY(got_histedit_list_entry) entry;
5968 struct got_object_id *commit_id;
5969 const struct got_histedit_cmd *cmd;
5970 char *logmsg;
5972 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5974 static const struct got_error *
5975 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5976 FILE *f, struct got_repository *repo)
5978 const struct got_error *err = NULL;
5979 char *logmsg = NULL, *id_str = NULL;
5980 struct got_commit_object *commit = NULL;
5981 int n;
5983 err = got_object_open_as_commit(&commit, repo, commit_id);
5984 if (err)
5985 goto done;
5987 err = get_short_logmsg(&logmsg, 34, commit);
5988 if (err)
5989 goto done;
5991 err = got_object_id_str(&id_str, commit_id);
5992 if (err)
5993 goto done;
5995 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5996 if (n < 0)
5997 err = got_ferror(f, GOT_ERR_IO);
5998 done:
5999 if (commit)
6000 got_object_commit_close(commit);
6001 free(id_str);
6002 free(logmsg);
6003 return err;
6006 static const struct got_error *
6007 histedit_write_commit_list(struct got_object_id_queue *commits,
6008 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6010 const struct got_error *err = NULL;
6011 struct got_object_qid *qid;
6013 if (SIMPLEQ_EMPTY(commits))
6014 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6016 SIMPLEQ_FOREACH(qid, commits, entry) {
6017 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6018 f, repo);
6019 if (err)
6020 break;
6021 if (edit_logmsg_only) {
6022 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6023 if (n < 0) {
6024 err = got_ferror(f, GOT_ERR_IO);
6025 break;
6030 return err;
6033 static const struct got_error *
6034 write_cmd_list(FILE *f, const char *branch_name,
6035 struct got_object_id_queue *commits)
6037 const struct got_error *err = NULL;
6038 int n, i;
6039 char *id_str;
6040 struct got_object_qid *qid;
6042 qid = SIMPLEQ_FIRST(commits);
6043 err = got_object_id_str(&id_str, qid->id);
6044 if (err)
6045 return err;
6047 n = fprintf(f,
6048 "# Editing the history of branch '%s' starting at\n"
6049 "# commit %s\n"
6050 "# Commits will be processed in order from top to "
6051 "bottom of this file.\n", branch_name, id_str);
6052 if (n < 0) {
6053 err = got_ferror(f, GOT_ERR_IO);
6054 goto done;
6057 n = fprintf(f, "# Available histedit commands:\n");
6058 if (n < 0) {
6059 err = got_ferror(f, GOT_ERR_IO);
6060 goto done;
6063 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6064 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6065 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6066 cmd->desc);
6067 if (n < 0) {
6068 err = got_ferror(f, GOT_ERR_IO);
6069 break;
6072 done:
6073 free(id_str);
6074 return err;
6077 static const struct got_error *
6078 histedit_syntax_error(int lineno)
6080 static char msg[42];
6081 int ret;
6083 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6084 lineno);
6085 if (ret == -1 || ret >= sizeof(msg))
6086 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6088 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6091 static const struct got_error *
6092 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6093 char *logmsg, struct got_repository *repo)
6095 const struct got_error *err;
6096 struct got_commit_object *folded_commit = NULL;
6097 char *id_str, *folded_logmsg = NULL;
6099 err = got_object_id_str(&id_str, hle->commit_id);
6100 if (err)
6101 return err;
6103 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6104 if (err)
6105 goto done;
6107 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6108 if (err)
6109 goto done;
6110 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6111 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6112 folded_logmsg) == -1) {
6113 err = got_error_from_errno("asprintf");
6115 done:
6116 if (folded_commit)
6117 got_object_commit_close(folded_commit);
6118 free(id_str);
6119 free(folded_logmsg);
6120 return err;
6123 static struct got_histedit_list_entry *
6124 get_folded_commits(struct got_histedit_list_entry *hle)
6126 struct got_histedit_list_entry *prev, *folded = NULL;
6128 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6129 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6130 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6131 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6132 folded = prev;
6133 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6136 return folded;
6139 static const struct got_error *
6140 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6141 struct got_repository *repo)
6143 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6144 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6145 const struct got_error *err = NULL;
6146 struct got_commit_object *commit = NULL;
6147 int fd;
6148 struct got_histedit_list_entry *folded = NULL;
6150 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6151 if (err)
6152 return err;
6154 folded = get_folded_commits(hle);
6155 if (folded) {
6156 while (folded != hle) {
6157 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6158 folded = TAILQ_NEXT(folded, entry);
6159 continue;
6161 err = append_folded_commit_msg(&new_msg, folded,
6162 logmsg, repo);
6163 if (err)
6164 goto done;
6165 free(logmsg);
6166 logmsg = new_msg;
6167 folded = TAILQ_NEXT(folded, entry);
6171 err = got_object_id_str(&id_str, hle->commit_id);
6172 if (err)
6173 goto done;
6174 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6175 if (err)
6176 goto done;
6177 if (asprintf(&new_msg,
6178 "%s\n# original log message of commit %s: %s",
6179 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6180 err = got_error_from_errno("asprintf");
6181 goto done;
6183 free(logmsg);
6184 logmsg = new_msg;
6186 err = got_object_id_str(&id_str, hle->commit_id);
6187 if (err)
6188 goto done;
6190 err = got_opentemp_named_fd(&logmsg_path, &fd,
6191 GOT_TMPDIR_STR "/got-logmsg");
6192 if (err)
6193 goto done;
6195 dprintf(fd, logmsg);
6196 close(fd);
6198 err = get_editor(&editor);
6199 if (err)
6200 goto done;
6202 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6203 if (err) {
6204 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6205 goto done;
6206 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6208 done:
6209 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6210 err = got_error_from_errno2("unlink", logmsg_path);
6211 free(logmsg_path);
6212 free(logmsg);
6213 free(orig_logmsg);
6214 free(editor);
6215 if (commit)
6216 got_object_commit_close(commit);
6217 return err;
6220 static const struct got_error *
6221 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6222 FILE *f, struct got_repository *repo)
6224 const struct got_error *err = NULL;
6225 char *line = NULL, *p, *end;
6226 size_t size;
6227 ssize_t len;
6228 int lineno = 0, i;
6229 const struct got_histedit_cmd *cmd;
6230 struct got_object_id *commit_id = NULL;
6231 struct got_histedit_list_entry *hle = NULL;
6233 for (;;) {
6234 len = getline(&line, &size, f);
6235 if (len == -1) {
6236 const struct got_error *getline_err;
6237 if (feof(f))
6238 break;
6239 getline_err = got_error_from_errno("getline");
6240 err = got_ferror(f, getline_err->code);
6241 break;
6243 lineno++;
6244 p = line;
6245 while (isspace((unsigned char)p[0]))
6246 p++;
6247 if (p[0] == '#' || p[0] == '\0') {
6248 free(line);
6249 line = NULL;
6250 continue;
6252 cmd = NULL;
6253 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6254 cmd = &got_histedit_cmds[i];
6255 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6256 isspace((unsigned char)p[strlen(cmd->name)])) {
6257 p += strlen(cmd->name);
6258 break;
6260 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6261 p++;
6262 break;
6265 if (i == nitems(got_histedit_cmds)) {
6266 err = histedit_syntax_error(lineno);
6267 break;
6269 while (isspace((unsigned char)p[0]))
6270 p++;
6271 if (cmd->code == GOT_HISTEDIT_MESG) {
6272 if (hle == NULL || hle->logmsg != NULL) {
6273 err = got_error(GOT_ERR_HISTEDIT_CMD);
6274 break;
6276 if (p[0] == '\0') {
6277 err = histedit_edit_logmsg(hle, repo);
6278 if (err)
6279 break;
6280 } else {
6281 hle->logmsg = strdup(p);
6282 if (hle->logmsg == NULL) {
6283 err = got_error_from_errno("strdup");
6284 break;
6287 free(line);
6288 line = NULL;
6289 continue;
6290 } else {
6291 end = p;
6292 while (end[0] && !isspace((unsigned char)end[0]))
6293 end++;
6294 *end = '\0';
6296 err = got_object_resolve_id_str(&commit_id, repo, p);
6297 if (err) {
6298 /* override error code */
6299 err = histedit_syntax_error(lineno);
6300 break;
6303 hle = malloc(sizeof(*hle));
6304 if (hle == NULL) {
6305 err = got_error_from_errno("malloc");
6306 break;
6308 hle->cmd = cmd;
6309 hle->commit_id = commit_id;
6310 hle->logmsg = NULL;
6311 commit_id = NULL;
6312 free(line);
6313 line = NULL;
6314 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6317 free(line);
6318 free(commit_id);
6319 return err;
6322 static const struct got_error *
6323 histedit_check_script(struct got_histedit_list *histedit_cmds,
6324 struct got_object_id_queue *commits, struct got_repository *repo)
6326 const struct got_error *err = NULL;
6327 struct got_object_qid *qid;
6328 struct got_histedit_list_entry *hle;
6329 static char msg[92];
6330 char *id_str;
6332 if (TAILQ_EMPTY(histedit_cmds))
6333 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6334 "histedit script contains no commands");
6335 if (SIMPLEQ_EMPTY(commits))
6336 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6338 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6339 struct got_histedit_list_entry *hle2;
6340 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6341 if (hle == hle2)
6342 continue;
6343 if (got_object_id_cmp(hle->commit_id,
6344 hle2->commit_id) != 0)
6345 continue;
6346 err = got_object_id_str(&id_str, hle->commit_id);
6347 if (err)
6348 return err;
6349 snprintf(msg, sizeof(msg), "commit %s is listed "
6350 "more than once in histedit script", id_str);
6351 free(id_str);
6352 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6356 SIMPLEQ_FOREACH(qid, commits, entry) {
6357 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6358 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6359 break;
6361 if (hle == NULL) {
6362 err = got_object_id_str(&id_str, qid->id);
6363 if (err)
6364 return err;
6365 snprintf(msg, sizeof(msg),
6366 "commit %s missing from histedit script", id_str);
6367 free(id_str);
6368 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6372 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6373 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6374 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6375 "last commit in histedit script cannot be folded");
6377 return NULL;
6380 static const struct got_error *
6381 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6382 const char *path, struct got_object_id_queue *commits,
6383 struct got_repository *repo)
6385 const struct got_error *err = NULL;
6386 char *editor;
6387 FILE *f = NULL;
6389 err = get_editor(&editor);
6390 if (err)
6391 return err;
6393 if (spawn_editor(editor, path) == -1) {
6394 err = got_error_from_errno("failed spawning editor");
6395 goto done;
6398 f = fopen(path, "r");
6399 if (f == NULL) {
6400 err = got_error_from_errno("fopen");
6401 goto done;
6403 err = histedit_parse_list(histedit_cmds, f, repo);
6404 if (err)
6405 goto done;
6407 err = histedit_check_script(histedit_cmds, commits, repo);
6408 done:
6409 if (f && fclose(f) != 0 && err == NULL)
6410 err = got_error_from_errno("fclose");
6411 free(editor);
6412 return err;
6415 static const struct got_error *
6416 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6417 struct got_object_id_queue *, const char *, const char *,
6418 struct got_repository *);
6420 static const struct got_error *
6421 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6422 struct got_object_id_queue *commits, const char *branch_name,
6423 int edit_logmsg_only, struct got_repository *repo)
6425 const struct got_error *err;
6426 FILE *f = NULL;
6427 char *path = NULL;
6429 err = got_opentemp_named(&path, &f, "got-histedit");
6430 if (err)
6431 return err;
6433 err = write_cmd_list(f, branch_name, commits);
6434 if (err)
6435 goto done;
6437 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6438 if (err)
6439 goto done;
6441 if (edit_logmsg_only) {
6442 rewind(f);
6443 err = histedit_parse_list(histedit_cmds, f, repo);
6444 } else {
6445 if (fclose(f) != 0) {
6446 err = got_error_from_errno("fclose");
6447 goto done;
6449 f = NULL;
6450 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6451 if (err) {
6452 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6453 err->code != GOT_ERR_HISTEDIT_CMD)
6454 goto done;
6455 err = histedit_edit_list_retry(histedit_cmds, err,
6456 commits, path, branch_name, repo);
6459 done:
6460 if (f && fclose(f) != 0 && err == NULL)
6461 err = got_error_from_errno("fclose");
6462 if (path && unlink(path) != 0 && err == NULL)
6463 err = got_error_from_errno2("unlink", path);
6464 free(path);
6465 return err;
6468 static const struct got_error *
6469 histedit_save_list(struct got_histedit_list *histedit_cmds,
6470 struct got_worktree *worktree, struct got_repository *repo)
6472 const struct got_error *err = NULL;
6473 char *path = NULL;
6474 FILE *f = NULL;
6475 struct got_histedit_list_entry *hle;
6476 struct got_commit_object *commit = NULL;
6478 err = got_worktree_get_histedit_script_path(&path, worktree);
6479 if (err)
6480 return err;
6482 f = fopen(path, "w");
6483 if (f == NULL) {
6484 err = got_error_from_errno2("fopen", path);
6485 goto done;
6487 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6488 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6489 repo);
6490 if (err)
6491 break;
6493 if (hle->logmsg) {
6494 int n = fprintf(f, "%c %s\n",
6495 GOT_HISTEDIT_MESG, hle->logmsg);
6496 if (n < 0) {
6497 err = got_ferror(f, GOT_ERR_IO);
6498 break;
6502 done:
6503 if (f && fclose(f) != 0 && err == NULL)
6504 err = got_error_from_errno("fclose");
6505 free(path);
6506 if (commit)
6507 got_object_commit_close(commit);
6508 return err;
6511 void
6512 histedit_free_list(struct got_histedit_list *histedit_cmds)
6514 struct got_histedit_list_entry *hle;
6516 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6517 TAILQ_REMOVE(histedit_cmds, hle, entry);
6518 free(hle);
6522 static const struct got_error *
6523 histedit_load_list(struct got_histedit_list *histedit_cmds,
6524 const char *path, struct got_repository *repo)
6526 const struct got_error *err = NULL;
6527 FILE *f = NULL;
6529 f = fopen(path, "r");
6530 if (f == NULL) {
6531 err = got_error_from_errno2("fopen", path);
6532 goto done;
6535 err = histedit_parse_list(histedit_cmds, f, repo);
6536 done:
6537 if (f && fclose(f) != 0 && err == NULL)
6538 err = got_error_from_errno("fclose");
6539 return err;
6542 static const struct got_error *
6543 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6544 const struct got_error *edit_err, struct got_object_id_queue *commits,
6545 const char *path, const char *branch_name, struct got_repository *repo)
6547 const struct got_error *err = NULL, *prev_err = edit_err;
6548 int resp = ' ';
6550 while (resp != 'c' && resp != 'r' && resp != 'a') {
6551 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6552 "or (a)bort: ", getprogname(), prev_err->msg);
6553 resp = getchar();
6554 if (resp == '\n')
6555 resp = getchar();
6556 if (resp == 'c') {
6557 histedit_free_list(histedit_cmds);
6558 err = histedit_run_editor(histedit_cmds, path, commits,
6559 repo);
6560 if (err) {
6561 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6562 err->code != GOT_ERR_HISTEDIT_CMD)
6563 break;
6564 prev_err = err;
6565 resp = ' ';
6566 continue;
6568 break;
6569 } else if (resp == 'r') {
6570 histedit_free_list(histedit_cmds);
6571 err = histedit_edit_script(histedit_cmds,
6572 commits, branch_name, 0, repo);
6573 if (err) {
6574 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6575 err->code != GOT_ERR_HISTEDIT_CMD)
6576 break;
6577 prev_err = err;
6578 resp = ' ';
6579 continue;
6581 break;
6582 } else if (resp == 'a') {
6583 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6584 break;
6585 } else
6586 printf("invalid response '%c'\n", resp);
6589 return err;
6592 static const struct got_error *
6593 histedit_complete(struct got_worktree *worktree,
6594 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6595 struct got_reference *branch, struct got_repository *repo)
6597 printf("Switching work tree to %s\n",
6598 got_ref_get_symref_target(branch));
6599 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6600 branch, repo);
6603 static const struct got_error *
6604 show_histedit_progress(struct got_commit_object *commit,
6605 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6607 const struct got_error *err;
6608 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6610 err = got_object_id_str(&old_id_str, hle->commit_id);
6611 if (err)
6612 goto done;
6614 if (new_id) {
6615 err = got_object_id_str(&new_id_str, new_id);
6616 if (err)
6617 goto done;
6620 old_id_str[12] = '\0';
6621 if (new_id_str)
6622 new_id_str[12] = '\0';
6624 if (hle->logmsg) {
6625 logmsg = strdup(hle->logmsg);
6626 if (logmsg == NULL) {
6627 err = got_error_from_errno("strdup");
6628 goto done;
6630 trim_logmsg(logmsg, 42);
6631 } else {
6632 err = get_short_logmsg(&logmsg, 42, commit);
6633 if (err)
6634 goto done;
6637 switch (hle->cmd->code) {
6638 case GOT_HISTEDIT_PICK:
6639 case GOT_HISTEDIT_EDIT:
6640 printf("%s -> %s: %s\n", old_id_str,
6641 new_id_str ? new_id_str : "no-op change", logmsg);
6642 break;
6643 case GOT_HISTEDIT_DROP:
6644 case GOT_HISTEDIT_FOLD:
6645 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6646 logmsg);
6647 break;
6648 default:
6649 break;
6651 done:
6652 free(old_id_str);
6653 free(new_id_str);
6654 return err;
6657 static const struct got_error *
6658 histedit_commit(struct got_pathlist_head *merged_paths,
6659 struct got_worktree *worktree, struct got_fileindex *fileindex,
6660 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6661 struct got_repository *repo)
6663 const struct got_error *err;
6664 struct got_commit_object *commit;
6665 struct got_object_id *new_commit_id;
6667 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6668 && hle->logmsg == NULL) {
6669 err = histedit_edit_logmsg(hle, repo);
6670 if (err)
6671 return err;
6674 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6675 if (err)
6676 return err;
6678 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6679 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6680 hle->logmsg, repo);
6681 if (err) {
6682 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6683 goto done;
6684 err = show_histedit_progress(commit, hle, NULL);
6685 } else {
6686 err = show_histedit_progress(commit, hle, new_commit_id);
6687 free(new_commit_id);
6689 done:
6690 got_object_commit_close(commit);
6691 return err;
6694 static const struct got_error *
6695 histedit_skip_commit(struct got_histedit_list_entry *hle,
6696 struct got_worktree *worktree, struct got_repository *repo)
6698 const struct got_error *error;
6699 struct got_commit_object *commit;
6701 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6702 repo);
6703 if (error)
6704 return error;
6706 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6707 if (error)
6708 return error;
6710 error = show_histedit_progress(commit, hle, NULL);
6711 got_object_commit_close(commit);
6712 return error;
6715 static const struct got_error *
6716 check_local_changes(void *arg, unsigned char status,
6717 unsigned char staged_status, const char *path,
6718 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6719 struct got_object_id *commit_id, int dirfd, const char *de_name)
6721 int *have_local_changes = arg;
6723 switch (status) {
6724 case GOT_STATUS_ADD:
6725 case GOT_STATUS_DELETE:
6726 case GOT_STATUS_MODIFY:
6727 case GOT_STATUS_CONFLICT:
6728 *have_local_changes = 1;
6729 return got_error(GOT_ERR_CANCELLED);
6730 default:
6731 break;
6734 switch (staged_status) {
6735 case GOT_STATUS_ADD:
6736 case GOT_STATUS_DELETE:
6737 case GOT_STATUS_MODIFY:
6738 *have_local_changes = 1;
6739 return got_error(GOT_ERR_CANCELLED);
6740 default:
6741 break;
6744 return NULL;
6747 static const struct got_error *
6748 cmd_histedit(int argc, char *argv[])
6750 const struct got_error *error = NULL;
6751 struct got_worktree *worktree = NULL;
6752 struct got_fileindex *fileindex = NULL;
6753 struct got_repository *repo = NULL;
6754 char *cwd = NULL;
6755 struct got_reference *branch = NULL;
6756 struct got_reference *tmp_branch = NULL;
6757 struct got_object_id *resume_commit_id = NULL;
6758 struct got_object_id *base_commit_id = NULL;
6759 struct got_object_id *head_commit_id = NULL;
6760 struct got_commit_object *commit = NULL;
6761 int ch, rebase_in_progress = 0, did_something;
6762 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6763 int edit_logmsg_only = 0;
6764 const char *edit_script_path = NULL;
6765 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6766 struct got_object_id_queue commits;
6767 struct got_pathlist_head merged_paths;
6768 const struct got_object_id_queue *parent_ids;
6769 struct got_object_qid *pid;
6770 struct got_histedit_list histedit_cmds;
6771 struct got_histedit_list_entry *hle;
6773 SIMPLEQ_INIT(&commits);
6774 TAILQ_INIT(&histedit_cmds);
6775 TAILQ_INIT(&merged_paths);
6777 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6778 switch (ch) {
6779 case 'a':
6780 abort_edit = 1;
6781 break;
6782 case 'c':
6783 continue_edit = 1;
6784 break;
6785 case 'F':
6786 edit_script_path = optarg;
6787 break;
6788 case 'm':
6789 edit_logmsg_only = 1;
6790 break;
6791 default:
6792 usage_histedit();
6793 /* NOTREACHED */
6797 argc -= optind;
6798 argv += optind;
6800 #ifndef PROFILE
6801 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6802 "unveil", NULL) == -1)
6803 err(1, "pledge");
6804 #endif
6805 if (abort_edit && continue_edit)
6806 errx(1, "histedit's -a and -c options are mutually exclusive");
6807 if (edit_script_path && edit_logmsg_only)
6808 errx(1, "histedit's -F and -m options are mutually exclusive");
6809 if (abort_edit && edit_logmsg_only)
6810 errx(1, "histedit's -a and -m options are mutually exclusive");
6811 if (continue_edit && edit_logmsg_only)
6812 errx(1, "histedit's -c and -m options are mutually exclusive");
6813 if (argc != 0)
6814 usage_histedit();
6817 * This command cannot apply unveil(2) in all cases because the
6818 * user may choose to run an editor to edit the histedit script
6819 * and to edit individual commit log messages.
6820 * unveil(2) traverses exec(2); if an editor is used we have to
6821 * apply unveil after edit script and log messages have been written.
6822 * XXX TODO: Make use of unveil(2) where possible.
6825 cwd = getcwd(NULL, 0);
6826 if (cwd == NULL) {
6827 error = got_error_from_errno("getcwd");
6828 goto done;
6830 error = got_worktree_open(&worktree, cwd);
6831 if (error)
6832 goto done;
6834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6835 NULL);
6836 if (error != NULL)
6837 goto done;
6839 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6840 if (error)
6841 goto done;
6842 if (rebase_in_progress) {
6843 error = got_error(GOT_ERR_REBASING);
6844 goto done;
6847 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6848 if (error)
6849 goto done;
6851 if (edit_in_progress && edit_logmsg_only) {
6852 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6853 "histedit operation is in progress in this "
6854 "work tree and must be continued or aborted "
6855 "before the -m option can be used");
6856 goto done;
6859 if (edit_in_progress && abort_edit) {
6860 error = got_worktree_histedit_continue(&resume_commit_id,
6861 &tmp_branch, &branch, &base_commit_id, &fileindex,
6862 worktree, repo);
6863 if (error)
6864 goto done;
6865 printf("Switching work tree to %s\n",
6866 got_ref_get_symref_target(branch));
6867 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6868 branch, base_commit_id, update_progress, &did_something);
6869 if (error)
6870 goto done;
6871 printf("Histedit of %s aborted\n",
6872 got_ref_get_symref_target(branch));
6873 goto done; /* nothing else to do */
6874 } else if (abort_edit) {
6875 error = got_error(GOT_ERR_NOT_HISTEDIT);
6876 goto done;
6879 if (continue_edit) {
6880 char *path;
6882 if (!edit_in_progress) {
6883 error = got_error(GOT_ERR_NOT_HISTEDIT);
6884 goto done;
6887 error = got_worktree_get_histedit_script_path(&path, worktree);
6888 if (error)
6889 goto done;
6891 error = histedit_load_list(&histedit_cmds, path, repo);
6892 free(path);
6893 if (error)
6894 goto done;
6896 error = got_worktree_histedit_continue(&resume_commit_id,
6897 &tmp_branch, &branch, &base_commit_id, &fileindex,
6898 worktree, repo);
6899 if (error)
6900 goto done;
6902 error = got_ref_resolve(&head_commit_id, repo, branch);
6903 if (error)
6904 goto done;
6906 error = got_object_open_as_commit(&commit, repo,
6907 head_commit_id);
6908 if (error)
6909 goto done;
6910 parent_ids = got_object_commit_get_parent_ids(commit);
6911 pid = SIMPLEQ_FIRST(parent_ids);
6912 if (pid == NULL) {
6913 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6914 goto done;
6916 error = collect_commits(&commits, head_commit_id, pid->id,
6917 base_commit_id, got_worktree_get_path_prefix(worktree),
6918 GOT_ERR_HISTEDIT_PATH, repo);
6919 got_object_commit_close(commit);
6920 commit = NULL;
6921 if (error)
6922 goto done;
6923 } else {
6924 if (edit_in_progress) {
6925 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6926 goto done;
6929 error = got_ref_open(&branch, repo,
6930 got_worktree_get_head_ref_name(worktree), 0);
6931 if (error != NULL)
6932 goto done;
6934 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6935 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6936 "will not edit commit history of a branch outside "
6937 "the \"refs/heads/\" reference namespace");
6938 goto done;
6941 error = got_ref_resolve(&head_commit_id, repo, branch);
6942 got_ref_close(branch);
6943 branch = NULL;
6944 if (error)
6945 goto done;
6947 error = got_object_open_as_commit(&commit, repo,
6948 head_commit_id);
6949 if (error)
6950 goto done;
6951 parent_ids = got_object_commit_get_parent_ids(commit);
6952 pid = SIMPLEQ_FIRST(parent_ids);
6953 if (pid == NULL) {
6954 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6955 goto done;
6957 error = collect_commits(&commits, head_commit_id, pid->id,
6958 got_worktree_get_base_commit_id(worktree),
6959 got_worktree_get_path_prefix(worktree),
6960 GOT_ERR_HISTEDIT_PATH, repo);
6961 got_object_commit_close(commit);
6962 commit = NULL;
6963 if (error)
6964 goto done;
6966 if (SIMPLEQ_EMPTY(&commits)) {
6967 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6968 goto done;
6971 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6972 &base_commit_id, &fileindex, worktree, repo);
6973 if (error)
6974 goto done;
6976 if (edit_script_path) {
6977 error = histedit_load_list(&histedit_cmds,
6978 edit_script_path, repo);
6979 if (error) {
6980 got_worktree_histedit_abort(worktree, fileindex,
6981 repo, branch, base_commit_id,
6982 update_progress, &did_something);
6983 goto done;
6985 } else {
6986 const char *branch_name;
6987 branch_name = got_ref_get_symref_target(branch);
6988 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6989 branch_name += 11;
6990 error = histedit_edit_script(&histedit_cmds, &commits,
6991 branch_name, edit_logmsg_only, repo);
6992 if (error) {
6993 got_worktree_histedit_abort(worktree, fileindex,
6994 repo, branch, base_commit_id,
6995 update_progress, &did_something);
6996 goto done;
7001 error = histedit_save_list(&histedit_cmds, worktree,
7002 repo);
7003 if (error) {
7004 got_worktree_histedit_abort(worktree, fileindex,
7005 repo, branch, base_commit_id,
7006 update_progress, &did_something);
7007 goto done;
7012 error = histedit_check_script(&histedit_cmds, &commits, repo);
7013 if (error)
7014 goto done;
7016 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7017 if (resume_commit_id) {
7018 if (got_object_id_cmp(hle->commit_id,
7019 resume_commit_id) != 0)
7020 continue;
7022 resume_commit_id = NULL;
7023 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7024 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7025 error = histedit_skip_commit(hle, worktree,
7026 repo);
7027 if (error)
7028 goto done;
7029 } else {
7030 struct got_pathlist_head paths;
7031 int have_changes = 0;
7033 TAILQ_INIT(&paths);
7034 error = got_pathlist_append(&paths, "", NULL);
7035 if (error)
7036 goto done;
7037 error = got_worktree_status(worktree, &paths,
7038 repo, check_local_changes, &have_changes,
7039 check_cancelled, NULL);
7040 got_pathlist_free(&paths);
7041 if (error) {
7042 if (error->code != GOT_ERR_CANCELLED)
7043 goto done;
7044 if (sigint_received || sigpipe_received)
7045 goto done;
7047 if (have_changes) {
7048 error = histedit_commit(NULL, worktree,
7049 fileindex, tmp_branch, hle, repo);
7050 if (error)
7051 goto done;
7052 } else {
7053 error = got_object_open_as_commit(
7054 &commit, repo, hle->commit_id);
7055 if (error)
7056 goto done;
7057 error = show_histedit_progress(commit,
7058 hle, NULL);
7059 got_object_commit_close(commit);
7060 commit = NULL;
7061 if (error)
7062 goto done;
7065 continue;
7068 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7069 error = histedit_skip_commit(hle, worktree, repo);
7070 if (error)
7071 goto done;
7072 continue;
7075 error = got_object_open_as_commit(&commit, repo,
7076 hle->commit_id);
7077 if (error)
7078 goto done;
7079 parent_ids = got_object_commit_get_parent_ids(commit);
7080 pid = SIMPLEQ_FIRST(parent_ids);
7082 error = got_worktree_histedit_merge_files(&merged_paths,
7083 worktree, fileindex, pid->id, hle->commit_id, repo,
7084 rebase_progress, &rebase_status, check_cancelled, NULL);
7085 if (error)
7086 goto done;
7087 got_object_commit_close(commit);
7088 commit = NULL;
7090 if (rebase_status == GOT_STATUS_CONFLICT) {
7091 error = show_rebase_merge_conflict(hle->commit_id,
7092 repo);
7093 if (error)
7094 goto done;
7095 got_worktree_rebase_pathlist_free(&merged_paths);
7096 break;
7099 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7100 char *id_str;
7101 error = got_object_id_str(&id_str, hle->commit_id);
7102 if (error)
7103 goto done;
7104 printf("Stopping histedit for amending commit %s\n",
7105 id_str);
7106 free(id_str);
7107 got_worktree_rebase_pathlist_free(&merged_paths);
7108 error = got_worktree_histedit_postpone(worktree,
7109 fileindex);
7110 goto done;
7113 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7114 error = histedit_skip_commit(hle, worktree, repo);
7115 if (error)
7116 goto done;
7117 continue;
7120 error = histedit_commit(&merged_paths, worktree, fileindex,
7121 tmp_branch, hle, repo);
7122 got_worktree_rebase_pathlist_free(&merged_paths);
7123 if (error)
7124 goto done;
7127 if (rebase_status == GOT_STATUS_CONFLICT) {
7128 error = got_worktree_histedit_postpone(worktree, fileindex);
7129 if (error)
7130 goto done;
7131 error = got_error_msg(GOT_ERR_CONFLICTS,
7132 "conflicts must be resolved before histedit can continue");
7133 } else
7134 error = histedit_complete(worktree, fileindex, tmp_branch,
7135 branch, repo);
7136 done:
7137 got_object_id_queue_free(&commits);
7138 histedit_free_list(&histedit_cmds);
7139 free(head_commit_id);
7140 free(base_commit_id);
7141 free(resume_commit_id);
7142 if (commit)
7143 got_object_commit_close(commit);
7144 if (branch)
7145 got_ref_close(branch);
7146 if (tmp_branch)
7147 got_ref_close(tmp_branch);
7148 if (worktree)
7149 got_worktree_close(worktree);
7150 if (repo)
7151 got_repo_close(repo);
7152 return error;
7155 __dead static void
7156 usage_integrate(void)
7158 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7159 exit(1);
7162 static const struct got_error *
7163 cmd_integrate(int argc, char *argv[])
7165 const struct got_error *error = NULL;
7166 struct got_repository *repo = NULL;
7167 struct got_worktree *worktree = NULL;
7168 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7169 const char *branch_arg = NULL;
7170 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7171 struct got_fileindex *fileindex = NULL;
7172 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7173 int ch, did_something = 0;
7175 while ((ch = getopt(argc, argv, "")) != -1) {
7176 switch (ch) {
7177 default:
7178 usage_integrate();
7179 /* NOTREACHED */
7183 argc -= optind;
7184 argv += optind;
7186 if (argc != 1)
7187 usage_integrate();
7188 branch_arg = argv[0];
7190 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7191 "unveil", NULL) == -1)
7192 err(1, "pledge");
7194 cwd = getcwd(NULL, 0);
7195 if (cwd == NULL) {
7196 error = got_error_from_errno("getcwd");
7197 goto done;
7200 error = got_worktree_open(&worktree, cwd);
7201 if (error)
7202 goto done;
7204 error = check_rebase_or_histedit_in_progress(worktree);
7205 if (error)
7206 goto done;
7208 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7209 NULL);
7210 if (error != NULL)
7211 goto done;
7213 error = apply_unveil(got_repo_get_path(repo), 0,
7214 got_worktree_get_root_path(worktree));
7215 if (error)
7216 goto done;
7218 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7219 error = got_error_from_errno("asprintf");
7220 goto done;
7223 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7224 &base_branch_ref, worktree, refname, repo);
7225 if (error)
7226 goto done;
7228 refname = strdup(got_ref_get_name(branch_ref));
7229 if (refname == NULL) {
7230 error = got_error_from_errno("strdup");
7231 got_worktree_integrate_abort(worktree, fileindex, repo,
7232 branch_ref, base_branch_ref);
7233 goto done;
7235 base_refname = strdup(got_ref_get_name(base_branch_ref));
7236 if (base_refname == NULL) {
7237 error = got_error_from_errno("strdup");
7238 got_worktree_integrate_abort(worktree, fileindex, repo,
7239 branch_ref, base_branch_ref);
7240 goto done;
7243 error = got_ref_resolve(&commit_id, repo, branch_ref);
7244 if (error)
7245 goto done;
7247 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7248 if (error)
7249 goto done;
7251 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7252 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7253 "specified branch has already been integrated");
7254 got_worktree_integrate_abort(worktree, fileindex, repo,
7255 branch_ref, base_branch_ref);
7256 goto done;
7259 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7260 if (error) {
7261 if (error->code == GOT_ERR_ANCESTRY)
7262 error = got_error(GOT_ERR_REBASE_REQUIRED);
7263 got_worktree_integrate_abort(worktree, fileindex, repo,
7264 branch_ref, base_branch_ref);
7265 goto done;
7268 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7269 branch_ref, base_branch_ref, update_progress, &did_something,
7270 check_cancelled, NULL);
7271 if (error)
7272 goto done;
7274 printf("Integrated %s into %s\n", refname, base_refname);
7275 done:
7276 if (repo)
7277 got_repo_close(repo);
7278 if (worktree)
7279 got_worktree_close(worktree);
7280 free(cwd);
7281 free(base_commit_id);
7282 free(commit_id);
7283 free(refname);
7284 free(base_refname);
7285 return error;
7288 __dead static void
7289 usage_stage(void)
7291 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7292 "[file-path ...]\n",
7293 getprogname());
7294 exit(1);
7297 static const struct got_error *
7298 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7299 const char *path, struct got_object_id *blob_id,
7300 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7301 int dirfd, const char *de_name)
7303 const struct got_error *err = NULL;
7304 char *id_str = NULL;
7306 if (staged_status != GOT_STATUS_ADD &&
7307 staged_status != GOT_STATUS_MODIFY &&
7308 staged_status != GOT_STATUS_DELETE)
7309 return NULL;
7311 if (staged_status == GOT_STATUS_ADD ||
7312 staged_status == GOT_STATUS_MODIFY)
7313 err = got_object_id_str(&id_str, staged_blob_id);
7314 else
7315 err = got_object_id_str(&id_str, blob_id);
7316 if (err)
7317 return err;
7319 printf("%s %c %s\n", id_str, staged_status, path);
7320 free(id_str);
7321 return NULL;
7324 static const struct got_error *
7325 cmd_stage(int argc, char *argv[])
7327 const struct got_error *error = NULL;
7328 struct got_repository *repo = NULL;
7329 struct got_worktree *worktree = NULL;
7330 char *cwd = NULL;
7331 struct got_pathlist_head paths;
7332 struct got_pathlist_entry *pe;
7333 int ch, list_stage = 0, pflag = 0;
7334 FILE *patch_script_file = NULL;
7335 const char *patch_script_path = NULL;
7336 struct choose_patch_arg cpa;
7338 TAILQ_INIT(&paths);
7340 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7341 switch (ch) {
7342 case 'l':
7343 list_stage = 1;
7344 break;
7345 case 'p':
7346 pflag = 1;
7347 break;
7348 case 'F':
7349 patch_script_path = optarg;
7350 break;
7351 default:
7352 usage_stage();
7353 /* NOTREACHED */
7357 argc -= optind;
7358 argv += optind;
7360 #ifndef PROFILE
7361 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7362 "unveil", NULL) == -1)
7363 err(1, "pledge");
7364 #endif
7365 if (list_stage && (pflag || patch_script_path))
7366 errx(1, "-l option cannot be used with other options");
7367 if (patch_script_path && !pflag)
7368 errx(1, "-F option can only be used together with -p option");
7370 cwd = getcwd(NULL, 0);
7371 if (cwd == NULL) {
7372 error = got_error_from_errno("getcwd");
7373 goto done;
7376 error = got_worktree_open(&worktree, cwd);
7377 if (error)
7378 goto done;
7380 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7381 NULL);
7382 if (error != NULL)
7383 goto done;
7385 if (patch_script_path) {
7386 patch_script_file = fopen(patch_script_path, "r");
7387 if (patch_script_file == NULL) {
7388 error = got_error_from_errno2("fopen",
7389 patch_script_path);
7390 goto done;
7393 error = apply_unveil(got_repo_get_path(repo), 0,
7394 got_worktree_get_root_path(worktree));
7395 if (error)
7396 goto done;
7398 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7399 if (error)
7400 goto done;
7402 if (list_stage)
7403 error = got_worktree_status(worktree, &paths, repo,
7404 print_stage, NULL, check_cancelled, NULL);
7405 else {
7406 cpa.patch_script_file = patch_script_file;
7407 cpa.action = "stage";
7408 error = got_worktree_stage(worktree, &paths,
7409 pflag ? NULL : print_status, NULL,
7410 pflag ? choose_patch : NULL, &cpa, repo);
7412 done:
7413 if (patch_script_file && fclose(patch_script_file) == EOF &&
7414 error == NULL)
7415 error = got_error_from_errno2("fclose", patch_script_path);
7416 if (repo)
7417 got_repo_close(repo);
7418 if (worktree)
7419 got_worktree_close(worktree);
7420 TAILQ_FOREACH(pe, &paths, entry)
7421 free((char *)pe->path);
7422 got_pathlist_free(&paths);
7423 free(cwd);
7424 return error;
7427 __dead static void
7428 usage_unstage(void)
7430 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7431 "[file-path ...]\n",
7432 getprogname());
7433 exit(1);
7437 static const struct got_error *
7438 cmd_unstage(int argc, char *argv[])
7440 const struct got_error *error = NULL;
7441 struct got_repository *repo = NULL;
7442 struct got_worktree *worktree = NULL;
7443 char *cwd = NULL;
7444 struct got_pathlist_head paths;
7445 struct got_pathlist_entry *pe;
7446 int ch, did_something = 0, pflag = 0;
7447 FILE *patch_script_file = NULL;
7448 const char *patch_script_path = NULL;
7449 struct choose_patch_arg cpa;
7451 TAILQ_INIT(&paths);
7453 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7454 switch (ch) {
7455 case 'p':
7456 pflag = 1;
7457 break;
7458 case 'F':
7459 patch_script_path = optarg;
7460 break;
7461 default:
7462 usage_unstage();
7463 /* NOTREACHED */
7467 argc -= optind;
7468 argv += optind;
7470 #ifndef PROFILE
7471 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7472 "unveil", NULL) == -1)
7473 err(1, "pledge");
7474 #endif
7475 if (patch_script_path && !pflag)
7476 errx(1, "-F option can only be used together with -p option");
7478 cwd = getcwd(NULL, 0);
7479 if (cwd == NULL) {
7480 error = got_error_from_errno("getcwd");
7481 goto done;
7484 error = got_worktree_open(&worktree, cwd);
7485 if (error)
7486 goto done;
7488 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7489 NULL);
7490 if (error != NULL)
7491 goto done;
7493 if (patch_script_path) {
7494 patch_script_file = fopen(patch_script_path, "r");
7495 if (patch_script_file == NULL) {
7496 error = got_error_from_errno2("fopen",
7497 patch_script_path);
7498 goto done;
7502 error = apply_unveil(got_repo_get_path(repo), 0,
7503 got_worktree_get_root_path(worktree));
7504 if (error)
7505 goto done;
7507 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7508 if (error)
7509 goto done;
7511 cpa.patch_script_file = patch_script_file;
7512 cpa.action = "unstage";
7513 error = got_worktree_unstage(worktree, &paths, update_progress,
7514 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7515 done:
7516 if (patch_script_file && fclose(patch_script_file) == EOF &&
7517 error == NULL)
7518 error = got_error_from_errno2("fclose", patch_script_path);
7519 if (repo)
7520 got_repo_close(repo);
7521 if (worktree)
7522 got_worktree_close(worktree);
7523 TAILQ_FOREACH(pe, &paths, entry)
7524 free((char *)pe->path);
7525 got_pathlist_free(&paths);
7526 free(cwd);
7527 return error;
7530 __dead static void
7531 usage_cat(void)
7533 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7534 "arg1 [arg2 ...]\n", getprogname());
7535 exit(1);
7538 static const struct got_error *
7539 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7541 const struct got_error *err;
7542 struct got_blob_object *blob;
7544 err = got_object_open_as_blob(&blob, repo, id, 8192);
7545 if (err)
7546 return err;
7548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7549 got_object_blob_close(blob);
7550 return err;
7553 static const struct got_error *
7554 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7556 const struct got_error *err;
7557 struct got_tree_object *tree;
7558 int nentries, i;
7560 err = got_object_open_as_tree(&tree, repo, id);
7561 if (err)
7562 return err;
7564 nentries = got_object_tree_get_nentries(tree);
7565 for (i = 0; i < nentries; i++) {
7566 struct got_tree_entry *te;
7567 char *id_str;
7568 if (sigint_received || sigpipe_received)
7569 break;
7570 te = got_object_tree_get_entry(tree, i);
7571 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7572 if (err)
7573 break;
7574 fprintf(outfile, "%s %.7o %s\n", id_str,
7575 got_tree_entry_get_mode(te),
7576 got_tree_entry_get_name(te));
7577 free(id_str);
7580 got_object_tree_close(tree);
7581 return err;
7584 static const struct got_error *
7585 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7587 const struct got_error *err;
7588 struct got_commit_object *commit;
7589 const struct got_object_id_queue *parent_ids;
7590 struct got_object_qid *pid;
7591 char *id_str = NULL;
7592 const char *logmsg = NULL;
7594 err = got_object_open_as_commit(&commit, repo, id);
7595 if (err)
7596 return err;
7598 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7599 if (err)
7600 goto done;
7602 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7603 parent_ids = got_object_commit_get_parent_ids(commit);
7604 fprintf(outfile, "numparents %d\n",
7605 got_object_commit_get_nparents(commit));
7606 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7607 char *pid_str;
7608 err = got_object_id_str(&pid_str, pid->id);
7609 if (err)
7610 goto done;
7611 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7612 free(pid_str);
7614 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7615 got_object_commit_get_author(commit),
7616 got_object_commit_get_author_time(commit));
7618 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7619 got_object_commit_get_author(commit),
7620 got_object_commit_get_committer_time(commit));
7622 logmsg = got_object_commit_get_logmsg_raw(commit);
7623 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7624 fprintf(outfile, "%s", logmsg);
7625 done:
7626 free(id_str);
7627 got_object_commit_close(commit);
7628 return err;
7631 static const struct got_error *
7632 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7634 const struct got_error *err;
7635 struct got_tag_object *tag;
7636 char *id_str = NULL;
7637 const char *tagmsg = NULL;
7639 err = got_object_open_as_tag(&tag, repo, id);
7640 if (err)
7641 return err;
7643 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7644 if (err)
7645 goto done;
7647 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7649 switch (got_object_tag_get_object_type(tag)) {
7650 case GOT_OBJ_TYPE_BLOB:
7651 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7652 GOT_OBJ_LABEL_BLOB);
7653 break;
7654 case GOT_OBJ_TYPE_TREE:
7655 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7656 GOT_OBJ_LABEL_TREE);
7657 break;
7658 case GOT_OBJ_TYPE_COMMIT:
7659 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7660 GOT_OBJ_LABEL_COMMIT);
7661 break;
7662 case GOT_OBJ_TYPE_TAG:
7663 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7664 GOT_OBJ_LABEL_TAG);
7665 break;
7666 default:
7667 break;
7670 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7671 got_object_tag_get_name(tag));
7673 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7674 got_object_tag_get_tagger(tag),
7675 got_object_tag_get_tagger_time(tag));
7677 tagmsg = got_object_tag_get_message(tag);
7678 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7679 fprintf(outfile, "%s", tagmsg);
7680 done:
7681 free(id_str);
7682 got_object_tag_close(tag);
7683 return err;
7686 static const struct got_error *
7687 cmd_cat(int argc, char *argv[])
7689 const struct got_error *error;
7690 struct got_repository *repo = NULL;
7691 struct got_worktree *worktree = NULL;
7692 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7693 const char *commit_id_str = NULL;
7694 struct got_object_id *id = NULL, *commit_id = NULL;
7695 int ch, obj_type, i, force_path = 0;
7697 #ifndef PROFILE
7698 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7699 NULL) == -1)
7700 err(1, "pledge");
7701 #endif
7703 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7704 switch (ch) {
7705 case 'c':
7706 commit_id_str = optarg;
7707 break;
7708 case 'r':
7709 repo_path = realpath(optarg, NULL);
7710 if (repo_path == NULL)
7711 return got_error_from_errno2("realpath",
7712 optarg);
7713 got_path_strip_trailing_slashes(repo_path);
7714 break;
7715 case 'P':
7716 force_path = 1;
7717 break;
7718 default:
7719 usage_cat();
7720 /* NOTREACHED */
7724 argc -= optind;
7725 argv += optind;
7727 cwd = getcwd(NULL, 0);
7728 if (cwd == NULL) {
7729 error = got_error_from_errno("getcwd");
7730 goto done;
7732 error = got_worktree_open(&worktree, cwd);
7733 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7734 goto done;
7735 if (worktree) {
7736 if (repo_path == NULL) {
7737 repo_path = strdup(
7738 got_worktree_get_repo_path(worktree));
7739 if (repo_path == NULL) {
7740 error = got_error_from_errno("strdup");
7741 goto done;
7746 if (repo_path == NULL) {
7747 repo_path = getcwd(NULL, 0);
7748 if (repo_path == NULL)
7749 return got_error_from_errno("getcwd");
7752 error = got_repo_open(&repo, repo_path, NULL);
7753 free(repo_path);
7754 if (error != NULL)
7755 goto done;
7757 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7758 if (error)
7759 goto done;
7761 if (commit_id_str == NULL)
7762 commit_id_str = GOT_REF_HEAD;
7763 error = got_repo_match_object_id(&commit_id, NULL,
7764 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7765 if (error)
7766 goto done;
7768 for (i = 0; i < argc; i++) {
7769 if (force_path) {
7770 error = got_object_id_by_path(&id, repo, commit_id,
7771 argv[i]);
7772 if (error)
7773 break;
7774 } else {
7775 error = got_repo_match_object_id(&id, &label, argv[i],
7776 GOT_OBJ_TYPE_ANY, 0, repo);
7777 if (error) {
7778 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7779 error->code != GOT_ERR_NOT_REF)
7780 break;
7781 error = got_object_id_by_path(&id, repo,
7782 commit_id, argv[i]);
7783 if (error)
7784 break;
7788 error = got_object_get_type(&obj_type, repo, id);
7789 if (error)
7790 break;
7792 switch (obj_type) {
7793 case GOT_OBJ_TYPE_BLOB:
7794 error = cat_blob(id, repo, stdout);
7795 break;
7796 case GOT_OBJ_TYPE_TREE:
7797 error = cat_tree(id, repo, stdout);
7798 break;
7799 case GOT_OBJ_TYPE_COMMIT:
7800 error = cat_commit(id, repo, stdout);
7801 break;
7802 case GOT_OBJ_TYPE_TAG:
7803 error = cat_tag(id, repo, stdout);
7804 break;
7805 default:
7806 error = got_error(GOT_ERR_OBJ_TYPE);
7807 break;
7809 if (error)
7810 break;
7811 free(label);
7812 label = NULL;
7813 free(id);
7814 id = NULL;
7816 done:
7817 free(label);
7818 free(id);
7819 free(commit_id);
7820 if (worktree)
7821 got_worktree_close(worktree);
7822 if (repo) {
7823 const struct got_error *repo_error;
7824 repo_error = got_repo_close(repo);
7825 if (error == NULL)
7826 error = repo_error;
7828 return error;