Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
41 #include <util.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
65 static void
66 catch_sigint(int signo)
67 {
68 sigint_received = 1;
69 }
71 static void
72 catch_sigpipe(int signo)
73 {
74 sigpipe_received = 1;
75 }
78 struct got_cmd {
79 const char *cmd_name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
83 };
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_checkout(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_update(void);
91 __dead static void usage_log(void);
92 __dead static void usage_diff(void);
93 __dead static void usage_blame(void);
94 __dead static void usage_tree(void);
95 __dead static void usage_status(void);
96 __dead static void usage_ref(void);
97 __dead static void usage_branch(void);
98 __dead static void usage_tag(void);
99 __dead static void usage_add(void);
100 __dead static void usage_remove(void);
101 __dead static void usage_revert(void);
102 __dead static void usage_commit(void);
103 __dead static void usage_cherrypick(void);
104 __dead static void usage_backout(void);
105 __dead static void usage_rebase(void);
106 __dead static void usage_histedit(void);
107 __dead static void usage_integrate(void);
108 __dead static void usage_stage(void);
109 __dead static void usage_unstage(void);
110 __dead static void usage_cat(void);
112 static const struct got_error* cmd_init(int, char *[]);
113 static const struct got_error* cmd_import(int, char *[]);
114 static const struct got_error* cmd_clone(int, char *[]);
115 static const struct got_error* cmd_checkout(int, char *[]);
116 static const struct got_error* cmd_update(int, char *[]);
117 static const struct got_error* cmd_log(int, char *[]);
118 static const struct got_error* cmd_diff(int, char *[]);
119 static const struct got_error* cmd_blame(int, char *[]);
120 static const struct got_error* cmd_tree(int, char *[]);
121 static const struct got_error* cmd_status(int, char *[]);
122 static const struct got_error* cmd_ref(int, char *[]);
123 static const struct got_error* cmd_branch(int, char *[]);
124 static const struct got_error* cmd_tag(int, char *[]);
125 static const struct got_error* cmd_add(int, char *[]);
126 static const struct got_error* cmd_remove(int, char *[]);
127 static const struct got_error* cmd_revert(int, char *[]);
128 static const struct got_error* cmd_commit(int, char *[]);
129 static const struct got_error* cmd_cherrypick(int, char *[]);
130 static const struct got_error* cmd_backout(int, char *[]);
131 static const struct got_error* cmd_rebase(int, char *[]);
132 static const struct got_error* cmd_histedit(int, char *[]);
133 static const struct got_error* cmd_integrate(int, char *[]);
134 static const struct got_error* cmd_stage(int, char *[]);
135 static const struct got_error* cmd_unstage(int, char *[]);
136 static const struct got_error* cmd_cat(int, char *[]);
138 static struct got_cmd got_commands[] = {
139 { "init", cmd_init, usage_init, "in" },
140 { "import", cmd_import, usage_import, "im" },
141 { "checkout", cmd_checkout, usage_checkout, "co" },
142 { "clone", cmd_clone, usage_clone, "cl" },
143 { "update", cmd_update, usage_update, "up" },
144 { "log", cmd_log, usage_log, "" },
145 { "diff", cmd_diff, usage_diff, "di" },
146 { "blame", cmd_blame, usage_blame, "bl" },
147 { "tree", cmd_tree, usage_tree, "tr" },
148 { "status", cmd_status, usage_status, "st" },
149 { "ref", cmd_ref, usage_ref, "" },
150 { "branch", cmd_branch, usage_branch, "br" },
151 { "tag", cmd_tag, usage_tag, "" },
152 { "add", cmd_add, usage_add, "" },
153 { "remove", cmd_remove, usage_remove, "rm" },
154 { "revert", cmd_revert, usage_revert, "rv" },
155 { "commit", cmd_commit, usage_commit, "ci" },
156 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 { "backout", cmd_backout, usage_backout, "bo" },
158 { "rebase", cmd_rebase, usage_rebase, "rb" },
159 { "histedit", cmd_histedit, usage_histedit, "he" },
160 { "integrate", cmd_integrate, usage_integrate,"ig" },
161 { "stage", cmd_stage, usage_stage, "sg" },
162 { "unstage", cmd_unstage, usage_unstage, "ug" },
163 { "cat", cmd_cat, usage_cat, "" },
164 };
166 static void
167 list_commands(void)
169 int i;
171 fprintf(stderr, "commands:");
172 for (i = 0; i < nitems(got_commands); i++) {
173 struct got_cmd *cmd = &got_commands[i];
174 fprintf(stderr, " %s", cmd->cmd_name);
176 fputc('\n', stderr);
179 int
180 main(int argc, char *argv[])
182 struct got_cmd *cmd;
183 unsigned int i;
184 int ch;
185 int hflag = 0, Vflag = 0;
186 static struct option longopts[] = {
187 { "version", no_argument, NULL, 'V' },
188 { NULL, 0, NULL, 0}
189 };
191 setlocale(LC_CTYPE, "");
193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 switch (ch) {
195 case 'h':
196 hflag = 1;
197 break;
198 case 'V':
199 Vflag = 1;
200 break;
201 default:
202 usage(hflag);
203 /* NOTREACHED */
207 argc -= optind;
208 argv += optind;
209 optind = 0;
211 if (Vflag) {
212 got_version_print_str();
213 return 1;
216 if (argc <= 0)
217 usage(hflag);
219 signal(SIGINT, catch_sigint);
220 signal(SIGPIPE, catch_sigpipe);
222 for (i = 0; i < nitems(got_commands); i++) {
223 const struct got_error *error;
225 cmd = &got_commands[i];
227 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 strcmp(cmd->cmd_alias, argv[0]) != 0)
229 continue;
231 if (hflag)
232 got_commands[i].cmd_usage();
234 error = got_commands[i].cmd_main(argc, argv);
235 if (error && error->code != GOT_ERR_CANCELLED &&
236 error->code != GOT_ERR_PRIVSEP_EXIT &&
237 !(sigpipe_received &&
238 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 !(sigint_received &&
240 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 return 1;
245 return 0;
248 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 list_commands();
250 return 1;
253 __dead static void
254 usage(int hflag)
256 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 getprogname());
258 if (hflag)
259 list_commands();
260 exit(1);
263 static const struct got_error *
264 get_editor(char **abspath)
266 const struct got_error *err = NULL;
267 const char *editor;
269 *abspath = NULL;
271 editor = getenv("VISUAL");
272 if (editor == NULL)
273 editor = getenv("EDITOR");
275 if (editor) {
276 err = got_path_find_prog(abspath, editor);
277 if (err)
278 return err;
281 if (*abspath == NULL) {
282 *abspath = strdup("/bin/ed");
283 if (*abspath == NULL)
284 return got_error_from_errno("strdup");
287 return NULL;
290 static const struct got_error *
291 apply_unveil(const char *repo_path, int repo_read_only,
292 const char *worktree_path)
294 const struct got_error *err;
296 #ifdef PROFILE
297 if (unveil("gmon.out", "rwc") != 0)
298 return got_error_from_errno2("unveil", "gmon.out");
299 #endif
300 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 return got_error_from_errno2("unveil", worktree_path);
306 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 err = got_privsep_unveil_exec_helpers();
310 if (err != NULL)
311 return err;
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
316 return NULL;
319 __dead static void
320 usage_init(void)
322 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
323 exit(1);
326 static const struct got_error *
327 cmd_init(int argc, char *argv[])
329 const struct got_error *error = NULL;
330 char *repo_path = NULL;
331 int ch;
333 while ((ch = getopt(argc, argv, "")) != -1) {
334 switch (ch) {
335 default:
336 usage_init();
337 /* NOTREACHED */
341 argc -= optind;
342 argv += optind;
344 #ifndef PROFILE
345 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 err(1, "pledge");
347 #endif
348 if (argc != 1)
349 usage_init();
351 repo_path = strdup(argv[0]);
352 if (repo_path == NULL)
353 return got_error_from_errno("strdup");
355 got_path_strip_trailing_slashes(repo_path);
357 error = got_path_mkdir(repo_path);
358 if (error &&
359 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
360 goto done;
362 error = apply_unveil(repo_path, 0, NULL);
363 if (error)
364 goto done;
366 error = got_repo_init(repo_path);
367 done:
368 free(repo_path);
369 return error;
372 __dead static void
373 usage_import(void)
375 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 "[-r repository-path] [-I pattern] path\n", getprogname());
377 exit(1);
380 int
381 spawn_editor(const char *editor, const char *file)
383 pid_t pid;
384 sig_t sighup, sigint, sigquit;
385 int st = -1;
387 sighup = signal(SIGHUP, SIG_IGN);
388 sigint = signal(SIGINT, SIG_IGN);
389 sigquit = signal(SIGQUIT, SIG_IGN);
391 switch (pid = fork()) {
392 case -1:
393 goto doneediting;
394 case 0:
395 execl(editor, editor, file, (char *)NULL);
396 _exit(127);
399 while (waitpid(pid, &st, 0) == -1)
400 if (errno != EINTR)
401 break;
403 doneediting:
404 (void)signal(SIGHUP, sighup);
405 (void)signal(SIGINT, sigint);
406 (void)signal(SIGQUIT, sigquit);
408 if (!WIFEXITED(st)) {
409 errno = EINTR;
410 return -1;
413 return WEXITSTATUS(st);
416 static const struct got_error *
417 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 const char *initial_content)
420 const struct got_error *err = NULL;
421 char buf[1024];
422 struct stat st, st2;
423 FILE *fp;
424 int content_changed = 0;
425 size_t len;
427 *logmsg = NULL;
429 if (stat(logmsg_path, &st) == -1)
430 return got_error_from_errno2("stat", logmsg_path);
432 if (spawn_editor(editor, logmsg_path) == -1)
433 return got_error_from_errno("failed spawning editor");
435 if (stat(logmsg_path, &st2) == -1)
436 return got_error_from_errno("stat");
438 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 "no changes made to commit message, aborting");
442 *logmsg = malloc(st2.st_size + 1);
443 if (*logmsg == NULL)
444 return got_error_from_errno("malloc");
445 (*logmsg)[0] = '\0';
446 len = 0;
448 fp = fopen(logmsg_path, "r");
449 if (fp == NULL) {
450 err = got_error_from_errno("fopen");
451 goto done;
453 while (fgets(buf, sizeof(buf), fp) != NULL) {
454 if (!content_changed && strcmp(buf, initial_content) != 0)
455 content_changed = 1;
456 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 continue; /* remove comments and leading empty lines */
458 len = strlcat(*logmsg, buf, st2.st_size);
460 fclose(fp);
462 while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 (*logmsg)[len - 1] = '\0';
464 len--;
467 if (len == 0 || !content_changed)
468 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "commit message cannot be empty, aborting");
470 done:
471 if (err) {
472 free(*logmsg);
473 *logmsg = NULL;
475 return err;
478 static const struct got_error *
479 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 const char *path_dir, const char *branch_name)
482 char *initial_content = NULL;
483 const struct got_error *err = NULL;
484 int fd;
486 if (asprintf(&initial_content,
487 "\n# %s to be imported to branch %s\n", path_dir,
488 branch_name) == -1)
489 return got_error_from_errno("asprintf");
491 err = got_opentemp_named_fd(logmsg_path, &fd,
492 GOT_TMPDIR_STR "/got-importmsg");
493 if (err)
494 goto done;
496 dprintf(fd, initial_content);
497 close(fd);
499 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 done:
501 free(initial_content);
502 return err;
505 static const struct got_error *
506 import_progress(void *arg, const char *path)
508 printf("A %s\n", path);
509 return NULL;
512 static const struct got_error *
513 get_author(char **author, struct got_repository *repo)
515 const struct got_error *err = NULL;
516 const char *got_author, *name, *email;
518 *author = NULL;
520 name = got_repo_get_gitconfig_author_name(repo);
521 email = got_repo_get_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
528 got_author = getenv("GOT_AUTHOR");
529 if (got_author == NULL) {
530 name = got_repo_get_global_gitconfig_author_name(repo);
531 email = got_repo_get_global_gitconfig_author_email(repo);
532 if (name && email) {
533 if (asprintf(author, "%s <%s>", name, email) == -1)
534 return got_error_from_errno("asprintf");
535 return NULL;
537 /* TODO: Look up user in password database? */
538 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
541 *author = strdup(got_author);
542 if (*author == NULL)
543 return got_error_from_errno("strdup");
545 /*
546 * Really dumb email address check; we're only doing this to
547 * avoid git's object parser breaking on commits we create.
548 */
549 while (*got_author && *got_author != '<')
550 got_author++;
551 if (*got_author != '<') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 goto done;
555 while (*got_author && *got_author != '@')
556 got_author++;
557 if (*got_author != '@') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 goto done;
561 while (*got_author && *got_author != '>')
562 got_author++;
563 if (*got_author != '>')
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 done:
566 if (err) {
567 free(*author);
568 *author = NULL;
570 return err;
573 static const struct got_error *
574 get_gitconfig_path(char **gitconfig_path)
576 const char *homedir = getenv("HOME");
578 *gitconfig_path = NULL;
579 if (homedir) {
580 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 return got_error_from_errno("asprintf");
584 return NULL;
587 static const struct got_error *
588 cmd_import(int argc, char *argv[])
590 const struct got_error *error = NULL;
591 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 const char *branch_name = "main";
594 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 struct got_repository *repo = NULL;
596 struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 struct got_object_id *new_commit_id = NULL;
598 int ch;
599 struct got_pathlist_head ignores;
600 struct got_pathlist_entry *pe;
601 int preserve_logmsg = 0;
603 TAILQ_INIT(&ignores);
605 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 switch (ch) {
607 case 'b':
608 branch_name = optarg;
609 break;
610 case 'm':
611 logmsg = strdup(optarg);
612 if (logmsg == NULL) {
613 error = got_error_from_errno("strdup");
614 goto done;
616 break;
617 case 'r':
618 repo_path = realpath(optarg, NULL);
619 if (repo_path == NULL) {
620 error = got_error_from_errno2("realpath",
621 optarg);
622 goto done;
624 break;
625 case 'I':
626 if (optarg[0] == '\0')
627 break;
628 error = got_pathlist_insert(&pe, &ignores, optarg,
629 NULL);
630 if (error)
631 goto done;
632 break;
633 default:
634 usage_import();
635 /* NOTREACHED */
639 argc -= optind;
640 argv += optind;
642 #ifndef PROFILE
643 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 "unveil",
645 NULL) == -1)
646 err(1, "pledge");
647 #endif
648 if (argc != 1)
649 usage_import();
651 if (repo_path == NULL) {
652 repo_path = getcwd(NULL, 0);
653 if (repo_path == NULL)
654 return got_error_from_errno("getcwd");
656 got_path_strip_trailing_slashes(repo_path);
657 error = get_gitconfig_path(&gitconfig_path);
658 if (error)
659 goto done;
660 error = got_repo_open(&repo, repo_path, gitconfig_path);
661 if (error)
662 goto done;
664 error = get_author(&author, repo);
665 if (error)
666 return error;
668 /*
669 * Don't let the user create a branch name with a leading '-'.
670 * While technically a valid reference name, this case is usually
671 * an unintended typo.
672 */
673 if (branch_name[0] == '-')
674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 error = got_error_from_errno("asprintf");
678 goto done;
681 error = got_ref_open(&branch_ref, repo, refname, 0);
682 if (error) {
683 if (error->code != GOT_ERR_NOT_REF)
684 goto done;
685 } else {
686 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 "import target branch already exists");
688 goto done;
691 path_dir = realpath(argv[0], NULL);
692 if (path_dir == NULL) {
693 error = got_error_from_errno2("realpath", argv[0]);
694 goto done;
696 got_path_strip_trailing_slashes(path_dir);
698 /*
699 * unveil(2) traverses exec(2); if an editor is used we have
700 * to apply unveil after the log message has been written.
701 */
702 if (logmsg == NULL || strlen(logmsg) == 0) {
703 error = get_editor(&editor);
704 if (error)
705 goto done;
706 free(logmsg);
707 error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 path_dir, refname);
709 if (error) {
710 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 logmsg_path != NULL)
712 preserve_logmsg = 1;
713 goto done;
717 if (unveil(path_dir, "r") != 0) {
718 error = got_error_from_errno2("unveil", path_dir);
719 if (logmsg_path)
720 preserve_logmsg = 1;
721 goto done;
724 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 if (error) {
726 if (logmsg_path)
727 preserve_logmsg = 1;
728 goto done;
731 error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 author, &ignores, repo, import_progress, NULL);
733 if (error) {
734 if (logmsg_path)
735 preserve_logmsg = 1;
736 goto done;
739 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 if (error) {
741 if (logmsg_path)
742 preserve_logmsg = 1;
743 goto done;
746 error = got_ref_write(branch_ref, repo);
747 if (error) {
748 if (logmsg_path)
749 preserve_logmsg = 1;
750 goto done;
753 error = got_object_id_str(&id_str, new_commit_id);
754 if (error) {
755 if (logmsg_path)
756 preserve_logmsg = 1;
757 goto done;
760 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 if (error) {
762 if (error->code != GOT_ERR_NOT_REF) {
763 if (logmsg_path)
764 preserve_logmsg = 1;
765 goto done;
768 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 branch_ref);
770 if (error) {
771 if (logmsg_path)
772 preserve_logmsg = 1;
773 goto done;
776 error = got_ref_write(head_ref, repo);
777 if (error) {
778 if (logmsg_path)
779 preserve_logmsg = 1;
780 goto done;
784 printf("Created branch %s with commit %s\n",
785 got_ref_get_name(branch_ref), id_str);
786 done:
787 if (preserve_logmsg) {
788 fprintf(stderr, "%s: log message preserved in %s\n",
789 getprogname(), logmsg_path);
790 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 error = got_error_from_errno2("unlink", logmsg_path);
792 free(logmsg);
793 free(logmsg_path);
794 free(repo_path);
795 free(editor);
796 free(refname);
797 free(new_commit_id);
798 free(id_str);
799 free(author);
800 free(gitconfig_path);
801 if (branch_ref)
802 got_ref_close(branch_ref);
803 if (head_ref)
804 got_ref_close(head_ref);
805 return error;
808 __dead static void
809 usage_clone(void)
811 fprintf(stderr, "usage: %s clone repo-url [target-directory]\n",
812 getprogname());
813 exit(1);
816 __dead static void
817 usage_checkout(void)
819 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
820 "[-p prefix] repository-path [worktree-path]\n", getprogname());
821 exit(1);
824 static void
825 show_worktree_base_ref_warning(void)
827 fprintf(stderr, "%s: warning: could not create a reference "
828 "to the work tree's base commit; the commit could be "
829 "garbage-collected by Git; making the repository "
830 "writable and running 'got update' will prevent this\n",
831 getprogname());
834 struct got_checkout_progress_arg {
835 const char *worktree_path;
836 int had_base_commit_ref_error;
837 };
839 static const struct got_error *
840 checkout_progress(void *arg, unsigned char status, const char *path)
842 struct got_checkout_progress_arg *a = arg;
844 /* Base commit bump happens silently. */
845 if (status == GOT_STATUS_BUMP_BASE)
846 return NULL;
848 if (status == GOT_STATUS_BASE_REF_ERR) {
849 a->had_base_commit_ref_error = 1;
850 return NULL;
853 while (path[0] == '/')
854 path++;
856 printf("%c %s/%s\n", status, a->worktree_path, path);
857 return NULL;
860 static const struct got_error *
861 check_cancelled(void *arg)
863 if (sigint_received || sigpipe_received)
864 return got_error(GOT_ERR_CANCELLED);
865 return NULL;
868 static const struct got_error *
869 check_linear_ancestry(struct got_object_id *commit_id,
870 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
871 struct got_repository *repo)
873 const struct got_error *err = NULL;
874 struct got_object_id *yca_id;
876 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
877 commit_id, base_commit_id, repo, check_cancelled, NULL);
878 if (err)
879 return err;
881 if (yca_id == NULL)
882 return got_error(GOT_ERR_ANCESTRY);
884 /*
885 * Require a straight line of history between the target commit
886 * and the work tree's base commit.
888 * Non-linear situations such as this require a rebase:
890 * (commit) D F (base_commit)
891 * \ /
892 * C E
893 * \ /
894 * B (yca)
895 * |
896 * A
898 * 'got update' only handles linear cases:
899 * Update forwards in time: A (base/yca) - B - C - D (commit)
900 * Update backwards in time: D (base) - C - B - A (commit/yca)
901 */
902 if (allow_forwards_in_time_only) {
903 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
904 return got_error(GOT_ERR_ANCESTRY);
905 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
906 got_object_id_cmp(base_commit_id, yca_id) != 0)
907 return got_error(GOT_ERR_ANCESTRY);
909 free(yca_id);
910 return NULL;
913 static const struct got_error *
914 check_same_branch(struct got_object_id *commit_id,
915 struct got_reference *head_ref, struct got_object_id *yca_id,
916 struct got_repository *repo)
918 const struct got_error *err = NULL;
919 struct got_commit_graph *graph = NULL;
920 struct got_object_id *head_commit_id = NULL;
921 int is_same_branch = 0;
923 err = got_ref_resolve(&head_commit_id, repo, head_ref);
924 if (err)
925 goto done;
927 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
928 is_same_branch = 1;
929 goto done;
931 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
932 is_same_branch = 1;
933 goto done;
936 err = got_commit_graph_open(&graph, "/", 1);
937 if (err)
938 goto done;
940 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
941 check_cancelled, NULL);
942 if (err)
943 goto done;
945 for (;;) {
946 struct got_object_id *id;
947 err = got_commit_graph_iter_next(&id, graph, repo,
948 check_cancelled, NULL);
949 if (err) {
950 if (err->code == GOT_ERR_ITER_COMPLETED)
951 err = NULL;
952 break;
955 if (id) {
956 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
957 break;
958 if (got_object_id_cmp(id, commit_id) == 0) {
959 is_same_branch = 1;
960 break;
964 done:
965 if (graph)
966 got_commit_graph_close(graph);
967 free(head_commit_id);
968 if (!err && !is_same_branch)
969 err = got_error(GOT_ERR_ANCESTRY);
970 return err;
973 struct got_fetch_progress_arg {
974 char last_scaled_size[FMT_SCALED_STRSIZE];
975 int last_p_indexed;
976 int last_p_resolved;
977 };
979 static const struct got_error *
980 fetch_progress(void *arg, const char *message, off_t packfile_size,
981 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
983 struct got_fetch_progress_arg *a = arg;
984 char scaled_size[FMT_SCALED_STRSIZE];
985 int p_indexed, p_resolved;
986 int print_size = 0, print_indexed = 0, print_resolved = 0;
988 if (message && message[0] != '\0' && message[0] != '\n') {
989 printf("\rserver: %s", message);
990 if (strchr(message, '\n') == 0)
991 printf("\n");
992 fflush(stdout);
995 if (packfile_size > 0 || nobj_indexed > 0) {
996 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
997 (a->last_scaled_size[0] == '\0' ||
998 strcmp(scaled_size, a->last_scaled_size)) != 0) {
999 print_size = 1;
1000 if (strlcpy(a->last_scaled_size, scaled_size,
1001 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1002 return got_error(GOT_ERR_NO_SPACE);
1004 if (nobj_indexed > 0) {
1005 p_indexed = (nobj_indexed * 100) / nobj_total;
1006 if (p_indexed != a->last_p_indexed) {
1007 a->last_p_indexed = p_indexed;
1008 print_indexed = 1;
1009 print_size = 1;
1012 if (nobj_resolved > 0) {
1013 p_resolved = (nobj_resolved * 100) /
1014 (nobj_total - nobj_loose);
1015 if (p_resolved != a->last_p_resolved) {
1016 a->last_p_resolved = p_resolved;
1017 print_resolved = 1;
1018 print_indexed = 1;
1019 print_size = 1;
1024 if (print_size || print_indexed || print_resolved)
1025 printf("\r");
1026 if (print_size)
1027 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1028 if (print_indexed)
1029 printf("; indexing %d%%", p_indexed);
1030 if (print_resolved)
1031 printf("; resolving deltas %d%%", p_resolved);
1032 if (print_size || print_indexed || print_resolved)
1033 fflush(stdout);
1035 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
1036 nobj_resolved == nobj_total - nobj_loose)
1037 printf("\nWriting pack index...\n");
1039 return NULL;
1042 static const struct got_error *
1043 cmd_clone(int argc, char *argv[])
1045 const struct got_error *err = NULL;
1046 const char *uri, *dirname;
1047 char *proto, *host, *port, *repo_name, *server_path;
1048 char *default_destdir = NULL, *id_str = NULL;
1049 const char *repo_path;
1050 struct got_repository *repo = NULL;
1051 struct got_pathlist_head refs, symrefs;
1052 struct got_pathlist_entry *pe;
1053 struct got_object_id *pack_hash = NULL;
1054 int ch, fetchfd = -1;
1055 struct got_fetch_progress_arg fpa;
1057 TAILQ_INIT(&refs);
1058 TAILQ_INIT(&symrefs);
1060 while ((ch = getopt(argc, argv, "")) != -1) {
1061 switch (ch) {
1062 default:
1063 usage_clone();
1064 break;
1067 argc -= optind;
1068 argv += optind;
1069 uri = argv[0];
1071 if (argc == 1)
1072 dirname = NULL;
1073 else if (argc == 2)
1074 dirname = argv[1];
1075 else
1076 usage_clone();
1078 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1079 &repo_name, argv[0]);
1080 if (err)
1081 goto done;
1083 if (dirname == NULL) {
1084 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1085 err = got_error_from_errno("asprintf");
1086 goto done;
1088 repo_path = default_destdir;
1089 } else
1090 repo_path = dirname;
1092 err = got_path_mkdir(repo_path);
1093 if (err)
1094 goto done;
1096 err = got_repo_init(repo_path);
1097 if (err)
1098 goto done;
1100 err = got_repo_open(&repo, repo_path, NULL);
1101 if (err)
1102 goto done;
1104 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1105 if (err)
1106 goto done;
1108 printf("Connected to %s:%s\n", host, port);
1110 fpa.last_scaled_size[0] = '\0';
1111 fpa.last_p_indexed = -1;
1112 fpa.last_p_resolved = -1;
1113 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1114 repo, fetch_progress, &fpa);
1115 if (err)
1116 goto done;
1118 err = got_object_id_str(&id_str, pack_hash);
1119 if (err)
1120 goto done;
1121 printf("Fetched %s.pack\n", id_str);
1122 free(id_str);
1124 /* Set up references provided with the pack file. */
1125 TAILQ_FOREACH(pe, &refs, entry) {
1126 const char *refname = pe->path;
1127 struct got_object_id *id = pe->data;
1128 struct got_reference *ref;
1131 err = got_ref_alloc(&ref, refname, id);
1132 if (err)
1133 goto done;
1135 #if 0
1136 err = got_object_id_str(&id_str, id);
1137 if (err)
1138 goto done;
1139 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1140 free(id_str);
1141 #endif
1142 err = got_ref_write(ref, repo);
1143 got_ref_close(ref);
1144 if (err)
1145 goto done;
1148 /* Set the HEAD reference if the server provided one. */
1149 TAILQ_FOREACH(pe, &symrefs, entry) {
1150 struct got_reference *symref, *target_ref;
1151 const char *refname = pe->path;
1152 const char *target = pe->data;
1154 if (strcmp(refname, GOT_REF_HEAD) != 0)
1155 continue;
1157 err = got_ref_open(&target_ref, repo, target, 0);
1158 if (err) {
1159 if (err->code == GOT_ERR_NOT_REF)
1160 continue;
1161 goto done;
1164 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1165 got_ref_close(target_ref);
1166 if (err)
1167 goto done;
1169 printf("Setting %s to %s\n", GOT_REF_HEAD,
1170 got_ref_get_symref_target(symref));
1172 err = got_ref_write(symref, repo);
1173 got_ref_close(symref);
1174 break;
1177 done:
1178 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1179 err = got_error_from_errno("close");
1180 if (repo)
1181 got_repo_close(repo);
1182 TAILQ_FOREACH(pe, &refs, entry) {
1183 free((void *)pe->path);
1184 free(pe->data);
1186 got_pathlist_free(&refs);
1187 TAILQ_FOREACH(pe, &symrefs, entry) {
1188 free((void *)pe->path);
1189 free(pe->data);
1191 got_pathlist_free(&symrefs);
1192 free(pack_hash);
1193 free(proto);
1194 free(host);
1195 free(port);
1196 free(server_path);
1197 free(repo_name);
1198 free(default_destdir);
1199 return err;
1202 static const struct got_error *
1203 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1205 static char msg[512];
1206 const char *branch_name;
1208 if (got_ref_is_symbolic(ref))
1209 branch_name = got_ref_get_symref_target(ref);
1210 else
1211 branch_name = got_ref_get_name(ref);
1213 if (strncmp("refs/heads/", branch_name, 11) == 0)
1214 branch_name += 11;
1216 snprintf(msg, sizeof(msg),
1217 "target commit is not contained in branch '%s'; "
1218 "the branch to use must be specified with -b; "
1219 "if necessary a new branch can be created for "
1220 "this commit with 'got branch -c %s BRANCH_NAME'",
1221 branch_name, commit_id_str);
1223 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1226 static const struct got_error *
1227 cmd_checkout(int argc, char *argv[])
1229 const struct got_error *error = NULL;
1230 struct got_repository *repo = NULL;
1231 struct got_reference *head_ref = NULL;
1232 struct got_worktree *worktree = NULL;
1233 char *repo_path = NULL;
1234 char *worktree_path = NULL;
1235 const char *path_prefix = "";
1236 const char *branch_name = GOT_REF_HEAD;
1237 char *commit_id_str = NULL;
1238 int ch, same_path_prefix, allow_nonempty = 0;
1239 struct got_pathlist_head paths;
1240 struct got_checkout_progress_arg cpa;
1242 TAILQ_INIT(&paths);
1244 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1245 switch (ch) {
1246 case 'b':
1247 branch_name = optarg;
1248 break;
1249 case 'c':
1250 commit_id_str = strdup(optarg);
1251 if (commit_id_str == NULL)
1252 return got_error_from_errno("strdup");
1253 break;
1254 case 'E':
1255 allow_nonempty = 1;
1256 break;
1257 case 'p':
1258 path_prefix = optarg;
1259 break;
1260 default:
1261 usage_checkout();
1262 /* NOTREACHED */
1266 argc -= optind;
1267 argv += optind;
1269 #ifndef PROFILE
1270 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1271 "unveil", NULL) == -1)
1272 err(1, "pledge");
1273 #endif
1274 if (argc == 1) {
1275 char *cwd, *base, *dotgit;
1276 repo_path = realpath(argv[0], NULL);
1277 if (repo_path == NULL)
1278 return got_error_from_errno2("realpath", argv[0]);
1279 cwd = getcwd(NULL, 0);
1280 if (cwd == NULL) {
1281 error = got_error_from_errno("getcwd");
1282 goto done;
1284 if (path_prefix[0]) {
1285 base = basename(path_prefix);
1286 if (base == NULL) {
1287 error = got_error_from_errno2("basename",
1288 path_prefix);
1289 goto done;
1291 } else {
1292 base = basename(repo_path);
1293 if (base == NULL) {
1294 error = got_error_from_errno2("basename",
1295 repo_path);
1296 goto done;
1299 dotgit = strstr(base, ".git");
1300 if (dotgit)
1301 *dotgit = '\0';
1302 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1303 error = got_error_from_errno("asprintf");
1304 free(cwd);
1305 goto done;
1307 free(cwd);
1308 } else if (argc == 2) {
1309 repo_path = realpath(argv[0], NULL);
1310 if (repo_path == NULL) {
1311 error = got_error_from_errno2("realpath", argv[0]);
1312 goto done;
1314 worktree_path = realpath(argv[1], NULL);
1315 if (worktree_path == NULL) {
1316 if (errno != ENOENT) {
1317 error = got_error_from_errno2("realpath",
1318 argv[1]);
1319 goto done;
1321 worktree_path = strdup(argv[1]);
1322 if (worktree_path == NULL) {
1323 error = got_error_from_errno("strdup");
1324 goto done;
1327 } else
1328 usage_checkout();
1330 got_path_strip_trailing_slashes(repo_path);
1331 got_path_strip_trailing_slashes(worktree_path);
1333 error = got_repo_open(&repo, repo_path, NULL);
1334 if (error != NULL)
1335 goto done;
1337 /* Pre-create work tree path for unveil(2) */
1338 error = got_path_mkdir(worktree_path);
1339 if (error) {
1340 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1341 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1342 goto done;
1343 if (!allow_nonempty &&
1344 !got_path_dir_is_empty(worktree_path)) {
1345 error = got_error_path(worktree_path,
1346 GOT_ERR_DIR_NOT_EMPTY);
1347 goto done;
1351 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1352 if (error)
1353 goto done;
1355 error = got_ref_open(&head_ref, repo, branch_name, 0);
1356 if (error != NULL)
1357 goto done;
1359 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1360 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1361 goto done;
1363 error = got_worktree_open(&worktree, worktree_path);
1364 if (error != NULL)
1365 goto done;
1367 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1368 path_prefix);
1369 if (error != NULL)
1370 goto done;
1371 if (!same_path_prefix) {
1372 error = got_error(GOT_ERR_PATH_PREFIX);
1373 goto done;
1376 if (commit_id_str) {
1377 struct got_object_id *commit_id;
1378 error = got_repo_match_object_id(&commit_id, NULL,
1379 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1380 if (error)
1381 goto done;
1382 error = check_linear_ancestry(commit_id,
1383 got_worktree_get_base_commit_id(worktree), 0, repo);
1384 if (error != NULL) {
1385 free(commit_id);
1386 if (error->code == GOT_ERR_ANCESTRY) {
1387 error = checkout_ancestry_error(
1388 head_ref, commit_id_str);
1390 goto done;
1392 error = check_same_branch(commit_id, head_ref, NULL, repo);
1393 if (error) {
1394 if (error->code == GOT_ERR_ANCESTRY) {
1395 error = checkout_ancestry_error(
1396 head_ref, commit_id_str);
1398 goto done;
1400 error = got_worktree_set_base_commit_id(worktree, repo,
1401 commit_id);
1402 free(commit_id);
1403 if (error)
1404 goto done;
1407 error = got_pathlist_append(&paths, "", NULL);
1408 if (error)
1409 goto done;
1410 cpa.worktree_path = worktree_path;
1411 cpa.had_base_commit_ref_error = 0;
1412 error = got_worktree_checkout_files(worktree, &paths, repo,
1413 checkout_progress, &cpa, check_cancelled, NULL);
1414 if (error != NULL)
1415 goto done;
1417 printf("Now shut up and hack\n");
1418 if (cpa.had_base_commit_ref_error)
1419 show_worktree_base_ref_warning();
1420 done:
1421 got_pathlist_free(&paths);
1422 free(commit_id_str);
1423 free(repo_path);
1424 free(worktree_path);
1425 return error;
1428 __dead static void
1429 usage_update(void)
1431 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1432 getprogname());
1433 exit(1);
1436 static const struct got_error *
1437 update_progress(void *arg, unsigned char status, const char *path)
1439 int *did_something = arg;
1441 if (status == GOT_STATUS_EXISTS ||
1442 status == GOT_STATUS_BASE_REF_ERR)
1443 return NULL;
1445 *did_something = 1;
1447 /* Base commit bump happens silently. */
1448 if (status == GOT_STATUS_BUMP_BASE)
1449 return NULL;
1451 while (path[0] == '/')
1452 path++;
1453 printf("%c %s\n", status, path);
1454 return NULL;
1457 static const struct got_error *
1458 switch_head_ref(struct got_reference *head_ref,
1459 struct got_object_id *commit_id, struct got_worktree *worktree,
1460 struct got_repository *repo)
1462 const struct got_error *err = NULL;
1463 char *base_id_str;
1464 int ref_has_moved = 0;
1466 /* Trivial case: switching between two different references. */
1467 if (strcmp(got_ref_get_name(head_ref),
1468 got_worktree_get_head_ref_name(worktree)) != 0) {
1469 printf("Switching work tree from %s to %s\n",
1470 got_worktree_get_head_ref_name(worktree),
1471 got_ref_get_name(head_ref));
1472 return got_worktree_set_head_ref(worktree, head_ref);
1475 err = check_linear_ancestry(commit_id,
1476 got_worktree_get_base_commit_id(worktree), 0, repo);
1477 if (err) {
1478 if (err->code != GOT_ERR_ANCESTRY)
1479 return err;
1480 ref_has_moved = 1;
1482 if (!ref_has_moved)
1483 return NULL;
1485 /* Switching to a rebased branch with the same reference name. */
1486 err = got_object_id_str(&base_id_str,
1487 got_worktree_get_base_commit_id(worktree));
1488 if (err)
1489 return err;
1490 printf("Reference %s now points at a different branch\n",
1491 got_worktree_get_head_ref_name(worktree));
1492 printf("Switching work tree from %s to %s\n", base_id_str,
1493 got_worktree_get_head_ref_name(worktree));
1494 return NULL;
1497 static const struct got_error *
1498 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1500 const struct got_error *err;
1501 int in_progress;
1503 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1504 if (err)
1505 return err;
1506 if (in_progress)
1507 return got_error(GOT_ERR_REBASING);
1509 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1510 if (err)
1511 return err;
1512 if (in_progress)
1513 return got_error(GOT_ERR_HISTEDIT_BUSY);
1515 return NULL;
1518 static const struct got_error *
1519 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1520 char *argv[], struct got_worktree *worktree)
1522 const struct got_error *err = NULL;
1523 char *path;
1524 int i;
1526 if (argc == 0) {
1527 path = strdup("");
1528 if (path == NULL)
1529 return got_error_from_errno("strdup");
1530 return got_pathlist_append(paths, path, NULL);
1533 for (i = 0; i < argc; i++) {
1534 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1535 if (err)
1536 break;
1537 err = got_pathlist_append(paths, path, NULL);
1538 if (err) {
1539 free(path);
1540 break;
1544 return err;
1547 static const struct got_error *
1548 cmd_update(int argc, char *argv[])
1550 const struct got_error *error = NULL;
1551 struct got_repository *repo = NULL;
1552 struct got_worktree *worktree = NULL;
1553 char *worktree_path = NULL;
1554 struct got_object_id *commit_id = NULL;
1555 char *commit_id_str = NULL;
1556 const char *branch_name = NULL;
1557 struct got_reference *head_ref = NULL;
1558 struct got_pathlist_head paths;
1559 struct got_pathlist_entry *pe;
1560 int ch, did_something = 0;
1562 TAILQ_INIT(&paths);
1564 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1565 switch (ch) {
1566 case 'b':
1567 branch_name = optarg;
1568 break;
1569 case 'c':
1570 commit_id_str = strdup(optarg);
1571 if (commit_id_str == NULL)
1572 return got_error_from_errno("strdup");
1573 break;
1574 default:
1575 usage_update();
1576 /* NOTREACHED */
1580 argc -= optind;
1581 argv += optind;
1583 #ifndef PROFILE
1584 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1585 "unveil", NULL) == -1)
1586 err(1, "pledge");
1587 #endif
1588 worktree_path = getcwd(NULL, 0);
1589 if (worktree_path == NULL) {
1590 error = got_error_from_errno("getcwd");
1591 goto done;
1593 error = got_worktree_open(&worktree, worktree_path);
1594 if (error)
1595 goto done;
1597 error = check_rebase_or_histedit_in_progress(worktree);
1598 if (error)
1599 goto done;
1601 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1602 NULL);
1603 if (error != NULL)
1604 goto done;
1606 error = apply_unveil(got_repo_get_path(repo), 0,
1607 got_worktree_get_root_path(worktree));
1608 if (error)
1609 goto done;
1611 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1612 if (error)
1613 goto done;
1615 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1616 got_worktree_get_head_ref_name(worktree), 0);
1617 if (error != NULL)
1618 goto done;
1619 if (commit_id_str == NULL) {
1620 error = got_ref_resolve(&commit_id, repo, head_ref);
1621 if (error != NULL)
1622 goto done;
1623 error = got_object_id_str(&commit_id_str, commit_id);
1624 if (error != NULL)
1625 goto done;
1626 } else {
1627 error = got_repo_match_object_id(&commit_id, NULL,
1628 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1629 free(commit_id_str);
1630 commit_id_str = NULL;
1631 if (error)
1632 goto done;
1633 error = got_object_id_str(&commit_id_str, commit_id);
1634 if (error)
1635 goto done;
1638 if (branch_name) {
1639 struct got_object_id *head_commit_id;
1640 TAILQ_FOREACH(pe, &paths, entry) {
1641 if (pe->path_len == 0)
1642 continue;
1643 error = got_error_msg(GOT_ERR_BAD_PATH,
1644 "switching between branches requires that "
1645 "the entire work tree gets updated");
1646 goto done;
1648 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1649 if (error)
1650 goto done;
1651 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1652 repo);
1653 free(head_commit_id);
1654 if (error != NULL)
1655 goto done;
1656 error = check_same_branch(commit_id, head_ref, NULL, repo);
1657 if (error)
1658 goto done;
1659 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1660 if (error)
1661 goto done;
1662 } else {
1663 error = check_linear_ancestry(commit_id,
1664 got_worktree_get_base_commit_id(worktree), 0, repo);
1665 if (error != NULL) {
1666 if (error->code == GOT_ERR_ANCESTRY)
1667 error = got_error(GOT_ERR_BRANCH_MOVED);
1668 goto done;
1670 error = check_same_branch(commit_id, head_ref, NULL, repo);
1671 if (error)
1672 goto done;
1675 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1676 commit_id) != 0) {
1677 error = got_worktree_set_base_commit_id(worktree, repo,
1678 commit_id);
1679 if (error)
1680 goto done;
1683 error = got_worktree_checkout_files(worktree, &paths, repo,
1684 update_progress, &did_something, check_cancelled, NULL);
1685 if (error != NULL)
1686 goto done;
1688 if (did_something)
1689 printf("Updated to commit %s\n", commit_id_str);
1690 else
1691 printf("Already up-to-date\n");
1692 done:
1693 free(worktree_path);
1694 TAILQ_FOREACH(pe, &paths, entry)
1695 free((char *)pe->path);
1696 got_pathlist_free(&paths);
1697 free(commit_id);
1698 free(commit_id_str);
1699 return error;
1702 static const struct got_error *
1703 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1704 const char *path, int diff_context, int ignore_whitespace,
1705 struct got_repository *repo)
1707 const struct got_error *err = NULL;
1708 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1710 if (blob_id1) {
1711 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1712 if (err)
1713 goto done;
1716 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1717 if (err)
1718 goto done;
1720 while (path[0] == '/')
1721 path++;
1722 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1723 ignore_whitespace, stdout);
1724 done:
1725 if (blob1)
1726 got_object_blob_close(blob1);
1727 got_object_blob_close(blob2);
1728 return err;
1731 static const struct got_error *
1732 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1733 const char *path, int diff_context, int ignore_whitespace,
1734 struct got_repository *repo)
1736 const struct got_error *err = NULL;
1737 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1738 struct got_diff_blob_output_unidiff_arg arg;
1740 if (tree_id1) {
1741 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1742 if (err)
1743 goto done;
1746 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1747 if (err)
1748 goto done;
1750 arg.diff_context = diff_context;
1751 arg.ignore_whitespace = ignore_whitespace;
1752 arg.outfile = stdout;
1753 while (path[0] == '/')
1754 path++;
1755 err = got_diff_tree(tree1, tree2, path, path, repo,
1756 got_diff_blob_output_unidiff, &arg, 1);
1757 done:
1758 if (tree1)
1759 got_object_tree_close(tree1);
1760 if (tree2)
1761 got_object_tree_close(tree2);
1762 return err;
1765 static const struct got_error *
1766 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1767 const char *path, int diff_context, struct got_repository *repo)
1769 const struct got_error *err = NULL;
1770 struct got_commit_object *pcommit = NULL;
1771 char *id_str1 = NULL, *id_str2 = NULL;
1772 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1773 struct got_object_qid *qid;
1775 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1776 if (qid != NULL) {
1777 err = got_object_open_as_commit(&pcommit, repo,
1778 qid->id);
1779 if (err)
1780 return err;
1783 if (path && path[0] != '\0') {
1784 int obj_type;
1785 err = got_object_id_by_path(&obj_id2, repo, id, path);
1786 if (err)
1787 goto done;
1788 err = got_object_id_str(&id_str2, obj_id2);
1789 if (err) {
1790 free(obj_id2);
1791 goto done;
1793 if (pcommit) {
1794 err = got_object_id_by_path(&obj_id1, repo,
1795 qid->id, path);
1796 if (err) {
1797 free(obj_id2);
1798 goto done;
1800 err = got_object_id_str(&id_str1, obj_id1);
1801 if (err) {
1802 free(obj_id2);
1803 goto done;
1806 err = got_object_get_type(&obj_type, repo, obj_id2);
1807 if (err) {
1808 free(obj_id2);
1809 goto done;
1811 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1812 switch (obj_type) {
1813 case GOT_OBJ_TYPE_BLOB:
1814 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1815 0, repo);
1816 break;
1817 case GOT_OBJ_TYPE_TREE:
1818 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1819 0, repo);
1820 break;
1821 default:
1822 err = got_error(GOT_ERR_OBJ_TYPE);
1823 break;
1825 free(obj_id1);
1826 free(obj_id2);
1827 } else {
1828 obj_id2 = got_object_commit_get_tree_id(commit);
1829 err = got_object_id_str(&id_str2, obj_id2);
1830 if (err)
1831 goto done;
1832 obj_id1 = got_object_commit_get_tree_id(pcommit);
1833 err = got_object_id_str(&id_str1, obj_id1);
1834 if (err)
1835 goto done;
1836 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1837 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1839 done:
1840 free(id_str1);
1841 free(id_str2);
1842 if (pcommit)
1843 got_object_commit_close(pcommit);
1844 return err;
1847 static char *
1848 get_datestr(time_t *time, char *datebuf)
1850 struct tm mytm, *tm;
1851 char *p, *s;
1853 tm = gmtime_r(time, &mytm);
1854 if (tm == NULL)
1855 return NULL;
1856 s = asctime_r(tm, datebuf);
1857 if (s == NULL)
1858 return NULL;
1859 p = strchr(s, '\n');
1860 if (p)
1861 *p = '\0';
1862 return s;
1865 static const struct got_error *
1866 match_logmsg(int *have_match, struct got_object_id *id,
1867 struct got_commit_object *commit, regex_t *regex)
1869 const struct got_error *err = NULL;
1870 regmatch_t regmatch;
1871 char *id_str = NULL, *logmsg = NULL;
1873 *have_match = 0;
1875 err = got_object_id_str(&id_str, id);
1876 if (err)
1877 return err;
1879 err = got_object_commit_get_logmsg(&logmsg, commit);
1880 if (err)
1881 goto done;
1883 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1884 *have_match = 1;
1885 done:
1886 free(id_str);
1887 free(logmsg);
1888 return err;
1891 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1893 static const struct got_error *
1894 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1895 struct got_repository *repo, const char *path, int show_patch,
1896 int diff_context, struct got_reflist_head *refs)
1898 const struct got_error *err = NULL;
1899 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1900 char datebuf[26];
1901 time_t committer_time;
1902 const char *author, *committer;
1903 char *refs_str = NULL;
1904 struct got_reflist_entry *re;
1906 SIMPLEQ_FOREACH(re, refs, entry) {
1907 char *s;
1908 const char *name;
1909 struct got_tag_object *tag = NULL;
1910 int cmp;
1912 name = got_ref_get_name(re->ref);
1913 if (strcmp(name, GOT_REF_HEAD) == 0)
1914 continue;
1915 if (strncmp(name, "refs/", 5) == 0)
1916 name += 5;
1917 if (strncmp(name, "got/", 4) == 0)
1918 continue;
1919 if (strncmp(name, "heads/", 6) == 0)
1920 name += 6;
1921 if (strncmp(name, "remotes/", 8) == 0)
1922 name += 8;
1923 if (strncmp(name, "tags/", 5) == 0) {
1924 err = got_object_open_as_tag(&tag, repo, re->id);
1925 if (err) {
1926 if (err->code != GOT_ERR_OBJ_TYPE)
1927 return err;
1928 /* Ref points at something other than a tag. */
1929 err = NULL;
1930 tag = NULL;
1933 cmp = got_object_id_cmp(tag ?
1934 got_object_tag_get_object_id(tag) : re->id, id);
1935 if (tag)
1936 got_object_tag_close(tag);
1937 if (cmp != 0)
1938 continue;
1939 s = refs_str;
1940 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1941 name) == -1) {
1942 err = got_error_from_errno("asprintf");
1943 free(s);
1944 return err;
1946 free(s);
1948 err = got_object_id_str(&id_str, id);
1949 if (err)
1950 return err;
1952 printf(GOT_COMMIT_SEP_STR);
1953 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1954 refs_str ? refs_str : "", refs_str ? ")" : "");
1955 free(id_str);
1956 id_str = NULL;
1957 free(refs_str);
1958 refs_str = NULL;
1959 printf("from: %s\n", got_object_commit_get_author(commit));
1960 committer_time = got_object_commit_get_committer_time(commit);
1961 datestr = get_datestr(&committer_time, datebuf);
1962 if (datestr)
1963 printf("date: %s UTC\n", datestr);
1964 author = got_object_commit_get_author(commit);
1965 committer = got_object_commit_get_committer(commit);
1966 if (strcmp(author, committer) != 0)
1967 printf("via: %s\n", committer);
1968 if (got_object_commit_get_nparents(commit) > 1) {
1969 const struct got_object_id_queue *parent_ids;
1970 struct got_object_qid *qid;
1971 int n = 1;
1972 parent_ids = got_object_commit_get_parent_ids(commit);
1973 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1974 err = got_object_id_str(&id_str, qid->id);
1975 if (err)
1976 return err;
1977 printf("parent %d: %s\n", n++, id_str);
1978 free(id_str);
1982 err = got_object_commit_get_logmsg(&logmsg0, commit);
1983 if (err)
1984 return err;
1986 logmsg = logmsg0;
1987 do {
1988 line = strsep(&logmsg, "\n");
1989 if (line)
1990 printf(" %s\n", line);
1991 } while (line);
1992 free(logmsg0);
1994 if (show_patch) {
1995 err = print_patch(commit, id, path, diff_context, repo);
1996 if (err == 0)
1997 printf("\n");
2000 if (fflush(stdout) != 0 && err == NULL)
2001 err = got_error_from_errno("fflush");
2002 return err;
2005 static const struct got_error *
2006 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2007 const char *path, int show_patch, const char *search_pattern,
2008 int diff_context, int limit, int log_branches,
2009 struct got_reflist_head *refs)
2011 const struct got_error *err;
2012 struct got_commit_graph *graph;
2013 regex_t regex;
2014 int have_match;
2016 if (search_pattern &&
2017 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2018 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2020 err = got_commit_graph_open(&graph, path, !log_branches);
2021 if (err)
2022 return err;
2023 err = got_commit_graph_iter_start(graph, root_id, repo,
2024 check_cancelled, NULL);
2025 if (err)
2026 goto done;
2027 for (;;) {
2028 struct got_commit_object *commit;
2029 struct got_object_id *id;
2031 if (sigint_received || sigpipe_received)
2032 break;
2034 err = got_commit_graph_iter_next(&id, graph, repo,
2035 check_cancelled, NULL);
2036 if (err) {
2037 if (err->code == GOT_ERR_ITER_COMPLETED)
2038 err = NULL;
2039 break;
2041 if (id == NULL)
2042 break;
2044 err = got_object_open_as_commit(&commit, repo, id);
2045 if (err)
2046 break;
2048 if (search_pattern) {
2049 err = match_logmsg(&have_match, id, commit, &regex);
2050 if (err) {
2051 got_object_commit_close(commit);
2052 break;
2054 if (have_match == 0) {
2055 got_object_commit_close(commit);
2056 continue;
2060 err = print_commit(commit, id, repo, path, show_patch,
2061 diff_context, refs);
2062 got_object_commit_close(commit);
2063 if (err || (limit && --limit == 0))
2064 break;
2066 done:
2067 if (search_pattern)
2068 regfree(&regex);
2069 got_commit_graph_close(graph);
2070 return err;
2073 __dead static void
2074 usage_log(void)
2076 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2077 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2078 exit(1);
2081 static int
2082 get_default_log_limit(void)
2084 const char *got_default_log_limit;
2085 long long n;
2086 const char *errstr;
2088 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2089 if (got_default_log_limit == NULL)
2090 return 0;
2091 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2092 if (errstr != NULL)
2093 return 0;
2094 return n;
2097 static const struct got_error *
2098 cmd_log(int argc, char *argv[])
2100 const struct got_error *error;
2101 struct got_repository *repo = NULL;
2102 struct got_worktree *worktree = NULL;
2103 struct got_commit_object *commit = NULL;
2104 struct got_object_id *id = NULL;
2105 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2106 const char *start_commit = NULL, *search_pattern = NULL;
2107 int diff_context = -1, ch;
2108 int show_patch = 0, limit = 0, log_branches = 0;
2109 const char *errstr;
2110 struct got_reflist_head refs;
2112 SIMPLEQ_INIT(&refs);
2114 #ifndef PROFILE
2115 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2116 NULL)
2117 == -1)
2118 err(1, "pledge");
2119 #endif
2121 limit = get_default_log_limit();
2123 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2124 switch (ch) {
2125 case 'p':
2126 show_patch = 1;
2127 break;
2128 case 'c':
2129 start_commit = optarg;
2130 break;
2131 case 'C':
2132 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2133 &errstr);
2134 if (errstr != NULL)
2135 err(1, "-C option %s", errstr);
2136 break;
2137 case 'l':
2138 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2139 if (errstr != NULL)
2140 err(1, "-l option %s", errstr);
2141 break;
2142 case 'b':
2143 log_branches = 1;
2144 break;
2145 case 'r':
2146 repo_path = realpath(optarg, NULL);
2147 if (repo_path == NULL)
2148 return got_error_from_errno2("realpath",
2149 optarg);
2150 got_path_strip_trailing_slashes(repo_path);
2151 break;
2152 case 's':
2153 search_pattern = optarg;
2154 break;
2155 default:
2156 usage_log();
2157 /* NOTREACHED */
2161 argc -= optind;
2162 argv += optind;
2164 if (diff_context == -1)
2165 diff_context = 3;
2166 else if (!show_patch)
2167 errx(1, "-C reguires -p");
2169 cwd = getcwd(NULL, 0);
2170 if (cwd == NULL) {
2171 error = got_error_from_errno("getcwd");
2172 goto done;
2175 error = got_worktree_open(&worktree, cwd);
2176 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2177 goto done;
2178 error = NULL;
2180 if (argc == 0) {
2181 path = strdup("");
2182 if (path == NULL) {
2183 error = got_error_from_errno("strdup");
2184 goto done;
2186 } else if (argc == 1) {
2187 if (worktree) {
2188 error = got_worktree_resolve_path(&path, worktree,
2189 argv[0]);
2190 if (error)
2191 goto done;
2192 } else {
2193 path = strdup(argv[0]);
2194 if (path == NULL) {
2195 error = got_error_from_errno("strdup");
2196 goto done;
2199 } else
2200 usage_log();
2202 if (repo_path == NULL) {
2203 repo_path = worktree ?
2204 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2206 if (repo_path == NULL) {
2207 error = got_error_from_errno("strdup");
2208 goto done;
2211 error = got_repo_open(&repo, repo_path, NULL);
2212 if (error != NULL)
2213 goto done;
2215 error = apply_unveil(got_repo_get_path(repo), 1,
2216 worktree ? got_worktree_get_root_path(worktree) : NULL);
2217 if (error)
2218 goto done;
2220 if (start_commit == NULL) {
2221 struct got_reference *head_ref;
2222 error = got_ref_open(&head_ref, repo,
2223 worktree ? got_worktree_get_head_ref_name(worktree)
2224 : GOT_REF_HEAD, 0);
2225 if (error != NULL)
2226 return error;
2227 error = got_ref_resolve(&id, repo, head_ref);
2228 got_ref_close(head_ref);
2229 if (error != NULL)
2230 return error;
2231 error = got_object_open_as_commit(&commit, repo, id);
2232 } else {
2233 struct got_reference *ref;
2234 error = got_ref_open(&ref, repo, start_commit, 0);
2235 if (error == NULL) {
2236 int obj_type;
2237 error = got_ref_resolve(&id, repo, ref);
2238 got_ref_close(ref);
2239 if (error != NULL)
2240 goto done;
2241 error = got_object_get_type(&obj_type, repo, id);
2242 if (error != NULL)
2243 goto done;
2244 if (obj_type == GOT_OBJ_TYPE_TAG) {
2245 struct got_tag_object *tag;
2246 error = got_object_open_as_tag(&tag, repo, id);
2247 if (error != NULL)
2248 goto done;
2249 if (got_object_tag_get_object_type(tag) !=
2250 GOT_OBJ_TYPE_COMMIT) {
2251 got_object_tag_close(tag);
2252 error = got_error(GOT_ERR_OBJ_TYPE);
2253 goto done;
2255 free(id);
2256 id = got_object_id_dup(
2257 got_object_tag_get_object_id(tag));
2258 if (id == NULL)
2259 error = got_error_from_errno(
2260 "got_object_id_dup");
2261 got_object_tag_close(tag);
2262 if (error)
2263 goto done;
2264 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2265 error = got_error(GOT_ERR_OBJ_TYPE);
2266 goto done;
2268 error = got_object_open_as_commit(&commit, repo, id);
2269 if (error != NULL)
2270 goto done;
2272 if (commit == NULL) {
2273 error = got_repo_match_object_id_prefix(&id,
2274 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2275 if (error != NULL)
2276 return error;
2279 if (error != NULL)
2280 goto done;
2282 if (worktree) {
2283 const char *prefix = got_worktree_get_path_prefix(worktree);
2284 char *p;
2285 if (asprintf(&p, "%s%s%s", prefix,
2286 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2287 error = got_error_from_errno("asprintf");
2288 goto done;
2290 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2291 free(p);
2292 } else
2293 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2294 if (error != NULL)
2295 goto done;
2296 if (in_repo_path) {
2297 free(path);
2298 path = in_repo_path;
2301 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2302 if (error)
2303 goto done;
2305 error = print_commits(id, repo, path, show_patch, search_pattern,
2306 diff_context, limit, log_branches, &refs);
2307 done:
2308 free(path);
2309 free(repo_path);
2310 free(cwd);
2311 free(id);
2312 if (worktree)
2313 got_worktree_close(worktree);
2314 if (repo) {
2315 const struct got_error *repo_error;
2316 repo_error = got_repo_close(repo);
2317 if (error == NULL)
2318 error = repo_error;
2320 got_ref_list_free(&refs);
2321 return error;
2324 __dead static void
2325 usage_diff(void)
2327 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2328 "[-w] [object1 object2 | path]\n", getprogname());
2329 exit(1);
2332 struct print_diff_arg {
2333 struct got_repository *repo;
2334 struct got_worktree *worktree;
2335 int diff_context;
2336 const char *id_str;
2337 int header_shown;
2338 int diff_staged;
2339 int ignore_whitespace;
2342 static const struct got_error *
2343 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2344 const char *path, struct got_object_id *blob_id,
2345 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2346 int dirfd, const char *de_name)
2348 struct print_diff_arg *a = arg;
2349 const struct got_error *err = NULL;
2350 struct got_blob_object *blob1 = NULL;
2351 int fd = -1;
2352 FILE *f2 = NULL;
2353 char *abspath = NULL, *label1 = NULL;
2354 struct stat sb;
2356 if (a->diff_staged) {
2357 if (staged_status != GOT_STATUS_MODIFY &&
2358 staged_status != GOT_STATUS_ADD &&
2359 staged_status != GOT_STATUS_DELETE)
2360 return NULL;
2361 } else {
2362 if (staged_status == GOT_STATUS_DELETE)
2363 return NULL;
2364 if (status == GOT_STATUS_NONEXISTENT)
2365 return got_error_set_errno(ENOENT, path);
2366 if (status != GOT_STATUS_MODIFY &&
2367 status != GOT_STATUS_ADD &&
2368 status != GOT_STATUS_DELETE &&
2369 status != GOT_STATUS_CONFLICT)
2370 return NULL;
2373 if (!a->header_shown) {
2374 printf("diff %s %s%s\n", a->id_str,
2375 got_worktree_get_root_path(a->worktree),
2376 a->diff_staged ? " (staged changes)" : "");
2377 a->header_shown = 1;
2380 if (a->diff_staged) {
2381 const char *label1 = NULL, *label2 = NULL;
2382 switch (staged_status) {
2383 case GOT_STATUS_MODIFY:
2384 label1 = path;
2385 label2 = path;
2386 break;
2387 case GOT_STATUS_ADD:
2388 label2 = path;
2389 break;
2390 case GOT_STATUS_DELETE:
2391 label1 = path;
2392 break;
2393 default:
2394 return got_error(GOT_ERR_FILE_STATUS);
2396 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2397 label1, label2, a->diff_context, a->ignore_whitespace,
2398 a->repo, stdout);
2401 if (staged_status == GOT_STATUS_ADD ||
2402 staged_status == GOT_STATUS_MODIFY) {
2403 char *id_str;
2404 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2405 8192);
2406 if (err)
2407 goto done;
2408 err = got_object_id_str(&id_str, staged_blob_id);
2409 if (err)
2410 goto done;
2411 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2412 err = got_error_from_errno("asprintf");
2413 free(id_str);
2414 goto done;
2416 free(id_str);
2417 } else if (status != GOT_STATUS_ADD) {
2418 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2419 if (err)
2420 goto done;
2423 if (status != GOT_STATUS_DELETE) {
2424 if (asprintf(&abspath, "%s/%s",
2425 got_worktree_get_root_path(a->worktree), path) == -1) {
2426 err = got_error_from_errno("asprintf");
2427 goto done;
2430 if (dirfd != -1) {
2431 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2432 if (fd == -1) {
2433 err = got_error_from_errno2("openat", abspath);
2434 goto done;
2436 } else {
2437 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2438 if (fd == -1) {
2439 err = got_error_from_errno2("open", abspath);
2440 goto done;
2443 if (fstat(fd, &sb) == -1) {
2444 err = got_error_from_errno2("fstat", abspath);
2445 goto done;
2447 f2 = fdopen(fd, "r");
2448 if (f2 == NULL) {
2449 err = got_error_from_errno2("fdopen", abspath);
2450 goto done;
2452 fd = -1;
2453 } else
2454 sb.st_size = 0;
2456 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2457 a->diff_context, a->ignore_whitespace, stdout);
2458 done:
2459 if (blob1)
2460 got_object_blob_close(blob1);
2461 if (f2 && fclose(f2) == EOF && err == NULL)
2462 err = got_error_from_errno("fclose");
2463 if (fd != -1 && close(fd) == -1 && err == NULL)
2464 err = got_error_from_errno("close");
2465 free(abspath);
2466 return err;
2469 static const struct got_error *
2470 cmd_diff(int argc, char *argv[])
2472 const struct got_error *error;
2473 struct got_repository *repo = NULL;
2474 struct got_worktree *worktree = NULL;
2475 char *cwd = NULL, *repo_path = NULL;
2476 struct got_object_id *id1 = NULL, *id2 = NULL;
2477 const char *id_str1 = NULL, *id_str2 = NULL;
2478 char *label1 = NULL, *label2 = NULL;
2479 int type1, type2;
2480 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2481 const char *errstr;
2482 char *path = NULL;
2484 #ifndef PROFILE
2485 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2486 NULL) == -1)
2487 err(1, "pledge");
2488 #endif
2490 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2491 switch (ch) {
2492 case 'C':
2493 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2494 &errstr);
2495 if (errstr != NULL)
2496 err(1, "-C option %s", errstr);
2497 break;
2498 case 'r':
2499 repo_path = realpath(optarg, NULL);
2500 if (repo_path == NULL)
2501 return got_error_from_errno2("realpath",
2502 optarg);
2503 got_path_strip_trailing_slashes(repo_path);
2504 break;
2505 case 's':
2506 diff_staged = 1;
2507 break;
2508 case 'w':
2509 ignore_whitespace = 1;
2510 break;
2511 default:
2512 usage_diff();
2513 /* NOTREACHED */
2517 argc -= optind;
2518 argv += optind;
2520 cwd = getcwd(NULL, 0);
2521 if (cwd == NULL) {
2522 error = got_error_from_errno("getcwd");
2523 goto done;
2525 error = got_worktree_open(&worktree, cwd);
2526 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2527 goto done;
2528 if (argc <= 1) {
2529 if (worktree == NULL) {
2530 error = got_error(GOT_ERR_NOT_WORKTREE);
2531 goto done;
2533 if (repo_path)
2534 errx(1,
2535 "-r option can't be used when diffing a work tree");
2536 repo_path = strdup(got_worktree_get_repo_path(worktree));
2537 if (repo_path == NULL) {
2538 error = got_error_from_errno("strdup");
2539 goto done;
2541 if (argc == 1) {
2542 error = got_worktree_resolve_path(&path, worktree,
2543 argv[0]);
2544 if (error)
2545 goto done;
2546 } else {
2547 path = strdup("");
2548 if (path == NULL) {
2549 error = got_error_from_errno("strdup");
2550 goto done;
2553 } else if (argc == 2) {
2554 if (diff_staged)
2555 errx(1, "-s option can't be used when diffing "
2556 "objects in repository");
2557 id_str1 = argv[0];
2558 id_str2 = argv[1];
2559 if (worktree && repo_path == NULL) {
2560 repo_path =
2561 strdup(got_worktree_get_repo_path(worktree));
2562 if (repo_path == NULL) {
2563 error = got_error_from_errno("strdup");
2564 goto done;
2567 } else
2568 usage_diff();
2570 if (repo_path == NULL) {
2571 repo_path = getcwd(NULL, 0);
2572 if (repo_path == NULL)
2573 return got_error_from_errno("getcwd");
2576 error = got_repo_open(&repo, repo_path, NULL);
2577 free(repo_path);
2578 if (error != NULL)
2579 goto done;
2581 error = apply_unveil(got_repo_get_path(repo), 1,
2582 worktree ? got_worktree_get_root_path(worktree) : NULL);
2583 if (error)
2584 goto done;
2586 if (argc <= 1) {
2587 struct print_diff_arg arg;
2588 struct got_pathlist_head paths;
2589 char *id_str;
2591 TAILQ_INIT(&paths);
2593 error = got_object_id_str(&id_str,
2594 got_worktree_get_base_commit_id(worktree));
2595 if (error)
2596 goto done;
2597 arg.repo = repo;
2598 arg.worktree = worktree;
2599 arg.diff_context = diff_context;
2600 arg.id_str = id_str;
2601 arg.header_shown = 0;
2602 arg.diff_staged = diff_staged;
2603 arg.ignore_whitespace = ignore_whitespace;
2605 error = got_pathlist_append(&paths, path, NULL);
2606 if (error)
2607 goto done;
2609 error = got_worktree_status(worktree, &paths, repo, print_diff,
2610 &arg, check_cancelled, NULL);
2611 free(id_str);
2612 got_pathlist_free(&paths);
2613 goto done;
2616 error = got_repo_match_object_id(&id1, &label1, id_str1,
2617 GOT_OBJ_TYPE_ANY, 1, repo);
2618 if (error)
2619 goto done;
2621 error = got_repo_match_object_id(&id2, &label2, id_str2,
2622 GOT_OBJ_TYPE_ANY, 1, repo);
2623 if (error)
2624 goto done;
2626 error = got_object_get_type(&type1, repo, id1);
2627 if (error)
2628 goto done;
2630 error = got_object_get_type(&type2, repo, id2);
2631 if (error)
2632 goto done;
2634 if (type1 != type2) {
2635 error = got_error(GOT_ERR_OBJ_TYPE);
2636 goto done;
2639 switch (type1) {
2640 case GOT_OBJ_TYPE_BLOB:
2641 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2642 diff_context, ignore_whitespace, repo, stdout);
2643 break;
2644 case GOT_OBJ_TYPE_TREE:
2645 error = got_diff_objects_as_trees(id1, id2, "", "",
2646 diff_context, ignore_whitespace, repo, stdout);
2647 break;
2648 case GOT_OBJ_TYPE_COMMIT:
2649 printf("diff %s %s\n", label1, label2);
2650 error = got_diff_objects_as_commits(id1, id2, diff_context,
2651 ignore_whitespace, repo, stdout);
2652 break;
2653 default:
2654 error = got_error(GOT_ERR_OBJ_TYPE);
2656 done:
2657 free(label1);
2658 free(label2);
2659 free(id1);
2660 free(id2);
2661 free(path);
2662 if (worktree)
2663 got_worktree_close(worktree);
2664 if (repo) {
2665 const struct got_error *repo_error;
2666 repo_error = got_repo_close(repo);
2667 if (error == NULL)
2668 error = repo_error;
2670 return error;
2673 __dead static void
2674 usage_blame(void)
2676 fprintf(stderr,
2677 "usage: %s blame [-c commit] [-r repository-path] path\n",
2678 getprogname());
2679 exit(1);
2682 struct blame_line {
2683 int annotated;
2684 char *id_str;
2685 char *committer;
2686 char datebuf[11]; /* YYYY-MM-DD + NUL */
2689 struct blame_cb_args {
2690 struct blame_line *lines;
2691 int nlines;
2692 int nlines_prec;
2693 int lineno_cur;
2694 off_t *line_offsets;
2695 FILE *f;
2696 struct got_repository *repo;
2699 static const struct got_error *
2700 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2702 const struct got_error *err = NULL;
2703 struct blame_cb_args *a = arg;
2704 struct blame_line *bline;
2705 char *line = NULL;
2706 size_t linesize = 0;
2707 struct got_commit_object *commit = NULL;
2708 off_t offset;
2709 struct tm tm;
2710 time_t committer_time;
2712 if (nlines != a->nlines ||
2713 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2714 return got_error(GOT_ERR_RANGE);
2716 if (sigint_received)
2717 return got_error(GOT_ERR_ITER_COMPLETED);
2719 if (lineno == -1)
2720 return NULL; /* no change in this commit */
2722 /* Annotate this line. */
2723 bline = &a->lines[lineno - 1];
2724 if (bline->annotated)
2725 return NULL;
2726 err = got_object_id_str(&bline->id_str, id);
2727 if (err)
2728 return err;
2730 err = got_object_open_as_commit(&commit, a->repo, id);
2731 if (err)
2732 goto done;
2734 bline->committer = strdup(got_object_commit_get_committer(commit));
2735 if (bline->committer == NULL) {
2736 err = got_error_from_errno("strdup");
2737 goto done;
2740 committer_time = got_object_commit_get_committer_time(commit);
2741 if (localtime_r(&committer_time, &tm) == NULL)
2742 return got_error_from_errno("localtime_r");
2743 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2744 &tm) >= sizeof(bline->datebuf)) {
2745 err = got_error(GOT_ERR_NO_SPACE);
2746 goto done;
2748 bline->annotated = 1;
2750 /* Print lines annotated so far. */
2751 bline = &a->lines[a->lineno_cur - 1];
2752 if (!bline->annotated)
2753 goto done;
2755 offset = a->line_offsets[a->lineno_cur - 1];
2756 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2757 err = got_error_from_errno("fseeko");
2758 goto done;
2761 while (bline->annotated) {
2762 char *smallerthan, *at, *nl, *committer;
2763 size_t len;
2765 if (getline(&line, &linesize, a->f) == -1) {
2766 if (ferror(a->f))
2767 err = got_error_from_errno("getline");
2768 break;
2771 committer = bline->committer;
2772 smallerthan = strchr(committer, '<');
2773 if (smallerthan && smallerthan[1] != '\0')
2774 committer = smallerthan + 1;
2775 at = strchr(committer, '@');
2776 if (at)
2777 *at = '\0';
2778 len = strlen(committer);
2779 if (len >= 9)
2780 committer[8] = '\0';
2782 nl = strchr(line, '\n');
2783 if (nl)
2784 *nl = '\0';
2785 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2786 bline->id_str, bline->datebuf, committer, line);
2788 a->lineno_cur++;
2789 bline = &a->lines[a->lineno_cur - 1];
2791 done:
2792 if (commit)
2793 got_object_commit_close(commit);
2794 free(line);
2795 return err;
2798 static const struct got_error *
2799 cmd_blame(int argc, char *argv[])
2801 const struct got_error *error;
2802 struct got_repository *repo = NULL;
2803 struct got_worktree *worktree = NULL;
2804 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2805 struct got_object_id *obj_id = NULL;
2806 struct got_object_id *commit_id = NULL;
2807 struct got_blob_object *blob = NULL;
2808 char *commit_id_str = NULL;
2809 struct blame_cb_args bca;
2810 int ch, obj_type, i;
2811 size_t filesize;
2813 memset(&bca, 0, sizeof(bca));
2815 #ifndef PROFILE
2816 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2817 NULL) == -1)
2818 err(1, "pledge");
2819 #endif
2821 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2822 switch (ch) {
2823 case 'c':
2824 commit_id_str = optarg;
2825 break;
2826 case 'r':
2827 repo_path = realpath(optarg, NULL);
2828 if (repo_path == NULL)
2829 return got_error_from_errno2("realpath",
2830 optarg);
2831 got_path_strip_trailing_slashes(repo_path);
2832 break;
2833 default:
2834 usage_blame();
2835 /* NOTREACHED */
2839 argc -= optind;
2840 argv += optind;
2842 if (argc == 1)
2843 path = argv[0];
2844 else
2845 usage_blame();
2847 cwd = getcwd(NULL, 0);
2848 if (cwd == NULL) {
2849 error = got_error_from_errno("getcwd");
2850 goto done;
2852 if (repo_path == NULL) {
2853 error = got_worktree_open(&worktree, cwd);
2854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2855 goto done;
2856 else
2857 error = NULL;
2858 if (worktree) {
2859 repo_path =
2860 strdup(got_worktree_get_repo_path(worktree));
2861 if (repo_path == NULL)
2862 error = got_error_from_errno("strdup");
2863 if (error)
2864 goto done;
2865 } else {
2866 repo_path = strdup(cwd);
2867 if (repo_path == NULL) {
2868 error = got_error_from_errno("strdup");
2869 goto done;
2874 error = got_repo_open(&repo, repo_path, NULL);
2875 if (error != NULL)
2876 goto done;
2878 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2879 if (error)
2880 goto done;
2882 if (worktree) {
2883 const char *prefix = got_worktree_get_path_prefix(worktree);
2884 char *p, *worktree_subdir = cwd +
2885 strlen(got_worktree_get_root_path(worktree));
2886 if (asprintf(&p, "%s%s%s%s%s",
2887 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2888 worktree_subdir, worktree_subdir[0] ? "/" : "",
2889 path) == -1) {
2890 error = got_error_from_errno("asprintf");
2891 goto done;
2893 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2894 free(p);
2895 } else {
2896 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2898 if (error)
2899 goto done;
2901 if (commit_id_str == NULL) {
2902 struct got_reference *head_ref;
2903 error = got_ref_open(&head_ref, repo, worktree ?
2904 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2905 if (error != NULL)
2906 goto done;
2907 error = got_ref_resolve(&commit_id, repo, head_ref);
2908 got_ref_close(head_ref);
2909 if (error != NULL)
2910 goto done;
2911 } else {
2912 error = got_repo_match_object_id(&commit_id, NULL,
2913 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2914 if (error)
2915 goto done;
2918 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2919 if (error)
2920 goto done;
2922 error = got_object_get_type(&obj_type, repo, obj_id);
2923 if (error)
2924 goto done;
2926 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2927 error = got_error(GOT_ERR_OBJ_TYPE);
2928 goto done;
2931 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2932 if (error)
2933 goto done;
2934 bca.f = got_opentemp();
2935 if (bca.f == NULL) {
2936 error = got_error_from_errno("got_opentemp");
2937 goto done;
2939 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2940 &bca.line_offsets, bca.f, blob);
2941 if (error || bca.nlines == 0)
2942 goto done;
2944 /* Don't include \n at EOF in the blame line count. */
2945 if (bca.line_offsets[bca.nlines - 1] == filesize)
2946 bca.nlines--;
2948 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2949 if (bca.lines == NULL) {
2950 error = got_error_from_errno("calloc");
2951 goto done;
2953 bca.lineno_cur = 1;
2954 bca.nlines_prec = 0;
2955 i = bca.nlines;
2956 while (i > 0) {
2957 i /= 10;
2958 bca.nlines_prec++;
2960 bca.repo = repo;
2962 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2963 check_cancelled, NULL);
2964 done:
2965 free(in_repo_path);
2966 free(repo_path);
2967 free(cwd);
2968 free(commit_id);
2969 free(obj_id);
2970 if (blob)
2971 got_object_blob_close(blob);
2972 if (worktree)
2973 got_worktree_close(worktree);
2974 if (repo) {
2975 const struct got_error *repo_error;
2976 repo_error = got_repo_close(repo);
2977 if (error == NULL)
2978 error = repo_error;
2980 if (bca.lines) {
2981 for (i = 0; i < bca.nlines; i++) {
2982 struct blame_line *bline = &bca.lines[i];
2983 free(bline->id_str);
2984 free(bline->committer);
2986 free(bca.lines);
2988 free(bca.line_offsets);
2989 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2990 error = got_error_from_errno("fclose");
2991 return error;
2994 __dead static void
2995 usage_tree(void)
2997 fprintf(stderr,
2998 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2999 getprogname());
3000 exit(1);
3003 static void
3004 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3005 const char *root_path)
3007 int is_root_path = (strcmp(path, root_path) == 0);
3008 const char *modestr = "";
3009 mode_t mode = got_tree_entry_get_mode(te);
3011 path += strlen(root_path);
3012 while (path[0] == '/')
3013 path++;
3015 if (got_object_tree_entry_is_submodule(te))
3016 modestr = "$";
3017 else if (S_ISLNK(mode))
3018 modestr = "@";
3019 else if (S_ISDIR(mode))
3020 modestr = "/";
3021 else if (mode & S_IXUSR)
3022 modestr = "*";
3024 printf("%s%s%s%s%s\n", id ? id : "", path,
3025 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3028 static const struct got_error *
3029 print_tree(const char *path, struct got_object_id *commit_id,
3030 int show_ids, int recurse, const char *root_path,
3031 struct got_repository *repo)
3033 const struct got_error *err = NULL;
3034 struct got_object_id *tree_id = NULL;
3035 struct got_tree_object *tree = NULL;
3036 int nentries, i;
3038 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3039 if (err)
3040 goto done;
3042 err = got_object_open_as_tree(&tree, repo, tree_id);
3043 if (err)
3044 goto done;
3045 nentries = got_object_tree_get_nentries(tree);
3046 for (i = 0; i < nentries; i++) {
3047 struct got_tree_entry *te;
3048 char *id = NULL;
3050 if (sigint_received || sigpipe_received)
3051 break;
3053 te = got_object_tree_get_entry(tree, i);
3054 if (show_ids) {
3055 char *id_str;
3056 err = got_object_id_str(&id_str,
3057 got_tree_entry_get_id(te));
3058 if (err)
3059 goto done;
3060 if (asprintf(&id, "%s ", id_str) == -1) {
3061 err = got_error_from_errno("asprintf");
3062 free(id_str);
3063 goto done;
3065 free(id_str);
3067 print_entry(te, id, path, root_path);
3068 free(id);
3070 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3071 char *child_path;
3072 if (asprintf(&child_path, "%s%s%s", path,
3073 path[0] == '/' && path[1] == '\0' ? "" : "/",
3074 got_tree_entry_get_name(te)) == -1) {
3075 err = got_error_from_errno("asprintf");
3076 goto done;
3078 err = print_tree(child_path, commit_id, show_ids, 1,
3079 root_path, repo);
3080 free(child_path);
3081 if (err)
3082 goto done;
3085 done:
3086 if (tree)
3087 got_object_tree_close(tree);
3088 free(tree_id);
3089 return err;
3092 static const struct got_error *
3093 cmd_tree(int argc, char *argv[])
3095 const struct got_error *error;
3096 struct got_repository *repo = NULL;
3097 struct got_worktree *worktree = NULL;
3098 const char *path;
3099 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3100 struct got_object_id *commit_id = NULL;
3101 char *commit_id_str = NULL;
3102 int show_ids = 0, recurse = 0;
3103 int ch;
3105 #ifndef PROFILE
3106 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3107 NULL) == -1)
3108 err(1, "pledge");
3109 #endif
3111 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3112 switch (ch) {
3113 case 'c':
3114 commit_id_str = optarg;
3115 break;
3116 case 'r':
3117 repo_path = realpath(optarg, NULL);
3118 if (repo_path == NULL)
3119 return got_error_from_errno2("realpath",
3120 optarg);
3121 got_path_strip_trailing_slashes(repo_path);
3122 break;
3123 case 'i':
3124 show_ids = 1;
3125 break;
3126 case 'R':
3127 recurse = 1;
3128 break;
3129 default:
3130 usage_tree();
3131 /* NOTREACHED */
3135 argc -= optind;
3136 argv += optind;
3138 if (argc == 1)
3139 path = argv[0];
3140 else if (argc > 1)
3141 usage_tree();
3142 else
3143 path = NULL;
3145 cwd = getcwd(NULL, 0);
3146 if (cwd == NULL) {
3147 error = got_error_from_errno("getcwd");
3148 goto done;
3150 if (repo_path == NULL) {
3151 error = got_worktree_open(&worktree, cwd);
3152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3153 goto done;
3154 else
3155 error = NULL;
3156 if (worktree) {
3157 repo_path =
3158 strdup(got_worktree_get_repo_path(worktree));
3159 if (repo_path == NULL)
3160 error = got_error_from_errno("strdup");
3161 if (error)
3162 goto done;
3163 } else {
3164 repo_path = strdup(cwd);
3165 if (repo_path == NULL) {
3166 error = got_error_from_errno("strdup");
3167 goto done;
3172 error = got_repo_open(&repo, repo_path, NULL);
3173 if (error != NULL)
3174 goto done;
3176 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3177 if (error)
3178 goto done;
3180 if (path == NULL) {
3181 if (worktree) {
3182 char *p, *worktree_subdir = cwd +
3183 strlen(got_worktree_get_root_path(worktree));
3184 if (asprintf(&p, "%s/%s",
3185 got_worktree_get_path_prefix(worktree),
3186 worktree_subdir) == -1) {
3187 error = got_error_from_errno("asprintf");
3188 goto done;
3190 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3191 free(p);
3192 if (error)
3193 goto done;
3194 } else
3195 path = "/";
3197 if (in_repo_path == NULL) {
3198 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3199 if (error != NULL)
3200 goto done;
3203 if (commit_id_str == NULL) {
3204 struct got_reference *head_ref;
3205 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3206 if (error != NULL)
3207 goto done;
3208 error = got_ref_resolve(&commit_id, repo, head_ref);
3209 got_ref_close(head_ref);
3210 if (error != NULL)
3211 goto done;
3212 } else {
3213 error = got_repo_match_object_id(&commit_id, NULL,
3214 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3215 if (error)
3216 goto done;
3219 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3220 in_repo_path, repo);
3221 done:
3222 free(in_repo_path);
3223 free(repo_path);
3224 free(cwd);
3225 free(commit_id);
3226 if (worktree)
3227 got_worktree_close(worktree);
3228 if (repo) {
3229 const struct got_error *repo_error;
3230 repo_error = got_repo_close(repo);
3231 if (error == NULL)
3232 error = repo_error;
3234 return error;
3237 __dead static void
3238 usage_status(void)
3240 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3241 exit(1);
3244 static const struct got_error *
3245 print_status(void *arg, unsigned char status, unsigned char staged_status,
3246 const char *path, struct got_object_id *blob_id,
3247 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3248 int dirfd, const char *de_name)
3250 if (status == staged_status && (status == GOT_STATUS_DELETE))
3251 status = GOT_STATUS_NO_CHANGE;
3252 printf("%c%c %s\n", status, staged_status, path);
3253 return NULL;
3256 static const struct got_error *
3257 cmd_status(int argc, char *argv[])
3259 const struct got_error *error = NULL;
3260 struct got_repository *repo = NULL;
3261 struct got_worktree *worktree = NULL;
3262 char *cwd = NULL;
3263 struct got_pathlist_head paths;
3264 struct got_pathlist_entry *pe;
3265 int ch;
3267 TAILQ_INIT(&paths);
3269 while ((ch = getopt(argc, argv, "")) != -1) {
3270 switch (ch) {
3271 default:
3272 usage_status();
3273 /* NOTREACHED */
3277 argc -= optind;
3278 argv += optind;
3280 #ifndef PROFILE
3281 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3282 NULL) == -1)
3283 err(1, "pledge");
3284 #endif
3285 cwd = getcwd(NULL, 0);
3286 if (cwd == NULL) {
3287 error = got_error_from_errno("getcwd");
3288 goto done;
3291 error = got_worktree_open(&worktree, cwd);
3292 if (error != NULL)
3293 goto done;
3295 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3296 NULL);
3297 if (error != NULL)
3298 goto done;
3300 error = apply_unveil(got_repo_get_path(repo), 1,
3301 got_worktree_get_root_path(worktree));
3302 if (error)
3303 goto done;
3305 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3306 if (error)
3307 goto done;
3309 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3310 check_cancelled, NULL);
3311 done:
3312 TAILQ_FOREACH(pe, &paths, entry)
3313 free((char *)pe->path);
3314 got_pathlist_free(&paths);
3315 free(cwd);
3316 return error;
3319 __dead static void
3320 usage_ref(void)
3322 fprintf(stderr,
3323 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3324 getprogname());
3325 exit(1);
3328 static const struct got_error *
3329 list_refs(struct got_repository *repo)
3331 static const struct got_error *err = NULL;
3332 struct got_reflist_head refs;
3333 struct got_reflist_entry *re;
3335 SIMPLEQ_INIT(&refs);
3336 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3337 if (err)
3338 return err;
3340 SIMPLEQ_FOREACH(re, &refs, entry) {
3341 char *refstr;
3342 refstr = got_ref_to_str(re->ref);
3343 if (refstr == NULL)
3344 return got_error_from_errno("got_ref_to_str");
3345 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3346 free(refstr);
3349 got_ref_list_free(&refs);
3350 return NULL;
3353 static const struct got_error *
3354 delete_ref(struct got_repository *repo, const char *refname)
3356 const struct got_error *err = NULL;
3357 struct got_reference *ref;
3359 err = got_ref_open(&ref, repo, refname, 0);
3360 if (err)
3361 return err;
3363 err = got_ref_delete(ref, repo);
3364 got_ref_close(ref);
3365 return err;
3368 static const struct got_error *
3369 add_ref(struct got_repository *repo, const char *refname, const char *target)
3371 const struct got_error *err = NULL;
3372 struct got_object_id *id;
3373 struct got_reference *ref = NULL;
3376 * Don't let the user create a reference name with a leading '-'.
3377 * While technically a valid reference name, this case is usually
3378 * an unintended typo.
3380 if (refname[0] == '-')
3381 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3383 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3384 repo);
3385 if (err) {
3386 struct got_reference *target_ref;
3388 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3389 return err;
3390 err = got_ref_open(&target_ref, repo, target, 0);
3391 if (err)
3392 return err;
3393 err = got_ref_resolve(&id, repo, target_ref);
3394 got_ref_close(target_ref);
3395 if (err)
3396 return err;
3399 err = got_ref_alloc(&ref, refname, id);
3400 if (err)
3401 goto done;
3403 err = got_ref_write(ref, repo);
3404 done:
3405 if (ref)
3406 got_ref_close(ref);
3407 free(id);
3408 return err;
3411 static const struct got_error *
3412 add_symref(struct got_repository *repo, const char *refname, const char *target)
3414 const struct got_error *err = NULL;
3415 struct got_reference *ref = NULL;
3416 struct got_reference *target_ref = NULL;
3419 * Don't let the user create a reference name with a leading '-'.
3420 * While technically a valid reference name, this case is usually
3421 * an unintended typo.
3423 if (refname[0] == '-')
3424 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3426 err = got_ref_open(&target_ref, repo, target, 0);
3427 if (err)
3428 return err;
3430 err = got_ref_alloc_symref(&ref, refname, target_ref);
3431 if (err)
3432 goto done;
3434 err = got_ref_write(ref, repo);
3435 done:
3436 if (target_ref)
3437 got_ref_close(target_ref);
3438 if (ref)
3439 got_ref_close(ref);
3440 return err;
3443 static const struct got_error *
3444 cmd_ref(int argc, char *argv[])
3446 const struct got_error *error = NULL;
3447 struct got_repository *repo = NULL;
3448 struct got_worktree *worktree = NULL;
3449 char *cwd = NULL, *repo_path = NULL;
3450 int ch, do_list = 0, create_symref = 0;
3451 const char *delref = NULL;
3453 /* TODO: Add -s option for adding symbolic references. */
3454 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3455 switch (ch) {
3456 case 'd':
3457 delref = optarg;
3458 break;
3459 case 'r':
3460 repo_path = realpath(optarg, NULL);
3461 if (repo_path == NULL)
3462 return got_error_from_errno2("realpath",
3463 optarg);
3464 got_path_strip_trailing_slashes(repo_path);
3465 break;
3466 case 'l':
3467 do_list = 1;
3468 break;
3469 case 's':
3470 create_symref = 1;
3471 break;
3472 default:
3473 usage_ref();
3474 /* NOTREACHED */
3478 if (do_list && delref)
3479 errx(1, "-l and -d options are mutually exclusive\n");
3481 argc -= optind;
3482 argv += optind;
3484 if (do_list || delref) {
3485 if (create_symref)
3486 errx(1, "-s option cannot be used together with the "
3487 "-l or -d options");
3488 if (argc > 0)
3489 usage_ref();
3490 } else if (argc != 2)
3491 usage_ref();
3493 #ifndef PROFILE
3494 if (do_list) {
3495 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3496 NULL) == -1)
3497 err(1, "pledge");
3498 } else {
3499 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3500 "sendfd unveil", NULL) == -1)
3501 err(1, "pledge");
3503 #endif
3504 cwd = getcwd(NULL, 0);
3505 if (cwd == NULL) {
3506 error = got_error_from_errno("getcwd");
3507 goto done;
3510 if (repo_path == NULL) {
3511 error = got_worktree_open(&worktree, cwd);
3512 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3513 goto done;
3514 else
3515 error = NULL;
3516 if (worktree) {
3517 repo_path =
3518 strdup(got_worktree_get_repo_path(worktree));
3519 if (repo_path == NULL)
3520 error = got_error_from_errno("strdup");
3521 if (error)
3522 goto done;
3523 } else {
3524 repo_path = strdup(cwd);
3525 if (repo_path == NULL) {
3526 error = got_error_from_errno("strdup");
3527 goto done;
3532 error = got_repo_open(&repo, repo_path, NULL);
3533 if (error != NULL)
3534 goto done;
3536 error = apply_unveil(got_repo_get_path(repo), do_list,
3537 worktree ? got_worktree_get_root_path(worktree) : NULL);
3538 if (error)
3539 goto done;
3541 if (do_list)
3542 error = list_refs(repo);
3543 else if (delref)
3544 error = delete_ref(repo, delref);
3545 else if (create_symref)
3546 error = add_symref(repo, argv[0], argv[1]);
3547 else
3548 error = add_ref(repo, argv[0], argv[1]);
3549 done:
3550 if (repo)
3551 got_repo_close(repo);
3552 if (worktree)
3553 got_worktree_close(worktree);
3554 free(cwd);
3555 free(repo_path);
3556 return error;
3559 __dead static void
3560 usage_branch(void)
3562 fprintf(stderr,
3563 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3564 "[name]\n", getprogname());
3565 exit(1);
3568 static const struct got_error *
3569 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3570 struct got_reference *ref)
3572 const struct got_error *err = NULL;
3573 const char *refname, *marker = " ";
3574 char *refstr;
3576 refname = got_ref_get_name(ref);
3577 if (worktree && strcmp(refname,
3578 got_worktree_get_head_ref_name(worktree)) == 0) {
3579 struct got_object_id *id = NULL;
3581 err = got_ref_resolve(&id, repo, ref);
3582 if (err)
3583 return err;
3584 if (got_object_id_cmp(id,
3585 got_worktree_get_base_commit_id(worktree)) == 0)
3586 marker = "* ";
3587 else
3588 marker = "~ ";
3589 free(id);
3592 if (strncmp(refname, "refs/heads/", 11) == 0)
3593 refname += 11;
3594 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3595 refname += 18;
3597 refstr = got_ref_to_str(ref);
3598 if (refstr == NULL)
3599 return got_error_from_errno("got_ref_to_str");
3601 printf("%s%s: %s\n", marker, refname, refstr);
3602 free(refstr);
3603 return NULL;
3606 static const struct got_error *
3607 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3609 const char *refname;
3611 if (worktree == NULL)
3612 return got_error(GOT_ERR_NOT_WORKTREE);
3614 refname = got_worktree_get_head_ref_name(worktree);
3616 if (strncmp(refname, "refs/heads/", 11) == 0)
3617 refname += 11;
3618 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3619 refname += 18;
3621 printf("%s\n", refname);
3623 return NULL;
3626 static const struct got_error *
3627 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3629 static const struct got_error *err = NULL;
3630 struct got_reflist_head refs;
3631 struct got_reflist_entry *re;
3632 struct got_reference *temp_ref = NULL;
3633 int rebase_in_progress, histedit_in_progress;
3635 SIMPLEQ_INIT(&refs);
3637 if (worktree) {
3638 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3639 worktree);
3640 if (err)
3641 return err;
3643 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3644 worktree);
3645 if (err)
3646 return err;
3648 if (rebase_in_progress || histedit_in_progress) {
3649 err = got_ref_open(&temp_ref, repo,
3650 got_worktree_get_head_ref_name(worktree), 0);
3651 if (err)
3652 return err;
3653 list_branch(repo, worktree, temp_ref);
3654 got_ref_close(temp_ref);
3658 err = got_ref_list(&refs, repo, "refs/heads",
3659 got_ref_cmp_by_name, NULL);
3660 if (err)
3661 return err;
3663 SIMPLEQ_FOREACH(re, &refs, entry)
3664 list_branch(repo, worktree, re->ref);
3666 got_ref_list_free(&refs);
3667 return NULL;
3670 static const struct got_error *
3671 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3672 const char *branch_name)
3674 const struct got_error *err = NULL;
3675 struct got_reference *ref = NULL;
3676 char *refname;
3678 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3679 return got_error_from_errno("asprintf");
3681 err = got_ref_open(&ref, repo, refname, 0);
3682 if (err)
3683 goto done;
3685 if (worktree &&
3686 strcmp(got_worktree_get_head_ref_name(worktree),
3687 got_ref_get_name(ref)) == 0) {
3688 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3689 "will not delete this work tree's current branch");
3690 goto done;
3693 err = got_ref_delete(ref, repo);
3694 done:
3695 if (ref)
3696 got_ref_close(ref);
3697 free(refname);
3698 return err;
3701 static const struct got_error *
3702 add_branch(struct got_repository *repo, const char *branch_name,
3703 struct got_object_id *base_commit_id)
3705 const struct got_error *err = NULL;
3706 struct got_reference *ref = NULL;
3707 char *base_refname = NULL, *refname = NULL;
3710 * Don't let the user create a branch name with a leading '-'.
3711 * While technically a valid reference name, this case is usually
3712 * an unintended typo.
3714 if (branch_name[0] == '-')
3715 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3717 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3718 err = got_error_from_errno("asprintf");
3719 goto done;
3722 err = got_ref_open(&ref, repo, refname, 0);
3723 if (err == NULL) {
3724 err = got_error(GOT_ERR_BRANCH_EXISTS);
3725 goto done;
3726 } else if (err->code != GOT_ERR_NOT_REF)
3727 goto done;
3729 err = got_ref_alloc(&ref, refname, base_commit_id);
3730 if (err)
3731 goto done;
3733 err = got_ref_write(ref, repo);
3734 done:
3735 if (ref)
3736 got_ref_close(ref);
3737 free(base_refname);
3738 free(refname);
3739 return err;
3742 static const struct got_error *
3743 cmd_branch(int argc, char *argv[])
3745 const struct got_error *error = NULL;
3746 struct got_repository *repo = NULL;
3747 struct got_worktree *worktree = NULL;
3748 char *cwd = NULL, *repo_path = NULL;
3749 int ch, do_list = 0, do_show = 0, do_update = 1;
3750 const char *delref = NULL, *commit_id_arg = NULL;
3751 struct got_reference *ref = NULL;
3752 struct got_pathlist_head paths;
3753 struct got_pathlist_entry *pe;
3754 struct got_object_id *commit_id = NULL;
3755 char *commit_id_str = NULL;
3757 TAILQ_INIT(&paths);
3759 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3760 switch (ch) {
3761 case 'c':
3762 commit_id_arg = optarg;
3763 break;
3764 case 'd':
3765 delref = optarg;
3766 break;
3767 case 'r':
3768 repo_path = realpath(optarg, NULL);
3769 if (repo_path == NULL)
3770 return got_error_from_errno2("realpath",
3771 optarg);
3772 got_path_strip_trailing_slashes(repo_path);
3773 break;
3774 case 'l':
3775 do_list = 1;
3776 break;
3777 case 'n':
3778 do_update = 0;
3779 break;
3780 default:
3781 usage_branch();
3782 /* NOTREACHED */
3786 if (do_list && delref)
3787 errx(1, "-l and -d options are mutually exclusive\n");
3789 argc -= optind;
3790 argv += optind;
3792 if (!do_list && !delref && argc == 0)
3793 do_show = 1;
3795 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3796 errx(1, "-c option can only be used when creating a branch");
3798 if (do_list || delref) {
3799 if (argc > 0)
3800 usage_branch();
3801 } else if (!do_show && argc != 1)
3802 usage_branch();
3804 #ifndef PROFILE
3805 if (do_list || do_show) {
3806 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3807 NULL) == -1)
3808 err(1, "pledge");
3809 } else {
3810 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3811 "sendfd unveil", NULL) == -1)
3812 err(1, "pledge");
3814 #endif
3815 cwd = getcwd(NULL, 0);
3816 if (cwd == NULL) {
3817 error = got_error_from_errno("getcwd");
3818 goto done;
3821 if (repo_path == NULL) {
3822 error = got_worktree_open(&worktree, cwd);
3823 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3824 goto done;
3825 else
3826 error = NULL;
3827 if (worktree) {
3828 repo_path =
3829 strdup(got_worktree_get_repo_path(worktree));
3830 if (repo_path == NULL)
3831 error = got_error_from_errno("strdup");
3832 if (error)
3833 goto done;
3834 } else {
3835 repo_path = strdup(cwd);
3836 if (repo_path == NULL) {
3837 error = got_error_from_errno("strdup");
3838 goto done;
3843 error = got_repo_open(&repo, repo_path, NULL);
3844 if (error != NULL)
3845 goto done;
3847 error = apply_unveil(got_repo_get_path(repo), do_list,
3848 worktree ? got_worktree_get_root_path(worktree) : NULL);
3849 if (error)
3850 goto done;
3852 if (do_show)
3853 error = show_current_branch(repo, worktree);
3854 else if (do_list)
3855 error = list_branches(repo, worktree);
3856 else if (delref)
3857 error = delete_branch(repo, worktree, delref);
3858 else {
3859 if (commit_id_arg == NULL)
3860 commit_id_arg = worktree ?
3861 got_worktree_get_head_ref_name(worktree) :
3862 GOT_REF_HEAD;
3863 error = got_repo_match_object_id(&commit_id, NULL,
3864 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3865 if (error)
3866 goto done;
3867 error = add_branch(repo, argv[0], commit_id);
3868 if (error)
3869 goto done;
3870 if (worktree && do_update) {
3871 int did_something = 0;
3872 char *branch_refname = NULL;
3874 error = got_object_id_str(&commit_id_str, commit_id);
3875 if (error)
3876 goto done;
3877 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3878 worktree);
3879 if (error)
3880 goto done;
3881 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3882 == -1) {
3883 error = got_error_from_errno("asprintf");
3884 goto done;
3886 error = got_ref_open(&ref, repo, branch_refname, 0);
3887 free(branch_refname);
3888 if (error)
3889 goto done;
3890 error = switch_head_ref(ref, commit_id, worktree,
3891 repo);
3892 if (error)
3893 goto done;
3894 error = got_worktree_set_base_commit_id(worktree, repo,
3895 commit_id);
3896 if (error)
3897 goto done;
3898 error = got_worktree_checkout_files(worktree, &paths,
3899 repo, update_progress, &did_something,
3900 check_cancelled, NULL);
3901 if (error)
3902 goto done;
3903 if (did_something)
3904 printf("Updated to commit %s\n", commit_id_str);
3907 done:
3908 if (ref)
3909 got_ref_close(ref);
3910 if (repo)
3911 got_repo_close(repo);
3912 if (worktree)
3913 got_worktree_close(worktree);
3914 free(cwd);
3915 free(repo_path);
3916 free(commit_id);
3917 free(commit_id_str);
3918 TAILQ_FOREACH(pe, &paths, entry)
3919 free((char *)pe->path);
3920 got_pathlist_free(&paths);
3921 return error;
3925 __dead static void
3926 usage_tag(void)
3928 fprintf(stderr,
3929 "usage: %s tag [-c commit] [-r repository] [-l] "
3930 "[-m message] name\n", getprogname());
3931 exit(1);
3934 #if 0
3935 static const struct got_error *
3936 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3938 const struct got_error *err = NULL;
3939 struct got_reflist_entry *re, *se, *new;
3940 struct got_object_id *re_id, *se_id;
3941 struct got_tag_object *re_tag, *se_tag;
3942 time_t re_time, se_time;
3944 SIMPLEQ_FOREACH(re, tags, entry) {
3945 se = SIMPLEQ_FIRST(sorted);
3946 if (se == NULL) {
3947 err = got_reflist_entry_dup(&new, re);
3948 if (err)
3949 return err;
3950 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3951 continue;
3952 } else {
3953 err = got_ref_resolve(&re_id, repo, re->ref);
3954 if (err)
3955 break;
3956 err = got_object_open_as_tag(&re_tag, repo, re_id);
3957 free(re_id);
3958 if (err)
3959 break;
3960 re_time = got_object_tag_get_tagger_time(re_tag);
3961 got_object_tag_close(re_tag);
3964 while (se) {
3965 err = got_ref_resolve(&se_id, repo, re->ref);
3966 if (err)
3967 break;
3968 err = got_object_open_as_tag(&se_tag, repo, se_id);
3969 free(se_id);
3970 if (err)
3971 break;
3972 se_time = got_object_tag_get_tagger_time(se_tag);
3973 got_object_tag_close(se_tag);
3975 if (se_time > re_time) {
3976 err = got_reflist_entry_dup(&new, re);
3977 if (err)
3978 return err;
3979 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3980 break;
3982 se = SIMPLEQ_NEXT(se, entry);
3983 continue;
3986 done:
3987 return err;
3989 #endif
3991 static const struct got_error *
3992 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3994 static const struct got_error *err = NULL;
3995 struct got_reflist_head refs;
3996 struct got_reflist_entry *re;
3998 SIMPLEQ_INIT(&refs);
4000 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4001 if (err)
4002 return err;
4004 SIMPLEQ_FOREACH(re, &refs, entry) {
4005 const char *refname;
4006 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4007 char datebuf[26];
4008 const char *tagger;
4009 time_t tagger_time;
4010 struct got_object_id *id;
4011 struct got_tag_object *tag;
4012 struct got_commit_object *commit = NULL;
4014 refname = got_ref_get_name(re->ref);
4015 if (strncmp(refname, "refs/tags/", 10) != 0)
4016 continue;
4017 refname += 10;
4018 refstr = got_ref_to_str(re->ref);
4019 if (refstr == NULL) {
4020 err = got_error_from_errno("got_ref_to_str");
4021 break;
4023 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4024 free(refstr);
4026 err = got_ref_resolve(&id, repo, re->ref);
4027 if (err)
4028 break;
4029 err = got_object_open_as_tag(&tag, repo, id);
4030 if (err) {
4031 if (err->code != GOT_ERR_OBJ_TYPE) {
4032 free(id);
4033 break;
4035 /* "lightweight" tag */
4036 err = got_object_open_as_commit(&commit, repo, id);
4037 if (err) {
4038 free(id);
4039 break;
4041 tagger = got_object_commit_get_committer(commit);
4042 tagger_time =
4043 got_object_commit_get_committer_time(commit);
4044 err = got_object_id_str(&id_str, id);
4045 free(id);
4046 if (err)
4047 break;
4048 } else {
4049 free(id);
4050 tagger = got_object_tag_get_tagger(tag);
4051 tagger_time = got_object_tag_get_tagger_time(tag);
4052 err = got_object_id_str(&id_str,
4053 got_object_tag_get_object_id(tag));
4054 if (err)
4055 break;
4057 printf("from: %s\n", tagger);
4058 datestr = get_datestr(&tagger_time, datebuf);
4059 if (datestr)
4060 printf("date: %s UTC\n", datestr);
4061 if (commit)
4062 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4063 else {
4064 switch (got_object_tag_get_object_type(tag)) {
4065 case GOT_OBJ_TYPE_BLOB:
4066 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4067 id_str);
4068 break;
4069 case GOT_OBJ_TYPE_TREE:
4070 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4071 id_str);
4072 break;
4073 case GOT_OBJ_TYPE_COMMIT:
4074 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4075 id_str);
4076 break;
4077 case GOT_OBJ_TYPE_TAG:
4078 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4079 id_str);
4080 break;
4081 default:
4082 break;
4085 free(id_str);
4086 if (commit) {
4087 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4088 if (err)
4089 break;
4090 got_object_commit_close(commit);
4091 } else {
4092 tagmsg0 = strdup(got_object_tag_get_message(tag));
4093 got_object_tag_close(tag);
4094 if (tagmsg0 == NULL) {
4095 err = got_error_from_errno("strdup");
4096 break;
4100 tagmsg = tagmsg0;
4101 do {
4102 line = strsep(&tagmsg, "\n");
4103 if (line)
4104 printf(" %s\n", line);
4105 } while (line);
4106 free(tagmsg0);
4109 got_ref_list_free(&refs);
4110 return NULL;
4113 static const struct got_error *
4114 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4115 const char *tag_name, const char *repo_path)
4117 const struct got_error *err = NULL;
4118 char *template = NULL, *initial_content = NULL;
4119 char *editor = NULL;
4120 int fd = -1;
4122 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4123 err = got_error_from_errno("asprintf");
4124 goto done;
4127 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4128 commit_id_str, tag_name) == -1) {
4129 err = got_error_from_errno("asprintf");
4130 goto done;
4133 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4134 if (err)
4135 goto done;
4137 dprintf(fd, initial_content);
4138 close(fd);
4140 err = get_editor(&editor);
4141 if (err)
4142 goto done;
4143 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4144 done:
4145 free(initial_content);
4146 free(template);
4147 free(editor);
4149 /* Editor is done; we can now apply unveil(2) */
4150 if (err == NULL) {
4151 err = apply_unveil(repo_path, 0, NULL);
4152 if (err) {
4153 free(*tagmsg);
4154 *tagmsg = NULL;
4157 return err;
4160 static const struct got_error *
4161 add_tag(struct got_repository *repo, const char *tag_name,
4162 const char *commit_arg, const char *tagmsg_arg)
4164 const struct got_error *err = NULL;
4165 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4166 char *label = NULL, *commit_id_str = NULL;
4167 struct got_reference *ref = NULL;
4168 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4169 char *tagmsg_path = NULL, *tag_id_str = NULL;
4170 int preserve_tagmsg = 0;
4173 * Don't let the user create a tag name with a leading '-'.
4174 * While technically a valid reference name, this case is usually
4175 * an unintended typo.
4177 if (tag_name[0] == '-')
4178 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4180 err = get_author(&tagger, repo);
4181 if (err)
4182 return err;
4184 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4185 GOT_OBJ_TYPE_COMMIT, 1, repo);
4186 if (err)
4187 goto done;
4189 err = got_object_id_str(&commit_id_str, commit_id);
4190 if (err)
4191 goto done;
4193 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4194 refname = strdup(tag_name);
4195 if (refname == NULL) {
4196 err = got_error_from_errno("strdup");
4197 goto done;
4199 tag_name += 10;
4200 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4201 err = got_error_from_errno("asprintf");
4202 goto done;
4205 err = got_ref_open(&ref, repo, refname, 0);
4206 if (err == NULL) {
4207 err = got_error(GOT_ERR_TAG_EXISTS);
4208 goto done;
4209 } else if (err->code != GOT_ERR_NOT_REF)
4210 goto done;
4212 if (tagmsg_arg == NULL) {
4213 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4214 tag_name, got_repo_get_path(repo));
4215 if (err) {
4216 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4217 tagmsg_path != NULL)
4218 preserve_tagmsg = 1;
4219 goto done;
4223 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4224 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4225 if (err) {
4226 if (tagmsg_path)
4227 preserve_tagmsg = 1;
4228 goto done;
4231 err = got_ref_alloc(&ref, refname, tag_id);
4232 if (err) {
4233 if (tagmsg_path)
4234 preserve_tagmsg = 1;
4235 goto done;
4238 err = got_ref_write(ref, repo);
4239 if (err) {
4240 if (tagmsg_path)
4241 preserve_tagmsg = 1;
4242 goto done;
4245 err = got_object_id_str(&tag_id_str, tag_id);
4246 if (err) {
4247 if (tagmsg_path)
4248 preserve_tagmsg = 1;
4249 goto done;
4251 printf("Created tag %s\n", tag_id_str);
4252 done:
4253 if (preserve_tagmsg) {
4254 fprintf(stderr, "%s: tag message preserved in %s\n",
4255 getprogname(), tagmsg_path);
4256 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4257 err = got_error_from_errno2("unlink", tagmsg_path);
4258 free(tag_id_str);
4259 if (ref)
4260 got_ref_close(ref);
4261 free(commit_id);
4262 free(commit_id_str);
4263 free(refname);
4264 free(tagmsg);
4265 free(tagmsg_path);
4266 free(tagger);
4267 return err;
4270 static const struct got_error *
4271 cmd_tag(int argc, char *argv[])
4273 const struct got_error *error = NULL;
4274 struct got_repository *repo = NULL;
4275 struct got_worktree *worktree = NULL;
4276 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4277 char *gitconfig_path = NULL;
4278 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4279 int ch, do_list = 0;
4281 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4282 switch (ch) {
4283 case 'c':
4284 commit_id_arg = optarg;
4285 break;
4286 case 'm':
4287 tagmsg = optarg;
4288 break;
4289 case 'r':
4290 repo_path = realpath(optarg, NULL);
4291 if (repo_path == NULL)
4292 return got_error_from_errno2("realpath",
4293 optarg);
4294 got_path_strip_trailing_slashes(repo_path);
4295 break;
4296 case 'l':
4297 do_list = 1;
4298 break;
4299 default:
4300 usage_tag();
4301 /* NOTREACHED */
4305 argc -= optind;
4306 argv += optind;
4308 if (do_list) {
4309 if (commit_id_arg != NULL)
4310 errx(1, "-c option can only be used when creating a tag");
4311 if (tagmsg)
4312 errx(1, "-l and -m options are mutually exclusive");
4313 if (argc > 0)
4314 usage_tag();
4315 } else if (argc != 1)
4316 usage_tag();
4318 tag_name = argv[0];
4320 #ifndef PROFILE
4321 if (do_list) {
4322 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4323 NULL) == -1)
4324 err(1, "pledge");
4325 } else {
4326 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4327 "sendfd unveil", NULL) == -1)
4328 err(1, "pledge");
4330 #endif
4331 cwd = getcwd(NULL, 0);
4332 if (cwd == NULL) {
4333 error = got_error_from_errno("getcwd");
4334 goto done;
4337 if (repo_path == NULL) {
4338 error = got_worktree_open(&worktree, cwd);
4339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4340 goto done;
4341 else
4342 error = NULL;
4343 if (worktree) {
4344 repo_path =
4345 strdup(got_worktree_get_repo_path(worktree));
4346 if (repo_path == NULL)
4347 error = got_error_from_errno("strdup");
4348 if (error)
4349 goto done;
4350 } else {
4351 repo_path = strdup(cwd);
4352 if (repo_path == NULL) {
4353 error = got_error_from_errno("strdup");
4354 goto done;
4359 if (do_list) {
4360 error = got_repo_open(&repo, repo_path, NULL);
4361 if (error != NULL)
4362 goto done;
4363 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4364 if (error)
4365 goto done;
4366 error = list_tags(repo, worktree);
4367 } else {
4368 error = get_gitconfig_path(&gitconfig_path);
4369 if (error)
4370 goto done;
4371 error = got_repo_open(&repo, repo_path, gitconfig_path);
4372 if (error != NULL)
4373 goto done;
4375 if (tagmsg) {
4376 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4377 if (error)
4378 goto done;
4381 if (commit_id_arg == NULL) {
4382 struct got_reference *head_ref;
4383 struct got_object_id *commit_id;
4384 error = got_ref_open(&head_ref, repo,
4385 worktree ? got_worktree_get_head_ref_name(worktree)
4386 : GOT_REF_HEAD, 0);
4387 if (error)
4388 goto done;
4389 error = got_ref_resolve(&commit_id, repo, head_ref);
4390 got_ref_close(head_ref);
4391 if (error)
4392 goto done;
4393 error = got_object_id_str(&commit_id_str, commit_id);
4394 free(commit_id);
4395 if (error)
4396 goto done;
4399 error = add_tag(repo, tag_name,
4400 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4402 done:
4403 if (repo)
4404 got_repo_close(repo);
4405 if (worktree)
4406 got_worktree_close(worktree);
4407 free(cwd);
4408 free(repo_path);
4409 free(gitconfig_path);
4410 free(commit_id_str);
4411 return error;
4414 __dead static void
4415 usage_add(void)
4417 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4418 getprogname());
4419 exit(1);
4422 static const struct got_error *
4423 add_progress(void *arg, unsigned char status, const char *path)
4425 while (path[0] == '/')
4426 path++;
4427 printf("%c %s\n", status, path);
4428 return NULL;
4431 static const struct got_error *
4432 cmd_add(int argc, char *argv[])
4434 const struct got_error *error = NULL;
4435 struct got_repository *repo = NULL;
4436 struct got_worktree *worktree = NULL;
4437 char *cwd = NULL;
4438 struct got_pathlist_head paths;
4439 struct got_pathlist_entry *pe;
4440 int ch, can_recurse = 0, no_ignores = 0;
4442 TAILQ_INIT(&paths);
4444 while ((ch = getopt(argc, argv, "IR")) != -1) {
4445 switch (ch) {
4446 case 'I':
4447 no_ignores = 1;
4448 break;
4449 case 'R':
4450 can_recurse = 1;
4451 break;
4452 default:
4453 usage_add();
4454 /* NOTREACHED */
4458 argc -= optind;
4459 argv += optind;
4461 #ifndef PROFILE
4462 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4463 NULL) == -1)
4464 err(1, "pledge");
4465 #endif
4466 if (argc < 1)
4467 usage_add();
4469 cwd = getcwd(NULL, 0);
4470 if (cwd == NULL) {
4471 error = got_error_from_errno("getcwd");
4472 goto done;
4475 error = got_worktree_open(&worktree, cwd);
4476 if (error)
4477 goto done;
4479 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4480 NULL);
4481 if (error != NULL)
4482 goto done;
4484 error = apply_unveil(got_repo_get_path(repo), 1,
4485 got_worktree_get_root_path(worktree));
4486 if (error)
4487 goto done;
4489 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4490 if (error)
4491 goto done;
4493 if (!can_recurse && no_ignores) {
4494 error = got_error_msg(GOT_ERR_BAD_PATH,
4495 "disregarding ignores requires -R option");
4496 goto done;
4500 if (!can_recurse) {
4501 char *ondisk_path;
4502 struct stat sb;
4503 TAILQ_FOREACH(pe, &paths, entry) {
4504 if (asprintf(&ondisk_path, "%s/%s",
4505 got_worktree_get_root_path(worktree),
4506 pe->path) == -1) {
4507 error = got_error_from_errno("asprintf");
4508 goto done;
4510 if (lstat(ondisk_path, &sb) == -1) {
4511 if (errno == ENOENT) {
4512 free(ondisk_path);
4513 continue;
4515 error = got_error_from_errno2("lstat",
4516 ondisk_path);
4517 free(ondisk_path);
4518 goto done;
4520 free(ondisk_path);
4521 if (S_ISDIR(sb.st_mode)) {
4522 error = got_error_msg(GOT_ERR_BAD_PATH,
4523 "adding directories requires -R option");
4524 goto done;
4529 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4530 NULL, repo, no_ignores);
4531 done:
4532 if (repo)
4533 got_repo_close(repo);
4534 if (worktree)
4535 got_worktree_close(worktree);
4536 TAILQ_FOREACH(pe, &paths, entry)
4537 free((char *)pe->path);
4538 got_pathlist_free(&paths);
4539 free(cwd);
4540 return error;
4543 __dead static void
4544 usage_remove(void)
4546 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4547 getprogname());
4548 exit(1);
4551 static const struct got_error *
4552 print_remove_status(void *arg, unsigned char status,
4553 unsigned char staged_status, const char *path)
4555 while (path[0] == '/')
4556 path++;
4557 if (status == GOT_STATUS_NONEXISTENT)
4558 return NULL;
4559 if (status == staged_status && (status == GOT_STATUS_DELETE))
4560 status = GOT_STATUS_NO_CHANGE;
4561 printf("%c%c %s\n", status, staged_status, path);
4562 return NULL;
4565 static const struct got_error *
4566 cmd_remove(int argc, char *argv[])
4568 const struct got_error *error = NULL;
4569 struct got_worktree *worktree = NULL;
4570 struct got_repository *repo = NULL;
4571 char *cwd = NULL;
4572 struct got_pathlist_head paths;
4573 struct got_pathlist_entry *pe;
4574 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4576 TAILQ_INIT(&paths);
4578 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4579 switch (ch) {
4580 case 'f':
4581 delete_local_mods = 1;
4582 break;
4583 case 'k':
4584 keep_on_disk = 1;
4585 break;
4586 case 'R':
4587 can_recurse = 1;
4588 break;
4589 default:
4590 usage_remove();
4591 /* NOTREACHED */
4595 argc -= optind;
4596 argv += optind;
4598 #ifndef PROFILE
4599 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4600 NULL) == -1)
4601 err(1, "pledge");
4602 #endif
4603 if (argc < 1)
4604 usage_remove();
4606 cwd = getcwd(NULL, 0);
4607 if (cwd == NULL) {
4608 error = got_error_from_errno("getcwd");
4609 goto done;
4611 error = got_worktree_open(&worktree, cwd);
4612 if (error)
4613 goto done;
4615 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4616 NULL);
4617 if (error)
4618 goto done;
4620 error = apply_unveil(got_repo_get_path(repo), 1,
4621 got_worktree_get_root_path(worktree));
4622 if (error)
4623 goto done;
4625 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4626 if (error)
4627 goto done;
4629 if (!can_recurse) {
4630 char *ondisk_path;
4631 struct stat sb;
4632 TAILQ_FOREACH(pe, &paths, entry) {
4633 if (asprintf(&ondisk_path, "%s/%s",
4634 got_worktree_get_root_path(worktree),
4635 pe->path) == -1) {
4636 error = got_error_from_errno("asprintf");
4637 goto done;
4639 if (lstat(ondisk_path, &sb) == -1) {
4640 if (errno == ENOENT) {
4641 free(ondisk_path);
4642 continue;
4644 error = got_error_from_errno2("lstat",
4645 ondisk_path);
4646 free(ondisk_path);
4647 goto done;
4649 free(ondisk_path);
4650 if (S_ISDIR(sb.st_mode)) {
4651 error = got_error_msg(GOT_ERR_BAD_PATH,
4652 "removing directories requires -R option");
4653 goto done;
4658 error = got_worktree_schedule_delete(worktree, &paths,
4659 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4660 done:
4661 if (repo)
4662 got_repo_close(repo);
4663 if (worktree)
4664 got_worktree_close(worktree);
4665 TAILQ_FOREACH(pe, &paths, entry)
4666 free((char *)pe->path);
4667 got_pathlist_free(&paths);
4668 free(cwd);
4669 return error;
4672 __dead static void
4673 usage_revert(void)
4675 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4676 "path ...\n", getprogname());
4677 exit(1);
4680 static const struct got_error *
4681 revert_progress(void *arg, unsigned char status, const char *path)
4683 if (status == GOT_STATUS_UNVERSIONED)
4684 return NULL;
4686 while (path[0] == '/')
4687 path++;
4688 printf("%c %s\n", status, path);
4689 return NULL;
4692 struct choose_patch_arg {
4693 FILE *patch_script_file;
4694 const char *action;
4697 static const struct got_error *
4698 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4699 int nchanges, const char *action)
4701 char *line = NULL;
4702 size_t linesize = 0;
4703 ssize_t linelen;
4705 switch (status) {
4706 case GOT_STATUS_ADD:
4707 printf("A %s\n%s this addition? [y/n] ", path, action);
4708 break;
4709 case GOT_STATUS_DELETE:
4710 printf("D %s\n%s this deletion? [y/n] ", path, action);
4711 break;
4712 case GOT_STATUS_MODIFY:
4713 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4714 return got_error_from_errno("fseek");
4715 printf(GOT_COMMIT_SEP_STR);
4716 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4717 printf("%s", line);
4718 if (ferror(patch_file))
4719 return got_error_from_errno("getline");
4720 printf(GOT_COMMIT_SEP_STR);
4721 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4722 path, n, nchanges, action);
4723 break;
4724 default:
4725 return got_error_path(path, GOT_ERR_FILE_STATUS);
4728 return NULL;
4731 static const struct got_error *
4732 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4733 FILE *patch_file, int n, int nchanges)
4735 const struct got_error *err = NULL;
4736 char *line = NULL;
4737 size_t linesize = 0;
4738 ssize_t linelen;
4739 int resp = ' ';
4740 struct choose_patch_arg *a = arg;
4742 *choice = GOT_PATCH_CHOICE_NONE;
4744 if (a->patch_script_file) {
4745 char *nl;
4746 err = show_change(status, path, patch_file, n, nchanges,
4747 a->action);
4748 if (err)
4749 return err;
4750 linelen = getline(&line, &linesize, a->patch_script_file);
4751 if (linelen == -1) {
4752 if (ferror(a->patch_script_file))
4753 return got_error_from_errno("getline");
4754 return NULL;
4756 nl = strchr(line, '\n');
4757 if (nl)
4758 *nl = '\0';
4759 if (strcmp(line, "y") == 0) {
4760 *choice = GOT_PATCH_CHOICE_YES;
4761 printf("y\n");
4762 } else if (strcmp(line, "n") == 0) {
4763 *choice = GOT_PATCH_CHOICE_NO;
4764 printf("n\n");
4765 } else if (strcmp(line, "q") == 0 &&
4766 status == GOT_STATUS_MODIFY) {
4767 *choice = GOT_PATCH_CHOICE_QUIT;
4768 printf("q\n");
4769 } else
4770 printf("invalid response '%s'\n", line);
4771 free(line);
4772 return NULL;
4775 while (resp != 'y' && resp != 'n' && resp != 'q') {
4776 err = show_change(status, path, patch_file, n, nchanges,
4777 a->action);
4778 if (err)
4779 return err;
4780 resp = getchar();
4781 if (resp == '\n')
4782 resp = getchar();
4783 if (status == GOT_STATUS_MODIFY) {
4784 if (resp != 'y' && resp != 'n' && resp != 'q') {
4785 printf("invalid response '%c'\n", resp);
4786 resp = ' ';
4788 } else if (resp != 'y' && resp != 'n') {
4789 printf("invalid response '%c'\n", resp);
4790 resp = ' ';
4794 if (resp == 'y')
4795 *choice = GOT_PATCH_CHOICE_YES;
4796 else if (resp == 'n')
4797 *choice = GOT_PATCH_CHOICE_NO;
4798 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4799 *choice = GOT_PATCH_CHOICE_QUIT;
4801 return NULL;
4805 static const struct got_error *
4806 cmd_revert(int argc, char *argv[])
4808 const struct got_error *error = NULL;
4809 struct got_worktree *worktree = NULL;
4810 struct got_repository *repo = NULL;
4811 char *cwd = NULL, *path = NULL;
4812 struct got_pathlist_head paths;
4813 struct got_pathlist_entry *pe;
4814 int ch, can_recurse = 0, pflag = 0;
4815 FILE *patch_script_file = NULL;
4816 const char *patch_script_path = NULL;
4817 struct choose_patch_arg cpa;
4819 TAILQ_INIT(&paths);
4821 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4822 switch (ch) {
4823 case 'p':
4824 pflag = 1;
4825 break;
4826 case 'F':
4827 patch_script_path = optarg;
4828 break;
4829 case 'R':
4830 can_recurse = 1;
4831 break;
4832 default:
4833 usage_revert();
4834 /* NOTREACHED */
4838 argc -= optind;
4839 argv += optind;
4841 #ifndef PROFILE
4842 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4843 "unveil", NULL) == -1)
4844 err(1, "pledge");
4845 #endif
4846 if (argc < 1)
4847 usage_revert();
4848 if (patch_script_path && !pflag)
4849 errx(1, "-F option can only be used together with -p option");
4851 cwd = getcwd(NULL, 0);
4852 if (cwd == NULL) {
4853 error = got_error_from_errno("getcwd");
4854 goto done;
4856 error = got_worktree_open(&worktree, cwd);
4857 if (error)
4858 goto done;
4860 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4861 NULL);
4862 if (error != NULL)
4863 goto done;
4865 if (patch_script_path) {
4866 patch_script_file = fopen(patch_script_path, "r");
4867 if (patch_script_file == NULL) {
4868 error = got_error_from_errno2("fopen",
4869 patch_script_path);
4870 goto done;
4873 error = apply_unveil(got_repo_get_path(repo), 1,
4874 got_worktree_get_root_path(worktree));
4875 if (error)
4876 goto done;
4878 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4879 if (error)
4880 goto done;
4882 if (!can_recurse) {
4883 char *ondisk_path;
4884 struct stat sb;
4885 TAILQ_FOREACH(pe, &paths, entry) {
4886 if (asprintf(&ondisk_path, "%s/%s",
4887 got_worktree_get_root_path(worktree),
4888 pe->path) == -1) {
4889 error = got_error_from_errno("asprintf");
4890 goto done;
4892 if (lstat(ondisk_path, &sb) == -1) {
4893 if (errno == ENOENT) {
4894 free(ondisk_path);
4895 continue;
4897 error = got_error_from_errno2("lstat",
4898 ondisk_path);
4899 free(ondisk_path);
4900 goto done;
4902 free(ondisk_path);
4903 if (S_ISDIR(sb.st_mode)) {
4904 error = got_error_msg(GOT_ERR_BAD_PATH,
4905 "reverting directories requires -R option");
4906 goto done;
4911 cpa.patch_script_file = patch_script_file;
4912 cpa.action = "revert";
4913 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4914 pflag ? choose_patch : NULL, &cpa, repo);
4915 done:
4916 if (patch_script_file && fclose(patch_script_file) == EOF &&
4917 error == NULL)
4918 error = got_error_from_errno2("fclose", patch_script_path);
4919 if (repo)
4920 got_repo_close(repo);
4921 if (worktree)
4922 got_worktree_close(worktree);
4923 free(path);
4924 free(cwd);
4925 return error;
4928 __dead static void
4929 usage_commit(void)
4931 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4932 getprogname());
4933 exit(1);
4936 struct collect_commit_logmsg_arg {
4937 const char *cmdline_log;
4938 const char *editor;
4939 const char *worktree_path;
4940 const char *branch_name;
4941 const char *repo_path;
4942 char *logmsg_path;
4946 static const struct got_error *
4947 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4948 void *arg)
4950 char *initial_content = NULL;
4951 struct got_pathlist_entry *pe;
4952 const struct got_error *err = NULL;
4953 char *template = NULL;
4954 struct collect_commit_logmsg_arg *a = arg;
4955 int fd;
4956 size_t len;
4958 /* if a message was specified on the command line, just use it */
4959 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4960 len = strlen(a->cmdline_log) + 1;
4961 *logmsg = malloc(len + 1);
4962 if (*logmsg == NULL)
4963 return got_error_from_errno("malloc");
4964 strlcpy(*logmsg, a->cmdline_log, len);
4965 return NULL;
4968 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4969 return got_error_from_errno("asprintf");
4971 if (asprintf(&initial_content,
4972 "\n# changes to be committed on branch %s:\n",
4973 a->branch_name) == -1)
4974 return got_error_from_errno("asprintf");
4976 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4977 if (err)
4978 goto done;
4980 dprintf(fd, initial_content);
4982 TAILQ_FOREACH(pe, commitable_paths, entry) {
4983 struct got_commitable *ct = pe->data;
4984 dprintf(fd, "# %c %s\n",
4985 got_commitable_get_status(ct),
4986 got_commitable_get_path(ct));
4988 close(fd);
4990 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4991 done:
4992 free(initial_content);
4993 free(template);
4995 /* Editor is done; we can now apply unveil(2) */
4996 if (err == NULL) {
4997 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4998 if (err) {
4999 free(*logmsg);
5000 *logmsg = NULL;
5003 return err;
5006 static const struct got_error *
5007 cmd_commit(int argc, char *argv[])
5009 const struct got_error *error = NULL;
5010 struct got_worktree *worktree = NULL;
5011 struct got_repository *repo = NULL;
5012 char *cwd = NULL, *id_str = NULL;
5013 struct got_object_id *id = NULL;
5014 const char *logmsg = NULL;
5015 struct collect_commit_logmsg_arg cl_arg;
5016 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5017 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5018 struct got_pathlist_head paths;
5020 TAILQ_INIT(&paths);
5021 cl_arg.logmsg_path = NULL;
5023 while ((ch = getopt(argc, argv, "m:")) != -1) {
5024 switch (ch) {
5025 case 'm':
5026 logmsg = optarg;
5027 break;
5028 default:
5029 usage_commit();
5030 /* NOTREACHED */
5034 argc -= optind;
5035 argv += optind;
5037 #ifndef PROFILE
5038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5039 "unveil", NULL) == -1)
5040 err(1, "pledge");
5041 #endif
5042 cwd = getcwd(NULL, 0);
5043 if (cwd == NULL) {
5044 error = got_error_from_errno("getcwd");
5045 goto done;
5047 error = got_worktree_open(&worktree, cwd);
5048 if (error)
5049 goto done;
5051 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5052 if (error)
5053 goto done;
5054 if (rebase_in_progress) {
5055 error = got_error(GOT_ERR_REBASING);
5056 goto done;
5059 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5060 worktree);
5061 if (error)
5062 goto done;
5064 error = get_gitconfig_path(&gitconfig_path);
5065 if (error)
5066 goto done;
5067 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5068 gitconfig_path);
5069 if (error != NULL)
5070 goto done;
5072 error = get_author(&author, repo);
5073 if (error)
5074 return error;
5077 * unveil(2) traverses exec(2); if an editor is used we have
5078 * to apply unveil after the log message has been written.
5080 if (logmsg == NULL || strlen(logmsg) == 0)
5081 error = get_editor(&editor);
5082 else
5083 error = apply_unveil(got_repo_get_path(repo), 0,
5084 got_worktree_get_root_path(worktree));
5085 if (error)
5086 goto done;
5088 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5089 if (error)
5090 goto done;
5092 cl_arg.editor = editor;
5093 cl_arg.cmdline_log = logmsg;
5094 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5095 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5096 if (!histedit_in_progress) {
5097 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5098 error = got_error(GOT_ERR_COMMIT_BRANCH);
5099 goto done;
5101 cl_arg.branch_name += 11;
5103 cl_arg.repo_path = got_repo_get_path(repo);
5104 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5105 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5106 if (error) {
5107 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5108 cl_arg.logmsg_path != NULL)
5109 preserve_logmsg = 1;
5110 goto done;
5113 error = got_object_id_str(&id_str, id);
5114 if (error)
5115 goto done;
5116 printf("Created commit %s\n", id_str);
5117 done:
5118 if (preserve_logmsg) {
5119 fprintf(stderr, "%s: log message preserved in %s\n",
5120 getprogname(), cl_arg.logmsg_path);
5121 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5122 error == NULL)
5123 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5124 free(cl_arg.logmsg_path);
5125 if (repo)
5126 got_repo_close(repo);
5127 if (worktree)
5128 got_worktree_close(worktree);
5129 free(cwd);
5130 free(id_str);
5131 free(gitconfig_path);
5132 free(editor);
5133 free(author);
5134 return error;
5137 __dead static void
5138 usage_cherrypick(void)
5140 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5141 exit(1);
5144 static const struct got_error *
5145 cmd_cherrypick(int argc, char *argv[])
5147 const struct got_error *error = NULL;
5148 struct got_worktree *worktree = NULL;
5149 struct got_repository *repo = NULL;
5150 char *cwd = NULL, *commit_id_str = NULL;
5151 struct got_object_id *commit_id = NULL;
5152 struct got_commit_object *commit = NULL;
5153 struct got_object_qid *pid;
5154 struct got_reference *head_ref = NULL;
5155 int ch, did_something = 0;
5157 while ((ch = getopt(argc, argv, "")) != -1) {
5158 switch (ch) {
5159 default:
5160 usage_cherrypick();
5161 /* NOTREACHED */
5165 argc -= optind;
5166 argv += optind;
5168 #ifndef PROFILE
5169 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5170 "unveil", NULL) == -1)
5171 err(1, "pledge");
5172 #endif
5173 if (argc != 1)
5174 usage_cherrypick();
5176 cwd = getcwd(NULL, 0);
5177 if (cwd == NULL) {
5178 error = got_error_from_errno("getcwd");
5179 goto done;
5181 error = got_worktree_open(&worktree, cwd);
5182 if (error)
5183 goto done;
5185 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5186 NULL);
5187 if (error != NULL)
5188 goto done;
5190 error = apply_unveil(got_repo_get_path(repo), 0,
5191 got_worktree_get_root_path(worktree));
5192 if (error)
5193 goto done;
5195 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5196 GOT_OBJ_TYPE_COMMIT, repo);
5197 if (error != NULL) {
5198 struct got_reference *ref;
5199 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5200 goto done;
5201 error = got_ref_open(&ref, repo, argv[0], 0);
5202 if (error != NULL)
5203 goto done;
5204 error = got_ref_resolve(&commit_id, repo, ref);
5205 got_ref_close(ref);
5206 if (error != NULL)
5207 goto done;
5209 error = got_object_id_str(&commit_id_str, commit_id);
5210 if (error)
5211 goto done;
5213 error = got_ref_open(&head_ref, repo,
5214 got_worktree_get_head_ref_name(worktree), 0);
5215 if (error != NULL)
5216 goto done;
5218 error = check_same_branch(commit_id, head_ref, NULL, repo);
5219 if (error) {
5220 if (error->code != GOT_ERR_ANCESTRY)
5221 goto done;
5222 error = NULL;
5223 } else {
5224 error = got_error(GOT_ERR_SAME_BRANCH);
5225 goto done;
5228 error = got_object_open_as_commit(&commit, repo, commit_id);
5229 if (error)
5230 goto done;
5231 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5232 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5233 commit_id, repo, update_progress, &did_something, check_cancelled,
5234 NULL);
5235 if (error != NULL)
5236 goto done;
5238 if (did_something)
5239 printf("Merged commit %s\n", commit_id_str);
5240 done:
5241 if (commit)
5242 got_object_commit_close(commit);
5243 free(commit_id_str);
5244 if (head_ref)
5245 got_ref_close(head_ref);
5246 if (worktree)
5247 got_worktree_close(worktree);
5248 if (repo)
5249 got_repo_close(repo);
5250 return error;
5253 __dead static void
5254 usage_backout(void)
5256 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5257 exit(1);
5260 static const struct got_error *
5261 cmd_backout(int argc, char *argv[])
5263 const struct got_error *error = NULL;
5264 struct got_worktree *worktree = NULL;
5265 struct got_repository *repo = NULL;
5266 char *cwd = NULL, *commit_id_str = NULL;
5267 struct got_object_id *commit_id = NULL;
5268 struct got_commit_object *commit = NULL;
5269 struct got_object_qid *pid;
5270 struct got_reference *head_ref = NULL;
5271 int ch, did_something = 0;
5273 while ((ch = getopt(argc, argv, "")) != -1) {
5274 switch (ch) {
5275 default:
5276 usage_backout();
5277 /* NOTREACHED */
5281 argc -= optind;
5282 argv += optind;
5284 #ifndef PROFILE
5285 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5286 "unveil", NULL) == -1)
5287 err(1, "pledge");
5288 #endif
5289 if (argc != 1)
5290 usage_backout();
5292 cwd = getcwd(NULL, 0);
5293 if (cwd == NULL) {
5294 error = got_error_from_errno("getcwd");
5295 goto done;
5297 error = got_worktree_open(&worktree, cwd);
5298 if (error)
5299 goto done;
5301 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5302 NULL);
5303 if (error != NULL)
5304 goto done;
5306 error = apply_unveil(got_repo_get_path(repo), 0,
5307 got_worktree_get_root_path(worktree));
5308 if (error)
5309 goto done;
5311 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5312 GOT_OBJ_TYPE_COMMIT, repo);
5313 if (error != NULL) {
5314 struct got_reference *ref;
5315 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5316 goto done;
5317 error = got_ref_open(&ref, repo, argv[0], 0);
5318 if (error != NULL)
5319 goto done;
5320 error = got_ref_resolve(&commit_id, repo, ref);
5321 got_ref_close(ref);
5322 if (error != NULL)
5323 goto done;
5325 error = got_object_id_str(&commit_id_str, commit_id);
5326 if (error)
5327 goto done;
5329 error = got_ref_open(&head_ref, repo,
5330 got_worktree_get_head_ref_name(worktree), 0);
5331 if (error != NULL)
5332 goto done;
5334 error = check_same_branch(commit_id, head_ref, NULL, repo);
5335 if (error)
5336 goto done;
5338 error = got_object_open_as_commit(&commit, repo, commit_id);
5339 if (error)
5340 goto done;
5341 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5342 if (pid == NULL) {
5343 error = got_error(GOT_ERR_ROOT_COMMIT);
5344 goto done;
5347 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5348 update_progress, &did_something, check_cancelled, NULL);
5349 if (error != NULL)
5350 goto done;
5352 if (did_something)
5353 printf("Backed out commit %s\n", commit_id_str);
5354 done:
5355 if (commit)
5356 got_object_commit_close(commit);
5357 free(commit_id_str);
5358 if (head_ref)
5359 got_ref_close(head_ref);
5360 if (worktree)
5361 got_worktree_close(worktree);
5362 if (repo)
5363 got_repo_close(repo);
5364 return error;
5367 __dead static void
5368 usage_rebase(void)
5370 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5371 getprogname());
5372 exit(1);
5375 void
5376 trim_logmsg(char *logmsg, int limit)
5378 char *nl;
5379 size_t len;
5381 len = strlen(logmsg);
5382 if (len > limit)
5383 len = limit;
5384 logmsg[len] = '\0';
5385 nl = strchr(logmsg, '\n');
5386 if (nl)
5387 *nl = '\0';
5390 static const struct got_error *
5391 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5393 const struct got_error *err;
5394 char *logmsg0 = NULL;
5395 const char *s;
5397 err = got_object_commit_get_logmsg(&logmsg0, commit);
5398 if (err)
5399 return err;
5401 s = logmsg0;
5402 while (isspace((unsigned char)s[0]))
5403 s++;
5405 *logmsg = strdup(s);
5406 if (*logmsg == NULL) {
5407 err = got_error_from_errno("strdup");
5408 goto done;
5411 trim_logmsg(*logmsg, limit);
5412 done:
5413 free(logmsg0);
5414 return err;
5417 static const struct got_error *
5418 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5420 const struct got_error *err;
5421 struct got_commit_object *commit = NULL;
5422 char *id_str = NULL, *logmsg = NULL;
5424 err = got_object_open_as_commit(&commit, repo, id);
5425 if (err)
5426 return err;
5428 err = got_object_id_str(&id_str, id);
5429 if (err)
5430 goto done;
5432 id_str[12] = '\0';
5434 err = get_short_logmsg(&logmsg, 42, commit);
5435 if (err)
5436 goto done;
5438 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5439 done:
5440 free(id_str);
5441 got_object_commit_close(commit);
5442 free(logmsg);
5443 return err;
5446 static const struct got_error *
5447 show_rebase_progress(struct got_commit_object *commit,
5448 struct got_object_id *old_id, struct got_object_id *new_id)
5450 const struct got_error *err;
5451 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5453 err = got_object_id_str(&old_id_str, old_id);
5454 if (err)
5455 goto done;
5457 if (new_id) {
5458 err = got_object_id_str(&new_id_str, new_id);
5459 if (err)
5460 goto done;
5463 old_id_str[12] = '\0';
5464 if (new_id_str)
5465 new_id_str[12] = '\0';
5467 err = get_short_logmsg(&logmsg, 42, commit);
5468 if (err)
5469 goto done;
5471 printf("%s -> %s: %s\n", old_id_str,
5472 new_id_str ? new_id_str : "no-op change", logmsg);
5473 done:
5474 free(old_id_str);
5475 free(new_id_str);
5476 free(logmsg);
5477 return err;
5480 static const struct got_error *
5481 rebase_progress(void *arg, unsigned char status, const char *path)
5483 unsigned char *rebase_status = arg;
5485 while (path[0] == '/')
5486 path++;
5487 printf("%c %s\n", status, path);
5489 if (*rebase_status == GOT_STATUS_CONFLICT)
5490 return NULL;
5491 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5492 *rebase_status = status;
5493 return NULL;
5496 static const struct got_error *
5497 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5498 struct got_reference *branch, struct got_reference *new_base_branch,
5499 struct got_reference *tmp_branch, struct got_repository *repo)
5501 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5502 return got_worktree_rebase_complete(worktree, fileindex,
5503 new_base_branch, tmp_branch, branch, repo);
5506 static const struct got_error *
5507 rebase_commit(struct got_pathlist_head *merged_paths,
5508 struct got_worktree *worktree, struct got_fileindex *fileindex,
5509 struct got_reference *tmp_branch,
5510 struct got_object_id *commit_id, struct got_repository *repo)
5512 const struct got_error *error;
5513 struct got_commit_object *commit;
5514 struct got_object_id *new_commit_id;
5516 error = got_object_open_as_commit(&commit, repo, commit_id);
5517 if (error)
5518 return error;
5520 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5521 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5522 if (error) {
5523 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5524 goto done;
5525 error = show_rebase_progress(commit, commit_id, NULL);
5526 } else {
5527 error = show_rebase_progress(commit, commit_id, new_commit_id);
5528 free(new_commit_id);
5530 done:
5531 got_object_commit_close(commit);
5532 return error;
5535 struct check_path_prefix_arg {
5536 const char *path_prefix;
5537 size_t len;
5538 int errcode;
5541 static const struct got_error *
5542 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5543 struct got_blob_object *blob2, struct got_object_id *id1,
5544 struct got_object_id *id2, const char *path1, const char *path2,
5545 mode_t mode1, mode_t mode2, struct got_repository *repo)
5547 struct check_path_prefix_arg *a = arg;
5549 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5550 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5551 return got_error(a->errcode);
5553 return NULL;
5556 static const struct got_error *
5557 check_path_prefix(struct got_object_id *parent_id,
5558 struct got_object_id *commit_id, const char *path_prefix,
5559 int errcode, struct got_repository *repo)
5561 const struct got_error *err;
5562 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5563 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5564 struct check_path_prefix_arg cpp_arg;
5566 if (got_path_is_root_dir(path_prefix))
5567 return NULL;
5569 err = got_object_open_as_commit(&commit, repo, commit_id);
5570 if (err)
5571 goto done;
5573 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5574 if (err)
5575 goto done;
5577 err = got_object_open_as_tree(&tree1, repo,
5578 got_object_commit_get_tree_id(parent_commit));
5579 if (err)
5580 goto done;
5582 err = got_object_open_as_tree(&tree2, repo,
5583 got_object_commit_get_tree_id(commit));
5584 if (err)
5585 goto done;
5587 cpp_arg.path_prefix = path_prefix;
5588 while (cpp_arg.path_prefix[0] == '/')
5589 cpp_arg.path_prefix++;
5590 cpp_arg.len = strlen(cpp_arg.path_prefix);
5591 cpp_arg.errcode = errcode;
5592 err = got_diff_tree(tree1, tree2, "", "", repo,
5593 check_path_prefix_in_diff, &cpp_arg, 0);
5594 done:
5595 if (tree1)
5596 got_object_tree_close(tree1);
5597 if (tree2)
5598 got_object_tree_close(tree2);
5599 if (commit)
5600 got_object_commit_close(commit);
5601 if (parent_commit)
5602 got_object_commit_close(parent_commit);
5603 return err;
5606 static const struct got_error *
5607 collect_commits(struct got_object_id_queue *commits,
5608 struct got_object_id *initial_commit_id,
5609 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5610 const char *path_prefix, int path_prefix_errcode,
5611 struct got_repository *repo)
5613 const struct got_error *err = NULL;
5614 struct got_commit_graph *graph = NULL;
5615 struct got_object_id *parent_id = NULL;
5616 struct got_object_qid *qid;
5617 struct got_object_id *commit_id = initial_commit_id;
5619 err = got_commit_graph_open(&graph, "/", 1);
5620 if (err)
5621 return err;
5623 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5624 check_cancelled, NULL);
5625 if (err)
5626 goto done;
5627 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5628 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5629 check_cancelled, NULL);
5630 if (err) {
5631 if (err->code == GOT_ERR_ITER_COMPLETED) {
5632 err = got_error_msg(GOT_ERR_ANCESTRY,
5633 "ran out of commits to rebase before "
5634 "youngest common ancestor commit has "
5635 "been reached?!?");
5637 goto done;
5638 } else {
5639 err = check_path_prefix(parent_id, commit_id,
5640 path_prefix, path_prefix_errcode, repo);
5641 if (err)
5642 goto done;
5644 err = got_object_qid_alloc(&qid, commit_id);
5645 if (err)
5646 goto done;
5647 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5648 commit_id = parent_id;
5651 done:
5652 got_commit_graph_close(graph);
5653 return err;
5656 static const struct got_error *
5657 cmd_rebase(int argc, char *argv[])
5659 const struct got_error *error = NULL;
5660 struct got_worktree *worktree = NULL;
5661 struct got_repository *repo = NULL;
5662 struct got_fileindex *fileindex = NULL;
5663 char *cwd = NULL;
5664 struct got_reference *branch = NULL;
5665 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5666 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5667 struct got_object_id *resume_commit_id = NULL;
5668 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5669 struct got_commit_object *commit = NULL;
5670 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5671 int histedit_in_progress = 0;
5672 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5673 struct got_object_id_queue commits;
5674 struct got_pathlist_head merged_paths;
5675 const struct got_object_id_queue *parent_ids;
5676 struct got_object_qid *qid, *pid;
5678 SIMPLEQ_INIT(&commits);
5679 TAILQ_INIT(&merged_paths);
5681 while ((ch = getopt(argc, argv, "ac")) != -1) {
5682 switch (ch) {
5683 case 'a':
5684 abort_rebase = 1;
5685 break;
5686 case 'c':
5687 continue_rebase = 1;
5688 break;
5689 default:
5690 usage_rebase();
5691 /* NOTREACHED */
5695 argc -= optind;
5696 argv += optind;
5698 #ifndef PROFILE
5699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5700 "unveil", NULL) == -1)
5701 err(1, "pledge");
5702 #endif
5703 if (abort_rebase && continue_rebase)
5704 usage_rebase();
5705 else if (abort_rebase || continue_rebase) {
5706 if (argc != 0)
5707 usage_rebase();
5708 } else if (argc != 1)
5709 usage_rebase();
5711 cwd = getcwd(NULL, 0);
5712 if (cwd == NULL) {
5713 error = got_error_from_errno("getcwd");
5714 goto done;
5716 error = got_worktree_open(&worktree, cwd);
5717 if (error)
5718 goto done;
5720 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5721 NULL);
5722 if (error != NULL)
5723 goto done;
5725 error = apply_unveil(got_repo_get_path(repo), 0,
5726 got_worktree_get_root_path(worktree));
5727 if (error)
5728 goto done;
5730 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5731 worktree);
5732 if (error)
5733 goto done;
5734 if (histedit_in_progress) {
5735 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5736 goto done;
5739 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5740 if (error)
5741 goto done;
5743 if (abort_rebase) {
5744 int did_something;
5745 if (!rebase_in_progress) {
5746 error = got_error(GOT_ERR_NOT_REBASING);
5747 goto done;
5749 error = got_worktree_rebase_continue(&resume_commit_id,
5750 &new_base_branch, &tmp_branch, &branch, &fileindex,
5751 worktree, repo);
5752 if (error)
5753 goto done;
5754 printf("Switching work tree to %s\n",
5755 got_ref_get_symref_target(new_base_branch));
5756 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5757 new_base_branch, update_progress, &did_something);
5758 if (error)
5759 goto done;
5760 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5761 goto done; /* nothing else to do */
5764 if (continue_rebase) {
5765 if (!rebase_in_progress) {
5766 error = got_error(GOT_ERR_NOT_REBASING);
5767 goto done;
5769 error = got_worktree_rebase_continue(&resume_commit_id,
5770 &new_base_branch, &tmp_branch, &branch, &fileindex,
5771 worktree, repo);
5772 if (error)
5773 goto done;
5775 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5776 resume_commit_id, repo);
5777 if (error)
5778 goto done;
5780 yca_id = got_object_id_dup(resume_commit_id);
5781 if (yca_id == NULL) {
5782 error = got_error_from_errno("got_object_id_dup");
5783 goto done;
5785 } else {
5786 error = got_ref_open(&branch, repo, argv[0], 0);
5787 if (error != NULL)
5788 goto done;
5791 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5792 if (error)
5793 goto done;
5795 if (!continue_rebase) {
5796 struct got_object_id *base_commit_id;
5798 base_commit_id = got_worktree_get_base_commit_id(worktree);
5799 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5800 base_commit_id, branch_head_commit_id, repo,
5801 check_cancelled, NULL);
5802 if (error)
5803 goto done;
5804 if (yca_id == NULL) {
5805 error = got_error_msg(GOT_ERR_ANCESTRY,
5806 "specified branch shares no common ancestry "
5807 "with work tree's branch");
5808 goto done;
5811 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5812 if (error) {
5813 if (error->code != GOT_ERR_ANCESTRY)
5814 goto done;
5815 error = NULL;
5816 } else {
5817 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5818 "specified branch resolves to a commit which "
5819 "is already contained in work tree's branch");
5820 goto done;
5822 error = got_worktree_rebase_prepare(&new_base_branch,
5823 &tmp_branch, &fileindex, worktree, branch, repo);
5824 if (error)
5825 goto done;
5828 commit_id = branch_head_commit_id;
5829 error = got_object_open_as_commit(&commit, repo, commit_id);
5830 if (error)
5831 goto done;
5833 parent_ids = got_object_commit_get_parent_ids(commit);
5834 pid = SIMPLEQ_FIRST(parent_ids);
5835 if (pid == NULL) {
5836 if (!continue_rebase) {
5837 int did_something;
5838 error = got_worktree_rebase_abort(worktree, fileindex,
5839 repo, new_base_branch, update_progress,
5840 &did_something);
5841 if (error)
5842 goto done;
5843 printf("Rebase of %s aborted\n",
5844 got_ref_get_name(branch));
5846 error = got_error(GOT_ERR_EMPTY_REBASE);
5847 goto done;
5849 error = collect_commits(&commits, commit_id, pid->id,
5850 yca_id, got_worktree_get_path_prefix(worktree),
5851 GOT_ERR_REBASE_PATH, repo);
5852 got_object_commit_close(commit);
5853 commit = NULL;
5854 if (error)
5855 goto done;
5857 if (SIMPLEQ_EMPTY(&commits)) {
5858 if (continue_rebase) {
5859 error = rebase_complete(worktree, fileindex,
5860 branch, new_base_branch, tmp_branch, repo);
5861 goto done;
5862 } else {
5863 /* Fast-forward the reference of the branch. */
5864 struct got_object_id *new_head_commit_id;
5865 char *id_str;
5866 error = got_ref_resolve(&new_head_commit_id, repo,
5867 new_base_branch);
5868 if (error)
5869 goto done;
5870 error = got_object_id_str(&id_str, new_head_commit_id);
5871 printf("Forwarding %s to commit %s\n",
5872 got_ref_get_name(branch), id_str);
5873 free(id_str);
5874 error = got_ref_change_ref(branch,
5875 new_head_commit_id);
5876 if (error)
5877 goto done;
5881 pid = NULL;
5882 SIMPLEQ_FOREACH(qid, &commits, entry) {
5883 commit_id = qid->id;
5884 parent_id = pid ? pid->id : yca_id;
5885 pid = qid;
5887 error = got_worktree_rebase_merge_files(&merged_paths,
5888 worktree, fileindex, parent_id, commit_id, repo,
5889 rebase_progress, &rebase_status, check_cancelled, NULL);
5890 if (error)
5891 goto done;
5893 if (rebase_status == GOT_STATUS_CONFLICT) {
5894 error = show_rebase_merge_conflict(qid->id, repo);
5895 if (error)
5896 goto done;
5897 got_worktree_rebase_pathlist_free(&merged_paths);
5898 break;
5901 error = rebase_commit(&merged_paths, worktree, fileindex,
5902 tmp_branch, commit_id, repo);
5903 got_worktree_rebase_pathlist_free(&merged_paths);
5904 if (error)
5905 goto done;
5908 if (rebase_status == GOT_STATUS_CONFLICT) {
5909 error = got_worktree_rebase_postpone(worktree, fileindex);
5910 if (error)
5911 goto done;
5912 error = got_error_msg(GOT_ERR_CONFLICTS,
5913 "conflicts must be resolved before rebasing can continue");
5914 } else
5915 error = rebase_complete(worktree, fileindex, branch,
5916 new_base_branch, tmp_branch, repo);
5917 done:
5918 got_object_id_queue_free(&commits);
5919 free(branch_head_commit_id);
5920 free(resume_commit_id);
5921 free(yca_id);
5922 if (commit)
5923 got_object_commit_close(commit);
5924 if (branch)
5925 got_ref_close(branch);
5926 if (new_base_branch)
5927 got_ref_close(new_base_branch);
5928 if (tmp_branch)
5929 got_ref_close(tmp_branch);
5930 if (worktree)
5931 got_worktree_close(worktree);
5932 if (repo)
5933 got_repo_close(repo);
5934 return error;
5937 __dead static void
5938 usage_histedit(void)
5940 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5941 getprogname());
5942 exit(1);
5945 #define GOT_HISTEDIT_PICK 'p'
5946 #define GOT_HISTEDIT_EDIT 'e'
5947 #define GOT_HISTEDIT_FOLD 'f'
5948 #define GOT_HISTEDIT_DROP 'd'
5949 #define GOT_HISTEDIT_MESG 'm'
5951 static struct got_histedit_cmd {
5952 unsigned char code;
5953 const char *name;
5954 const char *desc;
5955 } got_histedit_cmds[] = {
5956 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5957 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5958 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5959 "be used" },
5960 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5961 { GOT_HISTEDIT_MESG, "mesg",
5962 "single-line log message for commit above (open editor if empty)" },
5965 struct got_histedit_list_entry {
5966 TAILQ_ENTRY(got_histedit_list_entry) entry;
5967 struct got_object_id *commit_id;
5968 const struct got_histedit_cmd *cmd;
5969 char *logmsg;
5971 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5973 static const struct got_error *
5974 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5975 FILE *f, struct got_repository *repo)
5977 const struct got_error *err = NULL;
5978 char *logmsg = NULL, *id_str = NULL;
5979 struct got_commit_object *commit = NULL;
5980 int n;
5982 err = got_object_open_as_commit(&commit, repo, commit_id);
5983 if (err)
5984 goto done;
5986 err = get_short_logmsg(&logmsg, 34, commit);
5987 if (err)
5988 goto done;
5990 err = got_object_id_str(&id_str, commit_id);
5991 if (err)
5992 goto done;
5994 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5995 if (n < 0)
5996 err = got_ferror(f, GOT_ERR_IO);
5997 done:
5998 if (commit)
5999 got_object_commit_close(commit);
6000 free(id_str);
6001 free(logmsg);
6002 return err;
6005 static const struct got_error *
6006 histedit_write_commit_list(struct got_object_id_queue *commits,
6007 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6009 const struct got_error *err = NULL;
6010 struct got_object_qid *qid;
6012 if (SIMPLEQ_EMPTY(commits))
6013 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6015 SIMPLEQ_FOREACH(qid, commits, entry) {
6016 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6017 f, repo);
6018 if (err)
6019 break;
6020 if (edit_logmsg_only) {
6021 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6022 if (n < 0) {
6023 err = got_ferror(f, GOT_ERR_IO);
6024 break;
6029 return err;
6032 static const struct got_error *
6033 write_cmd_list(FILE *f, const char *branch_name,
6034 struct got_object_id_queue *commits)
6036 const struct got_error *err = NULL;
6037 int n, i;
6038 char *id_str;
6039 struct got_object_qid *qid;
6041 qid = SIMPLEQ_FIRST(commits);
6042 err = got_object_id_str(&id_str, qid->id);
6043 if (err)
6044 return err;
6046 n = fprintf(f,
6047 "# Editing the history of branch '%s' starting at\n"
6048 "# commit %s\n"
6049 "# Commits will be processed in order from top to "
6050 "bottom of this file.\n", branch_name, id_str);
6051 if (n < 0) {
6052 err = got_ferror(f, GOT_ERR_IO);
6053 goto done;
6056 n = fprintf(f, "# Available histedit commands:\n");
6057 if (n < 0) {
6058 err = got_ferror(f, GOT_ERR_IO);
6059 goto done;
6062 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6063 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6064 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6065 cmd->desc);
6066 if (n < 0) {
6067 err = got_ferror(f, GOT_ERR_IO);
6068 break;
6071 done:
6072 free(id_str);
6073 return err;
6076 static const struct got_error *
6077 histedit_syntax_error(int lineno)
6079 static char msg[42];
6080 int ret;
6082 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6083 lineno);
6084 if (ret == -1 || ret >= sizeof(msg))
6085 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6087 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6090 static const struct got_error *
6091 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6092 char *logmsg, struct got_repository *repo)
6094 const struct got_error *err;
6095 struct got_commit_object *folded_commit = NULL;
6096 char *id_str, *folded_logmsg = NULL;
6098 err = got_object_id_str(&id_str, hle->commit_id);
6099 if (err)
6100 return err;
6102 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6103 if (err)
6104 goto done;
6106 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6107 if (err)
6108 goto done;
6109 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6110 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6111 folded_logmsg) == -1) {
6112 err = got_error_from_errno("asprintf");
6114 done:
6115 if (folded_commit)
6116 got_object_commit_close(folded_commit);
6117 free(id_str);
6118 free(folded_logmsg);
6119 return err;
6122 static struct got_histedit_list_entry *
6123 get_folded_commits(struct got_histedit_list_entry *hle)
6125 struct got_histedit_list_entry *prev, *folded = NULL;
6127 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6128 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6129 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6130 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6131 folded = prev;
6132 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6135 return folded;
6138 static const struct got_error *
6139 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6140 struct got_repository *repo)
6142 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6143 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6144 const struct got_error *err = NULL;
6145 struct got_commit_object *commit = NULL;
6146 int fd;
6147 struct got_histedit_list_entry *folded = NULL;
6149 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6150 if (err)
6151 return err;
6153 folded = get_folded_commits(hle);
6154 if (folded) {
6155 while (folded != hle) {
6156 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6157 folded = TAILQ_NEXT(folded, entry);
6158 continue;
6160 err = append_folded_commit_msg(&new_msg, folded,
6161 logmsg, repo);
6162 if (err)
6163 goto done;
6164 free(logmsg);
6165 logmsg = new_msg;
6166 folded = TAILQ_NEXT(folded, entry);
6170 err = got_object_id_str(&id_str, hle->commit_id);
6171 if (err)
6172 goto done;
6173 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6174 if (err)
6175 goto done;
6176 if (asprintf(&new_msg,
6177 "%s\n# original log message of commit %s: %s",
6178 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6179 err = got_error_from_errno("asprintf");
6180 goto done;
6182 free(logmsg);
6183 logmsg = new_msg;
6185 err = got_object_id_str(&id_str, hle->commit_id);
6186 if (err)
6187 goto done;
6189 err = got_opentemp_named_fd(&logmsg_path, &fd,
6190 GOT_TMPDIR_STR "/got-logmsg");
6191 if (err)
6192 goto done;
6194 dprintf(fd, logmsg);
6195 close(fd);
6197 err = get_editor(&editor);
6198 if (err)
6199 goto done;
6201 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6202 if (err) {
6203 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6204 goto done;
6205 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6207 done:
6208 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6209 err = got_error_from_errno2("unlink", logmsg_path);
6210 free(logmsg_path);
6211 free(logmsg);
6212 free(orig_logmsg);
6213 free(editor);
6214 if (commit)
6215 got_object_commit_close(commit);
6216 return err;
6219 static const struct got_error *
6220 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6221 FILE *f, struct got_repository *repo)
6223 const struct got_error *err = NULL;
6224 char *line = NULL, *p, *end;
6225 size_t size;
6226 ssize_t len;
6227 int lineno = 0, i;
6228 const struct got_histedit_cmd *cmd;
6229 struct got_object_id *commit_id = NULL;
6230 struct got_histedit_list_entry *hle = NULL;
6232 for (;;) {
6233 len = getline(&line, &size, f);
6234 if (len == -1) {
6235 const struct got_error *getline_err;
6236 if (feof(f))
6237 break;
6238 getline_err = got_error_from_errno("getline");
6239 err = got_ferror(f, getline_err->code);
6240 break;
6242 lineno++;
6243 p = line;
6244 while (isspace((unsigned char)p[0]))
6245 p++;
6246 if (p[0] == '#' || p[0] == '\0') {
6247 free(line);
6248 line = NULL;
6249 continue;
6251 cmd = NULL;
6252 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6253 cmd = &got_histedit_cmds[i];
6254 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6255 isspace((unsigned char)p[strlen(cmd->name)])) {
6256 p += strlen(cmd->name);
6257 break;
6259 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6260 p++;
6261 break;
6264 if (i == nitems(got_histedit_cmds)) {
6265 err = histedit_syntax_error(lineno);
6266 break;
6268 while (isspace((unsigned char)p[0]))
6269 p++;
6270 if (cmd->code == GOT_HISTEDIT_MESG) {
6271 if (hle == NULL || hle->logmsg != NULL) {
6272 err = got_error(GOT_ERR_HISTEDIT_CMD);
6273 break;
6275 if (p[0] == '\0') {
6276 err = histedit_edit_logmsg(hle, repo);
6277 if (err)
6278 break;
6279 } else {
6280 hle->logmsg = strdup(p);
6281 if (hle->logmsg == NULL) {
6282 err = got_error_from_errno("strdup");
6283 break;
6286 free(line);
6287 line = NULL;
6288 continue;
6289 } else {
6290 end = p;
6291 while (end[0] && !isspace((unsigned char)end[0]))
6292 end++;
6293 *end = '\0';
6295 err = got_object_resolve_id_str(&commit_id, repo, p);
6296 if (err) {
6297 /* override error code */
6298 err = histedit_syntax_error(lineno);
6299 break;
6302 hle = malloc(sizeof(*hle));
6303 if (hle == NULL) {
6304 err = got_error_from_errno("malloc");
6305 break;
6307 hle->cmd = cmd;
6308 hle->commit_id = commit_id;
6309 hle->logmsg = NULL;
6310 commit_id = NULL;
6311 free(line);
6312 line = NULL;
6313 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6316 free(line);
6317 free(commit_id);
6318 return err;
6321 static const struct got_error *
6322 histedit_check_script(struct got_histedit_list *histedit_cmds,
6323 struct got_object_id_queue *commits, struct got_repository *repo)
6325 const struct got_error *err = NULL;
6326 struct got_object_qid *qid;
6327 struct got_histedit_list_entry *hle;
6328 static char msg[92];
6329 char *id_str;
6331 if (TAILQ_EMPTY(histedit_cmds))
6332 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6333 "histedit script contains no commands");
6334 if (SIMPLEQ_EMPTY(commits))
6335 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6337 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6338 struct got_histedit_list_entry *hle2;
6339 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6340 if (hle == hle2)
6341 continue;
6342 if (got_object_id_cmp(hle->commit_id,
6343 hle2->commit_id) != 0)
6344 continue;
6345 err = got_object_id_str(&id_str, hle->commit_id);
6346 if (err)
6347 return err;
6348 snprintf(msg, sizeof(msg), "commit %s is listed "
6349 "more than once in histedit script", id_str);
6350 free(id_str);
6351 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6355 SIMPLEQ_FOREACH(qid, commits, entry) {
6356 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6357 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6358 break;
6360 if (hle == NULL) {
6361 err = got_object_id_str(&id_str, qid->id);
6362 if (err)
6363 return err;
6364 snprintf(msg, sizeof(msg),
6365 "commit %s missing from histedit script", id_str);
6366 free(id_str);
6367 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6371 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6372 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6373 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6374 "last commit in histedit script cannot be folded");
6376 return NULL;
6379 static const struct got_error *
6380 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6381 const char *path, struct got_object_id_queue *commits,
6382 struct got_repository *repo)
6384 const struct got_error *err = NULL;
6385 char *editor;
6386 FILE *f = NULL;
6388 err = get_editor(&editor);
6389 if (err)
6390 return err;
6392 if (spawn_editor(editor, path) == -1) {
6393 err = got_error_from_errno("failed spawning editor");
6394 goto done;
6397 f = fopen(path, "r");
6398 if (f == NULL) {
6399 err = got_error_from_errno("fopen");
6400 goto done;
6402 err = histedit_parse_list(histedit_cmds, f, repo);
6403 if (err)
6404 goto done;
6406 err = histedit_check_script(histedit_cmds, commits, repo);
6407 done:
6408 if (f && fclose(f) != 0 && err == NULL)
6409 err = got_error_from_errno("fclose");
6410 free(editor);
6411 return err;
6414 static const struct got_error *
6415 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6416 struct got_object_id_queue *, const char *, const char *,
6417 struct got_repository *);
6419 static const struct got_error *
6420 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6421 struct got_object_id_queue *commits, const char *branch_name,
6422 int edit_logmsg_only, struct got_repository *repo)
6424 const struct got_error *err;
6425 FILE *f = NULL;
6426 char *path = NULL;
6428 err = got_opentemp_named(&path, &f, "got-histedit");
6429 if (err)
6430 return err;
6432 err = write_cmd_list(f, branch_name, commits);
6433 if (err)
6434 goto done;
6436 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6437 if (err)
6438 goto done;
6440 if (edit_logmsg_only) {
6441 rewind(f);
6442 err = histedit_parse_list(histedit_cmds, f, repo);
6443 } else {
6444 if (fclose(f) != 0) {
6445 err = got_error_from_errno("fclose");
6446 goto done;
6448 f = NULL;
6449 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6450 if (err) {
6451 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6452 err->code != GOT_ERR_HISTEDIT_CMD)
6453 goto done;
6454 err = histedit_edit_list_retry(histedit_cmds, err,
6455 commits, path, branch_name, repo);
6458 done:
6459 if (f && fclose(f) != 0 && err == NULL)
6460 err = got_error_from_errno("fclose");
6461 if (path && unlink(path) != 0 && err == NULL)
6462 err = got_error_from_errno2("unlink", path);
6463 free(path);
6464 return err;
6467 static const struct got_error *
6468 histedit_save_list(struct got_histedit_list *histedit_cmds,
6469 struct got_worktree *worktree, struct got_repository *repo)
6471 const struct got_error *err = NULL;
6472 char *path = NULL;
6473 FILE *f = NULL;
6474 struct got_histedit_list_entry *hle;
6475 struct got_commit_object *commit = NULL;
6477 err = got_worktree_get_histedit_script_path(&path, worktree);
6478 if (err)
6479 return err;
6481 f = fopen(path, "w");
6482 if (f == NULL) {
6483 err = got_error_from_errno2("fopen", path);
6484 goto done;
6486 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6487 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6488 repo);
6489 if (err)
6490 break;
6492 if (hle->logmsg) {
6493 int n = fprintf(f, "%c %s\n",
6494 GOT_HISTEDIT_MESG, hle->logmsg);
6495 if (n < 0) {
6496 err = got_ferror(f, GOT_ERR_IO);
6497 break;
6501 done:
6502 if (f && fclose(f) != 0 && err == NULL)
6503 err = got_error_from_errno("fclose");
6504 free(path);
6505 if (commit)
6506 got_object_commit_close(commit);
6507 return err;
6510 void
6511 histedit_free_list(struct got_histedit_list *histedit_cmds)
6513 struct got_histedit_list_entry *hle;
6515 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6516 TAILQ_REMOVE(histedit_cmds, hle, entry);
6517 free(hle);
6521 static const struct got_error *
6522 histedit_load_list(struct got_histedit_list *histedit_cmds,
6523 const char *path, struct got_repository *repo)
6525 const struct got_error *err = NULL;
6526 FILE *f = NULL;
6528 f = fopen(path, "r");
6529 if (f == NULL) {
6530 err = got_error_from_errno2("fopen", path);
6531 goto done;
6534 err = histedit_parse_list(histedit_cmds, f, repo);
6535 done:
6536 if (f && fclose(f) != 0 && err == NULL)
6537 err = got_error_from_errno("fclose");
6538 return err;
6541 static const struct got_error *
6542 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6543 const struct got_error *edit_err, struct got_object_id_queue *commits,
6544 const char *path, const char *branch_name, struct got_repository *repo)
6546 const struct got_error *err = NULL, *prev_err = edit_err;
6547 int resp = ' ';
6549 while (resp != 'c' && resp != 'r' && resp != 'a') {
6550 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6551 "or (a)bort: ", getprogname(), prev_err->msg);
6552 resp = getchar();
6553 if (resp == '\n')
6554 resp = getchar();
6555 if (resp == 'c') {
6556 histedit_free_list(histedit_cmds);
6557 err = histedit_run_editor(histedit_cmds, path, commits,
6558 repo);
6559 if (err) {
6560 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6561 err->code != GOT_ERR_HISTEDIT_CMD)
6562 break;
6563 prev_err = err;
6564 resp = ' ';
6565 continue;
6567 break;
6568 } else if (resp == 'r') {
6569 histedit_free_list(histedit_cmds);
6570 err = histedit_edit_script(histedit_cmds,
6571 commits, branch_name, 0, repo);
6572 if (err) {
6573 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6574 err->code != GOT_ERR_HISTEDIT_CMD)
6575 break;
6576 prev_err = err;
6577 resp = ' ';
6578 continue;
6580 break;
6581 } else if (resp == 'a') {
6582 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6583 break;
6584 } else
6585 printf("invalid response '%c'\n", resp);
6588 return err;
6591 static const struct got_error *
6592 histedit_complete(struct got_worktree *worktree,
6593 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6594 struct got_reference *branch, struct got_repository *repo)
6596 printf("Switching work tree to %s\n",
6597 got_ref_get_symref_target(branch));
6598 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6599 branch, repo);
6602 static const struct got_error *
6603 show_histedit_progress(struct got_commit_object *commit,
6604 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6606 const struct got_error *err;
6607 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6609 err = got_object_id_str(&old_id_str, hle->commit_id);
6610 if (err)
6611 goto done;
6613 if (new_id) {
6614 err = got_object_id_str(&new_id_str, new_id);
6615 if (err)
6616 goto done;
6619 old_id_str[12] = '\0';
6620 if (new_id_str)
6621 new_id_str[12] = '\0';
6623 if (hle->logmsg) {
6624 logmsg = strdup(hle->logmsg);
6625 if (logmsg == NULL) {
6626 err = got_error_from_errno("strdup");
6627 goto done;
6629 trim_logmsg(logmsg, 42);
6630 } else {
6631 err = get_short_logmsg(&logmsg, 42, commit);
6632 if (err)
6633 goto done;
6636 switch (hle->cmd->code) {
6637 case GOT_HISTEDIT_PICK:
6638 case GOT_HISTEDIT_EDIT:
6639 printf("%s -> %s: %s\n", old_id_str,
6640 new_id_str ? new_id_str : "no-op change", logmsg);
6641 break;
6642 case GOT_HISTEDIT_DROP:
6643 case GOT_HISTEDIT_FOLD:
6644 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6645 logmsg);
6646 break;
6647 default:
6648 break;
6650 done:
6651 free(old_id_str);
6652 free(new_id_str);
6653 return err;
6656 static const struct got_error *
6657 histedit_commit(struct got_pathlist_head *merged_paths,
6658 struct got_worktree *worktree, struct got_fileindex *fileindex,
6659 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6660 struct got_repository *repo)
6662 const struct got_error *err;
6663 struct got_commit_object *commit;
6664 struct got_object_id *new_commit_id;
6666 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6667 && hle->logmsg == NULL) {
6668 err = histedit_edit_logmsg(hle, repo);
6669 if (err)
6670 return err;
6673 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6674 if (err)
6675 return err;
6677 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6678 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6679 hle->logmsg, repo);
6680 if (err) {
6681 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6682 goto done;
6683 err = show_histedit_progress(commit, hle, NULL);
6684 } else {
6685 err = show_histedit_progress(commit, hle, new_commit_id);
6686 free(new_commit_id);
6688 done:
6689 got_object_commit_close(commit);
6690 return err;
6693 static const struct got_error *
6694 histedit_skip_commit(struct got_histedit_list_entry *hle,
6695 struct got_worktree *worktree, struct got_repository *repo)
6697 const struct got_error *error;
6698 struct got_commit_object *commit;
6700 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6701 repo);
6702 if (error)
6703 return error;
6705 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6706 if (error)
6707 return error;
6709 error = show_histedit_progress(commit, hle, NULL);
6710 got_object_commit_close(commit);
6711 return error;
6714 static const struct got_error *
6715 check_local_changes(void *arg, unsigned char status,
6716 unsigned char staged_status, const char *path,
6717 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6718 struct got_object_id *commit_id, int dirfd, const char *de_name)
6720 int *have_local_changes = arg;
6722 switch (status) {
6723 case GOT_STATUS_ADD:
6724 case GOT_STATUS_DELETE:
6725 case GOT_STATUS_MODIFY:
6726 case GOT_STATUS_CONFLICT:
6727 *have_local_changes = 1;
6728 return got_error(GOT_ERR_CANCELLED);
6729 default:
6730 break;
6733 switch (staged_status) {
6734 case GOT_STATUS_ADD:
6735 case GOT_STATUS_DELETE:
6736 case GOT_STATUS_MODIFY:
6737 *have_local_changes = 1;
6738 return got_error(GOT_ERR_CANCELLED);
6739 default:
6740 break;
6743 return NULL;
6746 static const struct got_error *
6747 cmd_histedit(int argc, char *argv[])
6749 const struct got_error *error = NULL;
6750 struct got_worktree *worktree = NULL;
6751 struct got_fileindex *fileindex = NULL;
6752 struct got_repository *repo = NULL;
6753 char *cwd = NULL;
6754 struct got_reference *branch = NULL;
6755 struct got_reference *tmp_branch = NULL;
6756 struct got_object_id *resume_commit_id = NULL;
6757 struct got_object_id *base_commit_id = NULL;
6758 struct got_object_id *head_commit_id = NULL;
6759 struct got_commit_object *commit = NULL;
6760 int ch, rebase_in_progress = 0, did_something;
6761 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6762 int edit_logmsg_only = 0;
6763 const char *edit_script_path = NULL;
6764 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6765 struct got_object_id_queue commits;
6766 struct got_pathlist_head merged_paths;
6767 const struct got_object_id_queue *parent_ids;
6768 struct got_object_qid *pid;
6769 struct got_histedit_list histedit_cmds;
6770 struct got_histedit_list_entry *hle;
6772 SIMPLEQ_INIT(&commits);
6773 TAILQ_INIT(&histedit_cmds);
6774 TAILQ_INIT(&merged_paths);
6776 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6777 switch (ch) {
6778 case 'a':
6779 abort_edit = 1;
6780 break;
6781 case 'c':
6782 continue_edit = 1;
6783 break;
6784 case 'F':
6785 edit_script_path = optarg;
6786 break;
6787 case 'm':
6788 edit_logmsg_only = 1;
6789 break;
6790 default:
6791 usage_histedit();
6792 /* NOTREACHED */
6796 argc -= optind;
6797 argv += optind;
6799 #ifndef PROFILE
6800 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6801 "unveil", NULL) == -1)
6802 err(1, "pledge");
6803 #endif
6804 if (abort_edit && continue_edit)
6805 errx(1, "histedit's -a and -c options are mutually exclusive");
6806 if (edit_script_path && edit_logmsg_only)
6807 errx(1, "histedit's -F and -m options are mutually exclusive");
6808 if (abort_edit && edit_logmsg_only)
6809 errx(1, "histedit's -a and -m options are mutually exclusive");
6810 if (continue_edit && edit_logmsg_only)
6811 errx(1, "histedit's -c and -m options are mutually exclusive");
6812 if (argc != 0)
6813 usage_histedit();
6816 * This command cannot apply unveil(2) in all cases because the
6817 * user may choose to run an editor to edit the histedit script
6818 * and to edit individual commit log messages.
6819 * unveil(2) traverses exec(2); if an editor is used we have to
6820 * apply unveil after edit script and log messages have been written.
6821 * XXX TODO: Make use of unveil(2) where possible.
6824 cwd = getcwd(NULL, 0);
6825 if (cwd == NULL) {
6826 error = got_error_from_errno("getcwd");
6827 goto done;
6829 error = got_worktree_open(&worktree, cwd);
6830 if (error)
6831 goto done;
6833 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6834 NULL);
6835 if (error != NULL)
6836 goto done;
6838 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6839 if (error)
6840 goto done;
6841 if (rebase_in_progress) {
6842 error = got_error(GOT_ERR_REBASING);
6843 goto done;
6846 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6847 if (error)
6848 goto done;
6850 if (edit_in_progress && edit_logmsg_only) {
6851 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6852 "histedit operation is in progress in this "
6853 "work tree and must be continued or aborted "
6854 "before the -m option can be used");
6855 goto done;
6858 if (edit_in_progress && abort_edit) {
6859 error = got_worktree_histedit_continue(&resume_commit_id,
6860 &tmp_branch, &branch, &base_commit_id, &fileindex,
6861 worktree, repo);
6862 if (error)
6863 goto done;
6864 printf("Switching work tree to %s\n",
6865 got_ref_get_symref_target(branch));
6866 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6867 branch, base_commit_id, update_progress, &did_something);
6868 if (error)
6869 goto done;
6870 printf("Histedit of %s aborted\n",
6871 got_ref_get_symref_target(branch));
6872 goto done; /* nothing else to do */
6873 } else if (abort_edit) {
6874 error = got_error(GOT_ERR_NOT_HISTEDIT);
6875 goto done;
6878 if (continue_edit) {
6879 char *path;
6881 if (!edit_in_progress) {
6882 error = got_error(GOT_ERR_NOT_HISTEDIT);
6883 goto done;
6886 error = got_worktree_get_histedit_script_path(&path, worktree);
6887 if (error)
6888 goto done;
6890 error = histedit_load_list(&histedit_cmds, path, repo);
6891 free(path);
6892 if (error)
6893 goto done;
6895 error = got_worktree_histedit_continue(&resume_commit_id,
6896 &tmp_branch, &branch, &base_commit_id, &fileindex,
6897 worktree, repo);
6898 if (error)
6899 goto done;
6901 error = got_ref_resolve(&head_commit_id, repo, branch);
6902 if (error)
6903 goto done;
6905 error = got_object_open_as_commit(&commit, repo,
6906 head_commit_id);
6907 if (error)
6908 goto done;
6909 parent_ids = got_object_commit_get_parent_ids(commit);
6910 pid = SIMPLEQ_FIRST(parent_ids);
6911 if (pid == NULL) {
6912 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6913 goto done;
6915 error = collect_commits(&commits, head_commit_id, pid->id,
6916 base_commit_id, got_worktree_get_path_prefix(worktree),
6917 GOT_ERR_HISTEDIT_PATH, repo);
6918 got_object_commit_close(commit);
6919 commit = NULL;
6920 if (error)
6921 goto done;
6922 } else {
6923 if (edit_in_progress) {
6924 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6925 goto done;
6928 error = got_ref_open(&branch, repo,
6929 got_worktree_get_head_ref_name(worktree), 0);
6930 if (error != NULL)
6931 goto done;
6933 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6934 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6935 "will not edit commit history of a branch outside "
6936 "the \"refs/heads/\" reference namespace");
6937 goto done;
6940 error = got_ref_resolve(&head_commit_id, repo, branch);
6941 got_ref_close(branch);
6942 branch = NULL;
6943 if (error)
6944 goto done;
6946 error = got_object_open_as_commit(&commit, repo,
6947 head_commit_id);
6948 if (error)
6949 goto done;
6950 parent_ids = got_object_commit_get_parent_ids(commit);
6951 pid = SIMPLEQ_FIRST(parent_ids);
6952 if (pid == NULL) {
6953 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6954 goto done;
6956 error = collect_commits(&commits, head_commit_id, pid->id,
6957 got_worktree_get_base_commit_id(worktree),
6958 got_worktree_get_path_prefix(worktree),
6959 GOT_ERR_HISTEDIT_PATH, repo);
6960 got_object_commit_close(commit);
6961 commit = NULL;
6962 if (error)
6963 goto done;
6965 if (SIMPLEQ_EMPTY(&commits)) {
6966 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6967 goto done;
6970 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6971 &base_commit_id, &fileindex, worktree, repo);
6972 if (error)
6973 goto done;
6975 if (edit_script_path) {
6976 error = histedit_load_list(&histedit_cmds,
6977 edit_script_path, repo);
6978 if (error) {
6979 got_worktree_histedit_abort(worktree, fileindex,
6980 repo, branch, base_commit_id,
6981 update_progress, &did_something);
6982 goto done;
6984 } else {
6985 const char *branch_name;
6986 branch_name = got_ref_get_symref_target(branch);
6987 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6988 branch_name += 11;
6989 error = histedit_edit_script(&histedit_cmds, &commits,
6990 branch_name, edit_logmsg_only, repo);
6991 if (error) {
6992 got_worktree_histedit_abort(worktree, fileindex,
6993 repo, branch, base_commit_id,
6994 update_progress, &did_something);
6995 goto done;
7000 error = histedit_save_list(&histedit_cmds, worktree,
7001 repo);
7002 if (error) {
7003 got_worktree_histedit_abort(worktree, fileindex,
7004 repo, branch, base_commit_id,
7005 update_progress, &did_something);
7006 goto done;
7011 error = histedit_check_script(&histedit_cmds, &commits, repo);
7012 if (error)
7013 goto done;
7015 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7016 if (resume_commit_id) {
7017 if (got_object_id_cmp(hle->commit_id,
7018 resume_commit_id) != 0)
7019 continue;
7021 resume_commit_id = NULL;
7022 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7023 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7024 error = histedit_skip_commit(hle, worktree,
7025 repo);
7026 if (error)
7027 goto done;
7028 } else {
7029 struct got_pathlist_head paths;
7030 int have_changes = 0;
7032 TAILQ_INIT(&paths);
7033 error = got_pathlist_append(&paths, "", NULL);
7034 if (error)
7035 goto done;
7036 error = got_worktree_status(worktree, &paths,
7037 repo, check_local_changes, &have_changes,
7038 check_cancelled, NULL);
7039 got_pathlist_free(&paths);
7040 if (error) {
7041 if (error->code != GOT_ERR_CANCELLED)
7042 goto done;
7043 if (sigint_received || sigpipe_received)
7044 goto done;
7046 if (have_changes) {
7047 error = histedit_commit(NULL, worktree,
7048 fileindex, tmp_branch, hle, repo);
7049 if (error)
7050 goto done;
7051 } else {
7052 error = got_object_open_as_commit(
7053 &commit, repo, hle->commit_id);
7054 if (error)
7055 goto done;
7056 error = show_histedit_progress(commit,
7057 hle, NULL);
7058 got_object_commit_close(commit);
7059 commit = NULL;
7060 if (error)
7061 goto done;
7064 continue;
7067 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7068 error = histedit_skip_commit(hle, worktree, repo);
7069 if (error)
7070 goto done;
7071 continue;
7074 error = got_object_open_as_commit(&commit, repo,
7075 hle->commit_id);
7076 if (error)
7077 goto done;
7078 parent_ids = got_object_commit_get_parent_ids(commit);
7079 pid = SIMPLEQ_FIRST(parent_ids);
7081 error = got_worktree_histedit_merge_files(&merged_paths,
7082 worktree, fileindex, pid->id, hle->commit_id, repo,
7083 rebase_progress, &rebase_status, check_cancelled, NULL);
7084 if (error)
7085 goto done;
7086 got_object_commit_close(commit);
7087 commit = NULL;
7089 if (rebase_status == GOT_STATUS_CONFLICT) {
7090 error = show_rebase_merge_conflict(hle->commit_id,
7091 repo);
7092 if (error)
7093 goto done;
7094 got_worktree_rebase_pathlist_free(&merged_paths);
7095 break;
7098 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7099 char *id_str;
7100 error = got_object_id_str(&id_str, hle->commit_id);
7101 if (error)
7102 goto done;
7103 printf("Stopping histedit for amending commit %s\n",
7104 id_str);
7105 free(id_str);
7106 got_worktree_rebase_pathlist_free(&merged_paths);
7107 error = got_worktree_histedit_postpone(worktree,
7108 fileindex);
7109 goto done;
7112 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7113 error = histedit_skip_commit(hle, worktree, repo);
7114 if (error)
7115 goto done;
7116 continue;
7119 error = histedit_commit(&merged_paths, worktree, fileindex,
7120 tmp_branch, hle, repo);
7121 got_worktree_rebase_pathlist_free(&merged_paths);
7122 if (error)
7123 goto done;
7126 if (rebase_status == GOT_STATUS_CONFLICT) {
7127 error = got_worktree_histedit_postpone(worktree, fileindex);
7128 if (error)
7129 goto done;
7130 error = got_error_msg(GOT_ERR_CONFLICTS,
7131 "conflicts must be resolved before histedit can continue");
7132 } else
7133 error = histedit_complete(worktree, fileindex, tmp_branch,
7134 branch, repo);
7135 done:
7136 got_object_id_queue_free(&commits);
7137 histedit_free_list(&histedit_cmds);
7138 free(head_commit_id);
7139 free(base_commit_id);
7140 free(resume_commit_id);
7141 if (commit)
7142 got_object_commit_close(commit);
7143 if (branch)
7144 got_ref_close(branch);
7145 if (tmp_branch)
7146 got_ref_close(tmp_branch);
7147 if (worktree)
7148 got_worktree_close(worktree);
7149 if (repo)
7150 got_repo_close(repo);
7151 return error;
7154 __dead static void
7155 usage_integrate(void)
7157 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7158 exit(1);
7161 static const struct got_error *
7162 cmd_integrate(int argc, char *argv[])
7164 const struct got_error *error = NULL;
7165 struct got_repository *repo = NULL;
7166 struct got_worktree *worktree = NULL;
7167 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7168 const char *branch_arg = NULL;
7169 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7170 struct got_fileindex *fileindex = NULL;
7171 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7172 int ch, did_something = 0;
7174 while ((ch = getopt(argc, argv, "")) != -1) {
7175 switch (ch) {
7176 default:
7177 usage_integrate();
7178 /* NOTREACHED */
7182 argc -= optind;
7183 argv += optind;
7185 if (argc != 1)
7186 usage_integrate();
7187 branch_arg = argv[0];
7189 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7190 "unveil", NULL) == -1)
7191 err(1, "pledge");
7193 cwd = getcwd(NULL, 0);
7194 if (cwd == NULL) {
7195 error = got_error_from_errno("getcwd");
7196 goto done;
7199 error = got_worktree_open(&worktree, cwd);
7200 if (error)
7201 goto done;
7203 error = check_rebase_or_histedit_in_progress(worktree);
7204 if (error)
7205 goto done;
7207 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7208 NULL);
7209 if (error != NULL)
7210 goto done;
7212 error = apply_unveil(got_repo_get_path(repo), 0,
7213 got_worktree_get_root_path(worktree));
7214 if (error)
7215 goto done;
7217 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7218 error = got_error_from_errno("asprintf");
7219 goto done;
7222 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7223 &base_branch_ref, worktree, refname, repo);
7224 if (error)
7225 goto done;
7227 refname = strdup(got_ref_get_name(branch_ref));
7228 if (refname == NULL) {
7229 error = got_error_from_errno("strdup");
7230 got_worktree_integrate_abort(worktree, fileindex, repo,
7231 branch_ref, base_branch_ref);
7232 goto done;
7234 base_refname = strdup(got_ref_get_name(base_branch_ref));
7235 if (base_refname == NULL) {
7236 error = got_error_from_errno("strdup");
7237 got_worktree_integrate_abort(worktree, fileindex, repo,
7238 branch_ref, base_branch_ref);
7239 goto done;
7242 error = got_ref_resolve(&commit_id, repo, branch_ref);
7243 if (error)
7244 goto done;
7246 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7247 if (error)
7248 goto done;
7250 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7251 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7252 "specified branch has already been integrated");
7253 got_worktree_integrate_abort(worktree, fileindex, repo,
7254 branch_ref, base_branch_ref);
7255 goto done;
7258 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7259 if (error) {
7260 if (error->code == GOT_ERR_ANCESTRY)
7261 error = got_error(GOT_ERR_REBASE_REQUIRED);
7262 got_worktree_integrate_abort(worktree, fileindex, repo,
7263 branch_ref, base_branch_ref);
7264 goto done;
7267 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7268 branch_ref, base_branch_ref, update_progress, &did_something,
7269 check_cancelled, NULL);
7270 if (error)
7271 goto done;
7273 printf("Integrated %s into %s\n", refname, base_refname);
7274 done:
7275 if (repo)
7276 got_repo_close(repo);
7277 if (worktree)
7278 got_worktree_close(worktree);
7279 free(cwd);
7280 free(base_commit_id);
7281 free(commit_id);
7282 free(refname);
7283 free(base_refname);
7284 return error;
7287 __dead static void
7288 usage_stage(void)
7290 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7291 "[file-path ...]\n",
7292 getprogname());
7293 exit(1);
7296 static const struct got_error *
7297 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7298 const char *path, struct got_object_id *blob_id,
7299 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7300 int dirfd, const char *de_name)
7302 const struct got_error *err = NULL;
7303 char *id_str = NULL;
7305 if (staged_status != GOT_STATUS_ADD &&
7306 staged_status != GOT_STATUS_MODIFY &&
7307 staged_status != GOT_STATUS_DELETE)
7308 return NULL;
7310 if (staged_status == GOT_STATUS_ADD ||
7311 staged_status == GOT_STATUS_MODIFY)
7312 err = got_object_id_str(&id_str, staged_blob_id);
7313 else
7314 err = got_object_id_str(&id_str, blob_id);
7315 if (err)
7316 return err;
7318 printf("%s %c %s\n", id_str, staged_status, path);
7319 free(id_str);
7320 return NULL;
7323 static const struct got_error *
7324 cmd_stage(int argc, char *argv[])
7326 const struct got_error *error = NULL;
7327 struct got_repository *repo = NULL;
7328 struct got_worktree *worktree = NULL;
7329 char *cwd = NULL;
7330 struct got_pathlist_head paths;
7331 struct got_pathlist_entry *pe;
7332 int ch, list_stage = 0, pflag = 0;
7333 FILE *patch_script_file = NULL;
7334 const char *patch_script_path = NULL;
7335 struct choose_patch_arg cpa;
7337 TAILQ_INIT(&paths);
7339 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7340 switch (ch) {
7341 case 'l':
7342 list_stage = 1;
7343 break;
7344 case 'p':
7345 pflag = 1;
7346 break;
7347 case 'F':
7348 patch_script_path = optarg;
7349 break;
7350 default:
7351 usage_stage();
7352 /* NOTREACHED */
7356 argc -= optind;
7357 argv += optind;
7359 #ifndef PROFILE
7360 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7361 "unveil", NULL) == -1)
7362 err(1, "pledge");
7363 #endif
7364 if (list_stage && (pflag || patch_script_path))
7365 errx(1, "-l option cannot be used with other options");
7366 if (patch_script_path && !pflag)
7367 errx(1, "-F option can only be used together with -p option");
7369 cwd = getcwd(NULL, 0);
7370 if (cwd == NULL) {
7371 error = got_error_from_errno("getcwd");
7372 goto done;
7375 error = got_worktree_open(&worktree, cwd);
7376 if (error)
7377 goto done;
7379 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7380 NULL);
7381 if (error != NULL)
7382 goto done;
7384 if (patch_script_path) {
7385 patch_script_file = fopen(patch_script_path, "r");
7386 if (patch_script_file == NULL) {
7387 error = got_error_from_errno2("fopen",
7388 patch_script_path);
7389 goto done;
7392 error = apply_unveil(got_repo_get_path(repo), 0,
7393 got_worktree_get_root_path(worktree));
7394 if (error)
7395 goto done;
7397 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7398 if (error)
7399 goto done;
7401 if (list_stage)
7402 error = got_worktree_status(worktree, &paths, repo,
7403 print_stage, NULL, check_cancelled, NULL);
7404 else {
7405 cpa.patch_script_file = patch_script_file;
7406 cpa.action = "stage";
7407 error = got_worktree_stage(worktree, &paths,
7408 pflag ? NULL : print_status, NULL,
7409 pflag ? choose_patch : NULL, &cpa, repo);
7411 done:
7412 if (patch_script_file && fclose(patch_script_file) == EOF &&
7413 error == NULL)
7414 error = got_error_from_errno2("fclose", patch_script_path);
7415 if (repo)
7416 got_repo_close(repo);
7417 if (worktree)
7418 got_worktree_close(worktree);
7419 TAILQ_FOREACH(pe, &paths, entry)
7420 free((char *)pe->path);
7421 got_pathlist_free(&paths);
7422 free(cwd);
7423 return error;
7426 __dead static void
7427 usage_unstage(void)
7429 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7430 "[file-path ...]\n",
7431 getprogname());
7432 exit(1);
7436 static const struct got_error *
7437 cmd_unstage(int argc, char *argv[])
7439 const struct got_error *error = NULL;
7440 struct got_repository *repo = NULL;
7441 struct got_worktree *worktree = NULL;
7442 char *cwd = NULL;
7443 struct got_pathlist_head paths;
7444 struct got_pathlist_entry *pe;
7445 int ch, did_something = 0, pflag = 0;
7446 FILE *patch_script_file = NULL;
7447 const char *patch_script_path = NULL;
7448 struct choose_patch_arg cpa;
7450 TAILQ_INIT(&paths);
7452 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7453 switch (ch) {
7454 case 'p':
7455 pflag = 1;
7456 break;
7457 case 'F':
7458 patch_script_path = optarg;
7459 break;
7460 default:
7461 usage_unstage();
7462 /* NOTREACHED */
7466 argc -= optind;
7467 argv += optind;
7469 #ifndef PROFILE
7470 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7471 "unveil", NULL) == -1)
7472 err(1, "pledge");
7473 #endif
7474 if (patch_script_path && !pflag)
7475 errx(1, "-F option can only be used together with -p option");
7477 cwd = getcwd(NULL, 0);
7478 if (cwd == NULL) {
7479 error = got_error_from_errno("getcwd");
7480 goto done;
7483 error = got_worktree_open(&worktree, cwd);
7484 if (error)
7485 goto done;
7487 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7488 NULL);
7489 if (error != NULL)
7490 goto done;
7492 if (patch_script_path) {
7493 patch_script_file = fopen(patch_script_path, "r");
7494 if (patch_script_file == NULL) {
7495 error = got_error_from_errno2("fopen",
7496 patch_script_path);
7497 goto done;
7501 error = apply_unveil(got_repo_get_path(repo), 0,
7502 got_worktree_get_root_path(worktree));
7503 if (error)
7504 goto done;
7506 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7507 if (error)
7508 goto done;
7510 cpa.patch_script_file = patch_script_file;
7511 cpa.action = "unstage";
7512 error = got_worktree_unstage(worktree, &paths, update_progress,
7513 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7514 done:
7515 if (patch_script_file && fclose(patch_script_file) == EOF &&
7516 error == NULL)
7517 error = got_error_from_errno2("fclose", patch_script_path);
7518 if (repo)
7519 got_repo_close(repo);
7520 if (worktree)
7521 got_worktree_close(worktree);
7522 TAILQ_FOREACH(pe, &paths, entry)
7523 free((char *)pe->path);
7524 got_pathlist_free(&paths);
7525 free(cwd);
7526 return error;
7529 __dead static void
7530 usage_cat(void)
7532 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7533 "arg1 [arg2 ...]\n", getprogname());
7534 exit(1);
7537 static const struct got_error *
7538 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7540 const struct got_error *err;
7541 struct got_blob_object *blob;
7543 err = got_object_open_as_blob(&blob, repo, id, 8192);
7544 if (err)
7545 return err;
7547 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7548 got_object_blob_close(blob);
7549 return err;
7552 static const struct got_error *
7553 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7555 const struct got_error *err;
7556 struct got_tree_object *tree;
7557 int nentries, i;
7559 err = got_object_open_as_tree(&tree, repo, id);
7560 if (err)
7561 return err;
7563 nentries = got_object_tree_get_nentries(tree);
7564 for (i = 0; i < nentries; i++) {
7565 struct got_tree_entry *te;
7566 char *id_str;
7567 if (sigint_received || sigpipe_received)
7568 break;
7569 te = got_object_tree_get_entry(tree, i);
7570 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7571 if (err)
7572 break;
7573 fprintf(outfile, "%s %.7o %s\n", id_str,
7574 got_tree_entry_get_mode(te),
7575 got_tree_entry_get_name(te));
7576 free(id_str);
7579 got_object_tree_close(tree);
7580 return err;
7583 static const struct got_error *
7584 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7586 const struct got_error *err;
7587 struct got_commit_object *commit;
7588 const struct got_object_id_queue *parent_ids;
7589 struct got_object_qid *pid;
7590 char *id_str = NULL;
7591 const char *logmsg = NULL;
7593 err = got_object_open_as_commit(&commit, repo, id);
7594 if (err)
7595 return err;
7597 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7598 if (err)
7599 goto done;
7601 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7602 parent_ids = got_object_commit_get_parent_ids(commit);
7603 fprintf(outfile, "numparents %d\n",
7604 got_object_commit_get_nparents(commit));
7605 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7606 char *pid_str;
7607 err = got_object_id_str(&pid_str, pid->id);
7608 if (err)
7609 goto done;
7610 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7611 free(pid_str);
7613 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7614 got_object_commit_get_author(commit),
7615 got_object_commit_get_author_time(commit));
7617 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7618 got_object_commit_get_author(commit),
7619 got_object_commit_get_committer_time(commit));
7621 logmsg = got_object_commit_get_logmsg_raw(commit);
7622 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7623 fprintf(outfile, "%s", logmsg);
7624 done:
7625 free(id_str);
7626 got_object_commit_close(commit);
7627 return err;
7630 static const struct got_error *
7631 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7633 const struct got_error *err;
7634 struct got_tag_object *tag;
7635 char *id_str = NULL;
7636 const char *tagmsg = NULL;
7638 err = got_object_open_as_tag(&tag, repo, id);
7639 if (err)
7640 return err;
7642 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7643 if (err)
7644 goto done;
7646 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7648 switch (got_object_tag_get_object_type(tag)) {
7649 case GOT_OBJ_TYPE_BLOB:
7650 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7651 GOT_OBJ_LABEL_BLOB);
7652 break;
7653 case GOT_OBJ_TYPE_TREE:
7654 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7655 GOT_OBJ_LABEL_TREE);
7656 break;
7657 case GOT_OBJ_TYPE_COMMIT:
7658 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7659 GOT_OBJ_LABEL_COMMIT);
7660 break;
7661 case GOT_OBJ_TYPE_TAG:
7662 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7663 GOT_OBJ_LABEL_TAG);
7664 break;
7665 default:
7666 break;
7669 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7670 got_object_tag_get_name(tag));
7672 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7673 got_object_tag_get_tagger(tag),
7674 got_object_tag_get_tagger_time(tag));
7676 tagmsg = got_object_tag_get_message(tag);
7677 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7678 fprintf(outfile, "%s", tagmsg);
7679 done:
7680 free(id_str);
7681 got_object_tag_close(tag);
7682 return err;
7685 static const struct got_error *
7686 cmd_cat(int argc, char *argv[])
7688 const struct got_error *error;
7689 struct got_repository *repo = NULL;
7690 struct got_worktree *worktree = NULL;
7691 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7692 const char *commit_id_str = NULL;
7693 struct got_object_id *id = NULL, *commit_id = NULL;
7694 int ch, obj_type, i, force_path = 0;
7696 #ifndef PROFILE
7697 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7698 NULL) == -1)
7699 err(1, "pledge");
7700 #endif
7702 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7703 switch (ch) {
7704 case 'c':
7705 commit_id_str = optarg;
7706 break;
7707 case 'r':
7708 repo_path = realpath(optarg, NULL);
7709 if (repo_path == NULL)
7710 return got_error_from_errno2("realpath",
7711 optarg);
7712 got_path_strip_trailing_slashes(repo_path);
7713 break;
7714 case 'P':
7715 force_path = 1;
7716 break;
7717 default:
7718 usage_cat();
7719 /* NOTREACHED */
7723 argc -= optind;
7724 argv += optind;
7726 cwd = getcwd(NULL, 0);
7727 if (cwd == NULL) {
7728 error = got_error_from_errno("getcwd");
7729 goto done;
7731 error = got_worktree_open(&worktree, cwd);
7732 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7733 goto done;
7734 if (worktree) {
7735 if (repo_path == NULL) {
7736 repo_path = strdup(
7737 got_worktree_get_repo_path(worktree));
7738 if (repo_path == NULL) {
7739 error = got_error_from_errno("strdup");
7740 goto done;
7745 if (repo_path == NULL) {
7746 repo_path = getcwd(NULL, 0);
7747 if (repo_path == NULL)
7748 return got_error_from_errno("getcwd");
7751 error = got_repo_open(&repo, repo_path, NULL);
7752 free(repo_path);
7753 if (error != NULL)
7754 goto done;
7756 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7757 if (error)
7758 goto done;
7760 if (commit_id_str == NULL)
7761 commit_id_str = GOT_REF_HEAD;
7762 error = got_repo_match_object_id(&commit_id, NULL,
7763 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7764 if (error)
7765 goto done;
7767 for (i = 0; i < argc; i++) {
7768 if (force_path) {
7769 error = got_object_id_by_path(&id, repo, commit_id,
7770 argv[i]);
7771 if (error)
7772 break;
7773 } else {
7774 error = got_repo_match_object_id(&id, &label, argv[i],
7775 GOT_OBJ_TYPE_ANY, 0, repo);
7776 if (error) {
7777 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7778 error->code != GOT_ERR_NOT_REF)
7779 break;
7780 error = got_object_id_by_path(&id, repo,
7781 commit_id, argv[i]);
7782 if (error)
7783 break;
7787 error = got_object_get_type(&obj_type, repo, id);
7788 if (error)
7789 break;
7791 switch (obj_type) {
7792 case GOT_OBJ_TYPE_BLOB:
7793 error = cat_blob(id, repo, stdout);
7794 break;
7795 case GOT_OBJ_TYPE_TREE:
7796 error = cat_tree(id, repo, stdout);
7797 break;
7798 case GOT_OBJ_TYPE_COMMIT:
7799 error = cat_commit(id, repo, stdout);
7800 break;
7801 case GOT_OBJ_TYPE_TAG:
7802 error = cat_tag(id, repo, stdout);
7803 break;
7804 default:
7805 error = got_error(GOT_ERR_OBJ_TYPE);
7806 break;
7808 if (error)
7809 break;
7810 free(label);
7811 label = NULL;
7812 free(id);
7813 id = NULL;
7815 done:
7816 free(label);
7817 free(id);
7818 free(commit_id);
7819 if (worktree)
7820 got_worktree_close(worktree);
7821 if (repo) {
7822 const struct got_error *repo_error;
7823 repo_error = got_repo_close(repo);
7824 if (error == NULL)
7825 error = repo_error;
7827 return error;