Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
41 #include <util.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
65 static void
66 catch_sigint(int signo)
67 {
68 sigint_received = 1;
69 }
71 static void
72 catch_sigpipe(int signo)
73 {
74 sigpipe_received = 1;
75 }
78 struct got_cmd {
79 const char *cmd_name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
83 };
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_checkout(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_update(void);
91 __dead static void usage_log(void);
92 __dead static void usage_diff(void);
93 __dead static void usage_blame(void);
94 __dead static void usage_tree(void);
95 __dead static void usage_status(void);
96 __dead static void usage_ref(void);
97 __dead static void usage_branch(void);
98 __dead static void usage_tag(void);
99 __dead static void usage_add(void);
100 __dead static void usage_remove(void);
101 __dead static void usage_revert(void);
102 __dead static void usage_commit(void);
103 __dead static void usage_cherrypick(void);
104 __dead static void usage_backout(void);
105 __dead static void usage_rebase(void);
106 __dead static void usage_histedit(void);
107 __dead static void usage_integrate(void);
108 __dead static void usage_stage(void);
109 __dead static void usage_unstage(void);
110 __dead static void usage_cat(void);
112 static const struct got_error* cmd_init(int, char *[]);
113 static const struct got_error* cmd_import(int, char *[]);
114 static const struct got_error* cmd_clone(int, char *[]);
115 static const struct got_error* cmd_checkout(int, char *[]);
116 static const struct got_error* cmd_update(int, char *[]);
117 static const struct got_error* cmd_log(int, char *[]);
118 static const struct got_error* cmd_diff(int, char *[]);
119 static const struct got_error* cmd_blame(int, char *[]);
120 static const struct got_error* cmd_tree(int, char *[]);
121 static const struct got_error* cmd_status(int, char *[]);
122 static const struct got_error* cmd_ref(int, char *[]);
123 static const struct got_error* cmd_branch(int, char *[]);
124 static const struct got_error* cmd_tag(int, char *[]);
125 static const struct got_error* cmd_add(int, char *[]);
126 static const struct got_error* cmd_remove(int, char *[]);
127 static const struct got_error* cmd_revert(int, char *[]);
128 static const struct got_error* cmd_commit(int, char *[]);
129 static const struct got_error* cmd_cherrypick(int, char *[]);
130 static const struct got_error* cmd_backout(int, char *[]);
131 static const struct got_error* cmd_rebase(int, char *[]);
132 static const struct got_error* cmd_histedit(int, char *[]);
133 static const struct got_error* cmd_integrate(int, char *[]);
134 static const struct got_error* cmd_stage(int, char *[]);
135 static const struct got_error* cmd_unstage(int, char *[]);
136 static const struct got_error* cmd_cat(int, char *[]);
138 static struct got_cmd got_commands[] = {
139 { "init", cmd_init, usage_init, "in" },
140 { "import", cmd_import, usage_import, "im" },
141 { "checkout", cmd_checkout, usage_checkout, "co" },
142 { "clone", cmd_clone, usage_clone, "cl" },
143 { "update", cmd_update, usage_update, "up" },
144 { "log", cmd_log, usage_log, "" },
145 { "diff", cmd_diff, usage_diff, "di" },
146 { "blame", cmd_blame, usage_blame, "bl" },
147 { "tree", cmd_tree, usage_tree, "tr" },
148 { "status", cmd_status, usage_status, "st" },
149 { "ref", cmd_ref, usage_ref, "" },
150 { "branch", cmd_branch, usage_branch, "br" },
151 { "tag", cmd_tag, usage_tag, "" },
152 { "add", cmd_add, usage_add, "" },
153 { "remove", cmd_remove, usage_remove, "rm" },
154 { "revert", cmd_revert, usage_revert, "rv" },
155 { "commit", cmd_commit, usage_commit, "ci" },
156 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 { "backout", cmd_backout, usage_backout, "bo" },
158 { "rebase", cmd_rebase, usage_rebase, "rb" },
159 { "histedit", cmd_histedit, usage_histedit, "he" },
160 { "integrate", cmd_integrate, usage_integrate,"ig" },
161 { "stage", cmd_stage, usage_stage, "sg" },
162 { "unstage", cmd_unstage, usage_unstage, "ug" },
163 { "cat", cmd_cat, usage_cat, "" },
164 };
166 static void
167 list_commands(void)
169 int i;
171 fprintf(stderr, "commands:");
172 for (i = 0; i < nitems(got_commands); i++) {
173 struct got_cmd *cmd = &got_commands[i];
174 fprintf(stderr, " %s", cmd->cmd_name);
176 fputc('\n', stderr);
179 int
180 main(int argc, char *argv[])
182 struct got_cmd *cmd;
183 unsigned int i;
184 int ch;
185 int hflag = 0, Vflag = 0;
186 static struct option longopts[] = {
187 { "version", no_argument, NULL, 'V' },
188 { NULL, 0, NULL, 0}
189 };
191 setlocale(LC_CTYPE, "");
193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 switch (ch) {
195 case 'h':
196 hflag = 1;
197 break;
198 case 'V':
199 Vflag = 1;
200 break;
201 default:
202 usage(hflag);
203 /* NOTREACHED */
207 argc -= optind;
208 argv += optind;
209 optind = 0;
211 if (Vflag) {
212 got_version_print_str();
213 return 1;
216 if (argc <= 0)
217 usage(hflag);
219 signal(SIGINT, catch_sigint);
220 signal(SIGPIPE, catch_sigpipe);
222 for (i = 0; i < nitems(got_commands); i++) {
223 const struct got_error *error;
225 cmd = &got_commands[i];
227 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 strcmp(cmd->cmd_alias, argv[0]) != 0)
229 continue;
231 if (hflag)
232 got_commands[i].cmd_usage();
234 error = got_commands[i].cmd_main(argc, argv);
235 if (error && error->code != GOT_ERR_CANCELLED &&
236 error->code != GOT_ERR_PRIVSEP_EXIT &&
237 !(sigpipe_received &&
238 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 !(sigint_received &&
240 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 return 1;
245 return 0;
248 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 list_commands();
250 return 1;
253 __dead static void
254 usage(int hflag)
256 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 getprogname());
258 if (hflag)
259 list_commands();
260 exit(1);
263 static const struct got_error *
264 get_editor(char **abspath)
266 const struct got_error *err = NULL;
267 const char *editor;
269 *abspath = NULL;
271 editor = getenv("VISUAL");
272 if (editor == NULL)
273 editor = getenv("EDITOR");
275 if (editor) {
276 err = got_path_find_prog(abspath, editor);
277 if (err)
278 return err;
281 if (*abspath == NULL) {
282 *abspath = strdup("/bin/ed");
283 if (*abspath == NULL)
284 return got_error_from_errno("strdup");
287 return NULL;
290 static const struct got_error *
291 apply_unveil(const char *repo_path, int repo_read_only,
292 const char *worktree_path)
294 const struct got_error *err;
296 #ifdef PROFILE
297 if (unveil("gmon.out", "rwc") != 0)
298 return got_error_from_errno2("unveil", "gmon.out");
299 #endif
300 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 return got_error_from_errno2("unveil", worktree_path);
306 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 err = got_privsep_unveil_exec_helpers();
310 if (err != NULL)
311 return err;
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
316 return NULL;
319 __dead static void
320 usage_init(void)
322 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
323 exit(1);
326 static const struct got_error *
327 cmd_init(int argc, char *argv[])
329 const struct got_error *error = NULL;
330 char *repo_path = NULL;
331 int ch;
333 while ((ch = getopt(argc, argv, "")) != -1) {
334 switch (ch) {
335 default:
336 usage_init();
337 /* NOTREACHED */
341 argc -= optind;
342 argv += optind;
344 #ifndef PROFILE
345 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 err(1, "pledge");
347 #endif
348 if (argc != 1)
349 usage_init();
351 repo_path = strdup(argv[0]);
352 if (repo_path == NULL)
353 return got_error_from_errno("strdup");
355 got_path_strip_trailing_slashes(repo_path);
357 error = got_path_mkdir(repo_path);
358 if (error &&
359 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
360 goto done;
362 error = apply_unveil(repo_path, 0, NULL);
363 if (error)
364 goto done;
366 error = got_repo_init(repo_path);
367 done:
368 free(repo_path);
369 return error;
372 __dead static void
373 usage_import(void)
375 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 "[-r repository-path] [-I pattern] path\n", getprogname());
377 exit(1);
380 int
381 spawn_editor(const char *editor, const char *file)
383 pid_t pid;
384 sig_t sighup, sigint, sigquit;
385 int st = -1;
387 sighup = signal(SIGHUP, SIG_IGN);
388 sigint = signal(SIGINT, SIG_IGN);
389 sigquit = signal(SIGQUIT, SIG_IGN);
391 switch (pid = fork()) {
392 case -1:
393 goto doneediting;
394 case 0:
395 execl(editor, editor, file, (char *)NULL);
396 _exit(127);
399 while (waitpid(pid, &st, 0) == -1)
400 if (errno != EINTR)
401 break;
403 doneediting:
404 (void)signal(SIGHUP, sighup);
405 (void)signal(SIGINT, sigint);
406 (void)signal(SIGQUIT, sigquit);
408 if (!WIFEXITED(st)) {
409 errno = EINTR;
410 return -1;
413 return WEXITSTATUS(st);
416 static const struct got_error *
417 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 const char *initial_content)
420 const struct got_error *err = NULL;
421 char buf[1024];
422 struct stat st, st2;
423 FILE *fp;
424 int content_changed = 0;
425 size_t len;
427 *logmsg = NULL;
429 if (stat(logmsg_path, &st) == -1)
430 return got_error_from_errno2("stat", logmsg_path);
432 if (spawn_editor(editor, logmsg_path) == -1)
433 return got_error_from_errno("failed spawning editor");
435 if (stat(logmsg_path, &st2) == -1)
436 return got_error_from_errno("stat");
438 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 "no changes made to commit message, aborting");
442 *logmsg = malloc(st2.st_size + 1);
443 if (*logmsg == NULL)
444 return got_error_from_errno("malloc");
445 (*logmsg)[0] = '\0';
446 len = 0;
448 fp = fopen(logmsg_path, "r");
449 if (fp == NULL) {
450 err = got_error_from_errno("fopen");
451 goto done;
453 while (fgets(buf, sizeof(buf), fp) != NULL) {
454 if (!content_changed && strcmp(buf, initial_content) != 0)
455 content_changed = 1;
456 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 continue; /* remove comments and leading empty lines */
458 len = strlcat(*logmsg, buf, st2.st_size);
460 fclose(fp);
462 while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 (*logmsg)[len - 1] = '\0';
464 len--;
467 if (len == 0 || !content_changed)
468 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "commit message cannot be empty, aborting");
470 done:
471 if (err) {
472 free(*logmsg);
473 *logmsg = NULL;
475 return err;
478 static const struct got_error *
479 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 const char *path_dir, const char *branch_name)
482 char *initial_content = NULL;
483 const struct got_error *err = NULL;
484 int fd;
486 if (asprintf(&initial_content,
487 "\n# %s to be imported to branch %s\n", path_dir,
488 branch_name) == -1)
489 return got_error_from_errno("asprintf");
491 err = got_opentemp_named_fd(logmsg_path, &fd,
492 GOT_TMPDIR_STR "/got-importmsg");
493 if (err)
494 goto done;
496 dprintf(fd, initial_content);
497 close(fd);
499 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 done:
501 free(initial_content);
502 return err;
505 static const struct got_error *
506 import_progress(void *arg, const char *path)
508 printf("A %s\n", path);
509 return NULL;
512 static const struct got_error *
513 get_author(char **author, struct got_repository *repo)
515 const struct got_error *err = NULL;
516 const char *got_author, *name, *email;
518 *author = NULL;
520 name = got_repo_get_gitconfig_author_name(repo);
521 email = got_repo_get_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
528 got_author = getenv("GOT_AUTHOR");
529 if (got_author == NULL) {
530 name = got_repo_get_global_gitconfig_author_name(repo);
531 email = got_repo_get_global_gitconfig_author_email(repo);
532 if (name && email) {
533 if (asprintf(author, "%s <%s>", name, email) == -1)
534 return got_error_from_errno("asprintf");
535 return NULL;
537 /* TODO: Look up user in password database? */
538 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
541 *author = strdup(got_author);
542 if (*author == NULL)
543 return got_error_from_errno("strdup");
545 /*
546 * Really dumb email address check; we're only doing this to
547 * avoid git's object parser breaking on commits we create.
548 */
549 while (*got_author && *got_author != '<')
550 got_author++;
551 if (*got_author != '<') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 goto done;
555 while (*got_author && *got_author != '@')
556 got_author++;
557 if (*got_author != '@') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 goto done;
561 while (*got_author && *got_author != '>')
562 got_author++;
563 if (*got_author != '>')
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 done:
566 if (err) {
567 free(*author);
568 *author = NULL;
570 return err;
573 static const struct got_error *
574 get_gitconfig_path(char **gitconfig_path)
576 const char *homedir = getenv("HOME");
578 *gitconfig_path = NULL;
579 if (homedir) {
580 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 return got_error_from_errno("asprintf");
584 return NULL;
587 static const struct got_error *
588 cmd_import(int argc, char *argv[])
590 const struct got_error *error = NULL;
591 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 const char *branch_name = "main";
594 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 struct got_repository *repo = NULL;
596 struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 struct got_object_id *new_commit_id = NULL;
598 int ch;
599 struct got_pathlist_head ignores;
600 struct got_pathlist_entry *pe;
601 int preserve_logmsg = 0;
603 TAILQ_INIT(&ignores);
605 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 switch (ch) {
607 case 'b':
608 branch_name = optarg;
609 break;
610 case 'm':
611 logmsg = strdup(optarg);
612 if (logmsg == NULL) {
613 error = got_error_from_errno("strdup");
614 goto done;
616 break;
617 case 'r':
618 repo_path = realpath(optarg, NULL);
619 if (repo_path == NULL) {
620 error = got_error_from_errno2("realpath",
621 optarg);
622 goto done;
624 break;
625 case 'I':
626 if (optarg[0] == '\0')
627 break;
628 error = got_pathlist_insert(&pe, &ignores, optarg,
629 NULL);
630 if (error)
631 goto done;
632 break;
633 default:
634 usage_import();
635 /* NOTREACHED */
639 argc -= optind;
640 argv += optind;
642 #ifndef PROFILE
643 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 "unveil",
645 NULL) == -1)
646 err(1, "pledge");
647 #endif
648 if (argc != 1)
649 usage_import();
651 if (repo_path == NULL) {
652 repo_path = getcwd(NULL, 0);
653 if (repo_path == NULL)
654 return got_error_from_errno("getcwd");
656 got_path_strip_trailing_slashes(repo_path);
657 error = get_gitconfig_path(&gitconfig_path);
658 if (error)
659 goto done;
660 error = got_repo_open(&repo, repo_path, gitconfig_path);
661 if (error)
662 goto done;
664 error = get_author(&author, repo);
665 if (error)
666 return error;
668 /*
669 * Don't let the user create a branch name with a leading '-'.
670 * While technically a valid reference name, this case is usually
671 * an unintended typo.
672 */
673 if (branch_name[0] == '-')
674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 error = got_error_from_errno("asprintf");
678 goto done;
681 error = got_ref_open(&branch_ref, repo, refname, 0);
682 if (error) {
683 if (error->code != GOT_ERR_NOT_REF)
684 goto done;
685 } else {
686 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 "import target branch already exists");
688 goto done;
691 path_dir = realpath(argv[0], NULL);
692 if (path_dir == NULL) {
693 error = got_error_from_errno2("realpath", argv[0]);
694 goto done;
696 got_path_strip_trailing_slashes(path_dir);
698 /*
699 * unveil(2) traverses exec(2); if an editor is used we have
700 * to apply unveil after the log message has been written.
701 */
702 if (logmsg == NULL || strlen(logmsg) == 0) {
703 error = get_editor(&editor);
704 if (error)
705 goto done;
706 free(logmsg);
707 error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 path_dir, refname);
709 if (error) {
710 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 logmsg_path != NULL)
712 preserve_logmsg = 1;
713 goto done;
717 if (unveil(path_dir, "r") != 0) {
718 error = got_error_from_errno2("unveil", path_dir);
719 if (logmsg_path)
720 preserve_logmsg = 1;
721 goto done;
724 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 if (error) {
726 if (logmsg_path)
727 preserve_logmsg = 1;
728 goto done;
731 error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 author, &ignores, repo, import_progress, NULL);
733 if (error) {
734 if (logmsg_path)
735 preserve_logmsg = 1;
736 goto done;
739 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 if (error) {
741 if (logmsg_path)
742 preserve_logmsg = 1;
743 goto done;
746 error = got_ref_write(branch_ref, repo);
747 if (error) {
748 if (logmsg_path)
749 preserve_logmsg = 1;
750 goto done;
753 error = got_object_id_str(&id_str, new_commit_id);
754 if (error) {
755 if (logmsg_path)
756 preserve_logmsg = 1;
757 goto done;
760 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 if (error) {
762 if (error->code != GOT_ERR_NOT_REF) {
763 if (logmsg_path)
764 preserve_logmsg = 1;
765 goto done;
768 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 branch_ref);
770 if (error) {
771 if (logmsg_path)
772 preserve_logmsg = 1;
773 goto done;
776 error = got_ref_write(head_ref, repo);
777 if (error) {
778 if (logmsg_path)
779 preserve_logmsg = 1;
780 goto done;
784 printf("Created branch %s with commit %s\n",
785 got_ref_get_name(branch_ref), id_str);
786 done:
787 if (preserve_logmsg) {
788 fprintf(stderr, "%s: log message preserved in %s\n",
789 getprogname(), logmsg_path);
790 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 error = got_error_from_errno2("unlink", logmsg_path);
792 free(logmsg);
793 free(logmsg_path);
794 free(repo_path);
795 free(editor);
796 free(refname);
797 free(new_commit_id);
798 free(id_str);
799 free(author);
800 free(gitconfig_path);
801 if (branch_ref)
802 got_ref_close(branch_ref);
803 if (head_ref)
804 got_ref_close(head_ref);
805 return error;
808 __dead static void
809 usage_clone(void)
811 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
812 exit(1);
815 __dead static void
816 usage_checkout(void)
818 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
819 "[-p prefix] repository-path [worktree-path]\n", getprogname());
820 exit(1);
823 static void
824 show_worktree_base_ref_warning(void)
826 fprintf(stderr, "%s: warning: could not create a reference "
827 "to the work tree's base commit; the commit could be "
828 "garbage-collected by Git; making the repository "
829 "writable and running 'got update' will prevent this\n",
830 getprogname());
833 struct got_checkout_progress_arg {
834 const char *worktree_path;
835 int had_base_commit_ref_error;
836 };
838 static const struct got_error *
839 checkout_progress(void *arg, unsigned char status, const char *path)
841 struct got_checkout_progress_arg *a = arg;
843 /* Base commit bump happens silently. */
844 if (status == GOT_STATUS_BUMP_BASE)
845 return NULL;
847 if (status == GOT_STATUS_BASE_REF_ERR) {
848 a->had_base_commit_ref_error = 1;
849 return NULL;
852 while (path[0] == '/')
853 path++;
855 printf("%c %s/%s\n", status, a->worktree_path, path);
856 return NULL;
859 static const struct got_error *
860 check_cancelled(void *arg)
862 if (sigint_received || sigpipe_received)
863 return got_error(GOT_ERR_CANCELLED);
864 return NULL;
867 static const struct got_error *
868 check_linear_ancestry(struct got_object_id *commit_id,
869 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
870 struct got_repository *repo)
872 const struct got_error *err = NULL;
873 struct got_object_id *yca_id;
875 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
876 commit_id, base_commit_id, repo, check_cancelled, NULL);
877 if (err)
878 return err;
880 if (yca_id == NULL)
881 return got_error(GOT_ERR_ANCESTRY);
883 /*
884 * Require a straight line of history between the target commit
885 * and the work tree's base commit.
887 * Non-linear situations such as this require a rebase:
889 * (commit) D F (base_commit)
890 * \ /
891 * C E
892 * \ /
893 * B (yca)
894 * |
895 * A
897 * 'got update' only handles linear cases:
898 * Update forwards in time: A (base/yca) - B - C - D (commit)
899 * Update backwards in time: D (base) - C - B - A (commit/yca)
900 */
901 if (allow_forwards_in_time_only) {
902 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
903 return got_error(GOT_ERR_ANCESTRY);
904 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
905 got_object_id_cmp(base_commit_id, yca_id) != 0)
906 return got_error(GOT_ERR_ANCESTRY);
908 free(yca_id);
909 return NULL;
912 static const struct got_error *
913 check_same_branch(struct got_object_id *commit_id,
914 struct got_reference *head_ref, struct got_object_id *yca_id,
915 struct got_repository *repo)
917 const struct got_error *err = NULL;
918 struct got_commit_graph *graph = NULL;
919 struct got_object_id *head_commit_id = NULL;
920 int is_same_branch = 0;
922 err = got_ref_resolve(&head_commit_id, repo, head_ref);
923 if (err)
924 goto done;
926 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
927 is_same_branch = 1;
928 goto done;
930 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
931 is_same_branch = 1;
932 goto done;
935 err = got_commit_graph_open(&graph, "/", 1);
936 if (err)
937 goto done;
939 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
940 check_cancelled, NULL);
941 if (err)
942 goto done;
944 for (;;) {
945 struct got_object_id *id;
946 err = got_commit_graph_iter_next(&id, graph, repo,
947 check_cancelled, NULL);
948 if (err) {
949 if (err->code == GOT_ERR_ITER_COMPLETED)
950 err = NULL;
951 break;
954 if (id) {
955 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 break;
957 if (got_object_id_cmp(id, commit_id) == 0) {
958 is_same_branch = 1;
959 break;
963 done:
964 if (graph)
965 got_commit_graph_close(graph);
966 free(head_commit_id);
967 if (!err && !is_same_branch)
968 err = got_error(GOT_ERR_ANCESTRY);
969 return err;
972 static const struct got_error *
973 fetch_progress(void *arg, const char *message, off_t packfile_size,
974 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
976 int *did_something = arg, p;
977 char scaled[FMT_SCALED_STRSIZE];
979 if (message) {
980 printf("\rserver: %s", message);
981 *did_something = 1;
982 } else if (packfile_size > 0 || nobj_indexed > 0) {
983 printf("\r");
984 if (fmt_scaled(packfile_size, scaled) == 0)
985 printf(" %*s fetched", FMT_SCALED_STRSIZE, scaled);
986 if (nobj_indexed > 0) {
987 p = (nobj_indexed * 100) / nobj_total;
988 printf("; indexing %d%%", p);
990 if (nobj_resolved > 0) {
991 p = (nobj_resolved * 100) / (nobj_total - nobj_loose);
992 printf("; resolving deltas %d%%", p);
994 *did_something = 1;
996 fflush(stdout);
997 return NULL;
1000 static const struct got_error *
1001 cmd_clone(int argc, char *argv[])
1003 const struct got_error *err = NULL;
1004 const char *uri, *branch_filter, *dirname;
1005 char *proto, *host, *port, *repo_name, *server_path;
1006 char *default_destdir = NULL, *id_str = NULL;
1007 const char *repo_path;
1008 struct got_repository *repo = NULL;
1009 struct got_pathlist_head refs, symrefs;
1010 struct got_pathlist_entry *pe;
1011 struct got_object_id *pack_hash = NULL;
1012 int ch, fetchfd = -1;
1013 int did_something = 0;
1015 TAILQ_INIT(&refs);
1016 TAILQ_INIT(&symrefs);
1018 while ((ch = getopt(argc, argv, "b:")) != -1) {
1019 switch (ch) {
1020 case 'b':
1021 branch_filter = optarg;
1022 break;
1023 default:
1024 usage_clone();
1025 break;
1028 argc -= optind;
1029 argv += optind;
1030 uri = argv[0];
1031 if(argc == 1)
1032 dirname = NULL;
1033 else if(argc == 2)
1034 dirname = argv[1];
1035 else
1036 usage_clone();
1038 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1039 &repo_name, argv[0]);
1040 if (err)
1041 goto done;
1043 if (dirname == NULL) {
1044 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1045 err = got_error_from_errno("asprintf");
1046 goto done;
1048 repo_path = default_destdir;
1049 } else
1050 repo_path = dirname;
1052 err = got_path_mkdir(repo_path);
1053 if (err)
1054 goto done;
1056 err = got_repo_init(repo_path);
1057 if (err)
1058 goto done;
1060 err = got_repo_open(&repo, repo_path, NULL);
1061 if (err)
1062 goto done;
1064 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1065 if (err)
1066 goto done;
1068 printf("Connected to %s:%s\n", host, port);
1070 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1071 repo, fetch_progress, &did_something);
1072 if (err)
1073 goto done;
1074 if (did_something)
1075 printf("\n");
1077 err = got_object_id_str(&id_str, pack_hash);
1078 if (err)
1079 goto done;
1080 printf("Fetched %s.pack\n", id_str);
1081 free(id_str);
1083 /* Set up references provided with the pack file. */
1084 TAILQ_FOREACH(pe, &refs, entry) {
1085 const char *refname = pe->path;
1086 struct got_object_id *id = pe->data;
1087 struct got_reference *ref;
1090 err = got_ref_alloc(&ref, refname, id);
1091 if (err)
1092 goto done;
1094 #if 0
1095 err = got_object_id_str(&id_str, id);
1096 if (err)
1097 goto done;
1098 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1099 free(id_str);
1100 #endif
1101 err = got_ref_write(ref, repo);
1102 got_ref_close(ref);
1103 if (err)
1104 goto done;
1107 /* Set the HEAD reference if the server provided one. */
1108 TAILQ_FOREACH(pe, &symrefs, entry) {
1109 struct got_reference *symref, *target_ref;
1110 const char *refname = pe->path;
1111 const char *target = pe->data;
1113 if (strcmp(refname, GOT_REF_HEAD) != 0)
1114 continue;
1116 err = got_ref_open(&target_ref, repo, target, 0);
1117 if (err) {
1118 if (err->code == GOT_ERR_NOT_REF)
1119 continue;
1120 goto done;
1123 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1124 got_ref_close(target_ref);
1125 if (err)
1126 goto done;
1128 printf("Setting %s to %s\n", GOT_REF_HEAD,
1129 got_ref_get_symref_target(symref));
1131 err = got_ref_write(symref, repo);
1132 got_ref_close(symref);
1133 break;
1136 done:
1137 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1138 err = got_error_from_errno("close");
1139 if (repo)
1140 got_repo_close(repo);
1141 TAILQ_FOREACH(pe, &refs, entry) {
1142 free((void *)pe->path);
1143 free(pe->data);
1145 got_pathlist_free(&refs);
1146 TAILQ_FOREACH(pe, &symrefs, entry) {
1147 free((void *)pe->path);
1148 free(pe->data);
1150 got_pathlist_free(&symrefs);
1151 free(pack_hash);
1152 free(proto);
1153 free(host);
1154 free(port);
1155 free(server_path);
1156 free(repo_name);
1157 free(default_destdir);
1158 return err;
1161 static const struct got_error *
1162 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1164 static char msg[512];
1165 const char *branch_name;
1167 if (got_ref_is_symbolic(ref))
1168 branch_name = got_ref_get_symref_target(ref);
1169 else
1170 branch_name = got_ref_get_name(ref);
1172 if (strncmp("refs/heads/", branch_name, 11) == 0)
1173 branch_name += 11;
1175 snprintf(msg, sizeof(msg),
1176 "target commit is not contained in branch '%s'; "
1177 "the branch to use must be specified with -b; "
1178 "if necessary a new branch can be created for "
1179 "this commit with 'got branch -c %s BRANCH_NAME'",
1180 branch_name, commit_id_str);
1182 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1185 static const struct got_error *
1186 cmd_checkout(int argc, char *argv[])
1188 const struct got_error *error = NULL;
1189 struct got_repository *repo = NULL;
1190 struct got_reference *head_ref = NULL;
1191 struct got_worktree *worktree = NULL;
1192 char *repo_path = NULL;
1193 char *worktree_path = NULL;
1194 const char *path_prefix = "";
1195 const char *branch_name = GOT_REF_HEAD;
1196 char *commit_id_str = NULL;
1197 int ch, same_path_prefix, allow_nonempty = 0;
1198 struct got_pathlist_head paths;
1199 struct got_checkout_progress_arg cpa;
1201 TAILQ_INIT(&paths);
1203 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1204 switch (ch) {
1205 case 'b':
1206 branch_name = optarg;
1207 break;
1208 case 'c':
1209 commit_id_str = strdup(optarg);
1210 if (commit_id_str == NULL)
1211 return got_error_from_errno("strdup");
1212 break;
1213 case 'E':
1214 allow_nonempty = 1;
1215 break;
1216 case 'p':
1217 path_prefix = optarg;
1218 break;
1219 default:
1220 usage_checkout();
1221 /* NOTREACHED */
1225 argc -= optind;
1226 argv += optind;
1228 #ifndef PROFILE
1229 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1230 "unveil", NULL) == -1)
1231 err(1, "pledge");
1232 #endif
1233 if (argc == 1) {
1234 char *cwd, *base, *dotgit;
1235 repo_path = realpath(argv[0], NULL);
1236 if (repo_path == NULL)
1237 return got_error_from_errno2("realpath", argv[0]);
1238 cwd = getcwd(NULL, 0);
1239 if (cwd == NULL) {
1240 error = got_error_from_errno("getcwd");
1241 goto done;
1243 if (path_prefix[0]) {
1244 base = basename(path_prefix);
1245 if (base == NULL) {
1246 error = got_error_from_errno2("basename",
1247 path_prefix);
1248 goto done;
1250 } else {
1251 base = basename(repo_path);
1252 if (base == NULL) {
1253 error = got_error_from_errno2("basename",
1254 repo_path);
1255 goto done;
1258 dotgit = strstr(base, ".git");
1259 if (dotgit)
1260 *dotgit = '\0';
1261 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1262 error = got_error_from_errno("asprintf");
1263 free(cwd);
1264 goto done;
1266 free(cwd);
1267 } else if (argc == 2) {
1268 repo_path = realpath(argv[0], NULL);
1269 if (repo_path == NULL) {
1270 error = got_error_from_errno2("realpath", argv[0]);
1271 goto done;
1273 worktree_path = realpath(argv[1], NULL);
1274 if (worktree_path == NULL) {
1275 if (errno != ENOENT) {
1276 error = got_error_from_errno2("realpath",
1277 argv[1]);
1278 goto done;
1280 worktree_path = strdup(argv[1]);
1281 if (worktree_path == NULL) {
1282 error = got_error_from_errno("strdup");
1283 goto done;
1286 } else
1287 usage_checkout();
1289 got_path_strip_trailing_slashes(repo_path);
1290 got_path_strip_trailing_slashes(worktree_path);
1292 error = got_repo_open(&repo, repo_path, NULL);
1293 if (error != NULL)
1294 goto done;
1296 /* Pre-create work tree path for unveil(2) */
1297 error = got_path_mkdir(worktree_path);
1298 if (error) {
1299 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1300 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1301 goto done;
1302 if (!allow_nonempty &&
1303 !got_path_dir_is_empty(worktree_path)) {
1304 error = got_error_path(worktree_path,
1305 GOT_ERR_DIR_NOT_EMPTY);
1306 goto done;
1310 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1311 if (error)
1312 goto done;
1314 error = got_ref_open(&head_ref, repo, branch_name, 0);
1315 if (error != NULL)
1316 goto done;
1318 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1319 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1320 goto done;
1322 error = got_worktree_open(&worktree, worktree_path);
1323 if (error != NULL)
1324 goto done;
1326 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1327 path_prefix);
1328 if (error != NULL)
1329 goto done;
1330 if (!same_path_prefix) {
1331 error = got_error(GOT_ERR_PATH_PREFIX);
1332 goto done;
1335 if (commit_id_str) {
1336 struct got_object_id *commit_id;
1337 error = got_repo_match_object_id(&commit_id, NULL,
1338 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1339 if (error)
1340 goto done;
1341 error = check_linear_ancestry(commit_id,
1342 got_worktree_get_base_commit_id(worktree), 0, repo);
1343 if (error != NULL) {
1344 free(commit_id);
1345 if (error->code == GOT_ERR_ANCESTRY) {
1346 error = checkout_ancestry_error(
1347 head_ref, commit_id_str);
1349 goto done;
1351 error = check_same_branch(commit_id, head_ref, NULL, repo);
1352 if (error) {
1353 if (error->code == GOT_ERR_ANCESTRY) {
1354 error = checkout_ancestry_error(
1355 head_ref, commit_id_str);
1357 goto done;
1359 error = got_worktree_set_base_commit_id(worktree, repo,
1360 commit_id);
1361 free(commit_id);
1362 if (error)
1363 goto done;
1366 error = got_pathlist_append(&paths, "", NULL);
1367 if (error)
1368 goto done;
1369 cpa.worktree_path = worktree_path;
1370 cpa.had_base_commit_ref_error = 0;
1371 error = got_worktree_checkout_files(worktree, &paths, repo,
1372 checkout_progress, &cpa, check_cancelled, NULL);
1373 if (error != NULL)
1374 goto done;
1376 printf("Now shut up and hack\n");
1377 if (cpa.had_base_commit_ref_error)
1378 show_worktree_base_ref_warning();
1379 done:
1380 got_pathlist_free(&paths);
1381 free(commit_id_str);
1382 free(repo_path);
1383 free(worktree_path);
1384 return error;
1387 __dead static void
1388 usage_update(void)
1390 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1391 getprogname());
1392 exit(1);
1395 static const struct got_error *
1396 update_progress(void *arg, unsigned char status, const char *path)
1398 int *did_something = arg;
1400 if (status == GOT_STATUS_EXISTS ||
1401 status == GOT_STATUS_BASE_REF_ERR)
1402 return NULL;
1404 *did_something = 1;
1406 /* Base commit bump happens silently. */
1407 if (status == GOT_STATUS_BUMP_BASE)
1408 return NULL;
1410 while (path[0] == '/')
1411 path++;
1412 printf("%c %s\n", status, path);
1413 return NULL;
1416 static const struct got_error *
1417 switch_head_ref(struct got_reference *head_ref,
1418 struct got_object_id *commit_id, struct got_worktree *worktree,
1419 struct got_repository *repo)
1421 const struct got_error *err = NULL;
1422 char *base_id_str;
1423 int ref_has_moved = 0;
1425 /* Trivial case: switching between two different references. */
1426 if (strcmp(got_ref_get_name(head_ref),
1427 got_worktree_get_head_ref_name(worktree)) != 0) {
1428 printf("Switching work tree from %s to %s\n",
1429 got_worktree_get_head_ref_name(worktree),
1430 got_ref_get_name(head_ref));
1431 return got_worktree_set_head_ref(worktree, head_ref);
1434 err = check_linear_ancestry(commit_id,
1435 got_worktree_get_base_commit_id(worktree), 0, repo);
1436 if (err) {
1437 if (err->code != GOT_ERR_ANCESTRY)
1438 return err;
1439 ref_has_moved = 1;
1441 if (!ref_has_moved)
1442 return NULL;
1444 /* Switching to a rebased branch with the same reference name. */
1445 err = got_object_id_str(&base_id_str,
1446 got_worktree_get_base_commit_id(worktree));
1447 if (err)
1448 return err;
1449 printf("Reference %s now points at a different branch\n",
1450 got_worktree_get_head_ref_name(worktree));
1451 printf("Switching work tree from %s to %s\n", base_id_str,
1452 got_worktree_get_head_ref_name(worktree));
1453 return NULL;
1456 static const struct got_error *
1457 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1459 const struct got_error *err;
1460 int in_progress;
1462 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1463 if (err)
1464 return err;
1465 if (in_progress)
1466 return got_error(GOT_ERR_REBASING);
1468 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1469 if (err)
1470 return err;
1471 if (in_progress)
1472 return got_error(GOT_ERR_HISTEDIT_BUSY);
1474 return NULL;
1477 static const struct got_error *
1478 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1479 char *argv[], struct got_worktree *worktree)
1481 const struct got_error *err = NULL;
1482 char *path;
1483 int i;
1485 if (argc == 0) {
1486 path = strdup("");
1487 if (path == NULL)
1488 return got_error_from_errno("strdup");
1489 return got_pathlist_append(paths, path, NULL);
1492 for (i = 0; i < argc; i++) {
1493 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1494 if (err)
1495 break;
1496 err = got_pathlist_append(paths, path, NULL);
1497 if (err) {
1498 free(path);
1499 break;
1503 return err;
1506 static const struct got_error *
1507 cmd_update(int argc, char *argv[])
1509 const struct got_error *error = NULL;
1510 struct got_repository *repo = NULL;
1511 struct got_worktree *worktree = NULL;
1512 char *worktree_path = NULL;
1513 struct got_object_id *commit_id = NULL;
1514 char *commit_id_str = NULL;
1515 const char *branch_name = NULL;
1516 struct got_reference *head_ref = NULL;
1517 struct got_pathlist_head paths;
1518 struct got_pathlist_entry *pe;
1519 int ch, did_something = 0;
1521 TAILQ_INIT(&paths);
1523 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1524 switch (ch) {
1525 case 'b':
1526 branch_name = optarg;
1527 break;
1528 case 'c':
1529 commit_id_str = strdup(optarg);
1530 if (commit_id_str == NULL)
1531 return got_error_from_errno("strdup");
1532 break;
1533 default:
1534 usage_update();
1535 /* NOTREACHED */
1539 argc -= optind;
1540 argv += optind;
1542 #ifndef PROFILE
1543 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1544 "unveil", NULL) == -1)
1545 err(1, "pledge");
1546 #endif
1547 worktree_path = getcwd(NULL, 0);
1548 if (worktree_path == NULL) {
1549 error = got_error_from_errno("getcwd");
1550 goto done;
1552 error = got_worktree_open(&worktree, worktree_path);
1553 if (error)
1554 goto done;
1556 error = check_rebase_or_histedit_in_progress(worktree);
1557 if (error)
1558 goto done;
1560 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1561 NULL);
1562 if (error != NULL)
1563 goto done;
1565 error = apply_unveil(got_repo_get_path(repo), 0,
1566 got_worktree_get_root_path(worktree));
1567 if (error)
1568 goto done;
1570 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1571 if (error)
1572 goto done;
1574 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1575 got_worktree_get_head_ref_name(worktree), 0);
1576 if (error != NULL)
1577 goto done;
1578 if (commit_id_str == NULL) {
1579 error = got_ref_resolve(&commit_id, repo, head_ref);
1580 if (error != NULL)
1581 goto done;
1582 error = got_object_id_str(&commit_id_str, commit_id);
1583 if (error != NULL)
1584 goto done;
1585 } else {
1586 error = got_repo_match_object_id(&commit_id, NULL,
1587 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1588 free(commit_id_str);
1589 commit_id_str = NULL;
1590 if (error)
1591 goto done;
1592 error = got_object_id_str(&commit_id_str, commit_id);
1593 if (error)
1594 goto done;
1597 if (branch_name) {
1598 struct got_object_id *head_commit_id;
1599 TAILQ_FOREACH(pe, &paths, entry) {
1600 if (pe->path_len == 0)
1601 continue;
1602 error = got_error_msg(GOT_ERR_BAD_PATH,
1603 "switching between branches requires that "
1604 "the entire work tree gets updated");
1605 goto done;
1607 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1608 if (error)
1609 goto done;
1610 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1611 repo);
1612 free(head_commit_id);
1613 if (error != NULL)
1614 goto done;
1615 error = check_same_branch(commit_id, head_ref, NULL, repo);
1616 if (error)
1617 goto done;
1618 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1619 if (error)
1620 goto done;
1621 } else {
1622 error = check_linear_ancestry(commit_id,
1623 got_worktree_get_base_commit_id(worktree), 0, repo);
1624 if (error != NULL) {
1625 if (error->code == GOT_ERR_ANCESTRY)
1626 error = got_error(GOT_ERR_BRANCH_MOVED);
1627 goto done;
1629 error = check_same_branch(commit_id, head_ref, NULL, repo);
1630 if (error)
1631 goto done;
1634 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1635 commit_id) != 0) {
1636 error = got_worktree_set_base_commit_id(worktree, repo,
1637 commit_id);
1638 if (error)
1639 goto done;
1642 error = got_worktree_checkout_files(worktree, &paths, repo,
1643 update_progress, &did_something, check_cancelled, NULL);
1644 if (error != NULL)
1645 goto done;
1647 if (did_something)
1648 printf("Updated to commit %s\n", commit_id_str);
1649 else
1650 printf("Already up-to-date\n");
1651 done:
1652 free(worktree_path);
1653 TAILQ_FOREACH(pe, &paths, entry)
1654 free((char *)pe->path);
1655 got_pathlist_free(&paths);
1656 free(commit_id);
1657 free(commit_id_str);
1658 return error;
1661 static const struct got_error *
1662 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1663 const char *path, int diff_context, int ignore_whitespace,
1664 struct got_repository *repo)
1666 const struct got_error *err = NULL;
1667 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1669 if (blob_id1) {
1670 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1671 if (err)
1672 goto done;
1675 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1676 if (err)
1677 goto done;
1679 while (path[0] == '/')
1680 path++;
1681 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1682 ignore_whitespace, stdout);
1683 done:
1684 if (blob1)
1685 got_object_blob_close(blob1);
1686 got_object_blob_close(blob2);
1687 return err;
1690 static const struct got_error *
1691 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1692 const char *path, int diff_context, int ignore_whitespace,
1693 struct got_repository *repo)
1695 const struct got_error *err = NULL;
1696 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1697 struct got_diff_blob_output_unidiff_arg arg;
1699 if (tree_id1) {
1700 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1701 if (err)
1702 goto done;
1705 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1706 if (err)
1707 goto done;
1709 arg.diff_context = diff_context;
1710 arg.ignore_whitespace = ignore_whitespace;
1711 arg.outfile = stdout;
1712 while (path[0] == '/')
1713 path++;
1714 err = got_diff_tree(tree1, tree2, path, path, repo,
1715 got_diff_blob_output_unidiff, &arg, 1);
1716 done:
1717 if (tree1)
1718 got_object_tree_close(tree1);
1719 if (tree2)
1720 got_object_tree_close(tree2);
1721 return err;
1724 static const struct got_error *
1725 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1726 const char *path, int diff_context, struct got_repository *repo)
1728 const struct got_error *err = NULL;
1729 struct got_commit_object *pcommit = NULL;
1730 char *id_str1 = NULL, *id_str2 = NULL;
1731 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1732 struct got_object_qid *qid;
1734 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1735 if (qid != NULL) {
1736 err = got_object_open_as_commit(&pcommit, repo,
1737 qid->id);
1738 if (err)
1739 return err;
1742 if (path && path[0] != '\0') {
1743 int obj_type;
1744 err = got_object_id_by_path(&obj_id2, repo, id, path);
1745 if (err)
1746 goto done;
1747 err = got_object_id_str(&id_str2, obj_id2);
1748 if (err) {
1749 free(obj_id2);
1750 goto done;
1752 if (pcommit) {
1753 err = got_object_id_by_path(&obj_id1, repo,
1754 qid->id, path);
1755 if (err) {
1756 free(obj_id2);
1757 goto done;
1759 err = got_object_id_str(&id_str1, obj_id1);
1760 if (err) {
1761 free(obj_id2);
1762 goto done;
1765 err = got_object_get_type(&obj_type, repo, obj_id2);
1766 if (err) {
1767 free(obj_id2);
1768 goto done;
1770 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1771 switch (obj_type) {
1772 case GOT_OBJ_TYPE_BLOB:
1773 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1774 0, repo);
1775 break;
1776 case GOT_OBJ_TYPE_TREE:
1777 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1778 0, repo);
1779 break;
1780 default:
1781 err = got_error(GOT_ERR_OBJ_TYPE);
1782 break;
1784 free(obj_id1);
1785 free(obj_id2);
1786 } else {
1787 obj_id2 = got_object_commit_get_tree_id(commit);
1788 err = got_object_id_str(&id_str2, obj_id2);
1789 if (err)
1790 goto done;
1791 obj_id1 = got_object_commit_get_tree_id(pcommit);
1792 err = got_object_id_str(&id_str1, obj_id1);
1793 if (err)
1794 goto done;
1795 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1796 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1798 done:
1799 free(id_str1);
1800 free(id_str2);
1801 if (pcommit)
1802 got_object_commit_close(pcommit);
1803 return err;
1806 static char *
1807 get_datestr(time_t *time, char *datebuf)
1809 struct tm mytm, *tm;
1810 char *p, *s;
1812 tm = gmtime_r(time, &mytm);
1813 if (tm == NULL)
1814 return NULL;
1815 s = asctime_r(tm, datebuf);
1816 if (s == NULL)
1817 return NULL;
1818 p = strchr(s, '\n');
1819 if (p)
1820 *p = '\0';
1821 return s;
1824 static const struct got_error *
1825 match_logmsg(int *have_match, struct got_object_id *id,
1826 struct got_commit_object *commit, regex_t *regex)
1828 const struct got_error *err = NULL;
1829 regmatch_t regmatch;
1830 char *id_str = NULL, *logmsg = NULL;
1832 *have_match = 0;
1834 err = got_object_id_str(&id_str, id);
1835 if (err)
1836 return err;
1838 err = got_object_commit_get_logmsg(&logmsg, commit);
1839 if (err)
1840 goto done;
1842 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1843 *have_match = 1;
1844 done:
1845 free(id_str);
1846 free(logmsg);
1847 return err;
1850 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1852 static const struct got_error *
1853 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1854 struct got_repository *repo, const char *path, int show_patch,
1855 int diff_context, struct got_reflist_head *refs)
1857 const struct got_error *err = NULL;
1858 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1859 char datebuf[26];
1860 time_t committer_time;
1861 const char *author, *committer;
1862 char *refs_str = NULL;
1863 struct got_reflist_entry *re;
1865 SIMPLEQ_FOREACH(re, refs, entry) {
1866 char *s;
1867 const char *name;
1868 struct got_tag_object *tag = NULL;
1869 int cmp;
1871 name = got_ref_get_name(re->ref);
1872 if (strcmp(name, GOT_REF_HEAD) == 0)
1873 continue;
1874 if (strncmp(name, "refs/", 5) == 0)
1875 name += 5;
1876 if (strncmp(name, "got/", 4) == 0)
1877 continue;
1878 if (strncmp(name, "heads/", 6) == 0)
1879 name += 6;
1880 if (strncmp(name, "remotes/", 8) == 0)
1881 name += 8;
1882 if (strncmp(name, "tags/", 5) == 0) {
1883 err = got_object_open_as_tag(&tag, repo, re->id);
1884 if (err) {
1885 if (err->code != GOT_ERR_OBJ_TYPE)
1886 return err;
1887 /* Ref points at something other than a tag. */
1888 err = NULL;
1889 tag = NULL;
1892 cmp = got_object_id_cmp(tag ?
1893 got_object_tag_get_object_id(tag) : re->id, id);
1894 if (tag)
1895 got_object_tag_close(tag);
1896 if (cmp != 0)
1897 continue;
1898 s = refs_str;
1899 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1900 name) == -1) {
1901 err = got_error_from_errno("asprintf");
1902 free(s);
1903 return err;
1905 free(s);
1907 err = got_object_id_str(&id_str, id);
1908 if (err)
1909 return err;
1911 printf(GOT_COMMIT_SEP_STR);
1912 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1913 refs_str ? refs_str : "", refs_str ? ")" : "");
1914 free(id_str);
1915 id_str = NULL;
1916 free(refs_str);
1917 refs_str = NULL;
1918 printf("from: %s\n", got_object_commit_get_author(commit));
1919 committer_time = got_object_commit_get_committer_time(commit);
1920 datestr = get_datestr(&committer_time, datebuf);
1921 if (datestr)
1922 printf("date: %s UTC\n", datestr);
1923 author = got_object_commit_get_author(commit);
1924 committer = got_object_commit_get_committer(commit);
1925 if (strcmp(author, committer) != 0)
1926 printf("via: %s\n", committer);
1927 if (got_object_commit_get_nparents(commit) > 1) {
1928 const struct got_object_id_queue *parent_ids;
1929 struct got_object_qid *qid;
1930 int n = 1;
1931 parent_ids = got_object_commit_get_parent_ids(commit);
1932 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1933 err = got_object_id_str(&id_str, qid->id);
1934 if (err)
1935 return err;
1936 printf("parent %d: %s\n", n++, id_str);
1937 free(id_str);
1941 err = got_object_commit_get_logmsg(&logmsg0, commit);
1942 if (err)
1943 return err;
1945 logmsg = logmsg0;
1946 do {
1947 line = strsep(&logmsg, "\n");
1948 if (line)
1949 printf(" %s\n", line);
1950 } while (line);
1951 free(logmsg0);
1953 if (show_patch) {
1954 err = print_patch(commit, id, path, diff_context, repo);
1955 if (err == 0)
1956 printf("\n");
1959 if (fflush(stdout) != 0 && err == NULL)
1960 err = got_error_from_errno("fflush");
1961 return err;
1964 static const struct got_error *
1965 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1966 const char *path, int show_patch, const char *search_pattern,
1967 int diff_context, int limit, int log_branches,
1968 struct got_reflist_head *refs)
1970 const struct got_error *err;
1971 struct got_commit_graph *graph;
1972 regex_t regex;
1973 int have_match;
1975 if (search_pattern &&
1976 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1977 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1979 err = got_commit_graph_open(&graph, path, !log_branches);
1980 if (err)
1981 return err;
1982 err = got_commit_graph_iter_start(graph, root_id, repo,
1983 check_cancelled, NULL);
1984 if (err)
1985 goto done;
1986 for (;;) {
1987 struct got_commit_object *commit;
1988 struct got_object_id *id;
1990 if (sigint_received || sigpipe_received)
1991 break;
1993 err = got_commit_graph_iter_next(&id, graph, repo,
1994 check_cancelled, NULL);
1995 if (err) {
1996 if (err->code == GOT_ERR_ITER_COMPLETED)
1997 err = NULL;
1998 break;
2000 if (id == NULL)
2001 break;
2003 err = got_object_open_as_commit(&commit, repo, id);
2004 if (err)
2005 break;
2007 if (search_pattern) {
2008 err = match_logmsg(&have_match, id, commit, &regex);
2009 if (err) {
2010 got_object_commit_close(commit);
2011 break;
2013 if (have_match == 0) {
2014 got_object_commit_close(commit);
2015 continue;
2019 err = print_commit(commit, id, repo, path, show_patch,
2020 diff_context, refs);
2021 got_object_commit_close(commit);
2022 if (err || (limit && --limit == 0))
2023 break;
2025 done:
2026 if (search_pattern)
2027 regfree(&regex);
2028 got_commit_graph_close(graph);
2029 return err;
2032 __dead static void
2033 usage_log(void)
2035 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2036 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2037 exit(1);
2040 static int
2041 get_default_log_limit(void)
2043 const char *got_default_log_limit;
2044 long long n;
2045 const char *errstr;
2047 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2048 if (got_default_log_limit == NULL)
2049 return 0;
2050 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2051 if (errstr != NULL)
2052 return 0;
2053 return n;
2056 static const struct got_error *
2057 cmd_log(int argc, char *argv[])
2059 const struct got_error *error;
2060 struct got_repository *repo = NULL;
2061 struct got_worktree *worktree = NULL;
2062 struct got_commit_object *commit = NULL;
2063 struct got_object_id *id = NULL;
2064 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2065 const char *start_commit = NULL, *search_pattern = NULL;
2066 int diff_context = -1, ch;
2067 int show_patch = 0, limit = 0, log_branches = 0;
2068 const char *errstr;
2069 struct got_reflist_head refs;
2071 SIMPLEQ_INIT(&refs);
2073 #ifndef PROFILE
2074 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2075 NULL)
2076 == -1)
2077 err(1, "pledge");
2078 #endif
2080 limit = get_default_log_limit();
2082 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2083 switch (ch) {
2084 case 'p':
2085 show_patch = 1;
2086 break;
2087 case 'c':
2088 start_commit = optarg;
2089 break;
2090 case 'C':
2091 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2092 &errstr);
2093 if (errstr != NULL)
2094 err(1, "-C option %s", errstr);
2095 break;
2096 case 'l':
2097 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2098 if (errstr != NULL)
2099 err(1, "-l option %s", errstr);
2100 break;
2101 case 'b':
2102 log_branches = 1;
2103 break;
2104 case 'r':
2105 repo_path = realpath(optarg, NULL);
2106 if (repo_path == NULL)
2107 return got_error_from_errno2("realpath",
2108 optarg);
2109 got_path_strip_trailing_slashes(repo_path);
2110 break;
2111 case 's':
2112 search_pattern = optarg;
2113 break;
2114 default:
2115 usage_log();
2116 /* NOTREACHED */
2120 argc -= optind;
2121 argv += optind;
2123 if (diff_context == -1)
2124 diff_context = 3;
2125 else if (!show_patch)
2126 errx(1, "-C reguires -p");
2128 cwd = getcwd(NULL, 0);
2129 if (cwd == NULL) {
2130 error = got_error_from_errno("getcwd");
2131 goto done;
2134 error = got_worktree_open(&worktree, cwd);
2135 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2136 goto done;
2137 error = NULL;
2139 if (argc == 0) {
2140 path = strdup("");
2141 if (path == NULL) {
2142 error = got_error_from_errno("strdup");
2143 goto done;
2145 } else if (argc == 1) {
2146 if (worktree) {
2147 error = got_worktree_resolve_path(&path, worktree,
2148 argv[0]);
2149 if (error)
2150 goto done;
2151 } else {
2152 path = strdup(argv[0]);
2153 if (path == NULL) {
2154 error = got_error_from_errno("strdup");
2155 goto done;
2158 } else
2159 usage_log();
2161 if (repo_path == NULL) {
2162 repo_path = worktree ?
2163 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2165 if (repo_path == NULL) {
2166 error = got_error_from_errno("strdup");
2167 goto done;
2170 error = got_repo_open(&repo, repo_path, NULL);
2171 if (error != NULL)
2172 goto done;
2174 error = apply_unveil(got_repo_get_path(repo), 1,
2175 worktree ? got_worktree_get_root_path(worktree) : NULL);
2176 if (error)
2177 goto done;
2179 if (start_commit == NULL) {
2180 struct got_reference *head_ref;
2181 error = got_ref_open(&head_ref, repo,
2182 worktree ? got_worktree_get_head_ref_name(worktree)
2183 : GOT_REF_HEAD, 0);
2184 if (error != NULL)
2185 return error;
2186 error = got_ref_resolve(&id, repo, head_ref);
2187 got_ref_close(head_ref);
2188 if (error != NULL)
2189 return error;
2190 error = got_object_open_as_commit(&commit, repo, id);
2191 } else {
2192 struct got_reference *ref;
2193 error = got_ref_open(&ref, repo, start_commit, 0);
2194 if (error == NULL) {
2195 int obj_type;
2196 error = got_ref_resolve(&id, repo, ref);
2197 got_ref_close(ref);
2198 if (error != NULL)
2199 goto done;
2200 error = got_object_get_type(&obj_type, repo, id);
2201 if (error != NULL)
2202 goto done;
2203 if (obj_type == GOT_OBJ_TYPE_TAG) {
2204 struct got_tag_object *tag;
2205 error = got_object_open_as_tag(&tag, repo, id);
2206 if (error != NULL)
2207 goto done;
2208 if (got_object_tag_get_object_type(tag) !=
2209 GOT_OBJ_TYPE_COMMIT) {
2210 got_object_tag_close(tag);
2211 error = got_error(GOT_ERR_OBJ_TYPE);
2212 goto done;
2214 free(id);
2215 id = got_object_id_dup(
2216 got_object_tag_get_object_id(tag));
2217 if (id == NULL)
2218 error = got_error_from_errno(
2219 "got_object_id_dup");
2220 got_object_tag_close(tag);
2221 if (error)
2222 goto done;
2223 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2224 error = got_error(GOT_ERR_OBJ_TYPE);
2225 goto done;
2227 error = got_object_open_as_commit(&commit, repo, id);
2228 if (error != NULL)
2229 goto done;
2231 if (commit == NULL) {
2232 error = got_repo_match_object_id_prefix(&id,
2233 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2234 if (error != NULL)
2235 return error;
2238 if (error != NULL)
2239 goto done;
2241 if (worktree) {
2242 const char *prefix = got_worktree_get_path_prefix(worktree);
2243 char *p;
2244 if (asprintf(&p, "%s%s%s", prefix,
2245 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2246 error = got_error_from_errno("asprintf");
2247 goto done;
2249 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2250 free(p);
2251 } else
2252 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2253 if (error != NULL)
2254 goto done;
2255 if (in_repo_path) {
2256 free(path);
2257 path = in_repo_path;
2260 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2261 if (error)
2262 goto done;
2264 error = print_commits(id, repo, path, show_patch, search_pattern,
2265 diff_context, limit, log_branches, &refs);
2266 done:
2267 free(path);
2268 free(repo_path);
2269 free(cwd);
2270 free(id);
2271 if (worktree)
2272 got_worktree_close(worktree);
2273 if (repo) {
2274 const struct got_error *repo_error;
2275 repo_error = got_repo_close(repo);
2276 if (error == NULL)
2277 error = repo_error;
2279 got_ref_list_free(&refs);
2280 return error;
2283 __dead static void
2284 usage_diff(void)
2286 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2287 "[-w] [object1 object2 | path]\n", getprogname());
2288 exit(1);
2291 struct print_diff_arg {
2292 struct got_repository *repo;
2293 struct got_worktree *worktree;
2294 int diff_context;
2295 const char *id_str;
2296 int header_shown;
2297 int diff_staged;
2298 int ignore_whitespace;
2301 static const struct got_error *
2302 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2303 const char *path, struct got_object_id *blob_id,
2304 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2305 int dirfd, const char *de_name)
2307 struct print_diff_arg *a = arg;
2308 const struct got_error *err = NULL;
2309 struct got_blob_object *blob1 = NULL;
2310 int fd = -1;
2311 FILE *f2 = NULL;
2312 char *abspath = NULL, *label1 = NULL;
2313 struct stat sb;
2315 if (a->diff_staged) {
2316 if (staged_status != GOT_STATUS_MODIFY &&
2317 staged_status != GOT_STATUS_ADD &&
2318 staged_status != GOT_STATUS_DELETE)
2319 return NULL;
2320 } else {
2321 if (staged_status == GOT_STATUS_DELETE)
2322 return NULL;
2323 if (status == GOT_STATUS_NONEXISTENT)
2324 return got_error_set_errno(ENOENT, path);
2325 if (status != GOT_STATUS_MODIFY &&
2326 status != GOT_STATUS_ADD &&
2327 status != GOT_STATUS_DELETE &&
2328 status != GOT_STATUS_CONFLICT)
2329 return NULL;
2332 if (!a->header_shown) {
2333 printf("diff %s %s%s\n", a->id_str,
2334 got_worktree_get_root_path(a->worktree),
2335 a->diff_staged ? " (staged changes)" : "");
2336 a->header_shown = 1;
2339 if (a->diff_staged) {
2340 const char *label1 = NULL, *label2 = NULL;
2341 switch (staged_status) {
2342 case GOT_STATUS_MODIFY:
2343 label1 = path;
2344 label2 = path;
2345 break;
2346 case GOT_STATUS_ADD:
2347 label2 = path;
2348 break;
2349 case GOT_STATUS_DELETE:
2350 label1 = path;
2351 break;
2352 default:
2353 return got_error(GOT_ERR_FILE_STATUS);
2355 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2356 label1, label2, a->diff_context, a->ignore_whitespace,
2357 a->repo, stdout);
2360 if (staged_status == GOT_STATUS_ADD ||
2361 staged_status == GOT_STATUS_MODIFY) {
2362 char *id_str;
2363 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2364 8192);
2365 if (err)
2366 goto done;
2367 err = got_object_id_str(&id_str, staged_blob_id);
2368 if (err)
2369 goto done;
2370 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2371 err = got_error_from_errno("asprintf");
2372 free(id_str);
2373 goto done;
2375 free(id_str);
2376 } else if (status != GOT_STATUS_ADD) {
2377 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2378 if (err)
2379 goto done;
2382 if (status != GOT_STATUS_DELETE) {
2383 if (asprintf(&abspath, "%s/%s",
2384 got_worktree_get_root_path(a->worktree), path) == -1) {
2385 err = got_error_from_errno("asprintf");
2386 goto done;
2389 if (dirfd != -1) {
2390 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2391 if (fd == -1) {
2392 err = got_error_from_errno2("openat", abspath);
2393 goto done;
2395 } else {
2396 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2397 if (fd == -1) {
2398 err = got_error_from_errno2("open", abspath);
2399 goto done;
2402 if (fstat(fd, &sb) == -1) {
2403 err = got_error_from_errno2("fstat", abspath);
2404 goto done;
2406 f2 = fdopen(fd, "r");
2407 if (f2 == NULL) {
2408 err = got_error_from_errno2("fdopen", abspath);
2409 goto done;
2411 fd = -1;
2412 } else
2413 sb.st_size = 0;
2415 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2416 a->diff_context, a->ignore_whitespace, stdout);
2417 done:
2418 if (blob1)
2419 got_object_blob_close(blob1);
2420 if (f2 && fclose(f2) == EOF && err == NULL)
2421 err = got_error_from_errno("fclose");
2422 if (fd != -1 && close(fd) == -1 && err == NULL)
2423 err = got_error_from_errno("close");
2424 free(abspath);
2425 return err;
2428 static const struct got_error *
2429 cmd_diff(int argc, char *argv[])
2431 const struct got_error *error;
2432 struct got_repository *repo = NULL;
2433 struct got_worktree *worktree = NULL;
2434 char *cwd = NULL, *repo_path = NULL;
2435 struct got_object_id *id1 = NULL, *id2 = NULL;
2436 const char *id_str1 = NULL, *id_str2 = NULL;
2437 char *label1 = NULL, *label2 = NULL;
2438 int type1, type2;
2439 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2440 const char *errstr;
2441 char *path = NULL;
2443 #ifndef PROFILE
2444 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2445 NULL) == -1)
2446 err(1, "pledge");
2447 #endif
2449 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2450 switch (ch) {
2451 case 'C':
2452 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2453 &errstr);
2454 if (errstr != NULL)
2455 err(1, "-C option %s", errstr);
2456 break;
2457 case 'r':
2458 repo_path = realpath(optarg, NULL);
2459 if (repo_path == NULL)
2460 return got_error_from_errno2("realpath",
2461 optarg);
2462 got_path_strip_trailing_slashes(repo_path);
2463 break;
2464 case 's':
2465 diff_staged = 1;
2466 break;
2467 case 'w':
2468 ignore_whitespace = 1;
2469 break;
2470 default:
2471 usage_diff();
2472 /* NOTREACHED */
2476 argc -= optind;
2477 argv += optind;
2479 cwd = getcwd(NULL, 0);
2480 if (cwd == NULL) {
2481 error = got_error_from_errno("getcwd");
2482 goto done;
2484 error = got_worktree_open(&worktree, cwd);
2485 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2486 goto done;
2487 if (argc <= 1) {
2488 if (worktree == NULL) {
2489 error = got_error(GOT_ERR_NOT_WORKTREE);
2490 goto done;
2492 if (repo_path)
2493 errx(1,
2494 "-r option can't be used when diffing a work tree");
2495 repo_path = strdup(got_worktree_get_repo_path(worktree));
2496 if (repo_path == NULL) {
2497 error = got_error_from_errno("strdup");
2498 goto done;
2500 if (argc == 1) {
2501 error = got_worktree_resolve_path(&path, worktree,
2502 argv[0]);
2503 if (error)
2504 goto done;
2505 } else {
2506 path = strdup("");
2507 if (path == NULL) {
2508 error = got_error_from_errno("strdup");
2509 goto done;
2512 } else if (argc == 2) {
2513 if (diff_staged)
2514 errx(1, "-s option can't be used when diffing "
2515 "objects in repository");
2516 id_str1 = argv[0];
2517 id_str2 = argv[1];
2518 if (worktree && repo_path == NULL) {
2519 repo_path =
2520 strdup(got_worktree_get_repo_path(worktree));
2521 if (repo_path == NULL) {
2522 error = got_error_from_errno("strdup");
2523 goto done;
2526 } else
2527 usage_diff();
2529 if (repo_path == NULL) {
2530 repo_path = getcwd(NULL, 0);
2531 if (repo_path == NULL)
2532 return got_error_from_errno("getcwd");
2535 error = got_repo_open(&repo, repo_path, NULL);
2536 free(repo_path);
2537 if (error != NULL)
2538 goto done;
2540 error = apply_unveil(got_repo_get_path(repo), 1,
2541 worktree ? got_worktree_get_root_path(worktree) : NULL);
2542 if (error)
2543 goto done;
2545 if (argc <= 1) {
2546 struct print_diff_arg arg;
2547 struct got_pathlist_head paths;
2548 char *id_str;
2550 TAILQ_INIT(&paths);
2552 error = got_object_id_str(&id_str,
2553 got_worktree_get_base_commit_id(worktree));
2554 if (error)
2555 goto done;
2556 arg.repo = repo;
2557 arg.worktree = worktree;
2558 arg.diff_context = diff_context;
2559 arg.id_str = id_str;
2560 arg.header_shown = 0;
2561 arg.diff_staged = diff_staged;
2562 arg.ignore_whitespace = ignore_whitespace;
2564 error = got_pathlist_append(&paths, path, NULL);
2565 if (error)
2566 goto done;
2568 error = got_worktree_status(worktree, &paths, repo, print_diff,
2569 &arg, check_cancelled, NULL);
2570 free(id_str);
2571 got_pathlist_free(&paths);
2572 goto done;
2575 error = got_repo_match_object_id(&id1, &label1, id_str1,
2576 GOT_OBJ_TYPE_ANY, 1, repo);
2577 if (error)
2578 goto done;
2580 error = got_repo_match_object_id(&id2, &label2, id_str2,
2581 GOT_OBJ_TYPE_ANY, 1, repo);
2582 if (error)
2583 goto done;
2585 error = got_object_get_type(&type1, repo, id1);
2586 if (error)
2587 goto done;
2589 error = got_object_get_type(&type2, repo, id2);
2590 if (error)
2591 goto done;
2593 if (type1 != type2) {
2594 error = got_error(GOT_ERR_OBJ_TYPE);
2595 goto done;
2598 switch (type1) {
2599 case GOT_OBJ_TYPE_BLOB:
2600 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2601 diff_context, ignore_whitespace, repo, stdout);
2602 break;
2603 case GOT_OBJ_TYPE_TREE:
2604 error = got_diff_objects_as_trees(id1, id2, "", "",
2605 diff_context, ignore_whitespace, repo, stdout);
2606 break;
2607 case GOT_OBJ_TYPE_COMMIT:
2608 printf("diff %s %s\n", label1, label2);
2609 error = got_diff_objects_as_commits(id1, id2, diff_context,
2610 ignore_whitespace, repo, stdout);
2611 break;
2612 default:
2613 error = got_error(GOT_ERR_OBJ_TYPE);
2615 done:
2616 free(label1);
2617 free(label2);
2618 free(id1);
2619 free(id2);
2620 free(path);
2621 if (worktree)
2622 got_worktree_close(worktree);
2623 if (repo) {
2624 const struct got_error *repo_error;
2625 repo_error = got_repo_close(repo);
2626 if (error == NULL)
2627 error = repo_error;
2629 return error;
2632 __dead static void
2633 usage_blame(void)
2635 fprintf(stderr,
2636 "usage: %s blame [-c commit] [-r repository-path] path\n",
2637 getprogname());
2638 exit(1);
2641 struct blame_line {
2642 int annotated;
2643 char *id_str;
2644 char *committer;
2645 char datebuf[11]; /* YYYY-MM-DD + NUL */
2648 struct blame_cb_args {
2649 struct blame_line *lines;
2650 int nlines;
2651 int nlines_prec;
2652 int lineno_cur;
2653 off_t *line_offsets;
2654 FILE *f;
2655 struct got_repository *repo;
2658 static const struct got_error *
2659 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2661 const struct got_error *err = NULL;
2662 struct blame_cb_args *a = arg;
2663 struct blame_line *bline;
2664 char *line = NULL;
2665 size_t linesize = 0;
2666 struct got_commit_object *commit = NULL;
2667 off_t offset;
2668 struct tm tm;
2669 time_t committer_time;
2671 if (nlines != a->nlines ||
2672 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2673 return got_error(GOT_ERR_RANGE);
2675 if (sigint_received)
2676 return got_error(GOT_ERR_ITER_COMPLETED);
2678 if (lineno == -1)
2679 return NULL; /* no change in this commit */
2681 /* Annotate this line. */
2682 bline = &a->lines[lineno - 1];
2683 if (bline->annotated)
2684 return NULL;
2685 err = got_object_id_str(&bline->id_str, id);
2686 if (err)
2687 return err;
2689 err = got_object_open_as_commit(&commit, a->repo, id);
2690 if (err)
2691 goto done;
2693 bline->committer = strdup(got_object_commit_get_committer(commit));
2694 if (bline->committer == NULL) {
2695 err = got_error_from_errno("strdup");
2696 goto done;
2699 committer_time = got_object_commit_get_committer_time(commit);
2700 if (localtime_r(&committer_time, &tm) == NULL)
2701 return got_error_from_errno("localtime_r");
2702 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2703 &tm) >= sizeof(bline->datebuf)) {
2704 err = got_error(GOT_ERR_NO_SPACE);
2705 goto done;
2707 bline->annotated = 1;
2709 /* Print lines annotated so far. */
2710 bline = &a->lines[a->lineno_cur - 1];
2711 if (!bline->annotated)
2712 goto done;
2714 offset = a->line_offsets[a->lineno_cur - 1];
2715 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2716 err = got_error_from_errno("fseeko");
2717 goto done;
2720 while (bline->annotated) {
2721 char *smallerthan, *at, *nl, *committer;
2722 size_t len;
2724 if (getline(&line, &linesize, a->f) == -1) {
2725 if (ferror(a->f))
2726 err = got_error_from_errno("getline");
2727 break;
2730 committer = bline->committer;
2731 smallerthan = strchr(committer, '<');
2732 if (smallerthan && smallerthan[1] != '\0')
2733 committer = smallerthan + 1;
2734 at = strchr(committer, '@');
2735 if (at)
2736 *at = '\0';
2737 len = strlen(committer);
2738 if (len >= 9)
2739 committer[8] = '\0';
2741 nl = strchr(line, '\n');
2742 if (nl)
2743 *nl = '\0';
2744 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2745 bline->id_str, bline->datebuf, committer, line);
2747 a->lineno_cur++;
2748 bline = &a->lines[a->lineno_cur - 1];
2750 done:
2751 if (commit)
2752 got_object_commit_close(commit);
2753 free(line);
2754 return err;
2757 static const struct got_error *
2758 cmd_blame(int argc, char *argv[])
2760 const struct got_error *error;
2761 struct got_repository *repo = NULL;
2762 struct got_worktree *worktree = NULL;
2763 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2764 struct got_object_id *obj_id = NULL;
2765 struct got_object_id *commit_id = NULL;
2766 struct got_blob_object *blob = NULL;
2767 char *commit_id_str = NULL;
2768 struct blame_cb_args bca;
2769 int ch, obj_type, i;
2770 size_t filesize;
2772 memset(&bca, 0, sizeof(bca));
2774 #ifndef PROFILE
2775 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2776 NULL) == -1)
2777 err(1, "pledge");
2778 #endif
2780 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2781 switch (ch) {
2782 case 'c':
2783 commit_id_str = optarg;
2784 break;
2785 case 'r':
2786 repo_path = realpath(optarg, NULL);
2787 if (repo_path == NULL)
2788 return got_error_from_errno2("realpath",
2789 optarg);
2790 got_path_strip_trailing_slashes(repo_path);
2791 break;
2792 default:
2793 usage_blame();
2794 /* NOTREACHED */
2798 argc -= optind;
2799 argv += optind;
2801 if (argc == 1)
2802 path = argv[0];
2803 else
2804 usage_blame();
2806 cwd = getcwd(NULL, 0);
2807 if (cwd == NULL) {
2808 error = got_error_from_errno("getcwd");
2809 goto done;
2811 if (repo_path == NULL) {
2812 error = got_worktree_open(&worktree, cwd);
2813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2814 goto done;
2815 else
2816 error = NULL;
2817 if (worktree) {
2818 repo_path =
2819 strdup(got_worktree_get_repo_path(worktree));
2820 if (repo_path == NULL)
2821 error = got_error_from_errno("strdup");
2822 if (error)
2823 goto done;
2824 } else {
2825 repo_path = strdup(cwd);
2826 if (repo_path == NULL) {
2827 error = got_error_from_errno("strdup");
2828 goto done;
2833 error = got_repo_open(&repo, repo_path, NULL);
2834 if (error != NULL)
2835 goto done;
2837 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2838 if (error)
2839 goto done;
2841 if (worktree) {
2842 const char *prefix = got_worktree_get_path_prefix(worktree);
2843 char *p, *worktree_subdir = cwd +
2844 strlen(got_worktree_get_root_path(worktree));
2845 if (asprintf(&p, "%s%s%s%s%s",
2846 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2847 worktree_subdir, worktree_subdir[0] ? "/" : "",
2848 path) == -1) {
2849 error = got_error_from_errno("asprintf");
2850 goto done;
2852 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2853 free(p);
2854 } else {
2855 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2857 if (error)
2858 goto done;
2860 if (commit_id_str == NULL) {
2861 struct got_reference *head_ref;
2862 error = got_ref_open(&head_ref, repo, worktree ?
2863 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2864 if (error != NULL)
2865 goto done;
2866 error = got_ref_resolve(&commit_id, repo, head_ref);
2867 got_ref_close(head_ref);
2868 if (error != NULL)
2869 goto done;
2870 } else {
2871 error = got_repo_match_object_id(&commit_id, NULL,
2872 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2873 if (error)
2874 goto done;
2877 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2878 if (error)
2879 goto done;
2881 error = got_object_get_type(&obj_type, repo, obj_id);
2882 if (error)
2883 goto done;
2885 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2886 error = got_error(GOT_ERR_OBJ_TYPE);
2887 goto done;
2890 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2891 if (error)
2892 goto done;
2893 bca.f = got_opentemp();
2894 if (bca.f == NULL) {
2895 error = got_error_from_errno("got_opentemp");
2896 goto done;
2898 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2899 &bca.line_offsets, bca.f, blob);
2900 if (error || bca.nlines == 0)
2901 goto done;
2903 /* Don't include \n at EOF in the blame line count. */
2904 if (bca.line_offsets[bca.nlines - 1] == filesize)
2905 bca.nlines--;
2907 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2908 if (bca.lines == NULL) {
2909 error = got_error_from_errno("calloc");
2910 goto done;
2912 bca.lineno_cur = 1;
2913 bca.nlines_prec = 0;
2914 i = bca.nlines;
2915 while (i > 0) {
2916 i /= 10;
2917 bca.nlines_prec++;
2919 bca.repo = repo;
2921 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2922 check_cancelled, NULL);
2923 done:
2924 free(in_repo_path);
2925 free(repo_path);
2926 free(cwd);
2927 free(commit_id);
2928 free(obj_id);
2929 if (blob)
2930 got_object_blob_close(blob);
2931 if (worktree)
2932 got_worktree_close(worktree);
2933 if (repo) {
2934 const struct got_error *repo_error;
2935 repo_error = got_repo_close(repo);
2936 if (error == NULL)
2937 error = repo_error;
2939 if (bca.lines) {
2940 for (i = 0; i < bca.nlines; i++) {
2941 struct blame_line *bline = &bca.lines[i];
2942 free(bline->id_str);
2943 free(bline->committer);
2945 free(bca.lines);
2947 free(bca.line_offsets);
2948 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2949 error = got_error_from_errno("fclose");
2950 return error;
2953 __dead static void
2954 usage_tree(void)
2956 fprintf(stderr,
2957 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2958 getprogname());
2959 exit(1);
2962 static void
2963 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2964 const char *root_path)
2966 int is_root_path = (strcmp(path, root_path) == 0);
2967 const char *modestr = "";
2968 mode_t mode = got_tree_entry_get_mode(te);
2970 path += strlen(root_path);
2971 while (path[0] == '/')
2972 path++;
2974 if (got_object_tree_entry_is_submodule(te))
2975 modestr = "$";
2976 else if (S_ISLNK(mode))
2977 modestr = "@";
2978 else if (S_ISDIR(mode))
2979 modestr = "/";
2980 else if (mode & S_IXUSR)
2981 modestr = "*";
2983 printf("%s%s%s%s%s\n", id ? id : "", path,
2984 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2987 static const struct got_error *
2988 print_tree(const char *path, struct got_object_id *commit_id,
2989 int show_ids, int recurse, const char *root_path,
2990 struct got_repository *repo)
2992 const struct got_error *err = NULL;
2993 struct got_object_id *tree_id = NULL;
2994 struct got_tree_object *tree = NULL;
2995 int nentries, i;
2997 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2998 if (err)
2999 goto done;
3001 err = got_object_open_as_tree(&tree, repo, tree_id);
3002 if (err)
3003 goto done;
3004 nentries = got_object_tree_get_nentries(tree);
3005 for (i = 0; i < nentries; i++) {
3006 struct got_tree_entry *te;
3007 char *id = NULL;
3009 if (sigint_received || sigpipe_received)
3010 break;
3012 te = got_object_tree_get_entry(tree, i);
3013 if (show_ids) {
3014 char *id_str;
3015 err = got_object_id_str(&id_str,
3016 got_tree_entry_get_id(te));
3017 if (err)
3018 goto done;
3019 if (asprintf(&id, "%s ", id_str) == -1) {
3020 err = got_error_from_errno("asprintf");
3021 free(id_str);
3022 goto done;
3024 free(id_str);
3026 print_entry(te, id, path, root_path);
3027 free(id);
3029 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3030 char *child_path;
3031 if (asprintf(&child_path, "%s%s%s", path,
3032 path[0] == '/' && path[1] == '\0' ? "" : "/",
3033 got_tree_entry_get_name(te)) == -1) {
3034 err = got_error_from_errno("asprintf");
3035 goto done;
3037 err = print_tree(child_path, commit_id, show_ids, 1,
3038 root_path, repo);
3039 free(child_path);
3040 if (err)
3041 goto done;
3044 done:
3045 if (tree)
3046 got_object_tree_close(tree);
3047 free(tree_id);
3048 return err;
3051 static const struct got_error *
3052 cmd_tree(int argc, char *argv[])
3054 const struct got_error *error;
3055 struct got_repository *repo = NULL;
3056 struct got_worktree *worktree = NULL;
3057 const char *path;
3058 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3059 struct got_object_id *commit_id = NULL;
3060 char *commit_id_str = NULL;
3061 int show_ids = 0, recurse = 0;
3062 int ch;
3064 #ifndef PROFILE
3065 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3066 NULL) == -1)
3067 err(1, "pledge");
3068 #endif
3070 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3071 switch (ch) {
3072 case 'c':
3073 commit_id_str = optarg;
3074 break;
3075 case 'r':
3076 repo_path = realpath(optarg, NULL);
3077 if (repo_path == NULL)
3078 return got_error_from_errno2("realpath",
3079 optarg);
3080 got_path_strip_trailing_slashes(repo_path);
3081 break;
3082 case 'i':
3083 show_ids = 1;
3084 break;
3085 case 'R':
3086 recurse = 1;
3087 break;
3088 default:
3089 usage_tree();
3090 /* NOTREACHED */
3094 argc -= optind;
3095 argv += optind;
3097 if (argc == 1)
3098 path = argv[0];
3099 else if (argc > 1)
3100 usage_tree();
3101 else
3102 path = NULL;
3104 cwd = getcwd(NULL, 0);
3105 if (cwd == NULL) {
3106 error = got_error_from_errno("getcwd");
3107 goto done;
3109 if (repo_path == NULL) {
3110 error = got_worktree_open(&worktree, cwd);
3111 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3112 goto done;
3113 else
3114 error = NULL;
3115 if (worktree) {
3116 repo_path =
3117 strdup(got_worktree_get_repo_path(worktree));
3118 if (repo_path == NULL)
3119 error = got_error_from_errno("strdup");
3120 if (error)
3121 goto done;
3122 } else {
3123 repo_path = strdup(cwd);
3124 if (repo_path == NULL) {
3125 error = got_error_from_errno("strdup");
3126 goto done;
3131 error = got_repo_open(&repo, repo_path, NULL);
3132 if (error != NULL)
3133 goto done;
3135 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3136 if (error)
3137 goto done;
3139 if (path == NULL) {
3140 if (worktree) {
3141 char *p, *worktree_subdir = cwd +
3142 strlen(got_worktree_get_root_path(worktree));
3143 if (asprintf(&p, "%s/%s",
3144 got_worktree_get_path_prefix(worktree),
3145 worktree_subdir) == -1) {
3146 error = got_error_from_errno("asprintf");
3147 goto done;
3149 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3150 free(p);
3151 if (error)
3152 goto done;
3153 } else
3154 path = "/";
3156 if (in_repo_path == NULL) {
3157 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3158 if (error != NULL)
3159 goto done;
3162 if (commit_id_str == NULL) {
3163 struct got_reference *head_ref;
3164 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3165 if (error != NULL)
3166 goto done;
3167 error = got_ref_resolve(&commit_id, repo, head_ref);
3168 got_ref_close(head_ref);
3169 if (error != NULL)
3170 goto done;
3171 } else {
3172 error = got_repo_match_object_id(&commit_id, NULL,
3173 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3174 if (error)
3175 goto done;
3178 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3179 in_repo_path, repo);
3180 done:
3181 free(in_repo_path);
3182 free(repo_path);
3183 free(cwd);
3184 free(commit_id);
3185 if (worktree)
3186 got_worktree_close(worktree);
3187 if (repo) {
3188 const struct got_error *repo_error;
3189 repo_error = got_repo_close(repo);
3190 if (error == NULL)
3191 error = repo_error;
3193 return error;
3196 __dead static void
3197 usage_status(void)
3199 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3200 exit(1);
3203 static const struct got_error *
3204 print_status(void *arg, unsigned char status, unsigned char staged_status,
3205 const char *path, struct got_object_id *blob_id,
3206 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3207 int dirfd, const char *de_name)
3209 if (status == staged_status && (status == GOT_STATUS_DELETE))
3210 status = GOT_STATUS_NO_CHANGE;
3211 printf("%c%c %s\n", status, staged_status, path);
3212 return NULL;
3215 static const struct got_error *
3216 cmd_status(int argc, char *argv[])
3218 const struct got_error *error = NULL;
3219 struct got_repository *repo = NULL;
3220 struct got_worktree *worktree = NULL;
3221 char *cwd = NULL;
3222 struct got_pathlist_head paths;
3223 struct got_pathlist_entry *pe;
3224 int ch;
3226 TAILQ_INIT(&paths);
3228 while ((ch = getopt(argc, argv, "")) != -1) {
3229 switch (ch) {
3230 default:
3231 usage_status();
3232 /* NOTREACHED */
3236 argc -= optind;
3237 argv += optind;
3239 #ifndef PROFILE
3240 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3241 NULL) == -1)
3242 err(1, "pledge");
3243 #endif
3244 cwd = getcwd(NULL, 0);
3245 if (cwd == NULL) {
3246 error = got_error_from_errno("getcwd");
3247 goto done;
3250 error = got_worktree_open(&worktree, cwd);
3251 if (error != NULL)
3252 goto done;
3254 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3255 NULL);
3256 if (error != NULL)
3257 goto done;
3259 error = apply_unveil(got_repo_get_path(repo), 1,
3260 got_worktree_get_root_path(worktree));
3261 if (error)
3262 goto done;
3264 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3265 if (error)
3266 goto done;
3268 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3269 check_cancelled, NULL);
3270 done:
3271 TAILQ_FOREACH(pe, &paths, entry)
3272 free((char *)pe->path);
3273 got_pathlist_free(&paths);
3274 free(cwd);
3275 return error;
3278 __dead static void
3279 usage_ref(void)
3281 fprintf(stderr,
3282 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3283 getprogname());
3284 exit(1);
3287 static const struct got_error *
3288 list_refs(struct got_repository *repo)
3290 static const struct got_error *err = NULL;
3291 struct got_reflist_head refs;
3292 struct got_reflist_entry *re;
3294 SIMPLEQ_INIT(&refs);
3295 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3296 if (err)
3297 return err;
3299 SIMPLEQ_FOREACH(re, &refs, entry) {
3300 char *refstr;
3301 refstr = got_ref_to_str(re->ref);
3302 if (refstr == NULL)
3303 return got_error_from_errno("got_ref_to_str");
3304 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3305 free(refstr);
3308 got_ref_list_free(&refs);
3309 return NULL;
3312 static const struct got_error *
3313 delete_ref(struct got_repository *repo, const char *refname)
3315 const struct got_error *err = NULL;
3316 struct got_reference *ref;
3318 err = got_ref_open(&ref, repo, refname, 0);
3319 if (err)
3320 return err;
3322 err = got_ref_delete(ref, repo);
3323 got_ref_close(ref);
3324 return err;
3327 static const struct got_error *
3328 add_ref(struct got_repository *repo, const char *refname, const char *target)
3330 const struct got_error *err = NULL;
3331 struct got_object_id *id;
3332 struct got_reference *ref = NULL;
3335 * Don't let the user create a reference name with a leading '-'.
3336 * While technically a valid reference name, this case is usually
3337 * an unintended typo.
3339 if (refname[0] == '-')
3340 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3342 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3343 repo);
3344 if (err) {
3345 struct got_reference *target_ref;
3347 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3348 return err;
3349 err = got_ref_open(&target_ref, repo, target, 0);
3350 if (err)
3351 return err;
3352 err = got_ref_resolve(&id, repo, target_ref);
3353 got_ref_close(target_ref);
3354 if (err)
3355 return err;
3358 err = got_ref_alloc(&ref, refname, id);
3359 if (err)
3360 goto done;
3362 err = got_ref_write(ref, repo);
3363 done:
3364 if (ref)
3365 got_ref_close(ref);
3366 free(id);
3367 return err;
3370 static const struct got_error *
3371 add_symref(struct got_repository *repo, const char *refname, const char *target)
3373 const struct got_error *err = NULL;
3374 struct got_reference *ref = NULL;
3375 struct got_reference *target_ref = NULL;
3378 * Don't let the user create a reference name with a leading '-'.
3379 * While technically a valid reference name, this case is usually
3380 * an unintended typo.
3382 if (refname[0] == '-')
3383 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3385 err = got_ref_open(&target_ref, repo, target, 0);
3386 if (err)
3387 return err;
3389 err = got_ref_alloc_symref(&ref, refname, target_ref);
3390 if (err)
3391 goto done;
3393 err = got_ref_write(ref, repo);
3394 done:
3395 if (target_ref)
3396 got_ref_close(target_ref);
3397 if (ref)
3398 got_ref_close(ref);
3399 return err;
3402 static const struct got_error *
3403 cmd_ref(int argc, char *argv[])
3405 const struct got_error *error = NULL;
3406 struct got_repository *repo = NULL;
3407 struct got_worktree *worktree = NULL;
3408 char *cwd = NULL, *repo_path = NULL;
3409 int ch, do_list = 0, create_symref = 0;
3410 const char *delref = NULL;
3412 /* TODO: Add -s option for adding symbolic references. */
3413 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3414 switch (ch) {
3415 case 'd':
3416 delref = optarg;
3417 break;
3418 case 'r':
3419 repo_path = realpath(optarg, NULL);
3420 if (repo_path == NULL)
3421 return got_error_from_errno2("realpath",
3422 optarg);
3423 got_path_strip_trailing_slashes(repo_path);
3424 break;
3425 case 'l':
3426 do_list = 1;
3427 break;
3428 case 's':
3429 create_symref = 1;
3430 break;
3431 default:
3432 usage_ref();
3433 /* NOTREACHED */
3437 if (do_list && delref)
3438 errx(1, "-l and -d options are mutually exclusive\n");
3440 argc -= optind;
3441 argv += optind;
3443 if (do_list || delref) {
3444 if (create_symref)
3445 errx(1, "-s option cannot be used together with the "
3446 "-l or -d options");
3447 if (argc > 0)
3448 usage_ref();
3449 } else if (argc != 2)
3450 usage_ref();
3452 #ifndef PROFILE
3453 if (do_list) {
3454 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3455 NULL) == -1)
3456 err(1, "pledge");
3457 } else {
3458 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3459 "sendfd unveil", NULL) == -1)
3460 err(1, "pledge");
3462 #endif
3463 cwd = getcwd(NULL, 0);
3464 if (cwd == NULL) {
3465 error = got_error_from_errno("getcwd");
3466 goto done;
3469 if (repo_path == NULL) {
3470 error = got_worktree_open(&worktree, cwd);
3471 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3472 goto done;
3473 else
3474 error = NULL;
3475 if (worktree) {
3476 repo_path =
3477 strdup(got_worktree_get_repo_path(worktree));
3478 if (repo_path == NULL)
3479 error = got_error_from_errno("strdup");
3480 if (error)
3481 goto done;
3482 } else {
3483 repo_path = strdup(cwd);
3484 if (repo_path == NULL) {
3485 error = got_error_from_errno("strdup");
3486 goto done;
3491 error = got_repo_open(&repo, repo_path, NULL);
3492 if (error != NULL)
3493 goto done;
3495 error = apply_unveil(got_repo_get_path(repo), do_list,
3496 worktree ? got_worktree_get_root_path(worktree) : NULL);
3497 if (error)
3498 goto done;
3500 if (do_list)
3501 error = list_refs(repo);
3502 else if (delref)
3503 error = delete_ref(repo, delref);
3504 else if (create_symref)
3505 error = add_symref(repo, argv[0], argv[1]);
3506 else
3507 error = add_ref(repo, argv[0], argv[1]);
3508 done:
3509 if (repo)
3510 got_repo_close(repo);
3511 if (worktree)
3512 got_worktree_close(worktree);
3513 free(cwd);
3514 free(repo_path);
3515 return error;
3518 __dead static void
3519 usage_branch(void)
3521 fprintf(stderr,
3522 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3523 "[name]\n", getprogname());
3524 exit(1);
3527 static const struct got_error *
3528 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3529 struct got_reference *ref)
3531 const struct got_error *err = NULL;
3532 const char *refname, *marker = " ";
3533 char *refstr;
3535 refname = got_ref_get_name(ref);
3536 if (worktree && strcmp(refname,
3537 got_worktree_get_head_ref_name(worktree)) == 0) {
3538 struct got_object_id *id = NULL;
3540 err = got_ref_resolve(&id, repo, ref);
3541 if (err)
3542 return err;
3543 if (got_object_id_cmp(id,
3544 got_worktree_get_base_commit_id(worktree)) == 0)
3545 marker = "* ";
3546 else
3547 marker = "~ ";
3548 free(id);
3551 if (strncmp(refname, "refs/heads/", 11) == 0)
3552 refname += 11;
3553 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3554 refname += 18;
3556 refstr = got_ref_to_str(ref);
3557 if (refstr == NULL)
3558 return got_error_from_errno("got_ref_to_str");
3560 printf("%s%s: %s\n", marker, refname, refstr);
3561 free(refstr);
3562 return NULL;
3565 static const struct got_error *
3566 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3568 const char *refname;
3570 if (worktree == NULL)
3571 return got_error(GOT_ERR_NOT_WORKTREE);
3573 refname = got_worktree_get_head_ref_name(worktree);
3575 if (strncmp(refname, "refs/heads/", 11) == 0)
3576 refname += 11;
3577 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3578 refname += 18;
3580 printf("%s\n", refname);
3582 return NULL;
3585 static const struct got_error *
3586 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3588 static const struct got_error *err = NULL;
3589 struct got_reflist_head refs;
3590 struct got_reflist_entry *re;
3591 struct got_reference *temp_ref = NULL;
3592 int rebase_in_progress, histedit_in_progress;
3594 SIMPLEQ_INIT(&refs);
3596 if (worktree) {
3597 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3598 worktree);
3599 if (err)
3600 return err;
3602 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3603 worktree);
3604 if (err)
3605 return err;
3607 if (rebase_in_progress || histedit_in_progress) {
3608 err = got_ref_open(&temp_ref, repo,
3609 got_worktree_get_head_ref_name(worktree), 0);
3610 if (err)
3611 return err;
3612 list_branch(repo, worktree, temp_ref);
3613 got_ref_close(temp_ref);
3617 err = got_ref_list(&refs, repo, "refs/heads",
3618 got_ref_cmp_by_name, NULL);
3619 if (err)
3620 return err;
3622 SIMPLEQ_FOREACH(re, &refs, entry)
3623 list_branch(repo, worktree, re->ref);
3625 got_ref_list_free(&refs);
3626 return NULL;
3629 static const struct got_error *
3630 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3631 const char *branch_name)
3633 const struct got_error *err = NULL;
3634 struct got_reference *ref = NULL;
3635 char *refname;
3637 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3638 return got_error_from_errno("asprintf");
3640 err = got_ref_open(&ref, repo, refname, 0);
3641 if (err)
3642 goto done;
3644 if (worktree &&
3645 strcmp(got_worktree_get_head_ref_name(worktree),
3646 got_ref_get_name(ref)) == 0) {
3647 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3648 "will not delete this work tree's current branch");
3649 goto done;
3652 err = got_ref_delete(ref, repo);
3653 done:
3654 if (ref)
3655 got_ref_close(ref);
3656 free(refname);
3657 return err;
3660 static const struct got_error *
3661 add_branch(struct got_repository *repo, const char *branch_name,
3662 struct got_object_id *base_commit_id)
3664 const struct got_error *err = NULL;
3665 struct got_reference *ref = NULL;
3666 char *base_refname = NULL, *refname = NULL;
3669 * Don't let the user create a branch name with a leading '-'.
3670 * While technically a valid reference name, this case is usually
3671 * an unintended typo.
3673 if (branch_name[0] == '-')
3674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3677 err = got_error_from_errno("asprintf");
3678 goto done;
3681 err = got_ref_open(&ref, repo, refname, 0);
3682 if (err == NULL) {
3683 err = got_error(GOT_ERR_BRANCH_EXISTS);
3684 goto done;
3685 } else if (err->code != GOT_ERR_NOT_REF)
3686 goto done;
3688 err = got_ref_alloc(&ref, refname, base_commit_id);
3689 if (err)
3690 goto done;
3692 err = got_ref_write(ref, repo);
3693 done:
3694 if (ref)
3695 got_ref_close(ref);
3696 free(base_refname);
3697 free(refname);
3698 return err;
3701 static const struct got_error *
3702 cmd_branch(int argc, char *argv[])
3704 const struct got_error *error = NULL;
3705 struct got_repository *repo = NULL;
3706 struct got_worktree *worktree = NULL;
3707 char *cwd = NULL, *repo_path = NULL;
3708 int ch, do_list = 0, do_show = 0, do_update = 1;
3709 const char *delref = NULL, *commit_id_arg = NULL;
3710 struct got_reference *ref = NULL;
3711 struct got_pathlist_head paths;
3712 struct got_pathlist_entry *pe;
3713 struct got_object_id *commit_id = NULL;
3714 char *commit_id_str = NULL;
3716 TAILQ_INIT(&paths);
3718 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3719 switch (ch) {
3720 case 'c':
3721 commit_id_arg = optarg;
3722 break;
3723 case 'd':
3724 delref = optarg;
3725 break;
3726 case 'r':
3727 repo_path = realpath(optarg, NULL);
3728 if (repo_path == NULL)
3729 return got_error_from_errno2("realpath",
3730 optarg);
3731 got_path_strip_trailing_slashes(repo_path);
3732 break;
3733 case 'l':
3734 do_list = 1;
3735 break;
3736 case 'n':
3737 do_update = 0;
3738 break;
3739 default:
3740 usage_branch();
3741 /* NOTREACHED */
3745 if (do_list && delref)
3746 errx(1, "-l and -d options are mutually exclusive\n");
3748 argc -= optind;
3749 argv += optind;
3751 if (!do_list && !delref && argc == 0)
3752 do_show = 1;
3754 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3755 errx(1, "-c option can only be used when creating a branch");
3757 if (do_list || delref) {
3758 if (argc > 0)
3759 usage_branch();
3760 } else if (!do_show && argc != 1)
3761 usage_branch();
3763 #ifndef PROFILE
3764 if (do_list || do_show) {
3765 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3766 NULL) == -1)
3767 err(1, "pledge");
3768 } else {
3769 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3770 "sendfd unveil", NULL) == -1)
3771 err(1, "pledge");
3773 #endif
3774 cwd = getcwd(NULL, 0);
3775 if (cwd == NULL) {
3776 error = got_error_from_errno("getcwd");
3777 goto done;
3780 if (repo_path == NULL) {
3781 error = got_worktree_open(&worktree, cwd);
3782 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3783 goto done;
3784 else
3785 error = NULL;
3786 if (worktree) {
3787 repo_path =
3788 strdup(got_worktree_get_repo_path(worktree));
3789 if (repo_path == NULL)
3790 error = got_error_from_errno("strdup");
3791 if (error)
3792 goto done;
3793 } else {
3794 repo_path = strdup(cwd);
3795 if (repo_path == NULL) {
3796 error = got_error_from_errno("strdup");
3797 goto done;
3802 error = got_repo_open(&repo, repo_path, NULL);
3803 if (error != NULL)
3804 goto done;
3806 error = apply_unveil(got_repo_get_path(repo), do_list,
3807 worktree ? got_worktree_get_root_path(worktree) : NULL);
3808 if (error)
3809 goto done;
3811 if (do_show)
3812 error = show_current_branch(repo, worktree);
3813 else if (do_list)
3814 error = list_branches(repo, worktree);
3815 else if (delref)
3816 error = delete_branch(repo, worktree, delref);
3817 else {
3818 if (commit_id_arg == NULL)
3819 commit_id_arg = worktree ?
3820 got_worktree_get_head_ref_name(worktree) :
3821 GOT_REF_HEAD;
3822 error = got_repo_match_object_id(&commit_id, NULL,
3823 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3824 if (error)
3825 goto done;
3826 error = add_branch(repo, argv[0], commit_id);
3827 if (error)
3828 goto done;
3829 if (worktree && do_update) {
3830 int did_something = 0;
3831 char *branch_refname = NULL;
3833 error = got_object_id_str(&commit_id_str, commit_id);
3834 if (error)
3835 goto done;
3836 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3837 worktree);
3838 if (error)
3839 goto done;
3840 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3841 == -1) {
3842 error = got_error_from_errno("asprintf");
3843 goto done;
3845 error = got_ref_open(&ref, repo, branch_refname, 0);
3846 free(branch_refname);
3847 if (error)
3848 goto done;
3849 error = switch_head_ref(ref, commit_id, worktree,
3850 repo);
3851 if (error)
3852 goto done;
3853 error = got_worktree_set_base_commit_id(worktree, repo,
3854 commit_id);
3855 if (error)
3856 goto done;
3857 error = got_worktree_checkout_files(worktree, &paths,
3858 repo, update_progress, &did_something,
3859 check_cancelled, NULL);
3860 if (error)
3861 goto done;
3862 if (did_something)
3863 printf("Updated to commit %s\n", commit_id_str);
3866 done:
3867 if (ref)
3868 got_ref_close(ref);
3869 if (repo)
3870 got_repo_close(repo);
3871 if (worktree)
3872 got_worktree_close(worktree);
3873 free(cwd);
3874 free(repo_path);
3875 free(commit_id);
3876 free(commit_id_str);
3877 TAILQ_FOREACH(pe, &paths, entry)
3878 free((char *)pe->path);
3879 got_pathlist_free(&paths);
3880 return error;
3884 __dead static void
3885 usage_tag(void)
3887 fprintf(stderr,
3888 "usage: %s tag [-c commit] [-r repository] [-l] "
3889 "[-m message] name\n", getprogname());
3890 exit(1);
3893 #if 0
3894 static const struct got_error *
3895 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3897 const struct got_error *err = NULL;
3898 struct got_reflist_entry *re, *se, *new;
3899 struct got_object_id *re_id, *se_id;
3900 struct got_tag_object *re_tag, *se_tag;
3901 time_t re_time, se_time;
3903 SIMPLEQ_FOREACH(re, tags, entry) {
3904 se = SIMPLEQ_FIRST(sorted);
3905 if (se == NULL) {
3906 err = got_reflist_entry_dup(&new, re);
3907 if (err)
3908 return err;
3909 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3910 continue;
3911 } else {
3912 err = got_ref_resolve(&re_id, repo, re->ref);
3913 if (err)
3914 break;
3915 err = got_object_open_as_tag(&re_tag, repo, re_id);
3916 free(re_id);
3917 if (err)
3918 break;
3919 re_time = got_object_tag_get_tagger_time(re_tag);
3920 got_object_tag_close(re_tag);
3923 while (se) {
3924 err = got_ref_resolve(&se_id, repo, re->ref);
3925 if (err)
3926 break;
3927 err = got_object_open_as_tag(&se_tag, repo, se_id);
3928 free(se_id);
3929 if (err)
3930 break;
3931 se_time = got_object_tag_get_tagger_time(se_tag);
3932 got_object_tag_close(se_tag);
3934 if (se_time > re_time) {
3935 err = got_reflist_entry_dup(&new, re);
3936 if (err)
3937 return err;
3938 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3939 break;
3941 se = SIMPLEQ_NEXT(se, entry);
3942 continue;
3945 done:
3946 return err;
3948 #endif
3950 static const struct got_error *
3951 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3953 static const struct got_error *err = NULL;
3954 struct got_reflist_head refs;
3955 struct got_reflist_entry *re;
3957 SIMPLEQ_INIT(&refs);
3959 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3960 if (err)
3961 return err;
3963 SIMPLEQ_FOREACH(re, &refs, entry) {
3964 const char *refname;
3965 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3966 char datebuf[26];
3967 const char *tagger;
3968 time_t tagger_time;
3969 struct got_object_id *id;
3970 struct got_tag_object *tag;
3971 struct got_commit_object *commit = NULL;
3973 refname = got_ref_get_name(re->ref);
3974 if (strncmp(refname, "refs/tags/", 10) != 0)
3975 continue;
3976 refname += 10;
3977 refstr = got_ref_to_str(re->ref);
3978 if (refstr == NULL) {
3979 err = got_error_from_errno("got_ref_to_str");
3980 break;
3982 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3983 free(refstr);
3985 err = got_ref_resolve(&id, repo, re->ref);
3986 if (err)
3987 break;
3988 err = got_object_open_as_tag(&tag, repo, id);
3989 if (err) {
3990 if (err->code != GOT_ERR_OBJ_TYPE) {
3991 free(id);
3992 break;
3994 /* "lightweight" tag */
3995 err = got_object_open_as_commit(&commit, repo, id);
3996 if (err) {
3997 free(id);
3998 break;
4000 tagger = got_object_commit_get_committer(commit);
4001 tagger_time =
4002 got_object_commit_get_committer_time(commit);
4003 err = got_object_id_str(&id_str, id);
4004 free(id);
4005 if (err)
4006 break;
4007 } else {
4008 free(id);
4009 tagger = got_object_tag_get_tagger(tag);
4010 tagger_time = got_object_tag_get_tagger_time(tag);
4011 err = got_object_id_str(&id_str,
4012 got_object_tag_get_object_id(tag));
4013 if (err)
4014 break;
4016 printf("from: %s\n", tagger);
4017 datestr = get_datestr(&tagger_time, datebuf);
4018 if (datestr)
4019 printf("date: %s UTC\n", datestr);
4020 if (commit)
4021 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4022 else {
4023 switch (got_object_tag_get_object_type(tag)) {
4024 case GOT_OBJ_TYPE_BLOB:
4025 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4026 id_str);
4027 break;
4028 case GOT_OBJ_TYPE_TREE:
4029 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4030 id_str);
4031 break;
4032 case GOT_OBJ_TYPE_COMMIT:
4033 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4034 id_str);
4035 break;
4036 case GOT_OBJ_TYPE_TAG:
4037 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4038 id_str);
4039 break;
4040 default:
4041 break;
4044 free(id_str);
4045 if (commit) {
4046 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4047 if (err)
4048 break;
4049 got_object_commit_close(commit);
4050 } else {
4051 tagmsg0 = strdup(got_object_tag_get_message(tag));
4052 got_object_tag_close(tag);
4053 if (tagmsg0 == NULL) {
4054 err = got_error_from_errno("strdup");
4055 break;
4059 tagmsg = tagmsg0;
4060 do {
4061 line = strsep(&tagmsg, "\n");
4062 if (line)
4063 printf(" %s\n", line);
4064 } while (line);
4065 free(tagmsg0);
4068 got_ref_list_free(&refs);
4069 return NULL;
4072 static const struct got_error *
4073 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4074 const char *tag_name, const char *repo_path)
4076 const struct got_error *err = NULL;
4077 char *template = NULL, *initial_content = NULL;
4078 char *editor = NULL;
4079 int fd = -1;
4081 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4082 err = got_error_from_errno("asprintf");
4083 goto done;
4086 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4087 commit_id_str, tag_name) == -1) {
4088 err = got_error_from_errno("asprintf");
4089 goto done;
4092 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4093 if (err)
4094 goto done;
4096 dprintf(fd, initial_content);
4097 close(fd);
4099 err = get_editor(&editor);
4100 if (err)
4101 goto done;
4102 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4103 done:
4104 free(initial_content);
4105 free(template);
4106 free(editor);
4108 /* Editor is done; we can now apply unveil(2) */
4109 if (err == NULL) {
4110 err = apply_unveil(repo_path, 0, NULL);
4111 if (err) {
4112 free(*tagmsg);
4113 *tagmsg = NULL;
4116 return err;
4119 static const struct got_error *
4120 add_tag(struct got_repository *repo, const char *tag_name,
4121 const char *commit_arg, const char *tagmsg_arg)
4123 const struct got_error *err = NULL;
4124 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4125 char *label = NULL, *commit_id_str = NULL;
4126 struct got_reference *ref = NULL;
4127 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4128 char *tagmsg_path = NULL, *tag_id_str = NULL;
4129 int preserve_tagmsg = 0;
4132 * Don't let the user create a tag name with a leading '-'.
4133 * While technically a valid reference name, this case is usually
4134 * an unintended typo.
4136 if (tag_name[0] == '-')
4137 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4139 err = get_author(&tagger, repo);
4140 if (err)
4141 return err;
4143 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4144 GOT_OBJ_TYPE_COMMIT, 1, repo);
4145 if (err)
4146 goto done;
4148 err = got_object_id_str(&commit_id_str, commit_id);
4149 if (err)
4150 goto done;
4152 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4153 refname = strdup(tag_name);
4154 if (refname == NULL) {
4155 err = got_error_from_errno("strdup");
4156 goto done;
4158 tag_name += 10;
4159 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4160 err = got_error_from_errno("asprintf");
4161 goto done;
4164 err = got_ref_open(&ref, repo, refname, 0);
4165 if (err == NULL) {
4166 err = got_error(GOT_ERR_TAG_EXISTS);
4167 goto done;
4168 } else if (err->code != GOT_ERR_NOT_REF)
4169 goto done;
4171 if (tagmsg_arg == NULL) {
4172 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4173 tag_name, got_repo_get_path(repo));
4174 if (err) {
4175 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4176 tagmsg_path != NULL)
4177 preserve_tagmsg = 1;
4178 goto done;
4182 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4183 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4184 if (err) {
4185 if (tagmsg_path)
4186 preserve_tagmsg = 1;
4187 goto done;
4190 err = got_ref_alloc(&ref, refname, tag_id);
4191 if (err) {
4192 if (tagmsg_path)
4193 preserve_tagmsg = 1;
4194 goto done;
4197 err = got_ref_write(ref, repo);
4198 if (err) {
4199 if (tagmsg_path)
4200 preserve_tagmsg = 1;
4201 goto done;
4204 err = got_object_id_str(&tag_id_str, tag_id);
4205 if (err) {
4206 if (tagmsg_path)
4207 preserve_tagmsg = 1;
4208 goto done;
4210 printf("Created tag %s\n", tag_id_str);
4211 done:
4212 if (preserve_tagmsg) {
4213 fprintf(stderr, "%s: tag message preserved in %s\n",
4214 getprogname(), tagmsg_path);
4215 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4216 err = got_error_from_errno2("unlink", tagmsg_path);
4217 free(tag_id_str);
4218 if (ref)
4219 got_ref_close(ref);
4220 free(commit_id);
4221 free(commit_id_str);
4222 free(refname);
4223 free(tagmsg);
4224 free(tagmsg_path);
4225 free(tagger);
4226 return err;
4229 static const struct got_error *
4230 cmd_tag(int argc, char *argv[])
4232 const struct got_error *error = NULL;
4233 struct got_repository *repo = NULL;
4234 struct got_worktree *worktree = NULL;
4235 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4236 char *gitconfig_path = NULL;
4237 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4238 int ch, do_list = 0;
4240 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4241 switch (ch) {
4242 case 'c':
4243 commit_id_arg = optarg;
4244 break;
4245 case 'm':
4246 tagmsg = optarg;
4247 break;
4248 case 'r':
4249 repo_path = realpath(optarg, NULL);
4250 if (repo_path == NULL)
4251 return got_error_from_errno2("realpath",
4252 optarg);
4253 got_path_strip_trailing_slashes(repo_path);
4254 break;
4255 case 'l':
4256 do_list = 1;
4257 break;
4258 default:
4259 usage_tag();
4260 /* NOTREACHED */
4264 argc -= optind;
4265 argv += optind;
4267 if (do_list) {
4268 if (commit_id_arg != NULL)
4269 errx(1, "-c option can only be used when creating a tag");
4270 if (tagmsg)
4271 errx(1, "-l and -m options are mutually exclusive");
4272 if (argc > 0)
4273 usage_tag();
4274 } else if (argc != 1)
4275 usage_tag();
4277 tag_name = argv[0];
4279 #ifndef PROFILE
4280 if (do_list) {
4281 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4282 NULL) == -1)
4283 err(1, "pledge");
4284 } else {
4285 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4286 "sendfd unveil", NULL) == -1)
4287 err(1, "pledge");
4289 #endif
4290 cwd = getcwd(NULL, 0);
4291 if (cwd == NULL) {
4292 error = got_error_from_errno("getcwd");
4293 goto done;
4296 if (repo_path == NULL) {
4297 error = got_worktree_open(&worktree, cwd);
4298 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4299 goto done;
4300 else
4301 error = NULL;
4302 if (worktree) {
4303 repo_path =
4304 strdup(got_worktree_get_repo_path(worktree));
4305 if (repo_path == NULL)
4306 error = got_error_from_errno("strdup");
4307 if (error)
4308 goto done;
4309 } else {
4310 repo_path = strdup(cwd);
4311 if (repo_path == NULL) {
4312 error = got_error_from_errno("strdup");
4313 goto done;
4318 if (do_list) {
4319 error = got_repo_open(&repo, repo_path, NULL);
4320 if (error != NULL)
4321 goto done;
4322 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4323 if (error)
4324 goto done;
4325 error = list_tags(repo, worktree);
4326 } else {
4327 error = get_gitconfig_path(&gitconfig_path);
4328 if (error)
4329 goto done;
4330 error = got_repo_open(&repo, repo_path, gitconfig_path);
4331 if (error != NULL)
4332 goto done;
4334 if (tagmsg) {
4335 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4336 if (error)
4337 goto done;
4340 if (commit_id_arg == NULL) {
4341 struct got_reference *head_ref;
4342 struct got_object_id *commit_id;
4343 error = got_ref_open(&head_ref, repo,
4344 worktree ? got_worktree_get_head_ref_name(worktree)
4345 : GOT_REF_HEAD, 0);
4346 if (error)
4347 goto done;
4348 error = got_ref_resolve(&commit_id, repo, head_ref);
4349 got_ref_close(head_ref);
4350 if (error)
4351 goto done;
4352 error = got_object_id_str(&commit_id_str, commit_id);
4353 free(commit_id);
4354 if (error)
4355 goto done;
4358 error = add_tag(repo, tag_name,
4359 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4361 done:
4362 if (repo)
4363 got_repo_close(repo);
4364 if (worktree)
4365 got_worktree_close(worktree);
4366 free(cwd);
4367 free(repo_path);
4368 free(gitconfig_path);
4369 free(commit_id_str);
4370 return error;
4373 __dead static void
4374 usage_add(void)
4376 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4377 getprogname());
4378 exit(1);
4381 static const struct got_error *
4382 add_progress(void *arg, unsigned char status, const char *path)
4384 while (path[0] == '/')
4385 path++;
4386 printf("%c %s\n", status, path);
4387 return NULL;
4390 static const struct got_error *
4391 cmd_add(int argc, char *argv[])
4393 const struct got_error *error = NULL;
4394 struct got_repository *repo = NULL;
4395 struct got_worktree *worktree = NULL;
4396 char *cwd = NULL;
4397 struct got_pathlist_head paths;
4398 struct got_pathlist_entry *pe;
4399 int ch, can_recurse = 0, no_ignores = 0;
4401 TAILQ_INIT(&paths);
4403 while ((ch = getopt(argc, argv, "IR")) != -1) {
4404 switch (ch) {
4405 case 'I':
4406 no_ignores = 1;
4407 break;
4408 case 'R':
4409 can_recurse = 1;
4410 break;
4411 default:
4412 usage_add();
4413 /* NOTREACHED */
4417 argc -= optind;
4418 argv += optind;
4420 #ifndef PROFILE
4421 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4422 NULL) == -1)
4423 err(1, "pledge");
4424 #endif
4425 if (argc < 1)
4426 usage_add();
4428 cwd = getcwd(NULL, 0);
4429 if (cwd == NULL) {
4430 error = got_error_from_errno("getcwd");
4431 goto done;
4434 error = got_worktree_open(&worktree, cwd);
4435 if (error)
4436 goto done;
4438 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4439 NULL);
4440 if (error != NULL)
4441 goto done;
4443 error = apply_unveil(got_repo_get_path(repo), 1,
4444 got_worktree_get_root_path(worktree));
4445 if (error)
4446 goto done;
4448 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4449 if (error)
4450 goto done;
4452 if (!can_recurse && no_ignores) {
4453 error = got_error_msg(GOT_ERR_BAD_PATH,
4454 "disregarding ignores requires -R option");
4455 goto done;
4459 if (!can_recurse) {
4460 char *ondisk_path;
4461 struct stat sb;
4462 TAILQ_FOREACH(pe, &paths, entry) {
4463 if (asprintf(&ondisk_path, "%s/%s",
4464 got_worktree_get_root_path(worktree),
4465 pe->path) == -1) {
4466 error = got_error_from_errno("asprintf");
4467 goto done;
4469 if (lstat(ondisk_path, &sb) == -1) {
4470 if (errno == ENOENT) {
4471 free(ondisk_path);
4472 continue;
4474 error = got_error_from_errno2("lstat",
4475 ondisk_path);
4476 free(ondisk_path);
4477 goto done;
4479 free(ondisk_path);
4480 if (S_ISDIR(sb.st_mode)) {
4481 error = got_error_msg(GOT_ERR_BAD_PATH,
4482 "adding directories requires -R option");
4483 goto done;
4488 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4489 NULL, repo, no_ignores);
4490 done:
4491 if (repo)
4492 got_repo_close(repo);
4493 if (worktree)
4494 got_worktree_close(worktree);
4495 TAILQ_FOREACH(pe, &paths, entry)
4496 free((char *)pe->path);
4497 got_pathlist_free(&paths);
4498 free(cwd);
4499 return error;
4502 __dead static void
4503 usage_remove(void)
4505 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4506 getprogname());
4507 exit(1);
4510 static const struct got_error *
4511 print_remove_status(void *arg, unsigned char status,
4512 unsigned char staged_status, const char *path)
4514 while (path[0] == '/')
4515 path++;
4516 if (status == GOT_STATUS_NONEXISTENT)
4517 return NULL;
4518 if (status == staged_status && (status == GOT_STATUS_DELETE))
4519 status = GOT_STATUS_NO_CHANGE;
4520 printf("%c%c %s\n", status, staged_status, path);
4521 return NULL;
4524 static const struct got_error *
4525 cmd_remove(int argc, char *argv[])
4527 const struct got_error *error = NULL;
4528 struct got_worktree *worktree = NULL;
4529 struct got_repository *repo = NULL;
4530 char *cwd = NULL;
4531 struct got_pathlist_head paths;
4532 struct got_pathlist_entry *pe;
4533 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4535 TAILQ_INIT(&paths);
4537 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4538 switch (ch) {
4539 case 'f':
4540 delete_local_mods = 1;
4541 break;
4542 case 'k':
4543 keep_on_disk = 1;
4544 break;
4545 case 'R':
4546 can_recurse = 1;
4547 break;
4548 default:
4549 usage_remove();
4550 /* NOTREACHED */
4554 argc -= optind;
4555 argv += optind;
4557 #ifndef PROFILE
4558 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4559 NULL) == -1)
4560 err(1, "pledge");
4561 #endif
4562 if (argc < 1)
4563 usage_remove();
4565 cwd = getcwd(NULL, 0);
4566 if (cwd == NULL) {
4567 error = got_error_from_errno("getcwd");
4568 goto done;
4570 error = got_worktree_open(&worktree, cwd);
4571 if (error)
4572 goto done;
4574 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4575 NULL);
4576 if (error)
4577 goto done;
4579 error = apply_unveil(got_repo_get_path(repo), 1,
4580 got_worktree_get_root_path(worktree));
4581 if (error)
4582 goto done;
4584 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4585 if (error)
4586 goto done;
4588 if (!can_recurse) {
4589 char *ondisk_path;
4590 struct stat sb;
4591 TAILQ_FOREACH(pe, &paths, entry) {
4592 if (asprintf(&ondisk_path, "%s/%s",
4593 got_worktree_get_root_path(worktree),
4594 pe->path) == -1) {
4595 error = got_error_from_errno("asprintf");
4596 goto done;
4598 if (lstat(ondisk_path, &sb) == -1) {
4599 if (errno == ENOENT) {
4600 free(ondisk_path);
4601 continue;
4603 error = got_error_from_errno2("lstat",
4604 ondisk_path);
4605 free(ondisk_path);
4606 goto done;
4608 free(ondisk_path);
4609 if (S_ISDIR(sb.st_mode)) {
4610 error = got_error_msg(GOT_ERR_BAD_PATH,
4611 "removing directories requires -R option");
4612 goto done;
4617 error = got_worktree_schedule_delete(worktree, &paths,
4618 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4619 done:
4620 if (repo)
4621 got_repo_close(repo);
4622 if (worktree)
4623 got_worktree_close(worktree);
4624 TAILQ_FOREACH(pe, &paths, entry)
4625 free((char *)pe->path);
4626 got_pathlist_free(&paths);
4627 free(cwd);
4628 return error;
4631 __dead static void
4632 usage_revert(void)
4634 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4635 "path ...\n", getprogname());
4636 exit(1);
4639 static const struct got_error *
4640 revert_progress(void *arg, unsigned char status, const char *path)
4642 if (status == GOT_STATUS_UNVERSIONED)
4643 return NULL;
4645 while (path[0] == '/')
4646 path++;
4647 printf("%c %s\n", status, path);
4648 return NULL;
4651 struct choose_patch_arg {
4652 FILE *patch_script_file;
4653 const char *action;
4656 static const struct got_error *
4657 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4658 int nchanges, const char *action)
4660 char *line = NULL;
4661 size_t linesize = 0;
4662 ssize_t linelen;
4664 switch (status) {
4665 case GOT_STATUS_ADD:
4666 printf("A %s\n%s this addition? [y/n] ", path, action);
4667 break;
4668 case GOT_STATUS_DELETE:
4669 printf("D %s\n%s this deletion? [y/n] ", path, action);
4670 break;
4671 case GOT_STATUS_MODIFY:
4672 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4673 return got_error_from_errno("fseek");
4674 printf(GOT_COMMIT_SEP_STR);
4675 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4676 printf("%s", line);
4677 if (ferror(patch_file))
4678 return got_error_from_errno("getline");
4679 printf(GOT_COMMIT_SEP_STR);
4680 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4681 path, n, nchanges, action);
4682 break;
4683 default:
4684 return got_error_path(path, GOT_ERR_FILE_STATUS);
4687 return NULL;
4690 static const struct got_error *
4691 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4692 FILE *patch_file, int n, int nchanges)
4694 const struct got_error *err = NULL;
4695 char *line = NULL;
4696 size_t linesize = 0;
4697 ssize_t linelen;
4698 int resp = ' ';
4699 struct choose_patch_arg *a = arg;
4701 *choice = GOT_PATCH_CHOICE_NONE;
4703 if (a->patch_script_file) {
4704 char *nl;
4705 err = show_change(status, path, patch_file, n, nchanges,
4706 a->action);
4707 if (err)
4708 return err;
4709 linelen = getline(&line, &linesize, a->patch_script_file);
4710 if (linelen == -1) {
4711 if (ferror(a->patch_script_file))
4712 return got_error_from_errno("getline");
4713 return NULL;
4715 nl = strchr(line, '\n');
4716 if (nl)
4717 *nl = '\0';
4718 if (strcmp(line, "y") == 0) {
4719 *choice = GOT_PATCH_CHOICE_YES;
4720 printf("y\n");
4721 } else if (strcmp(line, "n") == 0) {
4722 *choice = GOT_PATCH_CHOICE_NO;
4723 printf("n\n");
4724 } else if (strcmp(line, "q") == 0 &&
4725 status == GOT_STATUS_MODIFY) {
4726 *choice = GOT_PATCH_CHOICE_QUIT;
4727 printf("q\n");
4728 } else
4729 printf("invalid response '%s'\n", line);
4730 free(line);
4731 return NULL;
4734 while (resp != 'y' && resp != 'n' && resp != 'q') {
4735 err = show_change(status, path, patch_file, n, nchanges,
4736 a->action);
4737 if (err)
4738 return err;
4739 resp = getchar();
4740 if (resp == '\n')
4741 resp = getchar();
4742 if (status == GOT_STATUS_MODIFY) {
4743 if (resp != 'y' && resp != 'n' && resp != 'q') {
4744 printf("invalid response '%c'\n", resp);
4745 resp = ' ';
4747 } else if (resp != 'y' && resp != 'n') {
4748 printf("invalid response '%c'\n", resp);
4749 resp = ' ';
4753 if (resp == 'y')
4754 *choice = GOT_PATCH_CHOICE_YES;
4755 else if (resp == 'n')
4756 *choice = GOT_PATCH_CHOICE_NO;
4757 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4758 *choice = GOT_PATCH_CHOICE_QUIT;
4760 return NULL;
4764 static const struct got_error *
4765 cmd_revert(int argc, char *argv[])
4767 const struct got_error *error = NULL;
4768 struct got_worktree *worktree = NULL;
4769 struct got_repository *repo = NULL;
4770 char *cwd = NULL, *path = NULL;
4771 struct got_pathlist_head paths;
4772 struct got_pathlist_entry *pe;
4773 int ch, can_recurse = 0, pflag = 0;
4774 FILE *patch_script_file = NULL;
4775 const char *patch_script_path = NULL;
4776 struct choose_patch_arg cpa;
4778 TAILQ_INIT(&paths);
4780 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4781 switch (ch) {
4782 case 'p':
4783 pflag = 1;
4784 break;
4785 case 'F':
4786 patch_script_path = optarg;
4787 break;
4788 case 'R':
4789 can_recurse = 1;
4790 break;
4791 default:
4792 usage_revert();
4793 /* NOTREACHED */
4797 argc -= optind;
4798 argv += optind;
4800 #ifndef PROFILE
4801 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4802 "unveil", NULL) == -1)
4803 err(1, "pledge");
4804 #endif
4805 if (argc < 1)
4806 usage_revert();
4807 if (patch_script_path && !pflag)
4808 errx(1, "-F option can only be used together with -p option");
4810 cwd = getcwd(NULL, 0);
4811 if (cwd == NULL) {
4812 error = got_error_from_errno("getcwd");
4813 goto done;
4815 error = got_worktree_open(&worktree, cwd);
4816 if (error)
4817 goto done;
4819 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4820 NULL);
4821 if (error != NULL)
4822 goto done;
4824 if (patch_script_path) {
4825 patch_script_file = fopen(patch_script_path, "r");
4826 if (patch_script_file == NULL) {
4827 error = got_error_from_errno2("fopen",
4828 patch_script_path);
4829 goto done;
4832 error = apply_unveil(got_repo_get_path(repo), 1,
4833 got_worktree_get_root_path(worktree));
4834 if (error)
4835 goto done;
4837 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4838 if (error)
4839 goto done;
4841 if (!can_recurse) {
4842 char *ondisk_path;
4843 struct stat sb;
4844 TAILQ_FOREACH(pe, &paths, entry) {
4845 if (asprintf(&ondisk_path, "%s/%s",
4846 got_worktree_get_root_path(worktree),
4847 pe->path) == -1) {
4848 error = got_error_from_errno("asprintf");
4849 goto done;
4851 if (lstat(ondisk_path, &sb) == -1) {
4852 if (errno == ENOENT) {
4853 free(ondisk_path);
4854 continue;
4856 error = got_error_from_errno2("lstat",
4857 ondisk_path);
4858 free(ondisk_path);
4859 goto done;
4861 free(ondisk_path);
4862 if (S_ISDIR(sb.st_mode)) {
4863 error = got_error_msg(GOT_ERR_BAD_PATH,
4864 "reverting directories requires -R option");
4865 goto done;
4870 cpa.patch_script_file = patch_script_file;
4871 cpa.action = "revert";
4872 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4873 pflag ? choose_patch : NULL, &cpa, repo);
4874 done:
4875 if (patch_script_file && fclose(patch_script_file) == EOF &&
4876 error == NULL)
4877 error = got_error_from_errno2("fclose", patch_script_path);
4878 if (repo)
4879 got_repo_close(repo);
4880 if (worktree)
4881 got_worktree_close(worktree);
4882 free(path);
4883 free(cwd);
4884 return error;
4887 __dead static void
4888 usage_commit(void)
4890 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4891 getprogname());
4892 exit(1);
4895 struct collect_commit_logmsg_arg {
4896 const char *cmdline_log;
4897 const char *editor;
4898 const char *worktree_path;
4899 const char *branch_name;
4900 const char *repo_path;
4901 char *logmsg_path;
4905 static const struct got_error *
4906 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4907 void *arg)
4909 char *initial_content = NULL;
4910 struct got_pathlist_entry *pe;
4911 const struct got_error *err = NULL;
4912 char *template = NULL;
4913 struct collect_commit_logmsg_arg *a = arg;
4914 int fd;
4915 size_t len;
4917 /* if a message was specified on the command line, just use it */
4918 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4919 len = strlen(a->cmdline_log) + 1;
4920 *logmsg = malloc(len + 1);
4921 if (*logmsg == NULL)
4922 return got_error_from_errno("malloc");
4923 strlcpy(*logmsg, a->cmdline_log, len);
4924 return NULL;
4927 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4928 return got_error_from_errno("asprintf");
4930 if (asprintf(&initial_content,
4931 "\n# changes to be committed on branch %s:\n",
4932 a->branch_name) == -1)
4933 return got_error_from_errno("asprintf");
4935 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4936 if (err)
4937 goto done;
4939 dprintf(fd, initial_content);
4941 TAILQ_FOREACH(pe, commitable_paths, entry) {
4942 struct got_commitable *ct = pe->data;
4943 dprintf(fd, "# %c %s\n",
4944 got_commitable_get_status(ct),
4945 got_commitable_get_path(ct));
4947 close(fd);
4949 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4950 done:
4951 free(initial_content);
4952 free(template);
4954 /* Editor is done; we can now apply unveil(2) */
4955 if (err == NULL) {
4956 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4957 if (err) {
4958 free(*logmsg);
4959 *logmsg = NULL;
4962 return err;
4965 static const struct got_error *
4966 cmd_commit(int argc, char *argv[])
4968 const struct got_error *error = NULL;
4969 struct got_worktree *worktree = NULL;
4970 struct got_repository *repo = NULL;
4971 char *cwd = NULL, *id_str = NULL;
4972 struct got_object_id *id = NULL;
4973 const char *logmsg = NULL;
4974 struct collect_commit_logmsg_arg cl_arg;
4975 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4976 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4977 struct got_pathlist_head paths;
4979 TAILQ_INIT(&paths);
4980 cl_arg.logmsg_path = NULL;
4982 while ((ch = getopt(argc, argv, "m:")) != -1) {
4983 switch (ch) {
4984 case 'm':
4985 logmsg = optarg;
4986 break;
4987 default:
4988 usage_commit();
4989 /* NOTREACHED */
4993 argc -= optind;
4994 argv += optind;
4996 #ifndef PROFILE
4997 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4998 "unveil", NULL) == -1)
4999 err(1, "pledge");
5000 #endif
5001 cwd = getcwd(NULL, 0);
5002 if (cwd == NULL) {
5003 error = got_error_from_errno("getcwd");
5004 goto done;
5006 error = got_worktree_open(&worktree, cwd);
5007 if (error)
5008 goto done;
5010 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5011 if (error)
5012 goto done;
5013 if (rebase_in_progress) {
5014 error = got_error(GOT_ERR_REBASING);
5015 goto done;
5018 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5019 worktree);
5020 if (error)
5021 goto done;
5023 error = get_gitconfig_path(&gitconfig_path);
5024 if (error)
5025 goto done;
5026 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5027 gitconfig_path);
5028 if (error != NULL)
5029 goto done;
5031 error = get_author(&author, repo);
5032 if (error)
5033 return error;
5036 * unveil(2) traverses exec(2); if an editor is used we have
5037 * to apply unveil after the log message has been written.
5039 if (logmsg == NULL || strlen(logmsg) == 0)
5040 error = get_editor(&editor);
5041 else
5042 error = apply_unveil(got_repo_get_path(repo), 0,
5043 got_worktree_get_root_path(worktree));
5044 if (error)
5045 goto done;
5047 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5048 if (error)
5049 goto done;
5051 cl_arg.editor = editor;
5052 cl_arg.cmdline_log = logmsg;
5053 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5054 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5055 if (!histedit_in_progress) {
5056 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5057 error = got_error(GOT_ERR_COMMIT_BRANCH);
5058 goto done;
5060 cl_arg.branch_name += 11;
5062 cl_arg.repo_path = got_repo_get_path(repo);
5063 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5064 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5065 if (error) {
5066 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5067 cl_arg.logmsg_path != NULL)
5068 preserve_logmsg = 1;
5069 goto done;
5072 error = got_object_id_str(&id_str, id);
5073 if (error)
5074 goto done;
5075 printf("Created commit %s\n", id_str);
5076 done:
5077 if (preserve_logmsg) {
5078 fprintf(stderr, "%s: log message preserved in %s\n",
5079 getprogname(), cl_arg.logmsg_path);
5080 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5081 error == NULL)
5082 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5083 free(cl_arg.logmsg_path);
5084 if (repo)
5085 got_repo_close(repo);
5086 if (worktree)
5087 got_worktree_close(worktree);
5088 free(cwd);
5089 free(id_str);
5090 free(gitconfig_path);
5091 free(editor);
5092 free(author);
5093 return error;
5096 __dead static void
5097 usage_cherrypick(void)
5099 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5100 exit(1);
5103 static const struct got_error *
5104 cmd_cherrypick(int argc, char *argv[])
5106 const struct got_error *error = NULL;
5107 struct got_worktree *worktree = NULL;
5108 struct got_repository *repo = NULL;
5109 char *cwd = NULL, *commit_id_str = NULL;
5110 struct got_object_id *commit_id = NULL;
5111 struct got_commit_object *commit = NULL;
5112 struct got_object_qid *pid;
5113 struct got_reference *head_ref = NULL;
5114 int ch, did_something = 0;
5116 while ((ch = getopt(argc, argv, "")) != -1) {
5117 switch (ch) {
5118 default:
5119 usage_cherrypick();
5120 /* NOTREACHED */
5124 argc -= optind;
5125 argv += optind;
5127 #ifndef PROFILE
5128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5129 "unveil", NULL) == -1)
5130 err(1, "pledge");
5131 #endif
5132 if (argc != 1)
5133 usage_cherrypick();
5135 cwd = getcwd(NULL, 0);
5136 if (cwd == NULL) {
5137 error = got_error_from_errno("getcwd");
5138 goto done;
5140 error = got_worktree_open(&worktree, cwd);
5141 if (error)
5142 goto done;
5144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5145 NULL);
5146 if (error != NULL)
5147 goto done;
5149 error = apply_unveil(got_repo_get_path(repo), 0,
5150 got_worktree_get_root_path(worktree));
5151 if (error)
5152 goto done;
5154 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5155 GOT_OBJ_TYPE_COMMIT, repo);
5156 if (error != NULL) {
5157 struct got_reference *ref;
5158 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5159 goto done;
5160 error = got_ref_open(&ref, repo, argv[0], 0);
5161 if (error != NULL)
5162 goto done;
5163 error = got_ref_resolve(&commit_id, repo, ref);
5164 got_ref_close(ref);
5165 if (error != NULL)
5166 goto done;
5168 error = got_object_id_str(&commit_id_str, commit_id);
5169 if (error)
5170 goto done;
5172 error = got_ref_open(&head_ref, repo,
5173 got_worktree_get_head_ref_name(worktree), 0);
5174 if (error != NULL)
5175 goto done;
5177 error = check_same_branch(commit_id, head_ref, NULL, repo);
5178 if (error) {
5179 if (error->code != GOT_ERR_ANCESTRY)
5180 goto done;
5181 error = NULL;
5182 } else {
5183 error = got_error(GOT_ERR_SAME_BRANCH);
5184 goto done;
5187 error = got_object_open_as_commit(&commit, repo, commit_id);
5188 if (error)
5189 goto done;
5190 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5191 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5192 commit_id, repo, update_progress, &did_something, check_cancelled,
5193 NULL);
5194 if (error != NULL)
5195 goto done;
5197 if (did_something)
5198 printf("Merged commit %s\n", commit_id_str);
5199 done:
5200 if (commit)
5201 got_object_commit_close(commit);
5202 free(commit_id_str);
5203 if (head_ref)
5204 got_ref_close(head_ref);
5205 if (worktree)
5206 got_worktree_close(worktree);
5207 if (repo)
5208 got_repo_close(repo);
5209 return error;
5212 __dead static void
5213 usage_backout(void)
5215 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5216 exit(1);
5219 static const struct got_error *
5220 cmd_backout(int argc, char *argv[])
5222 const struct got_error *error = NULL;
5223 struct got_worktree *worktree = NULL;
5224 struct got_repository *repo = NULL;
5225 char *cwd = NULL, *commit_id_str = NULL;
5226 struct got_object_id *commit_id = NULL;
5227 struct got_commit_object *commit = NULL;
5228 struct got_object_qid *pid;
5229 struct got_reference *head_ref = NULL;
5230 int ch, did_something = 0;
5232 while ((ch = getopt(argc, argv, "")) != -1) {
5233 switch (ch) {
5234 default:
5235 usage_backout();
5236 /* NOTREACHED */
5240 argc -= optind;
5241 argv += optind;
5243 #ifndef PROFILE
5244 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5245 "unveil", NULL) == -1)
5246 err(1, "pledge");
5247 #endif
5248 if (argc != 1)
5249 usage_backout();
5251 cwd = getcwd(NULL, 0);
5252 if (cwd == NULL) {
5253 error = got_error_from_errno("getcwd");
5254 goto done;
5256 error = got_worktree_open(&worktree, cwd);
5257 if (error)
5258 goto done;
5260 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5261 NULL);
5262 if (error != NULL)
5263 goto done;
5265 error = apply_unveil(got_repo_get_path(repo), 0,
5266 got_worktree_get_root_path(worktree));
5267 if (error)
5268 goto done;
5270 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5271 GOT_OBJ_TYPE_COMMIT, repo);
5272 if (error != NULL) {
5273 struct got_reference *ref;
5274 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5275 goto done;
5276 error = got_ref_open(&ref, repo, argv[0], 0);
5277 if (error != NULL)
5278 goto done;
5279 error = got_ref_resolve(&commit_id, repo, ref);
5280 got_ref_close(ref);
5281 if (error != NULL)
5282 goto done;
5284 error = got_object_id_str(&commit_id_str, commit_id);
5285 if (error)
5286 goto done;
5288 error = got_ref_open(&head_ref, repo,
5289 got_worktree_get_head_ref_name(worktree), 0);
5290 if (error != NULL)
5291 goto done;
5293 error = check_same_branch(commit_id, head_ref, NULL, repo);
5294 if (error)
5295 goto done;
5297 error = got_object_open_as_commit(&commit, repo, commit_id);
5298 if (error)
5299 goto done;
5300 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5301 if (pid == NULL) {
5302 error = got_error(GOT_ERR_ROOT_COMMIT);
5303 goto done;
5306 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5307 update_progress, &did_something, check_cancelled, NULL);
5308 if (error != NULL)
5309 goto done;
5311 if (did_something)
5312 printf("Backed out commit %s\n", commit_id_str);
5313 done:
5314 if (commit)
5315 got_object_commit_close(commit);
5316 free(commit_id_str);
5317 if (head_ref)
5318 got_ref_close(head_ref);
5319 if (worktree)
5320 got_worktree_close(worktree);
5321 if (repo)
5322 got_repo_close(repo);
5323 return error;
5326 __dead static void
5327 usage_rebase(void)
5329 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5330 getprogname());
5331 exit(1);
5334 void
5335 trim_logmsg(char *logmsg, int limit)
5337 char *nl;
5338 size_t len;
5340 len = strlen(logmsg);
5341 if (len > limit)
5342 len = limit;
5343 logmsg[len] = '\0';
5344 nl = strchr(logmsg, '\n');
5345 if (nl)
5346 *nl = '\0';
5349 static const struct got_error *
5350 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5352 const struct got_error *err;
5353 char *logmsg0 = NULL;
5354 const char *s;
5356 err = got_object_commit_get_logmsg(&logmsg0, commit);
5357 if (err)
5358 return err;
5360 s = logmsg0;
5361 while (isspace((unsigned char)s[0]))
5362 s++;
5364 *logmsg = strdup(s);
5365 if (*logmsg == NULL) {
5366 err = got_error_from_errno("strdup");
5367 goto done;
5370 trim_logmsg(*logmsg, limit);
5371 done:
5372 free(logmsg0);
5373 return err;
5376 static const struct got_error *
5377 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5379 const struct got_error *err;
5380 struct got_commit_object *commit = NULL;
5381 char *id_str = NULL, *logmsg = NULL;
5383 err = got_object_open_as_commit(&commit, repo, id);
5384 if (err)
5385 return err;
5387 err = got_object_id_str(&id_str, id);
5388 if (err)
5389 goto done;
5391 id_str[12] = '\0';
5393 err = get_short_logmsg(&logmsg, 42, commit);
5394 if (err)
5395 goto done;
5397 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5398 done:
5399 free(id_str);
5400 got_object_commit_close(commit);
5401 free(logmsg);
5402 return err;
5405 static const struct got_error *
5406 show_rebase_progress(struct got_commit_object *commit,
5407 struct got_object_id *old_id, struct got_object_id *new_id)
5409 const struct got_error *err;
5410 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5412 err = got_object_id_str(&old_id_str, old_id);
5413 if (err)
5414 goto done;
5416 if (new_id) {
5417 err = got_object_id_str(&new_id_str, new_id);
5418 if (err)
5419 goto done;
5422 old_id_str[12] = '\0';
5423 if (new_id_str)
5424 new_id_str[12] = '\0';
5426 err = get_short_logmsg(&logmsg, 42, commit);
5427 if (err)
5428 goto done;
5430 printf("%s -> %s: %s\n", old_id_str,
5431 new_id_str ? new_id_str : "no-op change", logmsg);
5432 done:
5433 free(old_id_str);
5434 free(new_id_str);
5435 free(logmsg);
5436 return err;
5439 static const struct got_error *
5440 rebase_progress(void *arg, unsigned char status, const char *path)
5442 unsigned char *rebase_status = arg;
5444 while (path[0] == '/')
5445 path++;
5446 printf("%c %s\n", status, path);
5448 if (*rebase_status == GOT_STATUS_CONFLICT)
5449 return NULL;
5450 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5451 *rebase_status = status;
5452 return NULL;
5455 static const struct got_error *
5456 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5457 struct got_reference *branch, struct got_reference *new_base_branch,
5458 struct got_reference *tmp_branch, struct got_repository *repo)
5460 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5461 return got_worktree_rebase_complete(worktree, fileindex,
5462 new_base_branch, tmp_branch, branch, repo);
5465 static const struct got_error *
5466 rebase_commit(struct got_pathlist_head *merged_paths,
5467 struct got_worktree *worktree, struct got_fileindex *fileindex,
5468 struct got_reference *tmp_branch,
5469 struct got_object_id *commit_id, struct got_repository *repo)
5471 const struct got_error *error;
5472 struct got_commit_object *commit;
5473 struct got_object_id *new_commit_id;
5475 error = got_object_open_as_commit(&commit, repo, commit_id);
5476 if (error)
5477 return error;
5479 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5480 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5481 if (error) {
5482 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5483 goto done;
5484 error = show_rebase_progress(commit, commit_id, NULL);
5485 } else {
5486 error = show_rebase_progress(commit, commit_id, new_commit_id);
5487 free(new_commit_id);
5489 done:
5490 got_object_commit_close(commit);
5491 return error;
5494 struct check_path_prefix_arg {
5495 const char *path_prefix;
5496 size_t len;
5497 int errcode;
5500 static const struct got_error *
5501 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5502 struct got_blob_object *blob2, struct got_object_id *id1,
5503 struct got_object_id *id2, const char *path1, const char *path2,
5504 mode_t mode1, mode_t mode2, struct got_repository *repo)
5506 struct check_path_prefix_arg *a = arg;
5508 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5509 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5510 return got_error(a->errcode);
5512 return NULL;
5515 static const struct got_error *
5516 check_path_prefix(struct got_object_id *parent_id,
5517 struct got_object_id *commit_id, const char *path_prefix,
5518 int errcode, struct got_repository *repo)
5520 const struct got_error *err;
5521 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5522 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5523 struct check_path_prefix_arg cpp_arg;
5525 if (got_path_is_root_dir(path_prefix))
5526 return NULL;
5528 err = got_object_open_as_commit(&commit, repo, commit_id);
5529 if (err)
5530 goto done;
5532 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5533 if (err)
5534 goto done;
5536 err = got_object_open_as_tree(&tree1, repo,
5537 got_object_commit_get_tree_id(parent_commit));
5538 if (err)
5539 goto done;
5541 err = got_object_open_as_tree(&tree2, repo,
5542 got_object_commit_get_tree_id(commit));
5543 if (err)
5544 goto done;
5546 cpp_arg.path_prefix = path_prefix;
5547 while (cpp_arg.path_prefix[0] == '/')
5548 cpp_arg.path_prefix++;
5549 cpp_arg.len = strlen(cpp_arg.path_prefix);
5550 cpp_arg.errcode = errcode;
5551 err = got_diff_tree(tree1, tree2, "", "", repo,
5552 check_path_prefix_in_diff, &cpp_arg, 0);
5553 done:
5554 if (tree1)
5555 got_object_tree_close(tree1);
5556 if (tree2)
5557 got_object_tree_close(tree2);
5558 if (commit)
5559 got_object_commit_close(commit);
5560 if (parent_commit)
5561 got_object_commit_close(parent_commit);
5562 return err;
5565 static const struct got_error *
5566 collect_commits(struct got_object_id_queue *commits,
5567 struct got_object_id *initial_commit_id,
5568 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5569 const char *path_prefix, int path_prefix_errcode,
5570 struct got_repository *repo)
5572 const struct got_error *err = NULL;
5573 struct got_commit_graph *graph = NULL;
5574 struct got_object_id *parent_id = NULL;
5575 struct got_object_qid *qid;
5576 struct got_object_id *commit_id = initial_commit_id;
5578 err = got_commit_graph_open(&graph, "/", 1);
5579 if (err)
5580 return err;
5582 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5583 check_cancelled, NULL);
5584 if (err)
5585 goto done;
5586 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5587 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5588 check_cancelled, NULL);
5589 if (err) {
5590 if (err->code == GOT_ERR_ITER_COMPLETED) {
5591 err = got_error_msg(GOT_ERR_ANCESTRY,
5592 "ran out of commits to rebase before "
5593 "youngest common ancestor commit has "
5594 "been reached?!?");
5596 goto done;
5597 } else {
5598 err = check_path_prefix(parent_id, commit_id,
5599 path_prefix, path_prefix_errcode, repo);
5600 if (err)
5601 goto done;
5603 err = got_object_qid_alloc(&qid, commit_id);
5604 if (err)
5605 goto done;
5606 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5607 commit_id = parent_id;
5610 done:
5611 got_commit_graph_close(graph);
5612 return err;
5615 static const struct got_error *
5616 cmd_rebase(int argc, char *argv[])
5618 const struct got_error *error = NULL;
5619 struct got_worktree *worktree = NULL;
5620 struct got_repository *repo = NULL;
5621 struct got_fileindex *fileindex = NULL;
5622 char *cwd = NULL;
5623 struct got_reference *branch = NULL;
5624 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5625 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5626 struct got_object_id *resume_commit_id = NULL;
5627 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5628 struct got_commit_object *commit = NULL;
5629 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5630 int histedit_in_progress = 0;
5631 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5632 struct got_object_id_queue commits;
5633 struct got_pathlist_head merged_paths;
5634 const struct got_object_id_queue *parent_ids;
5635 struct got_object_qid *qid, *pid;
5637 SIMPLEQ_INIT(&commits);
5638 TAILQ_INIT(&merged_paths);
5640 while ((ch = getopt(argc, argv, "ac")) != -1) {
5641 switch (ch) {
5642 case 'a':
5643 abort_rebase = 1;
5644 break;
5645 case 'c':
5646 continue_rebase = 1;
5647 break;
5648 default:
5649 usage_rebase();
5650 /* NOTREACHED */
5654 argc -= optind;
5655 argv += optind;
5657 #ifndef PROFILE
5658 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5659 "unveil", NULL) == -1)
5660 err(1, "pledge");
5661 #endif
5662 if (abort_rebase && continue_rebase)
5663 usage_rebase();
5664 else if (abort_rebase || continue_rebase) {
5665 if (argc != 0)
5666 usage_rebase();
5667 } else if (argc != 1)
5668 usage_rebase();
5670 cwd = getcwd(NULL, 0);
5671 if (cwd == NULL) {
5672 error = got_error_from_errno("getcwd");
5673 goto done;
5675 error = got_worktree_open(&worktree, cwd);
5676 if (error)
5677 goto done;
5679 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5680 NULL);
5681 if (error != NULL)
5682 goto done;
5684 error = apply_unveil(got_repo_get_path(repo), 0,
5685 got_worktree_get_root_path(worktree));
5686 if (error)
5687 goto done;
5689 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5690 worktree);
5691 if (error)
5692 goto done;
5693 if (histedit_in_progress) {
5694 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5695 goto done;
5698 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5699 if (error)
5700 goto done;
5702 if (abort_rebase) {
5703 int did_something;
5704 if (!rebase_in_progress) {
5705 error = got_error(GOT_ERR_NOT_REBASING);
5706 goto done;
5708 error = got_worktree_rebase_continue(&resume_commit_id,
5709 &new_base_branch, &tmp_branch, &branch, &fileindex,
5710 worktree, repo);
5711 if (error)
5712 goto done;
5713 printf("Switching work tree to %s\n",
5714 got_ref_get_symref_target(new_base_branch));
5715 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5716 new_base_branch, update_progress, &did_something);
5717 if (error)
5718 goto done;
5719 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5720 goto done; /* nothing else to do */
5723 if (continue_rebase) {
5724 if (!rebase_in_progress) {
5725 error = got_error(GOT_ERR_NOT_REBASING);
5726 goto done;
5728 error = got_worktree_rebase_continue(&resume_commit_id,
5729 &new_base_branch, &tmp_branch, &branch, &fileindex,
5730 worktree, repo);
5731 if (error)
5732 goto done;
5734 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5735 resume_commit_id, repo);
5736 if (error)
5737 goto done;
5739 yca_id = got_object_id_dup(resume_commit_id);
5740 if (yca_id == NULL) {
5741 error = got_error_from_errno("got_object_id_dup");
5742 goto done;
5744 } else {
5745 error = got_ref_open(&branch, repo, argv[0], 0);
5746 if (error != NULL)
5747 goto done;
5750 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5751 if (error)
5752 goto done;
5754 if (!continue_rebase) {
5755 struct got_object_id *base_commit_id;
5757 base_commit_id = got_worktree_get_base_commit_id(worktree);
5758 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5759 base_commit_id, branch_head_commit_id, repo,
5760 check_cancelled, NULL);
5761 if (error)
5762 goto done;
5763 if (yca_id == NULL) {
5764 error = got_error_msg(GOT_ERR_ANCESTRY,
5765 "specified branch shares no common ancestry "
5766 "with work tree's branch");
5767 goto done;
5770 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5771 if (error) {
5772 if (error->code != GOT_ERR_ANCESTRY)
5773 goto done;
5774 error = NULL;
5775 } else {
5776 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5777 "specified branch resolves to a commit which "
5778 "is already contained in work tree's branch");
5779 goto done;
5781 error = got_worktree_rebase_prepare(&new_base_branch,
5782 &tmp_branch, &fileindex, worktree, branch, repo);
5783 if (error)
5784 goto done;
5787 commit_id = branch_head_commit_id;
5788 error = got_object_open_as_commit(&commit, repo, commit_id);
5789 if (error)
5790 goto done;
5792 parent_ids = got_object_commit_get_parent_ids(commit);
5793 pid = SIMPLEQ_FIRST(parent_ids);
5794 if (pid == NULL) {
5795 if (!continue_rebase) {
5796 int did_something;
5797 error = got_worktree_rebase_abort(worktree, fileindex,
5798 repo, new_base_branch, update_progress,
5799 &did_something);
5800 if (error)
5801 goto done;
5802 printf("Rebase of %s aborted\n",
5803 got_ref_get_name(branch));
5805 error = got_error(GOT_ERR_EMPTY_REBASE);
5806 goto done;
5808 error = collect_commits(&commits, commit_id, pid->id,
5809 yca_id, got_worktree_get_path_prefix(worktree),
5810 GOT_ERR_REBASE_PATH, repo);
5811 got_object_commit_close(commit);
5812 commit = NULL;
5813 if (error)
5814 goto done;
5816 if (SIMPLEQ_EMPTY(&commits)) {
5817 if (continue_rebase) {
5818 error = rebase_complete(worktree, fileindex,
5819 branch, new_base_branch, tmp_branch, repo);
5820 goto done;
5821 } else {
5822 /* Fast-forward the reference of the branch. */
5823 struct got_object_id *new_head_commit_id;
5824 char *id_str;
5825 error = got_ref_resolve(&new_head_commit_id, repo,
5826 new_base_branch);
5827 if (error)
5828 goto done;
5829 error = got_object_id_str(&id_str, new_head_commit_id);
5830 printf("Forwarding %s to commit %s\n",
5831 got_ref_get_name(branch), id_str);
5832 free(id_str);
5833 error = got_ref_change_ref(branch,
5834 new_head_commit_id);
5835 if (error)
5836 goto done;
5840 pid = NULL;
5841 SIMPLEQ_FOREACH(qid, &commits, entry) {
5842 commit_id = qid->id;
5843 parent_id = pid ? pid->id : yca_id;
5844 pid = qid;
5846 error = got_worktree_rebase_merge_files(&merged_paths,
5847 worktree, fileindex, parent_id, commit_id, repo,
5848 rebase_progress, &rebase_status, check_cancelled, NULL);
5849 if (error)
5850 goto done;
5852 if (rebase_status == GOT_STATUS_CONFLICT) {
5853 error = show_rebase_merge_conflict(qid->id, repo);
5854 if (error)
5855 goto done;
5856 got_worktree_rebase_pathlist_free(&merged_paths);
5857 break;
5860 error = rebase_commit(&merged_paths, worktree, fileindex,
5861 tmp_branch, commit_id, repo);
5862 got_worktree_rebase_pathlist_free(&merged_paths);
5863 if (error)
5864 goto done;
5867 if (rebase_status == GOT_STATUS_CONFLICT) {
5868 error = got_worktree_rebase_postpone(worktree, fileindex);
5869 if (error)
5870 goto done;
5871 error = got_error_msg(GOT_ERR_CONFLICTS,
5872 "conflicts must be resolved before rebasing can continue");
5873 } else
5874 error = rebase_complete(worktree, fileindex, branch,
5875 new_base_branch, tmp_branch, repo);
5876 done:
5877 got_object_id_queue_free(&commits);
5878 free(branch_head_commit_id);
5879 free(resume_commit_id);
5880 free(yca_id);
5881 if (commit)
5882 got_object_commit_close(commit);
5883 if (branch)
5884 got_ref_close(branch);
5885 if (new_base_branch)
5886 got_ref_close(new_base_branch);
5887 if (tmp_branch)
5888 got_ref_close(tmp_branch);
5889 if (worktree)
5890 got_worktree_close(worktree);
5891 if (repo)
5892 got_repo_close(repo);
5893 return error;
5896 __dead static void
5897 usage_histedit(void)
5899 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5900 getprogname());
5901 exit(1);
5904 #define GOT_HISTEDIT_PICK 'p'
5905 #define GOT_HISTEDIT_EDIT 'e'
5906 #define GOT_HISTEDIT_FOLD 'f'
5907 #define GOT_HISTEDIT_DROP 'd'
5908 #define GOT_HISTEDIT_MESG 'm'
5910 static struct got_histedit_cmd {
5911 unsigned char code;
5912 const char *name;
5913 const char *desc;
5914 } got_histedit_cmds[] = {
5915 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5916 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5917 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5918 "be used" },
5919 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5920 { GOT_HISTEDIT_MESG, "mesg",
5921 "single-line log message for commit above (open editor if empty)" },
5924 struct got_histedit_list_entry {
5925 TAILQ_ENTRY(got_histedit_list_entry) entry;
5926 struct got_object_id *commit_id;
5927 const struct got_histedit_cmd *cmd;
5928 char *logmsg;
5930 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5932 static const struct got_error *
5933 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5934 FILE *f, struct got_repository *repo)
5936 const struct got_error *err = NULL;
5937 char *logmsg = NULL, *id_str = NULL;
5938 struct got_commit_object *commit = NULL;
5939 int n;
5941 err = got_object_open_as_commit(&commit, repo, commit_id);
5942 if (err)
5943 goto done;
5945 err = get_short_logmsg(&logmsg, 34, commit);
5946 if (err)
5947 goto done;
5949 err = got_object_id_str(&id_str, commit_id);
5950 if (err)
5951 goto done;
5953 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5954 if (n < 0)
5955 err = got_ferror(f, GOT_ERR_IO);
5956 done:
5957 if (commit)
5958 got_object_commit_close(commit);
5959 free(id_str);
5960 free(logmsg);
5961 return err;
5964 static const struct got_error *
5965 histedit_write_commit_list(struct got_object_id_queue *commits,
5966 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5968 const struct got_error *err = NULL;
5969 struct got_object_qid *qid;
5971 if (SIMPLEQ_EMPTY(commits))
5972 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5974 SIMPLEQ_FOREACH(qid, commits, entry) {
5975 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5976 f, repo);
5977 if (err)
5978 break;
5979 if (edit_logmsg_only) {
5980 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5981 if (n < 0) {
5982 err = got_ferror(f, GOT_ERR_IO);
5983 break;
5988 return err;
5991 static const struct got_error *
5992 write_cmd_list(FILE *f, const char *branch_name,
5993 struct got_object_id_queue *commits)
5995 const struct got_error *err = NULL;
5996 int n, i;
5997 char *id_str;
5998 struct got_object_qid *qid;
6000 qid = SIMPLEQ_FIRST(commits);
6001 err = got_object_id_str(&id_str, qid->id);
6002 if (err)
6003 return err;
6005 n = fprintf(f,
6006 "# Editing the history of branch '%s' starting at\n"
6007 "# commit %s\n"
6008 "# Commits will be processed in order from top to "
6009 "bottom of this file.\n", branch_name, id_str);
6010 if (n < 0) {
6011 err = got_ferror(f, GOT_ERR_IO);
6012 goto done;
6015 n = fprintf(f, "# Available histedit commands:\n");
6016 if (n < 0) {
6017 err = got_ferror(f, GOT_ERR_IO);
6018 goto done;
6021 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6022 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6023 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6024 cmd->desc);
6025 if (n < 0) {
6026 err = got_ferror(f, GOT_ERR_IO);
6027 break;
6030 done:
6031 free(id_str);
6032 return err;
6035 static const struct got_error *
6036 histedit_syntax_error(int lineno)
6038 static char msg[42];
6039 int ret;
6041 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6042 lineno);
6043 if (ret == -1 || ret >= sizeof(msg))
6044 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6046 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6049 static const struct got_error *
6050 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6051 char *logmsg, struct got_repository *repo)
6053 const struct got_error *err;
6054 struct got_commit_object *folded_commit = NULL;
6055 char *id_str, *folded_logmsg = NULL;
6057 err = got_object_id_str(&id_str, hle->commit_id);
6058 if (err)
6059 return err;
6061 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6062 if (err)
6063 goto done;
6065 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6066 if (err)
6067 goto done;
6068 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6069 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6070 folded_logmsg) == -1) {
6071 err = got_error_from_errno("asprintf");
6073 done:
6074 if (folded_commit)
6075 got_object_commit_close(folded_commit);
6076 free(id_str);
6077 free(folded_logmsg);
6078 return err;
6081 static struct got_histedit_list_entry *
6082 get_folded_commits(struct got_histedit_list_entry *hle)
6084 struct got_histedit_list_entry *prev, *folded = NULL;
6086 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6087 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6088 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6089 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6090 folded = prev;
6091 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6094 return folded;
6097 static const struct got_error *
6098 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6099 struct got_repository *repo)
6101 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6102 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6103 const struct got_error *err = NULL;
6104 struct got_commit_object *commit = NULL;
6105 int fd;
6106 struct got_histedit_list_entry *folded = NULL;
6108 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6109 if (err)
6110 return err;
6112 folded = get_folded_commits(hle);
6113 if (folded) {
6114 while (folded != hle) {
6115 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6116 folded = TAILQ_NEXT(folded, entry);
6117 continue;
6119 err = append_folded_commit_msg(&new_msg, folded,
6120 logmsg, repo);
6121 if (err)
6122 goto done;
6123 free(logmsg);
6124 logmsg = new_msg;
6125 folded = TAILQ_NEXT(folded, entry);
6129 err = got_object_id_str(&id_str, hle->commit_id);
6130 if (err)
6131 goto done;
6132 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6133 if (err)
6134 goto done;
6135 if (asprintf(&new_msg,
6136 "%s\n# original log message of commit %s: %s",
6137 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6138 err = got_error_from_errno("asprintf");
6139 goto done;
6141 free(logmsg);
6142 logmsg = new_msg;
6144 err = got_object_id_str(&id_str, hle->commit_id);
6145 if (err)
6146 goto done;
6148 err = got_opentemp_named_fd(&logmsg_path, &fd,
6149 GOT_TMPDIR_STR "/got-logmsg");
6150 if (err)
6151 goto done;
6153 dprintf(fd, logmsg);
6154 close(fd);
6156 err = get_editor(&editor);
6157 if (err)
6158 goto done;
6160 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6161 if (err) {
6162 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6163 goto done;
6164 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6166 done:
6167 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6168 err = got_error_from_errno2("unlink", logmsg_path);
6169 free(logmsg_path);
6170 free(logmsg);
6171 free(orig_logmsg);
6172 free(editor);
6173 if (commit)
6174 got_object_commit_close(commit);
6175 return err;
6178 static const struct got_error *
6179 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6180 FILE *f, struct got_repository *repo)
6182 const struct got_error *err = NULL;
6183 char *line = NULL, *p, *end;
6184 size_t size;
6185 ssize_t len;
6186 int lineno = 0, i;
6187 const struct got_histedit_cmd *cmd;
6188 struct got_object_id *commit_id = NULL;
6189 struct got_histedit_list_entry *hle = NULL;
6191 for (;;) {
6192 len = getline(&line, &size, f);
6193 if (len == -1) {
6194 const struct got_error *getline_err;
6195 if (feof(f))
6196 break;
6197 getline_err = got_error_from_errno("getline");
6198 err = got_ferror(f, getline_err->code);
6199 break;
6201 lineno++;
6202 p = line;
6203 while (isspace((unsigned char)p[0]))
6204 p++;
6205 if (p[0] == '#' || p[0] == '\0') {
6206 free(line);
6207 line = NULL;
6208 continue;
6210 cmd = NULL;
6211 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6212 cmd = &got_histedit_cmds[i];
6213 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6214 isspace((unsigned char)p[strlen(cmd->name)])) {
6215 p += strlen(cmd->name);
6216 break;
6218 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6219 p++;
6220 break;
6223 if (i == nitems(got_histedit_cmds)) {
6224 err = histedit_syntax_error(lineno);
6225 break;
6227 while (isspace((unsigned char)p[0]))
6228 p++;
6229 if (cmd->code == GOT_HISTEDIT_MESG) {
6230 if (hle == NULL || hle->logmsg != NULL) {
6231 err = got_error(GOT_ERR_HISTEDIT_CMD);
6232 break;
6234 if (p[0] == '\0') {
6235 err = histedit_edit_logmsg(hle, repo);
6236 if (err)
6237 break;
6238 } else {
6239 hle->logmsg = strdup(p);
6240 if (hle->logmsg == NULL) {
6241 err = got_error_from_errno("strdup");
6242 break;
6245 free(line);
6246 line = NULL;
6247 continue;
6248 } else {
6249 end = p;
6250 while (end[0] && !isspace((unsigned char)end[0]))
6251 end++;
6252 *end = '\0';
6254 err = got_object_resolve_id_str(&commit_id, repo, p);
6255 if (err) {
6256 /* override error code */
6257 err = histedit_syntax_error(lineno);
6258 break;
6261 hle = malloc(sizeof(*hle));
6262 if (hle == NULL) {
6263 err = got_error_from_errno("malloc");
6264 break;
6266 hle->cmd = cmd;
6267 hle->commit_id = commit_id;
6268 hle->logmsg = NULL;
6269 commit_id = NULL;
6270 free(line);
6271 line = NULL;
6272 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6275 free(line);
6276 free(commit_id);
6277 return err;
6280 static const struct got_error *
6281 histedit_check_script(struct got_histedit_list *histedit_cmds,
6282 struct got_object_id_queue *commits, struct got_repository *repo)
6284 const struct got_error *err = NULL;
6285 struct got_object_qid *qid;
6286 struct got_histedit_list_entry *hle;
6287 static char msg[92];
6288 char *id_str;
6290 if (TAILQ_EMPTY(histedit_cmds))
6291 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6292 "histedit script contains no commands");
6293 if (SIMPLEQ_EMPTY(commits))
6294 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6296 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6297 struct got_histedit_list_entry *hle2;
6298 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6299 if (hle == hle2)
6300 continue;
6301 if (got_object_id_cmp(hle->commit_id,
6302 hle2->commit_id) != 0)
6303 continue;
6304 err = got_object_id_str(&id_str, hle->commit_id);
6305 if (err)
6306 return err;
6307 snprintf(msg, sizeof(msg), "commit %s is listed "
6308 "more than once in histedit script", id_str);
6309 free(id_str);
6310 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6314 SIMPLEQ_FOREACH(qid, commits, entry) {
6315 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6316 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6317 break;
6319 if (hle == NULL) {
6320 err = got_object_id_str(&id_str, qid->id);
6321 if (err)
6322 return err;
6323 snprintf(msg, sizeof(msg),
6324 "commit %s missing from histedit script", id_str);
6325 free(id_str);
6326 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6330 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6331 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6332 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6333 "last commit in histedit script cannot be folded");
6335 return NULL;
6338 static const struct got_error *
6339 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6340 const char *path, struct got_object_id_queue *commits,
6341 struct got_repository *repo)
6343 const struct got_error *err = NULL;
6344 char *editor;
6345 FILE *f = NULL;
6347 err = get_editor(&editor);
6348 if (err)
6349 return err;
6351 if (spawn_editor(editor, path) == -1) {
6352 err = got_error_from_errno("failed spawning editor");
6353 goto done;
6356 f = fopen(path, "r");
6357 if (f == NULL) {
6358 err = got_error_from_errno("fopen");
6359 goto done;
6361 err = histedit_parse_list(histedit_cmds, f, repo);
6362 if (err)
6363 goto done;
6365 err = histedit_check_script(histedit_cmds, commits, repo);
6366 done:
6367 if (f && fclose(f) != 0 && err == NULL)
6368 err = got_error_from_errno("fclose");
6369 free(editor);
6370 return err;
6373 static const struct got_error *
6374 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6375 struct got_object_id_queue *, const char *, const char *,
6376 struct got_repository *);
6378 static const struct got_error *
6379 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6380 struct got_object_id_queue *commits, const char *branch_name,
6381 int edit_logmsg_only, struct got_repository *repo)
6383 const struct got_error *err;
6384 FILE *f = NULL;
6385 char *path = NULL;
6387 err = got_opentemp_named(&path, &f, "got-histedit");
6388 if (err)
6389 return err;
6391 err = write_cmd_list(f, branch_name, commits);
6392 if (err)
6393 goto done;
6395 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6396 if (err)
6397 goto done;
6399 if (edit_logmsg_only) {
6400 rewind(f);
6401 err = histedit_parse_list(histedit_cmds, f, repo);
6402 } else {
6403 if (fclose(f) != 0) {
6404 err = got_error_from_errno("fclose");
6405 goto done;
6407 f = NULL;
6408 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6409 if (err) {
6410 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6411 err->code != GOT_ERR_HISTEDIT_CMD)
6412 goto done;
6413 err = histedit_edit_list_retry(histedit_cmds, err,
6414 commits, path, branch_name, repo);
6417 done:
6418 if (f && fclose(f) != 0 && err == NULL)
6419 err = got_error_from_errno("fclose");
6420 if (path && unlink(path) != 0 && err == NULL)
6421 err = got_error_from_errno2("unlink", path);
6422 free(path);
6423 return err;
6426 static const struct got_error *
6427 histedit_save_list(struct got_histedit_list *histedit_cmds,
6428 struct got_worktree *worktree, struct got_repository *repo)
6430 const struct got_error *err = NULL;
6431 char *path = NULL;
6432 FILE *f = NULL;
6433 struct got_histedit_list_entry *hle;
6434 struct got_commit_object *commit = NULL;
6436 err = got_worktree_get_histedit_script_path(&path, worktree);
6437 if (err)
6438 return err;
6440 f = fopen(path, "w");
6441 if (f == NULL) {
6442 err = got_error_from_errno2("fopen", path);
6443 goto done;
6445 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6446 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6447 repo);
6448 if (err)
6449 break;
6451 if (hle->logmsg) {
6452 int n = fprintf(f, "%c %s\n",
6453 GOT_HISTEDIT_MESG, hle->logmsg);
6454 if (n < 0) {
6455 err = got_ferror(f, GOT_ERR_IO);
6456 break;
6460 done:
6461 if (f && fclose(f) != 0 && err == NULL)
6462 err = got_error_from_errno("fclose");
6463 free(path);
6464 if (commit)
6465 got_object_commit_close(commit);
6466 return err;
6469 void
6470 histedit_free_list(struct got_histedit_list *histedit_cmds)
6472 struct got_histedit_list_entry *hle;
6474 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6475 TAILQ_REMOVE(histedit_cmds, hle, entry);
6476 free(hle);
6480 static const struct got_error *
6481 histedit_load_list(struct got_histedit_list *histedit_cmds,
6482 const char *path, struct got_repository *repo)
6484 const struct got_error *err = NULL;
6485 FILE *f = NULL;
6487 f = fopen(path, "r");
6488 if (f == NULL) {
6489 err = got_error_from_errno2("fopen", path);
6490 goto done;
6493 err = histedit_parse_list(histedit_cmds, f, repo);
6494 done:
6495 if (f && fclose(f) != 0 && err == NULL)
6496 err = got_error_from_errno("fclose");
6497 return err;
6500 static const struct got_error *
6501 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6502 const struct got_error *edit_err, struct got_object_id_queue *commits,
6503 const char *path, const char *branch_name, struct got_repository *repo)
6505 const struct got_error *err = NULL, *prev_err = edit_err;
6506 int resp = ' ';
6508 while (resp != 'c' && resp != 'r' && resp != 'a') {
6509 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6510 "or (a)bort: ", getprogname(), prev_err->msg);
6511 resp = getchar();
6512 if (resp == '\n')
6513 resp = getchar();
6514 if (resp == 'c') {
6515 histedit_free_list(histedit_cmds);
6516 err = histedit_run_editor(histedit_cmds, path, commits,
6517 repo);
6518 if (err) {
6519 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6520 err->code != GOT_ERR_HISTEDIT_CMD)
6521 break;
6522 prev_err = err;
6523 resp = ' ';
6524 continue;
6526 break;
6527 } else if (resp == 'r') {
6528 histedit_free_list(histedit_cmds);
6529 err = histedit_edit_script(histedit_cmds,
6530 commits, branch_name, 0, repo);
6531 if (err) {
6532 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6533 err->code != GOT_ERR_HISTEDIT_CMD)
6534 break;
6535 prev_err = err;
6536 resp = ' ';
6537 continue;
6539 break;
6540 } else if (resp == 'a') {
6541 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6542 break;
6543 } else
6544 printf("invalid response '%c'\n", resp);
6547 return err;
6550 static const struct got_error *
6551 histedit_complete(struct got_worktree *worktree,
6552 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6553 struct got_reference *branch, struct got_repository *repo)
6555 printf("Switching work tree to %s\n",
6556 got_ref_get_symref_target(branch));
6557 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6558 branch, repo);
6561 static const struct got_error *
6562 show_histedit_progress(struct got_commit_object *commit,
6563 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6565 const struct got_error *err;
6566 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6568 err = got_object_id_str(&old_id_str, hle->commit_id);
6569 if (err)
6570 goto done;
6572 if (new_id) {
6573 err = got_object_id_str(&new_id_str, new_id);
6574 if (err)
6575 goto done;
6578 old_id_str[12] = '\0';
6579 if (new_id_str)
6580 new_id_str[12] = '\0';
6582 if (hle->logmsg) {
6583 logmsg = strdup(hle->logmsg);
6584 if (logmsg == NULL) {
6585 err = got_error_from_errno("strdup");
6586 goto done;
6588 trim_logmsg(logmsg, 42);
6589 } else {
6590 err = get_short_logmsg(&logmsg, 42, commit);
6591 if (err)
6592 goto done;
6595 switch (hle->cmd->code) {
6596 case GOT_HISTEDIT_PICK:
6597 case GOT_HISTEDIT_EDIT:
6598 printf("%s -> %s: %s\n", old_id_str,
6599 new_id_str ? new_id_str : "no-op change", logmsg);
6600 break;
6601 case GOT_HISTEDIT_DROP:
6602 case GOT_HISTEDIT_FOLD:
6603 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6604 logmsg);
6605 break;
6606 default:
6607 break;
6609 done:
6610 free(old_id_str);
6611 free(new_id_str);
6612 return err;
6615 static const struct got_error *
6616 histedit_commit(struct got_pathlist_head *merged_paths,
6617 struct got_worktree *worktree, struct got_fileindex *fileindex,
6618 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6619 struct got_repository *repo)
6621 const struct got_error *err;
6622 struct got_commit_object *commit;
6623 struct got_object_id *new_commit_id;
6625 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6626 && hle->logmsg == NULL) {
6627 err = histedit_edit_logmsg(hle, repo);
6628 if (err)
6629 return err;
6632 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6633 if (err)
6634 return err;
6636 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6637 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6638 hle->logmsg, repo);
6639 if (err) {
6640 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6641 goto done;
6642 err = show_histedit_progress(commit, hle, NULL);
6643 } else {
6644 err = show_histedit_progress(commit, hle, new_commit_id);
6645 free(new_commit_id);
6647 done:
6648 got_object_commit_close(commit);
6649 return err;
6652 static const struct got_error *
6653 histedit_skip_commit(struct got_histedit_list_entry *hle,
6654 struct got_worktree *worktree, struct got_repository *repo)
6656 const struct got_error *error;
6657 struct got_commit_object *commit;
6659 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6660 repo);
6661 if (error)
6662 return error;
6664 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6665 if (error)
6666 return error;
6668 error = show_histedit_progress(commit, hle, NULL);
6669 got_object_commit_close(commit);
6670 return error;
6673 static const struct got_error *
6674 check_local_changes(void *arg, unsigned char status,
6675 unsigned char staged_status, const char *path,
6676 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6677 struct got_object_id *commit_id, int dirfd, const char *de_name)
6679 int *have_local_changes = arg;
6681 switch (status) {
6682 case GOT_STATUS_ADD:
6683 case GOT_STATUS_DELETE:
6684 case GOT_STATUS_MODIFY:
6685 case GOT_STATUS_CONFLICT:
6686 *have_local_changes = 1;
6687 return got_error(GOT_ERR_CANCELLED);
6688 default:
6689 break;
6692 switch (staged_status) {
6693 case GOT_STATUS_ADD:
6694 case GOT_STATUS_DELETE:
6695 case GOT_STATUS_MODIFY:
6696 *have_local_changes = 1;
6697 return got_error(GOT_ERR_CANCELLED);
6698 default:
6699 break;
6702 return NULL;
6705 static const struct got_error *
6706 cmd_histedit(int argc, char *argv[])
6708 const struct got_error *error = NULL;
6709 struct got_worktree *worktree = NULL;
6710 struct got_fileindex *fileindex = NULL;
6711 struct got_repository *repo = NULL;
6712 char *cwd = NULL;
6713 struct got_reference *branch = NULL;
6714 struct got_reference *tmp_branch = NULL;
6715 struct got_object_id *resume_commit_id = NULL;
6716 struct got_object_id *base_commit_id = NULL;
6717 struct got_object_id *head_commit_id = NULL;
6718 struct got_commit_object *commit = NULL;
6719 int ch, rebase_in_progress = 0, did_something;
6720 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6721 int edit_logmsg_only = 0;
6722 const char *edit_script_path = NULL;
6723 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6724 struct got_object_id_queue commits;
6725 struct got_pathlist_head merged_paths;
6726 const struct got_object_id_queue *parent_ids;
6727 struct got_object_qid *pid;
6728 struct got_histedit_list histedit_cmds;
6729 struct got_histedit_list_entry *hle;
6731 SIMPLEQ_INIT(&commits);
6732 TAILQ_INIT(&histedit_cmds);
6733 TAILQ_INIT(&merged_paths);
6735 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6736 switch (ch) {
6737 case 'a':
6738 abort_edit = 1;
6739 break;
6740 case 'c':
6741 continue_edit = 1;
6742 break;
6743 case 'F':
6744 edit_script_path = optarg;
6745 break;
6746 case 'm':
6747 edit_logmsg_only = 1;
6748 break;
6749 default:
6750 usage_histedit();
6751 /* NOTREACHED */
6755 argc -= optind;
6756 argv += optind;
6758 #ifndef PROFILE
6759 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6760 "unveil", NULL) == -1)
6761 err(1, "pledge");
6762 #endif
6763 if (abort_edit && continue_edit)
6764 errx(1, "histedit's -a and -c options are mutually exclusive");
6765 if (edit_script_path && edit_logmsg_only)
6766 errx(1, "histedit's -F and -m options are mutually exclusive");
6767 if (abort_edit && edit_logmsg_only)
6768 errx(1, "histedit's -a and -m options are mutually exclusive");
6769 if (continue_edit && edit_logmsg_only)
6770 errx(1, "histedit's -c and -m options are mutually exclusive");
6771 if (argc != 0)
6772 usage_histedit();
6775 * This command cannot apply unveil(2) in all cases because the
6776 * user may choose to run an editor to edit the histedit script
6777 * and to edit individual commit log messages.
6778 * unveil(2) traverses exec(2); if an editor is used we have to
6779 * apply unveil after edit script and log messages have been written.
6780 * XXX TODO: Make use of unveil(2) where possible.
6783 cwd = getcwd(NULL, 0);
6784 if (cwd == NULL) {
6785 error = got_error_from_errno("getcwd");
6786 goto done;
6788 error = got_worktree_open(&worktree, cwd);
6789 if (error)
6790 goto done;
6792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6793 NULL);
6794 if (error != NULL)
6795 goto done;
6797 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6798 if (error)
6799 goto done;
6800 if (rebase_in_progress) {
6801 error = got_error(GOT_ERR_REBASING);
6802 goto done;
6805 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6806 if (error)
6807 goto done;
6809 if (edit_in_progress && edit_logmsg_only) {
6810 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6811 "histedit operation is in progress in this "
6812 "work tree and must be continued or aborted "
6813 "before the -m option can be used");
6814 goto done;
6817 if (edit_in_progress && abort_edit) {
6818 error = got_worktree_histedit_continue(&resume_commit_id,
6819 &tmp_branch, &branch, &base_commit_id, &fileindex,
6820 worktree, repo);
6821 if (error)
6822 goto done;
6823 printf("Switching work tree to %s\n",
6824 got_ref_get_symref_target(branch));
6825 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6826 branch, base_commit_id, update_progress, &did_something);
6827 if (error)
6828 goto done;
6829 printf("Histedit of %s aborted\n",
6830 got_ref_get_symref_target(branch));
6831 goto done; /* nothing else to do */
6832 } else if (abort_edit) {
6833 error = got_error(GOT_ERR_NOT_HISTEDIT);
6834 goto done;
6837 if (continue_edit) {
6838 char *path;
6840 if (!edit_in_progress) {
6841 error = got_error(GOT_ERR_NOT_HISTEDIT);
6842 goto done;
6845 error = got_worktree_get_histedit_script_path(&path, worktree);
6846 if (error)
6847 goto done;
6849 error = histedit_load_list(&histedit_cmds, path, repo);
6850 free(path);
6851 if (error)
6852 goto done;
6854 error = got_worktree_histedit_continue(&resume_commit_id,
6855 &tmp_branch, &branch, &base_commit_id, &fileindex,
6856 worktree, repo);
6857 if (error)
6858 goto done;
6860 error = got_ref_resolve(&head_commit_id, repo, branch);
6861 if (error)
6862 goto done;
6864 error = got_object_open_as_commit(&commit, repo,
6865 head_commit_id);
6866 if (error)
6867 goto done;
6868 parent_ids = got_object_commit_get_parent_ids(commit);
6869 pid = SIMPLEQ_FIRST(parent_ids);
6870 if (pid == NULL) {
6871 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6872 goto done;
6874 error = collect_commits(&commits, head_commit_id, pid->id,
6875 base_commit_id, got_worktree_get_path_prefix(worktree),
6876 GOT_ERR_HISTEDIT_PATH, repo);
6877 got_object_commit_close(commit);
6878 commit = NULL;
6879 if (error)
6880 goto done;
6881 } else {
6882 if (edit_in_progress) {
6883 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6884 goto done;
6887 error = got_ref_open(&branch, repo,
6888 got_worktree_get_head_ref_name(worktree), 0);
6889 if (error != NULL)
6890 goto done;
6892 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6893 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6894 "will not edit commit history of a branch outside "
6895 "the \"refs/heads/\" reference namespace");
6896 goto done;
6899 error = got_ref_resolve(&head_commit_id, repo, branch);
6900 got_ref_close(branch);
6901 branch = NULL;
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 got_worktree_get_base_commit_id(worktree),
6917 got_worktree_get_path_prefix(worktree),
6918 GOT_ERR_HISTEDIT_PATH, repo);
6919 got_object_commit_close(commit);
6920 commit = NULL;
6921 if (error)
6922 goto done;
6924 if (SIMPLEQ_EMPTY(&commits)) {
6925 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6926 goto done;
6929 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6930 &base_commit_id, &fileindex, worktree, repo);
6931 if (error)
6932 goto done;
6934 if (edit_script_path) {
6935 error = histedit_load_list(&histedit_cmds,
6936 edit_script_path, repo);
6937 if (error) {
6938 got_worktree_histedit_abort(worktree, fileindex,
6939 repo, branch, base_commit_id,
6940 update_progress, &did_something);
6941 goto done;
6943 } else {
6944 const char *branch_name;
6945 branch_name = got_ref_get_symref_target(branch);
6946 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6947 branch_name += 11;
6948 error = histedit_edit_script(&histedit_cmds, &commits,
6949 branch_name, edit_logmsg_only, repo);
6950 if (error) {
6951 got_worktree_histedit_abort(worktree, fileindex,
6952 repo, branch, base_commit_id,
6953 update_progress, &did_something);
6954 goto done;
6959 error = histedit_save_list(&histedit_cmds, worktree,
6960 repo);
6961 if (error) {
6962 got_worktree_histedit_abort(worktree, fileindex,
6963 repo, branch, base_commit_id,
6964 update_progress, &did_something);
6965 goto done;
6970 error = histedit_check_script(&histedit_cmds, &commits, repo);
6971 if (error)
6972 goto done;
6974 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6975 if (resume_commit_id) {
6976 if (got_object_id_cmp(hle->commit_id,
6977 resume_commit_id) != 0)
6978 continue;
6980 resume_commit_id = NULL;
6981 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6982 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6983 error = histedit_skip_commit(hle, worktree,
6984 repo);
6985 if (error)
6986 goto done;
6987 } else {
6988 struct got_pathlist_head paths;
6989 int have_changes = 0;
6991 TAILQ_INIT(&paths);
6992 error = got_pathlist_append(&paths, "", NULL);
6993 if (error)
6994 goto done;
6995 error = got_worktree_status(worktree, &paths,
6996 repo, check_local_changes, &have_changes,
6997 check_cancelled, NULL);
6998 got_pathlist_free(&paths);
6999 if (error) {
7000 if (error->code != GOT_ERR_CANCELLED)
7001 goto done;
7002 if (sigint_received || sigpipe_received)
7003 goto done;
7005 if (have_changes) {
7006 error = histedit_commit(NULL, worktree,
7007 fileindex, tmp_branch, hle, repo);
7008 if (error)
7009 goto done;
7010 } else {
7011 error = got_object_open_as_commit(
7012 &commit, repo, hle->commit_id);
7013 if (error)
7014 goto done;
7015 error = show_histedit_progress(commit,
7016 hle, NULL);
7017 got_object_commit_close(commit);
7018 commit = NULL;
7019 if (error)
7020 goto done;
7023 continue;
7026 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7027 error = histedit_skip_commit(hle, worktree, repo);
7028 if (error)
7029 goto done;
7030 continue;
7033 error = got_object_open_as_commit(&commit, repo,
7034 hle->commit_id);
7035 if (error)
7036 goto done;
7037 parent_ids = got_object_commit_get_parent_ids(commit);
7038 pid = SIMPLEQ_FIRST(parent_ids);
7040 error = got_worktree_histedit_merge_files(&merged_paths,
7041 worktree, fileindex, pid->id, hle->commit_id, repo,
7042 rebase_progress, &rebase_status, check_cancelled, NULL);
7043 if (error)
7044 goto done;
7045 got_object_commit_close(commit);
7046 commit = NULL;
7048 if (rebase_status == GOT_STATUS_CONFLICT) {
7049 error = show_rebase_merge_conflict(hle->commit_id,
7050 repo);
7051 if (error)
7052 goto done;
7053 got_worktree_rebase_pathlist_free(&merged_paths);
7054 break;
7057 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7058 char *id_str;
7059 error = got_object_id_str(&id_str, hle->commit_id);
7060 if (error)
7061 goto done;
7062 printf("Stopping histedit for amending commit %s\n",
7063 id_str);
7064 free(id_str);
7065 got_worktree_rebase_pathlist_free(&merged_paths);
7066 error = got_worktree_histedit_postpone(worktree,
7067 fileindex);
7068 goto done;
7071 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7072 error = histedit_skip_commit(hle, worktree, repo);
7073 if (error)
7074 goto done;
7075 continue;
7078 error = histedit_commit(&merged_paths, worktree, fileindex,
7079 tmp_branch, hle, repo);
7080 got_worktree_rebase_pathlist_free(&merged_paths);
7081 if (error)
7082 goto done;
7085 if (rebase_status == GOT_STATUS_CONFLICT) {
7086 error = got_worktree_histedit_postpone(worktree, fileindex);
7087 if (error)
7088 goto done;
7089 error = got_error_msg(GOT_ERR_CONFLICTS,
7090 "conflicts must be resolved before histedit can continue");
7091 } else
7092 error = histedit_complete(worktree, fileindex, tmp_branch,
7093 branch, repo);
7094 done:
7095 got_object_id_queue_free(&commits);
7096 histedit_free_list(&histedit_cmds);
7097 free(head_commit_id);
7098 free(base_commit_id);
7099 free(resume_commit_id);
7100 if (commit)
7101 got_object_commit_close(commit);
7102 if (branch)
7103 got_ref_close(branch);
7104 if (tmp_branch)
7105 got_ref_close(tmp_branch);
7106 if (worktree)
7107 got_worktree_close(worktree);
7108 if (repo)
7109 got_repo_close(repo);
7110 return error;
7113 __dead static void
7114 usage_integrate(void)
7116 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7117 exit(1);
7120 static const struct got_error *
7121 cmd_integrate(int argc, char *argv[])
7123 const struct got_error *error = NULL;
7124 struct got_repository *repo = NULL;
7125 struct got_worktree *worktree = NULL;
7126 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7127 const char *branch_arg = NULL;
7128 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7129 struct got_fileindex *fileindex = NULL;
7130 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7131 int ch, did_something = 0;
7133 while ((ch = getopt(argc, argv, "")) != -1) {
7134 switch (ch) {
7135 default:
7136 usage_integrate();
7137 /* NOTREACHED */
7141 argc -= optind;
7142 argv += optind;
7144 if (argc != 1)
7145 usage_integrate();
7146 branch_arg = argv[0];
7148 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7149 "unveil", NULL) == -1)
7150 err(1, "pledge");
7152 cwd = getcwd(NULL, 0);
7153 if (cwd == NULL) {
7154 error = got_error_from_errno("getcwd");
7155 goto done;
7158 error = got_worktree_open(&worktree, cwd);
7159 if (error)
7160 goto done;
7162 error = check_rebase_or_histedit_in_progress(worktree);
7163 if (error)
7164 goto done;
7166 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7167 NULL);
7168 if (error != NULL)
7169 goto done;
7171 error = apply_unveil(got_repo_get_path(repo), 0,
7172 got_worktree_get_root_path(worktree));
7173 if (error)
7174 goto done;
7176 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7177 error = got_error_from_errno("asprintf");
7178 goto done;
7181 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7182 &base_branch_ref, worktree, refname, repo);
7183 if (error)
7184 goto done;
7186 refname = strdup(got_ref_get_name(branch_ref));
7187 if (refname == NULL) {
7188 error = got_error_from_errno("strdup");
7189 got_worktree_integrate_abort(worktree, fileindex, repo,
7190 branch_ref, base_branch_ref);
7191 goto done;
7193 base_refname = strdup(got_ref_get_name(base_branch_ref));
7194 if (base_refname == NULL) {
7195 error = got_error_from_errno("strdup");
7196 got_worktree_integrate_abort(worktree, fileindex, repo,
7197 branch_ref, base_branch_ref);
7198 goto done;
7201 error = got_ref_resolve(&commit_id, repo, branch_ref);
7202 if (error)
7203 goto done;
7205 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7206 if (error)
7207 goto done;
7209 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7210 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7211 "specified branch has already been integrated");
7212 got_worktree_integrate_abort(worktree, fileindex, repo,
7213 branch_ref, base_branch_ref);
7214 goto done;
7217 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7218 if (error) {
7219 if (error->code == GOT_ERR_ANCESTRY)
7220 error = got_error(GOT_ERR_REBASE_REQUIRED);
7221 got_worktree_integrate_abort(worktree, fileindex, repo,
7222 branch_ref, base_branch_ref);
7223 goto done;
7226 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7227 branch_ref, base_branch_ref, update_progress, &did_something,
7228 check_cancelled, NULL);
7229 if (error)
7230 goto done;
7232 printf("Integrated %s into %s\n", refname, base_refname);
7233 done:
7234 if (repo)
7235 got_repo_close(repo);
7236 if (worktree)
7237 got_worktree_close(worktree);
7238 free(cwd);
7239 free(base_commit_id);
7240 free(commit_id);
7241 free(refname);
7242 free(base_refname);
7243 return error;
7246 __dead static void
7247 usage_stage(void)
7249 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7250 "[file-path ...]\n",
7251 getprogname());
7252 exit(1);
7255 static const struct got_error *
7256 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7257 const char *path, struct got_object_id *blob_id,
7258 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7259 int dirfd, const char *de_name)
7261 const struct got_error *err = NULL;
7262 char *id_str = NULL;
7264 if (staged_status != GOT_STATUS_ADD &&
7265 staged_status != GOT_STATUS_MODIFY &&
7266 staged_status != GOT_STATUS_DELETE)
7267 return NULL;
7269 if (staged_status == GOT_STATUS_ADD ||
7270 staged_status == GOT_STATUS_MODIFY)
7271 err = got_object_id_str(&id_str, staged_blob_id);
7272 else
7273 err = got_object_id_str(&id_str, blob_id);
7274 if (err)
7275 return err;
7277 printf("%s %c %s\n", id_str, staged_status, path);
7278 free(id_str);
7279 return NULL;
7282 static const struct got_error *
7283 cmd_stage(int argc, char *argv[])
7285 const struct got_error *error = NULL;
7286 struct got_repository *repo = NULL;
7287 struct got_worktree *worktree = NULL;
7288 char *cwd = NULL;
7289 struct got_pathlist_head paths;
7290 struct got_pathlist_entry *pe;
7291 int ch, list_stage = 0, pflag = 0;
7292 FILE *patch_script_file = NULL;
7293 const char *patch_script_path = NULL;
7294 struct choose_patch_arg cpa;
7296 TAILQ_INIT(&paths);
7298 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7299 switch (ch) {
7300 case 'l':
7301 list_stage = 1;
7302 break;
7303 case 'p':
7304 pflag = 1;
7305 break;
7306 case 'F':
7307 patch_script_path = optarg;
7308 break;
7309 default:
7310 usage_stage();
7311 /* NOTREACHED */
7315 argc -= optind;
7316 argv += optind;
7318 #ifndef PROFILE
7319 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7320 "unveil", NULL) == -1)
7321 err(1, "pledge");
7322 #endif
7323 if (list_stage && (pflag || patch_script_path))
7324 errx(1, "-l option cannot be used with other options");
7325 if (patch_script_path && !pflag)
7326 errx(1, "-F option can only be used together with -p option");
7328 cwd = getcwd(NULL, 0);
7329 if (cwd == NULL) {
7330 error = got_error_from_errno("getcwd");
7331 goto done;
7334 error = got_worktree_open(&worktree, cwd);
7335 if (error)
7336 goto done;
7338 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7339 NULL);
7340 if (error != NULL)
7341 goto done;
7343 if (patch_script_path) {
7344 patch_script_file = fopen(patch_script_path, "r");
7345 if (patch_script_file == NULL) {
7346 error = got_error_from_errno2("fopen",
7347 patch_script_path);
7348 goto done;
7351 error = apply_unveil(got_repo_get_path(repo), 0,
7352 got_worktree_get_root_path(worktree));
7353 if (error)
7354 goto done;
7356 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7357 if (error)
7358 goto done;
7360 if (list_stage)
7361 error = got_worktree_status(worktree, &paths, repo,
7362 print_stage, NULL, check_cancelled, NULL);
7363 else {
7364 cpa.patch_script_file = patch_script_file;
7365 cpa.action = "stage";
7366 error = got_worktree_stage(worktree, &paths,
7367 pflag ? NULL : print_status, NULL,
7368 pflag ? choose_patch : NULL, &cpa, repo);
7370 done:
7371 if (patch_script_file && fclose(patch_script_file) == EOF &&
7372 error == NULL)
7373 error = got_error_from_errno2("fclose", patch_script_path);
7374 if (repo)
7375 got_repo_close(repo);
7376 if (worktree)
7377 got_worktree_close(worktree);
7378 TAILQ_FOREACH(pe, &paths, entry)
7379 free((char *)pe->path);
7380 got_pathlist_free(&paths);
7381 free(cwd);
7382 return error;
7385 __dead static void
7386 usage_unstage(void)
7388 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7389 "[file-path ...]\n",
7390 getprogname());
7391 exit(1);
7395 static const struct got_error *
7396 cmd_unstage(int argc, char *argv[])
7398 const struct got_error *error = NULL;
7399 struct got_repository *repo = NULL;
7400 struct got_worktree *worktree = NULL;
7401 char *cwd = NULL;
7402 struct got_pathlist_head paths;
7403 struct got_pathlist_entry *pe;
7404 int ch, did_something = 0, pflag = 0;
7405 FILE *patch_script_file = NULL;
7406 const char *patch_script_path = NULL;
7407 struct choose_patch_arg cpa;
7409 TAILQ_INIT(&paths);
7411 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7412 switch (ch) {
7413 case 'p':
7414 pflag = 1;
7415 break;
7416 case 'F':
7417 patch_script_path = optarg;
7418 break;
7419 default:
7420 usage_unstage();
7421 /* NOTREACHED */
7425 argc -= optind;
7426 argv += optind;
7428 #ifndef PROFILE
7429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7430 "unveil", NULL) == -1)
7431 err(1, "pledge");
7432 #endif
7433 if (patch_script_path && !pflag)
7434 errx(1, "-F option can only be used together with -p option");
7436 cwd = getcwd(NULL, 0);
7437 if (cwd == NULL) {
7438 error = got_error_from_errno("getcwd");
7439 goto done;
7442 error = got_worktree_open(&worktree, cwd);
7443 if (error)
7444 goto done;
7446 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7447 NULL);
7448 if (error != NULL)
7449 goto done;
7451 if (patch_script_path) {
7452 patch_script_file = fopen(patch_script_path, "r");
7453 if (patch_script_file == NULL) {
7454 error = got_error_from_errno2("fopen",
7455 patch_script_path);
7456 goto done;
7460 error = apply_unveil(got_repo_get_path(repo), 0,
7461 got_worktree_get_root_path(worktree));
7462 if (error)
7463 goto done;
7465 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7466 if (error)
7467 goto done;
7469 cpa.patch_script_file = patch_script_file;
7470 cpa.action = "unstage";
7471 error = got_worktree_unstage(worktree, &paths, update_progress,
7472 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7473 done:
7474 if (patch_script_file && fclose(patch_script_file) == EOF &&
7475 error == NULL)
7476 error = got_error_from_errno2("fclose", patch_script_path);
7477 if (repo)
7478 got_repo_close(repo);
7479 if (worktree)
7480 got_worktree_close(worktree);
7481 TAILQ_FOREACH(pe, &paths, entry)
7482 free((char *)pe->path);
7483 got_pathlist_free(&paths);
7484 free(cwd);
7485 return error;
7488 __dead static void
7489 usage_cat(void)
7491 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7492 "arg1 [arg2 ...]\n", getprogname());
7493 exit(1);
7496 static const struct got_error *
7497 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7499 const struct got_error *err;
7500 struct got_blob_object *blob;
7502 err = got_object_open_as_blob(&blob, repo, id, 8192);
7503 if (err)
7504 return err;
7506 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7507 got_object_blob_close(blob);
7508 return err;
7511 static const struct got_error *
7512 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7514 const struct got_error *err;
7515 struct got_tree_object *tree;
7516 int nentries, i;
7518 err = got_object_open_as_tree(&tree, repo, id);
7519 if (err)
7520 return err;
7522 nentries = got_object_tree_get_nentries(tree);
7523 for (i = 0; i < nentries; i++) {
7524 struct got_tree_entry *te;
7525 char *id_str;
7526 if (sigint_received || sigpipe_received)
7527 break;
7528 te = got_object_tree_get_entry(tree, i);
7529 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7530 if (err)
7531 break;
7532 fprintf(outfile, "%s %.7o %s\n", id_str,
7533 got_tree_entry_get_mode(te),
7534 got_tree_entry_get_name(te));
7535 free(id_str);
7538 got_object_tree_close(tree);
7539 return err;
7542 static const struct got_error *
7543 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7545 const struct got_error *err;
7546 struct got_commit_object *commit;
7547 const struct got_object_id_queue *parent_ids;
7548 struct got_object_qid *pid;
7549 char *id_str = NULL;
7550 const char *logmsg = NULL;
7552 err = got_object_open_as_commit(&commit, repo, id);
7553 if (err)
7554 return err;
7556 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7557 if (err)
7558 goto done;
7560 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7561 parent_ids = got_object_commit_get_parent_ids(commit);
7562 fprintf(outfile, "numparents %d\n",
7563 got_object_commit_get_nparents(commit));
7564 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7565 char *pid_str;
7566 err = got_object_id_str(&pid_str, pid->id);
7567 if (err)
7568 goto done;
7569 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7570 free(pid_str);
7572 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7573 got_object_commit_get_author(commit),
7574 got_object_commit_get_author_time(commit));
7576 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7577 got_object_commit_get_author(commit),
7578 got_object_commit_get_committer_time(commit));
7580 logmsg = got_object_commit_get_logmsg_raw(commit);
7581 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7582 fprintf(outfile, "%s", logmsg);
7583 done:
7584 free(id_str);
7585 got_object_commit_close(commit);
7586 return err;
7589 static const struct got_error *
7590 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7592 const struct got_error *err;
7593 struct got_tag_object *tag;
7594 char *id_str = NULL;
7595 const char *tagmsg = NULL;
7597 err = got_object_open_as_tag(&tag, repo, id);
7598 if (err)
7599 return err;
7601 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7602 if (err)
7603 goto done;
7605 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7607 switch (got_object_tag_get_object_type(tag)) {
7608 case GOT_OBJ_TYPE_BLOB:
7609 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7610 GOT_OBJ_LABEL_BLOB);
7611 break;
7612 case GOT_OBJ_TYPE_TREE:
7613 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7614 GOT_OBJ_LABEL_TREE);
7615 break;
7616 case GOT_OBJ_TYPE_COMMIT:
7617 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7618 GOT_OBJ_LABEL_COMMIT);
7619 break;
7620 case GOT_OBJ_TYPE_TAG:
7621 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7622 GOT_OBJ_LABEL_TAG);
7623 break;
7624 default:
7625 break;
7628 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7629 got_object_tag_get_name(tag));
7631 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7632 got_object_tag_get_tagger(tag),
7633 got_object_tag_get_tagger_time(tag));
7635 tagmsg = got_object_tag_get_message(tag);
7636 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7637 fprintf(outfile, "%s", tagmsg);
7638 done:
7639 free(id_str);
7640 got_object_tag_close(tag);
7641 return err;
7644 static const struct got_error *
7645 cmd_cat(int argc, char *argv[])
7647 const struct got_error *error;
7648 struct got_repository *repo = NULL;
7649 struct got_worktree *worktree = NULL;
7650 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7651 const char *commit_id_str = NULL;
7652 struct got_object_id *id = NULL, *commit_id = NULL;
7653 int ch, obj_type, i, force_path = 0;
7655 #ifndef PROFILE
7656 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7657 NULL) == -1)
7658 err(1, "pledge");
7659 #endif
7661 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7662 switch (ch) {
7663 case 'c':
7664 commit_id_str = optarg;
7665 break;
7666 case 'r':
7667 repo_path = realpath(optarg, NULL);
7668 if (repo_path == NULL)
7669 return got_error_from_errno2("realpath",
7670 optarg);
7671 got_path_strip_trailing_slashes(repo_path);
7672 break;
7673 case 'P':
7674 force_path = 1;
7675 break;
7676 default:
7677 usage_cat();
7678 /* NOTREACHED */
7682 argc -= optind;
7683 argv += optind;
7685 cwd = getcwd(NULL, 0);
7686 if (cwd == NULL) {
7687 error = got_error_from_errno("getcwd");
7688 goto done;
7690 error = got_worktree_open(&worktree, cwd);
7691 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7692 goto done;
7693 if (worktree) {
7694 if (repo_path == NULL) {
7695 repo_path = strdup(
7696 got_worktree_get_repo_path(worktree));
7697 if (repo_path == NULL) {
7698 error = got_error_from_errno("strdup");
7699 goto done;
7704 if (repo_path == NULL) {
7705 repo_path = getcwd(NULL, 0);
7706 if (repo_path == NULL)
7707 return got_error_from_errno("getcwd");
7710 error = got_repo_open(&repo, repo_path, NULL);
7711 free(repo_path);
7712 if (error != NULL)
7713 goto done;
7715 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7716 if (error)
7717 goto done;
7719 if (commit_id_str == NULL)
7720 commit_id_str = GOT_REF_HEAD;
7721 error = got_repo_match_object_id(&commit_id, NULL,
7722 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7723 if (error)
7724 goto done;
7726 for (i = 0; i < argc; i++) {
7727 if (force_path) {
7728 error = got_object_id_by_path(&id, repo, commit_id,
7729 argv[i]);
7730 if (error)
7731 break;
7732 } else {
7733 error = got_repo_match_object_id(&id, &label, argv[i],
7734 GOT_OBJ_TYPE_ANY, 0, repo);
7735 if (error) {
7736 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7737 error->code != GOT_ERR_NOT_REF)
7738 break;
7739 error = got_object_id_by_path(&id, repo,
7740 commit_id, argv[i]);
7741 if (error)
7742 break;
7746 error = got_object_get_type(&obj_type, repo, id);
7747 if (error)
7748 break;
7750 switch (obj_type) {
7751 case GOT_OBJ_TYPE_BLOB:
7752 error = cat_blob(id, repo, stdout);
7753 break;
7754 case GOT_OBJ_TYPE_TREE:
7755 error = cat_tree(id, repo, stdout);
7756 break;
7757 case GOT_OBJ_TYPE_COMMIT:
7758 error = cat_commit(id, repo, stdout);
7759 break;
7760 case GOT_OBJ_TYPE_TAG:
7761 error = cat_tag(id, repo, stdout);
7762 break;
7763 default:
7764 error = got_error(GOT_ERR_OBJ_TYPE);
7765 break;
7767 if (error)
7768 break;
7769 free(label);
7770 label = NULL;
7771 free(id);
7772 id = NULL;
7774 done:
7775 free(label);
7776 free(id);
7777 free(commit_id);
7778 if (worktree)
7779 got_worktree_close(worktree);
7780 if (repo) {
7781 const struct got_error *repo_error;
7782 repo_error = got_repo_close(repo);
7783 if (error == NULL)
7784 error = repo_error;
7786 return error;