Blame


1 417a6e49 2020-09-26 stsp /*
2 417a6e49 2020-09-26 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 417a6e49 2020-09-26 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 417a6e49 2020-09-26 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 417a6e49 2020-09-26 stsp *
6 417a6e49 2020-09-26 stsp * Permission to use, copy, modify, and distribute this software for any
7 417a6e49 2020-09-26 stsp * purpose with or without fee is hereby granted, provided that the above
8 417a6e49 2020-09-26 stsp * copyright notice and this permission notice appear in all copies.
9 417a6e49 2020-09-26 stsp *
10 417a6e49 2020-09-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 417a6e49 2020-09-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 417a6e49 2020-09-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 417a6e49 2020-09-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 417a6e49 2020-09-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 417a6e49 2020-09-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 417a6e49 2020-09-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 417a6e49 2020-09-26 stsp */
18 417a6e49 2020-09-26 stsp
19 417a6e49 2020-09-26 stsp #include <sys/queue.h>
20 417a6e49 2020-09-26 stsp #include <sys/types.h>
21 417a6e49 2020-09-26 stsp #include <sys/stat.h>
22 417a6e49 2020-09-26 stsp #include <sys/param.h>
23 417a6e49 2020-09-26 stsp #include <sys/wait.h>
24 417a6e49 2020-09-26 stsp
25 417a6e49 2020-09-26 stsp #include <err.h>
26 417a6e49 2020-09-26 stsp #include <errno.h>
27 417a6e49 2020-09-26 stsp #include <fcntl.h>
28 417a6e49 2020-09-26 stsp #include <limits.h>
29 417a6e49 2020-09-26 stsp #include <locale.h>
30 417a6e49 2020-09-26 stsp #include <ctype.h>
31 417a6e49 2020-09-26 stsp #include <signal.h>
32 417a6e49 2020-09-26 stsp #include <stdio.h>
33 417a6e49 2020-09-26 stsp #include <stdlib.h>
34 417a6e49 2020-09-26 stsp #include <string.h>
35 417a6e49 2020-09-26 stsp #include <unistd.h>
36 417a6e49 2020-09-26 stsp #include <libgen.h>
37 417a6e49 2020-09-26 stsp #include <time.h>
38 417a6e49 2020-09-26 stsp #include <paths.h>
39 417a6e49 2020-09-26 stsp #include <regex.h>
40 417a6e49 2020-09-26 stsp #include <getopt.h>
41 417a6e49 2020-09-26 stsp #include <util.h>
42 417a6e49 2020-09-26 stsp
43 417a6e49 2020-09-26 stsp #include "got_version.h"
44 417a6e49 2020-09-26 stsp #include "got_error.h"
45 417a6e49 2020-09-26 stsp #include "got_object.h"
46 417a6e49 2020-09-26 stsp #include "got_reference.h"
47 417a6e49 2020-09-26 stsp #include "got_repository.h"
48 417a6e49 2020-09-26 stsp #include "got_path.h"
49 417a6e49 2020-09-26 stsp #include "got_cancel.h"
50 417a6e49 2020-09-26 stsp #include "got_worktree.h"
51 417a6e49 2020-09-26 stsp #include "got_diff.h"
52 417a6e49 2020-09-26 stsp #include "got_commit_graph.h"
53 417a6e49 2020-09-26 stsp #include "got_fetch.h"
54 417a6e49 2020-09-26 stsp #include "got_blame.h"
55 417a6e49 2020-09-26 stsp #include "got_privsep.h"
56 417a6e49 2020-09-26 stsp #include "got_opentemp.h"
57 417a6e49 2020-09-26 stsp #include "got_gotconfig.h"
58 417a6e49 2020-09-26 stsp
59 417a6e49 2020-09-26 stsp #ifndef nitems
60 417a6e49 2020-09-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 417a6e49 2020-09-26 stsp #endif
62 417a6e49 2020-09-26 stsp
63 417a6e49 2020-09-26 stsp static volatile sig_atomic_t sigint_received;
64 417a6e49 2020-09-26 stsp static volatile sig_atomic_t sigpipe_received;
65 417a6e49 2020-09-26 stsp
66 417a6e49 2020-09-26 stsp static void
67 417a6e49 2020-09-26 stsp catch_sigint(int signo)
68 417a6e49 2020-09-26 stsp {
69 417a6e49 2020-09-26 stsp sigint_received = 1;
70 417a6e49 2020-09-26 stsp }
71 417a6e49 2020-09-26 stsp
72 417a6e49 2020-09-26 stsp static void
73 417a6e49 2020-09-26 stsp catch_sigpipe(int signo)
74 417a6e49 2020-09-26 stsp {
75 417a6e49 2020-09-26 stsp sigpipe_received = 1;
76 417a6e49 2020-09-26 stsp }
77 417a6e49 2020-09-26 stsp
78 417a6e49 2020-09-26 stsp
79 417a6e49 2020-09-26 stsp struct got_cmd {
80 417a6e49 2020-09-26 stsp const char *cmd_name;
81 417a6e49 2020-09-26 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 417a6e49 2020-09-26 stsp void (*cmd_usage)(void);
83 417a6e49 2020-09-26 stsp const char *cmd_alias;
84 417a6e49 2020-09-26 stsp };
85 417a6e49 2020-09-26 stsp
86 417a6e49 2020-09-26 stsp __dead static void usage(int);
87 417a6e49 2020-09-26 stsp __dead static void usage_init(void);
88 417a6e49 2020-09-26 stsp __dead static void usage_import(void);
89 417a6e49 2020-09-26 stsp __dead static void usage_clone(void);
90 417a6e49 2020-09-26 stsp __dead static void usage_fetch(void);
91 417a6e49 2020-09-26 stsp __dead static void usage_checkout(void);
92 417a6e49 2020-09-26 stsp __dead static void usage_update(void);
93 417a6e49 2020-09-26 stsp __dead static void usage_log(void);
94 417a6e49 2020-09-26 stsp __dead static void usage_diff(void);
95 417a6e49 2020-09-26 stsp __dead static void usage_blame(void);
96 417a6e49 2020-09-26 stsp __dead static void usage_tree(void);
97 417a6e49 2020-09-26 stsp __dead static void usage_status(void);
98 417a6e49 2020-09-26 stsp __dead static void usage_ref(void);
99 417a6e49 2020-09-26 stsp __dead static void usage_branch(void);
100 417a6e49 2020-09-26 stsp __dead static void usage_tag(void);
101 417a6e49 2020-09-26 stsp __dead static void usage_add(void);
102 417a6e49 2020-09-26 stsp __dead static void usage_remove(void);
103 417a6e49 2020-09-26 stsp __dead static void usage_revert(void);
104 417a6e49 2020-09-26 stsp __dead static void usage_commit(void);
105 417a6e49 2020-09-26 stsp __dead static void usage_cherrypick(void);
106 417a6e49 2020-09-26 stsp __dead static void usage_backout(void);
107 417a6e49 2020-09-26 stsp __dead static void usage_rebase(void);
108 417a6e49 2020-09-26 stsp __dead static void usage_histedit(void);
109 417a6e49 2020-09-26 stsp __dead static void usage_integrate(void);
110 417a6e49 2020-09-26 stsp __dead static void usage_stage(void);
111 417a6e49 2020-09-26 stsp __dead static void usage_unstage(void);
112 417a6e49 2020-09-26 stsp __dead static void usage_cat(void);
113 417a6e49 2020-09-26 stsp __dead static void usage_info(void);
114 417a6e49 2020-09-26 stsp
115 417a6e49 2020-09-26 stsp static const struct got_error* cmd_init(int, char *[]);
116 417a6e49 2020-09-26 stsp static const struct got_error* cmd_import(int, char *[]);
117 417a6e49 2020-09-26 stsp static const struct got_error* cmd_clone(int, char *[]);
118 417a6e49 2020-09-26 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 417a6e49 2020-09-26 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 417a6e49 2020-09-26 stsp static const struct got_error* cmd_update(int, char *[]);
121 417a6e49 2020-09-26 stsp static const struct got_error* cmd_log(int, char *[]);
122 417a6e49 2020-09-26 stsp static const struct got_error* cmd_diff(int, char *[]);
123 417a6e49 2020-09-26 stsp static const struct got_error* cmd_blame(int, char *[]);
124 417a6e49 2020-09-26 stsp static const struct got_error* cmd_tree(int, char *[]);
125 417a6e49 2020-09-26 stsp static const struct got_error* cmd_status(int, char *[]);
126 417a6e49 2020-09-26 stsp static const struct got_error* cmd_ref(int, char *[]);
127 417a6e49 2020-09-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 417a6e49 2020-09-26 stsp static const struct got_error* cmd_tag(int, char *[]);
129 417a6e49 2020-09-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 417a6e49 2020-09-26 stsp static const struct got_error* cmd_remove(int, char *[]);
131 417a6e49 2020-09-26 stsp static const struct got_error* cmd_revert(int, char *[]);
132 417a6e49 2020-09-26 stsp static const struct got_error* cmd_commit(int, char *[]);
133 417a6e49 2020-09-26 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 417a6e49 2020-09-26 stsp static const struct got_error* cmd_backout(int, char *[]);
135 417a6e49 2020-09-26 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 417a6e49 2020-09-26 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 417a6e49 2020-09-26 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 417a6e49 2020-09-26 stsp static const struct got_error* cmd_stage(int, char *[]);
139 417a6e49 2020-09-26 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 417a6e49 2020-09-26 stsp static const struct got_error* cmd_cat(int, char *[]);
141 417a6e49 2020-09-26 stsp static const struct got_error* cmd_info(int, char *[]);
142 417a6e49 2020-09-26 stsp
143 417a6e49 2020-09-26 stsp static struct got_cmd got_commands[] = {
144 417a6e49 2020-09-26 stsp { "init", cmd_init, usage_init, "" },
145 417a6e49 2020-09-26 stsp { "import", cmd_import, usage_import, "im" },
146 417a6e49 2020-09-26 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 417a6e49 2020-09-26 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 417a6e49 2020-09-26 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 417a6e49 2020-09-26 stsp { "update", cmd_update, usage_update, "up" },
150 417a6e49 2020-09-26 stsp { "log", cmd_log, usage_log, "" },
151 417a6e49 2020-09-26 stsp { "diff", cmd_diff, usage_diff, "di" },
152 417a6e49 2020-09-26 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 417a6e49 2020-09-26 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 417a6e49 2020-09-26 stsp { "status", cmd_status, usage_status, "st" },
155 417a6e49 2020-09-26 stsp { "ref", cmd_ref, usage_ref, "" },
156 417a6e49 2020-09-26 stsp { "branch", cmd_branch, usage_branch, "br" },
157 417a6e49 2020-09-26 stsp { "tag", cmd_tag, usage_tag, "" },
158 417a6e49 2020-09-26 stsp { "add", cmd_add, usage_add, "" },
159 417a6e49 2020-09-26 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 417a6e49 2020-09-26 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 417a6e49 2020-09-26 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 417a6e49 2020-09-26 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 417a6e49 2020-09-26 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 417a6e49 2020-09-26 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 417a6e49 2020-09-26 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 417a6e49 2020-09-26 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 417a6e49 2020-09-26 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 417a6e49 2020-09-26 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 417a6e49 2020-09-26 stsp { "cat", cmd_cat, usage_cat, "" },
170 417a6e49 2020-09-26 stsp { "info", cmd_info, usage_info, "" },
171 417a6e49 2020-09-26 stsp };
172 417a6e49 2020-09-26 stsp
173 417a6e49 2020-09-26 stsp static void
174 417a6e49 2020-09-26 stsp list_commands(void)
175 417a6e49 2020-09-26 stsp {
176 417a6e49 2020-09-26 stsp int i;
177 417a6e49 2020-09-26 stsp
178 417a6e49 2020-09-26 stsp fprintf(stderr, "commands:");
179 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_commands); i++) {
180 417a6e49 2020-09-26 stsp struct got_cmd *cmd = &got_commands[i];
181 417a6e49 2020-09-26 stsp fprintf(stderr, " %s", cmd->cmd_name);
182 417a6e49 2020-09-26 stsp }
183 417a6e49 2020-09-26 stsp fputc('\n', stderr);
184 417a6e49 2020-09-26 stsp }
185 417a6e49 2020-09-26 stsp
186 417a6e49 2020-09-26 stsp int
187 417a6e49 2020-09-26 stsp main(int argc, char *argv[])
188 417a6e49 2020-09-26 stsp {
189 417a6e49 2020-09-26 stsp struct got_cmd *cmd;
190 417a6e49 2020-09-26 stsp unsigned int i;
191 417a6e49 2020-09-26 stsp int ch;
192 417a6e49 2020-09-26 stsp int hflag = 0, Vflag = 0;
193 417a6e49 2020-09-26 stsp static struct option longopts[] = {
194 417a6e49 2020-09-26 stsp { "version", no_argument, NULL, 'V' },
195 417a6e49 2020-09-26 stsp { NULL, 0, NULL, 0}
196 417a6e49 2020-09-26 stsp };
197 417a6e49 2020-09-26 stsp
198 417a6e49 2020-09-26 stsp setlocale(LC_CTYPE, "");
199 417a6e49 2020-09-26 stsp
200 417a6e49 2020-09-26 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 417a6e49 2020-09-26 stsp switch (ch) {
202 417a6e49 2020-09-26 stsp case 'h':
203 417a6e49 2020-09-26 stsp hflag = 1;
204 417a6e49 2020-09-26 stsp break;
205 417a6e49 2020-09-26 stsp case 'V':
206 417a6e49 2020-09-26 stsp Vflag = 1;
207 417a6e49 2020-09-26 stsp break;
208 417a6e49 2020-09-26 stsp default:
209 417a6e49 2020-09-26 stsp usage(hflag);
210 417a6e49 2020-09-26 stsp /* NOTREACHED */
211 417a6e49 2020-09-26 stsp }
212 417a6e49 2020-09-26 stsp }
213 417a6e49 2020-09-26 stsp
214 417a6e49 2020-09-26 stsp argc -= optind;
215 417a6e49 2020-09-26 stsp argv += optind;
216 417a6e49 2020-09-26 stsp optind = 0;
217 417a6e49 2020-09-26 stsp
218 417a6e49 2020-09-26 stsp if (Vflag) {
219 417a6e49 2020-09-26 stsp got_version_print_str();
220 417a6e49 2020-09-26 stsp return 1;
221 417a6e49 2020-09-26 stsp }
222 417a6e49 2020-09-26 stsp
223 417a6e49 2020-09-26 stsp if (argc <= 0)
224 417a6e49 2020-09-26 stsp usage(hflag);
225 417a6e49 2020-09-26 stsp
226 417a6e49 2020-09-26 stsp signal(SIGINT, catch_sigint);
227 417a6e49 2020-09-26 stsp signal(SIGPIPE, catch_sigpipe);
228 417a6e49 2020-09-26 stsp
229 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_commands); i++) {
230 417a6e49 2020-09-26 stsp const struct got_error *error;
231 417a6e49 2020-09-26 stsp
232 417a6e49 2020-09-26 stsp cmd = &got_commands[i];
233 417a6e49 2020-09-26 stsp
234 417a6e49 2020-09-26 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 417a6e49 2020-09-26 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
236 417a6e49 2020-09-26 stsp continue;
237 417a6e49 2020-09-26 stsp
238 417a6e49 2020-09-26 stsp if (hflag)
239 417a6e49 2020-09-26 stsp got_commands[i].cmd_usage();
240 417a6e49 2020-09-26 stsp
241 417a6e49 2020-09-26 stsp error = got_commands[i].cmd_main(argc, argv);
242 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 417a6e49 2020-09-26 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 417a6e49 2020-09-26 stsp !(sigpipe_received &&
245 417a6e49 2020-09-26 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 417a6e49 2020-09-26 stsp !(sigint_received &&
247 417a6e49 2020-09-26 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
249 417a6e49 2020-09-26 stsp return 1;
250 417a6e49 2020-09-26 stsp }
251 417a6e49 2020-09-26 stsp
252 417a6e49 2020-09-26 stsp return 0;
253 417a6e49 2020-09-26 stsp }
254 417a6e49 2020-09-26 stsp
255 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 417a6e49 2020-09-26 stsp list_commands();
257 417a6e49 2020-09-26 stsp return 1;
258 417a6e49 2020-09-26 stsp }
259 417a6e49 2020-09-26 stsp
260 417a6e49 2020-09-26 stsp __dead static void
261 417a6e49 2020-09-26 stsp usage(int hflag)
262 417a6e49 2020-09-26 stsp {
263 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 417a6e49 2020-09-26 stsp getprogname());
265 417a6e49 2020-09-26 stsp if (hflag)
266 417a6e49 2020-09-26 stsp list_commands();
267 417a6e49 2020-09-26 stsp exit(1);
268 417a6e49 2020-09-26 stsp }
269 417a6e49 2020-09-26 stsp
270 417a6e49 2020-09-26 stsp static const struct got_error *
271 417a6e49 2020-09-26 stsp get_editor(char **abspath)
272 417a6e49 2020-09-26 stsp {
273 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
274 417a6e49 2020-09-26 stsp const char *editor;
275 417a6e49 2020-09-26 stsp
276 417a6e49 2020-09-26 stsp *abspath = NULL;
277 417a6e49 2020-09-26 stsp
278 417a6e49 2020-09-26 stsp editor = getenv("VISUAL");
279 417a6e49 2020-09-26 stsp if (editor == NULL)
280 417a6e49 2020-09-26 stsp editor = getenv("EDITOR");
281 417a6e49 2020-09-26 stsp
282 417a6e49 2020-09-26 stsp if (editor) {
283 417a6e49 2020-09-26 stsp err = got_path_find_prog(abspath, editor);
284 417a6e49 2020-09-26 stsp if (err)
285 417a6e49 2020-09-26 stsp return err;
286 417a6e49 2020-09-26 stsp }
287 417a6e49 2020-09-26 stsp
288 417a6e49 2020-09-26 stsp if (*abspath == NULL) {
289 417a6e49 2020-09-26 stsp *abspath = strdup("/bin/ed");
290 417a6e49 2020-09-26 stsp if (*abspath == NULL)
291 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
292 417a6e49 2020-09-26 stsp }
293 417a6e49 2020-09-26 stsp
294 417a6e49 2020-09-26 stsp return NULL;
295 417a6e49 2020-09-26 stsp }
296 417a6e49 2020-09-26 stsp
297 417a6e49 2020-09-26 stsp static const struct got_error *
298 417a6e49 2020-09-26 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 417a6e49 2020-09-26 stsp const char *worktree_path)
300 417a6e49 2020-09-26 stsp {
301 417a6e49 2020-09-26 stsp const struct got_error *err;
302 417a6e49 2020-09-26 stsp
303 417a6e49 2020-09-26 stsp #ifdef PROFILE
304 417a6e49 2020-09-26 stsp if (unveil("gmon.out", "rwc") != 0)
305 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", "gmon.out");
306 417a6e49 2020-09-26 stsp #endif
307 417a6e49 2020-09-26 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", repo_path);
309 417a6e49 2020-09-26 stsp
310 417a6e49 2020-09-26 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", worktree_path);
312 417a6e49 2020-09-26 stsp
313 417a6e49 2020-09-26 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
315 417a6e49 2020-09-26 stsp
316 417a6e49 2020-09-26 stsp err = got_privsep_unveil_exec_helpers();
317 417a6e49 2020-09-26 stsp if (err != NULL)
318 417a6e49 2020-09-26 stsp return err;
319 417a6e49 2020-09-26 stsp
320 417a6e49 2020-09-26 stsp if (unveil(NULL, NULL) != 0)
321 417a6e49 2020-09-26 stsp return got_error_from_errno("unveil");
322 417a6e49 2020-09-26 stsp
323 417a6e49 2020-09-26 stsp return NULL;
324 417a6e49 2020-09-26 stsp }
325 417a6e49 2020-09-26 stsp
326 417a6e49 2020-09-26 stsp __dead static void
327 417a6e49 2020-09-26 stsp usage_init(void)
328 417a6e49 2020-09-26 stsp {
329 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
330 417a6e49 2020-09-26 stsp exit(1);
331 417a6e49 2020-09-26 stsp }
332 417a6e49 2020-09-26 stsp
333 417a6e49 2020-09-26 stsp static const struct got_error *
334 417a6e49 2020-09-26 stsp cmd_init(int argc, char *argv[])
335 417a6e49 2020-09-26 stsp {
336 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
337 417a6e49 2020-09-26 stsp char *repo_path = NULL;
338 417a6e49 2020-09-26 stsp int ch;
339 417a6e49 2020-09-26 stsp
340 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 417a6e49 2020-09-26 stsp switch (ch) {
342 417a6e49 2020-09-26 stsp default:
343 417a6e49 2020-09-26 stsp usage_init();
344 417a6e49 2020-09-26 stsp /* NOTREACHED */
345 417a6e49 2020-09-26 stsp }
346 417a6e49 2020-09-26 stsp }
347 417a6e49 2020-09-26 stsp
348 417a6e49 2020-09-26 stsp argc -= optind;
349 417a6e49 2020-09-26 stsp argv += optind;
350 417a6e49 2020-09-26 stsp
351 417a6e49 2020-09-26 stsp #ifndef PROFILE
352 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 417a6e49 2020-09-26 stsp err(1, "pledge");
354 417a6e49 2020-09-26 stsp #endif
355 417a6e49 2020-09-26 stsp if (argc != 1)
356 417a6e49 2020-09-26 stsp usage_init();
357 417a6e49 2020-09-26 stsp
358 417a6e49 2020-09-26 stsp repo_path = strdup(argv[0]);
359 417a6e49 2020-09-26 stsp if (repo_path == NULL)
360 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
361 417a6e49 2020-09-26 stsp
362 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
363 417a6e49 2020-09-26 stsp
364 417a6e49 2020-09-26 stsp error = got_path_mkdir(repo_path);
365 417a6e49 2020-09-26 stsp if (error &&
366 417a6e49 2020-09-26 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
367 417a6e49 2020-09-26 stsp goto done;
368 417a6e49 2020-09-26 stsp
369 417a6e49 2020-09-26 stsp error = apply_unveil(repo_path, 0, NULL);
370 417a6e49 2020-09-26 stsp if (error)
371 417a6e49 2020-09-26 stsp goto done;
372 417a6e49 2020-09-26 stsp
373 417a6e49 2020-09-26 stsp error = got_repo_init(repo_path);
374 417a6e49 2020-09-26 stsp done:
375 417a6e49 2020-09-26 stsp free(repo_path);
376 417a6e49 2020-09-26 stsp return error;
377 417a6e49 2020-09-26 stsp }
378 417a6e49 2020-09-26 stsp
379 417a6e49 2020-09-26 stsp __dead static void
380 417a6e49 2020-09-26 stsp usage_import(void)
381 417a6e49 2020-09-26 stsp {
382 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 417a6e49 2020-09-26 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
384 417a6e49 2020-09-26 stsp exit(1);
385 417a6e49 2020-09-26 stsp }
386 417a6e49 2020-09-26 stsp
387 417a6e49 2020-09-26 stsp int
388 417a6e49 2020-09-26 stsp spawn_editor(const char *editor, const char *file)
389 417a6e49 2020-09-26 stsp {
390 417a6e49 2020-09-26 stsp pid_t pid;
391 417a6e49 2020-09-26 stsp sig_t sighup, sigint, sigquit;
392 417a6e49 2020-09-26 stsp int st = -1;
393 417a6e49 2020-09-26 stsp
394 417a6e49 2020-09-26 stsp sighup = signal(SIGHUP, SIG_IGN);
395 417a6e49 2020-09-26 stsp sigint = signal(SIGINT, SIG_IGN);
396 417a6e49 2020-09-26 stsp sigquit = signal(SIGQUIT, SIG_IGN);
397 417a6e49 2020-09-26 stsp
398 417a6e49 2020-09-26 stsp switch (pid = fork()) {
399 417a6e49 2020-09-26 stsp case -1:
400 417a6e49 2020-09-26 stsp goto doneediting;
401 417a6e49 2020-09-26 stsp case 0:
402 417a6e49 2020-09-26 stsp execl(editor, editor, file, (char *)NULL);
403 417a6e49 2020-09-26 stsp _exit(127);
404 417a6e49 2020-09-26 stsp }
405 417a6e49 2020-09-26 stsp
406 417a6e49 2020-09-26 stsp while (waitpid(pid, &st, 0) == -1)
407 417a6e49 2020-09-26 stsp if (errno != EINTR)
408 417a6e49 2020-09-26 stsp break;
409 417a6e49 2020-09-26 stsp
410 417a6e49 2020-09-26 stsp doneediting:
411 417a6e49 2020-09-26 stsp (void)signal(SIGHUP, sighup);
412 417a6e49 2020-09-26 stsp (void)signal(SIGINT, sigint);
413 417a6e49 2020-09-26 stsp (void)signal(SIGQUIT, sigquit);
414 417a6e49 2020-09-26 stsp
415 417a6e49 2020-09-26 stsp if (!WIFEXITED(st)) {
416 417a6e49 2020-09-26 stsp errno = EINTR;
417 417a6e49 2020-09-26 stsp return -1;
418 417a6e49 2020-09-26 stsp }
419 417a6e49 2020-09-26 stsp
420 417a6e49 2020-09-26 stsp return WEXITSTATUS(st);
421 417a6e49 2020-09-26 stsp }
422 417a6e49 2020-09-26 stsp
423 417a6e49 2020-09-26 stsp static const struct got_error *
424 417a6e49 2020-09-26 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 417a6e49 2020-09-26 stsp const char *initial_content)
426 417a6e49 2020-09-26 stsp {
427 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
428 417a6e49 2020-09-26 stsp char buf[1024];
429 417a6e49 2020-09-26 stsp struct stat st, st2;
430 417a6e49 2020-09-26 stsp FILE *fp;
431 417a6e49 2020-09-26 stsp int content_changed = 0;
432 417a6e49 2020-09-26 stsp size_t len;
433 417a6e49 2020-09-26 stsp
434 417a6e49 2020-09-26 stsp *logmsg = NULL;
435 417a6e49 2020-09-26 stsp
436 417a6e49 2020-09-26 stsp if (stat(logmsg_path, &st) == -1)
437 417a6e49 2020-09-26 stsp return got_error_from_errno2("stat", logmsg_path);
438 417a6e49 2020-09-26 stsp
439 417a6e49 2020-09-26 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 417a6e49 2020-09-26 stsp return got_error_from_errno("failed spawning editor");
441 417a6e49 2020-09-26 stsp
442 417a6e49 2020-09-26 stsp if (stat(logmsg_path, &st2) == -1)
443 417a6e49 2020-09-26 stsp return got_error_from_errno("stat");
444 417a6e49 2020-09-26 stsp
445 417a6e49 2020-09-26 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 417a6e49 2020-09-26 stsp "no changes made to commit message, aborting");
448 417a6e49 2020-09-26 stsp
449 417a6e49 2020-09-26 stsp *logmsg = malloc(st2.st_size + 1);
450 417a6e49 2020-09-26 stsp if (*logmsg == NULL)
451 417a6e49 2020-09-26 stsp return got_error_from_errno("malloc");
452 417a6e49 2020-09-26 stsp (*logmsg)[0] = '\0';
453 417a6e49 2020-09-26 stsp len = 0;
454 417a6e49 2020-09-26 stsp
455 417a6e49 2020-09-26 stsp fp = fopen(logmsg_path, "r");
456 417a6e49 2020-09-26 stsp if (fp == NULL) {
457 417a6e49 2020-09-26 stsp err = got_error_from_errno("fopen");
458 417a6e49 2020-09-26 stsp goto done;
459 417a6e49 2020-09-26 stsp }
460 417a6e49 2020-09-26 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 417a6e49 2020-09-26 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 417a6e49 2020-09-26 stsp content_changed = 1;
463 417a6e49 2020-09-26 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 417a6e49 2020-09-26 stsp continue; /* remove comments and leading empty lines */
465 417a6e49 2020-09-26 stsp len = strlcat(*logmsg, buf, st2.st_size);
466 417a6e49 2020-09-26 stsp }
467 417a6e49 2020-09-26 stsp fclose(fp);
468 417a6e49 2020-09-26 stsp
469 417a6e49 2020-09-26 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 417a6e49 2020-09-26 stsp (*logmsg)[len - 1] = '\0';
471 417a6e49 2020-09-26 stsp len--;
472 417a6e49 2020-09-26 stsp }
473 417a6e49 2020-09-26 stsp
474 417a6e49 2020-09-26 stsp if (len == 0 || !content_changed)
475 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 417a6e49 2020-09-26 stsp "commit message cannot be empty, aborting");
477 417a6e49 2020-09-26 stsp done:
478 417a6e49 2020-09-26 stsp if (err) {
479 417a6e49 2020-09-26 stsp free(*logmsg);
480 417a6e49 2020-09-26 stsp *logmsg = NULL;
481 417a6e49 2020-09-26 stsp }
482 417a6e49 2020-09-26 stsp return err;
483 417a6e49 2020-09-26 stsp }
484 417a6e49 2020-09-26 stsp
485 417a6e49 2020-09-26 stsp static const struct got_error *
486 417a6e49 2020-09-26 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 417a6e49 2020-09-26 stsp const char *path_dir, const char *branch_name)
488 417a6e49 2020-09-26 stsp {
489 417a6e49 2020-09-26 stsp char *initial_content = NULL;
490 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
491 417a6e49 2020-09-26 stsp int initial_content_len;
492 417a6e49 2020-09-26 stsp int fd = -1;
493 417a6e49 2020-09-26 stsp
494 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
495 417a6e49 2020-09-26 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 417a6e49 2020-09-26 stsp branch_name);
497 417a6e49 2020-09-26 stsp if (initial_content_len == -1)
498 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
499 417a6e49 2020-09-26 stsp
500 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 417a6e49 2020-09-26 stsp GOT_TMPDIR_STR "/got-importmsg");
502 417a6e49 2020-09-26 stsp if (err)
503 417a6e49 2020-09-26 stsp goto done;
504 417a6e49 2020-09-26 stsp
505 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", *logmsg_path);
507 417a6e49 2020-09-26 stsp goto done;
508 417a6e49 2020-09-26 stsp }
509 417a6e49 2020-09-26 stsp
510 417a6e49 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
511 417a6e49 2020-09-26 stsp done:
512 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", *logmsg_path);
514 417a6e49 2020-09-26 stsp free(initial_content);
515 417a6e49 2020-09-26 stsp if (err) {
516 417a6e49 2020-09-26 stsp free(*logmsg_path);
517 417a6e49 2020-09-26 stsp *logmsg_path = NULL;
518 417a6e49 2020-09-26 stsp }
519 417a6e49 2020-09-26 stsp return err;
520 417a6e49 2020-09-26 stsp }
521 417a6e49 2020-09-26 stsp
522 417a6e49 2020-09-26 stsp static const struct got_error *
523 417a6e49 2020-09-26 stsp import_progress(void *arg, const char *path)
524 417a6e49 2020-09-26 stsp {
525 417a6e49 2020-09-26 stsp printf("A %s\n", path);
526 417a6e49 2020-09-26 stsp return NULL;
527 417a6e49 2020-09-26 stsp }
528 417a6e49 2020-09-26 stsp
529 417a6e49 2020-09-26 stsp static const struct got_error *
530 417a6e49 2020-09-26 stsp get_author(char **author, struct got_repository *repo,
531 417a6e49 2020-09-26 stsp struct got_worktree *worktree)
532 417a6e49 2020-09-26 stsp {
533 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
534 417a6e49 2020-09-26 stsp const char *got_author = NULL, *name, *email;
535 417a6e49 2020-09-26 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
536 417a6e49 2020-09-26 stsp
537 417a6e49 2020-09-26 stsp *author = NULL;
538 417a6e49 2020-09-26 stsp
539 417a6e49 2020-09-26 stsp if (worktree)
540 417a6e49 2020-09-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
541 417a6e49 2020-09-26 stsp repo_conf = got_repo_get_gotconfig(repo);
542 417a6e49 2020-09-26 stsp
543 417a6e49 2020-09-26 stsp /*
544 417a6e49 2020-09-26 stsp * Priority of potential author information sources, from most
545 417a6e49 2020-09-26 stsp * significant to least significant:
546 417a6e49 2020-09-26 stsp * 1) work tree's .got/got.conf file
547 417a6e49 2020-09-26 stsp * 2) repository's got.conf file
548 417a6e49 2020-09-26 stsp * 3) repository's git config file
549 417a6e49 2020-09-26 stsp * 4) environment variables
550 417a6e49 2020-09-26 stsp * 5) global git config files (in user's home directory or /etc)
551 417a6e49 2020-09-26 stsp */
552 417a6e49 2020-09-26 stsp
553 417a6e49 2020-09-26 stsp if (worktree_conf)
554 417a6e49 2020-09-26 stsp got_author = got_gotconfig_get_author(worktree_conf);
555 417a6e49 2020-09-26 stsp if (got_author == NULL)
556 417a6e49 2020-09-26 stsp got_author = got_gotconfig_get_author(repo_conf);
557 417a6e49 2020-09-26 stsp if (got_author == NULL) {
558 417a6e49 2020-09-26 stsp name = got_repo_get_gitconfig_author_name(repo);
559 417a6e49 2020-09-26 stsp email = got_repo_get_gitconfig_author_email(repo);
560 417a6e49 2020-09-26 stsp if (name && email) {
561 417a6e49 2020-09-26 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
562 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
563 417a6e49 2020-09-26 stsp return NULL;
564 417a6e49 2020-09-26 stsp }
565 417a6e49 2020-09-26 stsp
566 417a6e49 2020-09-26 stsp got_author = getenv("GOT_AUTHOR");
567 417a6e49 2020-09-26 stsp if (got_author == NULL) {
568 417a6e49 2020-09-26 stsp name = got_repo_get_global_gitconfig_author_name(repo);
569 417a6e49 2020-09-26 stsp email = got_repo_get_global_gitconfig_author_email(
570 417a6e49 2020-09-26 stsp repo);
571 417a6e49 2020-09-26 stsp if (name && email) {
572 417a6e49 2020-09-26 stsp if (asprintf(author, "%s <%s>", name, email)
573 417a6e49 2020-09-26 stsp == -1)
574 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
575 417a6e49 2020-09-26 stsp return NULL;
576 417a6e49 2020-09-26 stsp }
577 417a6e49 2020-09-26 stsp /* TODO: Look up user in password database? */
578 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
579 417a6e49 2020-09-26 stsp }
580 417a6e49 2020-09-26 stsp }
581 417a6e49 2020-09-26 stsp
582 417a6e49 2020-09-26 stsp *author = strdup(got_author);
583 417a6e49 2020-09-26 stsp if (*author == NULL)
584 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
585 417a6e49 2020-09-26 stsp
586 417a6e49 2020-09-26 stsp /*
587 417a6e49 2020-09-26 stsp * Really dumb email address check; we're only doing this to
588 417a6e49 2020-09-26 stsp * avoid git's object parser breaking on commits we create.
589 417a6e49 2020-09-26 stsp */
590 417a6e49 2020-09-26 stsp while (*got_author && *got_author != '<')
591 417a6e49 2020-09-26 stsp got_author++;
592 417a6e49 2020-09-26 stsp if (*got_author != '<') {
593 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
594 417a6e49 2020-09-26 stsp goto done;
595 417a6e49 2020-09-26 stsp }
596 417a6e49 2020-09-26 stsp while (*got_author && *got_author != '@')
597 417a6e49 2020-09-26 stsp got_author++;
598 417a6e49 2020-09-26 stsp if (*got_author != '@') {
599 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
600 417a6e49 2020-09-26 stsp goto done;
601 417a6e49 2020-09-26 stsp }
602 417a6e49 2020-09-26 stsp while (*got_author && *got_author != '>')
603 417a6e49 2020-09-26 stsp got_author++;
604 417a6e49 2020-09-26 stsp if (*got_author != '>')
605 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
606 417a6e49 2020-09-26 stsp done:
607 417a6e49 2020-09-26 stsp if (err) {
608 417a6e49 2020-09-26 stsp free(*author);
609 417a6e49 2020-09-26 stsp *author = NULL;
610 417a6e49 2020-09-26 stsp }
611 417a6e49 2020-09-26 stsp return err;
612 417a6e49 2020-09-26 stsp }
613 417a6e49 2020-09-26 stsp
614 417a6e49 2020-09-26 stsp static const struct got_error *
615 417a6e49 2020-09-26 stsp get_gitconfig_path(char **gitconfig_path)
616 417a6e49 2020-09-26 stsp {
617 417a6e49 2020-09-26 stsp const char *homedir = getenv("HOME");
618 417a6e49 2020-09-26 stsp
619 417a6e49 2020-09-26 stsp *gitconfig_path = NULL;
620 417a6e49 2020-09-26 stsp if (homedir) {
621 417a6e49 2020-09-26 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
623 417a6e49 2020-09-26 stsp
624 417a6e49 2020-09-26 stsp }
625 417a6e49 2020-09-26 stsp return NULL;
626 417a6e49 2020-09-26 stsp }
627 417a6e49 2020-09-26 stsp
628 417a6e49 2020-09-26 stsp static const struct got_error *
629 417a6e49 2020-09-26 stsp cmd_import(int argc, char *argv[])
630 417a6e49 2020-09-26 stsp {
631 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
632 417a6e49 2020-09-26 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 417a6e49 2020-09-26 stsp const char *branch_name = "main";
635 417a6e49 2020-09-26 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
637 417a6e49 2020-09-26 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id = NULL;
639 417a6e49 2020-09-26 stsp int ch;
640 417a6e49 2020-09-26 stsp struct got_pathlist_head ignores;
641 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
642 417a6e49 2020-09-26 stsp int preserve_logmsg = 0;
643 417a6e49 2020-09-26 stsp
644 417a6e49 2020-09-26 stsp TAILQ_INIT(&ignores);
645 417a6e49 2020-09-26 stsp
646 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
647 417a6e49 2020-09-26 stsp switch (ch) {
648 417a6e49 2020-09-26 stsp case 'b':
649 417a6e49 2020-09-26 stsp branch_name = optarg;
650 417a6e49 2020-09-26 stsp break;
651 417a6e49 2020-09-26 stsp case 'm':
652 417a6e49 2020-09-26 stsp logmsg = strdup(optarg);
653 417a6e49 2020-09-26 stsp if (logmsg == NULL) {
654 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
655 417a6e49 2020-09-26 stsp goto done;
656 417a6e49 2020-09-26 stsp }
657 417a6e49 2020-09-26 stsp break;
658 417a6e49 2020-09-26 stsp case 'r':
659 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
660 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
661 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath",
662 417a6e49 2020-09-26 stsp optarg);
663 417a6e49 2020-09-26 stsp goto done;
664 417a6e49 2020-09-26 stsp }
665 417a6e49 2020-09-26 stsp break;
666 417a6e49 2020-09-26 stsp case 'I':
667 417a6e49 2020-09-26 stsp if (optarg[0] == '\0')
668 417a6e49 2020-09-26 stsp break;
669 417a6e49 2020-09-26 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
670 417a6e49 2020-09-26 stsp NULL);
671 417a6e49 2020-09-26 stsp if (error)
672 417a6e49 2020-09-26 stsp goto done;
673 417a6e49 2020-09-26 stsp break;
674 417a6e49 2020-09-26 stsp default:
675 417a6e49 2020-09-26 stsp usage_import();
676 417a6e49 2020-09-26 stsp /* NOTREACHED */
677 417a6e49 2020-09-26 stsp }
678 417a6e49 2020-09-26 stsp }
679 417a6e49 2020-09-26 stsp
680 417a6e49 2020-09-26 stsp argc -= optind;
681 417a6e49 2020-09-26 stsp argv += optind;
682 417a6e49 2020-09-26 stsp
683 417a6e49 2020-09-26 stsp #ifndef PROFILE
684 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
685 417a6e49 2020-09-26 stsp "unveil",
686 417a6e49 2020-09-26 stsp NULL) == -1)
687 417a6e49 2020-09-26 stsp err(1, "pledge");
688 417a6e49 2020-09-26 stsp #endif
689 417a6e49 2020-09-26 stsp if (argc != 1)
690 417a6e49 2020-09-26 stsp usage_import();
691 417a6e49 2020-09-26 stsp
692 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
693 417a6e49 2020-09-26 stsp repo_path = getcwd(NULL, 0);
694 417a6e49 2020-09-26 stsp if (repo_path == NULL)
695 417a6e49 2020-09-26 stsp return got_error_from_errno("getcwd");
696 417a6e49 2020-09-26 stsp }
697 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
698 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
699 417a6e49 2020-09-26 stsp if (error)
700 417a6e49 2020-09-26 stsp goto done;
701 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
702 417a6e49 2020-09-26 stsp if (error)
703 417a6e49 2020-09-26 stsp goto done;
704 417a6e49 2020-09-26 stsp
705 417a6e49 2020-09-26 stsp error = get_author(&author, repo, NULL);
706 417a6e49 2020-09-26 stsp if (error)
707 417a6e49 2020-09-26 stsp return error;
708 417a6e49 2020-09-26 stsp
709 417a6e49 2020-09-26 stsp /*
710 417a6e49 2020-09-26 stsp * Don't let the user create a branch name with a leading '-'.
711 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
712 417a6e49 2020-09-26 stsp * an unintended typo.
713 417a6e49 2020-09-26 stsp */
714 417a6e49 2020-09-26 stsp if (branch_name[0] == '-')
715 417a6e49 2020-09-26 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
716 417a6e49 2020-09-26 stsp
717 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
719 417a6e49 2020-09-26 stsp goto done;
720 417a6e49 2020-09-26 stsp }
721 417a6e49 2020-09-26 stsp
722 417a6e49 2020-09-26 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
723 417a6e49 2020-09-26 stsp if (error) {
724 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
725 417a6e49 2020-09-26 stsp goto done;
726 417a6e49 2020-09-26 stsp } else {
727 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 417a6e49 2020-09-26 stsp "import target branch already exists");
729 417a6e49 2020-09-26 stsp goto done;
730 417a6e49 2020-09-26 stsp }
731 417a6e49 2020-09-26 stsp
732 417a6e49 2020-09-26 stsp path_dir = realpath(argv[0], NULL);
733 417a6e49 2020-09-26 stsp if (path_dir == NULL) {
734 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath", argv[0]);
735 417a6e49 2020-09-26 stsp goto done;
736 417a6e49 2020-09-26 stsp }
737 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(path_dir);
738 417a6e49 2020-09-26 stsp
739 417a6e49 2020-09-26 stsp /*
740 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have
741 417a6e49 2020-09-26 stsp * to apply unveil after the log message has been written.
742 417a6e49 2020-09-26 stsp */
743 417a6e49 2020-09-26 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
744 417a6e49 2020-09-26 stsp error = get_editor(&editor);
745 417a6e49 2020-09-26 stsp if (error)
746 417a6e49 2020-09-26 stsp goto done;
747 417a6e49 2020-09-26 stsp free(logmsg);
748 417a6e49 2020-09-26 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
749 417a6e49 2020-09-26 stsp path_dir, refname);
750 417a6e49 2020-09-26 stsp if (error) {
751 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
752 417a6e49 2020-09-26 stsp logmsg_path != NULL)
753 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
754 417a6e49 2020-09-26 stsp goto done;
755 417a6e49 2020-09-26 stsp }
756 417a6e49 2020-09-26 stsp }
757 417a6e49 2020-09-26 stsp
758 417a6e49 2020-09-26 stsp if (unveil(path_dir, "r") != 0) {
759 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil", path_dir);
760 417a6e49 2020-09-26 stsp if (logmsg_path)
761 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
762 417a6e49 2020-09-26 stsp goto done;
763 417a6e49 2020-09-26 stsp }
764 417a6e49 2020-09-26 stsp
765 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
766 417a6e49 2020-09-26 stsp if (error) {
767 417a6e49 2020-09-26 stsp if (logmsg_path)
768 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
769 417a6e49 2020-09-26 stsp goto done;
770 417a6e49 2020-09-26 stsp }
771 417a6e49 2020-09-26 stsp
772 417a6e49 2020-09-26 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 417a6e49 2020-09-26 stsp author, &ignores, repo, import_progress, NULL);
774 417a6e49 2020-09-26 stsp if (error) {
775 417a6e49 2020-09-26 stsp if (logmsg_path)
776 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
777 417a6e49 2020-09-26 stsp goto done;
778 417a6e49 2020-09-26 stsp }
779 417a6e49 2020-09-26 stsp
780 417a6e49 2020-09-26 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
781 417a6e49 2020-09-26 stsp if (error) {
782 417a6e49 2020-09-26 stsp if (logmsg_path)
783 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
784 417a6e49 2020-09-26 stsp goto done;
785 417a6e49 2020-09-26 stsp }
786 417a6e49 2020-09-26 stsp
787 417a6e49 2020-09-26 stsp error = got_ref_write(branch_ref, repo);
788 417a6e49 2020-09-26 stsp if (error) {
789 417a6e49 2020-09-26 stsp if (logmsg_path)
790 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
791 417a6e49 2020-09-26 stsp goto done;
792 417a6e49 2020-09-26 stsp }
793 417a6e49 2020-09-26 stsp
794 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, new_commit_id);
795 417a6e49 2020-09-26 stsp if (error) {
796 417a6e49 2020-09-26 stsp if (logmsg_path)
797 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
798 417a6e49 2020-09-26 stsp goto done;
799 417a6e49 2020-09-26 stsp }
800 417a6e49 2020-09-26 stsp
801 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
802 417a6e49 2020-09-26 stsp if (error) {
803 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF) {
804 417a6e49 2020-09-26 stsp if (logmsg_path)
805 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
806 417a6e49 2020-09-26 stsp goto done;
807 417a6e49 2020-09-26 stsp }
808 417a6e49 2020-09-26 stsp
809 417a6e49 2020-09-26 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
810 417a6e49 2020-09-26 stsp branch_ref);
811 417a6e49 2020-09-26 stsp if (error) {
812 417a6e49 2020-09-26 stsp if (logmsg_path)
813 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
814 417a6e49 2020-09-26 stsp goto done;
815 417a6e49 2020-09-26 stsp }
816 417a6e49 2020-09-26 stsp
817 417a6e49 2020-09-26 stsp error = got_ref_write(head_ref, repo);
818 417a6e49 2020-09-26 stsp if (error) {
819 417a6e49 2020-09-26 stsp if (logmsg_path)
820 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
821 417a6e49 2020-09-26 stsp goto done;
822 417a6e49 2020-09-26 stsp }
823 417a6e49 2020-09-26 stsp }
824 417a6e49 2020-09-26 stsp
825 417a6e49 2020-09-26 stsp printf("Created branch %s with commit %s\n",
826 417a6e49 2020-09-26 stsp got_ref_get_name(branch_ref), id_str);
827 417a6e49 2020-09-26 stsp done:
828 417a6e49 2020-09-26 stsp if (preserve_logmsg) {
829 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: log message preserved in %s\n",
830 417a6e49 2020-09-26 stsp getprogname(), logmsg_path);
831 417a6e49 2020-09-26 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unlink", logmsg_path);
833 417a6e49 2020-09-26 stsp free(logmsg);
834 417a6e49 2020-09-26 stsp free(logmsg_path);
835 417a6e49 2020-09-26 stsp free(repo_path);
836 417a6e49 2020-09-26 stsp free(editor);
837 417a6e49 2020-09-26 stsp free(refname);
838 417a6e49 2020-09-26 stsp free(new_commit_id);
839 417a6e49 2020-09-26 stsp free(id_str);
840 417a6e49 2020-09-26 stsp free(author);
841 417a6e49 2020-09-26 stsp free(gitconfig_path);
842 417a6e49 2020-09-26 stsp if (branch_ref)
843 417a6e49 2020-09-26 stsp got_ref_close(branch_ref);
844 417a6e49 2020-09-26 stsp if (head_ref)
845 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
846 417a6e49 2020-09-26 stsp return error;
847 417a6e49 2020-09-26 stsp }
848 417a6e49 2020-09-26 stsp
849 417a6e49 2020-09-26 stsp __dead static void
850 417a6e49 2020-09-26 stsp usage_clone(void)
851 417a6e49 2020-09-26 stsp {
852 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 417a6e49 2020-09-26 stsp "[-R reference] repository-url [directory]\n", getprogname());
854 417a6e49 2020-09-26 stsp exit(1);
855 417a6e49 2020-09-26 stsp }
856 417a6e49 2020-09-26 stsp
857 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg {
858 417a6e49 2020-09-26 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
859 417a6e49 2020-09-26 stsp int last_p_indexed;
860 417a6e49 2020-09-26 stsp int last_p_resolved;
861 417a6e49 2020-09-26 stsp int verbosity;
862 417a6e49 2020-09-26 stsp };
863 417a6e49 2020-09-26 stsp
864 417a6e49 2020-09-26 stsp static const struct got_error *
865 417a6e49 2020-09-26 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
866 417a6e49 2020-09-26 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
867 417a6e49 2020-09-26 stsp {
868 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg *a = arg;
869 417a6e49 2020-09-26 stsp char scaled_size[FMT_SCALED_STRSIZE];
870 417a6e49 2020-09-26 stsp int p_indexed, p_resolved;
871 417a6e49 2020-09-26 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
872 417a6e49 2020-09-26 stsp
873 417a6e49 2020-09-26 stsp if (a->verbosity < 0)
874 417a6e49 2020-09-26 stsp return NULL;
875 417a6e49 2020-09-26 stsp
876 417a6e49 2020-09-26 stsp if (message && message[0] != '\0') {
877 417a6e49 2020-09-26 stsp printf("\rserver: %s", message);
878 417a6e49 2020-09-26 stsp fflush(stdout);
879 417a6e49 2020-09-26 stsp return NULL;
880 417a6e49 2020-09-26 stsp }
881 417a6e49 2020-09-26 stsp
882 417a6e49 2020-09-26 stsp if (packfile_size > 0 || nobj_indexed > 0) {
883 417a6e49 2020-09-26 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
884 417a6e49 2020-09-26 stsp (a->last_scaled_size[0] == '\0' ||
885 417a6e49 2020-09-26 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
886 417a6e49 2020-09-26 stsp print_size = 1;
887 417a6e49 2020-09-26 stsp if (strlcpy(a->last_scaled_size, scaled_size,
888 417a6e49 2020-09-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
889 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NO_SPACE);
890 417a6e49 2020-09-26 stsp }
891 417a6e49 2020-09-26 stsp if (nobj_indexed > 0) {
892 417a6e49 2020-09-26 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
893 417a6e49 2020-09-26 stsp if (p_indexed != a->last_p_indexed) {
894 417a6e49 2020-09-26 stsp a->last_p_indexed = p_indexed;
895 417a6e49 2020-09-26 stsp print_indexed = 1;
896 417a6e49 2020-09-26 stsp print_size = 1;
897 417a6e49 2020-09-26 stsp }
898 417a6e49 2020-09-26 stsp }
899 417a6e49 2020-09-26 stsp if (nobj_resolved > 0) {
900 417a6e49 2020-09-26 stsp p_resolved = (nobj_resolved * 100) /
901 417a6e49 2020-09-26 stsp (nobj_total - nobj_loose);
902 417a6e49 2020-09-26 stsp if (p_resolved != a->last_p_resolved) {
903 417a6e49 2020-09-26 stsp a->last_p_resolved = p_resolved;
904 417a6e49 2020-09-26 stsp print_resolved = 1;
905 417a6e49 2020-09-26 stsp print_indexed = 1;
906 417a6e49 2020-09-26 stsp print_size = 1;
907 417a6e49 2020-09-26 stsp }
908 417a6e49 2020-09-26 stsp }
909 417a6e49 2020-09-26 stsp
910 417a6e49 2020-09-26 stsp }
911 417a6e49 2020-09-26 stsp if (print_size || print_indexed || print_resolved)
912 417a6e49 2020-09-26 stsp printf("\r");
913 417a6e49 2020-09-26 stsp if (print_size)
914 417a6e49 2020-09-26 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
915 417a6e49 2020-09-26 stsp if (print_indexed)
916 417a6e49 2020-09-26 stsp printf("; indexing %d%%", p_indexed);
917 417a6e49 2020-09-26 stsp if (print_resolved)
918 417a6e49 2020-09-26 stsp printf("; resolving deltas %d%%", p_resolved);
919 417a6e49 2020-09-26 stsp if (print_size || print_indexed || print_resolved)
920 417a6e49 2020-09-26 stsp fflush(stdout);
921 417a6e49 2020-09-26 stsp
922 417a6e49 2020-09-26 stsp return NULL;
923 417a6e49 2020-09-26 stsp }
924 417a6e49 2020-09-26 stsp
925 417a6e49 2020-09-26 stsp static const struct got_error *
926 417a6e49 2020-09-26 stsp create_symref(const char *refname, struct got_reference *target_ref,
927 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
928 417a6e49 2020-09-26 stsp {
929 417a6e49 2020-09-26 stsp const struct got_error *err;
930 417a6e49 2020-09-26 stsp struct got_reference *head_symref;
931 417a6e49 2020-09-26 stsp
932 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
933 417a6e49 2020-09-26 stsp if (err)
934 417a6e49 2020-09-26 stsp return err;
935 417a6e49 2020-09-26 stsp
936 417a6e49 2020-09-26 stsp err = got_ref_write(head_symref, repo);
937 417a6e49 2020-09-26 stsp got_ref_close(head_symref);
938 417a6e49 2020-09-26 stsp if (err == NULL && verbosity > 0) {
939 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
940 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref));
941 417a6e49 2020-09-26 stsp }
942 417a6e49 2020-09-26 stsp return err;
943 417a6e49 2020-09-26 stsp }
944 417a6e49 2020-09-26 stsp
945 417a6e49 2020-09-26 stsp static const struct got_error *
946 417a6e49 2020-09-26 stsp list_remote_refs(struct got_pathlist_head *symrefs,
947 417a6e49 2020-09-26 stsp struct got_pathlist_head *refs)
948 417a6e49 2020-09-26 stsp {
949 417a6e49 2020-09-26 stsp const struct got_error *err;
950 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
951 417a6e49 2020-09-26 stsp
952 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, symrefs, entry) {
953 417a6e49 2020-09-26 stsp const char *refname = pe->path;
954 417a6e49 2020-09-26 stsp const char *targetref = pe->data;
955 417a6e49 2020-09-26 stsp
956 417a6e49 2020-09-26 stsp printf("%s: %s\n", refname, targetref);
957 417a6e49 2020-09-26 stsp }
958 417a6e49 2020-09-26 stsp
959 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, refs, entry) {
960 417a6e49 2020-09-26 stsp const char *refname = pe->path;
961 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
962 417a6e49 2020-09-26 stsp char *id_str;
963 417a6e49 2020-09-26 stsp
964 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
965 417a6e49 2020-09-26 stsp if (err)
966 417a6e49 2020-09-26 stsp return err;
967 417a6e49 2020-09-26 stsp printf("%s: %s\n", refname, id_str);
968 417a6e49 2020-09-26 stsp free(id_str);
969 417a6e49 2020-09-26 stsp }
970 417a6e49 2020-09-26 stsp
971 417a6e49 2020-09-26 stsp return NULL;
972 417a6e49 2020-09-26 stsp }
973 417a6e49 2020-09-26 stsp
974 417a6e49 2020-09-26 stsp static const struct got_error *
975 417a6e49 2020-09-26 stsp create_ref(const char *refname, struct got_object_id *id,
976 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
977 417a6e49 2020-09-26 stsp {
978 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
979 417a6e49 2020-09-26 stsp struct got_reference *ref;
980 417a6e49 2020-09-26 stsp char *id_str;
981 417a6e49 2020-09-26 stsp
982 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
983 417a6e49 2020-09-26 stsp if (err)
984 417a6e49 2020-09-26 stsp return err;
985 417a6e49 2020-09-26 stsp
986 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, id);
987 417a6e49 2020-09-26 stsp if (err)
988 417a6e49 2020-09-26 stsp goto done;
989 417a6e49 2020-09-26 stsp
990 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
991 417a6e49 2020-09-26 stsp got_ref_close(ref);
992 417a6e49 2020-09-26 stsp
993 417a6e49 2020-09-26 stsp if (err == NULL && verbosity >= 0)
994 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n", refname, id_str);
995 417a6e49 2020-09-26 stsp done:
996 417a6e49 2020-09-26 stsp free(id_str);
997 417a6e49 2020-09-26 stsp return err;
998 417a6e49 2020-09-26 stsp }
999 417a6e49 2020-09-26 stsp
1000 417a6e49 2020-09-26 stsp static int
1001 417a6e49 2020-09-26 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1002 417a6e49 2020-09-26 stsp {
1003 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/", 5) != 0)
1004 417a6e49 2020-09-26 stsp return 0;
1005 417a6e49 2020-09-26 stsp refname += 5;
1006 417a6e49 2020-09-26 stsp
1007 417a6e49 2020-09-26 stsp /*
1008 417a6e49 2020-09-26 stsp * Prevent fetching of references that won't make any
1009 417a6e49 2020-09-26 stsp * sense outside of the remote repository's context.
1010 417a6e49 2020-09-26 stsp */
1011 417a6e49 2020-09-26 stsp if (strncmp(refname, "got/", 4) == 0)
1012 417a6e49 2020-09-26 stsp return 0;
1013 417a6e49 2020-09-26 stsp if (strncmp(refname, "remotes/", 8) == 0)
1014 417a6e49 2020-09-26 stsp return 0;
1015 417a6e49 2020-09-26 stsp
1016 417a6e49 2020-09-26 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1017 417a6e49 2020-09-26 stsp wanted_ref += 5;
1018 417a6e49 2020-09-26 stsp
1019 417a6e49 2020-09-26 stsp /* Allow prefix match. */
1020 417a6e49 2020-09-26 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1021 417a6e49 2020-09-26 stsp return 1;
1022 417a6e49 2020-09-26 stsp
1023 417a6e49 2020-09-26 stsp /* Allow exact match. */
1024 417a6e49 2020-09-26 stsp return (strcmp(refname, wanted_ref) == 0);
1025 417a6e49 2020-09-26 stsp }
1026 417a6e49 2020-09-26 stsp
1027 417a6e49 2020-09-26 stsp static int
1028 417a6e49 2020-09-26 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1029 417a6e49 2020-09-26 stsp {
1030 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1031 417a6e49 2020-09-26 stsp
1032 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1033 417a6e49 2020-09-26 stsp if (match_wanted_ref(refname, pe->path))
1034 417a6e49 2020-09-26 stsp return 1;
1035 417a6e49 2020-09-26 stsp }
1036 417a6e49 2020-09-26 stsp
1037 417a6e49 2020-09-26 stsp return 0;
1038 417a6e49 2020-09-26 stsp }
1039 417a6e49 2020-09-26 stsp
1040 417a6e49 2020-09-26 stsp static const struct got_error *
1041 417a6e49 2020-09-26 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1042 417a6e49 2020-09-26 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1043 417a6e49 2020-09-26 stsp {
1044 417a6e49 2020-09-26 stsp const struct got_error *err;
1045 417a6e49 2020-09-26 stsp char *remote_refname;
1046 417a6e49 2020-09-26 stsp
1047 417a6e49 2020-09-26 stsp if (strncmp("refs/", refname, 5) == 0)
1048 417a6e49 2020-09-26 stsp refname += 5;
1049 417a6e49 2020-09-26 stsp
1050 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1051 417a6e49 2020-09-26 stsp remote_repo_name, refname) == -1)
1052 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1053 417a6e49 2020-09-26 stsp
1054 417a6e49 2020-09-26 stsp err = create_ref(remote_refname, id, verbosity, repo);
1055 417a6e49 2020-09-26 stsp free(remote_refname);
1056 417a6e49 2020-09-26 stsp return err;
1057 417a6e49 2020-09-26 stsp }
1058 417a6e49 2020-09-26 stsp
1059 417a6e49 2020-09-26 stsp static const struct got_error *
1060 417a6e49 2020-09-26 stsp cmd_clone(int argc, char *argv[])
1061 417a6e49 2020-09-26 stsp {
1062 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
1063 417a6e49 2020-09-26 stsp const char *uri, *dirname;
1064 417a6e49 2020-09-26 stsp char *proto, *host, *port, *repo_name, *server_path;
1065 417a6e49 2020-09-26 stsp char *default_destdir = NULL, *id_str = NULL;
1066 417a6e49 2020-09-26 stsp const char *repo_path, *remote_repo_path;
1067 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
1068 417a6e49 2020-09-26 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1069 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1070 417a6e49 2020-09-26 stsp struct got_object_id *pack_hash = NULL;
1071 417a6e49 2020-09-26 stsp int ch, fetchfd = -1, fetchstatus;
1072 417a6e49 2020-09-26 stsp pid_t fetchpid = -1;
1073 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg fpa;
1074 417a6e49 2020-09-26 stsp char *git_url = NULL;
1075 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *gotconfig_path = NULL;
1076 417a6e49 2020-09-26 stsp char *gitconfig = NULL, *gotconfig = NULL;
1077 417a6e49 2020-09-26 stsp FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1078 417a6e49 2020-09-26 stsp ssize_t n;
1079 417a6e49 2020-09-26 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1080 417a6e49 2020-09-26 stsp int list_refs_only = 0;
1081 417a6e49 2020-09-26 stsp struct got_reference *head_symref = NULL;
1082 417a6e49 2020-09-26 stsp
1083 417a6e49 2020-09-26 stsp TAILQ_INIT(&refs);
1084 417a6e49 2020-09-26 stsp TAILQ_INIT(&symrefs);
1085 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_branches);
1086 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_refs);
1087 417a6e49 2020-09-26 stsp
1088 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1089 417a6e49 2020-09-26 stsp switch (ch) {
1090 417a6e49 2020-09-26 stsp case 'a':
1091 417a6e49 2020-09-26 stsp fetch_all_branches = 1;
1092 417a6e49 2020-09-26 stsp break;
1093 417a6e49 2020-09-26 stsp case 'b':
1094 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_branches,
1095 417a6e49 2020-09-26 stsp optarg, NULL);
1096 417a6e49 2020-09-26 stsp if (error)
1097 417a6e49 2020-09-26 stsp return error;
1098 417a6e49 2020-09-26 stsp break;
1099 417a6e49 2020-09-26 stsp case 'l':
1100 417a6e49 2020-09-26 stsp list_refs_only = 1;
1101 417a6e49 2020-09-26 stsp break;
1102 417a6e49 2020-09-26 stsp case 'm':
1103 417a6e49 2020-09-26 stsp mirror_references = 1;
1104 417a6e49 2020-09-26 stsp break;
1105 417a6e49 2020-09-26 stsp case 'v':
1106 417a6e49 2020-09-26 stsp if (verbosity < 0)
1107 417a6e49 2020-09-26 stsp verbosity = 0;
1108 417a6e49 2020-09-26 stsp else if (verbosity < 3)
1109 417a6e49 2020-09-26 stsp verbosity++;
1110 417a6e49 2020-09-26 stsp break;
1111 417a6e49 2020-09-26 stsp case 'q':
1112 417a6e49 2020-09-26 stsp verbosity = -1;
1113 417a6e49 2020-09-26 stsp break;
1114 417a6e49 2020-09-26 stsp case 'R':
1115 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_refs,
1116 417a6e49 2020-09-26 stsp optarg, NULL);
1117 417a6e49 2020-09-26 stsp if (error)
1118 417a6e49 2020-09-26 stsp return error;
1119 417a6e49 2020-09-26 stsp break;
1120 417a6e49 2020-09-26 stsp default:
1121 417a6e49 2020-09-26 stsp usage_clone();
1122 417a6e49 2020-09-26 stsp break;
1123 417a6e49 2020-09-26 stsp }
1124 417a6e49 2020-09-26 stsp }
1125 417a6e49 2020-09-26 stsp argc -= optind;
1126 417a6e49 2020-09-26 stsp argv += optind;
1127 417a6e49 2020-09-26 stsp
1128 417a6e49 2020-09-26 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1129 417a6e49 2020-09-26 stsp errx(1, "-a and -b options are mutually exclusive");
1130 417a6e49 2020-09-26 stsp if (list_refs_only) {
1131 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_branches))
1132 417a6e49 2020-09-26 stsp errx(1, "-l and -b options are mutually exclusive");
1133 417a6e49 2020-09-26 stsp if (fetch_all_branches)
1134 417a6e49 2020-09-26 stsp errx(1, "-l and -a options are mutually exclusive");
1135 417a6e49 2020-09-26 stsp if (mirror_references)
1136 417a6e49 2020-09-26 stsp errx(1, "-l and -m options are mutually exclusive");
1137 417a6e49 2020-09-26 stsp if (verbosity == -1)
1138 417a6e49 2020-09-26 stsp errx(1, "-l and -q options are mutually exclusive");
1139 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_refs))
1140 417a6e49 2020-09-26 stsp errx(1, "-l and -R options are mutually exclusive");
1141 417a6e49 2020-09-26 stsp }
1142 417a6e49 2020-09-26 stsp
1143 417a6e49 2020-09-26 stsp uri = argv[0];
1144 417a6e49 2020-09-26 stsp
1145 417a6e49 2020-09-26 stsp if (argc == 1)
1146 417a6e49 2020-09-26 stsp dirname = NULL;
1147 417a6e49 2020-09-26 stsp else if (argc == 2)
1148 417a6e49 2020-09-26 stsp dirname = argv[1];
1149 417a6e49 2020-09-26 stsp else
1150 417a6e49 2020-09-26 stsp usage_clone();
1151 417a6e49 2020-09-26 stsp
1152 417a6e49 2020-09-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1153 417a6e49 2020-09-26 stsp &repo_name, uri);
1154 417a6e49 2020-09-26 stsp if (error)
1155 417a6e49 2020-09-26 stsp goto done;
1156 417a6e49 2020-09-26 stsp
1157 417a6e49 2020-09-26 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1158 417a6e49 2020-09-26 stsp host, port ? ":" : "", port ? port : "",
1159 417a6e49 2020-09-26 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1160 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1161 417a6e49 2020-09-26 stsp goto done;
1162 417a6e49 2020-09-26 stsp }
1163 417a6e49 2020-09-26 stsp
1164 417a6e49 2020-09-26 stsp if (strcmp(proto, "git") == 0) {
1165 417a6e49 2020-09-26 stsp #ifndef PROFILE
1166 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1167 417a6e49 2020-09-26 stsp "sendfd dns inet unveil", NULL) == -1)
1168 417a6e49 2020-09-26 stsp err(1, "pledge");
1169 417a6e49 2020-09-26 stsp #endif
1170 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1171 417a6e49 2020-09-26 stsp strcmp(proto, "ssh") == 0) {
1172 417a6e49 2020-09-26 stsp #ifndef PROFILE
1173 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1174 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
1175 417a6e49 2020-09-26 stsp err(1, "pledge");
1176 417a6e49 2020-09-26 stsp #endif
1177 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "http") == 0 ||
1178 417a6e49 2020-09-26 stsp strcmp(proto, "git+http") == 0) {
1179 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1180 417a6e49 2020-09-26 stsp goto done;
1181 417a6e49 2020-09-26 stsp } else {
1182 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1183 417a6e49 2020-09-26 stsp goto done;
1184 417a6e49 2020-09-26 stsp }
1185 417a6e49 2020-09-26 stsp if (dirname == NULL) {
1186 417a6e49 2020-09-26 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1187 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1188 417a6e49 2020-09-26 stsp goto done;
1189 417a6e49 2020-09-26 stsp }
1190 417a6e49 2020-09-26 stsp repo_path = default_destdir;
1191 417a6e49 2020-09-26 stsp } else
1192 417a6e49 2020-09-26 stsp repo_path = dirname;
1193 417a6e49 2020-09-26 stsp
1194 417a6e49 2020-09-26 stsp if (!list_refs_only) {
1195 417a6e49 2020-09-26 stsp error = got_path_mkdir(repo_path);
1196 417a6e49 2020-09-26 stsp if (error)
1197 417a6e49 2020-09-26 stsp goto done;
1198 417a6e49 2020-09-26 stsp
1199 417a6e49 2020-09-26 stsp error = got_repo_init(repo_path);
1200 417a6e49 2020-09-26 stsp if (error)
1201 417a6e49 2020-09-26 stsp goto done;
1202 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
1203 417a6e49 2020-09-26 stsp if (error)
1204 417a6e49 2020-09-26 stsp goto done;
1205 417a6e49 2020-09-26 stsp }
1206 417a6e49 2020-09-26 stsp
1207 417a6e49 2020-09-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1208 417a6e49 2020-09-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1209 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil",
1210 417a6e49 2020-09-26 stsp GOT_FETCH_PATH_SSH);
1211 417a6e49 2020-09-26 stsp goto done;
1212 417a6e49 2020-09-26 stsp }
1213 417a6e49 2020-09-26 stsp }
1214 417a6e49 2020-09-26 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1215 417a6e49 2020-09-26 stsp if (error)
1216 417a6e49 2020-09-26 stsp goto done;
1217 417a6e49 2020-09-26 stsp
1218 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1219 417a6e49 2020-09-26 stsp printf("Connecting to %s%s%s\n", host,
1220 417a6e49 2020-09-26 stsp port ? ":" : "", port ? port : "");
1221 417a6e49 2020-09-26 stsp
1222 417a6e49 2020-09-26 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1223 417a6e49 2020-09-26 stsp server_path, verbosity);
1224 417a6e49 2020-09-26 stsp if (error)
1225 417a6e49 2020-09-26 stsp goto done;
1226 417a6e49 2020-09-26 stsp
1227 417a6e49 2020-09-26 stsp fpa.last_scaled_size[0] = '\0';
1228 417a6e49 2020-09-26 stsp fpa.last_p_indexed = -1;
1229 417a6e49 2020-09-26 stsp fpa.last_p_resolved = -1;
1230 417a6e49 2020-09-26 stsp fpa.verbosity = verbosity;
1231 417a6e49 2020-09-26 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1232 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1233 417a6e49 2020-09-26 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1234 417a6e49 2020-09-26 stsp list_refs_only, verbosity, fetchfd, repo,
1235 417a6e49 2020-09-26 stsp fetch_progress, &fpa);
1236 417a6e49 2020-09-26 stsp if (error)
1237 417a6e49 2020-09-26 stsp goto done;
1238 417a6e49 2020-09-26 stsp
1239 417a6e49 2020-09-26 stsp if (list_refs_only) {
1240 417a6e49 2020-09-26 stsp error = list_remote_refs(&symrefs, &refs);
1241 417a6e49 2020-09-26 stsp goto done;
1242 417a6e49 2020-09-26 stsp }
1243 417a6e49 2020-09-26 stsp
1244 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, pack_hash);
1245 417a6e49 2020-09-26 stsp if (error)
1246 417a6e49 2020-09-26 stsp goto done;
1247 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1248 417a6e49 2020-09-26 stsp printf("\nFetched %s.pack\n", id_str);
1249 417a6e49 2020-09-26 stsp free(id_str);
1250 417a6e49 2020-09-26 stsp
1251 417a6e49 2020-09-26 stsp /* Set up references provided with the pack file. */
1252 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1253 417a6e49 2020-09-26 stsp const char *refname = pe->path;
1254 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
1255 417a6e49 2020-09-26 stsp char *remote_refname;
1256 417a6e49 2020-09-26 stsp
1257 417a6e49 2020-09-26 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1258 417a6e49 2020-09-26 stsp !mirror_references) {
1259 417a6e49 2020-09-26 stsp error = create_wanted_ref(refname, id,
1260 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1261 417a6e49 2020-09-26 stsp verbosity - 1, repo);
1262 417a6e49 2020-09-26 stsp if (error)
1263 417a6e49 2020-09-26 stsp goto done;
1264 417a6e49 2020-09-26 stsp continue;
1265 417a6e49 2020-09-26 stsp }
1266 417a6e49 2020-09-26 stsp
1267 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity - 1, repo);
1268 417a6e49 2020-09-26 stsp if (error)
1269 417a6e49 2020-09-26 stsp goto done;
1270 417a6e49 2020-09-26 stsp
1271 417a6e49 2020-09-26 stsp if (mirror_references)
1272 417a6e49 2020-09-26 stsp continue;
1273 417a6e49 2020-09-26 stsp
1274 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1275 417a6e49 2020-09-26 stsp continue;
1276 417a6e49 2020-09-26 stsp
1277 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname,
1278 417a6e49 2020-09-26 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1279 417a6e49 2020-09-26 stsp refname + 11) == -1) {
1280 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1281 417a6e49 2020-09-26 stsp goto done;
1282 417a6e49 2020-09-26 stsp }
1283 417a6e49 2020-09-26 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1284 417a6e49 2020-09-26 stsp free(remote_refname);
1285 417a6e49 2020-09-26 stsp if (error)
1286 417a6e49 2020-09-26 stsp goto done;
1287 417a6e49 2020-09-26 stsp }
1288 417a6e49 2020-09-26 stsp
1289 417a6e49 2020-09-26 stsp /* Set the HEAD reference if the server provided one. */
1290 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1291 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
1292 417a6e49 2020-09-26 stsp const char *refname = pe->path;
1293 417a6e49 2020-09-26 stsp const char *target = pe->data;
1294 417a6e49 2020-09-26 stsp char *remote_refname = NULL, *remote_target = NULL;
1295 417a6e49 2020-09-26 stsp
1296 417a6e49 2020-09-26 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1297 417a6e49 2020-09-26 stsp continue;
1298 417a6e49 2020-09-26 stsp
1299 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, target, 0);
1300 417a6e49 2020-09-26 stsp if (error) {
1301 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
1302 417a6e49 2020-09-26 stsp error = NULL;
1303 417a6e49 2020-09-26 stsp continue;
1304 417a6e49 2020-09-26 stsp }
1305 417a6e49 2020-09-26 stsp goto done;
1306 417a6e49 2020-09-26 stsp }
1307 417a6e49 2020-09-26 stsp
1308 417a6e49 2020-09-26 stsp error = create_symref(refname, target_ref, verbosity, repo);
1309 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
1310 417a6e49 2020-09-26 stsp if (error)
1311 417a6e49 2020-09-26 stsp goto done;
1312 417a6e49 2020-09-26 stsp
1313 417a6e49 2020-09-26 stsp if (mirror_references)
1314 417a6e49 2020-09-26 stsp continue;
1315 417a6e49 2020-09-26 stsp
1316 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", target, 11) != 0)
1317 417a6e49 2020-09-26 stsp continue;
1318 417a6e49 2020-09-26 stsp
1319 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname,
1320 417a6e49 2020-09-26 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1321 417a6e49 2020-09-26 stsp refname) == -1) {
1322 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1323 417a6e49 2020-09-26 stsp goto done;
1324 417a6e49 2020-09-26 stsp }
1325 417a6e49 2020-09-26 stsp if (asprintf(&remote_target,
1326 417a6e49 2020-09-26 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1327 417a6e49 2020-09-26 stsp target + 11) == -1) {
1328 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1329 417a6e49 2020-09-26 stsp free(remote_refname);
1330 417a6e49 2020-09-26 stsp goto done;
1331 417a6e49 2020-09-26 stsp }
1332 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1333 417a6e49 2020-09-26 stsp if (error) {
1334 417a6e49 2020-09-26 stsp free(remote_refname);
1335 417a6e49 2020-09-26 stsp free(remote_target);
1336 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
1337 417a6e49 2020-09-26 stsp error = NULL;
1338 417a6e49 2020-09-26 stsp continue;
1339 417a6e49 2020-09-26 stsp }
1340 417a6e49 2020-09-26 stsp goto done;
1341 417a6e49 2020-09-26 stsp }
1342 417a6e49 2020-09-26 stsp error = create_symref(remote_refname, target_ref,
1343 417a6e49 2020-09-26 stsp verbosity - 1, repo);
1344 417a6e49 2020-09-26 stsp free(remote_refname);
1345 417a6e49 2020-09-26 stsp free(remote_target);
1346 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
1347 417a6e49 2020-09-26 stsp if (error)
1348 417a6e49 2020-09-26 stsp goto done;
1349 417a6e49 2020-09-26 stsp }
1350 417a6e49 2020-09-26 stsp if (pe == NULL) {
1351 417a6e49 2020-09-26 stsp /*
1352 417a6e49 2020-09-26 stsp * We failed to set the HEAD reference. If we asked for
1353 417a6e49 2020-09-26 stsp * a set of wanted branches use the first of one of those
1354 417a6e49 2020-09-26 stsp * which could be fetched instead.
1355 417a6e49 2020-09-26 stsp */
1356 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1357 417a6e49 2020-09-26 stsp const char *target = pe->path;
1358 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
1359 417a6e49 2020-09-26 stsp
1360 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, target, 0);
1361 417a6e49 2020-09-26 stsp if (error) {
1362 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
1363 417a6e49 2020-09-26 stsp error = NULL;
1364 417a6e49 2020-09-26 stsp continue;
1365 417a6e49 2020-09-26 stsp }
1366 417a6e49 2020-09-26 stsp goto done;
1367 417a6e49 2020-09-26 stsp }
1368 417a6e49 2020-09-26 stsp
1369 417a6e49 2020-09-26 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1370 417a6e49 2020-09-26 stsp verbosity, repo);
1371 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
1372 417a6e49 2020-09-26 stsp if (error)
1373 417a6e49 2020-09-26 stsp goto done;
1374 417a6e49 2020-09-26 stsp break;
1375 417a6e49 2020-09-26 stsp }
1376 417a6e49 2020-09-26 stsp }
1377 417a6e49 2020-09-26 stsp
1378 417a6e49 2020-09-26 stsp /* Create got.conf(5). */
1379 417a6e49 2020-09-26 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1380 417a6e49 2020-09-26 stsp if (gotconfig_path == NULL) {
1381 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_repo_get_path_gotconfig");
1382 417a6e49 2020-09-26 stsp goto done;
1383 417a6e49 2020-09-26 stsp }
1384 417a6e49 2020-09-26 stsp gotconfig_file = fopen(gotconfig_path, "a");
1385 417a6e49 2020-09-26 stsp if (gotconfig_file == NULL) {
1386 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen", gotconfig_path);
1387 417a6e49 2020-09-26 stsp goto done;
1388 417a6e49 2020-09-26 stsp }
1389 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(server_path);
1390 417a6e49 2020-09-26 stsp remote_repo_path = server_path;
1391 417a6e49 2020-09-26 stsp while (remote_repo_path[0] == '/')
1392 417a6e49 2020-09-26 stsp remote_repo_path++;
1393 417a6e49 2020-09-26 stsp if (asprintf(&gotconfig,
1394 417a6e49 2020-09-26 stsp "remote \"%s\" {\n"
1395 417a6e49 2020-09-26 stsp "\tserver %s\n"
1396 417a6e49 2020-09-26 stsp "\tprotocol %s\n"
1397 417a6e49 2020-09-26 stsp "%s%s%s"
1398 417a6e49 2020-09-26 stsp "\trepository \"%s\"\n"
1399 417a6e49 2020-09-26 stsp "%s"
1400 417a6e49 2020-09-26 stsp "}\n",
1401 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1402 417a6e49 2020-09-26 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1403 417a6e49 2020-09-26 stsp remote_repo_path,
1404 417a6e49 2020-09-26 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1405 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1406 417a6e49 2020-09-26 stsp goto done;
1407 417a6e49 2020-09-26 stsp }
1408 417a6e49 2020-09-26 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1409 417a6e49 2020-09-26 stsp if (n != strlen(gotconfig)) {
1410 417a6e49 2020-09-26 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1411 417a6e49 2020-09-26 stsp goto done;
1412 417a6e49 2020-09-26 stsp }
1413 417a6e49 2020-09-26 stsp
1414 417a6e49 2020-09-26 stsp /* Create a config file Git can understand. */
1415 417a6e49 2020-09-26 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1416 417a6e49 2020-09-26 stsp if (gitconfig_path == NULL) {
1417 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1418 417a6e49 2020-09-26 stsp goto done;
1419 417a6e49 2020-09-26 stsp }
1420 417a6e49 2020-09-26 stsp gitconfig_file = fopen(gitconfig_path, "a");
1421 417a6e49 2020-09-26 stsp if (gitconfig_file == NULL) {
1422 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1423 417a6e49 2020-09-26 stsp goto done;
1424 417a6e49 2020-09-26 stsp }
1425 417a6e49 2020-09-26 stsp if (mirror_references) {
1426 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1427 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1428 417a6e49 2020-09-26 stsp "\turl = %s\n"
1429 417a6e49 2020-09-26 stsp "\tfetch = +refs/*:refs/*\n"
1430 417a6e49 2020-09-26 stsp "\tmirror = true\n",
1431 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1432 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1433 417a6e49 2020-09-26 stsp goto done;
1434 417a6e49 2020-09-26 stsp }
1435 417a6e49 2020-09-26 stsp } else if (fetch_all_branches) {
1436 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1437 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1438 417a6e49 2020-09-26 stsp "\turl = %s\n"
1439 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1440 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1441 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1442 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1443 417a6e49 2020-09-26 stsp goto done;
1444 417a6e49 2020-09-26 stsp }
1445 417a6e49 2020-09-26 stsp } else {
1446 417a6e49 2020-09-26 stsp const char *branchname;
1447 417a6e49 2020-09-26 stsp
1448 417a6e49 2020-09-26 stsp /*
1449 417a6e49 2020-09-26 stsp * If the server specified a default branch, use just that one.
1450 417a6e49 2020-09-26 stsp * Otherwise fall back to fetching all branches on next fetch.
1451 417a6e49 2020-09-26 stsp */
1452 417a6e49 2020-09-26 stsp if (head_symref) {
1453 417a6e49 2020-09-26 stsp branchname = got_ref_get_symref_target(head_symref);
1454 417a6e49 2020-09-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1455 417a6e49 2020-09-26 stsp branchname += 11;
1456 417a6e49 2020-09-26 stsp } else
1457 417a6e49 2020-09-26 stsp branchname = "*"; /* fall back to all branches */
1458 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1459 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1460 417a6e49 2020-09-26 stsp "\turl = %s\n"
1461 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1462 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1463 417a6e49 2020-09-26 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1464 417a6e49 2020-09-26 stsp branchname) == -1) {
1465 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1466 417a6e49 2020-09-26 stsp goto done;
1467 417a6e49 2020-09-26 stsp }
1468 417a6e49 2020-09-26 stsp }
1469 417a6e49 2020-09-26 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1470 417a6e49 2020-09-26 stsp if (n != strlen(gitconfig)) {
1471 417a6e49 2020-09-26 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1472 417a6e49 2020-09-26 stsp goto done;
1473 417a6e49 2020-09-26 stsp }
1474 417a6e49 2020-09-26 stsp
1475 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1476 417a6e49 2020-09-26 stsp printf("Created %s repository '%s'\n",
1477 417a6e49 2020-09-26 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1478 417a6e49 2020-09-26 stsp done:
1479 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
1480 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
1481 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
1482 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1483 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
1484 417a6e49 2020-09-26 stsp }
1485 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1486 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
1487 417a6e49 2020-09-26 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1488 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1489 417a6e49 2020-09-26 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1490 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1491 417a6e49 2020-09-26 stsp if (repo)
1492 417a6e49 2020-09-26 stsp got_repo_close(repo);
1493 417a6e49 2020-09-26 stsp if (head_symref)
1494 417a6e49 2020-09-26 stsp got_ref_close(head_symref);
1495 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1496 417a6e49 2020-09-26 stsp free((void *)pe->path);
1497 417a6e49 2020-09-26 stsp free(pe->data);
1498 417a6e49 2020-09-26 stsp }
1499 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
1500 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1501 417a6e49 2020-09-26 stsp free((void *)pe->path);
1502 417a6e49 2020-09-26 stsp free(pe->data);
1503 417a6e49 2020-09-26 stsp }
1504 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
1505 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
1506 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
1507 417a6e49 2020-09-26 stsp free(pack_hash);
1508 417a6e49 2020-09-26 stsp free(proto);
1509 417a6e49 2020-09-26 stsp free(host);
1510 417a6e49 2020-09-26 stsp free(port);
1511 417a6e49 2020-09-26 stsp free(server_path);
1512 417a6e49 2020-09-26 stsp free(repo_name);
1513 417a6e49 2020-09-26 stsp free(default_destdir);
1514 417a6e49 2020-09-26 stsp free(gotconfig);
1515 417a6e49 2020-09-26 stsp free(gitconfig);
1516 417a6e49 2020-09-26 stsp free(gotconfig_path);
1517 417a6e49 2020-09-26 stsp free(gitconfig_path);
1518 417a6e49 2020-09-26 stsp free(git_url);
1519 417a6e49 2020-09-26 stsp return error;
1520 417a6e49 2020-09-26 stsp }
1521 417a6e49 2020-09-26 stsp
1522 417a6e49 2020-09-26 stsp static const struct got_error *
1523 417a6e49 2020-09-26 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1524 417a6e49 2020-09-26 stsp int replace_tags, int verbosity, struct got_repository *repo)
1525 417a6e49 2020-09-26 stsp {
1526 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1527 417a6e49 2020-09-26 stsp char *new_id_str = NULL;
1528 417a6e49 2020-09-26 stsp struct got_object_id *old_id = NULL;
1529 417a6e49 2020-09-26 stsp
1530 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
1531 417a6e49 2020-09-26 stsp if (err)
1532 417a6e49 2020-09-26 stsp goto done;
1533 417a6e49 2020-09-26 stsp
1534 417a6e49 2020-09-26 stsp if (!replace_tags &&
1535 417a6e49 2020-09-26 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1536 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1537 417a6e49 2020-09-26 stsp if (err)
1538 417a6e49 2020-09-26 stsp goto done;
1539 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1540 417a6e49 2020-09-26 stsp goto done;
1541 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1542 417a6e49 2020-09-26 stsp printf("Rejecting update of existing tag %s: %s\n",
1543 417a6e49 2020-09-26 stsp got_ref_get_name(ref), new_id_str);
1544 417a6e49 2020-09-26 stsp }
1545 417a6e49 2020-09-26 stsp goto done;
1546 417a6e49 2020-09-26 stsp }
1547 417a6e49 2020-09-26 stsp
1548 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1549 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1550 417a6e49 2020-09-26 stsp printf("Replacing reference %s: %s\n",
1551 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1552 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1553 417a6e49 2020-09-26 stsp }
1554 417a6e49 2020-09-26 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1555 417a6e49 2020-09-26 stsp if (err)
1556 417a6e49 2020-09-26 stsp goto done;
1557 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1558 417a6e49 2020-09-26 stsp if (err)
1559 417a6e49 2020-09-26 stsp goto done;
1560 417a6e49 2020-09-26 stsp } else {
1561 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1562 417a6e49 2020-09-26 stsp if (err)
1563 417a6e49 2020-09-26 stsp goto done;
1564 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1565 417a6e49 2020-09-26 stsp goto done;
1566 417a6e49 2020-09-26 stsp
1567 417a6e49 2020-09-26 stsp err = got_ref_change_ref(ref, new_id);
1568 417a6e49 2020-09-26 stsp if (err)
1569 417a6e49 2020-09-26 stsp goto done;
1570 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1571 417a6e49 2020-09-26 stsp if (err)
1572 417a6e49 2020-09-26 stsp goto done;
1573 417a6e49 2020-09-26 stsp }
1574 417a6e49 2020-09-26 stsp
1575 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1576 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1577 417a6e49 2020-09-26 stsp new_id_str);
1578 417a6e49 2020-09-26 stsp done:
1579 417a6e49 2020-09-26 stsp free(old_id);
1580 417a6e49 2020-09-26 stsp free(new_id_str);
1581 417a6e49 2020-09-26 stsp return err;
1582 417a6e49 2020-09-26 stsp }
1583 417a6e49 2020-09-26 stsp
1584 417a6e49 2020-09-26 stsp static const struct got_error *
1585 417a6e49 2020-09-26 stsp update_symref(const char *refname, struct got_reference *target_ref,
1586 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1587 417a6e49 2020-09-26 stsp {
1588 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1589 417a6e49 2020-09-26 stsp struct got_reference *symref;
1590 417a6e49 2020-09-26 stsp int symref_is_locked = 0;
1591 417a6e49 2020-09-26 stsp
1592 417a6e49 2020-09-26 stsp err = got_ref_open(&symref, repo, refname, 1);
1593 417a6e49 2020-09-26 stsp if (err) {
1594 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1595 417a6e49 2020-09-26 stsp return err;
1596 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1597 417a6e49 2020-09-26 stsp if (err)
1598 417a6e49 2020-09-26 stsp goto done;
1599 417a6e49 2020-09-26 stsp
1600 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1601 417a6e49 2020-09-26 stsp if (err)
1602 417a6e49 2020-09-26 stsp goto done;
1603 417a6e49 2020-09-26 stsp
1604 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1605 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n",
1606 417a6e49 2020-09-26 stsp got_ref_get_name(symref),
1607 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1608 417a6e49 2020-09-26 stsp } else {
1609 417a6e49 2020-09-26 stsp symref_is_locked = 1;
1610 417a6e49 2020-09-26 stsp
1611 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1612 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref)) == 0)
1613 417a6e49 2020-09-26 stsp goto done;
1614 417a6e49 2020-09-26 stsp
1615 417a6e49 2020-09-26 stsp err = got_ref_change_symref(symref,
1616 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref));
1617 417a6e49 2020-09-26 stsp if (err)
1618 417a6e49 2020-09-26 stsp goto done;
1619 417a6e49 2020-09-26 stsp
1620 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1621 417a6e49 2020-09-26 stsp if (err)
1622 417a6e49 2020-09-26 stsp goto done;
1623 417a6e49 2020-09-26 stsp
1624 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1625 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1626 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1627 417a6e49 2020-09-26 stsp
1628 417a6e49 2020-09-26 stsp }
1629 417a6e49 2020-09-26 stsp done:
1630 417a6e49 2020-09-26 stsp if (symref_is_locked) {
1631 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(symref);
1632 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1633 417a6e49 2020-09-26 stsp err = unlock_err;
1634 417a6e49 2020-09-26 stsp }
1635 417a6e49 2020-09-26 stsp got_ref_close(symref);
1636 417a6e49 2020-09-26 stsp return err;
1637 417a6e49 2020-09-26 stsp }
1638 417a6e49 2020-09-26 stsp
1639 417a6e49 2020-09-26 stsp __dead static void
1640 417a6e49 2020-09-26 stsp usage_fetch(void)
1641 417a6e49 2020-09-26 stsp {
1642 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1643 417a6e49 2020-09-26 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1644 417a6e49 2020-09-26 stsp "[remote-repository-name]\n",
1645 417a6e49 2020-09-26 stsp getprogname());
1646 417a6e49 2020-09-26 stsp exit(1);
1647 417a6e49 2020-09-26 stsp }
1648 417a6e49 2020-09-26 stsp
1649 417a6e49 2020-09-26 stsp static const struct got_error *
1650 417a6e49 2020-09-26 stsp delete_missing_ref(struct got_reference *ref,
1651 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1652 417a6e49 2020-09-26 stsp {
1653 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1654 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
1655 417a6e49 2020-09-26 stsp char *id_str = NULL;
1656 417a6e49 2020-09-26 stsp
1657 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1658 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1659 417a6e49 2020-09-26 stsp if (err)
1660 417a6e49 2020-09-26 stsp return err;
1661 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1662 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1663 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1664 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1665 417a6e49 2020-09-26 stsp }
1666 417a6e49 2020-09-26 stsp } else {
1667 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
1668 417a6e49 2020-09-26 stsp if (err)
1669 417a6e49 2020-09-26 stsp return err;
1670 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
1671 417a6e49 2020-09-26 stsp if (err)
1672 417a6e49 2020-09-26 stsp goto done;
1673 417a6e49 2020-09-26 stsp
1674 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1675 417a6e49 2020-09-26 stsp if (err)
1676 417a6e49 2020-09-26 stsp goto done;
1677 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1678 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1679 417a6e49 2020-09-26 stsp got_ref_get_name(ref), id_str);
1680 417a6e49 2020-09-26 stsp }
1681 417a6e49 2020-09-26 stsp }
1682 417a6e49 2020-09-26 stsp done:
1683 417a6e49 2020-09-26 stsp free(id);
1684 417a6e49 2020-09-26 stsp free(id_str);
1685 417a6e49 2020-09-26 stsp return NULL;
1686 417a6e49 2020-09-26 stsp }
1687 417a6e49 2020-09-26 stsp
1688 417a6e49 2020-09-26 stsp static const struct got_error *
1689 417a6e49 2020-09-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1690 417a6e49 2020-09-26 stsp struct got_pathlist_head *their_symrefs,
1691 417a6e49 2020-09-26 stsp const struct got_remote_repo *remote,
1692 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1693 417a6e49 2020-09-26 stsp {
1694 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1695 417a6e49 2020-09-26 stsp struct got_reflist_head my_refs;
1696 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
1697 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1698 417a6e49 2020-09-26 stsp char *remote_namespace = NULL;
1699 417a6e49 2020-09-26 stsp char *local_refname = NULL;
1700 417a6e49 2020-09-26 stsp
1701 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&my_refs);
1702 417a6e49 2020-09-26 stsp
1703 417a6e49 2020-09-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1704 417a6e49 2020-09-26 stsp == -1)
1705 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1706 417a6e49 2020-09-26 stsp
1707 417a6e49 2020-09-26 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1708 417a6e49 2020-09-26 stsp if (err)
1709 417a6e49 2020-09-26 stsp goto done;
1710 417a6e49 2020-09-26 stsp
1711 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1712 417a6e49 2020-09-26 stsp const char *refname = got_ref_get_name(re->ref);
1713 417a6e49 2020-09-26 stsp
1714 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
1715 417a6e49 2020-09-26 stsp if (strncmp(refname, remote_namespace,
1716 417a6e49 2020-09-26 stsp strlen(remote_namespace)) == 0) {
1717 417a6e49 2020-09-26 stsp if (strcmp(refname + strlen(remote_namespace),
1718 417a6e49 2020-09-26 stsp GOT_REF_HEAD) == 0)
1719 417a6e49 2020-09-26 stsp continue;
1720 417a6e49 2020-09-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1721 417a6e49 2020-09-26 stsp refname + strlen(remote_namespace)) == -1) {
1722 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
1723 417a6e49 2020-09-26 stsp goto done;
1724 417a6e49 2020-09-26 stsp }
1725 417a6e49 2020-09-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1726 417a6e49 2020-09-26 stsp continue;
1727 417a6e49 2020-09-26 stsp }
1728 417a6e49 2020-09-26 stsp
1729 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1730 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1731 417a6e49 2020-09-26 stsp break;
1732 417a6e49 2020-09-26 stsp }
1733 417a6e49 2020-09-26 stsp if (pe != NULL)
1734 417a6e49 2020-09-26 stsp continue;
1735 417a6e49 2020-09-26 stsp
1736 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1737 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1738 417a6e49 2020-09-26 stsp break;
1739 417a6e49 2020-09-26 stsp }
1740 417a6e49 2020-09-26 stsp if (pe != NULL)
1741 417a6e49 2020-09-26 stsp continue;
1742 417a6e49 2020-09-26 stsp
1743 417a6e49 2020-09-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1744 417a6e49 2020-09-26 stsp if (err)
1745 417a6e49 2020-09-26 stsp break;
1746 417a6e49 2020-09-26 stsp
1747 417a6e49 2020-09-26 stsp if (local_refname) {
1748 417a6e49 2020-09-26 stsp struct got_reference *ref;
1749 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1750 417a6e49 2020-09-26 stsp if (err) {
1751 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1752 417a6e49 2020-09-26 stsp break;
1753 417a6e49 2020-09-26 stsp free(local_refname);
1754 417a6e49 2020-09-26 stsp local_refname = NULL;
1755 417a6e49 2020-09-26 stsp continue;
1756 417a6e49 2020-09-26 stsp }
1757 417a6e49 2020-09-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1758 417a6e49 2020-09-26 stsp if (err)
1759 417a6e49 2020-09-26 stsp break;
1760 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1761 417a6e49 2020-09-26 stsp got_ref_close(ref);
1762 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL) {
1763 417a6e49 2020-09-26 stsp err = unlock_err;
1764 417a6e49 2020-09-26 stsp break;
1765 417a6e49 2020-09-26 stsp }
1766 417a6e49 2020-09-26 stsp
1767 417a6e49 2020-09-26 stsp free(local_refname);
1768 417a6e49 2020-09-26 stsp local_refname = NULL;
1769 417a6e49 2020-09-26 stsp }
1770 417a6e49 2020-09-26 stsp }
1771 417a6e49 2020-09-26 stsp done:
1772 417a6e49 2020-09-26 stsp free(remote_namespace);
1773 417a6e49 2020-09-26 stsp free(local_refname);
1774 417a6e49 2020-09-26 stsp return err;
1775 417a6e49 2020-09-26 stsp }
1776 417a6e49 2020-09-26 stsp
1777 417a6e49 2020-09-26 stsp static const struct got_error *
1778 417a6e49 2020-09-26 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1779 417a6e49 2020-09-26 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1780 417a6e49 2020-09-26 stsp {
1781 417a6e49 2020-09-26 stsp const struct got_error *err, *unlock_err;
1782 417a6e49 2020-09-26 stsp char *remote_refname;
1783 417a6e49 2020-09-26 stsp struct got_reference *ref;
1784 417a6e49 2020-09-26 stsp
1785 417a6e49 2020-09-26 stsp if (strncmp("refs/", refname, 5) == 0)
1786 417a6e49 2020-09-26 stsp refname += 5;
1787 417a6e49 2020-09-26 stsp
1788 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1789 417a6e49 2020-09-26 stsp remote_repo_name, refname) == -1)
1790 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1791 417a6e49 2020-09-26 stsp
1792 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1793 417a6e49 2020-09-26 stsp if (err) {
1794 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1795 417a6e49 2020-09-26 stsp goto done;
1796 417a6e49 2020-09-26 stsp err = create_ref(remote_refname, id, verbosity, repo);
1797 417a6e49 2020-09-26 stsp } else {
1798 417a6e49 2020-09-26 stsp err = update_ref(ref, id, 0, verbosity, repo);
1799 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1800 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1801 417a6e49 2020-09-26 stsp err = unlock_err;
1802 417a6e49 2020-09-26 stsp got_ref_close(ref);
1803 417a6e49 2020-09-26 stsp }
1804 417a6e49 2020-09-26 stsp done:
1805 417a6e49 2020-09-26 stsp free(remote_refname);
1806 417a6e49 2020-09-26 stsp return err;
1807 417a6e49 2020-09-26 stsp }
1808 417a6e49 2020-09-26 stsp
1809 417a6e49 2020-09-26 stsp static const struct got_error *
1810 417a6e49 2020-09-26 stsp cmd_fetch(int argc, char *argv[])
1811 417a6e49 2020-09-26 stsp {
1812 417a6e49 2020-09-26 stsp const struct got_error *error = NULL, *unlock_err;
1813 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
1814 417a6e49 2020-09-26 stsp const char *remote_name;
1815 417a6e49 2020-09-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
1816 417a6e49 2020-09-26 stsp char *repo_name = NULL, *server_path = NULL;
1817 417a6e49 2020-09-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
1818 417a6e49 2020-09-26 stsp int nremotes;
1819 417a6e49 2020-09-26 stsp char *id_str = NULL;
1820 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
1821 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
1822 417a6e49 2020-09-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1823 417a6e49 2020-09-26 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1824 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1825 417a6e49 2020-09-26 stsp struct got_object_id *pack_hash = NULL;
1826 417a6e49 2020-09-26 stsp int i, ch, fetchfd = -1, fetchstatus;
1827 417a6e49 2020-09-26 stsp pid_t fetchpid = -1;
1828 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg fpa;
1829 417a6e49 2020-09-26 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1830 417a6e49 2020-09-26 stsp int delete_refs = 0, replace_tags = 0;
1831 417a6e49 2020-09-26 stsp
1832 417a6e49 2020-09-26 stsp TAILQ_INIT(&refs);
1833 417a6e49 2020-09-26 stsp TAILQ_INIT(&symrefs);
1834 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_branches);
1835 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_refs);
1836 417a6e49 2020-09-26 stsp
1837 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1838 417a6e49 2020-09-26 stsp switch (ch) {
1839 417a6e49 2020-09-26 stsp case 'a':
1840 417a6e49 2020-09-26 stsp fetch_all_branches = 1;
1841 417a6e49 2020-09-26 stsp break;
1842 417a6e49 2020-09-26 stsp case 'b':
1843 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_branches,
1844 417a6e49 2020-09-26 stsp optarg, NULL);
1845 417a6e49 2020-09-26 stsp if (error)
1846 417a6e49 2020-09-26 stsp return error;
1847 417a6e49 2020-09-26 stsp break;
1848 417a6e49 2020-09-26 stsp case 'd':
1849 417a6e49 2020-09-26 stsp delete_refs = 1;
1850 417a6e49 2020-09-26 stsp break;
1851 417a6e49 2020-09-26 stsp case 'l':
1852 417a6e49 2020-09-26 stsp list_refs_only = 1;
1853 417a6e49 2020-09-26 stsp break;
1854 417a6e49 2020-09-26 stsp case 'r':
1855 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
1856 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1857 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
1858 417a6e49 2020-09-26 stsp optarg);
1859 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
1860 417a6e49 2020-09-26 stsp break;
1861 417a6e49 2020-09-26 stsp case 't':
1862 417a6e49 2020-09-26 stsp replace_tags = 1;
1863 417a6e49 2020-09-26 stsp break;
1864 417a6e49 2020-09-26 stsp case 'v':
1865 417a6e49 2020-09-26 stsp if (verbosity < 0)
1866 417a6e49 2020-09-26 stsp verbosity = 0;
1867 417a6e49 2020-09-26 stsp else if (verbosity < 3)
1868 417a6e49 2020-09-26 stsp verbosity++;
1869 417a6e49 2020-09-26 stsp break;
1870 417a6e49 2020-09-26 stsp case 'q':
1871 417a6e49 2020-09-26 stsp verbosity = -1;
1872 417a6e49 2020-09-26 stsp break;
1873 417a6e49 2020-09-26 stsp case 'R':
1874 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_refs,
1875 417a6e49 2020-09-26 stsp optarg, NULL);
1876 417a6e49 2020-09-26 stsp if (error)
1877 417a6e49 2020-09-26 stsp return error;
1878 417a6e49 2020-09-26 stsp break;
1879 417a6e49 2020-09-26 stsp default:
1880 417a6e49 2020-09-26 stsp usage_fetch();
1881 417a6e49 2020-09-26 stsp break;
1882 417a6e49 2020-09-26 stsp }
1883 417a6e49 2020-09-26 stsp }
1884 417a6e49 2020-09-26 stsp argc -= optind;
1885 417a6e49 2020-09-26 stsp argv += optind;
1886 417a6e49 2020-09-26 stsp
1887 417a6e49 2020-09-26 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1888 417a6e49 2020-09-26 stsp errx(1, "-a and -b options are mutually exclusive");
1889 417a6e49 2020-09-26 stsp if (list_refs_only) {
1890 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_branches))
1891 417a6e49 2020-09-26 stsp errx(1, "-l and -b options are mutually exclusive");
1892 417a6e49 2020-09-26 stsp if (fetch_all_branches)
1893 417a6e49 2020-09-26 stsp errx(1, "-l and -a options are mutually exclusive");
1894 417a6e49 2020-09-26 stsp if (delete_refs)
1895 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
1896 417a6e49 2020-09-26 stsp if (verbosity == -1)
1897 417a6e49 2020-09-26 stsp errx(1, "-l and -q options are mutually exclusive");
1898 417a6e49 2020-09-26 stsp }
1899 417a6e49 2020-09-26 stsp
1900 417a6e49 2020-09-26 stsp if (argc == 0)
1901 417a6e49 2020-09-26 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1902 417a6e49 2020-09-26 stsp else if (argc == 1)
1903 417a6e49 2020-09-26 stsp remote_name = argv[0];
1904 417a6e49 2020-09-26 stsp else
1905 417a6e49 2020-09-26 stsp usage_fetch();
1906 417a6e49 2020-09-26 stsp
1907 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
1908 417a6e49 2020-09-26 stsp if (cwd == NULL) {
1909 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
1910 417a6e49 2020-09-26 stsp goto done;
1911 417a6e49 2020-09-26 stsp }
1912 417a6e49 2020-09-26 stsp
1913 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1914 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
1915 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1916 417a6e49 2020-09-26 stsp goto done;
1917 417a6e49 2020-09-26 stsp else
1918 417a6e49 2020-09-26 stsp error = NULL;
1919 417a6e49 2020-09-26 stsp if (worktree) {
1920 417a6e49 2020-09-26 stsp repo_path =
1921 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
1922 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1923 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1924 417a6e49 2020-09-26 stsp if (error)
1925 417a6e49 2020-09-26 stsp goto done;
1926 417a6e49 2020-09-26 stsp } else {
1927 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
1928 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1929 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1930 417a6e49 2020-09-26 stsp goto done;
1931 417a6e49 2020-09-26 stsp }
1932 417a6e49 2020-09-26 stsp }
1933 417a6e49 2020-09-26 stsp }
1934 417a6e49 2020-09-26 stsp
1935 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
1936 417a6e49 2020-09-26 stsp if (error)
1937 417a6e49 2020-09-26 stsp goto done;
1938 417a6e49 2020-09-26 stsp
1939 417a6e49 2020-09-26 stsp if (worktree) {
1940 417a6e49 2020-09-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1941 417a6e49 2020-09-26 stsp if (worktree_conf) {
1942 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1943 417a6e49 2020-09-26 stsp worktree_conf);
1944 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1945 417a6e49 2020-09-26 stsp remote = &remotes[i];
1946 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1947 417a6e49 2020-09-26 stsp break;
1948 417a6e49 2020-09-26 stsp }
1949 417a6e49 2020-09-26 stsp }
1950 417a6e49 2020-09-26 stsp }
1951 417a6e49 2020-09-26 stsp if (remote == NULL) {
1952 417a6e49 2020-09-26 stsp repo_conf = got_repo_get_gotconfig(repo);
1953 417a6e49 2020-09-26 stsp if (repo_conf) {
1954 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1955 417a6e49 2020-09-26 stsp repo_conf);
1956 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1957 417a6e49 2020-09-26 stsp remote = &remotes[i];
1958 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1959 417a6e49 2020-09-26 stsp break;
1960 417a6e49 2020-09-26 stsp }
1961 417a6e49 2020-09-26 stsp }
1962 417a6e49 2020-09-26 stsp }
1963 417a6e49 2020-09-26 stsp if (remote == NULL) {
1964 417a6e49 2020-09-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1965 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1966 417a6e49 2020-09-26 stsp remote = &remotes[i];
1967 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1968 417a6e49 2020-09-26 stsp break;
1969 417a6e49 2020-09-26 stsp }
1970 417a6e49 2020-09-26 stsp }
1971 417a6e49 2020-09-26 stsp if (remote == NULL) {
1972 417a6e49 2020-09-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1973 417a6e49 2020-09-26 stsp goto done;
1974 417a6e49 2020-09-26 stsp }
1975 417a6e49 2020-09-26 stsp
1976 417a6e49 2020-09-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1977 417a6e49 2020-09-26 stsp &repo_name, remote->url);
1978 417a6e49 2020-09-26 stsp if (error)
1979 417a6e49 2020-09-26 stsp goto done;
1980 417a6e49 2020-09-26 stsp
1981 417a6e49 2020-09-26 stsp if (strcmp(proto, "git") == 0) {
1982 417a6e49 2020-09-26 stsp #ifndef PROFILE
1983 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1984 417a6e49 2020-09-26 stsp "sendfd dns inet unveil", NULL) == -1)
1985 417a6e49 2020-09-26 stsp err(1, "pledge");
1986 417a6e49 2020-09-26 stsp #endif
1987 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1988 417a6e49 2020-09-26 stsp strcmp(proto, "ssh") == 0) {
1989 417a6e49 2020-09-26 stsp #ifndef PROFILE
1990 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1991 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
1992 417a6e49 2020-09-26 stsp err(1, "pledge");
1993 417a6e49 2020-09-26 stsp #endif
1994 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "http") == 0 ||
1995 417a6e49 2020-09-26 stsp strcmp(proto, "git+http") == 0) {
1996 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1997 417a6e49 2020-09-26 stsp goto done;
1998 417a6e49 2020-09-26 stsp } else {
1999 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2000 417a6e49 2020-09-26 stsp goto done;
2001 417a6e49 2020-09-26 stsp }
2002 417a6e49 2020-09-26 stsp
2003 417a6e49 2020-09-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2004 417a6e49 2020-09-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2005 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil",
2006 417a6e49 2020-09-26 stsp GOT_FETCH_PATH_SSH);
2007 417a6e49 2020-09-26 stsp goto done;
2008 417a6e49 2020-09-26 stsp }
2009 417a6e49 2020-09-26 stsp }
2010 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2011 417a6e49 2020-09-26 stsp if (error)
2012 417a6e49 2020-09-26 stsp goto done;
2013 417a6e49 2020-09-26 stsp
2014 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2015 417a6e49 2020-09-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2016 417a6e49 2020-09-26 stsp port ? ":" : "", port ? port : "");
2017 417a6e49 2020-09-26 stsp
2018 417a6e49 2020-09-26 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2019 417a6e49 2020-09-26 stsp server_path, verbosity);
2020 417a6e49 2020-09-26 stsp if (error)
2021 417a6e49 2020-09-26 stsp goto done;
2022 417a6e49 2020-09-26 stsp
2023 417a6e49 2020-09-26 stsp fpa.last_scaled_size[0] = '\0';
2024 417a6e49 2020-09-26 stsp fpa.last_p_indexed = -1;
2025 417a6e49 2020-09-26 stsp fpa.last_p_resolved = -1;
2026 417a6e49 2020-09-26 stsp fpa.verbosity = verbosity;
2027 417a6e49 2020-09-26 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2028 417a6e49 2020-09-26 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2029 417a6e49 2020-09-26 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2030 417a6e49 2020-09-26 stsp fetch_progress, &fpa);
2031 417a6e49 2020-09-26 stsp if (error)
2032 417a6e49 2020-09-26 stsp goto done;
2033 417a6e49 2020-09-26 stsp
2034 417a6e49 2020-09-26 stsp if (list_refs_only) {
2035 417a6e49 2020-09-26 stsp error = list_remote_refs(&symrefs, &refs);
2036 417a6e49 2020-09-26 stsp goto done;
2037 417a6e49 2020-09-26 stsp }
2038 417a6e49 2020-09-26 stsp
2039 417a6e49 2020-09-26 stsp if (pack_hash == NULL) {
2040 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2041 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2042 417a6e49 2020-09-26 stsp } else if (verbosity >= 0) {
2043 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, pack_hash);
2044 417a6e49 2020-09-26 stsp if (error)
2045 417a6e49 2020-09-26 stsp goto done;
2046 417a6e49 2020-09-26 stsp printf("\nFetched %s.pack\n", id_str);
2047 417a6e49 2020-09-26 stsp free(id_str);
2048 417a6e49 2020-09-26 stsp id_str = NULL;
2049 417a6e49 2020-09-26 stsp }
2050 417a6e49 2020-09-26 stsp
2051 417a6e49 2020-09-26 stsp /* Update references provided with the pack file. */
2052 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2053 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2054 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
2055 417a6e49 2020-09-26 stsp struct got_reference *ref;
2056 417a6e49 2020-09-26 stsp char *remote_refname;
2057 417a6e49 2020-09-26 stsp
2058 417a6e49 2020-09-26 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2059 417a6e49 2020-09-26 stsp !remote->mirror_references) {
2060 417a6e49 2020-09-26 stsp error = update_wanted_ref(refname, id,
2061 417a6e49 2020-09-26 stsp remote->name, verbosity, repo);
2062 417a6e49 2020-09-26 stsp if (error)
2063 417a6e49 2020-09-26 stsp goto done;
2064 417a6e49 2020-09-26 stsp continue;
2065 417a6e49 2020-09-26 stsp }
2066 417a6e49 2020-09-26 stsp
2067 417a6e49 2020-09-26 stsp if (remote->mirror_references ||
2068 417a6e49 2020-09-26 stsp strncmp("refs/tags/", refname, 10) == 0) {
2069 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2070 417a6e49 2020-09-26 stsp if (error) {
2071 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2072 417a6e49 2020-09-26 stsp goto done;
2073 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2074 417a6e49 2020-09-26 stsp repo);
2075 417a6e49 2020-09-26 stsp if (error)
2076 417a6e49 2020-09-26 stsp goto done;
2077 417a6e49 2020-09-26 stsp } else {
2078 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2079 417a6e49 2020-09-26 stsp verbosity, repo);
2080 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2081 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2082 417a6e49 2020-09-26 stsp error = unlock_err;
2083 417a6e49 2020-09-26 stsp got_ref_close(ref);
2084 417a6e49 2020-09-26 stsp if (error)
2085 417a6e49 2020-09-26 stsp goto done;
2086 417a6e49 2020-09-26 stsp }
2087 417a6e49 2020-09-26 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2088 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2089 417a6e49 2020-09-26 stsp remote_name, refname + 11) == -1) {
2090 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2091 417a6e49 2020-09-26 stsp goto done;
2092 417a6e49 2020-09-26 stsp }
2093 417a6e49 2020-09-26 stsp
2094 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2095 417a6e49 2020-09-26 stsp if (error) {
2096 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2097 417a6e49 2020-09-26 stsp goto done;
2098 417a6e49 2020-09-26 stsp error = create_ref(remote_refname, id,
2099 417a6e49 2020-09-26 stsp verbosity, repo);
2100 417a6e49 2020-09-26 stsp if (error)
2101 417a6e49 2020-09-26 stsp goto done;
2102 417a6e49 2020-09-26 stsp } else {
2103 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2104 417a6e49 2020-09-26 stsp verbosity, repo);
2105 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2106 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2107 417a6e49 2020-09-26 stsp error = unlock_err;
2108 417a6e49 2020-09-26 stsp got_ref_close(ref);
2109 417a6e49 2020-09-26 stsp if (error)
2110 417a6e49 2020-09-26 stsp goto done;
2111 417a6e49 2020-09-26 stsp }
2112 417a6e49 2020-09-26 stsp
2113 417a6e49 2020-09-26 stsp /* Also create a local branch if none exists yet. */
2114 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2115 417a6e49 2020-09-26 stsp if (error) {
2116 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2117 417a6e49 2020-09-26 stsp goto done;
2118 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2119 417a6e49 2020-09-26 stsp repo);
2120 417a6e49 2020-09-26 stsp if (error)
2121 417a6e49 2020-09-26 stsp goto done;
2122 417a6e49 2020-09-26 stsp } else {
2123 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2124 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2125 417a6e49 2020-09-26 stsp error = unlock_err;
2126 417a6e49 2020-09-26 stsp got_ref_close(ref);
2127 417a6e49 2020-09-26 stsp }
2128 417a6e49 2020-09-26 stsp }
2129 417a6e49 2020-09-26 stsp }
2130 417a6e49 2020-09-26 stsp if (delete_refs) {
2131 417a6e49 2020-09-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2132 417a6e49 2020-09-26 stsp verbosity, repo);
2133 417a6e49 2020-09-26 stsp if (error)
2134 417a6e49 2020-09-26 stsp goto done;
2135 417a6e49 2020-09-26 stsp }
2136 417a6e49 2020-09-26 stsp
2137 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
2138 417a6e49 2020-09-26 stsp /* Update remote HEAD reference if the server provided one. */
2139 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2140 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
2141 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2142 417a6e49 2020-09-26 stsp const char *target = pe->data;
2143 417a6e49 2020-09-26 stsp char *remote_refname = NULL, *remote_target = NULL;
2144 417a6e49 2020-09-26 stsp
2145 417a6e49 2020-09-26 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2146 417a6e49 2020-09-26 stsp continue;
2147 417a6e49 2020-09-26 stsp
2148 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", target, 11) != 0)
2149 417a6e49 2020-09-26 stsp continue;
2150 417a6e49 2020-09-26 stsp
2151 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2152 417a6e49 2020-09-26 stsp remote->name, refname) == -1) {
2153 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2154 417a6e49 2020-09-26 stsp goto done;
2155 417a6e49 2020-09-26 stsp }
2156 417a6e49 2020-09-26 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2157 417a6e49 2020-09-26 stsp remote->name, target + 11) == -1) {
2158 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2159 417a6e49 2020-09-26 stsp free(remote_refname);
2160 417a6e49 2020-09-26 stsp goto done;
2161 417a6e49 2020-09-26 stsp }
2162 417a6e49 2020-09-26 stsp
2163 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, remote_target,
2164 417a6e49 2020-09-26 stsp 0);
2165 417a6e49 2020-09-26 stsp if (error) {
2166 417a6e49 2020-09-26 stsp free(remote_refname);
2167 417a6e49 2020-09-26 stsp free(remote_target);
2168 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
2169 417a6e49 2020-09-26 stsp error = NULL;
2170 417a6e49 2020-09-26 stsp continue;
2171 417a6e49 2020-09-26 stsp }
2172 417a6e49 2020-09-26 stsp goto done;
2173 417a6e49 2020-09-26 stsp }
2174 417a6e49 2020-09-26 stsp error = update_symref(remote_refname, target_ref,
2175 417a6e49 2020-09-26 stsp verbosity, repo);
2176 417a6e49 2020-09-26 stsp free(remote_refname);
2177 417a6e49 2020-09-26 stsp free(remote_target);
2178 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
2179 417a6e49 2020-09-26 stsp if (error)
2180 417a6e49 2020-09-26 stsp goto done;
2181 417a6e49 2020-09-26 stsp }
2182 417a6e49 2020-09-26 stsp }
2183 417a6e49 2020-09-26 stsp done:
2184 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
2185 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
2186 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
2187 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2188 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
2189 417a6e49 2020-09-26 stsp }
2190 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2191 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
2192 417a6e49 2020-09-26 stsp if (repo)
2193 417a6e49 2020-09-26 stsp got_repo_close(repo);
2194 417a6e49 2020-09-26 stsp if (worktree)
2195 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
2196 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2197 417a6e49 2020-09-26 stsp free((void *)pe->path);
2198 417a6e49 2020-09-26 stsp free(pe->data);
2199 417a6e49 2020-09-26 stsp }
2200 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
2201 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2202 417a6e49 2020-09-26 stsp free((void *)pe->path);
2203 417a6e49 2020-09-26 stsp free(pe->data);
2204 417a6e49 2020-09-26 stsp }
2205 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
2206 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
2207 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
2208 417a6e49 2020-09-26 stsp free(id_str);
2209 417a6e49 2020-09-26 stsp free(cwd);
2210 417a6e49 2020-09-26 stsp free(repo_path);
2211 417a6e49 2020-09-26 stsp free(pack_hash);
2212 417a6e49 2020-09-26 stsp free(proto);
2213 417a6e49 2020-09-26 stsp free(host);
2214 417a6e49 2020-09-26 stsp free(port);
2215 417a6e49 2020-09-26 stsp free(server_path);
2216 417a6e49 2020-09-26 stsp free(repo_name);
2217 417a6e49 2020-09-26 stsp return error;
2218 417a6e49 2020-09-26 stsp }
2219 417a6e49 2020-09-26 stsp
2220 417a6e49 2020-09-26 stsp
2221 417a6e49 2020-09-26 stsp __dead static void
2222 417a6e49 2020-09-26 stsp usage_checkout(void)
2223 417a6e49 2020-09-26 stsp {
2224 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2225 417a6e49 2020-09-26 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2226 417a6e49 2020-09-26 stsp exit(1);
2227 417a6e49 2020-09-26 stsp }
2228 417a6e49 2020-09-26 stsp
2229 417a6e49 2020-09-26 stsp static void
2230 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning(void)
2231 417a6e49 2020-09-26 stsp {
2232 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: warning: could not create a reference "
2233 417a6e49 2020-09-26 stsp "to the work tree's base commit; the commit could be "
2234 417a6e49 2020-09-26 stsp "garbage-collected by Git; making the repository "
2235 417a6e49 2020-09-26 stsp "writable and running 'got update' will prevent this\n",
2236 417a6e49 2020-09-26 stsp getprogname());
2237 417a6e49 2020-09-26 stsp }
2238 417a6e49 2020-09-26 stsp
2239 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg {
2240 417a6e49 2020-09-26 stsp const char *worktree_path;
2241 417a6e49 2020-09-26 stsp int had_base_commit_ref_error;
2242 417a6e49 2020-09-26 stsp };
2243 417a6e49 2020-09-26 stsp
2244 417a6e49 2020-09-26 stsp static const struct got_error *
2245 417a6e49 2020-09-26 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2246 417a6e49 2020-09-26 stsp {
2247 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg *a = arg;
2248 417a6e49 2020-09-26 stsp
2249 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2250 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2251 417a6e49 2020-09-26 stsp return NULL;
2252 417a6e49 2020-09-26 stsp
2253 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2254 417a6e49 2020-09-26 stsp a->had_base_commit_ref_error = 1;
2255 417a6e49 2020-09-26 stsp return NULL;
2256 417a6e49 2020-09-26 stsp }
2257 417a6e49 2020-09-26 stsp
2258 417a6e49 2020-09-26 stsp while (path[0] == '/')
2259 417a6e49 2020-09-26 stsp path++;
2260 417a6e49 2020-09-26 stsp
2261 417a6e49 2020-09-26 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2262 417a6e49 2020-09-26 stsp return NULL;
2263 417a6e49 2020-09-26 stsp }
2264 417a6e49 2020-09-26 stsp
2265 417a6e49 2020-09-26 stsp static const struct got_error *
2266 417a6e49 2020-09-26 stsp check_cancelled(void *arg)
2267 417a6e49 2020-09-26 stsp {
2268 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
2269 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
2270 417a6e49 2020-09-26 stsp return NULL;
2271 417a6e49 2020-09-26 stsp }
2272 417a6e49 2020-09-26 stsp
2273 417a6e49 2020-09-26 stsp static const struct got_error *
2274 417a6e49 2020-09-26 stsp check_linear_ancestry(struct got_object_id *commit_id,
2275 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2276 417a6e49 2020-09-26 stsp struct got_repository *repo)
2277 417a6e49 2020-09-26 stsp {
2278 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2279 417a6e49 2020-09-26 stsp struct got_object_id *yca_id;
2280 417a6e49 2020-09-26 stsp
2281 417a6e49 2020-09-26 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2282 417a6e49 2020-09-26 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2283 417a6e49 2020-09-26 stsp if (err)
2284 417a6e49 2020-09-26 stsp return err;
2285 417a6e49 2020-09-26 stsp
2286 417a6e49 2020-09-26 stsp if (yca_id == NULL)
2287 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2288 417a6e49 2020-09-26 stsp
2289 417a6e49 2020-09-26 stsp /*
2290 417a6e49 2020-09-26 stsp * Require a straight line of history between the target commit
2291 417a6e49 2020-09-26 stsp * and the work tree's base commit.
2292 417a6e49 2020-09-26 stsp *
2293 417a6e49 2020-09-26 stsp * Non-linear situations such as this require a rebase:
2294 417a6e49 2020-09-26 stsp *
2295 417a6e49 2020-09-26 stsp * (commit) D F (base_commit)
2296 417a6e49 2020-09-26 stsp * \ /
2297 417a6e49 2020-09-26 stsp * C E
2298 417a6e49 2020-09-26 stsp * \ /
2299 417a6e49 2020-09-26 stsp * B (yca)
2300 417a6e49 2020-09-26 stsp * |
2301 417a6e49 2020-09-26 stsp * A
2302 417a6e49 2020-09-26 stsp *
2303 417a6e49 2020-09-26 stsp * 'got update' only handles linear cases:
2304 417a6e49 2020-09-26 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2305 417a6e49 2020-09-26 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2306 417a6e49 2020-09-26 stsp */
2307 417a6e49 2020-09-26 stsp if (allow_forwards_in_time_only) {
2308 417a6e49 2020-09-26 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2309 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2310 417a6e49 2020-09-26 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2311 417a6e49 2020-09-26 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2312 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2313 417a6e49 2020-09-26 stsp
2314 417a6e49 2020-09-26 stsp free(yca_id);
2315 417a6e49 2020-09-26 stsp return NULL;
2316 417a6e49 2020-09-26 stsp }
2317 417a6e49 2020-09-26 stsp
2318 417a6e49 2020-09-26 stsp static const struct got_error *
2319 417a6e49 2020-09-26 stsp check_same_branch(struct got_object_id *commit_id,
2320 417a6e49 2020-09-26 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2321 417a6e49 2020-09-26 stsp struct got_repository *repo)
2322 417a6e49 2020-09-26 stsp {
2323 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2324 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
2325 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
2326 417a6e49 2020-09-26 stsp int is_same_branch = 0;
2327 417a6e49 2020-09-26 stsp
2328 417a6e49 2020-09-26 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2329 417a6e49 2020-09-26 stsp if (err)
2330 417a6e49 2020-09-26 stsp goto done;
2331 417a6e49 2020-09-26 stsp
2332 417a6e49 2020-09-26 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2333 417a6e49 2020-09-26 stsp is_same_branch = 1;
2334 417a6e49 2020-09-26 stsp goto done;
2335 417a6e49 2020-09-26 stsp }
2336 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2337 417a6e49 2020-09-26 stsp is_same_branch = 1;
2338 417a6e49 2020-09-26 stsp goto done;
2339 417a6e49 2020-09-26 stsp }
2340 417a6e49 2020-09-26 stsp
2341 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
2342 417a6e49 2020-09-26 stsp if (err)
2343 417a6e49 2020-09-26 stsp goto done;
2344 417a6e49 2020-09-26 stsp
2345 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2346 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2347 417a6e49 2020-09-26 stsp if (err)
2348 417a6e49 2020-09-26 stsp goto done;
2349 417a6e49 2020-09-26 stsp
2350 417a6e49 2020-09-26 stsp for (;;) {
2351 417a6e49 2020-09-26 stsp struct got_object_id *id;
2352 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2353 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2354 417a6e49 2020-09-26 stsp if (err) {
2355 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2356 417a6e49 2020-09-26 stsp err = NULL;
2357 417a6e49 2020-09-26 stsp break;
2358 417a6e49 2020-09-26 stsp }
2359 417a6e49 2020-09-26 stsp
2360 417a6e49 2020-09-26 stsp if (id) {
2361 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2362 417a6e49 2020-09-26 stsp break;
2363 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2364 417a6e49 2020-09-26 stsp is_same_branch = 1;
2365 417a6e49 2020-09-26 stsp break;
2366 417a6e49 2020-09-26 stsp }
2367 417a6e49 2020-09-26 stsp }
2368 417a6e49 2020-09-26 stsp }
2369 417a6e49 2020-09-26 stsp done:
2370 417a6e49 2020-09-26 stsp if (graph)
2371 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
2372 417a6e49 2020-09-26 stsp free(head_commit_id);
2373 417a6e49 2020-09-26 stsp if (!err && !is_same_branch)
2374 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_ANCESTRY);
2375 417a6e49 2020-09-26 stsp return err;
2376 417a6e49 2020-09-26 stsp }
2377 417a6e49 2020-09-26 stsp
2378 417a6e49 2020-09-26 stsp static const struct got_error *
2379 417a6e49 2020-09-26 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2380 417a6e49 2020-09-26 stsp {
2381 417a6e49 2020-09-26 stsp static char msg[512];
2382 417a6e49 2020-09-26 stsp const char *branch_name;
2383 417a6e49 2020-09-26 stsp
2384 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref))
2385 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(ref);
2386 417a6e49 2020-09-26 stsp else
2387 417a6e49 2020-09-26 stsp branch_name = got_ref_get_name(ref);
2388 417a6e49 2020-09-26 stsp
2389 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2390 417a6e49 2020-09-26 stsp branch_name += 11;
2391 417a6e49 2020-09-26 stsp
2392 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2393 417a6e49 2020-09-26 stsp "target commit is not contained in branch '%s'; "
2394 417a6e49 2020-09-26 stsp "the branch to use must be specified with -b; "
2395 417a6e49 2020-09-26 stsp "if necessary a new branch can be created for "
2396 417a6e49 2020-09-26 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2397 417a6e49 2020-09-26 stsp branch_name, commit_id_str);
2398 417a6e49 2020-09-26 stsp
2399 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2400 417a6e49 2020-09-26 stsp }
2401 417a6e49 2020-09-26 stsp
2402 417a6e49 2020-09-26 stsp static const struct got_error *
2403 417a6e49 2020-09-26 stsp cmd_checkout(int argc, char *argv[])
2404 417a6e49 2020-09-26 stsp {
2405 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2406 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2407 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2408 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2409 417a6e49 2020-09-26 stsp char *repo_path = NULL;
2410 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2411 417a6e49 2020-09-26 stsp const char *path_prefix = "";
2412 417a6e49 2020-09-26 stsp const char *branch_name = GOT_REF_HEAD;
2413 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2414 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
2415 417a6e49 2020-09-26 stsp int ch, same_path_prefix, allow_nonempty = 0;
2416 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2417 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg cpa;
2418 417a6e49 2020-09-26 stsp
2419 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2420 417a6e49 2020-09-26 stsp
2421 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2422 417a6e49 2020-09-26 stsp switch (ch) {
2423 417a6e49 2020-09-26 stsp case 'b':
2424 417a6e49 2020-09-26 stsp branch_name = optarg;
2425 417a6e49 2020-09-26 stsp break;
2426 417a6e49 2020-09-26 stsp case 'c':
2427 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2428 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2429 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2430 417a6e49 2020-09-26 stsp break;
2431 417a6e49 2020-09-26 stsp case 'E':
2432 417a6e49 2020-09-26 stsp allow_nonempty = 1;
2433 417a6e49 2020-09-26 stsp break;
2434 417a6e49 2020-09-26 stsp case 'p':
2435 417a6e49 2020-09-26 stsp path_prefix = optarg;
2436 417a6e49 2020-09-26 stsp break;
2437 417a6e49 2020-09-26 stsp default:
2438 417a6e49 2020-09-26 stsp usage_checkout();
2439 417a6e49 2020-09-26 stsp /* NOTREACHED */
2440 417a6e49 2020-09-26 stsp }
2441 417a6e49 2020-09-26 stsp }
2442 417a6e49 2020-09-26 stsp
2443 417a6e49 2020-09-26 stsp argc -= optind;
2444 417a6e49 2020-09-26 stsp argv += optind;
2445 417a6e49 2020-09-26 stsp
2446 417a6e49 2020-09-26 stsp #ifndef PROFILE
2447 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2448 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2449 417a6e49 2020-09-26 stsp err(1, "pledge");
2450 417a6e49 2020-09-26 stsp #endif
2451 417a6e49 2020-09-26 stsp if (argc == 1) {
2452 417a6e49 2020-09-26 stsp char *base, *dotgit;
2453 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2454 417a6e49 2020-09-26 stsp if (repo_path == NULL)
2455 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath", argv[0]);
2456 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
2457 417a6e49 2020-09-26 stsp if (cwd == NULL) {
2458 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2459 417a6e49 2020-09-26 stsp goto done;
2460 417a6e49 2020-09-26 stsp }
2461 417a6e49 2020-09-26 stsp if (path_prefix[0])
2462 417a6e49 2020-09-26 stsp path = strdup(path_prefix);
2463 417a6e49 2020-09-26 stsp else
2464 417a6e49 2020-09-26 stsp path = strdup(repo_path);
2465 417a6e49 2020-09-26 stsp if (path == NULL) {
2466 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2467 417a6e49 2020-09-26 stsp goto done;
2468 417a6e49 2020-09-26 stsp }
2469 417a6e49 2020-09-26 stsp base = basename(path);
2470 417a6e49 2020-09-26 stsp if (base == NULL) {
2471 417a6e49 2020-09-26 stsp error = got_error_from_errno2("basename", path);
2472 417a6e49 2020-09-26 stsp goto done;
2473 417a6e49 2020-09-26 stsp }
2474 417a6e49 2020-09-26 stsp dotgit = strstr(base, ".git");
2475 417a6e49 2020-09-26 stsp if (dotgit)
2476 417a6e49 2020-09-26 stsp *dotgit = '\0';
2477 417a6e49 2020-09-26 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2478 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2479 417a6e49 2020-09-26 stsp goto done;
2480 417a6e49 2020-09-26 stsp }
2481 417a6e49 2020-09-26 stsp } else if (argc == 2) {
2482 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2483 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
2484 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath", argv[0]);
2485 417a6e49 2020-09-26 stsp goto done;
2486 417a6e49 2020-09-26 stsp }
2487 417a6e49 2020-09-26 stsp worktree_path = realpath(argv[1], NULL);
2488 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2489 417a6e49 2020-09-26 stsp if (errno != ENOENT) {
2490 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath",
2491 417a6e49 2020-09-26 stsp argv[1]);
2492 417a6e49 2020-09-26 stsp goto done;
2493 417a6e49 2020-09-26 stsp }
2494 417a6e49 2020-09-26 stsp worktree_path = strdup(argv[1]);
2495 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2496 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2497 417a6e49 2020-09-26 stsp goto done;
2498 417a6e49 2020-09-26 stsp }
2499 417a6e49 2020-09-26 stsp }
2500 417a6e49 2020-09-26 stsp } else
2501 417a6e49 2020-09-26 stsp usage_checkout();
2502 417a6e49 2020-09-26 stsp
2503 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
2504 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(worktree_path);
2505 417a6e49 2020-09-26 stsp
2506 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
2507 417a6e49 2020-09-26 stsp if (error != NULL)
2508 417a6e49 2020-09-26 stsp goto done;
2509 417a6e49 2020-09-26 stsp
2510 417a6e49 2020-09-26 stsp /* Pre-create work tree path for unveil(2) */
2511 417a6e49 2020-09-26 stsp error = got_path_mkdir(worktree_path);
2512 417a6e49 2020-09-26 stsp if (error) {
2513 417a6e49 2020-09-26 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2514 417a6e49 2020-09-26 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2515 417a6e49 2020-09-26 stsp goto done;
2516 417a6e49 2020-09-26 stsp if (!allow_nonempty &&
2517 417a6e49 2020-09-26 stsp !got_path_dir_is_empty(worktree_path)) {
2518 417a6e49 2020-09-26 stsp error = got_error_path(worktree_path,
2519 417a6e49 2020-09-26 stsp GOT_ERR_DIR_NOT_EMPTY);
2520 417a6e49 2020-09-26 stsp goto done;
2521 417a6e49 2020-09-26 stsp }
2522 417a6e49 2020-09-26 stsp }
2523 417a6e49 2020-09-26 stsp
2524 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2525 417a6e49 2020-09-26 stsp if (error)
2526 417a6e49 2020-09-26 stsp goto done;
2527 417a6e49 2020-09-26 stsp
2528 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2529 417a6e49 2020-09-26 stsp if (error != NULL)
2530 417a6e49 2020-09-26 stsp goto done;
2531 417a6e49 2020-09-26 stsp
2532 417a6e49 2020-09-26 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2533 417a6e49 2020-09-26 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2534 417a6e49 2020-09-26 stsp goto done;
2535 417a6e49 2020-09-26 stsp
2536 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2537 417a6e49 2020-09-26 stsp if (error != NULL)
2538 417a6e49 2020-09-26 stsp goto done;
2539 417a6e49 2020-09-26 stsp
2540 417a6e49 2020-09-26 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2541 417a6e49 2020-09-26 stsp path_prefix);
2542 417a6e49 2020-09-26 stsp if (error != NULL)
2543 417a6e49 2020-09-26 stsp goto done;
2544 417a6e49 2020-09-26 stsp if (!same_path_prefix) {
2545 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2546 417a6e49 2020-09-26 stsp goto done;
2547 417a6e49 2020-09-26 stsp }
2548 417a6e49 2020-09-26 stsp
2549 417a6e49 2020-09-26 stsp if (commit_id_str) {
2550 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
2551 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2552 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2553 417a6e49 2020-09-26 stsp if (error)
2554 417a6e49 2020-09-26 stsp goto done;
2555 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2556 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2557 417a6e49 2020-09-26 stsp if (error != NULL) {
2558 417a6e49 2020-09-26 stsp free(commit_id);
2559 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2560 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2561 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2562 417a6e49 2020-09-26 stsp }
2563 417a6e49 2020-09-26 stsp goto done;
2564 417a6e49 2020-09-26 stsp }
2565 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2566 417a6e49 2020-09-26 stsp if (error) {
2567 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2568 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2569 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2570 417a6e49 2020-09-26 stsp }
2571 417a6e49 2020-09-26 stsp goto done;
2572 417a6e49 2020-09-26 stsp }
2573 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2574 417a6e49 2020-09-26 stsp commit_id);
2575 417a6e49 2020-09-26 stsp free(commit_id);
2576 417a6e49 2020-09-26 stsp if (error)
2577 417a6e49 2020-09-26 stsp goto done;
2578 417a6e49 2020-09-26 stsp }
2579 417a6e49 2020-09-26 stsp
2580 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
2581 417a6e49 2020-09-26 stsp if (error)
2582 417a6e49 2020-09-26 stsp goto done;
2583 417a6e49 2020-09-26 stsp cpa.worktree_path = worktree_path;
2584 417a6e49 2020-09-26 stsp cpa.had_base_commit_ref_error = 0;
2585 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2586 417a6e49 2020-09-26 stsp checkout_progress, &cpa, check_cancelled, NULL);
2587 417a6e49 2020-09-26 stsp if (error != NULL)
2588 417a6e49 2020-09-26 stsp goto done;
2589 417a6e49 2020-09-26 stsp
2590 417a6e49 2020-09-26 stsp printf("Now shut up and hack\n");
2591 417a6e49 2020-09-26 stsp if (cpa.had_base_commit_ref_error)
2592 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning();
2593 417a6e49 2020-09-26 stsp done:
2594 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2595 417a6e49 2020-09-26 stsp free(commit_id_str);
2596 417a6e49 2020-09-26 stsp free(repo_path);
2597 417a6e49 2020-09-26 stsp free(worktree_path);
2598 417a6e49 2020-09-26 stsp free(cwd);
2599 417a6e49 2020-09-26 stsp free(path);
2600 417a6e49 2020-09-26 stsp return error;
2601 417a6e49 2020-09-26 stsp }
2602 417a6e49 2020-09-26 stsp
2603 417a6e49 2020-09-26 stsp struct got_update_progress_arg {
2604 417a6e49 2020-09-26 stsp int did_something;
2605 417a6e49 2020-09-26 stsp int conflicts;
2606 417a6e49 2020-09-26 stsp int obstructed;
2607 417a6e49 2020-09-26 stsp int not_updated;
2608 417a6e49 2020-09-26 stsp };
2609 417a6e49 2020-09-26 stsp
2610 417a6e49 2020-09-26 stsp void
2611 417a6e49 2020-09-26 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2612 417a6e49 2020-09-26 stsp {
2613 417a6e49 2020-09-26 stsp if (!upa->did_something)
2614 417a6e49 2020-09-26 stsp return;
2615 417a6e49 2020-09-26 stsp
2616 417a6e49 2020-09-26 stsp if (upa->conflicts > 0)
2617 417a6e49 2020-09-26 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2618 417a6e49 2020-09-26 stsp if (upa->obstructed > 0)
2619 417a6e49 2020-09-26 stsp printf("File paths obstructed by a non-regular file: %d\n",
2620 417a6e49 2020-09-26 stsp upa->obstructed);
2621 417a6e49 2020-09-26 stsp if (upa->not_updated > 0)
2622 417a6e49 2020-09-26 stsp printf("Files not updated because of existing merge "
2623 417a6e49 2020-09-26 stsp "conflicts: %d\n", upa->not_updated);
2624 417a6e49 2020-09-26 stsp }
2625 417a6e49 2020-09-26 stsp
2626 417a6e49 2020-09-26 stsp __dead static void
2627 417a6e49 2020-09-26 stsp usage_update(void)
2628 417a6e49 2020-09-26 stsp {
2629 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2630 417a6e49 2020-09-26 stsp getprogname());
2631 417a6e49 2020-09-26 stsp exit(1);
2632 417a6e49 2020-09-26 stsp }
2633 417a6e49 2020-09-26 stsp
2634 417a6e49 2020-09-26 stsp static const struct got_error *
2635 417a6e49 2020-09-26 stsp update_progress(void *arg, unsigned char status, const char *path)
2636 417a6e49 2020-09-26 stsp {
2637 417a6e49 2020-09-26 stsp struct got_update_progress_arg *upa = arg;
2638 417a6e49 2020-09-26 stsp
2639 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_EXISTS ||
2640 417a6e49 2020-09-26 stsp status == GOT_STATUS_BASE_REF_ERR)
2641 417a6e49 2020-09-26 stsp return NULL;
2642 417a6e49 2020-09-26 stsp
2643 417a6e49 2020-09-26 stsp upa->did_something = 1;
2644 417a6e49 2020-09-26 stsp
2645 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2646 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2647 417a6e49 2020-09-26 stsp return NULL;
2648 417a6e49 2020-09-26 stsp
2649 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CONFLICT)
2650 417a6e49 2020-09-26 stsp upa->conflicts++;
2651 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_OBSTRUCTED)
2652 417a6e49 2020-09-26 stsp upa->obstructed++;
2653 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2654 417a6e49 2020-09-26 stsp upa->not_updated++;
2655 417a6e49 2020-09-26 stsp
2656 417a6e49 2020-09-26 stsp while (path[0] == '/')
2657 417a6e49 2020-09-26 stsp path++;
2658 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
2659 417a6e49 2020-09-26 stsp return NULL;
2660 417a6e49 2020-09-26 stsp }
2661 417a6e49 2020-09-26 stsp
2662 417a6e49 2020-09-26 stsp static const struct got_error *
2663 417a6e49 2020-09-26 stsp switch_head_ref(struct got_reference *head_ref,
2664 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2665 417a6e49 2020-09-26 stsp struct got_repository *repo)
2666 417a6e49 2020-09-26 stsp {
2667 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2668 417a6e49 2020-09-26 stsp char *base_id_str;
2669 417a6e49 2020-09-26 stsp int ref_has_moved = 0;
2670 417a6e49 2020-09-26 stsp
2671 417a6e49 2020-09-26 stsp /* Trivial case: switching between two different references. */
2672 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_name(head_ref),
2673 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2674 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n",
2675 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree),
2676 417a6e49 2020-09-26 stsp got_ref_get_name(head_ref));
2677 417a6e49 2020-09-26 stsp return got_worktree_set_head_ref(worktree, head_ref);
2678 417a6e49 2020-09-26 stsp }
2679 417a6e49 2020-09-26 stsp
2680 417a6e49 2020-09-26 stsp err = check_linear_ancestry(commit_id,
2681 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2682 417a6e49 2020-09-26 stsp if (err) {
2683 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_ANCESTRY)
2684 417a6e49 2020-09-26 stsp return err;
2685 417a6e49 2020-09-26 stsp ref_has_moved = 1;
2686 417a6e49 2020-09-26 stsp }
2687 417a6e49 2020-09-26 stsp if (!ref_has_moved)
2688 417a6e49 2020-09-26 stsp return NULL;
2689 417a6e49 2020-09-26 stsp
2690 417a6e49 2020-09-26 stsp /* Switching to a rebased branch with the same reference name. */
2691 417a6e49 2020-09-26 stsp err = got_object_id_str(&base_id_str,
2692 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
2693 417a6e49 2020-09-26 stsp if (err)
2694 417a6e49 2020-09-26 stsp return err;
2695 417a6e49 2020-09-26 stsp printf("Reference %s now points at a different branch\n",
2696 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2697 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2698 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2699 417a6e49 2020-09-26 stsp return NULL;
2700 417a6e49 2020-09-26 stsp }
2701 417a6e49 2020-09-26 stsp
2702 417a6e49 2020-09-26 stsp static const struct got_error *
2703 417a6e49 2020-09-26 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2704 417a6e49 2020-09-26 stsp {
2705 417a6e49 2020-09-26 stsp const struct got_error *err;
2706 417a6e49 2020-09-26 stsp int in_progress;
2707 417a6e49 2020-09-26 stsp
2708 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2709 417a6e49 2020-09-26 stsp if (err)
2710 417a6e49 2020-09-26 stsp return err;
2711 417a6e49 2020-09-26 stsp if (in_progress)
2712 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_REBASING);
2713 417a6e49 2020-09-26 stsp
2714 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2715 417a6e49 2020-09-26 stsp if (err)
2716 417a6e49 2020-09-26 stsp return err;
2717 417a6e49 2020-09-26 stsp if (in_progress)
2718 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2719 417a6e49 2020-09-26 stsp
2720 417a6e49 2020-09-26 stsp return NULL;
2721 417a6e49 2020-09-26 stsp }
2722 417a6e49 2020-09-26 stsp
2723 417a6e49 2020-09-26 stsp static const struct got_error *
2724 417a6e49 2020-09-26 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2725 417a6e49 2020-09-26 stsp char *argv[], struct got_worktree *worktree)
2726 417a6e49 2020-09-26 stsp {
2727 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2728 417a6e49 2020-09-26 stsp char *path;
2729 417a6e49 2020-09-26 stsp int i;
2730 417a6e49 2020-09-26 stsp
2731 417a6e49 2020-09-26 stsp if (argc == 0) {
2732 417a6e49 2020-09-26 stsp path = strdup("");
2733 417a6e49 2020-09-26 stsp if (path == NULL)
2734 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2735 417a6e49 2020-09-26 stsp return got_pathlist_append(paths, path, NULL);
2736 417a6e49 2020-09-26 stsp }
2737 417a6e49 2020-09-26 stsp
2738 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
2739 417a6e49 2020-09-26 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2740 417a6e49 2020-09-26 stsp if (err)
2741 417a6e49 2020-09-26 stsp break;
2742 417a6e49 2020-09-26 stsp err = got_pathlist_append(paths, path, NULL);
2743 417a6e49 2020-09-26 stsp if (err) {
2744 417a6e49 2020-09-26 stsp free(path);
2745 417a6e49 2020-09-26 stsp break;
2746 417a6e49 2020-09-26 stsp }
2747 417a6e49 2020-09-26 stsp }
2748 417a6e49 2020-09-26 stsp
2749 417a6e49 2020-09-26 stsp return err;
2750 417a6e49 2020-09-26 stsp }
2751 417a6e49 2020-09-26 stsp
2752 417a6e49 2020-09-26 stsp static const struct got_error *
2753 417a6e49 2020-09-26 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2754 417a6e49 2020-09-26 stsp const char *cmdname, const char *path)
2755 417a6e49 2020-09-26 stsp {
2756 417a6e49 2020-09-26 stsp const struct got_error *err;
2757 417a6e49 2020-09-26 stsp struct got_repository *repo;
2758 417a6e49 2020-09-26 stsp static char msg[512];
2759 417a6e49 2020-09-26 stsp
2760 417a6e49 2020-09-26 stsp err = got_repo_open(&repo, path, NULL);
2761 417a6e49 2020-09-26 stsp if (err)
2762 417a6e49 2020-09-26 stsp return orig_err;
2763 417a6e49 2020-09-26 stsp
2764 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2765 417a6e49 2020-09-26 stsp "'got %s' needs a work tree in addition to a git repository\n"
2766 417a6e49 2020-09-26 stsp "Work trees can be checked out from this Git repository with "
2767 417a6e49 2020-09-26 stsp "'got checkout'.\n"
2768 417a6e49 2020-09-26 stsp "The got(1) manual page contains more information.", cmdname);
2769 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2770 417a6e49 2020-09-26 stsp got_repo_close(repo);
2771 417a6e49 2020-09-26 stsp return err;
2772 417a6e49 2020-09-26 stsp }
2773 417a6e49 2020-09-26 stsp
2774 417a6e49 2020-09-26 stsp static const struct got_error *
2775 417a6e49 2020-09-26 stsp cmd_update(int argc, char *argv[])
2776 417a6e49 2020-09-26 stsp {
2777 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2778 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2779 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2780 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2781 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
2782 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2783 417a6e49 2020-09-26 stsp const char *branch_name = NULL;
2784 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2785 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2786 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
2787 417a6e49 2020-09-26 stsp int ch;
2788 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
2789 417a6e49 2020-09-26 stsp
2790 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2791 417a6e49 2020-09-26 stsp
2792 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2793 417a6e49 2020-09-26 stsp switch (ch) {
2794 417a6e49 2020-09-26 stsp case 'b':
2795 417a6e49 2020-09-26 stsp branch_name = optarg;
2796 417a6e49 2020-09-26 stsp break;
2797 417a6e49 2020-09-26 stsp case 'c':
2798 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2799 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2800 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2801 417a6e49 2020-09-26 stsp break;
2802 417a6e49 2020-09-26 stsp default:
2803 417a6e49 2020-09-26 stsp usage_update();
2804 417a6e49 2020-09-26 stsp /* NOTREACHED */
2805 417a6e49 2020-09-26 stsp }
2806 417a6e49 2020-09-26 stsp }
2807 417a6e49 2020-09-26 stsp
2808 417a6e49 2020-09-26 stsp argc -= optind;
2809 417a6e49 2020-09-26 stsp argv += optind;
2810 417a6e49 2020-09-26 stsp
2811 417a6e49 2020-09-26 stsp #ifndef PROFILE
2812 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2813 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2814 417a6e49 2020-09-26 stsp err(1, "pledge");
2815 417a6e49 2020-09-26 stsp #endif
2816 417a6e49 2020-09-26 stsp worktree_path = getcwd(NULL, 0);
2817 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2818 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2819 417a6e49 2020-09-26 stsp goto done;
2820 417a6e49 2020-09-26 stsp }
2821 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2822 417a6e49 2020-09-26 stsp if (error) {
2823 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2824 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "update",
2825 417a6e49 2020-09-26 stsp worktree_path);
2826 417a6e49 2020-09-26 stsp goto done;
2827 417a6e49 2020-09-26 stsp }
2828 417a6e49 2020-09-26 stsp
2829 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
2830 417a6e49 2020-09-26 stsp if (error)
2831 417a6e49 2020-09-26 stsp goto done;
2832 417a6e49 2020-09-26 stsp
2833 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2834 417a6e49 2020-09-26 stsp NULL);
2835 417a6e49 2020-09-26 stsp if (error != NULL)
2836 417a6e49 2020-09-26 stsp goto done;
2837 417a6e49 2020-09-26 stsp
2838 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2839 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
2840 417a6e49 2020-09-26 stsp if (error)
2841 417a6e49 2020-09-26 stsp goto done;
2842 417a6e49 2020-09-26 stsp
2843 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2844 417a6e49 2020-09-26 stsp if (error)
2845 417a6e49 2020-09-26 stsp goto done;
2846 417a6e49 2020-09-26 stsp
2847 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2848 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
2849 417a6e49 2020-09-26 stsp if (error != NULL)
2850 417a6e49 2020-09-26 stsp goto done;
2851 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
2852 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2853 417a6e49 2020-09-26 stsp if (error != NULL)
2854 417a6e49 2020-09-26 stsp goto done;
2855 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2856 417a6e49 2020-09-26 stsp if (error != NULL)
2857 417a6e49 2020-09-26 stsp goto done;
2858 417a6e49 2020-09-26 stsp } else {
2859 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2860 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2861 417a6e49 2020-09-26 stsp free(commit_id_str);
2862 417a6e49 2020-09-26 stsp commit_id_str = NULL;
2863 417a6e49 2020-09-26 stsp if (error)
2864 417a6e49 2020-09-26 stsp goto done;
2865 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2866 417a6e49 2020-09-26 stsp if (error)
2867 417a6e49 2020-09-26 stsp goto done;
2868 417a6e49 2020-09-26 stsp }
2869 417a6e49 2020-09-26 stsp
2870 417a6e49 2020-09-26 stsp if (branch_name) {
2871 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id;
2872 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
2873 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
2874 417a6e49 2020-09-26 stsp continue;
2875 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2876 417a6e49 2020-09-26 stsp "switching between branches requires that "
2877 417a6e49 2020-09-26 stsp "the entire work tree gets updated");
2878 417a6e49 2020-09-26 stsp goto done;
2879 417a6e49 2020-09-26 stsp }
2880 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2881 417a6e49 2020-09-26 stsp if (error)
2882 417a6e49 2020-09-26 stsp goto done;
2883 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2884 417a6e49 2020-09-26 stsp repo);
2885 417a6e49 2020-09-26 stsp free(head_commit_id);
2886 417a6e49 2020-09-26 stsp if (error != NULL)
2887 417a6e49 2020-09-26 stsp goto done;
2888 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2889 417a6e49 2020-09-26 stsp if (error)
2890 417a6e49 2020-09-26 stsp goto done;
2891 417a6e49 2020-09-26 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2892 417a6e49 2020-09-26 stsp if (error)
2893 417a6e49 2020-09-26 stsp goto done;
2894 417a6e49 2020-09-26 stsp } else {
2895 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2896 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2897 417a6e49 2020-09-26 stsp if (error != NULL) {
2898 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
2899 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2900 417a6e49 2020-09-26 stsp goto done;
2901 417a6e49 2020-09-26 stsp }
2902 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2903 417a6e49 2020-09-26 stsp if (error)
2904 417a6e49 2020-09-26 stsp goto done;
2905 417a6e49 2020-09-26 stsp }
2906 417a6e49 2020-09-26 stsp
2907 417a6e49 2020-09-26 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2908 417a6e49 2020-09-26 stsp commit_id) != 0) {
2909 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2910 417a6e49 2020-09-26 stsp commit_id);
2911 417a6e49 2020-09-26 stsp if (error)
2912 417a6e49 2020-09-26 stsp goto done;
2913 417a6e49 2020-09-26 stsp }
2914 417a6e49 2020-09-26 stsp
2915 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
2916 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2917 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
2918 417a6e49 2020-09-26 stsp if (error != NULL)
2919 417a6e49 2020-09-26 stsp goto done;
2920 417a6e49 2020-09-26 stsp
2921 417a6e49 2020-09-26 stsp if (upa.did_something)
2922 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
2923 417a6e49 2020-09-26 stsp else
2924 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2925 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
2926 417a6e49 2020-09-26 stsp done:
2927 417a6e49 2020-09-26 stsp free(worktree_path);
2928 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
2929 417a6e49 2020-09-26 stsp free((char *)pe->path);
2930 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2931 417a6e49 2020-09-26 stsp free(commit_id);
2932 417a6e49 2020-09-26 stsp free(commit_id_str);
2933 417a6e49 2020-09-26 stsp return error;
2934 417a6e49 2020-09-26 stsp }
2935 417a6e49 2020-09-26 stsp
2936 417a6e49 2020-09-26 stsp static const struct got_error *
2937 417a6e49 2020-09-26 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2938 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2939 417a6e49 2020-09-26 stsp struct got_repository *repo)
2940 417a6e49 2020-09-26 stsp {
2941 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2942 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2943 417a6e49 2020-09-26 stsp
2944 417a6e49 2020-09-26 stsp if (blob_id1) {
2945 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2946 417a6e49 2020-09-26 stsp if (err)
2947 417a6e49 2020-09-26 stsp goto done;
2948 417a6e49 2020-09-26 stsp }
2949 417a6e49 2020-09-26 stsp
2950 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2951 417a6e49 2020-09-26 stsp if (err)
2952 417a6e49 2020-09-26 stsp goto done;
2953 417a6e49 2020-09-26 stsp
2954 417a6e49 2020-09-26 stsp while (path[0] == '/')
2955 417a6e49 2020-09-26 stsp path++;
2956 417a6e49 2020-09-26 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2957 417a6e49 2020-09-26 stsp ignore_whitespace, stdout);
2958 417a6e49 2020-09-26 stsp done:
2959 417a6e49 2020-09-26 stsp if (blob1)
2960 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
2961 417a6e49 2020-09-26 stsp got_object_blob_close(blob2);
2962 417a6e49 2020-09-26 stsp return err;
2963 417a6e49 2020-09-26 stsp }
2964 417a6e49 2020-09-26 stsp
2965 417a6e49 2020-09-26 stsp static const struct got_error *
2966 417a6e49 2020-09-26 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2967 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2968 417a6e49 2020-09-26 stsp struct got_repository *repo)
2969 417a6e49 2020-09-26 stsp {
2970 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2971 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2972 417a6e49 2020-09-26 stsp struct got_diff_blob_output_unidiff_arg arg;
2973 417a6e49 2020-09-26 stsp
2974 417a6e49 2020-09-26 stsp if (tree_id1) {
2975 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2976 417a6e49 2020-09-26 stsp if (err)
2977 417a6e49 2020-09-26 stsp goto done;
2978 417a6e49 2020-09-26 stsp }
2979 417a6e49 2020-09-26 stsp
2980 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2981 417a6e49 2020-09-26 stsp if (err)
2982 417a6e49 2020-09-26 stsp goto done;
2983 417a6e49 2020-09-26 stsp
2984 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
2985 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
2986 417a6e49 2020-09-26 stsp arg.outfile = stdout;
2987 417a6e49 2020-09-26 stsp while (path[0] == '/')
2988 417a6e49 2020-09-26 stsp path++;
2989 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2990 417a6e49 2020-09-26 stsp got_diff_blob_output_unidiff, &arg, 1);
2991 417a6e49 2020-09-26 stsp done:
2992 417a6e49 2020-09-26 stsp if (tree1)
2993 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
2994 417a6e49 2020-09-26 stsp if (tree2)
2995 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
2996 417a6e49 2020-09-26 stsp return err;
2997 417a6e49 2020-09-26 stsp }
2998 417a6e49 2020-09-26 stsp
2999 417a6e49 2020-09-26 stsp static const struct got_error *
3000 417a6e49 2020-09-26 stsp get_changed_paths(struct got_pathlist_head *paths,
3001 417a6e49 2020-09-26 stsp struct got_commit_object *commit, struct got_repository *repo)
3002 417a6e49 2020-09-26 stsp {
3003 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3004 417a6e49 2020-09-26 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3005 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3006 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3007 417a6e49 2020-09-26 stsp
3008 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3009 417a6e49 2020-09-26 stsp if (qid != NULL) {
3010 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit;
3011 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3012 417a6e49 2020-09-26 stsp qid->id);
3013 417a6e49 2020-09-26 stsp if (err)
3014 417a6e49 2020-09-26 stsp return err;
3015 417a6e49 2020-09-26 stsp
3016 417a6e49 2020-09-26 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3017 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3018 417a6e49 2020-09-26 stsp
3019 417a6e49 2020-09-26 stsp }
3020 417a6e49 2020-09-26 stsp
3021 417a6e49 2020-09-26 stsp if (tree_id1) {
3022 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3023 417a6e49 2020-09-26 stsp if (err)
3024 417a6e49 2020-09-26 stsp goto done;
3025 417a6e49 2020-09-26 stsp }
3026 417a6e49 2020-09-26 stsp
3027 417a6e49 2020-09-26 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3028 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3029 417a6e49 2020-09-26 stsp if (err)
3030 417a6e49 2020-09-26 stsp goto done;
3031 417a6e49 2020-09-26 stsp
3032 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3033 417a6e49 2020-09-26 stsp got_diff_tree_collect_changed_paths, paths, 0);
3034 417a6e49 2020-09-26 stsp done:
3035 417a6e49 2020-09-26 stsp if (tree1)
3036 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
3037 417a6e49 2020-09-26 stsp if (tree2)
3038 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
3039 417a6e49 2020-09-26 stsp return err;
3040 417a6e49 2020-09-26 stsp }
3041 417a6e49 2020-09-26 stsp
3042 417a6e49 2020-09-26 stsp static const struct got_error *
3043 417a6e49 2020-09-26 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3044 417a6e49 2020-09-26 stsp const char *path, int diff_context, struct got_repository *repo)
3045 417a6e49 2020-09-26 stsp {
3046 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3047 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit = NULL;
3048 417a6e49 2020-09-26 stsp char *id_str1 = NULL, *id_str2 = NULL;
3049 417a6e49 2020-09-26 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3050 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3051 417a6e49 2020-09-26 stsp
3052 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3053 417a6e49 2020-09-26 stsp if (qid != NULL) {
3054 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3055 417a6e49 2020-09-26 stsp qid->id);
3056 417a6e49 2020-09-26 stsp if (err)
3057 417a6e49 2020-09-26 stsp return err;
3058 417a6e49 2020-09-26 stsp }
3059 417a6e49 2020-09-26 stsp
3060 417a6e49 2020-09-26 stsp if (path && path[0] != '\0') {
3061 417a6e49 2020-09-26 stsp int obj_type;
3062 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3063 417a6e49 2020-09-26 stsp if (err)
3064 417a6e49 2020-09-26 stsp goto done;
3065 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3066 417a6e49 2020-09-26 stsp if (err) {
3067 417a6e49 2020-09-26 stsp free(obj_id2);
3068 417a6e49 2020-09-26 stsp goto done;
3069 417a6e49 2020-09-26 stsp }
3070 417a6e49 2020-09-26 stsp if (pcommit) {
3071 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id1, repo,
3072 417a6e49 2020-09-26 stsp qid->id, path);
3073 417a6e49 2020-09-26 stsp if (err) {
3074 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3075 417a6e49 2020-09-26 stsp free(obj_id2);
3076 417a6e49 2020-09-26 stsp goto done;
3077 417a6e49 2020-09-26 stsp }
3078 417a6e49 2020-09-26 stsp } else {
3079 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3080 417a6e49 2020-09-26 stsp if (err) {
3081 417a6e49 2020-09-26 stsp free(obj_id2);
3082 417a6e49 2020-09-26 stsp goto done;
3083 417a6e49 2020-09-26 stsp }
3084 417a6e49 2020-09-26 stsp }
3085 417a6e49 2020-09-26 stsp }
3086 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3087 417a6e49 2020-09-26 stsp if (err) {
3088 417a6e49 2020-09-26 stsp free(obj_id2);
3089 417a6e49 2020-09-26 stsp goto done;
3090 417a6e49 2020-09-26 stsp }
3091 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3092 417a6e49 2020-09-26 stsp switch (obj_type) {
3093 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
3094 417a6e49 2020-09-26 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3095 417a6e49 2020-09-26 stsp 0, repo);
3096 417a6e49 2020-09-26 stsp break;
3097 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
3098 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3099 417a6e49 2020-09-26 stsp 0, repo);
3100 417a6e49 2020-09-26 stsp break;
3101 417a6e49 2020-09-26 stsp default:
3102 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3103 417a6e49 2020-09-26 stsp break;
3104 417a6e49 2020-09-26 stsp }
3105 417a6e49 2020-09-26 stsp free(obj_id1);
3106 417a6e49 2020-09-26 stsp free(obj_id2);
3107 417a6e49 2020-09-26 stsp } else {
3108 417a6e49 2020-09-26 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3109 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3110 417a6e49 2020-09-26 stsp if (err)
3111 417a6e49 2020-09-26 stsp goto done;
3112 417a6e49 2020-09-26 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3113 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3114 417a6e49 2020-09-26 stsp if (err)
3115 417a6e49 2020-09-26 stsp goto done;
3116 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3117 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3118 417a6e49 2020-09-26 stsp }
3119 417a6e49 2020-09-26 stsp done:
3120 417a6e49 2020-09-26 stsp free(id_str1);
3121 417a6e49 2020-09-26 stsp free(id_str2);
3122 417a6e49 2020-09-26 stsp if (pcommit)
3123 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3124 417a6e49 2020-09-26 stsp return err;
3125 417a6e49 2020-09-26 stsp }
3126 417a6e49 2020-09-26 stsp
3127 417a6e49 2020-09-26 stsp static char *
3128 417a6e49 2020-09-26 stsp get_datestr(time_t *time, char *datebuf)
3129 417a6e49 2020-09-26 stsp {
3130 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
3131 417a6e49 2020-09-26 stsp char *p, *s;
3132 417a6e49 2020-09-26 stsp
3133 417a6e49 2020-09-26 stsp tm = gmtime_r(time, &mytm);
3134 417a6e49 2020-09-26 stsp if (tm == NULL)
3135 417a6e49 2020-09-26 stsp return NULL;
3136 417a6e49 2020-09-26 stsp s = asctime_r(tm, datebuf);
3137 417a6e49 2020-09-26 stsp if (s == NULL)
3138 417a6e49 2020-09-26 stsp return NULL;
3139 417a6e49 2020-09-26 stsp p = strchr(s, '\n');
3140 417a6e49 2020-09-26 stsp if (p)
3141 417a6e49 2020-09-26 stsp *p = '\0';
3142 417a6e49 2020-09-26 stsp return s;
3143 417a6e49 2020-09-26 stsp }
3144 417a6e49 2020-09-26 stsp
3145 417a6e49 2020-09-26 stsp static const struct got_error *
3146 417a6e49 2020-09-26 stsp match_logmsg(int *have_match, struct got_object_id *id,
3147 417a6e49 2020-09-26 stsp struct got_commit_object *commit, regex_t *regex)
3148 417a6e49 2020-09-26 stsp {
3149 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3150 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3151 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
3152 417a6e49 2020-09-26 stsp
3153 417a6e49 2020-09-26 stsp *have_match = 0;
3154 417a6e49 2020-09-26 stsp
3155 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3156 417a6e49 2020-09-26 stsp if (err)
3157 417a6e49 2020-09-26 stsp return err;
3158 417a6e49 2020-09-26 stsp
3159 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3160 417a6e49 2020-09-26 stsp if (err)
3161 417a6e49 2020-09-26 stsp goto done;
3162 417a6e49 2020-09-26 stsp
3163 417a6e49 2020-09-26 stsp if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3164 417a6e49 2020-09-26 stsp *have_match = 1;
3165 417a6e49 2020-09-26 stsp done:
3166 417a6e49 2020-09-26 stsp free(id_str);
3167 417a6e49 2020-09-26 stsp free(logmsg);
3168 417a6e49 2020-09-26 stsp return err;
3169 417a6e49 2020-09-26 stsp }
3170 417a6e49 2020-09-26 stsp
3171 417a6e49 2020-09-26 stsp static void
3172 417a6e49 2020-09-26 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3173 417a6e49 2020-09-26 stsp regex_t *regex)
3174 417a6e49 2020-09-26 stsp {
3175 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3176 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3177 417a6e49 2020-09-26 stsp
3178 417a6e49 2020-09-26 stsp *have_match = 0;
3179 417a6e49 2020-09-26 stsp
3180 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3181 417a6e49 2020-09-26 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3182 417a6e49 2020-09-26 stsp *have_match = 1;
3183 417a6e49 2020-09-26 stsp break;
3184 417a6e49 2020-09-26 stsp }
3185 417a6e49 2020-09-26 stsp }
3186 417a6e49 2020-09-26 stsp }
3187 417a6e49 2020-09-26 stsp
3188 417a6e49 2020-09-26 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3189 417a6e49 2020-09-26 stsp
3190 417a6e49 2020-09-26 stsp static const struct got_error *
3191 417a6e49 2020-09-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3192 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path,
3193 417a6e49 2020-09-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3194 417a6e49 2020-09-26 stsp int diff_context, struct got_reflist_head *refs)
3195 417a6e49 2020-09-26 stsp {
3196 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3197 417a6e49 2020-09-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3198 417a6e49 2020-09-26 stsp char datebuf[26];
3199 417a6e49 2020-09-26 stsp time_t committer_time;
3200 417a6e49 2020-09-26 stsp const char *author, *committer;
3201 417a6e49 2020-09-26 stsp char *refs_str = NULL;
3202 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
3203 417a6e49 2020-09-26 stsp
3204 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3205 417a6e49 2020-09-26 stsp char *s;
3206 417a6e49 2020-09-26 stsp const char *name;
3207 417a6e49 2020-09-26 stsp struct got_tag_object *tag = NULL;
3208 417a6e49 2020-09-26 stsp struct got_object_id *ref_id;
3209 417a6e49 2020-09-26 stsp int cmp;
3210 417a6e49 2020-09-26 stsp
3211 417a6e49 2020-09-26 stsp name = got_ref_get_name(re->ref);
3212 417a6e49 2020-09-26 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3213 417a6e49 2020-09-26 stsp continue;
3214 417a6e49 2020-09-26 stsp if (strncmp(name, "refs/", 5) == 0)
3215 417a6e49 2020-09-26 stsp name += 5;
3216 417a6e49 2020-09-26 stsp if (strncmp(name, "got/", 4) == 0)
3217 417a6e49 2020-09-26 stsp continue;
3218 417a6e49 2020-09-26 stsp if (strncmp(name, "heads/", 6) == 0)
3219 417a6e49 2020-09-26 stsp name += 6;
3220 417a6e49 2020-09-26 stsp if (strncmp(name, "remotes/", 8) == 0) {
3221 417a6e49 2020-09-26 stsp name += 8;
3222 417a6e49 2020-09-26 stsp s = strstr(name, "/" GOT_REF_HEAD);
3223 417a6e49 2020-09-26 stsp if (s != NULL && s[strlen(s)] == '\0')
3224 417a6e49 2020-09-26 stsp continue;
3225 417a6e49 2020-09-26 stsp }
3226 417a6e49 2020-09-26 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3227 417a6e49 2020-09-26 stsp if (err)
3228 417a6e49 2020-09-26 stsp return err;
3229 417a6e49 2020-09-26 stsp if (strncmp(name, "tags/", 5) == 0) {
3230 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3231 417a6e49 2020-09-26 stsp if (err) {
3232 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3233 417a6e49 2020-09-26 stsp free(ref_id);
3234 417a6e49 2020-09-26 stsp return err;
3235 417a6e49 2020-09-26 stsp }
3236 417a6e49 2020-09-26 stsp /* Ref points at something other than a tag. */
3237 417a6e49 2020-09-26 stsp err = NULL;
3238 417a6e49 2020-09-26 stsp tag = NULL;
3239 417a6e49 2020-09-26 stsp }
3240 417a6e49 2020-09-26 stsp }
3241 417a6e49 2020-09-26 stsp cmp = got_object_id_cmp(tag ?
3242 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3243 417a6e49 2020-09-26 stsp free(ref_id);
3244 417a6e49 2020-09-26 stsp if (tag)
3245 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3246 417a6e49 2020-09-26 stsp if (cmp != 0)
3247 417a6e49 2020-09-26 stsp continue;
3248 417a6e49 2020-09-26 stsp s = refs_str;
3249 417a6e49 2020-09-26 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3250 417a6e49 2020-09-26 stsp name) == -1) {
3251 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3252 417a6e49 2020-09-26 stsp free(s);
3253 417a6e49 2020-09-26 stsp return err;
3254 417a6e49 2020-09-26 stsp }
3255 417a6e49 2020-09-26 stsp free(s);
3256 417a6e49 2020-09-26 stsp }
3257 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3258 417a6e49 2020-09-26 stsp if (err)
3259 417a6e49 2020-09-26 stsp return err;
3260 417a6e49 2020-09-26 stsp
3261 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
3262 417a6e49 2020-09-26 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3263 417a6e49 2020-09-26 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3264 417a6e49 2020-09-26 stsp free(id_str);
3265 417a6e49 2020-09-26 stsp id_str = NULL;
3266 417a6e49 2020-09-26 stsp free(refs_str);
3267 417a6e49 2020-09-26 stsp refs_str = NULL;
3268 417a6e49 2020-09-26 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3269 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
3270 417a6e49 2020-09-26 stsp datestr = get_datestr(&committer_time, datebuf);
3271 417a6e49 2020-09-26 stsp if (datestr)
3272 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
3273 417a6e49 2020-09-26 stsp author = got_object_commit_get_author(commit);
3274 417a6e49 2020-09-26 stsp committer = got_object_commit_get_committer(commit);
3275 417a6e49 2020-09-26 stsp if (strcmp(author, committer) != 0)
3276 417a6e49 2020-09-26 stsp printf("via: %s\n", committer);
3277 417a6e49 2020-09-26 stsp if (got_object_commit_get_nparents(commit) > 1) {
3278 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
3279 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3280 417a6e49 2020-09-26 stsp int n = 1;
3281 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3282 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3283 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
3284 417a6e49 2020-09-26 stsp if (err)
3285 417a6e49 2020-09-26 stsp return err;
3286 417a6e49 2020-09-26 stsp printf("parent %d: %s\n", n++, id_str);
3287 417a6e49 2020-09-26 stsp free(id_str);
3288 417a6e49 2020-09-26 stsp }
3289 417a6e49 2020-09-26 stsp }
3290 417a6e49 2020-09-26 stsp
3291 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3292 417a6e49 2020-09-26 stsp if (err)
3293 417a6e49 2020-09-26 stsp return err;
3294 417a6e49 2020-09-26 stsp
3295 417a6e49 2020-09-26 stsp logmsg = logmsg0;
3296 417a6e49 2020-09-26 stsp do {
3297 417a6e49 2020-09-26 stsp line = strsep(&logmsg, "\n");
3298 417a6e49 2020-09-26 stsp if (line)
3299 417a6e49 2020-09-26 stsp printf(" %s\n", line);
3300 417a6e49 2020-09-26 stsp } while (line);
3301 417a6e49 2020-09-26 stsp free(logmsg0);
3302 417a6e49 2020-09-26 stsp
3303 417a6e49 2020-09-26 stsp if (changed_paths) {
3304 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3305 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3306 417a6e49 2020-09-26 stsp struct got_diff_changed_path *cp = pe->data;
3307 417a6e49 2020-09-26 stsp printf(" %c %s\n", cp->status, pe->path);
3308 417a6e49 2020-09-26 stsp }
3309 417a6e49 2020-09-26 stsp printf("\n");
3310 417a6e49 2020-09-26 stsp }
3311 417a6e49 2020-09-26 stsp if (show_patch) {
3312 417a6e49 2020-09-26 stsp err = print_patch(commit, id, path, diff_context, repo);
3313 417a6e49 2020-09-26 stsp if (err == 0)
3314 417a6e49 2020-09-26 stsp printf("\n");
3315 417a6e49 2020-09-26 stsp }
3316 417a6e49 2020-09-26 stsp
3317 417a6e49 2020-09-26 stsp if (fflush(stdout) != 0 && err == NULL)
3318 417a6e49 2020-09-26 stsp err = got_error_from_errno("fflush");
3319 417a6e49 2020-09-26 stsp return err;
3320 417a6e49 2020-09-26 stsp }
3321 417a6e49 2020-09-26 stsp
3322 417a6e49 2020-09-26 stsp static const struct got_error *
3323 417a6e49 2020-09-26 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3324 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3325 417a6e49 2020-09-26 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3326 417a6e49 2020-09-26 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3327 417a6e49 2020-09-26 stsp {
3328 417a6e49 2020-09-26 stsp const struct got_error *err;
3329 417a6e49 2020-09-26 stsp struct got_commit_graph *graph;
3330 417a6e49 2020-09-26 stsp regex_t regex;
3331 417a6e49 2020-09-26 stsp int have_match;
3332 417a6e49 2020-09-26 stsp struct got_object_id_queue reversed_commits;
3333 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3334 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
3335 417a6e49 2020-09-26 stsp struct got_pathlist_head changed_paths;
3336 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3337 417a6e49 2020-09-26 stsp
3338 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&reversed_commits);
3339 417a6e49 2020-09-26 stsp TAILQ_INIT(&changed_paths);
3340 417a6e49 2020-09-26 stsp
3341 417a6e49 2020-09-26 stsp if (search_pattern && regcomp(&regex, search_pattern,
3342 417a6e49 2020-09-26 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3343 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_REGEX, search_pattern);
3344 417a6e49 2020-09-26 stsp
3345 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3346 417a6e49 2020-09-26 stsp if (err)
3347 417a6e49 2020-09-26 stsp return err;
3348 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3349 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3350 417a6e49 2020-09-26 stsp if (err)
3351 417a6e49 2020-09-26 stsp goto done;
3352 417a6e49 2020-09-26 stsp for (;;) {
3353 417a6e49 2020-09-26 stsp struct got_object_id *id;
3354 417a6e49 2020-09-26 stsp
3355 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
3356 417a6e49 2020-09-26 stsp break;
3357 417a6e49 2020-09-26 stsp
3358 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3359 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3360 417a6e49 2020-09-26 stsp if (err) {
3361 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3362 417a6e49 2020-09-26 stsp err = NULL;
3363 417a6e49 2020-09-26 stsp break;
3364 417a6e49 2020-09-26 stsp }
3365 417a6e49 2020-09-26 stsp if (id == NULL)
3366 417a6e49 2020-09-26 stsp break;
3367 417a6e49 2020-09-26 stsp
3368 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
3369 417a6e49 2020-09-26 stsp if (err)
3370 417a6e49 2020-09-26 stsp break;
3371 417a6e49 2020-09-26 stsp
3372 417a6e49 2020-09-26 stsp if (show_changed_paths && !reverse_display_order) {
3373 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths, commit, repo);
3374 417a6e49 2020-09-26 stsp if (err)
3375 417a6e49 2020-09-26 stsp break;
3376 417a6e49 2020-09-26 stsp }
3377 417a6e49 2020-09-26 stsp
3378 417a6e49 2020-09-26 stsp if (search_pattern) {
3379 417a6e49 2020-09-26 stsp err = match_logmsg(&have_match, id, commit, &regex);
3380 417a6e49 2020-09-26 stsp if (err) {
3381 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3382 417a6e49 2020-09-26 stsp break;
3383 417a6e49 2020-09-26 stsp }
3384 417a6e49 2020-09-26 stsp if (have_match == 0 && show_changed_paths)
3385 417a6e49 2020-09-26 stsp match_changed_paths(&have_match,
3386 417a6e49 2020-09-26 stsp &changed_paths, &regex);
3387 417a6e49 2020-09-26 stsp if (have_match == 0) {
3388 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3389 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3390 417a6e49 2020-09-26 stsp free((char *)pe->path);
3391 417a6e49 2020-09-26 stsp free(pe->data);
3392 417a6e49 2020-09-26 stsp }
3393 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3394 417a6e49 2020-09-26 stsp continue;
3395 417a6e49 2020-09-26 stsp }
3396 417a6e49 2020-09-26 stsp }
3397 417a6e49 2020-09-26 stsp
3398 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3399 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, id);
3400 417a6e49 2020-09-26 stsp if (err)
3401 417a6e49 2020-09-26 stsp break;
3402 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3403 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3404 417a6e49 2020-09-26 stsp } else {
3405 417a6e49 2020-09-26 stsp err = print_commit(commit, id, repo, path,
3406 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3407 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3408 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3409 417a6e49 2020-09-26 stsp if (err)
3410 417a6e49 2020-09-26 stsp break;
3411 417a6e49 2020-09-26 stsp }
3412 417a6e49 2020-09-26 stsp if ((limit && --limit == 0) ||
3413 417a6e49 2020-09-26 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3414 417a6e49 2020-09-26 stsp break;
3415 417a6e49 2020-09-26 stsp
3416 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3417 417a6e49 2020-09-26 stsp free((char *)pe->path);
3418 417a6e49 2020-09-26 stsp free(pe->data);
3419 417a6e49 2020-09-26 stsp }
3420 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3421 417a6e49 2020-09-26 stsp }
3422 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3423 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3424 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3425 417a6e49 2020-09-26 stsp if (err)
3426 417a6e49 2020-09-26 stsp break;
3427 417a6e49 2020-09-26 stsp if (show_changed_paths) {
3428 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths,
3429 417a6e49 2020-09-26 stsp commit, repo);
3430 417a6e49 2020-09-26 stsp if (err)
3431 417a6e49 2020-09-26 stsp break;
3432 417a6e49 2020-09-26 stsp }
3433 417a6e49 2020-09-26 stsp err = print_commit(commit, qid->id, repo, path,
3434 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3435 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3436 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3437 417a6e49 2020-09-26 stsp if (err)
3438 417a6e49 2020-09-26 stsp break;
3439 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3440 417a6e49 2020-09-26 stsp free((char *)pe->path);
3441 417a6e49 2020-09-26 stsp free(pe->data);
3442 417a6e49 2020-09-26 stsp }
3443 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3444 417a6e49 2020-09-26 stsp }
3445 417a6e49 2020-09-26 stsp }
3446 417a6e49 2020-09-26 stsp done:
3447 417a6e49 2020-09-26 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3448 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3449 417a6e49 2020-09-26 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3450 417a6e49 2020-09-26 stsp got_object_qid_free(qid);
3451 417a6e49 2020-09-26 stsp }
3452 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3453 417a6e49 2020-09-26 stsp free((char *)pe->path);
3454 417a6e49 2020-09-26 stsp free(pe->data);
3455 417a6e49 2020-09-26 stsp }
3456 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3457 417a6e49 2020-09-26 stsp if (search_pattern)
3458 417a6e49 2020-09-26 stsp regfree(&regex);
3459 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
3460 417a6e49 2020-09-26 stsp return err;
3461 417a6e49 2020-09-26 stsp }
3462 417a6e49 2020-09-26 stsp
3463 417a6e49 2020-09-26 stsp __dead static void
3464 417a6e49 2020-09-26 stsp usage_log(void)
3465 417a6e49 2020-09-26 stsp {
3466 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3467 417a6e49 2020-09-26 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3468 417a6e49 2020-09-26 stsp "[-R] [path]\n", getprogname());
3469 417a6e49 2020-09-26 stsp exit(1);
3470 417a6e49 2020-09-26 stsp }
3471 417a6e49 2020-09-26 stsp
3472 417a6e49 2020-09-26 stsp static int
3473 417a6e49 2020-09-26 stsp get_default_log_limit(void)
3474 417a6e49 2020-09-26 stsp {
3475 417a6e49 2020-09-26 stsp const char *got_default_log_limit;
3476 417a6e49 2020-09-26 stsp long long n;
3477 417a6e49 2020-09-26 stsp const char *errstr;
3478 417a6e49 2020-09-26 stsp
3479 417a6e49 2020-09-26 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3480 417a6e49 2020-09-26 stsp if (got_default_log_limit == NULL)
3481 417a6e49 2020-09-26 stsp return 0;
3482 417a6e49 2020-09-26 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3483 417a6e49 2020-09-26 stsp if (errstr != NULL)
3484 417a6e49 2020-09-26 stsp return 0;
3485 417a6e49 2020-09-26 stsp return n;
3486 417a6e49 2020-09-26 stsp }
3487 417a6e49 2020-09-26 stsp
3488 417a6e49 2020-09-26 stsp static const struct got_error *
3489 417a6e49 2020-09-26 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3490 417a6e49 2020-09-26 stsp struct got_repository *repo)
3491 417a6e49 2020-09-26 stsp {
3492 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3493 417a6e49 2020-09-26 stsp struct got_reference *ref;
3494 417a6e49 2020-09-26 stsp
3495 417a6e49 2020-09-26 stsp *id = NULL;
3496 417a6e49 2020-09-26 stsp
3497 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3498 417a6e49 2020-09-26 stsp if (err == NULL) {
3499 417a6e49 2020-09-26 stsp int obj_type;
3500 417a6e49 2020-09-26 stsp err = got_ref_resolve(id, repo, ref);
3501 417a6e49 2020-09-26 stsp got_ref_close(ref);
3502 417a6e49 2020-09-26 stsp if (err)
3503 417a6e49 2020-09-26 stsp return err;
3504 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, *id);
3505 417a6e49 2020-09-26 stsp if (err)
3506 417a6e49 2020-09-26 stsp return err;
3507 417a6e49 2020-09-26 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3508 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
3509 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, *id);
3510 417a6e49 2020-09-26 stsp if (err)
3511 417a6e49 2020-09-26 stsp return err;
3512 417a6e49 2020-09-26 stsp if (got_object_tag_get_object_type(tag) !=
3513 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT) {
3514 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3515 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3516 417a6e49 2020-09-26 stsp }
3517 417a6e49 2020-09-26 stsp free(*id);
3518 417a6e49 2020-09-26 stsp *id = got_object_id_dup(
3519 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
3520 417a6e49 2020-09-26 stsp if (*id == NULL)
3521 417a6e49 2020-09-26 stsp err = got_error_from_errno(
3522 417a6e49 2020-09-26 stsp "got_object_id_dup");
3523 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3524 417a6e49 2020-09-26 stsp if (err)
3525 417a6e49 2020-09-26 stsp return err;
3526 417a6e49 2020-09-26 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3527 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3528 417a6e49 2020-09-26 stsp } else {
3529 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3530 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
3531 417a6e49 2020-09-26 stsp }
3532 417a6e49 2020-09-26 stsp
3533 417a6e49 2020-09-26 stsp return err;
3534 417a6e49 2020-09-26 stsp }
3535 417a6e49 2020-09-26 stsp
3536 417a6e49 2020-09-26 stsp static const struct got_error *
3537 417a6e49 2020-09-26 stsp cmd_log(int argc, char *argv[])
3538 417a6e49 2020-09-26 stsp {
3539 417a6e49 2020-09-26 stsp const struct got_error *error;
3540 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3541 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3542 417a6e49 2020-09-26 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3543 417a6e49 2020-09-26 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3544 417a6e49 2020-09-26 stsp const char *start_commit = NULL, *end_commit = NULL;
3545 417a6e49 2020-09-26 stsp const char *search_pattern = NULL;
3546 417a6e49 2020-09-26 stsp int diff_context = -1, ch;
3547 417a6e49 2020-09-26 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3548 417a6e49 2020-09-26 stsp int reverse_display_order = 0;
3549 417a6e49 2020-09-26 stsp const char *errstr;
3550 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
3551 417a6e49 2020-09-26 stsp
3552 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
3553 417a6e49 2020-09-26 stsp
3554 417a6e49 2020-09-26 stsp #ifndef PROFILE
3555 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3556 417a6e49 2020-09-26 stsp NULL)
3557 417a6e49 2020-09-26 stsp == -1)
3558 417a6e49 2020-09-26 stsp err(1, "pledge");
3559 417a6e49 2020-09-26 stsp #endif
3560 417a6e49 2020-09-26 stsp
3561 417a6e49 2020-09-26 stsp limit = get_default_log_limit();
3562 417a6e49 2020-09-26 stsp
3563 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3564 417a6e49 2020-09-26 stsp switch (ch) {
3565 417a6e49 2020-09-26 stsp case 'p':
3566 417a6e49 2020-09-26 stsp show_patch = 1;
3567 417a6e49 2020-09-26 stsp break;
3568 417a6e49 2020-09-26 stsp case 'P':
3569 417a6e49 2020-09-26 stsp show_changed_paths = 1;
3570 417a6e49 2020-09-26 stsp break;
3571 417a6e49 2020-09-26 stsp case 'c':
3572 417a6e49 2020-09-26 stsp start_commit = optarg;
3573 417a6e49 2020-09-26 stsp break;
3574 417a6e49 2020-09-26 stsp case 'C':
3575 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3576 417a6e49 2020-09-26 stsp &errstr);
3577 417a6e49 2020-09-26 stsp if (errstr != NULL)
3578 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3579 417a6e49 2020-09-26 stsp break;
3580 417a6e49 2020-09-26 stsp case 'l':
3581 417a6e49 2020-09-26 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3582 417a6e49 2020-09-26 stsp if (errstr != NULL)
3583 417a6e49 2020-09-26 stsp err(1, "-l option %s", errstr);
3584 417a6e49 2020-09-26 stsp break;
3585 417a6e49 2020-09-26 stsp case 'b':
3586 417a6e49 2020-09-26 stsp log_branches = 1;
3587 417a6e49 2020-09-26 stsp break;
3588 417a6e49 2020-09-26 stsp case 'r':
3589 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3590 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3591 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3592 417a6e49 2020-09-26 stsp optarg);
3593 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3594 417a6e49 2020-09-26 stsp break;
3595 417a6e49 2020-09-26 stsp case 'R':
3596 417a6e49 2020-09-26 stsp reverse_display_order = 1;
3597 417a6e49 2020-09-26 stsp break;
3598 417a6e49 2020-09-26 stsp case 's':
3599 417a6e49 2020-09-26 stsp search_pattern = optarg;
3600 417a6e49 2020-09-26 stsp break;
3601 417a6e49 2020-09-26 stsp case 'x':
3602 417a6e49 2020-09-26 stsp end_commit = optarg;
3603 417a6e49 2020-09-26 stsp break;
3604 417a6e49 2020-09-26 stsp default:
3605 417a6e49 2020-09-26 stsp usage_log();
3606 417a6e49 2020-09-26 stsp /* NOTREACHED */
3607 417a6e49 2020-09-26 stsp }
3608 417a6e49 2020-09-26 stsp }
3609 417a6e49 2020-09-26 stsp
3610 417a6e49 2020-09-26 stsp argc -= optind;
3611 417a6e49 2020-09-26 stsp argv += optind;
3612 417a6e49 2020-09-26 stsp
3613 417a6e49 2020-09-26 stsp if (diff_context == -1)
3614 417a6e49 2020-09-26 stsp diff_context = 3;
3615 417a6e49 2020-09-26 stsp else if (!show_patch)
3616 417a6e49 2020-09-26 stsp errx(1, "-C requires -p");
3617 417a6e49 2020-09-26 stsp
3618 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3619 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3620 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
3621 417a6e49 2020-09-26 stsp goto done;
3622 417a6e49 2020-09-26 stsp }
3623 417a6e49 2020-09-26 stsp
3624 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3625 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
3626 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3627 417a6e49 2020-09-26 stsp goto done;
3628 417a6e49 2020-09-26 stsp error = NULL;
3629 417a6e49 2020-09-26 stsp }
3630 417a6e49 2020-09-26 stsp
3631 417a6e49 2020-09-26 stsp if (argc == 0) {
3632 417a6e49 2020-09-26 stsp path = strdup("");
3633 417a6e49 2020-09-26 stsp if (path == NULL) {
3634 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3635 417a6e49 2020-09-26 stsp goto done;
3636 417a6e49 2020-09-26 stsp }
3637 417a6e49 2020-09-26 stsp } else if (argc == 1) {
3638 417a6e49 2020-09-26 stsp if (worktree) {
3639 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
3640 417a6e49 2020-09-26 stsp argv[0]);
3641 417a6e49 2020-09-26 stsp if (error)
3642 417a6e49 2020-09-26 stsp goto done;
3643 417a6e49 2020-09-26 stsp } else {
3644 417a6e49 2020-09-26 stsp path = strdup(argv[0]);
3645 417a6e49 2020-09-26 stsp if (path == NULL) {
3646 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3647 417a6e49 2020-09-26 stsp goto done;
3648 417a6e49 2020-09-26 stsp }
3649 417a6e49 2020-09-26 stsp }
3650 417a6e49 2020-09-26 stsp } else
3651 417a6e49 2020-09-26 stsp usage_log();
3652 417a6e49 2020-09-26 stsp
3653 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3654 417a6e49 2020-09-26 stsp repo_path = worktree ?
3655 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3656 417a6e49 2020-09-26 stsp }
3657 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3658 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3659 417a6e49 2020-09-26 stsp goto done;
3660 417a6e49 2020-09-26 stsp }
3661 417a6e49 2020-09-26 stsp
3662 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
3663 417a6e49 2020-09-26 stsp if (error != NULL)
3664 417a6e49 2020-09-26 stsp goto done;
3665 417a6e49 2020-09-26 stsp
3666 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3667 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3668 417a6e49 2020-09-26 stsp if (error)
3669 417a6e49 2020-09-26 stsp goto done;
3670 417a6e49 2020-09-26 stsp
3671 417a6e49 2020-09-26 stsp if (start_commit == NULL) {
3672 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
3673 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
3674 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
3675 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3676 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
3677 417a6e49 2020-09-26 stsp if (error != NULL)
3678 417a6e49 2020-09-26 stsp goto done;
3679 417a6e49 2020-09-26 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3680 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
3681 417a6e49 2020-09-26 stsp if (error != NULL)
3682 417a6e49 2020-09-26 stsp goto done;
3683 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
3684 417a6e49 2020-09-26 stsp start_id);
3685 417a6e49 2020-09-26 stsp if (error != NULL)
3686 417a6e49 2020-09-26 stsp goto done;
3687 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3688 417a6e49 2020-09-26 stsp } else {
3689 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3690 417a6e49 2020-09-26 stsp if (error != NULL)
3691 417a6e49 2020-09-26 stsp goto done;
3692 417a6e49 2020-09-26 stsp }
3693 417a6e49 2020-09-26 stsp if (end_commit != NULL) {
3694 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3695 417a6e49 2020-09-26 stsp if (error != NULL)
3696 417a6e49 2020-09-26 stsp goto done;
3697 417a6e49 2020-09-26 stsp }
3698 417a6e49 2020-09-26 stsp
3699 417a6e49 2020-09-26 stsp if (worktree) {
3700 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3701 417a6e49 2020-09-26 stsp char *p;
3702 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s", prefix,
3703 417a6e49 2020-09-26 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3704 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
3705 417a6e49 2020-09-26 stsp goto done;
3706 417a6e49 2020-09-26 stsp }
3707 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3708 417a6e49 2020-09-26 stsp free(p);
3709 417a6e49 2020-09-26 stsp } else
3710 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3711 417a6e49 2020-09-26 stsp if (error != NULL)
3712 417a6e49 2020-09-26 stsp goto done;
3713 417a6e49 2020-09-26 stsp if (in_repo_path) {
3714 417a6e49 2020-09-26 stsp free(path);
3715 417a6e49 2020-09-26 stsp path = in_repo_path;
3716 417a6e49 2020-09-26 stsp }
3717 417a6e49 2020-09-26 stsp
3718 417a6e49 2020-09-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3719 417a6e49 2020-09-26 stsp if (error)
3720 417a6e49 2020-09-26 stsp goto done;
3721 417a6e49 2020-09-26 stsp
3722 417a6e49 2020-09-26 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3723 417a6e49 2020-09-26 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3724 417a6e49 2020-09-26 stsp reverse_display_order, &refs);
3725 417a6e49 2020-09-26 stsp done:
3726 417a6e49 2020-09-26 stsp free(path);
3727 417a6e49 2020-09-26 stsp free(repo_path);
3728 417a6e49 2020-09-26 stsp free(cwd);
3729 417a6e49 2020-09-26 stsp if (worktree)
3730 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
3731 417a6e49 2020-09-26 stsp if (repo) {
3732 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
3733 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
3734 417a6e49 2020-09-26 stsp if (error == NULL)
3735 417a6e49 2020-09-26 stsp error = repo_error;
3736 417a6e49 2020-09-26 stsp }
3737 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
3738 417a6e49 2020-09-26 stsp return error;
3739 417a6e49 2020-09-26 stsp }
3740 417a6e49 2020-09-26 stsp
3741 417a6e49 2020-09-26 stsp __dead static void
3742 417a6e49 2020-09-26 stsp usage_diff(void)
3743 417a6e49 2020-09-26 stsp {
3744 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3745 417a6e49 2020-09-26 stsp "[-w] [object1 object2 | path]\n", getprogname());
3746 417a6e49 2020-09-26 stsp exit(1);
3747 417a6e49 2020-09-26 stsp }
3748 417a6e49 2020-09-26 stsp
3749 417a6e49 2020-09-26 stsp struct print_diff_arg {
3750 417a6e49 2020-09-26 stsp struct got_repository *repo;
3751 417a6e49 2020-09-26 stsp struct got_worktree *worktree;
3752 417a6e49 2020-09-26 stsp int diff_context;
3753 417a6e49 2020-09-26 stsp const char *id_str;
3754 417a6e49 2020-09-26 stsp int header_shown;
3755 417a6e49 2020-09-26 stsp int diff_staged;
3756 417a6e49 2020-09-26 stsp int ignore_whitespace;
3757 417a6e49 2020-09-26 stsp };
3758 417a6e49 2020-09-26 stsp
3759 417a6e49 2020-09-26 stsp /*
3760 417a6e49 2020-09-26 stsp * Create a file which contains the target path of a symlink so we can feed
3761 417a6e49 2020-09-26 stsp * it as content to the diff engine.
3762 417a6e49 2020-09-26 stsp */
3763 417a6e49 2020-09-26 stsp static const struct got_error *
3764 417a6e49 2020-09-26 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3765 417a6e49 2020-09-26 stsp const char *abspath)
3766 417a6e49 2020-09-26 stsp {
3767 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3768 417a6e49 2020-09-26 stsp char target_path[PATH_MAX];
3769 417a6e49 2020-09-26 stsp ssize_t target_len, outlen;
3770 417a6e49 2020-09-26 stsp
3771 417a6e49 2020-09-26 stsp *fd = -1;
3772 417a6e49 2020-09-26 stsp
3773 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3774 417a6e49 2020-09-26 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3775 417a6e49 2020-09-26 stsp if (target_len == -1)
3776 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlinkat", abspath);
3777 417a6e49 2020-09-26 stsp } else {
3778 417a6e49 2020-09-26 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3779 417a6e49 2020-09-26 stsp if (target_len == -1)
3780 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlink", abspath);
3781 417a6e49 2020-09-26 stsp }
3782 417a6e49 2020-09-26 stsp
3783 417a6e49 2020-09-26 stsp *fd = got_opentempfd();
3784 417a6e49 2020-09-26 stsp if (*fd == -1)
3785 417a6e49 2020-09-26 stsp return got_error_from_errno("got_opentempfd");
3786 417a6e49 2020-09-26 stsp
3787 417a6e49 2020-09-26 stsp outlen = write(*fd, target_path, target_len);
3788 417a6e49 2020-09-26 stsp if (outlen == -1) {
3789 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_opentempfd");
3790 417a6e49 2020-09-26 stsp goto done;
3791 417a6e49 2020-09-26 stsp }
3792 417a6e49 2020-09-26 stsp
3793 417a6e49 2020-09-26 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3794 417a6e49 2020-09-26 stsp err = got_error_from_errno2("lseek", abspath);
3795 417a6e49 2020-09-26 stsp goto done;
3796 417a6e49 2020-09-26 stsp }
3797 417a6e49 2020-09-26 stsp done:
3798 417a6e49 2020-09-26 stsp if (err) {
3799 417a6e49 2020-09-26 stsp close(*fd);
3800 417a6e49 2020-09-26 stsp *fd = -1;
3801 417a6e49 2020-09-26 stsp }
3802 417a6e49 2020-09-26 stsp return err;
3803 417a6e49 2020-09-26 stsp }
3804 417a6e49 2020-09-26 stsp
3805 417a6e49 2020-09-26 stsp static const struct got_error *
3806 417a6e49 2020-09-26 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3807 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
3808 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3809 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
3810 417a6e49 2020-09-26 stsp {
3811 417a6e49 2020-09-26 stsp struct print_diff_arg *a = arg;
3812 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3813 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL;
3814 417a6e49 2020-09-26 stsp int fd = -1;
3815 417a6e49 2020-09-26 stsp FILE *f2 = NULL;
3816 417a6e49 2020-09-26 stsp char *abspath = NULL, *label1 = NULL;
3817 417a6e49 2020-09-26 stsp struct stat sb;
3818 417a6e49 2020-09-26 stsp
3819 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3820 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_MODIFY &&
3821 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_ADD &&
3822 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
3823 417a6e49 2020-09-26 stsp return NULL;
3824 417a6e49 2020-09-26 stsp } else {
3825 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_DELETE)
3826 417a6e49 2020-09-26 stsp return NULL;
3827 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
3828 417a6e49 2020-09-26 stsp return got_error_set_errno(ENOENT, path);
3829 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_MODIFY &&
3830 417a6e49 2020-09-26 stsp status != GOT_STATUS_ADD &&
3831 417a6e49 2020-09-26 stsp status != GOT_STATUS_DELETE &&
3832 417a6e49 2020-09-26 stsp status != GOT_STATUS_CONFLICT)
3833 417a6e49 2020-09-26 stsp return NULL;
3834 417a6e49 2020-09-26 stsp }
3835 417a6e49 2020-09-26 stsp
3836 417a6e49 2020-09-26 stsp if (!a->header_shown) {
3837 417a6e49 2020-09-26 stsp printf("diff %s %s%s\n", a->id_str,
3838 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree),
3839 417a6e49 2020-09-26 stsp a->diff_staged ? " (staged changes)" : "");
3840 417a6e49 2020-09-26 stsp a->header_shown = 1;
3841 417a6e49 2020-09-26 stsp }
3842 417a6e49 2020-09-26 stsp
3843 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3844 417a6e49 2020-09-26 stsp const char *label1 = NULL, *label2 = NULL;
3845 417a6e49 2020-09-26 stsp switch (staged_status) {
3846 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
3847 417a6e49 2020-09-26 stsp label1 = path;
3848 417a6e49 2020-09-26 stsp label2 = path;
3849 417a6e49 2020-09-26 stsp break;
3850 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
3851 417a6e49 2020-09-26 stsp label2 = path;
3852 417a6e49 2020-09-26 stsp break;
3853 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
3854 417a6e49 2020-09-26 stsp label1 = path;
3855 417a6e49 2020-09-26 stsp break;
3856 417a6e49 2020-09-26 stsp default:
3857 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_FILE_STATUS);
3858 417a6e49 2020-09-26 stsp }
3859 417a6e49 2020-09-26 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3860 417a6e49 2020-09-26 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3861 417a6e49 2020-09-26 stsp a->repo, stdout);
3862 417a6e49 2020-09-26 stsp }
3863 417a6e49 2020-09-26 stsp
3864 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
3865 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY) {
3866 417a6e49 2020-09-26 stsp char *id_str;
3867 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3868 417a6e49 2020-09-26 stsp 8192);
3869 417a6e49 2020-09-26 stsp if (err)
3870 417a6e49 2020-09-26 stsp goto done;
3871 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
3872 417a6e49 2020-09-26 stsp if (err)
3873 417a6e49 2020-09-26 stsp goto done;
3874 417a6e49 2020-09-26 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3875 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3876 417a6e49 2020-09-26 stsp free(id_str);
3877 417a6e49 2020-09-26 stsp goto done;
3878 417a6e49 2020-09-26 stsp }
3879 417a6e49 2020-09-26 stsp free(id_str);
3880 417a6e49 2020-09-26 stsp } else if (status != GOT_STATUS_ADD) {
3881 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3882 417a6e49 2020-09-26 stsp if (err)
3883 417a6e49 2020-09-26 stsp goto done;
3884 417a6e49 2020-09-26 stsp }
3885 417a6e49 2020-09-26 stsp
3886 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_DELETE) {
3887 417a6e49 2020-09-26 stsp if (asprintf(&abspath, "%s/%s",
3888 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3889 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3890 417a6e49 2020-09-26 stsp goto done;
3891 417a6e49 2020-09-26 stsp }
3892 417a6e49 2020-09-26 stsp
3893 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3894 417a6e49 2020-09-26 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3895 417a6e49 2020-09-26 stsp if (fd == -1) {
3896 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3897 417a6e49 2020-09-26 stsp err = got_error_from_errno2("openat",
3898 417a6e49 2020-09-26 stsp abspath);
3899 417a6e49 2020-09-26 stsp goto done;
3900 417a6e49 2020-09-26 stsp }
3901 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3902 417a6e49 2020-09-26 stsp de_name, abspath);
3903 417a6e49 2020-09-26 stsp if (err)
3904 417a6e49 2020-09-26 stsp goto done;
3905 417a6e49 2020-09-26 stsp }
3906 417a6e49 2020-09-26 stsp } else {
3907 417a6e49 2020-09-26 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3908 417a6e49 2020-09-26 stsp if (fd == -1) {
3909 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3910 417a6e49 2020-09-26 stsp err = got_error_from_errno2("open",
3911 417a6e49 2020-09-26 stsp abspath);
3912 417a6e49 2020-09-26 stsp goto done;
3913 417a6e49 2020-09-26 stsp }
3914 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3915 417a6e49 2020-09-26 stsp de_name, abspath);
3916 417a6e49 2020-09-26 stsp if (err)
3917 417a6e49 2020-09-26 stsp goto done;
3918 417a6e49 2020-09-26 stsp }
3919 417a6e49 2020-09-26 stsp }
3920 417a6e49 2020-09-26 stsp if (fstat(fd, &sb) == -1) {
3921 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fstat", abspath);
3922 417a6e49 2020-09-26 stsp goto done;
3923 417a6e49 2020-09-26 stsp }
3924 417a6e49 2020-09-26 stsp f2 = fdopen(fd, "r");
3925 417a6e49 2020-09-26 stsp if (f2 == NULL) {
3926 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fdopen", abspath);
3927 417a6e49 2020-09-26 stsp goto done;
3928 417a6e49 2020-09-26 stsp }
3929 417a6e49 2020-09-26 stsp fd = -1;
3930 417a6e49 2020-09-26 stsp } else
3931 417a6e49 2020-09-26 stsp sb.st_size = 0;
3932 417a6e49 2020-09-26 stsp
3933 417a6e49 2020-09-26 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3934 417a6e49 2020-09-26 stsp a->diff_context, a->ignore_whitespace, stdout);
3935 417a6e49 2020-09-26 stsp done:
3936 417a6e49 2020-09-26 stsp if (blob1)
3937 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
3938 417a6e49 2020-09-26 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3939 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
3940 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3941 417a6e49 2020-09-26 stsp err = got_error_from_errno("close");
3942 417a6e49 2020-09-26 stsp free(abspath);
3943 417a6e49 2020-09-26 stsp return err;
3944 417a6e49 2020-09-26 stsp }
3945 417a6e49 2020-09-26 stsp
3946 417a6e49 2020-09-26 stsp static const struct got_error *
3947 417a6e49 2020-09-26 stsp cmd_diff(int argc, char *argv[])
3948 417a6e49 2020-09-26 stsp {
3949 417a6e49 2020-09-26 stsp const struct got_error *error;
3950 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3951 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3952 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
3953 417a6e49 2020-09-26 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3954 417a6e49 2020-09-26 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3955 417a6e49 2020-09-26 stsp char *label1 = NULL, *label2 = NULL;
3956 417a6e49 2020-09-26 stsp int type1, type2;
3957 417a6e49 2020-09-26 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3958 417a6e49 2020-09-26 stsp const char *errstr;
3959 417a6e49 2020-09-26 stsp char *path = NULL;
3960 417a6e49 2020-09-26 stsp
3961 417a6e49 2020-09-26 stsp #ifndef PROFILE
3962 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3963 417a6e49 2020-09-26 stsp NULL) == -1)
3964 417a6e49 2020-09-26 stsp err(1, "pledge");
3965 417a6e49 2020-09-26 stsp #endif
3966 417a6e49 2020-09-26 stsp
3967 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3968 417a6e49 2020-09-26 stsp switch (ch) {
3969 417a6e49 2020-09-26 stsp case 'C':
3970 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3971 417a6e49 2020-09-26 stsp &errstr);
3972 417a6e49 2020-09-26 stsp if (errstr != NULL)
3973 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3974 417a6e49 2020-09-26 stsp break;
3975 417a6e49 2020-09-26 stsp case 'r':
3976 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3977 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3978 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3979 417a6e49 2020-09-26 stsp optarg);
3980 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3981 417a6e49 2020-09-26 stsp break;
3982 417a6e49 2020-09-26 stsp case 's':
3983 417a6e49 2020-09-26 stsp diff_staged = 1;
3984 417a6e49 2020-09-26 stsp break;
3985 417a6e49 2020-09-26 stsp case 'w':
3986 417a6e49 2020-09-26 stsp ignore_whitespace = 1;
3987 417a6e49 2020-09-26 stsp break;
3988 417a6e49 2020-09-26 stsp default:
3989 417a6e49 2020-09-26 stsp usage_diff();
3990 417a6e49 2020-09-26 stsp /* NOTREACHED */
3991 417a6e49 2020-09-26 stsp }
3992 417a6e49 2020-09-26 stsp }
3993 417a6e49 2020-09-26 stsp
3994 417a6e49 2020-09-26 stsp argc -= optind;
3995 417a6e49 2020-09-26 stsp argv += optind;
3996 417a6e49 2020-09-26 stsp
3997 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3998 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3999 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4000 417a6e49 2020-09-26 stsp goto done;
4001 417a6e49 2020-09-26 stsp }
4002 417a6e49 2020-09-26 stsp if (argc <= 1) {
4003 417a6e49 2020-09-26 stsp if (repo_path)
4004 417a6e49 2020-09-26 stsp errx(1,
4005 417a6e49 2020-09-26 stsp "-r option can't be used when diffing a work tree");
4006 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4007 417a6e49 2020-09-26 stsp if (error) {
4008 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4009 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "diff",
4010 417a6e49 2020-09-26 stsp cwd);
4011 417a6e49 2020-09-26 stsp goto done;
4012 417a6e49 2020-09-26 stsp }
4013 417a6e49 2020-09-26 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4014 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4015 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4016 417a6e49 2020-09-26 stsp goto done;
4017 417a6e49 2020-09-26 stsp }
4018 417a6e49 2020-09-26 stsp if (argc == 1) {
4019 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
4020 417a6e49 2020-09-26 stsp argv[0]);
4021 417a6e49 2020-09-26 stsp if (error)
4022 417a6e49 2020-09-26 stsp goto done;
4023 417a6e49 2020-09-26 stsp } else {
4024 417a6e49 2020-09-26 stsp path = strdup("");
4025 417a6e49 2020-09-26 stsp if (path == NULL) {
4026 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4027 417a6e49 2020-09-26 stsp goto done;
4028 417a6e49 2020-09-26 stsp }
4029 417a6e49 2020-09-26 stsp }
4030 417a6e49 2020-09-26 stsp } else if (argc == 2) {
4031 417a6e49 2020-09-26 stsp if (diff_staged)
4032 417a6e49 2020-09-26 stsp errx(1, "-s option can't be used when diffing "
4033 417a6e49 2020-09-26 stsp "objects in repository");
4034 417a6e49 2020-09-26 stsp id_str1 = argv[0];
4035 417a6e49 2020-09-26 stsp id_str2 = argv[1];
4036 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4037 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4038 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4039 417a6e49 2020-09-26 stsp goto done;
4040 417a6e49 2020-09-26 stsp if (worktree) {
4041 417a6e49 2020-09-26 stsp repo_path = strdup(
4042 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
4043 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4044 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4045 417a6e49 2020-09-26 stsp goto done;
4046 417a6e49 2020-09-26 stsp }
4047 417a6e49 2020-09-26 stsp } else {
4048 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4049 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4050 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4051 417a6e49 2020-09-26 stsp goto done;
4052 417a6e49 2020-09-26 stsp }
4053 417a6e49 2020-09-26 stsp }
4054 417a6e49 2020-09-26 stsp }
4055 417a6e49 2020-09-26 stsp } else
4056 417a6e49 2020-09-26 stsp usage_diff();
4057 417a6e49 2020-09-26 stsp
4058 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4059 417a6e49 2020-09-26 stsp free(repo_path);
4060 417a6e49 2020-09-26 stsp if (error != NULL)
4061 417a6e49 2020-09-26 stsp goto done;
4062 417a6e49 2020-09-26 stsp
4063 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4064 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4065 417a6e49 2020-09-26 stsp if (error)
4066 417a6e49 2020-09-26 stsp goto done;
4067 417a6e49 2020-09-26 stsp
4068 417a6e49 2020-09-26 stsp if (argc <= 1) {
4069 417a6e49 2020-09-26 stsp struct print_diff_arg arg;
4070 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4071 417a6e49 2020-09-26 stsp char *id_str;
4072 417a6e49 2020-09-26 stsp
4073 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4074 417a6e49 2020-09-26 stsp
4075 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
4076 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
4077 417a6e49 2020-09-26 stsp if (error)
4078 417a6e49 2020-09-26 stsp goto done;
4079 417a6e49 2020-09-26 stsp arg.repo = repo;
4080 417a6e49 2020-09-26 stsp arg.worktree = worktree;
4081 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
4082 417a6e49 2020-09-26 stsp arg.id_str = id_str;
4083 417a6e49 2020-09-26 stsp arg.header_shown = 0;
4084 417a6e49 2020-09-26 stsp arg.diff_staged = diff_staged;
4085 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
4086 417a6e49 2020-09-26 stsp
4087 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, path, NULL);
4088 417a6e49 2020-09-26 stsp if (error)
4089 417a6e49 2020-09-26 stsp goto done;
4090 417a6e49 2020-09-26 stsp
4091 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4092 417a6e49 2020-09-26 stsp &arg, check_cancelled, NULL);
4093 417a6e49 2020-09-26 stsp free(id_str);
4094 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4095 417a6e49 2020-09-26 stsp goto done;
4096 417a6e49 2020-09-26 stsp }
4097 417a6e49 2020-09-26 stsp
4098 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4099 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4100 417a6e49 2020-09-26 stsp if (error)
4101 417a6e49 2020-09-26 stsp goto done;
4102 417a6e49 2020-09-26 stsp
4103 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4104 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4105 417a6e49 2020-09-26 stsp if (error)
4106 417a6e49 2020-09-26 stsp goto done;
4107 417a6e49 2020-09-26 stsp
4108 417a6e49 2020-09-26 stsp error = got_object_get_type(&type1, repo, id1);
4109 417a6e49 2020-09-26 stsp if (error)
4110 417a6e49 2020-09-26 stsp goto done;
4111 417a6e49 2020-09-26 stsp
4112 417a6e49 2020-09-26 stsp error = got_object_get_type(&type2, repo, id2);
4113 417a6e49 2020-09-26 stsp if (error)
4114 417a6e49 2020-09-26 stsp goto done;
4115 417a6e49 2020-09-26 stsp
4116 417a6e49 2020-09-26 stsp if (type1 != type2) {
4117 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4118 417a6e49 2020-09-26 stsp goto done;
4119 417a6e49 2020-09-26 stsp }
4120 417a6e49 2020-09-26 stsp
4121 417a6e49 2020-09-26 stsp switch (type1) {
4122 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
4123 417a6e49 2020-09-26 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4124 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4125 417a6e49 2020-09-26 stsp break;
4126 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
4127 417a6e49 2020-09-26 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4128 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4129 417a6e49 2020-09-26 stsp break;
4130 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
4131 417a6e49 2020-09-26 stsp printf("diff %s %s\n", label1, label2);
4132 417a6e49 2020-09-26 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4133 417a6e49 2020-09-26 stsp ignore_whitespace, repo, stdout);
4134 417a6e49 2020-09-26 stsp break;
4135 417a6e49 2020-09-26 stsp default:
4136 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4137 417a6e49 2020-09-26 stsp }
4138 417a6e49 2020-09-26 stsp done:
4139 417a6e49 2020-09-26 stsp free(label1);
4140 417a6e49 2020-09-26 stsp free(label2);
4141 417a6e49 2020-09-26 stsp free(id1);
4142 417a6e49 2020-09-26 stsp free(id2);
4143 417a6e49 2020-09-26 stsp free(path);
4144 417a6e49 2020-09-26 stsp if (worktree)
4145 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4146 417a6e49 2020-09-26 stsp if (repo) {
4147 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4148 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4149 417a6e49 2020-09-26 stsp if (error == NULL)
4150 417a6e49 2020-09-26 stsp error = repo_error;
4151 417a6e49 2020-09-26 stsp }
4152 417a6e49 2020-09-26 stsp return error;
4153 417a6e49 2020-09-26 stsp }
4154 417a6e49 2020-09-26 stsp
4155 417a6e49 2020-09-26 stsp __dead static void
4156 417a6e49 2020-09-26 stsp usage_blame(void)
4157 417a6e49 2020-09-26 stsp {
4158 417a6e49 2020-09-26 stsp fprintf(stderr,
4159 417a6e49 2020-09-26 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4160 417a6e49 2020-09-26 stsp getprogname());
4161 417a6e49 2020-09-26 stsp exit(1);
4162 417a6e49 2020-09-26 stsp }
4163 417a6e49 2020-09-26 stsp
4164 417a6e49 2020-09-26 stsp struct blame_line {
4165 417a6e49 2020-09-26 stsp int annotated;
4166 417a6e49 2020-09-26 stsp char *id_str;
4167 417a6e49 2020-09-26 stsp char *committer;
4168 417a6e49 2020-09-26 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4169 417a6e49 2020-09-26 stsp };
4170 417a6e49 2020-09-26 stsp
4171 417a6e49 2020-09-26 stsp struct blame_cb_args {
4172 417a6e49 2020-09-26 stsp struct blame_line *lines;
4173 417a6e49 2020-09-26 stsp int nlines;
4174 417a6e49 2020-09-26 stsp int nlines_prec;
4175 417a6e49 2020-09-26 stsp int lineno_cur;
4176 417a6e49 2020-09-26 stsp off_t *line_offsets;
4177 417a6e49 2020-09-26 stsp FILE *f;
4178 417a6e49 2020-09-26 stsp struct got_repository *repo;
4179 417a6e49 2020-09-26 stsp };
4180 417a6e49 2020-09-26 stsp
4181 417a6e49 2020-09-26 stsp static const struct got_error *
4182 417a6e49 2020-09-26 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4183 417a6e49 2020-09-26 stsp {
4184 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4185 417a6e49 2020-09-26 stsp struct blame_cb_args *a = arg;
4186 417a6e49 2020-09-26 stsp struct blame_line *bline;
4187 417a6e49 2020-09-26 stsp char *line = NULL;
4188 417a6e49 2020-09-26 stsp size_t linesize = 0;
4189 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
4190 417a6e49 2020-09-26 stsp off_t offset;
4191 417a6e49 2020-09-26 stsp struct tm tm;
4192 417a6e49 2020-09-26 stsp time_t committer_time;
4193 417a6e49 2020-09-26 stsp
4194 417a6e49 2020-09-26 stsp if (nlines != a->nlines ||
4195 417a6e49 2020-09-26 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4196 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_RANGE);
4197 417a6e49 2020-09-26 stsp
4198 417a6e49 2020-09-26 stsp if (sigint_received)
4199 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4200 417a6e49 2020-09-26 stsp
4201 417a6e49 2020-09-26 stsp if (lineno == -1)
4202 417a6e49 2020-09-26 stsp return NULL; /* no change in this commit */
4203 417a6e49 2020-09-26 stsp
4204 417a6e49 2020-09-26 stsp /* Annotate this line. */
4205 417a6e49 2020-09-26 stsp bline = &a->lines[lineno - 1];
4206 417a6e49 2020-09-26 stsp if (bline->annotated)
4207 417a6e49 2020-09-26 stsp return NULL;
4208 417a6e49 2020-09-26 stsp err = got_object_id_str(&bline->id_str, id);
4209 417a6e49 2020-09-26 stsp if (err)
4210 417a6e49 2020-09-26 stsp return err;
4211 417a6e49 2020-09-26 stsp
4212 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4213 417a6e49 2020-09-26 stsp if (err)
4214 417a6e49 2020-09-26 stsp goto done;
4215 417a6e49 2020-09-26 stsp
4216 417a6e49 2020-09-26 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4217 417a6e49 2020-09-26 stsp if (bline->committer == NULL) {
4218 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
4219 417a6e49 2020-09-26 stsp goto done;
4220 417a6e49 2020-09-26 stsp }
4221 417a6e49 2020-09-26 stsp
4222 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
4223 417a6e49 2020-09-26 stsp if (localtime_r(&committer_time, &tm) == NULL)
4224 417a6e49 2020-09-26 stsp return got_error_from_errno("localtime_r");
4225 417a6e49 2020-09-26 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4226 417a6e49 2020-09-26 stsp &tm) >= sizeof(bline->datebuf)) {
4227 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
4228 417a6e49 2020-09-26 stsp goto done;
4229 417a6e49 2020-09-26 stsp }
4230 417a6e49 2020-09-26 stsp bline->annotated = 1;
4231 417a6e49 2020-09-26 stsp
4232 417a6e49 2020-09-26 stsp /* Print lines annotated so far. */
4233 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4234 417a6e49 2020-09-26 stsp if (!bline->annotated)
4235 417a6e49 2020-09-26 stsp goto done;
4236 417a6e49 2020-09-26 stsp
4237 417a6e49 2020-09-26 stsp offset = a->line_offsets[a->lineno_cur - 1];
4238 417a6e49 2020-09-26 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4239 417a6e49 2020-09-26 stsp err = got_error_from_errno("fseeko");
4240 417a6e49 2020-09-26 stsp goto done;
4241 417a6e49 2020-09-26 stsp }
4242 417a6e49 2020-09-26 stsp
4243 417a6e49 2020-09-26 stsp while (bline->annotated) {
4244 417a6e49 2020-09-26 stsp char *smallerthan, *at, *nl, *committer;
4245 417a6e49 2020-09-26 stsp size_t len;
4246 417a6e49 2020-09-26 stsp
4247 417a6e49 2020-09-26 stsp if (getline(&line, &linesize, a->f) == -1) {
4248 417a6e49 2020-09-26 stsp if (ferror(a->f))
4249 417a6e49 2020-09-26 stsp err = got_error_from_errno("getline");
4250 417a6e49 2020-09-26 stsp break;
4251 417a6e49 2020-09-26 stsp }
4252 417a6e49 2020-09-26 stsp
4253 417a6e49 2020-09-26 stsp committer = bline->committer;
4254 417a6e49 2020-09-26 stsp smallerthan = strchr(committer, '<');
4255 417a6e49 2020-09-26 stsp if (smallerthan && smallerthan[1] != '\0')
4256 417a6e49 2020-09-26 stsp committer = smallerthan + 1;
4257 417a6e49 2020-09-26 stsp at = strchr(committer, '@');
4258 417a6e49 2020-09-26 stsp if (at)
4259 417a6e49 2020-09-26 stsp *at = '\0';
4260 417a6e49 2020-09-26 stsp len = strlen(committer);
4261 417a6e49 2020-09-26 stsp if (len >= 9)
4262 417a6e49 2020-09-26 stsp committer[8] = '\0';
4263 417a6e49 2020-09-26 stsp
4264 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
4265 417a6e49 2020-09-26 stsp if (nl)
4266 417a6e49 2020-09-26 stsp *nl = '\0';
4267 417a6e49 2020-09-26 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4268 417a6e49 2020-09-26 stsp bline->id_str, bline->datebuf, committer, line);
4269 417a6e49 2020-09-26 stsp
4270 417a6e49 2020-09-26 stsp a->lineno_cur++;
4271 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4272 417a6e49 2020-09-26 stsp }
4273 417a6e49 2020-09-26 stsp done:
4274 417a6e49 2020-09-26 stsp if (commit)
4275 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
4276 417a6e49 2020-09-26 stsp free(line);
4277 417a6e49 2020-09-26 stsp return err;
4278 417a6e49 2020-09-26 stsp }
4279 417a6e49 2020-09-26 stsp
4280 417a6e49 2020-09-26 stsp static const struct got_error *
4281 417a6e49 2020-09-26 stsp cmd_blame(int argc, char *argv[])
4282 417a6e49 2020-09-26 stsp {
4283 417a6e49 2020-09-26 stsp const struct got_error *error;
4284 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4285 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4286 417a6e49 2020-09-26 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4287 417a6e49 2020-09-26 stsp char *link_target = NULL;
4288 417a6e49 2020-09-26 stsp struct got_object_id *obj_id = NULL;
4289 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4290 417a6e49 2020-09-26 stsp struct got_blob_object *blob = NULL;
4291 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4292 417a6e49 2020-09-26 stsp struct blame_cb_args bca;
4293 417a6e49 2020-09-26 stsp int ch, obj_type, i;
4294 417a6e49 2020-09-26 stsp size_t filesize;
4295 417a6e49 2020-09-26 stsp
4296 417a6e49 2020-09-26 stsp memset(&bca, 0, sizeof(bca));
4297 417a6e49 2020-09-26 stsp
4298 417a6e49 2020-09-26 stsp #ifndef PROFILE
4299 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4300 417a6e49 2020-09-26 stsp NULL) == -1)
4301 417a6e49 2020-09-26 stsp err(1, "pledge");
4302 417a6e49 2020-09-26 stsp #endif
4303 417a6e49 2020-09-26 stsp
4304 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4305 417a6e49 2020-09-26 stsp switch (ch) {
4306 417a6e49 2020-09-26 stsp case 'c':
4307 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4308 417a6e49 2020-09-26 stsp break;
4309 417a6e49 2020-09-26 stsp case 'r':
4310 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4311 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4312 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4313 417a6e49 2020-09-26 stsp optarg);
4314 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4315 417a6e49 2020-09-26 stsp break;
4316 417a6e49 2020-09-26 stsp default:
4317 417a6e49 2020-09-26 stsp usage_blame();
4318 417a6e49 2020-09-26 stsp /* NOTREACHED */
4319 417a6e49 2020-09-26 stsp }
4320 417a6e49 2020-09-26 stsp }
4321 417a6e49 2020-09-26 stsp
4322 417a6e49 2020-09-26 stsp argc -= optind;
4323 417a6e49 2020-09-26 stsp argv += optind;
4324 417a6e49 2020-09-26 stsp
4325 417a6e49 2020-09-26 stsp if (argc == 1)
4326 417a6e49 2020-09-26 stsp path = argv[0];
4327 417a6e49 2020-09-26 stsp else
4328 417a6e49 2020-09-26 stsp usage_blame();
4329 417a6e49 2020-09-26 stsp
4330 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4331 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4332 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4333 417a6e49 2020-09-26 stsp goto done;
4334 417a6e49 2020-09-26 stsp }
4335 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4336 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4337 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4338 417a6e49 2020-09-26 stsp goto done;
4339 417a6e49 2020-09-26 stsp else
4340 417a6e49 2020-09-26 stsp error = NULL;
4341 417a6e49 2020-09-26 stsp if (worktree) {
4342 417a6e49 2020-09-26 stsp repo_path =
4343 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4344 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4345 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4346 417a6e49 2020-09-26 stsp if (error)
4347 417a6e49 2020-09-26 stsp goto done;
4348 417a6e49 2020-09-26 stsp }
4349 417a6e49 2020-09-26 stsp } else {
4350 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4351 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4352 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4353 417a6e49 2020-09-26 stsp goto done;
4354 417a6e49 2020-09-26 stsp }
4355 417a6e49 2020-09-26 stsp }
4356 417a6e49 2020-09-26 stsp }
4357 417a6e49 2020-09-26 stsp
4358 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4359 417a6e49 2020-09-26 stsp if (error != NULL)
4360 417a6e49 2020-09-26 stsp goto done;
4361 417a6e49 2020-09-26 stsp
4362 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4363 417a6e49 2020-09-26 stsp if (error)
4364 417a6e49 2020-09-26 stsp goto done;
4365 417a6e49 2020-09-26 stsp
4366 417a6e49 2020-09-26 stsp if (worktree) {
4367 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4368 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4369 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4370 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s%s%s",
4371 417a6e49 2020-09-26 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4372 417a6e49 2020-09-26 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4373 417a6e49 2020-09-26 stsp path) == -1) {
4374 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4375 417a6e49 2020-09-26 stsp goto done;
4376 417a6e49 2020-09-26 stsp }
4377 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4378 417a6e49 2020-09-26 stsp free(p);
4379 417a6e49 2020-09-26 stsp } else {
4380 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4381 417a6e49 2020-09-26 stsp }
4382 417a6e49 2020-09-26 stsp if (error)
4383 417a6e49 2020-09-26 stsp goto done;
4384 417a6e49 2020-09-26 stsp
4385 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4386 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4387 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, worktree ?
4388 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4389 417a6e49 2020-09-26 stsp if (error != NULL)
4390 417a6e49 2020-09-26 stsp goto done;
4391 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4392 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4393 417a6e49 2020-09-26 stsp if (error != NULL)
4394 417a6e49 2020-09-26 stsp goto done;
4395 417a6e49 2020-09-26 stsp } else {
4396 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4397 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4398 417a6e49 2020-09-26 stsp if (error)
4399 417a6e49 2020-09-26 stsp goto done;
4400 417a6e49 2020-09-26 stsp }
4401 417a6e49 2020-09-26 stsp
4402 417a6e49 2020-09-26 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4403 417a6e49 2020-09-26 stsp commit_id, repo);
4404 417a6e49 2020-09-26 stsp if (error)
4405 417a6e49 2020-09-26 stsp goto done;
4406 417a6e49 2020-09-26 stsp
4407 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4408 417a6e49 2020-09-26 stsp link_target ? link_target : in_repo_path);
4409 417a6e49 2020-09-26 stsp if (error)
4410 417a6e49 2020-09-26 stsp goto done;
4411 417a6e49 2020-09-26 stsp
4412 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4413 417a6e49 2020-09-26 stsp if (error)
4414 417a6e49 2020-09-26 stsp goto done;
4415 417a6e49 2020-09-26 stsp
4416 417a6e49 2020-09-26 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4417 417a6e49 2020-09-26 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4418 417a6e49 2020-09-26 stsp GOT_ERR_OBJ_TYPE);
4419 417a6e49 2020-09-26 stsp goto done;
4420 417a6e49 2020-09-26 stsp }
4421 417a6e49 2020-09-26 stsp
4422 417a6e49 2020-09-26 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4423 417a6e49 2020-09-26 stsp if (error)
4424 417a6e49 2020-09-26 stsp goto done;
4425 417a6e49 2020-09-26 stsp bca.f = got_opentemp();
4426 417a6e49 2020-09-26 stsp if (bca.f == NULL) {
4427 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_opentemp");
4428 417a6e49 2020-09-26 stsp goto done;
4429 417a6e49 2020-09-26 stsp }
4430 417a6e49 2020-09-26 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4431 417a6e49 2020-09-26 stsp &bca.line_offsets, bca.f, blob);
4432 417a6e49 2020-09-26 stsp if (error || bca.nlines == 0)
4433 417a6e49 2020-09-26 stsp goto done;
4434 417a6e49 2020-09-26 stsp
4435 417a6e49 2020-09-26 stsp /* Don't include \n at EOF in the blame line count. */
4436 417a6e49 2020-09-26 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4437 417a6e49 2020-09-26 stsp bca.nlines--;
4438 417a6e49 2020-09-26 stsp
4439 417a6e49 2020-09-26 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4440 417a6e49 2020-09-26 stsp if (bca.lines == NULL) {
4441 417a6e49 2020-09-26 stsp error = got_error_from_errno("calloc");
4442 417a6e49 2020-09-26 stsp goto done;
4443 417a6e49 2020-09-26 stsp }
4444 417a6e49 2020-09-26 stsp bca.lineno_cur = 1;
4445 417a6e49 2020-09-26 stsp bca.nlines_prec = 0;
4446 417a6e49 2020-09-26 stsp i = bca.nlines;
4447 417a6e49 2020-09-26 stsp while (i > 0) {
4448 417a6e49 2020-09-26 stsp i /= 10;
4449 417a6e49 2020-09-26 stsp bca.nlines_prec++;
4450 417a6e49 2020-09-26 stsp }
4451 417a6e49 2020-09-26 stsp bca.repo = repo;
4452 417a6e49 2020-09-26 stsp
4453 417a6e49 2020-09-26 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4454 417a6e49 2020-09-26 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4455 417a6e49 2020-09-26 stsp done:
4456 417a6e49 2020-09-26 stsp free(in_repo_path);
4457 417a6e49 2020-09-26 stsp free(link_target);
4458 417a6e49 2020-09-26 stsp free(repo_path);
4459 417a6e49 2020-09-26 stsp free(cwd);
4460 417a6e49 2020-09-26 stsp free(commit_id);
4461 417a6e49 2020-09-26 stsp free(obj_id);
4462 417a6e49 2020-09-26 stsp if (blob)
4463 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
4464 417a6e49 2020-09-26 stsp if (worktree)
4465 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4466 417a6e49 2020-09-26 stsp if (repo) {
4467 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4468 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4469 417a6e49 2020-09-26 stsp if (error == NULL)
4470 417a6e49 2020-09-26 stsp error = repo_error;
4471 417a6e49 2020-09-26 stsp }
4472 417a6e49 2020-09-26 stsp if (bca.lines) {
4473 417a6e49 2020-09-26 stsp for (i = 0; i < bca.nlines; i++) {
4474 417a6e49 2020-09-26 stsp struct blame_line *bline = &bca.lines[i];
4475 417a6e49 2020-09-26 stsp free(bline->id_str);
4476 417a6e49 2020-09-26 stsp free(bline->committer);
4477 417a6e49 2020-09-26 stsp }
4478 417a6e49 2020-09-26 stsp free(bca.lines);
4479 417a6e49 2020-09-26 stsp }
4480 417a6e49 2020-09-26 stsp free(bca.line_offsets);
4481 417a6e49 2020-09-26 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4482 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
4483 417a6e49 2020-09-26 stsp return error;
4484 417a6e49 2020-09-26 stsp }
4485 417a6e49 2020-09-26 stsp
4486 417a6e49 2020-09-26 stsp __dead static void
4487 417a6e49 2020-09-26 stsp usage_tree(void)
4488 417a6e49 2020-09-26 stsp {
4489 417a6e49 2020-09-26 stsp fprintf(stderr,
4490 417a6e49 2020-09-26 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4491 417a6e49 2020-09-26 stsp getprogname());
4492 417a6e49 2020-09-26 stsp exit(1);
4493 417a6e49 2020-09-26 stsp }
4494 417a6e49 2020-09-26 stsp
4495 417a6e49 2020-09-26 stsp static const struct got_error *
4496 417a6e49 2020-09-26 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4497 417a6e49 2020-09-26 stsp const char *root_path, struct got_repository *repo)
4498 417a6e49 2020-09-26 stsp {
4499 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4500 417a6e49 2020-09-26 stsp int is_root_path = (strcmp(path, root_path) == 0);
4501 417a6e49 2020-09-26 stsp const char *modestr = "";
4502 417a6e49 2020-09-26 stsp mode_t mode = got_tree_entry_get_mode(te);
4503 417a6e49 2020-09-26 stsp char *link_target = NULL;
4504 417a6e49 2020-09-26 stsp
4505 417a6e49 2020-09-26 stsp path += strlen(root_path);
4506 417a6e49 2020-09-26 stsp while (path[0] == '/')
4507 417a6e49 2020-09-26 stsp path++;
4508 417a6e49 2020-09-26 stsp
4509 417a6e49 2020-09-26 stsp if (got_object_tree_entry_is_submodule(te))
4510 417a6e49 2020-09-26 stsp modestr = "$";
4511 417a6e49 2020-09-26 stsp else if (S_ISLNK(mode)) {
4512 417a6e49 2020-09-26 stsp int i;
4513 417a6e49 2020-09-26 stsp
4514 417a6e49 2020-09-26 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4515 417a6e49 2020-09-26 stsp if (err)
4516 417a6e49 2020-09-26 stsp return err;
4517 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(link_target); i++) {
4518 417a6e49 2020-09-26 stsp if (!isprint((unsigned char)link_target[i]))
4519 417a6e49 2020-09-26 stsp link_target[i] = '?';
4520 417a6e49 2020-09-26 stsp }
4521 417a6e49 2020-09-26 stsp
4522 417a6e49 2020-09-26 stsp modestr = "@";
4523 417a6e49 2020-09-26 stsp }
4524 417a6e49 2020-09-26 stsp else if (S_ISDIR(mode))
4525 417a6e49 2020-09-26 stsp modestr = "/";
4526 417a6e49 2020-09-26 stsp else if (mode & S_IXUSR)
4527 417a6e49 2020-09-26 stsp modestr = "*";
4528 417a6e49 2020-09-26 stsp
4529 417a6e49 2020-09-26 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4530 417a6e49 2020-09-26 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4531 417a6e49 2020-09-26 stsp link_target ? " -> ": "", link_target ? link_target : "");
4532 417a6e49 2020-09-26 stsp
4533 417a6e49 2020-09-26 stsp free(link_target);
4534 417a6e49 2020-09-26 stsp return NULL;
4535 417a6e49 2020-09-26 stsp }
4536 417a6e49 2020-09-26 stsp
4537 417a6e49 2020-09-26 stsp static const struct got_error *
4538 417a6e49 2020-09-26 stsp print_tree(const char *path, struct got_object_id *commit_id,
4539 417a6e49 2020-09-26 stsp int show_ids, int recurse, const char *root_path,
4540 417a6e49 2020-09-26 stsp struct got_repository *repo)
4541 417a6e49 2020-09-26 stsp {
4542 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4543 417a6e49 2020-09-26 stsp struct got_object_id *tree_id = NULL;
4544 417a6e49 2020-09-26 stsp struct got_tree_object *tree = NULL;
4545 417a6e49 2020-09-26 stsp int nentries, i;
4546 417a6e49 2020-09-26 stsp
4547 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4548 417a6e49 2020-09-26 stsp if (err)
4549 417a6e49 2020-09-26 stsp goto done;
4550 417a6e49 2020-09-26 stsp
4551 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4552 417a6e49 2020-09-26 stsp if (err)
4553 417a6e49 2020-09-26 stsp goto done;
4554 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
4555 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
4556 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
4557 417a6e49 2020-09-26 stsp char *id = NULL;
4558 417a6e49 2020-09-26 stsp
4559 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
4560 417a6e49 2020-09-26 stsp break;
4561 417a6e49 2020-09-26 stsp
4562 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
4563 417a6e49 2020-09-26 stsp if (show_ids) {
4564 417a6e49 2020-09-26 stsp char *id_str;
4565 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
4566 417a6e49 2020-09-26 stsp got_tree_entry_get_id(te));
4567 417a6e49 2020-09-26 stsp if (err)
4568 417a6e49 2020-09-26 stsp goto done;
4569 417a6e49 2020-09-26 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4570 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4571 417a6e49 2020-09-26 stsp free(id_str);
4572 417a6e49 2020-09-26 stsp goto done;
4573 417a6e49 2020-09-26 stsp }
4574 417a6e49 2020-09-26 stsp free(id_str);
4575 417a6e49 2020-09-26 stsp }
4576 417a6e49 2020-09-26 stsp err = print_entry(te, id, path, root_path, repo);
4577 417a6e49 2020-09-26 stsp free(id);
4578 417a6e49 2020-09-26 stsp if (err)
4579 417a6e49 2020-09-26 stsp goto done;
4580 417a6e49 2020-09-26 stsp
4581 417a6e49 2020-09-26 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4582 417a6e49 2020-09-26 stsp char *child_path;
4583 417a6e49 2020-09-26 stsp if (asprintf(&child_path, "%s%s%s", path,
4584 417a6e49 2020-09-26 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4585 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te)) == -1) {
4586 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4587 417a6e49 2020-09-26 stsp goto done;
4588 417a6e49 2020-09-26 stsp }
4589 417a6e49 2020-09-26 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4590 417a6e49 2020-09-26 stsp root_path, repo);
4591 417a6e49 2020-09-26 stsp free(child_path);
4592 417a6e49 2020-09-26 stsp if (err)
4593 417a6e49 2020-09-26 stsp goto done;
4594 417a6e49 2020-09-26 stsp }
4595 417a6e49 2020-09-26 stsp }
4596 417a6e49 2020-09-26 stsp done:
4597 417a6e49 2020-09-26 stsp if (tree)
4598 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
4599 417a6e49 2020-09-26 stsp free(tree_id);
4600 417a6e49 2020-09-26 stsp return err;
4601 417a6e49 2020-09-26 stsp }
4602 417a6e49 2020-09-26 stsp
4603 417a6e49 2020-09-26 stsp static const struct got_error *
4604 417a6e49 2020-09-26 stsp cmd_tree(int argc, char *argv[])
4605 417a6e49 2020-09-26 stsp {
4606 417a6e49 2020-09-26 stsp const struct got_error *error;
4607 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4608 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4609 417a6e49 2020-09-26 stsp const char *path, *refname = NULL;
4610 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4611 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4612 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4613 417a6e49 2020-09-26 stsp int show_ids = 0, recurse = 0;
4614 417a6e49 2020-09-26 stsp int ch;
4615 417a6e49 2020-09-26 stsp
4616 417a6e49 2020-09-26 stsp #ifndef PROFILE
4617 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4618 417a6e49 2020-09-26 stsp NULL) == -1)
4619 417a6e49 2020-09-26 stsp err(1, "pledge");
4620 417a6e49 2020-09-26 stsp #endif
4621 417a6e49 2020-09-26 stsp
4622 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4623 417a6e49 2020-09-26 stsp switch (ch) {
4624 417a6e49 2020-09-26 stsp case 'c':
4625 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4626 417a6e49 2020-09-26 stsp break;
4627 417a6e49 2020-09-26 stsp case 'r':
4628 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4629 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4630 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4631 417a6e49 2020-09-26 stsp optarg);
4632 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4633 417a6e49 2020-09-26 stsp break;
4634 417a6e49 2020-09-26 stsp case 'i':
4635 417a6e49 2020-09-26 stsp show_ids = 1;
4636 417a6e49 2020-09-26 stsp break;
4637 417a6e49 2020-09-26 stsp case 'R':
4638 417a6e49 2020-09-26 stsp recurse = 1;
4639 417a6e49 2020-09-26 stsp break;
4640 417a6e49 2020-09-26 stsp default:
4641 417a6e49 2020-09-26 stsp usage_tree();
4642 417a6e49 2020-09-26 stsp /* NOTREACHED */
4643 417a6e49 2020-09-26 stsp }
4644 417a6e49 2020-09-26 stsp }
4645 417a6e49 2020-09-26 stsp
4646 417a6e49 2020-09-26 stsp argc -= optind;
4647 417a6e49 2020-09-26 stsp argv += optind;
4648 417a6e49 2020-09-26 stsp
4649 417a6e49 2020-09-26 stsp if (argc == 1)
4650 417a6e49 2020-09-26 stsp path = argv[0];
4651 417a6e49 2020-09-26 stsp else if (argc > 1)
4652 417a6e49 2020-09-26 stsp usage_tree();
4653 417a6e49 2020-09-26 stsp else
4654 417a6e49 2020-09-26 stsp path = NULL;
4655 417a6e49 2020-09-26 stsp
4656 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4657 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4658 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4659 417a6e49 2020-09-26 stsp goto done;
4660 417a6e49 2020-09-26 stsp }
4661 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4662 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4663 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4664 417a6e49 2020-09-26 stsp goto done;
4665 417a6e49 2020-09-26 stsp else
4666 417a6e49 2020-09-26 stsp error = NULL;
4667 417a6e49 2020-09-26 stsp if (worktree) {
4668 417a6e49 2020-09-26 stsp repo_path =
4669 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4670 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4671 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4672 417a6e49 2020-09-26 stsp if (error)
4673 417a6e49 2020-09-26 stsp goto done;
4674 417a6e49 2020-09-26 stsp } else {
4675 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4676 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4677 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4678 417a6e49 2020-09-26 stsp goto done;
4679 417a6e49 2020-09-26 stsp }
4680 417a6e49 2020-09-26 stsp }
4681 417a6e49 2020-09-26 stsp }
4682 417a6e49 2020-09-26 stsp
4683 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4684 417a6e49 2020-09-26 stsp if (error != NULL)
4685 417a6e49 2020-09-26 stsp goto done;
4686 417a6e49 2020-09-26 stsp
4687 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4688 417a6e49 2020-09-26 stsp if (error)
4689 417a6e49 2020-09-26 stsp goto done;
4690 417a6e49 2020-09-26 stsp
4691 417a6e49 2020-09-26 stsp if (path == NULL) {
4692 417a6e49 2020-09-26 stsp if (worktree) {
4693 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4694 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4695 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s/%s",
4696 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
4697 417a6e49 2020-09-26 stsp worktree_subdir) == -1) {
4698 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4699 417a6e49 2020-09-26 stsp goto done;
4700 417a6e49 2020-09-26 stsp }
4701 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4702 417a6e49 2020-09-26 stsp free(p);
4703 417a6e49 2020-09-26 stsp if (error)
4704 417a6e49 2020-09-26 stsp goto done;
4705 417a6e49 2020-09-26 stsp } else
4706 417a6e49 2020-09-26 stsp path = "/";
4707 417a6e49 2020-09-26 stsp }
4708 417a6e49 2020-09-26 stsp if (in_repo_path == NULL) {
4709 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4710 417a6e49 2020-09-26 stsp if (error != NULL)
4711 417a6e49 2020-09-26 stsp goto done;
4712 417a6e49 2020-09-26 stsp }
4713 417a6e49 2020-09-26 stsp
4714 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4715 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4716 417a6e49 2020-09-26 stsp if (worktree)
4717 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
4718 417a6e49 2020-09-26 stsp else
4719 417a6e49 2020-09-26 stsp refname = GOT_REF_HEAD;
4720 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, refname, 0);
4721 417a6e49 2020-09-26 stsp if (error != NULL)
4722 417a6e49 2020-09-26 stsp goto done;
4723 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4724 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4725 417a6e49 2020-09-26 stsp if (error != NULL)
4726 417a6e49 2020-09-26 stsp goto done;
4727 417a6e49 2020-09-26 stsp } else {
4728 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4729 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4730 417a6e49 2020-09-26 stsp if (error)
4731 417a6e49 2020-09-26 stsp goto done;
4732 417a6e49 2020-09-26 stsp }
4733 417a6e49 2020-09-26 stsp
4734 417a6e49 2020-09-26 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4735 417a6e49 2020-09-26 stsp in_repo_path, repo);
4736 417a6e49 2020-09-26 stsp done:
4737 417a6e49 2020-09-26 stsp free(in_repo_path);
4738 417a6e49 2020-09-26 stsp free(repo_path);
4739 417a6e49 2020-09-26 stsp free(cwd);
4740 417a6e49 2020-09-26 stsp free(commit_id);
4741 417a6e49 2020-09-26 stsp if (worktree)
4742 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4743 417a6e49 2020-09-26 stsp if (repo) {
4744 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4745 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4746 417a6e49 2020-09-26 stsp if (error == NULL)
4747 417a6e49 2020-09-26 stsp error = repo_error;
4748 417a6e49 2020-09-26 stsp }
4749 417a6e49 2020-09-26 stsp return error;
4750 417a6e49 2020-09-26 stsp }
4751 417a6e49 2020-09-26 stsp
4752 417a6e49 2020-09-26 stsp __dead static void
4753 417a6e49 2020-09-26 stsp usage_status(void)
4754 417a6e49 2020-09-26 stsp {
4755 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4756 417a6e49 2020-09-26 stsp getprogname());
4757 417a6e49 2020-09-26 stsp exit(1);
4758 417a6e49 2020-09-26 stsp }
4759 417a6e49 2020-09-26 stsp
4760 417a6e49 2020-09-26 stsp static const struct got_error *
4761 417a6e49 2020-09-26 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4762 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
4763 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4764 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
4765 417a6e49 2020-09-26 stsp {
4766 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4767 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
4768 417a6e49 2020-09-26 stsp if (arg) {
4769 417a6e49 2020-09-26 stsp char *status_codes = arg;
4770 417a6e49 2020-09-26 stsp size_t ncodes = strlen(status_codes);
4771 417a6e49 2020-09-26 stsp int i;
4772 417a6e49 2020-09-26 stsp for (i = 0; i < ncodes ; i++) {
4773 417a6e49 2020-09-26 stsp if (status == status_codes[i] ||
4774 417a6e49 2020-09-26 stsp staged_status == status_codes[i])
4775 417a6e49 2020-09-26 stsp break;
4776 417a6e49 2020-09-26 stsp }
4777 417a6e49 2020-09-26 stsp if (i == ncodes)
4778 417a6e49 2020-09-26 stsp return NULL;
4779 417a6e49 2020-09-26 stsp }
4780 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
4781 417a6e49 2020-09-26 stsp return NULL;
4782 417a6e49 2020-09-26 stsp }
4783 417a6e49 2020-09-26 stsp
4784 417a6e49 2020-09-26 stsp static const struct got_error *
4785 417a6e49 2020-09-26 stsp cmd_status(int argc, char *argv[])
4786 417a6e49 2020-09-26 stsp {
4787 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4788 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4789 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4790 417a6e49 2020-09-26 stsp char *cwd = NULL, *status_codes = NULL;;
4791 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4792 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
4793 417a6e49 2020-09-26 stsp int ch, i;
4794 417a6e49 2020-09-26 stsp
4795 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4796 417a6e49 2020-09-26 stsp
4797 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4798 417a6e49 2020-09-26 stsp switch (ch) {
4799 417a6e49 2020-09-26 stsp case 's':
4800 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
4801 417a6e49 2020-09-26 stsp switch (optarg[i]) {
4802 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
4803 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
4804 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
4805 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
4806 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
4807 417a6e49 2020-09-26 stsp case GOT_STATUS_OBSTRUCTED:
4808 417a6e49 2020-09-26 stsp case GOT_STATUS_UNVERSIONED:
4809 417a6e49 2020-09-26 stsp case GOT_STATUS_MODE_CHANGE:
4810 417a6e49 2020-09-26 stsp case GOT_STATUS_NONEXISTENT:
4811 417a6e49 2020-09-26 stsp break;
4812 417a6e49 2020-09-26 stsp default:
4813 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
4814 417a6e49 2020-09-26 stsp optarg[i]);
4815 417a6e49 2020-09-26 stsp }
4816 417a6e49 2020-09-26 stsp }
4817 417a6e49 2020-09-26 stsp status_codes = optarg;
4818 417a6e49 2020-09-26 stsp break;
4819 417a6e49 2020-09-26 stsp default:
4820 417a6e49 2020-09-26 stsp usage_status();
4821 417a6e49 2020-09-26 stsp /* NOTREACHED */
4822 417a6e49 2020-09-26 stsp }
4823 417a6e49 2020-09-26 stsp }
4824 417a6e49 2020-09-26 stsp
4825 417a6e49 2020-09-26 stsp argc -= optind;
4826 417a6e49 2020-09-26 stsp argv += optind;
4827 417a6e49 2020-09-26 stsp
4828 417a6e49 2020-09-26 stsp #ifndef PROFILE
4829 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4830 417a6e49 2020-09-26 stsp NULL) == -1)
4831 417a6e49 2020-09-26 stsp err(1, "pledge");
4832 417a6e49 2020-09-26 stsp #endif
4833 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4834 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4835 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4836 417a6e49 2020-09-26 stsp goto done;
4837 417a6e49 2020-09-26 stsp }
4838 417a6e49 2020-09-26 stsp
4839 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4840 417a6e49 2020-09-26 stsp if (error) {
4841 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4842 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
4843 417a6e49 2020-09-26 stsp goto done;
4844 417a6e49 2020-09-26 stsp }
4845 417a6e49 2020-09-26 stsp
4846 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4847 417a6e49 2020-09-26 stsp NULL);
4848 417a6e49 2020-09-26 stsp if (error != NULL)
4849 417a6e49 2020-09-26 stsp goto done;
4850 417a6e49 2020-09-26 stsp
4851 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4852 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
4853 417a6e49 2020-09-26 stsp if (error)
4854 417a6e49 2020-09-26 stsp goto done;
4855 417a6e49 2020-09-26 stsp
4856 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4857 417a6e49 2020-09-26 stsp if (error)
4858 417a6e49 2020-09-26 stsp goto done;
4859 417a6e49 2020-09-26 stsp
4860 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4861 417a6e49 2020-09-26 stsp status_codes, check_cancelled, NULL);
4862 417a6e49 2020-09-26 stsp done:
4863 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
4864 417a6e49 2020-09-26 stsp free((char *)pe->path);
4865 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4866 417a6e49 2020-09-26 stsp free(cwd);
4867 417a6e49 2020-09-26 stsp return error;
4868 417a6e49 2020-09-26 stsp }
4869 417a6e49 2020-09-26 stsp
4870 417a6e49 2020-09-26 stsp __dead static void
4871 417a6e49 2020-09-26 stsp usage_ref(void)
4872 417a6e49 2020-09-26 stsp {
4873 417a6e49 2020-09-26 stsp fprintf(stderr,
4874 417a6e49 2020-09-26 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4875 417a6e49 2020-09-26 stsp "[-d] [name]\n",
4876 417a6e49 2020-09-26 stsp getprogname());
4877 417a6e49 2020-09-26 stsp exit(1);
4878 417a6e49 2020-09-26 stsp }
4879 417a6e49 2020-09-26 stsp
4880 417a6e49 2020-09-26 stsp static const struct got_error *
4881 417a6e49 2020-09-26 stsp list_refs(struct got_repository *repo, const char *refname)
4882 417a6e49 2020-09-26 stsp {
4883 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
4884 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
4885 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
4886 417a6e49 2020-09-26 stsp
4887 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
4888 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4889 417a6e49 2020-09-26 stsp if (err)
4890 417a6e49 2020-09-26 stsp return err;
4891 417a6e49 2020-09-26 stsp
4892 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4893 417a6e49 2020-09-26 stsp char *refstr;
4894 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
4895 417a6e49 2020-09-26 stsp if (refstr == NULL)
4896 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
4897 417a6e49 2020-09-26 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4898 417a6e49 2020-09-26 stsp free(refstr);
4899 417a6e49 2020-09-26 stsp }
4900 417a6e49 2020-09-26 stsp
4901 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
4902 417a6e49 2020-09-26 stsp return NULL;
4903 417a6e49 2020-09-26 stsp }
4904 417a6e49 2020-09-26 stsp
4905 417a6e49 2020-09-26 stsp static const struct got_error *
4906 417a6e49 2020-09-26 stsp delete_ref(struct got_repository *repo, const char *refname)
4907 417a6e49 2020-09-26 stsp {
4908 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4909 417a6e49 2020-09-26 stsp struct got_reference *ref;
4910 417a6e49 2020-09-26 stsp
4911 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4912 417a6e49 2020-09-26 stsp if (err)
4913 417a6e49 2020-09-26 stsp return err;
4914 417a6e49 2020-09-26 stsp
4915 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
4916 417a6e49 2020-09-26 stsp got_ref_close(ref);
4917 417a6e49 2020-09-26 stsp return err;
4918 417a6e49 2020-09-26 stsp }
4919 417a6e49 2020-09-26 stsp
4920 417a6e49 2020-09-26 stsp static const struct got_error *
4921 417a6e49 2020-09-26 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4922 417a6e49 2020-09-26 stsp {
4923 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4924 417a6e49 2020-09-26 stsp struct got_object_id *id;
4925 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4926 417a6e49 2020-09-26 stsp
4927 417a6e49 2020-09-26 stsp /*
4928 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4929 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4930 417a6e49 2020-09-26 stsp * an unintended typo.
4931 417a6e49 2020-09-26 stsp */
4932 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4933 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4934 417a6e49 2020-09-26 stsp
4935 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4936 417a6e49 2020-09-26 stsp repo);
4937 417a6e49 2020-09-26 stsp if (err) {
4938 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
4939 417a6e49 2020-09-26 stsp
4940 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4941 417a6e49 2020-09-26 stsp return err;
4942 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4943 417a6e49 2020-09-26 stsp if (err)
4944 417a6e49 2020-09-26 stsp return err;
4945 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, target_ref);
4946 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4947 417a6e49 2020-09-26 stsp if (err)
4948 417a6e49 2020-09-26 stsp return err;
4949 417a6e49 2020-09-26 stsp }
4950 417a6e49 2020-09-26 stsp
4951 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, id);
4952 417a6e49 2020-09-26 stsp if (err)
4953 417a6e49 2020-09-26 stsp goto done;
4954 417a6e49 2020-09-26 stsp
4955 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4956 417a6e49 2020-09-26 stsp done:
4957 417a6e49 2020-09-26 stsp if (ref)
4958 417a6e49 2020-09-26 stsp got_ref_close(ref);
4959 417a6e49 2020-09-26 stsp free(id);
4960 417a6e49 2020-09-26 stsp return err;
4961 417a6e49 2020-09-26 stsp }
4962 417a6e49 2020-09-26 stsp
4963 417a6e49 2020-09-26 stsp static const struct got_error *
4964 417a6e49 2020-09-26 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4965 417a6e49 2020-09-26 stsp {
4966 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4967 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4968 417a6e49 2020-09-26 stsp struct got_reference *target_ref = NULL;
4969 417a6e49 2020-09-26 stsp
4970 417a6e49 2020-09-26 stsp /*
4971 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4972 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4973 417a6e49 2020-09-26 stsp * an unintended typo.
4974 417a6e49 2020-09-26 stsp */
4975 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4976 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4977 417a6e49 2020-09-26 stsp
4978 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4979 417a6e49 2020-09-26 stsp if (err)
4980 417a6e49 2020-09-26 stsp return err;
4981 417a6e49 2020-09-26 stsp
4982 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4983 417a6e49 2020-09-26 stsp if (err)
4984 417a6e49 2020-09-26 stsp goto done;
4985 417a6e49 2020-09-26 stsp
4986 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4987 417a6e49 2020-09-26 stsp done:
4988 417a6e49 2020-09-26 stsp if (target_ref)
4989 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4990 417a6e49 2020-09-26 stsp if (ref)
4991 417a6e49 2020-09-26 stsp got_ref_close(ref);
4992 417a6e49 2020-09-26 stsp return err;
4993 417a6e49 2020-09-26 stsp }
4994 417a6e49 2020-09-26 stsp
4995 417a6e49 2020-09-26 stsp static const struct got_error *
4996 417a6e49 2020-09-26 stsp cmd_ref(int argc, char *argv[])
4997 417a6e49 2020-09-26 stsp {
4998 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4999 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5000 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5001 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
5002 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_delete = 0;
5003 417a6e49 2020-09-26 stsp const char *obj_arg = NULL, *symref_target= NULL;
5004 417a6e49 2020-09-26 stsp char *refname = NULL;
5005 417a6e49 2020-09-26 stsp
5006 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5007 417a6e49 2020-09-26 stsp switch (ch) {
5008 417a6e49 2020-09-26 stsp case 'c':
5009 417a6e49 2020-09-26 stsp obj_arg = optarg;
5010 417a6e49 2020-09-26 stsp break;
5011 417a6e49 2020-09-26 stsp case 'd':
5012 417a6e49 2020-09-26 stsp do_delete = 1;
5013 417a6e49 2020-09-26 stsp break;
5014 417a6e49 2020-09-26 stsp case 'r':
5015 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5016 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5017 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5018 417a6e49 2020-09-26 stsp optarg);
5019 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5020 417a6e49 2020-09-26 stsp break;
5021 417a6e49 2020-09-26 stsp case 'l':
5022 417a6e49 2020-09-26 stsp do_list = 1;
5023 417a6e49 2020-09-26 stsp break;
5024 417a6e49 2020-09-26 stsp case 's':
5025 417a6e49 2020-09-26 stsp symref_target = optarg;
5026 417a6e49 2020-09-26 stsp break;
5027 417a6e49 2020-09-26 stsp default:
5028 417a6e49 2020-09-26 stsp usage_ref();
5029 417a6e49 2020-09-26 stsp /* NOTREACHED */
5030 417a6e49 2020-09-26 stsp }
5031 417a6e49 2020-09-26 stsp }
5032 417a6e49 2020-09-26 stsp
5033 417a6e49 2020-09-26 stsp if (obj_arg && do_list)
5034 417a6e49 2020-09-26 stsp errx(1, "-c and -l options are mutually exclusive");
5035 417a6e49 2020-09-26 stsp if (obj_arg && do_delete)
5036 417a6e49 2020-09-26 stsp errx(1, "-c and -d options are mutually exclusive");
5037 417a6e49 2020-09-26 stsp if (obj_arg && symref_target)
5038 417a6e49 2020-09-26 stsp errx(1, "-c and -s options are mutually exclusive");
5039 417a6e49 2020-09-26 stsp if (symref_target && do_delete)
5040 417a6e49 2020-09-26 stsp errx(1, "-s and -d options are mutually exclusive");
5041 417a6e49 2020-09-26 stsp if (symref_target && do_list)
5042 417a6e49 2020-09-26 stsp errx(1, "-s and -l options are mutually exclusive");
5043 417a6e49 2020-09-26 stsp if (do_delete && do_list)
5044 417a6e49 2020-09-26 stsp errx(1, "-d and -l options are mutually exclusive");
5045 417a6e49 2020-09-26 stsp
5046 417a6e49 2020-09-26 stsp argc -= optind;
5047 417a6e49 2020-09-26 stsp argv += optind;
5048 417a6e49 2020-09-26 stsp
5049 417a6e49 2020-09-26 stsp if (do_list) {
5050 417a6e49 2020-09-26 stsp if (argc != 0 && argc != 1)
5051 417a6e49 2020-09-26 stsp usage_ref();
5052 417a6e49 2020-09-26 stsp if (argc == 1) {
5053 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5054 417a6e49 2020-09-26 stsp if (refname == NULL) {
5055 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5056 417a6e49 2020-09-26 stsp goto done;
5057 417a6e49 2020-09-26 stsp }
5058 417a6e49 2020-09-26 stsp }
5059 417a6e49 2020-09-26 stsp } else {
5060 417a6e49 2020-09-26 stsp if (argc != 1)
5061 417a6e49 2020-09-26 stsp usage_ref();
5062 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5063 417a6e49 2020-09-26 stsp if (refname == NULL) {
5064 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5065 417a6e49 2020-09-26 stsp goto done;
5066 417a6e49 2020-09-26 stsp }
5067 417a6e49 2020-09-26 stsp }
5068 417a6e49 2020-09-26 stsp
5069 417a6e49 2020-09-26 stsp if (refname)
5070 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(refname);
5071 417a6e49 2020-09-26 stsp
5072 417a6e49 2020-09-26 stsp #ifndef PROFILE
5073 417a6e49 2020-09-26 stsp if (do_list) {
5074 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5075 417a6e49 2020-09-26 stsp NULL) == -1)
5076 417a6e49 2020-09-26 stsp err(1, "pledge");
5077 417a6e49 2020-09-26 stsp } else {
5078 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5079 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5080 417a6e49 2020-09-26 stsp err(1, "pledge");
5081 417a6e49 2020-09-26 stsp }
5082 417a6e49 2020-09-26 stsp #endif
5083 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5084 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5085 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5086 417a6e49 2020-09-26 stsp goto done;
5087 417a6e49 2020-09-26 stsp }
5088 417a6e49 2020-09-26 stsp
5089 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5090 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5091 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5092 417a6e49 2020-09-26 stsp goto done;
5093 417a6e49 2020-09-26 stsp else
5094 417a6e49 2020-09-26 stsp error = NULL;
5095 417a6e49 2020-09-26 stsp if (worktree) {
5096 417a6e49 2020-09-26 stsp repo_path =
5097 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5098 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5099 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5100 417a6e49 2020-09-26 stsp if (error)
5101 417a6e49 2020-09-26 stsp goto done;
5102 417a6e49 2020-09-26 stsp } else {
5103 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5104 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5105 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5106 417a6e49 2020-09-26 stsp goto done;
5107 417a6e49 2020-09-26 stsp }
5108 417a6e49 2020-09-26 stsp }
5109 417a6e49 2020-09-26 stsp }
5110 417a6e49 2020-09-26 stsp
5111 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5112 417a6e49 2020-09-26 stsp if (error != NULL)
5113 417a6e49 2020-09-26 stsp goto done;
5114 417a6e49 2020-09-26 stsp
5115 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5116 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5117 417a6e49 2020-09-26 stsp if (error)
5118 417a6e49 2020-09-26 stsp goto done;
5119 417a6e49 2020-09-26 stsp
5120 417a6e49 2020-09-26 stsp if (do_list)
5121 417a6e49 2020-09-26 stsp error = list_refs(repo, refname);
5122 417a6e49 2020-09-26 stsp else if (do_delete)
5123 417a6e49 2020-09-26 stsp error = delete_ref(repo, refname);
5124 417a6e49 2020-09-26 stsp else if (symref_target)
5125 417a6e49 2020-09-26 stsp error = add_symref(repo, refname, symref_target);
5126 417a6e49 2020-09-26 stsp else {
5127 417a6e49 2020-09-26 stsp if (obj_arg == NULL)
5128 417a6e49 2020-09-26 stsp usage_ref();
5129 417a6e49 2020-09-26 stsp error = add_ref(repo, refname, obj_arg);
5130 417a6e49 2020-09-26 stsp }
5131 417a6e49 2020-09-26 stsp done:
5132 417a6e49 2020-09-26 stsp free(refname);
5133 417a6e49 2020-09-26 stsp if (repo)
5134 417a6e49 2020-09-26 stsp got_repo_close(repo);
5135 417a6e49 2020-09-26 stsp if (worktree)
5136 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5137 417a6e49 2020-09-26 stsp free(cwd);
5138 417a6e49 2020-09-26 stsp free(repo_path);
5139 417a6e49 2020-09-26 stsp return error;
5140 417a6e49 2020-09-26 stsp }
5141 417a6e49 2020-09-26 stsp
5142 417a6e49 2020-09-26 stsp __dead static void
5143 417a6e49 2020-09-26 stsp usage_branch(void)
5144 417a6e49 2020-09-26 stsp {
5145 417a6e49 2020-09-26 stsp fprintf(stderr,
5146 417a6e49 2020-09-26 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5147 417a6e49 2020-09-26 stsp "[name]\n", getprogname());
5148 417a6e49 2020-09-26 stsp exit(1);
5149 417a6e49 2020-09-26 stsp }
5150 417a6e49 2020-09-26 stsp
5151 417a6e49 2020-09-26 stsp static const struct got_error *
5152 417a6e49 2020-09-26 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5153 417a6e49 2020-09-26 stsp struct got_reference *ref)
5154 417a6e49 2020-09-26 stsp {
5155 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5156 417a6e49 2020-09-26 stsp const char *refname, *marker = " ";
5157 417a6e49 2020-09-26 stsp char *refstr;
5158 417a6e49 2020-09-26 stsp
5159 417a6e49 2020-09-26 stsp refname = got_ref_get_name(ref);
5160 417a6e49 2020-09-26 stsp if (worktree && strcmp(refname,
5161 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5162 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
5163 417a6e49 2020-09-26 stsp
5164 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
5165 417a6e49 2020-09-26 stsp if (err)
5166 417a6e49 2020-09-26 stsp return err;
5167 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id,
5168 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5169 417a6e49 2020-09-26 stsp marker = "* ";
5170 417a6e49 2020-09-26 stsp else
5171 417a6e49 2020-09-26 stsp marker = "~ ";
5172 417a6e49 2020-09-26 stsp free(id);
5173 417a6e49 2020-09-26 stsp }
5174 417a6e49 2020-09-26 stsp
5175 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5176 417a6e49 2020-09-26 stsp refname += 11;
5177 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5178 417a6e49 2020-09-26 stsp refname += 18;
5179 417a6e49 2020-09-26 stsp
5180 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(ref);
5181 417a6e49 2020-09-26 stsp if (refstr == NULL)
5182 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
5183 417a6e49 2020-09-26 stsp
5184 417a6e49 2020-09-26 stsp printf("%s%s: %s\n", marker, refname, refstr);
5185 417a6e49 2020-09-26 stsp free(refstr);
5186 417a6e49 2020-09-26 stsp return NULL;
5187 417a6e49 2020-09-26 stsp }
5188 417a6e49 2020-09-26 stsp
5189 417a6e49 2020-09-26 stsp static const struct got_error *
5190 417a6e49 2020-09-26 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5191 417a6e49 2020-09-26 stsp {
5192 417a6e49 2020-09-26 stsp const char *refname;
5193 417a6e49 2020-09-26 stsp
5194 417a6e49 2020-09-26 stsp if (worktree == NULL)
5195 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5196 417a6e49 2020-09-26 stsp
5197 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
5198 417a6e49 2020-09-26 stsp
5199 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5200 417a6e49 2020-09-26 stsp refname += 11;
5201 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5202 417a6e49 2020-09-26 stsp refname += 18;
5203 417a6e49 2020-09-26 stsp
5204 417a6e49 2020-09-26 stsp printf("%s\n", refname);
5205 417a6e49 2020-09-26 stsp
5206 417a6e49 2020-09-26 stsp return NULL;
5207 417a6e49 2020-09-26 stsp }
5208 417a6e49 2020-09-26 stsp
5209 417a6e49 2020-09-26 stsp static const struct got_error *
5210 417a6e49 2020-09-26 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5211 417a6e49 2020-09-26 stsp {
5212 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5213 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5214 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5215 417a6e49 2020-09-26 stsp struct got_reference *temp_ref = NULL;
5216 417a6e49 2020-09-26 stsp int rebase_in_progress, histedit_in_progress;
5217 417a6e49 2020-09-26 stsp
5218 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5219 417a6e49 2020-09-26 stsp
5220 417a6e49 2020-09-26 stsp if (worktree) {
5221 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5222 417a6e49 2020-09-26 stsp worktree);
5223 417a6e49 2020-09-26 stsp if (err)
5224 417a6e49 2020-09-26 stsp return err;
5225 417a6e49 2020-09-26 stsp
5226 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5227 417a6e49 2020-09-26 stsp worktree);
5228 417a6e49 2020-09-26 stsp if (err)
5229 417a6e49 2020-09-26 stsp return err;
5230 417a6e49 2020-09-26 stsp
5231 417a6e49 2020-09-26 stsp if (rebase_in_progress || histedit_in_progress) {
5232 417a6e49 2020-09-26 stsp err = got_ref_open(&temp_ref, repo,
5233 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
5234 417a6e49 2020-09-26 stsp if (err)
5235 417a6e49 2020-09-26 stsp return err;
5236 417a6e49 2020-09-26 stsp list_branch(repo, worktree, temp_ref);
5237 417a6e49 2020-09-26 stsp got_ref_close(temp_ref);
5238 417a6e49 2020-09-26 stsp }
5239 417a6e49 2020-09-26 stsp }
5240 417a6e49 2020-09-26 stsp
5241 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/heads",
5242 417a6e49 2020-09-26 stsp got_ref_cmp_by_name, NULL);
5243 417a6e49 2020-09-26 stsp if (err)
5244 417a6e49 2020-09-26 stsp return err;
5245 417a6e49 2020-09-26 stsp
5246 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5247 417a6e49 2020-09-26 stsp list_branch(repo, worktree, re->ref);
5248 417a6e49 2020-09-26 stsp
5249 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5250 417a6e49 2020-09-26 stsp return NULL;
5251 417a6e49 2020-09-26 stsp }
5252 417a6e49 2020-09-26 stsp
5253 417a6e49 2020-09-26 stsp static const struct got_error *
5254 417a6e49 2020-09-26 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5255 417a6e49 2020-09-26 stsp const char *branch_name)
5256 417a6e49 2020-09-26 stsp {
5257 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5258 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5259 417a6e49 2020-09-26 stsp char *refname;
5260 417a6e49 2020-09-26 stsp
5261 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5262 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
5263 417a6e49 2020-09-26 stsp
5264 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5265 417a6e49 2020-09-26 stsp if (err)
5266 417a6e49 2020-09-26 stsp goto done;
5267 417a6e49 2020-09-26 stsp
5268 417a6e49 2020-09-26 stsp if (worktree &&
5269 417a6e49 2020-09-26 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5270 417a6e49 2020-09-26 stsp got_ref_get_name(ref)) == 0) {
5271 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5272 417a6e49 2020-09-26 stsp "will not delete this work tree's current branch");
5273 417a6e49 2020-09-26 stsp goto done;
5274 417a6e49 2020-09-26 stsp }
5275 417a6e49 2020-09-26 stsp
5276 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
5277 417a6e49 2020-09-26 stsp done:
5278 417a6e49 2020-09-26 stsp if (ref)
5279 417a6e49 2020-09-26 stsp got_ref_close(ref);
5280 417a6e49 2020-09-26 stsp free(refname);
5281 417a6e49 2020-09-26 stsp return err;
5282 417a6e49 2020-09-26 stsp }
5283 417a6e49 2020-09-26 stsp
5284 417a6e49 2020-09-26 stsp static const struct got_error *
5285 417a6e49 2020-09-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5286 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id)
5287 417a6e49 2020-09-26 stsp {
5288 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5289 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5290 417a6e49 2020-09-26 stsp char *base_refname = NULL, *refname = NULL;
5291 417a6e49 2020-09-26 stsp
5292 417a6e49 2020-09-26 stsp /*
5293 417a6e49 2020-09-26 stsp * Don't let the user create a branch name with a leading '-'.
5294 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5295 417a6e49 2020-09-26 stsp * an unintended typo.
5296 417a6e49 2020-09-26 stsp */
5297 417a6e49 2020-09-26 stsp if (branch_name[0] == '-')
5298 417a6e49 2020-09-26 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5299 417a6e49 2020-09-26 stsp
5300 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5301 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5302 417a6e49 2020-09-26 stsp goto done;
5303 417a6e49 2020-09-26 stsp }
5304 417a6e49 2020-09-26 stsp
5305 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5306 417a6e49 2020-09-26 stsp if (err == NULL) {
5307 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5308 417a6e49 2020-09-26 stsp goto done;
5309 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5310 417a6e49 2020-09-26 stsp goto done;
5311 417a6e49 2020-09-26 stsp
5312 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5313 417a6e49 2020-09-26 stsp if (err)
5314 417a6e49 2020-09-26 stsp goto done;
5315 417a6e49 2020-09-26 stsp
5316 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5317 417a6e49 2020-09-26 stsp done:
5318 417a6e49 2020-09-26 stsp if (ref)
5319 417a6e49 2020-09-26 stsp got_ref_close(ref);
5320 417a6e49 2020-09-26 stsp free(base_refname);
5321 417a6e49 2020-09-26 stsp free(refname);
5322 417a6e49 2020-09-26 stsp return err;
5323 417a6e49 2020-09-26 stsp }
5324 417a6e49 2020-09-26 stsp
5325 417a6e49 2020-09-26 stsp static const struct got_error *
5326 417a6e49 2020-09-26 stsp cmd_branch(int argc, char *argv[])
5327 417a6e49 2020-09-26 stsp {
5328 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5329 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5330 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5331 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
5332 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5333 417a6e49 2020-09-26 stsp const char *delref = NULL, *commit_id_arg = NULL;
5334 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5335 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
5336 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
5337 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
5338 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
5339 417a6e49 2020-09-26 stsp
5340 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
5341 417a6e49 2020-09-26 stsp
5342 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5343 417a6e49 2020-09-26 stsp switch (ch) {
5344 417a6e49 2020-09-26 stsp case 'c':
5345 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5346 417a6e49 2020-09-26 stsp break;
5347 417a6e49 2020-09-26 stsp case 'd':
5348 417a6e49 2020-09-26 stsp delref = optarg;
5349 417a6e49 2020-09-26 stsp break;
5350 417a6e49 2020-09-26 stsp case 'r':
5351 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5352 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5353 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5354 417a6e49 2020-09-26 stsp optarg);
5355 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5356 417a6e49 2020-09-26 stsp break;
5357 417a6e49 2020-09-26 stsp case 'l':
5358 417a6e49 2020-09-26 stsp do_list = 1;
5359 417a6e49 2020-09-26 stsp break;
5360 417a6e49 2020-09-26 stsp case 'n':
5361 417a6e49 2020-09-26 stsp do_update = 0;
5362 417a6e49 2020-09-26 stsp break;
5363 417a6e49 2020-09-26 stsp default:
5364 417a6e49 2020-09-26 stsp usage_branch();
5365 417a6e49 2020-09-26 stsp /* NOTREACHED */
5366 417a6e49 2020-09-26 stsp }
5367 417a6e49 2020-09-26 stsp }
5368 417a6e49 2020-09-26 stsp
5369 417a6e49 2020-09-26 stsp if (do_list && delref)
5370 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
5371 417a6e49 2020-09-26 stsp
5372 417a6e49 2020-09-26 stsp argc -= optind;
5373 417a6e49 2020-09-26 stsp argv += optind;
5374 417a6e49 2020-09-26 stsp
5375 417a6e49 2020-09-26 stsp if (!do_list && !delref && argc == 0)
5376 417a6e49 2020-09-26 stsp do_show = 1;
5377 417a6e49 2020-09-26 stsp
5378 417a6e49 2020-09-26 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5379 417a6e49 2020-09-26 stsp errx(1, "-c option can only be used when creating a branch");
5380 417a6e49 2020-09-26 stsp
5381 417a6e49 2020-09-26 stsp if (do_list || delref) {
5382 417a6e49 2020-09-26 stsp if (argc > 0)
5383 417a6e49 2020-09-26 stsp usage_branch();
5384 417a6e49 2020-09-26 stsp } else if (!do_show && argc != 1)
5385 417a6e49 2020-09-26 stsp usage_branch();
5386 417a6e49 2020-09-26 stsp
5387 417a6e49 2020-09-26 stsp #ifndef PROFILE
5388 417a6e49 2020-09-26 stsp if (do_list || do_show) {
5389 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5390 417a6e49 2020-09-26 stsp NULL) == -1)
5391 417a6e49 2020-09-26 stsp err(1, "pledge");
5392 417a6e49 2020-09-26 stsp } else {
5393 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5394 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5395 417a6e49 2020-09-26 stsp err(1, "pledge");
5396 417a6e49 2020-09-26 stsp }
5397 417a6e49 2020-09-26 stsp #endif
5398 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5399 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5400 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5401 417a6e49 2020-09-26 stsp goto done;
5402 417a6e49 2020-09-26 stsp }
5403 417a6e49 2020-09-26 stsp
5404 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5405 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5406 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5407 417a6e49 2020-09-26 stsp goto done;
5408 417a6e49 2020-09-26 stsp else
5409 417a6e49 2020-09-26 stsp error = NULL;
5410 417a6e49 2020-09-26 stsp if (worktree) {
5411 417a6e49 2020-09-26 stsp repo_path =
5412 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5413 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5414 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5415 417a6e49 2020-09-26 stsp if (error)
5416 417a6e49 2020-09-26 stsp goto done;
5417 417a6e49 2020-09-26 stsp } else {
5418 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5419 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5420 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5421 417a6e49 2020-09-26 stsp goto done;
5422 417a6e49 2020-09-26 stsp }
5423 417a6e49 2020-09-26 stsp }
5424 417a6e49 2020-09-26 stsp }
5425 417a6e49 2020-09-26 stsp
5426 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5427 417a6e49 2020-09-26 stsp if (error != NULL)
5428 417a6e49 2020-09-26 stsp goto done;
5429 417a6e49 2020-09-26 stsp
5430 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5431 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5432 417a6e49 2020-09-26 stsp if (error)
5433 417a6e49 2020-09-26 stsp goto done;
5434 417a6e49 2020-09-26 stsp
5435 417a6e49 2020-09-26 stsp if (do_show)
5436 417a6e49 2020-09-26 stsp error = show_current_branch(repo, worktree);
5437 417a6e49 2020-09-26 stsp else if (do_list)
5438 417a6e49 2020-09-26 stsp error = list_branches(repo, worktree);
5439 417a6e49 2020-09-26 stsp else if (delref)
5440 417a6e49 2020-09-26 stsp error = delete_branch(repo, worktree, delref);
5441 417a6e49 2020-09-26 stsp else {
5442 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL)
5443 417a6e49 2020-09-26 stsp commit_id_arg = worktree ?
5444 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) :
5445 417a6e49 2020-09-26 stsp GOT_REF_HEAD;
5446 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
5447 417a6e49 2020-09-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5448 417a6e49 2020-09-26 stsp if (error)
5449 417a6e49 2020-09-26 stsp goto done;
5450 417a6e49 2020-09-26 stsp error = add_branch(repo, argv[0], commit_id);
5451 417a6e49 2020-09-26 stsp if (error)
5452 417a6e49 2020-09-26 stsp goto done;
5453 417a6e49 2020-09-26 stsp if (worktree && do_update) {
5454 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
5455 417a6e49 2020-09-26 stsp char *branch_refname = NULL;
5456 417a6e49 2020-09-26 stsp
5457 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5458 417a6e49 2020-09-26 stsp if (error)
5459 417a6e49 2020-09-26 stsp goto done;
5460 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5461 417a6e49 2020-09-26 stsp worktree);
5462 417a6e49 2020-09-26 stsp if (error)
5463 417a6e49 2020-09-26 stsp goto done;
5464 417a6e49 2020-09-26 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5465 417a6e49 2020-09-26 stsp == -1) {
5466 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
5467 417a6e49 2020-09-26 stsp goto done;
5468 417a6e49 2020-09-26 stsp }
5469 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5470 417a6e49 2020-09-26 stsp free(branch_refname);
5471 417a6e49 2020-09-26 stsp if (error)
5472 417a6e49 2020-09-26 stsp goto done;
5473 417a6e49 2020-09-26 stsp error = switch_head_ref(ref, commit_id, worktree,
5474 417a6e49 2020-09-26 stsp repo);
5475 417a6e49 2020-09-26 stsp if (error)
5476 417a6e49 2020-09-26 stsp goto done;
5477 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5478 417a6e49 2020-09-26 stsp commit_id);
5479 417a6e49 2020-09-26 stsp if (error)
5480 417a6e49 2020-09-26 stsp goto done;
5481 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
5482 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths,
5483 417a6e49 2020-09-26 stsp repo, update_progress, &upa, check_cancelled,
5484 417a6e49 2020-09-26 stsp NULL);
5485 417a6e49 2020-09-26 stsp if (error)
5486 417a6e49 2020-09-26 stsp goto done;
5487 417a6e49 2020-09-26 stsp if (upa.did_something)
5488 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
5489 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
5490 417a6e49 2020-09-26 stsp }
5491 417a6e49 2020-09-26 stsp }
5492 417a6e49 2020-09-26 stsp done:
5493 417a6e49 2020-09-26 stsp if (ref)
5494 417a6e49 2020-09-26 stsp got_ref_close(ref);
5495 417a6e49 2020-09-26 stsp if (repo)
5496 417a6e49 2020-09-26 stsp got_repo_close(repo);
5497 417a6e49 2020-09-26 stsp if (worktree)
5498 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5499 417a6e49 2020-09-26 stsp free(cwd);
5500 417a6e49 2020-09-26 stsp free(repo_path);
5501 417a6e49 2020-09-26 stsp free(commit_id);
5502 417a6e49 2020-09-26 stsp free(commit_id_str);
5503 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
5504 417a6e49 2020-09-26 stsp free((char *)pe->path);
5505 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
5506 417a6e49 2020-09-26 stsp return error;
5507 417a6e49 2020-09-26 stsp }
5508 417a6e49 2020-09-26 stsp
5509 417a6e49 2020-09-26 stsp
5510 417a6e49 2020-09-26 stsp __dead static void
5511 417a6e49 2020-09-26 stsp usage_tag(void)
5512 417a6e49 2020-09-26 stsp {
5513 417a6e49 2020-09-26 stsp fprintf(stderr,
5514 417a6e49 2020-09-26 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5515 417a6e49 2020-09-26 stsp "[-m message] name\n", getprogname());
5516 417a6e49 2020-09-26 stsp exit(1);
5517 417a6e49 2020-09-26 stsp }
5518 417a6e49 2020-09-26 stsp
5519 417a6e49 2020-09-26 stsp #if 0
5520 417a6e49 2020-09-26 stsp static const struct got_error *
5521 417a6e49 2020-09-26 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5522 417a6e49 2020-09-26 stsp {
5523 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5524 417a6e49 2020-09-26 stsp struct got_reflist_entry *re, *se, *new;
5525 417a6e49 2020-09-26 stsp struct got_object_id *re_id, *se_id;
5526 417a6e49 2020-09-26 stsp struct got_tag_object *re_tag, *se_tag;
5527 417a6e49 2020-09-26 stsp time_t re_time, se_time;
5528 417a6e49 2020-09-26 stsp
5529 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5530 417a6e49 2020-09-26 stsp se = SIMPLEQ_FIRST(sorted);
5531 417a6e49 2020-09-26 stsp if (se == NULL) {
5532 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5533 417a6e49 2020-09-26 stsp if (err)
5534 417a6e49 2020-09-26 stsp return err;
5535 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5536 417a6e49 2020-09-26 stsp continue;
5537 417a6e49 2020-09-26 stsp } else {
5538 417a6e49 2020-09-26 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5539 417a6e49 2020-09-26 stsp if (err)
5540 417a6e49 2020-09-26 stsp break;
5541 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5542 417a6e49 2020-09-26 stsp free(re_id);
5543 417a6e49 2020-09-26 stsp if (err)
5544 417a6e49 2020-09-26 stsp break;
5545 417a6e49 2020-09-26 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5546 417a6e49 2020-09-26 stsp got_object_tag_close(re_tag);
5547 417a6e49 2020-09-26 stsp }
5548 417a6e49 2020-09-26 stsp
5549 417a6e49 2020-09-26 stsp while (se) {
5550 417a6e49 2020-09-26 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5551 417a6e49 2020-09-26 stsp if (err)
5552 417a6e49 2020-09-26 stsp break;
5553 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5554 417a6e49 2020-09-26 stsp free(se_id);
5555 417a6e49 2020-09-26 stsp if (err)
5556 417a6e49 2020-09-26 stsp break;
5557 417a6e49 2020-09-26 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5558 417a6e49 2020-09-26 stsp got_object_tag_close(se_tag);
5559 417a6e49 2020-09-26 stsp
5560 417a6e49 2020-09-26 stsp if (se_time > re_time) {
5561 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5562 417a6e49 2020-09-26 stsp if (err)
5563 417a6e49 2020-09-26 stsp return err;
5564 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5565 417a6e49 2020-09-26 stsp break;
5566 417a6e49 2020-09-26 stsp }
5567 417a6e49 2020-09-26 stsp se = SIMPLEQ_NEXT(se, entry);
5568 417a6e49 2020-09-26 stsp continue;
5569 417a6e49 2020-09-26 stsp }
5570 417a6e49 2020-09-26 stsp }
5571 417a6e49 2020-09-26 stsp done:
5572 417a6e49 2020-09-26 stsp return err;
5573 417a6e49 2020-09-26 stsp }
5574 417a6e49 2020-09-26 stsp #endif
5575 417a6e49 2020-09-26 stsp
5576 417a6e49 2020-09-26 stsp static const struct got_error *
5577 417a6e49 2020-09-26 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5578 417a6e49 2020-09-26 stsp {
5579 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5580 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5581 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5582 417a6e49 2020-09-26 stsp
5583 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5584 417a6e49 2020-09-26 stsp
5585 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5586 417a6e49 2020-09-26 stsp if (err)
5587 417a6e49 2020-09-26 stsp return err;
5588 417a6e49 2020-09-26 stsp
5589 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5590 417a6e49 2020-09-26 stsp const char *refname;
5591 417a6e49 2020-09-26 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5592 417a6e49 2020-09-26 stsp char datebuf[26];
5593 417a6e49 2020-09-26 stsp const char *tagger;
5594 417a6e49 2020-09-26 stsp time_t tagger_time;
5595 417a6e49 2020-09-26 stsp struct got_object_id *id;
5596 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
5597 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
5598 417a6e49 2020-09-26 stsp
5599 417a6e49 2020-09-26 stsp refname = got_ref_get_name(re->ref);
5600 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5601 417a6e49 2020-09-26 stsp continue;
5602 417a6e49 2020-09-26 stsp refname += 10;
5603 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
5604 417a6e49 2020-09-26 stsp if (refstr == NULL) {
5605 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_ref_to_str");
5606 417a6e49 2020-09-26 stsp break;
5607 417a6e49 2020-09-26 stsp }
5608 417a6e49 2020-09-26 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5609 417a6e49 2020-09-26 stsp free(refstr);
5610 417a6e49 2020-09-26 stsp
5611 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, re->ref);
5612 417a6e49 2020-09-26 stsp if (err)
5613 417a6e49 2020-09-26 stsp break;
5614 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
5615 417a6e49 2020-09-26 stsp if (err) {
5616 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5617 417a6e49 2020-09-26 stsp free(id);
5618 417a6e49 2020-09-26 stsp break;
5619 417a6e49 2020-09-26 stsp }
5620 417a6e49 2020-09-26 stsp /* "lightweight" tag */
5621 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
5622 417a6e49 2020-09-26 stsp if (err) {
5623 417a6e49 2020-09-26 stsp free(id);
5624 417a6e49 2020-09-26 stsp break;
5625 417a6e49 2020-09-26 stsp }
5626 417a6e49 2020-09-26 stsp tagger = got_object_commit_get_committer(commit);
5627 417a6e49 2020-09-26 stsp tagger_time =
5628 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit);
5629 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
5630 417a6e49 2020-09-26 stsp free(id);
5631 417a6e49 2020-09-26 stsp if (err)
5632 417a6e49 2020-09-26 stsp break;
5633 417a6e49 2020-09-26 stsp } else {
5634 417a6e49 2020-09-26 stsp free(id);
5635 417a6e49 2020-09-26 stsp tagger = got_object_tag_get_tagger(tag);
5636 417a6e49 2020-09-26 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5637 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
5638 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
5639 417a6e49 2020-09-26 stsp if (err)
5640 417a6e49 2020-09-26 stsp break;
5641 417a6e49 2020-09-26 stsp }
5642 417a6e49 2020-09-26 stsp printf("from: %s\n", tagger);
5643 417a6e49 2020-09-26 stsp datestr = get_datestr(&tagger_time, datebuf);
5644 417a6e49 2020-09-26 stsp if (datestr)
5645 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
5646 417a6e49 2020-09-26 stsp if (commit)
5647 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5648 417a6e49 2020-09-26 stsp else {
5649 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
5650 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
5651 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5652 417a6e49 2020-09-26 stsp id_str);
5653 417a6e49 2020-09-26 stsp break;
5654 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
5655 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5656 417a6e49 2020-09-26 stsp id_str);
5657 417a6e49 2020-09-26 stsp break;
5658 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
5659 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5660 417a6e49 2020-09-26 stsp id_str);
5661 417a6e49 2020-09-26 stsp break;
5662 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
5663 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5664 417a6e49 2020-09-26 stsp id_str);
5665 417a6e49 2020-09-26 stsp break;
5666 417a6e49 2020-09-26 stsp default:
5667 417a6e49 2020-09-26 stsp break;
5668 417a6e49 2020-09-26 stsp }
5669 417a6e49 2020-09-26 stsp }
5670 417a6e49 2020-09-26 stsp free(id_str);
5671 417a6e49 2020-09-26 stsp if (commit) {
5672 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5673 417a6e49 2020-09-26 stsp if (err)
5674 417a6e49 2020-09-26 stsp break;
5675 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
5676 417a6e49 2020-09-26 stsp } else {
5677 417a6e49 2020-09-26 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5678 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
5679 417a6e49 2020-09-26 stsp if (tagmsg0 == NULL) {
5680 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5681 417a6e49 2020-09-26 stsp break;
5682 417a6e49 2020-09-26 stsp }
5683 417a6e49 2020-09-26 stsp }
5684 417a6e49 2020-09-26 stsp
5685 417a6e49 2020-09-26 stsp tagmsg = tagmsg0;
5686 417a6e49 2020-09-26 stsp do {
5687 417a6e49 2020-09-26 stsp line = strsep(&tagmsg, "\n");
5688 417a6e49 2020-09-26 stsp if (line)
5689 417a6e49 2020-09-26 stsp printf(" %s\n", line);
5690 417a6e49 2020-09-26 stsp } while (line);
5691 417a6e49 2020-09-26 stsp free(tagmsg0);
5692 417a6e49 2020-09-26 stsp }
5693 417a6e49 2020-09-26 stsp
5694 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5695 417a6e49 2020-09-26 stsp return NULL;
5696 417a6e49 2020-09-26 stsp }
5697 417a6e49 2020-09-26 stsp
5698 417a6e49 2020-09-26 stsp static const struct got_error *
5699 417a6e49 2020-09-26 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5700 417a6e49 2020-09-26 stsp const char *tag_name, const char *repo_path)
5701 417a6e49 2020-09-26 stsp {
5702 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5703 417a6e49 2020-09-26 stsp char *template = NULL, *initial_content = NULL;
5704 417a6e49 2020-09-26 stsp char *editor = NULL;
5705 417a6e49 2020-09-26 stsp int initial_content_len;
5706 417a6e49 2020-09-26 stsp int fd = -1;
5707 417a6e49 2020-09-26 stsp
5708 417a6e49 2020-09-26 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5709 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5710 417a6e49 2020-09-26 stsp goto done;
5711 417a6e49 2020-09-26 stsp }
5712 417a6e49 2020-09-26 stsp
5713 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
5714 417a6e49 2020-09-26 stsp "\n# tagging commit %s as %s\n",
5715 417a6e49 2020-09-26 stsp commit_id_str, tag_name);
5716 417a6e49 2020-09-26 stsp if (initial_content_len == -1) {
5717 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5718 417a6e49 2020-09-26 stsp goto done;
5719 417a6e49 2020-09-26 stsp }
5720 417a6e49 2020-09-26 stsp
5721 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5722 417a6e49 2020-09-26 stsp if (err)
5723 417a6e49 2020-09-26 stsp goto done;
5724 417a6e49 2020-09-26 stsp
5725 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5726 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", *tagmsg_path);
5727 417a6e49 2020-09-26 stsp goto done;
5728 417a6e49 2020-09-26 stsp }
5729 417a6e49 2020-09-26 stsp
5730 417a6e49 2020-09-26 stsp err = get_editor(&editor);
5731 417a6e49 2020-09-26 stsp if (err)
5732 417a6e49 2020-09-26 stsp goto done;
5733 417a6e49 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5734 417a6e49 2020-09-26 stsp done:
5735 417a6e49 2020-09-26 stsp free(initial_content);
5736 417a6e49 2020-09-26 stsp free(template);
5737 417a6e49 2020-09-26 stsp free(editor);
5738 417a6e49 2020-09-26 stsp
5739 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5740 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", *tagmsg_path);
5741 417a6e49 2020-09-26 stsp
5742 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
5743 417a6e49 2020-09-26 stsp if (err == NULL)
5744 417a6e49 2020-09-26 stsp err = apply_unveil(repo_path, 0, NULL);
5745 417a6e49 2020-09-26 stsp if (err) {
5746 417a6e49 2020-09-26 stsp free(*tagmsg);
5747 417a6e49 2020-09-26 stsp *tagmsg = NULL;
5748 417a6e49 2020-09-26 stsp }
5749 417a6e49 2020-09-26 stsp return err;
5750 417a6e49 2020-09-26 stsp }
5751 417a6e49 2020-09-26 stsp
5752 417a6e49 2020-09-26 stsp static const struct got_error *
5753 417a6e49 2020-09-26 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5754 417a6e49 2020-09-26 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5755 417a6e49 2020-09-26 stsp {
5756 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5757 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5758 417a6e49 2020-09-26 stsp char *label = NULL, *commit_id_str = NULL;
5759 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5760 417a6e49 2020-09-26 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5761 417a6e49 2020-09-26 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5762 417a6e49 2020-09-26 stsp int preserve_tagmsg = 0;
5763 417a6e49 2020-09-26 stsp
5764 417a6e49 2020-09-26 stsp /*
5765 417a6e49 2020-09-26 stsp * Don't let the user create a tag name with a leading '-'.
5766 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5767 417a6e49 2020-09-26 stsp * an unintended typo.
5768 417a6e49 2020-09-26 stsp */
5769 417a6e49 2020-09-26 stsp if (tag_name[0] == '-')
5770 417a6e49 2020-09-26 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5771 417a6e49 2020-09-26 stsp
5772 417a6e49 2020-09-26 stsp err = get_author(&tagger, repo, worktree);
5773 417a6e49 2020-09-26 stsp if (err)
5774 417a6e49 2020-09-26 stsp return err;
5775 417a6e49 2020-09-26 stsp
5776 417a6e49 2020-09-26 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5777 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5778 417a6e49 2020-09-26 stsp if (err)
5779 417a6e49 2020-09-26 stsp goto done;
5780 417a6e49 2020-09-26 stsp
5781 417a6e49 2020-09-26 stsp err = got_object_id_str(&commit_id_str, commit_id);
5782 417a6e49 2020-09-26 stsp if (err)
5783 417a6e49 2020-09-26 stsp goto done;
5784 417a6e49 2020-09-26 stsp
5785 417a6e49 2020-09-26 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5786 417a6e49 2020-09-26 stsp refname = strdup(tag_name);
5787 417a6e49 2020-09-26 stsp if (refname == NULL) {
5788 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5789 417a6e49 2020-09-26 stsp goto done;
5790 417a6e49 2020-09-26 stsp }
5791 417a6e49 2020-09-26 stsp tag_name += 10;
5792 417a6e49 2020-09-26 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5793 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5794 417a6e49 2020-09-26 stsp goto done;
5795 417a6e49 2020-09-26 stsp }
5796 417a6e49 2020-09-26 stsp
5797 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5798 417a6e49 2020-09-26 stsp if (err == NULL) {
5799 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5800 417a6e49 2020-09-26 stsp goto done;
5801 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5802 417a6e49 2020-09-26 stsp goto done;
5803 417a6e49 2020-09-26 stsp
5804 417a6e49 2020-09-26 stsp if (tagmsg_arg == NULL) {
5805 417a6e49 2020-09-26 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5806 417a6e49 2020-09-26 stsp tag_name, got_repo_get_path(repo));
5807 417a6e49 2020-09-26 stsp if (err) {
5808 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5809 417a6e49 2020-09-26 stsp tagmsg_path != NULL)
5810 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5811 417a6e49 2020-09-26 stsp goto done;
5812 417a6e49 2020-09-26 stsp }
5813 417a6e49 2020-09-26 stsp }
5814 417a6e49 2020-09-26 stsp
5815 417a6e49 2020-09-26 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5816 417a6e49 2020-09-26 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5817 417a6e49 2020-09-26 stsp if (err) {
5818 417a6e49 2020-09-26 stsp if (tagmsg_path)
5819 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5820 417a6e49 2020-09-26 stsp goto done;
5821 417a6e49 2020-09-26 stsp }
5822 417a6e49 2020-09-26 stsp
5823 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, tag_id);
5824 417a6e49 2020-09-26 stsp if (err) {
5825 417a6e49 2020-09-26 stsp if (tagmsg_path)
5826 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5827 417a6e49 2020-09-26 stsp goto done;
5828 417a6e49 2020-09-26 stsp }
5829 417a6e49 2020-09-26 stsp
5830 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5831 417a6e49 2020-09-26 stsp if (err) {
5832 417a6e49 2020-09-26 stsp if (tagmsg_path)
5833 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5834 417a6e49 2020-09-26 stsp goto done;
5835 417a6e49 2020-09-26 stsp }
5836 417a6e49 2020-09-26 stsp
5837 417a6e49 2020-09-26 stsp err = got_object_id_str(&tag_id_str, tag_id);
5838 417a6e49 2020-09-26 stsp if (err) {
5839 417a6e49 2020-09-26 stsp if (tagmsg_path)
5840 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5841 417a6e49 2020-09-26 stsp goto done;
5842 417a6e49 2020-09-26 stsp }
5843 417a6e49 2020-09-26 stsp printf("Created tag %s\n", tag_id_str);
5844 417a6e49 2020-09-26 stsp done:
5845 417a6e49 2020-09-26 stsp if (preserve_tagmsg) {
5846 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5847 417a6e49 2020-09-26 stsp getprogname(), tagmsg_path);
5848 417a6e49 2020-09-26 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5849 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5850 417a6e49 2020-09-26 stsp free(tag_id_str);
5851 417a6e49 2020-09-26 stsp if (ref)
5852 417a6e49 2020-09-26 stsp got_ref_close(ref);
5853 417a6e49 2020-09-26 stsp free(commit_id);
5854 417a6e49 2020-09-26 stsp free(commit_id_str);
5855 417a6e49 2020-09-26 stsp free(refname);
5856 417a6e49 2020-09-26 stsp free(tagmsg);
5857 417a6e49 2020-09-26 stsp free(tagmsg_path);
5858 417a6e49 2020-09-26 stsp free(tagger);
5859 417a6e49 2020-09-26 stsp return err;
5860 417a6e49 2020-09-26 stsp }
5861 417a6e49 2020-09-26 stsp
5862 417a6e49 2020-09-26 stsp static const struct got_error *
5863 417a6e49 2020-09-26 stsp cmd_tag(int argc, char *argv[])
5864 417a6e49 2020-09-26 stsp {
5865 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5866 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5867 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5868 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5869 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL;
5870 417a6e49 2020-09-26 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5871 417a6e49 2020-09-26 stsp int ch, do_list = 0;
5872 417a6e49 2020-09-26 stsp
5873 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5874 417a6e49 2020-09-26 stsp switch (ch) {
5875 417a6e49 2020-09-26 stsp case 'c':
5876 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5877 417a6e49 2020-09-26 stsp break;
5878 417a6e49 2020-09-26 stsp case 'm':
5879 417a6e49 2020-09-26 stsp tagmsg = optarg;
5880 417a6e49 2020-09-26 stsp break;
5881 417a6e49 2020-09-26 stsp case 'r':
5882 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5883 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5884 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5885 417a6e49 2020-09-26 stsp optarg);
5886 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5887 417a6e49 2020-09-26 stsp break;
5888 417a6e49 2020-09-26 stsp case 'l':
5889 417a6e49 2020-09-26 stsp do_list = 1;
5890 417a6e49 2020-09-26 stsp break;
5891 417a6e49 2020-09-26 stsp default:
5892 417a6e49 2020-09-26 stsp usage_tag();
5893 417a6e49 2020-09-26 stsp /* NOTREACHED */
5894 417a6e49 2020-09-26 stsp }
5895 417a6e49 2020-09-26 stsp }
5896 417a6e49 2020-09-26 stsp
5897 417a6e49 2020-09-26 stsp argc -= optind;
5898 417a6e49 2020-09-26 stsp argv += optind;
5899 417a6e49 2020-09-26 stsp
5900 417a6e49 2020-09-26 stsp if (do_list) {
5901 417a6e49 2020-09-26 stsp if (commit_id_arg != NULL)
5902 417a6e49 2020-09-26 stsp errx(1,
5903 417a6e49 2020-09-26 stsp "-c option can only be used when creating a tag");
5904 417a6e49 2020-09-26 stsp if (tagmsg)
5905 417a6e49 2020-09-26 stsp errx(1, "-l and -m options are mutually exclusive");
5906 417a6e49 2020-09-26 stsp if (argc > 0)
5907 417a6e49 2020-09-26 stsp usage_tag();
5908 417a6e49 2020-09-26 stsp } else if (argc != 1)
5909 417a6e49 2020-09-26 stsp usage_tag();
5910 417a6e49 2020-09-26 stsp
5911 417a6e49 2020-09-26 stsp tag_name = argv[0];
5912 417a6e49 2020-09-26 stsp
5913 417a6e49 2020-09-26 stsp #ifndef PROFILE
5914 417a6e49 2020-09-26 stsp if (do_list) {
5915 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5916 417a6e49 2020-09-26 stsp NULL) == -1)
5917 417a6e49 2020-09-26 stsp err(1, "pledge");
5918 417a6e49 2020-09-26 stsp } else {
5919 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5920 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5921 417a6e49 2020-09-26 stsp err(1, "pledge");
5922 417a6e49 2020-09-26 stsp }
5923 417a6e49 2020-09-26 stsp #endif
5924 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5925 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5926 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5927 417a6e49 2020-09-26 stsp goto done;
5928 417a6e49 2020-09-26 stsp }
5929 417a6e49 2020-09-26 stsp
5930 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5931 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5932 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5933 417a6e49 2020-09-26 stsp goto done;
5934 417a6e49 2020-09-26 stsp else
5935 417a6e49 2020-09-26 stsp error = NULL;
5936 417a6e49 2020-09-26 stsp if (worktree) {
5937 417a6e49 2020-09-26 stsp repo_path =
5938 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5939 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5940 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5941 417a6e49 2020-09-26 stsp if (error)
5942 417a6e49 2020-09-26 stsp goto done;
5943 417a6e49 2020-09-26 stsp } else {
5944 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5945 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5946 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5947 417a6e49 2020-09-26 stsp goto done;
5948 417a6e49 2020-09-26 stsp }
5949 417a6e49 2020-09-26 stsp }
5950 417a6e49 2020-09-26 stsp }
5951 417a6e49 2020-09-26 stsp
5952 417a6e49 2020-09-26 stsp if (do_list) {
5953 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5954 417a6e49 2020-09-26 stsp if (error != NULL)
5955 417a6e49 2020-09-26 stsp goto done;
5956 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5957 417a6e49 2020-09-26 stsp if (error)
5958 417a6e49 2020-09-26 stsp goto done;
5959 417a6e49 2020-09-26 stsp error = list_tags(repo, worktree);
5960 417a6e49 2020-09-26 stsp } else {
5961 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
5962 417a6e49 2020-09-26 stsp if (error)
5963 417a6e49 2020-09-26 stsp goto done;
5964 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5965 417a6e49 2020-09-26 stsp if (error != NULL)
5966 417a6e49 2020-09-26 stsp goto done;
5967 417a6e49 2020-09-26 stsp
5968 417a6e49 2020-09-26 stsp if (tagmsg) {
5969 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5970 417a6e49 2020-09-26 stsp if (error)
5971 417a6e49 2020-09-26 stsp goto done;
5972 417a6e49 2020-09-26 stsp }
5973 417a6e49 2020-09-26 stsp
5974 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL) {
5975 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
5976 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
5977 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
5978 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5979 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
5980 417a6e49 2020-09-26 stsp if (error)
5981 417a6e49 2020-09-26 stsp goto done;
5982 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5983 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
5984 417a6e49 2020-09-26 stsp if (error)
5985 417a6e49 2020-09-26 stsp goto done;
5986 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5987 417a6e49 2020-09-26 stsp free(commit_id);
5988 417a6e49 2020-09-26 stsp if (error)
5989 417a6e49 2020-09-26 stsp goto done;
5990 417a6e49 2020-09-26 stsp }
5991 417a6e49 2020-09-26 stsp
5992 417a6e49 2020-09-26 stsp error = add_tag(repo, worktree, tag_name,
5993 417a6e49 2020-09-26 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5994 417a6e49 2020-09-26 stsp }
5995 417a6e49 2020-09-26 stsp done:
5996 417a6e49 2020-09-26 stsp if (repo)
5997 417a6e49 2020-09-26 stsp got_repo_close(repo);
5998 417a6e49 2020-09-26 stsp if (worktree)
5999 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6000 417a6e49 2020-09-26 stsp free(cwd);
6001 417a6e49 2020-09-26 stsp free(repo_path);
6002 417a6e49 2020-09-26 stsp free(gitconfig_path);
6003 417a6e49 2020-09-26 stsp free(commit_id_str);
6004 417a6e49 2020-09-26 stsp return error;
6005 417a6e49 2020-09-26 stsp }
6006 417a6e49 2020-09-26 stsp
6007 417a6e49 2020-09-26 stsp __dead static void
6008 417a6e49 2020-09-26 stsp usage_add(void)
6009 417a6e49 2020-09-26 stsp {
6010 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6011 417a6e49 2020-09-26 stsp getprogname());
6012 417a6e49 2020-09-26 stsp exit(1);
6013 417a6e49 2020-09-26 stsp }
6014 417a6e49 2020-09-26 stsp
6015 417a6e49 2020-09-26 stsp static const struct got_error *
6016 417a6e49 2020-09-26 stsp add_progress(void *arg, unsigned char status, const char *path)
6017 417a6e49 2020-09-26 stsp {
6018 417a6e49 2020-09-26 stsp while (path[0] == '/')
6019 417a6e49 2020-09-26 stsp path++;
6020 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6021 417a6e49 2020-09-26 stsp return NULL;
6022 417a6e49 2020-09-26 stsp }
6023 417a6e49 2020-09-26 stsp
6024 417a6e49 2020-09-26 stsp static const struct got_error *
6025 417a6e49 2020-09-26 stsp cmd_add(int argc, char *argv[])
6026 417a6e49 2020-09-26 stsp {
6027 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6028 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6029 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6030 417a6e49 2020-09-26 stsp char *cwd = NULL;
6031 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6032 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6033 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, no_ignores = 0;
6034 417a6e49 2020-09-26 stsp
6035 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6036 417a6e49 2020-09-26 stsp
6037 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "IR")) != -1) {
6038 417a6e49 2020-09-26 stsp switch (ch) {
6039 417a6e49 2020-09-26 stsp case 'I':
6040 417a6e49 2020-09-26 stsp no_ignores = 1;
6041 417a6e49 2020-09-26 stsp break;
6042 417a6e49 2020-09-26 stsp case 'R':
6043 417a6e49 2020-09-26 stsp can_recurse = 1;
6044 417a6e49 2020-09-26 stsp break;
6045 417a6e49 2020-09-26 stsp default:
6046 417a6e49 2020-09-26 stsp usage_add();
6047 417a6e49 2020-09-26 stsp /* NOTREACHED */
6048 417a6e49 2020-09-26 stsp }
6049 417a6e49 2020-09-26 stsp }
6050 417a6e49 2020-09-26 stsp
6051 417a6e49 2020-09-26 stsp argc -= optind;
6052 417a6e49 2020-09-26 stsp argv += optind;
6053 417a6e49 2020-09-26 stsp
6054 417a6e49 2020-09-26 stsp #ifndef PROFILE
6055 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6056 417a6e49 2020-09-26 stsp NULL) == -1)
6057 417a6e49 2020-09-26 stsp err(1, "pledge");
6058 417a6e49 2020-09-26 stsp #endif
6059 417a6e49 2020-09-26 stsp if (argc < 1)
6060 417a6e49 2020-09-26 stsp usage_add();
6061 417a6e49 2020-09-26 stsp
6062 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6063 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6064 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6065 417a6e49 2020-09-26 stsp goto done;
6066 417a6e49 2020-09-26 stsp }
6067 417a6e49 2020-09-26 stsp
6068 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6069 417a6e49 2020-09-26 stsp if (error) {
6070 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6071 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "add", cwd);
6072 417a6e49 2020-09-26 stsp goto done;
6073 417a6e49 2020-09-26 stsp }
6074 417a6e49 2020-09-26 stsp
6075 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6076 417a6e49 2020-09-26 stsp NULL);
6077 417a6e49 2020-09-26 stsp if (error != NULL)
6078 417a6e49 2020-09-26 stsp goto done;
6079 417a6e49 2020-09-26 stsp
6080 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6081 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6082 417a6e49 2020-09-26 stsp if (error)
6083 417a6e49 2020-09-26 stsp goto done;
6084 417a6e49 2020-09-26 stsp
6085 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6086 417a6e49 2020-09-26 stsp if (error)
6087 417a6e49 2020-09-26 stsp goto done;
6088 417a6e49 2020-09-26 stsp
6089 417a6e49 2020-09-26 stsp if (!can_recurse && no_ignores) {
6090 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6091 417a6e49 2020-09-26 stsp "disregarding ignores requires -R option");
6092 417a6e49 2020-09-26 stsp goto done;
6093 417a6e49 2020-09-26 stsp
6094 417a6e49 2020-09-26 stsp }
6095 417a6e49 2020-09-26 stsp
6096 417a6e49 2020-09-26 stsp if (!can_recurse) {
6097 417a6e49 2020-09-26 stsp char *ondisk_path;
6098 417a6e49 2020-09-26 stsp struct stat sb;
6099 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6100 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6101 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6102 417a6e49 2020-09-26 stsp pe->path) == -1) {
6103 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6104 417a6e49 2020-09-26 stsp goto done;
6105 417a6e49 2020-09-26 stsp }
6106 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6107 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6108 417a6e49 2020-09-26 stsp free(ondisk_path);
6109 417a6e49 2020-09-26 stsp continue;
6110 417a6e49 2020-09-26 stsp }
6111 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6112 417a6e49 2020-09-26 stsp ondisk_path);
6113 417a6e49 2020-09-26 stsp free(ondisk_path);
6114 417a6e49 2020-09-26 stsp goto done;
6115 417a6e49 2020-09-26 stsp }
6116 417a6e49 2020-09-26 stsp free(ondisk_path);
6117 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6118 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6119 417a6e49 2020-09-26 stsp "adding directories requires -R option");
6120 417a6e49 2020-09-26 stsp goto done;
6121 417a6e49 2020-09-26 stsp }
6122 417a6e49 2020-09-26 stsp }
6123 417a6e49 2020-09-26 stsp }
6124 417a6e49 2020-09-26 stsp
6125 417a6e49 2020-09-26 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6126 417a6e49 2020-09-26 stsp NULL, repo, no_ignores);
6127 417a6e49 2020-09-26 stsp done:
6128 417a6e49 2020-09-26 stsp if (repo)
6129 417a6e49 2020-09-26 stsp got_repo_close(repo);
6130 417a6e49 2020-09-26 stsp if (worktree)
6131 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6132 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6133 417a6e49 2020-09-26 stsp free((char *)pe->path);
6134 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6135 417a6e49 2020-09-26 stsp free(cwd);
6136 417a6e49 2020-09-26 stsp return error;
6137 417a6e49 2020-09-26 stsp }
6138 417a6e49 2020-09-26 stsp
6139 417a6e49 2020-09-26 stsp __dead static void
6140 417a6e49 2020-09-26 stsp usage_remove(void)
6141 417a6e49 2020-09-26 stsp {
6142 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6143 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6144 417a6e49 2020-09-26 stsp exit(1);
6145 417a6e49 2020-09-26 stsp }
6146 417a6e49 2020-09-26 stsp
6147 417a6e49 2020-09-26 stsp static const struct got_error *
6148 417a6e49 2020-09-26 stsp print_remove_status(void *arg, unsigned char status,
6149 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path)
6150 417a6e49 2020-09-26 stsp {
6151 417a6e49 2020-09-26 stsp while (path[0] == '/')
6152 417a6e49 2020-09-26 stsp path++;
6153 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
6154 417a6e49 2020-09-26 stsp return NULL;
6155 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6156 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
6157 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
6158 417a6e49 2020-09-26 stsp return NULL;
6159 417a6e49 2020-09-26 stsp }
6160 417a6e49 2020-09-26 stsp
6161 417a6e49 2020-09-26 stsp static const struct got_error *
6162 417a6e49 2020-09-26 stsp cmd_remove(int argc, char *argv[])
6163 417a6e49 2020-09-26 stsp {
6164 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6165 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6166 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6167 417a6e49 2020-09-26 stsp const char *status_codes = NULL;
6168 417a6e49 2020-09-26 stsp char *cwd = NULL;
6169 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6170 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6171 417a6e49 2020-09-26 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6172 417a6e49 2020-09-26 stsp
6173 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6174 417a6e49 2020-09-26 stsp
6175 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6176 417a6e49 2020-09-26 stsp switch (ch) {
6177 417a6e49 2020-09-26 stsp case 'f':
6178 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6179 417a6e49 2020-09-26 stsp break;
6180 417a6e49 2020-09-26 stsp case 'k':
6181 417a6e49 2020-09-26 stsp keep_on_disk = 1;
6182 417a6e49 2020-09-26 stsp break;
6183 417a6e49 2020-09-26 stsp case 'R':
6184 417a6e49 2020-09-26 stsp can_recurse = 1;
6185 417a6e49 2020-09-26 stsp break;
6186 417a6e49 2020-09-26 stsp case 's':
6187 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
6188 417a6e49 2020-09-26 stsp switch (optarg[i]) {
6189 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6190 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6191 417a6e49 2020-09-26 stsp break;
6192 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
6193 417a6e49 2020-09-26 stsp break;
6194 417a6e49 2020-09-26 stsp default:
6195 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
6196 417a6e49 2020-09-26 stsp optarg[i]);
6197 417a6e49 2020-09-26 stsp }
6198 417a6e49 2020-09-26 stsp }
6199 417a6e49 2020-09-26 stsp status_codes = optarg;
6200 417a6e49 2020-09-26 stsp break;
6201 417a6e49 2020-09-26 stsp default:
6202 417a6e49 2020-09-26 stsp usage_remove();
6203 417a6e49 2020-09-26 stsp /* NOTREACHED */
6204 417a6e49 2020-09-26 stsp }
6205 417a6e49 2020-09-26 stsp }
6206 417a6e49 2020-09-26 stsp
6207 417a6e49 2020-09-26 stsp argc -= optind;
6208 417a6e49 2020-09-26 stsp argv += optind;
6209 417a6e49 2020-09-26 stsp
6210 417a6e49 2020-09-26 stsp #ifndef PROFILE
6211 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6212 417a6e49 2020-09-26 stsp NULL) == -1)
6213 417a6e49 2020-09-26 stsp err(1, "pledge");
6214 417a6e49 2020-09-26 stsp #endif
6215 417a6e49 2020-09-26 stsp if (argc < 1)
6216 417a6e49 2020-09-26 stsp usage_remove();
6217 417a6e49 2020-09-26 stsp
6218 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6219 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6220 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6221 417a6e49 2020-09-26 stsp goto done;
6222 417a6e49 2020-09-26 stsp }
6223 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6224 417a6e49 2020-09-26 stsp if (error) {
6225 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6226 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6227 417a6e49 2020-09-26 stsp goto done;
6228 417a6e49 2020-09-26 stsp }
6229 417a6e49 2020-09-26 stsp
6230 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6231 417a6e49 2020-09-26 stsp NULL);
6232 417a6e49 2020-09-26 stsp if (error)
6233 417a6e49 2020-09-26 stsp goto done;
6234 417a6e49 2020-09-26 stsp
6235 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6236 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6237 417a6e49 2020-09-26 stsp if (error)
6238 417a6e49 2020-09-26 stsp goto done;
6239 417a6e49 2020-09-26 stsp
6240 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6241 417a6e49 2020-09-26 stsp if (error)
6242 417a6e49 2020-09-26 stsp goto done;
6243 417a6e49 2020-09-26 stsp
6244 417a6e49 2020-09-26 stsp if (!can_recurse) {
6245 417a6e49 2020-09-26 stsp char *ondisk_path;
6246 417a6e49 2020-09-26 stsp struct stat sb;
6247 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6248 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6249 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6250 417a6e49 2020-09-26 stsp pe->path) == -1) {
6251 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6252 417a6e49 2020-09-26 stsp goto done;
6253 417a6e49 2020-09-26 stsp }
6254 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6255 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6256 417a6e49 2020-09-26 stsp free(ondisk_path);
6257 417a6e49 2020-09-26 stsp continue;
6258 417a6e49 2020-09-26 stsp }
6259 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6260 417a6e49 2020-09-26 stsp ondisk_path);
6261 417a6e49 2020-09-26 stsp free(ondisk_path);
6262 417a6e49 2020-09-26 stsp goto done;
6263 417a6e49 2020-09-26 stsp }
6264 417a6e49 2020-09-26 stsp free(ondisk_path);
6265 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6266 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6267 417a6e49 2020-09-26 stsp "removing directories requires -R option");
6268 417a6e49 2020-09-26 stsp goto done;
6269 417a6e49 2020-09-26 stsp }
6270 417a6e49 2020-09-26 stsp }
6271 417a6e49 2020-09-26 stsp }
6272 417a6e49 2020-09-26 stsp
6273 417a6e49 2020-09-26 stsp error = got_worktree_schedule_delete(worktree, &paths,
6274 417a6e49 2020-09-26 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6275 417a6e49 2020-09-26 stsp repo, keep_on_disk);
6276 417a6e49 2020-09-26 stsp done:
6277 417a6e49 2020-09-26 stsp if (repo)
6278 417a6e49 2020-09-26 stsp got_repo_close(repo);
6279 417a6e49 2020-09-26 stsp if (worktree)
6280 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6281 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6282 417a6e49 2020-09-26 stsp free((char *)pe->path);
6283 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6284 417a6e49 2020-09-26 stsp free(cwd);
6285 417a6e49 2020-09-26 stsp return error;
6286 417a6e49 2020-09-26 stsp }
6287 417a6e49 2020-09-26 stsp
6288 417a6e49 2020-09-26 stsp __dead static void
6289 417a6e49 2020-09-26 stsp usage_revert(void)
6290 417a6e49 2020-09-26 stsp {
6291 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6292 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6293 417a6e49 2020-09-26 stsp exit(1);
6294 417a6e49 2020-09-26 stsp }
6295 417a6e49 2020-09-26 stsp
6296 417a6e49 2020-09-26 stsp static const struct got_error *
6297 417a6e49 2020-09-26 stsp revert_progress(void *arg, unsigned char status, const char *path)
6298 417a6e49 2020-09-26 stsp {
6299 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_UNVERSIONED)
6300 417a6e49 2020-09-26 stsp return NULL;
6301 417a6e49 2020-09-26 stsp
6302 417a6e49 2020-09-26 stsp while (path[0] == '/')
6303 417a6e49 2020-09-26 stsp path++;
6304 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6305 417a6e49 2020-09-26 stsp return NULL;
6306 417a6e49 2020-09-26 stsp }
6307 417a6e49 2020-09-26 stsp
6308 417a6e49 2020-09-26 stsp struct choose_patch_arg {
6309 417a6e49 2020-09-26 stsp FILE *patch_script_file;
6310 417a6e49 2020-09-26 stsp const char *action;
6311 417a6e49 2020-09-26 stsp };
6312 417a6e49 2020-09-26 stsp
6313 417a6e49 2020-09-26 stsp static const struct got_error *
6314 417a6e49 2020-09-26 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6315 417a6e49 2020-09-26 stsp int nchanges, const char *action)
6316 417a6e49 2020-09-26 stsp {
6317 417a6e49 2020-09-26 stsp char *line = NULL;
6318 417a6e49 2020-09-26 stsp size_t linesize = 0;
6319 417a6e49 2020-09-26 stsp ssize_t linelen;
6320 417a6e49 2020-09-26 stsp
6321 417a6e49 2020-09-26 stsp switch (status) {
6322 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
6323 417a6e49 2020-09-26 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6324 417a6e49 2020-09-26 stsp break;
6325 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
6326 417a6e49 2020-09-26 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6327 417a6e49 2020-09-26 stsp break;
6328 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6329 417a6e49 2020-09-26 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6330 417a6e49 2020-09-26 stsp return got_error_from_errno("fseek");
6331 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6332 417a6e49 2020-09-26 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6333 417a6e49 2020-09-26 stsp printf("%s", line);
6334 417a6e49 2020-09-26 stsp if (ferror(patch_file))
6335 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6336 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6337 417a6e49 2020-09-26 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6338 417a6e49 2020-09-26 stsp path, n, nchanges, action);
6339 417a6e49 2020-09-26 stsp break;
6340 417a6e49 2020-09-26 stsp default:
6341 417a6e49 2020-09-26 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6342 417a6e49 2020-09-26 stsp }
6343 417a6e49 2020-09-26 stsp
6344 417a6e49 2020-09-26 stsp return NULL;
6345 417a6e49 2020-09-26 stsp }
6346 417a6e49 2020-09-26 stsp
6347 417a6e49 2020-09-26 stsp static const struct got_error *
6348 417a6e49 2020-09-26 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6349 417a6e49 2020-09-26 stsp FILE *patch_file, int n, int nchanges)
6350 417a6e49 2020-09-26 stsp {
6351 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6352 417a6e49 2020-09-26 stsp char *line = NULL;
6353 417a6e49 2020-09-26 stsp size_t linesize = 0;
6354 417a6e49 2020-09-26 stsp ssize_t linelen;
6355 417a6e49 2020-09-26 stsp int resp = ' ';
6356 417a6e49 2020-09-26 stsp struct choose_patch_arg *a = arg;
6357 417a6e49 2020-09-26 stsp
6358 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NONE;
6359 417a6e49 2020-09-26 stsp
6360 417a6e49 2020-09-26 stsp if (a->patch_script_file) {
6361 417a6e49 2020-09-26 stsp char *nl;
6362 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6363 417a6e49 2020-09-26 stsp a->action);
6364 417a6e49 2020-09-26 stsp if (err)
6365 417a6e49 2020-09-26 stsp return err;
6366 417a6e49 2020-09-26 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6367 417a6e49 2020-09-26 stsp if (linelen == -1) {
6368 417a6e49 2020-09-26 stsp if (ferror(a->patch_script_file))
6369 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6370 417a6e49 2020-09-26 stsp return NULL;
6371 417a6e49 2020-09-26 stsp }
6372 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
6373 417a6e49 2020-09-26 stsp if (nl)
6374 417a6e49 2020-09-26 stsp *nl = '\0';
6375 417a6e49 2020-09-26 stsp if (strcmp(line, "y") == 0) {
6376 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6377 417a6e49 2020-09-26 stsp printf("y\n");
6378 417a6e49 2020-09-26 stsp } else if (strcmp(line, "n") == 0) {
6379 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6380 417a6e49 2020-09-26 stsp printf("n\n");
6381 417a6e49 2020-09-26 stsp } else if (strcmp(line, "q") == 0 &&
6382 417a6e49 2020-09-26 stsp status == GOT_STATUS_MODIFY) {
6383 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6384 417a6e49 2020-09-26 stsp printf("q\n");
6385 417a6e49 2020-09-26 stsp } else
6386 417a6e49 2020-09-26 stsp printf("invalid response '%s'\n", line);
6387 417a6e49 2020-09-26 stsp free(line);
6388 417a6e49 2020-09-26 stsp return NULL;
6389 417a6e49 2020-09-26 stsp }
6390 417a6e49 2020-09-26 stsp
6391 417a6e49 2020-09-26 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6392 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6393 417a6e49 2020-09-26 stsp a->action);
6394 417a6e49 2020-09-26 stsp if (err)
6395 417a6e49 2020-09-26 stsp return err;
6396 417a6e49 2020-09-26 stsp resp = getchar();
6397 417a6e49 2020-09-26 stsp if (resp == '\n')
6398 417a6e49 2020-09-26 stsp resp = getchar();
6399 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_MODIFY) {
6400 417a6e49 2020-09-26 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6401 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6402 417a6e49 2020-09-26 stsp resp = ' ';
6403 417a6e49 2020-09-26 stsp }
6404 417a6e49 2020-09-26 stsp } else if (resp != 'y' && resp != 'n') {
6405 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6406 417a6e49 2020-09-26 stsp resp = ' ';
6407 417a6e49 2020-09-26 stsp }
6408 417a6e49 2020-09-26 stsp }
6409 417a6e49 2020-09-26 stsp
6410 417a6e49 2020-09-26 stsp if (resp == 'y')
6411 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6412 417a6e49 2020-09-26 stsp else if (resp == 'n')
6413 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6414 417a6e49 2020-09-26 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6415 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6416 417a6e49 2020-09-26 stsp
6417 417a6e49 2020-09-26 stsp return NULL;
6418 417a6e49 2020-09-26 stsp }
6419 417a6e49 2020-09-26 stsp
6420 417a6e49 2020-09-26 stsp
6421 417a6e49 2020-09-26 stsp static const struct got_error *
6422 417a6e49 2020-09-26 stsp cmd_revert(int argc, char *argv[])
6423 417a6e49 2020-09-26 stsp {
6424 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6425 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6426 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6427 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
6428 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6429 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6430 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, pflag = 0;
6431 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
6432 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
6433 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
6434 417a6e49 2020-09-26 stsp
6435 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6436 417a6e49 2020-09-26 stsp
6437 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6438 417a6e49 2020-09-26 stsp switch (ch) {
6439 417a6e49 2020-09-26 stsp case 'p':
6440 417a6e49 2020-09-26 stsp pflag = 1;
6441 417a6e49 2020-09-26 stsp break;
6442 417a6e49 2020-09-26 stsp case 'F':
6443 417a6e49 2020-09-26 stsp patch_script_path = optarg;
6444 417a6e49 2020-09-26 stsp break;
6445 417a6e49 2020-09-26 stsp case 'R':
6446 417a6e49 2020-09-26 stsp can_recurse = 1;
6447 417a6e49 2020-09-26 stsp break;
6448 417a6e49 2020-09-26 stsp default:
6449 417a6e49 2020-09-26 stsp usage_revert();
6450 417a6e49 2020-09-26 stsp /* NOTREACHED */
6451 417a6e49 2020-09-26 stsp }
6452 417a6e49 2020-09-26 stsp }
6453 417a6e49 2020-09-26 stsp
6454 417a6e49 2020-09-26 stsp argc -= optind;
6455 417a6e49 2020-09-26 stsp argv += optind;
6456 417a6e49 2020-09-26 stsp
6457 417a6e49 2020-09-26 stsp #ifndef PROFILE
6458 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6459 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6460 417a6e49 2020-09-26 stsp err(1, "pledge");
6461 417a6e49 2020-09-26 stsp #endif
6462 417a6e49 2020-09-26 stsp if (argc < 1)
6463 417a6e49 2020-09-26 stsp usage_revert();
6464 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
6465 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
6466 417a6e49 2020-09-26 stsp
6467 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6468 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6469 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6470 417a6e49 2020-09-26 stsp goto done;
6471 417a6e49 2020-09-26 stsp }
6472 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6473 417a6e49 2020-09-26 stsp if (error) {
6474 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6475 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6476 417a6e49 2020-09-26 stsp goto done;
6477 417a6e49 2020-09-26 stsp }
6478 417a6e49 2020-09-26 stsp
6479 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6480 417a6e49 2020-09-26 stsp NULL);
6481 417a6e49 2020-09-26 stsp if (error != NULL)
6482 417a6e49 2020-09-26 stsp goto done;
6483 417a6e49 2020-09-26 stsp
6484 417a6e49 2020-09-26 stsp if (patch_script_path) {
6485 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
6486 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
6487 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
6488 417a6e49 2020-09-26 stsp patch_script_path);
6489 417a6e49 2020-09-26 stsp goto done;
6490 417a6e49 2020-09-26 stsp }
6491 417a6e49 2020-09-26 stsp }
6492 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6493 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6494 417a6e49 2020-09-26 stsp if (error)
6495 417a6e49 2020-09-26 stsp goto done;
6496 417a6e49 2020-09-26 stsp
6497 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6498 417a6e49 2020-09-26 stsp if (error)
6499 417a6e49 2020-09-26 stsp goto done;
6500 417a6e49 2020-09-26 stsp
6501 417a6e49 2020-09-26 stsp if (!can_recurse) {
6502 417a6e49 2020-09-26 stsp char *ondisk_path;
6503 417a6e49 2020-09-26 stsp struct stat sb;
6504 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6505 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6506 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6507 417a6e49 2020-09-26 stsp pe->path) == -1) {
6508 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6509 417a6e49 2020-09-26 stsp goto done;
6510 417a6e49 2020-09-26 stsp }
6511 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6512 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6513 417a6e49 2020-09-26 stsp free(ondisk_path);
6514 417a6e49 2020-09-26 stsp continue;
6515 417a6e49 2020-09-26 stsp }
6516 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6517 417a6e49 2020-09-26 stsp ondisk_path);
6518 417a6e49 2020-09-26 stsp free(ondisk_path);
6519 417a6e49 2020-09-26 stsp goto done;
6520 417a6e49 2020-09-26 stsp }
6521 417a6e49 2020-09-26 stsp free(ondisk_path);
6522 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6523 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6524 417a6e49 2020-09-26 stsp "reverting directories requires -R option");
6525 417a6e49 2020-09-26 stsp goto done;
6526 417a6e49 2020-09-26 stsp }
6527 417a6e49 2020-09-26 stsp }
6528 417a6e49 2020-09-26 stsp }
6529 417a6e49 2020-09-26 stsp
6530 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
6531 417a6e49 2020-09-26 stsp cpa.action = "revert";
6532 417a6e49 2020-09-26 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6533 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa, repo);
6534 417a6e49 2020-09-26 stsp done:
6535 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6536 417a6e49 2020-09-26 stsp error == NULL)
6537 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
6538 417a6e49 2020-09-26 stsp if (repo)
6539 417a6e49 2020-09-26 stsp got_repo_close(repo);
6540 417a6e49 2020-09-26 stsp if (worktree)
6541 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6542 417a6e49 2020-09-26 stsp free(path);
6543 417a6e49 2020-09-26 stsp free(cwd);
6544 417a6e49 2020-09-26 stsp return error;
6545 417a6e49 2020-09-26 stsp }
6546 417a6e49 2020-09-26 stsp
6547 417a6e49 2020-09-26 stsp __dead static void
6548 417a6e49 2020-09-26 stsp usage_commit(void)
6549 417a6e49 2020-09-26 stsp {
6550 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6551 417a6e49 2020-09-26 stsp getprogname());
6552 417a6e49 2020-09-26 stsp exit(1);
6553 417a6e49 2020-09-26 stsp }
6554 417a6e49 2020-09-26 stsp
6555 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg {
6556 417a6e49 2020-09-26 stsp const char *cmdline_log;
6557 417a6e49 2020-09-26 stsp const char *editor;
6558 417a6e49 2020-09-26 stsp const char *worktree_path;
6559 417a6e49 2020-09-26 stsp const char *branch_name;
6560 417a6e49 2020-09-26 stsp const char *repo_path;
6561 417a6e49 2020-09-26 stsp char *logmsg_path;
6562 417a6e49 2020-09-26 stsp
6563 417a6e49 2020-09-26 stsp };
6564 417a6e49 2020-09-26 stsp
6565 417a6e49 2020-09-26 stsp static const struct got_error *
6566 417a6e49 2020-09-26 stsp collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6567 417a6e49 2020-09-26 stsp void *arg)
6568 417a6e49 2020-09-26 stsp {
6569 417a6e49 2020-09-26 stsp char *initial_content = NULL;
6570 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6571 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6572 417a6e49 2020-09-26 stsp char *template = NULL;
6573 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg *a = arg;
6574 417a6e49 2020-09-26 stsp int initial_content_len;
6575 417a6e49 2020-09-26 stsp int fd = -1;
6576 417a6e49 2020-09-26 stsp size_t len;
6577 417a6e49 2020-09-26 stsp
6578 417a6e49 2020-09-26 stsp /* if a message was specified on the command line, just use it */
6579 417a6e49 2020-09-26 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6580 417a6e49 2020-09-26 stsp len = strlen(a->cmdline_log) + 1;
6581 417a6e49 2020-09-26 stsp *logmsg = malloc(len + 1);
6582 417a6e49 2020-09-26 stsp if (*logmsg == NULL)
6583 417a6e49 2020-09-26 stsp return got_error_from_errno("malloc");
6584 417a6e49 2020-09-26 stsp strlcpy(*logmsg, a->cmdline_log, len);
6585 417a6e49 2020-09-26 stsp return NULL;
6586 417a6e49 2020-09-26 stsp }
6587 417a6e49 2020-09-26 stsp
6588 417a6e49 2020-09-26 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6589 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6590 417a6e49 2020-09-26 stsp
6591 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
6592 417a6e49 2020-09-26 stsp "\n# changes to be committed on branch %s:\n",
6593 417a6e49 2020-09-26 stsp a->branch_name);
6594 417a6e49 2020-09-26 stsp if (initial_content_len == -1)
6595 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6596 417a6e49 2020-09-26 stsp
6597 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6598 417a6e49 2020-09-26 stsp if (err)
6599 417a6e49 2020-09-26 stsp goto done;
6600 417a6e49 2020-09-26 stsp
6601 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6602 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", a->logmsg_path);
6603 417a6e49 2020-09-26 stsp goto done;
6604 417a6e49 2020-09-26 stsp }
6605 417a6e49 2020-09-26 stsp
6606 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6607 417a6e49 2020-09-26 stsp struct got_commitable *ct = pe->data;
6608 417a6e49 2020-09-26 stsp dprintf(fd, "# %c %s\n",
6609 417a6e49 2020-09-26 stsp got_commitable_get_status(ct),
6610 417a6e49 2020-09-26 stsp got_commitable_get_path(ct));
6611 417a6e49 2020-09-26 stsp }
6612 417a6e49 2020-09-26 stsp
6613 417a6e49 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6614 417a6e49 2020-09-26 stsp done:
6615 417a6e49 2020-09-26 stsp free(initial_content);
6616 417a6e49 2020-09-26 stsp free(template);
6617 417a6e49 2020-09-26 stsp
6618 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6619 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", a->logmsg_path);
6620 417a6e49 2020-09-26 stsp
6621 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
6622 417a6e49 2020-09-26 stsp if (err == NULL)
6623 417a6e49 2020-09-26 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6624 417a6e49 2020-09-26 stsp if (err) {
6625 417a6e49 2020-09-26 stsp free(*logmsg);
6626 417a6e49 2020-09-26 stsp *logmsg = NULL;
6627 417a6e49 2020-09-26 stsp }
6628 417a6e49 2020-09-26 stsp return err;
6629 417a6e49 2020-09-26 stsp }
6630 417a6e49 2020-09-26 stsp
6631 417a6e49 2020-09-26 stsp static const struct got_error *
6632 417a6e49 2020-09-26 stsp cmd_commit(int argc, char *argv[])
6633 417a6e49 2020-09-26 stsp {
6634 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6635 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6636 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6637 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
6638 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
6639 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
6640 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg cl_arg;
6641 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6642 417a6e49 2020-09-26 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6643 417a6e49 2020-09-26 stsp int allow_bad_symlinks = 0;
6644 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6645 417a6e49 2020-09-26 stsp
6646 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6647 417a6e49 2020-09-26 stsp cl_arg.logmsg_path = NULL;
6648 417a6e49 2020-09-26 stsp
6649 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6650 417a6e49 2020-09-26 stsp switch (ch) {
6651 417a6e49 2020-09-26 stsp case 'm':
6652 417a6e49 2020-09-26 stsp logmsg = optarg;
6653 417a6e49 2020-09-26 stsp break;
6654 417a6e49 2020-09-26 stsp case 'S':
6655 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
6656 417a6e49 2020-09-26 stsp break;
6657 417a6e49 2020-09-26 stsp default:
6658 417a6e49 2020-09-26 stsp usage_commit();
6659 417a6e49 2020-09-26 stsp /* NOTREACHED */
6660 417a6e49 2020-09-26 stsp }
6661 417a6e49 2020-09-26 stsp }
6662 417a6e49 2020-09-26 stsp
6663 417a6e49 2020-09-26 stsp argc -= optind;
6664 417a6e49 2020-09-26 stsp argv += optind;
6665 417a6e49 2020-09-26 stsp
6666 417a6e49 2020-09-26 stsp #ifndef PROFILE
6667 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6668 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6669 417a6e49 2020-09-26 stsp err(1, "pledge");
6670 417a6e49 2020-09-26 stsp #endif
6671 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6672 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6673 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6674 417a6e49 2020-09-26 stsp goto done;
6675 417a6e49 2020-09-26 stsp }
6676 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6677 417a6e49 2020-09-26 stsp if (error) {
6678 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6679 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6680 417a6e49 2020-09-26 stsp goto done;
6681 417a6e49 2020-09-26 stsp }
6682 417a6e49 2020-09-26 stsp
6683 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6684 417a6e49 2020-09-26 stsp if (error)
6685 417a6e49 2020-09-26 stsp goto done;
6686 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
6687 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
6688 417a6e49 2020-09-26 stsp goto done;
6689 417a6e49 2020-09-26 stsp }
6690 417a6e49 2020-09-26 stsp
6691 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6692 417a6e49 2020-09-26 stsp worktree);
6693 417a6e49 2020-09-26 stsp if (error)
6694 417a6e49 2020-09-26 stsp goto done;
6695 417a6e49 2020-09-26 stsp
6696 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
6697 417a6e49 2020-09-26 stsp if (error)
6698 417a6e49 2020-09-26 stsp goto done;
6699 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6700 417a6e49 2020-09-26 stsp gitconfig_path);
6701 417a6e49 2020-09-26 stsp if (error != NULL)
6702 417a6e49 2020-09-26 stsp goto done;
6703 417a6e49 2020-09-26 stsp
6704 417a6e49 2020-09-26 stsp error = get_author(&author, repo, worktree);
6705 417a6e49 2020-09-26 stsp if (error)
6706 417a6e49 2020-09-26 stsp return error;
6707 417a6e49 2020-09-26 stsp
6708 417a6e49 2020-09-26 stsp /*
6709 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have
6710 417a6e49 2020-09-26 stsp * to apply unveil after the log message has been written.
6711 417a6e49 2020-09-26 stsp */
6712 417a6e49 2020-09-26 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6713 417a6e49 2020-09-26 stsp error = get_editor(&editor);
6714 417a6e49 2020-09-26 stsp else
6715 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6716 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6717 417a6e49 2020-09-26 stsp if (error)
6718 417a6e49 2020-09-26 stsp goto done;
6719 417a6e49 2020-09-26 stsp
6720 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6721 417a6e49 2020-09-26 stsp if (error)
6722 417a6e49 2020-09-26 stsp goto done;
6723 417a6e49 2020-09-26 stsp
6724 417a6e49 2020-09-26 stsp cl_arg.editor = editor;
6725 417a6e49 2020-09-26 stsp cl_arg.cmdline_log = logmsg;
6726 417a6e49 2020-09-26 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6727 417a6e49 2020-09-26 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6728 417a6e49 2020-09-26 stsp if (!histedit_in_progress) {
6729 417a6e49 2020-09-26 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6730 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6731 417a6e49 2020-09-26 stsp goto done;
6732 417a6e49 2020-09-26 stsp }
6733 417a6e49 2020-09-26 stsp cl_arg.branch_name += 11;
6734 417a6e49 2020-09-26 stsp }
6735 417a6e49 2020-09-26 stsp cl_arg.repo_path = got_repo_get_path(repo);
6736 417a6e49 2020-09-26 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6737 417a6e49 2020-09-26 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6738 417a6e49 2020-09-26 stsp print_status, NULL, repo);
6739 417a6e49 2020-09-26 stsp if (error) {
6740 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6741 417a6e49 2020-09-26 stsp cl_arg.logmsg_path != NULL)
6742 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
6743 417a6e49 2020-09-26 stsp goto done;
6744 417a6e49 2020-09-26 stsp }
6745 417a6e49 2020-09-26 stsp
6746 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, id);
6747 417a6e49 2020-09-26 stsp if (error)
6748 417a6e49 2020-09-26 stsp goto done;
6749 417a6e49 2020-09-26 stsp printf("Created commit %s\n", id_str);
6750 417a6e49 2020-09-26 stsp done:
6751 417a6e49 2020-09-26 stsp if (preserve_logmsg) {
6752 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6753 417a6e49 2020-09-26 stsp getprogname(), cl_arg.logmsg_path);
6754 417a6e49 2020-09-26 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6755 417a6e49 2020-09-26 stsp error == NULL)
6756 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6757 417a6e49 2020-09-26 stsp free(cl_arg.logmsg_path);
6758 417a6e49 2020-09-26 stsp if (repo)
6759 417a6e49 2020-09-26 stsp got_repo_close(repo);
6760 417a6e49 2020-09-26 stsp if (worktree)
6761 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6762 417a6e49 2020-09-26 stsp free(cwd);
6763 417a6e49 2020-09-26 stsp free(id_str);
6764 417a6e49 2020-09-26 stsp free(gitconfig_path);
6765 417a6e49 2020-09-26 stsp free(editor);
6766 417a6e49 2020-09-26 stsp free(author);
6767 417a6e49 2020-09-26 stsp return error;
6768 417a6e49 2020-09-26 stsp }
6769 417a6e49 2020-09-26 stsp
6770 417a6e49 2020-09-26 stsp __dead static void
6771 417a6e49 2020-09-26 stsp usage_cherrypick(void)
6772 417a6e49 2020-09-26 stsp {
6773 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6774 417a6e49 2020-09-26 stsp exit(1);
6775 417a6e49 2020-09-26 stsp }
6776 417a6e49 2020-09-26 stsp
6777 417a6e49 2020-09-26 stsp static const struct got_error *
6778 417a6e49 2020-09-26 stsp cmd_cherrypick(int argc, char *argv[])
6779 417a6e49 2020-09-26 stsp {
6780 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6781 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6782 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6783 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6784 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6785 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6786 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6787 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6788 417a6e49 2020-09-26 stsp int ch;
6789 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6790 417a6e49 2020-09-26 stsp
6791 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6792 417a6e49 2020-09-26 stsp switch (ch) {
6793 417a6e49 2020-09-26 stsp default:
6794 417a6e49 2020-09-26 stsp usage_cherrypick();
6795 417a6e49 2020-09-26 stsp /* NOTREACHED */
6796 417a6e49 2020-09-26 stsp }
6797 417a6e49 2020-09-26 stsp }
6798 417a6e49 2020-09-26 stsp
6799 417a6e49 2020-09-26 stsp argc -= optind;
6800 417a6e49 2020-09-26 stsp argv += optind;
6801 417a6e49 2020-09-26 stsp
6802 417a6e49 2020-09-26 stsp #ifndef PROFILE
6803 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6804 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6805 417a6e49 2020-09-26 stsp err(1, "pledge");
6806 417a6e49 2020-09-26 stsp #endif
6807 417a6e49 2020-09-26 stsp if (argc != 1)
6808 417a6e49 2020-09-26 stsp usage_cherrypick();
6809 417a6e49 2020-09-26 stsp
6810 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6811 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6812 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6813 417a6e49 2020-09-26 stsp goto done;
6814 417a6e49 2020-09-26 stsp }
6815 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6816 417a6e49 2020-09-26 stsp if (error) {
6817 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6818 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "cherrypick",
6819 417a6e49 2020-09-26 stsp cwd);
6820 417a6e49 2020-09-26 stsp goto done;
6821 417a6e49 2020-09-26 stsp }
6822 417a6e49 2020-09-26 stsp
6823 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6824 417a6e49 2020-09-26 stsp NULL);
6825 417a6e49 2020-09-26 stsp if (error != NULL)
6826 417a6e49 2020-09-26 stsp goto done;
6827 417a6e49 2020-09-26 stsp
6828 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6829 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6830 417a6e49 2020-09-26 stsp if (error)
6831 417a6e49 2020-09-26 stsp goto done;
6832 417a6e49 2020-09-26 stsp
6833 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6834 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6835 417a6e49 2020-09-26 stsp if (error != NULL) {
6836 417a6e49 2020-09-26 stsp struct got_reference *ref;
6837 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6838 417a6e49 2020-09-26 stsp goto done;
6839 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6840 417a6e49 2020-09-26 stsp if (error != NULL)
6841 417a6e49 2020-09-26 stsp goto done;
6842 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6843 417a6e49 2020-09-26 stsp got_ref_close(ref);
6844 417a6e49 2020-09-26 stsp if (error != NULL)
6845 417a6e49 2020-09-26 stsp goto done;
6846 417a6e49 2020-09-26 stsp }
6847 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6848 417a6e49 2020-09-26 stsp if (error)
6849 417a6e49 2020-09-26 stsp goto done;
6850 417a6e49 2020-09-26 stsp
6851 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6852 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6853 417a6e49 2020-09-26 stsp if (error != NULL)
6854 417a6e49 2020-09-26 stsp goto done;
6855 417a6e49 2020-09-26 stsp
6856 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6857 417a6e49 2020-09-26 stsp if (error) {
6858 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
6859 417a6e49 2020-09-26 stsp goto done;
6860 417a6e49 2020-09-26 stsp error = NULL;
6861 417a6e49 2020-09-26 stsp } else {
6862 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6863 417a6e49 2020-09-26 stsp goto done;
6864 417a6e49 2020-09-26 stsp }
6865 417a6e49 2020-09-26 stsp
6866 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6867 417a6e49 2020-09-26 stsp if (error)
6868 417a6e49 2020-09-26 stsp goto done;
6869 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6870 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6871 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6872 417a6e49 2020-09-26 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6873 417a6e49 2020-09-26 stsp NULL);
6874 417a6e49 2020-09-26 stsp if (error != NULL)
6875 417a6e49 2020-09-26 stsp goto done;
6876 417a6e49 2020-09-26 stsp
6877 417a6e49 2020-09-26 stsp if (upa.did_something)
6878 417a6e49 2020-09-26 stsp printf("Merged commit %s\n", commit_id_str);
6879 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
6880 417a6e49 2020-09-26 stsp done:
6881 417a6e49 2020-09-26 stsp if (commit)
6882 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
6883 417a6e49 2020-09-26 stsp free(commit_id_str);
6884 417a6e49 2020-09-26 stsp if (head_ref)
6885 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
6886 417a6e49 2020-09-26 stsp if (worktree)
6887 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6888 417a6e49 2020-09-26 stsp if (repo)
6889 417a6e49 2020-09-26 stsp got_repo_close(repo);
6890 417a6e49 2020-09-26 stsp return error;
6891 417a6e49 2020-09-26 stsp }
6892 417a6e49 2020-09-26 stsp
6893 417a6e49 2020-09-26 stsp __dead static void
6894 417a6e49 2020-09-26 stsp usage_backout(void)
6895 417a6e49 2020-09-26 stsp {
6896 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6897 417a6e49 2020-09-26 stsp exit(1);
6898 417a6e49 2020-09-26 stsp }
6899 417a6e49 2020-09-26 stsp
6900 417a6e49 2020-09-26 stsp static const struct got_error *
6901 417a6e49 2020-09-26 stsp cmd_backout(int argc, char *argv[])
6902 417a6e49 2020-09-26 stsp {
6903 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6904 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6905 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6906 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6907 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6908 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6909 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6910 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6911 417a6e49 2020-09-26 stsp int ch;
6912 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6913 417a6e49 2020-09-26 stsp
6914 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6915 417a6e49 2020-09-26 stsp switch (ch) {
6916 417a6e49 2020-09-26 stsp default:
6917 417a6e49 2020-09-26 stsp usage_backout();
6918 417a6e49 2020-09-26 stsp /* NOTREACHED */
6919 417a6e49 2020-09-26 stsp }
6920 417a6e49 2020-09-26 stsp }
6921 417a6e49 2020-09-26 stsp
6922 417a6e49 2020-09-26 stsp argc -= optind;
6923 417a6e49 2020-09-26 stsp argv += optind;
6924 417a6e49 2020-09-26 stsp
6925 417a6e49 2020-09-26 stsp #ifndef PROFILE
6926 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6927 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6928 417a6e49 2020-09-26 stsp err(1, "pledge");
6929 417a6e49 2020-09-26 stsp #endif
6930 417a6e49 2020-09-26 stsp if (argc != 1)
6931 417a6e49 2020-09-26 stsp usage_backout();
6932 417a6e49 2020-09-26 stsp
6933 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6934 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6935 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6936 417a6e49 2020-09-26 stsp goto done;
6937 417a6e49 2020-09-26 stsp }
6938 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6939 417a6e49 2020-09-26 stsp if (error) {
6940 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6941 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6942 417a6e49 2020-09-26 stsp goto done;
6943 417a6e49 2020-09-26 stsp }
6944 417a6e49 2020-09-26 stsp
6945 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6946 417a6e49 2020-09-26 stsp NULL);
6947 417a6e49 2020-09-26 stsp if (error != NULL)
6948 417a6e49 2020-09-26 stsp goto done;
6949 417a6e49 2020-09-26 stsp
6950 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6951 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6952 417a6e49 2020-09-26 stsp if (error)
6953 417a6e49 2020-09-26 stsp goto done;
6954 417a6e49 2020-09-26 stsp
6955 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6956 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6957 417a6e49 2020-09-26 stsp if (error != NULL) {
6958 417a6e49 2020-09-26 stsp struct got_reference *ref;
6959 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6960 417a6e49 2020-09-26 stsp goto done;
6961 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6962 417a6e49 2020-09-26 stsp if (error != NULL)
6963 417a6e49 2020-09-26 stsp goto done;
6964 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6965 417a6e49 2020-09-26 stsp got_ref_close(ref);
6966 417a6e49 2020-09-26 stsp if (error != NULL)
6967 417a6e49 2020-09-26 stsp goto done;
6968 417a6e49 2020-09-26 stsp }
6969 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6970 417a6e49 2020-09-26 stsp if (error)
6971 417a6e49 2020-09-26 stsp goto done;
6972 417a6e49 2020-09-26 stsp
6973 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6974 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6975 417a6e49 2020-09-26 stsp if (error != NULL)
6976 417a6e49 2020-09-26 stsp goto done;
6977 417a6e49 2020-09-26 stsp
6978 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6979 417a6e49 2020-09-26 stsp if (error)
6980 417a6e49 2020-09-26 stsp goto done;
6981 417a6e49 2020-09-26 stsp
6982 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6983 417a6e49 2020-09-26 stsp if (error)
6984 417a6e49 2020-09-26 stsp goto done;
6985 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6986 417a6e49 2020-09-26 stsp if (pid == NULL) {
6987 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6988 417a6e49 2020-09-26 stsp goto done;
6989 417a6e49 2020-09-26 stsp }
6990 417a6e49 2020-09-26 stsp
6991 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6992 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6993 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
6994 417a6e49 2020-09-26 stsp if (error != NULL)
6995 417a6e49 2020-09-26 stsp goto done;
6996 417a6e49 2020-09-26 stsp
6997 417a6e49 2020-09-26 stsp if (upa.did_something)
6998 417a6e49 2020-09-26 stsp printf("Backed out commit %s\n", commit_id_str);
6999 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7000 417a6e49 2020-09-26 stsp done:
7001 417a6e49 2020-09-26 stsp if (commit)
7002 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7003 417a6e49 2020-09-26 stsp free(commit_id_str);
7004 417a6e49 2020-09-26 stsp if (head_ref)
7005 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
7006 417a6e49 2020-09-26 stsp if (worktree)
7007 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7008 417a6e49 2020-09-26 stsp if (repo)
7009 417a6e49 2020-09-26 stsp got_repo_close(repo);
7010 417a6e49 2020-09-26 stsp return error;
7011 417a6e49 2020-09-26 stsp }
7012 417a6e49 2020-09-26 stsp
7013 417a6e49 2020-09-26 stsp __dead static void
7014 417a6e49 2020-09-26 stsp usage_rebase(void)
7015 417a6e49 2020-09-26 stsp {
7016 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7017 417a6e49 2020-09-26 stsp getprogname());
7018 417a6e49 2020-09-26 stsp exit(1);
7019 417a6e49 2020-09-26 stsp }
7020 417a6e49 2020-09-26 stsp
7021 417a6e49 2020-09-26 stsp void
7022 417a6e49 2020-09-26 stsp trim_logmsg(char *logmsg, int limit)
7023 417a6e49 2020-09-26 stsp {
7024 417a6e49 2020-09-26 stsp char *nl;
7025 417a6e49 2020-09-26 stsp size_t len;
7026 417a6e49 2020-09-26 stsp
7027 417a6e49 2020-09-26 stsp len = strlen(logmsg);
7028 417a6e49 2020-09-26 stsp if (len > limit)
7029 417a6e49 2020-09-26 stsp len = limit;
7030 417a6e49 2020-09-26 stsp logmsg[len] = '\0';
7031 417a6e49 2020-09-26 stsp nl = strchr(logmsg, '\n');
7032 417a6e49 2020-09-26 stsp if (nl)
7033 417a6e49 2020-09-26 stsp *nl = '\0';
7034 417a6e49 2020-09-26 stsp }
7035 417a6e49 2020-09-26 stsp
7036 417a6e49 2020-09-26 stsp static const struct got_error *
7037 417a6e49 2020-09-26 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7038 417a6e49 2020-09-26 stsp {
7039 417a6e49 2020-09-26 stsp const struct got_error *err;
7040 417a6e49 2020-09-26 stsp char *logmsg0 = NULL;
7041 417a6e49 2020-09-26 stsp const char *s;
7042 417a6e49 2020-09-26 stsp
7043 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7044 417a6e49 2020-09-26 stsp if (err)
7045 417a6e49 2020-09-26 stsp return err;
7046 417a6e49 2020-09-26 stsp
7047 417a6e49 2020-09-26 stsp s = logmsg0;
7048 417a6e49 2020-09-26 stsp while (isspace((unsigned char)s[0]))
7049 417a6e49 2020-09-26 stsp s++;
7050 417a6e49 2020-09-26 stsp
7051 417a6e49 2020-09-26 stsp *logmsg = strdup(s);
7052 417a6e49 2020-09-26 stsp if (*logmsg == NULL) {
7053 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7054 417a6e49 2020-09-26 stsp goto done;
7055 417a6e49 2020-09-26 stsp }
7056 417a6e49 2020-09-26 stsp
7057 417a6e49 2020-09-26 stsp trim_logmsg(*logmsg, limit);
7058 417a6e49 2020-09-26 stsp done:
7059 417a6e49 2020-09-26 stsp free(logmsg0);
7060 417a6e49 2020-09-26 stsp return err;
7061 417a6e49 2020-09-26 stsp }
7062 417a6e49 2020-09-26 stsp
7063 417a6e49 2020-09-26 stsp static const struct got_error *
7064 417a6e49 2020-09-26 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7065 417a6e49 2020-09-26 stsp {
7066 417a6e49 2020-09-26 stsp const struct got_error *err;
7067 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7068 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
7069 417a6e49 2020-09-26 stsp
7070 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
7071 417a6e49 2020-09-26 stsp if (err)
7072 417a6e49 2020-09-26 stsp return err;
7073 417a6e49 2020-09-26 stsp
7074 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
7075 417a6e49 2020-09-26 stsp if (err)
7076 417a6e49 2020-09-26 stsp goto done;
7077 417a6e49 2020-09-26 stsp
7078 417a6e49 2020-09-26 stsp id_str[12] = '\0';
7079 417a6e49 2020-09-26 stsp
7080 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7081 417a6e49 2020-09-26 stsp if (err)
7082 417a6e49 2020-09-26 stsp goto done;
7083 417a6e49 2020-09-26 stsp
7084 417a6e49 2020-09-26 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7085 417a6e49 2020-09-26 stsp done:
7086 417a6e49 2020-09-26 stsp free(id_str);
7087 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7088 417a6e49 2020-09-26 stsp free(logmsg);
7089 417a6e49 2020-09-26 stsp return err;
7090 417a6e49 2020-09-26 stsp }
7091 417a6e49 2020-09-26 stsp
7092 417a6e49 2020-09-26 stsp static const struct got_error *
7093 417a6e49 2020-09-26 stsp show_rebase_progress(struct got_commit_object *commit,
7094 417a6e49 2020-09-26 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7095 417a6e49 2020-09-26 stsp {
7096 417a6e49 2020-09-26 stsp const struct got_error *err;
7097 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7098 417a6e49 2020-09-26 stsp
7099 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, old_id);
7100 417a6e49 2020-09-26 stsp if (err)
7101 417a6e49 2020-09-26 stsp goto done;
7102 417a6e49 2020-09-26 stsp
7103 417a6e49 2020-09-26 stsp if (new_id) {
7104 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
7105 417a6e49 2020-09-26 stsp if (err)
7106 417a6e49 2020-09-26 stsp goto done;
7107 417a6e49 2020-09-26 stsp }
7108 417a6e49 2020-09-26 stsp
7109 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
7110 417a6e49 2020-09-26 stsp if (new_id_str)
7111 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
7112 417a6e49 2020-09-26 stsp
7113 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7114 417a6e49 2020-09-26 stsp if (err)
7115 417a6e49 2020-09-26 stsp goto done;
7116 417a6e49 2020-09-26 stsp
7117 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
7118 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7119 417a6e49 2020-09-26 stsp done:
7120 417a6e49 2020-09-26 stsp free(old_id_str);
7121 417a6e49 2020-09-26 stsp free(new_id_str);
7122 417a6e49 2020-09-26 stsp free(logmsg);
7123 417a6e49 2020-09-26 stsp return err;
7124 417a6e49 2020-09-26 stsp }
7125 417a6e49 2020-09-26 stsp
7126 417a6e49 2020-09-26 stsp static const struct got_error *
7127 417a6e49 2020-09-26 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7128 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7129 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7130 417a6e49 2020-09-26 stsp {
7131 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7132 417a6e49 2020-09-26 stsp return got_worktree_rebase_complete(worktree, fileindex,
7133 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, branch, repo);
7134 417a6e49 2020-09-26 stsp }
7135 417a6e49 2020-09-26 stsp
7136 417a6e49 2020-09-26 stsp static const struct got_error *
7137 417a6e49 2020-09-26 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7138 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7139 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch,
7140 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_repository *repo)
7141 417a6e49 2020-09-26 stsp {
7142 417a6e49 2020-09-26 stsp const struct got_error *error;
7143 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
7144 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
7145 417a6e49 2020-09-26 stsp
7146 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7147 417a6e49 2020-09-26 stsp if (error)
7148 417a6e49 2020-09-26 stsp return error;
7149 417a6e49 2020-09-26 stsp
7150 417a6e49 2020-09-26 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7151 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7152 417a6e49 2020-09-26 stsp if (error) {
7153 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7154 417a6e49 2020-09-26 stsp goto done;
7155 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, NULL);
7156 417a6e49 2020-09-26 stsp } else {
7157 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7158 417a6e49 2020-09-26 stsp free(new_commit_id);
7159 417a6e49 2020-09-26 stsp }
7160 417a6e49 2020-09-26 stsp done:
7161 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7162 417a6e49 2020-09-26 stsp return error;
7163 417a6e49 2020-09-26 stsp }
7164 417a6e49 2020-09-26 stsp
7165 417a6e49 2020-09-26 stsp struct check_path_prefix_arg {
7166 417a6e49 2020-09-26 stsp const char *path_prefix;
7167 417a6e49 2020-09-26 stsp size_t len;
7168 417a6e49 2020-09-26 stsp int errcode;
7169 417a6e49 2020-09-26 stsp };
7170 417a6e49 2020-09-26 stsp
7171 417a6e49 2020-09-26 stsp static const struct got_error *
7172 417a6e49 2020-09-26 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7173 417a6e49 2020-09-26 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7174 417a6e49 2020-09-26 stsp struct got_object_id *id2, const char *path1, const char *path2,
7175 417a6e49 2020-09-26 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7176 417a6e49 2020-09-26 stsp {
7177 417a6e49 2020-09-26 stsp struct check_path_prefix_arg *a = arg;
7178 417a6e49 2020-09-26 stsp
7179 417a6e49 2020-09-26 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7180 417a6e49 2020-09-26 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7181 417a6e49 2020-09-26 stsp return got_error(a->errcode);
7182 417a6e49 2020-09-26 stsp
7183 417a6e49 2020-09-26 stsp return NULL;
7184 417a6e49 2020-09-26 stsp }
7185 417a6e49 2020-09-26 stsp
7186 417a6e49 2020-09-26 stsp static const struct got_error *
7187 417a6e49 2020-09-26 stsp check_path_prefix(struct got_object_id *parent_id,
7188 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, const char *path_prefix,
7189 417a6e49 2020-09-26 stsp int errcode, struct got_repository *repo)
7190 417a6e49 2020-09-26 stsp {
7191 417a6e49 2020-09-26 stsp const struct got_error *err;
7192 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7193 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7194 417a6e49 2020-09-26 stsp struct check_path_prefix_arg cpp_arg;
7195 417a6e49 2020-09-26 stsp
7196 417a6e49 2020-09-26 stsp if (got_path_is_root_dir(path_prefix))
7197 417a6e49 2020-09-26 stsp return NULL;
7198 417a6e49 2020-09-26 stsp
7199 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7200 417a6e49 2020-09-26 stsp if (err)
7201 417a6e49 2020-09-26 stsp goto done;
7202 417a6e49 2020-09-26 stsp
7203 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7204 417a6e49 2020-09-26 stsp if (err)
7205 417a6e49 2020-09-26 stsp goto done;
7206 417a6e49 2020-09-26 stsp
7207 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo,
7208 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(parent_commit));
7209 417a6e49 2020-09-26 stsp if (err)
7210 417a6e49 2020-09-26 stsp goto done;
7211 417a6e49 2020-09-26 stsp
7212 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo,
7213 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(commit));
7214 417a6e49 2020-09-26 stsp if (err)
7215 417a6e49 2020-09-26 stsp goto done;
7216 417a6e49 2020-09-26 stsp
7217 417a6e49 2020-09-26 stsp cpp_arg.path_prefix = path_prefix;
7218 417a6e49 2020-09-26 stsp while (cpp_arg.path_prefix[0] == '/')
7219 417a6e49 2020-09-26 stsp cpp_arg.path_prefix++;
7220 417a6e49 2020-09-26 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7221 417a6e49 2020-09-26 stsp cpp_arg.errcode = errcode;
7222 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7223 417a6e49 2020-09-26 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7224 417a6e49 2020-09-26 stsp done:
7225 417a6e49 2020-09-26 stsp if (tree1)
7226 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
7227 417a6e49 2020-09-26 stsp if (tree2)
7228 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
7229 417a6e49 2020-09-26 stsp if (commit)
7230 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7231 417a6e49 2020-09-26 stsp if (parent_commit)
7232 417a6e49 2020-09-26 stsp got_object_commit_close(parent_commit);
7233 417a6e49 2020-09-26 stsp return err;
7234 417a6e49 2020-09-26 stsp }
7235 417a6e49 2020-09-26 stsp
7236 417a6e49 2020-09-26 stsp static const struct got_error *
7237 417a6e49 2020-09-26 stsp collect_commits(struct got_object_id_queue *commits,
7238 417a6e49 2020-09-26 stsp struct got_object_id *initial_commit_id,
7239 417a6e49 2020-09-26 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7240 417a6e49 2020-09-26 stsp const char *path_prefix, int path_prefix_errcode,
7241 417a6e49 2020-09-26 stsp struct got_repository *repo)
7242 417a6e49 2020-09-26 stsp {
7243 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7244 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
7245 417a6e49 2020-09-26 stsp struct got_object_id *parent_id = NULL;
7246 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7247 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = initial_commit_id;
7248 417a6e49 2020-09-26 stsp
7249 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
7250 417a6e49 2020-09-26 stsp if (err)
7251 417a6e49 2020-09-26 stsp return err;
7252 417a6e49 2020-09-26 stsp
7253 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7254 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7255 417a6e49 2020-09-26 stsp if (err)
7256 417a6e49 2020-09-26 stsp goto done;
7257 417a6e49 2020-09-26 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7258 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7259 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7260 417a6e49 2020-09-26 stsp if (err) {
7261 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7262 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7263 417a6e49 2020-09-26 stsp "ran out of commits to rebase before "
7264 417a6e49 2020-09-26 stsp "youngest common ancestor commit has "
7265 417a6e49 2020-09-26 stsp "been reached?!?");
7266 417a6e49 2020-09-26 stsp }
7267 417a6e49 2020-09-26 stsp goto done;
7268 417a6e49 2020-09-26 stsp } else {
7269 417a6e49 2020-09-26 stsp err = check_path_prefix(parent_id, commit_id,
7270 417a6e49 2020-09-26 stsp path_prefix, path_prefix_errcode, repo);
7271 417a6e49 2020-09-26 stsp if (err)
7272 417a6e49 2020-09-26 stsp goto done;
7273 417a6e49 2020-09-26 stsp
7274 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, commit_id);
7275 417a6e49 2020-09-26 stsp if (err)
7276 417a6e49 2020-09-26 stsp goto done;
7277 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7278 417a6e49 2020-09-26 stsp commit_id = parent_id;
7279 417a6e49 2020-09-26 stsp }
7280 417a6e49 2020-09-26 stsp }
7281 417a6e49 2020-09-26 stsp done:
7282 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
7283 417a6e49 2020-09-26 stsp return err;
7284 417a6e49 2020-09-26 stsp }
7285 417a6e49 2020-09-26 stsp
7286 417a6e49 2020-09-26 stsp static const struct got_error *
7287 417a6e49 2020-09-26 stsp cmd_rebase(int argc, char *argv[])
7288 417a6e49 2020-09-26 stsp {
7289 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
7290 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
7291 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
7292 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
7293 417a6e49 2020-09-26 stsp char *cwd = NULL;
7294 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
7295 417a6e49 2020-09-26 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7296 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7297 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
7298 417a6e49 2020-09-26 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7299 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7300 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7301 417a6e49 2020-09-26 stsp int histedit_in_progress = 0;
7302 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7303 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
7304 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
7305 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
7306 417a6e49 2020-09-26 stsp struct got_object_qid *qid, *pid;
7307 417a6e49 2020-09-26 stsp
7308 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
7309 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
7310 417a6e49 2020-09-26 stsp
7311 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7312 417a6e49 2020-09-26 stsp switch (ch) {
7313 417a6e49 2020-09-26 stsp case 'a':
7314 417a6e49 2020-09-26 stsp abort_rebase = 1;
7315 417a6e49 2020-09-26 stsp break;
7316 417a6e49 2020-09-26 stsp case 'c':
7317 417a6e49 2020-09-26 stsp continue_rebase = 1;
7318 417a6e49 2020-09-26 stsp break;
7319 417a6e49 2020-09-26 stsp default:
7320 417a6e49 2020-09-26 stsp usage_rebase();
7321 417a6e49 2020-09-26 stsp /* NOTREACHED */
7322 417a6e49 2020-09-26 stsp }
7323 417a6e49 2020-09-26 stsp }
7324 417a6e49 2020-09-26 stsp
7325 417a6e49 2020-09-26 stsp argc -= optind;
7326 417a6e49 2020-09-26 stsp argv += optind;
7327 417a6e49 2020-09-26 stsp
7328 417a6e49 2020-09-26 stsp #ifndef PROFILE
7329 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7330 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
7331 417a6e49 2020-09-26 stsp err(1, "pledge");
7332 417a6e49 2020-09-26 stsp #endif
7333 417a6e49 2020-09-26 stsp if (abort_rebase && continue_rebase)
7334 417a6e49 2020-09-26 stsp usage_rebase();
7335 417a6e49 2020-09-26 stsp else if (abort_rebase || continue_rebase) {
7336 417a6e49 2020-09-26 stsp if (argc != 0)
7337 417a6e49 2020-09-26 stsp usage_rebase();
7338 417a6e49 2020-09-26 stsp } else if (argc != 1)
7339 417a6e49 2020-09-26 stsp usage_rebase();
7340 417a6e49 2020-09-26 stsp
7341 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
7342 417a6e49 2020-09-26 stsp if (cwd == NULL) {
7343 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
7344 417a6e49 2020-09-26 stsp goto done;
7345 417a6e49 2020-09-26 stsp }
7346 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
7347 417a6e49 2020-09-26 stsp if (error) {
7348 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7349 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7350 417a6e49 2020-09-26 stsp goto done;
7351 417a6e49 2020-09-26 stsp }
7352 417a6e49 2020-09-26 stsp
7353 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7354 417a6e49 2020-09-26 stsp NULL);
7355 417a6e49 2020-09-26 stsp if (error != NULL)
7356 417a6e49 2020-09-26 stsp goto done;
7357 417a6e49 2020-09-26 stsp
7358 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7359 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
7360 417a6e49 2020-09-26 stsp if (error)
7361 417a6e49 2020-09-26 stsp goto done;
7362 417a6e49 2020-09-26 stsp
7363 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7364 417a6e49 2020-09-26 stsp worktree);
7365 417a6e49 2020-09-26 stsp if (error)
7366 417a6e49 2020-09-26 stsp goto done;
7367 417a6e49 2020-09-26 stsp if (histedit_in_progress) {
7368 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7369 417a6e49 2020-09-26 stsp goto done;
7370 417a6e49 2020-09-26 stsp }
7371 417a6e49 2020-09-26 stsp
7372 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7373 417a6e49 2020-09-26 stsp if (error)
7374 417a6e49 2020-09-26 stsp goto done;
7375 417a6e49 2020-09-26 stsp
7376 417a6e49 2020-09-26 stsp if (abort_rebase) {
7377 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7378 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7379 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7380 417a6e49 2020-09-26 stsp goto done;
7381 417a6e49 2020-09-26 stsp }
7382 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7383 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7384 417a6e49 2020-09-26 stsp worktree, repo);
7385 417a6e49 2020-09-26 stsp if (error)
7386 417a6e49 2020-09-26 stsp goto done;
7387 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
7388 417a6e49 2020-09-26 stsp got_ref_get_symref_target(new_base_branch));
7389 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7390 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7391 417a6e49 2020-09-26 stsp new_base_branch, update_progress, &upa);
7392 417a6e49 2020-09-26 stsp if (error)
7393 417a6e49 2020-09-26 stsp goto done;
7394 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7395 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7396 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
7397 417a6e49 2020-09-26 stsp }
7398 417a6e49 2020-09-26 stsp
7399 417a6e49 2020-09-26 stsp if (continue_rebase) {
7400 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7401 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7402 417a6e49 2020-09-26 stsp goto done;
7403 417a6e49 2020-09-26 stsp }
7404 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7405 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7406 417a6e49 2020-09-26 stsp worktree, repo);
7407 417a6e49 2020-09-26 stsp if (error)
7408 417a6e49 2020-09-26 stsp goto done;
7409 417a6e49 2020-09-26 stsp
7410 417a6e49 2020-09-26 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7411 417a6e49 2020-09-26 stsp resume_commit_id, repo);
7412 417a6e49 2020-09-26 stsp if (error)
7413 417a6e49 2020-09-26 stsp goto done;
7414 417a6e49 2020-09-26 stsp
7415 417a6e49 2020-09-26 stsp yca_id = got_object_id_dup(resume_commit_id);
7416 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7417 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_object_id_dup");
7418 417a6e49 2020-09-26 stsp goto done;
7419 417a6e49 2020-09-26 stsp }
7420 417a6e49 2020-09-26 stsp } else {
7421 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7422 417a6e49 2020-09-26 stsp if (error != NULL)
7423 417a6e49 2020-09-26 stsp goto done;
7424 417a6e49 2020-09-26 stsp }
7425 417a6e49 2020-09-26 stsp
7426 417a6e49 2020-09-26 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7427 417a6e49 2020-09-26 stsp if (error)
7428 417a6e49 2020-09-26 stsp goto done;
7429 417a6e49 2020-09-26 stsp
7430 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7431 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id;
7432 417a6e49 2020-09-26 stsp
7433 417a6e49 2020-09-26 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7434 417a6e49 2020-09-26 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7435 417a6e49 2020-09-26 stsp base_commit_id, branch_head_commit_id, repo,
7436 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7437 417a6e49 2020-09-26 stsp if (error)
7438 417a6e49 2020-09-26 stsp goto done;
7439 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7440 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7441 417a6e49 2020-09-26 stsp "specified branch shares no common ancestry "
7442 417a6e49 2020-09-26 stsp "with work tree's branch");
7443 417a6e49 2020-09-26 stsp goto done;
7444 417a6e49 2020-09-26 stsp }
7445 417a6e49 2020-09-26 stsp
7446 417a6e49 2020-09-26 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7447 417a6e49 2020-09-26 stsp if (error) {
7448 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
7449 417a6e49 2020-09-26 stsp goto done;
7450 417a6e49 2020-09-26 stsp error = NULL;
7451 417a6e49 2020-09-26 stsp } else {
7452 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7453 417a6e49 2020-09-26 stsp "specified branch resolves to a commit which "
7454 417a6e49 2020-09-26 stsp "is already contained in work tree's branch");
7455 417a6e49 2020-09-26 stsp goto done;
7456 417a6e49 2020-09-26 stsp }
7457 417a6e49 2020-09-26 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7458 417a6e49 2020-09-26 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7459 417a6e49 2020-09-26 stsp if (error)
7460 417a6e49 2020-09-26 stsp goto done;
7461 417a6e49 2020-09-26 stsp }
7462 417a6e49 2020-09-26 stsp
7463 417a6e49 2020-09-26 stsp commit_id = branch_head_commit_id;
7464 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7465 417a6e49 2020-09-26 stsp if (error)
7466 417a6e49 2020-09-26 stsp goto done;
7467 417a6e49 2020-09-26 stsp
7468 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7469 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
7470 417a6e49 2020-09-26 stsp if (pid == NULL) {
7471 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7472 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7473 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7474 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7475 417a6e49 2020-09-26 stsp repo, new_base_branch, update_progress, &upa);
7476 417a6e49 2020-09-26 stsp if (error)
7477 417a6e49 2020-09-26 stsp goto done;
7478 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n",
7479 417a6e49 2020-09-26 stsp got_ref_get_name(branch));
7480 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7481 417a6e49 2020-09-26 stsp
7482 417a6e49 2020-09-26 stsp }
7483 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7484 417a6e49 2020-09-26 stsp goto done;
7485 417a6e49 2020-09-26 stsp }
7486 417a6e49 2020-09-26 stsp error = collect_commits(&commits, commit_id, pid->id,
7487 417a6e49 2020-09-26 stsp yca_id, got_worktree_get_path_prefix(worktree),
7488 417a6e49 2020-09-26 stsp GOT_ERR_REBASE_PATH, repo);
7489 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7490 417a6e49 2020-09-26 stsp commit = NULL;
7491 417a6e49 2020-09-26 stsp if (error)
7492 417a6e49 2020-09-26 stsp goto done;
7493 417a6e49 2020-09-26 stsp
7494 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
7495 417a6e49 2020-09-26 stsp if (continue_rebase) {
7496 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex,
7497 417a6e49 2020-09-26 stsp branch, new_base_branch, tmp_branch, repo);
7498 417a6e49 2020-09-26 stsp goto done;
7499 417a6e49 2020-09-26 stsp } else {
7500 417a6e49 2020-09-26 stsp /* Fast-forward the reference of the branch. */
7501 417a6e49 2020-09-26 stsp struct got_object_id *new_head_commit_id;
7502 417a6e49 2020-09-26 stsp char *id_str;
7503 417a6e49 2020-09-26 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7504 417a6e49 2020-09-26 stsp new_base_branch);
7505 417a6e49 2020-09-26 stsp if (error)
7506 417a6e49 2020-09-26 stsp goto done;
7507 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7508 417a6e49 2020-09-26 stsp printf("Forwarding %s to commit %s\n",
7509 417a6e49 2020-09-26 stsp got_ref_get_name(branch), id_str);
7510 417a6e49 2020-09-26 stsp free(id_str);
7511 417a6e49 2020-09-26 stsp error = got_ref_change_ref(branch,
7512 417a6e49 2020-09-26 stsp new_head_commit_id);
7513 417a6e49 2020-09-26 stsp if (error)
7514 417a6e49 2020-09-26 stsp goto done;
7515 417a6e49 2020-09-26 stsp }
7516 417a6e49 2020-09-26 stsp }
7517 417a6e49 2020-09-26 stsp
7518 417a6e49 2020-09-26 stsp pid = NULL;
7519 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7520 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7521 417a6e49 2020-09-26 stsp
7522 417a6e49 2020-09-26 stsp commit_id = qid->id;
7523 417a6e49 2020-09-26 stsp parent_id = pid ? pid->id : yca_id;
7524 417a6e49 2020-09-26 stsp pid = qid;
7525 417a6e49 2020-09-26 stsp
7526 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7527 417a6e49 2020-09-26 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7528 417a6e49 2020-09-26 stsp worktree, fileindex, parent_id, commit_id, repo,
7529 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
7530 417a6e49 2020-09-26 stsp if (error)
7531 417a6e49 2020-09-26 stsp goto done;
7532 417a6e49 2020-09-26 stsp
7533 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7534 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
7535 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
7536 417a6e49 2020-09-26 stsp
7537 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7538 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(qid->id, repo);
7539 417a6e49 2020-09-26 stsp if (error)
7540 417a6e49 2020-09-26 stsp goto done;
7541 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7542 417a6e49 2020-09-26 stsp break;
7543 417a6e49 2020-09-26 stsp }
7544 417a6e49 2020-09-26 stsp
7545 417a6e49 2020-09-26 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7546 417a6e49 2020-09-26 stsp tmp_branch, commit_id, repo);
7547 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7548 417a6e49 2020-09-26 stsp if (error)
7549 417a6e49 2020-09-26 stsp goto done;
7550 417a6e49 2020-09-26 stsp }
7551 417a6e49 2020-09-26 stsp
7552 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7553 417a6e49 2020-09-26 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7554 417a6e49 2020-09-26 stsp if (error)
7555 417a6e49 2020-09-26 stsp goto done;
7556 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7557 417a6e49 2020-09-26 stsp "conflicts must be resolved before rebasing can continue");
7558 417a6e49 2020-09-26 stsp } else
7559 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex, branch,
7560 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, repo);
7561 417a6e49 2020-09-26 stsp done:
7562 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
7563 417a6e49 2020-09-26 stsp free(branch_head_commit_id);
7564 417a6e49 2020-09-26 stsp free(resume_commit_id);
7565 417a6e49 2020-09-26 stsp free(yca_id);
7566 417a6e49 2020-09-26 stsp if (commit)
7567 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7568 417a6e49 2020-09-26 stsp if (branch)
7569 417a6e49 2020-09-26 stsp got_ref_close(branch);
7570 417a6e49 2020-09-26 stsp if (new_base_branch)
7571 417a6e49 2020-09-26 stsp got_ref_close(new_base_branch);
7572 417a6e49 2020-09-26 stsp if (tmp_branch)
7573 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
7574 417a6e49 2020-09-26 stsp if (worktree)
7575 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7576 417a6e49 2020-09-26 stsp if (repo)
7577 417a6e49 2020-09-26 stsp got_repo_close(repo);
7578 417a6e49 2020-09-26 stsp return error;
7579 417a6e49 2020-09-26 stsp }
7580 417a6e49 2020-09-26 stsp
7581 417a6e49 2020-09-26 stsp __dead static void
7582 417a6e49 2020-09-26 stsp usage_histedit(void)
7583 417a6e49 2020-09-26 stsp {
7584 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7585 417a6e49 2020-09-26 stsp getprogname());
7586 417a6e49 2020-09-26 stsp exit(1);
7587 417a6e49 2020-09-26 stsp }
7588 417a6e49 2020-09-26 stsp
7589 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_PICK 'p'
7590 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_EDIT 'e'
7591 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_FOLD 'f'
7592 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_DROP 'd'
7593 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_MESG 'm'
7594 417a6e49 2020-09-26 stsp
7595 417a6e49 2020-09-26 stsp static struct got_histedit_cmd {
7596 417a6e49 2020-09-26 stsp unsigned char code;
7597 417a6e49 2020-09-26 stsp const char *name;
7598 417a6e49 2020-09-26 stsp const char *desc;
7599 417a6e49 2020-09-26 stsp } got_histedit_cmds[] = {
7600 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7601 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7602 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7603 417a6e49 2020-09-26 stsp "be used" },
7604 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7605 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_MESG, "mesg",
7606 417a6e49 2020-09-26 stsp "single-line log message for commit above (open editor if empty)" },
7607 417a6e49 2020-09-26 stsp };
7608 417a6e49 2020-09-26 stsp
7609 417a6e49 2020-09-26 stsp struct got_histedit_list_entry {
7610 417a6e49 2020-09-26 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7611 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
7612 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7613 417a6e49 2020-09-26 stsp char *logmsg;
7614 417a6e49 2020-09-26 stsp };
7615 417a6e49 2020-09-26 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7616 417a6e49 2020-09-26 stsp
7617 417a6e49 2020-09-26 stsp static const struct got_error *
7618 417a6e49 2020-09-26 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7619 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7620 417a6e49 2020-09-26 stsp {
7621 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7622 417a6e49 2020-09-26 stsp char *logmsg = NULL, *id_str = NULL;
7623 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7624 417a6e49 2020-09-26 stsp int n;
7625 417a6e49 2020-09-26 stsp
7626 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7627 417a6e49 2020-09-26 stsp if (err)
7628 417a6e49 2020-09-26 stsp goto done;
7629 417a6e49 2020-09-26 stsp
7630 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 34, commit);
7631 417a6e49 2020-09-26 stsp if (err)
7632 417a6e49 2020-09-26 stsp goto done;
7633 417a6e49 2020-09-26 stsp
7634 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
7635 417a6e49 2020-09-26 stsp if (err)
7636 417a6e49 2020-09-26 stsp goto done;
7637 417a6e49 2020-09-26 stsp
7638 417a6e49 2020-09-26 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7639 417a6e49 2020-09-26 stsp if (n < 0)
7640 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7641 417a6e49 2020-09-26 stsp done:
7642 417a6e49 2020-09-26 stsp if (commit)
7643 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7644 417a6e49 2020-09-26 stsp free(id_str);
7645 417a6e49 2020-09-26 stsp free(logmsg);
7646 417a6e49 2020-09-26 stsp return err;
7647 417a6e49 2020-09-26 stsp }
7648 417a6e49 2020-09-26 stsp
7649 417a6e49 2020-09-26 stsp static const struct got_error *
7650 417a6e49 2020-09-26 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7651 417a6e49 2020-09-26 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7652 417a6e49 2020-09-26 stsp {
7653 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7654 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7655 417a6e49 2020-09-26 stsp
7656 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7657 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7658 417a6e49 2020-09-26 stsp
7659 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7660 417a6e49 2020-09-26 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7661 417a6e49 2020-09-26 stsp f, repo);
7662 417a6e49 2020-09-26 stsp if (err)
7663 417a6e49 2020-09-26 stsp break;
7664 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
7665 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7666 417a6e49 2020-09-26 stsp if (n < 0) {
7667 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7668 417a6e49 2020-09-26 stsp break;
7669 417a6e49 2020-09-26 stsp }
7670 417a6e49 2020-09-26 stsp }
7671 417a6e49 2020-09-26 stsp }
7672 417a6e49 2020-09-26 stsp
7673 417a6e49 2020-09-26 stsp return err;
7674 417a6e49 2020-09-26 stsp }
7675 417a6e49 2020-09-26 stsp
7676 417a6e49 2020-09-26 stsp static const struct got_error *
7677 417a6e49 2020-09-26 stsp write_cmd_list(FILE *f, const char *branch_name,
7678 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits)
7679 417a6e49 2020-09-26 stsp {
7680 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7681 417a6e49 2020-09-26 stsp int n, i;
7682 417a6e49 2020-09-26 stsp char *id_str;
7683 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7684 417a6e49 2020-09-26 stsp
7685 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(commits);
7686 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
7687 417a6e49 2020-09-26 stsp if (err)
7688 417a6e49 2020-09-26 stsp return err;
7689 417a6e49 2020-09-26 stsp
7690 417a6e49 2020-09-26 stsp n = fprintf(f,
7691 417a6e49 2020-09-26 stsp "# Editing the history of branch '%s' starting at\n"
7692 417a6e49 2020-09-26 stsp "# commit %s\n"
7693 417a6e49 2020-09-26 stsp "# Commits will be processed in order from top to "
7694 417a6e49 2020-09-26 stsp "bottom of this file.\n", branch_name, id_str);
7695 417a6e49 2020-09-26 stsp if (n < 0) {
7696 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7697 417a6e49 2020-09-26 stsp goto done;
7698 417a6e49 2020-09-26 stsp }
7699 417a6e49 2020-09-26 stsp
7700 417a6e49 2020-09-26 stsp n = fprintf(f, "# Available histedit commands:\n");
7701 417a6e49 2020-09-26 stsp if (n < 0) {
7702 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7703 417a6e49 2020-09-26 stsp goto done;
7704 417a6e49 2020-09-26 stsp }
7705 417a6e49 2020-09-26 stsp
7706 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7707 417a6e49 2020-09-26 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7708 417a6e49 2020-09-26 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7709 417a6e49 2020-09-26 stsp cmd->desc);
7710 417a6e49 2020-09-26 stsp if (n < 0) {
7711 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7712 417a6e49 2020-09-26 stsp break;
7713 417a6e49 2020-09-26 stsp }
7714 417a6e49 2020-09-26 stsp }
7715 417a6e49 2020-09-26 stsp done:
7716 417a6e49 2020-09-26 stsp free(id_str);
7717 417a6e49 2020-09-26 stsp return err;
7718 417a6e49 2020-09-26 stsp }
7719 417a6e49 2020-09-26 stsp
7720 417a6e49 2020-09-26 stsp static const struct got_error *
7721 417a6e49 2020-09-26 stsp histedit_syntax_error(int lineno)
7722 417a6e49 2020-09-26 stsp {
7723 417a6e49 2020-09-26 stsp static char msg[42];
7724 417a6e49 2020-09-26 stsp int ret;
7725 417a6e49 2020-09-26 stsp
7726 417a6e49 2020-09-26 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7727 417a6e49 2020-09-26 stsp lineno);
7728 417a6e49 2020-09-26 stsp if (ret == -1 || ret >= sizeof(msg))
7729 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7730 417a6e49 2020-09-26 stsp
7731 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7732 417a6e49 2020-09-26 stsp }
7733 417a6e49 2020-09-26 stsp
7734 417a6e49 2020-09-26 stsp static const struct got_error *
7735 417a6e49 2020-09-26 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7736 417a6e49 2020-09-26 stsp char *logmsg, struct got_repository *repo)
7737 417a6e49 2020-09-26 stsp {
7738 417a6e49 2020-09-26 stsp const struct got_error *err;
7739 417a6e49 2020-09-26 stsp struct got_commit_object *folded_commit = NULL;
7740 417a6e49 2020-09-26 stsp char *id_str, *folded_logmsg = NULL;
7741 417a6e49 2020-09-26 stsp
7742 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7743 417a6e49 2020-09-26 stsp if (err)
7744 417a6e49 2020-09-26 stsp return err;
7745 417a6e49 2020-09-26 stsp
7746 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7747 417a6e49 2020-09-26 stsp if (err)
7748 417a6e49 2020-09-26 stsp goto done;
7749 417a6e49 2020-09-26 stsp
7750 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7751 417a6e49 2020-09-26 stsp if (err)
7752 417a6e49 2020-09-26 stsp goto done;
7753 417a6e49 2020-09-26 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7754 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7755 417a6e49 2020-09-26 stsp folded_logmsg) == -1) {
7756 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7757 417a6e49 2020-09-26 stsp }
7758 417a6e49 2020-09-26 stsp done:
7759 417a6e49 2020-09-26 stsp if (folded_commit)
7760 417a6e49 2020-09-26 stsp got_object_commit_close(folded_commit);
7761 417a6e49 2020-09-26 stsp free(id_str);
7762 417a6e49 2020-09-26 stsp free(folded_logmsg);
7763 417a6e49 2020-09-26 stsp return err;
7764 417a6e49 2020-09-26 stsp }
7765 417a6e49 2020-09-26 stsp
7766 417a6e49 2020-09-26 stsp static struct got_histedit_list_entry *
7767 417a6e49 2020-09-26 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7768 417a6e49 2020-09-26 stsp {
7769 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7770 417a6e49 2020-09-26 stsp
7771 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7772 417a6e49 2020-09-26 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7773 417a6e49 2020-09-26 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7774 417a6e49 2020-09-26 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7775 417a6e49 2020-09-26 stsp folded = prev;
7776 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7777 417a6e49 2020-09-26 stsp }
7778 417a6e49 2020-09-26 stsp
7779 417a6e49 2020-09-26 stsp return folded;
7780 417a6e49 2020-09-26 stsp }
7781 417a6e49 2020-09-26 stsp
7782 417a6e49 2020-09-26 stsp static const struct got_error *
7783 417a6e49 2020-09-26 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7784 417a6e49 2020-09-26 stsp struct got_repository *repo)
7785 417a6e49 2020-09-26 stsp {
7786 417a6e49 2020-09-26 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7787 417a6e49 2020-09-26 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7788 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7789 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7790 417a6e49 2020-09-26 stsp int logmsg_len;
7791 417a6e49 2020-09-26 stsp int fd;
7792 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *folded = NULL;
7793 417a6e49 2020-09-26 stsp
7794 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7795 417a6e49 2020-09-26 stsp if (err)
7796 417a6e49 2020-09-26 stsp return err;
7797 417a6e49 2020-09-26 stsp
7798 417a6e49 2020-09-26 stsp folded = get_folded_commits(hle);
7799 417a6e49 2020-09-26 stsp if (folded) {
7800 417a6e49 2020-09-26 stsp while (folded != hle) {
7801 417a6e49 2020-09-26 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7802 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7803 417a6e49 2020-09-26 stsp continue;
7804 417a6e49 2020-09-26 stsp }
7805 417a6e49 2020-09-26 stsp err = append_folded_commit_msg(&new_msg, folded,
7806 417a6e49 2020-09-26 stsp logmsg, repo);
7807 417a6e49 2020-09-26 stsp if (err)
7808 417a6e49 2020-09-26 stsp goto done;
7809 417a6e49 2020-09-26 stsp free(logmsg);
7810 417a6e49 2020-09-26 stsp logmsg = new_msg;
7811 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7812 417a6e49 2020-09-26 stsp }
7813 417a6e49 2020-09-26 stsp }
7814 417a6e49 2020-09-26 stsp
7815 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7816 417a6e49 2020-09-26 stsp if (err)
7817 417a6e49 2020-09-26 stsp goto done;
7818 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7819 417a6e49 2020-09-26 stsp if (err)
7820 417a6e49 2020-09-26 stsp goto done;
7821 417a6e49 2020-09-26 stsp logmsg_len = asprintf(&new_msg,
7822 417a6e49 2020-09-26 stsp "%s\n# original log message of commit %s: %s",
7823 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", id_str, orig_logmsg);
7824 417a6e49 2020-09-26 stsp if (logmsg_len == -1) {
7825 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7826 417a6e49 2020-09-26 stsp goto done;
7827 417a6e49 2020-09-26 stsp }
7828 417a6e49 2020-09-26 stsp free(logmsg);
7829 417a6e49 2020-09-26 stsp logmsg = new_msg;
7830 417a6e49 2020-09-26 stsp
7831 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7832 417a6e49 2020-09-26 stsp if (err)
7833 417a6e49 2020-09-26 stsp goto done;
7834 417a6e49 2020-09-26 stsp
7835 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7836 417a6e49 2020-09-26 stsp GOT_TMPDIR_STR "/got-logmsg");
7837 417a6e49 2020-09-26 stsp if (err)
7838 417a6e49 2020-09-26 stsp goto done;
7839 417a6e49 2020-09-26 stsp
7840 417a6e49 2020-09-26 stsp write(fd, logmsg, logmsg_len);
7841 417a6e49 2020-09-26 stsp close(fd);
7842 417a6e49 2020-09-26 stsp
7843 417a6e49 2020-09-26 stsp err = get_editor(&editor);
7844 417a6e49 2020-09-26 stsp if (err)
7845 417a6e49 2020-09-26 stsp goto done;
7846 417a6e49 2020-09-26 stsp
7847 417a6e49 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7848 417a6e49 2020-09-26 stsp if (err) {
7849 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7850 417a6e49 2020-09-26 stsp goto done;
7851 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7852 417a6e49 2020-09-26 stsp }
7853 417a6e49 2020-09-26 stsp done:
7854 417a6e49 2020-09-26 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7855 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", logmsg_path);
7856 417a6e49 2020-09-26 stsp free(logmsg_path);
7857 417a6e49 2020-09-26 stsp free(logmsg);
7858 417a6e49 2020-09-26 stsp free(orig_logmsg);
7859 417a6e49 2020-09-26 stsp free(editor);
7860 417a6e49 2020-09-26 stsp if (commit)
7861 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7862 417a6e49 2020-09-26 stsp return err;
7863 417a6e49 2020-09-26 stsp }
7864 417a6e49 2020-09-26 stsp
7865 417a6e49 2020-09-26 stsp static const struct got_error *
7866 417a6e49 2020-09-26 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7867 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7868 417a6e49 2020-09-26 stsp {
7869 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7870 417a6e49 2020-09-26 stsp char *line = NULL, *p, *end;
7871 417a6e49 2020-09-26 stsp size_t size;
7872 417a6e49 2020-09-26 stsp ssize_t len;
7873 417a6e49 2020-09-26 stsp int lineno = 0, i;
7874 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7875 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
7876 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle = NULL;
7877 417a6e49 2020-09-26 stsp
7878 417a6e49 2020-09-26 stsp for (;;) {
7879 417a6e49 2020-09-26 stsp len = getline(&line, &size, f);
7880 417a6e49 2020-09-26 stsp if (len == -1) {
7881 417a6e49 2020-09-26 stsp const struct got_error *getline_err;
7882 417a6e49 2020-09-26 stsp if (feof(f))
7883 417a6e49 2020-09-26 stsp break;
7884 417a6e49 2020-09-26 stsp getline_err = got_error_from_errno("getline");
7885 417a6e49 2020-09-26 stsp err = got_ferror(f, getline_err->code);
7886 417a6e49 2020-09-26 stsp break;
7887 417a6e49 2020-09-26 stsp }
7888 417a6e49 2020-09-26 stsp lineno++;
7889 417a6e49 2020-09-26 stsp p = line;
7890 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7891 417a6e49 2020-09-26 stsp p++;
7892 417a6e49 2020-09-26 stsp if (p[0] == '#' || p[0] == '\0') {
7893 417a6e49 2020-09-26 stsp free(line);
7894 417a6e49 2020-09-26 stsp line = NULL;
7895 417a6e49 2020-09-26 stsp continue;
7896 417a6e49 2020-09-26 stsp }
7897 417a6e49 2020-09-26 stsp cmd = NULL;
7898 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7899 417a6e49 2020-09-26 stsp cmd = &got_histedit_cmds[i];
7900 417a6e49 2020-09-26 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7901 417a6e49 2020-09-26 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7902 417a6e49 2020-09-26 stsp p += strlen(cmd->name);
7903 417a6e49 2020-09-26 stsp break;
7904 417a6e49 2020-09-26 stsp }
7905 417a6e49 2020-09-26 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7906 417a6e49 2020-09-26 stsp p++;
7907 417a6e49 2020-09-26 stsp break;
7908 417a6e49 2020-09-26 stsp }
7909 417a6e49 2020-09-26 stsp }
7910 417a6e49 2020-09-26 stsp if (i == nitems(got_histedit_cmds)) {
7911 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7912 417a6e49 2020-09-26 stsp break;
7913 417a6e49 2020-09-26 stsp }
7914 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7915 417a6e49 2020-09-26 stsp p++;
7916 417a6e49 2020-09-26 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7917 417a6e49 2020-09-26 stsp if (hle == NULL || hle->logmsg != NULL) {
7918 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7919 417a6e49 2020-09-26 stsp break;
7920 417a6e49 2020-09-26 stsp }
7921 417a6e49 2020-09-26 stsp if (p[0] == '\0') {
7922 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
7923 417a6e49 2020-09-26 stsp if (err)
7924 417a6e49 2020-09-26 stsp break;
7925 417a6e49 2020-09-26 stsp } else {
7926 417a6e49 2020-09-26 stsp hle->logmsg = strdup(p);
7927 417a6e49 2020-09-26 stsp if (hle->logmsg == NULL) {
7928 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7929 417a6e49 2020-09-26 stsp break;
7930 417a6e49 2020-09-26 stsp }
7931 417a6e49 2020-09-26 stsp }
7932 417a6e49 2020-09-26 stsp free(line);
7933 417a6e49 2020-09-26 stsp line = NULL;
7934 417a6e49 2020-09-26 stsp continue;
7935 417a6e49 2020-09-26 stsp } else {
7936 417a6e49 2020-09-26 stsp end = p;
7937 417a6e49 2020-09-26 stsp while (end[0] && !isspace((unsigned char)end[0]))
7938 417a6e49 2020-09-26 stsp end++;
7939 417a6e49 2020-09-26 stsp *end = '\0';
7940 417a6e49 2020-09-26 stsp
7941 417a6e49 2020-09-26 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7942 417a6e49 2020-09-26 stsp if (err) {
7943 417a6e49 2020-09-26 stsp /* override error code */
7944 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7945 417a6e49 2020-09-26 stsp break;
7946 417a6e49 2020-09-26 stsp }
7947 417a6e49 2020-09-26 stsp }
7948 417a6e49 2020-09-26 stsp hle = malloc(sizeof(*hle));
7949 417a6e49 2020-09-26 stsp if (hle == NULL) {
7950 417a6e49 2020-09-26 stsp err = got_error_from_errno("malloc");
7951 417a6e49 2020-09-26 stsp break;
7952 417a6e49 2020-09-26 stsp }
7953 417a6e49 2020-09-26 stsp hle->cmd = cmd;
7954 417a6e49 2020-09-26 stsp hle->commit_id = commit_id;
7955 417a6e49 2020-09-26 stsp hle->logmsg = NULL;
7956 417a6e49 2020-09-26 stsp commit_id = NULL;
7957 417a6e49 2020-09-26 stsp free(line);
7958 417a6e49 2020-09-26 stsp line = NULL;
7959 417a6e49 2020-09-26 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7960 417a6e49 2020-09-26 stsp }
7961 417a6e49 2020-09-26 stsp
7962 417a6e49 2020-09-26 stsp free(line);
7963 417a6e49 2020-09-26 stsp free(commit_id);
7964 417a6e49 2020-09-26 stsp return err;
7965 417a6e49 2020-09-26 stsp }
7966 417a6e49 2020-09-26 stsp
7967 417a6e49 2020-09-26 stsp static const struct got_error *
7968 417a6e49 2020-09-26 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7969 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7970 417a6e49 2020-09-26 stsp {
7971 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7972 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7973 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
7974 417a6e49 2020-09-26 stsp static char msg[92];
7975 417a6e49 2020-09-26 stsp char *id_str;
7976 417a6e49 2020-09-26 stsp
7977 417a6e49 2020-09-26 stsp if (TAILQ_EMPTY(histedit_cmds))
7978 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7979 417a6e49 2020-09-26 stsp "histedit script contains no commands");
7980 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7981 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7982 417a6e49 2020-09-26 stsp
7983 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7984 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle2;
7985 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7986 417a6e49 2020-09-26 stsp if (hle == hle2)
7987 417a6e49 2020-09-26 stsp continue;
7988 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
7989 417a6e49 2020-09-26 stsp hle2->commit_id) != 0)
7990 417a6e49 2020-09-26 stsp continue;
7991 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7992 417a6e49 2020-09-26 stsp if (err)
7993 417a6e49 2020-09-26 stsp return err;
7994 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7995 417a6e49 2020-09-26 stsp "more than once in histedit script", id_str);
7996 417a6e49 2020-09-26 stsp free(id_str);
7997 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7998 417a6e49 2020-09-26 stsp }
7999 417a6e49 2020-09-26 stsp }
8000 417a6e49 2020-09-26 stsp
8001 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8002 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8003 417a6e49 2020-09-26 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8004 417a6e49 2020-09-26 stsp break;
8005 417a6e49 2020-09-26 stsp }
8006 417a6e49 2020-09-26 stsp if (hle == NULL) {
8007 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
8008 417a6e49 2020-09-26 stsp if (err)
8009 417a6e49 2020-09-26 stsp return err;
8010 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
8011 417a6e49 2020-09-26 stsp "commit %s missing from histedit script", id_str);
8012 417a6e49 2020-09-26 stsp free(id_str);
8013 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8014 417a6e49 2020-09-26 stsp }
8015 417a6e49 2020-09-26 stsp }
8016 417a6e49 2020-09-26 stsp
8017 417a6e49 2020-09-26 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8018 417a6e49 2020-09-26 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8019 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8020 417a6e49 2020-09-26 stsp "last commit in histedit script cannot be folded");
8021 417a6e49 2020-09-26 stsp
8022 417a6e49 2020-09-26 stsp return NULL;
8023 417a6e49 2020-09-26 stsp }
8024 417a6e49 2020-09-26 stsp
8025 417a6e49 2020-09-26 stsp static const struct got_error *
8026 417a6e49 2020-09-26 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8027 417a6e49 2020-09-26 stsp const char *path, struct got_object_id_queue *commits,
8028 417a6e49 2020-09-26 stsp struct got_repository *repo)
8029 417a6e49 2020-09-26 stsp {
8030 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8031 417a6e49 2020-09-26 stsp char *editor;
8032 417a6e49 2020-09-26 stsp FILE *f = NULL;
8033 417a6e49 2020-09-26 stsp
8034 417a6e49 2020-09-26 stsp err = get_editor(&editor);
8035 417a6e49 2020-09-26 stsp if (err)
8036 417a6e49 2020-09-26 stsp return err;
8037 417a6e49 2020-09-26 stsp
8038 417a6e49 2020-09-26 stsp if (spawn_editor(editor, path) == -1) {
8039 417a6e49 2020-09-26 stsp err = got_error_from_errno("failed spawning editor");
8040 417a6e49 2020-09-26 stsp goto done;
8041 417a6e49 2020-09-26 stsp }
8042 417a6e49 2020-09-26 stsp
8043 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8044 417a6e49 2020-09-26 stsp if (f == NULL) {
8045 417a6e49 2020-09-26 stsp err = got_error_from_errno("fopen");
8046 417a6e49 2020-09-26 stsp goto done;
8047 417a6e49 2020-09-26 stsp }
8048 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8049 417a6e49 2020-09-26 stsp if (err)
8050 417a6e49 2020-09-26 stsp goto done;
8051 417a6e49 2020-09-26 stsp
8052 417a6e49 2020-09-26 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8053 417a6e49 2020-09-26 stsp done:
8054 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8055 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8056 417a6e49 2020-09-26 stsp free(editor);
8057 417a6e49 2020-09-26 stsp return err;
8058 417a6e49 2020-09-26 stsp }
8059 417a6e49 2020-09-26 stsp
8060 417a6e49 2020-09-26 stsp static const struct got_error *
8061 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8062 417a6e49 2020-09-26 stsp struct got_object_id_queue *, const char *, const char *,
8063 417a6e49 2020-09-26 stsp struct got_repository *);
8064 417a6e49 2020-09-26 stsp
8065 417a6e49 2020-09-26 stsp static const struct got_error *
8066 417a6e49 2020-09-26 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8067 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, const char *branch_name,
8068 417a6e49 2020-09-26 stsp int edit_logmsg_only, struct got_repository *repo)
8069 417a6e49 2020-09-26 stsp {
8070 417a6e49 2020-09-26 stsp const struct got_error *err;
8071 417a6e49 2020-09-26 stsp FILE *f = NULL;
8072 417a6e49 2020-09-26 stsp char *path = NULL;
8073 417a6e49 2020-09-26 stsp
8074 417a6e49 2020-09-26 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8075 417a6e49 2020-09-26 stsp if (err)
8076 417a6e49 2020-09-26 stsp return err;
8077 417a6e49 2020-09-26 stsp
8078 417a6e49 2020-09-26 stsp err = write_cmd_list(f, branch_name, commits);
8079 417a6e49 2020-09-26 stsp if (err)
8080 417a6e49 2020-09-26 stsp goto done;
8081 417a6e49 2020-09-26 stsp
8082 417a6e49 2020-09-26 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8083 417a6e49 2020-09-26 stsp if (err)
8084 417a6e49 2020-09-26 stsp goto done;
8085 417a6e49 2020-09-26 stsp
8086 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
8087 417a6e49 2020-09-26 stsp rewind(f);
8088 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8089 417a6e49 2020-09-26 stsp } else {
8090 417a6e49 2020-09-26 stsp if (fclose(f) != 0) {
8091 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8092 417a6e49 2020-09-26 stsp goto done;
8093 417a6e49 2020-09-26 stsp }
8094 417a6e49 2020-09-26 stsp f = NULL;
8095 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8096 417a6e49 2020-09-26 stsp if (err) {
8097 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8098 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8099 417a6e49 2020-09-26 stsp goto done;
8100 417a6e49 2020-09-26 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8101 417a6e49 2020-09-26 stsp commits, path, branch_name, repo);
8102 417a6e49 2020-09-26 stsp }
8103 417a6e49 2020-09-26 stsp }
8104 417a6e49 2020-09-26 stsp done:
8105 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8106 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8107 417a6e49 2020-09-26 stsp if (path && unlink(path) != 0 && err == NULL)
8108 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", path);
8109 417a6e49 2020-09-26 stsp free(path);
8110 417a6e49 2020-09-26 stsp return err;
8111 417a6e49 2020-09-26 stsp }
8112 417a6e49 2020-09-26 stsp
8113 417a6e49 2020-09-26 stsp static const struct got_error *
8114 417a6e49 2020-09-26 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8115 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8116 417a6e49 2020-09-26 stsp {
8117 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8118 417a6e49 2020-09-26 stsp char *path = NULL;
8119 417a6e49 2020-09-26 stsp FILE *f = NULL;
8120 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8121 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8122 417a6e49 2020-09-26 stsp
8123 417a6e49 2020-09-26 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8124 417a6e49 2020-09-26 stsp if (err)
8125 417a6e49 2020-09-26 stsp return err;
8126 417a6e49 2020-09-26 stsp
8127 417a6e49 2020-09-26 stsp f = fopen(path, "w");
8128 417a6e49 2020-09-26 stsp if (f == NULL) {
8129 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8130 417a6e49 2020-09-26 stsp goto done;
8131 417a6e49 2020-09-26 stsp }
8132 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8133 417a6e49 2020-09-26 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8134 417a6e49 2020-09-26 stsp repo);
8135 417a6e49 2020-09-26 stsp if (err)
8136 417a6e49 2020-09-26 stsp break;
8137 417a6e49 2020-09-26 stsp
8138 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8139 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c %s\n",
8140 417a6e49 2020-09-26 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8141 417a6e49 2020-09-26 stsp if (n < 0) {
8142 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
8143 417a6e49 2020-09-26 stsp break;
8144 417a6e49 2020-09-26 stsp }
8145 417a6e49 2020-09-26 stsp }
8146 417a6e49 2020-09-26 stsp }
8147 417a6e49 2020-09-26 stsp done:
8148 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8149 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8150 417a6e49 2020-09-26 stsp free(path);
8151 417a6e49 2020-09-26 stsp if (commit)
8152 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8153 417a6e49 2020-09-26 stsp return err;
8154 417a6e49 2020-09-26 stsp }
8155 417a6e49 2020-09-26 stsp
8156 417a6e49 2020-09-26 stsp void
8157 417a6e49 2020-09-26 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8158 417a6e49 2020-09-26 stsp {
8159 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8160 417a6e49 2020-09-26 stsp
8161 417a6e49 2020-09-26 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8162 417a6e49 2020-09-26 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8163 417a6e49 2020-09-26 stsp free(hle);
8164 417a6e49 2020-09-26 stsp }
8165 417a6e49 2020-09-26 stsp }
8166 417a6e49 2020-09-26 stsp
8167 417a6e49 2020-09-26 stsp static const struct got_error *
8168 417a6e49 2020-09-26 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8169 417a6e49 2020-09-26 stsp const char *path, struct got_repository *repo)
8170 417a6e49 2020-09-26 stsp {
8171 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8172 417a6e49 2020-09-26 stsp FILE *f = NULL;
8173 417a6e49 2020-09-26 stsp
8174 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8175 417a6e49 2020-09-26 stsp if (f == NULL) {
8176 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8177 417a6e49 2020-09-26 stsp goto done;
8178 417a6e49 2020-09-26 stsp }
8179 417a6e49 2020-09-26 stsp
8180 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8181 417a6e49 2020-09-26 stsp done:
8182 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8183 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8184 417a6e49 2020-09-26 stsp return err;
8185 417a6e49 2020-09-26 stsp }
8186 417a6e49 2020-09-26 stsp
8187 417a6e49 2020-09-26 stsp static const struct got_error *
8188 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8189 417a6e49 2020-09-26 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8190 417a6e49 2020-09-26 stsp const char *path, const char *branch_name, struct got_repository *repo)
8191 417a6e49 2020-09-26 stsp {
8192 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8193 417a6e49 2020-09-26 stsp int resp = ' ';
8194 417a6e49 2020-09-26 stsp
8195 417a6e49 2020-09-26 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8196 417a6e49 2020-09-26 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8197 417a6e49 2020-09-26 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8198 417a6e49 2020-09-26 stsp resp = getchar();
8199 417a6e49 2020-09-26 stsp if (resp == '\n')
8200 417a6e49 2020-09-26 stsp resp = getchar();
8201 417a6e49 2020-09-26 stsp if (resp == 'c') {
8202 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8203 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8204 417a6e49 2020-09-26 stsp repo);
8205 417a6e49 2020-09-26 stsp if (err) {
8206 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8207 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8208 417a6e49 2020-09-26 stsp break;
8209 417a6e49 2020-09-26 stsp prev_err = err;
8210 417a6e49 2020-09-26 stsp resp = ' ';
8211 417a6e49 2020-09-26 stsp continue;
8212 417a6e49 2020-09-26 stsp }
8213 417a6e49 2020-09-26 stsp break;
8214 417a6e49 2020-09-26 stsp } else if (resp == 'r') {
8215 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8216 417a6e49 2020-09-26 stsp err = histedit_edit_script(histedit_cmds,
8217 417a6e49 2020-09-26 stsp commits, branch_name, 0, repo);
8218 417a6e49 2020-09-26 stsp if (err) {
8219 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8220 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8221 417a6e49 2020-09-26 stsp break;
8222 417a6e49 2020-09-26 stsp prev_err = err;
8223 417a6e49 2020-09-26 stsp resp = ' ';
8224 417a6e49 2020-09-26 stsp continue;
8225 417a6e49 2020-09-26 stsp }
8226 417a6e49 2020-09-26 stsp break;
8227 417a6e49 2020-09-26 stsp } else if (resp == 'a') {
8228 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8229 417a6e49 2020-09-26 stsp break;
8230 417a6e49 2020-09-26 stsp } else
8231 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
8232 417a6e49 2020-09-26 stsp }
8233 417a6e49 2020-09-26 stsp
8234 417a6e49 2020-09-26 stsp return err;
8235 417a6e49 2020-09-26 stsp }
8236 417a6e49 2020-09-26 stsp
8237 417a6e49 2020-09-26 stsp static const struct got_error *
8238 417a6e49 2020-09-26 stsp histedit_complete(struct got_worktree *worktree,
8239 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8240 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_repository *repo)
8241 417a6e49 2020-09-26 stsp {
8242 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8243 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8244 417a6e49 2020-09-26 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8245 417a6e49 2020-09-26 stsp branch, repo);
8246 417a6e49 2020-09-26 stsp }
8247 417a6e49 2020-09-26 stsp
8248 417a6e49 2020-09-26 stsp static const struct got_error *
8249 417a6e49 2020-09-26 stsp show_histedit_progress(struct got_commit_object *commit,
8250 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8251 417a6e49 2020-09-26 stsp {
8252 417a6e49 2020-09-26 stsp const struct got_error *err;
8253 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8254 417a6e49 2020-09-26 stsp
8255 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8256 417a6e49 2020-09-26 stsp if (err)
8257 417a6e49 2020-09-26 stsp goto done;
8258 417a6e49 2020-09-26 stsp
8259 417a6e49 2020-09-26 stsp if (new_id) {
8260 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
8261 417a6e49 2020-09-26 stsp if (err)
8262 417a6e49 2020-09-26 stsp goto done;
8263 417a6e49 2020-09-26 stsp }
8264 417a6e49 2020-09-26 stsp
8265 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
8266 417a6e49 2020-09-26 stsp if (new_id_str)
8267 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
8268 417a6e49 2020-09-26 stsp
8269 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8270 417a6e49 2020-09-26 stsp logmsg = strdup(hle->logmsg);
8271 417a6e49 2020-09-26 stsp if (logmsg == NULL) {
8272 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
8273 417a6e49 2020-09-26 stsp goto done;
8274 417a6e49 2020-09-26 stsp }
8275 417a6e49 2020-09-26 stsp trim_logmsg(logmsg, 42);
8276 417a6e49 2020-09-26 stsp } else {
8277 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
8278 417a6e49 2020-09-26 stsp if (err)
8279 417a6e49 2020-09-26 stsp goto done;
8280 417a6e49 2020-09-26 stsp }
8281 417a6e49 2020-09-26 stsp
8282 417a6e49 2020-09-26 stsp switch (hle->cmd->code) {
8283 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_PICK:
8284 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_EDIT:
8285 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
8286 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8287 417a6e49 2020-09-26 stsp break;
8288 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_DROP:
8289 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_FOLD:
8290 417a6e49 2020-09-26 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8291 417a6e49 2020-09-26 stsp logmsg);
8292 417a6e49 2020-09-26 stsp break;
8293 417a6e49 2020-09-26 stsp default:
8294 417a6e49 2020-09-26 stsp break;
8295 417a6e49 2020-09-26 stsp }
8296 417a6e49 2020-09-26 stsp done:
8297 417a6e49 2020-09-26 stsp free(old_id_str);
8298 417a6e49 2020-09-26 stsp free(new_id_str);
8299 417a6e49 2020-09-26 stsp return err;
8300 417a6e49 2020-09-26 stsp }
8301 417a6e49 2020-09-26 stsp
8302 417a6e49 2020-09-26 stsp static const struct got_error *
8303 417a6e49 2020-09-26 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8304 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8305 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8306 417a6e49 2020-09-26 stsp struct got_repository *repo)
8307 417a6e49 2020-09-26 stsp {
8308 417a6e49 2020-09-26 stsp const struct got_error *err;
8309 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8310 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
8311 417a6e49 2020-09-26 stsp
8312 417a6e49 2020-09-26 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8313 417a6e49 2020-09-26 stsp && hle->logmsg == NULL) {
8314 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
8315 417a6e49 2020-09-26 stsp if (err)
8316 417a6e49 2020-09-26 stsp return err;
8317 417a6e49 2020-09-26 stsp }
8318 417a6e49 2020-09-26 stsp
8319 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8320 417a6e49 2020-09-26 stsp if (err)
8321 417a6e49 2020-09-26 stsp return err;
8322 417a6e49 2020-09-26 stsp
8323 417a6e49 2020-09-26 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8324 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8325 417a6e49 2020-09-26 stsp hle->logmsg, repo);
8326 417a6e49 2020-09-26 stsp if (err) {
8327 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8328 417a6e49 2020-09-26 stsp goto done;
8329 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, NULL);
8330 417a6e49 2020-09-26 stsp } else {
8331 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8332 417a6e49 2020-09-26 stsp free(new_commit_id);
8333 417a6e49 2020-09-26 stsp }
8334 417a6e49 2020-09-26 stsp done:
8335 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8336 417a6e49 2020-09-26 stsp return err;
8337 417a6e49 2020-09-26 stsp }
8338 417a6e49 2020-09-26 stsp
8339 417a6e49 2020-09-26 stsp static const struct got_error *
8340 417a6e49 2020-09-26 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8341 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8342 417a6e49 2020-09-26 stsp {
8343 417a6e49 2020-09-26 stsp const struct got_error *error;
8344 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8345 417a6e49 2020-09-26 stsp
8346 417a6e49 2020-09-26 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8347 417a6e49 2020-09-26 stsp repo);
8348 417a6e49 2020-09-26 stsp if (error)
8349 417a6e49 2020-09-26 stsp return error;
8350 417a6e49 2020-09-26 stsp
8351 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8352 417a6e49 2020-09-26 stsp if (error)
8353 417a6e49 2020-09-26 stsp return error;
8354 417a6e49 2020-09-26 stsp
8355 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit, hle, NULL);
8356 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8357 417a6e49 2020-09-26 stsp return error;
8358 417a6e49 2020-09-26 stsp }
8359 417a6e49 2020-09-26 stsp
8360 417a6e49 2020-09-26 stsp static const struct got_error *
8361 417a6e49 2020-09-26 stsp check_local_changes(void *arg, unsigned char status,
8362 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path,
8363 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8364 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8365 417a6e49 2020-09-26 stsp {
8366 417a6e49 2020-09-26 stsp int *have_local_changes = arg;
8367 417a6e49 2020-09-26 stsp
8368 417a6e49 2020-09-26 stsp switch (status) {
8369 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8370 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8371 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8372 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
8373 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8374 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8375 417a6e49 2020-09-26 stsp default:
8376 417a6e49 2020-09-26 stsp break;
8377 417a6e49 2020-09-26 stsp }
8378 417a6e49 2020-09-26 stsp
8379 417a6e49 2020-09-26 stsp switch (staged_status) {
8380 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8381 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8382 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8383 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8384 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8385 417a6e49 2020-09-26 stsp default:
8386 417a6e49 2020-09-26 stsp break;
8387 417a6e49 2020-09-26 stsp }
8388 417a6e49 2020-09-26 stsp
8389 417a6e49 2020-09-26 stsp return NULL;
8390 417a6e49 2020-09-26 stsp }
8391 417a6e49 2020-09-26 stsp
8392 417a6e49 2020-09-26 stsp static const struct got_error *
8393 417a6e49 2020-09-26 stsp cmd_histedit(int argc, char *argv[])
8394 417a6e49 2020-09-26 stsp {
8395 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8396 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8397 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8398 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8399 417a6e49 2020-09-26 stsp char *cwd = NULL;
8400 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
8401 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch = NULL;
8402 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
8403 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id = NULL;
8404 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
8405 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8406 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0;
8407 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8408 417a6e49 2020-09-26 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8409 417a6e49 2020-09-26 stsp int edit_logmsg_only = 0;
8410 417a6e49 2020-09-26 stsp const char *edit_script_path = NULL;
8411 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8412 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
8413 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
8414 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
8415 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
8416 417a6e49 2020-09-26 stsp struct got_histedit_list histedit_cmds;
8417 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8418 417a6e49 2020-09-26 stsp
8419 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
8420 417a6e49 2020-09-26 stsp TAILQ_INIT(&histedit_cmds);
8421 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
8422 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8423 417a6e49 2020-09-26 stsp
8424 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8425 417a6e49 2020-09-26 stsp switch (ch) {
8426 417a6e49 2020-09-26 stsp case 'a':
8427 417a6e49 2020-09-26 stsp abort_edit = 1;
8428 417a6e49 2020-09-26 stsp break;
8429 417a6e49 2020-09-26 stsp case 'c':
8430 417a6e49 2020-09-26 stsp continue_edit = 1;
8431 417a6e49 2020-09-26 stsp break;
8432 417a6e49 2020-09-26 stsp case 'F':
8433 417a6e49 2020-09-26 stsp edit_script_path = optarg;
8434 417a6e49 2020-09-26 stsp break;
8435 417a6e49 2020-09-26 stsp case 'm':
8436 417a6e49 2020-09-26 stsp edit_logmsg_only = 1;
8437 417a6e49 2020-09-26 stsp break;
8438 417a6e49 2020-09-26 stsp default:
8439 417a6e49 2020-09-26 stsp usage_histedit();
8440 417a6e49 2020-09-26 stsp /* NOTREACHED */
8441 417a6e49 2020-09-26 stsp }
8442 417a6e49 2020-09-26 stsp }
8443 417a6e49 2020-09-26 stsp
8444 417a6e49 2020-09-26 stsp argc -= optind;
8445 417a6e49 2020-09-26 stsp argv += optind;
8446 417a6e49 2020-09-26 stsp
8447 417a6e49 2020-09-26 stsp #ifndef PROFILE
8448 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8449 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8450 417a6e49 2020-09-26 stsp err(1, "pledge");
8451 417a6e49 2020-09-26 stsp #endif
8452 417a6e49 2020-09-26 stsp if (abort_edit && continue_edit)
8453 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8454 417a6e49 2020-09-26 stsp if (edit_script_path && edit_logmsg_only)
8455 417a6e49 2020-09-26 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8456 417a6e49 2020-09-26 stsp if (abort_edit && edit_logmsg_only)
8457 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8458 417a6e49 2020-09-26 stsp if (continue_edit && edit_logmsg_only)
8459 417a6e49 2020-09-26 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8460 417a6e49 2020-09-26 stsp if (argc != 0)
8461 417a6e49 2020-09-26 stsp usage_histedit();
8462 417a6e49 2020-09-26 stsp
8463 417a6e49 2020-09-26 stsp /*
8464 417a6e49 2020-09-26 stsp * This command cannot apply unveil(2) in all cases because the
8465 417a6e49 2020-09-26 stsp * user may choose to run an editor to edit the histedit script
8466 417a6e49 2020-09-26 stsp * and to edit individual commit log messages.
8467 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8468 417a6e49 2020-09-26 stsp * apply unveil after edit script and log messages have been written.
8469 417a6e49 2020-09-26 stsp * XXX TODO: Make use of unveil(2) where possible.
8470 417a6e49 2020-09-26 stsp */
8471 417a6e49 2020-09-26 stsp
8472 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8473 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8474 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8475 417a6e49 2020-09-26 stsp goto done;
8476 417a6e49 2020-09-26 stsp }
8477 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8478 417a6e49 2020-09-26 stsp if (error) {
8479 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8480 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8481 417a6e49 2020-09-26 stsp goto done;
8482 417a6e49 2020-09-26 stsp }
8483 417a6e49 2020-09-26 stsp
8484 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8485 417a6e49 2020-09-26 stsp NULL);
8486 417a6e49 2020-09-26 stsp if (error != NULL)
8487 417a6e49 2020-09-26 stsp goto done;
8488 417a6e49 2020-09-26 stsp
8489 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8490 417a6e49 2020-09-26 stsp if (error)
8491 417a6e49 2020-09-26 stsp goto done;
8492 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
8493 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
8494 417a6e49 2020-09-26 stsp goto done;
8495 417a6e49 2020-09-26 stsp }
8496 417a6e49 2020-09-26 stsp
8497 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8498 417a6e49 2020-09-26 stsp if (error)
8499 417a6e49 2020-09-26 stsp goto done;
8500 417a6e49 2020-09-26 stsp
8501 417a6e49 2020-09-26 stsp if (edit_in_progress && edit_logmsg_only) {
8502 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8503 417a6e49 2020-09-26 stsp "histedit operation is in progress in this "
8504 417a6e49 2020-09-26 stsp "work tree and must be continued or aborted "
8505 417a6e49 2020-09-26 stsp "before the -m option can be used");
8506 417a6e49 2020-09-26 stsp goto done;
8507 417a6e49 2020-09-26 stsp }
8508 417a6e49 2020-09-26 stsp
8509 417a6e49 2020-09-26 stsp if (edit_in_progress && abort_edit) {
8510 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8511 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8512 417a6e49 2020-09-26 stsp worktree, repo);
8513 417a6e49 2020-09-26 stsp if (error)
8514 417a6e49 2020-09-26 stsp goto done;
8515 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8516 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8517 417a6e49 2020-09-26 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8518 417a6e49 2020-09-26 stsp branch, base_commit_id, update_progress, &upa);
8519 417a6e49 2020-09-26 stsp if (error)
8520 417a6e49 2020-09-26 stsp goto done;
8521 417a6e49 2020-09-26 stsp printf("Histedit of %s aborted\n",
8522 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8523 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8524 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
8525 417a6e49 2020-09-26 stsp } else if (abort_edit) {
8526 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8527 417a6e49 2020-09-26 stsp goto done;
8528 417a6e49 2020-09-26 stsp }
8529 417a6e49 2020-09-26 stsp
8530 417a6e49 2020-09-26 stsp if (continue_edit) {
8531 417a6e49 2020-09-26 stsp char *path;
8532 417a6e49 2020-09-26 stsp
8533 417a6e49 2020-09-26 stsp if (!edit_in_progress) {
8534 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8535 417a6e49 2020-09-26 stsp goto done;
8536 417a6e49 2020-09-26 stsp }
8537 417a6e49 2020-09-26 stsp
8538 417a6e49 2020-09-26 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8539 417a6e49 2020-09-26 stsp if (error)
8540 417a6e49 2020-09-26 stsp goto done;
8541 417a6e49 2020-09-26 stsp
8542 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8543 417a6e49 2020-09-26 stsp free(path);
8544 417a6e49 2020-09-26 stsp if (error)
8545 417a6e49 2020-09-26 stsp goto done;
8546 417a6e49 2020-09-26 stsp
8547 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8548 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8549 417a6e49 2020-09-26 stsp worktree, repo);
8550 417a6e49 2020-09-26 stsp if (error)
8551 417a6e49 2020-09-26 stsp goto done;
8552 417a6e49 2020-09-26 stsp
8553 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8554 417a6e49 2020-09-26 stsp if (error)
8555 417a6e49 2020-09-26 stsp goto done;
8556 417a6e49 2020-09-26 stsp
8557 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8558 417a6e49 2020-09-26 stsp head_commit_id);
8559 417a6e49 2020-09-26 stsp if (error)
8560 417a6e49 2020-09-26 stsp goto done;
8561 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8562 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8563 417a6e49 2020-09-26 stsp if (pid == NULL) {
8564 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8565 417a6e49 2020-09-26 stsp goto done;
8566 417a6e49 2020-09-26 stsp }
8567 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8568 417a6e49 2020-09-26 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8569 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8570 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8571 417a6e49 2020-09-26 stsp commit = NULL;
8572 417a6e49 2020-09-26 stsp if (error)
8573 417a6e49 2020-09-26 stsp goto done;
8574 417a6e49 2020-09-26 stsp } else {
8575 417a6e49 2020-09-26 stsp if (edit_in_progress) {
8576 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8577 417a6e49 2020-09-26 stsp goto done;
8578 417a6e49 2020-09-26 stsp }
8579 417a6e49 2020-09-26 stsp
8580 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo,
8581 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8582 417a6e49 2020-09-26 stsp if (error != NULL)
8583 417a6e49 2020-09-26 stsp goto done;
8584 417a6e49 2020-09-26 stsp
8585 417a6e49 2020-09-26 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8586 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8587 417a6e49 2020-09-26 stsp "will not edit commit history of a branch outside "
8588 417a6e49 2020-09-26 stsp "the \"refs/heads/\" reference namespace");
8589 417a6e49 2020-09-26 stsp goto done;
8590 417a6e49 2020-09-26 stsp }
8591 417a6e49 2020-09-26 stsp
8592 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8593 417a6e49 2020-09-26 stsp got_ref_close(branch);
8594 417a6e49 2020-09-26 stsp branch = NULL;
8595 417a6e49 2020-09-26 stsp if (error)
8596 417a6e49 2020-09-26 stsp goto done;
8597 417a6e49 2020-09-26 stsp
8598 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8599 417a6e49 2020-09-26 stsp head_commit_id);
8600 417a6e49 2020-09-26 stsp if (error)
8601 417a6e49 2020-09-26 stsp goto done;
8602 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8603 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8604 417a6e49 2020-09-26 stsp if (pid == NULL) {
8605 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8606 417a6e49 2020-09-26 stsp goto done;
8607 417a6e49 2020-09-26 stsp }
8608 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8609 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree),
8610 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
8611 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8612 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8613 417a6e49 2020-09-26 stsp commit = NULL;
8614 417a6e49 2020-09-26 stsp if (error)
8615 417a6e49 2020-09-26 stsp goto done;
8616 417a6e49 2020-09-26 stsp
8617 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
8618 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8619 417a6e49 2020-09-26 stsp goto done;
8620 417a6e49 2020-09-26 stsp }
8621 417a6e49 2020-09-26 stsp
8622 417a6e49 2020-09-26 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8623 417a6e49 2020-09-26 stsp &base_commit_id, &fileindex, worktree, repo);
8624 417a6e49 2020-09-26 stsp if (error)
8625 417a6e49 2020-09-26 stsp goto done;
8626 417a6e49 2020-09-26 stsp
8627 417a6e49 2020-09-26 stsp if (edit_script_path) {
8628 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds,
8629 417a6e49 2020-09-26 stsp edit_script_path, repo);
8630 417a6e49 2020-09-26 stsp if (error) {
8631 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8632 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8633 417a6e49 2020-09-26 stsp update_progress, &upa);
8634 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8635 417a6e49 2020-09-26 stsp goto done;
8636 417a6e49 2020-09-26 stsp }
8637 417a6e49 2020-09-26 stsp } else {
8638 417a6e49 2020-09-26 stsp const char *branch_name;
8639 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(branch);
8640 417a6e49 2020-09-26 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8641 417a6e49 2020-09-26 stsp branch_name += 11;
8642 417a6e49 2020-09-26 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8643 417a6e49 2020-09-26 stsp branch_name, edit_logmsg_only, repo);
8644 417a6e49 2020-09-26 stsp if (error) {
8645 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8646 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8647 417a6e49 2020-09-26 stsp update_progress, &upa);
8648 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8649 417a6e49 2020-09-26 stsp goto done;
8650 417a6e49 2020-09-26 stsp }
8651 417a6e49 2020-09-26 stsp
8652 417a6e49 2020-09-26 stsp }
8653 417a6e49 2020-09-26 stsp
8654 417a6e49 2020-09-26 stsp error = histedit_save_list(&histedit_cmds, worktree,
8655 417a6e49 2020-09-26 stsp repo);
8656 417a6e49 2020-09-26 stsp if (error) {
8657 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8658 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8659 417a6e49 2020-09-26 stsp update_progress, &upa);
8660 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8661 417a6e49 2020-09-26 stsp goto done;
8662 417a6e49 2020-09-26 stsp }
8663 417a6e49 2020-09-26 stsp
8664 417a6e49 2020-09-26 stsp }
8665 417a6e49 2020-09-26 stsp
8666 417a6e49 2020-09-26 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
8667 417a6e49 2020-09-26 stsp if (error)
8668 417a6e49 2020-09-26 stsp goto done;
8669 417a6e49 2020-09-26 stsp
8670 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8671 417a6e49 2020-09-26 stsp if (resume_commit_id) {
8672 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
8673 417a6e49 2020-09-26 stsp resume_commit_id) != 0)
8674 417a6e49 2020-09-26 stsp continue;
8675 417a6e49 2020-09-26 stsp
8676 417a6e49 2020-09-26 stsp resume_commit_id = NULL;
8677 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8678 417a6e49 2020-09-26 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8679 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree,
8680 417a6e49 2020-09-26 stsp repo);
8681 417a6e49 2020-09-26 stsp if (error)
8682 417a6e49 2020-09-26 stsp goto done;
8683 417a6e49 2020-09-26 stsp } else {
8684 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8685 417a6e49 2020-09-26 stsp int have_changes = 0;
8686 417a6e49 2020-09-26 stsp
8687 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
8688 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
8689 417a6e49 2020-09-26 stsp if (error)
8690 417a6e49 2020-09-26 stsp goto done;
8691 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths,
8692 417a6e49 2020-09-26 stsp repo, check_local_changes, &have_changes,
8693 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8694 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
8695 417a6e49 2020-09-26 stsp if (error) {
8696 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_CANCELLED)
8697 417a6e49 2020-09-26 stsp goto done;
8698 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
8699 417a6e49 2020-09-26 stsp goto done;
8700 417a6e49 2020-09-26 stsp }
8701 417a6e49 2020-09-26 stsp if (have_changes) {
8702 417a6e49 2020-09-26 stsp error = histedit_commit(NULL, worktree,
8703 417a6e49 2020-09-26 stsp fileindex, tmp_branch, hle, repo);
8704 417a6e49 2020-09-26 stsp if (error)
8705 417a6e49 2020-09-26 stsp goto done;
8706 417a6e49 2020-09-26 stsp } else {
8707 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(
8708 417a6e49 2020-09-26 stsp &commit, repo, hle->commit_id);
8709 417a6e49 2020-09-26 stsp if (error)
8710 417a6e49 2020-09-26 stsp goto done;
8711 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit,
8712 417a6e49 2020-09-26 stsp hle, NULL);
8713 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8714 417a6e49 2020-09-26 stsp commit = NULL;
8715 417a6e49 2020-09-26 stsp if (error)
8716 417a6e49 2020-09-26 stsp goto done;
8717 417a6e49 2020-09-26 stsp }
8718 417a6e49 2020-09-26 stsp }
8719 417a6e49 2020-09-26 stsp continue;
8720 417a6e49 2020-09-26 stsp }
8721 417a6e49 2020-09-26 stsp
8722 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8723 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8724 417a6e49 2020-09-26 stsp if (error)
8725 417a6e49 2020-09-26 stsp goto done;
8726 417a6e49 2020-09-26 stsp continue;
8727 417a6e49 2020-09-26 stsp }
8728 417a6e49 2020-09-26 stsp
8729 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8730 417a6e49 2020-09-26 stsp hle->commit_id);
8731 417a6e49 2020-09-26 stsp if (error)
8732 417a6e49 2020-09-26 stsp goto done;
8733 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8734 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8735 417a6e49 2020-09-26 stsp
8736 417a6e49 2020-09-26 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8737 417a6e49 2020-09-26 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8738 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
8739 417a6e49 2020-09-26 stsp if (error)
8740 417a6e49 2020-09-26 stsp goto done;
8741 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8742 417a6e49 2020-09-26 stsp commit = NULL;
8743 417a6e49 2020-09-26 stsp
8744 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8745 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
8746 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
8747 417a6e49 2020-09-26 stsp
8748 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8749 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(hle->commit_id,
8750 417a6e49 2020-09-26 stsp repo);
8751 417a6e49 2020-09-26 stsp if (error)
8752 417a6e49 2020-09-26 stsp goto done;
8753 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8754 417a6e49 2020-09-26 stsp break;
8755 417a6e49 2020-09-26 stsp }
8756 417a6e49 2020-09-26 stsp
8757 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8758 417a6e49 2020-09-26 stsp char *id_str;
8759 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, hle->commit_id);
8760 417a6e49 2020-09-26 stsp if (error)
8761 417a6e49 2020-09-26 stsp goto done;
8762 417a6e49 2020-09-26 stsp printf("Stopping histedit for amending commit %s\n",
8763 417a6e49 2020-09-26 stsp id_str);
8764 417a6e49 2020-09-26 stsp free(id_str);
8765 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8766 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree,
8767 417a6e49 2020-09-26 stsp fileindex);
8768 417a6e49 2020-09-26 stsp goto done;
8769 417a6e49 2020-09-26 stsp }
8770 417a6e49 2020-09-26 stsp
8771 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8772 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8773 417a6e49 2020-09-26 stsp if (error)
8774 417a6e49 2020-09-26 stsp goto done;
8775 417a6e49 2020-09-26 stsp continue;
8776 417a6e49 2020-09-26 stsp }
8777 417a6e49 2020-09-26 stsp
8778 417a6e49 2020-09-26 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8779 417a6e49 2020-09-26 stsp tmp_branch, hle, repo);
8780 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8781 417a6e49 2020-09-26 stsp if (error)
8782 417a6e49 2020-09-26 stsp goto done;
8783 417a6e49 2020-09-26 stsp }
8784 417a6e49 2020-09-26 stsp
8785 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8786 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8787 417a6e49 2020-09-26 stsp if (error)
8788 417a6e49 2020-09-26 stsp goto done;
8789 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8790 417a6e49 2020-09-26 stsp "conflicts must be resolved before histedit can continue");
8791 417a6e49 2020-09-26 stsp } else
8792 417a6e49 2020-09-26 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8793 417a6e49 2020-09-26 stsp branch, repo);
8794 417a6e49 2020-09-26 stsp done:
8795 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
8796 417a6e49 2020-09-26 stsp histedit_free_list(&histedit_cmds);
8797 417a6e49 2020-09-26 stsp free(head_commit_id);
8798 417a6e49 2020-09-26 stsp free(base_commit_id);
8799 417a6e49 2020-09-26 stsp free(resume_commit_id);
8800 417a6e49 2020-09-26 stsp if (commit)
8801 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8802 417a6e49 2020-09-26 stsp if (branch)
8803 417a6e49 2020-09-26 stsp got_ref_close(branch);
8804 417a6e49 2020-09-26 stsp if (tmp_branch)
8805 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
8806 417a6e49 2020-09-26 stsp if (worktree)
8807 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8808 417a6e49 2020-09-26 stsp if (repo)
8809 417a6e49 2020-09-26 stsp got_repo_close(repo);
8810 417a6e49 2020-09-26 stsp return error;
8811 417a6e49 2020-09-26 stsp }
8812 417a6e49 2020-09-26 stsp
8813 417a6e49 2020-09-26 stsp __dead static void
8814 417a6e49 2020-09-26 stsp usage_integrate(void)
8815 417a6e49 2020-09-26 stsp {
8816 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8817 417a6e49 2020-09-26 stsp exit(1);
8818 417a6e49 2020-09-26 stsp }
8819 417a6e49 2020-09-26 stsp
8820 417a6e49 2020-09-26 stsp static const struct got_error *
8821 417a6e49 2020-09-26 stsp cmd_integrate(int argc, char *argv[])
8822 417a6e49 2020-09-26 stsp {
8823 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8824 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8825 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8826 417a6e49 2020-09-26 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8827 417a6e49 2020-09-26 stsp const char *branch_arg = NULL;
8828 417a6e49 2020-09-26 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8829 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8830 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8831 417a6e49 2020-09-26 stsp int ch;
8832 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8833 417a6e49 2020-09-26 stsp
8834 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8835 417a6e49 2020-09-26 stsp switch (ch) {
8836 417a6e49 2020-09-26 stsp default:
8837 417a6e49 2020-09-26 stsp usage_integrate();
8838 417a6e49 2020-09-26 stsp /* NOTREACHED */
8839 417a6e49 2020-09-26 stsp }
8840 417a6e49 2020-09-26 stsp }
8841 417a6e49 2020-09-26 stsp
8842 417a6e49 2020-09-26 stsp argc -= optind;
8843 417a6e49 2020-09-26 stsp argv += optind;
8844 417a6e49 2020-09-26 stsp
8845 417a6e49 2020-09-26 stsp if (argc != 1)
8846 417a6e49 2020-09-26 stsp usage_integrate();
8847 417a6e49 2020-09-26 stsp branch_arg = argv[0];
8848 417a6e49 2020-09-26 stsp
8849 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8850 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8851 417a6e49 2020-09-26 stsp err(1, "pledge");
8852 417a6e49 2020-09-26 stsp
8853 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8854 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8855 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8856 417a6e49 2020-09-26 stsp goto done;
8857 417a6e49 2020-09-26 stsp }
8858 417a6e49 2020-09-26 stsp
8859 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8860 417a6e49 2020-09-26 stsp if (error) {
8861 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8862 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "integrate",
8863 417a6e49 2020-09-26 stsp cwd);
8864 417a6e49 2020-09-26 stsp goto done;
8865 417a6e49 2020-09-26 stsp }
8866 417a6e49 2020-09-26 stsp
8867 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
8868 417a6e49 2020-09-26 stsp if (error)
8869 417a6e49 2020-09-26 stsp goto done;
8870 417a6e49 2020-09-26 stsp
8871 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8872 417a6e49 2020-09-26 stsp NULL);
8873 417a6e49 2020-09-26 stsp if (error != NULL)
8874 417a6e49 2020-09-26 stsp goto done;
8875 417a6e49 2020-09-26 stsp
8876 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8877 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
8878 417a6e49 2020-09-26 stsp if (error)
8879 417a6e49 2020-09-26 stsp goto done;
8880 417a6e49 2020-09-26 stsp
8881 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8882 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
8883 417a6e49 2020-09-26 stsp goto done;
8884 417a6e49 2020-09-26 stsp }
8885 417a6e49 2020-09-26 stsp
8886 417a6e49 2020-09-26 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8887 417a6e49 2020-09-26 stsp &base_branch_ref, worktree, refname, repo);
8888 417a6e49 2020-09-26 stsp if (error)
8889 417a6e49 2020-09-26 stsp goto done;
8890 417a6e49 2020-09-26 stsp
8891 417a6e49 2020-09-26 stsp refname = strdup(got_ref_get_name(branch_ref));
8892 417a6e49 2020-09-26 stsp if (refname == NULL) {
8893 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8894 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8895 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8896 417a6e49 2020-09-26 stsp goto done;
8897 417a6e49 2020-09-26 stsp }
8898 417a6e49 2020-09-26 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8899 417a6e49 2020-09-26 stsp if (base_refname == NULL) {
8900 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8901 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8902 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8903 417a6e49 2020-09-26 stsp goto done;
8904 417a6e49 2020-09-26 stsp }
8905 417a6e49 2020-09-26 stsp
8906 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8907 417a6e49 2020-09-26 stsp if (error)
8908 417a6e49 2020-09-26 stsp goto done;
8909 417a6e49 2020-09-26 stsp
8910 417a6e49 2020-09-26 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8911 417a6e49 2020-09-26 stsp if (error)
8912 417a6e49 2020-09-26 stsp goto done;
8913 417a6e49 2020-09-26 stsp
8914 417a6e49 2020-09-26 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8915 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8916 417a6e49 2020-09-26 stsp "specified branch has already been integrated");
8917 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8918 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8919 417a6e49 2020-09-26 stsp goto done;
8920 417a6e49 2020-09-26 stsp }
8921 417a6e49 2020-09-26 stsp
8922 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8923 417a6e49 2020-09-26 stsp if (error) {
8924 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
8925 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8926 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8927 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8928 417a6e49 2020-09-26 stsp goto done;
8929 417a6e49 2020-09-26 stsp }
8930 417a6e49 2020-09-26 stsp
8931 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8932 417a6e49 2020-09-26 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8933 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref, update_progress, &upa,
8934 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8935 417a6e49 2020-09-26 stsp if (error)
8936 417a6e49 2020-09-26 stsp goto done;
8937 417a6e49 2020-09-26 stsp
8938 417a6e49 2020-09-26 stsp printf("Integrated %s into %s\n", refname, base_refname);
8939 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8940 417a6e49 2020-09-26 stsp done:
8941 417a6e49 2020-09-26 stsp if (repo)
8942 417a6e49 2020-09-26 stsp got_repo_close(repo);
8943 417a6e49 2020-09-26 stsp if (worktree)
8944 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8945 417a6e49 2020-09-26 stsp free(cwd);
8946 417a6e49 2020-09-26 stsp free(base_commit_id);
8947 417a6e49 2020-09-26 stsp free(commit_id);
8948 417a6e49 2020-09-26 stsp free(refname);
8949 417a6e49 2020-09-26 stsp free(base_refname);
8950 417a6e49 2020-09-26 stsp return error;
8951 417a6e49 2020-09-26 stsp }
8952 417a6e49 2020-09-26 stsp
8953 417a6e49 2020-09-26 stsp __dead static void
8954 417a6e49 2020-09-26 stsp usage_stage(void)
8955 417a6e49 2020-09-26 stsp {
8956 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8957 417a6e49 2020-09-26 stsp "[-S] [file-path ...]\n",
8958 417a6e49 2020-09-26 stsp getprogname());
8959 417a6e49 2020-09-26 stsp exit(1);
8960 417a6e49 2020-09-26 stsp }
8961 417a6e49 2020-09-26 stsp
8962 417a6e49 2020-09-26 stsp static const struct got_error *
8963 417a6e49 2020-09-26 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8964 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
8965 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8966 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
8967 417a6e49 2020-09-26 stsp {
8968 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8969 417a6e49 2020-09-26 stsp char *id_str = NULL;
8970 417a6e49 2020-09-26 stsp
8971 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_ADD &&
8972 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_MODIFY &&
8973 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
8974 417a6e49 2020-09-26 stsp return NULL;
8975 417a6e49 2020-09-26 stsp
8976 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
8977 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY)
8978 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
8979 417a6e49 2020-09-26 stsp else
8980 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
8981 417a6e49 2020-09-26 stsp if (err)
8982 417a6e49 2020-09-26 stsp return err;
8983 417a6e49 2020-09-26 stsp
8984 417a6e49 2020-09-26 stsp printf("%s %c %s\n", id_str, staged_status, path);
8985 417a6e49 2020-09-26 stsp free(id_str);
8986 417a6e49 2020-09-26 stsp return NULL;
8987 417a6e49 2020-09-26 stsp }
8988 417a6e49 2020-09-26 stsp
8989 417a6e49 2020-09-26 stsp static const struct got_error *
8990 417a6e49 2020-09-26 stsp cmd_stage(int argc, char *argv[])
8991 417a6e49 2020-09-26 stsp {
8992 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8993 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8994 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8995 417a6e49 2020-09-26 stsp char *cwd = NULL;
8996 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8997 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
8998 417a6e49 2020-09-26 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8999 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
9000 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
9001 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
9002 417a6e49 2020-09-26 stsp
9003 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9004 417a6e49 2020-09-26 stsp
9005 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9006 417a6e49 2020-09-26 stsp switch (ch) {
9007 417a6e49 2020-09-26 stsp case 'l':
9008 417a6e49 2020-09-26 stsp list_stage = 1;
9009 417a6e49 2020-09-26 stsp break;
9010 417a6e49 2020-09-26 stsp case 'p':
9011 417a6e49 2020-09-26 stsp pflag = 1;
9012 417a6e49 2020-09-26 stsp break;
9013 417a6e49 2020-09-26 stsp case 'F':
9014 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9015 417a6e49 2020-09-26 stsp break;
9016 417a6e49 2020-09-26 stsp case 'S':
9017 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
9018 417a6e49 2020-09-26 stsp break;
9019 417a6e49 2020-09-26 stsp default:
9020 417a6e49 2020-09-26 stsp usage_stage();
9021 417a6e49 2020-09-26 stsp /* NOTREACHED */
9022 417a6e49 2020-09-26 stsp }
9023 417a6e49 2020-09-26 stsp }
9024 417a6e49 2020-09-26 stsp
9025 417a6e49 2020-09-26 stsp argc -= optind;
9026 417a6e49 2020-09-26 stsp argv += optind;
9027 417a6e49 2020-09-26 stsp
9028 417a6e49 2020-09-26 stsp #ifndef PROFILE
9029 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9030 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9031 417a6e49 2020-09-26 stsp err(1, "pledge");
9032 417a6e49 2020-09-26 stsp #endif
9033 417a6e49 2020-09-26 stsp if (list_stage && (pflag || patch_script_path))
9034 417a6e49 2020-09-26 stsp errx(1, "-l option cannot be used with other options");
9035 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9036 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9037 417a6e49 2020-09-26 stsp
9038 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9039 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9040 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9041 417a6e49 2020-09-26 stsp goto done;
9042 417a6e49 2020-09-26 stsp }
9043 417a6e49 2020-09-26 stsp
9044 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9045 417a6e49 2020-09-26 stsp if (error) {
9046 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9047 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9048 417a6e49 2020-09-26 stsp goto done;
9049 417a6e49 2020-09-26 stsp }
9050 417a6e49 2020-09-26 stsp
9051 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9052 417a6e49 2020-09-26 stsp NULL);
9053 417a6e49 2020-09-26 stsp if (error != NULL)
9054 417a6e49 2020-09-26 stsp goto done;
9055 417a6e49 2020-09-26 stsp
9056 417a6e49 2020-09-26 stsp if (patch_script_path) {
9057 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9058 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9059 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9060 417a6e49 2020-09-26 stsp patch_script_path);
9061 417a6e49 2020-09-26 stsp goto done;
9062 417a6e49 2020-09-26 stsp }
9063 417a6e49 2020-09-26 stsp }
9064 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9065 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9066 417a6e49 2020-09-26 stsp if (error)
9067 417a6e49 2020-09-26 stsp goto done;
9068 417a6e49 2020-09-26 stsp
9069 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9070 417a6e49 2020-09-26 stsp if (error)
9071 417a6e49 2020-09-26 stsp goto done;
9072 417a6e49 2020-09-26 stsp
9073 417a6e49 2020-09-26 stsp if (list_stage)
9074 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo,
9075 417a6e49 2020-09-26 stsp print_stage, NULL, check_cancelled, NULL);
9076 417a6e49 2020-09-26 stsp else {
9077 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9078 417a6e49 2020-09-26 stsp cpa.action = "stage";
9079 417a6e49 2020-09-26 stsp error = got_worktree_stage(worktree, &paths,
9080 417a6e49 2020-09-26 stsp pflag ? NULL : print_status, NULL,
9081 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa,
9082 417a6e49 2020-09-26 stsp allow_bad_symlinks, repo);
9083 417a6e49 2020-09-26 stsp }
9084 417a6e49 2020-09-26 stsp done:
9085 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9086 417a6e49 2020-09-26 stsp error == NULL)
9087 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9088 417a6e49 2020-09-26 stsp if (repo)
9089 417a6e49 2020-09-26 stsp got_repo_close(repo);
9090 417a6e49 2020-09-26 stsp if (worktree)
9091 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9092 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9093 417a6e49 2020-09-26 stsp free((char *)pe->path);
9094 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9095 417a6e49 2020-09-26 stsp free(cwd);
9096 417a6e49 2020-09-26 stsp return error;
9097 417a6e49 2020-09-26 stsp }
9098 417a6e49 2020-09-26 stsp
9099 417a6e49 2020-09-26 stsp __dead static void
9100 417a6e49 2020-09-26 stsp usage_unstage(void)
9101 417a6e49 2020-09-26 stsp {
9102 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9103 417a6e49 2020-09-26 stsp "[file-path ...]\n",
9104 417a6e49 2020-09-26 stsp getprogname());
9105 417a6e49 2020-09-26 stsp exit(1);
9106 417a6e49 2020-09-26 stsp }
9107 417a6e49 2020-09-26 stsp
9108 417a6e49 2020-09-26 stsp
9109 417a6e49 2020-09-26 stsp static const struct got_error *
9110 417a6e49 2020-09-26 stsp cmd_unstage(int argc, char *argv[])
9111 417a6e49 2020-09-26 stsp {
9112 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9113 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9114 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9115 417a6e49 2020-09-26 stsp char *cwd = NULL;
9116 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9117 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9118 417a6e49 2020-09-26 stsp int ch, pflag = 0;
9119 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
9120 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
9121 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
9122 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
9123 417a6e49 2020-09-26 stsp
9124 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9125 417a6e49 2020-09-26 stsp
9126 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9127 417a6e49 2020-09-26 stsp switch (ch) {
9128 417a6e49 2020-09-26 stsp case 'p':
9129 417a6e49 2020-09-26 stsp pflag = 1;
9130 417a6e49 2020-09-26 stsp break;
9131 417a6e49 2020-09-26 stsp case 'F':
9132 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9133 417a6e49 2020-09-26 stsp break;
9134 417a6e49 2020-09-26 stsp default:
9135 417a6e49 2020-09-26 stsp usage_unstage();
9136 417a6e49 2020-09-26 stsp /* NOTREACHED */
9137 417a6e49 2020-09-26 stsp }
9138 417a6e49 2020-09-26 stsp }
9139 417a6e49 2020-09-26 stsp
9140 417a6e49 2020-09-26 stsp argc -= optind;
9141 417a6e49 2020-09-26 stsp argv += optind;
9142 417a6e49 2020-09-26 stsp
9143 417a6e49 2020-09-26 stsp #ifndef PROFILE
9144 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9145 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9146 417a6e49 2020-09-26 stsp err(1, "pledge");
9147 417a6e49 2020-09-26 stsp #endif
9148 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9149 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9150 417a6e49 2020-09-26 stsp
9151 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9152 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9153 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9154 417a6e49 2020-09-26 stsp goto done;
9155 417a6e49 2020-09-26 stsp }
9156 417a6e49 2020-09-26 stsp
9157 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9158 417a6e49 2020-09-26 stsp if (error) {
9159 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9160 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9161 417a6e49 2020-09-26 stsp goto done;
9162 417a6e49 2020-09-26 stsp }
9163 417a6e49 2020-09-26 stsp
9164 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9165 417a6e49 2020-09-26 stsp NULL);
9166 417a6e49 2020-09-26 stsp if (error != NULL)
9167 417a6e49 2020-09-26 stsp goto done;
9168 417a6e49 2020-09-26 stsp
9169 417a6e49 2020-09-26 stsp if (patch_script_path) {
9170 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9171 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9172 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9173 417a6e49 2020-09-26 stsp patch_script_path);
9174 417a6e49 2020-09-26 stsp goto done;
9175 417a6e49 2020-09-26 stsp }
9176 417a6e49 2020-09-26 stsp }
9177 417a6e49 2020-09-26 stsp
9178 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9179 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9180 417a6e49 2020-09-26 stsp if (error)
9181 417a6e49 2020-09-26 stsp goto done;
9182 417a6e49 2020-09-26 stsp
9183 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9184 417a6e49 2020-09-26 stsp if (error)
9185 417a6e49 2020-09-26 stsp goto done;
9186 417a6e49 2020-09-26 stsp
9187 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9188 417a6e49 2020-09-26 stsp cpa.action = "unstage";
9189 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
9190 417a6e49 2020-09-26 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9191 417a6e49 2020-09-26 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9192 417a6e49 2020-09-26 stsp if (!error)
9193 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
9194 417a6e49 2020-09-26 stsp done:
9195 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9196 417a6e49 2020-09-26 stsp error == NULL)
9197 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9198 417a6e49 2020-09-26 stsp if (repo)
9199 417a6e49 2020-09-26 stsp got_repo_close(repo);
9200 417a6e49 2020-09-26 stsp if (worktree)
9201 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9202 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9203 417a6e49 2020-09-26 stsp free((char *)pe->path);
9204 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9205 417a6e49 2020-09-26 stsp free(cwd);
9206 417a6e49 2020-09-26 stsp return error;
9207 417a6e49 2020-09-26 stsp }
9208 417a6e49 2020-09-26 stsp
9209 417a6e49 2020-09-26 stsp __dead static void
9210 417a6e49 2020-09-26 stsp usage_cat(void)
9211 417a6e49 2020-09-26 stsp {
9212 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9213 417a6e49 2020-09-26 stsp "arg1 [arg2 ...]\n", getprogname());
9214 417a6e49 2020-09-26 stsp exit(1);
9215 417a6e49 2020-09-26 stsp }
9216 417a6e49 2020-09-26 stsp
9217 417a6e49 2020-09-26 stsp static const struct got_error *
9218 417a6e49 2020-09-26 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9219 417a6e49 2020-09-26 stsp {
9220 417a6e49 2020-09-26 stsp const struct got_error *err;
9221 417a6e49 2020-09-26 stsp struct got_blob_object *blob;
9222 417a6e49 2020-09-26 stsp
9223 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9224 417a6e49 2020-09-26 stsp if (err)
9225 417a6e49 2020-09-26 stsp return err;
9226 417a6e49 2020-09-26 stsp
9227 417a6e49 2020-09-26 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9228 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
9229 417a6e49 2020-09-26 stsp return err;
9230 417a6e49 2020-09-26 stsp }
9231 417a6e49 2020-09-26 stsp
9232 417a6e49 2020-09-26 stsp static const struct got_error *
9233 417a6e49 2020-09-26 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9234 417a6e49 2020-09-26 stsp {
9235 417a6e49 2020-09-26 stsp const struct got_error *err;
9236 417a6e49 2020-09-26 stsp struct got_tree_object *tree;
9237 417a6e49 2020-09-26 stsp int nentries, i;
9238 417a6e49 2020-09-26 stsp
9239 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, id);
9240 417a6e49 2020-09-26 stsp if (err)
9241 417a6e49 2020-09-26 stsp return err;
9242 417a6e49 2020-09-26 stsp
9243 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
9244 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
9245 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
9246 417a6e49 2020-09-26 stsp char *id_str;
9247 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
9248 417a6e49 2020-09-26 stsp break;
9249 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
9250 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9251 417a6e49 2020-09-26 stsp if (err)
9252 417a6e49 2020-09-26 stsp break;
9253 417a6e49 2020-09-26 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9254 417a6e49 2020-09-26 stsp got_tree_entry_get_mode(te),
9255 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te));
9256 417a6e49 2020-09-26 stsp free(id_str);
9257 417a6e49 2020-09-26 stsp }
9258 417a6e49 2020-09-26 stsp
9259 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
9260 417a6e49 2020-09-26 stsp return err;
9261 417a6e49 2020-09-26 stsp }
9262 417a6e49 2020-09-26 stsp
9263 417a6e49 2020-09-26 stsp static const struct got_error *
9264 417a6e49 2020-09-26 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9265 417a6e49 2020-09-26 stsp {
9266 417a6e49 2020-09-26 stsp const struct got_error *err;
9267 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
9268 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
9269 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
9270 417a6e49 2020-09-26 stsp char *id_str = NULL;
9271 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
9272 417a6e49 2020-09-26 stsp
9273 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
9274 417a6e49 2020-09-26 stsp if (err)
9275 417a6e49 2020-09-26 stsp return err;
9276 417a6e49 2020-09-26 stsp
9277 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9278 417a6e49 2020-09-26 stsp if (err)
9279 417a6e49 2020-09-26 stsp goto done;
9280 417a6e49 2020-09-26 stsp
9281 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9282 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9283 417a6e49 2020-09-26 stsp fprintf(outfile, "numparents %d\n",
9284 417a6e49 2020-09-26 stsp got_object_commit_get_nparents(commit));
9285 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9286 417a6e49 2020-09-26 stsp char *pid_str;
9287 417a6e49 2020-09-26 stsp err = got_object_id_str(&pid_str, pid->id);
9288 417a6e49 2020-09-26 stsp if (err)
9289 417a6e49 2020-09-26 stsp goto done;
9290 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9291 417a6e49 2020-09-26 stsp free(pid_str);
9292 417a6e49 2020-09-26 stsp }
9293 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9294 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9295 417a6e49 2020-09-26 stsp got_object_commit_get_author_time(commit));
9296 417a6e49 2020-09-26 stsp
9297 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9298 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9299 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit));
9300 417a6e49 2020-09-26 stsp
9301 417a6e49 2020-09-26 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9302 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9303 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", logmsg);
9304 417a6e49 2020-09-26 stsp done:
9305 417a6e49 2020-09-26 stsp free(id_str);
9306 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
9307 417a6e49 2020-09-26 stsp return err;
9308 417a6e49 2020-09-26 stsp }
9309 417a6e49 2020-09-26 stsp
9310 417a6e49 2020-09-26 stsp static const struct got_error *
9311 417a6e49 2020-09-26 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9312 417a6e49 2020-09-26 stsp {
9313 417a6e49 2020-09-26 stsp const struct got_error *err;
9314 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
9315 417a6e49 2020-09-26 stsp char *id_str = NULL;
9316 417a6e49 2020-09-26 stsp const char *tagmsg = NULL;
9317 417a6e49 2020-09-26 stsp
9318 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
9319 417a6e49 2020-09-26 stsp if (err)
9320 417a6e49 2020-09-26 stsp return err;
9321 417a6e49 2020-09-26 stsp
9322 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9323 417a6e49 2020-09-26 stsp if (err)
9324 417a6e49 2020-09-26 stsp goto done;
9325 417a6e49 2020-09-26 stsp
9326 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9327 417a6e49 2020-09-26 stsp
9328 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
9329 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9330 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9331 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_BLOB);
9332 417a6e49 2020-09-26 stsp break;
9333 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9334 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9335 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TREE);
9336 417a6e49 2020-09-26 stsp break;
9337 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9338 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9339 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_COMMIT);
9340 417a6e49 2020-09-26 stsp break;
9341 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9342 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9343 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TAG);
9344 417a6e49 2020-09-26 stsp break;
9345 417a6e49 2020-09-26 stsp default:
9346 417a6e49 2020-09-26 stsp break;
9347 417a6e49 2020-09-26 stsp }
9348 417a6e49 2020-09-26 stsp
9349 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9350 417a6e49 2020-09-26 stsp got_object_tag_get_name(tag));
9351 417a6e49 2020-09-26 stsp
9352 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9353 417a6e49 2020-09-26 stsp got_object_tag_get_tagger(tag),
9354 417a6e49 2020-09-26 stsp got_object_tag_get_tagger_time(tag));
9355 417a6e49 2020-09-26 stsp
9356 417a6e49 2020-09-26 stsp tagmsg = got_object_tag_get_message(tag);
9357 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9358 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", tagmsg);
9359 417a6e49 2020-09-26 stsp done:
9360 417a6e49 2020-09-26 stsp free(id_str);
9361 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
9362 417a6e49 2020-09-26 stsp return err;
9363 417a6e49 2020-09-26 stsp }
9364 417a6e49 2020-09-26 stsp
9365 417a6e49 2020-09-26 stsp static const struct got_error *
9366 417a6e49 2020-09-26 stsp cmd_cat(int argc, char *argv[])
9367 417a6e49 2020-09-26 stsp {
9368 417a6e49 2020-09-26 stsp const struct got_error *error;
9369 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9370 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9371 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9372 417a6e49 2020-09-26 stsp const char *commit_id_str = NULL;
9373 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9374 417a6e49 2020-09-26 stsp int ch, obj_type, i, force_path = 0;
9375 417a6e49 2020-09-26 stsp
9376 417a6e49 2020-09-26 stsp #ifndef PROFILE
9377 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9378 417a6e49 2020-09-26 stsp NULL) == -1)
9379 417a6e49 2020-09-26 stsp err(1, "pledge");
9380 417a6e49 2020-09-26 stsp #endif
9381 417a6e49 2020-09-26 stsp
9382 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9383 417a6e49 2020-09-26 stsp switch (ch) {
9384 417a6e49 2020-09-26 stsp case 'c':
9385 417a6e49 2020-09-26 stsp commit_id_str = optarg;
9386 417a6e49 2020-09-26 stsp break;
9387 417a6e49 2020-09-26 stsp case 'r':
9388 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
9389 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9390 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
9391 417a6e49 2020-09-26 stsp optarg);
9392 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
9393 417a6e49 2020-09-26 stsp break;
9394 417a6e49 2020-09-26 stsp case 'P':
9395 417a6e49 2020-09-26 stsp force_path = 1;
9396 417a6e49 2020-09-26 stsp break;
9397 417a6e49 2020-09-26 stsp default:
9398 417a6e49 2020-09-26 stsp usage_cat();
9399 417a6e49 2020-09-26 stsp /* NOTREACHED */
9400 417a6e49 2020-09-26 stsp }
9401 417a6e49 2020-09-26 stsp }
9402 417a6e49 2020-09-26 stsp
9403 417a6e49 2020-09-26 stsp argc -= optind;
9404 417a6e49 2020-09-26 stsp argv += optind;
9405 417a6e49 2020-09-26 stsp
9406 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9407 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9408 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9409 417a6e49 2020-09-26 stsp goto done;
9410 417a6e49 2020-09-26 stsp }
9411 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9412 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9413 417a6e49 2020-09-26 stsp goto done;
9414 417a6e49 2020-09-26 stsp if (worktree) {
9415 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9416 417a6e49 2020-09-26 stsp repo_path = strdup(
9417 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
9418 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9419 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
9420 417a6e49 2020-09-26 stsp goto done;
9421 417a6e49 2020-09-26 stsp }
9422 417a6e49 2020-09-26 stsp }
9423 417a6e49 2020-09-26 stsp }
9424 417a6e49 2020-09-26 stsp
9425 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9426 417a6e49 2020-09-26 stsp repo_path = getcwd(NULL, 0);
9427 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9428 417a6e49 2020-09-26 stsp return got_error_from_errno("getcwd");
9429 417a6e49 2020-09-26 stsp }
9430 417a6e49 2020-09-26 stsp
9431 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
9432 417a6e49 2020-09-26 stsp free(repo_path);
9433 417a6e49 2020-09-26 stsp if (error != NULL)
9434 417a6e49 2020-09-26 stsp goto done;
9435 417a6e49 2020-09-26 stsp
9436 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9437 417a6e49 2020-09-26 stsp if (error)
9438 417a6e49 2020-09-26 stsp goto done;
9439 417a6e49 2020-09-26 stsp
9440 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
9441 417a6e49 2020-09-26 stsp commit_id_str = GOT_REF_HEAD;
9442 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
9443 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9444 417a6e49 2020-09-26 stsp if (error)
9445 417a6e49 2020-09-26 stsp goto done;
9446 417a6e49 2020-09-26 stsp
9447 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
9448 417a6e49 2020-09-26 stsp if (force_path) {
9449 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9450 417a6e49 2020-09-26 stsp argv[i]);
9451 417a6e49 2020-09-26 stsp if (error)
9452 417a6e49 2020-09-26 stsp break;
9453 417a6e49 2020-09-26 stsp } else {
9454 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9455 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9456 417a6e49 2020-09-26 stsp if (error) {
9457 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9458 417a6e49 2020-09-26 stsp error->code != GOT_ERR_NOT_REF)
9459 417a6e49 2020-09-26 stsp break;
9460 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo,
9461 417a6e49 2020-09-26 stsp commit_id, argv[i]);
9462 417a6e49 2020-09-26 stsp if (error)
9463 417a6e49 2020-09-26 stsp break;
9464 417a6e49 2020-09-26 stsp }
9465 417a6e49 2020-09-26 stsp }
9466 417a6e49 2020-09-26 stsp
9467 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, id);
9468 417a6e49 2020-09-26 stsp if (error)
9469 417a6e49 2020-09-26 stsp break;
9470 417a6e49 2020-09-26 stsp
9471 417a6e49 2020-09-26 stsp switch (obj_type) {
9472 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9473 417a6e49 2020-09-26 stsp error = cat_blob(id, repo, stdout);
9474 417a6e49 2020-09-26 stsp break;
9475 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9476 417a6e49 2020-09-26 stsp error = cat_tree(id, repo, stdout);
9477 417a6e49 2020-09-26 stsp break;
9478 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9479 417a6e49 2020-09-26 stsp error = cat_commit(id, repo, stdout);
9480 417a6e49 2020-09-26 stsp break;
9481 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9482 417a6e49 2020-09-26 stsp error = cat_tag(id, repo, stdout);
9483 417a6e49 2020-09-26 stsp break;
9484 417a6e49 2020-09-26 stsp default:
9485 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9486 417a6e49 2020-09-26 stsp break;
9487 417a6e49 2020-09-26 stsp }
9488 417a6e49 2020-09-26 stsp if (error)
9489 417a6e49 2020-09-26 stsp break;
9490 417a6e49 2020-09-26 stsp free(label);
9491 417a6e49 2020-09-26 stsp label = NULL;
9492 417a6e49 2020-09-26 stsp free(id);
9493 417a6e49 2020-09-26 stsp id = NULL;
9494 417a6e49 2020-09-26 stsp }
9495 417a6e49 2020-09-26 stsp done:
9496 417a6e49 2020-09-26 stsp free(label);
9497 417a6e49 2020-09-26 stsp free(id);
9498 417a6e49 2020-09-26 stsp free(commit_id);
9499 417a6e49 2020-09-26 stsp if (worktree)
9500 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9501 417a6e49 2020-09-26 stsp if (repo) {
9502 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
9503 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
9504 417a6e49 2020-09-26 stsp if (error == NULL)
9505 417a6e49 2020-09-26 stsp error = repo_error;
9506 417a6e49 2020-09-26 stsp }
9507 417a6e49 2020-09-26 stsp return error;
9508 417a6e49 2020-09-26 stsp }
9509 417a6e49 2020-09-26 stsp
9510 417a6e49 2020-09-26 stsp __dead static void
9511 417a6e49 2020-09-26 stsp usage_info(void)
9512 417a6e49 2020-09-26 stsp {
9513 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9514 417a6e49 2020-09-26 stsp getprogname());
9515 417a6e49 2020-09-26 stsp exit(1);
9516 417a6e49 2020-09-26 stsp }
9517 417a6e49 2020-09-26 stsp
9518 417a6e49 2020-09-26 stsp static const struct got_error *
9519 417a6e49 2020-09-26 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9520 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9521 417a6e49 2020-09-26 stsp struct got_object_id *commit_id)
9522 417a6e49 2020-09-26 stsp {
9523 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
9524 417a6e49 2020-09-26 stsp char *id_str = NULL;
9525 417a6e49 2020-09-26 stsp char datebuf[128];
9526 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
9527 417a6e49 2020-09-26 stsp struct got_pathlist_head *paths = arg;
9528 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9529 417a6e49 2020-09-26 stsp
9530 417a6e49 2020-09-26 stsp /*
9531 417a6e49 2020-09-26 stsp * Clear error indication from any of the path arguments which
9532 417a6e49 2020-09-26 stsp * would cause this file index entry to be displayed.
9533 417a6e49 2020-09-26 stsp */
9534 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, paths, entry) {
9535 417a6e49 2020-09-26 stsp if (got_path_cmp(path, pe->path, strlen(path),
9536 417a6e49 2020-09-26 stsp pe->path_len) == 0 ||
9537 417a6e49 2020-09-26 stsp got_path_is_child(path, pe->path, pe->path_len))
9538 417a6e49 2020-09-26 stsp pe->data = NULL; /* no error */
9539 417a6e49 2020-09-26 stsp }
9540 417a6e49 2020-09-26 stsp
9541 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
9542 417a6e49 2020-09-26 stsp if (S_ISLNK(mode))
9543 417a6e49 2020-09-26 stsp printf("symlink: %s\n", path);
9544 417a6e49 2020-09-26 stsp else if (S_ISREG(mode)) {
9545 417a6e49 2020-09-26 stsp printf("file: %s\n", path);
9546 417a6e49 2020-09-26 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9547 417a6e49 2020-09-26 stsp } else if (S_ISDIR(mode))
9548 417a6e49 2020-09-26 stsp printf("directory: %s\n", path);
9549 417a6e49 2020-09-26 stsp else
9550 417a6e49 2020-09-26 stsp printf("something: %s\n", path);
9551 417a6e49 2020-09-26 stsp
9552 417a6e49 2020-09-26 stsp tm = localtime_r(&mtime, &mytm);
9553 417a6e49 2020-09-26 stsp if (tm == NULL)
9554 417a6e49 2020-09-26 stsp return NULL;
9555 417a6e49 2020-09-26 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9556 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NO_SPACE);
9557 417a6e49 2020-09-26 stsp printf("timestamp: %s\n", datebuf);
9558 417a6e49 2020-09-26 stsp
9559 417a6e49 2020-09-26 stsp if (blob_id) {
9560 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
9561 417a6e49 2020-09-26 stsp if (err)
9562 417a6e49 2020-09-26 stsp return err;
9563 417a6e49 2020-09-26 stsp printf("based on blob: %s\n", id_str);
9564 417a6e49 2020-09-26 stsp free(id_str);
9565 417a6e49 2020-09-26 stsp }
9566 417a6e49 2020-09-26 stsp
9567 417a6e49 2020-09-26 stsp if (staged_blob_id) {
9568 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
9569 417a6e49 2020-09-26 stsp if (err)
9570 417a6e49 2020-09-26 stsp return err;
9571 417a6e49 2020-09-26 stsp printf("based on staged blob: %s\n", id_str);
9572 417a6e49 2020-09-26 stsp free(id_str);
9573 417a6e49 2020-09-26 stsp }
9574 417a6e49 2020-09-26 stsp
9575 417a6e49 2020-09-26 stsp if (commit_id) {
9576 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
9577 417a6e49 2020-09-26 stsp if (err)
9578 417a6e49 2020-09-26 stsp return err;
9579 417a6e49 2020-09-26 stsp printf("based on commit: %s\n", id_str);
9580 417a6e49 2020-09-26 stsp free(id_str);
9581 417a6e49 2020-09-26 stsp }
9582 417a6e49 2020-09-26 stsp
9583 417a6e49 2020-09-26 stsp return NULL;
9584 417a6e49 2020-09-26 stsp }
9585 417a6e49 2020-09-26 stsp
9586 417a6e49 2020-09-26 stsp static const struct got_error *
9587 417a6e49 2020-09-26 stsp cmd_info(int argc, char *argv[])
9588 417a6e49 2020-09-26 stsp {
9589 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9590 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9591 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
9592 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9593 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9594 417a6e49 2020-09-26 stsp char *uuidstr = NULL;
9595 417a6e49 2020-09-26 stsp int ch, show_files = 0;
9596 417a6e49 2020-09-26 stsp
9597 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9598 417a6e49 2020-09-26 stsp
9599 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9600 417a6e49 2020-09-26 stsp switch (ch) {
9601 417a6e49 2020-09-26 stsp default:
9602 417a6e49 2020-09-26 stsp usage_info();
9603 417a6e49 2020-09-26 stsp /* NOTREACHED */
9604 417a6e49 2020-09-26 stsp }
9605 417a6e49 2020-09-26 stsp }
9606 417a6e49 2020-09-26 stsp
9607 417a6e49 2020-09-26 stsp argc -= optind;
9608 417a6e49 2020-09-26 stsp argv += optind;
9609 417a6e49 2020-09-26 stsp
9610 417a6e49 2020-09-26 stsp #ifndef PROFILE
9611 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9612 417a6e49 2020-09-26 stsp NULL) == -1)
9613 417a6e49 2020-09-26 stsp err(1, "pledge");
9614 417a6e49 2020-09-26 stsp #endif
9615 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9616 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9617 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9618 417a6e49 2020-09-26 stsp goto done;
9619 417a6e49 2020-09-26 stsp }
9620 417a6e49 2020-09-26 stsp
9621 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9622 417a6e49 2020-09-26 stsp if (error) {
9623 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9624 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
9625 417a6e49 2020-09-26 stsp goto done;
9626 417a6e49 2020-09-26 stsp }
9627 417a6e49 2020-09-26 stsp
9628 417a6e49 2020-09-26 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9629 417a6e49 2020-09-26 stsp if (error)
9630 417a6e49 2020-09-26 stsp goto done;
9631 417a6e49 2020-09-26 stsp
9632 417a6e49 2020-09-26 stsp if (argc >= 1) {
9633 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9634 417a6e49 2020-09-26 stsp worktree);
9635 417a6e49 2020-09-26 stsp if (error)
9636 417a6e49 2020-09-26 stsp goto done;
9637 417a6e49 2020-09-26 stsp show_files = 1;
9638 417a6e49 2020-09-26 stsp }
9639 417a6e49 2020-09-26 stsp
9640 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
9641 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
9642 417a6e49 2020-09-26 stsp if (error)
9643 417a6e49 2020-09-26 stsp goto done;
9644 417a6e49 2020-09-26 stsp
9645 417a6e49 2020-09-26 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9646 417a6e49 2020-09-26 stsp if (error)
9647 417a6e49 2020-09-26 stsp goto done;
9648 417a6e49 2020-09-26 stsp
9649 417a6e49 2020-09-26 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9650 417a6e49 2020-09-26 stsp printf("work tree base commit: %s\n", id_str);
9651 417a6e49 2020-09-26 stsp printf("work tree path prefix: %s\n",
9652 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree));
9653 417a6e49 2020-09-26 stsp printf("work tree branch reference: %s\n",
9654 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
9655 417a6e49 2020-09-26 stsp printf("work tree UUID: %s\n", uuidstr);
9656 417a6e49 2020-09-26 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9657 417a6e49 2020-09-26 stsp
9658 417a6e49 2020-09-26 stsp if (show_files) {
9659 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9660 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9661 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
9662 417a6e49 2020-09-26 stsp continue;
9663 417a6e49 2020-09-26 stsp /*
9664 417a6e49 2020-09-26 stsp * Assume this path will fail. This will be corrected
9665 417a6e49 2020-09-26 stsp * in print_path_info() in case the path does suceeed.
9666 417a6e49 2020-09-26 stsp */
9667 417a6e49 2020-09-26 stsp pe->data = (void *)got_error_path(pe->path,
9668 417a6e49 2020-09-26 stsp GOT_ERR_BAD_PATH);
9669 417a6e49 2020-09-26 stsp }
9670 417a6e49 2020-09-26 stsp error = got_worktree_path_info(worktree, &paths,
9671 417a6e49 2020-09-26 stsp print_path_info, &paths, check_cancelled, NULL);
9672 417a6e49 2020-09-26 stsp if (error)
9673 417a6e49 2020-09-26 stsp goto done;
9674 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9675 417a6e49 2020-09-26 stsp if (pe->data != NULL) {
9676 417a6e49 2020-09-26 stsp error = pe->data; /* bad path */
9677 417a6e49 2020-09-26 stsp break;
9678 417a6e49 2020-09-26 stsp }
9679 417a6e49 2020-09-26 stsp }
9680 417a6e49 2020-09-26 stsp }
9681 417a6e49 2020-09-26 stsp done:
9682 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9683 417a6e49 2020-09-26 stsp free((char *)pe->path);
9684 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9685 417a6e49 2020-09-26 stsp free(cwd);
9686 417a6e49 2020-09-26 stsp free(id_str);
9687 417a6e49 2020-09-26 stsp free(uuidstr);
9688 417a6e49 2020-09-26 stsp return error;
9689 417a6e49 2020-09-26 stsp }