2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
40 #include "got_compat.h"
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static volatile sig_atomic_t sigint_received
;
65 static volatile sig_atomic_t sigpipe_received
;
68 catch_sigint(int signo
)
74 catch_sigpipe(int signo
)
82 const struct got_error
*(*cmd_main
)(int, char *[]);
83 void (*cmd_usage
)(void);
84 const char *cmd_alias
;
87 __dead
static void usage(int, int);
88 __dead
static void usage_init(void);
89 __dead
static void usage_import(void);
90 __dead
static void usage_clone(void);
91 __dead
static void usage_fetch(void);
92 __dead
static void usage_checkout(void);
93 __dead
static void usage_update(void);
94 __dead
static void usage_log(void);
95 __dead
static void usage_diff(void);
96 __dead
static void usage_blame(void);
97 __dead
static void usage_tree(void);
98 __dead
static void usage_status(void);
99 __dead
static void usage_ref(void);
100 __dead
static void usage_branch(void);
101 __dead
static void usage_tag(void);
102 __dead
static void usage_add(void);
103 __dead
static void usage_remove(void);
104 __dead
static void usage_revert(void);
105 __dead
static void usage_commit(void);
106 __dead
static void usage_send(void);
107 __dead
static void usage_cherrypick(void);
108 __dead
static void usage_backout(void);
109 __dead
static void usage_rebase(void);
110 __dead
static void usage_histedit(void);
111 __dead
static void usage_integrate(void);
112 __dead
static void usage_merge(void);
113 __dead
static void usage_stage(void);
114 __dead
static void usage_unstage(void);
115 __dead
static void usage_cat(void);
116 __dead
static void usage_info(void);
118 static const struct got_error
* cmd_init(int, char *[]);
119 static const struct got_error
* cmd_import(int, char *[]);
120 static const struct got_error
* cmd_clone(int, char *[]);
121 static const struct got_error
* cmd_fetch(int, char *[]);
122 static const struct got_error
* cmd_checkout(int, char *[]);
123 static const struct got_error
* cmd_update(int, char *[]);
124 static const struct got_error
* cmd_log(int, char *[]);
125 static const struct got_error
* cmd_diff(int, char *[]);
126 static const struct got_error
* cmd_blame(int, char *[]);
127 static const struct got_error
* cmd_tree(int, char *[]);
128 static const struct got_error
* cmd_status(int, char *[]);
129 static const struct got_error
* cmd_ref(int, char *[]);
130 static const struct got_error
* cmd_branch(int, char *[]);
131 static const struct got_error
* cmd_tag(int, char *[]);
132 static const struct got_error
* cmd_add(int, char *[]);
133 static const struct got_error
* cmd_remove(int, char *[]);
134 static const struct got_error
* cmd_revert(int, char *[]);
135 static const struct got_error
* cmd_commit(int, char *[]);
136 static const struct got_error
* cmd_send(int, char *[]);
137 static const struct got_error
* cmd_cherrypick(int, char *[]);
138 static const struct got_error
* cmd_backout(int, char *[]);
139 static const struct got_error
* cmd_rebase(int, char *[]);
140 static const struct got_error
* cmd_histedit(int, char *[]);
141 static const struct got_error
* cmd_integrate(int, char *[]);
142 static const struct got_error
* cmd_merge(int, char *[]);
143 static const struct got_error
* cmd_stage(int, char *[]);
144 static const struct got_error
* cmd_unstage(int, char *[]);
145 static const struct got_error
* cmd_cat(int, char *[]);
146 static const struct got_error
* cmd_info(int, char *[]);
148 static struct got_cmd got_commands
[] = {
149 { "init", cmd_init
, usage_init
, "" },
150 { "import", cmd_import
, usage_import
, "im" },
151 { "clone", cmd_clone
, usage_clone
, "cl" },
152 { "fetch", cmd_fetch
, usage_fetch
, "fe" },
153 { "checkout", cmd_checkout
, usage_checkout
, "co" },
154 { "update", cmd_update
, usage_update
, "up" },
155 { "log", cmd_log
, usage_log
, "" },
156 { "diff", cmd_diff
, usage_diff
, "di" },
157 { "blame", cmd_blame
, usage_blame
, "bl" },
158 { "tree", cmd_tree
, usage_tree
, "tr" },
159 { "status", cmd_status
, usage_status
, "st" },
160 { "ref", cmd_ref
, usage_ref
, "" },
161 { "branch", cmd_branch
, usage_branch
, "br" },
162 { "tag", cmd_tag
, usage_tag
, "" },
163 { "add", cmd_add
, usage_add
, "" },
164 { "remove", cmd_remove
, usage_remove
, "rm" },
165 { "revert", cmd_revert
, usage_revert
, "rv" },
166 { "commit", cmd_commit
, usage_commit
, "ci" },
167 { "send", cmd_send
, usage_send
, "se" },
168 { "cherrypick", cmd_cherrypick
, usage_cherrypick
, "cy" },
169 { "backout", cmd_backout
, usage_backout
, "bo" },
170 { "rebase", cmd_rebase
, usage_rebase
, "rb" },
171 { "histedit", cmd_histedit
, usage_histedit
, "he" },
172 { "integrate", cmd_integrate
, usage_integrate
,"ig" },
173 { "merge", cmd_merge
, usage_merge
, "mg" },
174 { "stage", cmd_stage
, usage_stage
, "sg" },
175 { "unstage", cmd_unstage
, usage_unstage
, "ug" },
176 { "cat", cmd_cat
, usage_cat
, "" },
177 { "info", cmd_info
, usage_info
, "" },
181 list_commands(FILE *fp
)
185 fprintf(fp
, "commands:");
186 for (i
= 0; i
< nitems(got_commands
); i
++) {
187 struct got_cmd
*cmd
= &got_commands
[i
];
188 fprintf(fp
, " %s", cmd
->cmd_name
);
194 option_conflict(char a
, char b
)
196 errx(1, "-%c and -%c options are mutually exclusive", a
, b
);
200 main(int argc
, char *argv
[])
205 int hflag
= 0, Vflag
= 0;
206 static struct option longopts
[] = {
207 { "version", no_argument
, NULL
, 'V' },
211 setlocale(LC_CTYPE
, "");
213 while ((ch
= getopt_long(argc
, argv
, "+hV", longopts
, NULL
)) != -1) {
233 got_version_print_str();
238 usage(hflag
, hflag
? 0 : 1);
240 signal(SIGINT
, catch_sigint
);
241 signal(SIGPIPE
, catch_sigpipe
);
243 for (i
= 0; i
< nitems(got_commands
); i
++) {
244 const struct got_error
*error
;
246 cmd
= &got_commands
[i
];
248 if (strcmp(cmd
->cmd_name
, argv
[0]) != 0 &&
249 strcmp(cmd
->cmd_alias
, argv
[0]) != 0)
253 got_commands
[i
].cmd_usage();
255 error
= got_commands
[i
].cmd_main(argc
, argv
);
256 if (error
&& error
->code
!= GOT_ERR_CANCELLED
&&
257 error
->code
!= GOT_ERR_PRIVSEP_EXIT
&&
258 !(sigpipe_received
&&
259 error
->code
== GOT_ERR_ERRNO
&& errno
== EPIPE
) &&
261 error
->code
== GOT_ERR_ERRNO
&& errno
== EINTR
)) {
262 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
269 fprintf(stderr
, "%s: unknown command '%s'\n", getprogname(), argv
[0]);
270 list_commands(stderr
);
275 usage(int hflag
, int status
)
277 FILE *fp
= (status
== 0) ? stdout
: stderr
;
279 fprintf(fp
, "usage: %s [-h] [-V | --version] command [arg ...]\n",
286 static const struct got_error
*
287 get_editor(char **abspath
)
289 const struct got_error
*err
= NULL
;
294 editor
= getenv("VISUAL");
296 editor
= getenv("EDITOR");
299 err
= got_path_find_prog(abspath
, editor
);
304 if (*abspath
== NULL
) {
305 *abspath
= strdup("/bin/ed");
306 if (*abspath
== NULL
)
307 return got_error_from_errno("strdup");
313 static const struct got_error
*
314 apply_unveil(const char *repo_path
, int repo_read_only
,
315 const char *worktree_path
)
317 const struct got_error
*err
;
320 if (unveil("gmon.out", "rwc") != 0)
321 return got_error_from_errno2("unveil", "gmon.out");
323 if (repo_path
&& unveil(repo_path
, repo_read_only
? "r" : "rwc") != 0)
324 return got_error_from_errno2("unveil", repo_path
);
326 if (worktree_path
&& unveil(worktree_path
, "rwc") != 0)
327 return got_error_from_errno2("unveil", worktree_path
);
329 if (unveil(GOT_TMPDIR_STR
, "rwc") != 0)
330 return got_error_from_errno2("unveil", GOT_TMPDIR_STR
);
332 err
= got_privsep_unveil_exec_helpers();
336 if (unveil(NULL
, NULL
) != 0)
337 return got_error_from_errno("unveil");
345 fprintf(stderr
, "usage: %s init repository-path\n", getprogname());
349 static const struct got_error
*
350 cmd_init(int argc
, char *argv
[])
352 const struct got_error
*error
= NULL
;
353 char *repo_path
= NULL
;
356 while ((ch
= getopt(argc
, argv
, "")) != -1) {
368 if (pledge("stdio rpath wpath cpath unveil", NULL
) == -1)
374 repo_path
= strdup(argv
[0]);
375 if (repo_path
== NULL
)
376 return got_error_from_errno("strdup");
378 got_path_strip_trailing_slashes(repo_path
);
380 error
= got_path_mkdir(repo_path
);
382 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
))
385 error
= apply_unveil(repo_path
, 0, NULL
);
389 error
= got_repo_init(repo_path
);
398 fprintf(stderr
, "usage: %s import [-b branch] [-m message] "
399 "[-r repository-path] [-I pattern] path\n", getprogname());
404 spawn_editor(const char *editor
, const char *file
)
407 sig_t sighup
, sigint
, sigquit
;
410 sighup
= signal(SIGHUP
, SIG_IGN
);
411 sigint
= signal(SIGINT
, SIG_IGN
);
412 sigquit
= signal(SIGQUIT
, SIG_IGN
);
414 switch (pid
= fork()) {
418 execl(editor
, editor
, file
, (char *)NULL
);
422 while (waitpid(pid
, &st
, 0) == -1)
427 (void)signal(SIGHUP
, sighup
);
428 (void)signal(SIGINT
, sigint
);
429 (void)signal(SIGQUIT
, sigquit
);
431 if (!WIFEXITED(st
)) {
436 return WEXITSTATUS(st
);
439 static const struct got_error
*
440 edit_logmsg(char **logmsg
, const char *editor
, const char *logmsg_path
,
441 const char *initial_content
, size_t initial_content_len
,
442 int require_modification
)
444 const struct got_error
*err
= NULL
;
450 size_t len
, logmsg_len
;
451 char *initial_content_stripped
= NULL
, *buf
= NULL
, *s
;
455 if (stat(logmsg_path
, &st
) == -1)
456 return got_error_from_errno2("stat", logmsg_path
);
458 if (spawn_editor(editor
, logmsg_path
) == -1)
459 return got_error_from_errno("failed spawning editor");
461 if (stat(logmsg_path
, &st2
) == -1)
462 return got_error_from_errno("stat");
464 if (require_modification
&&
465 st
.st_mtime
== st2
.st_mtime
&& st
.st_size
== st2
.st_size
)
466 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY
,
467 "no changes made to commit message, aborting");
470 * Set up a stripped version of the initial content without comments
471 * and blank lines. We need this in order to check if the message
472 * has in fact been edited.
474 initial_content_stripped
= malloc(initial_content_len
+ 1);
475 if (initial_content_stripped
== NULL
)
476 return got_error_from_errno("malloc");
477 initial_content_stripped
[0] = '\0';
479 buf
= strdup(initial_content
);
481 err
= got_error_from_errno("strdup");
486 while ((line
= strsep(&s
, "\n")) != NULL
) {
487 if ((line
[0] == '#' || (len
== 0 && line
[0] == '\n')))
488 continue; /* remove comments and leading empty lines */
489 len
= strlcat(initial_content_stripped
, line
,
490 initial_content_len
+ 1);
491 if (len
>= initial_content_len
+ 1) {
492 err
= got_error(GOT_ERR_NO_SPACE
);
496 while (len
> 0 && initial_content_stripped
[len
- 1] == '\n') {
497 initial_content_stripped
[len
- 1] = '\0';
501 logmsg_len
= st2
.st_size
;
502 *logmsg
= malloc(logmsg_len
+ 1);
504 return got_error_from_errno("malloc");
507 fp
= fopen(logmsg_path
, "r");
509 err
= got_error_from_errno("fopen");
514 while ((linelen
= getline(&line
, &linesize
, fp
)) != -1) {
515 if ((line
[0] == '#' || (len
== 0 && line
[0] == '\n')))
516 continue; /* remove comments and leading empty lines */
517 len
= strlcat(*logmsg
, line
, logmsg_len
+ 1);
518 if (len
>= logmsg_len
+ 1) {
519 err
= got_error(GOT_ERR_NO_SPACE
);
525 err
= got_ferror(fp
, GOT_ERR_IO
);
528 while (len
> 0 && (*logmsg
)[len
- 1] == '\n') {
529 (*logmsg
)[len
- 1] = '\0';
534 err
= got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY
,
535 "commit message cannot be empty, aborting");
538 if (require_modification
&&
539 strcmp(*logmsg
, initial_content_stripped
) == 0)
540 err
= got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY
,
541 "no changes made to commit message, aborting");
543 free(initial_content_stripped
);
545 if (fp
&& fclose(fp
) == EOF
&& err
== NULL
)
546 err
= got_error_from_errno("fclose");
554 static const struct got_error
*
555 collect_import_msg(char **logmsg
, char **logmsg_path
, const char *editor
,
556 const char *path_dir
, const char *branch_name
)
558 char *initial_content
= NULL
;
559 const struct got_error
*err
= NULL
;
560 int initial_content_len
;
563 initial_content_len
= asprintf(&initial_content
,
564 "\n# %s to be imported to branch %s\n", path_dir
,
566 if (initial_content_len
== -1)
567 return got_error_from_errno("asprintf");
569 err
= got_opentemp_named_fd(logmsg_path
, &fd
,
570 GOT_TMPDIR_STR
"/got-importmsg");
574 if (write(fd
, initial_content
, initial_content_len
) == -1) {
575 err
= got_error_from_errno2("write", *logmsg_path
);
579 err
= edit_logmsg(logmsg
, editor
, *logmsg_path
, initial_content
,
580 initial_content_len
, 1);
582 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
583 err
= got_error_from_errno2("close", *logmsg_path
);
584 free(initial_content
);
592 static const struct got_error
*
593 import_progress(void *arg
, const char *path
)
595 printf("A %s\n", path
);
599 static const struct got_error
*
600 get_author(char **author
, struct got_repository
*repo
,
601 struct got_worktree
*worktree
)
603 const struct got_error
*err
= NULL
;
604 const char *got_author
= NULL
, *name
, *email
;
605 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
610 worktree_conf
= got_worktree_get_gotconfig(worktree
);
611 repo_conf
= got_repo_get_gotconfig(repo
);
614 * Priority of potential author information sources, from most
615 * significant to least significant:
616 * 1) work tree's .got/got.conf file
617 * 2) repository's got.conf file
618 * 3) repository's git config file
619 * 4) environment variables
620 * 5) global git config files (in user's home directory or /etc)
624 got_author
= got_gotconfig_get_author(worktree_conf
);
625 if (got_author
== NULL
)
626 got_author
= got_gotconfig_get_author(repo_conf
);
627 if (got_author
== NULL
) {
628 name
= got_repo_get_gitconfig_author_name(repo
);
629 email
= got_repo_get_gitconfig_author_email(repo
);
631 if (asprintf(author
, "%s <%s>", name
, email
) == -1)
632 return got_error_from_errno("asprintf");
636 got_author
= getenv("GOT_AUTHOR");
637 if (got_author
== NULL
) {
638 name
= got_repo_get_global_gitconfig_author_name(repo
);
639 email
= got_repo_get_global_gitconfig_author_email(
642 if (asprintf(author
, "%s <%s>", name
, email
)
644 return got_error_from_errno("asprintf");
647 /* TODO: Look up user in password database? */
648 return got_error(GOT_ERR_COMMIT_NO_AUTHOR
);
652 *author
= strdup(got_author
);
654 return got_error_from_errno("strdup");
657 * Really dumb email address check; we're only doing this to
658 * avoid git's object parser breaking on commits we create.
660 while (*got_author
&& *got_author
!= '<')
662 if (*got_author
!= '<') {
663 err
= got_error(GOT_ERR_COMMIT_NO_EMAIL
);
666 while (*got_author
&& *got_author
!= '@')
668 if (*got_author
!= '@') {
669 err
= got_error(GOT_ERR_COMMIT_NO_EMAIL
);
672 while (*got_author
&& *got_author
!= '>')
674 if (*got_author
!= '>')
675 err
= got_error(GOT_ERR_COMMIT_NO_EMAIL
);
684 static const struct got_error
*
685 get_gitconfig_path(char **gitconfig_path
)
687 const char *homedir
= getenv("HOME");
689 *gitconfig_path
= NULL
;
691 if (asprintf(gitconfig_path
, "%s/.gitconfig", homedir
) == -1)
692 return got_error_from_errno("asprintf");
698 static const struct got_error
*
699 cmd_import(int argc
, char *argv
[])
701 const struct got_error
*error
= NULL
;
702 char *path_dir
= NULL
, *repo_path
= NULL
, *logmsg
= NULL
;
703 char *gitconfig_path
= NULL
, *editor
= NULL
, *author
= NULL
;
704 const char *branch_name
= "main";
705 char *refname
= NULL
, *id_str
= NULL
, *logmsg_path
= NULL
;
706 struct got_repository
*repo
= NULL
;
707 struct got_reference
*branch_ref
= NULL
, *head_ref
= NULL
;
708 struct got_object_id
*new_commit_id
= NULL
;
710 struct got_pathlist_head ignores
;
711 struct got_pathlist_entry
*pe
;
712 int preserve_logmsg
= 0;
714 TAILQ_INIT(&ignores
);
716 while ((ch
= getopt(argc
, argv
, "b:m:r:I:")) != -1) {
719 branch_name
= optarg
;
722 logmsg
= strdup(optarg
);
723 if (logmsg
== NULL
) {
724 error
= got_error_from_errno("strdup");
729 repo_path
= realpath(optarg
, NULL
);
730 if (repo_path
== NULL
) {
731 error
= got_error_from_errno2("realpath",
737 if (optarg
[0] == '\0')
739 error
= got_pathlist_insert(&pe
, &ignores
, optarg
,
754 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
762 if (repo_path
== NULL
) {
763 repo_path
= getcwd(NULL
, 0);
764 if (repo_path
== NULL
)
765 return got_error_from_errno("getcwd");
767 got_path_strip_trailing_slashes(repo_path
);
768 error
= get_gitconfig_path(&gitconfig_path
);
771 error
= got_repo_open(&repo
, repo_path
, gitconfig_path
);
775 error
= get_author(&author
, repo
, NULL
);
780 * Don't let the user create a branch name with a leading '-'.
781 * While technically a valid reference name, this case is usually
782 * an unintended typo.
784 if (branch_name
[0] == '-')
785 return got_error_path(branch_name
, GOT_ERR_REF_NAME_MINUS
);
787 if (asprintf(&refname
, "refs/heads/%s", branch_name
) == -1) {
788 error
= got_error_from_errno("asprintf");
792 error
= got_ref_open(&branch_ref
, repo
, refname
, 0);
794 if (error
->code
!= GOT_ERR_NOT_REF
)
797 error
= got_error_msg(GOT_ERR_BRANCH_EXISTS
,
798 "import target branch already exists");
802 path_dir
= realpath(argv
[0], NULL
);
803 if (path_dir
== NULL
) {
804 error
= got_error_from_errno2("realpath", argv
[0]);
807 got_path_strip_trailing_slashes(path_dir
);
810 * unveil(2) traverses exec(2); if an editor is used we have
811 * to apply unveil after the log message has been written.
813 if (logmsg
== NULL
|| strlen(logmsg
) == 0) {
814 error
= get_editor(&editor
);
818 error
= collect_import_msg(&logmsg
, &logmsg_path
, editor
,
821 if (error
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
828 if (unveil(path_dir
, "r") != 0) {
829 error
= got_error_from_errno2("unveil", path_dir
);
835 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
842 error
= got_repo_import(&new_commit_id
, path_dir
, logmsg
,
843 author
, &ignores
, repo
, import_progress
, NULL
);
850 error
= got_ref_alloc(&branch_ref
, refname
, new_commit_id
);
857 error
= got_ref_write(branch_ref
, repo
);
864 error
= got_object_id_str(&id_str
, new_commit_id
);
871 error
= got_ref_open(&head_ref
, repo
, GOT_REF_HEAD
, 0);
873 if (error
->code
!= GOT_ERR_NOT_REF
) {
879 error
= got_ref_alloc_symref(&head_ref
, GOT_REF_HEAD
,
887 error
= got_ref_write(head_ref
, repo
);
895 printf("Created branch %s with commit %s\n",
896 got_ref_get_name(branch_ref
), id_str
);
898 if (preserve_logmsg
) {
899 fprintf(stderr
, "%s: log message preserved in %s\n",
900 getprogname(), logmsg_path
);
901 } else if (logmsg_path
&& unlink(logmsg_path
) == -1 && error
== NULL
)
902 error
= got_error_from_errno2("unlink", logmsg_path
);
911 free(gitconfig_path
);
913 got_ref_close(branch_ref
);
915 got_ref_close(head_ref
);
922 fprintf(stderr
, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
923 "[-R reference] repository-url [directory]\n", getprogname());
927 struct got_fetch_progress_arg
{
928 char last_scaled_size
[FMT_SCALED_STRSIZE
];
933 struct got_repository
*repo
;
938 struct got_pathlist_head
*symrefs
;
939 struct got_pathlist_head
*wanted_branches
;
940 struct got_pathlist_head
*wanted_refs
;
944 const char *remote_repo_path
;
946 int fetch_all_branches
;
947 int mirror_references
;
951 /* XXX forward declaration */
952 static const struct got_error
*
953 create_config_files(const char *proto
, const char *host
, const char *port
,
954 const char *remote_repo_path
, const char *git_url
, int fetch_all_branches
,
955 int mirror_references
, struct got_pathlist_head
*symrefs
,
956 struct got_pathlist_head
*wanted_branches
,
957 struct got_pathlist_head
*wanted_refs
, struct got_repository
*repo
);
959 static const struct got_error
*
960 fetch_progress(void *arg
, const char *message
, off_t packfile_size
,
961 int nobj_total
, int nobj_indexed
, int nobj_loose
, int nobj_resolved
)
963 const struct got_error
*err
= NULL
;
964 struct got_fetch_progress_arg
*a
= arg
;
965 char scaled_size
[FMT_SCALED_STRSIZE
];
966 int p_indexed
, p_resolved
;
967 int print_size
= 0, print_indexed
= 0, print_resolved
= 0;
970 * In order to allow a failed clone to be resumed with 'got fetch'
971 * we try to create configuration files as soon as possible.
972 * Once the server has sent information about its default branch
973 * we have all required information.
975 if (a
->create_configs
&& !a
->configs_created
&&
976 !TAILQ_EMPTY(a
->config_info
.symrefs
)) {
977 err
= create_config_files(a
->config_info
.proto
,
978 a
->config_info
.host
, a
->config_info
.port
,
979 a
->config_info
.remote_repo_path
,
980 a
->config_info
.git_url
,
981 a
->config_info
.fetch_all_branches
,
982 a
->config_info
.mirror_references
,
983 a
->config_info
.symrefs
,
984 a
->config_info
.wanted_branches
,
985 a
->config_info
.wanted_refs
, a
->repo
);
988 a
->configs_created
= 1;
991 if (a
->verbosity
< 0)
994 if (message
&& message
[0] != '\0') {
995 printf("\rserver: %s", message
);
1000 if (packfile_size
> 0 || nobj_indexed
> 0) {
1001 if (fmt_scaled(packfile_size
, scaled_size
) == 0 &&
1002 (a
->last_scaled_size
[0] == '\0' ||
1003 strcmp(scaled_size
, a
->last_scaled_size
)) != 0) {
1005 if (strlcpy(a
->last_scaled_size
, scaled_size
,
1006 FMT_SCALED_STRSIZE
) >= FMT_SCALED_STRSIZE
)
1007 return got_error(GOT_ERR_NO_SPACE
);
1009 if (nobj_indexed
> 0) {
1010 p_indexed
= (nobj_indexed
* 100) / nobj_total
;
1011 if (p_indexed
!= a
->last_p_indexed
) {
1012 a
->last_p_indexed
= p_indexed
;
1017 if (nobj_resolved
> 0) {
1018 p_resolved
= (nobj_resolved
* 100) /
1019 (nobj_total
- nobj_loose
);
1020 if (p_resolved
!= a
->last_p_resolved
) {
1021 a
->last_p_resolved
= p_resolved
;
1029 if (print_size
|| print_indexed
|| print_resolved
)
1032 printf("%*s fetched", FMT_SCALED_STRSIZE
, scaled_size
);
1034 printf("; indexing %d%%", p_indexed
);
1036 printf("; resolving deltas %d%%", p_resolved
);
1037 if (print_size
|| print_indexed
|| print_resolved
)
1043 static const struct got_error
*
1044 create_symref(const char *refname
, struct got_reference
*target_ref
,
1045 int verbosity
, struct got_repository
*repo
)
1047 const struct got_error
*err
;
1048 struct got_reference
*head_symref
;
1050 err
= got_ref_alloc_symref(&head_symref
, refname
, target_ref
);
1054 err
= got_ref_write(head_symref
, repo
);
1055 if (err
== NULL
&& verbosity
> 0) {
1056 printf("Created reference %s: %s\n", GOT_REF_HEAD
,
1057 got_ref_get_name(target_ref
));
1059 got_ref_close(head_symref
);
1063 static const struct got_error
*
1064 list_remote_refs(struct got_pathlist_head
*symrefs
,
1065 struct got_pathlist_head
*refs
)
1067 const struct got_error
*err
;
1068 struct got_pathlist_entry
*pe
;
1070 TAILQ_FOREACH(pe
, symrefs
, entry
) {
1071 const char *refname
= pe
->path
;
1072 const char *targetref
= pe
->data
;
1074 printf("%s: %s\n", refname
, targetref
);
1077 TAILQ_FOREACH(pe
, refs
, entry
) {
1078 const char *refname
= pe
->path
;
1079 struct got_object_id
*id
= pe
->data
;
1082 err
= got_object_id_str(&id_str
, id
);
1085 printf("%s: %s\n", refname
, id_str
);
1092 static const struct got_error
*
1093 create_ref(const char *refname
, struct got_object_id
*id
,
1094 int verbosity
, struct got_repository
*repo
)
1096 const struct got_error
*err
= NULL
;
1097 struct got_reference
*ref
;
1100 err
= got_object_id_str(&id_str
, id
);
1104 err
= got_ref_alloc(&ref
, refname
, id
);
1108 err
= got_ref_write(ref
, repo
);
1111 if (err
== NULL
&& verbosity
>= 0)
1112 printf("Created reference %s: %s\n", refname
, id_str
);
1119 match_wanted_ref(const char *refname
, const char *wanted_ref
)
1121 if (strncmp(refname
, "refs/", 5) != 0)
1126 * Prevent fetching of references that won't make any
1127 * sense outside of the remote repository's context.
1129 if (strncmp(refname
, "got/", 4) == 0)
1131 if (strncmp(refname
, "remotes/", 8) == 0)
1134 if (strncmp(wanted_ref
, "refs/", 5) == 0)
1137 /* Allow prefix match. */
1138 if (got_path_is_child(refname
, wanted_ref
, strlen(wanted_ref
)))
1141 /* Allow exact match. */
1142 return (strcmp(refname
, wanted_ref
) == 0);
1146 is_wanted_ref(struct got_pathlist_head
*wanted_refs
, const char *refname
)
1148 struct got_pathlist_entry
*pe
;
1150 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1151 if (match_wanted_ref(refname
, pe
->path
))
1158 static const struct got_error
*
1159 create_wanted_ref(const char *refname
, struct got_object_id
*id
,
1160 const char *remote_repo_name
, int verbosity
, struct got_repository
*repo
)
1162 const struct got_error
*err
;
1163 char *remote_refname
;
1165 if (strncmp("refs/", refname
, 5) == 0)
1168 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
1169 remote_repo_name
, refname
) == -1)
1170 return got_error_from_errno("asprintf");
1172 err
= create_ref(remote_refname
, id
, verbosity
, repo
);
1173 free(remote_refname
);
1177 static const struct got_error
*
1178 create_gotconfig(const char *proto
, const char *host
, const char *port
,
1179 const char *remote_repo_path
, const char *default_branch
,
1180 int fetch_all_branches
, struct got_pathlist_head
*wanted_branches
,
1181 struct got_pathlist_head
*wanted_refs
, int mirror_references
,
1182 struct got_repository
*repo
)
1184 const struct got_error
*err
= NULL
;
1185 char *gotconfig_path
= NULL
;
1186 char *gotconfig
= NULL
;
1187 FILE *gotconfig_file
= NULL
;
1188 const char *branchname
= NULL
;
1189 char *branches
= NULL
, *refs
= NULL
;
1192 if (!fetch_all_branches
&& !TAILQ_EMPTY(wanted_branches
)) {
1193 struct got_pathlist_entry
*pe
;
1194 TAILQ_FOREACH(pe
, wanted_branches
, entry
) {
1196 branchname
= pe
->path
;
1197 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1199 if (asprintf(&s
, "%s\"%s\" ",
1200 branches
? branches
: "", branchname
) == -1) {
1201 err
= got_error_from_errno("asprintf");
1207 } else if (!fetch_all_branches
&& default_branch
) {
1208 branchname
= default_branch
;
1209 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1211 if (asprintf(&branches
, "\"%s\" ", branchname
) == -1) {
1212 err
= got_error_from_errno("asprintf");
1216 if (!TAILQ_EMPTY(wanted_refs
)) {
1217 struct got_pathlist_entry
*pe
;
1218 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1220 const char *refname
= pe
->path
;
1221 if (strncmp(refname
, "refs/", 5) == 0)
1223 if (asprintf(&s
, "%s\"%s\" ",
1224 refs
? refs
: "", refname
) == -1) {
1225 err
= got_error_from_errno("asprintf");
1233 /* Create got.conf(5). */
1234 gotconfig_path
= got_repo_get_path_gotconfig(repo
);
1235 if (gotconfig_path
== NULL
) {
1236 err
= got_error_from_errno("got_repo_get_path_gotconfig");
1239 gotconfig_file
= fopen(gotconfig_path
, "a");
1240 if (gotconfig_file
== NULL
) {
1241 err
= got_error_from_errno2("fopen", gotconfig_path
);
1244 if (asprintf(&gotconfig
,
1249 "\trepository \"%s\"\n"
1255 GOT_FETCH_DEFAULT_REMOTE_NAME
, host
, proto
,
1256 port
? "\tport " : "", port
? port
: "", port
? "\n" : "",
1257 remote_repo_path
, branches
? "\tbranch { " : "",
1258 branches
? branches
: "", branches
? "}\n" : "",
1259 refs
? "\treference { " : "", refs
? refs
: "", refs
? "}\n" : "",
1260 mirror_references
? "\tmirror-references yes\n" : "",
1261 fetch_all_branches
? "\tfetch-all-branches yes\n" : "") == -1) {
1262 err
= got_error_from_errno("asprintf");
1265 n
= fwrite(gotconfig
, 1, strlen(gotconfig
), gotconfig_file
);
1266 if (n
!= strlen(gotconfig
)) {
1267 err
= got_ferror(gotconfig_file
, GOT_ERR_IO
);
1272 if (gotconfig_file
&& fclose(gotconfig_file
) == EOF
&& err
== NULL
)
1273 err
= got_error_from_errno2("fclose", gotconfig_path
);
1274 free(gotconfig_path
);
1279 static const struct got_error
*
1280 create_gitconfig(const char *git_url
, const char *default_branch
,
1281 int fetch_all_branches
, struct got_pathlist_head
*wanted_branches
,
1282 struct got_pathlist_head
*wanted_refs
, int mirror_references
,
1283 struct got_repository
*repo
)
1285 const struct got_error
*err
= NULL
;
1286 char *gitconfig_path
= NULL
;
1287 char *gitconfig
= NULL
;
1288 FILE *gitconfig_file
= NULL
;
1289 char *branches
= NULL
, *refs
= NULL
;
1290 const char *branchname
;
1293 /* Create a config file Git can understand. */
1294 gitconfig_path
= got_repo_get_path_gitconfig(repo
);
1295 if (gitconfig_path
== NULL
) {
1296 err
= got_error_from_errno("got_repo_get_path_gitconfig");
1299 gitconfig_file
= fopen(gitconfig_path
, "a");
1300 if (gitconfig_file
== NULL
) {
1301 err
= got_error_from_errno2("fopen", gitconfig_path
);
1304 if (fetch_all_branches
) {
1305 if (mirror_references
) {
1306 if (asprintf(&branches
,
1307 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1308 err
= got_error_from_errno("asprintf");
1311 } else if (asprintf(&branches
,
1312 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1313 GOT_FETCH_DEFAULT_REMOTE_NAME
) == -1) {
1314 err
= got_error_from_errno("asprintf");
1317 } else if (!TAILQ_EMPTY(wanted_branches
)) {
1318 struct got_pathlist_entry
*pe
;
1319 TAILQ_FOREACH(pe
, wanted_branches
, entry
) {
1321 branchname
= pe
->path
;
1322 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1324 if (mirror_references
) {
1326 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1327 branches
? branches
: "",
1328 branchname
, branchname
) == -1) {
1329 err
= got_error_from_errno("asprintf");
1332 } else if (asprintf(&s
,
1333 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1334 branches
? branches
: "",
1335 branchname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1336 branchname
) == -1) {
1337 err
= got_error_from_errno("asprintf");
1345 * If the server specified a default branch, use just that one.
1346 * Otherwise fall back to fetching all branches on next fetch.
1348 if (default_branch
) {
1349 branchname
= default_branch
;
1350 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1353 branchname
= "*"; /* fall back to all branches */
1354 if (mirror_references
) {
1355 if (asprintf(&branches
,
1356 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1357 branchname
, branchname
) == -1) {
1358 err
= got_error_from_errno("asprintf");
1361 } else if (asprintf(&branches
,
1362 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1363 branchname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1364 branchname
) == -1) {
1365 err
= got_error_from_errno("asprintf");
1369 if (!TAILQ_EMPTY(wanted_refs
)) {
1370 struct got_pathlist_entry
*pe
;
1371 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1373 const char *refname
= pe
->path
;
1374 if (strncmp(refname
, "refs/", 5) == 0)
1376 if (mirror_references
) {
1378 "%s\tfetch = refs/%s:refs/%s\n",
1379 refs
? refs
: "", refname
, refname
) == -1) {
1380 err
= got_error_from_errno("asprintf");
1383 } else if (asprintf(&s
,
1384 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1386 refname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1388 err
= got_error_from_errno("asprintf");
1396 if (asprintf(&gitconfig
,
1401 "\tfetch = refs/tags/*:refs/tags/*\n",
1402 GOT_FETCH_DEFAULT_REMOTE_NAME
, git_url
, branches
? branches
: "",
1403 refs
? refs
: "") == -1) {
1404 err
= got_error_from_errno("asprintf");
1407 n
= fwrite(gitconfig
, 1, strlen(gitconfig
), gitconfig_file
);
1408 if (n
!= strlen(gitconfig
)) {
1409 err
= got_ferror(gitconfig_file
, GOT_ERR_IO
);
1413 if (gitconfig_file
&& fclose(gitconfig_file
) == EOF
&& err
== NULL
)
1414 err
= got_error_from_errno2("fclose", gitconfig_path
);
1415 free(gitconfig_path
);
1420 static const struct got_error
*
1421 create_config_files(const char *proto
, const char *host
, const char *port
,
1422 const char *remote_repo_path
, const char *git_url
, int fetch_all_branches
,
1423 int mirror_references
, struct got_pathlist_head
*symrefs
,
1424 struct got_pathlist_head
*wanted_branches
,
1425 struct got_pathlist_head
*wanted_refs
, struct got_repository
*repo
)
1427 const struct got_error
*err
= NULL
;
1428 const char *default_branch
= NULL
;
1429 struct got_pathlist_entry
*pe
;
1432 * If we asked for a set of wanted branches then use the first
1435 if (!TAILQ_EMPTY(wanted_branches
)) {
1436 pe
= TAILQ_FIRST(wanted_branches
);
1437 default_branch
= pe
->path
;
1439 /* First HEAD ref listed by server is the default branch. */
1440 TAILQ_FOREACH(pe
, symrefs
, entry
) {
1441 const char *refname
= pe
->path
;
1442 const char *target
= pe
->data
;
1444 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
1447 default_branch
= target
;
1452 /* Create got.conf(5). */
1453 err
= create_gotconfig(proto
, host
, port
, remote_repo_path
,
1454 default_branch
, fetch_all_branches
, wanted_branches
,
1455 wanted_refs
, mirror_references
, repo
);
1459 /* Create a config file Git can understand. */
1460 return create_gitconfig(git_url
, default_branch
, fetch_all_branches
,
1461 wanted_branches
, wanted_refs
, mirror_references
, repo
);
1464 static const struct got_error
*
1465 cmd_clone(int argc
, char *argv
[])
1467 const struct got_error
*error
= NULL
;
1468 const char *uri
, *dirname
;
1469 char *proto
, *host
, *port
, *repo_name
, *server_path
;
1470 char *default_destdir
= NULL
, *id_str
= NULL
;
1471 const char *repo_path
;
1472 struct got_repository
*repo
= NULL
;
1473 struct got_pathlist_head refs
, symrefs
, wanted_branches
, wanted_refs
;
1474 struct got_pathlist_entry
*pe
;
1475 struct got_object_id
*pack_hash
= NULL
;
1476 int ch
, fetchfd
= -1, fetchstatus
;
1477 pid_t fetchpid
= -1;
1478 struct got_fetch_progress_arg fpa
;
1479 char *git_url
= NULL
;
1480 int verbosity
= 0, fetch_all_branches
= 0, mirror_references
= 0;
1481 int list_refs_only
= 0;
1484 TAILQ_INIT(&symrefs
);
1485 TAILQ_INIT(&wanted_branches
);
1486 TAILQ_INIT(&wanted_refs
);
1488 while ((ch
= getopt(argc
, argv
, "ab:lmvqR:")) != -1) {
1491 fetch_all_branches
= 1;
1494 error
= got_pathlist_append(&wanted_branches
,
1503 mirror_references
= 1;
1508 else if (verbosity
< 3)
1515 error
= got_pathlist_append(&wanted_refs
,
1528 if (fetch_all_branches
&& !TAILQ_EMPTY(&wanted_branches
))
1529 option_conflict('a', 'b');
1530 if (list_refs_only
) {
1531 if (!TAILQ_EMPTY(&wanted_branches
))
1532 option_conflict('l', 'b');
1533 if (fetch_all_branches
)
1534 option_conflict('l', 'a');
1535 if (mirror_references
)
1536 option_conflict('l', 'm');
1537 if (!TAILQ_EMPTY(&wanted_refs
))
1538 option_conflict('l', 'R');
1550 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
1555 if (asprintf(&git_url
, "%s://%s%s%s%s%s", proto
,
1556 host
, port
? ":" : "", port
? port
: "",
1557 server_path
[0] != '/' ? "/" : "", server_path
) == -1) {
1558 error
= got_error_from_errno("asprintf");
1562 if (strcmp(proto
, "git") == 0) {
1564 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 "sendfd dns inet unveil", NULL
) == -1)
1568 } else if (strcmp(proto
, "git+ssh") == 0 ||
1569 strcmp(proto
, "ssh") == 0) {
1571 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1572 "sendfd unveil", NULL
) == -1)
1575 } else if (strcmp(proto
, "http") == 0 ||
1576 strcmp(proto
, "git+http") == 0) {
1577 error
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
1580 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
1583 if (dirname
== NULL
) {
1584 if (asprintf(&default_destdir
, "%s.git", repo_name
) == -1) {
1585 error
= got_error_from_errno("asprintf");
1588 repo_path
= default_destdir
;
1590 repo_path
= dirname
;
1592 if (!list_refs_only
) {
1593 error
= got_path_mkdir(repo_path
);
1595 (!(error
->code
== GOT_ERR_ERRNO
&& errno
== EISDIR
) &&
1596 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
)))
1598 if (!got_path_dir_is_empty(repo_path
)) {
1599 error
= got_error_path(repo_path
,
1600 GOT_ERR_DIR_NOT_EMPTY
);
1605 error
= got_dial_apply_unveil(proto
);
1609 error
= apply_unveil(repo_path
, 0, NULL
);
1614 printf("Connecting to %s%s%s\n", host
,
1615 port
? ":" : "", port
? port
: "");
1617 error
= got_fetch_connect(&fetchpid
, &fetchfd
, proto
, host
, port
,
1618 server_path
, verbosity
);
1622 if (!list_refs_only
) {
1623 error
= got_repo_init(repo_path
);
1626 error
= got_repo_open(&repo
, repo_path
, NULL
);
1631 fpa
.last_scaled_size
[0] = '\0';
1632 fpa
.last_p_indexed
= -1;
1633 fpa
.last_p_resolved
= -1;
1634 fpa
.verbosity
= verbosity
;
1635 fpa
.create_configs
= 1;
1636 fpa
.configs_created
= 0;
1638 fpa
.config_info
.symrefs
= &symrefs
;
1639 fpa
.config_info
.wanted_branches
= &wanted_branches
;
1640 fpa
.config_info
.wanted_refs
= &wanted_refs
;
1641 fpa
.config_info
.proto
= proto
;
1642 fpa
.config_info
.host
= host
;
1643 fpa
.config_info
.port
= port
;
1644 fpa
.config_info
.remote_repo_path
= server_path
;
1645 fpa
.config_info
.git_url
= git_url
;
1646 fpa
.config_info
.fetch_all_branches
= fetch_all_branches
;
1647 fpa
.config_info
.mirror_references
= mirror_references
;
1648 error
= got_fetch_pack(&pack_hash
, &refs
, &symrefs
,
1649 GOT_FETCH_DEFAULT_REMOTE_NAME
, mirror_references
,
1650 fetch_all_branches
, &wanted_branches
, &wanted_refs
,
1651 list_refs_only
, verbosity
, fetchfd
, repo
,
1652 fetch_progress
, &fpa
);
1656 if (list_refs_only
) {
1657 error
= list_remote_refs(&symrefs
, &refs
);
1661 error
= got_object_id_str(&id_str
, pack_hash
);
1665 printf("\nFetched %s.pack\n", id_str
);
1668 /* Set up references provided with the pack file. */
1669 TAILQ_FOREACH(pe
, &refs
, entry
) {
1670 const char *refname
= pe
->path
;
1671 struct got_object_id
*id
= pe
->data
;
1672 char *remote_refname
;
1674 if (is_wanted_ref(&wanted_refs
, refname
) &&
1675 !mirror_references
) {
1676 error
= create_wanted_ref(refname
, id
,
1677 GOT_FETCH_DEFAULT_REMOTE_NAME
,
1678 verbosity
- 1, repo
);
1684 error
= create_ref(refname
, id
, verbosity
- 1, repo
);
1688 if (mirror_references
)
1691 if (strncmp("refs/heads/", refname
, 11) != 0)
1694 if (asprintf(&remote_refname
,
1695 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1696 refname
+ 11) == -1) {
1697 error
= got_error_from_errno("asprintf");
1700 error
= create_ref(remote_refname
, id
, verbosity
- 1, repo
);
1701 free(remote_refname
);
1706 /* Set the HEAD reference if the server provided one. */
1707 TAILQ_FOREACH(pe
, &symrefs
, entry
) {
1708 struct got_reference
*target_ref
;
1709 const char *refname
= pe
->path
;
1710 const char *target
= pe
->data
;
1711 char *remote_refname
= NULL
, *remote_target
= NULL
;
1713 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
1716 error
= got_ref_open(&target_ref
, repo
, target
, 0);
1718 if (error
->code
== GOT_ERR_NOT_REF
) {
1725 error
= create_symref(refname
, target_ref
, verbosity
, repo
);
1726 got_ref_close(target_ref
);
1730 if (mirror_references
)
1733 if (strncmp("refs/heads/", target
, 11) != 0)
1736 if (asprintf(&remote_refname
,
1737 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1739 error
= got_error_from_errno("asprintf");
1742 if (asprintf(&remote_target
,
1743 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1744 target
+ 11) == -1) {
1745 error
= got_error_from_errno("asprintf");
1746 free(remote_refname
);
1749 error
= got_ref_open(&target_ref
, repo
, remote_target
, 0);
1751 free(remote_refname
);
1752 free(remote_target
);
1753 if (error
->code
== GOT_ERR_NOT_REF
) {
1759 error
= create_symref(remote_refname
, target_ref
,
1760 verbosity
- 1, repo
);
1761 free(remote_refname
);
1762 free(remote_target
);
1763 got_ref_close(target_ref
);
1769 * We failed to set the HEAD reference. If we asked for
1770 * a set of wanted branches use the first of one of those
1771 * which could be fetched instead.
1773 TAILQ_FOREACH(pe
, &wanted_branches
, entry
) {
1774 const char *target
= pe
->path
;
1775 struct got_reference
*target_ref
;
1777 error
= got_ref_open(&target_ref
, repo
, target
, 0);
1779 if (error
->code
== GOT_ERR_NOT_REF
) {
1786 error
= create_symref(GOT_REF_HEAD
, target_ref
,
1788 got_ref_close(target_ref
);
1796 printf("Created %s repository '%s'\n",
1797 mirror_references
? "mirrored" : "cloned", repo_path
);
1800 if (kill(fetchpid
, SIGTERM
) == -1)
1801 error
= got_error_from_errno("kill");
1802 if (waitpid(fetchpid
, &fetchstatus
, 0) == -1 && error
== NULL
)
1803 error
= got_error_from_errno("waitpid");
1805 if (fetchfd
!= -1 && close(fetchfd
) == -1 && error
== NULL
)
1806 error
= got_error_from_errno("close");
1808 const struct got_error
*close_err
= got_repo_close(repo
);
1812 TAILQ_FOREACH(pe
, &refs
, entry
) {
1813 free((void *)pe
->path
);
1816 got_pathlist_free(&refs
);
1817 TAILQ_FOREACH(pe
, &symrefs
, entry
) {
1818 free((void *)pe
->path
);
1821 got_pathlist_free(&symrefs
);
1822 got_pathlist_free(&wanted_branches
);
1823 got_pathlist_free(&wanted_refs
);
1830 free(default_destdir
);
1835 static const struct got_error
*
1836 update_ref(struct got_reference
*ref
, struct got_object_id
*new_id
,
1837 int replace_tags
, int verbosity
, struct got_repository
*repo
)
1839 const struct got_error
*err
= NULL
;
1840 char *new_id_str
= NULL
;
1841 struct got_object_id
*old_id
= NULL
;
1843 err
= got_object_id_str(&new_id_str
, new_id
);
1847 if (!replace_tags
&&
1848 strncmp(got_ref_get_name(ref
), "refs/tags/", 10) == 0) {
1849 err
= got_ref_resolve(&old_id
, repo
, ref
);
1852 if (got_object_id_cmp(old_id
, new_id
) == 0)
1854 if (verbosity
>= 0) {
1855 printf("Rejecting update of existing tag %s: %s\n",
1856 got_ref_get_name(ref
), new_id_str
);
1861 if (got_ref_is_symbolic(ref
)) {
1862 if (verbosity
>= 0) {
1863 printf("Replacing reference %s: %s\n",
1864 got_ref_get_name(ref
),
1865 got_ref_get_symref_target(ref
));
1867 err
= got_ref_change_symref_to_ref(ref
, new_id
);
1870 err
= got_ref_write(ref
, repo
);
1874 err
= got_ref_resolve(&old_id
, repo
, ref
);
1877 if (got_object_id_cmp(old_id
, new_id
) == 0)
1880 err
= got_ref_change_ref(ref
, new_id
);
1883 err
= got_ref_write(ref
, repo
);
1889 printf("Updated %s: %s\n", got_ref_get_name(ref
),
1897 static const struct got_error
*
1898 update_symref(const char *refname
, struct got_reference
*target_ref
,
1899 int verbosity
, struct got_repository
*repo
)
1901 const struct got_error
*err
= NULL
, *unlock_err
;
1902 struct got_reference
*symref
;
1903 int symref_is_locked
= 0;
1905 err
= got_ref_open(&symref
, repo
, refname
, 1);
1907 if (err
->code
!= GOT_ERR_NOT_REF
)
1909 err
= got_ref_alloc_symref(&symref
, refname
, target_ref
);
1913 err
= got_ref_write(symref
, repo
);
1918 printf("Created reference %s: %s\n",
1919 got_ref_get_name(symref
),
1920 got_ref_get_symref_target(symref
));
1922 symref_is_locked
= 1;
1924 if (strcmp(got_ref_get_symref_target(symref
),
1925 got_ref_get_name(target_ref
)) == 0)
1928 err
= got_ref_change_symref(symref
,
1929 got_ref_get_name(target_ref
));
1933 err
= got_ref_write(symref
, repo
);
1938 printf("Updated %s: %s\n", got_ref_get_name(symref
),
1939 got_ref_get_symref_target(symref
));
1943 if (symref_is_locked
) {
1944 unlock_err
= got_ref_unlock(symref
);
1945 if (unlock_err
&& err
== NULL
)
1948 got_ref_close(symref
);
1955 fprintf(stderr
, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1956 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1957 "[remote-repository-name]\n",
1962 static const struct got_error
*
1963 delete_missing_ref(struct got_reference
*ref
,
1964 int verbosity
, struct got_repository
*repo
)
1966 const struct got_error
*err
= NULL
;
1967 struct got_object_id
*id
= NULL
;
1968 char *id_str
= NULL
;
1970 if (got_ref_is_symbolic(ref
)) {
1971 err
= got_ref_delete(ref
, repo
);
1974 if (verbosity
>= 0) {
1975 printf("Deleted %s: %s\n",
1976 got_ref_get_name(ref
),
1977 got_ref_get_symref_target(ref
));
1980 err
= got_ref_resolve(&id
, repo
, ref
);
1983 err
= got_object_id_str(&id_str
, id
);
1987 err
= got_ref_delete(ref
, repo
);
1990 if (verbosity
>= 0) {
1991 printf("Deleted %s: %s\n",
1992 got_ref_get_name(ref
), id_str
);
2001 static const struct got_error
*
2002 delete_missing_refs(struct got_pathlist_head
*their_refs
,
2003 struct got_pathlist_head
*their_symrefs
,
2004 const struct got_remote_repo
*remote
,
2005 int verbosity
, struct got_repository
*repo
)
2007 const struct got_error
*err
= NULL
, *unlock_err
;
2008 struct got_reflist_head my_refs
;
2009 struct got_reflist_entry
*re
;
2010 struct got_pathlist_entry
*pe
;
2011 char *remote_namespace
= NULL
;
2012 char *local_refname
= NULL
;
2014 TAILQ_INIT(&my_refs
);
2016 if (asprintf(&remote_namespace
, "refs/remotes/%s/", remote
->name
)
2018 return got_error_from_errno("asprintf");
2020 err
= got_ref_list(&my_refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
2024 TAILQ_FOREACH(re
, &my_refs
, entry
) {
2025 const char *refname
= got_ref_get_name(re
->ref
);
2026 const char *their_refname
;
2028 if (remote
->mirror_references
) {
2029 their_refname
= refname
;
2031 if (strncmp(refname
, remote_namespace
,
2032 strlen(remote_namespace
)) == 0) {
2033 if (strcmp(refname
+ strlen(remote_namespace
),
2036 if (asprintf(&local_refname
, "refs/heads/%s",
2037 refname
+ strlen(remote_namespace
)) == -1) {
2038 err
= got_error_from_errno("asprintf");
2041 } else if (strncmp(refname
, "refs/tags/", 10) != 0)
2044 their_refname
= local_refname
;
2047 TAILQ_FOREACH(pe
, their_refs
, entry
) {
2048 if (strcmp(their_refname
, pe
->path
) == 0)
2054 TAILQ_FOREACH(pe
, their_symrefs
, entry
) {
2055 if (strcmp(their_refname
, pe
->path
) == 0)
2061 err
= delete_missing_ref(re
->ref
, verbosity
, repo
);
2065 if (local_refname
) {
2066 struct got_reference
*ref
;
2067 err
= got_ref_open(&ref
, repo
, local_refname
, 1);
2069 if (err
->code
!= GOT_ERR_NOT_REF
)
2071 free(local_refname
);
2072 local_refname
= NULL
;
2075 err
= delete_missing_ref(ref
, verbosity
, repo
);
2078 unlock_err
= got_ref_unlock(ref
);
2080 if (unlock_err
&& err
== NULL
) {
2085 free(local_refname
);
2086 local_refname
= NULL
;
2090 free(remote_namespace
);
2091 free(local_refname
);
2095 static const struct got_error
*
2096 update_wanted_ref(const char *refname
, struct got_object_id
*id
,
2097 const char *remote_repo_name
, int verbosity
, struct got_repository
*repo
)
2099 const struct got_error
*err
, *unlock_err
;
2100 char *remote_refname
;
2101 struct got_reference
*ref
;
2103 if (strncmp("refs/", refname
, 5) == 0)
2106 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
2107 remote_repo_name
, refname
) == -1)
2108 return got_error_from_errno("asprintf");
2110 err
= got_ref_open(&ref
, repo
, remote_refname
, 1);
2112 if (err
->code
!= GOT_ERR_NOT_REF
)
2114 err
= create_ref(remote_refname
, id
, verbosity
, repo
);
2116 err
= update_ref(ref
, id
, 0, verbosity
, repo
);
2117 unlock_err
= got_ref_unlock(ref
);
2118 if (unlock_err
&& err
== NULL
)
2123 free(remote_refname
);
2127 static const struct got_error
*
2128 delete_ref(struct got_repository
*repo
, struct got_reference
*ref
)
2130 const struct got_error
*err
= NULL
;
2131 struct got_object_id
*id
= NULL
;
2132 char *id_str
= NULL
;
2135 if (got_ref_is_symbolic(ref
)) {
2136 target
= got_ref_get_symref_target(ref
);
2138 err
= got_ref_resolve(&id
, repo
, ref
);
2141 err
= got_object_id_str(&id_str
, id
);
2147 err
= got_ref_delete(ref
, repo
);
2151 printf("Deleted %s: %s\n", got_ref_get_name(ref
), target
);
2158 static const struct got_error
*
2159 delete_refs_for_remote(struct got_repository
*repo
, const char *remote_name
)
2161 const struct got_error
*err
= NULL
;
2162 struct got_reflist_head refs
;
2163 struct got_reflist_entry
*re
;
2168 if (asprintf(&prefix
, "refs/remotes/%s", remote_name
) == -1) {
2169 err
= got_error_from_errno("asprintf");
2172 err
= got_ref_list(&refs
, repo
, prefix
, got_ref_cmp_by_name
, NULL
);
2176 TAILQ_FOREACH(re
, &refs
, entry
)
2177 delete_ref(repo
, re
->ref
);
2179 got_ref_list_free(&refs
);
2183 static const struct got_error
*
2184 cmd_fetch(int argc
, char *argv
[])
2186 const struct got_error
*error
= NULL
, *unlock_err
;
2187 char *cwd
= NULL
, *repo_path
= NULL
;
2188 const char *remote_name
;
2189 char *proto
= NULL
, *host
= NULL
, *port
= NULL
;
2190 char *repo_name
= NULL
, *server_path
= NULL
;
2191 const struct got_remote_repo
*remotes
, *remote
= NULL
;
2193 char *id_str
= NULL
;
2194 struct got_repository
*repo
= NULL
;
2195 struct got_worktree
*worktree
= NULL
;
2196 const struct got_gotconfig
*repo_conf
= NULL
, *worktree_conf
= NULL
;
2197 struct got_pathlist_head refs
, symrefs
, wanted_branches
, wanted_refs
;
2198 struct got_pathlist_entry
*pe
;
2199 struct got_object_id
*pack_hash
= NULL
;
2200 int i
, ch
, fetchfd
= -1, fetchstatus
;
2201 pid_t fetchpid
= -1;
2202 struct got_fetch_progress_arg fpa
;
2203 int verbosity
= 0, fetch_all_branches
= 0, list_refs_only
= 0;
2204 int delete_refs
= 0, replace_tags
= 0, delete_remote
= 0;
2207 TAILQ_INIT(&symrefs
);
2208 TAILQ_INIT(&wanted_branches
);
2209 TAILQ_INIT(&wanted_refs
);
2211 while ((ch
= getopt(argc
, argv
, "ab:dlr:tvqR:X")) != -1) {
2214 fetch_all_branches
= 1;
2217 error
= got_pathlist_append(&wanted_branches
,
2229 repo_path
= realpath(optarg
, NULL
);
2230 if (repo_path
== NULL
)
2231 return got_error_from_errno2("realpath",
2233 got_path_strip_trailing_slashes(repo_path
);
2241 else if (verbosity
< 3)
2248 error
= got_pathlist_append(&wanted_refs
,
2264 if (fetch_all_branches
&& !TAILQ_EMPTY(&wanted_branches
))
2265 option_conflict('a', 'b');
2266 if (list_refs_only
) {
2267 if (!TAILQ_EMPTY(&wanted_branches
))
2268 option_conflict('l', 'b');
2269 if (fetch_all_branches
)
2270 option_conflict('l', 'a');
2272 option_conflict('l', 'd');
2274 option_conflict('l', 'X');
2276 if (delete_remote
) {
2277 if (fetch_all_branches
)
2278 option_conflict('X', 'a');
2279 if (!TAILQ_EMPTY(&wanted_branches
))
2280 option_conflict('X', 'b');
2282 option_conflict('X', 'd');
2284 option_conflict('X', 't');
2285 if (!TAILQ_EMPTY(&wanted_refs
))
2286 option_conflict('X', 'R');
2291 errx(1, "-X option requires a remote name");
2292 remote_name
= GOT_FETCH_DEFAULT_REMOTE_NAME
;
2293 } else if (argc
== 1)
2294 remote_name
= argv
[0];
2298 cwd
= getcwd(NULL
, 0);
2300 error
= got_error_from_errno("getcwd");
2304 if (repo_path
== NULL
) {
2305 error
= got_worktree_open(&worktree
, cwd
);
2306 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
2312 strdup(got_worktree_get_repo_path(worktree
));
2313 if (repo_path
== NULL
)
2314 error
= got_error_from_errno("strdup");
2318 repo_path
= strdup(cwd
);
2319 if (repo_path
== NULL
) {
2320 error
= got_error_from_errno("strdup");
2326 error
= got_repo_open(&repo
, repo_path
, NULL
);
2330 if (delete_remote
) {
2331 error
= delete_refs_for_remote(repo
, remote_name
);
2332 goto done
; /* nothing else to do */
2336 worktree_conf
= got_worktree_get_gotconfig(worktree
);
2337 if (worktree_conf
) {
2338 got_gotconfig_get_remotes(&nremotes
, &remotes
,
2340 for (i
= 0; i
< nremotes
; i
++) {
2341 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2342 remote
= &remotes
[i
];
2348 if (remote
== NULL
) {
2349 repo_conf
= got_repo_get_gotconfig(repo
);
2351 got_gotconfig_get_remotes(&nremotes
, &remotes
,
2353 for (i
= 0; i
< nremotes
; i
++) {
2354 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2355 remote
= &remotes
[i
];
2361 if (remote
== NULL
) {
2362 got_repo_get_gitconfig_remotes(&nremotes
, &remotes
, repo
);
2363 for (i
= 0; i
< nremotes
; i
++) {
2364 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2365 remote
= &remotes
[i
];
2370 if (remote
== NULL
) {
2371 error
= got_error_path(remote_name
, GOT_ERR_NO_REMOTE
);
2375 if (TAILQ_EMPTY(&wanted_branches
)) {
2376 if (!fetch_all_branches
)
2377 fetch_all_branches
= remote
->fetch_all_branches
;
2378 for (i
= 0; i
< remote
->nfetch_branches
; i
++) {
2379 got_pathlist_append(&wanted_branches
,
2380 remote
->fetch_branches
[i
], NULL
);
2383 if (TAILQ_EMPTY(&wanted_refs
)) {
2384 for (i
= 0; i
< remote
->nfetch_refs
; i
++) {
2385 got_pathlist_append(&wanted_refs
,
2386 remote
->fetch_refs
[i
], NULL
);
2390 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
2391 &repo_name
, remote
->fetch_url
);
2395 if (strcmp(proto
, "git") == 0) {
2397 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2398 "sendfd dns inet unveil", NULL
) == -1)
2401 } else if (strcmp(proto
, "git+ssh") == 0 ||
2402 strcmp(proto
, "ssh") == 0) {
2404 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2405 "sendfd unveil", NULL
) == -1)
2408 } else if (strcmp(proto
, "http") == 0 ||
2409 strcmp(proto
, "git+http") == 0) {
2410 error
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
2413 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
2417 error
= got_dial_apply_unveil(proto
);
2421 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
2426 printf("Connecting to \"%s\" %s%s%s\n", remote
->name
, host
,
2427 port
? ":" : "", port
? port
: "");
2429 error
= got_fetch_connect(&fetchpid
, &fetchfd
, proto
, host
, port
,
2430 server_path
, verbosity
);
2434 fpa
.last_scaled_size
[0] = '\0';
2435 fpa
.last_p_indexed
= -1;
2436 fpa
.last_p_resolved
= -1;
2437 fpa
.verbosity
= verbosity
;
2439 fpa
.create_configs
= 0;
2440 fpa
.configs_created
= 0;
2441 memset(&fpa
.config_info
, 0, sizeof(fpa
.config_info
));
2442 error
= got_fetch_pack(&pack_hash
, &refs
, &symrefs
, remote
->name
,
2443 remote
->mirror_references
, fetch_all_branches
, &wanted_branches
,
2444 &wanted_refs
, list_refs_only
, verbosity
, fetchfd
, repo
,
2445 fetch_progress
, &fpa
);
2449 if (list_refs_only
) {
2450 error
= list_remote_refs(&symrefs
, &refs
);
2454 if (pack_hash
== NULL
) {
2456 printf("Already up-to-date\n");
2457 } else if (verbosity
>= 0) {
2458 error
= got_object_id_str(&id_str
, pack_hash
);
2461 printf("\nFetched %s.pack\n", id_str
);
2466 /* Update references provided with the pack file. */
2467 TAILQ_FOREACH(pe
, &refs
, entry
) {
2468 const char *refname
= pe
->path
;
2469 struct got_object_id
*id
= pe
->data
;
2470 struct got_reference
*ref
;
2471 char *remote_refname
;
2473 if (is_wanted_ref(&wanted_refs
, refname
) &&
2474 !remote
->mirror_references
) {
2475 error
= update_wanted_ref(refname
, id
,
2476 remote
->name
, verbosity
, repo
);
2482 if (remote
->mirror_references
||
2483 strncmp("refs/tags/", refname
, 10) == 0) {
2484 error
= got_ref_open(&ref
, repo
, refname
, 1);
2486 if (error
->code
!= GOT_ERR_NOT_REF
)
2488 error
= create_ref(refname
, id
, verbosity
,
2493 error
= update_ref(ref
, id
, replace_tags
,
2495 unlock_err
= got_ref_unlock(ref
);
2496 if (unlock_err
&& error
== NULL
)
2502 } else if (strncmp("refs/heads/", refname
, 11) == 0) {
2503 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
2504 remote_name
, refname
+ 11) == -1) {
2505 error
= got_error_from_errno("asprintf");
2509 error
= got_ref_open(&ref
, repo
, remote_refname
, 1);
2511 if (error
->code
!= GOT_ERR_NOT_REF
)
2513 error
= create_ref(remote_refname
, id
,
2518 error
= update_ref(ref
, id
, replace_tags
,
2520 unlock_err
= got_ref_unlock(ref
);
2521 if (unlock_err
&& error
== NULL
)
2528 /* Also create a local branch if none exists yet. */
2529 error
= got_ref_open(&ref
, repo
, refname
, 1);
2531 if (error
->code
!= GOT_ERR_NOT_REF
)
2533 error
= create_ref(refname
, id
, verbosity
,
2538 unlock_err
= got_ref_unlock(ref
);
2539 if (unlock_err
&& error
== NULL
)
2546 error
= delete_missing_refs(&refs
, &symrefs
, remote
,
2552 if (!remote
->mirror_references
) {
2553 /* Update remote HEAD reference if the server provided one. */
2554 TAILQ_FOREACH(pe
, &symrefs
, entry
) {
2555 struct got_reference
*target_ref
;
2556 const char *refname
= pe
->path
;
2557 const char *target
= pe
->data
;
2558 char *remote_refname
= NULL
, *remote_target
= NULL
;
2560 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
2563 if (strncmp("refs/heads/", target
, 11) != 0)
2566 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
2567 remote
->name
, refname
) == -1) {
2568 error
= got_error_from_errno("asprintf");
2571 if (asprintf(&remote_target
, "refs/remotes/%s/%s",
2572 remote
->name
, target
+ 11) == -1) {
2573 error
= got_error_from_errno("asprintf");
2574 free(remote_refname
);
2578 error
= got_ref_open(&target_ref
, repo
, remote_target
,
2581 free(remote_refname
);
2582 free(remote_target
);
2583 if (error
->code
== GOT_ERR_NOT_REF
) {
2589 error
= update_symref(remote_refname
, target_ref
,
2591 free(remote_refname
);
2592 free(remote_target
);
2593 got_ref_close(target_ref
);
2600 if (kill(fetchpid
, SIGTERM
) == -1)
2601 error
= got_error_from_errno("kill");
2602 if (waitpid(fetchpid
, &fetchstatus
, 0) == -1 && error
== NULL
)
2603 error
= got_error_from_errno("waitpid");
2605 if (fetchfd
!= -1 && close(fetchfd
) == -1 && error
== NULL
)
2606 error
= got_error_from_errno("close");
2608 const struct got_error
*close_err
= got_repo_close(repo
);
2613 got_worktree_close(worktree
);
2614 TAILQ_FOREACH(pe
, &refs
, entry
) {
2615 free((void *)pe
->path
);
2618 got_pathlist_free(&refs
);
2619 TAILQ_FOREACH(pe
, &symrefs
, entry
) {
2620 free((void *)pe
->path
);
2623 got_pathlist_free(&symrefs
);
2624 got_pathlist_free(&wanted_branches
);
2625 got_pathlist_free(&wanted_refs
);
2640 usage_checkout(void)
2642 fprintf(stderr
, "usage: %s checkout [-E] [-b branch] [-c commit] "
2643 "[-p prefix] [-q] repository-path [worktree-path]\n",
2649 show_worktree_base_ref_warning(void)
2651 fprintf(stderr
, "%s: warning: could not create a reference "
2652 "to the work tree's base commit; the commit could be "
2653 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2654 "repository writable and running 'got update' will prevent this\n",
2658 struct got_checkout_progress_arg
{
2659 const char *worktree_path
;
2660 int had_base_commit_ref_error
;
2664 static const struct got_error
*
2665 checkout_progress(void *arg
, unsigned char status
, const char *path
)
2667 struct got_checkout_progress_arg
*a
= arg
;
2669 /* Base commit bump happens silently. */
2670 if (status
== GOT_STATUS_BUMP_BASE
)
2673 if (status
== GOT_STATUS_BASE_REF_ERR
) {
2674 a
->had_base_commit_ref_error
= 1;
2678 while (path
[0] == '/')
2681 if (a
->verbosity
>= 0)
2682 printf("%c %s/%s\n", status
, a
->worktree_path
, path
);
2687 static const struct got_error
*
2688 check_cancelled(void *arg
)
2690 if (sigint_received
|| sigpipe_received
)
2691 return got_error(GOT_ERR_CANCELLED
);
2695 static const struct got_error
*
2696 check_linear_ancestry(struct got_object_id
*commit_id
,
2697 struct got_object_id
*base_commit_id
, int allow_forwards_in_time_only
,
2698 struct got_repository
*repo
)
2700 const struct got_error
*err
= NULL
;
2701 struct got_object_id
*yca_id
;
2703 err
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
2704 commit_id
, base_commit_id
, 1, repo
, check_cancelled
, NULL
);
2709 return got_error(GOT_ERR_ANCESTRY
);
2712 * Require a straight line of history between the target commit
2713 * and the work tree's base commit.
2715 * Non-linear situations such as this require a rebase:
2717 * (commit) D F (base_commit)
2725 * 'got update' only handles linear cases:
2726 * Update forwards in time: A (base/yca) - B - C - D (commit)
2727 * Update backwards in time: D (base) - C - B - A (commit/yca)
2729 if (allow_forwards_in_time_only
) {
2730 if (got_object_id_cmp(base_commit_id
, yca_id
) != 0)
2731 return got_error(GOT_ERR_ANCESTRY
);
2732 } else if (got_object_id_cmp(commit_id
, yca_id
) != 0 &&
2733 got_object_id_cmp(base_commit_id
, yca_id
) != 0)
2734 return got_error(GOT_ERR_ANCESTRY
);
2740 static const struct got_error
*
2741 check_same_branch(struct got_object_id
*commit_id
,
2742 struct got_reference
*head_ref
, struct got_object_id
*yca_id
,
2743 struct got_repository
*repo
)
2745 const struct got_error
*err
= NULL
;
2746 struct got_commit_graph
*graph
= NULL
;
2747 struct got_object_id
*head_commit_id
= NULL
;
2748 int is_same_branch
= 0;
2750 err
= got_ref_resolve(&head_commit_id
, repo
, head_ref
);
2754 if (got_object_id_cmp(head_commit_id
, commit_id
) == 0) {
2758 if (yca_id
&& got_object_id_cmp(commit_id
, yca_id
) == 0) {
2763 err
= got_commit_graph_open(&graph
, "/", 1);
2767 err
= got_commit_graph_iter_start(graph
, head_commit_id
, repo
,
2768 check_cancelled
, NULL
);
2773 struct got_object_id
*id
;
2774 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
2775 check_cancelled
, NULL
);
2777 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
2783 if (yca_id
&& got_object_id_cmp(id
, yca_id
) == 0)
2785 if (got_object_id_cmp(id
, commit_id
) == 0) {
2793 got_commit_graph_close(graph
);
2794 free(head_commit_id
);
2795 if (!err
&& !is_same_branch
)
2796 err
= got_error(GOT_ERR_ANCESTRY
);
2800 static const struct got_error
*
2801 checkout_ancestry_error(struct got_reference
*ref
, const char *commit_id_str
)
2803 static char msg
[512];
2804 const char *branch_name
;
2806 if (got_ref_is_symbolic(ref
))
2807 branch_name
= got_ref_get_symref_target(ref
);
2809 branch_name
= got_ref_get_name(ref
);
2811 if (strncmp("refs/heads/", branch_name
, 11) == 0)
2814 snprintf(msg
, sizeof(msg
),
2815 "target commit is not contained in branch '%s'; "
2816 "the branch to use must be specified with -b; "
2817 "if necessary a new branch can be created for "
2818 "this commit with 'got branch -c %s BRANCH_NAME'",
2819 branch_name
, commit_id_str
);
2821 return got_error_msg(GOT_ERR_ANCESTRY
, msg
);
2824 static const struct got_error
*
2825 cmd_checkout(int argc
, char *argv
[])
2827 const struct got_error
*error
= NULL
;
2828 struct got_repository
*repo
= NULL
;
2829 struct got_reference
*head_ref
= NULL
, *ref
= NULL
;
2830 struct got_worktree
*worktree
= NULL
;
2831 char *repo_path
= NULL
;
2832 char *worktree_path
= NULL
;
2833 const char *path_prefix
= "";
2834 const char *branch_name
= GOT_REF_HEAD
, *refname
= NULL
;
2835 char *commit_id_str
= NULL
;
2836 struct got_object_id
*commit_id
= NULL
;
2838 int ch
, same_path_prefix
, allow_nonempty
= 0, verbosity
= 0;
2839 struct got_pathlist_head paths
;
2840 struct got_checkout_progress_arg cpa
;
2844 while ((ch
= getopt(argc
, argv
, "b:c:Ep:q")) != -1) {
2847 branch_name
= optarg
;
2850 commit_id_str
= strdup(optarg
);
2851 if (commit_id_str
== NULL
)
2852 return got_error_from_errno("strdup");
2858 path_prefix
= optarg
;
2873 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2874 "unveil", NULL
) == -1)
2878 char *base
, *dotgit
;
2880 repo_path
= realpath(argv
[0], NULL
);
2881 if (repo_path
== NULL
)
2882 return got_error_from_errno2("realpath", argv
[0]);
2883 cwd
= getcwd(NULL
, 0);
2885 error
= got_error_from_errno("getcwd");
2892 error
= got_path_basename(&base
, path
);
2895 dotgit
= strstr(base
, ".git");
2898 if (asprintf(&worktree_path
, "%s/%s", cwd
, base
) == -1) {
2899 error
= got_error_from_errno("asprintf");
2904 } else if (argc
== 2) {
2905 repo_path
= realpath(argv
[0], NULL
);
2906 if (repo_path
== NULL
) {
2907 error
= got_error_from_errno2("realpath", argv
[0]);
2910 worktree_path
= realpath(argv
[1], NULL
);
2911 if (worktree_path
== NULL
) {
2912 if (errno
!= ENOENT
) {
2913 error
= got_error_from_errno2("realpath",
2917 worktree_path
= strdup(argv
[1]);
2918 if (worktree_path
== NULL
) {
2919 error
= got_error_from_errno("strdup");
2926 got_path_strip_trailing_slashes(repo_path
);
2927 got_path_strip_trailing_slashes(worktree_path
);
2929 error
= got_repo_open(&repo
, repo_path
, NULL
);
2933 /* Pre-create work tree path for unveil(2) */
2934 error
= got_path_mkdir(worktree_path
);
2936 if (!(error
->code
== GOT_ERR_ERRNO
&& errno
== EISDIR
) &&
2937 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
))
2939 if (!allow_nonempty
&&
2940 !got_path_dir_is_empty(worktree_path
)) {
2941 error
= got_error_path(worktree_path
,
2942 GOT_ERR_DIR_NOT_EMPTY
);
2947 error
= apply_unveil(got_repo_get_path(repo
), 0, worktree_path
);
2951 error
= got_ref_open(&head_ref
, repo
, branch_name
, 0);
2955 error
= got_worktree_init(worktree_path
, head_ref
, path_prefix
, repo
);
2956 if (error
!= NULL
&& !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
))
2959 error
= got_worktree_open(&worktree
, worktree_path
);
2963 error
= got_worktree_match_path_prefix(&same_path_prefix
, worktree
,
2967 if (!same_path_prefix
) {
2968 error
= got_error(GOT_ERR_PATH_PREFIX
);
2972 if (commit_id_str
) {
2973 struct got_reflist_head refs
;
2975 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
2979 error
= got_repo_match_object_id(&commit_id
, NULL
,
2980 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
2981 got_ref_list_free(&refs
);
2984 error
= check_linear_ancestry(commit_id
,
2985 got_worktree_get_base_commit_id(worktree
), 0, repo
);
2986 if (error
!= NULL
) {
2988 if (error
->code
== GOT_ERR_ANCESTRY
) {
2989 error
= checkout_ancestry_error(
2990 head_ref
, commit_id_str
);
2994 error
= check_same_branch(commit_id
, head_ref
, NULL
, repo
);
2996 if (error
->code
== GOT_ERR_ANCESTRY
) {
2997 error
= checkout_ancestry_error(
2998 head_ref
, commit_id_str
);
3002 error
= got_worktree_set_base_commit_id(worktree
, repo
,
3006 /* Expand potentially abbreviated commit ID string. */
3007 free(commit_id_str
);
3008 error
= got_object_id_str(&commit_id_str
, commit_id
);
3012 commit_id
= got_object_id_dup(
3013 got_worktree_get_base_commit_id(worktree
));
3014 if (commit_id
== NULL
) {
3015 error
= got_error_from_errno("got_object_id_dup");
3018 error
= got_object_id_str(&commit_id_str
, commit_id
);
3023 error
= got_pathlist_append(&paths
, "", NULL
);
3026 cpa
.worktree_path
= worktree_path
;
3027 cpa
.had_base_commit_ref_error
= 0;
3028 cpa
.verbosity
= verbosity
;
3029 error
= got_worktree_checkout_files(worktree
, &paths
, repo
,
3030 checkout_progress
, &cpa
, check_cancelled
, NULL
);
3034 if (got_ref_is_symbolic(head_ref
)) {
3035 error
= got_ref_resolve_symbolic(&ref
, repo
, head_ref
);
3038 refname
= got_ref_get_name(ref
);
3040 refname
= got_ref_get_name(head_ref
);
3041 printf("Checked out %s: %s\n", refname
, commit_id_str
);
3042 printf("Now shut up and hack\n");
3043 if (cpa
.had_base_commit_ref_error
)
3044 show_worktree_base_ref_warning();
3047 got_ref_close(head_ref
);
3050 got_pathlist_free(&paths
);
3051 free(commit_id_str
);
3054 free(worktree_path
);
3059 struct got_update_progress_arg
{
3071 print_update_progress_stats(struct got_update_progress_arg
*upa
)
3073 if (!upa
->did_something
)
3076 if (upa
->conflicts
> 0)
3077 printf("Files with new merge conflicts: %d\n", upa
->conflicts
);
3078 if (upa
->obstructed
> 0)
3079 printf("File paths obstructed by a non-regular file: %d\n",
3081 if (upa
->not_updated
> 0)
3082 printf("Files not updated because of existing merge "
3083 "conflicts: %d\n", upa
->not_updated
);
3087 * The meaning of some status codes differs between merge-style operations and
3088 * update operations. For example, the ! status code means "file was missing"
3089 * if changes were merged into the work tree, and "missing file was restored"
3090 * if the work tree was updated. This function should be used by any operation
3091 * which merges changes into the work tree without updating the work tree.
3094 print_merge_progress_stats(struct got_update_progress_arg
*upa
)
3096 if (!upa
->did_something
)
3099 if (upa
->conflicts
> 0)
3100 printf("Files with new merge conflicts: %d\n", upa
->conflicts
);
3101 if (upa
->obstructed
> 0)
3102 printf("File paths obstructed by a non-regular file: %d\n",
3104 if (upa
->missing
> 0)
3105 printf("Files which had incoming changes but could not be "
3106 "found in the work tree: %d\n", upa
->missing
);
3107 if (upa
->not_deleted
> 0)
3108 printf("Files not deleted due to differences in deleted "
3109 "content: %d\n", upa
->not_deleted
);
3110 if (upa
->unversioned
> 0)
3111 printf("Files not merged because an unversioned file was "
3112 "found in the work tree: %d\n", upa
->unversioned
);
3118 fprintf(stderr
, "usage: %s update [-b branch] [-c commit] [-q] "
3124 static const struct got_error
*
3125 update_progress(void *arg
, unsigned char status
, const char *path
)
3127 struct got_update_progress_arg
*upa
= arg
;
3129 if (status
== GOT_STATUS_EXISTS
||
3130 status
== GOT_STATUS_BASE_REF_ERR
)
3133 upa
->did_something
= 1;
3135 /* Base commit bump happens silently. */
3136 if (status
== GOT_STATUS_BUMP_BASE
)
3139 if (status
== GOT_STATUS_CONFLICT
)
3141 if (status
== GOT_STATUS_OBSTRUCTED
)
3143 if (status
== GOT_STATUS_CANNOT_UPDATE
)
3145 if (status
== GOT_STATUS_MISSING
)
3147 if (status
== GOT_STATUS_CANNOT_DELETE
)
3149 if (status
== GOT_STATUS_UNVERSIONED
)
3152 while (path
[0] == '/')
3154 if (upa
->verbosity
>= 0)
3155 printf("%c %s\n", status
, path
);
3160 static const struct got_error
*
3161 switch_head_ref(struct got_reference
*head_ref
,
3162 struct got_object_id
*commit_id
, struct got_worktree
*worktree
,
3163 struct got_repository
*repo
)
3165 const struct got_error
*err
= NULL
;
3167 int ref_has_moved
= 0;
3169 /* Trivial case: switching between two different references. */
3170 if (strcmp(got_ref_get_name(head_ref
),
3171 got_worktree_get_head_ref_name(worktree
)) != 0) {
3172 printf("Switching work tree from %s to %s\n",
3173 got_worktree_get_head_ref_name(worktree
),
3174 got_ref_get_name(head_ref
));
3175 return got_worktree_set_head_ref(worktree
, head_ref
);
3178 err
= check_linear_ancestry(commit_id
,
3179 got_worktree_get_base_commit_id(worktree
), 0, repo
);
3181 if (err
->code
!= GOT_ERR_ANCESTRY
)
3188 /* Switching to a rebased branch with the same reference name. */
3189 err
= got_object_id_str(&base_id_str
,
3190 got_worktree_get_base_commit_id(worktree
));
3193 printf("Reference %s now points at a different branch\n",
3194 got_worktree_get_head_ref_name(worktree
));
3195 printf("Switching work tree from %s to %s\n", base_id_str
,
3196 got_worktree_get_head_ref_name(worktree
));
3200 static const struct got_error
*
3201 check_rebase_or_histedit_in_progress(struct got_worktree
*worktree
)
3203 const struct got_error
*err
;
3206 err
= got_worktree_rebase_in_progress(&in_progress
, worktree
);
3210 return got_error(GOT_ERR_REBASING
);
3212 err
= got_worktree_histedit_in_progress(&in_progress
, worktree
);
3216 return got_error(GOT_ERR_HISTEDIT_BUSY
);
3221 static const struct got_error
*
3222 check_merge_in_progress(struct got_worktree
*worktree
,
3223 struct got_repository
*repo
)
3225 const struct got_error
*err
;
3228 err
= got_worktree_merge_in_progress(&in_progress
, worktree
, repo
);
3232 return got_error(GOT_ERR_MERGE_BUSY
);
3237 static const struct got_error
*
3238 get_worktree_paths_from_argv(struct got_pathlist_head
*paths
, int argc
,
3239 char *argv
[], struct got_worktree
*worktree
)
3241 const struct got_error
*err
= NULL
;
3243 struct got_pathlist_entry
*new;
3249 return got_error_from_errno("strdup");
3250 return got_pathlist_append(paths
, path
, NULL
);
3253 for (i
= 0; i
< argc
; i
++) {
3254 err
= got_worktree_resolve_path(&path
, worktree
, argv
[i
]);
3257 err
= got_pathlist_insert(&new, paths
, path
, NULL
);
3258 if (err
|| new == NULL
/* duplicate */) {
3268 static const struct got_error
*
3269 wrap_not_worktree_error(const struct got_error
*orig_err
,
3270 const char *cmdname
, const char *path
)
3272 const struct got_error
*err
;
3273 struct got_repository
*repo
;
3274 static char msg
[512];
3276 err
= got_repo_open(&repo
, path
, NULL
);
3280 snprintf(msg
, sizeof(msg
),
3281 "'got %s' needs a work tree in addition to a git repository\n"
3282 "Work trees can be checked out from this Git repository with "
3284 "The got(1) manual page contains more information.", cmdname
);
3285 err
= got_error_msg(GOT_ERR_NOT_WORKTREE
, msg
);
3286 got_repo_close(repo
);
3290 static const struct got_error
*
3291 cmd_update(int argc
, char *argv
[])
3293 const struct got_error
*error
= NULL
;
3294 struct got_repository
*repo
= NULL
;
3295 struct got_worktree
*worktree
= NULL
;
3296 char *worktree_path
= NULL
;
3297 struct got_object_id
*commit_id
= NULL
;
3298 char *commit_id_str
= NULL
;
3299 const char *branch_name
= NULL
;
3300 struct got_reference
*head_ref
= NULL
;
3301 struct got_pathlist_head paths
;
3302 struct got_pathlist_entry
*pe
;
3303 int ch
, verbosity
= 0;
3304 struct got_update_progress_arg upa
;
3308 while ((ch
= getopt(argc
, argv
, "b:c:q")) != -1) {
3311 branch_name
= optarg
;
3314 commit_id_str
= strdup(optarg
);
3315 if (commit_id_str
== NULL
)
3316 return got_error_from_errno("strdup");
3331 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3332 "unveil", NULL
) == -1)
3335 worktree_path
= getcwd(NULL
, 0);
3336 if (worktree_path
== NULL
) {
3337 error
= got_error_from_errno("getcwd");
3340 error
= got_worktree_open(&worktree
, worktree_path
);
3342 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
3343 error
= wrap_not_worktree_error(error
, "update",
3348 error
= check_rebase_or_histedit_in_progress(worktree
);
3352 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
3357 error
= apply_unveil(got_repo_get_path(repo
), 0,
3358 got_worktree_get_root_path(worktree
));
3362 error
= check_merge_in_progress(worktree
, repo
);
3366 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
3370 error
= got_ref_open(&head_ref
, repo
, branch_name
? branch_name
:
3371 got_worktree_get_head_ref_name(worktree
), 0);
3374 if (commit_id_str
== NULL
) {
3375 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
3378 error
= got_object_id_str(&commit_id_str
, commit_id
);
3382 struct got_reflist_head refs
;
3384 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
3388 error
= got_repo_match_object_id(&commit_id
, NULL
,
3389 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
3390 got_ref_list_free(&refs
);
3391 free(commit_id_str
);
3392 commit_id_str
= NULL
;
3395 error
= got_object_id_str(&commit_id_str
, commit_id
);
3401 struct got_object_id
*head_commit_id
;
3402 TAILQ_FOREACH(pe
, &paths
, entry
) {
3403 if (pe
->path_len
== 0)
3405 error
= got_error_msg(GOT_ERR_BAD_PATH
,
3406 "switching between branches requires that "
3407 "the entire work tree gets updated");
3410 error
= got_ref_resolve(&head_commit_id
, repo
, head_ref
);
3413 error
= check_linear_ancestry(commit_id
, head_commit_id
, 0,
3415 free(head_commit_id
);
3418 error
= check_same_branch(commit_id
, head_ref
, NULL
, repo
);
3421 error
= switch_head_ref(head_ref
, commit_id
, worktree
, repo
);
3425 error
= check_linear_ancestry(commit_id
,
3426 got_worktree_get_base_commit_id(worktree
), 0, repo
);
3427 if (error
!= NULL
) {
3428 if (error
->code
== GOT_ERR_ANCESTRY
)
3429 error
= got_error(GOT_ERR_BRANCH_MOVED
);
3432 error
= check_same_branch(commit_id
, head_ref
, NULL
, repo
);
3437 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree
),
3439 error
= got_worktree_set_base_commit_id(worktree
, repo
,
3445 memset(&upa
, 0, sizeof(upa
));
3446 upa
.verbosity
= verbosity
;
3447 error
= got_worktree_checkout_files(worktree
, &paths
, repo
,
3448 update_progress
, &upa
, check_cancelled
, NULL
);
3452 if (upa
.did_something
) {
3453 printf("Updated to %s: %s\n",
3454 got_worktree_get_head_ref_name(worktree
), commit_id_str
);
3456 printf("Already up-to-date\n");
3457 print_update_progress_stats(&upa
);
3459 free(worktree_path
);
3460 TAILQ_FOREACH(pe
, &paths
, entry
)
3461 free((char *)pe
->path
);
3462 got_pathlist_free(&paths
);
3464 free(commit_id_str
);
3468 static const struct got_error
*
3469 diff_blobs(struct got_object_id
*blob_id1
, struct got_object_id
*blob_id2
,
3470 const char *path
, int diff_context
, int ignore_whitespace
,
3471 int force_text_diff
, struct got_repository
*repo
)
3473 const struct got_error
*err
= NULL
;
3474 struct got_blob_object
*blob1
= NULL
, *blob2
= NULL
;
3477 err
= got_object_open_as_blob(&blob1
, repo
, blob_id1
, 8192);
3482 err
= got_object_open_as_blob(&blob2
, repo
, blob_id2
, 8192);
3486 while (path
[0] == '/')
3488 err
= got_diff_blob(NULL
, NULL
, blob1
, blob2
, path
, path
,
3489 diff_context
, ignore_whitespace
, force_text_diff
, stdout
);
3492 got_object_blob_close(blob1
);
3493 got_object_blob_close(blob2
);
3497 static const struct got_error
*
3498 diff_trees(struct got_object_id
*tree_id1
, struct got_object_id
*tree_id2
,
3499 const char *path
, int diff_context
, int ignore_whitespace
,
3500 int force_text_diff
, struct got_repository
*repo
)
3502 const struct got_error
*err
= NULL
;
3503 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
3504 struct got_diff_blob_output_unidiff_arg arg
;
3507 err
= got_object_open_as_tree(&tree1
, repo
, tree_id1
);
3512 err
= got_object_open_as_tree(&tree2
, repo
, tree_id2
);
3516 arg
.diff_context
= diff_context
;
3517 arg
.ignore_whitespace
= ignore_whitespace
;
3518 arg
.force_text_diff
= force_text_diff
;
3519 arg
.outfile
= stdout
;
3520 arg
.line_offsets
= NULL
;
3522 while (path
[0] == '/')
3524 err
= got_diff_tree(tree1
, tree2
, path
, path
, repo
,
3525 got_diff_blob_output_unidiff
, &arg
, 1);
3528 got_object_tree_close(tree1
);
3530 got_object_tree_close(tree2
);
3534 static const struct got_error
*
3535 get_changed_paths(struct got_pathlist_head
*paths
,
3536 struct got_commit_object
*commit
, struct got_repository
*repo
)
3538 const struct got_error
*err
= NULL
;
3539 struct got_object_id
*tree_id1
= NULL
, *tree_id2
= NULL
;
3540 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
3541 struct got_object_qid
*qid
;
3543 qid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3545 struct got_commit_object
*pcommit
;
3546 err
= got_object_open_as_commit(&pcommit
, repo
,
3551 tree_id1
= got_object_id_dup(
3552 got_object_commit_get_tree_id(pcommit
));
3553 if (tree_id1
== NULL
) {
3554 got_object_commit_close(pcommit
);
3555 return got_error_from_errno("got_object_id_dup");
3557 got_object_commit_close(pcommit
);
3562 err
= got_object_open_as_tree(&tree1
, repo
, tree_id1
);
3567 tree_id2
= got_object_commit_get_tree_id(commit
);
3568 err
= got_object_open_as_tree(&tree2
, repo
, tree_id2
);
3572 err
= got_diff_tree(tree1
, tree2
, "", "", repo
,
3573 got_diff_tree_collect_changed_paths
, paths
, 0);
3576 got_object_tree_close(tree1
);
3578 got_object_tree_close(tree2
);
3583 static const struct got_error
*
3584 print_patch(struct got_commit_object
*commit
, struct got_object_id
*id
,
3585 const char *path
, int diff_context
, struct got_repository
*repo
)
3587 const struct got_error
*err
= NULL
;
3588 struct got_commit_object
*pcommit
= NULL
;
3589 char *id_str1
= NULL
, *id_str2
= NULL
;
3590 struct got_object_id
*obj_id1
= NULL
, *obj_id2
= NULL
;
3591 struct got_object_qid
*qid
;
3593 qid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3595 err
= got_object_open_as_commit(&pcommit
, repo
,
3601 if (path
&& path
[0] != '\0') {
3603 err
= got_object_id_by_path(&obj_id2
, repo
, id
, path
);
3606 err
= got_object_id_str(&id_str2
, obj_id2
);
3612 err
= got_object_id_by_path(&obj_id1
, repo
,
3615 if (err
->code
!= GOT_ERR_NO_TREE_ENTRY
) {
3620 err
= got_object_id_str(&id_str1
, obj_id1
);
3627 err
= got_object_get_type(&obj_type
, repo
, obj_id2
);
3632 printf("diff %s %s\n", id_str1
? id_str1
: "/dev/null", id_str2
);
3634 case GOT_OBJ_TYPE_BLOB
:
3635 err
= diff_blobs(obj_id1
, obj_id2
, path
, diff_context
,
3638 case GOT_OBJ_TYPE_TREE
:
3639 err
= diff_trees(obj_id1
, obj_id2
, path
, diff_context
,
3643 err
= got_error(GOT_ERR_OBJ_TYPE
);
3649 obj_id2
= got_object_commit_get_tree_id(commit
);
3650 err
= got_object_id_str(&id_str2
, obj_id2
);
3654 obj_id1
= got_object_commit_get_tree_id(pcommit
);
3655 err
= got_object_id_str(&id_str1
, obj_id1
);
3659 printf("diff %s %s\n", id_str1
? id_str1
: "/dev/null",
3661 err
= diff_trees(obj_id1
, obj_id2
, "", diff_context
, 0, 0,
3668 got_object_commit_close(pcommit
);
3673 get_datestr(time_t *time
, char *datebuf
)
3675 struct tm mytm
, *tm
;
3678 tm
= gmtime_r(time
, &mytm
);
3681 s
= asctime_r(tm
, datebuf
);
3684 p
= strchr(s
, '\n');
3690 static const struct got_error
*
3691 match_logmsg(int *have_match
, struct got_object_id
*id
,
3692 struct got_commit_object
*commit
, regex_t
*regex
)
3694 const struct got_error
*err
= NULL
;
3695 regmatch_t regmatch
;
3696 char *id_str
= NULL
, *logmsg
= NULL
;
3700 err
= got_object_id_str(&id_str
, id
);
3704 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
3708 if (regexec(regex
, logmsg
, 1, ®match
, 0) == 0)
3717 match_changed_paths(int *have_match
, struct got_pathlist_head
*changed_paths
,
3720 regmatch_t regmatch
;
3721 struct got_pathlist_entry
*pe
;
3725 TAILQ_FOREACH(pe
, changed_paths
, entry
) {
3726 if (regexec(regex
, pe
->path
, 1, ®match
, 0) == 0) {
3733 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3735 static const struct got_error
*
3736 build_refs_str(char **refs_str
, struct got_reflist_head
*refs
,
3737 struct got_object_id
*id
, struct got_repository
*repo
)
3739 static const struct got_error
*err
= NULL
;
3740 struct got_reflist_entry
*re
;
3746 TAILQ_FOREACH(re
, refs
, entry
) {
3747 struct got_tag_object
*tag
= NULL
;
3748 struct got_object_id
*ref_id
;
3751 name
= got_ref_get_name(re
->ref
);
3752 if (strcmp(name
, GOT_REF_HEAD
) == 0)
3754 if (strncmp(name
, "refs/", 5) == 0)
3756 if (strncmp(name
, "got/", 4) == 0)
3758 if (strncmp(name
, "heads/", 6) == 0)
3760 if (strncmp(name
, "remotes/", 8) == 0) {
3762 s
= strstr(name
, "/" GOT_REF_HEAD
);
3763 if (s
!= NULL
&& s
[strlen(s
)] == '\0')
3766 err
= got_ref_resolve(&ref_id
, repo
, re
->ref
);
3769 if (strncmp(name
, "tags/", 5) == 0) {
3770 err
= got_object_open_as_tag(&tag
, repo
, ref_id
);
3772 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
3776 /* Ref points at something other than a tag. */
3781 cmp
= got_object_id_cmp(tag
?
3782 got_object_tag_get_object_id(tag
) : ref_id
, id
);
3785 got_object_tag_close(tag
);
3789 if (asprintf(refs_str
, "%s%s%s", s
? s
: "",
3790 s
? ", " : "", name
) == -1) {
3791 err
= got_error_from_errno("asprintf");
3802 static const struct got_error
*
3803 print_commit(struct got_commit_object
*commit
, struct got_object_id
*id
,
3804 struct got_repository
*repo
, const char *path
,
3805 struct got_pathlist_head
*changed_paths
, int show_patch
,
3806 int diff_context
, struct got_reflist_object_id_map
*refs_idmap
,
3807 const char *custom_refs_str
)
3809 const struct got_error
*err
= NULL
;
3810 char *id_str
, *datestr
, *logmsg0
, *logmsg
, *line
;
3812 time_t committer_time
;
3813 const char *author
, *committer
;
3814 char *refs_str
= NULL
;
3816 err
= got_object_id_str(&id_str
, id
);
3820 if (custom_refs_str
== NULL
) {
3821 struct got_reflist_head
*refs
;
3822 refs
= got_reflist_object_id_map_lookup(refs_idmap
, id
);
3824 err
= build_refs_str(&refs_str
, refs
, id
, repo
);
3830 printf(GOT_COMMIT_SEP_STR
);
3831 if (custom_refs_str
)
3832 printf("commit %s (%s)\n", id_str
, custom_refs_str
);
3834 printf("commit %s%s%s%s\n", id_str
, refs_str
? " (" : "",
3835 refs_str
? refs_str
: "", refs_str
? ")" : "");
3840 printf("from: %s\n", got_object_commit_get_author(commit
));
3841 committer_time
= got_object_commit_get_committer_time(commit
);
3842 datestr
= get_datestr(&committer_time
, datebuf
);
3844 printf("date: %s UTC\n", datestr
);
3845 author
= got_object_commit_get_author(commit
);
3846 committer
= got_object_commit_get_committer(commit
);
3847 if (strcmp(author
, committer
) != 0)
3848 printf("via: %s\n", committer
);
3849 if (got_object_commit_get_nparents(commit
) > 1) {
3850 const struct got_object_id_queue
*parent_ids
;
3851 struct got_object_qid
*qid
;
3853 parent_ids
= got_object_commit_get_parent_ids(commit
);
3854 STAILQ_FOREACH(qid
, parent_ids
, entry
) {
3855 err
= got_object_id_str(&id_str
, qid
->id
);
3858 printf("parent %d: %s\n", n
++, id_str
);
3864 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
3870 line
= strsep(&logmsg
, "\n");
3872 printf(" %s\n", line
);
3876 if (changed_paths
) {
3877 struct got_pathlist_entry
*pe
;
3878 TAILQ_FOREACH(pe
, changed_paths
, entry
) {
3879 struct got_diff_changed_path
*cp
= pe
->data
;
3880 printf(" %c %s\n", cp
->status
, pe
->path
);
3885 err
= print_patch(commit
, id
, path
, diff_context
, repo
);
3890 if (fflush(stdout
) != 0 && err
== NULL
)
3891 err
= got_error_from_errno("fflush");
3898 static const struct got_error
*
3899 print_commits(struct got_object_id
*root_id
, struct got_object_id
*end_id
,
3900 struct got_repository
*repo
, const char *path
, int show_changed_paths
,
3901 int show_patch
, const char *search_pattern
, int diff_context
, int limit
,
3902 int log_branches
, int reverse_display_order
,
3903 struct got_reflist_object_id_map
*refs_idmap
)
3905 const struct got_error
*err
;
3906 struct got_commit_graph
*graph
;
3909 struct got_object_id_queue reversed_commits
;
3910 struct got_object_qid
*qid
;
3911 struct got_commit_object
*commit
;
3912 struct got_pathlist_head changed_paths
;
3913 struct got_pathlist_entry
*pe
;
3915 STAILQ_INIT(&reversed_commits
);
3916 TAILQ_INIT(&changed_paths
);
3918 if (search_pattern
&& regcomp(®ex
, search_pattern
,
3919 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
))
3920 return got_error_msg(GOT_ERR_REGEX
, search_pattern
);
3922 err
= got_commit_graph_open(&graph
, path
, !log_branches
);
3925 err
= got_commit_graph_iter_start(graph
, root_id
, repo
,
3926 check_cancelled
, NULL
);
3930 struct got_object_id
*id
;
3932 if (sigint_received
|| sigpipe_received
)
3935 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
3936 check_cancelled
, NULL
);
3938 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
3945 err
= got_object_open_as_commit(&commit
, repo
, id
);
3949 if (show_changed_paths
&& !reverse_display_order
) {
3950 err
= get_changed_paths(&changed_paths
, commit
, repo
);
3955 if (search_pattern
) {
3956 err
= match_logmsg(&have_match
, id
, commit
, ®ex
);
3958 got_object_commit_close(commit
);
3961 if (have_match
== 0 && show_changed_paths
)
3962 match_changed_paths(&have_match
,
3963 &changed_paths
, ®ex
);
3964 if (have_match
== 0) {
3965 got_object_commit_close(commit
);
3966 TAILQ_FOREACH(pe
, &changed_paths
, entry
) {
3967 free((char *)pe
->path
);
3970 got_pathlist_free(&changed_paths
);
3975 if (reverse_display_order
) {
3976 err
= got_object_qid_alloc(&qid
, id
);
3979 STAILQ_INSERT_HEAD(&reversed_commits
, qid
, entry
);
3980 got_object_commit_close(commit
);
3982 err
= print_commit(commit
, id
, repo
, path
,
3983 show_changed_paths
? &changed_paths
: NULL
,
3984 show_patch
, diff_context
, refs_idmap
, NULL
);
3985 got_object_commit_close(commit
);
3989 if ((limit
&& --limit
== 0) ||
3990 (end_id
&& got_object_id_cmp(id
, end_id
) == 0))
3993 TAILQ_FOREACH(pe
, &changed_paths
, entry
) {
3994 free((char *)pe
->path
);
3997 got_pathlist_free(&changed_paths
);
3999 if (reverse_display_order
) {
4000 STAILQ_FOREACH(qid
, &reversed_commits
, entry
) {
4001 err
= got_object_open_as_commit(&commit
, repo
, qid
->id
);
4004 if (show_changed_paths
) {
4005 err
= get_changed_paths(&changed_paths
,
4010 err
= print_commit(commit
, qid
->id
, repo
, path
,
4011 show_changed_paths
? &changed_paths
: NULL
,
4012 show_patch
, diff_context
, refs_idmap
, NULL
);
4013 got_object_commit_close(commit
);
4016 TAILQ_FOREACH(pe
, &changed_paths
, entry
) {
4017 free((char *)pe
->path
);
4020 got_pathlist_free(&changed_paths
);
4024 while (!STAILQ_EMPTY(&reversed_commits
)) {
4025 qid
= STAILQ_FIRST(&reversed_commits
);
4026 STAILQ_REMOVE_HEAD(&reversed_commits
, entry
);
4027 got_object_qid_free(qid
);
4029 TAILQ_FOREACH(pe
, &changed_paths
, entry
) {
4030 free((char *)pe
->path
);
4033 got_pathlist_free(&changed_paths
);
4036 got_commit_graph_close(graph
);
4043 fprintf(stderr
, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4044 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4045 "[-R] [path]\n", getprogname());
4050 get_default_log_limit(void)
4052 const char *got_default_log_limit
;
4056 got_default_log_limit
= getenv("GOT_LOG_DEFAULT_LIMIT");
4057 if (got_default_log_limit
== NULL
)
4059 n
= strtonum(got_default_log_limit
, 0, INT_MAX
, &errstr
);
4065 static const struct got_error
*
4066 cmd_log(int argc
, char *argv
[])
4068 const struct got_error
*error
;
4069 struct got_repository
*repo
= NULL
;
4070 struct got_worktree
*worktree
= NULL
;
4071 struct got_object_id
*start_id
= NULL
, *end_id
= NULL
;
4072 char *repo_path
= NULL
, *path
= NULL
, *cwd
= NULL
, *in_repo_path
= NULL
;
4073 const char *start_commit
= NULL
, *end_commit
= NULL
;
4074 const char *search_pattern
= NULL
;
4075 int diff_context
= -1, ch
;
4076 int show_changed_paths
= 0, show_patch
= 0, limit
= 0, log_branches
= 0;
4077 int reverse_display_order
= 0;
4079 struct got_reflist_head refs
;
4080 struct got_reflist_object_id_map
*refs_idmap
= NULL
;
4085 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4091 limit
= get_default_log_limit();
4093 while ((ch
= getopt(argc
, argv
, "bpPc:C:l:r:Rs:x:")) != -1) {
4099 show_changed_paths
= 1;
4102 start_commit
= optarg
;
4105 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
4108 err(1, "-C option %s", errstr
);
4111 limit
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
4113 err(1, "-l option %s", errstr
);
4119 repo_path
= realpath(optarg
, NULL
);
4120 if (repo_path
== NULL
)
4121 return got_error_from_errno2("realpath",
4123 got_path_strip_trailing_slashes(repo_path
);
4126 reverse_display_order
= 1;
4129 search_pattern
= optarg
;
4132 end_commit
= optarg
;
4143 if (diff_context
== -1)
4145 else if (!show_patch
)
4146 errx(1, "-C requires -p");
4148 cwd
= getcwd(NULL
, 0);
4150 error
= got_error_from_errno("getcwd");
4154 if (repo_path
== NULL
) {
4155 error
= got_worktree_open(&worktree
, cwd
);
4156 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
4163 error
= got_worktree_resolve_path(&path
, worktree
,
4168 path
= strdup(argv
[0]);
4170 error
= got_error_from_errno("strdup");
4174 } else if (argc
!= 0)
4177 if (repo_path
== NULL
) {
4178 repo_path
= worktree
?
4179 strdup(got_worktree_get_repo_path(worktree
)) : strdup(cwd
);
4181 if (repo_path
== NULL
) {
4182 error
= got_error_from_errno("strdup");
4186 error
= got_repo_open(&repo
, repo_path
, NULL
);
4190 error
= apply_unveil(got_repo_get_path(repo
), 1,
4191 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
4195 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
4199 error
= got_reflist_object_id_map_create(&refs_idmap
, &refs
, repo
);
4203 if (start_commit
== NULL
) {
4204 struct got_reference
*head_ref
;
4205 struct got_commit_object
*commit
= NULL
;
4206 error
= got_ref_open(&head_ref
, repo
,
4207 worktree
? got_worktree_get_head_ref_name(worktree
)
4211 error
= got_ref_resolve(&start_id
, repo
, head_ref
);
4212 got_ref_close(head_ref
);
4215 error
= got_object_open_as_commit(&commit
, repo
,
4219 got_object_commit_close(commit
);
4221 error
= got_repo_match_object_id(&start_id
, NULL
,
4222 start_commit
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
4226 if (end_commit
!= NULL
) {
4227 error
= got_repo_match_object_id(&end_id
, NULL
,
4228 end_commit
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
4235 * If a path was specified on the command line it was resolved
4236 * to a path in the work tree above. Prepend the work tree's
4237 * path prefix to obtain the corresponding in-repository path.
4241 prefix
= got_worktree_get_path_prefix(worktree
);
4242 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
4243 (path
[0] != '\0') ? "/" : "", path
) == -1) {
4244 error
= got_error_from_errno("asprintf");
4249 error
= got_repo_map_path(&in_repo_path
, repo
,
4255 path
= in_repo_path
;
4258 error
= print_commits(start_id
, end_id
, repo
, path
? path
: "",
4259 show_changed_paths
, show_patch
, search_pattern
, diff_context
,
4260 limit
, log_branches
, reverse_display_order
, refs_idmap
);
4266 got_worktree_close(worktree
);
4268 const struct got_error
*close_err
= got_repo_close(repo
);
4273 got_reflist_object_id_map_free(refs_idmap
);
4274 got_ref_list_free(&refs
);
4281 fprintf(stderr
, "usage: %s diff [-a] [-c commit] [-C number] "
4282 "[-r repository-path] [-s] [-w] [-P] "
4283 "[object1 object2 | path ...]\n", getprogname());
4287 struct print_diff_arg
{
4288 struct got_repository
*repo
;
4289 struct got_worktree
*worktree
;
4294 int ignore_whitespace
;
4295 int force_text_diff
;
4299 * Create a file which contains the target path of a symlink so we can feed
4300 * it as content to the diff engine.
4302 static const struct got_error
*
4303 get_symlink_target_file(int *fd
, int dirfd
, const char *de_name
,
4304 const char *abspath
)
4306 const struct got_error
*err
= NULL
;
4307 char target_path
[PATH_MAX
];
4308 ssize_t target_len
, outlen
;
4313 target_len
= readlinkat(dirfd
, de_name
, target_path
, PATH_MAX
);
4314 if (target_len
== -1)
4315 return got_error_from_errno2("readlinkat", abspath
);
4317 target_len
= readlink(abspath
, target_path
, PATH_MAX
);
4318 if (target_len
== -1)
4319 return got_error_from_errno2("readlink", abspath
);
4322 *fd
= got_opentempfd();
4324 return got_error_from_errno("got_opentempfd");
4326 outlen
= write(*fd
, target_path
, target_len
);
4328 err
= got_error_from_errno("got_opentempfd");
4332 if (lseek(*fd
, 0, SEEK_SET
) == -1) {
4333 err
= got_error_from_errno2("lseek", abspath
);
4344 static const struct got_error
*
4345 print_diff(void *arg
, unsigned char status
, unsigned char staged_status
,
4346 const char *path
, struct got_object_id
*blob_id
,
4347 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
4348 int dirfd
, const char *de_name
)
4350 struct print_diff_arg
*a
= arg
;
4351 const struct got_error
*err
= NULL
;
4352 struct got_blob_object
*blob1
= NULL
;
4355 char *abspath
= NULL
, *label1
= NULL
;
4358 if (a
->diff_staged
) {
4359 if (staged_status
!= GOT_STATUS_MODIFY
&&
4360 staged_status
!= GOT_STATUS_ADD
&&
4361 staged_status
!= GOT_STATUS_DELETE
)
4364 if (staged_status
== GOT_STATUS_DELETE
)
4366 if (status
== GOT_STATUS_NONEXISTENT
)
4367 return got_error_set_errno(ENOENT
, path
);
4368 if (status
!= GOT_STATUS_MODIFY
&&
4369 status
!= GOT_STATUS_ADD
&&
4370 status
!= GOT_STATUS_DELETE
&&
4371 status
!= GOT_STATUS_CONFLICT
)
4375 if (!a
->header_shown
) {
4376 printf("diff %s %s%s\n", a
->id_str
,
4377 got_worktree_get_root_path(a
->worktree
),
4378 a
->diff_staged
? " (staged changes)" : "");
4379 a
->header_shown
= 1;
4382 if (a
->diff_staged
) {
4383 const char *label1
= NULL
, *label2
= NULL
;
4384 switch (staged_status
) {
4385 case GOT_STATUS_MODIFY
:
4389 case GOT_STATUS_ADD
:
4392 case GOT_STATUS_DELETE
:
4396 return got_error(GOT_ERR_FILE_STATUS
);
4398 return got_diff_objects_as_blobs(NULL
, NULL
, blob_id
,
4399 staged_blob_id
, label1
, label2
, a
->diff_context
,
4400 a
->ignore_whitespace
, a
->force_text_diff
, a
->repo
, stdout
);
4403 if (staged_status
== GOT_STATUS_ADD
||
4404 staged_status
== GOT_STATUS_MODIFY
) {
4406 err
= got_object_open_as_blob(&blob1
, a
->repo
, staged_blob_id
,
4410 err
= got_object_id_str(&id_str
, staged_blob_id
);
4413 if (asprintf(&label1
, "%s (staged)", id_str
) == -1) {
4414 err
= got_error_from_errno("asprintf");
4419 } else if (status
!= GOT_STATUS_ADD
) {
4420 err
= got_object_open_as_blob(&blob1
, a
->repo
, blob_id
, 8192);
4425 if (status
!= GOT_STATUS_DELETE
) {
4426 if (asprintf(&abspath
, "%s/%s",
4427 got_worktree_get_root_path(a
->worktree
), path
) == -1) {
4428 err
= got_error_from_errno("asprintf");
4433 fd
= openat(dirfd
, de_name
, O_RDONLY
| O_NOFOLLOW
);
4435 if (!got_err_open_nofollow_on_symlink()) {
4436 err
= got_error_from_errno2("openat",
4440 err
= get_symlink_target_file(&fd
, dirfd
,
4446 fd
= open(abspath
, O_RDONLY
| O_NOFOLLOW
);
4448 if (!got_err_open_nofollow_on_symlink()) {
4449 err
= got_error_from_errno2("open",
4453 err
= get_symlink_target_file(&fd
, dirfd
,
4459 if (fstat(fd
, &sb
) == -1) {
4460 err
= got_error_from_errno2("fstat", abspath
);
4463 f2
= fdopen(fd
, "r");
4465 err
= got_error_from_errno2("fdopen", abspath
);
4472 err
= got_diff_blob_file(blob1
, label1
, f2
, sb
.st_size
, path
,
4473 a
->diff_context
, a
->ignore_whitespace
, a
->force_text_diff
, stdout
);
4476 got_object_blob_close(blob1
);
4477 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
4478 err
= got_error_from_errno("fclose");
4479 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
4480 err
= got_error_from_errno("close");
4485 static const struct got_error
*
4486 cmd_diff(int argc
, char *argv
[])
4488 const struct got_error
*error
;
4489 struct got_repository
*repo
= NULL
;
4490 struct got_worktree
*worktree
= NULL
;
4491 char *cwd
= NULL
, *repo_path
= NULL
;
4492 const char *commit_args
[2] = { NULL
, NULL
};
4493 int ncommit_args
= 0;
4494 struct got_object_id
*ids
[2] = { NULL
, NULL
};
4495 char *labels
[2] = { NULL
, NULL
};
4496 int type1
= GOT_OBJ_TYPE_ANY
, type2
= GOT_OBJ_TYPE_ANY
;
4497 int diff_context
= 3, diff_staged
= 0, ignore_whitespace
= 0, ch
, i
;
4498 int force_text_diff
= 0, force_path
= 0, rflag
= 0;
4500 struct got_reflist_head refs
;
4501 struct got_pathlist_head paths
;
4502 struct got_pathlist_entry
*pe
;
4508 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4513 while ((ch
= getopt(argc
, argv
, "ac:C:r:swP")) != -1) {
4516 force_text_diff
= 1;
4519 if (ncommit_args
>= 2)
4520 errx(1, "too many -c options used");
4521 commit_args
[ncommit_args
++] = optarg
;
4524 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
4527 err(1, "-C option %s", errstr
);
4530 repo_path
= realpath(optarg
, NULL
);
4531 if (repo_path
== NULL
)
4532 return got_error_from_errno2("realpath",
4534 got_path_strip_trailing_slashes(repo_path
);
4541 ignore_whitespace
= 1;
4555 cwd
= getcwd(NULL
, 0);
4557 error
= got_error_from_errno("getcwd");
4561 if (repo_path
== NULL
) {
4562 error
= got_worktree_open(&worktree
, cwd
);
4563 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
4569 strdup(got_worktree_get_repo_path(worktree
));
4570 if (repo_path
== NULL
) {
4571 error
= got_error_from_errno("strdup");
4575 repo_path
= strdup(cwd
);
4576 if (repo_path
== NULL
) {
4577 error
= got_error_from_errno("strdup");
4583 error
= got_repo_open(&repo
, repo_path
, NULL
);
4588 if (rflag
|| worktree
== NULL
|| ncommit_args
> 0) {
4590 error
= got_error_msg(GOT_ERR_NOT_IMPL
,
4591 "-P option can only be used when diffing "
4596 error
= got_error_msg(GOT_ERR_NOT_IMPL
,
4597 "-s option can only be used when diffing "
4603 error
= apply_unveil(got_repo_get_path(repo
), 1,
4604 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
4608 if ((!force_path
&& argc
== 2) || ncommit_args
> 0) {
4609 int obj_type
= (ncommit_args
> 0 ?
4610 GOT_OBJ_TYPE_COMMIT
: GOT_OBJ_TYPE_ANY
);
4611 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
4615 for (i
= 0; i
< (ncommit_args
> 0 ? ncommit_args
: argc
); i
++) {
4617 if (ncommit_args
> 0)
4618 arg
= commit_args
[i
];
4621 error
= got_repo_match_object_id(&ids
[i
], &labels
[i
],
4622 arg
, obj_type
, &refs
, repo
);
4624 if (error
->code
!= GOT_ERR_NOT_REF
&&
4625 error
->code
!= GOT_ERR_NO_OBJ
)
4627 if (ncommit_args
> 0)
4635 if (ncommit_args
== 0 && (ids
[0] == NULL
|| ids
[1] == NULL
)) {
4636 struct print_diff_arg arg
;
4639 if (worktree
== NULL
) {
4640 if (argc
== 2 && ids
[0] == NULL
) {
4641 error
= got_error_path(argv
[0], GOT_ERR_NO_OBJ
);
4643 } else if (argc
== 2 && ids
[1] == NULL
) {
4644 error
= got_error_path(argv
[1], GOT_ERR_NO_OBJ
);
4646 } else if (argc
> 0) {
4647 error
= got_error_fmt(GOT_ERR_NOT_WORKTREE
,
4648 "%s", "specified paths cannot be resolved");
4651 error
= got_error(GOT_ERR_NOT_WORKTREE
);
4656 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
,
4661 error
= got_object_id_str(&id_str
,
4662 got_worktree_get_base_commit_id(worktree
));
4666 arg
.worktree
= worktree
;
4667 arg
.diff_context
= diff_context
;
4668 arg
.id_str
= id_str
;
4669 arg
.header_shown
= 0;
4670 arg
.diff_staged
= diff_staged
;
4671 arg
.ignore_whitespace
= ignore_whitespace
;
4672 arg
.force_text_diff
= force_text_diff
;
4674 error
= got_worktree_status(worktree
, &paths
, repo
, 0,
4675 print_diff
, &arg
, check_cancelled
, NULL
);
4680 if (ncommit_args
== 1) {
4681 struct got_commit_object
*commit
;
4682 error
= got_object_open_as_commit(&commit
, repo
, ids
[0]);
4686 labels
[1] = labels
[0];
4688 if (got_object_commit_get_nparents(commit
) > 0) {
4689 const struct got_object_id_queue
*pids
;
4690 struct got_object_qid
*pid
;
4691 pids
= got_object_commit_get_parent_ids(commit
);
4692 pid
= STAILQ_FIRST(pids
);
4693 ids
[0] = got_object_id_dup(pid
->id
);
4694 if (ids
[0] == NULL
) {
4695 error
= got_error_from_errno(
4696 "got_object_id_dup");
4697 got_object_commit_close(commit
);
4700 error
= got_object_id_str(&labels
[0], ids
[0]);
4702 got_object_commit_close(commit
);
4707 labels
[0] = strdup("/dev/null");
4708 if (labels
[0] == NULL
) {
4709 error
= got_error_from_errno("strdup");
4710 got_object_commit_close(commit
);
4715 got_object_commit_close(commit
);
4718 if (ncommit_args
== 0 && argc
> 2) {
4719 error
= got_error_msg(GOT_ERR_BAD_PATH
,
4720 "path arguments cannot be used when diffing two objects");
4725 error
= got_object_get_type(&type1
, repo
, ids
[0]);
4730 error
= got_object_get_type(&type2
, repo
, ids
[1]);
4733 if (type1
!= GOT_OBJ_TYPE_ANY
&& type1
!= type2
) {
4734 error
= got_error(GOT_ERR_OBJ_TYPE
);
4737 if (type1
== GOT_OBJ_TYPE_BLOB
&& argc
> 0) {
4738 error
= got_error_msg(GOT_ERR_OBJ_TYPE
,
4739 "path arguments cannot be used when diffing blobs");
4743 for (i
= 0; ncommit_args
> 0 && i
< argc
; i
++) {
4745 struct got_pathlist_entry
*new;
4749 error
= got_worktree_resolve_path(&p
, worktree
,
4753 prefix
= got_worktree_get_path_prefix(worktree
);
4754 while (prefix
[0] == '/')
4756 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
4757 (p
[0] != '\0' && prefix
[0] != '\0') ? "/" : "",
4759 error
= got_error_from_errno("asprintf");
4765 char *mapped_path
, *s
;
4766 error
= got_repo_map_path(&mapped_path
, repo
, argv
[i
]);
4772 in_repo_path
= strdup(s
);
4773 if (in_repo_path
== NULL
) {
4774 error
= got_error_from_errno("asprintf");
4781 error
= got_pathlist_insert(&new, &paths
, in_repo_path
, NULL
);
4782 if (error
|| new == NULL
/* duplicate */)
4788 switch (type1
== GOT_OBJ_TYPE_ANY
? type2
: type1
) {
4789 case GOT_OBJ_TYPE_BLOB
:
4790 error
= got_diff_objects_as_blobs(NULL
, NULL
, ids
[0], ids
[1],
4791 NULL
, NULL
, diff_context
, ignore_whitespace
,
4792 force_text_diff
, repo
, stdout
);
4794 case GOT_OBJ_TYPE_TREE
:
4795 error
= got_diff_objects_as_trees(NULL
, NULL
, ids
[0], ids
[1],
4796 &paths
, "", "", diff_context
, ignore_whitespace
,
4797 force_text_diff
, repo
, stdout
);
4799 case GOT_OBJ_TYPE_COMMIT
:
4800 printf("diff %s %s\n", labels
[0], labels
[1]);
4801 error
= got_diff_objects_as_commits(NULL
, NULL
, ids
[0], ids
[1],
4802 &paths
, diff_context
, ignore_whitespace
, force_text_diff
,
4806 error
= got_error(GOT_ERR_OBJ_TYPE
);
4814 got_worktree_close(worktree
);
4816 const struct got_error
*close_err
= got_repo_close(repo
);
4820 TAILQ_FOREACH(pe
, &paths
, entry
)
4821 free((char *)pe
->path
);
4822 got_pathlist_free(&paths
);
4823 got_ref_list_free(&refs
);
4831 "usage: %s blame [-c commit] [-r repository-path] path\n",
4840 char datebuf
[11]; /* YYYY-MM-DD + NUL */
4843 struct blame_cb_args
{
4844 struct blame_line
*lines
;
4848 off_t
*line_offsets
;
4850 struct got_repository
*repo
;
4853 static const struct got_error
*
4854 blame_cb(void *arg
, int nlines
, int lineno
, struct got_object_id
*id
)
4856 const struct got_error
*err
= NULL
;
4857 struct blame_cb_args
*a
= arg
;
4858 struct blame_line
*bline
;
4860 size_t linesize
= 0;
4861 struct got_commit_object
*commit
= NULL
;
4864 time_t committer_time
;
4866 if (nlines
!= a
->nlines
||
4867 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
4868 return got_error(GOT_ERR_RANGE
);
4870 if (sigint_received
)
4871 return got_error(GOT_ERR_ITER_COMPLETED
);
4874 return NULL
; /* no change in this commit */
4876 /* Annotate this line. */
4877 bline
= &a
->lines
[lineno
- 1];
4878 if (bline
->annotated
)
4880 err
= got_object_id_str(&bline
->id_str
, id
);
4884 err
= got_object_open_as_commit(&commit
, a
->repo
, id
);
4888 bline
->committer
= strdup(got_object_commit_get_committer(commit
));
4889 if (bline
->committer
== NULL
) {
4890 err
= got_error_from_errno("strdup");
4894 committer_time
= got_object_commit_get_committer_time(commit
);
4895 if (gmtime_r(&committer_time
, &tm
) == NULL
)
4896 return got_error_from_errno("gmtime_r");
4897 if (strftime(bline
->datebuf
, sizeof(bline
->datebuf
), "%G-%m-%d",
4899 err
= got_error(GOT_ERR_NO_SPACE
);
4902 bline
->annotated
= 1;
4904 /* Print lines annotated so far. */
4905 bline
= &a
->lines
[a
->lineno_cur
- 1];
4906 if (!bline
->annotated
)
4909 offset
= a
->line_offsets
[a
->lineno_cur
- 1];
4910 if (fseeko(a
->f
, offset
, SEEK_SET
) == -1) {
4911 err
= got_error_from_errno("fseeko");
4915 while (bline
->annotated
) {
4916 char *smallerthan
, *at
, *nl
, *committer
;
4919 if (getline(&line
, &linesize
, a
->f
) == -1) {
4921 err
= got_error_from_errno("getline");
4925 committer
= bline
->committer
;
4926 smallerthan
= strchr(committer
, '<');
4927 if (smallerthan
&& smallerthan
[1] != '\0')
4928 committer
= smallerthan
+ 1;
4929 at
= strchr(committer
, '@');
4932 len
= strlen(committer
);
4934 committer
[8] = '\0';
4936 nl
= strchr(line
, '\n');
4939 printf("%.*d) %.8s %s %-8s %s\n", a
->nlines_prec
, a
->lineno_cur
,
4940 bline
->id_str
, bline
->datebuf
, committer
, line
);
4943 bline
= &a
->lines
[a
->lineno_cur
- 1];
4947 got_object_commit_close(commit
);
4952 static const struct got_error
*
4953 cmd_blame(int argc
, char *argv
[])
4955 const struct got_error
*error
;
4956 struct got_repository
*repo
= NULL
;
4957 struct got_worktree
*worktree
= NULL
;
4958 char *path
, *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
4959 char *link_target
= NULL
;
4960 struct got_object_id
*obj_id
= NULL
;
4961 struct got_object_id
*commit_id
= NULL
;
4962 struct got_blob_object
*blob
= NULL
;
4963 char *commit_id_str
= NULL
;
4964 struct blame_cb_args bca
;
4965 int ch
, obj_type
, i
;
4968 memset(&bca
, 0, sizeof(bca
));
4971 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4976 while ((ch
= getopt(argc
, argv
, "c:r:")) != -1) {
4979 commit_id_str
= optarg
;
4982 repo_path
= realpath(optarg
, NULL
);
4983 if (repo_path
== NULL
)
4984 return got_error_from_errno2("realpath",
4986 got_path_strip_trailing_slashes(repo_path
);
5002 cwd
= getcwd(NULL
, 0);
5004 error
= got_error_from_errno("getcwd");
5007 if (repo_path
== NULL
) {
5008 error
= got_worktree_open(&worktree
, cwd
);
5009 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5015 strdup(got_worktree_get_repo_path(worktree
));
5016 if (repo_path
== NULL
) {
5017 error
= got_error_from_errno("strdup");
5022 repo_path
= strdup(cwd
);
5023 if (repo_path
== NULL
) {
5024 error
= got_error_from_errno("strdup");
5030 error
= got_repo_open(&repo
, repo_path
, NULL
);
5035 const char *prefix
= got_worktree_get_path_prefix(worktree
);
5038 error
= got_worktree_resolve_path(&p
, worktree
, path
);
5041 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
5042 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
5044 error
= got_error_from_errno("asprintf");
5049 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5051 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5054 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
5059 if (commit_id_str
== NULL
) {
5060 struct got_reference
*head_ref
;
5061 error
= got_ref_open(&head_ref
, repo
, worktree
?
5062 got_worktree_get_head_ref_name(worktree
) : GOT_REF_HEAD
, 0);
5065 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
5066 got_ref_close(head_ref
);
5070 struct got_reflist_head refs
;
5072 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
5076 error
= got_repo_match_object_id(&commit_id
, NULL
,
5077 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
5078 got_ref_list_free(&refs
);
5083 error
= got_object_resolve_symlinks(&link_target
, in_repo_path
,
5088 error
= got_object_id_by_path(&obj_id
, repo
, commit_id
,
5089 link_target
? link_target
: in_repo_path
);
5093 error
= got_object_get_type(&obj_type
, repo
, obj_id
);
5097 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
5098 error
= got_error_path(link_target
? link_target
: in_repo_path
,
5103 error
= got_object_open_as_blob(&blob
, repo
, obj_id
, 8192);
5106 bca
.f
= got_opentemp();
5107 if (bca
.f
== NULL
) {
5108 error
= got_error_from_errno("got_opentemp");
5111 error
= got_object_blob_dump_to_file(&filesize
, &bca
.nlines
,
5112 &bca
.line_offsets
, bca
.f
, blob
);
5113 if (error
|| bca
.nlines
== 0)
5116 /* Don't include \n at EOF in the blame line count. */
5117 if (bca
.line_offsets
[bca
.nlines
- 1] == filesize
)
5120 bca
.lines
= calloc(bca
.nlines
, sizeof(*bca
.lines
));
5121 if (bca
.lines
== NULL
) {
5122 error
= got_error_from_errno("calloc");
5126 bca
.nlines_prec
= 0;
5134 error
= got_blame(link_target
? link_target
: in_repo_path
, commit_id
,
5135 repo
, blame_cb
, &bca
, check_cancelled
, NULL
);
5144 got_object_blob_close(blob
);
5146 got_worktree_close(worktree
);
5148 const struct got_error
*close_err
= got_repo_close(repo
);
5153 for (i
= 0; i
< bca
.nlines
; i
++) {
5154 struct blame_line
*bline
= &bca
.lines
[i
];
5155 free(bline
->id_str
);
5156 free(bline
->committer
);
5160 free(bca
.line_offsets
);
5161 if (bca
.f
&& fclose(bca
.f
) == EOF
&& error
== NULL
)
5162 error
= got_error_from_errno("fclose");
5170 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5175 static const struct got_error
*
5176 print_entry(struct got_tree_entry
*te
, const char *id
, const char *path
,
5177 const char *root_path
, struct got_repository
*repo
)
5179 const struct got_error
*err
= NULL
;
5180 int is_root_path
= (strcmp(path
, root_path
) == 0);
5181 const char *modestr
= "";
5182 mode_t mode
= got_tree_entry_get_mode(te
);
5183 char *link_target
= NULL
;
5185 path
+= strlen(root_path
);
5186 while (path
[0] == '/')
5189 if (got_object_tree_entry_is_submodule(te
))
5191 else if (S_ISLNK(mode
)) {
5194 err
= got_tree_entry_get_symlink_target(&link_target
, te
, repo
);
5197 for (i
= 0; i
< strlen(link_target
); i
++) {
5198 if (!isprint((unsigned char)link_target
[i
]))
5199 link_target
[i
] = '?';
5204 else if (S_ISDIR(mode
))
5206 else if (mode
& S_IXUSR
)
5209 printf("%s%s%s%s%s%s%s\n", id
? id
: "", path
,
5210 is_root_path
? "" : "/", got_tree_entry_get_name(te
), modestr
,
5211 link_target
? " -> ": "", link_target
? link_target
: "");
5217 static const struct got_error
*
5218 print_tree(const char *path
, struct got_object_id
*commit_id
,
5219 int show_ids
, int recurse
, const char *root_path
,
5220 struct got_repository
*repo
)
5222 const struct got_error
*err
= NULL
;
5223 struct got_object_id
*tree_id
= NULL
;
5224 struct got_tree_object
*tree
= NULL
;
5227 err
= got_object_id_by_path(&tree_id
, repo
, commit_id
, path
);
5231 err
= got_object_open_as_tree(&tree
, repo
, tree_id
);
5234 nentries
= got_object_tree_get_nentries(tree
);
5235 for (i
= 0; i
< nentries
; i
++) {
5236 struct got_tree_entry
*te
;
5239 if (sigint_received
|| sigpipe_received
)
5242 te
= got_object_tree_get_entry(tree
, i
);
5245 err
= got_object_id_str(&id_str
,
5246 got_tree_entry_get_id(te
));
5249 if (asprintf(&id
, "%s ", id_str
) == -1) {
5250 err
= got_error_from_errno("asprintf");
5256 err
= print_entry(te
, id
, path
, root_path
, repo
);
5261 if (recurse
&& S_ISDIR(got_tree_entry_get_mode(te
))) {
5263 if (asprintf(&child_path
, "%s%s%s", path
,
5264 path
[0] == '/' && path
[1] == '\0' ? "" : "/",
5265 got_tree_entry_get_name(te
)) == -1) {
5266 err
= got_error_from_errno("asprintf");
5269 err
= print_tree(child_path
, commit_id
, show_ids
, 1,
5278 got_object_tree_close(tree
);
5283 static const struct got_error
*
5284 cmd_tree(int argc
, char *argv
[])
5286 const struct got_error
*error
;
5287 struct got_repository
*repo
= NULL
;
5288 struct got_worktree
*worktree
= NULL
;
5289 const char *path
, *refname
= NULL
;
5290 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
5291 struct got_object_id
*commit_id
= NULL
;
5292 char *commit_id_str
= NULL
;
5293 int show_ids
= 0, recurse
= 0;
5297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5302 while ((ch
= getopt(argc
, argv
, "c:r:iR")) != -1) {
5305 commit_id_str
= optarg
;
5308 repo_path
= realpath(optarg
, NULL
);
5309 if (repo_path
== NULL
)
5310 return got_error_from_errno2("realpath",
5312 got_path_strip_trailing_slashes(repo_path
);
5336 cwd
= getcwd(NULL
, 0);
5338 error
= got_error_from_errno("getcwd");
5341 if (repo_path
== NULL
) {
5342 error
= got_worktree_open(&worktree
, cwd
);
5343 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5349 strdup(got_worktree_get_repo_path(worktree
));
5350 if (repo_path
== NULL
)
5351 error
= got_error_from_errno("strdup");
5355 repo_path
= strdup(cwd
);
5356 if (repo_path
== NULL
) {
5357 error
= got_error_from_errno("strdup");
5363 error
= got_repo_open(&repo
, repo_path
, NULL
);
5368 const char *prefix
= got_worktree_get_path_prefix(worktree
);
5373 error
= got_worktree_resolve_path(&p
, worktree
, path
);
5376 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
5377 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
5379 error
= got_error_from_errno("asprintf");
5384 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5388 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5393 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
5398 if (commit_id_str
== NULL
) {
5399 struct got_reference
*head_ref
;
5401 refname
= got_worktree_get_head_ref_name(worktree
);
5403 refname
= GOT_REF_HEAD
;
5404 error
= got_ref_open(&head_ref
, repo
, refname
, 0);
5407 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
5408 got_ref_close(head_ref
);
5412 struct got_reflist_head refs
;
5414 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
5418 error
= got_repo_match_object_id(&commit_id
, NULL
,
5419 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
5420 got_ref_list_free(&refs
);
5425 error
= print_tree(in_repo_path
, commit_id
, show_ids
, recurse
,
5426 in_repo_path
, repo
);
5433 got_worktree_close(worktree
);
5435 const struct got_error
*close_err
= got_repo_close(repo
);
5445 fprintf(stderr
, "usage: %s status [-I] [-s status-codes ] "
5446 "[-S status-codes] [path ...]\n", getprogname());
5450 struct got_status_arg
{
5455 static const struct got_error
*
5456 print_status(void *arg
, unsigned char status
, unsigned char staged_status
,
5457 const char *path
, struct got_object_id
*blob_id
,
5458 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
5459 int dirfd
, const char *de_name
)
5461 struct got_status_arg
*st
= arg
;
5463 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
5464 status
= GOT_STATUS_NO_CHANGE
;
5465 if (st
!= NULL
&& st
->status_codes
) {
5466 size_t ncodes
= strlen(st
->status_codes
);
5469 for (i
= 0; i
< ncodes
; i
++) {
5471 if (status
== st
->status_codes
[i
] ||
5472 staged_status
== st
->status_codes
[i
]) {
5477 if (status
== st
->status_codes
[i
] ||
5478 staged_status
== st
->status_codes
[i
])
5483 if (st
->suppress
&& j
== 0)
5490 printf("%c%c %s\n", status
, staged_status
, path
);
5494 static const struct got_error
*
5495 cmd_status(int argc
, char *argv
[])
5497 const struct got_error
*error
= NULL
;
5498 struct got_repository
*repo
= NULL
;
5499 struct got_worktree
*worktree
= NULL
;
5500 struct got_status_arg st
;
5502 struct got_pathlist_head paths
;
5503 struct got_pathlist_entry
*pe
;
5504 int ch
, i
, no_ignores
= 0;
5508 memset(&st
, 0, sizeof(st
));
5509 st
.status_codes
= NULL
;
5512 while ((ch
= getopt(argc
, argv
, "Is:S:")) != -1) {
5518 if (st
.status_codes
!= NULL
&& st
.suppress
== 0)
5519 option_conflict('S', 's');
5523 for (i
= 0; i
< strlen(optarg
); i
++) {
5524 switch (optarg
[i
]) {
5525 case GOT_STATUS_MODIFY
:
5526 case GOT_STATUS_ADD
:
5527 case GOT_STATUS_DELETE
:
5528 case GOT_STATUS_CONFLICT
:
5529 case GOT_STATUS_MISSING
:
5530 case GOT_STATUS_OBSTRUCTED
:
5531 case GOT_STATUS_UNVERSIONED
:
5532 case GOT_STATUS_MODE_CHANGE
:
5533 case GOT_STATUS_NONEXISTENT
:
5536 errx(1, "invalid status code '%c'",
5540 if (ch
== 's' && st
.suppress
)
5541 option_conflict('s', 'S');
5542 st
.status_codes
= optarg
;
5554 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5558 cwd
= getcwd(NULL
, 0);
5560 error
= got_error_from_errno("getcwd");
5564 error
= got_worktree_open(&worktree
, cwd
);
5566 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
5567 error
= wrap_not_worktree_error(error
, "status", cwd
);
5571 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
5576 error
= apply_unveil(got_repo_get_path(repo
), 1,
5577 got_worktree_get_root_path(worktree
));
5581 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
5585 error
= got_worktree_status(worktree
, &paths
, repo
, no_ignores
,
5586 print_status
, &st
, check_cancelled
, NULL
);
5588 TAILQ_FOREACH(pe
, &paths
, entry
)
5589 free((char *)pe
->path
);
5590 got_pathlist_free(&paths
);
5599 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5605 static const struct got_error
*
5606 list_refs(struct got_repository
*repo
, const char *refname
)
5608 static const struct got_error
*err
= NULL
;
5609 struct got_reflist_head refs
;
5610 struct got_reflist_entry
*re
;
5613 err
= got_ref_list(&refs
, repo
, refname
, got_ref_cmp_by_name
, NULL
);
5617 TAILQ_FOREACH(re
, &refs
, entry
) {
5619 refstr
= got_ref_to_str(re
->ref
);
5621 return got_error_from_errno("got_ref_to_str");
5622 printf("%s: %s\n", got_ref_get_name(re
->ref
), refstr
);
5626 got_ref_list_free(&refs
);
5630 static const struct got_error
*
5631 delete_ref_by_name(struct got_repository
*repo
, const char *refname
)
5633 const struct got_error
*err
;
5634 struct got_reference
*ref
;
5636 err
= got_ref_open(&ref
, repo
, refname
, 0);
5640 err
= delete_ref(repo
, ref
);
5645 static const struct got_error
*
5646 add_ref(struct got_repository
*repo
, const char *refname
, const char *target
)
5648 const struct got_error
*err
= NULL
;
5649 struct got_object_id
*id
;
5650 struct got_reference
*ref
= NULL
;
5653 * Don't let the user create a reference name with a leading '-'.
5654 * While technically a valid reference name, this case is usually
5655 * an unintended typo.
5657 if (refname
[0] == '-')
5658 return got_error_path(refname
, GOT_ERR_REF_NAME_MINUS
);
5660 err
= got_repo_match_object_id_prefix(&id
, target
, GOT_OBJ_TYPE_ANY
,
5663 struct got_reference
*target_ref
;
5665 if (err
->code
!= GOT_ERR_BAD_OBJ_ID_STR
)
5667 err
= got_ref_open(&target_ref
, repo
, target
, 0);
5670 err
= got_ref_resolve(&id
, repo
, target_ref
);
5671 got_ref_close(target_ref
);
5676 err
= got_ref_alloc(&ref
, refname
, id
);
5680 err
= got_ref_write(ref
, repo
);
5688 static const struct got_error
*
5689 add_symref(struct got_repository
*repo
, const char *refname
, const char *target
)
5691 const struct got_error
*err
= NULL
;
5692 struct got_reference
*ref
= NULL
;
5693 struct got_reference
*target_ref
= NULL
;
5696 * Don't let the user create a reference name with a leading '-'.
5697 * While technically a valid reference name, this case is usually
5698 * an unintended typo.
5700 if (refname
[0] == '-')
5701 return got_error_path(refname
, GOT_ERR_REF_NAME_MINUS
);
5703 err
= got_ref_open(&target_ref
, repo
, target
, 0);
5707 err
= got_ref_alloc_symref(&ref
, refname
, target_ref
);
5711 err
= got_ref_write(ref
, repo
);
5714 got_ref_close(target_ref
);
5720 static const struct got_error
*
5721 cmd_ref(int argc
, char *argv
[])
5723 const struct got_error
*error
= NULL
;
5724 struct got_repository
*repo
= NULL
;
5725 struct got_worktree
*worktree
= NULL
;
5726 char *cwd
= NULL
, *repo_path
= NULL
;
5727 int ch
, do_list
= 0, do_delete
= 0;
5728 const char *obj_arg
= NULL
, *symref_target
= NULL
;
5729 char *refname
= NULL
;
5731 while ((ch
= getopt(argc
, argv
, "c:dr:ls:")) != -1) {
5740 repo_path
= realpath(optarg
, NULL
);
5741 if (repo_path
== NULL
)
5742 return got_error_from_errno2("realpath",
5744 got_path_strip_trailing_slashes(repo_path
);
5750 symref_target
= optarg
;
5758 if (obj_arg
&& do_list
)
5759 option_conflict('c', 'l');
5760 if (obj_arg
&& do_delete
)
5761 option_conflict('c', 'd');
5762 if (obj_arg
&& symref_target
)
5763 option_conflict('c', 's');
5764 if (symref_target
&& do_delete
)
5765 option_conflict('s', 'd');
5766 if (symref_target
&& do_list
)
5767 option_conflict('s', 'l');
5768 if (do_delete
&& do_list
)
5769 option_conflict('d', 'l');
5775 if (argc
!= 0 && argc
!= 1)
5778 refname
= strdup(argv
[0]);
5779 if (refname
== NULL
) {
5780 error
= got_error_from_errno("strdup");
5787 refname
= strdup(argv
[0]);
5788 if (refname
== NULL
) {
5789 error
= got_error_from_errno("strdup");
5795 got_path_strip_trailing_slashes(refname
);
5799 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5803 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5804 "sendfd unveil", NULL
) == -1)
5808 cwd
= getcwd(NULL
, 0);
5810 error
= got_error_from_errno("getcwd");
5814 if (repo_path
== NULL
) {
5815 error
= got_worktree_open(&worktree
, cwd
);
5816 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5822 strdup(got_worktree_get_repo_path(worktree
));
5823 if (repo_path
== NULL
)
5824 error
= got_error_from_errno("strdup");
5828 repo_path
= strdup(cwd
);
5829 if (repo_path
== NULL
) {
5830 error
= got_error_from_errno("strdup");
5836 error
= got_repo_open(&repo
, repo_path
, NULL
);
5840 error
= apply_unveil(got_repo_get_path(repo
), do_list
,
5841 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
5846 error
= list_refs(repo
, refname
);
5848 error
= delete_ref_by_name(repo
, refname
);
5849 else if (symref_target
)
5850 error
= add_symref(repo
, refname
, symref_target
);
5852 if (obj_arg
== NULL
)
5854 error
= add_ref(repo
, refname
, obj_arg
);
5859 const struct got_error
*close_err
= got_repo_close(repo
);
5864 got_worktree_close(worktree
);
5874 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5875 "[name]\n", getprogname());
5879 static const struct got_error
*
5880 list_branch(struct got_repository
*repo
, struct got_worktree
*worktree
,
5881 struct got_reference
*ref
)
5883 const struct got_error
*err
= NULL
;
5884 const char *refname
, *marker
= " ";
5887 refname
= got_ref_get_name(ref
);
5888 if (worktree
&& strcmp(refname
,
5889 got_worktree_get_head_ref_name(worktree
)) == 0) {
5890 struct got_object_id
*id
= NULL
;
5892 err
= got_ref_resolve(&id
, repo
, ref
);
5895 if (got_object_id_cmp(id
,
5896 got_worktree_get_base_commit_id(worktree
)) == 0)
5903 if (strncmp(refname
, "refs/heads/", 11) == 0)
5905 if (strncmp(refname
, "refs/got/worktree/", 18) == 0)
5907 if (strncmp(refname
, "refs/remotes/", 13) == 0)
5910 refstr
= got_ref_to_str(ref
);
5912 return got_error_from_errno("got_ref_to_str");
5914 printf("%s%s: %s\n", marker
, refname
, refstr
);
5919 static const struct got_error
*
5920 show_current_branch(struct got_repository
*repo
, struct got_worktree
*worktree
)
5922 const char *refname
;
5924 if (worktree
== NULL
)
5925 return got_error(GOT_ERR_NOT_WORKTREE
);
5927 refname
= got_worktree_get_head_ref_name(worktree
);
5929 if (strncmp(refname
, "refs/heads/", 11) == 0)
5931 if (strncmp(refname
, "refs/got/worktree/", 18) == 0)
5934 printf("%s\n", refname
);
5939 static const struct got_error
*
5940 list_branches(struct got_repository
*repo
, struct got_worktree
*worktree
)
5942 static const struct got_error
*err
= NULL
;
5943 struct got_reflist_head refs
;
5944 struct got_reflist_entry
*re
;
5945 struct got_reference
*temp_ref
= NULL
;
5946 int rebase_in_progress
, histedit_in_progress
;
5951 err
= got_worktree_rebase_in_progress(&rebase_in_progress
,
5956 err
= got_worktree_histedit_in_progress(&histedit_in_progress
,
5961 if (rebase_in_progress
|| histedit_in_progress
) {
5962 err
= got_ref_open(&temp_ref
, repo
,
5963 got_worktree_get_head_ref_name(worktree
), 0);
5966 list_branch(repo
, worktree
, temp_ref
);
5967 got_ref_close(temp_ref
);
5971 err
= got_ref_list(&refs
, repo
, "refs/heads",
5972 got_ref_cmp_by_name
, NULL
);
5976 TAILQ_FOREACH(re
, &refs
, entry
)
5977 list_branch(repo
, worktree
, re
->ref
);
5979 got_ref_list_free(&refs
);
5981 err
= got_ref_list(&refs
, repo
, "refs/remotes",
5982 got_ref_cmp_by_name
, NULL
);
5986 TAILQ_FOREACH(re
, &refs
, entry
)
5987 list_branch(repo
, worktree
, re
->ref
);
5989 got_ref_list_free(&refs
);
5994 static const struct got_error
*
5995 delete_branch(struct got_repository
*repo
, struct got_worktree
*worktree
,
5996 const char *branch_name
)
5998 const struct got_error
*err
= NULL
;
5999 struct got_reference
*ref
= NULL
;
6000 char *refname
, *remote_refname
= NULL
;
6002 if (strncmp(branch_name
, "refs/", 5) == 0)
6004 if (strncmp(branch_name
, "heads/", 6) == 0)
6006 else if (strncmp(branch_name
, "remotes/", 8) == 0)
6009 if (asprintf(&refname
, "refs/heads/%s", branch_name
) == -1)
6010 return got_error_from_errno("asprintf");
6012 if (asprintf(&remote_refname
, "refs/remotes/%s",
6013 branch_name
) == -1) {
6014 err
= got_error_from_errno("asprintf");
6018 err
= got_ref_open(&ref
, repo
, refname
, 0);
6020 const struct got_error
*err2
;
6021 if (err
->code
!= GOT_ERR_NOT_REF
)
6024 * Keep 'err' intact such that if neither branch exists
6025 * we report "refs/heads" rather than "refs/remotes" in
6026 * our error message.
6028 err2
= got_ref_open(&ref
, repo
, remote_refname
, 0);
6035 strcmp(got_worktree_get_head_ref_name(worktree
),
6036 got_ref_get_name(ref
)) == 0) {
6037 err
= got_error_msg(GOT_ERR_SAME_BRANCH
,
6038 "will not delete this work tree's current branch");
6042 err
= delete_ref(repo
, ref
);
6047 free(remote_refname
);
6051 static const struct got_error
*
6052 add_branch(struct got_repository
*repo
, const char *branch_name
,
6053 struct got_object_id
*base_commit_id
)
6055 const struct got_error
*err
= NULL
;
6056 struct got_reference
*ref
= NULL
;
6057 char *base_refname
= NULL
, *refname
= NULL
;
6060 * Don't let the user create a branch name with a leading '-'.
6061 * While technically a valid reference name, this case is usually
6062 * an unintended typo.
6064 if (branch_name
[0] == '-')
6065 return got_error_path(branch_name
, GOT_ERR_REF_NAME_MINUS
);
6067 if (strncmp(branch_name
, "refs/heads/", 11) == 0)
6070 if (asprintf(&refname
, "refs/heads/%s", branch_name
) == -1) {
6071 err
= got_error_from_errno("asprintf");
6075 err
= got_ref_open(&ref
, repo
, refname
, 0);
6077 err
= got_error(GOT_ERR_BRANCH_EXISTS
);
6079 } else if (err
->code
!= GOT_ERR_NOT_REF
)
6082 err
= got_ref_alloc(&ref
, refname
, base_commit_id
);
6086 err
= got_ref_write(ref
, repo
);
6095 static const struct got_error
*
6096 cmd_branch(int argc
, char *argv
[])
6098 const struct got_error
*error
= NULL
;
6099 struct got_repository
*repo
= NULL
;
6100 struct got_worktree
*worktree
= NULL
;
6101 char *cwd
= NULL
, *repo_path
= NULL
;
6102 int ch
, do_list
= 0, do_show
= 0, do_update
= 1;
6103 const char *delref
= NULL
, *commit_id_arg
= NULL
;
6104 struct got_reference
*ref
= NULL
;
6105 struct got_pathlist_head paths
;
6106 struct got_pathlist_entry
*pe
;
6107 struct got_object_id
*commit_id
= NULL
;
6108 char *commit_id_str
= NULL
;
6112 while ((ch
= getopt(argc
, argv
, "c:d:r:ln")) != -1) {
6115 commit_id_arg
= optarg
;
6121 repo_path
= realpath(optarg
, NULL
);
6122 if (repo_path
== NULL
)
6123 return got_error_from_errno2("realpath",
6125 got_path_strip_trailing_slashes(repo_path
);
6139 if (do_list
&& delref
)
6140 option_conflict('l', 'd');
6145 if (!do_list
&& !delref
&& argc
== 0)
6148 if ((do_list
|| delref
|| do_show
) && commit_id_arg
!= NULL
)
6149 errx(1, "-c option can only be used when creating a branch");
6151 if (do_list
|| delref
) {
6154 } else if (!do_show
&& argc
!= 1)
6158 if (do_list
|| do_show
) {
6159 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6163 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6164 "sendfd unveil", NULL
) == -1)
6168 cwd
= getcwd(NULL
, 0);
6170 error
= got_error_from_errno("getcwd");
6174 if (repo_path
== NULL
) {
6175 error
= got_worktree_open(&worktree
, cwd
);
6176 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6182 strdup(got_worktree_get_repo_path(worktree
));
6183 if (repo_path
== NULL
)
6184 error
= got_error_from_errno("strdup");
6188 repo_path
= strdup(cwd
);
6189 if (repo_path
== NULL
) {
6190 error
= got_error_from_errno("strdup");
6196 error
= got_repo_open(&repo
, repo_path
, NULL
);
6200 error
= apply_unveil(got_repo_get_path(repo
), do_list
,
6201 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
6206 error
= show_current_branch(repo
, worktree
);
6208 error
= list_branches(repo
, worktree
);
6210 error
= delete_branch(repo
, worktree
, delref
);
6212 struct got_reflist_head refs
;
6214 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
6218 if (commit_id_arg
== NULL
)
6219 commit_id_arg
= worktree
?
6220 got_worktree_get_head_ref_name(worktree
) :
6222 error
= got_repo_match_object_id(&commit_id
, NULL
,
6223 commit_id_arg
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
6224 got_ref_list_free(&refs
);
6227 error
= add_branch(repo
, argv
[0], commit_id
);
6230 if (worktree
&& do_update
) {
6231 struct got_update_progress_arg upa
;
6232 char *branch_refname
= NULL
;
6234 error
= got_object_id_str(&commit_id_str
, commit_id
);
6237 error
= get_worktree_paths_from_argv(&paths
, 0, NULL
,
6241 if (asprintf(&branch_refname
, "refs/heads/%s", argv
[0])
6243 error
= got_error_from_errno("asprintf");
6246 error
= got_ref_open(&ref
, repo
, branch_refname
, 0);
6247 free(branch_refname
);
6250 error
= switch_head_ref(ref
, commit_id
, worktree
,
6254 error
= got_worktree_set_base_commit_id(worktree
, repo
,
6258 memset(&upa
, 0, sizeof(upa
));
6259 error
= got_worktree_checkout_files(worktree
, &paths
,
6260 repo
, update_progress
, &upa
, check_cancelled
,
6264 if (upa
.did_something
) {
6265 printf("Updated to %s: %s\n",
6266 got_worktree_get_head_ref_name(worktree
),
6269 print_update_progress_stats(&upa
);
6276 const struct got_error
*close_err
= got_repo_close(repo
);
6281 got_worktree_close(worktree
);
6285 free(commit_id_str
);
6286 TAILQ_FOREACH(pe
, &paths
, entry
)
6287 free((char *)pe
->path
);
6288 got_pathlist_free(&paths
);
6297 "usage: %s tag [-c commit] [-r repository] [-l] "
6298 "[-m message] name\n", getprogname());
6303 static const struct got_error
*
6304 sort_tags(struct got_reflist_head
*sorted
, struct got_reflist_head
*tags
)
6306 const struct got_error
*err
= NULL
;
6307 struct got_reflist_entry
*re
, *se
, *new;
6308 struct got_object_id
*re_id
, *se_id
;
6309 struct got_tag_object
*re_tag
, *se_tag
;
6310 time_t re_time
, se_time
;
6312 STAILQ_FOREACH(re
, tags
, entry
) {
6313 se
= STAILQ_FIRST(sorted
);
6315 err
= got_reflist_entry_dup(&new, re
);
6318 STAILQ_INSERT_HEAD(sorted
, new, entry
);
6321 err
= got_ref_resolve(&re_id
, repo
, re
->ref
);
6324 err
= got_object_open_as_tag(&re_tag
, repo
, re_id
);
6328 re_time
= got_object_tag_get_tagger_time(re_tag
);
6329 got_object_tag_close(re_tag
);
6333 err
= got_ref_resolve(&se_id
, repo
, re
->ref
);
6336 err
= got_object_open_as_tag(&se_tag
, repo
, se_id
);
6340 se_time
= got_object_tag_get_tagger_time(se_tag
);
6341 got_object_tag_close(se_tag
);
6343 if (se_time
> re_time
) {
6344 err
= got_reflist_entry_dup(&new, re
);
6347 STAILQ_INSERT_AFTER(sorted
, se
, new, entry
);
6350 se
= STAILQ_NEXT(se
, entry
);
6359 static const struct got_error
*
6360 list_tags(struct got_repository
*repo
, struct got_worktree
*worktree
)
6362 static const struct got_error
*err
= NULL
;
6363 struct got_reflist_head refs
;
6364 struct got_reflist_entry
*re
;
6368 err
= got_ref_list(&refs
, repo
, "refs/tags", got_ref_cmp_tags
, repo
);
6372 TAILQ_FOREACH(re
, &refs
, entry
) {
6373 const char *refname
;
6374 char *refstr
, *tagmsg0
, *tagmsg
, *line
, *id_str
, *datestr
;
6378 struct got_object_id
*id
;
6379 struct got_tag_object
*tag
;
6380 struct got_commit_object
*commit
= NULL
;
6382 refname
= got_ref_get_name(re
->ref
);
6383 if (strncmp(refname
, "refs/tags/", 10) != 0)
6386 refstr
= got_ref_to_str(re
->ref
);
6387 if (refstr
== NULL
) {
6388 err
= got_error_from_errno("got_ref_to_str");
6391 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR
, refname
, refstr
);
6394 err
= got_ref_resolve(&id
, repo
, re
->ref
);
6397 err
= got_object_open_as_tag(&tag
, repo
, id
);
6399 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
6403 /* "lightweight" tag */
6404 err
= got_object_open_as_commit(&commit
, repo
, id
);
6409 tagger
= got_object_commit_get_committer(commit
);
6411 got_object_commit_get_committer_time(commit
);
6412 err
= got_object_id_str(&id_str
, id
);
6418 tagger
= got_object_tag_get_tagger(tag
);
6419 tagger_time
= got_object_tag_get_tagger_time(tag
);
6420 err
= got_object_id_str(&id_str
,
6421 got_object_tag_get_object_id(tag
));
6425 printf("from: %s\n", tagger
);
6426 datestr
= get_datestr(&tagger_time
, datebuf
);
6428 printf("date: %s UTC\n", datestr
);
6430 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT
, id_str
);
6432 switch (got_object_tag_get_object_type(tag
)) {
6433 case GOT_OBJ_TYPE_BLOB
:
6434 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB
,
6437 case GOT_OBJ_TYPE_TREE
:
6438 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE
,
6441 case GOT_OBJ_TYPE_COMMIT
:
6442 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT
,
6445 case GOT_OBJ_TYPE_TAG
:
6446 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG
,
6455 err
= got_object_commit_get_logmsg(&tagmsg0
, commit
);
6458 got_object_commit_close(commit
);
6460 tagmsg0
= strdup(got_object_tag_get_message(tag
));
6461 got_object_tag_close(tag
);
6462 if (tagmsg0
== NULL
) {
6463 err
= got_error_from_errno("strdup");
6470 line
= strsep(&tagmsg
, "\n");
6472 printf(" %s\n", line
);
6477 got_ref_list_free(&refs
);
6481 static const struct got_error
*
6482 get_tag_message(char **tagmsg
, char **tagmsg_path
, const char *commit_id_str
,
6483 const char *tag_name
, const char *repo_path
)
6485 const struct got_error
*err
= NULL
;
6486 char *template = NULL
, *initial_content
= NULL
;
6487 char *editor
= NULL
;
6488 int initial_content_len
;
6491 if (asprintf(&template, GOT_TMPDIR_STR
"/got-tagmsg") == -1) {
6492 err
= got_error_from_errno("asprintf");
6496 initial_content_len
= asprintf(&initial_content
,
6497 "\n# tagging commit %s as %s\n",
6498 commit_id_str
, tag_name
);
6499 if (initial_content_len
== -1) {
6500 err
= got_error_from_errno("asprintf");
6504 err
= got_opentemp_named_fd(tagmsg_path
, &fd
, template);
6508 if (write(fd
, initial_content
, initial_content_len
) == -1) {
6509 err
= got_error_from_errno2("write", *tagmsg_path
);
6513 err
= get_editor(&editor
);
6516 err
= edit_logmsg(tagmsg
, editor
, *tagmsg_path
, initial_content
,
6517 initial_content_len
, 1);
6519 free(initial_content
);
6523 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
6524 err
= got_error_from_errno2("close", *tagmsg_path
);
6526 /* Editor is done; we can now apply unveil(2) */
6528 err
= apply_unveil(repo_path
, 0, NULL
);
6536 static const struct got_error
*
6537 add_tag(struct got_repository
*repo
, struct got_worktree
*worktree
,
6538 const char *tag_name
, const char *commit_arg
, const char *tagmsg_arg
)
6540 const struct got_error
*err
= NULL
;
6541 struct got_object_id
*commit_id
= NULL
, *tag_id
= NULL
;
6542 char *label
= NULL
, *commit_id_str
= NULL
;
6543 struct got_reference
*ref
= NULL
;
6544 char *refname
= NULL
, *tagmsg
= NULL
, *tagger
= NULL
;
6545 char *tagmsg_path
= NULL
, *tag_id_str
= NULL
;
6546 int preserve_tagmsg
= 0;
6547 struct got_reflist_head refs
;
6552 * Don't let the user create a tag name with a leading '-'.
6553 * While technically a valid reference name, this case is usually
6554 * an unintended typo.
6556 if (tag_name
[0] == '-')
6557 return got_error_path(tag_name
, GOT_ERR_REF_NAME_MINUS
);
6559 err
= get_author(&tagger
, repo
, worktree
);
6563 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
6567 err
= got_repo_match_object_id(&commit_id
, &label
, commit_arg
,
6568 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
6572 err
= got_object_id_str(&commit_id_str
, commit_id
);
6576 if (strncmp("refs/tags/", tag_name
, 10) == 0) {
6577 refname
= strdup(tag_name
);
6578 if (refname
== NULL
) {
6579 err
= got_error_from_errno("strdup");
6583 } else if (asprintf(&refname
, "refs/tags/%s", tag_name
) == -1) {
6584 err
= got_error_from_errno("asprintf");
6588 err
= got_ref_open(&ref
, repo
, refname
, 0);
6590 err
= got_error(GOT_ERR_TAG_EXISTS
);
6592 } else if (err
->code
!= GOT_ERR_NOT_REF
)
6595 if (tagmsg_arg
== NULL
) {
6596 err
= get_tag_message(&tagmsg
, &tagmsg_path
, commit_id_str
,
6597 tag_name
, got_repo_get_path(repo
));
6599 if (err
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
6600 tagmsg_path
!= NULL
)
6601 preserve_tagmsg
= 1;
6606 err
= got_object_tag_create(&tag_id
, tag_name
, commit_id
,
6607 tagger
, time(NULL
), tagmsg
? tagmsg
: tagmsg_arg
, repo
);
6610 preserve_tagmsg
= 1;
6614 err
= got_ref_alloc(&ref
, refname
, tag_id
);
6617 preserve_tagmsg
= 1;
6621 err
= got_ref_write(ref
, repo
);
6624 preserve_tagmsg
= 1;
6628 err
= got_object_id_str(&tag_id_str
, tag_id
);
6631 preserve_tagmsg
= 1;
6634 printf("Created tag %s\n", tag_id_str
);
6636 if (preserve_tagmsg
) {
6637 fprintf(stderr
, "%s: tag message preserved in %s\n",
6638 getprogname(), tagmsg_path
);
6639 } else if (tagmsg_path
&& unlink(tagmsg_path
) == -1 && err
== NULL
)
6640 err
= got_error_from_errno2("unlink", tagmsg_path
);
6645 free(commit_id_str
);
6650 got_ref_list_free(&refs
);
6654 static const struct got_error
*
6655 cmd_tag(int argc
, char *argv
[])
6657 const struct got_error
*error
= NULL
;
6658 struct got_repository
*repo
= NULL
;
6659 struct got_worktree
*worktree
= NULL
;
6660 char *cwd
= NULL
, *repo_path
= NULL
, *commit_id_str
= NULL
;
6661 char *gitconfig_path
= NULL
;
6662 const char *tag_name
, *commit_id_arg
= NULL
, *tagmsg
= NULL
;
6663 int ch
, do_list
= 0;
6665 while ((ch
= getopt(argc
, argv
, "c:m:r:l")) != -1) {
6668 commit_id_arg
= optarg
;
6674 repo_path
= realpath(optarg
, NULL
);
6675 if (repo_path
== NULL
)
6676 return got_error_from_errno2("realpath",
6678 got_path_strip_trailing_slashes(repo_path
);
6693 if (commit_id_arg
!= NULL
)
6695 "-c option can only be used when creating a tag");
6697 option_conflict('l', 'm');
6700 } else if (argc
!= 1)
6707 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6711 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6712 "sendfd unveil", NULL
) == -1)
6716 cwd
= getcwd(NULL
, 0);
6718 error
= got_error_from_errno("getcwd");
6722 if (repo_path
== NULL
) {
6723 error
= got_worktree_open(&worktree
, cwd
);
6724 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6730 strdup(got_worktree_get_repo_path(worktree
));
6731 if (repo_path
== NULL
)
6732 error
= got_error_from_errno("strdup");
6736 repo_path
= strdup(cwd
);
6737 if (repo_path
== NULL
) {
6738 error
= got_error_from_errno("strdup");
6745 error
= got_repo_open(&repo
, repo_path
, NULL
);
6748 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
6751 error
= list_tags(repo
, worktree
);
6753 error
= get_gitconfig_path(&gitconfig_path
);
6756 error
= got_repo_open(&repo
, repo_path
, gitconfig_path
);
6761 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
6766 if (commit_id_arg
== NULL
) {
6767 struct got_reference
*head_ref
;
6768 struct got_object_id
*commit_id
;
6769 error
= got_ref_open(&head_ref
, repo
,
6770 worktree
? got_worktree_get_head_ref_name(worktree
)
6774 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
6775 got_ref_close(head_ref
);
6778 error
= got_object_id_str(&commit_id_str
, commit_id
);
6784 error
= add_tag(repo
, worktree
, tag_name
,
6785 commit_id_str
? commit_id_str
: commit_id_arg
, tagmsg
);
6789 const struct got_error
*close_err
= got_repo_close(repo
);
6794 got_worktree_close(worktree
);
6797 free(gitconfig_path
);
6798 free(commit_id_str
);
6805 fprintf(stderr
, "usage: %s add [-R] [-I] path ...\n",
6810 static const struct got_error
*
6811 add_progress(void *arg
, unsigned char status
, const char *path
)
6813 while (path
[0] == '/')
6815 printf("%c %s\n", status
, path
);
6819 static const struct got_error
*
6820 cmd_add(int argc
, char *argv
[])
6822 const struct got_error
*error
= NULL
;
6823 struct got_repository
*repo
= NULL
;
6824 struct got_worktree
*worktree
= NULL
;
6826 struct got_pathlist_head paths
;
6827 struct got_pathlist_entry
*pe
;
6828 int ch
, can_recurse
= 0, no_ignores
= 0;
6832 while ((ch
= getopt(argc
, argv
, "IR")) != -1) {
6850 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6857 cwd
= getcwd(NULL
, 0);
6859 error
= got_error_from_errno("getcwd");
6863 error
= got_worktree_open(&worktree
, cwd
);
6865 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
6866 error
= wrap_not_worktree_error(error
, "add", cwd
);
6870 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
6875 error
= apply_unveil(got_repo_get_path(repo
), 1,
6876 got_worktree_get_root_path(worktree
));
6880 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
6887 TAILQ_FOREACH(pe
, &paths
, entry
) {
6888 if (asprintf(&ondisk_path
, "%s/%s",
6889 got_worktree_get_root_path(worktree
),
6891 error
= got_error_from_errno("asprintf");
6894 if (lstat(ondisk_path
, &sb
) == -1) {
6895 if (errno
== ENOENT
) {
6899 error
= got_error_from_errno2("lstat",
6905 if (S_ISDIR(sb
.st_mode
)) {
6906 error
= got_error_msg(GOT_ERR_BAD_PATH
,
6907 "adding directories requires -R option");
6913 error
= got_worktree_schedule_add(worktree
, &paths
, add_progress
,
6914 NULL
, repo
, no_ignores
);
6917 const struct got_error
*close_err
= got_repo_close(repo
);
6922 got_worktree_close(worktree
);
6923 TAILQ_FOREACH(pe
, &paths
, entry
)
6924 free((char *)pe
->path
);
6925 got_pathlist_free(&paths
);
6933 fprintf(stderr
, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6934 "path ...\n", getprogname());
6938 static const struct got_error
*
6939 print_remove_status(void *arg
, unsigned char status
,
6940 unsigned char staged_status
, const char *path
)
6942 while (path
[0] == '/')
6944 if (status
== GOT_STATUS_NONEXISTENT
)
6946 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
6947 status
= GOT_STATUS_NO_CHANGE
;
6948 printf("%c%c %s\n", status
, staged_status
, path
);
6952 static const struct got_error
*
6953 cmd_remove(int argc
, char *argv
[])
6955 const struct got_error
*error
= NULL
;
6956 struct got_worktree
*worktree
= NULL
;
6957 struct got_repository
*repo
= NULL
;
6958 const char *status_codes
= NULL
;
6960 struct got_pathlist_head paths
;
6961 struct got_pathlist_entry
*pe
;
6962 int ch
, delete_local_mods
= 0, can_recurse
= 0, keep_on_disk
= 0, i
;
6966 while ((ch
= getopt(argc
, argv
, "fkRs:")) != -1) {
6969 delete_local_mods
= 1;
6978 for (i
= 0; i
< strlen(optarg
); i
++) {
6979 switch (optarg
[i
]) {
6980 case GOT_STATUS_MODIFY
:
6981 delete_local_mods
= 1;
6983 case GOT_STATUS_MISSING
:
6986 errx(1, "invalid status code '%c'",
6990 status_codes
= optarg
;
7002 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7009 cwd
= getcwd(NULL
, 0);
7011 error
= got_error_from_errno("getcwd");
7014 error
= got_worktree_open(&worktree
, cwd
);
7016 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
7017 error
= wrap_not_worktree_error(error
, "remove", cwd
);
7021 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
7026 error
= apply_unveil(got_repo_get_path(repo
), 1,
7027 got_worktree_get_root_path(worktree
));
7031 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
7038 TAILQ_FOREACH(pe
, &paths
, entry
) {
7039 if (asprintf(&ondisk_path
, "%s/%s",
7040 got_worktree_get_root_path(worktree
),
7042 error
= got_error_from_errno("asprintf");
7045 if (lstat(ondisk_path
, &sb
) == -1) {
7046 if (errno
== ENOENT
) {
7050 error
= got_error_from_errno2("lstat",
7056 if (S_ISDIR(sb
.st_mode
)) {
7057 error
= got_error_msg(GOT_ERR_BAD_PATH
,
7058 "removing directories requires -R option");
7064 error
= got_worktree_schedule_delete(worktree
, &paths
,
7065 delete_local_mods
, status_codes
, print_remove_status
, NULL
,
7066 repo
, keep_on_disk
);
7069 const struct got_error
*close_err
= got_repo_close(repo
);
7074 got_worktree_close(worktree
);
7075 TAILQ_FOREACH(pe
, &paths
, entry
)
7076 free((char *)pe
->path
);
7077 got_pathlist_free(&paths
);
7085 fprintf(stderr
, "usage: %s revert [-p] [-F response-script] [-R] "
7086 "path ...\n", getprogname());
7090 static const struct got_error
*
7091 revert_progress(void *arg
, unsigned char status
, const char *path
)
7093 if (status
== GOT_STATUS_UNVERSIONED
)
7096 while (path
[0] == '/')
7098 printf("%c %s\n", status
, path
);
7102 struct choose_patch_arg
{
7103 FILE *patch_script_file
;
7107 static const struct got_error
*
7108 show_change(unsigned char status
, const char *path
, FILE *patch_file
, int n
,
7109 int nchanges
, const char *action
)
7112 size_t linesize
= 0;
7116 case GOT_STATUS_ADD
:
7117 printf("A %s\n%s this addition? [y/n] ", path
, action
);
7119 case GOT_STATUS_DELETE
:
7120 printf("D %s\n%s this deletion? [y/n] ", path
, action
);
7122 case GOT_STATUS_MODIFY
:
7123 if (fseek(patch_file
, 0L, SEEK_SET
) == -1)
7124 return got_error_from_errno("fseek");
7125 printf(GOT_COMMIT_SEP_STR
);
7126 while ((linelen
= getline(&line
, &linesize
, patch_file
)) != -1)
7128 if (ferror(patch_file
))
7129 return got_error_from_errno("getline");
7130 printf(GOT_COMMIT_SEP_STR
);
7131 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7132 path
, n
, nchanges
, action
);
7135 return got_error_path(path
, GOT_ERR_FILE_STATUS
);
7141 static const struct got_error
*
7142 choose_patch(int *choice
, void *arg
, unsigned char status
, const char *path
,
7143 FILE *patch_file
, int n
, int nchanges
)
7145 const struct got_error
*err
= NULL
;
7147 size_t linesize
= 0;
7150 struct choose_patch_arg
*a
= arg
;
7152 *choice
= GOT_PATCH_CHOICE_NONE
;
7154 if (a
->patch_script_file
) {
7156 err
= show_change(status
, path
, patch_file
, n
, nchanges
,
7160 linelen
= getline(&line
, &linesize
, a
->patch_script_file
);
7161 if (linelen
== -1) {
7162 if (ferror(a
->patch_script_file
))
7163 return got_error_from_errno("getline");
7166 nl
= strchr(line
, '\n');
7169 if (strcmp(line
, "y") == 0) {
7170 *choice
= GOT_PATCH_CHOICE_YES
;
7172 } else if (strcmp(line
, "n") == 0) {
7173 *choice
= GOT_PATCH_CHOICE_NO
;
7175 } else if (strcmp(line
, "q") == 0 &&
7176 status
== GOT_STATUS_MODIFY
) {
7177 *choice
= GOT_PATCH_CHOICE_QUIT
;
7180 printf("invalid response '%s'\n", line
);
7185 while (resp
!= 'y' && resp
!= 'n' && resp
!= 'q') {
7186 err
= show_change(status
, path
, patch_file
, n
, nchanges
,
7193 if (status
== GOT_STATUS_MODIFY
) {
7194 if (resp
!= 'y' && resp
!= 'n' && resp
!= 'q') {
7195 printf("invalid response '%c'\n", resp
);
7198 } else if (resp
!= 'y' && resp
!= 'n') {
7199 printf("invalid response '%c'\n", resp
);
7205 *choice
= GOT_PATCH_CHOICE_YES
;
7206 else if (resp
== 'n')
7207 *choice
= GOT_PATCH_CHOICE_NO
;
7208 else if (resp
== 'q' && status
== GOT_STATUS_MODIFY
)
7209 *choice
= GOT_PATCH_CHOICE_QUIT
;
7215 static const struct got_error
*
7216 cmd_revert(int argc
, char *argv
[])
7218 const struct got_error
*error
= NULL
;
7219 struct got_worktree
*worktree
= NULL
;
7220 struct got_repository
*repo
= NULL
;
7221 char *cwd
= NULL
, *path
= NULL
;
7222 struct got_pathlist_head paths
;
7223 struct got_pathlist_entry
*pe
;
7224 int ch
, can_recurse
= 0, pflag
= 0;
7225 FILE *patch_script_file
= NULL
;
7226 const char *patch_script_path
= NULL
;
7227 struct choose_patch_arg cpa
;
7231 while ((ch
= getopt(argc
, argv
, "pF:R")) != -1) {
7237 patch_script_path
= optarg
;
7252 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7253 "unveil", NULL
) == -1)
7258 if (patch_script_path
&& !pflag
)
7259 errx(1, "-F option can only be used together with -p option");
7261 cwd
= getcwd(NULL
, 0);
7263 error
= got_error_from_errno("getcwd");
7266 error
= got_worktree_open(&worktree
, cwd
);
7268 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
7269 error
= wrap_not_worktree_error(error
, "revert", cwd
);
7273 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
7278 if (patch_script_path
) {
7279 patch_script_file
= fopen(patch_script_path
, "r");
7280 if (patch_script_file
== NULL
) {
7281 error
= got_error_from_errno2("fopen",
7286 error
= apply_unveil(got_repo_get_path(repo
), 1,
7287 got_worktree_get_root_path(worktree
));
7291 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
7298 TAILQ_FOREACH(pe
, &paths
, entry
) {
7299 if (asprintf(&ondisk_path
, "%s/%s",
7300 got_worktree_get_root_path(worktree
),
7302 error
= got_error_from_errno("asprintf");
7305 if (lstat(ondisk_path
, &sb
) == -1) {
7306 if (errno
== ENOENT
) {
7310 error
= got_error_from_errno2("lstat",
7316 if (S_ISDIR(sb
.st_mode
)) {
7317 error
= got_error_msg(GOT_ERR_BAD_PATH
,
7318 "reverting directories requires -R option");
7324 cpa
.patch_script_file
= patch_script_file
;
7325 cpa
.action
= "revert";
7326 error
= got_worktree_revert(worktree
, &paths
, revert_progress
, NULL
,
7327 pflag
? choose_patch
: NULL
, &cpa
, repo
);
7329 if (patch_script_file
&& fclose(patch_script_file
) == EOF
&&
7331 error
= got_error_from_errno2("fclose", patch_script_path
);
7333 const struct got_error
*close_err
= got_repo_close(repo
);
7338 got_worktree_close(worktree
);
7347 fprintf(stderr
, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7348 "[path ...]\n", getprogname());
7352 struct collect_commit_logmsg_arg
{
7353 const char *cmdline_log
;
7354 const char *prepared_log
;
7355 int non_interactive
;
7357 const char *worktree_path
;
7358 const char *branch_name
;
7359 const char *repo_path
;
7364 static const struct got_error
*
7365 read_prepared_logmsg(char **logmsg
, const char *path
)
7367 const struct got_error
*err
= NULL
;
7373 memset(&sb
, 0, sizeof(sb
));
7375 f
= fopen(path
, "r");
7377 return got_error_from_errno2("fopen", path
);
7379 if (fstat(fileno(f
), &sb
) == -1) {
7380 err
= got_error_from_errno2("fstat", path
);
7383 if (sb
.st_size
== 0) {
7384 err
= got_error(GOT_ERR_COMMIT_MSG_EMPTY
);
7388 *logmsg
= malloc(sb
.st_size
+ 1);
7389 if (*logmsg
== NULL
) {
7390 err
= got_error_from_errno("malloc");
7394 r
= fread(*logmsg
, 1, sb
.st_size
, f
);
7395 if (r
!= sb
.st_size
) {
7397 err
= got_error_from_errno2("fread", path
);
7399 err
= got_error(GOT_ERR_IO
);
7402 (*logmsg
)[sb
.st_size
] = '\0';
7404 if (fclose(f
) == EOF
&& err
== NULL
)
7405 err
= got_error_from_errno2("fclose", path
);
7414 static const struct got_error
*
7415 collect_commit_logmsg(struct got_pathlist_head
*commitable_paths
, char **logmsg
,
7418 char *initial_content
= NULL
;
7419 struct got_pathlist_entry
*pe
;
7420 const struct got_error
*err
= NULL
;
7421 char *template = NULL
;
7422 struct collect_commit_logmsg_arg
*a
= arg
;
7423 int initial_content_len
;
7427 /* if a message was specified on the command line, just use it */
7428 if (a
->cmdline_log
!= NULL
&& strlen(a
->cmdline_log
) != 0) {
7429 len
= strlen(a
->cmdline_log
) + 1;
7430 *logmsg
= malloc(len
+ 1);
7431 if (*logmsg
== NULL
)
7432 return got_error_from_errno("malloc");
7433 strlcpy(*logmsg
, a
->cmdline_log
, len
);
7435 } else if (a
->prepared_log
!= NULL
&& a
->non_interactive
)
7436 return read_prepared_logmsg(logmsg
, a
->prepared_log
);
7438 if (asprintf(&template, "%s/logmsg", a
->worktree_path
) == -1)
7439 return got_error_from_errno("asprintf");
7441 err
= got_opentemp_named_fd(&a
->logmsg_path
, &fd
, template);
7445 if (a
->prepared_log
) {
7447 err
= read_prepared_logmsg(&msg
, a
->prepared_log
);
7450 if (write(fd
, msg
, strlen(msg
)) == -1) {
7451 err
= got_error_from_errno2("write", a
->logmsg_path
);
7458 initial_content_len
= asprintf(&initial_content
,
7459 "\n# changes to be committed on branch %s:\n",
7461 if (initial_content_len
== -1) {
7462 err
= got_error_from_errno("asprintf");
7466 if (write(fd
, initial_content
, initial_content_len
) == -1) {
7467 err
= got_error_from_errno2("write", a
->logmsg_path
);
7471 TAILQ_FOREACH(pe
, commitable_paths
, entry
) {
7472 struct got_commitable
*ct
= pe
->data
;
7473 dprintf(fd
, "# %c %s\n",
7474 got_commitable_get_status(ct
),
7475 got_commitable_get_path(ct
));
7478 err
= edit_logmsg(logmsg
, a
->editor
, a
->logmsg_path
, initial_content
,
7479 initial_content_len
, a
->prepared_log
? 0 : 1);
7481 free(initial_content
);
7484 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
7485 err
= got_error_from_errno2("close", a
->logmsg_path
);
7487 /* Editor is done; we can now apply unveil(2) */
7489 err
= apply_unveil(a
->repo_path
, 0, a
->worktree_path
);
7497 static const struct got_error
*
7498 cmd_commit(int argc
, char *argv
[])
7500 const struct got_error
*error
= NULL
;
7501 struct got_worktree
*worktree
= NULL
;
7502 struct got_repository
*repo
= NULL
;
7503 char *cwd
= NULL
, *id_str
= NULL
;
7504 struct got_object_id
*id
= NULL
;
7505 const char *logmsg
= NULL
;
7506 char *prepared_logmsg
= NULL
;
7507 struct collect_commit_logmsg_arg cl_arg
;
7508 char *gitconfig_path
= NULL
, *editor
= NULL
, *author
= NULL
;
7509 int ch
, rebase_in_progress
, histedit_in_progress
, preserve_logmsg
= 0;
7510 int allow_bad_symlinks
= 0, non_interactive
= 0, merge_in_progress
= 0;
7511 struct got_pathlist_head paths
;
7514 cl_arg
.logmsg_path
= NULL
;
7516 while ((ch
= getopt(argc
, argv
, "F:m:NS")) != -1) {
7520 option_conflict('F', 'm');
7521 prepared_logmsg
= realpath(optarg
, NULL
);
7522 if (prepared_logmsg
== NULL
)
7523 return got_error_from_errno2("realpath",
7527 if (prepared_logmsg
)
7528 option_conflict('m', 'F');
7532 non_interactive
= 1;
7535 allow_bad_symlinks
= 1;
7547 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7548 "unveil", NULL
) == -1)
7551 cwd
= getcwd(NULL
, 0);
7553 error
= got_error_from_errno("getcwd");
7556 error
= got_worktree_open(&worktree
, cwd
);
7558 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
7559 error
= wrap_not_worktree_error(error
, "commit", cwd
);
7563 error
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
7566 if (rebase_in_progress
) {
7567 error
= got_error(GOT_ERR_REBASING
);
7571 error
= got_worktree_histedit_in_progress(&histedit_in_progress
,
7576 error
= get_gitconfig_path(&gitconfig_path
);
7579 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
7584 error
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
, repo
);
7587 if (merge_in_progress
) {
7588 error
= got_error(GOT_ERR_MERGE_BUSY
);
7592 error
= get_author(&author
, repo
, worktree
);
7597 * unveil(2) traverses exec(2); if an editor is used we have
7598 * to apply unveil after the log message has been written.
7600 if (logmsg
== NULL
|| strlen(logmsg
) == 0)
7601 error
= get_editor(&editor
);
7603 error
= apply_unveil(got_repo_get_path(repo
), 0,
7604 got_worktree_get_root_path(worktree
));
7608 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
7612 cl_arg
.editor
= editor
;
7613 cl_arg
.cmdline_log
= logmsg
;
7614 cl_arg
.prepared_log
= prepared_logmsg
;
7615 cl_arg
.non_interactive
= non_interactive
;
7616 cl_arg
.worktree_path
= got_worktree_get_root_path(worktree
);
7617 cl_arg
.branch_name
= got_worktree_get_head_ref_name(worktree
);
7618 if (!histedit_in_progress
) {
7619 if (strncmp(cl_arg
.branch_name
, "refs/heads/", 11) != 0) {
7620 error
= got_error(GOT_ERR_COMMIT_BRANCH
);
7623 cl_arg
.branch_name
+= 11;
7625 cl_arg
.repo_path
= got_repo_get_path(repo
);
7626 error
= got_worktree_commit(&id
, worktree
, &paths
, author
, NULL
,
7627 allow_bad_symlinks
, collect_commit_logmsg
, &cl_arg
,
7628 print_status
, NULL
, repo
);
7630 if (error
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
7631 cl_arg
.logmsg_path
!= NULL
)
7632 preserve_logmsg
= 1;
7636 error
= got_object_id_str(&id_str
, id
);
7639 printf("Created commit %s\n", id_str
);
7641 if (preserve_logmsg
) {
7642 fprintf(stderr
, "%s: log message preserved in %s\n",
7643 getprogname(), cl_arg
.logmsg_path
);
7644 } else if (cl_arg
.logmsg_path
&& unlink(cl_arg
.logmsg_path
) == -1 &&
7646 error
= got_error_from_errno2("unlink", cl_arg
.logmsg_path
);
7647 free(cl_arg
.logmsg_path
);
7649 const struct got_error
*close_err
= got_repo_close(repo
);
7654 got_worktree_close(worktree
);
7657 free(gitconfig_path
);
7660 free(prepared_logmsg
);
7667 fprintf(stderr
, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7668 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7669 "[remote-repository]\n", getprogname());
7673 struct got_send_progress_arg
{
7674 char last_scaled_packsize
[FMT_SCALED_STRSIZE
];
7677 int last_nobj_total
;
7681 int printed_something
;
7683 struct got_pathlist_head
*delete_branches
;
7686 static const struct got_error
*
7687 send_progress(void *arg
, off_t packfile_size
, int ncommits
, int nobj_total
,
7688 int nobj_deltify
, int nobj_written
, off_t bytes_sent
, const char *refname
,
7691 struct got_send_progress_arg
*a
= arg
;
7692 char scaled_packsize
[FMT_SCALED_STRSIZE
];
7693 char scaled_sent
[FMT_SCALED_STRSIZE
];
7694 int p_deltify
= 0, p_written
= 0, p_sent
= 0;
7695 int print_searching
= 0, print_total
= 0;
7696 int print_deltify
= 0, print_written
= 0, print_sent
= 0;
7698 if (a
->verbosity
< 0)
7702 const char *status
= success
? "accepted" : "rejected";
7705 struct got_pathlist_entry
*pe
;
7706 TAILQ_FOREACH(pe
, a
->delete_branches
, entry
) {
7707 const char *branchname
= pe
->path
;
7708 if (got_path_cmp(branchname
, refname
,
7709 strlen(branchname
), strlen(refname
)) == 0) {
7711 a
->sent_something
= 1;
7717 if (a
->printed_something
)
7719 printf("Server has %s %s", status
, refname
);
7720 a
->printed_something
= 1;
7724 if (fmt_scaled(packfile_size
, scaled_packsize
) == -1)
7725 return got_error_from_errno("fmt_scaled");
7726 if (fmt_scaled(bytes_sent
, scaled_sent
) == -1)
7727 return got_error_from_errno("fmt_scaled");
7729 if (a
->last_ncommits
!= ncommits
) {
7730 print_searching
= 1;
7731 a
->last_ncommits
= ncommits
;
7734 if (a
->last_nobj_total
!= nobj_total
) {
7735 print_searching
= 1;
7737 a
->last_nobj_total
= nobj_total
;
7740 if (packfile_size
> 0 && (a
->last_scaled_packsize
[0] == '\0' ||
7741 strcmp(scaled_packsize
, a
->last_scaled_packsize
)) != 0) {
7742 if (strlcpy(a
->last_scaled_packsize
, scaled_packsize
,
7743 FMT_SCALED_STRSIZE
) >= FMT_SCALED_STRSIZE
)
7744 return got_error(GOT_ERR_NO_SPACE
);
7747 if (nobj_deltify
> 0 || nobj_written
> 0) {
7748 if (nobj_deltify
> 0) {
7749 p_deltify
= (nobj_deltify
* 100) / nobj_total
;
7750 if (p_deltify
!= a
->last_p_deltify
) {
7751 a
->last_p_deltify
= p_deltify
;
7752 print_searching
= 1;
7757 if (nobj_written
> 0) {
7758 p_written
= (nobj_written
* 100) / nobj_total
;
7759 if (p_written
!= a
->last_p_written
) {
7760 a
->last_p_written
= p_written
;
7761 print_searching
= 1;
7769 if (bytes_sent
> 0) {
7770 p_sent
= (bytes_sent
* 100) / packfile_size
;
7771 if (p_sent
!= a
->last_p_sent
) {
7772 a
->last_p_sent
= p_sent
;
7773 print_searching
= 1;
7779 a
->sent_something
= 1;
7782 if (print_searching
|| print_total
|| print_deltify
|| print_written
||
7785 if (print_searching
)
7786 printf("packing %d reference%s", ncommits
,
7787 ncommits
== 1 ? "" : "s");
7789 printf("; %d object%s", nobj_total
,
7790 nobj_total
== 1 ? "" : "s");
7792 printf("; deltify: %d%%", p_deltify
);
7794 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE
,
7795 scaled_packsize
, p_sent
);
7796 else if (print_written
)
7797 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE
,
7798 scaled_packsize
, p_written
);
7799 if (print_searching
|| print_total
|| print_deltify
||
7800 print_written
|| print_sent
) {
7801 a
->printed_something
= 1;
7807 static const struct got_error
*
7808 cmd_send(int argc
, char *argv
[])
7810 const struct got_error
*error
= NULL
;
7811 char *cwd
= NULL
, *repo_path
= NULL
;
7812 const char *remote_name
;
7813 char *proto
= NULL
, *host
= NULL
, *port
= NULL
;
7814 char *repo_name
= NULL
, *server_path
= NULL
;
7815 const struct got_remote_repo
*remotes
, *remote
= NULL
;
7816 int nremotes
, nbranches
= 0, ntags
= 0, ndelete_branches
= 0;
7817 struct got_repository
*repo
= NULL
;
7818 struct got_worktree
*worktree
= NULL
;
7819 const struct got_gotconfig
*repo_conf
= NULL
, *worktree_conf
= NULL
;
7820 struct got_pathlist_head branches
;
7821 struct got_pathlist_head tags
;
7822 struct got_reflist_head all_branches
;
7823 struct got_reflist_head all_tags
;
7824 struct got_pathlist_head delete_args
;
7825 struct got_pathlist_head delete_branches
;
7826 struct got_reflist_entry
*re
;
7827 struct got_pathlist_entry
*pe
;
7828 int i
, ch
, sendfd
= -1, sendstatus
;
7830 struct got_send_progress_arg spa
;
7831 int verbosity
= 0, overwrite_refs
= 0;
7832 int send_all_branches
= 0, send_all_tags
= 0;
7833 struct got_reference
*ref
= NULL
;
7835 TAILQ_INIT(&branches
);
7837 TAILQ_INIT(&all_branches
);
7838 TAILQ_INIT(&all_tags
);
7839 TAILQ_INIT(&delete_args
);
7840 TAILQ_INIT(&delete_branches
);
7842 while ((ch
= getopt(argc
, argv
, "ab:d:fr:t:Tvq")) != -1) {
7845 send_all_branches
= 1;
7848 error
= got_pathlist_append(&branches
, optarg
, NULL
);
7854 error
= got_pathlist_append(&delete_args
, optarg
, NULL
);
7862 repo_path
= realpath(optarg
, NULL
);
7863 if (repo_path
== NULL
)
7864 return got_error_from_errno2("realpath",
7866 got_path_strip_trailing_slashes(repo_path
);
7869 error
= got_pathlist_append(&tags
, optarg
, NULL
);
7880 else if (verbosity
< 3)
7894 if (send_all_branches
&& !TAILQ_EMPTY(&branches
))
7895 option_conflict('a', 'b');
7896 if (send_all_tags
&& !TAILQ_EMPTY(&tags
))
7897 option_conflict('T', 't');
7901 remote_name
= GOT_SEND_DEFAULT_REMOTE_NAME
;
7903 remote_name
= argv
[0];
7907 cwd
= getcwd(NULL
, 0);
7909 error
= got_error_from_errno("getcwd");
7913 if (repo_path
== NULL
) {
7914 error
= got_worktree_open(&worktree
, cwd
);
7915 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
7921 strdup(got_worktree_get_repo_path(worktree
));
7922 if (repo_path
== NULL
)
7923 error
= got_error_from_errno("strdup");
7927 repo_path
= strdup(cwd
);
7928 if (repo_path
== NULL
) {
7929 error
= got_error_from_errno("strdup");
7935 error
= got_repo_open(&repo
, repo_path
, NULL
);
7940 worktree_conf
= got_worktree_get_gotconfig(worktree
);
7941 if (worktree_conf
) {
7942 got_gotconfig_get_remotes(&nremotes
, &remotes
,
7944 for (i
= 0; i
< nremotes
; i
++) {
7945 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
7946 remote
= &remotes
[i
];
7952 if (remote
== NULL
) {
7953 repo_conf
= got_repo_get_gotconfig(repo
);
7955 got_gotconfig_get_remotes(&nremotes
, &remotes
,
7957 for (i
= 0; i
< nremotes
; i
++) {
7958 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
7959 remote
= &remotes
[i
];
7965 if (remote
== NULL
) {
7966 got_repo_get_gitconfig_remotes(&nremotes
, &remotes
, repo
);
7967 for (i
= 0; i
< nremotes
; i
++) {
7968 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
7969 remote
= &remotes
[i
];
7974 if (remote
== NULL
) {
7975 error
= got_error_path(remote_name
, GOT_ERR_NO_REMOTE
);
7979 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
7980 &repo_name
, remote
->send_url
);
7984 if (strcmp(proto
, "git") == 0) {
7986 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7987 "sendfd dns inet unveil", NULL
) == -1)
7990 } else if (strcmp(proto
, "git+ssh") == 0 ||
7991 strcmp(proto
, "ssh") == 0) {
7993 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7994 "sendfd unveil", NULL
) == -1)
7997 } else if (strcmp(proto
, "http") == 0 ||
7998 strcmp(proto
, "git+http") == 0) {
7999 error
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
8002 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
8006 error
= got_dial_apply_unveil(proto
);
8010 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
8014 if (send_all_branches
) {
8015 error
= got_ref_list(&all_branches
, repo
, "refs/heads",
8016 got_ref_cmp_by_name
, NULL
);
8019 TAILQ_FOREACH(re
, &all_branches
, entry
) {
8020 const char *branchname
= got_ref_get_name(re
->ref
);
8021 error
= got_pathlist_append(&branches
,
8027 } else if (nbranches
== 0) {
8028 for (i
= 0; i
< remote
->nsend_branches
; i
++) {
8029 got_pathlist_append(&branches
,
8030 remote
->send_branches
[i
], NULL
);
8034 if (send_all_tags
) {
8035 error
= got_ref_list(&all_tags
, repo
, "refs/tags",
8036 got_ref_cmp_by_name
, NULL
);
8039 TAILQ_FOREACH(re
, &all_tags
, entry
) {
8040 const char *tagname
= got_ref_get_name(re
->ref
);
8041 error
= got_pathlist_append(&tags
,
8050 * To prevent accidents only branches in refs/heads/ can be deleted
8051 * with 'got send -d'.
8052 * Deleting anything else requires local repository access or Git.
8054 TAILQ_FOREACH(pe
, &delete_args
, entry
) {
8055 const char *branchname
= pe
->path
;
8057 struct got_pathlist_entry
*new;
8058 if (strncmp(branchname
, "refs/heads/", 11) == 0) {
8059 s
= strdup(branchname
);
8061 error
= got_error_from_errno("strdup");
8065 if (asprintf(&s
, "refs/heads/%s", branchname
) == -1) {
8066 error
= got_error_from_errno("asprintf");
8070 error
= got_pathlist_insert(&new, &delete_branches
, s
, NULL
);
8071 if (error
|| new == NULL
/* duplicate */)
8078 if (nbranches
== 0 && ndelete_branches
== 0) {
8079 struct got_reference
*head_ref
;
8081 error
= got_ref_open(&head_ref
, repo
,
8082 got_worktree_get_head_ref_name(worktree
), 0);
8084 error
= got_ref_open(&head_ref
, repo
, GOT_REF_HEAD
, 0);
8087 if (got_ref_is_symbolic(head_ref
)) {
8088 error
= got_ref_resolve_symbolic(&ref
, repo
, head_ref
);
8089 got_ref_close(head_ref
);
8094 error
= got_pathlist_append(&branches
, got_ref_get_name(ref
),
8102 printf("Connecting to \"%s\" %s%s%s\n", remote
->name
, host
,
8103 port
? ":" : "", port
? port
: "");
8105 error
= got_send_connect(&sendpid
, &sendfd
, proto
, host
, port
,
8106 server_path
, verbosity
);
8110 memset(&spa
, 0, sizeof(spa
));
8111 spa
.last_scaled_packsize
[0] = '\0';
8112 spa
.last_p_deltify
= -1;
8113 spa
.last_p_written
= -1;
8114 spa
.verbosity
= verbosity
;
8115 spa
.delete_branches
= &delete_branches
;
8116 error
= got_send_pack(remote_name
, &branches
, &tags
, &delete_branches
,
8117 verbosity
, overwrite_refs
, sendfd
, repo
, send_progress
, &spa
,
8118 check_cancelled
, NULL
);
8119 if (spa
.printed_something
)
8123 if (!spa
.sent_something
&& verbosity
>= 0)
8124 printf("Already up-to-date\n");
8127 if (kill(sendpid
, SIGTERM
) == -1)
8128 error
= got_error_from_errno("kill");
8129 if (waitpid(sendpid
, &sendstatus
, 0) == -1 && error
== NULL
)
8130 error
= got_error_from_errno("waitpid");
8132 if (sendfd
!= -1 && close(sendfd
) == -1 && error
== NULL
)
8133 error
= got_error_from_errno("close");
8135 const struct got_error
*close_err
= got_repo_close(repo
);
8140 got_worktree_close(worktree
);
8143 got_pathlist_free(&branches
);
8144 got_pathlist_free(&tags
);
8145 got_ref_list_free(&all_branches
);
8146 got_ref_list_free(&all_tags
);
8147 got_pathlist_free(&delete_args
);
8148 TAILQ_FOREACH(pe
, &delete_branches
, entry
)
8149 free((char *)pe
->path
);
8150 got_pathlist_free(&delete_branches
);
8162 usage_cherrypick(void)
8164 fprintf(stderr
, "usage: %s cherrypick commit-id\n", getprogname());
8168 static const struct got_error
*
8169 cmd_cherrypick(int argc
, char *argv
[])
8171 const struct got_error
*error
= NULL
;
8172 struct got_worktree
*worktree
= NULL
;
8173 struct got_repository
*repo
= NULL
;
8174 char *cwd
= NULL
, *commit_id_str
= NULL
;
8175 struct got_object_id
*commit_id
= NULL
;
8176 struct got_commit_object
*commit
= NULL
;
8177 struct got_object_qid
*pid
;
8179 struct got_update_progress_arg upa
;
8181 while ((ch
= getopt(argc
, argv
, "")) != -1) {
8193 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8194 "unveil", NULL
) == -1)
8200 cwd
= getcwd(NULL
, 0);
8202 error
= got_error_from_errno("getcwd");
8205 error
= got_worktree_open(&worktree
, cwd
);
8207 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
8208 error
= wrap_not_worktree_error(error
, "cherrypick",
8213 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
8218 error
= apply_unveil(got_repo_get_path(repo
), 0,
8219 got_worktree_get_root_path(worktree
));
8223 error
= got_repo_match_object_id_prefix(&commit_id
, argv
[0],
8224 GOT_OBJ_TYPE_COMMIT
, repo
);
8225 if (error
!= NULL
) {
8226 struct got_reference
*ref
;
8227 if (error
->code
!= GOT_ERR_BAD_OBJ_ID_STR
)
8229 error
= got_ref_open(&ref
, repo
, argv
[0], 0);
8232 error
= got_ref_resolve(&commit_id
, repo
, ref
);
8237 error
= got_object_id_str(&commit_id_str
, commit_id
);
8241 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8244 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
8245 memset(&upa
, 0, sizeof(upa
));
8246 error
= got_worktree_merge_files(worktree
, pid
? pid
->id
: NULL
,
8247 commit_id
, repo
, update_progress
, &upa
, check_cancelled
,
8252 if (upa
.did_something
)
8253 printf("Merged commit %s\n", commit_id_str
);
8254 print_merge_progress_stats(&upa
);
8257 got_object_commit_close(commit
);
8258 free(commit_id_str
);
8260 got_worktree_close(worktree
);
8262 const struct got_error
*close_err
= got_repo_close(repo
);
8272 fprintf(stderr
, "usage: %s backout commit-id\n", getprogname());
8276 static const struct got_error
*
8277 cmd_backout(int argc
, char *argv
[])
8279 const struct got_error
*error
= NULL
;
8280 struct got_worktree
*worktree
= NULL
;
8281 struct got_repository
*repo
= NULL
;
8282 char *cwd
= NULL
, *commit_id_str
= NULL
;
8283 struct got_object_id
*commit_id
= NULL
;
8284 struct got_commit_object
*commit
= NULL
;
8285 struct got_object_qid
*pid
;
8287 struct got_update_progress_arg upa
;
8289 while ((ch
= getopt(argc
, argv
, "")) != -1) {
8301 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8302 "unveil", NULL
) == -1)
8308 cwd
= getcwd(NULL
, 0);
8310 error
= got_error_from_errno("getcwd");
8313 error
= got_worktree_open(&worktree
, cwd
);
8315 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
8316 error
= wrap_not_worktree_error(error
, "backout", cwd
);
8320 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
8325 error
= apply_unveil(got_repo_get_path(repo
), 0,
8326 got_worktree_get_root_path(worktree
));
8330 error
= got_repo_match_object_id_prefix(&commit_id
, argv
[0],
8331 GOT_OBJ_TYPE_COMMIT
, repo
);
8332 if (error
!= NULL
) {
8333 struct got_reference
*ref
;
8334 if (error
->code
!= GOT_ERR_BAD_OBJ_ID_STR
)
8336 error
= got_ref_open(&ref
, repo
, argv
[0], 0);
8339 error
= got_ref_resolve(&commit_id
, repo
, ref
);
8344 error
= got_object_id_str(&commit_id_str
, commit_id
);
8348 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8351 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
8353 error
= got_error(GOT_ERR_ROOT_COMMIT
);
8357 memset(&upa
, 0, sizeof(upa
));
8358 error
= got_worktree_merge_files(worktree
, commit_id
, pid
->id
,
8359 repo
, update_progress
, &upa
, check_cancelled
, NULL
);
8363 if (upa
.did_something
)
8364 printf("Backed out commit %s\n", commit_id_str
);
8365 print_merge_progress_stats(&upa
);
8368 got_object_commit_close(commit
);
8369 free(commit_id_str
);
8371 got_worktree_close(worktree
);
8373 const struct got_error
*close_err
= got_repo_close(repo
);
8383 fprintf(stderr
, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8389 trim_logmsg(char *logmsg
, int limit
)
8394 len
= strlen(logmsg
);
8398 nl
= strchr(logmsg
, '\n');
8403 static const struct got_error
*
8404 get_short_logmsg(char **logmsg
, int limit
, struct got_commit_object
*commit
)
8406 const struct got_error
*err
;
8407 char *logmsg0
= NULL
;
8410 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
8415 while (isspace((unsigned char)s
[0]))
8418 *logmsg
= strdup(s
);
8419 if (*logmsg
== NULL
) {
8420 err
= got_error_from_errno("strdup");
8424 trim_logmsg(*logmsg
, limit
);
8430 static const struct got_error
*
8431 show_rebase_merge_conflict(struct got_object_id
*id
, struct got_repository
*repo
)
8433 const struct got_error
*err
;
8434 struct got_commit_object
*commit
= NULL
;
8435 char *id_str
= NULL
, *logmsg
= NULL
;
8437 err
= got_object_open_as_commit(&commit
, repo
, id
);
8441 err
= got_object_id_str(&id_str
, id
);
8447 err
= get_short_logmsg(&logmsg
, 42, commit
);
8451 printf("%s -> merge conflict: %s\n", id_str
, logmsg
);
8454 got_object_commit_close(commit
);
8459 static const struct got_error
*
8460 show_rebase_progress(struct got_commit_object
*commit
,
8461 struct got_object_id
*old_id
, struct got_object_id
*new_id
)
8463 const struct got_error
*err
;
8464 char *old_id_str
= NULL
, *new_id_str
= NULL
, *logmsg
= NULL
;
8466 err
= got_object_id_str(&old_id_str
, old_id
);
8471 err
= got_object_id_str(&new_id_str
, new_id
);
8476 old_id_str
[12] = '\0';
8478 new_id_str
[12] = '\0';
8480 err
= get_short_logmsg(&logmsg
, 42, commit
);
8484 printf("%s -> %s: %s\n", old_id_str
,
8485 new_id_str
? new_id_str
: "no-op change", logmsg
);
8493 static const struct got_error
*
8494 rebase_complete(struct got_worktree
*worktree
, struct got_fileindex
*fileindex
,
8495 struct got_reference
*branch
, struct got_reference
*new_base_branch
,
8496 struct got_reference
*tmp_branch
, struct got_repository
*repo
,
8499 printf("Switching work tree to %s\n", got_ref_get_name(branch
));
8500 return got_worktree_rebase_complete(worktree
, fileindex
,
8501 new_base_branch
, tmp_branch
, branch
, repo
, create_backup
);
8504 static const struct got_error
*
8505 rebase_commit(struct got_pathlist_head
*merged_paths
,
8506 struct got_worktree
*worktree
, struct got_fileindex
*fileindex
,
8507 struct got_reference
*tmp_branch
,
8508 struct got_object_id
*commit_id
, struct got_repository
*repo
)
8510 const struct got_error
*error
;
8511 struct got_commit_object
*commit
;
8512 struct got_object_id
*new_commit_id
;
8514 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8518 error
= got_worktree_rebase_commit(&new_commit_id
, merged_paths
,
8519 worktree
, fileindex
, tmp_branch
, commit
, commit_id
, repo
);
8521 if (error
->code
!= GOT_ERR_COMMIT_NO_CHANGES
)
8523 error
= show_rebase_progress(commit
, commit_id
, NULL
);
8525 error
= show_rebase_progress(commit
, commit_id
, new_commit_id
);
8526 free(new_commit_id
);
8529 got_object_commit_close(commit
);
8533 struct check_path_prefix_arg
{
8534 const char *path_prefix
;
8539 static const struct got_error
*
8540 check_path_prefix_in_diff(void *arg
, struct got_blob_object
*blob1
,
8541 struct got_blob_object
*blob2
, struct got_object_id
*id1
,
8542 struct got_object_id
*id2
, const char *path1
, const char *path2
,
8543 mode_t mode1
, mode_t mode2
, struct got_repository
*repo
)
8545 struct check_path_prefix_arg
*a
= arg
;
8547 if ((path1
&& !got_path_is_child(path1
, a
->path_prefix
, a
->len
)) ||
8548 (path2
&& !got_path_is_child(path2
, a
->path_prefix
, a
->len
)))
8549 return got_error(a
->errcode
);
8554 static const struct got_error
*
8555 check_path_prefix(struct got_object_id
*parent_id
,
8556 struct got_object_id
*commit_id
, const char *path_prefix
,
8557 int errcode
, struct got_repository
*repo
)
8559 const struct got_error
*err
;
8560 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
8561 struct got_commit_object
*commit
= NULL
, *parent_commit
= NULL
;
8562 struct check_path_prefix_arg cpp_arg
;
8564 if (got_path_is_root_dir(path_prefix
))
8567 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8571 err
= got_object_open_as_commit(&parent_commit
, repo
, parent_id
);
8575 err
= got_object_open_as_tree(&tree1
, repo
,
8576 got_object_commit_get_tree_id(parent_commit
));
8580 err
= got_object_open_as_tree(&tree2
, repo
,
8581 got_object_commit_get_tree_id(commit
));
8585 cpp_arg
.path_prefix
= path_prefix
;
8586 while (cpp_arg
.path_prefix
[0] == '/')
8587 cpp_arg
.path_prefix
++;
8588 cpp_arg
.len
= strlen(cpp_arg
.path_prefix
);
8589 cpp_arg
.errcode
= errcode
;
8590 err
= got_diff_tree(tree1
, tree2
, "", "", repo
,
8591 check_path_prefix_in_diff
, &cpp_arg
, 0);
8594 got_object_tree_close(tree1
);
8596 got_object_tree_close(tree2
);
8598 got_object_commit_close(commit
);
8600 got_object_commit_close(parent_commit
);
8604 static const struct got_error
*
8605 collect_commits(struct got_object_id_queue
*commits
,
8606 struct got_object_id
*initial_commit_id
,
8607 struct got_object_id
*iter_start_id
, struct got_object_id
*iter_stop_id
,
8608 const char *path_prefix
, int path_prefix_errcode
,
8609 struct got_repository
*repo
)
8611 const struct got_error
*err
= NULL
;
8612 struct got_commit_graph
*graph
= NULL
;
8613 struct got_object_id
*parent_id
= NULL
;
8614 struct got_object_qid
*qid
;
8615 struct got_object_id
*commit_id
= initial_commit_id
;
8617 err
= got_commit_graph_open(&graph
, "/", 1);
8621 err
= got_commit_graph_iter_start(graph
, iter_start_id
, repo
,
8622 check_cancelled
, NULL
);
8625 while (got_object_id_cmp(commit_id
, iter_stop_id
) != 0) {
8626 err
= got_commit_graph_iter_next(&parent_id
, graph
, repo
,
8627 check_cancelled
, NULL
);
8629 if (err
->code
== GOT_ERR_ITER_COMPLETED
) {
8630 err
= got_error_msg(GOT_ERR_ANCESTRY
,
8631 "ran out of commits to rebase before "
8632 "youngest common ancestor commit has "
8637 err
= check_path_prefix(parent_id
, commit_id
,
8638 path_prefix
, path_prefix_errcode
, repo
);
8642 err
= got_object_qid_alloc(&qid
, commit_id
);
8645 STAILQ_INSERT_HEAD(commits
, qid
, entry
);
8646 commit_id
= parent_id
;
8650 got_commit_graph_close(graph
);
8654 static const struct got_error
*
8655 get_commit_brief_str(char **brief_str
, struct got_commit_object
*commit
)
8657 const struct got_error
*err
= NULL
;
8658 time_t committer_time
;
8660 char datebuf
[11]; /* YYYY-MM-DD + NUL */
8661 char *author0
= NULL
, *author
, *smallerthan
;
8662 char *logmsg0
= NULL
, *logmsg
, *newline
;
8664 committer_time
= got_object_commit_get_committer_time(commit
);
8665 if (gmtime_r(&committer_time
, &tm
) == NULL
)
8666 return got_error_from_errno("gmtime_r");
8667 if (strftime(datebuf
, sizeof(datebuf
), "%G-%m-%d", &tm
) == 0)
8668 return got_error(GOT_ERR_NO_SPACE
);
8670 author0
= strdup(got_object_commit_get_author(commit
));
8671 if (author0
== NULL
)
8672 return got_error_from_errno("strdup");
8674 smallerthan
= strchr(author
, '<');
8675 if (smallerthan
&& smallerthan
[1] != '\0')
8676 author
= smallerthan
+ 1;
8677 author
[strcspn(author
, "@>")] = '\0';
8679 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
8683 while (*logmsg
== '\n')
8685 newline
= strchr(logmsg
, '\n');
8689 if (asprintf(brief_str
, "%s %s %s",
8690 datebuf
, author
, logmsg
) == -1)
8691 err
= got_error_from_errno("asprintf");
8698 static const struct got_error
*
8699 delete_backup_ref(struct got_reference
*ref
, struct got_object_id
*id
,
8700 struct got_repository
*repo
)
8702 const struct got_error
*err
;
8705 err
= got_object_id_str(&id_str
, id
);
8709 err
= got_ref_delete(ref
, repo
);
8713 printf("Deleted %s: %s\n", got_ref_get_name(ref
), id_str
);
8719 static const struct got_error
*
8720 print_backup_ref(const char *branch_name
, const char *new_id_str
,
8721 struct got_object_id
*old_commit_id
, struct got_commit_object
*old_commit
,
8722 struct got_reflist_object_id_map
*refs_idmap
,
8723 struct got_repository
*repo
)
8725 const struct got_error
*err
= NULL
;
8726 struct got_reflist_head
*refs
;
8727 char *refs_str
= NULL
;
8728 struct got_object_id
*new_commit_id
= NULL
;
8729 struct got_commit_object
*new_commit
= NULL
;
8730 char *new_commit_brief_str
= NULL
;
8731 struct got_object_id
*yca_id
= NULL
;
8732 struct got_commit_object
*yca_commit
= NULL
;
8733 char *yca_id_str
= NULL
, *yca_brief_str
= NULL
;
8734 char *custom_refs_str
;
8736 if (asprintf(&custom_refs_str
, "formerly %s", branch_name
) == -1)
8737 return got_error_from_errno("asprintf");
8739 err
= print_commit(old_commit
, old_commit_id
, repo
, NULL
, NULL
,
8740 0, 0, refs_idmap
, custom_refs_str
);
8744 err
= got_object_resolve_id_str(&new_commit_id
, repo
, new_id_str
);
8748 refs
= got_reflist_object_id_map_lookup(refs_idmap
, new_commit_id
);
8750 err
= build_refs_str(&refs_str
, refs
, new_commit_id
, repo
);
8755 err
= got_object_open_as_commit(&new_commit
, repo
, new_commit_id
);
8759 err
= get_commit_brief_str(&new_commit_brief_str
, new_commit
);
8763 err
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
8764 old_commit_id
, new_commit_id
, 1, repo
, check_cancelled
, NULL
);
8768 printf("has become commit %s%s%s%s\n %s\n", new_id_str
,
8769 refs_str
? " (" : "", refs_str
? refs_str
: "",
8770 refs_str
? ")" : "", new_commit_brief_str
);
8771 if (yca_id
&& got_object_id_cmp(yca_id
, new_commit_id
) != 0 &&
8772 got_object_id_cmp(yca_id
, old_commit_id
) != 0) {
8776 err
= got_object_open_as_commit(&yca_commit
, repo
, yca_id
);
8780 err
= get_commit_brief_str(&yca_brief_str
, yca_commit
);
8784 err
= got_object_id_str(&yca_id_str
, yca_id
);
8788 refs
= got_reflist_object_id_map_lookup(refs_idmap
, yca_id
);
8790 err
= build_refs_str(&refs_str
, refs
, yca_id
, repo
);
8794 printf("history forked at %s%s%s%s\n %s\n",
8796 refs_str
? " (" : "", refs_str
? refs_str
: "",
8797 refs_str
? ")" : "", yca_brief_str
);
8800 free(custom_refs_str
);
8801 free(new_commit_id
);
8805 free(yca_brief_str
);
8807 got_object_commit_close(new_commit
);
8809 got_object_commit_close(yca_commit
);
8814 static const struct got_error
*
8815 process_backup_refs(const char *backup_ref_prefix
, const char *wanted_branch_name
,
8816 int delete, struct got_repository
*repo
)
8818 const struct got_error
*err
;
8819 struct got_reflist_head refs
, backup_refs
;
8820 struct got_reflist_entry
*re
;
8821 const size_t backup_ref_prefix_len
= strlen(backup_ref_prefix
);
8822 struct got_object_id
*old_commit_id
= NULL
;
8823 char *branch_name
= NULL
;
8824 struct got_commit_object
*old_commit
= NULL
;
8825 struct got_reflist_object_id_map
*refs_idmap
= NULL
;
8826 int wanted_branch_found
= 0;
8829 TAILQ_INIT(&backup_refs
);
8831 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
8835 err
= got_reflist_object_id_map_create(&refs_idmap
, &refs
, repo
);
8839 if (wanted_branch_name
) {
8840 if (strncmp(wanted_branch_name
, "refs/heads/", 11) == 0)
8841 wanted_branch_name
+= 11;
8844 err
= got_ref_list(&backup_refs
, repo
, backup_ref_prefix
,
8845 got_ref_cmp_by_commit_timestamp_descending
, repo
);
8849 TAILQ_FOREACH(re
, &backup_refs
, entry
) {
8850 const char *refname
= got_ref_get_name(re
->ref
);
8853 err
= check_cancelled(NULL
);
8857 err
= got_ref_resolve(&old_commit_id
, repo
, re
->ref
);
8861 err
= got_object_open_as_commit(&old_commit
, repo
,
8866 if (strncmp(backup_ref_prefix
, refname
,
8867 backup_ref_prefix_len
) == 0)
8868 refname
+= backup_ref_prefix_len
;
8870 while (refname
[0] == '/')
8873 branch_name
= strdup(refname
);
8874 if (branch_name
== NULL
) {
8875 err
= got_error_from_errno("strdup");
8878 slash
= strrchr(branch_name
, '/');
8881 refname
+= strlen(branch_name
) + 1;
8884 if (wanted_branch_name
== NULL
||
8885 strcmp(wanted_branch_name
, branch_name
) == 0) {
8886 wanted_branch_found
= 1;
8888 err
= delete_backup_ref(re
->ref
,
8889 old_commit_id
, repo
);
8891 err
= print_backup_ref(branch_name
, refname
,
8892 old_commit_id
, old_commit
, refs_idmap
,
8899 free(old_commit_id
);
8900 old_commit_id
= NULL
;
8903 got_object_commit_close(old_commit
);
8907 if (wanted_branch_name
&& !wanted_branch_found
) {
8908 err
= got_error_fmt(GOT_ERR_NOT_REF
,
8909 "%s/%s/", backup_ref_prefix
, wanted_branch_name
);
8913 got_reflist_object_id_map_free(refs_idmap
);
8914 got_ref_list_free(&refs
);
8915 got_ref_list_free(&backup_refs
);
8916 free(old_commit_id
);
8919 got_object_commit_close(old_commit
);
8923 static const struct got_error
*
8924 abort_progress(void *arg
, unsigned char status
, const char *path
)
8927 * Unversioned files should not clutter progress output when
8928 * an operation is aborted.
8930 if (status
== GOT_STATUS_UNVERSIONED
)
8933 return update_progress(arg
, status
, path
);
8936 static const struct got_error
*
8937 cmd_rebase(int argc
, char *argv
[])
8939 const struct got_error
*error
= NULL
;
8940 struct got_worktree
*worktree
= NULL
;
8941 struct got_repository
*repo
= NULL
;
8942 struct got_fileindex
*fileindex
= NULL
;
8944 struct got_reference
*branch
= NULL
;
8945 struct got_reference
*new_base_branch
= NULL
, *tmp_branch
= NULL
;
8946 struct got_object_id
*commit_id
= NULL
, *parent_id
= NULL
;
8947 struct got_object_id
*resume_commit_id
= NULL
;
8948 struct got_object_id
*branch_head_commit_id
= NULL
, *yca_id
= NULL
;
8949 struct got_commit_object
*commit
= NULL
;
8950 int ch
, rebase_in_progress
= 0, abort_rebase
= 0, continue_rebase
= 0;
8951 int histedit_in_progress
= 0, merge_in_progress
= 0;
8952 int create_backup
= 1, list_backups
= 0, delete_backups
= 0;
8953 struct got_object_id_queue commits
;
8954 struct got_pathlist_head merged_paths
;
8955 const struct got_object_id_queue
*parent_ids
;
8956 struct got_object_qid
*qid
, *pid
;
8957 struct got_update_progress_arg upa
;
8959 STAILQ_INIT(&commits
);
8960 TAILQ_INIT(&merged_paths
);
8961 memset(&upa
, 0, sizeof(upa
));
8963 while ((ch
= getopt(argc
, argv
, "aclX")) != -1) {
8969 continue_rebase
= 1;
8987 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8988 "unveil", NULL
) == -1)
8993 option_conflict('l', 'a');
8994 if (continue_rebase
)
8995 option_conflict('l', 'c');
8997 option_conflict('l', 'X');
8998 if (argc
!= 0 && argc
!= 1)
9000 } else if (delete_backups
) {
9002 option_conflict('X', 'a');
9003 if (continue_rebase
)
9004 option_conflict('X', 'c');
9006 option_conflict('l', 'X');
9007 if (argc
!= 0 && argc
!= 1)
9010 if (abort_rebase
&& continue_rebase
)
9012 else if (abort_rebase
|| continue_rebase
) {
9015 } else if (argc
!= 1)
9019 cwd
= getcwd(NULL
, 0);
9021 error
= got_error_from_errno("getcwd");
9024 error
= got_worktree_open(&worktree
, cwd
);
9026 if (list_backups
|| delete_backups
) {
9027 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
9030 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
9031 error
= wrap_not_worktree_error(error
,
9037 error
= got_repo_open(&repo
,
9038 worktree
? got_worktree_get_repo_path(worktree
) : cwd
, NULL
);
9042 error
= apply_unveil(got_repo_get_path(repo
), 0,
9043 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
9047 if (list_backups
|| delete_backups
) {
9048 error
= process_backup_refs(
9049 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX
,
9050 argc
== 1 ? argv
[0] : NULL
, delete_backups
, repo
);
9051 goto done
; /* nothing else to do */
9054 error
= got_worktree_histedit_in_progress(&histedit_in_progress
,
9058 if (histedit_in_progress
) {
9059 error
= got_error(GOT_ERR_HISTEDIT_BUSY
);
9063 error
= got_worktree_merge_in_progress(&merge_in_progress
,
9067 if (merge_in_progress
) {
9068 error
= got_error(GOT_ERR_MERGE_BUSY
);
9072 error
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
9077 if (!rebase_in_progress
) {
9078 error
= got_error(GOT_ERR_NOT_REBASING
);
9081 error
= got_worktree_rebase_continue(&resume_commit_id
,
9082 &new_base_branch
, &tmp_branch
, &branch
, &fileindex
,
9086 printf("Switching work tree to %s\n",
9087 got_ref_get_symref_target(new_base_branch
));
9088 error
= got_worktree_rebase_abort(worktree
, fileindex
, repo
,
9089 new_base_branch
, abort_progress
, &upa
);
9092 printf("Rebase of %s aborted\n", got_ref_get_name(branch
));
9093 print_merge_progress_stats(&upa
);
9094 goto done
; /* nothing else to do */
9097 if (continue_rebase
) {
9098 if (!rebase_in_progress
) {
9099 error
= got_error(GOT_ERR_NOT_REBASING
);
9102 error
= got_worktree_rebase_continue(&resume_commit_id
,
9103 &new_base_branch
, &tmp_branch
, &branch
, &fileindex
,
9108 error
= rebase_commit(NULL
, worktree
, fileindex
, tmp_branch
,
9109 resume_commit_id
, repo
);
9113 yca_id
= got_object_id_dup(resume_commit_id
);
9114 if (yca_id
== NULL
) {
9115 error
= got_error_from_errno("got_object_id_dup");
9119 error
= got_ref_open(&branch
, repo
, argv
[0], 0);
9124 error
= got_ref_resolve(&branch_head_commit_id
, repo
, branch
);
9128 if (!continue_rebase
) {
9129 struct got_object_id
*base_commit_id
;
9131 base_commit_id
= got_worktree_get_base_commit_id(worktree
);
9132 error
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
9133 base_commit_id
, branch_head_commit_id
, 1, repo
,
9134 check_cancelled
, NULL
);
9137 if (yca_id
== NULL
) {
9138 error
= got_error_msg(GOT_ERR_ANCESTRY
,
9139 "specified branch shares no common ancestry "
9140 "with work tree's branch");
9144 error
= check_same_branch(base_commit_id
, branch
, yca_id
, repo
);
9146 if (error
->code
!= GOT_ERR_ANCESTRY
)
9150 static char msg
[128];
9151 snprintf(msg
, sizeof(msg
),
9152 "%s is already based on %s",
9153 got_ref_get_name(branch
),
9154 got_worktree_get_head_ref_name(worktree
));
9155 error
= got_error_msg(GOT_ERR_SAME_BRANCH
, msg
);
9158 error
= got_worktree_rebase_prepare(&new_base_branch
,
9159 &tmp_branch
, &fileindex
, worktree
, branch
, repo
);
9164 commit_id
= branch_head_commit_id
;
9165 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
9169 parent_ids
= got_object_commit_get_parent_ids(commit
);
9170 pid
= STAILQ_FIRST(parent_ids
);
9172 if (!continue_rebase
) {
9173 error
= got_worktree_rebase_abort(worktree
, fileindex
,
9174 repo
, new_base_branch
, abort_progress
, &upa
);
9177 printf("Rebase of %s aborted\n",
9178 got_ref_get_name(branch
));
9179 print_merge_progress_stats(&upa
);
9182 error
= got_error(GOT_ERR_EMPTY_REBASE
);
9185 error
= collect_commits(&commits
, commit_id
, pid
->id
,
9186 yca_id
, got_worktree_get_path_prefix(worktree
),
9187 GOT_ERR_REBASE_PATH
, repo
);
9188 got_object_commit_close(commit
);
9193 if (STAILQ_EMPTY(&commits
)) {
9194 if (continue_rebase
) {
9195 error
= rebase_complete(worktree
, fileindex
,
9196 branch
, new_base_branch
, tmp_branch
, repo
,
9200 /* Fast-forward the reference of the branch. */
9201 struct got_object_id
*new_head_commit_id
;
9203 error
= got_ref_resolve(&new_head_commit_id
, repo
,
9207 error
= got_object_id_str(&id_str
, new_head_commit_id
);
9208 printf("Forwarding %s to commit %s\n",
9209 got_ref_get_name(branch
), id_str
);
9211 error
= got_ref_change_ref(branch
,
9212 new_head_commit_id
);
9215 /* No backup needed since objects did not change. */
9221 STAILQ_FOREACH(qid
, &commits
, entry
) {
9223 commit_id
= qid
->id
;
9224 parent_id
= pid
? pid
->id
: yca_id
;
9227 memset(&upa
, 0, sizeof(upa
));
9228 error
= got_worktree_rebase_merge_files(&merged_paths
,
9229 worktree
, fileindex
, parent_id
, commit_id
, repo
,
9230 update_progress
, &upa
, check_cancelled
, NULL
);
9234 print_merge_progress_stats(&upa
);
9235 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
9236 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
9237 if (upa
.conflicts
> 0) {
9238 error
= show_rebase_merge_conflict(qid
->id
,
9243 got_worktree_rebase_pathlist_free(&merged_paths
);
9247 error
= rebase_commit(&merged_paths
, worktree
, fileindex
,
9248 tmp_branch
, commit_id
, repo
);
9249 got_worktree_rebase_pathlist_free(&merged_paths
);
9254 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
9255 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
9256 error
= got_worktree_rebase_postpone(worktree
, fileindex
);
9259 if (upa
.conflicts
> 0 && upa
.missing
== 0 &&
9260 upa
.not_deleted
== 0 && upa
.unversioned
== 0) {
9261 error
= got_error_msg(GOT_ERR_CONFLICTS
,
9262 "conflicts must be resolved before rebasing "
9264 } else if (upa
.conflicts
> 0) {
9265 error
= got_error_msg(GOT_ERR_CONFLICTS
,
9266 "conflicts must be resolved before rebasing "
9267 "can continue; changes destined for some "
9268 "files were not yet merged and should be "
9269 "merged manually if required before the "
9270 "rebase operation is continued");
9272 error
= got_error_msg(GOT_ERR_CONFLICTS
,
9273 "changes destined for some files were not "
9274 "yet merged and should be merged manually "
9275 "if required before the rebase operation "
9279 error
= rebase_complete(worktree
, fileindex
, branch
,
9280 new_base_branch
, tmp_branch
, repo
, create_backup
);
9282 got_object_id_queue_free(&commits
);
9283 free(branch_head_commit_id
);
9284 free(resume_commit_id
);
9287 got_object_commit_close(commit
);
9289 got_ref_close(branch
);
9290 if (new_base_branch
)
9291 got_ref_close(new_base_branch
);
9293 got_ref_close(tmp_branch
);
9295 got_worktree_close(worktree
);
9297 const struct got_error
*close_err
= got_repo_close(repo
);
9305 usage_histedit(void)
9307 fprintf(stderr
, "usage: %s histedit [-a] [-c] [-e] [-f] "
9308 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9313 #define GOT_HISTEDIT_PICK 'p'
9314 #define GOT_HISTEDIT_EDIT 'e'
9315 #define GOT_HISTEDIT_FOLD 'f'
9316 #define GOT_HISTEDIT_DROP 'd'
9317 #define GOT_HISTEDIT_MESG 'm'
9319 static struct got_histedit_cmd
{
9323 } got_histedit_cmds
[] = {
9324 { GOT_HISTEDIT_PICK
, "pick", "use commit" },
9325 { GOT_HISTEDIT_EDIT
, "edit", "use commit but stop for amending" },
9326 { GOT_HISTEDIT_FOLD
, "fold", "combine with next commit that will "
9328 { GOT_HISTEDIT_DROP
, "drop", "remove commit from history" },
9329 { GOT_HISTEDIT_MESG
, "mesg",
9330 "single-line log message for commit above (open editor if empty)" },
9333 struct got_histedit_list_entry
{
9334 TAILQ_ENTRY(got_histedit_list_entry
) entry
;
9335 struct got_object_id
*commit_id
;
9336 const struct got_histedit_cmd
*cmd
;
9339 TAILQ_HEAD(got_histedit_list
, got_histedit_list_entry
);
9341 static const struct got_error
*
9342 histedit_write_commit(struct got_object_id
*commit_id
, const char *cmdname
,
9343 FILE *f
, struct got_repository
*repo
)
9345 const struct got_error
*err
= NULL
;
9346 char *logmsg
= NULL
, *id_str
= NULL
;
9347 struct got_commit_object
*commit
= NULL
;
9350 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
9354 err
= get_short_logmsg(&logmsg
, 34, commit
);
9358 err
= got_object_id_str(&id_str
, commit_id
);
9362 n
= fprintf(f
, "%s %s %s\n", cmdname
, id_str
, logmsg
);
9364 err
= got_ferror(f
, GOT_ERR_IO
);
9367 got_object_commit_close(commit
);
9373 static const struct got_error
*
9374 histedit_write_commit_list(struct got_object_id_queue
*commits
,
9375 FILE *f
, int edit_logmsg_only
, int fold_only
, int edit_only
,
9376 struct got_repository
*repo
)
9378 const struct got_error
*err
= NULL
;
9379 struct got_object_qid
*qid
;
9380 const char *histedit_cmd
= NULL
;
9382 if (STAILQ_EMPTY(commits
))
9383 return got_error(GOT_ERR_EMPTY_HISTEDIT
);
9385 STAILQ_FOREACH(qid
, commits
, entry
) {
9386 histedit_cmd
= got_histedit_cmds
[0].name
;
9388 histedit_cmd
= "edit";
9389 else if (fold_only
&& STAILQ_NEXT(qid
, entry
) != NULL
)
9390 histedit_cmd
= "fold";
9391 err
= histedit_write_commit(qid
->id
, histedit_cmd
, f
, repo
);
9394 if (edit_logmsg_only
) {
9395 int n
= fprintf(f
, "%c\n", GOT_HISTEDIT_MESG
);
9397 err
= got_ferror(f
, GOT_ERR_IO
);
9406 static const struct got_error
*
9407 write_cmd_list(FILE *f
, const char *branch_name
,
9408 struct got_object_id_queue
*commits
)
9410 const struct got_error
*err
= NULL
;
9414 struct got_object_qid
*qid
;
9416 qid
= STAILQ_FIRST(commits
);
9417 err
= got_object_id_str(&id_str
, qid
->id
);
9422 "# Editing the history of branch '%s' starting at\n"
9424 "# Commits will be processed in order from top to "
9425 "bottom of this file.\n", branch_name
, id_str
);
9427 err
= got_ferror(f
, GOT_ERR_IO
);
9431 n
= fprintf(f
, "# Available histedit commands:\n");
9433 err
= got_ferror(f
, GOT_ERR_IO
);
9437 for (i
= 0; i
< nitems(got_histedit_cmds
); i
++) {
9438 struct got_histedit_cmd
*cmd
= &got_histedit_cmds
[i
];
9439 n
= fprintf(f
, "# %s (%c): %s\n", cmd
->name
, cmd
->code
,
9442 err
= got_ferror(f
, GOT_ERR_IO
);
9451 static const struct got_error
*
9452 histedit_syntax_error(int lineno
)
9454 static char msg
[42];
9457 ret
= snprintf(msg
, sizeof(msg
), "histedit syntax error on line %d",
9459 if (ret
== -1 || ret
>= sizeof(msg
))
9460 return got_error(GOT_ERR_HISTEDIT_SYNTAX
);
9462 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX
, msg
);
9465 static const struct got_error
*
9466 append_folded_commit_msg(char **new_msg
, struct got_histedit_list_entry
*hle
,
9467 char *logmsg
, struct got_repository
*repo
)
9469 const struct got_error
*err
;
9470 struct got_commit_object
*folded_commit
= NULL
;
9471 char *id_str
, *folded_logmsg
= NULL
;
9473 err
= got_object_id_str(&id_str
, hle
->commit_id
);
9477 err
= got_object_open_as_commit(&folded_commit
, repo
, hle
->commit_id
);
9481 err
= got_object_commit_get_logmsg(&folded_logmsg
, folded_commit
);
9484 if (asprintf(new_msg
, "%s%s# log message of folded commit %s: %s",
9485 logmsg
? logmsg
: "", logmsg
? "\n" : "", id_str
,
9486 folded_logmsg
) == -1) {
9487 err
= got_error_from_errno("asprintf");
9491 got_object_commit_close(folded_commit
);
9493 free(folded_logmsg
);
9497 static struct got_histedit_list_entry
*
9498 get_folded_commits(struct got_histedit_list_entry
*hle
)
9500 struct got_histedit_list_entry
*prev
, *folded
= NULL
;
9502 prev
= TAILQ_PREV(hle
, got_histedit_list
, entry
);
9503 while (prev
&& (prev
->cmd
->code
== GOT_HISTEDIT_FOLD
||
9504 prev
->cmd
->code
== GOT_HISTEDIT_DROP
)) {
9505 if (prev
->cmd
->code
== GOT_HISTEDIT_FOLD
)
9507 prev
= TAILQ_PREV(prev
, got_histedit_list
, entry
);
9513 static const struct got_error
*
9514 histedit_edit_logmsg(struct got_histedit_list_entry
*hle
,
9515 struct got_repository
*repo
)
9517 char *logmsg_path
= NULL
, *id_str
= NULL
, *orig_logmsg
= NULL
;
9518 char *logmsg
= NULL
, *new_msg
= NULL
, *editor
= NULL
;
9519 const struct got_error
*err
= NULL
;
9520 struct got_commit_object
*commit
= NULL
;
9523 struct got_histedit_list_entry
*folded
= NULL
;
9525 err
= got_object_open_as_commit(&commit
, repo
, hle
->commit_id
);
9529 folded
= get_folded_commits(hle
);
9531 while (folded
!= hle
) {
9532 if (folded
->cmd
->code
== GOT_HISTEDIT_DROP
) {
9533 folded
= TAILQ_NEXT(folded
, entry
);
9536 err
= append_folded_commit_msg(&new_msg
, folded
,
9542 folded
= TAILQ_NEXT(folded
, entry
);
9546 err
= got_object_id_str(&id_str
, hle
->commit_id
);
9549 err
= got_object_commit_get_logmsg(&orig_logmsg
, commit
);
9552 logmsg_len
= asprintf(&new_msg
,
9553 "%s\n# original log message of commit %s: %s",
9554 logmsg
? logmsg
: "", id_str
, orig_logmsg
);
9555 if (logmsg_len
== -1) {
9556 err
= got_error_from_errno("asprintf");
9562 err
= got_object_id_str(&id_str
, hle
->commit_id
);
9566 err
= got_opentemp_named_fd(&logmsg_path
, &fd
,
9567 GOT_TMPDIR_STR
"/got-logmsg");
9571 write(fd
, logmsg
, logmsg_len
);
9574 err
= get_editor(&editor
);
9578 err
= edit_logmsg(&hle
->logmsg
, editor
, logmsg_path
, logmsg
,
9581 if (err
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
)
9584 hle
->logmsg
= strdup(new_msg
);
9585 if (hle
->logmsg
== NULL
)
9586 err
= got_error_from_errno("strdup");
9589 if (logmsg_path
&& unlink(logmsg_path
) != 0 && err
== NULL
)
9590 err
= got_error_from_errno2("unlink", logmsg_path
);
9596 got_object_commit_close(commit
);
9600 static const struct got_error
*
9601 histedit_parse_list(struct got_histedit_list
*histedit_cmds
,
9602 FILE *f
, struct got_repository
*repo
)
9604 const struct got_error
*err
= NULL
;
9605 char *line
= NULL
, *p
, *end
;
9609 const struct got_histedit_cmd
*cmd
;
9610 struct got_object_id
*commit_id
= NULL
;
9611 struct got_histedit_list_entry
*hle
= NULL
;
9614 len
= getline(&line
, &size
, f
);
9616 const struct got_error
*getline_err
;
9619 getline_err
= got_error_from_errno("getline");
9620 err
= got_ferror(f
, getline_err
->code
);
9625 while (isspace((unsigned char)p
[0]))
9627 if (p
[0] == '#' || p
[0] == '\0') {
9633 for (i
= 0; i
< nitems(got_histedit_cmds
); i
++) {
9634 cmd
= &got_histedit_cmds
[i
];
9635 if (strncmp(cmd
->name
, p
, strlen(cmd
->name
)) == 0 &&
9636 isspace((unsigned char)p
[strlen(cmd
->name
)])) {
9637 p
+= strlen(cmd
->name
);
9640 if (p
[0] == cmd
->code
&& isspace((unsigned char)p
[1])) {
9645 if (i
== nitems(got_histedit_cmds
)) {
9646 err
= histedit_syntax_error(lineno
);
9649 while (isspace((unsigned char)p
[0]))
9651 if (cmd
->code
== GOT_HISTEDIT_MESG
) {
9652 if (hle
== NULL
|| hle
->logmsg
!= NULL
) {
9653 err
= got_error(GOT_ERR_HISTEDIT_CMD
);
9657 err
= histedit_edit_logmsg(hle
, repo
);
9661 hle
->logmsg
= strdup(p
);
9662 if (hle
->logmsg
== NULL
) {
9663 err
= got_error_from_errno("strdup");
9672 while (end
[0] && !isspace((unsigned char)end
[0]))
9676 err
= got_object_resolve_id_str(&commit_id
, repo
, p
);
9678 /* override error code */
9679 err
= histedit_syntax_error(lineno
);
9683 hle
= malloc(sizeof(*hle
));
9685 err
= got_error_from_errno("malloc");
9689 hle
->commit_id
= commit_id
;
9694 TAILQ_INSERT_TAIL(histedit_cmds
, hle
, entry
);
9702 static const struct got_error
*
9703 histedit_check_script(struct got_histedit_list
*histedit_cmds
,
9704 struct got_object_id_queue
*commits
, struct got_repository
*repo
)
9706 const struct got_error
*err
= NULL
;
9707 struct got_object_qid
*qid
;
9708 struct got_histedit_list_entry
*hle
;
9709 static char msg
[92];
9712 if (TAILQ_EMPTY(histedit_cmds
))
9713 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT
,
9714 "histedit script contains no commands");
9715 if (STAILQ_EMPTY(commits
))
9716 return got_error(GOT_ERR_EMPTY_HISTEDIT
);
9718 TAILQ_FOREACH(hle
, histedit_cmds
, entry
) {
9719 struct got_histedit_list_entry
*hle2
;
9720 TAILQ_FOREACH(hle2
, histedit_cmds
, entry
) {
9723 if (got_object_id_cmp(hle
->commit_id
,
9724 hle2
->commit_id
) != 0)
9726 err
= got_object_id_str(&id_str
, hle
->commit_id
);
9729 snprintf(msg
, sizeof(msg
), "commit %s is listed "
9730 "more than once in histedit script", id_str
);
9732 return got_error_msg(GOT_ERR_HISTEDIT_CMD
, msg
);
9736 STAILQ_FOREACH(qid
, commits
, entry
) {
9737 TAILQ_FOREACH(hle
, histedit_cmds
, entry
) {
9738 if (got_object_id_cmp(qid
->id
, hle
->commit_id
) == 0)
9742 err
= got_object_id_str(&id_str
, qid
->id
);
9745 snprintf(msg
, sizeof(msg
),
9746 "commit %s missing from histedit script", id_str
);
9748 return got_error_msg(GOT_ERR_HISTEDIT_CMD
, msg
);
9752 hle
= TAILQ_LAST(histedit_cmds
, got_histedit_list
);
9753 if (hle
&& hle
->cmd
->code
== GOT_HISTEDIT_FOLD
)
9754 return got_error_msg(GOT_ERR_HISTEDIT_CMD
,
9755 "last commit in histedit script cannot be folded");
9760 static const struct got_error
*
9761 histedit_run_editor(struct got_histedit_list
*histedit_cmds
,
9762 const char *path
, struct got_object_id_queue
*commits
,
9763 struct got_repository
*repo
)
9765 const struct got_error
*err
= NULL
;
9769 err
= get_editor(&editor
);
9773 if (spawn_editor(editor
, path
) == -1) {
9774 err
= got_error_from_errno("failed spawning editor");
9778 f
= fopen(path
, "r");
9780 err
= got_error_from_errno("fopen");
9783 err
= histedit_parse_list(histedit_cmds
, f
, repo
);
9787 err
= histedit_check_script(histedit_cmds
, commits
, repo
);
9789 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
9790 err
= got_error_from_errno("fclose");
9795 static const struct got_error
*
9796 histedit_edit_list_retry(struct got_histedit_list
*, const struct got_error
*,
9797 struct got_object_id_queue
*, const char *, const char *,
9798 struct got_repository
*);
9800 static const struct got_error
*
9801 histedit_edit_script(struct got_histedit_list
*histedit_cmds
,
9802 struct got_object_id_queue
*commits
, const char *branch_name
,
9803 int edit_logmsg_only
, int fold_only
, int edit_only
,
9804 struct got_repository
*repo
)
9806 const struct got_error
*err
;
9810 err
= got_opentemp_named(&path
, &f
, "got-histedit");
9814 err
= write_cmd_list(f
, branch_name
, commits
);
9818 err
= histedit_write_commit_list(commits
, f
, edit_logmsg_only
,
9819 fold_only
, edit_only
, repo
);
9823 if (edit_logmsg_only
|| fold_only
|| edit_only
) {
9825 err
= histedit_parse_list(histedit_cmds
, f
, repo
);
9827 if (fclose(f
) == EOF
) {
9828 err
= got_error_from_errno("fclose");
9832 err
= histedit_run_editor(histedit_cmds
, path
, commits
, repo
);
9834 if (err
->code
!= GOT_ERR_HISTEDIT_SYNTAX
&&
9835 err
->code
!= GOT_ERR_HISTEDIT_CMD
)
9837 err
= histedit_edit_list_retry(histedit_cmds
, err
,
9838 commits
, path
, branch_name
, repo
);
9842 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
9843 err
= got_error_from_errno("fclose");
9844 if (path
&& unlink(path
) != 0 && err
== NULL
)
9845 err
= got_error_from_errno2("unlink", path
);
9850 static const struct got_error
*
9851 histedit_save_list(struct got_histedit_list
*histedit_cmds
,
9852 struct got_worktree
*worktree
, struct got_repository
*repo
)
9854 const struct got_error
*err
= NULL
;
9857 struct got_histedit_list_entry
*hle
;
9858 struct got_commit_object
*commit
= NULL
;
9860 err
= got_worktree_get_histedit_script_path(&path
, worktree
);
9864 f
= fopen(path
, "w");
9866 err
= got_error_from_errno2("fopen", path
);
9869 TAILQ_FOREACH(hle
, histedit_cmds
, entry
) {
9870 err
= histedit_write_commit(hle
->commit_id
, hle
->cmd
->name
, f
,
9876 int n
= fprintf(f
, "%c %s\n",
9877 GOT_HISTEDIT_MESG
, hle
->logmsg
);
9879 err
= got_ferror(f
, GOT_ERR_IO
);
9885 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
9886 err
= got_error_from_errno("fclose");
9889 got_object_commit_close(commit
);
9894 histedit_free_list(struct got_histedit_list
*histedit_cmds
)
9896 struct got_histedit_list_entry
*hle
;
9898 while ((hle
= TAILQ_FIRST(histedit_cmds
))) {
9899 TAILQ_REMOVE(histedit_cmds
, hle
, entry
);
9904 static const struct got_error
*
9905 histedit_load_list(struct got_histedit_list
*histedit_cmds
,
9906 const char *path
, struct got_repository
*repo
)
9908 const struct got_error
*err
= NULL
;
9911 f
= fopen(path
, "r");
9913 err
= got_error_from_errno2("fopen", path
);
9917 err
= histedit_parse_list(histedit_cmds
, f
, repo
);
9919 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
9920 err
= got_error_from_errno("fclose");
9924 static const struct got_error
*
9925 histedit_edit_list_retry(struct got_histedit_list
*histedit_cmds
,
9926 const struct got_error
*edit_err
, struct got_object_id_queue
*commits
,
9927 const char *path
, const char *branch_name
, struct got_repository
*repo
)
9929 const struct got_error
*err
= NULL
, *prev_err
= edit_err
;
9932 while (resp
!= 'c' && resp
!= 'r' && resp
!= 'a') {
9933 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9934 "or (a)bort: ", getprogname(), prev_err
->msg
);
9939 histedit_free_list(histedit_cmds
);
9940 err
= histedit_run_editor(histedit_cmds
, path
, commits
,
9943 if (err
->code
!= GOT_ERR_HISTEDIT_SYNTAX
&&
9944 err
->code
!= GOT_ERR_HISTEDIT_CMD
)
9951 } else if (resp
== 'r') {
9952 histedit_free_list(histedit_cmds
);
9953 err
= histedit_edit_script(histedit_cmds
,
9954 commits
, branch_name
, 0, 0, 0, repo
);
9956 if (err
->code
!= GOT_ERR_HISTEDIT_SYNTAX
&&
9957 err
->code
!= GOT_ERR_HISTEDIT_CMD
)
9964 } else if (resp
== 'a') {
9965 err
= got_error(GOT_ERR_HISTEDIT_CANCEL
);
9968 printf("invalid response '%c'\n", resp
);
9974 static const struct got_error
*
9975 histedit_complete(struct got_worktree
*worktree
,
9976 struct got_fileindex
*fileindex
, struct got_reference
*tmp_branch
,
9977 struct got_reference
*branch
, struct got_repository
*repo
)
9979 printf("Switching work tree to %s\n",
9980 got_ref_get_symref_target(branch
));
9981 return got_worktree_histedit_complete(worktree
, fileindex
, tmp_branch
,
9985 static const struct got_error
*
9986 show_histedit_progress(struct got_commit_object
*commit
,
9987 struct got_histedit_list_entry
*hle
, struct got_object_id
*new_id
)
9989 const struct got_error
*err
;
9990 char *old_id_str
= NULL
, *new_id_str
= NULL
, *logmsg
= NULL
;
9992 err
= got_object_id_str(&old_id_str
, hle
->commit_id
);
9997 err
= got_object_id_str(&new_id_str
, new_id
);
10002 old_id_str
[12] = '\0';
10004 new_id_str
[12] = '\0';
10007 logmsg
= strdup(hle
->logmsg
);
10008 if (logmsg
== NULL
) {
10009 err
= got_error_from_errno("strdup");
10012 trim_logmsg(logmsg
, 42);
10014 err
= get_short_logmsg(&logmsg
, 42, commit
);
10019 switch (hle
->cmd
->code
) {
10020 case GOT_HISTEDIT_PICK
:
10021 case GOT_HISTEDIT_EDIT
:
10022 printf("%s -> %s: %s\n", old_id_str
,
10023 new_id_str
? new_id_str
: "no-op change", logmsg
);
10025 case GOT_HISTEDIT_DROP
:
10026 case GOT_HISTEDIT_FOLD
:
10027 printf("%s -> %s commit: %s\n", old_id_str
, hle
->cmd
->name
,
10039 static const struct got_error
*
10040 histedit_commit(struct got_pathlist_head
*merged_paths
,
10041 struct got_worktree
*worktree
, struct got_fileindex
*fileindex
,
10042 struct got_reference
*tmp_branch
, struct got_histedit_list_entry
*hle
,
10043 struct got_repository
*repo
)
10045 const struct got_error
*err
;
10046 struct got_commit_object
*commit
;
10047 struct got_object_id
*new_commit_id
;
10049 if ((hle
->cmd
->code
== GOT_HISTEDIT_EDIT
|| get_folded_commits(hle
))
10050 && hle
->logmsg
== NULL
) {
10051 err
= histedit_edit_logmsg(hle
, repo
);
10056 err
= got_object_open_as_commit(&commit
, repo
, hle
->commit_id
);
10060 err
= got_worktree_histedit_commit(&new_commit_id
, merged_paths
,
10061 worktree
, fileindex
, tmp_branch
, commit
, hle
->commit_id
,
10062 hle
->logmsg
, repo
);
10064 if (err
->code
!= GOT_ERR_COMMIT_NO_CHANGES
)
10066 err
= show_histedit_progress(commit
, hle
, NULL
);
10068 err
= show_histedit_progress(commit
, hle
, new_commit_id
);
10069 free(new_commit_id
);
10072 got_object_commit_close(commit
);
10076 static const struct got_error
*
10077 histedit_skip_commit(struct got_histedit_list_entry
*hle
,
10078 struct got_worktree
*worktree
, struct got_repository
*repo
)
10080 const struct got_error
*error
;
10081 struct got_commit_object
*commit
;
10083 error
= got_worktree_histedit_skip_commit(worktree
, hle
->commit_id
,
10088 error
= got_object_open_as_commit(&commit
, repo
, hle
->commit_id
);
10092 error
= show_histedit_progress(commit
, hle
, NULL
);
10093 got_object_commit_close(commit
);
10097 static const struct got_error
*
10098 check_local_changes(void *arg
, unsigned char status
,
10099 unsigned char staged_status
, const char *path
,
10100 struct got_object_id
*blob_id
, struct got_object_id
*staged_blob_id
,
10101 struct got_object_id
*commit_id
, int dirfd
, const char *de_name
)
10103 int *have_local_changes
= arg
;
10106 case GOT_STATUS_ADD
:
10107 case GOT_STATUS_DELETE
:
10108 case GOT_STATUS_MODIFY
:
10109 case GOT_STATUS_CONFLICT
:
10110 *have_local_changes
= 1;
10111 return got_error(GOT_ERR_CANCELLED
);
10116 switch (staged_status
) {
10117 case GOT_STATUS_ADD
:
10118 case GOT_STATUS_DELETE
:
10119 case GOT_STATUS_MODIFY
:
10120 *have_local_changes
= 1;
10121 return got_error(GOT_ERR_CANCELLED
);
10129 static const struct got_error
*
10130 cmd_histedit(int argc
, char *argv
[])
10132 const struct got_error
*error
= NULL
;
10133 struct got_worktree
*worktree
= NULL
;
10134 struct got_fileindex
*fileindex
= NULL
;
10135 struct got_repository
*repo
= NULL
;
10137 struct got_reference
*branch
= NULL
;
10138 struct got_reference
*tmp_branch
= NULL
;
10139 struct got_object_id
*resume_commit_id
= NULL
;
10140 struct got_object_id
*base_commit_id
= NULL
;
10141 struct got_object_id
*head_commit_id
= NULL
;
10142 struct got_commit_object
*commit
= NULL
;
10143 int ch
, rebase_in_progress
= 0, merge_in_progress
= 0;
10144 struct got_update_progress_arg upa
;
10145 int edit_in_progress
= 0, abort_edit
= 0, continue_edit
= 0;
10146 int edit_logmsg_only
= 0, fold_only
= 0, edit_only
= 0;
10147 int list_backups
= 0, delete_backups
= 0;
10148 const char *edit_script_path
= NULL
;
10149 struct got_object_id_queue commits
;
10150 struct got_pathlist_head merged_paths
;
10151 const struct got_object_id_queue
*parent_ids
;
10152 struct got_object_qid
*pid
;
10153 struct got_histedit_list histedit_cmds
;
10154 struct got_histedit_list_entry
*hle
;
10156 STAILQ_INIT(&commits
);
10157 TAILQ_INIT(&histedit_cmds
);
10158 TAILQ_INIT(&merged_paths
);
10159 memset(&upa
, 0, sizeof(upa
));
10161 while ((ch
= getopt(argc
, argv
, "acefF:mlX")) != -1) {
10176 edit_script_path
= optarg
;
10179 edit_logmsg_only
= 1;
10185 delete_backups
= 1;
10197 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10198 "unveil", NULL
) == -1)
10201 if (abort_edit
&& continue_edit
)
10202 option_conflict('a', 'c');
10203 if (edit_script_path
&& edit_logmsg_only
)
10204 option_conflict('F', 'm');
10205 if (abort_edit
&& edit_logmsg_only
)
10206 option_conflict('a', 'm');
10207 if (continue_edit
&& edit_logmsg_only
)
10208 option_conflict('c', 'm');
10209 if (abort_edit
&& fold_only
)
10210 option_conflict('a', 'f');
10211 if (continue_edit
&& fold_only
)
10212 option_conflict('c', 'f');
10213 if (fold_only
&& edit_logmsg_only
)
10214 option_conflict('f', 'm');
10215 if (edit_script_path
&& fold_only
)
10216 option_conflict('F', 'f');
10217 if (abort_edit
&& edit_only
)
10218 option_conflict('a', 'e');
10219 if (continue_edit
&& edit_only
)
10220 option_conflict('c', 'e');
10221 if (edit_only
&& edit_logmsg_only
)
10222 option_conflict('e', 'm');
10223 if (edit_script_path
&& edit_only
)
10224 option_conflict('F', 'e');
10225 if (list_backups
) {
10227 option_conflict('l', 'a');
10229 option_conflict('l', 'c');
10230 if (edit_script_path
)
10231 option_conflict('l', 'F');
10232 if (edit_logmsg_only
)
10233 option_conflict('l', 'm');
10235 option_conflict('l', 'f');
10237 option_conflict('l', 'e');
10238 if (delete_backups
)
10239 option_conflict('l', 'X');
10240 if (argc
!= 0 && argc
!= 1)
10242 } else if (delete_backups
) {
10244 option_conflict('X', 'a');
10246 option_conflict('X', 'c');
10247 if (edit_script_path
)
10248 option_conflict('X', 'F');
10249 if (edit_logmsg_only
)
10250 option_conflict('X', 'm');
10252 option_conflict('X', 'f');
10254 option_conflict('X', 'e');
10256 option_conflict('X', 'l');
10257 if (argc
!= 0 && argc
!= 1)
10259 } else if (argc
!= 0)
10263 * This command cannot apply unveil(2) in all cases because the
10264 * user may choose to run an editor to edit the histedit script
10265 * and to edit individual commit log messages.
10266 * unveil(2) traverses exec(2); if an editor is used we have to
10267 * apply unveil after edit script and log messages have been written.
10268 * XXX TODO: Make use of unveil(2) where possible.
10271 cwd
= getcwd(NULL
, 0);
10273 error
= got_error_from_errno("getcwd");
10276 error
= got_worktree_open(&worktree
, cwd
);
10278 if (list_backups
|| delete_backups
) {
10279 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
10282 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
10283 error
= wrap_not_worktree_error(error
,
10289 if (list_backups
|| delete_backups
) {
10290 error
= got_repo_open(&repo
,
10291 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
10295 error
= apply_unveil(got_repo_get_path(repo
), 0,
10296 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
10299 error
= process_backup_refs(
10300 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX
,
10301 argc
== 1 ? argv
[0] : NULL
, delete_backups
, repo
);
10302 goto done
; /* nothing else to do */
10305 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
10310 error
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
10313 if (rebase_in_progress
) {
10314 error
= got_error(GOT_ERR_REBASING
);
10318 error
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
,
10322 if (merge_in_progress
) {
10323 error
= got_error(GOT_ERR_MERGE_BUSY
);
10327 error
= got_worktree_histedit_in_progress(&edit_in_progress
, worktree
);
10331 if (edit_in_progress
&& edit_logmsg_only
) {
10332 error
= got_error_msg(GOT_ERR_HISTEDIT_BUSY
,
10333 "histedit operation is in progress in this "
10334 "work tree and must be continued or aborted "
10335 "before the -m option can be used");
10338 if (edit_in_progress
&& fold_only
) {
10339 error
= got_error_msg(GOT_ERR_HISTEDIT_BUSY
,
10340 "histedit operation is in progress in this "
10341 "work tree and must be continued or aborted "
10342 "before the -f option can be used");
10345 if (edit_in_progress
&& edit_only
) {
10346 error
= got_error_msg(GOT_ERR_HISTEDIT_BUSY
,
10347 "histedit operation is in progress in this "
10348 "work tree and must be continued or aborted "
10349 "before the -e option can be used");
10353 if (edit_in_progress
&& abort_edit
) {
10354 error
= got_worktree_histedit_continue(&resume_commit_id
,
10355 &tmp_branch
, &branch
, &base_commit_id
, &fileindex
,
10359 printf("Switching work tree to %s\n",
10360 got_ref_get_symref_target(branch
));
10361 error
= got_worktree_histedit_abort(worktree
, fileindex
, repo
,
10362 branch
, base_commit_id
, abort_progress
, &upa
);
10365 printf("Histedit of %s aborted\n",
10366 got_ref_get_symref_target(branch
));
10367 print_merge_progress_stats(&upa
);
10368 goto done
; /* nothing else to do */
10369 } else if (abort_edit
) {
10370 error
= got_error(GOT_ERR_NOT_HISTEDIT
);
10374 if (continue_edit
) {
10377 if (!edit_in_progress
) {
10378 error
= got_error(GOT_ERR_NOT_HISTEDIT
);
10382 error
= got_worktree_get_histedit_script_path(&path
, worktree
);
10386 error
= histedit_load_list(&histedit_cmds
, path
, repo
);
10391 error
= got_worktree_histedit_continue(&resume_commit_id
,
10392 &tmp_branch
, &branch
, &base_commit_id
, &fileindex
,
10397 error
= got_ref_resolve(&head_commit_id
, repo
, branch
);
10401 error
= got_object_open_as_commit(&commit
, repo
,
10405 parent_ids
= got_object_commit_get_parent_ids(commit
);
10406 pid
= STAILQ_FIRST(parent_ids
);
10408 error
= got_error(GOT_ERR_EMPTY_HISTEDIT
);
10411 error
= collect_commits(&commits
, head_commit_id
, pid
->id
,
10412 base_commit_id
, got_worktree_get_path_prefix(worktree
),
10413 GOT_ERR_HISTEDIT_PATH
, repo
);
10414 got_object_commit_close(commit
);
10419 if (edit_in_progress
) {
10420 error
= got_error(GOT_ERR_HISTEDIT_BUSY
);
10424 error
= got_ref_open(&branch
, repo
,
10425 got_worktree_get_head_ref_name(worktree
), 0);
10429 if (strncmp(got_ref_get_name(branch
), "refs/heads/", 11) != 0) {
10430 error
= got_error_msg(GOT_ERR_COMMIT_BRANCH
,
10431 "will not edit commit history of a branch outside "
10432 "the \"refs/heads/\" reference namespace");
10436 error
= got_ref_resolve(&head_commit_id
, repo
, branch
);
10437 got_ref_close(branch
);
10442 error
= got_object_open_as_commit(&commit
, repo
,
10446 parent_ids
= got_object_commit_get_parent_ids(commit
);
10447 pid
= STAILQ_FIRST(parent_ids
);
10449 error
= got_error(GOT_ERR_EMPTY_HISTEDIT
);
10452 error
= collect_commits(&commits
, head_commit_id
, pid
->id
,
10453 got_worktree_get_base_commit_id(worktree
),
10454 got_worktree_get_path_prefix(worktree
),
10455 GOT_ERR_HISTEDIT_PATH
, repo
);
10456 got_object_commit_close(commit
);
10461 if (STAILQ_EMPTY(&commits
)) {
10462 error
= got_error(GOT_ERR_EMPTY_HISTEDIT
);
10466 error
= got_worktree_histedit_prepare(&tmp_branch
, &branch
,
10467 &base_commit_id
, &fileindex
, worktree
, repo
);
10471 if (edit_script_path
) {
10472 error
= histedit_load_list(&histedit_cmds
,
10473 edit_script_path
, repo
);
10475 got_worktree_histedit_abort(worktree
, fileindex
,
10476 repo
, branch
, base_commit_id
,
10477 abort_progress
, &upa
);
10478 print_merge_progress_stats(&upa
);
10482 const char *branch_name
;
10483 branch_name
= got_ref_get_symref_target(branch
);
10484 if (strncmp(branch_name
, "refs/heads/", 11) == 0)
10486 error
= histedit_edit_script(&histedit_cmds
, &commits
,
10487 branch_name
, edit_logmsg_only
, fold_only
,
10490 got_worktree_histedit_abort(worktree
, fileindex
,
10491 repo
, branch
, base_commit_id
,
10492 abort_progress
, &upa
);
10493 print_merge_progress_stats(&upa
);
10499 error
= histedit_save_list(&histedit_cmds
, worktree
,
10502 got_worktree_histedit_abort(worktree
, fileindex
,
10503 repo
, branch
, base_commit_id
,
10504 abort_progress
, &upa
);
10505 print_merge_progress_stats(&upa
);
10511 error
= histedit_check_script(&histedit_cmds
, &commits
, repo
);
10515 TAILQ_FOREACH(hle
, &histedit_cmds
, entry
) {
10516 if (resume_commit_id
) {
10517 if (got_object_id_cmp(hle
->commit_id
,
10518 resume_commit_id
) != 0)
10521 resume_commit_id
= NULL
;
10522 if (hle
->cmd
->code
== GOT_HISTEDIT_DROP
||
10523 hle
->cmd
->code
== GOT_HISTEDIT_FOLD
) {
10524 error
= histedit_skip_commit(hle
, worktree
,
10529 struct got_pathlist_head paths
;
10530 int have_changes
= 0;
10532 TAILQ_INIT(&paths
);
10533 error
= got_pathlist_append(&paths
, "", NULL
);
10536 error
= got_worktree_status(worktree
, &paths
,
10537 repo
, 0, check_local_changes
, &have_changes
,
10538 check_cancelled
, NULL
);
10539 got_pathlist_free(&paths
);
10541 if (error
->code
!= GOT_ERR_CANCELLED
)
10543 if (sigint_received
|| sigpipe_received
)
10546 if (have_changes
) {
10547 error
= histedit_commit(NULL
, worktree
,
10548 fileindex
, tmp_branch
, hle
, repo
);
10552 error
= got_object_open_as_commit(
10553 &commit
, repo
, hle
->commit_id
);
10556 error
= show_histedit_progress(commit
,
10558 got_object_commit_close(commit
);
10567 if (hle
->cmd
->code
== GOT_HISTEDIT_DROP
) {
10568 error
= histedit_skip_commit(hle
, worktree
, repo
);
10574 error
= got_object_open_as_commit(&commit
, repo
,
10578 parent_ids
= got_object_commit_get_parent_ids(commit
);
10579 pid
= STAILQ_FIRST(parent_ids
);
10581 error
= got_worktree_histedit_merge_files(&merged_paths
,
10582 worktree
, fileindex
, pid
->id
, hle
->commit_id
, repo
,
10583 update_progress
, &upa
, check_cancelled
, NULL
);
10586 got_object_commit_close(commit
);
10589 print_merge_progress_stats(&upa
);
10590 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
10591 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
10592 if (upa
.conflicts
> 0) {
10593 error
= show_rebase_merge_conflict(
10594 hle
->commit_id
, repo
);
10598 got_worktree_rebase_pathlist_free(&merged_paths
);
10602 if (hle
->cmd
->code
== GOT_HISTEDIT_EDIT
) {
10604 error
= got_object_id_str(&id_str
, hle
->commit_id
);
10607 printf("Stopping histedit for amending commit %s\n",
10610 got_worktree_rebase_pathlist_free(&merged_paths
);
10611 error
= got_worktree_histedit_postpone(worktree
,
10616 if (hle
->cmd
->code
== GOT_HISTEDIT_FOLD
) {
10617 error
= histedit_skip_commit(hle
, worktree
, repo
);
10623 error
= histedit_commit(&merged_paths
, worktree
, fileindex
,
10624 tmp_branch
, hle
, repo
);
10625 got_worktree_rebase_pathlist_free(&merged_paths
);
10630 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
10631 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
10632 error
= got_worktree_histedit_postpone(worktree
, fileindex
);
10635 if (upa
.conflicts
> 0 && upa
.missing
== 0 &&
10636 upa
.not_deleted
== 0 && upa
.unversioned
== 0) {
10637 error
= got_error_msg(GOT_ERR_CONFLICTS
,
10638 "conflicts must be resolved before histedit "
10640 } else if (upa
.conflicts
> 0) {
10641 error
= got_error_msg(GOT_ERR_CONFLICTS
,
10642 "conflicts must be resolved before histedit "
10643 "can continue; changes destined for some "
10644 "files were not yet merged and should be "
10645 "merged manually if required before the "
10646 "histedit operation is continued");
10648 error
= got_error_msg(GOT_ERR_CONFLICTS
,
10649 "changes destined for some files were not "
10650 "yet merged and should be merged manually "
10651 "if required before the histedit operation "
10655 error
= histedit_complete(worktree
, fileindex
, tmp_branch
,
10658 got_object_id_queue_free(&commits
);
10659 histedit_free_list(&histedit_cmds
);
10660 free(head_commit_id
);
10661 free(base_commit_id
);
10662 free(resume_commit_id
);
10664 got_object_commit_close(commit
);
10666 got_ref_close(branch
);
10668 got_ref_close(tmp_branch
);
10670 got_worktree_close(worktree
);
10672 const struct got_error
*close_err
= got_repo_close(repo
);
10680 usage_integrate(void)
10682 fprintf(stderr
, "usage: %s integrate branch\n", getprogname());
10686 static const struct got_error
*
10687 cmd_integrate(int argc
, char *argv
[])
10689 const struct got_error
*error
= NULL
;
10690 struct got_repository
*repo
= NULL
;
10691 struct got_worktree
*worktree
= NULL
;
10692 char *cwd
= NULL
, *refname
= NULL
, *base_refname
= NULL
;
10693 const char *branch_arg
= NULL
;
10694 struct got_reference
*branch_ref
= NULL
, *base_branch_ref
= NULL
;
10695 struct got_fileindex
*fileindex
= NULL
;
10696 struct got_object_id
*commit_id
= NULL
, *base_commit_id
= NULL
;
10698 struct got_update_progress_arg upa
;
10700 while ((ch
= getopt(argc
, argv
, "")) != -1) {
10713 branch_arg
= argv
[0];
10715 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10716 "unveil", NULL
) == -1)
10719 cwd
= getcwd(NULL
, 0);
10721 error
= got_error_from_errno("getcwd");
10725 error
= got_worktree_open(&worktree
, cwd
);
10727 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
10728 error
= wrap_not_worktree_error(error
, "integrate",
10733 error
= check_rebase_or_histedit_in_progress(worktree
);
10737 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
10742 error
= apply_unveil(got_repo_get_path(repo
), 0,
10743 got_worktree_get_root_path(worktree
));
10747 error
= check_merge_in_progress(worktree
, repo
);
10751 if (asprintf(&refname
, "refs/heads/%s", branch_arg
) == -1) {
10752 error
= got_error_from_errno("asprintf");
10756 error
= got_worktree_integrate_prepare(&fileindex
, &branch_ref
,
10757 &base_branch_ref
, worktree
, refname
, repo
);
10761 refname
= strdup(got_ref_get_name(branch_ref
));
10762 if (refname
== NULL
) {
10763 error
= got_error_from_errno("strdup");
10764 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
10765 branch_ref
, base_branch_ref
);
10768 base_refname
= strdup(got_ref_get_name(base_branch_ref
));
10769 if (base_refname
== NULL
) {
10770 error
= got_error_from_errno("strdup");
10771 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
10772 branch_ref
, base_branch_ref
);
10776 error
= got_ref_resolve(&commit_id
, repo
, branch_ref
);
10780 error
= got_ref_resolve(&base_commit_id
, repo
, base_branch_ref
);
10784 if (got_object_id_cmp(commit_id
, base_commit_id
) == 0) {
10785 error
= got_error_msg(GOT_ERR_SAME_BRANCH
,
10786 "specified branch has already been integrated");
10787 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
10788 branch_ref
, base_branch_ref
);
10792 error
= check_linear_ancestry(commit_id
, base_commit_id
, 1, repo
);
10794 if (error
->code
== GOT_ERR_ANCESTRY
)
10795 error
= got_error(GOT_ERR_REBASE_REQUIRED
);
10796 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
10797 branch_ref
, base_branch_ref
);
10801 memset(&upa
, 0, sizeof(upa
));
10802 error
= got_worktree_integrate_continue(worktree
, fileindex
, repo
,
10803 branch_ref
, base_branch_ref
, update_progress
, &upa
,
10804 check_cancelled
, NULL
);
10808 printf("Integrated %s into %s\n", refname
, base_refname
);
10809 print_update_progress_stats(&upa
);
10812 const struct got_error
*close_err
= got_repo_close(repo
);
10817 got_worktree_close(worktree
);
10819 free(base_commit_id
);
10822 free(base_refname
);
10829 fprintf(stderr
, "usage: %s merge [-a] [-c] [-n] [branch]\n",
10834 static const struct got_error
*
10835 cmd_merge(int argc
, char *argv
[])
10837 const struct got_error
*error
= NULL
;
10838 struct got_worktree
*worktree
= NULL
;
10839 struct got_repository
*repo
= NULL
;
10840 struct got_fileindex
*fileindex
= NULL
;
10841 char *cwd
= NULL
, *id_str
= NULL
, *author
= NULL
;
10842 struct got_reference
*branch
= NULL
, *wt_branch
= NULL
;
10843 struct got_object_id
*branch_tip
= NULL
, *yca_id
= NULL
;
10844 struct got_object_id
*wt_branch_tip
= NULL
;
10845 int ch
, merge_in_progress
= 0, abort_merge
= 0, continue_merge
= 0;
10846 int interrupt_merge
= 0;
10847 struct got_update_progress_arg upa
;
10848 struct got_object_id
*merge_commit_id
= NULL
;
10849 char *branch_name
= NULL
;
10851 memset(&upa
, 0, sizeof(upa
));
10853 while ((ch
= getopt(argc
, argv
, "acn")) != -1) {
10859 continue_merge
= 1;
10862 interrupt_merge
= 1;
10874 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10875 "unveil", NULL
) == -1)
10879 if (abort_merge
&& continue_merge
)
10880 option_conflict('a', 'c');
10881 if (abort_merge
|| continue_merge
) {
10884 } else if (argc
!= 1)
10887 cwd
= getcwd(NULL
, 0);
10889 error
= got_error_from_errno("getcwd");
10893 error
= got_worktree_open(&worktree
, cwd
);
10895 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
10896 error
= wrap_not_worktree_error(error
,
10901 error
= got_repo_open(&repo
,
10902 worktree
? got_worktree_get_repo_path(worktree
) : cwd
, NULL
);
10906 error
= apply_unveil(got_repo_get_path(repo
), 0,
10907 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
10911 error
= check_rebase_or_histedit_in_progress(worktree
);
10915 error
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
,
10921 if (!merge_in_progress
) {
10922 error
= got_error(GOT_ERR_NOT_MERGING
);
10925 error
= got_worktree_merge_continue(&branch_name
,
10926 &branch_tip
, &fileindex
, worktree
, repo
);
10929 error
= got_worktree_merge_abort(worktree
, fileindex
, repo
,
10930 abort_progress
, &upa
);
10933 printf("Merge of %s aborted\n", branch_name
);
10934 goto done
; /* nothing else to do */
10937 error
= get_author(&author
, repo
, worktree
);
10941 if (continue_merge
) {
10942 if (!merge_in_progress
) {
10943 error
= got_error(GOT_ERR_NOT_MERGING
);
10946 error
= got_worktree_merge_continue(&branch_name
,
10947 &branch_tip
, &fileindex
, worktree
, repo
);
10951 error
= got_ref_open(&branch
, repo
, argv
[0], 0);
10954 branch_name
= strdup(got_ref_get_name(branch
));
10955 if (branch_name
== NULL
) {
10956 error
= got_error_from_errno("strdup");
10959 error
= got_ref_resolve(&branch_tip
, repo
, branch
);
10964 error
= got_ref_open(&wt_branch
, repo
,
10965 got_worktree_get_head_ref_name(worktree
), 0);
10968 error
= got_ref_resolve(&wt_branch_tip
, repo
, wt_branch
);
10971 error
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
10972 wt_branch_tip
, branch_tip
, 0, repo
,
10973 check_cancelled
, NULL
);
10974 if (error
&& error
->code
!= GOT_ERR_ANCESTRY
)
10977 if (!continue_merge
) {
10978 error
= check_path_prefix(wt_branch_tip
, branch_tip
,
10979 got_worktree_get_path_prefix(worktree
),
10980 GOT_ERR_MERGE_PATH
, repo
);
10984 error
= check_same_branch(wt_branch_tip
, branch
,
10987 if (error
->code
!= GOT_ERR_ANCESTRY
)
10991 static char msg
[512];
10992 snprintf(msg
, sizeof(msg
),
10993 "cannot create a merge commit because "
10994 "%s is based on %s; %s can be integrated "
10995 "with 'got integrate' instead", branch_name
,
10996 got_worktree_get_head_ref_name(worktree
),
10998 error
= got_error_msg(GOT_ERR_SAME_BRANCH
, msg
);
11002 error
= got_worktree_merge_prepare(&fileindex
, worktree
,
11007 error
= got_worktree_merge_branch(worktree
, fileindex
,
11008 yca_id
, branch_tip
, repo
, update_progress
, &upa
,
11009 check_cancelled
, NULL
);
11012 print_merge_progress_stats(&upa
);
11013 if (!upa
.did_something
) {
11014 error
= got_worktree_merge_abort(worktree
, fileindex
,
11015 repo
, abort_progress
, &upa
);
11018 printf("Already up-to-date\n");
11023 if (interrupt_merge
) {
11024 error
= got_worktree_merge_postpone(worktree
, fileindex
);
11027 printf("Merge of %s interrupted on request\n", branch_name
);
11028 } else if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
11029 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
11030 error
= got_worktree_merge_postpone(worktree
, fileindex
);
11033 if (upa
.conflicts
> 0 && upa
.missing
== 0 &&
11034 upa
.not_deleted
== 0 && upa
.unversioned
== 0) {
11035 error
= got_error_msg(GOT_ERR_CONFLICTS
,
11036 "conflicts must be resolved before merging "
11038 } else if (upa
.conflicts
> 0) {
11039 error
= got_error_msg(GOT_ERR_CONFLICTS
,
11040 "conflicts must be resolved before merging "
11041 "can continue; changes destined for some "
11042 "files were not yet merged and "
11043 "should be merged manually if required before the "
11044 "merge operation is continued");
11046 error
= got_error_msg(GOT_ERR_CONFLICTS
,
11047 "changes destined for some "
11048 "files were not yet merged and should be "
11049 "merged manually if required before the "
11050 "merge operation is continued");
11054 error
= got_worktree_merge_commit(&merge_commit_id
, worktree
,
11055 fileindex
, author
, NULL
, 1, branch_tip
, branch_name
,
11056 repo
, continue_merge
? print_status
: NULL
, NULL
);
11059 error
= got_worktree_merge_complete(worktree
, fileindex
, repo
);
11062 error
= got_object_id_str(&id_str
, merge_commit_id
);
11065 printf("Merged %s into %s: %s\n", branch_name
,
11066 got_worktree_get_head_ref_name(worktree
),
11072 free(merge_commit_id
);
11078 got_ref_close(branch
);
11080 got_ref_close(wt_branch
);
11082 got_worktree_close(worktree
);
11084 const struct got_error
*close_err
= got_repo_close(repo
);
11094 fprintf(stderr
, "usage: %s stage [-l] | [-p] [-F response-script] "
11095 "[-S] [file-path ...]\n",
11100 static const struct got_error
*
11101 print_stage(void *arg
, unsigned char status
, unsigned char staged_status
,
11102 const char *path
, struct got_object_id
*blob_id
,
11103 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
11104 int dirfd
, const char *de_name
)
11106 const struct got_error
*err
= NULL
;
11107 char *id_str
= NULL
;
11109 if (staged_status
!= GOT_STATUS_ADD
&&
11110 staged_status
!= GOT_STATUS_MODIFY
&&
11111 staged_status
!= GOT_STATUS_DELETE
)
11114 if (staged_status
== GOT_STATUS_ADD
||
11115 staged_status
== GOT_STATUS_MODIFY
)
11116 err
= got_object_id_str(&id_str
, staged_blob_id
);
11118 err
= got_object_id_str(&id_str
, blob_id
);
11122 printf("%s %c %s\n", id_str
, staged_status
, path
);
11127 static const struct got_error
*
11128 cmd_stage(int argc
, char *argv
[])
11130 const struct got_error
*error
= NULL
;
11131 struct got_repository
*repo
= NULL
;
11132 struct got_worktree
*worktree
= NULL
;
11134 struct got_pathlist_head paths
;
11135 struct got_pathlist_entry
*pe
;
11136 int ch
, list_stage
= 0, pflag
= 0, allow_bad_symlinks
= 0;
11137 FILE *patch_script_file
= NULL
;
11138 const char *patch_script_path
= NULL
;
11139 struct choose_patch_arg cpa
;
11141 TAILQ_INIT(&paths
);
11143 while ((ch
= getopt(argc
, argv
, "lpF:S")) != -1) {
11152 patch_script_path
= optarg
;
11155 allow_bad_symlinks
= 1;
11167 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11168 "unveil", NULL
) == -1)
11171 if (list_stage
&& (pflag
|| patch_script_path
))
11172 errx(1, "-l option cannot be used with other options");
11173 if (patch_script_path
&& !pflag
)
11174 errx(1, "-F option can only be used together with -p option");
11176 cwd
= getcwd(NULL
, 0);
11178 error
= got_error_from_errno("getcwd");
11182 error
= got_worktree_open(&worktree
, cwd
);
11184 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
11185 error
= wrap_not_worktree_error(error
, "stage", cwd
);
11189 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
11194 if (patch_script_path
) {
11195 patch_script_file
= fopen(patch_script_path
, "r");
11196 if (patch_script_file
== NULL
) {
11197 error
= got_error_from_errno2("fopen",
11198 patch_script_path
);
11202 error
= apply_unveil(got_repo_get_path(repo
), 0,
11203 got_worktree_get_root_path(worktree
));
11207 error
= check_merge_in_progress(worktree
, repo
);
11211 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
11216 error
= got_worktree_status(worktree
, &paths
, repo
, 0,
11217 print_stage
, NULL
, check_cancelled
, NULL
);
11219 cpa
.patch_script_file
= patch_script_file
;
11220 cpa
.action
= "stage";
11221 error
= got_worktree_stage(worktree
, &paths
,
11222 pflag
? NULL
: print_status
, NULL
,
11223 pflag
? choose_patch
: NULL
, &cpa
,
11224 allow_bad_symlinks
, repo
);
11227 if (patch_script_file
&& fclose(patch_script_file
) == EOF
&&
11229 error
= got_error_from_errno2("fclose", patch_script_path
);
11231 const struct got_error
*close_err
= got_repo_close(repo
);
11236 got_worktree_close(worktree
);
11237 TAILQ_FOREACH(pe
, &paths
, entry
)
11238 free((char *)pe
->path
);
11239 got_pathlist_free(&paths
);
11245 usage_unstage(void)
11247 fprintf(stderr
, "usage: %s unstage [-p] [-F response-script] "
11248 "[file-path ...]\n",
11254 static const struct got_error
*
11255 cmd_unstage(int argc
, char *argv
[])
11257 const struct got_error
*error
= NULL
;
11258 struct got_repository
*repo
= NULL
;
11259 struct got_worktree
*worktree
= NULL
;
11261 struct got_pathlist_head paths
;
11262 struct got_pathlist_entry
*pe
;
11264 struct got_update_progress_arg upa
;
11265 FILE *patch_script_file
= NULL
;
11266 const char *patch_script_path
= NULL
;
11267 struct choose_patch_arg cpa
;
11269 TAILQ_INIT(&paths
);
11271 while ((ch
= getopt(argc
, argv
, "pF:")) != -1) {
11277 patch_script_path
= optarg
;
11289 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11290 "unveil", NULL
) == -1)
11293 if (patch_script_path
&& !pflag
)
11294 errx(1, "-F option can only be used together with -p option");
11296 cwd
= getcwd(NULL
, 0);
11298 error
= got_error_from_errno("getcwd");
11302 error
= got_worktree_open(&worktree
, cwd
);
11304 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
11305 error
= wrap_not_worktree_error(error
, "unstage", cwd
);
11309 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
11314 if (patch_script_path
) {
11315 patch_script_file
= fopen(patch_script_path
, "r");
11316 if (patch_script_file
== NULL
) {
11317 error
= got_error_from_errno2("fopen",
11318 patch_script_path
);
11323 error
= apply_unveil(got_repo_get_path(repo
), 0,
11324 got_worktree_get_root_path(worktree
));
11328 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
11332 cpa
.patch_script_file
= patch_script_file
;
11333 cpa
.action
= "unstage";
11334 memset(&upa
, 0, sizeof(upa
));
11335 error
= got_worktree_unstage(worktree
, &paths
, update_progress
,
11336 &upa
, pflag
? choose_patch
: NULL
, &cpa
, repo
);
11338 print_merge_progress_stats(&upa
);
11340 if (patch_script_file
&& fclose(patch_script_file
) == EOF
&&
11342 error
= got_error_from_errno2("fclose", patch_script_path
);
11344 const struct got_error
*close_err
= got_repo_close(repo
);
11349 got_worktree_close(worktree
);
11350 TAILQ_FOREACH(pe
, &paths
, entry
)
11351 free((char *)pe
->path
);
11352 got_pathlist_free(&paths
);
11360 fprintf(stderr
, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11361 "arg1 [arg2 ...]\n", getprogname());
11365 static const struct got_error
*
11366 cat_blob(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
11368 const struct got_error
*err
;
11369 struct got_blob_object
*blob
;
11371 err
= got_object_open_as_blob(&blob
, repo
, id
, 8192);
11375 err
= got_object_blob_dump_to_file(NULL
, NULL
, NULL
, outfile
, blob
);
11376 got_object_blob_close(blob
);
11380 static const struct got_error
*
11381 cat_tree(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
11383 const struct got_error
*err
;
11384 struct got_tree_object
*tree
;
11387 err
= got_object_open_as_tree(&tree
, repo
, id
);
11391 nentries
= got_object_tree_get_nentries(tree
);
11392 for (i
= 0; i
< nentries
; i
++) {
11393 struct got_tree_entry
*te
;
11395 if (sigint_received
|| sigpipe_received
)
11397 te
= got_object_tree_get_entry(tree
, i
);
11398 err
= got_object_id_str(&id_str
, got_tree_entry_get_id(te
));
11401 fprintf(outfile
, "%s %.7o %s\n", id_str
,
11402 got_tree_entry_get_mode(te
),
11403 got_tree_entry_get_name(te
));
11407 got_object_tree_close(tree
);
11411 static const struct got_error
*
11412 cat_commit(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
11414 const struct got_error
*err
;
11415 struct got_commit_object
*commit
;
11416 const struct got_object_id_queue
*parent_ids
;
11417 struct got_object_qid
*pid
;
11418 char *id_str
= NULL
;
11419 const char *logmsg
= NULL
;
11421 err
= got_object_open_as_commit(&commit
, repo
, id
);
11425 err
= got_object_id_str(&id_str
, got_object_commit_get_tree_id(commit
));
11429 fprintf(outfile
, "%s%s\n", GOT_COMMIT_LABEL_TREE
, id_str
);
11430 parent_ids
= got_object_commit_get_parent_ids(commit
);
11431 fprintf(outfile
, "numparents %d\n",
11432 got_object_commit_get_nparents(commit
));
11433 STAILQ_FOREACH(pid
, parent_ids
, entry
) {
11435 err
= got_object_id_str(&pid_str
, pid
->id
);
11438 fprintf(outfile
, "%s%s\n", GOT_COMMIT_LABEL_PARENT
, pid_str
);
11441 fprintf(outfile
, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR
,
11442 got_object_commit_get_author(commit
),
11443 (long long)got_object_commit_get_author_time(commit
));
11445 fprintf(outfile
, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER
,
11446 got_object_commit_get_author(commit
),
11447 (long long)got_object_commit_get_committer_time(commit
));
11449 logmsg
= got_object_commit_get_logmsg_raw(commit
);
11450 fprintf(outfile
, "messagelen %zd\n", strlen(logmsg
));
11451 fprintf(outfile
, "%s", logmsg
);
11454 got_object_commit_close(commit
);
11458 static const struct got_error
*
11459 cat_tag(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
11461 const struct got_error
*err
;
11462 struct got_tag_object
*tag
;
11463 char *id_str
= NULL
;
11464 const char *tagmsg
= NULL
;
11466 err
= got_object_open_as_tag(&tag
, repo
, id
);
11470 err
= got_object_id_str(&id_str
, got_object_tag_get_object_id(tag
));
11474 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_OBJECT
, id_str
);
11476 switch (got_object_tag_get_object_type(tag
)) {
11477 case GOT_OBJ_TYPE_BLOB
:
11478 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
11479 GOT_OBJ_LABEL_BLOB
);
11481 case GOT_OBJ_TYPE_TREE
:
11482 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
11483 GOT_OBJ_LABEL_TREE
);
11485 case GOT_OBJ_TYPE_COMMIT
:
11486 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
11487 GOT_OBJ_LABEL_COMMIT
);
11489 case GOT_OBJ_TYPE_TAG
:
11490 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
11491 GOT_OBJ_LABEL_TAG
);
11497 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TAG
,
11498 got_object_tag_get_name(tag
));
11500 fprintf(outfile
, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER
,
11501 got_object_tag_get_tagger(tag
),
11502 (long long)got_object_tag_get_tagger_time(tag
));
11504 tagmsg
= got_object_tag_get_message(tag
);
11505 fprintf(outfile
, "messagelen %zd\n", strlen(tagmsg
));
11506 fprintf(outfile
, "%s", tagmsg
);
11509 got_object_tag_close(tag
);
11513 static const struct got_error
*
11514 cmd_cat(int argc
, char *argv
[])
11516 const struct got_error
*error
;
11517 struct got_repository
*repo
= NULL
;
11518 struct got_worktree
*worktree
= NULL
;
11519 char *cwd
= NULL
, *repo_path
= NULL
, *label
= NULL
;
11520 const char *commit_id_str
= NULL
;
11521 struct got_object_id
*id
= NULL
, *commit_id
= NULL
;
11522 int ch
, obj_type
, i
, force_path
= 0;
11523 struct got_reflist_head refs
;
11528 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11533 while ((ch
= getopt(argc
, argv
, "c:r:P")) != -1) {
11536 commit_id_str
= optarg
;
11539 repo_path
= realpath(optarg
, NULL
);
11540 if (repo_path
== NULL
)
11541 return got_error_from_errno2("realpath",
11543 got_path_strip_trailing_slashes(repo_path
);
11557 cwd
= getcwd(NULL
, 0);
11559 error
= got_error_from_errno("getcwd");
11562 error
= got_worktree_open(&worktree
, cwd
);
11563 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
11566 if (repo_path
== NULL
) {
11567 repo_path
= strdup(
11568 got_worktree_get_repo_path(worktree
));
11569 if (repo_path
== NULL
) {
11570 error
= got_error_from_errno("strdup");
11576 if (repo_path
== NULL
) {
11577 repo_path
= getcwd(NULL
, 0);
11578 if (repo_path
== NULL
)
11579 return got_error_from_errno("getcwd");
11582 error
= got_repo_open(&repo
, repo_path
, NULL
);
11587 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
11591 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
11595 if (commit_id_str
== NULL
)
11596 commit_id_str
= GOT_REF_HEAD
;
11597 error
= got_repo_match_object_id(&commit_id
, NULL
,
11598 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
11602 for (i
= 0; i
< argc
; i
++) {
11604 error
= got_object_id_by_path(&id
, repo
, commit_id
,
11609 error
= got_repo_match_object_id(&id
, &label
, argv
[i
],
11610 GOT_OBJ_TYPE_ANY
, NULL
/* do not resolve tags */,
11613 if (error
->code
!= GOT_ERR_BAD_OBJ_ID_STR
&&
11614 error
->code
!= GOT_ERR_NOT_REF
)
11616 error
= got_object_id_by_path(&id
, repo
,
11617 commit_id
, argv
[i
]);
11623 error
= got_object_get_type(&obj_type
, repo
, id
);
11627 switch (obj_type
) {
11628 case GOT_OBJ_TYPE_BLOB
:
11629 error
= cat_blob(id
, repo
, stdout
);
11631 case GOT_OBJ_TYPE_TREE
:
11632 error
= cat_tree(id
, repo
, stdout
);
11634 case GOT_OBJ_TYPE_COMMIT
:
11635 error
= cat_commit(id
, repo
, stdout
);
11637 case GOT_OBJ_TYPE_TAG
:
11638 error
= cat_tag(id
, repo
, stdout
);
11641 error
= got_error(GOT_ERR_OBJ_TYPE
);
11656 got_worktree_close(worktree
);
11658 const struct got_error
*close_err
= got_repo_close(repo
);
11662 got_ref_list_free(&refs
);
11669 fprintf(stderr
, "usage: %s info [path ...]\n",
11674 static const struct got_error
*
11675 print_path_info(void *arg
, const char *path
, mode_t mode
, time_t mtime
,
11676 struct got_object_id
*blob_id
, struct got_object_id
*staged_blob_id
,
11677 struct got_object_id
*commit_id
)
11679 const struct got_error
*err
= NULL
;
11680 char *id_str
= NULL
;
11682 struct tm mytm
, *tm
;
11683 struct got_pathlist_head
*paths
= arg
;
11684 struct got_pathlist_entry
*pe
;
11687 * Clear error indication from any of the path arguments which
11688 * would cause this file index entry to be displayed.
11690 TAILQ_FOREACH(pe
, paths
, entry
) {
11691 if (got_path_cmp(path
, pe
->path
, strlen(path
),
11692 pe
->path_len
) == 0 ||
11693 got_path_is_child(path
, pe
->path
, pe
->path_len
))
11694 pe
->data
= NULL
; /* no error */
11697 printf(GOT_COMMIT_SEP_STR
);
11699 printf("symlink: %s\n", path
);
11700 else if (S_ISREG(mode
)) {
11701 printf("file: %s\n", path
);
11702 printf("mode: %o\n", mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
));
11703 } else if (S_ISDIR(mode
))
11704 printf("directory: %s\n", path
);
11706 printf("something: %s\n", path
);
11708 tm
= localtime_r(&mtime
, &mytm
);
11711 if (strftime(datebuf
, sizeof(datebuf
), "%c %Z", tm
) == 0)
11712 return got_error(GOT_ERR_NO_SPACE
);
11713 printf("timestamp: %s\n", datebuf
);
11716 err
= got_object_id_str(&id_str
, blob_id
);
11719 printf("based on blob: %s\n", id_str
);
11723 if (staged_blob_id
) {
11724 err
= got_object_id_str(&id_str
, staged_blob_id
);
11727 printf("based on staged blob: %s\n", id_str
);
11732 err
= got_object_id_str(&id_str
, commit_id
);
11735 printf("based on commit: %s\n", id_str
);
11742 static const struct got_error
*
11743 cmd_info(int argc
, char *argv
[])
11745 const struct got_error
*error
= NULL
;
11746 struct got_worktree
*worktree
= NULL
;
11747 char *cwd
= NULL
, *id_str
= NULL
;
11748 struct got_pathlist_head paths
;
11749 struct got_pathlist_entry
*pe
;
11750 char *uuidstr
= NULL
;
11751 int ch
, show_files
= 0;
11753 TAILQ_INIT(&paths
);
11755 while ((ch
= getopt(argc
, argv
, "")) != -1) {
11767 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11771 cwd
= getcwd(NULL
, 0);
11773 error
= got_error_from_errno("getcwd");
11777 error
= got_worktree_open(&worktree
, cwd
);
11779 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
11780 error
= wrap_not_worktree_error(error
, "info", cwd
);
11784 error
= apply_unveil(NULL
, 0, got_worktree_get_root_path(worktree
));
11789 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
,
11796 error
= got_object_id_str(&id_str
,
11797 got_worktree_get_base_commit_id(worktree
));
11801 error
= got_worktree_get_uuid(&uuidstr
, worktree
);
11805 printf("work tree: %s\n", got_worktree_get_root_path(worktree
));
11806 printf("work tree base commit: %s\n", id_str
);
11807 printf("work tree path prefix: %s\n",
11808 got_worktree_get_path_prefix(worktree
));
11809 printf("work tree branch reference: %s\n",
11810 got_worktree_get_head_ref_name(worktree
));
11811 printf("work tree UUID: %s\n", uuidstr
);
11812 printf("repository: %s\n", got_worktree_get_repo_path(worktree
));
11815 struct got_pathlist_entry
*pe
;
11816 TAILQ_FOREACH(pe
, &paths
, entry
) {
11817 if (pe
->path_len
== 0)
11820 * Assume this path will fail. This will be corrected
11821 * in print_path_info() in case the path does suceeed.
11823 pe
->data
= (void *)got_error_path(pe
->path
,
11826 error
= got_worktree_path_info(worktree
, &paths
,
11827 print_path_info
, &paths
, check_cancelled
, NULL
);
11830 TAILQ_FOREACH(pe
, &paths
, entry
) {
11831 if (pe
->data
!= NULL
) {
11832 error
= pe
->data
; /* bad path */
11838 TAILQ_FOREACH(pe
, &paths
, entry
)
11839 free((char *)pe
->path
);
11840 got_pathlist_free(&paths
);