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\n", host, port);
1018 /* Create a config file git-fetch(1) can understand. */
1019 gitconfig_path = got_repo_get_path_gitconfig(repo);
1020 if (gitconfig_path == NULL) {
1021 error = got_error_from_errno("got_repo_get_path_gitconfig");
1022 goto done;
1024 gitconfig_file = fopen(gitconfig_path, "a");
1025 if (gitconfig_file == NULL) {
1026 error = got_error_from_errno2("fopen", gitconfig_path);
1027 goto done;
1029 if (mirror_references) {
1030 if (asprintf(&gitconfig,
1031 "[remote \"%s\"]\n"
1032 "\turl = %s\n"
1033 "\tmirror = true\n",
1034 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1035 error = got_error_from_errno("asprintf");
1036 goto done;
1038 } else {
1039 if (asprintf(&gitconfig,
1040 "[remote \"%s\"]\n"
1041 "\turl = %s\n"
1042 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1043 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1044 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1045 error = got_error_from_errno("asprintf");
1046 goto done;
1049 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1050 if (n != strlen(gitconfig)) {
1051 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1052 goto done;
1055 fpa.last_scaled_size[0] = '\0';
1056 fpa.last_p_indexed = -1;
1057 fpa.last_p_resolved = -1;
1058 fpa.verbosity = verbosity;
1059 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1060 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1061 fetchfd, repo, fetch_progress, &fpa);
1062 if (error)
1063 goto done;
1065 error = got_object_id_str(&id_str, pack_hash);
1066 if (error)
1067 goto done;
1068 if (verbosity >= 0)
1069 printf("\nFetched %s.pack\n", id_str);
1070 free(id_str);
1072 /* Set up references provided with the pack file. */
1073 TAILQ_FOREACH(pe, &refs, entry) {
1074 const char *refname = pe->path;
1075 struct got_object_id *id = pe->data;
1076 struct got_reference *ref;
1077 char *remote_refname;
1079 error = got_ref_alloc(&ref, refname, id);
1080 if (error)
1081 goto done;
1082 error = got_ref_write(ref, repo);
1083 got_ref_close(ref);
1084 if (error)
1085 goto done;
1087 if (mirror_references)
1088 continue;
1090 if (strncmp("refs/heads/", refname, 11) != 0)
1091 continue;
1093 if (asprintf(&remote_refname,
1094 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1095 refname + 11) == -1) {
1096 error = got_error_from_errno("asprintf");
1097 goto done;
1099 error = got_ref_alloc(&ref, remote_refname, id);
1100 if (error)
1101 goto done;
1102 error = got_ref_write(ref, repo);
1103 got_ref_close(ref);
1104 if (error)
1105 goto done;
1108 /* Set the HEAD reference if the server provided one. */
1109 TAILQ_FOREACH(pe, &symrefs, entry) {
1110 struct got_reference *symref, *target_ref;
1111 const char *refname = pe->path;
1112 const char *target = pe->data;
1114 if (strcmp(refname, GOT_REF_HEAD) != 0)
1115 continue;
1117 error = got_ref_open(&target_ref, repo, target, 0);
1118 if (error) {
1119 if (error->code == GOT_ERR_NOT_REF)
1120 continue;
1121 goto done;
1124 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1125 got_ref_close(target_ref);
1126 if (error)
1127 goto done;
1129 if (verbosity >= 0)
1130 printf("Setting %s to %s\n", GOT_REF_HEAD,
1131 got_ref_get_symref_target(symref));
1133 error = got_ref_write(symref, repo);
1134 got_ref_close(symref);
1135 break;
1138 if (verbosity >= 0)
1139 printf("Created %s repository '%s'\n",
1140 mirror_references ? "mirrored" : "cloned", repo_path);
1141 done:
1142 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1143 error = got_error_from_errno("close");
1144 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1145 error = got_error_from_errno("fclose");
1146 if (repo)
1147 got_repo_close(repo);
1148 TAILQ_FOREACH(pe, &refs, entry) {
1149 free((void *)pe->path);
1150 free(pe->data);
1152 got_pathlist_free(&refs);
1153 TAILQ_FOREACH(pe, &symrefs, entry) {
1154 free((void *)pe->path);
1155 free(pe->data);
1157 got_pathlist_free(&symrefs);
1158 free(pack_hash);
1159 free(proto);
1160 free(host);
1161 free(port);
1162 free(server_path);
1163 free(repo_name);
1164 free(default_destdir);
1165 free(gitconfig_path);
1166 free(git_url);
1167 return error;
1170 static const struct got_error *
1171 create_ref(const char *refname, struct got_object_id *id,
1172 const char *id_str, struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 struct got_reference *ref;
1177 printf("Creating %s: %s\n", refname, id_str);
1179 err = got_ref_alloc(&ref, refname, id);
1180 if (err)
1181 return err;
1183 err = got_ref_write(ref, repo);
1184 got_ref_close(ref);
1185 return err;
1188 static const struct got_error *
1189 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1190 struct got_repository *repo)
1192 const struct got_error *err = NULL;
1193 char *new_id_str = NULL;
1194 struct got_object_id *old_id = NULL;
1196 err = got_object_id_str(&new_id_str, new_id);
1197 if (err)
1198 goto done;
1200 if (got_ref_is_symbolic(ref)) {
1201 struct got_reference *new_ref;
1202 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1203 if (err)
1204 goto done;
1205 printf("Deleting symbolic reference %s -> %s\n",
1206 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1207 err = got_ref_delete(ref, repo);
1208 if (err)
1209 goto done;
1210 printf("Setting %s to %s\n", got_ref_get_name(ref),
1211 new_id_str);
1212 err = got_ref_write(new_ref, repo);
1213 if (err)
1214 goto done;
1215 } else {
1216 err = got_ref_resolve(&old_id, repo, ref);
1217 if (err)
1218 goto done;
1219 if (got_object_id_cmp(old_id, new_id) != 0) {
1220 printf("Setting %s to %s\n",
1221 got_ref_get_name(ref), new_id_str);
1222 err = got_ref_change_ref(ref, new_id);
1223 if (err)
1224 goto done;
1225 err = got_ref_write(ref, repo);
1226 if (err)
1227 goto done;
1230 done:
1231 free(old_id);
1232 free(new_id_str);
1233 return err;
1236 __dead static void
1237 usage_fetch(void)
1239 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1240 "[remote-repository-name]\n", getprogname());
1241 exit(1);
1244 static const struct got_error *
1245 cmd_fetch(int argc, char *argv[])
1247 const struct got_error *error = NULL;
1248 char *cwd = NULL, *repo_path = NULL;
1249 const char *remote_name;
1250 char *proto = NULL, *host = NULL, *port = NULL;
1251 char *repo_name = NULL, *server_path = NULL;
1252 struct got_remote_repo *remotes, *remote = NULL;
1253 int nremotes;
1254 char *id_str = NULL;
1255 struct got_repository *repo = NULL;
1256 struct got_worktree *worktree = NULL;
1257 struct got_pathlist_head refs, symrefs;
1258 struct got_pathlist_entry *pe;
1259 struct got_object_id *pack_hash = NULL;
1260 int i, ch, fetchfd = -1;
1261 struct got_fetch_progress_arg fpa;
1262 int verbosity = 0;
1264 TAILQ_INIT(&refs);
1265 TAILQ_INIT(&symrefs);
1267 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1268 switch (ch) {
1269 case 'r':
1270 repo_path = realpath(optarg, NULL);
1271 if (repo_path == NULL)
1272 return got_error_from_errno2("realpath",
1273 optarg);
1274 got_path_strip_trailing_slashes(repo_path);
1275 break;
1276 case 'v':
1277 if (verbosity < 0)
1278 verbosity = 0;
1279 else if (verbosity < 3)
1280 verbosity++;
1281 break;
1282 case 'q':
1283 verbosity = -1;
1284 break;
1285 default:
1286 usage_fetch();
1287 break;
1290 argc -= optind;
1291 argv += optind;
1293 if (argc == 0)
1294 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1295 else if (argc == 1)
1296 remote_name = argv[0];
1297 else
1298 usage_fetch();
1300 cwd = getcwd(NULL, 0);
1301 if (cwd == NULL) {
1302 error = got_error_from_errno("getcwd");
1303 goto done;
1306 if (repo_path == NULL) {
1307 error = got_worktree_open(&worktree, cwd);
1308 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1309 goto done;
1310 else
1311 error = NULL;
1312 if (worktree) {
1313 repo_path =
1314 strdup(got_worktree_get_repo_path(worktree));
1315 if (repo_path == NULL)
1316 error = got_error_from_errno("strdup");
1317 if (error)
1318 goto done;
1319 } else {
1320 repo_path = strdup(cwd);
1321 if (repo_path == NULL) {
1322 error = got_error_from_errno("strdup");
1323 goto done;
1328 error = got_repo_open(&repo, repo_path, NULL);
1329 if (error)
1330 goto done;
1332 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1333 for (i = 0; i < nremotes; i++) {
1334 remote = &remotes[i];
1335 if (strcmp(remote->name, remote_name) == 0)
1336 break;
1338 if (i == nremotes) {
1339 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1340 goto done;
1343 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1344 &repo_name, remote->url);
1345 if (error)
1346 goto done;
1348 if (strcmp(proto, "git") == 0) {
1349 #ifndef PROFILE
1350 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1351 "sendfd dns inet unveil", NULL) == -1)
1352 err(1, "pledge");
1353 #endif
1354 } else if (strcmp(proto, "git+ssh") == 0 ||
1355 strcmp(proto, "ssh") == 0) {
1356 #ifndef PROFILE
1357 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1358 "sendfd unveil", NULL) == -1)
1359 err(1, "pledge");
1360 #endif
1361 } else if (strcmp(proto, "http") == 0 ||
1362 strcmp(proto, "git+http") == 0) {
1363 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1364 goto done;
1365 } else {
1366 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1367 goto done;
1370 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1371 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1372 error = got_error_from_errno2("unveil",
1373 GOT_FETCH_PATH_SSH);
1374 goto done;
1377 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1378 if (error)
1379 goto done;
1381 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1382 verbosity);
1383 if (error)
1384 goto done;
1386 if (verbosity >= 0)
1387 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1389 fpa.last_scaled_size[0] = '\0';
1390 fpa.last_p_indexed = -1;
1391 fpa.last_p_resolved = -1;
1392 fpa.verbosity = verbosity;
1393 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1394 remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
1395 if (error)
1396 goto done;
1398 if (pack_hash == NULL) {
1399 if (verbosity >= 0)
1400 printf("Already up-to-date\n");
1401 goto done;
1404 if (verbosity >= 0) {
1405 error = got_object_id_str(&id_str, pack_hash);
1406 if (error)
1407 goto done;
1408 printf("\nFetched %s.pack\n", id_str);
1409 free(id_str);
1410 id_str = NULL;
1413 /* Update references provided with the pack file. */
1414 TAILQ_FOREACH(pe, &refs, entry) {
1415 const char *refname = pe->path;
1416 struct got_object_id *id = pe->data;
1417 struct got_reference *ref;
1418 char *remote_refname;
1420 error = got_object_id_str(&id_str, id);
1421 if (error)
1422 goto done;
1424 if (strncmp("refs/tags/", refname, 10) == 0) {
1425 error = got_ref_open(&ref, repo, refname, 0);
1426 if (error) {
1427 if (error->code != GOT_ERR_NOT_REF)
1428 goto done;
1429 error = create_ref(refname, id, id_str, repo);
1430 if (error)
1431 goto done;
1432 } else {
1433 error = update_ref(ref, id, repo);
1434 got_ref_close(ref);
1435 if (error)
1436 goto done;
1438 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1439 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1440 remote_name, refname + 11) == -1) {
1441 error = got_error_from_errno("asprintf");
1442 goto done;
1445 error = got_ref_open(&ref, repo, remote_refname, 0);
1446 if (error) {
1447 if (error->code != GOT_ERR_NOT_REF)
1448 goto done;
1449 error = create_ref(remote_refname, id, id_str,
1450 repo);
1451 if (error)
1452 goto done;
1453 } else {
1454 error = update_ref(ref, id, repo);
1455 got_ref_close(ref);
1456 if (error)
1457 goto done;
1460 free(id_str);
1461 id_str = NULL;
1463 done:
1464 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1465 error = got_error_from_errno("close");
1466 if (repo)
1467 got_repo_close(repo);
1468 if (worktree)
1469 got_worktree_close(worktree);
1470 TAILQ_FOREACH(pe, &refs, entry) {
1471 free((void *)pe->path);
1472 free(pe->data);
1474 got_pathlist_free(&refs);
1475 TAILQ_FOREACH(pe, &symrefs, entry) {
1476 free((void *)pe->path);
1477 free(pe->data);
1479 got_pathlist_free(&symrefs);
1480 free(id_str);
1481 free(cwd);
1482 free(repo_path);
1483 free(pack_hash);
1484 free(proto);
1485 free(host);
1486 free(port);
1487 free(server_path);
1488 free(repo_name);
1489 return error;
1493 __dead static void
1494 usage_checkout(void)
1496 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1497 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1498 exit(1);
1501 static void
1502 show_worktree_base_ref_warning(void)
1504 fprintf(stderr, "%s: warning: could not create a reference "
1505 "to the work tree's base commit; the commit could be "
1506 "garbage-collected by Git; making the repository "
1507 "writable and running 'got update' will prevent this\n",
1508 getprogname());
1511 struct got_checkout_progress_arg {
1512 const char *worktree_path;
1513 int had_base_commit_ref_error;
1516 static const struct got_error *
1517 checkout_progress(void *arg, unsigned char status, const char *path)
1519 struct got_checkout_progress_arg *a = arg;
1521 /* Base commit bump happens silently. */
1522 if (status == GOT_STATUS_BUMP_BASE)
1523 return NULL;
1525 if (status == GOT_STATUS_BASE_REF_ERR) {
1526 a->had_base_commit_ref_error = 1;
1527 return NULL;
1530 while (path[0] == '/')
1531 path++;
1533 printf("%c %s/%s\n", status, a->worktree_path, path);
1534 return NULL;
1537 static const struct got_error *
1538 check_cancelled(void *arg)
1540 if (sigint_received || sigpipe_received)
1541 return got_error(GOT_ERR_CANCELLED);
1542 return NULL;
1545 static const struct got_error *
1546 check_linear_ancestry(struct got_object_id *commit_id,
1547 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1548 struct got_repository *repo)
1550 const struct got_error *err = NULL;
1551 struct got_object_id *yca_id;
1553 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1554 commit_id, base_commit_id, repo, check_cancelled, NULL);
1555 if (err)
1556 return err;
1558 if (yca_id == NULL)
1559 return got_error(GOT_ERR_ANCESTRY);
1562 * Require a straight line of history between the target commit
1563 * and the work tree's base commit.
1565 * Non-linear situations such as this require a rebase:
1567 * (commit) D F (base_commit)
1568 * \ /
1569 * C E
1570 * \ /
1571 * B (yca)
1572 * |
1573 * A
1575 * 'got update' only handles linear cases:
1576 * Update forwards in time: A (base/yca) - B - C - D (commit)
1577 * Update backwards in time: D (base) - C - B - A (commit/yca)
1579 if (allow_forwards_in_time_only) {
1580 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1581 return got_error(GOT_ERR_ANCESTRY);
1582 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1583 got_object_id_cmp(base_commit_id, yca_id) != 0)
1584 return got_error(GOT_ERR_ANCESTRY);
1586 free(yca_id);
1587 return NULL;
1590 static const struct got_error *
1591 check_same_branch(struct got_object_id *commit_id,
1592 struct got_reference *head_ref, struct got_object_id *yca_id,
1593 struct got_repository *repo)
1595 const struct got_error *err = NULL;
1596 struct got_commit_graph *graph = NULL;
1597 struct got_object_id *head_commit_id = NULL;
1598 int is_same_branch = 0;
1600 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1601 if (err)
1602 goto done;
1604 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1605 is_same_branch = 1;
1606 goto done;
1608 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1609 is_same_branch = 1;
1610 goto done;
1613 err = got_commit_graph_open(&graph, "/", 1);
1614 if (err)
1615 goto done;
1617 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1618 check_cancelled, NULL);
1619 if (err)
1620 goto done;
1622 for (;;) {
1623 struct got_object_id *id;
1624 err = got_commit_graph_iter_next(&id, graph, repo,
1625 check_cancelled, NULL);
1626 if (err) {
1627 if (err->code == GOT_ERR_ITER_COMPLETED)
1628 err = NULL;
1629 break;
1632 if (id) {
1633 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1634 break;
1635 if (got_object_id_cmp(id, commit_id) == 0) {
1636 is_same_branch = 1;
1637 break;
1641 done:
1642 if (graph)
1643 got_commit_graph_close(graph);
1644 free(head_commit_id);
1645 if (!err && !is_same_branch)
1646 err = got_error(GOT_ERR_ANCESTRY);
1647 return err;
1650 static const struct got_error *
1651 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1653 static char msg[512];
1654 const char *branch_name;
1656 if (got_ref_is_symbolic(ref))
1657 branch_name = got_ref_get_symref_target(ref);
1658 else
1659 branch_name = got_ref_get_name(ref);
1661 if (strncmp("refs/heads/", branch_name, 11) == 0)
1662 branch_name += 11;
1664 snprintf(msg, sizeof(msg),
1665 "target commit is not contained in branch '%s'; "
1666 "the branch to use must be specified with -b; "
1667 "if necessary a new branch can be created for "
1668 "this commit with 'got branch -c %s BRANCH_NAME'",
1669 branch_name, commit_id_str);
1671 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1674 static const struct got_error *
1675 cmd_checkout(int argc, char *argv[])
1677 const struct got_error *error = NULL;
1678 struct got_repository *repo = NULL;
1679 struct got_reference *head_ref = NULL;
1680 struct got_worktree *worktree = NULL;
1681 char *repo_path = NULL;
1682 char *worktree_path = NULL;
1683 const char *path_prefix = "";
1684 const char *branch_name = GOT_REF_HEAD;
1685 char *commit_id_str = NULL;
1686 int ch, same_path_prefix, allow_nonempty = 0;
1687 struct got_pathlist_head paths;
1688 struct got_checkout_progress_arg cpa;
1690 TAILQ_INIT(&paths);
1692 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1693 switch (ch) {
1694 case 'b':
1695 branch_name = optarg;
1696 break;
1697 case 'c':
1698 commit_id_str = strdup(optarg);
1699 if (commit_id_str == NULL)
1700 return got_error_from_errno("strdup");
1701 break;
1702 case 'E':
1703 allow_nonempty = 1;
1704 break;
1705 case 'p':
1706 path_prefix = optarg;
1707 break;
1708 default:
1709 usage_checkout();
1710 /* NOTREACHED */
1714 argc -= optind;
1715 argv += optind;
1717 #ifndef PROFILE
1718 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1719 "unveil", NULL) == -1)
1720 err(1, "pledge");
1721 #endif
1722 if (argc == 1) {
1723 char *cwd, *base, *dotgit;
1724 repo_path = realpath(argv[0], NULL);
1725 if (repo_path == NULL)
1726 return got_error_from_errno2("realpath", argv[0]);
1727 cwd = getcwd(NULL, 0);
1728 if (cwd == NULL) {
1729 error = got_error_from_errno("getcwd");
1730 goto done;
1732 if (path_prefix[0]) {
1733 base = basename(path_prefix);
1734 if (base == NULL) {
1735 error = got_error_from_errno2("basename",
1736 path_prefix);
1737 goto done;
1739 } else {
1740 base = basename(repo_path);
1741 if (base == NULL) {
1742 error = got_error_from_errno2("basename",
1743 repo_path);
1744 goto done;
1747 dotgit = strstr(base, ".git");
1748 if (dotgit)
1749 *dotgit = '\0';
1750 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1751 error = got_error_from_errno("asprintf");
1752 free(cwd);
1753 goto done;
1755 free(cwd);
1756 } else if (argc == 2) {
1757 repo_path = realpath(argv[0], NULL);
1758 if (repo_path == NULL) {
1759 error = got_error_from_errno2("realpath", argv[0]);
1760 goto done;
1762 worktree_path = realpath(argv[1], NULL);
1763 if (worktree_path == NULL) {
1764 if (errno != ENOENT) {
1765 error = got_error_from_errno2("realpath",
1766 argv[1]);
1767 goto done;
1769 worktree_path = strdup(argv[1]);
1770 if (worktree_path == NULL) {
1771 error = got_error_from_errno("strdup");
1772 goto done;
1775 } else
1776 usage_checkout();
1778 got_path_strip_trailing_slashes(repo_path);
1779 got_path_strip_trailing_slashes(worktree_path);
1781 error = got_repo_open(&repo, repo_path, NULL);
1782 if (error != NULL)
1783 goto done;
1785 /* Pre-create work tree path for unveil(2) */
1786 error = got_path_mkdir(worktree_path);
1787 if (error) {
1788 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1789 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1790 goto done;
1791 if (!allow_nonempty &&
1792 !got_path_dir_is_empty(worktree_path)) {
1793 error = got_error_path(worktree_path,
1794 GOT_ERR_DIR_NOT_EMPTY);
1795 goto done;
1799 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1800 if (error)
1801 goto done;
1803 error = got_ref_open(&head_ref, repo, branch_name, 0);
1804 if (error != NULL)
1805 goto done;
1807 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1808 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1809 goto done;
1811 error = got_worktree_open(&worktree, worktree_path);
1812 if (error != NULL)
1813 goto done;
1815 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1816 path_prefix);
1817 if (error != NULL)
1818 goto done;
1819 if (!same_path_prefix) {
1820 error = got_error(GOT_ERR_PATH_PREFIX);
1821 goto done;
1824 if (commit_id_str) {
1825 struct got_object_id *commit_id;
1826 error = got_repo_match_object_id(&commit_id, NULL,
1827 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1828 if (error)
1829 goto done;
1830 error = check_linear_ancestry(commit_id,
1831 got_worktree_get_base_commit_id(worktree), 0, repo);
1832 if (error != NULL) {
1833 free(commit_id);
1834 if (error->code == GOT_ERR_ANCESTRY) {
1835 error = checkout_ancestry_error(
1836 head_ref, commit_id_str);
1838 goto done;
1840 error = check_same_branch(commit_id, head_ref, NULL, repo);
1841 if (error) {
1842 if (error->code == GOT_ERR_ANCESTRY) {
1843 error = checkout_ancestry_error(
1844 head_ref, commit_id_str);
1846 goto done;
1848 error = got_worktree_set_base_commit_id(worktree, repo,
1849 commit_id);
1850 free(commit_id);
1851 if (error)
1852 goto done;
1855 error = got_pathlist_append(&paths, "", NULL);
1856 if (error)
1857 goto done;
1858 cpa.worktree_path = worktree_path;
1859 cpa.had_base_commit_ref_error = 0;
1860 error = got_worktree_checkout_files(worktree, &paths, repo,
1861 checkout_progress, &cpa, check_cancelled, NULL);
1862 if (error != NULL)
1863 goto done;
1865 printf("Now shut up and hack\n");
1866 if (cpa.had_base_commit_ref_error)
1867 show_worktree_base_ref_warning();
1868 done:
1869 got_pathlist_free(&paths);
1870 free(commit_id_str);
1871 free(repo_path);
1872 free(worktree_path);
1873 return error;
1876 __dead static void
1877 usage_update(void)
1879 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1880 getprogname());
1881 exit(1);
1884 static const struct got_error *
1885 update_progress(void *arg, unsigned char status, const char *path)
1887 int *did_something = arg;
1889 if (status == GOT_STATUS_EXISTS ||
1890 status == GOT_STATUS_BASE_REF_ERR)
1891 return NULL;
1893 *did_something = 1;
1895 /* Base commit bump happens silently. */
1896 if (status == GOT_STATUS_BUMP_BASE)
1897 return NULL;
1899 while (path[0] == '/')
1900 path++;
1901 printf("%c %s\n", status, path);
1902 return NULL;
1905 static const struct got_error *
1906 switch_head_ref(struct got_reference *head_ref,
1907 struct got_object_id *commit_id, struct got_worktree *worktree,
1908 struct got_repository *repo)
1910 const struct got_error *err = NULL;
1911 char *base_id_str;
1912 int ref_has_moved = 0;
1914 /* Trivial case: switching between two different references. */
1915 if (strcmp(got_ref_get_name(head_ref),
1916 got_worktree_get_head_ref_name(worktree)) != 0) {
1917 printf("Switching work tree from %s to %s\n",
1918 got_worktree_get_head_ref_name(worktree),
1919 got_ref_get_name(head_ref));
1920 return got_worktree_set_head_ref(worktree, head_ref);
1923 err = check_linear_ancestry(commit_id,
1924 got_worktree_get_base_commit_id(worktree), 0, repo);
1925 if (err) {
1926 if (err->code != GOT_ERR_ANCESTRY)
1927 return err;
1928 ref_has_moved = 1;
1930 if (!ref_has_moved)
1931 return NULL;
1933 /* Switching to a rebased branch with the same reference name. */
1934 err = got_object_id_str(&base_id_str,
1935 got_worktree_get_base_commit_id(worktree));
1936 if (err)
1937 return err;
1938 printf("Reference %s now points at a different branch\n",
1939 got_worktree_get_head_ref_name(worktree));
1940 printf("Switching work tree from %s to %s\n", base_id_str,
1941 got_worktree_get_head_ref_name(worktree));
1942 return NULL;
1945 static const struct got_error *
1946 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1948 const struct got_error *err;
1949 int in_progress;
1951 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1952 if (err)
1953 return err;
1954 if (in_progress)
1955 return got_error(GOT_ERR_REBASING);
1957 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1958 if (err)
1959 return err;
1960 if (in_progress)
1961 return got_error(GOT_ERR_HISTEDIT_BUSY);
1963 return NULL;
1966 static const struct got_error *
1967 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1968 char *argv[], struct got_worktree *worktree)
1970 const struct got_error *err = NULL;
1971 char *path;
1972 int i;
1974 if (argc == 0) {
1975 path = strdup("");
1976 if (path == NULL)
1977 return got_error_from_errno("strdup");
1978 return got_pathlist_append(paths, path, NULL);
1981 for (i = 0; i < argc; i++) {
1982 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1983 if (err)
1984 break;
1985 err = got_pathlist_append(paths, path, NULL);
1986 if (err) {
1987 free(path);
1988 break;
1992 return err;
1995 static const struct got_error *
1996 cmd_update(int argc, char *argv[])
1998 const struct got_error *error = NULL;
1999 struct got_repository *repo = NULL;
2000 struct got_worktree *worktree = NULL;
2001 char *worktree_path = NULL;
2002 struct got_object_id *commit_id = NULL;
2003 char *commit_id_str = NULL;
2004 const char *branch_name = NULL;
2005 struct got_reference *head_ref = NULL;
2006 struct got_pathlist_head paths;
2007 struct got_pathlist_entry *pe;
2008 int ch, did_something = 0;
2010 TAILQ_INIT(&paths);
2012 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2013 switch (ch) {
2014 case 'b':
2015 branch_name = optarg;
2016 break;
2017 case 'c':
2018 commit_id_str = strdup(optarg);
2019 if (commit_id_str == NULL)
2020 return got_error_from_errno("strdup");
2021 break;
2022 default:
2023 usage_update();
2024 /* NOTREACHED */
2028 argc -= optind;
2029 argv += optind;
2031 #ifndef PROFILE
2032 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2033 "unveil", NULL) == -1)
2034 err(1, "pledge");
2035 #endif
2036 worktree_path = getcwd(NULL, 0);
2037 if (worktree_path == NULL) {
2038 error = got_error_from_errno("getcwd");
2039 goto done;
2041 error = got_worktree_open(&worktree, worktree_path);
2042 if (error)
2043 goto done;
2045 error = check_rebase_or_histedit_in_progress(worktree);
2046 if (error)
2047 goto done;
2049 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2050 NULL);
2051 if (error != NULL)
2052 goto done;
2054 error = apply_unveil(got_repo_get_path(repo), 0,
2055 got_worktree_get_root_path(worktree));
2056 if (error)
2057 goto done;
2059 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2060 if (error)
2061 goto done;
2063 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2064 got_worktree_get_head_ref_name(worktree), 0);
2065 if (error != NULL)
2066 goto done;
2067 if (commit_id_str == NULL) {
2068 error = got_ref_resolve(&commit_id, repo, head_ref);
2069 if (error != NULL)
2070 goto done;
2071 error = got_object_id_str(&commit_id_str, commit_id);
2072 if (error != NULL)
2073 goto done;
2074 } else {
2075 error = got_repo_match_object_id(&commit_id, NULL,
2076 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2077 free(commit_id_str);
2078 commit_id_str = NULL;
2079 if (error)
2080 goto done;
2081 error = got_object_id_str(&commit_id_str, commit_id);
2082 if (error)
2083 goto done;
2086 if (branch_name) {
2087 struct got_object_id *head_commit_id;
2088 TAILQ_FOREACH(pe, &paths, entry) {
2089 if (pe->path_len == 0)
2090 continue;
2091 error = got_error_msg(GOT_ERR_BAD_PATH,
2092 "switching between branches requires that "
2093 "the entire work tree gets updated");
2094 goto done;
2096 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2097 if (error)
2098 goto done;
2099 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2100 repo);
2101 free(head_commit_id);
2102 if (error != NULL)
2103 goto done;
2104 error = check_same_branch(commit_id, head_ref, NULL, repo);
2105 if (error)
2106 goto done;
2107 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2108 if (error)
2109 goto done;
2110 } else {
2111 error = check_linear_ancestry(commit_id,
2112 got_worktree_get_base_commit_id(worktree), 0, repo);
2113 if (error != NULL) {
2114 if (error->code == GOT_ERR_ANCESTRY)
2115 error = got_error(GOT_ERR_BRANCH_MOVED);
2116 goto done;
2118 error = check_same_branch(commit_id, head_ref, NULL, repo);
2119 if (error)
2120 goto done;
2123 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2124 commit_id) != 0) {
2125 error = got_worktree_set_base_commit_id(worktree, repo,
2126 commit_id);
2127 if (error)
2128 goto done;
2131 error = got_worktree_checkout_files(worktree, &paths, repo,
2132 update_progress, &did_something, check_cancelled, NULL);
2133 if (error != NULL)
2134 goto done;
2136 if (did_something)
2137 printf("Updated to commit %s\n", commit_id_str);
2138 else
2139 printf("Already up-to-date\n");
2140 done:
2141 free(worktree_path);
2142 TAILQ_FOREACH(pe, &paths, entry)
2143 free((char *)pe->path);
2144 got_pathlist_free(&paths);
2145 free(commit_id);
2146 free(commit_id_str);
2147 return error;
2150 static const struct got_error *
2151 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2152 const char *path, int diff_context, int ignore_whitespace,
2153 struct got_repository *repo)
2155 const struct got_error *err = NULL;
2156 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2158 if (blob_id1) {
2159 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2160 if (err)
2161 goto done;
2164 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2165 if (err)
2166 goto done;
2168 while (path[0] == '/')
2169 path++;
2170 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2171 ignore_whitespace, stdout);
2172 done:
2173 if (blob1)
2174 got_object_blob_close(blob1);
2175 got_object_blob_close(blob2);
2176 return err;
2179 static const struct got_error *
2180 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2181 const char *path, int diff_context, int ignore_whitespace,
2182 struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2186 struct got_diff_blob_output_unidiff_arg arg;
2188 if (tree_id1) {
2189 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2190 if (err)
2191 goto done;
2194 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2195 if (err)
2196 goto done;
2198 arg.diff_context = diff_context;
2199 arg.ignore_whitespace = ignore_whitespace;
2200 arg.outfile = stdout;
2201 while (path[0] == '/')
2202 path++;
2203 err = got_diff_tree(tree1, tree2, path, path, repo,
2204 got_diff_blob_output_unidiff, &arg, 1);
2205 done:
2206 if (tree1)
2207 got_object_tree_close(tree1);
2208 if (tree2)
2209 got_object_tree_close(tree2);
2210 return err;
2213 static const struct got_error *
2214 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2215 const char *path, int diff_context, struct got_repository *repo)
2217 const struct got_error *err = NULL;
2218 struct got_commit_object *pcommit = NULL;
2219 char *id_str1 = NULL, *id_str2 = NULL;
2220 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2221 struct got_object_qid *qid;
2223 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2224 if (qid != NULL) {
2225 err = got_object_open_as_commit(&pcommit, repo,
2226 qid->id);
2227 if (err)
2228 return err;
2231 if (path && path[0] != '\0') {
2232 int obj_type;
2233 err = got_object_id_by_path(&obj_id2, repo, id, path);
2234 if (err)
2235 goto done;
2236 err = got_object_id_str(&id_str2, obj_id2);
2237 if (err) {
2238 free(obj_id2);
2239 goto done;
2241 if (pcommit) {
2242 err = got_object_id_by_path(&obj_id1, repo,
2243 qid->id, path);
2244 if (err) {
2245 free(obj_id2);
2246 goto done;
2248 err = got_object_id_str(&id_str1, obj_id1);
2249 if (err) {
2250 free(obj_id2);
2251 goto done;
2254 err = got_object_get_type(&obj_type, repo, obj_id2);
2255 if (err) {
2256 free(obj_id2);
2257 goto done;
2259 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2260 switch (obj_type) {
2261 case GOT_OBJ_TYPE_BLOB:
2262 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2263 0, repo);
2264 break;
2265 case GOT_OBJ_TYPE_TREE:
2266 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2267 0, repo);
2268 break;
2269 default:
2270 err = got_error(GOT_ERR_OBJ_TYPE);
2271 break;
2273 free(obj_id1);
2274 free(obj_id2);
2275 } else {
2276 obj_id2 = got_object_commit_get_tree_id(commit);
2277 err = got_object_id_str(&id_str2, obj_id2);
2278 if (err)
2279 goto done;
2280 obj_id1 = got_object_commit_get_tree_id(pcommit);
2281 err = got_object_id_str(&id_str1, obj_id1);
2282 if (err)
2283 goto done;
2284 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2285 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2287 done:
2288 free(id_str1);
2289 free(id_str2);
2290 if (pcommit)
2291 got_object_commit_close(pcommit);
2292 return err;
2295 static char *
2296 get_datestr(time_t *time, char *datebuf)
2298 struct tm mytm, *tm;
2299 char *p, *s;
2301 tm = gmtime_r(time, &mytm);
2302 if (tm == NULL)
2303 return NULL;
2304 s = asctime_r(tm, datebuf);
2305 if (s == NULL)
2306 return NULL;
2307 p = strchr(s, '\n');
2308 if (p)
2309 *p = '\0';
2310 return s;
2313 static const struct got_error *
2314 match_logmsg(int *have_match, struct got_object_id *id,
2315 struct got_commit_object *commit, regex_t *regex)
2317 const struct got_error *err = NULL;
2318 regmatch_t regmatch;
2319 char *id_str = NULL, *logmsg = NULL;
2321 *have_match = 0;
2323 err = got_object_id_str(&id_str, id);
2324 if (err)
2325 return err;
2327 err = got_object_commit_get_logmsg(&logmsg, commit);
2328 if (err)
2329 goto done;
2331 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2332 *have_match = 1;
2333 done:
2334 free(id_str);
2335 free(logmsg);
2336 return err;
2339 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2341 static const struct got_error *
2342 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2343 struct got_repository *repo, const char *path, int show_patch,
2344 int diff_context, struct got_reflist_head *refs)
2346 const struct got_error *err = NULL;
2347 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2348 char datebuf[26];
2349 time_t committer_time;
2350 const char *author, *committer;
2351 char *refs_str = NULL;
2352 struct got_reflist_entry *re;
2354 SIMPLEQ_FOREACH(re, refs, entry) {
2355 char *s;
2356 const char *name;
2357 struct got_tag_object *tag = NULL;
2358 int cmp;
2360 name = got_ref_get_name(re->ref);
2361 if (strcmp(name, GOT_REF_HEAD) == 0)
2362 continue;
2363 if (strncmp(name, "refs/", 5) == 0)
2364 name += 5;
2365 if (strncmp(name, "got/", 4) == 0)
2366 continue;
2367 if (strncmp(name, "heads/", 6) == 0)
2368 name += 6;
2369 if (strncmp(name, "remotes/", 8) == 0)
2370 name += 8;
2371 if (strncmp(name, "tags/", 5) == 0) {
2372 err = got_object_open_as_tag(&tag, repo, re->id);
2373 if (err) {
2374 if (err->code != GOT_ERR_OBJ_TYPE)
2375 return err;
2376 /* Ref points at something other than a tag. */
2377 err = NULL;
2378 tag = NULL;
2381 cmp = got_object_id_cmp(tag ?
2382 got_object_tag_get_object_id(tag) : re->id, id);
2383 if (tag)
2384 got_object_tag_close(tag);
2385 if (cmp != 0)
2386 continue;
2387 s = refs_str;
2388 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2389 name) == -1) {
2390 err = got_error_from_errno("asprintf");
2391 free(s);
2392 return err;
2394 free(s);
2396 err = got_object_id_str(&id_str, id);
2397 if (err)
2398 return err;
2400 printf(GOT_COMMIT_SEP_STR);
2401 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2402 refs_str ? refs_str : "", refs_str ? ")" : "");
2403 free(id_str);
2404 id_str = NULL;
2405 free(refs_str);
2406 refs_str = NULL;
2407 printf("from: %s\n", got_object_commit_get_author(commit));
2408 committer_time = got_object_commit_get_committer_time(commit);
2409 datestr = get_datestr(&committer_time, datebuf);
2410 if (datestr)
2411 printf("date: %s UTC\n", datestr);
2412 author = got_object_commit_get_author(commit);
2413 committer = got_object_commit_get_committer(commit);
2414 if (strcmp(author, committer) != 0)
2415 printf("via: %s\n", committer);
2416 if (got_object_commit_get_nparents(commit) > 1) {
2417 const struct got_object_id_queue *parent_ids;
2418 struct got_object_qid *qid;
2419 int n = 1;
2420 parent_ids = got_object_commit_get_parent_ids(commit);
2421 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2422 err = got_object_id_str(&id_str, qid->id);
2423 if (err)
2424 return err;
2425 printf("parent %d: %s\n", n++, id_str);
2426 free(id_str);
2430 err = got_object_commit_get_logmsg(&logmsg0, commit);
2431 if (err)
2432 return err;
2434 logmsg = logmsg0;
2435 do {
2436 line = strsep(&logmsg, "\n");
2437 if (line)
2438 printf(" %s\n", line);
2439 } while (line);
2440 free(logmsg0);
2442 if (show_patch) {
2443 err = print_patch(commit, id, path, diff_context, repo);
2444 if (err == 0)
2445 printf("\n");
2448 if (fflush(stdout) != 0 && err == NULL)
2449 err = got_error_from_errno("fflush");
2450 return err;
2453 static const struct got_error *
2454 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2455 const char *path, int show_patch, const char *search_pattern,
2456 int diff_context, int limit, int log_branches,
2457 struct got_reflist_head *refs)
2459 const struct got_error *err;
2460 struct got_commit_graph *graph;
2461 regex_t regex;
2462 int have_match;
2464 if (search_pattern &&
2465 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2466 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2468 err = got_commit_graph_open(&graph, path, !log_branches);
2469 if (err)
2470 return err;
2471 err = got_commit_graph_iter_start(graph, root_id, repo,
2472 check_cancelled, NULL);
2473 if (err)
2474 goto done;
2475 for (;;) {
2476 struct got_commit_object *commit;
2477 struct got_object_id *id;
2479 if (sigint_received || sigpipe_received)
2480 break;
2482 err = got_commit_graph_iter_next(&id, graph, repo,
2483 check_cancelled, NULL);
2484 if (err) {
2485 if (err->code == GOT_ERR_ITER_COMPLETED)
2486 err = NULL;
2487 break;
2489 if (id == NULL)
2490 break;
2492 err = got_object_open_as_commit(&commit, repo, id);
2493 if (err)
2494 break;
2496 if (search_pattern) {
2497 err = match_logmsg(&have_match, id, commit, &regex);
2498 if (err) {
2499 got_object_commit_close(commit);
2500 break;
2502 if (have_match == 0) {
2503 got_object_commit_close(commit);
2504 continue;
2508 err = print_commit(commit, id, repo, path, show_patch,
2509 diff_context, refs);
2510 got_object_commit_close(commit);
2511 if (err || (limit && --limit == 0))
2512 break;
2514 done:
2515 if (search_pattern)
2516 regfree(&regex);
2517 got_commit_graph_close(graph);
2518 return err;
2521 __dead static void
2522 usage_log(void)
2524 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2525 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2526 exit(1);
2529 static int
2530 get_default_log_limit(void)
2532 const char *got_default_log_limit;
2533 long long n;
2534 const char *errstr;
2536 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2537 if (got_default_log_limit == NULL)
2538 return 0;
2539 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2540 if (errstr != NULL)
2541 return 0;
2542 return n;
2545 static const struct got_error *
2546 cmd_log(int argc, char *argv[])
2548 const struct got_error *error;
2549 struct got_repository *repo = NULL;
2550 struct got_worktree *worktree = NULL;
2551 struct got_commit_object *commit = NULL;
2552 struct got_object_id *id = NULL;
2553 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2554 const char *start_commit = NULL, *search_pattern = NULL;
2555 int diff_context = -1, ch;
2556 int show_patch = 0, limit = 0, log_branches = 0;
2557 const char *errstr;
2558 struct got_reflist_head refs;
2560 SIMPLEQ_INIT(&refs);
2562 #ifndef PROFILE
2563 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2564 NULL)
2565 == -1)
2566 err(1, "pledge");
2567 #endif
2569 limit = get_default_log_limit();
2571 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2572 switch (ch) {
2573 case 'p':
2574 show_patch = 1;
2575 break;
2576 case 'c':
2577 start_commit = optarg;
2578 break;
2579 case 'C':
2580 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2581 &errstr);
2582 if (errstr != NULL)
2583 err(1, "-C option %s", errstr);
2584 break;
2585 case 'l':
2586 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2587 if (errstr != NULL)
2588 err(1, "-l option %s", errstr);
2589 break;
2590 case 'b':
2591 log_branches = 1;
2592 break;
2593 case 'r':
2594 repo_path = realpath(optarg, NULL);
2595 if (repo_path == NULL)
2596 return got_error_from_errno2("realpath",
2597 optarg);
2598 got_path_strip_trailing_slashes(repo_path);
2599 break;
2600 case 's':
2601 search_pattern = optarg;
2602 break;
2603 default:
2604 usage_log();
2605 /* NOTREACHED */
2609 argc -= optind;
2610 argv += optind;
2612 if (diff_context == -1)
2613 diff_context = 3;
2614 else if (!show_patch)
2615 errx(1, "-C reguires -p");
2617 cwd = getcwd(NULL, 0);
2618 if (cwd == NULL) {
2619 error = got_error_from_errno("getcwd");
2620 goto done;
2623 error = got_worktree_open(&worktree, cwd);
2624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2625 goto done;
2626 error = NULL;
2628 if (argc == 0) {
2629 path = strdup("");
2630 if (path == NULL) {
2631 error = got_error_from_errno("strdup");
2632 goto done;
2634 } else if (argc == 1) {
2635 if (worktree) {
2636 error = got_worktree_resolve_path(&path, worktree,
2637 argv[0]);
2638 if (error)
2639 goto done;
2640 } else {
2641 path = strdup(argv[0]);
2642 if (path == NULL) {
2643 error = got_error_from_errno("strdup");
2644 goto done;
2647 } else
2648 usage_log();
2650 if (repo_path == NULL) {
2651 repo_path = worktree ?
2652 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2654 if (repo_path == NULL) {
2655 error = got_error_from_errno("strdup");
2656 goto done;
2659 error = got_repo_open(&repo, repo_path, NULL);
2660 if (error != NULL)
2661 goto done;
2663 error = apply_unveil(got_repo_get_path(repo), 1,
2664 worktree ? got_worktree_get_root_path(worktree) : NULL);
2665 if (error)
2666 goto done;
2668 if (start_commit == NULL) {
2669 struct got_reference *head_ref;
2670 error = got_ref_open(&head_ref, repo,
2671 worktree ? got_worktree_get_head_ref_name(worktree)
2672 : GOT_REF_HEAD, 0);
2673 if (error != NULL)
2674 return error;
2675 error = got_ref_resolve(&id, repo, head_ref);
2676 got_ref_close(head_ref);
2677 if (error != NULL)
2678 return error;
2679 error = got_object_open_as_commit(&commit, repo, id);
2680 } else {
2681 struct got_reference *ref;
2682 error = got_ref_open(&ref, repo, start_commit, 0);
2683 if (error == NULL) {
2684 int obj_type;
2685 error = got_ref_resolve(&id, repo, ref);
2686 got_ref_close(ref);
2687 if (error != NULL)
2688 goto done;
2689 error = got_object_get_type(&obj_type, repo, id);
2690 if (error != NULL)
2691 goto done;
2692 if (obj_type == GOT_OBJ_TYPE_TAG) {
2693 struct got_tag_object *tag;
2694 error = got_object_open_as_tag(&tag, repo, id);
2695 if (error != NULL)
2696 goto done;
2697 if (got_object_tag_get_object_type(tag) !=
2698 GOT_OBJ_TYPE_COMMIT) {
2699 got_object_tag_close(tag);
2700 error = got_error(GOT_ERR_OBJ_TYPE);
2701 goto done;
2703 free(id);
2704 id = got_object_id_dup(
2705 got_object_tag_get_object_id(tag));
2706 if (id == NULL)
2707 error = got_error_from_errno(
2708 "got_object_id_dup");
2709 got_object_tag_close(tag);
2710 if (error)
2711 goto done;
2712 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2713 error = got_error(GOT_ERR_OBJ_TYPE);
2714 goto done;
2716 error = got_object_open_as_commit(&commit, repo, id);
2717 if (error != NULL)
2718 goto done;
2720 if (commit == NULL) {
2721 error = got_repo_match_object_id_prefix(&id,
2722 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2723 if (error != NULL)
2724 return error;
2727 if (error != NULL)
2728 goto done;
2730 if (worktree) {
2731 const char *prefix = got_worktree_get_path_prefix(worktree);
2732 char *p;
2733 if (asprintf(&p, "%s%s%s", prefix,
2734 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2735 error = got_error_from_errno("asprintf");
2736 goto done;
2738 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2739 free(p);
2740 } else
2741 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2742 if (error != NULL)
2743 goto done;
2744 if (in_repo_path) {
2745 free(path);
2746 path = in_repo_path;
2749 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2750 if (error)
2751 goto done;
2753 error = print_commits(id, repo, path, show_patch, search_pattern,
2754 diff_context, limit, log_branches, &refs);
2755 done:
2756 free(path);
2757 free(repo_path);
2758 free(cwd);
2759 free(id);
2760 if (worktree)
2761 got_worktree_close(worktree);
2762 if (repo) {
2763 const struct got_error *repo_error;
2764 repo_error = got_repo_close(repo);
2765 if (error == NULL)
2766 error = repo_error;
2768 got_ref_list_free(&refs);
2769 return error;
2772 __dead static void
2773 usage_diff(void)
2775 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2776 "[-w] [object1 object2 | path]\n", getprogname());
2777 exit(1);
2780 struct print_diff_arg {
2781 struct got_repository *repo;
2782 struct got_worktree *worktree;
2783 int diff_context;
2784 const char *id_str;
2785 int header_shown;
2786 int diff_staged;
2787 int ignore_whitespace;
2790 static const struct got_error *
2791 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2792 const char *path, struct got_object_id *blob_id,
2793 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2794 int dirfd, const char *de_name)
2796 struct print_diff_arg *a = arg;
2797 const struct got_error *err = NULL;
2798 struct got_blob_object *blob1 = NULL;
2799 int fd = -1;
2800 FILE *f2 = NULL;
2801 char *abspath = NULL, *label1 = NULL;
2802 struct stat sb;
2804 if (a->diff_staged) {
2805 if (staged_status != GOT_STATUS_MODIFY &&
2806 staged_status != GOT_STATUS_ADD &&
2807 staged_status != GOT_STATUS_DELETE)
2808 return NULL;
2809 } else {
2810 if (staged_status == GOT_STATUS_DELETE)
2811 return NULL;
2812 if (status == GOT_STATUS_NONEXISTENT)
2813 return got_error_set_errno(ENOENT, path);
2814 if (status != GOT_STATUS_MODIFY &&
2815 status != GOT_STATUS_ADD &&
2816 status != GOT_STATUS_DELETE &&
2817 status != GOT_STATUS_CONFLICT)
2818 return NULL;
2821 if (!a->header_shown) {
2822 printf("diff %s %s%s\n", a->id_str,
2823 got_worktree_get_root_path(a->worktree),
2824 a->diff_staged ? " (staged changes)" : "");
2825 a->header_shown = 1;
2828 if (a->diff_staged) {
2829 const char *label1 = NULL, *label2 = NULL;
2830 switch (staged_status) {
2831 case GOT_STATUS_MODIFY:
2832 label1 = path;
2833 label2 = path;
2834 break;
2835 case GOT_STATUS_ADD:
2836 label2 = path;
2837 break;
2838 case GOT_STATUS_DELETE:
2839 label1 = path;
2840 break;
2841 default:
2842 return got_error(GOT_ERR_FILE_STATUS);
2844 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2845 label1, label2, a->diff_context, a->ignore_whitespace,
2846 a->repo, stdout);
2849 if (staged_status == GOT_STATUS_ADD ||
2850 staged_status == GOT_STATUS_MODIFY) {
2851 char *id_str;
2852 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2853 8192);
2854 if (err)
2855 goto done;
2856 err = got_object_id_str(&id_str, staged_blob_id);
2857 if (err)
2858 goto done;
2859 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2860 err = got_error_from_errno("asprintf");
2861 free(id_str);
2862 goto done;
2864 free(id_str);
2865 } else if (status != GOT_STATUS_ADD) {
2866 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2867 if (err)
2868 goto done;
2871 if (status != GOT_STATUS_DELETE) {
2872 if (asprintf(&abspath, "%s/%s",
2873 got_worktree_get_root_path(a->worktree), path) == -1) {
2874 err = got_error_from_errno("asprintf");
2875 goto done;
2878 if (dirfd != -1) {
2879 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2880 if (fd == -1) {
2881 err = got_error_from_errno2("openat", abspath);
2882 goto done;
2884 } else {
2885 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2886 if (fd == -1) {
2887 err = got_error_from_errno2("open", abspath);
2888 goto done;
2891 if (fstat(fd, &sb) == -1) {
2892 err = got_error_from_errno2("fstat", abspath);
2893 goto done;
2895 f2 = fdopen(fd, "r");
2896 if (f2 == NULL) {
2897 err = got_error_from_errno2("fdopen", abspath);
2898 goto done;
2900 fd = -1;
2901 } else
2902 sb.st_size = 0;
2904 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2905 a->diff_context, a->ignore_whitespace, stdout);
2906 done:
2907 if (blob1)
2908 got_object_blob_close(blob1);
2909 if (f2 && fclose(f2) == EOF && err == NULL)
2910 err = got_error_from_errno("fclose");
2911 if (fd != -1 && close(fd) == -1 && err == NULL)
2912 err = got_error_from_errno("close");
2913 free(abspath);
2914 return err;
2917 static const struct got_error *
2918 cmd_diff(int argc, char *argv[])
2920 const struct got_error *error;
2921 struct got_repository *repo = NULL;
2922 struct got_worktree *worktree = NULL;
2923 char *cwd = NULL, *repo_path = NULL;
2924 struct got_object_id *id1 = NULL, *id2 = NULL;
2925 const char *id_str1 = NULL, *id_str2 = NULL;
2926 char *label1 = NULL, *label2 = NULL;
2927 int type1, type2;
2928 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2929 const char *errstr;
2930 char *path = NULL;
2932 #ifndef PROFILE
2933 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2934 NULL) == -1)
2935 err(1, "pledge");
2936 #endif
2938 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2939 switch (ch) {
2940 case 'C':
2941 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2942 &errstr);
2943 if (errstr != NULL)
2944 err(1, "-C option %s", errstr);
2945 break;
2946 case 'r':
2947 repo_path = realpath(optarg, NULL);
2948 if (repo_path == NULL)
2949 return got_error_from_errno2("realpath",
2950 optarg);
2951 got_path_strip_trailing_slashes(repo_path);
2952 break;
2953 case 's':
2954 diff_staged = 1;
2955 break;
2956 case 'w':
2957 ignore_whitespace = 1;
2958 break;
2959 default:
2960 usage_diff();
2961 /* NOTREACHED */
2965 argc -= optind;
2966 argv += optind;
2968 cwd = getcwd(NULL, 0);
2969 if (cwd == NULL) {
2970 error = got_error_from_errno("getcwd");
2971 goto done;
2973 error = got_worktree_open(&worktree, cwd);
2974 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2975 goto done;
2976 if (argc <= 1) {
2977 if (worktree == NULL) {
2978 error = got_error(GOT_ERR_NOT_WORKTREE);
2979 goto done;
2981 if (repo_path)
2982 errx(1,
2983 "-r option can't be used when diffing a work tree");
2984 repo_path = strdup(got_worktree_get_repo_path(worktree));
2985 if (repo_path == NULL) {
2986 error = got_error_from_errno("strdup");
2987 goto done;
2989 if (argc == 1) {
2990 error = got_worktree_resolve_path(&path, worktree,
2991 argv[0]);
2992 if (error)
2993 goto done;
2994 } else {
2995 path = strdup("");
2996 if (path == NULL) {
2997 error = got_error_from_errno("strdup");
2998 goto done;
3001 } else if (argc == 2) {
3002 if (diff_staged)
3003 errx(1, "-s option can't be used when diffing "
3004 "objects in repository");
3005 id_str1 = argv[0];
3006 id_str2 = argv[1];
3007 if (worktree && repo_path == NULL) {
3008 repo_path =
3009 strdup(got_worktree_get_repo_path(worktree));
3010 if (repo_path == NULL) {
3011 error = got_error_from_errno("strdup");
3012 goto done;
3015 } else
3016 usage_diff();
3018 if (repo_path == NULL) {
3019 repo_path = getcwd(NULL, 0);
3020 if (repo_path == NULL)
3021 return got_error_from_errno("getcwd");
3024 error = got_repo_open(&repo, repo_path, NULL);
3025 free(repo_path);
3026 if (error != NULL)
3027 goto done;
3029 error = apply_unveil(got_repo_get_path(repo), 1,
3030 worktree ? got_worktree_get_root_path(worktree) : NULL);
3031 if (error)
3032 goto done;
3034 if (argc <= 1) {
3035 struct print_diff_arg arg;
3036 struct got_pathlist_head paths;
3037 char *id_str;
3039 TAILQ_INIT(&paths);
3041 error = got_object_id_str(&id_str,
3042 got_worktree_get_base_commit_id(worktree));
3043 if (error)
3044 goto done;
3045 arg.repo = repo;
3046 arg.worktree = worktree;
3047 arg.diff_context = diff_context;
3048 arg.id_str = id_str;
3049 arg.header_shown = 0;
3050 arg.diff_staged = diff_staged;
3051 arg.ignore_whitespace = ignore_whitespace;
3053 error = got_pathlist_append(&paths, path, NULL);
3054 if (error)
3055 goto done;
3057 error = got_worktree_status(worktree, &paths, repo, print_diff,
3058 &arg, check_cancelled, NULL);
3059 free(id_str);
3060 got_pathlist_free(&paths);
3061 goto done;
3064 error = got_repo_match_object_id(&id1, &label1, id_str1,
3065 GOT_OBJ_TYPE_ANY, 1, repo);
3066 if (error)
3067 goto done;
3069 error = got_repo_match_object_id(&id2, &label2, id_str2,
3070 GOT_OBJ_TYPE_ANY, 1, repo);
3071 if (error)
3072 goto done;
3074 error = got_object_get_type(&type1, repo, id1);
3075 if (error)
3076 goto done;
3078 error = got_object_get_type(&type2, repo, id2);
3079 if (error)
3080 goto done;
3082 if (type1 != type2) {
3083 error = got_error(GOT_ERR_OBJ_TYPE);
3084 goto done;
3087 switch (type1) {
3088 case GOT_OBJ_TYPE_BLOB:
3089 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3090 diff_context, ignore_whitespace, repo, stdout);
3091 break;
3092 case GOT_OBJ_TYPE_TREE:
3093 error = got_diff_objects_as_trees(id1, id2, "", "",
3094 diff_context, ignore_whitespace, repo, stdout);
3095 break;
3096 case GOT_OBJ_TYPE_COMMIT:
3097 printf("diff %s %s\n", label1, label2);
3098 error = got_diff_objects_as_commits(id1, id2, diff_context,
3099 ignore_whitespace, repo, stdout);
3100 break;
3101 default:
3102 error = got_error(GOT_ERR_OBJ_TYPE);
3104 done:
3105 free(label1);
3106 free(label2);
3107 free(id1);
3108 free(id2);
3109 free(path);
3110 if (worktree)
3111 got_worktree_close(worktree);
3112 if (repo) {
3113 const struct got_error *repo_error;
3114 repo_error = got_repo_close(repo);
3115 if (error == NULL)
3116 error = repo_error;
3118 return error;
3121 __dead static void
3122 usage_blame(void)
3124 fprintf(stderr,
3125 "usage: %s blame [-c commit] [-r repository-path] path\n",
3126 getprogname());
3127 exit(1);
3130 struct blame_line {
3131 int annotated;
3132 char *id_str;
3133 char *committer;
3134 char datebuf[11]; /* YYYY-MM-DD + NUL */
3137 struct blame_cb_args {
3138 struct blame_line *lines;
3139 int nlines;
3140 int nlines_prec;
3141 int lineno_cur;
3142 off_t *line_offsets;
3143 FILE *f;
3144 struct got_repository *repo;
3147 static const struct got_error *
3148 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3150 const struct got_error *err = NULL;
3151 struct blame_cb_args *a = arg;
3152 struct blame_line *bline;
3153 char *line = NULL;
3154 size_t linesize = 0;
3155 struct got_commit_object *commit = NULL;
3156 off_t offset;
3157 struct tm tm;
3158 time_t committer_time;
3160 if (nlines != a->nlines ||
3161 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3162 return got_error(GOT_ERR_RANGE);
3164 if (sigint_received)
3165 return got_error(GOT_ERR_ITER_COMPLETED);
3167 if (lineno == -1)
3168 return NULL; /* no change in this commit */
3170 /* Annotate this line. */
3171 bline = &a->lines[lineno - 1];
3172 if (bline->annotated)
3173 return NULL;
3174 err = got_object_id_str(&bline->id_str, id);
3175 if (err)
3176 return err;
3178 err = got_object_open_as_commit(&commit, a->repo, id);
3179 if (err)
3180 goto done;
3182 bline->committer = strdup(got_object_commit_get_committer(commit));
3183 if (bline->committer == NULL) {
3184 err = got_error_from_errno("strdup");
3185 goto done;
3188 committer_time = got_object_commit_get_committer_time(commit);
3189 if (localtime_r(&committer_time, &tm) == NULL)
3190 return got_error_from_errno("localtime_r");
3191 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3192 &tm) >= sizeof(bline->datebuf)) {
3193 err = got_error(GOT_ERR_NO_SPACE);
3194 goto done;
3196 bline->annotated = 1;
3198 /* Print lines annotated so far. */
3199 bline = &a->lines[a->lineno_cur - 1];
3200 if (!bline->annotated)
3201 goto done;
3203 offset = a->line_offsets[a->lineno_cur - 1];
3204 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3205 err = got_error_from_errno("fseeko");
3206 goto done;
3209 while (bline->annotated) {
3210 char *smallerthan, *at, *nl, *committer;
3211 size_t len;
3213 if (getline(&line, &linesize, a->f) == -1) {
3214 if (ferror(a->f))
3215 err = got_error_from_errno("getline");
3216 break;
3219 committer = bline->committer;
3220 smallerthan = strchr(committer, '<');
3221 if (smallerthan && smallerthan[1] != '\0')
3222 committer = smallerthan + 1;
3223 at = strchr(committer, '@');
3224 if (at)
3225 *at = '\0';
3226 len = strlen(committer);
3227 if (len >= 9)
3228 committer[8] = '\0';
3230 nl = strchr(line, '\n');
3231 if (nl)
3232 *nl = '\0';
3233 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3234 bline->id_str, bline->datebuf, committer, line);
3236 a->lineno_cur++;
3237 bline = &a->lines[a->lineno_cur - 1];
3239 done:
3240 if (commit)
3241 got_object_commit_close(commit);
3242 free(line);
3243 return err;
3246 static const struct got_error *
3247 cmd_blame(int argc, char *argv[])
3249 const struct got_error *error;
3250 struct got_repository *repo = NULL;
3251 struct got_worktree *worktree = NULL;
3252 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3253 struct got_object_id *obj_id = NULL;
3254 struct got_object_id *commit_id = NULL;
3255 struct got_blob_object *blob = NULL;
3256 char *commit_id_str = NULL;
3257 struct blame_cb_args bca;
3258 int ch, obj_type, i;
3259 size_t filesize;
3261 memset(&bca, 0, sizeof(bca));
3263 #ifndef PROFILE
3264 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3265 NULL) == -1)
3266 err(1, "pledge");
3267 #endif
3269 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3270 switch (ch) {
3271 case 'c':
3272 commit_id_str = optarg;
3273 break;
3274 case 'r':
3275 repo_path = realpath(optarg, NULL);
3276 if (repo_path == NULL)
3277 return got_error_from_errno2("realpath",
3278 optarg);
3279 got_path_strip_trailing_slashes(repo_path);
3280 break;
3281 default:
3282 usage_blame();
3283 /* NOTREACHED */
3287 argc -= optind;
3288 argv += optind;
3290 if (argc == 1)
3291 path = argv[0];
3292 else
3293 usage_blame();
3295 cwd = getcwd(NULL, 0);
3296 if (cwd == NULL) {
3297 error = got_error_from_errno("getcwd");
3298 goto done;
3300 if (repo_path == NULL) {
3301 error = got_worktree_open(&worktree, cwd);
3302 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3303 goto done;
3304 else
3305 error = NULL;
3306 if (worktree) {
3307 repo_path =
3308 strdup(got_worktree_get_repo_path(worktree));
3309 if (repo_path == NULL)
3310 error = got_error_from_errno("strdup");
3311 if (error)
3312 goto done;
3313 } else {
3314 repo_path = strdup(cwd);
3315 if (repo_path == NULL) {
3316 error = got_error_from_errno("strdup");
3317 goto done;
3322 error = got_repo_open(&repo, repo_path, NULL);
3323 if (error != NULL)
3324 goto done;
3326 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3327 if (error)
3328 goto done;
3330 if (worktree) {
3331 const char *prefix = got_worktree_get_path_prefix(worktree);
3332 char *p, *worktree_subdir = cwd +
3333 strlen(got_worktree_get_root_path(worktree));
3334 if (asprintf(&p, "%s%s%s%s%s",
3335 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3336 worktree_subdir, worktree_subdir[0] ? "/" : "",
3337 path) == -1) {
3338 error = got_error_from_errno("asprintf");
3339 goto done;
3341 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3342 free(p);
3343 } else {
3344 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3346 if (error)
3347 goto done;
3349 if (commit_id_str == NULL) {
3350 struct got_reference *head_ref;
3351 error = got_ref_open(&head_ref, repo, worktree ?
3352 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3353 if (error != NULL)
3354 goto done;
3355 error = got_ref_resolve(&commit_id, repo, head_ref);
3356 got_ref_close(head_ref);
3357 if (error != NULL)
3358 goto done;
3359 } else {
3360 error = got_repo_match_object_id(&commit_id, NULL,
3361 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3362 if (error)
3363 goto done;
3366 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3367 if (error)
3368 goto done;
3370 error = got_object_get_type(&obj_type, repo, obj_id);
3371 if (error)
3372 goto done;
3374 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3375 error = got_error(GOT_ERR_OBJ_TYPE);
3376 goto done;
3379 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3380 if (error)
3381 goto done;
3382 bca.f = got_opentemp();
3383 if (bca.f == NULL) {
3384 error = got_error_from_errno("got_opentemp");
3385 goto done;
3387 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3388 &bca.line_offsets, bca.f, blob);
3389 if (error || bca.nlines == 0)
3390 goto done;
3392 /* Don't include \n at EOF in the blame line count. */
3393 if (bca.line_offsets[bca.nlines - 1] == filesize)
3394 bca.nlines--;
3396 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3397 if (bca.lines == NULL) {
3398 error = got_error_from_errno("calloc");
3399 goto done;
3401 bca.lineno_cur = 1;
3402 bca.nlines_prec = 0;
3403 i = bca.nlines;
3404 while (i > 0) {
3405 i /= 10;
3406 bca.nlines_prec++;
3408 bca.repo = repo;
3410 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3411 check_cancelled, NULL);
3412 done:
3413 free(in_repo_path);
3414 free(repo_path);
3415 free(cwd);
3416 free(commit_id);
3417 free(obj_id);
3418 if (blob)
3419 got_object_blob_close(blob);
3420 if (worktree)
3421 got_worktree_close(worktree);
3422 if (repo) {
3423 const struct got_error *repo_error;
3424 repo_error = got_repo_close(repo);
3425 if (error == NULL)
3426 error = repo_error;
3428 if (bca.lines) {
3429 for (i = 0; i < bca.nlines; i++) {
3430 struct blame_line *bline = &bca.lines[i];
3431 free(bline->id_str);
3432 free(bline->committer);
3434 free(bca.lines);
3436 free(bca.line_offsets);
3437 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3438 error = got_error_from_errno("fclose");
3439 return error;
3442 __dead static void
3443 usage_tree(void)
3445 fprintf(stderr,
3446 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3447 getprogname());
3448 exit(1);
3451 static void
3452 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3453 const char *root_path)
3455 int is_root_path = (strcmp(path, root_path) == 0);
3456 const char *modestr = "";
3457 mode_t mode = got_tree_entry_get_mode(te);
3459 path += strlen(root_path);
3460 while (path[0] == '/')
3461 path++;
3463 if (got_object_tree_entry_is_submodule(te))
3464 modestr = "$";
3465 else if (S_ISLNK(mode))
3466 modestr = "@";
3467 else if (S_ISDIR(mode))
3468 modestr = "/";
3469 else if (mode & S_IXUSR)
3470 modestr = "*";
3472 printf("%s%s%s%s%s\n", id ? id : "", path,
3473 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3476 static const struct got_error *
3477 print_tree(const char *path, struct got_object_id *commit_id,
3478 int show_ids, int recurse, const char *root_path,
3479 struct got_repository *repo)
3481 const struct got_error *err = NULL;
3482 struct got_object_id *tree_id = NULL;
3483 struct got_tree_object *tree = NULL;
3484 int nentries, i;
3486 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3487 if (err)
3488 goto done;
3490 err = got_object_open_as_tree(&tree, repo, tree_id);
3491 if (err)
3492 goto done;
3493 nentries = got_object_tree_get_nentries(tree);
3494 for (i = 0; i < nentries; i++) {
3495 struct got_tree_entry *te;
3496 char *id = NULL;
3498 if (sigint_received || sigpipe_received)
3499 break;
3501 te = got_object_tree_get_entry(tree, i);
3502 if (show_ids) {
3503 char *id_str;
3504 err = got_object_id_str(&id_str,
3505 got_tree_entry_get_id(te));
3506 if (err)
3507 goto done;
3508 if (asprintf(&id, "%s ", id_str) == -1) {
3509 err = got_error_from_errno("asprintf");
3510 free(id_str);
3511 goto done;
3513 free(id_str);
3515 print_entry(te, id, path, root_path);
3516 free(id);
3518 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3519 char *child_path;
3520 if (asprintf(&child_path, "%s%s%s", path,
3521 path[0] == '/' && path[1] == '\0' ? "" : "/",
3522 got_tree_entry_get_name(te)) == -1) {
3523 err = got_error_from_errno("asprintf");
3524 goto done;
3526 err = print_tree(child_path, commit_id, show_ids, 1,
3527 root_path, repo);
3528 free(child_path);
3529 if (err)
3530 goto done;
3533 done:
3534 if (tree)
3535 got_object_tree_close(tree);
3536 free(tree_id);
3537 return err;
3540 static const struct got_error *
3541 cmd_tree(int argc, char *argv[])
3543 const struct got_error *error;
3544 struct got_repository *repo = NULL;
3545 struct got_worktree *worktree = NULL;
3546 const char *path;
3547 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3548 struct got_object_id *commit_id = NULL;
3549 char *commit_id_str = NULL;
3550 int show_ids = 0, recurse = 0;
3551 int ch;
3553 #ifndef PROFILE
3554 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3555 NULL) == -1)
3556 err(1, "pledge");
3557 #endif
3559 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3560 switch (ch) {
3561 case 'c':
3562 commit_id_str = optarg;
3563 break;
3564 case 'r':
3565 repo_path = realpath(optarg, NULL);
3566 if (repo_path == NULL)
3567 return got_error_from_errno2("realpath",
3568 optarg);
3569 got_path_strip_trailing_slashes(repo_path);
3570 break;
3571 case 'i':
3572 show_ids = 1;
3573 break;
3574 case 'R':
3575 recurse = 1;
3576 break;
3577 default:
3578 usage_tree();
3579 /* NOTREACHED */
3583 argc -= optind;
3584 argv += optind;
3586 if (argc == 1)
3587 path = argv[0];
3588 else if (argc > 1)
3589 usage_tree();
3590 else
3591 path = NULL;
3593 cwd = getcwd(NULL, 0);
3594 if (cwd == NULL) {
3595 error = got_error_from_errno("getcwd");
3596 goto done;
3598 if (repo_path == NULL) {
3599 error = got_worktree_open(&worktree, cwd);
3600 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3601 goto done;
3602 else
3603 error = NULL;
3604 if (worktree) {
3605 repo_path =
3606 strdup(got_worktree_get_repo_path(worktree));
3607 if (repo_path == NULL)
3608 error = got_error_from_errno("strdup");
3609 if (error)
3610 goto done;
3611 } else {
3612 repo_path = strdup(cwd);
3613 if (repo_path == NULL) {
3614 error = got_error_from_errno("strdup");
3615 goto done;
3620 error = got_repo_open(&repo, repo_path, NULL);
3621 if (error != NULL)
3622 goto done;
3624 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3625 if (error)
3626 goto done;
3628 if (path == NULL) {
3629 if (worktree) {
3630 char *p, *worktree_subdir = cwd +
3631 strlen(got_worktree_get_root_path(worktree));
3632 if (asprintf(&p, "%s/%s",
3633 got_worktree_get_path_prefix(worktree),
3634 worktree_subdir) == -1) {
3635 error = got_error_from_errno("asprintf");
3636 goto done;
3638 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3639 free(p);
3640 if (error)
3641 goto done;
3642 } else
3643 path = "/";
3645 if (in_repo_path == NULL) {
3646 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3647 if (error != NULL)
3648 goto done;
3651 if (commit_id_str == NULL) {
3652 struct got_reference *head_ref;
3653 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3654 if (error != NULL)
3655 goto done;
3656 error = got_ref_resolve(&commit_id, repo, head_ref);
3657 got_ref_close(head_ref);
3658 if (error != NULL)
3659 goto done;
3660 } else {
3661 error = got_repo_match_object_id(&commit_id, NULL,
3662 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3663 if (error)
3664 goto done;
3667 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3668 in_repo_path, repo);
3669 done:
3670 free(in_repo_path);
3671 free(repo_path);
3672 free(cwd);
3673 free(commit_id);
3674 if (worktree)
3675 got_worktree_close(worktree);
3676 if (repo) {
3677 const struct got_error *repo_error;
3678 repo_error = got_repo_close(repo);
3679 if (error == NULL)
3680 error = repo_error;
3682 return error;
3685 __dead static void
3686 usage_status(void)
3688 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3689 exit(1);
3692 static const struct got_error *
3693 print_status(void *arg, unsigned char status, unsigned char staged_status,
3694 const char *path, struct got_object_id *blob_id,
3695 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3696 int dirfd, const char *de_name)
3698 if (status == staged_status && (status == GOT_STATUS_DELETE))
3699 status = GOT_STATUS_NO_CHANGE;
3700 printf("%c%c %s\n", status, staged_status, path);
3701 return NULL;
3704 static const struct got_error *
3705 cmd_status(int argc, char *argv[])
3707 const struct got_error *error = NULL;
3708 struct got_repository *repo = NULL;
3709 struct got_worktree *worktree = NULL;
3710 char *cwd = NULL;
3711 struct got_pathlist_head paths;
3712 struct got_pathlist_entry *pe;
3713 int ch;
3715 TAILQ_INIT(&paths);
3717 while ((ch = getopt(argc, argv, "")) != -1) {
3718 switch (ch) {
3719 default:
3720 usage_status();
3721 /* NOTREACHED */
3725 argc -= optind;
3726 argv += optind;
3728 #ifndef PROFILE
3729 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3730 NULL) == -1)
3731 err(1, "pledge");
3732 #endif
3733 cwd = getcwd(NULL, 0);
3734 if (cwd == NULL) {
3735 error = got_error_from_errno("getcwd");
3736 goto done;
3739 error = got_worktree_open(&worktree, cwd);
3740 if (error != NULL)
3741 goto done;
3743 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3744 NULL);
3745 if (error != NULL)
3746 goto done;
3748 error = apply_unveil(got_repo_get_path(repo), 1,
3749 got_worktree_get_root_path(worktree));
3750 if (error)
3751 goto done;
3753 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3754 if (error)
3755 goto done;
3757 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3758 check_cancelled, NULL);
3759 done:
3760 TAILQ_FOREACH(pe, &paths, entry)
3761 free((char *)pe->path);
3762 got_pathlist_free(&paths);
3763 free(cwd);
3764 return error;
3767 __dead static void
3768 usage_ref(void)
3770 fprintf(stderr,
3771 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3772 getprogname());
3773 exit(1);
3776 static const struct got_error *
3777 list_refs(struct got_repository *repo)
3779 static const struct got_error *err = NULL;
3780 struct got_reflist_head refs;
3781 struct got_reflist_entry *re;
3783 SIMPLEQ_INIT(&refs);
3784 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3785 if (err)
3786 return err;
3788 SIMPLEQ_FOREACH(re, &refs, entry) {
3789 char *refstr;
3790 refstr = got_ref_to_str(re->ref);
3791 if (refstr == NULL)
3792 return got_error_from_errno("got_ref_to_str");
3793 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3794 free(refstr);
3797 got_ref_list_free(&refs);
3798 return NULL;
3801 static const struct got_error *
3802 delete_ref(struct got_repository *repo, const char *refname)
3804 const struct got_error *err = NULL;
3805 struct got_reference *ref;
3807 err = got_ref_open(&ref, repo, refname, 0);
3808 if (err)
3809 return err;
3811 err = got_ref_delete(ref, repo);
3812 got_ref_close(ref);
3813 return err;
3816 static const struct got_error *
3817 add_ref(struct got_repository *repo, const char *refname, const char *target)
3819 const struct got_error *err = NULL;
3820 struct got_object_id *id;
3821 struct got_reference *ref = NULL;
3824 * Don't let the user create a reference name with a leading '-'.
3825 * While technically a valid reference name, this case is usually
3826 * an unintended typo.
3828 if (refname[0] == '-')
3829 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3831 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3832 repo);
3833 if (err) {
3834 struct got_reference *target_ref;
3836 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3837 return err;
3838 err = got_ref_open(&target_ref, repo, target, 0);
3839 if (err)
3840 return err;
3841 err = got_ref_resolve(&id, repo, target_ref);
3842 got_ref_close(target_ref);
3843 if (err)
3844 return err;
3847 err = got_ref_alloc(&ref, refname, id);
3848 if (err)
3849 goto done;
3851 err = got_ref_write(ref, repo);
3852 done:
3853 if (ref)
3854 got_ref_close(ref);
3855 free(id);
3856 return err;
3859 static const struct got_error *
3860 add_symref(struct got_repository *repo, const char *refname, const char *target)
3862 const struct got_error *err = NULL;
3863 struct got_reference *ref = NULL;
3864 struct got_reference *target_ref = NULL;
3867 * Don't let the user create a reference name with a leading '-'.
3868 * While technically a valid reference name, this case is usually
3869 * an unintended typo.
3871 if (refname[0] == '-')
3872 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3874 err = got_ref_open(&target_ref, repo, target, 0);
3875 if (err)
3876 return err;
3878 err = got_ref_alloc_symref(&ref, refname, target_ref);
3879 if (err)
3880 goto done;
3882 err = got_ref_write(ref, repo);
3883 done:
3884 if (target_ref)
3885 got_ref_close(target_ref);
3886 if (ref)
3887 got_ref_close(ref);
3888 return err;
3891 static const struct got_error *
3892 cmd_ref(int argc, char *argv[])
3894 const struct got_error *error = NULL;
3895 struct got_repository *repo = NULL;
3896 struct got_worktree *worktree = NULL;
3897 char *cwd = NULL, *repo_path = NULL;
3898 int ch, do_list = 0, create_symref = 0;
3899 const char *delref = NULL;
3901 /* TODO: Add -s option for adding symbolic references. */
3902 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3903 switch (ch) {
3904 case 'd':
3905 delref = optarg;
3906 break;
3907 case 'r':
3908 repo_path = realpath(optarg, NULL);
3909 if (repo_path == NULL)
3910 return got_error_from_errno2("realpath",
3911 optarg);
3912 got_path_strip_trailing_slashes(repo_path);
3913 break;
3914 case 'l':
3915 do_list = 1;
3916 break;
3917 case 's':
3918 create_symref = 1;
3919 break;
3920 default:
3921 usage_ref();
3922 /* NOTREACHED */
3926 if (do_list && delref)
3927 errx(1, "-l and -d options are mutually exclusive\n");
3929 argc -= optind;
3930 argv += optind;
3932 if (do_list || delref) {
3933 if (create_symref)
3934 errx(1, "-s option cannot be used together with the "
3935 "-l or -d options");
3936 if (argc > 0)
3937 usage_ref();
3938 } else if (argc != 2)
3939 usage_ref();
3941 #ifndef PROFILE
3942 if (do_list) {
3943 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3944 NULL) == -1)
3945 err(1, "pledge");
3946 } else {
3947 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3948 "sendfd unveil", NULL) == -1)
3949 err(1, "pledge");
3951 #endif
3952 cwd = getcwd(NULL, 0);
3953 if (cwd == NULL) {
3954 error = got_error_from_errno("getcwd");
3955 goto done;
3958 if (repo_path == NULL) {
3959 error = got_worktree_open(&worktree, cwd);
3960 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3961 goto done;
3962 else
3963 error = NULL;
3964 if (worktree) {
3965 repo_path =
3966 strdup(got_worktree_get_repo_path(worktree));
3967 if (repo_path == NULL)
3968 error = got_error_from_errno("strdup");
3969 if (error)
3970 goto done;
3971 } else {
3972 repo_path = strdup(cwd);
3973 if (repo_path == NULL) {
3974 error = got_error_from_errno("strdup");
3975 goto done;
3980 error = got_repo_open(&repo, repo_path, NULL);
3981 if (error != NULL)
3982 goto done;
3984 error = apply_unveil(got_repo_get_path(repo), do_list,
3985 worktree ? got_worktree_get_root_path(worktree) : NULL);
3986 if (error)
3987 goto done;
3989 if (do_list)
3990 error = list_refs(repo);
3991 else if (delref)
3992 error = delete_ref(repo, delref);
3993 else if (create_symref)
3994 error = add_symref(repo, argv[0], argv[1]);
3995 else
3996 error = add_ref(repo, argv[0], argv[1]);
3997 done:
3998 if (repo)
3999 got_repo_close(repo);
4000 if (worktree)
4001 got_worktree_close(worktree);
4002 free(cwd);
4003 free(repo_path);
4004 return error;
4007 __dead static void
4008 usage_branch(void)
4010 fprintf(stderr,
4011 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4012 "[name]\n", getprogname());
4013 exit(1);
4016 static const struct got_error *
4017 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4018 struct got_reference *ref)
4020 const struct got_error *err = NULL;
4021 const char *refname, *marker = " ";
4022 char *refstr;
4024 refname = got_ref_get_name(ref);
4025 if (worktree && strcmp(refname,
4026 got_worktree_get_head_ref_name(worktree)) == 0) {
4027 struct got_object_id *id = NULL;
4029 err = got_ref_resolve(&id, repo, ref);
4030 if (err)
4031 return err;
4032 if (got_object_id_cmp(id,
4033 got_worktree_get_base_commit_id(worktree)) == 0)
4034 marker = "* ";
4035 else
4036 marker = "~ ";
4037 free(id);
4040 if (strncmp(refname, "refs/heads/", 11) == 0)
4041 refname += 11;
4042 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4043 refname += 18;
4045 refstr = got_ref_to_str(ref);
4046 if (refstr == NULL)
4047 return got_error_from_errno("got_ref_to_str");
4049 printf("%s%s: %s\n", marker, refname, refstr);
4050 free(refstr);
4051 return NULL;
4054 static const struct got_error *
4055 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4057 const char *refname;
4059 if (worktree == NULL)
4060 return got_error(GOT_ERR_NOT_WORKTREE);
4062 refname = got_worktree_get_head_ref_name(worktree);
4064 if (strncmp(refname, "refs/heads/", 11) == 0)
4065 refname += 11;
4066 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4067 refname += 18;
4069 printf("%s\n", refname);
4071 return NULL;
4074 static const struct got_error *
4075 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4077 static const struct got_error *err = NULL;
4078 struct got_reflist_head refs;
4079 struct got_reflist_entry *re;
4080 struct got_reference *temp_ref = NULL;
4081 int rebase_in_progress, histedit_in_progress;
4083 SIMPLEQ_INIT(&refs);
4085 if (worktree) {
4086 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4087 worktree);
4088 if (err)
4089 return err;
4091 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4092 worktree);
4093 if (err)
4094 return err;
4096 if (rebase_in_progress || histedit_in_progress) {
4097 err = got_ref_open(&temp_ref, repo,
4098 got_worktree_get_head_ref_name(worktree), 0);
4099 if (err)
4100 return err;
4101 list_branch(repo, worktree, temp_ref);
4102 got_ref_close(temp_ref);
4106 err = got_ref_list(&refs, repo, "refs/heads",
4107 got_ref_cmp_by_name, NULL);
4108 if (err)
4109 return err;
4111 SIMPLEQ_FOREACH(re, &refs, entry)
4112 list_branch(repo, worktree, re->ref);
4114 got_ref_list_free(&refs);
4115 return NULL;
4118 static const struct got_error *
4119 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4120 const char *branch_name)
4122 const struct got_error *err = NULL;
4123 struct got_reference *ref = NULL;
4124 char *refname;
4126 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4127 return got_error_from_errno("asprintf");
4129 err = got_ref_open(&ref, repo, refname, 0);
4130 if (err)
4131 goto done;
4133 if (worktree &&
4134 strcmp(got_worktree_get_head_ref_name(worktree),
4135 got_ref_get_name(ref)) == 0) {
4136 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4137 "will not delete this work tree's current branch");
4138 goto done;
4141 err = got_ref_delete(ref, repo);
4142 done:
4143 if (ref)
4144 got_ref_close(ref);
4145 free(refname);
4146 return err;
4149 static const struct got_error *
4150 add_branch(struct got_repository *repo, const char *branch_name,
4151 struct got_object_id *base_commit_id)
4153 const struct got_error *err = NULL;
4154 struct got_reference *ref = NULL;
4155 char *base_refname = NULL, *refname = NULL;
4158 * Don't let the user create a branch name with a leading '-'.
4159 * While technically a valid reference name, this case is usually
4160 * an unintended typo.
4162 if (branch_name[0] == '-')
4163 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4165 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4166 err = got_error_from_errno("asprintf");
4167 goto done;
4170 err = got_ref_open(&ref, repo, refname, 0);
4171 if (err == NULL) {
4172 err = got_error(GOT_ERR_BRANCH_EXISTS);
4173 goto done;
4174 } else if (err->code != GOT_ERR_NOT_REF)
4175 goto done;
4177 err = got_ref_alloc(&ref, refname, base_commit_id);
4178 if (err)
4179 goto done;
4181 err = got_ref_write(ref, repo);
4182 done:
4183 if (ref)
4184 got_ref_close(ref);
4185 free(base_refname);
4186 free(refname);
4187 return err;
4190 static const struct got_error *
4191 cmd_branch(int argc, char *argv[])
4193 const struct got_error *error = NULL;
4194 struct got_repository *repo = NULL;
4195 struct got_worktree *worktree = NULL;
4196 char *cwd = NULL, *repo_path = NULL;
4197 int ch, do_list = 0, do_show = 0, do_update = 1;
4198 const char *delref = NULL, *commit_id_arg = NULL;
4199 struct got_reference *ref = NULL;
4200 struct got_pathlist_head paths;
4201 struct got_pathlist_entry *pe;
4202 struct got_object_id *commit_id = NULL;
4203 char *commit_id_str = NULL;
4205 TAILQ_INIT(&paths);
4207 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4208 switch (ch) {
4209 case 'c':
4210 commit_id_arg = optarg;
4211 break;
4212 case 'd':
4213 delref = optarg;
4214 break;
4215 case 'r':
4216 repo_path = realpath(optarg, NULL);
4217 if (repo_path == NULL)
4218 return got_error_from_errno2("realpath",
4219 optarg);
4220 got_path_strip_trailing_slashes(repo_path);
4221 break;
4222 case 'l':
4223 do_list = 1;
4224 break;
4225 case 'n':
4226 do_update = 0;
4227 break;
4228 default:
4229 usage_branch();
4230 /* NOTREACHED */
4234 if (do_list && delref)
4235 errx(1, "-l and -d options are mutually exclusive\n");
4237 argc -= optind;
4238 argv += optind;
4240 if (!do_list && !delref && argc == 0)
4241 do_show = 1;
4243 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4244 errx(1, "-c option can only be used when creating a branch");
4246 if (do_list || delref) {
4247 if (argc > 0)
4248 usage_branch();
4249 } else if (!do_show && argc != 1)
4250 usage_branch();
4252 #ifndef PROFILE
4253 if (do_list || do_show) {
4254 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4255 NULL) == -1)
4256 err(1, "pledge");
4257 } else {
4258 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4259 "sendfd unveil", NULL) == -1)
4260 err(1, "pledge");
4262 #endif
4263 cwd = getcwd(NULL, 0);
4264 if (cwd == NULL) {
4265 error = got_error_from_errno("getcwd");
4266 goto done;
4269 if (repo_path == NULL) {
4270 error = got_worktree_open(&worktree, cwd);
4271 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4272 goto done;
4273 else
4274 error = NULL;
4275 if (worktree) {
4276 repo_path =
4277 strdup(got_worktree_get_repo_path(worktree));
4278 if (repo_path == NULL)
4279 error = got_error_from_errno("strdup");
4280 if (error)
4281 goto done;
4282 } else {
4283 repo_path = strdup(cwd);
4284 if (repo_path == NULL) {
4285 error = got_error_from_errno("strdup");
4286 goto done;
4291 error = got_repo_open(&repo, repo_path, NULL);
4292 if (error != NULL)
4293 goto done;
4295 error = apply_unveil(got_repo_get_path(repo), do_list,
4296 worktree ? got_worktree_get_root_path(worktree) : NULL);
4297 if (error)
4298 goto done;
4300 if (do_show)
4301 error = show_current_branch(repo, worktree);
4302 else if (do_list)
4303 error = list_branches(repo, worktree);
4304 else if (delref)
4305 error = delete_branch(repo, worktree, delref);
4306 else {
4307 if (commit_id_arg == NULL)
4308 commit_id_arg = worktree ?
4309 got_worktree_get_head_ref_name(worktree) :
4310 GOT_REF_HEAD;
4311 error = got_repo_match_object_id(&commit_id, NULL,
4312 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4313 if (error)
4314 goto done;
4315 error = add_branch(repo, argv[0], commit_id);
4316 if (error)
4317 goto done;
4318 if (worktree && do_update) {
4319 int did_something = 0;
4320 char *branch_refname = NULL;
4322 error = got_object_id_str(&commit_id_str, commit_id);
4323 if (error)
4324 goto done;
4325 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4326 worktree);
4327 if (error)
4328 goto done;
4329 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4330 == -1) {
4331 error = got_error_from_errno("asprintf");
4332 goto done;
4334 error = got_ref_open(&ref, repo, branch_refname, 0);
4335 free(branch_refname);
4336 if (error)
4337 goto done;
4338 error = switch_head_ref(ref, commit_id, worktree,
4339 repo);
4340 if (error)
4341 goto done;
4342 error = got_worktree_set_base_commit_id(worktree, repo,
4343 commit_id);
4344 if (error)
4345 goto done;
4346 error = got_worktree_checkout_files(worktree, &paths,
4347 repo, update_progress, &did_something,
4348 check_cancelled, NULL);
4349 if (error)
4350 goto done;
4351 if (did_something)
4352 printf("Updated to commit %s\n", commit_id_str);
4355 done:
4356 if (ref)
4357 got_ref_close(ref);
4358 if (repo)
4359 got_repo_close(repo);
4360 if (worktree)
4361 got_worktree_close(worktree);
4362 free(cwd);
4363 free(repo_path);
4364 free(commit_id);
4365 free(commit_id_str);
4366 TAILQ_FOREACH(pe, &paths, entry)
4367 free((char *)pe->path);
4368 got_pathlist_free(&paths);
4369 return error;
4373 __dead static void
4374 usage_tag(void)
4376 fprintf(stderr,
4377 "usage: %s tag [-c commit] [-r repository] [-l] "
4378 "[-m message] name\n", getprogname());
4379 exit(1);
4382 #if 0
4383 static const struct got_error *
4384 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4386 const struct got_error *err = NULL;
4387 struct got_reflist_entry *re, *se, *new;
4388 struct got_object_id *re_id, *se_id;
4389 struct got_tag_object *re_tag, *se_tag;
4390 time_t re_time, se_time;
4392 SIMPLEQ_FOREACH(re, tags, entry) {
4393 se = SIMPLEQ_FIRST(sorted);
4394 if (se == NULL) {
4395 err = got_reflist_entry_dup(&new, re);
4396 if (err)
4397 return err;
4398 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4399 continue;
4400 } else {
4401 err = got_ref_resolve(&re_id, repo, re->ref);
4402 if (err)
4403 break;
4404 err = got_object_open_as_tag(&re_tag, repo, re_id);
4405 free(re_id);
4406 if (err)
4407 break;
4408 re_time = got_object_tag_get_tagger_time(re_tag);
4409 got_object_tag_close(re_tag);
4412 while (se) {
4413 err = got_ref_resolve(&se_id, repo, re->ref);
4414 if (err)
4415 break;
4416 err = got_object_open_as_tag(&se_tag, repo, se_id);
4417 free(se_id);
4418 if (err)
4419 break;
4420 se_time = got_object_tag_get_tagger_time(se_tag);
4421 got_object_tag_close(se_tag);
4423 if (se_time > re_time) {
4424 err = got_reflist_entry_dup(&new, re);
4425 if (err)
4426 return err;
4427 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4428 break;
4430 se = SIMPLEQ_NEXT(se, entry);
4431 continue;
4434 done:
4435 return err;
4437 #endif
4439 static const struct got_error *
4440 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4442 static const struct got_error *err = NULL;
4443 struct got_reflist_head refs;
4444 struct got_reflist_entry *re;
4446 SIMPLEQ_INIT(&refs);
4448 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4449 if (err)
4450 return err;
4452 SIMPLEQ_FOREACH(re, &refs, entry) {
4453 const char *refname;
4454 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4455 char datebuf[26];
4456 const char *tagger;
4457 time_t tagger_time;
4458 struct got_object_id *id;
4459 struct got_tag_object *tag;
4460 struct got_commit_object *commit = NULL;
4462 refname = got_ref_get_name(re->ref);
4463 if (strncmp(refname, "refs/tags/", 10) != 0)
4464 continue;
4465 refname += 10;
4466 refstr = got_ref_to_str(re->ref);
4467 if (refstr == NULL) {
4468 err = got_error_from_errno("got_ref_to_str");
4469 break;
4471 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4472 free(refstr);
4474 err = got_ref_resolve(&id, repo, re->ref);
4475 if (err)
4476 break;
4477 err = got_object_open_as_tag(&tag, repo, id);
4478 if (err) {
4479 if (err->code != GOT_ERR_OBJ_TYPE) {
4480 free(id);
4481 break;
4483 /* "lightweight" tag */
4484 err = got_object_open_as_commit(&commit, repo, id);
4485 if (err) {
4486 free(id);
4487 break;
4489 tagger = got_object_commit_get_committer(commit);
4490 tagger_time =
4491 got_object_commit_get_committer_time(commit);
4492 err = got_object_id_str(&id_str, id);
4493 free(id);
4494 if (err)
4495 break;
4496 } else {
4497 free(id);
4498 tagger = got_object_tag_get_tagger(tag);
4499 tagger_time = got_object_tag_get_tagger_time(tag);
4500 err = got_object_id_str(&id_str,
4501 got_object_tag_get_object_id(tag));
4502 if (err)
4503 break;
4505 printf("from: %s\n", tagger);
4506 datestr = get_datestr(&tagger_time, datebuf);
4507 if (datestr)
4508 printf("date: %s UTC\n", datestr);
4509 if (commit)
4510 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4511 else {
4512 switch (got_object_tag_get_object_type(tag)) {
4513 case GOT_OBJ_TYPE_BLOB:
4514 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4515 id_str);
4516 break;
4517 case GOT_OBJ_TYPE_TREE:
4518 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4519 id_str);
4520 break;
4521 case GOT_OBJ_TYPE_COMMIT:
4522 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4523 id_str);
4524 break;
4525 case GOT_OBJ_TYPE_TAG:
4526 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4527 id_str);
4528 break;
4529 default:
4530 break;
4533 free(id_str);
4534 if (commit) {
4535 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4536 if (err)
4537 break;
4538 got_object_commit_close(commit);
4539 } else {
4540 tagmsg0 = strdup(got_object_tag_get_message(tag));
4541 got_object_tag_close(tag);
4542 if (tagmsg0 == NULL) {
4543 err = got_error_from_errno("strdup");
4544 break;
4548 tagmsg = tagmsg0;
4549 do {
4550 line = strsep(&tagmsg, "\n");
4551 if (line)
4552 printf(" %s\n", line);
4553 } while (line);
4554 free(tagmsg0);
4557 got_ref_list_free(&refs);
4558 return NULL;
4561 static const struct got_error *
4562 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4563 const char *tag_name, const char *repo_path)
4565 const struct got_error *err = NULL;
4566 char *template = NULL, *initial_content = NULL;
4567 char *editor = NULL;
4568 int fd = -1;
4570 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4571 err = got_error_from_errno("asprintf");
4572 goto done;
4575 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4576 commit_id_str, tag_name) == -1) {
4577 err = got_error_from_errno("asprintf");
4578 goto done;
4581 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4582 if (err)
4583 goto done;
4585 dprintf(fd, initial_content);
4586 close(fd);
4588 err = get_editor(&editor);
4589 if (err)
4590 goto done;
4591 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4592 done:
4593 free(initial_content);
4594 free(template);
4595 free(editor);
4597 /* Editor is done; we can now apply unveil(2) */
4598 if (err == NULL) {
4599 err = apply_unveil(repo_path, 0, NULL);
4600 if (err) {
4601 free(*tagmsg);
4602 *tagmsg = NULL;
4605 return err;
4608 static const struct got_error *
4609 add_tag(struct got_repository *repo, const char *tag_name,
4610 const char *commit_arg, const char *tagmsg_arg)
4612 const struct got_error *err = NULL;
4613 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4614 char *label = NULL, *commit_id_str = NULL;
4615 struct got_reference *ref = NULL;
4616 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4617 char *tagmsg_path = NULL, *tag_id_str = NULL;
4618 int preserve_tagmsg = 0;
4621 * Don't let the user create a tag name with a leading '-'.
4622 * While technically a valid reference name, this case is usually
4623 * an unintended typo.
4625 if (tag_name[0] == '-')
4626 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4628 err = get_author(&tagger, repo);
4629 if (err)
4630 return err;
4632 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4633 GOT_OBJ_TYPE_COMMIT, 1, repo);
4634 if (err)
4635 goto done;
4637 err = got_object_id_str(&commit_id_str, commit_id);
4638 if (err)
4639 goto done;
4641 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4642 refname = strdup(tag_name);
4643 if (refname == NULL) {
4644 err = got_error_from_errno("strdup");
4645 goto done;
4647 tag_name += 10;
4648 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4649 err = got_error_from_errno("asprintf");
4650 goto done;
4653 err = got_ref_open(&ref, repo, refname, 0);
4654 if (err == NULL) {
4655 err = got_error(GOT_ERR_TAG_EXISTS);
4656 goto done;
4657 } else if (err->code != GOT_ERR_NOT_REF)
4658 goto done;
4660 if (tagmsg_arg == NULL) {
4661 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4662 tag_name, got_repo_get_path(repo));
4663 if (err) {
4664 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4665 tagmsg_path != NULL)
4666 preserve_tagmsg = 1;
4667 goto done;
4671 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4672 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4673 if (err) {
4674 if (tagmsg_path)
4675 preserve_tagmsg = 1;
4676 goto done;
4679 err = got_ref_alloc(&ref, refname, tag_id);
4680 if (err) {
4681 if (tagmsg_path)
4682 preserve_tagmsg = 1;
4683 goto done;
4686 err = got_ref_write(ref, repo);
4687 if (err) {
4688 if (tagmsg_path)
4689 preserve_tagmsg = 1;
4690 goto done;
4693 err = got_object_id_str(&tag_id_str, tag_id);
4694 if (err) {
4695 if (tagmsg_path)
4696 preserve_tagmsg = 1;
4697 goto done;
4699 printf("Created tag %s\n", tag_id_str);
4700 done:
4701 if (preserve_tagmsg) {
4702 fprintf(stderr, "%s: tag message preserved in %s\n",
4703 getprogname(), tagmsg_path);
4704 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4705 err = got_error_from_errno2("unlink", tagmsg_path);
4706 free(tag_id_str);
4707 if (ref)
4708 got_ref_close(ref);
4709 free(commit_id);
4710 free(commit_id_str);
4711 free(refname);
4712 free(tagmsg);
4713 free(tagmsg_path);
4714 free(tagger);
4715 return err;
4718 static const struct got_error *
4719 cmd_tag(int argc, char *argv[])
4721 const struct got_error *error = NULL;
4722 struct got_repository *repo = NULL;
4723 struct got_worktree *worktree = NULL;
4724 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4725 char *gitconfig_path = NULL;
4726 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4727 int ch, do_list = 0;
4729 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4730 switch (ch) {
4731 case 'c':
4732 commit_id_arg = optarg;
4733 break;
4734 case 'm':
4735 tagmsg = optarg;
4736 break;
4737 case 'r':
4738 repo_path = realpath(optarg, NULL);
4739 if (repo_path == NULL)
4740 return got_error_from_errno2("realpath",
4741 optarg);
4742 got_path_strip_trailing_slashes(repo_path);
4743 break;
4744 case 'l':
4745 do_list = 1;
4746 break;
4747 default:
4748 usage_tag();
4749 /* NOTREACHED */
4753 argc -= optind;
4754 argv += optind;
4756 if (do_list) {
4757 if (commit_id_arg != NULL)
4758 errx(1, "-c option can only be used when creating a tag");
4759 if (tagmsg)
4760 errx(1, "-l and -m options are mutually exclusive");
4761 if (argc > 0)
4762 usage_tag();
4763 } else if (argc != 1)
4764 usage_tag();
4766 tag_name = argv[0];
4768 #ifndef PROFILE
4769 if (do_list) {
4770 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4771 NULL) == -1)
4772 err(1, "pledge");
4773 } else {
4774 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4775 "sendfd unveil", NULL) == -1)
4776 err(1, "pledge");
4778 #endif
4779 cwd = getcwd(NULL, 0);
4780 if (cwd == NULL) {
4781 error = got_error_from_errno("getcwd");
4782 goto done;
4785 if (repo_path == NULL) {
4786 error = got_worktree_open(&worktree, cwd);
4787 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4788 goto done;
4789 else
4790 error = NULL;
4791 if (worktree) {
4792 repo_path =
4793 strdup(got_worktree_get_repo_path(worktree));
4794 if (repo_path == NULL)
4795 error = got_error_from_errno("strdup");
4796 if (error)
4797 goto done;
4798 } else {
4799 repo_path = strdup(cwd);
4800 if (repo_path == NULL) {
4801 error = got_error_from_errno("strdup");
4802 goto done;
4807 if (do_list) {
4808 error = got_repo_open(&repo, repo_path, NULL);
4809 if (error != NULL)
4810 goto done;
4811 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4812 if (error)
4813 goto done;
4814 error = list_tags(repo, worktree);
4815 } else {
4816 error = get_gitconfig_path(&gitconfig_path);
4817 if (error)
4818 goto done;
4819 error = got_repo_open(&repo, repo_path, gitconfig_path);
4820 if (error != NULL)
4821 goto done;
4823 if (tagmsg) {
4824 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4825 if (error)
4826 goto done;
4829 if (commit_id_arg == NULL) {
4830 struct got_reference *head_ref;
4831 struct got_object_id *commit_id;
4832 error = got_ref_open(&head_ref, repo,
4833 worktree ? got_worktree_get_head_ref_name(worktree)
4834 : GOT_REF_HEAD, 0);
4835 if (error)
4836 goto done;
4837 error = got_ref_resolve(&commit_id, repo, head_ref);
4838 got_ref_close(head_ref);
4839 if (error)
4840 goto done;
4841 error = got_object_id_str(&commit_id_str, commit_id);
4842 free(commit_id);
4843 if (error)
4844 goto done;
4847 error = add_tag(repo, tag_name,
4848 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4850 done:
4851 if (repo)
4852 got_repo_close(repo);
4853 if (worktree)
4854 got_worktree_close(worktree);
4855 free(cwd);
4856 free(repo_path);
4857 free(gitconfig_path);
4858 free(commit_id_str);
4859 return error;
4862 __dead static void
4863 usage_add(void)
4865 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4866 getprogname());
4867 exit(1);
4870 static const struct got_error *
4871 add_progress(void *arg, unsigned char status, const char *path)
4873 while (path[0] == '/')
4874 path++;
4875 printf("%c %s\n", status, path);
4876 return NULL;
4879 static const struct got_error *
4880 cmd_add(int argc, char *argv[])
4882 const struct got_error *error = NULL;
4883 struct got_repository *repo = NULL;
4884 struct got_worktree *worktree = NULL;
4885 char *cwd = NULL;
4886 struct got_pathlist_head paths;
4887 struct got_pathlist_entry *pe;
4888 int ch, can_recurse = 0, no_ignores = 0;
4890 TAILQ_INIT(&paths);
4892 while ((ch = getopt(argc, argv, "IR")) != -1) {
4893 switch (ch) {
4894 case 'I':
4895 no_ignores = 1;
4896 break;
4897 case 'R':
4898 can_recurse = 1;
4899 break;
4900 default:
4901 usage_add();
4902 /* NOTREACHED */
4906 argc -= optind;
4907 argv += optind;
4909 #ifndef PROFILE
4910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4911 NULL) == -1)
4912 err(1, "pledge");
4913 #endif
4914 if (argc < 1)
4915 usage_add();
4917 cwd = getcwd(NULL, 0);
4918 if (cwd == NULL) {
4919 error = got_error_from_errno("getcwd");
4920 goto done;
4923 error = got_worktree_open(&worktree, cwd);
4924 if (error)
4925 goto done;
4927 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4928 NULL);
4929 if (error != NULL)
4930 goto done;
4932 error = apply_unveil(got_repo_get_path(repo), 1,
4933 got_worktree_get_root_path(worktree));
4934 if (error)
4935 goto done;
4937 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4938 if (error)
4939 goto done;
4941 if (!can_recurse && no_ignores) {
4942 error = got_error_msg(GOT_ERR_BAD_PATH,
4943 "disregarding ignores requires -R option");
4944 goto done;
4948 if (!can_recurse) {
4949 char *ondisk_path;
4950 struct stat sb;
4951 TAILQ_FOREACH(pe, &paths, entry) {
4952 if (asprintf(&ondisk_path, "%s/%s",
4953 got_worktree_get_root_path(worktree),
4954 pe->path) == -1) {
4955 error = got_error_from_errno("asprintf");
4956 goto done;
4958 if (lstat(ondisk_path, &sb) == -1) {
4959 if (errno == ENOENT) {
4960 free(ondisk_path);
4961 continue;
4963 error = got_error_from_errno2("lstat",
4964 ondisk_path);
4965 free(ondisk_path);
4966 goto done;
4968 free(ondisk_path);
4969 if (S_ISDIR(sb.st_mode)) {
4970 error = got_error_msg(GOT_ERR_BAD_PATH,
4971 "adding directories requires -R option");
4972 goto done;
4977 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4978 NULL, repo, no_ignores);
4979 done:
4980 if (repo)
4981 got_repo_close(repo);
4982 if (worktree)
4983 got_worktree_close(worktree);
4984 TAILQ_FOREACH(pe, &paths, entry)
4985 free((char *)pe->path);
4986 got_pathlist_free(&paths);
4987 free(cwd);
4988 return error;
4991 __dead static void
4992 usage_remove(void)
4994 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4995 getprogname());
4996 exit(1);
4999 static const struct got_error *
5000 print_remove_status(void *arg, unsigned char status,
5001 unsigned char staged_status, const char *path)
5003 while (path[0] == '/')
5004 path++;
5005 if (status == GOT_STATUS_NONEXISTENT)
5006 return NULL;
5007 if (status == staged_status && (status == GOT_STATUS_DELETE))
5008 status = GOT_STATUS_NO_CHANGE;
5009 printf("%c%c %s\n", status, staged_status, path);
5010 return NULL;
5013 static const struct got_error *
5014 cmd_remove(int argc, char *argv[])
5016 const struct got_error *error = NULL;
5017 struct got_worktree *worktree = NULL;
5018 struct got_repository *repo = NULL;
5019 char *cwd = NULL;
5020 struct got_pathlist_head paths;
5021 struct got_pathlist_entry *pe;
5022 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5024 TAILQ_INIT(&paths);
5026 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5027 switch (ch) {
5028 case 'f':
5029 delete_local_mods = 1;
5030 break;
5031 case 'k':
5032 keep_on_disk = 1;
5033 break;
5034 case 'R':
5035 can_recurse = 1;
5036 break;
5037 default:
5038 usage_remove();
5039 /* NOTREACHED */
5043 argc -= optind;
5044 argv += optind;
5046 #ifndef PROFILE
5047 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5048 NULL) == -1)
5049 err(1, "pledge");
5050 #endif
5051 if (argc < 1)
5052 usage_remove();
5054 cwd = getcwd(NULL, 0);
5055 if (cwd == NULL) {
5056 error = got_error_from_errno("getcwd");
5057 goto done;
5059 error = got_worktree_open(&worktree, cwd);
5060 if (error)
5061 goto done;
5063 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5064 NULL);
5065 if (error)
5066 goto done;
5068 error = apply_unveil(got_repo_get_path(repo), 1,
5069 got_worktree_get_root_path(worktree));
5070 if (error)
5071 goto done;
5073 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5074 if (error)
5075 goto done;
5077 if (!can_recurse) {
5078 char *ondisk_path;
5079 struct stat sb;
5080 TAILQ_FOREACH(pe, &paths, entry) {
5081 if (asprintf(&ondisk_path, "%s/%s",
5082 got_worktree_get_root_path(worktree),
5083 pe->path) == -1) {
5084 error = got_error_from_errno("asprintf");
5085 goto done;
5087 if (lstat(ondisk_path, &sb) == -1) {
5088 if (errno == ENOENT) {
5089 free(ondisk_path);
5090 continue;
5092 error = got_error_from_errno2("lstat",
5093 ondisk_path);
5094 free(ondisk_path);
5095 goto done;
5097 free(ondisk_path);
5098 if (S_ISDIR(sb.st_mode)) {
5099 error = got_error_msg(GOT_ERR_BAD_PATH,
5100 "removing directories requires -R option");
5101 goto done;
5106 error = got_worktree_schedule_delete(worktree, &paths,
5107 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5108 done:
5109 if (repo)
5110 got_repo_close(repo);
5111 if (worktree)
5112 got_worktree_close(worktree);
5113 TAILQ_FOREACH(pe, &paths, entry)
5114 free((char *)pe->path);
5115 got_pathlist_free(&paths);
5116 free(cwd);
5117 return error;
5120 __dead static void
5121 usage_revert(void)
5123 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5124 "path ...\n", getprogname());
5125 exit(1);
5128 static const struct got_error *
5129 revert_progress(void *arg, unsigned char status, const char *path)
5131 if (status == GOT_STATUS_UNVERSIONED)
5132 return NULL;
5134 while (path[0] == '/')
5135 path++;
5136 printf("%c %s\n", status, path);
5137 return NULL;
5140 struct choose_patch_arg {
5141 FILE *patch_script_file;
5142 const char *action;
5145 static const struct got_error *
5146 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5147 int nchanges, const char *action)
5149 char *line = NULL;
5150 size_t linesize = 0;
5151 ssize_t linelen;
5153 switch (status) {
5154 case GOT_STATUS_ADD:
5155 printf("A %s\n%s this addition? [y/n] ", path, action);
5156 break;
5157 case GOT_STATUS_DELETE:
5158 printf("D %s\n%s this deletion? [y/n] ", path, action);
5159 break;
5160 case GOT_STATUS_MODIFY:
5161 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5162 return got_error_from_errno("fseek");
5163 printf(GOT_COMMIT_SEP_STR);
5164 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5165 printf("%s", line);
5166 if (ferror(patch_file))
5167 return got_error_from_errno("getline");
5168 printf(GOT_COMMIT_SEP_STR);
5169 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5170 path, n, nchanges, action);
5171 break;
5172 default:
5173 return got_error_path(path, GOT_ERR_FILE_STATUS);
5176 return NULL;
5179 static const struct got_error *
5180 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5181 FILE *patch_file, int n, int nchanges)
5183 const struct got_error *err = NULL;
5184 char *line = NULL;
5185 size_t linesize = 0;
5186 ssize_t linelen;
5187 int resp = ' ';
5188 struct choose_patch_arg *a = arg;
5190 *choice = GOT_PATCH_CHOICE_NONE;
5192 if (a->patch_script_file) {
5193 char *nl;
5194 err = show_change(status, path, patch_file, n, nchanges,
5195 a->action);
5196 if (err)
5197 return err;
5198 linelen = getline(&line, &linesize, a->patch_script_file);
5199 if (linelen == -1) {
5200 if (ferror(a->patch_script_file))
5201 return got_error_from_errno("getline");
5202 return NULL;
5204 nl = strchr(line, '\n');
5205 if (nl)
5206 *nl = '\0';
5207 if (strcmp(line, "y") == 0) {
5208 *choice = GOT_PATCH_CHOICE_YES;
5209 printf("y\n");
5210 } else if (strcmp(line, "n") == 0) {
5211 *choice = GOT_PATCH_CHOICE_NO;
5212 printf("n\n");
5213 } else if (strcmp(line, "q") == 0 &&
5214 status == GOT_STATUS_MODIFY) {
5215 *choice = GOT_PATCH_CHOICE_QUIT;
5216 printf("q\n");
5217 } else
5218 printf("invalid response '%s'\n", line);
5219 free(line);
5220 return NULL;
5223 while (resp != 'y' && resp != 'n' && resp != 'q') {
5224 err = show_change(status, path, patch_file, n, nchanges,
5225 a->action);
5226 if (err)
5227 return err;
5228 resp = getchar();
5229 if (resp == '\n')
5230 resp = getchar();
5231 if (status == GOT_STATUS_MODIFY) {
5232 if (resp != 'y' && resp != 'n' && resp != 'q') {
5233 printf("invalid response '%c'\n", resp);
5234 resp = ' ';
5236 } else if (resp != 'y' && resp != 'n') {
5237 printf("invalid response '%c'\n", resp);
5238 resp = ' ';
5242 if (resp == 'y')
5243 *choice = GOT_PATCH_CHOICE_YES;
5244 else if (resp == 'n')
5245 *choice = GOT_PATCH_CHOICE_NO;
5246 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5247 *choice = GOT_PATCH_CHOICE_QUIT;
5249 return NULL;
5253 static const struct got_error *
5254 cmd_revert(int argc, char *argv[])
5256 const struct got_error *error = NULL;
5257 struct got_worktree *worktree = NULL;
5258 struct got_repository *repo = NULL;
5259 char *cwd = NULL, *path = NULL;
5260 struct got_pathlist_head paths;
5261 struct got_pathlist_entry *pe;
5262 int ch, can_recurse = 0, pflag = 0;
5263 FILE *patch_script_file = NULL;
5264 const char *patch_script_path = NULL;
5265 struct choose_patch_arg cpa;
5267 TAILQ_INIT(&paths);
5269 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5270 switch (ch) {
5271 case 'p':
5272 pflag = 1;
5273 break;
5274 case 'F':
5275 patch_script_path = optarg;
5276 break;
5277 case 'R':
5278 can_recurse = 1;
5279 break;
5280 default:
5281 usage_revert();
5282 /* NOTREACHED */
5286 argc -= optind;
5287 argv += optind;
5289 #ifndef PROFILE
5290 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5291 "unveil", NULL) == -1)
5292 err(1, "pledge");
5293 #endif
5294 if (argc < 1)
5295 usage_revert();
5296 if (patch_script_path && !pflag)
5297 errx(1, "-F option can only be used together with -p option");
5299 cwd = getcwd(NULL, 0);
5300 if (cwd == NULL) {
5301 error = got_error_from_errno("getcwd");
5302 goto done;
5304 error = got_worktree_open(&worktree, cwd);
5305 if (error)
5306 goto done;
5308 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5309 NULL);
5310 if (error != NULL)
5311 goto done;
5313 if (patch_script_path) {
5314 patch_script_file = fopen(patch_script_path, "r");
5315 if (patch_script_file == NULL) {
5316 error = got_error_from_errno2("fopen",
5317 patch_script_path);
5318 goto done;
5321 error = apply_unveil(got_repo_get_path(repo), 1,
5322 got_worktree_get_root_path(worktree));
5323 if (error)
5324 goto done;
5326 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5327 if (error)
5328 goto done;
5330 if (!can_recurse) {
5331 char *ondisk_path;
5332 struct stat sb;
5333 TAILQ_FOREACH(pe, &paths, entry) {
5334 if (asprintf(&ondisk_path, "%s/%s",
5335 got_worktree_get_root_path(worktree),
5336 pe->path) == -1) {
5337 error = got_error_from_errno("asprintf");
5338 goto done;
5340 if (lstat(ondisk_path, &sb) == -1) {
5341 if (errno == ENOENT) {
5342 free(ondisk_path);
5343 continue;
5345 error = got_error_from_errno2("lstat",
5346 ondisk_path);
5347 free(ondisk_path);
5348 goto done;
5350 free(ondisk_path);
5351 if (S_ISDIR(sb.st_mode)) {
5352 error = got_error_msg(GOT_ERR_BAD_PATH,
5353 "reverting directories requires -R option");
5354 goto done;
5359 cpa.patch_script_file = patch_script_file;
5360 cpa.action = "revert";
5361 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5362 pflag ? choose_patch : NULL, &cpa, repo);
5363 done:
5364 if (patch_script_file && fclose(patch_script_file) == EOF &&
5365 error == NULL)
5366 error = got_error_from_errno2("fclose", patch_script_path);
5367 if (repo)
5368 got_repo_close(repo);
5369 if (worktree)
5370 got_worktree_close(worktree);
5371 free(path);
5372 free(cwd);
5373 return error;
5376 __dead static void
5377 usage_commit(void)
5379 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5380 getprogname());
5381 exit(1);
5384 struct collect_commit_logmsg_arg {
5385 const char *cmdline_log;
5386 const char *editor;
5387 const char *worktree_path;
5388 const char *branch_name;
5389 const char *repo_path;
5390 char *logmsg_path;
5394 static const struct got_error *
5395 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5396 void *arg)
5398 char *initial_content = NULL;
5399 struct got_pathlist_entry *pe;
5400 const struct got_error *err = NULL;
5401 char *template = NULL;
5402 struct collect_commit_logmsg_arg *a = arg;
5403 int fd;
5404 size_t len;
5406 /* if a message was specified on the command line, just use it */
5407 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5408 len = strlen(a->cmdline_log) + 1;
5409 *logmsg = malloc(len + 1);
5410 if (*logmsg == NULL)
5411 return got_error_from_errno("malloc");
5412 strlcpy(*logmsg, a->cmdline_log, len);
5413 return NULL;
5416 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5417 return got_error_from_errno("asprintf");
5419 if (asprintf(&initial_content,
5420 "\n# changes to be committed on branch %s:\n",
5421 a->branch_name) == -1)
5422 return got_error_from_errno("asprintf");
5424 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5425 if (err)
5426 goto done;
5428 dprintf(fd, initial_content);
5430 TAILQ_FOREACH(pe, commitable_paths, entry) {
5431 struct got_commitable *ct = pe->data;
5432 dprintf(fd, "# %c %s\n",
5433 got_commitable_get_status(ct),
5434 got_commitable_get_path(ct));
5436 close(fd);
5438 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5439 done:
5440 free(initial_content);
5441 free(template);
5443 /* Editor is done; we can now apply unveil(2) */
5444 if (err == NULL) {
5445 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5446 if (err) {
5447 free(*logmsg);
5448 *logmsg = NULL;
5451 return err;
5454 static const struct got_error *
5455 cmd_commit(int argc, char *argv[])
5457 const struct got_error *error = NULL;
5458 struct got_worktree *worktree = NULL;
5459 struct got_repository *repo = NULL;
5460 char *cwd = NULL, *id_str = NULL;
5461 struct got_object_id *id = NULL;
5462 const char *logmsg = NULL;
5463 struct collect_commit_logmsg_arg cl_arg;
5464 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5465 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5466 struct got_pathlist_head paths;
5468 TAILQ_INIT(&paths);
5469 cl_arg.logmsg_path = NULL;
5471 while ((ch = getopt(argc, argv, "m:")) != -1) {
5472 switch (ch) {
5473 case 'm':
5474 logmsg = optarg;
5475 break;
5476 default:
5477 usage_commit();
5478 /* NOTREACHED */
5482 argc -= optind;
5483 argv += optind;
5485 #ifndef PROFILE
5486 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5487 "unveil", NULL) == -1)
5488 err(1, "pledge");
5489 #endif
5490 cwd = getcwd(NULL, 0);
5491 if (cwd == NULL) {
5492 error = got_error_from_errno("getcwd");
5493 goto done;
5495 error = got_worktree_open(&worktree, cwd);
5496 if (error)
5497 goto done;
5499 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5500 if (error)
5501 goto done;
5502 if (rebase_in_progress) {
5503 error = got_error(GOT_ERR_REBASING);
5504 goto done;
5507 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5508 worktree);
5509 if (error)
5510 goto done;
5512 error = get_gitconfig_path(&gitconfig_path);
5513 if (error)
5514 goto done;
5515 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5516 gitconfig_path);
5517 if (error != NULL)
5518 goto done;
5520 error = get_author(&author, repo);
5521 if (error)
5522 return error;
5525 * unveil(2) traverses exec(2); if an editor is used we have
5526 * to apply unveil after the log message has been written.
5528 if (logmsg == NULL || strlen(logmsg) == 0)
5529 error = get_editor(&editor);
5530 else
5531 error = apply_unveil(got_repo_get_path(repo), 0,
5532 got_worktree_get_root_path(worktree));
5533 if (error)
5534 goto done;
5536 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5537 if (error)
5538 goto done;
5540 cl_arg.editor = editor;
5541 cl_arg.cmdline_log = logmsg;
5542 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5543 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5544 if (!histedit_in_progress) {
5545 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5546 error = got_error(GOT_ERR_COMMIT_BRANCH);
5547 goto done;
5549 cl_arg.branch_name += 11;
5551 cl_arg.repo_path = got_repo_get_path(repo);
5552 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5553 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5554 if (error) {
5555 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5556 cl_arg.logmsg_path != NULL)
5557 preserve_logmsg = 1;
5558 goto done;
5561 error = got_object_id_str(&id_str, id);
5562 if (error)
5563 goto done;
5564 printf("Created commit %s\n", id_str);
5565 done:
5566 if (preserve_logmsg) {
5567 fprintf(stderr, "%s: log message preserved in %s\n",
5568 getprogname(), cl_arg.logmsg_path);
5569 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5570 error == NULL)
5571 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5572 free(cl_arg.logmsg_path);
5573 if (repo)
5574 got_repo_close(repo);
5575 if (worktree)
5576 got_worktree_close(worktree);
5577 free(cwd);
5578 free(id_str);
5579 free(gitconfig_path);
5580 free(editor);
5581 free(author);
5582 return error;
5585 __dead static void
5586 usage_cherrypick(void)
5588 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5589 exit(1);
5592 static const struct got_error *
5593 cmd_cherrypick(int argc, char *argv[])
5595 const struct got_error *error = NULL;
5596 struct got_worktree *worktree = NULL;
5597 struct got_repository *repo = NULL;
5598 char *cwd = NULL, *commit_id_str = NULL;
5599 struct got_object_id *commit_id = NULL;
5600 struct got_commit_object *commit = NULL;
5601 struct got_object_qid *pid;
5602 struct got_reference *head_ref = NULL;
5603 int ch, did_something = 0;
5605 while ((ch = getopt(argc, argv, "")) != -1) {
5606 switch (ch) {
5607 default:
5608 usage_cherrypick();
5609 /* NOTREACHED */
5613 argc -= optind;
5614 argv += optind;
5616 #ifndef PROFILE
5617 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5618 "unveil", NULL) == -1)
5619 err(1, "pledge");
5620 #endif
5621 if (argc != 1)
5622 usage_cherrypick();
5624 cwd = getcwd(NULL, 0);
5625 if (cwd == NULL) {
5626 error = got_error_from_errno("getcwd");
5627 goto done;
5629 error = got_worktree_open(&worktree, cwd);
5630 if (error)
5631 goto done;
5633 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5634 NULL);
5635 if (error != NULL)
5636 goto done;
5638 error = apply_unveil(got_repo_get_path(repo), 0,
5639 got_worktree_get_root_path(worktree));
5640 if (error)
5641 goto done;
5643 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5644 GOT_OBJ_TYPE_COMMIT, repo);
5645 if (error != NULL) {
5646 struct got_reference *ref;
5647 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5648 goto done;
5649 error = got_ref_open(&ref, repo, argv[0], 0);
5650 if (error != NULL)
5651 goto done;
5652 error = got_ref_resolve(&commit_id, repo, ref);
5653 got_ref_close(ref);
5654 if (error != NULL)
5655 goto done;
5657 error = got_object_id_str(&commit_id_str, commit_id);
5658 if (error)
5659 goto done;
5661 error = got_ref_open(&head_ref, repo,
5662 got_worktree_get_head_ref_name(worktree), 0);
5663 if (error != NULL)
5664 goto done;
5666 error = check_same_branch(commit_id, head_ref, NULL, repo);
5667 if (error) {
5668 if (error->code != GOT_ERR_ANCESTRY)
5669 goto done;
5670 error = NULL;
5671 } else {
5672 error = got_error(GOT_ERR_SAME_BRANCH);
5673 goto done;
5676 error = got_object_open_as_commit(&commit, repo, commit_id);
5677 if (error)
5678 goto done;
5679 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5680 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5681 commit_id, repo, update_progress, &did_something, check_cancelled,
5682 NULL);
5683 if (error != NULL)
5684 goto done;
5686 if (did_something)
5687 printf("Merged commit %s\n", commit_id_str);
5688 done:
5689 if (commit)
5690 got_object_commit_close(commit);
5691 free(commit_id_str);
5692 if (head_ref)
5693 got_ref_close(head_ref);
5694 if (worktree)
5695 got_worktree_close(worktree);
5696 if (repo)
5697 got_repo_close(repo);
5698 return error;
5701 __dead static void
5702 usage_backout(void)
5704 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5705 exit(1);
5708 static const struct got_error *
5709 cmd_backout(int argc, char *argv[])
5711 const struct got_error *error = NULL;
5712 struct got_worktree *worktree = NULL;
5713 struct got_repository *repo = NULL;
5714 char *cwd = NULL, *commit_id_str = NULL;
5715 struct got_object_id *commit_id = NULL;
5716 struct got_commit_object *commit = NULL;
5717 struct got_object_qid *pid;
5718 struct got_reference *head_ref = NULL;
5719 int ch, did_something = 0;
5721 while ((ch = getopt(argc, argv, "")) != -1) {
5722 switch (ch) {
5723 default:
5724 usage_backout();
5725 /* NOTREACHED */
5729 argc -= optind;
5730 argv += optind;
5732 #ifndef PROFILE
5733 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5734 "unveil", NULL) == -1)
5735 err(1, "pledge");
5736 #endif
5737 if (argc != 1)
5738 usage_backout();
5740 cwd = getcwd(NULL, 0);
5741 if (cwd == NULL) {
5742 error = got_error_from_errno("getcwd");
5743 goto done;
5745 error = got_worktree_open(&worktree, cwd);
5746 if (error)
5747 goto done;
5749 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5750 NULL);
5751 if (error != NULL)
5752 goto done;
5754 error = apply_unveil(got_repo_get_path(repo), 0,
5755 got_worktree_get_root_path(worktree));
5756 if (error)
5757 goto done;
5759 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5760 GOT_OBJ_TYPE_COMMIT, repo);
5761 if (error != NULL) {
5762 struct got_reference *ref;
5763 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5764 goto done;
5765 error = got_ref_open(&ref, repo, argv[0], 0);
5766 if (error != NULL)
5767 goto done;
5768 error = got_ref_resolve(&commit_id, repo, ref);
5769 got_ref_close(ref);
5770 if (error != NULL)
5771 goto done;
5773 error = got_object_id_str(&commit_id_str, commit_id);
5774 if (error)
5775 goto done;
5777 error = got_ref_open(&head_ref, repo,
5778 got_worktree_get_head_ref_name(worktree), 0);
5779 if (error != NULL)
5780 goto done;
5782 error = check_same_branch(commit_id, head_ref, NULL, repo);
5783 if (error)
5784 goto done;
5786 error = got_object_open_as_commit(&commit, repo, commit_id);
5787 if (error)
5788 goto done;
5789 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5790 if (pid == NULL) {
5791 error = got_error(GOT_ERR_ROOT_COMMIT);
5792 goto done;
5795 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5796 update_progress, &did_something, check_cancelled, NULL);
5797 if (error != NULL)
5798 goto done;
5800 if (did_something)
5801 printf("Backed out commit %s\n", commit_id_str);
5802 done:
5803 if (commit)
5804 got_object_commit_close(commit);
5805 free(commit_id_str);
5806 if (head_ref)
5807 got_ref_close(head_ref);
5808 if (worktree)
5809 got_worktree_close(worktree);
5810 if (repo)
5811 got_repo_close(repo);
5812 return error;
5815 __dead static void
5816 usage_rebase(void)
5818 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5819 getprogname());
5820 exit(1);
5823 void
5824 trim_logmsg(char *logmsg, int limit)
5826 char *nl;
5827 size_t len;
5829 len = strlen(logmsg);
5830 if (len > limit)
5831 len = limit;
5832 logmsg[len] = '\0';
5833 nl = strchr(logmsg, '\n');
5834 if (nl)
5835 *nl = '\0';
5838 static const struct got_error *
5839 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5841 const struct got_error *err;
5842 char *logmsg0 = NULL;
5843 const char *s;
5845 err = got_object_commit_get_logmsg(&logmsg0, commit);
5846 if (err)
5847 return err;
5849 s = logmsg0;
5850 while (isspace((unsigned char)s[0]))
5851 s++;
5853 *logmsg = strdup(s);
5854 if (*logmsg == NULL) {
5855 err = got_error_from_errno("strdup");
5856 goto done;
5859 trim_logmsg(*logmsg, limit);
5860 done:
5861 free(logmsg0);
5862 return err;
5865 static const struct got_error *
5866 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5868 const struct got_error *err;
5869 struct got_commit_object *commit = NULL;
5870 char *id_str = NULL, *logmsg = NULL;
5872 err = got_object_open_as_commit(&commit, repo, id);
5873 if (err)
5874 return err;
5876 err = got_object_id_str(&id_str, id);
5877 if (err)
5878 goto done;
5880 id_str[12] = '\0';
5882 err = get_short_logmsg(&logmsg, 42, commit);
5883 if (err)
5884 goto done;
5886 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5887 done:
5888 free(id_str);
5889 got_object_commit_close(commit);
5890 free(logmsg);
5891 return err;
5894 static const struct got_error *
5895 show_rebase_progress(struct got_commit_object *commit,
5896 struct got_object_id *old_id, struct got_object_id *new_id)
5898 const struct got_error *err;
5899 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5901 err = got_object_id_str(&old_id_str, old_id);
5902 if (err)
5903 goto done;
5905 if (new_id) {
5906 err = got_object_id_str(&new_id_str, new_id);
5907 if (err)
5908 goto done;
5911 old_id_str[12] = '\0';
5912 if (new_id_str)
5913 new_id_str[12] = '\0';
5915 err = get_short_logmsg(&logmsg, 42, commit);
5916 if (err)
5917 goto done;
5919 printf("%s -> %s: %s\n", old_id_str,
5920 new_id_str ? new_id_str : "no-op change", logmsg);
5921 done:
5922 free(old_id_str);
5923 free(new_id_str);
5924 free(logmsg);
5925 return err;
5928 static const struct got_error *
5929 rebase_progress(void *arg, unsigned char status, const char *path)
5931 unsigned char *rebase_status = arg;
5933 while (path[0] == '/')
5934 path++;
5935 printf("%c %s\n", status, path);
5937 if (*rebase_status == GOT_STATUS_CONFLICT)
5938 return NULL;
5939 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5940 *rebase_status = status;
5941 return NULL;
5944 static const struct got_error *
5945 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5946 struct got_reference *branch, struct got_reference *new_base_branch,
5947 struct got_reference *tmp_branch, struct got_repository *repo)
5949 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5950 return got_worktree_rebase_complete(worktree, fileindex,
5951 new_base_branch, tmp_branch, branch, repo);
5954 static const struct got_error *
5955 rebase_commit(struct got_pathlist_head *merged_paths,
5956 struct got_worktree *worktree, struct got_fileindex *fileindex,
5957 struct got_reference *tmp_branch,
5958 struct got_object_id *commit_id, struct got_repository *repo)
5960 const struct got_error *error;
5961 struct got_commit_object *commit;
5962 struct got_object_id *new_commit_id;
5964 error = got_object_open_as_commit(&commit, repo, commit_id);
5965 if (error)
5966 return error;
5968 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5969 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5970 if (error) {
5971 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5972 goto done;
5973 error = show_rebase_progress(commit, commit_id, NULL);
5974 } else {
5975 error = show_rebase_progress(commit, commit_id, new_commit_id);
5976 free(new_commit_id);
5978 done:
5979 got_object_commit_close(commit);
5980 return error;
5983 struct check_path_prefix_arg {
5984 const char *path_prefix;
5985 size_t len;
5986 int errcode;
5989 static const struct got_error *
5990 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5991 struct got_blob_object *blob2, struct got_object_id *id1,
5992 struct got_object_id *id2, const char *path1, const char *path2,
5993 mode_t mode1, mode_t mode2, struct got_repository *repo)
5995 struct check_path_prefix_arg *a = arg;
5997 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5998 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5999 return got_error(a->errcode);
6001 return NULL;
6004 static const struct got_error *
6005 check_path_prefix(struct got_object_id *parent_id,
6006 struct got_object_id *commit_id, const char *path_prefix,
6007 int errcode, struct got_repository *repo)
6009 const struct got_error *err;
6010 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6011 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6012 struct check_path_prefix_arg cpp_arg;
6014 if (got_path_is_root_dir(path_prefix))
6015 return NULL;
6017 err = got_object_open_as_commit(&commit, repo, commit_id);
6018 if (err)
6019 goto done;
6021 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6022 if (err)
6023 goto done;
6025 err = got_object_open_as_tree(&tree1, repo,
6026 got_object_commit_get_tree_id(parent_commit));
6027 if (err)
6028 goto done;
6030 err = got_object_open_as_tree(&tree2, repo,
6031 got_object_commit_get_tree_id(commit));
6032 if (err)
6033 goto done;
6035 cpp_arg.path_prefix = path_prefix;
6036 while (cpp_arg.path_prefix[0] == '/')
6037 cpp_arg.path_prefix++;
6038 cpp_arg.len = strlen(cpp_arg.path_prefix);
6039 cpp_arg.errcode = errcode;
6040 err = got_diff_tree(tree1, tree2, "", "", repo,
6041 check_path_prefix_in_diff, &cpp_arg, 0);
6042 done:
6043 if (tree1)
6044 got_object_tree_close(tree1);
6045 if (tree2)
6046 got_object_tree_close(tree2);
6047 if (commit)
6048 got_object_commit_close(commit);
6049 if (parent_commit)
6050 got_object_commit_close(parent_commit);
6051 return err;
6054 static const struct got_error *
6055 collect_commits(struct got_object_id_queue *commits,
6056 struct got_object_id *initial_commit_id,
6057 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6058 const char *path_prefix, int path_prefix_errcode,
6059 struct got_repository *repo)
6061 const struct got_error *err = NULL;
6062 struct got_commit_graph *graph = NULL;
6063 struct got_object_id *parent_id = NULL;
6064 struct got_object_qid *qid;
6065 struct got_object_id *commit_id = initial_commit_id;
6067 err = got_commit_graph_open(&graph, "/", 1);
6068 if (err)
6069 return err;
6071 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6072 check_cancelled, NULL);
6073 if (err)
6074 goto done;
6075 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6076 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6077 check_cancelled, NULL);
6078 if (err) {
6079 if (err->code == GOT_ERR_ITER_COMPLETED) {
6080 err = got_error_msg(GOT_ERR_ANCESTRY,
6081 "ran out of commits to rebase before "
6082 "youngest common ancestor commit has "
6083 "been reached?!?");
6085 goto done;
6086 } else {
6087 err = check_path_prefix(parent_id, commit_id,
6088 path_prefix, path_prefix_errcode, repo);
6089 if (err)
6090 goto done;
6092 err = got_object_qid_alloc(&qid, commit_id);
6093 if (err)
6094 goto done;
6095 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6096 commit_id = parent_id;
6099 done:
6100 got_commit_graph_close(graph);
6101 return err;
6104 static const struct got_error *
6105 cmd_rebase(int argc, char *argv[])
6107 const struct got_error *error = NULL;
6108 struct got_worktree *worktree = NULL;
6109 struct got_repository *repo = NULL;
6110 struct got_fileindex *fileindex = NULL;
6111 char *cwd = NULL;
6112 struct got_reference *branch = NULL;
6113 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6114 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6115 struct got_object_id *resume_commit_id = NULL;
6116 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6117 struct got_commit_object *commit = NULL;
6118 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6119 int histedit_in_progress = 0;
6120 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6121 struct got_object_id_queue commits;
6122 struct got_pathlist_head merged_paths;
6123 const struct got_object_id_queue *parent_ids;
6124 struct got_object_qid *qid, *pid;
6126 SIMPLEQ_INIT(&commits);
6127 TAILQ_INIT(&merged_paths);
6129 while ((ch = getopt(argc, argv, "ac")) != -1) {
6130 switch (ch) {
6131 case 'a':
6132 abort_rebase = 1;
6133 break;
6134 case 'c':
6135 continue_rebase = 1;
6136 break;
6137 default:
6138 usage_rebase();
6139 /* NOTREACHED */
6143 argc -= optind;
6144 argv += optind;
6146 #ifndef PROFILE
6147 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6148 "unveil", NULL) == -1)
6149 err(1, "pledge");
6150 #endif
6151 if (abort_rebase && continue_rebase)
6152 usage_rebase();
6153 else if (abort_rebase || continue_rebase) {
6154 if (argc != 0)
6155 usage_rebase();
6156 } else if (argc != 1)
6157 usage_rebase();
6159 cwd = getcwd(NULL, 0);
6160 if (cwd == NULL) {
6161 error = got_error_from_errno("getcwd");
6162 goto done;
6164 error = got_worktree_open(&worktree, cwd);
6165 if (error)
6166 goto done;
6168 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6169 NULL);
6170 if (error != NULL)
6171 goto done;
6173 error = apply_unveil(got_repo_get_path(repo), 0,
6174 got_worktree_get_root_path(worktree));
6175 if (error)
6176 goto done;
6178 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6179 worktree);
6180 if (error)
6181 goto done;
6182 if (histedit_in_progress) {
6183 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6184 goto done;
6187 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6188 if (error)
6189 goto done;
6191 if (abort_rebase) {
6192 int did_something;
6193 if (!rebase_in_progress) {
6194 error = got_error(GOT_ERR_NOT_REBASING);
6195 goto done;
6197 error = got_worktree_rebase_continue(&resume_commit_id,
6198 &new_base_branch, &tmp_branch, &branch, &fileindex,
6199 worktree, repo);
6200 if (error)
6201 goto done;
6202 printf("Switching work tree to %s\n",
6203 got_ref_get_symref_target(new_base_branch));
6204 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6205 new_base_branch, update_progress, &did_something);
6206 if (error)
6207 goto done;
6208 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6209 goto done; /* nothing else to do */
6212 if (continue_rebase) {
6213 if (!rebase_in_progress) {
6214 error = got_error(GOT_ERR_NOT_REBASING);
6215 goto done;
6217 error = got_worktree_rebase_continue(&resume_commit_id,
6218 &new_base_branch, &tmp_branch, &branch, &fileindex,
6219 worktree, repo);
6220 if (error)
6221 goto done;
6223 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6224 resume_commit_id, repo);
6225 if (error)
6226 goto done;
6228 yca_id = got_object_id_dup(resume_commit_id);
6229 if (yca_id == NULL) {
6230 error = got_error_from_errno("got_object_id_dup");
6231 goto done;
6233 } else {
6234 error = got_ref_open(&branch, repo, argv[0], 0);
6235 if (error != NULL)
6236 goto done;
6239 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6240 if (error)
6241 goto done;
6243 if (!continue_rebase) {
6244 struct got_object_id *base_commit_id;
6246 base_commit_id = got_worktree_get_base_commit_id(worktree);
6247 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6248 base_commit_id, branch_head_commit_id, repo,
6249 check_cancelled, NULL);
6250 if (error)
6251 goto done;
6252 if (yca_id == NULL) {
6253 error = got_error_msg(GOT_ERR_ANCESTRY,
6254 "specified branch shares no common ancestry "
6255 "with work tree's branch");
6256 goto done;
6259 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6260 if (error) {
6261 if (error->code != GOT_ERR_ANCESTRY)
6262 goto done;
6263 error = NULL;
6264 } else {
6265 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6266 "specified branch resolves to a commit which "
6267 "is already contained in work tree's branch");
6268 goto done;
6270 error = got_worktree_rebase_prepare(&new_base_branch,
6271 &tmp_branch, &fileindex, worktree, branch, repo);
6272 if (error)
6273 goto done;
6276 commit_id = branch_head_commit_id;
6277 error = got_object_open_as_commit(&commit, repo, commit_id);
6278 if (error)
6279 goto done;
6281 parent_ids = got_object_commit_get_parent_ids(commit);
6282 pid = SIMPLEQ_FIRST(parent_ids);
6283 if (pid == NULL) {
6284 if (!continue_rebase) {
6285 int did_something;
6286 error = got_worktree_rebase_abort(worktree, fileindex,
6287 repo, new_base_branch, update_progress,
6288 &did_something);
6289 if (error)
6290 goto done;
6291 printf("Rebase of %s aborted\n",
6292 got_ref_get_name(branch));
6294 error = got_error(GOT_ERR_EMPTY_REBASE);
6295 goto done;
6297 error = collect_commits(&commits, commit_id, pid->id,
6298 yca_id, got_worktree_get_path_prefix(worktree),
6299 GOT_ERR_REBASE_PATH, repo);
6300 got_object_commit_close(commit);
6301 commit = NULL;
6302 if (error)
6303 goto done;
6305 if (SIMPLEQ_EMPTY(&commits)) {
6306 if (continue_rebase) {
6307 error = rebase_complete(worktree, fileindex,
6308 branch, new_base_branch, tmp_branch, repo);
6309 goto done;
6310 } else {
6311 /* Fast-forward the reference of the branch. */
6312 struct got_object_id *new_head_commit_id;
6313 char *id_str;
6314 error = got_ref_resolve(&new_head_commit_id, repo,
6315 new_base_branch);
6316 if (error)
6317 goto done;
6318 error = got_object_id_str(&id_str, new_head_commit_id);
6319 printf("Forwarding %s to commit %s\n",
6320 got_ref_get_name(branch), id_str);
6321 free(id_str);
6322 error = got_ref_change_ref(branch,
6323 new_head_commit_id);
6324 if (error)
6325 goto done;
6329 pid = NULL;
6330 SIMPLEQ_FOREACH(qid, &commits, entry) {
6331 commit_id = qid->id;
6332 parent_id = pid ? pid->id : yca_id;
6333 pid = qid;
6335 error = got_worktree_rebase_merge_files(&merged_paths,
6336 worktree, fileindex, parent_id, commit_id, repo,
6337 rebase_progress, &rebase_status, check_cancelled, NULL);
6338 if (error)
6339 goto done;
6341 if (rebase_status == GOT_STATUS_CONFLICT) {
6342 error = show_rebase_merge_conflict(qid->id, repo);
6343 if (error)
6344 goto done;
6345 got_worktree_rebase_pathlist_free(&merged_paths);
6346 break;
6349 error = rebase_commit(&merged_paths, worktree, fileindex,
6350 tmp_branch, commit_id, repo);
6351 got_worktree_rebase_pathlist_free(&merged_paths);
6352 if (error)
6353 goto done;
6356 if (rebase_status == GOT_STATUS_CONFLICT) {
6357 error = got_worktree_rebase_postpone(worktree, fileindex);
6358 if (error)
6359 goto done;
6360 error = got_error_msg(GOT_ERR_CONFLICTS,
6361 "conflicts must be resolved before rebasing can continue");
6362 } else
6363 error = rebase_complete(worktree, fileindex, branch,
6364 new_base_branch, tmp_branch, repo);
6365 done:
6366 got_object_id_queue_free(&commits);
6367 free(branch_head_commit_id);
6368 free(resume_commit_id);
6369 free(yca_id);
6370 if (commit)
6371 got_object_commit_close(commit);
6372 if (branch)
6373 got_ref_close(branch);
6374 if (new_base_branch)
6375 got_ref_close(new_base_branch);
6376 if (tmp_branch)
6377 got_ref_close(tmp_branch);
6378 if (worktree)
6379 got_worktree_close(worktree);
6380 if (repo)
6381 got_repo_close(repo);
6382 return error;
6385 __dead static void
6386 usage_histedit(void)
6388 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6389 getprogname());
6390 exit(1);
6393 #define GOT_HISTEDIT_PICK 'p'
6394 #define GOT_HISTEDIT_EDIT 'e'
6395 #define GOT_HISTEDIT_FOLD 'f'
6396 #define GOT_HISTEDIT_DROP 'd'
6397 #define GOT_HISTEDIT_MESG 'm'
6399 static struct got_histedit_cmd {
6400 unsigned char code;
6401 const char *name;
6402 const char *desc;
6403 } got_histedit_cmds[] = {
6404 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6405 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6406 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6407 "be used" },
6408 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6409 { GOT_HISTEDIT_MESG, "mesg",
6410 "single-line log message for commit above (open editor if empty)" },
6413 struct got_histedit_list_entry {
6414 TAILQ_ENTRY(got_histedit_list_entry) entry;
6415 struct got_object_id *commit_id;
6416 const struct got_histedit_cmd *cmd;
6417 char *logmsg;
6419 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6421 static const struct got_error *
6422 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6423 FILE *f, struct got_repository *repo)
6425 const struct got_error *err = NULL;
6426 char *logmsg = NULL, *id_str = NULL;
6427 struct got_commit_object *commit = NULL;
6428 int n;
6430 err = got_object_open_as_commit(&commit, repo, commit_id);
6431 if (err)
6432 goto done;
6434 err = get_short_logmsg(&logmsg, 34, commit);
6435 if (err)
6436 goto done;
6438 err = got_object_id_str(&id_str, commit_id);
6439 if (err)
6440 goto done;
6442 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6443 if (n < 0)
6444 err = got_ferror(f, GOT_ERR_IO);
6445 done:
6446 if (commit)
6447 got_object_commit_close(commit);
6448 free(id_str);
6449 free(logmsg);
6450 return err;
6453 static const struct got_error *
6454 histedit_write_commit_list(struct got_object_id_queue *commits,
6455 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6457 const struct got_error *err = NULL;
6458 struct got_object_qid *qid;
6460 if (SIMPLEQ_EMPTY(commits))
6461 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6463 SIMPLEQ_FOREACH(qid, commits, entry) {
6464 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6465 f, repo);
6466 if (err)
6467 break;
6468 if (edit_logmsg_only) {
6469 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6470 if (n < 0) {
6471 err = got_ferror(f, GOT_ERR_IO);
6472 break;
6477 return err;
6480 static const struct got_error *
6481 write_cmd_list(FILE *f, const char *branch_name,
6482 struct got_object_id_queue *commits)
6484 const struct got_error *err = NULL;
6485 int n, i;
6486 char *id_str;
6487 struct got_object_qid *qid;
6489 qid = SIMPLEQ_FIRST(commits);
6490 err = got_object_id_str(&id_str, qid->id);
6491 if (err)
6492 return err;
6494 n = fprintf(f,
6495 "# Editing the history of branch '%s' starting at\n"
6496 "# commit %s\n"
6497 "# Commits will be processed in order from top to "
6498 "bottom of this file.\n", branch_name, id_str);
6499 if (n < 0) {
6500 err = got_ferror(f, GOT_ERR_IO);
6501 goto done;
6504 n = fprintf(f, "# Available histedit commands:\n");
6505 if (n < 0) {
6506 err = got_ferror(f, GOT_ERR_IO);
6507 goto done;
6510 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6511 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6512 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6513 cmd->desc);
6514 if (n < 0) {
6515 err = got_ferror(f, GOT_ERR_IO);
6516 break;
6519 done:
6520 free(id_str);
6521 return err;
6524 static const struct got_error *
6525 histedit_syntax_error(int lineno)
6527 static char msg[42];
6528 int ret;
6530 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6531 lineno);
6532 if (ret == -1 || ret >= sizeof(msg))
6533 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6535 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6538 static const struct got_error *
6539 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6540 char *logmsg, struct got_repository *repo)
6542 const struct got_error *err;
6543 struct got_commit_object *folded_commit = NULL;
6544 char *id_str, *folded_logmsg = NULL;
6546 err = got_object_id_str(&id_str, hle->commit_id);
6547 if (err)
6548 return err;
6550 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6551 if (err)
6552 goto done;
6554 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6555 if (err)
6556 goto done;
6557 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6558 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6559 folded_logmsg) == -1) {
6560 err = got_error_from_errno("asprintf");
6562 done:
6563 if (folded_commit)
6564 got_object_commit_close(folded_commit);
6565 free(id_str);
6566 free(folded_logmsg);
6567 return err;
6570 static struct got_histedit_list_entry *
6571 get_folded_commits(struct got_histedit_list_entry *hle)
6573 struct got_histedit_list_entry *prev, *folded = NULL;
6575 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6576 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6577 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6578 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6579 folded = prev;
6580 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6583 return folded;
6586 static const struct got_error *
6587 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6588 struct got_repository *repo)
6590 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6591 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6592 const struct got_error *err = NULL;
6593 struct got_commit_object *commit = NULL;
6594 int fd;
6595 struct got_histedit_list_entry *folded = NULL;
6597 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6598 if (err)
6599 return err;
6601 folded = get_folded_commits(hle);
6602 if (folded) {
6603 while (folded != hle) {
6604 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6605 folded = TAILQ_NEXT(folded, entry);
6606 continue;
6608 err = append_folded_commit_msg(&new_msg, folded,
6609 logmsg, repo);
6610 if (err)
6611 goto done;
6612 free(logmsg);
6613 logmsg = new_msg;
6614 folded = TAILQ_NEXT(folded, entry);
6618 err = got_object_id_str(&id_str, hle->commit_id);
6619 if (err)
6620 goto done;
6621 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6622 if (err)
6623 goto done;
6624 if (asprintf(&new_msg,
6625 "%s\n# original log message of commit %s: %s",
6626 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6627 err = got_error_from_errno("asprintf");
6628 goto done;
6630 free(logmsg);
6631 logmsg = new_msg;
6633 err = got_object_id_str(&id_str, hle->commit_id);
6634 if (err)
6635 goto done;
6637 err = got_opentemp_named_fd(&logmsg_path, &fd,
6638 GOT_TMPDIR_STR "/got-logmsg");
6639 if (err)
6640 goto done;
6642 dprintf(fd, logmsg);
6643 close(fd);
6645 err = get_editor(&editor);
6646 if (err)
6647 goto done;
6649 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6650 if (err) {
6651 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6652 goto done;
6653 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6655 done:
6656 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6657 err = got_error_from_errno2("unlink", logmsg_path);
6658 free(logmsg_path);
6659 free(logmsg);
6660 free(orig_logmsg);
6661 free(editor);
6662 if (commit)
6663 got_object_commit_close(commit);
6664 return err;
6667 static const struct got_error *
6668 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6669 FILE *f, struct got_repository *repo)
6671 const struct got_error *err = NULL;
6672 char *line = NULL, *p, *end;
6673 size_t size;
6674 ssize_t len;
6675 int lineno = 0, i;
6676 const struct got_histedit_cmd *cmd;
6677 struct got_object_id *commit_id = NULL;
6678 struct got_histedit_list_entry *hle = NULL;
6680 for (;;) {
6681 len = getline(&line, &size, f);
6682 if (len == -1) {
6683 const struct got_error *getline_err;
6684 if (feof(f))
6685 break;
6686 getline_err = got_error_from_errno("getline");
6687 err = got_ferror(f, getline_err->code);
6688 break;
6690 lineno++;
6691 p = line;
6692 while (isspace((unsigned char)p[0]))
6693 p++;
6694 if (p[0] == '#' || p[0] == '\0') {
6695 free(line);
6696 line = NULL;
6697 continue;
6699 cmd = NULL;
6700 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6701 cmd = &got_histedit_cmds[i];
6702 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6703 isspace((unsigned char)p[strlen(cmd->name)])) {
6704 p += strlen(cmd->name);
6705 break;
6707 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6708 p++;
6709 break;
6712 if (i == nitems(got_histedit_cmds)) {
6713 err = histedit_syntax_error(lineno);
6714 break;
6716 while (isspace((unsigned char)p[0]))
6717 p++;
6718 if (cmd->code == GOT_HISTEDIT_MESG) {
6719 if (hle == NULL || hle->logmsg != NULL) {
6720 err = got_error(GOT_ERR_HISTEDIT_CMD);
6721 break;
6723 if (p[0] == '\0') {
6724 err = histedit_edit_logmsg(hle, repo);
6725 if (err)
6726 break;
6727 } else {
6728 hle->logmsg = strdup(p);
6729 if (hle->logmsg == NULL) {
6730 err = got_error_from_errno("strdup");
6731 break;
6734 free(line);
6735 line = NULL;
6736 continue;
6737 } else {
6738 end = p;
6739 while (end[0] && !isspace((unsigned char)end[0]))
6740 end++;
6741 *end = '\0';
6743 err = got_object_resolve_id_str(&commit_id, repo, p);
6744 if (err) {
6745 /* override error code */
6746 err = histedit_syntax_error(lineno);
6747 break;
6750 hle = malloc(sizeof(*hle));
6751 if (hle == NULL) {
6752 err = got_error_from_errno("malloc");
6753 break;
6755 hle->cmd = cmd;
6756 hle->commit_id = commit_id;
6757 hle->logmsg = NULL;
6758 commit_id = NULL;
6759 free(line);
6760 line = NULL;
6761 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6764 free(line);
6765 free(commit_id);
6766 return err;
6769 static const struct got_error *
6770 histedit_check_script(struct got_histedit_list *histedit_cmds,
6771 struct got_object_id_queue *commits, struct got_repository *repo)
6773 const struct got_error *err = NULL;
6774 struct got_object_qid *qid;
6775 struct got_histedit_list_entry *hle;
6776 static char msg[92];
6777 char *id_str;
6779 if (TAILQ_EMPTY(histedit_cmds))
6780 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6781 "histedit script contains no commands");
6782 if (SIMPLEQ_EMPTY(commits))
6783 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6785 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6786 struct got_histedit_list_entry *hle2;
6787 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6788 if (hle == hle2)
6789 continue;
6790 if (got_object_id_cmp(hle->commit_id,
6791 hle2->commit_id) != 0)
6792 continue;
6793 err = got_object_id_str(&id_str, hle->commit_id);
6794 if (err)
6795 return err;
6796 snprintf(msg, sizeof(msg), "commit %s is listed "
6797 "more than once in histedit script", id_str);
6798 free(id_str);
6799 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6803 SIMPLEQ_FOREACH(qid, commits, entry) {
6804 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6805 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6806 break;
6808 if (hle == NULL) {
6809 err = got_object_id_str(&id_str, qid->id);
6810 if (err)
6811 return err;
6812 snprintf(msg, sizeof(msg),
6813 "commit %s missing from histedit script", id_str);
6814 free(id_str);
6815 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6819 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6820 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6821 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6822 "last commit in histedit script cannot be folded");
6824 return NULL;
6827 static const struct got_error *
6828 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6829 const char *path, struct got_object_id_queue *commits,
6830 struct got_repository *repo)
6832 const struct got_error *err = NULL;
6833 char *editor;
6834 FILE *f = NULL;
6836 err = get_editor(&editor);
6837 if (err)
6838 return err;
6840 if (spawn_editor(editor, path) == -1) {
6841 err = got_error_from_errno("failed spawning editor");
6842 goto done;
6845 f = fopen(path, "r");
6846 if (f == NULL) {
6847 err = got_error_from_errno("fopen");
6848 goto done;
6850 err = histedit_parse_list(histedit_cmds, f, repo);
6851 if (err)
6852 goto done;
6854 err = histedit_check_script(histedit_cmds, commits, repo);
6855 done:
6856 if (f && fclose(f) != 0 && err == NULL)
6857 err = got_error_from_errno("fclose");
6858 free(editor);
6859 return err;
6862 static const struct got_error *
6863 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6864 struct got_object_id_queue *, const char *, const char *,
6865 struct got_repository *);
6867 static const struct got_error *
6868 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6869 struct got_object_id_queue *commits, const char *branch_name,
6870 int edit_logmsg_only, struct got_repository *repo)
6872 const struct got_error *err;
6873 FILE *f = NULL;
6874 char *path = NULL;
6876 err = got_opentemp_named(&path, &f, "got-histedit");
6877 if (err)
6878 return err;
6880 err = write_cmd_list(f, branch_name, commits);
6881 if (err)
6882 goto done;
6884 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6885 if (err)
6886 goto done;
6888 if (edit_logmsg_only) {
6889 rewind(f);
6890 err = histedit_parse_list(histedit_cmds, f, repo);
6891 } else {
6892 if (fclose(f) != 0) {
6893 err = got_error_from_errno("fclose");
6894 goto done;
6896 f = NULL;
6897 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6898 if (err) {
6899 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6900 err->code != GOT_ERR_HISTEDIT_CMD)
6901 goto done;
6902 err = histedit_edit_list_retry(histedit_cmds, err,
6903 commits, path, branch_name, repo);
6906 done:
6907 if (f && fclose(f) != 0 && err == NULL)
6908 err = got_error_from_errno("fclose");
6909 if (path && unlink(path) != 0 && err == NULL)
6910 err = got_error_from_errno2("unlink", path);
6911 free(path);
6912 return err;
6915 static const struct got_error *
6916 histedit_save_list(struct got_histedit_list *histedit_cmds,
6917 struct got_worktree *worktree, struct got_repository *repo)
6919 const struct got_error *err = NULL;
6920 char *path = NULL;
6921 FILE *f = NULL;
6922 struct got_histedit_list_entry *hle;
6923 struct got_commit_object *commit = NULL;
6925 err = got_worktree_get_histedit_script_path(&path, worktree);
6926 if (err)
6927 return err;
6929 f = fopen(path, "w");
6930 if (f == NULL) {
6931 err = got_error_from_errno2("fopen", path);
6932 goto done;
6934 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6935 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6936 repo);
6937 if (err)
6938 break;
6940 if (hle->logmsg) {
6941 int n = fprintf(f, "%c %s\n",
6942 GOT_HISTEDIT_MESG, hle->logmsg);
6943 if (n < 0) {
6944 err = got_ferror(f, GOT_ERR_IO);
6945 break;
6949 done:
6950 if (f && fclose(f) != 0 && err == NULL)
6951 err = got_error_from_errno("fclose");
6952 free(path);
6953 if (commit)
6954 got_object_commit_close(commit);
6955 return err;
6958 void
6959 histedit_free_list(struct got_histedit_list *histedit_cmds)
6961 struct got_histedit_list_entry *hle;
6963 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6964 TAILQ_REMOVE(histedit_cmds, hle, entry);
6965 free(hle);
6969 static const struct got_error *
6970 histedit_load_list(struct got_histedit_list *histedit_cmds,
6971 const char *path, struct got_repository *repo)
6973 const struct got_error *err = NULL;
6974 FILE *f = NULL;
6976 f = fopen(path, "r");
6977 if (f == NULL) {
6978 err = got_error_from_errno2("fopen", path);
6979 goto done;
6982 err = histedit_parse_list(histedit_cmds, f, repo);
6983 done:
6984 if (f && fclose(f) != 0 && err == NULL)
6985 err = got_error_from_errno("fclose");
6986 return err;
6989 static const struct got_error *
6990 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6991 const struct got_error *edit_err, struct got_object_id_queue *commits,
6992 const char *path, const char *branch_name, struct got_repository *repo)
6994 const struct got_error *err = NULL, *prev_err = edit_err;
6995 int resp = ' ';
6997 while (resp != 'c' && resp != 'r' && resp != 'a') {
6998 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6999 "or (a)bort: ", getprogname(), prev_err->msg);
7000 resp = getchar();
7001 if (resp == '\n')
7002 resp = getchar();
7003 if (resp == 'c') {
7004 histedit_free_list(histedit_cmds);
7005 err = histedit_run_editor(histedit_cmds, path, commits,
7006 repo);
7007 if (err) {
7008 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7009 err->code != GOT_ERR_HISTEDIT_CMD)
7010 break;
7011 prev_err = err;
7012 resp = ' ';
7013 continue;
7015 break;
7016 } else if (resp == 'r') {
7017 histedit_free_list(histedit_cmds);
7018 err = histedit_edit_script(histedit_cmds,
7019 commits, branch_name, 0, repo);
7020 if (err) {
7021 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7022 err->code != GOT_ERR_HISTEDIT_CMD)
7023 break;
7024 prev_err = err;
7025 resp = ' ';
7026 continue;
7028 break;
7029 } else if (resp == 'a') {
7030 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7031 break;
7032 } else
7033 printf("invalid response '%c'\n", resp);
7036 return err;
7039 static const struct got_error *
7040 histedit_complete(struct got_worktree *worktree,
7041 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7042 struct got_reference *branch, struct got_repository *repo)
7044 printf("Switching work tree to %s\n",
7045 got_ref_get_symref_target(branch));
7046 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7047 branch, repo);
7050 static const struct got_error *
7051 show_histedit_progress(struct got_commit_object *commit,
7052 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7054 const struct got_error *err;
7055 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7057 err = got_object_id_str(&old_id_str, hle->commit_id);
7058 if (err)
7059 goto done;
7061 if (new_id) {
7062 err = got_object_id_str(&new_id_str, new_id);
7063 if (err)
7064 goto done;
7067 old_id_str[12] = '\0';
7068 if (new_id_str)
7069 new_id_str[12] = '\0';
7071 if (hle->logmsg) {
7072 logmsg = strdup(hle->logmsg);
7073 if (logmsg == NULL) {
7074 err = got_error_from_errno("strdup");
7075 goto done;
7077 trim_logmsg(logmsg, 42);
7078 } else {
7079 err = get_short_logmsg(&logmsg, 42, commit);
7080 if (err)
7081 goto done;
7084 switch (hle->cmd->code) {
7085 case GOT_HISTEDIT_PICK:
7086 case GOT_HISTEDIT_EDIT:
7087 printf("%s -> %s: %s\n", old_id_str,
7088 new_id_str ? new_id_str : "no-op change", logmsg);
7089 break;
7090 case GOT_HISTEDIT_DROP:
7091 case GOT_HISTEDIT_FOLD:
7092 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7093 logmsg);
7094 break;
7095 default:
7096 break;
7098 done:
7099 free(old_id_str);
7100 free(new_id_str);
7101 return err;
7104 static const struct got_error *
7105 histedit_commit(struct got_pathlist_head *merged_paths,
7106 struct got_worktree *worktree, struct got_fileindex *fileindex,
7107 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7108 struct got_repository *repo)
7110 const struct got_error *err;
7111 struct got_commit_object *commit;
7112 struct got_object_id *new_commit_id;
7114 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7115 && hle->logmsg == NULL) {
7116 err = histedit_edit_logmsg(hle, repo);
7117 if (err)
7118 return err;
7121 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7122 if (err)
7123 return err;
7125 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7126 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7127 hle->logmsg, repo);
7128 if (err) {
7129 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7130 goto done;
7131 err = show_histedit_progress(commit, hle, NULL);
7132 } else {
7133 err = show_histedit_progress(commit, hle, new_commit_id);
7134 free(new_commit_id);
7136 done:
7137 got_object_commit_close(commit);
7138 return err;
7141 static const struct got_error *
7142 histedit_skip_commit(struct got_histedit_list_entry *hle,
7143 struct got_worktree *worktree, struct got_repository *repo)
7145 const struct got_error *error;
7146 struct got_commit_object *commit;
7148 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7149 repo);
7150 if (error)
7151 return error;
7153 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7154 if (error)
7155 return error;
7157 error = show_histedit_progress(commit, hle, NULL);
7158 got_object_commit_close(commit);
7159 return error;
7162 static const struct got_error *
7163 check_local_changes(void *arg, unsigned char status,
7164 unsigned char staged_status, const char *path,
7165 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7166 struct got_object_id *commit_id, int dirfd, const char *de_name)
7168 int *have_local_changes = arg;
7170 switch (status) {
7171 case GOT_STATUS_ADD:
7172 case GOT_STATUS_DELETE:
7173 case GOT_STATUS_MODIFY:
7174 case GOT_STATUS_CONFLICT:
7175 *have_local_changes = 1;
7176 return got_error(GOT_ERR_CANCELLED);
7177 default:
7178 break;
7181 switch (staged_status) {
7182 case GOT_STATUS_ADD:
7183 case GOT_STATUS_DELETE:
7184 case GOT_STATUS_MODIFY:
7185 *have_local_changes = 1;
7186 return got_error(GOT_ERR_CANCELLED);
7187 default:
7188 break;
7191 return NULL;
7194 static const struct got_error *
7195 cmd_histedit(int argc, char *argv[])
7197 const struct got_error *error = NULL;
7198 struct got_worktree *worktree = NULL;
7199 struct got_fileindex *fileindex = NULL;
7200 struct got_repository *repo = NULL;
7201 char *cwd = NULL;
7202 struct got_reference *branch = NULL;
7203 struct got_reference *tmp_branch = NULL;
7204 struct got_object_id *resume_commit_id = NULL;
7205 struct got_object_id *base_commit_id = NULL;
7206 struct got_object_id *head_commit_id = NULL;
7207 struct got_commit_object *commit = NULL;
7208 int ch, rebase_in_progress = 0, did_something;
7209 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7210 int edit_logmsg_only = 0;
7211 const char *edit_script_path = NULL;
7212 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7213 struct got_object_id_queue commits;
7214 struct got_pathlist_head merged_paths;
7215 const struct got_object_id_queue *parent_ids;
7216 struct got_object_qid *pid;
7217 struct got_histedit_list histedit_cmds;
7218 struct got_histedit_list_entry *hle;
7220 SIMPLEQ_INIT(&commits);
7221 TAILQ_INIT(&histedit_cmds);
7222 TAILQ_INIT(&merged_paths);
7224 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7225 switch (ch) {
7226 case 'a':
7227 abort_edit = 1;
7228 break;
7229 case 'c':
7230 continue_edit = 1;
7231 break;
7232 case 'F':
7233 edit_script_path = optarg;
7234 break;
7235 case 'm':
7236 edit_logmsg_only = 1;
7237 break;
7238 default:
7239 usage_histedit();
7240 /* NOTREACHED */
7244 argc -= optind;
7245 argv += optind;
7247 #ifndef PROFILE
7248 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7249 "unveil", NULL) == -1)
7250 err(1, "pledge");
7251 #endif
7252 if (abort_edit && continue_edit)
7253 errx(1, "histedit's -a and -c options are mutually exclusive");
7254 if (edit_script_path && edit_logmsg_only)
7255 errx(1, "histedit's -F and -m options are mutually exclusive");
7256 if (abort_edit && edit_logmsg_only)
7257 errx(1, "histedit's -a and -m options are mutually exclusive");
7258 if (continue_edit && edit_logmsg_only)
7259 errx(1, "histedit's -c and -m options are mutually exclusive");
7260 if (argc != 0)
7261 usage_histedit();
7264 * This command cannot apply unveil(2) in all cases because the
7265 * user may choose to run an editor to edit the histedit script
7266 * and to edit individual commit log messages.
7267 * unveil(2) traverses exec(2); if an editor is used we have to
7268 * apply unveil after edit script and log messages have been written.
7269 * XXX TODO: Make use of unveil(2) where possible.
7272 cwd = getcwd(NULL, 0);
7273 if (cwd == NULL) {
7274 error = got_error_from_errno("getcwd");
7275 goto done;
7277 error = got_worktree_open(&worktree, cwd);
7278 if (error)
7279 goto done;
7281 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7282 NULL);
7283 if (error != NULL)
7284 goto done;
7286 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7287 if (error)
7288 goto done;
7289 if (rebase_in_progress) {
7290 error = got_error(GOT_ERR_REBASING);
7291 goto done;
7294 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7295 if (error)
7296 goto done;
7298 if (edit_in_progress && edit_logmsg_only) {
7299 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7300 "histedit operation is in progress in this "
7301 "work tree and must be continued or aborted "
7302 "before the -m option can be used");
7303 goto done;
7306 if (edit_in_progress && abort_edit) {
7307 error = got_worktree_histedit_continue(&resume_commit_id,
7308 &tmp_branch, &branch, &base_commit_id, &fileindex,
7309 worktree, repo);
7310 if (error)
7311 goto done;
7312 printf("Switching work tree to %s\n",
7313 got_ref_get_symref_target(branch));
7314 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7315 branch, base_commit_id, update_progress, &did_something);
7316 if (error)
7317 goto done;
7318 printf("Histedit of %s aborted\n",
7319 got_ref_get_symref_target(branch));
7320 goto done; /* nothing else to do */
7321 } else if (abort_edit) {
7322 error = got_error(GOT_ERR_NOT_HISTEDIT);
7323 goto done;
7326 if (continue_edit) {
7327 char *path;
7329 if (!edit_in_progress) {
7330 error = got_error(GOT_ERR_NOT_HISTEDIT);
7331 goto done;
7334 error = got_worktree_get_histedit_script_path(&path, worktree);
7335 if (error)
7336 goto done;
7338 error = histedit_load_list(&histedit_cmds, path, repo);
7339 free(path);
7340 if (error)
7341 goto done;
7343 error = got_worktree_histedit_continue(&resume_commit_id,
7344 &tmp_branch, &branch, &base_commit_id, &fileindex,
7345 worktree, repo);
7346 if (error)
7347 goto done;
7349 error = got_ref_resolve(&head_commit_id, repo, branch);
7350 if (error)
7351 goto done;
7353 error = got_object_open_as_commit(&commit, repo,
7354 head_commit_id);
7355 if (error)
7356 goto done;
7357 parent_ids = got_object_commit_get_parent_ids(commit);
7358 pid = SIMPLEQ_FIRST(parent_ids);
7359 if (pid == NULL) {
7360 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7361 goto done;
7363 error = collect_commits(&commits, head_commit_id, pid->id,
7364 base_commit_id, got_worktree_get_path_prefix(worktree),
7365 GOT_ERR_HISTEDIT_PATH, repo);
7366 got_object_commit_close(commit);
7367 commit = NULL;
7368 if (error)
7369 goto done;
7370 } else {
7371 if (edit_in_progress) {
7372 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7373 goto done;
7376 error = got_ref_open(&branch, repo,
7377 got_worktree_get_head_ref_name(worktree), 0);
7378 if (error != NULL)
7379 goto done;
7381 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7382 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7383 "will not edit commit history of a branch outside "
7384 "the \"refs/heads/\" reference namespace");
7385 goto done;
7388 error = got_ref_resolve(&head_commit_id, repo, branch);
7389 got_ref_close(branch);
7390 branch = NULL;
7391 if (error)
7392 goto done;
7394 error = got_object_open_as_commit(&commit, repo,
7395 head_commit_id);
7396 if (error)
7397 goto done;
7398 parent_ids = got_object_commit_get_parent_ids(commit);
7399 pid = SIMPLEQ_FIRST(parent_ids);
7400 if (pid == NULL) {
7401 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7402 goto done;
7404 error = collect_commits(&commits, head_commit_id, pid->id,
7405 got_worktree_get_base_commit_id(worktree),
7406 got_worktree_get_path_prefix(worktree),
7407 GOT_ERR_HISTEDIT_PATH, repo);
7408 got_object_commit_close(commit);
7409 commit = NULL;
7410 if (error)
7411 goto done;
7413 if (SIMPLEQ_EMPTY(&commits)) {
7414 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7415 goto done;
7418 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7419 &base_commit_id, &fileindex, worktree, repo);
7420 if (error)
7421 goto done;
7423 if (edit_script_path) {
7424 error = histedit_load_list(&histedit_cmds,
7425 edit_script_path, repo);
7426 if (error) {
7427 got_worktree_histedit_abort(worktree, fileindex,
7428 repo, branch, base_commit_id,
7429 update_progress, &did_something);
7430 goto done;
7432 } else {
7433 const char *branch_name;
7434 branch_name = got_ref_get_symref_target(branch);
7435 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7436 branch_name += 11;
7437 error = histedit_edit_script(&histedit_cmds, &commits,
7438 branch_name, edit_logmsg_only, repo);
7439 if (error) {
7440 got_worktree_histedit_abort(worktree, fileindex,
7441 repo, branch, base_commit_id,
7442 update_progress, &did_something);
7443 goto done;
7448 error = histedit_save_list(&histedit_cmds, worktree,
7449 repo);
7450 if (error) {
7451 got_worktree_histedit_abort(worktree, fileindex,
7452 repo, branch, base_commit_id,
7453 update_progress, &did_something);
7454 goto done;
7459 error = histedit_check_script(&histedit_cmds, &commits, repo);
7460 if (error)
7461 goto done;
7463 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7464 if (resume_commit_id) {
7465 if (got_object_id_cmp(hle->commit_id,
7466 resume_commit_id) != 0)
7467 continue;
7469 resume_commit_id = NULL;
7470 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7471 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7472 error = histedit_skip_commit(hle, worktree,
7473 repo);
7474 if (error)
7475 goto done;
7476 } else {
7477 struct got_pathlist_head paths;
7478 int have_changes = 0;
7480 TAILQ_INIT(&paths);
7481 error = got_pathlist_append(&paths, "", NULL);
7482 if (error)
7483 goto done;
7484 error = got_worktree_status(worktree, &paths,
7485 repo, check_local_changes, &have_changes,
7486 check_cancelled, NULL);
7487 got_pathlist_free(&paths);
7488 if (error) {
7489 if (error->code != GOT_ERR_CANCELLED)
7490 goto done;
7491 if (sigint_received || sigpipe_received)
7492 goto done;
7494 if (have_changes) {
7495 error = histedit_commit(NULL, worktree,
7496 fileindex, tmp_branch, hle, repo);
7497 if (error)
7498 goto done;
7499 } else {
7500 error = got_object_open_as_commit(
7501 &commit, repo, hle->commit_id);
7502 if (error)
7503 goto done;
7504 error = show_histedit_progress(commit,
7505 hle, NULL);
7506 got_object_commit_close(commit);
7507 commit = NULL;
7508 if (error)
7509 goto done;
7512 continue;
7515 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7516 error = histedit_skip_commit(hle, worktree, repo);
7517 if (error)
7518 goto done;
7519 continue;
7522 error = got_object_open_as_commit(&commit, repo,
7523 hle->commit_id);
7524 if (error)
7525 goto done;
7526 parent_ids = got_object_commit_get_parent_ids(commit);
7527 pid = SIMPLEQ_FIRST(parent_ids);
7529 error = got_worktree_histedit_merge_files(&merged_paths,
7530 worktree, fileindex, pid->id, hle->commit_id, repo,
7531 rebase_progress, &rebase_status, check_cancelled, NULL);
7532 if (error)
7533 goto done;
7534 got_object_commit_close(commit);
7535 commit = NULL;
7537 if (rebase_status == GOT_STATUS_CONFLICT) {
7538 error = show_rebase_merge_conflict(hle->commit_id,
7539 repo);
7540 if (error)
7541 goto done;
7542 got_worktree_rebase_pathlist_free(&merged_paths);
7543 break;
7546 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7547 char *id_str;
7548 error = got_object_id_str(&id_str, hle->commit_id);
7549 if (error)
7550 goto done;
7551 printf("Stopping histedit for amending commit %s\n",
7552 id_str);
7553 free(id_str);
7554 got_worktree_rebase_pathlist_free(&merged_paths);
7555 error = got_worktree_histedit_postpone(worktree,
7556 fileindex);
7557 goto done;
7560 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7561 error = histedit_skip_commit(hle, worktree, repo);
7562 if (error)
7563 goto done;
7564 continue;
7567 error = histedit_commit(&merged_paths, worktree, fileindex,
7568 tmp_branch, hle, repo);
7569 got_worktree_rebase_pathlist_free(&merged_paths);
7570 if (error)
7571 goto done;
7574 if (rebase_status == GOT_STATUS_CONFLICT) {
7575 error = got_worktree_histedit_postpone(worktree, fileindex);
7576 if (error)
7577 goto done;
7578 error = got_error_msg(GOT_ERR_CONFLICTS,
7579 "conflicts must be resolved before histedit can continue");
7580 } else
7581 error = histedit_complete(worktree, fileindex, tmp_branch,
7582 branch, repo);
7583 done:
7584 got_object_id_queue_free(&commits);
7585 histedit_free_list(&histedit_cmds);
7586 free(head_commit_id);
7587 free(base_commit_id);
7588 free(resume_commit_id);
7589 if (commit)
7590 got_object_commit_close(commit);
7591 if (branch)
7592 got_ref_close(branch);
7593 if (tmp_branch)
7594 got_ref_close(tmp_branch);
7595 if (worktree)
7596 got_worktree_close(worktree);
7597 if (repo)
7598 got_repo_close(repo);
7599 return error;
7602 __dead static void
7603 usage_integrate(void)
7605 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7606 exit(1);
7609 static const struct got_error *
7610 cmd_integrate(int argc, char *argv[])
7612 const struct got_error *error = NULL;
7613 struct got_repository *repo = NULL;
7614 struct got_worktree *worktree = NULL;
7615 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7616 const char *branch_arg = NULL;
7617 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7618 struct got_fileindex *fileindex = NULL;
7619 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7620 int ch, did_something = 0;
7622 while ((ch = getopt(argc, argv, "")) != -1) {
7623 switch (ch) {
7624 default:
7625 usage_integrate();
7626 /* NOTREACHED */
7630 argc -= optind;
7631 argv += optind;
7633 if (argc != 1)
7634 usage_integrate();
7635 branch_arg = argv[0];
7637 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7638 "unveil", NULL) == -1)
7639 err(1, "pledge");
7641 cwd = getcwd(NULL, 0);
7642 if (cwd == NULL) {
7643 error = got_error_from_errno("getcwd");
7644 goto done;
7647 error = got_worktree_open(&worktree, cwd);
7648 if (error)
7649 goto done;
7651 error = check_rebase_or_histedit_in_progress(worktree);
7652 if (error)
7653 goto done;
7655 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7656 NULL);
7657 if (error != NULL)
7658 goto done;
7660 error = apply_unveil(got_repo_get_path(repo), 0,
7661 got_worktree_get_root_path(worktree));
7662 if (error)
7663 goto done;
7665 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7666 error = got_error_from_errno("asprintf");
7667 goto done;
7670 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7671 &base_branch_ref, worktree, refname, repo);
7672 if (error)
7673 goto done;
7675 refname = strdup(got_ref_get_name(branch_ref));
7676 if (refname == NULL) {
7677 error = got_error_from_errno("strdup");
7678 got_worktree_integrate_abort(worktree, fileindex, repo,
7679 branch_ref, base_branch_ref);
7680 goto done;
7682 base_refname = strdup(got_ref_get_name(base_branch_ref));
7683 if (base_refname == NULL) {
7684 error = got_error_from_errno("strdup");
7685 got_worktree_integrate_abort(worktree, fileindex, repo,
7686 branch_ref, base_branch_ref);
7687 goto done;
7690 error = got_ref_resolve(&commit_id, repo, branch_ref);
7691 if (error)
7692 goto done;
7694 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7695 if (error)
7696 goto done;
7698 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7699 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7700 "specified branch has already been integrated");
7701 got_worktree_integrate_abort(worktree, fileindex, repo,
7702 branch_ref, base_branch_ref);
7703 goto done;
7706 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7707 if (error) {
7708 if (error->code == GOT_ERR_ANCESTRY)
7709 error = got_error(GOT_ERR_REBASE_REQUIRED);
7710 got_worktree_integrate_abort(worktree, fileindex, repo,
7711 branch_ref, base_branch_ref);
7712 goto done;
7715 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7716 branch_ref, base_branch_ref, update_progress, &did_something,
7717 check_cancelled, NULL);
7718 if (error)
7719 goto done;
7721 printf("Integrated %s into %s\n", refname, base_refname);
7722 done:
7723 if (repo)
7724 got_repo_close(repo);
7725 if (worktree)
7726 got_worktree_close(worktree);
7727 free(cwd);
7728 free(base_commit_id);
7729 free(commit_id);
7730 free(refname);
7731 free(base_refname);
7732 return error;
7735 __dead static void
7736 usage_stage(void)
7738 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7739 "[file-path ...]\n",
7740 getprogname());
7741 exit(1);
7744 static const struct got_error *
7745 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7746 const char *path, struct got_object_id *blob_id,
7747 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7748 int dirfd, const char *de_name)
7750 const struct got_error *err = NULL;
7751 char *id_str = NULL;
7753 if (staged_status != GOT_STATUS_ADD &&
7754 staged_status != GOT_STATUS_MODIFY &&
7755 staged_status != GOT_STATUS_DELETE)
7756 return NULL;
7758 if (staged_status == GOT_STATUS_ADD ||
7759 staged_status == GOT_STATUS_MODIFY)
7760 err = got_object_id_str(&id_str, staged_blob_id);
7761 else
7762 err = got_object_id_str(&id_str, blob_id);
7763 if (err)
7764 return err;
7766 printf("%s %c %s\n", id_str, staged_status, path);
7767 free(id_str);
7768 return NULL;
7771 static const struct got_error *
7772 cmd_stage(int argc, char *argv[])
7774 const struct got_error *error = NULL;
7775 struct got_repository *repo = NULL;
7776 struct got_worktree *worktree = NULL;
7777 char *cwd = NULL;
7778 struct got_pathlist_head paths;
7779 struct got_pathlist_entry *pe;
7780 int ch, list_stage = 0, pflag = 0;
7781 FILE *patch_script_file = NULL;
7782 const char *patch_script_path = NULL;
7783 struct choose_patch_arg cpa;
7785 TAILQ_INIT(&paths);
7787 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7788 switch (ch) {
7789 case 'l':
7790 list_stage = 1;
7791 break;
7792 case 'p':
7793 pflag = 1;
7794 break;
7795 case 'F':
7796 patch_script_path = optarg;
7797 break;
7798 default:
7799 usage_stage();
7800 /* NOTREACHED */
7804 argc -= optind;
7805 argv += optind;
7807 #ifndef PROFILE
7808 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7809 "unveil", NULL) == -1)
7810 err(1, "pledge");
7811 #endif
7812 if (list_stage && (pflag || patch_script_path))
7813 errx(1, "-l option cannot be used with other options");
7814 if (patch_script_path && !pflag)
7815 errx(1, "-F option can only be used together with -p option");
7817 cwd = getcwd(NULL, 0);
7818 if (cwd == NULL) {
7819 error = got_error_from_errno("getcwd");
7820 goto done;
7823 error = got_worktree_open(&worktree, cwd);
7824 if (error)
7825 goto done;
7827 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7828 NULL);
7829 if (error != NULL)
7830 goto done;
7832 if (patch_script_path) {
7833 patch_script_file = fopen(patch_script_path, "r");
7834 if (patch_script_file == NULL) {
7835 error = got_error_from_errno2("fopen",
7836 patch_script_path);
7837 goto done;
7840 error = apply_unveil(got_repo_get_path(repo), 0,
7841 got_worktree_get_root_path(worktree));
7842 if (error)
7843 goto done;
7845 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7846 if (error)
7847 goto done;
7849 if (list_stage)
7850 error = got_worktree_status(worktree, &paths, repo,
7851 print_stage, NULL, check_cancelled, NULL);
7852 else {
7853 cpa.patch_script_file = patch_script_file;
7854 cpa.action = "stage";
7855 error = got_worktree_stage(worktree, &paths,
7856 pflag ? NULL : print_status, NULL,
7857 pflag ? choose_patch : NULL, &cpa, repo);
7859 done:
7860 if (patch_script_file && fclose(patch_script_file) == EOF &&
7861 error == NULL)
7862 error = got_error_from_errno2("fclose", patch_script_path);
7863 if (repo)
7864 got_repo_close(repo);
7865 if (worktree)
7866 got_worktree_close(worktree);
7867 TAILQ_FOREACH(pe, &paths, entry)
7868 free((char *)pe->path);
7869 got_pathlist_free(&paths);
7870 free(cwd);
7871 return error;
7874 __dead static void
7875 usage_unstage(void)
7877 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7878 "[file-path ...]\n",
7879 getprogname());
7880 exit(1);
7884 static const struct got_error *
7885 cmd_unstage(int argc, char *argv[])
7887 const struct got_error *error = NULL;
7888 struct got_repository *repo = NULL;
7889 struct got_worktree *worktree = NULL;
7890 char *cwd = NULL;
7891 struct got_pathlist_head paths;
7892 struct got_pathlist_entry *pe;
7893 int ch, did_something = 0, pflag = 0;
7894 FILE *patch_script_file = NULL;
7895 const char *patch_script_path = NULL;
7896 struct choose_patch_arg cpa;
7898 TAILQ_INIT(&paths);
7900 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7901 switch (ch) {
7902 case 'p':
7903 pflag = 1;
7904 break;
7905 case 'F':
7906 patch_script_path = optarg;
7907 break;
7908 default:
7909 usage_unstage();
7910 /* NOTREACHED */
7914 argc -= optind;
7915 argv += optind;
7917 #ifndef PROFILE
7918 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7919 "unveil", NULL) == -1)
7920 err(1, "pledge");
7921 #endif
7922 if (patch_script_path && !pflag)
7923 errx(1, "-F option can only be used together with -p option");
7925 cwd = getcwd(NULL, 0);
7926 if (cwd == NULL) {
7927 error = got_error_from_errno("getcwd");
7928 goto done;
7931 error = got_worktree_open(&worktree, cwd);
7932 if (error)
7933 goto done;
7935 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7936 NULL);
7937 if (error != NULL)
7938 goto done;
7940 if (patch_script_path) {
7941 patch_script_file = fopen(patch_script_path, "r");
7942 if (patch_script_file == NULL) {
7943 error = got_error_from_errno2("fopen",
7944 patch_script_path);
7945 goto done;
7949 error = apply_unveil(got_repo_get_path(repo), 0,
7950 got_worktree_get_root_path(worktree));
7951 if (error)
7952 goto done;
7954 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7955 if (error)
7956 goto done;
7958 cpa.patch_script_file = patch_script_file;
7959 cpa.action = "unstage";
7960 error = got_worktree_unstage(worktree, &paths, update_progress,
7961 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7962 done:
7963 if (patch_script_file && fclose(patch_script_file) == EOF &&
7964 error == NULL)
7965 error = got_error_from_errno2("fclose", patch_script_path);
7966 if (repo)
7967 got_repo_close(repo);
7968 if (worktree)
7969 got_worktree_close(worktree);
7970 TAILQ_FOREACH(pe, &paths, entry)
7971 free((char *)pe->path);
7972 got_pathlist_free(&paths);
7973 free(cwd);
7974 return error;
7977 __dead static void
7978 usage_cat(void)
7980 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7981 "arg1 [arg2 ...]\n", getprogname());
7982 exit(1);
7985 static const struct got_error *
7986 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7988 const struct got_error *err;
7989 struct got_blob_object *blob;
7991 err = got_object_open_as_blob(&blob, repo, id, 8192);
7992 if (err)
7993 return err;
7995 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7996 got_object_blob_close(blob);
7997 return err;
8000 static const struct got_error *
8001 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8003 const struct got_error *err;
8004 struct got_tree_object *tree;
8005 int nentries, i;
8007 err = got_object_open_as_tree(&tree, repo, id);
8008 if (err)
8009 return err;
8011 nentries = got_object_tree_get_nentries(tree);
8012 for (i = 0; i < nentries; i++) {
8013 struct got_tree_entry *te;
8014 char *id_str;
8015 if (sigint_received || sigpipe_received)
8016 break;
8017 te = got_object_tree_get_entry(tree, i);
8018 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8019 if (err)
8020 break;
8021 fprintf(outfile, "%s %.7o %s\n", id_str,
8022 got_tree_entry_get_mode(te),
8023 got_tree_entry_get_name(te));
8024 free(id_str);
8027 got_object_tree_close(tree);
8028 return err;
8031 static const struct got_error *
8032 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8034 const struct got_error *err;
8035 struct got_commit_object *commit;
8036 const struct got_object_id_queue *parent_ids;
8037 struct got_object_qid *pid;
8038 char *id_str = NULL;
8039 const char *logmsg = NULL;
8041 err = got_object_open_as_commit(&commit, repo, id);
8042 if (err)
8043 return err;
8045 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8046 if (err)
8047 goto done;
8049 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8050 parent_ids = got_object_commit_get_parent_ids(commit);
8051 fprintf(outfile, "numparents %d\n",
8052 got_object_commit_get_nparents(commit));
8053 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8054 char *pid_str;
8055 err = got_object_id_str(&pid_str, pid->id);
8056 if (err)
8057 goto done;
8058 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8059 free(pid_str);
8061 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8062 got_object_commit_get_author(commit),
8063 got_object_commit_get_author_time(commit));
8065 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8066 got_object_commit_get_author(commit),
8067 got_object_commit_get_committer_time(commit));
8069 logmsg = got_object_commit_get_logmsg_raw(commit);
8070 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8071 fprintf(outfile, "%s", logmsg);
8072 done:
8073 free(id_str);
8074 got_object_commit_close(commit);
8075 return err;
8078 static const struct got_error *
8079 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8081 const struct got_error *err;
8082 struct got_tag_object *tag;
8083 char *id_str = NULL;
8084 const char *tagmsg = NULL;
8086 err = got_object_open_as_tag(&tag, repo, id);
8087 if (err)
8088 return err;
8090 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8091 if (err)
8092 goto done;
8094 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8096 switch (got_object_tag_get_object_type(tag)) {
8097 case GOT_OBJ_TYPE_BLOB:
8098 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8099 GOT_OBJ_LABEL_BLOB);
8100 break;
8101 case GOT_OBJ_TYPE_TREE:
8102 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8103 GOT_OBJ_LABEL_TREE);
8104 break;
8105 case GOT_OBJ_TYPE_COMMIT:
8106 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8107 GOT_OBJ_LABEL_COMMIT);
8108 break;
8109 case GOT_OBJ_TYPE_TAG:
8110 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8111 GOT_OBJ_LABEL_TAG);
8112 break;
8113 default:
8114 break;
8117 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8118 got_object_tag_get_name(tag));
8120 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8121 got_object_tag_get_tagger(tag),
8122 got_object_tag_get_tagger_time(tag));
8124 tagmsg = got_object_tag_get_message(tag);
8125 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8126 fprintf(outfile, "%s", tagmsg);
8127 done:
8128 free(id_str);
8129 got_object_tag_close(tag);
8130 return err;
8133 static const struct got_error *
8134 cmd_cat(int argc, char *argv[])
8136 const struct got_error *error;
8137 struct got_repository *repo = NULL;
8138 struct got_worktree *worktree = NULL;
8139 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8140 const char *commit_id_str = NULL;
8141 struct got_object_id *id = NULL, *commit_id = NULL;
8142 int ch, obj_type, i, force_path = 0;
8144 #ifndef PROFILE
8145 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8146 NULL) == -1)
8147 err(1, "pledge");
8148 #endif
8150 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8151 switch (ch) {
8152 case 'c':
8153 commit_id_str = optarg;
8154 break;
8155 case 'r':
8156 repo_path = realpath(optarg, NULL);
8157 if (repo_path == NULL)
8158 return got_error_from_errno2("realpath",
8159 optarg);
8160 got_path_strip_trailing_slashes(repo_path);
8161 break;
8162 case 'P':
8163 force_path = 1;
8164 break;
8165 default:
8166 usage_cat();
8167 /* NOTREACHED */
8171 argc -= optind;
8172 argv += optind;
8174 cwd = getcwd(NULL, 0);
8175 if (cwd == NULL) {
8176 error = got_error_from_errno("getcwd");
8177 goto done;
8179 error = got_worktree_open(&worktree, cwd);
8180 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8181 goto done;
8182 if (worktree) {
8183 if (repo_path == NULL) {
8184 repo_path = strdup(
8185 got_worktree_get_repo_path(worktree));
8186 if (repo_path == NULL) {
8187 error = got_error_from_errno("strdup");
8188 goto done;
8193 if (repo_path == NULL) {
8194 repo_path = getcwd(NULL, 0);
8195 if (repo_path == NULL)
8196 return got_error_from_errno("getcwd");
8199 error = got_repo_open(&repo, repo_path, NULL);
8200 free(repo_path);
8201 if (error != NULL)
8202 goto done;
8204 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8205 if (error)
8206 goto done;
8208 if (commit_id_str == NULL)
8209 commit_id_str = GOT_REF_HEAD;
8210 error = got_repo_match_object_id(&commit_id, NULL,
8211 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8212 if (error)
8213 goto done;
8215 for (i = 0; i < argc; i++) {
8216 if (force_path) {
8217 error = got_object_id_by_path(&id, repo, commit_id,
8218 argv[i]);
8219 if (error)
8220 break;
8221 } else {
8222 error = got_repo_match_object_id(&id, &label, argv[i],
8223 GOT_OBJ_TYPE_ANY, 0, repo);
8224 if (error) {
8225 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8226 error->code != GOT_ERR_NOT_REF)
8227 break;
8228 error = got_object_id_by_path(&id, repo,
8229 commit_id, argv[i]);
8230 if (error)
8231 break;
8235 error = got_object_get_type(&obj_type, repo, id);
8236 if (error)
8237 break;
8239 switch (obj_type) {
8240 case GOT_OBJ_TYPE_BLOB:
8241 error = cat_blob(id, repo, stdout);
8242 break;
8243 case GOT_OBJ_TYPE_TREE:
8244 error = cat_tree(id, repo, stdout);
8245 break;
8246 case GOT_OBJ_TYPE_COMMIT:
8247 error = cat_commit(id, repo, stdout);
8248 break;
8249 case GOT_OBJ_TYPE_TAG:
8250 error = cat_tag(id, repo, stdout);
8251 break;
8252 default:
8253 error = got_error(GOT_ERR_OBJ_TYPE);
8254 break;
8256 if (error)
8257 break;
8258 free(label);
8259 label = NULL;
8260 free(id);
8261 id = NULL;
8263 done:
8264 free(label);
8265 free(id);
8266 free(commit_id);
8267 if (worktree)
8268 got_worktree_close(worktree);
8269 if (repo) {
8270 const struct got_error *repo_error;
8271 repo_error = got_repo_close(repo);
8272 if (error == NULL)
8273 error = repo_error;
8275 return error;