Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
39 #include <getopt.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_diff.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
62 static void
63 catch_sigint(int signo)
64 {
65 sigint_received = 1;
66 }
68 static void
69 catch_sigpipe(int signo)
70 {
71 sigpipe_received = 1;
72 }
75 struct got_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
158 };
160 static void
161 list_commands(void)
163 int i;
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
170 fputc('\n', stderr);
173 int
174 main(int argc, char *argv[])
176 struct got_cmd *cmd;
177 unsigned int i;
178 int ch;
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
182 { NULL, 0, NULL, 0}
183 };
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 switch (ch) {
189 case 'h':
190 hflag = 1;
191 break;
192 case 'V':
193 Vflag = 1;
194 break;
195 default:
196 usage(hflag);
197 /* NOTREACHED */
201 argc -= optind;
202 argv += optind;
203 optind = 0;
205 if (Vflag) {
206 got_version_print_str();
207 return 1;
210 if (argc <= 0)
211 usage(hflag);
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
223 continue;
225 if (hflag)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 !(sigint_received &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 return 1;
239 return 0;
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 list_commands();
244 return 1;
247 __dead static void
248 usage(int hflag)
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 getprogname());
252 if (hflag)
253 list_commands();
254 exit(1);
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
261 const char *editor;
263 *abspath = NULL;
265 editor = getenv("VISUAL");
266 if (editor == NULL)
267 editor = getenv("EDITOR");
269 if (editor) {
270 err = got_path_find_prog(abspath, editor);
271 if (err)
272 return err;
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
281 return NULL;
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
290 #ifdef PROFILE
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
293 #endif
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
304 if (err != NULL)
305 return err;
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
310 return NULL;
313 __dead static void
314 usage_init(void)
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 exit(1);
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
325 int ch;
327 while ((ch = getopt(argc, argv, "")) != -1) {
328 switch (ch) {
329 default:
330 usage_init();
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
338 #ifndef PROFILE
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 err(1, "pledge");
341 #endif
342 if (argc != 1)
343 usage_init();
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
352 if (error &&
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 goto done;
356 error = apply_unveil(repo_path, 0, NULL);
357 if (error)
358 goto done;
360 error = got_repo_init(repo_path);
361 if (error != NULL)
362 goto done;
364 done:
365 free(repo_path);
366 return error;
369 __dead static void
370 usage_import(void)
372 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
373 "[-r repository-path] [-I pattern] path\n", getprogname());
374 exit(1);
377 int
378 spawn_editor(const char *editor, const char *file)
380 pid_t pid;
381 sig_t sighup, sigint, sigquit;
382 int st = -1;
384 sighup = signal(SIGHUP, SIG_IGN);
385 sigint = signal(SIGINT, SIG_IGN);
386 sigquit = signal(SIGQUIT, SIG_IGN);
388 switch (pid = fork()) {
389 case -1:
390 goto doneediting;
391 case 0:
392 execl(editor, editor, file, (char *)NULL);
393 _exit(127);
396 while (waitpid(pid, &st, 0) == -1)
397 if (errno != EINTR)
398 break;
400 doneediting:
401 (void)signal(SIGHUP, sighup);
402 (void)signal(SIGINT, sigint);
403 (void)signal(SIGQUIT, sigquit);
405 if (!WIFEXITED(st)) {
406 errno = EINTR;
407 return -1;
410 return WEXITSTATUS(st);
413 static const struct got_error *
414 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
415 const char *initial_content)
417 const struct got_error *err = NULL;
418 char buf[1024];
419 struct stat st, st2;
420 FILE *fp;
421 int content_changed = 0;
422 size_t len;
424 *logmsg = NULL;
426 if (stat(logmsg_path, &st) == -1)
427 return got_error_from_errno2("stat", logmsg_path);
429 if (spawn_editor(editor, logmsg_path) == -1)
430 return got_error_from_errno("failed spawning editor");
432 if (stat(logmsg_path, &st2) == -1)
433 return got_error_from_errno("stat");
435 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
436 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 "no changes made to commit message, aborting");
439 *logmsg = malloc(st2.st_size + 1);
440 if (*logmsg == NULL)
441 return got_error_from_errno("malloc");
442 (*logmsg)[0] = '\0';
443 len = 0;
445 fp = fopen(logmsg_path, "r");
446 if (fp == NULL) {
447 err = got_error_from_errno("fopen");
448 goto done;
450 while (fgets(buf, sizeof(buf), fp) != NULL) {
451 if (!content_changed && strcmp(buf, initial_content) != 0)
452 content_changed = 1;
453 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
454 continue; /* remove comments and leading empty lines */
455 len = strlcat(*logmsg, buf, st2.st_size);
457 fclose(fp);
459 while (len > 0 && (*logmsg)[len - 1] == '\n') {
460 (*logmsg)[len - 1] = '\0';
461 len--;
464 if (len == 0 || !content_changed)
465 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
466 "commit message cannot be empty, aborting");
467 done:
468 if (err) {
469 free(*logmsg);
470 *logmsg = NULL;
472 return err;
475 static const struct got_error *
476 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
477 const char *path_dir, const char *branch_name)
479 char *initial_content = NULL;
480 const struct got_error *err = NULL;
481 int fd;
483 if (asprintf(&initial_content,
484 "\n# %s to be imported to branch %s\n", path_dir,
485 branch_name) == -1)
486 return got_error_from_errno("asprintf");
488 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
489 if (err)
490 goto done;
492 dprintf(fd, initial_content);
493 close(fd);
495 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
496 done:
497 free(initial_content);
498 return err;
501 static const struct got_error *
502 import_progress(void *arg, const char *path)
504 printf("A %s\n", path);
505 return NULL;
508 static const struct got_error *
509 get_author(char **author, struct got_repository *repo)
511 const struct got_error *err = NULL;
512 const char *got_author, *name, *email;
514 *author = NULL;
516 name = got_repo_get_gitconfig_author_name(repo);
517 email = got_repo_get_gitconfig_author_email(repo);
518 if (name && email) {
519 if (asprintf(author, "%s <%s>", name, email) == -1)
520 return got_error_from_errno("asprintf");
521 return NULL;
524 got_author = getenv("GOT_AUTHOR");
525 if (got_author == NULL) {
526 name = got_repo_get_global_gitconfig_author_name(repo);
527 email = got_repo_get_global_gitconfig_author_email(repo);
528 if (name && email) {
529 if (asprintf(author, "%s <%s>", name, email) == -1)
530 return got_error_from_errno("asprintf");
531 return NULL;
533 /* TODO: Look up user in password database? */
534 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 *author = strdup(got_author);
538 if (*author == NULL)
539 return got_error_from_errno("strdup");
541 /*
542 * Really dumb email address check; we're only doing this to
543 * avoid git's object parser breaking on commits we create.
544 */
545 while (*got_author && *got_author != '<')
546 got_author++;
547 if (*got_author != '<') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 goto done;
551 while (*got_author && *got_author != '@')
552 got_author++;
553 if (*got_author != '@') {
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 goto done;
557 while (*got_author && *got_author != '>')
558 got_author++;
559 if (*got_author != '>')
560 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
561 done:
562 if (err) {
563 free(*author);
564 *author = NULL;
566 return err;
569 static const struct got_error *
570 get_gitconfig_path(char **gitconfig_path)
572 const char *homedir = getenv("HOME");
574 *gitconfig_path = NULL;
575 if (homedir) {
576 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
577 return got_error_from_errno("asprintf");
580 return NULL;
583 static const struct got_error *
584 cmd_import(int argc, char *argv[])
586 const struct got_error *error = NULL;
587 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
588 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
589 const char *branch_name = "main";
590 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
591 struct got_repository *repo = NULL;
592 struct got_reference *branch_ref = NULL, *head_ref = NULL;
593 struct got_object_id *new_commit_id = NULL;
594 int ch;
595 struct got_pathlist_head ignores;
596 struct got_pathlist_entry *pe;
597 int preserve_logmsg = 0;
599 TAILQ_INIT(&ignores);
601 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
602 switch (ch) {
603 case 'b':
604 branch_name = optarg;
605 break;
606 case 'm':
607 logmsg = strdup(optarg);
608 if (logmsg == NULL) {
609 error = got_error_from_errno("strdup");
610 goto done;
612 break;
613 case 'r':
614 repo_path = realpath(optarg, NULL);
615 if (repo_path == NULL) {
616 error = got_error_from_errno2("realpath",
617 optarg);
618 goto done;
620 break;
621 case 'I':
622 if (optarg[0] == '\0')
623 break;
624 error = got_pathlist_insert(&pe, &ignores, optarg,
625 NULL);
626 if (error)
627 goto done;
628 break;
629 default:
630 usage_import();
631 /* NOTREACHED */
635 argc -= optind;
636 argv += optind;
638 #ifndef PROFILE
639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
640 "unveil",
641 NULL) == -1)
642 err(1, "pledge");
643 #endif
644 if (argc != 1)
645 usage_import();
647 if (repo_path == NULL) {
648 repo_path = getcwd(NULL, 0);
649 if (repo_path == NULL)
650 return got_error_from_errno("getcwd");
652 got_path_strip_trailing_slashes(repo_path);
653 error = get_gitconfig_path(&gitconfig_path);
654 if (error)
655 goto done;
656 error = got_repo_open(&repo, repo_path, gitconfig_path);
657 if (error)
658 goto done;
660 error = get_author(&author, repo);
661 if (error)
662 return error;
664 /*
665 * Don't let the user create a branch name with a leading '-'.
666 * While technically a valid reference name, this case is usually
667 * an unintended typo.
668 */
669 if (branch_name[0] == '-')
670 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
672 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
673 error = got_error_from_errno("asprintf");
674 goto done;
677 error = got_ref_open(&branch_ref, repo, refname, 0);
678 if (error) {
679 if (error->code != GOT_ERR_NOT_REF)
680 goto done;
681 } else {
682 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
683 "import target branch already exists");
684 goto done;
687 path_dir = realpath(argv[0], NULL);
688 if (path_dir == NULL) {
689 error = got_error_from_errno2("realpath", argv[0]);
690 goto done;
692 got_path_strip_trailing_slashes(path_dir);
694 /*
695 * unveil(2) traverses exec(2); if an editor is used we have
696 * to apply unveil after the log message has been written.
697 */
698 if (logmsg == NULL || strlen(logmsg) == 0) {
699 error = get_editor(&editor);
700 if (error)
701 goto done;
702 free(logmsg);
703 error = collect_import_msg(&logmsg, &logmsg_path, editor,
704 path_dir, refname);
705 if (error) {
706 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
707 logmsg_path != NULL)
708 preserve_logmsg = 1;
709 goto done;
713 if (unveil(path_dir, "r") != 0) {
714 error = got_error_from_errno2("unveil", path_dir);
715 if (logmsg_path)
716 preserve_logmsg = 1;
717 goto done;
720 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
721 if (error) {
722 if (logmsg_path)
723 preserve_logmsg = 1;
724 goto done;
727 error = got_repo_import(&new_commit_id, path_dir, logmsg,
728 author, &ignores, repo, import_progress, NULL);
729 if (error) {
730 if (logmsg_path)
731 preserve_logmsg = 1;
732 goto done;
735 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
736 if (error) {
737 if (logmsg_path)
738 preserve_logmsg = 1;
739 goto done;
742 error = got_ref_write(branch_ref, repo);
743 if (error) {
744 if (logmsg_path)
745 preserve_logmsg = 1;
746 goto done;
749 error = got_object_id_str(&id_str, new_commit_id);
750 if (error) {
751 if (logmsg_path)
752 preserve_logmsg = 1;
753 goto done;
756 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
757 if (error) {
758 if (error->code != GOT_ERR_NOT_REF) {
759 if (logmsg_path)
760 preserve_logmsg = 1;
761 goto done;
764 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
765 branch_ref);
766 if (error) {
767 if (logmsg_path)
768 preserve_logmsg = 1;
769 goto done;
772 error = got_ref_write(head_ref, repo);
773 if (error) {
774 if (logmsg_path)
775 preserve_logmsg = 1;
776 goto done;
780 printf("Created branch %s with commit %s\n",
781 got_ref_get_name(branch_ref), id_str);
782 done:
783 if (preserve_logmsg) {
784 fprintf(stderr, "%s: log message preserved in %s\n",
785 getprogname(), logmsg_path);
786 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
787 error = got_error_from_errno2("unlink", logmsg_path);
788 free(logmsg);
789 free(logmsg_path);
790 free(repo_path);
791 free(editor);
792 free(refname);
793 free(new_commit_id);
794 free(id_str);
795 free(author);
796 free(gitconfig_path);
797 if (branch_ref)
798 got_ref_close(branch_ref);
799 if (head_ref)
800 got_ref_close(head_ref);
801 return error;
804 __dead static void
805 usage_checkout(void)
807 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
808 "[-p prefix] repository-path [worktree-path]\n", getprogname());
809 exit(1);
812 static void
813 show_worktree_base_ref_warning(void)
815 fprintf(stderr, "%s: warning: could not create a reference "
816 "to the work tree's base commit; the commit could be "
817 "garbage-collected by Git; making the repository "
818 "writable and running 'got update' will prevent this\n",
819 getprogname());
822 struct got_checkout_progress_arg {
823 const char *worktree_path;
824 int had_base_commit_ref_error;
825 };
827 static const struct got_error *
828 checkout_progress(void *arg, unsigned char status, const char *path)
830 struct got_checkout_progress_arg *a = arg;
832 /* Base commit bump happens silently. */
833 if (status == GOT_STATUS_BUMP_BASE)
834 return NULL;
836 if (status == GOT_STATUS_BASE_REF_ERR) {
837 a->had_base_commit_ref_error = 1;
838 return NULL;
841 while (path[0] == '/')
842 path++;
844 printf("%c %s/%s\n", status, a->worktree_path, path);
845 return NULL;
848 static const struct got_error *
849 check_cancelled(void *arg)
851 if (sigint_received || sigpipe_received)
852 return got_error(GOT_ERR_CANCELLED);
853 return NULL;
856 static const struct got_error *
857 check_linear_ancestry(struct got_object_id *commit_id,
858 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
859 struct got_repository *repo)
861 const struct got_error *err = NULL;
862 struct got_object_id *yca_id;
864 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
865 commit_id, base_commit_id, repo, check_cancelled, NULL);
866 if (err)
867 return err;
869 if (yca_id == NULL)
870 return got_error(GOT_ERR_ANCESTRY);
872 /*
873 * Require a straight line of history between the target commit
874 * and the work tree's base commit.
876 * Non-linear situations such as this require a rebase:
878 * (commit) D F (base_commit)
879 * \ /
880 * C E
881 * \ /
882 * B (yca)
883 * |
884 * A
886 * 'got update' only handles linear cases:
887 * Update forwards in time: A (base/yca) - B - C - D (commit)
888 * Update backwards in time: D (base) - C - B - A (commit/yca)
889 */
890 if (allow_forwards_in_time_only) {
891 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
893 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
894 got_object_id_cmp(base_commit_id, yca_id) != 0)
895 return got_error(GOT_ERR_ANCESTRY);
897 free(yca_id);
898 return NULL;
901 static const struct got_error *
902 check_same_branch(struct got_object_id *commit_id,
903 struct got_reference *head_ref, struct got_object_id *yca_id,
904 struct got_repository *repo)
906 const struct got_error *err = NULL;
907 struct got_commit_graph *graph = NULL;
908 struct got_object_id *head_commit_id = NULL;
909 int is_same_branch = 0;
911 err = got_ref_resolve(&head_commit_id, repo, head_ref);
912 if (err)
913 goto done;
915 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
916 is_same_branch = 1;
917 goto done;
919 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
920 is_same_branch = 1;
921 goto done;
924 err = got_commit_graph_open(&graph, "/", 1);
925 if (err)
926 goto done;
928 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
929 check_cancelled, NULL);
930 if (err)
931 goto done;
933 for (;;) {
934 struct got_object_id *id;
935 err = got_commit_graph_iter_next(&id, graph, repo,
936 check_cancelled, NULL);
937 if (err) {
938 if (err->code == GOT_ERR_ITER_COMPLETED)
939 err = NULL;
940 break;
943 if (id) {
944 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
945 break;
946 if (got_object_id_cmp(id, commit_id) == 0) {
947 is_same_branch = 1;
948 break;
952 done:
953 if (graph)
954 got_commit_graph_close(graph);
955 free(head_commit_id);
956 if (!err && !is_same_branch)
957 err = got_error(GOT_ERR_ANCESTRY);
958 return err;
961 static const struct got_error *
962 resolve_commit_arg(struct got_object_id **commit_id,
963 const char *commit_id_arg, struct got_repository *repo)
965 const struct got_error *err;
966 struct got_reference *ref;
967 struct got_tag_object *tag;
969 err = got_repo_object_match_tag(&tag, commit_id_arg,
970 GOT_OBJ_TYPE_COMMIT, repo);
971 if (err == NULL) {
972 *commit_id = got_object_id_dup(
973 got_object_tag_get_object_id(tag));
974 if (*commit_id == NULL)
975 err = got_error_from_errno("got_object_id_dup");
976 got_object_tag_close(tag);
977 return err;
978 } else if (err->code != GOT_ERR_NO_OBJ)
979 return err;
981 err = got_ref_open(&ref, repo, commit_id_arg, 0);
982 if (err == NULL) {
983 err = got_ref_resolve(commit_id, repo, ref);
984 got_ref_close(ref);
985 } else {
986 if (err->code != GOT_ERR_NOT_REF)
987 return err;
988 err = got_repo_match_object_id_prefix(commit_id,
989 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
991 return err;
994 static const struct got_error *
995 cmd_checkout(int argc, char *argv[])
997 const struct got_error *error = NULL;
998 struct got_repository *repo = NULL;
999 struct got_reference *head_ref = NULL;
1000 struct got_worktree *worktree = NULL;
1001 char *repo_path = NULL;
1002 char *worktree_path = NULL;
1003 const char *path_prefix = "";
1004 const char *branch_name = GOT_REF_HEAD;
1005 char *commit_id_str = NULL;
1006 int ch, same_path_prefix, allow_nonempty = 0;
1007 struct got_pathlist_head paths;
1008 struct got_checkout_progress_arg cpa;
1010 TAILQ_INIT(&paths);
1012 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1013 switch (ch) {
1014 case 'b':
1015 branch_name = optarg;
1016 break;
1017 case 'c':
1018 commit_id_str = strdup(optarg);
1019 if (commit_id_str == NULL)
1020 return got_error_from_errno("strdup");
1021 break;
1022 case 'E':
1023 allow_nonempty = 1;
1024 break;
1025 case 'p':
1026 path_prefix = optarg;
1027 break;
1028 default:
1029 usage_checkout();
1030 /* NOTREACHED */
1034 argc -= optind;
1035 argv += optind;
1037 #ifndef PROFILE
1038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1039 "unveil", NULL) == -1)
1040 err(1, "pledge");
1041 #endif
1042 if (argc == 1) {
1043 char *cwd, *base, *dotgit;
1044 repo_path = realpath(argv[0], NULL);
1045 if (repo_path == NULL)
1046 return got_error_from_errno2("realpath", argv[0]);
1047 cwd = getcwd(NULL, 0);
1048 if (cwd == NULL) {
1049 error = got_error_from_errno("getcwd");
1050 goto done;
1052 if (path_prefix[0]) {
1053 base = basename(path_prefix);
1054 if (base == NULL) {
1055 error = got_error_from_errno2("basename",
1056 path_prefix);
1057 goto done;
1059 } else {
1060 base = basename(repo_path);
1061 if (base == NULL) {
1062 error = got_error_from_errno2("basename",
1063 repo_path);
1064 goto done;
1067 dotgit = strstr(base, ".git");
1068 if (dotgit)
1069 *dotgit = '\0';
1070 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1071 error = got_error_from_errno("asprintf");
1072 free(cwd);
1073 goto done;
1075 free(cwd);
1076 } else if (argc == 2) {
1077 repo_path = realpath(argv[0], NULL);
1078 if (repo_path == NULL) {
1079 error = got_error_from_errno2("realpath", argv[0]);
1080 goto done;
1082 worktree_path = realpath(argv[1], NULL);
1083 if (worktree_path == NULL) {
1084 if (errno != ENOENT) {
1085 error = got_error_from_errno2("realpath",
1086 argv[1]);
1087 goto done;
1089 worktree_path = strdup(argv[1]);
1090 if (worktree_path == NULL) {
1091 error = got_error_from_errno("strdup");
1092 goto done;
1095 } else
1096 usage_checkout();
1098 got_path_strip_trailing_slashes(repo_path);
1099 got_path_strip_trailing_slashes(worktree_path);
1101 error = got_repo_open(&repo, repo_path, NULL);
1102 if (error != NULL)
1103 goto done;
1105 /* Pre-create work tree path for unveil(2) */
1106 error = got_path_mkdir(worktree_path);
1107 if (error) {
1108 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1109 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1110 goto done;
1111 if (!allow_nonempty &&
1112 !got_path_dir_is_empty(worktree_path)) {
1113 error = got_error_path(worktree_path,
1114 GOT_ERR_DIR_NOT_EMPTY);
1115 goto done;
1119 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1120 if (error)
1121 goto done;
1123 error = got_ref_open(&head_ref, repo, branch_name, 0);
1124 if (error != NULL)
1125 goto done;
1127 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1128 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1129 goto done;
1131 error = got_worktree_open(&worktree, worktree_path);
1132 if (error != NULL)
1133 goto done;
1135 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1136 path_prefix);
1137 if (error != NULL)
1138 goto done;
1139 if (!same_path_prefix) {
1140 error = got_error(GOT_ERR_PATH_PREFIX);
1141 goto done;
1144 if (commit_id_str) {
1145 struct got_object_id *commit_id;
1146 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1147 if (error)
1148 goto done;
1149 error = check_linear_ancestry(commit_id,
1150 got_worktree_get_base_commit_id(worktree), 0, repo);
1151 if (error != NULL) {
1152 free(commit_id);
1153 goto done;
1155 error = check_same_branch(commit_id, head_ref, NULL, repo);
1156 if (error)
1157 goto done;
1158 error = got_worktree_set_base_commit_id(worktree, repo,
1159 commit_id);
1160 free(commit_id);
1161 if (error)
1162 goto done;
1165 error = got_pathlist_append(&paths, "", NULL);
1166 if (error)
1167 goto done;
1168 cpa.worktree_path = worktree_path;
1169 cpa.had_base_commit_ref_error = 0;
1170 error = got_worktree_checkout_files(worktree, &paths, repo,
1171 checkout_progress, &cpa, check_cancelled, NULL);
1172 if (error != NULL)
1173 goto done;
1175 printf("Now shut up and hack\n");
1176 if (cpa.had_base_commit_ref_error)
1177 show_worktree_base_ref_warning();
1179 done:
1180 got_pathlist_free(&paths);
1181 free(commit_id_str);
1182 free(repo_path);
1183 free(worktree_path);
1184 return error;
1187 __dead static void
1188 usage_update(void)
1190 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1191 getprogname());
1192 exit(1);
1195 static const struct got_error *
1196 update_progress(void *arg, unsigned char status, const char *path)
1198 int *did_something = arg;
1200 if (status == GOT_STATUS_EXISTS ||
1201 status == GOT_STATUS_BASE_REF_ERR)
1202 return NULL;
1204 *did_something = 1;
1206 /* Base commit bump happens silently. */
1207 if (status == GOT_STATUS_BUMP_BASE)
1208 return NULL;
1210 while (path[0] == '/')
1211 path++;
1212 printf("%c %s\n", status, path);
1213 return NULL;
1216 static const struct got_error *
1217 switch_head_ref(struct got_reference *head_ref,
1218 struct got_object_id *commit_id, struct got_worktree *worktree,
1219 struct got_repository *repo)
1221 const struct got_error *err = NULL;
1222 char *base_id_str;
1223 int ref_has_moved = 0;
1225 /* Trivial case: switching between two different references. */
1226 if (strcmp(got_ref_get_name(head_ref),
1227 got_worktree_get_head_ref_name(worktree)) != 0) {
1228 printf("Switching work tree from %s to %s\n",
1229 got_worktree_get_head_ref_name(worktree),
1230 got_ref_get_name(head_ref));
1231 return got_worktree_set_head_ref(worktree, head_ref);
1234 err = check_linear_ancestry(commit_id,
1235 got_worktree_get_base_commit_id(worktree), 0, repo);
1236 if (err) {
1237 if (err->code != GOT_ERR_ANCESTRY)
1238 return err;
1239 ref_has_moved = 1;
1241 if (!ref_has_moved)
1242 return NULL;
1244 /* Switching to a rebased branch with the same reference name. */
1245 err = got_object_id_str(&base_id_str,
1246 got_worktree_get_base_commit_id(worktree));
1247 if (err)
1248 return err;
1249 printf("Reference %s now points at a different branch\n",
1250 got_worktree_get_head_ref_name(worktree));
1251 printf("Switching work tree from %s to %s\n", base_id_str,
1252 got_worktree_get_head_ref_name(worktree));
1253 return NULL;
1256 static const struct got_error *
1257 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1259 const struct got_error *err;
1260 int in_progress;
1262 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1263 if (err)
1264 return err;
1265 if (in_progress)
1266 return got_error(GOT_ERR_REBASING);
1268 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1269 if (err)
1270 return err;
1271 if (in_progress)
1272 return got_error(GOT_ERR_HISTEDIT_BUSY);
1274 return NULL;
1277 static const struct got_error *
1278 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1279 char *argv[], struct got_worktree *worktree)
1281 const struct got_error *err = NULL;
1282 char *path;
1283 int i;
1285 if (argc == 0) {
1286 path = strdup("");
1287 if (path == NULL)
1288 return got_error_from_errno("strdup");
1289 return got_pathlist_append(paths, path, NULL);
1292 for (i = 0; i < argc; i++) {
1293 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1294 if (err)
1295 break;
1296 err = got_pathlist_append(paths, path, NULL);
1297 if (err) {
1298 free(path);
1299 break;
1303 return err;
1306 static const struct got_error *
1307 cmd_update(int argc, char *argv[])
1309 const struct got_error *error = NULL;
1310 struct got_repository *repo = NULL;
1311 struct got_worktree *worktree = NULL;
1312 char *worktree_path = NULL;
1313 struct got_object_id *commit_id = NULL;
1314 char *commit_id_str = NULL;
1315 const char *branch_name = NULL;
1316 struct got_reference *head_ref = NULL;
1317 struct got_pathlist_head paths;
1318 struct got_pathlist_entry *pe;
1319 int ch, did_something = 0;
1321 TAILQ_INIT(&paths);
1323 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1324 switch (ch) {
1325 case 'b':
1326 branch_name = optarg;
1327 break;
1328 case 'c':
1329 commit_id_str = strdup(optarg);
1330 if (commit_id_str == NULL)
1331 return got_error_from_errno("strdup");
1332 break;
1333 default:
1334 usage_update();
1335 /* NOTREACHED */
1339 argc -= optind;
1340 argv += optind;
1342 #ifndef PROFILE
1343 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1344 "unveil", NULL) == -1)
1345 err(1, "pledge");
1346 #endif
1347 worktree_path = getcwd(NULL, 0);
1348 if (worktree_path == NULL) {
1349 error = got_error_from_errno("getcwd");
1350 goto done;
1352 error = got_worktree_open(&worktree, worktree_path);
1353 if (error)
1354 goto done;
1356 error = check_rebase_or_histedit_in_progress(worktree);
1357 if (error)
1358 goto done;
1360 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1361 NULL);
1362 if (error != NULL)
1363 goto done;
1365 error = apply_unveil(got_repo_get_path(repo), 0,
1366 got_worktree_get_root_path(worktree));
1367 if (error)
1368 goto done;
1370 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1371 if (error)
1372 goto done;
1374 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1375 got_worktree_get_head_ref_name(worktree), 0);
1376 if (error != NULL)
1377 goto done;
1378 if (commit_id_str == NULL) {
1379 error = got_ref_resolve(&commit_id, repo, head_ref);
1380 if (error != NULL)
1381 goto done;
1382 error = got_object_id_str(&commit_id_str, commit_id);
1383 if (error != NULL)
1384 goto done;
1385 } else {
1386 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1387 free(commit_id_str);
1388 commit_id_str = NULL;
1389 if (error)
1390 goto done;
1391 error = got_object_id_str(&commit_id_str, commit_id);
1392 if (error)
1393 goto done;
1396 if (branch_name) {
1397 struct got_object_id *head_commit_id;
1398 TAILQ_FOREACH(pe, &paths, entry) {
1399 if (pe->path_len == 0)
1400 continue;
1401 error = got_error_msg(GOT_ERR_BAD_PATH,
1402 "switching between branches requires that "
1403 "the entire work tree gets updated");
1404 goto done;
1406 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1407 if (error)
1408 goto done;
1409 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1410 repo);
1411 free(head_commit_id);
1412 if (error != NULL)
1413 goto done;
1414 error = check_same_branch(commit_id, head_ref, NULL, repo);
1415 if (error)
1416 goto done;
1417 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1418 if (error)
1419 goto done;
1420 } else {
1421 error = check_linear_ancestry(commit_id,
1422 got_worktree_get_base_commit_id(worktree), 0, repo);
1423 if (error != NULL) {
1424 if (error->code == GOT_ERR_ANCESTRY)
1425 error = got_error(GOT_ERR_BRANCH_MOVED);
1426 goto done;
1428 error = check_same_branch(commit_id, head_ref, NULL, repo);
1429 if (error)
1430 goto done;
1433 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1434 commit_id) != 0) {
1435 error = got_worktree_set_base_commit_id(worktree, repo,
1436 commit_id);
1437 if (error)
1438 goto done;
1441 error = got_worktree_checkout_files(worktree, &paths, repo,
1442 update_progress, &did_something, check_cancelled, NULL);
1443 if (error != NULL)
1444 goto done;
1446 if (did_something)
1447 printf("Updated to commit %s\n", commit_id_str);
1448 else
1449 printf("Already up-to-date\n");
1450 done:
1451 free(worktree_path);
1452 TAILQ_FOREACH(pe, &paths, entry)
1453 free((char *)pe->path);
1454 got_pathlist_free(&paths);
1455 free(commit_id);
1456 free(commit_id_str);
1457 return error;
1460 static const struct got_error *
1461 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1462 const char *path, int diff_context, int ignore_whitespace,
1463 struct got_repository *repo)
1465 const struct got_error *err = NULL;
1466 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1468 if (blob_id1) {
1469 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1470 if (err)
1471 goto done;
1474 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1475 if (err)
1476 goto done;
1478 while (path[0] == '/')
1479 path++;
1480 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1481 ignore_whitespace, stdout);
1482 done:
1483 if (blob1)
1484 got_object_blob_close(blob1);
1485 got_object_blob_close(blob2);
1486 return err;
1489 static const struct got_error *
1490 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1491 const char *path, int diff_context, int ignore_whitespace,
1492 struct got_repository *repo)
1494 const struct got_error *err = NULL;
1495 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1496 struct got_diff_blob_output_unidiff_arg arg;
1498 if (tree_id1) {
1499 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1500 if (err)
1501 goto done;
1504 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1505 if (err)
1506 goto done;
1508 arg.diff_context = diff_context;
1509 arg.ignore_whitespace = ignore_whitespace;
1510 arg.outfile = stdout;
1511 while (path[0] == '/')
1512 path++;
1513 err = got_diff_tree(tree1, tree2, path, path, repo,
1514 got_diff_blob_output_unidiff, &arg, 1);
1515 done:
1516 if (tree1)
1517 got_object_tree_close(tree1);
1518 if (tree2)
1519 got_object_tree_close(tree2);
1520 return err;
1523 static const struct got_error *
1524 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1525 const char *path, int diff_context, struct got_repository *repo)
1527 const struct got_error *err = NULL;
1528 struct got_commit_object *pcommit = NULL;
1529 char *id_str1 = NULL, *id_str2 = NULL;
1530 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1531 struct got_object_qid *qid;
1533 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1534 if (qid != NULL) {
1535 err = got_object_open_as_commit(&pcommit, repo,
1536 qid->id);
1537 if (err)
1538 return err;
1541 if (path && path[0] != '\0') {
1542 int obj_type;
1543 err = got_object_id_by_path(&obj_id2, repo, id, path);
1544 if (err)
1545 goto done;
1546 err = got_object_id_str(&id_str2, obj_id2);
1547 if (err) {
1548 free(obj_id2);
1549 goto done;
1551 if (pcommit) {
1552 err = got_object_id_by_path(&obj_id1, repo,
1553 qid->id, path);
1554 if (err) {
1555 free(obj_id2);
1556 goto done;
1558 err = got_object_id_str(&id_str1, obj_id1);
1559 if (err) {
1560 free(obj_id2);
1561 goto done;
1564 err = got_object_get_type(&obj_type, repo, obj_id2);
1565 if (err) {
1566 free(obj_id2);
1567 goto done;
1569 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1570 switch (obj_type) {
1571 case GOT_OBJ_TYPE_BLOB:
1572 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1573 0, repo);
1574 break;
1575 case GOT_OBJ_TYPE_TREE:
1576 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1577 0, repo);
1578 break;
1579 default:
1580 err = got_error(GOT_ERR_OBJ_TYPE);
1581 break;
1583 free(obj_id1);
1584 free(obj_id2);
1585 } else {
1586 obj_id2 = got_object_commit_get_tree_id(commit);
1587 err = got_object_id_str(&id_str2, obj_id2);
1588 if (err)
1589 goto done;
1590 obj_id1 = got_object_commit_get_tree_id(pcommit);
1591 err = got_object_id_str(&id_str1, obj_id1);
1592 if (err)
1593 goto done;
1594 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1595 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1598 done:
1599 free(id_str1);
1600 free(id_str2);
1601 if (pcommit)
1602 got_object_commit_close(pcommit);
1603 return err;
1606 static char *
1607 get_datestr(time_t *time, char *datebuf)
1609 struct tm mytm, *tm;
1610 char *p, *s;
1612 tm = gmtime_r(time, &mytm);
1613 if (tm == NULL)
1614 return NULL;
1615 s = asctime_r(tm, datebuf);
1616 if (s == NULL)
1617 return NULL;
1618 p = strchr(s, '\n');
1619 if (p)
1620 *p = '\0';
1621 return s;
1624 static const struct got_error *
1625 match_logmsg(int *have_match, struct got_object_id *id,
1626 struct got_commit_object *commit, regex_t *regex)
1628 const struct got_error *err = NULL;
1629 regmatch_t regmatch;
1630 char *id_str = NULL, *logmsg = NULL;
1632 *have_match = 0;
1634 err = got_object_id_str(&id_str, id);
1635 if (err)
1636 return err;
1638 err = got_object_commit_get_logmsg(&logmsg, commit);
1639 if (err)
1640 goto done;
1642 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1643 *have_match = 1;
1644 done:
1645 free(id_str);
1646 free(logmsg);
1647 return err;
1650 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1652 static const struct got_error *
1653 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1654 struct got_repository *repo, const char *path, int show_patch,
1655 int diff_context, struct got_reflist_head *refs)
1657 const struct got_error *err = NULL;
1658 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1659 char datebuf[26];
1660 time_t committer_time;
1661 const char *author, *committer;
1662 char *refs_str = NULL;
1663 struct got_reflist_entry *re;
1665 SIMPLEQ_FOREACH(re, refs, entry) {
1666 char *s;
1667 const char *name;
1668 struct got_tag_object *tag = NULL;
1669 int cmp;
1671 name = got_ref_get_name(re->ref);
1672 if (strcmp(name, GOT_REF_HEAD) == 0)
1673 continue;
1674 if (strncmp(name, "refs/", 5) == 0)
1675 name += 5;
1676 if (strncmp(name, "got/", 4) == 0)
1677 continue;
1678 if (strncmp(name, "heads/", 6) == 0)
1679 name += 6;
1680 if (strncmp(name, "remotes/", 8) == 0)
1681 name += 8;
1682 if (strncmp(name, "tags/", 5) == 0) {
1683 err = got_object_open_as_tag(&tag, repo, re->id);
1684 if (err) {
1685 if (err->code != GOT_ERR_OBJ_TYPE)
1686 return err;
1687 /* Ref points at something other than a tag. */
1688 err = NULL;
1689 tag = NULL;
1692 cmp = got_object_id_cmp(tag ?
1693 got_object_tag_get_object_id(tag) : re->id, id);
1694 if (tag)
1695 got_object_tag_close(tag);
1696 if (cmp != 0)
1697 continue;
1698 s = refs_str;
1699 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1700 name) == -1) {
1701 err = got_error_from_errno("asprintf");
1702 free(s);
1703 return err;
1705 free(s);
1707 err = got_object_id_str(&id_str, id);
1708 if (err)
1709 return err;
1711 printf(GOT_COMMIT_SEP_STR);
1712 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1713 refs_str ? refs_str : "", refs_str ? ")" : "");
1714 free(id_str);
1715 id_str = NULL;
1716 free(refs_str);
1717 refs_str = NULL;
1718 printf("from: %s\n", got_object_commit_get_author(commit));
1719 committer_time = got_object_commit_get_committer_time(commit);
1720 datestr = get_datestr(&committer_time, datebuf);
1721 if (datestr)
1722 printf("date: %s UTC\n", datestr);
1723 author = got_object_commit_get_author(commit);
1724 committer = got_object_commit_get_committer(commit);
1725 if (strcmp(author, committer) != 0)
1726 printf("via: %s\n", committer);
1727 if (got_object_commit_get_nparents(commit) > 1) {
1728 const struct got_object_id_queue *parent_ids;
1729 struct got_object_qid *qid;
1730 int n = 1;
1731 parent_ids = got_object_commit_get_parent_ids(commit);
1732 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1733 err = got_object_id_str(&id_str, qid->id);
1734 if (err)
1735 return err;
1736 printf("parent %d: %s\n", n++, id_str);
1737 free(id_str);
1741 err = got_object_commit_get_logmsg(&logmsg0, commit);
1742 if (err)
1743 return err;
1745 logmsg = logmsg0;
1746 do {
1747 line = strsep(&logmsg, "\n");
1748 if (line)
1749 printf(" %s\n", line);
1750 } while (line);
1751 free(logmsg0);
1753 if (show_patch) {
1754 err = print_patch(commit, id, path, diff_context, repo);
1755 if (err == 0)
1756 printf("\n");
1759 if (fflush(stdout) != 0 && err == NULL)
1760 err = got_error_from_errno("fflush");
1761 return err;
1764 static const struct got_error *
1765 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1766 const char *path, int show_patch, const char *search_pattern,
1767 int diff_context, int limit, int first_parent_traversal,
1768 struct got_reflist_head *refs)
1770 const struct got_error *err;
1771 struct got_commit_graph *graph;
1772 regex_t regex;
1773 int have_match;
1775 if (search_pattern &&
1776 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1777 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1779 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1780 if (err)
1781 return err;
1782 err = got_commit_graph_iter_start(graph, root_id, repo,
1783 check_cancelled, NULL);
1784 if (err)
1785 goto done;
1786 for (;;) {
1787 struct got_commit_object *commit;
1788 struct got_object_id *id;
1790 if (sigint_received || sigpipe_received)
1791 break;
1793 err = got_commit_graph_iter_next(&id, graph, repo,
1794 check_cancelled, NULL);
1795 if (err) {
1796 if (err->code == GOT_ERR_ITER_COMPLETED)
1797 err = NULL;
1798 break;
1800 if (id == NULL)
1801 break;
1803 err = got_object_open_as_commit(&commit, repo, id);
1804 if (err)
1805 break;
1807 if (search_pattern) {
1808 err = match_logmsg(&have_match, id, commit, &regex);
1809 if (err) {
1810 got_object_commit_close(commit);
1811 break;
1813 if (have_match == 0) {
1814 got_object_commit_close(commit);
1815 continue;
1819 err = print_commit(commit, id, repo, path, show_patch,
1820 diff_context, refs);
1821 got_object_commit_close(commit);
1822 if (err || (limit && --limit == 0))
1823 break;
1825 done:
1826 if (search_pattern)
1827 regfree(&regex);
1828 got_commit_graph_close(graph);
1829 return err;
1832 __dead static void
1833 usage_log(void)
1835 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1836 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1837 exit(1);
1840 static int
1841 get_default_log_limit(void)
1843 const char *got_default_log_limit;
1844 long long n;
1845 const char *errstr;
1847 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1848 if (got_default_log_limit == NULL)
1849 return 0;
1850 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1851 if (errstr != NULL)
1852 return 0;
1853 return n;
1856 static const struct got_error *
1857 cmd_log(int argc, char *argv[])
1859 const struct got_error *error;
1860 struct got_repository *repo = NULL;
1861 struct got_worktree *worktree = NULL;
1862 struct got_commit_object *commit = NULL;
1863 struct got_object_id *id = NULL;
1864 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1865 const char *start_commit = NULL, *search_pattern = NULL;
1866 int diff_context = -1, ch;
1867 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1868 const char *errstr;
1869 struct got_reflist_head refs;
1871 SIMPLEQ_INIT(&refs);
1873 #ifndef PROFILE
1874 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1875 NULL)
1876 == -1)
1877 err(1, "pledge");
1878 #endif
1880 limit = get_default_log_limit();
1882 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1883 switch (ch) {
1884 case 'p':
1885 show_patch = 1;
1886 break;
1887 case 'c':
1888 start_commit = optarg;
1889 break;
1890 case 'C':
1891 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1892 &errstr);
1893 if (errstr != NULL)
1894 err(1, "-C option %s", errstr);
1895 break;
1896 case 'l':
1897 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1898 if (errstr != NULL)
1899 err(1, "-l option %s", errstr);
1900 break;
1901 case 'f':
1902 first_parent_traversal = 1;
1903 break;
1904 case 'r':
1905 repo_path = realpath(optarg, NULL);
1906 if (repo_path == NULL)
1907 return got_error_from_errno2("realpath",
1908 optarg);
1909 got_path_strip_trailing_slashes(repo_path);
1910 break;
1911 case 's':
1912 search_pattern = optarg;
1913 break;
1914 default:
1915 usage_log();
1916 /* NOTREACHED */
1920 argc -= optind;
1921 argv += optind;
1923 if (diff_context == -1)
1924 diff_context = 3;
1925 else if (!show_patch)
1926 errx(1, "-C reguires -p");
1928 cwd = getcwd(NULL, 0);
1929 if (cwd == NULL) {
1930 error = got_error_from_errno("getcwd");
1931 goto done;
1934 error = got_worktree_open(&worktree, cwd);
1935 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1936 goto done;
1937 error = NULL;
1939 if (argc == 0) {
1940 path = strdup("");
1941 if (path == NULL) {
1942 error = got_error_from_errno("strdup");
1943 goto done;
1945 } else if (argc == 1) {
1946 if (worktree) {
1947 error = got_worktree_resolve_path(&path, worktree,
1948 argv[0]);
1949 if (error)
1950 goto done;
1951 } else {
1952 path = strdup(argv[0]);
1953 if (path == NULL) {
1954 error = got_error_from_errno("strdup");
1955 goto done;
1958 } else
1959 usage_log();
1961 if (repo_path == NULL) {
1962 repo_path = worktree ?
1963 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1965 if (repo_path == NULL) {
1966 error = got_error_from_errno("strdup");
1967 goto done;
1970 error = got_repo_open(&repo, repo_path, NULL);
1971 if (error != NULL)
1972 goto done;
1974 error = apply_unveil(got_repo_get_path(repo), 1,
1975 worktree ? got_worktree_get_root_path(worktree) : NULL);
1976 if (error)
1977 goto done;
1979 if (start_commit == NULL) {
1980 struct got_reference *head_ref;
1981 error = got_ref_open(&head_ref, repo,
1982 worktree ? got_worktree_get_head_ref_name(worktree)
1983 : GOT_REF_HEAD, 0);
1984 if (error != NULL)
1985 return error;
1986 error = got_ref_resolve(&id, repo, head_ref);
1987 got_ref_close(head_ref);
1988 if (error != NULL)
1989 return error;
1990 error = got_object_open_as_commit(&commit, repo, id);
1991 } else {
1992 struct got_reference *ref;
1993 error = got_ref_open(&ref, repo, start_commit, 0);
1994 if (error == NULL) {
1995 int obj_type;
1996 error = got_ref_resolve(&id, repo, ref);
1997 got_ref_close(ref);
1998 if (error != NULL)
1999 goto done;
2000 error = got_object_get_type(&obj_type, repo, id);
2001 if (error != NULL)
2002 goto done;
2003 if (obj_type == GOT_OBJ_TYPE_TAG) {
2004 struct got_tag_object *tag;
2005 error = got_object_open_as_tag(&tag, repo, id);
2006 if (error != NULL)
2007 goto done;
2008 if (got_object_tag_get_object_type(tag) !=
2009 GOT_OBJ_TYPE_COMMIT) {
2010 got_object_tag_close(tag);
2011 error = got_error(GOT_ERR_OBJ_TYPE);
2012 goto done;
2014 free(id);
2015 id = got_object_id_dup(
2016 got_object_tag_get_object_id(tag));
2017 if (id == NULL)
2018 error = got_error_from_errno(
2019 "got_object_id_dup");
2020 got_object_tag_close(tag);
2021 if (error)
2022 goto done;
2023 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2024 error = got_error(GOT_ERR_OBJ_TYPE);
2025 goto done;
2027 error = got_object_open_as_commit(&commit, repo, id);
2028 if (error != NULL)
2029 goto done;
2031 if (commit == NULL) {
2032 error = got_repo_match_object_id_prefix(&id,
2033 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2034 if (error != NULL)
2035 return error;
2038 if (error != NULL)
2039 goto done;
2041 if (worktree) {
2042 const char *prefix = got_worktree_get_path_prefix(worktree);
2043 char *p;
2044 if (asprintf(&p, "%s%s%s", prefix,
2045 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2046 error = got_error_from_errno("asprintf");
2047 goto done;
2049 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2050 free(p);
2051 } else
2052 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2053 if (error != NULL)
2054 goto done;
2055 if (in_repo_path) {
2056 free(path);
2057 path = in_repo_path;
2060 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2061 if (error)
2062 goto done;
2064 error = print_commits(id, repo, path, show_patch, search_pattern,
2065 diff_context, limit, first_parent_traversal, &refs);
2066 done:
2067 free(path);
2068 free(repo_path);
2069 free(cwd);
2070 free(id);
2071 if (worktree)
2072 got_worktree_close(worktree);
2073 if (repo) {
2074 const struct got_error *repo_error;
2075 repo_error = got_repo_close(repo);
2076 if (error == NULL)
2077 error = repo_error;
2079 got_ref_list_free(&refs);
2080 return error;
2083 __dead static void
2084 usage_diff(void)
2086 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2087 "[-w] [object1 object2 | path]\n", getprogname());
2088 exit(1);
2091 struct print_diff_arg {
2092 struct got_repository *repo;
2093 struct got_worktree *worktree;
2094 int diff_context;
2095 const char *id_str;
2096 int header_shown;
2097 int diff_staged;
2098 int ignore_whitespace;
2101 static const struct got_error *
2102 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2103 const char *path, struct got_object_id *blob_id,
2104 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2105 int dirfd, const char *de_name)
2107 struct print_diff_arg *a = arg;
2108 const struct got_error *err = NULL;
2109 struct got_blob_object *blob1 = NULL;
2110 int fd = -1;
2111 FILE *f2 = NULL;
2112 char *abspath = NULL, *label1 = NULL;
2113 struct stat sb;
2115 if (a->diff_staged) {
2116 if (staged_status != GOT_STATUS_MODIFY &&
2117 staged_status != GOT_STATUS_ADD &&
2118 staged_status != GOT_STATUS_DELETE)
2119 return NULL;
2120 } else {
2121 if (staged_status == GOT_STATUS_DELETE)
2122 return NULL;
2123 if (status == GOT_STATUS_NONEXISTENT)
2124 return got_error_set_errno(ENOENT, path);
2125 if (status != GOT_STATUS_MODIFY &&
2126 status != GOT_STATUS_ADD &&
2127 status != GOT_STATUS_DELETE &&
2128 status != GOT_STATUS_CONFLICT)
2129 return NULL;
2132 if (!a->header_shown) {
2133 printf("diff %s %s%s\n", a->id_str,
2134 got_worktree_get_root_path(a->worktree),
2135 a->diff_staged ? " (staged changes)" : "");
2136 a->header_shown = 1;
2139 if (a->diff_staged) {
2140 const char *label1 = NULL, *label2 = NULL;
2141 switch (staged_status) {
2142 case GOT_STATUS_MODIFY:
2143 label1 = path;
2144 label2 = path;
2145 break;
2146 case GOT_STATUS_ADD:
2147 label2 = path;
2148 break;
2149 case GOT_STATUS_DELETE:
2150 label1 = path;
2151 break;
2152 default:
2153 return got_error(GOT_ERR_FILE_STATUS);
2155 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2156 label1, label2, a->diff_context, a->ignore_whitespace,
2157 a->repo, stdout);
2160 if (staged_status == GOT_STATUS_ADD ||
2161 staged_status == GOT_STATUS_MODIFY) {
2162 char *id_str;
2163 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2164 8192);
2165 if (err)
2166 goto done;
2167 err = got_object_id_str(&id_str, staged_blob_id);
2168 if (err)
2169 goto done;
2170 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2171 err = got_error_from_errno("asprintf");
2172 free(id_str);
2173 goto done;
2175 free(id_str);
2176 } else if (status != GOT_STATUS_ADD) {
2177 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2178 if (err)
2179 goto done;
2182 if (status != GOT_STATUS_DELETE) {
2183 if (asprintf(&abspath, "%s/%s",
2184 got_worktree_get_root_path(a->worktree), path) == -1) {
2185 err = got_error_from_errno("asprintf");
2186 goto done;
2189 if (dirfd != -1) {
2190 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2191 if (fd == -1) {
2192 err = got_error_from_errno2("openat", abspath);
2193 goto done;
2195 } else {
2196 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2197 if (fd == -1) {
2198 err = got_error_from_errno2("open", abspath);
2199 goto done;
2202 if (fstat(fd, &sb) == -1) {
2203 err = got_error_from_errno2("fstat", abspath);
2204 goto done;
2206 f2 = fdopen(fd, "r");
2207 if (f2 == NULL) {
2208 err = got_error_from_errno2("fdopen", abspath);
2209 goto done;
2211 fd = -1;
2212 } else
2213 sb.st_size = 0;
2215 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2216 a->diff_context, a->ignore_whitespace, stdout);
2217 done:
2218 if (blob1)
2219 got_object_blob_close(blob1);
2220 if (f2 && fclose(f2) == EOF && err == NULL)
2221 err = got_error_from_errno("fclose");
2222 if (fd != -1 && close(fd) == -1 && err == NULL)
2223 err = got_error_from_errno("close");
2224 free(abspath);
2225 return err;
2228 static const struct got_error *
2229 match_object_id(struct got_object_id **id, char **label,
2230 const char *id_str, int obj_type, int resolve_tags,
2231 struct got_repository *repo)
2233 const struct got_error *err;
2234 struct got_tag_object *tag;
2235 struct got_reference *ref = NULL;
2237 *id = NULL;
2238 *label = NULL;
2240 if (resolve_tags) {
2241 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2242 repo);
2243 if (err == NULL) {
2244 *id = got_object_id_dup(
2245 got_object_tag_get_object_id(tag));
2246 if (*id == NULL)
2247 err = got_error_from_errno("got_object_id_dup");
2248 else if (asprintf(label, "refs/tags/%s",
2249 got_object_tag_get_name(tag)) == -1) {
2250 err = got_error_from_errno("asprintf");
2251 free(*id);
2252 *id = NULL;
2254 got_object_tag_close(tag);
2255 return err;
2256 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2257 err->code != GOT_ERR_NO_OBJ)
2258 return err;
2261 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2262 if (err) {
2263 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2264 return err;
2265 err = got_ref_open(&ref, repo, id_str, 0);
2266 if (err != NULL)
2267 goto done;
2268 *label = strdup(got_ref_get_name(ref));
2269 if (*label == NULL) {
2270 err = got_error_from_errno("strdup");
2271 goto done;
2273 err = got_ref_resolve(id, repo, ref);
2274 } else {
2275 err = got_object_id_str(label, *id);
2276 if (*label == NULL) {
2277 err = got_error_from_errno("strdup");
2278 goto done;
2281 done:
2282 if (ref)
2283 got_ref_close(ref);
2284 return err;
2288 static const struct got_error *
2289 cmd_diff(int argc, char *argv[])
2291 const struct got_error *error;
2292 struct got_repository *repo = NULL;
2293 struct got_worktree *worktree = NULL;
2294 char *cwd = NULL, *repo_path = NULL;
2295 struct got_object_id *id1 = NULL, *id2 = NULL;
2296 const char *id_str1 = NULL, *id_str2 = NULL;
2297 char *label1 = NULL, *label2 = NULL;
2298 int type1, type2;
2299 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2300 const char *errstr;
2301 char *path = NULL;
2303 #ifndef PROFILE
2304 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2305 NULL) == -1)
2306 err(1, "pledge");
2307 #endif
2309 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2310 switch (ch) {
2311 case 'C':
2312 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2313 &errstr);
2314 if (errstr != NULL)
2315 err(1, "-C option %s", errstr);
2316 break;
2317 case 'r':
2318 repo_path = realpath(optarg, NULL);
2319 if (repo_path == NULL)
2320 return got_error_from_errno2("realpath",
2321 optarg);
2322 got_path_strip_trailing_slashes(repo_path);
2323 break;
2324 case 's':
2325 diff_staged = 1;
2326 break;
2327 case 'w':
2328 ignore_whitespace = 1;
2329 break;
2330 default:
2331 usage_diff();
2332 /* NOTREACHED */
2336 argc -= optind;
2337 argv += optind;
2339 cwd = getcwd(NULL, 0);
2340 if (cwd == NULL) {
2341 error = got_error_from_errno("getcwd");
2342 goto done;
2344 error = got_worktree_open(&worktree, cwd);
2345 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2346 goto done;
2347 if (argc <= 1) {
2348 if (worktree == NULL) {
2349 error = got_error(GOT_ERR_NOT_WORKTREE);
2350 goto done;
2352 if (repo_path)
2353 errx(1,
2354 "-r option can't be used when diffing a work tree");
2355 repo_path = strdup(got_worktree_get_repo_path(worktree));
2356 if (repo_path == NULL) {
2357 error = got_error_from_errno("strdup");
2358 goto done;
2360 if (argc == 1) {
2361 error = got_worktree_resolve_path(&path, worktree,
2362 argv[0]);
2363 if (error)
2364 goto done;
2365 } else {
2366 path = strdup("");
2367 if (path == NULL) {
2368 error = got_error_from_errno("strdup");
2369 goto done;
2372 } else if (argc == 2) {
2373 if (diff_staged)
2374 errx(1, "-s option can't be used when diffing "
2375 "objects in repository");
2376 id_str1 = argv[0];
2377 id_str2 = argv[1];
2378 if (worktree && repo_path == NULL) {
2379 repo_path =
2380 strdup(got_worktree_get_repo_path(worktree));
2381 if (repo_path == NULL) {
2382 error = got_error_from_errno("strdup");
2383 goto done;
2386 } else
2387 usage_diff();
2389 if (repo_path == NULL) {
2390 repo_path = getcwd(NULL, 0);
2391 if (repo_path == NULL)
2392 return got_error_from_errno("getcwd");
2395 error = got_repo_open(&repo, repo_path, NULL);
2396 free(repo_path);
2397 if (error != NULL)
2398 goto done;
2400 error = apply_unveil(got_repo_get_path(repo), 1,
2401 worktree ? got_worktree_get_root_path(worktree) : NULL);
2402 if (error)
2403 goto done;
2405 if (argc <= 1) {
2406 struct print_diff_arg arg;
2407 struct got_pathlist_head paths;
2408 char *id_str;
2410 TAILQ_INIT(&paths);
2412 error = got_object_id_str(&id_str,
2413 got_worktree_get_base_commit_id(worktree));
2414 if (error)
2415 goto done;
2416 arg.repo = repo;
2417 arg.worktree = worktree;
2418 arg.diff_context = diff_context;
2419 arg.id_str = id_str;
2420 arg.header_shown = 0;
2421 arg.diff_staged = diff_staged;
2422 arg.ignore_whitespace = ignore_whitespace;
2424 error = got_pathlist_append(&paths, path, NULL);
2425 if (error)
2426 goto done;
2428 error = got_worktree_status(worktree, &paths, repo, print_diff,
2429 &arg, check_cancelled, NULL);
2430 free(id_str);
2431 got_pathlist_free(&paths);
2432 goto done;
2435 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2436 repo);
2437 if (error)
2438 goto done;
2440 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2441 repo);
2442 if (error)
2443 goto done;
2445 error = got_object_get_type(&type1, repo, id1);
2446 if (error)
2447 goto done;
2449 error = got_object_get_type(&type2, repo, id2);
2450 if (error)
2451 goto done;
2453 if (type1 != type2) {
2454 error = got_error(GOT_ERR_OBJ_TYPE);
2455 goto done;
2458 switch (type1) {
2459 case GOT_OBJ_TYPE_BLOB:
2460 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2461 diff_context, ignore_whitespace, repo, stdout);
2462 break;
2463 case GOT_OBJ_TYPE_TREE:
2464 error = got_diff_objects_as_trees(id1, id2, "", "",
2465 diff_context, ignore_whitespace, repo, stdout);
2466 break;
2467 case GOT_OBJ_TYPE_COMMIT:
2468 printf("diff %s %s\n", label1, label2);
2469 error = got_diff_objects_as_commits(id1, id2, diff_context,
2470 ignore_whitespace, repo, stdout);
2471 break;
2472 default:
2473 error = got_error(GOT_ERR_OBJ_TYPE);
2476 done:
2477 free(label1);
2478 free(label2);
2479 free(id1);
2480 free(id2);
2481 free(path);
2482 if (worktree)
2483 got_worktree_close(worktree);
2484 if (repo) {
2485 const struct got_error *repo_error;
2486 repo_error = got_repo_close(repo);
2487 if (error == NULL)
2488 error = repo_error;
2490 return error;
2493 __dead static void
2494 usage_blame(void)
2496 fprintf(stderr,
2497 "usage: %s blame [-c commit] [-r repository-path] path\n",
2498 getprogname());
2499 exit(1);
2502 struct blame_line {
2503 int annotated;
2504 char *id_str;
2505 char *committer;
2506 char datebuf[11]; /* YYYY-MM-DD + NUL */
2509 struct blame_cb_args {
2510 struct blame_line *lines;
2511 int nlines;
2512 int nlines_prec;
2513 int lineno_cur;
2514 off_t *line_offsets;
2515 FILE *f;
2516 struct got_repository *repo;
2519 static const struct got_error *
2520 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2522 const struct got_error *err = NULL;
2523 struct blame_cb_args *a = arg;
2524 struct blame_line *bline;
2525 char *line = NULL;
2526 size_t linesize = 0;
2527 struct got_commit_object *commit = NULL;
2528 off_t offset;
2529 struct tm tm;
2530 time_t committer_time;
2532 if (nlines != a->nlines ||
2533 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2534 return got_error(GOT_ERR_RANGE);
2536 if (sigint_received)
2537 return got_error(GOT_ERR_ITER_COMPLETED);
2539 if (lineno == -1)
2540 return NULL; /* no change in this commit */
2542 /* Annotate this line. */
2543 bline = &a->lines[lineno - 1];
2544 if (bline->annotated)
2545 return NULL;
2546 err = got_object_id_str(&bline->id_str, id);
2547 if (err)
2548 return err;
2550 err = got_object_open_as_commit(&commit, a->repo, id);
2551 if (err)
2552 goto done;
2554 bline->committer = strdup(got_object_commit_get_committer(commit));
2555 if (bline->committer == NULL) {
2556 err = got_error_from_errno("strdup");
2557 goto done;
2560 committer_time = got_object_commit_get_committer_time(commit);
2561 if (localtime_r(&committer_time, &tm) == NULL)
2562 return got_error_from_errno("localtime_r");
2563 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2564 &tm) >= sizeof(bline->datebuf)) {
2565 err = got_error(GOT_ERR_NO_SPACE);
2566 goto done;
2568 bline->annotated = 1;
2570 /* Print lines annotated so far. */
2571 bline = &a->lines[a->lineno_cur - 1];
2572 if (!bline->annotated)
2573 goto done;
2575 offset = a->line_offsets[a->lineno_cur - 1];
2576 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2577 err = got_error_from_errno("fseeko");
2578 goto done;
2581 while (bline->annotated) {
2582 char *smallerthan, *at, *nl, *committer;
2583 size_t len;
2585 if (getline(&line, &linesize, a->f) == -1) {
2586 if (ferror(a->f))
2587 err = got_error_from_errno("getline");
2588 break;
2591 committer = bline->committer;
2592 smallerthan = strchr(committer, '<');
2593 if (smallerthan && smallerthan[1] != '\0')
2594 committer = smallerthan + 1;
2595 at = strchr(committer, '@');
2596 if (at)
2597 *at = '\0';
2598 len = strlen(committer);
2599 if (len >= 9)
2600 committer[8] = '\0';
2602 nl = strchr(line, '\n');
2603 if (nl)
2604 *nl = '\0';
2605 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2606 bline->id_str, bline->datebuf, committer, line);
2608 a->lineno_cur++;
2609 bline = &a->lines[a->lineno_cur - 1];
2611 done:
2612 if (commit)
2613 got_object_commit_close(commit);
2614 free(line);
2615 return err;
2618 static const struct got_error *
2619 cmd_blame(int argc, char *argv[])
2621 const struct got_error *error;
2622 struct got_repository *repo = NULL;
2623 struct got_worktree *worktree = NULL;
2624 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2625 struct got_object_id *obj_id = NULL;
2626 struct got_object_id *commit_id = NULL;
2627 struct got_blob_object *blob = NULL;
2628 char *commit_id_str = NULL;
2629 struct blame_cb_args bca;
2630 int ch, obj_type, i;
2631 size_t filesize;
2633 memset(&bca, 0, sizeof(bca));
2635 #ifndef PROFILE
2636 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2637 NULL) == -1)
2638 err(1, "pledge");
2639 #endif
2641 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2642 switch (ch) {
2643 case 'c':
2644 commit_id_str = optarg;
2645 break;
2646 case 'r':
2647 repo_path = realpath(optarg, NULL);
2648 if (repo_path == NULL)
2649 return got_error_from_errno2("realpath",
2650 optarg);
2651 got_path_strip_trailing_slashes(repo_path);
2652 break;
2653 default:
2654 usage_blame();
2655 /* NOTREACHED */
2659 argc -= optind;
2660 argv += optind;
2662 if (argc == 1)
2663 path = argv[0];
2664 else
2665 usage_blame();
2667 cwd = getcwd(NULL, 0);
2668 if (cwd == NULL) {
2669 error = got_error_from_errno("getcwd");
2670 goto done;
2672 if (repo_path == NULL) {
2673 error = got_worktree_open(&worktree, cwd);
2674 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2675 goto done;
2676 else
2677 error = NULL;
2678 if (worktree) {
2679 repo_path =
2680 strdup(got_worktree_get_repo_path(worktree));
2681 if (repo_path == NULL)
2682 error = got_error_from_errno("strdup");
2683 if (error)
2684 goto done;
2685 } else {
2686 repo_path = strdup(cwd);
2687 if (repo_path == NULL) {
2688 error = got_error_from_errno("strdup");
2689 goto done;
2694 error = got_repo_open(&repo, repo_path, NULL);
2695 if (error != NULL)
2696 goto done;
2698 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2699 if (error)
2700 goto done;
2702 if (worktree) {
2703 const char *prefix = got_worktree_get_path_prefix(worktree);
2704 char *p, *worktree_subdir = cwd +
2705 strlen(got_worktree_get_root_path(worktree));
2706 if (asprintf(&p, "%s%s%s%s%s",
2707 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2708 worktree_subdir, worktree_subdir[0] ? "/" : "",
2709 path) == -1) {
2710 error = got_error_from_errno("asprintf");
2711 goto done;
2713 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2714 free(p);
2715 } else {
2716 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2718 if (error)
2719 goto done;
2721 if (commit_id_str == NULL) {
2722 struct got_reference *head_ref;
2723 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2724 if (error != NULL)
2725 goto done;
2726 error = got_ref_resolve(&commit_id, repo, head_ref);
2727 got_ref_close(head_ref);
2728 if (error != NULL)
2729 goto done;
2730 } else {
2731 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2732 if (error)
2733 goto done;
2736 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2737 if (error)
2738 goto done;
2739 if (obj_id == NULL) {
2740 error = got_error(GOT_ERR_NO_OBJ);
2741 goto done;
2744 error = got_object_get_type(&obj_type, repo, obj_id);
2745 if (error)
2746 goto done;
2748 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2749 error = got_error(GOT_ERR_OBJ_TYPE);
2750 goto done;
2753 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2754 if (error)
2755 goto done;
2756 bca.f = got_opentemp();
2757 if (bca.f == NULL) {
2758 error = got_error_from_errno("got_opentemp");
2759 goto done;
2761 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2762 &bca.line_offsets, bca.f, blob);
2763 if (error || bca.nlines == 0)
2764 goto done;
2766 /* Don't include \n at EOF in the blame line count. */
2767 if (bca.line_offsets[bca.nlines - 1] == filesize)
2768 bca.nlines--;
2770 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2771 if (bca.lines == NULL) {
2772 error = got_error_from_errno("calloc");
2773 goto done;
2775 bca.lineno_cur = 1;
2776 bca.nlines_prec = 0;
2777 i = bca.nlines;
2778 while (i > 0) {
2779 i /= 10;
2780 bca.nlines_prec++;
2782 bca.repo = repo;
2784 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2785 check_cancelled, NULL);
2786 if (error)
2787 goto done;
2788 done:
2789 free(in_repo_path);
2790 free(repo_path);
2791 free(cwd);
2792 free(commit_id);
2793 free(obj_id);
2794 if (blob)
2795 got_object_blob_close(blob);
2796 if (worktree)
2797 got_worktree_close(worktree);
2798 if (repo) {
2799 const struct got_error *repo_error;
2800 repo_error = got_repo_close(repo);
2801 if (error == NULL)
2802 error = repo_error;
2804 if (bca.lines) {
2805 for (i = 0; i < bca.nlines; i++) {
2806 struct blame_line *bline = &bca.lines[i];
2807 free(bline->id_str);
2808 free(bline->committer);
2810 free(bca.lines);
2812 free(bca.line_offsets);
2813 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2814 error = got_error_from_errno("fclose");
2815 return error;
2818 __dead static void
2819 usage_tree(void)
2821 fprintf(stderr,
2822 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2823 getprogname());
2824 exit(1);
2827 static void
2828 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2829 const char *root_path)
2831 int is_root_path = (strcmp(path, root_path) == 0);
2832 const char *modestr = "";
2833 mode_t mode = got_tree_entry_get_mode(te);
2835 path += strlen(root_path);
2836 while (path[0] == '/')
2837 path++;
2839 if (got_object_tree_entry_is_submodule(te))
2840 modestr = "$";
2841 else if (S_ISLNK(mode))
2842 modestr = "@";
2843 else if (S_ISDIR(mode))
2844 modestr = "/";
2845 else if (mode & S_IXUSR)
2846 modestr = "*";
2848 printf("%s%s%s%s%s\n", id ? id : "", path,
2849 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2852 static const struct got_error *
2853 print_tree(const char *path, struct got_object_id *commit_id,
2854 int show_ids, int recurse, const char *root_path,
2855 struct got_repository *repo)
2857 const struct got_error *err = NULL;
2858 struct got_object_id *tree_id = NULL;
2859 struct got_tree_object *tree = NULL;
2860 int nentries, i;
2862 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2863 if (err)
2864 goto done;
2866 err = got_object_open_as_tree(&tree, repo, tree_id);
2867 if (err)
2868 goto done;
2869 nentries = got_object_tree_get_nentries(tree);
2870 for (i = 0; i < nentries; i++) {
2871 struct got_tree_entry *te;
2872 char *id = NULL;
2874 if (sigint_received || sigpipe_received)
2875 break;
2877 te = got_object_tree_get_entry(tree, i);
2878 if (show_ids) {
2879 char *id_str;
2880 err = got_object_id_str(&id_str,
2881 got_tree_entry_get_id(te));
2882 if (err)
2883 goto done;
2884 if (asprintf(&id, "%s ", id_str) == -1) {
2885 err = got_error_from_errno("asprintf");
2886 free(id_str);
2887 goto done;
2889 free(id_str);
2891 print_entry(te, id, path, root_path);
2892 free(id);
2894 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2895 char *child_path;
2896 if (asprintf(&child_path, "%s%s%s", path,
2897 path[0] == '/' && path[1] == '\0' ? "" : "/",
2898 got_tree_entry_get_name(te)) == -1) {
2899 err = got_error_from_errno("asprintf");
2900 goto done;
2902 err = print_tree(child_path, commit_id, show_ids, 1,
2903 root_path, repo);
2904 free(child_path);
2905 if (err)
2906 goto done;
2909 done:
2910 if (tree)
2911 got_object_tree_close(tree);
2912 free(tree_id);
2913 return err;
2916 static const struct got_error *
2917 cmd_tree(int argc, char *argv[])
2919 const struct got_error *error;
2920 struct got_repository *repo = NULL;
2921 struct got_worktree *worktree = NULL;
2922 const char *path;
2923 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2924 struct got_object_id *commit_id = NULL;
2925 char *commit_id_str = NULL;
2926 int show_ids = 0, recurse = 0;
2927 int ch;
2929 #ifndef PROFILE
2930 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2931 NULL) == -1)
2932 err(1, "pledge");
2933 #endif
2935 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2936 switch (ch) {
2937 case 'c':
2938 commit_id_str = optarg;
2939 break;
2940 case 'r':
2941 repo_path = realpath(optarg, NULL);
2942 if (repo_path == NULL)
2943 return got_error_from_errno2("realpath",
2944 optarg);
2945 got_path_strip_trailing_slashes(repo_path);
2946 break;
2947 case 'i':
2948 show_ids = 1;
2949 break;
2950 case 'R':
2951 recurse = 1;
2952 break;
2953 default:
2954 usage_tree();
2955 /* NOTREACHED */
2959 argc -= optind;
2960 argv += optind;
2962 if (argc == 1)
2963 path = argv[0];
2964 else if (argc > 1)
2965 usage_tree();
2966 else
2967 path = NULL;
2969 cwd = getcwd(NULL, 0);
2970 if (cwd == NULL) {
2971 error = got_error_from_errno("getcwd");
2972 goto done;
2974 if (repo_path == NULL) {
2975 error = got_worktree_open(&worktree, cwd);
2976 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2977 goto done;
2978 else
2979 error = NULL;
2980 if (worktree) {
2981 repo_path =
2982 strdup(got_worktree_get_repo_path(worktree));
2983 if (repo_path == NULL)
2984 error = got_error_from_errno("strdup");
2985 if (error)
2986 goto done;
2987 } else {
2988 repo_path = strdup(cwd);
2989 if (repo_path == NULL) {
2990 error = got_error_from_errno("strdup");
2991 goto done;
2996 error = got_repo_open(&repo, repo_path, NULL);
2997 if (error != NULL)
2998 goto done;
3000 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3001 if (error)
3002 goto done;
3004 if (path == NULL) {
3005 if (worktree) {
3006 char *p, *worktree_subdir = cwd +
3007 strlen(got_worktree_get_root_path(worktree));
3008 if (asprintf(&p, "%s/%s",
3009 got_worktree_get_path_prefix(worktree),
3010 worktree_subdir) == -1) {
3011 error = got_error_from_errno("asprintf");
3012 goto done;
3014 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3015 free(p);
3016 if (error)
3017 goto done;
3018 } else
3019 path = "/";
3021 if (in_repo_path == NULL) {
3022 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3023 if (error != NULL)
3024 goto done;
3027 if (commit_id_str == NULL) {
3028 struct got_reference *head_ref;
3029 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3030 if (error != NULL)
3031 goto done;
3032 error = got_ref_resolve(&commit_id, repo, head_ref);
3033 got_ref_close(head_ref);
3034 if (error != NULL)
3035 goto done;
3036 } else {
3037 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3038 if (error)
3039 goto done;
3042 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3043 in_repo_path, repo);
3044 done:
3045 free(in_repo_path);
3046 free(repo_path);
3047 free(cwd);
3048 free(commit_id);
3049 if (worktree)
3050 got_worktree_close(worktree);
3051 if (repo) {
3052 const struct got_error *repo_error;
3053 repo_error = got_repo_close(repo);
3054 if (error == NULL)
3055 error = repo_error;
3057 return error;
3060 __dead static void
3061 usage_status(void)
3063 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3064 exit(1);
3067 static const struct got_error *
3068 print_status(void *arg, unsigned char status, unsigned char staged_status,
3069 const char *path, struct got_object_id *blob_id,
3070 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3071 int dirfd, const char *de_name)
3073 if (status == staged_status && (status == GOT_STATUS_DELETE))
3074 status = GOT_STATUS_NO_CHANGE;
3075 printf("%c%c %s\n", status, staged_status, path);
3076 return NULL;
3079 static const struct got_error *
3080 cmd_status(int argc, char *argv[])
3082 const struct got_error *error = NULL;
3083 struct got_repository *repo = NULL;
3084 struct got_worktree *worktree = NULL;
3085 char *cwd = NULL;
3086 struct got_pathlist_head paths;
3087 struct got_pathlist_entry *pe;
3088 int ch;
3090 TAILQ_INIT(&paths);
3092 while ((ch = getopt(argc, argv, "")) != -1) {
3093 switch (ch) {
3094 default:
3095 usage_status();
3096 /* NOTREACHED */
3100 argc -= optind;
3101 argv += optind;
3103 #ifndef PROFILE
3104 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3105 NULL) == -1)
3106 err(1, "pledge");
3107 #endif
3108 cwd = getcwd(NULL, 0);
3109 if (cwd == NULL) {
3110 error = got_error_from_errno("getcwd");
3111 goto done;
3114 error = got_worktree_open(&worktree, cwd);
3115 if (error != NULL)
3116 goto done;
3118 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3119 NULL);
3120 if (error != NULL)
3121 goto done;
3123 error = apply_unveil(got_repo_get_path(repo), 1,
3124 got_worktree_get_root_path(worktree));
3125 if (error)
3126 goto done;
3128 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3129 if (error)
3130 goto done;
3132 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3133 check_cancelled, NULL);
3134 done:
3135 TAILQ_FOREACH(pe, &paths, entry)
3136 free((char *)pe->path);
3137 got_pathlist_free(&paths);
3138 free(cwd);
3139 return error;
3142 __dead static void
3143 usage_ref(void)
3145 fprintf(stderr,
3146 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3147 getprogname());
3148 exit(1);
3151 static const struct got_error *
3152 list_refs(struct got_repository *repo)
3154 static const struct got_error *err = NULL;
3155 struct got_reflist_head refs;
3156 struct got_reflist_entry *re;
3158 SIMPLEQ_INIT(&refs);
3159 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3160 if (err)
3161 return err;
3163 SIMPLEQ_FOREACH(re, &refs, entry) {
3164 char *refstr;
3165 refstr = got_ref_to_str(re->ref);
3166 if (refstr == NULL)
3167 return got_error_from_errno("got_ref_to_str");
3168 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3169 free(refstr);
3172 got_ref_list_free(&refs);
3173 return NULL;
3176 static const struct got_error *
3177 delete_ref(struct got_repository *repo, const char *refname)
3179 const struct got_error *err = NULL;
3180 struct got_reference *ref;
3182 err = got_ref_open(&ref, repo, refname, 0);
3183 if (err)
3184 return err;
3186 err = got_ref_delete(ref, repo);
3187 got_ref_close(ref);
3188 return err;
3191 static const struct got_error *
3192 add_ref(struct got_repository *repo, const char *refname, const char *target)
3194 const struct got_error *err = NULL;
3195 struct got_object_id *id;
3196 struct got_reference *ref = NULL;
3199 * Don't let the user create a reference name with a leading '-'.
3200 * While technically a valid reference name, this case is usually
3201 * an unintended typo.
3203 if (refname[0] == '-')
3204 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3206 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3207 repo);
3208 if (err) {
3209 struct got_reference *target_ref;
3211 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3212 return err;
3213 err = got_ref_open(&target_ref, repo, target, 0);
3214 if (err)
3215 return err;
3216 err = got_ref_resolve(&id, repo, target_ref);
3217 got_ref_close(target_ref);
3218 if (err)
3219 return err;
3222 err = got_ref_alloc(&ref, refname, id);
3223 if (err)
3224 goto done;
3226 err = got_ref_write(ref, repo);
3227 done:
3228 if (ref)
3229 got_ref_close(ref);
3230 free(id);
3231 return err;
3234 static const struct got_error *
3235 add_symref(struct got_repository *repo, const char *refname, const char *target)
3237 const struct got_error *err = NULL;
3238 struct got_reference *ref = NULL;
3239 struct got_reference *target_ref = NULL;
3242 * Don't let the user create a reference name with a leading '-'.
3243 * While technically a valid reference name, this case is usually
3244 * an unintended typo.
3246 if (refname[0] == '-')
3247 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3249 err = got_ref_open(&target_ref, repo, target, 0);
3250 if (err)
3251 return err;
3253 err = got_ref_alloc_symref(&ref, refname, target_ref);
3254 if (err)
3255 goto done;
3257 err = got_ref_write(ref, repo);
3258 done:
3259 if (target_ref)
3260 got_ref_close(target_ref);
3261 if (ref)
3262 got_ref_close(ref);
3263 return err;
3266 static const struct got_error *
3267 cmd_ref(int argc, char *argv[])
3269 const struct got_error *error = NULL;
3270 struct got_repository *repo = NULL;
3271 struct got_worktree *worktree = NULL;
3272 char *cwd = NULL, *repo_path = NULL;
3273 int ch, do_list = 0, create_symref = 0;
3274 const char *delref = NULL;
3276 /* TODO: Add -s option for adding symbolic references. */
3277 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3278 switch (ch) {
3279 case 'd':
3280 delref = optarg;
3281 break;
3282 case 'r':
3283 repo_path = realpath(optarg, NULL);
3284 if (repo_path == NULL)
3285 return got_error_from_errno2("realpath",
3286 optarg);
3287 got_path_strip_trailing_slashes(repo_path);
3288 break;
3289 case 'l':
3290 do_list = 1;
3291 break;
3292 case 's':
3293 create_symref = 1;
3294 break;
3295 default:
3296 usage_ref();
3297 /* NOTREACHED */
3301 if (do_list && delref)
3302 errx(1, "-l and -d options are mutually exclusive\n");
3304 argc -= optind;
3305 argv += optind;
3307 if (do_list || delref) {
3308 if (create_symref)
3309 errx(1, "-s option cannot be used together with the "
3310 "-l or -d options");
3311 if (argc > 0)
3312 usage_ref();
3313 } else if (argc != 2)
3314 usage_ref();
3316 #ifndef PROFILE
3317 if (do_list) {
3318 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3319 NULL) == -1)
3320 err(1, "pledge");
3321 } else {
3322 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3323 "sendfd unveil", NULL) == -1)
3324 err(1, "pledge");
3326 #endif
3327 cwd = getcwd(NULL, 0);
3328 if (cwd == NULL) {
3329 error = got_error_from_errno("getcwd");
3330 goto done;
3333 if (repo_path == NULL) {
3334 error = got_worktree_open(&worktree, cwd);
3335 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3336 goto done;
3337 else
3338 error = NULL;
3339 if (worktree) {
3340 repo_path =
3341 strdup(got_worktree_get_repo_path(worktree));
3342 if (repo_path == NULL)
3343 error = got_error_from_errno("strdup");
3344 if (error)
3345 goto done;
3346 } else {
3347 repo_path = strdup(cwd);
3348 if (repo_path == NULL) {
3349 error = got_error_from_errno("strdup");
3350 goto done;
3355 error = got_repo_open(&repo, repo_path, NULL);
3356 if (error != NULL)
3357 goto done;
3359 error = apply_unveil(got_repo_get_path(repo), do_list,
3360 worktree ? got_worktree_get_root_path(worktree) : NULL);
3361 if (error)
3362 goto done;
3364 if (do_list)
3365 error = list_refs(repo);
3366 else if (delref)
3367 error = delete_ref(repo, delref);
3368 else if (create_symref)
3369 error = add_symref(repo, argv[0], argv[1]);
3370 else
3371 error = add_ref(repo, argv[0], argv[1]);
3372 done:
3373 if (repo)
3374 got_repo_close(repo);
3375 if (worktree)
3376 got_worktree_close(worktree);
3377 free(cwd);
3378 free(repo_path);
3379 return error;
3382 __dead static void
3383 usage_branch(void)
3385 fprintf(stderr,
3386 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3387 "[name]\n", getprogname());
3388 exit(1);
3391 static const struct got_error *
3392 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3393 struct got_reference *ref)
3395 const struct got_error *err = NULL;
3396 const char *refname, *marker = " ";
3397 char *refstr;
3399 refname = got_ref_get_name(ref);
3400 if (worktree && strcmp(refname,
3401 got_worktree_get_head_ref_name(worktree)) == 0) {
3402 struct got_object_id *id = NULL;
3404 err = got_ref_resolve(&id, repo, ref);
3405 if (err)
3406 return err;
3407 if (got_object_id_cmp(id,
3408 got_worktree_get_base_commit_id(worktree)) == 0)
3409 marker = "* ";
3410 else
3411 marker = "~ ";
3412 free(id);
3415 if (strncmp(refname, "refs/heads/", 11) == 0)
3416 refname += 11;
3417 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3418 refname += 18;
3420 refstr = got_ref_to_str(ref);
3421 if (refstr == NULL)
3422 return got_error_from_errno("got_ref_to_str");
3424 printf("%s%s: %s\n", marker, refname, refstr);
3425 free(refstr);
3426 return NULL;
3429 static const struct got_error *
3430 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3432 const char *refname;
3434 if (worktree == NULL)
3435 return got_error(GOT_ERR_NOT_WORKTREE);
3437 refname = got_worktree_get_head_ref_name(worktree);
3439 if (strncmp(refname, "refs/heads/", 11) == 0)
3440 refname += 11;
3441 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3442 refname += 18;
3444 printf("%s\n", refname);
3446 return NULL;
3449 static const struct got_error *
3450 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3452 static const struct got_error *err = NULL;
3453 struct got_reflist_head refs;
3454 struct got_reflist_entry *re;
3455 struct got_reference *temp_ref = NULL;
3456 int rebase_in_progress, histedit_in_progress;
3458 SIMPLEQ_INIT(&refs);
3460 if (worktree) {
3461 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3462 worktree);
3463 if (err)
3464 return err;
3466 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3467 worktree);
3468 if (err)
3469 return err;
3471 if (rebase_in_progress || histedit_in_progress) {
3472 err = got_ref_open(&temp_ref, repo,
3473 got_worktree_get_head_ref_name(worktree), 0);
3474 if (err)
3475 return err;
3476 list_branch(repo, worktree, temp_ref);
3477 got_ref_close(temp_ref);
3481 err = got_ref_list(&refs, repo, "refs/heads",
3482 got_ref_cmp_by_name, NULL);
3483 if (err)
3484 return err;
3486 SIMPLEQ_FOREACH(re, &refs, entry)
3487 list_branch(repo, worktree, re->ref);
3489 got_ref_list_free(&refs);
3490 return NULL;
3493 static const struct got_error *
3494 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3495 const char *branch_name)
3497 const struct got_error *err = NULL;
3498 struct got_reference *ref = NULL;
3499 char *refname;
3501 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3502 return got_error_from_errno("asprintf");
3504 err = got_ref_open(&ref, repo, refname, 0);
3505 if (err)
3506 goto done;
3508 if (worktree &&
3509 strcmp(got_worktree_get_head_ref_name(worktree),
3510 got_ref_get_name(ref)) == 0) {
3511 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3512 "will not delete this work tree's current branch");
3513 goto done;
3516 err = got_ref_delete(ref, repo);
3517 done:
3518 if (ref)
3519 got_ref_close(ref);
3520 free(refname);
3521 return err;
3524 static const struct got_error *
3525 add_branch(struct got_repository *repo, const char *branch_name,
3526 struct got_object_id *base_commit_id)
3528 const struct got_error *err = NULL;
3529 struct got_reference *ref = NULL;
3530 char *base_refname = NULL, *refname = NULL;
3533 * Don't let the user create a branch name with a leading '-'.
3534 * While technically a valid reference name, this case is usually
3535 * an unintended typo.
3537 if (branch_name[0] == '-')
3538 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3540 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3541 err = got_error_from_errno("asprintf");
3542 goto done;
3545 err = got_ref_open(&ref, repo, refname, 0);
3546 if (err == NULL) {
3547 err = got_error(GOT_ERR_BRANCH_EXISTS);
3548 goto done;
3549 } else if (err->code != GOT_ERR_NOT_REF)
3550 goto done;
3552 err = got_ref_alloc(&ref, refname, base_commit_id);
3553 if (err)
3554 goto done;
3556 err = got_ref_write(ref, repo);
3557 done:
3558 if (ref)
3559 got_ref_close(ref);
3560 free(base_refname);
3561 free(refname);
3562 return err;
3565 static const struct got_error *
3566 cmd_branch(int argc, char *argv[])
3568 const struct got_error *error = NULL;
3569 struct got_repository *repo = NULL;
3570 struct got_worktree *worktree = NULL;
3571 char *cwd = NULL, *repo_path = NULL;
3572 int ch, do_list = 0, do_show = 0;
3573 const char *delref = NULL, *commit_id_arg = NULL;
3575 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3576 switch (ch) {
3577 case 'c':
3578 commit_id_arg = optarg;
3579 break;
3580 case 'd':
3581 delref = optarg;
3582 break;
3583 case 'r':
3584 repo_path = realpath(optarg, NULL);
3585 if (repo_path == NULL)
3586 return got_error_from_errno2("realpath",
3587 optarg);
3588 got_path_strip_trailing_slashes(repo_path);
3589 break;
3590 case 'l':
3591 do_list = 1;
3592 break;
3593 default:
3594 usage_branch();
3595 /* NOTREACHED */
3599 if (do_list && delref)
3600 errx(1, "-l and -d options are mutually exclusive\n");
3602 argc -= optind;
3603 argv += optind;
3605 if (!do_list && !delref && argc == 0)
3606 do_show = 1;
3608 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3609 errx(1, "-c option can only be used when creating a branch");
3611 if (do_list || delref) {
3612 if (argc > 0)
3613 usage_branch();
3614 } else if (!do_show && argc != 1)
3615 usage_branch();
3617 #ifndef PROFILE
3618 if (do_list || do_show) {
3619 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3620 NULL) == -1)
3621 err(1, "pledge");
3622 } else {
3623 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3624 "sendfd unveil", NULL) == -1)
3625 err(1, "pledge");
3627 #endif
3628 cwd = getcwd(NULL, 0);
3629 if (cwd == NULL) {
3630 error = got_error_from_errno("getcwd");
3631 goto done;
3634 if (repo_path == NULL) {
3635 error = got_worktree_open(&worktree, cwd);
3636 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3637 goto done;
3638 else
3639 error = NULL;
3640 if (worktree) {
3641 repo_path =
3642 strdup(got_worktree_get_repo_path(worktree));
3643 if (repo_path == NULL)
3644 error = got_error_from_errno("strdup");
3645 if (error)
3646 goto done;
3647 } else {
3648 repo_path = strdup(cwd);
3649 if (repo_path == NULL) {
3650 error = got_error_from_errno("strdup");
3651 goto done;
3656 error = got_repo_open(&repo, repo_path, NULL);
3657 if (error != NULL)
3658 goto done;
3660 error = apply_unveil(got_repo_get_path(repo), do_list,
3661 worktree ? got_worktree_get_root_path(worktree) : NULL);
3662 if (error)
3663 goto done;
3665 if (do_show)
3666 error = show_current_branch(repo, worktree);
3667 else if (do_list)
3668 error = list_branches(repo, worktree);
3669 else if (delref)
3670 error = delete_branch(repo, worktree, delref);
3671 else {
3672 struct got_object_id *commit_id;
3673 if (commit_id_arg == NULL)
3674 commit_id_arg = worktree ?
3675 got_worktree_get_head_ref_name(worktree) :
3676 GOT_REF_HEAD;
3677 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3678 if (error)
3679 goto done;
3680 error = add_branch(repo, argv[0], commit_id);
3681 free(commit_id);
3683 done:
3684 if (repo)
3685 got_repo_close(repo);
3686 if (worktree)
3687 got_worktree_close(worktree);
3688 free(cwd);
3689 free(repo_path);
3690 return error;
3694 __dead static void
3695 usage_tag(void)
3697 fprintf(stderr,
3698 "usage: %s tag [-r repository] | -l | "
3699 "[-m message] name [commit]\n", getprogname());
3700 exit(1);
3703 #if 0
3704 static const struct got_error *
3705 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3707 const struct got_error *err = NULL;
3708 struct got_reflist_entry *re, *se, *new;
3709 struct got_object_id *re_id, *se_id;
3710 struct got_tag_object *re_tag, *se_tag;
3711 time_t re_time, se_time;
3713 SIMPLEQ_FOREACH(re, tags, entry) {
3714 se = SIMPLEQ_FIRST(sorted);
3715 if (se == NULL) {
3716 err = got_reflist_entry_dup(&new, re);
3717 if (err)
3718 return err;
3719 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3720 continue;
3721 } else {
3722 err = got_ref_resolve(&re_id, repo, re->ref);
3723 if (err)
3724 break;
3725 err = got_object_open_as_tag(&re_tag, repo, re_id);
3726 free(re_id);
3727 if (err)
3728 break;
3729 re_time = got_object_tag_get_tagger_time(re_tag);
3730 got_object_tag_close(re_tag);
3733 while (se) {
3734 err = got_ref_resolve(&se_id, repo, re->ref);
3735 if (err)
3736 break;
3737 err = got_object_open_as_tag(&se_tag, repo, se_id);
3738 free(se_id);
3739 if (err)
3740 break;
3741 se_time = got_object_tag_get_tagger_time(se_tag);
3742 got_object_tag_close(se_tag);
3744 if (se_time > re_time) {
3745 err = got_reflist_entry_dup(&new, re);
3746 if (err)
3747 return err;
3748 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3749 break;
3751 se = SIMPLEQ_NEXT(se, entry);
3752 continue;
3755 done:
3756 return err;
3758 #endif
3760 static const struct got_error *
3761 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3762 struct got_reference *ref2)
3764 const struct got_error *err = NULL;
3765 struct got_repository *repo = arg;
3766 struct got_object_id *id1, *id2 = NULL;
3767 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3768 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3769 time_t time1, time2;
3771 *cmp = 0;
3773 err = got_ref_resolve(&id1, repo, ref1);
3774 if (err)
3775 return err;
3776 err = got_object_open_as_tag(&tag1, repo, id1);
3777 if (err) {
3778 if (err->code != GOT_ERR_OBJ_TYPE)
3779 goto done;
3780 /* "lightweight" tag */
3781 err = got_object_open_as_commit(&commit1, repo, id1);
3782 if (err)
3783 goto done;
3784 time1 = got_object_commit_get_committer_time(commit1);
3785 } else
3786 time1 = got_object_tag_get_tagger_time(tag1);
3788 err = got_ref_resolve(&id2, repo, ref2);
3789 if (err)
3790 goto done;
3791 err = got_object_open_as_tag(&tag2, repo, id2);
3792 if (err) {
3793 if (err->code != GOT_ERR_OBJ_TYPE)
3794 goto done;
3795 /* "lightweight" tag */
3796 err = got_object_open_as_commit(&commit2, repo, id2);
3797 if (err)
3798 goto done;
3799 time2 = got_object_commit_get_committer_time(commit2);
3800 } else
3801 time2 = got_object_tag_get_tagger_time(tag2);
3803 /* Put latest tags first. */
3804 if (time1 < time2)
3805 *cmp = 1;
3806 else if (time1 > time2)
3807 *cmp = -1;
3808 else
3809 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3810 done:
3811 free(id1);
3812 free(id2);
3813 if (tag1)
3814 got_object_tag_close(tag1);
3815 if (tag2)
3816 got_object_tag_close(tag2);
3817 if (commit1)
3818 got_object_commit_close(commit1);
3819 if (commit2)
3820 got_object_commit_close(commit2);
3821 return err;
3824 static const struct got_error *
3825 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3827 static const struct got_error *err = NULL;
3828 struct got_reflist_head refs;
3829 struct got_reflist_entry *re;
3831 SIMPLEQ_INIT(&refs);
3833 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3834 if (err)
3835 return err;
3837 SIMPLEQ_FOREACH(re, &refs, entry) {
3838 const char *refname;
3839 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3840 char datebuf[26];
3841 const char *tagger;
3842 time_t tagger_time;
3843 struct got_object_id *id;
3844 struct got_tag_object *tag;
3845 struct got_commit_object *commit = NULL;
3847 refname = got_ref_get_name(re->ref);
3848 if (strncmp(refname, "refs/tags/", 10) != 0)
3849 continue;
3850 refname += 10;
3851 refstr = got_ref_to_str(re->ref);
3852 if (refstr == NULL) {
3853 err = got_error_from_errno("got_ref_to_str");
3854 break;
3856 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3857 free(refstr);
3859 err = got_ref_resolve(&id, repo, re->ref);
3860 if (err)
3861 break;
3862 err = got_object_open_as_tag(&tag, repo, id);
3863 if (err) {
3864 if (err->code != GOT_ERR_OBJ_TYPE) {
3865 free(id);
3866 break;
3868 /* "lightweight" tag */
3869 err = got_object_open_as_commit(&commit, repo, id);
3870 if (err) {
3871 free(id);
3872 break;
3874 tagger = got_object_commit_get_committer(commit);
3875 tagger_time =
3876 got_object_commit_get_committer_time(commit);
3877 err = got_object_id_str(&id_str, id);
3878 free(id);
3879 if (err)
3880 break;
3881 } else {
3882 free(id);
3883 tagger = got_object_tag_get_tagger(tag);
3884 tagger_time = got_object_tag_get_tagger_time(tag);
3885 err = got_object_id_str(&id_str,
3886 got_object_tag_get_object_id(tag));
3887 if (err)
3888 break;
3890 printf("from: %s\n", tagger);
3891 datestr = get_datestr(&tagger_time, datebuf);
3892 if (datestr)
3893 printf("date: %s UTC\n", datestr);
3894 if (commit)
3895 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3896 else {
3897 switch (got_object_tag_get_object_type(tag)) {
3898 case GOT_OBJ_TYPE_BLOB:
3899 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3900 id_str);
3901 break;
3902 case GOT_OBJ_TYPE_TREE:
3903 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3904 id_str);
3905 break;
3906 case GOT_OBJ_TYPE_COMMIT:
3907 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3908 id_str);
3909 break;
3910 case GOT_OBJ_TYPE_TAG:
3911 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3912 id_str);
3913 break;
3914 default:
3915 break;
3918 free(id_str);
3919 if (commit) {
3920 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3921 if (err)
3922 break;
3923 got_object_commit_close(commit);
3924 } else {
3925 tagmsg0 = strdup(got_object_tag_get_message(tag));
3926 got_object_tag_close(tag);
3927 if (tagmsg0 == NULL) {
3928 err = got_error_from_errno("strdup");
3929 break;
3933 tagmsg = tagmsg0;
3934 do {
3935 line = strsep(&tagmsg, "\n");
3936 if (line)
3937 printf(" %s\n", line);
3938 } while (line);
3939 free(tagmsg0);
3942 got_ref_list_free(&refs);
3943 return NULL;
3946 static const struct got_error *
3947 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3948 const char *tag_name, const char *repo_path)
3950 const struct got_error *err = NULL;
3951 char *template = NULL, *initial_content = NULL;
3952 char *editor = NULL;
3953 int fd = -1;
3955 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3956 err = got_error_from_errno("asprintf");
3957 goto done;
3960 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3961 commit_id_str, tag_name) == -1) {
3962 err = got_error_from_errno("asprintf");
3963 goto done;
3966 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3967 if (err)
3968 goto done;
3970 dprintf(fd, initial_content);
3971 close(fd);
3973 err = get_editor(&editor);
3974 if (err)
3975 goto done;
3976 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3977 done:
3978 free(initial_content);
3979 free(template);
3980 free(editor);
3982 /* Editor is done; we can now apply unveil(2) */
3983 if (err == NULL) {
3984 err = apply_unveil(repo_path, 0, NULL);
3985 if (err) {
3986 free(*tagmsg);
3987 *tagmsg = NULL;
3990 return err;
3993 static const struct got_error *
3994 add_tag(struct got_repository *repo, const char *tag_name,
3995 const char *commit_arg, const char *tagmsg_arg)
3997 const struct got_error *err = NULL;
3998 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3999 char *label = NULL, *commit_id_str = NULL;
4000 struct got_reference *ref = NULL;
4001 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4002 char *tagmsg_path = NULL, *tag_id_str = NULL;
4003 int preserve_tagmsg = 0;
4006 * Don't let the user create a tag name with a leading '-'.
4007 * While technically a valid reference name, this case is usually
4008 * an unintended typo.
4010 if (tag_name[0] == '-')
4011 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4013 err = get_author(&tagger, repo);
4014 if (err)
4015 return err;
4017 err = match_object_id(&commit_id, &label, commit_arg,
4018 GOT_OBJ_TYPE_COMMIT, 1, repo);
4019 if (err)
4020 goto done;
4022 err = got_object_id_str(&commit_id_str, commit_id);
4023 if (err)
4024 goto done;
4026 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4027 refname = strdup(tag_name);
4028 if (refname == NULL) {
4029 err = got_error_from_errno("strdup");
4030 goto done;
4032 tag_name += 10;
4033 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4034 err = got_error_from_errno("asprintf");
4035 goto done;
4038 err = got_ref_open(&ref, repo, refname, 0);
4039 if (err == NULL) {
4040 err = got_error(GOT_ERR_TAG_EXISTS);
4041 goto done;
4042 } else if (err->code != GOT_ERR_NOT_REF)
4043 goto done;
4045 if (tagmsg_arg == NULL) {
4046 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4047 tag_name, got_repo_get_path(repo));
4048 if (err) {
4049 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4050 tagmsg_path != NULL)
4051 preserve_tagmsg = 1;
4052 goto done;
4056 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4057 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4058 if (err) {
4059 if (tagmsg_path)
4060 preserve_tagmsg = 1;
4061 goto done;
4064 err = got_ref_alloc(&ref, refname, tag_id);
4065 if (err) {
4066 if (tagmsg_path)
4067 preserve_tagmsg = 1;
4068 goto done;
4071 err = got_ref_write(ref, repo);
4072 if (err) {
4073 if (tagmsg_path)
4074 preserve_tagmsg = 1;
4075 goto done;
4078 err = got_object_id_str(&tag_id_str, tag_id);
4079 if (err) {
4080 if (tagmsg_path)
4081 preserve_tagmsg = 1;
4082 goto done;
4084 printf("Created tag %s\n", tag_id_str);
4085 done:
4086 if (preserve_tagmsg) {
4087 fprintf(stderr, "%s: tag message preserved in %s\n",
4088 getprogname(), tagmsg_path);
4089 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4090 err = got_error_from_errno2("unlink", tagmsg_path);
4091 free(tag_id_str);
4092 if (ref)
4093 got_ref_close(ref);
4094 free(commit_id);
4095 free(commit_id_str);
4096 free(refname);
4097 free(tagmsg);
4098 free(tagmsg_path);
4099 free(tagger);
4100 return err;
4103 static const struct got_error *
4104 cmd_tag(int argc, char *argv[])
4106 const struct got_error *error = NULL;
4107 struct got_repository *repo = NULL;
4108 struct got_worktree *worktree = NULL;
4109 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4110 char *gitconfig_path = NULL;
4111 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4112 int ch, do_list = 0;
4114 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4115 switch (ch) {
4116 case 'm':
4117 tagmsg = optarg;
4118 break;
4119 case 'r':
4120 repo_path = realpath(optarg, NULL);
4121 if (repo_path == NULL)
4122 return got_error_from_errno2("realpath",
4123 optarg);
4124 got_path_strip_trailing_slashes(repo_path);
4125 break;
4126 case 'l':
4127 do_list = 1;
4128 break;
4129 default:
4130 usage_tag();
4131 /* NOTREACHED */
4135 argc -= optind;
4136 argv += optind;
4138 if (do_list) {
4139 if (tagmsg)
4140 errx(1, "-l and -m options are mutually exclusive\n");
4141 if (argc > 0)
4142 usage_tag();
4143 } else if (argc < 1 || argc > 2)
4144 usage_tag();
4145 else if (argc > 1)
4146 commit_id_arg = argv[1];
4147 tag_name = argv[0];
4149 #ifndef PROFILE
4150 if (do_list) {
4151 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4152 NULL) == -1)
4153 err(1, "pledge");
4154 } else {
4155 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4156 "sendfd unveil", NULL) == -1)
4157 err(1, "pledge");
4159 #endif
4160 cwd = getcwd(NULL, 0);
4161 if (cwd == NULL) {
4162 error = got_error_from_errno("getcwd");
4163 goto done;
4166 if (repo_path == NULL) {
4167 error = got_worktree_open(&worktree, cwd);
4168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4169 goto done;
4170 else
4171 error = NULL;
4172 if (worktree) {
4173 repo_path =
4174 strdup(got_worktree_get_repo_path(worktree));
4175 if (repo_path == NULL)
4176 error = got_error_from_errno("strdup");
4177 if (error)
4178 goto done;
4179 } else {
4180 repo_path = strdup(cwd);
4181 if (repo_path == NULL) {
4182 error = got_error_from_errno("strdup");
4183 goto done;
4188 if (do_list) {
4189 error = got_repo_open(&repo, repo_path, NULL);
4190 if (error != NULL)
4191 goto done;
4192 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4193 if (error)
4194 goto done;
4195 error = list_tags(repo, worktree);
4196 } else {
4197 error = get_gitconfig_path(&gitconfig_path);
4198 if (error)
4199 goto done;
4200 error = got_repo_open(&repo, repo_path, gitconfig_path);
4201 if (error != NULL)
4202 goto done;
4204 if (tagmsg) {
4205 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4206 if (error)
4207 goto done;
4210 if (commit_id_arg == NULL) {
4211 struct got_reference *head_ref;
4212 struct got_object_id *commit_id;
4213 error = got_ref_open(&head_ref, repo,
4214 worktree ? got_worktree_get_head_ref_name(worktree)
4215 : GOT_REF_HEAD, 0);
4216 if (error)
4217 goto done;
4218 error = got_ref_resolve(&commit_id, repo, head_ref);
4219 got_ref_close(head_ref);
4220 if (error)
4221 goto done;
4222 error = got_object_id_str(&commit_id_str, commit_id);
4223 free(commit_id);
4224 if (error)
4225 goto done;
4228 error = add_tag(repo, tag_name,
4229 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4231 done:
4232 if (repo)
4233 got_repo_close(repo);
4234 if (worktree)
4235 got_worktree_close(worktree);
4236 free(cwd);
4237 free(repo_path);
4238 free(gitconfig_path);
4239 free(commit_id_str);
4240 return error;
4243 __dead static void
4244 usage_add(void)
4246 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4247 getprogname());
4248 exit(1);
4251 static const struct got_error *
4252 add_progress(void *arg, unsigned char status, const char *path)
4254 while (path[0] == '/')
4255 path++;
4256 printf("%c %s\n", status, path);
4257 return NULL;
4260 static const struct got_error *
4261 cmd_add(int argc, char *argv[])
4263 const struct got_error *error = NULL;
4264 struct got_repository *repo = NULL;
4265 struct got_worktree *worktree = NULL;
4266 char *cwd = NULL;
4267 struct got_pathlist_head paths;
4268 struct got_pathlist_entry *pe;
4269 int ch, can_recurse = 0, no_ignores = 0;
4271 TAILQ_INIT(&paths);
4273 while ((ch = getopt(argc, argv, "IR")) != -1) {
4274 switch (ch) {
4275 case 'I':
4276 no_ignores = 1;
4277 break;
4278 case 'R':
4279 can_recurse = 1;
4280 break;
4281 default:
4282 usage_add();
4283 /* NOTREACHED */
4287 argc -= optind;
4288 argv += optind;
4290 #ifndef PROFILE
4291 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4292 NULL) == -1)
4293 err(1, "pledge");
4294 #endif
4295 if (argc < 1)
4296 usage_add();
4298 cwd = getcwd(NULL, 0);
4299 if (cwd == NULL) {
4300 error = got_error_from_errno("getcwd");
4301 goto done;
4304 error = got_worktree_open(&worktree, cwd);
4305 if (error)
4306 goto done;
4308 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4309 NULL);
4310 if (error != NULL)
4311 goto done;
4313 error = apply_unveil(got_repo_get_path(repo), 1,
4314 got_worktree_get_root_path(worktree));
4315 if (error)
4316 goto done;
4318 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4319 if (error)
4320 goto done;
4322 if (!can_recurse && no_ignores) {
4323 error = got_error_msg(GOT_ERR_BAD_PATH,
4324 "disregarding ignores requires -R option");
4325 goto done;
4329 if (!can_recurse) {
4330 char *ondisk_path;
4331 struct stat sb;
4332 TAILQ_FOREACH(pe, &paths, entry) {
4333 if (asprintf(&ondisk_path, "%s/%s",
4334 got_worktree_get_root_path(worktree),
4335 pe->path) == -1) {
4336 error = got_error_from_errno("asprintf");
4337 goto done;
4339 if (lstat(ondisk_path, &sb) == -1) {
4340 if (errno == ENOENT) {
4341 free(ondisk_path);
4342 continue;
4344 error = got_error_from_errno2("lstat",
4345 ondisk_path);
4346 free(ondisk_path);
4347 goto done;
4349 free(ondisk_path);
4350 if (S_ISDIR(sb.st_mode)) {
4351 error = got_error_msg(GOT_ERR_BAD_PATH,
4352 "adding directories requires -R option");
4353 goto done;
4358 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4359 NULL, repo, no_ignores);
4360 done:
4361 if (repo)
4362 got_repo_close(repo);
4363 if (worktree)
4364 got_worktree_close(worktree);
4365 TAILQ_FOREACH(pe, &paths, entry)
4366 free((char *)pe->path);
4367 got_pathlist_free(&paths);
4368 free(cwd);
4369 return error;
4372 __dead static void
4373 usage_remove(void)
4375 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4376 getprogname());
4377 exit(1);
4380 static const struct got_error *
4381 print_remove_status(void *arg, unsigned char status,
4382 unsigned char staged_status, const char *path)
4384 while (path[0] == '/')
4385 path++;
4386 if (status == GOT_STATUS_NONEXISTENT)
4387 return NULL;
4388 if (status == staged_status && (status == GOT_STATUS_DELETE))
4389 status = GOT_STATUS_NO_CHANGE;
4390 printf("%c%c %s\n", status, staged_status, path);
4391 return NULL;
4394 static const struct got_error *
4395 cmd_remove(int argc, char *argv[])
4397 const struct got_error *error = NULL;
4398 struct got_worktree *worktree = NULL;
4399 struct got_repository *repo = NULL;
4400 char *cwd = NULL;
4401 struct got_pathlist_head paths;
4402 struct got_pathlist_entry *pe;
4403 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4405 TAILQ_INIT(&paths);
4407 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4408 switch (ch) {
4409 case 'f':
4410 delete_local_mods = 1;
4411 break;
4412 case 'k':
4413 keep_on_disk = 1;
4414 break;
4415 case 'R':
4416 can_recurse = 1;
4417 break;
4418 default:
4419 usage_remove();
4420 /* NOTREACHED */
4424 argc -= optind;
4425 argv += optind;
4427 #ifndef PROFILE
4428 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4429 NULL) == -1)
4430 err(1, "pledge");
4431 #endif
4432 if (argc < 1)
4433 usage_remove();
4435 cwd = getcwd(NULL, 0);
4436 if (cwd == NULL) {
4437 error = got_error_from_errno("getcwd");
4438 goto done;
4440 error = got_worktree_open(&worktree, cwd);
4441 if (error)
4442 goto done;
4444 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4445 NULL);
4446 if (error)
4447 goto done;
4449 error = apply_unveil(got_repo_get_path(repo), 1,
4450 got_worktree_get_root_path(worktree));
4451 if (error)
4452 goto done;
4454 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4455 if (error)
4456 goto done;
4458 if (!can_recurse) {
4459 char *ondisk_path;
4460 struct stat sb;
4461 TAILQ_FOREACH(pe, &paths, entry) {
4462 if (asprintf(&ondisk_path, "%s/%s",
4463 got_worktree_get_root_path(worktree),
4464 pe->path) == -1) {
4465 error = got_error_from_errno("asprintf");
4466 goto done;
4468 if (lstat(ondisk_path, &sb) == -1) {
4469 if (errno == ENOENT) {
4470 free(ondisk_path);
4471 continue;
4473 error = got_error_from_errno2("lstat",
4474 ondisk_path);
4475 free(ondisk_path);
4476 goto done;
4478 free(ondisk_path);
4479 if (S_ISDIR(sb.st_mode)) {
4480 error = got_error_msg(GOT_ERR_BAD_PATH,
4481 "removing directories requires -R option");
4482 goto done;
4487 error = got_worktree_schedule_delete(worktree, &paths,
4488 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4489 if (error)
4490 goto done;
4491 done:
4492 if (repo)
4493 got_repo_close(repo);
4494 if (worktree)
4495 got_worktree_close(worktree);
4496 TAILQ_FOREACH(pe, &paths, entry)
4497 free((char *)pe->path);
4498 got_pathlist_free(&paths);
4499 free(cwd);
4500 return error;
4503 __dead static void
4504 usage_revert(void)
4506 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4507 "path ...\n", getprogname());
4508 exit(1);
4511 static const struct got_error *
4512 revert_progress(void *arg, unsigned char status, const char *path)
4514 while (path[0] == '/')
4515 path++;
4516 printf("%c %s\n", status, path);
4517 return NULL;
4520 struct choose_patch_arg {
4521 FILE *patch_script_file;
4522 const char *action;
4525 static const struct got_error *
4526 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4527 int nchanges, const char *action)
4529 char *line = NULL;
4530 size_t linesize = 0;
4531 ssize_t linelen;
4533 switch (status) {
4534 case GOT_STATUS_ADD:
4535 printf("A %s\n%s this addition? [y/n] ", path, action);
4536 break;
4537 case GOT_STATUS_DELETE:
4538 printf("D %s\n%s this deletion? [y/n] ", path, action);
4539 break;
4540 case GOT_STATUS_MODIFY:
4541 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4542 return got_error_from_errno("fseek");
4543 printf(GOT_COMMIT_SEP_STR);
4544 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4545 printf("%s", line);
4546 if (ferror(patch_file))
4547 return got_error_from_errno("getline");
4548 printf(GOT_COMMIT_SEP_STR);
4549 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4550 path, n, nchanges, action);
4551 break;
4552 default:
4553 return got_error_path(path, GOT_ERR_FILE_STATUS);
4556 return NULL;
4559 static const struct got_error *
4560 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4561 FILE *patch_file, int n, int nchanges)
4563 const struct got_error *err = NULL;
4564 char *line = NULL;
4565 size_t linesize = 0;
4566 ssize_t linelen;
4567 int resp = ' ';
4568 struct choose_patch_arg *a = arg;
4570 *choice = GOT_PATCH_CHOICE_NONE;
4572 if (a->patch_script_file) {
4573 char *nl;
4574 err = show_change(status, path, patch_file, n, nchanges,
4575 a->action);
4576 if (err)
4577 return err;
4578 linelen = getline(&line, &linesize, a->patch_script_file);
4579 if (linelen == -1) {
4580 if (ferror(a->patch_script_file))
4581 return got_error_from_errno("getline");
4582 return NULL;
4584 nl = strchr(line, '\n');
4585 if (nl)
4586 *nl = '\0';
4587 if (strcmp(line, "y") == 0) {
4588 *choice = GOT_PATCH_CHOICE_YES;
4589 printf("y\n");
4590 } else if (strcmp(line, "n") == 0) {
4591 *choice = GOT_PATCH_CHOICE_NO;
4592 printf("n\n");
4593 } else if (strcmp(line, "q") == 0 &&
4594 status == GOT_STATUS_MODIFY) {
4595 *choice = GOT_PATCH_CHOICE_QUIT;
4596 printf("q\n");
4597 } else
4598 printf("invalid response '%s'\n", line);
4599 free(line);
4600 return NULL;
4603 while (resp != 'y' && resp != 'n' && resp != 'q') {
4604 err = show_change(status, path, patch_file, n, nchanges,
4605 a->action);
4606 if (err)
4607 return err;
4608 resp = getchar();
4609 if (resp == '\n')
4610 resp = getchar();
4611 if (status == GOT_STATUS_MODIFY) {
4612 if (resp != 'y' && resp != 'n' && resp != 'q') {
4613 printf("invalid response '%c'\n", resp);
4614 resp = ' ';
4616 } else if (resp != 'y' && resp != 'n') {
4617 printf("invalid response '%c'\n", resp);
4618 resp = ' ';
4622 if (resp == 'y')
4623 *choice = GOT_PATCH_CHOICE_YES;
4624 else if (resp == 'n')
4625 *choice = GOT_PATCH_CHOICE_NO;
4626 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4627 *choice = GOT_PATCH_CHOICE_QUIT;
4629 return NULL;
4633 static const struct got_error *
4634 cmd_revert(int argc, char *argv[])
4636 const struct got_error *error = NULL;
4637 struct got_worktree *worktree = NULL;
4638 struct got_repository *repo = NULL;
4639 char *cwd = NULL, *path = NULL;
4640 struct got_pathlist_head paths;
4641 struct got_pathlist_entry *pe;
4642 int ch, can_recurse = 0, pflag = 0;
4643 FILE *patch_script_file = NULL;
4644 const char *patch_script_path = NULL;
4645 struct choose_patch_arg cpa;
4647 TAILQ_INIT(&paths);
4649 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4650 switch (ch) {
4651 case 'p':
4652 pflag = 1;
4653 break;
4654 case 'F':
4655 patch_script_path = optarg;
4656 break;
4657 case 'R':
4658 can_recurse = 1;
4659 break;
4660 default:
4661 usage_revert();
4662 /* NOTREACHED */
4666 argc -= optind;
4667 argv += optind;
4669 #ifndef PROFILE
4670 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4671 "unveil", NULL) == -1)
4672 err(1, "pledge");
4673 #endif
4674 if (argc < 1)
4675 usage_revert();
4676 if (patch_script_path && !pflag)
4677 errx(1, "-F option can only be used together with -p option");
4679 cwd = getcwd(NULL, 0);
4680 if (cwd == NULL) {
4681 error = got_error_from_errno("getcwd");
4682 goto done;
4684 error = got_worktree_open(&worktree, cwd);
4685 if (error)
4686 goto done;
4688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4689 NULL);
4690 if (error != NULL)
4691 goto done;
4693 if (patch_script_path) {
4694 patch_script_file = fopen(patch_script_path, "r");
4695 if (patch_script_file == NULL) {
4696 error = got_error_from_errno2("fopen",
4697 patch_script_path);
4698 goto done;
4701 error = apply_unveil(got_repo_get_path(repo), 1,
4702 got_worktree_get_root_path(worktree));
4703 if (error)
4704 goto done;
4706 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4707 if (error)
4708 goto done;
4710 if (!can_recurse) {
4711 char *ondisk_path;
4712 struct stat sb;
4713 TAILQ_FOREACH(pe, &paths, entry) {
4714 if (asprintf(&ondisk_path, "%s/%s",
4715 got_worktree_get_root_path(worktree),
4716 pe->path) == -1) {
4717 error = got_error_from_errno("asprintf");
4718 goto done;
4720 if (lstat(ondisk_path, &sb) == -1) {
4721 if (errno == ENOENT) {
4722 free(ondisk_path);
4723 continue;
4725 error = got_error_from_errno2("lstat",
4726 ondisk_path);
4727 free(ondisk_path);
4728 goto done;
4730 free(ondisk_path);
4731 if (S_ISDIR(sb.st_mode)) {
4732 error = got_error_msg(GOT_ERR_BAD_PATH,
4733 "reverting directories requires -R option");
4734 goto done;
4739 cpa.patch_script_file = patch_script_file;
4740 cpa.action = "revert";
4741 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4742 pflag ? choose_patch : NULL, &cpa, repo);
4743 if (error)
4744 goto done;
4745 done:
4746 if (patch_script_file && fclose(patch_script_file) == EOF &&
4747 error == NULL)
4748 error = got_error_from_errno2("fclose", patch_script_path);
4749 if (repo)
4750 got_repo_close(repo);
4751 if (worktree)
4752 got_worktree_close(worktree);
4753 free(path);
4754 free(cwd);
4755 return error;
4758 __dead static void
4759 usage_commit(void)
4761 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4762 getprogname());
4763 exit(1);
4766 struct collect_commit_logmsg_arg {
4767 const char *cmdline_log;
4768 const char *editor;
4769 const char *worktree_path;
4770 const char *branch_name;
4771 const char *repo_path;
4772 char *logmsg_path;
4776 static const struct got_error *
4777 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4778 void *arg)
4780 char *initial_content = NULL;
4781 struct got_pathlist_entry *pe;
4782 const struct got_error *err = NULL;
4783 char *template = NULL;
4784 struct collect_commit_logmsg_arg *a = arg;
4785 int fd;
4786 size_t len;
4788 /* if a message was specified on the command line, just use it */
4789 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4790 len = strlen(a->cmdline_log) + 1;
4791 *logmsg = malloc(len + 1);
4792 if (*logmsg == NULL)
4793 return got_error_from_errno("malloc");
4794 strlcpy(*logmsg, a->cmdline_log, len);
4795 return NULL;
4798 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4799 return got_error_from_errno("asprintf");
4801 if (asprintf(&initial_content,
4802 "\n# changes to be committed on branch %s:\n",
4803 a->branch_name) == -1)
4804 return got_error_from_errno("asprintf");
4806 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4807 if (err)
4808 goto done;
4810 dprintf(fd, initial_content);
4812 TAILQ_FOREACH(pe, commitable_paths, entry) {
4813 struct got_commitable *ct = pe->data;
4814 dprintf(fd, "# %c %s\n",
4815 got_commitable_get_status(ct),
4816 got_commitable_get_path(ct));
4818 close(fd);
4820 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4821 done:
4822 free(initial_content);
4823 free(template);
4825 /* Editor is done; we can now apply unveil(2) */
4826 if (err == NULL) {
4827 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4828 if (err) {
4829 free(*logmsg);
4830 *logmsg = NULL;
4833 return err;
4836 static const struct got_error *
4837 cmd_commit(int argc, char *argv[])
4839 const struct got_error *error = NULL;
4840 struct got_worktree *worktree = NULL;
4841 struct got_repository *repo = NULL;
4842 char *cwd = NULL, *id_str = NULL;
4843 struct got_object_id *id = NULL;
4844 const char *logmsg = NULL;
4845 struct collect_commit_logmsg_arg cl_arg;
4846 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4847 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4848 struct got_pathlist_head paths;
4850 TAILQ_INIT(&paths);
4851 cl_arg.logmsg_path = NULL;
4853 while ((ch = getopt(argc, argv, "m:")) != -1) {
4854 switch (ch) {
4855 case 'm':
4856 logmsg = optarg;
4857 break;
4858 default:
4859 usage_commit();
4860 /* NOTREACHED */
4864 argc -= optind;
4865 argv += optind;
4867 #ifndef PROFILE
4868 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4869 "unveil", NULL) == -1)
4870 err(1, "pledge");
4871 #endif
4872 cwd = getcwd(NULL, 0);
4873 if (cwd == NULL) {
4874 error = got_error_from_errno("getcwd");
4875 goto done;
4877 error = got_worktree_open(&worktree, cwd);
4878 if (error)
4879 goto done;
4881 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4882 if (error)
4883 goto done;
4884 if (rebase_in_progress) {
4885 error = got_error(GOT_ERR_REBASING);
4886 goto done;
4889 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4890 worktree);
4891 if (error)
4892 goto done;
4894 error = get_gitconfig_path(&gitconfig_path);
4895 if (error)
4896 goto done;
4897 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4898 gitconfig_path);
4899 if (error != NULL)
4900 goto done;
4902 error = get_author(&author, repo);
4903 if (error)
4904 return error;
4907 * unveil(2) traverses exec(2); if an editor is used we have
4908 * to apply unveil after the log message has been written.
4910 if (logmsg == NULL || strlen(logmsg) == 0)
4911 error = get_editor(&editor);
4912 else
4913 error = apply_unveil(got_repo_get_path(repo), 0,
4914 got_worktree_get_root_path(worktree));
4915 if (error)
4916 goto done;
4918 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4919 if (error)
4920 goto done;
4922 cl_arg.editor = editor;
4923 cl_arg.cmdline_log = logmsg;
4924 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4925 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4926 if (!histedit_in_progress) {
4927 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4928 error = got_error(GOT_ERR_COMMIT_BRANCH);
4929 goto done;
4931 cl_arg.branch_name += 11;
4933 cl_arg.repo_path = got_repo_get_path(repo);
4934 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4935 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4936 if (error) {
4937 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4938 cl_arg.logmsg_path != NULL)
4939 preserve_logmsg = 1;
4940 goto done;
4943 error = got_object_id_str(&id_str, id);
4944 if (error)
4945 goto done;
4946 printf("Created commit %s\n", id_str);
4947 done:
4948 if (preserve_logmsg) {
4949 fprintf(stderr, "%s: log message preserved in %s\n",
4950 getprogname(), cl_arg.logmsg_path);
4951 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4952 error == NULL)
4953 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4954 free(cl_arg.logmsg_path);
4955 if (repo)
4956 got_repo_close(repo);
4957 if (worktree)
4958 got_worktree_close(worktree);
4959 free(cwd);
4960 free(id_str);
4961 free(gitconfig_path);
4962 free(editor);
4963 free(author);
4964 return error;
4967 __dead static void
4968 usage_cherrypick(void)
4970 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4971 exit(1);
4974 static const struct got_error *
4975 cmd_cherrypick(int argc, char *argv[])
4977 const struct got_error *error = NULL;
4978 struct got_worktree *worktree = NULL;
4979 struct got_repository *repo = NULL;
4980 char *cwd = NULL, *commit_id_str = NULL;
4981 struct got_object_id *commit_id = NULL;
4982 struct got_commit_object *commit = NULL;
4983 struct got_object_qid *pid;
4984 struct got_reference *head_ref = NULL;
4985 int ch, did_something = 0;
4987 while ((ch = getopt(argc, argv, "")) != -1) {
4988 switch (ch) {
4989 default:
4990 usage_cherrypick();
4991 /* NOTREACHED */
4995 argc -= optind;
4996 argv += optind;
4998 #ifndef PROFILE
4999 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5000 "unveil", NULL) == -1)
5001 err(1, "pledge");
5002 #endif
5003 if (argc != 1)
5004 usage_cherrypick();
5006 cwd = getcwd(NULL, 0);
5007 if (cwd == NULL) {
5008 error = got_error_from_errno("getcwd");
5009 goto done;
5011 error = got_worktree_open(&worktree, cwd);
5012 if (error)
5013 goto done;
5015 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5016 NULL);
5017 if (error != NULL)
5018 goto done;
5020 error = apply_unveil(got_repo_get_path(repo), 0,
5021 got_worktree_get_root_path(worktree));
5022 if (error)
5023 goto done;
5025 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5026 GOT_OBJ_TYPE_COMMIT, repo);
5027 if (error != NULL) {
5028 struct got_reference *ref;
5029 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5030 goto done;
5031 error = got_ref_open(&ref, repo, argv[0], 0);
5032 if (error != NULL)
5033 goto done;
5034 error = got_ref_resolve(&commit_id, repo, ref);
5035 got_ref_close(ref);
5036 if (error != NULL)
5037 goto done;
5039 error = got_object_id_str(&commit_id_str, commit_id);
5040 if (error)
5041 goto done;
5043 error = got_ref_open(&head_ref, repo,
5044 got_worktree_get_head_ref_name(worktree), 0);
5045 if (error != NULL)
5046 goto done;
5048 error = check_same_branch(commit_id, head_ref, NULL, repo);
5049 if (error) {
5050 if (error->code != GOT_ERR_ANCESTRY)
5051 goto done;
5052 error = NULL;
5053 } else {
5054 error = got_error(GOT_ERR_SAME_BRANCH);
5055 goto done;
5058 error = got_object_open_as_commit(&commit, repo, commit_id);
5059 if (error)
5060 goto done;
5061 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5062 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5063 commit_id, repo, update_progress, &did_something, check_cancelled,
5064 NULL);
5065 if (error != NULL)
5066 goto done;
5068 if (did_something)
5069 printf("Merged commit %s\n", commit_id_str);
5070 done:
5071 if (commit)
5072 got_object_commit_close(commit);
5073 free(commit_id_str);
5074 if (head_ref)
5075 got_ref_close(head_ref);
5076 if (worktree)
5077 got_worktree_close(worktree);
5078 if (repo)
5079 got_repo_close(repo);
5080 return error;
5083 __dead static void
5084 usage_backout(void)
5086 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5087 exit(1);
5090 static const struct got_error *
5091 cmd_backout(int argc, char *argv[])
5093 const struct got_error *error = NULL;
5094 struct got_worktree *worktree = NULL;
5095 struct got_repository *repo = NULL;
5096 char *cwd = NULL, *commit_id_str = NULL;
5097 struct got_object_id *commit_id = NULL;
5098 struct got_commit_object *commit = NULL;
5099 struct got_object_qid *pid;
5100 struct got_reference *head_ref = NULL;
5101 int ch, did_something = 0;
5103 while ((ch = getopt(argc, argv, "")) != -1) {
5104 switch (ch) {
5105 default:
5106 usage_backout();
5107 /* NOTREACHED */
5111 argc -= optind;
5112 argv += optind;
5114 #ifndef PROFILE
5115 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5116 "unveil", NULL) == -1)
5117 err(1, "pledge");
5118 #endif
5119 if (argc != 1)
5120 usage_backout();
5122 cwd = getcwd(NULL, 0);
5123 if (cwd == NULL) {
5124 error = got_error_from_errno("getcwd");
5125 goto done;
5127 error = got_worktree_open(&worktree, cwd);
5128 if (error)
5129 goto done;
5131 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5132 NULL);
5133 if (error != NULL)
5134 goto done;
5136 error = apply_unveil(got_repo_get_path(repo), 0,
5137 got_worktree_get_root_path(worktree));
5138 if (error)
5139 goto done;
5141 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5142 GOT_OBJ_TYPE_COMMIT, repo);
5143 if (error != NULL) {
5144 struct got_reference *ref;
5145 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5146 goto done;
5147 error = got_ref_open(&ref, repo, argv[0], 0);
5148 if (error != NULL)
5149 goto done;
5150 error = got_ref_resolve(&commit_id, repo, ref);
5151 got_ref_close(ref);
5152 if (error != NULL)
5153 goto done;
5155 error = got_object_id_str(&commit_id_str, commit_id);
5156 if (error)
5157 goto done;
5159 error = got_ref_open(&head_ref, repo,
5160 got_worktree_get_head_ref_name(worktree), 0);
5161 if (error != NULL)
5162 goto done;
5164 error = check_same_branch(commit_id, head_ref, NULL, repo);
5165 if (error)
5166 goto done;
5168 error = got_object_open_as_commit(&commit, repo, commit_id);
5169 if (error)
5170 goto done;
5171 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5172 if (pid == NULL) {
5173 error = got_error(GOT_ERR_ROOT_COMMIT);
5174 goto done;
5177 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5178 update_progress, &did_something, check_cancelled, NULL);
5179 if (error != NULL)
5180 goto done;
5182 if (did_something)
5183 printf("Backed out commit %s\n", commit_id_str);
5184 done:
5185 if (commit)
5186 got_object_commit_close(commit);
5187 free(commit_id_str);
5188 if (head_ref)
5189 got_ref_close(head_ref);
5190 if (worktree)
5191 got_worktree_close(worktree);
5192 if (repo)
5193 got_repo_close(repo);
5194 return error;
5197 __dead static void
5198 usage_rebase(void)
5200 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5201 getprogname());
5202 exit(1);
5205 void
5206 trim_logmsg(char *logmsg, int limit)
5208 char *nl;
5209 size_t len;
5211 len = strlen(logmsg);
5212 if (len > limit)
5213 len = limit;
5214 logmsg[len] = '\0';
5215 nl = strchr(logmsg, '\n');
5216 if (nl)
5217 *nl = '\0';
5220 static const struct got_error *
5221 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5223 const struct got_error *err;
5224 char *logmsg0 = NULL;
5225 const char *s;
5227 err = got_object_commit_get_logmsg(&logmsg0, commit);
5228 if (err)
5229 return err;
5231 s = logmsg0;
5232 while (isspace((unsigned char)s[0]))
5233 s++;
5235 *logmsg = strdup(s);
5236 if (*logmsg == NULL) {
5237 err = got_error_from_errno("strdup");
5238 goto done;
5241 trim_logmsg(*logmsg, limit);
5242 done:
5243 free(logmsg0);
5244 return err;
5247 static const struct got_error *
5248 show_rebase_progress(struct got_commit_object *commit,
5249 struct got_object_id *old_id, struct got_object_id *new_id)
5251 const struct got_error *err;
5252 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5254 err = got_object_id_str(&old_id_str, old_id);
5255 if (err)
5256 goto done;
5258 if (new_id) {
5259 err = got_object_id_str(&new_id_str, new_id);
5260 if (err)
5261 goto done;
5264 old_id_str[12] = '\0';
5265 if (new_id_str)
5266 new_id_str[12] = '\0';
5268 err = get_short_logmsg(&logmsg, 42, commit);
5269 if (err)
5270 goto done;
5272 printf("%s -> %s: %s\n", old_id_str,
5273 new_id_str ? new_id_str : "no-op change", logmsg);
5274 done:
5275 free(old_id_str);
5276 free(new_id_str);
5277 return err;
5280 static const struct got_error *
5281 rebase_progress(void *arg, unsigned char status, const char *path)
5283 unsigned char *rebase_status = arg;
5285 while (path[0] == '/')
5286 path++;
5287 printf("%c %s\n", status, path);
5289 if (*rebase_status == GOT_STATUS_CONFLICT)
5290 return NULL;
5291 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5292 *rebase_status = status;
5293 return NULL;
5296 static const struct got_error *
5297 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5298 struct got_reference *branch, struct got_reference *new_base_branch,
5299 struct got_reference *tmp_branch, struct got_repository *repo)
5301 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5302 return got_worktree_rebase_complete(worktree, fileindex,
5303 new_base_branch, tmp_branch, branch, repo);
5306 static const struct got_error *
5307 rebase_commit(struct got_pathlist_head *merged_paths,
5308 struct got_worktree *worktree, struct got_fileindex *fileindex,
5309 struct got_reference *tmp_branch,
5310 struct got_object_id *commit_id, struct got_repository *repo)
5312 const struct got_error *error;
5313 struct got_commit_object *commit;
5314 struct got_object_id *new_commit_id;
5316 error = got_object_open_as_commit(&commit, repo, commit_id);
5317 if (error)
5318 return error;
5320 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5321 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5322 if (error) {
5323 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5324 goto done;
5325 error = show_rebase_progress(commit, commit_id, NULL);
5326 } else {
5327 error = show_rebase_progress(commit, commit_id, new_commit_id);
5328 free(new_commit_id);
5330 done:
5331 got_object_commit_close(commit);
5332 return error;
5335 struct check_path_prefix_arg {
5336 const char *path_prefix;
5337 size_t len;
5338 int errcode;
5341 static const struct got_error *
5342 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5343 struct got_blob_object *blob2, struct got_object_id *id1,
5344 struct got_object_id *id2, const char *path1, const char *path2,
5345 mode_t mode1, mode_t mode2, struct got_repository *repo)
5347 struct check_path_prefix_arg *a = arg;
5349 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5350 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5351 return got_error(a->errcode);
5353 return NULL;
5356 static const struct got_error *
5357 check_path_prefix(struct got_object_id *parent_id,
5358 struct got_object_id *commit_id, const char *path_prefix,
5359 int errcode, struct got_repository *repo)
5361 const struct got_error *err;
5362 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5363 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5364 struct check_path_prefix_arg cpp_arg;
5366 if (got_path_is_root_dir(path_prefix))
5367 return NULL;
5369 err = got_object_open_as_commit(&commit, repo, commit_id);
5370 if (err)
5371 goto done;
5373 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5374 if (err)
5375 goto done;
5377 err = got_object_open_as_tree(&tree1, repo,
5378 got_object_commit_get_tree_id(parent_commit));
5379 if (err)
5380 goto done;
5382 err = got_object_open_as_tree(&tree2, repo,
5383 got_object_commit_get_tree_id(commit));
5384 if (err)
5385 goto done;
5387 cpp_arg.path_prefix = path_prefix;
5388 while (cpp_arg.path_prefix[0] == '/')
5389 cpp_arg.path_prefix++;
5390 cpp_arg.len = strlen(cpp_arg.path_prefix);
5391 cpp_arg.errcode = errcode;
5392 err = got_diff_tree(tree1, tree2, "", "", repo,
5393 check_path_prefix_in_diff, &cpp_arg, 0);
5394 done:
5395 if (tree1)
5396 got_object_tree_close(tree1);
5397 if (tree2)
5398 got_object_tree_close(tree2);
5399 if (commit)
5400 got_object_commit_close(commit);
5401 if (parent_commit)
5402 got_object_commit_close(parent_commit);
5403 return err;
5406 static const struct got_error *
5407 collect_commits(struct got_object_id_queue *commits,
5408 struct got_object_id *initial_commit_id,
5409 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5410 const char *path_prefix, int path_prefix_errcode,
5411 struct got_repository *repo)
5413 const struct got_error *err = NULL;
5414 struct got_commit_graph *graph = NULL;
5415 struct got_object_id *parent_id = NULL;
5416 struct got_object_qid *qid;
5417 struct got_object_id *commit_id = initial_commit_id;
5419 err = got_commit_graph_open(&graph, "/", 1);
5420 if (err)
5421 return err;
5423 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5424 check_cancelled, NULL);
5425 if (err)
5426 goto done;
5427 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5428 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5429 check_cancelled, NULL);
5430 if (err) {
5431 if (err->code == GOT_ERR_ITER_COMPLETED) {
5432 err = got_error_msg(GOT_ERR_ANCESTRY,
5433 "ran out of commits to rebase before "
5434 "youngest common ancestor commit has "
5435 "been reached?!?");
5437 goto done;
5438 } else {
5439 err = check_path_prefix(parent_id, commit_id,
5440 path_prefix, path_prefix_errcode, repo);
5441 if (err)
5442 goto done;
5444 err = got_object_qid_alloc(&qid, commit_id);
5445 if (err)
5446 goto done;
5447 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5448 commit_id = parent_id;
5451 done:
5452 got_commit_graph_close(graph);
5453 return err;
5456 static const struct got_error *
5457 cmd_rebase(int argc, char *argv[])
5459 const struct got_error *error = NULL;
5460 struct got_worktree *worktree = NULL;
5461 struct got_repository *repo = NULL;
5462 struct got_fileindex *fileindex = NULL;
5463 char *cwd = NULL;
5464 struct got_reference *branch = NULL;
5465 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5466 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5467 struct got_object_id *resume_commit_id = NULL;
5468 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5469 struct got_commit_object *commit = NULL;
5470 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5471 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5472 struct got_object_id_queue commits;
5473 struct got_pathlist_head merged_paths;
5474 const struct got_object_id_queue *parent_ids;
5475 struct got_object_qid *qid, *pid;
5477 SIMPLEQ_INIT(&commits);
5478 TAILQ_INIT(&merged_paths);
5480 while ((ch = getopt(argc, argv, "ac")) != -1) {
5481 switch (ch) {
5482 case 'a':
5483 abort_rebase = 1;
5484 break;
5485 case 'c':
5486 continue_rebase = 1;
5487 break;
5488 default:
5489 usage_rebase();
5490 /* NOTREACHED */
5494 argc -= optind;
5495 argv += optind;
5497 #ifndef PROFILE
5498 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5499 "unveil", NULL) == -1)
5500 err(1, "pledge");
5501 #endif
5502 if (abort_rebase && continue_rebase)
5503 usage_rebase();
5504 else if (abort_rebase || continue_rebase) {
5505 if (argc != 0)
5506 usage_rebase();
5507 } else if (argc != 1)
5508 usage_rebase();
5510 cwd = getcwd(NULL, 0);
5511 if (cwd == NULL) {
5512 error = got_error_from_errno("getcwd");
5513 goto done;
5515 error = got_worktree_open(&worktree, cwd);
5516 if (error)
5517 goto done;
5519 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5520 NULL);
5521 if (error != NULL)
5522 goto done;
5524 error = apply_unveil(got_repo_get_path(repo), 0,
5525 got_worktree_get_root_path(worktree));
5526 if (error)
5527 goto done;
5529 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5530 if (error)
5531 goto done;
5533 if (abort_rebase) {
5534 int did_something;
5535 if (!rebase_in_progress) {
5536 error = got_error(GOT_ERR_NOT_REBASING);
5537 goto done;
5539 error = got_worktree_rebase_continue(&resume_commit_id,
5540 &new_base_branch, &tmp_branch, &branch, &fileindex,
5541 worktree, repo);
5542 if (error)
5543 goto done;
5544 printf("Switching work tree to %s\n",
5545 got_ref_get_symref_target(new_base_branch));
5546 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5547 new_base_branch, update_progress, &did_something);
5548 if (error)
5549 goto done;
5550 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5551 goto done; /* nothing else to do */
5554 if (continue_rebase) {
5555 if (!rebase_in_progress) {
5556 error = got_error(GOT_ERR_NOT_REBASING);
5557 goto done;
5559 error = got_worktree_rebase_continue(&resume_commit_id,
5560 &new_base_branch, &tmp_branch, &branch, &fileindex,
5561 worktree, repo);
5562 if (error)
5563 goto done;
5565 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5566 resume_commit_id, repo);
5567 if (error)
5568 goto done;
5570 yca_id = got_object_id_dup(resume_commit_id);
5571 if (yca_id == NULL) {
5572 error = got_error_from_errno("got_object_id_dup");
5573 goto done;
5575 } else {
5576 error = got_ref_open(&branch, repo, argv[0], 0);
5577 if (error != NULL)
5578 goto done;
5581 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5582 if (error)
5583 goto done;
5585 if (!continue_rebase) {
5586 struct got_object_id *base_commit_id;
5588 base_commit_id = got_worktree_get_base_commit_id(worktree);
5589 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5590 base_commit_id, branch_head_commit_id, repo,
5591 check_cancelled, NULL);
5592 if (error)
5593 goto done;
5594 if (yca_id == NULL) {
5595 error = got_error_msg(GOT_ERR_ANCESTRY,
5596 "specified branch shares no common ancestry "
5597 "with work tree's branch");
5598 goto done;
5601 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5602 if (error) {
5603 if (error->code != GOT_ERR_ANCESTRY)
5604 goto done;
5605 error = NULL;
5606 } else {
5607 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5608 "specified branch resolves to a commit which "
5609 "is already contained in work tree's branch");
5610 goto done;
5612 error = got_worktree_rebase_prepare(&new_base_branch,
5613 &tmp_branch, &fileindex, worktree, branch, repo);
5614 if (error)
5615 goto done;
5618 commit_id = branch_head_commit_id;
5619 error = got_object_open_as_commit(&commit, repo, commit_id);
5620 if (error)
5621 goto done;
5623 parent_ids = got_object_commit_get_parent_ids(commit);
5624 pid = SIMPLEQ_FIRST(parent_ids);
5625 if (pid == NULL) {
5626 if (!continue_rebase) {
5627 int did_something;
5628 error = got_worktree_rebase_abort(worktree, fileindex,
5629 repo, new_base_branch, update_progress,
5630 &did_something);
5631 if (error)
5632 goto done;
5633 printf("Rebase of %s aborted\n",
5634 got_ref_get_name(branch));
5636 error = got_error(GOT_ERR_EMPTY_REBASE);
5637 goto done;
5639 error = collect_commits(&commits, commit_id, pid->id,
5640 yca_id, got_worktree_get_path_prefix(worktree),
5641 GOT_ERR_REBASE_PATH, repo);
5642 got_object_commit_close(commit);
5643 commit = NULL;
5644 if (error)
5645 goto done;
5647 if (SIMPLEQ_EMPTY(&commits)) {
5648 if (continue_rebase) {
5649 error = rebase_complete(worktree, fileindex,
5650 branch, new_base_branch, tmp_branch, repo);
5651 goto done;
5652 } else {
5653 /* Fast-forward the reference of the branch. */
5654 struct got_object_id *new_head_commit_id;
5655 char *id_str;
5656 error = got_ref_resolve(&new_head_commit_id, repo,
5657 new_base_branch);
5658 if (error)
5659 goto done;
5660 error = got_object_id_str(&id_str, new_head_commit_id);
5661 printf("Forwarding %s to commit %s\n",
5662 got_ref_get_name(branch), id_str);
5663 free(id_str);
5664 error = got_ref_change_ref(branch,
5665 new_head_commit_id);
5666 if (error)
5667 goto done;
5671 pid = NULL;
5672 SIMPLEQ_FOREACH(qid, &commits, entry) {
5673 commit_id = qid->id;
5674 parent_id = pid ? pid->id : yca_id;
5675 pid = qid;
5677 error = got_worktree_rebase_merge_files(&merged_paths,
5678 worktree, fileindex, parent_id, commit_id, repo,
5679 rebase_progress, &rebase_status, check_cancelled, NULL);
5680 if (error)
5681 goto done;
5683 if (rebase_status == GOT_STATUS_CONFLICT) {
5684 got_worktree_rebase_pathlist_free(&merged_paths);
5685 break;
5688 error = rebase_commit(&merged_paths, worktree, fileindex,
5689 tmp_branch, commit_id, repo);
5690 got_worktree_rebase_pathlist_free(&merged_paths);
5691 if (error)
5692 goto done;
5695 if (rebase_status == GOT_STATUS_CONFLICT) {
5696 error = got_worktree_rebase_postpone(worktree, fileindex);
5697 if (error)
5698 goto done;
5699 error = got_error_msg(GOT_ERR_CONFLICTS,
5700 "conflicts must be resolved before rebasing can continue");
5701 } else
5702 error = rebase_complete(worktree, fileindex, branch,
5703 new_base_branch, tmp_branch, repo);
5704 done:
5705 got_object_id_queue_free(&commits);
5706 free(branch_head_commit_id);
5707 free(resume_commit_id);
5708 free(yca_id);
5709 if (commit)
5710 got_object_commit_close(commit);
5711 if (branch)
5712 got_ref_close(branch);
5713 if (new_base_branch)
5714 got_ref_close(new_base_branch);
5715 if (tmp_branch)
5716 got_ref_close(tmp_branch);
5717 if (worktree)
5718 got_worktree_close(worktree);
5719 if (repo)
5720 got_repo_close(repo);
5721 return error;
5724 __dead static void
5725 usage_histedit(void)
5727 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5728 getprogname());
5729 exit(1);
5732 #define GOT_HISTEDIT_PICK 'p'
5733 #define GOT_HISTEDIT_EDIT 'e'
5734 #define GOT_HISTEDIT_FOLD 'f'
5735 #define GOT_HISTEDIT_DROP 'd'
5736 #define GOT_HISTEDIT_MESG 'm'
5738 static struct got_histedit_cmd {
5739 unsigned char code;
5740 const char *name;
5741 const char *desc;
5742 } got_histedit_cmds[] = {
5743 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5744 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5745 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5746 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5747 { GOT_HISTEDIT_MESG, "mesg",
5748 "single-line log message for commit above (open editor if empty)" },
5751 struct got_histedit_list_entry {
5752 TAILQ_ENTRY(got_histedit_list_entry) entry;
5753 struct got_object_id *commit_id;
5754 const struct got_histedit_cmd *cmd;
5755 char *logmsg;
5757 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5759 static const struct got_error *
5760 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5761 FILE *f, struct got_repository *repo)
5763 const struct got_error *err = NULL;
5764 char *logmsg = NULL, *id_str = NULL;
5765 struct got_commit_object *commit = NULL;
5766 int n;
5768 err = got_object_open_as_commit(&commit, repo, commit_id);
5769 if (err)
5770 goto done;
5772 err = get_short_logmsg(&logmsg, 34, commit);
5773 if (err)
5774 goto done;
5776 err = got_object_id_str(&id_str, commit_id);
5777 if (err)
5778 goto done;
5780 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5781 if (n < 0)
5782 err = got_ferror(f, GOT_ERR_IO);
5783 done:
5784 if (commit)
5785 got_object_commit_close(commit);
5786 free(id_str);
5787 free(logmsg);
5788 return err;
5791 static const struct got_error *
5792 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5793 struct got_repository *repo)
5795 const struct got_error *err = NULL;
5796 struct got_object_qid *qid;
5798 if (SIMPLEQ_EMPTY(commits))
5799 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5801 SIMPLEQ_FOREACH(qid, commits, entry) {
5802 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5803 f, repo);
5804 if (err)
5805 break;
5808 return err;
5811 static const struct got_error *
5812 write_cmd_list(FILE *f)
5814 const struct got_error *err = NULL;
5815 int n, i;
5817 n = fprintf(f, "# Available histedit commands:\n");
5818 if (n < 0)
5819 return got_ferror(f, GOT_ERR_IO);
5821 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5822 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5823 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5824 cmd->desc);
5825 if (n < 0) {
5826 err = got_ferror(f, GOT_ERR_IO);
5827 break;
5830 n = fprintf(f, "# Commits will be processed in order from top to "
5831 "bottom of this file.\n");
5832 if (n < 0)
5833 return got_ferror(f, GOT_ERR_IO);
5834 return err;
5837 static const struct got_error *
5838 histedit_syntax_error(int lineno)
5840 static char msg[42];
5841 int ret;
5843 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5844 lineno);
5845 if (ret == -1 || ret >= sizeof(msg))
5846 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5848 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5851 static const struct got_error *
5852 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5853 char *logmsg, struct got_repository *repo)
5855 const struct got_error *err;
5856 struct got_commit_object *folded_commit = NULL;
5857 char *id_str, *folded_logmsg = NULL;
5859 err = got_object_id_str(&id_str, hle->commit_id);
5860 if (err)
5861 return err;
5863 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5864 if (err)
5865 goto done;
5867 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5868 if (err)
5869 goto done;
5870 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5871 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5872 folded_logmsg) == -1) {
5873 err = got_error_from_errno("asprintf");
5874 goto done;
5876 done:
5877 if (folded_commit)
5878 got_object_commit_close(folded_commit);
5879 free(id_str);
5880 free(folded_logmsg);
5881 return err;
5884 static struct got_histedit_list_entry *
5885 get_folded_commits(struct got_histedit_list_entry *hle)
5887 struct got_histedit_list_entry *prev, *folded = NULL;
5889 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5890 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5891 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5892 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5893 folded = prev;
5894 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5897 return folded;
5900 static const struct got_error *
5901 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5902 struct got_repository *repo)
5904 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5905 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5906 const struct got_error *err = NULL;
5907 struct got_commit_object *commit = NULL;
5908 int fd;
5909 struct got_histedit_list_entry *folded = NULL;
5911 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5912 if (err)
5913 return err;
5915 folded = get_folded_commits(hle);
5916 if (folded) {
5917 while (folded != hle) {
5918 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5919 folded = TAILQ_NEXT(folded, entry);
5920 continue;
5922 err = append_folded_commit_msg(&new_msg, folded,
5923 logmsg, repo);
5924 if (err)
5925 goto done;
5926 free(logmsg);
5927 logmsg = new_msg;
5928 folded = TAILQ_NEXT(folded, entry);
5932 err = got_object_id_str(&id_str, hle->commit_id);
5933 if (err)
5934 goto done;
5935 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5936 if (err)
5937 goto done;
5938 if (asprintf(&new_msg,
5939 "%s\n# original log message of commit %s: %s",
5940 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5941 err = got_error_from_errno("asprintf");
5942 goto done;
5944 free(logmsg);
5945 logmsg = new_msg;
5947 err = got_object_id_str(&id_str, hle->commit_id);
5948 if (err)
5949 goto done;
5951 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5952 if (err)
5953 goto done;
5955 dprintf(fd, logmsg);
5956 close(fd);
5958 err = get_editor(&editor);
5959 if (err)
5960 goto done;
5962 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5963 if (err) {
5964 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5965 goto done;
5966 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5968 done:
5969 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5970 err = got_error_from_errno2("unlink", logmsg_path);
5971 free(logmsg_path);
5972 free(logmsg);
5973 free(orig_logmsg);
5974 free(editor);
5975 if (commit)
5976 got_object_commit_close(commit);
5977 return err;
5980 static const struct got_error *
5981 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5982 FILE *f, struct got_repository *repo)
5984 const struct got_error *err = NULL;
5985 char *line = NULL, *p, *end;
5986 size_t size;
5987 ssize_t len;
5988 int lineno = 0, i;
5989 const struct got_histedit_cmd *cmd;
5990 struct got_object_id *commit_id = NULL;
5991 struct got_histedit_list_entry *hle = NULL;
5993 for (;;) {
5994 len = getline(&line, &size, f);
5995 if (len == -1) {
5996 const struct got_error *getline_err;
5997 if (feof(f))
5998 break;
5999 getline_err = got_error_from_errno("getline");
6000 err = got_ferror(f, getline_err->code);
6001 break;
6003 lineno++;
6004 p = line;
6005 while (isspace((unsigned char)p[0]))
6006 p++;
6007 if (p[0] == '#' || p[0] == '\0') {
6008 free(line);
6009 line = NULL;
6010 continue;
6012 cmd = NULL;
6013 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6014 cmd = &got_histedit_cmds[i];
6015 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6016 isspace((unsigned char)p[strlen(cmd->name)])) {
6017 p += strlen(cmd->name);
6018 break;
6020 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6021 p++;
6022 break;
6025 if (i == nitems(got_histedit_cmds)) {
6026 err = histedit_syntax_error(lineno);
6027 break;
6029 while (isspace((unsigned char)p[0]))
6030 p++;
6031 if (cmd->code == GOT_HISTEDIT_MESG) {
6032 if (hle == NULL || hle->logmsg != NULL) {
6033 err = got_error(GOT_ERR_HISTEDIT_CMD);
6034 break;
6036 if (p[0] == '\0') {
6037 err = histedit_edit_logmsg(hle, repo);
6038 if (err)
6039 break;
6040 } else {
6041 hle->logmsg = strdup(p);
6042 if (hle->logmsg == NULL) {
6043 err = got_error_from_errno("strdup");
6044 break;
6047 free(line);
6048 line = NULL;
6049 continue;
6050 } else {
6051 end = p;
6052 while (end[0] && !isspace((unsigned char)end[0]))
6053 end++;
6054 *end = '\0';
6056 err = got_object_resolve_id_str(&commit_id, repo, p);
6057 if (err) {
6058 /* override error code */
6059 err = histedit_syntax_error(lineno);
6060 break;
6063 hle = malloc(sizeof(*hle));
6064 if (hle == NULL) {
6065 err = got_error_from_errno("malloc");
6066 break;
6068 hle->cmd = cmd;
6069 hle->commit_id = commit_id;
6070 hle->logmsg = NULL;
6071 commit_id = NULL;
6072 free(line);
6073 line = NULL;
6074 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6077 free(line);
6078 free(commit_id);
6079 return err;
6082 static const struct got_error *
6083 histedit_check_script(struct got_histedit_list *histedit_cmds,
6084 struct got_object_id_queue *commits, struct got_repository *repo)
6086 const struct got_error *err = NULL;
6087 struct got_object_qid *qid;
6088 struct got_histedit_list_entry *hle;
6089 static char msg[80];
6090 char *id_str;
6092 if (TAILQ_EMPTY(histedit_cmds))
6093 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6094 "histedit script contains no commands");
6095 if (SIMPLEQ_EMPTY(commits))
6096 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6098 SIMPLEQ_FOREACH(qid, commits, entry) {
6099 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6100 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6101 break;
6103 if (hle == NULL) {
6104 err = got_object_id_str(&id_str, qid->id);
6105 if (err)
6106 return err;
6107 snprintf(msg, sizeof(msg),
6108 "commit %s missing from histedit script", id_str);
6109 free(id_str);
6110 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6114 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6115 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6116 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6117 "last commit in histedit script cannot be folded");
6119 return NULL;
6122 static const struct got_error *
6123 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6124 const char *path, struct got_object_id_queue *commits,
6125 struct got_repository *repo)
6127 const struct got_error *err = NULL;
6128 char *editor;
6129 FILE *f = NULL;
6131 err = get_editor(&editor);
6132 if (err)
6133 return err;
6135 if (spawn_editor(editor, path) == -1) {
6136 err = got_error_from_errno("failed spawning editor");
6137 goto done;
6140 f = fopen(path, "r");
6141 if (f == NULL) {
6142 err = got_error_from_errno("fopen");
6143 goto done;
6145 err = histedit_parse_list(histedit_cmds, f, repo);
6146 if (err)
6147 goto done;
6149 err = histedit_check_script(histedit_cmds, commits, repo);
6150 done:
6151 if (f && fclose(f) != 0 && err == NULL)
6152 err = got_error_from_errno("fclose");
6153 free(editor);
6154 return err;
6157 static const struct got_error *
6158 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6159 struct got_object_id_queue *, const char *, struct got_repository *);
6161 static const struct got_error *
6162 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6163 struct got_object_id_queue *commits, struct got_repository *repo)
6165 const struct got_error *err;
6166 FILE *f = NULL;
6167 char *path = NULL;
6169 err = got_opentemp_named(&path, &f, "got-histedit");
6170 if (err)
6171 return err;
6173 err = write_cmd_list(f);
6174 if (err)
6175 goto done;
6177 err = histedit_write_commit_list(commits, f, repo);
6178 if (err)
6179 goto done;
6181 if (fclose(f) != 0) {
6182 err = got_error_from_errno("fclose");
6183 goto done;
6185 f = NULL;
6187 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6188 if (err) {
6189 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6190 err->code != GOT_ERR_HISTEDIT_CMD)
6191 goto done;
6192 err = histedit_edit_list_retry(histedit_cmds, err,
6193 commits, path, repo);
6195 done:
6196 if (f && fclose(f) != 0 && err == NULL)
6197 err = got_error_from_errno("fclose");
6198 if (path && unlink(path) != 0 && err == NULL)
6199 err = got_error_from_errno2("unlink", path);
6200 free(path);
6201 return err;
6204 static const struct got_error *
6205 histedit_save_list(struct got_histedit_list *histedit_cmds,
6206 struct got_worktree *worktree, struct got_repository *repo)
6208 const struct got_error *err = NULL;
6209 char *path = NULL;
6210 FILE *f = NULL;
6211 struct got_histedit_list_entry *hle;
6212 struct got_commit_object *commit = NULL;
6214 err = got_worktree_get_histedit_script_path(&path, worktree);
6215 if (err)
6216 return err;
6218 f = fopen(path, "w");
6219 if (f == NULL) {
6220 err = got_error_from_errno2("fopen", path);
6221 goto done;
6223 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6224 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6225 repo);
6226 if (err)
6227 break;
6229 if (hle->logmsg) {
6230 int n = fprintf(f, "%c %s\n",
6231 GOT_HISTEDIT_MESG, hle->logmsg);
6232 if (n < 0) {
6233 err = got_ferror(f, GOT_ERR_IO);
6234 break;
6238 done:
6239 if (f && fclose(f) != 0 && err == NULL)
6240 err = got_error_from_errno("fclose");
6241 free(path);
6242 if (commit)
6243 got_object_commit_close(commit);
6244 return err;
6247 void
6248 histedit_free_list(struct got_histedit_list *histedit_cmds)
6250 struct got_histedit_list_entry *hle;
6252 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6253 TAILQ_REMOVE(histedit_cmds, hle, entry);
6254 free(hle);
6258 static const struct got_error *
6259 histedit_load_list(struct got_histedit_list *histedit_cmds,
6260 const char *path, struct got_repository *repo)
6262 const struct got_error *err = NULL;
6263 FILE *f = NULL;
6265 f = fopen(path, "r");
6266 if (f == NULL) {
6267 err = got_error_from_errno2("fopen", path);
6268 goto done;
6271 err = histedit_parse_list(histedit_cmds, f, repo);
6272 done:
6273 if (f && fclose(f) != 0 && err == NULL)
6274 err = got_error_from_errno("fclose");
6275 return err;
6278 static const struct got_error *
6279 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6280 const struct got_error *edit_err, struct got_object_id_queue *commits,
6281 const char *path, struct got_repository *repo)
6283 const struct got_error *err = NULL, *prev_err = edit_err;
6284 int resp = ' ';
6286 while (resp != 'c' && resp != 'r' && resp != 'a') {
6287 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6288 "or (a)bort: ", getprogname(), prev_err->msg);
6289 resp = getchar();
6290 if (resp == '\n')
6291 resp = getchar();
6292 if (resp == 'c') {
6293 histedit_free_list(histedit_cmds);
6294 err = histedit_run_editor(histedit_cmds, path, commits,
6295 repo);
6296 if (err) {
6297 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6298 err->code != GOT_ERR_HISTEDIT_CMD)
6299 break;
6300 prev_err = err;
6301 resp = ' ';
6302 continue;
6304 break;
6305 } else if (resp == 'r') {
6306 histedit_free_list(histedit_cmds);
6307 err = histedit_edit_script(histedit_cmds,
6308 commits, repo);
6309 if (err) {
6310 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6311 err->code != GOT_ERR_HISTEDIT_CMD)
6312 break;
6313 prev_err = err;
6314 resp = ' ';
6315 continue;
6317 break;
6318 } else if (resp == 'a') {
6319 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6320 break;
6321 } else
6322 printf("invalid response '%c'\n", resp);
6325 return err;
6328 static const struct got_error *
6329 histedit_complete(struct got_worktree *worktree,
6330 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6331 struct got_reference *branch, struct got_repository *repo)
6333 printf("Switching work tree to %s\n",
6334 got_ref_get_symref_target(branch));
6335 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6336 branch, repo);
6339 static const struct got_error *
6340 show_histedit_progress(struct got_commit_object *commit,
6341 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6343 const struct got_error *err;
6344 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6346 err = got_object_id_str(&old_id_str, hle->commit_id);
6347 if (err)
6348 goto done;
6350 if (new_id) {
6351 err = got_object_id_str(&new_id_str, new_id);
6352 if (err)
6353 goto done;
6356 old_id_str[12] = '\0';
6357 if (new_id_str)
6358 new_id_str[12] = '\0';
6360 if (hle->logmsg) {
6361 logmsg = strdup(hle->logmsg);
6362 if (logmsg == NULL) {
6363 err = got_error_from_errno("strdup");
6364 goto done;
6366 trim_logmsg(logmsg, 42);
6367 } else {
6368 err = get_short_logmsg(&logmsg, 42, commit);
6369 if (err)
6370 goto done;
6373 switch (hle->cmd->code) {
6374 case GOT_HISTEDIT_PICK:
6375 case GOT_HISTEDIT_EDIT:
6376 printf("%s -> %s: %s\n", old_id_str,
6377 new_id_str ? new_id_str : "no-op change", logmsg);
6378 break;
6379 case GOT_HISTEDIT_DROP:
6380 case GOT_HISTEDIT_FOLD:
6381 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6382 logmsg);
6383 break;
6384 default:
6385 break;
6388 done:
6389 free(old_id_str);
6390 free(new_id_str);
6391 return err;
6394 static const struct got_error *
6395 histedit_commit(struct got_pathlist_head *merged_paths,
6396 struct got_worktree *worktree, struct got_fileindex *fileindex,
6397 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6398 struct got_repository *repo)
6400 const struct got_error *err;
6401 struct got_commit_object *commit;
6402 struct got_object_id *new_commit_id;
6404 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6405 && hle->logmsg == NULL) {
6406 err = histedit_edit_logmsg(hle, repo);
6407 if (err)
6408 return err;
6411 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6412 if (err)
6413 return err;
6415 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6416 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6417 hle->logmsg, repo);
6418 if (err) {
6419 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6420 goto done;
6421 err = show_histedit_progress(commit, hle, NULL);
6422 } else {
6423 err = show_histedit_progress(commit, hle, new_commit_id);
6424 free(new_commit_id);
6426 done:
6427 got_object_commit_close(commit);
6428 return err;
6431 static const struct got_error *
6432 histedit_skip_commit(struct got_histedit_list_entry *hle,
6433 struct got_worktree *worktree, struct got_repository *repo)
6435 const struct got_error *error;
6436 struct got_commit_object *commit;
6438 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6439 repo);
6440 if (error)
6441 return error;
6443 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6444 if (error)
6445 return error;
6447 error = show_histedit_progress(commit, hle, NULL);
6448 got_object_commit_close(commit);
6449 return error;
6452 static const struct got_error *
6453 cmd_histedit(int argc, char *argv[])
6455 const struct got_error *error = NULL;
6456 struct got_worktree *worktree = NULL;
6457 struct got_fileindex *fileindex = NULL;
6458 struct got_repository *repo = NULL;
6459 char *cwd = NULL;
6460 struct got_reference *branch = NULL;
6461 struct got_reference *tmp_branch = NULL;
6462 struct got_object_id *resume_commit_id = NULL;
6463 struct got_object_id *base_commit_id = NULL;
6464 struct got_object_id *head_commit_id = NULL;
6465 struct got_commit_object *commit = NULL;
6466 int ch, rebase_in_progress = 0, did_something;
6467 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6468 const char *edit_script_path = NULL;
6469 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6470 struct got_object_id_queue commits;
6471 struct got_pathlist_head merged_paths;
6472 const struct got_object_id_queue *parent_ids;
6473 struct got_object_qid *pid;
6474 struct got_histedit_list histedit_cmds;
6475 struct got_histedit_list_entry *hle;
6477 SIMPLEQ_INIT(&commits);
6478 TAILQ_INIT(&histedit_cmds);
6479 TAILQ_INIT(&merged_paths);
6481 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6482 switch (ch) {
6483 case 'a':
6484 abort_edit = 1;
6485 break;
6486 case 'c':
6487 continue_edit = 1;
6488 break;
6489 case 'F':
6490 edit_script_path = optarg;
6491 break;
6492 default:
6493 usage_histedit();
6494 /* NOTREACHED */
6498 argc -= optind;
6499 argv += optind;
6501 #ifndef PROFILE
6502 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6503 "unveil", NULL) == -1)
6504 err(1, "pledge");
6505 #endif
6506 if (abort_edit && continue_edit)
6507 usage_histedit();
6508 if (argc != 0)
6509 usage_histedit();
6512 * This command cannot apply unveil(2) in all cases because the
6513 * user may choose to run an editor to edit the histedit script
6514 * and to edit individual commit log messages.
6515 * unveil(2) traverses exec(2); if an editor is used we have to
6516 * apply unveil after edit script and log messages have been written.
6517 * XXX TODO: Make use of unveil(2) where possible.
6520 cwd = getcwd(NULL, 0);
6521 if (cwd == NULL) {
6522 error = got_error_from_errno("getcwd");
6523 goto done;
6525 error = got_worktree_open(&worktree, cwd);
6526 if (error)
6527 goto done;
6529 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6530 NULL);
6531 if (error != NULL)
6532 goto done;
6534 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6535 if (error)
6536 goto done;
6537 if (rebase_in_progress) {
6538 error = got_error(GOT_ERR_REBASING);
6539 goto done;
6542 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6543 if (error)
6544 goto done;
6546 if (edit_in_progress && abort_edit) {
6547 error = got_worktree_histedit_continue(&resume_commit_id,
6548 &tmp_branch, &branch, &base_commit_id, &fileindex,
6549 worktree, repo);
6550 if (error)
6551 goto done;
6552 printf("Switching work tree to %s\n",
6553 got_ref_get_symref_target(branch));
6554 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6555 branch, base_commit_id, update_progress, &did_something);
6556 if (error)
6557 goto done;
6558 printf("Histedit of %s aborted\n",
6559 got_ref_get_symref_target(branch));
6560 goto done; /* nothing else to do */
6561 } else if (abort_edit) {
6562 error = got_error(GOT_ERR_NOT_HISTEDIT);
6563 goto done;
6566 if (continue_edit) {
6567 char *path;
6569 if (!edit_in_progress) {
6570 error = got_error(GOT_ERR_NOT_HISTEDIT);
6571 goto done;
6574 error = got_worktree_get_histedit_script_path(&path, worktree);
6575 if (error)
6576 goto done;
6578 error = histedit_load_list(&histedit_cmds, path, repo);
6579 free(path);
6580 if (error)
6581 goto done;
6583 error = got_worktree_histedit_continue(&resume_commit_id,
6584 &tmp_branch, &branch, &base_commit_id, &fileindex,
6585 worktree, repo);
6586 if (error)
6587 goto done;
6589 error = got_ref_resolve(&head_commit_id, repo, branch);
6590 if (error)
6591 goto done;
6593 error = got_object_open_as_commit(&commit, repo,
6594 head_commit_id);
6595 if (error)
6596 goto done;
6597 parent_ids = got_object_commit_get_parent_ids(commit);
6598 pid = SIMPLEQ_FIRST(parent_ids);
6599 if (pid == NULL) {
6600 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6601 goto done;
6603 error = collect_commits(&commits, head_commit_id, pid->id,
6604 base_commit_id, got_worktree_get_path_prefix(worktree),
6605 GOT_ERR_HISTEDIT_PATH, repo);
6606 got_object_commit_close(commit);
6607 commit = NULL;
6608 if (error)
6609 goto done;
6610 } else {
6611 if (edit_in_progress) {
6612 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6613 goto done;
6616 error = got_ref_open(&branch, repo,
6617 got_worktree_get_head_ref_name(worktree), 0);
6618 if (error != NULL)
6619 goto done;
6621 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6622 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6623 "will not edit commit history of a branch outside "
6624 "the \"refs/heads/\" reference namespace");
6625 goto done;
6628 error = got_ref_resolve(&head_commit_id, repo, branch);
6629 got_ref_close(branch);
6630 branch = NULL;
6631 if (error)
6632 goto done;
6634 error = got_object_open_as_commit(&commit, repo,
6635 head_commit_id);
6636 if (error)
6637 goto done;
6638 parent_ids = got_object_commit_get_parent_ids(commit);
6639 pid = SIMPLEQ_FIRST(parent_ids);
6640 if (pid == NULL) {
6641 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6642 goto done;
6644 error = collect_commits(&commits, head_commit_id, pid->id,
6645 got_worktree_get_base_commit_id(worktree),
6646 got_worktree_get_path_prefix(worktree),
6647 GOT_ERR_HISTEDIT_PATH, repo);
6648 got_object_commit_close(commit);
6649 commit = NULL;
6650 if (error)
6651 goto done;
6653 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6654 &base_commit_id, &fileindex, worktree, repo);
6655 if (error)
6656 goto done;
6658 if (edit_script_path) {
6659 error = histedit_load_list(&histedit_cmds,
6660 edit_script_path, repo);
6661 if (error) {
6662 got_worktree_histedit_abort(worktree, fileindex,
6663 repo, branch, base_commit_id,
6664 update_progress, &did_something);
6665 goto done;
6667 } else {
6668 error = histedit_edit_script(&histedit_cmds, &commits,
6669 repo);
6670 if (error) {
6671 got_worktree_histedit_abort(worktree, fileindex,
6672 repo, branch, base_commit_id,
6673 update_progress, &did_something);
6674 goto done;
6679 error = histedit_save_list(&histedit_cmds, worktree,
6680 repo);
6681 if (error) {
6682 got_worktree_histedit_abort(worktree, fileindex,
6683 repo, branch, base_commit_id,
6684 update_progress, &did_something);
6685 goto done;
6690 error = histedit_check_script(&histedit_cmds, &commits, repo);
6691 if (error)
6692 goto done;
6694 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6695 if (resume_commit_id) {
6696 if (got_object_id_cmp(hle->commit_id,
6697 resume_commit_id) != 0)
6698 continue;
6700 resume_commit_id = NULL;
6701 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6702 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6703 error = histedit_skip_commit(hle, worktree,
6704 repo);
6705 } else {
6706 error = histedit_commit(NULL, worktree,
6707 fileindex, tmp_branch, hle, repo);
6709 if (error)
6710 goto done;
6711 continue;
6714 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6715 error = histedit_skip_commit(hle, worktree, repo);
6716 if (error)
6717 goto done;
6718 continue;
6721 error = got_object_open_as_commit(&commit, repo,
6722 hle->commit_id);
6723 if (error)
6724 goto done;
6725 parent_ids = got_object_commit_get_parent_ids(commit);
6726 pid = SIMPLEQ_FIRST(parent_ids);
6728 error = got_worktree_histedit_merge_files(&merged_paths,
6729 worktree, fileindex, pid->id, hle->commit_id, repo,
6730 rebase_progress, &rebase_status, check_cancelled, NULL);
6731 if (error)
6732 goto done;
6733 got_object_commit_close(commit);
6734 commit = NULL;
6736 if (rebase_status == GOT_STATUS_CONFLICT) {
6737 got_worktree_rebase_pathlist_free(&merged_paths);
6738 break;
6741 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6742 char *id_str;
6743 error = got_object_id_str(&id_str, hle->commit_id);
6744 if (error)
6745 goto done;
6746 printf("Stopping histedit for amending commit %s\n",
6747 id_str);
6748 free(id_str);
6749 got_worktree_rebase_pathlist_free(&merged_paths);
6750 error = got_worktree_histedit_postpone(worktree,
6751 fileindex);
6752 goto done;
6755 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6756 error = histedit_skip_commit(hle, worktree, repo);
6757 if (error)
6758 goto done;
6759 continue;
6762 error = histedit_commit(&merged_paths, worktree, fileindex,
6763 tmp_branch, hle, repo);
6764 got_worktree_rebase_pathlist_free(&merged_paths);
6765 if (error)
6766 goto done;
6769 if (rebase_status == GOT_STATUS_CONFLICT) {
6770 error = got_worktree_histedit_postpone(worktree, fileindex);
6771 if (error)
6772 goto done;
6773 error = got_error_msg(GOT_ERR_CONFLICTS,
6774 "conflicts must be resolved before rebasing can continue");
6775 } else
6776 error = histedit_complete(worktree, fileindex, tmp_branch,
6777 branch, repo);
6778 done:
6779 got_object_id_queue_free(&commits);
6780 histedit_free_list(&histedit_cmds);
6781 free(head_commit_id);
6782 free(base_commit_id);
6783 free(resume_commit_id);
6784 if (commit)
6785 got_object_commit_close(commit);
6786 if (branch)
6787 got_ref_close(branch);
6788 if (tmp_branch)
6789 got_ref_close(tmp_branch);
6790 if (worktree)
6791 got_worktree_close(worktree);
6792 if (repo)
6793 got_repo_close(repo);
6794 return error;
6797 __dead static void
6798 usage_integrate(void)
6800 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6801 exit(1);
6804 static const struct got_error *
6805 cmd_integrate(int argc, char *argv[])
6807 const struct got_error *error = NULL;
6808 struct got_repository *repo = NULL;
6809 struct got_worktree *worktree = NULL;
6810 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6811 const char *branch_arg = NULL;
6812 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6813 struct got_fileindex *fileindex = NULL;
6814 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6815 int ch, did_something = 0;
6817 while ((ch = getopt(argc, argv, "")) != -1) {
6818 switch (ch) {
6819 default:
6820 usage_integrate();
6821 /* NOTREACHED */
6825 argc -= optind;
6826 argv += optind;
6828 if (argc != 1)
6829 usage_integrate();
6830 branch_arg = argv[0];
6832 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6833 "unveil", NULL) == -1)
6834 err(1, "pledge");
6836 cwd = getcwd(NULL, 0);
6837 if (cwd == NULL) {
6838 error = got_error_from_errno("getcwd");
6839 goto done;
6842 error = got_worktree_open(&worktree, cwd);
6843 if (error)
6844 goto done;
6846 error = check_rebase_or_histedit_in_progress(worktree);
6847 if (error)
6848 goto done;
6850 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6851 NULL);
6852 if (error != NULL)
6853 goto done;
6855 error = apply_unveil(got_repo_get_path(repo), 0,
6856 got_worktree_get_root_path(worktree));
6857 if (error)
6858 goto done;
6860 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6861 error = got_error_from_errno("asprintf");
6862 goto done;
6865 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6866 &base_branch_ref, worktree, refname, repo);
6867 if (error)
6868 goto done;
6870 refname = strdup(got_ref_get_name(branch_ref));
6871 if (refname == NULL) {
6872 error = got_error_from_errno("strdup");
6873 got_worktree_integrate_abort(worktree, fileindex, repo,
6874 branch_ref, base_branch_ref);
6875 goto done;
6877 base_refname = strdup(got_ref_get_name(base_branch_ref));
6878 if (base_refname == NULL) {
6879 error = got_error_from_errno("strdup");
6880 got_worktree_integrate_abort(worktree, fileindex, repo,
6881 branch_ref, base_branch_ref);
6882 goto done;
6885 error = got_ref_resolve(&commit_id, repo, branch_ref);
6886 if (error)
6887 goto done;
6889 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6890 if (error)
6891 goto done;
6893 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6894 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6895 "specified branch has already been integrated");
6896 got_worktree_integrate_abort(worktree, fileindex, repo,
6897 branch_ref, base_branch_ref);
6898 goto done;
6901 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6902 if (error) {
6903 if (error->code == GOT_ERR_ANCESTRY)
6904 error = got_error(GOT_ERR_REBASE_REQUIRED);
6905 got_worktree_integrate_abort(worktree, fileindex, repo,
6906 branch_ref, base_branch_ref);
6907 goto done;
6910 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6911 branch_ref, base_branch_ref, update_progress, &did_something,
6912 check_cancelled, NULL);
6913 if (error)
6914 goto done;
6916 printf("Integrated %s into %s\n", refname, base_refname);
6917 done:
6918 if (repo)
6919 got_repo_close(repo);
6920 if (worktree)
6921 got_worktree_close(worktree);
6922 free(cwd);
6923 free(base_commit_id);
6924 free(commit_id);
6925 free(refname);
6926 free(base_refname);
6927 return error;
6930 __dead static void
6931 usage_stage(void)
6933 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6934 "[file-path ...]\n",
6935 getprogname());
6936 exit(1);
6939 static const struct got_error *
6940 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6941 const char *path, struct got_object_id *blob_id,
6942 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6943 int dirfd, const char *de_name)
6945 const struct got_error *err = NULL;
6946 char *id_str = NULL;
6948 if (staged_status != GOT_STATUS_ADD &&
6949 staged_status != GOT_STATUS_MODIFY &&
6950 staged_status != GOT_STATUS_DELETE)
6951 return NULL;
6953 if (staged_status == GOT_STATUS_ADD ||
6954 staged_status == GOT_STATUS_MODIFY)
6955 err = got_object_id_str(&id_str, staged_blob_id);
6956 else
6957 err = got_object_id_str(&id_str, blob_id);
6958 if (err)
6959 return err;
6961 printf("%s %c %s\n", id_str, staged_status, path);
6962 free(id_str);
6963 return NULL;
6966 static const struct got_error *
6967 cmd_stage(int argc, char *argv[])
6969 const struct got_error *error = NULL;
6970 struct got_repository *repo = NULL;
6971 struct got_worktree *worktree = NULL;
6972 char *cwd = NULL;
6973 struct got_pathlist_head paths;
6974 struct got_pathlist_entry *pe;
6975 int ch, list_stage = 0, pflag = 0;
6976 FILE *patch_script_file = NULL;
6977 const char *patch_script_path = NULL;
6978 struct choose_patch_arg cpa;
6980 TAILQ_INIT(&paths);
6982 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6983 switch (ch) {
6984 case 'l':
6985 list_stage = 1;
6986 break;
6987 case 'p':
6988 pflag = 1;
6989 break;
6990 case 'F':
6991 patch_script_path = optarg;
6992 break;
6993 default:
6994 usage_stage();
6995 /* NOTREACHED */
6999 argc -= optind;
7000 argv += optind;
7002 #ifndef PROFILE
7003 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7004 "unveil", NULL) == -1)
7005 err(1, "pledge");
7006 #endif
7007 if (list_stage && (pflag || patch_script_path))
7008 errx(1, "-l option cannot be used with other options");
7009 if (patch_script_path && !pflag)
7010 errx(1, "-F option can only be used together with -p option");
7012 cwd = getcwd(NULL, 0);
7013 if (cwd == NULL) {
7014 error = got_error_from_errno("getcwd");
7015 goto done;
7018 error = got_worktree_open(&worktree, cwd);
7019 if (error)
7020 goto done;
7022 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7023 NULL);
7024 if (error != NULL)
7025 goto done;
7027 if (patch_script_path) {
7028 patch_script_file = fopen(patch_script_path, "r");
7029 if (patch_script_file == NULL) {
7030 error = got_error_from_errno2("fopen",
7031 patch_script_path);
7032 goto done;
7035 error = apply_unveil(got_repo_get_path(repo), 0,
7036 got_worktree_get_root_path(worktree));
7037 if (error)
7038 goto done;
7040 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7041 if (error)
7042 goto done;
7044 if (list_stage)
7045 error = got_worktree_status(worktree, &paths, repo,
7046 print_stage, NULL, check_cancelled, NULL);
7047 else {
7048 cpa.patch_script_file = patch_script_file;
7049 cpa.action = "stage";
7050 error = got_worktree_stage(worktree, &paths,
7051 pflag ? NULL : print_status, NULL,
7052 pflag ? choose_patch : NULL, &cpa, repo);
7054 done:
7055 if (patch_script_file && fclose(patch_script_file) == EOF &&
7056 error == NULL)
7057 error = got_error_from_errno2("fclose", patch_script_path);
7058 if (repo)
7059 got_repo_close(repo);
7060 if (worktree)
7061 got_worktree_close(worktree);
7062 TAILQ_FOREACH(pe, &paths, entry)
7063 free((char *)pe->path);
7064 got_pathlist_free(&paths);
7065 free(cwd);
7066 return error;
7069 __dead static void
7070 usage_unstage(void)
7072 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7073 "[file-path ...]\n",
7074 getprogname());
7075 exit(1);
7079 static const struct got_error *
7080 cmd_unstage(int argc, char *argv[])
7082 const struct got_error *error = NULL;
7083 struct got_repository *repo = NULL;
7084 struct got_worktree *worktree = NULL;
7085 char *cwd = NULL;
7086 struct got_pathlist_head paths;
7087 struct got_pathlist_entry *pe;
7088 int ch, did_something = 0, pflag = 0;
7089 FILE *patch_script_file = NULL;
7090 const char *patch_script_path = NULL;
7091 struct choose_patch_arg cpa;
7093 TAILQ_INIT(&paths);
7095 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7096 switch (ch) {
7097 case 'p':
7098 pflag = 1;
7099 break;
7100 case 'F':
7101 patch_script_path = optarg;
7102 break;
7103 default:
7104 usage_unstage();
7105 /* NOTREACHED */
7109 argc -= optind;
7110 argv += optind;
7112 #ifndef PROFILE
7113 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7114 "unveil", NULL) == -1)
7115 err(1, "pledge");
7116 #endif
7117 if (patch_script_path && !pflag)
7118 errx(1, "-F option can only be used together with -p option");
7120 cwd = getcwd(NULL, 0);
7121 if (cwd == NULL) {
7122 error = got_error_from_errno("getcwd");
7123 goto done;
7126 error = got_worktree_open(&worktree, cwd);
7127 if (error)
7128 goto done;
7130 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7131 NULL);
7132 if (error != NULL)
7133 goto done;
7135 if (patch_script_path) {
7136 patch_script_file = fopen(patch_script_path, "r");
7137 if (patch_script_file == NULL) {
7138 error = got_error_from_errno2("fopen",
7139 patch_script_path);
7140 goto done;
7144 error = apply_unveil(got_repo_get_path(repo), 0,
7145 got_worktree_get_root_path(worktree));
7146 if (error)
7147 goto done;
7149 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7150 if (error)
7151 goto done;
7153 cpa.patch_script_file = patch_script_file;
7154 cpa.action = "unstage";
7155 error = got_worktree_unstage(worktree, &paths, update_progress,
7156 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7157 done:
7158 if (patch_script_file && fclose(patch_script_file) == EOF &&
7159 error == NULL)
7160 error = got_error_from_errno2("fclose", patch_script_path);
7161 if (repo)
7162 got_repo_close(repo);
7163 if (worktree)
7164 got_worktree_close(worktree);
7165 TAILQ_FOREACH(pe, &paths, entry)
7166 free((char *)pe->path);
7167 got_pathlist_free(&paths);
7168 free(cwd);
7169 return error;
7172 __dead static void
7173 usage_cat(void)
7175 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7176 "arg1 [arg2 ...]\n", getprogname());
7177 exit(1);
7180 static const struct got_error *
7181 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7183 const struct got_error *err;
7184 struct got_blob_object *blob;
7186 err = got_object_open_as_blob(&blob, repo, id, 8192);
7187 if (err)
7188 return err;
7190 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7191 got_object_blob_close(blob);
7192 return err;
7195 static const struct got_error *
7196 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7198 const struct got_error *err;
7199 struct got_tree_object *tree;
7200 int nentries, i;
7202 err = got_object_open_as_tree(&tree, repo, id);
7203 if (err)
7204 return err;
7206 nentries = got_object_tree_get_nentries(tree);
7207 for (i = 0; i < nentries; i++) {
7208 struct got_tree_entry *te;
7209 char *id_str;
7210 if (sigint_received || sigpipe_received)
7211 break;
7212 te = got_object_tree_get_entry(tree, i);
7213 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7214 if (err)
7215 break;
7216 fprintf(outfile, "%s %.7o %s\n", id_str,
7217 got_tree_entry_get_mode(te),
7218 got_tree_entry_get_name(te));
7219 free(id_str);
7222 got_object_tree_close(tree);
7223 return err;
7226 static const struct got_error *
7227 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7229 const struct got_error *err;
7230 struct got_commit_object *commit;
7231 const struct got_object_id_queue *parent_ids;
7232 struct got_object_qid *pid;
7233 char *id_str = NULL;
7234 const char *logmsg = NULL;
7236 err = got_object_open_as_commit(&commit, repo, id);
7237 if (err)
7238 return err;
7240 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7241 if (err)
7242 goto done;
7244 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7245 parent_ids = got_object_commit_get_parent_ids(commit);
7246 fprintf(outfile, "numparents %d\n",
7247 got_object_commit_get_nparents(commit));
7248 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7249 char *pid_str;
7250 err = got_object_id_str(&pid_str, pid->id);
7251 if (err)
7252 goto done;
7253 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7254 free(pid_str);
7256 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7257 got_object_commit_get_author(commit),
7258 got_object_commit_get_author_time(commit));
7260 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7261 got_object_commit_get_author(commit),
7262 got_object_commit_get_committer_time(commit));
7264 logmsg = got_object_commit_get_logmsg_raw(commit);
7265 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7266 fprintf(outfile, "%s", logmsg);
7267 done:
7268 free(id_str);
7269 got_object_commit_close(commit);
7270 return err;
7273 static const struct got_error *
7274 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7276 const struct got_error *err;
7277 struct got_tag_object *tag;
7278 char *id_str = NULL;
7279 const char *tagmsg = NULL;
7281 err = got_object_open_as_tag(&tag, repo, id);
7282 if (err)
7283 return err;
7285 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7286 if (err)
7287 goto done;
7289 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7291 switch (got_object_tag_get_object_type(tag)) {
7292 case GOT_OBJ_TYPE_BLOB:
7293 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7294 GOT_OBJ_LABEL_BLOB);
7295 break;
7296 case GOT_OBJ_TYPE_TREE:
7297 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7298 GOT_OBJ_LABEL_TREE);
7299 break;
7300 case GOT_OBJ_TYPE_COMMIT:
7301 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7302 GOT_OBJ_LABEL_COMMIT);
7303 break;
7304 case GOT_OBJ_TYPE_TAG:
7305 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7306 GOT_OBJ_LABEL_TAG);
7307 break;
7308 default:
7309 break;
7312 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7313 got_object_tag_get_name(tag));
7315 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7316 got_object_tag_get_tagger(tag),
7317 got_object_tag_get_tagger_time(tag));
7319 tagmsg = got_object_tag_get_message(tag);
7320 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7321 fprintf(outfile, "%s", tagmsg);
7322 done:
7323 free(id_str);
7324 got_object_tag_close(tag);
7325 return err;
7328 static const struct got_error *
7329 cmd_cat(int argc, char *argv[])
7331 const struct got_error *error;
7332 struct got_repository *repo = NULL;
7333 struct got_worktree *worktree = NULL;
7334 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7335 const char *commit_id_str = NULL;
7336 struct got_object_id *id = NULL, *commit_id = NULL;
7337 int ch, obj_type, i, force_path = 0;
7339 #ifndef PROFILE
7340 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7341 NULL) == -1)
7342 err(1, "pledge");
7343 #endif
7345 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7346 switch (ch) {
7347 case 'c':
7348 commit_id_str = optarg;
7349 break;
7350 case 'r':
7351 repo_path = realpath(optarg, NULL);
7352 if (repo_path == NULL)
7353 return got_error_from_errno2("realpath",
7354 optarg);
7355 got_path_strip_trailing_slashes(repo_path);
7356 break;
7357 case 'P':
7358 force_path = 1;
7359 break;
7360 default:
7361 usage_cat();
7362 /* NOTREACHED */
7366 argc -= optind;
7367 argv += optind;
7369 cwd = getcwd(NULL, 0);
7370 if (cwd == NULL) {
7371 error = got_error_from_errno("getcwd");
7372 goto done;
7374 error = got_worktree_open(&worktree, cwd);
7375 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7376 goto done;
7377 if (worktree) {
7378 if (repo_path == NULL) {
7379 repo_path = strdup(
7380 got_worktree_get_repo_path(worktree));
7381 if (repo_path == NULL) {
7382 error = got_error_from_errno("strdup");
7383 goto done;
7388 if (repo_path == NULL) {
7389 repo_path = getcwd(NULL, 0);
7390 if (repo_path == NULL)
7391 return got_error_from_errno("getcwd");
7394 error = got_repo_open(&repo, repo_path, NULL);
7395 free(repo_path);
7396 if (error != NULL)
7397 goto done;
7399 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7400 if (error)
7401 goto done;
7403 if (commit_id_str == NULL)
7404 commit_id_str = GOT_REF_HEAD;
7405 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7406 if (error)
7407 goto done;
7409 for (i = 0; i < argc; i++) {
7410 if (force_path) {
7411 error = got_object_id_by_path(&id, repo, commit_id,
7412 argv[i]);
7413 if (error)
7414 break;
7415 } else {
7416 error = match_object_id(&id, &label, argv[i],
7417 GOT_OBJ_TYPE_ANY, 0, repo);
7418 if (error) {
7419 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7420 error->code != GOT_ERR_NOT_REF)
7421 break;
7422 error = got_object_id_by_path(&id, repo,
7423 commit_id, argv[i]);
7424 if (error)
7425 break;
7429 error = got_object_get_type(&obj_type, repo, id);
7430 if (error)
7431 break;
7433 switch (obj_type) {
7434 case GOT_OBJ_TYPE_BLOB:
7435 error = cat_blob(id, repo, stdout);
7436 break;
7437 case GOT_OBJ_TYPE_TREE:
7438 error = cat_tree(id, repo, stdout);
7439 break;
7440 case GOT_OBJ_TYPE_COMMIT:
7441 error = cat_commit(id, repo, stdout);
7442 break;
7443 case GOT_OBJ_TYPE_TAG:
7444 error = cat_tag(id, repo, stdout);
7445 break;
7446 default:
7447 error = got_error(GOT_ERR_OBJ_TYPE);
7448 break;
7450 if (error)
7451 break;
7452 free(label);
7453 label = NULL;
7454 free(id);
7455 id = NULL;
7458 done:
7459 free(label);
7460 free(id);
7461 free(commit_id);
7462 if (worktree)
7463 got_worktree_close(worktree);
7464 if (repo) {
7465 const struct got_error *repo_error;
7466 repo_error = got_repo_close(repo);
7467 if (error == NULL)
7468 error = repo_error;
7470 return error;