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 [-m] [-q] [-v] repository-url "
815 "[target-directory]\n", getprogname());
816 exit(1);
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
821 int last_p_indexed;
822 int last_p_resolved;
823 int verbosity;
824 };
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
836 return NULL;
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
840 fflush(stdout);
841 return NULL;
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 print_size = 1;
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
857 print_indexed = 1;
858 print_size = 1;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
866 print_resolved = 1;
867 print_indexed = 1;
868 print_size = 1;
873 if (print_size || print_indexed || print_resolved)
874 printf("\r");
875 if (print_size)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 if (print_indexed)
878 printf("; indexing %d%%", p_indexed);
879 if (print_resolved)
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
882 fflush(stdout);
884 return NULL;
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
905 ssize_t n;
906 int verbosity = 0, mirror_references = 0;
908 TAILQ_INIT(&refs);
909 TAILQ_INIT(&symrefs);
911 while ((ch = getopt(argc, argv, "mvq")) != -1) {
912 switch (ch) {
913 case 'm':
914 mirror_references = 1;
915 break;
916 case 'v':
917 if (verbosity < 0)
918 verbosity = 0;
919 else if (verbosity < 3)
920 verbosity++;
921 break;
922 case 'q':
923 verbosity = -1;
924 break;
925 default:
926 usage_clone();
927 break;
930 argc -= optind;
931 argv += optind;
933 uri = argv[0];
935 if (argc == 1)
936 dirname = NULL;
937 else if (argc == 2)
938 dirname = argv[1];
939 else
940 usage_clone();
942 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
943 &repo_name, argv[0]);
944 if (error)
945 goto done;
947 if (strcmp(proto, "git") == 0) {
948 #ifndef PROFILE
949 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
950 "sendfd dns inet unveil", NULL) == -1)
951 err(1, "pledge");
952 #endif
953 git_url = strdup(argv[0]);
954 if (git_url == NULL) {
955 error = got_error_from_errno("strdup");
956 goto done;
958 } else if (strcmp(proto, "git+ssh") == 0 ||
959 strcmp(proto, "ssh") == 0) {
960 #ifndef PROFILE
961 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
962 "sendfd unveil", NULL) == -1)
963 err(1, "pledge");
964 #endif
965 if (asprintf(&git_url, "ssh://%s:%s/%s", host, port,
966 server_path) == -1) {
967 error = got_error_from_errno("asprintf");
968 goto done;
970 } else if (strcmp(proto, "http") == 0 ||
971 strcmp(proto, "git+http") == 0) {
972 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
973 goto done;
974 } else {
975 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
976 goto done;
978 if (dirname == NULL) {
979 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
980 error = got_error_from_errno("asprintf");
981 goto done;
983 repo_path = default_destdir;
984 } else
985 repo_path = dirname;
987 error = got_path_mkdir(repo_path);
988 if (error)
989 goto done;
991 error = got_repo_init(repo_path);
992 if (error)
993 goto done;
995 error = got_repo_open(&repo, repo_path, NULL);
996 if (error)
997 goto done;
999 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1000 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1001 error = got_error_from_errno2("unveil",
1002 GOT_FETCH_PATH_SSH);
1003 goto done;
1006 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1007 if (error)
1008 goto done;
1010 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1011 verbosity);
1012 if (error)
1013 goto done;
1015 if (verbosity >= 0)
1016 printf("Connected to %s%s%s\n", host,
1017 port ? ":" : "", port ? port : "");
1019 /* Create a config file git-fetch(1) can understand. */
1020 gitconfig_path = got_repo_get_path_gitconfig(repo);
1021 if (gitconfig_path == NULL) {
1022 error = got_error_from_errno("got_repo_get_path_gitconfig");
1023 goto done;
1025 gitconfig_file = fopen(gitconfig_path, "a");
1026 if (gitconfig_file == NULL) {
1027 error = got_error_from_errno2("fopen", gitconfig_path);
1028 goto done;
1030 if (mirror_references) {
1031 if (asprintf(&gitconfig,
1032 "[remote \"%s\"]\n"
1033 "\turl = %s\n"
1034 "\tmirror = true\n",
1035 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1036 error = got_error_from_errno("asprintf");
1037 goto done;
1039 } else {
1040 if (asprintf(&gitconfig,
1041 "[remote \"%s\"]\n"
1042 "\turl = %s\n"
1043 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1044 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1045 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1046 error = got_error_from_errno("asprintf");
1047 goto done;
1050 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1051 if (n != strlen(gitconfig)) {
1052 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1053 goto done;
1056 fpa.last_scaled_size[0] = '\0';
1057 fpa.last_p_indexed = -1;
1058 fpa.last_p_resolved = -1;
1059 fpa.verbosity = verbosity;
1060 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1061 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1062 fetchfd, repo, fetch_progress, &fpa);
1063 if (error)
1064 goto done;
1066 error = got_object_id_str(&id_str, pack_hash);
1067 if (error)
1068 goto done;
1069 if (verbosity >= 0)
1070 printf("\nFetched %s.pack\n", id_str);
1071 free(id_str);
1073 /* Set up references provided with the pack file. */
1074 TAILQ_FOREACH(pe, &refs, entry) {
1075 const char *refname = pe->path;
1076 struct got_object_id *id = pe->data;
1077 struct got_reference *ref;
1078 char *remote_refname;
1080 error = got_ref_alloc(&ref, refname, id);
1081 if (error)
1082 goto done;
1083 error = got_ref_write(ref, repo);
1084 got_ref_close(ref);
1085 if (error)
1086 goto done;
1088 if (mirror_references)
1089 continue;
1091 if (strncmp("refs/heads/", refname, 11) != 0)
1092 continue;
1094 if (asprintf(&remote_refname,
1095 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1096 refname + 11) == -1) {
1097 error = got_error_from_errno("asprintf");
1098 goto done;
1100 error = got_ref_alloc(&ref, remote_refname, id);
1101 if (error)
1102 goto done;
1103 error = got_ref_write(ref, repo);
1104 got_ref_close(ref);
1105 if (error)
1106 goto done;
1109 /* Set the HEAD reference if the server provided one. */
1110 TAILQ_FOREACH(pe, &symrefs, entry) {
1111 struct got_reference *symref, *target_ref;
1112 const char *refname = pe->path;
1113 const char *target = pe->data;
1115 if (strcmp(refname, GOT_REF_HEAD) != 0)
1116 continue;
1118 error = got_ref_open(&target_ref, repo, target, 0);
1119 if (error) {
1120 if (error->code == GOT_ERR_NOT_REF)
1121 continue;
1122 goto done;
1125 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1126 got_ref_close(target_ref);
1127 if (error)
1128 goto done;
1130 if (verbosity >= 0)
1131 printf("Setting %s to %s\n", GOT_REF_HEAD,
1132 got_ref_get_symref_target(symref));
1134 error = got_ref_write(symref, repo);
1135 got_ref_close(symref);
1136 break;
1139 if (verbosity >= 0)
1140 printf("Created %s repository '%s'\n",
1141 mirror_references ? "mirrored" : "cloned", repo_path);
1142 done:
1143 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1144 error = got_error_from_errno("close");
1145 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1146 error = got_error_from_errno("fclose");
1147 if (repo)
1148 got_repo_close(repo);
1149 TAILQ_FOREACH(pe, &refs, entry) {
1150 free((void *)pe->path);
1151 free(pe->data);
1153 got_pathlist_free(&refs);
1154 TAILQ_FOREACH(pe, &symrefs, entry) {
1155 free((void *)pe->path);
1156 free(pe->data);
1158 got_pathlist_free(&symrefs);
1159 free(pack_hash);
1160 free(proto);
1161 free(host);
1162 free(port);
1163 free(server_path);
1164 free(repo_name);
1165 free(default_destdir);
1166 free(gitconfig_path);
1167 free(git_url);
1168 return error;
1171 static const struct got_error *
1172 create_ref(const char *refname, struct got_object_id *id,
1173 const char *id_str, struct got_repository *repo)
1175 const struct got_error *err = NULL;
1176 struct got_reference *ref;
1178 printf("Creating %s: %s\n", refname, id_str);
1180 err = got_ref_alloc(&ref, refname, id);
1181 if (err)
1182 return err;
1184 err = got_ref_write(ref, repo);
1185 got_ref_close(ref);
1186 return err;
1189 static const struct got_error *
1190 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1191 struct got_repository *repo)
1193 const struct got_error *err = NULL;
1194 char *new_id_str = NULL;
1195 struct got_object_id *old_id = NULL;
1197 err = got_object_id_str(&new_id_str, new_id);
1198 if (err)
1199 goto done;
1201 if (got_ref_is_symbolic(ref)) {
1202 struct got_reference *new_ref;
1203 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1204 if (err)
1205 goto done;
1206 printf("Deleting symbolic reference %s -> %s\n",
1207 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1208 err = got_ref_delete(ref, repo);
1209 if (err)
1210 goto done;
1211 printf("Setting %s to %s\n", got_ref_get_name(ref),
1212 new_id_str);
1213 err = got_ref_write(new_ref, repo);
1214 if (err)
1215 goto done;
1216 } else {
1217 err = got_ref_resolve(&old_id, repo, ref);
1218 if (err)
1219 goto done;
1220 if (got_object_id_cmp(old_id, new_id) != 0) {
1221 printf("Setting %s to %s\n",
1222 got_ref_get_name(ref), new_id_str);
1223 err = got_ref_change_ref(ref, new_id);
1224 if (err)
1225 goto done;
1226 err = got_ref_write(ref, repo);
1227 if (err)
1228 goto done;
1231 done:
1232 free(old_id);
1233 free(new_id_str);
1234 return err;
1237 __dead static void
1238 usage_fetch(void)
1240 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1241 "[remote-repository-name]\n", getprogname());
1242 exit(1);
1245 static const struct got_error *
1246 cmd_fetch(int argc, char *argv[])
1248 const struct got_error *error = NULL;
1249 char *cwd = NULL, *repo_path = NULL;
1250 const char *remote_name;
1251 char *proto = NULL, *host = NULL, *port = NULL;
1252 char *repo_name = NULL, *server_path = NULL;
1253 struct got_remote_repo *remotes, *remote = NULL;
1254 int nremotes;
1255 char *id_str = NULL;
1256 struct got_repository *repo = NULL;
1257 struct got_worktree *worktree = NULL;
1258 struct got_pathlist_head refs, symrefs;
1259 struct got_pathlist_entry *pe;
1260 struct got_object_id *pack_hash = NULL;
1261 int i, ch, fetchfd = -1;
1262 struct got_fetch_progress_arg fpa;
1263 int verbosity = 0;
1265 TAILQ_INIT(&refs);
1266 TAILQ_INIT(&symrefs);
1268 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1269 switch (ch) {
1270 case 'r':
1271 repo_path = realpath(optarg, NULL);
1272 if (repo_path == NULL)
1273 return got_error_from_errno2("realpath",
1274 optarg);
1275 got_path_strip_trailing_slashes(repo_path);
1276 break;
1277 case 'v':
1278 if (verbosity < 0)
1279 verbosity = 0;
1280 else if (verbosity < 3)
1281 verbosity++;
1282 break;
1283 case 'q':
1284 verbosity = -1;
1285 break;
1286 default:
1287 usage_fetch();
1288 break;
1291 argc -= optind;
1292 argv += optind;
1294 if (argc == 0)
1295 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1296 else if (argc == 1)
1297 remote_name = argv[0];
1298 else
1299 usage_fetch();
1301 cwd = getcwd(NULL, 0);
1302 if (cwd == NULL) {
1303 error = got_error_from_errno("getcwd");
1304 goto done;
1307 if (repo_path == NULL) {
1308 error = got_worktree_open(&worktree, cwd);
1309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1310 goto done;
1311 else
1312 error = NULL;
1313 if (worktree) {
1314 repo_path =
1315 strdup(got_worktree_get_repo_path(worktree));
1316 if (repo_path == NULL)
1317 error = got_error_from_errno("strdup");
1318 if (error)
1319 goto done;
1320 } else {
1321 repo_path = strdup(cwd);
1322 if (repo_path == NULL) {
1323 error = got_error_from_errno("strdup");
1324 goto done;
1329 error = got_repo_open(&repo, repo_path, NULL);
1330 if (error)
1331 goto done;
1333 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1334 for (i = 0; i < nremotes; i++) {
1335 remote = &remotes[i];
1336 if (strcmp(remote->name, remote_name) == 0)
1337 break;
1339 if (i == nremotes) {
1340 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1341 goto done;
1344 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1345 &repo_name, remote->url);
1346 if (error)
1347 goto done;
1349 if (strcmp(proto, "git") == 0) {
1350 #ifndef PROFILE
1351 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1352 "sendfd dns inet unveil", NULL) == -1)
1353 err(1, "pledge");
1354 #endif
1355 } else if (strcmp(proto, "git+ssh") == 0 ||
1356 strcmp(proto, "ssh") == 0) {
1357 #ifndef PROFILE
1358 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1359 "sendfd unveil", NULL) == -1)
1360 err(1, "pledge");
1361 #endif
1362 } else if (strcmp(proto, "http") == 0 ||
1363 strcmp(proto, "git+http") == 0) {
1364 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1365 goto done;
1366 } else {
1367 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1368 goto done;
1371 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1372 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1373 error = got_error_from_errno2("unveil",
1374 GOT_FETCH_PATH_SSH);
1375 goto done;
1378 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1379 if (error)
1380 goto done;
1382 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1383 verbosity);
1384 if (error)
1385 goto done;
1387 if (verbosity >= 0)
1388 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1389 port ? ":" : "", port ? port : "");
1391 fpa.last_scaled_size[0] = '\0';
1392 fpa.last_p_indexed = -1;
1393 fpa.last_p_resolved = -1;
1394 fpa.verbosity = verbosity;
1395 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1396 remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
1397 if (error)
1398 goto done;
1400 if (pack_hash == NULL) {
1401 if (verbosity >= 0)
1402 printf("Already up-to-date\n");
1403 goto done;
1406 if (verbosity >= 0) {
1407 error = got_object_id_str(&id_str, pack_hash);
1408 if (error)
1409 goto done;
1410 printf("\nFetched %s.pack\n", id_str);
1411 free(id_str);
1412 id_str = NULL;
1415 /* Update references provided with the pack file. */
1416 TAILQ_FOREACH(pe, &refs, entry) {
1417 const char *refname = pe->path;
1418 struct got_object_id *id = pe->data;
1419 struct got_reference *ref;
1420 char *remote_refname;
1422 error = got_object_id_str(&id_str, id);
1423 if (error)
1424 goto done;
1426 if (strncmp("refs/tags/", refname, 10) == 0) {
1427 error = got_ref_open(&ref, repo, refname, 0);
1428 if (error) {
1429 if (error->code != GOT_ERR_NOT_REF)
1430 goto done;
1431 error = create_ref(refname, id, id_str, repo);
1432 if (error)
1433 goto done;
1434 } else {
1435 error = update_ref(ref, id, repo);
1436 got_ref_close(ref);
1437 if (error)
1438 goto done;
1440 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1441 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1442 remote_name, refname + 11) == -1) {
1443 error = got_error_from_errno("asprintf");
1444 goto done;
1447 error = got_ref_open(&ref, repo, remote_refname, 0);
1448 if (error) {
1449 if (error->code != GOT_ERR_NOT_REF)
1450 goto done;
1451 error = create_ref(remote_refname, id, id_str,
1452 repo);
1453 if (error)
1454 goto done;
1455 } else {
1456 error = update_ref(ref, id, repo);
1457 got_ref_close(ref);
1458 if (error)
1459 goto done;
1462 free(id_str);
1463 id_str = NULL;
1465 done:
1466 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1467 error = got_error_from_errno("close");
1468 if (repo)
1469 got_repo_close(repo);
1470 if (worktree)
1471 got_worktree_close(worktree);
1472 TAILQ_FOREACH(pe, &refs, entry) {
1473 free((void *)pe->path);
1474 free(pe->data);
1476 got_pathlist_free(&refs);
1477 TAILQ_FOREACH(pe, &symrefs, entry) {
1478 free((void *)pe->path);
1479 free(pe->data);
1481 got_pathlist_free(&symrefs);
1482 free(id_str);
1483 free(cwd);
1484 free(repo_path);
1485 free(pack_hash);
1486 free(proto);
1487 free(host);
1488 free(port);
1489 free(server_path);
1490 free(repo_name);
1491 return error;
1495 __dead static void
1496 usage_checkout(void)
1498 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1499 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1500 exit(1);
1503 static void
1504 show_worktree_base_ref_warning(void)
1506 fprintf(stderr, "%s: warning: could not create a reference "
1507 "to the work tree's base commit; the commit could be "
1508 "garbage-collected by Git; making the repository "
1509 "writable and running 'got update' will prevent this\n",
1510 getprogname());
1513 struct got_checkout_progress_arg {
1514 const char *worktree_path;
1515 int had_base_commit_ref_error;
1518 static const struct got_error *
1519 checkout_progress(void *arg, unsigned char status, const char *path)
1521 struct got_checkout_progress_arg *a = arg;
1523 /* Base commit bump happens silently. */
1524 if (status == GOT_STATUS_BUMP_BASE)
1525 return NULL;
1527 if (status == GOT_STATUS_BASE_REF_ERR) {
1528 a->had_base_commit_ref_error = 1;
1529 return NULL;
1532 while (path[0] == '/')
1533 path++;
1535 printf("%c %s/%s\n", status, a->worktree_path, path);
1536 return NULL;
1539 static const struct got_error *
1540 check_cancelled(void *arg)
1542 if (sigint_received || sigpipe_received)
1543 return got_error(GOT_ERR_CANCELLED);
1544 return NULL;
1547 static const struct got_error *
1548 check_linear_ancestry(struct got_object_id *commit_id,
1549 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1550 struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_object_id *yca_id;
1555 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1556 commit_id, base_commit_id, repo, check_cancelled, NULL);
1557 if (err)
1558 return err;
1560 if (yca_id == NULL)
1561 return got_error(GOT_ERR_ANCESTRY);
1564 * Require a straight line of history between the target commit
1565 * and the work tree's base commit.
1567 * Non-linear situations such as this require a rebase:
1569 * (commit) D F (base_commit)
1570 * \ /
1571 * C E
1572 * \ /
1573 * B (yca)
1574 * |
1575 * A
1577 * 'got update' only handles linear cases:
1578 * Update forwards in time: A (base/yca) - B - C - D (commit)
1579 * Update backwards in time: D (base) - C - B - A (commit/yca)
1581 if (allow_forwards_in_time_only) {
1582 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1583 return got_error(GOT_ERR_ANCESTRY);
1584 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1585 got_object_id_cmp(base_commit_id, yca_id) != 0)
1586 return got_error(GOT_ERR_ANCESTRY);
1588 free(yca_id);
1589 return NULL;
1592 static const struct got_error *
1593 check_same_branch(struct got_object_id *commit_id,
1594 struct got_reference *head_ref, struct got_object_id *yca_id,
1595 struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 struct got_commit_graph *graph = NULL;
1599 struct got_object_id *head_commit_id = NULL;
1600 int is_same_branch = 0;
1602 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1603 if (err)
1604 goto done;
1606 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1607 is_same_branch = 1;
1608 goto done;
1610 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1611 is_same_branch = 1;
1612 goto done;
1615 err = got_commit_graph_open(&graph, "/", 1);
1616 if (err)
1617 goto done;
1619 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1620 check_cancelled, NULL);
1621 if (err)
1622 goto done;
1624 for (;;) {
1625 struct got_object_id *id;
1626 err = got_commit_graph_iter_next(&id, graph, repo,
1627 check_cancelled, NULL);
1628 if (err) {
1629 if (err->code == GOT_ERR_ITER_COMPLETED)
1630 err = NULL;
1631 break;
1634 if (id) {
1635 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1636 break;
1637 if (got_object_id_cmp(id, commit_id) == 0) {
1638 is_same_branch = 1;
1639 break;
1643 done:
1644 if (graph)
1645 got_commit_graph_close(graph);
1646 free(head_commit_id);
1647 if (!err && !is_same_branch)
1648 err = got_error(GOT_ERR_ANCESTRY);
1649 return err;
1652 static const struct got_error *
1653 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1655 static char msg[512];
1656 const char *branch_name;
1658 if (got_ref_is_symbolic(ref))
1659 branch_name = got_ref_get_symref_target(ref);
1660 else
1661 branch_name = got_ref_get_name(ref);
1663 if (strncmp("refs/heads/", branch_name, 11) == 0)
1664 branch_name += 11;
1666 snprintf(msg, sizeof(msg),
1667 "target commit is not contained in branch '%s'; "
1668 "the branch to use must be specified with -b; "
1669 "if necessary a new branch can be created for "
1670 "this commit with 'got branch -c %s BRANCH_NAME'",
1671 branch_name, commit_id_str);
1673 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1676 static const struct got_error *
1677 cmd_checkout(int argc, char *argv[])
1679 const struct got_error *error = NULL;
1680 struct got_repository *repo = NULL;
1681 struct got_reference *head_ref = NULL;
1682 struct got_worktree *worktree = NULL;
1683 char *repo_path = NULL;
1684 char *worktree_path = NULL;
1685 const char *path_prefix = "";
1686 const char *branch_name = GOT_REF_HEAD;
1687 char *commit_id_str = NULL;
1688 int ch, same_path_prefix, allow_nonempty = 0;
1689 struct got_pathlist_head paths;
1690 struct got_checkout_progress_arg cpa;
1692 TAILQ_INIT(&paths);
1694 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1695 switch (ch) {
1696 case 'b':
1697 branch_name = optarg;
1698 break;
1699 case 'c':
1700 commit_id_str = strdup(optarg);
1701 if (commit_id_str == NULL)
1702 return got_error_from_errno("strdup");
1703 break;
1704 case 'E':
1705 allow_nonempty = 1;
1706 break;
1707 case 'p':
1708 path_prefix = optarg;
1709 break;
1710 default:
1711 usage_checkout();
1712 /* NOTREACHED */
1716 argc -= optind;
1717 argv += optind;
1719 #ifndef PROFILE
1720 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1721 "unveil", NULL) == -1)
1722 err(1, "pledge");
1723 #endif
1724 if (argc == 1) {
1725 char *cwd, *base, *dotgit;
1726 repo_path = realpath(argv[0], NULL);
1727 if (repo_path == NULL)
1728 return got_error_from_errno2("realpath", argv[0]);
1729 cwd = getcwd(NULL, 0);
1730 if (cwd == NULL) {
1731 error = got_error_from_errno("getcwd");
1732 goto done;
1734 if (path_prefix[0]) {
1735 base = basename(path_prefix);
1736 if (base == NULL) {
1737 error = got_error_from_errno2("basename",
1738 path_prefix);
1739 goto done;
1741 } else {
1742 base = basename(repo_path);
1743 if (base == NULL) {
1744 error = got_error_from_errno2("basename",
1745 repo_path);
1746 goto done;
1749 dotgit = strstr(base, ".git");
1750 if (dotgit)
1751 *dotgit = '\0';
1752 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1753 error = got_error_from_errno("asprintf");
1754 free(cwd);
1755 goto done;
1757 free(cwd);
1758 } else if (argc == 2) {
1759 repo_path = realpath(argv[0], NULL);
1760 if (repo_path == NULL) {
1761 error = got_error_from_errno2("realpath", argv[0]);
1762 goto done;
1764 worktree_path = realpath(argv[1], NULL);
1765 if (worktree_path == NULL) {
1766 if (errno != ENOENT) {
1767 error = got_error_from_errno2("realpath",
1768 argv[1]);
1769 goto done;
1771 worktree_path = strdup(argv[1]);
1772 if (worktree_path == NULL) {
1773 error = got_error_from_errno("strdup");
1774 goto done;
1777 } else
1778 usage_checkout();
1780 got_path_strip_trailing_slashes(repo_path);
1781 got_path_strip_trailing_slashes(worktree_path);
1783 error = got_repo_open(&repo, repo_path, NULL);
1784 if (error != NULL)
1785 goto done;
1787 /* Pre-create work tree path for unveil(2) */
1788 error = got_path_mkdir(worktree_path);
1789 if (error) {
1790 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1791 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1792 goto done;
1793 if (!allow_nonempty &&
1794 !got_path_dir_is_empty(worktree_path)) {
1795 error = got_error_path(worktree_path,
1796 GOT_ERR_DIR_NOT_EMPTY);
1797 goto done;
1801 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1802 if (error)
1803 goto done;
1805 error = got_ref_open(&head_ref, repo, branch_name, 0);
1806 if (error != NULL)
1807 goto done;
1809 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1810 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1811 goto done;
1813 error = got_worktree_open(&worktree, worktree_path);
1814 if (error != NULL)
1815 goto done;
1817 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1818 path_prefix);
1819 if (error != NULL)
1820 goto done;
1821 if (!same_path_prefix) {
1822 error = got_error(GOT_ERR_PATH_PREFIX);
1823 goto done;
1826 if (commit_id_str) {
1827 struct got_object_id *commit_id;
1828 error = got_repo_match_object_id(&commit_id, NULL,
1829 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1830 if (error)
1831 goto done;
1832 error = check_linear_ancestry(commit_id,
1833 got_worktree_get_base_commit_id(worktree), 0, repo);
1834 if (error != NULL) {
1835 free(commit_id);
1836 if (error->code == GOT_ERR_ANCESTRY) {
1837 error = checkout_ancestry_error(
1838 head_ref, commit_id_str);
1840 goto done;
1842 error = check_same_branch(commit_id, head_ref, NULL, repo);
1843 if (error) {
1844 if (error->code == GOT_ERR_ANCESTRY) {
1845 error = checkout_ancestry_error(
1846 head_ref, commit_id_str);
1848 goto done;
1850 error = got_worktree_set_base_commit_id(worktree, repo,
1851 commit_id);
1852 free(commit_id);
1853 if (error)
1854 goto done;
1857 error = got_pathlist_append(&paths, "", NULL);
1858 if (error)
1859 goto done;
1860 cpa.worktree_path = worktree_path;
1861 cpa.had_base_commit_ref_error = 0;
1862 error = got_worktree_checkout_files(worktree, &paths, repo,
1863 checkout_progress, &cpa, check_cancelled, NULL);
1864 if (error != NULL)
1865 goto done;
1867 printf("Now shut up and hack\n");
1868 if (cpa.had_base_commit_ref_error)
1869 show_worktree_base_ref_warning();
1870 done:
1871 got_pathlist_free(&paths);
1872 free(commit_id_str);
1873 free(repo_path);
1874 free(worktree_path);
1875 return error;
1878 __dead static void
1879 usage_update(void)
1881 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1882 getprogname());
1883 exit(1);
1886 static const struct got_error *
1887 update_progress(void *arg, unsigned char status, const char *path)
1889 int *did_something = arg;
1891 if (status == GOT_STATUS_EXISTS ||
1892 status == GOT_STATUS_BASE_REF_ERR)
1893 return NULL;
1895 *did_something = 1;
1897 /* Base commit bump happens silently. */
1898 if (status == GOT_STATUS_BUMP_BASE)
1899 return NULL;
1901 while (path[0] == '/')
1902 path++;
1903 printf("%c %s\n", status, path);
1904 return NULL;
1907 static const struct got_error *
1908 switch_head_ref(struct got_reference *head_ref,
1909 struct got_object_id *commit_id, struct got_worktree *worktree,
1910 struct got_repository *repo)
1912 const struct got_error *err = NULL;
1913 char *base_id_str;
1914 int ref_has_moved = 0;
1916 /* Trivial case: switching between two different references. */
1917 if (strcmp(got_ref_get_name(head_ref),
1918 got_worktree_get_head_ref_name(worktree)) != 0) {
1919 printf("Switching work tree from %s to %s\n",
1920 got_worktree_get_head_ref_name(worktree),
1921 got_ref_get_name(head_ref));
1922 return got_worktree_set_head_ref(worktree, head_ref);
1925 err = check_linear_ancestry(commit_id,
1926 got_worktree_get_base_commit_id(worktree), 0, repo);
1927 if (err) {
1928 if (err->code != GOT_ERR_ANCESTRY)
1929 return err;
1930 ref_has_moved = 1;
1932 if (!ref_has_moved)
1933 return NULL;
1935 /* Switching to a rebased branch with the same reference name. */
1936 err = got_object_id_str(&base_id_str,
1937 got_worktree_get_base_commit_id(worktree));
1938 if (err)
1939 return err;
1940 printf("Reference %s now points at a different branch\n",
1941 got_worktree_get_head_ref_name(worktree));
1942 printf("Switching work tree from %s to %s\n", base_id_str,
1943 got_worktree_get_head_ref_name(worktree));
1944 return NULL;
1947 static const struct got_error *
1948 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1950 const struct got_error *err;
1951 int in_progress;
1953 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1954 if (err)
1955 return err;
1956 if (in_progress)
1957 return got_error(GOT_ERR_REBASING);
1959 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1960 if (err)
1961 return err;
1962 if (in_progress)
1963 return got_error(GOT_ERR_HISTEDIT_BUSY);
1965 return NULL;
1968 static const struct got_error *
1969 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1970 char *argv[], struct got_worktree *worktree)
1972 const struct got_error *err = NULL;
1973 char *path;
1974 int i;
1976 if (argc == 0) {
1977 path = strdup("");
1978 if (path == NULL)
1979 return got_error_from_errno("strdup");
1980 return got_pathlist_append(paths, path, NULL);
1983 for (i = 0; i < argc; i++) {
1984 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1985 if (err)
1986 break;
1987 err = got_pathlist_append(paths, path, NULL);
1988 if (err) {
1989 free(path);
1990 break;
1994 return err;
1997 static const struct got_error *
1998 cmd_update(int argc, char *argv[])
2000 const struct got_error *error = NULL;
2001 struct got_repository *repo = NULL;
2002 struct got_worktree *worktree = NULL;
2003 char *worktree_path = NULL;
2004 struct got_object_id *commit_id = NULL;
2005 char *commit_id_str = NULL;
2006 const char *branch_name = NULL;
2007 struct got_reference *head_ref = NULL;
2008 struct got_pathlist_head paths;
2009 struct got_pathlist_entry *pe;
2010 int ch, did_something = 0;
2012 TAILQ_INIT(&paths);
2014 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2015 switch (ch) {
2016 case 'b':
2017 branch_name = optarg;
2018 break;
2019 case 'c':
2020 commit_id_str = strdup(optarg);
2021 if (commit_id_str == NULL)
2022 return got_error_from_errno("strdup");
2023 break;
2024 default:
2025 usage_update();
2026 /* NOTREACHED */
2030 argc -= optind;
2031 argv += optind;
2033 #ifndef PROFILE
2034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2035 "unveil", NULL) == -1)
2036 err(1, "pledge");
2037 #endif
2038 worktree_path = getcwd(NULL, 0);
2039 if (worktree_path == NULL) {
2040 error = got_error_from_errno("getcwd");
2041 goto done;
2043 error = got_worktree_open(&worktree, worktree_path);
2044 if (error)
2045 goto done;
2047 error = check_rebase_or_histedit_in_progress(worktree);
2048 if (error)
2049 goto done;
2051 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2052 NULL);
2053 if (error != NULL)
2054 goto done;
2056 error = apply_unveil(got_repo_get_path(repo), 0,
2057 got_worktree_get_root_path(worktree));
2058 if (error)
2059 goto done;
2061 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2062 if (error)
2063 goto done;
2065 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2066 got_worktree_get_head_ref_name(worktree), 0);
2067 if (error != NULL)
2068 goto done;
2069 if (commit_id_str == NULL) {
2070 error = got_ref_resolve(&commit_id, repo, head_ref);
2071 if (error != NULL)
2072 goto done;
2073 error = got_object_id_str(&commit_id_str, commit_id);
2074 if (error != NULL)
2075 goto done;
2076 } else {
2077 error = got_repo_match_object_id(&commit_id, NULL,
2078 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2079 free(commit_id_str);
2080 commit_id_str = NULL;
2081 if (error)
2082 goto done;
2083 error = got_object_id_str(&commit_id_str, commit_id);
2084 if (error)
2085 goto done;
2088 if (branch_name) {
2089 struct got_object_id *head_commit_id;
2090 TAILQ_FOREACH(pe, &paths, entry) {
2091 if (pe->path_len == 0)
2092 continue;
2093 error = got_error_msg(GOT_ERR_BAD_PATH,
2094 "switching between branches requires that "
2095 "the entire work tree gets updated");
2096 goto done;
2098 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2099 if (error)
2100 goto done;
2101 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2102 repo);
2103 free(head_commit_id);
2104 if (error != NULL)
2105 goto done;
2106 error = check_same_branch(commit_id, head_ref, NULL, repo);
2107 if (error)
2108 goto done;
2109 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2110 if (error)
2111 goto done;
2112 } else {
2113 error = check_linear_ancestry(commit_id,
2114 got_worktree_get_base_commit_id(worktree), 0, repo);
2115 if (error != NULL) {
2116 if (error->code == GOT_ERR_ANCESTRY)
2117 error = got_error(GOT_ERR_BRANCH_MOVED);
2118 goto done;
2120 error = check_same_branch(commit_id, head_ref, NULL, repo);
2121 if (error)
2122 goto done;
2125 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2126 commit_id) != 0) {
2127 error = got_worktree_set_base_commit_id(worktree, repo,
2128 commit_id);
2129 if (error)
2130 goto done;
2133 error = got_worktree_checkout_files(worktree, &paths, repo,
2134 update_progress, &did_something, check_cancelled, NULL);
2135 if (error != NULL)
2136 goto done;
2138 if (did_something)
2139 printf("Updated to commit %s\n", commit_id_str);
2140 else
2141 printf("Already up-to-date\n");
2142 done:
2143 free(worktree_path);
2144 TAILQ_FOREACH(pe, &paths, entry)
2145 free((char *)pe->path);
2146 got_pathlist_free(&paths);
2147 free(commit_id);
2148 free(commit_id_str);
2149 return error;
2152 static const struct got_error *
2153 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2154 const char *path, int diff_context, int ignore_whitespace,
2155 struct got_repository *repo)
2157 const struct got_error *err = NULL;
2158 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2160 if (blob_id1) {
2161 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2162 if (err)
2163 goto done;
2166 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2167 if (err)
2168 goto done;
2170 while (path[0] == '/')
2171 path++;
2172 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2173 ignore_whitespace, stdout);
2174 done:
2175 if (blob1)
2176 got_object_blob_close(blob1);
2177 got_object_blob_close(blob2);
2178 return err;
2181 static const struct got_error *
2182 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2183 const char *path, int diff_context, int ignore_whitespace,
2184 struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2188 struct got_diff_blob_output_unidiff_arg arg;
2190 if (tree_id1) {
2191 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2192 if (err)
2193 goto done;
2196 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2197 if (err)
2198 goto done;
2200 arg.diff_context = diff_context;
2201 arg.ignore_whitespace = ignore_whitespace;
2202 arg.outfile = stdout;
2203 while (path[0] == '/')
2204 path++;
2205 err = got_diff_tree(tree1, tree2, path, path, repo,
2206 got_diff_blob_output_unidiff, &arg, 1);
2207 done:
2208 if (tree1)
2209 got_object_tree_close(tree1);
2210 if (tree2)
2211 got_object_tree_close(tree2);
2212 return err;
2215 static const struct got_error *
2216 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2217 const char *path, int diff_context, struct got_repository *repo)
2219 const struct got_error *err = NULL;
2220 struct got_commit_object *pcommit = NULL;
2221 char *id_str1 = NULL, *id_str2 = NULL;
2222 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2223 struct got_object_qid *qid;
2225 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2226 if (qid != NULL) {
2227 err = got_object_open_as_commit(&pcommit, repo,
2228 qid->id);
2229 if (err)
2230 return err;
2233 if (path && path[0] != '\0') {
2234 int obj_type;
2235 err = got_object_id_by_path(&obj_id2, repo, id, path);
2236 if (err)
2237 goto done;
2238 err = got_object_id_str(&id_str2, obj_id2);
2239 if (err) {
2240 free(obj_id2);
2241 goto done;
2243 if (pcommit) {
2244 err = got_object_id_by_path(&obj_id1, repo,
2245 qid->id, path);
2246 if (err) {
2247 free(obj_id2);
2248 goto done;
2250 err = got_object_id_str(&id_str1, obj_id1);
2251 if (err) {
2252 free(obj_id2);
2253 goto done;
2256 err = got_object_get_type(&obj_type, repo, obj_id2);
2257 if (err) {
2258 free(obj_id2);
2259 goto done;
2261 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2262 switch (obj_type) {
2263 case GOT_OBJ_TYPE_BLOB:
2264 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2265 0, repo);
2266 break;
2267 case GOT_OBJ_TYPE_TREE:
2268 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2269 0, repo);
2270 break;
2271 default:
2272 err = got_error(GOT_ERR_OBJ_TYPE);
2273 break;
2275 free(obj_id1);
2276 free(obj_id2);
2277 } else {
2278 obj_id2 = got_object_commit_get_tree_id(commit);
2279 err = got_object_id_str(&id_str2, obj_id2);
2280 if (err)
2281 goto done;
2282 obj_id1 = got_object_commit_get_tree_id(pcommit);
2283 err = got_object_id_str(&id_str1, obj_id1);
2284 if (err)
2285 goto done;
2286 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2287 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2289 done:
2290 free(id_str1);
2291 free(id_str2);
2292 if (pcommit)
2293 got_object_commit_close(pcommit);
2294 return err;
2297 static char *
2298 get_datestr(time_t *time, char *datebuf)
2300 struct tm mytm, *tm;
2301 char *p, *s;
2303 tm = gmtime_r(time, &mytm);
2304 if (tm == NULL)
2305 return NULL;
2306 s = asctime_r(tm, datebuf);
2307 if (s == NULL)
2308 return NULL;
2309 p = strchr(s, '\n');
2310 if (p)
2311 *p = '\0';
2312 return s;
2315 static const struct got_error *
2316 match_logmsg(int *have_match, struct got_object_id *id,
2317 struct got_commit_object *commit, regex_t *regex)
2319 const struct got_error *err = NULL;
2320 regmatch_t regmatch;
2321 char *id_str = NULL, *logmsg = NULL;
2323 *have_match = 0;
2325 err = got_object_id_str(&id_str, id);
2326 if (err)
2327 return err;
2329 err = got_object_commit_get_logmsg(&logmsg, commit);
2330 if (err)
2331 goto done;
2333 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2334 *have_match = 1;
2335 done:
2336 free(id_str);
2337 free(logmsg);
2338 return err;
2341 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2343 static const struct got_error *
2344 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2345 struct got_repository *repo, const char *path, int show_patch,
2346 int diff_context, struct got_reflist_head *refs)
2348 const struct got_error *err = NULL;
2349 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2350 char datebuf[26];
2351 time_t committer_time;
2352 const char *author, *committer;
2353 char *refs_str = NULL;
2354 struct got_reflist_entry *re;
2356 SIMPLEQ_FOREACH(re, refs, entry) {
2357 char *s;
2358 const char *name;
2359 struct got_tag_object *tag = NULL;
2360 int cmp;
2362 name = got_ref_get_name(re->ref);
2363 if (strcmp(name, GOT_REF_HEAD) == 0)
2364 continue;
2365 if (strncmp(name, "refs/", 5) == 0)
2366 name += 5;
2367 if (strncmp(name, "got/", 4) == 0)
2368 continue;
2369 if (strncmp(name, "heads/", 6) == 0)
2370 name += 6;
2371 if (strncmp(name, "remotes/", 8) == 0)
2372 name += 8;
2373 if (strncmp(name, "tags/", 5) == 0) {
2374 err = got_object_open_as_tag(&tag, repo, re->id);
2375 if (err) {
2376 if (err->code != GOT_ERR_OBJ_TYPE)
2377 return err;
2378 /* Ref points at something other than a tag. */
2379 err = NULL;
2380 tag = NULL;
2383 cmp = got_object_id_cmp(tag ?
2384 got_object_tag_get_object_id(tag) : re->id, id);
2385 if (tag)
2386 got_object_tag_close(tag);
2387 if (cmp != 0)
2388 continue;
2389 s = refs_str;
2390 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2391 name) == -1) {
2392 err = got_error_from_errno("asprintf");
2393 free(s);
2394 return err;
2396 free(s);
2398 err = got_object_id_str(&id_str, id);
2399 if (err)
2400 return err;
2402 printf(GOT_COMMIT_SEP_STR);
2403 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2404 refs_str ? refs_str : "", refs_str ? ")" : "");
2405 free(id_str);
2406 id_str = NULL;
2407 free(refs_str);
2408 refs_str = NULL;
2409 printf("from: %s\n", got_object_commit_get_author(commit));
2410 committer_time = got_object_commit_get_committer_time(commit);
2411 datestr = get_datestr(&committer_time, datebuf);
2412 if (datestr)
2413 printf("date: %s UTC\n", datestr);
2414 author = got_object_commit_get_author(commit);
2415 committer = got_object_commit_get_committer(commit);
2416 if (strcmp(author, committer) != 0)
2417 printf("via: %s\n", committer);
2418 if (got_object_commit_get_nparents(commit) > 1) {
2419 const struct got_object_id_queue *parent_ids;
2420 struct got_object_qid *qid;
2421 int n = 1;
2422 parent_ids = got_object_commit_get_parent_ids(commit);
2423 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2424 err = got_object_id_str(&id_str, qid->id);
2425 if (err)
2426 return err;
2427 printf("parent %d: %s\n", n++, id_str);
2428 free(id_str);
2432 err = got_object_commit_get_logmsg(&logmsg0, commit);
2433 if (err)
2434 return err;
2436 logmsg = logmsg0;
2437 do {
2438 line = strsep(&logmsg, "\n");
2439 if (line)
2440 printf(" %s\n", line);
2441 } while (line);
2442 free(logmsg0);
2444 if (show_patch) {
2445 err = print_patch(commit, id, path, diff_context, repo);
2446 if (err == 0)
2447 printf("\n");
2450 if (fflush(stdout) != 0 && err == NULL)
2451 err = got_error_from_errno("fflush");
2452 return err;
2455 static const struct got_error *
2456 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2457 const char *path, int show_patch, const char *search_pattern,
2458 int diff_context, int limit, int log_branches,
2459 struct got_reflist_head *refs)
2461 const struct got_error *err;
2462 struct got_commit_graph *graph;
2463 regex_t regex;
2464 int have_match;
2466 if (search_pattern &&
2467 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2468 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2470 err = got_commit_graph_open(&graph, path, !log_branches);
2471 if (err)
2472 return err;
2473 err = got_commit_graph_iter_start(graph, root_id, repo,
2474 check_cancelled, NULL);
2475 if (err)
2476 goto done;
2477 for (;;) {
2478 struct got_commit_object *commit;
2479 struct got_object_id *id;
2481 if (sigint_received || sigpipe_received)
2482 break;
2484 err = got_commit_graph_iter_next(&id, graph, repo,
2485 check_cancelled, NULL);
2486 if (err) {
2487 if (err->code == GOT_ERR_ITER_COMPLETED)
2488 err = NULL;
2489 break;
2491 if (id == NULL)
2492 break;
2494 err = got_object_open_as_commit(&commit, repo, id);
2495 if (err)
2496 break;
2498 if (search_pattern) {
2499 err = match_logmsg(&have_match, id, commit, &regex);
2500 if (err) {
2501 got_object_commit_close(commit);
2502 break;
2504 if (have_match == 0) {
2505 got_object_commit_close(commit);
2506 continue;
2510 err = print_commit(commit, id, repo, path, show_patch,
2511 diff_context, refs);
2512 got_object_commit_close(commit);
2513 if (err || (limit && --limit == 0))
2514 break;
2516 done:
2517 if (search_pattern)
2518 regfree(&regex);
2519 got_commit_graph_close(graph);
2520 return err;
2523 __dead static void
2524 usage_log(void)
2526 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2527 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2528 exit(1);
2531 static int
2532 get_default_log_limit(void)
2534 const char *got_default_log_limit;
2535 long long n;
2536 const char *errstr;
2538 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2539 if (got_default_log_limit == NULL)
2540 return 0;
2541 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2542 if (errstr != NULL)
2543 return 0;
2544 return n;
2547 static const struct got_error *
2548 cmd_log(int argc, char *argv[])
2550 const struct got_error *error;
2551 struct got_repository *repo = NULL;
2552 struct got_worktree *worktree = NULL;
2553 struct got_commit_object *commit = NULL;
2554 struct got_object_id *id = NULL;
2555 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2556 const char *start_commit = NULL, *search_pattern = NULL;
2557 int diff_context = -1, ch;
2558 int show_patch = 0, limit = 0, log_branches = 0;
2559 const char *errstr;
2560 struct got_reflist_head refs;
2562 SIMPLEQ_INIT(&refs);
2564 #ifndef PROFILE
2565 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2566 NULL)
2567 == -1)
2568 err(1, "pledge");
2569 #endif
2571 limit = get_default_log_limit();
2573 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2574 switch (ch) {
2575 case 'p':
2576 show_patch = 1;
2577 break;
2578 case 'c':
2579 start_commit = optarg;
2580 break;
2581 case 'C':
2582 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2583 &errstr);
2584 if (errstr != NULL)
2585 err(1, "-C option %s", errstr);
2586 break;
2587 case 'l':
2588 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2589 if (errstr != NULL)
2590 err(1, "-l option %s", errstr);
2591 break;
2592 case 'b':
2593 log_branches = 1;
2594 break;
2595 case 'r':
2596 repo_path = realpath(optarg, NULL);
2597 if (repo_path == NULL)
2598 return got_error_from_errno2("realpath",
2599 optarg);
2600 got_path_strip_trailing_slashes(repo_path);
2601 break;
2602 case 's':
2603 search_pattern = optarg;
2604 break;
2605 default:
2606 usage_log();
2607 /* NOTREACHED */
2611 argc -= optind;
2612 argv += optind;
2614 if (diff_context == -1)
2615 diff_context = 3;
2616 else if (!show_patch)
2617 errx(1, "-C reguires -p");
2619 cwd = getcwd(NULL, 0);
2620 if (cwd == NULL) {
2621 error = got_error_from_errno("getcwd");
2622 goto done;
2625 error = got_worktree_open(&worktree, cwd);
2626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2627 goto done;
2628 error = NULL;
2630 if (argc == 0) {
2631 path = strdup("");
2632 if (path == NULL) {
2633 error = got_error_from_errno("strdup");
2634 goto done;
2636 } else if (argc == 1) {
2637 if (worktree) {
2638 error = got_worktree_resolve_path(&path, worktree,
2639 argv[0]);
2640 if (error)
2641 goto done;
2642 } else {
2643 path = strdup(argv[0]);
2644 if (path == NULL) {
2645 error = got_error_from_errno("strdup");
2646 goto done;
2649 } else
2650 usage_log();
2652 if (repo_path == NULL) {
2653 repo_path = worktree ?
2654 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2656 if (repo_path == NULL) {
2657 error = got_error_from_errno("strdup");
2658 goto done;
2661 error = got_repo_open(&repo, repo_path, NULL);
2662 if (error != NULL)
2663 goto done;
2665 error = apply_unveil(got_repo_get_path(repo), 1,
2666 worktree ? got_worktree_get_root_path(worktree) : NULL);
2667 if (error)
2668 goto done;
2670 if (start_commit == NULL) {
2671 struct got_reference *head_ref;
2672 error = got_ref_open(&head_ref, repo,
2673 worktree ? got_worktree_get_head_ref_name(worktree)
2674 : GOT_REF_HEAD, 0);
2675 if (error != NULL)
2676 return error;
2677 error = got_ref_resolve(&id, repo, head_ref);
2678 got_ref_close(head_ref);
2679 if (error != NULL)
2680 return error;
2681 error = got_object_open_as_commit(&commit, repo, id);
2682 } else {
2683 struct got_reference *ref;
2684 error = got_ref_open(&ref, repo, start_commit, 0);
2685 if (error == NULL) {
2686 int obj_type;
2687 error = got_ref_resolve(&id, repo, ref);
2688 got_ref_close(ref);
2689 if (error != NULL)
2690 goto done;
2691 error = got_object_get_type(&obj_type, repo, id);
2692 if (error != NULL)
2693 goto done;
2694 if (obj_type == GOT_OBJ_TYPE_TAG) {
2695 struct got_tag_object *tag;
2696 error = got_object_open_as_tag(&tag, repo, id);
2697 if (error != NULL)
2698 goto done;
2699 if (got_object_tag_get_object_type(tag) !=
2700 GOT_OBJ_TYPE_COMMIT) {
2701 got_object_tag_close(tag);
2702 error = got_error(GOT_ERR_OBJ_TYPE);
2703 goto done;
2705 free(id);
2706 id = got_object_id_dup(
2707 got_object_tag_get_object_id(tag));
2708 if (id == NULL)
2709 error = got_error_from_errno(
2710 "got_object_id_dup");
2711 got_object_tag_close(tag);
2712 if (error)
2713 goto done;
2714 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2715 error = got_error(GOT_ERR_OBJ_TYPE);
2716 goto done;
2718 error = got_object_open_as_commit(&commit, repo, id);
2719 if (error != NULL)
2720 goto done;
2722 if (commit == NULL) {
2723 error = got_repo_match_object_id_prefix(&id,
2724 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2725 if (error != NULL)
2726 return error;
2729 if (error != NULL)
2730 goto done;
2732 if (worktree) {
2733 const char *prefix = got_worktree_get_path_prefix(worktree);
2734 char *p;
2735 if (asprintf(&p, "%s%s%s", prefix,
2736 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2737 error = got_error_from_errno("asprintf");
2738 goto done;
2740 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2741 free(p);
2742 } else
2743 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2744 if (error != NULL)
2745 goto done;
2746 if (in_repo_path) {
2747 free(path);
2748 path = in_repo_path;
2751 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2752 if (error)
2753 goto done;
2755 error = print_commits(id, repo, path, show_patch, search_pattern,
2756 diff_context, limit, log_branches, &refs);
2757 done:
2758 free(path);
2759 free(repo_path);
2760 free(cwd);
2761 free(id);
2762 if (worktree)
2763 got_worktree_close(worktree);
2764 if (repo) {
2765 const struct got_error *repo_error;
2766 repo_error = got_repo_close(repo);
2767 if (error == NULL)
2768 error = repo_error;
2770 got_ref_list_free(&refs);
2771 return error;
2774 __dead static void
2775 usage_diff(void)
2777 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2778 "[-w] [object1 object2 | path]\n", getprogname());
2779 exit(1);
2782 struct print_diff_arg {
2783 struct got_repository *repo;
2784 struct got_worktree *worktree;
2785 int diff_context;
2786 const char *id_str;
2787 int header_shown;
2788 int diff_staged;
2789 int ignore_whitespace;
2792 static const struct got_error *
2793 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2794 const char *path, struct got_object_id *blob_id,
2795 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2796 int dirfd, const char *de_name)
2798 struct print_diff_arg *a = arg;
2799 const struct got_error *err = NULL;
2800 struct got_blob_object *blob1 = NULL;
2801 int fd = -1;
2802 FILE *f2 = NULL;
2803 char *abspath = NULL, *label1 = NULL;
2804 struct stat sb;
2806 if (a->diff_staged) {
2807 if (staged_status != GOT_STATUS_MODIFY &&
2808 staged_status != GOT_STATUS_ADD &&
2809 staged_status != GOT_STATUS_DELETE)
2810 return NULL;
2811 } else {
2812 if (staged_status == GOT_STATUS_DELETE)
2813 return NULL;
2814 if (status == GOT_STATUS_NONEXISTENT)
2815 return got_error_set_errno(ENOENT, path);
2816 if (status != GOT_STATUS_MODIFY &&
2817 status != GOT_STATUS_ADD &&
2818 status != GOT_STATUS_DELETE &&
2819 status != GOT_STATUS_CONFLICT)
2820 return NULL;
2823 if (!a->header_shown) {
2824 printf("diff %s %s%s\n", a->id_str,
2825 got_worktree_get_root_path(a->worktree),
2826 a->diff_staged ? " (staged changes)" : "");
2827 a->header_shown = 1;
2830 if (a->diff_staged) {
2831 const char *label1 = NULL, *label2 = NULL;
2832 switch (staged_status) {
2833 case GOT_STATUS_MODIFY:
2834 label1 = path;
2835 label2 = path;
2836 break;
2837 case GOT_STATUS_ADD:
2838 label2 = path;
2839 break;
2840 case GOT_STATUS_DELETE:
2841 label1 = path;
2842 break;
2843 default:
2844 return got_error(GOT_ERR_FILE_STATUS);
2846 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2847 label1, label2, a->diff_context, a->ignore_whitespace,
2848 a->repo, stdout);
2851 if (staged_status == GOT_STATUS_ADD ||
2852 staged_status == GOT_STATUS_MODIFY) {
2853 char *id_str;
2854 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2855 8192);
2856 if (err)
2857 goto done;
2858 err = got_object_id_str(&id_str, staged_blob_id);
2859 if (err)
2860 goto done;
2861 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2862 err = got_error_from_errno("asprintf");
2863 free(id_str);
2864 goto done;
2866 free(id_str);
2867 } else if (status != GOT_STATUS_ADD) {
2868 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2869 if (err)
2870 goto done;
2873 if (status != GOT_STATUS_DELETE) {
2874 if (asprintf(&abspath, "%s/%s",
2875 got_worktree_get_root_path(a->worktree), path) == -1) {
2876 err = got_error_from_errno("asprintf");
2877 goto done;
2880 if (dirfd != -1) {
2881 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2882 if (fd == -1) {
2883 err = got_error_from_errno2("openat", abspath);
2884 goto done;
2886 } else {
2887 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2888 if (fd == -1) {
2889 err = got_error_from_errno2("open", abspath);
2890 goto done;
2893 if (fstat(fd, &sb) == -1) {
2894 err = got_error_from_errno2("fstat", abspath);
2895 goto done;
2897 f2 = fdopen(fd, "r");
2898 if (f2 == NULL) {
2899 err = got_error_from_errno2("fdopen", abspath);
2900 goto done;
2902 fd = -1;
2903 } else
2904 sb.st_size = 0;
2906 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2907 a->diff_context, a->ignore_whitespace, stdout);
2908 done:
2909 if (blob1)
2910 got_object_blob_close(blob1);
2911 if (f2 && fclose(f2) == EOF && err == NULL)
2912 err = got_error_from_errno("fclose");
2913 if (fd != -1 && close(fd) == -1 && err == NULL)
2914 err = got_error_from_errno("close");
2915 free(abspath);
2916 return err;
2919 static const struct got_error *
2920 cmd_diff(int argc, char *argv[])
2922 const struct got_error *error;
2923 struct got_repository *repo = NULL;
2924 struct got_worktree *worktree = NULL;
2925 char *cwd = NULL, *repo_path = NULL;
2926 struct got_object_id *id1 = NULL, *id2 = NULL;
2927 const char *id_str1 = NULL, *id_str2 = NULL;
2928 char *label1 = NULL, *label2 = NULL;
2929 int type1, type2;
2930 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2931 const char *errstr;
2932 char *path = NULL;
2934 #ifndef PROFILE
2935 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2936 NULL) == -1)
2937 err(1, "pledge");
2938 #endif
2940 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2941 switch (ch) {
2942 case 'C':
2943 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2944 &errstr);
2945 if (errstr != NULL)
2946 err(1, "-C option %s", errstr);
2947 break;
2948 case 'r':
2949 repo_path = realpath(optarg, NULL);
2950 if (repo_path == NULL)
2951 return got_error_from_errno2("realpath",
2952 optarg);
2953 got_path_strip_trailing_slashes(repo_path);
2954 break;
2955 case 's':
2956 diff_staged = 1;
2957 break;
2958 case 'w':
2959 ignore_whitespace = 1;
2960 break;
2961 default:
2962 usage_diff();
2963 /* NOTREACHED */
2967 argc -= optind;
2968 argv += optind;
2970 cwd = getcwd(NULL, 0);
2971 if (cwd == NULL) {
2972 error = got_error_from_errno("getcwd");
2973 goto done;
2975 error = got_worktree_open(&worktree, cwd);
2976 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2977 goto done;
2978 if (argc <= 1) {
2979 if (worktree == NULL) {
2980 error = got_error(GOT_ERR_NOT_WORKTREE);
2981 goto done;
2983 if (repo_path)
2984 errx(1,
2985 "-r option can't be used when diffing a work tree");
2986 repo_path = strdup(got_worktree_get_repo_path(worktree));
2987 if (repo_path == NULL) {
2988 error = got_error_from_errno("strdup");
2989 goto done;
2991 if (argc == 1) {
2992 error = got_worktree_resolve_path(&path, worktree,
2993 argv[0]);
2994 if (error)
2995 goto done;
2996 } else {
2997 path = strdup("");
2998 if (path == NULL) {
2999 error = got_error_from_errno("strdup");
3000 goto done;
3003 } else if (argc == 2) {
3004 if (diff_staged)
3005 errx(1, "-s option can't be used when diffing "
3006 "objects in repository");
3007 id_str1 = argv[0];
3008 id_str2 = argv[1];
3009 if (worktree && repo_path == NULL) {
3010 repo_path =
3011 strdup(got_worktree_get_repo_path(worktree));
3012 if (repo_path == NULL) {
3013 error = got_error_from_errno("strdup");
3014 goto done;
3017 } else
3018 usage_diff();
3020 if (repo_path == NULL) {
3021 repo_path = getcwd(NULL, 0);
3022 if (repo_path == NULL)
3023 return got_error_from_errno("getcwd");
3026 error = got_repo_open(&repo, repo_path, NULL);
3027 free(repo_path);
3028 if (error != NULL)
3029 goto done;
3031 error = apply_unveil(got_repo_get_path(repo), 1,
3032 worktree ? got_worktree_get_root_path(worktree) : NULL);
3033 if (error)
3034 goto done;
3036 if (argc <= 1) {
3037 struct print_diff_arg arg;
3038 struct got_pathlist_head paths;
3039 char *id_str;
3041 TAILQ_INIT(&paths);
3043 error = got_object_id_str(&id_str,
3044 got_worktree_get_base_commit_id(worktree));
3045 if (error)
3046 goto done;
3047 arg.repo = repo;
3048 arg.worktree = worktree;
3049 arg.diff_context = diff_context;
3050 arg.id_str = id_str;
3051 arg.header_shown = 0;
3052 arg.diff_staged = diff_staged;
3053 arg.ignore_whitespace = ignore_whitespace;
3055 error = got_pathlist_append(&paths, path, NULL);
3056 if (error)
3057 goto done;
3059 error = got_worktree_status(worktree, &paths, repo, print_diff,
3060 &arg, check_cancelled, NULL);
3061 free(id_str);
3062 got_pathlist_free(&paths);
3063 goto done;
3066 error = got_repo_match_object_id(&id1, &label1, id_str1,
3067 GOT_OBJ_TYPE_ANY, 1, repo);
3068 if (error)
3069 goto done;
3071 error = got_repo_match_object_id(&id2, &label2, id_str2,
3072 GOT_OBJ_TYPE_ANY, 1, repo);
3073 if (error)
3074 goto done;
3076 error = got_object_get_type(&type1, repo, id1);
3077 if (error)
3078 goto done;
3080 error = got_object_get_type(&type2, repo, id2);
3081 if (error)
3082 goto done;
3084 if (type1 != type2) {
3085 error = got_error(GOT_ERR_OBJ_TYPE);
3086 goto done;
3089 switch (type1) {
3090 case GOT_OBJ_TYPE_BLOB:
3091 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3092 diff_context, ignore_whitespace, repo, stdout);
3093 break;
3094 case GOT_OBJ_TYPE_TREE:
3095 error = got_diff_objects_as_trees(id1, id2, "", "",
3096 diff_context, ignore_whitespace, repo, stdout);
3097 break;
3098 case GOT_OBJ_TYPE_COMMIT:
3099 printf("diff %s %s\n", label1, label2);
3100 error = got_diff_objects_as_commits(id1, id2, diff_context,
3101 ignore_whitespace, repo, stdout);
3102 break;
3103 default:
3104 error = got_error(GOT_ERR_OBJ_TYPE);
3106 done:
3107 free(label1);
3108 free(label2);
3109 free(id1);
3110 free(id2);
3111 free(path);
3112 if (worktree)
3113 got_worktree_close(worktree);
3114 if (repo) {
3115 const struct got_error *repo_error;
3116 repo_error = got_repo_close(repo);
3117 if (error == NULL)
3118 error = repo_error;
3120 return error;
3123 __dead static void
3124 usage_blame(void)
3126 fprintf(stderr,
3127 "usage: %s blame [-c commit] [-r repository-path] path\n",
3128 getprogname());
3129 exit(1);
3132 struct blame_line {
3133 int annotated;
3134 char *id_str;
3135 char *committer;
3136 char datebuf[11]; /* YYYY-MM-DD + NUL */
3139 struct blame_cb_args {
3140 struct blame_line *lines;
3141 int nlines;
3142 int nlines_prec;
3143 int lineno_cur;
3144 off_t *line_offsets;
3145 FILE *f;
3146 struct got_repository *repo;
3149 static const struct got_error *
3150 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3152 const struct got_error *err = NULL;
3153 struct blame_cb_args *a = arg;
3154 struct blame_line *bline;
3155 char *line = NULL;
3156 size_t linesize = 0;
3157 struct got_commit_object *commit = NULL;
3158 off_t offset;
3159 struct tm tm;
3160 time_t committer_time;
3162 if (nlines != a->nlines ||
3163 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3164 return got_error(GOT_ERR_RANGE);
3166 if (sigint_received)
3167 return got_error(GOT_ERR_ITER_COMPLETED);
3169 if (lineno == -1)
3170 return NULL; /* no change in this commit */
3172 /* Annotate this line. */
3173 bline = &a->lines[lineno - 1];
3174 if (bline->annotated)
3175 return NULL;
3176 err = got_object_id_str(&bline->id_str, id);
3177 if (err)
3178 return err;
3180 err = got_object_open_as_commit(&commit, a->repo, id);
3181 if (err)
3182 goto done;
3184 bline->committer = strdup(got_object_commit_get_committer(commit));
3185 if (bline->committer == NULL) {
3186 err = got_error_from_errno("strdup");
3187 goto done;
3190 committer_time = got_object_commit_get_committer_time(commit);
3191 if (localtime_r(&committer_time, &tm) == NULL)
3192 return got_error_from_errno("localtime_r");
3193 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3194 &tm) >= sizeof(bline->datebuf)) {
3195 err = got_error(GOT_ERR_NO_SPACE);
3196 goto done;
3198 bline->annotated = 1;
3200 /* Print lines annotated so far. */
3201 bline = &a->lines[a->lineno_cur - 1];
3202 if (!bline->annotated)
3203 goto done;
3205 offset = a->line_offsets[a->lineno_cur - 1];
3206 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3207 err = got_error_from_errno("fseeko");
3208 goto done;
3211 while (bline->annotated) {
3212 char *smallerthan, *at, *nl, *committer;
3213 size_t len;
3215 if (getline(&line, &linesize, a->f) == -1) {
3216 if (ferror(a->f))
3217 err = got_error_from_errno("getline");
3218 break;
3221 committer = bline->committer;
3222 smallerthan = strchr(committer, '<');
3223 if (smallerthan && smallerthan[1] != '\0')
3224 committer = smallerthan + 1;
3225 at = strchr(committer, '@');
3226 if (at)
3227 *at = '\0';
3228 len = strlen(committer);
3229 if (len >= 9)
3230 committer[8] = '\0';
3232 nl = strchr(line, '\n');
3233 if (nl)
3234 *nl = '\0';
3235 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3236 bline->id_str, bline->datebuf, committer, line);
3238 a->lineno_cur++;
3239 bline = &a->lines[a->lineno_cur - 1];
3241 done:
3242 if (commit)
3243 got_object_commit_close(commit);
3244 free(line);
3245 return err;
3248 static const struct got_error *
3249 cmd_blame(int argc, char *argv[])
3251 const struct got_error *error;
3252 struct got_repository *repo = NULL;
3253 struct got_worktree *worktree = NULL;
3254 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3255 struct got_object_id *obj_id = NULL;
3256 struct got_object_id *commit_id = NULL;
3257 struct got_blob_object *blob = NULL;
3258 char *commit_id_str = NULL;
3259 struct blame_cb_args bca;
3260 int ch, obj_type, i;
3261 size_t filesize;
3263 memset(&bca, 0, sizeof(bca));
3265 #ifndef PROFILE
3266 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3267 NULL) == -1)
3268 err(1, "pledge");
3269 #endif
3271 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3272 switch (ch) {
3273 case 'c':
3274 commit_id_str = optarg;
3275 break;
3276 case 'r':
3277 repo_path = realpath(optarg, NULL);
3278 if (repo_path == NULL)
3279 return got_error_from_errno2("realpath",
3280 optarg);
3281 got_path_strip_trailing_slashes(repo_path);
3282 break;
3283 default:
3284 usage_blame();
3285 /* NOTREACHED */
3289 argc -= optind;
3290 argv += optind;
3292 if (argc == 1)
3293 path = argv[0];
3294 else
3295 usage_blame();
3297 cwd = getcwd(NULL, 0);
3298 if (cwd == NULL) {
3299 error = got_error_from_errno("getcwd");
3300 goto done;
3302 if (repo_path == NULL) {
3303 error = got_worktree_open(&worktree, cwd);
3304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3305 goto done;
3306 else
3307 error = NULL;
3308 if (worktree) {
3309 repo_path =
3310 strdup(got_worktree_get_repo_path(worktree));
3311 if (repo_path == NULL)
3312 error = got_error_from_errno("strdup");
3313 if (error)
3314 goto done;
3315 } else {
3316 repo_path = strdup(cwd);
3317 if (repo_path == NULL) {
3318 error = got_error_from_errno("strdup");
3319 goto done;
3324 error = got_repo_open(&repo, repo_path, NULL);
3325 if (error != NULL)
3326 goto done;
3328 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3329 if (error)
3330 goto done;
3332 if (worktree) {
3333 const char *prefix = got_worktree_get_path_prefix(worktree);
3334 char *p, *worktree_subdir = cwd +
3335 strlen(got_worktree_get_root_path(worktree));
3336 if (asprintf(&p, "%s%s%s%s%s",
3337 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3338 worktree_subdir, worktree_subdir[0] ? "/" : "",
3339 path) == -1) {
3340 error = got_error_from_errno("asprintf");
3341 goto done;
3343 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3344 free(p);
3345 } else {
3346 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3348 if (error)
3349 goto done;
3351 if (commit_id_str == NULL) {
3352 struct got_reference *head_ref;
3353 error = got_ref_open(&head_ref, repo, worktree ?
3354 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3355 if (error != NULL)
3356 goto done;
3357 error = got_ref_resolve(&commit_id, repo, head_ref);
3358 got_ref_close(head_ref);
3359 if (error != NULL)
3360 goto done;
3361 } else {
3362 error = got_repo_match_object_id(&commit_id, NULL,
3363 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3364 if (error)
3365 goto done;
3368 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3369 if (error)
3370 goto done;
3372 error = got_object_get_type(&obj_type, repo, obj_id);
3373 if (error)
3374 goto done;
3376 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3377 error = got_error(GOT_ERR_OBJ_TYPE);
3378 goto done;
3381 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3382 if (error)
3383 goto done;
3384 bca.f = got_opentemp();
3385 if (bca.f == NULL) {
3386 error = got_error_from_errno("got_opentemp");
3387 goto done;
3389 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3390 &bca.line_offsets, bca.f, blob);
3391 if (error || bca.nlines == 0)
3392 goto done;
3394 /* Don't include \n at EOF in the blame line count. */
3395 if (bca.line_offsets[bca.nlines - 1] == filesize)
3396 bca.nlines--;
3398 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3399 if (bca.lines == NULL) {
3400 error = got_error_from_errno("calloc");
3401 goto done;
3403 bca.lineno_cur = 1;
3404 bca.nlines_prec = 0;
3405 i = bca.nlines;
3406 while (i > 0) {
3407 i /= 10;
3408 bca.nlines_prec++;
3410 bca.repo = repo;
3412 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3413 check_cancelled, NULL);
3414 done:
3415 free(in_repo_path);
3416 free(repo_path);
3417 free(cwd);
3418 free(commit_id);
3419 free(obj_id);
3420 if (blob)
3421 got_object_blob_close(blob);
3422 if (worktree)
3423 got_worktree_close(worktree);
3424 if (repo) {
3425 const struct got_error *repo_error;
3426 repo_error = got_repo_close(repo);
3427 if (error == NULL)
3428 error = repo_error;
3430 if (bca.lines) {
3431 for (i = 0; i < bca.nlines; i++) {
3432 struct blame_line *bline = &bca.lines[i];
3433 free(bline->id_str);
3434 free(bline->committer);
3436 free(bca.lines);
3438 free(bca.line_offsets);
3439 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3440 error = got_error_from_errno("fclose");
3441 return error;
3444 __dead static void
3445 usage_tree(void)
3447 fprintf(stderr,
3448 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3449 getprogname());
3450 exit(1);
3453 static void
3454 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3455 const char *root_path)
3457 int is_root_path = (strcmp(path, root_path) == 0);
3458 const char *modestr = "";
3459 mode_t mode = got_tree_entry_get_mode(te);
3461 path += strlen(root_path);
3462 while (path[0] == '/')
3463 path++;
3465 if (got_object_tree_entry_is_submodule(te))
3466 modestr = "$";
3467 else if (S_ISLNK(mode))
3468 modestr = "@";
3469 else if (S_ISDIR(mode))
3470 modestr = "/";
3471 else if (mode & S_IXUSR)
3472 modestr = "*";
3474 printf("%s%s%s%s%s\n", id ? id : "", path,
3475 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3478 static const struct got_error *
3479 print_tree(const char *path, struct got_object_id *commit_id,
3480 int show_ids, int recurse, const char *root_path,
3481 struct got_repository *repo)
3483 const struct got_error *err = NULL;
3484 struct got_object_id *tree_id = NULL;
3485 struct got_tree_object *tree = NULL;
3486 int nentries, i;
3488 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3489 if (err)
3490 goto done;
3492 err = got_object_open_as_tree(&tree, repo, tree_id);
3493 if (err)
3494 goto done;
3495 nentries = got_object_tree_get_nentries(tree);
3496 for (i = 0; i < nentries; i++) {
3497 struct got_tree_entry *te;
3498 char *id = NULL;
3500 if (sigint_received || sigpipe_received)
3501 break;
3503 te = got_object_tree_get_entry(tree, i);
3504 if (show_ids) {
3505 char *id_str;
3506 err = got_object_id_str(&id_str,
3507 got_tree_entry_get_id(te));
3508 if (err)
3509 goto done;
3510 if (asprintf(&id, "%s ", id_str) == -1) {
3511 err = got_error_from_errno("asprintf");
3512 free(id_str);
3513 goto done;
3515 free(id_str);
3517 print_entry(te, id, path, root_path);
3518 free(id);
3520 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3521 char *child_path;
3522 if (asprintf(&child_path, "%s%s%s", path,
3523 path[0] == '/' && path[1] == '\0' ? "" : "/",
3524 got_tree_entry_get_name(te)) == -1) {
3525 err = got_error_from_errno("asprintf");
3526 goto done;
3528 err = print_tree(child_path, commit_id, show_ids, 1,
3529 root_path, repo);
3530 free(child_path);
3531 if (err)
3532 goto done;
3535 done:
3536 if (tree)
3537 got_object_tree_close(tree);
3538 free(tree_id);
3539 return err;
3542 static const struct got_error *
3543 cmd_tree(int argc, char *argv[])
3545 const struct got_error *error;
3546 struct got_repository *repo = NULL;
3547 struct got_worktree *worktree = NULL;
3548 const char *path;
3549 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3550 struct got_object_id *commit_id = NULL;
3551 char *commit_id_str = NULL;
3552 int show_ids = 0, recurse = 0;
3553 int ch;
3555 #ifndef PROFILE
3556 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3557 NULL) == -1)
3558 err(1, "pledge");
3559 #endif
3561 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3562 switch (ch) {
3563 case 'c':
3564 commit_id_str = optarg;
3565 break;
3566 case 'r':
3567 repo_path = realpath(optarg, NULL);
3568 if (repo_path == NULL)
3569 return got_error_from_errno2("realpath",
3570 optarg);
3571 got_path_strip_trailing_slashes(repo_path);
3572 break;
3573 case 'i':
3574 show_ids = 1;
3575 break;
3576 case 'R':
3577 recurse = 1;
3578 break;
3579 default:
3580 usage_tree();
3581 /* NOTREACHED */
3585 argc -= optind;
3586 argv += optind;
3588 if (argc == 1)
3589 path = argv[0];
3590 else if (argc > 1)
3591 usage_tree();
3592 else
3593 path = NULL;
3595 cwd = getcwd(NULL, 0);
3596 if (cwd == NULL) {
3597 error = got_error_from_errno("getcwd");
3598 goto done;
3600 if (repo_path == NULL) {
3601 error = got_worktree_open(&worktree, cwd);
3602 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3603 goto done;
3604 else
3605 error = NULL;
3606 if (worktree) {
3607 repo_path =
3608 strdup(got_worktree_get_repo_path(worktree));
3609 if (repo_path == NULL)
3610 error = got_error_from_errno("strdup");
3611 if (error)
3612 goto done;
3613 } else {
3614 repo_path = strdup(cwd);
3615 if (repo_path == NULL) {
3616 error = got_error_from_errno("strdup");
3617 goto done;
3622 error = got_repo_open(&repo, repo_path, NULL);
3623 if (error != NULL)
3624 goto done;
3626 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3627 if (error)
3628 goto done;
3630 if (path == NULL) {
3631 if (worktree) {
3632 char *p, *worktree_subdir = cwd +
3633 strlen(got_worktree_get_root_path(worktree));
3634 if (asprintf(&p, "%s/%s",
3635 got_worktree_get_path_prefix(worktree),
3636 worktree_subdir) == -1) {
3637 error = got_error_from_errno("asprintf");
3638 goto done;
3640 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3641 free(p);
3642 if (error)
3643 goto done;
3644 } else
3645 path = "/";
3647 if (in_repo_path == NULL) {
3648 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3649 if (error != NULL)
3650 goto done;
3653 if (commit_id_str == NULL) {
3654 struct got_reference *head_ref;
3655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3656 if (error != NULL)
3657 goto done;
3658 error = got_ref_resolve(&commit_id, repo, head_ref);
3659 got_ref_close(head_ref);
3660 if (error != NULL)
3661 goto done;
3662 } else {
3663 error = got_repo_match_object_id(&commit_id, NULL,
3664 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3665 if (error)
3666 goto done;
3669 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3670 in_repo_path, repo);
3671 done:
3672 free(in_repo_path);
3673 free(repo_path);
3674 free(cwd);
3675 free(commit_id);
3676 if (worktree)
3677 got_worktree_close(worktree);
3678 if (repo) {
3679 const struct got_error *repo_error;
3680 repo_error = got_repo_close(repo);
3681 if (error == NULL)
3682 error = repo_error;
3684 return error;
3687 __dead static void
3688 usage_status(void)
3690 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3691 exit(1);
3694 static const struct got_error *
3695 print_status(void *arg, unsigned char status, unsigned char staged_status,
3696 const char *path, struct got_object_id *blob_id,
3697 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3698 int dirfd, const char *de_name)
3700 if (status == staged_status && (status == GOT_STATUS_DELETE))
3701 status = GOT_STATUS_NO_CHANGE;
3702 printf("%c%c %s\n", status, staged_status, path);
3703 return NULL;
3706 static const struct got_error *
3707 cmd_status(int argc, char *argv[])
3709 const struct got_error *error = NULL;
3710 struct got_repository *repo = NULL;
3711 struct got_worktree *worktree = NULL;
3712 char *cwd = NULL;
3713 struct got_pathlist_head paths;
3714 struct got_pathlist_entry *pe;
3715 int ch;
3717 TAILQ_INIT(&paths);
3719 while ((ch = getopt(argc, argv, "")) != -1) {
3720 switch (ch) {
3721 default:
3722 usage_status();
3723 /* NOTREACHED */
3727 argc -= optind;
3728 argv += optind;
3730 #ifndef PROFILE
3731 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3732 NULL) == -1)
3733 err(1, "pledge");
3734 #endif
3735 cwd = getcwd(NULL, 0);
3736 if (cwd == NULL) {
3737 error = got_error_from_errno("getcwd");
3738 goto done;
3741 error = got_worktree_open(&worktree, cwd);
3742 if (error != NULL)
3743 goto done;
3745 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3746 NULL);
3747 if (error != NULL)
3748 goto done;
3750 error = apply_unveil(got_repo_get_path(repo), 1,
3751 got_worktree_get_root_path(worktree));
3752 if (error)
3753 goto done;
3755 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3756 if (error)
3757 goto done;
3759 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3760 check_cancelled, NULL);
3761 done:
3762 TAILQ_FOREACH(pe, &paths, entry)
3763 free((char *)pe->path);
3764 got_pathlist_free(&paths);
3765 free(cwd);
3766 return error;
3769 __dead static void
3770 usage_ref(void)
3772 fprintf(stderr,
3773 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3774 getprogname());
3775 exit(1);
3778 static const struct got_error *
3779 list_refs(struct got_repository *repo)
3781 static const struct got_error *err = NULL;
3782 struct got_reflist_head refs;
3783 struct got_reflist_entry *re;
3785 SIMPLEQ_INIT(&refs);
3786 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3787 if (err)
3788 return err;
3790 SIMPLEQ_FOREACH(re, &refs, entry) {
3791 char *refstr;
3792 refstr = got_ref_to_str(re->ref);
3793 if (refstr == NULL)
3794 return got_error_from_errno("got_ref_to_str");
3795 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3796 free(refstr);
3799 got_ref_list_free(&refs);
3800 return NULL;
3803 static const struct got_error *
3804 delete_ref(struct got_repository *repo, const char *refname)
3806 const struct got_error *err = NULL;
3807 struct got_reference *ref;
3809 err = got_ref_open(&ref, repo, refname, 0);
3810 if (err)
3811 return err;
3813 err = got_ref_delete(ref, repo);
3814 got_ref_close(ref);
3815 return err;
3818 static const struct got_error *
3819 add_ref(struct got_repository *repo, const char *refname, const char *target)
3821 const struct got_error *err = NULL;
3822 struct got_object_id *id;
3823 struct got_reference *ref = NULL;
3826 * Don't let the user create a reference name with a leading '-'.
3827 * While technically a valid reference name, this case is usually
3828 * an unintended typo.
3830 if (refname[0] == '-')
3831 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3833 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3834 repo);
3835 if (err) {
3836 struct got_reference *target_ref;
3838 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3839 return err;
3840 err = got_ref_open(&target_ref, repo, target, 0);
3841 if (err)
3842 return err;
3843 err = got_ref_resolve(&id, repo, target_ref);
3844 got_ref_close(target_ref);
3845 if (err)
3846 return err;
3849 err = got_ref_alloc(&ref, refname, id);
3850 if (err)
3851 goto done;
3853 err = got_ref_write(ref, repo);
3854 done:
3855 if (ref)
3856 got_ref_close(ref);
3857 free(id);
3858 return err;
3861 static const struct got_error *
3862 add_symref(struct got_repository *repo, const char *refname, const char *target)
3864 const struct got_error *err = NULL;
3865 struct got_reference *ref = NULL;
3866 struct got_reference *target_ref = NULL;
3869 * Don't let the user create a reference name with a leading '-'.
3870 * While technically a valid reference name, this case is usually
3871 * an unintended typo.
3873 if (refname[0] == '-')
3874 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3876 err = got_ref_open(&target_ref, repo, target, 0);
3877 if (err)
3878 return err;
3880 err = got_ref_alloc_symref(&ref, refname, target_ref);
3881 if (err)
3882 goto done;
3884 err = got_ref_write(ref, repo);
3885 done:
3886 if (target_ref)
3887 got_ref_close(target_ref);
3888 if (ref)
3889 got_ref_close(ref);
3890 return err;
3893 static const struct got_error *
3894 cmd_ref(int argc, char *argv[])
3896 const struct got_error *error = NULL;
3897 struct got_repository *repo = NULL;
3898 struct got_worktree *worktree = NULL;
3899 char *cwd = NULL, *repo_path = NULL;
3900 int ch, do_list = 0, create_symref = 0;
3901 const char *delref = NULL;
3903 /* TODO: Add -s option for adding symbolic references. */
3904 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3905 switch (ch) {
3906 case 'd':
3907 delref = optarg;
3908 break;
3909 case 'r':
3910 repo_path = realpath(optarg, NULL);
3911 if (repo_path == NULL)
3912 return got_error_from_errno2("realpath",
3913 optarg);
3914 got_path_strip_trailing_slashes(repo_path);
3915 break;
3916 case 'l':
3917 do_list = 1;
3918 break;
3919 case 's':
3920 create_symref = 1;
3921 break;
3922 default:
3923 usage_ref();
3924 /* NOTREACHED */
3928 if (do_list && delref)
3929 errx(1, "-l and -d options are mutually exclusive\n");
3931 argc -= optind;
3932 argv += optind;
3934 if (do_list || delref) {
3935 if (create_symref)
3936 errx(1, "-s option cannot be used together with the "
3937 "-l or -d options");
3938 if (argc > 0)
3939 usage_ref();
3940 } else if (argc != 2)
3941 usage_ref();
3943 #ifndef PROFILE
3944 if (do_list) {
3945 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3946 NULL) == -1)
3947 err(1, "pledge");
3948 } else {
3949 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3950 "sendfd unveil", NULL) == -1)
3951 err(1, "pledge");
3953 #endif
3954 cwd = getcwd(NULL, 0);
3955 if (cwd == NULL) {
3956 error = got_error_from_errno("getcwd");
3957 goto done;
3960 if (repo_path == NULL) {
3961 error = got_worktree_open(&worktree, cwd);
3962 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3963 goto done;
3964 else
3965 error = NULL;
3966 if (worktree) {
3967 repo_path =
3968 strdup(got_worktree_get_repo_path(worktree));
3969 if (repo_path == NULL)
3970 error = got_error_from_errno("strdup");
3971 if (error)
3972 goto done;
3973 } else {
3974 repo_path = strdup(cwd);
3975 if (repo_path == NULL) {
3976 error = got_error_from_errno("strdup");
3977 goto done;
3982 error = got_repo_open(&repo, repo_path, NULL);
3983 if (error != NULL)
3984 goto done;
3986 error = apply_unveil(got_repo_get_path(repo), do_list,
3987 worktree ? got_worktree_get_root_path(worktree) : NULL);
3988 if (error)
3989 goto done;
3991 if (do_list)
3992 error = list_refs(repo);
3993 else if (delref)
3994 error = delete_ref(repo, delref);
3995 else if (create_symref)
3996 error = add_symref(repo, argv[0], argv[1]);
3997 else
3998 error = add_ref(repo, argv[0], argv[1]);
3999 done:
4000 if (repo)
4001 got_repo_close(repo);
4002 if (worktree)
4003 got_worktree_close(worktree);
4004 free(cwd);
4005 free(repo_path);
4006 return error;
4009 __dead static void
4010 usage_branch(void)
4012 fprintf(stderr,
4013 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4014 "[name]\n", getprogname());
4015 exit(1);
4018 static const struct got_error *
4019 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4020 struct got_reference *ref)
4022 const struct got_error *err = NULL;
4023 const char *refname, *marker = " ";
4024 char *refstr;
4026 refname = got_ref_get_name(ref);
4027 if (worktree && strcmp(refname,
4028 got_worktree_get_head_ref_name(worktree)) == 0) {
4029 struct got_object_id *id = NULL;
4031 err = got_ref_resolve(&id, repo, ref);
4032 if (err)
4033 return err;
4034 if (got_object_id_cmp(id,
4035 got_worktree_get_base_commit_id(worktree)) == 0)
4036 marker = "* ";
4037 else
4038 marker = "~ ";
4039 free(id);
4042 if (strncmp(refname, "refs/heads/", 11) == 0)
4043 refname += 11;
4044 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4045 refname += 18;
4047 refstr = got_ref_to_str(ref);
4048 if (refstr == NULL)
4049 return got_error_from_errno("got_ref_to_str");
4051 printf("%s%s: %s\n", marker, refname, refstr);
4052 free(refstr);
4053 return NULL;
4056 static const struct got_error *
4057 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4059 const char *refname;
4061 if (worktree == NULL)
4062 return got_error(GOT_ERR_NOT_WORKTREE);
4064 refname = got_worktree_get_head_ref_name(worktree);
4066 if (strncmp(refname, "refs/heads/", 11) == 0)
4067 refname += 11;
4068 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4069 refname += 18;
4071 printf("%s\n", refname);
4073 return NULL;
4076 static const struct got_error *
4077 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4079 static const struct got_error *err = NULL;
4080 struct got_reflist_head refs;
4081 struct got_reflist_entry *re;
4082 struct got_reference *temp_ref = NULL;
4083 int rebase_in_progress, histedit_in_progress;
4085 SIMPLEQ_INIT(&refs);
4087 if (worktree) {
4088 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4089 worktree);
4090 if (err)
4091 return err;
4093 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4094 worktree);
4095 if (err)
4096 return err;
4098 if (rebase_in_progress || histedit_in_progress) {
4099 err = got_ref_open(&temp_ref, repo,
4100 got_worktree_get_head_ref_name(worktree), 0);
4101 if (err)
4102 return err;
4103 list_branch(repo, worktree, temp_ref);
4104 got_ref_close(temp_ref);
4108 err = got_ref_list(&refs, repo, "refs/heads",
4109 got_ref_cmp_by_name, NULL);
4110 if (err)
4111 return err;
4113 SIMPLEQ_FOREACH(re, &refs, entry)
4114 list_branch(repo, worktree, re->ref);
4116 got_ref_list_free(&refs);
4117 return NULL;
4120 static const struct got_error *
4121 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4122 const char *branch_name)
4124 const struct got_error *err = NULL;
4125 struct got_reference *ref = NULL;
4126 char *refname;
4128 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4129 return got_error_from_errno("asprintf");
4131 err = got_ref_open(&ref, repo, refname, 0);
4132 if (err)
4133 goto done;
4135 if (worktree &&
4136 strcmp(got_worktree_get_head_ref_name(worktree),
4137 got_ref_get_name(ref)) == 0) {
4138 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4139 "will not delete this work tree's current branch");
4140 goto done;
4143 err = got_ref_delete(ref, repo);
4144 done:
4145 if (ref)
4146 got_ref_close(ref);
4147 free(refname);
4148 return err;
4151 static const struct got_error *
4152 add_branch(struct got_repository *repo, const char *branch_name,
4153 struct got_object_id *base_commit_id)
4155 const struct got_error *err = NULL;
4156 struct got_reference *ref = NULL;
4157 char *base_refname = NULL, *refname = NULL;
4160 * Don't let the user create a branch name with a leading '-'.
4161 * While technically a valid reference name, this case is usually
4162 * an unintended typo.
4164 if (branch_name[0] == '-')
4165 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4167 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4168 err = got_error_from_errno("asprintf");
4169 goto done;
4172 err = got_ref_open(&ref, repo, refname, 0);
4173 if (err == NULL) {
4174 err = got_error(GOT_ERR_BRANCH_EXISTS);
4175 goto done;
4176 } else if (err->code != GOT_ERR_NOT_REF)
4177 goto done;
4179 err = got_ref_alloc(&ref, refname, base_commit_id);
4180 if (err)
4181 goto done;
4183 err = got_ref_write(ref, repo);
4184 done:
4185 if (ref)
4186 got_ref_close(ref);
4187 free(base_refname);
4188 free(refname);
4189 return err;
4192 static const struct got_error *
4193 cmd_branch(int argc, char *argv[])
4195 const struct got_error *error = NULL;
4196 struct got_repository *repo = NULL;
4197 struct got_worktree *worktree = NULL;
4198 char *cwd = NULL, *repo_path = NULL;
4199 int ch, do_list = 0, do_show = 0, do_update = 1;
4200 const char *delref = NULL, *commit_id_arg = NULL;
4201 struct got_reference *ref = NULL;
4202 struct got_pathlist_head paths;
4203 struct got_pathlist_entry *pe;
4204 struct got_object_id *commit_id = NULL;
4205 char *commit_id_str = NULL;
4207 TAILQ_INIT(&paths);
4209 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4210 switch (ch) {
4211 case 'c':
4212 commit_id_arg = optarg;
4213 break;
4214 case 'd':
4215 delref = optarg;
4216 break;
4217 case 'r':
4218 repo_path = realpath(optarg, NULL);
4219 if (repo_path == NULL)
4220 return got_error_from_errno2("realpath",
4221 optarg);
4222 got_path_strip_trailing_slashes(repo_path);
4223 break;
4224 case 'l':
4225 do_list = 1;
4226 break;
4227 case 'n':
4228 do_update = 0;
4229 break;
4230 default:
4231 usage_branch();
4232 /* NOTREACHED */
4236 if (do_list && delref)
4237 errx(1, "-l and -d options are mutually exclusive\n");
4239 argc -= optind;
4240 argv += optind;
4242 if (!do_list && !delref && argc == 0)
4243 do_show = 1;
4245 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4246 errx(1, "-c option can only be used when creating a branch");
4248 if (do_list || delref) {
4249 if (argc > 0)
4250 usage_branch();
4251 } else if (!do_show && argc != 1)
4252 usage_branch();
4254 #ifndef PROFILE
4255 if (do_list || do_show) {
4256 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4257 NULL) == -1)
4258 err(1, "pledge");
4259 } else {
4260 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4261 "sendfd unveil", NULL) == -1)
4262 err(1, "pledge");
4264 #endif
4265 cwd = getcwd(NULL, 0);
4266 if (cwd == NULL) {
4267 error = got_error_from_errno("getcwd");
4268 goto done;
4271 if (repo_path == NULL) {
4272 error = got_worktree_open(&worktree, cwd);
4273 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4274 goto done;
4275 else
4276 error = NULL;
4277 if (worktree) {
4278 repo_path =
4279 strdup(got_worktree_get_repo_path(worktree));
4280 if (repo_path == NULL)
4281 error = got_error_from_errno("strdup");
4282 if (error)
4283 goto done;
4284 } else {
4285 repo_path = strdup(cwd);
4286 if (repo_path == NULL) {
4287 error = got_error_from_errno("strdup");
4288 goto done;
4293 error = got_repo_open(&repo, repo_path, NULL);
4294 if (error != NULL)
4295 goto done;
4297 error = apply_unveil(got_repo_get_path(repo), do_list,
4298 worktree ? got_worktree_get_root_path(worktree) : NULL);
4299 if (error)
4300 goto done;
4302 if (do_show)
4303 error = show_current_branch(repo, worktree);
4304 else if (do_list)
4305 error = list_branches(repo, worktree);
4306 else if (delref)
4307 error = delete_branch(repo, worktree, delref);
4308 else {
4309 if (commit_id_arg == NULL)
4310 commit_id_arg = worktree ?
4311 got_worktree_get_head_ref_name(worktree) :
4312 GOT_REF_HEAD;
4313 error = got_repo_match_object_id(&commit_id, NULL,
4314 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4315 if (error)
4316 goto done;
4317 error = add_branch(repo, argv[0], commit_id);
4318 if (error)
4319 goto done;
4320 if (worktree && do_update) {
4321 int did_something = 0;
4322 char *branch_refname = NULL;
4324 error = got_object_id_str(&commit_id_str, commit_id);
4325 if (error)
4326 goto done;
4327 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4328 worktree);
4329 if (error)
4330 goto done;
4331 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4332 == -1) {
4333 error = got_error_from_errno("asprintf");
4334 goto done;
4336 error = got_ref_open(&ref, repo, branch_refname, 0);
4337 free(branch_refname);
4338 if (error)
4339 goto done;
4340 error = switch_head_ref(ref, commit_id, worktree,
4341 repo);
4342 if (error)
4343 goto done;
4344 error = got_worktree_set_base_commit_id(worktree, repo,
4345 commit_id);
4346 if (error)
4347 goto done;
4348 error = got_worktree_checkout_files(worktree, &paths,
4349 repo, update_progress, &did_something,
4350 check_cancelled, NULL);
4351 if (error)
4352 goto done;
4353 if (did_something)
4354 printf("Updated to commit %s\n", commit_id_str);
4357 done:
4358 if (ref)
4359 got_ref_close(ref);
4360 if (repo)
4361 got_repo_close(repo);
4362 if (worktree)
4363 got_worktree_close(worktree);
4364 free(cwd);
4365 free(repo_path);
4366 free(commit_id);
4367 free(commit_id_str);
4368 TAILQ_FOREACH(pe, &paths, entry)
4369 free((char *)pe->path);
4370 got_pathlist_free(&paths);
4371 return error;
4375 __dead static void
4376 usage_tag(void)
4378 fprintf(stderr,
4379 "usage: %s tag [-c commit] [-r repository] [-l] "
4380 "[-m message] name\n", getprogname());
4381 exit(1);
4384 #if 0
4385 static const struct got_error *
4386 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4388 const struct got_error *err = NULL;
4389 struct got_reflist_entry *re, *se, *new;
4390 struct got_object_id *re_id, *se_id;
4391 struct got_tag_object *re_tag, *se_tag;
4392 time_t re_time, se_time;
4394 SIMPLEQ_FOREACH(re, tags, entry) {
4395 se = SIMPLEQ_FIRST(sorted);
4396 if (se == NULL) {
4397 err = got_reflist_entry_dup(&new, re);
4398 if (err)
4399 return err;
4400 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4401 continue;
4402 } else {
4403 err = got_ref_resolve(&re_id, repo, re->ref);
4404 if (err)
4405 break;
4406 err = got_object_open_as_tag(&re_tag, repo, re_id);
4407 free(re_id);
4408 if (err)
4409 break;
4410 re_time = got_object_tag_get_tagger_time(re_tag);
4411 got_object_tag_close(re_tag);
4414 while (se) {
4415 err = got_ref_resolve(&se_id, repo, re->ref);
4416 if (err)
4417 break;
4418 err = got_object_open_as_tag(&se_tag, repo, se_id);
4419 free(se_id);
4420 if (err)
4421 break;
4422 se_time = got_object_tag_get_tagger_time(se_tag);
4423 got_object_tag_close(se_tag);
4425 if (se_time > re_time) {
4426 err = got_reflist_entry_dup(&new, re);
4427 if (err)
4428 return err;
4429 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4430 break;
4432 se = SIMPLEQ_NEXT(se, entry);
4433 continue;
4436 done:
4437 return err;
4439 #endif
4441 static const struct got_error *
4442 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4444 static const struct got_error *err = NULL;
4445 struct got_reflist_head refs;
4446 struct got_reflist_entry *re;
4448 SIMPLEQ_INIT(&refs);
4450 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4451 if (err)
4452 return err;
4454 SIMPLEQ_FOREACH(re, &refs, entry) {
4455 const char *refname;
4456 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4457 char datebuf[26];
4458 const char *tagger;
4459 time_t tagger_time;
4460 struct got_object_id *id;
4461 struct got_tag_object *tag;
4462 struct got_commit_object *commit = NULL;
4464 refname = got_ref_get_name(re->ref);
4465 if (strncmp(refname, "refs/tags/", 10) != 0)
4466 continue;
4467 refname += 10;
4468 refstr = got_ref_to_str(re->ref);
4469 if (refstr == NULL) {
4470 err = got_error_from_errno("got_ref_to_str");
4471 break;
4473 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4474 free(refstr);
4476 err = got_ref_resolve(&id, repo, re->ref);
4477 if (err)
4478 break;
4479 err = got_object_open_as_tag(&tag, repo, id);
4480 if (err) {
4481 if (err->code != GOT_ERR_OBJ_TYPE) {
4482 free(id);
4483 break;
4485 /* "lightweight" tag */
4486 err = got_object_open_as_commit(&commit, repo, id);
4487 if (err) {
4488 free(id);
4489 break;
4491 tagger = got_object_commit_get_committer(commit);
4492 tagger_time =
4493 got_object_commit_get_committer_time(commit);
4494 err = got_object_id_str(&id_str, id);
4495 free(id);
4496 if (err)
4497 break;
4498 } else {
4499 free(id);
4500 tagger = got_object_tag_get_tagger(tag);
4501 tagger_time = got_object_tag_get_tagger_time(tag);
4502 err = got_object_id_str(&id_str,
4503 got_object_tag_get_object_id(tag));
4504 if (err)
4505 break;
4507 printf("from: %s\n", tagger);
4508 datestr = get_datestr(&tagger_time, datebuf);
4509 if (datestr)
4510 printf("date: %s UTC\n", datestr);
4511 if (commit)
4512 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4513 else {
4514 switch (got_object_tag_get_object_type(tag)) {
4515 case GOT_OBJ_TYPE_BLOB:
4516 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4517 id_str);
4518 break;
4519 case GOT_OBJ_TYPE_TREE:
4520 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4521 id_str);
4522 break;
4523 case GOT_OBJ_TYPE_COMMIT:
4524 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4525 id_str);
4526 break;
4527 case GOT_OBJ_TYPE_TAG:
4528 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4529 id_str);
4530 break;
4531 default:
4532 break;
4535 free(id_str);
4536 if (commit) {
4537 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4538 if (err)
4539 break;
4540 got_object_commit_close(commit);
4541 } else {
4542 tagmsg0 = strdup(got_object_tag_get_message(tag));
4543 got_object_tag_close(tag);
4544 if (tagmsg0 == NULL) {
4545 err = got_error_from_errno("strdup");
4546 break;
4550 tagmsg = tagmsg0;
4551 do {
4552 line = strsep(&tagmsg, "\n");
4553 if (line)
4554 printf(" %s\n", line);
4555 } while (line);
4556 free(tagmsg0);
4559 got_ref_list_free(&refs);
4560 return NULL;
4563 static const struct got_error *
4564 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4565 const char *tag_name, const char *repo_path)
4567 const struct got_error *err = NULL;
4568 char *template = NULL, *initial_content = NULL;
4569 char *editor = NULL;
4570 int fd = -1;
4572 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4573 err = got_error_from_errno("asprintf");
4574 goto done;
4577 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4578 commit_id_str, tag_name) == -1) {
4579 err = got_error_from_errno("asprintf");
4580 goto done;
4583 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4584 if (err)
4585 goto done;
4587 dprintf(fd, initial_content);
4588 close(fd);
4590 err = get_editor(&editor);
4591 if (err)
4592 goto done;
4593 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4594 done:
4595 free(initial_content);
4596 free(template);
4597 free(editor);
4599 /* Editor is done; we can now apply unveil(2) */
4600 if (err == NULL) {
4601 err = apply_unveil(repo_path, 0, NULL);
4602 if (err) {
4603 free(*tagmsg);
4604 *tagmsg = NULL;
4607 return err;
4610 static const struct got_error *
4611 add_tag(struct got_repository *repo, const char *tag_name,
4612 const char *commit_arg, const char *tagmsg_arg)
4614 const struct got_error *err = NULL;
4615 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4616 char *label = NULL, *commit_id_str = NULL;
4617 struct got_reference *ref = NULL;
4618 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4619 char *tagmsg_path = NULL, *tag_id_str = NULL;
4620 int preserve_tagmsg = 0;
4623 * Don't let the user create a tag name with a leading '-'.
4624 * While technically a valid reference name, this case is usually
4625 * an unintended typo.
4627 if (tag_name[0] == '-')
4628 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4630 err = get_author(&tagger, repo);
4631 if (err)
4632 return err;
4634 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4635 GOT_OBJ_TYPE_COMMIT, 1, repo);
4636 if (err)
4637 goto done;
4639 err = got_object_id_str(&commit_id_str, commit_id);
4640 if (err)
4641 goto done;
4643 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4644 refname = strdup(tag_name);
4645 if (refname == NULL) {
4646 err = got_error_from_errno("strdup");
4647 goto done;
4649 tag_name += 10;
4650 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4651 err = got_error_from_errno("asprintf");
4652 goto done;
4655 err = got_ref_open(&ref, repo, refname, 0);
4656 if (err == NULL) {
4657 err = got_error(GOT_ERR_TAG_EXISTS);
4658 goto done;
4659 } else if (err->code != GOT_ERR_NOT_REF)
4660 goto done;
4662 if (tagmsg_arg == NULL) {
4663 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4664 tag_name, got_repo_get_path(repo));
4665 if (err) {
4666 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4667 tagmsg_path != NULL)
4668 preserve_tagmsg = 1;
4669 goto done;
4673 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4674 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4675 if (err) {
4676 if (tagmsg_path)
4677 preserve_tagmsg = 1;
4678 goto done;
4681 err = got_ref_alloc(&ref, refname, tag_id);
4682 if (err) {
4683 if (tagmsg_path)
4684 preserve_tagmsg = 1;
4685 goto done;
4688 err = got_ref_write(ref, repo);
4689 if (err) {
4690 if (tagmsg_path)
4691 preserve_tagmsg = 1;
4692 goto done;
4695 err = got_object_id_str(&tag_id_str, tag_id);
4696 if (err) {
4697 if (tagmsg_path)
4698 preserve_tagmsg = 1;
4699 goto done;
4701 printf("Created tag %s\n", tag_id_str);
4702 done:
4703 if (preserve_tagmsg) {
4704 fprintf(stderr, "%s: tag message preserved in %s\n",
4705 getprogname(), tagmsg_path);
4706 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4707 err = got_error_from_errno2("unlink", tagmsg_path);
4708 free(tag_id_str);
4709 if (ref)
4710 got_ref_close(ref);
4711 free(commit_id);
4712 free(commit_id_str);
4713 free(refname);
4714 free(tagmsg);
4715 free(tagmsg_path);
4716 free(tagger);
4717 return err;
4720 static const struct got_error *
4721 cmd_tag(int argc, char *argv[])
4723 const struct got_error *error = NULL;
4724 struct got_repository *repo = NULL;
4725 struct got_worktree *worktree = NULL;
4726 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4727 char *gitconfig_path = NULL;
4728 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4729 int ch, do_list = 0;
4731 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4732 switch (ch) {
4733 case 'c':
4734 commit_id_arg = optarg;
4735 break;
4736 case 'm':
4737 tagmsg = optarg;
4738 break;
4739 case 'r':
4740 repo_path = realpath(optarg, NULL);
4741 if (repo_path == NULL)
4742 return got_error_from_errno2("realpath",
4743 optarg);
4744 got_path_strip_trailing_slashes(repo_path);
4745 break;
4746 case 'l':
4747 do_list = 1;
4748 break;
4749 default:
4750 usage_tag();
4751 /* NOTREACHED */
4755 argc -= optind;
4756 argv += optind;
4758 if (do_list) {
4759 if (commit_id_arg != NULL)
4760 errx(1, "-c option can only be used when creating a tag");
4761 if (tagmsg)
4762 errx(1, "-l and -m options are mutually exclusive");
4763 if (argc > 0)
4764 usage_tag();
4765 } else if (argc != 1)
4766 usage_tag();
4768 tag_name = argv[0];
4770 #ifndef PROFILE
4771 if (do_list) {
4772 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4773 NULL) == -1)
4774 err(1, "pledge");
4775 } else {
4776 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4777 "sendfd unveil", NULL) == -1)
4778 err(1, "pledge");
4780 #endif
4781 cwd = getcwd(NULL, 0);
4782 if (cwd == NULL) {
4783 error = got_error_from_errno("getcwd");
4784 goto done;
4787 if (repo_path == NULL) {
4788 error = got_worktree_open(&worktree, cwd);
4789 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4790 goto done;
4791 else
4792 error = NULL;
4793 if (worktree) {
4794 repo_path =
4795 strdup(got_worktree_get_repo_path(worktree));
4796 if (repo_path == NULL)
4797 error = got_error_from_errno("strdup");
4798 if (error)
4799 goto done;
4800 } else {
4801 repo_path = strdup(cwd);
4802 if (repo_path == NULL) {
4803 error = got_error_from_errno("strdup");
4804 goto done;
4809 if (do_list) {
4810 error = got_repo_open(&repo, repo_path, NULL);
4811 if (error != NULL)
4812 goto done;
4813 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4814 if (error)
4815 goto done;
4816 error = list_tags(repo, worktree);
4817 } else {
4818 error = get_gitconfig_path(&gitconfig_path);
4819 if (error)
4820 goto done;
4821 error = got_repo_open(&repo, repo_path, gitconfig_path);
4822 if (error != NULL)
4823 goto done;
4825 if (tagmsg) {
4826 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4827 if (error)
4828 goto done;
4831 if (commit_id_arg == NULL) {
4832 struct got_reference *head_ref;
4833 struct got_object_id *commit_id;
4834 error = got_ref_open(&head_ref, repo,
4835 worktree ? got_worktree_get_head_ref_name(worktree)
4836 : GOT_REF_HEAD, 0);
4837 if (error)
4838 goto done;
4839 error = got_ref_resolve(&commit_id, repo, head_ref);
4840 got_ref_close(head_ref);
4841 if (error)
4842 goto done;
4843 error = got_object_id_str(&commit_id_str, commit_id);
4844 free(commit_id);
4845 if (error)
4846 goto done;
4849 error = add_tag(repo, tag_name,
4850 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4852 done:
4853 if (repo)
4854 got_repo_close(repo);
4855 if (worktree)
4856 got_worktree_close(worktree);
4857 free(cwd);
4858 free(repo_path);
4859 free(gitconfig_path);
4860 free(commit_id_str);
4861 return error;
4864 __dead static void
4865 usage_add(void)
4867 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4868 getprogname());
4869 exit(1);
4872 static const struct got_error *
4873 add_progress(void *arg, unsigned char status, const char *path)
4875 while (path[0] == '/')
4876 path++;
4877 printf("%c %s\n", status, path);
4878 return NULL;
4881 static const struct got_error *
4882 cmd_add(int argc, char *argv[])
4884 const struct got_error *error = NULL;
4885 struct got_repository *repo = NULL;
4886 struct got_worktree *worktree = NULL;
4887 char *cwd = NULL;
4888 struct got_pathlist_head paths;
4889 struct got_pathlist_entry *pe;
4890 int ch, can_recurse = 0, no_ignores = 0;
4892 TAILQ_INIT(&paths);
4894 while ((ch = getopt(argc, argv, "IR")) != -1) {
4895 switch (ch) {
4896 case 'I':
4897 no_ignores = 1;
4898 break;
4899 case 'R':
4900 can_recurse = 1;
4901 break;
4902 default:
4903 usage_add();
4904 /* NOTREACHED */
4908 argc -= optind;
4909 argv += optind;
4911 #ifndef PROFILE
4912 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4913 NULL) == -1)
4914 err(1, "pledge");
4915 #endif
4916 if (argc < 1)
4917 usage_add();
4919 cwd = getcwd(NULL, 0);
4920 if (cwd == NULL) {
4921 error = got_error_from_errno("getcwd");
4922 goto done;
4925 error = got_worktree_open(&worktree, cwd);
4926 if (error)
4927 goto done;
4929 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4930 NULL);
4931 if (error != NULL)
4932 goto done;
4934 error = apply_unveil(got_repo_get_path(repo), 1,
4935 got_worktree_get_root_path(worktree));
4936 if (error)
4937 goto done;
4939 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4940 if (error)
4941 goto done;
4943 if (!can_recurse && no_ignores) {
4944 error = got_error_msg(GOT_ERR_BAD_PATH,
4945 "disregarding ignores requires -R option");
4946 goto done;
4950 if (!can_recurse) {
4951 char *ondisk_path;
4952 struct stat sb;
4953 TAILQ_FOREACH(pe, &paths, entry) {
4954 if (asprintf(&ondisk_path, "%s/%s",
4955 got_worktree_get_root_path(worktree),
4956 pe->path) == -1) {
4957 error = got_error_from_errno("asprintf");
4958 goto done;
4960 if (lstat(ondisk_path, &sb) == -1) {
4961 if (errno == ENOENT) {
4962 free(ondisk_path);
4963 continue;
4965 error = got_error_from_errno2("lstat",
4966 ondisk_path);
4967 free(ondisk_path);
4968 goto done;
4970 free(ondisk_path);
4971 if (S_ISDIR(sb.st_mode)) {
4972 error = got_error_msg(GOT_ERR_BAD_PATH,
4973 "adding directories requires -R option");
4974 goto done;
4979 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4980 NULL, repo, no_ignores);
4981 done:
4982 if (repo)
4983 got_repo_close(repo);
4984 if (worktree)
4985 got_worktree_close(worktree);
4986 TAILQ_FOREACH(pe, &paths, entry)
4987 free((char *)pe->path);
4988 got_pathlist_free(&paths);
4989 free(cwd);
4990 return error;
4993 __dead static void
4994 usage_remove(void)
4996 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4997 getprogname());
4998 exit(1);
5001 static const struct got_error *
5002 print_remove_status(void *arg, unsigned char status,
5003 unsigned char staged_status, const char *path)
5005 while (path[0] == '/')
5006 path++;
5007 if (status == GOT_STATUS_NONEXISTENT)
5008 return NULL;
5009 if (status == staged_status && (status == GOT_STATUS_DELETE))
5010 status = GOT_STATUS_NO_CHANGE;
5011 printf("%c%c %s\n", status, staged_status, path);
5012 return NULL;
5015 static const struct got_error *
5016 cmd_remove(int argc, char *argv[])
5018 const struct got_error *error = NULL;
5019 struct got_worktree *worktree = NULL;
5020 struct got_repository *repo = NULL;
5021 char *cwd = NULL;
5022 struct got_pathlist_head paths;
5023 struct got_pathlist_entry *pe;
5024 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5026 TAILQ_INIT(&paths);
5028 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5029 switch (ch) {
5030 case 'f':
5031 delete_local_mods = 1;
5032 break;
5033 case 'k':
5034 keep_on_disk = 1;
5035 break;
5036 case 'R':
5037 can_recurse = 1;
5038 break;
5039 default:
5040 usage_remove();
5041 /* NOTREACHED */
5045 argc -= optind;
5046 argv += optind;
5048 #ifndef PROFILE
5049 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5050 NULL) == -1)
5051 err(1, "pledge");
5052 #endif
5053 if (argc < 1)
5054 usage_remove();
5056 cwd = getcwd(NULL, 0);
5057 if (cwd == NULL) {
5058 error = got_error_from_errno("getcwd");
5059 goto done;
5061 error = got_worktree_open(&worktree, cwd);
5062 if (error)
5063 goto done;
5065 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5066 NULL);
5067 if (error)
5068 goto done;
5070 error = apply_unveil(got_repo_get_path(repo), 1,
5071 got_worktree_get_root_path(worktree));
5072 if (error)
5073 goto done;
5075 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5076 if (error)
5077 goto done;
5079 if (!can_recurse) {
5080 char *ondisk_path;
5081 struct stat sb;
5082 TAILQ_FOREACH(pe, &paths, entry) {
5083 if (asprintf(&ondisk_path, "%s/%s",
5084 got_worktree_get_root_path(worktree),
5085 pe->path) == -1) {
5086 error = got_error_from_errno("asprintf");
5087 goto done;
5089 if (lstat(ondisk_path, &sb) == -1) {
5090 if (errno == ENOENT) {
5091 free(ondisk_path);
5092 continue;
5094 error = got_error_from_errno2("lstat",
5095 ondisk_path);
5096 free(ondisk_path);
5097 goto done;
5099 free(ondisk_path);
5100 if (S_ISDIR(sb.st_mode)) {
5101 error = got_error_msg(GOT_ERR_BAD_PATH,
5102 "removing directories requires -R option");
5103 goto done;
5108 error = got_worktree_schedule_delete(worktree, &paths,
5109 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5110 done:
5111 if (repo)
5112 got_repo_close(repo);
5113 if (worktree)
5114 got_worktree_close(worktree);
5115 TAILQ_FOREACH(pe, &paths, entry)
5116 free((char *)pe->path);
5117 got_pathlist_free(&paths);
5118 free(cwd);
5119 return error;
5122 __dead static void
5123 usage_revert(void)
5125 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5126 "path ...\n", getprogname());
5127 exit(1);
5130 static const struct got_error *
5131 revert_progress(void *arg, unsigned char status, const char *path)
5133 if (status == GOT_STATUS_UNVERSIONED)
5134 return NULL;
5136 while (path[0] == '/')
5137 path++;
5138 printf("%c %s\n", status, path);
5139 return NULL;
5142 struct choose_patch_arg {
5143 FILE *patch_script_file;
5144 const char *action;
5147 static const struct got_error *
5148 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5149 int nchanges, const char *action)
5151 char *line = NULL;
5152 size_t linesize = 0;
5153 ssize_t linelen;
5155 switch (status) {
5156 case GOT_STATUS_ADD:
5157 printf("A %s\n%s this addition? [y/n] ", path, action);
5158 break;
5159 case GOT_STATUS_DELETE:
5160 printf("D %s\n%s this deletion? [y/n] ", path, action);
5161 break;
5162 case GOT_STATUS_MODIFY:
5163 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5164 return got_error_from_errno("fseek");
5165 printf(GOT_COMMIT_SEP_STR);
5166 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5167 printf("%s", line);
5168 if (ferror(patch_file))
5169 return got_error_from_errno("getline");
5170 printf(GOT_COMMIT_SEP_STR);
5171 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5172 path, n, nchanges, action);
5173 break;
5174 default:
5175 return got_error_path(path, GOT_ERR_FILE_STATUS);
5178 return NULL;
5181 static const struct got_error *
5182 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5183 FILE *patch_file, int n, int nchanges)
5185 const struct got_error *err = NULL;
5186 char *line = NULL;
5187 size_t linesize = 0;
5188 ssize_t linelen;
5189 int resp = ' ';
5190 struct choose_patch_arg *a = arg;
5192 *choice = GOT_PATCH_CHOICE_NONE;
5194 if (a->patch_script_file) {
5195 char *nl;
5196 err = show_change(status, path, patch_file, n, nchanges,
5197 a->action);
5198 if (err)
5199 return err;
5200 linelen = getline(&line, &linesize, a->patch_script_file);
5201 if (linelen == -1) {
5202 if (ferror(a->patch_script_file))
5203 return got_error_from_errno("getline");
5204 return NULL;
5206 nl = strchr(line, '\n');
5207 if (nl)
5208 *nl = '\0';
5209 if (strcmp(line, "y") == 0) {
5210 *choice = GOT_PATCH_CHOICE_YES;
5211 printf("y\n");
5212 } else if (strcmp(line, "n") == 0) {
5213 *choice = GOT_PATCH_CHOICE_NO;
5214 printf("n\n");
5215 } else if (strcmp(line, "q") == 0 &&
5216 status == GOT_STATUS_MODIFY) {
5217 *choice = GOT_PATCH_CHOICE_QUIT;
5218 printf("q\n");
5219 } else
5220 printf("invalid response '%s'\n", line);
5221 free(line);
5222 return NULL;
5225 while (resp != 'y' && resp != 'n' && resp != 'q') {
5226 err = show_change(status, path, patch_file, n, nchanges,
5227 a->action);
5228 if (err)
5229 return err;
5230 resp = getchar();
5231 if (resp == '\n')
5232 resp = getchar();
5233 if (status == GOT_STATUS_MODIFY) {
5234 if (resp != 'y' && resp != 'n' && resp != 'q') {
5235 printf("invalid response '%c'\n", resp);
5236 resp = ' ';
5238 } else if (resp != 'y' && resp != 'n') {
5239 printf("invalid response '%c'\n", resp);
5240 resp = ' ';
5244 if (resp == 'y')
5245 *choice = GOT_PATCH_CHOICE_YES;
5246 else if (resp == 'n')
5247 *choice = GOT_PATCH_CHOICE_NO;
5248 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5249 *choice = GOT_PATCH_CHOICE_QUIT;
5251 return NULL;
5255 static const struct got_error *
5256 cmd_revert(int argc, char *argv[])
5258 const struct got_error *error = NULL;
5259 struct got_worktree *worktree = NULL;
5260 struct got_repository *repo = NULL;
5261 char *cwd = NULL, *path = NULL;
5262 struct got_pathlist_head paths;
5263 struct got_pathlist_entry *pe;
5264 int ch, can_recurse = 0, pflag = 0;
5265 FILE *patch_script_file = NULL;
5266 const char *patch_script_path = NULL;
5267 struct choose_patch_arg cpa;
5269 TAILQ_INIT(&paths);
5271 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5272 switch (ch) {
5273 case 'p':
5274 pflag = 1;
5275 break;
5276 case 'F':
5277 patch_script_path = optarg;
5278 break;
5279 case 'R':
5280 can_recurse = 1;
5281 break;
5282 default:
5283 usage_revert();
5284 /* NOTREACHED */
5288 argc -= optind;
5289 argv += optind;
5291 #ifndef PROFILE
5292 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5293 "unveil", NULL) == -1)
5294 err(1, "pledge");
5295 #endif
5296 if (argc < 1)
5297 usage_revert();
5298 if (patch_script_path && !pflag)
5299 errx(1, "-F option can only be used together with -p option");
5301 cwd = getcwd(NULL, 0);
5302 if (cwd == NULL) {
5303 error = got_error_from_errno("getcwd");
5304 goto done;
5306 error = got_worktree_open(&worktree, cwd);
5307 if (error)
5308 goto done;
5310 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5311 NULL);
5312 if (error != NULL)
5313 goto done;
5315 if (patch_script_path) {
5316 patch_script_file = fopen(patch_script_path, "r");
5317 if (patch_script_file == NULL) {
5318 error = got_error_from_errno2("fopen",
5319 patch_script_path);
5320 goto done;
5323 error = apply_unveil(got_repo_get_path(repo), 1,
5324 got_worktree_get_root_path(worktree));
5325 if (error)
5326 goto done;
5328 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5329 if (error)
5330 goto done;
5332 if (!can_recurse) {
5333 char *ondisk_path;
5334 struct stat sb;
5335 TAILQ_FOREACH(pe, &paths, entry) {
5336 if (asprintf(&ondisk_path, "%s/%s",
5337 got_worktree_get_root_path(worktree),
5338 pe->path) == -1) {
5339 error = got_error_from_errno("asprintf");
5340 goto done;
5342 if (lstat(ondisk_path, &sb) == -1) {
5343 if (errno == ENOENT) {
5344 free(ondisk_path);
5345 continue;
5347 error = got_error_from_errno2("lstat",
5348 ondisk_path);
5349 free(ondisk_path);
5350 goto done;
5352 free(ondisk_path);
5353 if (S_ISDIR(sb.st_mode)) {
5354 error = got_error_msg(GOT_ERR_BAD_PATH,
5355 "reverting directories requires -R option");
5356 goto done;
5361 cpa.patch_script_file = patch_script_file;
5362 cpa.action = "revert";
5363 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5364 pflag ? choose_patch : NULL, &cpa, repo);
5365 done:
5366 if (patch_script_file && fclose(patch_script_file) == EOF &&
5367 error == NULL)
5368 error = got_error_from_errno2("fclose", patch_script_path);
5369 if (repo)
5370 got_repo_close(repo);
5371 if (worktree)
5372 got_worktree_close(worktree);
5373 free(path);
5374 free(cwd);
5375 return error;
5378 __dead static void
5379 usage_commit(void)
5381 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5382 getprogname());
5383 exit(1);
5386 struct collect_commit_logmsg_arg {
5387 const char *cmdline_log;
5388 const char *editor;
5389 const char *worktree_path;
5390 const char *branch_name;
5391 const char *repo_path;
5392 char *logmsg_path;
5396 static const struct got_error *
5397 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5398 void *arg)
5400 char *initial_content = NULL;
5401 struct got_pathlist_entry *pe;
5402 const struct got_error *err = NULL;
5403 char *template = NULL;
5404 struct collect_commit_logmsg_arg *a = arg;
5405 int fd;
5406 size_t len;
5408 /* if a message was specified on the command line, just use it */
5409 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5410 len = strlen(a->cmdline_log) + 1;
5411 *logmsg = malloc(len + 1);
5412 if (*logmsg == NULL)
5413 return got_error_from_errno("malloc");
5414 strlcpy(*logmsg, a->cmdline_log, len);
5415 return NULL;
5418 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5419 return got_error_from_errno("asprintf");
5421 if (asprintf(&initial_content,
5422 "\n# changes to be committed on branch %s:\n",
5423 a->branch_name) == -1)
5424 return got_error_from_errno("asprintf");
5426 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5427 if (err)
5428 goto done;
5430 dprintf(fd, initial_content);
5432 TAILQ_FOREACH(pe, commitable_paths, entry) {
5433 struct got_commitable *ct = pe->data;
5434 dprintf(fd, "# %c %s\n",
5435 got_commitable_get_status(ct),
5436 got_commitable_get_path(ct));
5438 close(fd);
5440 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5441 done:
5442 free(initial_content);
5443 free(template);
5445 /* Editor is done; we can now apply unveil(2) */
5446 if (err == NULL) {
5447 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5448 if (err) {
5449 free(*logmsg);
5450 *logmsg = NULL;
5453 return err;
5456 static const struct got_error *
5457 cmd_commit(int argc, char *argv[])
5459 const struct got_error *error = NULL;
5460 struct got_worktree *worktree = NULL;
5461 struct got_repository *repo = NULL;
5462 char *cwd = NULL, *id_str = NULL;
5463 struct got_object_id *id = NULL;
5464 const char *logmsg = NULL;
5465 struct collect_commit_logmsg_arg cl_arg;
5466 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5467 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5468 struct got_pathlist_head paths;
5470 TAILQ_INIT(&paths);
5471 cl_arg.logmsg_path = NULL;
5473 while ((ch = getopt(argc, argv, "m:")) != -1) {
5474 switch (ch) {
5475 case 'm':
5476 logmsg = optarg;
5477 break;
5478 default:
5479 usage_commit();
5480 /* NOTREACHED */
5484 argc -= optind;
5485 argv += optind;
5487 #ifndef PROFILE
5488 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5489 "unveil", NULL) == -1)
5490 err(1, "pledge");
5491 #endif
5492 cwd = getcwd(NULL, 0);
5493 if (cwd == NULL) {
5494 error = got_error_from_errno("getcwd");
5495 goto done;
5497 error = got_worktree_open(&worktree, cwd);
5498 if (error)
5499 goto done;
5501 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5502 if (error)
5503 goto done;
5504 if (rebase_in_progress) {
5505 error = got_error(GOT_ERR_REBASING);
5506 goto done;
5509 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5510 worktree);
5511 if (error)
5512 goto done;
5514 error = get_gitconfig_path(&gitconfig_path);
5515 if (error)
5516 goto done;
5517 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5518 gitconfig_path);
5519 if (error != NULL)
5520 goto done;
5522 error = get_author(&author, repo);
5523 if (error)
5524 return error;
5527 * unveil(2) traverses exec(2); if an editor is used we have
5528 * to apply unveil after the log message has been written.
5530 if (logmsg == NULL || strlen(logmsg) == 0)
5531 error = get_editor(&editor);
5532 else
5533 error = apply_unveil(got_repo_get_path(repo), 0,
5534 got_worktree_get_root_path(worktree));
5535 if (error)
5536 goto done;
5538 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5539 if (error)
5540 goto done;
5542 cl_arg.editor = editor;
5543 cl_arg.cmdline_log = logmsg;
5544 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5545 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5546 if (!histedit_in_progress) {
5547 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5548 error = got_error(GOT_ERR_COMMIT_BRANCH);
5549 goto done;
5551 cl_arg.branch_name += 11;
5553 cl_arg.repo_path = got_repo_get_path(repo);
5554 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5555 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5556 if (error) {
5557 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5558 cl_arg.logmsg_path != NULL)
5559 preserve_logmsg = 1;
5560 goto done;
5563 error = got_object_id_str(&id_str, id);
5564 if (error)
5565 goto done;
5566 printf("Created commit %s\n", id_str);
5567 done:
5568 if (preserve_logmsg) {
5569 fprintf(stderr, "%s: log message preserved in %s\n",
5570 getprogname(), cl_arg.logmsg_path);
5571 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5572 error == NULL)
5573 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5574 free(cl_arg.logmsg_path);
5575 if (repo)
5576 got_repo_close(repo);
5577 if (worktree)
5578 got_worktree_close(worktree);
5579 free(cwd);
5580 free(id_str);
5581 free(gitconfig_path);
5582 free(editor);
5583 free(author);
5584 return error;
5587 __dead static void
5588 usage_cherrypick(void)
5590 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5591 exit(1);
5594 static const struct got_error *
5595 cmd_cherrypick(int argc, char *argv[])
5597 const struct got_error *error = NULL;
5598 struct got_worktree *worktree = NULL;
5599 struct got_repository *repo = NULL;
5600 char *cwd = NULL, *commit_id_str = NULL;
5601 struct got_object_id *commit_id = NULL;
5602 struct got_commit_object *commit = NULL;
5603 struct got_object_qid *pid;
5604 struct got_reference *head_ref = NULL;
5605 int ch, did_something = 0;
5607 while ((ch = getopt(argc, argv, "")) != -1) {
5608 switch (ch) {
5609 default:
5610 usage_cherrypick();
5611 /* NOTREACHED */
5615 argc -= optind;
5616 argv += optind;
5618 #ifndef PROFILE
5619 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5620 "unveil", NULL) == -1)
5621 err(1, "pledge");
5622 #endif
5623 if (argc != 1)
5624 usage_cherrypick();
5626 cwd = getcwd(NULL, 0);
5627 if (cwd == NULL) {
5628 error = got_error_from_errno("getcwd");
5629 goto done;
5631 error = got_worktree_open(&worktree, cwd);
5632 if (error)
5633 goto done;
5635 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5636 NULL);
5637 if (error != NULL)
5638 goto done;
5640 error = apply_unveil(got_repo_get_path(repo), 0,
5641 got_worktree_get_root_path(worktree));
5642 if (error)
5643 goto done;
5645 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5646 GOT_OBJ_TYPE_COMMIT, repo);
5647 if (error != NULL) {
5648 struct got_reference *ref;
5649 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5650 goto done;
5651 error = got_ref_open(&ref, repo, argv[0], 0);
5652 if (error != NULL)
5653 goto done;
5654 error = got_ref_resolve(&commit_id, repo, ref);
5655 got_ref_close(ref);
5656 if (error != NULL)
5657 goto done;
5659 error = got_object_id_str(&commit_id_str, commit_id);
5660 if (error)
5661 goto done;
5663 error = got_ref_open(&head_ref, repo,
5664 got_worktree_get_head_ref_name(worktree), 0);
5665 if (error != NULL)
5666 goto done;
5668 error = check_same_branch(commit_id, head_ref, NULL, repo);
5669 if (error) {
5670 if (error->code != GOT_ERR_ANCESTRY)
5671 goto done;
5672 error = NULL;
5673 } else {
5674 error = got_error(GOT_ERR_SAME_BRANCH);
5675 goto done;
5678 error = got_object_open_as_commit(&commit, repo, commit_id);
5679 if (error)
5680 goto done;
5681 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5682 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5683 commit_id, repo, update_progress, &did_something, check_cancelled,
5684 NULL);
5685 if (error != NULL)
5686 goto done;
5688 if (did_something)
5689 printf("Merged commit %s\n", commit_id_str);
5690 done:
5691 if (commit)
5692 got_object_commit_close(commit);
5693 free(commit_id_str);
5694 if (head_ref)
5695 got_ref_close(head_ref);
5696 if (worktree)
5697 got_worktree_close(worktree);
5698 if (repo)
5699 got_repo_close(repo);
5700 return error;
5703 __dead static void
5704 usage_backout(void)
5706 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5707 exit(1);
5710 static const struct got_error *
5711 cmd_backout(int argc, char *argv[])
5713 const struct got_error *error = NULL;
5714 struct got_worktree *worktree = NULL;
5715 struct got_repository *repo = NULL;
5716 char *cwd = NULL, *commit_id_str = NULL;
5717 struct got_object_id *commit_id = NULL;
5718 struct got_commit_object *commit = NULL;
5719 struct got_object_qid *pid;
5720 struct got_reference *head_ref = NULL;
5721 int ch, did_something = 0;
5723 while ((ch = getopt(argc, argv, "")) != -1) {
5724 switch (ch) {
5725 default:
5726 usage_backout();
5727 /* NOTREACHED */
5731 argc -= optind;
5732 argv += optind;
5734 #ifndef PROFILE
5735 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5736 "unveil", NULL) == -1)
5737 err(1, "pledge");
5738 #endif
5739 if (argc != 1)
5740 usage_backout();
5742 cwd = getcwd(NULL, 0);
5743 if (cwd == NULL) {
5744 error = got_error_from_errno("getcwd");
5745 goto done;
5747 error = got_worktree_open(&worktree, cwd);
5748 if (error)
5749 goto done;
5751 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5752 NULL);
5753 if (error != NULL)
5754 goto done;
5756 error = apply_unveil(got_repo_get_path(repo), 0,
5757 got_worktree_get_root_path(worktree));
5758 if (error)
5759 goto done;
5761 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5762 GOT_OBJ_TYPE_COMMIT, repo);
5763 if (error != NULL) {
5764 struct got_reference *ref;
5765 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5766 goto done;
5767 error = got_ref_open(&ref, repo, argv[0], 0);
5768 if (error != NULL)
5769 goto done;
5770 error = got_ref_resolve(&commit_id, repo, ref);
5771 got_ref_close(ref);
5772 if (error != NULL)
5773 goto done;
5775 error = got_object_id_str(&commit_id_str, commit_id);
5776 if (error)
5777 goto done;
5779 error = got_ref_open(&head_ref, repo,
5780 got_worktree_get_head_ref_name(worktree), 0);
5781 if (error != NULL)
5782 goto done;
5784 error = check_same_branch(commit_id, head_ref, NULL, repo);
5785 if (error)
5786 goto done;
5788 error = got_object_open_as_commit(&commit, repo, commit_id);
5789 if (error)
5790 goto done;
5791 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5792 if (pid == NULL) {
5793 error = got_error(GOT_ERR_ROOT_COMMIT);
5794 goto done;
5797 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5798 update_progress, &did_something, check_cancelled, NULL);
5799 if (error != NULL)
5800 goto done;
5802 if (did_something)
5803 printf("Backed out commit %s\n", commit_id_str);
5804 done:
5805 if (commit)
5806 got_object_commit_close(commit);
5807 free(commit_id_str);
5808 if (head_ref)
5809 got_ref_close(head_ref);
5810 if (worktree)
5811 got_worktree_close(worktree);
5812 if (repo)
5813 got_repo_close(repo);
5814 return error;
5817 __dead static void
5818 usage_rebase(void)
5820 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5821 getprogname());
5822 exit(1);
5825 void
5826 trim_logmsg(char *logmsg, int limit)
5828 char *nl;
5829 size_t len;
5831 len = strlen(logmsg);
5832 if (len > limit)
5833 len = limit;
5834 logmsg[len] = '\0';
5835 nl = strchr(logmsg, '\n');
5836 if (nl)
5837 *nl = '\0';
5840 static const struct got_error *
5841 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5843 const struct got_error *err;
5844 char *logmsg0 = NULL;
5845 const char *s;
5847 err = got_object_commit_get_logmsg(&logmsg0, commit);
5848 if (err)
5849 return err;
5851 s = logmsg0;
5852 while (isspace((unsigned char)s[0]))
5853 s++;
5855 *logmsg = strdup(s);
5856 if (*logmsg == NULL) {
5857 err = got_error_from_errno("strdup");
5858 goto done;
5861 trim_logmsg(*logmsg, limit);
5862 done:
5863 free(logmsg0);
5864 return err;
5867 static const struct got_error *
5868 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5870 const struct got_error *err;
5871 struct got_commit_object *commit = NULL;
5872 char *id_str = NULL, *logmsg = NULL;
5874 err = got_object_open_as_commit(&commit, repo, id);
5875 if (err)
5876 return err;
5878 err = got_object_id_str(&id_str, id);
5879 if (err)
5880 goto done;
5882 id_str[12] = '\0';
5884 err = get_short_logmsg(&logmsg, 42, commit);
5885 if (err)
5886 goto done;
5888 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5889 done:
5890 free(id_str);
5891 got_object_commit_close(commit);
5892 free(logmsg);
5893 return err;
5896 static const struct got_error *
5897 show_rebase_progress(struct got_commit_object *commit,
5898 struct got_object_id *old_id, struct got_object_id *new_id)
5900 const struct got_error *err;
5901 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5903 err = got_object_id_str(&old_id_str, old_id);
5904 if (err)
5905 goto done;
5907 if (new_id) {
5908 err = got_object_id_str(&new_id_str, new_id);
5909 if (err)
5910 goto done;
5913 old_id_str[12] = '\0';
5914 if (new_id_str)
5915 new_id_str[12] = '\0';
5917 err = get_short_logmsg(&logmsg, 42, commit);
5918 if (err)
5919 goto done;
5921 printf("%s -> %s: %s\n", old_id_str,
5922 new_id_str ? new_id_str : "no-op change", logmsg);
5923 done:
5924 free(old_id_str);
5925 free(new_id_str);
5926 free(logmsg);
5927 return err;
5930 static const struct got_error *
5931 rebase_progress(void *arg, unsigned char status, const char *path)
5933 unsigned char *rebase_status = arg;
5935 while (path[0] == '/')
5936 path++;
5937 printf("%c %s\n", status, path);
5939 if (*rebase_status == GOT_STATUS_CONFLICT)
5940 return NULL;
5941 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5942 *rebase_status = status;
5943 return NULL;
5946 static const struct got_error *
5947 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5948 struct got_reference *branch, struct got_reference *new_base_branch,
5949 struct got_reference *tmp_branch, struct got_repository *repo)
5951 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5952 return got_worktree_rebase_complete(worktree, fileindex,
5953 new_base_branch, tmp_branch, branch, repo);
5956 static const struct got_error *
5957 rebase_commit(struct got_pathlist_head *merged_paths,
5958 struct got_worktree *worktree, struct got_fileindex *fileindex,
5959 struct got_reference *tmp_branch,
5960 struct got_object_id *commit_id, struct got_repository *repo)
5962 const struct got_error *error;
5963 struct got_commit_object *commit;
5964 struct got_object_id *new_commit_id;
5966 error = got_object_open_as_commit(&commit, repo, commit_id);
5967 if (error)
5968 return error;
5970 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5971 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5972 if (error) {
5973 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5974 goto done;
5975 error = show_rebase_progress(commit, commit_id, NULL);
5976 } else {
5977 error = show_rebase_progress(commit, commit_id, new_commit_id);
5978 free(new_commit_id);
5980 done:
5981 got_object_commit_close(commit);
5982 return error;
5985 struct check_path_prefix_arg {
5986 const char *path_prefix;
5987 size_t len;
5988 int errcode;
5991 static const struct got_error *
5992 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5993 struct got_blob_object *blob2, struct got_object_id *id1,
5994 struct got_object_id *id2, const char *path1, const char *path2,
5995 mode_t mode1, mode_t mode2, struct got_repository *repo)
5997 struct check_path_prefix_arg *a = arg;
5999 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6000 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6001 return got_error(a->errcode);
6003 return NULL;
6006 static const struct got_error *
6007 check_path_prefix(struct got_object_id *parent_id,
6008 struct got_object_id *commit_id, const char *path_prefix,
6009 int errcode, struct got_repository *repo)
6011 const struct got_error *err;
6012 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6013 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6014 struct check_path_prefix_arg cpp_arg;
6016 if (got_path_is_root_dir(path_prefix))
6017 return NULL;
6019 err = got_object_open_as_commit(&commit, repo, commit_id);
6020 if (err)
6021 goto done;
6023 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6024 if (err)
6025 goto done;
6027 err = got_object_open_as_tree(&tree1, repo,
6028 got_object_commit_get_tree_id(parent_commit));
6029 if (err)
6030 goto done;
6032 err = got_object_open_as_tree(&tree2, repo,
6033 got_object_commit_get_tree_id(commit));
6034 if (err)
6035 goto done;
6037 cpp_arg.path_prefix = path_prefix;
6038 while (cpp_arg.path_prefix[0] == '/')
6039 cpp_arg.path_prefix++;
6040 cpp_arg.len = strlen(cpp_arg.path_prefix);
6041 cpp_arg.errcode = errcode;
6042 err = got_diff_tree(tree1, tree2, "", "", repo,
6043 check_path_prefix_in_diff, &cpp_arg, 0);
6044 done:
6045 if (tree1)
6046 got_object_tree_close(tree1);
6047 if (tree2)
6048 got_object_tree_close(tree2);
6049 if (commit)
6050 got_object_commit_close(commit);
6051 if (parent_commit)
6052 got_object_commit_close(parent_commit);
6053 return err;
6056 static const struct got_error *
6057 collect_commits(struct got_object_id_queue *commits,
6058 struct got_object_id *initial_commit_id,
6059 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6060 const char *path_prefix, int path_prefix_errcode,
6061 struct got_repository *repo)
6063 const struct got_error *err = NULL;
6064 struct got_commit_graph *graph = NULL;
6065 struct got_object_id *parent_id = NULL;
6066 struct got_object_qid *qid;
6067 struct got_object_id *commit_id = initial_commit_id;
6069 err = got_commit_graph_open(&graph, "/", 1);
6070 if (err)
6071 return err;
6073 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6074 check_cancelled, NULL);
6075 if (err)
6076 goto done;
6077 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6078 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6079 check_cancelled, NULL);
6080 if (err) {
6081 if (err->code == GOT_ERR_ITER_COMPLETED) {
6082 err = got_error_msg(GOT_ERR_ANCESTRY,
6083 "ran out of commits to rebase before "
6084 "youngest common ancestor commit has "
6085 "been reached?!?");
6087 goto done;
6088 } else {
6089 err = check_path_prefix(parent_id, commit_id,
6090 path_prefix, path_prefix_errcode, repo);
6091 if (err)
6092 goto done;
6094 err = got_object_qid_alloc(&qid, commit_id);
6095 if (err)
6096 goto done;
6097 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6098 commit_id = parent_id;
6101 done:
6102 got_commit_graph_close(graph);
6103 return err;
6106 static const struct got_error *
6107 cmd_rebase(int argc, char *argv[])
6109 const struct got_error *error = NULL;
6110 struct got_worktree *worktree = NULL;
6111 struct got_repository *repo = NULL;
6112 struct got_fileindex *fileindex = NULL;
6113 char *cwd = NULL;
6114 struct got_reference *branch = NULL;
6115 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6116 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6117 struct got_object_id *resume_commit_id = NULL;
6118 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6119 struct got_commit_object *commit = NULL;
6120 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6121 int histedit_in_progress = 0;
6122 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6123 struct got_object_id_queue commits;
6124 struct got_pathlist_head merged_paths;
6125 const struct got_object_id_queue *parent_ids;
6126 struct got_object_qid *qid, *pid;
6128 SIMPLEQ_INIT(&commits);
6129 TAILQ_INIT(&merged_paths);
6131 while ((ch = getopt(argc, argv, "ac")) != -1) {
6132 switch (ch) {
6133 case 'a':
6134 abort_rebase = 1;
6135 break;
6136 case 'c':
6137 continue_rebase = 1;
6138 break;
6139 default:
6140 usage_rebase();
6141 /* NOTREACHED */
6145 argc -= optind;
6146 argv += optind;
6148 #ifndef PROFILE
6149 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6150 "unveil", NULL) == -1)
6151 err(1, "pledge");
6152 #endif
6153 if (abort_rebase && continue_rebase)
6154 usage_rebase();
6155 else if (abort_rebase || continue_rebase) {
6156 if (argc != 0)
6157 usage_rebase();
6158 } else if (argc != 1)
6159 usage_rebase();
6161 cwd = getcwd(NULL, 0);
6162 if (cwd == NULL) {
6163 error = got_error_from_errno("getcwd");
6164 goto done;
6166 error = got_worktree_open(&worktree, cwd);
6167 if (error)
6168 goto done;
6170 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6171 NULL);
6172 if (error != NULL)
6173 goto done;
6175 error = apply_unveil(got_repo_get_path(repo), 0,
6176 got_worktree_get_root_path(worktree));
6177 if (error)
6178 goto done;
6180 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6181 worktree);
6182 if (error)
6183 goto done;
6184 if (histedit_in_progress) {
6185 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6186 goto done;
6189 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6190 if (error)
6191 goto done;
6193 if (abort_rebase) {
6194 int did_something;
6195 if (!rebase_in_progress) {
6196 error = got_error(GOT_ERR_NOT_REBASING);
6197 goto done;
6199 error = got_worktree_rebase_continue(&resume_commit_id,
6200 &new_base_branch, &tmp_branch, &branch, &fileindex,
6201 worktree, repo);
6202 if (error)
6203 goto done;
6204 printf("Switching work tree to %s\n",
6205 got_ref_get_symref_target(new_base_branch));
6206 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6207 new_base_branch, update_progress, &did_something);
6208 if (error)
6209 goto done;
6210 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6211 goto done; /* nothing else to do */
6214 if (continue_rebase) {
6215 if (!rebase_in_progress) {
6216 error = got_error(GOT_ERR_NOT_REBASING);
6217 goto done;
6219 error = got_worktree_rebase_continue(&resume_commit_id,
6220 &new_base_branch, &tmp_branch, &branch, &fileindex,
6221 worktree, repo);
6222 if (error)
6223 goto done;
6225 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6226 resume_commit_id, repo);
6227 if (error)
6228 goto done;
6230 yca_id = got_object_id_dup(resume_commit_id);
6231 if (yca_id == NULL) {
6232 error = got_error_from_errno("got_object_id_dup");
6233 goto done;
6235 } else {
6236 error = got_ref_open(&branch, repo, argv[0], 0);
6237 if (error != NULL)
6238 goto done;
6241 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6242 if (error)
6243 goto done;
6245 if (!continue_rebase) {
6246 struct got_object_id *base_commit_id;
6248 base_commit_id = got_worktree_get_base_commit_id(worktree);
6249 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6250 base_commit_id, branch_head_commit_id, repo,
6251 check_cancelled, NULL);
6252 if (error)
6253 goto done;
6254 if (yca_id == NULL) {
6255 error = got_error_msg(GOT_ERR_ANCESTRY,
6256 "specified branch shares no common ancestry "
6257 "with work tree's branch");
6258 goto done;
6261 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6262 if (error) {
6263 if (error->code != GOT_ERR_ANCESTRY)
6264 goto done;
6265 error = NULL;
6266 } else {
6267 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6268 "specified branch resolves to a commit which "
6269 "is already contained in work tree's branch");
6270 goto done;
6272 error = got_worktree_rebase_prepare(&new_base_branch,
6273 &tmp_branch, &fileindex, worktree, branch, repo);
6274 if (error)
6275 goto done;
6278 commit_id = branch_head_commit_id;
6279 error = got_object_open_as_commit(&commit, repo, commit_id);
6280 if (error)
6281 goto done;
6283 parent_ids = got_object_commit_get_parent_ids(commit);
6284 pid = SIMPLEQ_FIRST(parent_ids);
6285 if (pid == NULL) {
6286 if (!continue_rebase) {
6287 int did_something;
6288 error = got_worktree_rebase_abort(worktree, fileindex,
6289 repo, new_base_branch, update_progress,
6290 &did_something);
6291 if (error)
6292 goto done;
6293 printf("Rebase of %s aborted\n",
6294 got_ref_get_name(branch));
6296 error = got_error(GOT_ERR_EMPTY_REBASE);
6297 goto done;
6299 error = collect_commits(&commits, commit_id, pid->id,
6300 yca_id, got_worktree_get_path_prefix(worktree),
6301 GOT_ERR_REBASE_PATH, repo);
6302 got_object_commit_close(commit);
6303 commit = NULL;
6304 if (error)
6305 goto done;
6307 if (SIMPLEQ_EMPTY(&commits)) {
6308 if (continue_rebase) {
6309 error = rebase_complete(worktree, fileindex,
6310 branch, new_base_branch, tmp_branch, repo);
6311 goto done;
6312 } else {
6313 /* Fast-forward the reference of the branch. */
6314 struct got_object_id *new_head_commit_id;
6315 char *id_str;
6316 error = got_ref_resolve(&new_head_commit_id, repo,
6317 new_base_branch);
6318 if (error)
6319 goto done;
6320 error = got_object_id_str(&id_str, new_head_commit_id);
6321 printf("Forwarding %s to commit %s\n",
6322 got_ref_get_name(branch), id_str);
6323 free(id_str);
6324 error = got_ref_change_ref(branch,
6325 new_head_commit_id);
6326 if (error)
6327 goto done;
6331 pid = NULL;
6332 SIMPLEQ_FOREACH(qid, &commits, entry) {
6333 commit_id = qid->id;
6334 parent_id = pid ? pid->id : yca_id;
6335 pid = qid;
6337 error = got_worktree_rebase_merge_files(&merged_paths,
6338 worktree, fileindex, parent_id, commit_id, repo,
6339 rebase_progress, &rebase_status, check_cancelled, NULL);
6340 if (error)
6341 goto done;
6343 if (rebase_status == GOT_STATUS_CONFLICT) {
6344 error = show_rebase_merge_conflict(qid->id, repo);
6345 if (error)
6346 goto done;
6347 got_worktree_rebase_pathlist_free(&merged_paths);
6348 break;
6351 error = rebase_commit(&merged_paths, worktree, fileindex,
6352 tmp_branch, commit_id, repo);
6353 got_worktree_rebase_pathlist_free(&merged_paths);
6354 if (error)
6355 goto done;
6358 if (rebase_status == GOT_STATUS_CONFLICT) {
6359 error = got_worktree_rebase_postpone(worktree, fileindex);
6360 if (error)
6361 goto done;
6362 error = got_error_msg(GOT_ERR_CONFLICTS,
6363 "conflicts must be resolved before rebasing can continue");
6364 } else
6365 error = rebase_complete(worktree, fileindex, branch,
6366 new_base_branch, tmp_branch, repo);
6367 done:
6368 got_object_id_queue_free(&commits);
6369 free(branch_head_commit_id);
6370 free(resume_commit_id);
6371 free(yca_id);
6372 if (commit)
6373 got_object_commit_close(commit);
6374 if (branch)
6375 got_ref_close(branch);
6376 if (new_base_branch)
6377 got_ref_close(new_base_branch);
6378 if (tmp_branch)
6379 got_ref_close(tmp_branch);
6380 if (worktree)
6381 got_worktree_close(worktree);
6382 if (repo)
6383 got_repo_close(repo);
6384 return error;
6387 __dead static void
6388 usage_histedit(void)
6390 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6391 getprogname());
6392 exit(1);
6395 #define GOT_HISTEDIT_PICK 'p'
6396 #define GOT_HISTEDIT_EDIT 'e'
6397 #define GOT_HISTEDIT_FOLD 'f'
6398 #define GOT_HISTEDIT_DROP 'd'
6399 #define GOT_HISTEDIT_MESG 'm'
6401 static struct got_histedit_cmd {
6402 unsigned char code;
6403 const char *name;
6404 const char *desc;
6405 } got_histedit_cmds[] = {
6406 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6407 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6408 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6409 "be used" },
6410 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6411 { GOT_HISTEDIT_MESG, "mesg",
6412 "single-line log message for commit above (open editor if empty)" },
6415 struct got_histedit_list_entry {
6416 TAILQ_ENTRY(got_histedit_list_entry) entry;
6417 struct got_object_id *commit_id;
6418 const struct got_histedit_cmd *cmd;
6419 char *logmsg;
6421 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6423 static const struct got_error *
6424 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6425 FILE *f, struct got_repository *repo)
6427 const struct got_error *err = NULL;
6428 char *logmsg = NULL, *id_str = NULL;
6429 struct got_commit_object *commit = NULL;
6430 int n;
6432 err = got_object_open_as_commit(&commit, repo, commit_id);
6433 if (err)
6434 goto done;
6436 err = get_short_logmsg(&logmsg, 34, commit);
6437 if (err)
6438 goto done;
6440 err = got_object_id_str(&id_str, commit_id);
6441 if (err)
6442 goto done;
6444 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6445 if (n < 0)
6446 err = got_ferror(f, GOT_ERR_IO);
6447 done:
6448 if (commit)
6449 got_object_commit_close(commit);
6450 free(id_str);
6451 free(logmsg);
6452 return err;
6455 static const struct got_error *
6456 histedit_write_commit_list(struct got_object_id_queue *commits,
6457 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6459 const struct got_error *err = NULL;
6460 struct got_object_qid *qid;
6462 if (SIMPLEQ_EMPTY(commits))
6463 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6465 SIMPLEQ_FOREACH(qid, commits, entry) {
6466 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6467 f, repo);
6468 if (err)
6469 break;
6470 if (edit_logmsg_only) {
6471 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6472 if (n < 0) {
6473 err = got_ferror(f, GOT_ERR_IO);
6474 break;
6479 return err;
6482 static const struct got_error *
6483 write_cmd_list(FILE *f, const char *branch_name,
6484 struct got_object_id_queue *commits)
6486 const struct got_error *err = NULL;
6487 int n, i;
6488 char *id_str;
6489 struct got_object_qid *qid;
6491 qid = SIMPLEQ_FIRST(commits);
6492 err = got_object_id_str(&id_str, qid->id);
6493 if (err)
6494 return err;
6496 n = fprintf(f,
6497 "# Editing the history of branch '%s' starting at\n"
6498 "# commit %s\n"
6499 "# Commits will be processed in order from top to "
6500 "bottom of this file.\n", branch_name, id_str);
6501 if (n < 0) {
6502 err = got_ferror(f, GOT_ERR_IO);
6503 goto done;
6506 n = fprintf(f, "# Available histedit commands:\n");
6507 if (n < 0) {
6508 err = got_ferror(f, GOT_ERR_IO);
6509 goto done;
6512 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6513 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6514 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6515 cmd->desc);
6516 if (n < 0) {
6517 err = got_ferror(f, GOT_ERR_IO);
6518 break;
6521 done:
6522 free(id_str);
6523 return err;
6526 static const struct got_error *
6527 histedit_syntax_error(int lineno)
6529 static char msg[42];
6530 int ret;
6532 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6533 lineno);
6534 if (ret == -1 || ret >= sizeof(msg))
6535 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6537 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6540 static const struct got_error *
6541 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6542 char *logmsg, struct got_repository *repo)
6544 const struct got_error *err;
6545 struct got_commit_object *folded_commit = NULL;
6546 char *id_str, *folded_logmsg = NULL;
6548 err = got_object_id_str(&id_str, hle->commit_id);
6549 if (err)
6550 return err;
6552 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6553 if (err)
6554 goto done;
6556 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6557 if (err)
6558 goto done;
6559 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6560 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6561 folded_logmsg) == -1) {
6562 err = got_error_from_errno("asprintf");
6564 done:
6565 if (folded_commit)
6566 got_object_commit_close(folded_commit);
6567 free(id_str);
6568 free(folded_logmsg);
6569 return err;
6572 static struct got_histedit_list_entry *
6573 get_folded_commits(struct got_histedit_list_entry *hle)
6575 struct got_histedit_list_entry *prev, *folded = NULL;
6577 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6578 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6579 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6580 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6581 folded = prev;
6582 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6585 return folded;
6588 static const struct got_error *
6589 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6590 struct got_repository *repo)
6592 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6593 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6594 const struct got_error *err = NULL;
6595 struct got_commit_object *commit = NULL;
6596 int fd;
6597 struct got_histedit_list_entry *folded = NULL;
6599 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6600 if (err)
6601 return err;
6603 folded = get_folded_commits(hle);
6604 if (folded) {
6605 while (folded != hle) {
6606 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6607 folded = TAILQ_NEXT(folded, entry);
6608 continue;
6610 err = append_folded_commit_msg(&new_msg, folded,
6611 logmsg, repo);
6612 if (err)
6613 goto done;
6614 free(logmsg);
6615 logmsg = new_msg;
6616 folded = TAILQ_NEXT(folded, entry);
6620 err = got_object_id_str(&id_str, hle->commit_id);
6621 if (err)
6622 goto done;
6623 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6624 if (err)
6625 goto done;
6626 if (asprintf(&new_msg,
6627 "%s\n# original log message of commit %s: %s",
6628 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6629 err = got_error_from_errno("asprintf");
6630 goto done;
6632 free(logmsg);
6633 logmsg = new_msg;
6635 err = got_object_id_str(&id_str, hle->commit_id);
6636 if (err)
6637 goto done;
6639 err = got_opentemp_named_fd(&logmsg_path, &fd,
6640 GOT_TMPDIR_STR "/got-logmsg");
6641 if (err)
6642 goto done;
6644 dprintf(fd, logmsg);
6645 close(fd);
6647 err = get_editor(&editor);
6648 if (err)
6649 goto done;
6651 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6652 if (err) {
6653 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6654 goto done;
6655 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6657 done:
6658 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6659 err = got_error_from_errno2("unlink", logmsg_path);
6660 free(logmsg_path);
6661 free(logmsg);
6662 free(orig_logmsg);
6663 free(editor);
6664 if (commit)
6665 got_object_commit_close(commit);
6666 return err;
6669 static const struct got_error *
6670 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6671 FILE *f, struct got_repository *repo)
6673 const struct got_error *err = NULL;
6674 char *line = NULL, *p, *end;
6675 size_t size;
6676 ssize_t len;
6677 int lineno = 0, i;
6678 const struct got_histedit_cmd *cmd;
6679 struct got_object_id *commit_id = NULL;
6680 struct got_histedit_list_entry *hle = NULL;
6682 for (;;) {
6683 len = getline(&line, &size, f);
6684 if (len == -1) {
6685 const struct got_error *getline_err;
6686 if (feof(f))
6687 break;
6688 getline_err = got_error_from_errno("getline");
6689 err = got_ferror(f, getline_err->code);
6690 break;
6692 lineno++;
6693 p = line;
6694 while (isspace((unsigned char)p[0]))
6695 p++;
6696 if (p[0] == '#' || p[0] == '\0') {
6697 free(line);
6698 line = NULL;
6699 continue;
6701 cmd = NULL;
6702 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6703 cmd = &got_histedit_cmds[i];
6704 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6705 isspace((unsigned char)p[strlen(cmd->name)])) {
6706 p += strlen(cmd->name);
6707 break;
6709 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6710 p++;
6711 break;
6714 if (i == nitems(got_histedit_cmds)) {
6715 err = histedit_syntax_error(lineno);
6716 break;
6718 while (isspace((unsigned char)p[0]))
6719 p++;
6720 if (cmd->code == GOT_HISTEDIT_MESG) {
6721 if (hle == NULL || hle->logmsg != NULL) {
6722 err = got_error(GOT_ERR_HISTEDIT_CMD);
6723 break;
6725 if (p[0] == '\0') {
6726 err = histedit_edit_logmsg(hle, repo);
6727 if (err)
6728 break;
6729 } else {
6730 hle->logmsg = strdup(p);
6731 if (hle->logmsg == NULL) {
6732 err = got_error_from_errno("strdup");
6733 break;
6736 free(line);
6737 line = NULL;
6738 continue;
6739 } else {
6740 end = p;
6741 while (end[0] && !isspace((unsigned char)end[0]))
6742 end++;
6743 *end = '\0';
6745 err = got_object_resolve_id_str(&commit_id, repo, p);
6746 if (err) {
6747 /* override error code */
6748 err = histedit_syntax_error(lineno);
6749 break;
6752 hle = malloc(sizeof(*hle));
6753 if (hle == NULL) {
6754 err = got_error_from_errno("malloc");
6755 break;
6757 hle->cmd = cmd;
6758 hle->commit_id = commit_id;
6759 hle->logmsg = NULL;
6760 commit_id = NULL;
6761 free(line);
6762 line = NULL;
6763 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6766 free(line);
6767 free(commit_id);
6768 return err;
6771 static const struct got_error *
6772 histedit_check_script(struct got_histedit_list *histedit_cmds,
6773 struct got_object_id_queue *commits, struct got_repository *repo)
6775 const struct got_error *err = NULL;
6776 struct got_object_qid *qid;
6777 struct got_histedit_list_entry *hle;
6778 static char msg[92];
6779 char *id_str;
6781 if (TAILQ_EMPTY(histedit_cmds))
6782 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6783 "histedit script contains no commands");
6784 if (SIMPLEQ_EMPTY(commits))
6785 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6787 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6788 struct got_histedit_list_entry *hle2;
6789 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6790 if (hle == hle2)
6791 continue;
6792 if (got_object_id_cmp(hle->commit_id,
6793 hle2->commit_id) != 0)
6794 continue;
6795 err = got_object_id_str(&id_str, hle->commit_id);
6796 if (err)
6797 return err;
6798 snprintf(msg, sizeof(msg), "commit %s is listed "
6799 "more than once in histedit script", id_str);
6800 free(id_str);
6801 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6805 SIMPLEQ_FOREACH(qid, commits, entry) {
6806 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6807 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6808 break;
6810 if (hle == NULL) {
6811 err = got_object_id_str(&id_str, qid->id);
6812 if (err)
6813 return err;
6814 snprintf(msg, sizeof(msg),
6815 "commit %s missing from histedit script", id_str);
6816 free(id_str);
6817 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6821 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6822 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6823 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6824 "last commit in histedit script cannot be folded");
6826 return NULL;
6829 static const struct got_error *
6830 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6831 const char *path, struct got_object_id_queue *commits,
6832 struct got_repository *repo)
6834 const struct got_error *err = NULL;
6835 char *editor;
6836 FILE *f = NULL;
6838 err = get_editor(&editor);
6839 if (err)
6840 return err;
6842 if (spawn_editor(editor, path) == -1) {
6843 err = got_error_from_errno("failed spawning editor");
6844 goto done;
6847 f = fopen(path, "r");
6848 if (f == NULL) {
6849 err = got_error_from_errno("fopen");
6850 goto done;
6852 err = histedit_parse_list(histedit_cmds, f, repo);
6853 if (err)
6854 goto done;
6856 err = histedit_check_script(histedit_cmds, commits, repo);
6857 done:
6858 if (f && fclose(f) != 0 && err == NULL)
6859 err = got_error_from_errno("fclose");
6860 free(editor);
6861 return err;
6864 static const struct got_error *
6865 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6866 struct got_object_id_queue *, const char *, const char *,
6867 struct got_repository *);
6869 static const struct got_error *
6870 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6871 struct got_object_id_queue *commits, const char *branch_name,
6872 int edit_logmsg_only, struct got_repository *repo)
6874 const struct got_error *err;
6875 FILE *f = NULL;
6876 char *path = NULL;
6878 err = got_opentemp_named(&path, &f, "got-histedit");
6879 if (err)
6880 return err;
6882 err = write_cmd_list(f, branch_name, commits);
6883 if (err)
6884 goto done;
6886 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6887 if (err)
6888 goto done;
6890 if (edit_logmsg_only) {
6891 rewind(f);
6892 err = histedit_parse_list(histedit_cmds, f, repo);
6893 } else {
6894 if (fclose(f) != 0) {
6895 err = got_error_from_errno("fclose");
6896 goto done;
6898 f = NULL;
6899 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6900 if (err) {
6901 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6902 err->code != GOT_ERR_HISTEDIT_CMD)
6903 goto done;
6904 err = histedit_edit_list_retry(histedit_cmds, err,
6905 commits, path, branch_name, repo);
6908 done:
6909 if (f && fclose(f) != 0 && err == NULL)
6910 err = got_error_from_errno("fclose");
6911 if (path && unlink(path) != 0 && err == NULL)
6912 err = got_error_from_errno2("unlink", path);
6913 free(path);
6914 return err;
6917 static const struct got_error *
6918 histedit_save_list(struct got_histedit_list *histedit_cmds,
6919 struct got_worktree *worktree, struct got_repository *repo)
6921 const struct got_error *err = NULL;
6922 char *path = NULL;
6923 FILE *f = NULL;
6924 struct got_histedit_list_entry *hle;
6925 struct got_commit_object *commit = NULL;
6927 err = got_worktree_get_histedit_script_path(&path, worktree);
6928 if (err)
6929 return err;
6931 f = fopen(path, "w");
6932 if (f == NULL) {
6933 err = got_error_from_errno2("fopen", path);
6934 goto done;
6936 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6937 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6938 repo);
6939 if (err)
6940 break;
6942 if (hle->logmsg) {
6943 int n = fprintf(f, "%c %s\n",
6944 GOT_HISTEDIT_MESG, hle->logmsg);
6945 if (n < 0) {
6946 err = got_ferror(f, GOT_ERR_IO);
6947 break;
6951 done:
6952 if (f && fclose(f) != 0 && err == NULL)
6953 err = got_error_from_errno("fclose");
6954 free(path);
6955 if (commit)
6956 got_object_commit_close(commit);
6957 return err;
6960 void
6961 histedit_free_list(struct got_histedit_list *histedit_cmds)
6963 struct got_histedit_list_entry *hle;
6965 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6966 TAILQ_REMOVE(histedit_cmds, hle, entry);
6967 free(hle);
6971 static const struct got_error *
6972 histedit_load_list(struct got_histedit_list *histedit_cmds,
6973 const char *path, struct got_repository *repo)
6975 const struct got_error *err = NULL;
6976 FILE *f = NULL;
6978 f = fopen(path, "r");
6979 if (f == NULL) {
6980 err = got_error_from_errno2("fopen", path);
6981 goto done;
6984 err = histedit_parse_list(histedit_cmds, f, repo);
6985 done:
6986 if (f && fclose(f) != 0 && err == NULL)
6987 err = got_error_from_errno("fclose");
6988 return err;
6991 static const struct got_error *
6992 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6993 const struct got_error *edit_err, struct got_object_id_queue *commits,
6994 const char *path, const char *branch_name, struct got_repository *repo)
6996 const struct got_error *err = NULL, *prev_err = edit_err;
6997 int resp = ' ';
6999 while (resp != 'c' && resp != 'r' && resp != 'a') {
7000 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7001 "or (a)bort: ", getprogname(), prev_err->msg);
7002 resp = getchar();
7003 if (resp == '\n')
7004 resp = getchar();
7005 if (resp == 'c') {
7006 histedit_free_list(histedit_cmds);
7007 err = histedit_run_editor(histedit_cmds, path, commits,
7008 repo);
7009 if (err) {
7010 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7011 err->code != GOT_ERR_HISTEDIT_CMD)
7012 break;
7013 prev_err = err;
7014 resp = ' ';
7015 continue;
7017 break;
7018 } else if (resp == 'r') {
7019 histedit_free_list(histedit_cmds);
7020 err = histedit_edit_script(histedit_cmds,
7021 commits, branch_name, 0, repo);
7022 if (err) {
7023 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7024 err->code != GOT_ERR_HISTEDIT_CMD)
7025 break;
7026 prev_err = err;
7027 resp = ' ';
7028 continue;
7030 break;
7031 } else if (resp == 'a') {
7032 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7033 break;
7034 } else
7035 printf("invalid response '%c'\n", resp);
7038 return err;
7041 static const struct got_error *
7042 histedit_complete(struct got_worktree *worktree,
7043 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7044 struct got_reference *branch, struct got_repository *repo)
7046 printf("Switching work tree to %s\n",
7047 got_ref_get_symref_target(branch));
7048 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7049 branch, repo);
7052 static const struct got_error *
7053 show_histedit_progress(struct got_commit_object *commit,
7054 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7056 const struct got_error *err;
7057 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7059 err = got_object_id_str(&old_id_str, hle->commit_id);
7060 if (err)
7061 goto done;
7063 if (new_id) {
7064 err = got_object_id_str(&new_id_str, new_id);
7065 if (err)
7066 goto done;
7069 old_id_str[12] = '\0';
7070 if (new_id_str)
7071 new_id_str[12] = '\0';
7073 if (hle->logmsg) {
7074 logmsg = strdup(hle->logmsg);
7075 if (logmsg == NULL) {
7076 err = got_error_from_errno("strdup");
7077 goto done;
7079 trim_logmsg(logmsg, 42);
7080 } else {
7081 err = get_short_logmsg(&logmsg, 42, commit);
7082 if (err)
7083 goto done;
7086 switch (hle->cmd->code) {
7087 case GOT_HISTEDIT_PICK:
7088 case GOT_HISTEDIT_EDIT:
7089 printf("%s -> %s: %s\n", old_id_str,
7090 new_id_str ? new_id_str : "no-op change", logmsg);
7091 break;
7092 case GOT_HISTEDIT_DROP:
7093 case GOT_HISTEDIT_FOLD:
7094 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7095 logmsg);
7096 break;
7097 default:
7098 break;
7100 done:
7101 free(old_id_str);
7102 free(new_id_str);
7103 return err;
7106 static const struct got_error *
7107 histedit_commit(struct got_pathlist_head *merged_paths,
7108 struct got_worktree *worktree, struct got_fileindex *fileindex,
7109 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7110 struct got_repository *repo)
7112 const struct got_error *err;
7113 struct got_commit_object *commit;
7114 struct got_object_id *new_commit_id;
7116 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7117 && hle->logmsg == NULL) {
7118 err = histedit_edit_logmsg(hle, repo);
7119 if (err)
7120 return err;
7123 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7124 if (err)
7125 return err;
7127 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7128 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7129 hle->logmsg, repo);
7130 if (err) {
7131 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7132 goto done;
7133 err = show_histedit_progress(commit, hle, NULL);
7134 } else {
7135 err = show_histedit_progress(commit, hle, new_commit_id);
7136 free(new_commit_id);
7138 done:
7139 got_object_commit_close(commit);
7140 return err;
7143 static const struct got_error *
7144 histedit_skip_commit(struct got_histedit_list_entry *hle,
7145 struct got_worktree *worktree, struct got_repository *repo)
7147 const struct got_error *error;
7148 struct got_commit_object *commit;
7150 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7151 repo);
7152 if (error)
7153 return error;
7155 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7156 if (error)
7157 return error;
7159 error = show_histedit_progress(commit, hle, NULL);
7160 got_object_commit_close(commit);
7161 return error;
7164 static const struct got_error *
7165 check_local_changes(void *arg, unsigned char status,
7166 unsigned char staged_status, const char *path,
7167 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7168 struct got_object_id *commit_id, int dirfd, const char *de_name)
7170 int *have_local_changes = arg;
7172 switch (status) {
7173 case GOT_STATUS_ADD:
7174 case GOT_STATUS_DELETE:
7175 case GOT_STATUS_MODIFY:
7176 case GOT_STATUS_CONFLICT:
7177 *have_local_changes = 1;
7178 return got_error(GOT_ERR_CANCELLED);
7179 default:
7180 break;
7183 switch (staged_status) {
7184 case GOT_STATUS_ADD:
7185 case GOT_STATUS_DELETE:
7186 case GOT_STATUS_MODIFY:
7187 *have_local_changes = 1;
7188 return got_error(GOT_ERR_CANCELLED);
7189 default:
7190 break;
7193 return NULL;
7196 static const struct got_error *
7197 cmd_histedit(int argc, char *argv[])
7199 const struct got_error *error = NULL;
7200 struct got_worktree *worktree = NULL;
7201 struct got_fileindex *fileindex = NULL;
7202 struct got_repository *repo = NULL;
7203 char *cwd = NULL;
7204 struct got_reference *branch = NULL;
7205 struct got_reference *tmp_branch = NULL;
7206 struct got_object_id *resume_commit_id = NULL;
7207 struct got_object_id *base_commit_id = NULL;
7208 struct got_object_id *head_commit_id = NULL;
7209 struct got_commit_object *commit = NULL;
7210 int ch, rebase_in_progress = 0, did_something;
7211 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7212 int edit_logmsg_only = 0;
7213 const char *edit_script_path = NULL;
7214 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7215 struct got_object_id_queue commits;
7216 struct got_pathlist_head merged_paths;
7217 const struct got_object_id_queue *parent_ids;
7218 struct got_object_qid *pid;
7219 struct got_histedit_list histedit_cmds;
7220 struct got_histedit_list_entry *hle;
7222 SIMPLEQ_INIT(&commits);
7223 TAILQ_INIT(&histedit_cmds);
7224 TAILQ_INIT(&merged_paths);
7226 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7227 switch (ch) {
7228 case 'a':
7229 abort_edit = 1;
7230 break;
7231 case 'c':
7232 continue_edit = 1;
7233 break;
7234 case 'F':
7235 edit_script_path = optarg;
7236 break;
7237 case 'm':
7238 edit_logmsg_only = 1;
7239 break;
7240 default:
7241 usage_histedit();
7242 /* NOTREACHED */
7246 argc -= optind;
7247 argv += optind;
7249 #ifndef PROFILE
7250 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7251 "unveil", NULL) == -1)
7252 err(1, "pledge");
7253 #endif
7254 if (abort_edit && continue_edit)
7255 errx(1, "histedit's -a and -c options are mutually exclusive");
7256 if (edit_script_path && edit_logmsg_only)
7257 errx(1, "histedit's -F and -m options are mutually exclusive");
7258 if (abort_edit && edit_logmsg_only)
7259 errx(1, "histedit's -a and -m options are mutually exclusive");
7260 if (continue_edit && edit_logmsg_only)
7261 errx(1, "histedit's -c and -m options are mutually exclusive");
7262 if (argc != 0)
7263 usage_histedit();
7266 * This command cannot apply unveil(2) in all cases because the
7267 * user may choose to run an editor to edit the histedit script
7268 * and to edit individual commit log messages.
7269 * unveil(2) traverses exec(2); if an editor is used we have to
7270 * apply unveil after edit script and log messages have been written.
7271 * XXX TODO: Make use of unveil(2) where possible.
7274 cwd = getcwd(NULL, 0);
7275 if (cwd == NULL) {
7276 error = got_error_from_errno("getcwd");
7277 goto done;
7279 error = got_worktree_open(&worktree, cwd);
7280 if (error)
7281 goto done;
7283 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7284 NULL);
7285 if (error != NULL)
7286 goto done;
7288 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7289 if (error)
7290 goto done;
7291 if (rebase_in_progress) {
7292 error = got_error(GOT_ERR_REBASING);
7293 goto done;
7296 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7297 if (error)
7298 goto done;
7300 if (edit_in_progress && edit_logmsg_only) {
7301 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7302 "histedit operation is in progress in this "
7303 "work tree and must be continued or aborted "
7304 "before the -m option can be used");
7305 goto done;
7308 if (edit_in_progress && abort_edit) {
7309 error = got_worktree_histedit_continue(&resume_commit_id,
7310 &tmp_branch, &branch, &base_commit_id, &fileindex,
7311 worktree, repo);
7312 if (error)
7313 goto done;
7314 printf("Switching work tree to %s\n",
7315 got_ref_get_symref_target(branch));
7316 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7317 branch, base_commit_id, update_progress, &did_something);
7318 if (error)
7319 goto done;
7320 printf("Histedit of %s aborted\n",
7321 got_ref_get_symref_target(branch));
7322 goto done; /* nothing else to do */
7323 } else if (abort_edit) {
7324 error = got_error(GOT_ERR_NOT_HISTEDIT);
7325 goto done;
7328 if (continue_edit) {
7329 char *path;
7331 if (!edit_in_progress) {
7332 error = got_error(GOT_ERR_NOT_HISTEDIT);
7333 goto done;
7336 error = got_worktree_get_histedit_script_path(&path, worktree);
7337 if (error)
7338 goto done;
7340 error = histedit_load_list(&histedit_cmds, path, repo);
7341 free(path);
7342 if (error)
7343 goto done;
7345 error = got_worktree_histedit_continue(&resume_commit_id,
7346 &tmp_branch, &branch, &base_commit_id, &fileindex,
7347 worktree, repo);
7348 if (error)
7349 goto done;
7351 error = got_ref_resolve(&head_commit_id, repo, branch);
7352 if (error)
7353 goto done;
7355 error = got_object_open_as_commit(&commit, repo,
7356 head_commit_id);
7357 if (error)
7358 goto done;
7359 parent_ids = got_object_commit_get_parent_ids(commit);
7360 pid = SIMPLEQ_FIRST(parent_ids);
7361 if (pid == NULL) {
7362 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7363 goto done;
7365 error = collect_commits(&commits, head_commit_id, pid->id,
7366 base_commit_id, got_worktree_get_path_prefix(worktree),
7367 GOT_ERR_HISTEDIT_PATH, repo);
7368 got_object_commit_close(commit);
7369 commit = NULL;
7370 if (error)
7371 goto done;
7372 } else {
7373 if (edit_in_progress) {
7374 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7375 goto done;
7378 error = got_ref_open(&branch, repo,
7379 got_worktree_get_head_ref_name(worktree), 0);
7380 if (error != NULL)
7381 goto done;
7383 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7384 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7385 "will not edit commit history of a branch outside "
7386 "the \"refs/heads/\" reference namespace");
7387 goto done;
7390 error = got_ref_resolve(&head_commit_id, repo, branch);
7391 got_ref_close(branch);
7392 branch = NULL;
7393 if (error)
7394 goto done;
7396 error = got_object_open_as_commit(&commit, repo,
7397 head_commit_id);
7398 if (error)
7399 goto done;
7400 parent_ids = got_object_commit_get_parent_ids(commit);
7401 pid = SIMPLEQ_FIRST(parent_ids);
7402 if (pid == NULL) {
7403 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7404 goto done;
7406 error = collect_commits(&commits, head_commit_id, pid->id,
7407 got_worktree_get_base_commit_id(worktree),
7408 got_worktree_get_path_prefix(worktree),
7409 GOT_ERR_HISTEDIT_PATH, repo);
7410 got_object_commit_close(commit);
7411 commit = NULL;
7412 if (error)
7413 goto done;
7415 if (SIMPLEQ_EMPTY(&commits)) {
7416 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7417 goto done;
7420 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7421 &base_commit_id, &fileindex, worktree, repo);
7422 if (error)
7423 goto done;
7425 if (edit_script_path) {
7426 error = histedit_load_list(&histedit_cmds,
7427 edit_script_path, repo);
7428 if (error) {
7429 got_worktree_histedit_abort(worktree, fileindex,
7430 repo, branch, base_commit_id,
7431 update_progress, &did_something);
7432 goto done;
7434 } else {
7435 const char *branch_name;
7436 branch_name = got_ref_get_symref_target(branch);
7437 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7438 branch_name += 11;
7439 error = histedit_edit_script(&histedit_cmds, &commits,
7440 branch_name, edit_logmsg_only, repo);
7441 if (error) {
7442 got_worktree_histedit_abort(worktree, fileindex,
7443 repo, branch, base_commit_id,
7444 update_progress, &did_something);
7445 goto done;
7450 error = histedit_save_list(&histedit_cmds, worktree,
7451 repo);
7452 if (error) {
7453 got_worktree_histedit_abort(worktree, fileindex,
7454 repo, branch, base_commit_id,
7455 update_progress, &did_something);
7456 goto done;
7461 error = histedit_check_script(&histedit_cmds, &commits, repo);
7462 if (error)
7463 goto done;
7465 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7466 if (resume_commit_id) {
7467 if (got_object_id_cmp(hle->commit_id,
7468 resume_commit_id) != 0)
7469 continue;
7471 resume_commit_id = NULL;
7472 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7473 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7474 error = histedit_skip_commit(hle, worktree,
7475 repo);
7476 if (error)
7477 goto done;
7478 } else {
7479 struct got_pathlist_head paths;
7480 int have_changes = 0;
7482 TAILQ_INIT(&paths);
7483 error = got_pathlist_append(&paths, "", NULL);
7484 if (error)
7485 goto done;
7486 error = got_worktree_status(worktree, &paths,
7487 repo, check_local_changes, &have_changes,
7488 check_cancelled, NULL);
7489 got_pathlist_free(&paths);
7490 if (error) {
7491 if (error->code != GOT_ERR_CANCELLED)
7492 goto done;
7493 if (sigint_received || sigpipe_received)
7494 goto done;
7496 if (have_changes) {
7497 error = histedit_commit(NULL, worktree,
7498 fileindex, tmp_branch, hle, repo);
7499 if (error)
7500 goto done;
7501 } else {
7502 error = got_object_open_as_commit(
7503 &commit, repo, hle->commit_id);
7504 if (error)
7505 goto done;
7506 error = show_histedit_progress(commit,
7507 hle, NULL);
7508 got_object_commit_close(commit);
7509 commit = NULL;
7510 if (error)
7511 goto done;
7514 continue;
7517 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7518 error = histedit_skip_commit(hle, worktree, repo);
7519 if (error)
7520 goto done;
7521 continue;
7524 error = got_object_open_as_commit(&commit, repo,
7525 hle->commit_id);
7526 if (error)
7527 goto done;
7528 parent_ids = got_object_commit_get_parent_ids(commit);
7529 pid = SIMPLEQ_FIRST(parent_ids);
7531 error = got_worktree_histedit_merge_files(&merged_paths,
7532 worktree, fileindex, pid->id, hle->commit_id, repo,
7533 rebase_progress, &rebase_status, check_cancelled, NULL);
7534 if (error)
7535 goto done;
7536 got_object_commit_close(commit);
7537 commit = NULL;
7539 if (rebase_status == GOT_STATUS_CONFLICT) {
7540 error = show_rebase_merge_conflict(hle->commit_id,
7541 repo);
7542 if (error)
7543 goto done;
7544 got_worktree_rebase_pathlist_free(&merged_paths);
7545 break;
7548 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7549 char *id_str;
7550 error = got_object_id_str(&id_str, hle->commit_id);
7551 if (error)
7552 goto done;
7553 printf("Stopping histedit for amending commit %s\n",
7554 id_str);
7555 free(id_str);
7556 got_worktree_rebase_pathlist_free(&merged_paths);
7557 error = got_worktree_histedit_postpone(worktree,
7558 fileindex);
7559 goto done;
7562 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7563 error = histedit_skip_commit(hle, worktree, repo);
7564 if (error)
7565 goto done;
7566 continue;
7569 error = histedit_commit(&merged_paths, worktree, fileindex,
7570 tmp_branch, hle, repo);
7571 got_worktree_rebase_pathlist_free(&merged_paths);
7572 if (error)
7573 goto done;
7576 if (rebase_status == GOT_STATUS_CONFLICT) {
7577 error = got_worktree_histedit_postpone(worktree, fileindex);
7578 if (error)
7579 goto done;
7580 error = got_error_msg(GOT_ERR_CONFLICTS,
7581 "conflicts must be resolved before histedit can continue");
7582 } else
7583 error = histedit_complete(worktree, fileindex, tmp_branch,
7584 branch, repo);
7585 done:
7586 got_object_id_queue_free(&commits);
7587 histedit_free_list(&histedit_cmds);
7588 free(head_commit_id);
7589 free(base_commit_id);
7590 free(resume_commit_id);
7591 if (commit)
7592 got_object_commit_close(commit);
7593 if (branch)
7594 got_ref_close(branch);
7595 if (tmp_branch)
7596 got_ref_close(tmp_branch);
7597 if (worktree)
7598 got_worktree_close(worktree);
7599 if (repo)
7600 got_repo_close(repo);
7601 return error;
7604 __dead static void
7605 usage_integrate(void)
7607 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7608 exit(1);
7611 static const struct got_error *
7612 cmd_integrate(int argc, char *argv[])
7614 const struct got_error *error = NULL;
7615 struct got_repository *repo = NULL;
7616 struct got_worktree *worktree = NULL;
7617 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7618 const char *branch_arg = NULL;
7619 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7620 struct got_fileindex *fileindex = NULL;
7621 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7622 int ch, did_something = 0;
7624 while ((ch = getopt(argc, argv, "")) != -1) {
7625 switch (ch) {
7626 default:
7627 usage_integrate();
7628 /* NOTREACHED */
7632 argc -= optind;
7633 argv += optind;
7635 if (argc != 1)
7636 usage_integrate();
7637 branch_arg = argv[0];
7639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7640 "unveil", NULL) == -1)
7641 err(1, "pledge");
7643 cwd = getcwd(NULL, 0);
7644 if (cwd == NULL) {
7645 error = got_error_from_errno("getcwd");
7646 goto done;
7649 error = got_worktree_open(&worktree, cwd);
7650 if (error)
7651 goto done;
7653 error = check_rebase_or_histedit_in_progress(worktree);
7654 if (error)
7655 goto done;
7657 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7658 NULL);
7659 if (error != NULL)
7660 goto done;
7662 error = apply_unveil(got_repo_get_path(repo), 0,
7663 got_worktree_get_root_path(worktree));
7664 if (error)
7665 goto done;
7667 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7668 error = got_error_from_errno("asprintf");
7669 goto done;
7672 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7673 &base_branch_ref, worktree, refname, repo);
7674 if (error)
7675 goto done;
7677 refname = strdup(got_ref_get_name(branch_ref));
7678 if (refname == NULL) {
7679 error = got_error_from_errno("strdup");
7680 got_worktree_integrate_abort(worktree, fileindex, repo,
7681 branch_ref, base_branch_ref);
7682 goto done;
7684 base_refname = strdup(got_ref_get_name(base_branch_ref));
7685 if (base_refname == NULL) {
7686 error = got_error_from_errno("strdup");
7687 got_worktree_integrate_abort(worktree, fileindex, repo,
7688 branch_ref, base_branch_ref);
7689 goto done;
7692 error = got_ref_resolve(&commit_id, repo, branch_ref);
7693 if (error)
7694 goto done;
7696 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7697 if (error)
7698 goto done;
7700 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7701 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7702 "specified branch has already been integrated");
7703 got_worktree_integrate_abort(worktree, fileindex, repo,
7704 branch_ref, base_branch_ref);
7705 goto done;
7708 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7709 if (error) {
7710 if (error->code == GOT_ERR_ANCESTRY)
7711 error = got_error(GOT_ERR_REBASE_REQUIRED);
7712 got_worktree_integrate_abort(worktree, fileindex, repo,
7713 branch_ref, base_branch_ref);
7714 goto done;
7717 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7718 branch_ref, base_branch_ref, update_progress, &did_something,
7719 check_cancelled, NULL);
7720 if (error)
7721 goto done;
7723 printf("Integrated %s into %s\n", refname, base_refname);
7724 done:
7725 if (repo)
7726 got_repo_close(repo);
7727 if (worktree)
7728 got_worktree_close(worktree);
7729 free(cwd);
7730 free(base_commit_id);
7731 free(commit_id);
7732 free(refname);
7733 free(base_refname);
7734 return error;
7737 __dead static void
7738 usage_stage(void)
7740 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7741 "[file-path ...]\n",
7742 getprogname());
7743 exit(1);
7746 static const struct got_error *
7747 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7748 const char *path, struct got_object_id *blob_id,
7749 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7750 int dirfd, const char *de_name)
7752 const struct got_error *err = NULL;
7753 char *id_str = NULL;
7755 if (staged_status != GOT_STATUS_ADD &&
7756 staged_status != GOT_STATUS_MODIFY &&
7757 staged_status != GOT_STATUS_DELETE)
7758 return NULL;
7760 if (staged_status == GOT_STATUS_ADD ||
7761 staged_status == GOT_STATUS_MODIFY)
7762 err = got_object_id_str(&id_str, staged_blob_id);
7763 else
7764 err = got_object_id_str(&id_str, blob_id);
7765 if (err)
7766 return err;
7768 printf("%s %c %s\n", id_str, staged_status, path);
7769 free(id_str);
7770 return NULL;
7773 static const struct got_error *
7774 cmd_stage(int argc, char *argv[])
7776 const struct got_error *error = NULL;
7777 struct got_repository *repo = NULL;
7778 struct got_worktree *worktree = NULL;
7779 char *cwd = NULL;
7780 struct got_pathlist_head paths;
7781 struct got_pathlist_entry *pe;
7782 int ch, list_stage = 0, pflag = 0;
7783 FILE *patch_script_file = NULL;
7784 const char *patch_script_path = NULL;
7785 struct choose_patch_arg cpa;
7787 TAILQ_INIT(&paths);
7789 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7790 switch (ch) {
7791 case 'l':
7792 list_stage = 1;
7793 break;
7794 case 'p':
7795 pflag = 1;
7796 break;
7797 case 'F':
7798 patch_script_path = optarg;
7799 break;
7800 default:
7801 usage_stage();
7802 /* NOTREACHED */
7806 argc -= optind;
7807 argv += optind;
7809 #ifndef PROFILE
7810 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7811 "unveil", NULL) == -1)
7812 err(1, "pledge");
7813 #endif
7814 if (list_stage && (pflag || patch_script_path))
7815 errx(1, "-l option cannot be used with other options");
7816 if (patch_script_path && !pflag)
7817 errx(1, "-F option can only be used together with -p option");
7819 cwd = getcwd(NULL, 0);
7820 if (cwd == NULL) {
7821 error = got_error_from_errno("getcwd");
7822 goto done;
7825 error = got_worktree_open(&worktree, cwd);
7826 if (error)
7827 goto done;
7829 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7830 NULL);
7831 if (error != NULL)
7832 goto done;
7834 if (patch_script_path) {
7835 patch_script_file = fopen(patch_script_path, "r");
7836 if (patch_script_file == NULL) {
7837 error = got_error_from_errno2("fopen",
7838 patch_script_path);
7839 goto done;
7842 error = apply_unveil(got_repo_get_path(repo), 0,
7843 got_worktree_get_root_path(worktree));
7844 if (error)
7845 goto done;
7847 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7848 if (error)
7849 goto done;
7851 if (list_stage)
7852 error = got_worktree_status(worktree, &paths, repo,
7853 print_stage, NULL, check_cancelled, NULL);
7854 else {
7855 cpa.patch_script_file = patch_script_file;
7856 cpa.action = "stage";
7857 error = got_worktree_stage(worktree, &paths,
7858 pflag ? NULL : print_status, NULL,
7859 pflag ? choose_patch : NULL, &cpa, repo);
7861 done:
7862 if (patch_script_file && fclose(patch_script_file) == EOF &&
7863 error == NULL)
7864 error = got_error_from_errno2("fclose", patch_script_path);
7865 if (repo)
7866 got_repo_close(repo);
7867 if (worktree)
7868 got_worktree_close(worktree);
7869 TAILQ_FOREACH(pe, &paths, entry)
7870 free((char *)pe->path);
7871 got_pathlist_free(&paths);
7872 free(cwd);
7873 return error;
7876 __dead static void
7877 usage_unstage(void)
7879 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7880 "[file-path ...]\n",
7881 getprogname());
7882 exit(1);
7886 static const struct got_error *
7887 cmd_unstage(int argc, char *argv[])
7889 const struct got_error *error = NULL;
7890 struct got_repository *repo = NULL;
7891 struct got_worktree *worktree = NULL;
7892 char *cwd = NULL;
7893 struct got_pathlist_head paths;
7894 struct got_pathlist_entry *pe;
7895 int ch, did_something = 0, pflag = 0;
7896 FILE *patch_script_file = NULL;
7897 const char *patch_script_path = NULL;
7898 struct choose_patch_arg cpa;
7900 TAILQ_INIT(&paths);
7902 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7903 switch (ch) {
7904 case 'p':
7905 pflag = 1;
7906 break;
7907 case 'F':
7908 patch_script_path = optarg;
7909 break;
7910 default:
7911 usage_unstage();
7912 /* NOTREACHED */
7916 argc -= optind;
7917 argv += optind;
7919 #ifndef PROFILE
7920 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7921 "unveil", NULL) == -1)
7922 err(1, "pledge");
7923 #endif
7924 if (patch_script_path && !pflag)
7925 errx(1, "-F option can only be used together with -p option");
7927 cwd = getcwd(NULL, 0);
7928 if (cwd == NULL) {
7929 error = got_error_from_errno("getcwd");
7930 goto done;
7933 error = got_worktree_open(&worktree, cwd);
7934 if (error)
7935 goto done;
7937 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7938 NULL);
7939 if (error != NULL)
7940 goto done;
7942 if (patch_script_path) {
7943 patch_script_file = fopen(patch_script_path, "r");
7944 if (patch_script_file == NULL) {
7945 error = got_error_from_errno2("fopen",
7946 patch_script_path);
7947 goto done;
7951 error = apply_unveil(got_repo_get_path(repo), 0,
7952 got_worktree_get_root_path(worktree));
7953 if (error)
7954 goto done;
7956 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7957 if (error)
7958 goto done;
7960 cpa.patch_script_file = patch_script_file;
7961 cpa.action = "unstage";
7962 error = got_worktree_unstage(worktree, &paths, update_progress,
7963 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7964 done:
7965 if (patch_script_file && fclose(patch_script_file) == EOF &&
7966 error == NULL)
7967 error = got_error_from_errno2("fclose", patch_script_path);
7968 if (repo)
7969 got_repo_close(repo);
7970 if (worktree)
7971 got_worktree_close(worktree);
7972 TAILQ_FOREACH(pe, &paths, entry)
7973 free((char *)pe->path);
7974 got_pathlist_free(&paths);
7975 free(cwd);
7976 return error;
7979 __dead static void
7980 usage_cat(void)
7982 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7983 "arg1 [arg2 ...]\n", getprogname());
7984 exit(1);
7987 static const struct got_error *
7988 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7990 const struct got_error *err;
7991 struct got_blob_object *blob;
7993 err = got_object_open_as_blob(&blob, repo, id, 8192);
7994 if (err)
7995 return err;
7997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7998 got_object_blob_close(blob);
7999 return err;
8002 static const struct got_error *
8003 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8005 const struct got_error *err;
8006 struct got_tree_object *tree;
8007 int nentries, i;
8009 err = got_object_open_as_tree(&tree, repo, id);
8010 if (err)
8011 return err;
8013 nentries = got_object_tree_get_nentries(tree);
8014 for (i = 0; i < nentries; i++) {
8015 struct got_tree_entry *te;
8016 char *id_str;
8017 if (sigint_received || sigpipe_received)
8018 break;
8019 te = got_object_tree_get_entry(tree, i);
8020 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8021 if (err)
8022 break;
8023 fprintf(outfile, "%s %.7o %s\n", id_str,
8024 got_tree_entry_get_mode(te),
8025 got_tree_entry_get_name(te));
8026 free(id_str);
8029 got_object_tree_close(tree);
8030 return err;
8033 static const struct got_error *
8034 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8036 const struct got_error *err;
8037 struct got_commit_object *commit;
8038 const struct got_object_id_queue *parent_ids;
8039 struct got_object_qid *pid;
8040 char *id_str = NULL;
8041 const char *logmsg = NULL;
8043 err = got_object_open_as_commit(&commit, repo, id);
8044 if (err)
8045 return err;
8047 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8048 if (err)
8049 goto done;
8051 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8052 parent_ids = got_object_commit_get_parent_ids(commit);
8053 fprintf(outfile, "numparents %d\n",
8054 got_object_commit_get_nparents(commit));
8055 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8056 char *pid_str;
8057 err = got_object_id_str(&pid_str, pid->id);
8058 if (err)
8059 goto done;
8060 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8061 free(pid_str);
8063 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8064 got_object_commit_get_author(commit),
8065 got_object_commit_get_author_time(commit));
8067 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8068 got_object_commit_get_author(commit),
8069 got_object_commit_get_committer_time(commit));
8071 logmsg = got_object_commit_get_logmsg_raw(commit);
8072 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8073 fprintf(outfile, "%s", logmsg);
8074 done:
8075 free(id_str);
8076 got_object_commit_close(commit);
8077 return err;
8080 static const struct got_error *
8081 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8083 const struct got_error *err;
8084 struct got_tag_object *tag;
8085 char *id_str = NULL;
8086 const char *tagmsg = NULL;
8088 err = got_object_open_as_tag(&tag, repo, id);
8089 if (err)
8090 return err;
8092 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8093 if (err)
8094 goto done;
8096 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8098 switch (got_object_tag_get_object_type(tag)) {
8099 case GOT_OBJ_TYPE_BLOB:
8100 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8101 GOT_OBJ_LABEL_BLOB);
8102 break;
8103 case GOT_OBJ_TYPE_TREE:
8104 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8105 GOT_OBJ_LABEL_TREE);
8106 break;
8107 case GOT_OBJ_TYPE_COMMIT:
8108 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8109 GOT_OBJ_LABEL_COMMIT);
8110 break;
8111 case GOT_OBJ_TYPE_TAG:
8112 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8113 GOT_OBJ_LABEL_TAG);
8114 break;
8115 default:
8116 break;
8119 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8120 got_object_tag_get_name(tag));
8122 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8123 got_object_tag_get_tagger(tag),
8124 got_object_tag_get_tagger_time(tag));
8126 tagmsg = got_object_tag_get_message(tag);
8127 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8128 fprintf(outfile, "%s", tagmsg);
8129 done:
8130 free(id_str);
8131 got_object_tag_close(tag);
8132 return err;
8135 static const struct got_error *
8136 cmd_cat(int argc, char *argv[])
8138 const struct got_error *error;
8139 struct got_repository *repo = NULL;
8140 struct got_worktree *worktree = NULL;
8141 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8142 const char *commit_id_str = NULL;
8143 struct got_object_id *id = NULL, *commit_id = NULL;
8144 int ch, obj_type, i, force_path = 0;
8146 #ifndef PROFILE
8147 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8148 NULL) == -1)
8149 err(1, "pledge");
8150 #endif
8152 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8153 switch (ch) {
8154 case 'c':
8155 commit_id_str = optarg;
8156 break;
8157 case 'r':
8158 repo_path = realpath(optarg, NULL);
8159 if (repo_path == NULL)
8160 return got_error_from_errno2("realpath",
8161 optarg);
8162 got_path_strip_trailing_slashes(repo_path);
8163 break;
8164 case 'P':
8165 force_path = 1;
8166 break;
8167 default:
8168 usage_cat();
8169 /* NOTREACHED */
8173 argc -= optind;
8174 argv += optind;
8176 cwd = getcwd(NULL, 0);
8177 if (cwd == NULL) {
8178 error = got_error_from_errno("getcwd");
8179 goto done;
8181 error = got_worktree_open(&worktree, cwd);
8182 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8183 goto done;
8184 if (worktree) {
8185 if (repo_path == NULL) {
8186 repo_path = strdup(
8187 got_worktree_get_repo_path(worktree));
8188 if (repo_path == NULL) {
8189 error = got_error_from_errno("strdup");
8190 goto done;
8195 if (repo_path == NULL) {
8196 repo_path = getcwd(NULL, 0);
8197 if (repo_path == NULL)
8198 return got_error_from_errno("getcwd");
8201 error = got_repo_open(&repo, repo_path, NULL);
8202 free(repo_path);
8203 if (error != NULL)
8204 goto done;
8206 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8207 if (error)
8208 goto done;
8210 if (commit_id_str == NULL)
8211 commit_id_str = GOT_REF_HEAD;
8212 error = got_repo_match_object_id(&commit_id, NULL,
8213 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8214 if (error)
8215 goto done;
8217 for (i = 0; i < argc; i++) {
8218 if (force_path) {
8219 error = got_object_id_by_path(&id, repo, commit_id,
8220 argv[i]);
8221 if (error)
8222 break;
8223 } else {
8224 error = got_repo_match_object_id(&id, &label, argv[i],
8225 GOT_OBJ_TYPE_ANY, 0, repo);
8226 if (error) {
8227 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8228 error->code != GOT_ERR_NOT_REF)
8229 break;
8230 error = got_object_id_by_path(&id, repo,
8231 commit_id, argv[i]);
8232 if (error)
8233 break;
8237 error = got_object_get_type(&obj_type, repo, id);
8238 if (error)
8239 break;
8241 switch (obj_type) {
8242 case GOT_OBJ_TYPE_BLOB:
8243 error = cat_blob(id, repo, stdout);
8244 break;
8245 case GOT_OBJ_TYPE_TREE:
8246 error = cat_tree(id, repo, stdout);
8247 break;
8248 case GOT_OBJ_TYPE_COMMIT:
8249 error = cat_commit(id, repo, stdout);
8250 break;
8251 case GOT_OBJ_TYPE_TAG:
8252 error = cat_tag(id, repo, stdout);
8253 break;
8254 default:
8255 error = got_error(GOT_ERR_OBJ_TYPE);
8256 break;
8258 if (error)
8259 break;
8260 free(label);
8261 label = NULL;
8262 free(id);
8263 id = NULL;
8265 done:
8266 free(label);
8267 free(id);
8268 free(commit_id);
8269 if (worktree)
8270 got_worktree_close(worktree);
8271 if (repo) {
8272 const struct got_error *repo_error;
8273 repo_error = got_repo_close(repo);
8274 if (error == NULL)
8275 error = repo_error;
8277 return error;