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 [target-directory]\n",
815 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);
843 if (packfile_size > 0 || nobj_indexed > 0) {
844 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
845 (a->last_scaled_size[0] == '\0' ||
846 strcmp(scaled_size, a->last_scaled_size)) != 0) {
847 print_size = 1;
848 if (strlcpy(a->last_scaled_size, scaled_size,
849 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
850 return got_error(GOT_ERR_NO_SPACE);
852 if (nobj_indexed > 0) {
853 p_indexed = (nobj_indexed * 100) / nobj_total;
854 if (p_indexed != a->last_p_indexed) {
855 a->last_p_indexed = p_indexed;
856 print_indexed = 1;
857 print_size = 1;
860 if (nobj_resolved > 0) {
861 p_resolved = (nobj_resolved * 100) /
862 (nobj_total - nobj_loose);
863 if (p_resolved != a->last_p_resolved) {
864 a->last_p_resolved = p_resolved;
865 print_resolved = 1;
866 print_indexed = 1;
867 print_size = 1;
872 if (print_size || print_indexed || print_resolved)
873 printf("\r");
874 if (print_size)
875 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
876 if (print_indexed) {
877 printf("; indexing %d%%", p_indexed);
878 if (a->verbosity > 0)
879 printf(" (%d/%d)", nobj_indexed, nobj_total);
881 if (print_resolved) {
882 printf("; resolving deltas %d%%", p_resolved);
883 if (a->verbosity > 0)
884 printf(" (%d/%d)", nobj_resolved,
885 nobj_total - nobj_loose);
887 if (print_size || print_indexed || print_resolved)
888 fflush(stdout);
890 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
891 nobj_resolved == nobj_total - nobj_loose)
892 printf("\nWriting pack index...\n");
894 return NULL;
897 static const struct got_error *
898 cmd_clone(int argc, char *argv[])
900 const struct got_error *error = NULL;
901 const char *uri, *dirname;
902 char *proto, *host, *port, *repo_name, *server_path;
903 char *default_destdir = NULL, *id_str = NULL;
904 const char *repo_path;
905 struct got_repository *repo = NULL;
906 struct got_pathlist_head refs, symrefs;
907 struct got_pathlist_entry *pe;
908 struct got_object_id *pack_hash = NULL;
909 int ch, fetchfd = -1;
910 struct got_fetch_progress_arg fpa;
911 char *git_url = NULL;
912 char *gitconfig_path = NULL;
913 char *gitconfig = NULL;
914 FILE *gitconfig_file = NULL;
915 ssize_t n;
916 int verbosity = 0;
918 TAILQ_INIT(&refs);
919 TAILQ_INIT(&symrefs);
921 while ((ch = getopt(argc, argv, "vq")) != -1) {
922 switch (ch) {
923 case 'v':
924 if (verbosity < 0)
925 verbosity = 0;
926 else if (verbosity < 3)
927 verbosity++;
928 break;
929 case 'q':
930 verbosity = -1;
931 break;
932 default:
933 usage_clone();
934 break;
937 argc -= optind;
938 argv += optind;
940 uri = argv[0];
942 if (argc == 1)
943 dirname = NULL;
944 else if (argc == 2)
945 dirname = argv[1];
946 else
947 usage_clone();
949 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
950 &repo_name, argv[0]);
951 if (error)
952 goto done;
954 if (strcmp(proto, "git") == 0) {
955 #ifndef PROFILE
956 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
957 "sendfd dns inet unveil", NULL) == -1)
958 err(1, "pledge");
959 #endif
960 git_url = strdup(argv[0]);
961 if (git_url == NULL) {
962 error = got_error_from_errno("strdup");
963 goto done;
965 } else if (strcmp(proto, "git+ssh") == 0 ||
966 strcmp(proto, "ssh") == 0) {
967 #ifndef PROFILE
968 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
969 "sendfd unveil", NULL) == -1)
970 err(1, "pledge");
971 #endif
972 if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
973 error = got_error_from_errno("asprintf");
974 goto done;
976 } else if (strcmp(proto, "http") == 0 ||
977 strcmp(proto, "git+http") == 0) {
978 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
979 goto done;
980 } else {
981 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
982 goto done;
984 if (dirname == NULL) {
985 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
986 error = got_error_from_errno("asprintf");
987 goto done;
989 repo_path = default_destdir;
990 } else
991 repo_path = dirname;
993 error = got_path_mkdir(repo_path);
994 if (error)
995 goto done;
997 error = got_repo_init(repo_path);
998 if (error)
999 goto done;
1001 error = got_repo_open(&repo, repo_path, NULL);
1002 if (error)
1003 goto done;
1005 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1006 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1007 error = got_error_from_errno2("unveil",
1008 GOT_FETCH_PATH_SSH);
1009 goto done;
1012 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1013 if (error)
1014 goto done;
1016 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1017 verbosity);
1018 if (error)
1019 goto done;
1021 if (verbosity >= 0)
1022 printf("Connected to %s:%s\n", host, port);
1024 fpa.last_scaled_size[0] = '\0';
1025 fpa.last_p_indexed = -1;
1026 fpa.last_p_resolved = -1;
1027 fpa.verbosity = verbosity;
1028 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1029 GOT_FETCH_DEFAULT_REMOTE_NAME, fetchfd, repo,
1030 fetch_progress, &fpa);
1031 if (error)
1032 goto done;
1034 error = got_object_id_str(&id_str, pack_hash);
1035 if (error)
1036 goto done;
1037 if (verbosity >= 0)
1038 printf("Fetched %s.pack\n", id_str);
1039 free(id_str);
1041 /* Set up references provided with the pack file. */
1042 TAILQ_FOREACH(pe, &refs, entry) {
1043 const char *refname = pe->path;
1044 struct got_object_id *id = pe->data;
1045 struct got_reference *ref;
1046 char *remote_refname;
1048 error = got_ref_alloc(&ref, refname, id);
1049 if (error)
1050 goto done;
1051 error = got_ref_write(ref, repo);
1052 got_ref_close(ref);
1053 if (error)
1054 goto done;
1056 if (strncmp("refs/heads/", refname, 11) != 0)
1057 continue;
1059 if (asprintf(&remote_refname,
1060 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1061 refname + 11) == -1) {
1062 error = got_error_from_errno("asprintf");
1063 goto done;
1065 error = got_ref_alloc(&ref, remote_refname, id);
1066 if (error)
1067 goto done;
1068 error = got_ref_write(ref, repo);
1069 got_ref_close(ref);
1070 if (error)
1071 goto done;
1074 /* Set the HEAD reference if the server provided one. */
1075 TAILQ_FOREACH(pe, &symrefs, entry) {
1076 struct got_reference *symref, *target_ref;
1077 const char *refname = pe->path;
1078 const char *target = pe->data;
1080 if (strcmp(refname, GOT_REF_HEAD) != 0)
1081 continue;
1083 error = got_ref_open(&target_ref, repo, target, 0);
1084 if (error) {
1085 if (error->code == GOT_ERR_NOT_REF)
1086 continue;
1087 goto done;
1090 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1091 got_ref_close(target_ref);
1092 if (error)
1093 goto done;
1095 if (verbosity > 1) {
1096 printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 got_ref_get_symref_target(symref));
1099 if (verbosity >= 0)
1100 printf("Created cloned repository '%s'\n", repo_path);
1102 error = got_ref_write(symref, repo);
1103 got_ref_close(symref);
1104 break;
1107 /* Create a config file so Git can understand this repository. */
1108 gitconfig_path = got_repo_get_path_gitconfig(repo);
1109 if (gitconfig_path == NULL) {
1110 error = got_error_from_errno("got_repo_get_path_gitconfig");
1111 goto done;
1113 gitconfig_file = fopen(gitconfig_path, "w");
1114 if (gitconfig_file == NULL) {
1115 error = got_error_from_errno2("fopen", gitconfig_path);
1116 goto done;
1118 if (asprintf(&gitconfig,
1119 "[core]\n"
1120 "\trepositoryformatversion = 0\n"
1121 "\tbare = true\n"
1122 "[remote \"%s\"]\n"
1123 "\turl = %s\n"
1124 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1125 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1126 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1127 error = got_error_from_errno("asprintf");
1128 goto done;
1130 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1131 if (n != strlen(gitconfig))
1132 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1133 done:
1134 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1135 error = got_error_from_errno("close");
1136 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1137 error = got_error_from_errno("fclose");
1138 if (repo)
1139 got_repo_close(repo);
1140 TAILQ_FOREACH(pe, &refs, entry) {
1141 free((void *)pe->path);
1142 free(pe->data);
1144 got_pathlist_free(&refs);
1145 TAILQ_FOREACH(pe, &symrefs, entry) {
1146 free((void *)pe->path);
1147 free(pe->data);
1149 got_pathlist_free(&symrefs);
1150 free(pack_hash);
1151 free(proto);
1152 free(host);
1153 free(port);
1154 free(server_path);
1155 free(repo_name);
1156 free(default_destdir);
1157 free(gitconfig_path);
1158 free(git_url);
1159 return error;
1162 static const struct got_error *
1163 create_ref(const char *refname, struct got_object_id *id,
1164 const char *id_str, struct got_repository *repo)
1166 const struct got_error *err = NULL;
1167 struct got_reference *ref;
1169 printf("Creating %s: %s\n", refname, id_str);
1171 err = got_ref_alloc(&ref, refname, id);
1172 if (err)
1173 return err;
1175 err = got_ref_write(ref, repo);
1176 got_ref_close(ref);
1177 return err;
1180 static const struct got_error *
1181 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 char *new_id_str = NULL;
1186 struct got_object_id *old_id = NULL;
1188 err = got_object_id_str(&new_id_str, new_id);
1189 if (err)
1190 goto done;
1192 if (got_ref_is_symbolic(ref)) {
1193 struct got_reference *new_ref;
1194 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1195 if (err)
1196 goto done;
1197 printf("Deleting symbolic reference %s -> %s\n",
1198 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1199 err = got_ref_delete(ref, repo);
1200 if (err)
1201 goto done;
1202 printf("Setting %s to %s\n", got_ref_get_name(ref),
1203 new_id_str);
1204 err = got_ref_write(new_ref, repo);
1205 if (err)
1206 goto done;
1207 } else {
1208 err = got_ref_resolve(&old_id, repo, ref);
1209 if (err)
1210 goto done;
1211 if (got_object_id_cmp(old_id, new_id) != 0) {
1212 printf("Setting %s to %s\n",
1213 got_ref_get_name(ref), new_id_str);
1214 err = got_ref_change_ref(ref, new_id);
1215 if (err)
1216 goto done;
1217 err = got_ref_write(ref, repo);
1218 if (err)
1219 goto done;
1222 done:
1223 free(old_id);
1224 free(new_id_str);
1225 return err;
1228 __dead static void
1229 usage_fetch(void)
1231 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1232 "[remote-repository-name]\n", getprogname());
1233 exit(1);
1236 static const struct got_error *
1237 cmd_fetch(int argc, char *argv[])
1239 const struct got_error *error = NULL;
1240 char *cwd = NULL, *repo_path = NULL;
1241 const char *remote_name;
1242 char *proto = NULL, *host = NULL, *port = NULL;
1243 char *repo_name = NULL, *server_path = NULL;
1244 struct got_remote_repo *remotes, *remote = NULL;
1245 int nremotes;
1246 char *id_str = NULL;
1247 struct got_repository *repo = NULL;
1248 struct got_worktree *worktree = NULL;
1249 struct got_pathlist_head refs, symrefs;
1250 struct got_pathlist_entry *pe;
1251 struct got_object_id *pack_hash = NULL;
1252 int i, ch, fetchfd = -1;
1253 struct got_fetch_progress_arg fpa;
1254 int verbosity = 0;
1256 TAILQ_INIT(&refs);
1257 TAILQ_INIT(&symrefs);
1259 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1260 switch (ch) {
1261 case 'r':
1262 repo_path = realpath(optarg, NULL);
1263 if (repo_path == NULL)
1264 return got_error_from_errno2("realpath",
1265 optarg);
1266 got_path_strip_trailing_slashes(repo_path);
1267 break;
1268 case 'v':
1269 if (verbosity < 0)
1270 verbosity = 0;
1271 else if (verbosity < 3)
1272 verbosity++;
1273 break;
1274 case 'q':
1275 verbosity = -1;
1276 break;
1277 default:
1278 usage_fetch();
1279 break;
1282 argc -= optind;
1283 argv += optind;
1285 if (argc == 0)
1286 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1287 else if (argc == 1)
1288 remote_name = argv[0];
1289 else
1290 usage_fetch();
1292 cwd = getcwd(NULL, 0);
1293 if (cwd == NULL) {
1294 error = got_error_from_errno("getcwd");
1295 goto done;
1298 if (repo_path == NULL) {
1299 error = got_worktree_open(&worktree, cwd);
1300 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1301 goto done;
1302 else
1303 error = NULL;
1304 if (worktree) {
1305 repo_path =
1306 strdup(got_worktree_get_repo_path(worktree));
1307 if (repo_path == NULL)
1308 error = got_error_from_errno("strdup");
1309 if (error)
1310 goto done;
1311 } else {
1312 repo_path = strdup(cwd);
1313 if (repo_path == NULL) {
1314 error = got_error_from_errno("strdup");
1315 goto done;
1320 error = got_repo_open(&repo, repo_path, NULL);
1321 if (error)
1322 goto done;
1324 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1325 for (i = 0; i < nremotes; i++) {
1326 remote = &remotes[i];
1327 if (strcmp(remote->name, remote_name) == 0)
1328 break;
1330 if (i == nremotes) {
1331 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1332 goto done;
1335 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1336 &repo_name, remote->url);
1337 if (error)
1338 goto done;
1340 if (strcmp(proto, "git") == 0) {
1341 #ifndef PROFILE
1342 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1343 "sendfd dns inet unveil", NULL) == -1)
1344 err(1, "pledge");
1345 #endif
1346 } else if (strcmp(proto, "git+ssh") == 0 ||
1347 strcmp(proto, "ssh") == 0) {
1348 #ifndef PROFILE
1349 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1350 "sendfd unveil", NULL) == -1)
1351 err(1, "pledge");
1352 #endif
1353 } else if (strcmp(proto, "http") == 0 ||
1354 strcmp(proto, "git+http") == 0) {
1355 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1356 goto done;
1357 } else {
1358 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1359 goto done;
1362 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1363 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1364 error = got_error_from_errno2("unveil",
1365 GOT_FETCH_PATH_SSH);
1366 goto done;
1369 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1370 if (error)
1371 goto done;
1373 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1374 verbosity);
1375 if (error)
1376 goto done;
1378 if (verbosity >= 0)
1379 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1381 fpa.last_scaled_size[0] = '\0';
1382 fpa.last_p_indexed = -1;
1383 fpa.last_p_resolved = -1;
1384 fpa.verbosity = verbosity;
1385 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1386 fetchfd, repo, fetch_progress, &fpa);
1387 if (error)
1388 goto done;
1390 if (pack_hash == NULL) {
1391 if (verbosity >= 0)
1392 printf("Already up-to-date\n");
1393 goto done;
1396 error = got_object_id_str(&id_str, pack_hash);
1397 if (error)
1398 goto done;
1399 if (verbosity >= 0)
1400 printf("Fetched %s.pack\n", id_str);
1401 free(id_str);
1402 id_str = NULL;
1404 /* Update references provided with the pack file. */
1405 TAILQ_FOREACH(pe, &refs, entry) {
1406 const char *refname = pe->path;
1407 struct got_object_id *id = pe->data;
1408 struct got_reference *ref;
1409 char *remote_refname;
1411 error = got_object_id_str(&id_str, id);
1412 if (error)
1413 goto done;
1415 if (strncmp("refs/tags/", refname, 10) == 0) {
1416 error = got_ref_open(&ref, repo, refname, 0);
1417 if (error) {
1418 if (error->code != GOT_ERR_NOT_REF)
1419 goto done;
1420 error = create_ref(refname, id, id_str, repo);
1421 if (error)
1422 goto done;
1423 } else {
1424 error = update_ref(ref, id, repo);
1425 got_ref_close(ref);
1426 if (error)
1427 goto done;
1429 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1430 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1431 remote_name, refname + 11) == -1) {
1432 error = got_error_from_errno("asprintf");
1433 goto done;
1436 error = got_ref_open(&ref, repo, remote_refname, 0);
1437 if (error) {
1438 if (error->code != GOT_ERR_NOT_REF)
1439 goto done;
1440 error = create_ref(refname, id, id_str, repo);
1441 if (error)
1442 goto done;
1443 } else {
1444 error = update_ref(ref, id, repo);
1445 got_ref_close(ref);
1446 if (error)
1447 goto done;
1450 free(id_str);
1451 id_str = NULL;
1453 done:
1454 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1455 error = got_error_from_errno("close");
1456 if (repo)
1457 got_repo_close(repo);
1458 if (worktree)
1459 got_worktree_close(worktree);
1460 TAILQ_FOREACH(pe, &refs, entry) {
1461 free((void *)pe->path);
1462 free(pe->data);
1464 got_pathlist_free(&refs);
1465 TAILQ_FOREACH(pe, &symrefs, entry) {
1466 free((void *)pe->path);
1467 free(pe->data);
1469 got_pathlist_free(&symrefs);
1470 free(id_str);
1471 free(cwd);
1472 free(repo_path);
1473 free(pack_hash);
1474 free(proto);
1475 free(host);
1476 free(port);
1477 free(server_path);
1478 free(repo_name);
1479 return error;
1483 __dead static void
1484 usage_checkout(void)
1486 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1487 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1488 exit(1);
1491 static void
1492 show_worktree_base_ref_warning(void)
1494 fprintf(stderr, "%s: warning: could not create a reference "
1495 "to the work tree's base commit; the commit could be "
1496 "garbage-collected by Git; making the repository "
1497 "writable and running 'got update' will prevent this\n",
1498 getprogname());
1501 struct got_checkout_progress_arg {
1502 const char *worktree_path;
1503 int had_base_commit_ref_error;
1506 static const struct got_error *
1507 checkout_progress(void *arg, unsigned char status, const char *path)
1509 struct got_checkout_progress_arg *a = arg;
1511 /* Base commit bump happens silently. */
1512 if (status == GOT_STATUS_BUMP_BASE)
1513 return NULL;
1515 if (status == GOT_STATUS_BASE_REF_ERR) {
1516 a->had_base_commit_ref_error = 1;
1517 return NULL;
1520 while (path[0] == '/')
1521 path++;
1523 printf("%c %s/%s\n", status, a->worktree_path, path);
1524 return NULL;
1527 static const struct got_error *
1528 check_cancelled(void *arg)
1530 if (sigint_received || sigpipe_received)
1531 return got_error(GOT_ERR_CANCELLED);
1532 return NULL;
1535 static const struct got_error *
1536 check_linear_ancestry(struct got_object_id *commit_id,
1537 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1538 struct got_repository *repo)
1540 const struct got_error *err = NULL;
1541 struct got_object_id *yca_id;
1543 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1544 commit_id, base_commit_id, repo, check_cancelled, NULL);
1545 if (err)
1546 return err;
1548 if (yca_id == NULL)
1549 return got_error(GOT_ERR_ANCESTRY);
1552 * Require a straight line of history between the target commit
1553 * and the work tree's base commit.
1555 * Non-linear situations such as this require a rebase:
1557 * (commit) D F (base_commit)
1558 * \ /
1559 * C E
1560 * \ /
1561 * B (yca)
1562 * |
1563 * A
1565 * 'got update' only handles linear cases:
1566 * Update forwards in time: A (base/yca) - B - C - D (commit)
1567 * Update backwards in time: D (base) - C - B - A (commit/yca)
1569 if (allow_forwards_in_time_only) {
1570 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1571 return got_error(GOT_ERR_ANCESTRY);
1572 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1573 got_object_id_cmp(base_commit_id, yca_id) != 0)
1574 return got_error(GOT_ERR_ANCESTRY);
1576 free(yca_id);
1577 return NULL;
1580 static const struct got_error *
1581 check_same_branch(struct got_object_id *commit_id,
1582 struct got_reference *head_ref, struct got_object_id *yca_id,
1583 struct got_repository *repo)
1585 const struct got_error *err = NULL;
1586 struct got_commit_graph *graph = NULL;
1587 struct got_object_id *head_commit_id = NULL;
1588 int is_same_branch = 0;
1590 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1591 if (err)
1592 goto done;
1594 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1595 is_same_branch = 1;
1596 goto done;
1598 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1599 is_same_branch = 1;
1600 goto done;
1603 err = got_commit_graph_open(&graph, "/", 1);
1604 if (err)
1605 goto done;
1607 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1608 check_cancelled, NULL);
1609 if (err)
1610 goto done;
1612 for (;;) {
1613 struct got_object_id *id;
1614 err = got_commit_graph_iter_next(&id, graph, repo,
1615 check_cancelled, NULL);
1616 if (err) {
1617 if (err->code == GOT_ERR_ITER_COMPLETED)
1618 err = NULL;
1619 break;
1622 if (id) {
1623 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1624 break;
1625 if (got_object_id_cmp(id, commit_id) == 0) {
1626 is_same_branch = 1;
1627 break;
1631 done:
1632 if (graph)
1633 got_commit_graph_close(graph);
1634 free(head_commit_id);
1635 if (!err && !is_same_branch)
1636 err = got_error(GOT_ERR_ANCESTRY);
1637 return err;
1640 static const struct got_error *
1641 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1643 static char msg[512];
1644 const char *branch_name;
1646 if (got_ref_is_symbolic(ref))
1647 branch_name = got_ref_get_symref_target(ref);
1648 else
1649 branch_name = got_ref_get_name(ref);
1651 if (strncmp("refs/heads/", branch_name, 11) == 0)
1652 branch_name += 11;
1654 snprintf(msg, sizeof(msg),
1655 "target commit is not contained in branch '%s'; "
1656 "the branch to use must be specified with -b; "
1657 "if necessary a new branch can be created for "
1658 "this commit with 'got branch -c %s BRANCH_NAME'",
1659 branch_name, commit_id_str);
1661 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1664 static const struct got_error *
1665 cmd_checkout(int argc, char *argv[])
1667 const struct got_error *error = NULL;
1668 struct got_repository *repo = NULL;
1669 struct got_reference *head_ref = NULL;
1670 struct got_worktree *worktree = NULL;
1671 char *repo_path = NULL;
1672 char *worktree_path = NULL;
1673 const char *path_prefix = "";
1674 const char *branch_name = GOT_REF_HEAD;
1675 char *commit_id_str = NULL;
1676 int ch, same_path_prefix, allow_nonempty = 0;
1677 struct got_pathlist_head paths;
1678 struct got_checkout_progress_arg cpa;
1680 TAILQ_INIT(&paths);
1682 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1683 switch (ch) {
1684 case 'b':
1685 branch_name = optarg;
1686 break;
1687 case 'c':
1688 commit_id_str = strdup(optarg);
1689 if (commit_id_str == NULL)
1690 return got_error_from_errno("strdup");
1691 break;
1692 case 'E':
1693 allow_nonempty = 1;
1694 break;
1695 case 'p':
1696 path_prefix = optarg;
1697 break;
1698 default:
1699 usage_checkout();
1700 /* NOTREACHED */
1704 argc -= optind;
1705 argv += optind;
1707 #ifndef PROFILE
1708 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1709 "unveil", NULL) == -1)
1710 err(1, "pledge");
1711 #endif
1712 if (argc == 1) {
1713 char *cwd, *base, *dotgit;
1714 repo_path = realpath(argv[0], NULL);
1715 if (repo_path == NULL)
1716 return got_error_from_errno2("realpath", argv[0]);
1717 cwd = getcwd(NULL, 0);
1718 if (cwd == NULL) {
1719 error = got_error_from_errno("getcwd");
1720 goto done;
1722 if (path_prefix[0]) {
1723 base = basename(path_prefix);
1724 if (base == NULL) {
1725 error = got_error_from_errno2("basename",
1726 path_prefix);
1727 goto done;
1729 } else {
1730 base = basename(repo_path);
1731 if (base == NULL) {
1732 error = got_error_from_errno2("basename",
1733 repo_path);
1734 goto done;
1737 dotgit = strstr(base, ".git");
1738 if (dotgit)
1739 *dotgit = '\0';
1740 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1741 error = got_error_from_errno("asprintf");
1742 free(cwd);
1743 goto done;
1745 free(cwd);
1746 } else if (argc == 2) {
1747 repo_path = realpath(argv[0], NULL);
1748 if (repo_path == NULL) {
1749 error = got_error_from_errno2("realpath", argv[0]);
1750 goto done;
1752 worktree_path = realpath(argv[1], NULL);
1753 if (worktree_path == NULL) {
1754 if (errno != ENOENT) {
1755 error = got_error_from_errno2("realpath",
1756 argv[1]);
1757 goto done;
1759 worktree_path = strdup(argv[1]);
1760 if (worktree_path == NULL) {
1761 error = got_error_from_errno("strdup");
1762 goto done;
1765 } else
1766 usage_checkout();
1768 got_path_strip_trailing_slashes(repo_path);
1769 got_path_strip_trailing_slashes(worktree_path);
1771 error = got_repo_open(&repo, repo_path, NULL);
1772 if (error != NULL)
1773 goto done;
1775 /* Pre-create work tree path for unveil(2) */
1776 error = got_path_mkdir(worktree_path);
1777 if (error) {
1778 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1779 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1780 goto done;
1781 if (!allow_nonempty &&
1782 !got_path_dir_is_empty(worktree_path)) {
1783 error = got_error_path(worktree_path,
1784 GOT_ERR_DIR_NOT_EMPTY);
1785 goto done;
1789 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1790 if (error)
1791 goto done;
1793 error = got_ref_open(&head_ref, repo, branch_name, 0);
1794 if (error != NULL)
1795 goto done;
1797 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1798 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1799 goto done;
1801 error = got_worktree_open(&worktree, worktree_path);
1802 if (error != NULL)
1803 goto done;
1805 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1806 path_prefix);
1807 if (error != NULL)
1808 goto done;
1809 if (!same_path_prefix) {
1810 error = got_error(GOT_ERR_PATH_PREFIX);
1811 goto done;
1814 if (commit_id_str) {
1815 struct got_object_id *commit_id;
1816 error = got_repo_match_object_id(&commit_id, NULL,
1817 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1818 if (error)
1819 goto done;
1820 error = check_linear_ancestry(commit_id,
1821 got_worktree_get_base_commit_id(worktree), 0, repo);
1822 if (error != NULL) {
1823 free(commit_id);
1824 if (error->code == GOT_ERR_ANCESTRY) {
1825 error = checkout_ancestry_error(
1826 head_ref, commit_id_str);
1828 goto done;
1830 error = check_same_branch(commit_id, head_ref, NULL, repo);
1831 if (error) {
1832 if (error->code == GOT_ERR_ANCESTRY) {
1833 error = checkout_ancestry_error(
1834 head_ref, commit_id_str);
1836 goto done;
1838 error = got_worktree_set_base_commit_id(worktree, repo,
1839 commit_id);
1840 free(commit_id);
1841 if (error)
1842 goto done;
1845 error = got_pathlist_append(&paths, "", NULL);
1846 if (error)
1847 goto done;
1848 cpa.worktree_path = worktree_path;
1849 cpa.had_base_commit_ref_error = 0;
1850 error = got_worktree_checkout_files(worktree, &paths, repo,
1851 checkout_progress, &cpa, check_cancelled, NULL);
1852 if (error != NULL)
1853 goto done;
1855 printf("Now shut up and hack\n");
1856 if (cpa.had_base_commit_ref_error)
1857 show_worktree_base_ref_warning();
1858 done:
1859 got_pathlist_free(&paths);
1860 free(commit_id_str);
1861 free(repo_path);
1862 free(worktree_path);
1863 return error;
1866 __dead static void
1867 usage_update(void)
1869 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1870 getprogname());
1871 exit(1);
1874 static const struct got_error *
1875 update_progress(void *arg, unsigned char status, const char *path)
1877 int *did_something = arg;
1879 if (status == GOT_STATUS_EXISTS ||
1880 status == GOT_STATUS_BASE_REF_ERR)
1881 return NULL;
1883 *did_something = 1;
1885 /* Base commit bump happens silently. */
1886 if (status == GOT_STATUS_BUMP_BASE)
1887 return NULL;
1889 while (path[0] == '/')
1890 path++;
1891 printf("%c %s\n", status, path);
1892 return NULL;
1895 static const struct got_error *
1896 switch_head_ref(struct got_reference *head_ref,
1897 struct got_object_id *commit_id, struct got_worktree *worktree,
1898 struct got_repository *repo)
1900 const struct got_error *err = NULL;
1901 char *base_id_str;
1902 int ref_has_moved = 0;
1904 /* Trivial case: switching between two different references. */
1905 if (strcmp(got_ref_get_name(head_ref),
1906 got_worktree_get_head_ref_name(worktree)) != 0) {
1907 printf("Switching work tree from %s to %s\n",
1908 got_worktree_get_head_ref_name(worktree),
1909 got_ref_get_name(head_ref));
1910 return got_worktree_set_head_ref(worktree, head_ref);
1913 err = check_linear_ancestry(commit_id,
1914 got_worktree_get_base_commit_id(worktree), 0, repo);
1915 if (err) {
1916 if (err->code != GOT_ERR_ANCESTRY)
1917 return err;
1918 ref_has_moved = 1;
1920 if (!ref_has_moved)
1921 return NULL;
1923 /* Switching to a rebased branch with the same reference name. */
1924 err = got_object_id_str(&base_id_str,
1925 got_worktree_get_base_commit_id(worktree));
1926 if (err)
1927 return err;
1928 printf("Reference %s now points at a different branch\n",
1929 got_worktree_get_head_ref_name(worktree));
1930 printf("Switching work tree from %s to %s\n", base_id_str,
1931 got_worktree_get_head_ref_name(worktree));
1932 return NULL;
1935 static const struct got_error *
1936 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1938 const struct got_error *err;
1939 int in_progress;
1941 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1942 if (err)
1943 return err;
1944 if (in_progress)
1945 return got_error(GOT_ERR_REBASING);
1947 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1948 if (err)
1949 return err;
1950 if (in_progress)
1951 return got_error(GOT_ERR_HISTEDIT_BUSY);
1953 return NULL;
1956 static const struct got_error *
1957 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1958 char *argv[], struct got_worktree *worktree)
1960 const struct got_error *err = NULL;
1961 char *path;
1962 int i;
1964 if (argc == 0) {
1965 path = strdup("");
1966 if (path == NULL)
1967 return got_error_from_errno("strdup");
1968 return got_pathlist_append(paths, path, NULL);
1971 for (i = 0; i < argc; i++) {
1972 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1973 if (err)
1974 break;
1975 err = got_pathlist_append(paths, path, NULL);
1976 if (err) {
1977 free(path);
1978 break;
1982 return err;
1985 static const struct got_error *
1986 cmd_update(int argc, char *argv[])
1988 const struct got_error *error = NULL;
1989 struct got_repository *repo = NULL;
1990 struct got_worktree *worktree = NULL;
1991 char *worktree_path = NULL;
1992 struct got_object_id *commit_id = NULL;
1993 char *commit_id_str = NULL;
1994 const char *branch_name = NULL;
1995 struct got_reference *head_ref = NULL;
1996 struct got_pathlist_head paths;
1997 struct got_pathlist_entry *pe;
1998 int ch, did_something = 0;
2000 TAILQ_INIT(&paths);
2002 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2003 switch (ch) {
2004 case 'b':
2005 branch_name = optarg;
2006 break;
2007 case 'c':
2008 commit_id_str = strdup(optarg);
2009 if (commit_id_str == NULL)
2010 return got_error_from_errno("strdup");
2011 break;
2012 default:
2013 usage_update();
2014 /* NOTREACHED */
2018 argc -= optind;
2019 argv += optind;
2021 #ifndef PROFILE
2022 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2023 "unveil", NULL) == -1)
2024 err(1, "pledge");
2025 #endif
2026 worktree_path = getcwd(NULL, 0);
2027 if (worktree_path == NULL) {
2028 error = got_error_from_errno("getcwd");
2029 goto done;
2031 error = got_worktree_open(&worktree, worktree_path);
2032 if (error)
2033 goto done;
2035 error = check_rebase_or_histedit_in_progress(worktree);
2036 if (error)
2037 goto done;
2039 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2040 NULL);
2041 if (error != NULL)
2042 goto done;
2044 error = apply_unveil(got_repo_get_path(repo), 0,
2045 got_worktree_get_root_path(worktree));
2046 if (error)
2047 goto done;
2049 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2050 if (error)
2051 goto done;
2053 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2054 got_worktree_get_head_ref_name(worktree), 0);
2055 if (error != NULL)
2056 goto done;
2057 if (commit_id_str == NULL) {
2058 error = got_ref_resolve(&commit_id, repo, head_ref);
2059 if (error != NULL)
2060 goto done;
2061 error = got_object_id_str(&commit_id_str, commit_id);
2062 if (error != NULL)
2063 goto done;
2064 } else {
2065 error = got_repo_match_object_id(&commit_id, NULL,
2066 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2067 free(commit_id_str);
2068 commit_id_str = NULL;
2069 if (error)
2070 goto done;
2071 error = got_object_id_str(&commit_id_str, commit_id);
2072 if (error)
2073 goto done;
2076 if (branch_name) {
2077 struct got_object_id *head_commit_id;
2078 TAILQ_FOREACH(pe, &paths, entry) {
2079 if (pe->path_len == 0)
2080 continue;
2081 error = got_error_msg(GOT_ERR_BAD_PATH,
2082 "switching between branches requires that "
2083 "the entire work tree gets updated");
2084 goto done;
2086 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2087 if (error)
2088 goto done;
2089 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2090 repo);
2091 free(head_commit_id);
2092 if (error != NULL)
2093 goto done;
2094 error = check_same_branch(commit_id, head_ref, NULL, repo);
2095 if (error)
2096 goto done;
2097 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2098 if (error)
2099 goto done;
2100 } else {
2101 error = check_linear_ancestry(commit_id,
2102 got_worktree_get_base_commit_id(worktree), 0, repo);
2103 if (error != NULL) {
2104 if (error->code == GOT_ERR_ANCESTRY)
2105 error = got_error(GOT_ERR_BRANCH_MOVED);
2106 goto done;
2108 error = check_same_branch(commit_id, head_ref, NULL, repo);
2109 if (error)
2110 goto done;
2113 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2114 commit_id) != 0) {
2115 error = got_worktree_set_base_commit_id(worktree, repo,
2116 commit_id);
2117 if (error)
2118 goto done;
2121 error = got_worktree_checkout_files(worktree, &paths, repo,
2122 update_progress, &did_something, check_cancelled, NULL);
2123 if (error != NULL)
2124 goto done;
2126 if (did_something)
2127 printf("Updated to commit %s\n", commit_id_str);
2128 else
2129 printf("Already up-to-date\n");
2130 done:
2131 free(worktree_path);
2132 TAILQ_FOREACH(pe, &paths, entry)
2133 free((char *)pe->path);
2134 got_pathlist_free(&paths);
2135 free(commit_id);
2136 free(commit_id_str);
2137 return error;
2140 static const struct got_error *
2141 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2142 const char *path, int diff_context, int ignore_whitespace,
2143 struct got_repository *repo)
2145 const struct got_error *err = NULL;
2146 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2148 if (blob_id1) {
2149 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2150 if (err)
2151 goto done;
2154 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2155 if (err)
2156 goto done;
2158 while (path[0] == '/')
2159 path++;
2160 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2161 ignore_whitespace, stdout);
2162 done:
2163 if (blob1)
2164 got_object_blob_close(blob1);
2165 got_object_blob_close(blob2);
2166 return err;
2169 static const struct got_error *
2170 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2171 const char *path, int diff_context, int ignore_whitespace,
2172 struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2176 struct got_diff_blob_output_unidiff_arg arg;
2178 if (tree_id1) {
2179 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2180 if (err)
2181 goto done;
2184 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2185 if (err)
2186 goto done;
2188 arg.diff_context = diff_context;
2189 arg.ignore_whitespace = ignore_whitespace;
2190 arg.outfile = stdout;
2191 while (path[0] == '/')
2192 path++;
2193 err = got_diff_tree(tree1, tree2, path, path, repo,
2194 got_diff_blob_output_unidiff, &arg, 1);
2195 done:
2196 if (tree1)
2197 got_object_tree_close(tree1);
2198 if (tree2)
2199 got_object_tree_close(tree2);
2200 return err;
2203 static const struct got_error *
2204 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2205 const char *path, int diff_context, struct got_repository *repo)
2207 const struct got_error *err = NULL;
2208 struct got_commit_object *pcommit = NULL;
2209 char *id_str1 = NULL, *id_str2 = NULL;
2210 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2211 struct got_object_qid *qid;
2213 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2214 if (qid != NULL) {
2215 err = got_object_open_as_commit(&pcommit, repo,
2216 qid->id);
2217 if (err)
2218 return err;
2221 if (path && path[0] != '\0') {
2222 int obj_type;
2223 err = got_object_id_by_path(&obj_id2, repo, id, path);
2224 if (err)
2225 goto done;
2226 err = got_object_id_str(&id_str2, obj_id2);
2227 if (err) {
2228 free(obj_id2);
2229 goto done;
2231 if (pcommit) {
2232 err = got_object_id_by_path(&obj_id1, repo,
2233 qid->id, path);
2234 if (err) {
2235 free(obj_id2);
2236 goto done;
2238 err = got_object_id_str(&id_str1, obj_id1);
2239 if (err) {
2240 free(obj_id2);
2241 goto done;
2244 err = got_object_get_type(&obj_type, repo, obj_id2);
2245 if (err) {
2246 free(obj_id2);
2247 goto done;
2249 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2250 switch (obj_type) {
2251 case GOT_OBJ_TYPE_BLOB:
2252 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2253 0, repo);
2254 break;
2255 case GOT_OBJ_TYPE_TREE:
2256 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2257 0, repo);
2258 break;
2259 default:
2260 err = got_error(GOT_ERR_OBJ_TYPE);
2261 break;
2263 free(obj_id1);
2264 free(obj_id2);
2265 } else {
2266 obj_id2 = got_object_commit_get_tree_id(commit);
2267 err = got_object_id_str(&id_str2, obj_id2);
2268 if (err)
2269 goto done;
2270 obj_id1 = got_object_commit_get_tree_id(pcommit);
2271 err = got_object_id_str(&id_str1, obj_id1);
2272 if (err)
2273 goto done;
2274 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2275 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2277 done:
2278 free(id_str1);
2279 free(id_str2);
2280 if (pcommit)
2281 got_object_commit_close(pcommit);
2282 return err;
2285 static char *
2286 get_datestr(time_t *time, char *datebuf)
2288 struct tm mytm, *tm;
2289 char *p, *s;
2291 tm = gmtime_r(time, &mytm);
2292 if (tm == NULL)
2293 return NULL;
2294 s = asctime_r(tm, datebuf);
2295 if (s == NULL)
2296 return NULL;
2297 p = strchr(s, '\n');
2298 if (p)
2299 *p = '\0';
2300 return s;
2303 static const struct got_error *
2304 match_logmsg(int *have_match, struct got_object_id *id,
2305 struct got_commit_object *commit, regex_t *regex)
2307 const struct got_error *err = NULL;
2308 regmatch_t regmatch;
2309 char *id_str = NULL, *logmsg = NULL;
2311 *have_match = 0;
2313 err = got_object_id_str(&id_str, id);
2314 if (err)
2315 return err;
2317 err = got_object_commit_get_logmsg(&logmsg, commit);
2318 if (err)
2319 goto done;
2321 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2322 *have_match = 1;
2323 done:
2324 free(id_str);
2325 free(logmsg);
2326 return err;
2329 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2331 static const struct got_error *
2332 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2333 struct got_repository *repo, const char *path, int show_patch,
2334 int diff_context, struct got_reflist_head *refs)
2336 const struct got_error *err = NULL;
2337 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2338 char datebuf[26];
2339 time_t committer_time;
2340 const char *author, *committer;
2341 char *refs_str = NULL;
2342 struct got_reflist_entry *re;
2344 SIMPLEQ_FOREACH(re, refs, entry) {
2345 char *s;
2346 const char *name;
2347 struct got_tag_object *tag = NULL;
2348 int cmp;
2350 name = got_ref_get_name(re->ref);
2351 if (strcmp(name, GOT_REF_HEAD) == 0)
2352 continue;
2353 if (strncmp(name, "refs/", 5) == 0)
2354 name += 5;
2355 if (strncmp(name, "got/", 4) == 0)
2356 continue;
2357 if (strncmp(name, "heads/", 6) == 0)
2358 name += 6;
2359 if (strncmp(name, "remotes/", 8) == 0)
2360 name += 8;
2361 if (strncmp(name, "tags/", 5) == 0) {
2362 err = got_object_open_as_tag(&tag, repo, re->id);
2363 if (err) {
2364 if (err->code != GOT_ERR_OBJ_TYPE)
2365 return err;
2366 /* Ref points at something other than a tag. */
2367 err = NULL;
2368 tag = NULL;
2371 cmp = got_object_id_cmp(tag ?
2372 got_object_tag_get_object_id(tag) : re->id, id);
2373 if (tag)
2374 got_object_tag_close(tag);
2375 if (cmp != 0)
2376 continue;
2377 s = refs_str;
2378 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2379 name) == -1) {
2380 err = got_error_from_errno("asprintf");
2381 free(s);
2382 return err;
2384 free(s);
2386 err = got_object_id_str(&id_str, id);
2387 if (err)
2388 return err;
2390 printf(GOT_COMMIT_SEP_STR);
2391 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2392 refs_str ? refs_str : "", refs_str ? ")" : "");
2393 free(id_str);
2394 id_str = NULL;
2395 free(refs_str);
2396 refs_str = NULL;
2397 printf("from: %s\n", got_object_commit_get_author(commit));
2398 committer_time = got_object_commit_get_committer_time(commit);
2399 datestr = get_datestr(&committer_time, datebuf);
2400 if (datestr)
2401 printf("date: %s UTC\n", datestr);
2402 author = got_object_commit_get_author(commit);
2403 committer = got_object_commit_get_committer(commit);
2404 if (strcmp(author, committer) != 0)
2405 printf("via: %s\n", committer);
2406 if (got_object_commit_get_nparents(commit) > 1) {
2407 const struct got_object_id_queue *parent_ids;
2408 struct got_object_qid *qid;
2409 int n = 1;
2410 parent_ids = got_object_commit_get_parent_ids(commit);
2411 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2412 err = got_object_id_str(&id_str, qid->id);
2413 if (err)
2414 return err;
2415 printf("parent %d: %s\n", n++, id_str);
2416 free(id_str);
2420 err = got_object_commit_get_logmsg(&logmsg0, commit);
2421 if (err)
2422 return err;
2424 logmsg = logmsg0;
2425 do {
2426 line = strsep(&logmsg, "\n");
2427 if (line)
2428 printf(" %s\n", line);
2429 } while (line);
2430 free(logmsg0);
2432 if (show_patch) {
2433 err = print_patch(commit, id, path, diff_context, repo);
2434 if (err == 0)
2435 printf("\n");
2438 if (fflush(stdout) != 0 && err == NULL)
2439 err = got_error_from_errno("fflush");
2440 return err;
2443 static const struct got_error *
2444 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2445 const char *path, int show_patch, const char *search_pattern,
2446 int diff_context, int limit, int log_branches,
2447 struct got_reflist_head *refs)
2449 const struct got_error *err;
2450 struct got_commit_graph *graph;
2451 regex_t regex;
2452 int have_match;
2454 if (search_pattern &&
2455 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2456 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2458 err = got_commit_graph_open(&graph, path, !log_branches);
2459 if (err)
2460 return err;
2461 err = got_commit_graph_iter_start(graph, root_id, repo,
2462 check_cancelled, NULL);
2463 if (err)
2464 goto done;
2465 for (;;) {
2466 struct got_commit_object *commit;
2467 struct got_object_id *id;
2469 if (sigint_received || sigpipe_received)
2470 break;
2472 err = got_commit_graph_iter_next(&id, graph, repo,
2473 check_cancelled, NULL);
2474 if (err) {
2475 if (err->code == GOT_ERR_ITER_COMPLETED)
2476 err = NULL;
2477 break;
2479 if (id == NULL)
2480 break;
2482 err = got_object_open_as_commit(&commit, repo, id);
2483 if (err)
2484 break;
2486 if (search_pattern) {
2487 err = match_logmsg(&have_match, id, commit, &regex);
2488 if (err) {
2489 got_object_commit_close(commit);
2490 break;
2492 if (have_match == 0) {
2493 got_object_commit_close(commit);
2494 continue;
2498 err = print_commit(commit, id, repo, path, show_patch,
2499 diff_context, refs);
2500 got_object_commit_close(commit);
2501 if (err || (limit && --limit == 0))
2502 break;
2504 done:
2505 if (search_pattern)
2506 regfree(&regex);
2507 got_commit_graph_close(graph);
2508 return err;
2511 __dead static void
2512 usage_log(void)
2514 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2515 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2516 exit(1);
2519 static int
2520 get_default_log_limit(void)
2522 const char *got_default_log_limit;
2523 long long n;
2524 const char *errstr;
2526 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2527 if (got_default_log_limit == NULL)
2528 return 0;
2529 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2530 if (errstr != NULL)
2531 return 0;
2532 return n;
2535 static const struct got_error *
2536 cmd_log(int argc, char *argv[])
2538 const struct got_error *error;
2539 struct got_repository *repo = NULL;
2540 struct got_worktree *worktree = NULL;
2541 struct got_commit_object *commit = NULL;
2542 struct got_object_id *id = NULL;
2543 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2544 const char *start_commit = NULL, *search_pattern = NULL;
2545 int diff_context = -1, ch;
2546 int show_patch = 0, limit = 0, log_branches = 0;
2547 const char *errstr;
2548 struct got_reflist_head refs;
2550 SIMPLEQ_INIT(&refs);
2552 #ifndef PROFILE
2553 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2554 NULL)
2555 == -1)
2556 err(1, "pledge");
2557 #endif
2559 limit = get_default_log_limit();
2561 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2562 switch (ch) {
2563 case 'p':
2564 show_patch = 1;
2565 break;
2566 case 'c':
2567 start_commit = optarg;
2568 break;
2569 case 'C':
2570 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2571 &errstr);
2572 if (errstr != NULL)
2573 err(1, "-C option %s", errstr);
2574 break;
2575 case 'l':
2576 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2577 if (errstr != NULL)
2578 err(1, "-l option %s", errstr);
2579 break;
2580 case 'b':
2581 log_branches = 1;
2582 break;
2583 case 'r':
2584 repo_path = realpath(optarg, NULL);
2585 if (repo_path == NULL)
2586 return got_error_from_errno2("realpath",
2587 optarg);
2588 got_path_strip_trailing_slashes(repo_path);
2589 break;
2590 case 's':
2591 search_pattern = optarg;
2592 break;
2593 default:
2594 usage_log();
2595 /* NOTREACHED */
2599 argc -= optind;
2600 argv += optind;
2602 if (diff_context == -1)
2603 diff_context = 3;
2604 else if (!show_patch)
2605 errx(1, "-C reguires -p");
2607 cwd = getcwd(NULL, 0);
2608 if (cwd == NULL) {
2609 error = got_error_from_errno("getcwd");
2610 goto done;
2613 error = got_worktree_open(&worktree, cwd);
2614 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2615 goto done;
2616 error = NULL;
2618 if (argc == 0) {
2619 path = strdup("");
2620 if (path == NULL) {
2621 error = got_error_from_errno("strdup");
2622 goto done;
2624 } else if (argc == 1) {
2625 if (worktree) {
2626 error = got_worktree_resolve_path(&path, worktree,
2627 argv[0]);
2628 if (error)
2629 goto done;
2630 } else {
2631 path = strdup(argv[0]);
2632 if (path == NULL) {
2633 error = got_error_from_errno("strdup");
2634 goto done;
2637 } else
2638 usage_log();
2640 if (repo_path == NULL) {
2641 repo_path = worktree ?
2642 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2644 if (repo_path == NULL) {
2645 error = got_error_from_errno("strdup");
2646 goto done;
2649 error = got_repo_open(&repo, repo_path, NULL);
2650 if (error != NULL)
2651 goto done;
2653 error = apply_unveil(got_repo_get_path(repo), 1,
2654 worktree ? got_worktree_get_root_path(worktree) : NULL);
2655 if (error)
2656 goto done;
2658 if (start_commit == NULL) {
2659 struct got_reference *head_ref;
2660 error = got_ref_open(&head_ref, repo,
2661 worktree ? got_worktree_get_head_ref_name(worktree)
2662 : GOT_REF_HEAD, 0);
2663 if (error != NULL)
2664 return error;
2665 error = got_ref_resolve(&id, repo, head_ref);
2666 got_ref_close(head_ref);
2667 if (error != NULL)
2668 return error;
2669 error = got_object_open_as_commit(&commit, repo, id);
2670 } else {
2671 struct got_reference *ref;
2672 error = got_ref_open(&ref, repo, start_commit, 0);
2673 if (error == NULL) {
2674 int obj_type;
2675 error = got_ref_resolve(&id, repo, ref);
2676 got_ref_close(ref);
2677 if (error != NULL)
2678 goto done;
2679 error = got_object_get_type(&obj_type, repo, id);
2680 if (error != NULL)
2681 goto done;
2682 if (obj_type == GOT_OBJ_TYPE_TAG) {
2683 struct got_tag_object *tag;
2684 error = got_object_open_as_tag(&tag, repo, id);
2685 if (error != NULL)
2686 goto done;
2687 if (got_object_tag_get_object_type(tag) !=
2688 GOT_OBJ_TYPE_COMMIT) {
2689 got_object_tag_close(tag);
2690 error = got_error(GOT_ERR_OBJ_TYPE);
2691 goto done;
2693 free(id);
2694 id = got_object_id_dup(
2695 got_object_tag_get_object_id(tag));
2696 if (id == NULL)
2697 error = got_error_from_errno(
2698 "got_object_id_dup");
2699 got_object_tag_close(tag);
2700 if (error)
2701 goto done;
2702 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2703 error = got_error(GOT_ERR_OBJ_TYPE);
2704 goto done;
2706 error = got_object_open_as_commit(&commit, repo, id);
2707 if (error != NULL)
2708 goto done;
2710 if (commit == NULL) {
2711 error = got_repo_match_object_id_prefix(&id,
2712 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2713 if (error != NULL)
2714 return error;
2717 if (error != NULL)
2718 goto done;
2720 if (worktree) {
2721 const char *prefix = got_worktree_get_path_prefix(worktree);
2722 char *p;
2723 if (asprintf(&p, "%s%s%s", prefix,
2724 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2725 error = got_error_from_errno("asprintf");
2726 goto done;
2728 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2729 free(p);
2730 } else
2731 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2732 if (error != NULL)
2733 goto done;
2734 if (in_repo_path) {
2735 free(path);
2736 path = in_repo_path;
2739 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2740 if (error)
2741 goto done;
2743 error = print_commits(id, repo, path, show_patch, search_pattern,
2744 diff_context, limit, log_branches, &refs);
2745 done:
2746 free(path);
2747 free(repo_path);
2748 free(cwd);
2749 free(id);
2750 if (worktree)
2751 got_worktree_close(worktree);
2752 if (repo) {
2753 const struct got_error *repo_error;
2754 repo_error = got_repo_close(repo);
2755 if (error == NULL)
2756 error = repo_error;
2758 got_ref_list_free(&refs);
2759 return error;
2762 __dead static void
2763 usage_diff(void)
2765 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2766 "[-w] [object1 object2 | path]\n", getprogname());
2767 exit(1);
2770 struct print_diff_arg {
2771 struct got_repository *repo;
2772 struct got_worktree *worktree;
2773 int diff_context;
2774 const char *id_str;
2775 int header_shown;
2776 int diff_staged;
2777 int ignore_whitespace;
2780 static const struct got_error *
2781 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2782 const char *path, struct got_object_id *blob_id,
2783 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2784 int dirfd, const char *de_name)
2786 struct print_diff_arg *a = arg;
2787 const struct got_error *err = NULL;
2788 struct got_blob_object *blob1 = NULL;
2789 int fd = -1;
2790 FILE *f2 = NULL;
2791 char *abspath = NULL, *label1 = NULL;
2792 struct stat sb;
2794 if (a->diff_staged) {
2795 if (staged_status != GOT_STATUS_MODIFY &&
2796 staged_status != GOT_STATUS_ADD &&
2797 staged_status != GOT_STATUS_DELETE)
2798 return NULL;
2799 } else {
2800 if (staged_status == GOT_STATUS_DELETE)
2801 return NULL;
2802 if (status == GOT_STATUS_NONEXISTENT)
2803 return got_error_set_errno(ENOENT, path);
2804 if (status != GOT_STATUS_MODIFY &&
2805 status != GOT_STATUS_ADD &&
2806 status != GOT_STATUS_DELETE &&
2807 status != GOT_STATUS_CONFLICT)
2808 return NULL;
2811 if (!a->header_shown) {
2812 printf("diff %s %s%s\n", a->id_str,
2813 got_worktree_get_root_path(a->worktree),
2814 a->diff_staged ? " (staged changes)" : "");
2815 a->header_shown = 1;
2818 if (a->diff_staged) {
2819 const char *label1 = NULL, *label2 = NULL;
2820 switch (staged_status) {
2821 case GOT_STATUS_MODIFY:
2822 label1 = path;
2823 label2 = path;
2824 break;
2825 case GOT_STATUS_ADD:
2826 label2 = path;
2827 break;
2828 case GOT_STATUS_DELETE:
2829 label1 = path;
2830 break;
2831 default:
2832 return got_error(GOT_ERR_FILE_STATUS);
2834 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2835 label1, label2, a->diff_context, a->ignore_whitespace,
2836 a->repo, stdout);
2839 if (staged_status == GOT_STATUS_ADD ||
2840 staged_status == GOT_STATUS_MODIFY) {
2841 char *id_str;
2842 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2843 8192);
2844 if (err)
2845 goto done;
2846 err = got_object_id_str(&id_str, staged_blob_id);
2847 if (err)
2848 goto done;
2849 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2850 err = got_error_from_errno("asprintf");
2851 free(id_str);
2852 goto done;
2854 free(id_str);
2855 } else if (status != GOT_STATUS_ADD) {
2856 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2857 if (err)
2858 goto done;
2861 if (status != GOT_STATUS_DELETE) {
2862 if (asprintf(&abspath, "%s/%s",
2863 got_worktree_get_root_path(a->worktree), path) == -1) {
2864 err = got_error_from_errno("asprintf");
2865 goto done;
2868 if (dirfd != -1) {
2869 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2870 if (fd == -1) {
2871 err = got_error_from_errno2("openat", abspath);
2872 goto done;
2874 } else {
2875 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2876 if (fd == -1) {
2877 err = got_error_from_errno2("open", abspath);
2878 goto done;
2881 if (fstat(fd, &sb) == -1) {
2882 err = got_error_from_errno2("fstat", abspath);
2883 goto done;
2885 f2 = fdopen(fd, "r");
2886 if (f2 == NULL) {
2887 err = got_error_from_errno2("fdopen", abspath);
2888 goto done;
2890 fd = -1;
2891 } else
2892 sb.st_size = 0;
2894 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2895 a->diff_context, a->ignore_whitespace, stdout);
2896 done:
2897 if (blob1)
2898 got_object_blob_close(blob1);
2899 if (f2 && fclose(f2) == EOF && err == NULL)
2900 err = got_error_from_errno("fclose");
2901 if (fd != -1 && close(fd) == -1 && err == NULL)
2902 err = got_error_from_errno("close");
2903 free(abspath);
2904 return err;
2907 static const struct got_error *
2908 cmd_diff(int argc, char *argv[])
2910 const struct got_error *error;
2911 struct got_repository *repo = NULL;
2912 struct got_worktree *worktree = NULL;
2913 char *cwd = NULL, *repo_path = NULL;
2914 struct got_object_id *id1 = NULL, *id2 = NULL;
2915 const char *id_str1 = NULL, *id_str2 = NULL;
2916 char *label1 = NULL, *label2 = NULL;
2917 int type1, type2;
2918 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2919 const char *errstr;
2920 char *path = NULL;
2922 #ifndef PROFILE
2923 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2924 NULL) == -1)
2925 err(1, "pledge");
2926 #endif
2928 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2929 switch (ch) {
2930 case 'C':
2931 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2932 &errstr);
2933 if (errstr != NULL)
2934 err(1, "-C option %s", errstr);
2935 break;
2936 case 'r':
2937 repo_path = realpath(optarg, NULL);
2938 if (repo_path == NULL)
2939 return got_error_from_errno2("realpath",
2940 optarg);
2941 got_path_strip_trailing_slashes(repo_path);
2942 break;
2943 case 's':
2944 diff_staged = 1;
2945 break;
2946 case 'w':
2947 ignore_whitespace = 1;
2948 break;
2949 default:
2950 usage_diff();
2951 /* NOTREACHED */
2955 argc -= optind;
2956 argv += optind;
2958 cwd = getcwd(NULL, 0);
2959 if (cwd == NULL) {
2960 error = got_error_from_errno("getcwd");
2961 goto done;
2963 error = got_worktree_open(&worktree, cwd);
2964 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2965 goto done;
2966 if (argc <= 1) {
2967 if (worktree == NULL) {
2968 error = got_error(GOT_ERR_NOT_WORKTREE);
2969 goto done;
2971 if (repo_path)
2972 errx(1,
2973 "-r option can't be used when diffing a work tree");
2974 repo_path = strdup(got_worktree_get_repo_path(worktree));
2975 if (repo_path == NULL) {
2976 error = got_error_from_errno("strdup");
2977 goto done;
2979 if (argc == 1) {
2980 error = got_worktree_resolve_path(&path, worktree,
2981 argv[0]);
2982 if (error)
2983 goto done;
2984 } else {
2985 path = strdup("");
2986 if (path == NULL) {
2987 error = got_error_from_errno("strdup");
2988 goto done;
2991 } else if (argc == 2) {
2992 if (diff_staged)
2993 errx(1, "-s option can't be used when diffing "
2994 "objects in repository");
2995 id_str1 = argv[0];
2996 id_str2 = argv[1];
2997 if (worktree && repo_path == NULL) {
2998 repo_path =
2999 strdup(got_worktree_get_repo_path(worktree));
3000 if (repo_path == NULL) {
3001 error = got_error_from_errno("strdup");
3002 goto done;
3005 } else
3006 usage_diff();
3008 if (repo_path == NULL) {
3009 repo_path = getcwd(NULL, 0);
3010 if (repo_path == NULL)
3011 return got_error_from_errno("getcwd");
3014 error = got_repo_open(&repo, repo_path, NULL);
3015 free(repo_path);
3016 if (error != NULL)
3017 goto done;
3019 error = apply_unveil(got_repo_get_path(repo), 1,
3020 worktree ? got_worktree_get_root_path(worktree) : NULL);
3021 if (error)
3022 goto done;
3024 if (argc <= 1) {
3025 struct print_diff_arg arg;
3026 struct got_pathlist_head paths;
3027 char *id_str;
3029 TAILQ_INIT(&paths);
3031 error = got_object_id_str(&id_str,
3032 got_worktree_get_base_commit_id(worktree));
3033 if (error)
3034 goto done;
3035 arg.repo = repo;
3036 arg.worktree = worktree;
3037 arg.diff_context = diff_context;
3038 arg.id_str = id_str;
3039 arg.header_shown = 0;
3040 arg.diff_staged = diff_staged;
3041 arg.ignore_whitespace = ignore_whitespace;
3043 error = got_pathlist_append(&paths, path, NULL);
3044 if (error)
3045 goto done;
3047 error = got_worktree_status(worktree, &paths, repo, print_diff,
3048 &arg, check_cancelled, NULL);
3049 free(id_str);
3050 got_pathlist_free(&paths);
3051 goto done;
3054 error = got_repo_match_object_id(&id1, &label1, id_str1,
3055 GOT_OBJ_TYPE_ANY, 1, repo);
3056 if (error)
3057 goto done;
3059 error = got_repo_match_object_id(&id2, &label2, id_str2,
3060 GOT_OBJ_TYPE_ANY, 1, repo);
3061 if (error)
3062 goto done;
3064 error = got_object_get_type(&type1, repo, id1);
3065 if (error)
3066 goto done;
3068 error = got_object_get_type(&type2, repo, id2);
3069 if (error)
3070 goto done;
3072 if (type1 != type2) {
3073 error = got_error(GOT_ERR_OBJ_TYPE);
3074 goto done;
3077 switch (type1) {
3078 case GOT_OBJ_TYPE_BLOB:
3079 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3080 diff_context, ignore_whitespace, repo, stdout);
3081 break;
3082 case GOT_OBJ_TYPE_TREE:
3083 error = got_diff_objects_as_trees(id1, id2, "", "",
3084 diff_context, ignore_whitespace, repo, stdout);
3085 break;
3086 case GOT_OBJ_TYPE_COMMIT:
3087 printf("diff %s %s\n", label1, label2);
3088 error = got_diff_objects_as_commits(id1, id2, diff_context,
3089 ignore_whitespace, repo, stdout);
3090 break;
3091 default:
3092 error = got_error(GOT_ERR_OBJ_TYPE);
3094 done:
3095 free(label1);
3096 free(label2);
3097 free(id1);
3098 free(id2);
3099 free(path);
3100 if (worktree)
3101 got_worktree_close(worktree);
3102 if (repo) {
3103 const struct got_error *repo_error;
3104 repo_error = got_repo_close(repo);
3105 if (error == NULL)
3106 error = repo_error;
3108 return error;
3111 __dead static void
3112 usage_blame(void)
3114 fprintf(stderr,
3115 "usage: %s blame [-c commit] [-r repository-path] path\n",
3116 getprogname());
3117 exit(1);
3120 struct blame_line {
3121 int annotated;
3122 char *id_str;
3123 char *committer;
3124 char datebuf[11]; /* YYYY-MM-DD + NUL */
3127 struct blame_cb_args {
3128 struct blame_line *lines;
3129 int nlines;
3130 int nlines_prec;
3131 int lineno_cur;
3132 off_t *line_offsets;
3133 FILE *f;
3134 struct got_repository *repo;
3137 static const struct got_error *
3138 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3140 const struct got_error *err = NULL;
3141 struct blame_cb_args *a = arg;
3142 struct blame_line *bline;
3143 char *line = NULL;
3144 size_t linesize = 0;
3145 struct got_commit_object *commit = NULL;
3146 off_t offset;
3147 struct tm tm;
3148 time_t committer_time;
3150 if (nlines != a->nlines ||
3151 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3152 return got_error(GOT_ERR_RANGE);
3154 if (sigint_received)
3155 return got_error(GOT_ERR_ITER_COMPLETED);
3157 if (lineno == -1)
3158 return NULL; /* no change in this commit */
3160 /* Annotate this line. */
3161 bline = &a->lines[lineno - 1];
3162 if (bline->annotated)
3163 return NULL;
3164 err = got_object_id_str(&bline->id_str, id);
3165 if (err)
3166 return err;
3168 err = got_object_open_as_commit(&commit, a->repo, id);
3169 if (err)
3170 goto done;
3172 bline->committer = strdup(got_object_commit_get_committer(commit));
3173 if (bline->committer == NULL) {
3174 err = got_error_from_errno("strdup");
3175 goto done;
3178 committer_time = got_object_commit_get_committer_time(commit);
3179 if (localtime_r(&committer_time, &tm) == NULL)
3180 return got_error_from_errno("localtime_r");
3181 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3182 &tm) >= sizeof(bline->datebuf)) {
3183 err = got_error(GOT_ERR_NO_SPACE);
3184 goto done;
3186 bline->annotated = 1;
3188 /* Print lines annotated so far. */
3189 bline = &a->lines[a->lineno_cur - 1];
3190 if (!bline->annotated)
3191 goto done;
3193 offset = a->line_offsets[a->lineno_cur - 1];
3194 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3195 err = got_error_from_errno("fseeko");
3196 goto done;
3199 while (bline->annotated) {
3200 char *smallerthan, *at, *nl, *committer;
3201 size_t len;
3203 if (getline(&line, &linesize, a->f) == -1) {
3204 if (ferror(a->f))
3205 err = got_error_from_errno("getline");
3206 break;
3209 committer = bline->committer;
3210 smallerthan = strchr(committer, '<');
3211 if (smallerthan && smallerthan[1] != '\0')
3212 committer = smallerthan + 1;
3213 at = strchr(committer, '@');
3214 if (at)
3215 *at = '\0';
3216 len = strlen(committer);
3217 if (len >= 9)
3218 committer[8] = '\0';
3220 nl = strchr(line, '\n');
3221 if (nl)
3222 *nl = '\0';
3223 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3224 bline->id_str, bline->datebuf, committer, line);
3226 a->lineno_cur++;
3227 bline = &a->lines[a->lineno_cur - 1];
3229 done:
3230 if (commit)
3231 got_object_commit_close(commit);
3232 free(line);
3233 return err;
3236 static const struct got_error *
3237 cmd_blame(int argc, char *argv[])
3239 const struct got_error *error;
3240 struct got_repository *repo = NULL;
3241 struct got_worktree *worktree = NULL;
3242 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3243 struct got_object_id *obj_id = NULL;
3244 struct got_object_id *commit_id = NULL;
3245 struct got_blob_object *blob = NULL;
3246 char *commit_id_str = NULL;
3247 struct blame_cb_args bca;
3248 int ch, obj_type, i;
3249 size_t filesize;
3251 memset(&bca, 0, sizeof(bca));
3253 #ifndef PROFILE
3254 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3255 NULL) == -1)
3256 err(1, "pledge");
3257 #endif
3259 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3260 switch (ch) {
3261 case 'c':
3262 commit_id_str = optarg;
3263 break;
3264 case 'r':
3265 repo_path = realpath(optarg, NULL);
3266 if (repo_path == NULL)
3267 return got_error_from_errno2("realpath",
3268 optarg);
3269 got_path_strip_trailing_slashes(repo_path);
3270 break;
3271 default:
3272 usage_blame();
3273 /* NOTREACHED */
3277 argc -= optind;
3278 argv += optind;
3280 if (argc == 1)
3281 path = argv[0];
3282 else
3283 usage_blame();
3285 cwd = getcwd(NULL, 0);
3286 if (cwd == NULL) {
3287 error = got_error_from_errno("getcwd");
3288 goto done;
3290 if (repo_path == NULL) {
3291 error = got_worktree_open(&worktree, cwd);
3292 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3293 goto done;
3294 else
3295 error = NULL;
3296 if (worktree) {
3297 repo_path =
3298 strdup(got_worktree_get_repo_path(worktree));
3299 if (repo_path == NULL)
3300 error = got_error_from_errno("strdup");
3301 if (error)
3302 goto done;
3303 } else {
3304 repo_path = strdup(cwd);
3305 if (repo_path == NULL) {
3306 error = got_error_from_errno("strdup");
3307 goto done;
3312 error = got_repo_open(&repo, repo_path, NULL);
3313 if (error != NULL)
3314 goto done;
3316 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3317 if (error)
3318 goto done;
3320 if (worktree) {
3321 const char *prefix = got_worktree_get_path_prefix(worktree);
3322 char *p, *worktree_subdir = cwd +
3323 strlen(got_worktree_get_root_path(worktree));
3324 if (asprintf(&p, "%s%s%s%s%s",
3325 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3326 worktree_subdir, worktree_subdir[0] ? "/" : "",
3327 path) == -1) {
3328 error = got_error_from_errno("asprintf");
3329 goto done;
3331 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3332 free(p);
3333 } else {
3334 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3336 if (error)
3337 goto done;
3339 if (commit_id_str == NULL) {
3340 struct got_reference *head_ref;
3341 error = got_ref_open(&head_ref, repo, worktree ?
3342 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3343 if (error != NULL)
3344 goto done;
3345 error = got_ref_resolve(&commit_id, repo, head_ref);
3346 got_ref_close(head_ref);
3347 if (error != NULL)
3348 goto done;
3349 } else {
3350 error = got_repo_match_object_id(&commit_id, NULL,
3351 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3352 if (error)
3353 goto done;
3356 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3357 if (error)
3358 goto done;
3360 error = got_object_get_type(&obj_type, repo, obj_id);
3361 if (error)
3362 goto done;
3364 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3365 error = got_error(GOT_ERR_OBJ_TYPE);
3366 goto done;
3369 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3370 if (error)
3371 goto done;
3372 bca.f = got_opentemp();
3373 if (bca.f == NULL) {
3374 error = got_error_from_errno("got_opentemp");
3375 goto done;
3377 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3378 &bca.line_offsets, bca.f, blob);
3379 if (error || bca.nlines == 0)
3380 goto done;
3382 /* Don't include \n at EOF in the blame line count. */
3383 if (bca.line_offsets[bca.nlines - 1] == filesize)
3384 bca.nlines--;
3386 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3387 if (bca.lines == NULL) {
3388 error = got_error_from_errno("calloc");
3389 goto done;
3391 bca.lineno_cur = 1;
3392 bca.nlines_prec = 0;
3393 i = bca.nlines;
3394 while (i > 0) {
3395 i /= 10;
3396 bca.nlines_prec++;
3398 bca.repo = repo;
3400 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3401 check_cancelled, NULL);
3402 done:
3403 free(in_repo_path);
3404 free(repo_path);
3405 free(cwd);
3406 free(commit_id);
3407 free(obj_id);
3408 if (blob)
3409 got_object_blob_close(blob);
3410 if (worktree)
3411 got_worktree_close(worktree);
3412 if (repo) {
3413 const struct got_error *repo_error;
3414 repo_error = got_repo_close(repo);
3415 if (error == NULL)
3416 error = repo_error;
3418 if (bca.lines) {
3419 for (i = 0; i < bca.nlines; i++) {
3420 struct blame_line *bline = &bca.lines[i];
3421 free(bline->id_str);
3422 free(bline->committer);
3424 free(bca.lines);
3426 free(bca.line_offsets);
3427 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3428 error = got_error_from_errno("fclose");
3429 return error;
3432 __dead static void
3433 usage_tree(void)
3435 fprintf(stderr,
3436 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3437 getprogname());
3438 exit(1);
3441 static void
3442 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3443 const char *root_path)
3445 int is_root_path = (strcmp(path, root_path) == 0);
3446 const char *modestr = "";
3447 mode_t mode = got_tree_entry_get_mode(te);
3449 path += strlen(root_path);
3450 while (path[0] == '/')
3451 path++;
3453 if (got_object_tree_entry_is_submodule(te))
3454 modestr = "$";
3455 else if (S_ISLNK(mode))
3456 modestr = "@";
3457 else if (S_ISDIR(mode))
3458 modestr = "/";
3459 else if (mode & S_IXUSR)
3460 modestr = "*";
3462 printf("%s%s%s%s%s\n", id ? id : "", path,
3463 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3466 static const struct got_error *
3467 print_tree(const char *path, struct got_object_id *commit_id,
3468 int show_ids, int recurse, const char *root_path,
3469 struct got_repository *repo)
3471 const struct got_error *err = NULL;
3472 struct got_object_id *tree_id = NULL;
3473 struct got_tree_object *tree = NULL;
3474 int nentries, i;
3476 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3477 if (err)
3478 goto done;
3480 err = got_object_open_as_tree(&tree, repo, tree_id);
3481 if (err)
3482 goto done;
3483 nentries = got_object_tree_get_nentries(tree);
3484 for (i = 0; i < nentries; i++) {
3485 struct got_tree_entry *te;
3486 char *id = NULL;
3488 if (sigint_received || sigpipe_received)
3489 break;
3491 te = got_object_tree_get_entry(tree, i);
3492 if (show_ids) {
3493 char *id_str;
3494 err = got_object_id_str(&id_str,
3495 got_tree_entry_get_id(te));
3496 if (err)
3497 goto done;
3498 if (asprintf(&id, "%s ", id_str) == -1) {
3499 err = got_error_from_errno("asprintf");
3500 free(id_str);
3501 goto done;
3503 free(id_str);
3505 print_entry(te, id, path, root_path);
3506 free(id);
3508 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3509 char *child_path;
3510 if (asprintf(&child_path, "%s%s%s", path,
3511 path[0] == '/' && path[1] == '\0' ? "" : "/",
3512 got_tree_entry_get_name(te)) == -1) {
3513 err = got_error_from_errno("asprintf");
3514 goto done;
3516 err = print_tree(child_path, commit_id, show_ids, 1,
3517 root_path, repo);
3518 free(child_path);
3519 if (err)
3520 goto done;
3523 done:
3524 if (tree)
3525 got_object_tree_close(tree);
3526 free(tree_id);
3527 return err;
3530 static const struct got_error *
3531 cmd_tree(int argc, char *argv[])
3533 const struct got_error *error;
3534 struct got_repository *repo = NULL;
3535 struct got_worktree *worktree = NULL;
3536 const char *path;
3537 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3538 struct got_object_id *commit_id = NULL;
3539 char *commit_id_str = NULL;
3540 int show_ids = 0, recurse = 0;
3541 int ch;
3543 #ifndef PROFILE
3544 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3545 NULL) == -1)
3546 err(1, "pledge");
3547 #endif
3549 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3550 switch (ch) {
3551 case 'c':
3552 commit_id_str = optarg;
3553 break;
3554 case 'r':
3555 repo_path = realpath(optarg, NULL);
3556 if (repo_path == NULL)
3557 return got_error_from_errno2("realpath",
3558 optarg);
3559 got_path_strip_trailing_slashes(repo_path);
3560 break;
3561 case 'i':
3562 show_ids = 1;
3563 break;
3564 case 'R':
3565 recurse = 1;
3566 break;
3567 default:
3568 usage_tree();
3569 /* NOTREACHED */
3573 argc -= optind;
3574 argv += optind;
3576 if (argc == 1)
3577 path = argv[0];
3578 else if (argc > 1)
3579 usage_tree();
3580 else
3581 path = NULL;
3583 cwd = getcwd(NULL, 0);
3584 if (cwd == NULL) {
3585 error = got_error_from_errno("getcwd");
3586 goto done;
3588 if (repo_path == NULL) {
3589 error = got_worktree_open(&worktree, cwd);
3590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3591 goto done;
3592 else
3593 error = NULL;
3594 if (worktree) {
3595 repo_path =
3596 strdup(got_worktree_get_repo_path(worktree));
3597 if (repo_path == NULL)
3598 error = got_error_from_errno("strdup");
3599 if (error)
3600 goto done;
3601 } else {
3602 repo_path = strdup(cwd);
3603 if (repo_path == NULL) {
3604 error = got_error_from_errno("strdup");
3605 goto done;
3610 error = got_repo_open(&repo, repo_path, NULL);
3611 if (error != NULL)
3612 goto done;
3614 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3615 if (error)
3616 goto done;
3618 if (path == NULL) {
3619 if (worktree) {
3620 char *p, *worktree_subdir = cwd +
3621 strlen(got_worktree_get_root_path(worktree));
3622 if (asprintf(&p, "%s/%s",
3623 got_worktree_get_path_prefix(worktree),
3624 worktree_subdir) == -1) {
3625 error = got_error_from_errno("asprintf");
3626 goto done;
3628 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3629 free(p);
3630 if (error)
3631 goto done;
3632 } else
3633 path = "/";
3635 if (in_repo_path == NULL) {
3636 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3637 if (error != NULL)
3638 goto done;
3641 if (commit_id_str == NULL) {
3642 struct got_reference *head_ref;
3643 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3644 if (error != NULL)
3645 goto done;
3646 error = got_ref_resolve(&commit_id, repo, head_ref);
3647 got_ref_close(head_ref);
3648 if (error != NULL)
3649 goto done;
3650 } else {
3651 error = got_repo_match_object_id(&commit_id, NULL,
3652 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3653 if (error)
3654 goto done;
3657 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3658 in_repo_path, repo);
3659 done:
3660 free(in_repo_path);
3661 free(repo_path);
3662 free(cwd);
3663 free(commit_id);
3664 if (worktree)
3665 got_worktree_close(worktree);
3666 if (repo) {
3667 const struct got_error *repo_error;
3668 repo_error = got_repo_close(repo);
3669 if (error == NULL)
3670 error = repo_error;
3672 return error;
3675 __dead static void
3676 usage_status(void)
3678 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3679 exit(1);
3682 static const struct got_error *
3683 print_status(void *arg, unsigned char status, unsigned char staged_status,
3684 const char *path, struct got_object_id *blob_id,
3685 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3686 int dirfd, const char *de_name)
3688 if (status == staged_status && (status == GOT_STATUS_DELETE))
3689 status = GOT_STATUS_NO_CHANGE;
3690 printf("%c%c %s\n", status, staged_status, path);
3691 return NULL;
3694 static const struct got_error *
3695 cmd_status(int argc, char *argv[])
3697 const struct got_error *error = NULL;
3698 struct got_repository *repo = NULL;
3699 struct got_worktree *worktree = NULL;
3700 char *cwd = NULL;
3701 struct got_pathlist_head paths;
3702 struct got_pathlist_entry *pe;
3703 int ch;
3705 TAILQ_INIT(&paths);
3707 while ((ch = getopt(argc, argv, "")) != -1) {
3708 switch (ch) {
3709 default:
3710 usage_status();
3711 /* NOTREACHED */
3715 argc -= optind;
3716 argv += optind;
3718 #ifndef PROFILE
3719 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3720 NULL) == -1)
3721 err(1, "pledge");
3722 #endif
3723 cwd = getcwd(NULL, 0);
3724 if (cwd == NULL) {
3725 error = got_error_from_errno("getcwd");
3726 goto done;
3729 error = got_worktree_open(&worktree, cwd);
3730 if (error != NULL)
3731 goto done;
3733 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3734 NULL);
3735 if (error != NULL)
3736 goto done;
3738 error = apply_unveil(got_repo_get_path(repo), 1,
3739 got_worktree_get_root_path(worktree));
3740 if (error)
3741 goto done;
3743 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3744 if (error)
3745 goto done;
3747 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3748 check_cancelled, NULL);
3749 done:
3750 TAILQ_FOREACH(pe, &paths, entry)
3751 free((char *)pe->path);
3752 got_pathlist_free(&paths);
3753 free(cwd);
3754 return error;
3757 __dead static void
3758 usage_ref(void)
3760 fprintf(stderr,
3761 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3762 getprogname());
3763 exit(1);
3766 static const struct got_error *
3767 list_refs(struct got_repository *repo)
3769 static const struct got_error *err = NULL;
3770 struct got_reflist_head refs;
3771 struct got_reflist_entry *re;
3773 SIMPLEQ_INIT(&refs);
3774 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3775 if (err)
3776 return err;
3778 SIMPLEQ_FOREACH(re, &refs, entry) {
3779 char *refstr;
3780 refstr = got_ref_to_str(re->ref);
3781 if (refstr == NULL)
3782 return got_error_from_errno("got_ref_to_str");
3783 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3784 free(refstr);
3787 got_ref_list_free(&refs);
3788 return NULL;
3791 static const struct got_error *
3792 delete_ref(struct got_repository *repo, const char *refname)
3794 const struct got_error *err = NULL;
3795 struct got_reference *ref;
3797 err = got_ref_open(&ref, repo, refname, 0);
3798 if (err)
3799 return err;
3801 err = got_ref_delete(ref, repo);
3802 got_ref_close(ref);
3803 return err;
3806 static const struct got_error *
3807 add_ref(struct got_repository *repo, const char *refname, const char *target)
3809 const struct got_error *err = NULL;
3810 struct got_object_id *id;
3811 struct got_reference *ref = NULL;
3814 * Don't let the user create a reference name with a leading '-'.
3815 * While technically a valid reference name, this case is usually
3816 * an unintended typo.
3818 if (refname[0] == '-')
3819 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3821 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3822 repo);
3823 if (err) {
3824 struct got_reference *target_ref;
3826 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3827 return err;
3828 err = got_ref_open(&target_ref, repo, target, 0);
3829 if (err)
3830 return err;
3831 err = got_ref_resolve(&id, repo, target_ref);
3832 got_ref_close(target_ref);
3833 if (err)
3834 return err;
3837 err = got_ref_alloc(&ref, refname, id);
3838 if (err)
3839 goto done;
3841 err = got_ref_write(ref, repo);
3842 done:
3843 if (ref)
3844 got_ref_close(ref);
3845 free(id);
3846 return err;
3849 static const struct got_error *
3850 add_symref(struct got_repository *repo, const char *refname, const char *target)
3852 const struct got_error *err = NULL;
3853 struct got_reference *ref = NULL;
3854 struct got_reference *target_ref = NULL;
3857 * Don't let the user create a reference name with a leading '-'.
3858 * While technically a valid reference name, this case is usually
3859 * an unintended typo.
3861 if (refname[0] == '-')
3862 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3864 err = got_ref_open(&target_ref, repo, target, 0);
3865 if (err)
3866 return err;
3868 err = got_ref_alloc_symref(&ref, refname, target_ref);
3869 if (err)
3870 goto done;
3872 err = got_ref_write(ref, repo);
3873 done:
3874 if (target_ref)
3875 got_ref_close(target_ref);
3876 if (ref)
3877 got_ref_close(ref);
3878 return err;
3881 static const struct got_error *
3882 cmd_ref(int argc, char *argv[])
3884 const struct got_error *error = NULL;
3885 struct got_repository *repo = NULL;
3886 struct got_worktree *worktree = NULL;
3887 char *cwd = NULL, *repo_path = NULL;
3888 int ch, do_list = 0, create_symref = 0;
3889 const char *delref = NULL;
3891 /* TODO: Add -s option for adding symbolic references. */
3892 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3893 switch (ch) {
3894 case 'd':
3895 delref = optarg;
3896 break;
3897 case 'r':
3898 repo_path = realpath(optarg, NULL);
3899 if (repo_path == NULL)
3900 return got_error_from_errno2("realpath",
3901 optarg);
3902 got_path_strip_trailing_slashes(repo_path);
3903 break;
3904 case 'l':
3905 do_list = 1;
3906 break;
3907 case 's':
3908 create_symref = 1;
3909 break;
3910 default:
3911 usage_ref();
3912 /* NOTREACHED */
3916 if (do_list && delref)
3917 errx(1, "-l and -d options are mutually exclusive\n");
3919 argc -= optind;
3920 argv += optind;
3922 if (do_list || delref) {
3923 if (create_symref)
3924 errx(1, "-s option cannot be used together with the "
3925 "-l or -d options");
3926 if (argc > 0)
3927 usage_ref();
3928 } else if (argc != 2)
3929 usage_ref();
3931 #ifndef PROFILE
3932 if (do_list) {
3933 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3934 NULL) == -1)
3935 err(1, "pledge");
3936 } else {
3937 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3938 "sendfd unveil", NULL) == -1)
3939 err(1, "pledge");
3941 #endif
3942 cwd = getcwd(NULL, 0);
3943 if (cwd == NULL) {
3944 error = got_error_from_errno("getcwd");
3945 goto done;
3948 if (repo_path == NULL) {
3949 error = got_worktree_open(&worktree, cwd);
3950 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3951 goto done;
3952 else
3953 error = NULL;
3954 if (worktree) {
3955 repo_path =
3956 strdup(got_worktree_get_repo_path(worktree));
3957 if (repo_path == NULL)
3958 error = got_error_from_errno("strdup");
3959 if (error)
3960 goto done;
3961 } else {
3962 repo_path = strdup(cwd);
3963 if (repo_path == NULL) {
3964 error = got_error_from_errno("strdup");
3965 goto done;
3970 error = got_repo_open(&repo, repo_path, NULL);
3971 if (error != NULL)
3972 goto done;
3974 error = apply_unveil(got_repo_get_path(repo), do_list,
3975 worktree ? got_worktree_get_root_path(worktree) : NULL);
3976 if (error)
3977 goto done;
3979 if (do_list)
3980 error = list_refs(repo);
3981 else if (delref)
3982 error = delete_ref(repo, delref);
3983 else if (create_symref)
3984 error = add_symref(repo, argv[0], argv[1]);
3985 else
3986 error = add_ref(repo, argv[0], argv[1]);
3987 done:
3988 if (repo)
3989 got_repo_close(repo);
3990 if (worktree)
3991 got_worktree_close(worktree);
3992 free(cwd);
3993 free(repo_path);
3994 return error;
3997 __dead static void
3998 usage_branch(void)
4000 fprintf(stderr,
4001 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4002 "[name]\n", getprogname());
4003 exit(1);
4006 static const struct got_error *
4007 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4008 struct got_reference *ref)
4010 const struct got_error *err = NULL;
4011 const char *refname, *marker = " ";
4012 char *refstr;
4014 refname = got_ref_get_name(ref);
4015 if (worktree && strcmp(refname,
4016 got_worktree_get_head_ref_name(worktree)) == 0) {
4017 struct got_object_id *id = NULL;
4019 err = got_ref_resolve(&id, repo, ref);
4020 if (err)
4021 return err;
4022 if (got_object_id_cmp(id,
4023 got_worktree_get_base_commit_id(worktree)) == 0)
4024 marker = "* ";
4025 else
4026 marker = "~ ";
4027 free(id);
4030 if (strncmp(refname, "refs/heads/", 11) == 0)
4031 refname += 11;
4032 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4033 refname += 18;
4035 refstr = got_ref_to_str(ref);
4036 if (refstr == NULL)
4037 return got_error_from_errno("got_ref_to_str");
4039 printf("%s%s: %s\n", marker, refname, refstr);
4040 free(refstr);
4041 return NULL;
4044 static const struct got_error *
4045 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4047 const char *refname;
4049 if (worktree == NULL)
4050 return got_error(GOT_ERR_NOT_WORKTREE);
4052 refname = got_worktree_get_head_ref_name(worktree);
4054 if (strncmp(refname, "refs/heads/", 11) == 0)
4055 refname += 11;
4056 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4057 refname += 18;
4059 printf("%s\n", refname);
4061 return NULL;
4064 static const struct got_error *
4065 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4067 static const struct got_error *err = NULL;
4068 struct got_reflist_head refs;
4069 struct got_reflist_entry *re;
4070 struct got_reference *temp_ref = NULL;
4071 int rebase_in_progress, histedit_in_progress;
4073 SIMPLEQ_INIT(&refs);
4075 if (worktree) {
4076 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4077 worktree);
4078 if (err)
4079 return err;
4081 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4082 worktree);
4083 if (err)
4084 return err;
4086 if (rebase_in_progress || histedit_in_progress) {
4087 err = got_ref_open(&temp_ref, repo,
4088 got_worktree_get_head_ref_name(worktree), 0);
4089 if (err)
4090 return err;
4091 list_branch(repo, worktree, temp_ref);
4092 got_ref_close(temp_ref);
4096 err = got_ref_list(&refs, repo, "refs/heads",
4097 got_ref_cmp_by_name, NULL);
4098 if (err)
4099 return err;
4101 SIMPLEQ_FOREACH(re, &refs, entry)
4102 list_branch(repo, worktree, re->ref);
4104 got_ref_list_free(&refs);
4105 return NULL;
4108 static const struct got_error *
4109 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4110 const char *branch_name)
4112 const struct got_error *err = NULL;
4113 struct got_reference *ref = NULL;
4114 char *refname;
4116 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4117 return got_error_from_errno("asprintf");
4119 err = got_ref_open(&ref, repo, refname, 0);
4120 if (err)
4121 goto done;
4123 if (worktree &&
4124 strcmp(got_worktree_get_head_ref_name(worktree),
4125 got_ref_get_name(ref)) == 0) {
4126 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4127 "will not delete this work tree's current branch");
4128 goto done;
4131 err = got_ref_delete(ref, repo);
4132 done:
4133 if (ref)
4134 got_ref_close(ref);
4135 free(refname);
4136 return err;
4139 static const struct got_error *
4140 add_branch(struct got_repository *repo, const char *branch_name,
4141 struct got_object_id *base_commit_id)
4143 const struct got_error *err = NULL;
4144 struct got_reference *ref = NULL;
4145 char *base_refname = NULL, *refname = NULL;
4148 * Don't let the user create a branch name with a leading '-'.
4149 * While technically a valid reference name, this case is usually
4150 * an unintended typo.
4152 if (branch_name[0] == '-')
4153 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4155 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4156 err = got_error_from_errno("asprintf");
4157 goto done;
4160 err = got_ref_open(&ref, repo, refname, 0);
4161 if (err == NULL) {
4162 err = got_error(GOT_ERR_BRANCH_EXISTS);
4163 goto done;
4164 } else if (err->code != GOT_ERR_NOT_REF)
4165 goto done;
4167 err = got_ref_alloc(&ref, refname, base_commit_id);
4168 if (err)
4169 goto done;
4171 err = got_ref_write(ref, repo);
4172 done:
4173 if (ref)
4174 got_ref_close(ref);
4175 free(base_refname);
4176 free(refname);
4177 return err;
4180 static const struct got_error *
4181 cmd_branch(int argc, char *argv[])
4183 const struct got_error *error = NULL;
4184 struct got_repository *repo = NULL;
4185 struct got_worktree *worktree = NULL;
4186 char *cwd = NULL, *repo_path = NULL;
4187 int ch, do_list = 0, do_show = 0, do_update = 1;
4188 const char *delref = NULL, *commit_id_arg = NULL;
4189 struct got_reference *ref = NULL;
4190 struct got_pathlist_head paths;
4191 struct got_pathlist_entry *pe;
4192 struct got_object_id *commit_id = NULL;
4193 char *commit_id_str = NULL;
4195 TAILQ_INIT(&paths);
4197 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4198 switch (ch) {
4199 case 'c':
4200 commit_id_arg = optarg;
4201 break;
4202 case 'd':
4203 delref = optarg;
4204 break;
4205 case 'r':
4206 repo_path = realpath(optarg, NULL);
4207 if (repo_path == NULL)
4208 return got_error_from_errno2("realpath",
4209 optarg);
4210 got_path_strip_trailing_slashes(repo_path);
4211 break;
4212 case 'l':
4213 do_list = 1;
4214 break;
4215 case 'n':
4216 do_update = 0;
4217 break;
4218 default:
4219 usage_branch();
4220 /* NOTREACHED */
4224 if (do_list && delref)
4225 errx(1, "-l and -d options are mutually exclusive\n");
4227 argc -= optind;
4228 argv += optind;
4230 if (!do_list && !delref && argc == 0)
4231 do_show = 1;
4233 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4234 errx(1, "-c option can only be used when creating a branch");
4236 if (do_list || delref) {
4237 if (argc > 0)
4238 usage_branch();
4239 } else if (!do_show && argc != 1)
4240 usage_branch();
4242 #ifndef PROFILE
4243 if (do_list || do_show) {
4244 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4245 NULL) == -1)
4246 err(1, "pledge");
4247 } else {
4248 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4249 "sendfd unveil", NULL) == -1)
4250 err(1, "pledge");
4252 #endif
4253 cwd = getcwd(NULL, 0);
4254 if (cwd == NULL) {
4255 error = got_error_from_errno("getcwd");
4256 goto done;
4259 if (repo_path == NULL) {
4260 error = got_worktree_open(&worktree, cwd);
4261 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4262 goto done;
4263 else
4264 error = NULL;
4265 if (worktree) {
4266 repo_path =
4267 strdup(got_worktree_get_repo_path(worktree));
4268 if (repo_path == NULL)
4269 error = got_error_from_errno("strdup");
4270 if (error)
4271 goto done;
4272 } else {
4273 repo_path = strdup(cwd);
4274 if (repo_path == NULL) {
4275 error = got_error_from_errno("strdup");
4276 goto done;
4281 error = got_repo_open(&repo, repo_path, NULL);
4282 if (error != NULL)
4283 goto done;
4285 error = apply_unveil(got_repo_get_path(repo), do_list,
4286 worktree ? got_worktree_get_root_path(worktree) : NULL);
4287 if (error)
4288 goto done;
4290 if (do_show)
4291 error = show_current_branch(repo, worktree);
4292 else if (do_list)
4293 error = list_branches(repo, worktree);
4294 else if (delref)
4295 error = delete_branch(repo, worktree, delref);
4296 else {
4297 if (commit_id_arg == NULL)
4298 commit_id_arg = worktree ?
4299 got_worktree_get_head_ref_name(worktree) :
4300 GOT_REF_HEAD;
4301 error = got_repo_match_object_id(&commit_id, NULL,
4302 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4303 if (error)
4304 goto done;
4305 error = add_branch(repo, argv[0], commit_id);
4306 if (error)
4307 goto done;
4308 if (worktree && do_update) {
4309 int did_something = 0;
4310 char *branch_refname = NULL;
4312 error = got_object_id_str(&commit_id_str, commit_id);
4313 if (error)
4314 goto done;
4315 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4316 worktree);
4317 if (error)
4318 goto done;
4319 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4320 == -1) {
4321 error = got_error_from_errno("asprintf");
4322 goto done;
4324 error = got_ref_open(&ref, repo, branch_refname, 0);
4325 free(branch_refname);
4326 if (error)
4327 goto done;
4328 error = switch_head_ref(ref, commit_id, worktree,
4329 repo);
4330 if (error)
4331 goto done;
4332 error = got_worktree_set_base_commit_id(worktree, repo,
4333 commit_id);
4334 if (error)
4335 goto done;
4336 error = got_worktree_checkout_files(worktree, &paths,
4337 repo, update_progress, &did_something,
4338 check_cancelled, NULL);
4339 if (error)
4340 goto done;
4341 if (did_something)
4342 printf("Updated to commit %s\n", commit_id_str);
4345 done:
4346 if (ref)
4347 got_ref_close(ref);
4348 if (repo)
4349 got_repo_close(repo);
4350 if (worktree)
4351 got_worktree_close(worktree);
4352 free(cwd);
4353 free(repo_path);
4354 free(commit_id);
4355 free(commit_id_str);
4356 TAILQ_FOREACH(pe, &paths, entry)
4357 free((char *)pe->path);
4358 got_pathlist_free(&paths);
4359 return error;
4363 __dead static void
4364 usage_tag(void)
4366 fprintf(stderr,
4367 "usage: %s tag [-c commit] [-r repository] [-l] "
4368 "[-m message] name\n", getprogname());
4369 exit(1);
4372 #if 0
4373 static const struct got_error *
4374 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4376 const struct got_error *err = NULL;
4377 struct got_reflist_entry *re, *se, *new;
4378 struct got_object_id *re_id, *se_id;
4379 struct got_tag_object *re_tag, *se_tag;
4380 time_t re_time, se_time;
4382 SIMPLEQ_FOREACH(re, tags, entry) {
4383 se = SIMPLEQ_FIRST(sorted);
4384 if (se == NULL) {
4385 err = got_reflist_entry_dup(&new, re);
4386 if (err)
4387 return err;
4388 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4389 continue;
4390 } else {
4391 err = got_ref_resolve(&re_id, repo, re->ref);
4392 if (err)
4393 break;
4394 err = got_object_open_as_tag(&re_tag, repo, re_id);
4395 free(re_id);
4396 if (err)
4397 break;
4398 re_time = got_object_tag_get_tagger_time(re_tag);
4399 got_object_tag_close(re_tag);
4402 while (se) {
4403 err = got_ref_resolve(&se_id, repo, re->ref);
4404 if (err)
4405 break;
4406 err = got_object_open_as_tag(&se_tag, repo, se_id);
4407 free(se_id);
4408 if (err)
4409 break;
4410 se_time = got_object_tag_get_tagger_time(se_tag);
4411 got_object_tag_close(se_tag);
4413 if (se_time > re_time) {
4414 err = got_reflist_entry_dup(&new, re);
4415 if (err)
4416 return err;
4417 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4418 break;
4420 se = SIMPLEQ_NEXT(se, entry);
4421 continue;
4424 done:
4425 return err;
4427 #endif
4429 static const struct got_error *
4430 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4432 static const struct got_error *err = NULL;
4433 struct got_reflist_head refs;
4434 struct got_reflist_entry *re;
4436 SIMPLEQ_INIT(&refs);
4438 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4439 if (err)
4440 return err;
4442 SIMPLEQ_FOREACH(re, &refs, entry) {
4443 const char *refname;
4444 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4445 char datebuf[26];
4446 const char *tagger;
4447 time_t tagger_time;
4448 struct got_object_id *id;
4449 struct got_tag_object *tag;
4450 struct got_commit_object *commit = NULL;
4452 refname = got_ref_get_name(re->ref);
4453 if (strncmp(refname, "refs/tags/", 10) != 0)
4454 continue;
4455 refname += 10;
4456 refstr = got_ref_to_str(re->ref);
4457 if (refstr == NULL) {
4458 err = got_error_from_errno("got_ref_to_str");
4459 break;
4461 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4462 free(refstr);
4464 err = got_ref_resolve(&id, repo, re->ref);
4465 if (err)
4466 break;
4467 err = got_object_open_as_tag(&tag, repo, id);
4468 if (err) {
4469 if (err->code != GOT_ERR_OBJ_TYPE) {
4470 free(id);
4471 break;
4473 /* "lightweight" tag */
4474 err = got_object_open_as_commit(&commit, repo, id);
4475 if (err) {
4476 free(id);
4477 break;
4479 tagger = got_object_commit_get_committer(commit);
4480 tagger_time =
4481 got_object_commit_get_committer_time(commit);
4482 err = got_object_id_str(&id_str, id);
4483 free(id);
4484 if (err)
4485 break;
4486 } else {
4487 free(id);
4488 tagger = got_object_tag_get_tagger(tag);
4489 tagger_time = got_object_tag_get_tagger_time(tag);
4490 err = got_object_id_str(&id_str,
4491 got_object_tag_get_object_id(tag));
4492 if (err)
4493 break;
4495 printf("from: %s\n", tagger);
4496 datestr = get_datestr(&tagger_time, datebuf);
4497 if (datestr)
4498 printf("date: %s UTC\n", datestr);
4499 if (commit)
4500 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4501 else {
4502 switch (got_object_tag_get_object_type(tag)) {
4503 case GOT_OBJ_TYPE_BLOB:
4504 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4505 id_str);
4506 break;
4507 case GOT_OBJ_TYPE_TREE:
4508 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4509 id_str);
4510 break;
4511 case GOT_OBJ_TYPE_COMMIT:
4512 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4513 id_str);
4514 break;
4515 case GOT_OBJ_TYPE_TAG:
4516 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4517 id_str);
4518 break;
4519 default:
4520 break;
4523 free(id_str);
4524 if (commit) {
4525 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4526 if (err)
4527 break;
4528 got_object_commit_close(commit);
4529 } else {
4530 tagmsg0 = strdup(got_object_tag_get_message(tag));
4531 got_object_tag_close(tag);
4532 if (tagmsg0 == NULL) {
4533 err = got_error_from_errno("strdup");
4534 break;
4538 tagmsg = tagmsg0;
4539 do {
4540 line = strsep(&tagmsg, "\n");
4541 if (line)
4542 printf(" %s\n", line);
4543 } while (line);
4544 free(tagmsg0);
4547 got_ref_list_free(&refs);
4548 return NULL;
4551 static const struct got_error *
4552 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4553 const char *tag_name, const char *repo_path)
4555 const struct got_error *err = NULL;
4556 char *template = NULL, *initial_content = NULL;
4557 char *editor = NULL;
4558 int fd = -1;
4560 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4561 err = got_error_from_errno("asprintf");
4562 goto done;
4565 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4566 commit_id_str, tag_name) == -1) {
4567 err = got_error_from_errno("asprintf");
4568 goto done;
4571 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4572 if (err)
4573 goto done;
4575 dprintf(fd, initial_content);
4576 close(fd);
4578 err = get_editor(&editor);
4579 if (err)
4580 goto done;
4581 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4582 done:
4583 free(initial_content);
4584 free(template);
4585 free(editor);
4587 /* Editor is done; we can now apply unveil(2) */
4588 if (err == NULL) {
4589 err = apply_unveil(repo_path, 0, NULL);
4590 if (err) {
4591 free(*tagmsg);
4592 *tagmsg = NULL;
4595 return err;
4598 static const struct got_error *
4599 add_tag(struct got_repository *repo, const char *tag_name,
4600 const char *commit_arg, const char *tagmsg_arg)
4602 const struct got_error *err = NULL;
4603 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4604 char *label = NULL, *commit_id_str = NULL;
4605 struct got_reference *ref = NULL;
4606 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4607 char *tagmsg_path = NULL, *tag_id_str = NULL;
4608 int preserve_tagmsg = 0;
4611 * Don't let the user create a tag name with a leading '-'.
4612 * While technically a valid reference name, this case is usually
4613 * an unintended typo.
4615 if (tag_name[0] == '-')
4616 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4618 err = get_author(&tagger, repo);
4619 if (err)
4620 return err;
4622 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4623 GOT_OBJ_TYPE_COMMIT, 1, repo);
4624 if (err)
4625 goto done;
4627 err = got_object_id_str(&commit_id_str, commit_id);
4628 if (err)
4629 goto done;
4631 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4632 refname = strdup(tag_name);
4633 if (refname == NULL) {
4634 err = got_error_from_errno("strdup");
4635 goto done;
4637 tag_name += 10;
4638 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4639 err = got_error_from_errno("asprintf");
4640 goto done;
4643 err = got_ref_open(&ref, repo, refname, 0);
4644 if (err == NULL) {
4645 err = got_error(GOT_ERR_TAG_EXISTS);
4646 goto done;
4647 } else if (err->code != GOT_ERR_NOT_REF)
4648 goto done;
4650 if (tagmsg_arg == NULL) {
4651 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4652 tag_name, got_repo_get_path(repo));
4653 if (err) {
4654 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4655 tagmsg_path != NULL)
4656 preserve_tagmsg = 1;
4657 goto done;
4661 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4662 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4663 if (err) {
4664 if (tagmsg_path)
4665 preserve_tagmsg = 1;
4666 goto done;
4669 err = got_ref_alloc(&ref, refname, tag_id);
4670 if (err) {
4671 if (tagmsg_path)
4672 preserve_tagmsg = 1;
4673 goto done;
4676 err = got_ref_write(ref, repo);
4677 if (err) {
4678 if (tagmsg_path)
4679 preserve_tagmsg = 1;
4680 goto done;
4683 err = got_object_id_str(&tag_id_str, tag_id);
4684 if (err) {
4685 if (tagmsg_path)
4686 preserve_tagmsg = 1;
4687 goto done;
4689 printf("Created tag %s\n", tag_id_str);
4690 done:
4691 if (preserve_tagmsg) {
4692 fprintf(stderr, "%s: tag message preserved in %s\n",
4693 getprogname(), tagmsg_path);
4694 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4695 err = got_error_from_errno2("unlink", tagmsg_path);
4696 free(tag_id_str);
4697 if (ref)
4698 got_ref_close(ref);
4699 free(commit_id);
4700 free(commit_id_str);
4701 free(refname);
4702 free(tagmsg);
4703 free(tagmsg_path);
4704 free(tagger);
4705 return err;
4708 static const struct got_error *
4709 cmd_tag(int argc, char *argv[])
4711 const struct got_error *error = NULL;
4712 struct got_repository *repo = NULL;
4713 struct got_worktree *worktree = NULL;
4714 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4715 char *gitconfig_path = NULL;
4716 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4717 int ch, do_list = 0;
4719 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4720 switch (ch) {
4721 case 'c':
4722 commit_id_arg = optarg;
4723 break;
4724 case 'm':
4725 tagmsg = optarg;
4726 break;
4727 case 'r':
4728 repo_path = realpath(optarg, NULL);
4729 if (repo_path == NULL)
4730 return got_error_from_errno2("realpath",
4731 optarg);
4732 got_path_strip_trailing_slashes(repo_path);
4733 break;
4734 case 'l':
4735 do_list = 1;
4736 break;
4737 default:
4738 usage_tag();
4739 /* NOTREACHED */
4743 argc -= optind;
4744 argv += optind;
4746 if (do_list) {
4747 if (commit_id_arg != NULL)
4748 errx(1, "-c option can only be used when creating a tag");
4749 if (tagmsg)
4750 errx(1, "-l and -m options are mutually exclusive");
4751 if (argc > 0)
4752 usage_tag();
4753 } else if (argc != 1)
4754 usage_tag();
4756 tag_name = argv[0];
4758 #ifndef PROFILE
4759 if (do_list) {
4760 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4761 NULL) == -1)
4762 err(1, "pledge");
4763 } else {
4764 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4765 "sendfd unveil", NULL) == -1)
4766 err(1, "pledge");
4768 #endif
4769 cwd = getcwd(NULL, 0);
4770 if (cwd == NULL) {
4771 error = got_error_from_errno("getcwd");
4772 goto done;
4775 if (repo_path == NULL) {
4776 error = got_worktree_open(&worktree, cwd);
4777 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4778 goto done;
4779 else
4780 error = NULL;
4781 if (worktree) {
4782 repo_path =
4783 strdup(got_worktree_get_repo_path(worktree));
4784 if (repo_path == NULL)
4785 error = got_error_from_errno("strdup");
4786 if (error)
4787 goto done;
4788 } else {
4789 repo_path = strdup(cwd);
4790 if (repo_path == NULL) {
4791 error = got_error_from_errno("strdup");
4792 goto done;
4797 if (do_list) {
4798 error = got_repo_open(&repo, repo_path, NULL);
4799 if (error != NULL)
4800 goto done;
4801 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4802 if (error)
4803 goto done;
4804 error = list_tags(repo, worktree);
4805 } else {
4806 error = get_gitconfig_path(&gitconfig_path);
4807 if (error)
4808 goto done;
4809 error = got_repo_open(&repo, repo_path, gitconfig_path);
4810 if (error != NULL)
4811 goto done;
4813 if (tagmsg) {
4814 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4815 if (error)
4816 goto done;
4819 if (commit_id_arg == NULL) {
4820 struct got_reference *head_ref;
4821 struct got_object_id *commit_id;
4822 error = got_ref_open(&head_ref, repo,
4823 worktree ? got_worktree_get_head_ref_name(worktree)
4824 : GOT_REF_HEAD, 0);
4825 if (error)
4826 goto done;
4827 error = got_ref_resolve(&commit_id, repo, head_ref);
4828 got_ref_close(head_ref);
4829 if (error)
4830 goto done;
4831 error = got_object_id_str(&commit_id_str, commit_id);
4832 free(commit_id);
4833 if (error)
4834 goto done;
4837 error = add_tag(repo, tag_name,
4838 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4840 done:
4841 if (repo)
4842 got_repo_close(repo);
4843 if (worktree)
4844 got_worktree_close(worktree);
4845 free(cwd);
4846 free(repo_path);
4847 free(gitconfig_path);
4848 free(commit_id_str);
4849 return error;
4852 __dead static void
4853 usage_add(void)
4855 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4856 getprogname());
4857 exit(1);
4860 static const struct got_error *
4861 add_progress(void *arg, unsigned char status, const char *path)
4863 while (path[0] == '/')
4864 path++;
4865 printf("%c %s\n", status, path);
4866 return NULL;
4869 static const struct got_error *
4870 cmd_add(int argc, char *argv[])
4872 const struct got_error *error = NULL;
4873 struct got_repository *repo = NULL;
4874 struct got_worktree *worktree = NULL;
4875 char *cwd = NULL;
4876 struct got_pathlist_head paths;
4877 struct got_pathlist_entry *pe;
4878 int ch, can_recurse = 0, no_ignores = 0;
4880 TAILQ_INIT(&paths);
4882 while ((ch = getopt(argc, argv, "IR")) != -1) {
4883 switch (ch) {
4884 case 'I':
4885 no_ignores = 1;
4886 break;
4887 case 'R':
4888 can_recurse = 1;
4889 break;
4890 default:
4891 usage_add();
4892 /* NOTREACHED */
4896 argc -= optind;
4897 argv += optind;
4899 #ifndef PROFILE
4900 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4901 NULL) == -1)
4902 err(1, "pledge");
4903 #endif
4904 if (argc < 1)
4905 usage_add();
4907 cwd = getcwd(NULL, 0);
4908 if (cwd == NULL) {
4909 error = got_error_from_errno("getcwd");
4910 goto done;
4913 error = got_worktree_open(&worktree, cwd);
4914 if (error)
4915 goto done;
4917 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4918 NULL);
4919 if (error != NULL)
4920 goto done;
4922 error = apply_unveil(got_repo_get_path(repo), 1,
4923 got_worktree_get_root_path(worktree));
4924 if (error)
4925 goto done;
4927 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4928 if (error)
4929 goto done;
4931 if (!can_recurse && no_ignores) {
4932 error = got_error_msg(GOT_ERR_BAD_PATH,
4933 "disregarding ignores requires -R option");
4934 goto done;
4938 if (!can_recurse) {
4939 char *ondisk_path;
4940 struct stat sb;
4941 TAILQ_FOREACH(pe, &paths, entry) {
4942 if (asprintf(&ondisk_path, "%s/%s",
4943 got_worktree_get_root_path(worktree),
4944 pe->path) == -1) {
4945 error = got_error_from_errno("asprintf");
4946 goto done;
4948 if (lstat(ondisk_path, &sb) == -1) {
4949 if (errno == ENOENT) {
4950 free(ondisk_path);
4951 continue;
4953 error = got_error_from_errno2("lstat",
4954 ondisk_path);
4955 free(ondisk_path);
4956 goto done;
4958 free(ondisk_path);
4959 if (S_ISDIR(sb.st_mode)) {
4960 error = got_error_msg(GOT_ERR_BAD_PATH,
4961 "adding directories requires -R option");
4962 goto done;
4967 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4968 NULL, repo, no_ignores);
4969 done:
4970 if (repo)
4971 got_repo_close(repo);
4972 if (worktree)
4973 got_worktree_close(worktree);
4974 TAILQ_FOREACH(pe, &paths, entry)
4975 free((char *)pe->path);
4976 got_pathlist_free(&paths);
4977 free(cwd);
4978 return error;
4981 __dead static void
4982 usage_remove(void)
4984 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4985 getprogname());
4986 exit(1);
4989 static const struct got_error *
4990 print_remove_status(void *arg, unsigned char status,
4991 unsigned char staged_status, const char *path)
4993 while (path[0] == '/')
4994 path++;
4995 if (status == GOT_STATUS_NONEXISTENT)
4996 return NULL;
4997 if (status == staged_status && (status == GOT_STATUS_DELETE))
4998 status = GOT_STATUS_NO_CHANGE;
4999 printf("%c%c %s\n", status, staged_status, path);
5000 return NULL;
5003 static const struct got_error *
5004 cmd_remove(int argc, char *argv[])
5006 const struct got_error *error = NULL;
5007 struct got_worktree *worktree = NULL;
5008 struct got_repository *repo = NULL;
5009 char *cwd = NULL;
5010 struct got_pathlist_head paths;
5011 struct got_pathlist_entry *pe;
5012 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5014 TAILQ_INIT(&paths);
5016 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5017 switch (ch) {
5018 case 'f':
5019 delete_local_mods = 1;
5020 break;
5021 case 'k':
5022 keep_on_disk = 1;
5023 break;
5024 case 'R':
5025 can_recurse = 1;
5026 break;
5027 default:
5028 usage_remove();
5029 /* NOTREACHED */
5033 argc -= optind;
5034 argv += optind;
5036 #ifndef PROFILE
5037 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5038 NULL) == -1)
5039 err(1, "pledge");
5040 #endif
5041 if (argc < 1)
5042 usage_remove();
5044 cwd = getcwd(NULL, 0);
5045 if (cwd == NULL) {
5046 error = got_error_from_errno("getcwd");
5047 goto done;
5049 error = got_worktree_open(&worktree, cwd);
5050 if (error)
5051 goto done;
5053 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5054 NULL);
5055 if (error)
5056 goto done;
5058 error = apply_unveil(got_repo_get_path(repo), 1,
5059 got_worktree_get_root_path(worktree));
5060 if (error)
5061 goto done;
5063 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5064 if (error)
5065 goto done;
5067 if (!can_recurse) {
5068 char *ondisk_path;
5069 struct stat sb;
5070 TAILQ_FOREACH(pe, &paths, entry) {
5071 if (asprintf(&ondisk_path, "%s/%s",
5072 got_worktree_get_root_path(worktree),
5073 pe->path) == -1) {
5074 error = got_error_from_errno("asprintf");
5075 goto done;
5077 if (lstat(ondisk_path, &sb) == -1) {
5078 if (errno == ENOENT) {
5079 free(ondisk_path);
5080 continue;
5082 error = got_error_from_errno2("lstat",
5083 ondisk_path);
5084 free(ondisk_path);
5085 goto done;
5087 free(ondisk_path);
5088 if (S_ISDIR(sb.st_mode)) {
5089 error = got_error_msg(GOT_ERR_BAD_PATH,
5090 "removing directories requires -R option");
5091 goto done;
5096 error = got_worktree_schedule_delete(worktree, &paths,
5097 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5098 done:
5099 if (repo)
5100 got_repo_close(repo);
5101 if (worktree)
5102 got_worktree_close(worktree);
5103 TAILQ_FOREACH(pe, &paths, entry)
5104 free((char *)pe->path);
5105 got_pathlist_free(&paths);
5106 free(cwd);
5107 return error;
5110 __dead static void
5111 usage_revert(void)
5113 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5114 "path ...\n", getprogname());
5115 exit(1);
5118 static const struct got_error *
5119 revert_progress(void *arg, unsigned char status, const char *path)
5121 if (status == GOT_STATUS_UNVERSIONED)
5122 return NULL;
5124 while (path[0] == '/')
5125 path++;
5126 printf("%c %s\n", status, path);
5127 return NULL;
5130 struct choose_patch_arg {
5131 FILE *patch_script_file;
5132 const char *action;
5135 static const struct got_error *
5136 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5137 int nchanges, const char *action)
5139 char *line = NULL;
5140 size_t linesize = 0;
5141 ssize_t linelen;
5143 switch (status) {
5144 case GOT_STATUS_ADD:
5145 printf("A %s\n%s this addition? [y/n] ", path, action);
5146 break;
5147 case GOT_STATUS_DELETE:
5148 printf("D %s\n%s this deletion? [y/n] ", path, action);
5149 break;
5150 case GOT_STATUS_MODIFY:
5151 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5152 return got_error_from_errno("fseek");
5153 printf(GOT_COMMIT_SEP_STR);
5154 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5155 printf("%s", line);
5156 if (ferror(patch_file))
5157 return got_error_from_errno("getline");
5158 printf(GOT_COMMIT_SEP_STR);
5159 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5160 path, n, nchanges, action);
5161 break;
5162 default:
5163 return got_error_path(path, GOT_ERR_FILE_STATUS);
5166 return NULL;
5169 static const struct got_error *
5170 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5171 FILE *patch_file, int n, int nchanges)
5173 const struct got_error *err = NULL;
5174 char *line = NULL;
5175 size_t linesize = 0;
5176 ssize_t linelen;
5177 int resp = ' ';
5178 struct choose_patch_arg *a = arg;
5180 *choice = GOT_PATCH_CHOICE_NONE;
5182 if (a->patch_script_file) {
5183 char *nl;
5184 err = show_change(status, path, patch_file, n, nchanges,
5185 a->action);
5186 if (err)
5187 return err;
5188 linelen = getline(&line, &linesize, a->patch_script_file);
5189 if (linelen == -1) {
5190 if (ferror(a->patch_script_file))
5191 return got_error_from_errno("getline");
5192 return NULL;
5194 nl = strchr(line, '\n');
5195 if (nl)
5196 *nl = '\0';
5197 if (strcmp(line, "y") == 0) {
5198 *choice = GOT_PATCH_CHOICE_YES;
5199 printf("y\n");
5200 } else if (strcmp(line, "n") == 0) {
5201 *choice = GOT_PATCH_CHOICE_NO;
5202 printf("n\n");
5203 } else if (strcmp(line, "q") == 0 &&
5204 status == GOT_STATUS_MODIFY) {
5205 *choice = GOT_PATCH_CHOICE_QUIT;
5206 printf("q\n");
5207 } else
5208 printf("invalid response '%s'\n", line);
5209 free(line);
5210 return NULL;
5213 while (resp != 'y' && resp != 'n' && resp != 'q') {
5214 err = show_change(status, path, patch_file, n, nchanges,
5215 a->action);
5216 if (err)
5217 return err;
5218 resp = getchar();
5219 if (resp == '\n')
5220 resp = getchar();
5221 if (status == GOT_STATUS_MODIFY) {
5222 if (resp != 'y' && resp != 'n' && resp != 'q') {
5223 printf("invalid response '%c'\n", resp);
5224 resp = ' ';
5226 } else if (resp != 'y' && resp != 'n') {
5227 printf("invalid response '%c'\n", resp);
5228 resp = ' ';
5232 if (resp == 'y')
5233 *choice = GOT_PATCH_CHOICE_YES;
5234 else if (resp == 'n')
5235 *choice = GOT_PATCH_CHOICE_NO;
5236 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5237 *choice = GOT_PATCH_CHOICE_QUIT;
5239 return NULL;
5243 static const struct got_error *
5244 cmd_revert(int argc, char *argv[])
5246 const struct got_error *error = NULL;
5247 struct got_worktree *worktree = NULL;
5248 struct got_repository *repo = NULL;
5249 char *cwd = NULL, *path = NULL;
5250 struct got_pathlist_head paths;
5251 struct got_pathlist_entry *pe;
5252 int ch, can_recurse = 0, pflag = 0;
5253 FILE *patch_script_file = NULL;
5254 const char *patch_script_path = NULL;
5255 struct choose_patch_arg cpa;
5257 TAILQ_INIT(&paths);
5259 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5260 switch (ch) {
5261 case 'p':
5262 pflag = 1;
5263 break;
5264 case 'F':
5265 patch_script_path = optarg;
5266 break;
5267 case 'R':
5268 can_recurse = 1;
5269 break;
5270 default:
5271 usage_revert();
5272 /* NOTREACHED */
5276 argc -= optind;
5277 argv += optind;
5279 #ifndef PROFILE
5280 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5281 "unveil", NULL) == -1)
5282 err(1, "pledge");
5283 #endif
5284 if (argc < 1)
5285 usage_revert();
5286 if (patch_script_path && !pflag)
5287 errx(1, "-F option can only be used together with -p option");
5289 cwd = getcwd(NULL, 0);
5290 if (cwd == NULL) {
5291 error = got_error_from_errno("getcwd");
5292 goto done;
5294 error = got_worktree_open(&worktree, cwd);
5295 if (error)
5296 goto done;
5298 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5299 NULL);
5300 if (error != NULL)
5301 goto done;
5303 if (patch_script_path) {
5304 patch_script_file = fopen(patch_script_path, "r");
5305 if (patch_script_file == NULL) {
5306 error = got_error_from_errno2("fopen",
5307 patch_script_path);
5308 goto done;
5311 error = apply_unveil(got_repo_get_path(repo), 1,
5312 got_worktree_get_root_path(worktree));
5313 if (error)
5314 goto done;
5316 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5317 if (error)
5318 goto done;
5320 if (!can_recurse) {
5321 char *ondisk_path;
5322 struct stat sb;
5323 TAILQ_FOREACH(pe, &paths, entry) {
5324 if (asprintf(&ondisk_path, "%s/%s",
5325 got_worktree_get_root_path(worktree),
5326 pe->path) == -1) {
5327 error = got_error_from_errno("asprintf");
5328 goto done;
5330 if (lstat(ondisk_path, &sb) == -1) {
5331 if (errno == ENOENT) {
5332 free(ondisk_path);
5333 continue;
5335 error = got_error_from_errno2("lstat",
5336 ondisk_path);
5337 free(ondisk_path);
5338 goto done;
5340 free(ondisk_path);
5341 if (S_ISDIR(sb.st_mode)) {
5342 error = got_error_msg(GOT_ERR_BAD_PATH,
5343 "reverting directories requires -R option");
5344 goto done;
5349 cpa.patch_script_file = patch_script_file;
5350 cpa.action = "revert";
5351 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5352 pflag ? choose_patch : NULL, &cpa, repo);
5353 done:
5354 if (patch_script_file && fclose(patch_script_file) == EOF &&
5355 error == NULL)
5356 error = got_error_from_errno2("fclose", patch_script_path);
5357 if (repo)
5358 got_repo_close(repo);
5359 if (worktree)
5360 got_worktree_close(worktree);
5361 free(path);
5362 free(cwd);
5363 return error;
5366 __dead static void
5367 usage_commit(void)
5369 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5370 getprogname());
5371 exit(1);
5374 struct collect_commit_logmsg_arg {
5375 const char *cmdline_log;
5376 const char *editor;
5377 const char *worktree_path;
5378 const char *branch_name;
5379 const char *repo_path;
5380 char *logmsg_path;
5384 static const struct got_error *
5385 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5386 void *arg)
5388 char *initial_content = NULL;
5389 struct got_pathlist_entry *pe;
5390 const struct got_error *err = NULL;
5391 char *template = NULL;
5392 struct collect_commit_logmsg_arg *a = arg;
5393 int fd;
5394 size_t len;
5396 /* if a message was specified on the command line, just use it */
5397 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5398 len = strlen(a->cmdline_log) + 1;
5399 *logmsg = malloc(len + 1);
5400 if (*logmsg == NULL)
5401 return got_error_from_errno("malloc");
5402 strlcpy(*logmsg, a->cmdline_log, len);
5403 return NULL;
5406 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5407 return got_error_from_errno("asprintf");
5409 if (asprintf(&initial_content,
5410 "\n# changes to be committed on branch %s:\n",
5411 a->branch_name) == -1)
5412 return got_error_from_errno("asprintf");
5414 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5415 if (err)
5416 goto done;
5418 dprintf(fd, initial_content);
5420 TAILQ_FOREACH(pe, commitable_paths, entry) {
5421 struct got_commitable *ct = pe->data;
5422 dprintf(fd, "# %c %s\n",
5423 got_commitable_get_status(ct),
5424 got_commitable_get_path(ct));
5426 close(fd);
5428 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5429 done:
5430 free(initial_content);
5431 free(template);
5433 /* Editor is done; we can now apply unveil(2) */
5434 if (err == NULL) {
5435 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5436 if (err) {
5437 free(*logmsg);
5438 *logmsg = NULL;
5441 return err;
5444 static const struct got_error *
5445 cmd_commit(int argc, char *argv[])
5447 const struct got_error *error = NULL;
5448 struct got_worktree *worktree = NULL;
5449 struct got_repository *repo = NULL;
5450 char *cwd = NULL, *id_str = NULL;
5451 struct got_object_id *id = NULL;
5452 const char *logmsg = NULL;
5453 struct collect_commit_logmsg_arg cl_arg;
5454 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5455 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5456 struct got_pathlist_head paths;
5458 TAILQ_INIT(&paths);
5459 cl_arg.logmsg_path = NULL;
5461 while ((ch = getopt(argc, argv, "m:")) != -1) {
5462 switch (ch) {
5463 case 'm':
5464 logmsg = optarg;
5465 break;
5466 default:
5467 usage_commit();
5468 /* NOTREACHED */
5472 argc -= optind;
5473 argv += optind;
5475 #ifndef PROFILE
5476 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5477 "unveil", NULL) == -1)
5478 err(1, "pledge");
5479 #endif
5480 cwd = getcwd(NULL, 0);
5481 if (cwd == NULL) {
5482 error = got_error_from_errno("getcwd");
5483 goto done;
5485 error = got_worktree_open(&worktree, cwd);
5486 if (error)
5487 goto done;
5489 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5490 if (error)
5491 goto done;
5492 if (rebase_in_progress) {
5493 error = got_error(GOT_ERR_REBASING);
5494 goto done;
5497 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5498 worktree);
5499 if (error)
5500 goto done;
5502 error = get_gitconfig_path(&gitconfig_path);
5503 if (error)
5504 goto done;
5505 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5506 gitconfig_path);
5507 if (error != NULL)
5508 goto done;
5510 error = get_author(&author, repo);
5511 if (error)
5512 return error;
5515 * unveil(2) traverses exec(2); if an editor is used we have
5516 * to apply unveil after the log message has been written.
5518 if (logmsg == NULL || strlen(logmsg) == 0)
5519 error = get_editor(&editor);
5520 else
5521 error = apply_unveil(got_repo_get_path(repo), 0,
5522 got_worktree_get_root_path(worktree));
5523 if (error)
5524 goto done;
5526 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5527 if (error)
5528 goto done;
5530 cl_arg.editor = editor;
5531 cl_arg.cmdline_log = logmsg;
5532 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5533 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5534 if (!histedit_in_progress) {
5535 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5536 error = got_error(GOT_ERR_COMMIT_BRANCH);
5537 goto done;
5539 cl_arg.branch_name += 11;
5541 cl_arg.repo_path = got_repo_get_path(repo);
5542 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5543 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5544 if (error) {
5545 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5546 cl_arg.logmsg_path != NULL)
5547 preserve_logmsg = 1;
5548 goto done;
5551 error = got_object_id_str(&id_str, id);
5552 if (error)
5553 goto done;
5554 printf("Created commit %s\n", id_str);
5555 done:
5556 if (preserve_logmsg) {
5557 fprintf(stderr, "%s: log message preserved in %s\n",
5558 getprogname(), cl_arg.logmsg_path);
5559 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5560 error == NULL)
5561 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5562 free(cl_arg.logmsg_path);
5563 if (repo)
5564 got_repo_close(repo);
5565 if (worktree)
5566 got_worktree_close(worktree);
5567 free(cwd);
5568 free(id_str);
5569 free(gitconfig_path);
5570 free(editor);
5571 free(author);
5572 return error;
5575 __dead static void
5576 usage_cherrypick(void)
5578 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5579 exit(1);
5582 static const struct got_error *
5583 cmd_cherrypick(int argc, char *argv[])
5585 const struct got_error *error = NULL;
5586 struct got_worktree *worktree = NULL;
5587 struct got_repository *repo = NULL;
5588 char *cwd = NULL, *commit_id_str = NULL;
5589 struct got_object_id *commit_id = NULL;
5590 struct got_commit_object *commit = NULL;
5591 struct got_object_qid *pid;
5592 struct got_reference *head_ref = NULL;
5593 int ch, did_something = 0;
5595 while ((ch = getopt(argc, argv, "")) != -1) {
5596 switch (ch) {
5597 default:
5598 usage_cherrypick();
5599 /* NOTREACHED */
5603 argc -= optind;
5604 argv += optind;
5606 #ifndef PROFILE
5607 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5608 "unveil", NULL) == -1)
5609 err(1, "pledge");
5610 #endif
5611 if (argc != 1)
5612 usage_cherrypick();
5614 cwd = getcwd(NULL, 0);
5615 if (cwd == NULL) {
5616 error = got_error_from_errno("getcwd");
5617 goto done;
5619 error = got_worktree_open(&worktree, cwd);
5620 if (error)
5621 goto done;
5623 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5624 NULL);
5625 if (error != NULL)
5626 goto done;
5628 error = apply_unveil(got_repo_get_path(repo), 0,
5629 got_worktree_get_root_path(worktree));
5630 if (error)
5631 goto done;
5633 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5634 GOT_OBJ_TYPE_COMMIT, repo);
5635 if (error != NULL) {
5636 struct got_reference *ref;
5637 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5638 goto done;
5639 error = got_ref_open(&ref, repo, argv[0], 0);
5640 if (error != NULL)
5641 goto done;
5642 error = got_ref_resolve(&commit_id, repo, ref);
5643 got_ref_close(ref);
5644 if (error != NULL)
5645 goto done;
5647 error = got_object_id_str(&commit_id_str, commit_id);
5648 if (error)
5649 goto done;
5651 error = got_ref_open(&head_ref, repo,
5652 got_worktree_get_head_ref_name(worktree), 0);
5653 if (error != NULL)
5654 goto done;
5656 error = check_same_branch(commit_id, head_ref, NULL, repo);
5657 if (error) {
5658 if (error->code != GOT_ERR_ANCESTRY)
5659 goto done;
5660 error = NULL;
5661 } else {
5662 error = got_error(GOT_ERR_SAME_BRANCH);
5663 goto done;
5666 error = got_object_open_as_commit(&commit, repo, commit_id);
5667 if (error)
5668 goto done;
5669 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5670 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5671 commit_id, repo, update_progress, &did_something, check_cancelled,
5672 NULL);
5673 if (error != NULL)
5674 goto done;
5676 if (did_something)
5677 printf("Merged commit %s\n", commit_id_str);
5678 done:
5679 if (commit)
5680 got_object_commit_close(commit);
5681 free(commit_id_str);
5682 if (head_ref)
5683 got_ref_close(head_ref);
5684 if (worktree)
5685 got_worktree_close(worktree);
5686 if (repo)
5687 got_repo_close(repo);
5688 return error;
5691 __dead static void
5692 usage_backout(void)
5694 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5695 exit(1);
5698 static const struct got_error *
5699 cmd_backout(int argc, char *argv[])
5701 const struct got_error *error = NULL;
5702 struct got_worktree *worktree = NULL;
5703 struct got_repository *repo = NULL;
5704 char *cwd = NULL, *commit_id_str = NULL;
5705 struct got_object_id *commit_id = NULL;
5706 struct got_commit_object *commit = NULL;
5707 struct got_object_qid *pid;
5708 struct got_reference *head_ref = NULL;
5709 int ch, did_something = 0;
5711 while ((ch = getopt(argc, argv, "")) != -1) {
5712 switch (ch) {
5713 default:
5714 usage_backout();
5715 /* NOTREACHED */
5719 argc -= optind;
5720 argv += optind;
5722 #ifndef PROFILE
5723 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5724 "unveil", NULL) == -1)
5725 err(1, "pledge");
5726 #endif
5727 if (argc != 1)
5728 usage_backout();
5730 cwd = getcwd(NULL, 0);
5731 if (cwd == NULL) {
5732 error = got_error_from_errno("getcwd");
5733 goto done;
5735 error = got_worktree_open(&worktree, cwd);
5736 if (error)
5737 goto done;
5739 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5740 NULL);
5741 if (error != NULL)
5742 goto done;
5744 error = apply_unveil(got_repo_get_path(repo), 0,
5745 got_worktree_get_root_path(worktree));
5746 if (error)
5747 goto done;
5749 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5750 GOT_OBJ_TYPE_COMMIT, repo);
5751 if (error != NULL) {
5752 struct got_reference *ref;
5753 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5754 goto done;
5755 error = got_ref_open(&ref, repo, argv[0], 0);
5756 if (error != NULL)
5757 goto done;
5758 error = got_ref_resolve(&commit_id, repo, ref);
5759 got_ref_close(ref);
5760 if (error != NULL)
5761 goto done;
5763 error = got_object_id_str(&commit_id_str, commit_id);
5764 if (error)
5765 goto done;
5767 error = got_ref_open(&head_ref, repo,
5768 got_worktree_get_head_ref_name(worktree), 0);
5769 if (error != NULL)
5770 goto done;
5772 error = check_same_branch(commit_id, head_ref, NULL, repo);
5773 if (error)
5774 goto done;
5776 error = got_object_open_as_commit(&commit, repo, commit_id);
5777 if (error)
5778 goto done;
5779 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5780 if (pid == NULL) {
5781 error = got_error(GOT_ERR_ROOT_COMMIT);
5782 goto done;
5785 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5786 update_progress, &did_something, check_cancelled, NULL);
5787 if (error != NULL)
5788 goto done;
5790 if (did_something)
5791 printf("Backed out commit %s\n", commit_id_str);
5792 done:
5793 if (commit)
5794 got_object_commit_close(commit);
5795 free(commit_id_str);
5796 if (head_ref)
5797 got_ref_close(head_ref);
5798 if (worktree)
5799 got_worktree_close(worktree);
5800 if (repo)
5801 got_repo_close(repo);
5802 return error;
5805 __dead static void
5806 usage_rebase(void)
5808 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5809 getprogname());
5810 exit(1);
5813 void
5814 trim_logmsg(char *logmsg, int limit)
5816 char *nl;
5817 size_t len;
5819 len = strlen(logmsg);
5820 if (len > limit)
5821 len = limit;
5822 logmsg[len] = '\0';
5823 nl = strchr(logmsg, '\n');
5824 if (nl)
5825 *nl = '\0';
5828 static const struct got_error *
5829 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5831 const struct got_error *err;
5832 char *logmsg0 = NULL;
5833 const char *s;
5835 err = got_object_commit_get_logmsg(&logmsg0, commit);
5836 if (err)
5837 return err;
5839 s = logmsg0;
5840 while (isspace((unsigned char)s[0]))
5841 s++;
5843 *logmsg = strdup(s);
5844 if (*logmsg == NULL) {
5845 err = got_error_from_errno("strdup");
5846 goto done;
5849 trim_logmsg(*logmsg, limit);
5850 done:
5851 free(logmsg0);
5852 return err;
5855 static const struct got_error *
5856 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5858 const struct got_error *err;
5859 struct got_commit_object *commit = NULL;
5860 char *id_str = NULL, *logmsg = NULL;
5862 err = got_object_open_as_commit(&commit, repo, id);
5863 if (err)
5864 return err;
5866 err = got_object_id_str(&id_str, id);
5867 if (err)
5868 goto done;
5870 id_str[12] = '\0';
5872 err = get_short_logmsg(&logmsg, 42, commit);
5873 if (err)
5874 goto done;
5876 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5877 done:
5878 free(id_str);
5879 got_object_commit_close(commit);
5880 free(logmsg);
5881 return err;
5884 static const struct got_error *
5885 show_rebase_progress(struct got_commit_object *commit,
5886 struct got_object_id *old_id, struct got_object_id *new_id)
5888 const struct got_error *err;
5889 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5891 err = got_object_id_str(&old_id_str, old_id);
5892 if (err)
5893 goto done;
5895 if (new_id) {
5896 err = got_object_id_str(&new_id_str, new_id);
5897 if (err)
5898 goto done;
5901 old_id_str[12] = '\0';
5902 if (new_id_str)
5903 new_id_str[12] = '\0';
5905 err = get_short_logmsg(&logmsg, 42, commit);
5906 if (err)
5907 goto done;
5909 printf("%s -> %s: %s\n", old_id_str,
5910 new_id_str ? new_id_str : "no-op change", logmsg);
5911 done:
5912 free(old_id_str);
5913 free(new_id_str);
5914 free(logmsg);
5915 return err;
5918 static const struct got_error *
5919 rebase_progress(void *arg, unsigned char status, const char *path)
5921 unsigned char *rebase_status = arg;
5923 while (path[0] == '/')
5924 path++;
5925 printf("%c %s\n", status, path);
5927 if (*rebase_status == GOT_STATUS_CONFLICT)
5928 return NULL;
5929 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5930 *rebase_status = status;
5931 return NULL;
5934 static const struct got_error *
5935 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5936 struct got_reference *branch, struct got_reference *new_base_branch,
5937 struct got_reference *tmp_branch, struct got_repository *repo)
5939 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5940 return got_worktree_rebase_complete(worktree, fileindex,
5941 new_base_branch, tmp_branch, branch, repo);
5944 static const struct got_error *
5945 rebase_commit(struct got_pathlist_head *merged_paths,
5946 struct got_worktree *worktree, struct got_fileindex *fileindex,
5947 struct got_reference *tmp_branch,
5948 struct got_object_id *commit_id, struct got_repository *repo)
5950 const struct got_error *error;
5951 struct got_commit_object *commit;
5952 struct got_object_id *new_commit_id;
5954 error = got_object_open_as_commit(&commit, repo, commit_id);
5955 if (error)
5956 return error;
5958 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5959 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5960 if (error) {
5961 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5962 goto done;
5963 error = show_rebase_progress(commit, commit_id, NULL);
5964 } else {
5965 error = show_rebase_progress(commit, commit_id, new_commit_id);
5966 free(new_commit_id);
5968 done:
5969 got_object_commit_close(commit);
5970 return error;
5973 struct check_path_prefix_arg {
5974 const char *path_prefix;
5975 size_t len;
5976 int errcode;
5979 static const struct got_error *
5980 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5981 struct got_blob_object *blob2, struct got_object_id *id1,
5982 struct got_object_id *id2, const char *path1, const char *path2,
5983 mode_t mode1, mode_t mode2, struct got_repository *repo)
5985 struct check_path_prefix_arg *a = arg;
5987 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5988 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5989 return got_error(a->errcode);
5991 return NULL;
5994 static const struct got_error *
5995 check_path_prefix(struct got_object_id *parent_id,
5996 struct got_object_id *commit_id, const char *path_prefix,
5997 int errcode, struct got_repository *repo)
5999 const struct got_error *err;
6000 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6001 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6002 struct check_path_prefix_arg cpp_arg;
6004 if (got_path_is_root_dir(path_prefix))
6005 return NULL;
6007 err = got_object_open_as_commit(&commit, repo, commit_id);
6008 if (err)
6009 goto done;
6011 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6012 if (err)
6013 goto done;
6015 err = got_object_open_as_tree(&tree1, repo,
6016 got_object_commit_get_tree_id(parent_commit));
6017 if (err)
6018 goto done;
6020 err = got_object_open_as_tree(&tree2, repo,
6021 got_object_commit_get_tree_id(commit));
6022 if (err)
6023 goto done;
6025 cpp_arg.path_prefix = path_prefix;
6026 while (cpp_arg.path_prefix[0] == '/')
6027 cpp_arg.path_prefix++;
6028 cpp_arg.len = strlen(cpp_arg.path_prefix);
6029 cpp_arg.errcode = errcode;
6030 err = got_diff_tree(tree1, tree2, "", "", repo,
6031 check_path_prefix_in_diff, &cpp_arg, 0);
6032 done:
6033 if (tree1)
6034 got_object_tree_close(tree1);
6035 if (tree2)
6036 got_object_tree_close(tree2);
6037 if (commit)
6038 got_object_commit_close(commit);
6039 if (parent_commit)
6040 got_object_commit_close(parent_commit);
6041 return err;
6044 static const struct got_error *
6045 collect_commits(struct got_object_id_queue *commits,
6046 struct got_object_id *initial_commit_id,
6047 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6048 const char *path_prefix, int path_prefix_errcode,
6049 struct got_repository *repo)
6051 const struct got_error *err = NULL;
6052 struct got_commit_graph *graph = NULL;
6053 struct got_object_id *parent_id = NULL;
6054 struct got_object_qid *qid;
6055 struct got_object_id *commit_id = initial_commit_id;
6057 err = got_commit_graph_open(&graph, "/", 1);
6058 if (err)
6059 return err;
6061 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6062 check_cancelled, NULL);
6063 if (err)
6064 goto done;
6065 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6066 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6067 check_cancelled, NULL);
6068 if (err) {
6069 if (err->code == GOT_ERR_ITER_COMPLETED) {
6070 err = got_error_msg(GOT_ERR_ANCESTRY,
6071 "ran out of commits to rebase before "
6072 "youngest common ancestor commit has "
6073 "been reached?!?");
6075 goto done;
6076 } else {
6077 err = check_path_prefix(parent_id, commit_id,
6078 path_prefix, path_prefix_errcode, repo);
6079 if (err)
6080 goto done;
6082 err = got_object_qid_alloc(&qid, commit_id);
6083 if (err)
6084 goto done;
6085 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6086 commit_id = parent_id;
6089 done:
6090 got_commit_graph_close(graph);
6091 return err;
6094 static const struct got_error *
6095 cmd_rebase(int argc, char *argv[])
6097 const struct got_error *error = NULL;
6098 struct got_worktree *worktree = NULL;
6099 struct got_repository *repo = NULL;
6100 struct got_fileindex *fileindex = NULL;
6101 char *cwd = NULL;
6102 struct got_reference *branch = NULL;
6103 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6104 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6105 struct got_object_id *resume_commit_id = NULL;
6106 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6107 struct got_commit_object *commit = NULL;
6108 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6109 int histedit_in_progress = 0;
6110 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6111 struct got_object_id_queue commits;
6112 struct got_pathlist_head merged_paths;
6113 const struct got_object_id_queue *parent_ids;
6114 struct got_object_qid *qid, *pid;
6116 SIMPLEQ_INIT(&commits);
6117 TAILQ_INIT(&merged_paths);
6119 while ((ch = getopt(argc, argv, "ac")) != -1) {
6120 switch (ch) {
6121 case 'a':
6122 abort_rebase = 1;
6123 break;
6124 case 'c':
6125 continue_rebase = 1;
6126 break;
6127 default:
6128 usage_rebase();
6129 /* NOTREACHED */
6133 argc -= optind;
6134 argv += optind;
6136 #ifndef PROFILE
6137 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6138 "unveil", NULL) == -1)
6139 err(1, "pledge");
6140 #endif
6141 if (abort_rebase && continue_rebase)
6142 usage_rebase();
6143 else if (abort_rebase || continue_rebase) {
6144 if (argc != 0)
6145 usage_rebase();
6146 } else if (argc != 1)
6147 usage_rebase();
6149 cwd = getcwd(NULL, 0);
6150 if (cwd == NULL) {
6151 error = got_error_from_errno("getcwd");
6152 goto done;
6154 error = got_worktree_open(&worktree, cwd);
6155 if (error)
6156 goto done;
6158 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6159 NULL);
6160 if (error != NULL)
6161 goto done;
6163 error = apply_unveil(got_repo_get_path(repo), 0,
6164 got_worktree_get_root_path(worktree));
6165 if (error)
6166 goto done;
6168 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6169 worktree);
6170 if (error)
6171 goto done;
6172 if (histedit_in_progress) {
6173 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6174 goto done;
6177 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6178 if (error)
6179 goto done;
6181 if (abort_rebase) {
6182 int did_something;
6183 if (!rebase_in_progress) {
6184 error = got_error(GOT_ERR_NOT_REBASING);
6185 goto done;
6187 error = got_worktree_rebase_continue(&resume_commit_id,
6188 &new_base_branch, &tmp_branch, &branch, &fileindex,
6189 worktree, repo);
6190 if (error)
6191 goto done;
6192 printf("Switching work tree to %s\n",
6193 got_ref_get_symref_target(new_base_branch));
6194 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6195 new_base_branch, update_progress, &did_something);
6196 if (error)
6197 goto done;
6198 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6199 goto done; /* nothing else to do */
6202 if (continue_rebase) {
6203 if (!rebase_in_progress) {
6204 error = got_error(GOT_ERR_NOT_REBASING);
6205 goto done;
6207 error = got_worktree_rebase_continue(&resume_commit_id,
6208 &new_base_branch, &tmp_branch, &branch, &fileindex,
6209 worktree, repo);
6210 if (error)
6211 goto done;
6213 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6214 resume_commit_id, repo);
6215 if (error)
6216 goto done;
6218 yca_id = got_object_id_dup(resume_commit_id);
6219 if (yca_id == NULL) {
6220 error = got_error_from_errno("got_object_id_dup");
6221 goto done;
6223 } else {
6224 error = got_ref_open(&branch, repo, argv[0], 0);
6225 if (error != NULL)
6226 goto done;
6229 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6230 if (error)
6231 goto done;
6233 if (!continue_rebase) {
6234 struct got_object_id *base_commit_id;
6236 base_commit_id = got_worktree_get_base_commit_id(worktree);
6237 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6238 base_commit_id, branch_head_commit_id, repo,
6239 check_cancelled, NULL);
6240 if (error)
6241 goto done;
6242 if (yca_id == NULL) {
6243 error = got_error_msg(GOT_ERR_ANCESTRY,
6244 "specified branch shares no common ancestry "
6245 "with work tree's branch");
6246 goto done;
6249 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6250 if (error) {
6251 if (error->code != GOT_ERR_ANCESTRY)
6252 goto done;
6253 error = NULL;
6254 } else {
6255 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6256 "specified branch resolves to a commit which "
6257 "is already contained in work tree's branch");
6258 goto done;
6260 error = got_worktree_rebase_prepare(&new_base_branch,
6261 &tmp_branch, &fileindex, worktree, branch, repo);
6262 if (error)
6263 goto done;
6266 commit_id = branch_head_commit_id;
6267 error = got_object_open_as_commit(&commit, repo, commit_id);
6268 if (error)
6269 goto done;
6271 parent_ids = got_object_commit_get_parent_ids(commit);
6272 pid = SIMPLEQ_FIRST(parent_ids);
6273 if (pid == NULL) {
6274 if (!continue_rebase) {
6275 int did_something;
6276 error = got_worktree_rebase_abort(worktree, fileindex,
6277 repo, new_base_branch, update_progress,
6278 &did_something);
6279 if (error)
6280 goto done;
6281 printf("Rebase of %s aborted\n",
6282 got_ref_get_name(branch));
6284 error = got_error(GOT_ERR_EMPTY_REBASE);
6285 goto done;
6287 error = collect_commits(&commits, commit_id, pid->id,
6288 yca_id, got_worktree_get_path_prefix(worktree),
6289 GOT_ERR_REBASE_PATH, repo);
6290 got_object_commit_close(commit);
6291 commit = NULL;
6292 if (error)
6293 goto done;
6295 if (SIMPLEQ_EMPTY(&commits)) {
6296 if (continue_rebase) {
6297 error = rebase_complete(worktree, fileindex,
6298 branch, new_base_branch, tmp_branch, repo);
6299 goto done;
6300 } else {
6301 /* Fast-forward the reference of the branch. */
6302 struct got_object_id *new_head_commit_id;
6303 char *id_str;
6304 error = got_ref_resolve(&new_head_commit_id, repo,
6305 new_base_branch);
6306 if (error)
6307 goto done;
6308 error = got_object_id_str(&id_str, new_head_commit_id);
6309 printf("Forwarding %s to commit %s\n",
6310 got_ref_get_name(branch), id_str);
6311 free(id_str);
6312 error = got_ref_change_ref(branch,
6313 new_head_commit_id);
6314 if (error)
6315 goto done;
6319 pid = NULL;
6320 SIMPLEQ_FOREACH(qid, &commits, entry) {
6321 commit_id = qid->id;
6322 parent_id = pid ? pid->id : yca_id;
6323 pid = qid;
6325 error = got_worktree_rebase_merge_files(&merged_paths,
6326 worktree, fileindex, parent_id, commit_id, repo,
6327 rebase_progress, &rebase_status, check_cancelled, NULL);
6328 if (error)
6329 goto done;
6331 if (rebase_status == GOT_STATUS_CONFLICT) {
6332 error = show_rebase_merge_conflict(qid->id, repo);
6333 if (error)
6334 goto done;
6335 got_worktree_rebase_pathlist_free(&merged_paths);
6336 break;
6339 error = rebase_commit(&merged_paths, worktree, fileindex,
6340 tmp_branch, commit_id, repo);
6341 got_worktree_rebase_pathlist_free(&merged_paths);
6342 if (error)
6343 goto done;
6346 if (rebase_status == GOT_STATUS_CONFLICT) {
6347 error = got_worktree_rebase_postpone(worktree, fileindex);
6348 if (error)
6349 goto done;
6350 error = got_error_msg(GOT_ERR_CONFLICTS,
6351 "conflicts must be resolved before rebasing can continue");
6352 } else
6353 error = rebase_complete(worktree, fileindex, branch,
6354 new_base_branch, tmp_branch, repo);
6355 done:
6356 got_object_id_queue_free(&commits);
6357 free(branch_head_commit_id);
6358 free(resume_commit_id);
6359 free(yca_id);
6360 if (commit)
6361 got_object_commit_close(commit);
6362 if (branch)
6363 got_ref_close(branch);
6364 if (new_base_branch)
6365 got_ref_close(new_base_branch);
6366 if (tmp_branch)
6367 got_ref_close(tmp_branch);
6368 if (worktree)
6369 got_worktree_close(worktree);
6370 if (repo)
6371 got_repo_close(repo);
6372 return error;
6375 __dead static void
6376 usage_histedit(void)
6378 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6379 getprogname());
6380 exit(1);
6383 #define GOT_HISTEDIT_PICK 'p'
6384 #define GOT_HISTEDIT_EDIT 'e'
6385 #define GOT_HISTEDIT_FOLD 'f'
6386 #define GOT_HISTEDIT_DROP 'd'
6387 #define GOT_HISTEDIT_MESG 'm'
6389 static struct got_histedit_cmd {
6390 unsigned char code;
6391 const char *name;
6392 const char *desc;
6393 } got_histedit_cmds[] = {
6394 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6395 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6396 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6397 "be used" },
6398 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6399 { GOT_HISTEDIT_MESG, "mesg",
6400 "single-line log message for commit above (open editor if empty)" },
6403 struct got_histedit_list_entry {
6404 TAILQ_ENTRY(got_histedit_list_entry) entry;
6405 struct got_object_id *commit_id;
6406 const struct got_histedit_cmd *cmd;
6407 char *logmsg;
6409 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6411 static const struct got_error *
6412 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6413 FILE *f, struct got_repository *repo)
6415 const struct got_error *err = NULL;
6416 char *logmsg = NULL, *id_str = NULL;
6417 struct got_commit_object *commit = NULL;
6418 int n;
6420 err = got_object_open_as_commit(&commit, repo, commit_id);
6421 if (err)
6422 goto done;
6424 err = get_short_logmsg(&logmsg, 34, commit);
6425 if (err)
6426 goto done;
6428 err = got_object_id_str(&id_str, commit_id);
6429 if (err)
6430 goto done;
6432 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6433 if (n < 0)
6434 err = got_ferror(f, GOT_ERR_IO);
6435 done:
6436 if (commit)
6437 got_object_commit_close(commit);
6438 free(id_str);
6439 free(logmsg);
6440 return err;
6443 static const struct got_error *
6444 histedit_write_commit_list(struct got_object_id_queue *commits,
6445 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6447 const struct got_error *err = NULL;
6448 struct got_object_qid *qid;
6450 if (SIMPLEQ_EMPTY(commits))
6451 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6453 SIMPLEQ_FOREACH(qid, commits, entry) {
6454 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6455 f, repo);
6456 if (err)
6457 break;
6458 if (edit_logmsg_only) {
6459 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6460 if (n < 0) {
6461 err = got_ferror(f, GOT_ERR_IO);
6462 break;
6467 return err;
6470 static const struct got_error *
6471 write_cmd_list(FILE *f, const char *branch_name,
6472 struct got_object_id_queue *commits)
6474 const struct got_error *err = NULL;
6475 int n, i;
6476 char *id_str;
6477 struct got_object_qid *qid;
6479 qid = SIMPLEQ_FIRST(commits);
6480 err = got_object_id_str(&id_str, qid->id);
6481 if (err)
6482 return err;
6484 n = fprintf(f,
6485 "# Editing the history of branch '%s' starting at\n"
6486 "# commit %s\n"
6487 "# Commits will be processed in order from top to "
6488 "bottom of this file.\n", branch_name, id_str);
6489 if (n < 0) {
6490 err = got_ferror(f, GOT_ERR_IO);
6491 goto done;
6494 n = fprintf(f, "# Available histedit commands:\n");
6495 if (n < 0) {
6496 err = got_ferror(f, GOT_ERR_IO);
6497 goto done;
6500 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6501 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6502 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6503 cmd->desc);
6504 if (n < 0) {
6505 err = got_ferror(f, GOT_ERR_IO);
6506 break;
6509 done:
6510 free(id_str);
6511 return err;
6514 static const struct got_error *
6515 histedit_syntax_error(int lineno)
6517 static char msg[42];
6518 int ret;
6520 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6521 lineno);
6522 if (ret == -1 || ret >= sizeof(msg))
6523 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6525 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6528 static const struct got_error *
6529 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6530 char *logmsg, struct got_repository *repo)
6532 const struct got_error *err;
6533 struct got_commit_object *folded_commit = NULL;
6534 char *id_str, *folded_logmsg = NULL;
6536 err = got_object_id_str(&id_str, hle->commit_id);
6537 if (err)
6538 return err;
6540 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6541 if (err)
6542 goto done;
6544 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6545 if (err)
6546 goto done;
6547 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6548 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6549 folded_logmsg) == -1) {
6550 err = got_error_from_errno("asprintf");
6552 done:
6553 if (folded_commit)
6554 got_object_commit_close(folded_commit);
6555 free(id_str);
6556 free(folded_logmsg);
6557 return err;
6560 static struct got_histedit_list_entry *
6561 get_folded_commits(struct got_histedit_list_entry *hle)
6563 struct got_histedit_list_entry *prev, *folded = NULL;
6565 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6566 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6567 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6568 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6569 folded = prev;
6570 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6573 return folded;
6576 static const struct got_error *
6577 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6578 struct got_repository *repo)
6580 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6581 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6582 const struct got_error *err = NULL;
6583 struct got_commit_object *commit = NULL;
6584 int fd;
6585 struct got_histedit_list_entry *folded = NULL;
6587 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6588 if (err)
6589 return err;
6591 folded = get_folded_commits(hle);
6592 if (folded) {
6593 while (folded != hle) {
6594 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6595 folded = TAILQ_NEXT(folded, entry);
6596 continue;
6598 err = append_folded_commit_msg(&new_msg, folded,
6599 logmsg, repo);
6600 if (err)
6601 goto done;
6602 free(logmsg);
6603 logmsg = new_msg;
6604 folded = TAILQ_NEXT(folded, entry);
6608 err = got_object_id_str(&id_str, hle->commit_id);
6609 if (err)
6610 goto done;
6611 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6612 if (err)
6613 goto done;
6614 if (asprintf(&new_msg,
6615 "%s\n# original log message of commit %s: %s",
6616 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6617 err = got_error_from_errno("asprintf");
6618 goto done;
6620 free(logmsg);
6621 logmsg = new_msg;
6623 err = got_object_id_str(&id_str, hle->commit_id);
6624 if (err)
6625 goto done;
6627 err = got_opentemp_named_fd(&logmsg_path, &fd,
6628 GOT_TMPDIR_STR "/got-logmsg");
6629 if (err)
6630 goto done;
6632 dprintf(fd, logmsg);
6633 close(fd);
6635 err = get_editor(&editor);
6636 if (err)
6637 goto done;
6639 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6640 if (err) {
6641 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6642 goto done;
6643 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6645 done:
6646 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6647 err = got_error_from_errno2("unlink", logmsg_path);
6648 free(logmsg_path);
6649 free(logmsg);
6650 free(orig_logmsg);
6651 free(editor);
6652 if (commit)
6653 got_object_commit_close(commit);
6654 return err;
6657 static const struct got_error *
6658 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6659 FILE *f, struct got_repository *repo)
6661 const struct got_error *err = NULL;
6662 char *line = NULL, *p, *end;
6663 size_t size;
6664 ssize_t len;
6665 int lineno = 0, i;
6666 const struct got_histedit_cmd *cmd;
6667 struct got_object_id *commit_id = NULL;
6668 struct got_histedit_list_entry *hle = NULL;
6670 for (;;) {
6671 len = getline(&line, &size, f);
6672 if (len == -1) {
6673 const struct got_error *getline_err;
6674 if (feof(f))
6675 break;
6676 getline_err = got_error_from_errno("getline");
6677 err = got_ferror(f, getline_err->code);
6678 break;
6680 lineno++;
6681 p = line;
6682 while (isspace((unsigned char)p[0]))
6683 p++;
6684 if (p[0] == '#' || p[0] == '\0') {
6685 free(line);
6686 line = NULL;
6687 continue;
6689 cmd = NULL;
6690 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6691 cmd = &got_histedit_cmds[i];
6692 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6693 isspace((unsigned char)p[strlen(cmd->name)])) {
6694 p += strlen(cmd->name);
6695 break;
6697 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6698 p++;
6699 break;
6702 if (i == nitems(got_histedit_cmds)) {
6703 err = histedit_syntax_error(lineno);
6704 break;
6706 while (isspace((unsigned char)p[0]))
6707 p++;
6708 if (cmd->code == GOT_HISTEDIT_MESG) {
6709 if (hle == NULL || hle->logmsg != NULL) {
6710 err = got_error(GOT_ERR_HISTEDIT_CMD);
6711 break;
6713 if (p[0] == '\0') {
6714 err = histedit_edit_logmsg(hle, repo);
6715 if (err)
6716 break;
6717 } else {
6718 hle->logmsg = strdup(p);
6719 if (hle->logmsg == NULL) {
6720 err = got_error_from_errno("strdup");
6721 break;
6724 free(line);
6725 line = NULL;
6726 continue;
6727 } else {
6728 end = p;
6729 while (end[0] && !isspace((unsigned char)end[0]))
6730 end++;
6731 *end = '\0';
6733 err = got_object_resolve_id_str(&commit_id, repo, p);
6734 if (err) {
6735 /* override error code */
6736 err = histedit_syntax_error(lineno);
6737 break;
6740 hle = malloc(sizeof(*hle));
6741 if (hle == NULL) {
6742 err = got_error_from_errno("malloc");
6743 break;
6745 hle->cmd = cmd;
6746 hle->commit_id = commit_id;
6747 hle->logmsg = NULL;
6748 commit_id = NULL;
6749 free(line);
6750 line = NULL;
6751 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6754 free(line);
6755 free(commit_id);
6756 return err;
6759 static const struct got_error *
6760 histedit_check_script(struct got_histedit_list *histedit_cmds,
6761 struct got_object_id_queue *commits, struct got_repository *repo)
6763 const struct got_error *err = NULL;
6764 struct got_object_qid *qid;
6765 struct got_histedit_list_entry *hle;
6766 static char msg[92];
6767 char *id_str;
6769 if (TAILQ_EMPTY(histedit_cmds))
6770 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6771 "histedit script contains no commands");
6772 if (SIMPLEQ_EMPTY(commits))
6773 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6775 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6776 struct got_histedit_list_entry *hle2;
6777 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6778 if (hle == hle2)
6779 continue;
6780 if (got_object_id_cmp(hle->commit_id,
6781 hle2->commit_id) != 0)
6782 continue;
6783 err = got_object_id_str(&id_str, hle->commit_id);
6784 if (err)
6785 return err;
6786 snprintf(msg, sizeof(msg), "commit %s is listed "
6787 "more than once in histedit script", id_str);
6788 free(id_str);
6789 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6793 SIMPLEQ_FOREACH(qid, commits, entry) {
6794 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6795 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6796 break;
6798 if (hle == NULL) {
6799 err = got_object_id_str(&id_str, qid->id);
6800 if (err)
6801 return err;
6802 snprintf(msg, sizeof(msg),
6803 "commit %s missing from histedit script", id_str);
6804 free(id_str);
6805 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6809 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6810 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6811 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6812 "last commit in histedit script cannot be folded");
6814 return NULL;
6817 static const struct got_error *
6818 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6819 const char *path, struct got_object_id_queue *commits,
6820 struct got_repository *repo)
6822 const struct got_error *err = NULL;
6823 char *editor;
6824 FILE *f = NULL;
6826 err = get_editor(&editor);
6827 if (err)
6828 return err;
6830 if (spawn_editor(editor, path) == -1) {
6831 err = got_error_from_errno("failed spawning editor");
6832 goto done;
6835 f = fopen(path, "r");
6836 if (f == NULL) {
6837 err = got_error_from_errno("fopen");
6838 goto done;
6840 err = histedit_parse_list(histedit_cmds, f, repo);
6841 if (err)
6842 goto done;
6844 err = histedit_check_script(histedit_cmds, commits, repo);
6845 done:
6846 if (f && fclose(f) != 0 && err == NULL)
6847 err = got_error_from_errno("fclose");
6848 free(editor);
6849 return err;
6852 static const struct got_error *
6853 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6854 struct got_object_id_queue *, const char *, const char *,
6855 struct got_repository *);
6857 static const struct got_error *
6858 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6859 struct got_object_id_queue *commits, const char *branch_name,
6860 int edit_logmsg_only, struct got_repository *repo)
6862 const struct got_error *err;
6863 FILE *f = NULL;
6864 char *path = NULL;
6866 err = got_opentemp_named(&path, &f, "got-histedit");
6867 if (err)
6868 return err;
6870 err = write_cmd_list(f, branch_name, commits);
6871 if (err)
6872 goto done;
6874 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6875 if (err)
6876 goto done;
6878 if (edit_logmsg_only) {
6879 rewind(f);
6880 err = histedit_parse_list(histedit_cmds, f, repo);
6881 } else {
6882 if (fclose(f) != 0) {
6883 err = got_error_from_errno("fclose");
6884 goto done;
6886 f = NULL;
6887 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6888 if (err) {
6889 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6890 err->code != GOT_ERR_HISTEDIT_CMD)
6891 goto done;
6892 err = histedit_edit_list_retry(histedit_cmds, err,
6893 commits, path, branch_name, repo);
6896 done:
6897 if (f && fclose(f) != 0 && err == NULL)
6898 err = got_error_from_errno("fclose");
6899 if (path && unlink(path) != 0 && err == NULL)
6900 err = got_error_from_errno2("unlink", path);
6901 free(path);
6902 return err;
6905 static const struct got_error *
6906 histedit_save_list(struct got_histedit_list *histedit_cmds,
6907 struct got_worktree *worktree, struct got_repository *repo)
6909 const struct got_error *err = NULL;
6910 char *path = NULL;
6911 FILE *f = NULL;
6912 struct got_histedit_list_entry *hle;
6913 struct got_commit_object *commit = NULL;
6915 err = got_worktree_get_histedit_script_path(&path, worktree);
6916 if (err)
6917 return err;
6919 f = fopen(path, "w");
6920 if (f == NULL) {
6921 err = got_error_from_errno2("fopen", path);
6922 goto done;
6924 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6925 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6926 repo);
6927 if (err)
6928 break;
6930 if (hle->logmsg) {
6931 int n = fprintf(f, "%c %s\n",
6932 GOT_HISTEDIT_MESG, hle->logmsg);
6933 if (n < 0) {
6934 err = got_ferror(f, GOT_ERR_IO);
6935 break;
6939 done:
6940 if (f && fclose(f) != 0 && err == NULL)
6941 err = got_error_from_errno("fclose");
6942 free(path);
6943 if (commit)
6944 got_object_commit_close(commit);
6945 return err;
6948 void
6949 histedit_free_list(struct got_histedit_list *histedit_cmds)
6951 struct got_histedit_list_entry *hle;
6953 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6954 TAILQ_REMOVE(histedit_cmds, hle, entry);
6955 free(hle);
6959 static const struct got_error *
6960 histedit_load_list(struct got_histedit_list *histedit_cmds,
6961 const char *path, struct got_repository *repo)
6963 const struct got_error *err = NULL;
6964 FILE *f = NULL;
6966 f = fopen(path, "r");
6967 if (f == NULL) {
6968 err = got_error_from_errno2("fopen", path);
6969 goto done;
6972 err = histedit_parse_list(histedit_cmds, f, repo);
6973 done:
6974 if (f && fclose(f) != 0 && err == NULL)
6975 err = got_error_from_errno("fclose");
6976 return err;
6979 static const struct got_error *
6980 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6981 const struct got_error *edit_err, struct got_object_id_queue *commits,
6982 const char *path, const char *branch_name, struct got_repository *repo)
6984 const struct got_error *err = NULL, *prev_err = edit_err;
6985 int resp = ' ';
6987 while (resp != 'c' && resp != 'r' && resp != 'a') {
6988 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6989 "or (a)bort: ", getprogname(), prev_err->msg);
6990 resp = getchar();
6991 if (resp == '\n')
6992 resp = getchar();
6993 if (resp == 'c') {
6994 histedit_free_list(histedit_cmds);
6995 err = histedit_run_editor(histedit_cmds, path, commits,
6996 repo);
6997 if (err) {
6998 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6999 err->code != GOT_ERR_HISTEDIT_CMD)
7000 break;
7001 prev_err = err;
7002 resp = ' ';
7003 continue;
7005 break;
7006 } else if (resp == 'r') {
7007 histedit_free_list(histedit_cmds);
7008 err = histedit_edit_script(histedit_cmds,
7009 commits, branch_name, 0, repo);
7010 if (err) {
7011 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7012 err->code != GOT_ERR_HISTEDIT_CMD)
7013 break;
7014 prev_err = err;
7015 resp = ' ';
7016 continue;
7018 break;
7019 } else if (resp == 'a') {
7020 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7021 break;
7022 } else
7023 printf("invalid response '%c'\n", resp);
7026 return err;
7029 static const struct got_error *
7030 histedit_complete(struct got_worktree *worktree,
7031 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7032 struct got_reference *branch, struct got_repository *repo)
7034 printf("Switching work tree to %s\n",
7035 got_ref_get_symref_target(branch));
7036 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7037 branch, repo);
7040 static const struct got_error *
7041 show_histedit_progress(struct got_commit_object *commit,
7042 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7044 const struct got_error *err;
7045 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7047 err = got_object_id_str(&old_id_str, hle->commit_id);
7048 if (err)
7049 goto done;
7051 if (new_id) {
7052 err = got_object_id_str(&new_id_str, new_id);
7053 if (err)
7054 goto done;
7057 old_id_str[12] = '\0';
7058 if (new_id_str)
7059 new_id_str[12] = '\0';
7061 if (hle->logmsg) {
7062 logmsg = strdup(hle->logmsg);
7063 if (logmsg == NULL) {
7064 err = got_error_from_errno("strdup");
7065 goto done;
7067 trim_logmsg(logmsg, 42);
7068 } else {
7069 err = get_short_logmsg(&logmsg, 42, commit);
7070 if (err)
7071 goto done;
7074 switch (hle->cmd->code) {
7075 case GOT_HISTEDIT_PICK:
7076 case GOT_HISTEDIT_EDIT:
7077 printf("%s -> %s: %s\n", old_id_str,
7078 new_id_str ? new_id_str : "no-op change", logmsg);
7079 break;
7080 case GOT_HISTEDIT_DROP:
7081 case GOT_HISTEDIT_FOLD:
7082 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7083 logmsg);
7084 break;
7085 default:
7086 break;
7088 done:
7089 free(old_id_str);
7090 free(new_id_str);
7091 return err;
7094 static const struct got_error *
7095 histedit_commit(struct got_pathlist_head *merged_paths,
7096 struct got_worktree *worktree, struct got_fileindex *fileindex,
7097 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7098 struct got_repository *repo)
7100 const struct got_error *err;
7101 struct got_commit_object *commit;
7102 struct got_object_id *new_commit_id;
7104 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7105 && hle->logmsg == NULL) {
7106 err = histedit_edit_logmsg(hle, repo);
7107 if (err)
7108 return err;
7111 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7112 if (err)
7113 return err;
7115 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7116 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7117 hle->logmsg, repo);
7118 if (err) {
7119 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7120 goto done;
7121 err = show_histedit_progress(commit, hle, NULL);
7122 } else {
7123 err = show_histedit_progress(commit, hle, new_commit_id);
7124 free(new_commit_id);
7126 done:
7127 got_object_commit_close(commit);
7128 return err;
7131 static const struct got_error *
7132 histedit_skip_commit(struct got_histedit_list_entry *hle,
7133 struct got_worktree *worktree, struct got_repository *repo)
7135 const struct got_error *error;
7136 struct got_commit_object *commit;
7138 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7139 repo);
7140 if (error)
7141 return error;
7143 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7144 if (error)
7145 return error;
7147 error = show_histedit_progress(commit, hle, NULL);
7148 got_object_commit_close(commit);
7149 return error;
7152 static const struct got_error *
7153 check_local_changes(void *arg, unsigned char status,
7154 unsigned char staged_status, const char *path,
7155 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7156 struct got_object_id *commit_id, int dirfd, const char *de_name)
7158 int *have_local_changes = arg;
7160 switch (status) {
7161 case GOT_STATUS_ADD:
7162 case GOT_STATUS_DELETE:
7163 case GOT_STATUS_MODIFY:
7164 case GOT_STATUS_CONFLICT:
7165 *have_local_changes = 1;
7166 return got_error(GOT_ERR_CANCELLED);
7167 default:
7168 break;
7171 switch (staged_status) {
7172 case GOT_STATUS_ADD:
7173 case GOT_STATUS_DELETE:
7174 case GOT_STATUS_MODIFY:
7175 *have_local_changes = 1;
7176 return got_error(GOT_ERR_CANCELLED);
7177 default:
7178 break;
7181 return NULL;
7184 static const struct got_error *
7185 cmd_histedit(int argc, char *argv[])
7187 const struct got_error *error = NULL;
7188 struct got_worktree *worktree = NULL;
7189 struct got_fileindex *fileindex = NULL;
7190 struct got_repository *repo = NULL;
7191 char *cwd = NULL;
7192 struct got_reference *branch = NULL;
7193 struct got_reference *tmp_branch = NULL;
7194 struct got_object_id *resume_commit_id = NULL;
7195 struct got_object_id *base_commit_id = NULL;
7196 struct got_object_id *head_commit_id = NULL;
7197 struct got_commit_object *commit = NULL;
7198 int ch, rebase_in_progress = 0, did_something;
7199 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7200 int edit_logmsg_only = 0;
7201 const char *edit_script_path = NULL;
7202 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7203 struct got_object_id_queue commits;
7204 struct got_pathlist_head merged_paths;
7205 const struct got_object_id_queue *parent_ids;
7206 struct got_object_qid *pid;
7207 struct got_histedit_list histedit_cmds;
7208 struct got_histedit_list_entry *hle;
7210 SIMPLEQ_INIT(&commits);
7211 TAILQ_INIT(&histedit_cmds);
7212 TAILQ_INIT(&merged_paths);
7214 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7215 switch (ch) {
7216 case 'a':
7217 abort_edit = 1;
7218 break;
7219 case 'c':
7220 continue_edit = 1;
7221 break;
7222 case 'F':
7223 edit_script_path = optarg;
7224 break;
7225 case 'm':
7226 edit_logmsg_only = 1;
7227 break;
7228 default:
7229 usage_histedit();
7230 /* NOTREACHED */
7234 argc -= optind;
7235 argv += optind;
7237 #ifndef PROFILE
7238 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7239 "unveil", NULL) == -1)
7240 err(1, "pledge");
7241 #endif
7242 if (abort_edit && continue_edit)
7243 errx(1, "histedit's -a and -c options are mutually exclusive");
7244 if (edit_script_path && edit_logmsg_only)
7245 errx(1, "histedit's -F and -m options are mutually exclusive");
7246 if (abort_edit && edit_logmsg_only)
7247 errx(1, "histedit's -a and -m options are mutually exclusive");
7248 if (continue_edit && edit_logmsg_only)
7249 errx(1, "histedit's -c and -m options are mutually exclusive");
7250 if (argc != 0)
7251 usage_histedit();
7254 * This command cannot apply unveil(2) in all cases because the
7255 * user may choose to run an editor to edit the histedit script
7256 * and to edit individual commit log messages.
7257 * unveil(2) traverses exec(2); if an editor is used we have to
7258 * apply unveil after edit script and log messages have been written.
7259 * XXX TODO: Make use of unveil(2) where possible.
7262 cwd = getcwd(NULL, 0);
7263 if (cwd == NULL) {
7264 error = got_error_from_errno("getcwd");
7265 goto done;
7267 error = got_worktree_open(&worktree, cwd);
7268 if (error)
7269 goto done;
7271 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7272 NULL);
7273 if (error != NULL)
7274 goto done;
7276 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7277 if (error)
7278 goto done;
7279 if (rebase_in_progress) {
7280 error = got_error(GOT_ERR_REBASING);
7281 goto done;
7284 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7285 if (error)
7286 goto done;
7288 if (edit_in_progress && edit_logmsg_only) {
7289 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7290 "histedit operation is in progress in this "
7291 "work tree and must be continued or aborted "
7292 "before the -m option can be used");
7293 goto done;
7296 if (edit_in_progress && abort_edit) {
7297 error = got_worktree_histedit_continue(&resume_commit_id,
7298 &tmp_branch, &branch, &base_commit_id, &fileindex,
7299 worktree, repo);
7300 if (error)
7301 goto done;
7302 printf("Switching work tree to %s\n",
7303 got_ref_get_symref_target(branch));
7304 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7305 branch, base_commit_id, update_progress, &did_something);
7306 if (error)
7307 goto done;
7308 printf("Histedit of %s aborted\n",
7309 got_ref_get_symref_target(branch));
7310 goto done; /* nothing else to do */
7311 } else if (abort_edit) {
7312 error = got_error(GOT_ERR_NOT_HISTEDIT);
7313 goto done;
7316 if (continue_edit) {
7317 char *path;
7319 if (!edit_in_progress) {
7320 error = got_error(GOT_ERR_NOT_HISTEDIT);
7321 goto done;
7324 error = got_worktree_get_histedit_script_path(&path, worktree);
7325 if (error)
7326 goto done;
7328 error = histedit_load_list(&histedit_cmds, path, repo);
7329 free(path);
7330 if (error)
7331 goto done;
7333 error = got_worktree_histedit_continue(&resume_commit_id,
7334 &tmp_branch, &branch, &base_commit_id, &fileindex,
7335 worktree, repo);
7336 if (error)
7337 goto done;
7339 error = got_ref_resolve(&head_commit_id, repo, branch);
7340 if (error)
7341 goto done;
7343 error = got_object_open_as_commit(&commit, repo,
7344 head_commit_id);
7345 if (error)
7346 goto done;
7347 parent_ids = got_object_commit_get_parent_ids(commit);
7348 pid = SIMPLEQ_FIRST(parent_ids);
7349 if (pid == NULL) {
7350 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7351 goto done;
7353 error = collect_commits(&commits, head_commit_id, pid->id,
7354 base_commit_id, got_worktree_get_path_prefix(worktree),
7355 GOT_ERR_HISTEDIT_PATH, repo);
7356 got_object_commit_close(commit);
7357 commit = NULL;
7358 if (error)
7359 goto done;
7360 } else {
7361 if (edit_in_progress) {
7362 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7363 goto done;
7366 error = got_ref_open(&branch, repo,
7367 got_worktree_get_head_ref_name(worktree), 0);
7368 if (error != NULL)
7369 goto done;
7371 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7372 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7373 "will not edit commit history of a branch outside "
7374 "the \"refs/heads/\" reference namespace");
7375 goto done;
7378 error = got_ref_resolve(&head_commit_id, repo, branch);
7379 got_ref_close(branch);
7380 branch = NULL;
7381 if (error)
7382 goto done;
7384 error = got_object_open_as_commit(&commit, repo,
7385 head_commit_id);
7386 if (error)
7387 goto done;
7388 parent_ids = got_object_commit_get_parent_ids(commit);
7389 pid = SIMPLEQ_FIRST(parent_ids);
7390 if (pid == NULL) {
7391 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7392 goto done;
7394 error = collect_commits(&commits, head_commit_id, pid->id,
7395 got_worktree_get_base_commit_id(worktree),
7396 got_worktree_get_path_prefix(worktree),
7397 GOT_ERR_HISTEDIT_PATH, repo);
7398 got_object_commit_close(commit);
7399 commit = NULL;
7400 if (error)
7401 goto done;
7403 if (SIMPLEQ_EMPTY(&commits)) {
7404 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7405 goto done;
7408 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7409 &base_commit_id, &fileindex, worktree, repo);
7410 if (error)
7411 goto done;
7413 if (edit_script_path) {
7414 error = histedit_load_list(&histedit_cmds,
7415 edit_script_path, repo);
7416 if (error) {
7417 got_worktree_histedit_abort(worktree, fileindex,
7418 repo, branch, base_commit_id,
7419 update_progress, &did_something);
7420 goto done;
7422 } else {
7423 const char *branch_name;
7424 branch_name = got_ref_get_symref_target(branch);
7425 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7426 branch_name += 11;
7427 error = histedit_edit_script(&histedit_cmds, &commits,
7428 branch_name, edit_logmsg_only, repo);
7429 if (error) {
7430 got_worktree_histedit_abort(worktree, fileindex,
7431 repo, branch, base_commit_id,
7432 update_progress, &did_something);
7433 goto done;
7438 error = histedit_save_list(&histedit_cmds, worktree,
7439 repo);
7440 if (error) {
7441 got_worktree_histedit_abort(worktree, fileindex,
7442 repo, branch, base_commit_id,
7443 update_progress, &did_something);
7444 goto done;
7449 error = histedit_check_script(&histedit_cmds, &commits, repo);
7450 if (error)
7451 goto done;
7453 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7454 if (resume_commit_id) {
7455 if (got_object_id_cmp(hle->commit_id,
7456 resume_commit_id) != 0)
7457 continue;
7459 resume_commit_id = NULL;
7460 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7461 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7462 error = histedit_skip_commit(hle, worktree,
7463 repo);
7464 if (error)
7465 goto done;
7466 } else {
7467 struct got_pathlist_head paths;
7468 int have_changes = 0;
7470 TAILQ_INIT(&paths);
7471 error = got_pathlist_append(&paths, "", NULL);
7472 if (error)
7473 goto done;
7474 error = got_worktree_status(worktree, &paths,
7475 repo, check_local_changes, &have_changes,
7476 check_cancelled, NULL);
7477 got_pathlist_free(&paths);
7478 if (error) {
7479 if (error->code != GOT_ERR_CANCELLED)
7480 goto done;
7481 if (sigint_received || sigpipe_received)
7482 goto done;
7484 if (have_changes) {
7485 error = histedit_commit(NULL, worktree,
7486 fileindex, tmp_branch, hle, repo);
7487 if (error)
7488 goto done;
7489 } else {
7490 error = got_object_open_as_commit(
7491 &commit, repo, hle->commit_id);
7492 if (error)
7493 goto done;
7494 error = show_histedit_progress(commit,
7495 hle, NULL);
7496 got_object_commit_close(commit);
7497 commit = NULL;
7498 if (error)
7499 goto done;
7502 continue;
7505 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7506 error = histedit_skip_commit(hle, worktree, repo);
7507 if (error)
7508 goto done;
7509 continue;
7512 error = got_object_open_as_commit(&commit, repo,
7513 hle->commit_id);
7514 if (error)
7515 goto done;
7516 parent_ids = got_object_commit_get_parent_ids(commit);
7517 pid = SIMPLEQ_FIRST(parent_ids);
7519 error = got_worktree_histedit_merge_files(&merged_paths,
7520 worktree, fileindex, pid->id, hle->commit_id, repo,
7521 rebase_progress, &rebase_status, check_cancelled, NULL);
7522 if (error)
7523 goto done;
7524 got_object_commit_close(commit);
7525 commit = NULL;
7527 if (rebase_status == GOT_STATUS_CONFLICT) {
7528 error = show_rebase_merge_conflict(hle->commit_id,
7529 repo);
7530 if (error)
7531 goto done;
7532 got_worktree_rebase_pathlist_free(&merged_paths);
7533 break;
7536 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7537 char *id_str;
7538 error = got_object_id_str(&id_str, hle->commit_id);
7539 if (error)
7540 goto done;
7541 printf("Stopping histedit for amending commit %s\n",
7542 id_str);
7543 free(id_str);
7544 got_worktree_rebase_pathlist_free(&merged_paths);
7545 error = got_worktree_histedit_postpone(worktree,
7546 fileindex);
7547 goto done;
7550 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7551 error = histedit_skip_commit(hle, worktree, repo);
7552 if (error)
7553 goto done;
7554 continue;
7557 error = histedit_commit(&merged_paths, worktree, fileindex,
7558 tmp_branch, hle, repo);
7559 got_worktree_rebase_pathlist_free(&merged_paths);
7560 if (error)
7561 goto done;
7564 if (rebase_status == GOT_STATUS_CONFLICT) {
7565 error = got_worktree_histedit_postpone(worktree, fileindex);
7566 if (error)
7567 goto done;
7568 error = got_error_msg(GOT_ERR_CONFLICTS,
7569 "conflicts must be resolved before histedit can continue");
7570 } else
7571 error = histedit_complete(worktree, fileindex, tmp_branch,
7572 branch, repo);
7573 done:
7574 got_object_id_queue_free(&commits);
7575 histedit_free_list(&histedit_cmds);
7576 free(head_commit_id);
7577 free(base_commit_id);
7578 free(resume_commit_id);
7579 if (commit)
7580 got_object_commit_close(commit);
7581 if (branch)
7582 got_ref_close(branch);
7583 if (tmp_branch)
7584 got_ref_close(tmp_branch);
7585 if (worktree)
7586 got_worktree_close(worktree);
7587 if (repo)
7588 got_repo_close(repo);
7589 return error;
7592 __dead static void
7593 usage_integrate(void)
7595 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7596 exit(1);
7599 static const struct got_error *
7600 cmd_integrate(int argc, char *argv[])
7602 const struct got_error *error = NULL;
7603 struct got_repository *repo = NULL;
7604 struct got_worktree *worktree = NULL;
7605 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7606 const char *branch_arg = NULL;
7607 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7608 struct got_fileindex *fileindex = NULL;
7609 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7610 int ch, did_something = 0;
7612 while ((ch = getopt(argc, argv, "")) != -1) {
7613 switch (ch) {
7614 default:
7615 usage_integrate();
7616 /* NOTREACHED */
7620 argc -= optind;
7621 argv += optind;
7623 if (argc != 1)
7624 usage_integrate();
7625 branch_arg = argv[0];
7627 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7628 "unveil", NULL) == -1)
7629 err(1, "pledge");
7631 cwd = getcwd(NULL, 0);
7632 if (cwd == NULL) {
7633 error = got_error_from_errno("getcwd");
7634 goto done;
7637 error = got_worktree_open(&worktree, cwd);
7638 if (error)
7639 goto done;
7641 error = check_rebase_or_histedit_in_progress(worktree);
7642 if (error)
7643 goto done;
7645 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7646 NULL);
7647 if (error != NULL)
7648 goto done;
7650 error = apply_unveil(got_repo_get_path(repo), 0,
7651 got_worktree_get_root_path(worktree));
7652 if (error)
7653 goto done;
7655 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7656 error = got_error_from_errno("asprintf");
7657 goto done;
7660 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7661 &base_branch_ref, worktree, refname, repo);
7662 if (error)
7663 goto done;
7665 refname = strdup(got_ref_get_name(branch_ref));
7666 if (refname == NULL) {
7667 error = got_error_from_errno("strdup");
7668 got_worktree_integrate_abort(worktree, fileindex, repo,
7669 branch_ref, base_branch_ref);
7670 goto done;
7672 base_refname = strdup(got_ref_get_name(base_branch_ref));
7673 if (base_refname == NULL) {
7674 error = got_error_from_errno("strdup");
7675 got_worktree_integrate_abort(worktree, fileindex, repo,
7676 branch_ref, base_branch_ref);
7677 goto done;
7680 error = got_ref_resolve(&commit_id, repo, branch_ref);
7681 if (error)
7682 goto done;
7684 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7685 if (error)
7686 goto done;
7688 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7689 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7690 "specified branch has already been integrated");
7691 got_worktree_integrate_abort(worktree, fileindex, repo,
7692 branch_ref, base_branch_ref);
7693 goto done;
7696 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7697 if (error) {
7698 if (error->code == GOT_ERR_ANCESTRY)
7699 error = got_error(GOT_ERR_REBASE_REQUIRED);
7700 got_worktree_integrate_abort(worktree, fileindex, repo,
7701 branch_ref, base_branch_ref);
7702 goto done;
7705 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7706 branch_ref, base_branch_ref, update_progress, &did_something,
7707 check_cancelled, NULL);
7708 if (error)
7709 goto done;
7711 printf("Integrated %s into %s\n", refname, base_refname);
7712 done:
7713 if (repo)
7714 got_repo_close(repo);
7715 if (worktree)
7716 got_worktree_close(worktree);
7717 free(cwd);
7718 free(base_commit_id);
7719 free(commit_id);
7720 free(refname);
7721 free(base_refname);
7722 return error;
7725 __dead static void
7726 usage_stage(void)
7728 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7729 "[file-path ...]\n",
7730 getprogname());
7731 exit(1);
7734 static const struct got_error *
7735 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7736 const char *path, struct got_object_id *blob_id,
7737 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7738 int dirfd, const char *de_name)
7740 const struct got_error *err = NULL;
7741 char *id_str = NULL;
7743 if (staged_status != GOT_STATUS_ADD &&
7744 staged_status != GOT_STATUS_MODIFY &&
7745 staged_status != GOT_STATUS_DELETE)
7746 return NULL;
7748 if (staged_status == GOT_STATUS_ADD ||
7749 staged_status == GOT_STATUS_MODIFY)
7750 err = got_object_id_str(&id_str, staged_blob_id);
7751 else
7752 err = got_object_id_str(&id_str, blob_id);
7753 if (err)
7754 return err;
7756 printf("%s %c %s\n", id_str, staged_status, path);
7757 free(id_str);
7758 return NULL;
7761 static const struct got_error *
7762 cmd_stage(int argc, char *argv[])
7764 const struct got_error *error = NULL;
7765 struct got_repository *repo = NULL;
7766 struct got_worktree *worktree = NULL;
7767 char *cwd = NULL;
7768 struct got_pathlist_head paths;
7769 struct got_pathlist_entry *pe;
7770 int ch, list_stage = 0, pflag = 0;
7771 FILE *patch_script_file = NULL;
7772 const char *patch_script_path = NULL;
7773 struct choose_patch_arg cpa;
7775 TAILQ_INIT(&paths);
7777 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7778 switch (ch) {
7779 case 'l':
7780 list_stage = 1;
7781 break;
7782 case 'p':
7783 pflag = 1;
7784 break;
7785 case 'F':
7786 patch_script_path = optarg;
7787 break;
7788 default:
7789 usage_stage();
7790 /* NOTREACHED */
7794 argc -= optind;
7795 argv += optind;
7797 #ifndef PROFILE
7798 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7799 "unveil", NULL) == -1)
7800 err(1, "pledge");
7801 #endif
7802 if (list_stage && (pflag || patch_script_path))
7803 errx(1, "-l option cannot be used with other options");
7804 if (patch_script_path && !pflag)
7805 errx(1, "-F option can only be used together with -p option");
7807 cwd = getcwd(NULL, 0);
7808 if (cwd == NULL) {
7809 error = got_error_from_errno("getcwd");
7810 goto done;
7813 error = got_worktree_open(&worktree, cwd);
7814 if (error)
7815 goto done;
7817 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7818 NULL);
7819 if (error != NULL)
7820 goto done;
7822 if (patch_script_path) {
7823 patch_script_file = fopen(patch_script_path, "r");
7824 if (patch_script_file == NULL) {
7825 error = got_error_from_errno2("fopen",
7826 patch_script_path);
7827 goto done;
7830 error = apply_unveil(got_repo_get_path(repo), 0,
7831 got_worktree_get_root_path(worktree));
7832 if (error)
7833 goto done;
7835 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7836 if (error)
7837 goto done;
7839 if (list_stage)
7840 error = got_worktree_status(worktree, &paths, repo,
7841 print_stage, NULL, check_cancelled, NULL);
7842 else {
7843 cpa.patch_script_file = patch_script_file;
7844 cpa.action = "stage";
7845 error = got_worktree_stage(worktree, &paths,
7846 pflag ? NULL : print_status, NULL,
7847 pflag ? choose_patch : NULL, &cpa, repo);
7849 done:
7850 if (patch_script_file && fclose(patch_script_file) == EOF &&
7851 error == NULL)
7852 error = got_error_from_errno2("fclose", patch_script_path);
7853 if (repo)
7854 got_repo_close(repo);
7855 if (worktree)
7856 got_worktree_close(worktree);
7857 TAILQ_FOREACH(pe, &paths, entry)
7858 free((char *)pe->path);
7859 got_pathlist_free(&paths);
7860 free(cwd);
7861 return error;
7864 __dead static void
7865 usage_unstage(void)
7867 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7868 "[file-path ...]\n",
7869 getprogname());
7870 exit(1);
7874 static const struct got_error *
7875 cmd_unstage(int argc, char *argv[])
7877 const struct got_error *error = NULL;
7878 struct got_repository *repo = NULL;
7879 struct got_worktree *worktree = NULL;
7880 char *cwd = NULL;
7881 struct got_pathlist_head paths;
7882 struct got_pathlist_entry *pe;
7883 int ch, did_something = 0, pflag = 0;
7884 FILE *patch_script_file = NULL;
7885 const char *patch_script_path = NULL;
7886 struct choose_patch_arg cpa;
7888 TAILQ_INIT(&paths);
7890 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7891 switch (ch) {
7892 case 'p':
7893 pflag = 1;
7894 break;
7895 case 'F':
7896 patch_script_path = optarg;
7897 break;
7898 default:
7899 usage_unstage();
7900 /* NOTREACHED */
7904 argc -= optind;
7905 argv += optind;
7907 #ifndef PROFILE
7908 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7909 "unveil", NULL) == -1)
7910 err(1, "pledge");
7911 #endif
7912 if (patch_script_path && !pflag)
7913 errx(1, "-F option can only be used together with -p option");
7915 cwd = getcwd(NULL, 0);
7916 if (cwd == NULL) {
7917 error = got_error_from_errno("getcwd");
7918 goto done;
7921 error = got_worktree_open(&worktree, cwd);
7922 if (error)
7923 goto done;
7925 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7926 NULL);
7927 if (error != NULL)
7928 goto done;
7930 if (patch_script_path) {
7931 patch_script_file = fopen(patch_script_path, "r");
7932 if (patch_script_file == NULL) {
7933 error = got_error_from_errno2("fopen",
7934 patch_script_path);
7935 goto done;
7939 error = apply_unveil(got_repo_get_path(repo), 0,
7940 got_worktree_get_root_path(worktree));
7941 if (error)
7942 goto done;
7944 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7945 if (error)
7946 goto done;
7948 cpa.patch_script_file = patch_script_file;
7949 cpa.action = "unstage";
7950 error = got_worktree_unstage(worktree, &paths, update_progress,
7951 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7952 done:
7953 if (patch_script_file && fclose(patch_script_file) == EOF &&
7954 error == NULL)
7955 error = got_error_from_errno2("fclose", patch_script_path);
7956 if (repo)
7957 got_repo_close(repo);
7958 if (worktree)
7959 got_worktree_close(worktree);
7960 TAILQ_FOREACH(pe, &paths, entry)
7961 free((char *)pe->path);
7962 got_pathlist_free(&paths);
7963 free(cwd);
7964 return error;
7967 __dead static void
7968 usage_cat(void)
7970 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7971 "arg1 [arg2 ...]\n", getprogname());
7972 exit(1);
7975 static const struct got_error *
7976 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7978 const struct got_error *err;
7979 struct got_blob_object *blob;
7981 err = got_object_open_as_blob(&blob, repo, id, 8192);
7982 if (err)
7983 return err;
7985 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7986 got_object_blob_close(blob);
7987 return err;
7990 static const struct got_error *
7991 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7993 const struct got_error *err;
7994 struct got_tree_object *tree;
7995 int nentries, i;
7997 err = got_object_open_as_tree(&tree, repo, id);
7998 if (err)
7999 return err;
8001 nentries = got_object_tree_get_nentries(tree);
8002 for (i = 0; i < nentries; i++) {
8003 struct got_tree_entry *te;
8004 char *id_str;
8005 if (sigint_received || sigpipe_received)
8006 break;
8007 te = got_object_tree_get_entry(tree, i);
8008 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8009 if (err)
8010 break;
8011 fprintf(outfile, "%s %.7o %s\n", id_str,
8012 got_tree_entry_get_mode(te),
8013 got_tree_entry_get_name(te));
8014 free(id_str);
8017 got_object_tree_close(tree);
8018 return err;
8021 static const struct got_error *
8022 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8024 const struct got_error *err;
8025 struct got_commit_object *commit;
8026 const struct got_object_id_queue *parent_ids;
8027 struct got_object_qid *pid;
8028 char *id_str = NULL;
8029 const char *logmsg = NULL;
8031 err = got_object_open_as_commit(&commit, repo, id);
8032 if (err)
8033 return err;
8035 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8036 if (err)
8037 goto done;
8039 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8040 parent_ids = got_object_commit_get_parent_ids(commit);
8041 fprintf(outfile, "numparents %d\n",
8042 got_object_commit_get_nparents(commit));
8043 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8044 char *pid_str;
8045 err = got_object_id_str(&pid_str, pid->id);
8046 if (err)
8047 goto done;
8048 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8049 free(pid_str);
8051 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8052 got_object_commit_get_author(commit),
8053 got_object_commit_get_author_time(commit));
8055 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8056 got_object_commit_get_author(commit),
8057 got_object_commit_get_committer_time(commit));
8059 logmsg = got_object_commit_get_logmsg_raw(commit);
8060 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8061 fprintf(outfile, "%s", logmsg);
8062 done:
8063 free(id_str);
8064 got_object_commit_close(commit);
8065 return err;
8068 static const struct got_error *
8069 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8071 const struct got_error *err;
8072 struct got_tag_object *tag;
8073 char *id_str = NULL;
8074 const char *tagmsg = NULL;
8076 err = got_object_open_as_tag(&tag, repo, id);
8077 if (err)
8078 return err;
8080 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8081 if (err)
8082 goto done;
8084 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8086 switch (got_object_tag_get_object_type(tag)) {
8087 case GOT_OBJ_TYPE_BLOB:
8088 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8089 GOT_OBJ_LABEL_BLOB);
8090 break;
8091 case GOT_OBJ_TYPE_TREE:
8092 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8093 GOT_OBJ_LABEL_TREE);
8094 break;
8095 case GOT_OBJ_TYPE_COMMIT:
8096 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8097 GOT_OBJ_LABEL_COMMIT);
8098 break;
8099 case GOT_OBJ_TYPE_TAG:
8100 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8101 GOT_OBJ_LABEL_TAG);
8102 break;
8103 default:
8104 break;
8107 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8108 got_object_tag_get_name(tag));
8110 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8111 got_object_tag_get_tagger(tag),
8112 got_object_tag_get_tagger_time(tag));
8114 tagmsg = got_object_tag_get_message(tag);
8115 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8116 fprintf(outfile, "%s", tagmsg);
8117 done:
8118 free(id_str);
8119 got_object_tag_close(tag);
8120 return err;
8123 static const struct got_error *
8124 cmd_cat(int argc, char *argv[])
8126 const struct got_error *error;
8127 struct got_repository *repo = NULL;
8128 struct got_worktree *worktree = NULL;
8129 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8130 const char *commit_id_str = NULL;
8131 struct got_object_id *id = NULL, *commit_id = NULL;
8132 int ch, obj_type, i, force_path = 0;
8134 #ifndef PROFILE
8135 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8136 NULL) == -1)
8137 err(1, "pledge");
8138 #endif
8140 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8141 switch (ch) {
8142 case 'c':
8143 commit_id_str = optarg;
8144 break;
8145 case 'r':
8146 repo_path = realpath(optarg, NULL);
8147 if (repo_path == NULL)
8148 return got_error_from_errno2("realpath",
8149 optarg);
8150 got_path_strip_trailing_slashes(repo_path);
8151 break;
8152 case 'P':
8153 force_path = 1;
8154 break;
8155 default:
8156 usage_cat();
8157 /* NOTREACHED */
8161 argc -= optind;
8162 argv += optind;
8164 cwd = getcwd(NULL, 0);
8165 if (cwd == NULL) {
8166 error = got_error_from_errno("getcwd");
8167 goto done;
8169 error = got_worktree_open(&worktree, cwd);
8170 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8171 goto done;
8172 if (worktree) {
8173 if (repo_path == NULL) {
8174 repo_path = strdup(
8175 got_worktree_get_repo_path(worktree));
8176 if (repo_path == NULL) {
8177 error = got_error_from_errno("strdup");
8178 goto done;
8183 if (repo_path == NULL) {
8184 repo_path = getcwd(NULL, 0);
8185 if (repo_path == NULL)
8186 return got_error_from_errno("getcwd");
8189 error = got_repo_open(&repo, repo_path, NULL);
8190 free(repo_path);
8191 if (error != NULL)
8192 goto done;
8194 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8195 if (error)
8196 goto done;
8198 if (commit_id_str == NULL)
8199 commit_id_str = GOT_REF_HEAD;
8200 error = got_repo_match_object_id(&commit_id, NULL,
8201 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8202 if (error)
8203 goto done;
8205 for (i = 0; i < argc; i++) {
8206 if (force_path) {
8207 error = got_object_id_by_path(&id, repo, commit_id,
8208 argv[i]);
8209 if (error)
8210 break;
8211 } else {
8212 error = got_repo_match_object_id(&id, &label, argv[i],
8213 GOT_OBJ_TYPE_ANY, 0, repo);
8214 if (error) {
8215 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8216 error->code != GOT_ERR_NOT_REF)
8217 break;
8218 error = got_object_id_by_path(&id, repo,
8219 commit_id, argv[i]);
8220 if (error)
8221 break;
8225 error = got_object_get_type(&obj_type, repo, id);
8226 if (error)
8227 break;
8229 switch (obj_type) {
8230 case GOT_OBJ_TYPE_BLOB:
8231 error = cat_blob(id, repo, stdout);
8232 break;
8233 case GOT_OBJ_TYPE_TREE:
8234 error = cat_tree(id, repo, stdout);
8235 break;
8236 case GOT_OBJ_TYPE_COMMIT:
8237 error = cat_commit(id, repo, stdout);
8238 break;
8239 case GOT_OBJ_TYPE_TAG:
8240 error = cat_tag(id, repo, stdout);
8241 break;
8242 default:
8243 error = got_error(GOT_ERR_OBJ_TYPE);
8244 break;
8246 if (error)
8247 break;
8248 free(label);
8249 label = NULL;
8250 free(id);
8251 id = NULL;
8253 done:
8254 free(label);
8255 free(id);
8256 free(commit_id);
8257 if (worktree)
8258 got_worktree_close(worktree);
8259 if (repo) {
8260 const struct got_error *repo_error;
8261 repo_error = got_repo_close(repo);
8262 if (error == NULL)
8263 error = repo_error;
8265 return error;