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 [-a] [-m] [-q] [-v] repository-url "
815 "[directory]\n", getprogname());
816 exit(1);
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
821 int last_p_indexed;
822 int last_p_resolved;
823 int verbosity;
824 };
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
836 return NULL;
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
840 fflush(stdout);
841 return NULL;
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 print_size = 1;
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
857 print_indexed = 1;
858 print_size = 1;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
866 print_resolved = 1;
867 print_indexed = 1;
868 print_size = 1;
873 if (print_size || print_indexed || print_resolved)
874 printf("\r");
875 if (print_size)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 if (print_indexed)
878 printf("; indexing %d%%", p_indexed);
879 if (print_resolved)
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
882 fflush(stdout);
884 return NULL;
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
905 ssize_t n;
906 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 struct got_reference *head_symref = NULL;
909 TAILQ_INIT(&refs);
910 TAILQ_INIT(&symrefs);
912 while ((ch = getopt(argc, argv, "amvq")) != -1) {
913 switch (ch) {
914 case 'a':
915 fetch_all_branches = 1;
916 break;
917 case 'm':
918 mirror_references = 1;
919 break;
920 case 'v':
921 if (verbosity < 0)
922 verbosity = 0;
923 else if (verbosity < 3)
924 verbosity++;
925 break;
926 case 'q':
927 verbosity = -1;
928 break;
929 default:
930 usage_clone();
931 break;
934 argc -= optind;
935 argv += optind;
937 uri = argv[0];
939 if (argc == 1)
940 dirname = NULL;
941 else if (argc == 2)
942 dirname = argv[1];
943 else
944 usage_clone();
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
948 if (error)
949 goto done;
951 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 host, port ? ":" : "", port ? port : "",
953 server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 error = got_error_from_errno("asprintf");
955 goto done;
958 if (strcmp(proto, "git") == 0) {
959 #ifndef PROFILE
960 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 "sendfd dns inet unveil", NULL) == -1)
962 err(1, "pledge");
963 #endif
964 } else if (strcmp(proto, "git+ssh") == 0 ||
965 strcmp(proto, "ssh") == 0) {
966 #ifndef PROFILE
967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 "sendfd unveil", NULL) == -1)
969 err(1, "pledge");
970 #endif
971 } else if (strcmp(proto, "http") == 0 ||
972 strcmp(proto, "git+http") == 0) {
973 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
974 goto done;
975 } else {
976 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
977 goto done;
979 if (dirname == NULL) {
980 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 error = got_error_from_errno("asprintf");
982 goto done;
984 repo_path = default_destdir;
985 } else
986 repo_path = dirname;
988 error = got_path_mkdir(repo_path);
989 if (error)
990 goto done;
992 error = got_repo_init(repo_path);
993 if (error)
994 goto done;
996 error = got_repo_open(&repo, repo_path, NULL);
997 if (error)
998 goto done;
1000 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 error = got_error_from_errno2("unveil",
1003 GOT_FETCH_PATH_SSH);
1004 goto done;
1007 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1008 if (error)
1009 goto done;
1011 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1012 verbosity);
1013 if (error)
1014 goto done;
1016 if (verbosity >= 0)
1017 printf("Connected to %s%s%s\n", host,
1018 port ? ":" : "", port ? port : "");
1020 fpa.last_scaled_size[0] = '\0';
1021 fpa.last_p_indexed = -1;
1022 fpa.last_p_resolved = -1;
1023 fpa.verbosity = verbosity;
1024 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1027 if (error)
1028 goto done;
1030 error = got_object_id_str(&id_str, pack_hash);
1031 if (error)
1032 goto done;
1033 if (verbosity >= 0)
1034 printf("\nFetched %s.pack\n", id_str);
1035 free(id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1045 if (error)
1046 goto done;
1047 error = got_ref_write(ref, repo);
1048 got_ref_close(ref);
1049 if (error)
1050 goto done;
1052 if (mirror_references)
1053 continue;
1055 if (strncmp("refs/heads/", refname, 11) != 0)
1056 continue;
1058 if (asprintf(&remote_refname,
1059 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 refname + 11) == -1) {
1061 error = got_error_from_errno("asprintf");
1062 goto done;
1064 error = got_ref_alloc(&ref, remote_refname, id);
1065 if (error)
1066 goto done;
1067 error = got_ref_write(ref, repo);
1068 got_ref_close(ref);
1069 if (error)
1070 goto done;
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1080 continue;
1082 error = got_ref_open(&target_ref, repo, target, 0);
1083 if (error) {
1084 if (error->code == GOT_ERR_NOT_REF) {
1085 error = NULL;
1086 continue;
1088 goto done;
1091 error = got_ref_alloc_symref(&head_symref,
1092 GOT_REF_HEAD, target_ref);
1093 got_ref_close(target_ref);
1094 if (error)
1095 goto done;
1097 if (verbosity >= 0)
1098 printf("Setting %s to %s\n", GOT_REF_HEAD,
1099 got_ref_get_symref_target(head_symref));
1101 error = got_ref_write(head_symref, repo);
1102 if (error)
1103 goto done;
1106 /* Create a config file git-fetch(1) can understand. */
1107 gitconfig_path = got_repo_get_path_gitconfig(repo);
1108 if (gitconfig_path == NULL) {
1109 error = got_error_from_errno("got_repo_get_path_gitconfig");
1110 goto done;
1112 gitconfig_file = fopen(gitconfig_path, "a");
1113 if (gitconfig_file == NULL) {
1114 error = got_error_from_errno2("fopen", gitconfig_path);
1115 goto done;
1117 if (mirror_references) {
1118 if (asprintf(&gitconfig,
1119 "[remote \"%s\"]\n"
1120 "\turl = %s\n"
1121 "\tfetch = +refs/*:refs/*\n"
1122 "\tmirror = true\n",
1123 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1124 error = got_error_from_errno("asprintf");
1125 goto done;
1127 } else if (fetch_all_branches) {
1128 if (asprintf(&gitconfig,
1129 "[remote \"%s\"]\n"
1130 "\turl = %s\n"
1131 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1132 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1133 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1134 error = got_error_from_errno("asprintf");
1135 goto done;
1137 } else {
1138 const char *branchname;
1141 * If the server specified a default branch, use just that one.
1142 * Otherwise fall back to fetching all branches on next fetch.
1144 if (head_symref) {
1145 branchname = got_ref_get_symref_target(head_symref);
1146 if (strncmp(branchname, "refs/heads/", 11) == 0)
1147 branchname += 11;
1148 } else
1149 branchname = "*"; /* fall back to all branches */
1150 if (asprintf(&gitconfig,
1151 "[remote \"%s\"]\n"
1152 "\turl = %s\n"
1153 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1154 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1155 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1156 branchname) == -1) {
1157 error = got_error_from_errno("asprintf");
1158 goto done;
1161 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1162 if (n != strlen(gitconfig)) {
1163 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1164 goto done;
1168 if (verbosity >= 0)
1169 printf("Created %s repository '%s'\n",
1170 mirror_references ? "mirrored" : "cloned", repo_path);
1171 done:
1172 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1173 error = got_error_from_errno("close");
1174 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1175 error = got_error_from_errno("fclose");
1176 if (repo)
1177 got_repo_close(repo);
1178 if (head_symref)
1179 got_ref_close(head_symref);
1180 TAILQ_FOREACH(pe, &refs, entry) {
1181 free((void *)pe->path);
1182 free(pe->data);
1184 got_pathlist_free(&refs);
1185 TAILQ_FOREACH(pe, &symrefs, entry) {
1186 free((void *)pe->path);
1187 free(pe->data);
1189 got_pathlist_free(&symrefs);
1190 free(pack_hash);
1191 free(proto);
1192 free(host);
1193 free(port);
1194 free(server_path);
1195 free(repo_name);
1196 free(default_destdir);
1197 free(gitconfig_path);
1198 free(git_url);
1199 return error;
1202 static const struct got_error *
1203 create_ref(const char *refname, struct got_object_id *id,
1204 const char *id_str, struct got_repository *repo)
1206 const struct got_error *err = NULL;
1207 struct got_reference *ref;
1209 printf("Creating %s: %s\n", refname, id_str);
1211 err = got_ref_alloc(&ref, refname, id);
1212 if (err)
1213 return err;
1215 err = got_ref_write(ref, repo);
1216 got_ref_close(ref);
1217 return err;
1220 static const struct got_error *
1221 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1222 struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 char *new_id_str = NULL;
1226 struct got_object_id *old_id = NULL;
1228 err = got_object_id_str(&new_id_str, new_id);
1229 if (err)
1230 goto done;
1232 if (got_ref_is_symbolic(ref)) {
1233 struct got_reference *new_ref;
1234 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1235 if (err)
1236 goto done;
1237 printf("Deleting symbolic reference %s -> %s\n",
1238 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1239 err = got_ref_delete(ref, repo);
1240 if (err)
1241 goto done;
1242 printf("Setting %s to %s\n", got_ref_get_name(ref),
1243 new_id_str);
1244 err = got_ref_write(new_ref, repo);
1245 if (err)
1246 goto done;
1247 } else {
1248 err = got_ref_resolve(&old_id, repo, ref);
1249 if (err)
1250 goto done;
1251 if (got_object_id_cmp(old_id, new_id) != 0) {
1252 printf("Setting %s to %s\n",
1253 got_ref_get_name(ref), new_id_str);
1254 err = got_ref_change_ref(ref, new_id);
1255 if (err)
1256 goto done;
1257 err = got_ref_write(ref, repo);
1258 if (err)
1259 goto done;
1262 done:
1263 free(old_id);
1264 free(new_id_str);
1265 return err;
1268 __dead static void
1269 usage_fetch(void)
1271 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1272 "[remote-repository-name]\n", getprogname());
1273 exit(1);
1276 static const struct got_error *
1277 cmd_fetch(int argc, char *argv[])
1279 const struct got_error *error = NULL;
1280 char *cwd = NULL, *repo_path = NULL;
1281 const char *remote_name;
1282 char *proto = NULL, *host = NULL, *port = NULL;
1283 char *repo_name = NULL, *server_path = NULL;
1284 struct got_remote_repo *remotes, *remote = NULL;
1285 int nremotes;
1286 char *id_str = NULL;
1287 struct got_repository *repo = NULL;
1288 struct got_worktree *worktree = NULL;
1289 struct got_pathlist_head refs, symrefs;
1290 struct got_pathlist_entry *pe;
1291 struct got_object_id *pack_hash = NULL;
1292 int i, ch, fetchfd = -1;
1293 struct got_fetch_progress_arg fpa;
1294 int verbosity = 0, fetch_all_branches = 0;
1296 TAILQ_INIT(&refs);
1297 TAILQ_INIT(&symrefs);
1299 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1300 switch (ch) {
1301 case 'a':
1302 fetch_all_branches = 1;
1303 break;
1304 case 'r':
1305 repo_path = realpath(optarg, NULL);
1306 if (repo_path == NULL)
1307 return got_error_from_errno2("realpath",
1308 optarg);
1309 got_path_strip_trailing_slashes(repo_path);
1310 break;
1311 case 'v':
1312 if (verbosity < 0)
1313 verbosity = 0;
1314 else if (verbosity < 3)
1315 verbosity++;
1316 break;
1317 case 'q':
1318 verbosity = -1;
1319 break;
1320 default:
1321 usage_fetch();
1322 break;
1325 argc -= optind;
1326 argv += optind;
1328 if (argc == 0)
1329 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1330 else if (argc == 1)
1331 remote_name = argv[0];
1332 else
1333 usage_fetch();
1335 cwd = getcwd(NULL, 0);
1336 if (cwd == NULL) {
1337 error = got_error_from_errno("getcwd");
1338 goto done;
1341 if (repo_path == NULL) {
1342 error = got_worktree_open(&worktree, cwd);
1343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1344 goto done;
1345 else
1346 error = NULL;
1347 if (worktree) {
1348 repo_path =
1349 strdup(got_worktree_get_repo_path(worktree));
1350 if (repo_path == NULL)
1351 error = got_error_from_errno("strdup");
1352 if (error)
1353 goto done;
1354 } else {
1355 repo_path = strdup(cwd);
1356 if (repo_path == NULL) {
1357 error = got_error_from_errno("strdup");
1358 goto done;
1363 error = got_repo_open(&repo, repo_path, NULL);
1364 if (error)
1365 goto done;
1367 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1368 for (i = 0; i < nremotes; i++) {
1369 remote = &remotes[i];
1370 if (strcmp(remote->name, remote_name) == 0)
1371 break;
1373 if (i == nremotes) {
1374 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1375 goto done;
1378 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1379 &repo_name, remote->url);
1380 if (error)
1381 goto done;
1383 if (strcmp(proto, "git") == 0) {
1384 #ifndef PROFILE
1385 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1386 "sendfd dns inet unveil", NULL) == -1)
1387 err(1, "pledge");
1388 #endif
1389 } else if (strcmp(proto, "git+ssh") == 0 ||
1390 strcmp(proto, "ssh") == 0) {
1391 #ifndef PROFILE
1392 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1393 "sendfd unveil", NULL) == -1)
1394 err(1, "pledge");
1395 #endif
1396 } else if (strcmp(proto, "http") == 0 ||
1397 strcmp(proto, "git+http") == 0) {
1398 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1399 goto done;
1400 } else {
1401 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1402 goto done;
1405 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1406 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1407 error = got_error_from_errno2("unveil",
1408 GOT_FETCH_PATH_SSH);
1409 goto done;
1412 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1413 if (error)
1414 goto done;
1416 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1417 verbosity);
1418 if (error)
1419 goto done;
1421 if (verbosity >= 0)
1422 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1423 port ? ":" : "", port ? port : "");
1425 fpa.last_scaled_size[0] = '\0';
1426 fpa.last_p_indexed = -1;
1427 fpa.last_p_resolved = -1;
1428 fpa.verbosity = verbosity;
1429 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1430 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1431 fetch_progress, &fpa);
1432 if (error)
1433 goto done;
1435 if (pack_hash == NULL) {
1436 if (verbosity >= 0)
1437 printf("Already up-to-date\n");
1438 goto done;
1441 if (verbosity >= 0) {
1442 error = got_object_id_str(&id_str, pack_hash);
1443 if (error)
1444 goto done;
1445 printf("\nFetched %s.pack\n", id_str);
1446 free(id_str);
1447 id_str = NULL;
1450 /* Update references provided with the pack file. */
1451 TAILQ_FOREACH(pe, &refs, entry) {
1452 const char *refname = pe->path;
1453 struct got_object_id *id = pe->data;
1454 struct got_reference *ref;
1455 char *remote_refname;
1457 error = got_object_id_str(&id_str, id);
1458 if (error)
1459 goto done;
1461 if (remote->mirror_references ||
1462 strncmp("refs/tags/", refname, 10) == 0) {
1463 error = got_ref_open(&ref, repo, refname, 0);
1464 if (error) {
1465 if (error->code != GOT_ERR_NOT_REF)
1466 goto done;
1467 error = create_ref(refname, id, id_str, repo);
1468 if (error)
1469 goto done;
1470 } else {
1471 error = update_ref(ref, id, repo);
1472 got_ref_close(ref);
1473 if (error)
1474 goto done;
1476 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1477 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1478 remote_name, refname + 11) == -1) {
1479 error = got_error_from_errno("asprintf");
1480 goto done;
1483 error = got_ref_open(&ref, repo, remote_refname, 0);
1484 if (error) {
1485 if (error->code != GOT_ERR_NOT_REF)
1486 goto done;
1487 error = create_ref(remote_refname, id, id_str,
1488 repo);
1489 if (error)
1490 goto done;
1491 } else {
1492 error = update_ref(ref, id, repo);
1493 got_ref_close(ref);
1494 if (error)
1495 goto done;
1498 /* Also create a local branch if none exists yet. */
1499 error = got_ref_open(&ref, repo, refname, 0);
1500 if (error) {
1501 if (error->code != GOT_ERR_NOT_REF)
1502 goto done;
1503 error = create_ref(refname, id, id_str, repo);
1504 if (error)
1505 goto done;
1506 } else
1507 got_ref_close(ref);
1509 free(id_str);
1510 id_str = NULL;
1512 done:
1513 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1514 error = got_error_from_errno("close");
1515 if (repo)
1516 got_repo_close(repo);
1517 if (worktree)
1518 got_worktree_close(worktree);
1519 TAILQ_FOREACH(pe, &refs, entry) {
1520 free((void *)pe->path);
1521 free(pe->data);
1523 got_pathlist_free(&refs);
1524 TAILQ_FOREACH(pe, &symrefs, entry) {
1525 free((void *)pe->path);
1526 free(pe->data);
1528 got_pathlist_free(&symrefs);
1529 free(id_str);
1530 free(cwd);
1531 free(repo_path);
1532 free(pack_hash);
1533 free(proto);
1534 free(host);
1535 free(port);
1536 free(server_path);
1537 free(repo_name);
1538 return error;
1542 __dead static void
1543 usage_checkout(void)
1545 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1546 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1547 exit(1);
1550 static void
1551 show_worktree_base_ref_warning(void)
1553 fprintf(stderr, "%s: warning: could not create a reference "
1554 "to the work tree's base commit; the commit could be "
1555 "garbage-collected by Git; making the repository "
1556 "writable and running 'got update' will prevent this\n",
1557 getprogname());
1560 struct got_checkout_progress_arg {
1561 const char *worktree_path;
1562 int had_base_commit_ref_error;
1565 static const struct got_error *
1566 checkout_progress(void *arg, unsigned char status, const char *path)
1568 struct got_checkout_progress_arg *a = arg;
1570 /* Base commit bump happens silently. */
1571 if (status == GOT_STATUS_BUMP_BASE)
1572 return NULL;
1574 if (status == GOT_STATUS_BASE_REF_ERR) {
1575 a->had_base_commit_ref_error = 1;
1576 return NULL;
1579 while (path[0] == '/')
1580 path++;
1582 printf("%c %s/%s\n", status, a->worktree_path, path);
1583 return NULL;
1586 static const struct got_error *
1587 check_cancelled(void *arg)
1589 if (sigint_received || sigpipe_received)
1590 return got_error(GOT_ERR_CANCELLED);
1591 return NULL;
1594 static const struct got_error *
1595 check_linear_ancestry(struct got_object_id *commit_id,
1596 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1597 struct got_repository *repo)
1599 const struct got_error *err = NULL;
1600 struct got_object_id *yca_id;
1602 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1603 commit_id, base_commit_id, repo, check_cancelled, NULL);
1604 if (err)
1605 return err;
1607 if (yca_id == NULL)
1608 return got_error(GOT_ERR_ANCESTRY);
1611 * Require a straight line of history between the target commit
1612 * and the work tree's base commit.
1614 * Non-linear situations such as this require a rebase:
1616 * (commit) D F (base_commit)
1617 * \ /
1618 * C E
1619 * \ /
1620 * B (yca)
1621 * |
1622 * A
1624 * 'got update' only handles linear cases:
1625 * Update forwards in time: A (base/yca) - B - C - D (commit)
1626 * Update backwards in time: D (base) - C - B - A (commit/yca)
1628 if (allow_forwards_in_time_only) {
1629 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1630 return got_error(GOT_ERR_ANCESTRY);
1631 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1632 got_object_id_cmp(base_commit_id, yca_id) != 0)
1633 return got_error(GOT_ERR_ANCESTRY);
1635 free(yca_id);
1636 return NULL;
1639 static const struct got_error *
1640 check_same_branch(struct got_object_id *commit_id,
1641 struct got_reference *head_ref, struct got_object_id *yca_id,
1642 struct got_repository *repo)
1644 const struct got_error *err = NULL;
1645 struct got_commit_graph *graph = NULL;
1646 struct got_object_id *head_commit_id = NULL;
1647 int is_same_branch = 0;
1649 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1650 if (err)
1651 goto done;
1653 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1654 is_same_branch = 1;
1655 goto done;
1657 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1658 is_same_branch = 1;
1659 goto done;
1662 err = got_commit_graph_open(&graph, "/", 1);
1663 if (err)
1664 goto done;
1666 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1667 check_cancelled, NULL);
1668 if (err)
1669 goto done;
1671 for (;;) {
1672 struct got_object_id *id;
1673 err = got_commit_graph_iter_next(&id, graph, repo,
1674 check_cancelled, NULL);
1675 if (err) {
1676 if (err->code == GOT_ERR_ITER_COMPLETED)
1677 err = NULL;
1678 break;
1681 if (id) {
1682 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1683 break;
1684 if (got_object_id_cmp(id, commit_id) == 0) {
1685 is_same_branch = 1;
1686 break;
1690 done:
1691 if (graph)
1692 got_commit_graph_close(graph);
1693 free(head_commit_id);
1694 if (!err && !is_same_branch)
1695 err = got_error(GOT_ERR_ANCESTRY);
1696 return err;
1699 static const struct got_error *
1700 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1702 static char msg[512];
1703 const char *branch_name;
1705 if (got_ref_is_symbolic(ref))
1706 branch_name = got_ref_get_symref_target(ref);
1707 else
1708 branch_name = got_ref_get_name(ref);
1710 if (strncmp("refs/heads/", branch_name, 11) == 0)
1711 branch_name += 11;
1713 snprintf(msg, sizeof(msg),
1714 "target commit is not contained in branch '%s'; "
1715 "the branch to use must be specified with -b; "
1716 "if necessary a new branch can be created for "
1717 "this commit with 'got branch -c %s BRANCH_NAME'",
1718 branch_name, commit_id_str);
1720 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1723 static const struct got_error *
1724 cmd_checkout(int argc, char *argv[])
1726 const struct got_error *error = NULL;
1727 struct got_repository *repo = NULL;
1728 struct got_reference *head_ref = NULL;
1729 struct got_worktree *worktree = NULL;
1730 char *repo_path = NULL;
1731 char *worktree_path = NULL;
1732 const char *path_prefix = "";
1733 const char *branch_name = GOT_REF_HEAD;
1734 char *commit_id_str = NULL;
1735 int ch, same_path_prefix, allow_nonempty = 0;
1736 struct got_pathlist_head paths;
1737 struct got_checkout_progress_arg cpa;
1739 TAILQ_INIT(&paths);
1741 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1742 switch (ch) {
1743 case 'b':
1744 branch_name = optarg;
1745 break;
1746 case 'c':
1747 commit_id_str = strdup(optarg);
1748 if (commit_id_str == NULL)
1749 return got_error_from_errno("strdup");
1750 break;
1751 case 'E':
1752 allow_nonempty = 1;
1753 break;
1754 case 'p':
1755 path_prefix = optarg;
1756 break;
1757 default:
1758 usage_checkout();
1759 /* NOTREACHED */
1763 argc -= optind;
1764 argv += optind;
1766 #ifndef PROFILE
1767 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1768 "unveil", NULL) == -1)
1769 err(1, "pledge");
1770 #endif
1771 if (argc == 1) {
1772 char *cwd, *base, *dotgit;
1773 repo_path = realpath(argv[0], NULL);
1774 if (repo_path == NULL)
1775 return got_error_from_errno2("realpath", argv[0]);
1776 cwd = getcwd(NULL, 0);
1777 if (cwd == NULL) {
1778 error = got_error_from_errno("getcwd");
1779 goto done;
1781 if (path_prefix[0]) {
1782 base = basename(path_prefix);
1783 if (base == NULL) {
1784 error = got_error_from_errno2("basename",
1785 path_prefix);
1786 goto done;
1788 } else {
1789 base = basename(repo_path);
1790 if (base == NULL) {
1791 error = got_error_from_errno2("basename",
1792 repo_path);
1793 goto done;
1796 dotgit = strstr(base, ".git");
1797 if (dotgit)
1798 *dotgit = '\0';
1799 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1800 error = got_error_from_errno("asprintf");
1801 free(cwd);
1802 goto done;
1804 free(cwd);
1805 } else if (argc == 2) {
1806 repo_path = realpath(argv[0], NULL);
1807 if (repo_path == NULL) {
1808 error = got_error_from_errno2("realpath", argv[0]);
1809 goto done;
1811 worktree_path = realpath(argv[1], NULL);
1812 if (worktree_path == NULL) {
1813 if (errno != ENOENT) {
1814 error = got_error_from_errno2("realpath",
1815 argv[1]);
1816 goto done;
1818 worktree_path = strdup(argv[1]);
1819 if (worktree_path == NULL) {
1820 error = got_error_from_errno("strdup");
1821 goto done;
1824 } else
1825 usage_checkout();
1827 got_path_strip_trailing_slashes(repo_path);
1828 got_path_strip_trailing_slashes(worktree_path);
1830 error = got_repo_open(&repo, repo_path, NULL);
1831 if (error != NULL)
1832 goto done;
1834 /* Pre-create work tree path for unveil(2) */
1835 error = got_path_mkdir(worktree_path);
1836 if (error) {
1837 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1838 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1839 goto done;
1840 if (!allow_nonempty &&
1841 !got_path_dir_is_empty(worktree_path)) {
1842 error = got_error_path(worktree_path,
1843 GOT_ERR_DIR_NOT_EMPTY);
1844 goto done;
1848 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1849 if (error)
1850 goto done;
1852 error = got_ref_open(&head_ref, repo, branch_name, 0);
1853 if (error != NULL)
1854 goto done;
1856 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1857 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1858 goto done;
1860 error = got_worktree_open(&worktree, worktree_path);
1861 if (error != NULL)
1862 goto done;
1864 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1865 path_prefix);
1866 if (error != NULL)
1867 goto done;
1868 if (!same_path_prefix) {
1869 error = got_error(GOT_ERR_PATH_PREFIX);
1870 goto done;
1873 if (commit_id_str) {
1874 struct got_object_id *commit_id;
1875 error = got_repo_match_object_id(&commit_id, NULL,
1876 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1877 if (error)
1878 goto done;
1879 error = check_linear_ancestry(commit_id,
1880 got_worktree_get_base_commit_id(worktree), 0, repo);
1881 if (error != NULL) {
1882 free(commit_id);
1883 if (error->code == GOT_ERR_ANCESTRY) {
1884 error = checkout_ancestry_error(
1885 head_ref, commit_id_str);
1887 goto done;
1889 error = check_same_branch(commit_id, head_ref, NULL, repo);
1890 if (error) {
1891 if (error->code == GOT_ERR_ANCESTRY) {
1892 error = checkout_ancestry_error(
1893 head_ref, commit_id_str);
1895 goto done;
1897 error = got_worktree_set_base_commit_id(worktree, repo,
1898 commit_id);
1899 free(commit_id);
1900 if (error)
1901 goto done;
1904 error = got_pathlist_append(&paths, "", NULL);
1905 if (error)
1906 goto done;
1907 cpa.worktree_path = worktree_path;
1908 cpa.had_base_commit_ref_error = 0;
1909 error = got_worktree_checkout_files(worktree, &paths, repo,
1910 checkout_progress, &cpa, check_cancelled, NULL);
1911 if (error != NULL)
1912 goto done;
1914 printf("Now shut up and hack\n");
1915 if (cpa.had_base_commit_ref_error)
1916 show_worktree_base_ref_warning();
1917 done:
1918 got_pathlist_free(&paths);
1919 free(commit_id_str);
1920 free(repo_path);
1921 free(worktree_path);
1922 return error;
1925 __dead static void
1926 usage_update(void)
1928 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1929 getprogname());
1930 exit(1);
1933 static const struct got_error *
1934 update_progress(void *arg, unsigned char status, const char *path)
1936 int *did_something = arg;
1938 if (status == GOT_STATUS_EXISTS ||
1939 status == GOT_STATUS_BASE_REF_ERR)
1940 return NULL;
1942 *did_something = 1;
1944 /* Base commit bump happens silently. */
1945 if (status == GOT_STATUS_BUMP_BASE)
1946 return NULL;
1948 while (path[0] == '/')
1949 path++;
1950 printf("%c %s\n", status, path);
1951 return NULL;
1954 static const struct got_error *
1955 switch_head_ref(struct got_reference *head_ref,
1956 struct got_object_id *commit_id, struct got_worktree *worktree,
1957 struct got_repository *repo)
1959 const struct got_error *err = NULL;
1960 char *base_id_str;
1961 int ref_has_moved = 0;
1963 /* Trivial case: switching between two different references. */
1964 if (strcmp(got_ref_get_name(head_ref),
1965 got_worktree_get_head_ref_name(worktree)) != 0) {
1966 printf("Switching work tree from %s to %s\n",
1967 got_worktree_get_head_ref_name(worktree),
1968 got_ref_get_name(head_ref));
1969 return got_worktree_set_head_ref(worktree, head_ref);
1972 err = check_linear_ancestry(commit_id,
1973 got_worktree_get_base_commit_id(worktree), 0, repo);
1974 if (err) {
1975 if (err->code != GOT_ERR_ANCESTRY)
1976 return err;
1977 ref_has_moved = 1;
1979 if (!ref_has_moved)
1980 return NULL;
1982 /* Switching to a rebased branch with the same reference name. */
1983 err = got_object_id_str(&base_id_str,
1984 got_worktree_get_base_commit_id(worktree));
1985 if (err)
1986 return err;
1987 printf("Reference %s now points at a different branch\n",
1988 got_worktree_get_head_ref_name(worktree));
1989 printf("Switching work tree from %s to %s\n", base_id_str,
1990 got_worktree_get_head_ref_name(worktree));
1991 return NULL;
1994 static const struct got_error *
1995 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1997 const struct got_error *err;
1998 int in_progress;
2000 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2001 if (err)
2002 return err;
2003 if (in_progress)
2004 return got_error(GOT_ERR_REBASING);
2006 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2007 if (err)
2008 return err;
2009 if (in_progress)
2010 return got_error(GOT_ERR_HISTEDIT_BUSY);
2012 return NULL;
2015 static const struct got_error *
2016 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2017 char *argv[], struct got_worktree *worktree)
2019 const struct got_error *err = NULL;
2020 char *path;
2021 int i;
2023 if (argc == 0) {
2024 path = strdup("");
2025 if (path == NULL)
2026 return got_error_from_errno("strdup");
2027 return got_pathlist_append(paths, path, NULL);
2030 for (i = 0; i < argc; i++) {
2031 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2032 if (err)
2033 break;
2034 err = got_pathlist_append(paths, path, NULL);
2035 if (err) {
2036 free(path);
2037 break;
2041 return err;
2044 static const struct got_error *
2045 cmd_update(int argc, char *argv[])
2047 const struct got_error *error = NULL;
2048 struct got_repository *repo = NULL;
2049 struct got_worktree *worktree = NULL;
2050 char *worktree_path = NULL;
2051 struct got_object_id *commit_id = NULL;
2052 char *commit_id_str = NULL;
2053 const char *branch_name = NULL;
2054 struct got_reference *head_ref = NULL;
2055 struct got_pathlist_head paths;
2056 struct got_pathlist_entry *pe;
2057 int ch, did_something = 0;
2059 TAILQ_INIT(&paths);
2061 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2062 switch (ch) {
2063 case 'b':
2064 branch_name = optarg;
2065 break;
2066 case 'c':
2067 commit_id_str = strdup(optarg);
2068 if (commit_id_str == NULL)
2069 return got_error_from_errno("strdup");
2070 break;
2071 default:
2072 usage_update();
2073 /* NOTREACHED */
2077 argc -= optind;
2078 argv += optind;
2080 #ifndef PROFILE
2081 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2082 "unveil", NULL) == -1)
2083 err(1, "pledge");
2084 #endif
2085 worktree_path = getcwd(NULL, 0);
2086 if (worktree_path == NULL) {
2087 error = got_error_from_errno("getcwd");
2088 goto done;
2090 error = got_worktree_open(&worktree, worktree_path);
2091 if (error)
2092 goto done;
2094 error = check_rebase_or_histedit_in_progress(worktree);
2095 if (error)
2096 goto done;
2098 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2099 NULL);
2100 if (error != NULL)
2101 goto done;
2103 error = apply_unveil(got_repo_get_path(repo), 0,
2104 got_worktree_get_root_path(worktree));
2105 if (error)
2106 goto done;
2108 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2109 if (error)
2110 goto done;
2112 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2113 got_worktree_get_head_ref_name(worktree), 0);
2114 if (error != NULL)
2115 goto done;
2116 if (commit_id_str == NULL) {
2117 error = got_ref_resolve(&commit_id, repo, head_ref);
2118 if (error != NULL)
2119 goto done;
2120 error = got_object_id_str(&commit_id_str, commit_id);
2121 if (error != NULL)
2122 goto done;
2123 } else {
2124 error = got_repo_match_object_id(&commit_id, NULL,
2125 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2126 free(commit_id_str);
2127 commit_id_str = NULL;
2128 if (error)
2129 goto done;
2130 error = got_object_id_str(&commit_id_str, commit_id);
2131 if (error)
2132 goto done;
2135 if (branch_name) {
2136 struct got_object_id *head_commit_id;
2137 TAILQ_FOREACH(pe, &paths, entry) {
2138 if (pe->path_len == 0)
2139 continue;
2140 error = got_error_msg(GOT_ERR_BAD_PATH,
2141 "switching between branches requires that "
2142 "the entire work tree gets updated");
2143 goto done;
2145 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2146 if (error)
2147 goto done;
2148 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2149 repo);
2150 free(head_commit_id);
2151 if (error != NULL)
2152 goto done;
2153 error = check_same_branch(commit_id, head_ref, NULL, repo);
2154 if (error)
2155 goto done;
2156 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2157 if (error)
2158 goto done;
2159 } else {
2160 error = check_linear_ancestry(commit_id,
2161 got_worktree_get_base_commit_id(worktree), 0, repo);
2162 if (error != NULL) {
2163 if (error->code == GOT_ERR_ANCESTRY)
2164 error = got_error(GOT_ERR_BRANCH_MOVED);
2165 goto done;
2167 error = check_same_branch(commit_id, head_ref, NULL, repo);
2168 if (error)
2169 goto done;
2172 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2173 commit_id) != 0) {
2174 error = got_worktree_set_base_commit_id(worktree, repo,
2175 commit_id);
2176 if (error)
2177 goto done;
2180 error = got_worktree_checkout_files(worktree, &paths, repo,
2181 update_progress, &did_something, check_cancelled, NULL);
2182 if (error != NULL)
2183 goto done;
2185 if (did_something)
2186 printf("Updated to commit %s\n", commit_id_str);
2187 else
2188 printf("Already up-to-date\n");
2189 done:
2190 free(worktree_path);
2191 TAILQ_FOREACH(pe, &paths, entry)
2192 free((char *)pe->path);
2193 got_pathlist_free(&paths);
2194 free(commit_id);
2195 free(commit_id_str);
2196 return error;
2199 static const struct got_error *
2200 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2201 const char *path, int diff_context, int ignore_whitespace,
2202 struct got_repository *repo)
2204 const struct got_error *err = NULL;
2205 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2207 if (blob_id1) {
2208 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2209 if (err)
2210 goto done;
2213 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2214 if (err)
2215 goto done;
2217 while (path[0] == '/')
2218 path++;
2219 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2220 ignore_whitespace, stdout);
2221 done:
2222 if (blob1)
2223 got_object_blob_close(blob1);
2224 got_object_blob_close(blob2);
2225 return err;
2228 static const struct got_error *
2229 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2230 const char *path, int diff_context, int ignore_whitespace,
2231 struct got_repository *repo)
2233 const struct got_error *err = NULL;
2234 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2235 struct got_diff_blob_output_unidiff_arg arg;
2237 if (tree_id1) {
2238 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2239 if (err)
2240 goto done;
2243 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2244 if (err)
2245 goto done;
2247 arg.diff_context = diff_context;
2248 arg.ignore_whitespace = ignore_whitespace;
2249 arg.outfile = stdout;
2250 while (path[0] == '/')
2251 path++;
2252 err = got_diff_tree(tree1, tree2, path, path, repo,
2253 got_diff_blob_output_unidiff, &arg, 1);
2254 done:
2255 if (tree1)
2256 got_object_tree_close(tree1);
2257 if (tree2)
2258 got_object_tree_close(tree2);
2259 return err;
2262 static const struct got_error *
2263 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2264 const char *path, int diff_context, struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 struct got_commit_object *pcommit = NULL;
2268 char *id_str1 = NULL, *id_str2 = NULL;
2269 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2270 struct got_object_qid *qid;
2272 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2273 if (qid != NULL) {
2274 err = got_object_open_as_commit(&pcommit, repo,
2275 qid->id);
2276 if (err)
2277 return err;
2280 if (path && path[0] != '\0') {
2281 int obj_type;
2282 err = got_object_id_by_path(&obj_id2, repo, id, path);
2283 if (err)
2284 goto done;
2285 err = got_object_id_str(&id_str2, obj_id2);
2286 if (err) {
2287 free(obj_id2);
2288 goto done;
2290 if (pcommit) {
2291 err = got_object_id_by_path(&obj_id1, repo,
2292 qid->id, path);
2293 if (err) {
2294 free(obj_id2);
2295 goto done;
2297 err = got_object_id_str(&id_str1, obj_id1);
2298 if (err) {
2299 free(obj_id2);
2300 goto done;
2303 err = got_object_get_type(&obj_type, repo, obj_id2);
2304 if (err) {
2305 free(obj_id2);
2306 goto done;
2308 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2309 switch (obj_type) {
2310 case GOT_OBJ_TYPE_BLOB:
2311 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2312 0, repo);
2313 break;
2314 case GOT_OBJ_TYPE_TREE:
2315 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2316 0, repo);
2317 break;
2318 default:
2319 err = got_error(GOT_ERR_OBJ_TYPE);
2320 break;
2322 free(obj_id1);
2323 free(obj_id2);
2324 } else {
2325 obj_id2 = got_object_commit_get_tree_id(commit);
2326 err = got_object_id_str(&id_str2, obj_id2);
2327 if (err)
2328 goto done;
2329 obj_id1 = got_object_commit_get_tree_id(pcommit);
2330 err = got_object_id_str(&id_str1, obj_id1);
2331 if (err)
2332 goto done;
2333 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2334 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2336 done:
2337 free(id_str1);
2338 free(id_str2);
2339 if (pcommit)
2340 got_object_commit_close(pcommit);
2341 return err;
2344 static char *
2345 get_datestr(time_t *time, char *datebuf)
2347 struct tm mytm, *tm;
2348 char *p, *s;
2350 tm = gmtime_r(time, &mytm);
2351 if (tm == NULL)
2352 return NULL;
2353 s = asctime_r(tm, datebuf);
2354 if (s == NULL)
2355 return NULL;
2356 p = strchr(s, '\n');
2357 if (p)
2358 *p = '\0';
2359 return s;
2362 static const struct got_error *
2363 match_logmsg(int *have_match, struct got_object_id *id,
2364 struct got_commit_object *commit, regex_t *regex)
2366 const struct got_error *err = NULL;
2367 regmatch_t regmatch;
2368 char *id_str = NULL, *logmsg = NULL;
2370 *have_match = 0;
2372 err = got_object_id_str(&id_str, id);
2373 if (err)
2374 return err;
2376 err = got_object_commit_get_logmsg(&logmsg, commit);
2377 if (err)
2378 goto done;
2380 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2381 *have_match = 1;
2382 done:
2383 free(id_str);
2384 free(logmsg);
2385 return err;
2388 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2390 static const struct got_error *
2391 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2392 struct got_repository *repo, const char *path, int show_patch,
2393 int diff_context, struct got_reflist_head *refs)
2395 const struct got_error *err = NULL;
2396 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2397 char datebuf[26];
2398 time_t committer_time;
2399 const char *author, *committer;
2400 char *refs_str = NULL;
2401 struct got_reflist_entry *re;
2403 SIMPLEQ_FOREACH(re, refs, entry) {
2404 char *s;
2405 const char *name;
2406 struct got_tag_object *tag = NULL;
2407 int cmp;
2409 name = got_ref_get_name(re->ref);
2410 if (strcmp(name, GOT_REF_HEAD) == 0)
2411 continue;
2412 if (strncmp(name, "refs/", 5) == 0)
2413 name += 5;
2414 if (strncmp(name, "got/", 4) == 0)
2415 continue;
2416 if (strncmp(name, "heads/", 6) == 0)
2417 name += 6;
2418 if (strncmp(name, "remotes/", 8) == 0)
2419 name += 8;
2420 if (strncmp(name, "tags/", 5) == 0) {
2421 err = got_object_open_as_tag(&tag, repo, re->id);
2422 if (err) {
2423 if (err->code != GOT_ERR_OBJ_TYPE)
2424 return err;
2425 /* Ref points at something other than a tag. */
2426 err = NULL;
2427 tag = NULL;
2430 cmp = got_object_id_cmp(tag ?
2431 got_object_tag_get_object_id(tag) : re->id, id);
2432 if (tag)
2433 got_object_tag_close(tag);
2434 if (cmp != 0)
2435 continue;
2436 s = refs_str;
2437 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2438 name) == -1) {
2439 err = got_error_from_errno("asprintf");
2440 free(s);
2441 return err;
2443 free(s);
2445 err = got_object_id_str(&id_str, id);
2446 if (err)
2447 return err;
2449 printf(GOT_COMMIT_SEP_STR);
2450 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2451 refs_str ? refs_str : "", refs_str ? ")" : "");
2452 free(id_str);
2453 id_str = NULL;
2454 free(refs_str);
2455 refs_str = NULL;
2456 printf("from: %s\n", got_object_commit_get_author(commit));
2457 committer_time = got_object_commit_get_committer_time(commit);
2458 datestr = get_datestr(&committer_time, datebuf);
2459 if (datestr)
2460 printf("date: %s UTC\n", datestr);
2461 author = got_object_commit_get_author(commit);
2462 committer = got_object_commit_get_committer(commit);
2463 if (strcmp(author, committer) != 0)
2464 printf("via: %s\n", committer);
2465 if (got_object_commit_get_nparents(commit) > 1) {
2466 const struct got_object_id_queue *parent_ids;
2467 struct got_object_qid *qid;
2468 int n = 1;
2469 parent_ids = got_object_commit_get_parent_ids(commit);
2470 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2471 err = got_object_id_str(&id_str, qid->id);
2472 if (err)
2473 return err;
2474 printf("parent %d: %s\n", n++, id_str);
2475 free(id_str);
2479 err = got_object_commit_get_logmsg(&logmsg0, commit);
2480 if (err)
2481 return err;
2483 logmsg = logmsg0;
2484 do {
2485 line = strsep(&logmsg, "\n");
2486 if (line)
2487 printf(" %s\n", line);
2488 } while (line);
2489 free(logmsg0);
2491 if (show_patch) {
2492 err = print_patch(commit, id, path, diff_context, repo);
2493 if (err == 0)
2494 printf("\n");
2497 if (fflush(stdout) != 0 && err == NULL)
2498 err = got_error_from_errno("fflush");
2499 return err;
2502 static const struct got_error *
2503 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2504 const char *path, int show_patch, const char *search_pattern,
2505 int diff_context, int limit, int log_branches,
2506 struct got_reflist_head *refs)
2508 const struct got_error *err;
2509 struct got_commit_graph *graph;
2510 regex_t regex;
2511 int have_match;
2513 if (search_pattern &&
2514 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2515 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2517 err = got_commit_graph_open(&graph, path, !log_branches);
2518 if (err)
2519 return err;
2520 err = got_commit_graph_iter_start(graph, root_id, repo,
2521 check_cancelled, NULL);
2522 if (err)
2523 goto done;
2524 for (;;) {
2525 struct got_commit_object *commit;
2526 struct got_object_id *id;
2528 if (sigint_received || sigpipe_received)
2529 break;
2531 err = got_commit_graph_iter_next(&id, graph, repo,
2532 check_cancelled, NULL);
2533 if (err) {
2534 if (err->code == GOT_ERR_ITER_COMPLETED)
2535 err = NULL;
2536 break;
2538 if (id == NULL)
2539 break;
2541 err = got_object_open_as_commit(&commit, repo, id);
2542 if (err)
2543 break;
2545 if (search_pattern) {
2546 err = match_logmsg(&have_match, id, commit, &regex);
2547 if (err) {
2548 got_object_commit_close(commit);
2549 break;
2551 if (have_match == 0) {
2552 got_object_commit_close(commit);
2553 continue;
2557 err = print_commit(commit, id, repo, path, show_patch,
2558 diff_context, refs);
2559 got_object_commit_close(commit);
2560 if (err || (limit && --limit == 0))
2561 break;
2563 done:
2564 if (search_pattern)
2565 regfree(&regex);
2566 got_commit_graph_close(graph);
2567 return err;
2570 __dead static void
2571 usage_log(void)
2573 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2574 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2575 exit(1);
2578 static int
2579 get_default_log_limit(void)
2581 const char *got_default_log_limit;
2582 long long n;
2583 const char *errstr;
2585 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2586 if (got_default_log_limit == NULL)
2587 return 0;
2588 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2589 if (errstr != NULL)
2590 return 0;
2591 return n;
2594 static const struct got_error *
2595 cmd_log(int argc, char *argv[])
2597 const struct got_error *error;
2598 struct got_repository *repo = NULL;
2599 struct got_worktree *worktree = NULL;
2600 struct got_commit_object *commit = NULL;
2601 struct got_object_id *id = NULL;
2602 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2603 const char *start_commit = NULL, *search_pattern = NULL;
2604 int diff_context = -1, ch;
2605 int show_patch = 0, limit = 0, log_branches = 0;
2606 const char *errstr;
2607 struct got_reflist_head refs;
2609 SIMPLEQ_INIT(&refs);
2611 #ifndef PROFILE
2612 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2613 NULL)
2614 == -1)
2615 err(1, "pledge");
2616 #endif
2618 limit = get_default_log_limit();
2620 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2621 switch (ch) {
2622 case 'p':
2623 show_patch = 1;
2624 break;
2625 case 'c':
2626 start_commit = optarg;
2627 break;
2628 case 'C':
2629 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2630 &errstr);
2631 if (errstr != NULL)
2632 err(1, "-C option %s", errstr);
2633 break;
2634 case 'l':
2635 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2636 if (errstr != NULL)
2637 err(1, "-l option %s", errstr);
2638 break;
2639 case 'b':
2640 log_branches = 1;
2641 break;
2642 case 'r':
2643 repo_path = realpath(optarg, NULL);
2644 if (repo_path == NULL)
2645 return got_error_from_errno2("realpath",
2646 optarg);
2647 got_path_strip_trailing_slashes(repo_path);
2648 break;
2649 case 's':
2650 search_pattern = optarg;
2651 break;
2652 default:
2653 usage_log();
2654 /* NOTREACHED */
2658 argc -= optind;
2659 argv += optind;
2661 if (diff_context == -1)
2662 diff_context = 3;
2663 else if (!show_patch)
2664 errx(1, "-C reguires -p");
2666 cwd = getcwd(NULL, 0);
2667 if (cwd == NULL) {
2668 error = got_error_from_errno("getcwd");
2669 goto done;
2672 error = got_worktree_open(&worktree, cwd);
2673 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2674 goto done;
2675 error = NULL;
2677 if (argc == 0) {
2678 path = strdup("");
2679 if (path == NULL) {
2680 error = got_error_from_errno("strdup");
2681 goto done;
2683 } else if (argc == 1) {
2684 if (worktree) {
2685 error = got_worktree_resolve_path(&path, worktree,
2686 argv[0]);
2687 if (error)
2688 goto done;
2689 } else {
2690 path = strdup(argv[0]);
2691 if (path == NULL) {
2692 error = got_error_from_errno("strdup");
2693 goto done;
2696 } else
2697 usage_log();
2699 if (repo_path == NULL) {
2700 repo_path = worktree ?
2701 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2703 if (repo_path == NULL) {
2704 error = got_error_from_errno("strdup");
2705 goto done;
2708 error = got_repo_open(&repo, repo_path, NULL);
2709 if (error != NULL)
2710 goto done;
2712 error = apply_unveil(got_repo_get_path(repo), 1,
2713 worktree ? got_worktree_get_root_path(worktree) : NULL);
2714 if (error)
2715 goto done;
2717 if (start_commit == NULL) {
2718 struct got_reference *head_ref;
2719 error = got_ref_open(&head_ref, repo,
2720 worktree ? got_worktree_get_head_ref_name(worktree)
2721 : GOT_REF_HEAD, 0);
2722 if (error != NULL)
2723 return error;
2724 error = got_ref_resolve(&id, repo, head_ref);
2725 got_ref_close(head_ref);
2726 if (error != NULL)
2727 return error;
2728 error = got_object_open_as_commit(&commit, repo, id);
2729 } else {
2730 struct got_reference *ref;
2731 error = got_ref_open(&ref, repo, start_commit, 0);
2732 if (error == NULL) {
2733 int obj_type;
2734 error = got_ref_resolve(&id, repo, ref);
2735 got_ref_close(ref);
2736 if (error != NULL)
2737 goto done;
2738 error = got_object_get_type(&obj_type, repo, id);
2739 if (error != NULL)
2740 goto done;
2741 if (obj_type == GOT_OBJ_TYPE_TAG) {
2742 struct got_tag_object *tag;
2743 error = got_object_open_as_tag(&tag, repo, id);
2744 if (error != NULL)
2745 goto done;
2746 if (got_object_tag_get_object_type(tag) !=
2747 GOT_OBJ_TYPE_COMMIT) {
2748 got_object_tag_close(tag);
2749 error = got_error(GOT_ERR_OBJ_TYPE);
2750 goto done;
2752 free(id);
2753 id = got_object_id_dup(
2754 got_object_tag_get_object_id(tag));
2755 if (id == NULL)
2756 error = got_error_from_errno(
2757 "got_object_id_dup");
2758 got_object_tag_close(tag);
2759 if (error)
2760 goto done;
2761 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2762 error = got_error(GOT_ERR_OBJ_TYPE);
2763 goto done;
2765 error = got_object_open_as_commit(&commit, repo, id);
2766 if (error != NULL)
2767 goto done;
2769 if (commit == NULL) {
2770 error = got_repo_match_object_id_prefix(&id,
2771 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2772 if (error != NULL)
2773 return error;
2776 if (error != NULL)
2777 goto done;
2779 if (worktree) {
2780 const char *prefix = got_worktree_get_path_prefix(worktree);
2781 char *p;
2782 if (asprintf(&p, "%s%s%s", prefix,
2783 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2784 error = got_error_from_errno("asprintf");
2785 goto done;
2787 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2788 free(p);
2789 } else
2790 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2791 if (error != NULL)
2792 goto done;
2793 if (in_repo_path) {
2794 free(path);
2795 path = in_repo_path;
2798 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2799 if (error)
2800 goto done;
2802 error = print_commits(id, repo, path, show_patch, search_pattern,
2803 diff_context, limit, log_branches, &refs);
2804 done:
2805 free(path);
2806 free(repo_path);
2807 free(cwd);
2808 free(id);
2809 if (worktree)
2810 got_worktree_close(worktree);
2811 if (repo) {
2812 const struct got_error *repo_error;
2813 repo_error = got_repo_close(repo);
2814 if (error == NULL)
2815 error = repo_error;
2817 got_ref_list_free(&refs);
2818 return error;
2821 __dead static void
2822 usage_diff(void)
2824 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2825 "[-w] [object1 object2 | path]\n", getprogname());
2826 exit(1);
2829 struct print_diff_arg {
2830 struct got_repository *repo;
2831 struct got_worktree *worktree;
2832 int diff_context;
2833 const char *id_str;
2834 int header_shown;
2835 int diff_staged;
2836 int ignore_whitespace;
2839 static const struct got_error *
2840 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2841 const char *path, struct got_object_id *blob_id,
2842 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2843 int dirfd, const char *de_name)
2845 struct print_diff_arg *a = arg;
2846 const struct got_error *err = NULL;
2847 struct got_blob_object *blob1 = NULL;
2848 int fd = -1;
2849 FILE *f2 = NULL;
2850 char *abspath = NULL, *label1 = NULL;
2851 struct stat sb;
2853 if (a->diff_staged) {
2854 if (staged_status != GOT_STATUS_MODIFY &&
2855 staged_status != GOT_STATUS_ADD &&
2856 staged_status != GOT_STATUS_DELETE)
2857 return NULL;
2858 } else {
2859 if (staged_status == GOT_STATUS_DELETE)
2860 return NULL;
2861 if (status == GOT_STATUS_NONEXISTENT)
2862 return got_error_set_errno(ENOENT, path);
2863 if (status != GOT_STATUS_MODIFY &&
2864 status != GOT_STATUS_ADD &&
2865 status != GOT_STATUS_DELETE &&
2866 status != GOT_STATUS_CONFLICT)
2867 return NULL;
2870 if (!a->header_shown) {
2871 printf("diff %s %s%s\n", a->id_str,
2872 got_worktree_get_root_path(a->worktree),
2873 a->diff_staged ? " (staged changes)" : "");
2874 a->header_shown = 1;
2877 if (a->diff_staged) {
2878 const char *label1 = NULL, *label2 = NULL;
2879 switch (staged_status) {
2880 case GOT_STATUS_MODIFY:
2881 label1 = path;
2882 label2 = path;
2883 break;
2884 case GOT_STATUS_ADD:
2885 label2 = path;
2886 break;
2887 case GOT_STATUS_DELETE:
2888 label1 = path;
2889 break;
2890 default:
2891 return got_error(GOT_ERR_FILE_STATUS);
2893 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2894 label1, label2, a->diff_context, a->ignore_whitespace,
2895 a->repo, stdout);
2898 if (staged_status == GOT_STATUS_ADD ||
2899 staged_status == GOT_STATUS_MODIFY) {
2900 char *id_str;
2901 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2902 8192);
2903 if (err)
2904 goto done;
2905 err = got_object_id_str(&id_str, staged_blob_id);
2906 if (err)
2907 goto done;
2908 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2909 err = got_error_from_errno("asprintf");
2910 free(id_str);
2911 goto done;
2913 free(id_str);
2914 } else if (status != GOT_STATUS_ADD) {
2915 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2916 if (err)
2917 goto done;
2920 if (status != GOT_STATUS_DELETE) {
2921 if (asprintf(&abspath, "%s/%s",
2922 got_worktree_get_root_path(a->worktree), path) == -1) {
2923 err = got_error_from_errno("asprintf");
2924 goto done;
2927 if (dirfd != -1) {
2928 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2929 if (fd == -1) {
2930 err = got_error_from_errno2("openat", abspath);
2931 goto done;
2933 } else {
2934 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2935 if (fd == -1) {
2936 err = got_error_from_errno2("open", abspath);
2937 goto done;
2940 if (fstat(fd, &sb) == -1) {
2941 err = got_error_from_errno2("fstat", abspath);
2942 goto done;
2944 f2 = fdopen(fd, "r");
2945 if (f2 == NULL) {
2946 err = got_error_from_errno2("fdopen", abspath);
2947 goto done;
2949 fd = -1;
2950 } else
2951 sb.st_size = 0;
2953 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2954 a->diff_context, a->ignore_whitespace, stdout);
2955 done:
2956 if (blob1)
2957 got_object_blob_close(blob1);
2958 if (f2 && fclose(f2) == EOF && err == NULL)
2959 err = got_error_from_errno("fclose");
2960 if (fd != -1 && close(fd) == -1 && err == NULL)
2961 err = got_error_from_errno("close");
2962 free(abspath);
2963 return err;
2966 static const struct got_error *
2967 cmd_diff(int argc, char *argv[])
2969 const struct got_error *error;
2970 struct got_repository *repo = NULL;
2971 struct got_worktree *worktree = NULL;
2972 char *cwd = NULL, *repo_path = NULL;
2973 struct got_object_id *id1 = NULL, *id2 = NULL;
2974 const char *id_str1 = NULL, *id_str2 = NULL;
2975 char *label1 = NULL, *label2 = NULL;
2976 int type1, type2;
2977 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2978 const char *errstr;
2979 char *path = NULL;
2981 #ifndef PROFILE
2982 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2983 NULL) == -1)
2984 err(1, "pledge");
2985 #endif
2987 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2988 switch (ch) {
2989 case 'C':
2990 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2991 &errstr);
2992 if (errstr != NULL)
2993 err(1, "-C option %s", errstr);
2994 break;
2995 case 'r':
2996 repo_path = realpath(optarg, NULL);
2997 if (repo_path == NULL)
2998 return got_error_from_errno2("realpath",
2999 optarg);
3000 got_path_strip_trailing_slashes(repo_path);
3001 break;
3002 case 's':
3003 diff_staged = 1;
3004 break;
3005 case 'w':
3006 ignore_whitespace = 1;
3007 break;
3008 default:
3009 usage_diff();
3010 /* NOTREACHED */
3014 argc -= optind;
3015 argv += optind;
3017 cwd = getcwd(NULL, 0);
3018 if (cwd == NULL) {
3019 error = got_error_from_errno("getcwd");
3020 goto done;
3022 error = got_worktree_open(&worktree, cwd);
3023 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3024 goto done;
3025 if (argc <= 1) {
3026 if (worktree == NULL) {
3027 error = got_error(GOT_ERR_NOT_WORKTREE);
3028 goto done;
3030 if (repo_path)
3031 errx(1,
3032 "-r option can't be used when diffing a work tree");
3033 repo_path = strdup(got_worktree_get_repo_path(worktree));
3034 if (repo_path == NULL) {
3035 error = got_error_from_errno("strdup");
3036 goto done;
3038 if (argc == 1) {
3039 error = got_worktree_resolve_path(&path, worktree,
3040 argv[0]);
3041 if (error)
3042 goto done;
3043 } else {
3044 path = strdup("");
3045 if (path == NULL) {
3046 error = got_error_from_errno("strdup");
3047 goto done;
3050 } else if (argc == 2) {
3051 if (diff_staged)
3052 errx(1, "-s option can't be used when diffing "
3053 "objects in repository");
3054 id_str1 = argv[0];
3055 id_str2 = argv[1];
3056 if (worktree && repo_path == NULL) {
3057 repo_path =
3058 strdup(got_worktree_get_repo_path(worktree));
3059 if (repo_path == NULL) {
3060 error = got_error_from_errno("strdup");
3061 goto done;
3064 } else
3065 usage_diff();
3067 if (repo_path == NULL) {
3068 repo_path = getcwd(NULL, 0);
3069 if (repo_path == NULL)
3070 return got_error_from_errno("getcwd");
3073 error = got_repo_open(&repo, repo_path, NULL);
3074 free(repo_path);
3075 if (error != NULL)
3076 goto done;
3078 error = apply_unveil(got_repo_get_path(repo), 1,
3079 worktree ? got_worktree_get_root_path(worktree) : NULL);
3080 if (error)
3081 goto done;
3083 if (argc <= 1) {
3084 struct print_diff_arg arg;
3085 struct got_pathlist_head paths;
3086 char *id_str;
3088 TAILQ_INIT(&paths);
3090 error = got_object_id_str(&id_str,
3091 got_worktree_get_base_commit_id(worktree));
3092 if (error)
3093 goto done;
3094 arg.repo = repo;
3095 arg.worktree = worktree;
3096 arg.diff_context = diff_context;
3097 arg.id_str = id_str;
3098 arg.header_shown = 0;
3099 arg.diff_staged = diff_staged;
3100 arg.ignore_whitespace = ignore_whitespace;
3102 error = got_pathlist_append(&paths, path, NULL);
3103 if (error)
3104 goto done;
3106 error = got_worktree_status(worktree, &paths, repo, print_diff,
3107 &arg, check_cancelled, NULL);
3108 free(id_str);
3109 got_pathlist_free(&paths);
3110 goto done;
3113 error = got_repo_match_object_id(&id1, &label1, id_str1,
3114 GOT_OBJ_TYPE_ANY, 1, repo);
3115 if (error)
3116 goto done;
3118 error = got_repo_match_object_id(&id2, &label2, id_str2,
3119 GOT_OBJ_TYPE_ANY, 1, repo);
3120 if (error)
3121 goto done;
3123 error = got_object_get_type(&type1, repo, id1);
3124 if (error)
3125 goto done;
3127 error = got_object_get_type(&type2, repo, id2);
3128 if (error)
3129 goto done;
3131 if (type1 != type2) {
3132 error = got_error(GOT_ERR_OBJ_TYPE);
3133 goto done;
3136 switch (type1) {
3137 case GOT_OBJ_TYPE_BLOB:
3138 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3139 diff_context, ignore_whitespace, repo, stdout);
3140 break;
3141 case GOT_OBJ_TYPE_TREE:
3142 error = got_diff_objects_as_trees(id1, id2, "", "",
3143 diff_context, ignore_whitespace, repo, stdout);
3144 break;
3145 case GOT_OBJ_TYPE_COMMIT:
3146 printf("diff %s %s\n", label1, label2);
3147 error = got_diff_objects_as_commits(id1, id2, diff_context,
3148 ignore_whitespace, repo, stdout);
3149 break;
3150 default:
3151 error = got_error(GOT_ERR_OBJ_TYPE);
3153 done:
3154 free(label1);
3155 free(label2);
3156 free(id1);
3157 free(id2);
3158 free(path);
3159 if (worktree)
3160 got_worktree_close(worktree);
3161 if (repo) {
3162 const struct got_error *repo_error;
3163 repo_error = got_repo_close(repo);
3164 if (error == NULL)
3165 error = repo_error;
3167 return error;
3170 __dead static void
3171 usage_blame(void)
3173 fprintf(stderr,
3174 "usage: %s blame [-c commit] [-r repository-path] path\n",
3175 getprogname());
3176 exit(1);
3179 struct blame_line {
3180 int annotated;
3181 char *id_str;
3182 char *committer;
3183 char datebuf[11]; /* YYYY-MM-DD + NUL */
3186 struct blame_cb_args {
3187 struct blame_line *lines;
3188 int nlines;
3189 int nlines_prec;
3190 int lineno_cur;
3191 off_t *line_offsets;
3192 FILE *f;
3193 struct got_repository *repo;
3196 static const struct got_error *
3197 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3199 const struct got_error *err = NULL;
3200 struct blame_cb_args *a = arg;
3201 struct blame_line *bline;
3202 char *line = NULL;
3203 size_t linesize = 0;
3204 struct got_commit_object *commit = NULL;
3205 off_t offset;
3206 struct tm tm;
3207 time_t committer_time;
3209 if (nlines != a->nlines ||
3210 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3211 return got_error(GOT_ERR_RANGE);
3213 if (sigint_received)
3214 return got_error(GOT_ERR_ITER_COMPLETED);
3216 if (lineno == -1)
3217 return NULL; /* no change in this commit */
3219 /* Annotate this line. */
3220 bline = &a->lines[lineno - 1];
3221 if (bline->annotated)
3222 return NULL;
3223 err = got_object_id_str(&bline->id_str, id);
3224 if (err)
3225 return err;
3227 err = got_object_open_as_commit(&commit, a->repo, id);
3228 if (err)
3229 goto done;
3231 bline->committer = strdup(got_object_commit_get_committer(commit));
3232 if (bline->committer == NULL) {
3233 err = got_error_from_errno("strdup");
3234 goto done;
3237 committer_time = got_object_commit_get_committer_time(commit);
3238 if (localtime_r(&committer_time, &tm) == NULL)
3239 return got_error_from_errno("localtime_r");
3240 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3241 &tm) >= sizeof(bline->datebuf)) {
3242 err = got_error(GOT_ERR_NO_SPACE);
3243 goto done;
3245 bline->annotated = 1;
3247 /* Print lines annotated so far. */
3248 bline = &a->lines[a->lineno_cur - 1];
3249 if (!bline->annotated)
3250 goto done;
3252 offset = a->line_offsets[a->lineno_cur - 1];
3253 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3254 err = got_error_from_errno("fseeko");
3255 goto done;
3258 while (bline->annotated) {
3259 char *smallerthan, *at, *nl, *committer;
3260 size_t len;
3262 if (getline(&line, &linesize, a->f) == -1) {
3263 if (ferror(a->f))
3264 err = got_error_from_errno("getline");
3265 break;
3268 committer = bline->committer;
3269 smallerthan = strchr(committer, '<');
3270 if (smallerthan && smallerthan[1] != '\0')
3271 committer = smallerthan + 1;
3272 at = strchr(committer, '@');
3273 if (at)
3274 *at = '\0';
3275 len = strlen(committer);
3276 if (len >= 9)
3277 committer[8] = '\0';
3279 nl = strchr(line, '\n');
3280 if (nl)
3281 *nl = '\0';
3282 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3283 bline->id_str, bline->datebuf, committer, line);
3285 a->lineno_cur++;
3286 bline = &a->lines[a->lineno_cur - 1];
3288 done:
3289 if (commit)
3290 got_object_commit_close(commit);
3291 free(line);
3292 return err;
3295 static const struct got_error *
3296 cmd_blame(int argc, char *argv[])
3298 const struct got_error *error;
3299 struct got_repository *repo = NULL;
3300 struct got_worktree *worktree = NULL;
3301 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3302 struct got_object_id *obj_id = NULL;
3303 struct got_object_id *commit_id = NULL;
3304 struct got_blob_object *blob = NULL;
3305 char *commit_id_str = NULL;
3306 struct blame_cb_args bca;
3307 int ch, obj_type, i;
3308 size_t filesize;
3310 memset(&bca, 0, sizeof(bca));
3312 #ifndef PROFILE
3313 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3314 NULL) == -1)
3315 err(1, "pledge");
3316 #endif
3318 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3319 switch (ch) {
3320 case 'c':
3321 commit_id_str = optarg;
3322 break;
3323 case 'r':
3324 repo_path = realpath(optarg, NULL);
3325 if (repo_path == NULL)
3326 return got_error_from_errno2("realpath",
3327 optarg);
3328 got_path_strip_trailing_slashes(repo_path);
3329 break;
3330 default:
3331 usage_blame();
3332 /* NOTREACHED */
3336 argc -= optind;
3337 argv += optind;
3339 if (argc == 1)
3340 path = argv[0];
3341 else
3342 usage_blame();
3344 cwd = getcwd(NULL, 0);
3345 if (cwd == NULL) {
3346 error = got_error_from_errno("getcwd");
3347 goto done;
3349 if (repo_path == NULL) {
3350 error = got_worktree_open(&worktree, cwd);
3351 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3352 goto done;
3353 else
3354 error = NULL;
3355 if (worktree) {
3356 repo_path =
3357 strdup(got_worktree_get_repo_path(worktree));
3358 if (repo_path == NULL)
3359 error = got_error_from_errno("strdup");
3360 if (error)
3361 goto done;
3362 } else {
3363 repo_path = strdup(cwd);
3364 if (repo_path == NULL) {
3365 error = got_error_from_errno("strdup");
3366 goto done;
3371 error = got_repo_open(&repo, repo_path, NULL);
3372 if (error != NULL)
3373 goto done;
3375 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3376 if (error)
3377 goto done;
3379 if (worktree) {
3380 const char *prefix = got_worktree_get_path_prefix(worktree);
3381 char *p, *worktree_subdir = cwd +
3382 strlen(got_worktree_get_root_path(worktree));
3383 if (asprintf(&p, "%s%s%s%s%s",
3384 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3385 worktree_subdir, worktree_subdir[0] ? "/" : "",
3386 path) == -1) {
3387 error = got_error_from_errno("asprintf");
3388 goto done;
3390 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3391 free(p);
3392 } else {
3393 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3395 if (error)
3396 goto done;
3398 if (commit_id_str == NULL) {
3399 struct got_reference *head_ref;
3400 error = got_ref_open(&head_ref, repo, worktree ?
3401 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3402 if (error != NULL)
3403 goto done;
3404 error = got_ref_resolve(&commit_id, repo, head_ref);
3405 got_ref_close(head_ref);
3406 if (error != NULL)
3407 goto done;
3408 } else {
3409 error = got_repo_match_object_id(&commit_id, NULL,
3410 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3411 if (error)
3412 goto done;
3415 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3416 if (error)
3417 goto done;
3419 error = got_object_get_type(&obj_type, repo, obj_id);
3420 if (error)
3421 goto done;
3423 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3424 error = got_error(GOT_ERR_OBJ_TYPE);
3425 goto done;
3428 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3429 if (error)
3430 goto done;
3431 bca.f = got_opentemp();
3432 if (bca.f == NULL) {
3433 error = got_error_from_errno("got_opentemp");
3434 goto done;
3436 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3437 &bca.line_offsets, bca.f, blob);
3438 if (error || bca.nlines == 0)
3439 goto done;
3441 /* Don't include \n at EOF in the blame line count. */
3442 if (bca.line_offsets[bca.nlines - 1] == filesize)
3443 bca.nlines--;
3445 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3446 if (bca.lines == NULL) {
3447 error = got_error_from_errno("calloc");
3448 goto done;
3450 bca.lineno_cur = 1;
3451 bca.nlines_prec = 0;
3452 i = bca.nlines;
3453 while (i > 0) {
3454 i /= 10;
3455 bca.nlines_prec++;
3457 bca.repo = repo;
3459 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3460 check_cancelled, NULL);
3461 done:
3462 free(in_repo_path);
3463 free(repo_path);
3464 free(cwd);
3465 free(commit_id);
3466 free(obj_id);
3467 if (blob)
3468 got_object_blob_close(blob);
3469 if (worktree)
3470 got_worktree_close(worktree);
3471 if (repo) {
3472 const struct got_error *repo_error;
3473 repo_error = got_repo_close(repo);
3474 if (error == NULL)
3475 error = repo_error;
3477 if (bca.lines) {
3478 for (i = 0; i < bca.nlines; i++) {
3479 struct blame_line *bline = &bca.lines[i];
3480 free(bline->id_str);
3481 free(bline->committer);
3483 free(bca.lines);
3485 free(bca.line_offsets);
3486 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3487 error = got_error_from_errno("fclose");
3488 return error;
3491 __dead static void
3492 usage_tree(void)
3494 fprintf(stderr,
3495 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3496 getprogname());
3497 exit(1);
3500 static void
3501 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3502 const char *root_path)
3504 int is_root_path = (strcmp(path, root_path) == 0);
3505 const char *modestr = "";
3506 mode_t mode = got_tree_entry_get_mode(te);
3508 path += strlen(root_path);
3509 while (path[0] == '/')
3510 path++;
3512 if (got_object_tree_entry_is_submodule(te))
3513 modestr = "$";
3514 else if (S_ISLNK(mode))
3515 modestr = "@";
3516 else if (S_ISDIR(mode))
3517 modestr = "/";
3518 else if (mode & S_IXUSR)
3519 modestr = "*";
3521 printf("%s%s%s%s%s\n", id ? id : "", path,
3522 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3525 static const struct got_error *
3526 print_tree(const char *path, struct got_object_id *commit_id,
3527 int show_ids, int recurse, const char *root_path,
3528 struct got_repository *repo)
3530 const struct got_error *err = NULL;
3531 struct got_object_id *tree_id = NULL;
3532 struct got_tree_object *tree = NULL;
3533 int nentries, i;
3535 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3536 if (err)
3537 goto done;
3539 err = got_object_open_as_tree(&tree, repo, tree_id);
3540 if (err)
3541 goto done;
3542 nentries = got_object_tree_get_nentries(tree);
3543 for (i = 0; i < nentries; i++) {
3544 struct got_tree_entry *te;
3545 char *id = NULL;
3547 if (sigint_received || sigpipe_received)
3548 break;
3550 te = got_object_tree_get_entry(tree, i);
3551 if (show_ids) {
3552 char *id_str;
3553 err = got_object_id_str(&id_str,
3554 got_tree_entry_get_id(te));
3555 if (err)
3556 goto done;
3557 if (asprintf(&id, "%s ", id_str) == -1) {
3558 err = got_error_from_errno("asprintf");
3559 free(id_str);
3560 goto done;
3562 free(id_str);
3564 print_entry(te, id, path, root_path);
3565 free(id);
3567 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3568 char *child_path;
3569 if (asprintf(&child_path, "%s%s%s", path,
3570 path[0] == '/' && path[1] == '\0' ? "" : "/",
3571 got_tree_entry_get_name(te)) == -1) {
3572 err = got_error_from_errno("asprintf");
3573 goto done;
3575 err = print_tree(child_path, commit_id, show_ids, 1,
3576 root_path, repo);
3577 free(child_path);
3578 if (err)
3579 goto done;
3582 done:
3583 if (tree)
3584 got_object_tree_close(tree);
3585 free(tree_id);
3586 return err;
3589 static const struct got_error *
3590 cmd_tree(int argc, char *argv[])
3592 const struct got_error *error;
3593 struct got_repository *repo = NULL;
3594 struct got_worktree *worktree = NULL;
3595 const char *path;
3596 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3597 struct got_object_id *commit_id = NULL;
3598 char *commit_id_str = NULL;
3599 int show_ids = 0, recurse = 0;
3600 int ch;
3602 #ifndef PROFILE
3603 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3604 NULL) == -1)
3605 err(1, "pledge");
3606 #endif
3608 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3609 switch (ch) {
3610 case 'c':
3611 commit_id_str = optarg;
3612 break;
3613 case 'r':
3614 repo_path = realpath(optarg, NULL);
3615 if (repo_path == NULL)
3616 return got_error_from_errno2("realpath",
3617 optarg);
3618 got_path_strip_trailing_slashes(repo_path);
3619 break;
3620 case 'i':
3621 show_ids = 1;
3622 break;
3623 case 'R':
3624 recurse = 1;
3625 break;
3626 default:
3627 usage_tree();
3628 /* NOTREACHED */
3632 argc -= optind;
3633 argv += optind;
3635 if (argc == 1)
3636 path = argv[0];
3637 else if (argc > 1)
3638 usage_tree();
3639 else
3640 path = NULL;
3642 cwd = getcwd(NULL, 0);
3643 if (cwd == NULL) {
3644 error = got_error_from_errno("getcwd");
3645 goto done;
3647 if (repo_path == NULL) {
3648 error = got_worktree_open(&worktree, cwd);
3649 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3650 goto done;
3651 else
3652 error = NULL;
3653 if (worktree) {
3654 repo_path =
3655 strdup(got_worktree_get_repo_path(worktree));
3656 if (repo_path == NULL)
3657 error = got_error_from_errno("strdup");
3658 if (error)
3659 goto done;
3660 } else {
3661 repo_path = strdup(cwd);
3662 if (repo_path == NULL) {
3663 error = got_error_from_errno("strdup");
3664 goto done;
3669 error = got_repo_open(&repo, repo_path, NULL);
3670 if (error != NULL)
3671 goto done;
3673 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3674 if (error)
3675 goto done;
3677 if (path == NULL) {
3678 if (worktree) {
3679 char *p, *worktree_subdir = cwd +
3680 strlen(got_worktree_get_root_path(worktree));
3681 if (asprintf(&p, "%s/%s",
3682 got_worktree_get_path_prefix(worktree),
3683 worktree_subdir) == -1) {
3684 error = got_error_from_errno("asprintf");
3685 goto done;
3687 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3688 free(p);
3689 if (error)
3690 goto done;
3691 } else
3692 path = "/";
3694 if (in_repo_path == NULL) {
3695 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3696 if (error != NULL)
3697 goto done;
3700 if (commit_id_str == NULL) {
3701 struct got_reference *head_ref;
3702 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3703 if (error != NULL)
3704 goto done;
3705 error = got_ref_resolve(&commit_id, repo, head_ref);
3706 got_ref_close(head_ref);
3707 if (error != NULL)
3708 goto done;
3709 } else {
3710 error = got_repo_match_object_id(&commit_id, NULL,
3711 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3712 if (error)
3713 goto done;
3716 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3717 in_repo_path, repo);
3718 done:
3719 free(in_repo_path);
3720 free(repo_path);
3721 free(cwd);
3722 free(commit_id);
3723 if (worktree)
3724 got_worktree_close(worktree);
3725 if (repo) {
3726 const struct got_error *repo_error;
3727 repo_error = got_repo_close(repo);
3728 if (error == NULL)
3729 error = repo_error;
3731 return error;
3734 __dead static void
3735 usage_status(void)
3737 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3738 exit(1);
3741 static const struct got_error *
3742 print_status(void *arg, unsigned char status, unsigned char staged_status,
3743 const char *path, struct got_object_id *blob_id,
3744 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3745 int dirfd, const char *de_name)
3747 if (status == staged_status && (status == GOT_STATUS_DELETE))
3748 status = GOT_STATUS_NO_CHANGE;
3749 printf("%c%c %s\n", status, staged_status, path);
3750 return NULL;
3753 static const struct got_error *
3754 cmd_status(int argc, char *argv[])
3756 const struct got_error *error = NULL;
3757 struct got_repository *repo = NULL;
3758 struct got_worktree *worktree = NULL;
3759 char *cwd = NULL;
3760 struct got_pathlist_head paths;
3761 struct got_pathlist_entry *pe;
3762 int ch;
3764 TAILQ_INIT(&paths);
3766 while ((ch = getopt(argc, argv, "")) != -1) {
3767 switch (ch) {
3768 default:
3769 usage_status();
3770 /* NOTREACHED */
3774 argc -= optind;
3775 argv += optind;
3777 #ifndef PROFILE
3778 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3779 NULL) == -1)
3780 err(1, "pledge");
3781 #endif
3782 cwd = getcwd(NULL, 0);
3783 if (cwd == NULL) {
3784 error = got_error_from_errno("getcwd");
3785 goto done;
3788 error = got_worktree_open(&worktree, cwd);
3789 if (error != NULL)
3790 goto done;
3792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3793 NULL);
3794 if (error != NULL)
3795 goto done;
3797 error = apply_unveil(got_repo_get_path(repo), 1,
3798 got_worktree_get_root_path(worktree));
3799 if (error)
3800 goto done;
3802 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3803 if (error)
3804 goto done;
3806 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3807 check_cancelled, NULL);
3808 done:
3809 TAILQ_FOREACH(pe, &paths, entry)
3810 free((char *)pe->path);
3811 got_pathlist_free(&paths);
3812 free(cwd);
3813 return error;
3816 __dead static void
3817 usage_ref(void)
3819 fprintf(stderr,
3820 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3821 getprogname());
3822 exit(1);
3825 static const struct got_error *
3826 list_refs(struct got_repository *repo)
3828 static const struct got_error *err = NULL;
3829 struct got_reflist_head refs;
3830 struct got_reflist_entry *re;
3832 SIMPLEQ_INIT(&refs);
3833 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3834 if (err)
3835 return err;
3837 SIMPLEQ_FOREACH(re, &refs, entry) {
3838 char *refstr;
3839 refstr = got_ref_to_str(re->ref);
3840 if (refstr == NULL)
3841 return got_error_from_errno("got_ref_to_str");
3842 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3843 free(refstr);
3846 got_ref_list_free(&refs);
3847 return NULL;
3850 static const struct got_error *
3851 delete_ref(struct got_repository *repo, const char *refname)
3853 const struct got_error *err = NULL;
3854 struct got_reference *ref;
3856 err = got_ref_open(&ref, repo, refname, 0);
3857 if (err)
3858 return err;
3860 err = got_ref_delete(ref, repo);
3861 got_ref_close(ref);
3862 return err;
3865 static const struct got_error *
3866 add_ref(struct got_repository *repo, const char *refname, const char *target)
3868 const struct got_error *err = NULL;
3869 struct got_object_id *id;
3870 struct got_reference *ref = NULL;
3873 * Don't let the user create a reference name with a leading '-'.
3874 * While technically a valid reference name, this case is usually
3875 * an unintended typo.
3877 if (refname[0] == '-')
3878 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3880 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3881 repo);
3882 if (err) {
3883 struct got_reference *target_ref;
3885 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3886 return err;
3887 err = got_ref_open(&target_ref, repo, target, 0);
3888 if (err)
3889 return err;
3890 err = got_ref_resolve(&id, repo, target_ref);
3891 got_ref_close(target_ref);
3892 if (err)
3893 return err;
3896 err = got_ref_alloc(&ref, refname, id);
3897 if (err)
3898 goto done;
3900 err = got_ref_write(ref, repo);
3901 done:
3902 if (ref)
3903 got_ref_close(ref);
3904 free(id);
3905 return err;
3908 static const struct got_error *
3909 add_symref(struct got_repository *repo, const char *refname, const char *target)
3911 const struct got_error *err = NULL;
3912 struct got_reference *ref = NULL;
3913 struct got_reference *target_ref = NULL;
3916 * Don't let the user create a reference name with a leading '-'.
3917 * While technically a valid reference name, this case is usually
3918 * an unintended typo.
3920 if (refname[0] == '-')
3921 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3923 err = got_ref_open(&target_ref, repo, target, 0);
3924 if (err)
3925 return err;
3927 err = got_ref_alloc_symref(&ref, refname, target_ref);
3928 if (err)
3929 goto done;
3931 err = got_ref_write(ref, repo);
3932 done:
3933 if (target_ref)
3934 got_ref_close(target_ref);
3935 if (ref)
3936 got_ref_close(ref);
3937 return err;
3940 static const struct got_error *
3941 cmd_ref(int argc, char *argv[])
3943 const struct got_error *error = NULL;
3944 struct got_repository *repo = NULL;
3945 struct got_worktree *worktree = NULL;
3946 char *cwd = NULL, *repo_path = NULL;
3947 int ch, do_list = 0, create_symref = 0;
3948 const char *delref = NULL;
3950 /* TODO: Add -s option for adding symbolic references. */
3951 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3952 switch (ch) {
3953 case 'd':
3954 delref = optarg;
3955 break;
3956 case 'r':
3957 repo_path = realpath(optarg, NULL);
3958 if (repo_path == NULL)
3959 return got_error_from_errno2("realpath",
3960 optarg);
3961 got_path_strip_trailing_slashes(repo_path);
3962 break;
3963 case 'l':
3964 do_list = 1;
3965 break;
3966 case 's':
3967 create_symref = 1;
3968 break;
3969 default:
3970 usage_ref();
3971 /* NOTREACHED */
3975 if (do_list && delref)
3976 errx(1, "-l and -d options are mutually exclusive\n");
3978 argc -= optind;
3979 argv += optind;
3981 if (do_list || delref) {
3982 if (create_symref)
3983 errx(1, "-s option cannot be used together with the "
3984 "-l or -d options");
3985 if (argc > 0)
3986 usage_ref();
3987 } else if (argc != 2)
3988 usage_ref();
3990 #ifndef PROFILE
3991 if (do_list) {
3992 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3993 NULL) == -1)
3994 err(1, "pledge");
3995 } else {
3996 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3997 "sendfd unveil", NULL) == -1)
3998 err(1, "pledge");
4000 #endif
4001 cwd = getcwd(NULL, 0);
4002 if (cwd == NULL) {
4003 error = got_error_from_errno("getcwd");
4004 goto done;
4007 if (repo_path == NULL) {
4008 error = got_worktree_open(&worktree, cwd);
4009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4010 goto done;
4011 else
4012 error = NULL;
4013 if (worktree) {
4014 repo_path =
4015 strdup(got_worktree_get_repo_path(worktree));
4016 if (repo_path == NULL)
4017 error = got_error_from_errno("strdup");
4018 if (error)
4019 goto done;
4020 } else {
4021 repo_path = strdup(cwd);
4022 if (repo_path == NULL) {
4023 error = got_error_from_errno("strdup");
4024 goto done;
4029 error = got_repo_open(&repo, repo_path, NULL);
4030 if (error != NULL)
4031 goto done;
4033 error = apply_unveil(got_repo_get_path(repo), do_list,
4034 worktree ? got_worktree_get_root_path(worktree) : NULL);
4035 if (error)
4036 goto done;
4038 if (do_list)
4039 error = list_refs(repo);
4040 else if (delref)
4041 error = delete_ref(repo, delref);
4042 else if (create_symref)
4043 error = add_symref(repo, argv[0], argv[1]);
4044 else
4045 error = add_ref(repo, argv[0], argv[1]);
4046 done:
4047 if (repo)
4048 got_repo_close(repo);
4049 if (worktree)
4050 got_worktree_close(worktree);
4051 free(cwd);
4052 free(repo_path);
4053 return error;
4056 __dead static void
4057 usage_branch(void)
4059 fprintf(stderr,
4060 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4061 "[name]\n", getprogname());
4062 exit(1);
4065 static const struct got_error *
4066 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4067 struct got_reference *ref)
4069 const struct got_error *err = NULL;
4070 const char *refname, *marker = " ";
4071 char *refstr;
4073 refname = got_ref_get_name(ref);
4074 if (worktree && strcmp(refname,
4075 got_worktree_get_head_ref_name(worktree)) == 0) {
4076 struct got_object_id *id = NULL;
4078 err = got_ref_resolve(&id, repo, ref);
4079 if (err)
4080 return err;
4081 if (got_object_id_cmp(id,
4082 got_worktree_get_base_commit_id(worktree)) == 0)
4083 marker = "* ";
4084 else
4085 marker = "~ ";
4086 free(id);
4089 if (strncmp(refname, "refs/heads/", 11) == 0)
4090 refname += 11;
4091 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4092 refname += 18;
4094 refstr = got_ref_to_str(ref);
4095 if (refstr == NULL)
4096 return got_error_from_errno("got_ref_to_str");
4098 printf("%s%s: %s\n", marker, refname, refstr);
4099 free(refstr);
4100 return NULL;
4103 static const struct got_error *
4104 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4106 const char *refname;
4108 if (worktree == NULL)
4109 return got_error(GOT_ERR_NOT_WORKTREE);
4111 refname = got_worktree_get_head_ref_name(worktree);
4113 if (strncmp(refname, "refs/heads/", 11) == 0)
4114 refname += 11;
4115 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4116 refname += 18;
4118 printf("%s\n", refname);
4120 return NULL;
4123 static const struct got_error *
4124 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4126 static const struct got_error *err = NULL;
4127 struct got_reflist_head refs;
4128 struct got_reflist_entry *re;
4129 struct got_reference *temp_ref = NULL;
4130 int rebase_in_progress, histedit_in_progress;
4132 SIMPLEQ_INIT(&refs);
4134 if (worktree) {
4135 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4136 worktree);
4137 if (err)
4138 return err;
4140 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4141 worktree);
4142 if (err)
4143 return err;
4145 if (rebase_in_progress || histedit_in_progress) {
4146 err = got_ref_open(&temp_ref, repo,
4147 got_worktree_get_head_ref_name(worktree), 0);
4148 if (err)
4149 return err;
4150 list_branch(repo, worktree, temp_ref);
4151 got_ref_close(temp_ref);
4155 err = got_ref_list(&refs, repo, "refs/heads",
4156 got_ref_cmp_by_name, NULL);
4157 if (err)
4158 return err;
4160 SIMPLEQ_FOREACH(re, &refs, entry)
4161 list_branch(repo, worktree, re->ref);
4163 got_ref_list_free(&refs);
4164 return NULL;
4167 static const struct got_error *
4168 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4169 const char *branch_name)
4171 const struct got_error *err = NULL;
4172 struct got_reference *ref = NULL;
4173 char *refname;
4175 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4176 return got_error_from_errno("asprintf");
4178 err = got_ref_open(&ref, repo, refname, 0);
4179 if (err)
4180 goto done;
4182 if (worktree &&
4183 strcmp(got_worktree_get_head_ref_name(worktree),
4184 got_ref_get_name(ref)) == 0) {
4185 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4186 "will not delete this work tree's current branch");
4187 goto done;
4190 err = got_ref_delete(ref, repo);
4191 done:
4192 if (ref)
4193 got_ref_close(ref);
4194 free(refname);
4195 return err;
4198 static const struct got_error *
4199 add_branch(struct got_repository *repo, const char *branch_name,
4200 struct got_object_id *base_commit_id)
4202 const struct got_error *err = NULL;
4203 struct got_reference *ref = NULL;
4204 char *base_refname = NULL, *refname = NULL;
4207 * Don't let the user create a branch name with a leading '-'.
4208 * While technically a valid reference name, this case is usually
4209 * an unintended typo.
4211 if (branch_name[0] == '-')
4212 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4214 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4215 err = got_error_from_errno("asprintf");
4216 goto done;
4219 err = got_ref_open(&ref, repo, refname, 0);
4220 if (err == NULL) {
4221 err = got_error(GOT_ERR_BRANCH_EXISTS);
4222 goto done;
4223 } else if (err->code != GOT_ERR_NOT_REF)
4224 goto done;
4226 err = got_ref_alloc(&ref, refname, base_commit_id);
4227 if (err)
4228 goto done;
4230 err = got_ref_write(ref, repo);
4231 done:
4232 if (ref)
4233 got_ref_close(ref);
4234 free(base_refname);
4235 free(refname);
4236 return err;
4239 static const struct got_error *
4240 cmd_branch(int argc, char *argv[])
4242 const struct got_error *error = NULL;
4243 struct got_repository *repo = NULL;
4244 struct got_worktree *worktree = NULL;
4245 char *cwd = NULL, *repo_path = NULL;
4246 int ch, do_list = 0, do_show = 0, do_update = 1;
4247 const char *delref = NULL, *commit_id_arg = NULL;
4248 struct got_reference *ref = NULL;
4249 struct got_pathlist_head paths;
4250 struct got_pathlist_entry *pe;
4251 struct got_object_id *commit_id = NULL;
4252 char *commit_id_str = NULL;
4254 TAILQ_INIT(&paths);
4256 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4257 switch (ch) {
4258 case 'c':
4259 commit_id_arg = optarg;
4260 break;
4261 case 'd':
4262 delref = optarg;
4263 break;
4264 case 'r':
4265 repo_path = realpath(optarg, NULL);
4266 if (repo_path == NULL)
4267 return got_error_from_errno2("realpath",
4268 optarg);
4269 got_path_strip_trailing_slashes(repo_path);
4270 break;
4271 case 'l':
4272 do_list = 1;
4273 break;
4274 case 'n':
4275 do_update = 0;
4276 break;
4277 default:
4278 usage_branch();
4279 /* NOTREACHED */
4283 if (do_list && delref)
4284 errx(1, "-l and -d options are mutually exclusive\n");
4286 argc -= optind;
4287 argv += optind;
4289 if (!do_list && !delref && argc == 0)
4290 do_show = 1;
4292 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4293 errx(1, "-c option can only be used when creating a branch");
4295 if (do_list || delref) {
4296 if (argc > 0)
4297 usage_branch();
4298 } else if (!do_show && argc != 1)
4299 usage_branch();
4301 #ifndef PROFILE
4302 if (do_list || do_show) {
4303 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4304 NULL) == -1)
4305 err(1, "pledge");
4306 } else {
4307 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4308 "sendfd unveil", NULL) == -1)
4309 err(1, "pledge");
4311 #endif
4312 cwd = getcwd(NULL, 0);
4313 if (cwd == NULL) {
4314 error = got_error_from_errno("getcwd");
4315 goto done;
4318 if (repo_path == NULL) {
4319 error = got_worktree_open(&worktree, cwd);
4320 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4321 goto done;
4322 else
4323 error = NULL;
4324 if (worktree) {
4325 repo_path =
4326 strdup(got_worktree_get_repo_path(worktree));
4327 if (repo_path == NULL)
4328 error = got_error_from_errno("strdup");
4329 if (error)
4330 goto done;
4331 } else {
4332 repo_path = strdup(cwd);
4333 if (repo_path == NULL) {
4334 error = got_error_from_errno("strdup");
4335 goto done;
4340 error = got_repo_open(&repo, repo_path, NULL);
4341 if (error != NULL)
4342 goto done;
4344 error = apply_unveil(got_repo_get_path(repo), do_list,
4345 worktree ? got_worktree_get_root_path(worktree) : NULL);
4346 if (error)
4347 goto done;
4349 if (do_show)
4350 error = show_current_branch(repo, worktree);
4351 else if (do_list)
4352 error = list_branches(repo, worktree);
4353 else if (delref)
4354 error = delete_branch(repo, worktree, delref);
4355 else {
4356 if (commit_id_arg == NULL)
4357 commit_id_arg = worktree ?
4358 got_worktree_get_head_ref_name(worktree) :
4359 GOT_REF_HEAD;
4360 error = got_repo_match_object_id(&commit_id, NULL,
4361 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4362 if (error)
4363 goto done;
4364 error = add_branch(repo, argv[0], commit_id);
4365 if (error)
4366 goto done;
4367 if (worktree && do_update) {
4368 int did_something = 0;
4369 char *branch_refname = NULL;
4371 error = got_object_id_str(&commit_id_str, commit_id);
4372 if (error)
4373 goto done;
4374 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4375 worktree);
4376 if (error)
4377 goto done;
4378 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4379 == -1) {
4380 error = got_error_from_errno("asprintf");
4381 goto done;
4383 error = got_ref_open(&ref, repo, branch_refname, 0);
4384 free(branch_refname);
4385 if (error)
4386 goto done;
4387 error = switch_head_ref(ref, commit_id, worktree,
4388 repo);
4389 if (error)
4390 goto done;
4391 error = got_worktree_set_base_commit_id(worktree, repo,
4392 commit_id);
4393 if (error)
4394 goto done;
4395 error = got_worktree_checkout_files(worktree, &paths,
4396 repo, update_progress, &did_something,
4397 check_cancelled, NULL);
4398 if (error)
4399 goto done;
4400 if (did_something)
4401 printf("Updated to commit %s\n", commit_id_str);
4404 done:
4405 if (ref)
4406 got_ref_close(ref);
4407 if (repo)
4408 got_repo_close(repo);
4409 if (worktree)
4410 got_worktree_close(worktree);
4411 free(cwd);
4412 free(repo_path);
4413 free(commit_id);
4414 free(commit_id_str);
4415 TAILQ_FOREACH(pe, &paths, entry)
4416 free((char *)pe->path);
4417 got_pathlist_free(&paths);
4418 return error;
4422 __dead static void
4423 usage_tag(void)
4425 fprintf(stderr,
4426 "usage: %s tag [-c commit] [-r repository] [-l] "
4427 "[-m message] name\n", getprogname());
4428 exit(1);
4431 #if 0
4432 static const struct got_error *
4433 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4435 const struct got_error *err = NULL;
4436 struct got_reflist_entry *re, *se, *new;
4437 struct got_object_id *re_id, *se_id;
4438 struct got_tag_object *re_tag, *se_tag;
4439 time_t re_time, se_time;
4441 SIMPLEQ_FOREACH(re, tags, entry) {
4442 se = SIMPLEQ_FIRST(sorted);
4443 if (se == NULL) {
4444 err = got_reflist_entry_dup(&new, re);
4445 if (err)
4446 return err;
4447 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4448 continue;
4449 } else {
4450 err = got_ref_resolve(&re_id, repo, re->ref);
4451 if (err)
4452 break;
4453 err = got_object_open_as_tag(&re_tag, repo, re_id);
4454 free(re_id);
4455 if (err)
4456 break;
4457 re_time = got_object_tag_get_tagger_time(re_tag);
4458 got_object_tag_close(re_tag);
4461 while (se) {
4462 err = got_ref_resolve(&se_id, repo, re->ref);
4463 if (err)
4464 break;
4465 err = got_object_open_as_tag(&se_tag, repo, se_id);
4466 free(se_id);
4467 if (err)
4468 break;
4469 se_time = got_object_tag_get_tagger_time(se_tag);
4470 got_object_tag_close(se_tag);
4472 if (se_time > re_time) {
4473 err = got_reflist_entry_dup(&new, re);
4474 if (err)
4475 return err;
4476 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4477 break;
4479 se = SIMPLEQ_NEXT(se, entry);
4480 continue;
4483 done:
4484 return err;
4486 #endif
4488 static const struct got_error *
4489 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4491 static const struct got_error *err = NULL;
4492 struct got_reflist_head refs;
4493 struct got_reflist_entry *re;
4495 SIMPLEQ_INIT(&refs);
4497 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4498 if (err)
4499 return err;
4501 SIMPLEQ_FOREACH(re, &refs, entry) {
4502 const char *refname;
4503 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4504 char datebuf[26];
4505 const char *tagger;
4506 time_t tagger_time;
4507 struct got_object_id *id;
4508 struct got_tag_object *tag;
4509 struct got_commit_object *commit = NULL;
4511 refname = got_ref_get_name(re->ref);
4512 if (strncmp(refname, "refs/tags/", 10) != 0)
4513 continue;
4514 refname += 10;
4515 refstr = got_ref_to_str(re->ref);
4516 if (refstr == NULL) {
4517 err = got_error_from_errno("got_ref_to_str");
4518 break;
4520 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4521 free(refstr);
4523 err = got_ref_resolve(&id, repo, re->ref);
4524 if (err)
4525 break;
4526 err = got_object_open_as_tag(&tag, repo, id);
4527 if (err) {
4528 if (err->code != GOT_ERR_OBJ_TYPE) {
4529 free(id);
4530 break;
4532 /* "lightweight" tag */
4533 err = got_object_open_as_commit(&commit, repo, id);
4534 if (err) {
4535 free(id);
4536 break;
4538 tagger = got_object_commit_get_committer(commit);
4539 tagger_time =
4540 got_object_commit_get_committer_time(commit);
4541 err = got_object_id_str(&id_str, id);
4542 free(id);
4543 if (err)
4544 break;
4545 } else {
4546 free(id);
4547 tagger = got_object_tag_get_tagger(tag);
4548 tagger_time = got_object_tag_get_tagger_time(tag);
4549 err = got_object_id_str(&id_str,
4550 got_object_tag_get_object_id(tag));
4551 if (err)
4552 break;
4554 printf("from: %s\n", tagger);
4555 datestr = get_datestr(&tagger_time, datebuf);
4556 if (datestr)
4557 printf("date: %s UTC\n", datestr);
4558 if (commit)
4559 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4560 else {
4561 switch (got_object_tag_get_object_type(tag)) {
4562 case GOT_OBJ_TYPE_BLOB:
4563 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4564 id_str);
4565 break;
4566 case GOT_OBJ_TYPE_TREE:
4567 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4568 id_str);
4569 break;
4570 case GOT_OBJ_TYPE_COMMIT:
4571 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4572 id_str);
4573 break;
4574 case GOT_OBJ_TYPE_TAG:
4575 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4576 id_str);
4577 break;
4578 default:
4579 break;
4582 free(id_str);
4583 if (commit) {
4584 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4585 if (err)
4586 break;
4587 got_object_commit_close(commit);
4588 } else {
4589 tagmsg0 = strdup(got_object_tag_get_message(tag));
4590 got_object_tag_close(tag);
4591 if (tagmsg0 == NULL) {
4592 err = got_error_from_errno("strdup");
4593 break;
4597 tagmsg = tagmsg0;
4598 do {
4599 line = strsep(&tagmsg, "\n");
4600 if (line)
4601 printf(" %s\n", line);
4602 } while (line);
4603 free(tagmsg0);
4606 got_ref_list_free(&refs);
4607 return NULL;
4610 static const struct got_error *
4611 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4612 const char *tag_name, const char *repo_path)
4614 const struct got_error *err = NULL;
4615 char *template = NULL, *initial_content = NULL;
4616 char *editor = NULL;
4617 int fd = -1;
4619 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4620 err = got_error_from_errno("asprintf");
4621 goto done;
4624 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4625 commit_id_str, tag_name) == -1) {
4626 err = got_error_from_errno("asprintf");
4627 goto done;
4630 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4631 if (err)
4632 goto done;
4634 dprintf(fd, initial_content);
4635 close(fd);
4637 err = get_editor(&editor);
4638 if (err)
4639 goto done;
4640 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4641 done:
4642 free(initial_content);
4643 free(template);
4644 free(editor);
4646 /* Editor is done; we can now apply unveil(2) */
4647 if (err == NULL) {
4648 err = apply_unveil(repo_path, 0, NULL);
4649 if (err) {
4650 free(*tagmsg);
4651 *tagmsg = NULL;
4654 return err;
4657 static const struct got_error *
4658 add_tag(struct got_repository *repo, const char *tag_name,
4659 const char *commit_arg, const char *tagmsg_arg)
4661 const struct got_error *err = NULL;
4662 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4663 char *label = NULL, *commit_id_str = NULL;
4664 struct got_reference *ref = NULL;
4665 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4666 char *tagmsg_path = NULL, *tag_id_str = NULL;
4667 int preserve_tagmsg = 0;
4670 * Don't let the user create a tag name with a leading '-'.
4671 * While technically a valid reference name, this case is usually
4672 * an unintended typo.
4674 if (tag_name[0] == '-')
4675 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4677 err = get_author(&tagger, repo);
4678 if (err)
4679 return err;
4681 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4682 GOT_OBJ_TYPE_COMMIT, 1, repo);
4683 if (err)
4684 goto done;
4686 err = got_object_id_str(&commit_id_str, commit_id);
4687 if (err)
4688 goto done;
4690 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4691 refname = strdup(tag_name);
4692 if (refname == NULL) {
4693 err = got_error_from_errno("strdup");
4694 goto done;
4696 tag_name += 10;
4697 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4698 err = got_error_from_errno("asprintf");
4699 goto done;
4702 err = got_ref_open(&ref, repo, refname, 0);
4703 if (err == NULL) {
4704 err = got_error(GOT_ERR_TAG_EXISTS);
4705 goto done;
4706 } else if (err->code != GOT_ERR_NOT_REF)
4707 goto done;
4709 if (tagmsg_arg == NULL) {
4710 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4711 tag_name, got_repo_get_path(repo));
4712 if (err) {
4713 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4714 tagmsg_path != NULL)
4715 preserve_tagmsg = 1;
4716 goto done;
4720 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4721 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4722 if (err) {
4723 if (tagmsg_path)
4724 preserve_tagmsg = 1;
4725 goto done;
4728 err = got_ref_alloc(&ref, refname, tag_id);
4729 if (err) {
4730 if (tagmsg_path)
4731 preserve_tagmsg = 1;
4732 goto done;
4735 err = got_ref_write(ref, repo);
4736 if (err) {
4737 if (tagmsg_path)
4738 preserve_tagmsg = 1;
4739 goto done;
4742 err = got_object_id_str(&tag_id_str, tag_id);
4743 if (err) {
4744 if (tagmsg_path)
4745 preserve_tagmsg = 1;
4746 goto done;
4748 printf("Created tag %s\n", tag_id_str);
4749 done:
4750 if (preserve_tagmsg) {
4751 fprintf(stderr, "%s: tag message preserved in %s\n",
4752 getprogname(), tagmsg_path);
4753 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4754 err = got_error_from_errno2("unlink", tagmsg_path);
4755 free(tag_id_str);
4756 if (ref)
4757 got_ref_close(ref);
4758 free(commit_id);
4759 free(commit_id_str);
4760 free(refname);
4761 free(tagmsg);
4762 free(tagmsg_path);
4763 free(tagger);
4764 return err;
4767 static const struct got_error *
4768 cmd_tag(int argc, char *argv[])
4770 const struct got_error *error = NULL;
4771 struct got_repository *repo = NULL;
4772 struct got_worktree *worktree = NULL;
4773 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4774 char *gitconfig_path = NULL;
4775 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4776 int ch, do_list = 0;
4778 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4779 switch (ch) {
4780 case 'c':
4781 commit_id_arg = optarg;
4782 break;
4783 case 'm':
4784 tagmsg = optarg;
4785 break;
4786 case 'r':
4787 repo_path = realpath(optarg, NULL);
4788 if (repo_path == NULL)
4789 return got_error_from_errno2("realpath",
4790 optarg);
4791 got_path_strip_trailing_slashes(repo_path);
4792 break;
4793 case 'l':
4794 do_list = 1;
4795 break;
4796 default:
4797 usage_tag();
4798 /* NOTREACHED */
4802 argc -= optind;
4803 argv += optind;
4805 if (do_list) {
4806 if (commit_id_arg != NULL)
4807 errx(1, "-c option can only be used when creating a tag");
4808 if (tagmsg)
4809 errx(1, "-l and -m options are mutually exclusive");
4810 if (argc > 0)
4811 usage_tag();
4812 } else if (argc != 1)
4813 usage_tag();
4815 tag_name = argv[0];
4817 #ifndef PROFILE
4818 if (do_list) {
4819 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4820 NULL) == -1)
4821 err(1, "pledge");
4822 } else {
4823 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4824 "sendfd unveil", NULL) == -1)
4825 err(1, "pledge");
4827 #endif
4828 cwd = getcwd(NULL, 0);
4829 if (cwd == NULL) {
4830 error = got_error_from_errno("getcwd");
4831 goto done;
4834 if (repo_path == NULL) {
4835 error = got_worktree_open(&worktree, cwd);
4836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4837 goto done;
4838 else
4839 error = NULL;
4840 if (worktree) {
4841 repo_path =
4842 strdup(got_worktree_get_repo_path(worktree));
4843 if (repo_path == NULL)
4844 error = got_error_from_errno("strdup");
4845 if (error)
4846 goto done;
4847 } else {
4848 repo_path = strdup(cwd);
4849 if (repo_path == NULL) {
4850 error = got_error_from_errno("strdup");
4851 goto done;
4856 if (do_list) {
4857 error = got_repo_open(&repo, repo_path, NULL);
4858 if (error != NULL)
4859 goto done;
4860 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4861 if (error)
4862 goto done;
4863 error = list_tags(repo, worktree);
4864 } else {
4865 error = get_gitconfig_path(&gitconfig_path);
4866 if (error)
4867 goto done;
4868 error = got_repo_open(&repo, repo_path, gitconfig_path);
4869 if (error != NULL)
4870 goto done;
4872 if (tagmsg) {
4873 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4874 if (error)
4875 goto done;
4878 if (commit_id_arg == NULL) {
4879 struct got_reference *head_ref;
4880 struct got_object_id *commit_id;
4881 error = got_ref_open(&head_ref, repo,
4882 worktree ? got_worktree_get_head_ref_name(worktree)
4883 : GOT_REF_HEAD, 0);
4884 if (error)
4885 goto done;
4886 error = got_ref_resolve(&commit_id, repo, head_ref);
4887 got_ref_close(head_ref);
4888 if (error)
4889 goto done;
4890 error = got_object_id_str(&commit_id_str, commit_id);
4891 free(commit_id);
4892 if (error)
4893 goto done;
4896 error = add_tag(repo, tag_name,
4897 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4899 done:
4900 if (repo)
4901 got_repo_close(repo);
4902 if (worktree)
4903 got_worktree_close(worktree);
4904 free(cwd);
4905 free(repo_path);
4906 free(gitconfig_path);
4907 free(commit_id_str);
4908 return error;
4911 __dead static void
4912 usage_add(void)
4914 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4915 getprogname());
4916 exit(1);
4919 static const struct got_error *
4920 add_progress(void *arg, unsigned char status, const char *path)
4922 while (path[0] == '/')
4923 path++;
4924 printf("%c %s\n", status, path);
4925 return NULL;
4928 static const struct got_error *
4929 cmd_add(int argc, char *argv[])
4931 const struct got_error *error = NULL;
4932 struct got_repository *repo = NULL;
4933 struct got_worktree *worktree = NULL;
4934 char *cwd = NULL;
4935 struct got_pathlist_head paths;
4936 struct got_pathlist_entry *pe;
4937 int ch, can_recurse = 0, no_ignores = 0;
4939 TAILQ_INIT(&paths);
4941 while ((ch = getopt(argc, argv, "IR")) != -1) {
4942 switch (ch) {
4943 case 'I':
4944 no_ignores = 1;
4945 break;
4946 case 'R':
4947 can_recurse = 1;
4948 break;
4949 default:
4950 usage_add();
4951 /* NOTREACHED */
4955 argc -= optind;
4956 argv += optind;
4958 #ifndef PROFILE
4959 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4960 NULL) == -1)
4961 err(1, "pledge");
4962 #endif
4963 if (argc < 1)
4964 usage_add();
4966 cwd = getcwd(NULL, 0);
4967 if (cwd == NULL) {
4968 error = got_error_from_errno("getcwd");
4969 goto done;
4972 error = got_worktree_open(&worktree, cwd);
4973 if (error)
4974 goto done;
4976 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4977 NULL);
4978 if (error != NULL)
4979 goto done;
4981 error = apply_unveil(got_repo_get_path(repo), 1,
4982 got_worktree_get_root_path(worktree));
4983 if (error)
4984 goto done;
4986 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4987 if (error)
4988 goto done;
4990 if (!can_recurse && no_ignores) {
4991 error = got_error_msg(GOT_ERR_BAD_PATH,
4992 "disregarding ignores requires -R option");
4993 goto done;
4997 if (!can_recurse) {
4998 char *ondisk_path;
4999 struct stat sb;
5000 TAILQ_FOREACH(pe, &paths, entry) {
5001 if (asprintf(&ondisk_path, "%s/%s",
5002 got_worktree_get_root_path(worktree),
5003 pe->path) == -1) {
5004 error = got_error_from_errno("asprintf");
5005 goto done;
5007 if (lstat(ondisk_path, &sb) == -1) {
5008 if (errno == ENOENT) {
5009 free(ondisk_path);
5010 continue;
5012 error = got_error_from_errno2("lstat",
5013 ondisk_path);
5014 free(ondisk_path);
5015 goto done;
5017 free(ondisk_path);
5018 if (S_ISDIR(sb.st_mode)) {
5019 error = got_error_msg(GOT_ERR_BAD_PATH,
5020 "adding directories requires -R option");
5021 goto done;
5026 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5027 NULL, repo, no_ignores);
5028 done:
5029 if (repo)
5030 got_repo_close(repo);
5031 if (worktree)
5032 got_worktree_close(worktree);
5033 TAILQ_FOREACH(pe, &paths, entry)
5034 free((char *)pe->path);
5035 got_pathlist_free(&paths);
5036 free(cwd);
5037 return error;
5040 __dead static void
5041 usage_remove(void)
5043 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5044 getprogname());
5045 exit(1);
5048 static const struct got_error *
5049 print_remove_status(void *arg, unsigned char status,
5050 unsigned char staged_status, const char *path)
5052 while (path[0] == '/')
5053 path++;
5054 if (status == GOT_STATUS_NONEXISTENT)
5055 return NULL;
5056 if (status == staged_status && (status == GOT_STATUS_DELETE))
5057 status = GOT_STATUS_NO_CHANGE;
5058 printf("%c%c %s\n", status, staged_status, path);
5059 return NULL;
5062 static const struct got_error *
5063 cmd_remove(int argc, char *argv[])
5065 const struct got_error *error = NULL;
5066 struct got_worktree *worktree = NULL;
5067 struct got_repository *repo = NULL;
5068 char *cwd = NULL;
5069 struct got_pathlist_head paths;
5070 struct got_pathlist_entry *pe;
5071 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5073 TAILQ_INIT(&paths);
5075 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5076 switch (ch) {
5077 case 'f':
5078 delete_local_mods = 1;
5079 break;
5080 case 'k':
5081 keep_on_disk = 1;
5082 break;
5083 case 'R':
5084 can_recurse = 1;
5085 break;
5086 default:
5087 usage_remove();
5088 /* NOTREACHED */
5092 argc -= optind;
5093 argv += optind;
5095 #ifndef PROFILE
5096 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5097 NULL) == -1)
5098 err(1, "pledge");
5099 #endif
5100 if (argc < 1)
5101 usage_remove();
5103 cwd = getcwd(NULL, 0);
5104 if (cwd == NULL) {
5105 error = got_error_from_errno("getcwd");
5106 goto done;
5108 error = got_worktree_open(&worktree, cwd);
5109 if (error)
5110 goto done;
5112 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5113 NULL);
5114 if (error)
5115 goto done;
5117 error = apply_unveil(got_repo_get_path(repo), 1,
5118 got_worktree_get_root_path(worktree));
5119 if (error)
5120 goto done;
5122 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5123 if (error)
5124 goto done;
5126 if (!can_recurse) {
5127 char *ondisk_path;
5128 struct stat sb;
5129 TAILQ_FOREACH(pe, &paths, entry) {
5130 if (asprintf(&ondisk_path, "%s/%s",
5131 got_worktree_get_root_path(worktree),
5132 pe->path) == -1) {
5133 error = got_error_from_errno("asprintf");
5134 goto done;
5136 if (lstat(ondisk_path, &sb) == -1) {
5137 if (errno == ENOENT) {
5138 free(ondisk_path);
5139 continue;
5141 error = got_error_from_errno2("lstat",
5142 ondisk_path);
5143 free(ondisk_path);
5144 goto done;
5146 free(ondisk_path);
5147 if (S_ISDIR(sb.st_mode)) {
5148 error = got_error_msg(GOT_ERR_BAD_PATH,
5149 "removing directories requires -R option");
5150 goto done;
5155 error = got_worktree_schedule_delete(worktree, &paths,
5156 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5157 done:
5158 if (repo)
5159 got_repo_close(repo);
5160 if (worktree)
5161 got_worktree_close(worktree);
5162 TAILQ_FOREACH(pe, &paths, entry)
5163 free((char *)pe->path);
5164 got_pathlist_free(&paths);
5165 free(cwd);
5166 return error;
5169 __dead static void
5170 usage_revert(void)
5172 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5173 "path ...\n", getprogname());
5174 exit(1);
5177 static const struct got_error *
5178 revert_progress(void *arg, unsigned char status, const char *path)
5180 if (status == GOT_STATUS_UNVERSIONED)
5181 return NULL;
5183 while (path[0] == '/')
5184 path++;
5185 printf("%c %s\n", status, path);
5186 return NULL;
5189 struct choose_patch_arg {
5190 FILE *patch_script_file;
5191 const char *action;
5194 static const struct got_error *
5195 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5196 int nchanges, const char *action)
5198 char *line = NULL;
5199 size_t linesize = 0;
5200 ssize_t linelen;
5202 switch (status) {
5203 case GOT_STATUS_ADD:
5204 printf("A %s\n%s this addition? [y/n] ", path, action);
5205 break;
5206 case GOT_STATUS_DELETE:
5207 printf("D %s\n%s this deletion? [y/n] ", path, action);
5208 break;
5209 case GOT_STATUS_MODIFY:
5210 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5211 return got_error_from_errno("fseek");
5212 printf(GOT_COMMIT_SEP_STR);
5213 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5214 printf("%s", line);
5215 if (ferror(patch_file))
5216 return got_error_from_errno("getline");
5217 printf(GOT_COMMIT_SEP_STR);
5218 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5219 path, n, nchanges, action);
5220 break;
5221 default:
5222 return got_error_path(path, GOT_ERR_FILE_STATUS);
5225 return NULL;
5228 static const struct got_error *
5229 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5230 FILE *patch_file, int n, int nchanges)
5232 const struct got_error *err = NULL;
5233 char *line = NULL;
5234 size_t linesize = 0;
5235 ssize_t linelen;
5236 int resp = ' ';
5237 struct choose_patch_arg *a = arg;
5239 *choice = GOT_PATCH_CHOICE_NONE;
5241 if (a->patch_script_file) {
5242 char *nl;
5243 err = show_change(status, path, patch_file, n, nchanges,
5244 a->action);
5245 if (err)
5246 return err;
5247 linelen = getline(&line, &linesize, a->patch_script_file);
5248 if (linelen == -1) {
5249 if (ferror(a->patch_script_file))
5250 return got_error_from_errno("getline");
5251 return NULL;
5253 nl = strchr(line, '\n');
5254 if (nl)
5255 *nl = '\0';
5256 if (strcmp(line, "y") == 0) {
5257 *choice = GOT_PATCH_CHOICE_YES;
5258 printf("y\n");
5259 } else if (strcmp(line, "n") == 0) {
5260 *choice = GOT_PATCH_CHOICE_NO;
5261 printf("n\n");
5262 } else if (strcmp(line, "q") == 0 &&
5263 status == GOT_STATUS_MODIFY) {
5264 *choice = GOT_PATCH_CHOICE_QUIT;
5265 printf("q\n");
5266 } else
5267 printf("invalid response '%s'\n", line);
5268 free(line);
5269 return NULL;
5272 while (resp != 'y' && resp != 'n' && resp != 'q') {
5273 err = show_change(status, path, patch_file, n, nchanges,
5274 a->action);
5275 if (err)
5276 return err;
5277 resp = getchar();
5278 if (resp == '\n')
5279 resp = getchar();
5280 if (status == GOT_STATUS_MODIFY) {
5281 if (resp != 'y' && resp != 'n' && resp != 'q') {
5282 printf("invalid response '%c'\n", resp);
5283 resp = ' ';
5285 } else if (resp != 'y' && resp != 'n') {
5286 printf("invalid response '%c'\n", resp);
5287 resp = ' ';
5291 if (resp == 'y')
5292 *choice = GOT_PATCH_CHOICE_YES;
5293 else if (resp == 'n')
5294 *choice = GOT_PATCH_CHOICE_NO;
5295 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5296 *choice = GOT_PATCH_CHOICE_QUIT;
5298 return NULL;
5302 static const struct got_error *
5303 cmd_revert(int argc, char *argv[])
5305 const struct got_error *error = NULL;
5306 struct got_worktree *worktree = NULL;
5307 struct got_repository *repo = NULL;
5308 char *cwd = NULL, *path = NULL;
5309 struct got_pathlist_head paths;
5310 struct got_pathlist_entry *pe;
5311 int ch, can_recurse = 0, pflag = 0;
5312 FILE *patch_script_file = NULL;
5313 const char *patch_script_path = NULL;
5314 struct choose_patch_arg cpa;
5316 TAILQ_INIT(&paths);
5318 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5319 switch (ch) {
5320 case 'p':
5321 pflag = 1;
5322 break;
5323 case 'F':
5324 patch_script_path = optarg;
5325 break;
5326 case 'R':
5327 can_recurse = 1;
5328 break;
5329 default:
5330 usage_revert();
5331 /* NOTREACHED */
5335 argc -= optind;
5336 argv += optind;
5338 #ifndef PROFILE
5339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5340 "unveil", NULL) == -1)
5341 err(1, "pledge");
5342 #endif
5343 if (argc < 1)
5344 usage_revert();
5345 if (patch_script_path && !pflag)
5346 errx(1, "-F option can only be used together with -p option");
5348 cwd = getcwd(NULL, 0);
5349 if (cwd == NULL) {
5350 error = got_error_from_errno("getcwd");
5351 goto done;
5353 error = got_worktree_open(&worktree, cwd);
5354 if (error)
5355 goto done;
5357 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5358 NULL);
5359 if (error != NULL)
5360 goto done;
5362 if (patch_script_path) {
5363 patch_script_file = fopen(patch_script_path, "r");
5364 if (patch_script_file == NULL) {
5365 error = got_error_from_errno2("fopen",
5366 patch_script_path);
5367 goto done;
5370 error = apply_unveil(got_repo_get_path(repo), 1,
5371 got_worktree_get_root_path(worktree));
5372 if (error)
5373 goto done;
5375 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5376 if (error)
5377 goto done;
5379 if (!can_recurse) {
5380 char *ondisk_path;
5381 struct stat sb;
5382 TAILQ_FOREACH(pe, &paths, entry) {
5383 if (asprintf(&ondisk_path, "%s/%s",
5384 got_worktree_get_root_path(worktree),
5385 pe->path) == -1) {
5386 error = got_error_from_errno("asprintf");
5387 goto done;
5389 if (lstat(ondisk_path, &sb) == -1) {
5390 if (errno == ENOENT) {
5391 free(ondisk_path);
5392 continue;
5394 error = got_error_from_errno2("lstat",
5395 ondisk_path);
5396 free(ondisk_path);
5397 goto done;
5399 free(ondisk_path);
5400 if (S_ISDIR(sb.st_mode)) {
5401 error = got_error_msg(GOT_ERR_BAD_PATH,
5402 "reverting directories requires -R option");
5403 goto done;
5408 cpa.patch_script_file = patch_script_file;
5409 cpa.action = "revert";
5410 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5411 pflag ? choose_patch : NULL, &cpa, repo);
5412 done:
5413 if (patch_script_file && fclose(patch_script_file) == EOF &&
5414 error == NULL)
5415 error = got_error_from_errno2("fclose", patch_script_path);
5416 if (repo)
5417 got_repo_close(repo);
5418 if (worktree)
5419 got_worktree_close(worktree);
5420 free(path);
5421 free(cwd);
5422 return error;
5425 __dead static void
5426 usage_commit(void)
5428 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5429 getprogname());
5430 exit(1);
5433 struct collect_commit_logmsg_arg {
5434 const char *cmdline_log;
5435 const char *editor;
5436 const char *worktree_path;
5437 const char *branch_name;
5438 const char *repo_path;
5439 char *logmsg_path;
5443 static const struct got_error *
5444 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5445 void *arg)
5447 char *initial_content = NULL;
5448 struct got_pathlist_entry *pe;
5449 const struct got_error *err = NULL;
5450 char *template = NULL;
5451 struct collect_commit_logmsg_arg *a = arg;
5452 int fd;
5453 size_t len;
5455 /* if a message was specified on the command line, just use it */
5456 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5457 len = strlen(a->cmdline_log) + 1;
5458 *logmsg = malloc(len + 1);
5459 if (*logmsg == NULL)
5460 return got_error_from_errno("malloc");
5461 strlcpy(*logmsg, a->cmdline_log, len);
5462 return NULL;
5465 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5466 return got_error_from_errno("asprintf");
5468 if (asprintf(&initial_content,
5469 "\n# changes to be committed on branch %s:\n",
5470 a->branch_name) == -1)
5471 return got_error_from_errno("asprintf");
5473 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5474 if (err)
5475 goto done;
5477 dprintf(fd, initial_content);
5479 TAILQ_FOREACH(pe, commitable_paths, entry) {
5480 struct got_commitable *ct = pe->data;
5481 dprintf(fd, "# %c %s\n",
5482 got_commitable_get_status(ct),
5483 got_commitable_get_path(ct));
5485 close(fd);
5487 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5488 done:
5489 free(initial_content);
5490 free(template);
5492 /* Editor is done; we can now apply unveil(2) */
5493 if (err == NULL) {
5494 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5495 if (err) {
5496 free(*logmsg);
5497 *logmsg = NULL;
5500 return err;
5503 static const struct got_error *
5504 cmd_commit(int argc, char *argv[])
5506 const struct got_error *error = NULL;
5507 struct got_worktree *worktree = NULL;
5508 struct got_repository *repo = NULL;
5509 char *cwd = NULL, *id_str = NULL;
5510 struct got_object_id *id = NULL;
5511 const char *logmsg = NULL;
5512 struct collect_commit_logmsg_arg cl_arg;
5513 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5514 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5515 struct got_pathlist_head paths;
5517 TAILQ_INIT(&paths);
5518 cl_arg.logmsg_path = NULL;
5520 while ((ch = getopt(argc, argv, "m:")) != -1) {
5521 switch (ch) {
5522 case 'm':
5523 logmsg = optarg;
5524 break;
5525 default:
5526 usage_commit();
5527 /* NOTREACHED */
5531 argc -= optind;
5532 argv += optind;
5534 #ifndef PROFILE
5535 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5536 "unveil", NULL) == -1)
5537 err(1, "pledge");
5538 #endif
5539 cwd = getcwd(NULL, 0);
5540 if (cwd == NULL) {
5541 error = got_error_from_errno("getcwd");
5542 goto done;
5544 error = got_worktree_open(&worktree, cwd);
5545 if (error)
5546 goto done;
5548 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5549 if (error)
5550 goto done;
5551 if (rebase_in_progress) {
5552 error = got_error(GOT_ERR_REBASING);
5553 goto done;
5556 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5557 worktree);
5558 if (error)
5559 goto done;
5561 error = get_gitconfig_path(&gitconfig_path);
5562 if (error)
5563 goto done;
5564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5565 gitconfig_path);
5566 if (error != NULL)
5567 goto done;
5569 error = get_author(&author, repo);
5570 if (error)
5571 return error;
5574 * unveil(2) traverses exec(2); if an editor is used we have
5575 * to apply unveil after the log message has been written.
5577 if (logmsg == NULL || strlen(logmsg) == 0)
5578 error = get_editor(&editor);
5579 else
5580 error = apply_unveil(got_repo_get_path(repo), 0,
5581 got_worktree_get_root_path(worktree));
5582 if (error)
5583 goto done;
5585 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5586 if (error)
5587 goto done;
5589 cl_arg.editor = editor;
5590 cl_arg.cmdline_log = logmsg;
5591 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5592 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5593 if (!histedit_in_progress) {
5594 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5595 error = got_error(GOT_ERR_COMMIT_BRANCH);
5596 goto done;
5598 cl_arg.branch_name += 11;
5600 cl_arg.repo_path = got_repo_get_path(repo);
5601 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5602 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5603 if (error) {
5604 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5605 cl_arg.logmsg_path != NULL)
5606 preserve_logmsg = 1;
5607 goto done;
5610 error = got_object_id_str(&id_str, id);
5611 if (error)
5612 goto done;
5613 printf("Created commit %s\n", id_str);
5614 done:
5615 if (preserve_logmsg) {
5616 fprintf(stderr, "%s: log message preserved in %s\n",
5617 getprogname(), cl_arg.logmsg_path);
5618 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5619 error == NULL)
5620 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5621 free(cl_arg.logmsg_path);
5622 if (repo)
5623 got_repo_close(repo);
5624 if (worktree)
5625 got_worktree_close(worktree);
5626 free(cwd);
5627 free(id_str);
5628 free(gitconfig_path);
5629 free(editor);
5630 free(author);
5631 return error;
5634 __dead static void
5635 usage_cherrypick(void)
5637 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5638 exit(1);
5641 static const struct got_error *
5642 cmd_cherrypick(int argc, char *argv[])
5644 const struct got_error *error = NULL;
5645 struct got_worktree *worktree = NULL;
5646 struct got_repository *repo = NULL;
5647 char *cwd = NULL, *commit_id_str = NULL;
5648 struct got_object_id *commit_id = NULL;
5649 struct got_commit_object *commit = NULL;
5650 struct got_object_qid *pid;
5651 struct got_reference *head_ref = NULL;
5652 int ch, did_something = 0;
5654 while ((ch = getopt(argc, argv, "")) != -1) {
5655 switch (ch) {
5656 default:
5657 usage_cherrypick();
5658 /* NOTREACHED */
5662 argc -= optind;
5663 argv += optind;
5665 #ifndef PROFILE
5666 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5667 "unveil", NULL) == -1)
5668 err(1, "pledge");
5669 #endif
5670 if (argc != 1)
5671 usage_cherrypick();
5673 cwd = getcwd(NULL, 0);
5674 if (cwd == NULL) {
5675 error = got_error_from_errno("getcwd");
5676 goto done;
5678 error = got_worktree_open(&worktree, cwd);
5679 if (error)
5680 goto done;
5682 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5683 NULL);
5684 if (error != NULL)
5685 goto done;
5687 error = apply_unveil(got_repo_get_path(repo), 0,
5688 got_worktree_get_root_path(worktree));
5689 if (error)
5690 goto done;
5692 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5693 GOT_OBJ_TYPE_COMMIT, repo);
5694 if (error != NULL) {
5695 struct got_reference *ref;
5696 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5697 goto done;
5698 error = got_ref_open(&ref, repo, argv[0], 0);
5699 if (error != NULL)
5700 goto done;
5701 error = got_ref_resolve(&commit_id, repo, ref);
5702 got_ref_close(ref);
5703 if (error != NULL)
5704 goto done;
5706 error = got_object_id_str(&commit_id_str, commit_id);
5707 if (error)
5708 goto done;
5710 error = got_ref_open(&head_ref, repo,
5711 got_worktree_get_head_ref_name(worktree), 0);
5712 if (error != NULL)
5713 goto done;
5715 error = check_same_branch(commit_id, head_ref, NULL, repo);
5716 if (error) {
5717 if (error->code != GOT_ERR_ANCESTRY)
5718 goto done;
5719 error = NULL;
5720 } else {
5721 error = got_error(GOT_ERR_SAME_BRANCH);
5722 goto done;
5725 error = got_object_open_as_commit(&commit, repo, commit_id);
5726 if (error)
5727 goto done;
5728 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5729 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5730 commit_id, repo, update_progress, &did_something, check_cancelled,
5731 NULL);
5732 if (error != NULL)
5733 goto done;
5735 if (did_something)
5736 printf("Merged commit %s\n", commit_id_str);
5737 done:
5738 if (commit)
5739 got_object_commit_close(commit);
5740 free(commit_id_str);
5741 if (head_ref)
5742 got_ref_close(head_ref);
5743 if (worktree)
5744 got_worktree_close(worktree);
5745 if (repo)
5746 got_repo_close(repo);
5747 return error;
5750 __dead static void
5751 usage_backout(void)
5753 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5754 exit(1);
5757 static const struct got_error *
5758 cmd_backout(int argc, char *argv[])
5760 const struct got_error *error = NULL;
5761 struct got_worktree *worktree = NULL;
5762 struct got_repository *repo = NULL;
5763 char *cwd = NULL, *commit_id_str = NULL;
5764 struct got_object_id *commit_id = NULL;
5765 struct got_commit_object *commit = NULL;
5766 struct got_object_qid *pid;
5767 struct got_reference *head_ref = NULL;
5768 int ch, did_something = 0;
5770 while ((ch = getopt(argc, argv, "")) != -1) {
5771 switch (ch) {
5772 default:
5773 usage_backout();
5774 /* NOTREACHED */
5778 argc -= optind;
5779 argv += optind;
5781 #ifndef PROFILE
5782 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5783 "unveil", NULL) == -1)
5784 err(1, "pledge");
5785 #endif
5786 if (argc != 1)
5787 usage_backout();
5789 cwd = getcwd(NULL, 0);
5790 if (cwd == NULL) {
5791 error = got_error_from_errno("getcwd");
5792 goto done;
5794 error = got_worktree_open(&worktree, cwd);
5795 if (error)
5796 goto done;
5798 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5799 NULL);
5800 if (error != NULL)
5801 goto done;
5803 error = apply_unveil(got_repo_get_path(repo), 0,
5804 got_worktree_get_root_path(worktree));
5805 if (error)
5806 goto done;
5808 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5809 GOT_OBJ_TYPE_COMMIT, repo);
5810 if (error != NULL) {
5811 struct got_reference *ref;
5812 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5813 goto done;
5814 error = got_ref_open(&ref, repo, argv[0], 0);
5815 if (error != NULL)
5816 goto done;
5817 error = got_ref_resolve(&commit_id, repo, ref);
5818 got_ref_close(ref);
5819 if (error != NULL)
5820 goto done;
5822 error = got_object_id_str(&commit_id_str, commit_id);
5823 if (error)
5824 goto done;
5826 error = got_ref_open(&head_ref, repo,
5827 got_worktree_get_head_ref_name(worktree), 0);
5828 if (error != NULL)
5829 goto done;
5831 error = check_same_branch(commit_id, head_ref, NULL, repo);
5832 if (error)
5833 goto done;
5835 error = got_object_open_as_commit(&commit, repo, commit_id);
5836 if (error)
5837 goto done;
5838 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5839 if (pid == NULL) {
5840 error = got_error(GOT_ERR_ROOT_COMMIT);
5841 goto done;
5844 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5845 update_progress, &did_something, check_cancelled, NULL);
5846 if (error != NULL)
5847 goto done;
5849 if (did_something)
5850 printf("Backed out commit %s\n", commit_id_str);
5851 done:
5852 if (commit)
5853 got_object_commit_close(commit);
5854 free(commit_id_str);
5855 if (head_ref)
5856 got_ref_close(head_ref);
5857 if (worktree)
5858 got_worktree_close(worktree);
5859 if (repo)
5860 got_repo_close(repo);
5861 return error;
5864 __dead static void
5865 usage_rebase(void)
5867 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5868 getprogname());
5869 exit(1);
5872 void
5873 trim_logmsg(char *logmsg, int limit)
5875 char *nl;
5876 size_t len;
5878 len = strlen(logmsg);
5879 if (len > limit)
5880 len = limit;
5881 logmsg[len] = '\0';
5882 nl = strchr(logmsg, '\n');
5883 if (nl)
5884 *nl = '\0';
5887 static const struct got_error *
5888 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5890 const struct got_error *err;
5891 char *logmsg0 = NULL;
5892 const char *s;
5894 err = got_object_commit_get_logmsg(&logmsg0, commit);
5895 if (err)
5896 return err;
5898 s = logmsg0;
5899 while (isspace((unsigned char)s[0]))
5900 s++;
5902 *logmsg = strdup(s);
5903 if (*logmsg == NULL) {
5904 err = got_error_from_errno("strdup");
5905 goto done;
5908 trim_logmsg(*logmsg, limit);
5909 done:
5910 free(logmsg0);
5911 return err;
5914 static const struct got_error *
5915 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5917 const struct got_error *err;
5918 struct got_commit_object *commit = NULL;
5919 char *id_str = NULL, *logmsg = NULL;
5921 err = got_object_open_as_commit(&commit, repo, id);
5922 if (err)
5923 return err;
5925 err = got_object_id_str(&id_str, id);
5926 if (err)
5927 goto done;
5929 id_str[12] = '\0';
5931 err = get_short_logmsg(&logmsg, 42, commit);
5932 if (err)
5933 goto done;
5935 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5936 done:
5937 free(id_str);
5938 got_object_commit_close(commit);
5939 free(logmsg);
5940 return err;
5943 static const struct got_error *
5944 show_rebase_progress(struct got_commit_object *commit,
5945 struct got_object_id *old_id, struct got_object_id *new_id)
5947 const struct got_error *err;
5948 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5950 err = got_object_id_str(&old_id_str, old_id);
5951 if (err)
5952 goto done;
5954 if (new_id) {
5955 err = got_object_id_str(&new_id_str, new_id);
5956 if (err)
5957 goto done;
5960 old_id_str[12] = '\0';
5961 if (new_id_str)
5962 new_id_str[12] = '\0';
5964 err = get_short_logmsg(&logmsg, 42, commit);
5965 if (err)
5966 goto done;
5968 printf("%s -> %s: %s\n", old_id_str,
5969 new_id_str ? new_id_str : "no-op change", logmsg);
5970 done:
5971 free(old_id_str);
5972 free(new_id_str);
5973 free(logmsg);
5974 return err;
5977 static const struct got_error *
5978 rebase_progress(void *arg, unsigned char status, const char *path)
5980 unsigned char *rebase_status = arg;
5982 while (path[0] == '/')
5983 path++;
5984 printf("%c %s\n", status, path);
5986 if (*rebase_status == GOT_STATUS_CONFLICT)
5987 return NULL;
5988 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5989 *rebase_status = status;
5990 return NULL;
5993 static const struct got_error *
5994 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5995 struct got_reference *branch, struct got_reference *new_base_branch,
5996 struct got_reference *tmp_branch, struct got_repository *repo)
5998 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5999 return got_worktree_rebase_complete(worktree, fileindex,
6000 new_base_branch, tmp_branch, branch, repo);
6003 static const struct got_error *
6004 rebase_commit(struct got_pathlist_head *merged_paths,
6005 struct got_worktree *worktree, struct got_fileindex *fileindex,
6006 struct got_reference *tmp_branch,
6007 struct got_object_id *commit_id, struct got_repository *repo)
6009 const struct got_error *error;
6010 struct got_commit_object *commit;
6011 struct got_object_id *new_commit_id;
6013 error = got_object_open_as_commit(&commit, repo, commit_id);
6014 if (error)
6015 return error;
6017 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6018 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6019 if (error) {
6020 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6021 goto done;
6022 error = show_rebase_progress(commit, commit_id, NULL);
6023 } else {
6024 error = show_rebase_progress(commit, commit_id, new_commit_id);
6025 free(new_commit_id);
6027 done:
6028 got_object_commit_close(commit);
6029 return error;
6032 struct check_path_prefix_arg {
6033 const char *path_prefix;
6034 size_t len;
6035 int errcode;
6038 static const struct got_error *
6039 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6040 struct got_blob_object *blob2, struct got_object_id *id1,
6041 struct got_object_id *id2, const char *path1, const char *path2,
6042 mode_t mode1, mode_t mode2, struct got_repository *repo)
6044 struct check_path_prefix_arg *a = arg;
6046 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6047 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6048 return got_error(a->errcode);
6050 return NULL;
6053 static const struct got_error *
6054 check_path_prefix(struct got_object_id *parent_id,
6055 struct got_object_id *commit_id, const char *path_prefix,
6056 int errcode, struct got_repository *repo)
6058 const struct got_error *err;
6059 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6060 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6061 struct check_path_prefix_arg cpp_arg;
6063 if (got_path_is_root_dir(path_prefix))
6064 return NULL;
6066 err = got_object_open_as_commit(&commit, repo, commit_id);
6067 if (err)
6068 goto done;
6070 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6071 if (err)
6072 goto done;
6074 err = got_object_open_as_tree(&tree1, repo,
6075 got_object_commit_get_tree_id(parent_commit));
6076 if (err)
6077 goto done;
6079 err = got_object_open_as_tree(&tree2, repo,
6080 got_object_commit_get_tree_id(commit));
6081 if (err)
6082 goto done;
6084 cpp_arg.path_prefix = path_prefix;
6085 while (cpp_arg.path_prefix[0] == '/')
6086 cpp_arg.path_prefix++;
6087 cpp_arg.len = strlen(cpp_arg.path_prefix);
6088 cpp_arg.errcode = errcode;
6089 err = got_diff_tree(tree1, tree2, "", "", repo,
6090 check_path_prefix_in_diff, &cpp_arg, 0);
6091 done:
6092 if (tree1)
6093 got_object_tree_close(tree1);
6094 if (tree2)
6095 got_object_tree_close(tree2);
6096 if (commit)
6097 got_object_commit_close(commit);
6098 if (parent_commit)
6099 got_object_commit_close(parent_commit);
6100 return err;
6103 static const struct got_error *
6104 collect_commits(struct got_object_id_queue *commits,
6105 struct got_object_id *initial_commit_id,
6106 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6107 const char *path_prefix, int path_prefix_errcode,
6108 struct got_repository *repo)
6110 const struct got_error *err = NULL;
6111 struct got_commit_graph *graph = NULL;
6112 struct got_object_id *parent_id = NULL;
6113 struct got_object_qid *qid;
6114 struct got_object_id *commit_id = initial_commit_id;
6116 err = got_commit_graph_open(&graph, "/", 1);
6117 if (err)
6118 return err;
6120 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6121 check_cancelled, NULL);
6122 if (err)
6123 goto done;
6124 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6125 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6126 check_cancelled, NULL);
6127 if (err) {
6128 if (err->code == GOT_ERR_ITER_COMPLETED) {
6129 err = got_error_msg(GOT_ERR_ANCESTRY,
6130 "ran out of commits to rebase before "
6131 "youngest common ancestor commit has "
6132 "been reached?!?");
6134 goto done;
6135 } else {
6136 err = check_path_prefix(parent_id, commit_id,
6137 path_prefix, path_prefix_errcode, repo);
6138 if (err)
6139 goto done;
6141 err = got_object_qid_alloc(&qid, commit_id);
6142 if (err)
6143 goto done;
6144 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6145 commit_id = parent_id;
6148 done:
6149 got_commit_graph_close(graph);
6150 return err;
6153 static const struct got_error *
6154 cmd_rebase(int argc, char *argv[])
6156 const struct got_error *error = NULL;
6157 struct got_worktree *worktree = NULL;
6158 struct got_repository *repo = NULL;
6159 struct got_fileindex *fileindex = NULL;
6160 char *cwd = NULL;
6161 struct got_reference *branch = NULL;
6162 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6163 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6164 struct got_object_id *resume_commit_id = NULL;
6165 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6166 struct got_commit_object *commit = NULL;
6167 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6168 int histedit_in_progress = 0;
6169 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6170 struct got_object_id_queue commits;
6171 struct got_pathlist_head merged_paths;
6172 const struct got_object_id_queue *parent_ids;
6173 struct got_object_qid *qid, *pid;
6175 SIMPLEQ_INIT(&commits);
6176 TAILQ_INIT(&merged_paths);
6178 while ((ch = getopt(argc, argv, "ac")) != -1) {
6179 switch (ch) {
6180 case 'a':
6181 abort_rebase = 1;
6182 break;
6183 case 'c':
6184 continue_rebase = 1;
6185 break;
6186 default:
6187 usage_rebase();
6188 /* NOTREACHED */
6192 argc -= optind;
6193 argv += optind;
6195 #ifndef PROFILE
6196 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6197 "unveil", NULL) == -1)
6198 err(1, "pledge");
6199 #endif
6200 if (abort_rebase && continue_rebase)
6201 usage_rebase();
6202 else if (abort_rebase || continue_rebase) {
6203 if (argc != 0)
6204 usage_rebase();
6205 } else if (argc != 1)
6206 usage_rebase();
6208 cwd = getcwd(NULL, 0);
6209 if (cwd == NULL) {
6210 error = got_error_from_errno("getcwd");
6211 goto done;
6213 error = got_worktree_open(&worktree, cwd);
6214 if (error)
6215 goto done;
6217 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6218 NULL);
6219 if (error != NULL)
6220 goto done;
6222 error = apply_unveil(got_repo_get_path(repo), 0,
6223 got_worktree_get_root_path(worktree));
6224 if (error)
6225 goto done;
6227 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6228 worktree);
6229 if (error)
6230 goto done;
6231 if (histedit_in_progress) {
6232 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6233 goto done;
6236 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6237 if (error)
6238 goto done;
6240 if (abort_rebase) {
6241 int did_something;
6242 if (!rebase_in_progress) {
6243 error = got_error(GOT_ERR_NOT_REBASING);
6244 goto done;
6246 error = got_worktree_rebase_continue(&resume_commit_id,
6247 &new_base_branch, &tmp_branch, &branch, &fileindex,
6248 worktree, repo);
6249 if (error)
6250 goto done;
6251 printf("Switching work tree to %s\n",
6252 got_ref_get_symref_target(new_base_branch));
6253 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6254 new_base_branch, update_progress, &did_something);
6255 if (error)
6256 goto done;
6257 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6258 goto done; /* nothing else to do */
6261 if (continue_rebase) {
6262 if (!rebase_in_progress) {
6263 error = got_error(GOT_ERR_NOT_REBASING);
6264 goto done;
6266 error = got_worktree_rebase_continue(&resume_commit_id,
6267 &new_base_branch, &tmp_branch, &branch, &fileindex,
6268 worktree, repo);
6269 if (error)
6270 goto done;
6272 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6273 resume_commit_id, repo);
6274 if (error)
6275 goto done;
6277 yca_id = got_object_id_dup(resume_commit_id);
6278 if (yca_id == NULL) {
6279 error = got_error_from_errno("got_object_id_dup");
6280 goto done;
6282 } else {
6283 error = got_ref_open(&branch, repo, argv[0], 0);
6284 if (error != NULL)
6285 goto done;
6288 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6289 if (error)
6290 goto done;
6292 if (!continue_rebase) {
6293 struct got_object_id *base_commit_id;
6295 base_commit_id = got_worktree_get_base_commit_id(worktree);
6296 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6297 base_commit_id, branch_head_commit_id, repo,
6298 check_cancelled, NULL);
6299 if (error)
6300 goto done;
6301 if (yca_id == NULL) {
6302 error = got_error_msg(GOT_ERR_ANCESTRY,
6303 "specified branch shares no common ancestry "
6304 "with work tree's branch");
6305 goto done;
6308 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6309 if (error) {
6310 if (error->code != GOT_ERR_ANCESTRY)
6311 goto done;
6312 error = NULL;
6313 } else {
6314 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6315 "specified branch resolves to a commit which "
6316 "is already contained in work tree's branch");
6317 goto done;
6319 error = got_worktree_rebase_prepare(&new_base_branch,
6320 &tmp_branch, &fileindex, worktree, branch, repo);
6321 if (error)
6322 goto done;
6325 commit_id = branch_head_commit_id;
6326 error = got_object_open_as_commit(&commit, repo, commit_id);
6327 if (error)
6328 goto done;
6330 parent_ids = got_object_commit_get_parent_ids(commit);
6331 pid = SIMPLEQ_FIRST(parent_ids);
6332 if (pid == NULL) {
6333 if (!continue_rebase) {
6334 int did_something;
6335 error = got_worktree_rebase_abort(worktree, fileindex,
6336 repo, new_base_branch, update_progress,
6337 &did_something);
6338 if (error)
6339 goto done;
6340 printf("Rebase of %s aborted\n",
6341 got_ref_get_name(branch));
6343 error = got_error(GOT_ERR_EMPTY_REBASE);
6344 goto done;
6346 error = collect_commits(&commits, commit_id, pid->id,
6347 yca_id, got_worktree_get_path_prefix(worktree),
6348 GOT_ERR_REBASE_PATH, repo);
6349 got_object_commit_close(commit);
6350 commit = NULL;
6351 if (error)
6352 goto done;
6354 if (SIMPLEQ_EMPTY(&commits)) {
6355 if (continue_rebase) {
6356 error = rebase_complete(worktree, fileindex,
6357 branch, new_base_branch, tmp_branch, repo);
6358 goto done;
6359 } else {
6360 /* Fast-forward the reference of the branch. */
6361 struct got_object_id *new_head_commit_id;
6362 char *id_str;
6363 error = got_ref_resolve(&new_head_commit_id, repo,
6364 new_base_branch);
6365 if (error)
6366 goto done;
6367 error = got_object_id_str(&id_str, new_head_commit_id);
6368 printf("Forwarding %s to commit %s\n",
6369 got_ref_get_name(branch), id_str);
6370 free(id_str);
6371 error = got_ref_change_ref(branch,
6372 new_head_commit_id);
6373 if (error)
6374 goto done;
6378 pid = NULL;
6379 SIMPLEQ_FOREACH(qid, &commits, entry) {
6380 commit_id = qid->id;
6381 parent_id = pid ? pid->id : yca_id;
6382 pid = qid;
6384 error = got_worktree_rebase_merge_files(&merged_paths,
6385 worktree, fileindex, parent_id, commit_id, repo,
6386 rebase_progress, &rebase_status, check_cancelled, NULL);
6387 if (error)
6388 goto done;
6390 if (rebase_status == GOT_STATUS_CONFLICT) {
6391 error = show_rebase_merge_conflict(qid->id, repo);
6392 if (error)
6393 goto done;
6394 got_worktree_rebase_pathlist_free(&merged_paths);
6395 break;
6398 error = rebase_commit(&merged_paths, worktree, fileindex,
6399 tmp_branch, commit_id, repo);
6400 got_worktree_rebase_pathlist_free(&merged_paths);
6401 if (error)
6402 goto done;
6405 if (rebase_status == GOT_STATUS_CONFLICT) {
6406 error = got_worktree_rebase_postpone(worktree, fileindex);
6407 if (error)
6408 goto done;
6409 error = got_error_msg(GOT_ERR_CONFLICTS,
6410 "conflicts must be resolved before rebasing can continue");
6411 } else
6412 error = rebase_complete(worktree, fileindex, branch,
6413 new_base_branch, tmp_branch, repo);
6414 done:
6415 got_object_id_queue_free(&commits);
6416 free(branch_head_commit_id);
6417 free(resume_commit_id);
6418 free(yca_id);
6419 if (commit)
6420 got_object_commit_close(commit);
6421 if (branch)
6422 got_ref_close(branch);
6423 if (new_base_branch)
6424 got_ref_close(new_base_branch);
6425 if (tmp_branch)
6426 got_ref_close(tmp_branch);
6427 if (worktree)
6428 got_worktree_close(worktree);
6429 if (repo)
6430 got_repo_close(repo);
6431 return error;
6434 __dead static void
6435 usage_histedit(void)
6437 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6438 getprogname());
6439 exit(1);
6442 #define GOT_HISTEDIT_PICK 'p'
6443 #define GOT_HISTEDIT_EDIT 'e'
6444 #define GOT_HISTEDIT_FOLD 'f'
6445 #define GOT_HISTEDIT_DROP 'd'
6446 #define GOT_HISTEDIT_MESG 'm'
6448 static struct got_histedit_cmd {
6449 unsigned char code;
6450 const char *name;
6451 const char *desc;
6452 } got_histedit_cmds[] = {
6453 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6454 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6455 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6456 "be used" },
6457 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6458 { GOT_HISTEDIT_MESG, "mesg",
6459 "single-line log message for commit above (open editor if empty)" },
6462 struct got_histedit_list_entry {
6463 TAILQ_ENTRY(got_histedit_list_entry) entry;
6464 struct got_object_id *commit_id;
6465 const struct got_histedit_cmd *cmd;
6466 char *logmsg;
6468 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6470 static const struct got_error *
6471 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6472 FILE *f, struct got_repository *repo)
6474 const struct got_error *err = NULL;
6475 char *logmsg = NULL, *id_str = NULL;
6476 struct got_commit_object *commit = NULL;
6477 int n;
6479 err = got_object_open_as_commit(&commit, repo, commit_id);
6480 if (err)
6481 goto done;
6483 err = get_short_logmsg(&logmsg, 34, commit);
6484 if (err)
6485 goto done;
6487 err = got_object_id_str(&id_str, commit_id);
6488 if (err)
6489 goto done;
6491 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6492 if (n < 0)
6493 err = got_ferror(f, GOT_ERR_IO);
6494 done:
6495 if (commit)
6496 got_object_commit_close(commit);
6497 free(id_str);
6498 free(logmsg);
6499 return err;
6502 static const struct got_error *
6503 histedit_write_commit_list(struct got_object_id_queue *commits,
6504 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6506 const struct got_error *err = NULL;
6507 struct got_object_qid *qid;
6509 if (SIMPLEQ_EMPTY(commits))
6510 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6512 SIMPLEQ_FOREACH(qid, commits, entry) {
6513 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6514 f, repo);
6515 if (err)
6516 break;
6517 if (edit_logmsg_only) {
6518 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6519 if (n < 0) {
6520 err = got_ferror(f, GOT_ERR_IO);
6521 break;
6526 return err;
6529 static const struct got_error *
6530 write_cmd_list(FILE *f, const char *branch_name,
6531 struct got_object_id_queue *commits)
6533 const struct got_error *err = NULL;
6534 int n, i;
6535 char *id_str;
6536 struct got_object_qid *qid;
6538 qid = SIMPLEQ_FIRST(commits);
6539 err = got_object_id_str(&id_str, qid->id);
6540 if (err)
6541 return err;
6543 n = fprintf(f,
6544 "# Editing the history of branch '%s' starting at\n"
6545 "# commit %s\n"
6546 "# Commits will be processed in order from top to "
6547 "bottom of this file.\n", branch_name, id_str);
6548 if (n < 0) {
6549 err = got_ferror(f, GOT_ERR_IO);
6550 goto done;
6553 n = fprintf(f, "# Available histedit commands:\n");
6554 if (n < 0) {
6555 err = got_ferror(f, GOT_ERR_IO);
6556 goto done;
6559 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6560 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6561 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6562 cmd->desc);
6563 if (n < 0) {
6564 err = got_ferror(f, GOT_ERR_IO);
6565 break;
6568 done:
6569 free(id_str);
6570 return err;
6573 static const struct got_error *
6574 histedit_syntax_error(int lineno)
6576 static char msg[42];
6577 int ret;
6579 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6580 lineno);
6581 if (ret == -1 || ret >= sizeof(msg))
6582 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6584 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6587 static const struct got_error *
6588 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6589 char *logmsg, struct got_repository *repo)
6591 const struct got_error *err;
6592 struct got_commit_object *folded_commit = NULL;
6593 char *id_str, *folded_logmsg = NULL;
6595 err = got_object_id_str(&id_str, hle->commit_id);
6596 if (err)
6597 return err;
6599 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6600 if (err)
6601 goto done;
6603 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6604 if (err)
6605 goto done;
6606 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6607 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6608 folded_logmsg) == -1) {
6609 err = got_error_from_errno("asprintf");
6611 done:
6612 if (folded_commit)
6613 got_object_commit_close(folded_commit);
6614 free(id_str);
6615 free(folded_logmsg);
6616 return err;
6619 static struct got_histedit_list_entry *
6620 get_folded_commits(struct got_histedit_list_entry *hle)
6622 struct got_histedit_list_entry *prev, *folded = NULL;
6624 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6625 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6626 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6627 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6628 folded = prev;
6629 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6632 return folded;
6635 static const struct got_error *
6636 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6637 struct got_repository *repo)
6639 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6640 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6641 const struct got_error *err = NULL;
6642 struct got_commit_object *commit = NULL;
6643 int fd;
6644 struct got_histedit_list_entry *folded = NULL;
6646 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6647 if (err)
6648 return err;
6650 folded = get_folded_commits(hle);
6651 if (folded) {
6652 while (folded != hle) {
6653 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6654 folded = TAILQ_NEXT(folded, entry);
6655 continue;
6657 err = append_folded_commit_msg(&new_msg, folded,
6658 logmsg, repo);
6659 if (err)
6660 goto done;
6661 free(logmsg);
6662 logmsg = new_msg;
6663 folded = TAILQ_NEXT(folded, entry);
6667 err = got_object_id_str(&id_str, hle->commit_id);
6668 if (err)
6669 goto done;
6670 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6671 if (err)
6672 goto done;
6673 if (asprintf(&new_msg,
6674 "%s\n# original log message of commit %s: %s",
6675 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6676 err = got_error_from_errno("asprintf");
6677 goto done;
6679 free(logmsg);
6680 logmsg = new_msg;
6682 err = got_object_id_str(&id_str, hle->commit_id);
6683 if (err)
6684 goto done;
6686 err = got_opentemp_named_fd(&logmsg_path, &fd,
6687 GOT_TMPDIR_STR "/got-logmsg");
6688 if (err)
6689 goto done;
6691 dprintf(fd, logmsg);
6692 close(fd);
6694 err = get_editor(&editor);
6695 if (err)
6696 goto done;
6698 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6699 if (err) {
6700 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6701 goto done;
6702 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6704 done:
6705 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6706 err = got_error_from_errno2("unlink", logmsg_path);
6707 free(logmsg_path);
6708 free(logmsg);
6709 free(orig_logmsg);
6710 free(editor);
6711 if (commit)
6712 got_object_commit_close(commit);
6713 return err;
6716 static const struct got_error *
6717 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6718 FILE *f, struct got_repository *repo)
6720 const struct got_error *err = NULL;
6721 char *line = NULL, *p, *end;
6722 size_t size;
6723 ssize_t len;
6724 int lineno = 0, i;
6725 const struct got_histedit_cmd *cmd;
6726 struct got_object_id *commit_id = NULL;
6727 struct got_histedit_list_entry *hle = NULL;
6729 for (;;) {
6730 len = getline(&line, &size, f);
6731 if (len == -1) {
6732 const struct got_error *getline_err;
6733 if (feof(f))
6734 break;
6735 getline_err = got_error_from_errno("getline");
6736 err = got_ferror(f, getline_err->code);
6737 break;
6739 lineno++;
6740 p = line;
6741 while (isspace((unsigned char)p[0]))
6742 p++;
6743 if (p[0] == '#' || p[0] == '\0') {
6744 free(line);
6745 line = NULL;
6746 continue;
6748 cmd = NULL;
6749 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6750 cmd = &got_histedit_cmds[i];
6751 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6752 isspace((unsigned char)p[strlen(cmd->name)])) {
6753 p += strlen(cmd->name);
6754 break;
6756 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6757 p++;
6758 break;
6761 if (i == nitems(got_histedit_cmds)) {
6762 err = histedit_syntax_error(lineno);
6763 break;
6765 while (isspace((unsigned char)p[0]))
6766 p++;
6767 if (cmd->code == GOT_HISTEDIT_MESG) {
6768 if (hle == NULL || hle->logmsg != NULL) {
6769 err = got_error(GOT_ERR_HISTEDIT_CMD);
6770 break;
6772 if (p[0] == '\0') {
6773 err = histedit_edit_logmsg(hle, repo);
6774 if (err)
6775 break;
6776 } else {
6777 hle->logmsg = strdup(p);
6778 if (hle->logmsg == NULL) {
6779 err = got_error_from_errno("strdup");
6780 break;
6783 free(line);
6784 line = NULL;
6785 continue;
6786 } else {
6787 end = p;
6788 while (end[0] && !isspace((unsigned char)end[0]))
6789 end++;
6790 *end = '\0';
6792 err = got_object_resolve_id_str(&commit_id, repo, p);
6793 if (err) {
6794 /* override error code */
6795 err = histedit_syntax_error(lineno);
6796 break;
6799 hle = malloc(sizeof(*hle));
6800 if (hle == NULL) {
6801 err = got_error_from_errno("malloc");
6802 break;
6804 hle->cmd = cmd;
6805 hle->commit_id = commit_id;
6806 hle->logmsg = NULL;
6807 commit_id = NULL;
6808 free(line);
6809 line = NULL;
6810 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6813 free(line);
6814 free(commit_id);
6815 return err;
6818 static const struct got_error *
6819 histedit_check_script(struct got_histedit_list *histedit_cmds,
6820 struct got_object_id_queue *commits, struct got_repository *repo)
6822 const struct got_error *err = NULL;
6823 struct got_object_qid *qid;
6824 struct got_histedit_list_entry *hle;
6825 static char msg[92];
6826 char *id_str;
6828 if (TAILQ_EMPTY(histedit_cmds))
6829 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6830 "histedit script contains no commands");
6831 if (SIMPLEQ_EMPTY(commits))
6832 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6834 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6835 struct got_histedit_list_entry *hle2;
6836 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6837 if (hle == hle2)
6838 continue;
6839 if (got_object_id_cmp(hle->commit_id,
6840 hle2->commit_id) != 0)
6841 continue;
6842 err = got_object_id_str(&id_str, hle->commit_id);
6843 if (err)
6844 return err;
6845 snprintf(msg, sizeof(msg), "commit %s is listed "
6846 "more than once in histedit script", id_str);
6847 free(id_str);
6848 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6852 SIMPLEQ_FOREACH(qid, commits, entry) {
6853 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6854 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6855 break;
6857 if (hle == NULL) {
6858 err = got_object_id_str(&id_str, qid->id);
6859 if (err)
6860 return err;
6861 snprintf(msg, sizeof(msg),
6862 "commit %s missing from histedit script", id_str);
6863 free(id_str);
6864 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6868 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6869 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6870 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6871 "last commit in histedit script cannot be folded");
6873 return NULL;
6876 static const struct got_error *
6877 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6878 const char *path, struct got_object_id_queue *commits,
6879 struct got_repository *repo)
6881 const struct got_error *err = NULL;
6882 char *editor;
6883 FILE *f = NULL;
6885 err = get_editor(&editor);
6886 if (err)
6887 return err;
6889 if (spawn_editor(editor, path) == -1) {
6890 err = got_error_from_errno("failed spawning editor");
6891 goto done;
6894 f = fopen(path, "r");
6895 if (f == NULL) {
6896 err = got_error_from_errno("fopen");
6897 goto done;
6899 err = histedit_parse_list(histedit_cmds, f, repo);
6900 if (err)
6901 goto done;
6903 err = histedit_check_script(histedit_cmds, commits, repo);
6904 done:
6905 if (f && fclose(f) != 0 && err == NULL)
6906 err = got_error_from_errno("fclose");
6907 free(editor);
6908 return err;
6911 static const struct got_error *
6912 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6913 struct got_object_id_queue *, const char *, const char *,
6914 struct got_repository *);
6916 static const struct got_error *
6917 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6918 struct got_object_id_queue *commits, const char *branch_name,
6919 int edit_logmsg_only, struct got_repository *repo)
6921 const struct got_error *err;
6922 FILE *f = NULL;
6923 char *path = NULL;
6925 err = got_opentemp_named(&path, &f, "got-histedit");
6926 if (err)
6927 return err;
6929 err = write_cmd_list(f, branch_name, commits);
6930 if (err)
6931 goto done;
6933 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6934 if (err)
6935 goto done;
6937 if (edit_logmsg_only) {
6938 rewind(f);
6939 err = histedit_parse_list(histedit_cmds, f, repo);
6940 } else {
6941 if (fclose(f) != 0) {
6942 err = got_error_from_errno("fclose");
6943 goto done;
6945 f = NULL;
6946 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6947 if (err) {
6948 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6949 err->code != GOT_ERR_HISTEDIT_CMD)
6950 goto done;
6951 err = histedit_edit_list_retry(histedit_cmds, err,
6952 commits, path, branch_name, repo);
6955 done:
6956 if (f && fclose(f) != 0 && err == NULL)
6957 err = got_error_from_errno("fclose");
6958 if (path && unlink(path) != 0 && err == NULL)
6959 err = got_error_from_errno2("unlink", path);
6960 free(path);
6961 return err;
6964 static const struct got_error *
6965 histedit_save_list(struct got_histedit_list *histedit_cmds,
6966 struct got_worktree *worktree, struct got_repository *repo)
6968 const struct got_error *err = NULL;
6969 char *path = NULL;
6970 FILE *f = NULL;
6971 struct got_histedit_list_entry *hle;
6972 struct got_commit_object *commit = NULL;
6974 err = got_worktree_get_histedit_script_path(&path, worktree);
6975 if (err)
6976 return err;
6978 f = fopen(path, "w");
6979 if (f == NULL) {
6980 err = got_error_from_errno2("fopen", path);
6981 goto done;
6983 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6984 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6985 repo);
6986 if (err)
6987 break;
6989 if (hle->logmsg) {
6990 int n = fprintf(f, "%c %s\n",
6991 GOT_HISTEDIT_MESG, hle->logmsg);
6992 if (n < 0) {
6993 err = got_ferror(f, GOT_ERR_IO);
6994 break;
6998 done:
6999 if (f && fclose(f) != 0 && err == NULL)
7000 err = got_error_from_errno("fclose");
7001 free(path);
7002 if (commit)
7003 got_object_commit_close(commit);
7004 return err;
7007 void
7008 histedit_free_list(struct got_histedit_list *histedit_cmds)
7010 struct got_histedit_list_entry *hle;
7012 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7013 TAILQ_REMOVE(histedit_cmds, hle, entry);
7014 free(hle);
7018 static const struct got_error *
7019 histedit_load_list(struct got_histedit_list *histedit_cmds,
7020 const char *path, struct got_repository *repo)
7022 const struct got_error *err = NULL;
7023 FILE *f = NULL;
7025 f = fopen(path, "r");
7026 if (f == NULL) {
7027 err = got_error_from_errno2("fopen", path);
7028 goto done;
7031 err = histedit_parse_list(histedit_cmds, f, repo);
7032 done:
7033 if (f && fclose(f) != 0 && err == NULL)
7034 err = got_error_from_errno("fclose");
7035 return err;
7038 static const struct got_error *
7039 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7040 const struct got_error *edit_err, struct got_object_id_queue *commits,
7041 const char *path, const char *branch_name, struct got_repository *repo)
7043 const struct got_error *err = NULL, *prev_err = edit_err;
7044 int resp = ' ';
7046 while (resp != 'c' && resp != 'r' && resp != 'a') {
7047 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7048 "or (a)bort: ", getprogname(), prev_err->msg);
7049 resp = getchar();
7050 if (resp == '\n')
7051 resp = getchar();
7052 if (resp == 'c') {
7053 histedit_free_list(histedit_cmds);
7054 err = histedit_run_editor(histedit_cmds, path, commits,
7055 repo);
7056 if (err) {
7057 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7058 err->code != GOT_ERR_HISTEDIT_CMD)
7059 break;
7060 prev_err = err;
7061 resp = ' ';
7062 continue;
7064 break;
7065 } else if (resp == 'r') {
7066 histedit_free_list(histedit_cmds);
7067 err = histedit_edit_script(histedit_cmds,
7068 commits, branch_name, 0, repo);
7069 if (err) {
7070 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7071 err->code != GOT_ERR_HISTEDIT_CMD)
7072 break;
7073 prev_err = err;
7074 resp = ' ';
7075 continue;
7077 break;
7078 } else if (resp == 'a') {
7079 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7080 break;
7081 } else
7082 printf("invalid response '%c'\n", resp);
7085 return err;
7088 static const struct got_error *
7089 histedit_complete(struct got_worktree *worktree,
7090 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7091 struct got_reference *branch, struct got_repository *repo)
7093 printf("Switching work tree to %s\n",
7094 got_ref_get_symref_target(branch));
7095 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7096 branch, repo);
7099 static const struct got_error *
7100 show_histedit_progress(struct got_commit_object *commit,
7101 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7103 const struct got_error *err;
7104 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7106 err = got_object_id_str(&old_id_str, hle->commit_id);
7107 if (err)
7108 goto done;
7110 if (new_id) {
7111 err = got_object_id_str(&new_id_str, new_id);
7112 if (err)
7113 goto done;
7116 old_id_str[12] = '\0';
7117 if (new_id_str)
7118 new_id_str[12] = '\0';
7120 if (hle->logmsg) {
7121 logmsg = strdup(hle->logmsg);
7122 if (logmsg == NULL) {
7123 err = got_error_from_errno("strdup");
7124 goto done;
7126 trim_logmsg(logmsg, 42);
7127 } else {
7128 err = get_short_logmsg(&logmsg, 42, commit);
7129 if (err)
7130 goto done;
7133 switch (hle->cmd->code) {
7134 case GOT_HISTEDIT_PICK:
7135 case GOT_HISTEDIT_EDIT:
7136 printf("%s -> %s: %s\n", old_id_str,
7137 new_id_str ? new_id_str : "no-op change", logmsg);
7138 break;
7139 case GOT_HISTEDIT_DROP:
7140 case GOT_HISTEDIT_FOLD:
7141 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7142 logmsg);
7143 break;
7144 default:
7145 break;
7147 done:
7148 free(old_id_str);
7149 free(new_id_str);
7150 return err;
7153 static const struct got_error *
7154 histedit_commit(struct got_pathlist_head *merged_paths,
7155 struct got_worktree *worktree, struct got_fileindex *fileindex,
7156 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7157 struct got_repository *repo)
7159 const struct got_error *err;
7160 struct got_commit_object *commit;
7161 struct got_object_id *new_commit_id;
7163 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7164 && hle->logmsg == NULL) {
7165 err = histedit_edit_logmsg(hle, repo);
7166 if (err)
7167 return err;
7170 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7171 if (err)
7172 return err;
7174 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7175 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7176 hle->logmsg, repo);
7177 if (err) {
7178 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7179 goto done;
7180 err = show_histedit_progress(commit, hle, NULL);
7181 } else {
7182 err = show_histedit_progress(commit, hle, new_commit_id);
7183 free(new_commit_id);
7185 done:
7186 got_object_commit_close(commit);
7187 return err;
7190 static const struct got_error *
7191 histedit_skip_commit(struct got_histedit_list_entry *hle,
7192 struct got_worktree *worktree, struct got_repository *repo)
7194 const struct got_error *error;
7195 struct got_commit_object *commit;
7197 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7198 repo);
7199 if (error)
7200 return error;
7202 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7203 if (error)
7204 return error;
7206 error = show_histedit_progress(commit, hle, NULL);
7207 got_object_commit_close(commit);
7208 return error;
7211 static const struct got_error *
7212 check_local_changes(void *arg, unsigned char status,
7213 unsigned char staged_status, const char *path,
7214 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7215 struct got_object_id *commit_id, int dirfd, const char *de_name)
7217 int *have_local_changes = arg;
7219 switch (status) {
7220 case GOT_STATUS_ADD:
7221 case GOT_STATUS_DELETE:
7222 case GOT_STATUS_MODIFY:
7223 case GOT_STATUS_CONFLICT:
7224 *have_local_changes = 1;
7225 return got_error(GOT_ERR_CANCELLED);
7226 default:
7227 break;
7230 switch (staged_status) {
7231 case GOT_STATUS_ADD:
7232 case GOT_STATUS_DELETE:
7233 case GOT_STATUS_MODIFY:
7234 *have_local_changes = 1;
7235 return got_error(GOT_ERR_CANCELLED);
7236 default:
7237 break;
7240 return NULL;
7243 static const struct got_error *
7244 cmd_histedit(int argc, char *argv[])
7246 const struct got_error *error = NULL;
7247 struct got_worktree *worktree = NULL;
7248 struct got_fileindex *fileindex = NULL;
7249 struct got_repository *repo = NULL;
7250 char *cwd = NULL;
7251 struct got_reference *branch = NULL;
7252 struct got_reference *tmp_branch = NULL;
7253 struct got_object_id *resume_commit_id = NULL;
7254 struct got_object_id *base_commit_id = NULL;
7255 struct got_object_id *head_commit_id = NULL;
7256 struct got_commit_object *commit = NULL;
7257 int ch, rebase_in_progress = 0, did_something;
7258 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7259 int edit_logmsg_only = 0;
7260 const char *edit_script_path = NULL;
7261 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7262 struct got_object_id_queue commits;
7263 struct got_pathlist_head merged_paths;
7264 const struct got_object_id_queue *parent_ids;
7265 struct got_object_qid *pid;
7266 struct got_histedit_list histedit_cmds;
7267 struct got_histedit_list_entry *hle;
7269 SIMPLEQ_INIT(&commits);
7270 TAILQ_INIT(&histedit_cmds);
7271 TAILQ_INIT(&merged_paths);
7273 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7274 switch (ch) {
7275 case 'a':
7276 abort_edit = 1;
7277 break;
7278 case 'c':
7279 continue_edit = 1;
7280 break;
7281 case 'F':
7282 edit_script_path = optarg;
7283 break;
7284 case 'm':
7285 edit_logmsg_only = 1;
7286 break;
7287 default:
7288 usage_histedit();
7289 /* NOTREACHED */
7293 argc -= optind;
7294 argv += optind;
7296 #ifndef PROFILE
7297 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7298 "unveil", NULL) == -1)
7299 err(1, "pledge");
7300 #endif
7301 if (abort_edit && continue_edit)
7302 errx(1, "histedit's -a and -c options are mutually exclusive");
7303 if (edit_script_path && edit_logmsg_only)
7304 errx(1, "histedit's -F and -m options are mutually exclusive");
7305 if (abort_edit && edit_logmsg_only)
7306 errx(1, "histedit's -a and -m options are mutually exclusive");
7307 if (continue_edit && edit_logmsg_only)
7308 errx(1, "histedit's -c and -m options are mutually exclusive");
7309 if (argc != 0)
7310 usage_histedit();
7313 * This command cannot apply unveil(2) in all cases because the
7314 * user may choose to run an editor to edit the histedit script
7315 * and to edit individual commit log messages.
7316 * unveil(2) traverses exec(2); if an editor is used we have to
7317 * apply unveil after edit script and log messages have been written.
7318 * XXX TODO: Make use of unveil(2) where possible.
7321 cwd = getcwd(NULL, 0);
7322 if (cwd == NULL) {
7323 error = got_error_from_errno("getcwd");
7324 goto done;
7326 error = got_worktree_open(&worktree, cwd);
7327 if (error)
7328 goto done;
7330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7331 NULL);
7332 if (error != NULL)
7333 goto done;
7335 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7336 if (error)
7337 goto done;
7338 if (rebase_in_progress) {
7339 error = got_error(GOT_ERR_REBASING);
7340 goto done;
7343 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7344 if (error)
7345 goto done;
7347 if (edit_in_progress && edit_logmsg_only) {
7348 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7349 "histedit operation is in progress in this "
7350 "work tree and must be continued or aborted "
7351 "before the -m option can be used");
7352 goto done;
7355 if (edit_in_progress && abort_edit) {
7356 error = got_worktree_histedit_continue(&resume_commit_id,
7357 &tmp_branch, &branch, &base_commit_id, &fileindex,
7358 worktree, repo);
7359 if (error)
7360 goto done;
7361 printf("Switching work tree to %s\n",
7362 got_ref_get_symref_target(branch));
7363 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7364 branch, base_commit_id, update_progress, &did_something);
7365 if (error)
7366 goto done;
7367 printf("Histedit of %s aborted\n",
7368 got_ref_get_symref_target(branch));
7369 goto done; /* nothing else to do */
7370 } else if (abort_edit) {
7371 error = got_error(GOT_ERR_NOT_HISTEDIT);
7372 goto done;
7375 if (continue_edit) {
7376 char *path;
7378 if (!edit_in_progress) {
7379 error = got_error(GOT_ERR_NOT_HISTEDIT);
7380 goto done;
7383 error = got_worktree_get_histedit_script_path(&path, worktree);
7384 if (error)
7385 goto done;
7387 error = histedit_load_list(&histedit_cmds, path, repo);
7388 free(path);
7389 if (error)
7390 goto done;
7392 error = got_worktree_histedit_continue(&resume_commit_id,
7393 &tmp_branch, &branch, &base_commit_id, &fileindex,
7394 worktree, repo);
7395 if (error)
7396 goto done;
7398 error = got_ref_resolve(&head_commit_id, repo, branch);
7399 if (error)
7400 goto done;
7402 error = got_object_open_as_commit(&commit, repo,
7403 head_commit_id);
7404 if (error)
7405 goto done;
7406 parent_ids = got_object_commit_get_parent_ids(commit);
7407 pid = SIMPLEQ_FIRST(parent_ids);
7408 if (pid == NULL) {
7409 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7410 goto done;
7412 error = collect_commits(&commits, head_commit_id, pid->id,
7413 base_commit_id, got_worktree_get_path_prefix(worktree),
7414 GOT_ERR_HISTEDIT_PATH, repo);
7415 got_object_commit_close(commit);
7416 commit = NULL;
7417 if (error)
7418 goto done;
7419 } else {
7420 if (edit_in_progress) {
7421 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7422 goto done;
7425 error = got_ref_open(&branch, repo,
7426 got_worktree_get_head_ref_name(worktree), 0);
7427 if (error != NULL)
7428 goto done;
7430 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7431 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7432 "will not edit commit history of a branch outside "
7433 "the \"refs/heads/\" reference namespace");
7434 goto done;
7437 error = got_ref_resolve(&head_commit_id, repo, branch);
7438 got_ref_close(branch);
7439 branch = NULL;
7440 if (error)
7441 goto done;
7443 error = got_object_open_as_commit(&commit, repo,
7444 head_commit_id);
7445 if (error)
7446 goto done;
7447 parent_ids = got_object_commit_get_parent_ids(commit);
7448 pid = SIMPLEQ_FIRST(parent_ids);
7449 if (pid == NULL) {
7450 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7451 goto done;
7453 error = collect_commits(&commits, head_commit_id, pid->id,
7454 got_worktree_get_base_commit_id(worktree),
7455 got_worktree_get_path_prefix(worktree),
7456 GOT_ERR_HISTEDIT_PATH, repo);
7457 got_object_commit_close(commit);
7458 commit = NULL;
7459 if (error)
7460 goto done;
7462 if (SIMPLEQ_EMPTY(&commits)) {
7463 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7464 goto done;
7467 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7468 &base_commit_id, &fileindex, worktree, repo);
7469 if (error)
7470 goto done;
7472 if (edit_script_path) {
7473 error = histedit_load_list(&histedit_cmds,
7474 edit_script_path, repo);
7475 if (error) {
7476 got_worktree_histedit_abort(worktree, fileindex,
7477 repo, branch, base_commit_id,
7478 update_progress, &did_something);
7479 goto done;
7481 } else {
7482 const char *branch_name;
7483 branch_name = got_ref_get_symref_target(branch);
7484 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7485 branch_name += 11;
7486 error = histedit_edit_script(&histedit_cmds, &commits,
7487 branch_name, edit_logmsg_only, repo);
7488 if (error) {
7489 got_worktree_histedit_abort(worktree, fileindex,
7490 repo, branch, base_commit_id,
7491 update_progress, &did_something);
7492 goto done;
7497 error = histedit_save_list(&histedit_cmds, worktree,
7498 repo);
7499 if (error) {
7500 got_worktree_histedit_abort(worktree, fileindex,
7501 repo, branch, base_commit_id,
7502 update_progress, &did_something);
7503 goto done;
7508 error = histedit_check_script(&histedit_cmds, &commits, repo);
7509 if (error)
7510 goto done;
7512 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7513 if (resume_commit_id) {
7514 if (got_object_id_cmp(hle->commit_id,
7515 resume_commit_id) != 0)
7516 continue;
7518 resume_commit_id = NULL;
7519 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7520 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7521 error = histedit_skip_commit(hle, worktree,
7522 repo);
7523 if (error)
7524 goto done;
7525 } else {
7526 struct got_pathlist_head paths;
7527 int have_changes = 0;
7529 TAILQ_INIT(&paths);
7530 error = got_pathlist_append(&paths, "", NULL);
7531 if (error)
7532 goto done;
7533 error = got_worktree_status(worktree, &paths,
7534 repo, check_local_changes, &have_changes,
7535 check_cancelled, NULL);
7536 got_pathlist_free(&paths);
7537 if (error) {
7538 if (error->code != GOT_ERR_CANCELLED)
7539 goto done;
7540 if (sigint_received || sigpipe_received)
7541 goto done;
7543 if (have_changes) {
7544 error = histedit_commit(NULL, worktree,
7545 fileindex, tmp_branch, hle, repo);
7546 if (error)
7547 goto done;
7548 } else {
7549 error = got_object_open_as_commit(
7550 &commit, repo, hle->commit_id);
7551 if (error)
7552 goto done;
7553 error = show_histedit_progress(commit,
7554 hle, NULL);
7555 got_object_commit_close(commit);
7556 commit = NULL;
7557 if (error)
7558 goto done;
7561 continue;
7564 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7565 error = histedit_skip_commit(hle, worktree, repo);
7566 if (error)
7567 goto done;
7568 continue;
7571 error = got_object_open_as_commit(&commit, repo,
7572 hle->commit_id);
7573 if (error)
7574 goto done;
7575 parent_ids = got_object_commit_get_parent_ids(commit);
7576 pid = SIMPLEQ_FIRST(parent_ids);
7578 error = got_worktree_histedit_merge_files(&merged_paths,
7579 worktree, fileindex, pid->id, hle->commit_id, repo,
7580 rebase_progress, &rebase_status, check_cancelled, NULL);
7581 if (error)
7582 goto done;
7583 got_object_commit_close(commit);
7584 commit = NULL;
7586 if (rebase_status == GOT_STATUS_CONFLICT) {
7587 error = show_rebase_merge_conflict(hle->commit_id,
7588 repo);
7589 if (error)
7590 goto done;
7591 got_worktree_rebase_pathlist_free(&merged_paths);
7592 break;
7595 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7596 char *id_str;
7597 error = got_object_id_str(&id_str, hle->commit_id);
7598 if (error)
7599 goto done;
7600 printf("Stopping histedit for amending commit %s\n",
7601 id_str);
7602 free(id_str);
7603 got_worktree_rebase_pathlist_free(&merged_paths);
7604 error = got_worktree_histedit_postpone(worktree,
7605 fileindex);
7606 goto done;
7609 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7610 error = histedit_skip_commit(hle, worktree, repo);
7611 if (error)
7612 goto done;
7613 continue;
7616 error = histedit_commit(&merged_paths, worktree, fileindex,
7617 tmp_branch, hle, repo);
7618 got_worktree_rebase_pathlist_free(&merged_paths);
7619 if (error)
7620 goto done;
7623 if (rebase_status == GOT_STATUS_CONFLICT) {
7624 error = got_worktree_histedit_postpone(worktree, fileindex);
7625 if (error)
7626 goto done;
7627 error = got_error_msg(GOT_ERR_CONFLICTS,
7628 "conflicts must be resolved before histedit can continue");
7629 } else
7630 error = histedit_complete(worktree, fileindex, tmp_branch,
7631 branch, repo);
7632 done:
7633 got_object_id_queue_free(&commits);
7634 histedit_free_list(&histedit_cmds);
7635 free(head_commit_id);
7636 free(base_commit_id);
7637 free(resume_commit_id);
7638 if (commit)
7639 got_object_commit_close(commit);
7640 if (branch)
7641 got_ref_close(branch);
7642 if (tmp_branch)
7643 got_ref_close(tmp_branch);
7644 if (worktree)
7645 got_worktree_close(worktree);
7646 if (repo)
7647 got_repo_close(repo);
7648 return error;
7651 __dead static void
7652 usage_integrate(void)
7654 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7655 exit(1);
7658 static const struct got_error *
7659 cmd_integrate(int argc, char *argv[])
7661 const struct got_error *error = NULL;
7662 struct got_repository *repo = NULL;
7663 struct got_worktree *worktree = NULL;
7664 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7665 const char *branch_arg = NULL;
7666 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7667 struct got_fileindex *fileindex = NULL;
7668 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7669 int ch, did_something = 0;
7671 while ((ch = getopt(argc, argv, "")) != -1) {
7672 switch (ch) {
7673 default:
7674 usage_integrate();
7675 /* NOTREACHED */
7679 argc -= optind;
7680 argv += optind;
7682 if (argc != 1)
7683 usage_integrate();
7684 branch_arg = argv[0];
7686 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7687 "unveil", NULL) == -1)
7688 err(1, "pledge");
7690 cwd = getcwd(NULL, 0);
7691 if (cwd == NULL) {
7692 error = got_error_from_errno("getcwd");
7693 goto done;
7696 error = got_worktree_open(&worktree, cwd);
7697 if (error)
7698 goto done;
7700 error = check_rebase_or_histedit_in_progress(worktree);
7701 if (error)
7702 goto done;
7704 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7705 NULL);
7706 if (error != NULL)
7707 goto done;
7709 error = apply_unveil(got_repo_get_path(repo), 0,
7710 got_worktree_get_root_path(worktree));
7711 if (error)
7712 goto done;
7714 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7715 error = got_error_from_errno("asprintf");
7716 goto done;
7719 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7720 &base_branch_ref, worktree, refname, repo);
7721 if (error)
7722 goto done;
7724 refname = strdup(got_ref_get_name(branch_ref));
7725 if (refname == NULL) {
7726 error = got_error_from_errno("strdup");
7727 got_worktree_integrate_abort(worktree, fileindex, repo,
7728 branch_ref, base_branch_ref);
7729 goto done;
7731 base_refname = strdup(got_ref_get_name(base_branch_ref));
7732 if (base_refname == NULL) {
7733 error = got_error_from_errno("strdup");
7734 got_worktree_integrate_abort(worktree, fileindex, repo,
7735 branch_ref, base_branch_ref);
7736 goto done;
7739 error = got_ref_resolve(&commit_id, repo, branch_ref);
7740 if (error)
7741 goto done;
7743 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7744 if (error)
7745 goto done;
7747 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7748 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7749 "specified branch has already been integrated");
7750 got_worktree_integrate_abort(worktree, fileindex, repo,
7751 branch_ref, base_branch_ref);
7752 goto done;
7755 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7756 if (error) {
7757 if (error->code == GOT_ERR_ANCESTRY)
7758 error = got_error(GOT_ERR_REBASE_REQUIRED);
7759 got_worktree_integrate_abort(worktree, fileindex, repo,
7760 branch_ref, base_branch_ref);
7761 goto done;
7764 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7765 branch_ref, base_branch_ref, update_progress, &did_something,
7766 check_cancelled, NULL);
7767 if (error)
7768 goto done;
7770 printf("Integrated %s into %s\n", refname, base_refname);
7771 done:
7772 if (repo)
7773 got_repo_close(repo);
7774 if (worktree)
7775 got_worktree_close(worktree);
7776 free(cwd);
7777 free(base_commit_id);
7778 free(commit_id);
7779 free(refname);
7780 free(base_refname);
7781 return error;
7784 __dead static void
7785 usage_stage(void)
7787 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7788 "[file-path ...]\n",
7789 getprogname());
7790 exit(1);
7793 static const struct got_error *
7794 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7795 const char *path, struct got_object_id *blob_id,
7796 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7797 int dirfd, const char *de_name)
7799 const struct got_error *err = NULL;
7800 char *id_str = NULL;
7802 if (staged_status != GOT_STATUS_ADD &&
7803 staged_status != GOT_STATUS_MODIFY &&
7804 staged_status != GOT_STATUS_DELETE)
7805 return NULL;
7807 if (staged_status == GOT_STATUS_ADD ||
7808 staged_status == GOT_STATUS_MODIFY)
7809 err = got_object_id_str(&id_str, staged_blob_id);
7810 else
7811 err = got_object_id_str(&id_str, blob_id);
7812 if (err)
7813 return err;
7815 printf("%s %c %s\n", id_str, staged_status, path);
7816 free(id_str);
7817 return NULL;
7820 static const struct got_error *
7821 cmd_stage(int argc, char *argv[])
7823 const struct got_error *error = NULL;
7824 struct got_repository *repo = NULL;
7825 struct got_worktree *worktree = NULL;
7826 char *cwd = NULL;
7827 struct got_pathlist_head paths;
7828 struct got_pathlist_entry *pe;
7829 int ch, list_stage = 0, pflag = 0;
7830 FILE *patch_script_file = NULL;
7831 const char *patch_script_path = NULL;
7832 struct choose_patch_arg cpa;
7834 TAILQ_INIT(&paths);
7836 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7837 switch (ch) {
7838 case 'l':
7839 list_stage = 1;
7840 break;
7841 case 'p':
7842 pflag = 1;
7843 break;
7844 case 'F':
7845 patch_script_path = optarg;
7846 break;
7847 default:
7848 usage_stage();
7849 /* NOTREACHED */
7853 argc -= optind;
7854 argv += optind;
7856 #ifndef PROFILE
7857 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7858 "unveil", NULL) == -1)
7859 err(1, "pledge");
7860 #endif
7861 if (list_stage && (pflag || patch_script_path))
7862 errx(1, "-l option cannot be used with other options");
7863 if (patch_script_path && !pflag)
7864 errx(1, "-F option can only be used together with -p option");
7866 cwd = getcwd(NULL, 0);
7867 if (cwd == NULL) {
7868 error = got_error_from_errno("getcwd");
7869 goto done;
7872 error = got_worktree_open(&worktree, cwd);
7873 if (error)
7874 goto done;
7876 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7877 NULL);
7878 if (error != NULL)
7879 goto done;
7881 if (patch_script_path) {
7882 patch_script_file = fopen(patch_script_path, "r");
7883 if (patch_script_file == NULL) {
7884 error = got_error_from_errno2("fopen",
7885 patch_script_path);
7886 goto done;
7889 error = apply_unveil(got_repo_get_path(repo), 0,
7890 got_worktree_get_root_path(worktree));
7891 if (error)
7892 goto done;
7894 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7895 if (error)
7896 goto done;
7898 if (list_stage)
7899 error = got_worktree_status(worktree, &paths, repo,
7900 print_stage, NULL, check_cancelled, NULL);
7901 else {
7902 cpa.patch_script_file = patch_script_file;
7903 cpa.action = "stage";
7904 error = got_worktree_stage(worktree, &paths,
7905 pflag ? NULL : print_status, NULL,
7906 pflag ? choose_patch : NULL, &cpa, repo);
7908 done:
7909 if (patch_script_file && fclose(patch_script_file) == EOF &&
7910 error == NULL)
7911 error = got_error_from_errno2("fclose", patch_script_path);
7912 if (repo)
7913 got_repo_close(repo);
7914 if (worktree)
7915 got_worktree_close(worktree);
7916 TAILQ_FOREACH(pe, &paths, entry)
7917 free((char *)pe->path);
7918 got_pathlist_free(&paths);
7919 free(cwd);
7920 return error;
7923 __dead static void
7924 usage_unstage(void)
7926 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7927 "[file-path ...]\n",
7928 getprogname());
7929 exit(1);
7933 static const struct got_error *
7934 cmd_unstage(int argc, char *argv[])
7936 const struct got_error *error = NULL;
7937 struct got_repository *repo = NULL;
7938 struct got_worktree *worktree = NULL;
7939 char *cwd = NULL;
7940 struct got_pathlist_head paths;
7941 struct got_pathlist_entry *pe;
7942 int ch, did_something = 0, pflag = 0;
7943 FILE *patch_script_file = NULL;
7944 const char *patch_script_path = NULL;
7945 struct choose_patch_arg cpa;
7947 TAILQ_INIT(&paths);
7949 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7950 switch (ch) {
7951 case 'p':
7952 pflag = 1;
7953 break;
7954 case 'F':
7955 patch_script_path = optarg;
7956 break;
7957 default:
7958 usage_unstage();
7959 /* NOTREACHED */
7963 argc -= optind;
7964 argv += optind;
7966 #ifndef PROFILE
7967 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7968 "unveil", NULL) == -1)
7969 err(1, "pledge");
7970 #endif
7971 if (patch_script_path && !pflag)
7972 errx(1, "-F option can only be used together with -p option");
7974 cwd = getcwd(NULL, 0);
7975 if (cwd == NULL) {
7976 error = got_error_from_errno("getcwd");
7977 goto done;
7980 error = got_worktree_open(&worktree, cwd);
7981 if (error)
7982 goto done;
7984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7985 NULL);
7986 if (error != NULL)
7987 goto done;
7989 if (patch_script_path) {
7990 patch_script_file = fopen(patch_script_path, "r");
7991 if (patch_script_file == NULL) {
7992 error = got_error_from_errno2("fopen",
7993 patch_script_path);
7994 goto done;
7998 error = apply_unveil(got_repo_get_path(repo), 0,
7999 got_worktree_get_root_path(worktree));
8000 if (error)
8001 goto done;
8003 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8004 if (error)
8005 goto done;
8007 cpa.patch_script_file = patch_script_file;
8008 cpa.action = "unstage";
8009 error = got_worktree_unstage(worktree, &paths, update_progress,
8010 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8011 done:
8012 if (patch_script_file && fclose(patch_script_file) == EOF &&
8013 error == NULL)
8014 error = got_error_from_errno2("fclose", patch_script_path);
8015 if (repo)
8016 got_repo_close(repo);
8017 if (worktree)
8018 got_worktree_close(worktree);
8019 TAILQ_FOREACH(pe, &paths, entry)
8020 free((char *)pe->path);
8021 got_pathlist_free(&paths);
8022 free(cwd);
8023 return error;
8026 __dead static void
8027 usage_cat(void)
8029 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8030 "arg1 [arg2 ...]\n", getprogname());
8031 exit(1);
8034 static const struct got_error *
8035 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8037 const struct got_error *err;
8038 struct got_blob_object *blob;
8040 err = got_object_open_as_blob(&blob, repo, id, 8192);
8041 if (err)
8042 return err;
8044 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8045 got_object_blob_close(blob);
8046 return err;
8049 static const struct got_error *
8050 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8052 const struct got_error *err;
8053 struct got_tree_object *tree;
8054 int nentries, i;
8056 err = got_object_open_as_tree(&tree, repo, id);
8057 if (err)
8058 return err;
8060 nentries = got_object_tree_get_nentries(tree);
8061 for (i = 0; i < nentries; i++) {
8062 struct got_tree_entry *te;
8063 char *id_str;
8064 if (sigint_received || sigpipe_received)
8065 break;
8066 te = got_object_tree_get_entry(tree, i);
8067 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8068 if (err)
8069 break;
8070 fprintf(outfile, "%s %.7o %s\n", id_str,
8071 got_tree_entry_get_mode(te),
8072 got_tree_entry_get_name(te));
8073 free(id_str);
8076 got_object_tree_close(tree);
8077 return err;
8080 static const struct got_error *
8081 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8083 const struct got_error *err;
8084 struct got_commit_object *commit;
8085 const struct got_object_id_queue *parent_ids;
8086 struct got_object_qid *pid;
8087 char *id_str = NULL;
8088 const char *logmsg = NULL;
8090 err = got_object_open_as_commit(&commit, repo, id);
8091 if (err)
8092 return err;
8094 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8095 if (err)
8096 goto done;
8098 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8099 parent_ids = got_object_commit_get_parent_ids(commit);
8100 fprintf(outfile, "numparents %d\n",
8101 got_object_commit_get_nparents(commit));
8102 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8103 char *pid_str;
8104 err = got_object_id_str(&pid_str, pid->id);
8105 if (err)
8106 goto done;
8107 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8108 free(pid_str);
8110 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8111 got_object_commit_get_author(commit),
8112 got_object_commit_get_author_time(commit));
8114 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8115 got_object_commit_get_author(commit),
8116 got_object_commit_get_committer_time(commit));
8118 logmsg = got_object_commit_get_logmsg_raw(commit);
8119 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8120 fprintf(outfile, "%s", logmsg);
8121 done:
8122 free(id_str);
8123 got_object_commit_close(commit);
8124 return err;
8127 static const struct got_error *
8128 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8130 const struct got_error *err;
8131 struct got_tag_object *tag;
8132 char *id_str = NULL;
8133 const char *tagmsg = NULL;
8135 err = got_object_open_as_tag(&tag, repo, id);
8136 if (err)
8137 return err;
8139 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8140 if (err)
8141 goto done;
8143 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8145 switch (got_object_tag_get_object_type(tag)) {
8146 case GOT_OBJ_TYPE_BLOB:
8147 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8148 GOT_OBJ_LABEL_BLOB);
8149 break;
8150 case GOT_OBJ_TYPE_TREE:
8151 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8152 GOT_OBJ_LABEL_TREE);
8153 break;
8154 case GOT_OBJ_TYPE_COMMIT:
8155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8156 GOT_OBJ_LABEL_COMMIT);
8157 break;
8158 case GOT_OBJ_TYPE_TAG:
8159 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8160 GOT_OBJ_LABEL_TAG);
8161 break;
8162 default:
8163 break;
8166 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8167 got_object_tag_get_name(tag));
8169 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8170 got_object_tag_get_tagger(tag),
8171 got_object_tag_get_tagger_time(tag));
8173 tagmsg = got_object_tag_get_message(tag);
8174 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8175 fprintf(outfile, "%s", tagmsg);
8176 done:
8177 free(id_str);
8178 got_object_tag_close(tag);
8179 return err;
8182 static const struct got_error *
8183 cmd_cat(int argc, char *argv[])
8185 const struct got_error *error;
8186 struct got_repository *repo = NULL;
8187 struct got_worktree *worktree = NULL;
8188 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8189 const char *commit_id_str = NULL;
8190 struct got_object_id *id = NULL, *commit_id = NULL;
8191 int ch, obj_type, i, force_path = 0;
8193 #ifndef PROFILE
8194 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8195 NULL) == -1)
8196 err(1, "pledge");
8197 #endif
8199 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8200 switch (ch) {
8201 case 'c':
8202 commit_id_str = optarg;
8203 break;
8204 case 'r':
8205 repo_path = realpath(optarg, NULL);
8206 if (repo_path == NULL)
8207 return got_error_from_errno2("realpath",
8208 optarg);
8209 got_path_strip_trailing_slashes(repo_path);
8210 break;
8211 case 'P':
8212 force_path = 1;
8213 break;
8214 default:
8215 usage_cat();
8216 /* NOTREACHED */
8220 argc -= optind;
8221 argv += optind;
8223 cwd = getcwd(NULL, 0);
8224 if (cwd == NULL) {
8225 error = got_error_from_errno("getcwd");
8226 goto done;
8228 error = got_worktree_open(&worktree, cwd);
8229 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8230 goto done;
8231 if (worktree) {
8232 if (repo_path == NULL) {
8233 repo_path = strdup(
8234 got_worktree_get_repo_path(worktree));
8235 if (repo_path == NULL) {
8236 error = got_error_from_errno("strdup");
8237 goto done;
8242 if (repo_path == NULL) {
8243 repo_path = getcwd(NULL, 0);
8244 if (repo_path == NULL)
8245 return got_error_from_errno("getcwd");
8248 error = got_repo_open(&repo, repo_path, NULL);
8249 free(repo_path);
8250 if (error != NULL)
8251 goto done;
8253 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8254 if (error)
8255 goto done;
8257 if (commit_id_str == NULL)
8258 commit_id_str = GOT_REF_HEAD;
8259 error = got_repo_match_object_id(&commit_id, NULL,
8260 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8261 if (error)
8262 goto done;
8264 for (i = 0; i < argc; i++) {
8265 if (force_path) {
8266 error = got_object_id_by_path(&id, repo, commit_id,
8267 argv[i]);
8268 if (error)
8269 break;
8270 } else {
8271 error = got_repo_match_object_id(&id, &label, argv[i],
8272 GOT_OBJ_TYPE_ANY, 0, repo);
8273 if (error) {
8274 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8275 error->code != GOT_ERR_NOT_REF)
8276 break;
8277 error = got_object_id_by_path(&id, repo,
8278 commit_id, argv[i]);
8279 if (error)
8280 break;
8284 error = got_object_get_type(&obj_type, repo, id);
8285 if (error)
8286 break;
8288 switch (obj_type) {
8289 case GOT_OBJ_TYPE_BLOB:
8290 error = cat_blob(id, repo, stdout);
8291 break;
8292 case GOT_OBJ_TYPE_TREE:
8293 error = cat_tree(id, repo, stdout);
8294 break;
8295 case GOT_OBJ_TYPE_COMMIT:
8296 error = cat_commit(id, repo, stdout);
8297 break;
8298 case GOT_OBJ_TYPE_TAG:
8299 error = cat_tag(id, repo, stdout);
8300 break;
8301 default:
8302 error = got_error(GOT_ERR_OBJ_TYPE);
8303 break;
8305 if (error)
8306 break;
8307 free(label);
8308 label = NULL;
8309 free(id);
8310 id = NULL;
8312 done:
8313 free(label);
8314 free(id);
8315 free(commit_id);
8316 if (worktree)
8317 got_worktree_close(worktree);
8318 if (repo) {
8319 const struct got_error *repo_error;
8320 repo_error = got_repo_close(repo);
8321 if (error == NULL)
8322 error = repo_error;
8324 return error;