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_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
167 };
169 static void
170 list_commands(void)
172 int i;
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
179 fputc('\n', stderr);
182 int
183 main(int argc, char *argv[])
185 struct got_cmd *cmd;
186 unsigned int i;
187 int ch;
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
191 { NULL, 0, NULL, 0}
192 };
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 switch (ch) {
198 case 'h':
199 hflag = 1;
200 break;
201 case 'V':
202 Vflag = 1;
203 break;
204 default:
205 usage(hflag);
206 /* NOTREACHED */
210 argc -= optind;
211 argv += optind;
212 optind = 0;
214 if (Vflag) {
215 got_version_print_str();
216 return 1;
219 if (argc <= 0)
220 usage(hflag);
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
232 continue;
234 if (hflag)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 !(sigint_received &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 return 1;
248 return 0;
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 list_commands();
253 return 1;
256 __dead static void
257 usage(int hflag)
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 getprogname());
261 if (hflag)
262 list_commands();
263 exit(1);
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
270 const char *editor;
272 *abspath = NULL;
274 editor = getenv("VISUAL");
275 if (editor == NULL)
276 editor = getenv("EDITOR");
278 if (editor) {
279 err = got_path_find_prog(abspath, editor);
280 if (err)
281 return err;
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
290 return NULL;
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
299 #ifdef PROFILE
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
302 #endif
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
313 if (err != NULL)
314 return err;
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
319 return NULL;
322 __dead static void
323 usage_init(void)
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 exit(1);
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
334 int ch;
336 while ((ch = getopt(argc, argv, "")) != -1) {
337 switch (ch) {
338 default:
339 usage_init();
340 /* NOTREACHED */
344 argc -= optind;
345 argv += optind;
347 #ifndef PROFILE
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 err(1, "pledge");
350 #endif
351 if (argc != 1)
352 usage_init();
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
361 if (error &&
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 goto done;
365 error = apply_unveil(repo_path, 0, NULL);
366 if (error)
367 goto done;
369 error = got_repo_init(repo_path);
370 done:
371 free(repo_path);
372 return error;
375 __dead static void
376 usage_import(void)
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
380 exit(1);
383 int
384 spawn_editor(const char *editor, const char *file)
386 pid_t pid;
387 sig_t sighup, sigint, sigquit;
388 int st = -1;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
395 case -1:
396 goto doneediting;
397 case 0:
398 execl(editor, editor, file, (char *)NULL);
399 _exit(127);
402 while (waitpid(pid, &st, 0) == -1)
403 if (errno != EINTR)
404 break;
406 doneediting:
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
412 errno = EINTR;
413 return -1;
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
424 char buf[1024];
425 struct stat st, st2;
426 FILE *fp;
427 int content_changed = 0;
428 size_t len;
430 *logmsg = NULL;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
446 if (*logmsg == NULL)
447 return got_error_from_errno("malloc");
448 (*logmsg)[0] = '\0';
449 len = 0;
451 fp = fopen(logmsg_path, "r");
452 if (fp == NULL) {
453 err = got_error_from_errno("fopen");
454 goto done;
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
458 content_changed = 1;
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
463 fclose(fp);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
467 len--;
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
473 done:
474 if (err) {
475 free(*logmsg);
476 *logmsg = NULL;
478 return err;
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
487 int fd;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
491 branch_name) == -1)
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
496 if (err)
497 goto done;
499 dprintf(fd, initial_content);
500 close(fd);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 done:
504 free(initial_content);
505 return err;
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
512 return NULL;
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
521 *author = NULL;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
525 if (name && email) {
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
528 return NULL;
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
535 if (name && email) {
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
538 return NULL;
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
545 if (*author == NULL)
546 return got_error_from_errno("strdup");
548 /*
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
551 */
552 while (*got_author && *got_author != '<')
553 got_author++;
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 goto done;
558 while (*got_author && *got_author != '@')
559 got_author++;
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 goto done;
564 while (*got_author && *got_author != '>')
565 got_author++;
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 done:
569 if (err) {
570 free(*author);
571 *author = NULL;
573 return err;
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
582 if (homedir) {
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
587 return NULL;
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
601 int ch;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 switch (ch) {
610 case 'b':
611 branch_name = optarg;
612 break;
613 case 'm':
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
617 goto done;
619 break;
620 case 'r':
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
624 optarg);
625 goto done;
627 break;
628 case 'I':
629 if (optarg[0] == '\0')
630 break;
631 error = got_pathlist_insert(&pe, &ignores, optarg,
632 NULL);
633 if (error)
634 goto done;
635 break;
636 default:
637 usage_import();
638 /* NOTREACHED */
642 argc -= optind;
643 argv += optind;
645 #ifndef PROFILE
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 "unveil",
648 NULL) == -1)
649 err(1, "pledge");
650 #endif
651 if (argc != 1)
652 usage_import();
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
661 if (error)
662 goto done;
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
664 if (error)
665 goto done;
667 error = get_author(&author, repo);
668 if (error)
669 return error;
671 /*
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
675 */
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
681 goto done;
684 error = got_ref_open(&branch_ref, repo, refname, 0);
685 if (error) {
686 if (error->code != GOT_ERR_NOT_REF)
687 goto done;
688 } else {
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
691 goto done;
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
697 goto done;
699 got_path_strip_trailing_slashes(path_dir);
701 /*
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
704 */
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
707 if (error)
708 goto done;
709 free(logmsg);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 path_dir, refname);
712 if (error) {
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 logmsg_path != NULL)
715 preserve_logmsg = 1;
716 goto done;
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
722 if (logmsg_path)
723 preserve_logmsg = 1;
724 goto done;
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 if (error) {
729 if (logmsg_path)
730 preserve_logmsg = 1;
731 goto done;
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
736 if (error) {
737 if (logmsg_path)
738 preserve_logmsg = 1;
739 goto done;
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 if (error) {
744 if (logmsg_path)
745 preserve_logmsg = 1;
746 goto done;
749 error = got_ref_write(branch_ref, repo);
750 if (error) {
751 if (logmsg_path)
752 preserve_logmsg = 1;
753 goto done;
756 error = got_object_id_str(&id_str, new_commit_id);
757 if (error) {
758 if (logmsg_path)
759 preserve_logmsg = 1;
760 goto done;
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 if (error) {
765 if (error->code != GOT_ERR_NOT_REF) {
766 if (logmsg_path)
767 preserve_logmsg = 1;
768 goto done;
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 branch_ref);
773 if (error) {
774 if (logmsg_path)
775 preserve_logmsg = 1;
776 goto done;
779 error = got_ref_write(head_ref, repo);
780 if (error) {
781 if (logmsg_path)
782 preserve_logmsg = 1;
783 goto done;
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
789 done:
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
795 free(logmsg);
796 free(logmsg_path);
797 free(repo_path);
798 free(editor);
799 free(refname);
800 free(new_commit_id);
801 free(id_str);
802 free(author);
803 free(gitconfig_path);
804 if (branch_ref)
805 got_ref_close(branch_ref);
806 if (head_ref)
807 got_ref_close(head_ref);
808 return error;
811 __dead static void
812 usage_clone(void)
814 fprintf(stderr, "usage: %s clone [-q] [-v] repository-url "
815 "[target-directory]\n", getprogname());
816 exit(1);
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
821 int last_p_indexed;
822 int last_p_resolved;
823 int verbosity;
824 };
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
836 return NULL;
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
840 fflush(stdout);
841 return NULL;
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 print_size = 1;
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
857 print_indexed = 1;
858 print_size = 1;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
866 print_resolved = 1;
867 print_indexed = 1;
868 print_size = 1;
873 if (print_size || print_indexed || print_resolved)
874 printf("\r");
875 if (print_size)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 if (print_indexed)
878 printf("; indexing %d%%", p_indexed);
879 if (print_resolved)
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
882 fflush(stdout);
884 return NULL;
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
905 ssize_t n;
906 int verbosity = 0;
908 TAILQ_INIT(&refs);
909 TAILQ_INIT(&symrefs);
911 while ((ch = getopt(argc, argv, "vq")) != -1) {
912 switch (ch) {
913 case 'v':
914 if (verbosity < 0)
915 verbosity = 0;
916 else if (verbosity < 3)
917 verbosity++;
918 break;
919 case 'q':
920 verbosity = -1;
921 break;
922 default:
923 usage_clone();
924 break;
927 argc -= optind;
928 argv += optind;
930 uri = argv[0];
932 if (argc == 1)
933 dirname = NULL;
934 else if (argc == 2)
935 dirname = argv[1];
936 else
937 usage_clone();
939 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
940 &repo_name, argv[0]);
941 if (error)
942 goto done;
944 if (strcmp(proto, "git") == 0) {
945 #ifndef PROFILE
946 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
947 "sendfd dns inet unveil", NULL) == -1)
948 err(1, "pledge");
949 #endif
950 git_url = strdup(argv[0]);
951 if (git_url == NULL) {
952 error = got_error_from_errno("strdup");
953 goto done;
955 } else if (strcmp(proto, "git+ssh") == 0 ||
956 strcmp(proto, "ssh") == 0) {
957 #ifndef PROFILE
958 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
959 "sendfd unveil", NULL) == -1)
960 err(1, "pledge");
961 #endif
962 if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
963 error = got_error_from_errno("asprintf");
964 goto done;
966 } else if (strcmp(proto, "http") == 0 ||
967 strcmp(proto, "git+http") == 0) {
968 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
969 goto done;
970 } else {
971 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
972 goto done;
974 if (dirname == NULL) {
975 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
976 error = got_error_from_errno("asprintf");
977 goto done;
979 repo_path = default_destdir;
980 } else
981 repo_path = dirname;
983 error = got_path_mkdir(repo_path);
984 if (error)
985 goto done;
987 error = got_repo_init(repo_path);
988 if (error)
989 goto done;
991 error = got_repo_open(&repo, repo_path, NULL);
992 if (error)
993 goto done;
995 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
996 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
997 error = got_error_from_errno2("unveil",
998 GOT_FETCH_PATH_SSH);
999 goto done;
1002 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1003 if (error)
1004 goto done;
1006 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1007 verbosity);
1008 if (error)
1009 goto done;
1011 if (verbosity >= 0)
1012 printf("Connected to %s:%s\n", host, port);
1014 /* Create a config file git-fetch(1) can understand. */
1015 gitconfig_path = got_repo_get_path_gitconfig(repo);
1016 if (gitconfig_path == NULL) {
1017 error = got_error_from_errno("got_repo_get_path_gitconfig");
1018 goto done;
1020 gitconfig_file = fopen(gitconfig_path, "a");
1021 if (gitconfig_file == NULL) {
1022 error = got_error_from_errno2("fopen", gitconfig_path);
1023 goto done;
1025 if (asprintf(&gitconfig,
1026 "[remote \"%s\"]\n"
1027 "\turl = %s\n"
1028 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1029 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1030 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1031 error = got_error_from_errno("asprintf");
1032 goto done;
1034 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1035 if (n != strlen(gitconfig)) {
1036 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1037 goto done;
1040 fpa.last_scaled_size[0] = '\0';
1041 fpa.last_p_indexed = -1;
1042 fpa.last_p_resolved = -1;
1043 fpa.verbosity = verbosity;
1044 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1045 GOT_FETCH_DEFAULT_REMOTE_NAME, fetchfd, repo,
1046 fetch_progress, &fpa);
1047 if (error)
1048 goto done;
1050 error = got_object_id_str(&id_str, pack_hash);
1051 if (error)
1052 goto done;
1053 if (verbosity >= 0)
1054 printf("\nFetched %s.pack\n", id_str);
1055 free(id_str);
1057 /* Set up references provided with the pack file. */
1058 TAILQ_FOREACH(pe, &refs, entry) {
1059 const char *refname = pe->path;
1060 struct got_object_id *id = pe->data;
1061 struct got_reference *ref;
1062 char *remote_refname;
1064 error = got_ref_alloc(&ref, refname, id);
1065 if (error)
1066 goto done;
1067 error = got_ref_write(ref, repo);
1068 got_ref_close(ref);
1069 if (error)
1070 goto done;
1072 if (strncmp("refs/heads/", refname, 11) != 0)
1073 continue;
1075 if (asprintf(&remote_refname,
1076 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1077 refname + 11) == -1) {
1078 error = got_error_from_errno("asprintf");
1079 goto done;
1081 error = got_ref_alloc(&ref, remote_refname, id);
1082 if (error)
1083 goto done;
1084 error = got_ref_write(ref, repo);
1085 got_ref_close(ref);
1086 if (error)
1087 goto done;
1090 /* Set the HEAD reference if the server provided one. */
1091 TAILQ_FOREACH(pe, &symrefs, entry) {
1092 struct got_reference *symref, *target_ref;
1093 const char *refname = pe->path;
1094 const char *target = pe->data;
1096 if (strcmp(refname, GOT_REF_HEAD) != 0)
1097 continue;
1099 error = got_ref_open(&target_ref, repo, target, 0);
1100 if (error) {
1101 if (error->code == GOT_ERR_NOT_REF)
1102 continue;
1103 goto done;
1106 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1107 got_ref_close(target_ref);
1108 if (error)
1109 goto done;
1111 if (verbosity >= 0)
1112 printf("Setting %s to %s\n", GOT_REF_HEAD,
1113 got_ref_get_symref_target(symref));
1115 error = got_ref_write(symref, repo);
1116 got_ref_close(symref);
1117 break;
1120 if (verbosity >= 0)
1121 printf("Created cloned repository '%s'\n", repo_path);
1122 done:
1123 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1124 error = got_error_from_errno("close");
1125 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1126 error = got_error_from_errno("fclose");
1127 if (repo)
1128 got_repo_close(repo);
1129 TAILQ_FOREACH(pe, &refs, entry) {
1130 free((void *)pe->path);
1131 free(pe->data);
1133 got_pathlist_free(&refs);
1134 TAILQ_FOREACH(pe, &symrefs, entry) {
1135 free((void *)pe->path);
1136 free(pe->data);
1138 got_pathlist_free(&symrefs);
1139 free(pack_hash);
1140 free(proto);
1141 free(host);
1142 free(port);
1143 free(server_path);
1144 free(repo_name);
1145 free(default_destdir);
1146 free(gitconfig_path);
1147 free(git_url);
1148 return error;
1151 static const struct got_error *
1152 create_ref(const char *refname, struct got_object_id *id,
1153 const char *id_str, struct got_repository *repo)
1155 const struct got_error *err = NULL;
1156 struct got_reference *ref;
1158 printf("Creating %s: %s\n", refname, id_str);
1160 err = got_ref_alloc(&ref, refname, id);
1161 if (err)
1162 return err;
1164 err = got_ref_write(ref, repo);
1165 got_ref_close(ref);
1166 return err;
1169 static const struct got_error *
1170 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1171 struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 char *new_id_str = NULL;
1175 struct got_object_id *old_id = NULL;
1177 err = got_object_id_str(&new_id_str, new_id);
1178 if (err)
1179 goto done;
1181 if (got_ref_is_symbolic(ref)) {
1182 struct got_reference *new_ref;
1183 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1184 if (err)
1185 goto done;
1186 printf("Deleting symbolic reference %s -> %s\n",
1187 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1188 err = got_ref_delete(ref, repo);
1189 if (err)
1190 goto done;
1191 printf("Setting %s to %s\n", got_ref_get_name(ref),
1192 new_id_str);
1193 err = got_ref_write(new_ref, repo);
1194 if (err)
1195 goto done;
1196 } else {
1197 err = got_ref_resolve(&old_id, repo, ref);
1198 if (err)
1199 goto done;
1200 if (got_object_id_cmp(old_id, new_id) != 0) {
1201 printf("Setting %s to %s\n",
1202 got_ref_get_name(ref), new_id_str);
1203 err = got_ref_change_ref(ref, new_id);
1204 if (err)
1205 goto done;
1206 err = got_ref_write(ref, repo);
1207 if (err)
1208 goto done;
1211 done:
1212 free(old_id);
1213 free(new_id_str);
1214 return err;
1217 __dead static void
1218 usage_fetch(void)
1220 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1221 "[remote-repository-name]\n", getprogname());
1222 exit(1);
1225 static const struct got_error *
1226 cmd_fetch(int argc, char *argv[])
1228 const struct got_error *error = NULL;
1229 char *cwd = NULL, *repo_path = NULL;
1230 const char *remote_name;
1231 char *proto = NULL, *host = NULL, *port = NULL;
1232 char *repo_name = NULL, *server_path = NULL;
1233 struct got_remote_repo *remotes, *remote = NULL;
1234 int nremotes;
1235 char *id_str = NULL;
1236 struct got_repository *repo = NULL;
1237 struct got_worktree *worktree = NULL;
1238 struct got_pathlist_head refs, symrefs;
1239 struct got_pathlist_entry *pe;
1240 struct got_object_id *pack_hash = NULL;
1241 int i, ch, fetchfd = -1;
1242 struct got_fetch_progress_arg fpa;
1243 int verbosity = 0;
1245 TAILQ_INIT(&refs);
1246 TAILQ_INIT(&symrefs);
1248 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1249 switch (ch) {
1250 case 'r':
1251 repo_path = realpath(optarg, NULL);
1252 if (repo_path == NULL)
1253 return got_error_from_errno2("realpath",
1254 optarg);
1255 got_path_strip_trailing_slashes(repo_path);
1256 break;
1257 case 'v':
1258 if (verbosity < 0)
1259 verbosity = 0;
1260 else if (verbosity < 3)
1261 verbosity++;
1262 break;
1263 case 'q':
1264 verbosity = -1;
1265 break;
1266 default:
1267 usage_fetch();
1268 break;
1271 argc -= optind;
1272 argv += optind;
1274 if (argc == 0)
1275 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1276 else if (argc == 1)
1277 remote_name = argv[0];
1278 else
1279 usage_fetch();
1281 cwd = getcwd(NULL, 0);
1282 if (cwd == NULL) {
1283 error = got_error_from_errno("getcwd");
1284 goto done;
1287 if (repo_path == NULL) {
1288 error = got_worktree_open(&worktree, cwd);
1289 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1290 goto done;
1291 else
1292 error = NULL;
1293 if (worktree) {
1294 repo_path =
1295 strdup(got_worktree_get_repo_path(worktree));
1296 if (repo_path == NULL)
1297 error = got_error_from_errno("strdup");
1298 if (error)
1299 goto done;
1300 } else {
1301 repo_path = strdup(cwd);
1302 if (repo_path == NULL) {
1303 error = got_error_from_errno("strdup");
1304 goto done;
1309 error = got_repo_open(&repo, repo_path, NULL);
1310 if (error)
1311 goto done;
1313 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1314 for (i = 0; i < nremotes; i++) {
1315 remote = &remotes[i];
1316 if (strcmp(remote->name, remote_name) == 0)
1317 break;
1319 if (i == nremotes) {
1320 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1321 goto done;
1324 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1325 &repo_name, remote->url);
1326 if (error)
1327 goto done;
1329 if (strcmp(proto, "git") == 0) {
1330 #ifndef PROFILE
1331 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1332 "sendfd dns inet unveil", NULL) == -1)
1333 err(1, "pledge");
1334 #endif
1335 } else if (strcmp(proto, "git+ssh") == 0 ||
1336 strcmp(proto, "ssh") == 0) {
1337 #ifndef PROFILE
1338 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1339 "sendfd unveil", NULL) == -1)
1340 err(1, "pledge");
1341 #endif
1342 } else if (strcmp(proto, "http") == 0 ||
1343 strcmp(proto, "git+http") == 0) {
1344 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1345 goto done;
1346 } else {
1347 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1348 goto done;
1351 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1352 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1353 error = got_error_from_errno2("unveil",
1354 GOT_FETCH_PATH_SSH);
1355 goto done;
1358 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1359 if (error)
1360 goto done;
1362 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1363 verbosity);
1364 if (error)
1365 goto done;
1367 if (verbosity >= 0)
1368 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1370 fpa.last_scaled_size[0] = '\0';
1371 fpa.last_p_indexed = -1;
1372 fpa.last_p_resolved = -1;
1373 fpa.verbosity = verbosity;
1374 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1375 fetchfd, repo, fetch_progress, &fpa);
1376 if (error)
1377 goto done;
1379 if (pack_hash == NULL) {
1380 if (verbosity >= 0)
1381 printf("Already up-to-date\n");
1382 goto done;
1385 if (verbosity >= 0) {
1386 error = got_object_id_str(&id_str, pack_hash);
1387 if (error)
1388 goto done;
1389 printf("\nFetched %s.pack\n", id_str);
1390 free(id_str);
1391 id_str = NULL;
1394 /* Update references provided with the pack file. */
1395 TAILQ_FOREACH(pe, &refs, entry) {
1396 const char *refname = pe->path;
1397 struct got_object_id *id = pe->data;
1398 struct got_reference *ref;
1399 char *remote_refname;
1401 error = got_object_id_str(&id_str, id);
1402 if (error)
1403 goto done;
1405 if (strncmp("refs/tags/", refname, 10) == 0) {
1406 error = got_ref_open(&ref, repo, refname, 0);
1407 if (error) {
1408 if (error->code != GOT_ERR_NOT_REF)
1409 goto done;
1410 error = create_ref(refname, id, id_str, repo);
1411 if (error)
1412 goto done;
1413 } else {
1414 error = update_ref(ref, id, repo);
1415 got_ref_close(ref);
1416 if (error)
1417 goto done;
1419 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1420 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1421 remote_name, refname + 11) == -1) {
1422 error = got_error_from_errno("asprintf");
1423 goto done;
1426 error = got_ref_open(&ref, repo, remote_refname, 0);
1427 if (error) {
1428 if (error->code != GOT_ERR_NOT_REF)
1429 goto done;
1430 error = create_ref(remote_refname, id, id_str,
1431 repo);
1432 if (error)
1433 goto done;
1434 } else {
1435 error = update_ref(ref, id, repo);
1436 got_ref_close(ref);
1437 if (error)
1438 goto done;
1441 free(id_str);
1442 id_str = NULL;
1444 done:
1445 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1446 error = got_error_from_errno("close");
1447 if (repo)
1448 got_repo_close(repo);
1449 if (worktree)
1450 got_worktree_close(worktree);
1451 TAILQ_FOREACH(pe, &refs, entry) {
1452 free((void *)pe->path);
1453 free(pe->data);
1455 got_pathlist_free(&refs);
1456 TAILQ_FOREACH(pe, &symrefs, entry) {
1457 free((void *)pe->path);
1458 free(pe->data);
1460 got_pathlist_free(&symrefs);
1461 free(id_str);
1462 free(cwd);
1463 free(repo_path);
1464 free(pack_hash);
1465 free(proto);
1466 free(host);
1467 free(port);
1468 free(server_path);
1469 free(repo_name);
1470 return error;
1474 __dead static void
1475 usage_checkout(void)
1477 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1478 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1479 exit(1);
1482 static void
1483 show_worktree_base_ref_warning(void)
1485 fprintf(stderr, "%s: warning: could not create a reference "
1486 "to the work tree's base commit; the commit could be "
1487 "garbage-collected by Git; making the repository "
1488 "writable and running 'got update' will prevent this\n",
1489 getprogname());
1492 struct got_checkout_progress_arg {
1493 const char *worktree_path;
1494 int had_base_commit_ref_error;
1497 static const struct got_error *
1498 checkout_progress(void *arg, unsigned char status, const char *path)
1500 struct got_checkout_progress_arg *a = arg;
1502 /* Base commit bump happens silently. */
1503 if (status == GOT_STATUS_BUMP_BASE)
1504 return NULL;
1506 if (status == GOT_STATUS_BASE_REF_ERR) {
1507 a->had_base_commit_ref_error = 1;
1508 return NULL;
1511 while (path[0] == '/')
1512 path++;
1514 printf("%c %s/%s\n", status, a->worktree_path, path);
1515 return NULL;
1518 static const struct got_error *
1519 check_cancelled(void *arg)
1521 if (sigint_received || sigpipe_received)
1522 return got_error(GOT_ERR_CANCELLED);
1523 return NULL;
1526 static const struct got_error *
1527 check_linear_ancestry(struct got_object_id *commit_id,
1528 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1529 struct got_repository *repo)
1531 const struct got_error *err = NULL;
1532 struct got_object_id *yca_id;
1534 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1535 commit_id, base_commit_id, repo, check_cancelled, NULL);
1536 if (err)
1537 return err;
1539 if (yca_id == NULL)
1540 return got_error(GOT_ERR_ANCESTRY);
1543 * Require a straight line of history between the target commit
1544 * and the work tree's base commit.
1546 * Non-linear situations such as this require a rebase:
1548 * (commit) D F (base_commit)
1549 * \ /
1550 * C E
1551 * \ /
1552 * B (yca)
1553 * |
1554 * A
1556 * 'got update' only handles linear cases:
1557 * Update forwards in time: A (base/yca) - B - C - D (commit)
1558 * Update backwards in time: D (base) - C - B - A (commit/yca)
1560 if (allow_forwards_in_time_only) {
1561 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1562 return got_error(GOT_ERR_ANCESTRY);
1563 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1564 got_object_id_cmp(base_commit_id, yca_id) != 0)
1565 return got_error(GOT_ERR_ANCESTRY);
1567 free(yca_id);
1568 return NULL;
1571 static const struct got_error *
1572 check_same_branch(struct got_object_id *commit_id,
1573 struct got_reference *head_ref, struct got_object_id *yca_id,
1574 struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 struct got_commit_graph *graph = NULL;
1578 struct got_object_id *head_commit_id = NULL;
1579 int is_same_branch = 0;
1581 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1582 if (err)
1583 goto done;
1585 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1586 is_same_branch = 1;
1587 goto done;
1589 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1590 is_same_branch = 1;
1591 goto done;
1594 err = got_commit_graph_open(&graph, "/", 1);
1595 if (err)
1596 goto done;
1598 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1599 check_cancelled, NULL);
1600 if (err)
1601 goto done;
1603 for (;;) {
1604 struct got_object_id *id;
1605 err = got_commit_graph_iter_next(&id, graph, repo,
1606 check_cancelled, NULL);
1607 if (err) {
1608 if (err->code == GOT_ERR_ITER_COMPLETED)
1609 err = NULL;
1610 break;
1613 if (id) {
1614 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1615 break;
1616 if (got_object_id_cmp(id, commit_id) == 0) {
1617 is_same_branch = 1;
1618 break;
1622 done:
1623 if (graph)
1624 got_commit_graph_close(graph);
1625 free(head_commit_id);
1626 if (!err && !is_same_branch)
1627 err = got_error(GOT_ERR_ANCESTRY);
1628 return err;
1631 static const struct got_error *
1632 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1634 static char msg[512];
1635 const char *branch_name;
1637 if (got_ref_is_symbolic(ref))
1638 branch_name = got_ref_get_symref_target(ref);
1639 else
1640 branch_name = got_ref_get_name(ref);
1642 if (strncmp("refs/heads/", branch_name, 11) == 0)
1643 branch_name += 11;
1645 snprintf(msg, sizeof(msg),
1646 "target commit is not contained in branch '%s'; "
1647 "the branch to use must be specified with -b; "
1648 "if necessary a new branch can be created for "
1649 "this commit with 'got branch -c %s BRANCH_NAME'",
1650 branch_name, commit_id_str);
1652 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1655 static const struct got_error *
1656 cmd_checkout(int argc, char *argv[])
1658 const struct got_error *error = NULL;
1659 struct got_repository *repo = NULL;
1660 struct got_reference *head_ref = NULL;
1661 struct got_worktree *worktree = NULL;
1662 char *repo_path = NULL;
1663 char *worktree_path = NULL;
1664 const char *path_prefix = "";
1665 const char *branch_name = GOT_REF_HEAD;
1666 char *commit_id_str = NULL;
1667 int ch, same_path_prefix, allow_nonempty = 0;
1668 struct got_pathlist_head paths;
1669 struct got_checkout_progress_arg cpa;
1671 TAILQ_INIT(&paths);
1673 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1674 switch (ch) {
1675 case 'b':
1676 branch_name = optarg;
1677 break;
1678 case 'c':
1679 commit_id_str = strdup(optarg);
1680 if (commit_id_str == NULL)
1681 return got_error_from_errno("strdup");
1682 break;
1683 case 'E':
1684 allow_nonempty = 1;
1685 break;
1686 case 'p':
1687 path_prefix = optarg;
1688 break;
1689 default:
1690 usage_checkout();
1691 /* NOTREACHED */
1695 argc -= optind;
1696 argv += optind;
1698 #ifndef PROFILE
1699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1700 "unveil", NULL) == -1)
1701 err(1, "pledge");
1702 #endif
1703 if (argc == 1) {
1704 char *cwd, *base, *dotgit;
1705 repo_path = realpath(argv[0], NULL);
1706 if (repo_path == NULL)
1707 return got_error_from_errno2("realpath", argv[0]);
1708 cwd = getcwd(NULL, 0);
1709 if (cwd == NULL) {
1710 error = got_error_from_errno("getcwd");
1711 goto done;
1713 if (path_prefix[0]) {
1714 base = basename(path_prefix);
1715 if (base == NULL) {
1716 error = got_error_from_errno2("basename",
1717 path_prefix);
1718 goto done;
1720 } else {
1721 base = basename(repo_path);
1722 if (base == NULL) {
1723 error = got_error_from_errno2("basename",
1724 repo_path);
1725 goto done;
1728 dotgit = strstr(base, ".git");
1729 if (dotgit)
1730 *dotgit = '\0';
1731 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1732 error = got_error_from_errno("asprintf");
1733 free(cwd);
1734 goto done;
1736 free(cwd);
1737 } else if (argc == 2) {
1738 repo_path = realpath(argv[0], NULL);
1739 if (repo_path == NULL) {
1740 error = got_error_from_errno2("realpath", argv[0]);
1741 goto done;
1743 worktree_path = realpath(argv[1], NULL);
1744 if (worktree_path == NULL) {
1745 if (errno != ENOENT) {
1746 error = got_error_from_errno2("realpath",
1747 argv[1]);
1748 goto done;
1750 worktree_path = strdup(argv[1]);
1751 if (worktree_path == NULL) {
1752 error = got_error_from_errno("strdup");
1753 goto done;
1756 } else
1757 usage_checkout();
1759 got_path_strip_trailing_slashes(repo_path);
1760 got_path_strip_trailing_slashes(worktree_path);
1762 error = got_repo_open(&repo, repo_path, NULL);
1763 if (error != NULL)
1764 goto done;
1766 /* Pre-create work tree path for unveil(2) */
1767 error = got_path_mkdir(worktree_path);
1768 if (error) {
1769 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1770 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1771 goto done;
1772 if (!allow_nonempty &&
1773 !got_path_dir_is_empty(worktree_path)) {
1774 error = got_error_path(worktree_path,
1775 GOT_ERR_DIR_NOT_EMPTY);
1776 goto done;
1780 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1781 if (error)
1782 goto done;
1784 error = got_ref_open(&head_ref, repo, branch_name, 0);
1785 if (error != NULL)
1786 goto done;
1788 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1789 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1790 goto done;
1792 error = got_worktree_open(&worktree, worktree_path);
1793 if (error != NULL)
1794 goto done;
1796 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1797 path_prefix);
1798 if (error != NULL)
1799 goto done;
1800 if (!same_path_prefix) {
1801 error = got_error(GOT_ERR_PATH_PREFIX);
1802 goto done;
1805 if (commit_id_str) {
1806 struct got_object_id *commit_id;
1807 error = got_repo_match_object_id(&commit_id, NULL,
1808 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1809 if (error)
1810 goto done;
1811 error = check_linear_ancestry(commit_id,
1812 got_worktree_get_base_commit_id(worktree), 0, repo);
1813 if (error != NULL) {
1814 free(commit_id);
1815 if (error->code == GOT_ERR_ANCESTRY) {
1816 error = checkout_ancestry_error(
1817 head_ref, commit_id_str);
1819 goto done;
1821 error = check_same_branch(commit_id, head_ref, NULL, repo);
1822 if (error) {
1823 if (error->code == GOT_ERR_ANCESTRY) {
1824 error = checkout_ancestry_error(
1825 head_ref, commit_id_str);
1827 goto done;
1829 error = got_worktree_set_base_commit_id(worktree, repo,
1830 commit_id);
1831 free(commit_id);
1832 if (error)
1833 goto done;
1836 error = got_pathlist_append(&paths, "", NULL);
1837 if (error)
1838 goto done;
1839 cpa.worktree_path = worktree_path;
1840 cpa.had_base_commit_ref_error = 0;
1841 error = got_worktree_checkout_files(worktree, &paths, repo,
1842 checkout_progress, &cpa, check_cancelled, NULL);
1843 if (error != NULL)
1844 goto done;
1846 printf("Now shut up and hack\n");
1847 if (cpa.had_base_commit_ref_error)
1848 show_worktree_base_ref_warning();
1849 done:
1850 got_pathlist_free(&paths);
1851 free(commit_id_str);
1852 free(repo_path);
1853 free(worktree_path);
1854 return error;
1857 __dead static void
1858 usage_update(void)
1860 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1861 getprogname());
1862 exit(1);
1865 static const struct got_error *
1866 update_progress(void *arg, unsigned char status, const char *path)
1868 int *did_something = arg;
1870 if (status == GOT_STATUS_EXISTS ||
1871 status == GOT_STATUS_BASE_REF_ERR)
1872 return NULL;
1874 *did_something = 1;
1876 /* Base commit bump happens silently. */
1877 if (status == GOT_STATUS_BUMP_BASE)
1878 return NULL;
1880 while (path[0] == '/')
1881 path++;
1882 printf("%c %s\n", status, path);
1883 return NULL;
1886 static const struct got_error *
1887 switch_head_ref(struct got_reference *head_ref,
1888 struct got_object_id *commit_id, struct got_worktree *worktree,
1889 struct got_repository *repo)
1891 const struct got_error *err = NULL;
1892 char *base_id_str;
1893 int ref_has_moved = 0;
1895 /* Trivial case: switching between two different references. */
1896 if (strcmp(got_ref_get_name(head_ref),
1897 got_worktree_get_head_ref_name(worktree)) != 0) {
1898 printf("Switching work tree from %s to %s\n",
1899 got_worktree_get_head_ref_name(worktree),
1900 got_ref_get_name(head_ref));
1901 return got_worktree_set_head_ref(worktree, head_ref);
1904 err = check_linear_ancestry(commit_id,
1905 got_worktree_get_base_commit_id(worktree), 0, repo);
1906 if (err) {
1907 if (err->code != GOT_ERR_ANCESTRY)
1908 return err;
1909 ref_has_moved = 1;
1911 if (!ref_has_moved)
1912 return NULL;
1914 /* Switching to a rebased branch with the same reference name. */
1915 err = got_object_id_str(&base_id_str,
1916 got_worktree_get_base_commit_id(worktree));
1917 if (err)
1918 return err;
1919 printf("Reference %s now points at a different branch\n",
1920 got_worktree_get_head_ref_name(worktree));
1921 printf("Switching work tree from %s to %s\n", base_id_str,
1922 got_worktree_get_head_ref_name(worktree));
1923 return NULL;
1926 static const struct got_error *
1927 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1929 const struct got_error *err;
1930 int in_progress;
1932 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1933 if (err)
1934 return err;
1935 if (in_progress)
1936 return got_error(GOT_ERR_REBASING);
1938 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1939 if (err)
1940 return err;
1941 if (in_progress)
1942 return got_error(GOT_ERR_HISTEDIT_BUSY);
1944 return NULL;
1947 static const struct got_error *
1948 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1949 char *argv[], struct got_worktree *worktree)
1951 const struct got_error *err = NULL;
1952 char *path;
1953 int i;
1955 if (argc == 0) {
1956 path = strdup("");
1957 if (path == NULL)
1958 return got_error_from_errno("strdup");
1959 return got_pathlist_append(paths, path, NULL);
1962 for (i = 0; i < argc; i++) {
1963 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1964 if (err)
1965 break;
1966 err = got_pathlist_append(paths, path, NULL);
1967 if (err) {
1968 free(path);
1969 break;
1973 return err;
1976 static const struct got_error *
1977 cmd_update(int argc, char *argv[])
1979 const struct got_error *error = NULL;
1980 struct got_repository *repo = NULL;
1981 struct got_worktree *worktree = NULL;
1982 char *worktree_path = NULL;
1983 struct got_object_id *commit_id = NULL;
1984 char *commit_id_str = NULL;
1985 const char *branch_name = NULL;
1986 struct got_reference *head_ref = NULL;
1987 struct got_pathlist_head paths;
1988 struct got_pathlist_entry *pe;
1989 int ch, did_something = 0;
1991 TAILQ_INIT(&paths);
1993 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1994 switch (ch) {
1995 case 'b':
1996 branch_name = optarg;
1997 break;
1998 case 'c':
1999 commit_id_str = strdup(optarg);
2000 if (commit_id_str == NULL)
2001 return got_error_from_errno("strdup");
2002 break;
2003 default:
2004 usage_update();
2005 /* NOTREACHED */
2009 argc -= optind;
2010 argv += optind;
2012 #ifndef PROFILE
2013 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2014 "unveil", NULL) == -1)
2015 err(1, "pledge");
2016 #endif
2017 worktree_path = getcwd(NULL, 0);
2018 if (worktree_path == NULL) {
2019 error = got_error_from_errno("getcwd");
2020 goto done;
2022 error = got_worktree_open(&worktree, worktree_path);
2023 if (error)
2024 goto done;
2026 error = check_rebase_or_histedit_in_progress(worktree);
2027 if (error)
2028 goto done;
2030 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2031 NULL);
2032 if (error != NULL)
2033 goto done;
2035 error = apply_unveil(got_repo_get_path(repo), 0,
2036 got_worktree_get_root_path(worktree));
2037 if (error)
2038 goto done;
2040 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2041 if (error)
2042 goto done;
2044 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2045 got_worktree_get_head_ref_name(worktree), 0);
2046 if (error != NULL)
2047 goto done;
2048 if (commit_id_str == NULL) {
2049 error = got_ref_resolve(&commit_id, repo, head_ref);
2050 if (error != NULL)
2051 goto done;
2052 error = got_object_id_str(&commit_id_str, commit_id);
2053 if (error != NULL)
2054 goto done;
2055 } else {
2056 error = got_repo_match_object_id(&commit_id, NULL,
2057 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2058 free(commit_id_str);
2059 commit_id_str = NULL;
2060 if (error)
2061 goto done;
2062 error = got_object_id_str(&commit_id_str, commit_id);
2063 if (error)
2064 goto done;
2067 if (branch_name) {
2068 struct got_object_id *head_commit_id;
2069 TAILQ_FOREACH(pe, &paths, entry) {
2070 if (pe->path_len == 0)
2071 continue;
2072 error = got_error_msg(GOT_ERR_BAD_PATH,
2073 "switching between branches requires that "
2074 "the entire work tree gets updated");
2075 goto done;
2077 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2078 if (error)
2079 goto done;
2080 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2081 repo);
2082 free(head_commit_id);
2083 if (error != NULL)
2084 goto done;
2085 error = check_same_branch(commit_id, head_ref, NULL, repo);
2086 if (error)
2087 goto done;
2088 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2089 if (error)
2090 goto done;
2091 } else {
2092 error = check_linear_ancestry(commit_id,
2093 got_worktree_get_base_commit_id(worktree), 0, repo);
2094 if (error != NULL) {
2095 if (error->code == GOT_ERR_ANCESTRY)
2096 error = got_error(GOT_ERR_BRANCH_MOVED);
2097 goto done;
2099 error = check_same_branch(commit_id, head_ref, NULL, repo);
2100 if (error)
2101 goto done;
2104 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2105 commit_id) != 0) {
2106 error = got_worktree_set_base_commit_id(worktree, repo,
2107 commit_id);
2108 if (error)
2109 goto done;
2112 error = got_worktree_checkout_files(worktree, &paths, repo,
2113 update_progress, &did_something, check_cancelled, NULL);
2114 if (error != NULL)
2115 goto done;
2117 if (did_something)
2118 printf("Updated to commit %s\n", commit_id_str);
2119 else
2120 printf("Already up-to-date\n");
2121 done:
2122 free(worktree_path);
2123 TAILQ_FOREACH(pe, &paths, entry)
2124 free((char *)pe->path);
2125 got_pathlist_free(&paths);
2126 free(commit_id);
2127 free(commit_id_str);
2128 return error;
2131 static const struct got_error *
2132 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2133 const char *path, int diff_context, int ignore_whitespace,
2134 struct got_repository *repo)
2136 const struct got_error *err = NULL;
2137 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2139 if (blob_id1) {
2140 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2141 if (err)
2142 goto done;
2145 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2146 if (err)
2147 goto done;
2149 while (path[0] == '/')
2150 path++;
2151 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2152 ignore_whitespace, stdout);
2153 done:
2154 if (blob1)
2155 got_object_blob_close(blob1);
2156 got_object_blob_close(blob2);
2157 return err;
2160 static const struct got_error *
2161 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2162 const char *path, int diff_context, int ignore_whitespace,
2163 struct got_repository *repo)
2165 const struct got_error *err = NULL;
2166 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2167 struct got_diff_blob_output_unidiff_arg arg;
2169 if (tree_id1) {
2170 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2171 if (err)
2172 goto done;
2175 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2176 if (err)
2177 goto done;
2179 arg.diff_context = diff_context;
2180 arg.ignore_whitespace = ignore_whitespace;
2181 arg.outfile = stdout;
2182 while (path[0] == '/')
2183 path++;
2184 err = got_diff_tree(tree1, tree2, path, path, repo,
2185 got_diff_blob_output_unidiff, &arg, 1);
2186 done:
2187 if (tree1)
2188 got_object_tree_close(tree1);
2189 if (tree2)
2190 got_object_tree_close(tree2);
2191 return err;
2194 static const struct got_error *
2195 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2196 const char *path, int diff_context, struct got_repository *repo)
2198 const struct got_error *err = NULL;
2199 struct got_commit_object *pcommit = NULL;
2200 char *id_str1 = NULL, *id_str2 = NULL;
2201 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2202 struct got_object_qid *qid;
2204 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2205 if (qid != NULL) {
2206 err = got_object_open_as_commit(&pcommit, repo,
2207 qid->id);
2208 if (err)
2209 return err;
2212 if (path && path[0] != '\0') {
2213 int obj_type;
2214 err = got_object_id_by_path(&obj_id2, repo, id, path);
2215 if (err)
2216 goto done;
2217 err = got_object_id_str(&id_str2, obj_id2);
2218 if (err) {
2219 free(obj_id2);
2220 goto done;
2222 if (pcommit) {
2223 err = got_object_id_by_path(&obj_id1, repo,
2224 qid->id, path);
2225 if (err) {
2226 free(obj_id2);
2227 goto done;
2229 err = got_object_id_str(&id_str1, obj_id1);
2230 if (err) {
2231 free(obj_id2);
2232 goto done;
2235 err = got_object_get_type(&obj_type, repo, obj_id2);
2236 if (err) {
2237 free(obj_id2);
2238 goto done;
2240 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2241 switch (obj_type) {
2242 case GOT_OBJ_TYPE_BLOB:
2243 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2244 0, repo);
2245 break;
2246 case GOT_OBJ_TYPE_TREE:
2247 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2248 0, repo);
2249 break;
2250 default:
2251 err = got_error(GOT_ERR_OBJ_TYPE);
2252 break;
2254 free(obj_id1);
2255 free(obj_id2);
2256 } else {
2257 obj_id2 = got_object_commit_get_tree_id(commit);
2258 err = got_object_id_str(&id_str2, obj_id2);
2259 if (err)
2260 goto done;
2261 obj_id1 = got_object_commit_get_tree_id(pcommit);
2262 err = got_object_id_str(&id_str1, obj_id1);
2263 if (err)
2264 goto done;
2265 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2266 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2268 done:
2269 free(id_str1);
2270 free(id_str2);
2271 if (pcommit)
2272 got_object_commit_close(pcommit);
2273 return err;
2276 static char *
2277 get_datestr(time_t *time, char *datebuf)
2279 struct tm mytm, *tm;
2280 char *p, *s;
2282 tm = gmtime_r(time, &mytm);
2283 if (tm == NULL)
2284 return NULL;
2285 s = asctime_r(tm, datebuf);
2286 if (s == NULL)
2287 return NULL;
2288 p = strchr(s, '\n');
2289 if (p)
2290 *p = '\0';
2291 return s;
2294 static const struct got_error *
2295 match_logmsg(int *have_match, struct got_object_id *id,
2296 struct got_commit_object *commit, regex_t *regex)
2298 const struct got_error *err = NULL;
2299 regmatch_t regmatch;
2300 char *id_str = NULL, *logmsg = NULL;
2302 *have_match = 0;
2304 err = got_object_id_str(&id_str, id);
2305 if (err)
2306 return err;
2308 err = got_object_commit_get_logmsg(&logmsg, commit);
2309 if (err)
2310 goto done;
2312 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2313 *have_match = 1;
2314 done:
2315 free(id_str);
2316 free(logmsg);
2317 return err;
2320 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2322 static const struct got_error *
2323 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2324 struct got_repository *repo, const char *path, int show_patch,
2325 int diff_context, struct got_reflist_head *refs)
2327 const struct got_error *err = NULL;
2328 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2329 char datebuf[26];
2330 time_t committer_time;
2331 const char *author, *committer;
2332 char *refs_str = NULL;
2333 struct got_reflist_entry *re;
2335 SIMPLEQ_FOREACH(re, refs, entry) {
2336 char *s;
2337 const char *name;
2338 struct got_tag_object *tag = NULL;
2339 int cmp;
2341 name = got_ref_get_name(re->ref);
2342 if (strcmp(name, GOT_REF_HEAD) == 0)
2343 continue;
2344 if (strncmp(name, "refs/", 5) == 0)
2345 name += 5;
2346 if (strncmp(name, "got/", 4) == 0)
2347 continue;
2348 if (strncmp(name, "heads/", 6) == 0)
2349 name += 6;
2350 if (strncmp(name, "remotes/", 8) == 0)
2351 name += 8;
2352 if (strncmp(name, "tags/", 5) == 0) {
2353 err = got_object_open_as_tag(&tag, repo, re->id);
2354 if (err) {
2355 if (err->code != GOT_ERR_OBJ_TYPE)
2356 return err;
2357 /* Ref points at something other than a tag. */
2358 err = NULL;
2359 tag = NULL;
2362 cmp = got_object_id_cmp(tag ?
2363 got_object_tag_get_object_id(tag) : re->id, id);
2364 if (tag)
2365 got_object_tag_close(tag);
2366 if (cmp != 0)
2367 continue;
2368 s = refs_str;
2369 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2370 name) == -1) {
2371 err = got_error_from_errno("asprintf");
2372 free(s);
2373 return err;
2375 free(s);
2377 err = got_object_id_str(&id_str, id);
2378 if (err)
2379 return err;
2381 printf(GOT_COMMIT_SEP_STR);
2382 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2383 refs_str ? refs_str : "", refs_str ? ")" : "");
2384 free(id_str);
2385 id_str = NULL;
2386 free(refs_str);
2387 refs_str = NULL;
2388 printf("from: %s\n", got_object_commit_get_author(commit));
2389 committer_time = got_object_commit_get_committer_time(commit);
2390 datestr = get_datestr(&committer_time, datebuf);
2391 if (datestr)
2392 printf("date: %s UTC\n", datestr);
2393 author = got_object_commit_get_author(commit);
2394 committer = got_object_commit_get_committer(commit);
2395 if (strcmp(author, committer) != 0)
2396 printf("via: %s\n", committer);
2397 if (got_object_commit_get_nparents(commit) > 1) {
2398 const struct got_object_id_queue *parent_ids;
2399 struct got_object_qid *qid;
2400 int n = 1;
2401 parent_ids = got_object_commit_get_parent_ids(commit);
2402 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2403 err = got_object_id_str(&id_str, qid->id);
2404 if (err)
2405 return err;
2406 printf("parent %d: %s\n", n++, id_str);
2407 free(id_str);
2411 err = got_object_commit_get_logmsg(&logmsg0, commit);
2412 if (err)
2413 return err;
2415 logmsg = logmsg0;
2416 do {
2417 line = strsep(&logmsg, "\n");
2418 if (line)
2419 printf(" %s\n", line);
2420 } while (line);
2421 free(logmsg0);
2423 if (show_patch) {
2424 err = print_patch(commit, id, path, diff_context, repo);
2425 if (err == 0)
2426 printf("\n");
2429 if (fflush(stdout) != 0 && err == NULL)
2430 err = got_error_from_errno("fflush");
2431 return err;
2434 static const struct got_error *
2435 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2436 const char *path, int show_patch, const char *search_pattern,
2437 int diff_context, int limit, int log_branches,
2438 struct got_reflist_head *refs)
2440 const struct got_error *err;
2441 struct got_commit_graph *graph;
2442 regex_t regex;
2443 int have_match;
2445 if (search_pattern &&
2446 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2447 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2449 err = got_commit_graph_open(&graph, path, !log_branches);
2450 if (err)
2451 return err;
2452 err = got_commit_graph_iter_start(graph, root_id, repo,
2453 check_cancelled, NULL);
2454 if (err)
2455 goto done;
2456 for (;;) {
2457 struct got_commit_object *commit;
2458 struct got_object_id *id;
2460 if (sigint_received || sigpipe_received)
2461 break;
2463 err = got_commit_graph_iter_next(&id, graph, repo,
2464 check_cancelled, NULL);
2465 if (err) {
2466 if (err->code == GOT_ERR_ITER_COMPLETED)
2467 err = NULL;
2468 break;
2470 if (id == NULL)
2471 break;
2473 err = got_object_open_as_commit(&commit, repo, id);
2474 if (err)
2475 break;
2477 if (search_pattern) {
2478 err = match_logmsg(&have_match, id, commit, &regex);
2479 if (err) {
2480 got_object_commit_close(commit);
2481 break;
2483 if (have_match == 0) {
2484 got_object_commit_close(commit);
2485 continue;
2489 err = print_commit(commit, id, repo, path, show_patch,
2490 diff_context, refs);
2491 got_object_commit_close(commit);
2492 if (err || (limit && --limit == 0))
2493 break;
2495 done:
2496 if (search_pattern)
2497 regfree(&regex);
2498 got_commit_graph_close(graph);
2499 return err;
2502 __dead static void
2503 usage_log(void)
2505 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2506 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2507 exit(1);
2510 static int
2511 get_default_log_limit(void)
2513 const char *got_default_log_limit;
2514 long long n;
2515 const char *errstr;
2517 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2518 if (got_default_log_limit == NULL)
2519 return 0;
2520 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2521 if (errstr != NULL)
2522 return 0;
2523 return n;
2526 static const struct got_error *
2527 cmd_log(int argc, char *argv[])
2529 const struct got_error *error;
2530 struct got_repository *repo = NULL;
2531 struct got_worktree *worktree = NULL;
2532 struct got_commit_object *commit = NULL;
2533 struct got_object_id *id = NULL;
2534 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2535 const char *start_commit = NULL, *search_pattern = NULL;
2536 int diff_context = -1, ch;
2537 int show_patch = 0, limit = 0, log_branches = 0;
2538 const char *errstr;
2539 struct got_reflist_head refs;
2541 SIMPLEQ_INIT(&refs);
2543 #ifndef PROFILE
2544 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2545 NULL)
2546 == -1)
2547 err(1, "pledge");
2548 #endif
2550 limit = get_default_log_limit();
2552 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2553 switch (ch) {
2554 case 'p':
2555 show_patch = 1;
2556 break;
2557 case 'c':
2558 start_commit = optarg;
2559 break;
2560 case 'C':
2561 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2562 &errstr);
2563 if (errstr != NULL)
2564 err(1, "-C option %s", errstr);
2565 break;
2566 case 'l':
2567 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2568 if (errstr != NULL)
2569 err(1, "-l option %s", errstr);
2570 break;
2571 case 'b':
2572 log_branches = 1;
2573 break;
2574 case 'r':
2575 repo_path = realpath(optarg, NULL);
2576 if (repo_path == NULL)
2577 return got_error_from_errno2("realpath",
2578 optarg);
2579 got_path_strip_trailing_slashes(repo_path);
2580 break;
2581 case 's':
2582 search_pattern = optarg;
2583 break;
2584 default:
2585 usage_log();
2586 /* NOTREACHED */
2590 argc -= optind;
2591 argv += optind;
2593 if (diff_context == -1)
2594 diff_context = 3;
2595 else if (!show_patch)
2596 errx(1, "-C reguires -p");
2598 cwd = getcwd(NULL, 0);
2599 if (cwd == NULL) {
2600 error = got_error_from_errno("getcwd");
2601 goto done;
2604 error = got_worktree_open(&worktree, cwd);
2605 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2606 goto done;
2607 error = NULL;
2609 if (argc == 0) {
2610 path = strdup("");
2611 if (path == NULL) {
2612 error = got_error_from_errno("strdup");
2613 goto done;
2615 } else if (argc == 1) {
2616 if (worktree) {
2617 error = got_worktree_resolve_path(&path, worktree,
2618 argv[0]);
2619 if (error)
2620 goto done;
2621 } else {
2622 path = strdup(argv[0]);
2623 if (path == NULL) {
2624 error = got_error_from_errno("strdup");
2625 goto done;
2628 } else
2629 usage_log();
2631 if (repo_path == NULL) {
2632 repo_path = worktree ?
2633 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2635 if (repo_path == NULL) {
2636 error = got_error_from_errno("strdup");
2637 goto done;
2640 error = got_repo_open(&repo, repo_path, NULL);
2641 if (error != NULL)
2642 goto done;
2644 error = apply_unveil(got_repo_get_path(repo), 1,
2645 worktree ? got_worktree_get_root_path(worktree) : NULL);
2646 if (error)
2647 goto done;
2649 if (start_commit == NULL) {
2650 struct got_reference *head_ref;
2651 error = got_ref_open(&head_ref, repo,
2652 worktree ? got_worktree_get_head_ref_name(worktree)
2653 : GOT_REF_HEAD, 0);
2654 if (error != NULL)
2655 return error;
2656 error = got_ref_resolve(&id, repo, head_ref);
2657 got_ref_close(head_ref);
2658 if (error != NULL)
2659 return error;
2660 error = got_object_open_as_commit(&commit, repo, id);
2661 } else {
2662 struct got_reference *ref;
2663 error = got_ref_open(&ref, repo, start_commit, 0);
2664 if (error == NULL) {
2665 int obj_type;
2666 error = got_ref_resolve(&id, repo, ref);
2667 got_ref_close(ref);
2668 if (error != NULL)
2669 goto done;
2670 error = got_object_get_type(&obj_type, repo, id);
2671 if (error != NULL)
2672 goto done;
2673 if (obj_type == GOT_OBJ_TYPE_TAG) {
2674 struct got_tag_object *tag;
2675 error = got_object_open_as_tag(&tag, repo, id);
2676 if (error != NULL)
2677 goto done;
2678 if (got_object_tag_get_object_type(tag) !=
2679 GOT_OBJ_TYPE_COMMIT) {
2680 got_object_tag_close(tag);
2681 error = got_error(GOT_ERR_OBJ_TYPE);
2682 goto done;
2684 free(id);
2685 id = got_object_id_dup(
2686 got_object_tag_get_object_id(tag));
2687 if (id == NULL)
2688 error = got_error_from_errno(
2689 "got_object_id_dup");
2690 got_object_tag_close(tag);
2691 if (error)
2692 goto done;
2693 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2694 error = got_error(GOT_ERR_OBJ_TYPE);
2695 goto done;
2697 error = got_object_open_as_commit(&commit, repo, id);
2698 if (error != NULL)
2699 goto done;
2701 if (commit == NULL) {
2702 error = got_repo_match_object_id_prefix(&id,
2703 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2704 if (error != NULL)
2705 return error;
2708 if (error != NULL)
2709 goto done;
2711 if (worktree) {
2712 const char *prefix = got_worktree_get_path_prefix(worktree);
2713 char *p;
2714 if (asprintf(&p, "%s%s%s", prefix,
2715 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2716 error = got_error_from_errno("asprintf");
2717 goto done;
2719 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2720 free(p);
2721 } else
2722 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2723 if (error != NULL)
2724 goto done;
2725 if (in_repo_path) {
2726 free(path);
2727 path = in_repo_path;
2730 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2731 if (error)
2732 goto done;
2734 error = print_commits(id, repo, path, show_patch, search_pattern,
2735 diff_context, limit, log_branches, &refs);
2736 done:
2737 free(path);
2738 free(repo_path);
2739 free(cwd);
2740 free(id);
2741 if (worktree)
2742 got_worktree_close(worktree);
2743 if (repo) {
2744 const struct got_error *repo_error;
2745 repo_error = got_repo_close(repo);
2746 if (error == NULL)
2747 error = repo_error;
2749 got_ref_list_free(&refs);
2750 return error;
2753 __dead static void
2754 usage_diff(void)
2756 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2757 "[-w] [object1 object2 | path]\n", getprogname());
2758 exit(1);
2761 struct print_diff_arg {
2762 struct got_repository *repo;
2763 struct got_worktree *worktree;
2764 int diff_context;
2765 const char *id_str;
2766 int header_shown;
2767 int diff_staged;
2768 int ignore_whitespace;
2771 static const struct got_error *
2772 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2773 const char *path, struct got_object_id *blob_id,
2774 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2775 int dirfd, const char *de_name)
2777 struct print_diff_arg *a = arg;
2778 const struct got_error *err = NULL;
2779 struct got_blob_object *blob1 = NULL;
2780 int fd = -1;
2781 FILE *f2 = NULL;
2782 char *abspath = NULL, *label1 = NULL;
2783 struct stat sb;
2785 if (a->diff_staged) {
2786 if (staged_status != GOT_STATUS_MODIFY &&
2787 staged_status != GOT_STATUS_ADD &&
2788 staged_status != GOT_STATUS_DELETE)
2789 return NULL;
2790 } else {
2791 if (staged_status == GOT_STATUS_DELETE)
2792 return NULL;
2793 if (status == GOT_STATUS_NONEXISTENT)
2794 return got_error_set_errno(ENOENT, path);
2795 if (status != GOT_STATUS_MODIFY &&
2796 status != GOT_STATUS_ADD &&
2797 status != GOT_STATUS_DELETE &&
2798 status != GOT_STATUS_CONFLICT)
2799 return NULL;
2802 if (!a->header_shown) {
2803 printf("diff %s %s%s\n", a->id_str,
2804 got_worktree_get_root_path(a->worktree),
2805 a->diff_staged ? " (staged changes)" : "");
2806 a->header_shown = 1;
2809 if (a->diff_staged) {
2810 const char *label1 = NULL, *label2 = NULL;
2811 switch (staged_status) {
2812 case GOT_STATUS_MODIFY:
2813 label1 = path;
2814 label2 = path;
2815 break;
2816 case GOT_STATUS_ADD:
2817 label2 = path;
2818 break;
2819 case GOT_STATUS_DELETE:
2820 label1 = path;
2821 break;
2822 default:
2823 return got_error(GOT_ERR_FILE_STATUS);
2825 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2826 label1, label2, a->diff_context, a->ignore_whitespace,
2827 a->repo, stdout);
2830 if (staged_status == GOT_STATUS_ADD ||
2831 staged_status == GOT_STATUS_MODIFY) {
2832 char *id_str;
2833 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2834 8192);
2835 if (err)
2836 goto done;
2837 err = got_object_id_str(&id_str, staged_blob_id);
2838 if (err)
2839 goto done;
2840 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2841 err = got_error_from_errno("asprintf");
2842 free(id_str);
2843 goto done;
2845 free(id_str);
2846 } else if (status != GOT_STATUS_ADD) {
2847 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2848 if (err)
2849 goto done;
2852 if (status != GOT_STATUS_DELETE) {
2853 if (asprintf(&abspath, "%s/%s",
2854 got_worktree_get_root_path(a->worktree), path) == -1) {
2855 err = got_error_from_errno("asprintf");
2856 goto done;
2859 if (dirfd != -1) {
2860 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2861 if (fd == -1) {
2862 err = got_error_from_errno2("openat", abspath);
2863 goto done;
2865 } else {
2866 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2867 if (fd == -1) {
2868 err = got_error_from_errno2("open", abspath);
2869 goto done;
2872 if (fstat(fd, &sb) == -1) {
2873 err = got_error_from_errno2("fstat", abspath);
2874 goto done;
2876 f2 = fdopen(fd, "r");
2877 if (f2 == NULL) {
2878 err = got_error_from_errno2("fdopen", abspath);
2879 goto done;
2881 fd = -1;
2882 } else
2883 sb.st_size = 0;
2885 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2886 a->diff_context, a->ignore_whitespace, stdout);
2887 done:
2888 if (blob1)
2889 got_object_blob_close(blob1);
2890 if (f2 && fclose(f2) == EOF && err == NULL)
2891 err = got_error_from_errno("fclose");
2892 if (fd != -1 && close(fd) == -1 && err == NULL)
2893 err = got_error_from_errno("close");
2894 free(abspath);
2895 return err;
2898 static const struct got_error *
2899 cmd_diff(int argc, char *argv[])
2901 const struct got_error *error;
2902 struct got_repository *repo = NULL;
2903 struct got_worktree *worktree = NULL;
2904 char *cwd = NULL, *repo_path = NULL;
2905 struct got_object_id *id1 = NULL, *id2 = NULL;
2906 const char *id_str1 = NULL, *id_str2 = NULL;
2907 char *label1 = NULL, *label2 = NULL;
2908 int type1, type2;
2909 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2910 const char *errstr;
2911 char *path = NULL;
2913 #ifndef PROFILE
2914 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2915 NULL) == -1)
2916 err(1, "pledge");
2917 #endif
2919 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2920 switch (ch) {
2921 case 'C':
2922 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2923 &errstr);
2924 if (errstr != NULL)
2925 err(1, "-C option %s", errstr);
2926 break;
2927 case 'r':
2928 repo_path = realpath(optarg, NULL);
2929 if (repo_path == NULL)
2930 return got_error_from_errno2("realpath",
2931 optarg);
2932 got_path_strip_trailing_slashes(repo_path);
2933 break;
2934 case 's':
2935 diff_staged = 1;
2936 break;
2937 case 'w':
2938 ignore_whitespace = 1;
2939 break;
2940 default:
2941 usage_diff();
2942 /* NOTREACHED */
2946 argc -= optind;
2947 argv += optind;
2949 cwd = getcwd(NULL, 0);
2950 if (cwd == NULL) {
2951 error = got_error_from_errno("getcwd");
2952 goto done;
2954 error = got_worktree_open(&worktree, cwd);
2955 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2956 goto done;
2957 if (argc <= 1) {
2958 if (worktree == NULL) {
2959 error = got_error(GOT_ERR_NOT_WORKTREE);
2960 goto done;
2962 if (repo_path)
2963 errx(1,
2964 "-r option can't be used when diffing a work tree");
2965 repo_path = strdup(got_worktree_get_repo_path(worktree));
2966 if (repo_path == NULL) {
2967 error = got_error_from_errno("strdup");
2968 goto done;
2970 if (argc == 1) {
2971 error = got_worktree_resolve_path(&path, worktree,
2972 argv[0]);
2973 if (error)
2974 goto done;
2975 } else {
2976 path = strdup("");
2977 if (path == NULL) {
2978 error = got_error_from_errno("strdup");
2979 goto done;
2982 } else if (argc == 2) {
2983 if (diff_staged)
2984 errx(1, "-s option can't be used when diffing "
2985 "objects in repository");
2986 id_str1 = argv[0];
2987 id_str2 = argv[1];
2988 if (worktree && repo_path == NULL) {
2989 repo_path =
2990 strdup(got_worktree_get_repo_path(worktree));
2991 if (repo_path == NULL) {
2992 error = got_error_from_errno("strdup");
2993 goto done;
2996 } else
2997 usage_diff();
2999 if (repo_path == NULL) {
3000 repo_path = getcwd(NULL, 0);
3001 if (repo_path == NULL)
3002 return got_error_from_errno("getcwd");
3005 error = got_repo_open(&repo, repo_path, NULL);
3006 free(repo_path);
3007 if (error != NULL)
3008 goto done;
3010 error = apply_unveil(got_repo_get_path(repo), 1,
3011 worktree ? got_worktree_get_root_path(worktree) : NULL);
3012 if (error)
3013 goto done;
3015 if (argc <= 1) {
3016 struct print_diff_arg arg;
3017 struct got_pathlist_head paths;
3018 char *id_str;
3020 TAILQ_INIT(&paths);
3022 error = got_object_id_str(&id_str,
3023 got_worktree_get_base_commit_id(worktree));
3024 if (error)
3025 goto done;
3026 arg.repo = repo;
3027 arg.worktree = worktree;
3028 arg.diff_context = diff_context;
3029 arg.id_str = id_str;
3030 arg.header_shown = 0;
3031 arg.diff_staged = diff_staged;
3032 arg.ignore_whitespace = ignore_whitespace;
3034 error = got_pathlist_append(&paths, path, NULL);
3035 if (error)
3036 goto done;
3038 error = got_worktree_status(worktree, &paths, repo, print_diff,
3039 &arg, check_cancelled, NULL);
3040 free(id_str);
3041 got_pathlist_free(&paths);
3042 goto done;
3045 error = got_repo_match_object_id(&id1, &label1, id_str1,
3046 GOT_OBJ_TYPE_ANY, 1, repo);
3047 if (error)
3048 goto done;
3050 error = got_repo_match_object_id(&id2, &label2, id_str2,
3051 GOT_OBJ_TYPE_ANY, 1, repo);
3052 if (error)
3053 goto done;
3055 error = got_object_get_type(&type1, repo, id1);
3056 if (error)
3057 goto done;
3059 error = got_object_get_type(&type2, repo, id2);
3060 if (error)
3061 goto done;
3063 if (type1 != type2) {
3064 error = got_error(GOT_ERR_OBJ_TYPE);
3065 goto done;
3068 switch (type1) {
3069 case GOT_OBJ_TYPE_BLOB:
3070 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3071 diff_context, ignore_whitespace, repo, stdout);
3072 break;
3073 case GOT_OBJ_TYPE_TREE:
3074 error = got_diff_objects_as_trees(id1, id2, "", "",
3075 diff_context, ignore_whitespace, repo, stdout);
3076 break;
3077 case GOT_OBJ_TYPE_COMMIT:
3078 printf("diff %s %s\n", label1, label2);
3079 error = got_diff_objects_as_commits(id1, id2, diff_context,
3080 ignore_whitespace, repo, stdout);
3081 break;
3082 default:
3083 error = got_error(GOT_ERR_OBJ_TYPE);
3085 done:
3086 free(label1);
3087 free(label2);
3088 free(id1);
3089 free(id2);
3090 free(path);
3091 if (worktree)
3092 got_worktree_close(worktree);
3093 if (repo) {
3094 const struct got_error *repo_error;
3095 repo_error = got_repo_close(repo);
3096 if (error == NULL)
3097 error = repo_error;
3099 return error;
3102 __dead static void
3103 usage_blame(void)
3105 fprintf(stderr,
3106 "usage: %s blame [-c commit] [-r repository-path] path\n",
3107 getprogname());
3108 exit(1);
3111 struct blame_line {
3112 int annotated;
3113 char *id_str;
3114 char *committer;
3115 char datebuf[11]; /* YYYY-MM-DD + NUL */
3118 struct blame_cb_args {
3119 struct blame_line *lines;
3120 int nlines;
3121 int nlines_prec;
3122 int lineno_cur;
3123 off_t *line_offsets;
3124 FILE *f;
3125 struct got_repository *repo;
3128 static const struct got_error *
3129 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3131 const struct got_error *err = NULL;
3132 struct blame_cb_args *a = arg;
3133 struct blame_line *bline;
3134 char *line = NULL;
3135 size_t linesize = 0;
3136 struct got_commit_object *commit = NULL;
3137 off_t offset;
3138 struct tm tm;
3139 time_t committer_time;
3141 if (nlines != a->nlines ||
3142 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3143 return got_error(GOT_ERR_RANGE);
3145 if (sigint_received)
3146 return got_error(GOT_ERR_ITER_COMPLETED);
3148 if (lineno == -1)
3149 return NULL; /* no change in this commit */
3151 /* Annotate this line. */
3152 bline = &a->lines[lineno - 1];
3153 if (bline->annotated)
3154 return NULL;
3155 err = got_object_id_str(&bline->id_str, id);
3156 if (err)
3157 return err;
3159 err = got_object_open_as_commit(&commit, a->repo, id);
3160 if (err)
3161 goto done;
3163 bline->committer = strdup(got_object_commit_get_committer(commit));
3164 if (bline->committer == NULL) {
3165 err = got_error_from_errno("strdup");
3166 goto done;
3169 committer_time = got_object_commit_get_committer_time(commit);
3170 if (localtime_r(&committer_time, &tm) == NULL)
3171 return got_error_from_errno("localtime_r");
3172 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3173 &tm) >= sizeof(bline->datebuf)) {
3174 err = got_error(GOT_ERR_NO_SPACE);
3175 goto done;
3177 bline->annotated = 1;
3179 /* Print lines annotated so far. */
3180 bline = &a->lines[a->lineno_cur - 1];
3181 if (!bline->annotated)
3182 goto done;
3184 offset = a->line_offsets[a->lineno_cur - 1];
3185 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3186 err = got_error_from_errno("fseeko");
3187 goto done;
3190 while (bline->annotated) {
3191 char *smallerthan, *at, *nl, *committer;
3192 size_t len;
3194 if (getline(&line, &linesize, a->f) == -1) {
3195 if (ferror(a->f))
3196 err = got_error_from_errno("getline");
3197 break;
3200 committer = bline->committer;
3201 smallerthan = strchr(committer, '<');
3202 if (smallerthan && smallerthan[1] != '\0')
3203 committer = smallerthan + 1;
3204 at = strchr(committer, '@');
3205 if (at)
3206 *at = '\0';
3207 len = strlen(committer);
3208 if (len >= 9)
3209 committer[8] = '\0';
3211 nl = strchr(line, '\n');
3212 if (nl)
3213 *nl = '\0';
3214 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3215 bline->id_str, bline->datebuf, committer, line);
3217 a->lineno_cur++;
3218 bline = &a->lines[a->lineno_cur - 1];
3220 done:
3221 if (commit)
3222 got_object_commit_close(commit);
3223 free(line);
3224 return err;
3227 static const struct got_error *
3228 cmd_blame(int argc, char *argv[])
3230 const struct got_error *error;
3231 struct got_repository *repo = NULL;
3232 struct got_worktree *worktree = NULL;
3233 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3234 struct got_object_id *obj_id = NULL;
3235 struct got_object_id *commit_id = NULL;
3236 struct got_blob_object *blob = NULL;
3237 char *commit_id_str = NULL;
3238 struct blame_cb_args bca;
3239 int ch, obj_type, i;
3240 size_t filesize;
3242 memset(&bca, 0, sizeof(bca));
3244 #ifndef PROFILE
3245 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3246 NULL) == -1)
3247 err(1, "pledge");
3248 #endif
3250 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3251 switch (ch) {
3252 case 'c':
3253 commit_id_str = optarg;
3254 break;
3255 case 'r':
3256 repo_path = realpath(optarg, NULL);
3257 if (repo_path == NULL)
3258 return got_error_from_errno2("realpath",
3259 optarg);
3260 got_path_strip_trailing_slashes(repo_path);
3261 break;
3262 default:
3263 usage_blame();
3264 /* NOTREACHED */
3268 argc -= optind;
3269 argv += optind;
3271 if (argc == 1)
3272 path = argv[0];
3273 else
3274 usage_blame();
3276 cwd = getcwd(NULL, 0);
3277 if (cwd == NULL) {
3278 error = got_error_from_errno("getcwd");
3279 goto done;
3281 if (repo_path == NULL) {
3282 error = got_worktree_open(&worktree, cwd);
3283 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3284 goto done;
3285 else
3286 error = NULL;
3287 if (worktree) {
3288 repo_path =
3289 strdup(got_worktree_get_repo_path(worktree));
3290 if (repo_path == NULL)
3291 error = got_error_from_errno("strdup");
3292 if (error)
3293 goto done;
3294 } else {
3295 repo_path = strdup(cwd);
3296 if (repo_path == NULL) {
3297 error = got_error_from_errno("strdup");
3298 goto done;
3303 error = got_repo_open(&repo, repo_path, NULL);
3304 if (error != NULL)
3305 goto done;
3307 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3308 if (error)
3309 goto done;
3311 if (worktree) {
3312 const char *prefix = got_worktree_get_path_prefix(worktree);
3313 char *p, *worktree_subdir = cwd +
3314 strlen(got_worktree_get_root_path(worktree));
3315 if (asprintf(&p, "%s%s%s%s%s",
3316 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3317 worktree_subdir, worktree_subdir[0] ? "/" : "",
3318 path) == -1) {
3319 error = got_error_from_errno("asprintf");
3320 goto done;
3322 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3323 free(p);
3324 } else {
3325 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3327 if (error)
3328 goto done;
3330 if (commit_id_str == NULL) {
3331 struct got_reference *head_ref;
3332 error = got_ref_open(&head_ref, repo, worktree ?
3333 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3334 if (error != NULL)
3335 goto done;
3336 error = got_ref_resolve(&commit_id, repo, head_ref);
3337 got_ref_close(head_ref);
3338 if (error != NULL)
3339 goto done;
3340 } else {
3341 error = got_repo_match_object_id(&commit_id, NULL,
3342 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3343 if (error)
3344 goto done;
3347 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3348 if (error)
3349 goto done;
3351 error = got_object_get_type(&obj_type, repo, obj_id);
3352 if (error)
3353 goto done;
3355 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3356 error = got_error(GOT_ERR_OBJ_TYPE);
3357 goto done;
3360 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3361 if (error)
3362 goto done;
3363 bca.f = got_opentemp();
3364 if (bca.f == NULL) {
3365 error = got_error_from_errno("got_opentemp");
3366 goto done;
3368 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3369 &bca.line_offsets, bca.f, blob);
3370 if (error || bca.nlines == 0)
3371 goto done;
3373 /* Don't include \n at EOF in the blame line count. */
3374 if (bca.line_offsets[bca.nlines - 1] == filesize)
3375 bca.nlines--;
3377 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3378 if (bca.lines == NULL) {
3379 error = got_error_from_errno("calloc");
3380 goto done;
3382 bca.lineno_cur = 1;
3383 bca.nlines_prec = 0;
3384 i = bca.nlines;
3385 while (i > 0) {
3386 i /= 10;
3387 bca.nlines_prec++;
3389 bca.repo = repo;
3391 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3392 check_cancelled, NULL);
3393 done:
3394 free(in_repo_path);
3395 free(repo_path);
3396 free(cwd);
3397 free(commit_id);
3398 free(obj_id);
3399 if (blob)
3400 got_object_blob_close(blob);
3401 if (worktree)
3402 got_worktree_close(worktree);
3403 if (repo) {
3404 const struct got_error *repo_error;
3405 repo_error = got_repo_close(repo);
3406 if (error == NULL)
3407 error = repo_error;
3409 if (bca.lines) {
3410 for (i = 0; i < bca.nlines; i++) {
3411 struct blame_line *bline = &bca.lines[i];
3412 free(bline->id_str);
3413 free(bline->committer);
3415 free(bca.lines);
3417 free(bca.line_offsets);
3418 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3419 error = got_error_from_errno("fclose");
3420 return error;
3423 __dead static void
3424 usage_tree(void)
3426 fprintf(stderr,
3427 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3428 getprogname());
3429 exit(1);
3432 static void
3433 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3434 const char *root_path)
3436 int is_root_path = (strcmp(path, root_path) == 0);
3437 const char *modestr = "";
3438 mode_t mode = got_tree_entry_get_mode(te);
3440 path += strlen(root_path);
3441 while (path[0] == '/')
3442 path++;
3444 if (got_object_tree_entry_is_submodule(te))
3445 modestr = "$";
3446 else if (S_ISLNK(mode))
3447 modestr = "@";
3448 else if (S_ISDIR(mode))
3449 modestr = "/";
3450 else if (mode & S_IXUSR)
3451 modestr = "*";
3453 printf("%s%s%s%s%s\n", id ? id : "", path,
3454 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3457 static const struct got_error *
3458 print_tree(const char *path, struct got_object_id *commit_id,
3459 int show_ids, int recurse, const char *root_path,
3460 struct got_repository *repo)
3462 const struct got_error *err = NULL;
3463 struct got_object_id *tree_id = NULL;
3464 struct got_tree_object *tree = NULL;
3465 int nentries, i;
3467 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3468 if (err)
3469 goto done;
3471 err = got_object_open_as_tree(&tree, repo, tree_id);
3472 if (err)
3473 goto done;
3474 nentries = got_object_tree_get_nentries(tree);
3475 for (i = 0; i < nentries; i++) {
3476 struct got_tree_entry *te;
3477 char *id = NULL;
3479 if (sigint_received || sigpipe_received)
3480 break;
3482 te = got_object_tree_get_entry(tree, i);
3483 if (show_ids) {
3484 char *id_str;
3485 err = got_object_id_str(&id_str,
3486 got_tree_entry_get_id(te));
3487 if (err)
3488 goto done;
3489 if (asprintf(&id, "%s ", id_str) == -1) {
3490 err = got_error_from_errno("asprintf");
3491 free(id_str);
3492 goto done;
3494 free(id_str);
3496 print_entry(te, id, path, root_path);
3497 free(id);
3499 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3500 char *child_path;
3501 if (asprintf(&child_path, "%s%s%s", path,
3502 path[0] == '/' && path[1] == '\0' ? "" : "/",
3503 got_tree_entry_get_name(te)) == -1) {
3504 err = got_error_from_errno("asprintf");
3505 goto done;
3507 err = print_tree(child_path, commit_id, show_ids, 1,
3508 root_path, repo);
3509 free(child_path);
3510 if (err)
3511 goto done;
3514 done:
3515 if (tree)
3516 got_object_tree_close(tree);
3517 free(tree_id);
3518 return err;
3521 static const struct got_error *
3522 cmd_tree(int argc, char *argv[])
3524 const struct got_error *error;
3525 struct got_repository *repo = NULL;
3526 struct got_worktree *worktree = NULL;
3527 const char *path;
3528 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3529 struct got_object_id *commit_id = NULL;
3530 char *commit_id_str = NULL;
3531 int show_ids = 0, recurse = 0;
3532 int ch;
3534 #ifndef PROFILE
3535 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3536 NULL) == -1)
3537 err(1, "pledge");
3538 #endif
3540 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3541 switch (ch) {
3542 case 'c':
3543 commit_id_str = optarg;
3544 break;
3545 case 'r':
3546 repo_path = realpath(optarg, NULL);
3547 if (repo_path == NULL)
3548 return got_error_from_errno2("realpath",
3549 optarg);
3550 got_path_strip_trailing_slashes(repo_path);
3551 break;
3552 case 'i':
3553 show_ids = 1;
3554 break;
3555 case 'R':
3556 recurse = 1;
3557 break;
3558 default:
3559 usage_tree();
3560 /* NOTREACHED */
3564 argc -= optind;
3565 argv += optind;
3567 if (argc == 1)
3568 path = argv[0];
3569 else if (argc > 1)
3570 usage_tree();
3571 else
3572 path = NULL;
3574 cwd = getcwd(NULL, 0);
3575 if (cwd == NULL) {
3576 error = got_error_from_errno("getcwd");
3577 goto done;
3579 if (repo_path == NULL) {
3580 error = got_worktree_open(&worktree, cwd);
3581 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3582 goto done;
3583 else
3584 error = NULL;
3585 if (worktree) {
3586 repo_path =
3587 strdup(got_worktree_get_repo_path(worktree));
3588 if (repo_path == NULL)
3589 error = got_error_from_errno("strdup");
3590 if (error)
3591 goto done;
3592 } else {
3593 repo_path = strdup(cwd);
3594 if (repo_path == NULL) {
3595 error = got_error_from_errno("strdup");
3596 goto done;
3601 error = got_repo_open(&repo, repo_path, NULL);
3602 if (error != NULL)
3603 goto done;
3605 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3606 if (error)
3607 goto done;
3609 if (path == NULL) {
3610 if (worktree) {
3611 char *p, *worktree_subdir = cwd +
3612 strlen(got_worktree_get_root_path(worktree));
3613 if (asprintf(&p, "%s/%s",
3614 got_worktree_get_path_prefix(worktree),
3615 worktree_subdir) == -1) {
3616 error = got_error_from_errno("asprintf");
3617 goto done;
3619 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3620 free(p);
3621 if (error)
3622 goto done;
3623 } else
3624 path = "/";
3626 if (in_repo_path == NULL) {
3627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3628 if (error != NULL)
3629 goto done;
3632 if (commit_id_str == NULL) {
3633 struct got_reference *head_ref;
3634 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3635 if (error != NULL)
3636 goto done;
3637 error = got_ref_resolve(&commit_id, repo, head_ref);
3638 got_ref_close(head_ref);
3639 if (error != NULL)
3640 goto done;
3641 } else {
3642 error = got_repo_match_object_id(&commit_id, NULL,
3643 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3644 if (error)
3645 goto done;
3648 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3649 in_repo_path, repo);
3650 done:
3651 free(in_repo_path);
3652 free(repo_path);
3653 free(cwd);
3654 free(commit_id);
3655 if (worktree)
3656 got_worktree_close(worktree);
3657 if (repo) {
3658 const struct got_error *repo_error;
3659 repo_error = got_repo_close(repo);
3660 if (error == NULL)
3661 error = repo_error;
3663 return error;
3666 __dead static void
3667 usage_status(void)
3669 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3670 exit(1);
3673 static const struct got_error *
3674 print_status(void *arg, unsigned char status, unsigned char staged_status,
3675 const char *path, struct got_object_id *blob_id,
3676 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3677 int dirfd, const char *de_name)
3679 if (status == staged_status && (status == GOT_STATUS_DELETE))
3680 status = GOT_STATUS_NO_CHANGE;
3681 printf("%c%c %s\n", status, staged_status, path);
3682 return NULL;
3685 static const struct got_error *
3686 cmd_status(int argc, char *argv[])
3688 const struct got_error *error = NULL;
3689 struct got_repository *repo = NULL;
3690 struct got_worktree *worktree = NULL;
3691 char *cwd = NULL;
3692 struct got_pathlist_head paths;
3693 struct got_pathlist_entry *pe;
3694 int ch;
3696 TAILQ_INIT(&paths);
3698 while ((ch = getopt(argc, argv, "")) != -1) {
3699 switch (ch) {
3700 default:
3701 usage_status();
3702 /* NOTREACHED */
3706 argc -= optind;
3707 argv += optind;
3709 #ifndef PROFILE
3710 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3711 NULL) == -1)
3712 err(1, "pledge");
3713 #endif
3714 cwd = getcwd(NULL, 0);
3715 if (cwd == NULL) {
3716 error = got_error_from_errno("getcwd");
3717 goto done;
3720 error = got_worktree_open(&worktree, cwd);
3721 if (error != NULL)
3722 goto done;
3724 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3725 NULL);
3726 if (error != NULL)
3727 goto done;
3729 error = apply_unveil(got_repo_get_path(repo), 1,
3730 got_worktree_get_root_path(worktree));
3731 if (error)
3732 goto done;
3734 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3735 if (error)
3736 goto done;
3738 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3739 check_cancelled, NULL);
3740 done:
3741 TAILQ_FOREACH(pe, &paths, entry)
3742 free((char *)pe->path);
3743 got_pathlist_free(&paths);
3744 free(cwd);
3745 return error;
3748 __dead static void
3749 usage_ref(void)
3751 fprintf(stderr,
3752 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3753 getprogname());
3754 exit(1);
3757 static const struct got_error *
3758 list_refs(struct got_repository *repo)
3760 static const struct got_error *err = NULL;
3761 struct got_reflist_head refs;
3762 struct got_reflist_entry *re;
3764 SIMPLEQ_INIT(&refs);
3765 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3766 if (err)
3767 return err;
3769 SIMPLEQ_FOREACH(re, &refs, entry) {
3770 char *refstr;
3771 refstr = got_ref_to_str(re->ref);
3772 if (refstr == NULL)
3773 return got_error_from_errno("got_ref_to_str");
3774 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3775 free(refstr);
3778 got_ref_list_free(&refs);
3779 return NULL;
3782 static const struct got_error *
3783 delete_ref(struct got_repository *repo, const char *refname)
3785 const struct got_error *err = NULL;
3786 struct got_reference *ref;
3788 err = got_ref_open(&ref, repo, refname, 0);
3789 if (err)
3790 return err;
3792 err = got_ref_delete(ref, repo);
3793 got_ref_close(ref);
3794 return err;
3797 static const struct got_error *
3798 add_ref(struct got_repository *repo, const char *refname, const char *target)
3800 const struct got_error *err = NULL;
3801 struct got_object_id *id;
3802 struct got_reference *ref = NULL;
3805 * Don't let the user create a reference name with a leading '-'.
3806 * While technically a valid reference name, this case is usually
3807 * an unintended typo.
3809 if (refname[0] == '-')
3810 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3812 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3813 repo);
3814 if (err) {
3815 struct got_reference *target_ref;
3817 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3818 return err;
3819 err = got_ref_open(&target_ref, repo, target, 0);
3820 if (err)
3821 return err;
3822 err = got_ref_resolve(&id, repo, target_ref);
3823 got_ref_close(target_ref);
3824 if (err)
3825 return err;
3828 err = got_ref_alloc(&ref, refname, id);
3829 if (err)
3830 goto done;
3832 err = got_ref_write(ref, repo);
3833 done:
3834 if (ref)
3835 got_ref_close(ref);
3836 free(id);
3837 return err;
3840 static const struct got_error *
3841 add_symref(struct got_repository *repo, const char *refname, const char *target)
3843 const struct got_error *err = NULL;
3844 struct got_reference *ref = NULL;
3845 struct got_reference *target_ref = NULL;
3848 * Don't let the user create a reference name with a leading '-'.
3849 * While technically a valid reference name, this case is usually
3850 * an unintended typo.
3852 if (refname[0] == '-')
3853 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3855 err = got_ref_open(&target_ref, repo, target, 0);
3856 if (err)
3857 return err;
3859 err = got_ref_alloc_symref(&ref, refname, target_ref);
3860 if (err)
3861 goto done;
3863 err = got_ref_write(ref, repo);
3864 done:
3865 if (target_ref)
3866 got_ref_close(target_ref);
3867 if (ref)
3868 got_ref_close(ref);
3869 return err;
3872 static const struct got_error *
3873 cmd_ref(int argc, char *argv[])
3875 const struct got_error *error = NULL;
3876 struct got_repository *repo = NULL;
3877 struct got_worktree *worktree = NULL;
3878 char *cwd = NULL, *repo_path = NULL;
3879 int ch, do_list = 0, create_symref = 0;
3880 const char *delref = NULL;
3882 /* TODO: Add -s option for adding symbolic references. */
3883 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3884 switch (ch) {
3885 case 'd':
3886 delref = optarg;
3887 break;
3888 case 'r':
3889 repo_path = realpath(optarg, NULL);
3890 if (repo_path == NULL)
3891 return got_error_from_errno2("realpath",
3892 optarg);
3893 got_path_strip_trailing_slashes(repo_path);
3894 break;
3895 case 'l':
3896 do_list = 1;
3897 break;
3898 case 's':
3899 create_symref = 1;
3900 break;
3901 default:
3902 usage_ref();
3903 /* NOTREACHED */
3907 if (do_list && delref)
3908 errx(1, "-l and -d options are mutually exclusive\n");
3910 argc -= optind;
3911 argv += optind;
3913 if (do_list || delref) {
3914 if (create_symref)
3915 errx(1, "-s option cannot be used together with the "
3916 "-l or -d options");
3917 if (argc > 0)
3918 usage_ref();
3919 } else if (argc != 2)
3920 usage_ref();
3922 #ifndef PROFILE
3923 if (do_list) {
3924 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3925 NULL) == -1)
3926 err(1, "pledge");
3927 } else {
3928 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3929 "sendfd unveil", NULL) == -1)
3930 err(1, "pledge");
3932 #endif
3933 cwd = getcwd(NULL, 0);
3934 if (cwd == NULL) {
3935 error = got_error_from_errno("getcwd");
3936 goto done;
3939 if (repo_path == NULL) {
3940 error = got_worktree_open(&worktree, cwd);
3941 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3942 goto done;
3943 else
3944 error = NULL;
3945 if (worktree) {
3946 repo_path =
3947 strdup(got_worktree_get_repo_path(worktree));
3948 if (repo_path == NULL)
3949 error = got_error_from_errno("strdup");
3950 if (error)
3951 goto done;
3952 } else {
3953 repo_path = strdup(cwd);
3954 if (repo_path == NULL) {
3955 error = got_error_from_errno("strdup");
3956 goto done;
3961 error = got_repo_open(&repo, repo_path, NULL);
3962 if (error != NULL)
3963 goto done;
3965 error = apply_unveil(got_repo_get_path(repo), do_list,
3966 worktree ? got_worktree_get_root_path(worktree) : NULL);
3967 if (error)
3968 goto done;
3970 if (do_list)
3971 error = list_refs(repo);
3972 else if (delref)
3973 error = delete_ref(repo, delref);
3974 else if (create_symref)
3975 error = add_symref(repo, argv[0], argv[1]);
3976 else
3977 error = add_ref(repo, argv[0], argv[1]);
3978 done:
3979 if (repo)
3980 got_repo_close(repo);
3981 if (worktree)
3982 got_worktree_close(worktree);
3983 free(cwd);
3984 free(repo_path);
3985 return error;
3988 __dead static void
3989 usage_branch(void)
3991 fprintf(stderr,
3992 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3993 "[name]\n", getprogname());
3994 exit(1);
3997 static const struct got_error *
3998 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3999 struct got_reference *ref)
4001 const struct got_error *err = NULL;
4002 const char *refname, *marker = " ";
4003 char *refstr;
4005 refname = got_ref_get_name(ref);
4006 if (worktree && strcmp(refname,
4007 got_worktree_get_head_ref_name(worktree)) == 0) {
4008 struct got_object_id *id = NULL;
4010 err = got_ref_resolve(&id, repo, ref);
4011 if (err)
4012 return err;
4013 if (got_object_id_cmp(id,
4014 got_worktree_get_base_commit_id(worktree)) == 0)
4015 marker = "* ";
4016 else
4017 marker = "~ ";
4018 free(id);
4021 if (strncmp(refname, "refs/heads/", 11) == 0)
4022 refname += 11;
4023 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4024 refname += 18;
4026 refstr = got_ref_to_str(ref);
4027 if (refstr == NULL)
4028 return got_error_from_errno("got_ref_to_str");
4030 printf("%s%s: %s\n", marker, refname, refstr);
4031 free(refstr);
4032 return NULL;
4035 static const struct got_error *
4036 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4038 const char *refname;
4040 if (worktree == NULL)
4041 return got_error(GOT_ERR_NOT_WORKTREE);
4043 refname = got_worktree_get_head_ref_name(worktree);
4045 if (strncmp(refname, "refs/heads/", 11) == 0)
4046 refname += 11;
4047 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4048 refname += 18;
4050 printf("%s\n", refname);
4052 return NULL;
4055 static const struct got_error *
4056 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4058 static const struct got_error *err = NULL;
4059 struct got_reflist_head refs;
4060 struct got_reflist_entry *re;
4061 struct got_reference *temp_ref = NULL;
4062 int rebase_in_progress, histedit_in_progress;
4064 SIMPLEQ_INIT(&refs);
4066 if (worktree) {
4067 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4068 worktree);
4069 if (err)
4070 return err;
4072 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4073 worktree);
4074 if (err)
4075 return err;
4077 if (rebase_in_progress || histedit_in_progress) {
4078 err = got_ref_open(&temp_ref, repo,
4079 got_worktree_get_head_ref_name(worktree), 0);
4080 if (err)
4081 return err;
4082 list_branch(repo, worktree, temp_ref);
4083 got_ref_close(temp_ref);
4087 err = got_ref_list(&refs, repo, "refs/heads",
4088 got_ref_cmp_by_name, NULL);
4089 if (err)
4090 return err;
4092 SIMPLEQ_FOREACH(re, &refs, entry)
4093 list_branch(repo, worktree, re->ref);
4095 got_ref_list_free(&refs);
4096 return NULL;
4099 static const struct got_error *
4100 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4101 const char *branch_name)
4103 const struct got_error *err = NULL;
4104 struct got_reference *ref = NULL;
4105 char *refname;
4107 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4108 return got_error_from_errno("asprintf");
4110 err = got_ref_open(&ref, repo, refname, 0);
4111 if (err)
4112 goto done;
4114 if (worktree &&
4115 strcmp(got_worktree_get_head_ref_name(worktree),
4116 got_ref_get_name(ref)) == 0) {
4117 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4118 "will not delete this work tree's current branch");
4119 goto done;
4122 err = got_ref_delete(ref, repo);
4123 done:
4124 if (ref)
4125 got_ref_close(ref);
4126 free(refname);
4127 return err;
4130 static const struct got_error *
4131 add_branch(struct got_repository *repo, const char *branch_name,
4132 struct got_object_id *base_commit_id)
4134 const struct got_error *err = NULL;
4135 struct got_reference *ref = NULL;
4136 char *base_refname = NULL, *refname = NULL;
4139 * Don't let the user create a branch name with a leading '-'.
4140 * While technically a valid reference name, this case is usually
4141 * an unintended typo.
4143 if (branch_name[0] == '-')
4144 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4146 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4147 err = got_error_from_errno("asprintf");
4148 goto done;
4151 err = got_ref_open(&ref, repo, refname, 0);
4152 if (err == NULL) {
4153 err = got_error(GOT_ERR_BRANCH_EXISTS);
4154 goto done;
4155 } else if (err->code != GOT_ERR_NOT_REF)
4156 goto done;
4158 err = got_ref_alloc(&ref, refname, base_commit_id);
4159 if (err)
4160 goto done;
4162 err = got_ref_write(ref, repo);
4163 done:
4164 if (ref)
4165 got_ref_close(ref);
4166 free(base_refname);
4167 free(refname);
4168 return err;
4171 static const struct got_error *
4172 cmd_branch(int argc, char *argv[])
4174 const struct got_error *error = NULL;
4175 struct got_repository *repo = NULL;
4176 struct got_worktree *worktree = NULL;
4177 char *cwd = NULL, *repo_path = NULL;
4178 int ch, do_list = 0, do_show = 0, do_update = 1;
4179 const char *delref = NULL, *commit_id_arg = NULL;
4180 struct got_reference *ref = NULL;
4181 struct got_pathlist_head paths;
4182 struct got_pathlist_entry *pe;
4183 struct got_object_id *commit_id = NULL;
4184 char *commit_id_str = NULL;
4186 TAILQ_INIT(&paths);
4188 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4189 switch (ch) {
4190 case 'c':
4191 commit_id_arg = optarg;
4192 break;
4193 case 'd':
4194 delref = optarg;
4195 break;
4196 case 'r':
4197 repo_path = realpath(optarg, NULL);
4198 if (repo_path == NULL)
4199 return got_error_from_errno2("realpath",
4200 optarg);
4201 got_path_strip_trailing_slashes(repo_path);
4202 break;
4203 case 'l':
4204 do_list = 1;
4205 break;
4206 case 'n':
4207 do_update = 0;
4208 break;
4209 default:
4210 usage_branch();
4211 /* NOTREACHED */
4215 if (do_list && delref)
4216 errx(1, "-l and -d options are mutually exclusive\n");
4218 argc -= optind;
4219 argv += optind;
4221 if (!do_list && !delref && argc == 0)
4222 do_show = 1;
4224 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4225 errx(1, "-c option can only be used when creating a branch");
4227 if (do_list || delref) {
4228 if (argc > 0)
4229 usage_branch();
4230 } else if (!do_show && argc != 1)
4231 usage_branch();
4233 #ifndef PROFILE
4234 if (do_list || do_show) {
4235 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4236 NULL) == -1)
4237 err(1, "pledge");
4238 } else {
4239 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4240 "sendfd unveil", NULL) == -1)
4241 err(1, "pledge");
4243 #endif
4244 cwd = getcwd(NULL, 0);
4245 if (cwd == NULL) {
4246 error = got_error_from_errno("getcwd");
4247 goto done;
4250 if (repo_path == NULL) {
4251 error = got_worktree_open(&worktree, cwd);
4252 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4253 goto done;
4254 else
4255 error = NULL;
4256 if (worktree) {
4257 repo_path =
4258 strdup(got_worktree_get_repo_path(worktree));
4259 if (repo_path == NULL)
4260 error = got_error_from_errno("strdup");
4261 if (error)
4262 goto done;
4263 } else {
4264 repo_path = strdup(cwd);
4265 if (repo_path == NULL) {
4266 error = got_error_from_errno("strdup");
4267 goto done;
4272 error = got_repo_open(&repo, repo_path, NULL);
4273 if (error != NULL)
4274 goto done;
4276 error = apply_unveil(got_repo_get_path(repo), do_list,
4277 worktree ? got_worktree_get_root_path(worktree) : NULL);
4278 if (error)
4279 goto done;
4281 if (do_show)
4282 error = show_current_branch(repo, worktree);
4283 else if (do_list)
4284 error = list_branches(repo, worktree);
4285 else if (delref)
4286 error = delete_branch(repo, worktree, delref);
4287 else {
4288 if (commit_id_arg == NULL)
4289 commit_id_arg = worktree ?
4290 got_worktree_get_head_ref_name(worktree) :
4291 GOT_REF_HEAD;
4292 error = got_repo_match_object_id(&commit_id, NULL,
4293 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4294 if (error)
4295 goto done;
4296 error = add_branch(repo, argv[0], commit_id);
4297 if (error)
4298 goto done;
4299 if (worktree && do_update) {
4300 int did_something = 0;
4301 char *branch_refname = NULL;
4303 error = got_object_id_str(&commit_id_str, commit_id);
4304 if (error)
4305 goto done;
4306 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4307 worktree);
4308 if (error)
4309 goto done;
4310 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4311 == -1) {
4312 error = got_error_from_errno("asprintf");
4313 goto done;
4315 error = got_ref_open(&ref, repo, branch_refname, 0);
4316 free(branch_refname);
4317 if (error)
4318 goto done;
4319 error = switch_head_ref(ref, commit_id, worktree,
4320 repo);
4321 if (error)
4322 goto done;
4323 error = got_worktree_set_base_commit_id(worktree, repo,
4324 commit_id);
4325 if (error)
4326 goto done;
4327 error = got_worktree_checkout_files(worktree, &paths,
4328 repo, update_progress, &did_something,
4329 check_cancelled, NULL);
4330 if (error)
4331 goto done;
4332 if (did_something)
4333 printf("Updated to commit %s\n", commit_id_str);
4336 done:
4337 if (ref)
4338 got_ref_close(ref);
4339 if (repo)
4340 got_repo_close(repo);
4341 if (worktree)
4342 got_worktree_close(worktree);
4343 free(cwd);
4344 free(repo_path);
4345 free(commit_id);
4346 free(commit_id_str);
4347 TAILQ_FOREACH(pe, &paths, entry)
4348 free((char *)pe->path);
4349 got_pathlist_free(&paths);
4350 return error;
4354 __dead static void
4355 usage_tag(void)
4357 fprintf(stderr,
4358 "usage: %s tag [-c commit] [-r repository] [-l] "
4359 "[-m message] name\n", getprogname());
4360 exit(1);
4363 #if 0
4364 static const struct got_error *
4365 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4367 const struct got_error *err = NULL;
4368 struct got_reflist_entry *re, *se, *new;
4369 struct got_object_id *re_id, *se_id;
4370 struct got_tag_object *re_tag, *se_tag;
4371 time_t re_time, se_time;
4373 SIMPLEQ_FOREACH(re, tags, entry) {
4374 se = SIMPLEQ_FIRST(sorted);
4375 if (se == NULL) {
4376 err = got_reflist_entry_dup(&new, re);
4377 if (err)
4378 return err;
4379 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4380 continue;
4381 } else {
4382 err = got_ref_resolve(&re_id, repo, re->ref);
4383 if (err)
4384 break;
4385 err = got_object_open_as_tag(&re_tag, repo, re_id);
4386 free(re_id);
4387 if (err)
4388 break;
4389 re_time = got_object_tag_get_tagger_time(re_tag);
4390 got_object_tag_close(re_tag);
4393 while (se) {
4394 err = got_ref_resolve(&se_id, repo, re->ref);
4395 if (err)
4396 break;
4397 err = got_object_open_as_tag(&se_tag, repo, se_id);
4398 free(se_id);
4399 if (err)
4400 break;
4401 se_time = got_object_tag_get_tagger_time(se_tag);
4402 got_object_tag_close(se_tag);
4404 if (se_time > re_time) {
4405 err = got_reflist_entry_dup(&new, re);
4406 if (err)
4407 return err;
4408 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4409 break;
4411 se = SIMPLEQ_NEXT(se, entry);
4412 continue;
4415 done:
4416 return err;
4418 #endif
4420 static const struct got_error *
4421 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4423 static const struct got_error *err = NULL;
4424 struct got_reflist_head refs;
4425 struct got_reflist_entry *re;
4427 SIMPLEQ_INIT(&refs);
4429 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4430 if (err)
4431 return err;
4433 SIMPLEQ_FOREACH(re, &refs, entry) {
4434 const char *refname;
4435 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4436 char datebuf[26];
4437 const char *tagger;
4438 time_t tagger_time;
4439 struct got_object_id *id;
4440 struct got_tag_object *tag;
4441 struct got_commit_object *commit = NULL;
4443 refname = got_ref_get_name(re->ref);
4444 if (strncmp(refname, "refs/tags/", 10) != 0)
4445 continue;
4446 refname += 10;
4447 refstr = got_ref_to_str(re->ref);
4448 if (refstr == NULL) {
4449 err = got_error_from_errno("got_ref_to_str");
4450 break;
4452 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4453 free(refstr);
4455 err = got_ref_resolve(&id, repo, re->ref);
4456 if (err)
4457 break;
4458 err = got_object_open_as_tag(&tag, repo, id);
4459 if (err) {
4460 if (err->code != GOT_ERR_OBJ_TYPE) {
4461 free(id);
4462 break;
4464 /* "lightweight" tag */
4465 err = got_object_open_as_commit(&commit, repo, id);
4466 if (err) {
4467 free(id);
4468 break;
4470 tagger = got_object_commit_get_committer(commit);
4471 tagger_time =
4472 got_object_commit_get_committer_time(commit);
4473 err = got_object_id_str(&id_str, id);
4474 free(id);
4475 if (err)
4476 break;
4477 } else {
4478 free(id);
4479 tagger = got_object_tag_get_tagger(tag);
4480 tagger_time = got_object_tag_get_tagger_time(tag);
4481 err = got_object_id_str(&id_str,
4482 got_object_tag_get_object_id(tag));
4483 if (err)
4484 break;
4486 printf("from: %s\n", tagger);
4487 datestr = get_datestr(&tagger_time, datebuf);
4488 if (datestr)
4489 printf("date: %s UTC\n", datestr);
4490 if (commit)
4491 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4492 else {
4493 switch (got_object_tag_get_object_type(tag)) {
4494 case GOT_OBJ_TYPE_BLOB:
4495 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4496 id_str);
4497 break;
4498 case GOT_OBJ_TYPE_TREE:
4499 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4500 id_str);
4501 break;
4502 case GOT_OBJ_TYPE_COMMIT:
4503 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4504 id_str);
4505 break;
4506 case GOT_OBJ_TYPE_TAG:
4507 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4508 id_str);
4509 break;
4510 default:
4511 break;
4514 free(id_str);
4515 if (commit) {
4516 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4517 if (err)
4518 break;
4519 got_object_commit_close(commit);
4520 } else {
4521 tagmsg0 = strdup(got_object_tag_get_message(tag));
4522 got_object_tag_close(tag);
4523 if (tagmsg0 == NULL) {
4524 err = got_error_from_errno("strdup");
4525 break;
4529 tagmsg = tagmsg0;
4530 do {
4531 line = strsep(&tagmsg, "\n");
4532 if (line)
4533 printf(" %s\n", line);
4534 } while (line);
4535 free(tagmsg0);
4538 got_ref_list_free(&refs);
4539 return NULL;
4542 static const struct got_error *
4543 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4544 const char *tag_name, const char *repo_path)
4546 const struct got_error *err = NULL;
4547 char *template = NULL, *initial_content = NULL;
4548 char *editor = NULL;
4549 int fd = -1;
4551 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4552 err = got_error_from_errno("asprintf");
4553 goto done;
4556 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4557 commit_id_str, tag_name) == -1) {
4558 err = got_error_from_errno("asprintf");
4559 goto done;
4562 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4563 if (err)
4564 goto done;
4566 dprintf(fd, initial_content);
4567 close(fd);
4569 err = get_editor(&editor);
4570 if (err)
4571 goto done;
4572 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4573 done:
4574 free(initial_content);
4575 free(template);
4576 free(editor);
4578 /* Editor is done; we can now apply unveil(2) */
4579 if (err == NULL) {
4580 err = apply_unveil(repo_path, 0, NULL);
4581 if (err) {
4582 free(*tagmsg);
4583 *tagmsg = NULL;
4586 return err;
4589 static const struct got_error *
4590 add_tag(struct got_repository *repo, const char *tag_name,
4591 const char *commit_arg, const char *tagmsg_arg)
4593 const struct got_error *err = NULL;
4594 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4595 char *label = NULL, *commit_id_str = NULL;
4596 struct got_reference *ref = NULL;
4597 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4598 char *tagmsg_path = NULL, *tag_id_str = NULL;
4599 int preserve_tagmsg = 0;
4602 * Don't let the user create a tag name with a leading '-'.
4603 * While technically a valid reference name, this case is usually
4604 * an unintended typo.
4606 if (tag_name[0] == '-')
4607 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4609 err = get_author(&tagger, repo);
4610 if (err)
4611 return err;
4613 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4614 GOT_OBJ_TYPE_COMMIT, 1, repo);
4615 if (err)
4616 goto done;
4618 err = got_object_id_str(&commit_id_str, commit_id);
4619 if (err)
4620 goto done;
4622 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4623 refname = strdup(tag_name);
4624 if (refname == NULL) {
4625 err = got_error_from_errno("strdup");
4626 goto done;
4628 tag_name += 10;
4629 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4630 err = got_error_from_errno("asprintf");
4631 goto done;
4634 err = got_ref_open(&ref, repo, refname, 0);
4635 if (err == NULL) {
4636 err = got_error(GOT_ERR_TAG_EXISTS);
4637 goto done;
4638 } else if (err->code != GOT_ERR_NOT_REF)
4639 goto done;
4641 if (tagmsg_arg == NULL) {
4642 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4643 tag_name, got_repo_get_path(repo));
4644 if (err) {
4645 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4646 tagmsg_path != NULL)
4647 preserve_tagmsg = 1;
4648 goto done;
4652 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4653 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4654 if (err) {
4655 if (tagmsg_path)
4656 preserve_tagmsg = 1;
4657 goto done;
4660 err = got_ref_alloc(&ref, refname, tag_id);
4661 if (err) {
4662 if (tagmsg_path)
4663 preserve_tagmsg = 1;
4664 goto done;
4667 err = got_ref_write(ref, repo);
4668 if (err) {
4669 if (tagmsg_path)
4670 preserve_tagmsg = 1;
4671 goto done;
4674 err = got_object_id_str(&tag_id_str, tag_id);
4675 if (err) {
4676 if (tagmsg_path)
4677 preserve_tagmsg = 1;
4678 goto done;
4680 printf("Created tag %s\n", tag_id_str);
4681 done:
4682 if (preserve_tagmsg) {
4683 fprintf(stderr, "%s: tag message preserved in %s\n",
4684 getprogname(), tagmsg_path);
4685 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4686 err = got_error_from_errno2("unlink", tagmsg_path);
4687 free(tag_id_str);
4688 if (ref)
4689 got_ref_close(ref);
4690 free(commit_id);
4691 free(commit_id_str);
4692 free(refname);
4693 free(tagmsg);
4694 free(tagmsg_path);
4695 free(tagger);
4696 return err;
4699 static const struct got_error *
4700 cmd_tag(int argc, char *argv[])
4702 const struct got_error *error = NULL;
4703 struct got_repository *repo = NULL;
4704 struct got_worktree *worktree = NULL;
4705 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4706 char *gitconfig_path = NULL;
4707 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4708 int ch, do_list = 0;
4710 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4711 switch (ch) {
4712 case 'c':
4713 commit_id_arg = optarg;
4714 break;
4715 case 'm':
4716 tagmsg = optarg;
4717 break;
4718 case 'r':
4719 repo_path = realpath(optarg, NULL);
4720 if (repo_path == NULL)
4721 return got_error_from_errno2("realpath",
4722 optarg);
4723 got_path_strip_trailing_slashes(repo_path);
4724 break;
4725 case 'l':
4726 do_list = 1;
4727 break;
4728 default:
4729 usage_tag();
4730 /* NOTREACHED */
4734 argc -= optind;
4735 argv += optind;
4737 if (do_list) {
4738 if (commit_id_arg != NULL)
4739 errx(1, "-c option can only be used when creating a tag");
4740 if (tagmsg)
4741 errx(1, "-l and -m options are mutually exclusive");
4742 if (argc > 0)
4743 usage_tag();
4744 } else if (argc != 1)
4745 usage_tag();
4747 tag_name = argv[0];
4749 #ifndef PROFILE
4750 if (do_list) {
4751 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4752 NULL) == -1)
4753 err(1, "pledge");
4754 } else {
4755 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4756 "sendfd unveil", NULL) == -1)
4757 err(1, "pledge");
4759 #endif
4760 cwd = getcwd(NULL, 0);
4761 if (cwd == NULL) {
4762 error = got_error_from_errno("getcwd");
4763 goto done;
4766 if (repo_path == NULL) {
4767 error = got_worktree_open(&worktree, cwd);
4768 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4769 goto done;
4770 else
4771 error = NULL;
4772 if (worktree) {
4773 repo_path =
4774 strdup(got_worktree_get_repo_path(worktree));
4775 if (repo_path == NULL)
4776 error = got_error_from_errno("strdup");
4777 if (error)
4778 goto done;
4779 } else {
4780 repo_path = strdup(cwd);
4781 if (repo_path == NULL) {
4782 error = got_error_from_errno("strdup");
4783 goto done;
4788 if (do_list) {
4789 error = got_repo_open(&repo, repo_path, NULL);
4790 if (error != NULL)
4791 goto done;
4792 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4793 if (error)
4794 goto done;
4795 error = list_tags(repo, worktree);
4796 } else {
4797 error = get_gitconfig_path(&gitconfig_path);
4798 if (error)
4799 goto done;
4800 error = got_repo_open(&repo, repo_path, gitconfig_path);
4801 if (error != NULL)
4802 goto done;
4804 if (tagmsg) {
4805 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4806 if (error)
4807 goto done;
4810 if (commit_id_arg == NULL) {
4811 struct got_reference *head_ref;
4812 struct got_object_id *commit_id;
4813 error = got_ref_open(&head_ref, repo,
4814 worktree ? got_worktree_get_head_ref_name(worktree)
4815 : GOT_REF_HEAD, 0);
4816 if (error)
4817 goto done;
4818 error = got_ref_resolve(&commit_id, repo, head_ref);
4819 got_ref_close(head_ref);
4820 if (error)
4821 goto done;
4822 error = got_object_id_str(&commit_id_str, commit_id);
4823 free(commit_id);
4824 if (error)
4825 goto done;
4828 error = add_tag(repo, tag_name,
4829 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4831 done:
4832 if (repo)
4833 got_repo_close(repo);
4834 if (worktree)
4835 got_worktree_close(worktree);
4836 free(cwd);
4837 free(repo_path);
4838 free(gitconfig_path);
4839 free(commit_id_str);
4840 return error;
4843 __dead static void
4844 usage_add(void)
4846 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4847 getprogname());
4848 exit(1);
4851 static const struct got_error *
4852 add_progress(void *arg, unsigned char status, const char *path)
4854 while (path[0] == '/')
4855 path++;
4856 printf("%c %s\n", status, path);
4857 return NULL;
4860 static const struct got_error *
4861 cmd_add(int argc, char *argv[])
4863 const struct got_error *error = NULL;
4864 struct got_repository *repo = NULL;
4865 struct got_worktree *worktree = NULL;
4866 char *cwd = NULL;
4867 struct got_pathlist_head paths;
4868 struct got_pathlist_entry *pe;
4869 int ch, can_recurse = 0, no_ignores = 0;
4871 TAILQ_INIT(&paths);
4873 while ((ch = getopt(argc, argv, "IR")) != -1) {
4874 switch (ch) {
4875 case 'I':
4876 no_ignores = 1;
4877 break;
4878 case 'R':
4879 can_recurse = 1;
4880 break;
4881 default:
4882 usage_add();
4883 /* NOTREACHED */
4887 argc -= optind;
4888 argv += optind;
4890 #ifndef PROFILE
4891 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4892 NULL) == -1)
4893 err(1, "pledge");
4894 #endif
4895 if (argc < 1)
4896 usage_add();
4898 cwd = getcwd(NULL, 0);
4899 if (cwd == NULL) {
4900 error = got_error_from_errno("getcwd");
4901 goto done;
4904 error = got_worktree_open(&worktree, cwd);
4905 if (error)
4906 goto done;
4908 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4909 NULL);
4910 if (error != NULL)
4911 goto done;
4913 error = apply_unveil(got_repo_get_path(repo), 1,
4914 got_worktree_get_root_path(worktree));
4915 if (error)
4916 goto done;
4918 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4919 if (error)
4920 goto done;
4922 if (!can_recurse && no_ignores) {
4923 error = got_error_msg(GOT_ERR_BAD_PATH,
4924 "disregarding ignores requires -R option");
4925 goto done;
4929 if (!can_recurse) {
4930 char *ondisk_path;
4931 struct stat sb;
4932 TAILQ_FOREACH(pe, &paths, entry) {
4933 if (asprintf(&ondisk_path, "%s/%s",
4934 got_worktree_get_root_path(worktree),
4935 pe->path) == -1) {
4936 error = got_error_from_errno("asprintf");
4937 goto done;
4939 if (lstat(ondisk_path, &sb) == -1) {
4940 if (errno == ENOENT) {
4941 free(ondisk_path);
4942 continue;
4944 error = got_error_from_errno2("lstat",
4945 ondisk_path);
4946 free(ondisk_path);
4947 goto done;
4949 free(ondisk_path);
4950 if (S_ISDIR(sb.st_mode)) {
4951 error = got_error_msg(GOT_ERR_BAD_PATH,
4952 "adding directories requires -R option");
4953 goto done;
4958 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4959 NULL, repo, no_ignores);
4960 done:
4961 if (repo)
4962 got_repo_close(repo);
4963 if (worktree)
4964 got_worktree_close(worktree);
4965 TAILQ_FOREACH(pe, &paths, entry)
4966 free((char *)pe->path);
4967 got_pathlist_free(&paths);
4968 free(cwd);
4969 return error;
4972 __dead static void
4973 usage_remove(void)
4975 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4976 getprogname());
4977 exit(1);
4980 static const struct got_error *
4981 print_remove_status(void *arg, unsigned char status,
4982 unsigned char staged_status, const char *path)
4984 while (path[0] == '/')
4985 path++;
4986 if (status == GOT_STATUS_NONEXISTENT)
4987 return NULL;
4988 if (status == staged_status && (status == GOT_STATUS_DELETE))
4989 status = GOT_STATUS_NO_CHANGE;
4990 printf("%c%c %s\n", status, staged_status, path);
4991 return NULL;
4994 static const struct got_error *
4995 cmd_remove(int argc, char *argv[])
4997 const struct got_error *error = NULL;
4998 struct got_worktree *worktree = NULL;
4999 struct got_repository *repo = NULL;
5000 char *cwd = NULL;
5001 struct got_pathlist_head paths;
5002 struct got_pathlist_entry *pe;
5003 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5005 TAILQ_INIT(&paths);
5007 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5008 switch (ch) {
5009 case 'f':
5010 delete_local_mods = 1;
5011 break;
5012 case 'k':
5013 keep_on_disk = 1;
5014 break;
5015 case 'R':
5016 can_recurse = 1;
5017 break;
5018 default:
5019 usage_remove();
5020 /* NOTREACHED */
5024 argc -= optind;
5025 argv += optind;
5027 #ifndef PROFILE
5028 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5029 NULL) == -1)
5030 err(1, "pledge");
5031 #endif
5032 if (argc < 1)
5033 usage_remove();
5035 cwd = getcwd(NULL, 0);
5036 if (cwd == NULL) {
5037 error = got_error_from_errno("getcwd");
5038 goto done;
5040 error = got_worktree_open(&worktree, cwd);
5041 if (error)
5042 goto done;
5044 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5045 NULL);
5046 if (error)
5047 goto done;
5049 error = apply_unveil(got_repo_get_path(repo), 1,
5050 got_worktree_get_root_path(worktree));
5051 if (error)
5052 goto done;
5054 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5055 if (error)
5056 goto done;
5058 if (!can_recurse) {
5059 char *ondisk_path;
5060 struct stat sb;
5061 TAILQ_FOREACH(pe, &paths, entry) {
5062 if (asprintf(&ondisk_path, "%s/%s",
5063 got_worktree_get_root_path(worktree),
5064 pe->path) == -1) {
5065 error = got_error_from_errno("asprintf");
5066 goto done;
5068 if (lstat(ondisk_path, &sb) == -1) {
5069 if (errno == ENOENT) {
5070 free(ondisk_path);
5071 continue;
5073 error = got_error_from_errno2("lstat",
5074 ondisk_path);
5075 free(ondisk_path);
5076 goto done;
5078 free(ondisk_path);
5079 if (S_ISDIR(sb.st_mode)) {
5080 error = got_error_msg(GOT_ERR_BAD_PATH,
5081 "removing directories requires -R option");
5082 goto done;
5087 error = got_worktree_schedule_delete(worktree, &paths,
5088 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5089 done:
5090 if (repo)
5091 got_repo_close(repo);
5092 if (worktree)
5093 got_worktree_close(worktree);
5094 TAILQ_FOREACH(pe, &paths, entry)
5095 free((char *)pe->path);
5096 got_pathlist_free(&paths);
5097 free(cwd);
5098 return error;
5101 __dead static void
5102 usage_revert(void)
5104 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5105 "path ...\n", getprogname());
5106 exit(1);
5109 static const struct got_error *
5110 revert_progress(void *arg, unsigned char status, const char *path)
5112 if (status == GOT_STATUS_UNVERSIONED)
5113 return NULL;
5115 while (path[0] == '/')
5116 path++;
5117 printf("%c %s\n", status, path);
5118 return NULL;
5121 struct choose_patch_arg {
5122 FILE *patch_script_file;
5123 const char *action;
5126 static const struct got_error *
5127 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5128 int nchanges, const char *action)
5130 char *line = NULL;
5131 size_t linesize = 0;
5132 ssize_t linelen;
5134 switch (status) {
5135 case GOT_STATUS_ADD:
5136 printf("A %s\n%s this addition? [y/n] ", path, action);
5137 break;
5138 case GOT_STATUS_DELETE:
5139 printf("D %s\n%s this deletion? [y/n] ", path, action);
5140 break;
5141 case GOT_STATUS_MODIFY:
5142 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5143 return got_error_from_errno("fseek");
5144 printf(GOT_COMMIT_SEP_STR);
5145 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5146 printf("%s", line);
5147 if (ferror(patch_file))
5148 return got_error_from_errno("getline");
5149 printf(GOT_COMMIT_SEP_STR);
5150 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5151 path, n, nchanges, action);
5152 break;
5153 default:
5154 return got_error_path(path, GOT_ERR_FILE_STATUS);
5157 return NULL;
5160 static const struct got_error *
5161 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5162 FILE *patch_file, int n, int nchanges)
5164 const struct got_error *err = NULL;
5165 char *line = NULL;
5166 size_t linesize = 0;
5167 ssize_t linelen;
5168 int resp = ' ';
5169 struct choose_patch_arg *a = arg;
5171 *choice = GOT_PATCH_CHOICE_NONE;
5173 if (a->patch_script_file) {
5174 char *nl;
5175 err = show_change(status, path, patch_file, n, nchanges,
5176 a->action);
5177 if (err)
5178 return err;
5179 linelen = getline(&line, &linesize, a->patch_script_file);
5180 if (linelen == -1) {
5181 if (ferror(a->patch_script_file))
5182 return got_error_from_errno("getline");
5183 return NULL;
5185 nl = strchr(line, '\n');
5186 if (nl)
5187 *nl = '\0';
5188 if (strcmp(line, "y") == 0) {
5189 *choice = GOT_PATCH_CHOICE_YES;
5190 printf("y\n");
5191 } else if (strcmp(line, "n") == 0) {
5192 *choice = GOT_PATCH_CHOICE_NO;
5193 printf("n\n");
5194 } else if (strcmp(line, "q") == 0 &&
5195 status == GOT_STATUS_MODIFY) {
5196 *choice = GOT_PATCH_CHOICE_QUIT;
5197 printf("q\n");
5198 } else
5199 printf("invalid response '%s'\n", line);
5200 free(line);
5201 return NULL;
5204 while (resp != 'y' && resp != 'n' && resp != 'q') {
5205 err = show_change(status, path, patch_file, n, nchanges,
5206 a->action);
5207 if (err)
5208 return err;
5209 resp = getchar();
5210 if (resp == '\n')
5211 resp = getchar();
5212 if (status == GOT_STATUS_MODIFY) {
5213 if (resp != 'y' && resp != 'n' && resp != 'q') {
5214 printf("invalid response '%c'\n", resp);
5215 resp = ' ';
5217 } else if (resp != 'y' && resp != 'n') {
5218 printf("invalid response '%c'\n", resp);
5219 resp = ' ';
5223 if (resp == 'y')
5224 *choice = GOT_PATCH_CHOICE_YES;
5225 else if (resp == 'n')
5226 *choice = GOT_PATCH_CHOICE_NO;
5227 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5228 *choice = GOT_PATCH_CHOICE_QUIT;
5230 return NULL;
5234 static const struct got_error *
5235 cmd_revert(int argc, char *argv[])
5237 const struct got_error *error = NULL;
5238 struct got_worktree *worktree = NULL;
5239 struct got_repository *repo = NULL;
5240 char *cwd = NULL, *path = NULL;
5241 struct got_pathlist_head paths;
5242 struct got_pathlist_entry *pe;
5243 int ch, can_recurse = 0, pflag = 0;
5244 FILE *patch_script_file = NULL;
5245 const char *patch_script_path = NULL;
5246 struct choose_patch_arg cpa;
5248 TAILQ_INIT(&paths);
5250 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5251 switch (ch) {
5252 case 'p':
5253 pflag = 1;
5254 break;
5255 case 'F':
5256 patch_script_path = optarg;
5257 break;
5258 case 'R':
5259 can_recurse = 1;
5260 break;
5261 default:
5262 usage_revert();
5263 /* NOTREACHED */
5267 argc -= optind;
5268 argv += optind;
5270 #ifndef PROFILE
5271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5272 "unveil", NULL) == -1)
5273 err(1, "pledge");
5274 #endif
5275 if (argc < 1)
5276 usage_revert();
5277 if (patch_script_path && !pflag)
5278 errx(1, "-F option can only be used together with -p option");
5280 cwd = getcwd(NULL, 0);
5281 if (cwd == NULL) {
5282 error = got_error_from_errno("getcwd");
5283 goto done;
5285 error = got_worktree_open(&worktree, cwd);
5286 if (error)
5287 goto done;
5289 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5290 NULL);
5291 if (error != NULL)
5292 goto done;
5294 if (patch_script_path) {
5295 patch_script_file = fopen(patch_script_path, "r");
5296 if (patch_script_file == NULL) {
5297 error = got_error_from_errno2("fopen",
5298 patch_script_path);
5299 goto done;
5302 error = apply_unveil(got_repo_get_path(repo), 1,
5303 got_worktree_get_root_path(worktree));
5304 if (error)
5305 goto done;
5307 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5308 if (error)
5309 goto done;
5311 if (!can_recurse) {
5312 char *ondisk_path;
5313 struct stat sb;
5314 TAILQ_FOREACH(pe, &paths, entry) {
5315 if (asprintf(&ondisk_path, "%s/%s",
5316 got_worktree_get_root_path(worktree),
5317 pe->path) == -1) {
5318 error = got_error_from_errno("asprintf");
5319 goto done;
5321 if (lstat(ondisk_path, &sb) == -1) {
5322 if (errno == ENOENT) {
5323 free(ondisk_path);
5324 continue;
5326 error = got_error_from_errno2("lstat",
5327 ondisk_path);
5328 free(ondisk_path);
5329 goto done;
5331 free(ondisk_path);
5332 if (S_ISDIR(sb.st_mode)) {
5333 error = got_error_msg(GOT_ERR_BAD_PATH,
5334 "reverting directories requires -R option");
5335 goto done;
5340 cpa.patch_script_file = patch_script_file;
5341 cpa.action = "revert";
5342 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5343 pflag ? choose_patch : NULL, &cpa, repo);
5344 done:
5345 if (patch_script_file && fclose(patch_script_file) == EOF &&
5346 error == NULL)
5347 error = got_error_from_errno2("fclose", patch_script_path);
5348 if (repo)
5349 got_repo_close(repo);
5350 if (worktree)
5351 got_worktree_close(worktree);
5352 free(path);
5353 free(cwd);
5354 return error;
5357 __dead static void
5358 usage_commit(void)
5360 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5361 getprogname());
5362 exit(1);
5365 struct collect_commit_logmsg_arg {
5366 const char *cmdline_log;
5367 const char *editor;
5368 const char *worktree_path;
5369 const char *branch_name;
5370 const char *repo_path;
5371 char *logmsg_path;
5375 static const struct got_error *
5376 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5377 void *arg)
5379 char *initial_content = NULL;
5380 struct got_pathlist_entry *pe;
5381 const struct got_error *err = NULL;
5382 char *template = NULL;
5383 struct collect_commit_logmsg_arg *a = arg;
5384 int fd;
5385 size_t len;
5387 /* if a message was specified on the command line, just use it */
5388 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5389 len = strlen(a->cmdline_log) + 1;
5390 *logmsg = malloc(len + 1);
5391 if (*logmsg == NULL)
5392 return got_error_from_errno("malloc");
5393 strlcpy(*logmsg, a->cmdline_log, len);
5394 return NULL;
5397 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5398 return got_error_from_errno("asprintf");
5400 if (asprintf(&initial_content,
5401 "\n# changes to be committed on branch %s:\n",
5402 a->branch_name) == -1)
5403 return got_error_from_errno("asprintf");
5405 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5406 if (err)
5407 goto done;
5409 dprintf(fd, initial_content);
5411 TAILQ_FOREACH(pe, commitable_paths, entry) {
5412 struct got_commitable *ct = pe->data;
5413 dprintf(fd, "# %c %s\n",
5414 got_commitable_get_status(ct),
5415 got_commitable_get_path(ct));
5417 close(fd);
5419 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5420 done:
5421 free(initial_content);
5422 free(template);
5424 /* Editor is done; we can now apply unveil(2) */
5425 if (err == NULL) {
5426 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5427 if (err) {
5428 free(*logmsg);
5429 *logmsg = NULL;
5432 return err;
5435 static const struct got_error *
5436 cmd_commit(int argc, char *argv[])
5438 const struct got_error *error = NULL;
5439 struct got_worktree *worktree = NULL;
5440 struct got_repository *repo = NULL;
5441 char *cwd = NULL, *id_str = NULL;
5442 struct got_object_id *id = NULL;
5443 const char *logmsg = NULL;
5444 struct collect_commit_logmsg_arg cl_arg;
5445 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5446 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5447 struct got_pathlist_head paths;
5449 TAILQ_INIT(&paths);
5450 cl_arg.logmsg_path = NULL;
5452 while ((ch = getopt(argc, argv, "m:")) != -1) {
5453 switch (ch) {
5454 case 'm':
5455 logmsg = optarg;
5456 break;
5457 default:
5458 usage_commit();
5459 /* NOTREACHED */
5463 argc -= optind;
5464 argv += optind;
5466 #ifndef PROFILE
5467 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5468 "unveil", NULL) == -1)
5469 err(1, "pledge");
5470 #endif
5471 cwd = getcwd(NULL, 0);
5472 if (cwd == NULL) {
5473 error = got_error_from_errno("getcwd");
5474 goto done;
5476 error = got_worktree_open(&worktree, cwd);
5477 if (error)
5478 goto done;
5480 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5481 if (error)
5482 goto done;
5483 if (rebase_in_progress) {
5484 error = got_error(GOT_ERR_REBASING);
5485 goto done;
5488 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5489 worktree);
5490 if (error)
5491 goto done;
5493 error = get_gitconfig_path(&gitconfig_path);
5494 if (error)
5495 goto done;
5496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5497 gitconfig_path);
5498 if (error != NULL)
5499 goto done;
5501 error = get_author(&author, repo);
5502 if (error)
5503 return error;
5506 * unveil(2) traverses exec(2); if an editor is used we have
5507 * to apply unveil after the log message has been written.
5509 if (logmsg == NULL || strlen(logmsg) == 0)
5510 error = get_editor(&editor);
5511 else
5512 error = apply_unveil(got_repo_get_path(repo), 0,
5513 got_worktree_get_root_path(worktree));
5514 if (error)
5515 goto done;
5517 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5518 if (error)
5519 goto done;
5521 cl_arg.editor = editor;
5522 cl_arg.cmdline_log = logmsg;
5523 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5524 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5525 if (!histedit_in_progress) {
5526 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5527 error = got_error(GOT_ERR_COMMIT_BRANCH);
5528 goto done;
5530 cl_arg.branch_name += 11;
5532 cl_arg.repo_path = got_repo_get_path(repo);
5533 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5534 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5535 if (error) {
5536 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5537 cl_arg.logmsg_path != NULL)
5538 preserve_logmsg = 1;
5539 goto done;
5542 error = got_object_id_str(&id_str, id);
5543 if (error)
5544 goto done;
5545 printf("Created commit %s\n", id_str);
5546 done:
5547 if (preserve_logmsg) {
5548 fprintf(stderr, "%s: log message preserved in %s\n",
5549 getprogname(), cl_arg.logmsg_path);
5550 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5551 error == NULL)
5552 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5553 free(cl_arg.logmsg_path);
5554 if (repo)
5555 got_repo_close(repo);
5556 if (worktree)
5557 got_worktree_close(worktree);
5558 free(cwd);
5559 free(id_str);
5560 free(gitconfig_path);
5561 free(editor);
5562 free(author);
5563 return error;
5566 __dead static void
5567 usage_cherrypick(void)
5569 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5570 exit(1);
5573 static const struct got_error *
5574 cmd_cherrypick(int argc, char *argv[])
5576 const struct got_error *error = NULL;
5577 struct got_worktree *worktree = NULL;
5578 struct got_repository *repo = NULL;
5579 char *cwd = NULL, *commit_id_str = NULL;
5580 struct got_object_id *commit_id = NULL;
5581 struct got_commit_object *commit = NULL;
5582 struct got_object_qid *pid;
5583 struct got_reference *head_ref = NULL;
5584 int ch, did_something = 0;
5586 while ((ch = getopt(argc, argv, "")) != -1) {
5587 switch (ch) {
5588 default:
5589 usage_cherrypick();
5590 /* NOTREACHED */
5594 argc -= optind;
5595 argv += optind;
5597 #ifndef PROFILE
5598 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5599 "unveil", NULL) == -1)
5600 err(1, "pledge");
5601 #endif
5602 if (argc != 1)
5603 usage_cherrypick();
5605 cwd = getcwd(NULL, 0);
5606 if (cwd == NULL) {
5607 error = got_error_from_errno("getcwd");
5608 goto done;
5610 error = got_worktree_open(&worktree, cwd);
5611 if (error)
5612 goto done;
5614 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5615 NULL);
5616 if (error != NULL)
5617 goto done;
5619 error = apply_unveil(got_repo_get_path(repo), 0,
5620 got_worktree_get_root_path(worktree));
5621 if (error)
5622 goto done;
5624 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5625 GOT_OBJ_TYPE_COMMIT, repo);
5626 if (error != NULL) {
5627 struct got_reference *ref;
5628 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5629 goto done;
5630 error = got_ref_open(&ref, repo, argv[0], 0);
5631 if (error != NULL)
5632 goto done;
5633 error = got_ref_resolve(&commit_id, repo, ref);
5634 got_ref_close(ref);
5635 if (error != NULL)
5636 goto done;
5638 error = got_object_id_str(&commit_id_str, commit_id);
5639 if (error)
5640 goto done;
5642 error = got_ref_open(&head_ref, repo,
5643 got_worktree_get_head_ref_name(worktree), 0);
5644 if (error != NULL)
5645 goto done;
5647 error = check_same_branch(commit_id, head_ref, NULL, repo);
5648 if (error) {
5649 if (error->code != GOT_ERR_ANCESTRY)
5650 goto done;
5651 error = NULL;
5652 } else {
5653 error = got_error(GOT_ERR_SAME_BRANCH);
5654 goto done;
5657 error = got_object_open_as_commit(&commit, repo, commit_id);
5658 if (error)
5659 goto done;
5660 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5661 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5662 commit_id, repo, update_progress, &did_something, check_cancelled,
5663 NULL);
5664 if (error != NULL)
5665 goto done;
5667 if (did_something)
5668 printf("Merged commit %s\n", commit_id_str);
5669 done:
5670 if (commit)
5671 got_object_commit_close(commit);
5672 free(commit_id_str);
5673 if (head_ref)
5674 got_ref_close(head_ref);
5675 if (worktree)
5676 got_worktree_close(worktree);
5677 if (repo)
5678 got_repo_close(repo);
5679 return error;
5682 __dead static void
5683 usage_backout(void)
5685 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5686 exit(1);
5689 static const struct got_error *
5690 cmd_backout(int argc, char *argv[])
5692 const struct got_error *error = NULL;
5693 struct got_worktree *worktree = NULL;
5694 struct got_repository *repo = NULL;
5695 char *cwd = NULL, *commit_id_str = NULL;
5696 struct got_object_id *commit_id = NULL;
5697 struct got_commit_object *commit = NULL;
5698 struct got_object_qid *pid;
5699 struct got_reference *head_ref = NULL;
5700 int ch, did_something = 0;
5702 while ((ch = getopt(argc, argv, "")) != -1) {
5703 switch (ch) {
5704 default:
5705 usage_backout();
5706 /* NOTREACHED */
5710 argc -= optind;
5711 argv += optind;
5713 #ifndef PROFILE
5714 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5715 "unveil", NULL) == -1)
5716 err(1, "pledge");
5717 #endif
5718 if (argc != 1)
5719 usage_backout();
5721 cwd = getcwd(NULL, 0);
5722 if (cwd == NULL) {
5723 error = got_error_from_errno("getcwd");
5724 goto done;
5726 error = got_worktree_open(&worktree, cwd);
5727 if (error)
5728 goto done;
5730 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5731 NULL);
5732 if (error != NULL)
5733 goto done;
5735 error = apply_unveil(got_repo_get_path(repo), 0,
5736 got_worktree_get_root_path(worktree));
5737 if (error)
5738 goto done;
5740 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5741 GOT_OBJ_TYPE_COMMIT, repo);
5742 if (error != NULL) {
5743 struct got_reference *ref;
5744 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5745 goto done;
5746 error = got_ref_open(&ref, repo, argv[0], 0);
5747 if (error != NULL)
5748 goto done;
5749 error = got_ref_resolve(&commit_id, repo, ref);
5750 got_ref_close(ref);
5751 if (error != NULL)
5752 goto done;
5754 error = got_object_id_str(&commit_id_str, commit_id);
5755 if (error)
5756 goto done;
5758 error = got_ref_open(&head_ref, repo,
5759 got_worktree_get_head_ref_name(worktree), 0);
5760 if (error != NULL)
5761 goto done;
5763 error = check_same_branch(commit_id, head_ref, NULL, repo);
5764 if (error)
5765 goto done;
5767 error = got_object_open_as_commit(&commit, repo, commit_id);
5768 if (error)
5769 goto done;
5770 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5771 if (pid == NULL) {
5772 error = got_error(GOT_ERR_ROOT_COMMIT);
5773 goto done;
5776 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5777 update_progress, &did_something, check_cancelled, NULL);
5778 if (error != NULL)
5779 goto done;
5781 if (did_something)
5782 printf("Backed out commit %s\n", commit_id_str);
5783 done:
5784 if (commit)
5785 got_object_commit_close(commit);
5786 free(commit_id_str);
5787 if (head_ref)
5788 got_ref_close(head_ref);
5789 if (worktree)
5790 got_worktree_close(worktree);
5791 if (repo)
5792 got_repo_close(repo);
5793 return error;
5796 __dead static void
5797 usage_rebase(void)
5799 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5800 getprogname());
5801 exit(1);
5804 void
5805 trim_logmsg(char *logmsg, int limit)
5807 char *nl;
5808 size_t len;
5810 len = strlen(logmsg);
5811 if (len > limit)
5812 len = limit;
5813 logmsg[len] = '\0';
5814 nl = strchr(logmsg, '\n');
5815 if (nl)
5816 *nl = '\0';
5819 static const struct got_error *
5820 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5822 const struct got_error *err;
5823 char *logmsg0 = NULL;
5824 const char *s;
5826 err = got_object_commit_get_logmsg(&logmsg0, commit);
5827 if (err)
5828 return err;
5830 s = logmsg0;
5831 while (isspace((unsigned char)s[0]))
5832 s++;
5834 *logmsg = strdup(s);
5835 if (*logmsg == NULL) {
5836 err = got_error_from_errno("strdup");
5837 goto done;
5840 trim_logmsg(*logmsg, limit);
5841 done:
5842 free(logmsg0);
5843 return err;
5846 static const struct got_error *
5847 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5849 const struct got_error *err;
5850 struct got_commit_object *commit = NULL;
5851 char *id_str = NULL, *logmsg = NULL;
5853 err = got_object_open_as_commit(&commit, repo, id);
5854 if (err)
5855 return err;
5857 err = got_object_id_str(&id_str, id);
5858 if (err)
5859 goto done;
5861 id_str[12] = '\0';
5863 err = get_short_logmsg(&logmsg, 42, commit);
5864 if (err)
5865 goto done;
5867 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5868 done:
5869 free(id_str);
5870 got_object_commit_close(commit);
5871 free(logmsg);
5872 return err;
5875 static const struct got_error *
5876 show_rebase_progress(struct got_commit_object *commit,
5877 struct got_object_id *old_id, struct got_object_id *new_id)
5879 const struct got_error *err;
5880 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5882 err = got_object_id_str(&old_id_str, old_id);
5883 if (err)
5884 goto done;
5886 if (new_id) {
5887 err = got_object_id_str(&new_id_str, new_id);
5888 if (err)
5889 goto done;
5892 old_id_str[12] = '\0';
5893 if (new_id_str)
5894 new_id_str[12] = '\0';
5896 err = get_short_logmsg(&logmsg, 42, commit);
5897 if (err)
5898 goto done;
5900 printf("%s -> %s: %s\n", old_id_str,
5901 new_id_str ? new_id_str : "no-op change", logmsg);
5902 done:
5903 free(old_id_str);
5904 free(new_id_str);
5905 free(logmsg);
5906 return err;
5909 static const struct got_error *
5910 rebase_progress(void *arg, unsigned char status, const char *path)
5912 unsigned char *rebase_status = arg;
5914 while (path[0] == '/')
5915 path++;
5916 printf("%c %s\n", status, path);
5918 if (*rebase_status == GOT_STATUS_CONFLICT)
5919 return NULL;
5920 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5921 *rebase_status = status;
5922 return NULL;
5925 static const struct got_error *
5926 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5927 struct got_reference *branch, struct got_reference *new_base_branch,
5928 struct got_reference *tmp_branch, struct got_repository *repo)
5930 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5931 return got_worktree_rebase_complete(worktree, fileindex,
5932 new_base_branch, tmp_branch, branch, repo);
5935 static const struct got_error *
5936 rebase_commit(struct got_pathlist_head *merged_paths,
5937 struct got_worktree *worktree, struct got_fileindex *fileindex,
5938 struct got_reference *tmp_branch,
5939 struct got_object_id *commit_id, struct got_repository *repo)
5941 const struct got_error *error;
5942 struct got_commit_object *commit;
5943 struct got_object_id *new_commit_id;
5945 error = got_object_open_as_commit(&commit, repo, commit_id);
5946 if (error)
5947 return error;
5949 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5950 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5951 if (error) {
5952 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5953 goto done;
5954 error = show_rebase_progress(commit, commit_id, NULL);
5955 } else {
5956 error = show_rebase_progress(commit, commit_id, new_commit_id);
5957 free(new_commit_id);
5959 done:
5960 got_object_commit_close(commit);
5961 return error;
5964 struct check_path_prefix_arg {
5965 const char *path_prefix;
5966 size_t len;
5967 int errcode;
5970 static const struct got_error *
5971 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5972 struct got_blob_object *blob2, struct got_object_id *id1,
5973 struct got_object_id *id2, const char *path1, const char *path2,
5974 mode_t mode1, mode_t mode2, struct got_repository *repo)
5976 struct check_path_prefix_arg *a = arg;
5978 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5979 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5980 return got_error(a->errcode);
5982 return NULL;
5985 static const struct got_error *
5986 check_path_prefix(struct got_object_id *parent_id,
5987 struct got_object_id *commit_id, const char *path_prefix,
5988 int errcode, struct got_repository *repo)
5990 const struct got_error *err;
5991 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5992 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5993 struct check_path_prefix_arg cpp_arg;
5995 if (got_path_is_root_dir(path_prefix))
5996 return NULL;
5998 err = got_object_open_as_commit(&commit, repo, commit_id);
5999 if (err)
6000 goto done;
6002 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6003 if (err)
6004 goto done;
6006 err = got_object_open_as_tree(&tree1, repo,
6007 got_object_commit_get_tree_id(parent_commit));
6008 if (err)
6009 goto done;
6011 err = got_object_open_as_tree(&tree2, repo,
6012 got_object_commit_get_tree_id(commit));
6013 if (err)
6014 goto done;
6016 cpp_arg.path_prefix = path_prefix;
6017 while (cpp_arg.path_prefix[0] == '/')
6018 cpp_arg.path_prefix++;
6019 cpp_arg.len = strlen(cpp_arg.path_prefix);
6020 cpp_arg.errcode = errcode;
6021 err = got_diff_tree(tree1, tree2, "", "", repo,
6022 check_path_prefix_in_diff, &cpp_arg, 0);
6023 done:
6024 if (tree1)
6025 got_object_tree_close(tree1);
6026 if (tree2)
6027 got_object_tree_close(tree2);
6028 if (commit)
6029 got_object_commit_close(commit);
6030 if (parent_commit)
6031 got_object_commit_close(parent_commit);
6032 return err;
6035 static const struct got_error *
6036 collect_commits(struct got_object_id_queue *commits,
6037 struct got_object_id *initial_commit_id,
6038 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6039 const char *path_prefix, int path_prefix_errcode,
6040 struct got_repository *repo)
6042 const struct got_error *err = NULL;
6043 struct got_commit_graph *graph = NULL;
6044 struct got_object_id *parent_id = NULL;
6045 struct got_object_qid *qid;
6046 struct got_object_id *commit_id = initial_commit_id;
6048 err = got_commit_graph_open(&graph, "/", 1);
6049 if (err)
6050 return err;
6052 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6053 check_cancelled, NULL);
6054 if (err)
6055 goto done;
6056 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6057 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6058 check_cancelled, NULL);
6059 if (err) {
6060 if (err->code == GOT_ERR_ITER_COMPLETED) {
6061 err = got_error_msg(GOT_ERR_ANCESTRY,
6062 "ran out of commits to rebase before "
6063 "youngest common ancestor commit has "
6064 "been reached?!?");
6066 goto done;
6067 } else {
6068 err = check_path_prefix(parent_id, commit_id,
6069 path_prefix, path_prefix_errcode, repo);
6070 if (err)
6071 goto done;
6073 err = got_object_qid_alloc(&qid, commit_id);
6074 if (err)
6075 goto done;
6076 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6077 commit_id = parent_id;
6080 done:
6081 got_commit_graph_close(graph);
6082 return err;
6085 static const struct got_error *
6086 cmd_rebase(int argc, char *argv[])
6088 const struct got_error *error = NULL;
6089 struct got_worktree *worktree = NULL;
6090 struct got_repository *repo = NULL;
6091 struct got_fileindex *fileindex = NULL;
6092 char *cwd = NULL;
6093 struct got_reference *branch = NULL;
6094 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6095 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6096 struct got_object_id *resume_commit_id = NULL;
6097 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6098 struct got_commit_object *commit = NULL;
6099 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6100 int histedit_in_progress = 0;
6101 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6102 struct got_object_id_queue commits;
6103 struct got_pathlist_head merged_paths;
6104 const struct got_object_id_queue *parent_ids;
6105 struct got_object_qid *qid, *pid;
6107 SIMPLEQ_INIT(&commits);
6108 TAILQ_INIT(&merged_paths);
6110 while ((ch = getopt(argc, argv, "ac")) != -1) {
6111 switch (ch) {
6112 case 'a':
6113 abort_rebase = 1;
6114 break;
6115 case 'c':
6116 continue_rebase = 1;
6117 break;
6118 default:
6119 usage_rebase();
6120 /* NOTREACHED */
6124 argc -= optind;
6125 argv += optind;
6127 #ifndef PROFILE
6128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6129 "unveil", NULL) == -1)
6130 err(1, "pledge");
6131 #endif
6132 if (abort_rebase && continue_rebase)
6133 usage_rebase();
6134 else if (abort_rebase || continue_rebase) {
6135 if (argc != 0)
6136 usage_rebase();
6137 } else if (argc != 1)
6138 usage_rebase();
6140 cwd = getcwd(NULL, 0);
6141 if (cwd == NULL) {
6142 error = got_error_from_errno("getcwd");
6143 goto done;
6145 error = got_worktree_open(&worktree, cwd);
6146 if (error)
6147 goto done;
6149 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6150 NULL);
6151 if (error != NULL)
6152 goto done;
6154 error = apply_unveil(got_repo_get_path(repo), 0,
6155 got_worktree_get_root_path(worktree));
6156 if (error)
6157 goto done;
6159 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6160 worktree);
6161 if (error)
6162 goto done;
6163 if (histedit_in_progress) {
6164 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6165 goto done;
6168 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6169 if (error)
6170 goto done;
6172 if (abort_rebase) {
6173 int did_something;
6174 if (!rebase_in_progress) {
6175 error = got_error(GOT_ERR_NOT_REBASING);
6176 goto done;
6178 error = got_worktree_rebase_continue(&resume_commit_id,
6179 &new_base_branch, &tmp_branch, &branch, &fileindex,
6180 worktree, repo);
6181 if (error)
6182 goto done;
6183 printf("Switching work tree to %s\n",
6184 got_ref_get_symref_target(new_base_branch));
6185 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6186 new_base_branch, update_progress, &did_something);
6187 if (error)
6188 goto done;
6189 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6190 goto done; /* nothing else to do */
6193 if (continue_rebase) {
6194 if (!rebase_in_progress) {
6195 error = got_error(GOT_ERR_NOT_REBASING);
6196 goto done;
6198 error = got_worktree_rebase_continue(&resume_commit_id,
6199 &new_base_branch, &tmp_branch, &branch, &fileindex,
6200 worktree, repo);
6201 if (error)
6202 goto done;
6204 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6205 resume_commit_id, repo);
6206 if (error)
6207 goto done;
6209 yca_id = got_object_id_dup(resume_commit_id);
6210 if (yca_id == NULL) {
6211 error = got_error_from_errno("got_object_id_dup");
6212 goto done;
6214 } else {
6215 error = got_ref_open(&branch, repo, argv[0], 0);
6216 if (error != NULL)
6217 goto done;
6220 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6221 if (error)
6222 goto done;
6224 if (!continue_rebase) {
6225 struct got_object_id *base_commit_id;
6227 base_commit_id = got_worktree_get_base_commit_id(worktree);
6228 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6229 base_commit_id, branch_head_commit_id, repo,
6230 check_cancelled, NULL);
6231 if (error)
6232 goto done;
6233 if (yca_id == NULL) {
6234 error = got_error_msg(GOT_ERR_ANCESTRY,
6235 "specified branch shares no common ancestry "
6236 "with work tree's branch");
6237 goto done;
6240 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6241 if (error) {
6242 if (error->code != GOT_ERR_ANCESTRY)
6243 goto done;
6244 error = NULL;
6245 } else {
6246 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6247 "specified branch resolves to a commit which "
6248 "is already contained in work tree's branch");
6249 goto done;
6251 error = got_worktree_rebase_prepare(&new_base_branch,
6252 &tmp_branch, &fileindex, worktree, branch, repo);
6253 if (error)
6254 goto done;
6257 commit_id = branch_head_commit_id;
6258 error = got_object_open_as_commit(&commit, repo, commit_id);
6259 if (error)
6260 goto done;
6262 parent_ids = got_object_commit_get_parent_ids(commit);
6263 pid = SIMPLEQ_FIRST(parent_ids);
6264 if (pid == NULL) {
6265 if (!continue_rebase) {
6266 int did_something;
6267 error = got_worktree_rebase_abort(worktree, fileindex,
6268 repo, new_base_branch, update_progress,
6269 &did_something);
6270 if (error)
6271 goto done;
6272 printf("Rebase of %s aborted\n",
6273 got_ref_get_name(branch));
6275 error = got_error(GOT_ERR_EMPTY_REBASE);
6276 goto done;
6278 error = collect_commits(&commits, commit_id, pid->id,
6279 yca_id, got_worktree_get_path_prefix(worktree),
6280 GOT_ERR_REBASE_PATH, repo);
6281 got_object_commit_close(commit);
6282 commit = NULL;
6283 if (error)
6284 goto done;
6286 if (SIMPLEQ_EMPTY(&commits)) {
6287 if (continue_rebase) {
6288 error = rebase_complete(worktree, fileindex,
6289 branch, new_base_branch, tmp_branch, repo);
6290 goto done;
6291 } else {
6292 /* Fast-forward the reference of the branch. */
6293 struct got_object_id *new_head_commit_id;
6294 char *id_str;
6295 error = got_ref_resolve(&new_head_commit_id, repo,
6296 new_base_branch);
6297 if (error)
6298 goto done;
6299 error = got_object_id_str(&id_str, new_head_commit_id);
6300 printf("Forwarding %s to commit %s\n",
6301 got_ref_get_name(branch), id_str);
6302 free(id_str);
6303 error = got_ref_change_ref(branch,
6304 new_head_commit_id);
6305 if (error)
6306 goto done;
6310 pid = NULL;
6311 SIMPLEQ_FOREACH(qid, &commits, entry) {
6312 commit_id = qid->id;
6313 parent_id = pid ? pid->id : yca_id;
6314 pid = qid;
6316 error = got_worktree_rebase_merge_files(&merged_paths,
6317 worktree, fileindex, parent_id, commit_id, repo,
6318 rebase_progress, &rebase_status, check_cancelled, NULL);
6319 if (error)
6320 goto done;
6322 if (rebase_status == GOT_STATUS_CONFLICT) {
6323 error = show_rebase_merge_conflict(qid->id, repo);
6324 if (error)
6325 goto done;
6326 got_worktree_rebase_pathlist_free(&merged_paths);
6327 break;
6330 error = rebase_commit(&merged_paths, worktree, fileindex,
6331 tmp_branch, commit_id, repo);
6332 got_worktree_rebase_pathlist_free(&merged_paths);
6333 if (error)
6334 goto done;
6337 if (rebase_status == GOT_STATUS_CONFLICT) {
6338 error = got_worktree_rebase_postpone(worktree, fileindex);
6339 if (error)
6340 goto done;
6341 error = got_error_msg(GOT_ERR_CONFLICTS,
6342 "conflicts must be resolved before rebasing can continue");
6343 } else
6344 error = rebase_complete(worktree, fileindex, branch,
6345 new_base_branch, tmp_branch, repo);
6346 done:
6347 got_object_id_queue_free(&commits);
6348 free(branch_head_commit_id);
6349 free(resume_commit_id);
6350 free(yca_id);
6351 if (commit)
6352 got_object_commit_close(commit);
6353 if (branch)
6354 got_ref_close(branch);
6355 if (new_base_branch)
6356 got_ref_close(new_base_branch);
6357 if (tmp_branch)
6358 got_ref_close(tmp_branch);
6359 if (worktree)
6360 got_worktree_close(worktree);
6361 if (repo)
6362 got_repo_close(repo);
6363 return error;
6366 __dead static void
6367 usage_histedit(void)
6369 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6370 getprogname());
6371 exit(1);
6374 #define GOT_HISTEDIT_PICK 'p'
6375 #define GOT_HISTEDIT_EDIT 'e'
6376 #define GOT_HISTEDIT_FOLD 'f'
6377 #define GOT_HISTEDIT_DROP 'd'
6378 #define GOT_HISTEDIT_MESG 'm'
6380 static struct got_histedit_cmd {
6381 unsigned char code;
6382 const char *name;
6383 const char *desc;
6384 } got_histedit_cmds[] = {
6385 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6386 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6387 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6388 "be used" },
6389 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6390 { GOT_HISTEDIT_MESG, "mesg",
6391 "single-line log message for commit above (open editor if empty)" },
6394 struct got_histedit_list_entry {
6395 TAILQ_ENTRY(got_histedit_list_entry) entry;
6396 struct got_object_id *commit_id;
6397 const struct got_histedit_cmd *cmd;
6398 char *logmsg;
6400 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6402 static const struct got_error *
6403 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6404 FILE *f, struct got_repository *repo)
6406 const struct got_error *err = NULL;
6407 char *logmsg = NULL, *id_str = NULL;
6408 struct got_commit_object *commit = NULL;
6409 int n;
6411 err = got_object_open_as_commit(&commit, repo, commit_id);
6412 if (err)
6413 goto done;
6415 err = get_short_logmsg(&logmsg, 34, commit);
6416 if (err)
6417 goto done;
6419 err = got_object_id_str(&id_str, commit_id);
6420 if (err)
6421 goto done;
6423 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6424 if (n < 0)
6425 err = got_ferror(f, GOT_ERR_IO);
6426 done:
6427 if (commit)
6428 got_object_commit_close(commit);
6429 free(id_str);
6430 free(logmsg);
6431 return err;
6434 static const struct got_error *
6435 histedit_write_commit_list(struct got_object_id_queue *commits,
6436 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6438 const struct got_error *err = NULL;
6439 struct got_object_qid *qid;
6441 if (SIMPLEQ_EMPTY(commits))
6442 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6444 SIMPLEQ_FOREACH(qid, commits, entry) {
6445 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6446 f, repo);
6447 if (err)
6448 break;
6449 if (edit_logmsg_only) {
6450 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6451 if (n < 0) {
6452 err = got_ferror(f, GOT_ERR_IO);
6453 break;
6458 return err;
6461 static const struct got_error *
6462 write_cmd_list(FILE *f, const char *branch_name,
6463 struct got_object_id_queue *commits)
6465 const struct got_error *err = NULL;
6466 int n, i;
6467 char *id_str;
6468 struct got_object_qid *qid;
6470 qid = SIMPLEQ_FIRST(commits);
6471 err = got_object_id_str(&id_str, qid->id);
6472 if (err)
6473 return err;
6475 n = fprintf(f,
6476 "# Editing the history of branch '%s' starting at\n"
6477 "# commit %s\n"
6478 "# Commits will be processed in order from top to "
6479 "bottom of this file.\n", branch_name, id_str);
6480 if (n < 0) {
6481 err = got_ferror(f, GOT_ERR_IO);
6482 goto done;
6485 n = fprintf(f, "# Available histedit commands:\n");
6486 if (n < 0) {
6487 err = got_ferror(f, GOT_ERR_IO);
6488 goto done;
6491 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6492 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6493 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6494 cmd->desc);
6495 if (n < 0) {
6496 err = got_ferror(f, GOT_ERR_IO);
6497 break;
6500 done:
6501 free(id_str);
6502 return err;
6505 static const struct got_error *
6506 histedit_syntax_error(int lineno)
6508 static char msg[42];
6509 int ret;
6511 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6512 lineno);
6513 if (ret == -1 || ret >= sizeof(msg))
6514 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6516 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6519 static const struct got_error *
6520 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6521 char *logmsg, struct got_repository *repo)
6523 const struct got_error *err;
6524 struct got_commit_object *folded_commit = NULL;
6525 char *id_str, *folded_logmsg = NULL;
6527 err = got_object_id_str(&id_str, hle->commit_id);
6528 if (err)
6529 return err;
6531 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6532 if (err)
6533 goto done;
6535 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6536 if (err)
6537 goto done;
6538 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6539 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6540 folded_logmsg) == -1) {
6541 err = got_error_from_errno("asprintf");
6543 done:
6544 if (folded_commit)
6545 got_object_commit_close(folded_commit);
6546 free(id_str);
6547 free(folded_logmsg);
6548 return err;
6551 static struct got_histedit_list_entry *
6552 get_folded_commits(struct got_histedit_list_entry *hle)
6554 struct got_histedit_list_entry *prev, *folded = NULL;
6556 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6557 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6558 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6559 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6560 folded = prev;
6561 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6564 return folded;
6567 static const struct got_error *
6568 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6569 struct got_repository *repo)
6571 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6572 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6573 const struct got_error *err = NULL;
6574 struct got_commit_object *commit = NULL;
6575 int fd;
6576 struct got_histedit_list_entry *folded = NULL;
6578 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6579 if (err)
6580 return err;
6582 folded = get_folded_commits(hle);
6583 if (folded) {
6584 while (folded != hle) {
6585 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6586 folded = TAILQ_NEXT(folded, entry);
6587 continue;
6589 err = append_folded_commit_msg(&new_msg, folded,
6590 logmsg, repo);
6591 if (err)
6592 goto done;
6593 free(logmsg);
6594 logmsg = new_msg;
6595 folded = TAILQ_NEXT(folded, entry);
6599 err = got_object_id_str(&id_str, hle->commit_id);
6600 if (err)
6601 goto done;
6602 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6603 if (err)
6604 goto done;
6605 if (asprintf(&new_msg,
6606 "%s\n# original log message of commit %s: %s",
6607 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6608 err = got_error_from_errno("asprintf");
6609 goto done;
6611 free(logmsg);
6612 logmsg = new_msg;
6614 err = got_object_id_str(&id_str, hle->commit_id);
6615 if (err)
6616 goto done;
6618 err = got_opentemp_named_fd(&logmsg_path, &fd,
6619 GOT_TMPDIR_STR "/got-logmsg");
6620 if (err)
6621 goto done;
6623 dprintf(fd, logmsg);
6624 close(fd);
6626 err = get_editor(&editor);
6627 if (err)
6628 goto done;
6630 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6631 if (err) {
6632 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6633 goto done;
6634 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6636 done:
6637 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6638 err = got_error_from_errno2("unlink", logmsg_path);
6639 free(logmsg_path);
6640 free(logmsg);
6641 free(orig_logmsg);
6642 free(editor);
6643 if (commit)
6644 got_object_commit_close(commit);
6645 return err;
6648 static const struct got_error *
6649 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6650 FILE *f, struct got_repository *repo)
6652 const struct got_error *err = NULL;
6653 char *line = NULL, *p, *end;
6654 size_t size;
6655 ssize_t len;
6656 int lineno = 0, i;
6657 const struct got_histedit_cmd *cmd;
6658 struct got_object_id *commit_id = NULL;
6659 struct got_histedit_list_entry *hle = NULL;
6661 for (;;) {
6662 len = getline(&line, &size, f);
6663 if (len == -1) {
6664 const struct got_error *getline_err;
6665 if (feof(f))
6666 break;
6667 getline_err = got_error_from_errno("getline");
6668 err = got_ferror(f, getline_err->code);
6669 break;
6671 lineno++;
6672 p = line;
6673 while (isspace((unsigned char)p[0]))
6674 p++;
6675 if (p[0] == '#' || p[0] == '\0') {
6676 free(line);
6677 line = NULL;
6678 continue;
6680 cmd = NULL;
6681 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6682 cmd = &got_histedit_cmds[i];
6683 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6684 isspace((unsigned char)p[strlen(cmd->name)])) {
6685 p += strlen(cmd->name);
6686 break;
6688 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6689 p++;
6690 break;
6693 if (i == nitems(got_histedit_cmds)) {
6694 err = histedit_syntax_error(lineno);
6695 break;
6697 while (isspace((unsigned char)p[0]))
6698 p++;
6699 if (cmd->code == GOT_HISTEDIT_MESG) {
6700 if (hle == NULL || hle->logmsg != NULL) {
6701 err = got_error(GOT_ERR_HISTEDIT_CMD);
6702 break;
6704 if (p[0] == '\0') {
6705 err = histedit_edit_logmsg(hle, repo);
6706 if (err)
6707 break;
6708 } else {
6709 hle->logmsg = strdup(p);
6710 if (hle->logmsg == NULL) {
6711 err = got_error_from_errno("strdup");
6712 break;
6715 free(line);
6716 line = NULL;
6717 continue;
6718 } else {
6719 end = p;
6720 while (end[0] && !isspace((unsigned char)end[0]))
6721 end++;
6722 *end = '\0';
6724 err = got_object_resolve_id_str(&commit_id, repo, p);
6725 if (err) {
6726 /* override error code */
6727 err = histedit_syntax_error(lineno);
6728 break;
6731 hle = malloc(sizeof(*hle));
6732 if (hle == NULL) {
6733 err = got_error_from_errno("malloc");
6734 break;
6736 hle->cmd = cmd;
6737 hle->commit_id = commit_id;
6738 hle->logmsg = NULL;
6739 commit_id = NULL;
6740 free(line);
6741 line = NULL;
6742 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6745 free(line);
6746 free(commit_id);
6747 return err;
6750 static const struct got_error *
6751 histedit_check_script(struct got_histedit_list *histedit_cmds,
6752 struct got_object_id_queue *commits, struct got_repository *repo)
6754 const struct got_error *err = NULL;
6755 struct got_object_qid *qid;
6756 struct got_histedit_list_entry *hle;
6757 static char msg[92];
6758 char *id_str;
6760 if (TAILQ_EMPTY(histedit_cmds))
6761 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6762 "histedit script contains no commands");
6763 if (SIMPLEQ_EMPTY(commits))
6764 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6766 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6767 struct got_histedit_list_entry *hle2;
6768 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6769 if (hle == hle2)
6770 continue;
6771 if (got_object_id_cmp(hle->commit_id,
6772 hle2->commit_id) != 0)
6773 continue;
6774 err = got_object_id_str(&id_str, hle->commit_id);
6775 if (err)
6776 return err;
6777 snprintf(msg, sizeof(msg), "commit %s is listed "
6778 "more than once in histedit script", id_str);
6779 free(id_str);
6780 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6784 SIMPLEQ_FOREACH(qid, commits, entry) {
6785 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6786 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6787 break;
6789 if (hle == NULL) {
6790 err = got_object_id_str(&id_str, qid->id);
6791 if (err)
6792 return err;
6793 snprintf(msg, sizeof(msg),
6794 "commit %s missing from histedit script", id_str);
6795 free(id_str);
6796 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6800 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6801 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6802 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6803 "last commit in histedit script cannot be folded");
6805 return NULL;
6808 static const struct got_error *
6809 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6810 const char *path, struct got_object_id_queue *commits,
6811 struct got_repository *repo)
6813 const struct got_error *err = NULL;
6814 char *editor;
6815 FILE *f = NULL;
6817 err = get_editor(&editor);
6818 if (err)
6819 return err;
6821 if (spawn_editor(editor, path) == -1) {
6822 err = got_error_from_errno("failed spawning editor");
6823 goto done;
6826 f = fopen(path, "r");
6827 if (f == NULL) {
6828 err = got_error_from_errno("fopen");
6829 goto done;
6831 err = histedit_parse_list(histedit_cmds, f, repo);
6832 if (err)
6833 goto done;
6835 err = histedit_check_script(histedit_cmds, commits, repo);
6836 done:
6837 if (f && fclose(f) != 0 && err == NULL)
6838 err = got_error_from_errno("fclose");
6839 free(editor);
6840 return err;
6843 static const struct got_error *
6844 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6845 struct got_object_id_queue *, const char *, const char *,
6846 struct got_repository *);
6848 static const struct got_error *
6849 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6850 struct got_object_id_queue *commits, const char *branch_name,
6851 int edit_logmsg_only, struct got_repository *repo)
6853 const struct got_error *err;
6854 FILE *f = NULL;
6855 char *path = NULL;
6857 err = got_opentemp_named(&path, &f, "got-histedit");
6858 if (err)
6859 return err;
6861 err = write_cmd_list(f, branch_name, commits);
6862 if (err)
6863 goto done;
6865 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6866 if (err)
6867 goto done;
6869 if (edit_logmsg_only) {
6870 rewind(f);
6871 err = histedit_parse_list(histedit_cmds, f, repo);
6872 } else {
6873 if (fclose(f) != 0) {
6874 err = got_error_from_errno("fclose");
6875 goto done;
6877 f = NULL;
6878 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6879 if (err) {
6880 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6881 err->code != GOT_ERR_HISTEDIT_CMD)
6882 goto done;
6883 err = histedit_edit_list_retry(histedit_cmds, err,
6884 commits, path, branch_name, repo);
6887 done:
6888 if (f && fclose(f) != 0 && err == NULL)
6889 err = got_error_from_errno("fclose");
6890 if (path && unlink(path) != 0 && err == NULL)
6891 err = got_error_from_errno2("unlink", path);
6892 free(path);
6893 return err;
6896 static const struct got_error *
6897 histedit_save_list(struct got_histedit_list *histedit_cmds,
6898 struct got_worktree *worktree, struct got_repository *repo)
6900 const struct got_error *err = NULL;
6901 char *path = NULL;
6902 FILE *f = NULL;
6903 struct got_histedit_list_entry *hle;
6904 struct got_commit_object *commit = NULL;
6906 err = got_worktree_get_histedit_script_path(&path, worktree);
6907 if (err)
6908 return err;
6910 f = fopen(path, "w");
6911 if (f == NULL) {
6912 err = got_error_from_errno2("fopen", path);
6913 goto done;
6915 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6916 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6917 repo);
6918 if (err)
6919 break;
6921 if (hle->logmsg) {
6922 int n = fprintf(f, "%c %s\n",
6923 GOT_HISTEDIT_MESG, hle->logmsg);
6924 if (n < 0) {
6925 err = got_ferror(f, GOT_ERR_IO);
6926 break;
6930 done:
6931 if (f && fclose(f) != 0 && err == NULL)
6932 err = got_error_from_errno("fclose");
6933 free(path);
6934 if (commit)
6935 got_object_commit_close(commit);
6936 return err;
6939 void
6940 histedit_free_list(struct got_histedit_list *histedit_cmds)
6942 struct got_histedit_list_entry *hle;
6944 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6945 TAILQ_REMOVE(histedit_cmds, hle, entry);
6946 free(hle);
6950 static const struct got_error *
6951 histedit_load_list(struct got_histedit_list *histedit_cmds,
6952 const char *path, struct got_repository *repo)
6954 const struct got_error *err = NULL;
6955 FILE *f = NULL;
6957 f = fopen(path, "r");
6958 if (f == NULL) {
6959 err = got_error_from_errno2("fopen", path);
6960 goto done;
6963 err = histedit_parse_list(histedit_cmds, f, repo);
6964 done:
6965 if (f && fclose(f) != 0 && err == NULL)
6966 err = got_error_from_errno("fclose");
6967 return err;
6970 static const struct got_error *
6971 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6972 const struct got_error *edit_err, struct got_object_id_queue *commits,
6973 const char *path, const char *branch_name, struct got_repository *repo)
6975 const struct got_error *err = NULL, *prev_err = edit_err;
6976 int resp = ' ';
6978 while (resp != 'c' && resp != 'r' && resp != 'a') {
6979 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6980 "or (a)bort: ", getprogname(), prev_err->msg);
6981 resp = getchar();
6982 if (resp == '\n')
6983 resp = getchar();
6984 if (resp == 'c') {
6985 histedit_free_list(histedit_cmds);
6986 err = histedit_run_editor(histedit_cmds, path, commits,
6987 repo);
6988 if (err) {
6989 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6990 err->code != GOT_ERR_HISTEDIT_CMD)
6991 break;
6992 prev_err = err;
6993 resp = ' ';
6994 continue;
6996 break;
6997 } else if (resp == 'r') {
6998 histedit_free_list(histedit_cmds);
6999 err = histedit_edit_script(histedit_cmds,
7000 commits, branch_name, 0, repo);
7001 if (err) {
7002 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7003 err->code != GOT_ERR_HISTEDIT_CMD)
7004 break;
7005 prev_err = err;
7006 resp = ' ';
7007 continue;
7009 break;
7010 } else if (resp == 'a') {
7011 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7012 break;
7013 } else
7014 printf("invalid response '%c'\n", resp);
7017 return err;
7020 static const struct got_error *
7021 histedit_complete(struct got_worktree *worktree,
7022 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7023 struct got_reference *branch, struct got_repository *repo)
7025 printf("Switching work tree to %s\n",
7026 got_ref_get_symref_target(branch));
7027 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7028 branch, repo);
7031 static const struct got_error *
7032 show_histedit_progress(struct got_commit_object *commit,
7033 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7035 const struct got_error *err;
7036 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7038 err = got_object_id_str(&old_id_str, hle->commit_id);
7039 if (err)
7040 goto done;
7042 if (new_id) {
7043 err = got_object_id_str(&new_id_str, new_id);
7044 if (err)
7045 goto done;
7048 old_id_str[12] = '\0';
7049 if (new_id_str)
7050 new_id_str[12] = '\0';
7052 if (hle->logmsg) {
7053 logmsg = strdup(hle->logmsg);
7054 if (logmsg == NULL) {
7055 err = got_error_from_errno("strdup");
7056 goto done;
7058 trim_logmsg(logmsg, 42);
7059 } else {
7060 err = get_short_logmsg(&logmsg, 42, commit);
7061 if (err)
7062 goto done;
7065 switch (hle->cmd->code) {
7066 case GOT_HISTEDIT_PICK:
7067 case GOT_HISTEDIT_EDIT:
7068 printf("%s -> %s: %s\n", old_id_str,
7069 new_id_str ? new_id_str : "no-op change", logmsg);
7070 break;
7071 case GOT_HISTEDIT_DROP:
7072 case GOT_HISTEDIT_FOLD:
7073 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7074 logmsg);
7075 break;
7076 default:
7077 break;
7079 done:
7080 free(old_id_str);
7081 free(new_id_str);
7082 return err;
7085 static const struct got_error *
7086 histedit_commit(struct got_pathlist_head *merged_paths,
7087 struct got_worktree *worktree, struct got_fileindex *fileindex,
7088 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7089 struct got_repository *repo)
7091 const struct got_error *err;
7092 struct got_commit_object *commit;
7093 struct got_object_id *new_commit_id;
7095 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7096 && hle->logmsg == NULL) {
7097 err = histedit_edit_logmsg(hle, repo);
7098 if (err)
7099 return err;
7102 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7103 if (err)
7104 return err;
7106 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7107 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7108 hle->logmsg, repo);
7109 if (err) {
7110 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7111 goto done;
7112 err = show_histedit_progress(commit, hle, NULL);
7113 } else {
7114 err = show_histedit_progress(commit, hle, new_commit_id);
7115 free(new_commit_id);
7117 done:
7118 got_object_commit_close(commit);
7119 return err;
7122 static const struct got_error *
7123 histedit_skip_commit(struct got_histedit_list_entry *hle,
7124 struct got_worktree *worktree, struct got_repository *repo)
7126 const struct got_error *error;
7127 struct got_commit_object *commit;
7129 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7130 repo);
7131 if (error)
7132 return error;
7134 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7135 if (error)
7136 return error;
7138 error = show_histedit_progress(commit, hle, NULL);
7139 got_object_commit_close(commit);
7140 return error;
7143 static const struct got_error *
7144 check_local_changes(void *arg, unsigned char status,
7145 unsigned char staged_status, const char *path,
7146 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7147 struct got_object_id *commit_id, int dirfd, const char *de_name)
7149 int *have_local_changes = arg;
7151 switch (status) {
7152 case GOT_STATUS_ADD:
7153 case GOT_STATUS_DELETE:
7154 case GOT_STATUS_MODIFY:
7155 case GOT_STATUS_CONFLICT:
7156 *have_local_changes = 1;
7157 return got_error(GOT_ERR_CANCELLED);
7158 default:
7159 break;
7162 switch (staged_status) {
7163 case GOT_STATUS_ADD:
7164 case GOT_STATUS_DELETE:
7165 case GOT_STATUS_MODIFY:
7166 *have_local_changes = 1;
7167 return got_error(GOT_ERR_CANCELLED);
7168 default:
7169 break;
7172 return NULL;
7175 static const struct got_error *
7176 cmd_histedit(int argc, char *argv[])
7178 const struct got_error *error = NULL;
7179 struct got_worktree *worktree = NULL;
7180 struct got_fileindex *fileindex = NULL;
7181 struct got_repository *repo = NULL;
7182 char *cwd = NULL;
7183 struct got_reference *branch = NULL;
7184 struct got_reference *tmp_branch = NULL;
7185 struct got_object_id *resume_commit_id = NULL;
7186 struct got_object_id *base_commit_id = NULL;
7187 struct got_object_id *head_commit_id = NULL;
7188 struct got_commit_object *commit = NULL;
7189 int ch, rebase_in_progress = 0, did_something;
7190 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7191 int edit_logmsg_only = 0;
7192 const char *edit_script_path = NULL;
7193 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7194 struct got_object_id_queue commits;
7195 struct got_pathlist_head merged_paths;
7196 const struct got_object_id_queue *parent_ids;
7197 struct got_object_qid *pid;
7198 struct got_histedit_list histedit_cmds;
7199 struct got_histedit_list_entry *hle;
7201 SIMPLEQ_INIT(&commits);
7202 TAILQ_INIT(&histedit_cmds);
7203 TAILQ_INIT(&merged_paths);
7205 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7206 switch (ch) {
7207 case 'a':
7208 abort_edit = 1;
7209 break;
7210 case 'c':
7211 continue_edit = 1;
7212 break;
7213 case 'F':
7214 edit_script_path = optarg;
7215 break;
7216 case 'm':
7217 edit_logmsg_only = 1;
7218 break;
7219 default:
7220 usage_histedit();
7221 /* NOTREACHED */
7225 argc -= optind;
7226 argv += optind;
7228 #ifndef PROFILE
7229 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7230 "unveil", NULL) == -1)
7231 err(1, "pledge");
7232 #endif
7233 if (abort_edit && continue_edit)
7234 errx(1, "histedit's -a and -c options are mutually exclusive");
7235 if (edit_script_path && edit_logmsg_only)
7236 errx(1, "histedit's -F and -m options are mutually exclusive");
7237 if (abort_edit && edit_logmsg_only)
7238 errx(1, "histedit's -a and -m options are mutually exclusive");
7239 if (continue_edit && edit_logmsg_only)
7240 errx(1, "histedit's -c and -m options are mutually exclusive");
7241 if (argc != 0)
7242 usage_histedit();
7245 * This command cannot apply unveil(2) in all cases because the
7246 * user may choose to run an editor to edit the histedit script
7247 * and to edit individual commit log messages.
7248 * unveil(2) traverses exec(2); if an editor is used we have to
7249 * apply unveil after edit script and log messages have been written.
7250 * XXX TODO: Make use of unveil(2) where possible.
7253 cwd = getcwd(NULL, 0);
7254 if (cwd == NULL) {
7255 error = got_error_from_errno("getcwd");
7256 goto done;
7258 error = got_worktree_open(&worktree, cwd);
7259 if (error)
7260 goto done;
7262 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7263 NULL);
7264 if (error != NULL)
7265 goto done;
7267 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7268 if (error)
7269 goto done;
7270 if (rebase_in_progress) {
7271 error = got_error(GOT_ERR_REBASING);
7272 goto done;
7275 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7276 if (error)
7277 goto done;
7279 if (edit_in_progress && edit_logmsg_only) {
7280 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7281 "histedit operation is in progress in this "
7282 "work tree and must be continued or aborted "
7283 "before the -m option can be used");
7284 goto done;
7287 if (edit_in_progress && abort_edit) {
7288 error = got_worktree_histedit_continue(&resume_commit_id,
7289 &tmp_branch, &branch, &base_commit_id, &fileindex,
7290 worktree, repo);
7291 if (error)
7292 goto done;
7293 printf("Switching work tree to %s\n",
7294 got_ref_get_symref_target(branch));
7295 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7296 branch, base_commit_id, update_progress, &did_something);
7297 if (error)
7298 goto done;
7299 printf("Histedit of %s aborted\n",
7300 got_ref_get_symref_target(branch));
7301 goto done; /* nothing else to do */
7302 } else if (abort_edit) {
7303 error = got_error(GOT_ERR_NOT_HISTEDIT);
7304 goto done;
7307 if (continue_edit) {
7308 char *path;
7310 if (!edit_in_progress) {
7311 error = got_error(GOT_ERR_NOT_HISTEDIT);
7312 goto done;
7315 error = got_worktree_get_histedit_script_path(&path, worktree);
7316 if (error)
7317 goto done;
7319 error = histedit_load_list(&histedit_cmds, path, repo);
7320 free(path);
7321 if (error)
7322 goto done;
7324 error = got_worktree_histedit_continue(&resume_commit_id,
7325 &tmp_branch, &branch, &base_commit_id, &fileindex,
7326 worktree, repo);
7327 if (error)
7328 goto done;
7330 error = got_ref_resolve(&head_commit_id, repo, branch);
7331 if (error)
7332 goto done;
7334 error = got_object_open_as_commit(&commit, repo,
7335 head_commit_id);
7336 if (error)
7337 goto done;
7338 parent_ids = got_object_commit_get_parent_ids(commit);
7339 pid = SIMPLEQ_FIRST(parent_ids);
7340 if (pid == NULL) {
7341 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7342 goto done;
7344 error = collect_commits(&commits, head_commit_id, pid->id,
7345 base_commit_id, got_worktree_get_path_prefix(worktree),
7346 GOT_ERR_HISTEDIT_PATH, repo);
7347 got_object_commit_close(commit);
7348 commit = NULL;
7349 if (error)
7350 goto done;
7351 } else {
7352 if (edit_in_progress) {
7353 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7354 goto done;
7357 error = got_ref_open(&branch, repo,
7358 got_worktree_get_head_ref_name(worktree), 0);
7359 if (error != NULL)
7360 goto done;
7362 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7363 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7364 "will not edit commit history of a branch outside "
7365 "the \"refs/heads/\" reference namespace");
7366 goto done;
7369 error = got_ref_resolve(&head_commit_id, repo, branch);
7370 got_ref_close(branch);
7371 branch = NULL;
7372 if (error)
7373 goto done;
7375 error = got_object_open_as_commit(&commit, repo,
7376 head_commit_id);
7377 if (error)
7378 goto done;
7379 parent_ids = got_object_commit_get_parent_ids(commit);
7380 pid = SIMPLEQ_FIRST(parent_ids);
7381 if (pid == NULL) {
7382 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7383 goto done;
7385 error = collect_commits(&commits, head_commit_id, pid->id,
7386 got_worktree_get_base_commit_id(worktree),
7387 got_worktree_get_path_prefix(worktree),
7388 GOT_ERR_HISTEDIT_PATH, repo);
7389 got_object_commit_close(commit);
7390 commit = NULL;
7391 if (error)
7392 goto done;
7394 if (SIMPLEQ_EMPTY(&commits)) {
7395 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7396 goto done;
7399 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7400 &base_commit_id, &fileindex, worktree, repo);
7401 if (error)
7402 goto done;
7404 if (edit_script_path) {
7405 error = histedit_load_list(&histedit_cmds,
7406 edit_script_path, repo);
7407 if (error) {
7408 got_worktree_histedit_abort(worktree, fileindex,
7409 repo, branch, base_commit_id,
7410 update_progress, &did_something);
7411 goto done;
7413 } else {
7414 const char *branch_name;
7415 branch_name = got_ref_get_symref_target(branch);
7416 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7417 branch_name += 11;
7418 error = histedit_edit_script(&histedit_cmds, &commits,
7419 branch_name, edit_logmsg_only, repo);
7420 if (error) {
7421 got_worktree_histedit_abort(worktree, fileindex,
7422 repo, branch, base_commit_id,
7423 update_progress, &did_something);
7424 goto done;
7429 error = histedit_save_list(&histedit_cmds, worktree,
7430 repo);
7431 if (error) {
7432 got_worktree_histedit_abort(worktree, fileindex,
7433 repo, branch, base_commit_id,
7434 update_progress, &did_something);
7435 goto done;
7440 error = histedit_check_script(&histedit_cmds, &commits, repo);
7441 if (error)
7442 goto done;
7444 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7445 if (resume_commit_id) {
7446 if (got_object_id_cmp(hle->commit_id,
7447 resume_commit_id) != 0)
7448 continue;
7450 resume_commit_id = NULL;
7451 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7452 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7453 error = histedit_skip_commit(hle, worktree,
7454 repo);
7455 if (error)
7456 goto done;
7457 } else {
7458 struct got_pathlist_head paths;
7459 int have_changes = 0;
7461 TAILQ_INIT(&paths);
7462 error = got_pathlist_append(&paths, "", NULL);
7463 if (error)
7464 goto done;
7465 error = got_worktree_status(worktree, &paths,
7466 repo, check_local_changes, &have_changes,
7467 check_cancelled, NULL);
7468 got_pathlist_free(&paths);
7469 if (error) {
7470 if (error->code != GOT_ERR_CANCELLED)
7471 goto done;
7472 if (sigint_received || sigpipe_received)
7473 goto done;
7475 if (have_changes) {
7476 error = histedit_commit(NULL, worktree,
7477 fileindex, tmp_branch, hle, repo);
7478 if (error)
7479 goto done;
7480 } else {
7481 error = got_object_open_as_commit(
7482 &commit, repo, hle->commit_id);
7483 if (error)
7484 goto done;
7485 error = show_histedit_progress(commit,
7486 hle, NULL);
7487 got_object_commit_close(commit);
7488 commit = NULL;
7489 if (error)
7490 goto done;
7493 continue;
7496 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7497 error = histedit_skip_commit(hle, worktree, repo);
7498 if (error)
7499 goto done;
7500 continue;
7503 error = got_object_open_as_commit(&commit, repo,
7504 hle->commit_id);
7505 if (error)
7506 goto done;
7507 parent_ids = got_object_commit_get_parent_ids(commit);
7508 pid = SIMPLEQ_FIRST(parent_ids);
7510 error = got_worktree_histedit_merge_files(&merged_paths,
7511 worktree, fileindex, pid->id, hle->commit_id, repo,
7512 rebase_progress, &rebase_status, check_cancelled, NULL);
7513 if (error)
7514 goto done;
7515 got_object_commit_close(commit);
7516 commit = NULL;
7518 if (rebase_status == GOT_STATUS_CONFLICT) {
7519 error = show_rebase_merge_conflict(hle->commit_id,
7520 repo);
7521 if (error)
7522 goto done;
7523 got_worktree_rebase_pathlist_free(&merged_paths);
7524 break;
7527 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7528 char *id_str;
7529 error = got_object_id_str(&id_str, hle->commit_id);
7530 if (error)
7531 goto done;
7532 printf("Stopping histedit for amending commit %s\n",
7533 id_str);
7534 free(id_str);
7535 got_worktree_rebase_pathlist_free(&merged_paths);
7536 error = got_worktree_histedit_postpone(worktree,
7537 fileindex);
7538 goto done;
7541 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7542 error = histedit_skip_commit(hle, worktree, repo);
7543 if (error)
7544 goto done;
7545 continue;
7548 error = histedit_commit(&merged_paths, worktree, fileindex,
7549 tmp_branch, hle, repo);
7550 got_worktree_rebase_pathlist_free(&merged_paths);
7551 if (error)
7552 goto done;
7555 if (rebase_status == GOT_STATUS_CONFLICT) {
7556 error = got_worktree_histedit_postpone(worktree, fileindex);
7557 if (error)
7558 goto done;
7559 error = got_error_msg(GOT_ERR_CONFLICTS,
7560 "conflicts must be resolved before histedit can continue");
7561 } else
7562 error = histedit_complete(worktree, fileindex, tmp_branch,
7563 branch, repo);
7564 done:
7565 got_object_id_queue_free(&commits);
7566 histedit_free_list(&histedit_cmds);
7567 free(head_commit_id);
7568 free(base_commit_id);
7569 free(resume_commit_id);
7570 if (commit)
7571 got_object_commit_close(commit);
7572 if (branch)
7573 got_ref_close(branch);
7574 if (tmp_branch)
7575 got_ref_close(tmp_branch);
7576 if (worktree)
7577 got_worktree_close(worktree);
7578 if (repo)
7579 got_repo_close(repo);
7580 return error;
7583 __dead static void
7584 usage_integrate(void)
7586 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7587 exit(1);
7590 static const struct got_error *
7591 cmd_integrate(int argc, char *argv[])
7593 const struct got_error *error = NULL;
7594 struct got_repository *repo = NULL;
7595 struct got_worktree *worktree = NULL;
7596 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7597 const char *branch_arg = NULL;
7598 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7599 struct got_fileindex *fileindex = NULL;
7600 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7601 int ch, did_something = 0;
7603 while ((ch = getopt(argc, argv, "")) != -1) {
7604 switch (ch) {
7605 default:
7606 usage_integrate();
7607 /* NOTREACHED */
7611 argc -= optind;
7612 argv += optind;
7614 if (argc != 1)
7615 usage_integrate();
7616 branch_arg = argv[0];
7618 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7619 "unveil", NULL) == -1)
7620 err(1, "pledge");
7622 cwd = getcwd(NULL, 0);
7623 if (cwd == NULL) {
7624 error = got_error_from_errno("getcwd");
7625 goto done;
7628 error = got_worktree_open(&worktree, cwd);
7629 if (error)
7630 goto done;
7632 error = check_rebase_or_histedit_in_progress(worktree);
7633 if (error)
7634 goto done;
7636 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7637 NULL);
7638 if (error != NULL)
7639 goto done;
7641 error = apply_unveil(got_repo_get_path(repo), 0,
7642 got_worktree_get_root_path(worktree));
7643 if (error)
7644 goto done;
7646 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7647 error = got_error_from_errno("asprintf");
7648 goto done;
7651 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7652 &base_branch_ref, worktree, refname, repo);
7653 if (error)
7654 goto done;
7656 refname = strdup(got_ref_get_name(branch_ref));
7657 if (refname == NULL) {
7658 error = got_error_from_errno("strdup");
7659 got_worktree_integrate_abort(worktree, fileindex, repo,
7660 branch_ref, base_branch_ref);
7661 goto done;
7663 base_refname = strdup(got_ref_get_name(base_branch_ref));
7664 if (base_refname == NULL) {
7665 error = got_error_from_errno("strdup");
7666 got_worktree_integrate_abort(worktree, fileindex, repo,
7667 branch_ref, base_branch_ref);
7668 goto done;
7671 error = got_ref_resolve(&commit_id, repo, branch_ref);
7672 if (error)
7673 goto done;
7675 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7676 if (error)
7677 goto done;
7679 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7680 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7681 "specified branch has already been integrated");
7682 got_worktree_integrate_abort(worktree, fileindex, repo,
7683 branch_ref, base_branch_ref);
7684 goto done;
7687 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7688 if (error) {
7689 if (error->code == GOT_ERR_ANCESTRY)
7690 error = got_error(GOT_ERR_REBASE_REQUIRED);
7691 got_worktree_integrate_abort(worktree, fileindex, repo,
7692 branch_ref, base_branch_ref);
7693 goto done;
7696 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7697 branch_ref, base_branch_ref, update_progress, &did_something,
7698 check_cancelled, NULL);
7699 if (error)
7700 goto done;
7702 printf("Integrated %s into %s\n", refname, base_refname);
7703 done:
7704 if (repo)
7705 got_repo_close(repo);
7706 if (worktree)
7707 got_worktree_close(worktree);
7708 free(cwd);
7709 free(base_commit_id);
7710 free(commit_id);
7711 free(refname);
7712 free(base_refname);
7713 return error;
7716 __dead static void
7717 usage_stage(void)
7719 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7720 "[file-path ...]\n",
7721 getprogname());
7722 exit(1);
7725 static const struct got_error *
7726 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7727 const char *path, struct got_object_id *blob_id,
7728 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7729 int dirfd, const char *de_name)
7731 const struct got_error *err = NULL;
7732 char *id_str = NULL;
7734 if (staged_status != GOT_STATUS_ADD &&
7735 staged_status != GOT_STATUS_MODIFY &&
7736 staged_status != GOT_STATUS_DELETE)
7737 return NULL;
7739 if (staged_status == GOT_STATUS_ADD ||
7740 staged_status == GOT_STATUS_MODIFY)
7741 err = got_object_id_str(&id_str, staged_blob_id);
7742 else
7743 err = got_object_id_str(&id_str, blob_id);
7744 if (err)
7745 return err;
7747 printf("%s %c %s\n", id_str, staged_status, path);
7748 free(id_str);
7749 return NULL;
7752 static const struct got_error *
7753 cmd_stage(int argc, char *argv[])
7755 const struct got_error *error = NULL;
7756 struct got_repository *repo = NULL;
7757 struct got_worktree *worktree = NULL;
7758 char *cwd = NULL;
7759 struct got_pathlist_head paths;
7760 struct got_pathlist_entry *pe;
7761 int ch, list_stage = 0, pflag = 0;
7762 FILE *patch_script_file = NULL;
7763 const char *patch_script_path = NULL;
7764 struct choose_patch_arg cpa;
7766 TAILQ_INIT(&paths);
7768 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7769 switch (ch) {
7770 case 'l':
7771 list_stage = 1;
7772 break;
7773 case 'p':
7774 pflag = 1;
7775 break;
7776 case 'F':
7777 patch_script_path = optarg;
7778 break;
7779 default:
7780 usage_stage();
7781 /* NOTREACHED */
7785 argc -= optind;
7786 argv += optind;
7788 #ifndef PROFILE
7789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7790 "unveil", NULL) == -1)
7791 err(1, "pledge");
7792 #endif
7793 if (list_stage && (pflag || patch_script_path))
7794 errx(1, "-l option cannot be used with other options");
7795 if (patch_script_path && !pflag)
7796 errx(1, "-F option can only be used together with -p option");
7798 cwd = getcwd(NULL, 0);
7799 if (cwd == NULL) {
7800 error = got_error_from_errno("getcwd");
7801 goto done;
7804 error = got_worktree_open(&worktree, cwd);
7805 if (error)
7806 goto done;
7808 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7809 NULL);
7810 if (error != NULL)
7811 goto done;
7813 if (patch_script_path) {
7814 patch_script_file = fopen(patch_script_path, "r");
7815 if (patch_script_file == NULL) {
7816 error = got_error_from_errno2("fopen",
7817 patch_script_path);
7818 goto done;
7821 error = apply_unveil(got_repo_get_path(repo), 0,
7822 got_worktree_get_root_path(worktree));
7823 if (error)
7824 goto done;
7826 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7827 if (error)
7828 goto done;
7830 if (list_stage)
7831 error = got_worktree_status(worktree, &paths, repo,
7832 print_stage, NULL, check_cancelled, NULL);
7833 else {
7834 cpa.patch_script_file = patch_script_file;
7835 cpa.action = "stage";
7836 error = got_worktree_stage(worktree, &paths,
7837 pflag ? NULL : print_status, NULL,
7838 pflag ? choose_patch : NULL, &cpa, repo);
7840 done:
7841 if (patch_script_file && fclose(patch_script_file) == EOF &&
7842 error == NULL)
7843 error = got_error_from_errno2("fclose", patch_script_path);
7844 if (repo)
7845 got_repo_close(repo);
7846 if (worktree)
7847 got_worktree_close(worktree);
7848 TAILQ_FOREACH(pe, &paths, entry)
7849 free((char *)pe->path);
7850 got_pathlist_free(&paths);
7851 free(cwd);
7852 return error;
7855 __dead static void
7856 usage_unstage(void)
7858 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7859 "[file-path ...]\n",
7860 getprogname());
7861 exit(1);
7865 static const struct got_error *
7866 cmd_unstage(int argc, char *argv[])
7868 const struct got_error *error = NULL;
7869 struct got_repository *repo = NULL;
7870 struct got_worktree *worktree = NULL;
7871 char *cwd = NULL;
7872 struct got_pathlist_head paths;
7873 struct got_pathlist_entry *pe;
7874 int ch, did_something = 0, pflag = 0;
7875 FILE *patch_script_file = NULL;
7876 const char *patch_script_path = NULL;
7877 struct choose_patch_arg cpa;
7879 TAILQ_INIT(&paths);
7881 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7882 switch (ch) {
7883 case 'p':
7884 pflag = 1;
7885 break;
7886 case 'F':
7887 patch_script_path = optarg;
7888 break;
7889 default:
7890 usage_unstage();
7891 /* NOTREACHED */
7895 argc -= optind;
7896 argv += optind;
7898 #ifndef PROFILE
7899 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7900 "unveil", NULL) == -1)
7901 err(1, "pledge");
7902 #endif
7903 if (patch_script_path && !pflag)
7904 errx(1, "-F option can only be used together with -p option");
7906 cwd = getcwd(NULL, 0);
7907 if (cwd == NULL) {
7908 error = got_error_from_errno("getcwd");
7909 goto done;
7912 error = got_worktree_open(&worktree, cwd);
7913 if (error)
7914 goto done;
7916 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7917 NULL);
7918 if (error != NULL)
7919 goto done;
7921 if (patch_script_path) {
7922 patch_script_file = fopen(patch_script_path, "r");
7923 if (patch_script_file == NULL) {
7924 error = got_error_from_errno2("fopen",
7925 patch_script_path);
7926 goto done;
7930 error = apply_unveil(got_repo_get_path(repo), 0,
7931 got_worktree_get_root_path(worktree));
7932 if (error)
7933 goto done;
7935 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7936 if (error)
7937 goto done;
7939 cpa.patch_script_file = patch_script_file;
7940 cpa.action = "unstage";
7941 error = got_worktree_unstage(worktree, &paths, update_progress,
7942 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7943 done:
7944 if (patch_script_file && fclose(patch_script_file) == EOF &&
7945 error == NULL)
7946 error = got_error_from_errno2("fclose", patch_script_path);
7947 if (repo)
7948 got_repo_close(repo);
7949 if (worktree)
7950 got_worktree_close(worktree);
7951 TAILQ_FOREACH(pe, &paths, entry)
7952 free((char *)pe->path);
7953 got_pathlist_free(&paths);
7954 free(cwd);
7955 return error;
7958 __dead static void
7959 usage_cat(void)
7961 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7962 "arg1 [arg2 ...]\n", getprogname());
7963 exit(1);
7966 static const struct got_error *
7967 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7969 const struct got_error *err;
7970 struct got_blob_object *blob;
7972 err = got_object_open_as_blob(&blob, repo, id, 8192);
7973 if (err)
7974 return err;
7976 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7977 got_object_blob_close(blob);
7978 return err;
7981 static const struct got_error *
7982 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7984 const struct got_error *err;
7985 struct got_tree_object *tree;
7986 int nentries, i;
7988 err = got_object_open_as_tree(&tree, repo, id);
7989 if (err)
7990 return err;
7992 nentries = got_object_tree_get_nentries(tree);
7993 for (i = 0; i < nentries; i++) {
7994 struct got_tree_entry *te;
7995 char *id_str;
7996 if (sigint_received || sigpipe_received)
7997 break;
7998 te = got_object_tree_get_entry(tree, i);
7999 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8000 if (err)
8001 break;
8002 fprintf(outfile, "%s %.7o %s\n", id_str,
8003 got_tree_entry_get_mode(te),
8004 got_tree_entry_get_name(te));
8005 free(id_str);
8008 got_object_tree_close(tree);
8009 return err;
8012 static const struct got_error *
8013 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8015 const struct got_error *err;
8016 struct got_commit_object *commit;
8017 const struct got_object_id_queue *parent_ids;
8018 struct got_object_qid *pid;
8019 char *id_str = NULL;
8020 const char *logmsg = NULL;
8022 err = got_object_open_as_commit(&commit, repo, id);
8023 if (err)
8024 return err;
8026 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8027 if (err)
8028 goto done;
8030 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8031 parent_ids = got_object_commit_get_parent_ids(commit);
8032 fprintf(outfile, "numparents %d\n",
8033 got_object_commit_get_nparents(commit));
8034 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8035 char *pid_str;
8036 err = got_object_id_str(&pid_str, pid->id);
8037 if (err)
8038 goto done;
8039 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8040 free(pid_str);
8042 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8043 got_object_commit_get_author(commit),
8044 got_object_commit_get_author_time(commit));
8046 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8047 got_object_commit_get_author(commit),
8048 got_object_commit_get_committer_time(commit));
8050 logmsg = got_object_commit_get_logmsg_raw(commit);
8051 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8052 fprintf(outfile, "%s", logmsg);
8053 done:
8054 free(id_str);
8055 got_object_commit_close(commit);
8056 return err;
8059 static const struct got_error *
8060 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8062 const struct got_error *err;
8063 struct got_tag_object *tag;
8064 char *id_str = NULL;
8065 const char *tagmsg = NULL;
8067 err = got_object_open_as_tag(&tag, repo, id);
8068 if (err)
8069 return err;
8071 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8072 if (err)
8073 goto done;
8075 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8077 switch (got_object_tag_get_object_type(tag)) {
8078 case GOT_OBJ_TYPE_BLOB:
8079 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8080 GOT_OBJ_LABEL_BLOB);
8081 break;
8082 case GOT_OBJ_TYPE_TREE:
8083 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8084 GOT_OBJ_LABEL_TREE);
8085 break;
8086 case GOT_OBJ_TYPE_COMMIT:
8087 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8088 GOT_OBJ_LABEL_COMMIT);
8089 break;
8090 case GOT_OBJ_TYPE_TAG:
8091 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8092 GOT_OBJ_LABEL_TAG);
8093 break;
8094 default:
8095 break;
8098 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8099 got_object_tag_get_name(tag));
8101 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8102 got_object_tag_get_tagger(tag),
8103 got_object_tag_get_tagger_time(tag));
8105 tagmsg = got_object_tag_get_message(tag);
8106 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8107 fprintf(outfile, "%s", tagmsg);
8108 done:
8109 free(id_str);
8110 got_object_tag_close(tag);
8111 return err;
8114 static const struct got_error *
8115 cmd_cat(int argc, char *argv[])
8117 const struct got_error *error;
8118 struct got_repository *repo = NULL;
8119 struct got_worktree *worktree = NULL;
8120 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8121 const char *commit_id_str = NULL;
8122 struct got_object_id *id = NULL, *commit_id = NULL;
8123 int ch, obj_type, i, force_path = 0;
8125 #ifndef PROFILE
8126 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8127 NULL) == -1)
8128 err(1, "pledge");
8129 #endif
8131 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8132 switch (ch) {
8133 case 'c':
8134 commit_id_str = optarg;
8135 break;
8136 case 'r':
8137 repo_path = realpath(optarg, NULL);
8138 if (repo_path == NULL)
8139 return got_error_from_errno2("realpath",
8140 optarg);
8141 got_path_strip_trailing_slashes(repo_path);
8142 break;
8143 case 'P':
8144 force_path = 1;
8145 break;
8146 default:
8147 usage_cat();
8148 /* NOTREACHED */
8152 argc -= optind;
8153 argv += optind;
8155 cwd = getcwd(NULL, 0);
8156 if (cwd == NULL) {
8157 error = got_error_from_errno("getcwd");
8158 goto done;
8160 error = got_worktree_open(&worktree, cwd);
8161 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8162 goto done;
8163 if (worktree) {
8164 if (repo_path == NULL) {
8165 repo_path = strdup(
8166 got_worktree_get_repo_path(worktree));
8167 if (repo_path == NULL) {
8168 error = got_error_from_errno("strdup");
8169 goto done;
8174 if (repo_path == NULL) {
8175 repo_path = getcwd(NULL, 0);
8176 if (repo_path == NULL)
8177 return got_error_from_errno("getcwd");
8180 error = got_repo_open(&repo, repo_path, NULL);
8181 free(repo_path);
8182 if (error != NULL)
8183 goto done;
8185 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8186 if (error)
8187 goto done;
8189 if (commit_id_str == NULL)
8190 commit_id_str = GOT_REF_HEAD;
8191 error = got_repo_match_object_id(&commit_id, NULL,
8192 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8193 if (error)
8194 goto done;
8196 for (i = 0; i < argc; i++) {
8197 if (force_path) {
8198 error = got_object_id_by_path(&id, repo, commit_id,
8199 argv[i]);
8200 if (error)
8201 break;
8202 } else {
8203 error = got_repo_match_object_id(&id, &label, argv[i],
8204 GOT_OBJ_TYPE_ANY, 0, repo);
8205 if (error) {
8206 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8207 error->code != GOT_ERR_NOT_REF)
8208 break;
8209 error = got_object_id_by_path(&id, repo,
8210 commit_id, argv[i]);
8211 if (error)
8212 break;
8216 error = got_object_get_type(&obj_type, repo, id);
8217 if (error)
8218 break;
8220 switch (obj_type) {
8221 case GOT_OBJ_TYPE_BLOB:
8222 error = cat_blob(id, repo, stdout);
8223 break;
8224 case GOT_OBJ_TYPE_TREE:
8225 error = cat_tree(id, repo, stdout);
8226 break;
8227 case GOT_OBJ_TYPE_COMMIT:
8228 error = cat_commit(id, repo, stdout);
8229 break;
8230 case GOT_OBJ_TYPE_TAG:
8231 error = cat_tag(id, repo, stdout);
8232 break;
8233 default:
8234 error = got_error(GOT_ERR_OBJ_TYPE);
8235 break;
8237 if (error)
8238 break;
8239 free(label);
8240 label = NULL;
8241 free(id);
8242 id = NULL;
8244 done:
8245 free(label);
8246 free(id);
8247 free(commit_id);
8248 if (worktree)
8249 got_worktree_close(worktree);
8250 if (repo) {
8251 const struct got_error *repo_error;
8252 repo_error = got_repo_close(repo);
8253 if (error == NULL)
8254 error = repo_error;
8256 return error;