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;
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 if (asprintf(&gotconfig,
1391 417a6e49 2020-09-26 stsp "remote \"%s\" {\n"
1392 417a6e49 2020-09-26 stsp "\tserver %s\n"
1393 417a6e49 2020-09-26 stsp "\tprotocol %s\n"
1394 417a6e49 2020-09-26 stsp "%s%s%s"
1395 417a6e49 2020-09-26 stsp "\trepository \"%s\"\n"
1396 417a6e49 2020-09-26 stsp "%s"
1397 417a6e49 2020-09-26 stsp "}\n",
1398 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1399 417a6e49 2020-09-26 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1400 417a6e49 2020-09-26 stsp server_path,
1401 417a6e49 2020-09-26 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1402 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1403 417a6e49 2020-09-26 stsp goto done;
1404 417a6e49 2020-09-26 stsp }
1405 417a6e49 2020-09-26 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1406 417a6e49 2020-09-26 stsp if (n != strlen(gotconfig)) {
1407 417a6e49 2020-09-26 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1408 417a6e49 2020-09-26 stsp goto done;
1409 417a6e49 2020-09-26 stsp }
1410 417a6e49 2020-09-26 stsp
1411 417a6e49 2020-09-26 stsp /* Create a config file Git can understand. */
1412 417a6e49 2020-09-26 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1413 417a6e49 2020-09-26 stsp if (gitconfig_path == NULL) {
1414 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1415 417a6e49 2020-09-26 stsp goto done;
1416 417a6e49 2020-09-26 stsp }
1417 417a6e49 2020-09-26 stsp gitconfig_file = fopen(gitconfig_path, "a");
1418 417a6e49 2020-09-26 stsp if (gitconfig_file == NULL) {
1419 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1420 417a6e49 2020-09-26 stsp goto done;
1421 417a6e49 2020-09-26 stsp }
1422 417a6e49 2020-09-26 stsp if (mirror_references) {
1423 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1424 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1425 417a6e49 2020-09-26 stsp "\turl = %s\n"
1426 417a6e49 2020-09-26 stsp "\tfetch = +refs/*:refs/*\n"
1427 417a6e49 2020-09-26 stsp "\tmirror = true\n",
1428 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1429 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1430 417a6e49 2020-09-26 stsp goto done;
1431 417a6e49 2020-09-26 stsp }
1432 417a6e49 2020-09-26 stsp } else if (fetch_all_branches) {
1433 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1434 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1435 417a6e49 2020-09-26 stsp "\turl = %s\n"
1436 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1437 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1438 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1439 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1440 417a6e49 2020-09-26 stsp goto done;
1441 417a6e49 2020-09-26 stsp }
1442 417a6e49 2020-09-26 stsp } else {
1443 417a6e49 2020-09-26 stsp const char *branchname;
1444 417a6e49 2020-09-26 stsp
1445 417a6e49 2020-09-26 stsp /*
1446 417a6e49 2020-09-26 stsp * If the server specified a default branch, use just that one.
1447 417a6e49 2020-09-26 stsp * Otherwise fall back to fetching all branches on next fetch.
1448 417a6e49 2020-09-26 stsp */
1449 417a6e49 2020-09-26 stsp if (head_symref) {
1450 417a6e49 2020-09-26 stsp branchname = got_ref_get_symref_target(head_symref);
1451 417a6e49 2020-09-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1452 417a6e49 2020-09-26 stsp branchname += 11;
1453 417a6e49 2020-09-26 stsp } else
1454 417a6e49 2020-09-26 stsp branchname = "*"; /* fall back to all branches */
1455 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1456 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1457 417a6e49 2020-09-26 stsp "\turl = %s\n"
1458 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1459 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1460 417a6e49 2020-09-26 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1461 417a6e49 2020-09-26 stsp branchname) == -1) {
1462 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1463 417a6e49 2020-09-26 stsp goto done;
1464 417a6e49 2020-09-26 stsp }
1465 417a6e49 2020-09-26 stsp }
1466 417a6e49 2020-09-26 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1467 417a6e49 2020-09-26 stsp if (n != strlen(gitconfig)) {
1468 417a6e49 2020-09-26 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1469 417a6e49 2020-09-26 stsp goto done;
1470 417a6e49 2020-09-26 stsp }
1471 417a6e49 2020-09-26 stsp
1472 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1473 417a6e49 2020-09-26 stsp printf("Created %s repository '%s'\n",
1474 417a6e49 2020-09-26 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1475 417a6e49 2020-09-26 stsp done:
1476 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
1477 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
1478 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
1479 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1480 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
1481 417a6e49 2020-09-26 stsp }
1482 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1483 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
1484 417a6e49 2020-09-26 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1485 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1486 417a6e49 2020-09-26 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1487 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1488 417a6e49 2020-09-26 stsp if (repo)
1489 417a6e49 2020-09-26 stsp got_repo_close(repo);
1490 417a6e49 2020-09-26 stsp if (head_symref)
1491 417a6e49 2020-09-26 stsp got_ref_close(head_symref);
1492 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1493 417a6e49 2020-09-26 stsp free((void *)pe->path);
1494 417a6e49 2020-09-26 stsp free(pe->data);
1495 417a6e49 2020-09-26 stsp }
1496 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
1497 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1498 417a6e49 2020-09-26 stsp free((void *)pe->path);
1499 417a6e49 2020-09-26 stsp free(pe->data);
1500 417a6e49 2020-09-26 stsp }
1501 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
1502 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
1503 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
1504 417a6e49 2020-09-26 stsp free(pack_hash);
1505 417a6e49 2020-09-26 stsp free(proto);
1506 417a6e49 2020-09-26 stsp free(host);
1507 417a6e49 2020-09-26 stsp free(port);
1508 417a6e49 2020-09-26 stsp free(server_path);
1509 417a6e49 2020-09-26 stsp free(repo_name);
1510 417a6e49 2020-09-26 stsp free(default_destdir);
1511 417a6e49 2020-09-26 stsp free(gotconfig);
1512 417a6e49 2020-09-26 stsp free(gitconfig);
1513 417a6e49 2020-09-26 stsp free(gotconfig_path);
1514 417a6e49 2020-09-26 stsp free(gitconfig_path);
1515 417a6e49 2020-09-26 stsp free(git_url);
1516 417a6e49 2020-09-26 stsp return error;
1517 417a6e49 2020-09-26 stsp }
1518 417a6e49 2020-09-26 stsp
1519 417a6e49 2020-09-26 stsp static const struct got_error *
1520 417a6e49 2020-09-26 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1521 417a6e49 2020-09-26 stsp int replace_tags, int verbosity, struct got_repository *repo)
1522 417a6e49 2020-09-26 stsp {
1523 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1524 417a6e49 2020-09-26 stsp char *new_id_str = NULL;
1525 417a6e49 2020-09-26 stsp struct got_object_id *old_id = NULL;
1526 417a6e49 2020-09-26 stsp
1527 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
1528 417a6e49 2020-09-26 stsp if (err)
1529 417a6e49 2020-09-26 stsp goto done;
1530 417a6e49 2020-09-26 stsp
1531 417a6e49 2020-09-26 stsp if (!replace_tags &&
1532 417a6e49 2020-09-26 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1533 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1534 417a6e49 2020-09-26 stsp if (err)
1535 417a6e49 2020-09-26 stsp goto done;
1536 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1537 417a6e49 2020-09-26 stsp goto done;
1538 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1539 417a6e49 2020-09-26 stsp printf("Rejecting update of existing tag %s: %s\n",
1540 417a6e49 2020-09-26 stsp got_ref_get_name(ref), new_id_str);
1541 417a6e49 2020-09-26 stsp }
1542 417a6e49 2020-09-26 stsp goto done;
1543 417a6e49 2020-09-26 stsp }
1544 417a6e49 2020-09-26 stsp
1545 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1546 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1547 417a6e49 2020-09-26 stsp printf("Replacing reference %s: %s\n",
1548 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1549 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1550 417a6e49 2020-09-26 stsp }
1551 417a6e49 2020-09-26 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1552 417a6e49 2020-09-26 stsp if (err)
1553 417a6e49 2020-09-26 stsp goto done;
1554 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1555 417a6e49 2020-09-26 stsp if (err)
1556 417a6e49 2020-09-26 stsp goto done;
1557 417a6e49 2020-09-26 stsp } else {
1558 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1559 417a6e49 2020-09-26 stsp if (err)
1560 417a6e49 2020-09-26 stsp goto done;
1561 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1562 417a6e49 2020-09-26 stsp goto done;
1563 417a6e49 2020-09-26 stsp
1564 417a6e49 2020-09-26 stsp err = got_ref_change_ref(ref, new_id);
1565 417a6e49 2020-09-26 stsp if (err)
1566 417a6e49 2020-09-26 stsp goto done;
1567 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1568 417a6e49 2020-09-26 stsp if (err)
1569 417a6e49 2020-09-26 stsp goto done;
1570 417a6e49 2020-09-26 stsp }
1571 417a6e49 2020-09-26 stsp
1572 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1573 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1574 417a6e49 2020-09-26 stsp new_id_str);
1575 417a6e49 2020-09-26 stsp done:
1576 417a6e49 2020-09-26 stsp free(old_id);
1577 417a6e49 2020-09-26 stsp free(new_id_str);
1578 417a6e49 2020-09-26 stsp return err;
1579 417a6e49 2020-09-26 stsp }
1580 417a6e49 2020-09-26 stsp
1581 417a6e49 2020-09-26 stsp static const struct got_error *
1582 417a6e49 2020-09-26 stsp update_symref(const char *refname, struct got_reference *target_ref,
1583 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1584 417a6e49 2020-09-26 stsp {
1585 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1586 417a6e49 2020-09-26 stsp struct got_reference *symref;
1587 417a6e49 2020-09-26 stsp int symref_is_locked = 0;
1588 417a6e49 2020-09-26 stsp
1589 417a6e49 2020-09-26 stsp err = got_ref_open(&symref, repo, refname, 1);
1590 417a6e49 2020-09-26 stsp if (err) {
1591 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1592 417a6e49 2020-09-26 stsp return err;
1593 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1594 417a6e49 2020-09-26 stsp if (err)
1595 417a6e49 2020-09-26 stsp goto done;
1596 417a6e49 2020-09-26 stsp
1597 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1598 417a6e49 2020-09-26 stsp if (err)
1599 417a6e49 2020-09-26 stsp goto done;
1600 417a6e49 2020-09-26 stsp
1601 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1602 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n",
1603 417a6e49 2020-09-26 stsp got_ref_get_name(symref),
1604 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1605 417a6e49 2020-09-26 stsp } else {
1606 417a6e49 2020-09-26 stsp symref_is_locked = 1;
1607 417a6e49 2020-09-26 stsp
1608 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1609 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref)) == 0)
1610 417a6e49 2020-09-26 stsp goto done;
1611 417a6e49 2020-09-26 stsp
1612 417a6e49 2020-09-26 stsp err = got_ref_change_symref(symref,
1613 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref));
1614 417a6e49 2020-09-26 stsp if (err)
1615 417a6e49 2020-09-26 stsp goto done;
1616 417a6e49 2020-09-26 stsp
1617 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1618 417a6e49 2020-09-26 stsp if (err)
1619 417a6e49 2020-09-26 stsp goto done;
1620 417a6e49 2020-09-26 stsp
1621 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1622 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1623 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1624 417a6e49 2020-09-26 stsp
1625 417a6e49 2020-09-26 stsp }
1626 417a6e49 2020-09-26 stsp done:
1627 417a6e49 2020-09-26 stsp if (symref_is_locked) {
1628 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(symref);
1629 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1630 417a6e49 2020-09-26 stsp err = unlock_err;
1631 417a6e49 2020-09-26 stsp }
1632 417a6e49 2020-09-26 stsp got_ref_close(symref);
1633 417a6e49 2020-09-26 stsp return err;
1634 417a6e49 2020-09-26 stsp }
1635 417a6e49 2020-09-26 stsp
1636 417a6e49 2020-09-26 stsp __dead static void
1637 417a6e49 2020-09-26 stsp usage_fetch(void)
1638 417a6e49 2020-09-26 stsp {
1639 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1640 417a6e49 2020-09-26 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1641 417a6e49 2020-09-26 stsp "[remote-repository-name]\n",
1642 417a6e49 2020-09-26 stsp getprogname());
1643 417a6e49 2020-09-26 stsp exit(1);
1644 417a6e49 2020-09-26 stsp }
1645 417a6e49 2020-09-26 stsp
1646 417a6e49 2020-09-26 stsp static const struct got_error *
1647 417a6e49 2020-09-26 stsp delete_missing_ref(struct got_reference *ref,
1648 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1649 417a6e49 2020-09-26 stsp {
1650 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1651 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
1652 417a6e49 2020-09-26 stsp char *id_str = NULL;
1653 417a6e49 2020-09-26 stsp
1654 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1655 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1656 417a6e49 2020-09-26 stsp if (err)
1657 417a6e49 2020-09-26 stsp return err;
1658 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1659 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1660 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1661 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1662 417a6e49 2020-09-26 stsp }
1663 417a6e49 2020-09-26 stsp } else {
1664 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
1665 417a6e49 2020-09-26 stsp if (err)
1666 417a6e49 2020-09-26 stsp return err;
1667 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
1668 417a6e49 2020-09-26 stsp if (err)
1669 417a6e49 2020-09-26 stsp goto done;
1670 417a6e49 2020-09-26 stsp
1671 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1672 417a6e49 2020-09-26 stsp if (err)
1673 417a6e49 2020-09-26 stsp goto done;
1674 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1675 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1676 417a6e49 2020-09-26 stsp got_ref_get_name(ref), id_str);
1677 417a6e49 2020-09-26 stsp }
1678 417a6e49 2020-09-26 stsp }
1679 417a6e49 2020-09-26 stsp done:
1680 417a6e49 2020-09-26 stsp free(id);
1681 417a6e49 2020-09-26 stsp free(id_str);
1682 417a6e49 2020-09-26 stsp return NULL;
1683 417a6e49 2020-09-26 stsp }
1684 417a6e49 2020-09-26 stsp
1685 417a6e49 2020-09-26 stsp static const struct got_error *
1686 417a6e49 2020-09-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1687 417a6e49 2020-09-26 stsp struct got_pathlist_head *their_symrefs,
1688 417a6e49 2020-09-26 stsp const struct got_remote_repo *remote,
1689 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1690 417a6e49 2020-09-26 stsp {
1691 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1692 417a6e49 2020-09-26 stsp struct got_reflist_head my_refs;
1693 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
1694 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1695 417a6e49 2020-09-26 stsp char *remote_namespace = NULL;
1696 417a6e49 2020-09-26 stsp char *local_refname = NULL;
1697 417a6e49 2020-09-26 stsp
1698 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&my_refs);
1699 417a6e49 2020-09-26 stsp
1700 417a6e49 2020-09-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1701 417a6e49 2020-09-26 stsp == -1)
1702 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1703 417a6e49 2020-09-26 stsp
1704 417a6e49 2020-09-26 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1705 417a6e49 2020-09-26 stsp if (err)
1706 417a6e49 2020-09-26 stsp goto done;
1707 417a6e49 2020-09-26 stsp
1708 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1709 417a6e49 2020-09-26 stsp const char *refname = got_ref_get_name(re->ref);
1710 417a6e49 2020-09-26 stsp
1711 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
1712 417a6e49 2020-09-26 stsp if (strncmp(refname, remote_namespace,
1713 417a6e49 2020-09-26 stsp strlen(remote_namespace)) == 0) {
1714 417a6e49 2020-09-26 stsp if (strcmp(refname + strlen(remote_namespace),
1715 417a6e49 2020-09-26 stsp GOT_REF_HEAD) == 0)
1716 417a6e49 2020-09-26 stsp continue;
1717 417a6e49 2020-09-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1718 417a6e49 2020-09-26 stsp refname + strlen(remote_namespace)) == -1) {
1719 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
1720 417a6e49 2020-09-26 stsp goto done;
1721 417a6e49 2020-09-26 stsp }
1722 417a6e49 2020-09-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1723 417a6e49 2020-09-26 stsp continue;
1724 417a6e49 2020-09-26 stsp }
1725 417a6e49 2020-09-26 stsp
1726 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1727 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1728 417a6e49 2020-09-26 stsp break;
1729 417a6e49 2020-09-26 stsp }
1730 417a6e49 2020-09-26 stsp if (pe != NULL)
1731 417a6e49 2020-09-26 stsp continue;
1732 417a6e49 2020-09-26 stsp
1733 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1734 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1735 417a6e49 2020-09-26 stsp break;
1736 417a6e49 2020-09-26 stsp }
1737 417a6e49 2020-09-26 stsp if (pe != NULL)
1738 417a6e49 2020-09-26 stsp continue;
1739 417a6e49 2020-09-26 stsp
1740 417a6e49 2020-09-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1741 417a6e49 2020-09-26 stsp if (err)
1742 417a6e49 2020-09-26 stsp break;
1743 417a6e49 2020-09-26 stsp
1744 417a6e49 2020-09-26 stsp if (local_refname) {
1745 417a6e49 2020-09-26 stsp struct got_reference *ref;
1746 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1747 417a6e49 2020-09-26 stsp if (err) {
1748 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1749 417a6e49 2020-09-26 stsp break;
1750 417a6e49 2020-09-26 stsp free(local_refname);
1751 417a6e49 2020-09-26 stsp local_refname = NULL;
1752 417a6e49 2020-09-26 stsp continue;
1753 417a6e49 2020-09-26 stsp }
1754 417a6e49 2020-09-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1755 417a6e49 2020-09-26 stsp if (err)
1756 417a6e49 2020-09-26 stsp break;
1757 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1758 417a6e49 2020-09-26 stsp got_ref_close(ref);
1759 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL) {
1760 417a6e49 2020-09-26 stsp err = unlock_err;
1761 417a6e49 2020-09-26 stsp break;
1762 417a6e49 2020-09-26 stsp }
1763 417a6e49 2020-09-26 stsp
1764 417a6e49 2020-09-26 stsp free(local_refname);
1765 417a6e49 2020-09-26 stsp local_refname = NULL;
1766 417a6e49 2020-09-26 stsp }
1767 417a6e49 2020-09-26 stsp }
1768 417a6e49 2020-09-26 stsp done:
1769 417a6e49 2020-09-26 stsp free(remote_namespace);
1770 417a6e49 2020-09-26 stsp free(local_refname);
1771 417a6e49 2020-09-26 stsp return err;
1772 417a6e49 2020-09-26 stsp }
1773 417a6e49 2020-09-26 stsp
1774 417a6e49 2020-09-26 stsp static const struct got_error *
1775 417a6e49 2020-09-26 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1776 417a6e49 2020-09-26 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1777 417a6e49 2020-09-26 stsp {
1778 417a6e49 2020-09-26 stsp const struct got_error *err, *unlock_err;
1779 417a6e49 2020-09-26 stsp char *remote_refname;
1780 417a6e49 2020-09-26 stsp struct got_reference *ref;
1781 417a6e49 2020-09-26 stsp
1782 417a6e49 2020-09-26 stsp if (strncmp("refs/", refname, 5) == 0)
1783 417a6e49 2020-09-26 stsp refname += 5;
1784 417a6e49 2020-09-26 stsp
1785 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1786 417a6e49 2020-09-26 stsp remote_repo_name, refname) == -1)
1787 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1788 417a6e49 2020-09-26 stsp
1789 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1790 417a6e49 2020-09-26 stsp if (err) {
1791 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1792 417a6e49 2020-09-26 stsp goto done;
1793 417a6e49 2020-09-26 stsp err = create_ref(remote_refname, id, verbosity, repo);
1794 417a6e49 2020-09-26 stsp } else {
1795 417a6e49 2020-09-26 stsp err = update_ref(ref, id, 0, verbosity, repo);
1796 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1797 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1798 417a6e49 2020-09-26 stsp err = unlock_err;
1799 417a6e49 2020-09-26 stsp got_ref_close(ref);
1800 417a6e49 2020-09-26 stsp }
1801 417a6e49 2020-09-26 stsp done:
1802 417a6e49 2020-09-26 stsp free(remote_refname);
1803 417a6e49 2020-09-26 stsp return err;
1804 417a6e49 2020-09-26 stsp }
1805 417a6e49 2020-09-26 stsp
1806 417a6e49 2020-09-26 stsp static const struct got_error *
1807 417a6e49 2020-09-26 stsp cmd_fetch(int argc, char *argv[])
1808 417a6e49 2020-09-26 stsp {
1809 417a6e49 2020-09-26 stsp const struct got_error *error = NULL, *unlock_err;
1810 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
1811 417a6e49 2020-09-26 stsp const char *remote_name;
1812 417a6e49 2020-09-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
1813 417a6e49 2020-09-26 stsp char *repo_name = NULL, *server_path = NULL;
1814 417a6e49 2020-09-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
1815 417a6e49 2020-09-26 stsp int nremotes;
1816 417a6e49 2020-09-26 stsp char *id_str = NULL;
1817 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
1818 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
1819 417a6e49 2020-09-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1820 417a6e49 2020-09-26 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1821 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1822 417a6e49 2020-09-26 stsp struct got_object_id *pack_hash = NULL;
1823 417a6e49 2020-09-26 stsp int i, ch, fetchfd = -1, fetchstatus;
1824 417a6e49 2020-09-26 stsp pid_t fetchpid = -1;
1825 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg fpa;
1826 417a6e49 2020-09-26 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1827 417a6e49 2020-09-26 stsp int delete_refs = 0, replace_tags = 0;
1828 417a6e49 2020-09-26 stsp
1829 417a6e49 2020-09-26 stsp TAILQ_INIT(&refs);
1830 417a6e49 2020-09-26 stsp TAILQ_INIT(&symrefs);
1831 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_branches);
1832 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_refs);
1833 417a6e49 2020-09-26 stsp
1834 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1835 417a6e49 2020-09-26 stsp switch (ch) {
1836 417a6e49 2020-09-26 stsp case 'a':
1837 417a6e49 2020-09-26 stsp fetch_all_branches = 1;
1838 417a6e49 2020-09-26 stsp break;
1839 417a6e49 2020-09-26 stsp case 'b':
1840 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_branches,
1841 417a6e49 2020-09-26 stsp optarg, NULL);
1842 417a6e49 2020-09-26 stsp if (error)
1843 417a6e49 2020-09-26 stsp return error;
1844 417a6e49 2020-09-26 stsp break;
1845 417a6e49 2020-09-26 stsp case 'd':
1846 417a6e49 2020-09-26 stsp delete_refs = 1;
1847 417a6e49 2020-09-26 stsp break;
1848 417a6e49 2020-09-26 stsp case 'l':
1849 417a6e49 2020-09-26 stsp list_refs_only = 1;
1850 417a6e49 2020-09-26 stsp break;
1851 417a6e49 2020-09-26 stsp case 'r':
1852 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
1853 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1854 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
1855 417a6e49 2020-09-26 stsp optarg);
1856 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
1857 417a6e49 2020-09-26 stsp break;
1858 417a6e49 2020-09-26 stsp case 't':
1859 417a6e49 2020-09-26 stsp replace_tags = 1;
1860 417a6e49 2020-09-26 stsp break;
1861 417a6e49 2020-09-26 stsp case 'v':
1862 417a6e49 2020-09-26 stsp if (verbosity < 0)
1863 417a6e49 2020-09-26 stsp verbosity = 0;
1864 417a6e49 2020-09-26 stsp else if (verbosity < 3)
1865 417a6e49 2020-09-26 stsp verbosity++;
1866 417a6e49 2020-09-26 stsp break;
1867 417a6e49 2020-09-26 stsp case 'q':
1868 417a6e49 2020-09-26 stsp verbosity = -1;
1869 417a6e49 2020-09-26 stsp break;
1870 417a6e49 2020-09-26 stsp case 'R':
1871 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_refs,
1872 417a6e49 2020-09-26 stsp optarg, NULL);
1873 417a6e49 2020-09-26 stsp if (error)
1874 417a6e49 2020-09-26 stsp return error;
1875 417a6e49 2020-09-26 stsp break;
1876 417a6e49 2020-09-26 stsp default:
1877 417a6e49 2020-09-26 stsp usage_fetch();
1878 417a6e49 2020-09-26 stsp break;
1879 417a6e49 2020-09-26 stsp }
1880 417a6e49 2020-09-26 stsp }
1881 417a6e49 2020-09-26 stsp argc -= optind;
1882 417a6e49 2020-09-26 stsp argv += optind;
1883 417a6e49 2020-09-26 stsp
1884 417a6e49 2020-09-26 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1885 417a6e49 2020-09-26 stsp errx(1, "-a and -b options are mutually exclusive");
1886 417a6e49 2020-09-26 stsp if (list_refs_only) {
1887 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_branches))
1888 417a6e49 2020-09-26 stsp errx(1, "-l and -b options are mutually exclusive");
1889 417a6e49 2020-09-26 stsp if (fetch_all_branches)
1890 417a6e49 2020-09-26 stsp errx(1, "-l and -a options are mutually exclusive");
1891 417a6e49 2020-09-26 stsp if (delete_refs)
1892 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
1893 417a6e49 2020-09-26 stsp if (verbosity == -1)
1894 417a6e49 2020-09-26 stsp errx(1, "-l and -q options are mutually exclusive");
1895 417a6e49 2020-09-26 stsp }
1896 417a6e49 2020-09-26 stsp
1897 417a6e49 2020-09-26 stsp if (argc == 0)
1898 417a6e49 2020-09-26 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1899 417a6e49 2020-09-26 stsp else if (argc == 1)
1900 417a6e49 2020-09-26 stsp remote_name = argv[0];
1901 417a6e49 2020-09-26 stsp else
1902 417a6e49 2020-09-26 stsp usage_fetch();
1903 417a6e49 2020-09-26 stsp
1904 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
1905 417a6e49 2020-09-26 stsp if (cwd == NULL) {
1906 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
1907 417a6e49 2020-09-26 stsp goto done;
1908 417a6e49 2020-09-26 stsp }
1909 417a6e49 2020-09-26 stsp
1910 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1911 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
1912 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 417a6e49 2020-09-26 stsp goto done;
1914 417a6e49 2020-09-26 stsp else
1915 417a6e49 2020-09-26 stsp error = NULL;
1916 417a6e49 2020-09-26 stsp if (worktree) {
1917 417a6e49 2020-09-26 stsp repo_path =
1918 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
1919 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1920 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1921 417a6e49 2020-09-26 stsp if (error)
1922 417a6e49 2020-09-26 stsp goto done;
1923 417a6e49 2020-09-26 stsp } else {
1924 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
1925 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1926 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1927 417a6e49 2020-09-26 stsp goto done;
1928 417a6e49 2020-09-26 stsp }
1929 417a6e49 2020-09-26 stsp }
1930 417a6e49 2020-09-26 stsp }
1931 417a6e49 2020-09-26 stsp
1932 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
1933 417a6e49 2020-09-26 stsp if (error)
1934 417a6e49 2020-09-26 stsp goto done;
1935 417a6e49 2020-09-26 stsp
1936 417a6e49 2020-09-26 stsp if (worktree) {
1937 417a6e49 2020-09-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1938 417a6e49 2020-09-26 stsp if (worktree_conf) {
1939 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1940 417a6e49 2020-09-26 stsp worktree_conf);
1941 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1942 417a6e49 2020-09-26 stsp remote = &remotes[i];
1943 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1944 417a6e49 2020-09-26 stsp break;
1945 417a6e49 2020-09-26 stsp }
1946 417a6e49 2020-09-26 stsp }
1947 417a6e49 2020-09-26 stsp }
1948 417a6e49 2020-09-26 stsp if (remote == NULL) {
1949 417a6e49 2020-09-26 stsp repo_conf = got_repo_get_gotconfig(repo);
1950 417a6e49 2020-09-26 stsp if (repo_conf) {
1951 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1952 417a6e49 2020-09-26 stsp repo_conf);
1953 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1954 417a6e49 2020-09-26 stsp remote = &remotes[i];
1955 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1956 417a6e49 2020-09-26 stsp break;
1957 417a6e49 2020-09-26 stsp }
1958 417a6e49 2020-09-26 stsp }
1959 417a6e49 2020-09-26 stsp }
1960 417a6e49 2020-09-26 stsp if (remote == NULL) {
1961 417a6e49 2020-09-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1962 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1963 417a6e49 2020-09-26 stsp remote = &remotes[i];
1964 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1965 417a6e49 2020-09-26 stsp break;
1966 417a6e49 2020-09-26 stsp }
1967 417a6e49 2020-09-26 stsp }
1968 417a6e49 2020-09-26 stsp if (remote == NULL) {
1969 417a6e49 2020-09-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1970 417a6e49 2020-09-26 stsp goto done;
1971 417a6e49 2020-09-26 stsp }
1972 417a6e49 2020-09-26 stsp
1973 417a6e49 2020-09-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1974 417a6e49 2020-09-26 stsp &repo_name, remote->url);
1975 417a6e49 2020-09-26 stsp if (error)
1976 417a6e49 2020-09-26 stsp goto done;
1977 417a6e49 2020-09-26 stsp
1978 417a6e49 2020-09-26 stsp if (strcmp(proto, "git") == 0) {
1979 417a6e49 2020-09-26 stsp #ifndef PROFILE
1980 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1981 417a6e49 2020-09-26 stsp "sendfd dns inet unveil", NULL) == -1)
1982 417a6e49 2020-09-26 stsp err(1, "pledge");
1983 417a6e49 2020-09-26 stsp #endif
1984 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1985 417a6e49 2020-09-26 stsp strcmp(proto, "ssh") == 0) {
1986 417a6e49 2020-09-26 stsp #ifndef PROFILE
1987 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1988 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
1989 417a6e49 2020-09-26 stsp err(1, "pledge");
1990 417a6e49 2020-09-26 stsp #endif
1991 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "http") == 0 ||
1992 417a6e49 2020-09-26 stsp strcmp(proto, "git+http") == 0) {
1993 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1994 417a6e49 2020-09-26 stsp goto done;
1995 417a6e49 2020-09-26 stsp } else {
1996 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1997 417a6e49 2020-09-26 stsp goto done;
1998 417a6e49 2020-09-26 stsp }
1999 417a6e49 2020-09-26 stsp
2000 417a6e49 2020-09-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2001 417a6e49 2020-09-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2002 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil",
2003 417a6e49 2020-09-26 stsp GOT_FETCH_PATH_SSH);
2004 417a6e49 2020-09-26 stsp goto done;
2005 417a6e49 2020-09-26 stsp }
2006 417a6e49 2020-09-26 stsp }
2007 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2008 417a6e49 2020-09-26 stsp if (error)
2009 417a6e49 2020-09-26 stsp goto done;
2010 417a6e49 2020-09-26 stsp
2011 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2012 417a6e49 2020-09-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2013 417a6e49 2020-09-26 stsp port ? ":" : "", port ? port : "");
2014 417a6e49 2020-09-26 stsp
2015 417a6e49 2020-09-26 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2016 417a6e49 2020-09-26 stsp server_path, verbosity);
2017 417a6e49 2020-09-26 stsp if (error)
2018 417a6e49 2020-09-26 stsp goto done;
2019 417a6e49 2020-09-26 stsp
2020 417a6e49 2020-09-26 stsp fpa.last_scaled_size[0] = '\0';
2021 417a6e49 2020-09-26 stsp fpa.last_p_indexed = -1;
2022 417a6e49 2020-09-26 stsp fpa.last_p_resolved = -1;
2023 417a6e49 2020-09-26 stsp fpa.verbosity = verbosity;
2024 417a6e49 2020-09-26 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2025 417a6e49 2020-09-26 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2026 417a6e49 2020-09-26 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2027 417a6e49 2020-09-26 stsp fetch_progress, &fpa);
2028 417a6e49 2020-09-26 stsp if (error)
2029 417a6e49 2020-09-26 stsp goto done;
2030 417a6e49 2020-09-26 stsp
2031 417a6e49 2020-09-26 stsp if (list_refs_only) {
2032 417a6e49 2020-09-26 stsp error = list_remote_refs(&symrefs, &refs);
2033 417a6e49 2020-09-26 stsp goto done;
2034 417a6e49 2020-09-26 stsp }
2035 417a6e49 2020-09-26 stsp
2036 417a6e49 2020-09-26 stsp if (pack_hash == NULL) {
2037 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2038 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2039 417a6e49 2020-09-26 stsp } else if (verbosity >= 0) {
2040 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, pack_hash);
2041 417a6e49 2020-09-26 stsp if (error)
2042 417a6e49 2020-09-26 stsp goto done;
2043 417a6e49 2020-09-26 stsp printf("\nFetched %s.pack\n", id_str);
2044 417a6e49 2020-09-26 stsp free(id_str);
2045 417a6e49 2020-09-26 stsp id_str = NULL;
2046 417a6e49 2020-09-26 stsp }
2047 417a6e49 2020-09-26 stsp
2048 417a6e49 2020-09-26 stsp /* Update references provided with the pack file. */
2049 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2050 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2051 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
2052 417a6e49 2020-09-26 stsp struct got_reference *ref;
2053 417a6e49 2020-09-26 stsp char *remote_refname;
2054 417a6e49 2020-09-26 stsp
2055 417a6e49 2020-09-26 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2056 417a6e49 2020-09-26 stsp !remote->mirror_references) {
2057 417a6e49 2020-09-26 stsp error = update_wanted_ref(refname, id,
2058 417a6e49 2020-09-26 stsp remote->name, verbosity, repo);
2059 417a6e49 2020-09-26 stsp if (error)
2060 417a6e49 2020-09-26 stsp goto done;
2061 417a6e49 2020-09-26 stsp continue;
2062 417a6e49 2020-09-26 stsp }
2063 417a6e49 2020-09-26 stsp
2064 417a6e49 2020-09-26 stsp if (remote->mirror_references ||
2065 417a6e49 2020-09-26 stsp strncmp("refs/tags/", refname, 10) == 0) {
2066 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2067 417a6e49 2020-09-26 stsp if (error) {
2068 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2069 417a6e49 2020-09-26 stsp goto done;
2070 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2071 417a6e49 2020-09-26 stsp repo);
2072 417a6e49 2020-09-26 stsp if (error)
2073 417a6e49 2020-09-26 stsp goto done;
2074 417a6e49 2020-09-26 stsp } else {
2075 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2076 417a6e49 2020-09-26 stsp verbosity, repo);
2077 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2078 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2079 417a6e49 2020-09-26 stsp error = unlock_err;
2080 417a6e49 2020-09-26 stsp got_ref_close(ref);
2081 417a6e49 2020-09-26 stsp if (error)
2082 417a6e49 2020-09-26 stsp goto done;
2083 417a6e49 2020-09-26 stsp }
2084 417a6e49 2020-09-26 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2085 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2086 417a6e49 2020-09-26 stsp remote_name, refname + 11) == -1) {
2087 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2088 417a6e49 2020-09-26 stsp goto done;
2089 417a6e49 2020-09-26 stsp }
2090 417a6e49 2020-09-26 stsp
2091 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2092 417a6e49 2020-09-26 stsp if (error) {
2093 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2094 417a6e49 2020-09-26 stsp goto done;
2095 417a6e49 2020-09-26 stsp error = create_ref(remote_refname, id,
2096 417a6e49 2020-09-26 stsp verbosity, repo);
2097 417a6e49 2020-09-26 stsp if (error)
2098 417a6e49 2020-09-26 stsp goto done;
2099 417a6e49 2020-09-26 stsp } else {
2100 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2101 417a6e49 2020-09-26 stsp verbosity, repo);
2102 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2103 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2104 417a6e49 2020-09-26 stsp error = unlock_err;
2105 417a6e49 2020-09-26 stsp got_ref_close(ref);
2106 417a6e49 2020-09-26 stsp if (error)
2107 417a6e49 2020-09-26 stsp goto done;
2108 417a6e49 2020-09-26 stsp }
2109 417a6e49 2020-09-26 stsp
2110 417a6e49 2020-09-26 stsp /* Also create a local branch if none exists yet. */
2111 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2112 417a6e49 2020-09-26 stsp if (error) {
2113 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2114 417a6e49 2020-09-26 stsp goto done;
2115 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2116 417a6e49 2020-09-26 stsp repo);
2117 417a6e49 2020-09-26 stsp if (error)
2118 417a6e49 2020-09-26 stsp goto done;
2119 417a6e49 2020-09-26 stsp } else {
2120 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2121 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2122 417a6e49 2020-09-26 stsp error = unlock_err;
2123 417a6e49 2020-09-26 stsp got_ref_close(ref);
2124 417a6e49 2020-09-26 stsp }
2125 417a6e49 2020-09-26 stsp }
2126 417a6e49 2020-09-26 stsp }
2127 417a6e49 2020-09-26 stsp if (delete_refs) {
2128 417a6e49 2020-09-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2129 417a6e49 2020-09-26 stsp verbosity, repo);
2130 417a6e49 2020-09-26 stsp if (error)
2131 417a6e49 2020-09-26 stsp goto done;
2132 417a6e49 2020-09-26 stsp }
2133 417a6e49 2020-09-26 stsp
2134 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
2135 417a6e49 2020-09-26 stsp /* Update remote HEAD reference if the server provided one. */
2136 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2137 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
2138 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2139 417a6e49 2020-09-26 stsp const char *target = pe->data;
2140 417a6e49 2020-09-26 stsp char *remote_refname = NULL, *remote_target = NULL;
2141 417a6e49 2020-09-26 stsp
2142 417a6e49 2020-09-26 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2143 417a6e49 2020-09-26 stsp continue;
2144 417a6e49 2020-09-26 stsp
2145 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", target, 11) != 0)
2146 417a6e49 2020-09-26 stsp continue;
2147 417a6e49 2020-09-26 stsp
2148 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2149 417a6e49 2020-09-26 stsp remote->name, refname) == -1) {
2150 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2151 417a6e49 2020-09-26 stsp goto done;
2152 417a6e49 2020-09-26 stsp }
2153 417a6e49 2020-09-26 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2154 417a6e49 2020-09-26 stsp remote->name, target + 11) == -1) {
2155 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2156 417a6e49 2020-09-26 stsp free(remote_refname);
2157 417a6e49 2020-09-26 stsp goto done;
2158 417a6e49 2020-09-26 stsp }
2159 417a6e49 2020-09-26 stsp
2160 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, remote_target,
2161 417a6e49 2020-09-26 stsp 0);
2162 417a6e49 2020-09-26 stsp if (error) {
2163 417a6e49 2020-09-26 stsp free(remote_refname);
2164 417a6e49 2020-09-26 stsp free(remote_target);
2165 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
2166 417a6e49 2020-09-26 stsp error = NULL;
2167 417a6e49 2020-09-26 stsp continue;
2168 417a6e49 2020-09-26 stsp }
2169 417a6e49 2020-09-26 stsp goto done;
2170 417a6e49 2020-09-26 stsp }
2171 417a6e49 2020-09-26 stsp error = update_symref(remote_refname, target_ref,
2172 417a6e49 2020-09-26 stsp verbosity, repo);
2173 417a6e49 2020-09-26 stsp free(remote_refname);
2174 417a6e49 2020-09-26 stsp free(remote_target);
2175 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
2176 417a6e49 2020-09-26 stsp if (error)
2177 417a6e49 2020-09-26 stsp goto done;
2178 417a6e49 2020-09-26 stsp }
2179 417a6e49 2020-09-26 stsp }
2180 417a6e49 2020-09-26 stsp done:
2181 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
2182 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
2183 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
2184 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2185 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
2186 417a6e49 2020-09-26 stsp }
2187 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2188 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
2189 417a6e49 2020-09-26 stsp if (repo)
2190 417a6e49 2020-09-26 stsp got_repo_close(repo);
2191 417a6e49 2020-09-26 stsp if (worktree)
2192 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
2193 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2194 417a6e49 2020-09-26 stsp free((void *)pe->path);
2195 417a6e49 2020-09-26 stsp free(pe->data);
2196 417a6e49 2020-09-26 stsp }
2197 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
2198 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2199 417a6e49 2020-09-26 stsp free((void *)pe->path);
2200 417a6e49 2020-09-26 stsp free(pe->data);
2201 417a6e49 2020-09-26 stsp }
2202 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
2203 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
2204 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
2205 417a6e49 2020-09-26 stsp free(id_str);
2206 417a6e49 2020-09-26 stsp free(cwd);
2207 417a6e49 2020-09-26 stsp free(repo_path);
2208 417a6e49 2020-09-26 stsp free(pack_hash);
2209 417a6e49 2020-09-26 stsp free(proto);
2210 417a6e49 2020-09-26 stsp free(host);
2211 417a6e49 2020-09-26 stsp free(port);
2212 417a6e49 2020-09-26 stsp free(server_path);
2213 417a6e49 2020-09-26 stsp free(repo_name);
2214 417a6e49 2020-09-26 stsp return error;
2215 417a6e49 2020-09-26 stsp }
2216 417a6e49 2020-09-26 stsp
2217 417a6e49 2020-09-26 stsp
2218 417a6e49 2020-09-26 stsp __dead static void
2219 417a6e49 2020-09-26 stsp usage_checkout(void)
2220 417a6e49 2020-09-26 stsp {
2221 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2222 417a6e49 2020-09-26 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2223 417a6e49 2020-09-26 stsp exit(1);
2224 417a6e49 2020-09-26 stsp }
2225 417a6e49 2020-09-26 stsp
2226 417a6e49 2020-09-26 stsp static void
2227 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning(void)
2228 417a6e49 2020-09-26 stsp {
2229 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: warning: could not create a reference "
2230 417a6e49 2020-09-26 stsp "to the work tree's base commit; the commit could be "
2231 417a6e49 2020-09-26 stsp "garbage-collected by Git; making the repository "
2232 417a6e49 2020-09-26 stsp "writable and running 'got update' will prevent this\n",
2233 417a6e49 2020-09-26 stsp getprogname());
2234 417a6e49 2020-09-26 stsp }
2235 417a6e49 2020-09-26 stsp
2236 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg {
2237 417a6e49 2020-09-26 stsp const char *worktree_path;
2238 417a6e49 2020-09-26 stsp int had_base_commit_ref_error;
2239 417a6e49 2020-09-26 stsp };
2240 417a6e49 2020-09-26 stsp
2241 417a6e49 2020-09-26 stsp static const struct got_error *
2242 417a6e49 2020-09-26 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2243 417a6e49 2020-09-26 stsp {
2244 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg *a = arg;
2245 417a6e49 2020-09-26 stsp
2246 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2247 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2248 417a6e49 2020-09-26 stsp return NULL;
2249 417a6e49 2020-09-26 stsp
2250 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2251 417a6e49 2020-09-26 stsp a->had_base_commit_ref_error = 1;
2252 417a6e49 2020-09-26 stsp return NULL;
2253 417a6e49 2020-09-26 stsp }
2254 417a6e49 2020-09-26 stsp
2255 417a6e49 2020-09-26 stsp while (path[0] == '/')
2256 417a6e49 2020-09-26 stsp path++;
2257 417a6e49 2020-09-26 stsp
2258 417a6e49 2020-09-26 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2259 417a6e49 2020-09-26 stsp return NULL;
2260 417a6e49 2020-09-26 stsp }
2261 417a6e49 2020-09-26 stsp
2262 417a6e49 2020-09-26 stsp static const struct got_error *
2263 417a6e49 2020-09-26 stsp check_cancelled(void *arg)
2264 417a6e49 2020-09-26 stsp {
2265 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
2266 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
2267 417a6e49 2020-09-26 stsp return NULL;
2268 417a6e49 2020-09-26 stsp }
2269 417a6e49 2020-09-26 stsp
2270 417a6e49 2020-09-26 stsp static const struct got_error *
2271 417a6e49 2020-09-26 stsp check_linear_ancestry(struct got_object_id *commit_id,
2272 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2273 417a6e49 2020-09-26 stsp struct got_repository *repo)
2274 417a6e49 2020-09-26 stsp {
2275 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2276 417a6e49 2020-09-26 stsp struct got_object_id *yca_id;
2277 417a6e49 2020-09-26 stsp
2278 417a6e49 2020-09-26 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2279 417a6e49 2020-09-26 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2280 417a6e49 2020-09-26 stsp if (err)
2281 417a6e49 2020-09-26 stsp return err;
2282 417a6e49 2020-09-26 stsp
2283 417a6e49 2020-09-26 stsp if (yca_id == NULL)
2284 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2285 417a6e49 2020-09-26 stsp
2286 417a6e49 2020-09-26 stsp /*
2287 417a6e49 2020-09-26 stsp * Require a straight line of history between the target commit
2288 417a6e49 2020-09-26 stsp * and the work tree's base commit.
2289 417a6e49 2020-09-26 stsp *
2290 417a6e49 2020-09-26 stsp * Non-linear situations such as this require a rebase:
2291 417a6e49 2020-09-26 stsp *
2292 417a6e49 2020-09-26 stsp * (commit) D F (base_commit)
2293 417a6e49 2020-09-26 stsp * \ /
2294 417a6e49 2020-09-26 stsp * C E
2295 417a6e49 2020-09-26 stsp * \ /
2296 417a6e49 2020-09-26 stsp * B (yca)
2297 417a6e49 2020-09-26 stsp * |
2298 417a6e49 2020-09-26 stsp * A
2299 417a6e49 2020-09-26 stsp *
2300 417a6e49 2020-09-26 stsp * 'got update' only handles linear cases:
2301 417a6e49 2020-09-26 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2302 417a6e49 2020-09-26 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2303 417a6e49 2020-09-26 stsp */
2304 417a6e49 2020-09-26 stsp if (allow_forwards_in_time_only) {
2305 417a6e49 2020-09-26 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2306 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2307 417a6e49 2020-09-26 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2308 417a6e49 2020-09-26 stsp 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
2311 417a6e49 2020-09-26 stsp free(yca_id);
2312 417a6e49 2020-09-26 stsp return NULL;
2313 417a6e49 2020-09-26 stsp }
2314 417a6e49 2020-09-26 stsp
2315 417a6e49 2020-09-26 stsp static const struct got_error *
2316 417a6e49 2020-09-26 stsp check_same_branch(struct got_object_id *commit_id,
2317 417a6e49 2020-09-26 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2318 417a6e49 2020-09-26 stsp struct got_repository *repo)
2319 417a6e49 2020-09-26 stsp {
2320 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2321 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
2322 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
2323 417a6e49 2020-09-26 stsp int is_same_branch = 0;
2324 417a6e49 2020-09-26 stsp
2325 417a6e49 2020-09-26 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2326 417a6e49 2020-09-26 stsp if (err)
2327 417a6e49 2020-09-26 stsp goto done;
2328 417a6e49 2020-09-26 stsp
2329 417a6e49 2020-09-26 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2330 417a6e49 2020-09-26 stsp is_same_branch = 1;
2331 417a6e49 2020-09-26 stsp goto done;
2332 417a6e49 2020-09-26 stsp }
2333 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2334 417a6e49 2020-09-26 stsp is_same_branch = 1;
2335 417a6e49 2020-09-26 stsp goto done;
2336 417a6e49 2020-09-26 stsp }
2337 417a6e49 2020-09-26 stsp
2338 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
2339 417a6e49 2020-09-26 stsp if (err)
2340 417a6e49 2020-09-26 stsp goto done;
2341 417a6e49 2020-09-26 stsp
2342 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2343 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2344 417a6e49 2020-09-26 stsp if (err)
2345 417a6e49 2020-09-26 stsp goto done;
2346 417a6e49 2020-09-26 stsp
2347 417a6e49 2020-09-26 stsp for (;;) {
2348 417a6e49 2020-09-26 stsp struct got_object_id *id;
2349 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2350 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2351 417a6e49 2020-09-26 stsp if (err) {
2352 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2353 417a6e49 2020-09-26 stsp err = NULL;
2354 417a6e49 2020-09-26 stsp break;
2355 417a6e49 2020-09-26 stsp }
2356 417a6e49 2020-09-26 stsp
2357 417a6e49 2020-09-26 stsp if (id) {
2358 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2359 417a6e49 2020-09-26 stsp break;
2360 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2361 417a6e49 2020-09-26 stsp is_same_branch = 1;
2362 417a6e49 2020-09-26 stsp break;
2363 417a6e49 2020-09-26 stsp }
2364 417a6e49 2020-09-26 stsp }
2365 417a6e49 2020-09-26 stsp }
2366 417a6e49 2020-09-26 stsp done:
2367 417a6e49 2020-09-26 stsp if (graph)
2368 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
2369 417a6e49 2020-09-26 stsp free(head_commit_id);
2370 417a6e49 2020-09-26 stsp if (!err && !is_same_branch)
2371 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_ANCESTRY);
2372 417a6e49 2020-09-26 stsp return err;
2373 417a6e49 2020-09-26 stsp }
2374 417a6e49 2020-09-26 stsp
2375 417a6e49 2020-09-26 stsp static const struct got_error *
2376 417a6e49 2020-09-26 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2377 417a6e49 2020-09-26 stsp {
2378 417a6e49 2020-09-26 stsp static char msg[512];
2379 417a6e49 2020-09-26 stsp const char *branch_name;
2380 417a6e49 2020-09-26 stsp
2381 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref))
2382 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(ref);
2383 417a6e49 2020-09-26 stsp else
2384 417a6e49 2020-09-26 stsp branch_name = got_ref_get_name(ref);
2385 417a6e49 2020-09-26 stsp
2386 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2387 417a6e49 2020-09-26 stsp branch_name += 11;
2388 417a6e49 2020-09-26 stsp
2389 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2390 417a6e49 2020-09-26 stsp "target commit is not contained in branch '%s'; "
2391 417a6e49 2020-09-26 stsp "the branch to use must be specified with -b; "
2392 417a6e49 2020-09-26 stsp "if necessary a new branch can be created for "
2393 417a6e49 2020-09-26 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2394 417a6e49 2020-09-26 stsp branch_name, commit_id_str);
2395 417a6e49 2020-09-26 stsp
2396 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2397 417a6e49 2020-09-26 stsp }
2398 417a6e49 2020-09-26 stsp
2399 417a6e49 2020-09-26 stsp static const struct got_error *
2400 417a6e49 2020-09-26 stsp cmd_checkout(int argc, char *argv[])
2401 417a6e49 2020-09-26 stsp {
2402 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2403 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2404 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2405 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2406 417a6e49 2020-09-26 stsp char *repo_path = NULL;
2407 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2408 417a6e49 2020-09-26 stsp const char *path_prefix = "";
2409 417a6e49 2020-09-26 stsp const char *branch_name = GOT_REF_HEAD;
2410 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2411 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
2412 417a6e49 2020-09-26 stsp int ch, same_path_prefix, allow_nonempty = 0;
2413 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2414 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg cpa;
2415 417a6e49 2020-09-26 stsp
2416 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2417 417a6e49 2020-09-26 stsp
2418 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2419 417a6e49 2020-09-26 stsp switch (ch) {
2420 417a6e49 2020-09-26 stsp case 'b':
2421 417a6e49 2020-09-26 stsp branch_name = optarg;
2422 417a6e49 2020-09-26 stsp break;
2423 417a6e49 2020-09-26 stsp case 'c':
2424 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2425 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2426 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2427 417a6e49 2020-09-26 stsp break;
2428 417a6e49 2020-09-26 stsp case 'E':
2429 417a6e49 2020-09-26 stsp allow_nonempty = 1;
2430 417a6e49 2020-09-26 stsp break;
2431 417a6e49 2020-09-26 stsp case 'p':
2432 417a6e49 2020-09-26 stsp path_prefix = optarg;
2433 417a6e49 2020-09-26 stsp break;
2434 417a6e49 2020-09-26 stsp default:
2435 417a6e49 2020-09-26 stsp usage_checkout();
2436 417a6e49 2020-09-26 stsp /* NOTREACHED */
2437 417a6e49 2020-09-26 stsp }
2438 417a6e49 2020-09-26 stsp }
2439 417a6e49 2020-09-26 stsp
2440 417a6e49 2020-09-26 stsp argc -= optind;
2441 417a6e49 2020-09-26 stsp argv += optind;
2442 417a6e49 2020-09-26 stsp
2443 417a6e49 2020-09-26 stsp #ifndef PROFILE
2444 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2445 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2446 417a6e49 2020-09-26 stsp err(1, "pledge");
2447 417a6e49 2020-09-26 stsp #endif
2448 417a6e49 2020-09-26 stsp if (argc == 1) {
2449 417a6e49 2020-09-26 stsp char *base, *dotgit;
2450 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2451 417a6e49 2020-09-26 stsp if (repo_path == NULL)
2452 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath", argv[0]);
2453 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
2454 417a6e49 2020-09-26 stsp if (cwd == NULL) {
2455 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2456 417a6e49 2020-09-26 stsp goto done;
2457 417a6e49 2020-09-26 stsp }
2458 417a6e49 2020-09-26 stsp if (path_prefix[0])
2459 417a6e49 2020-09-26 stsp path = strdup(path_prefix);
2460 417a6e49 2020-09-26 stsp else
2461 417a6e49 2020-09-26 stsp path = strdup(repo_path);
2462 417a6e49 2020-09-26 stsp if (path == NULL) {
2463 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2464 417a6e49 2020-09-26 stsp goto done;
2465 417a6e49 2020-09-26 stsp }
2466 417a6e49 2020-09-26 stsp base = basename(path);
2467 417a6e49 2020-09-26 stsp if (base == NULL) {
2468 417a6e49 2020-09-26 stsp error = got_error_from_errno2("basename", path);
2469 417a6e49 2020-09-26 stsp goto done;
2470 417a6e49 2020-09-26 stsp }
2471 417a6e49 2020-09-26 stsp dotgit = strstr(base, ".git");
2472 417a6e49 2020-09-26 stsp if (dotgit)
2473 417a6e49 2020-09-26 stsp *dotgit = '\0';
2474 417a6e49 2020-09-26 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2475 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2476 417a6e49 2020-09-26 stsp goto done;
2477 417a6e49 2020-09-26 stsp }
2478 417a6e49 2020-09-26 stsp } else if (argc == 2) {
2479 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2480 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
2481 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath", argv[0]);
2482 417a6e49 2020-09-26 stsp goto done;
2483 417a6e49 2020-09-26 stsp }
2484 417a6e49 2020-09-26 stsp worktree_path = realpath(argv[1], NULL);
2485 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2486 417a6e49 2020-09-26 stsp if (errno != ENOENT) {
2487 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath",
2488 417a6e49 2020-09-26 stsp argv[1]);
2489 417a6e49 2020-09-26 stsp goto done;
2490 417a6e49 2020-09-26 stsp }
2491 417a6e49 2020-09-26 stsp worktree_path = strdup(argv[1]);
2492 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2493 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2494 417a6e49 2020-09-26 stsp goto done;
2495 417a6e49 2020-09-26 stsp }
2496 417a6e49 2020-09-26 stsp }
2497 417a6e49 2020-09-26 stsp } else
2498 417a6e49 2020-09-26 stsp usage_checkout();
2499 417a6e49 2020-09-26 stsp
2500 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
2501 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(worktree_path);
2502 417a6e49 2020-09-26 stsp
2503 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
2504 417a6e49 2020-09-26 stsp if (error != NULL)
2505 417a6e49 2020-09-26 stsp goto done;
2506 417a6e49 2020-09-26 stsp
2507 417a6e49 2020-09-26 stsp /* Pre-create work tree path for unveil(2) */
2508 417a6e49 2020-09-26 stsp error = got_path_mkdir(worktree_path);
2509 417a6e49 2020-09-26 stsp if (error) {
2510 417a6e49 2020-09-26 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2511 417a6e49 2020-09-26 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2512 417a6e49 2020-09-26 stsp goto done;
2513 417a6e49 2020-09-26 stsp if (!allow_nonempty &&
2514 417a6e49 2020-09-26 stsp !got_path_dir_is_empty(worktree_path)) {
2515 417a6e49 2020-09-26 stsp error = got_error_path(worktree_path,
2516 417a6e49 2020-09-26 stsp GOT_ERR_DIR_NOT_EMPTY);
2517 417a6e49 2020-09-26 stsp goto done;
2518 417a6e49 2020-09-26 stsp }
2519 417a6e49 2020-09-26 stsp }
2520 417a6e49 2020-09-26 stsp
2521 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2522 417a6e49 2020-09-26 stsp if (error)
2523 417a6e49 2020-09-26 stsp goto done;
2524 417a6e49 2020-09-26 stsp
2525 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2526 417a6e49 2020-09-26 stsp if (error != NULL)
2527 417a6e49 2020-09-26 stsp goto done;
2528 417a6e49 2020-09-26 stsp
2529 417a6e49 2020-09-26 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2530 417a6e49 2020-09-26 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2531 417a6e49 2020-09-26 stsp goto done;
2532 417a6e49 2020-09-26 stsp
2533 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2534 417a6e49 2020-09-26 stsp if (error != NULL)
2535 417a6e49 2020-09-26 stsp goto done;
2536 417a6e49 2020-09-26 stsp
2537 417a6e49 2020-09-26 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2538 417a6e49 2020-09-26 stsp path_prefix);
2539 417a6e49 2020-09-26 stsp if (error != NULL)
2540 417a6e49 2020-09-26 stsp goto done;
2541 417a6e49 2020-09-26 stsp if (!same_path_prefix) {
2542 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2543 417a6e49 2020-09-26 stsp goto done;
2544 417a6e49 2020-09-26 stsp }
2545 417a6e49 2020-09-26 stsp
2546 417a6e49 2020-09-26 stsp if (commit_id_str) {
2547 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
2548 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2549 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2550 417a6e49 2020-09-26 stsp if (error)
2551 417a6e49 2020-09-26 stsp goto done;
2552 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2553 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2554 417a6e49 2020-09-26 stsp if (error != NULL) {
2555 417a6e49 2020-09-26 stsp free(commit_id);
2556 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2557 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2558 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2559 417a6e49 2020-09-26 stsp }
2560 417a6e49 2020-09-26 stsp goto done;
2561 417a6e49 2020-09-26 stsp }
2562 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2563 417a6e49 2020-09-26 stsp if (error) {
2564 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2565 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2566 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2567 417a6e49 2020-09-26 stsp }
2568 417a6e49 2020-09-26 stsp goto done;
2569 417a6e49 2020-09-26 stsp }
2570 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2571 417a6e49 2020-09-26 stsp commit_id);
2572 417a6e49 2020-09-26 stsp free(commit_id);
2573 417a6e49 2020-09-26 stsp if (error)
2574 417a6e49 2020-09-26 stsp goto done;
2575 417a6e49 2020-09-26 stsp }
2576 417a6e49 2020-09-26 stsp
2577 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
2578 417a6e49 2020-09-26 stsp if (error)
2579 417a6e49 2020-09-26 stsp goto done;
2580 417a6e49 2020-09-26 stsp cpa.worktree_path = worktree_path;
2581 417a6e49 2020-09-26 stsp cpa.had_base_commit_ref_error = 0;
2582 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2583 417a6e49 2020-09-26 stsp checkout_progress, &cpa, check_cancelled, NULL);
2584 417a6e49 2020-09-26 stsp if (error != NULL)
2585 417a6e49 2020-09-26 stsp goto done;
2586 417a6e49 2020-09-26 stsp
2587 417a6e49 2020-09-26 stsp printf("Now shut up and hack\n");
2588 417a6e49 2020-09-26 stsp if (cpa.had_base_commit_ref_error)
2589 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning();
2590 417a6e49 2020-09-26 stsp done:
2591 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2592 417a6e49 2020-09-26 stsp free(commit_id_str);
2593 417a6e49 2020-09-26 stsp free(repo_path);
2594 417a6e49 2020-09-26 stsp free(worktree_path);
2595 417a6e49 2020-09-26 stsp free(cwd);
2596 417a6e49 2020-09-26 stsp free(path);
2597 417a6e49 2020-09-26 stsp return error;
2598 417a6e49 2020-09-26 stsp }
2599 417a6e49 2020-09-26 stsp
2600 417a6e49 2020-09-26 stsp struct got_update_progress_arg {
2601 417a6e49 2020-09-26 stsp int did_something;
2602 417a6e49 2020-09-26 stsp int conflicts;
2603 417a6e49 2020-09-26 stsp int obstructed;
2604 417a6e49 2020-09-26 stsp int not_updated;
2605 417a6e49 2020-09-26 stsp };
2606 417a6e49 2020-09-26 stsp
2607 417a6e49 2020-09-26 stsp void
2608 417a6e49 2020-09-26 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2609 417a6e49 2020-09-26 stsp {
2610 417a6e49 2020-09-26 stsp if (!upa->did_something)
2611 417a6e49 2020-09-26 stsp return;
2612 417a6e49 2020-09-26 stsp
2613 417a6e49 2020-09-26 stsp if (upa->conflicts > 0)
2614 417a6e49 2020-09-26 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2615 417a6e49 2020-09-26 stsp if (upa->obstructed > 0)
2616 417a6e49 2020-09-26 stsp printf("File paths obstructed by a non-regular file: %d\n",
2617 417a6e49 2020-09-26 stsp upa->obstructed);
2618 417a6e49 2020-09-26 stsp if (upa->not_updated > 0)
2619 417a6e49 2020-09-26 stsp printf("Files not updated because of existing merge "
2620 417a6e49 2020-09-26 stsp "conflicts: %d\n", upa->not_updated);
2621 417a6e49 2020-09-26 stsp }
2622 417a6e49 2020-09-26 stsp
2623 417a6e49 2020-09-26 stsp __dead static void
2624 417a6e49 2020-09-26 stsp usage_update(void)
2625 417a6e49 2020-09-26 stsp {
2626 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2627 417a6e49 2020-09-26 stsp getprogname());
2628 417a6e49 2020-09-26 stsp exit(1);
2629 417a6e49 2020-09-26 stsp }
2630 417a6e49 2020-09-26 stsp
2631 417a6e49 2020-09-26 stsp static const struct got_error *
2632 417a6e49 2020-09-26 stsp update_progress(void *arg, unsigned char status, const char *path)
2633 417a6e49 2020-09-26 stsp {
2634 417a6e49 2020-09-26 stsp struct got_update_progress_arg *upa = arg;
2635 417a6e49 2020-09-26 stsp
2636 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_EXISTS ||
2637 417a6e49 2020-09-26 stsp status == GOT_STATUS_BASE_REF_ERR)
2638 417a6e49 2020-09-26 stsp return NULL;
2639 417a6e49 2020-09-26 stsp
2640 417a6e49 2020-09-26 stsp upa->did_something = 1;
2641 417a6e49 2020-09-26 stsp
2642 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2643 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2644 417a6e49 2020-09-26 stsp return NULL;
2645 417a6e49 2020-09-26 stsp
2646 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CONFLICT)
2647 417a6e49 2020-09-26 stsp upa->conflicts++;
2648 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_OBSTRUCTED)
2649 417a6e49 2020-09-26 stsp upa->obstructed++;
2650 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2651 417a6e49 2020-09-26 stsp upa->not_updated++;
2652 417a6e49 2020-09-26 stsp
2653 417a6e49 2020-09-26 stsp while (path[0] == '/')
2654 417a6e49 2020-09-26 stsp path++;
2655 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
2656 417a6e49 2020-09-26 stsp return NULL;
2657 417a6e49 2020-09-26 stsp }
2658 417a6e49 2020-09-26 stsp
2659 417a6e49 2020-09-26 stsp static const struct got_error *
2660 417a6e49 2020-09-26 stsp switch_head_ref(struct got_reference *head_ref,
2661 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2662 417a6e49 2020-09-26 stsp struct got_repository *repo)
2663 417a6e49 2020-09-26 stsp {
2664 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2665 417a6e49 2020-09-26 stsp char *base_id_str;
2666 417a6e49 2020-09-26 stsp int ref_has_moved = 0;
2667 417a6e49 2020-09-26 stsp
2668 417a6e49 2020-09-26 stsp /* Trivial case: switching between two different references. */
2669 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_name(head_ref),
2670 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2671 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n",
2672 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree),
2673 417a6e49 2020-09-26 stsp got_ref_get_name(head_ref));
2674 417a6e49 2020-09-26 stsp return got_worktree_set_head_ref(worktree, head_ref);
2675 417a6e49 2020-09-26 stsp }
2676 417a6e49 2020-09-26 stsp
2677 417a6e49 2020-09-26 stsp err = check_linear_ancestry(commit_id,
2678 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2679 417a6e49 2020-09-26 stsp if (err) {
2680 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_ANCESTRY)
2681 417a6e49 2020-09-26 stsp return err;
2682 417a6e49 2020-09-26 stsp ref_has_moved = 1;
2683 417a6e49 2020-09-26 stsp }
2684 417a6e49 2020-09-26 stsp if (!ref_has_moved)
2685 417a6e49 2020-09-26 stsp return NULL;
2686 417a6e49 2020-09-26 stsp
2687 417a6e49 2020-09-26 stsp /* Switching to a rebased branch with the same reference name. */
2688 417a6e49 2020-09-26 stsp err = got_object_id_str(&base_id_str,
2689 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
2690 417a6e49 2020-09-26 stsp if (err)
2691 417a6e49 2020-09-26 stsp return err;
2692 417a6e49 2020-09-26 stsp printf("Reference %s now points at a different branch\n",
2693 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2694 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2695 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2696 417a6e49 2020-09-26 stsp return NULL;
2697 417a6e49 2020-09-26 stsp }
2698 417a6e49 2020-09-26 stsp
2699 417a6e49 2020-09-26 stsp static const struct got_error *
2700 417a6e49 2020-09-26 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2701 417a6e49 2020-09-26 stsp {
2702 417a6e49 2020-09-26 stsp const struct got_error *err;
2703 417a6e49 2020-09-26 stsp int in_progress;
2704 417a6e49 2020-09-26 stsp
2705 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2706 417a6e49 2020-09-26 stsp if (err)
2707 417a6e49 2020-09-26 stsp return err;
2708 417a6e49 2020-09-26 stsp if (in_progress)
2709 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_REBASING);
2710 417a6e49 2020-09-26 stsp
2711 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2712 417a6e49 2020-09-26 stsp if (err)
2713 417a6e49 2020-09-26 stsp return err;
2714 417a6e49 2020-09-26 stsp if (in_progress)
2715 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2716 417a6e49 2020-09-26 stsp
2717 417a6e49 2020-09-26 stsp return NULL;
2718 417a6e49 2020-09-26 stsp }
2719 417a6e49 2020-09-26 stsp
2720 417a6e49 2020-09-26 stsp static const struct got_error *
2721 417a6e49 2020-09-26 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2722 417a6e49 2020-09-26 stsp char *argv[], struct got_worktree *worktree)
2723 417a6e49 2020-09-26 stsp {
2724 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2725 417a6e49 2020-09-26 stsp char *path;
2726 417a6e49 2020-09-26 stsp int i;
2727 417a6e49 2020-09-26 stsp
2728 417a6e49 2020-09-26 stsp if (argc == 0) {
2729 417a6e49 2020-09-26 stsp path = strdup("");
2730 417a6e49 2020-09-26 stsp if (path == NULL)
2731 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2732 417a6e49 2020-09-26 stsp return got_pathlist_append(paths, path, NULL);
2733 417a6e49 2020-09-26 stsp }
2734 417a6e49 2020-09-26 stsp
2735 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
2736 417a6e49 2020-09-26 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2737 417a6e49 2020-09-26 stsp if (err)
2738 417a6e49 2020-09-26 stsp break;
2739 417a6e49 2020-09-26 stsp err = got_pathlist_append(paths, path, NULL);
2740 417a6e49 2020-09-26 stsp if (err) {
2741 417a6e49 2020-09-26 stsp free(path);
2742 417a6e49 2020-09-26 stsp break;
2743 417a6e49 2020-09-26 stsp }
2744 417a6e49 2020-09-26 stsp }
2745 417a6e49 2020-09-26 stsp
2746 417a6e49 2020-09-26 stsp return err;
2747 417a6e49 2020-09-26 stsp }
2748 417a6e49 2020-09-26 stsp
2749 417a6e49 2020-09-26 stsp static const struct got_error *
2750 417a6e49 2020-09-26 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2751 417a6e49 2020-09-26 stsp const char *cmdname, const char *path)
2752 417a6e49 2020-09-26 stsp {
2753 417a6e49 2020-09-26 stsp const struct got_error *err;
2754 417a6e49 2020-09-26 stsp struct got_repository *repo;
2755 417a6e49 2020-09-26 stsp static char msg[512];
2756 417a6e49 2020-09-26 stsp
2757 417a6e49 2020-09-26 stsp err = got_repo_open(&repo, path, NULL);
2758 417a6e49 2020-09-26 stsp if (err)
2759 417a6e49 2020-09-26 stsp return orig_err;
2760 417a6e49 2020-09-26 stsp
2761 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2762 417a6e49 2020-09-26 stsp "'got %s' needs a work tree in addition to a git repository\n"
2763 417a6e49 2020-09-26 stsp "Work trees can be checked out from this Git repository with "
2764 417a6e49 2020-09-26 stsp "'got checkout'.\n"
2765 417a6e49 2020-09-26 stsp "The got(1) manual page contains more information.", cmdname);
2766 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2767 417a6e49 2020-09-26 stsp got_repo_close(repo);
2768 417a6e49 2020-09-26 stsp return err;
2769 417a6e49 2020-09-26 stsp }
2770 417a6e49 2020-09-26 stsp
2771 417a6e49 2020-09-26 stsp static const struct got_error *
2772 417a6e49 2020-09-26 stsp cmd_update(int argc, char *argv[])
2773 417a6e49 2020-09-26 stsp {
2774 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2775 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2776 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2777 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2778 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
2779 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2780 417a6e49 2020-09-26 stsp const char *branch_name = NULL;
2781 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2782 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2783 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
2784 417a6e49 2020-09-26 stsp int ch;
2785 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
2786 417a6e49 2020-09-26 stsp
2787 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2788 417a6e49 2020-09-26 stsp
2789 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2790 417a6e49 2020-09-26 stsp switch (ch) {
2791 417a6e49 2020-09-26 stsp case 'b':
2792 417a6e49 2020-09-26 stsp branch_name = optarg;
2793 417a6e49 2020-09-26 stsp break;
2794 417a6e49 2020-09-26 stsp case 'c':
2795 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2796 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2797 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2798 417a6e49 2020-09-26 stsp break;
2799 417a6e49 2020-09-26 stsp default:
2800 417a6e49 2020-09-26 stsp usage_update();
2801 417a6e49 2020-09-26 stsp /* NOTREACHED */
2802 417a6e49 2020-09-26 stsp }
2803 417a6e49 2020-09-26 stsp }
2804 417a6e49 2020-09-26 stsp
2805 417a6e49 2020-09-26 stsp argc -= optind;
2806 417a6e49 2020-09-26 stsp argv += optind;
2807 417a6e49 2020-09-26 stsp
2808 417a6e49 2020-09-26 stsp #ifndef PROFILE
2809 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2810 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2811 417a6e49 2020-09-26 stsp err(1, "pledge");
2812 417a6e49 2020-09-26 stsp #endif
2813 417a6e49 2020-09-26 stsp worktree_path = getcwd(NULL, 0);
2814 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2815 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2816 417a6e49 2020-09-26 stsp goto done;
2817 417a6e49 2020-09-26 stsp }
2818 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2819 417a6e49 2020-09-26 stsp if (error) {
2820 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2821 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "update",
2822 417a6e49 2020-09-26 stsp worktree_path);
2823 417a6e49 2020-09-26 stsp goto done;
2824 417a6e49 2020-09-26 stsp }
2825 417a6e49 2020-09-26 stsp
2826 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
2827 417a6e49 2020-09-26 stsp if (error)
2828 417a6e49 2020-09-26 stsp goto done;
2829 417a6e49 2020-09-26 stsp
2830 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2831 417a6e49 2020-09-26 stsp NULL);
2832 417a6e49 2020-09-26 stsp if (error != NULL)
2833 417a6e49 2020-09-26 stsp goto done;
2834 417a6e49 2020-09-26 stsp
2835 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2836 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
2837 417a6e49 2020-09-26 stsp if (error)
2838 417a6e49 2020-09-26 stsp goto done;
2839 417a6e49 2020-09-26 stsp
2840 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2841 417a6e49 2020-09-26 stsp if (error)
2842 417a6e49 2020-09-26 stsp goto done;
2843 417a6e49 2020-09-26 stsp
2844 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2845 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
2846 417a6e49 2020-09-26 stsp if (error != NULL)
2847 417a6e49 2020-09-26 stsp goto done;
2848 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
2849 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2850 417a6e49 2020-09-26 stsp if (error != NULL)
2851 417a6e49 2020-09-26 stsp goto done;
2852 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2853 417a6e49 2020-09-26 stsp if (error != NULL)
2854 417a6e49 2020-09-26 stsp goto done;
2855 417a6e49 2020-09-26 stsp } else {
2856 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2857 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2858 417a6e49 2020-09-26 stsp free(commit_id_str);
2859 417a6e49 2020-09-26 stsp commit_id_str = NULL;
2860 417a6e49 2020-09-26 stsp if (error)
2861 417a6e49 2020-09-26 stsp goto done;
2862 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2863 417a6e49 2020-09-26 stsp if (error)
2864 417a6e49 2020-09-26 stsp goto done;
2865 417a6e49 2020-09-26 stsp }
2866 417a6e49 2020-09-26 stsp
2867 417a6e49 2020-09-26 stsp if (branch_name) {
2868 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id;
2869 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
2870 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
2871 417a6e49 2020-09-26 stsp continue;
2872 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2873 417a6e49 2020-09-26 stsp "switching between branches requires that "
2874 417a6e49 2020-09-26 stsp "the entire work tree gets updated");
2875 417a6e49 2020-09-26 stsp goto done;
2876 417a6e49 2020-09-26 stsp }
2877 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2878 417a6e49 2020-09-26 stsp if (error)
2879 417a6e49 2020-09-26 stsp goto done;
2880 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2881 417a6e49 2020-09-26 stsp repo);
2882 417a6e49 2020-09-26 stsp free(head_commit_id);
2883 417a6e49 2020-09-26 stsp if (error != NULL)
2884 417a6e49 2020-09-26 stsp goto done;
2885 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2886 417a6e49 2020-09-26 stsp if (error)
2887 417a6e49 2020-09-26 stsp goto done;
2888 417a6e49 2020-09-26 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2889 417a6e49 2020-09-26 stsp if (error)
2890 417a6e49 2020-09-26 stsp goto done;
2891 417a6e49 2020-09-26 stsp } else {
2892 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2893 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2894 417a6e49 2020-09-26 stsp if (error != NULL) {
2895 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
2896 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2897 417a6e49 2020-09-26 stsp goto done;
2898 417a6e49 2020-09-26 stsp }
2899 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2900 417a6e49 2020-09-26 stsp if (error)
2901 417a6e49 2020-09-26 stsp goto done;
2902 417a6e49 2020-09-26 stsp }
2903 417a6e49 2020-09-26 stsp
2904 417a6e49 2020-09-26 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2905 417a6e49 2020-09-26 stsp commit_id) != 0) {
2906 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2907 417a6e49 2020-09-26 stsp commit_id);
2908 417a6e49 2020-09-26 stsp if (error)
2909 417a6e49 2020-09-26 stsp goto done;
2910 417a6e49 2020-09-26 stsp }
2911 417a6e49 2020-09-26 stsp
2912 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
2913 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2914 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
2915 417a6e49 2020-09-26 stsp if (error != NULL)
2916 417a6e49 2020-09-26 stsp goto done;
2917 417a6e49 2020-09-26 stsp
2918 417a6e49 2020-09-26 stsp if (upa.did_something)
2919 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
2920 417a6e49 2020-09-26 stsp else
2921 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2922 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
2923 417a6e49 2020-09-26 stsp done:
2924 417a6e49 2020-09-26 stsp free(worktree_path);
2925 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
2926 417a6e49 2020-09-26 stsp free((char *)pe->path);
2927 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2928 417a6e49 2020-09-26 stsp free(commit_id);
2929 417a6e49 2020-09-26 stsp free(commit_id_str);
2930 417a6e49 2020-09-26 stsp return error;
2931 417a6e49 2020-09-26 stsp }
2932 417a6e49 2020-09-26 stsp
2933 417a6e49 2020-09-26 stsp static const struct got_error *
2934 417a6e49 2020-09-26 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2935 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2936 417a6e49 2020-09-26 stsp struct got_repository *repo)
2937 417a6e49 2020-09-26 stsp {
2938 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2939 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2940 417a6e49 2020-09-26 stsp
2941 417a6e49 2020-09-26 stsp if (blob_id1) {
2942 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2943 417a6e49 2020-09-26 stsp if (err)
2944 417a6e49 2020-09-26 stsp goto done;
2945 417a6e49 2020-09-26 stsp }
2946 417a6e49 2020-09-26 stsp
2947 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2948 417a6e49 2020-09-26 stsp if (err)
2949 417a6e49 2020-09-26 stsp goto done;
2950 417a6e49 2020-09-26 stsp
2951 417a6e49 2020-09-26 stsp while (path[0] == '/')
2952 417a6e49 2020-09-26 stsp path++;
2953 417a6e49 2020-09-26 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2954 417a6e49 2020-09-26 stsp ignore_whitespace, stdout);
2955 417a6e49 2020-09-26 stsp done:
2956 417a6e49 2020-09-26 stsp if (blob1)
2957 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
2958 417a6e49 2020-09-26 stsp got_object_blob_close(blob2);
2959 417a6e49 2020-09-26 stsp return err;
2960 417a6e49 2020-09-26 stsp }
2961 417a6e49 2020-09-26 stsp
2962 417a6e49 2020-09-26 stsp static const struct got_error *
2963 417a6e49 2020-09-26 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2964 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2965 417a6e49 2020-09-26 stsp struct got_repository *repo)
2966 417a6e49 2020-09-26 stsp {
2967 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2968 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2969 417a6e49 2020-09-26 stsp struct got_diff_blob_output_unidiff_arg arg;
2970 417a6e49 2020-09-26 stsp
2971 417a6e49 2020-09-26 stsp if (tree_id1) {
2972 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2973 417a6e49 2020-09-26 stsp if (err)
2974 417a6e49 2020-09-26 stsp goto done;
2975 417a6e49 2020-09-26 stsp }
2976 417a6e49 2020-09-26 stsp
2977 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2978 417a6e49 2020-09-26 stsp if (err)
2979 417a6e49 2020-09-26 stsp goto done;
2980 417a6e49 2020-09-26 stsp
2981 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
2982 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
2983 417a6e49 2020-09-26 stsp arg.outfile = stdout;
2984 417a6e49 2020-09-26 stsp while (path[0] == '/')
2985 417a6e49 2020-09-26 stsp path++;
2986 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2987 417a6e49 2020-09-26 stsp got_diff_blob_output_unidiff, &arg, 1);
2988 417a6e49 2020-09-26 stsp done:
2989 417a6e49 2020-09-26 stsp if (tree1)
2990 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
2991 417a6e49 2020-09-26 stsp if (tree2)
2992 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
2993 417a6e49 2020-09-26 stsp return err;
2994 417a6e49 2020-09-26 stsp }
2995 417a6e49 2020-09-26 stsp
2996 417a6e49 2020-09-26 stsp static const struct got_error *
2997 417a6e49 2020-09-26 stsp get_changed_paths(struct got_pathlist_head *paths,
2998 417a6e49 2020-09-26 stsp struct got_commit_object *commit, struct got_repository *repo)
2999 417a6e49 2020-09-26 stsp {
3000 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3001 417a6e49 2020-09-26 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3002 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3003 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3004 417a6e49 2020-09-26 stsp
3005 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3006 417a6e49 2020-09-26 stsp if (qid != NULL) {
3007 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit;
3008 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3009 417a6e49 2020-09-26 stsp qid->id);
3010 417a6e49 2020-09-26 stsp if (err)
3011 417a6e49 2020-09-26 stsp return err;
3012 417a6e49 2020-09-26 stsp
3013 417a6e49 2020-09-26 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3014 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3015 417a6e49 2020-09-26 stsp
3016 417a6e49 2020-09-26 stsp }
3017 417a6e49 2020-09-26 stsp
3018 417a6e49 2020-09-26 stsp if (tree_id1) {
3019 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3020 417a6e49 2020-09-26 stsp if (err)
3021 417a6e49 2020-09-26 stsp goto done;
3022 417a6e49 2020-09-26 stsp }
3023 417a6e49 2020-09-26 stsp
3024 417a6e49 2020-09-26 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3025 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3026 417a6e49 2020-09-26 stsp if (err)
3027 417a6e49 2020-09-26 stsp goto done;
3028 417a6e49 2020-09-26 stsp
3029 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3030 417a6e49 2020-09-26 stsp got_diff_tree_collect_changed_paths, paths, 0);
3031 417a6e49 2020-09-26 stsp done:
3032 417a6e49 2020-09-26 stsp if (tree1)
3033 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
3034 417a6e49 2020-09-26 stsp if (tree2)
3035 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
3036 417a6e49 2020-09-26 stsp return err;
3037 417a6e49 2020-09-26 stsp }
3038 417a6e49 2020-09-26 stsp
3039 417a6e49 2020-09-26 stsp static const struct got_error *
3040 417a6e49 2020-09-26 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3041 417a6e49 2020-09-26 stsp const char *path, int diff_context, struct got_repository *repo)
3042 417a6e49 2020-09-26 stsp {
3043 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3044 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit = NULL;
3045 417a6e49 2020-09-26 stsp char *id_str1 = NULL, *id_str2 = NULL;
3046 417a6e49 2020-09-26 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3047 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3048 417a6e49 2020-09-26 stsp
3049 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3050 417a6e49 2020-09-26 stsp if (qid != NULL) {
3051 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3052 417a6e49 2020-09-26 stsp qid->id);
3053 417a6e49 2020-09-26 stsp if (err)
3054 417a6e49 2020-09-26 stsp return err;
3055 417a6e49 2020-09-26 stsp }
3056 417a6e49 2020-09-26 stsp
3057 417a6e49 2020-09-26 stsp if (path && path[0] != '\0') {
3058 417a6e49 2020-09-26 stsp int obj_type;
3059 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3060 417a6e49 2020-09-26 stsp if (err)
3061 417a6e49 2020-09-26 stsp goto done;
3062 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3063 417a6e49 2020-09-26 stsp if (err) {
3064 417a6e49 2020-09-26 stsp free(obj_id2);
3065 417a6e49 2020-09-26 stsp goto done;
3066 417a6e49 2020-09-26 stsp }
3067 417a6e49 2020-09-26 stsp if (pcommit) {
3068 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id1, repo,
3069 417a6e49 2020-09-26 stsp qid->id, path);
3070 417a6e49 2020-09-26 stsp if (err) {
3071 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3072 417a6e49 2020-09-26 stsp free(obj_id2);
3073 417a6e49 2020-09-26 stsp goto done;
3074 417a6e49 2020-09-26 stsp }
3075 417a6e49 2020-09-26 stsp } else {
3076 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3077 417a6e49 2020-09-26 stsp if (err) {
3078 417a6e49 2020-09-26 stsp free(obj_id2);
3079 417a6e49 2020-09-26 stsp goto done;
3080 417a6e49 2020-09-26 stsp }
3081 417a6e49 2020-09-26 stsp }
3082 417a6e49 2020-09-26 stsp }
3083 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3084 417a6e49 2020-09-26 stsp if (err) {
3085 417a6e49 2020-09-26 stsp free(obj_id2);
3086 417a6e49 2020-09-26 stsp goto done;
3087 417a6e49 2020-09-26 stsp }
3088 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3089 417a6e49 2020-09-26 stsp switch (obj_type) {
3090 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
3091 417a6e49 2020-09-26 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3092 417a6e49 2020-09-26 stsp 0, repo);
3093 417a6e49 2020-09-26 stsp break;
3094 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
3095 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3096 417a6e49 2020-09-26 stsp 0, repo);
3097 417a6e49 2020-09-26 stsp break;
3098 417a6e49 2020-09-26 stsp default:
3099 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3100 417a6e49 2020-09-26 stsp break;
3101 417a6e49 2020-09-26 stsp }
3102 417a6e49 2020-09-26 stsp free(obj_id1);
3103 417a6e49 2020-09-26 stsp free(obj_id2);
3104 417a6e49 2020-09-26 stsp } else {
3105 417a6e49 2020-09-26 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3106 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3107 417a6e49 2020-09-26 stsp if (err)
3108 417a6e49 2020-09-26 stsp goto done;
3109 417a6e49 2020-09-26 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3110 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3111 417a6e49 2020-09-26 stsp if (err)
3112 417a6e49 2020-09-26 stsp goto done;
3113 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3114 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3115 417a6e49 2020-09-26 stsp }
3116 417a6e49 2020-09-26 stsp done:
3117 417a6e49 2020-09-26 stsp free(id_str1);
3118 417a6e49 2020-09-26 stsp free(id_str2);
3119 417a6e49 2020-09-26 stsp if (pcommit)
3120 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3121 417a6e49 2020-09-26 stsp return err;
3122 417a6e49 2020-09-26 stsp }
3123 417a6e49 2020-09-26 stsp
3124 417a6e49 2020-09-26 stsp static char *
3125 417a6e49 2020-09-26 stsp get_datestr(time_t *time, char *datebuf)
3126 417a6e49 2020-09-26 stsp {
3127 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
3128 417a6e49 2020-09-26 stsp char *p, *s;
3129 417a6e49 2020-09-26 stsp
3130 417a6e49 2020-09-26 stsp tm = gmtime_r(time, &mytm);
3131 417a6e49 2020-09-26 stsp if (tm == NULL)
3132 417a6e49 2020-09-26 stsp return NULL;
3133 417a6e49 2020-09-26 stsp s = asctime_r(tm, datebuf);
3134 417a6e49 2020-09-26 stsp if (s == NULL)
3135 417a6e49 2020-09-26 stsp return NULL;
3136 417a6e49 2020-09-26 stsp p = strchr(s, '\n');
3137 417a6e49 2020-09-26 stsp if (p)
3138 417a6e49 2020-09-26 stsp *p = '\0';
3139 417a6e49 2020-09-26 stsp return s;
3140 417a6e49 2020-09-26 stsp }
3141 417a6e49 2020-09-26 stsp
3142 417a6e49 2020-09-26 stsp static const struct got_error *
3143 417a6e49 2020-09-26 stsp match_logmsg(int *have_match, struct got_object_id *id,
3144 417a6e49 2020-09-26 stsp struct got_commit_object *commit, regex_t *regex)
3145 417a6e49 2020-09-26 stsp {
3146 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3147 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3148 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
3149 417a6e49 2020-09-26 stsp
3150 417a6e49 2020-09-26 stsp *have_match = 0;
3151 417a6e49 2020-09-26 stsp
3152 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3153 417a6e49 2020-09-26 stsp if (err)
3154 417a6e49 2020-09-26 stsp return err;
3155 417a6e49 2020-09-26 stsp
3156 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3157 417a6e49 2020-09-26 stsp if (err)
3158 417a6e49 2020-09-26 stsp goto done;
3159 417a6e49 2020-09-26 stsp
3160 417a6e49 2020-09-26 stsp if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3161 417a6e49 2020-09-26 stsp *have_match = 1;
3162 417a6e49 2020-09-26 stsp done:
3163 417a6e49 2020-09-26 stsp free(id_str);
3164 417a6e49 2020-09-26 stsp free(logmsg);
3165 417a6e49 2020-09-26 stsp return err;
3166 417a6e49 2020-09-26 stsp }
3167 417a6e49 2020-09-26 stsp
3168 417a6e49 2020-09-26 stsp static void
3169 417a6e49 2020-09-26 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3170 417a6e49 2020-09-26 stsp regex_t *regex)
3171 417a6e49 2020-09-26 stsp {
3172 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3173 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3174 417a6e49 2020-09-26 stsp
3175 417a6e49 2020-09-26 stsp *have_match = 0;
3176 417a6e49 2020-09-26 stsp
3177 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3178 417a6e49 2020-09-26 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3179 417a6e49 2020-09-26 stsp *have_match = 1;
3180 417a6e49 2020-09-26 stsp break;
3181 417a6e49 2020-09-26 stsp }
3182 417a6e49 2020-09-26 stsp }
3183 417a6e49 2020-09-26 stsp }
3184 417a6e49 2020-09-26 stsp
3185 417a6e49 2020-09-26 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3186 417a6e49 2020-09-26 stsp
3187 417a6e49 2020-09-26 stsp static const struct got_error *
3188 417a6e49 2020-09-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3189 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path,
3190 417a6e49 2020-09-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3191 417a6e49 2020-09-26 stsp int diff_context, struct got_reflist_head *refs)
3192 417a6e49 2020-09-26 stsp {
3193 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3194 417a6e49 2020-09-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3195 417a6e49 2020-09-26 stsp char datebuf[26];
3196 417a6e49 2020-09-26 stsp time_t committer_time;
3197 417a6e49 2020-09-26 stsp const char *author, *committer;
3198 417a6e49 2020-09-26 stsp char *refs_str = NULL;
3199 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
3200 417a6e49 2020-09-26 stsp
3201 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3202 417a6e49 2020-09-26 stsp char *s;
3203 417a6e49 2020-09-26 stsp const char *name;
3204 417a6e49 2020-09-26 stsp struct got_tag_object *tag = NULL;
3205 417a6e49 2020-09-26 stsp struct got_object_id *ref_id;
3206 417a6e49 2020-09-26 stsp int cmp;
3207 417a6e49 2020-09-26 stsp
3208 417a6e49 2020-09-26 stsp name = got_ref_get_name(re->ref);
3209 417a6e49 2020-09-26 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3210 417a6e49 2020-09-26 stsp continue;
3211 417a6e49 2020-09-26 stsp if (strncmp(name, "refs/", 5) == 0)
3212 417a6e49 2020-09-26 stsp name += 5;
3213 417a6e49 2020-09-26 stsp if (strncmp(name, "got/", 4) == 0)
3214 417a6e49 2020-09-26 stsp continue;
3215 417a6e49 2020-09-26 stsp if (strncmp(name, "heads/", 6) == 0)
3216 417a6e49 2020-09-26 stsp name += 6;
3217 417a6e49 2020-09-26 stsp if (strncmp(name, "remotes/", 8) == 0) {
3218 417a6e49 2020-09-26 stsp name += 8;
3219 417a6e49 2020-09-26 stsp s = strstr(name, "/" GOT_REF_HEAD);
3220 417a6e49 2020-09-26 stsp if (s != NULL && s[strlen(s)] == '\0')
3221 417a6e49 2020-09-26 stsp continue;
3222 417a6e49 2020-09-26 stsp }
3223 417a6e49 2020-09-26 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3224 417a6e49 2020-09-26 stsp if (err)
3225 417a6e49 2020-09-26 stsp return err;
3226 417a6e49 2020-09-26 stsp if (strncmp(name, "tags/", 5) == 0) {
3227 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3228 417a6e49 2020-09-26 stsp if (err) {
3229 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3230 417a6e49 2020-09-26 stsp free(ref_id);
3231 417a6e49 2020-09-26 stsp return err;
3232 417a6e49 2020-09-26 stsp }
3233 417a6e49 2020-09-26 stsp /* Ref points at something other than a tag. */
3234 417a6e49 2020-09-26 stsp err = NULL;
3235 417a6e49 2020-09-26 stsp tag = NULL;
3236 417a6e49 2020-09-26 stsp }
3237 417a6e49 2020-09-26 stsp }
3238 417a6e49 2020-09-26 stsp cmp = got_object_id_cmp(tag ?
3239 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3240 417a6e49 2020-09-26 stsp free(ref_id);
3241 417a6e49 2020-09-26 stsp if (tag)
3242 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3243 417a6e49 2020-09-26 stsp if (cmp != 0)
3244 417a6e49 2020-09-26 stsp continue;
3245 417a6e49 2020-09-26 stsp s = refs_str;
3246 417a6e49 2020-09-26 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3247 417a6e49 2020-09-26 stsp name) == -1) {
3248 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3249 417a6e49 2020-09-26 stsp free(s);
3250 417a6e49 2020-09-26 stsp return err;
3251 417a6e49 2020-09-26 stsp }
3252 417a6e49 2020-09-26 stsp free(s);
3253 417a6e49 2020-09-26 stsp }
3254 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3255 417a6e49 2020-09-26 stsp if (err)
3256 417a6e49 2020-09-26 stsp return err;
3257 417a6e49 2020-09-26 stsp
3258 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
3259 417a6e49 2020-09-26 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3260 417a6e49 2020-09-26 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3261 417a6e49 2020-09-26 stsp free(id_str);
3262 417a6e49 2020-09-26 stsp id_str = NULL;
3263 417a6e49 2020-09-26 stsp free(refs_str);
3264 417a6e49 2020-09-26 stsp refs_str = NULL;
3265 417a6e49 2020-09-26 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3266 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
3267 417a6e49 2020-09-26 stsp datestr = get_datestr(&committer_time, datebuf);
3268 417a6e49 2020-09-26 stsp if (datestr)
3269 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
3270 417a6e49 2020-09-26 stsp author = got_object_commit_get_author(commit);
3271 417a6e49 2020-09-26 stsp committer = got_object_commit_get_committer(commit);
3272 417a6e49 2020-09-26 stsp if (strcmp(author, committer) != 0)
3273 417a6e49 2020-09-26 stsp printf("via: %s\n", committer);
3274 417a6e49 2020-09-26 stsp if (got_object_commit_get_nparents(commit) > 1) {
3275 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
3276 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3277 417a6e49 2020-09-26 stsp int n = 1;
3278 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3279 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3280 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
3281 417a6e49 2020-09-26 stsp if (err)
3282 417a6e49 2020-09-26 stsp return err;
3283 417a6e49 2020-09-26 stsp printf("parent %d: %s\n", n++, id_str);
3284 417a6e49 2020-09-26 stsp free(id_str);
3285 417a6e49 2020-09-26 stsp }
3286 417a6e49 2020-09-26 stsp }
3287 417a6e49 2020-09-26 stsp
3288 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3289 417a6e49 2020-09-26 stsp if (err)
3290 417a6e49 2020-09-26 stsp return err;
3291 417a6e49 2020-09-26 stsp
3292 417a6e49 2020-09-26 stsp logmsg = logmsg0;
3293 417a6e49 2020-09-26 stsp do {
3294 417a6e49 2020-09-26 stsp line = strsep(&logmsg, "\n");
3295 417a6e49 2020-09-26 stsp if (line)
3296 417a6e49 2020-09-26 stsp printf(" %s\n", line);
3297 417a6e49 2020-09-26 stsp } while (line);
3298 417a6e49 2020-09-26 stsp free(logmsg0);
3299 417a6e49 2020-09-26 stsp
3300 417a6e49 2020-09-26 stsp if (changed_paths) {
3301 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3302 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3303 417a6e49 2020-09-26 stsp struct got_diff_changed_path *cp = pe->data;
3304 417a6e49 2020-09-26 stsp printf(" %c %s\n", cp->status, pe->path);
3305 417a6e49 2020-09-26 stsp }
3306 417a6e49 2020-09-26 stsp printf("\n");
3307 417a6e49 2020-09-26 stsp }
3308 417a6e49 2020-09-26 stsp if (show_patch) {
3309 417a6e49 2020-09-26 stsp err = print_patch(commit, id, path, diff_context, repo);
3310 417a6e49 2020-09-26 stsp if (err == 0)
3311 417a6e49 2020-09-26 stsp printf("\n");
3312 417a6e49 2020-09-26 stsp }
3313 417a6e49 2020-09-26 stsp
3314 417a6e49 2020-09-26 stsp if (fflush(stdout) != 0 && err == NULL)
3315 417a6e49 2020-09-26 stsp err = got_error_from_errno("fflush");
3316 417a6e49 2020-09-26 stsp return err;
3317 417a6e49 2020-09-26 stsp }
3318 417a6e49 2020-09-26 stsp
3319 417a6e49 2020-09-26 stsp static const struct got_error *
3320 417a6e49 2020-09-26 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3321 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3322 417a6e49 2020-09-26 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3323 417a6e49 2020-09-26 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3324 417a6e49 2020-09-26 stsp {
3325 417a6e49 2020-09-26 stsp const struct got_error *err;
3326 417a6e49 2020-09-26 stsp struct got_commit_graph *graph;
3327 417a6e49 2020-09-26 stsp regex_t regex;
3328 417a6e49 2020-09-26 stsp int have_match;
3329 417a6e49 2020-09-26 stsp struct got_object_id_queue reversed_commits;
3330 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3331 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
3332 417a6e49 2020-09-26 stsp struct got_pathlist_head changed_paths;
3333 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3334 417a6e49 2020-09-26 stsp
3335 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&reversed_commits);
3336 417a6e49 2020-09-26 stsp TAILQ_INIT(&changed_paths);
3337 417a6e49 2020-09-26 stsp
3338 417a6e49 2020-09-26 stsp if (search_pattern && regcomp(&regex, search_pattern,
3339 417a6e49 2020-09-26 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3340 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_REGEX, search_pattern);
3341 417a6e49 2020-09-26 stsp
3342 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3343 417a6e49 2020-09-26 stsp if (err)
3344 417a6e49 2020-09-26 stsp return err;
3345 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3346 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3347 417a6e49 2020-09-26 stsp if (err)
3348 417a6e49 2020-09-26 stsp goto done;
3349 417a6e49 2020-09-26 stsp for (;;) {
3350 417a6e49 2020-09-26 stsp struct got_object_id *id;
3351 417a6e49 2020-09-26 stsp
3352 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
3353 417a6e49 2020-09-26 stsp break;
3354 417a6e49 2020-09-26 stsp
3355 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3356 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3357 417a6e49 2020-09-26 stsp if (err) {
3358 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3359 417a6e49 2020-09-26 stsp err = NULL;
3360 417a6e49 2020-09-26 stsp break;
3361 417a6e49 2020-09-26 stsp }
3362 417a6e49 2020-09-26 stsp if (id == NULL)
3363 417a6e49 2020-09-26 stsp break;
3364 417a6e49 2020-09-26 stsp
3365 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
3366 417a6e49 2020-09-26 stsp if (err)
3367 417a6e49 2020-09-26 stsp break;
3368 417a6e49 2020-09-26 stsp
3369 417a6e49 2020-09-26 stsp if (show_changed_paths && !reverse_display_order) {
3370 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths, commit, repo);
3371 417a6e49 2020-09-26 stsp if (err)
3372 417a6e49 2020-09-26 stsp break;
3373 417a6e49 2020-09-26 stsp }
3374 417a6e49 2020-09-26 stsp
3375 417a6e49 2020-09-26 stsp if (search_pattern) {
3376 417a6e49 2020-09-26 stsp err = match_logmsg(&have_match, id, commit, &regex);
3377 417a6e49 2020-09-26 stsp if (err) {
3378 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3379 417a6e49 2020-09-26 stsp break;
3380 417a6e49 2020-09-26 stsp }
3381 417a6e49 2020-09-26 stsp if (have_match == 0 && show_changed_paths)
3382 417a6e49 2020-09-26 stsp match_changed_paths(&have_match,
3383 417a6e49 2020-09-26 stsp &changed_paths, &regex);
3384 417a6e49 2020-09-26 stsp if (have_match == 0) {
3385 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3386 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3387 417a6e49 2020-09-26 stsp free((char *)pe->path);
3388 417a6e49 2020-09-26 stsp free(pe->data);
3389 417a6e49 2020-09-26 stsp }
3390 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3391 417a6e49 2020-09-26 stsp continue;
3392 417a6e49 2020-09-26 stsp }
3393 417a6e49 2020-09-26 stsp }
3394 417a6e49 2020-09-26 stsp
3395 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3396 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, id);
3397 417a6e49 2020-09-26 stsp if (err)
3398 417a6e49 2020-09-26 stsp break;
3399 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3400 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3401 417a6e49 2020-09-26 stsp } else {
3402 417a6e49 2020-09-26 stsp err = print_commit(commit, id, repo, path,
3403 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3404 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3405 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3406 417a6e49 2020-09-26 stsp if (err)
3407 417a6e49 2020-09-26 stsp break;
3408 417a6e49 2020-09-26 stsp }
3409 417a6e49 2020-09-26 stsp if ((limit && --limit == 0) ||
3410 417a6e49 2020-09-26 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3411 417a6e49 2020-09-26 stsp break;
3412 417a6e49 2020-09-26 stsp
3413 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3414 417a6e49 2020-09-26 stsp free((char *)pe->path);
3415 417a6e49 2020-09-26 stsp free(pe->data);
3416 417a6e49 2020-09-26 stsp }
3417 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3418 417a6e49 2020-09-26 stsp }
3419 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3420 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3421 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3422 417a6e49 2020-09-26 stsp if (err)
3423 417a6e49 2020-09-26 stsp break;
3424 417a6e49 2020-09-26 stsp if (show_changed_paths) {
3425 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths,
3426 417a6e49 2020-09-26 stsp commit, repo);
3427 417a6e49 2020-09-26 stsp if (err)
3428 417a6e49 2020-09-26 stsp break;
3429 417a6e49 2020-09-26 stsp }
3430 417a6e49 2020-09-26 stsp err = print_commit(commit, qid->id, repo, path,
3431 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3432 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3433 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3434 417a6e49 2020-09-26 stsp if (err)
3435 417a6e49 2020-09-26 stsp break;
3436 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3437 417a6e49 2020-09-26 stsp free((char *)pe->path);
3438 417a6e49 2020-09-26 stsp free(pe->data);
3439 417a6e49 2020-09-26 stsp }
3440 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3441 417a6e49 2020-09-26 stsp }
3442 417a6e49 2020-09-26 stsp }
3443 417a6e49 2020-09-26 stsp done:
3444 417a6e49 2020-09-26 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3445 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3446 417a6e49 2020-09-26 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3447 417a6e49 2020-09-26 stsp got_object_qid_free(qid);
3448 417a6e49 2020-09-26 stsp }
3449 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3450 417a6e49 2020-09-26 stsp free((char *)pe->path);
3451 417a6e49 2020-09-26 stsp free(pe->data);
3452 417a6e49 2020-09-26 stsp }
3453 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3454 417a6e49 2020-09-26 stsp if (search_pattern)
3455 417a6e49 2020-09-26 stsp regfree(&regex);
3456 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
3457 417a6e49 2020-09-26 stsp return err;
3458 417a6e49 2020-09-26 stsp }
3459 417a6e49 2020-09-26 stsp
3460 417a6e49 2020-09-26 stsp __dead static void
3461 417a6e49 2020-09-26 stsp usage_log(void)
3462 417a6e49 2020-09-26 stsp {
3463 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3464 417a6e49 2020-09-26 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3465 417a6e49 2020-09-26 stsp "[-R] [path]\n", getprogname());
3466 417a6e49 2020-09-26 stsp exit(1);
3467 417a6e49 2020-09-26 stsp }
3468 417a6e49 2020-09-26 stsp
3469 417a6e49 2020-09-26 stsp static int
3470 417a6e49 2020-09-26 stsp get_default_log_limit(void)
3471 417a6e49 2020-09-26 stsp {
3472 417a6e49 2020-09-26 stsp const char *got_default_log_limit;
3473 417a6e49 2020-09-26 stsp long long n;
3474 417a6e49 2020-09-26 stsp const char *errstr;
3475 417a6e49 2020-09-26 stsp
3476 417a6e49 2020-09-26 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3477 417a6e49 2020-09-26 stsp if (got_default_log_limit == NULL)
3478 417a6e49 2020-09-26 stsp return 0;
3479 417a6e49 2020-09-26 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3480 417a6e49 2020-09-26 stsp if (errstr != NULL)
3481 417a6e49 2020-09-26 stsp return 0;
3482 417a6e49 2020-09-26 stsp return n;
3483 417a6e49 2020-09-26 stsp }
3484 417a6e49 2020-09-26 stsp
3485 417a6e49 2020-09-26 stsp static const struct got_error *
3486 417a6e49 2020-09-26 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3487 417a6e49 2020-09-26 stsp struct got_repository *repo)
3488 417a6e49 2020-09-26 stsp {
3489 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3490 417a6e49 2020-09-26 stsp struct got_reference *ref;
3491 417a6e49 2020-09-26 stsp
3492 417a6e49 2020-09-26 stsp *id = NULL;
3493 417a6e49 2020-09-26 stsp
3494 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3495 417a6e49 2020-09-26 stsp if (err == NULL) {
3496 417a6e49 2020-09-26 stsp int obj_type;
3497 417a6e49 2020-09-26 stsp err = got_ref_resolve(id, repo, ref);
3498 417a6e49 2020-09-26 stsp got_ref_close(ref);
3499 417a6e49 2020-09-26 stsp if (err)
3500 417a6e49 2020-09-26 stsp return err;
3501 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, *id);
3502 417a6e49 2020-09-26 stsp if (err)
3503 417a6e49 2020-09-26 stsp return err;
3504 417a6e49 2020-09-26 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3505 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
3506 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, *id);
3507 417a6e49 2020-09-26 stsp if (err)
3508 417a6e49 2020-09-26 stsp return err;
3509 417a6e49 2020-09-26 stsp if (got_object_tag_get_object_type(tag) !=
3510 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT) {
3511 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3512 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3513 417a6e49 2020-09-26 stsp }
3514 417a6e49 2020-09-26 stsp free(*id);
3515 417a6e49 2020-09-26 stsp *id = got_object_id_dup(
3516 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
3517 417a6e49 2020-09-26 stsp if (*id == NULL)
3518 417a6e49 2020-09-26 stsp err = got_error_from_errno(
3519 417a6e49 2020-09-26 stsp "got_object_id_dup");
3520 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3521 417a6e49 2020-09-26 stsp if (err)
3522 417a6e49 2020-09-26 stsp return err;
3523 417a6e49 2020-09-26 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3524 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3525 417a6e49 2020-09-26 stsp } else {
3526 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3527 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
3528 417a6e49 2020-09-26 stsp }
3529 417a6e49 2020-09-26 stsp
3530 417a6e49 2020-09-26 stsp return err;
3531 417a6e49 2020-09-26 stsp }
3532 417a6e49 2020-09-26 stsp
3533 417a6e49 2020-09-26 stsp static const struct got_error *
3534 417a6e49 2020-09-26 stsp cmd_log(int argc, char *argv[])
3535 417a6e49 2020-09-26 stsp {
3536 417a6e49 2020-09-26 stsp const struct got_error *error;
3537 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3538 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3539 417a6e49 2020-09-26 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3540 417a6e49 2020-09-26 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3541 417a6e49 2020-09-26 stsp const char *start_commit = NULL, *end_commit = NULL;
3542 417a6e49 2020-09-26 stsp const char *search_pattern = NULL;
3543 417a6e49 2020-09-26 stsp int diff_context = -1, ch;
3544 417a6e49 2020-09-26 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3545 417a6e49 2020-09-26 stsp int reverse_display_order = 0;
3546 417a6e49 2020-09-26 stsp const char *errstr;
3547 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
3548 417a6e49 2020-09-26 stsp
3549 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
3550 417a6e49 2020-09-26 stsp
3551 417a6e49 2020-09-26 stsp #ifndef PROFILE
3552 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3553 417a6e49 2020-09-26 stsp NULL)
3554 417a6e49 2020-09-26 stsp == -1)
3555 417a6e49 2020-09-26 stsp err(1, "pledge");
3556 417a6e49 2020-09-26 stsp #endif
3557 417a6e49 2020-09-26 stsp
3558 417a6e49 2020-09-26 stsp limit = get_default_log_limit();
3559 417a6e49 2020-09-26 stsp
3560 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3561 417a6e49 2020-09-26 stsp switch (ch) {
3562 417a6e49 2020-09-26 stsp case 'p':
3563 417a6e49 2020-09-26 stsp show_patch = 1;
3564 417a6e49 2020-09-26 stsp break;
3565 417a6e49 2020-09-26 stsp case 'P':
3566 417a6e49 2020-09-26 stsp show_changed_paths = 1;
3567 417a6e49 2020-09-26 stsp break;
3568 417a6e49 2020-09-26 stsp case 'c':
3569 417a6e49 2020-09-26 stsp start_commit = optarg;
3570 417a6e49 2020-09-26 stsp break;
3571 417a6e49 2020-09-26 stsp case 'C':
3572 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3573 417a6e49 2020-09-26 stsp &errstr);
3574 417a6e49 2020-09-26 stsp if (errstr != NULL)
3575 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3576 417a6e49 2020-09-26 stsp break;
3577 417a6e49 2020-09-26 stsp case 'l':
3578 417a6e49 2020-09-26 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3579 417a6e49 2020-09-26 stsp if (errstr != NULL)
3580 417a6e49 2020-09-26 stsp err(1, "-l option %s", errstr);
3581 417a6e49 2020-09-26 stsp break;
3582 417a6e49 2020-09-26 stsp case 'b':
3583 417a6e49 2020-09-26 stsp log_branches = 1;
3584 417a6e49 2020-09-26 stsp break;
3585 417a6e49 2020-09-26 stsp case 'r':
3586 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3587 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3588 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3589 417a6e49 2020-09-26 stsp optarg);
3590 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3591 417a6e49 2020-09-26 stsp break;
3592 417a6e49 2020-09-26 stsp case 'R':
3593 417a6e49 2020-09-26 stsp reverse_display_order = 1;
3594 417a6e49 2020-09-26 stsp break;
3595 417a6e49 2020-09-26 stsp case 's':
3596 417a6e49 2020-09-26 stsp search_pattern = optarg;
3597 417a6e49 2020-09-26 stsp break;
3598 417a6e49 2020-09-26 stsp case 'x':
3599 417a6e49 2020-09-26 stsp end_commit = optarg;
3600 417a6e49 2020-09-26 stsp break;
3601 417a6e49 2020-09-26 stsp default:
3602 417a6e49 2020-09-26 stsp usage_log();
3603 417a6e49 2020-09-26 stsp /* NOTREACHED */
3604 417a6e49 2020-09-26 stsp }
3605 417a6e49 2020-09-26 stsp }
3606 417a6e49 2020-09-26 stsp
3607 417a6e49 2020-09-26 stsp argc -= optind;
3608 417a6e49 2020-09-26 stsp argv += optind;
3609 417a6e49 2020-09-26 stsp
3610 417a6e49 2020-09-26 stsp if (diff_context == -1)
3611 417a6e49 2020-09-26 stsp diff_context = 3;
3612 417a6e49 2020-09-26 stsp else if (!show_patch)
3613 417a6e49 2020-09-26 stsp errx(1, "-C requires -p");
3614 417a6e49 2020-09-26 stsp
3615 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3616 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3617 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
3618 417a6e49 2020-09-26 stsp goto done;
3619 417a6e49 2020-09-26 stsp }
3620 417a6e49 2020-09-26 stsp
3621 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3622 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
3623 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3624 417a6e49 2020-09-26 stsp goto done;
3625 417a6e49 2020-09-26 stsp error = NULL;
3626 417a6e49 2020-09-26 stsp }
3627 417a6e49 2020-09-26 stsp
3628 417a6e49 2020-09-26 stsp if (argc == 0) {
3629 417a6e49 2020-09-26 stsp path = strdup("");
3630 417a6e49 2020-09-26 stsp if (path == NULL) {
3631 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3632 417a6e49 2020-09-26 stsp goto done;
3633 417a6e49 2020-09-26 stsp }
3634 417a6e49 2020-09-26 stsp } else if (argc == 1) {
3635 417a6e49 2020-09-26 stsp if (worktree) {
3636 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
3637 417a6e49 2020-09-26 stsp argv[0]);
3638 417a6e49 2020-09-26 stsp if (error)
3639 417a6e49 2020-09-26 stsp goto done;
3640 417a6e49 2020-09-26 stsp } else {
3641 417a6e49 2020-09-26 stsp path = strdup(argv[0]);
3642 417a6e49 2020-09-26 stsp if (path == NULL) {
3643 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3644 417a6e49 2020-09-26 stsp goto done;
3645 417a6e49 2020-09-26 stsp }
3646 417a6e49 2020-09-26 stsp }
3647 417a6e49 2020-09-26 stsp } else
3648 417a6e49 2020-09-26 stsp usage_log();
3649 417a6e49 2020-09-26 stsp
3650 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3651 417a6e49 2020-09-26 stsp repo_path = worktree ?
3652 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3653 417a6e49 2020-09-26 stsp }
3654 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3655 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3656 417a6e49 2020-09-26 stsp goto done;
3657 417a6e49 2020-09-26 stsp }
3658 417a6e49 2020-09-26 stsp
3659 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
3660 417a6e49 2020-09-26 stsp if (error != NULL)
3661 417a6e49 2020-09-26 stsp goto done;
3662 417a6e49 2020-09-26 stsp
3663 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3664 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3665 417a6e49 2020-09-26 stsp if (error)
3666 417a6e49 2020-09-26 stsp goto done;
3667 417a6e49 2020-09-26 stsp
3668 417a6e49 2020-09-26 stsp if (start_commit == NULL) {
3669 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
3670 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
3671 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
3672 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3673 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
3674 417a6e49 2020-09-26 stsp if (error != NULL)
3675 417a6e49 2020-09-26 stsp goto done;
3676 417a6e49 2020-09-26 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3677 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
3678 417a6e49 2020-09-26 stsp if (error != NULL)
3679 417a6e49 2020-09-26 stsp goto done;
3680 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
3681 417a6e49 2020-09-26 stsp start_id);
3682 417a6e49 2020-09-26 stsp if (error != NULL)
3683 417a6e49 2020-09-26 stsp goto done;
3684 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3685 417a6e49 2020-09-26 stsp } else {
3686 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3687 417a6e49 2020-09-26 stsp if (error != NULL)
3688 417a6e49 2020-09-26 stsp goto done;
3689 417a6e49 2020-09-26 stsp }
3690 417a6e49 2020-09-26 stsp if (end_commit != NULL) {
3691 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3692 417a6e49 2020-09-26 stsp if (error != NULL)
3693 417a6e49 2020-09-26 stsp goto done;
3694 417a6e49 2020-09-26 stsp }
3695 417a6e49 2020-09-26 stsp
3696 417a6e49 2020-09-26 stsp if (worktree) {
3697 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3698 417a6e49 2020-09-26 stsp char *p;
3699 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s", prefix,
3700 417a6e49 2020-09-26 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3701 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
3702 417a6e49 2020-09-26 stsp goto done;
3703 417a6e49 2020-09-26 stsp }
3704 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3705 417a6e49 2020-09-26 stsp free(p);
3706 417a6e49 2020-09-26 stsp } else
3707 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3708 417a6e49 2020-09-26 stsp if (error != NULL)
3709 417a6e49 2020-09-26 stsp goto done;
3710 417a6e49 2020-09-26 stsp if (in_repo_path) {
3711 417a6e49 2020-09-26 stsp free(path);
3712 417a6e49 2020-09-26 stsp path = in_repo_path;
3713 417a6e49 2020-09-26 stsp }
3714 417a6e49 2020-09-26 stsp
3715 417a6e49 2020-09-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3716 417a6e49 2020-09-26 stsp if (error)
3717 417a6e49 2020-09-26 stsp goto done;
3718 417a6e49 2020-09-26 stsp
3719 417a6e49 2020-09-26 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3720 417a6e49 2020-09-26 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3721 417a6e49 2020-09-26 stsp reverse_display_order, &refs);
3722 417a6e49 2020-09-26 stsp done:
3723 417a6e49 2020-09-26 stsp free(path);
3724 417a6e49 2020-09-26 stsp free(repo_path);
3725 417a6e49 2020-09-26 stsp free(cwd);
3726 417a6e49 2020-09-26 stsp if (worktree)
3727 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
3728 417a6e49 2020-09-26 stsp if (repo) {
3729 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
3730 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
3731 417a6e49 2020-09-26 stsp if (error == NULL)
3732 417a6e49 2020-09-26 stsp error = repo_error;
3733 417a6e49 2020-09-26 stsp }
3734 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
3735 417a6e49 2020-09-26 stsp return error;
3736 417a6e49 2020-09-26 stsp }
3737 417a6e49 2020-09-26 stsp
3738 417a6e49 2020-09-26 stsp __dead static void
3739 417a6e49 2020-09-26 stsp usage_diff(void)
3740 417a6e49 2020-09-26 stsp {
3741 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3742 417a6e49 2020-09-26 stsp "[-w] [object1 object2 | path]\n", getprogname());
3743 417a6e49 2020-09-26 stsp exit(1);
3744 417a6e49 2020-09-26 stsp }
3745 417a6e49 2020-09-26 stsp
3746 417a6e49 2020-09-26 stsp struct print_diff_arg {
3747 417a6e49 2020-09-26 stsp struct got_repository *repo;
3748 417a6e49 2020-09-26 stsp struct got_worktree *worktree;
3749 417a6e49 2020-09-26 stsp int diff_context;
3750 417a6e49 2020-09-26 stsp const char *id_str;
3751 417a6e49 2020-09-26 stsp int header_shown;
3752 417a6e49 2020-09-26 stsp int diff_staged;
3753 417a6e49 2020-09-26 stsp int ignore_whitespace;
3754 417a6e49 2020-09-26 stsp };
3755 417a6e49 2020-09-26 stsp
3756 417a6e49 2020-09-26 stsp /*
3757 417a6e49 2020-09-26 stsp * Create a file which contains the target path of a symlink so we can feed
3758 417a6e49 2020-09-26 stsp * it as content to the diff engine.
3759 417a6e49 2020-09-26 stsp */
3760 417a6e49 2020-09-26 stsp static const struct got_error *
3761 417a6e49 2020-09-26 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3762 417a6e49 2020-09-26 stsp const char *abspath)
3763 417a6e49 2020-09-26 stsp {
3764 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3765 417a6e49 2020-09-26 stsp char target_path[PATH_MAX];
3766 417a6e49 2020-09-26 stsp ssize_t target_len, outlen;
3767 417a6e49 2020-09-26 stsp
3768 417a6e49 2020-09-26 stsp *fd = -1;
3769 417a6e49 2020-09-26 stsp
3770 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3771 417a6e49 2020-09-26 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3772 417a6e49 2020-09-26 stsp if (target_len == -1)
3773 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlinkat", abspath);
3774 417a6e49 2020-09-26 stsp } else {
3775 417a6e49 2020-09-26 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3776 417a6e49 2020-09-26 stsp if (target_len == -1)
3777 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlink", abspath);
3778 417a6e49 2020-09-26 stsp }
3779 417a6e49 2020-09-26 stsp
3780 417a6e49 2020-09-26 stsp *fd = got_opentempfd();
3781 417a6e49 2020-09-26 stsp if (*fd == -1)
3782 417a6e49 2020-09-26 stsp return got_error_from_errno("got_opentempfd");
3783 417a6e49 2020-09-26 stsp
3784 417a6e49 2020-09-26 stsp outlen = write(*fd, target_path, target_len);
3785 417a6e49 2020-09-26 stsp if (outlen == -1) {
3786 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_opentempfd");
3787 417a6e49 2020-09-26 stsp goto done;
3788 417a6e49 2020-09-26 stsp }
3789 417a6e49 2020-09-26 stsp
3790 417a6e49 2020-09-26 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3791 417a6e49 2020-09-26 stsp err = got_error_from_errno2("lseek", abspath);
3792 417a6e49 2020-09-26 stsp goto done;
3793 417a6e49 2020-09-26 stsp }
3794 417a6e49 2020-09-26 stsp done:
3795 417a6e49 2020-09-26 stsp if (err) {
3796 417a6e49 2020-09-26 stsp close(*fd);
3797 417a6e49 2020-09-26 stsp *fd = -1;
3798 417a6e49 2020-09-26 stsp }
3799 417a6e49 2020-09-26 stsp return err;
3800 417a6e49 2020-09-26 stsp }
3801 417a6e49 2020-09-26 stsp
3802 417a6e49 2020-09-26 stsp static const struct got_error *
3803 417a6e49 2020-09-26 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3804 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
3805 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3806 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
3807 417a6e49 2020-09-26 stsp {
3808 417a6e49 2020-09-26 stsp struct print_diff_arg *a = arg;
3809 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3810 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL;
3811 417a6e49 2020-09-26 stsp int fd = -1;
3812 417a6e49 2020-09-26 stsp FILE *f2 = NULL;
3813 417a6e49 2020-09-26 stsp char *abspath = NULL, *label1 = NULL;
3814 417a6e49 2020-09-26 stsp struct stat sb;
3815 417a6e49 2020-09-26 stsp
3816 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3817 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_MODIFY &&
3818 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_ADD &&
3819 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
3820 417a6e49 2020-09-26 stsp return NULL;
3821 417a6e49 2020-09-26 stsp } else {
3822 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_DELETE)
3823 417a6e49 2020-09-26 stsp return NULL;
3824 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
3825 417a6e49 2020-09-26 stsp return got_error_set_errno(ENOENT, path);
3826 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_MODIFY &&
3827 417a6e49 2020-09-26 stsp status != GOT_STATUS_ADD &&
3828 417a6e49 2020-09-26 stsp status != GOT_STATUS_DELETE &&
3829 417a6e49 2020-09-26 stsp status != GOT_STATUS_CONFLICT)
3830 417a6e49 2020-09-26 stsp return NULL;
3831 417a6e49 2020-09-26 stsp }
3832 417a6e49 2020-09-26 stsp
3833 417a6e49 2020-09-26 stsp if (!a->header_shown) {
3834 417a6e49 2020-09-26 stsp printf("diff %s %s%s\n", a->id_str,
3835 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree),
3836 417a6e49 2020-09-26 stsp a->diff_staged ? " (staged changes)" : "");
3837 417a6e49 2020-09-26 stsp a->header_shown = 1;
3838 417a6e49 2020-09-26 stsp }
3839 417a6e49 2020-09-26 stsp
3840 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3841 417a6e49 2020-09-26 stsp const char *label1 = NULL, *label2 = NULL;
3842 417a6e49 2020-09-26 stsp switch (staged_status) {
3843 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
3844 417a6e49 2020-09-26 stsp label1 = path;
3845 417a6e49 2020-09-26 stsp label2 = path;
3846 417a6e49 2020-09-26 stsp break;
3847 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
3848 417a6e49 2020-09-26 stsp label2 = path;
3849 417a6e49 2020-09-26 stsp break;
3850 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
3851 417a6e49 2020-09-26 stsp label1 = path;
3852 417a6e49 2020-09-26 stsp break;
3853 417a6e49 2020-09-26 stsp default:
3854 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_FILE_STATUS);
3855 417a6e49 2020-09-26 stsp }
3856 417a6e49 2020-09-26 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3857 417a6e49 2020-09-26 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3858 417a6e49 2020-09-26 stsp a->repo, stdout);
3859 417a6e49 2020-09-26 stsp }
3860 417a6e49 2020-09-26 stsp
3861 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
3862 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY) {
3863 417a6e49 2020-09-26 stsp char *id_str;
3864 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3865 417a6e49 2020-09-26 stsp 8192);
3866 417a6e49 2020-09-26 stsp if (err)
3867 417a6e49 2020-09-26 stsp goto done;
3868 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
3869 417a6e49 2020-09-26 stsp if (err)
3870 417a6e49 2020-09-26 stsp goto done;
3871 417a6e49 2020-09-26 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3872 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3873 417a6e49 2020-09-26 stsp free(id_str);
3874 417a6e49 2020-09-26 stsp goto done;
3875 417a6e49 2020-09-26 stsp }
3876 417a6e49 2020-09-26 stsp free(id_str);
3877 417a6e49 2020-09-26 stsp } else if (status != GOT_STATUS_ADD) {
3878 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3879 417a6e49 2020-09-26 stsp if (err)
3880 417a6e49 2020-09-26 stsp goto done;
3881 417a6e49 2020-09-26 stsp }
3882 417a6e49 2020-09-26 stsp
3883 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_DELETE) {
3884 417a6e49 2020-09-26 stsp if (asprintf(&abspath, "%s/%s",
3885 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3886 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3887 417a6e49 2020-09-26 stsp goto done;
3888 417a6e49 2020-09-26 stsp }
3889 417a6e49 2020-09-26 stsp
3890 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3891 417a6e49 2020-09-26 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3892 417a6e49 2020-09-26 stsp if (fd == -1) {
3893 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3894 417a6e49 2020-09-26 stsp err = got_error_from_errno2("openat",
3895 417a6e49 2020-09-26 stsp abspath);
3896 417a6e49 2020-09-26 stsp goto done;
3897 417a6e49 2020-09-26 stsp }
3898 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3899 417a6e49 2020-09-26 stsp de_name, abspath);
3900 417a6e49 2020-09-26 stsp if (err)
3901 417a6e49 2020-09-26 stsp goto done;
3902 417a6e49 2020-09-26 stsp }
3903 417a6e49 2020-09-26 stsp } else {
3904 417a6e49 2020-09-26 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3905 417a6e49 2020-09-26 stsp if (fd == -1) {
3906 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3907 417a6e49 2020-09-26 stsp err = got_error_from_errno2("open",
3908 417a6e49 2020-09-26 stsp abspath);
3909 417a6e49 2020-09-26 stsp goto done;
3910 417a6e49 2020-09-26 stsp }
3911 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3912 417a6e49 2020-09-26 stsp de_name, abspath);
3913 417a6e49 2020-09-26 stsp if (err)
3914 417a6e49 2020-09-26 stsp goto done;
3915 417a6e49 2020-09-26 stsp }
3916 417a6e49 2020-09-26 stsp }
3917 417a6e49 2020-09-26 stsp if (fstat(fd, &sb) == -1) {
3918 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fstat", abspath);
3919 417a6e49 2020-09-26 stsp goto done;
3920 417a6e49 2020-09-26 stsp }
3921 417a6e49 2020-09-26 stsp f2 = fdopen(fd, "r");
3922 417a6e49 2020-09-26 stsp if (f2 == NULL) {
3923 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fdopen", abspath);
3924 417a6e49 2020-09-26 stsp goto done;
3925 417a6e49 2020-09-26 stsp }
3926 417a6e49 2020-09-26 stsp fd = -1;
3927 417a6e49 2020-09-26 stsp } else
3928 417a6e49 2020-09-26 stsp sb.st_size = 0;
3929 417a6e49 2020-09-26 stsp
3930 417a6e49 2020-09-26 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3931 417a6e49 2020-09-26 stsp a->diff_context, a->ignore_whitespace, stdout);
3932 417a6e49 2020-09-26 stsp done:
3933 417a6e49 2020-09-26 stsp if (blob1)
3934 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
3935 417a6e49 2020-09-26 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3936 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
3937 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3938 417a6e49 2020-09-26 stsp err = got_error_from_errno("close");
3939 417a6e49 2020-09-26 stsp free(abspath);
3940 417a6e49 2020-09-26 stsp return err;
3941 417a6e49 2020-09-26 stsp }
3942 417a6e49 2020-09-26 stsp
3943 417a6e49 2020-09-26 stsp static const struct got_error *
3944 417a6e49 2020-09-26 stsp cmd_diff(int argc, char *argv[])
3945 417a6e49 2020-09-26 stsp {
3946 417a6e49 2020-09-26 stsp const struct got_error *error;
3947 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3948 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3949 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
3950 417a6e49 2020-09-26 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3951 417a6e49 2020-09-26 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3952 417a6e49 2020-09-26 stsp char *label1 = NULL, *label2 = NULL;
3953 417a6e49 2020-09-26 stsp int type1, type2;
3954 417a6e49 2020-09-26 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3955 417a6e49 2020-09-26 stsp const char *errstr;
3956 417a6e49 2020-09-26 stsp char *path = NULL;
3957 417a6e49 2020-09-26 stsp
3958 417a6e49 2020-09-26 stsp #ifndef PROFILE
3959 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3960 417a6e49 2020-09-26 stsp NULL) == -1)
3961 417a6e49 2020-09-26 stsp err(1, "pledge");
3962 417a6e49 2020-09-26 stsp #endif
3963 417a6e49 2020-09-26 stsp
3964 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3965 417a6e49 2020-09-26 stsp switch (ch) {
3966 417a6e49 2020-09-26 stsp case 'C':
3967 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3968 417a6e49 2020-09-26 stsp &errstr);
3969 417a6e49 2020-09-26 stsp if (errstr != NULL)
3970 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3971 417a6e49 2020-09-26 stsp break;
3972 417a6e49 2020-09-26 stsp case 'r':
3973 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3974 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3975 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3976 417a6e49 2020-09-26 stsp optarg);
3977 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3978 417a6e49 2020-09-26 stsp break;
3979 417a6e49 2020-09-26 stsp case 's':
3980 417a6e49 2020-09-26 stsp diff_staged = 1;
3981 417a6e49 2020-09-26 stsp break;
3982 417a6e49 2020-09-26 stsp case 'w':
3983 417a6e49 2020-09-26 stsp ignore_whitespace = 1;
3984 417a6e49 2020-09-26 stsp break;
3985 417a6e49 2020-09-26 stsp default:
3986 417a6e49 2020-09-26 stsp usage_diff();
3987 417a6e49 2020-09-26 stsp /* NOTREACHED */
3988 417a6e49 2020-09-26 stsp }
3989 417a6e49 2020-09-26 stsp }
3990 417a6e49 2020-09-26 stsp
3991 417a6e49 2020-09-26 stsp argc -= optind;
3992 417a6e49 2020-09-26 stsp argv += optind;
3993 417a6e49 2020-09-26 stsp
3994 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3995 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3996 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
3997 417a6e49 2020-09-26 stsp goto done;
3998 417a6e49 2020-09-26 stsp }
3999 417a6e49 2020-09-26 stsp if (argc <= 1) {
4000 417a6e49 2020-09-26 stsp if (repo_path)
4001 417a6e49 2020-09-26 stsp errx(1,
4002 417a6e49 2020-09-26 stsp "-r option can't be used when diffing a work tree");
4003 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4004 417a6e49 2020-09-26 stsp if (error) {
4005 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4006 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "diff",
4007 417a6e49 2020-09-26 stsp cwd);
4008 417a6e49 2020-09-26 stsp goto done;
4009 417a6e49 2020-09-26 stsp }
4010 417a6e49 2020-09-26 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4011 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4012 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4013 417a6e49 2020-09-26 stsp goto done;
4014 417a6e49 2020-09-26 stsp }
4015 417a6e49 2020-09-26 stsp if (argc == 1) {
4016 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
4017 417a6e49 2020-09-26 stsp argv[0]);
4018 417a6e49 2020-09-26 stsp if (error)
4019 417a6e49 2020-09-26 stsp goto done;
4020 417a6e49 2020-09-26 stsp } else {
4021 417a6e49 2020-09-26 stsp path = strdup("");
4022 417a6e49 2020-09-26 stsp if (path == NULL) {
4023 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4024 417a6e49 2020-09-26 stsp goto done;
4025 417a6e49 2020-09-26 stsp }
4026 417a6e49 2020-09-26 stsp }
4027 417a6e49 2020-09-26 stsp } else if (argc == 2) {
4028 417a6e49 2020-09-26 stsp if (diff_staged)
4029 417a6e49 2020-09-26 stsp errx(1, "-s option can't be used when diffing "
4030 417a6e49 2020-09-26 stsp "objects in repository");
4031 417a6e49 2020-09-26 stsp id_str1 = argv[0];
4032 417a6e49 2020-09-26 stsp id_str2 = argv[1];
4033 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4034 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4035 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4036 417a6e49 2020-09-26 stsp goto done;
4037 417a6e49 2020-09-26 stsp if (worktree) {
4038 417a6e49 2020-09-26 stsp repo_path = strdup(
4039 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
4040 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4041 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4042 417a6e49 2020-09-26 stsp goto done;
4043 417a6e49 2020-09-26 stsp }
4044 417a6e49 2020-09-26 stsp } else {
4045 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4046 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4047 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4048 417a6e49 2020-09-26 stsp goto done;
4049 417a6e49 2020-09-26 stsp }
4050 417a6e49 2020-09-26 stsp }
4051 417a6e49 2020-09-26 stsp }
4052 417a6e49 2020-09-26 stsp } else
4053 417a6e49 2020-09-26 stsp usage_diff();
4054 417a6e49 2020-09-26 stsp
4055 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4056 417a6e49 2020-09-26 stsp free(repo_path);
4057 417a6e49 2020-09-26 stsp if (error != NULL)
4058 417a6e49 2020-09-26 stsp goto done;
4059 417a6e49 2020-09-26 stsp
4060 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4061 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4062 417a6e49 2020-09-26 stsp if (error)
4063 417a6e49 2020-09-26 stsp goto done;
4064 417a6e49 2020-09-26 stsp
4065 417a6e49 2020-09-26 stsp if (argc <= 1) {
4066 417a6e49 2020-09-26 stsp struct print_diff_arg arg;
4067 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4068 417a6e49 2020-09-26 stsp char *id_str;
4069 417a6e49 2020-09-26 stsp
4070 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4071 417a6e49 2020-09-26 stsp
4072 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
4073 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
4074 417a6e49 2020-09-26 stsp if (error)
4075 417a6e49 2020-09-26 stsp goto done;
4076 417a6e49 2020-09-26 stsp arg.repo = repo;
4077 417a6e49 2020-09-26 stsp arg.worktree = worktree;
4078 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
4079 417a6e49 2020-09-26 stsp arg.id_str = id_str;
4080 417a6e49 2020-09-26 stsp arg.header_shown = 0;
4081 417a6e49 2020-09-26 stsp arg.diff_staged = diff_staged;
4082 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
4083 417a6e49 2020-09-26 stsp
4084 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, path, NULL);
4085 417a6e49 2020-09-26 stsp if (error)
4086 417a6e49 2020-09-26 stsp goto done;
4087 417a6e49 2020-09-26 stsp
4088 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4089 417a6e49 2020-09-26 stsp &arg, check_cancelled, NULL);
4090 417a6e49 2020-09-26 stsp free(id_str);
4091 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4092 417a6e49 2020-09-26 stsp goto done;
4093 417a6e49 2020-09-26 stsp }
4094 417a6e49 2020-09-26 stsp
4095 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4096 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4097 417a6e49 2020-09-26 stsp if (error)
4098 417a6e49 2020-09-26 stsp goto done;
4099 417a6e49 2020-09-26 stsp
4100 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4101 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4102 417a6e49 2020-09-26 stsp if (error)
4103 417a6e49 2020-09-26 stsp goto done;
4104 417a6e49 2020-09-26 stsp
4105 417a6e49 2020-09-26 stsp error = got_object_get_type(&type1, repo, id1);
4106 417a6e49 2020-09-26 stsp if (error)
4107 417a6e49 2020-09-26 stsp goto done;
4108 417a6e49 2020-09-26 stsp
4109 417a6e49 2020-09-26 stsp error = got_object_get_type(&type2, repo, id2);
4110 417a6e49 2020-09-26 stsp if (error)
4111 417a6e49 2020-09-26 stsp goto done;
4112 417a6e49 2020-09-26 stsp
4113 417a6e49 2020-09-26 stsp if (type1 != type2) {
4114 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4115 417a6e49 2020-09-26 stsp goto done;
4116 417a6e49 2020-09-26 stsp }
4117 417a6e49 2020-09-26 stsp
4118 417a6e49 2020-09-26 stsp switch (type1) {
4119 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
4120 417a6e49 2020-09-26 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4121 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4122 417a6e49 2020-09-26 stsp break;
4123 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
4124 417a6e49 2020-09-26 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4125 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4126 417a6e49 2020-09-26 stsp break;
4127 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
4128 417a6e49 2020-09-26 stsp printf("diff %s %s\n", label1, label2);
4129 417a6e49 2020-09-26 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4130 417a6e49 2020-09-26 stsp ignore_whitespace, repo, stdout);
4131 417a6e49 2020-09-26 stsp break;
4132 417a6e49 2020-09-26 stsp default:
4133 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4134 417a6e49 2020-09-26 stsp }
4135 417a6e49 2020-09-26 stsp done:
4136 417a6e49 2020-09-26 stsp free(label1);
4137 417a6e49 2020-09-26 stsp free(label2);
4138 417a6e49 2020-09-26 stsp free(id1);
4139 417a6e49 2020-09-26 stsp free(id2);
4140 417a6e49 2020-09-26 stsp free(path);
4141 417a6e49 2020-09-26 stsp if (worktree)
4142 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4143 417a6e49 2020-09-26 stsp if (repo) {
4144 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4145 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4146 417a6e49 2020-09-26 stsp if (error == NULL)
4147 417a6e49 2020-09-26 stsp error = repo_error;
4148 417a6e49 2020-09-26 stsp }
4149 417a6e49 2020-09-26 stsp return error;
4150 417a6e49 2020-09-26 stsp }
4151 417a6e49 2020-09-26 stsp
4152 417a6e49 2020-09-26 stsp __dead static void
4153 417a6e49 2020-09-26 stsp usage_blame(void)
4154 417a6e49 2020-09-26 stsp {
4155 417a6e49 2020-09-26 stsp fprintf(stderr,
4156 417a6e49 2020-09-26 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4157 417a6e49 2020-09-26 stsp getprogname());
4158 417a6e49 2020-09-26 stsp exit(1);
4159 417a6e49 2020-09-26 stsp }
4160 417a6e49 2020-09-26 stsp
4161 417a6e49 2020-09-26 stsp struct blame_line {
4162 417a6e49 2020-09-26 stsp int annotated;
4163 417a6e49 2020-09-26 stsp char *id_str;
4164 417a6e49 2020-09-26 stsp char *committer;
4165 417a6e49 2020-09-26 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4166 417a6e49 2020-09-26 stsp };
4167 417a6e49 2020-09-26 stsp
4168 417a6e49 2020-09-26 stsp struct blame_cb_args {
4169 417a6e49 2020-09-26 stsp struct blame_line *lines;
4170 417a6e49 2020-09-26 stsp int nlines;
4171 417a6e49 2020-09-26 stsp int nlines_prec;
4172 417a6e49 2020-09-26 stsp int lineno_cur;
4173 417a6e49 2020-09-26 stsp off_t *line_offsets;
4174 417a6e49 2020-09-26 stsp FILE *f;
4175 417a6e49 2020-09-26 stsp struct got_repository *repo;
4176 417a6e49 2020-09-26 stsp };
4177 417a6e49 2020-09-26 stsp
4178 417a6e49 2020-09-26 stsp static const struct got_error *
4179 417a6e49 2020-09-26 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4180 417a6e49 2020-09-26 stsp {
4181 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4182 417a6e49 2020-09-26 stsp struct blame_cb_args *a = arg;
4183 417a6e49 2020-09-26 stsp struct blame_line *bline;
4184 417a6e49 2020-09-26 stsp char *line = NULL;
4185 417a6e49 2020-09-26 stsp size_t linesize = 0;
4186 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
4187 417a6e49 2020-09-26 stsp off_t offset;
4188 417a6e49 2020-09-26 stsp struct tm tm;
4189 417a6e49 2020-09-26 stsp time_t committer_time;
4190 417a6e49 2020-09-26 stsp
4191 417a6e49 2020-09-26 stsp if (nlines != a->nlines ||
4192 417a6e49 2020-09-26 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4193 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_RANGE);
4194 417a6e49 2020-09-26 stsp
4195 417a6e49 2020-09-26 stsp if (sigint_received)
4196 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4197 417a6e49 2020-09-26 stsp
4198 417a6e49 2020-09-26 stsp if (lineno == -1)
4199 417a6e49 2020-09-26 stsp return NULL; /* no change in this commit */
4200 417a6e49 2020-09-26 stsp
4201 417a6e49 2020-09-26 stsp /* Annotate this line. */
4202 417a6e49 2020-09-26 stsp bline = &a->lines[lineno - 1];
4203 417a6e49 2020-09-26 stsp if (bline->annotated)
4204 417a6e49 2020-09-26 stsp return NULL;
4205 417a6e49 2020-09-26 stsp err = got_object_id_str(&bline->id_str, id);
4206 417a6e49 2020-09-26 stsp if (err)
4207 417a6e49 2020-09-26 stsp return err;
4208 417a6e49 2020-09-26 stsp
4209 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4210 417a6e49 2020-09-26 stsp if (err)
4211 417a6e49 2020-09-26 stsp goto done;
4212 417a6e49 2020-09-26 stsp
4213 417a6e49 2020-09-26 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4214 417a6e49 2020-09-26 stsp if (bline->committer == NULL) {
4215 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
4216 417a6e49 2020-09-26 stsp goto done;
4217 417a6e49 2020-09-26 stsp }
4218 417a6e49 2020-09-26 stsp
4219 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
4220 417a6e49 2020-09-26 stsp if (localtime_r(&committer_time, &tm) == NULL)
4221 417a6e49 2020-09-26 stsp return got_error_from_errno("localtime_r");
4222 417a6e49 2020-09-26 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4223 417a6e49 2020-09-26 stsp &tm) >= sizeof(bline->datebuf)) {
4224 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
4225 417a6e49 2020-09-26 stsp goto done;
4226 417a6e49 2020-09-26 stsp }
4227 417a6e49 2020-09-26 stsp bline->annotated = 1;
4228 417a6e49 2020-09-26 stsp
4229 417a6e49 2020-09-26 stsp /* Print lines annotated so far. */
4230 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4231 417a6e49 2020-09-26 stsp if (!bline->annotated)
4232 417a6e49 2020-09-26 stsp goto done;
4233 417a6e49 2020-09-26 stsp
4234 417a6e49 2020-09-26 stsp offset = a->line_offsets[a->lineno_cur - 1];
4235 417a6e49 2020-09-26 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4236 417a6e49 2020-09-26 stsp err = got_error_from_errno("fseeko");
4237 417a6e49 2020-09-26 stsp goto done;
4238 417a6e49 2020-09-26 stsp }
4239 417a6e49 2020-09-26 stsp
4240 417a6e49 2020-09-26 stsp while (bline->annotated) {
4241 417a6e49 2020-09-26 stsp char *smallerthan, *at, *nl, *committer;
4242 417a6e49 2020-09-26 stsp size_t len;
4243 417a6e49 2020-09-26 stsp
4244 417a6e49 2020-09-26 stsp if (getline(&line, &linesize, a->f) == -1) {
4245 417a6e49 2020-09-26 stsp if (ferror(a->f))
4246 417a6e49 2020-09-26 stsp err = got_error_from_errno("getline");
4247 417a6e49 2020-09-26 stsp break;
4248 417a6e49 2020-09-26 stsp }
4249 417a6e49 2020-09-26 stsp
4250 417a6e49 2020-09-26 stsp committer = bline->committer;
4251 417a6e49 2020-09-26 stsp smallerthan = strchr(committer, '<');
4252 417a6e49 2020-09-26 stsp if (smallerthan && smallerthan[1] != '\0')
4253 417a6e49 2020-09-26 stsp committer = smallerthan + 1;
4254 417a6e49 2020-09-26 stsp at = strchr(committer, '@');
4255 417a6e49 2020-09-26 stsp if (at)
4256 417a6e49 2020-09-26 stsp *at = '\0';
4257 417a6e49 2020-09-26 stsp len = strlen(committer);
4258 417a6e49 2020-09-26 stsp if (len >= 9)
4259 417a6e49 2020-09-26 stsp committer[8] = '\0';
4260 417a6e49 2020-09-26 stsp
4261 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
4262 417a6e49 2020-09-26 stsp if (nl)
4263 417a6e49 2020-09-26 stsp *nl = '\0';
4264 417a6e49 2020-09-26 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4265 417a6e49 2020-09-26 stsp bline->id_str, bline->datebuf, committer, line);
4266 417a6e49 2020-09-26 stsp
4267 417a6e49 2020-09-26 stsp a->lineno_cur++;
4268 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4269 417a6e49 2020-09-26 stsp }
4270 417a6e49 2020-09-26 stsp done:
4271 417a6e49 2020-09-26 stsp if (commit)
4272 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
4273 417a6e49 2020-09-26 stsp free(line);
4274 417a6e49 2020-09-26 stsp return err;
4275 417a6e49 2020-09-26 stsp }
4276 417a6e49 2020-09-26 stsp
4277 417a6e49 2020-09-26 stsp static const struct got_error *
4278 417a6e49 2020-09-26 stsp cmd_blame(int argc, char *argv[])
4279 417a6e49 2020-09-26 stsp {
4280 417a6e49 2020-09-26 stsp const struct got_error *error;
4281 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4282 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4283 417a6e49 2020-09-26 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4284 417a6e49 2020-09-26 stsp char *link_target = NULL;
4285 417a6e49 2020-09-26 stsp struct got_object_id *obj_id = NULL;
4286 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4287 417a6e49 2020-09-26 stsp struct got_blob_object *blob = NULL;
4288 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4289 417a6e49 2020-09-26 stsp struct blame_cb_args bca;
4290 417a6e49 2020-09-26 stsp int ch, obj_type, i;
4291 417a6e49 2020-09-26 stsp size_t filesize;
4292 417a6e49 2020-09-26 stsp
4293 417a6e49 2020-09-26 stsp memset(&bca, 0, sizeof(bca));
4294 417a6e49 2020-09-26 stsp
4295 417a6e49 2020-09-26 stsp #ifndef PROFILE
4296 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4297 417a6e49 2020-09-26 stsp NULL) == -1)
4298 417a6e49 2020-09-26 stsp err(1, "pledge");
4299 417a6e49 2020-09-26 stsp #endif
4300 417a6e49 2020-09-26 stsp
4301 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4302 417a6e49 2020-09-26 stsp switch (ch) {
4303 417a6e49 2020-09-26 stsp case 'c':
4304 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4305 417a6e49 2020-09-26 stsp break;
4306 417a6e49 2020-09-26 stsp case 'r':
4307 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4308 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4309 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4310 417a6e49 2020-09-26 stsp optarg);
4311 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4312 417a6e49 2020-09-26 stsp break;
4313 417a6e49 2020-09-26 stsp default:
4314 417a6e49 2020-09-26 stsp usage_blame();
4315 417a6e49 2020-09-26 stsp /* NOTREACHED */
4316 417a6e49 2020-09-26 stsp }
4317 417a6e49 2020-09-26 stsp }
4318 417a6e49 2020-09-26 stsp
4319 417a6e49 2020-09-26 stsp argc -= optind;
4320 417a6e49 2020-09-26 stsp argv += optind;
4321 417a6e49 2020-09-26 stsp
4322 417a6e49 2020-09-26 stsp if (argc == 1)
4323 417a6e49 2020-09-26 stsp path = argv[0];
4324 417a6e49 2020-09-26 stsp else
4325 417a6e49 2020-09-26 stsp usage_blame();
4326 417a6e49 2020-09-26 stsp
4327 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4328 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4329 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4330 417a6e49 2020-09-26 stsp goto done;
4331 417a6e49 2020-09-26 stsp }
4332 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4333 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4334 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4335 417a6e49 2020-09-26 stsp goto done;
4336 417a6e49 2020-09-26 stsp else
4337 417a6e49 2020-09-26 stsp error = NULL;
4338 417a6e49 2020-09-26 stsp if (worktree) {
4339 417a6e49 2020-09-26 stsp repo_path =
4340 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4341 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4342 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4343 417a6e49 2020-09-26 stsp if (error)
4344 417a6e49 2020-09-26 stsp goto done;
4345 417a6e49 2020-09-26 stsp }
4346 417a6e49 2020-09-26 stsp } else {
4347 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4348 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4349 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4350 417a6e49 2020-09-26 stsp goto done;
4351 417a6e49 2020-09-26 stsp }
4352 417a6e49 2020-09-26 stsp }
4353 417a6e49 2020-09-26 stsp }
4354 417a6e49 2020-09-26 stsp
4355 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4356 417a6e49 2020-09-26 stsp if (error != NULL)
4357 417a6e49 2020-09-26 stsp goto done;
4358 417a6e49 2020-09-26 stsp
4359 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4360 417a6e49 2020-09-26 stsp if (error)
4361 417a6e49 2020-09-26 stsp goto done;
4362 417a6e49 2020-09-26 stsp
4363 417a6e49 2020-09-26 stsp if (worktree) {
4364 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4365 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4366 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4367 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s%s%s",
4368 417a6e49 2020-09-26 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4369 417a6e49 2020-09-26 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4370 417a6e49 2020-09-26 stsp path) == -1) {
4371 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4372 417a6e49 2020-09-26 stsp goto done;
4373 417a6e49 2020-09-26 stsp }
4374 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4375 417a6e49 2020-09-26 stsp free(p);
4376 417a6e49 2020-09-26 stsp } else {
4377 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4378 417a6e49 2020-09-26 stsp }
4379 417a6e49 2020-09-26 stsp if (error)
4380 417a6e49 2020-09-26 stsp goto done;
4381 417a6e49 2020-09-26 stsp
4382 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4383 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4384 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, worktree ?
4385 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4386 417a6e49 2020-09-26 stsp if (error != NULL)
4387 417a6e49 2020-09-26 stsp goto done;
4388 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4389 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4390 417a6e49 2020-09-26 stsp if (error != NULL)
4391 417a6e49 2020-09-26 stsp goto done;
4392 417a6e49 2020-09-26 stsp } else {
4393 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4394 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4395 417a6e49 2020-09-26 stsp if (error)
4396 417a6e49 2020-09-26 stsp goto done;
4397 417a6e49 2020-09-26 stsp }
4398 417a6e49 2020-09-26 stsp
4399 417a6e49 2020-09-26 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4400 417a6e49 2020-09-26 stsp commit_id, repo);
4401 417a6e49 2020-09-26 stsp if (error)
4402 417a6e49 2020-09-26 stsp goto done;
4403 417a6e49 2020-09-26 stsp
4404 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4405 417a6e49 2020-09-26 stsp link_target ? link_target : in_repo_path);
4406 417a6e49 2020-09-26 stsp if (error)
4407 417a6e49 2020-09-26 stsp goto done;
4408 417a6e49 2020-09-26 stsp
4409 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4410 417a6e49 2020-09-26 stsp if (error)
4411 417a6e49 2020-09-26 stsp goto done;
4412 417a6e49 2020-09-26 stsp
4413 417a6e49 2020-09-26 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4414 417a6e49 2020-09-26 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4415 417a6e49 2020-09-26 stsp GOT_ERR_OBJ_TYPE);
4416 417a6e49 2020-09-26 stsp goto done;
4417 417a6e49 2020-09-26 stsp }
4418 417a6e49 2020-09-26 stsp
4419 417a6e49 2020-09-26 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4420 417a6e49 2020-09-26 stsp if (error)
4421 417a6e49 2020-09-26 stsp goto done;
4422 417a6e49 2020-09-26 stsp bca.f = got_opentemp();
4423 417a6e49 2020-09-26 stsp if (bca.f == NULL) {
4424 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_opentemp");
4425 417a6e49 2020-09-26 stsp goto done;
4426 417a6e49 2020-09-26 stsp }
4427 417a6e49 2020-09-26 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4428 417a6e49 2020-09-26 stsp &bca.line_offsets, bca.f, blob);
4429 417a6e49 2020-09-26 stsp if (error || bca.nlines == 0)
4430 417a6e49 2020-09-26 stsp goto done;
4431 417a6e49 2020-09-26 stsp
4432 417a6e49 2020-09-26 stsp /* Don't include \n at EOF in the blame line count. */
4433 417a6e49 2020-09-26 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4434 417a6e49 2020-09-26 stsp bca.nlines--;
4435 417a6e49 2020-09-26 stsp
4436 417a6e49 2020-09-26 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4437 417a6e49 2020-09-26 stsp if (bca.lines == NULL) {
4438 417a6e49 2020-09-26 stsp error = got_error_from_errno("calloc");
4439 417a6e49 2020-09-26 stsp goto done;
4440 417a6e49 2020-09-26 stsp }
4441 417a6e49 2020-09-26 stsp bca.lineno_cur = 1;
4442 417a6e49 2020-09-26 stsp bca.nlines_prec = 0;
4443 417a6e49 2020-09-26 stsp i = bca.nlines;
4444 417a6e49 2020-09-26 stsp while (i > 0) {
4445 417a6e49 2020-09-26 stsp i /= 10;
4446 417a6e49 2020-09-26 stsp bca.nlines_prec++;
4447 417a6e49 2020-09-26 stsp }
4448 417a6e49 2020-09-26 stsp bca.repo = repo;
4449 417a6e49 2020-09-26 stsp
4450 417a6e49 2020-09-26 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4451 417a6e49 2020-09-26 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4452 417a6e49 2020-09-26 stsp done:
4453 417a6e49 2020-09-26 stsp free(in_repo_path);
4454 417a6e49 2020-09-26 stsp free(link_target);
4455 417a6e49 2020-09-26 stsp free(repo_path);
4456 417a6e49 2020-09-26 stsp free(cwd);
4457 417a6e49 2020-09-26 stsp free(commit_id);
4458 417a6e49 2020-09-26 stsp free(obj_id);
4459 417a6e49 2020-09-26 stsp if (blob)
4460 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
4461 417a6e49 2020-09-26 stsp if (worktree)
4462 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4463 417a6e49 2020-09-26 stsp if (repo) {
4464 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4465 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4466 417a6e49 2020-09-26 stsp if (error == NULL)
4467 417a6e49 2020-09-26 stsp error = repo_error;
4468 417a6e49 2020-09-26 stsp }
4469 417a6e49 2020-09-26 stsp if (bca.lines) {
4470 417a6e49 2020-09-26 stsp for (i = 0; i < bca.nlines; i++) {
4471 417a6e49 2020-09-26 stsp struct blame_line *bline = &bca.lines[i];
4472 417a6e49 2020-09-26 stsp free(bline->id_str);
4473 417a6e49 2020-09-26 stsp free(bline->committer);
4474 417a6e49 2020-09-26 stsp }
4475 417a6e49 2020-09-26 stsp free(bca.lines);
4476 417a6e49 2020-09-26 stsp }
4477 417a6e49 2020-09-26 stsp free(bca.line_offsets);
4478 417a6e49 2020-09-26 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4479 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
4480 417a6e49 2020-09-26 stsp return error;
4481 417a6e49 2020-09-26 stsp }
4482 417a6e49 2020-09-26 stsp
4483 417a6e49 2020-09-26 stsp __dead static void
4484 417a6e49 2020-09-26 stsp usage_tree(void)
4485 417a6e49 2020-09-26 stsp {
4486 417a6e49 2020-09-26 stsp fprintf(stderr,
4487 417a6e49 2020-09-26 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4488 417a6e49 2020-09-26 stsp getprogname());
4489 417a6e49 2020-09-26 stsp exit(1);
4490 417a6e49 2020-09-26 stsp }
4491 417a6e49 2020-09-26 stsp
4492 417a6e49 2020-09-26 stsp static const struct got_error *
4493 417a6e49 2020-09-26 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4494 417a6e49 2020-09-26 stsp const char *root_path, struct got_repository *repo)
4495 417a6e49 2020-09-26 stsp {
4496 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4497 417a6e49 2020-09-26 stsp int is_root_path = (strcmp(path, root_path) == 0);
4498 417a6e49 2020-09-26 stsp const char *modestr = "";
4499 417a6e49 2020-09-26 stsp mode_t mode = got_tree_entry_get_mode(te);
4500 417a6e49 2020-09-26 stsp char *link_target = NULL;
4501 417a6e49 2020-09-26 stsp
4502 417a6e49 2020-09-26 stsp path += strlen(root_path);
4503 417a6e49 2020-09-26 stsp while (path[0] == '/')
4504 417a6e49 2020-09-26 stsp path++;
4505 417a6e49 2020-09-26 stsp
4506 417a6e49 2020-09-26 stsp if (got_object_tree_entry_is_submodule(te))
4507 417a6e49 2020-09-26 stsp modestr = "$";
4508 417a6e49 2020-09-26 stsp else if (S_ISLNK(mode)) {
4509 417a6e49 2020-09-26 stsp int i;
4510 417a6e49 2020-09-26 stsp
4511 417a6e49 2020-09-26 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4512 417a6e49 2020-09-26 stsp if (err)
4513 417a6e49 2020-09-26 stsp return err;
4514 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(link_target); i++) {
4515 417a6e49 2020-09-26 stsp if (!isprint((unsigned char)link_target[i]))
4516 417a6e49 2020-09-26 stsp link_target[i] = '?';
4517 417a6e49 2020-09-26 stsp }
4518 417a6e49 2020-09-26 stsp
4519 417a6e49 2020-09-26 stsp modestr = "@";
4520 417a6e49 2020-09-26 stsp }
4521 417a6e49 2020-09-26 stsp else if (S_ISDIR(mode))
4522 417a6e49 2020-09-26 stsp modestr = "/";
4523 417a6e49 2020-09-26 stsp else if (mode & S_IXUSR)
4524 417a6e49 2020-09-26 stsp modestr = "*";
4525 417a6e49 2020-09-26 stsp
4526 417a6e49 2020-09-26 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4527 417a6e49 2020-09-26 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4528 417a6e49 2020-09-26 stsp link_target ? " -> ": "", link_target ? link_target : "");
4529 417a6e49 2020-09-26 stsp
4530 417a6e49 2020-09-26 stsp free(link_target);
4531 417a6e49 2020-09-26 stsp return NULL;
4532 417a6e49 2020-09-26 stsp }
4533 417a6e49 2020-09-26 stsp
4534 417a6e49 2020-09-26 stsp static const struct got_error *
4535 417a6e49 2020-09-26 stsp print_tree(const char *path, struct got_object_id *commit_id,
4536 417a6e49 2020-09-26 stsp int show_ids, int recurse, const char *root_path,
4537 417a6e49 2020-09-26 stsp struct got_repository *repo)
4538 417a6e49 2020-09-26 stsp {
4539 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4540 417a6e49 2020-09-26 stsp struct got_object_id *tree_id = NULL;
4541 417a6e49 2020-09-26 stsp struct got_tree_object *tree = NULL;
4542 417a6e49 2020-09-26 stsp int nentries, i;
4543 417a6e49 2020-09-26 stsp
4544 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4545 417a6e49 2020-09-26 stsp if (err)
4546 417a6e49 2020-09-26 stsp goto done;
4547 417a6e49 2020-09-26 stsp
4548 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4549 417a6e49 2020-09-26 stsp if (err)
4550 417a6e49 2020-09-26 stsp goto done;
4551 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
4552 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
4553 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
4554 417a6e49 2020-09-26 stsp char *id = NULL;
4555 417a6e49 2020-09-26 stsp
4556 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
4557 417a6e49 2020-09-26 stsp break;
4558 417a6e49 2020-09-26 stsp
4559 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
4560 417a6e49 2020-09-26 stsp if (show_ids) {
4561 417a6e49 2020-09-26 stsp char *id_str;
4562 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
4563 417a6e49 2020-09-26 stsp got_tree_entry_get_id(te));
4564 417a6e49 2020-09-26 stsp if (err)
4565 417a6e49 2020-09-26 stsp goto done;
4566 417a6e49 2020-09-26 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4567 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4568 417a6e49 2020-09-26 stsp free(id_str);
4569 417a6e49 2020-09-26 stsp goto done;
4570 417a6e49 2020-09-26 stsp }
4571 417a6e49 2020-09-26 stsp free(id_str);
4572 417a6e49 2020-09-26 stsp }
4573 417a6e49 2020-09-26 stsp err = print_entry(te, id, path, root_path, repo);
4574 417a6e49 2020-09-26 stsp free(id);
4575 417a6e49 2020-09-26 stsp if (err)
4576 417a6e49 2020-09-26 stsp goto done;
4577 417a6e49 2020-09-26 stsp
4578 417a6e49 2020-09-26 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4579 417a6e49 2020-09-26 stsp char *child_path;
4580 417a6e49 2020-09-26 stsp if (asprintf(&child_path, "%s%s%s", path,
4581 417a6e49 2020-09-26 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4582 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te)) == -1) {
4583 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4584 417a6e49 2020-09-26 stsp goto done;
4585 417a6e49 2020-09-26 stsp }
4586 417a6e49 2020-09-26 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4587 417a6e49 2020-09-26 stsp root_path, repo);
4588 417a6e49 2020-09-26 stsp free(child_path);
4589 417a6e49 2020-09-26 stsp if (err)
4590 417a6e49 2020-09-26 stsp goto done;
4591 417a6e49 2020-09-26 stsp }
4592 417a6e49 2020-09-26 stsp }
4593 417a6e49 2020-09-26 stsp done:
4594 417a6e49 2020-09-26 stsp if (tree)
4595 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
4596 417a6e49 2020-09-26 stsp free(tree_id);
4597 417a6e49 2020-09-26 stsp return err;
4598 417a6e49 2020-09-26 stsp }
4599 417a6e49 2020-09-26 stsp
4600 417a6e49 2020-09-26 stsp static const struct got_error *
4601 417a6e49 2020-09-26 stsp cmd_tree(int argc, char *argv[])
4602 417a6e49 2020-09-26 stsp {
4603 417a6e49 2020-09-26 stsp const struct got_error *error;
4604 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4605 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4606 417a6e49 2020-09-26 stsp const char *path, *refname = NULL;
4607 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4608 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4609 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4610 417a6e49 2020-09-26 stsp int show_ids = 0, recurse = 0;
4611 417a6e49 2020-09-26 stsp int ch;
4612 417a6e49 2020-09-26 stsp
4613 417a6e49 2020-09-26 stsp #ifndef PROFILE
4614 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4615 417a6e49 2020-09-26 stsp NULL) == -1)
4616 417a6e49 2020-09-26 stsp err(1, "pledge");
4617 417a6e49 2020-09-26 stsp #endif
4618 417a6e49 2020-09-26 stsp
4619 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4620 417a6e49 2020-09-26 stsp switch (ch) {
4621 417a6e49 2020-09-26 stsp case 'c':
4622 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4623 417a6e49 2020-09-26 stsp break;
4624 417a6e49 2020-09-26 stsp case 'r':
4625 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4626 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4627 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4628 417a6e49 2020-09-26 stsp optarg);
4629 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4630 417a6e49 2020-09-26 stsp break;
4631 417a6e49 2020-09-26 stsp case 'i':
4632 417a6e49 2020-09-26 stsp show_ids = 1;
4633 417a6e49 2020-09-26 stsp break;
4634 417a6e49 2020-09-26 stsp case 'R':
4635 417a6e49 2020-09-26 stsp recurse = 1;
4636 417a6e49 2020-09-26 stsp break;
4637 417a6e49 2020-09-26 stsp default:
4638 417a6e49 2020-09-26 stsp usage_tree();
4639 417a6e49 2020-09-26 stsp /* NOTREACHED */
4640 417a6e49 2020-09-26 stsp }
4641 417a6e49 2020-09-26 stsp }
4642 417a6e49 2020-09-26 stsp
4643 417a6e49 2020-09-26 stsp argc -= optind;
4644 417a6e49 2020-09-26 stsp argv += optind;
4645 417a6e49 2020-09-26 stsp
4646 417a6e49 2020-09-26 stsp if (argc == 1)
4647 417a6e49 2020-09-26 stsp path = argv[0];
4648 417a6e49 2020-09-26 stsp else if (argc > 1)
4649 417a6e49 2020-09-26 stsp usage_tree();
4650 417a6e49 2020-09-26 stsp else
4651 417a6e49 2020-09-26 stsp path = NULL;
4652 417a6e49 2020-09-26 stsp
4653 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4654 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4655 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4656 417a6e49 2020-09-26 stsp goto done;
4657 417a6e49 2020-09-26 stsp }
4658 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4659 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4660 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4661 417a6e49 2020-09-26 stsp goto done;
4662 417a6e49 2020-09-26 stsp else
4663 417a6e49 2020-09-26 stsp error = NULL;
4664 417a6e49 2020-09-26 stsp if (worktree) {
4665 417a6e49 2020-09-26 stsp repo_path =
4666 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4667 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4668 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4669 417a6e49 2020-09-26 stsp if (error)
4670 417a6e49 2020-09-26 stsp goto done;
4671 417a6e49 2020-09-26 stsp } else {
4672 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4673 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4674 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4675 417a6e49 2020-09-26 stsp goto done;
4676 417a6e49 2020-09-26 stsp }
4677 417a6e49 2020-09-26 stsp }
4678 417a6e49 2020-09-26 stsp }
4679 417a6e49 2020-09-26 stsp
4680 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4681 417a6e49 2020-09-26 stsp if (error != NULL)
4682 417a6e49 2020-09-26 stsp goto done;
4683 417a6e49 2020-09-26 stsp
4684 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4685 417a6e49 2020-09-26 stsp if (error)
4686 417a6e49 2020-09-26 stsp goto done;
4687 417a6e49 2020-09-26 stsp
4688 417a6e49 2020-09-26 stsp if (path == NULL) {
4689 417a6e49 2020-09-26 stsp if (worktree) {
4690 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4691 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4692 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s/%s",
4693 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
4694 417a6e49 2020-09-26 stsp worktree_subdir) == -1) {
4695 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4696 417a6e49 2020-09-26 stsp goto done;
4697 417a6e49 2020-09-26 stsp }
4698 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4699 417a6e49 2020-09-26 stsp free(p);
4700 417a6e49 2020-09-26 stsp if (error)
4701 417a6e49 2020-09-26 stsp goto done;
4702 417a6e49 2020-09-26 stsp } else
4703 417a6e49 2020-09-26 stsp path = "/";
4704 417a6e49 2020-09-26 stsp }
4705 417a6e49 2020-09-26 stsp if (in_repo_path == NULL) {
4706 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4707 417a6e49 2020-09-26 stsp if (error != NULL)
4708 417a6e49 2020-09-26 stsp goto done;
4709 417a6e49 2020-09-26 stsp }
4710 417a6e49 2020-09-26 stsp
4711 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4712 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4713 417a6e49 2020-09-26 stsp if (worktree)
4714 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
4715 417a6e49 2020-09-26 stsp else
4716 417a6e49 2020-09-26 stsp refname = GOT_REF_HEAD;
4717 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, refname, 0);
4718 417a6e49 2020-09-26 stsp if (error != NULL)
4719 417a6e49 2020-09-26 stsp goto done;
4720 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4721 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4722 417a6e49 2020-09-26 stsp if (error != NULL)
4723 417a6e49 2020-09-26 stsp goto done;
4724 417a6e49 2020-09-26 stsp } else {
4725 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4726 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4727 417a6e49 2020-09-26 stsp if (error)
4728 417a6e49 2020-09-26 stsp goto done;
4729 417a6e49 2020-09-26 stsp }
4730 417a6e49 2020-09-26 stsp
4731 417a6e49 2020-09-26 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4732 417a6e49 2020-09-26 stsp in_repo_path, repo);
4733 417a6e49 2020-09-26 stsp done:
4734 417a6e49 2020-09-26 stsp free(in_repo_path);
4735 417a6e49 2020-09-26 stsp free(repo_path);
4736 417a6e49 2020-09-26 stsp free(cwd);
4737 417a6e49 2020-09-26 stsp free(commit_id);
4738 417a6e49 2020-09-26 stsp if (worktree)
4739 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4740 417a6e49 2020-09-26 stsp if (repo) {
4741 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4742 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4743 417a6e49 2020-09-26 stsp if (error == NULL)
4744 417a6e49 2020-09-26 stsp error = repo_error;
4745 417a6e49 2020-09-26 stsp }
4746 417a6e49 2020-09-26 stsp return error;
4747 417a6e49 2020-09-26 stsp }
4748 417a6e49 2020-09-26 stsp
4749 417a6e49 2020-09-26 stsp __dead static void
4750 417a6e49 2020-09-26 stsp usage_status(void)
4751 417a6e49 2020-09-26 stsp {
4752 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4753 417a6e49 2020-09-26 stsp getprogname());
4754 417a6e49 2020-09-26 stsp exit(1);
4755 417a6e49 2020-09-26 stsp }
4756 417a6e49 2020-09-26 stsp
4757 417a6e49 2020-09-26 stsp static const struct got_error *
4758 417a6e49 2020-09-26 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4759 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
4760 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4761 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
4762 417a6e49 2020-09-26 stsp {
4763 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4764 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
4765 417a6e49 2020-09-26 stsp if (arg) {
4766 417a6e49 2020-09-26 stsp char *status_codes = arg;
4767 417a6e49 2020-09-26 stsp size_t ncodes = strlen(status_codes);
4768 417a6e49 2020-09-26 stsp int i;
4769 417a6e49 2020-09-26 stsp for (i = 0; i < ncodes ; i++) {
4770 417a6e49 2020-09-26 stsp if (status == status_codes[i] ||
4771 417a6e49 2020-09-26 stsp staged_status == status_codes[i])
4772 417a6e49 2020-09-26 stsp break;
4773 417a6e49 2020-09-26 stsp }
4774 417a6e49 2020-09-26 stsp if (i == ncodes)
4775 417a6e49 2020-09-26 stsp return NULL;
4776 417a6e49 2020-09-26 stsp }
4777 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
4778 417a6e49 2020-09-26 stsp return NULL;
4779 417a6e49 2020-09-26 stsp }
4780 417a6e49 2020-09-26 stsp
4781 417a6e49 2020-09-26 stsp static const struct got_error *
4782 417a6e49 2020-09-26 stsp cmd_status(int argc, char *argv[])
4783 417a6e49 2020-09-26 stsp {
4784 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4785 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4786 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4787 417a6e49 2020-09-26 stsp char *cwd = NULL, *status_codes = NULL;;
4788 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4789 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
4790 417a6e49 2020-09-26 stsp int ch, i;
4791 417a6e49 2020-09-26 stsp
4792 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4793 417a6e49 2020-09-26 stsp
4794 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4795 417a6e49 2020-09-26 stsp switch (ch) {
4796 417a6e49 2020-09-26 stsp case 's':
4797 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
4798 417a6e49 2020-09-26 stsp switch (optarg[i]) {
4799 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
4800 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
4801 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
4802 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
4803 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
4804 417a6e49 2020-09-26 stsp case GOT_STATUS_OBSTRUCTED:
4805 417a6e49 2020-09-26 stsp case GOT_STATUS_UNVERSIONED:
4806 417a6e49 2020-09-26 stsp case GOT_STATUS_MODE_CHANGE:
4807 417a6e49 2020-09-26 stsp case GOT_STATUS_NONEXISTENT:
4808 417a6e49 2020-09-26 stsp break;
4809 417a6e49 2020-09-26 stsp default:
4810 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
4811 417a6e49 2020-09-26 stsp optarg[i]);
4812 417a6e49 2020-09-26 stsp }
4813 417a6e49 2020-09-26 stsp }
4814 417a6e49 2020-09-26 stsp status_codes = optarg;
4815 417a6e49 2020-09-26 stsp break;
4816 417a6e49 2020-09-26 stsp default:
4817 417a6e49 2020-09-26 stsp usage_status();
4818 417a6e49 2020-09-26 stsp /* NOTREACHED */
4819 417a6e49 2020-09-26 stsp }
4820 417a6e49 2020-09-26 stsp }
4821 417a6e49 2020-09-26 stsp
4822 417a6e49 2020-09-26 stsp argc -= optind;
4823 417a6e49 2020-09-26 stsp argv += optind;
4824 417a6e49 2020-09-26 stsp
4825 417a6e49 2020-09-26 stsp #ifndef PROFILE
4826 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4827 417a6e49 2020-09-26 stsp NULL) == -1)
4828 417a6e49 2020-09-26 stsp err(1, "pledge");
4829 417a6e49 2020-09-26 stsp #endif
4830 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4831 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4832 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4833 417a6e49 2020-09-26 stsp goto done;
4834 417a6e49 2020-09-26 stsp }
4835 417a6e49 2020-09-26 stsp
4836 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4837 417a6e49 2020-09-26 stsp if (error) {
4838 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4839 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
4840 417a6e49 2020-09-26 stsp goto done;
4841 417a6e49 2020-09-26 stsp }
4842 417a6e49 2020-09-26 stsp
4843 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4844 417a6e49 2020-09-26 stsp NULL);
4845 417a6e49 2020-09-26 stsp if (error != NULL)
4846 417a6e49 2020-09-26 stsp goto done;
4847 417a6e49 2020-09-26 stsp
4848 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4849 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
4850 417a6e49 2020-09-26 stsp if (error)
4851 417a6e49 2020-09-26 stsp goto done;
4852 417a6e49 2020-09-26 stsp
4853 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4854 417a6e49 2020-09-26 stsp if (error)
4855 417a6e49 2020-09-26 stsp goto done;
4856 417a6e49 2020-09-26 stsp
4857 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4858 417a6e49 2020-09-26 stsp status_codes, check_cancelled, NULL);
4859 417a6e49 2020-09-26 stsp done:
4860 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
4861 417a6e49 2020-09-26 stsp free((char *)pe->path);
4862 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4863 417a6e49 2020-09-26 stsp free(cwd);
4864 417a6e49 2020-09-26 stsp return error;
4865 417a6e49 2020-09-26 stsp }
4866 417a6e49 2020-09-26 stsp
4867 417a6e49 2020-09-26 stsp __dead static void
4868 417a6e49 2020-09-26 stsp usage_ref(void)
4869 417a6e49 2020-09-26 stsp {
4870 417a6e49 2020-09-26 stsp fprintf(stderr,
4871 417a6e49 2020-09-26 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4872 417a6e49 2020-09-26 stsp "[-d] [name]\n",
4873 417a6e49 2020-09-26 stsp getprogname());
4874 417a6e49 2020-09-26 stsp exit(1);
4875 417a6e49 2020-09-26 stsp }
4876 417a6e49 2020-09-26 stsp
4877 417a6e49 2020-09-26 stsp static const struct got_error *
4878 417a6e49 2020-09-26 stsp list_refs(struct got_repository *repo, const char *refname)
4879 417a6e49 2020-09-26 stsp {
4880 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
4881 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
4882 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
4883 417a6e49 2020-09-26 stsp
4884 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
4885 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4886 417a6e49 2020-09-26 stsp if (err)
4887 417a6e49 2020-09-26 stsp return err;
4888 417a6e49 2020-09-26 stsp
4889 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4890 417a6e49 2020-09-26 stsp char *refstr;
4891 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
4892 417a6e49 2020-09-26 stsp if (refstr == NULL)
4893 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
4894 417a6e49 2020-09-26 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4895 417a6e49 2020-09-26 stsp free(refstr);
4896 417a6e49 2020-09-26 stsp }
4897 417a6e49 2020-09-26 stsp
4898 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
4899 417a6e49 2020-09-26 stsp return NULL;
4900 417a6e49 2020-09-26 stsp }
4901 417a6e49 2020-09-26 stsp
4902 417a6e49 2020-09-26 stsp static const struct got_error *
4903 417a6e49 2020-09-26 stsp delete_ref(struct got_repository *repo, const char *refname)
4904 417a6e49 2020-09-26 stsp {
4905 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4906 417a6e49 2020-09-26 stsp struct got_reference *ref;
4907 417a6e49 2020-09-26 stsp
4908 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4909 417a6e49 2020-09-26 stsp if (err)
4910 417a6e49 2020-09-26 stsp return err;
4911 417a6e49 2020-09-26 stsp
4912 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
4913 417a6e49 2020-09-26 stsp got_ref_close(ref);
4914 417a6e49 2020-09-26 stsp return err;
4915 417a6e49 2020-09-26 stsp }
4916 417a6e49 2020-09-26 stsp
4917 417a6e49 2020-09-26 stsp static const struct got_error *
4918 417a6e49 2020-09-26 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4919 417a6e49 2020-09-26 stsp {
4920 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4921 417a6e49 2020-09-26 stsp struct got_object_id *id;
4922 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4923 417a6e49 2020-09-26 stsp
4924 417a6e49 2020-09-26 stsp /*
4925 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4926 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4927 417a6e49 2020-09-26 stsp * an unintended typo.
4928 417a6e49 2020-09-26 stsp */
4929 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4930 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4931 417a6e49 2020-09-26 stsp
4932 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4933 417a6e49 2020-09-26 stsp repo);
4934 417a6e49 2020-09-26 stsp if (err) {
4935 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
4936 417a6e49 2020-09-26 stsp
4937 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4938 417a6e49 2020-09-26 stsp return err;
4939 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4940 417a6e49 2020-09-26 stsp if (err)
4941 417a6e49 2020-09-26 stsp return err;
4942 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, target_ref);
4943 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4944 417a6e49 2020-09-26 stsp if (err)
4945 417a6e49 2020-09-26 stsp return err;
4946 417a6e49 2020-09-26 stsp }
4947 417a6e49 2020-09-26 stsp
4948 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, id);
4949 417a6e49 2020-09-26 stsp if (err)
4950 417a6e49 2020-09-26 stsp goto done;
4951 417a6e49 2020-09-26 stsp
4952 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4953 417a6e49 2020-09-26 stsp done:
4954 417a6e49 2020-09-26 stsp if (ref)
4955 417a6e49 2020-09-26 stsp got_ref_close(ref);
4956 417a6e49 2020-09-26 stsp free(id);
4957 417a6e49 2020-09-26 stsp return err;
4958 417a6e49 2020-09-26 stsp }
4959 417a6e49 2020-09-26 stsp
4960 417a6e49 2020-09-26 stsp static const struct got_error *
4961 417a6e49 2020-09-26 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4962 417a6e49 2020-09-26 stsp {
4963 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4964 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4965 417a6e49 2020-09-26 stsp struct got_reference *target_ref = NULL;
4966 417a6e49 2020-09-26 stsp
4967 417a6e49 2020-09-26 stsp /*
4968 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4969 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4970 417a6e49 2020-09-26 stsp * an unintended typo.
4971 417a6e49 2020-09-26 stsp */
4972 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4973 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4974 417a6e49 2020-09-26 stsp
4975 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4976 417a6e49 2020-09-26 stsp if (err)
4977 417a6e49 2020-09-26 stsp return err;
4978 417a6e49 2020-09-26 stsp
4979 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4980 417a6e49 2020-09-26 stsp if (err)
4981 417a6e49 2020-09-26 stsp goto done;
4982 417a6e49 2020-09-26 stsp
4983 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4984 417a6e49 2020-09-26 stsp done:
4985 417a6e49 2020-09-26 stsp if (target_ref)
4986 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4987 417a6e49 2020-09-26 stsp if (ref)
4988 417a6e49 2020-09-26 stsp got_ref_close(ref);
4989 417a6e49 2020-09-26 stsp return err;
4990 417a6e49 2020-09-26 stsp }
4991 417a6e49 2020-09-26 stsp
4992 417a6e49 2020-09-26 stsp static const struct got_error *
4993 417a6e49 2020-09-26 stsp cmd_ref(int argc, char *argv[])
4994 417a6e49 2020-09-26 stsp {
4995 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4996 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4997 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4998 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
4999 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_delete = 0;
5000 417a6e49 2020-09-26 stsp const char *obj_arg = NULL, *symref_target= NULL;
5001 417a6e49 2020-09-26 stsp char *refname = NULL;
5002 417a6e49 2020-09-26 stsp
5003 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5004 417a6e49 2020-09-26 stsp switch (ch) {
5005 417a6e49 2020-09-26 stsp case 'c':
5006 417a6e49 2020-09-26 stsp obj_arg = optarg;
5007 417a6e49 2020-09-26 stsp break;
5008 417a6e49 2020-09-26 stsp case 'd':
5009 417a6e49 2020-09-26 stsp do_delete = 1;
5010 417a6e49 2020-09-26 stsp break;
5011 417a6e49 2020-09-26 stsp case 'r':
5012 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5013 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5014 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5015 417a6e49 2020-09-26 stsp optarg);
5016 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5017 417a6e49 2020-09-26 stsp break;
5018 417a6e49 2020-09-26 stsp case 'l':
5019 417a6e49 2020-09-26 stsp do_list = 1;
5020 417a6e49 2020-09-26 stsp break;
5021 417a6e49 2020-09-26 stsp case 's':
5022 417a6e49 2020-09-26 stsp symref_target = optarg;
5023 417a6e49 2020-09-26 stsp break;
5024 417a6e49 2020-09-26 stsp default:
5025 417a6e49 2020-09-26 stsp usage_ref();
5026 417a6e49 2020-09-26 stsp /* NOTREACHED */
5027 417a6e49 2020-09-26 stsp }
5028 417a6e49 2020-09-26 stsp }
5029 417a6e49 2020-09-26 stsp
5030 417a6e49 2020-09-26 stsp if (obj_arg && do_list)
5031 417a6e49 2020-09-26 stsp errx(1, "-c and -l options are mutually exclusive");
5032 417a6e49 2020-09-26 stsp if (obj_arg && do_delete)
5033 417a6e49 2020-09-26 stsp errx(1, "-c and -d options are mutually exclusive");
5034 417a6e49 2020-09-26 stsp if (obj_arg && symref_target)
5035 417a6e49 2020-09-26 stsp errx(1, "-c and -s options are mutually exclusive");
5036 417a6e49 2020-09-26 stsp if (symref_target && do_delete)
5037 417a6e49 2020-09-26 stsp errx(1, "-s and -d options are mutually exclusive");
5038 417a6e49 2020-09-26 stsp if (symref_target && do_list)
5039 417a6e49 2020-09-26 stsp errx(1, "-s and -l options are mutually exclusive");
5040 417a6e49 2020-09-26 stsp if (do_delete && do_list)
5041 417a6e49 2020-09-26 stsp errx(1, "-d and -l options are mutually exclusive");
5042 417a6e49 2020-09-26 stsp
5043 417a6e49 2020-09-26 stsp argc -= optind;
5044 417a6e49 2020-09-26 stsp argv += optind;
5045 417a6e49 2020-09-26 stsp
5046 417a6e49 2020-09-26 stsp if (do_list) {
5047 417a6e49 2020-09-26 stsp if (argc != 0 && argc != 1)
5048 417a6e49 2020-09-26 stsp usage_ref();
5049 417a6e49 2020-09-26 stsp if (argc == 1) {
5050 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5051 417a6e49 2020-09-26 stsp if (refname == NULL) {
5052 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5053 417a6e49 2020-09-26 stsp goto done;
5054 417a6e49 2020-09-26 stsp }
5055 417a6e49 2020-09-26 stsp }
5056 417a6e49 2020-09-26 stsp } else {
5057 417a6e49 2020-09-26 stsp if (argc != 1)
5058 417a6e49 2020-09-26 stsp usage_ref();
5059 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5060 417a6e49 2020-09-26 stsp if (refname == NULL) {
5061 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5062 417a6e49 2020-09-26 stsp goto done;
5063 417a6e49 2020-09-26 stsp }
5064 417a6e49 2020-09-26 stsp }
5065 417a6e49 2020-09-26 stsp
5066 417a6e49 2020-09-26 stsp if (refname)
5067 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(refname);
5068 417a6e49 2020-09-26 stsp
5069 417a6e49 2020-09-26 stsp #ifndef PROFILE
5070 417a6e49 2020-09-26 stsp if (do_list) {
5071 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5072 417a6e49 2020-09-26 stsp NULL) == -1)
5073 417a6e49 2020-09-26 stsp err(1, "pledge");
5074 417a6e49 2020-09-26 stsp } else {
5075 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5076 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5077 417a6e49 2020-09-26 stsp err(1, "pledge");
5078 417a6e49 2020-09-26 stsp }
5079 417a6e49 2020-09-26 stsp #endif
5080 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5081 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5082 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5083 417a6e49 2020-09-26 stsp goto done;
5084 417a6e49 2020-09-26 stsp }
5085 417a6e49 2020-09-26 stsp
5086 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5087 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5088 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5089 417a6e49 2020-09-26 stsp goto done;
5090 417a6e49 2020-09-26 stsp else
5091 417a6e49 2020-09-26 stsp error = NULL;
5092 417a6e49 2020-09-26 stsp if (worktree) {
5093 417a6e49 2020-09-26 stsp repo_path =
5094 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5095 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5096 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5097 417a6e49 2020-09-26 stsp if (error)
5098 417a6e49 2020-09-26 stsp goto done;
5099 417a6e49 2020-09-26 stsp } else {
5100 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5101 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5102 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5103 417a6e49 2020-09-26 stsp goto done;
5104 417a6e49 2020-09-26 stsp }
5105 417a6e49 2020-09-26 stsp }
5106 417a6e49 2020-09-26 stsp }
5107 417a6e49 2020-09-26 stsp
5108 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5109 417a6e49 2020-09-26 stsp if (error != NULL)
5110 417a6e49 2020-09-26 stsp goto done;
5111 417a6e49 2020-09-26 stsp
5112 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5113 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5114 417a6e49 2020-09-26 stsp if (error)
5115 417a6e49 2020-09-26 stsp goto done;
5116 417a6e49 2020-09-26 stsp
5117 417a6e49 2020-09-26 stsp if (do_list)
5118 417a6e49 2020-09-26 stsp error = list_refs(repo, refname);
5119 417a6e49 2020-09-26 stsp else if (do_delete)
5120 417a6e49 2020-09-26 stsp error = delete_ref(repo, refname);
5121 417a6e49 2020-09-26 stsp else if (symref_target)
5122 417a6e49 2020-09-26 stsp error = add_symref(repo, refname, symref_target);
5123 417a6e49 2020-09-26 stsp else {
5124 417a6e49 2020-09-26 stsp if (obj_arg == NULL)
5125 417a6e49 2020-09-26 stsp usage_ref();
5126 417a6e49 2020-09-26 stsp error = add_ref(repo, refname, obj_arg);
5127 417a6e49 2020-09-26 stsp }
5128 417a6e49 2020-09-26 stsp done:
5129 417a6e49 2020-09-26 stsp free(refname);
5130 417a6e49 2020-09-26 stsp if (repo)
5131 417a6e49 2020-09-26 stsp got_repo_close(repo);
5132 417a6e49 2020-09-26 stsp if (worktree)
5133 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5134 417a6e49 2020-09-26 stsp free(cwd);
5135 417a6e49 2020-09-26 stsp free(repo_path);
5136 417a6e49 2020-09-26 stsp return error;
5137 417a6e49 2020-09-26 stsp }
5138 417a6e49 2020-09-26 stsp
5139 417a6e49 2020-09-26 stsp __dead static void
5140 417a6e49 2020-09-26 stsp usage_branch(void)
5141 417a6e49 2020-09-26 stsp {
5142 417a6e49 2020-09-26 stsp fprintf(stderr,
5143 417a6e49 2020-09-26 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5144 417a6e49 2020-09-26 stsp "[name]\n", getprogname());
5145 417a6e49 2020-09-26 stsp exit(1);
5146 417a6e49 2020-09-26 stsp }
5147 417a6e49 2020-09-26 stsp
5148 417a6e49 2020-09-26 stsp static const struct got_error *
5149 417a6e49 2020-09-26 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5150 417a6e49 2020-09-26 stsp struct got_reference *ref)
5151 417a6e49 2020-09-26 stsp {
5152 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5153 417a6e49 2020-09-26 stsp const char *refname, *marker = " ";
5154 417a6e49 2020-09-26 stsp char *refstr;
5155 417a6e49 2020-09-26 stsp
5156 417a6e49 2020-09-26 stsp refname = got_ref_get_name(ref);
5157 417a6e49 2020-09-26 stsp if (worktree && strcmp(refname,
5158 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5159 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
5160 417a6e49 2020-09-26 stsp
5161 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
5162 417a6e49 2020-09-26 stsp if (err)
5163 417a6e49 2020-09-26 stsp return err;
5164 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id,
5165 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5166 417a6e49 2020-09-26 stsp marker = "* ";
5167 417a6e49 2020-09-26 stsp else
5168 417a6e49 2020-09-26 stsp marker = "~ ";
5169 417a6e49 2020-09-26 stsp free(id);
5170 417a6e49 2020-09-26 stsp }
5171 417a6e49 2020-09-26 stsp
5172 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5173 417a6e49 2020-09-26 stsp refname += 11;
5174 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5175 417a6e49 2020-09-26 stsp refname += 18;
5176 417a6e49 2020-09-26 stsp
5177 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(ref);
5178 417a6e49 2020-09-26 stsp if (refstr == NULL)
5179 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
5180 417a6e49 2020-09-26 stsp
5181 417a6e49 2020-09-26 stsp printf("%s%s: %s\n", marker, refname, refstr);
5182 417a6e49 2020-09-26 stsp free(refstr);
5183 417a6e49 2020-09-26 stsp return NULL;
5184 417a6e49 2020-09-26 stsp }
5185 417a6e49 2020-09-26 stsp
5186 417a6e49 2020-09-26 stsp static const struct got_error *
5187 417a6e49 2020-09-26 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5188 417a6e49 2020-09-26 stsp {
5189 417a6e49 2020-09-26 stsp const char *refname;
5190 417a6e49 2020-09-26 stsp
5191 417a6e49 2020-09-26 stsp if (worktree == NULL)
5192 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5193 417a6e49 2020-09-26 stsp
5194 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
5195 417a6e49 2020-09-26 stsp
5196 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5197 417a6e49 2020-09-26 stsp refname += 11;
5198 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5199 417a6e49 2020-09-26 stsp refname += 18;
5200 417a6e49 2020-09-26 stsp
5201 417a6e49 2020-09-26 stsp printf("%s\n", refname);
5202 417a6e49 2020-09-26 stsp
5203 417a6e49 2020-09-26 stsp return NULL;
5204 417a6e49 2020-09-26 stsp }
5205 417a6e49 2020-09-26 stsp
5206 417a6e49 2020-09-26 stsp static const struct got_error *
5207 417a6e49 2020-09-26 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5208 417a6e49 2020-09-26 stsp {
5209 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5210 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5211 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5212 417a6e49 2020-09-26 stsp struct got_reference *temp_ref = NULL;
5213 417a6e49 2020-09-26 stsp int rebase_in_progress, histedit_in_progress;
5214 417a6e49 2020-09-26 stsp
5215 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5216 417a6e49 2020-09-26 stsp
5217 417a6e49 2020-09-26 stsp if (worktree) {
5218 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5219 417a6e49 2020-09-26 stsp worktree);
5220 417a6e49 2020-09-26 stsp if (err)
5221 417a6e49 2020-09-26 stsp return err;
5222 417a6e49 2020-09-26 stsp
5223 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5224 417a6e49 2020-09-26 stsp worktree);
5225 417a6e49 2020-09-26 stsp if (err)
5226 417a6e49 2020-09-26 stsp return err;
5227 417a6e49 2020-09-26 stsp
5228 417a6e49 2020-09-26 stsp if (rebase_in_progress || histedit_in_progress) {
5229 417a6e49 2020-09-26 stsp err = got_ref_open(&temp_ref, repo,
5230 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
5231 417a6e49 2020-09-26 stsp if (err)
5232 417a6e49 2020-09-26 stsp return err;
5233 417a6e49 2020-09-26 stsp list_branch(repo, worktree, temp_ref);
5234 417a6e49 2020-09-26 stsp got_ref_close(temp_ref);
5235 417a6e49 2020-09-26 stsp }
5236 417a6e49 2020-09-26 stsp }
5237 417a6e49 2020-09-26 stsp
5238 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/heads",
5239 417a6e49 2020-09-26 stsp got_ref_cmp_by_name, NULL);
5240 417a6e49 2020-09-26 stsp if (err)
5241 417a6e49 2020-09-26 stsp return err;
5242 417a6e49 2020-09-26 stsp
5243 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5244 417a6e49 2020-09-26 stsp list_branch(repo, worktree, re->ref);
5245 417a6e49 2020-09-26 stsp
5246 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5247 417a6e49 2020-09-26 stsp return NULL;
5248 417a6e49 2020-09-26 stsp }
5249 417a6e49 2020-09-26 stsp
5250 417a6e49 2020-09-26 stsp static const struct got_error *
5251 417a6e49 2020-09-26 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5252 417a6e49 2020-09-26 stsp const char *branch_name)
5253 417a6e49 2020-09-26 stsp {
5254 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5255 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5256 417a6e49 2020-09-26 stsp char *refname;
5257 417a6e49 2020-09-26 stsp
5258 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5259 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
5260 417a6e49 2020-09-26 stsp
5261 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5262 417a6e49 2020-09-26 stsp if (err)
5263 417a6e49 2020-09-26 stsp goto done;
5264 417a6e49 2020-09-26 stsp
5265 417a6e49 2020-09-26 stsp if (worktree &&
5266 417a6e49 2020-09-26 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5267 417a6e49 2020-09-26 stsp got_ref_get_name(ref)) == 0) {
5268 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5269 417a6e49 2020-09-26 stsp "will not delete this work tree's current branch");
5270 417a6e49 2020-09-26 stsp goto done;
5271 417a6e49 2020-09-26 stsp }
5272 417a6e49 2020-09-26 stsp
5273 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
5274 417a6e49 2020-09-26 stsp done:
5275 417a6e49 2020-09-26 stsp if (ref)
5276 417a6e49 2020-09-26 stsp got_ref_close(ref);
5277 417a6e49 2020-09-26 stsp free(refname);
5278 417a6e49 2020-09-26 stsp return err;
5279 417a6e49 2020-09-26 stsp }
5280 417a6e49 2020-09-26 stsp
5281 417a6e49 2020-09-26 stsp static const struct got_error *
5282 417a6e49 2020-09-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5283 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id)
5284 417a6e49 2020-09-26 stsp {
5285 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5286 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5287 417a6e49 2020-09-26 stsp char *base_refname = NULL, *refname = NULL;
5288 417a6e49 2020-09-26 stsp
5289 417a6e49 2020-09-26 stsp /*
5290 417a6e49 2020-09-26 stsp * Don't let the user create a branch name with a leading '-'.
5291 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5292 417a6e49 2020-09-26 stsp * an unintended typo.
5293 417a6e49 2020-09-26 stsp */
5294 417a6e49 2020-09-26 stsp if (branch_name[0] == '-')
5295 417a6e49 2020-09-26 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5296 417a6e49 2020-09-26 stsp
5297 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5298 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5299 417a6e49 2020-09-26 stsp goto done;
5300 417a6e49 2020-09-26 stsp }
5301 417a6e49 2020-09-26 stsp
5302 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5303 417a6e49 2020-09-26 stsp if (err == NULL) {
5304 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5305 417a6e49 2020-09-26 stsp goto done;
5306 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5307 417a6e49 2020-09-26 stsp goto done;
5308 417a6e49 2020-09-26 stsp
5309 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5310 417a6e49 2020-09-26 stsp if (err)
5311 417a6e49 2020-09-26 stsp goto done;
5312 417a6e49 2020-09-26 stsp
5313 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5314 417a6e49 2020-09-26 stsp done:
5315 417a6e49 2020-09-26 stsp if (ref)
5316 417a6e49 2020-09-26 stsp got_ref_close(ref);
5317 417a6e49 2020-09-26 stsp free(base_refname);
5318 417a6e49 2020-09-26 stsp free(refname);
5319 417a6e49 2020-09-26 stsp return err;
5320 417a6e49 2020-09-26 stsp }
5321 417a6e49 2020-09-26 stsp
5322 417a6e49 2020-09-26 stsp static const struct got_error *
5323 417a6e49 2020-09-26 stsp cmd_branch(int argc, char *argv[])
5324 417a6e49 2020-09-26 stsp {
5325 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5326 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5327 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5328 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
5329 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5330 417a6e49 2020-09-26 stsp const char *delref = NULL, *commit_id_arg = NULL;
5331 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5332 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
5333 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
5334 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
5335 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
5336 417a6e49 2020-09-26 stsp
5337 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
5338 417a6e49 2020-09-26 stsp
5339 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5340 417a6e49 2020-09-26 stsp switch (ch) {
5341 417a6e49 2020-09-26 stsp case 'c':
5342 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5343 417a6e49 2020-09-26 stsp break;
5344 417a6e49 2020-09-26 stsp case 'd':
5345 417a6e49 2020-09-26 stsp delref = optarg;
5346 417a6e49 2020-09-26 stsp break;
5347 417a6e49 2020-09-26 stsp case 'r':
5348 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5349 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5350 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5351 417a6e49 2020-09-26 stsp optarg);
5352 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5353 417a6e49 2020-09-26 stsp break;
5354 417a6e49 2020-09-26 stsp case 'l':
5355 417a6e49 2020-09-26 stsp do_list = 1;
5356 417a6e49 2020-09-26 stsp break;
5357 417a6e49 2020-09-26 stsp case 'n':
5358 417a6e49 2020-09-26 stsp do_update = 0;
5359 417a6e49 2020-09-26 stsp break;
5360 417a6e49 2020-09-26 stsp default:
5361 417a6e49 2020-09-26 stsp usage_branch();
5362 417a6e49 2020-09-26 stsp /* NOTREACHED */
5363 417a6e49 2020-09-26 stsp }
5364 417a6e49 2020-09-26 stsp }
5365 417a6e49 2020-09-26 stsp
5366 417a6e49 2020-09-26 stsp if (do_list && delref)
5367 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
5368 417a6e49 2020-09-26 stsp
5369 417a6e49 2020-09-26 stsp argc -= optind;
5370 417a6e49 2020-09-26 stsp argv += optind;
5371 417a6e49 2020-09-26 stsp
5372 417a6e49 2020-09-26 stsp if (!do_list && !delref && argc == 0)
5373 417a6e49 2020-09-26 stsp do_show = 1;
5374 417a6e49 2020-09-26 stsp
5375 417a6e49 2020-09-26 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5376 417a6e49 2020-09-26 stsp errx(1, "-c option can only be used when creating a branch");
5377 417a6e49 2020-09-26 stsp
5378 417a6e49 2020-09-26 stsp if (do_list || delref) {
5379 417a6e49 2020-09-26 stsp if (argc > 0)
5380 417a6e49 2020-09-26 stsp usage_branch();
5381 417a6e49 2020-09-26 stsp } else if (!do_show && argc != 1)
5382 417a6e49 2020-09-26 stsp usage_branch();
5383 417a6e49 2020-09-26 stsp
5384 417a6e49 2020-09-26 stsp #ifndef PROFILE
5385 417a6e49 2020-09-26 stsp if (do_list || do_show) {
5386 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5387 417a6e49 2020-09-26 stsp NULL) == -1)
5388 417a6e49 2020-09-26 stsp err(1, "pledge");
5389 417a6e49 2020-09-26 stsp } else {
5390 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5391 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5392 417a6e49 2020-09-26 stsp err(1, "pledge");
5393 417a6e49 2020-09-26 stsp }
5394 417a6e49 2020-09-26 stsp #endif
5395 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5396 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5397 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5398 417a6e49 2020-09-26 stsp goto done;
5399 417a6e49 2020-09-26 stsp }
5400 417a6e49 2020-09-26 stsp
5401 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5402 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5403 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5404 417a6e49 2020-09-26 stsp goto done;
5405 417a6e49 2020-09-26 stsp else
5406 417a6e49 2020-09-26 stsp error = NULL;
5407 417a6e49 2020-09-26 stsp if (worktree) {
5408 417a6e49 2020-09-26 stsp repo_path =
5409 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5410 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5411 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5412 417a6e49 2020-09-26 stsp if (error)
5413 417a6e49 2020-09-26 stsp goto done;
5414 417a6e49 2020-09-26 stsp } else {
5415 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5416 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5417 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5418 417a6e49 2020-09-26 stsp goto done;
5419 417a6e49 2020-09-26 stsp }
5420 417a6e49 2020-09-26 stsp }
5421 417a6e49 2020-09-26 stsp }
5422 417a6e49 2020-09-26 stsp
5423 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5424 417a6e49 2020-09-26 stsp if (error != NULL)
5425 417a6e49 2020-09-26 stsp goto done;
5426 417a6e49 2020-09-26 stsp
5427 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5428 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5429 417a6e49 2020-09-26 stsp if (error)
5430 417a6e49 2020-09-26 stsp goto done;
5431 417a6e49 2020-09-26 stsp
5432 417a6e49 2020-09-26 stsp if (do_show)
5433 417a6e49 2020-09-26 stsp error = show_current_branch(repo, worktree);
5434 417a6e49 2020-09-26 stsp else if (do_list)
5435 417a6e49 2020-09-26 stsp error = list_branches(repo, worktree);
5436 417a6e49 2020-09-26 stsp else if (delref)
5437 417a6e49 2020-09-26 stsp error = delete_branch(repo, worktree, delref);
5438 417a6e49 2020-09-26 stsp else {
5439 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL)
5440 417a6e49 2020-09-26 stsp commit_id_arg = worktree ?
5441 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) :
5442 417a6e49 2020-09-26 stsp GOT_REF_HEAD;
5443 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
5444 417a6e49 2020-09-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5445 417a6e49 2020-09-26 stsp if (error)
5446 417a6e49 2020-09-26 stsp goto done;
5447 417a6e49 2020-09-26 stsp error = add_branch(repo, argv[0], commit_id);
5448 417a6e49 2020-09-26 stsp if (error)
5449 417a6e49 2020-09-26 stsp goto done;
5450 417a6e49 2020-09-26 stsp if (worktree && do_update) {
5451 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
5452 417a6e49 2020-09-26 stsp char *branch_refname = NULL;
5453 417a6e49 2020-09-26 stsp
5454 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5455 417a6e49 2020-09-26 stsp if (error)
5456 417a6e49 2020-09-26 stsp goto done;
5457 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5458 417a6e49 2020-09-26 stsp worktree);
5459 417a6e49 2020-09-26 stsp if (error)
5460 417a6e49 2020-09-26 stsp goto done;
5461 417a6e49 2020-09-26 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5462 417a6e49 2020-09-26 stsp == -1) {
5463 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
5464 417a6e49 2020-09-26 stsp goto done;
5465 417a6e49 2020-09-26 stsp }
5466 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5467 417a6e49 2020-09-26 stsp free(branch_refname);
5468 417a6e49 2020-09-26 stsp if (error)
5469 417a6e49 2020-09-26 stsp goto done;
5470 417a6e49 2020-09-26 stsp error = switch_head_ref(ref, commit_id, worktree,
5471 417a6e49 2020-09-26 stsp repo);
5472 417a6e49 2020-09-26 stsp if (error)
5473 417a6e49 2020-09-26 stsp goto done;
5474 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5475 417a6e49 2020-09-26 stsp commit_id);
5476 417a6e49 2020-09-26 stsp if (error)
5477 417a6e49 2020-09-26 stsp goto done;
5478 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
5479 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths,
5480 417a6e49 2020-09-26 stsp repo, update_progress, &upa, check_cancelled,
5481 417a6e49 2020-09-26 stsp NULL);
5482 417a6e49 2020-09-26 stsp if (error)
5483 417a6e49 2020-09-26 stsp goto done;
5484 417a6e49 2020-09-26 stsp if (upa.did_something)
5485 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
5486 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
5487 417a6e49 2020-09-26 stsp }
5488 417a6e49 2020-09-26 stsp }
5489 417a6e49 2020-09-26 stsp done:
5490 417a6e49 2020-09-26 stsp if (ref)
5491 417a6e49 2020-09-26 stsp got_ref_close(ref);
5492 417a6e49 2020-09-26 stsp if (repo)
5493 417a6e49 2020-09-26 stsp got_repo_close(repo);
5494 417a6e49 2020-09-26 stsp if (worktree)
5495 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5496 417a6e49 2020-09-26 stsp free(cwd);
5497 417a6e49 2020-09-26 stsp free(repo_path);
5498 417a6e49 2020-09-26 stsp free(commit_id);
5499 417a6e49 2020-09-26 stsp free(commit_id_str);
5500 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
5501 417a6e49 2020-09-26 stsp free((char *)pe->path);
5502 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
5503 417a6e49 2020-09-26 stsp return error;
5504 417a6e49 2020-09-26 stsp }
5505 417a6e49 2020-09-26 stsp
5506 417a6e49 2020-09-26 stsp
5507 417a6e49 2020-09-26 stsp __dead static void
5508 417a6e49 2020-09-26 stsp usage_tag(void)
5509 417a6e49 2020-09-26 stsp {
5510 417a6e49 2020-09-26 stsp fprintf(stderr,
5511 417a6e49 2020-09-26 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5512 417a6e49 2020-09-26 stsp "[-m message] name\n", getprogname());
5513 417a6e49 2020-09-26 stsp exit(1);
5514 417a6e49 2020-09-26 stsp }
5515 417a6e49 2020-09-26 stsp
5516 417a6e49 2020-09-26 stsp #if 0
5517 417a6e49 2020-09-26 stsp static const struct got_error *
5518 417a6e49 2020-09-26 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5519 417a6e49 2020-09-26 stsp {
5520 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5521 417a6e49 2020-09-26 stsp struct got_reflist_entry *re, *se, *new;
5522 417a6e49 2020-09-26 stsp struct got_object_id *re_id, *se_id;
5523 417a6e49 2020-09-26 stsp struct got_tag_object *re_tag, *se_tag;
5524 417a6e49 2020-09-26 stsp time_t re_time, se_time;
5525 417a6e49 2020-09-26 stsp
5526 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5527 417a6e49 2020-09-26 stsp se = SIMPLEQ_FIRST(sorted);
5528 417a6e49 2020-09-26 stsp if (se == NULL) {
5529 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5530 417a6e49 2020-09-26 stsp if (err)
5531 417a6e49 2020-09-26 stsp return err;
5532 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5533 417a6e49 2020-09-26 stsp continue;
5534 417a6e49 2020-09-26 stsp } else {
5535 417a6e49 2020-09-26 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5536 417a6e49 2020-09-26 stsp if (err)
5537 417a6e49 2020-09-26 stsp break;
5538 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5539 417a6e49 2020-09-26 stsp free(re_id);
5540 417a6e49 2020-09-26 stsp if (err)
5541 417a6e49 2020-09-26 stsp break;
5542 417a6e49 2020-09-26 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5543 417a6e49 2020-09-26 stsp got_object_tag_close(re_tag);
5544 417a6e49 2020-09-26 stsp }
5545 417a6e49 2020-09-26 stsp
5546 417a6e49 2020-09-26 stsp while (se) {
5547 417a6e49 2020-09-26 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5548 417a6e49 2020-09-26 stsp if (err)
5549 417a6e49 2020-09-26 stsp break;
5550 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5551 417a6e49 2020-09-26 stsp free(se_id);
5552 417a6e49 2020-09-26 stsp if (err)
5553 417a6e49 2020-09-26 stsp break;
5554 417a6e49 2020-09-26 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5555 417a6e49 2020-09-26 stsp got_object_tag_close(se_tag);
5556 417a6e49 2020-09-26 stsp
5557 417a6e49 2020-09-26 stsp if (se_time > re_time) {
5558 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5559 417a6e49 2020-09-26 stsp if (err)
5560 417a6e49 2020-09-26 stsp return err;
5561 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5562 417a6e49 2020-09-26 stsp break;
5563 417a6e49 2020-09-26 stsp }
5564 417a6e49 2020-09-26 stsp se = SIMPLEQ_NEXT(se, entry);
5565 417a6e49 2020-09-26 stsp continue;
5566 417a6e49 2020-09-26 stsp }
5567 417a6e49 2020-09-26 stsp }
5568 417a6e49 2020-09-26 stsp done:
5569 417a6e49 2020-09-26 stsp return err;
5570 417a6e49 2020-09-26 stsp }
5571 417a6e49 2020-09-26 stsp #endif
5572 417a6e49 2020-09-26 stsp
5573 417a6e49 2020-09-26 stsp static const struct got_error *
5574 417a6e49 2020-09-26 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5575 417a6e49 2020-09-26 stsp {
5576 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5577 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5578 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5579 417a6e49 2020-09-26 stsp
5580 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5581 417a6e49 2020-09-26 stsp
5582 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5583 417a6e49 2020-09-26 stsp if (err)
5584 417a6e49 2020-09-26 stsp return err;
5585 417a6e49 2020-09-26 stsp
5586 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5587 417a6e49 2020-09-26 stsp const char *refname;
5588 417a6e49 2020-09-26 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5589 417a6e49 2020-09-26 stsp char datebuf[26];
5590 417a6e49 2020-09-26 stsp const char *tagger;
5591 417a6e49 2020-09-26 stsp time_t tagger_time;
5592 417a6e49 2020-09-26 stsp struct got_object_id *id;
5593 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
5594 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
5595 417a6e49 2020-09-26 stsp
5596 417a6e49 2020-09-26 stsp refname = got_ref_get_name(re->ref);
5597 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5598 417a6e49 2020-09-26 stsp continue;
5599 417a6e49 2020-09-26 stsp refname += 10;
5600 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
5601 417a6e49 2020-09-26 stsp if (refstr == NULL) {
5602 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_ref_to_str");
5603 417a6e49 2020-09-26 stsp break;
5604 417a6e49 2020-09-26 stsp }
5605 417a6e49 2020-09-26 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5606 417a6e49 2020-09-26 stsp free(refstr);
5607 417a6e49 2020-09-26 stsp
5608 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, re->ref);
5609 417a6e49 2020-09-26 stsp if (err)
5610 417a6e49 2020-09-26 stsp break;
5611 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
5612 417a6e49 2020-09-26 stsp if (err) {
5613 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5614 417a6e49 2020-09-26 stsp free(id);
5615 417a6e49 2020-09-26 stsp break;
5616 417a6e49 2020-09-26 stsp }
5617 417a6e49 2020-09-26 stsp /* "lightweight" tag */
5618 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
5619 417a6e49 2020-09-26 stsp if (err) {
5620 417a6e49 2020-09-26 stsp free(id);
5621 417a6e49 2020-09-26 stsp break;
5622 417a6e49 2020-09-26 stsp }
5623 417a6e49 2020-09-26 stsp tagger = got_object_commit_get_committer(commit);
5624 417a6e49 2020-09-26 stsp tagger_time =
5625 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit);
5626 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
5627 417a6e49 2020-09-26 stsp free(id);
5628 417a6e49 2020-09-26 stsp if (err)
5629 417a6e49 2020-09-26 stsp break;
5630 417a6e49 2020-09-26 stsp } else {
5631 417a6e49 2020-09-26 stsp free(id);
5632 417a6e49 2020-09-26 stsp tagger = got_object_tag_get_tagger(tag);
5633 417a6e49 2020-09-26 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5634 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
5635 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
5636 417a6e49 2020-09-26 stsp if (err)
5637 417a6e49 2020-09-26 stsp break;
5638 417a6e49 2020-09-26 stsp }
5639 417a6e49 2020-09-26 stsp printf("from: %s\n", tagger);
5640 417a6e49 2020-09-26 stsp datestr = get_datestr(&tagger_time, datebuf);
5641 417a6e49 2020-09-26 stsp if (datestr)
5642 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
5643 417a6e49 2020-09-26 stsp if (commit)
5644 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5645 417a6e49 2020-09-26 stsp else {
5646 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
5647 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
5648 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5649 417a6e49 2020-09-26 stsp id_str);
5650 417a6e49 2020-09-26 stsp break;
5651 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
5652 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5653 417a6e49 2020-09-26 stsp id_str);
5654 417a6e49 2020-09-26 stsp break;
5655 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
5656 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5657 417a6e49 2020-09-26 stsp id_str);
5658 417a6e49 2020-09-26 stsp break;
5659 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
5660 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5661 417a6e49 2020-09-26 stsp id_str);
5662 417a6e49 2020-09-26 stsp break;
5663 417a6e49 2020-09-26 stsp default:
5664 417a6e49 2020-09-26 stsp break;
5665 417a6e49 2020-09-26 stsp }
5666 417a6e49 2020-09-26 stsp }
5667 417a6e49 2020-09-26 stsp free(id_str);
5668 417a6e49 2020-09-26 stsp if (commit) {
5669 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5670 417a6e49 2020-09-26 stsp if (err)
5671 417a6e49 2020-09-26 stsp break;
5672 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
5673 417a6e49 2020-09-26 stsp } else {
5674 417a6e49 2020-09-26 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5675 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
5676 417a6e49 2020-09-26 stsp if (tagmsg0 == NULL) {
5677 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5678 417a6e49 2020-09-26 stsp break;
5679 417a6e49 2020-09-26 stsp }
5680 417a6e49 2020-09-26 stsp }
5681 417a6e49 2020-09-26 stsp
5682 417a6e49 2020-09-26 stsp tagmsg = tagmsg0;
5683 417a6e49 2020-09-26 stsp do {
5684 417a6e49 2020-09-26 stsp line = strsep(&tagmsg, "\n");
5685 417a6e49 2020-09-26 stsp if (line)
5686 417a6e49 2020-09-26 stsp printf(" %s\n", line);
5687 417a6e49 2020-09-26 stsp } while (line);
5688 417a6e49 2020-09-26 stsp free(tagmsg0);
5689 417a6e49 2020-09-26 stsp }
5690 417a6e49 2020-09-26 stsp
5691 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5692 417a6e49 2020-09-26 stsp return NULL;
5693 417a6e49 2020-09-26 stsp }
5694 417a6e49 2020-09-26 stsp
5695 417a6e49 2020-09-26 stsp static const struct got_error *
5696 417a6e49 2020-09-26 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5697 417a6e49 2020-09-26 stsp const char *tag_name, const char *repo_path)
5698 417a6e49 2020-09-26 stsp {
5699 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5700 417a6e49 2020-09-26 stsp char *template = NULL, *initial_content = NULL;
5701 417a6e49 2020-09-26 stsp char *editor = NULL;
5702 417a6e49 2020-09-26 stsp int initial_content_len;
5703 417a6e49 2020-09-26 stsp int fd = -1;
5704 417a6e49 2020-09-26 stsp
5705 417a6e49 2020-09-26 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5706 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5707 417a6e49 2020-09-26 stsp goto done;
5708 417a6e49 2020-09-26 stsp }
5709 417a6e49 2020-09-26 stsp
5710 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
5711 417a6e49 2020-09-26 stsp "\n# tagging commit %s as %s\n",
5712 417a6e49 2020-09-26 stsp commit_id_str, tag_name);
5713 417a6e49 2020-09-26 stsp if (initial_content_len == -1) {
5714 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5715 417a6e49 2020-09-26 stsp goto done;
5716 417a6e49 2020-09-26 stsp }
5717 417a6e49 2020-09-26 stsp
5718 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5719 417a6e49 2020-09-26 stsp if (err)
5720 417a6e49 2020-09-26 stsp goto done;
5721 417a6e49 2020-09-26 stsp
5722 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5723 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", *tagmsg_path);
5724 417a6e49 2020-09-26 stsp goto done;
5725 417a6e49 2020-09-26 stsp }
5726 417a6e49 2020-09-26 stsp
5727 417a6e49 2020-09-26 stsp err = get_editor(&editor);
5728 417a6e49 2020-09-26 stsp if (err)
5729 417a6e49 2020-09-26 stsp goto done;
5730 417a6e49 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5731 417a6e49 2020-09-26 stsp done:
5732 417a6e49 2020-09-26 stsp free(initial_content);
5733 417a6e49 2020-09-26 stsp free(template);
5734 417a6e49 2020-09-26 stsp free(editor);
5735 417a6e49 2020-09-26 stsp
5736 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5737 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", *tagmsg_path);
5738 417a6e49 2020-09-26 stsp
5739 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
5740 417a6e49 2020-09-26 stsp if (err == NULL)
5741 417a6e49 2020-09-26 stsp err = apply_unveil(repo_path, 0, NULL);
5742 417a6e49 2020-09-26 stsp if (err) {
5743 417a6e49 2020-09-26 stsp free(*tagmsg);
5744 417a6e49 2020-09-26 stsp *tagmsg = NULL;
5745 417a6e49 2020-09-26 stsp }
5746 417a6e49 2020-09-26 stsp return err;
5747 417a6e49 2020-09-26 stsp }
5748 417a6e49 2020-09-26 stsp
5749 417a6e49 2020-09-26 stsp static const struct got_error *
5750 417a6e49 2020-09-26 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5751 417a6e49 2020-09-26 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5752 417a6e49 2020-09-26 stsp {
5753 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5754 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5755 417a6e49 2020-09-26 stsp char *label = NULL, *commit_id_str = NULL;
5756 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5757 417a6e49 2020-09-26 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5758 417a6e49 2020-09-26 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5759 417a6e49 2020-09-26 stsp int preserve_tagmsg = 0;
5760 417a6e49 2020-09-26 stsp
5761 417a6e49 2020-09-26 stsp /*
5762 417a6e49 2020-09-26 stsp * Don't let the user create a tag name with a leading '-'.
5763 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5764 417a6e49 2020-09-26 stsp * an unintended typo.
5765 417a6e49 2020-09-26 stsp */
5766 417a6e49 2020-09-26 stsp if (tag_name[0] == '-')
5767 417a6e49 2020-09-26 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5768 417a6e49 2020-09-26 stsp
5769 417a6e49 2020-09-26 stsp err = get_author(&tagger, repo, worktree);
5770 417a6e49 2020-09-26 stsp if (err)
5771 417a6e49 2020-09-26 stsp return err;
5772 417a6e49 2020-09-26 stsp
5773 417a6e49 2020-09-26 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5774 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5775 417a6e49 2020-09-26 stsp if (err)
5776 417a6e49 2020-09-26 stsp goto done;
5777 417a6e49 2020-09-26 stsp
5778 417a6e49 2020-09-26 stsp err = got_object_id_str(&commit_id_str, commit_id);
5779 417a6e49 2020-09-26 stsp if (err)
5780 417a6e49 2020-09-26 stsp goto done;
5781 417a6e49 2020-09-26 stsp
5782 417a6e49 2020-09-26 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5783 417a6e49 2020-09-26 stsp refname = strdup(tag_name);
5784 417a6e49 2020-09-26 stsp if (refname == NULL) {
5785 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5786 417a6e49 2020-09-26 stsp goto done;
5787 417a6e49 2020-09-26 stsp }
5788 417a6e49 2020-09-26 stsp tag_name += 10;
5789 417a6e49 2020-09-26 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5790 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5791 417a6e49 2020-09-26 stsp goto done;
5792 417a6e49 2020-09-26 stsp }
5793 417a6e49 2020-09-26 stsp
5794 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5795 417a6e49 2020-09-26 stsp if (err == NULL) {
5796 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5797 417a6e49 2020-09-26 stsp goto done;
5798 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5799 417a6e49 2020-09-26 stsp goto done;
5800 417a6e49 2020-09-26 stsp
5801 417a6e49 2020-09-26 stsp if (tagmsg_arg == NULL) {
5802 417a6e49 2020-09-26 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5803 417a6e49 2020-09-26 stsp tag_name, got_repo_get_path(repo));
5804 417a6e49 2020-09-26 stsp if (err) {
5805 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5806 417a6e49 2020-09-26 stsp tagmsg_path != NULL)
5807 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5808 417a6e49 2020-09-26 stsp goto done;
5809 417a6e49 2020-09-26 stsp }
5810 417a6e49 2020-09-26 stsp }
5811 417a6e49 2020-09-26 stsp
5812 417a6e49 2020-09-26 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5813 417a6e49 2020-09-26 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5814 417a6e49 2020-09-26 stsp if (err) {
5815 417a6e49 2020-09-26 stsp if (tagmsg_path)
5816 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5817 417a6e49 2020-09-26 stsp goto done;
5818 417a6e49 2020-09-26 stsp }
5819 417a6e49 2020-09-26 stsp
5820 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, tag_id);
5821 417a6e49 2020-09-26 stsp if (err) {
5822 417a6e49 2020-09-26 stsp if (tagmsg_path)
5823 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5824 417a6e49 2020-09-26 stsp goto done;
5825 417a6e49 2020-09-26 stsp }
5826 417a6e49 2020-09-26 stsp
5827 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5828 417a6e49 2020-09-26 stsp if (err) {
5829 417a6e49 2020-09-26 stsp if (tagmsg_path)
5830 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5831 417a6e49 2020-09-26 stsp goto done;
5832 417a6e49 2020-09-26 stsp }
5833 417a6e49 2020-09-26 stsp
5834 417a6e49 2020-09-26 stsp err = got_object_id_str(&tag_id_str, tag_id);
5835 417a6e49 2020-09-26 stsp if (err) {
5836 417a6e49 2020-09-26 stsp if (tagmsg_path)
5837 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5838 417a6e49 2020-09-26 stsp goto done;
5839 417a6e49 2020-09-26 stsp }
5840 417a6e49 2020-09-26 stsp printf("Created tag %s\n", tag_id_str);
5841 417a6e49 2020-09-26 stsp done:
5842 417a6e49 2020-09-26 stsp if (preserve_tagmsg) {
5843 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5844 417a6e49 2020-09-26 stsp getprogname(), tagmsg_path);
5845 417a6e49 2020-09-26 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5846 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5847 417a6e49 2020-09-26 stsp free(tag_id_str);
5848 417a6e49 2020-09-26 stsp if (ref)
5849 417a6e49 2020-09-26 stsp got_ref_close(ref);
5850 417a6e49 2020-09-26 stsp free(commit_id);
5851 417a6e49 2020-09-26 stsp free(commit_id_str);
5852 417a6e49 2020-09-26 stsp free(refname);
5853 417a6e49 2020-09-26 stsp free(tagmsg);
5854 417a6e49 2020-09-26 stsp free(tagmsg_path);
5855 417a6e49 2020-09-26 stsp free(tagger);
5856 417a6e49 2020-09-26 stsp return err;
5857 417a6e49 2020-09-26 stsp }
5858 417a6e49 2020-09-26 stsp
5859 417a6e49 2020-09-26 stsp static const struct got_error *
5860 417a6e49 2020-09-26 stsp cmd_tag(int argc, char *argv[])
5861 417a6e49 2020-09-26 stsp {
5862 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5863 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5864 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5865 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5866 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL;
5867 417a6e49 2020-09-26 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5868 417a6e49 2020-09-26 stsp int ch, do_list = 0;
5869 417a6e49 2020-09-26 stsp
5870 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5871 417a6e49 2020-09-26 stsp switch (ch) {
5872 417a6e49 2020-09-26 stsp case 'c':
5873 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5874 417a6e49 2020-09-26 stsp break;
5875 417a6e49 2020-09-26 stsp case 'm':
5876 417a6e49 2020-09-26 stsp tagmsg = optarg;
5877 417a6e49 2020-09-26 stsp break;
5878 417a6e49 2020-09-26 stsp case 'r':
5879 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5880 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5881 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5882 417a6e49 2020-09-26 stsp optarg);
5883 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5884 417a6e49 2020-09-26 stsp break;
5885 417a6e49 2020-09-26 stsp case 'l':
5886 417a6e49 2020-09-26 stsp do_list = 1;
5887 417a6e49 2020-09-26 stsp break;
5888 417a6e49 2020-09-26 stsp default:
5889 417a6e49 2020-09-26 stsp usage_tag();
5890 417a6e49 2020-09-26 stsp /* NOTREACHED */
5891 417a6e49 2020-09-26 stsp }
5892 417a6e49 2020-09-26 stsp }
5893 417a6e49 2020-09-26 stsp
5894 417a6e49 2020-09-26 stsp argc -= optind;
5895 417a6e49 2020-09-26 stsp argv += optind;
5896 417a6e49 2020-09-26 stsp
5897 417a6e49 2020-09-26 stsp if (do_list) {
5898 417a6e49 2020-09-26 stsp if (commit_id_arg != NULL)
5899 417a6e49 2020-09-26 stsp errx(1,
5900 417a6e49 2020-09-26 stsp "-c option can only be used when creating a tag");
5901 417a6e49 2020-09-26 stsp if (tagmsg)
5902 417a6e49 2020-09-26 stsp errx(1, "-l and -m options are mutually exclusive");
5903 417a6e49 2020-09-26 stsp if (argc > 0)
5904 417a6e49 2020-09-26 stsp usage_tag();
5905 417a6e49 2020-09-26 stsp } else if (argc != 1)
5906 417a6e49 2020-09-26 stsp usage_tag();
5907 417a6e49 2020-09-26 stsp
5908 417a6e49 2020-09-26 stsp tag_name = argv[0];
5909 417a6e49 2020-09-26 stsp
5910 417a6e49 2020-09-26 stsp #ifndef PROFILE
5911 417a6e49 2020-09-26 stsp if (do_list) {
5912 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5913 417a6e49 2020-09-26 stsp NULL) == -1)
5914 417a6e49 2020-09-26 stsp err(1, "pledge");
5915 417a6e49 2020-09-26 stsp } else {
5916 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5917 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5918 417a6e49 2020-09-26 stsp err(1, "pledge");
5919 417a6e49 2020-09-26 stsp }
5920 417a6e49 2020-09-26 stsp #endif
5921 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5922 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5923 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5924 417a6e49 2020-09-26 stsp goto done;
5925 417a6e49 2020-09-26 stsp }
5926 417a6e49 2020-09-26 stsp
5927 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5928 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5929 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5930 417a6e49 2020-09-26 stsp goto done;
5931 417a6e49 2020-09-26 stsp else
5932 417a6e49 2020-09-26 stsp error = NULL;
5933 417a6e49 2020-09-26 stsp if (worktree) {
5934 417a6e49 2020-09-26 stsp repo_path =
5935 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5936 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5937 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5938 417a6e49 2020-09-26 stsp if (error)
5939 417a6e49 2020-09-26 stsp goto done;
5940 417a6e49 2020-09-26 stsp } else {
5941 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5942 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5943 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5944 417a6e49 2020-09-26 stsp goto done;
5945 417a6e49 2020-09-26 stsp }
5946 417a6e49 2020-09-26 stsp }
5947 417a6e49 2020-09-26 stsp }
5948 417a6e49 2020-09-26 stsp
5949 417a6e49 2020-09-26 stsp if (do_list) {
5950 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5951 417a6e49 2020-09-26 stsp if (error != NULL)
5952 417a6e49 2020-09-26 stsp goto done;
5953 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5954 417a6e49 2020-09-26 stsp if (error)
5955 417a6e49 2020-09-26 stsp goto done;
5956 417a6e49 2020-09-26 stsp error = list_tags(repo, worktree);
5957 417a6e49 2020-09-26 stsp } else {
5958 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
5959 417a6e49 2020-09-26 stsp if (error)
5960 417a6e49 2020-09-26 stsp goto done;
5961 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5962 417a6e49 2020-09-26 stsp if (error != NULL)
5963 417a6e49 2020-09-26 stsp goto done;
5964 417a6e49 2020-09-26 stsp
5965 417a6e49 2020-09-26 stsp if (tagmsg) {
5966 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5967 417a6e49 2020-09-26 stsp if (error)
5968 417a6e49 2020-09-26 stsp goto done;
5969 417a6e49 2020-09-26 stsp }
5970 417a6e49 2020-09-26 stsp
5971 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL) {
5972 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
5973 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
5974 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
5975 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5976 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
5977 417a6e49 2020-09-26 stsp if (error)
5978 417a6e49 2020-09-26 stsp goto done;
5979 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5980 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
5981 417a6e49 2020-09-26 stsp if (error)
5982 417a6e49 2020-09-26 stsp goto done;
5983 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5984 417a6e49 2020-09-26 stsp free(commit_id);
5985 417a6e49 2020-09-26 stsp if (error)
5986 417a6e49 2020-09-26 stsp goto done;
5987 417a6e49 2020-09-26 stsp }
5988 417a6e49 2020-09-26 stsp
5989 417a6e49 2020-09-26 stsp error = add_tag(repo, worktree, tag_name,
5990 417a6e49 2020-09-26 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5991 417a6e49 2020-09-26 stsp }
5992 417a6e49 2020-09-26 stsp done:
5993 417a6e49 2020-09-26 stsp if (repo)
5994 417a6e49 2020-09-26 stsp got_repo_close(repo);
5995 417a6e49 2020-09-26 stsp if (worktree)
5996 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5997 417a6e49 2020-09-26 stsp free(cwd);
5998 417a6e49 2020-09-26 stsp free(repo_path);
5999 417a6e49 2020-09-26 stsp free(gitconfig_path);
6000 417a6e49 2020-09-26 stsp free(commit_id_str);
6001 417a6e49 2020-09-26 stsp return error;
6002 417a6e49 2020-09-26 stsp }
6003 417a6e49 2020-09-26 stsp
6004 417a6e49 2020-09-26 stsp __dead static void
6005 417a6e49 2020-09-26 stsp usage_add(void)
6006 417a6e49 2020-09-26 stsp {
6007 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6008 417a6e49 2020-09-26 stsp getprogname());
6009 417a6e49 2020-09-26 stsp exit(1);
6010 417a6e49 2020-09-26 stsp }
6011 417a6e49 2020-09-26 stsp
6012 417a6e49 2020-09-26 stsp static const struct got_error *
6013 417a6e49 2020-09-26 stsp add_progress(void *arg, unsigned char status, const char *path)
6014 417a6e49 2020-09-26 stsp {
6015 417a6e49 2020-09-26 stsp while (path[0] == '/')
6016 417a6e49 2020-09-26 stsp path++;
6017 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6018 417a6e49 2020-09-26 stsp return NULL;
6019 417a6e49 2020-09-26 stsp }
6020 417a6e49 2020-09-26 stsp
6021 417a6e49 2020-09-26 stsp static const struct got_error *
6022 417a6e49 2020-09-26 stsp cmd_add(int argc, char *argv[])
6023 417a6e49 2020-09-26 stsp {
6024 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6025 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6026 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6027 417a6e49 2020-09-26 stsp char *cwd = NULL;
6028 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6029 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6030 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, no_ignores = 0;
6031 417a6e49 2020-09-26 stsp
6032 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6033 417a6e49 2020-09-26 stsp
6034 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "IR")) != -1) {
6035 417a6e49 2020-09-26 stsp switch (ch) {
6036 417a6e49 2020-09-26 stsp case 'I':
6037 417a6e49 2020-09-26 stsp no_ignores = 1;
6038 417a6e49 2020-09-26 stsp break;
6039 417a6e49 2020-09-26 stsp case 'R':
6040 417a6e49 2020-09-26 stsp can_recurse = 1;
6041 417a6e49 2020-09-26 stsp break;
6042 417a6e49 2020-09-26 stsp default:
6043 417a6e49 2020-09-26 stsp usage_add();
6044 417a6e49 2020-09-26 stsp /* NOTREACHED */
6045 417a6e49 2020-09-26 stsp }
6046 417a6e49 2020-09-26 stsp }
6047 417a6e49 2020-09-26 stsp
6048 417a6e49 2020-09-26 stsp argc -= optind;
6049 417a6e49 2020-09-26 stsp argv += optind;
6050 417a6e49 2020-09-26 stsp
6051 417a6e49 2020-09-26 stsp #ifndef PROFILE
6052 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6053 417a6e49 2020-09-26 stsp NULL) == -1)
6054 417a6e49 2020-09-26 stsp err(1, "pledge");
6055 417a6e49 2020-09-26 stsp #endif
6056 417a6e49 2020-09-26 stsp if (argc < 1)
6057 417a6e49 2020-09-26 stsp usage_add();
6058 417a6e49 2020-09-26 stsp
6059 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6060 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6061 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6062 417a6e49 2020-09-26 stsp goto done;
6063 417a6e49 2020-09-26 stsp }
6064 417a6e49 2020-09-26 stsp
6065 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6066 417a6e49 2020-09-26 stsp if (error) {
6067 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6068 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "add", cwd);
6069 417a6e49 2020-09-26 stsp goto done;
6070 417a6e49 2020-09-26 stsp }
6071 417a6e49 2020-09-26 stsp
6072 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6073 417a6e49 2020-09-26 stsp NULL);
6074 417a6e49 2020-09-26 stsp if (error != NULL)
6075 417a6e49 2020-09-26 stsp goto done;
6076 417a6e49 2020-09-26 stsp
6077 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6078 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6079 417a6e49 2020-09-26 stsp if (error)
6080 417a6e49 2020-09-26 stsp goto done;
6081 417a6e49 2020-09-26 stsp
6082 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6083 417a6e49 2020-09-26 stsp if (error)
6084 417a6e49 2020-09-26 stsp goto done;
6085 417a6e49 2020-09-26 stsp
6086 417a6e49 2020-09-26 stsp if (!can_recurse && no_ignores) {
6087 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6088 417a6e49 2020-09-26 stsp "disregarding ignores requires -R option");
6089 417a6e49 2020-09-26 stsp goto done;
6090 417a6e49 2020-09-26 stsp
6091 417a6e49 2020-09-26 stsp }
6092 417a6e49 2020-09-26 stsp
6093 417a6e49 2020-09-26 stsp if (!can_recurse) {
6094 417a6e49 2020-09-26 stsp char *ondisk_path;
6095 417a6e49 2020-09-26 stsp struct stat sb;
6096 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6097 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6098 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6099 417a6e49 2020-09-26 stsp pe->path) == -1) {
6100 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6101 417a6e49 2020-09-26 stsp goto done;
6102 417a6e49 2020-09-26 stsp }
6103 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6104 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6105 417a6e49 2020-09-26 stsp free(ondisk_path);
6106 417a6e49 2020-09-26 stsp continue;
6107 417a6e49 2020-09-26 stsp }
6108 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6109 417a6e49 2020-09-26 stsp ondisk_path);
6110 417a6e49 2020-09-26 stsp free(ondisk_path);
6111 417a6e49 2020-09-26 stsp goto done;
6112 417a6e49 2020-09-26 stsp }
6113 417a6e49 2020-09-26 stsp free(ondisk_path);
6114 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6115 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6116 417a6e49 2020-09-26 stsp "adding directories requires -R option");
6117 417a6e49 2020-09-26 stsp goto done;
6118 417a6e49 2020-09-26 stsp }
6119 417a6e49 2020-09-26 stsp }
6120 417a6e49 2020-09-26 stsp }
6121 417a6e49 2020-09-26 stsp
6122 417a6e49 2020-09-26 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6123 417a6e49 2020-09-26 stsp NULL, repo, no_ignores);
6124 417a6e49 2020-09-26 stsp done:
6125 417a6e49 2020-09-26 stsp if (repo)
6126 417a6e49 2020-09-26 stsp got_repo_close(repo);
6127 417a6e49 2020-09-26 stsp if (worktree)
6128 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6129 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6130 417a6e49 2020-09-26 stsp free((char *)pe->path);
6131 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6132 417a6e49 2020-09-26 stsp free(cwd);
6133 417a6e49 2020-09-26 stsp return error;
6134 417a6e49 2020-09-26 stsp }
6135 417a6e49 2020-09-26 stsp
6136 417a6e49 2020-09-26 stsp __dead static void
6137 417a6e49 2020-09-26 stsp usage_remove(void)
6138 417a6e49 2020-09-26 stsp {
6139 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6140 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6141 417a6e49 2020-09-26 stsp exit(1);
6142 417a6e49 2020-09-26 stsp }
6143 417a6e49 2020-09-26 stsp
6144 417a6e49 2020-09-26 stsp static const struct got_error *
6145 417a6e49 2020-09-26 stsp print_remove_status(void *arg, unsigned char status,
6146 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path)
6147 417a6e49 2020-09-26 stsp {
6148 417a6e49 2020-09-26 stsp while (path[0] == '/')
6149 417a6e49 2020-09-26 stsp path++;
6150 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
6151 417a6e49 2020-09-26 stsp return NULL;
6152 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6153 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
6154 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
6155 417a6e49 2020-09-26 stsp return NULL;
6156 417a6e49 2020-09-26 stsp }
6157 417a6e49 2020-09-26 stsp
6158 417a6e49 2020-09-26 stsp static const struct got_error *
6159 417a6e49 2020-09-26 stsp cmd_remove(int argc, char *argv[])
6160 417a6e49 2020-09-26 stsp {
6161 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6162 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6163 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6164 417a6e49 2020-09-26 stsp const char *status_codes = NULL;
6165 417a6e49 2020-09-26 stsp char *cwd = NULL;
6166 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6167 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6168 417a6e49 2020-09-26 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6169 417a6e49 2020-09-26 stsp
6170 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6171 417a6e49 2020-09-26 stsp
6172 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6173 417a6e49 2020-09-26 stsp switch (ch) {
6174 417a6e49 2020-09-26 stsp case 'f':
6175 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6176 417a6e49 2020-09-26 stsp break;
6177 417a6e49 2020-09-26 stsp case 'k':
6178 417a6e49 2020-09-26 stsp keep_on_disk = 1;
6179 417a6e49 2020-09-26 stsp break;
6180 417a6e49 2020-09-26 stsp case 'R':
6181 417a6e49 2020-09-26 stsp can_recurse = 1;
6182 417a6e49 2020-09-26 stsp break;
6183 417a6e49 2020-09-26 stsp case 's':
6184 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
6185 417a6e49 2020-09-26 stsp switch (optarg[i]) {
6186 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6187 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6188 417a6e49 2020-09-26 stsp break;
6189 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
6190 417a6e49 2020-09-26 stsp break;
6191 417a6e49 2020-09-26 stsp default:
6192 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
6193 417a6e49 2020-09-26 stsp optarg[i]);
6194 417a6e49 2020-09-26 stsp }
6195 417a6e49 2020-09-26 stsp }
6196 417a6e49 2020-09-26 stsp status_codes = optarg;
6197 417a6e49 2020-09-26 stsp break;
6198 417a6e49 2020-09-26 stsp default:
6199 417a6e49 2020-09-26 stsp usage_remove();
6200 417a6e49 2020-09-26 stsp /* NOTREACHED */
6201 417a6e49 2020-09-26 stsp }
6202 417a6e49 2020-09-26 stsp }
6203 417a6e49 2020-09-26 stsp
6204 417a6e49 2020-09-26 stsp argc -= optind;
6205 417a6e49 2020-09-26 stsp argv += optind;
6206 417a6e49 2020-09-26 stsp
6207 417a6e49 2020-09-26 stsp #ifndef PROFILE
6208 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6209 417a6e49 2020-09-26 stsp NULL) == -1)
6210 417a6e49 2020-09-26 stsp err(1, "pledge");
6211 417a6e49 2020-09-26 stsp #endif
6212 417a6e49 2020-09-26 stsp if (argc < 1)
6213 417a6e49 2020-09-26 stsp usage_remove();
6214 417a6e49 2020-09-26 stsp
6215 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6216 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6217 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6218 417a6e49 2020-09-26 stsp goto done;
6219 417a6e49 2020-09-26 stsp }
6220 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6221 417a6e49 2020-09-26 stsp if (error) {
6222 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6223 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6224 417a6e49 2020-09-26 stsp goto done;
6225 417a6e49 2020-09-26 stsp }
6226 417a6e49 2020-09-26 stsp
6227 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6228 417a6e49 2020-09-26 stsp NULL);
6229 417a6e49 2020-09-26 stsp if (error)
6230 417a6e49 2020-09-26 stsp goto done;
6231 417a6e49 2020-09-26 stsp
6232 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6233 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6234 417a6e49 2020-09-26 stsp if (error)
6235 417a6e49 2020-09-26 stsp goto done;
6236 417a6e49 2020-09-26 stsp
6237 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6238 417a6e49 2020-09-26 stsp if (error)
6239 417a6e49 2020-09-26 stsp goto done;
6240 417a6e49 2020-09-26 stsp
6241 417a6e49 2020-09-26 stsp if (!can_recurse) {
6242 417a6e49 2020-09-26 stsp char *ondisk_path;
6243 417a6e49 2020-09-26 stsp struct stat sb;
6244 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6245 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6246 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6247 417a6e49 2020-09-26 stsp pe->path) == -1) {
6248 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6249 417a6e49 2020-09-26 stsp goto done;
6250 417a6e49 2020-09-26 stsp }
6251 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6252 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6253 417a6e49 2020-09-26 stsp free(ondisk_path);
6254 417a6e49 2020-09-26 stsp continue;
6255 417a6e49 2020-09-26 stsp }
6256 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6257 417a6e49 2020-09-26 stsp ondisk_path);
6258 417a6e49 2020-09-26 stsp free(ondisk_path);
6259 417a6e49 2020-09-26 stsp goto done;
6260 417a6e49 2020-09-26 stsp }
6261 417a6e49 2020-09-26 stsp free(ondisk_path);
6262 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6263 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6264 417a6e49 2020-09-26 stsp "removing directories requires -R option");
6265 417a6e49 2020-09-26 stsp goto done;
6266 417a6e49 2020-09-26 stsp }
6267 417a6e49 2020-09-26 stsp }
6268 417a6e49 2020-09-26 stsp }
6269 417a6e49 2020-09-26 stsp
6270 417a6e49 2020-09-26 stsp error = got_worktree_schedule_delete(worktree, &paths,
6271 417a6e49 2020-09-26 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6272 417a6e49 2020-09-26 stsp repo, keep_on_disk);
6273 417a6e49 2020-09-26 stsp done:
6274 417a6e49 2020-09-26 stsp if (repo)
6275 417a6e49 2020-09-26 stsp got_repo_close(repo);
6276 417a6e49 2020-09-26 stsp if (worktree)
6277 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6278 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6279 417a6e49 2020-09-26 stsp free((char *)pe->path);
6280 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6281 417a6e49 2020-09-26 stsp free(cwd);
6282 417a6e49 2020-09-26 stsp return error;
6283 417a6e49 2020-09-26 stsp }
6284 417a6e49 2020-09-26 stsp
6285 417a6e49 2020-09-26 stsp __dead static void
6286 417a6e49 2020-09-26 stsp usage_revert(void)
6287 417a6e49 2020-09-26 stsp {
6288 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6289 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6290 417a6e49 2020-09-26 stsp exit(1);
6291 417a6e49 2020-09-26 stsp }
6292 417a6e49 2020-09-26 stsp
6293 417a6e49 2020-09-26 stsp static const struct got_error *
6294 417a6e49 2020-09-26 stsp revert_progress(void *arg, unsigned char status, const char *path)
6295 417a6e49 2020-09-26 stsp {
6296 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_UNVERSIONED)
6297 417a6e49 2020-09-26 stsp return NULL;
6298 417a6e49 2020-09-26 stsp
6299 417a6e49 2020-09-26 stsp while (path[0] == '/')
6300 417a6e49 2020-09-26 stsp path++;
6301 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6302 417a6e49 2020-09-26 stsp return NULL;
6303 417a6e49 2020-09-26 stsp }
6304 417a6e49 2020-09-26 stsp
6305 417a6e49 2020-09-26 stsp struct choose_patch_arg {
6306 417a6e49 2020-09-26 stsp FILE *patch_script_file;
6307 417a6e49 2020-09-26 stsp const char *action;
6308 417a6e49 2020-09-26 stsp };
6309 417a6e49 2020-09-26 stsp
6310 417a6e49 2020-09-26 stsp static const struct got_error *
6311 417a6e49 2020-09-26 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6312 417a6e49 2020-09-26 stsp int nchanges, const char *action)
6313 417a6e49 2020-09-26 stsp {
6314 417a6e49 2020-09-26 stsp char *line = NULL;
6315 417a6e49 2020-09-26 stsp size_t linesize = 0;
6316 417a6e49 2020-09-26 stsp ssize_t linelen;
6317 417a6e49 2020-09-26 stsp
6318 417a6e49 2020-09-26 stsp switch (status) {
6319 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
6320 417a6e49 2020-09-26 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6321 417a6e49 2020-09-26 stsp break;
6322 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
6323 417a6e49 2020-09-26 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6324 417a6e49 2020-09-26 stsp break;
6325 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6326 417a6e49 2020-09-26 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6327 417a6e49 2020-09-26 stsp return got_error_from_errno("fseek");
6328 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6329 417a6e49 2020-09-26 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6330 417a6e49 2020-09-26 stsp printf("%s", line);
6331 417a6e49 2020-09-26 stsp if (ferror(patch_file))
6332 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6333 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6334 417a6e49 2020-09-26 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6335 417a6e49 2020-09-26 stsp path, n, nchanges, action);
6336 417a6e49 2020-09-26 stsp break;
6337 417a6e49 2020-09-26 stsp default:
6338 417a6e49 2020-09-26 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6339 417a6e49 2020-09-26 stsp }
6340 417a6e49 2020-09-26 stsp
6341 417a6e49 2020-09-26 stsp return NULL;
6342 417a6e49 2020-09-26 stsp }
6343 417a6e49 2020-09-26 stsp
6344 417a6e49 2020-09-26 stsp static const struct got_error *
6345 417a6e49 2020-09-26 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6346 417a6e49 2020-09-26 stsp FILE *patch_file, int n, int nchanges)
6347 417a6e49 2020-09-26 stsp {
6348 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6349 417a6e49 2020-09-26 stsp char *line = NULL;
6350 417a6e49 2020-09-26 stsp size_t linesize = 0;
6351 417a6e49 2020-09-26 stsp ssize_t linelen;
6352 417a6e49 2020-09-26 stsp int resp = ' ';
6353 417a6e49 2020-09-26 stsp struct choose_patch_arg *a = arg;
6354 417a6e49 2020-09-26 stsp
6355 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NONE;
6356 417a6e49 2020-09-26 stsp
6357 417a6e49 2020-09-26 stsp if (a->patch_script_file) {
6358 417a6e49 2020-09-26 stsp char *nl;
6359 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6360 417a6e49 2020-09-26 stsp a->action);
6361 417a6e49 2020-09-26 stsp if (err)
6362 417a6e49 2020-09-26 stsp return err;
6363 417a6e49 2020-09-26 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6364 417a6e49 2020-09-26 stsp if (linelen == -1) {
6365 417a6e49 2020-09-26 stsp if (ferror(a->patch_script_file))
6366 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6367 417a6e49 2020-09-26 stsp return NULL;
6368 417a6e49 2020-09-26 stsp }
6369 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
6370 417a6e49 2020-09-26 stsp if (nl)
6371 417a6e49 2020-09-26 stsp *nl = '\0';
6372 417a6e49 2020-09-26 stsp if (strcmp(line, "y") == 0) {
6373 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6374 417a6e49 2020-09-26 stsp printf("y\n");
6375 417a6e49 2020-09-26 stsp } else if (strcmp(line, "n") == 0) {
6376 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6377 417a6e49 2020-09-26 stsp printf("n\n");
6378 417a6e49 2020-09-26 stsp } else if (strcmp(line, "q") == 0 &&
6379 417a6e49 2020-09-26 stsp status == GOT_STATUS_MODIFY) {
6380 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6381 417a6e49 2020-09-26 stsp printf("q\n");
6382 417a6e49 2020-09-26 stsp } else
6383 417a6e49 2020-09-26 stsp printf("invalid response '%s'\n", line);
6384 417a6e49 2020-09-26 stsp free(line);
6385 417a6e49 2020-09-26 stsp return NULL;
6386 417a6e49 2020-09-26 stsp }
6387 417a6e49 2020-09-26 stsp
6388 417a6e49 2020-09-26 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6389 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6390 417a6e49 2020-09-26 stsp a->action);
6391 417a6e49 2020-09-26 stsp if (err)
6392 417a6e49 2020-09-26 stsp return err;
6393 417a6e49 2020-09-26 stsp resp = getchar();
6394 417a6e49 2020-09-26 stsp if (resp == '\n')
6395 417a6e49 2020-09-26 stsp resp = getchar();
6396 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_MODIFY) {
6397 417a6e49 2020-09-26 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6398 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6399 417a6e49 2020-09-26 stsp resp = ' ';
6400 417a6e49 2020-09-26 stsp }
6401 417a6e49 2020-09-26 stsp } else if (resp != 'y' && resp != 'n') {
6402 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6403 417a6e49 2020-09-26 stsp resp = ' ';
6404 417a6e49 2020-09-26 stsp }
6405 417a6e49 2020-09-26 stsp }
6406 417a6e49 2020-09-26 stsp
6407 417a6e49 2020-09-26 stsp if (resp == 'y')
6408 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6409 417a6e49 2020-09-26 stsp else if (resp == 'n')
6410 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6411 417a6e49 2020-09-26 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6412 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6413 417a6e49 2020-09-26 stsp
6414 417a6e49 2020-09-26 stsp return NULL;
6415 417a6e49 2020-09-26 stsp }
6416 417a6e49 2020-09-26 stsp
6417 417a6e49 2020-09-26 stsp
6418 417a6e49 2020-09-26 stsp static const struct got_error *
6419 417a6e49 2020-09-26 stsp cmd_revert(int argc, char *argv[])
6420 417a6e49 2020-09-26 stsp {
6421 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6422 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6423 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6424 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
6425 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6426 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6427 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, pflag = 0;
6428 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
6429 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
6430 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
6431 417a6e49 2020-09-26 stsp
6432 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6433 417a6e49 2020-09-26 stsp
6434 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6435 417a6e49 2020-09-26 stsp switch (ch) {
6436 417a6e49 2020-09-26 stsp case 'p':
6437 417a6e49 2020-09-26 stsp pflag = 1;
6438 417a6e49 2020-09-26 stsp break;
6439 417a6e49 2020-09-26 stsp case 'F':
6440 417a6e49 2020-09-26 stsp patch_script_path = optarg;
6441 417a6e49 2020-09-26 stsp break;
6442 417a6e49 2020-09-26 stsp case 'R':
6443 417a6e49 2020-09-26 stsp can_recurse = 1;
6444 417a6e49 2020-09-26 stsp break;
6445 417a6e49 2020-09-26 stsp default:
6446 417a6e49 2020-09-26 stsp usage_revert();
6447 417a6e49 2020-09-26 stsp /* NOTREACHED */
6448 417a6e49 2020-09-26 stsp }
6449 417a6e49 2020-09-26 stsp }
6450 417a6e49 2020-09-26 stsp
6451 417a6e49 2020-09-26 stsp argc -= optind;
6452 417a6e49 2020-09-26 stsp argv += optind;
6453 417a6e49 2020-09-26 stsp
6454 417a6e49 2020-09-26 stsp #ifndef PROFILE
6455 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6456 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6457 417a6e49 2020-09-26 stsp err(1, "pledge");
6458 417a6e49 2020-09-26 stsp #endif
6459 417a6e49 2020-09-26 stsp if (argc < 1)
6460 417a6e49 2020-09-26 stsp usage_revert();
6461 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
6462 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
6463 417a6e49 2020-09-26 stsp
6464 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6465 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6466 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6467 417a6e49 2020-09-26 stsp goto done;
6468 417a6e49 2020-09-26 stsp }
6469 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6470 417a6e49 2020-09-26 stsp if (error) {
6471 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6472 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6473 417a6e49 2020-09-26 stsp goto done;
6474 417a6e49 2020-09-26 stsp }
6475 417a6e49 2020-09-26 stsp
6476 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6477 417a6e49 2020-09-26 stsp NULL);
6478 417a6e49 2020-09-26 stsp if (error != NULL)
6479 417a6e49 2020-09-26 stsp goto done;
6480 417a6e49 2020-09-26 stsp
6481 417a6e49 2020-09-26 stsp if (patch_script_path) {
6482 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
6483 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
6484 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
6485 417a6e49 2020-09-26 stsp patch_script_path);
6486 417a6e49 2020-09-26 stsp goto done;
6487 417a6e49 2020-09-26 stsp }
6488 417a6e49 2020-09-26 stsp }
6489 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6490 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6491 417a6e49 2020-09-26 stsp if (error)
6492 417a6e49 2020-09-26 stsp goto done;
6493 417a6e49 2020-09-26 stsp
6494 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6495 417a6e49 2020-09-26 stsp if (error)
6496 417a6e49 2020-09-26 stsp goto done;
6497 417a6e49 2020-09-26 stsp
6498 417a6e49 2020-09-26 stsp if (!can_recurse) {
6499 417a6e49 2020-09-26 stsp char *ondisk_path;
6500 417a6e49 2020-09-26 stsp struct stat sb;
6501 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6502 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6503 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6504 417a6e49 2020-09-26 stsp pe->path) == -1) {
6505 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6506 417a6e49 2020-09-26 stsp goto done;
6507 417a6e49 2020-09-26 stsp }
6508 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6509 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6510 417a6e49 2020-09-26 stsp free(ondisk_path);
6511 417a6e49 2020-09-26 stsp continue;
6512 417a6e49 2020-09-26 stsp }
6513 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6514 417a6e49 2020-09-26 stsp ondisk_path);
6515 417a6e49 2020-09-26 stsp free(ondisk_path);
6516 417a6e49 2020-09-26 stsp goto done;
6517 417a6e49 2020-09-26 stsp }
6518 417a6e49 2020-09-26 stsp free(ondisk_path);
6519 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6520 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6521 417a6e49 2020-09-26 stsp "reverting directories requires -R option");
6522 417a6e49 2020-09-26 stsp goto done;
6523 417a6e49 2020-09-26 stsp }
6524 417a6e49 2020-09-26 stsp }
6525 417a6e49 2020-09-26 stsp }
6526 417a6e49 2020-09-26 stsp
6527 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
6528 417a6e49 2020-09-26 stsp cpa.action = "revert";
6529 417a6e49 2020-09-26 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6530 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa, repo);
6531 417a6e49 2020-09-26 stsp done:
6532 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6533 417a6e49 2020-09-26 stsp error == NULL)
6534 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
6535 417a6e49 2020-09-26 stsp if (repo)
6536 417a6e49 2020-09-26 stsp got_repo_close(repo);
6537 417a6e49 2020-09-26 stsp if (worktree)
6538 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6539 417a6e49 2020-09-26 stsp free(path);
6540 417a6e49 2020-09-26 stsp free(cwd);
6541 417a6e49 2020-09-26 stsp return error;
6542 417a6e49 2020-09-26 stsp }
6543 417a6e49 2020-09-26 stsp
6544 417a6e49 2020-09-26 stsp __dead static void
6545 417a6e49 2020-09-26 stsp usage_commit(void)
6546 417a6e49 2020-09-26 stsp {
6547 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6548 417a6e49 2020-09-26 stsp getprogname());
6549 417a6e49 2020-09-26 stsp exit(1);
6550 417a6e49 2020-09-26 stsp }
6551 417a6e49 2020-09-26 stsp
6552 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg {
6553 417a6e49 2020-09-26 stsp const char *cmdline_log;
6554 417a6e49 2020-09-26 stsp const char *editor;
6555 417a6e49 2020-09-26 stsp const char *worktree_path;
6556 417a6e49 2020-09-26 stsp const char *branch_name;
6557 417a6e49 2020-09-26 stsp const char *repo_path;
6558 417a6e49 2020-09-26 stsp char *logmsg_path;
6559 417a6e49 2020-09-26 stsp
6560 417a6e49 2020-09-26 stsp };
6561 417a6e49 2020-09-26 stsp
6562 417a6e49 2020-09-26 stsp static const struct got_error *
6563 417a6e49 2020-09-26 stsp collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6564 417a6e49 2020-09-26 stsp void *arg)
6565 417a6e49 2020-09-26 stsp {
6566 417a6e49 2020-09-26 stsp char *initial_content = NULL;
6567 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6568 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6569 417a6e49 2020-09-26 stsp char *template = NULL;
6570 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg *a = arg;
6571 417a6e49 2020-09-26 stsp int initial_content_len;
6572 417a6e49 2020-09-26 stsp int fd = -1;
6573 417a6e49 2020-09-26 stsp size_t len;
6574 417a6e49 2020-09-26 stsp
6575 417a6e49 2020-09-26 stsp /* if a message was specified on the command line, just use it */
6576 417a6e49 2020-09-26 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6577 417a6e49 2020-09-26 stsp len = strlen(a->cmdline_log) + 1;
6578 417a6e49 2020-09-26 stsp *logmsg = malloc(len + 1);
6579 417a6e49 2020-09-26 stsp if (*logmsg == NULL)
6580 417a6e49 2020-09-26 stsp return got_error_from_errno("malloc");
6581 417a6e49 2020-09-26 stsp strlcpy(*logmsg, a->cmdline_log, len);
6582 417a6e49 2020-09-26 stsp return NULL;
6583 417a6e49 2020-09-26 stsp }
6584 417a6e49 2020-09-26 stsp
6585 417a6e49 2020-09-26 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6586 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6587 417a6e49 2020-09-26 stsp
6588 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
6589 417a6e49 2020-09-26 stsp "\n# changes to be committed on branch %s:\n",
6590 417a6e49 2020-09-26 stsp a->branch_name);
6591 417a6e49 2020-09-26 stsp if (initial_content_len == -1)
6592 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6593 417a6e49 2020-09-26 stsp
6594 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6595 417a6e49 2020-09-26 stsp if (err)
6596 417a6e49 2020-09-26 stsp goto done;
6597 417a6e49 2020-09-26 stsp
6598 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6599 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", a->logmsg_path);
6600 417a6e49 2020-09-26 stsp goto done;
6601 417a6e49 2020-09-26 stsp }
6602 417a6e49 2020-09-26 stsp
6603 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6604 417a6e49 2020-09-26 stsp struct got_commitable *ct = pe->data;
6605 417a6e49 2020-09-26 stsp dprintf(fd, "# %c %s\n",
6606 417a6e49 2020-09-26 stsp got_commitable_get_status(ct),
6607 417a6e49 2020-09-26 stsp got_commitable_get_path(ct));
6608 417a6e49 2020-09-26 stsp }
6609 417a6e49 2020-09-26 stsp
6610 417a6e49 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6611 417a6e49 2020-09-26 stsp done:
6612 417a6e49 2020-09-26 stsp free(initial_content);
6613 417a6e49 2020-09-26 stsp free(template);
6614 417a6e49 2020-09-26 stsp
6615 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6616 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", a->logmsg_path);
6617 417a6e49 2020-09-26 stsp
6618 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
6619 417a6e49 2020-09-26 stsp if (err == NULL)
6620 417a6e49 2020-09-26 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6621 417a6e49 2020-09-26 stsp if (err) {
6622 417a6e49 2020-09-26 stsp free(*logmsg);
6623 417a6e49 2020-09-26 stsp *logmsg = NULL;
6624 417a6e49 2020-09-26 stsp }
6625 417a6e49 2020-09-26 stsp return err;
6626 417a6e49 2020-09-26 stsp }
6627 417a6e49 2020-09-26 stsp
6628 417a6e49 2020-09-26 stsp static const struct got_error *
6629 417a6e49 2020-09-26 stsp cmd_commit(int argc, char *argv[])
6630 417a6e49 2020-09-26 stsp {
6631 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6632 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6633 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6634 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
6635 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
6636 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
6637 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg cl_arg;
6638 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6639 417a6e49 2020-09-26 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6640 417a6e49 2020-09-26 stsp int allow_bad_symlinks = 0;
6641 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6642 417a6e49 2020-09-26 stsp
6643 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6644 417a6e49 2020-09-26 stsp cl_arg.logmsg_path = NULL;
6645 417a6e49 2020-09-26 stsp
6646 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6647 417a6e49 2020-09-26 stsp switch (ch) {
6648 417a6e49 2020-09-26 stsp case 'm':
6649 417a6e49 2020-09-26 stsp logmsg = optarg;
6650 417a6e49 2020-09-26 stsp break;
6651 417a6e49 2020-09-26 stsp case 'S':
6652 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
6653 417a6e49 2020-09-26 stsp break;
6654 417a6e49 2020-09-26 stsp default:
6655 417a6e49 2020-09-26 stsp usage_commit();
6656 417a6e49 2020-09-26 stsp /* NOTREACHED */
6657 417a6e49 2020-09-26 stsp }
6658 417a6e49 2020-09-26 stsp }
6659 417a6e49 2020-09-26 stsp
6660 417a6e49 2020-09-26 stsp argc -= optind;
6661 417a6e49 2020-09-26 stsp argv += optind;
6662 417a6e49 2020-09-26 stsp
6663 417a6e49 2020-09-26 stsp #ifndef PROFILE
6664 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6665 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6666 417a6e49 2020-09-26 stsp err(1, "pledge");
6667 417a6e49 2020-09-26 stsp #endif
6668 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6669 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6670 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6671 417a6e49 2020-09-26 stsp goto done;
6672 417a6e49 2020-09-26 stsp }
6673 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6674 417a6e49 2020-09-26 stsp if (error) {
6675 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6676 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6677 417a6e49 2020-09-26 stsp goto done;
6678 417a6e49 2020-09-26 stsp }
6679 417a6e49 2020-09-26 stsp
6680 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6681 417a6e49 2020-09-26 stsp if (error)
6682 417a6e49 2020-09-26 stsp goto done;
6683 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
6684 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
6685 417a6e49 2020-09-26 stsp goto done;
6686 417a6e49 2020-09-26 stsp }
6687 417a6e49 2020-09-26 stsp
6688 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6689 417a6e49 2020-09-26 stsp worktree);
6690 417a6e49 2020-09-26 stsp if (error)
6691 417a6e49 2020-09-26 stsp goto done;
6692 417a6e49 2020-09-26 stsp
6693 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
6694 417a6e49 2020-09-26 stsp if (error)
6695 417a6e49 2020-09-26 stsp goto done;
6696 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6697 417a6e49 2020-09-26 stsp gitconfig_path);
6698 417a6e49 2020-09-26 stsp if (error != NULL)
6699 417a6e49 2020-09-26 stsp goto done;
6700 417a6e49 2020-09-26 stsp
6701 417a6e49 2020-09-26 stsp error = get_author(&author, repo, worktree);
6702 417a6e49 2020-09-26 stsp if (error)
6703 417a6e49 2020-09-26 stsp return error;
6704 417a6e49 2020-09-26 stsp
6705 417a6e49 2020-09-26 stsp /*
6706 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have
6707 417a6e49 2020-09-26 stsp * to apply unveil after the log message has been written.
6708 417a6e49 2020-09-26 stsp */
6709 417a6e49 2020-09-26 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6710 417a6e49 2020-09-26 stsp error = get_editor(&editor);
6711 417a6e49 2020-09-26 stsp else
6712 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6713 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6714 417a6e49 2020-09-26 stsp if (error)
6715 417a6e49 2020-09-26 stsp goto done;
6716 417a6e49 2020-09-26 stsp
6717 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6718 417a6e49 2020-09-26 stsp if (error)
6719 417a6e49 2020-09-26 stsp goto done;
6720 417a6e49 2020-09-26 stsp
6721 417a6e49 2020-09-26 stsp cl_arg.editor = editor;
6722 417a6e49 2020-09-26 stsp cl_arg.cmdline_log = logmsg;
6723 417a6e49 2020-09-26 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6724 417a6e49 2020-09-26 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6725 417a6e49 2020-09-26 stsp if (!histedit_in_progress) {
6726 417a6e49 2020-09-26 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6727 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6728 417a6e49 2020-09-26 stsp goto done;
6729 417a6e49 2020-09-26 stsp }
6730 417a6e49 2020-09-26 stsp cl_arg.branch_name += 11;
6731 417a6e49 2020-09-26 stsp }
6732 417a6e49 2020-09-26 stsp cl_arg.repo_path = got_repo_get_path(repo);
6733 417a6e49 2020-09-26 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6734 417a6e49 2020-09-26 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6735 417a6e49 2020-09-26 stsp print_status, NULL, repo);
6736 417a6e49 2020-09-26 stsp if (error) {
6737 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6738 417a6e49 2020-09-26 stsp cl_arg.logmsg_path != NULL)
6739 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
6740 417a6e49 2020-09-26 stsp goto done;
6741 417a6e49 2020-09-26 stsp }
6742 417a6e49 2020-09-26 stsp
6743 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, id);
6744 417a6e49 2020-09-26 stsp if (error)
6745 417a6e49 2020-09-26 stsp goto done;
6746 417a6e49 2020-09-26 stsp printf("Created commit %s\n", id_str);
6747 417a6e49 2020-09-26 stsp done:
6748 417a6e49 2020-09-26 stsp if (preserve_logmsg) {
6749 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6750 417a6e49 2020-09-26 stsp getprogname(), cl_arg.logmsg_path);
6751 417a6e49 2020-09-26 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6752 417a6e49 2020-09-26 stsp error == NULL)
6753 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6754 417a6e49 2020-09-26 stsp free(cl_arg.logmsg_path);
6755 417a6e49 2020-09-26 stsp if (repo)
6756 417a6e49 2020-09-26 stsp got_repo_close(repo);
6757 417a6e49 2020-09-26 stsp if (worktree)
6758 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6759 417a6e49 2020-09-26 stsp free(cwd);
6760 417a6e49 2020-09-26 stsp free(id_str);
6761 417a6e49 2020-09-26 stsp free(gitconfig_path);
6762 417a6e49 2020-09-26 stsp free(editor);
6763 417a6e49 2020-09-26 stsp free(author);
6764 417a6e49 2020-09-26 stsp return error;
6765 417a6e49 2020-09-26 stsp }
6766 417a6e49 2020-09-26 stsp
6767 417a6e49 2020-09-26 stsp __dead static void
6768 417a6e49 2020-09-26 stsp usage_cherrypick(void)
6769 417a6e49 2020-09-26 stsp {
6770 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6771 417a6e49 2020-09-26 stsp exit(1);
6772 417a6e49 2020-09-26 stsp }
6773 417a6e49 2020-09-26 stsp
6774 417a6e49 2020-09-26 stsp static const struct got_error *
6775 417a6e49 2020-09-26 stsp cmd_cherrypick(int argc, char *argv[])
6776 417a6e49 2020-09-26 stsp {
6777 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6778 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6779 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6780 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6781 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6782 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6783 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6784 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6785 417a6e49 2020-09-26 stsp int ch;
6786 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6787 417a6e49 2020-09-26 stsp
6788 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6789 417a6e49 2020-09-26 stsp switch (ch) {
6790 417a6e49 2020-09-26 stsp default:
6791 417a6e49 2020-09-26 stsp usage_cherrypick();
6792 417a6e49 2020-09-26 stsp /* NOTREACHED */
6793 417a6e49 2020-09-26 stsp }
6794 417a6e49 2020-09-26 stsp }
6795 417a6e49 2020-09-26 stsp
6796 417a6e49 2020-09-26 stsp argc -= optind;
6797 417a6e49 2020-09-26 stsp argv += optind;
6798 417a6e49 2020-09-26 stsp
6799 417a6e49 2020-09-26 stsp #ifndef PROFILE
6800 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6801 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6802 417a6e49 2020-09-26 stsp err(1, "pledge");
6803 417a6e49 2020-09-26 stsp #endif
6804 417a6e49 2020-09-26 stsp if (argc != 1)
6805 417a6e49 2020-09-26 stsp usage_cherrypick();
6806 417a6e49 2020-09-26 stsp
6807 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6808 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6809 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6810 417a6e49 2020-09-26 stsp goto done;
6811 417a6e49 2020-09-26 stsp }
6812 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6813 417a6e49 2020-09-26 stsp if (error) {
6814 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6815 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "cherrypick",
6816 417a6e49 2020-09-26 stsp cwd);
6817 417a6e49 2020-09-26 stsp goto done;
6818 417a6e49 2020-09-26 stsp }
6819 417a6e49 2020-09-26 stsp
6820 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6821 417a6e49 2020-09-26 stsp NULL);
6822 417a6e49 2020-09-26 stsp if (error != NULL)
6823 417a6e49 2020-09-26 stsp goto done;
6824 417a6e49 2020-09-26 stsp
6825 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6826 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6827 417a6e49 2020-09-26 stsp if (error)
6828 417a6e49 2020-09-26 stsp goto done;
6829 417a6e49 2020-09-26 stsp
6830 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6831 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6832 417a6e49 2020-09-26 stsp if (error != NULL) {
6833 417a6e49 2020-09-26 stsp struct got_reference *ref;
6834 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6835 417a6e49 2020-09-26 stsp goto done;
6836 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6837 417a6e49 2020-09-26 stsp if (error != NULL)
6838 417a6e49 2020-09-26 stsp goto done;
6839 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6840 417a6e49 2020-09-26 stsp got_ref_close(ref);
6841 417a6e49 2020-09-26 stsp if (error != NULL)
6842 417a6e49 2020-09-26 stsp goto done;
6843 417a6e49 2020-09-26 stsp }
6844 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6845 417a6e49 2020-09-26 stsp if (error)
6846 417a6e49 2020-09-26 stsp goto done;
6847 417a6e49 2020-09-26 stsp
6848 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6849 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6850 417a6e49 2020-09-26 stsp if (error != NULL)
6851 417a6e49 2020-09-26 stsp goto done;
6852 417a6e49 2020-09-26 stsp
6853 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6854 417a6e49 2020-09-26 stsp if (error) {
6855 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
6856 417a6e49 2020-09-26 stsp goto done;
6857 417a6e49 2020-09-26 stsp error = NULL;
6858 417a6e49 2020-09-26 stsp } else {
6859 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6860 417a6e49 2020-09-26 stsp goto done;
6861 417a6e49 2020-09-26 stsp }
6862 417a6e49 2020-09-26 stsp
6863 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6864 417a6e49 2020-09-26 stsp if (error)
6865 417a6e49 2020-09-26 stsp goto done;
6866 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6867 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6868 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6869 417a6e49 2020-09-26 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6870 417a6e49 2020-09-26 stsp NULL);
6871 417a6e49 2020-09-26 stsp if (error != NULL)
6872 417a6e49 2020-09-26 stsp goto done;
6873 417a6e49 2020-09-26 stsp
6874 417a6e49 2020-09-26 stsp if (upa.did_something)
6875 417a6e49 2020-09-26 stsp printf("Merged commit %s\n", commit_id_str);
6876 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
6877 417a6e49 2020-09-26 stsp done:
6878 417a6e49 2020-09-26 stsp if (commit)
6879 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
6880 417a6e49 2020-09-26 stsp free(commit_id_str);
6881 417a6e49 2020-09-26 stsp if (head_ref)
6882 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
6883 417a6e49 2020-09-26 stsp if (worktree)
6884 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6885 417a6e49 2020-09-26 stsp if (repo)
6886 417a6e49 2020-09-26 stsp got_repo_close(repo);
6887 417a6e49 2020-09-26 stsp return error;
6888 417a6e49 2020-09-26 stsp }
6889 417a6e49 2020-09-26 stsp
6890 417a6e49 2020-09-26 stsp __dead static void
6891 417a6e49 2020-09-26 stsp usage_backout(void)
6892 417a6e49 2020-09-26 stsp {
6893 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6894 417a6e49 2020-09-26 stsp exit(1);
6895 417a6e49 2020-09-26 stsp }
6896 417a6e49 2020-09-26 stsp
6897 417a6e49 2020-09-26 stsp static const struct got_error *
6898 417a6e49 2020-09-26 stsp cmd_backout(int argc, char *argv[])
6899 417a6e49 2020-09-26 stsp {
6900 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6901 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6902 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6903 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6904 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6905 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6906 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6907 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6908 417a6e49 2020-09-26 stsp int ch;
6909 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6910 417a6e49 2020-09-26 stsp
6911 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6912 417a6e49 2020-09-26 stsp switch (ch) {
6913 417a6e49 2020-09-26 stsp default:
6914 417a6e49 2020-09-26 stsp usage_backout();
6915 417a6e49 2020-09-26 stsp /* NOTREACHED */
6916 417a6e49 2020-09-26 stsp }
6917 417a6e49 2020-09-26 stsp }
6918 417a6e49 2020-09-26 stsp
6919 417a6e49 2020-09-26 stsp argc -= optind;
6920 417a6e49 2020-09-26 stsp argv += optind;
6921 417a6e49 2020-09-26 stsp
6922 417a6e49 2020-09-26 stsp #ifndef PROFILE
6923 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6924 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6925 417a6e49 2020-09-26 stsp err(1, "pledge");
6926 417a6e49 2020-09-26 stsp #endif
6927 417a6e49 2020-09-26 stsp if (argc != 1)
6928 417a6e49 2020-09-26 stsp usage_backout();
6929 417a6e49 2020-09-26 stsp
6930 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6931 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6932 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6933 417a6e49 2020-09-26 stsp goto done;
6934 417a6e49 2020-09-26 stsp }
6935 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6936 417a6e49 2020-09-26 stsp if (error) {
6937 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6938 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6939 417a6e49 2020-09-26 stsp goto done;
6940 417a6e49 2020-09-26 stsp }
6941 417a6e49 2020-09-26 stsp
6942 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6943 417a6e49 2020-09-26 stsp NULL);
6944 417a6e49 2020-09-26 stsp if (error != NULL)
6945 417a6e49 2020-09-26 stsp goto done;
6946 417a6e49 2020-09-26 stsp
6947 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6948 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6949 417a6e49 2020-09-26 stsp if (error)
6950 417a6e49 2020-09-26 stsp goto done;
6951 417a6e49 2020-09-26 stsp
6952 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6953 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6954 417a6e49 2020-09-26 stsp if (error != NULL) {
6955 417a6e49 2020-09-26 stsp struct got_reference *ref;
6956 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6957 417a6e49 2020-09-26 stsp goto done;
6958 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6959 417a6e49 2020-09-26 stsp if (error != NULL)
6960 417a6e49 2020-09-26 stsp goto done;
6961 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6962 417a6e49 2020-09-26 stsp got_ref_close(ref);
6963 417a6e49 2020-09-26 stsp if (error != NULL)
6964 417a6e49 2020-09-26 stsp goto done;
6965 417a6e49 2020-09-26 stsp }
6966 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6967 417a6e49 2020-09-26 stsp if (error)
6968 417a6e49 2020-09-26 stsp goto done;
6969 417a6e49 2020-09-26 stsp
6970 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6971 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6972 417a6e49 2020-09-26 stsp if (error != NULL)
6973 417a6e49 2020-09-26 stsp goto done;
6974 417a6e49 2020-09-26 stsp
6975 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6976 417a6e49 2020-09-26 stsp if (error)
6977 417a6e49 2020-09-26 stsp goto done;
6978 417a6e49 2020-09-26 stsp
6979 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6980 417a6e49 2020-09-26 stsp if (error)
6981 417a6e49 2020-09-26 stsp goto done;
6982 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6983 417a6e49 2020-09-26 stsp if (pid == NULL) {
6984 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6985 417a6e49 2020-09-26 stsp goto done;
6986 417a6e49 2020-09-26 stsp }
6987 417a6e49 2020-09-26 stsp
6988 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6989 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6990 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
6991 417a6e49 2020-09-26 stsp if (error != NULL)
6992 417a6e49 2020-09-26 stsp goto done;
6993 417a6e49 2020-09-26 stsp
6994 417a6e49 2020-09-26 stsp if (upa.did_something)
6995 417a6e49 2020-09-26 stsp printf("Backed out commit %s\n", commit_id_str);
6996 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
6997 417a6e49 2020-09-26 stsp done:
6998 417a6e49 2020-09-26 stsp if (commit)
6999 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7000 417a6e49 2020-09-26 stsp free(commit_id_str);
7001 417a6e49 2020-09-26 stsp if (head_ref)
7002 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
7003 417a6e49 2020-09-26 stsp if (worktree)
7004 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7005 417a6e49 2020-09-26 stsp if (repo)
7006 417a6e49 2020-09-26 stsp got_repo_close(repo);
7007 417a6e49 2020-09-26 stsp return error;
7008 417a6e49 2020-09-26 stsp }
7009 417a6e49 2020-09-26 stsp
7010 417a6e49 2020-09-26 stsp __dead static void
7011 417a6e49 2020-09-26 stsp usage_rebase(void)
7012 417a6e49 2020-09-26 stsp {
7013 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7014 417a6e49 2020-09-26 stsp getprogname());
7015 417a6e49 2020-09-26 stsp exit(1);
7016 417a6e49 2020-09-26 stsp }
7017 417a6e49 2020-09-26 stsp
7018 417a6e49 2020-09-26 stsp void
7019 417a6e49 2020-09-26 stsp trim_logmsg(char *logmsg, int limit)
7020 417a6e49 2020-09-26 stsp {
7021 417a6e49 2020-09-26 stsp char *nl;
7022 417a6e49 2020-09-26 stsp size_t len;
7023 417a6e49 2020-09-26 stsp
7024 417a6e49 2020-09-26 stsp len = strlen(logmsg);
7025 417a6e49 2020-09-26 stsp if (len > limit)
7026 417a6e49 2020-09-26 stsp len = limit;
7027 417a6e49 2020-09-26 stsp logmsg[len] = '\0';
7028 417a6e49 2020-09-26 stsp nl = strchr(logmsg, '\n');
7029 417a6e49 2020-09-26 stsp if (nl)
7030 417a6e49 2020-09-26 stsp *nl = '\0';
7031 417a6e49 2020-09-26 stsp }
7032 417a6e49 2020-09-26 stsp
7033 417a6e49 2020-09-26 stsp static const struct got_error *
7034 417a6e49 2020-09-26 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7035 417a6e49 2020-09-26 stsp {
7036 417a6e49 2020-09-26 stsp const struct got_error *err;
7037 417a6e49 2020-09-26 stsp char *logmsg0 = NULL;
7038 417a6e49 2020-09-26 stsp const char *s;
7039 417a6e49 2020-09-26 stsp
7040 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7041 417a6e49 2020-09-26 stsp if (err)
7042 417a6e49 2020-09-26 stsp return err;
7043 417a6e49 2020-09-26 stsp
7044 417a6e49 2020-09-26 stsp s = logmsg0;
7045 417a6e49 2020-09-26 stsp while (isspace((unsigned char)s[0]))
7046 417a6e49 2020-09-26 stsp s++;
7047 417a6e49 2020-09-26 stsp
7048 417a6e49 2020-09-26 stsp *logmsg = strdup(s);
7049 417a6e49 2020-09-26 stsp if (*logmsg == NULL) {
7050 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7051 417a6e49 2020-09-26 stsp goto done;
7052 417a6e49 2020-09-26 stsp }
7053 417a6e49 2020-09-26 stsp
7054 417a6e49 2020-09-26 stsp trim_logmsg(*logmsg, limit);
7055 417a6e49 2020-09-26 stsp done:
7056 417a6e49 2020-09-26 stsp free(logmsg0);
7057 417a6e49 2020-09-26 stsp return err;
7058 417a6e49 2020-09-26 stsp }
7059 417a6e49 2020-09-26 stsp
7060 417a6e49 2020-09-26 stsp static const struct got_error *
7061 417a6e49 2020-09-26 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7062 417a6e49 2020-09-26 stsp {
7063 417a6e49 2020-09-26 stsp const struct got_error *err;
7064 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7065 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
7066 417a6e49 2020-09-26 stsp
7067 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
7068 417a6e49 2020-09-26 stsp if (err)
7069 417a6e49 2020-09-26 stsp return err;
7070 417a6e49 2020-09-26 stsp
7071 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
7072 417a6e49 2020-09-26 stsp if (err)
7073 417a6e49 2020-09-26 stsp goto done;
7074 417a6e49 2020-09-26 stsp
7075 417a6e49 2020-09-26 stsp id_str[12] = '\0';
7076 417a6e49 2020-09-26 stsp
7077 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7078 417a6e49 2020-09-26 stsp if (err)
7079 417a6e49 2020-09-26 stsp goto done;
7080 417a6e49 2020-09-26 stsp
7081 417a6e49 2020-09-26 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7082 417a6e49 2020-09-26 stsp done:
7083 417a6e49 2020-09-26 stsp free(id_str);
7084 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7085 417a6e49 2020-09-26 stsp free(logmsg);
7086 417a6e49 2020-09-26 stsp return err;
7087 417a6e49 2020-09-26 stsp }
7088 417a6e49 2020-09-26 stsp
7089 417a6e49 2020-09-26 stsp static const struct got_error *
7090 417a6e49 2020-09-26 stsp show_rebase_progress(struct got_commit_object *commit,
7091 417a6e49 2020-09-26 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7092 417a6e49 2020-09-26 stsp {
7093 417a6e49 2020-09-26 stsp const struct got_error *err;
7094 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7095 417a6e49 2020-09-26 stsp
7096 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, old_id);
7097 417a6e49 2020-09-26 stsp if (err)
7098 417a6e49 2020-09-26 stsp goto done;
7099 417a6e49 2020-09-26 stsp
7100 417a6e49 2020-09-26 stsp if (new_id) {
7101 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
7102 417a6e49 2020-09-26 stsp if (err)
7103 417a6e49 2020-09-26 stsp goto done;
7104 417a6e49 2020-09-26 stsp }
7105 417a6e49 2020-09-26 stsp
7106 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
7107 417a6e49 2020-09-26 stsp if (new_id_str)
7108 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
7109 417a6e49 2020-09-26 stsp
7110 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7111 417a6e49 2020-09-26 stsp if (err)
7112 417a6e49 2020-09-26 stsp goto done;
7113 417a6e49 2020-09-26 stsp
7114 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
7115 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7116 417a6e49 2020-09-26 stsp done:
7117 417a6e49 2020-09-26 stsp free(old_id_str);
7118 417a6e49 2020-09-26 stsp free(new_id_str);
7119 417a6e49 2020-09-26 stsp free(logmsg);
7120 417a6e49 2020-09-26 stsp return err;
7121 417a6e49 2020-09-26 stsp }
7122 417a6e49 2020-09-26 stsp
7123 417a6e49 2020-09-26 stsp static const struct got_error *
7124 417a6e49 2020-09-26 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7125 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7126 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7127 417a6e49 2020-09-26 stsp {
7128 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7129 417a6e49 2020-09-26 stsp return got_worktree_rebase_complete(worktree, fileindex,
7130 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, branch, repo);
7131 417a6e49 2020-09-26 stsp }
7132 417a6e49 2020-09-26 stsp
7133 417a6e49 2020-09-26 stsp static const struct got_error *
7134 417a6e49 2020-09-26 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7135 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7136 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch,
7137 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_repository *repo)
7138 417a6e49 2020-09-26 stsp {
7139 417a6e49 2020-09-26 stsp const struct got_error *error;
7140 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
7141 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
7142 417a6e49 2020-09-26 stsp
7143 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7144 417a6e49 2020-09-26 stsp if (error)
7145 417a6e49 2020-09-26 stsp return error;
7146 417a6e49 2020-09-26 stsp
7147 417a6e49 2020-09-26 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7148 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7149 417a6e49 2020-09-26 stsp if (error) {
7150 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7151 417a6e49 2020-09-26 stsp goto done;
7152 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, NULL);
7153 417a6e49 2020-09-26 stsp } else {
7154 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7155 417a6e49 2020-09-26 stsp free(new_commit_id);
7156 417a6e49 2020-09-26 stsp }
7157 417a6e49 2020-09-26 stsp done:
7158 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7159 417a6e49 2020-09-26 stsp return error;
7160 417a6e49 2020-09-26 stsp }
7161 417a6e49 2020-09-26 stsp
7162 417a6e49 2020-09-26 stsp struct check_path_prefix_arg {
7163 417a6e49 2020-09-26 stsp const char *path_prefix;
7164 417a6e49 2020-09-26 stsp size_t len;
7165 417a6e49 2020-09-26 stsp int errcode;
7166 417a6e49 2020-09-26 stsp };
7167 417a6e49 2020-09-26 stsp
7168 417a6e49 2020-09-26 stsp static const struct got_error *
7169 417a6e49 2020-09-26 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7170 417a6e49 2020-09-26 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7171 417a6e49 2020-09-26 stsp struct got_object_id *id2, const char *path1, const char *path2,
7172 417a6e49 2020-09-26 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7173 417a6e49 2020-09-26 stsp {
7174 417a6e49 2020-09-26 stsp struct check_path_prefix_arg *a = arg;
7175 417a6e49 2020-09-26 stsp
7176 417a6e49 2020-09-26 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7177 417a6e49 2020-09-26 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7178 417a6e49 2020-09-26 stsp return got_error(a->errcode);
7179 417a6e49 2020-09-26 stsp
7180 417a6e49 2020-09-26 stsp return NULL;
7181 417a6e49 2020-09-26 stsp }
7182 417a6e49 2020-09-26 stsp
7183 417a6e49 2020-09-26 stsp static const struct got_error *
7184 417a6e49 2020-09-26 stsp check_path_prefix(struct got_object_id *parent_id,
7185 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, const char *path_prefix,
7186 417a6e49 2020-09-26 stsp int errcode, struct got_repository *repo)
7187 417a6e49 2020-09-26 stsp {
7188 417a6e49 2020-09-26 stsp const struct got_error *err;
7189 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7190 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7191 417a6e49 2020-09-26 stsp struct check_path_prefix_arg cpp_arg;
7192 417a6e49 2020-09-26 stsp
7193 417a6e49 2020-09-26 stsp if (got_path_is_root_dir(path_prefix))
7194 417a6e49 2020-09-26 stsp return NULL;
7195 417a6e49 2020-09-26 stsp
7196 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7197 417a6e49 2020-09-26 stsp if (err)
7198 417a6e49 2020-09-26 stsp goto done;
7199 417a6e49 2020-09-26 stsp
7200 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7201 417a6e49 2020-09-26 stsp if (err)
7202 417a6e49 2020-09-26 stsp goto done;
7203 417a6e49 2020-09-26 stsp
7204 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo,
7205 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(parent_commit));
7206 417a6e49 2020-09-26 stsp if (err)
7207 417a6e49 2020-09-26 stsp goto done;
7208 417a6e49 2020-09-26 stsp
7209 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo,
7210 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(commit));
7211 417a6e49 2020-09-26 stsp if (err)
7212 417a6e49 2020-09-26 stsp goto done;
7213 417a6e49 2020-09-26 stsp
7214 417a6e49 2020-09-26 stsp cpp_arg.path_prefix = path_prefix;
7215 417a6e49 2020-09-26 stsp while (cpp_arg.path_prefix[0] == '/')
7216 417a6e49 2020-09-26 stsp cpp_arg.path_prefix++;
7217 417a6e49 2020-09-26 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7218 417a6e49 2020-09-26 stsp cpp_arg.errcode = errcode;
7219 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7220 417a6e49 2020-09-26 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7221 417a6e49 2020-09-26 stsp done:
7222 417a6e49 2020-09-26 stsp if (tree1)
7223 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
7224 417a6e49 2020-09-26 stsp if (tree2)
7225 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
7226 417a6e49 2020-09-26 stsp if (commit)
7227 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7228 417a6e49 2020-09-26 stsp if (parent_commit)
7229 417a6e49 2020-09-26 stsp got_object_commit_close(parent_commit);
7230 417a6e49 2020-09-26 stsp return err;
7231 417a6e49 2020-09-26 stsp }
7232 417a6e49 2020-09-26 stsp
7233 417a6e49 2020-09-26 stsp static const struct got_error *
7234 417a6e49 2020-09-26 stsp collect_commits(struct got_object_id_queue *commits,
7235 417a6e49 2020-09-26 stsp struct got_object_id *initial_commit_id,
7236 417a6e49 2020-09-26 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7237 417a6e49 2020-09-26 stsp const char *path_prefix, int path_prefix_errcode,
7238 417a6e49 2020-09-26 stsp struct got_repository *repo)
7239 417a6e49 2020-09-26 stsp {
7240 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7241 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
7242 417a6e49 2020-09-26 stsp struct got_object_id *parent_id = NULL;
7243 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7244 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = initial_commit_id;
7245 417a6e49 2020-09-26 stsp
7246 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
7247 417a6e49 2020-09-26 stsp if (err)
7248 417a6e49 2020-09-26 stsp return err;
7249 417a6e49 2020-09-26 stsp
7250 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7251 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7252 417a6e49 2020-09-26 stsp if (err)
7253 417a6e49 2020-09-26 stsp goto done;
7254 417a6e49 2020-09-26 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7255 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7256 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7257 417a6e49 2020-09-26 stsp if (err) {
7258 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7259 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7260 417a6e49 2020-09-26 stsp "ran out of commits to rebase before "
7261 417a6e49 2020-09-26 stsp "youngest common ancestor commit has "
7262 417a6e49 2020-09-26 stsp "been reached?!?");
7263 417a6e49 2020-09-26 stsp }
7264 417a6e49 2020-09-26 stsp goto done;
7265 417a6e49 2020-09-26 stsp } else {
7266 417a6e49 2020-09-26 stsp err = check_path_prefix(parent_id, commit_id,
7267 417a6e49 2020-09-26 stsp path_prefix, path_prefix_errcode, repo);
7268 417a6e49 2020-09-26 stsp if (err)
7269 417a6e49 2020-09-26 stsp goto done;
7270 417a6e49 2020-09-26 stsp
7271 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, commit_id);
7272 417a6e49 2020-09-26 stsp if (err)
7273 417a6e49 2020-09-26 stsp goto done;
7274 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7275 417a6e49 2020-09-26 stsp commit_id = parent_id;
7276 417a6e49 2020-09-26 stsp }
7277 417a6e49 2020-09-26 stsp }
7278 417a6e49 2020-09-26 stsp done:
7279 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
7280 417a6e49 2020-09-26 stsp return err;
7281 417a6e49 2020-09-26 stsp }
7282 417a6e49 2020-09-26 stsp
7283 417a6e49 2020-09-26 stsp static const struct got_error *
7284 417a6e49 2020-09-26 stsp cmd_rebase(int argc, char *argv[])
7285 417a6e49 2020-09-26 stsp {
7286 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
7287 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
7288 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
7289 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
7290 417a6e49 2020-09-26 stsp char *cwd = NULL;
7291 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
7292 417a6e49 2020-09-26 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7293 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7294 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
7295 417a6e49 2020-09-26 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7296 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7297 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7298 417a6e49 2020-09-26 stsp int histedit_in_progress = 0;
7299 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7300 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
7301 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
7302 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
7303 417a6e49 2020-09-26 stsp struct got_object_qid *qid, *pid;
7304 417a6e49 2020-09-26 stsp
7305 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
7306 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
7307 417a6e49 2020-09-26 stsp
7308 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7309 417a6e49 2020-09-26 stsp switch (ch) {
7310 417a6e49 2020-09-26 stsp case 'a':
7311 417a6e49 2020-09-26 stsp abort_rebase = 1;
7312 417a6e49 2020-09-26 stsp break;
7313 417a6e49 2020-09-26 stsp case 'c':
7314 417a6e49 2020-09-26 stsp continue_rebase = 1;
7315 417a6e49 2020-09-26 stsp break;
7316 417a6e49 2020-09-26 stsp default:
7317 417a6e49 2020-09-26 stsp usage_rebase();
7318 417a6e49 2020-09-26 stsp /* NOTREACHED */
7319 417a6e49 2020-09-26 stsp }
7320 417a6e49 2020-09-26 stsp }
7321 417a6e49 2020-09-26 stsp
7322 417a6e49 2020-09-26 stsp argc -= optind;
7323 417a6e49 2020-09-26 stsp argv += optind;
7324 417a6e49 2020-09-26 stsp
7325 417a6e49 2020-09-26 stsp #ifndef PROFILE
7326 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7327 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
7328 417a6e49 2020-09-26 stsp err(1, "pledge");
7329 417a6e49 2020-09-26 stsp #endif
7330 417a6e49 2020-09-26 stsp if (abort_rebase && continue_rebase)
7331 417a6e49 2020-09-26 stsp usage_rebase();
7332 417a6e49 2020-09-26 stsp else if (abort_rebase || continue_rebase) {
7333 417a6e49 2020-09-26 stsp if (argc != 0)
7334 417a6e49 2020-09-26 stsp usage_rebase();
7335 417a6e49 2020-09-26 stsp } else if (argc != 1)
7336 417a6e49 2020-09-26 stsp usage_rebase();
7337 417a6e49 2020-09-26 stsp
7338 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
7339 417a6e49 2020-09-26 stsp if (cwd == NULL) {
7340 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
7341 417a6e49 2020-09-26 stsp goto done;
7342 417a6e49 2020-09-26 stsp }
7343 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
7344 417a6e49 2020-09-26 stsp if (error) {
7345 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7346 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7347 417a6e49 2020-09-26 stsp goto done;
7348 417a6e49 2020-09-26 stsp }
7349 417a6e49 2020-09-26 stsp
7350 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7351 417a6e49 2020-09-26 stsp NULL);
7352 417a6e49 2020-09-26 stsp if (error != NULL)
7353 417a6e49 2020-09-26 stsp goto done;
7354 417a6e49 2020-09-26 stsp
7355 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7356 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
7357 417a6e49 2020-09-26 stsp if (error)
7358 417a6e49 2020-09-26 stsp goto done;
7359 417a6e49 2020-09-26 stsp
7360 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7361 417a6e49 2020-09-26 stsp worktree);
7362 417a6e49 2020-09-26 stsp if (error)
7363 417a6e49 2020-09-26 stsp goto done;
7364 417a6e49 2020-09-26 stsp if (histedit_in_progress) {
7365 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7366 417a6e49 2020-09-26 stsp goto done;
7367 417a6e49 2020-09-26 stsp }
7368 417a6e49 2020-09-26 stsp
7369 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7370 417a6e49 2020-09-26 stsp if (error)
7371 417a6e49 2020-09-26 stsp goto done;
7372 417a6e49 2020-09-26 stsp
7373 417a6e49 2020-09-26 stsp if (abort_rebase) {
7374 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7375 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7376 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7377 417a6e49 2020-09-26 stsp goto done;
7378 417a6e49 2020-09-26 stsp }
7379 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7380 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7381 417a6e49 2020-09-26 stsp worktree, repo);
7382 417a6e49 2020-09-26 stsp if (error)
7383 417a6e49 2020-09-26 stsp goto done;
7384 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
7385 417a6e49 2020-09-26 stsp got_ref_get_symref_target(new_base_branch));
7386 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7387 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7388 417a6e49 2020-09-26 stsp new_base_branch, update_progress, &upa);
7389 417a6e49 2020-09-26 stsp if (error)
7390 417a6e49 2020-09-26 stsp goto done;
7391 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7392 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7393 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
7394 417a6e49 2020-09-26 stsp }
7395 417a6e49 2020-09-26 stsp
7396 417a6e49 2020-09-26 stsp if (continue_rebase) {
7397 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7398 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7399 417a6e49 2020-09-26 stsp goto done;
7400 417a6e49 2020-09-26 stsp }
7401 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7402 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7403 417a6e49 2020-09-26 stsp worktree, repo);
7404 417a6e49 2020-09-26 stsp if (error)
7405 417a6e49 2020-09-26 stsp goto done;
7406 417a6e49 2020-09-26 stsp
7407 417a6e49 2020-09-26 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7408 417a6e49 2020-09-26 stsp resume_commit_id, repo);
7409 417a6e49 2020-09-26 stsp if (error)
7410 417a6e49 2020-09-26 stsp goto done;
7411 417a6e49 2020-09-26 stsp
7412 417a6e49 2020-09-26 stsp yca_id = got_object_id_dup(resume_commit_id);
7413 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7414 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_object_id_dup");
7415 417a6e49 2020-09-26 stsp goto done;
7416 417a6e49 2020-09-26 stsp }
7417 417a6e49 2020-09-26 stsp } else {
7418 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7419 417a6e49 2020-09-26 stsp if (error != NULL)
7420 417a6e49 2020-09-26 stsp goto done;
7421 417a6e49 2020-09-26 stsp }
7422 417a6e49 2020-09-26 stsp
7423 417a6e49 2020-09-26 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7424 417a6e49 2020-09-26 stsp if (error)
7425 417a6e49 2020-09-26 stsp goto done;
7426 417a6e49 2020-09-26 stsp
7427 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7428 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id;
7429 417a6e49 2020-09-26 stsp
7430 417a6e49 2020-09-26 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7431 417a6e49 2020-09-26 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7432 417a6e49 2020-09-26 stsp base_commit_id, branch_head_commit_id, repo,
7433 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7434 417a6e49 2020-09-26 stsp if (error)
7435 417a6e49 2020-09-26 stsp goto done;
7436 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7437 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7438 417a6e49 2020-09-26 stsp "specified branch shares no common ancestry "
7439 417a6e49 2020-09-26 stsp "with work tree's branch");
7440 417a6e49 2020-09-26 stsp goto done;
7441 417a6e49 2020-09-26 stsp }
7442 417a6e49 2020-09-26 stsp
7443 417a6e49 2020-09-26 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7444 417a6e49 2020-09-26 stsp if (error) {
7445 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
7446 417a6e49 2020-09-26 stsp goto done;
7447 417a6e49 2020-09-26 stsp error = NULL;
7448 417a6e49 2020-09-26 stsp } else {
7449 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7450 417a6e49 2020-09-26 stsp "specified branch resolves to a commit which "
7451 417a6e49 2020-09-26 stsp "is already contained in work tree's branch");
7452 417a6e49 2020-09-26 stsp goto done;
7453 417a6e49 2020-09-26 stsp }
7454 417a6e49 2020-09-26 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7455 417a6e49 2020-09-26 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7456 417a6e49 2020-09-26 stsp if (error)
7457 417a6e49 2020-09-26 stsp goto done;
7458 417a6e49 2020-09-26 stsp }
7459 417a6e49 2020-09-26 stsp
7460 417a6e49 2020-09-26 stsp commit_id = branch_head_commit_id;
7461 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7462 417a6e49 2020-09-26 stsp if (error)
7463 417a6e49 2020-09-26 stsp goto done;
7464 417a6e49 2020-09-26 stsp
7465 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7466 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
7467 417a6e49 2020-09-26 stsp if (pid == NULL) {
7468 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7469 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7470 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7471 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7472 417a6e49 2020-09-26 stsp repo, new_base_branch, update_progress, &upa);
7473 417a6e49 2020-09-26 stsp if (error)
7474 417a6e49 2020-09-26 stsp goto done;
7475 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n",
7476 417a6e49 2020-09-26 stsp got_ref_get_name(branch));
7477 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7478 417a6e49 2020-09-26 stsp
7479 417a6e49 2020-09-26 stsp }
7480 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7481 417a6e49 2020-09-26 stsp goto done;
7482 417a6e49 2020-09-26 stsp }
7483 417a6e49 2020-09-26 stsp error = collect_commits(&commits, commit_id, pid->id,
7484 417a6e49 2020-09-26 stsp yca_id, got_worktree_get_path_prefix(worktree),
7485 417a6e49 2020-09-26 stsp GOT_ERR_REBASE_PATH, repo);
7486 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7487 417a6e49 2020-09-26 stsp commit = NULL;
7488 417a6e49 2020-09-26 stsp if (error)
7489 417a6e49 2020-09-26 stsp goto done;
7490 417a6e49 2020-09-26 stsp
7491 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
7492 417a6e49 2020-09-26 stsp if (continue_rebase) {
7493 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex,
7494 417a6e49 2020-09-26 stsp branch, new_base_branch, tmp_branch, repo);
7495 417a6e49 2020-09-26 stsp goto done;
7496 417a6e49 2020-09-26 stsp } else {
7497 417a6e49 2020-09-26 stsp /* Fast-forward the reference of the branch. */
7498 417a6e49 2020-09-26 stsp struct got_object_id *new_head_commit_id;
7499 417a6e49 2020-09-26 stsp char *id_str;
7500 417a6e49 2020-09-26 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7501 417a6e49 2020-09-26 stsp new_base_branch);
7502 417a6e49 2020-09-26 stsp if (error)
7503 417a6e49 2020-09-26 stsp goto done;
7504 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7505 417a6e49 2020-09-26 stsp printf("Forwarding %s to commit %s\n",
7506 417a6e49 2020-09-26 stsp got_ref_get_name(branch), id_str);
7507 417a6e49 2020-09-26 stsp free(id_str);
7508 417a6e49 2020-09-26 stsp error = got_ref_change_ref(branch,
7509 417a6e49 2020-09-26 stsp new_head_commit_id);
7510 417a6e49 2020-09-26 stsp if (error)
7511 417a6e49 2020-09-26 stsp goto done;
7512 417a6e49 2020-09-26 stsp }
7513 417a6e49 2020-09-26 stsp }
7514 417a6e49 2020-09-26 stsp
7515 417a6e49 2020-09-26 stsp pid = NULL;
7516 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7517 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7518 417a6e49 2020-09-26 stsp
7519 417a6e49 2020-09-26 stsp commit_id = qid->id;
7520 417a6e49 2020-09-26 stsp parent_id = pid ? pid->id : yca_id;
7521 417a6e49 2020-09-26 stsp pid = qid;
7522 417a6e49 2020-09-26 stsp
7523 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7524 417a6e49 2020-09-26 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7525 417a6e49 2020-09-26 stsp worktree, fileindex, parent_id, commit_id, repo,
7526 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
7527 417a6e49 2020-09-26 stsp if (error)
7528 417a6e49 2020-09-26 stsp goto done;
7529 417a6e49 2020-09-26 stsp
7530 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7531 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
7532 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
7533 417a6e49 2020-09-26 stsp
7534 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7535 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(qid->id, repo);
7536 417a6e49 2020-09-26 stsp if (error)
7537 417a6e49 2020-09-26 stsp goto done;
7538 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7539 417a6e49 2020-09-26 stsp break;
7540 417a6e49 2020-09-26 stsp }
7541 417a6e49 2020-09-26 stsp
7542 417a6e49 2020-09-26 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7543 417a6e49 2020-09-26 stsp tmp_branch, commit_id, repo);
7544 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7545 417a6e49 2020-09-26 stsp if (error)
7546 417a6e49 2020-09-26 stsp goto done;
7547 417a6e49 2020-09-26 stsp }
7548 417a6e49 2020-09-26 stsp
7549 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7550 417a6e49 2020-09-26 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7551 417a6e49 2020-09-26 stsp if (error)
7552 417a6e49 2020-09-26 stsp goto done;
7553 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7554 417a6e49 2020-09-26 stsp "conflicts must be resolved before rebasing can continue");
7555 417a6e49 2020-09-26 stsp } else
7556 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex, branch,
7557 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, repo);
7558 417a6e49 2020-09-26 stsp done:
7559 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
7560 417a6e49 2020-09-26 stsp free(branch_head_commit_id);
7561 417a6e49 2020-09-26 stsp free(resume_commit_id);
7562 417a6e49 2020-09-26 stsp free(yca_id);
7563 417a6e49 2020-09-26 stsp if (commit)
7564 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7565 417a6e49 2020-09-26 stsp if (branch)
7566 417a6e49 2020-09-26 stsp got_ref_close(branch);
7567 417a6e49 2020-09-26 stsp if (new_base_branch)
7568 417a6e49 2020-09-26 stsp got_ref_close(new_base_branch);
7569 417a6e49 2020-09-26 stsp if (tmp_branch)
7570 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
7571 417a6e49 2020-09-26 stsp if (worktree)
7572 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7573 417a6e49 2020-09-26 stsp if (repo)
7574 417a6e49 2020-09-26 stsp got_repo_close(repo);
7575 417a6e49 2020-09-26 stsp return error;
7576 417a6e49 2020-09-26 stsp }
7577 417a6e49 2020-09-26 stsp
7578 417a6e49 2020-09-26 stsp __dead static void
7579 417a6e49 2020-09-26 stsp usage_histedit(void)
7580 417a6e49 2020-09-26 stsp {
7581 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7582 417a6e49 2020-09-26 stsp getprogname());
7583 417a6e49 2020-09-26 stsp exit(1);
7584 417a6e49 2020-09-26 stsp }
7585 417a6e49 2020-09-26 stsp
7586 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_PICK 'p'
7587 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_EDIT 'e'
7588 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_FOLD 'f'
7589 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_DROP 'd'
7590 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_MESG 'm'
7591 417a6e49 2020-09-26 stsp
7592 417a6e49 2020-09-26 stsp static struct got_histedit_cmd {
7593 417a6e49 2020-09-26 stsp unsigned char code;
7594 417a6e49 2020-09-26 stsp const char *name;
7595 417a6e49 2020-09-26 stsp const char *desc;
7596 417a6e49 2020-09-26 stsp } got_histedit_cmds[] = {
7597 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7598 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7599 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7600 417a6e49 2020-09-26 stsp "be used" },
7601 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7602 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_MESG, "mesg",
7603 417a6e49 2020-09-26 stsp "single-line log message for commit above (open editor if empty)" },
7604 417a6e49 2020-09-26 stsp };
7605 417a6e49 2020-09-26 stsp
7606 417a6e49 2020-09-26 stsp struct got_histedit_list_entry {
7607 417a6e49 2020-09-26 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7608 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
7609 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7610 417a6e49 2020-09-26 stsp char *logmsg;
7611 417a6e49 2020-09-26 stsp };
7612 417a6e49 2020-09-26 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7613 417a6e49 2020-09-26 stsp
7614 417a6e49 2020-09-26 stsp static const struct got_error *
7615 417a6e49 2020-09-26 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7616 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7617 417a6e49 2020-09-26 stsp {
7618 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7619 417a6e49 2020-09-26 stsp char *logmsg = NULL, *id_str = NULL;
7620 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7621 417a6e49 2020-09-26 stsp int n;
7622 417a6e49 2020-09-26 stsp
7623 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7624 417a6e49 2020-09-26 stsp if (err)
7625 417a6e49 2020-09-26 stsp goto done;
7626 417a6e49 2020-09-26 stsp
7627 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 34, commit);
7628 417a6e49 2020-09-26 stsp if (err)
7629 417a6e49 2020-09-26 stsp goto done;
7630 417a6e49 2020-09-26 stsp
7631 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
7632 417a6e49 2020-09-26 stsp if (err)
7633 417a6e49 2020-09-26 stsp goto done;
7634 417a6e49 2020-09-26 stsp
7635 417a6e49 2020-09-26 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7636 417a6e49 2020-09-26 stsp if (n < 0)
7637 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7638 417a6e49 2020-09-26 stsp done:
7639 417a6e49 2020-09-26 stsp if (commit)
7640 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7641 417a6e49 2020-09-26 stsp free(id_str);
7642 417a6e49 2020-09-26 stsp free(logmsg);
7643 417a6e49 2020-09-26 stsp return err;
7644 417a6e49 2020-09-26 stsp }
7645 417a6e49 2020-09-26 stsp
7646 417a6e49 2020-09-26 stsp static const struct got_error *
7647 417a6e49 2020-09-26 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7648 417a6e49 2020-09-26 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7649 417a6e49 2020-09-26 stsp {
7650 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7651 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7652 417a6e49 2020-09-26 stsp
7653 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7654 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7655 417a6e49 2020-09-26 stsp
7656 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7657 417a6e49 2020-09-26 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7658 417a6e49 2020-09-26 stsp f, repo);
7659 417a6e49 2020-09-26 stsp if (err)
7660 417a6e49 2020-09-26 stsp break;
7661 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
7662 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7663 417a6e49 2020-09-26 stsp if (n < 0) {
7664 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7665 417a6e49 2020-09-26 stsp break;
7666 417a6e49 2020-09-26 stsp }
7667 417a6e49 2020-09-26 stsp }
7668 417a6e49 2020-09-26 stsp }
7669 417a6e49 2020-09-26 stsp
7670 417a6e49 2020-09-26 stsp return err;
7671 417a6e49 2020-09-26 stsp }
7672 417a6e49 2020-09-26 stsp
7673 417a6e49 2020-09-26 stsp static const struct got_error *
7674 417a6e49 2020-09-26 stsp write_cmd_list(FILE *f, const char *branch_name,
7675 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits)
7676 417a6e49 2020-09-26 stsp {
7677 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7678 417a6e49 2020-09-26 stsp int n, i;
7679 417a6e49 2020-09-26 stsp char *id_str;
7680 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7681 417a6e49 2020-09-26 stsp
7682 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(commits);
7683 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
7684 417a6e49 2020-09-26 stsp if (err)
7685 417a6e49 2020-09-26 stsp return err;
7686 417a6e49 2020-09-26 stsp
7687 417a6e49 2020-09-26 stsp n = fprintf(f,
7688 417a6e49 2020-09-26 stsp "# Editing the history of branch '%s' starting at\n"
7689 417a6e49 2020-09-26 stsp "# commit %s\n"
7690 417a6e49 2020-09-26 stsp "# Commits will be processed in order from top to "
7691 417a6e49 2020-09-26 stsp "bottom of this file.\n", branch_name, id_str);
7692 417a6e49 2020-09-26 stsp if (n < 0) {
7693 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7694 417a6e49 2020-09-26 stsp goto done;
7695 417a6e49 2020-09-26 stsp }
7696 417a6e49 2020-09-26 stsp
7697 417a6e49 2020-09-26 stsp n = fprintf(f, "# Available histedit commands:\n");
7698 417a6e49 2020-09-26 stsp if (n < 0) {
7699 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7700 417a6e49 2020-09-26 stsp goto done;
7701 417a6e49 2020-09-26 stsp }
7702 417a6e49 2020-09-26 stsp
7703 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7704 417a6e49 2020-09-26 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7705 417a6e49 2020-09-26 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7706 417a6e49 2020-09-26 stsp cmd->desc);
7707 417a6e49 2020-09-26 stsp if (n < 0) {
7708 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7709 417a6e49 2020-09-26 stsp break;
7710 417a6e49 2020-09-26 stsp }
7711 417a6e49 2020-09-26 stsp }
7712 417a6e49 2020-09-26 stsp done:
7713 417a6e49 2020-09-26 stsp free(id_str);
7714 417a6e49 2020-09-26 stsp return err;
7715 417a6e49 2020-09-26 stsp }
7716 417a6e49 2020-09-26 stsp
7717 417a6e49 2020-09-26 stsp static const struct got_error *
7718 417a6e49 2020-09-26 stsp histedit_syntax_error(int lineno)
7719 417a6e49 2020-09-26 stsp {
7720 417a6e49 2020-09-26 stsp static char msg[42];
7721 417a6e49 2020-09-26 stsp int ret;
7722 417a6e49 2020-09-26 stsp
7723 417a6e49 2020-09-26 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7724 417a6e49 2020-09-26 stsp lineno);
7725 417a6e49 2020-09-26 stsp if (ret == -1 || ret >= sizeof(msg))
7726 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7727 417a6e49 2020-09-26 stsp
7728 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7729 417a6e49 2020-09-26 stsp }
7730 417a6e49 2020-09-26 stsp
7731 417a6e49 2020-09-26 stsp static const struct got_error *
7732 417a6e49 2020-09-26 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7733 417a6e49 2020-09-26 stsp char *logmsg, struct got_repository *repo)
7734 417a6e49 2020-09-26 stsp {
7735 417a6e49 2020-09-26 stsp const struct got_error *err;
7736 417a6e49 2020-09-26 stsp struct got_commit_object *folded_commit = NULL;
7737 417a6e49 2020-09-26 stsp char *id_str, *folded_logmsg = NULL;
7738 417a6e49 2020-09-26 stsp
7739 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7740 417a6e49 2020-09-26 stsp if (err)
7741 417a6e49 2020-09-26 stsp return err;
7742 417a6e49 2020-09-26 stsp
7743 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7744 417a6e49 2020-09-26 stsp if (err)
7745 417a6e49 2020-09-26 stsp goto done;
7746 417a6e49 2020-09-26 stsp
7747 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7748 417a6e49 2020-09-26 stsp if (err)
7749 417a6e49 2020-09-26 stsp goto done;
7750 417a6e49 2020-09-26 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7751 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7752 417a6e49 2020-09-26 stsp folded_logmsg) == -1) {
7753 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7754 417a6e49 2020-09-26 stsp }
7755 417a6e49 2020-09-26 stsp done:
7756 417a6e49 2020-09-26 stsp if (folded_commit)
7757 417a6e49 2020-09-26 stsp got_object_commit_close(folded_commit);
7758 417a6e49 2020-09-26 stsp free(id_str);
7759 417a6e49 2020-09-26 stsp free(folded_logmsg);
7760 417a6e49 2020-09-26 stsp return err;
7761 417a6e49 2020-09-26 stsp }
7762 417a6e49 2020-09-26 stsp
7763 417a6e49 2020-09-26 stsp static struct got_histedit_list_entry *
7764 417a6e49 2020-09-26 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7765 417a6e49 2020-09-26 stsp {
7766 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7767 417a6e49 2020-09-26 stsp
7768 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7769 417a6e49 2020-09-26 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7770 417a6e49 2020-09-26 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7771 417a6e49 2020-09-26 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7772 417a6e49 2020-09-26 stsp folded = prev;
7773 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7774 417a6e49 2020-09-26 stsp }
7775 417a6e49 2020-09-26 stsp
7776 417a6e49 2020-09-26 stsp return folded;
7777 417a6e49 2020-09-26 stsp }
7778 417a6e49 2020-09-26 stsp
7779 417a6e49 2020-09-26 stsp static const struct got_error *
7780 417a6e49 2020-09-26 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7781 417a6e49 2020-09-26 stsp struct got_repository *repo)
7782 417a6e49 2020-09-26 stsp {
7783 417a6e49 2020-09-26 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7784 417a6e49 2020-09-26 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7785 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7786 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7787 417a6e49 2020-09-26 stsp int logmsg_len;
7788 417a6e49 2020-09-26 stsp int fd;
7789 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *folded = NULL;
7790 417a6e49 2020-09-26 stsp
7791 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7792 417a6e49 2020-09-26 stsp if (err)
7793 417a6e49 2020-09-26 stsp return err;
7794 417a6e49 2020-09-26 stsp
7795 417a6e49 2020-09-26 stsp folded = get_folded_commits(hle);
7796 417a6e49 2020-09-26 stsp if (folded) {
7797 417a6e49 2020-09-26 stsp while (folded != hle) {
7798 417a6e49 2020-09-26 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7799 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7800 417a6e49 2020-09-26 stsp continue;
7801 417a6e49 2020-09-26 stsp }
7802 417a6e49 2020-09-26 stsp err = append_folded_commit_msg(&new_msg, folded,
7803 417a6e49 2020-09-26 stsp logmsg, repo);
7804 417a6e49 2020-09-26 stsp if (err)
7805 417a6e49 2020-09-26 stsp goto done;
7806 417a6e49 2020-09-26 stsp free(logmsg);
7807 417a6e49 2020-09-26 stsp logmsg = new_msg;
7808 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7809 417a6e49 2020-09-26 stsp }
7810 417a6e49 2020-09-26 stsp }
7811 417a6e49 2020-09-26 stsp
7812 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7813 417a6e49 2020-09-26 stsp if (err)
7814 417a6e49 2020-09-26 stsp goto done;
7815 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7816 417a6e49 2020-09-26 stsp if (err)
7817 417a6e49 2020-09-26 stsp goto done;
7818 417a6e49 2020-09-26 stsp logmsg_len = asprintf(&new_msg,
7819 417a6e49 2020-09-26 stsp "%s\n# original log message of commit %s: %s",
7820 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", id_str, orig_logmsg);
7821 417a6e49 2020-09-26 stsp if (logmsg_len == -1) {
7822 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7823 417a6e49 2020-09-26 stsp goto done;
7824 417a6e49 2020-09-26 stsp }
7825 417a6e49 2020-09-26 stsp free(logmsg);
7826 417a6e49 2020-09-26 stsp logmsg = new_msg;
7827 417a6e49 2020-09-26 stsp
7828 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7829 417a6e49 2020-09-26 stsp if (err)
7830 417a6e49 2020-09-26 stsp goto done;
7831 417a6e49 2020-09-26 stsp
7832 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7833 417a6e49 2020-09-26 stsp GOT_TMPDIR_STR "/got-logmsg");
7834 417a6e49 2020-09-26 stsp if (err)
7835 417a6e49 2020-09-26 stsp goto done;
7836 417a6e49 2020-09-26 stsp
7837 417a6e49 2020-09-26 stsp write(fd, logmsg, logmsg_len);
7838 417a6e49 2020-09-26 stsp close(fd);
7839 417a6e49 2020-09-26 stsp
7840 417a6e49 2020-09-26 stsp err = get_editor(&editor);
7841 417a6e49 2020-09-26 stsp if (err)
7842 417a6e49 2020-09-26 stsp goto done;
7843 417a6e49 2020-09-26 stsp
7844 417a6e49 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7845 417a6e49 2020-09-26 stsp if (err) {
7846 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7847 417a6e49 2020-09-26 stsp goto done;
7848 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7849 417a6e49 2020-09-26 stsp }
7850 417a6e49 2020-09-26 stsp done:
7851 417a6e49 2020-09-26 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7852 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", logmsg_path);
7853 417a6e49 2020-09-26 stsp free(logmsg_path);
7854 417a6e49 2020-09-26 stsp free(logmsg);
7855 417a6e49 2020-09-26 stsp free(orig_logmsg);
7856 417a6e49 2020-09-26 stsp free(editor);
7857 417a6e49 2020-09-26 stsp if (commit)
7858 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7859 417a6e49 2020-09-26 stsp return err;
7860 417a6e49 2020-09-26 stsp }
7861 417a6e49 2020-09-26 stsp
7862 417a6e49 2020-09-26 stsp static const struct got_error *
7863 417a6e49 2020-09-26 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7864 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7865 417a6e49 2020-09-26 stsp {
7866 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7867 417a6e49 2020-09-26 stsp char *line = NULL, *p, *end;
7868 417a6e49 2020-09-26 stsp size_t size;
7869 417a6e49 2020-09-26 stsp ssize_t len;
7870 417a6e49 2020-09-26 stsp int lineno = 0, i;
7871 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7872 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
7873 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle = NULL;
7874 417a6e49 2020-09-26 stsp
7875 417a6e49 2020-09-26 stsp for (;;) {
7876 417a6e49 2020-09-26 stsp len = getline(&line, &size, f);
7877 417a6e49 2020-09-26 stsp if (len == -1) {
7878 417a6e49 2020-09-26 stsp const struct got_error *getline_err;
7879 417a6e49 2020-09-26 stsp if (feof(f))
7880 417a6e49 2020-09-26 stsp break;
7881 417a6e49 2020-09-26 stsp getline_err = got_error_from_errno("getline");
7882 417a6e49 2020-09-26 stsp err = got_ferror(f, getline_err->code);
7883 417a6e49 2020-09-26 stsp break;
7884 417a6e49 2020-09-26 stsp }
7885 417a6e49 2020-09-26 stsp lineno++;
7886 417a6e49 2020-09-26 stsp p = line;
7887 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7888 417a6e49 2020-09-26 stsp p++;
7889 417a6e49 2020-09-26 stsp if (p[0] == '#' || p[0] == '\0') {
7890 417a6e49 2020-09-26 stsp free(line);
7891 417a6e49 2020-09-26 stsp line = NULL;
7892 417a6e49 2020-09-26 stsp continue;
7893 417a6e49 2020-09-26 stsp }
7894 417a6e49 2020-09-26 stsp cmd = NULL;
7895 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7896 417a6e49 2020-09-26 stsp cmd = &got_histedit_cmds[i];
7897 417a6e49 2020-09-26 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7898 417a6e49 2020-09-26 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7899 417a6e49 2020-09-26 stsp p += strlen(cmd->name);
7900 417a6e49 2020-09-26 stsp break;
7901 417a6e49 2020-09-26 stsp }
7902 417a6e49 2020-09-26 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7903 417a6e49 2020-09-26 stsp p++;
7904 417a6e49 2020-09-26 stsp break;
7905 417a6e49 2020-09-26 stsp }
7906 417a6e49 2020-09-26 stsp }
7907 417a6e49 2020-09-26 stsp if (i == nitems(got_histedit_cmds)) {
7908 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7909 417a6e49 2020-09-26 stsp break;
7910 417a6e49 2020-09-26 stsp }
7911 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7912 417a6e49 2020-09-26 stsp p++;
7913 417a6e49 2020-09-26 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7914 417a6e49 2020-09-26 stsp if (hle == NULL || hle->logmsg != NULL) {
7915 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7916 417a6e49 2020-09-26 stsp break;
7917 417a6e49 2020-09-26 stsp }
7918 417a6e49 2020-09-26 stsp if (p[0] == '\0') {
7919 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
7920 417a6e49 2020-09-26 stsp if (err)
7921 417a6e49 2020-09-26 stsp break;
7922 417a6e49 2020-09-26 stsp } else {
7923 417a6e49 2020-09-26 stsp hle->logmsg = strdup(p);
7924 417a6e49 2020-09-26 stsp if (hle->logmsg == NULL) {
7925 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7926 417a6e49 2020-09-26 stsp break;
7927 417a6e49 2020-09-26 stsp }
7928 417a6e49 2020-09-26 stsp }
7929 417a6e49 2020-09-26 stsp free(line);
7930 417a6e49 2020-09-26 stsp line = NULL;
7931 417a6e49 2020-09-26 stsp continue;
7932 417a6e49 2020-09-26 stsp } else {
7933 417a6e49 2020-09-26 stsp end = p;
7934 417a6e49 2020-09-26 stsp while (end[0] && !isspace((unsigned char)end[0]))
7935 417a6e49 2020-09-26 stsp end++;
7936 417a6e49 2020-09-26 stsp *end = '\0';
7937 417a6e49 2020-09-26 stsp
7938 417a6e49 2020-09-26 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7939 417a6e49 2020-09-26 stsp if (err) {
7940 417a6e49 2020-09-26 stsp /* override error code */
7941 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7942 417a6e49 2020-09-26 stsp break;
7943 417a6e49 2020-09-26 stsp }
7944 417a6e49 2020-09-26 stsp }
7945 417a6e49 2020-09-26 stsp hle = malloc(sizeof(*hle));
7946 417a6e49 2020-09-26 stsp if (hle == NULL) {
7947 417a6e49 2020-09-26 stsp err = got_error_from_errno("malloc");
7948 417a6e49 2020-09-26 stsp break;
7949 417a6e49 2020-09-26 stsp }
7950 417a6e49 2020-09-26 stsp hle->cmd = cmd;
7951 417a6e49 2020-09-26 stsp hle->commit_id = commit_id;
7952 417a6e49 2020-09-26 stsp hle->logmsg = NULL;
7953 417a6e49 2020-09-26 stsp commit_id = NULL;
7954 417a6e49 2020-09-26 stsp free(line);
7955 417a6e49 2020-09-26 stsp line = NULL;
7956 417a6e49 2020-09-26 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7957 417a6e49 2020-09-26 stsp }
7958 417a6e49 2020-09-26 stsp
7959 417a6e49 2020-09-26 stsp free(line);
7960 417a6e49 2020-09-26 stsp free(commit_id);
7961 417a6e49 2020-09-26 stsp return err;
7962 417a6e49 2020-09-26 stsp }
7963 417a6e49 2020-09-26 stsp
7964 417a6e49 2020-09-26 stsp static const struct got_error *
7965 417a6e49 2020-09-26 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7966 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7967 417a6e49 2020-09-26 stsp {
7968 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7969 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7970 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
7971 417a6e49 2020-09-26 stsp static char msg[92];
7972 417a6e49 2020-09-26 stsp char *id_str;
7973 417a6e49 2020-09-26 stsp
7974 417a6e49 2020-09-26 stsp if (TAILQ_EMPTY(histedit_cmds))
7975 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7976 417a6e49 2020-09-26 stsp "histedit script contains no commands");
7977 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7978 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7979 417a6e49 2020-09-26 stsp
7980 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7981 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle2;
7982 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7983 417a6e49 2020-09-26 stsp if (hle == hle2)
7984 417a6e49 2020-09-26 stsp continue;
7985 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
7986 417a6e49 2020-09-26 stsp hle2->commit_id) != 0)
7987 417a6e49 2020-09-26 stsp continue;
7988 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7989 417a6e49 2020-09-26 stsp if (err)
7990 417a6e49 2020-09-26 stsp return err;
7991 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7992 417a6e49 2020-09-26 stsp "more than once in histedit script", id_str);
7993 417a6e49 2020-09-26 stsp free(id_str);
7994 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7995 417a6e49 2020-09-26 stsp }
7996 417a6e49 2020-09-26 stsp }
7997 417a6e49 2020-09-26 stsp
7998 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7999 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8000 417a6e49 2020-09-26 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8001 417a6e49 2020-09-26 stsp break;
8002 417a6e49 2020-09-26 stsp }
8003 417a6e49 2020-09-26 stsp if (hle == NULL) {
8004 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
8005 417a6e49 2020-09-26 stsp if (err)
8006 417a6e49 2020-09-26 stsp return err;
8007 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
8008 417a6e49 2020-09-26 stsp "commit %s missing from histedit script", id_str);
8009 417a6e49 2020-09-26 stsp free(id_str);
8010 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8011 417a6e49 2020-09-26 stsp }
8012 417a6e49 2020-09-26 stsp }
8013 417a6e49 2020-09-26 stsp
8014 417a6e49 2020-09-26 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8015 417a6e49 2020-09-26 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8016 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8017 417a6e49 2020-09-26 stsp "last commit in histedit script cannot be folded");
8018 417a6e49 2020-09-26 stsp
8019 417a6e49 2020-09-26 stsp return NULL;
8020 417a6e49 2020-09-26 stsp }
8021 417a6e49 2020-09-26 stsp
8022 417a6e49 2020-09-26 stsp static const struct got_error *
8023 417a6e49 2020-09-26 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8024 417a6e49 2020-09-26 stsp const char *path, struct got_object_id_queue *commits,
8025 417a6e49 2020-09-26 stsp struct got_repository *repo)
8026 417a6e49 2020-09-26 stsp {
8027 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8028 417a6e49 2020-09-26 stsp char *editor;
8029 417a6e49 2020-09-26 stsp FILE *f = NULL;
8030 417a6e49 2020-09-26 stsp
8031 417a6e49 2020-09-26 stsp err = get_editor(&editor);
8032 417a6e49 2020-09-26 stsp if (err)
8033 417a6e49 2020-09-26 stsp return err;
8034 417a6e49 2020-09-26 stsp
8035 417a6e49 2020-09-26 stsp if (spawn_editor(editor, path) == -1) {
8036 417a6e49 2020-09-26 stsp err = got_error_from_errno("failed spawning editor");
8037 417a6e49 2020-09-26 stsp goto done;
8038 417a6e49 2020-09-26 stsp }
8039 417a6e49 2020-09-26 stsp
8040 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8041 417a6e49 2020-09-26 stsp if (f == NULL) {
8042 417a6e49 2020-09-26 stsp err = got_error_from_errno("fopen");
8043 417a6e49 2020-09-26 stsp goto done;
8044 417a6e49 2020-09-26 stsp }
8045 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8046 417a6e49 2020-09-26 stsp if (err)
8047 417a6e49 2020-09-26 stsp goto done;
8048 417a6e49 2020-09-26 stsp
8049 417a6e49 2020-09-26 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8050 417a6e49 2020-09-26 stsp done:
8051 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8052 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8053 417a6e49 2020-09-26 stsp free(editor);
8054 417a6e49 2020-09-26 stsp return err;
8055 417a6e49 2020-09-26 stsp }
8056 417a6e49 2020-09-26 stsp
8057 417a6e49 2020-09-26 stsp static const struct got_error *
8058 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8059 417a6e49 2020-09-26 stsp struct got_object_id_queue *, const char *, const char *,
8060 417a6e49 2020-09-26 stsp struct got_repository *);
8061 417a6e49 2020-09-26 stsp
8062 417a6e49 2020-09-26 stsp static const struct got_error *
8063 417a6e49 2020-09-26 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8064 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, const char *branch_name,
8065 417a6e49 2020-09-26 stsp int edit_logmsg_only, struct got_repository *repo)
8066 417a6e49 2020-09-26 stsp {
8067 417a6e49 2020-09-26 stsp const struct got_error *err;
8068 417a6e49 2020-09-26 stsp FILE *f = NULL;
8069 417a6e49 2020-09-26 stsp char *path = NULL;
8070 417a6e49 2020-09-26 stsp
8071 417a6e49 2020-09-26 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8072 417a6e49 2020-09-26 stsp if (err)
8073 417a6e49 2020-09-26 stsp return err;
8074 417a6e49 2020-09-26 stsp
8075 417a6e49 2020-09-26 stsp err = write_cmd_list(f, branch_name, commits);
8076 417a6e49 2020-09-26 stsp if (err)
8077 417a6e49 2020-09-26 stsp goto done;
8078 417a6e49 2020-09-26 stsp
8079 417a6e49 2020-09-26 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8080 417a6e49 2020-09-26 stsp if (err)
8081 417a6e49 2020-09-26 stsp goto done;
8082 417a6e49 2020-09-26 stsp
8083 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
8084 417a6e49 2020-09-26 stsp rewind(f);
8085 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8086 417a6e49 2020-09-26 stsp } else {
8087 417a6e49 2020-09-26 stsp if (fclose(f) != 0) {
8088 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8089 417a6e49 2020-09-26 stsp goto done;
8090 417a6e49 2020-09-26 stsp }
8091 417a6e49 2020-09-26 stsp f = NULL;
8092 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8093 417a6e49 2020-09-26 stsp if (err) {
8094 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8095 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8096 417a6e49 2020-09-26 stsp goto done;
8097 417a6e49 2020-09-26 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8098 417a6e49 2020-09-26 stsp commits, path, branch_name, repo);
8099 417a6e49 2020-09-26 stsp }
8100 417a6e49 2020-09-26 stsp }
8101 417a6e49 2020-09-26 stsp done:
8102 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8103 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8104 417a6e49 2020-09-26 stsp if (path && unlink(path) != 0 && err == NULL)
8105 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", path);
8106 417a6e49 2020-09-26 stsp free(path);
8107 417a6e49 2020-09-26 stsp return err;
8108 417a6e49 2020-09-26 stsp }
8109 417a6e49 2020-09-26 stsp
8110 417a6e49 2020-09-26 stsp static const struct got_error *
8111 417a6e49 2020-09-26 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8112 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8113 417a6e49 2020-09-26 stsp {
8114 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8115 417a6e49 2020-09-26 stsp char *path = NULL;
8116 417a6e49 2020-09-26 stsp FILE *f = NULL;
8117 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8118 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8119 417a6e49 2020-09-26 stsp
8120 417a6e49 2020-09-26 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8121 417a6e49 2020-09-26 stsp if (err)
8122 417a6e49 2020-09-26 stsp return err;
8123 417a6e49 2020-09-26 stsp
8124 417a6e49 2020-09-26 stsp f = fopen(path, "w");
8125 417a6e49 2020-09-26 stsp if (f == NULL) {
8126 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8127 417a6e49 2020-09-26 stsp goto done;
8128 417a6e49 2020-09-26 stsp }
8129 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8130 417a6e49 2020-09-26 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8131 417a6e49 2020-09-26 stsp repo);
8132 417a6e49 2020-09-26 stsp if (err)
8133 417a6e49 2020-09-26 stsp break;
8134 417a6e49 2020-09-26 stsp
8135 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8136 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c %s\n",
8137 417a6e49 2020-09-26 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8138 417a6e49 2020-09-26 stsp if (n < 0) {
8139 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
8140 417a6e49 2020-09-26 stsp break;
8141 417a6e49 2020-09-26 stsp }
8142 417a6e49 2020-09-26 stsp }
8143 417a6e49 2020-09-26 stsp }
8144 417a6e49 2020-09-26 stsp done:
8145 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8146 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8147 417a6e49 2020-09-26 stsp free(path);
8148 417a6e49 2020-09-26 stsp if (commit)
8149 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8150 417a6e49 2020-09-26 stsp return err;
8151 417a6e49 2020-09-26 stsp }
8152 417a6e49 2020-09-26 stsp
8153 417a6e49 2020-09-26 stsp void
8154 417a6e49 2020-09-26 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8155 417a6e49 2020-09-26 stsp {
8156 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8157 417a6e49 2020-09-26 stsp
8158 417a6e49 2020-09-26 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8159 417a6e49 2020-09-26 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8160 417a6e49 2020-09-26 stsp free(hle);
8161 417a6e49 2020-09-26 stsp }
8162 417a6e49 2020-09-26 stsp }
8163 417a6e49 2020-09-26 stsp
8164 417a6e49 2020-09-26 stsp static const struct got_error *
8165 417a6e49 2020-09-26 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8166 417a6e49 2020-09-26 stsp const char *path, struct got_repository *repo)
8167 417a6e49 2020-09-26 stsp {
8168 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8169 417a6e49 2020-09-26 stsp FILE *f = NULL;
8170 417a6e49 2020-09-26 stsp
8171 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8172 417a6e49 2020-09-26 stsp if (f == NULL) {
8173 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8174 417a6e49 2020-09-26 stsp goto done;
8175 417a6e49 2020-09-26 stsp }
8176 417a6e49 2020-09-26 stsp
8177 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8178 417a6e49 2020-09-26 stsp done:
8179 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8180 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8181 417a6e49 2020-09-26 stsp return err;
8182 417a6e49 2020-09-26 stsp }
8183 417a6e49 2020-09-26 stsp
8184 417a6e49 2020-09-26 stsp static const struct got_error *
8185 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8186 417a6e49 2020-09-26 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8187 417a6e49 2020-09-26 stsp const char *path, const char *branch_name, struct got_repository *repo)
8188 417a6e49 2020-09-26 stsp {
8189 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8190 417a6e49 2020-09-26 stsp int resp = ' ';
8191 417a6e49 2020-09-26 stsp
8192 417a6e49 2020-09-26 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8193 417a6e49 2020-09-26 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8194 417a6e49 2020-09-26 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8195 417a6e49 2020-09-26 stsp resp = getchar();
8196 417a6e49 2020-09-26 stsp if (resp == '\n')
8197 417a6e49 2020-09-26 stsp resp = getchar();
8198 417a6e49 2020-09-26 stsp if (resp == 'c') {
8199 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8200 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8201 417a6e49 2020-09-26 stsp repo);
8202 417a6e49 2020-09-26 stsp if (err) {
8203 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8204 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8205 417a6e49 2020-09-26 stsp break;
8206 417a6e49 2020-09-26 stsp prev_err = err;
8207 417a6e49 2020-09-26 stsp resp = ' ';
8208 417a6e49 2020-09-26 stsp continue;
8209 417a6e49 2020-09-26 stsp }
8210 417a6e49 2020-09-26 stsp break;
8211 417a6e49 2020-09-26 stsp } else if (resp == 'r') {
8212 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8213 417a6e49 2020-09-26 stsp err = histedit_edit_script(histedit_cmds,
8214 417a6e49 2020-09-26 stsp commits, branch_name, 0, repo);
8215 417a6e49 2020-09-26 stsp if (err) {
8216 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8217 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8218 417a6e49 2020-09-26 stsp break;
8219 417a6e49 2020-09-26 stsp prev_err = err;
8220 417a6e49 2020-09-26 stsp resp = ' ';
8221 417a6e49 2020-09-26 stsp continue;
8222 417a6e49 2020-09-26 stsp }
8223 417a6e49 2020-09-26 stsp break;
8224 417a6e49 2020-09-26 stsp } else if (resp == 'a') {
8225 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8226 417a6e49 2020-09-26 stsp break;
8227 417a6e49 2020-09-26 stsp } else
8228 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
8229 417a6e49 2020-09-26 stsp }
8230 417a6e49 2020-09-26 stsp
8231 417a6e49 2020-09-26 stsp return err;
8232 417a6e49 2020-09-26 stsp }
8233 417a6e49 2020-09-26 stsp
8234 417a6e49 2020-09-26 stsp static const struct got_error *
8235 417a6e49 2020-09-26 stsp histedit_complete(struct got_worktree *worktree,
8236 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8237 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_repository *repo)
8238 417a6e49 2020-09-26 stsp {
8239 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8240 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8241 417a6e49 2020-09-26 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8242 417a6e49 2020-09-26 stsp branch, repo);
8243 417a6e49 2020-09-26 stsp }
8244 417a6e49 2020-09-26 stsp
8245 417a6e49 2020-09-26 stsp static const struct got_error *
8246 417a6e49 2020-09-26 stsp show_histedit_progress(struct got_commit_object *commit,
8247 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8248 417a6e49 2020-09-26 stsp {
8249 417a6e49 2020-09-26 stsp const struct got_error *err;
8250 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8251 417a6e49 2020-09-26 stsp
8252 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8253 417a6e49 2020-09-26 stsp if (err)
8254 417a6e49 2020-09-26 stsp goto done;
8255 417a6e49 2020-09-26 stsp
8256 417a6e49 2020-09-26 stsp if (new_id) {
8257 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
8258 417a6e49 2020-09-26 stsp if (err)
8259 417a6e49 2020-09-26 stsp goto done;
8260 417a6e49 2020-09-26 stsp }
8261 417a6e49 2020-09-26 stsp
8262 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
8263 417a6e49 2020-09-26 stsp if (new_id_str)
8264 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
8265 417a6e49 2020-09-26 stsp
8266 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8267 417a6e49 2020-09-26 stsp logmsg = strdup(hle->logmsg);
8268 417a6e49 2020-09-26 stsp if (logmsg == NULL) {
8269 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
8270 417a6e49 2020-09-26 stsp goto done;
8271 417a6e49 2020-09-26 stsp }
8272 417a6e49 2020-09-26 stsp trim_logmsg(logmsg, 42);
8273 417a6e49 2020-09-26 stsp } else {
8274 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
8275 417a6e49 2020-09-26 stsp if (err)
8276 417a6e49 2020-09-26 stsp goto done;
8277 417a6e49 2020-09-26 stsp }
8278 417a6e49 2020-09-26 stsp
8279 417a6e49 2020-09-26 stsp switch (hle->cmd->code) {
8280 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_PICK:
8281 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_EDIT:
8282 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
8283 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8284 417a6e49 2020-09-26 stsp break;
8285 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_DROP:
8286 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_FOLD:
8287 417a6e49 2020-09-26 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8288 417a6e49 2020-09-26 stsp logmsg);
8289 417a6e49 2020-09-26 stsp break;
8290 417a6e49 2020-09-26 stsp default:
8291 417a6e49 2020-09-26 stsp break;
8292 417a6e49 2020-09-26 stsp }
8293 417a6e49 2020-09-26 stsp done:
8294 417a6e49 2020-09-26 stsp free(old_id_str);
8295 417a6e49 2020-09-26 stsp free(new_id_str);
8296 417a6e49 2020-09-26 stsp return err;
8297 417a6e49 2020-09-26 stsp }
8298 417a6e49 2020-09-26 stsp
8299 417a6e49 2020-09-26 stsp static const struct got_error *
8300 417a6e49 2020-09-26 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8301 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8302 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8303 417a6e49 2020-09-26 stsp struct got_repository *repo)
8304 417a6e49 2020-09-26 stsp {
8305 417a6e49 2020-09-26 stsp const struct got_error *err;
8306 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8307 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
8308 417a6e49 2020-09-26 stsp
8309 417a6e49 2020-09-26 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8310 417a6e49 2020-09-26 stsp && hle->logmsg == NULL) {
8311 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
8312 417a6e49 2020-09-26 stsp if (err)
8313 417a6e49 2020-09-26 stsp return err;
8314 417a6e49 2020-09-26 stsp }
8315 417a6e49 2020-09-26 stsp
8316 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8317 417a6e49 2020-09-26 stsp if (err)
8318 417a6e49 2020-09-26 stsp return err;
8319 417a6e49 2020-09-26 stsp
8320 417a6e49 2020-09-26 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8321 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8322 417a6e49 2020-09-26 stsp hle->logmsg, repo);
8323 417a6e49 2020-09-26 stsp if (err) {
8324 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8325 417a6e49 2020-09-26 stsp goto done;
8326 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, NULL);
8327 417a6e49 2020-09-26 stsp } else {
8328 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8329 417a6e49 2020-09-26 stsp free(new_commit_id);
8330 417a6e49 2020-09-26 stsp }
8331 417a6e49 2020-09-26 stsp done:
8332 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8333 417a6e49 2020-09-26 stsp return err;
8334 417a6e49 2020-09-26 stsp }
8335 417a6e49 2020-09-26 stsp
8336 417a6e49 2020-09-26 stsp static const struct got_error *
8337 417a6e49 2020-09-26 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8338 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8339 417a6e49 2020-09-26 stsp {
8340 417a6e49 2020-09-26 stsp const struct got_error *error;
8341 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8342 417a6e49 2020-09-26 stsp
8343 417a6e49 2020-09-26 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8344 417a6e49 2020-09-26 stsp repo);
8345 417a6e49 2020-09-26 stsp if (error)
8346 417a6e49 2020-09-26 stsp return error;
8347 417a6e49 2020-09-26 stsp
8348 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8349 417a6e49 2020-09-26 stsp if (error)
8350 417a6e49 2020-09-26 stsp return error;
8351 417a6e49 2020-09-26 stsp
8352 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit, hle, NULL);
8353 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8354 417a6e49 2020-09-26 stsp return error;
8355 417a6e49 2020-09-26 stsp }
8356 417a6e49 2020-09-26 stsp
8357 417a6e49 2020-09-26 stsp static const struct got_error *
8358 417a6e49 2020-09-26 stsp check_local_changes(void *arg, unsigned char status,
8359 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path,
8360 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8361 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8362 417a6e49 2020-09-26 stsp {
8363 417a6e49 2020-09-26 stsp int *have_local_changes = arg;
8364 417a6e49 2020-09-26 stsp
8365 417a6e49 2020-09-26 stsp switch (status) {
8366 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8367 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8368 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8369 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
8370 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8371 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8372 417a6e49 2020-09-26 stsp default:
8373 417a6e49 2020-09-26 stsp break;
8374 417a6e49 2020-09-26 stsp }
8375 417a6e49 2020-09-26 stsp
8376 417a6e49 2020-09-26 stsp switch (staged_status) {
8377 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8378 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8379 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8380 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8381 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8382 417a6e49 2020-09-26 stsp default:
8383 417a6e49 2020-09-26 stsp break;
8384 417a6e49 2020-09-26 stsp }
8385 417a6e49 2020-09-26 stsp
8386 417a6e49 2020-09-26 stsp return NULL;
8387 417a6e49 2020-09-26 stsp }
8388 417a6e49 2020-09-26 stsp
8389 417a6e49 2020-09-26 stsp static const struct got_error *
8390 417a6e49 2020-09-26 stsp cmd_histedit(int argc, char *argv[])
8391 417a6e49 2020-09-26 stsp {
8392 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8393 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8394 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8395 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8396 417a6e49 2020-09-26 stsp char *cwd = NULL;
8397 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
8398 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch = NULL;
8399 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
8400 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id = NULL;
8401 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
8402 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8403 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0;
8404 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8405 417a6e49 2020-09-26 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8406 417a6e49 2020-09-26 stsp int edit_logmsg_only = 0;
8407 417a6e49 2020-09-26 stsp const char *edit_script_path = NULL;
8408 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8409 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
8410 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
8411 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
8412 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
8413 417a6e49 2020-09-26 stsp struct got_histedit_list histedit_cmds;
8414 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8415 417a6e49 2020-09-26 stsp
8416 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
8417 417a6e49 2020-09-26 stsp TAILQ_INIT(&histedit_cmds);
8418 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
8419 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8420 417a6e49 2020-09-26 stsp
8421 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8422 417a6e49 2020-09-26 stsp switch (ch) {
8423 417a6e49 2020-09-26 stsp case 'a':
8424 417a6e49 2020-09-26 stsp abort_edit = 1;
8425 417a6e49 2020-09-26 stsp break;
8426 417a6e49 2020-09-26 stsp case 'c':
8427 417a6e49 2020-09-26 stsp continue_edit = 1;
8428 417a6e49 2020-09-26 stsp break;
8429 417a6e49 2020-09-26 stsp case 'F':
8430 417a6e49 2020-09-26 stsp edit_script_path = optarg;
8431 417a6e49 2020-09-26 stsp break;
8432 417a6e49 2020-09-26 stsp case 'm':
8433 417a6e49 2020-09-26 stsp edit_logmsg_only = 1;
8434 417a6e49 2020-09-26 stsp break;
8435 417a6e49 2020-09-26 stsp default:
8436 417a6e49 2020-09-26 stsp usage_histedit();
8437 417a6e49 2020-09-26 stsp /* NOTREACHED */
8438 417a6e49 2020-09-26 stsp }
8439 417a6e49 2020-09-26 stsp }
8440 417a6e49 2020-09-26 stsp
8441 417a6e49 2020-09-26 stsp argc -= optind;
8442 417a6e49 2020-09-26 stsp argv += optind;
8443 417a6e49 2020-09-26 stsp
8444 417a6e49 2020-09-26 stsp #ifndef PROFILE
8445 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8446 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8447 417a6e49 2020-09-26 stsp err(1, "pledge");
8448 417a6e49 2020-09-26 stsp #endif
8449 417a6e49 2020-09-26 stsp if (abort_edit && continue_edit)
8450 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8451 417a6e49 2020-09-26 stsp if (edit_script_path && edit_logmsg_only)
8452 417a6e49 2020-09-26 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8453 417a6e49 2020-09-26 stsp if (abort_edit && edit_logmsg_only)
8454 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8455 417a6e49 2020-09-26 stsp if (continue_edit && edit_logmsg_only)
8456 417a6e49 2020-09-26 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8457 417a6e49 2020-09-26 stsp if (argc != 0)
8458 417a6e49 2020-09-26 stsp usage_histedit();
8459 417a6e49 2020-09-26 stsp
8460 417a6e49 2020-09-26 stsp /*
8461 417a6e49 2020-09-26 stsp * This command cannot apply unveil(2) in all cases because the
8462 417a6e49 2020-09-26 stsp * user may choose to run an editor to edit the histedit script
8463 417a6e49 2020-09-26 stsp * and to edit individual commit log messages.
8464 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8465 417a6e49 2020-09-26 stsp * apply unveil after edit script and log messages have been written.
8466 417a6e49 2020-09-26 stsp * XXX TODO: Make use of unveil(2) where possible.
8467 417a6e49 2020-09-26 stsp */
8468 417a6e49 2020-09-26 stsp
8469 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8470 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8471 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8472 417a6e49 2020-09-26 stsp goto done;
8473 417a6e49 2020-09-26 stsp }
8474 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8475 417a6e49 2020-09-26 stsp if (error) {
8476 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8477 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8478 417a6e49 2020-09-26 stsp goto done;
8479 417a6e49 2020-09-26 stsp }
8480 417a6e49 2020-09-26 stsp
8481 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8482 417a6e49 2020-09-26 stsp NULL);
8483 417a6e49 2020-09-26 stsp if (error != NULL)
8484 417a6e49 2020-09-26 stsp goto done;
8485 417a6e49 2020-09-26 stsp
8486 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8487 417a6e49 2020-09-26 stsp if (error)
8488 417a6e49 2020-09-26 stsp goto done;
8489 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
8490 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
8491 417a6e49 2020-09-26 stsp goto done;
8492 417a6e49 2020-09-26 stsp }
8493 417a6e49 2020-09-26 stsp
8494 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8495 417a6e49 2020-09-26 stsp if (error)
8496 417a6e49 2020-09-26 stsp goto done;
8497 417a6e49 2020-09-26 stsp
8498 417a6e49 2020-09-26 stsp if (edit_in_progress && edit_logmsg_only) {
8499 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8500 417a6e49 2020-09-26 stsp "histedit operation is in progress in this "
8501 417a6e49 2020-09-26 stsp "work tree and must be continued or aborted "
8502 417a6e49 2020-09-26 stsp "before the -m option can be used");
8503 417a6e49 2020-09-26 stsp goto done;
8504 417a6e49 2020-09-26 stsp }
8505 417a6e49 2020-09-26 stsp
8506 417a6e49 2020-09-26 stsp if (edit_in_progress && abort_edit) {
8507 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8508 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8509 417a6e49 2020-09-26 stsp worktree, repo);
8510 417a6e49 2020-09-26 stsp if (error)
8511 417a6e49 2020-09-26 stsp goto done;
8512 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8513 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8514 417a6e49 2020-09-26 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8515 417a6e49 2020-09-26 stsp branch, base_commit_id, update_progress, &upa);
8516 417a6e49 2020-09-26 stsp if (error)
8517 417a6e49 2020-09-26 stsp goto done;
8518 417a6e49 2020-09-26 stsp printf("Histedit of %s aborted\n",
8519 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8520 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8521 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
8522 417a6e49 2020-09-26 stsp } else if (abort_edit) {
8523 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8524 417a6e49 2020-09-26 stsp goto done;
8525 417a6e49 2020-09-26 stsp }
8526 417a6e49 2020-09-26 stsp
8527 417a6e49 2020-09-26 stsp if (continue_edit) {
8528 417a6e49 2020-09-26 stsp char *path;
8529 417a6e49 2020-09-26 stsp
8530 417a6e49 2020-09-26 stsp if (!edit_in_progress) {
8531 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8532 417a6e49 2020-09-26 stsp goto done;
8533 417a6e49 2020-09-26 stsp }
8534 417a6e49 2020-09-26 stsp
8535 417a6e49 2020-09-26 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8536 417a6e49 2020-09-26 stsp if (error)
8537 417a6e49 2020-09-26 stsp goto done;
8538 417a6e49 2020-09-26 stsp
8539 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8540 417a6e49 2020-09-26 stsp free(path);
8541 417a6e49 2020-09-26 stsp if (error)
8542 417a6e49 2020-09-26 stsp goto done;
8543 417a6e49 2020-09-26 stsp
8544 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8545 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8546 417a6e49 2020-09-26 stsp worktree, repo);
8547 417a6e49 2020-09-26 stsp if (error)
8548 417a6e49 2020-09-26 stsp goto done;
8549 417a6e49 2020-09-26 stsp
8550 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8551 417a6e49 2020-09-26 stsp if (error)
8552 417a6e49 2020-09-26 stsp goto done;
8553 417a6e49 2020-09-26 stsp
8554 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8555 417a6e49 2020-09-26 stsp head_commit_id);
8556 417a6e49 2020-09-26 stsp if (error)
8557 417a6e49 2020-09-26 stsp goto done;
8558 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8559 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8560 417a6e49 2020-09-26 stsp if (pid == NULL) {
8561 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8562 417a6e49 2020-09-26 stsp goto done;
8563 417a6e49 2020-09-26 stsp }
8564 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8565 417a6e49 2020-09-26 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8566 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8567 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8568 417a6e49 2020-09-26 stsp commit = NULL;
8569 417a6e49 2020-09-26 stsp if (error)
8570 417a6e49 2020-09-26 stsp goto done;
8571 417a6e49 2020-09-26 stsp } else {
8572 417a6e49 2020-09-26 stsp if (edit_in_progress) {
8573 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8574 417a6e49 2020-09-26 stsp goto done;
8575 417a6e49 2020-09-26 stsp }
8576 417a6e49 2020-09-26 stsp
8577 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo,
8578 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8579 417a6e49 2020-09-26 stsp if (error != NULL)
8580 417a6e49 2020-09-26 stsp goto done;
8581 417a6e49 2020-09-26 stsp
8582 417a6e49 2020-09-26 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8583 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8584 417a6e49 2020-09-26 stsp "will not edit commit history of a branch outside "
8585 417a6e49 2020-09-26 stsp "the \"refs/heads/\" reference namespace");
8586 417a6e49 2020-09-26 stsp goto done;
8587 417a6e49 2020-09-26 stsp }
8588 417a6e49 2020-09-26 stsp
8589 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8590 417a6e49 2020-09-26 stsp got_ref_close(branch);
8591 417a6e49 2020-09-26 stsp branch = NULL;
8592 417a6e49 2020-09-26 stsp if (error)
8593 417a6e49 2020-09-26 stsp goto done;
8594 417a6e49 2020-09-26 stsp
8595 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8596 417a6e49 2020-09-26 stsp head_commit_id);
8597 417a6e49 2020-09-26 stsp if (error)
8598 417a6e49 2020-09-26 stsp goto done;
8599 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8600 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8601 417a6e49 2020-09-26 stsp if (pid == NULL) {
8602 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8603 417a6e49 2020-09-26 stsp goto done;
8604 417a6e49 2020-09-26 stsp }
8605 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8606 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree),
8607 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
8608 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8609 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8610 417a6e49 2020-09-26 stsp commit = NULL;
8611 417a6e49 2020-09-26 stsp if (error)
8612 417a6e49 2020-09-26 stsp goto done;
8613 417a6e49 2020-09-26 stsp
8614 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
8615 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8616 417a6e49 2020-09-26 stsp goto done;
8617 417a6e49 2020-09-26 stsp }
8618 417a6e49 2020-09-26 stsp
8619 417a6e49 2020-09-26 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8620 417a6e49 2020-09-26 stsp &base_commit_id, &fileindex, worktree, repo);
8621 417a6e49 2020-09-26 stsp if (error)
8622 417a6e49 2020-09-26 stsp goto done;
8623 417a6e49 2020-09-26 stsp
8624 417a6e49 2020-09-26 stsp if (edit_script_path) {
8625 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds,
8626 417a6e49 2020-09-26 stsp edit_script_path, repo);
8627 417a6e49 2020-09-26 stsp if (error) {
8628 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8629 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8630 417a6e49 2020-09-26 stsp update_progress, &upa);
8631 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8632 417a6e49 2020-09-26 stsp goto done;
8633 417a6e49 2020-09-26 stsp }
8634 417a6e49 2020-09-26 stsp } else {
8635 417a6e49 2020-09-26 stsp const char *branch_name;
8636 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(branch);
8637 417a6e49 2020-09-26 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8638 417a6e49 2020-09-26 stsp branch_name += 11;
8639 417a6e49 2020-09-26 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8640 417a6e49 2020-09-26 stsp branch_name, edit_logmsg_only, repo);
8641 417a6e49 2020-09-26 stsp if (error) {
8642 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8643 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8644 417a6e49 2020-09-26 stsp update_progress, &upa);
8645 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8646 417a6e49 2020-09-26 stsp goto done;
8647 417a6e49 2020-09-26 stsp }
8648 417a6e49 2020-09-26 stsp
8649 417a6e49 2020-09-26 stsp }
8650 417a6e49 2020-09-26 stsp
8651 417a6e49 2020-09-26 stsp error = histedit_save_list(&histedit_cmds, worktree,
8652 417a6e49 2020-09-26 stsp repo);
8653 417a6e49 2020-09-26 stsp if (error) {
8654 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8655 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8656 417a6e49 2020-09-26 stsp update_progress, &upa);
8657 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8658 417a6e49 2020-09-26 stsp goto done;
8659 417a6e49 2020-09-26 stsp }
8660 417a6e49 2020-09-26 stsp
8661 417a6e49 2020-09-26 stsp }
8662 417a6e49 2020-09-26 stsp
8663 417a6e49 2020-09-26 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
8664 417a6e49 2020-09-26 stsp if (error)
8665 417a6e49 2020-09-26 stsp goto done;
8666 417a6e49 2020-09-26 stsp
8667 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8668 417a6e49 2020-09-26 stsp if (resume_commit_id) {
8669 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
8670 417a6e49 2020-09-26 stsp resume_commit_id) != 0)
8671 417a6e49 2020-09-26 stsp continue;
8672 417a6e49 2020-09-26 stsp
8673 417a6e49 2020-09-26 stsp resume_commit_id = NULL;
8674 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8675 417a6e49 2020-09-26 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8676 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree,
8677 417a6e49 2020-09-26 stsp repo);
8678 417a6e49 2020-09-26 stsp if (error)
8679 417a6e49 2020-09-26 stsp goto done;
8680 417a6e49 2020-09-26 stsp } else {
8681 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8682 417a6e49 2020-09-26 stsp int have_changes = 0;
8683 417a6e49 2020-09-26 stsp
8684 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
8685 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
8686 417a6e49 2020-09-26 stsp if (error)
8687 417a6e49 2020-09-26 stsp goto done;
8688 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths,
8689 417a6e49 2020-09-26 stsp repo, check_local_changes, &have_changes,
8690 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8691 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
8692 417a6e49 2020-09-26 stsp if (error) {
8693 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_CANCELLED)
8694 417a6e49 2020-09-26 stsp goto done;
8695 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
8696 417a6e49 2020-09-26 stsp goto done;
8697 417a6e49 2020-09-26 stsp }
8698 417a6e49 2020-09-26 stsp if (have_changes) {
8699 417a6e49 2020-09-26 stsp error = histedit_commit(NULL, worktree,
8700 417a6e49 2020-09-26 stsp fileindex, tmp_branch, hle, repo);
8701 417a6e49 2020-09-26 stsp if (error)
8702 417a6e49 2020-09-26 stsp goto done;
8703 417a6e49 2020-09-26 stsp } else {
8704 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(
8705 417a6e49 2020-09-26 stsp &commit, repo, hle->commit_id);
8706 417a6e49 2020-09-26 stsp if (error)
8707 417a6e49 2020-09-26 stsp goto done;
8708 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit,
8709 417a6e49 2020-09-26 stsp hle, NULL);
8710 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8711 417a6e49 2020-09-26 stsp commit = NULL;
8712 417a6e49 2020-09-26 stsp if (error)
8713 417a6e49 2020-09-26 stsp goto done;
8714 417a6e49 2020-09-26 stsp }
8715 417a6e49 2020-09-26 stsp }
8716 417a6e49 2020-09-26 stsp continue;
8717 417a6e49 2020-09-26 stsp }
8718 417a6e49 2020-09-26 stsp
8719 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8720 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8721 417a6e49 2020-09-26 stsp if (error)
8722 417a6e49 2020-09-26 stsp goto done;
8723 417a6e49 2020-09-26 stsp continue;
8724 417a6e49 2020-09-26 stsp }
8725 417a6e49 2020-09-26 stsp
8726 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8727 417a6e49 2020-09-26 stsp hle->commit_id);
8728 417a6e49 2020-09-26 stsp if (error)
8729 417a6e49 2020-09-26 stsp goto done;
8730 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8731 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8732 417a6e49 2020-09-26 stsp
8733 417a6e49 2020-09-26 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8734 417a6e49 2020-09-26 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8735 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
8736 417a6e49 2020-09-26 stsp if (error)
8737 417a6e49 2020-09-26 stsp goto done;
8738 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8739 417a6e49 2020-09-26 stsp commit = NULL;
8740 417a6e49 2020-09-26 stsp
8741 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8742 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
8743 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
8744 417a6e49 2020-09-26 stsp
8745 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8746 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(hle->commit_id,
8747 417a6e49 2020-09-26 stsp repo);
8748 417a6e49 2020-09-26 stsp if (error)
8749 417a6e49 2020-09-26 stsp goto done;
8750 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8751 417a6e49 2020-09-26 stsp break;
8752 417a6e49 2020-09-26 stsp }
8753 417a6e49 2020-09-26 stsp
8754 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8755 417a6e49 2020-09-26 stsp char *id_str;
8756 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, hle->commit_id);
8757 417a6e49 2020-09-26 stsp if (error)
8758 417a6e49 2020-09-26 stsp goto done;
8759 417a6e49 2020-09-26 stsp printf("Stopping histedit for amending commit %s\n",
8760 417a6e49 2020-09-26 stsp id_str);
8761 417a6e49 2020-09-26 stsp free(id_str);
8762 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8763 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree,
8764 417a6e49 2020-09-26 stsp fileindex);
8765 417a6e49 2020-09-26 stsp goto done;
8766 417a6e49 2020-09-26 stsp }
8767 417a6e49 2020-09-26 stsp
8768 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8769 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8770 417a6e49 2020-09-26 stsp if (error)
8771 417a6e49 2020-09-26 stsp goto done;
8772 417a6e49 2020-09-26 stsp continue;
8773 417a6e49 2020-09-26 stsp }
8774 417a6e49 2020-09-26 stsp
8775 417a6e49 2020-09-26 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8776 417a6e49 2020-09-26 stsp tmp_branch, hle, repo);
8777 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8778 417a6e49 2020-09-26 stsp if (error)
8779 417a6e49 2020-09-26 stsp goto done;
8780 417a6e49 2020-09-26 stsp }
8781 417a6e49 2020-09-26 stsp
8782 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8783 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8784 417a6e49 2020-09-26 stsp if (error)
8785 417a6e49 2020-09-26 stsp goto done;
8786 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8787 417a6e49 2020-09-26 stsp "conflicts must be resolved before histedit can continue");
8788 417a6e49 2020-09-26 stsp } else
8789 417a6e49 2020-09-26 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8790 417a6e49 2020-09-26 stsp branch, repo);
8791 417a6e49 2020-09-26 stsp done:
8792 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
8793 417a6e49 2020-09-26 stsp histedit_free_list(&histedit_cmds);
8794 417a6e49 2020-09-26 stsp free(head_commit_id);
8795 417a6e49 2020-09-26 stsp free(base_commit_id);
8796 417a6e49 2020-09-26 stsp free(resume_commit_id);
8797 417a6e49 2020-09-26 stsp if (commit)
8798 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8799 417a6e49 2020-09-26 stsp if (branch)
8800 417a6e49 2020-09-26 stsp got_ref_close(branch);
8801 417a6e49 2020-09-26 stsp if (tmp_branch)
8802 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
8803 417a6e49 2020-09-26 stsp if (worktree)
8804 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8805 417a6e49 2020-09-26 stsp if (repo)
8806 417a6e49 2020-09-26 stsp got_repo_close(repo);
8807 417a6e49 2020-09-26 stsp return error;
8808 417a6e49 2020-09-26 stsp }
8809 417a6e49 2020-09-26 stsp
8810 417a6e49 2020-09-26 stsp __dead static void
8811 417a6e49 2020-09-26 stsp usage_integrate(void)
8812 417a6e49 2020-09-26 stsp {
8813 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8814 417a6e49 2020-09-26 stsp exit(1);
8815 417a6e49 2020-09-26 stsp }
8816 417a6e49 2020-09-26 stsp
8817 417a6e49 2020-09-26 stsp static const struct got_error *
8818 417a6e49 2020-09-26 stsp cmd_integrate(int argc, char *argv[])
8819 417a6e49 2020-09-26 stsp {
8820 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8821 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8822 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8823 417a6e49 2020-09-26 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8824 417a6e49 2020-09-26 stsp const char *branch_arg = NULL;
8825 417a6e49 2020-09-26 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8826 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8827 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8828 417a6e49 2020-09-26 stsp int ch;
8829 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8830 417a6e49 2020-09-26 stsp
8831 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8832 417a6e49 2020-09-26 stsp switch (ch) {
8833 417a6e49 2020-09-26 stsp default:
8834 417a6e49 2020-09-26 stsp usage_integrate();
8835 417a6e49 2020-09-26 stsp /* NOTREACHED */
8836 417a6e49 2020-09-26 stsp }
8837 417a6e49 2020-09-26 stsp }
8838 417a6e49 2020-09-26 stsp
8839 417a6e49 2020-09-26 stsp argc -= optind;
8840 417a6e49 2020-09-26 stsp argv += optind;
8841 417a6e49 2020-09-26 stsp
8842 417a6e49 2020-09-26 stsp if (argc != 1)
8843 417a6e49 2020-09-26 stsp usage_integrate();
8844 417a6e49 2020-09-26 stsp branch_arg = argv[0];
8845 417a6e49 2020-09-26 stsp
8846 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8847 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8848 417a6e49 2020-09-26 stsp err(1, "pledge");
8849 417a6e49 2020-09-26 stsp
8850 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8851 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8852 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8853 417a6e49 2020-09-26 stsp goto done;
8854 417a6e49 2020-09-26 stsp }
8855 417a6e49 2020-09-26 stsp
8856 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8857 417a6e49 2020-09-26 stsp if (error) {
8858 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8859 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "integrate",
8860 417a6e49 2020-09-26 stsp cwd);
8861 417a6e49 2020-09-26 stsp goto done;
8862 417a6e49 2020-09-26 stsp }
8863 417a6e49 2020-09-26 stsp
8864 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
8865 417a6e49 2020-09-26 stsp if (error)
8866 417a6e49 2020-09-26 stsp goto done;
8867 417a6e49 2020-09-26 stsp
8868 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8869 417a6e49 2020-09-26 stsp NULL);
8870 417a6e49 2020-09-26 stsp if (error != NULL)
8871 417a6e49 2020-09-26 stsp goto done;
8872 417a6e49 2020-09-26 stsp
8873 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8874 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
8875 417a6e49 2020-09-26 stsp if (error)
8876 417a6e49 2020-09-26 stsp goto done;
8877 417a6e49 2020-09-26 stsp
8878 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8879 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
8880 417a6e49 2020-09-26 stsp goto done;
8881 417a6e49 2020-09-26 stsp }
8882 417a6e49 2020-09-26 stsp
8883 417a6e49 2020-09-26 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8884 417a6e49 2020-09-26 stsp &base_branch_ref, worktree, refname, repo);
8885 417a6e49 2020-09-26 stsp if (error)
8886 417a6e49 2020-09-26 stsp goto done;
8887 417a6e49 2020-09-26 stsp
8888 417a6e49 2020-09-26 stsp refname = strdup(got_ref_get_name(branch_ref));
8889 417a6e49 2020-09-26 stsp if (refname == NULL) {
8890 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8891 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8892 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8893 417a6e49 2020-09-26 stsp goto done;
8894 417a6e49 2020-09-26 stsp }
8895 417a6e49 2020-09-26 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8896 417a6e49 2020-09-26 stsp if (base_refname == NULL) {
8897 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8898 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8899 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8900 417a6e49 2020-09-26 stsp goto done;
8901 417a6e49 2020-09-26 stsp }
8902 417a6e49 2020-09-26 stsp
8903 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8904 417a6e49 2020-09-26 stsp if (error)
8905 417a6e49 2020-09-26 stsp goto done;
8906 417a6e49 2020-09-26 stsp
8907 417a6e49 2020-09-26 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8908 417a6e49 2020-09-26 stsp if (error)
8909 417a6e49 2020-09-26 stsp goto done;
8910 417a6e49 2020-09-26 stsp
8911 417a6e49 2020-09-26 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8912 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8913 417a6e49 2020-09-26 stsp "specified branch has already been integrated");
8914 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8915 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8916 417a6e49 2020-09-26 stsp goto done;
8917 417a6e49 2020-09-26 stsp }
8918 417a6e49 2020-09-26 stsp
8919 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8920 417a6e49 2020-09-26 stsp if (error) {
8921 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
8922 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8923 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8924 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8925 417a6e49 2020-09-26 stsp goto done;
8926 417a6e49 2020-09-26 stsp }
8927 417a6e49 2020-09-26 stsp
8928 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8929 417a6e49 2020-09-26 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8930 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref, update_progress, &upa,
8931 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8932 417a6e49 2020-09-26 stsp if (error)
8933 417a6e49 2020-09-26 stsp goto done;
8934 417a6e49 2020-09-26 stsp
8935 417a6e49 2020-09-26 stsp printf("Integrated %s into %s\n", refname, base_refname);
8936 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8937 417a6e49 2020-09-26 stsp done:
8938 417a6e49 2020-09-26 stsp if (repo)
8939 417a6e49 2020-09-26 stsp got_repo_close(repo);
8940 417a6e49 2020-09-26 stsp if (worktree)
8941 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8942 417a6e49 2020-09-26 stsp free(cwd);
8943 417a6e49 2020-09-26 stsp free(base_commit_id);
8944 417a6e49 2020-09-26 stsp free(commit_id);
8945 417a6e49 2020-09-26 stsp free(refname);
8946 417a6e49 2020-09-26 stsp free(base_refname);
8947 417a6e49 2020-09-26 stsp return error;
8948 417a6e49 2020-09-26 stsp }
8949 417a6e49 2020-09-26 stsp
8950 417a6e49 2020-09-26 stsp __dead static void
8951 417a6e49 2020-09-26 stsp usage_stage(void)
8952 417a6e49 2020-09-26 stsp {
8953 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8954 417a6e49 2020-09-26 stsp "[-S] [file-path ...]\n",
8955 417a6e49 2020-09-26 stsp getprogname());
8956 417a6e49 2020-09-26 stsp exit(1);
8957 417a6e49 2020-09-26 stsp }
8958 417a6e49 2020-09-26 stsp
8959 417a6e49 2020-09-26 stsp static const struct got_error *
8960 417a6e49 2020-09-26 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8961 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
8962 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8963 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
8964 417a6e49 2020-09-26 stsp {
8965 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8966 417a6e49 2020-09-26 stsp char *id_str = NULL;
8967 417a6e49 2020-09-26 stsp
8968 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_ADD &&
8969 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_MODIFY &&
8970 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
8971 417a6e49 2020-09-26 stsp return NULL;
8972 417a6e49 2020-09-26 stsp
8973 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
8974 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY)
8975 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
8976 417a6e49 2020-09-26 stsp else
8977 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
8978 417a6e49 2020-09-26 stsp if (err)
8979 417a6e49 2020-09-26 stsp return err;
8980 417a6e49 2020-09-26 stsp
8981 417a6e49 2020-09-26 stsp printf("%s %c %s\n", id_str, staged_status, path);
8982 417a6e49 2020-09-26 stsp free(id_str);
8983 417a6e49 2020-09-26 stsp return NULL;
8984 417a6e49 2020-09-26 stsp }
8985 417a6e49 2020-09-26 stsp
8986 417a6e49 2020-09-26 stsp static const struct got_error *
8987 417a6e49 2020-09-26 stsp cmd_stage(int argc, char *argv[])
8988 417a6e49 2020-09-26 stsp {
8989 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8990 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8991 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8992 417a6e49 2020-09-26 stsp char *cwd = NULL;
8993 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8994 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
8995 417a6e49 2020-09-26 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8996 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
8997 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
8998 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
8999 417a6e49 2020-09-26 stsp
9000 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9001 417a6e49 2020-09-26 stsp
9002 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9003 417a6e49 2020-09-26 stsp switch (ch) {
9004 417a6e49 2020-09-26 stsp case 'l':
9005 417a6e49 2020-09-26 stsp list_stage = 1;
9006 417a6e49 2020-09-26 stsp break;
9007 417a6e49 2020-09-26 stsp case 'p':
9008 417a6e49 2020-09-26 stsp pflag = 1;
9009 417a6e49 2020-09-26 stsp break;
9010 417a6e49 2020-09-26 stsp case 'F':
9011 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9012 417a6e49 2020-09-26 stsp break;
9013 417a6e49 2020-09-26 stsp case 'S':
9014 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
9015 417a6e49 2020-09-26 stsp break;
9016 417a6e49 2020-09-26 stsp default:
9017 417a6e49 2020-09-26 stsp usage_stage();
9018 417a6e49 2020-09-26 stsp /* NOTREACHED */
9019 417a6e49 2020-09-26 stsp }
9020 417a6e49 2020-09-26 stsp }
9021 417a6e49 2020-09-26 stsp
9022 417a6e49 2020-09-26 stsp argc -= optind;
9023 417a6e49 2020-09-26 stsp argv += optind;
9024 417a6e49 2020-09-26 stsp
9025 417a6e49 2020-09-26 stsp #ifndef PROFILE
9026 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9027 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9028 417a6e49 2020-09-26 stsp err(1, "pledge");
9029 417a6e49 2020-09-26 stsp #endif
9030 417a6e49 2020-09-26 stsp if (list_stage && (pflag || patch_script_path))
9031 417a6e49 2020-09-26 stsp errx(1, "-l option cannot be used with other options");
9032 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9033 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9034 417a6e49 2020-09-26 stsp
9035 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9036 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9037 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9038 417a6e49 2020-09-26 stsp goto done;
9039 417a6e49 2020-09-26 stsp }
9040 417a6e49 2020-09-26 stsp
9041 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9042 417a6e49 2020-09-26 stsp if (error) {
9043 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9044 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9045 417a6e49 2020-09-26 stsp goto done;
9046 417a6e49 2020-09-26 stsp }
9047 417a6e49 2020-09-26 stsp
9048 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9049 417a6e49 2020-09-26 stsp NULL);
9050 417a6e49 2020-09-26 stsp if (error != NULL)
9051 417a6e49 2020-09-26 stsp goto done;
9052 417a6e49 2020-09-26 stsp
9053 417a6e49 2020-09-26 stsp if (patch_script_path) {
9054 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9055 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9056 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9057 417a6e49 2020-09-26 stsp patch_script_path);
9058 417a6e49 2020-09-26 stsp goto done;
9059 417a6e49 2020-09-26 stsp }
9060 417a6e49 2020-09-26 stsp }
9061 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9062 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9063 417a6e49 2020-09-26 stsp if (error)
9064 417a6e49 2020-09-26 stsp goto done;
9065 417a6e49 2020-09-26 stsp
9066 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9067 417a6e49 2020-09-26 stsp if (error)
9068 417a6e49 2020-09-26 stsp goto done;
9069 417a6e49 2020-09-26 stsp
9070 417a6e49 2020-09-26 stsp if (list_stage)
9071 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo,
9072 417a6e49 2020-09-26 stsp print_stage, NULL, check_cancelled, NULL);
9073 417a6e49 2020-09-26 stsp else {
9074 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9075 417a6e49 2020-09-26 stsp cpa.action = "stage";
9076 417a6e49 2020-09-26 stsp error = got_worktree_stage(worktree, &paths,
9077 417a6e49 2020-09-26 stsp pflag ? NULL : print_status, NULL,
9078 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa,
9079 417a6e49 2020-09-26 stsp allow_bad_symlinks, repo);
9080 417a6e49 2020-09-26 stsp }
9081 417a6e49 2020-09-26 stsp done:
9082 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9083 417a6e49 2020-09-26 stsp error == NULL)
9084 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9085 417a6e49 2020-09-26 stsp if (repo)
9086 417a6e49 2020-09-26 stsp got_repo_close(repo);
9087 417a6e49 2020-09-26 stsp if (worktree)
9088 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9089 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9090 417a6e49 2020-09-26 stsp free((char *)pe->path);
9091 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9092 417a6e49 2020-09-26 stsp free(cwd);
9093 417a6e49 2020-09-26 stsp return error;
9094 417a6e49 2020-09-26 stsp }
9095 417a6e49 2020-09-26 stsp
9096 417a6e49 2020-09-26 stsp __dead static void
9097 417a6e49 2020-09-26 stsp usage_unstage(void)
9098 417a6e49 2020-09-26 stsp {
9099 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9100 417a6e49 2020-09-26 stsp "[file-path ...]\n",
9101 417a6e49 2020-09-26 stsp getprogname());
9102 417a6e49 2020-09-26 stsp exit(1);
9103 417a6e49 2020-09-26 stsp }
9104 417a6e49 2020-09-26 stsp
9105 417a6e49 2020-09-26 stsp
9106 417a6e49 2020-09-26 stsp static const struct got_error *
9107 417a6e49 2020-09-26 stsp cmd_unstage(int argc, char *argv[])
9108 417a6e49 2020-09-26 stsp {
9109 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9110 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9111 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9112 417a6e49 2020-09-26 stsp char *cwd = NULL;
9113 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9114 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9115 417a6e49 2020-09-26 stsp int ch, pflag = 0;
9116 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
9117 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
9118 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
9119 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
9120 417a6e49 2020-09-26 stsp
9121 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9122 417a6e49 2020-09-26 stsp
9123 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9124 417a6e49 2020-09-26 stsp switch (ch) {
9125 417a6e49 2020-09-26 stsp case 'p':
9126 417a6e49 2020-09-26 stsp pflag = 1;
9127 417a6e49 2020-09-26 stsp break;
9128 417a6e49 2020-09-26 stsp case 'F':
9129 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9130 417a6e49 2020-09-26 stsp break;
9131 417a6e49 2020-09-26 stsp default:
9132 417a6e49 2020-09-26 stsp usage_unstage();
9133 417a6e49 2020-09-26 stsp /* NOTREACHED */
9134 417a6e49 2020-09-26 stsp }
9135 417a6e49 2020-09-26 stsp }
9136 417a6e49 2020-09-26 stsp
9137 417a6e49 2020-09-26 stsp argc -= optind;
9138 417a6e49 2020-09-26 stsp argv += optind;
9139 417a6e49 2020-09-26 stsp
9140 417a6e49 2020-09-26 stsp #ifndef PROFILE
9141 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9142 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9143 417a6e49 2020-09-26 stsp err(1, "pledge");
9144 417a6e49 2020-09-26 stsp #endif
9145 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9146 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9147 417a6e49 2020-09-26 stsp
9148 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9149 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9150 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9151 417a6e49 2020-09-26 stsp goto done;
9152 417a6e49 2020-09-26 stsp }
9153 417a6e49 2020-09-26 stsp
9154 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9155 417a6e49 2020-09-26 stsp if (error) {
9156 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9157 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9158 417a6e49 2020-09-26 stsp goto done;
9159 417a6e49 2020-09-26 stsp }
9160 417a6e49 2020-09-26 stsp
9161 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9162 417a6e49 2020-09-26 stsp NULL);
9163 417a6e49 2020-09-26 stsp if (error != NULL)
9164 417a6e49 2020-09-26 stsp goto done;
9165 417a6e49 2020-09-26 stsp
9166 417a6e49 2020-09-26 stsp if (patch_script_path) {
9167 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9168 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9169 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9170 417a6e49 2020-09-26 stsp patch_script_path);
9171 417a6e49 2020-09-26 stsp goto done;
9172 417a6e49 2020-09-26 stsp }
9173 417a6e49 2020-09-26 stsp }
9174 417a6e49 2020-09-26 stsp
9175 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9176 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9177 417a6e49 2020-09-26 stsp if (error)
9178 417a6e49 2020-09-26 stsp goto done;
9179 417a6e49 2020-09-26 stsp
9180 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9181 417a6e49 2020-09-26 stsp if (error)
9182 417a6e49 2020-09-26 stsp goto done;
9183 417a6e49 2020-09-26 stsp
9184 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9185 417a6e49 2020-09-26 stsp cpa.action = "unstage";
9186 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
9187 417a6e49 2020-09-26 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9188 417a6e49 2020-09-26 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9189 417a6e49 2020-09-26 stsp if (!error)
9190 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
9191 417a6e49 2020-09-26 stsp done:
9192 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9193 417a6e49 2020-09-26 stsp error == NULL)
9194 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9195 417a6e49 2020-09-26 stsp if (repo)
9196 417a6e49 2020-09-26 stsp got_repo_close(repo);
9197 417a6e49 2020-09-26 stsp if (worktree)
9198 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9199 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9200 417a6e49 2020-09-26 stsp free((char *)pe->path);
9201 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9202 417a6e49 2020-09-26 stsp free(cwd);
9203 417a6e49 2020-09-26 stsp return error;
9204 417a6e49 2020-09-26 stsp }
9205 417a6e49 2020-09-26 stsp
9206 417a6e49 2020-09-26 stsp __dead static void
9207 417a6e49 2020-09-26 stsp usage_cat(void)
9208 417a6e49 2020-09-26 stsp {
9209 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9210 417a6e49 2020-09-26 stsp "arg1 [arg2 ...]\n", getprogname());
9211 417a6e49 2020-09-26 stsp exit(1);
9212 417a6e49 2020-09-26 stsp }
9213 417a6e49 2020-09-26 stsp
9214 417a6e49 2020-09-26 stsp static const struct got_error *
9215 417a6e49 2020-09-26 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9216 417a6e49 2020-09-26 stsp {
9217 417a6e49 2020-09-26 stsp const struct got_error *err;
9218 417a6e49 2020-09-26 stsp struct got_blob_object *blob;
9219 417a6e49 2020-09-26 stsp
9220 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9221 417a6e49 2020-09-26 stsp if (err)
9222 417a6e49 2020-09-26 stsp return err;
9223 417a6e49 2020-09-26 stsp
9224 417a6e49 2020-09-26 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9225 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
9226 417a6e49 2020-09-26 stsp return err;
9227 417a6e49 2020-09-26 stsp }
9228 417a6e49 2020-09-26 stsp
9229 417a6e49 2020-09-26 stsp static const struct got_error *
9230 417a6e49 2020-09-26 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9231 417a6e49 2020-09-26 stsp {
9232 417a6e49 2020-09-26 stsp const struct got_error *err;
9233 417a6e49 2020-09-26 stsp struct got_tree_object *tree;
9234 417a6e49 2020-09-26 stsp int nentries, i;
9235 417a6e49 2020-09-26 stsp
9236 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, id);
9237 417a6e49 2020-09-26 stsp if (err)
9238 417a6e49 2020-09-26 stsp return err;
9239 417a6e49 2020-09-26 stsp
9240 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
9241 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
9242 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
9243 417a6e49 2020-09-26 stsp char *id_str;
9244 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
9245 417a6e49 2020-09-26 stsp break;
9246 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
9247 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9248 417a6e49 2020-09-26 stsp if (err)
9249 417a6e49 2020-09-26 stsp break;
9250 417a6e49 2020-09-26 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9251 417a6e49 2020-09-26 stsp got_tree_entry_get_mode(te),
9252 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te));
9253 417a6e49 2020-09-26 stsp free(id_str);
9254 417a6e49 2020-09-26 stsp }
9255 417a6e49 2020-09-26 stsp
9256 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
9257 417a6e49 2020-09-26 stsp return err;
9258 417a6e49 2020-09-26 stsp }
9259 417a6e49 2020-09-26 stsp
9260 417a6e49 2020-09-26 stsp static const struct got_error *
9261 417a6e49 2020-09-26 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9262 417a6e49 2020-09-26 stsp {
9263 417a6e49 2020-09-26 stsp const struct got_error *err;
9264 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
9265 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
9266 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
9267 417a6e49 2020-09-26 stsp char *id_str = NULL;
9268 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
9269 417a6e49 2020-09-26 stsp
9270 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
9271 417a6e49 2020-09-26 stsp if (err)
9272 417a6e49 2020-09-26 stsp return err;
9273 417a6e49 2020-09-26 stsp
9274 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9275 417a6e49 2020-09-26 stsp if (err)
9276 417a6e49 2020-09-26 stsp goto done;
9277 417a6e49 2020-09-26 stsp
9278 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9279 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9280 417a6e49 2020-09-26 stsp fprintf(outfile, "numparents %d\n",
9281 417a6e49 2020-09-26 stsp got_object_commit_get_nparents(commit));
9282 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9283 417a6e49 2020-09-26 stsp char *pid_str;
9284 417a6e49 2020-09-26 stsp err = got_object_id_str(&pid_str, pid->id);
9285 417a6e49 2020-09-26 stsp if (err)
9286 417a6e49 2020-09-26 stsp goto done;
9287 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9288 417a6e49 2020-09-26 stsp free(pid_str);
9289 417a6e49 2020-09-26 stsp }
9290 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9291 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9292 417a6e49 2020-09-26 stsp got_object_commit_get_author_time(commit));
9293 417a6e49 2020-09-26 stsp
9294 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9295 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9296 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit));
9297 417a6e49 2020-09-26 stsp
9298 417a6e49 2020-09-26 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9299 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9300 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", logmsg);
9301 417a6e49 2020-09-26 stsp done:
9302 417a6e49 2020-09-26 stsp free(id_str);
9303 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
9304 417a6e49 2020-09-26 stsp return err;
9305 417a6e49 2020-09-26 stsp }
9306 417a6e49 2020-09-26 stsp
9307 417a6e49 2020-09-26 stsp static const struct got_error *
9308 417a6e49 2020-09-26 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9309 417a6e49 2020-09-26 stsp {
9310 417a6e49 2020-09-26 stsp const struct got_error *err;
9311 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
9312 417a6e49 2020-09-26 stsp char *id_str = NULL;
9313 417a6e49 2020-09-26 stsp const char *tagmsg = NULL;
9314 417a6e49 2020-09-26 stsp
9315 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
9316 417a6e49 2020-09-26 stsp if (err)
9317 417a6e49 2020-09-26 stsp return err;
9318 417a6e49 2020-09-26 stsp
9319 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9320 417a6e49 2020-09-26 stsp if (err)
9321 417a6e49 2020-09-26 stsp goto done;
9322 417a6e49 2020-09-26 stsp
9323 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9324 417a6e49 2020-09-26 stsp
9325 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
9326 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9327 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9328 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_BLOB);
9329 417a6e49 2020-09-26 stsp break;
9330 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9331 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9332 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TREE);
9333 417a6e49 2020-09-26 stsp break;
9334 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9335 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9336 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_COMMIT);
9337 417a6e49 2020-09-26 stsp break;
9338 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9339 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9340 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TAG);
9341 417a6e49 2020-09-26 stsp break;
9342 417a6e49 2020-09-26 stsp default:
9343 417a6e49 2020-09-26 stsp break;
9344 417a6e49 2020-09-26 stsp }
9345 417a6e49 2020-09-26 stsp
9346 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9347 417a6e49 2020-09-26 stsp got_object_tag_get_name(tag));
9348 417a6e49 2020-09-26 stsp
9349 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9350 417a6e49 2020-09-26 stsp got_object_tag_get_tagger(tag),
9351 417a6e49 2020-09-26 stsp got_object_tag_get_tagger_time(tag));
9352 417a6e49 2020-09-26 stsp
9353 417a6e49 2020-09-26 stsp tagmsg = got_object_tag_get_message(tag);
9354 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9355 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", tagmsg);
9356 417a6e49 2020-09-26 stsp done:
9357 417a6e49 2020-09-26 stsp free(id_str);
9358 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
9359 417a6e49 2020-09-26 stsp return err;
9360 417a6e49 2020-09-26 stsp }
9361 417a6e49 2020-09-26 stsp
9362 417a6e49 2020-09-26 stsp static const struct got_error *
9363 417a6e49 2020-09-26 stsp cmd_cat(int argc, char *argv[])
9364 417a6e49 2020-09-26 stsp {
9365 417a6e49 2020-09-26 stsp const struct got_error *error;
9366 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9367 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9368 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9369 417a6e49 2020-09-26 stsp const char *commit_id_str = NULL;
9370 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9371 417a6e49 2020-09-26 stsp int ch, obj_type, i, force_path = 0;
9372 417a6e49 2020-09-26 stsp
9373 417a6e49 2020-09-26 stsp #ifndef PROFILE
9374 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9375 417a6e49 2020-09-26 stsp NULL) == -1)
9376 417a6e49 2020-09-26 stsp err(1, "pledge");
9377 417a6e49 2020-09-26 stsp #endif
9378 417a6e49 2020-09-26 stsp
9379 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9380 417a6e49 2020-09-26 stsp switch (ch) {
9381 417a6e49 2020-09-26 stsp case 'c':
9382 417a6e49 2020-09-26 stsp commit_id_str = optarg;
9383 417a6e49 2020-09-26 stsp break;
9384 417a6e49 2020-09-26 stsp case 'r':
9385 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
9386 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9387 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
9388 417a6e49 2020-09-26 stsp optarg);
9389 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
9390 417a6e49 2020-09-26 stsp break;
9391 417a6e49 2020-09-26 stsp case 'P':
9392 417a6e49 2020-09-26 stsp force_path = 1;
9393 417a6e49 2020-09-26 stsp break;
9394 417a6e49 2020-09-26 stsp default:
9395 417a6e49 2020-09-26 stsp usage_cat();
9396 417a6e49 2020-09-26 stsp /* NOTREACHED */
9397 417a6e49 2020-09-26 stsp }
9398 417a6e49 2020-09-26 stsp }
9399 417a6e49 2020-09-26 stsp
9400 417a6e49 2020-09-26 stsp argc -= optind;
9401 417a6e49 2020-09-26 stsp argv += optind;
9402 417a6e49 2020-09-26 stsp
9403 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9404 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9405 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9406 417a6e49 2020-09-26 stsp goto done;
9407 417a6e49 2020-09-26 stsp }
9408 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9409 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9410 417a6e49 2020-09-26 stsp goto done;
9411 417a6e49 2020-09-26 stsp if (worktree) {
9412 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9413 417a6e49 2020-09-26 stsp repo_path = strdup(
9414 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
9415 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9416 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
9417 417a6e49 2020-09-26 stsp goto done;
9418 417a6e49 2020-09-26 stsp }
9419 417a6e49 2020-09-26 stsp }
9420 417a6e49 2020-09-26 stsp }
9421 417a6e49 2020-09-26 stsp
9422 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9423 417a6e49 2020-09-26 stsp repo_path = getcwd(NULL, 0);
9424 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9425 417a6e49 2020-09-26 stsp return got_error_from_errno("getcwd");
9426 417a6e49 2020-09-26 stsp }
9427 417a6e49 2020-09-26 stsp
9428 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
9429 417a6e49 2020-09-26 stsp free(repo_path);
9430 417a6e49 2020-09-26 stsp if (error != NULL)
9431 417a6e49 2020-09-26 stsp goto done;
9432 417a6e49 2020-09-26 stsp
9433 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9434 417a6e49 2020-09-26 stsp if (error)
9435 417a6e49 2020-09-26 stsp goto done;
9436 417a6e49 2020-09-26 stsp
9437 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
9438 417a6e49 2020-09-26 stsp commit_id_str = GOT_REF_HEAD;
9439 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
9440 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9441 417a6e49 2020-09-26 stsp if (error)
9442 417a6e49 2020-09-26 stsp goto done;
9443 417a6e49 2020-09-26 stsp
9444 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
9445 417a6e49 2020-09-26 stsp if (force_path) {
9446 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9447 417a6e49 2020-09-26 stsp argv[i]);
9448 417a6e49 2020-09-26 stsp if (error)
9449 417a6e49 2020-09-26 stsp break;
9450 417a6e49 2020-09-26 stsp } else {
9451 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9452 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9453 417a6e49 2020-09-26 stsp if (error) {
9454 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9455 417a6e49 2020-09-26 stsp error->code != GOT_ERR_NOT_REF)
9456 417a6e49 2020-09-26 stsp break;
9457 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo,
9458 417a6e49 2020-09-26 stsp commit_id, argv[i]);
9459 417a6e49 2020-09-26 stsp if (error)
9460 417a6e49 2020-09-26 stsp break;
9461 417a6e49 2020-09-26 stsp }
9462 417a6e49 2020-09-26 stsp }
9463 417a6e49 2020-09-26 stsp
9464 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, id);
9465 417a6e49 2020-09-26 stsp if (error)
9466 417a6e49 2020-09-26 stsp break;
9467 417a6e49 2020-09-26 stsp
9468 417a6e49 2020-09-26 stsp switch (obj_type) {
9469 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9470 417a6e49 2020-09-26 stsp error = cat_blob(id, repo, stdout);
9471 417a6e49 2020-09-26 stsp break;
9472 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9473 417a6e49 2020-09-26 stsp error = cat_tree(id, repo, stdout);
9474 417a6e49 2020-09-26 stsp break;
9475 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9476 417a6e49 2020-09-26 stsp error = cat_commit(id, repo, stdout);
9477 417a6e49 2020-09-26 stsp break;
9478 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9479 417a6e49 2020-09-26 stsp error = cat_tag(id, repo, stdout);
9480 417a6e49 2020-09-26 stsp break;
9481 417a6e49 2020-09-26 stsp default:
9482 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9483 417a6e49 2020-09-26 stsp break;
9484 417a6e49 2020-09-26 stsp }
9485 417a6e49 2020-09-26 stsp if (error)
9486 417a6e49 2020-09-26 stsp break;
9487 417a6e49 2020-09-26 stsp free(label);
9488 417a6e49 2020-09-26 stsp label = NULL;
9489 417a6e49 2020-09-26 stsp free(id);
9490 417a6e49 2020-09-26 stsp id = NULL;
9491 417a6e49 2020-09-26 stsp }
9492 417a6e49 2020-09-26 stsp done:
9493 417a6e49 2020-09-26 stsp free(label);
9494 417a6e49 2020-09-26 stsp free(id);
9495 417a6e49 2020-09-26 stsp free(commit_id);
9496 417a6e49 2020-09-26 stsp if (worktree)
9497 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9498 417a6e49 2020-09-26 stsp if (repo) {
9499 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
9500 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
9501 417a6e49 2020-09-26 stsp if (error == NULL)
9502 417a6e49 2020-09-26 stsp error = repo_error;
9503 417a6e49 2020-09-26 stsp }
9504 417a6e49 2020-09-26 stsp return error;
9505 417a6e49 2020-09-26 stsp }
9506 417a6e49 2020-09-26 stsp
9507 417a6e49 2020-09-26 stsp __dead static void
9508 417a6e49 2020-09-26 stsp usage_info(void)
9509 417a6e49 2020-09-26 stsp {
9510 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9511 417a6e49 2020-09-26 stsp getprogname());
9512 417a6e49 2020-09-26 stsp exit(1);
9513 417a6e49 2020-09-26 stsp }
9514 417a6e49 2020-09-26 stsp
9515 417a6e49 2020-09-26 stsp static const struct got_error *
9516 417a6e49 2020-09-26 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9517 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9518 417a6e49 2020-09-26 stsp struct got_object_id *commit_id)
9519 417a6e49 2020-09-26 stsp {
9520 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
9521 417a6e49 2020-09-26 stsp char *id_str = NULL;
9522 417a6e49 2020-09-26 stsp char datebuf[128];
9523 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
9524 417a6e49 2020-09-26 stsp struct got_pathlist_head *paths = arg;
9525 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9526 417a6e49 2020-09-26 stsp
9527 417a6e49 2020-09-26 stsp /*
9528 417a6e49 2020-09-26 stsp * Clear error indication from any of the path arguments which
9529 417a6e49 2020-09-26 stsp * would cause this file index entry to be displayed.
9530 417a6e49 2020-09-26 stsp */
9531 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, paths, entry) {
9532 417a6e49 2020-09-26 stsp if (got_path_cmp(path, pe->path, strlen(path),
9533 417a6e49 2020-09-26 stsp pe->path_len) == 0 ||
9534 417a6e49 2020-09-26 stsp got_path_is_child(path, pe->path, pe->path_len))
9535 417a6e49 2020-09-26 stsp pe->data = NULL; /* no error */
9536 417a6e49 2020-09-26 stsp }
9537 417a6e49 2020-09-26 stsp
9538 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
9539 417a6e49 2020-09-26 stsp if (S_ISLNK(mode))
9540 417a6e49 2020-09-26 stsp printf("symlink: %s\n", path);
9541 417a6e49 2020-09-26 stsp else if (S_ISREG(mode)) {
9542 417a6e49 2020-09-26 stsp printf("file: %s\n", path);
9543 417a6e49 2020-09-26 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9544 417a6e49 2020-09-26 stsp } else if (S_ISDIR(mode))
9545 417a6e49 2020-09-26 stsp printf("directory: %s\n", path);
9546 417a6e49 2020-09-26 stsp else
9547 417a6e49 2020-09-26 stsp printf("something: %s\n", path);
9548 417a6e49 2020-09-26 stsp
9549 417a6e49 2020-09-26 stsp tm = localtime_r(&mtime, &mytm);
9550 417a6e49 2020-09-26 stsp if (tm == NULL)
9551 417a6e49 2020-09-26 stsp return NULL;
9552 417a6e49 2020-09-26 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9553 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NO_SPACE);
9554 417a6e49 2020-09-26 stsp printf("timestamp: %s\n", datebuf);
9555 417a6e49 2020-09-26 stsp
9556 417a6e49 2020-09-26 stsp if (blob_id) {
9557 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
9558 417a6e49 2020-09-26 stsp if (err)
9559 417a6e49 2020-09-26 stsp return err;
9560 417a6e49 2020-09-26 stsp printf("based on blob: %s\n", id_str);
9561 417a6e49 2020-09-26 stsp free(id_str);
9562 417a6e49 2020-09-26 stsp }
9563 417a6e49 2020-09-26 stsp
9564 417a6e49 2020-09-26 stsp if (staged_blob_id) {
9565 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
9566 417a6e49 2020-09-26 stsp if (err)
9567 417a6e49 2020-09-26 stsp return err;
9568 417a6e49 2020-09-26 stsp printf("based on staged blob: %s\n", id_str);
9569 417a6e49 2020-09-26 stsp free(id_str);
9570 417a6e49 2020-09-26 stsp }
9571 417a6e49 2020-09-26 stsp
9572 417a6e49 2020-09-26 stsp if (commit_id) {
9573 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
9574 417a6e49 2020-09-26 stsp if (err)
9575 417a6e49 2020-09-26 stsp return err;
9576 417a6e49 2020-09-26 stsp printf("based on commit: %s\n", id_str);
9577 417a6e49 2020-09-26 stsp free(id_str);
9578 417a6e49 2020-09-26 stsp }
9579 417a6e49 2020-09-26 stsp
9580 417a6e49 2020-09-26 stsp return NULL;
9581 417a6e49 2020-09-26 stsp }
9582 417a6e49 2020-09-26 stsp
9583 417a6e49 2020-09-26 stsp static const struct got_error *
9584 417a6e49 2020-09-26 stsp cmd_info(int argc, char *argv[])
9585 417a6e49 2020-09-26 stsp {
9586 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9587 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9588 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
9589 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9590 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9591 417a6e49 2020-09-26 stsp char *uuidstr = NULL;
9592 417a6e49 2020-09-26 stsp int ch, show_files = 0;
9593 417a6e49 2020-09-26 stsp
9594 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9595 417a6e49 2020-09-26 stsp
9596 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9597 417a6e49 2020-09-26 stsp switch (ch) {
9598 417a6e49 2020-09-26 stsp default:
9599 417a6e49 2020-09-26 stsp usage_info();
9600 417a6e49 2020-09-26 stsp /* NOTREACHED */
9601 417a6e49 2020-09-26 stsp }
9602 417a6e49 2020-09-26 stsp }
9603 417a6e49 2020-09-26 stsp
9604 417a6e49 2020-09-26 stsp argc -= optind;
9605 417a6e49 2020-09-26 stsp argv += optind;
9606 417a6e49 2020-09-26 stsp
9607 417a6e49 2020-09-26 stsp #ifndef PROFILE
9608 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9609 417a6e49 2020-09-26 stsp NULL) == -1)
9610 417a6e49 2020-09-26 stsp err(1, "pledge");
9611 417a6e49 2020-09-26 stsp #endif
9612 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9613 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9614 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9615 417a6e49 2020-09-26 stsp goto done;
9616 417a6e49 2020-09-26 stsp }
9617 417a6e49 2020-09-26 stsp
9618 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9619 417a6e49 2020-09-26 stsp if (error) {
9620 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9621 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
9622 417a6e49 2020-09-26 stsp goto done;
9623 417a6e49 2020-09-26 stsp }
9624 417a6e49 2020-09-26 stsp
9625 417a6e49 2020-09-26 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9626 417a6e49 2020-09-26 stsp if (error)
9627 417a6e49 2020-09-26 stsp goto done;
9628 417a6e49 2020-09-26 stsp
9629 417a6e49 2020-09-26 stsp if (argc >= 1) {
9630 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9631 417a6e49 2020-09-26 stsp worktree);
9632 417a6e49 2020-09-26 stsp if (error)
9633 417a6e49 2020-09-26 stsp goto done;
9634 417a6e49 2020-09-26 stsp show_files = 1;
9635 417a6e49 2020-09-26 stsp }
9636 417a6e49 2020-09-26 stsp
9637 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
9638 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
9639 417a6e49 2020-09-26 stsp if (error)
9640 417a6e49 2020-09-26 stsp goto done;
9641 417a6e49 2020-09-26 stsp
9642 417a6e49 2020-09-26 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9643 417a6e49 2020-09-26 stsp if (error)
9644 417a6e49 2020-09-26 stsp goto done;
9645 417a6e49 2020-09-26 stsp
9646 417a6e49 2020-09-26 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9647 417a6e49 2020-09-26 stsp printf("work tree base commit: %s\n", id_str);
9648 417a6e49 2020-09-26 stsp printf("work tree path prefix: %s\n",
9649 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree));
9650 417a6e49 2020-09-26 stsp printf("work tree branch reference: %s\n",
9651 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
9652 417a6e49 2020-09-26 stsp printf("work tree UUID: %s\n", uuidstr);
9653 417a6e49 2020-09-26 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9654 417a6e49 2020-09-26 stsp
9655 417a6e49 2020-09-26 stsp if (show_files) {
9656 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9657 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9658 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
9659 417a6e49 2020-09-26 stsp continue;
9660 417a6e49 2020-09-26 stsp /*
9661 417a6e49 2020-09-26 stsp * Assume this path will fail. This will be corrected
9662 417a6e49 2020-09-26 stsp * in print_path_info() in case the path does suceeed.
9663 417a6e49 2020-09-26 stsp */
9664 417a6e49 2020-09-26 stsp pe->data = (void *)got_error_path(pe->path,
9665 417a6e49 2020-09-26 stsp GOT_ERR_BAD_PATH);
9666 417a6e49 2020-09-26 stsp }
9667 417a6e49 2020-09-26 stsp error = got_worktree_path_info(worktree, &paths,
9668 417a6e49 2020-09-26 stsp print_path_info, &paths, check_cancelled, NULL);
9669 417a6e49 2020-09-26 stsp if (error)
9670 417a6e49 2020-09-26 stsp goto done;
9671 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9672 417a6e49 2020-09-26 stsp if (pe->data != NULL) {
9673 417a6e49 2020-09-26 stsp error = pe->data; /* bad path */
9674 417a6e49 2020-09-26 stsp break;
9675 417a6e49 2020-09-26 stsp }
9676 417a6e49 2020-09-26 stsp }
9677 417a6e49 2020-09-26 stsp }
9678 417a6e49 2020-09-26 stsp done:
9679 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9680 417a6e49 2020-09-26 stsp free((char *)pe->path);
9681 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9682 417a6e49 2020-09-26 stsp free(cwd);
9683 417a6e49 2020-09-26 stsp free(id_str);
9684 417a6e49 2020-09-26 stsp free(uuidstr);
9685 417a6e49 2020-09-26 stsp return error;
9686 417a6e49 2020-09-26 stsp }